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