]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/in6.c
Update our copy of DTS from the ones from Linux 4.14
[FreeBSD/FreeBSD.git] / sys / netinet6 / in6.c
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $
30  */
31
32 /*-
33  * Copyright (c) 1982, 1986, 1991, 1993
34  *      The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *      @(#)in.c        8.2 (Berkeley) 11/15/93
61  */
62
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
65
66 #include "opt_compat.h"
67 #include "opt_inet.h"
68 #include "opt_inet6.h"
69
70 #include <sys/param.h>
71 #include <sys/eventhandler.h>
72 #include <sys/errno.h>
73 #include <sys/jail.h>
74 #include <sys/malloc.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/sockio.h>
78 #include <sys/systm.h>
79 #include <sys/priv.h>
80 #include <sys/proc.h>
81 #include <sys/time.h>
82 #include <sys/kernel.h>
83 #include <sys/lock.h>
84 #include <sys/rmlock.h>
85 #include <sys/syslog.h>
86
87 #include <net/if.h>
88 #include <net/if_var.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
91 #include <net/if_dl.h>
92 #include <net/vnet.h>
93
94 #include <netinet/in.h>
95 #include <netinet/in_var.h>
96 #include <net/if_llatbl.h>
97 #include <netinet/if_ether.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/ip.h>
100 #include <netinet/in_pcb.h>
101 #include <netinet/ip_carp.h>
102
103 #include <netinet/ip6.h>
104 #include <netinet6/ip6_var.h>
105 #include <netinet6/nd6.h>
106 #include <netinet6/mld6_var.h>
107 #include <netinet6/ip6_mroute.h>
108 #include <netinet6/in6_ifattach.h>
109 #include <netinet6/scope6_var.h>
110 #include <netinet6/in6_fib.h>
111 #include <netinet6/in6_pcb.h>
112
113 VNET_DECLARE(int, icmp6_nodeinfo_oldmcprefix);
114 #define V_icmp6_nodeinfo_oldmcprefix    VNET(icmp6_nodeinfo_oldmcprefix)
115
116 /*
117  * Definitions of some costant IP6 addresses.
118  */
119 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
120 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
121 const struct in6_addr in6addr_nodelocal_allnodes =
122         IN6ADDR_NODELOCAL_ALLNODES_INIT;
123 const struct in6_addr in6addr_linklocal_allnodes =
124         IN6ADDR_LINKLOCAL_ALLNODES_INIT;
125 const struct in6_addr in6addr_linklocal_allrouters =
126         IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
127 const struct in6_addr in6addr_linklocal_allv2routers =
128         IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
129
130 const struct in6_addr in6mask0 = IN6MASK0;
131 const struct in6_addr in6mask32 = IN6MASK32;
132 const struct in6_addr in6mask64 = IN6MASK64;
133 const struct in6_addr in6mask96 = IN6MASK96;
134 const struct in6_addr in6mask128 = IN6MASK128;
135
136 const struct sockaddr_in6 sa6_any =
137         { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
138
139 static int in6_notify_ifa(struct ifnet *, struct in6_ifaddr *,
140         struct in6_aliasreq *, int);
141 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
142
143 static int in6_validate_ifra(struct ifnet *, struct in6_aliasreq *,
144     struct in6_ifaddr *, int);
145 static struct in6_ifaddr *in6_alloc_ifa(struct ifnet *,
146     struct in6_aliasreq *, int flags);
147 static int in6_update_ifa_internal(struct ifnet *, struct in6_aliasreq *,
148     struct in6_ifaddr *, int, int);
149 static int in6_broadcast_ifa(struct ifnet *, struct in6_aliasreq *,
150     struct in6_ifaddr *, int);
151
152 #define ifa2ia6(ifa)    ((struct in6_ifaddr *)(ifa))
153 #define ia62ifa(ia6)    (&((ia6)->ia_ifa))
154
155
156 void
157 in6_newaddrmsg(struct in6_ifaddr *ia, int cmd)
158 {
159         struct sockaddr_dl gateway;
160         struct sockaddr_in6 mask, addr;
161         struct rtentry rt;
162         int fibnum;
163
164         /*
165          * initialize for rtmsg generation
166          */
167         bzero(&gateway, sizeof(gateway));
168         gateway.sdl_len = sizeof(gateway);
169         gateway.sdl_family = AF_LINK;
170
171         bzero(&rt, sizeof(rt));
172         rt.rt_gateway = (struct sockaddr *)&gateway;
173         memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
174         memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
175         rt_mask(&rt) = (struct sockaddr *)&mask;
176         rt_key(&rt) = (struct sockaddr *)&addr;
177         rt.rt_flags = RTF_HOST | RTF_STATIC;
178         if (cmd == RTM_ADD)
179                 rt.rt_flags |= RTF_UP;
180         fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS : ia62ifa(ia)->ifa_ifp->if_fib;
181         /* Announce arrival of local address to this FIB. */
182         rt_newaddrmsg_fib(cmd, &ia->ia_ifa, 0, &rt, fibnum);
183 }
184
185 int
186 in6_mask2len(struct in6_addr *mask, u_char *lim0)
187 {
188         int x = 0, y;
189         u_char *lim = lim0, *p;
190
191         /* ignore the scope_id part */
192         if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
193                 lim = (u_char *)mask + sizeof(*mask);
194         for (p = (u_char *)mask; p < lim; x++, p++) {
195                 if (*p != 0xff)
196                         break;
197         }
198         y = 0;
199         if (p < lim) {
200                 for (y = 0; y < 8; y++) {
201                         if ((*p & (0x80 >> y)) == 0)
202                                 break;
203                 }
204         }
205
206         /*
207          * when the limit pointer is given, do a stricter check on the
208          * remaining bits.
209          */
210         if (p < lim) {
211                 if (y != 0 && (*p & (0x00ff >> y)) != 0)
212                         return (-1);
213                 for (p = p + 1; p < lim; p++)
214                         if (*p != 0)
215                                 return (-1);
216         }
217
218         return x * 8 + y;
219 }
220
221 #ifdef COMPAT_FREEBSD32
222 struct in6_ndifreq32 {
223         char ifname[IFNAMSIZ];
224         uint32_t ifindex;
225 };
226 #define SIOCGDEFIFACE32_IN6     _IOWR('i', 86, struct in6_ndifreq32)
227 #endif
228
229 int
230 in6_control(struct socket *so, u_long cmd, caddr_t data,
231     struct ifnet *ifp, struct thread *td)
232 {
233         struct  in6_ifreq *ifr = (struct in6_ifreq *)data;
234         struct  in6_ifaddr *ia = NULL;
235         struct  in6_aliasreq *ifra = (struct in6_aliasreq *)data;
236         struct sockaddr_in6 *sa6;
237         int carp_attached = 0;
238         int error;
239         u_long ocmd = cmd;
240
241         /*
242          * Compat to make pre-10.x ifconfig(8) operable.
243          */
244         if (cmd == OSIOCAIFADDR_IN6)
245                 cmd = SIOCAIFADDR_IN6;
246
247         switch (cmd) {
248         case SIOCGETSGCNT_IN6:
249         case SIOCGETMIFCNT_IN6:
250                 /*
251                  * XXX mrt_ioctl has a 3rd, unused, FIB argument in route.c.
252                  * We cannot see how that would be needed, so do not adjust the
253                  * KPI blindly; more likely should clean up the IPv4 variant.
254                  */
255                 return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
256         }
257
258         switch (cmd) {
259         case SIOCAADDRCTL_POLICY:
260         case SIOCDADDRCTL_POLICY:
261                 if (td != NULL) {
262                         error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
263                         if (error)
264                                 return (error);
265                 }
266                 return (in6_src_ioctl(cmd, data));
267         }
268
269         if (ifp == NULL)
270                 return (EOPNOTSUPP);
271
272         switch (cmd) {
273         case SIOCSNDFLUSH_IN6:
274         case SIOCSPFXFLUSH_IN6:
275         case SIOCSRTRFLUSH_IN6:
276         case SIOCSDEFIFACE_IN6:
277         case SIOCSIFINFO_FLAGS:
278         case SIOCSIFINFO_IN6:
279                 if (td != NULL) {
280                         error = priv_check(td, PRIV_NETINET_ND6);
281                         if (error)
282                                 return (error);
283                 }
284                 /* FALLTHROUGH */
285         case OSIOCGIFINFO_IN6:
286         case SIOCGIFINFO_IN6:
287         case SIOCGNBRINFO_IN6:
288         case SIOCGDEFIFACE_IN6:
289                 return (nd6_ioctl(cmd, data, ifp));
290
291 #ifdef COMPAT_FREEBSD32
292         case SIOCGDEFIFACE32_IN6:
293                 {
294                         struct in6_ndifreq ndif;
295                         struct in6_ndifreq32 *ndif32;
296
297                         error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif,
298                             ifp);
299                         if (error)
300                                 return (error);
301                         ndif32 = (struct in6_ndifreq32 *)data;
302                         ndif32->ifindex = ndif.ifindex;
303                         return (0);
304                 }
305 #endif
306         }
307
308         switch (cmd) {
309         case SIOCSIFPREFIX_IN6:
310         case SIOCDIFPREFIX_IN6:
311         case SIOCAIFPREFIX_IN6:
312         case SIOCCIFPREFIX_IN6:
313         case SIOCSGIFPREFIX_IN6:
314         case SIOCGIFPREFIX_IN6:
315                 log(LOG_NOTICE,
316                     "prefix ioctls are now invalidated. "
317                     "please use ifconfig.\n");
318                 return (EOPNOTSUPP);
319         }
320
321         switch (cmd) {
322         case SIOCSSCOPE6:
323                 if (td != NULL) {
324                         error = priv_check(td, PRIV_NETINET_SCOPE6);
325                         if (error)
326                                 return (error);
327                 }
328                 /* FALLTHROUGH */
329         case SIOCGSCOPE6:
330         case SIOCGSCOPE6DEF:
331                 return (scope6_ioctl(cmd, data, ifp));
332         }
333
334         /*
335          * Find address for this interface, if it exists.
336          *
337          * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
338          * only, and used the first interface address as the target of other
339          * operations (without checking ifra_addr).  This was because netinet
340          * code/API assumed at most 1 interface address per interface.
341          * Since IPv6 allows a node to assign multiple addresses
342          * on a single interface, we almost always look and check the
343          * presence of ifra_addr, and reject invalid ones here.
344          * It also decreases duplicated code among SIOC*_IN6 operations.
345          */
346         switch (cmd) {
347         case SIOCAIFADDR_IN6:
348         case SIOCSIFPHYADDR_IN6:
349                 sa6 = &ifra->ifra_addr;
350                 break;
351         case SIOCSIFADDR_IN6:
352         case SIOCGIFADDR_IN6:
353         case SIOCSIFDSTADDR_IN6:
354         case SIOCSIFNETMASK_IN6:
355         case SIOCGIFDSTADDR_IN6:
356         case SIOCGIFNETMASK_IN6:
357         case SIOCDIFADDR_IN6:
358         case SIOCGIFPSRCADDR_IN6:
359         case SIOCGIFPDSTADDR_IN6:
360         case SIOCGIFAFLAG_IN6:
361         case SIOCSNDFLUSH_IN6:
362         case SIOCSPFXFLUSH_IN6:
363         case SIOCSRTRFLUSH_IN6:
364         case SIOCGIFALIFETIME_IN6:
365         case SIOCGIFSTAT_IN6:
366         case SIOCGIFSTAT_ICMP6:
367                 sa6 = &ifr->ifr_addr;
368                 break;
369         case SIOCSIFADDR:
370         case SIOCSIFBRDADDR:
371         case SIOCSIFDSTADDR:
372         case SIOCSIFNETMASK:
373                 /*
374                  * Although we should pass any non-INET6 ioctl requests
375                  * down to driver, we filter some legacy INET requests.
376                  * Drivers trust SIOCSIFADDR et al to come from an already
377                  * privileged layer, and do not perform any credentials
378                  * checks or input validation.
379                  */
380                 return (EINVAL);
381         default:
382                 sa6 = NULL;
383                 break;
384         }
385         if (sa6 && sa6->sin6_family == AF_INET6) {
386                 if (sa6->sin6_scope_id != 0)
387                         error = sa6_embedscope(sa6, 0);
388                 else
389                         error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
390                 if (error != 0)
391                         return (error);
392                 if (td != NULL && (error = prison_check_ip6(td->td_ucred,
393                     &sa6->sin6_addr)) != 0)
394                         return (error);
395                 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
396         } else
397                 ia = NULL;
398
399         switch (cmd) {
400         case SIOCSIFADDR_IN6:
401         case SIOCSIFDSTADDR_IN6:
402         case SIOCSIFNETMASK_IN6:
403                 /*
404                  * Since IPv6 allows a node to assign multiple addresses
405                  * on a single interface, SIOCSIFxxx ioctls are deprecated.
406                  */
407                 /* we decided to obsolete this command (20000704) */
408                 error = EINVAL;
409                 goto out;
410
411         case SIOCDIFADDR_IN6:
412                 /*
413                  * for IPv4, we look for existing in_ifaddr here to allow
414                  * "ifconfig if0 delete" to remove the first IPv4 address on
415                  * the interface.  For IPv6, as the spec allows multiple
416                  * interface address from the day one, we consider "remove the
417                  * first one" semantics to be not preferable.
418                  */
419                 if (ia == NULL) {
420                         error = EADDRNOTAVAIL;
421                         goto out;
422                 }
423                 /* FALLTHROUGH */
424         case SIOCAIFADDR_IN6:
425                 /*
426                  * We always require users to specify a valid IPv6 address for
427                  * the corresponding operation.
428                  */
429                 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
430                     ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
431                         error = EAFNOSUPPORT;
432                         goto out;
433                 }
434
435                 if (td != NULL) {
436                         error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
437                             PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
438                         if (error)
439                                 goto out;
440                 }
441                 /* FALLTHROUGH */
442         case SIOCGIFSTAT_IN6:
443         case SIOCGIFSTAT_ICMP6:
444                 if (ifp->if_afdata[AF_INET6] == NULL) {
445                         error = EPFNOSUPPORT;
446                         goto out;
447                 }
448                 break;
449
450         case SIOCGIFADDR_IN6:
451                 /* This interface is basically deprecated. use SIOCGIFCONF. */
452                 /* FALLTHROUGH */
453         case SIOCGIFAFLAG_IN6:
454         case SIOCGIFNETMASK_IN6:
455         case SIOCGIFDSTADDR_IN6:
456         case SIOCGIFALIFETIME_IN6:
457                 /* must think again about its semantics */
458                 if (ia == NULL) {
459                         error = EADDRNOTAVAIL;
460                         goto out;
461                 }
462                 break;
463         }
464
465         switch (cmd) {
466         case SIOCGIFADDR_IN6:
467                 ifr->ifr_addr = ia->ia_addr;
468                 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
469                         goto out;
470                 break;
471
472         case SIOCGIFDSTADDR_IN6:
473                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
474                         error = EINVAL;
475                         goto out;
476                 }
477                 /*
478                  * XXX: should we check if ifa_dstaddr is NULL and return
479                  * an error?
480                  */
481                 ifr->ifr_dstaddr = ia->ia_dstaddr;
482                 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
483                         goto out;
484                 break;
485
486         case SIOCGIFNETMASK_IN6:
487                 ifr->ifr_addr = ia->ia_prefixmask;
488                 break;
489
490         case SIOCGIFAFLAG_IN6:
491                 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
492                 break;
493
494         case SIOCGIFSTAT_IN6:
495                 COUNTER_ARRAY_COPY(((struct in6_ifextra *)
496                     ifp->if_afdata[AF_INET6])->in6_ifstat,
497                     &ifr->ifr_ifru.ifru_stat,
498                     sizeof(struct in6_ifstat) / sizeof(uint64_t));
499                 break;
500
501         case SIOCGIFSTAT_ICMP6:
502                 COUNTER_ARRAY_COPY(((struct in6_ifextra *)
503                     ifp->if_afdata[AF_INET6])->icmp6_ifstat,
504                     &ifr->ifr_ifru.ifru_icmp6stat,
505                     sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
506                 break;
507
508         case SIOCGIFALIFETIME_IN6:
509                 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
510                 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
511                         time_t maxexpire;
512                         struct in6_addrlifetime *retlt =
513                             &ifr->ifr_ifru.ifru_lifetime;
514
515                         /*
516                          * XXX: adjust expiration time assuming time_t is
517                          * signed.
518                          */
519                         maxexpire = (-1) &
520                             ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
521                         if (ia->ia6_lifetime.ia6t_vltime <
522                             maxexpire - ia->ia6_updatetime) {
523                                 retlt->ia6t_expire = ia->ia6_updatetime +
524                                     ia->ia6_lifetime.ia6t_vltime;
525                         } else
526                                 retlt->ia6t_expire = maxexpire;
527                 }
528                 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
529                         time_t maxexpire;
530                         struct in6_addrlifetime *retlt =
531                             &ifr->ifr_ifru.ifru_lifetime;
532
533                         /*
534                          * XXX: adjust expiration time assuming time_t is
535                          * signed.
536                          */
537                         maxexpire = (-1) &
538                             ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
539                         if (ia->ia6_lifetime.ia6t_pltime <
540                             maxexpire - ia->ia6_updatetime) {
541                                 retlt->ia6t_preferred = ia->ia6_updatetime +
542                                     ia->ia6_lifetime.ia6t_pltime;
543                         } else
544                                 retlt->ia6t_preferred = maxexpire;
545                 }
546                 break;
547
548         case SIOCAIFADDR_IN6:
549         {
550                 struct nd_prefixctl pr0;
551                 struct nd_prefix *pr;
552
553                 /*
554                  * first, make or update the interface address structure,
555                  * and link it to the list.
556                  */
557                 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
558                         goto out;
559                 if (ia != NULL) {
560                         if (ia->ia_ifa.ifa_carp)
561                                 (*carp_detach_p)(&ia->ia_ifa, true);
562                         ifa_free(&ia->ia_ifa);
563                 }
564                 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
565                     == NULL) {
566                         /*
567                          * this can happen when the user specify the 0 valid
568                          * lifetime.
569                          */
570                         break;
571                 }
572
573                 if (cmd == ocmd && ifra->ifra_vhid > 0) {
574                         if (carp_attach_p != NULL)
575                                 error = (*carp_attach_p)(&ia->ia_ifa,
576                                     ifra->ifra_vhid);
577                         else
578                                 error = EPROTONOSUPPORT;
579                         if (error)
580                                 goto out;
581                         else
582                                 carp_attached = 1;
583                 }
584
585                 /*
586                  * then, make the prefix on-link on the interface.
587                  * XXX: we'd rather create the prefix before the address, but
588                  * we need at least one address to install the corresponding
589                  * interface route, so we configure the address first.
590                  */
591
592                 /*
593                  * convert mask to prefix length (prefixmask has already
594                  * been validated in in6_update_ifa().
595                  */
596                 bzero(&pr0, sizeof(pr0));
597                 pr0.ndpr_ifp = ifp;
598                 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
599                     NULL);
600                 if (pr0.ndpr_plen == 128) {
601                         /* we don't need to install a host route. */
602                         goto aifaddr_out;
603                 }
604                 pr0.ndpr_prefix = ifra->ifra_addr;
605                 /* apply the mask for safety. */
606                 IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr,
607                     &ifra->ifra_prefixmask.sin6_addr);
608
609                 /*
610                  * XXX: since we don't have an API to set prefix (not address)
611                  * lifetimes, we just use the same lifetimes as addresses.
612                  * The (temporarily) installed lifetimes can be overridden by
613                  * later advertised RAs (when accept_rtadv is non 0), which is
614                  * an intended behavior.
615                  */
616                 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
617                 pr0.ndpr_raf_auto =
618                     ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
619                 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
620                 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
621
622                 /* add the prefix if not yet. */
623                 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
624                         /*
625                          * nd6_prelist_add will install the corresponding
626                          * interface route.
627                          */
628                         if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) {
629                                 if (carp_attached)
630                                         (*carp_detach_p)(&ia->ia_ifa, false);
631                                 goto out;
632                         }
633                 }
634
635                 /* relate the address to the prefix */
636                 if (ia->ia6_ndpr == NULL) {
637                         ia->ia6_ndpr = pr;
638                         pr->ndpr_addrcnt++;
639
640                         /*
641                          * If this is the first autoconf address from the
642                          * prefix, create a temporary address as well
643                          * (when required).
644                          */
645                         if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
646                             V_ip6_use_tempaddr && pr->ndpr_addrcnt == 1) {
647                                 int e;
648                                 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
649                                         log(LOG_NOTICE, "in6_control: failed "
650                                             "to create a temporary address, "
651                                             "errno=%d\n", e);
652                                 }
653                         }
654                 }
655                 nd6_prefix_rele(pr);
656
657                 /*
658                  * this might affect the status of autoconfigured addresses,
659                  * that is, this address might make other addresses detached.
660                  */
661                 pfxlist_onlink_check();
662
663 aifaddr_out:
664                 /*
665                  * Try to clear the flag when a new IPv6 address is added
666                  * onto an IFDISABLED interface and it succeeds.
667                  */
668                 if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
669                         struct in6_ndireq nd;
670
671                         memset(&nd, 0, sizeof(nd));
672                         nd.ndi.flags = ND_IFINFO(ifp)->flags;
673                         nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
674                         if (nd6_ioctl(SIOCSIFINFO_FLAGS, (caddr_t)&nd, ifp) < 0)
675                                 log(LOG_NOTICE, "SIOCAIFADDR_IN6: "
676                                     "SIOCSIFINFO_FLAGS for -ifdisabled "
677                                     "failed.");
678                         /*
679                          * Ignore failure of clearing the flag intentionally.
680                          * The failure means address duplication was detected.
681                          */
682                 }
683                 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
684                 break;
685         }
686
687         case SIOCDIFADDR_IN6:
688         {
689                 struct nd_prefix *pr;
690
691                 /*
692                  * If the address being deleted is the only one that owns
693                  * the corresponding prefix, expire the prefix as well.
694                  * XXX: theoretically, we don't have to worry about such
695                  * relationship, since we separate the address management
696                  * and the prefix management.  We do this, however, to provide
697                  * as much backward compatibility as possible in terms of
698                  * the ioctl operation.
699                  * Note that in6_purgeaddr() will decrement ndpr_addrcnt.
700                  */
701                 pr = ia->ia6_ndpr;
702                 in6_purgeaddr(&ia->ia_ifa);
703                 if (pr != NULL && pr->ndpr_addrcnt == 0) {
704                         ND6_WLOCK();
705                         nd6_prefix_unlink(pr, NULL);
706                         ND6_WUNLOCK();
707                         nd6_prefix_del(pr);
708                 }
709                 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
710                 break;
711         }
712
713         default:
714                 if (ifp->if_ioctl == NULL) {
715                         error = EOPNOTSUPP;
716                         goto out;
717                 }
718                 error = (*ifp->if_ioctl)(ifp, cmd, data);
719                 goto out;
720         }
721
722         error = 0;
723 out:
724         if (ia != NULL)
725                 ifa_free(&ia->ia_ifa);
726         return (error);
727 }
728
729
730 /*
731  * Join necessary multicast groups.  Factored out from in6_update_ifa().
732  * This entire work should only be done once, for the default FIB.
733  */
734 static int
735 in6_update_ifa_join_mc(struct ifnet *ifp, struct in6_aliasreq *ifra,
736     struct in6_ifaddr *ia, int flags, struct in6_multi **in6m_sol)
737 {
738         char ip6buf[INET6_ADDRSTRLEN];
739         struct in6_addr mltaddr;
740         struct in6_multi_mship *imm;
741         int delay, error;
742
743         KASSERT(in6m_sol != NULL, ("%s: in6m_sol is NULL", __func__));
744
745         /* Join solicited multicast addr for new host id. */
746         bzero(&mltaddr, sizeof(struct in6_addr));
747         mltaddr.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
748         mltaddr.s6_addr32[2] = htonl(1);
749         mltaddr.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
750         mltaddr.s6_addr8[12] = 0xff;
751         if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0) {
752                 /* XXX: should not happen */
753                 log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
754                 goto cleanup;
755         }
756         delay = error = 0;
757         if ((flags & IN6_IFAUPDATE_DADDELAY)) {
758                 /*
759                  * We need a random delay for DAD on the address being
760                  * configured.  It also means delaying transmission of the
761                  * corresponding MLD report to avoid report collision.
762                  * [RFC 4861, Section 6.3.7]
763                  */
764                 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
765         }
766         imm = in6_joingroup(ifp, &mltaddr, &error, delay);
767         if (imm == NULL) {
768                 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
769                     "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &mltaddr),
770                     if_name(ifp), error));
771                 goto cleanup;
772         }
773         LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
774         *in6m_sol = imm->i6mm_maddr;
775
776         /*
777          * Join link-local all-nodes address.
778          */
779         mltaddr = in6addr_linklocal_allnodes;
780         if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0)
781                 goto cleanup; /* XXX: should not fail */
782
783         imm = in6_joingroup(ifp, &mltaddr, &error, 0);
784         if (imm == NULL) {
785                 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
786                     "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &mltaddr),
787                     if_name(ifp), error));
788                 goto cleanup;
789         }
790         LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
791
792         /*
793          * Join node information group address.
794          */
795         delay = 0;
796         if ((flags & IN6_IFAUPDATE_DADDELAY)) {
797                 /*
798                  * The spec does not say anything about delay for this group,
799                  * but the same logic should apply.
800                  */
801                 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
802         }
803         if (in6_nigroup(ifp, NULL, -1, &mltaddr) == 0) {
804                 /* XXX jinmei */
805                 imm = in6_joingroup(ifp, &mltaddr, &error, delay);
806                 if (imm == NULL)
807                         nd6log((LOG_WARNING,
808                             "%s: in6_joingroup failed for %s on %s "
809                             "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
810                             &mltaddr), if_name(ifp), error));
811                         /* XXX not very fatal, go on... */
812                 else
813                         LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
814         }
815         if (V_icmp6_nodeinfo_oldmcprefix &&
816             in6_nigroup_oldmcprefix(ifp, NULL, -1, &mltaddr) == 0) {
817                 imm = in6_joingroup(ifp, &mltaddr, &error, delay);
818                 if (imm == NULL)
819                         nd6log((LOG_WARNING,
820                             "%s: in6_joingroup failed for %s on %s "
821                             "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
822                             &mltaddr), if_name(ifp), error));
823                         /* XXX not very fatal, go on... */
824                 else
825                         LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
826         }
827
828         /*
829          * Join interface-local all-nodes address.
830          * (ff01::1%ifN, and ff01::%ifN/32)
831          */
832         mltaddr = in6addr_nodelocal_allnodes;
833         if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0)
834                 goto cleanup; /* XXX: should not fail */
835
836         imm = in6_joingroup(ifp, &mltaddr, &error, 0);
837         if (imm == NULL) {
838                 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s "
839                     "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
840                     &mltaddr), if_name(ifp), error));
841                 goto cleanup;
842         }
843         LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
844
845 cleanup:
846         return (error);
847 }
848
849 /*
850  * Update parameters of an IPv6 interface address.
851  * If necessary, a new entry is created and linked into address chains.
852  * This function is separated from in6_control().
853  */
854 int
855 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
856     struct in6_ifaddr *ia, int flags)
857 {
858         int error, hostIsNew = 0;
859
860         if ((error = in6_validate_ifra(ifp, ifra, ia, flags)) != 0)
861                 return (error);
862
863         if (ia == NULL) {
864                 hostIsNew = 1;
865                 if ((ia = in6_alloc_ifa(ifp, ifra, flags)) == NULL)
866                         return (ENOBUFS);
867         }
868
869         error = in6_update_ifa_internal(ifp, ifra, ia, hostIsNew, flags);
870         if (error != 0) {
871                 if (hostIsNew != 0) {
872                         in6_unlink_ifa(ia, ifp);
873                         ifa_free(&ia->ia_ifa);
874                 }
875                 return (error);
876         }
877
878         if (hostIsNew)
879                 error = in6_broadcast_ifa(ifp, ifra, ia, flags);
880
881         return (error);
882 }
883
884 /*
885  * Fill in basic IPv6 address request info.
886  */
887 void
888 in6_prepare_ifra(struct in6_aliasreq *ifra, const struct in6_addr *addr,
889     const struct in6_addr *mask)
890 {
891
892         memset(ifra, 0, sizeof(struct in6_aliasreq));
893
894         ifra->ifra_addr.sin6_family = AF_INET6;
895         ifra->ifra_addr.sin6_len = sizeof(struct sockaddr_in6);
896         if (addr != NULL)
897                 ifra->ifra_addr.sin6_addr = *addr;
898
899         ifra->ifra_prefixmask.sin6_family = AF_INET6;
900         ifra->ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
901         if (mask != NULL)
902                 ifra->ifra_prefixmask.sin6_addr = *mask;
903 }
904
905 static int
906 in6_validate_ifra(struct ifnet *ifp, struct in6_aliasreq *ifra,
907     struct in6_ifaddr *ia, int flags)
908 {
909         int plen = -1;
910         struct sockaddr_in6 dst6;
911         struct in6_addrlifetime *lt;
912         char ip6buf[INET6_ADDRSTRLEN];
913
914         /* Validate parameters */
915         if (ifp == NULL || ifra == NULL) /* this maybe redundant */
916                 return (EINVAL);
917
918         /*
919          * The destination address for a p2p link must have a family
920          * of AF_UNSPEC or AF_INET6.
921          */
922         if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
923             ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
924             ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
925                 return (EAFNOSUPPORT);
926
927         /*
928          * Validate address
929          */
930         if (ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6) ||
931             ifra->ifra_addr.sin6_family != AF_INET6)
932                 return (EINVAL);
933
934         /*
935          * validate ifra_prefixmask.  don't check sin6_family, netmask
936          * does not carry fields other than sin6_len.
937          */
938         if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
939                 return (EINVAL);
940         /*
941          * Because the IPv6 address architecture is classless, we require
942          * users to specify a (non 0) prefix length (mask) for a new address.
943          * We also require the prefix (when specified) mask is valid, and thus
944          * reject a non-consecutive mask.
945          */
946         if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
947                 return (EINVAL);
948         if (ifra->ifra_prefixmask.sin6_len != 0) {
949                 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
950                     (u_char *)&ifra->ifra_prefixmask +
951                     ifra->ifra_prefixmask.sin6_len);
952                 if (plen <= 0)
953                         return (EINVAL);
954         } else {
955                 /*
956                  * In this case, ia must not be NULL.  We just use its prefix
957                  * length.
958                  */
959                 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
960         }
961         /*
962          * If the destination address on a p2p interface is specified,
963          * and the address is a scoped one, validate/set the scope
964          * zone identifier.
965          */
966         dst6 = ifra->ifra_dstaddr;
967         if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
968             (dst6.sin6_family == AF_INET6)) {
969                 struct in6_addr in6_tmp;
970                 u_int32_t zoneid;
971
972                 in6_tmp = dst6.sin6_addr;
973                 if (in6_setscope(&in6_tmp, ifp, &zoneid))
974                         return (EINVAL); /* XXX: should be impossible */
975
976                 if (dst6.sin6_scope_id != 0) {
977                         if (dst6.sin6_scope_id != zoneid)
978                                 return (EINVAL);
979                 } else          /* user omit to specify the ID. */
980                         dst6.sin6_scope_id = zoneid;
981
982                 /* convert into the internal form */
983                 if (sa6_embedscope(&dst6, 0))
984                         return (EINVAL); /* XXX: should be impossible */
985         }
986         /* Modify original ifra_dstaddr to reflect changes */
987         ifra->ifra_dstaddr = dst6;
988
989         /*
990          * The destination address can be specified only for a p2p or a
991          * loopback interface.  If specified, the corresponding prefix length
992          * must be 128.
993          */
994         if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
995                 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
996                         /* XXX: noisy message */
997                         nd6log((LOG_INFO, "in6_update_ifa: a destination can "
998                             "be specified for a p2p or a loopback IF only\n"));
999                         return (EINVAL);
1000                 }
1001                 if (plen != 128) {
1002                         nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
1003                             "be 128 when dstaddr is specified\n"));
1004                         return (EINVAL);
1005                 }
1006         }
1007         /* lifetime consistency check */
1008         lt = &ifra->ifra_lifetime;
1009         if (lt->ia6t_pltime > lt->ia6t_vltime)
1010                 return (EINVAL);
1011         if (lt->ia6t_vltime == 0) {
1012                 /*
1013                  * the following log might be noisy, but this is a typical
1014                  * configuration mistake or a tool's bug.
1015                  */
1016                 nd6log((LOG_INFO,
1017                     "in6_update_ifa: valid lifetime is 0 for %s\n",
1018                     ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
1019
1020                 if (ia == NULL)
1021                         return (0); /* there's nothing to do */
1022         }
1023
1024         /* Check prefix mask */
1025         if (ia != NULL && ifra->ifra_prefixmask.sin6_len != 0) {
1026                 /*
1027                  * We prohibit changing the prefix length of an existing
1028                  * address, because
1029                  * + such an operation should be rare in IPv6, and
1030                  * + the operation would confuse prefix management.
1031                  */
1032                 if (ia->ia_prefixmask.sin6_len != 0 &&
1033                     in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1034                         nd6log((LOG_INFO, "in6_validate_ifa: the prefix length "
1035                             "of an existing %s address should not be changed\n",
1036                             ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1037
1038                         return (EINVAL);
1039                 }
1040         }
1041
1042         return (0);
1043 }
1044
1045
1046 /*
1047  * Allocate a new ifaddr and link it into chains.
1048  */
1049 static struct in6_ifaddr *
1050 in6_alloc_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, int flags)
1051 {
1052         struct in6_ifaddr *ia;
1053
1054         /*
1055          * When in6_alloc_ifa() is called in a process of a received
1056          * RA, it is called under an interrupt context.  So, we should
1057          * call malloc with M_NOWAIT.
1058          */
1059         ia = (struct in6_ifaddr *)ifa_alloc(sizeof(*ia), M_NOWAIT);
1060         if (ia == NULL)
1061                 return (NULL);
1062         LIST_INIT(&ia->ia6_memberships);
1063         /* Initialize the address and masks, and put time stamp */
1064         ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1065         ia->ia_addr.sin6_family = AF_INET6;
1066         ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
1067         /* XXX: Can we assign ,sin6_addr and skip the rest? */
1068         ia->ia_addr = ifra->ifra_addr;
1069         ia->ia6_createtime = time_uptime;
1070         if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
1071                 /*
1072                  * Some functions expect that ifa_dstaddr is not
1073                  * NULL for p2p interfaces.
1074                  */
1075                 ia->ia_ifa.ifa_dstaddr =
1076                     (struct sockaddr *)&ia->ia_dstaddr;
1077         } else {
1078                 ia->ia_ifa.ifa_dstaddr = NULL;
1079         }
1080
1081         /* set prefix mask if any */
1082         ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
1083         if (ifra->ifra_prefixmask.sin6_len != 0) {
1084                 ia->ia_prefixmask.sin6_family = AF_INET6;
1085                 ia->ia_prefixmask.sin6_len = ifra->ifra_prefixmask.sin6_len;
1086                 ia->ia_prefixmask.sin6_addr = ifra->ifra_prefixmask.sin6_addr;
1087         }
1088
1089         ia->ia_ifp = ifp;
1090         ifa_ref(&ia->ia_ifa);                   /* if_addrhead */
1091         IF_ADDR_WLOCK(ifp);
1092         TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1093         IF_ADDR_WUNLOCK(ifp);
1094
1095         ifa_ref(&ia->ia_ifa);                   /* in6_ifaddrhead */
1096         IN6_IFADDR_WLOCK();
1097         TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
1098         LIST_INSERT_HEAD(IN6ADDR_HASH(&ia->ia_addr.sin6_addr), ia, ia6_hash);
1099         IN6_IFADDR_WUNLOCK();
1100
1101         return (ia);
1102 }
1103
1104 /*
1105  * Update/configure interface address parameters:
1106  *
1107  * 1) Update lifetime
1108  * 2) Update interface metric ad flags
1109  * 3) Notify other subsystems
1110  */
1111 static int
1112 in6_update_ifa_internal(struct ifnet *ifp, struct in6_aliasreq *ifra,
1113     struct in6_ifaddr *ia, int hostIsNew, int flags)
1114 {
1115         int error;
1116
1117         /* update timestamp */
1118         ia->ia6_updatetime = time_uptime;
1119
1120         /*
1121          * Set lifetimes.  We do not refer to ia6t_expire and ia6t_preferred
1122          * to see if the address is deprecated or invalidated, but initialize
1123          * these members for applications.
1124          */
1125         ia->ia6_lifetime = ifra->ifra_lifetime;
1126         if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1127                 ia->ia6_lifetime.ia6t_expire =
1128                     time_uptime + ia->ia6_lifetime.ia6t_vltime;
1129         } else
1130                 ia->ia6_lifetime.ia6t_expire = 0;
1131         if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1132                 ia->ia6_lifetime.ia6t_preferred =
1133                     time_uptime + ia->ia6_lifetime.ia6t_pltime;
1134         } else
1135                 ia->ia6_lifetime.ia6t_preferred = 0;
1136
1137         /*
1138          * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1139          * userland, make it deprecated.
1140          */
1141         if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1142                 ia->ia6_lifetime.ia6t_pltime = 0;
1143                 ia->ia6_lifetime.ia6t_preferred = time_uptime;
1144         }
1145
1146         /*
1147          * configure address flags.
1148          */
1149         ia->ia6_flags = ifra->ifra_flags;
1150
1151         /*
1152          * Make the address tentative before joining multicast addresses,
1153          * so that corresponding MLD responses would not have a tentative
1154          * source address.
1155          */
1156         ia->ia6_flags &= ~IN6_IFF_DUPLICATED;   /* safety */
1157
1158         /*
1159          * DAD should be performed for an new address or addresses on
1160          * an interface with ND6_IFF_IFDISABLED.
1161          */
1162         if (in6if_do_dad(ifp) &&
1163             (hostIsNew || (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)))
1164                 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1165
1166         /* notify other subsystems */
1167         error = in6_notify_ifa(ifp, ia, ifra, hostIsNew);
1168
1169         return (error);
1170 }
1171
1172 /*
1173  * Do link-level ifa job:
1174  * 1) Add lle entry for added address
1175  * 2) Notifies routing socket users about new address
1176  * 3) join appropriate multicast group
1177  * 4) start DAD if enabled
1178  */
1179 static int
1180 in6_broadcast_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
1181     struct in6_ifaddr *ia, int flags)
1182 {
1183         struct in6_multi *in6m_sol;
1184         int error = 0;
1185
1186         /* Add local address to lltable, if necessary (ex. on p2p link). */
1187         if ((error = nd6_add_ifa_lle(ia)) != 0) {
1188                 in6_purgeaddr(&ia->ia_ifa);
1189                 ifa_free(&ia->ia_ifa);
1190                 return (error);
1191         }
1192
1193         /* Join necessary multicast groups. */
1194         in6m_sol = NULL;
1195         if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1196                 error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol);
1197                 if (error != 0) {
1198                         in6_purgeaddr(&ia->ia_ifa);
1199                         ifa_free(&ia->ia_ifa);
1200                         return (error);
1201                 }
1202         }
1203
1204         /* Perform DAD, if the address is TENTATIVE. */
1205         if ((ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1206                 int delay, mindelay, maxdelay;
1207
1208                 delay = 0;
1209                 if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1210                         /*
1211                          * We need to impose a delay before sending an NS
1212                          * for DAD.  Check if we also needed a delay for the
1213                          * corresponding MLD message.  If we did, the delay
1214                          * should be larger than the MLD delay (this could be
1215                          * relaxed a bit, but this simple logic is at least
1216                          * safe).
1217                          * XXX: Break data hiding guidelines and look at
1218                          * state for the solicited multicast group.
1219                          */
1220                         mindelay = 0;
1221                         if (in6m_sol != NULL &&
1222                             in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1223                                 mindelay = in6m_sol->in6m_timer;
1224                         }
1225                         maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1226                         if (maxdelay - mindelay == 0)
1227                                 delay = 0;
1228                         else {
1229                                 delay =
1230                                     (arc4random() % (maxdelay - mindelay)) +
1231                                     mindelay;
1232                         }
1233                 }
1234                 nd6_dad_start((struct ifaddr *)ia, delay);
1235         }
1236
1237         in6_newaddrmsg(ia, RTM_ADD);
1238         ifa_free(&ia->ia_ifa);
1239         return (error);
1240 }
1241
1242 void
1243 in6_purgeaddr(struct ifaddr *ifa)
1244 {
1245         struct ifnet *ifp = ifa->ifa_ifp;
1246         struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1247         struct in6_multi_mship *imm;
1248         int plen, error;
1249
1250         if (ifa->ifa_carp)
1251                 (*carp_detach_p)(ifa, false);
1252
1253         /*
1254          * Remove the loopback route to the interface address.
1255          * The check for the current setting of "nd6_useloopback"
1256          * is not needed.
1257          */
1258         if (ia->ia_flags & IFA_RTSELF) {
1259                 error = ifa_del_loopback_route((struct ifaddr *)ia,
1260                     (struct sockaddr *)&ia->ia_addr);
1261                 if (error == 0)
1262                         ia->ia_flags &= ~IFA_RTSELF;
1263         }
1264
1265         /* stop DAD processing */
1266         nd6_dad_stop(ifa);
1267
1268         /* Leave multicast groups. */
1269         while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1270                 LIST_REMOVE(imm, i6mm_chain);
1271                 in6_leavegroup(imm);
1272         }
1273         plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1274         if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1275                 error = rtinit(&(ia->ia_ifa), RTM_DELETE, ia->ia_flags |
1276                     (ia->ia_dstaddr.sin6_family == AF_INET6 ? RTF_HOST : 0));
1277                 if (error != 0)
1278                         log(LOG_INFO, "%s: err=%d, destination address delete "
1279                             "failed\n", __func__, error);
1280                 ia->ia_flags &= ~IFA_ROUTE;
1281         }
1282
1283         in6_newaddrmsg(ia, RTM_DELETE);
1284         in6_unlink_ifa(ia, ifp);
1285 }
1286
1287 static void
1288 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
1289 {
1290         char ip6buf[INET6_ADDRSTRLEN];
1291         int remove_lle;
1292
1293         IF_ADDR_WLOCK(ifp);
1294         TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1295         IF_ADDR_WUNLOCK(ifp);
1296         ifa_free(&ia->ia_ifa);                  /* if_addrhead */
1297
1298         /*
1299          * Defer the release of what might be the last reference to the
1300          * in6_ifaddr so that it can't be freed before the remainder of the
1301          * cleanup.
1302          */
1303         IN6_IFADDR_WLOCK();
1304         TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1305         LIST_REMOVE(ia, ia6_hash);
1306         IN6_IFADDR_WUNLOCK();
1307
1308         /*
1309          * Release the reference to the base prefix.  There should be a
1310          * positive reference.
1311          */
1312         remove_lle = 0;
1313         if (ia->ia6_ndpr == NULL) {
1314                 nd6log((LOG_NOTICE,
1315                     "in6_unlink_ifa: autoconf'ed address "
1316                     "%s has no prefix\n", ip6_sprintf(ip6buf, IA6_IN6(ia))));
1317         } else {
1318                 ia->ia6_ndpr->ndpr_addrcnt--;
1319                 /* Do not delete lles within prefix if refcont != 0 */
1320                 if (ia->ia6_ndpr->ndpr_addrcnt == 0)
1321                         remove_lle = 1;
1322                 ia->ia6_ndpr = NULL;
1323         }
1324
1325         nd6_rem_ifa_lle(ia, remove_lle);
1326
1327         /*
1328          * Also, if the address being removed is autoconf'ed, call
1329          * pfxlist_onlink_check() since the release might affect the status of
1330          * other (detached) addresses.
1331          */
1332         if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
1333                 pfxlist_onlink_check();
1334         }
1335         ifa_free(&ia->ia_ifa);                  /* in6_ifaddrhead */
1336 }
1337
1338 /*
1339  * Notifies other subsystems about address change/arrival:
1340  * 1) Notifies device handler on the first IPv6 address assignment
1341  * 2) Handle routing table changes for P2P links and route
1342  * 3) Handle routing table changes for address host route
1343  */
1344 static int
1345 in6_notify_ifa(struct ifnet *ifp, struct in6_ifaddr *ia,
1346     struct in6_aliasreq *ifra, int hostIsNew)
1347 {
1348         int     error = 0, plen, ifacount = 0;
1349         struct ifaddr *ifa;
1350         struct sockaddr_in6 *pdst;
1351         char ip6buf[INET6_ADDRSTRLEN];
1352
1353         /*
1354          * Give the interface a chance to initialize
1355          * if this is its first address,
1356          */
1357         if (hostIsNew != 0) {
1358                 IF_ADDR_RLOCK(ifp);
1359                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1360                         if (ifa->ifa_addr->sa_family != AF_INET6)
1361                                 continue;
1362                         ifacount++;
1363                 }
1364                 IF_ADDR_RUNLOCK(ifp);
1365         }
1366
1367         if (ifacount <= 1 && ifp->if_ioctl) {
1368                 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1369                 if (error)
1370                         return (error);
1371         }
1372
1373         /*
1374          * If a new destination address is specified, scrub the old one and
1375          * install the new destination.  Note that the interface must be
1376          * p2p or loopback.
1377          */
1378         pdst = &ifra->ifra_dstaddr;
1379         if (pdst->sin6_family == AF_INET6 &&
1380             !IN6_ARE_ADDR_EQUAL(&pdst->sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
1381                 if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1382                     (rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST) != 0)) {
1383                         nd6log((LOG_ERR, "in6_update_ifa_internal: failed to "
1384                             "remove a route to the old destination: %s\n",
1385                             ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
1386                         /* proceed anyway... */
1387                 } else
1388                         ia->ia_flags &= ~IFA_ROUTE;
1389                 ia->ia_dstaddr = *pdst;
1390         }
1391
1392         /*
1393          * If a new destination address is specified for a point-to-point
1394          * interface, install a route to the destination as an interface
1395          * direct route.
1396          * XXX: the logic below rejects assigning multiple addresses on a p2p
1397          * interface that share the same destination.
1398          */
1399         plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1400         if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1401             ia->ia_dstaddr.sin6_family == AF_INET6) {
1402                 int rtflags = RTF_UP | RTF_HOST;
1403                 /*
1404                  * Handle the case for ::1 .
1405                  */
1406                 if (ifp->if_flags & IFF_LOOPBACK)
1407                         ia->ia_flags |= IFA_RTSELF;
1408                 error = rtinit(&ia->ia_ifa, RTM_ADD, ia->ia_flags | rtflags);
1409                 if (error)
1410                         return (error);
1411                 ia->ia_flags |= IFA_ROUTE;
1412         }
1413
1414         /*
1415          * add a loopback route to self if not exists
1416          */
1417         if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
1418                 error = ifa_add_loopback_route((struct ifaddr *)ia,
1419                     (struct sockaddr *)&ia->ia_addr);
1420                 if (error == 0)
1421                         ia->ia_flags |= IFA_RTSELF;
1422         }
1423
1424         return (error);
1425 }
1426
1427 /*
1428  * Find an IPv6 interface link-local address specific to an interface.
1429  * ifaddr is returned referenced.
1430  */
1431 struct in6_ifaddr *
1432 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
1433 {
1434         struct ifaddr *ifa;
1435
1436         IF_ADDR_RLOCK(ifp);
1437         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1438                 if (ifa->ifa_addr->sa_family != AF_INET6)
1439                         continue;
1440                 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1441                         if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1442                             ignoreflags) != 0)
1443                                 continue;
1444                         ifa_ref(ifa);
1445                         break;
1446                 }
1447         }
1448         IF_ADDR_RUNLOCK(ifp);
1449
1450         return ((struct in6_ifaddr *)ifa);
1451 }
1452
1453
1454 /*
1455  * find the internet address corresponding to a given address.
1456  * ifaddr is returned referenced.
1457  */
1458 struct in6_ifaddr *
1459 in6ifa_ifwithaddr(const struct in6_addr *addr, uint32_t zoneid)
1460 {
1461         struct rm_priotracker in6_ifa_tracker;
1462         struct in6_ifaddr *ia;
1463
1464         IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1465         LIST_FOREACH(ia, IN6ADDR_HASH(addr), ia6_hash) {
1466                 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), addr)) {
1467                         if (zoneid != 0 &&
1468                             zoneid != ia->ia_addr.sin6_scope_id)
1469                                 continue;
1470                         ifa_ref(&ia->ia_ifa);
1471                         break;
1472                 }
1473         }
1474         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1475         return (ia);
1476 }
1477
1478 /*
1479  * find the internet address corresponding to a given interface and address.
1480  * ifaddr is returned referenced.
1481  */
1482 struct in6_ifaddr *
1483 in6ifa_ifpwithaddr(struct ifnet *ifp, const struct in6_addr *addr)
1484 {
1485         struct ifaddr *ifa;
1486
1487         IF_ADDR_RLOCK(ifp);
1488         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1489                 if (ifa->ifa_addr->sa_family != AF_INET6)
1490                         continue;
1491                 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1492                         ifa_ref(ifa);
1493                         break;
1494                 }
1495         }
1496         IF_ADDR_RUNLOCK(ifp);
1497
1498         return ((struct in6_ifaddr *)ifa);
1499 }
1500
1501 /*
1502  * Find a link-local scoped address on ifp and return it if any.
1503  */
1504 struct in6_ifaddr *
1505 in6ifa_llaonifp(struct ifnet *ifp)
1506 {
1507         struct sockaddr_in6 *sin6;
1508         struct ifaddr *ifa;
1509
1510         if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1511                 return (NULL);
1512         IF_ADDR_RLOCK(ifp);
1513         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1514                 if (ifa->ifa_addr->sa_family != AF_INET6)
1515                         continue;
1516                 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1517                 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
1518                     IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
1519                     IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
1520                         break;
1521         }
1522         IF_ADDR_RUNLOCK(ifp);
1523
1524         return ((struct in6_ifaddr *)ifa);
1525 }
1526
1527 /*
1528  * Convert IP6 address to printable (loggable) representation. Caller
1529  * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
1530  */
1531 static char digits[] = "0123456789abcdef";
1532 char *
1533 ip6_sprintf(char *ip6buf, const struct in6_addr *addr)
1534 {
1535         int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
1536         char *cp;
1537         const u_int16_t *a = (const u_int16_t *)addr;
1538         const u_int8_t *d;
1539         int dcolon = 0, zero = 0;
1540
1541         cp = ip6buf;
1542
1543         for (i = 0; i < 8; i++) {
1544                 if (*(a + i) == 0) {
1545                         cnt++;
1546                         if (cnt == 1)
1547                                 idx = i;
1548                 }
1549                 else if (maxcnt < cnt) {
1550                         maxcnt = cnt;
1551                         index = idx;
1552                         cnt = 0;
1553                 }
1554         }
1555         if (maxcnt < cnt) {
1556                 maxcnt = cnt;
1557                 index = idx;
1558         }
1559
1560         for (i = 0; i < 8; i++) {
1561                 if (dcolon == 1) {
1562                         if (*a == 0) {
1563                                 if (i == 7)
1564                                         *cp++ = ':';
1565                                 a++;
1566                                 continue;
1567                         } else
1568                                 dcolon = 2;
1569                 }
1570                 if (*a == 0) {
1571                         if (dcolon == 0 && *(a + 1) == 0 && i == index) {
1572                                 if (i == 0)
1573                                         *cp++ = ':';
1574                                 *cp++ = ':';
1575                                 dcolon = 1;
1576                         } else {
1577                                 *cp++ = '0';
1578                                 *cp++ = ':';
1579                         }
1580                         a++;
1581                         continue;
1582                 }
1583                 d = (const u_char *)a;
1584                 /* Try to eliminate leading zeros in printout like in :0001. */
1585                 zero = 1;
1586                 *cp = digits[*d >> 4];
1587                 if (*cp != '0') {
1588                         zero = 0;
1589                         cp++;
1590                 }
1591                 *cp = digits[*d++ & 0xf];
1592                 if (zero == 0 || (*cp != '0')) {
1593                         zero = 0;
1594                         cp++;
1595                 }
1596                 *cp = digits[*d >> 4];
1597                 if (zero == 0 || (*cp != '0')) {
1598                         zero = 0;
1599                         cp++;
1600                 }
1601                 *cp++ = digits[*d & 0xf];
1602                 *cp++ = ':';
1603                 a++;
1604         }
1605         *--cp = '\0';
1606         return (ip6buf);
1607 }
1608
1609 int
1610 in6_localaddr(struct in6_addr *in6)
1611 {
1612         struct rm_priotracker in6_ifa_tracker;
1613         struct in6_ifaddr *ia;
1614
1615         if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1616                 return 1;
1617
1618         IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1619         TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
1620                 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1621                     &ia->ia_prefixmask.sin6_addr)) {
1622                         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1623                         return 1;
1624                 }
1625         }
1626         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1627
1628         return (0);
1629 }
1630
1631 /*
1632  * Return 1 if an internet address is for the local host and configured
1633  * on one of its interfaces.
1634  */
1635 int
1636 in6_localip(struct in6_addr *in6)
1637 {
1638         struct rm_priotracker in6_ifa_tracker;
1639         struct in6_ifaddr *ia;
1640
1641         IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1642         LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) {
1643                 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) {
1644                         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1645                         return (1);
1646                 }
1647         }
1648         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1649         return (0);
1650 }
1651  
1652 /*
1653  * Return 1 if an internet address is configured on an interface.
1654  */
1655 int
1656 in6_ifhasaddr(struct ifnet *ifp, struct in6_addr *addr)
1657 {
1658         struct in6_addr in6;
1659         struct ifaddr *ifa;
1660         struct in6_ifaddr *ia6;
1661
1662         in6 = *addr;
1663         if (in6_clearscope(&in6))
1664                 return (0);
1665         in6_setscope(&in6, ifp, NULL);
1666
1667         IF_ADDR_RLOCK(ifp);
1668         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1669                 if (ifa->ifa_addr->sa_family != AF_INET6)
1670                         continue;
1671                 ia6 = (struct in6_ifaddr *)ifa;
1672                 if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &in6)) {
1673                         IF_ADDR_RUNLOCK(ifp);
1674                         return (1);
1675                 }
1676         }
1677         IF_ADDR_RUNLOCK(ifp);
1678
1679         return (0);
1680 }
1681
1682 int
1683 in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
1684 {
1685         struct rm_priotracker in6_ifa_tracker;
1686         struct in6_ifaddr *ia;
1687
1688         IN6_IFADDR_RLOCK(&in6_ifa_tracker);
1689         LIST_FOREACH(ia, IN6ADDR_HASH(&sa6->sin6_addr), ia6_hash) {
1690                 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), &sa6->sin6_addr)) {
1691                         if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
1692                                 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1693                                 return (1); /* true */
1694                         }
1695                         break;
1696                 }
1697         }
1698         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1699
1700         return (0);             /* false */
1701 }
1702
1703 /*
1704  * return length of part which dst and src are equal
1705  * hard coding...
1706  */
1707 int
1708 in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
1709 {
1710         int match = 0;
1711         u_char *s = (u_char *)src, *d = (u_char *)dst;
1712         u_char *lim = s + 16, r;
1713
1714         while (s < lim)
1715                 if ((r = (*d++ ^ *s++)) != 0) {
1716                         while (r < 128) {
1717                                 match++;
1718                                 r <<= 1;
1719                         }
1720                         break;
1721                 } else
1722                         match += 8;
1723         return match;
1724 }
1725
1726 /* XXX: to be scope conscious */
1727 int
1728 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
1729 {
1730         int bytelen, bitlen;
1731
1732         /* sanity check */
1733         if (0 > len || len > 128) {
1734                 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1735                     len);
1736                 return (0);
1737         }
1738
1739         bytelen = len / 8;
1740         bitlen = len % 8;
1741
1742         if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1743                 return (0);
1744         if (bitlen != 0 &&
1745             p1->s6_addr[bytelen] >> (8 - bitlen) !=
1746             p2->s6_addr[bytelen] >> (8 - bitlen))
1747                 return (0);
1748
1749         return (1);
1750 }
1751
1752 void
1753 in6_prefixlen2mask(struct in6_addr *maskp, int len)
1754 {
1755         u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1756         int bytelen, bitlen, i;
1757
1758         /* sanity check */
1759         if (0 > len || len > 128) {
1760                 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1761                     len);
1762                 return;
1763         }
1764
1765         bzero(maskp, sizeof(*maskp));
1766         bytelen = len / 8;
1767         bitlen = len % 8;
1768         for (i = 0; i < bytelen; i++)
1769                 maskp->s6_addr[i] = 0xff;
1770         if (bitlen)
1771                 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1772 }
1773
1774 /*
1775  * return the best address out of the same scope. if no address was
1776  * found, return the first valid address from designated IF.
1777  */
1778 struct in6_ifaddr *
1779 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
1780 {
1781         int dst_scope = in6_addrscope(dst), blen = -1, tlen;
1782         struct ifaddr *ifa;
1783         struct in6_ifaddr *besta = NULL;
1784         struct in6_ifaddr *dep[2];      /* last-resort: deprecated */
1785
1786         dep[0] = dep[1] = NULL;
1787
1788         /*
1789          * We first look for addresses in the same scope.
1790          * If there is one, return it.
1791          * If two or more, return one which matches the dst longest.
1792          * If none, return one of global addresses assigned other ifs.
1793          */
1794         IF_ADDR_RLOCK(ifp);
1795         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1796                 if (ifa->ifa_addr->sa_family != AF_INET6)
1797                         continue;
1798                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1799                         continue; /* XXX: is there any case to allow anycast? */
1800                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1801                         continue; /* don't use this interface */
1802                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1803                         continue;
1804                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1805                         if (V_ip6_use_deprecated)
1806                                 dep[0] = (struct in6_ifaddr *)ifa;
1807                         continue;
1808                 }
1809
1810                 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
1811                         /*
1812                          * call in6_matchlen() as few as possible
1813                          */
1814                         if (besta) {
1815                                 if (blen == -1)
1816                                         blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
1817                                 tlen = in6_matchlen(IFA_IN6(ifa), dst);
1818                                 if (tlen > blen) {
1819                                         blen = tlen;
1820                                         besta = (struct in6_ifaddr *)ifa;
1821                                 }
1822                         } else
1823                                 besta = (struct in6_ifaddr *)ifa;
1824                 }
1825         }
1826         if (besta) {
1827                 ifa_ref(&besta->ia_ifa);
1828                 IF_ADDR_RUNLOCK(ifp);
1829                 return (besta);
1830         }
1831
1832         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1833                 if (ifa->ifa_addr->sa_family != AF_INET6)
1834                         continue;
1835                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
1836                         continue; /* XXX: is there any case to allow anycast? */
1837                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
1838                         continue; /* don't use this interface */
1839                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
1840                         continue;
1841                 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
1842                         if (V_ip6_use_deprecated)
1843                                 dep[1] = (struct in6_ifaddr *)ifa;
1844                         continue;
1845                 }
1846
1847                 if (ifa != NULL)
1848                         ifa_ref(ifa);
1849                 IF_ADDR_RUNLOCK(ifp);
1850                 return (struct in6_ifaddr *)ifa;
1851         }
1852
1853         /* use the last-resort values, that are, deprecated addresses */
1854         if (dep[0]) {
1855                 ifa_ref((struct ifaddr *)dep[0]);
1856                 IF_ADDR_RUNLOCK(ifp);
1857                 return dep[0];
1858         }
1859         if (dep[1]) {
1860                 ifa_ref((struct ifaddr *)dep[1]);
1861                 IF_ADDR_RUNLOCK(ifp);
1862                 return dep[1];
1863         }
1864
1865         IF_ADDR_RUNLOCK(ifp);
1866         return NULL;
1867 }
1868
1869 /*
1870  * perform DAD when interface becomes IFF_UP.
1871  */
1872 void
1873 in6_if_up(struct ifnet *ifp)
1874 {
1875         struct ifaddr *ifa;
1876         struct in6_ifaddr *ia;
1877
1878         IF_ADDR_RLOCK(ifp);
1879         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1880                 if (ifa->ifa_addr->sa_family != AF_INET6)
1881                         continue;
1882                 ia = (struct in6_ifaddr *)ifa;
1883                 if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
1884                         /*
1885                          * The TENTATIVE flag was likely set by hand
1886                          * beforehand, implicitly indicating the need for DAD.
1887                          * We may be able to skip the random delay in this
1888                          * case, but we impose delays just in case.
1889                          */
1890                         nd6_dad_start(ifa,
1891                             arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
1892                 }
1893         }
1894         IF_ADDR_RUNLOCK(ifp);
1895
1896         /*
1897          * special cases, like 6to4, are handled in in6_ifattach
1898          */
1899         in6_ifattach(ifp, NULL);
1900 }
1901
1902 int
1903 in6if_do_dad(struct ifnet *ifp)
1904 {
1905         if ((ifp->if_flags & IFF_LOOPBACK) != 0)
1906                 return (0);
1907
1908         if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) ||
1909             (ND_IFINFO(ifp)->flags & ND6_IFF_NO_DAD))
1910                 return (0);
1911
1912         /*
1913          * Our DAD routine requires the interface up and running.
1914          * However, some interfaces can be up before the RUNNING
1915          * status.  Additionally, users may try to assign addresses
1916          * before the interface becomes up (or running).
1917          * This function returns EAGAIN in that case.
1918          * The caller should mark "tentative" on the address instead of
1919          * performing DAD immediately.
1920          */
1921         if (!((ifp->if_flags & IFF_UP) &&
1922             (ifp->if_drv_flags & IFF_DRV_RUNNING)))
1923                 return (EAGAIN);
1924
1925         return (1);
1926 }
1927
1928 /*
1929  * Calculate max IPv6 MTU through all the interfaces and store it
1930  * to in6_maxmtu.
1931  */
1932 void
1933 in6_setmaxmtu(void)
1934 {
1935         unsigned long maxmtu = 0;
1936         struct ifnet *ifp;
1937
1938         IFNET_RLOCK_NOSLEEP();
1939         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1940                 /* this function can be called during ifnet initialization */
1941                 if (!ifp->if_afdata[AF_INET6])
1942                         continue;
1943                 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
1944                     IN6_LINKMTU(ifp) > maxmtu)
1945                         maxmtu = IN6_LINKMTU(ifp);
1946         }
1947         IFNET_RUNLOCK_NOSLEEP();
1948         if (maxmtu)     /* update only when maxmtu is positive */
1949                 V_in6_maxmtu = maxmtu;
1950 }
1951
1952 /*
1953  * Provide the length of interface identifiers to be used for the link attached
1954  * to the given interface.  The length should be defined in "IPv6 over
1955  * xxx-link" document.  Note that address architecture might also define
1956  * the length for a particular set of address prefixes, regardless of the
1957  * link type.  As clarified in rfc2462bis, those two definitions should be
1958  * consistent, and those really are as of August 2004.
1959  */
1960 int
1961 in6_if2idlen(struct ifnet *ifp)
1962 {
1963         switch (ifp->if_type) {
1964         case IFT_ETHER:         /* RFC2464 */
1965         case IFT_PROPVIRTUAL:   /* XXX: no RFC. treat it as ether */
1966         case IFT_L2VLAN:        /* ditto */
1967         case IFT_BRIDGE:        /* bridge(4) only does Ethernet-like links */
1968         case IFT_INFINIBAND:
1969                 return (64);
1970         case IFT_FDDI:          /* RFC2467 */
1971                 return (64);
1972         case IFT_ISO88025:      /* RFC2470 (IPv6 over Token Ring) */
1973                 return (64);
1974         case IFT_PPP:           /* RFC2472 */
1975                 return (64);
1976         case IFT_ARCNET:        /* RFC2497 */
1977                 return (64);
1978         case IFT_FRELAY:        /* RFC2590 */
1979                 return (64);
1980         case IFT_IEEE1394:      /* RFC3146 */
1981                 return (64);
1982         case IFT_GIF:
1983                 return (64);    /* draft-ietf-v6ops-mech-v2-07 */
1984         case IFT_LOOP:
1985                 return (64);    /* XXX: is this really correct? */
1986         default:
1987                 /*
1988                  * Unknown link type:
1989                  * It might be controversial to use the today's common constant
1990                  * of 64 for these cases unconditionally.  For full compliance,
1991                  * we should return an error in this case.  On the other hand,
1992                  * if we simply miss the standard for the link type or a new
1993                  * standard is defined for a new link type, the IFID length
1994                  * is very likely to be the common constant.  As a compromise,
1995                  * we always use the constant, but make an explicit notice
1996                  * indicating the "unknown" case.
1997                  */
1998                 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
1999                 return (64);
2000         }
2001 }
2002
2003 #include <sys/sysctl.h>
2004
2005 struct in6_llentry {
2006         struct llentry          base;
2007 };
2008
2009 #define IN6_LLTBL_DEFAULT_HSIZE 32
2010 #define IN6_LLTBL_HASH(k, h) \
2011         (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
2012
2013 /*
2014  * Do actual deallocation of @lle.
2015  */
2016 static void
2017 in6_lltable_destroy_lle_unlocked(struct llentry *lle)
2018 {
2019
2020         LLE_LOCK_DESTROY(lle);
2021         LLE_REQ_DESTROY(lle);
2022         free(lle, M_LLTABLE);
2023 }
2024
2025 /*
2026  * Called by LLE_FREE_LOCKED when number of references
2027  * drops to zero.
2028  */
2029 static void
2030 in6_lltable_destroy_lle(struct llentry *lle)
2031 {
2032
2033         LLE_WUNLOCK(lle);
2034         in6_lltable_destroy_lle_unlocked(lle);
2035 }
2036
2037 static struct llentry *
2038 in6_lltable_new(const struct in6_addr *addr6, u_int flags)
2039 {
2040         struct in6_llentry *lle;
2041
2042         lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
2043         if (lle == NULL)                /* NB: caller generates msg */
2044                 return NULL;
2045
2046         lle->base.r_l3addr.addr6 = *addr6;
2047         lle->base.lle_refcnt = 1;
2048         lle->base.lle_free = in6_lltable_destroy_lle;
2049         LLE_LOCK_INIT(&lle->base);
2050         LLE_REQ_INIT(&lle->base);
2051         callout_init(&lle->base.lle_timer, 1);
2052
2053         return (&lle->base);
2054 }
2055
2056 static int
2057 in6_lltable_match_prefix(const struct sockaddr *saddr,
2058     const struct sockaddr *smask, u_int flags, struct llentry *lle)
2059 {
2060         const struct in6_addr *addr, *mask, *lle_addr;
2061
2062         addr = &((const struct sockaddr_in6 *)saddr)->sin6_addr;
2063         mask = &((const struct sockaddr_in6 *)smask)->sin6_addr;
2064         lle_addr = &lle->r_l3addr.addr6;
2065
2066         if (IN6_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
2067                 return (0);
2068
2069         if (lle->la_flags & LLE_IFADDR) {
2070
2071                 /*
2072                  * Delete LLE_IFADDR records IFF address & flag matches.
2073                  * Note that addr is the interface address within prefix
2074                  * being matched.
2075                  */
2076                 if (IN6_ARE_ADDR_EQUAL(addr, lle_addr) &&
2077                     (flags & LLE_STATIC) != 0)
2078                         return (1);
2079                 return (0);
2080         }
2081
2082         /* flags & LLE_STATIC means deleting both dynamic and static entries */
2083         if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
2084                 return (1);
2085
2086         return (0);
2087 }
2088
2089 static void
2090 in6_lltable_free_entry(struct lltable *llt, struct llentry *lle)
2091 {
2092         struct ifnet *ifp;
2093
2094         LLE_WLOCK_ASSERT(lle);
2095         KASSERT(llt != NULL, ("lltable is NULL"));
2096
2097         /* Unlink entry from table */
2098         if ((lle->la_flags & LLE_LINKED) != 0) {
2099
2100                 ifp = llt->llt_ifp;
2101                 IF_AFDATA_WLOCK_ASSERT(ifp);
2102                 lltable_unlink_entry(llt, lle);
2103         }
2104
2105         if (callout_stop(&lle->lle_timer) > 0)
2106                 LLE_REMREF(lle);
2107
2108         llentry_free(lle);
2109 }
2110
2111 static int
2112 in6_lltable_rtcheck(struct ifnet *ifp,
2113                     u_int flags,
2114                     const struct sockaddr *l3addr)
2115 {
2116         const struct sockaddr_in6 *sin6;
2117         struct nhop6_basic nh6;
2118         struct in6_addr dst;
2119         uint32_t scopeid;
2120         int error;
2121         char ip6buf[INET6_ADDRSTRLEN];
2122         int fibnum;
2123
2124         KASSERT(l3addr->sa_family == AF_INET6,
2125             ("sin_family %d", l3addr->sa_family));
2126
2127         sin6 = (const struct sockaddr_in6 *)l3addr;
2128         in6_splitscope(&sin6->sin6_addr, &dst, &scopeid);
2129         fibnum = V_rt_add_addr_allfibs ? RT_DEFAULT_FIB : ifp->if_fib;
2130         error = fib6_lookup_nh_basic(fibnum, &dst, scopeid, 0, 0, &nh6);
2131         if (error != 0 || (nh6.nh_flags & NHF_GATEWAY) || nh6.nh_ifp != ifp) {
2132                 struct ifaddr *ifa;
2133                 /*
2134                  * Create an ND6 cache for an IPv6 neighbor
2135                  * that is not covered by our own prefix.
2136                  */
2137                 ifa = ifaof_ifpforaddr(l3addr, ifp);
2138                 if (ifa != NULL) {
2139                         ifa_free(ifa);
2140                         return 0;
2141                 }
2142                 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2143                     ip6_sprintf(ip6buf, &sin6->sin6_addr));
2144                 return EINVAL;
2145         }
2146         return 0;
2147 }
2148
2149 static inline uint32_t
2150 in6_lltable_hash_dst(const struct in6_addr *dst, uint32_t hsize)
2151 {
2152
2153         return (IN6_LLTBL_HASH(dst->s6_addr32[3], hsize));
2154 }
2155
2156 static uint32_t
2157 in6_lltable_hash(const struct llentry *lle, uint32_t hsize)
2158 {
2159
2160         return (in6_lltable_hash_dst(&lle->r_l3addr.addr6, hsize));
2161 }
2162
2163 static void
2164 in6_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
2165 {
2166         struct sockaddr_in6 *sin6;
2167
2168         sin6 = (struct sockaddr_in6 *)sa;
2169         bzero(sin6, sizeof(*sin6));
2170         sin6->sin6_family = AF_INET6;
2171         sin6->sin6_len = sizeof(*sin6);
2172         sin6->sin6_addr = lle->r_l3addr.addr6;
2173 }
2174
2175 static inline struct llentry *
2176 in6_lltable_find_dst(struct lltable *llt, const struct in6_addr *dst)
2177 {
2178         struct llentry *lle;
2179         struct llentries *lleh;
2180         u_int hashidx;
2181
2182         hashidx = in6_lltable_hash_dst(dst, llt->llt_hsize);
2183         lleh = &llt->lle_head[hashidx];
2184         LIST_FOREACH(lle, lleh, lle_next) {
2185                 if (lle->la_flags & LLE_DELETED)
2186                         continue;
2187                 if (IN6_ARE_ADDR_EQUAL(&lle->r_l3addr.addr6, dst))
2188                         break;
2189         }
2190
2191         return (lle);
2192 }
2193
2194 static void
2195 in6_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
2196 {
2197
2198         lle->la_flags |= LLE_DELETED;
2199         EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
2200 #ifdef DIAGNOSTIC
2201         log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2202 #endif
2203         llentry_free(lle);
2204 }
2205
2206 static struct llentry *
2207 in6_lltable_alloc(struct lltable *llt, u_int flags,
2208         const struct sockaddr *l3addr)
2209 {
2210         const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2211         struct ifnet *ifp = llt->llt_ifp;
2212         struct llentry *lle;
2213         char linkhdr[LLE_MAX_LINKHDR];
2214         size_t linkhdrsize;
2215         int lladdr_off;
2216
2217         KASSERT(l3addr->sa_family == AF_INET6,
2218             ("sin_family %d", l3addr->sa_family));
2219
2220         /*
2221          * A route that covers the given address must have
2222          * been installed 1st because we are doing a resolution,
2223          * verify this.
2224          */
2225         if (!(flags & LLE_IFADDR) &&
2226             in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2227                 return (NULL);
2228
2229         lle = in6_lltable_new(&sin6->sin6_addr, flags);
2230         if (lle == NULL) {
2231                 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2232                 return (NULL);
2233         }
2234         lle->la_flags = flags;
2235         if ((flags & LLE_IFADDR) == LLE_IFADDR) {
2236                 linkhdrsize = LLE_MAX_LINKHDR;
2237                 if (lltable_calc_llheader(ifp, AF_INET6, IF_LLADDR(ifp),
2238                     linkhdr, &linkhdrsize, &lladdr_off) != 0) {
2239                         in6_lltable_destroy_lle_unlocked(lle);
2240                         return (NULL);
2241                 }
2242                 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
2243                     lladdr_off);
2244                 lle->la_flags |= LLE_STATIC;
2245         }
2246
2247         if ((lle->la_flags & LLE_STATIC) != 0)
2248                 lle->ln_state = ND6_LLINFO_REACHABLE;
2249
2250         return (lle);
2251 }
2252
2253 static struct llentry *
2254 in6_lltable_lookup(struct lltable *llt, u_int flags,
2255         const struct sockaddr *l3addr)
2256 {
2257         const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2258         struct llentry *lle;
2259
2260         IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
2261         KASSERT(l3addr->sa_family == AF_INET6,
2262             ("sin_family %d", l3addr->sa_family));
2263
2264         lle = in6_lltable_find_dst(llt, &sin6->sin6_addr);
2265
2266         if (lle == NULL)
2267                 return (NULL);
2268
2269         KASSERT((flags & (LLE_UNLOCKED|LLE_EXCLUSIVE)) !=
2270             (LLE_UNLOCKED|LLE_EXCLUSIVE),("wrong lle request flags: 0x%X",
2271             flags));
2272
2273         if (flags & LLE_UNLOCKED)
2274                 return (lle);
2275
2276         if (flags & LLE_EXCLUSIVE)
2277                 LLE_WLOCK(lle);
2278         else
2279                 LLE_RLOCK(lle);
2280         return (lle);
2281 }
2282
2283 static int
2284 in6_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
2285     struct sysctl_req *wr)
2286 {
2287         struct ifnet *ifp = llt->llt_ifp;
2288         /* XXX stack use */
2289         struct {
2290                 struct rt_msghdr        rtm;
2291                 struct sockaddr_in6     sin6;
2292                 /*
2293                  * ndp.c assumes that sdl is word aligned
2294                  */
2295 #ifdef __LP64__
2296                 uint32_t                pad;
2297 #endif
2298                 struct sockaddr_dl      sdl;
2299         } ndpc;
2300         struct sockaddr_dl *sdl;
2301         int error;
2302
2303         bzero(&ndpc, sizeof(ndpc));
2304                         /* skip deleted entries */
2305                         if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
2306                                 return (0);
2307                         /* Skip if jailed and not a valid IP of the prison. */
2308                         lltable_fill_sa_entry(lle,
2309                             (struct sockaddr *)&ndpc.sin6);
2310                         if (prison_if(wr->td->td_ucred,
2311                             (struct sockaddr *)&ndpc.sin6) != 0)
2312                                 return (0);
2313                         /*
2314                          * produce a msg made of:
2315                          *  struct rt_msghdr;
2316                          *  struct sockaddr_in6 (IPv6)
2317                          *  struct sockaddr_dl;
2318                          */
2319                         ndpc.rtm.rtm_msglen = sizeof(ndpc);
2320                         ndpc.rtm.rtm_version = RTM_VERSION;
2321                         ndpc.rtm.rtm_type = RTM_GET;
2322                         ndpc.rtm.rtm_flags = RTF_UP;
2323                         ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2324                         if (V_deembed_scopeid)
2325                                 sa6_recoverscope(&ndpc.sin6);
2326
2327                         /* publish */
2328                         if (lle->la_flags & LLE_PUB)
2329                                 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2330
2331                         sdl = &ndpc.sdl;
2332                         sdl->sdl_family = AF_LINK;
2333                         sdl->sdl_len = sizeof(*sdl);
2334                         sdl->sdl_index = ifp->if_index;
2335                         sdl->sdl_type = ifp->if_type;
2336                         if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
2337                                 sdl->sdl_alen = ifp->if_addrlen;
2338                                 bcopy(lle->ll_addr, LLADDR(sdl),
2339                                     ifp->if_addrlen);
2340                         } else {
2341                                 sdl->sdl_alen = 0;
2342                                 bzero(LLADDR(sdl), ifp->if_addrlen);
2343                         }
2344                         if (lle->la_expire != 0)
2345                                 ndpc.rtm.rtm_rmx.rmx_expire = lle->la_expire +
2346                                     lle->lle_remtime / hz +
2347                                     time_second - time_uptime;
2348                         ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2349                         if (lle->la_flags & LLE_STATIC)
2350                                 ndpc.rtm.rtm_flags |= RTF_STATIC;
2351                         if (lle->la_flags & LLE_IFADDR)
2352                                 ndpc.rtm.rtm_flags |= RTF_PINNED;
2353                         if (lle->ln_router != 0)
2354                                 ndpc.rtm.rtm_flags |= RTF_GATEWAY;
2355                         ndpc.rtm.rtm_rmx.rmx_pksent = lle->la_asked;
2356                         /* Store state in rmx_weight value */
2357                         ndpc.rtm.rtm_rmx.rmx_state = lle->ln_state;
2358                         ndpc.rtm.rtm_index = ifp->if_index;
2359                         error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2360
2361         return (error);
2362 }
2363
2364 static struct lltable *
2365 in6_lltattach(struct ifnet *ifp)
2366 {
2367         struct lltable *llt;
2368
2369         llt = lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE);
2370         llt->llt_af = AF_INET6;
2371         llt->llt_ifp = ifp;
2372
2373         llt->llt_lookup = in6_lltable_lookup;
2374         llt->llt_alloc_entry = in6_lltable_alloc;
2375         llt->llt_delete_entry = in6_lltable_delete_entry;
2376         llt->llt_dump_entry = in6_lltable_dump_entry;
2377         llt->llt_hash = in6_lltable_hash;
2378         llt->llt_fill_sa_entry = in6_lltable_fill_sa_entry;
2379         llt->llt_free_entry = in6_lltable_free_entry;
2380         llt->llt_match_prefix = in6_lltable_match_prefix;
2381         lltable_link(llt);
2382
2383         return (llt);
2384 }
2385
2386 void *
2387 in6_domifattach(struct ifnet *ifp)
2388 {
2389         struct in6_ifextra *ext;
2390
2391         /* There are not IPv6-capable interfaces. */
2392         switch (ifp->if_type) {
2393         case IFT_PFLOG:
2394         case IFT_PFSYNC:
2395         case IFT_USB:
2396                 return (NULL);
2397         }
2398         ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2399         bzero(ext, sizeof(*ext));
2400
2401         ext->in6_ifstat = malloc(sizeof(counter_u64_t) *
2402             sizeof(struct in6_ifstat) / sizeof(uint64_t), M_IFADDR, M_WAITOK);
2403         COUNTER_ARRAY_ALLOC(ext->in6_ifstat,
2404             sizeof(struct in6_ifstat) / sizeof(uint64_t), M_WAITOK);
2405
2406         ext->icmp6_ifstat = malloc(sizeof(counter_u64_t) *
2407             sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_IFADDR,
2408             M_WAITOK);
2409         COUNTER_ARRAY_ALLOC(ext->icmp6_ifstat,
2410             sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_WAITOK);
2411
2412         ext->nd_ifinfo = nd6_ifattach(ifp);
2413         ext->scope6_id = scope6_ifattach(ifp);
2414         ext->lltable = in6_lltattach(ifp);
2415
2416         ext->mld_ifinfo = mld_domifattach(ifp);
2417
2418         return ext;
2419 }
2420
2421 int
2422 in6_domifmtu(struct ifnet *ifp)
2423 {
2424         if (ifp->if_afdata[AF_INET6] == NULL)
2425                 return ifp->if_mtu;
2426
2427         return (IN6_LINKMTU(ifp));
2428 }
2429
2430 void
2431 in6_domifdetach(struct ifnet *ifp, void *aux)
2432 {
2433         struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2434
2435         mld_domifdetach(ifp);
2436         scope6_ifdetach(ext->scope6_id);
2437         nd6_ifdetach(ifp, ext->nd_ifinfo);
2438         lltable_free(ext->lltable);
2439         COUNTER_ARRAY_FREE(ext->in6_ifstat,
2440             sizeof(struct in6_ifstat) / sizeof(uint64_t));
2441         free(ext->in6_ifstat, M_IFADDR);
2442         COUNTER_ARRAY_FREE(ext->icmp6_ifstat,
2443             sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
2444         free(ext->icmp6_ifstat, M_IFADDR);
2445         free(ext, M_IFADDR);
2446 }
2447
2448 /*
2449  * Convert sockaddr_in6 to sockaddr_in.  Original sockaddr_in6 must be
2450  * v4 mapped addr or v4 compat addr
2451  */
2452 void
2453 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2454 {
2455
2456         bzero(sin, sizeof(*sin));
2457         sin->sin_len = sizeof(struct sockaddr_in);
2458         sin->sin_family = AF_INET;
2459         sin->sin_port = sin6->sin6_port;
2460         sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2461 }
2462
2463 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2464 void
2465 in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2466 {
2467         bzero(sin6, sizeof(*sin6));
2468         sin6->sin6_len = sizeof(struct sockaddr_in6);
2469         sin6->sin6_family = AF_INET6;
2470         sin6->sin6_port = sin->sin_port;
2471         sin6->sin6_addr.s6_addr32[0] = 0;
2472         sin6->sin6_addr.s6_addr32[1] = 0;
2473         sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2474         sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2475 }
2476
2477 /* Convert sockaddr_in6 into sockaddr_in. */
2478 void
2479 in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2480 {
2481         struct sockaddr_in *sin_p;
2482         struct sockaddr_in6 sin6;
2483
2484         /*
2485          * Save original sockaddr_in6 addr and convert it
2486          * to sockaddr_in.
2487          */
2488         sin6 = *(struct sockaddr_in6 *)nam;
2489         sin_p = (struct sockaddr_in *)nam;
2490         in6_sin6_2_sin(sin_p, &sin6);
2491 }
2492
2493 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2494 void
2495 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2496 {
2497         struct sockaddr_in *sin_p;
2498         struct sockaddr_in6 *sin6_p;
2499
2500         sin6_p = malloc(sizeof *sin6_p, M_SONAME, M_WAITOK);
2501         sin_p = (struct sockaddr_in *)*nam;
2502         in6_sin_2_v4mapsin6(sin_p, sin6_p);
2503         free(*nam, M_SONAME);
2504         *nam = (struct sockaddr *)sin6_p;
2505 }