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