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