in6.c revision 121742
1252661Snp/*	$FreeBSD: head/sys/netinet6/in6.c 121742 2003-10-30 15:29:17Z ume $	*/
2252661Snp/*	$KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $	*/
3319269Snp
4252661Snp/*
5285527Snp * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6285527Snp * All rights reserved.
7285527Snp *
8252661Snp * Redistribution and use in source and binary forms, with or without
9285527Snp * modification, are permitted provided that the following conditions
10285527Snp * are met:
11252661Snp * 1. Redistributions of source code must retain the above copyright
12285527Snp *    notice, this list of conditions and the following disclaimer.
13285527Snp * 2. Redistributions in binary form must reproduce the above copyright
14285527Snp *    notice, this list of conditions and the following disclaimer in the
15285527Snp *    documentation and/or other materials provided with the distribution.
16252661Snp * 3. Neither the name of the project nor the names of its contributors
17285527Snp *    may be used to endorse or promote products derived from this software
18285527Snp *    without specific prior written permission.
19252661Snp *
20252661Snp * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21298976Spfg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22285527Snp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23285527Snp * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24285527Snp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25285527Snp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26252661Snp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27252661Snp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28252661Snp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29252661Snp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30252661Snp * SUCH DAMAGE.
31285527Snp */
32252661Snp
33252661Snp/*
34252661Snp * Copyright (c) 1982, 1986, 1991, 1993
35252661Snp *	The Regents of the University of California.  All rights reserved.
36252661Snp *
37252661Snp * Redistribution and use in source and binary forms, with or without
38252661Snp * modification, are permitted provided that the following conditions
39252661Snp * are met:
40252661Snp * 1. Redistributions of source code must retain the above copyright
41252661Snp *    notice, this list of conditions and the following disclaimer.
42252661Snp * 2. Redistributions in binary form must reproduce the above copyright
43252661Snp *    notice, this list of conditions and the following disclaimer in the
44252661Snp *    documentation and/or other materials provided with the distribution.
45252661Snp * 3. All advertising materials mentioning features or use of this software
46252661Snp *    must display the following acknowledgement:
47252661Snp *	This product includes software developed by the University of
48252661Snp *	California, Berkeley and its contributors.
49252661Snp * 4. Neither the name of the University nor the names of its contributors
50252661Snp *    may be used to endorse or promote products derived from this software
51252661Snp *    without specific prior written permission.
52285527Snp *
53285527Snp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54285527Snp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55285527Snp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56252661Snp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57252661Snp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58252661Snp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59252661Snp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60252661Snp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61252661Snp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62252661Snp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63252661Snp * SUCH DAMAGE.
64252661Snp *
65252661Snp *	@(#)in.c	8.2 (Berkeley) 11/15/93
66285527Snp */
67252661Snp
68252661Snp#include "opt_inet.h"
69252661Snp#include "opt_inet6.h"
70252661Snp
71252661Snp#include <sys/param.h>
72252661Snp#include <sys/errno.h>
73252661Snp#include <sys/malloc.h>
74252661Snp#include <sys/socket.h>
75252661Snp#include <sys/socketvar.h>
76252661Snp#include <sys/sockio.h>
77252661Snp#include <sys/systm.h>
78252661Snp#include <sys/proc.h>
79252661Snp#include <sys/time.h>
80252661Snp#include <sys/kernel.h>
81252661Snp#include <sys/syslog.h>
82252661Snp
83252661Snp#include <net/if.h>
84252661Snp#include <net/if_types.h>
85252661Snp#include <net/route.h>
86252661Snp#include <net/if_dl.h>
87252661Snp
88252661Snp#include <netinet/in.h>
89252661Snp#include <netinet/in_var.h>
90252661Snp#include <netinet/if_ether.h>
91252661Snp#include <netinet/in_systm.h>
92252661Snp#include <netinet/ip.h>
93252661Snp#include <netinet/in_pcb.h>
94311506Snp
95311506Snp#include <netinet/ip6.h>
96252661Snp#include <netinet6/ip6_var.h>
97252661Snp#include <netinet6/nd6.h>
98252661Snp#include <netinet6/mld6_var.h>
99252661Snp#include <netinet6/ip6_mroute.h>
100252661Snp#include <netinet6/in6_ifattach.h>
101252661Snp#include <netinet6/scope6_var.h>
102252661Snp#include <netinet6/in6_pcb.h>
103252661Snp
104252661Snp#include <net/net_osdep.h>
105252661Snp
106252661SnpMALLOC_DEFINE(M_IPMADDR, "in6_multi", "internet multicast address");
107252661Snp
108252661Snp/*
109252661Snp * Definitions of some costant IP6 addresses.
110252661Snp */
111252661Snpconst struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
112252661Snpconst struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
113252661Snpconst struct in6_addr in6addr_nodelocal_allnodes =
114252661Snp	IN6ADDR_NODELOCAL_ALLNODES_INIT;
115252661Snpconst struct in6_addr in6addr_linklocal_allnodes =
116252661Snp	IN6ADDR_LINKLOCAL_ALLNODES_INIT;
117252661Snpconst struct in6_addr in6addr_linklocal_allrouters =
118252661Snp	IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
119252661Snp
120252661Snpconst struct in6_addr in6mask0 = IN6MASK0;
121252661Snpconst struct in6_addr in6mask32 = IN6MASK32;
122256459Snpconst struct in6_addr in6mask64 = IN6MASK64;
123256459Snpconst struct in6_addr in6mask96 = IN6MASK96;
124256459Snpconst struct in6_addr in6mask128 = IN6MASK128;
125296249Snp
126296249Snpconst struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
127296249Snp				     0, 0, IN6ADDR_ANY_INIT, 0};
128296249Snp
129296249Snpstatic int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
130296249Snp	struct ifnet *, struct thread *));
131285527Snpstatic int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
132252661Snp	struct sockaddr_in6 *, int));
133256459Snpstatic void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));
134256459Snp
135256459Snpstruct in6_multihead in6_multihead;	/* XXX BSS initialization */
136256459Snpint	(*faithprefix_p)(struct in6_addr *);
137256459Snp
138256459Snp/*
139285527Snp * Subroutine for in6_ifaddloop() and in6_ifremloop().
140256459Snp * This routine does actual work.
141256459Snp */
142252661Snpstatic void
143252661Snpin6_ifloop_request(int cmd, struct ifaddr *ifa)
144252661Snp{
145252661Snp	struct sockaddr_in6 all1_sa;
146285527Snp	struct rtentry *nrt = NULL;
147252661Snp	int e;
148252661Snp
149252661Snp	bzero(&all1_sa, sizeof(all1_sa));
150252661Snp	all1_sa.sin6_family = AF_INET6;
151252661Snp	all1_sa.sin6_len = sizeof(struct sockaddr_in6);
152252661Snp	all1_sa.sin6_addr = in6mask128;
153252661Snp
154252661Snp	/*
155252661Snp	 * We specify the address itself as the gateway, and set the
156285527Snp	 * RTF_LLINFO flag, so that the corresponding host route would have
157252661Snp	 * the flag, and thus applications that assume traditional behavior
158252661Snp	 * would be happy.  Note that we assume the caller of the function
159252661Snp	 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
160252661Snp	 * which changes the outgoing interface to the loopback interface.
161252661Snp	 */
162252661Snp	e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr,
163252661Snp	    (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt);
164256459Snp	if (e != 0) {
165256459Snp		/* XXX need more descriptive message */
166256459Snp		log(LOG_ERR, "in6_ifloop_request: "
167252661Snp		    "%s operation failed for %s (errno=%d)\n",
168252661Snp		    cmd == RTM_ADD ? "ADD" : "DELETE",
169252661Snp		    ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
170267757Snp		    e);
171267757Snp	}
172267757Snp
173256459Snp	if (nrt) {
174256459Snp		RT_LOCK(nrt);
175256459Snp		/*
176285527Snp		 * Make sure rt_ifa be equal to IFA, the second argument of
177285527Snp		 * the function.  We need this because when we refer to
178285527Snp		 * rt_ifa->ia6_flags in ip6_input, we assume that the rt_ifa
179252661Snp		 * points to the address instead of the loopback address.
180252661Snp		 */
181252661Snp		if (cmd == RTM_ADD && ifa != nrt->rt_ifa) {
182256459Snp			IFAFREE(nrt->rt_ifa);
183256459Snp			IFAREF(ifa);
184256459Snp			nrt->rt_ifa = ifa;
185256459Snp		}
186296249Snp
187296249Snp		/*
188296249Snp		 * Report the addition/removal of the address to the routing
189296249Snp		 * socket.
190252661Snp		 *
191252661Snp		 * XXX: since we called rtinit for a p2p interface with a
192252661Snp		 *      destination, we end up reporting twice in such a case.
193252661Snp		 *      Should we rather omit the second report?
194252661Snp		 */
195252661Snp		rt_newaddrmsg(cmd, ifa, e, nrt);
196252661Snp		if (cmd == RTM_DELETE) {
197252661Snp			rtfree(nrt);
198252661Snp		} else {
199252661Snp			/* the cmd must be RTM_ADD here */
200252661Snp			nrt->rt_refcnt--;
201252661Snp			RT_UNLOCK(nrt);
202252661Snp		}
203252661Snp	}
204252661Snp}
205252661Snp
206252661Snp/*
207252661Snp * Add ownaddr as loopback rtentry.  We previously add the route only if
208252661Snp * necessary (ex. on a p2p link).  However, since we now manage addresses
209252661Snp * separately from prefixes, we should always add the route.  We can't
210252661Snp * rely on the cloning mechanism from the corresponding interface route
211252661Snp * any more.
212252661Snp */
213252661Snpstatic void
214252661Snpin6_ifaddloop(struct ifaddr *ifa)
215252661Snp{
216252661Snp	struct rtentry *rt;
217252661Snp	int need_loop;
218285527Snp
219252661Snp	/* If there is no loopback entry, allocate one. */
220252661Snp	rt = rtalloc1(ifa->ifa_addr, 0, 0);
221252661Snp	need_loop = (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 ||
222252661Snp	    (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0);
223252661Snp	if (rt)
224252661Snp		rtfree(rt);
225252661Snp	if (need_loop)
226252661Snp		in6_ifloop_request(RTM_ADD, ifa);
227252661Snp}
228252661Snp
229252661Snp/*
230252661Snp * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
231252661Snp * if it exists.
232252661Snp */
233252661Snpstatic void
234252661Snpin6_ifremloop(struct ifaddr *ifa)
235252661Snp{
236252661Snp	struct in6_ifaddr *ia;
237252661Snp	struct rtentry *rt;
238252661Snp	int ia_count = 0;
239252661Snp
240252661Snp	/*
241252661Snp	 * Some of BSD variants do not remove cloned routes
242252661Snp	 * from an interface direct route, when removing the direct route
243252661Snp	 * (see comments in net/net_osdep.h).  Even for variants that do remove
244252661Snp	 * cloned routes, they could fail to remove the cloned routes when
245252661Snp	 * we handle multple addresses that share a common prefix.
246252661Snp	 * So, we should remove the route corresponding to the deleted address
247252661Snp	 * regardless of the result of in6_is_ifloop_auto().
248252661Snp	 */
249252661Snp
250252661Snp	/*
251252661Snp	 * Delete the entry only if exact one ifa exists.  More than one ifa
252252661Snp	 * can exist if we assign a same single address to multiple
253252661Snp	 * (probably p2p) interfaces.
254252661Snp	 * XXX: we should avoid such a configuration in IPv6...
255252661Snp	 */
256252661Snp	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
257252661Snp		if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
258252661Snp			ia_count++;
259252661Snp			if (ia_count > 1)
260252661Snp				break;
261252661Snp		}
262252661Snp	}
263252661Snp
264252661Snp	if (ia_count == 1) {
265252661Snp		/*
266252661Snp		 * Before deleting, check if a corresponding loopbacked host
267252661Snp		 * route surely exists.  With this check, we can avoid to
268252661Snp		 * delete an interface direct route whose destination is same
269252661Snp		 * as the address being removed.  This can happen when removing
270252661Snp		 * a subnet-router anycast address on an interface attahced
271252661Snp		 * to a shared medium.
272252661Snp		 */
273252661Snp		rt = rtalloc1(ifa->ifa_addr, 0, 0);
274252661Snp		if (rt != NULL) {
275252661Snp			if ((rt->rt_flags & RTF_HOST) != 0 &&
276252661Snp			    (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
277252661Snp				rtfree(rt);
278252661Snp				in6_ifloop_request(RTM_DELETE, ifa);
279252661Snp			} else
280252661Snp				RT_UNLOCK(rt);
281252661Snp		}
282252661Snp	}
283252661Snp}
284252661Snp
285252661Snpint
286252661Snpin6_mask2len(mask, lim0)
287252661Snp	struct in6_addr *mask;
288252661Snp	u_char *lim0;
289252661Snp{
290252661Snp	int x = 0, y;
291252661Snp	u_char *lim = lim0, *p;
292252661Snp
293252661Snp	/* ignore the scope_id part */
294252661Snp	if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
295252661Snp		lim = (u_char *)mask + sizeof(*mask);
296252661Snp	for (p = (u_char *)mask; p < lim; x++, p++) {
297252661Snp		if (*p != 0xff)
298252661Snp			break;
299252661Snp	}
300252661Snp	y = 0;
301252661Snp	if (p < lim) {
302252661Snp		for (y = 0; y < 8; y++) {
303252661Snp			if ((*p & (0x80 >> y)) == 0)
304252661Snp				break;
305252661Snp		}
306252661Snp	}
307252661Snp
308252661Snp	/*
309252661Snp	 * when the limit pointer is given, do a stricter check on the
310252661Snp	 * remaining bits.
311252661Snp	 */
312252661Snp	if (p < lim) {
313298976Spfg		if (y != 0 && (*p & (0x00ff >> y)) != 0)
314252661Snp			return (-1);
315252661Snp		for (p = p + 1; p < lim; p++)
316252661Snp			if (*p != 0)
317252661Snp				return (-1);
318252661Snp	}
319252661Snp
320252661Snp	return x * 8 + y;
321252661Snp}
322285527Snp
323252661Snp#define ifa2ia6(ifa)	((struct in6_ifaddr *)(ifa))
324252661Snp#define ia62ifa(ia6)	(&((ia6)->ia_ifa))
325252661Snp
326252661Snpint
327252661Snpin6_control(so, cmd, data, ifp, td)
328285527Snp	struct	socket *so;
329252661Snp	u_long cmd;
330285527Snp	caddr_t	data;
331252661Snp	struct ifnet *ifp;
332252661Snp	struct thread *td;
333252661Snp{
334252661Snp	struct	in6_ifreq *ifr = (struct in6_ifreq *)data;
335252661Snp	struct	in6_ifaddr *ia = NULL;
336285527Snp	struct	in6_aliasreq *ifra = (struct in6_aliasreq *)data;
337252661Snp	int privileged;
338252661Snp
339252661Snp	privileged = 0;
340252661Snp	if (td == NULL || !suser(td))
341252661Snp		privileged++;
342252661Snp
343252661Snp	switch (cmd) {
344252661Snp	case SIOCGETSGCNT_IN6:
345252661Snp	case SIOCGETMIFCNT_IN6:
346252661Snp		return (mrt6_ioctl(cmd, data));
347252661Snp	}
348252661Snp
349252661Snp	switch(cmd) {
350252661Snp	case SIOCAADDRCTL_POLICY:
351252661Snp	case SIOCDADDRCTL_POLICY:
352252661Snp		if (!privileged)
353252661Snp			return (EPERM);
354252661Snp		return (in6_src_ioctl(cmd, data));
355285527Snp	}
356252661Snp
357252661Snp	if (ifp == NULL)
358252661Snp		return (EOPNOTSUPP);
359252661Snp
360252661Snp	switch (cmd) {
361252661Snp	case SIOCSNDFLUSH_IN6:
362252661Snp	case SIOCSPFXFLUSH_IN6:
363252661Snp	case SIOCSRTRFLUSH_IN6:
364252661Snp	case SIOCSDEFIFACE_IN6:
365252661Snp	case SIOCSIFINFO_FLAGS:
366252661Snp		if (!privileged)
367252661Snp			return (EPERM);
368285527Snp		/* FALLTHROUGH */
369252661Snp	case OSIOCGIFINFO_IN6:
370252661Snp	case SIOCGIFINFO_IN6:
371252661Snp	case SIOCGDRLST_IN6:
372252661Snp	case SIOCGPRLST_IN6:
373252661Snp	case SIOCGNBRINFO_IN6:
374252661Snp	case SIOCGDEFIFACE_IN6:
375252661Snp		return (nd6_ioctl(cmd, data, ifp));
376252661Snp	}
377252661Snp
378252661Snp	switch (cmd) {
379252661Snp	case SIOCSIFPREFIX_IN6:
380252661Snp	case SIOCDIFPREFIX_IN6:
381285527Snp	case SIOCAIFPREFIX_IN6:
382252661Snp	case SIOCCIFPREFIX_IN6:
383252661Snp	case SIOCSGIFPREFIX_IN6:
384252661Snp	case SIOCGIFPREFIX_IN6:
385252661Snp		log(LOG_NOTICE,
386252661Snp		    "prefix ioctls are now invalidated. "
387252661Snp		    "please use ifconfig.\n");
388252661Snp		return (EOPNOTSUPP);
389252661Snp	}
390252661Snp
391252661Snp	switch (cmd) {
392252661Snp	case SIOCSSCOPE6:
393252661Snp		if (!privileged)
394285527Snp			return (EPERM);
395252661Snp		return (scope6_set(ifp,
396252661Snp		    (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
397252661Snp	case SIOCGSCOPE6:
398252661Snp		return (scope6_get(ifp,
399252661Snp		    (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
400252661Snp	case SIOCGSCOPE6DEF:
401252661Snp		return (scope6_get_default((struct scope6_id *)
402252661Snp		    ifr->ifr_ifru.ifru_scope_id));
403252661Snp	}
404252661Snp
405252661Snp	switch (cmd) {
406252661Snp	case SIOCALIFADDR:
407252661Snp	case SIOCDLIFADDR:
408285527Snp		if (!privileged)
409252661Snp			return (EPERM);
410252661Snp		/* FALLTHROUGH */
411252661Snp	case SIOCGLIFADDR:
412252661Snp		return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
413252661Snp	}
414252661Snp
415252661Snp	/*
416252661Snp	 * Find address for this interface, if it exists.
417252661Snp	 */
418346940Snp	if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
419252661Snp		struct sockaddr_in6 *sa6 =
420252661Snp			(struct sockaddr_in6 *)&ifra->ifra_addr;
421252661Snp
422252661Snp		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
423252661Snp			if (sa6->sin6_addr.s6_addr16[1] == 0) {
424252661Snp				/* link ID is not embedded by the user */
425252661Snp				sa6->sin6_addr.s6_addr16[1] =
426285527Snp				    htons(ifp->if_index);
427252661Snp			} else if (sa6->sin6_addr.s6_addr16[1] !=
428252661Snp			    htons(ifp->if_index)) {
429252661Snp				return (EINVAL);	/* link ID contradicts */
430252661Snp			}
431252661Snp			if (sa6->sin6_scope_id) {
432252661Snp				if (sa6->sin6_scope_id !=
433252661Snp				    (u_int32_t)ifp->if_index)
434252661Snp					return (EINVAL);
435252661Snp				sa6->sin6_scope_id = 0; /* XXX: good way? */
436252661Snp			}
437252661Snp		}
438285527Snp		ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
439252661Snp	}
440252661Snp
441252661Snp	switch (cmd) {
442252661Snp	case SIOCSIFADDR_IN6:
443267757Snp	case SIOCSIFDSTADDR_IN6:
444252661Snp	case SIOCSIFNETMASK_IN6:
445252661Snp		/*
446252661Snp		 * Since IPv6 allows a node to assign multiple addresses
447252661Snp		 * on a single interface, SIOCSIFxxx ioctls are not suitable
448252661Snp		 * and should be unused.
449252661Snp		 */
450252661Snp		/* we decided to obsolete this command (20000704) */
451285527Snp		return (EINVAL);
452252661Snp
453252661Snp	case SIOCDIFADDR_IN6:
454252661Snp		/*
455252661Snp		 * for IPv4, we look for existing in_ifaddr here to allow
456252661Snp		 * "ifconfig if0 delete" to remove first IPv4 address on the
457252661Snp		 * interface.  For IPv6, as the spec allow multiple interface
458252661Snp		 * address from the day one, we consider "remove the first one"
459252661Snp		 * semantics to be not preferable.
460252661Snp		 */
461252661Snp		if (ia == NULL)
462252661Snp			return (EADDRNOTAVAIL);
463252661Snp		/* FALLTHROUGH */
464252661Snp	case SIOCAIFADDR_IN6:
465267757Snp		/*
466252661Snp		 * We always require users to specify a valid IPv6 address for
467252661Snp		 * the corresponding operation.
468252661Snp		 */
469252661Snp		if (ifra->ifra_addr.sin6_family != AF_INET6 ||
470252661Snp		    ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
471252661Snp			return (EAFNOSUPPORT);
472285527Snp		if (!privileged)
473252661Snp			return (EPERM);
474252661Snp
475252661Snp		break;
476252661Snp
477252661Snp	case SIOCGIFADDR_IN6:
478252661Snp		/* This interface is basically deprecated. use SIOCGIFCONF. */
479252661Snp		/* FALLTHROUGH */
480252661Snp	case SIOCGIFAFLAG_IN6:
481252661Snp	case SIOCGIFNETMASK_IN6:
482252661Snp	case SIOCGIFDSTADDR_IN6:
483252661Snp	case SIOCGIFALIFETIME_IN6:
484252661Snp		/* must think again about its semantics */
485252661Snp		if (ia == NULL)
486285527Snp			return (EADDRNOTAVAIL);
487252661Snp		break;
488252661Snp	case SIOCSIFALIFETIME_IN6:
489252661Snp	    {
490252661Snp		struct in6_addrlifetime *lt;
491252661Snp
492252661Snp		if (!privileged)
493252661Snp			return (EPERM);
494252661Snp		if (ia == NULL)
495252661Snp			return (EADDRNOTAVAIL);
496252661Snp		/* sanity for overflow - beware unsigned */
497319269Snp		lt = &ifr->ifr_ifru.ifru_lifetime;
498319269Snp		if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
499319269Snp		 && lt->ia6t_vltime + time_second < time_second) {
500252661Snp			return EINVAL;
501252661Snp		}
502252661Snp		if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
503252661Snp		 && lt->ia6t_pltime + time_second < time_second) {
504285527Snp			return EINVAL;
505252661Snp		}
506252661Snp		break;
507252661Snp	    }
508252661Snp	}
509319269Snp
510319269Snp	switch (cmd) {
511319269Snp
512252661Snp	case SIOCGIFADDR_IN6:
513252661Snp		ifr->ifr_addr = ia->ia_addr;
514252661Snp		break;
515252661Snp
516285527Snp	case SIOCGIFDSTADDR_IN6:
517252661Snp		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
518252661Snp			return (EINVAL);
519252661Snp		/*
520252661Snp		 * XXX: should we check if ifa_dstaddr is NULL and return
521319269Snp		 * an error?
522319269Snp		 */
523319269Snp		ifr->ifr_dstaddr = ia->ia_dstaddr;
524252661Snp		break;
525252661Snp
526252661Snp	case SIOCGIFNETMASK_IN6:
527252661Snp		ifr->ifr_addr = ia->ia_prefixmask;
528285527Snp		break;
529252661Snp
530252661Snp	case SIOCGIFAFLAG_IN6:
531252661Snp		ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
532252661Snp		break;
533319269Snp
534319269Snp	case SIOCGIFSTAT_IN6:
535319269Snp		if (ifp == NULL)
536252661Snp			return EINVAL;
537252661Snp		bzero(&ifr->ifr_ifru.ifru_stat,
538252661Snp		    sizeof(ifr->ifr_ifru.ifru_stat));
539252661Snp		ifr->ifr_ifru.ifru_stat =
540285527Snp		    *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
541252661Snp		break;
542252661Snp
543252661Snp	case SIOCGIFSTAT_ICMP6:
544252661Snp		if (ifp == NULL)
545252661Snp			return EINVAL;
546252661Snp		bzero(&ifr->ifr_ifru.ifru_stat,
547252661Snp		    sizeof(ifr->ifr_ifru.ifru_icmp6stat));
548252661Snp		ifr->ifr_ifru.ifru_icmp6stat =
549252661Snp		    *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
550252661Snp		break;
551252661Snp
552252661Snp	case SIOCGIFALIFETIME_IN6:
553252661Snp		ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
554252661Snp		break;
555252661Snp
556252661Snp	case SIOCSIFALIFETIME_IN6:
557252661Snp		ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
558252661Snp		/* for sanity */
559252661Snp		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
560252661Snp			ia->ia6_lifetime.ia6t_expire =
561252661Snp				time_second + ia->ia6_lifetime.ia6t_vltime;
562267757Snp		} else
563267757Snp			ia->ia6_lifetime.ia6t_expire = 0;
564267757Snp		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
565252661Snp			ia->ia6_lifetime.ia6t_preferred =
566285527Snp				time_second + ia->ia6_lifetime.ia6t_pltime;
567252661Snp		} else
568252661Snp			ia->ia6_lifetime.ia6t_preferred = 0;
569252661Snp		break;
570252661Snp
571252661Snp	case SIOCAIFADDR_IN6:
572252661Snp	{
573252661Snp		int i, error = 0;
574267757Snp		struct nd_prefix pr0, *pr;
575267757Snp
576267757Snp		/*
577252661Snp		 * first, make or update the interface address structure,
578285527Snp		 * and link it to the list.
579252661Snp		 */
580252661Snp		if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
581252661Snp			return (error);
582252661Snp
583252661Snp		/*
584252661Snp		 * then, make the prefix on-link on the interface.
585252661Snp		 * XXX: we'd rather create the prefix before the address, but
586267757Snp		 * we need at least one address to install the corresponding
587267757Snp		 * interface route, so we configure the address first.
588267757Snp		 */
589252661Snp
590285527Snp		/*
591252661Snp		 * convert mask to prefix length (prefixmask has already
592252661Snp		 * been validated in in6_update_ifa().
593252661Snp		 */
594252661Snp		bzero(&pr0, sizeof(pr0));
595252661Snp		pr0.ndpr_ifp = ifp;
596252661Snp		pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
597252661Snp		    NULL);
598267757Snp		if (pr0.ndpr_plen == 128) {
599267757Snp			break;	/* we don't need to install a host route. */
600267757Snp		}
601252661Snp		pr0.ndpr_prefix = ifra->ifra_addr;
602285527Snp		pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
603252661Snp		/* apply the mask for safety. */
604346940Snp		for (i = 0; i < 4; i++) {
605346940Snp			pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
606252661Snp			    ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
607252661Snp		}
608252661Snp		/*
609252661Snp		 * XXX: since we don't have an API to set prefix (not address)
610252661Snp		 * lifetimes, we just use the same lifetimes as addresses.
611252661Snp		 * The (temporarily) installed lifetimes can be overridden by
612252661Snp		 * later advertised RAs (when accept_rtadv is non 0), which is
613252661Snp		 * an intended behavior.
614252661Snp		 */
615252661Snp		pr0.ndpr_raf_onlink = 1; /* should be configurable? */
616252661Snp		pr0.ndpr_raf_auto =
617		    ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
618		pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
619		pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
620
621		/* add the prefix if not yet. */
622		if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
623			/*
624			 * nd6_prelist_add will install the corresponding
625			 * interface route.
626			 */
627			if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
628				return (error);
629			if (pr == NULL) {
630				log(LOG_ERR, "nd6_prelist_add succeeded but "
631				    "no prefix\n");
632				return (EINVAL); /* XXX panic here? */
633			}
634		}
635		if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
636		    == NULL) {
637		    	/* XXX: this should not happen! */
638			log(LOG_ERR, "in6_control: addition succeeded, but"
639			    " no ifaddr\n");
640		} else {
641			if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
642			    ia->ia6_ndpr == NULL) { /* new autoconfed addr */
643				ia->ia6_ndpr = pr;
644				pr->ndpr_refcnt++;
645
646				/*
647				 * If this is the first autoconf address from
648				 * the prefix, create a temporary address
649				 * as well (when specified).
650				 */
651				if (ip6_use_tempaddr &&
652				    pr->ndpr_refcnt == 1) {
653					int e;
654					if ((e = in6_tmpifadd(ia, 1)) != 0) {
655						log(LOG_NOTICE, "in6_control: "
656						    "failed to create a "
657						    "temporary address, "
658						    "errno=%d\n",
659						    e);
660					}
661				}
662			}
663
664			/*
665			 * this might affect the status of autoconfigured
666			 * addresses, that is, this address might make
667			 * other addresses detached.
668			 */
669			pfxlist_onlink_check();
670		}
671		break;
672	}
673
674	case SIOCDIFADDR_IN6:
675	{
676		int i = 0;
677		struct nd_prefix pr0, *pr;
678
679		/*
680		 * If the address being deleted is the only one that owns
681		 * the corresponding prefix, expire the prefix as well.
682		 * XXX: theoretically, we don't have to worry about such
683		 * relationship, since we separate the address management
684		 * and the prefix management.  We do this, however, to provide
685		 * as much backward compatibility as possible in terms of
686		 * the ioctl operation.
687		 */
688		bzero(&pr0, sizeof(pr0));
689		pr0.ndpr_ifp = ifp;
690		pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
691					     NULL);
692		if (pr0.ndpr_plen == 128)
693			goto purgeaddr;
694		pr0.ndpr_prefix = ia->ia_addr;
695		pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
696		for (i = 0; i < 4; i++) {
697			pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
698				ia->ia_prefixmask.sin6_addr.s6_addr32[i];
699		}
700		/*
701		 * The logic of the following condition is a bit complicated.
702		 * We expire the prefix when
703		 * 1. the address obeys autoconfiguration and it is the
704		 *    only owner of the associated prefix, or
705		 * 2. the address does not obey autoconf and there is no
706		 *    other owner of the prefix.
707		 */
708		if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
709		    (((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0 &&
710		      pr->ndpr_refcnt == 1) ||
711		     ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0 &&
712		      pr->ndpr_refcnt == 0))) {
713			pr->ndpr_expire = 1; /* XXX: just for expiration */
714		}
715
716	  purgeaddr:
717		in6_purgeaddr(&ia->ia_ifa);
718		break;
719	}
720
721	default:
722		if (ifp == NULL || ifp->if_ioctl == 0)
723			return (EOPNOTSUPP);
724		return ((*ifp->if_ioctl)(ifp, cmd, data));
725	}
726
727	return (0);
728}
729
730/*
731 * Update parameters of an IPv6 interface address.
732 * If necessary, a new entry is created and linked into address chains.
733 * This function is separated from in6_control().
734 * XXX: should this be performed under splnet()?
735 */
736int
737in6_update_ifa(ifp, ifra, ia)
738	struct ifnet *ifp;
739	struct in6_aliasreq *ifra;
740	struct in6_ifaddr *ia;
741{
742	int error = 0, hostIsNew = 0, plen = -1;
743	struct in6_ifaddr *oia;
744	struct sockaddr_in6 dst6;
745	struct in6_addrlifetime *lt;
746
747	/* Validate parameters */
748	if (ifp == NULL || ifra == NULL) /* this maybe redundant */
749		return (EINVAL);
750
751	/*
752	 * The destination address for a p2p link must have a family
753	 * of AF_UNSPEC or AF_INET6.
754	 */
755	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
756	    ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
757	    ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
758		return (EAFNOSUPPORT);
759	/*
760	 * validate ifra_prefixmask.  don't check sin6_family, netmask
761	 * does not carry fields other than sin6_len.
762	 */
763	if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
764		return (EINVAL);
765	/*
766	 * Because the IPv6 address architecture is classless, we require
767	 * users to specify a (non 0) prefix length (mask) for a new address.
768	 * We also require the prefix (when specified) mask is valid, and thus
769	 * reject a non-consecutive mask.
770	 */
771	if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
772		return (EINVAL);
773	if (ifra->ifra_prefixmask.sin6_len != 0) {
774		plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
775		    (u_char *)&ifra->ifra_prefixmask +
776		    ifra->ifra_prefixmask.sin6_len);
777		if (plen <= 0)
778			return (EINVAL);
779	} else {
780		/*
781		 * In this case, ia must not be NULL.  We just use its prefix
782		 * length.
783		 */
784		plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
785	}
786	/*
787	 * If the destination address on a p2p interface is specified,
788	 * and the address is a scoped one, validate/set the scope
789	 * zone identifier.
790	 */
791	dst6 = ifra->ifra_dstaddr;
792	if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
793	    (dst6.sin6_family == AF_INET6)) {
794		int scopeid;
795
796		if ((error = in6_recoverscope(&dst6,
797		    &ifra->ifra_dstaddr.sin6_addr, ifp)) != 0)
798			return (error);
799		if (in6_addr2zoneid(ifp, &dst6.sin6_addr, &scopeid))
800			return (EINVAL);
801		if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
802			dst6.sin6_scope_id = scopeid;
803		else if (dst6.sin6_scope_id != scopeid)
804			return (EINVAL); /* scope ID mismatch. */
805		if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
806		    != 0)
807			return (error);
808		dst6.sin6_scope_id = 0; /* XXX */
809	}
810	/*
811	 * The destination address can be specified only for a p2p or a
812	 * loopback interface.  If specified, the corresponding prefix length
813	 * must be 128.
814	 */
815	if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
816		if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
817			/* XXX: noisy message */
818			log(LOG_INFO, "in6_update_ifa: a destination can be "
819			    "specified for a p2p or a loopback IF only\n");
820			return (EINVAL);
821		}
822		if (plen != 128) {
823			/*
824			 * The following message seems noisy, but we dare to
825			 * add it for diagnosis.
826			 */
827			log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
828			    "when dstaddr is specified\n");
829			return (EINVAL);
830		}
831	}
832	/* lifetime consistency check */
833	lt = &ifra->ifra_lifetime;
834	if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
835	    && lt->ia6t_vltime + time_second < time_second) {
836		return EINVAL;
837	}
838	if (lt->ia6t_vltime == 0) {
839		/*
840		 * the following log might be noisy, but this is a typical
841		 * configuration mistake or a tool's bug.
842		 */
843		log(LOG_INFO,
844		    "in6_update_ifa: valid lifetime is 0 for %s\n",
845		    ip6_sprintf(&ifra->ifra_addr.sin6_addr));
846	}
847	if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
848	    && lt->ia6t_pltime + time_second < time_second) {
849		return EINVAL;
850	}
851
852	/*
853	 * If this is a new address, allocate a new ifaddr and link it
854	 * into chains.
855	 */
856	if (ia == NULL) {
857		hostIsNew = 1;
858		/*
859		 * When in6_update_ifa() is called in a process of a received
860		 * RA, it is called under an interrupt context.  So, we should
861		 * call malloc with M_NOWAIT.
862		 */
863		ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
864		    M_NOWAIT);
865		if (ia == NULL)
866			return (ENOBUFS);
867		bzero((caddr_t)ia, sizeof(*ia));
868		/* Initialize the address and masks */
869		IFA_LOCK_INIT(&ia->ia_ifa);
870		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
871		ia->ia_addr.sin6_family = AF_INET6;
872		ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
873		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
874			/*
875			 * XXX: some functions expect that ifa_dstaddr is not
876			 * NULL for p2p interfaces.
877			 */
878			ia->ia_ifa.ifa_dstaddr =
879			    (struct sockaddr *)&ia->ia_dstaddr;
880		} else {
881			ia->ia_ifa.ifa_dstaddr = NULL;
882		}
883		ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
884
885		ia->ia_ifp = ifp;
886		if ((oia = in6_ifaddr) != NULL) {
887			for ( ; oia->ia_next; oia = oia->ia_next)
888				continue;
889			oia->ia_next = ia;
890		} else
891			in6_ifaddr = ia;
892
893		ia->ia_ifa.ifa_refcnt = 1;
894		TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
895	}
896
897	/* set prefix mask */
898	if (ifra->ifra_prefixmask.sin6_len) {
899		/*
900		 * We prohibit changing the prefix length of an existing
901		 * address, because
902		 * + such an operation should be rare in IPv6, and
903		 * + the operation would confuse prefix management.
904		 */
905		if (ia->ia_prefixmask.sin6_len &&
906		    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
907			log(LOG_INFO, "in6_update_ifa: the prefix length of an"
908			    " existing (%s) address should not be changed\n",
909			    ip6_sprintf(&ia->ia_addr.sin6_addr));
910			error = EINVAL;
911			goto unlink;
912		}
913		ia->ia_prefixmask = ifra->ifra_prefixmask;
914	}
915
916	/*
917	 * If a new destination address is specified, scrub the old one and
918	 * install the new destination.  Note that the interface must be
919	 * p2p or loopback (see the check above.)
920	 */
921	if (dst6.sin6_family == AF_INET6 &&
922	    !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
923		int e;
924
925		if ((ia->ia_flags & IFA_ROUTE) != 0 &&
926		    (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
927			log(LOG_ERR, "in6_update_ifa: failed to remove "
928			    "a route to the old destination: %s\n",
929			    ip6_sprintf(&ia->ia_addr.sin6_addr));
930			/* proceed anyway... */
931		} else
932			ia->ia_flags &= ~IFA_ROUTE;
933		ia->ia_dstaddr = dst6;
934	}
935
936	/* reset the interface and routing table appropriately. */
937	if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
938		goto unlink;
939
940	/*
941	 * Beyond this point, we should call in6_purgeaddr upon an error,
942	 * not just go to unlink.
943	 */
944
945	if ((ifp->if_flags & IFF_MULTICAST) != 0) {
946		struct sockaddr_in6 mltaddr, mltmask;
947		struct in6_multi *in6m;
948
949		if (hostIsNew) {
950			/* join solicited multicast addr for new host id */
951			struct in6_addr llsol;
952
953			bzero(&llsol, sizeof(struct in6_addr));
954			llsol.s6_addr16[0] = htons(0xff02);
955			llsol.s6_addr16[1] = htons(ifp->if_index);
956			llsol.s6_addr32[1] = 0;
957			llsol.s6_addr32[2] = htonl(1);
958			llsol.s6_addr32[3] =
959				ifra->ifra_addr.sin6_addr.s6_addr32[3];
960			llsol.s6_addr8[12] = 0xff;
961			(void)in6_addmulti(&llsol, ifp, &error);
962			if (error != 0) {
963				log(LOG_WARNING,
964				    "in6_update_ifa: addmulti failed for "
965				    "%s on %s (errno=%d)\n",
966				    ip6_sprintf(&llsol), if_name(ifp),
967				    error);
968				in6_purgeaddr((struct ifaddr *)ia);
969				return (error);
970			}
971		}
972
973		bzero(&mltmask, sizeof(mltmask));
974		mltmask.sin6_len = sizeof(struct sockaddr_in6);
975		mltmask.sin6_family = AF_INET6;
976		mltmask.sin6_addr = in6mask32;
977
978		/*
979		 * join link-local all-nodes address
980		 */
981		bzero(&mltaddr, sizeof(mltaddr));
982		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
983		mltaddr.sin6_family = AF_INET6;
984		mltaddr.sin6_addr = in6addr_linklocal_allnodes;
985		mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
986
987		IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
988		if (in6m == NULL) {
989			rtrequest(RTM_ADD,
990				  (struct sockaddr *)&mltaddr,
991				  (struct sockaddr *)&ia->ia_addr,
992				  (struct sockaddr *)&mltmask,
993				  RTF_UP|RTF_CLONING,  /* xxx */
994				  (struct rtentry **)0);
995			(void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
996			if (error != 0) {
997				log(LOG_WARNING,
998				    "in6_update_ifa: addmulti failed for "
999				    "%s on %s (errno=%d)\n",
1000				    ip6_sprintf(&mltaddr.sin6_addr),
1001				    if_name(ifp), error);
1002			}
1003		}
1004
1005		/*
1006		 * join node information group address
1007		 */
1008#define hostnamelen	strlen(hostname)
1009		if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1010		    == 0) {
1011			IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1012			if (in6m == NULL && ia != NULL) {
1013				(void)in6_addmulti(&mltaddr.sin6_addr,
1014				    ifp, &error);
1015				if (error != 0) {
1016					log(LOG_WARNING, "in6_update_ifa: "
1017					    "addmulti failed for "
1018					    "%s on %s (errno=%d)\n",
1019					    ip6_sprintf(&mltaddr.sin6_addr),
1020					    if_name(ifp), error);
1021				}
1022			}
1023		}
1024#undef hostnamelen
1025
1026		/*
1027		 * join node-local all-nodes address, on loopback.
1028		 * XXX: since "node-local" is obsoleted by interface-local,
1029		 *      we have to join the group on every interface with
1030		 *      some interface-boundary restriction.
1031		 */
1032		if (ifp->if_flags & IFF_LOOPBACK) {
1033			struct in6_ifaddr *ia_loop;
1034
1035			struct in6_addr loop6 = in6addr_loopback;
1036			ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1037
1038			mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1039
1040			IN6_LOOKUP_MULTI(mltaddr.sin6_addr, ifp, in6m);
1041			if (in6m == NULL && ia_loop != NULL) {
1042				rtrequest(RTM_ADD,
1043					  (struct sockaddr *)&mltaddr,
1044					  (struct sockaddr *)&ia_loop->ia_addr,
1045					  (struct sockaddr *)&mltmask,
1046					  RTF_UP,
1047					  (struct rtentry **)0);
1048				(void)in6_addmulti(&mltaddr.sin6_addr, ifp,
1049						   &error);
1050				if (error != 0) {
1051					log(LOG_WARNING, "in6_update_ifa: "
1052					    "addmulti failed for %s on %s "
1053					    "(errno=%d)\n",
1054					    ip6_sprintf(&mltaddr.sin6_addr),
1055					    if_name(ifp), error);
1056				}
1057			}
1058		}
1059	}
1060
1061	ia->ia6_flags = ifra->ifra_flags;
1062	ia->ia6_flags &= ~IN6_IFF_DUPLICATED;	/*safety*/
1063	ia->ia6_flags &= ~IN6_IFF_NODAD;	/* Mobile IPv6 */
1064
1065	ia->ia6_lifetime = ifra->ifra_lifetime;
1066	/* for sanity */
1067	if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1068		ia->ia6_lifetime.ia6t_expire =
1069			time_second + ia->ia6_lifetime.ia6t_vltime;
1070	} else
1071		ia->ia6_lifetime.ia6t_expire = 0;
1072	if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1073		ia->ia6_lifetime.ia6t_preferred =
1074			time_second + ia->ia6_lifetime.ia6t_pltime;
1075	} else
1076		ia->ia6_lifetime.ia6t_preferred = 0;
1077
1078	/*
1079	 * Perform DAD, if needed.
1080	 * XXX It may be of use, if we can administratively
1081	 * disable DAD.
1082	 */
1083	if (in6if_do_dad(ifp) && (ifra->ifra_flags & IN6_IFF_NODAD) == 0) {
1084		ia->ia6_flags |= IN6_IFF_TENTATIVE;
1085		nd6_dad_start((struct ifaddr *)ia, NULL);
1086	}
1087
1088	return (error);
1089
1090  unlink:
1091	/*
1092	 * XXX: if a change of an existing address failed, keep the entry
1093	 * anyway.
1094	 */
1095	if (hostIsNew)
1096		in6_unlink_ifa(ia, ifp);
1097	return (error);
1098}
1099
1100void
1101in6_purgeaddr(ifa)
1102	struct ifaddr *ifa;
1103{
1104	struct ifnet *ifp = ifa->ifa_ifp;
1105	struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1106
1107	/* stop DAD processing */
1108	nd6_dad_stop(ifa);
1109
1110	/*
1111	 * delete route to the destination of the address being purged.
1112	 * The interface must be p2p or loopback in this case.
1113	 */
1114	if ((ia->ia_flags & IFA_ROUTE) != 0 && ia->ia_dstaddr.sin6_len != 0) {
1115		int e;
1116
1117		if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1118		    != 0) {
1119			log(LOG_ERR, "in6_purgeaddr: failed to remove "
1120			    "a route to the p2p destination: %s on %s, "
1121			    "errno=%d\n",
1122			    ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1123			    e);
1124			/* proceed anyway... */
1125		} else
1126			ia->ia_flags &= ~IFA_ROUTE;
1127	}
1128
1129	/* Remove ownaddr's loopback rtentry, if it exists. */
1130	in6_ifremloop(&(ia->ia_ifa));
1131
1132	if (ifp->if_flags & IFF_MULTICAST) {
1133		/*
1134		 * delete solicited multicast addr for deleting host id
1135		 */
1136		struct in6_multi *in6m;
1137		struct in6_addr llsol;
1138		bzero(&llsol, sizeof(struct in6_addr));
1139		llsol.s6_addr16[0] = htons(0xff02);
1140		llsol.s6_addr16[1] = htons(ifp->if_index);
1141		llsol.s6_addr32[1] = 0;
1142		llsol.s6_addr32[2] = htonl(1);
1143		llsol.s6_addr32[3] =
1144			ia->ia_addr.sin6_addr.s6_addr32[3];
1145		llsol.s6_addr8[12] = 0xff;
1146
1147		IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1148		if (in6m)
1149			in6_delmulti(in6m);
1150	}
1151
1152	in6_unlink_ifa(ia, ifp);
1153}
1154
1155static void
1156in6_unlink_ifa(ia, ifp)
1157	struct in6_ifaddr *ia;
1158	struct ifnet *ifp;
1159{
1160	int plen, iilen;
1161	struct in6_ifaddr *oia;
1162	int	s = splnet();
1163
1164	TAILQ_REMOVE(&ifp->if_addrlist, &ia->ia_ifa, ifa_list);
1165
1166	oia = ia;
1167	if (oia == (ia = in6_ifaddr))
1168		in6_ifaddr = ia->ia_next;
1169	else {
1170		while (ia->ia_next && (ia->ia_next != oia))
1171			ia = ia->ia_next;
1172		if (ia->ia_next)
1173			ia->ia_next = oia->ia_next;
1174		else {
1175			/* search failed */
1176			printf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
1177		}
1178	}
1179
1180	if (oia->ia6_ifpr) {	/* check for safety */
1181		plen = in6_mask2len(&oia->ia_prefixmask.sin6_addr, NULL);
1182		iilen = (sizeof(oia->ia_prefixmask.sin6_addr) << 3) - plen;
1183		in6_prefix_remove_ifid(iilen, oia);
1184	}
1185
1186	/*
1187	 * When an autoconfigured address is being removed, release the
1188	 * reference to the base prefix.  Also, since the release might
1189	 * affect the status of other (detached) addresses, call
1190	 * pfxlist_onlink_check().
1191	 */
1192	if ((oia->ia6_flags & IN6_IFF_AUTOCONF) != 0) {
1193		if (oia->ia6_ndpr == NULL) {
1194			log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1195			    "%p has no prefix\n", oia);
1196		} else {
1197			oia->ia6_ndpr->ndpr_refcnt--;
1198			oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1199			oia->ia6_ndpr = NULL;
1200		}
1201
1202		pfxlist_onlink_check();
1203	}
1204
1205	/*
1206	 * release another refcnt for the link from in6_ifaddr.
1207	 * Note that we should decrement the refcnt at least once for all *BSD.
1208	 */
1209	IFAFREE(&oia->ia_ifa);
1210
1211	splx(s);
1212}
1213
1214void
1215in6_purgeif(ifp)
1216	struct ifnet *ifp;
1217{
1218	struct ifaddr *ifa, *nifa;
1219
1220	for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; ifa = nifa) {
1221		nifa = TAILQ_NEXT(ifa, ifa_list);
1222		if (ifa->ifa_addr->sa_family != AF_INET6)
1223			continue;
1224		in6_purgeaddr(ifa);
1225	}
1226
1227	in6_ifdetach(ifp);
1228}
1229
1230/*
1231 * SIOC[GAD]LIFADDR.
1232 *	SIOCGLIFADDR: get first address. (?)
1233 *	SIOCGLIFADDR with IFLR_PREFIX:
1234 *		get first address that matches the specified prefix.
1235 *	SIOCALIFADDR: add the specified address.
1236 *	SIOCALIFADDR with IFLR_PREFIX:
1237 *		add the specified prefix, filling hostid part from
1238 *		the first link-local address.  prefixlen must be <= 64.
1239 *	SIOCDLIFADDR: delete the specified address.
1240 *	SIOCDLIFADDR with IFLR_PREFIX:
1241 *		delete the first address that matches the specified prefix.
1242 * return values:
1243 *	EINVAL on invalid parameters
1244 *	EADDRNOTAVAIL on prefix match failed/specified address not found
1245 *	other values may be returned from in6_ioctl()
1246 *
1247 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1248 * this is to accomodate address naming scheme other than RFC2374,
1249 * in the future.
1250 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1251 * address encoding scheme. (see figure on page 8)
1252 */
1253static int
1254in6_lifaddr_ioctl(so, cmd, data, ifp, td)
1255	struct socket *so;
1256	u_long cmd;
1257	caddr_t	data;
1258	struct ifnet *ifp;
1259	struct thread *td;
1260{
1261	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
1262	struct ifaddr *ifa;
1263	struct sockaddr *sa;
1264
1265	/* sanity checks */
1266	if (!data || !ifp) {
1267		panic("invalid argument to in6_lifaddr_ioctl");
1268		/* NOTREACHED */
1269	}
1270
1271	switch (cmd) {
1272	case SIOCGLIFADDR:
1273		/* address must be specified on GET with IFLR_PREFIX */
1274		if ((iflr->flags & IFLR_PREFIX) == 0)
1275			break;
1276		/* FALLTHROUGH */
1277	case SIOCALIFADDR:
1278	case SIOCDLIFADDR:
1279		/* address must be specified on ADD and DELETE */
1280		sa = (struct sockaddr *)&iflr->addr;
1281		if (sa->sa_family != AF_INET6)
1282			return EINVAL;
1283		if (sa->sa_len != sizeof(struct sockaddr_in6))
1284			return EINVAL;
1285		/* XXX need improvement */
1286		sa = (struct sockaddr *)&iflr->dstaddr;
1287		if (sa->sa_family && sa->sa_family != AF_INET6)
1288			return EINVAL;
1289		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1290			return EINVAL;
1291		break;
1292	default: /* shouldn't happen */
1293#if 0
1294		panic("invalid cmd to in6_lifaddr_ioctl");
1295		/* NOTREACHED */
1296#else
1297		return EOPNOTSUPP;
1298#endif
1299	}
1300	if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1301		return EINVAL;
1302
1303	switch (cmd) {
1304	case SIOCALIFADDR:
1305	    {
1306		struct in6_aliasreq ifra;
1307		struct in6_addr *hostid = NULL;
1308		int prefixlen;
1309
1310		if ((iflr->flags & IFLR_PREFIX) != 0) {
1311			struct sockaddr_in6 *sin6;
1312
1313			/*
1314			 * hostid is to fill in the hostid part of the
1315			 * address.  hostid points to the first link-local
1316			 * address attached to the interface.
1317			 */
1318			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1319			if (!ifa)
1320				return EADDRNOTAVAIL;
1321			hostid = IFA_IN6(ifa);
1322
1323		 	/* prefixlen must be <= 64. */
1324			if (64 < iflr->prefixlen)
1325				return EINVAL;
1326			prefixlen = iflr->prefixlen;
1327
1328			/* hostid part must be zero. */
1329			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1330			if (sin6->sin6_addr.s6_addr32[2] != 0
1331			 || sin6->sin6_addr.s6_addr32[3] != 0) {
1332				return EINVAL;
1333			}
1334		} else
1335			prefixlen = iflr->prefixlen;
1336
1337		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1338		bzero(&ifra, sizeof(ifra));
1339		bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
1340
1341		bcopy(&iflr->addr, &ifra.ifra_addr,
1342		    ((struct sockaddr *)&iflr->addr)->sa_len);
1343		if (hostid) {
1344			/* fill in hostid part */
1345			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1346			    hostid->s6_addr32[2];
1347			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1348			    hostid->s6_addr32[3];
1349		}
1350
1351		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
1352			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1353			    ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1354			if (hostid) {
1355				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1356				    hostid->s6_addr32[2];
1357				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1358				    hostid->s6_addr32[3];
1359			}
1360		}
1361
1362		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1363		in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
1364
1365		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
1366		return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
1367	    }
1368	case SIOCGLIFADDR:
1369	case SIOCDLIFADDR:
1370	    {
1371		struct in6_ifaddr *ia;
1372		struct in6_addr mask, candidate, match;
1373		struct sockaddr_in6 *sin6;
1374		int cmp;
1375
1376		bzero(&mask, sizeof(mask));
1377		if (iflr->flags & IFLR_PREFIX) {
1378			/* lookup a prefix rather than address. */
1379			in6_prefixlen2mask(&mask, iflr->prefixlen);
1380
1381			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1382			bcopy(&sin6->sin6_addr, &match, sizeof(match));
1383			match.s6_addr32[0] &= mask.s6_addr32[0];
1384			match.s6_addr32[1] &= mask.s6_addr32[1];
1385			match.s6_addr32[2] &= mask.s6_addr32[2];
1386			match.s6_addr32[3] &= mask.s6_addr32[3];
1387
1388			/* if you set extra bits, that's wrong */
1389			if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1390				return EINVAL;
1391
1392			cmp = 1;
1393		} else {
1394			if (cmd == SIOCGLIFADDR) {
1395				/* on getting an address, take the 1st match */
1396				cmp = 0;	/* XXX */
1397			} else {
1398				/* on deleting an address, do exact match */
1399				in6_prefixlen2mask(&mask, 128);
1400				sin6 = (struct sockaddr_in6 *)&iflr->addr;
1401				bcopy(&sin6->sin6_addr, &match, sizeof(match));
1402
1403				cmp = 1;
1404			}
1405		}
1406
1407		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1408			if (ifa->ifa_addr->sa_family != AF_INET6)
1409				continue;
1410			if (!cmp)
1411				break;
1412
1413			bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1414			/*
1415			 * XXX: this is adhoc, but is necessary to allow
1416			 * a user to specify fe80::/64 (not /10) for a
1417			 * link-local address.
1418			 */
1419			if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1420				candidate.s6_addr16[1] = 0;
1421			candidate.s6_addr32[0] &= mask.s6_addr32[0];
1422			candidate.s6_addr32[1] &= mask.s6_addr32[1];
1423			candidate.s6_addr32[2] &= mask.s6_addr32[2];
1424			candidate.s6_addr32[3] &= mask.s6_addr32[3];
1425			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1426				break;
1427		}
1428		if (!ifa)
1429			return EADDRNOTAVAIL;
1430		ia = ifa2ia6(ifa);
1431
1432		if (cmd == SIOCGLIFADDR) {
1433			struct sockaddr_in6 *s6;
1434
1435			/* fill in the if_laddrreq structure */
1436			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1437			s6 = (struct sockaddr_in6 *)&iflr->addr;
1438			if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1439				s6->sin6_addr.s6_addr16[1] = 0;
1440				if (in6_addr2zoneid(ifp, &s6->sin6_addr,
1441				    &s6->sin6_scope_id))
1442					return (EINVAL);/* XXX */
1443			}
1444			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1445				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1446				    ia->ia_dstaddr.sin6_len);
1447				s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1448				if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1449					s6->sin6_addr.s6_addr16[1] = 0;
1450					if (in6_addr2zoneid(ifp,
1451					    &s6->sin6_addr, &s6->sin6_scope_id))
1452						return (EINVAL); /* EINVAL */
1453				}
1454			} else
1455				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1456
1457			iflr->prefixlen =
1458			    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
1459
1460			iflr->flags = ia->ia6_flags;	/* XXX */
1461
1462			return 0;
1463		} else {
1464			struct in6_aliasreq ifra;
1465
1466			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1467			bzero(&ifra, sizeof(ifra));
1468			bcopy(iflr->iflr_name, ifra.ifra_name,
1469			    sizeof(ifra.ifra_name));
1470
1471			bcopy(&ia->ia_addr, &ifra.ifra_addr,
1472			    ia->ia_addr.sin6_len);
1473			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
1474				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1475				    ia->ia_dstaddr.sin6_len);
1476			} else {
1477				bzero(&ifra.ifra_dstaddr,
1478				    sizeof(ifra.ifra_dstaddr));
1479			}
1480			bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1481			    ia->ia_prefixmask.sin6_len);
1482
1483			ifra.ifra_flags = ia->ia6_flags;
1484			return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1485			    ifp, td);
1486		}
1487	    }
1488	}
1489
1490	return EOPNOTSUPP;	/* just for safety */
1491}
1492
1493/*
1494 * Initialize an interface's intetnet6 address
1495 * and routing table entry.
1496 */
1497static int
1498in6_ifinit(ifp, ia, sin6, newhost)
1499	struct ifnet *ifp;
1500	struct in6_ifaddr *ia;
1501	struct sockaddr_in6 *sin6;
1502	int newhost;
1503{
1504	int	error = 0, plen, ifacount = 0;
1505	int	s = splimp();
1506	struct ifaddr *ifa;
1507
1508	/*
1509	 * Give the interface a chance to initialize
1510	 * if this is its first address,
1511	 * and to validate the address if necessary.
1512	 */
1513	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1514		if (ifa->ifa_addr == NULL)
1515			continue;	/* just for safety */
1516		if (ifa->ifa_addr->sa_family != AF_INET6)
1517			continue;
1518		ifacount++;
1519	}
1520
1521	ia->ia_addr = *sin6;
1522
1523	if (ifacount <= 1 && ifp->if_ioctl &&
1524	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
1525		splx(s);
1526		return (error);
1527	}
1528	splx(s);
1529
1530	ia->ia_ifa.ifa_metric = ifp->if_metric;
1531
1532	/* we could do in(6)_socktrim here, but just omit it at this moment. */
1533
1534	/*
1535	 * Special case:
1536	 * If the destination address is specified for a point-to-point
1537	 * interface, install a route to the destination as an interface
1538	 * direct route.
1539	 */
1540	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1541	if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1542		if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1543				    RTF_UP | RTF_HOST)) != 0)
1544			return (error);
1545		ia->ia_flags |= IFA_ROUTE;
1546	}
1547	if (plen < 128) {
1548		/*
1549		 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1550		 */
1551		ia->ia_ifa.ifa_flags |= RTF_CLONING;
1552	}
1553
1554	/* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1555	if (newhost) {
1556		/* set the rtrequest function to create llinfo */
1557		ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1558		in6_ifaddloop(&(ia->ia_ifa));
1559	}
1560
1561	return (error);
1562}
1563
1564/*
1565 * Add an address to the list of IP6 multicast addresses for a
1566 * given interface.
1567 */
1568struct	in6_multi *
1569in6_addmulti(maddr6, ifp, errorp)
1570	struct in6_addr *maddr6;
1571	struct ifnet *ifp;
1572	int *errorp;
1573{
1574	struct	in6_multi *in6m;
1575	struct sockaddr_in6 sin6;
1576	struct ifmultiaddr *ifma;
1577	int	s = splnet();
1578
1579	*errorp = 0;
1580
1581	/*
1582	 * Call generic routine to add membership or increment
1583	 * refcount.  It wants addresses in the form of a sockaddr,
1584	 * so we build one here (being careful to zero the unused bytes).
1585	 */
1586	bzero(&sin6, sizeof sin6);
1587	sin6.sin6_family = AF_INET6;
1588	sin6.sin6_len = sizeof sin6;
1589	sin6.sin6_addr = *maddr6;
1590	*errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1591	if (*errorp) {
1592		splx(s);
1593		return 0;
1594	}
1595
1596	/*
1597	 * If ifma->ifma_protospec is null, then if_addmulti() created
1598	 * a new record.  Otherwise, we are done.
1599	 */
1600	if (ifma->ifma_protospec != 0)
1601		return ifma->ifma_protospec;
1602
1603	/* XXX - if_addmulti uses M_WAITOK.  Can this really be called
1604	   at interrupt time?  If so, need to fix if_addmulti. XXX */
1605	in6m = (struct in6_multi *)malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
1606	if (in6m == NULL) {
1607		splx(s);
1608		return (NULL);
1609	}
1610
1611	bzero(in6m, sizeof *in6m);
1612	in6m->in6m_addr = *maddr6;
1613	in6m->in6m_ifp = ifp;
1614	in6m->in6m_ifma = ifma;
1615	ifma->ifma_protospec = in6m;
1616	LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1617
1618	/*
1619	 * Let MLD6 know that we have joined a new IPv6 multicast
1620	 * group.
1621	 */
1622	mld6_start_listening(in6m);
1623	splx(s);
1624	return (in6m);
1625}
1626
1627/*
1628 * Delete a multicast address record.
1629 */
1630void
1631in6_delmulti(in6m)
1632	struct in6_multi *in6m;
1633{
1634	struct ifmultiaddr *ifma = in6m->in6m_ifma;
1635	int	s = splnet();
1636
1637	if (ifma->ifma_refcount == 1) {
1638		/*
1639		 * No remaining claims to this record; let MLD6 know
1640		 * that we are leaving the multicast group.
1641		 */
1642		mld6_stop_listening(in6m);
1643		ifma->ifma_protospec = 0;
1644		LIST_REMOVE(in6m, in6m_entry);
1645		free(in6m, M_IPMADDR);
1646	}
1647	/* XXX - should be separate API for when we have an ifma? */
1648	if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1649	splx(s);
1650}
1651
1652/*
1653 * Find an IPv6 interface link-local address specific to an interface.
1654 */
1655struct in6_ifaddr *
1656in6ifa_ifpforlinklocal(ifp, ignoreflags)
1657	struct ifnet *ifp;
1658	int ignoreflags;
1659{
1660	struct ifaddr *ifa;
1661
1662	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1663		if (ifa->ifa_addr == NULL)
1664			continue;	/* just for safety */
1665		if (ifa->ifa_addr->sa_family != AF_INET6)
1666			continue;
1667		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1668			if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1669			     ignoreflags) != 0)
1670				continue;
1671			break;
1672		}
1673	}
1674
1675	return ((struct in6_ifaddr *)ifa);
1676}
1677
1678
1679/*
1680 * find the internet address corresponding to a given interface and address.
1681 */
1682struct in6_ifaddr *
1683in6ifa_ifpwithaddr(ifp, addr)
1684	struct ifnet *ifp;
1685	struct in6_addr *addr;
1686{
1687	struct ifaddr *ifa;
1688
1689	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1690		if (ifa->ifa_addr == NULL)
1691			continue;	/* just for safety */
1692		if (ifa->ifa_addr->sa_family != AF_INET6)
1693			continue;
1694		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
1695			break;
1696	}
1697
1698	return ((struct in6_ifaddr *)ifa);
1699}
1700
1701/*
1702 * Convert IP6 address to printable (loggable) representation.
1703 */
1704static char digits[] = "0123456789abcdef";
1705static int ip6round = 0;
1706char *
1707ip6_sprintf(addr)
1708	const struct in6_addr *addr;
1709{
1710	static char ip6buf[8][48];
1711	int i;
1712	char *cp;
1713	const u_short *a = (const u_short *)addr;
1714	const u_char *d;
1715	int dcolon = 0;
1716
1717	ip6round = (ip6round + 1) & 7;
1718	cp = ip6buf[ip6round];
1719
1720	for (i = 0; i < 8; i++) {
1721		if (dcolon == 1) {
1722			if (*a == 0) {
1723				if (i == 7)
1724					*cp++ = ':';
1725				a++;
1726				continue;
1727			} else
1728				dcolon = 2;
1729		}
1730		if (*a == 0) {
1731			if (dcolon == 0 && *(a + 1) == 0) {
1732				if (i == 0)
1733					*cp++ = ':';
1734				*cp++ = ':';
1735				dcolon = 1;
1736			} else {
1737				*cp++ = '0';
1738				*cp++ = ':';
1739			}
1740			a++;
1741			continue;
1742		}
1743		d = (const u_char *)a;
1744		*cp++ = digits[*d >> 4];
1745		*cp++ = digits[*d++ & 0xf];
1746		*cp++ = digits[*d >> 4];
1747		*cp++ = digits[*d & 0xf];
1748		*cp++ = ':';
1749		a++;
1750	}
1751	*--cp = 0;
1752	return (ip6buf[ip6round]);
1753}
1754
1755int
1756in6_localaddr(in6)
1757	struct in6_addr *in6;
1758{
1759	struct in6_ifaddr *ia;
1760
1761	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1762		return 1;
1763
1764	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1765		if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1766		    &ia->ia_prefixmask.sin6_addr)) {
1767			return 1;
1768		}
1769	}
1770
1771	return (0);
1772}
1773
1774int
1775in6_is_addr_deprecated(sa6)
1776	struct sockaddr_in6 *sa6;
1777{
1778	struct in6_ifaddr *ia;
1779
1780	for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1781		if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1782				       &sa6->sin6_addr) &&
1783		    (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0)
1784			return (1); /* true */
1785
1786		/* XXX: do we still have to go thru the rest of the list? */
1787	}
1788
1789	return (0);		/* false */
1790}
1791
1792/*
1793 * return length of part which dst and src are equal
1794 * hard coding...
1795 */
1796int
1797in6_matchlen(src, dst)
1798struct in6_addr *src, *dst;
1799{
1800	int match = 0;
1801	u_char *s = (u_char *)src, *d = (u_char *)dst;
1802	u_char *lim = s + 16, r;
1803
1804	while (s < lim)
1805		if ((r = (*d++ ^ *s++)) != 0) {
1806			while (r < 128) {
1807				match++;
1808				r <<= 1;
1809			}
1810			break;
1811		} else
1812			match += 8;
1813	return match;
1814}
1815
1816/* XXX: to be scope conscious */
1817int
1818in6_are_prefix_equal(p1, p2, len)
1819	struct in6_addr *p1, *p2;
1820	int len;
1821{
1822	int bytelen, bitlen;
1823
1824	/* sanity check */
1825	if (0 > len || len > 128) {
1826		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
1827		    len);
1828		return (0);
1829	}
1830
1831	bytelen = len / 8;
1832	bitlen = len % 8;
1833
1834	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
1835		return (0);
1836	if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
1837	    p2->s6_addr[bytelen] >> (8 - bitlen))
1838		return (0);
1839
1840	return (1);
1841}
1842
1843void
1844in6_prefixlen2mask(maskp, len)
1845	struct in6_addr *maskp;
1846	int len;
1847{
1848	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
1849	int bytelen, bitlen, i;
1850
1851	/* sanity check */
1852	if (0 > len || len > 128) {
1853		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
1854		    len);
1855		return;
1856	}
1857
1858	bzero(maskp, sizeof(*maskp));
1859	bytelen = len / 8;
1860	bitlen = len % 8;
1861	for (i = 0; i < bytelen; i++)
1862		maskp->s6_addr[i] = 0xff;
1863	if (bitlen)
1864		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
1865}
1866
1867/*
1868 * return the best address out of the same scope
1869 */
1870struct in6_ifaddr *
1871in6_ifawithscope(oifp, dst)
1872	struct ifnet *oifp;
1873	struct in6_addr *dst;
1874{
1875	int dst_scope =	in6_addrscope(dst), src_scope, best_scope = 0;
1876	int blen = -1;
1877	struct ifaddr *ifa;
1878	struct ifnet *ifp;
1879	struct in6_ifaddr *ifa_best = NULL;
1880	u_int32_t dstzone, odstzone;
1881
1882	if (oifp == NULL) {
1883		return (NULL);
1884	}
1885
1886	if (in6_addr2zoneid(oifp, dst, &odstzone))
1887		return (NULL);
1888
1889	/*
1890	 * We search for all addresses on all interfaces from the beginning.
1891	 * Comparing an interface with the outgoing interface will be done
1892	 * only at the final stage of tiebreaking.
1893	 */
1894	IFNET_RLOCK();
1895	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1896	{
1897		/*
1898		 * We can never take an address that breaks the scope zone
1899		 * of the destination.
1900		 */
1901		if (in6_addr2zoneid(ifp, dst, &dstzone) || dstzone != odstzone)
1902			continue;
1903
1904		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1905		{
1906			int tlen = -1, dscopecmp, bscopecmp, matchcmp;
1907
1908			if (ifa->ifa_addr->sa_family != AF_INET6)
1909				continue;
1910
1911			src_scope = in6_addrscope(IFA_IN6(ifa));
1912
1913			/*
1914			 * Don't use an address before completing DAD
1915			 * nor a duplicated address.
1916			 */
1917			if (((struct in6_ifaddr *)ifa)->ia6_flags &
1918			    IN6_IFF_NOTREADY)
1919				continue;
1920
1921			/* XXX: is there any case to allow anycasts? */
1922			if (((struct in6_ifaddr *)ifa)->ia6_flags &
1923			    IN6_IFF_ANYCAST)
1924				continue;
1925
1926			if (((struct in6_ifaddr *)ifa)->ia6_flags &
1927			    IN6_IFF_DETACHED)
1928				continue;
1929
1930			/*
1931			 * If this is the first address we find,
1932			 * keep it anyway.
1933			 */
1934			if (ifa_best == NULL)
1935				goto replace;
1936
1937			/*
1938			 * ifa_best is never NULL beyond this line except
1939			 * within the block labeled "replace".
1940			 */
1941
1942			/*
1943			 * If ifa_best has a smaller scope than dst and
1944			 * the current address has a larger one than
1945			 * (or equal to) dst, always replace ifa_best.
1946			 * Also, if the current address has a smaller scope
1947			 * than dst, ignore it unless ifa_best also has a
1948			 * smaller scope.
1949			 * Consequently, after the two if-clause below,
1950			 * the followings must be satisfied:
1951			 * (scope(src) < scope(dst) &&
1952			 *  scope(best) < scope(dst))
1953			 *  OR
1954			 * (scope(best) >= scope(dst) &&
1955			 *  scope(src) >= scope(dst))
1956			 */
1957			if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
1958			    IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
1959				goto replace; /* (A) */
1960			if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
1961			    IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
1962				continue; /* (B) */
1963
1964			/*
1965			 * A deprecated address SHOULD NOT be used in new
1966			 * communications if an alternate (non-deprecated)
1967			 * address is available and has sufficient scope.
1968			 * RFC 2462, Section 5.5.4.
1969			 */
1970			if (((struct in6_ifaddr *)ifa)->ia6_flags &
1971			    IN6_IFF_DEPRECATED) {
1972				/*
1973				 * Ignore any deprecated addresses if
1974				 * specified by configuration.
1975				 */
1976				if (!ip6_use_deprecated)
1977					continue;
1978
1979				/*
1980				 * If we have already found a non-deprecated
1981				 * candidate, just ignore deprecated addresses.
1982				 */
1983				if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
1984				    == 0)
1985					continue;
1986			}
1987
1988			/*
1989			 * A non-deprecated address is always preferred
1990			 * to a deprecated one regardless of scopes and
1991			 * address matching (Note invariants ensured by the
1992			 * conditions (A) and (B) above.)
1993			 */
1994			if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
1995			    (((struct in6_ifaddr *)ifa)->ia6_flags &
1996			     IN6_IFF_DEPRECATED) == 0)
1997				goto replace;
1998
1999			/*
2000			 * When we use temporary addresses described in
2001			 * RFC 3041, we prefer temporary addresses to
2002			 * public autoconf addresses.  Again, note the
2003			 * invariants from (A) and (B).  Also note that we
2004			 * don't have any preference between static addresses
2005			 * and autoconf addresses (despite of whether or not
2006			 * the latter is temporary or public.)
2007			 */
2008			if (ip6_use_tempaddr) {
2009				struct in6_ifaddr *ifat;
2010
2011				ifat = (struct in6_ifaddr *)ifa;
2012				if ((ifa_best->ia6_flags &
2013				     (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2014				     == IN6_IFF_AUTOCONF &&
2015				    (ifat->ia6_flags &
2016				     (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2017				     == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2018					goto replace;
2019				}
2020				if ((ifa_best->ia6_flags &
2021				     (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2022				    == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2023				    (ifat->ia6_flags &
2024				     (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2025				     == IN6_IFF_AUTOCONF) {
2026					continue;
2027				}
2028			}
2029
2030			/*
2031			 * At this point, we have two cases:
2032			 * 1. we are looking at a non-deprecated address,
2033			 *    and ifa_best is also non-deprecated.
2034			 * 2. we are looking at a deprecated address,
2035			 *    and ifa_best is also deprecated.
2036			 * Also, we do not have to consider a case where
2037			 * the scope of if_best is larger(smaller) than dst and
2038			 * the scope of the current address is smaller(larger)
2039			 * than dst. Such a case has already been covered.
2040			 * Tiebreaking is done according to the following
2041			 * items:
2042			 * - the scope comparison between the address and
2043			 *   dst (dscopecmp)
2044			 * - the scope comparison between the address and
2045			 *   ifa_best (bscopecmp)
2046			 * - if the address match dst longer than ifa_best
2047			 *   (matchcmp)
2048			 * - if the address is on the outgoing I/F (outI/F)
2049			 *
2050			 * Roughly speaking, the selection policy is
2051			 * - the most important item is scope. The same scope
2052			 *   is best. Then search for a larger scope.
2053			 *   Smaller scopes are the last resort.
2054			 * - A deprecated address is chosen only when we have
2055			 *   no address that has an enough scope, but is
2056			 *   prefered to any addresses of smaller scopes
2057			 *   (this must be already done above.)
2058			 * - addresses on the outgoing I/F are preferred to
2059			 *   ones on other interfaces if none of above
2060			 *   tiebreaks.  In the table below, the column "bI"
2061			 *   means if the best_ifa is on the outgoing
2062			 *   interface, and the column "sI" means if the ifa
2063			 *   is on the outgoing interface.
2064			 * - If there is no other reasons to choose one,
2065			 *   longest address match against dst is considered.
2066			 *
2067			 * The precise decision table is as follows:
2068			 * dscopecmp bscopecmp    match  bI oI | replace?
2069			 *       N/A     equal      N/A   Y  N |   No (1)
2070			 *       N/A     equal      N/A   N  Y |  Yes (2)
2071			 *       N/A     equal   larger    N/A |  Yes (3)
2072			 *       N/A     equal  !larger    N/A |   No (4)
2073			 *    larger    larger      N/A    N/A |   No (5)
2074			 *    larger   smaller      N/A    N/A |  Yes (6)
2075			 *   smaller    larger      N/A    N/A |  Yes (7)
2076			 *   smaller   smaller      N/A    N/A |   No (8)
2077			 *     equal   smaller      N/A    N/A |  Yes (9)
2078			 *     equal    larger       (already done at A above)
2079			 */
2080			dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2081			bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2082
2083			if (bscopecmp == 0) {
2084				struct ifnet *bifp = ifa_best->ia_ifp;
2085
2086				if (bifp == oifp && ifp != oifp) /* (1) */
2087					continue;
2088				if (bifp != oifp && ifp == oifp) /* (2) */
2089					goto replace;
2090
2091				/*
2092				 * Both bifp and ifp are on the outgoing
2093				 * interface, or both two are on a different
2094				 * interface from the outgoing I/F.
2095				 * now we need address matching against dst
2096				 * for tiebreaking.
2097				 */
2098				tlen = in6_matchlen(IFA_IN6(ifa), dst);
2099				matchcmp = tlen - blen;
2100				if (matchcmp > 0) /* (3) */
2101					goto replace;
2102				continue; /* (4) */
2103			}
2104			if (dscopecmp > 0) {
2105				if (bscopecmp > 0) /* (5) */
2106					continue;
2107				goto replace; /* (6) */
2108			}
2109			if (dscopecmp < 0) {
2110				if (bscopecmp > 0) /* (7) */
2111					goto replace;
2112				continue; /* (8) */
2113			}
2114
2115			/* now dscopecmp must be 0 */
2116			if (bscopecmp < 0)
2117				goto replace; /* (9) */
2118
2119		  replace:
2120			ifa_best = (struct in6_ifaddr *)ifa;
2121			blen = tlen >= 0 ? tlen :
2122				in6_matchlen(IFA_IN6(ifa), dst);
2123			best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2124		}
2125	}
2126	IFNET_RUNLOCK();
2127
2128	/* count statistics for future improvements */
2129	if (ifa_best == NULL)
2130		ip6stat.ip6s_sources_none++;
2131	else {
2132		if (oifp == ifa_best->ia_ifp)
2133			ip6stat.ip6s_sources_sameif[best_scope]++;
2134		else
2135			ip6stat.ip6s_sources_otherif[best_scope]++;
2136
2137		if (best_scope == dst_scope)
2138			ip6stat.ip6s_sources_samescope[best_scope]++;
2139		else
2140			ip6stat.ip6s_sources_otherscope[best_scope]++;
2141
2142		if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) != 0)
2143			ip6stat.ip6s_sources_deprecated[best_scope]++;
2144	}
2145
2146	return (ifa_best);
2147}
2148
2149/*
2150 * return the best address out of the same scope. if no address was
2151 * found, return the first valid address from designated IF.
2152 */
2153struct in6_ifaddr *
2154in6_ifawithifp(ifp, dst)
2155	struct ifnet *ifp;
2156	struct in6_addr *dst;
2157{
2158	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
2159	struct ifaddr *ifa;
2160	struct in6_ifaddr *besta = 0;
2161	struct in6_ifaddr *dep[2];	/* last-resort: deprecated */
2162
2163	dep[0] = dep[1] = NULL;
2164
2165	/*
2166	 * We first look for addresses in the same scope.
2167	 * If there is one, return it.
2168	 * If two or more, return one which matches the dst longest.
2169	 * If none, return one of global addresses assigned other ifs.
2170	 */
2171	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2172		if (ifa->ifa_addr->sa_family != AF_INET6)
2173			continue;
2174		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2175			continue; /* XXX: is there any case to allow anycast? */
2176		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2177			continue; /* don't use this interface */
2178		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2179			continue;
2180		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2181			if (ip6_use_deprecated)
2182				dep[0] = (struct in6_ifaddr *)ifa;
2183			continue;
2184		}
2185
2186		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2187			/*
2188			 * call in6_matchlen() as few as possible
2189			 */
2190			if (besta) {
2191				if (blen == -1)
2192					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2193				tlen = in6_matchlen(IFA_IN6(ifa), dst);
2194				if (tlen > blen) {
2195					blen = tlen;
2196					besta = (struct in6_ifaddr *)ifa;
2197				}
2198			} else
2199				besta = (struct in6_ifaddr *)ifa;
2200		}
2201	}
2202	if (besta)
2203		return (besta);
2204
2205	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2206		if (ifa->ifa_addr->sa_family != AF_INET6)
2207			continue;
2208		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2209			continue; /* XXX: is there any case to allow anycast? */
2210		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2211			continue; /* don't use this interface */
2212		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2213			continue;
2214		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2215			if (ip6_use_deprecated)
2216				dep[1] = (struct in6_ifaddr *)ifa;
2217			continue;
2218		}
2219
2220		return (struct in6_ifaddr *)ifa;
2221	}
2222
2223	/* use the last-resort values, that are, deprecated addresses */
2224	if (dep[0])
2225		return dep[0];
2226	if (dep[1])
2227		return dep[1];
2228
2229	return NULL;
2230}
2231
2232/*
2233 * perform DAD when interface becomes IFF_UP.
2234 */
2235void
2236in6_if_up(ifp)
2237	struct ifnet *ifp;
2238{
2239	struct ifaddr *ifa;
2240	struct in6_ifaddr *ia;
2241	int dad_delay;		/* delay ticks before DAD output */
2242
2243	/*
2244	 * special cases, like 6to4, are handled in in6_ifattach
2245	 */
2246	in6_ifattach(ifp, NULL);
2247
2248	dad_delay = 0;
2249	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
2250		if (ifa->ifa_addr->sa_family != AF_INET6)
2251			continue;
2252		ia = (struct in6_ifaddr *)ifa;
2253		if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2254			nd6_dad_start(ifa, &dad_delay);
2255	}
2256}
2257
2258int
2259in6if_do_dad(ifp)
2260	struct ifnet *ifp;
2261{
2262	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2263		return (0);
2264
2265	switch (ifp->if_type) {
2266#ifdef IFT_DUMMY
2267	case IFT_DUMMY:
2268#endif
2269	case IFT_FAITH:
2270		/*
2271		 * These interfaces do not have the IFF_LOOPBACK flag,
2272		 * but loop packets back.  We do not have to do DAD on such
2273		 * interfaces.  We should even omit it, because loop-backed
2274		 * NS would confuse the DAD procedure.
2275		 */
2276		return (0);
2277	default:
2278		/*
2279		 * Our DAD routine requires the interface up and running.
2280		 * However, some interfaces can be up before the RUNNING
2281		 * status.  Additionaly, users may try to assign addresses
2282		 * before the interface becomes up (or running).
2283		 * We simply skip DAD in such a case as a work around.
2284		 * XXX: we should rather mark "tentative" on such addresses,
2285		 * and do DAD after the interface becomes ready.
2286		 */
2287		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2288		    (IFF_UP|IFF_RUNNING))
2289			return (0);
2290
2291		return (1);
2292	}
2293}
2294
2295/*
2296 * Calculate max IPv6 MTU through all the interfaces and store it
2297 * to in6_maxmtu.
2298 */
2299void
2300in6_setmaxmtu()
2301{
2302	unsigned long maxmtu = 0;
2303	struct ifnet *ifp;
2304
2305	IFNET_RLOCK();
2306	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
2307		/* this function can be called during ifnet initialization */
2308		if (!ifp->if_afdata[AF_INET6])
2309			continue;
2310		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2311		    IN6_LINKMTU(ifp) > maxmtu)
2312			maxmtu = IN6_LINKMTU(ifp);
2313	}
2314	IFNET_RUNLOCK();
2315	if (maxmtu)	     /* update only when maxmtu is positive */
2316		in6_maxmtu = maxmtu;
2317}
2318
2319void *
2320in6_domifattach(ifp)
2321	struct ifnet *ifp;
2322{
2323	struct in6_ifextra *ext;
2324
2325	ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2326	bzero(ext, sizeof(*ext));
2327
2328	ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2329	    M_IFADDR, M_WAITOK);
2330	bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2331
2332	ext->icmp6_ifstat =
2333	    (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2334	    M_IFADDR, M_WAITOK);
2335	bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2336
2337	ext->nd_ifinfo = nd6_ifattach(ifp);
2338	ext->scope6_id = scope6_ifattach(ifp);
2339	return ext;
2340}
2341
2342void
2343in6_domifdetach(ifp, aux)
2344	struct ifnet *ifp;
2345	void *aux;
2346{
2347	struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2348
2349	scope6_ifdetach(ext->scope6_id);
2350	nd6_ifdetach(ext->nd_ifinfo);
2351	free(ext->in6_ifstat, M_IFADDR);
2352	free(ext->icmp6_ifstat, M_IFADDR);
2353	free(ext, M_IFADDR);
2354}
2355
2356/*
2357 * Convert sockaddr_in6 to sockaddr_in.  Original sockaddr_in6 must be
2358 * v4 mapped addr or v4 compat addr
2359 */
2360void
2361in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2362{
2363	bzero(sin, sizeof(*sin));
2364	sin->sin_len = sizeof(struct sockaddr_in);
2365	sin->sin_family = AF_INET;
2366	sin->sin_port = sin6->sin6_port;
2367	sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
2368}
2369
2370/* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
2371void
2372in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
2373{
2374	bzero(sin6, sizeof(*sin6));
2375	sin6->sin6_len = sizeof(struct sockaddr_in6);
2376	sin6->sin6_family = AF_INET6;
2377	sin6->sin6_port = sin->sin_port;
2378	sin6->sin6_addr.s6_addr32[0] = 0;
2379	sin6->sin6_addr.s6_addr32[1] = 0;
2380	sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
2381	sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
2382}
2383
2384/* Convert sockaddr_in6 into sockaddr_in. */
2385void
2386in6_sin6_2_sin_in_sock(struct sockaddr *nam)
2387{
2388	struct sockaddr_in *sin_p;
2389	struct sockaddr_in6 sin6;
2390
2391	/*
2392	 * Save original sockaddr_in6 addr and convert it
2393	 * to sockaddr_in.
2394	 */
2395	sin6 = *(struct sockaddr_in6 *)nam;
2396	sin_p = (struct sockaddr_in *)nam;
2397	in6_sin6_2_sin(sin_p, &sin6);
2398}
2399
2400/* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
2401void
2402in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
2403{
2404	struct sockaddr_in *sin_p;
2405	struct sockaddr_in6 *sin6_p;
2406
2407	MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
2408	       M_WAITOK);
2409	sin_p = (struct sockaddr_in *)*nam;
2410	in6_sin_2_v4mapsin6(sin_p, sin6_p);
2411	FREE(*nam, M_SONAME);
2412	*nam = (struct sockaddr *)sin6_p;
2413}
2414