in6.c revision 222730
1139826Simp/*-
253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
353541Sshin * All rights reserved.
453541Sshin *
553541Sshin * Redistribution and use in source and binary forms, with or without
653541Sshin * modification, are permitted provided that the following conditions
753541Sshin * are met:
853541Sshin * 1. Redistributions of source code must retain the above copyright
953541Sshin *    notice, this list of conditions and the following disclaimer.
1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1153541Sshin *    notice, this list of conditions and the following disclaimer in the
1253541Sshin *    documentation and/or other materials provided with the distribution.
1353541Sshin * 3. Neither the name of the project nor the names of its contributors
1453541Sshin *    may be used to endorse or promote products derived from this software
1553541Sshin *    without specific prior written permission.
1653541Sshin *
1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2053541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2753541Sshin * SUCH DAMAGE.
28174510Sobrien *
29174510Sobrien *	$KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $
3053541Sshin */
3153541Sshin
32139826Simp/*-
3353541Sshin * Copyright (c) 1982, 1986, 1991, 1993
3453541Sshin *	The Regents of the University of California.  All rights reserved.
3553541Sshin *
3653541Sshin * Redistribution and use in source and binary forms, with or without
3753541Sshin * modification, are permitted provided that the following conditions
3853541Sshin * are met:
3953541Sshin * 1. Redistributions of source code must retain the above copyright
4053541Sshin *    notice, this list of conditions and the following disclaimer.
4153541Sshin * 2. Redistributions in binary form must reproduce the above copyright
4253541Sshin *    notice, this list of conditions and the following disclaimer in the
4353541Sshin *    documentation and/or other materials provided with the distribution.
4453541Sshin * 4. Neither the name of the University nor the names of its contributors
4553541Sshin *    may be used to endorse or promote products derived from this software
4653541Sshin *    without specific prior written permission.
4753541Sshin *
4853541Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4953541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5053541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5153541Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5253541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5353541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5453541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5553541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5653541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5753541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5853541Sshin * SUCH DAMAGE.
5953541Sshin *
6053541Sshin *	@(#)in.c	8.2 (Berkeley) 11/15/93
6153541Sshin */
6253541Sshin
63174510Sobrien#include <sys/cdefs.h>
64174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/in6.c 222730 2011-06-06 02:37:38Z hrs $");
65174510Sobrien
66207268Skib#include "opt_compat.h"
6762587Sitojun#include "opt_inet.h"
6862587Sitojun#include "opt_inet6.h"
6962587Sitojun
7053541Sshin#include <sys/param.h>
7153541Sshin#include <sys/errno.h>
72186948Sbz#include <sys/jail.h>
7353541Sshin#include <sys/malloc.h>
7453541Sshin#include <sys/socket.h>
7553541Sshin#include <sys/socketvar.h>
7653541Sshin#include <sys/sockio.h>
7753541Sshin#include <sys/systm.h>
78164033Srwatson#include <sys/priv.h>
7953541Sshin#include <sys/proc.h>
8053541Sshin#include <sys/time.h>
8153541Sshin#include <sys/kernel.h>
8253541Sshin#include <sys/syslog.h>
8353541Sshin
8453541Sshin#include <net/if.h>
85197227Sqingli#include <net/if_var.h>
8653541Sshin#include <net/if_types.h>
8753541Sshin#include <net/route.h>
8853541Sshin#include <net/if_dl.h>
89185571Sbz#include <net/vnet.h>
9053541Sshin
9153541Sshin#include <netinet/in.h>
9253541Sshin#include <netinet/in_var.h>
93186119Sqingli#include <net/if_llatbl.h>
9453541Sshin#include <netinet/if_ether.h>
9578064Sume#include <netinet/in_systm.h>
9678064Sume#include <netinet/ip.h>
9778064Sume#include <netinet/in_pcb.h>
9853541Sshin
9962587Sitojun#include <netinet/ip6.h>
10053541Sshin#include <netinet6/ip6_var.h>
10195023Ssuz#include <netinet6/nd6.h>
10253541Sshin#include <netinet6/mld6_var.h>
10362587Sitojun#include <netinet6/ip6_mroute.h>
10453541Sshin#include <netinet6/in6_ifattach.h>
10562587Sitojun#include <netinet6/scope6_var.h>
10678064Sume#include <netinet6/in6_pcb.h>
10762587Sitojun
10853541Sshin/*
10953541Sshin * Definitions of some costant IP6 addresses.
11053541Sshin */
11162587Sitojunconst struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
11262587Sitojunconst struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
11362587Sitojunconst struct in6_addr in6addr_nodelocal_allnodes =
11453541Sshin	IN6ADDR_NODELOCAL_ALLNODES_INIT;
11562587Sitojunconst struct in6_addr in6addr_linklocal_allnodes =
11653541Sshin	IN6ADDR_LINKLOCAL_ALLNODES_INIT;
11762587Sitojunconst struct in6_addr in6addr_linklocal_allrouters =
11853541Sshin	IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
119191672Sbmsconst struct in6_addr in6addr_linklocal_allv2routers =
120191672Sbms	IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
12153541Sshin
12262587Sitojunconst struct in6_addr in6mask0 = IN6MASK0;
12362587Sitojunconst struct in6_addr in6mask32 = IN6MASK32;
12462587Sitojunconst struct in6_addr in6mask64 = IN6MASK64;
12562587Sitojunconst struct in6_addr in6mask96 = IN6MASK96;
12662587Sitojunconst struct in6_addr in6mask128 = IN6MASK128;
12753541Sshin
128126552Sumeconst struct sockaddr_in6 sa6_any =
129126552Sume	{ sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
13078064Sume
13162587Sitojunstatic int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
13283366Sjulian	struct ifnet *, struct thread *));
13378064Sumestatic int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *,
134120891Sume	struct sockaddr_in6 *, int));
135175162Sobrienstatic void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
13653541Sshin
13783934Sbrooksint	(*faithprefix_p)(struct in6_addr *);
13883934Sbrooks
139120891Sume
14078064Sume
14153541Sshinint
142171259Sdelphijin6_mask2len(struct in6_addr *mask, u_char *lim0)
14353541Sshin{
14478064Sume	int x = 0, y;
14578064Sume	u_char *lim = lim0, *p;
14653541Sshin
147120891Sume	/* ignore the scope_id part */
148120891Sume	if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
14978064Sume		lim = (u_char *)mask + sizeof(*mask);
15078064Sume	for (p = (u_char *)mask; p < lim; x++, p++) {
15178064Sume		if (*p != 0xff)
15253541Sshin			break;
15353541Sshin	}
15453541Sshin	y = 0;
15578064Sume	if (p < lim) {
15653541Sshin		for (y = 0; y < 8; y++) {
15778064Sume			if ((*p & (0x80 >> y)) == 0)
15853541Sshin				break;
15953541Sshin		}
16053541Sshin	}
16178064Sume
16278064Sume	/*
16378064Sume	 * when the limit pointer is given, do a stricter check on the
16478064Sume	 * remaining bits.
16578064Sume	 */
16678064Sume	if (p < lim) {
16778064Sume		if (y != 0 && (*p & (0x00ff >> y)) != 0)
168120856Sume			return (-1);
16978064Sume		for (p = p + 1; p < lim; p++)
17078064Sume			if (*p != 0)
171120856Sume				return (-1);
17278064Sume	}
173120891Sume
17453541Sshin	return x * 8 + y;
17553541Sshin}
17653541Sshin
17753541Sshin#define ifa2ia6(ifa)	((struct in6_ifaddr *)(ifa))
17862587Sitojun#define ia62ifa(ia6)	(&((ia6)->ia_ifa))
17953541Sshin
180207268Skib#ifdef COMPAT_FREEBSD32
181207268Skibstruct in6_ndifreq32 {
182207268Skib        char ifname[IFNAMSIZ];
183207268Skib        uint32_t ifindex;
184207268Skib};
185207268Skib#define	SIOCGDEFIFACE32_IN6     _IOWR('i', 86, struct in6_ndifreq32)
186207268Skib#endif
187207268Skib
18853541Sshinint
189171259Sdelphijin6_control(struct socket *so, u_long cmd, caddr_t data,
190171259Sdelphij    struct ifnet *ifp, struct thread *td)
19153541Sshin{
19253541Sshin	struct	in6_ifreq *ifr = (struct in6_ifreq *)data;
19378064Sume	struct	in6_ifaddr *ia = NULL;
19453541Sshin	struct	in6_aliasreq *ifra = (struct in6_aliasreq *)data;
195151539Ssuz	struct sockaddr_in6 *sa6;
196164033Srwatson	int error;
19753541Sshin
19862587Sitojun	switch (cmd) {
19962587Sitojun	case SIOCGETSGCNT_IN6:
20062587Sitojun	case SIOCGETMIFCNT_IN6:
201166938Sbms		return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
20262587Sitojun	}
20353541Sshin
204121742Sume	switch(cmd) {
205121742Sume	case SIOCAADDRCTL_POLICY:
206121742Sume	case SIOCDADDRCTL_POLICY:
207164033Srwatson		if (td != NULL) {
208164033Srwatson			error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
209164033Srwatson			if (error)
210164033Srwatson				return (error);
211164033Srwatson		}
212121742Sume		return (in6_src_ioctl(cmd, data));
213121742Sume	}
214121742Sume
21562587Sitojun	if (ifp == NULL)
216120856Sume		return (EOPNOTSUPP);
21753541Sshin
21853541Sshin	switch (cmd) {
21953541Sshin	case SIOCSNDFLUSH_IN6:
22053541Sshin	case SIOCSPFXFLUSH_IN6:
22153541Sshin	case SIOCSRTRFLUSH_IN6:
22262587Sitojun	case SIOCSDEFIFACE_IN6:
22362587Sitojun	case SIOCSIFINFO_FLAGS:
224193893Scperciva	case SIOCSIFINFO_IN6:
225164033Srwatson		if (td != NULL) {
226164033Srwatson			error = priv_check(td, PRIV_NETINET_ND6);
227164033Srwatson			if (error)
228164033Srwatson				return (error);
229164033Srwatson		}
230120891Sume		/* FALLTHROUGH */
23178064Sume	case OSIOCGIFINFO_IN6:
23253541Sshin	case SIOCGIFINFO_IN6:
23353541Sshin	case SIOCGDRLST_IN6:
23453541Sshin	case SIOCGPRLST_IN6:
23553541Sshin	case SIOCGNBRINFO_IN6:
23662587Sitojun	case SIOCGDEFIFACE_IN6:
237120856Sume		return (nd6_ioctl(cmd, data, ifp));
238207268Skib
239207268Skib#ifdef COMPAT_FREEBSD32
240207268Skib	case SIOCGDEFIFACE32_IN6:
241207268Skib		{
242207268Skib			struct in6_ndifreq ndif;
243207268Skib			struct in6_ndifreq32 *ndif32;
244207268Skib
245207268Skib			error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif,
246207268Skib			    ifp);
247207268Skib			if (error)
248207268Skib				return (error);
249207268Skib			ndif32 = (struct in6_ndifreq32 *)data;
250207268Skib			ndif32->ifindex = ndif.ifindex;
251207268Skib			return (0);
252207268Skib		}
253207268Skib#endif
25453541Sshin	}
25553541Sshin
25653541Sshin	switch (cmd) {
25753541Sshin	case SIOCSIFPREFIX_IN6:
25853541Sshin	case SIOCDIFPREFIX_IN6:
25953541Sshin	case SIOCAIFPREFIX_IN6:
26053541Sshin	case SIOCCIFPREFIX_IN6:
26153541Sshin	case SIOCSGIFPREFIX_IN6:
26253541Sshin	case SIOCGIFPREFIX_IN6:
26378064Sume		log(LOG_NOTICE,
26478064Sume		    "prefix ioctls are now invalidated. "
26578064Sume		    "please use ifconfig.\n");
266120856Sume		return (EOPNOTSUPP);
26753541Sshin	}
26853541Sshin
26995023Ssuz	switch (cmd) {
27062587Sitojun	case SIOCSSCOPE6:
271164033Srwatson		if (td != NULL) {
272164033Srwatson			error = priv_check(td, PRIV_NETINET_SCOPE6);
273164033Srwatson			if (error)
274164033Srwatson				return (error);
275164033Srwatson		}
276121161Sume		return (scope6_set(ifp,
277121161Sume		    (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
27862587Sitojun	case SIOCGSCOPE6:
279121161Sume		return (scope6_get(ifp,
280121161Sume		    (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
28162587Sitojun	case SIOCGSCOPE6DEF:
282121161Sume		return (scope6_get_default((struct scope6_id *)
283121161Sume		    ifr->ifr_ifru.ifru_scope_id));
28462587Sitojun	}
28562587Sitojun
28653541Sshin	switch (cmd) {
28753541Sshin	case SIOCALIFADDR:
288175630Sbz		if (td != NULL) {
289175630Sbz			error = priv_check(td, PRIV_NET_ADDIFADDR);
290175630Sbz			if (error)
291175630Sbz				return (error);
292175630Sbz		}
293175630Sbz		return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
294175630Sbz
29553541Sshin	case SIOCDLIFADDR:
296164033Srwatson		if (td != NULL) {
297175630Sbz			error = priv_check(td, PRIV_NET_DELIFADDR);
298164033Srwatson			if (error)
299164033Srwatson				return (error);
300164033Srwatson		}
301120891Sume		/* FALLTHROUGH */
30253541Sshin	case SIOCGLIFADDR:
30383366Sjulian		return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
30453541Sshin	}
30553541Sshin
30653541Sshin	/*
30753541Sshin	 * Find address for this interface, if it exists.
308151539Ssuz	 *
309151539Ssuz	 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
310151539Ssuz	 * only, and used the first interface address as the target of other
311151539Ssuz	 * operations (without checking ifra_addr).  This was because netinet
312151539Ssuz	 * code/API assumed at most 1 interface address per interface.
313151539Ssuz	 * Since IPv6 allows a node to assign multiple addresses
314151539Ssuz	 * on a single interface, we almost always look and check the
315151539Ssuz	 * presence of ifra_addr, and reject invalid ones here.
316151539Ssuz	 * It also decreases duplicated code among SIOC*_IN6 operations.
31753541Sshin	 */
318151539Ssuz	switch (cmd) {
319151539Ssuz	case SIOCAIFADDR_IN6:
320151539Ssuz	case SIOCSIFPHYADDR_IN6:
321151539Ssuz		sa6 = &ifra->ifra_addr;
322151539Ssuz		break;
323151539Ssuz	case SIOCSIFADDR_IN6:
324151539Ssuz	case SIOCGIFADDR_IN6:
325151539Ssuz	case SIOCSIFDSTADDR_IN6:
326151539Ssuz	case SIOCSIFNETMASK_IN6:
327151539Ssuz	case SIOCGIFDSTADDR_IN6:
328151539Ssuz	case SIOCGIFNETMASK_IN6:
329151539Ssuz	case SIOCDIFADDR_IN6:
330151539Ssuz	case SIOCGIFPSRCADDR_IN6:
331151539Ssuz	case SIOCGIFPDSTADDR_IN6:
332151539Ssuz	case SIOCGIFAFLAG_IN6:
333151539Ssuz	case SIOCSNDFLUSH_IN6:
334151539Ssuz	case SIOCSPFXFLUSH_IN6:
335151539Ssuz	case SIOCSRTRFLUSH_IN6:
336151539Ssuz	case SIOCGIFALIFETIME_IN6:
337151539Ssuz	case SIOCSIFALIFETIME_IN6:
338151539Ssuz	case SIOCGIFSTAT_IN6:
339151539Ssuz	case SIOCGIFSTAT_ICMP6:
340151539Ssuz		sa6 = &ifr->ifr_addr;
341151539Ssuz		break;
342151539Ssuz	default:
343151539Ssuz		sa6 = NULL;
344151539Ssuz		break;
345151539Ssuz	}
346151539Ssuz	if (sa6 && sa6->sin6_family == AF_INET6) {
347151539Ssuz		if (sa6->sin6_scope_id != 0)
348151539Ssuz			error = sa6_embedscope(sa6, 0);
349148385Sume		else
350151539Ssuz			error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
351148385Sume		if (error != 0)
352148385Sume			return (error);
353188144Sjamie		if (td != NULL && (error = prison_check_ip6(td->td_ucred,
354188144Sjamie		    &sa6->sin6_addr)) != 0)
355188144Sjamie			return (error);
356151539Ssuz		ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
357151539Ssuz	} else
358151539Ssuz		ia = NULL;
35953541Sshin
36053541Sshin	switch (cmd) {
36178064Sume	case SIOCSIFADDR_IN6:
36278064Sume	case SIOCSIFDSTADDR_IN6:
36378064Sume	case SIOCSIFNETMASK_IN6:
36478064Sume		/*
36578064Sume		 * Since IPv6 allows a node to assign multiple addresses
366151465Ssuz		 * on a single interface, SIOCSIFxxx ioctls are deprecated.
36778064Sume		 */
36878064Sume		/* we decided to obsolete this command (20000704) */
369194760Srwatson		error = EINVAL;
370194760Srwatson		goto out;
37153541Sshin
37253541Sshin	case SIOCDIFADDR_IN6:
37362587Sitojun		/*
37478064Sume		 * for IPv4, we look for existing in_ifaddr here to allow
375151465Ssuz		 * "ifconfig if0 delete" to remove the first IPv4 address on
376151465Ssuz		 * the interface.  For IPv6, as the spec allows multiple
377151465Ssuz		 * interface address from the day one, we consider "remove the
378151465Ssuz		 * first one" semantics to be not preferable.
37962587Sitojun		 */
380194760Srwatson		if (ia == NULL) {
381194760Srwatson			error = EADDRNOTAVAIL;
382194760Srwatson			goto out;
383194760Srwatson		}
38453541Sshin		/* FALLTHROUGH */
38553541Sshin	case SIOCAIFADDR_IN6:
38662587Sitojun		/*
38778064Sume		 * We always require users to specify a valid IPv6 address for
38878064Sume		 * the corresponding operation.
38962587Sitojun		 */
39078064Sume		if (ifra->ifra_addr.sin6_family != AF_INET6 ||
391194760Srwatson		    ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
392194760Srwatson			error = EAFNOSUPPORT;
393194760Srwatson			goto out;
394194760Srwatson		}
39553541Sshin
396164033Srwatson		if (td != NULL) {
397175630Sbz			error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
398175630Sbz			    PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
399164033Srwatson			if (error)
400194760Srwatson				goto out;
401164033Srwatson		}
40253541Sshin		break;
40353541Sshin
40453541Sshin	case SIOCGIFADDR_IN6:
40553541Sshin		/* This interface is basically deprecated. use SIOCGIFCONF. */
406120891Sume		/* FALLTHROUGH */
40753541Sshin	case SIOCGIFAFLAG_IN6:
40853541Sshin	case SIOCGIFNETMASK_IN6:
40953541Sshin	case SIOCGIFDSTADDR_IN6:
41053541Sshin	case SIOCGIFALIFETIME_IN6:
41153541Sshin		/* must think again about its semantics */
412194760Srwatson		if (ia == NULL) {
413194760Srwatson			error = EADDRNOTAVAIL;
414194760Srwatson			goto out;
415194760Srwatson		}
41653541Sshin		break;
417194760Srwatson
41853541Sshin	case SIOCSIFALIFETIME_IN6:
41953541Sshin	    {
42053541Sshin		struct in6_addrlifetime *lt;
42153541Sshin
422164033Srwatson		if (td != NULL) {
423164033Srwatson			error = priv_check(td, PRIV_NETINET_ALIFETIME6);
424164033Srwatson			if (error)
425194760Srwatson				goto out;
426164033Srwatson		}
427194760Srwatson		if (ia == NULL) {
428194760Srwatson			error = EADDRNOTAVAIL;
429194760Srwatson			goto out;
430194760Srwatson		}
43153541Sshin		/* sanity for overflow - beware unsigned */
43253541Sshin		lt = &ifr->ifr_ifru.ifru_lifetime;
433126552Sume		if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
434126552Sume		    lt->ia6t_vltime + time_second < time_second) {
435194760Srwatson			error = EINVAL;
436194760Srwatson			goto out;
43753541Sshin		}
438126552Sume		if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
439126552Sume		    lt->ia6t_pltime + time_second < time_second) {
440194760Srwatson			error = EINVAL;
441194760Srwatson			goto out;
44253541Sshin		}
44353541Sshin		break;
44453541Sshin	    }
44553541Sshin	}
44653541Sshin
44753541Sshin	switch (cmd) {
44853541Sshin	case SIOCGIFADDR_IN6:
44953541Sshin		ifr->ifr_addr = ia->ia_addr;
450148385Sume		if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
451194760Srwatson			goto out;
45253541Sshin		break;
45353541Sshin
45453541Sshin	case SIOCGIFDSTADDR_IN6:
455194760Srwatson		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
456194760Srwatson			error = EINVAL;
457194760Srwatson			goto out;
458194760Srwatson		}
45962587Sitojun		/*
46062587Sitojun		 * XXX: should we check if ifa_dstaddr is NULL and return
46162587Sitojun		 * an error?
46262587Sitojun		 */
46353541Sshin		ifr->ifr_dstaddr = ia->ia_dstaddr;
464148385Sume		if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
465194760Srwatson			goto out;
46653541Sshin		break;
46753541Sshin
46853541Sshin	case SIOCGIFNETMASK_IN6:
46953541Sshin		ifr->ifr_addr = ia->ia_prefixmask;
47053541Sshin		break;
47153541Sshin
47253541Sshin	case SIOCGIFAFLAG_IN6:
47353541Sshin		ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
47453541Sshin		break;
47553541Sshin
47653541Sshin	case SIOCGIFSTAT_IN6:
477194760Srwatson		if (ifp == NULL) {
478194760Srwatson			error = EINVAL;
479194760Srwatson			goto out;
480194760Srwatson		}
481121161Sume		bzero(&ifr->ifr_ifru.ifru_stat,
482121161Sume		    sizeof(ifr->ifr_ifru.ifru_stat));
483121161Sume		ifr->ifr_ifru.ifru_stat =
484121161Sume		    *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat;
48553541Sshin		break;
48653541Sshin
48753541Sshin	case SIOCGIFSTAT_ICMP6:
488194760Srwatson		if (ifp == NULL) {
489194760Srwatson			error = EINVAL;
490194760Srwatson			goto out;
491194760Srwatson		}
492155454Sgnn		bzero(&ifr->ifr_ifru.ifru_icmp6stat,
493121161Sume		    sizeof(ifr->ifr_ifru.ifru_icmp6stat));
494121161Sume		ifr->ifr_ifru.ifru_icmp6stat =
495121161Sume		    *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat;
49653541Sshin		break;
49753541Sshin
49853541Sshin	case SIOCGIFALIFETIME_IN6:
49953541Sshin		ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
500151539Ssuz		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
501151539Ssuz			time_t maxexpire;
502151539Ssuz			struct in6_addrlifetime *retlt =
503151539Ssuz			    &ifr->ifr_ifru.ifru_lifetime;
504151539Ssuz
505151539Ssuz			/*
506151539Ssuz			 * XXX: adjust expiration time assuming time_t is
507151539Ssuz			 * signed.
508151539Ssuz			 */
509151539Ssuz			maxexpire = (-1) &
510151546Ssuz			    ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
511151539Ssuz			if (ia->ia6_lifetime.ia6t_vltime <
512151539Ssuz			    maxexpire - ia->ia6_updatetime) {
513151539Ssuz				retlt->ia6t_expire = ia->ia6_updatetime +
514151539Ssuz				    ia->ia6_lifetime.ia6t_vltime;
515151539Ssuz			} else
516151539Ssuz				retlt->ia6t_expire = maxexpire;
517151539Ssuz		}
518151539Ssuz		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
519151539Ssuz			time_t maxexpire;
520151539Ssuz			struct in6_addrlifetime *retlt =
521151539Ssuz			    &ifr->ifr_ifru.ifru_lifetime;
522151539Ssuz
523151539Ssuz			/*
524151539Ssuz			 * XXX: adjust expiration time assuming time_t is
525151539Ssuz			 * signed.
526151539Ssuz			 */
527151539Ssuz			maxexpire = (-1) &
528151546Ssuz			    ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
529151539Ssuz			if (ia->ia6_lifetime.ia6t_pltime <
530151539Ssuz			    maxexpire - ia->ia6_updatetime) {
531151539Ssuz				retlt->ia6t_preferred = ia->ia6_updatetime +
532151539Ssuz				    ia->ia6_lifetime.ia6t_pltime;
533151539Ssuz			} else
534151539Ssuz				retlt->ia6t_preferred = maxexpire;
535151539Ssuz		}
53653541Sshin		break;
53753541Sshin
53853541Sshin	case SIOCSIFALIFETIME_IN6:
53953541Sshin		ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
54053541Sshin		/* for sanity */
54153541Sshin		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
54253541Sshin			ia->ia6_lifetime.ia6t_expire =
54353541Sshin				time_second + ia->ia6_lifetime.ia6t_vltime;
54453541Sshin		} else
54553541Sshin			ia->ia6_lifetime.ia6t_expire = 0;
54653541Sshin		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
54753541Sshin			ia->ia6_lifetime.ia6t_preferred =
54853541Sshin				time_second + ia->ia6_lifetime.ia6t_pltime;
54953541Sshin		} else
55053541Sshin			ia->ia6_lifetime.ia6t_preferred = 0;
55153541Sshin		break;
55253541Sshin
55378064Sume	case SIOCAIFADDR_IN6:
55478064Sume	{
555194760Srwatson		int i;
556151539Ssuz		struct nd_prefixctl pr0;
557151539Ssuz		struct nd_prefix *pr;
55878064Sume
55962587Sitojun		/*
56078064Sume		 * first, make or update the interface address structure,
56178064Sume		 * and link it to the list.
56262587Sitojun		 */
563151539Ssuz		if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
564194760Srwatson			goto out;
565194760Srwatson		if (ia != NULL)
566194760Srwatson			ifa_free(&ia->ia_ifa);
567151915Ssuz		if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
568151915Ssuz		    == NULL) {
569171260Sdelphij			/*
570151915Ssuz			 * this can happen when the user specify the 0 valid
571151915Ssuz			 * lifetime.
572151915Ssuz			 */
573151915Ssuz			break;
574151915Ssuz		}
57553541Sshin
57678064Sume		/*
57778064Sume		 * then, make the prefix on-link on the interface.
57878064Sume		 * XXX: we'd rather create the prefix before the address, but
57978064Sume		 * we need at least one address to install the corresponding
58078064Sume		 * interface route, so we configure the address first.
58178064Sume		 */
58278064Sume
58378064Sume		/*
58478064Sume		 * convert mask to prefix length (prefixmask has already
58578064Sume		 * been validated in in6_update_ifa().
58678064Sume		 */
58778064Sume		bzero(&pr0, sizeof(pr0));
58878064Sume		pr0.ndpr_ifp = ifp;
58978064Sume		pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
590120891Sume		    NULL);
591120891Sume		if (pr0.ndpr_plen == 128) {
59278064Sume			break;	/* we don't need to install a host route. */
593120891Sume		}
59478064Sume		pr0.ndpr_prefix = ifra->ifra_addr;
59578064Sume		/* apply the mask for safety. */
59678064Sume		for (i = 0; i < 4; i++) {
59778064Sume			pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
598120891Sume			    ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
59978064Sume		}
60078064Sume		/*
60195023Ssuz		 * XXX: since we don't have an API to set prefix (not address)
60295023Ssuz		 * lifetimes, we just use the same lifetimes as addresses.
60395023Ssuz		 * The (temporarily) installed lifetimes can be overridden by
60495023Ssuz		 * later advertised RAs (when accept_rtadv is non 0), which is
60595023Ssuz		 * an intended behavior.
60678064Sume		 */
60778064Sume		pr0.ndpr_raf_onlink = 1; /* should be configurable? */
60878064Sume		pr0.ndpr_raf_auto =
609120891Sume		    ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
61078064Sume		pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
61178064Sume		pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
61278064Sume
613120891Sume		/* add the prefix if not yet. */
61478064Sume		if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
61578064Sume			/*
61678064Sume			 * nd6_prelist_add will install the corresponding
61778064Sume			 * interface route.
61878064Sume			 */
61978064Sume			if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
620194760Srwatson				goto out;
62178064Sume			if (pr == NULL) {
622120891Sume				log(LOG_ERR, "nd6_prelist_add succeeded but "
62378064Sume				    "no prefix\n");
624194760Srwatson				error = EINVAL;
625194760Srwatson				goto out;
62678064Sume			}
62778064Sume		}
62878064Sume
629151915Ssuz		/* relate the address to the prefix */
630151915Ssuz		if (ia->ia6_ndpr == NULL) {
631151915Ssuz			ia->ia6_ndpr = pr;
632151915Ssuz			pr->ndpr_refcnt++;
63378064Sume
63478064Sume			/*
635151915Ssuz			 * If this is the first autoconf address from the
636151915Ssuz			 * prefix, create a temporary address as well
637151915Ssuz			 * (when required).
63878064Sume			 */
639151915Ssuz			if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
640181803Sbz			    V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
641151915Ssuz				int e;
642151915Ssuz				if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
643151915Ssuz					log(LOG_NOTICE, "in6_control: failed "
644151915Ssuz					    "to create a temporary address, "
645151915Ssuz					    "errno=%d\n", e);
646151915Ssuz				}
647151915Ssuz			}
64862587Sitojun		}
649151915Ssuz
650151915Ssuz		/*
651151915Ssuz		 * this might affect the status of autoconfigured addresses,
652151915Ssuz		 * that is, this address might make other addresses detached.
653151915Ssuz		 */
654151915Ssuz		pfxlist_onlink_check();
655222730Shrs		if (error == 0 && ia) {
656222730Shrs			if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
657222730Shrs				/*
658222730Shrs				 * Try to clear the flag when a new
659222730Shrs				 * IPv6 address is added onto an
660222730Shrs				 * IFDISABLED interface and it
661222730Shrs				 * succeeds.
662222730Shrs				 */
663222730Shrs				struct in6_ndireq nd;
664222730Shrs
665222730Shrs				memset(&nd, 0, sizeof(nd));
666222730Shrs				nd.ndi.flags = ND_IFINFO(ifp)->flags;
667222730Shrs				nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
668222730Shrs				if (nd6_ioctl(SIOCSIFINFO_FLAGS,
669222730Shrs				    (caddr_t)&nd, ifp) < 0)
670222730Shrs					log(LOG_NOTICE, "SIOCAIFADDR_IN6: "
671222730Shrs					    "SIOCSIFINFO_FLAGS for -ifdisabled "
672222730Shrs					    "failed.");
673222730Shrs				/*
674222730Shrs				 * Ignore failure of clearing the flag
675222730Shrs				 * intentionally.  The failure means
676222730Shrs				 * address duplication was detected.
677222730Shrs				 */
678222730Shrs			}
679126264Smlaier			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
680222730Shrs		}
68178064Sume		break;
68278064Sume	}
68362587Sitojun
68478064Sume	case SIOCDIFADDR_IN6:
68578064Sume	{
686151539Ssuz		struct nd_prefix *pr;
68778064Sume
68878064Sume		/*
68978064Sume		 * If the address being deleted is the only one that owns
69078064Sume		 * the corresponding prefix, expire the prefix as well.
691120891Sume		 * XXX: theoretically, we don't have to worry about such
69278064Sume		 * relationship, since we separate the address management
69378064Sume		 * and the prefix management.  We do this, however, to provide
69478064Sume		 * as much backward compatibility as possible in terms of
69578064Sume		 * the ioctl operation.
696151915Ssuz		 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
69778064Sume		 */
698151915Ssuz		pr = ia->ia6_ndpr;
69978064Sume		in6_purgeaddr(&ia->ia_ifa);
700151915Ssuz		if (pr && pr->ndpr_refcnt == 0)
701151915Ssuz			prelist_remove(pr);
702126264Smlaier		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
70353541Sshin		break;
70478064Sume	}
70553541Sshin
70678064Sume	default:
707194760Srwatson		if (ifp == NULL || ifp->if_ioctl == 0) {
708194760Srwatson			error = EOPNOTSUPP;
709194760Srwatson			goto out;
710194760Srwatson		}
711194760Srwatson		error = (*ifp->if_ioctl)(ifp, cmd, data);
712194760Srwatson		goto out;
71378064Sume	}
71453541Sshin
715194760Srwatson	error = 0;
716194760Srwatsonout:
717194760Srwatson	if (ia != NULL)
718194760Srwatson		ifa_free(&ia->ia_ifa);
719194760Srwatson	return (error);
72078064Sume}
72153541Sshin
72278064Sume/*
72378064Sume * Update parameters of an IPv6 interface address.
72478064Sume * If necessary, a new entry is created and linked into address chains.
72578064Sume * This function is separated from in6_control().
72678064Sume * XXX: should this be performed under splnet()?
72778064Sume */
72878064Sumeint
729171259Sdelphijin6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
730171259Sdelphij    struct in6_ifaddr *ia, int flags)
73178064Sume{
73278064Sume	int error = 0, hostIsNew = 0, plen = -1;
73378064Sume	struct sockaddr_in6 dst6;
73478064Sume	struct in6_addrlifetime *lt;
735151539Ssuz	struct in6_multi_mship *imm;
736151539Ssuz	struct in6_multi *in6m_sol;
737148385Sume	struct rtentry *rt;
738151539Ssuz	int delay;
739165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
74078064Sume
74178064Sume	/* Validate parameters */
74278064Sume	if (ifp == NULL || ifra == NULL) /* this maybe redundant */
743120856Sume		return (EINVAL);
74478064Sume
74578064Sume	/*
74678064Sume	 * The destination address for a p2p link must have a family
74778064Sume	 * of AF_UNSPEC or AF_INET6.
74878064Sume	 */
74978064Sume	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
75078064Sume	    ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
75178064Sume	    ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
752120856Sume		return (EAFNOSUPPORT);
75378064Sume	/*
75478064Sume	 * validate ifra_prefixmask.  don't check sin6_family, netmask
75578064Sume	 * does not carry fields other than sin6_len.
75678064Sume	 */
75778064Sume	if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
758120856Sume		return (EINVAL);
75978064Sume	/*
76078064Sume	 * Because the IPv6 address architecture is classless, we require
76178064Sume	 * users to specify a (non 0) prefix length (mask) for a new address.
76278064Sume	 * We also require the prefix (when specified) mask is valid, and thus
76378064Sume	 * reject a non-consecutive mask.
76478064Sume	 */
76578064Sume	if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
766120856Sume		return (EINVAL);
76778064Sume	if (ifra->ifra_prefixmask.sin6_len != 0) {
76878064Sume		plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
769120891Sume		    (u_char *)&ifra->ifra_prefixmask +
770120891Sume		    ifra->ifra_prefixmask.sin6_len);
77178064Sume		if (plen <= 0)
772120856Sume			return (EINVAL);
773120891Sume	} else {
77462587Sitojun		/*
77595023Ssuz		 * In this case, ia must not be NULL.  We just use its prefix
77678064Sume		 * length.
77762587Sitojun		 */
77878064Sume		plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
77978064Sume	}
78078064Sume	/*
78178064Sume	 * If the destination address on a p2p interface is specified,
78278064Sume	 * and the address is a scoped one, validate/set the scope
78378064Sume	 * zone identifier.
78478064Sume	 */
78578064Sume	dst6 = ifra->ifra_dstaddr;
786120891Sume	if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
78778064Sume	    (dst6.sin6_family == AF_INET6)) {
788148385Sume		struct in6_addr in6_tmp;
789126552Sume		u_int32_t zoneid;
79078064Sume
791148385Sume		in6_tmp = dst6.sin6_addr;
792148385Sume		if (in6_setscope(&in6_tmp, ifp, &zoneid))
793148385Sume			return (EINVAL); /* XXX: should be impossible */
794148385Sume
795148385Sume		if (dst6.sin6_scope_id != 0) {
796148385Sume			if (dst6.sin6_scope_id != zoneid)
797148385Sume				return (EINVAL);
798148385Sume		} else		/* user omit to specify the ID. */
799126552Sume			dst6.sin6_scope_id = zoneid;
800148385Sume
801148385Sume		/* convert into the internal form */
802148385Sume		if (sa6_embedscope(&dst6, 0))
803148385Sume			return (EINVAL); /* XXX: should be impossible */
80478064Sume	}
80578064Sume	/*
80678064Sume	 * The destination address can be specified only for a p2p or a
80778064Sume	 * loopback interface.  If specified, the corresponding prefix length
80878064Sume	 * must be 128.
80978064Sume	 */
81078064Sume	if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
81178064Sume		if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
812126552Sume			/* XXX: noisy message */
813122059Sume			nd6log((LOG_INFO, "in6_update_ifa: a destination can "
814122059Sume			    "be specified for a p2p or a loopback IF only\n"));
815120856Sume			return (EINVAL);
81678064Sume		}
81778064Sume		if (plen != 128) {
818122059Sume			nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
819122059Sume			    "be 128 when dstaddr is specified\n"));
820120856Sume			return (EINVAL);
82178064Sume		}
82278064Sume	}
82378064Sume	/* lifetime consistency check */
82478064Sume	lt = &ifra->ifra_lifetime;
825151539Ssuz	if (lt->ia6t_pltime > lt->ia6t_vltime)
826151539Ssuz		return (EINVAL);
82778064Sume	if (lt->ia6t_vltime == 0) {
82862587Sitojun		/*
82978064Sume		 * the following log might be noisy, but this is a typical
83078064Sume		 * configuration mistake or a tool's bug.
83162587Sitojun		 */
832122059Sume		nd6log((LOG_INFO,
83378064Sume		    "in6_update_ifa: valid lifetime is 0 for %s\n",
834165118Sbz		    ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
835151539Ssuz
836151539Ssuz		if (ia == NULL)
837151539Ssuz			return (0); /* there's nothing to do */
83878064Sume	}
83962587Sitojun
84078064Sume	/*
84178064Sume	 * If this is a new address, allocate a new ifaddr and link it
84278064Sume	 * into chains.
84378064Sume	 */
84478064Sume	if (ia == NULL) {
84578064Sume		hostIsNew = 1;
84679763Sume		/*
84779763Sume		 * When in6_update_ifa() is called in a process of a received
848120891Sume		 * RA, it is called under an interrupt context.  So, we should
849120891Sume		 * call malloc with M_NOWAIT.
85079763Sume		 */
851120891Sume		ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
852120891Sume		    M_NOWAIT);
85378064Sume		if (ia == NULL)
85478064Sume			return (ENOBUFS);
85578064Sume		bzero((caddr_t)ia, sizeof(*ia));
856194602Srwatson		ifa_init(&ia->ia_ifa);
857170202Sjinmei		LIST_INIT(&ia->ia6_memberships);
858151539Ssuz		/* Initialize the address and masks, and put time stamp */
85978064Sume		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
86078064Sume		ia->ia_addr.sin6_family = AF_INET6;
86178064Sume		ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
862151539Ssuz		ia->ia6_createtime = time_second;
86378064Sume		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
86478064Sume			/*
86578064Sume			 * XXX: some functions expect that ifa_dstaddr is not
86678064Sume			 * NULL for p2p interfaces.
86778064Sume			 */
868120891Sume			ia->ia_ifa.ifa_dstaddr =
869120891Sume			    (struct sockaddr *)&ia->ia_dstaddr;
87078064Sume		} else {
87178064Sume			ia->ia_ifa.ifa_dstaddr = NULL;
87253541Sshin		}
873108033Shsu		ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
87478064Sume		ia->ia_ifp = ifp;
875194760Srwatson		ifa_ref(&ia->ia_ifa);			/* if_addrhead */
876191323Srwatson		IF_ADDR_LOCK(ifp);
877191340Srwatson		TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
878191323Srwatson		IF_ADDR_UNLOCK(ifp);
879194907Srwatson
880194971Srwatson		ifa_ref(&ia->ia_ifa);			/* in6_ifaddrhead */
881194971Srwatson		IN6_IFADDR_WLOCK();
882194907Srwatson		TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
883194971Srwatson		IN6_IFADDR_WUNLOCK();
88478064Sume	}
88578064Sume
886151539Ssuz	/* update timestamp */
887151539Ssuz	ia->ia6_updatetime = time_second;
888151539Ssuz
88978064Sume	/* set prefix mask */
89078064Sume	if (ifra->ifra_prefixmask.sin6_len) {
89178064Sume		/*
89278064Sume		 * We prohibit changing the prefix length of an existing
89378064Sume		 * address, because
89478064Sume		 * + such an operation should be rare in IPv6, and
89578064Sume		 * + the operation would confuse prefix management.
89678064Sume		 */
89778064Sume		if (ia->ia_prefixmask.sin6_len &&
89878064Sume		    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
899122059Sume			nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
90078064Sume			    " existing (%s) address should not be changed\n",
901165118Sbz			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
90278064Sume			error = EINVAL;
90378064Sume			goto unlink;
90453541Sshin		}
90578064Sume		ia->ia_prefixmask = ifra->ifra_prefixmask;
90678064Sume	}
90778064Sume
90878064Sume	/*
90978064Sume	 * If a new destination address is specified, scrub the old one and
91078064Sume	 * install the new destination.  Note that the interface must be
911120891Sume	 * p2p or loopback (see the check above.)
91278064Sume	 */
91378064Sume	if (dst6.sin6_family == AF_INET6 &&
914120891Sume	    !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
91578064Sume		int e;
91678064Sume
91778064Sume		if ((ia->ia_flags & IFA_ROUTE) != 0 &&
918120891Sume		    (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
919122059Sume			nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
92078064Sume			    "a route to the old destination: %s\n",
921165118Sbz			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
92278064Sume			/* proceed anyway... */
923120891Sume		} else
92478064Sume			ia->ia_flags &= ~IFA_ROUTE;
92578064Sume		ia->ia_dstaddr = dst6;
92678064Sume	}
92753541Sshin
928148385Sume	/*
929148385Sume	 * Set lifetimes.  We do not refer to ia6t_expire and ia6t_preferred
930148385Sume	 * to see if the address is deprecated or invalidated, but initialize
931148385Sume	 * these members for applications.
932148385Sume	 */
933148385Sume	ia->ia6_lifetime = ifra->ifra_lifetime;
934148385Sume	if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
935148385Sume		ia->ia6_lifetime.ia6t_expire =
936148385Sume		    time_second + ia->ia6_lifetime.ia6t_vltime;
937148385Sume	} else
938148385Sume		ia->ia6_lifetime.ia6t_expire = 0;
939148385Sume	if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
940148385Sume		ia->ia6_lifetime.ia6t_preferred =
941148385Sume		    time_second + ia->ia6_lifetime.ia6t_pltime;
942148385Sume	} else
943148385Sume		ia->ia6_lifetime.ia6t_preferred = 0;
944148385Sume
94578064Sume	/* reset the interface and routing table appropriately. */
94678064Sume	if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
94778064Sume		goto unlink;
94878064Sume
94978064Sume	/*
950148385Sume	 * configure address flags.
951148385Sume	 */
952148385Sume	ia->ia6_flags = ifra->ifra_flags;
953148385Sume	/*
954148385Sume	 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
955148385Sume	 * userland, make it deprecated.
956148385Sume	 */
957148385Sume	if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
958148385Sume		ia->ia6_lifetime.ia6t_pltime = 0;
959148385Sume		ia->ia6_lifetime.ia6t_preferred = time_second;
960148385Sume	}
961148385Sume	/*
962151539Ssuz	 * Make the address tentative before joining multicast addresses,
963151539Ssuz	 * so that corresponding MLD responses would not have a tentative
964151539Ssuz	 * source address.
965148385Sume	 */
966151539Ssuz	ia->ia6_flags &= ~IN6_IFF_DUPLICATED;	/* safety */
967151539Ssuz	if (hostIsNew && in6if_do_dad(ifp))
968148385Sume		ia->ia6_flags |= IN6_IFF_TENTATIVE;
969148385Sume
970197138Shrs	/* DAD should be performed after ND6_IFF_IFDISABLED is cleared. */
971197138Shrs	if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
972197138Shrs		ia->ia6_flags |= IN6_IFF_TENTATIVE;
973197138Shrs
974148385Sume	/*
975148385Sume	 * We are done if we have simply modified an existing address.
976148385Sume	 */
977148385Sume	if (!hostIsNew)
978148385Sume		return (error);
979148385Sume
980148385Sume	/*
98178064Sume	 * Beyond this point, we should call in6_purgeaddr upon an error,
982120891Sume	 * not just go to unlink.
98378064Sume	 */
98478064Sume
985151465Ssuz	/* Join necessary multicast groups */
986151539Ssuz	in6m_sol = NULL;
98778064Sume	if ((ifp->if_flags & IFF_MULTICAST) != 0) {
98878064Sume		struct sockaddr_in6 mltaddr, mltmask;
989148385Sume		struct in6_addr llsol;
99078064Sume
991148385Sume		/* join solicited multicast addr for new host id */
992148385Sume		bzero(&llsol, sizeof(struct in6_addr));
993151465Ssuz		llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
994148385Sume		llsol.s6_addr32[1] = 0;
995148385Sume		llsol.s6_addr32[2] = htonl(1);
996148385Sume		llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
997148385Sume		llsol.s6_addr8[12] = 0xff;
998148385Sume		if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
999148385Sume			/* XXX: should not happen */
1000148385Sume			log(LOG_ERR, "in6_update_ifa: "
1001148385Sume			    "in6_setscope failed\n");
1002148385Sume			goto cleanup;
100353541Sshin		}
1004151539Ssuz		delay = 0;
1005151539Ssuz		if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1006151539Ssuz			/*
1007151539Ssuz			 * We need a random delay for DAD on the address
1008151539Ssuz			 * being configured.  It also means delaying
1009151539Ssuz			 * transmission of the corresponding MLD report to
1010151539Ssuz			 * avoid report collision.
1011197138Shrs			 * [RFC 4861, Section 6.3.7]
1012151539Ssuz			 */
1013151539Ssuz			delay = arc4random() %
1014151539Ssuz			    (MAX_RTR_SOLICITATION_DELAY * hz);
1015151539Ssuz		}
1016151539Ssuz		imm = in6_joingroup(ifp, &llsol, &error, delay);
1017170202Sjinmei		if (imm == NULL) {
1018148385Sume			nd6log((LOG_WARNING,
1019148385Sume			    "in6_update_ifa: addmulti failed for "
1020148385Sume			    "%s on %s (errno=%d)\n",
1021165118Sbz			    ip6_sprintf(ip6buf, &llsol), if_name(ifp),
1022148385Sume			    error));
1023195102Srwatson			goto cleanup;
1024148385Sume		}
1025170202Sjinmei		LIST_INSERT_HEAD(&ia->ia6_memberships,
1026170202Sjinmei		    imm, i6mm_chain);
1027151539Ssuz		in6m_sol = imm->i6mm_maddr;
102853541Sshin
102978064Sume		bzero(&mltmask, sizeof(mltmask));
103078064Sume		mltmask.sin6_len = sizeof(struct sockaddr_in6);
103178064Sume		mltmask.sin6_family = AF_INET6;
103278064Sume		mltmask.sin6_addr = in6mask32;
1033151465Ssuz#define	MLTMASK_LEN  4	/* mltmask's masklen (=32bit=4octet) */
103453541Sshin
103553541Sshin		/*
103678064Sume		 * join link-local all-nodes address
103771207Sitojun		 */
103878064Sume		bzero(&mltaddr, sizeof(mltaddr));
103978064Sume		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
104078064Sume		mltaddr.sin6_family = AF_INET6;
104178064Sume		mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1042148385Sume		if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1043148385Sume		    0)
1044148385Sume			goto cleanup; /* XXX: should not fail */
104571207Sitojun
1046148385Sume		/*
1047148385Sume		 * XXX: do we really need this automatic routes?
1048148385Sume		 * We should probably reconsider this stuff.  Most applications
1049148385Sume		 * actually do not need the routes, since they usually specify
1050148385Sume		 * the outgoing interface.
1051148385Sume		 */
1052148385Sume		rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1053148385Sume		if (rt) {
1054174375Sjulian			/* XXX: only works in !SCOPEDROUTING case. */
1055148385Sume			if (memcmp(&mltaddr.sin6_addr,
1056148385Sume			    &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1057151465Ssuz			    MLTMASK_LEN)) {
1058148385Sume				RTFREE_LOCKED(rt);
1059148385Sume				rt = NULL;
1060148385Sume			}
1061148385Sume		}
1062148385Sume		if (!rt) {
1063148385Sume			error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1064148385Sume			    (struct sockaddr *)&ia->ia_addr,
1065186119Sqingli			    (struct sockaddr *)&mltmask, RTF_UP,
1066148385Sume			    (struct rtentry **)0);
1067148385Sume			if (error)
1068148385Sume				goto cleanup;
1069151539Ssuz		} else {
1070151539Ssuz			RTFREE_LOCKED(rt);
1071151539Ssuz		}
107278064Sume
1073151539Ssuz		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1074151539Ssuz		if (!imm) {
1075151539Ssuz			nd6log((LOG_WARNING,
1076151539Ssuz			    "in6_update_ifa: addmulti failed for "
1077151539Ssuz			    "%s on %s (errno=%d)\n",
1078165118Sbz			    ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1079151539Ssuz			    if_name(ifp), error));
1080151539Ssuz			goto cleanup;
1081151539Ssuz		}
1082170202Sjinmei		LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1083151539Ssuz
108471207Sitojun		/*
108578064Sume		 * join node information group address
108653541Sshin		 */
1087151539Ssuz		delay = 0;
1088151539Ssuz		if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1089151539Ssuz			/*
1090151539Ssuz			 * The spec doesn't say anything about delay for this
1091151539Ssuz			 * group, but the same logic should apply.
1092151539Ssuz			 */
1093151539Ssuz			delay = arc4random() %
1094151539Ssuz			    (MAX_RTR_SOLICITATION_DELAY * hz);
1095151539Ssuz		}
1096192895Sjamie		if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
1097151539Ssuz			imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
1098151539Ssuz			    delay); /* XXX jinmei */
1099151539Ssuz			if (!imm) {
1100151539Ssuz				nd6log((LOG_WARNING, "in6_update_ifa: "
1101151539Ssuz				    "addmulti failed for %s on %s "
1102151539Ssuz				    "(errno=%d)\n",
1103165118Sbz				    ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1104151539Ssuz				    if_name(ifp), error));
1105151539Ssuz				/* XXX not very fatal, go on... */
1106170202Sjinmei			} else {
1107170202Sjinmei				LIST_INSERT_HEAD(&ia->ia6_memberships,
1108170202Sjinmei				    imm, i6mm_chain);
110962587Sitojun			}
1110192895Sjamie		}
111153541Sshin
111278064Sume		/*
1113148385Sume		 * join interface-local all-nodes address.
1114148385Sume		 * (ff01::1%ifN, and ff01::%ifN/32)
111578064Sume		 */
1116148385Sume		mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1117148385Sume		if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL))
1118148385Sume		    != 0)
1119148385Sume			goto cleanup; /* XXX: should not fail */
1120148385Sume		/* XXX: again, do we really need the route? */
1121148385Sume		rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1122148385Sume		if (rt) {
1123148385Sume			if (memcmp(&mltaddr.sin6_addr,
1124148385Sume			    &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1125151465Ssuz			    MLTMASK_LEN)) {
1126148385Sume				RTFREE_LOCKED(rt);
1127148385Sume				rt = NULL;
1128148385Sume			}
1129148385Sume		}
1130148385Sume		if (!rt) {
1131148385Sume			error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1132148385Sume			    (struct sockaddr *)&ia->ia_addr,
1133186119Sqingli			    (struct sockaddr *)&mltmask, RTF_UP,
1134148385Sume			    (struct rtentry **)0);
1135148385Sume			if (error)
1136148385Sume				goto cleanup;
1137148385Sume		} else
1138148385Sume			RTFREE_LOCKED(rt);
113981115Sume
1140151539Ssuz		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1141151539Ssuz		if (!imm) {
1142151539Ssuz			nd6log((LOG_WARNING, "in6_update_ifa: "
1143151539Ssuz			    "addmulti failed for %s on %s "
1144151539Ssuz			    "(errno=%d)\n",
1145165118Sbz			    ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1146151539Ssuz			    if_name(ifp), error));
1147151539Ssuz			goto cleanup;
1148151539Ssuz		}
1149170202Sjinmei		LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1150151465Ssuz#undef	MLTMASK_LEN
115178064Sume	}
115253541Sshin
1153151539Ssuz	/*
1154151539Ssuz	 * Perform DAD, if needed.
1155151539Ssuz	 * XXX It may be of use, if we can administratively
1156151539Ssuz	 * disable DAD.
1157151539Ssuz	 */
1158194760Srwatson	if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1159151539Ssuz	    (ia->ia6_flags & IN6_IFF_TENTATIVE))
1160151539Ssuz	{
1161151539Ssuz		int mindelay, maxdelay;
1162151539Ssuz
1163151539Ssuz		delay = 0;
1164151539Ssuz		if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1165151539Ssuz			/*
1166151539Ssuz			 * We need to impose a delay before sending an NS
1167151539Ssuz			 * for DAD.  Check if we also needed a delay for the
1168151539Ssuz			 * corresponding MLD message.  If we did, the delay
1169151539Ssuz			 * should be larger than the MLD delay (this could be
1170151539Ssuz			 * relaxed a bit, but this simple logic is at least
1171151539Ssuz			 * safe).
1172191672Sbms			 * XXX: Break data hiding guidelines and look at
1173191672Sbms			 * state for the solicited multicast group.
1174151539Ssuz			 */
1175151539Ssuz			mindelay = 0;
1176151539Ssuz			if (in6m_sol != NULL &&
1177191672Sbms			    in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1178151539Ssuz				mindelay = in6m_sol->in6m_timer;
1179151539Ssuz			}
1180151539Ssuz			maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1181151539Ssuz			if (maxdelay - mindelay == 0)
1182151539Ssuz				delay = 0;
1183151539Ssuz			else {
1184151539Ssuz				delay =
1185151539Ssuz				    (arc4random() % (maxdelay - mindelay)) +
1186151539Ssuz				    mindelay;
1187151539Ssuz			}
1188151539Ssuz		}
1189151539Ssuz		nd6_dad_start((struct ifaddr *)ia, delay);
1190151539Ssuz	}
1191151539Ssuz
1192194760Srwatson	KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew"));
1193194760Srwatson	ifa_free(&ia->ia_ifa);
1194120856Sume	return (error);
119578064Sume
119678064Sume  unlink:
119778064Sume	/*
119878064Sume	 * XXX: if a change of an existing address failed, keep the entry
119978064Sume	 * anyway.
120078064Sume	 */
1201194760Srwatson	if (hostIsNew) {
1202194943Srwatson		in6_unlink_ifa(ia, ifp);
1203194760Srwatson		ifa_free(&ia->ia_ifa);
1204194760Srwatson	}
1205120856Sume	return (error);
1206148385Sume
1207148385Sume  cleanup:
1208194760Srwatson	KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew"));
1209194760Srwatson	ifa_free(&ia->ia_ifa);
1210148385Sume	in6_purgeaddr(&ia->ia_ifa);
1211148385Sume	return error;
121253541Sshin}
121353541Sshin
121462587Sitojunvoid
1215171259Sdelphijin6_purgeaddr(struct ifaddr *ifa)
121662587Sitojun{
121778064Sume	struct ifnet *ifp = ifa->ifa_ifp;
121878064Sume	struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1219170202Sjinmei	struct in6_multi_mship *imm;
1220192282Sqingli	struct sockaddr_in6 mltaddr, mltmask;
1221192282Sqingli	struct rtentry rt0;
1222192282Sqingli	struct sockaddr_dl gateway;
1223192282Sqingli	struct sockaddr_in6 mask, addr;
1224192282Sqingli	int plen, error;
1225192282Sqingli	struct rtentry *rt;
1226192282Sqingli	struct ifaddr *ifa0, *nifa;
122762587Sitojun
1228192282Sqingli	/*
1229192282Sqingli	 * find another IPv6 address as the gateway for the
1230192282Sqingli	 * link-local and node-local all-nodes multicast
1231192282Sqingli	 * address routes
1232192282Sqingli	 */
1233194760Srwatson	IF_ADDR_LOCK(ifp);
1234192282Sqingli	TAILQ_FOREACH_SAFE(ifa0, &ifp->if_addrhead, ifa_link, nifa) {
1235192282Sqingli		if ((ifa0->ifa_addr->sa_family != AF_INET6) ||
1236192282Sqingli		    memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr,
1237192282Sqingli			   &ia->ia_addr.sin6_addr,
1238192282Sqingli			   sizeof(struct in6_addr)) == 0)
1239192282Sqingli			continue;
1240192282Sqingli		else
1241192282Sqingli			break;
1242192282Sqingli	}
1243194760Srwatson	if (ifa0 != NULL)
1244194760Srwatson		ifa_ref(ifa0);
1245194760Srwatson	IF_ADDR_UNLOCK(ifp);
1246192282Sqingli
1247195914Sqingli	/*
1248195914Sqingli	 * Remove the loopback route to the interface address.
1249196871Sqingli	 * The check for the current setting of "nd6_useloopback"
1250196871Sqingli	 * is not needed.
1251195914Sqingli	 */
1252201282Sqingli	if (ia->ia_flags & IFA_RTSELF) {
1253201282Sqingli		error = ifa_del_loopback_route((struct ifaddr *)ia,
1254201282Sqingli				       (struct sockaddr *)&ia->ia_addr);
1255201282Sqingli		if (error == 0)
1256201282Sqingli			ia->ia_flags &= ~IFA_RTSELF;
1257201282Sqingli	}
1258195643Sqingli
125978064Sume	/* stop DAD processing */
126078064Sume	nd6_dad_stop(ifa);
126162587Sitojun
1262186119Sqingli	IF_AFDATA_LOCK(ifp);
1263186119Sqingli	lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR),
1264186119Sqingli	    (struct sockaddr *)&ia->ia_addr);
1265186119Sqingli	IF_AFDATA_UNLOCK(ifp);
1266192282Sqingli
126778064Sume	/*
1268192282Sqingli	 * initialize for rtmsg generation
1269192282Sqingli	 */
1270192282Sqingli	bzero(&gateway, sizeof(gateway));
1271192282Sqingli	gateway.sdl_len = sizeof(gateway);
1272192282Sqingli	gateway.sdl_family = AF_LINK;
1273192282Sqingli	gateway.sdl_nlen = 0;
1274192282Sqingli	gateway.sdl_alen = ifp->if_addrlen;
1275192282Sqingli	/* */
1276192282Sqingli	bzero(&rt0, sizeof(rt0));
1277192282Sqingli	rt0.rt_gateway = (struct sockaddr *)&gateway;
1278192282Sqingli	memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
1279192282Sqingli	memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
1280192282Sqingli	rt_mask(&rt0) = (struct sockaddr *)&mask;
1281192282Sqingli	rt_key(&rt0) = (struct sockaddr *)&addr;
1282192282Sqingli	rt0.rt_flags = RTF_HOST | RTF_STATIC;
1283192282Sqingli	rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0);
1284192282Sqingli
1285192282Sqingli	/*
1286170202Sjinmei	 * leave from multicast groups we have joined for the interface
1287170202Sjinmei	 */
1288170202Sjinmei	while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1289170202Sjinmei		LIST_REMOVE(imm, i6mm_chain);
1290170202Sjinmei		in6_leavegroup(imm);
129162587Sitojun	}
129262587Sitojun
1293192282Sqingli	/*
1294192282Sqingli	 * remove the link-local all-nodes address
1295192282Sqingli	 */
1296192282Sqingli	bzero(&mltmask, sizeof(mltmask));
1297192282Sqingli	mltmask.sin6_len = sizeof(struct sockaddr_in6);
1298192282Sqingli	mltmask.sin6_family = AF_INET6;
1299192282Sqingli	mltmask.sin6_addr = in6mask32;
1300192282Sqingli
1301192282Sqingli	bzero(&mltaddr, sizeof(mltaddr));
1302192282Sqingli	mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1303192282Sqingli	mltaddr.sin6_family = AF_INET6;
1304192282Sqingli	mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1305192282Sqingli
1306192282Sqingli	if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1307192282Sqingli	    0)
1308192282Sqingli		goto cleanup;
1309192282Sqingli
1310192282Sqingli	rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1311192282Sqingli	if (rt != NULL && rt->rt_gateway != NULL &&
1312192282Sqingli	    (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1313192282Sqingli		    &ia->ia_addr.sin6_addr,
1314192282Sqingli		    sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1315192282Sqingli		/*
1316192282Sqingli		 * if no more IPv6 address exists on this interface
1317192282Sqingli		 * then remove the multicast address route
1318192282Sqingli		 */
1319192282Sqingli		if (ifa0 == NULL) {
1320192282Sqingli			memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1321192282Sqingli			       sizeof(mltaddr.sin6_addr));
1322192282Sqingli			RTFREE_LOCKED(rt);
1323192282Sqingli			error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr,
1324192282Sqingli					  (struct sockaddr *)&ia->ia_addr,
1325192282Sqingli					  (struct sockaddr *)&mltmask, RTF_UP,
1326192282Sqingli					  (struct rtentry **)0);
1327192282Sqingli			if (error)
1328192282Sqingli				log(LOG_INFO, "in6_purgeaddr: link-local all-nodes"
1329192282Sqingli				    "multicast address deletion error\n");
1330192282Sqingli		} else {
1331192282Sqingli			/*
1332192282Sqingli			 * replace the gateway of the route
1333192282Sqingli			 */
1334192282Sqingli			struct sockaddr_in6 sa;
1335192282Sqingli
1336192282Sqingli			bzero(&sa, sizeof(sa));
1337192282Sqingli			sa.sin6_len = sizeof(struct sockaddr_in6);
1338192282Sqingli			sa.sin6_family = AF_INET6;
1339192282Sqingli			memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1340192282Sqingli			       sizeof(sa.sin6_addr));
1341192282Sqingli			in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1342192282Sqingli			memcpy(rt->rt_gateway, &sa, sizeof(sa));
1343192282Sqingli			RTFREE_LOCKED(rt);
1344192282Sqingli		}
1345192282Sqingli	} else {
1346192282Sqingli		if (rt != NULL)
1347192282Sqingli			RTFREE_LOCKED(rt);
1348192282Sqingli	}
1349192282Sqingli
1350192282Sqingli	/*
1351192282Sqingli	 * remove the node-local all-nodes address
1352192282Sqingli	 */
1353192282Sqingli	mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1354192282Sqingli	if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1355192282Sqingli	    0)
1356192282Sqingli		goto cleanup;
1357192282Sqingli
1358192282Sqingli	rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1359192282Sqingli	if (rt != NULL && rt->rt_gateway != NULL &&
1360192282Sqingli	    (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1361192282Sqingli		    &ia->ia_addr.sin6_addr,
1362192282Sqingli		    sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1363192282Sqingli		/*
1364192282Sqingli		 * if no more IPv6 address exists on this interface
1365192282Sqingli		 * then remove the multicast address route
1366192282Sqingli		 */
1367192282Sqingli		if (ifa0 == NULL) {
1368192282Sqingli			memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1369192282Sqingli			       sizeof(mltaddr.sin6_addr));
1370192282Sqingli
1371192282Sqingli			RTFREE_LOCKED(rt);
1372192282Sqingli			error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr,
1373192282Sqingli					  (struct sockaddr *)&ia->ia_addr,
1374192282Sqingli					  (struct sockaddr *)&mltmask, RTF_UP,
1375192282Sqingli					  (struct rtentry **)0);
1376192282Sqingli
1377192282Sqingli			if (error)
1378192282Sqingli				log(LOG_INFO, "in6_purgeaddr: node-local all-nodes"
1379192282Sqingli				    "multicast address deletion error\n");
1380192282Sqingli		} else {
1381192282Sqingli			/*
1382192282Sqingli			 * replace the gateway of the route
1383192282Sqingli			 */
1384192282Sqingli			struct sockaddr_in6 sa;
1385192282Sqingli
1386192282Sqingli			bzero(&sa, sizeof(sa));
1387192282Sqingli			sa.sin6_len = sizeof(struct sockaddr_in6);
1388192282Sqingli			sa.sin6_family = AF_INET6;
1389192282Sqingli			memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1390192282Sqingli			       sizeof(sa.sin6_addr));
1391192282Sqingli			in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1392192282Sqingli			memcpy(rt->rt_gateway, &sa, sizeof(sa));
1393192282Sqingli			RTFREE_LOCKED(rt);
1394192282Sqingli		}
1395192282Sqingli	} else {
1396192282Sqingli		if (rt != NULL)
1397192282Sqingli			RTFREE_LOCKED(rt);
1398192282Sqingli	}
1399192282Sqingli
1400192282Sqinglicleanup:
1401192282Sqingli
1402192282Sqingli	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1403192282Sqingli	if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1404192282Sqingli		int error;
1405192282Sqingli		struct sockaddr *dstaddr;
1406192282Sqingli
1407192282Sqingli		/*
1408192282Sqingli		 * use the interface address if configuring an
1409192282Sqingli		 * interface address with a /128 prefix len
1410192282Sqingli		 */
1411192282Sqingli		if (ia->ia_dstaddr.sin6_family == AF_INET6)
1412192282Sqingli			dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
1413192282Sqingli		else
1414192282Sqingli			dstaddr = (struct sockaddr *)&ia->ia_addr;
1415192282Sqingli
1416192282Sqingli		error = rtrequest(RTM_DELETE,
1417192282Sqingli		    (struct sockaddr *)dstaddr,
1418192282Sqingli		    (struct sockaddr *)&ia->ia_addr,
1419192282Sqingli		    (struct sockaddr *)&ia->ia_prefixmask,
1420192282Sqingli		    ia->ia_flags | RTF_HOST, NULL);
1421192282Sqingli		if (error != 0)
1422192282Sqingli			return;
1423192282Sqingli		ia->ia_flags &= ~IFA_ROUTE;
1424192282Sqingli	}
1425194760Srwatson	if (ifa0 != NULL)
1426194760Srwatson		ifa_free(ifa0);
1427192282Sqingli
142878064Sume	in6_unlink_ifa(ia, ifp);
142978064Sume}
143078064Sume
143178064Sumestatic void
1432171259Sdelphijin6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
143378064Sume{
143478064Sume	int	s = splnet();
143578064Sume
1436191323Srwatson	IF_ADDR_LOCK(ifp);
1437191340Srwatson	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1438191323Srwatson	IF_ADDR_UNLOCK(ifp);
1439194760Srwatson	ifa_free(&ia->ia_ifa);			/* if_addrhead */
144062587Sitojun
1441195102Srwatson	/*
1442195102Srwatson	 * Defer the release of what might be the last reference to the
1443195102Srwatson	 * in6_ifaddr so that it can't be freed before the remainder of the
1444195102Srwatson	 * cleanup.
1445195102Srwatson	 */
1446194971Srwatson	IN6_IFADDR_WLOCK();
1447194907Srwatson	TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1448194971Srwatson	IN6_IFADDR_WUNLOCK();
144962587Sitojun
145062587Sitojun	/*
1451151915Ssuz	 * Release the reference to the base prefix.  There should be a
1452151915Ssuz	 * positive reference.
145362587Sitojun	 */
1454194907Srwatson	if (ia->ia6_ndpr == NULL) {
1455151915Ssuz		nd6log((LOG_NOTICE,
1456151915Ssuz		    "in6_unlink_ifa: autoconf'ed address "
1457194907Srwatson		    "%p has no prefix\n", ia));
1458151915Ssuz	} else {
1459194907Srwatson		ia->ia6_ndpr->ndpr_refcnt--;
1460194907Srwatson		ia->ia6_ndpr = NULL;
1461151915Ssuz	}
146262587Sitojun
1463151915Ssuz	/*
1464151915Ssuz	 * Also, if the address being removed is autoconf'ed, call
1465151915Ssuz	 * pfxlist_onlink_check() since the release might affect the status of
1466171260Sdelphij	 * other (detached) addresses.
1467151915Ssuz	 */
1468194907Srwatson	if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
146978064Sume		pfxlist_onlink_check();
147062587Sitojun	}
1471195102Srwatson	ifa_free(&ia->ia_ifa);			/* in6_ifaddrhead */
147278064Sume	splx(s);
147362587Sitojun}
147462587Sitojun
147578064Sumevoid
1476171259Sdelphijin6_purgeif(struct ifnet *ifp)
147778064Sume{
147878064Sume	struct ifaddr *ifa, *nifa;
147978064Sume
1480191340Srwatson	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
148178064Sume		if (ifa->ifa_addr->sa_family != AF_INET6)
148278064Sume			continue;
148378064Sume		in6_purgeaddr(ifa);
148478064Sume	}
148578064Sume
148678064Sume	in6_ifdetach(ifp);
148778064Sume}
148878064Sume
148953541Sshin/*
149053541Sshin * SIOC[GAD]LIFADDR.
149162744Sgrog *	SIOCGLIFADDR: get first address. (?)
149253541Sshin *	SIOCGLIFADDR with IFLR_PREFIX:
149353541Sshin *		get first address that matches the specified prefix.
149453541Sshin *	SIOCALIFADDR: add the specified address.
149553541Sshin *	SIOCALIFADDR with IFLR_PREFIX:
149653541Sshin *		add the specified prefix, filling hostid part from
149753541Sshin *		the first link-local address.  prefixlen must be <= 64.
149853541Sshin *	SIOCDLIFADDR: delete the specified address.
149953541Sshin *	SIOCDLIFADDR with IFLR_PREFIX:
150053541Sshin *		delete the first address that matches the specified prefix.
150153541Sshin * return values:
150253541Sshin *	EINVAL on invalid parameters
150353541Sshin *	EADDRNOTAVAIL on prefix match failed/specified address not found
150453541Sshin *	other values may be returned from in6_ioctl()
150553541Sshin *
150653541Sshin * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
150753541Sshin * this is to accomodate address naming scheme other than RFC2374,
150853541Sshin * in the future.
150953541Sshin * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
151053541Sshin * address encoding scheme. (see figure on page 8)
151153541Sshin */
151253541Sshinstatic int
1513171259Sdelphijin6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1514171259Sdelphij    struct ifnet *ifp, struct thread *td)
151553541Sshin{
151653541Sshin	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
151753541Sshin	struct ifaddr *ifa;
151862587Sitojun	struct sockaddr *sa;
151953541Sshin
152053541Sshin	/* sanity checks */
152153541Sshin	if (!data || !ifp) {
152253541Sshin		panic("invalid argument to in6_lifaddr_ioctl");
1523120891Sume		/* NOTREACHED */
152453541Sshin	}
152553541Sshin
152653541Sshin	switch (cmd) {
152753541Sshin	case SIOCGLIFADDR:
152853541Sshin		/* address must be specified on GET with IFLR_PREFIX */
152953541Sshin		if ((iflr->flags & IFLR_PREFIX) == 0)
153053541Sshin			break;
153195023Ssuz		/* FALLTHROUGH */
153253541Sshin	case SIOCALIFADDR:
153353541Sshin	case SIOCDLIFADDR:
153453541Sshin		/* address must be specified on ADD and DELETE */
153562587Sitojun		sa = (struct sockaddr *)&iflr->addr;
153662587Sitojun		if (sa->sa_family != AF_INET6)
153753541Sshin			return EINVAL;
153862587Sitojun		if (sa->sa_len != sizeof(struct sockaddr_in6))
153953541Sshin			return EINVAL;
154053541Sshin		/* XXX need improvement */
154162587Sitojun		sa = (struct sockaddr *)&iflr->dstaddr;
154262587Sitojun		if (sa->sa_family && sa->sa_family != AF_INET6)
154353541Sshin			return EINVAL;
154462587Sitojun		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
154553541Sshin			return EINVAL;
154653541Sshin		break;
154795023Ssuz	default: /* shouldn't happen */
154862587Sitojun#if 0
154962587Sitojun		panic("invalid cmd to in6_lifaddr_ioctl");
155095023Ssuz		/* NOTREACHED */
155162587Sitojun#else
155253541Sshin		return EOPNOTSUPP;
155362587Sitojun#endif
155453541Sshin	}
155553541Sshin	if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
155653541Sshin		return EINVAL;
155753541Sshin
155853541Sshin	switch (cmd) {
155953541Sshin	case SIOCALIFADDR:
156053541Sshin	    {
156153541Sshin		struct in6_aliasreq ifra;
156253541Sshin		struct in6_addr *hostid = NULL;
156353541Sshin		int prefixlen;
156453541Sshin
1565194760Srwatson		ifa = NULL;
156653541Sshin		if ((iflr->flags & IFLR_PREFIX) != 0) {
156753541Sshin			struct sockaddr_in6 *sin6;
156853541Sshin
156953541Sshin			/*
157053541Sshin			 * hostid is to fill in the hostid part of the
157153541Sshin			 * address.  hostid points to the first link-local
157253541Sshin			 * address attached to the interface.
157353541Sshin			 */
157462587Sitojun			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
157553541Sshin			if (!ifa)
157653541Sshin				return EADDRNOTAVAIL;
157753541Sshin			hostid = IFA_IN6(ifa);
157853541Sshin
1579171260Sdelphij			/* prefixlen must be <= 64. */
158053541Sshin			if (64 < iflr->prefixlen)
158153541Sshin				return EINVAL;
158253541Sshin			prefixlen = iflr->prefixlen;
158353541Sshin
158453541Sshin			/* hostid part must be zero. */
158553541Sshin			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1586126552Sume			if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1587126552Sume			    sin6->sin6_addr.s6_addr32[3] != 0) {
158853541Sshin				return EINVAL;
158953541Sshin			}
159053541Sshin		} else
159153541Sshin			prefixlen = iflr->prefixlen;
159253541Sshin
159353541Sshin		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
159453541Sshin		bzero(&ifra, sizeof(ifra));
1595120891Sume		bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
159653541Sshin
159762587Sitojun		bcopy(&iflr->addr, &ifra.ifra_addr,
1598120891Sume		    ((struct sockaddr *)&iflr->addr)->sa_len);
159953541Sshin		if (hostid) {
160053541Sshin			/* fill in hostid part */
160153541Sshin			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1602120891Sume			    hostid->s6_addr32[2];
160353541Sshin			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1604120891Sume			    hostid->s6_addr32[3];
160553541Sshin		}
160653541Sshin
1607120891Sume		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
160853541Sshin			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1609120891Sume			    ((struct sockaddr *)&iflr->dstaddr)->sa_len);
161053541Sshin			if (hostid) {
161153541Sshin				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1612120891Sume				    hostid->s6_addr32[2];
161353541Sshin				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1614120891Sume				    hostid->s6_addr32[3];
161553541Sshin			}
161653541Sshin		}
1617194760Srwatson		if (ifa != NULL)
1618194760Srwatson			ifa_free(ifa);
161953541Sshin
162053541Sshin		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1621121168Sume		in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
162253541Sshin
162353541Sshin		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
162483366Sjulian		return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
162553541Sshin	    }
162653541Sshin	case SIOCGLIFADDR:
162753541Sshin	case SIOCDLIFADDR:
162853541Sshin	    {
162953541Sshin		struct in6_ifaddr *ia;
163053541Sshin		struct in6_addr mask, candidate, match;
163153541Sshin		struct sockaddr_in6 *sin6;
163253541Sshin		int cmp;
163353541Sshin
163453541Sshin		bzero(&mask, sizeof(mask));
163553541Sshin		if (iflr->flags & IFLR_PREFIX) {
163653541Sshin			/* lookup a prefix rather than address. */
1637121168Sume			in6_prefixlen2mask(&mask, iflr->prefixlen);
163853541Sshin
163953541Sshin			sin6 = (struct sockaddr_in6 *)&iflr->addr;
164053541Sshin			bcopy(&sin6->sin6_addr, &match, sizeof(match));
164153541Sshin			match.s6_addr32[0] &= mask.s6_addr32[0];
164253541Sshin			match.s6_addr32[1] &= mask.s6_addr32[1];
164353541Sshin			match.s6_addr32[2] &= mask.s6_addr32[2];
164453541Sshin			match.s6_addr32[3] &= mask.s6_addr32[3];
164553541Sshin
164653541Sshin			/* if you set extra bits, that's wrong */
164753541Sshin			if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
164853541Sshin				return EINVAL;
164953541Sshin
165053541Sshin			cmp = 1;
165153541Sshin		} else {
165253541Sshin			if (cmd == SIOCGLIFADDR) {
165353541Sshin				/* on getting an address, take the 1st match */
165495023Ssuz				cmp = 0;	/* XXX */
165553541Sshin			} else {
165653541Sshin				/* on deleting an address, do exact match */
1657121168Sume				in6_prefixlen2mask(&mask, 128);
165853541Sshin				sin6 = (struct sockaddr_in6 *)&iflr->addr;
165953541Sshin				bcopy(&sin6->sin6_addr, &match, sizeof(match));
166053541Sshin
166153541Sshin				cmp = 1;
166253541Sshin			}
166353541Sshin		}
166453541Sshin
1665191323Srwatson		IF_ADDR_LOCK(ifp);
1666191340Srwatson		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
166753541Sshin			if (ifa->ifa_addr->sa_family != AF_INET6)
166853541Sshin				continue;
166953541Sshin			if (!cmp)
167053541Sshin				break;
167178064Sume
167278064Sume			/*
167378064Sume			 * XXX: this is adhoc, but is necessary to allow
167478064Sume			 * a user to specify fe80::/64 (not /10) for a
167578064Sume			 * link-local address.
167678064Sume			 */
1677148385Sume			bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1678148385Sume			in6_clearscope(&candidate);
167953541Sshin			candidate.s6_addr32[0] &= mask.s6_addr32[0];
168053541Sshin			candidate.s6_addr32[1] &= mask.s6_addr32[1];
168153541Sshin			candidate.s6_addr32[2] &= mask.s6_addr32[2];
168253541Sshin			candidate.s6_addr32[3] &= mask.s6_addr32[3];
168353541Sshin			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
168453541Sshin				break;
168553541Sshin		}
1686191323Srwatson		IF_ADDR_UNLOCK(ifp);
168753541Sshin		if (!ifa)
168853541Sshin			return EADDRNOTAVAIL;
168953541Sshin		ia = ifa2ia6(ifa);
169053541Sshin
169153541Sshin		if (cmd == SIOCGLIFADDR) {
1692148385Sume			int error;
169378064Sume
169453541Sshin			/* fill in the if_laddrreq structure */
169553541Sshin			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1696148385Sume			error = sa6_recoverscope(
1697148385Sume			    (struct sockaddr_in6 *)&iflr->addr);
1698148385Sume			if (error != 0)
1699148385Sume				return (error);
1700148385Sume
170153541Sshin			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
170253541Sshin				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1703120891Sume				    ia->ia_dstaddr.sin6_len);
1704148385Sume				error = sa6_recoverscope(
1705148385Sume				    (struct sockaddr_in6 *)&iflr->dstaddr);
1706148385Sume				if (error != 0)
1707148385Sume					return (error);
170853541Sshin			} else
170953541Sshin				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
171053541Sshin
171153541Sshin			iflr->prefixlen =
1712120891Sume			    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
171353541Sshin
171495023Ssuz			iflr->flags = ia->ia6_flags;	/* XXX */
171553541Sshin
171653541Sshin			return 0;
171753541Sshin		} else {
171853541Sshin			struct in6_aliasreq ifra;
171953541Sshin
172053541Sshin			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
172153541Sshin			bzero(&ifra, sizeof(ifra));
172253541Sshin			bcopy(iflr->iflr_name, ifra.ifra_name,
1723120891Sume			    sizeof(ifra.ifra_name));
172453541Sshin
172553541Sshin			bcopy(&ia->ia_addr, &ifra.ifra_addr,
1726120891Sume			    ia->ia_addr.sin6_len);
172753541Sshin			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
172853541Sshin				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1729120891Sume				    ia->ia_dstaddr.sin6_len);
173062587Sitojun			} else {
173162587Sitojun				bzero(&ifra.ifra_dstaddr,
173262587Sitojun				    sizeof(ifra.ifra_dstaddr));
173353541Sshin			}
173453541Sshin			bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1735120891Sume			    ia->ia_prefixmask.sin6_len);
173653541Sshin
173753541Sshin			ifra.ifra_flags = ia->ia6_flags;
173853541Sshin			return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1739120891Sume			    ifp, td);
174053541Sshin		}
174153541Sshin	    }
174253541Sshin	}
174353541Sshin
174495023Ssuz	return EOPNOTSUPP;	/* just for safety */
174553541Sshin}
174653541Sshin
174753541Sshin/*
174853541Sshin * Initialize an interface's intetnet6 address
174953541Sshin * and routing table entry.
175053541Sshin */
175178064Sumestatic int
1752171259Sdelphijin6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1753171259Sdelphij    struct sockaddr_in6 *sin6, int newhost)
175453541Sshin{
175578064Sume	int	error = 0, plen, ifacount = 0;
175653541Sshin	int	s = splimp();
175778064Sume	struct ifaddr *ifa;
175853541Sshin
175953541Sshin	/*
176053541Sshin	 * Give the interface a chance to initialize
176153541Sshin	 * if this is its first address,
176253541Sshin	 * and to validate the address if necessary.
176353541Sshin	 */
1764191323Srwatson	IF_ADDR_LOCK(ifp);
1765191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
176678064Sume		if (ifa->ifa_addr->sa_family != AF_INET6)
176778064Sume			continue;
176878064Sume		ifacount++;
176978064Sume	}
1770191323Srwatson	IF_ADDR_UNLOCK(ifp);
177178064Sume
177278064Sume	ia->ia_addr = *sin6;
177378064Sume
1774146883Siedowse	if (ifacount <= 1 && ifp->if_ioctl) {
1775146883Siedowse		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1776146883Siedowse		if (error) {
1777146883Siedowse			splx(s);
1778146883Siedowse			return (error);
1779146883Siedowse		}
178053541Sshin	}
178178064Sume	splx(s);
178253541Sshin
178378064Sume	ia->ia_ifa.ifa_metric = ifp->if_metric;
178453541Sshin
178578064Sume	/* we could do in(6)_socktrim here, but just omit it at this moment. */
178678064Sume
178753541Sshin	/*
178878064Sume	 * Special case:
1789124337Sume	 * If a new destination address is specified for a point-to-point
179078064Sume	 * interface, install a route to the destination as an interface
1791186119Sqingli	 * direct route.
1792124337Sume	 * XXX: the logic below rejects assigning multiple addresses on a p2p
1793159390Sgnn	 * interface that share the same destination.
179453541Sshin	 */
179578064Sume	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1796196152Sqingli	if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1797196152Sqingli	    ia->ia_dstaddr.sin6_family == AF_INET6) {
1798159390Sgnn		int rtflags = RTF_UP | RTF_HOST;
1799159390Sgnn
1800178888Sjulian		error = rtrequest(RTM_ADD,
1801196152Sqingli		    (struct sockaddr *)&ia->ia_dstaddr,
1802159390Sgnn		    (struct sockaddr *)&ia->ia_addr,
1803159390Sgnn		    (struct sockaddr *)&ia->ia_prefixmask,
1804186708Sqingli		    ia->ia_flags | rtflags, NULL);
1805159390Sgnn		if (error != 0)
1806120856Sume			return (error);
180778064Sume		ia->ia_flags |= IFA_ROUTE;
180853541Sshin	}
180953541Sshin
1810195643Sqingli	/*
1811195643Sqingli	 * add a loopback route to self
1812195643Sqingli	 */
1813196871Sqingli	if (!(ia->ia_flags & IFA_ROUTE)
1814196871Sqingli	    && (V_nd6_useloopback
1815196871Sqingli		|| (ifp->if_flags & IFF_LOOPBACK))) {
1816197227Sqingli		error = ifa_add_loopback_route((struct ifaddr *)ia,
1817197227Sqingli				       (struct sockaddr *)&ia->ia_addr);
1818201282Sqingli		if (error == 0)
1819201282Sqingli			ia->ia_flags |= IFA_RTSELF;
1820195643Sqingli	}
1821195643Sqingli
182295023Ssuz	/* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1823186119Sqingli	if (newhost) {
1824186119Sqingli		struct llentry *ln;
1825192282Sqingli		struct rtentry rt;
1826192282Sqingli		struct sockaddr_dl gateway;
1827192282Sqingli		struct sockaddr_in6 mask, addr;
182853541Sshin
1829186119Sqingli		IF_AFDATA_LOCK(ifp);
1830186119Sqingli		ia->ia_ifa.ifa_rtrequest = NULL;
1831186119Sqingli
1832186119Sqingli		/* XXX QL
1833186119Sqingli		 * we need to report rt_newaddrmsg
1834186119Sqingli		 */
1835186119Sqingli		ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR | LLE_EXCLUSIVE),
1836186119Sqingli		    (struct sockaddr *)&ia->ia_addr);
1837186119Sqingli		IF_AFDATA_UNLOCK(ifp);
1838186158Skmacy		if (ln != NULL) {
1839186119Sqingli			ln->la_expire = 0;  /* for IPv6 this means permanent */
1840186119Sqingli			ln->ln_state = ND6_LLINFO_REACHABLE;
1841192282Sqingli			/*
1842192282Sqingli			 * initialize for rtmsg generation
1843192282Sqingli			 */
1844192282Sqingli			bzero(&gateway, sizeof(gateway));
1845192282Sqingli			gateway.sdl_len = sizeof(gateway);
1846192282Sqingli			gateway.sdl_family = AF_LINK;
1847192282Sqingli			gateway.sdl_nlen = 0;
1848192282Sqingli			gateway.sdl_alen = 6;
1849192282Sqingli			memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned, sizeof(ln->ll_addr));
1850192282Sqingli			/* */
1851186119Sqingli			LLE_WUNLOCK(ln);
1852186119Sqingli		}
1853192282Sqingli
1854192282Sqingli		bzero(&rt, sizeof(rt));
1855192282Sqingli		rt.rt_gateway = (struct sockaddr *)&gateway;
1856192282Sqingli		memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
1857192282Sqingli		memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
1858192282Sqingli		rt_mask(&rt) = (struct sockaddr *)&mask;
1859192282Sqingli		rt_key(&rt) = (struct sockaddr *)&addr;
1860192282Sqingli		rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC;
1861192282Sqingli		rt_newaddrmsg(RTM_ADD, &ia->ia_ifa, 0, &rt);
1862186119Sqingli	}
1863186119Sqingli
1864120856Sume	return (error);
186553541Sshin}
186653541Sshin
186753541Sshin/*
186853541Sshin * Find an IPv6 interface link-local address specific to an interface.
1869194760Srwatson * ifaddr is returned referenced.
187053541Sshin */
187153541Sshinstruct in6_ifaddr *
1872171259Sdelphijin6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
187353541Sshin{
187478064Sume	struct ifaddr *ifa;
187553541Sshin
1876191323Srwatson	IF_ADDR_LOCK(ifp);
1877191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
187853541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
187953541Sshin			continue;
188062587Sitojun		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
188162587Sitojun			if ((((struct in6_ifaddr *)ifa)->ia6_flags &
188262587Sitojun			     ignoreflags) != 0)
188362587Sitojun				continue;
1884194760Srwatson			ifa_ref(ifa);
188553541Sshin			break;
188662587Sitojun		}
188753541Sshin	}
1888191323Srwatson	IF_ADDR_UNLOCK(ifp);
188953541Sshin
1890120856Sume	return ((struct in6_ifaddr *)ifa);
189153541Sshin}
189253541Sshin
189353541Sshin
189453541Sshin/*
189553541Sshin * find the internet address corresponding to a given interface and address.
1896194760Srwatson * ifaddr is returned referenced.
189753541Sshin */
189853541Sshinstruct in6_ifaddr *
1899171259Sdelphijin6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
190053541Sshin{
190178064Sume	struct ifaddr *ifa;
190253541Sshin
1903191337Srwatson	IF_ADDR_LOCK(ifp);
1904191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
190553541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
190653541Sshin			continue;
1907194760Srwatson		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1908194760Srwatson			ifa_ref(ifa);
190953541Sshin			break;
1910194760Srwatson		}
191153541Sshin	}
1912191337Srwatson	IF_ADDR_UNLOCK(ifp);
191353541Sshin
1914120856Sume	return ((struct in6_ifaddr *)ifa);
191553541Sshin}
191653541Sshin
191753541Sshin/*
1918165118Sbz * Convert IP6 address to printable (loggable) representation. Caller
1919165118Sbz * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
192053541Sshin */
192153541Sshinstatic char digits[] = "0123456789abcdef";
192253541Sshinchar *
1923165118Sbzip6_sprintf(char *ip6buf, const struct in6_addr *addr)
192453541Sshin{
1925208284Salfred	int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
192678064Sume	char *cp;
1927126552Sume	const u_int16_t *a = (const u_int16_t *)addr;
1928126552Sume	const u_int8_t *d;
1929165287Sbz	int dcolon = 0, zero = 0;
193053541Sshin
1931165118Sbz	cp = ip6buf;
193253541Sshin
193353541Sshin	for (i = 0; i < 8; i++) {
1934208284Salfred		if (*(a + i) == 0) {
1935208284Salfred			cnt++;
1936208284Salfred			if (cnt == 1)
1937208284Salfred				idx = i;
1938208284Salfred		}
1939208284Salfred		else if (maxcnt < cnt) {
1940208284Salfred			maxcnt = cnt;
1941208284Salfred			index = idx;
1942208284Salfred			cnt = 0;
1943208284Salfred		}
1944208284Salfred	}
1945208284Salfred	if (maxcnt < cnt) {
1946208284Salfred		maxcnt = cnt;
1947208284Salfred		index = idx;
1948208284Salfred	}
1949208284Salfred
1950208284Salfred	for (i = 0; i < 8; i++) {
195153541Sshin		if (dcolon == 1) {
195253541Sshin			if (*a == 0) {
195353541Sshin				if (i == 7)
195453541Sshin					*cp++ = ':';
195553541Sshin				a++;
195653541Sshin				continue;
195753541Sshin			} else
195853541Sshin				dcolon = 2;
195953541Sshin		}
196053541Sshin		if (*a == 0) {
1961208284Salfred			if (dcolon == 0 && *(a + 1) == 0 && i == index) {
196253541Sshin				if (i == 0)
196353541Sshin					*cp++ = ':';
196453541Sshin				*cp++ = ':';
196553541Sshin				dcolon = 1;
196653541Sshin			} else {
196753541Sshin				*cp++ = '0';
196853541Sshin				*cp++ = ':';
196953541Sshin			}
197053541Sshin			a++;
197153541Sshin			continue;
197253541Sshin		}
197391346Salfred		d = (const u_char *)a;
1974165287Sbz		/* Try to eliminate leading zeros in printout like in :0001. */
1975165287Sbz		zero = 1;
1976165287Sbz		*cp = digits[*d >> 4];
1977165287Sbz		if (*cp != '0') {
1978165287Sbz			zero = 0;
1979165287Sbz			cp++;
1980165287Sbz		}
1981165287Sbz		*cp = digits[*d++ & 0xf];
1982165287Sbz		if (zero == 0 || (*cp != '0')) {
1983165287Sbz			zero = 0;
1984165287Sbz			cp++;
1985165287Sbz		}
1986165287Sbz		*cp = digits[*d >> 4];
1987165287Sbz		if (zero == 0 || (*cp != '0')) {
1988165287Sbz			zero = 0;
1989165287Sbz			cp++;
1990165287Sbz		}
199153541Sshin		*cp++ = digits[*d & 0xf];
199253541Sshin		*cp++ = ':';
199353541Sshin		a++;
199453541Sshin	}
1995165118Sbz	*--cp = '\0';
1996165118Sbz	return (ip6buf);
199753541Sshin}
199853541Sshin
199953541Sshinint
2000171259Sdelphijin6_localaddr(struct in6_addr *in6)
200153541Sshin{
200253541Sshin	struct in6_ifaddr *ia;
200353541Sshin
200453541Sshin	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
200553541Sshin		return 1;
200653541Sshin
2007194971Srwatson	IN6_IFADDR_RLOCK();
2008194907Srwatson	TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
200953541Sshin		if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
2010120891Sume		    &ia->ia_prefixmask.sin6_addr)) {
2011194971Srwatson			IN6_IFADDR_RUNLOCK();
201253541Sshin			return 1;
2013120891Sume		}
2014120891Sume	}
2015194971Srwatson	IN6_IFADDR_RUNLOCK();
201653541Sshin
201753541Sshin	return (0);
201853541Sshin}
201953541Sshin
202078064Sumeint
2021171259Sdelphijin6_is_addr_deprecated(struct sockaddr_in6 *sa6)
202278064Sume{
202378064Sume	struct in6_ifaddr *ia;
202478064Sume
2025194971Srwatson	IN6_IFADDR_RLOCK();
2026194907Srwatson	TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
202778064Sume		if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
202878064Sume				       &sa6->sin6_addr) &&
2029194971Srwatson		    (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
2030194971Srwatson			IN6_IFADDR_RUNLOCK();
2031120856Sume			return (1); /* true */
2032194971Srwatson		}
203378064Sume
203478064Sume		/* XXX: do we still have to go thru the rest of the list? */
203578064Sume	}
2036194971Srwatson	IN6_IFADDR_RUNLOCK();
203778064Sume
2038120856Sume	return (0);		/* false */
203978064Sume}
204078064Sume
204153541Sshin/*
204253541Sshin * return length of part which dst and src are equal
204353541Sshin * hard coding...
204453541Sshin */
204553541Sshinint
2046171259Sdelphijin6_matchlen(struct in6_addr *src, struct in6_addr *dst)
204753541Sshin{
204853541Sshin	int match = 0;
204953541Sshin	u_char *s = (u_char *)src, *d = (u_char *)dst;
205053541Sshin	u_char *lim = s + 16, r;
205153541Sshin
205253541Sshin	while (s < lim)
205353541Sshin		if ((r = (*d++ ^ *s++)) != 0) {
205453541Sshin			while (r < 128) {
205553541Sshin				match++;
205653541Sshin				r <<= 1;
205753541Sshin			}
205853541Sshin			break;
205953541Sshin		} else
206053541Sshin			match += 8;
206153541Sshin	return match;
206253541Sshin}
206353541Sshin
206462587Sitojun/* XXX: to be scope conscious */
206553541Sshinint
2066171259Sdelphijin6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
206753541Sshin{
206853541Sshin	int bytelen, bitlen;
206953541Sshin
207053541Sshin	/* sanity check */
207153541Sshin	if (0 > len || len > 128) {
207253541Sshin		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
207353541Sshin		    len);
2074120856Sume		return (0);
207553541Sshin	}
207653541Sshin
207753541Sshin	bytelen = len / 8;
207853541Sshin	bitlen = len % 8;
207953541Sshin
208053541Sshin	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2081120856Sume		return (0);
2082126184Scperciva	if (bitlen != 0 &&
2083126184Scperciva	    p1->s6_addr[bytelen] >> (8 - bitlen) !=
208453541Sshin	    p2->s6_addr[bytelen] >> (8 - bitlen))
2085120856Sume		return (0);
208653541Sshin
2087120856Sume	return (1);
208853541Sshin}
208953541Sshin
209053541Sshinvoid
2091171259Sdelphijin6_prefixlen2mask(struct in6_addr *maskp, int len)
209253541Sshin{
209353541Sshin	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
209453541Sshin	int bytelen, bitlen, i;
209553541Sshin
209653541Sshin	/* sanity check */
209753541Sshin	if (0 > len || len > 128) {
209853541Sshin		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
209953541Sshin		    len);
210053541Sshin		return;
210153541Sshin	}
210253541Sshin
210353541Sshin	bzero(maskp, sizeof(*maskp));
210453541Sshin	bytelen = len / 8;
210553541Sshin	bitlen = len % 8;
210653541Sshin	for (i = 0; i < bytelen; i++)
210753541Sshin		maskp->s6_addr[i] = 0xff;
210853541Sshin	if (bitlen)
210953541Sshin		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
211053541Sshin}
211153541Sshin
211253541Sshin/*
211353541Sshin * return the best address out of the same scope. if no address was
211453541Sshin * found, return the first valid address from designated IF.
211553541Sshin */
211653541Sshinstruct in6_ifaddr *
2117171259Sdelphijin6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
211853541Sshin{
211953541Sshin	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
212053541Sshin	struct ifaddr *ifa;
212153541Sshin	struct in6_ifaddr *besta = 0;
212295023Ssuz	struct in6_ifaddr *dep[2];	/* last-resort: deprecated */
212353541Sshin
212453541Sshin	dep[0] = dep[1] = NULL;
212553541Sshin
212653541Sshin	/*
212753541Sshin	 * We first look for addresses in the same scope.
212853541Sshin	 * If there is one, return it.
212953541Sshin	 * If two or more, return one which matches the dst longest.
213053541Sshin	 * If none, return one of global addresses assigned other ifs.
213153541Sshin	 */
2132191323Srwatson	IF_ADDR_LOCK(ifp);
2133191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
213453541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
213553541Sshin			continue;
213653541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
213753541Sshin			continue; /* XXX: is there any case to allow anycast? */
213853541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
213953541Sshin			continue; /* don't use this interface */
214053541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
214153541Sshin			continue;
214253541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2143181803Sbz			if (V_ip6_use_deprecated)
214453541Sshin				dep[0] = (struct in6_ifaddr *)ifa;
214553541Sshin			continue;
214653541Sshin		}
214753541Sshin
214853541Sshin		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
214953541Sshin			/*
215053541Sshin			 * call in6_matchlen() as few as possible
215153541Sshin			 */
215253541Sshin			if (besta) {
215353541Sshin				if (blen == -1)
215453541Sshin					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
215553541Sshin				tlen = in6_matchlen(IFA_IN6(ifa), dst);
215653541Sshin				if (tlen > blen) {
215753541Sshin					blen = tlen;
215853541Sshin					besta = (struct in6_ifaddr *)ifa;
215953541Sshin				}
216053541Sshin			} else
216153541Sshin				besta = (struct in6_ifaddr *)ifa;
216253541Sshin		}
216353541Sshin	}
2164191323Srwatson	if (besta) {
2165194760Srwatson		ifa_ref(&besta->ia_ifa);
2166191323Srwatson		IF_ADDR_UNLOCK(ifp);
2167120856Sume		return (besta);
2168191323Srwatson	}
2169194971Srwatson	IF_ADDR_UNLOCK(ifp);
217053541Sshin
2171194971Srwatson	IN6_IFADDR_RLOCK();
2172191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
217353541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
217453541Sshin			continue;
217553541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
217653541Sshin			continue; /* XXX: is there any case to allow anycast? */
217753541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
217853541Sshin			continue; /* don't use this interface */
217953541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
218053541Sshin			continue;
218153541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2182181803Sbz			if (V_ip6_use_deprecated)
218353541Sshin				dep[1] = (struct in6_ifaddr *)ifa;
218453541Sshin			continue;
218553541Sshin		}
218653541Sshin
2187194760Srwatson		if (ifa != NULL)
2188194760Srwatson			ifa_ref(ifa);
2189194971Srwatson		IN6_IFADDR_RUNLOCK();
219053541Sshin		return (struct in6_ifaddr *)ifa;
219153541Sshin	}
2192194971Srwatson	IN6_IFADDR_RUNLOCK();
219353541Sshin
219453541Sshin	/* use the last-resort values, that are, deprecated addresses */
219553541Sshin	if (dep[0])
219653541Sshin		return dep[0];
219753541Sshin	if (dep[1])
219853541Sshin		return dep[1];
219953541Sshin
220053541Sshin	return NULL;
220153541Sshin}
220253541Sshin
220353541Sshin/*
220453541Sshin * perform DAD when interface becomes IFF_UP.
220553541Sshin */
220653541Sshinvoid
2207171259Sdelphijin6_if_up(struct ifnet *ifp)
220853541Sshin{
220953541Sshin	struct ifaddr *ifa;
221053541Sshin	struct in6_ifaddr *ia;
221153541Sshin
2212191323Srwatson	IF_ADDR_LOCK(ifp);
2213191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
221453541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
221553541Sshin			continue;
221653541Sshin		ia = (struct in6_ifaddr *)ifa;
2217151539Ssuz		if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2218151539Ssuz			/*
2219151539Ssuz			 * The TENTATIVE flag was likely set by hand
2220151539Ssuz			 * beforehand, implicitly indicating the need for DAD.
2221151539Ssuz			 * We may be able to skip the random delay in this
2222151539Ssuz			 * case, but we impose delays just in case.
2223151539Ssuz			 */
2224151539Ssuz			nd6_dad_start(ifa,
2225151539Ssuz			    arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2226151539Ssuz		}
222753541Sshin	}
2228191323Srwatson	IF_ADDR_UNLOCK(ifp);
2229151539Ssuz
2230151539Ssuz	/*
2231151539Ssuz	 * special cases, like 6to4, are handled in in6_ifattach
2232151539Ssuz	 */
2233151539Ssuz	in6_ifattach(ifp, NULL);
223453541Sshin}
223553541Sshin
223678064Sumeint
2237171259Sdelphijin6if_do_dad(struct ifnet *ifp)
223878064Sume{
223978064Sume	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2240120856Sume		return (0);
224178064Sume
2242197138Shrs	if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
2243197138Shrs		return (0);
2244197138Shrs
224578064Sume	switch (ifp->if_type) {
224678064Sume#ifdef IFT_DUMMY
224778064Sume	case IFT_DUMMY:
224878064Sume#endif
224978064Sume	case IFT_FAITH:
225078064Sume		/*
225178064Sume		 * These interfaces do not have the IFF_LOOPBACK flag,
225278064Sume		 * but loop packets back.  We do not have to do DAD on such
225378064Sume		 * interfaces.  We should even omit it, because loop-backed
225478064Sume		 * NS would confuse the DAD procedure.
225578064Sume		 */
2256120856Sume		return (0);
225778064Sume	default:
225878064Sume		/*
225978064Sume		 * Our DAD routine requires the interface up and running.
226078064Sume		 * However, some interfaces can be up before the RUNNING
226178064Sume		 * status.  Additionaly, users may try to assign addresses
226278064Sume		 * before the interface becomes up (or running).
226378064Sume		 * We simply skip DAD in such a case as a work around.
226478064Sume		 * XXX: we should rather mark "tentative" on such addresses,
226578064Sume		 * and do DAD after the interface becomes ready.
226678064Sume		 */
2267148887Srwatson		if (!((ifp->if_flags & IFF_UP) &&
2268148887Srwatson		    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2269120856Sume			return (0);
227078064Sume
2271120856Sume		return (1);
227278064Sume	}
227378064Sume}
227478064Sume
227553541Sshin/*
227653541Sshin * Calculate max IPv6 MTU through all the interfaces and store it
227753541Sshin * to in6_maxmtu.
227853541Sshin */
227953541Sshinvoid
2280171259Sdelphijin6_setmaxmtu(void)
228153541Sshin{
228253541Sshin	unsigned long maxmtu = 0;
228353541Sshin	struct ifnet *ifp;
228453541Sshin
2285196481Srwatson	IFNET_RLOCK_NOSLEEP();
2286181887Sjulian	for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
2287181888Sjulian	    ifp = TAILQ_NEXT(ifp, if_list)) {
2288121283Sume		/* this function can be called during ifnet initialization */
2289121283Sume		if (!ifp->if_afdata[AF_INET6])
2290121283Sume			continue;
229153541Sshin		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2292121283Sume		    IN6_LINKMTU(ifp) > maxmtu)
2293121283Sume			maxmtu = IN6_LINKMTU(ifp);
229453541Sshin	}
2295196481Srwatson	IFNET_RUNLOCK_NOSLEEP();
2296120891Sume	if (maxmtu)	     /* update only when maxmtu is positive */
2297181803Sbz		V_in6_maxmtu = maxmtu;
229853541Sshin}
229953541Sshin
2300151539Ssuz/*
2301151539Ssuz * Provide the length of interface identifiers to be used for the link attached
2302151539Ssuz * to the given interface.  The length should be defined in "IPv6 over
2303151539Ssuz * xxx-link" document.  Note that address architecture might also define
2304151539Ssuz * the length for a particular set of address prefixes, regardless of the
2305151539Ssuz * link type.  As clarified in rfc2462bis, those two definitions should be
2306151539Ssuz * consistent, and those really are as of August 2004.
2307151539Ssuz */
2308151539Ssuzint
2309171259Sdelphijin6_if2idlen(struct ifnet *ifp)
2310151539Ssuz{
2311151539Ssuz	switch (ifp->if_type) {
2312151539Ssuz	case IFT_ETHER:		/* RFC2464 */
2313151539Ssuz#ifdef IFT_PROPVIRTUAL
2314151539Ssuz	case IFT_PROPVIRTUAL:	/* XXX: no RFC. treat it as ether */
2315151539Ssuz#endif
2316151539Ssuz#ifdef IFT_L2VLAN
2317151539Ssuz	case IFT_L2VLAN:	/* ditto */
2318151539Ssuz#endif
2319151539Ssuz#ifdef IFT_IEEE80211
2320151539Ssuz	case IFT_IEEE80211:	/* ditto */
2321151539Ssuz#endif
2322151539Ssuz#ifdef IFT_MIP
2323151539Ssuz	case IFT_MIP:	/* ditto */
2324151539Ssuz#endif
2325219819Sjeff	case IFT_INFINIBAND:
2326151539Ssuz		return (64);
2327151539Ssuz	case IFT_FDDI:		/* RFC2467 */
2328151539Ssuz		return (64);
2329151539Ssuz	case IFT_ISO88025:	/* RFC2470 (IPv6 over Token Ring) */
2330151539Ssuz		return (64);
2331151539Ssuz	case IFT_PPP:		/* RFC2472 */
2332151539Ssuz		return (64);
2333151539Ssuz	case IFT_ARCNET:	/* RFC2497 */
2334151539Ssuz		return (64);
2335151539Ssuz	case IFT_FRELAY:	/* RFC2590 */
2336151539Ssuz		return (64);
2337151539Ssuz	case IFT_IEEE1394:	/* RFC3146 */
2338151539Ssuz		return (64);
2339151539Ssuz	case IFT_GIF:
2340151539Ssuz		return (64);	/* draft-ietf-v6ops-mech-v2-07 */
2341151539Ssuz	case IFT_LOOP:
2342151539Ssuz		return (64);	/* XXX: is this really correct? */
2343151539Ssuz	default:
2344151539Ssuz		/*
2345151539Ssuz		 * Unknown link type:
2346151539Ssuz		 * It might be controversial to use the today's common constant
2347151539Ssuz		 * of 64 for these cases unconditionally.  For full compliance,
2348151539Ssuz		 * we should return an error in this case.  On the other hand,
2349151539Ssuz		 * if we simply miss the standard for the link type or a new
2350151539Ssuz		 * standard is defined for a new link type, the IFID length
2351151539Ssuz		 * is very likely to be the common constant.  As a compromise,
2352151539Ssuz		 * we always use the constant, but make an explicit notice
2353151539Ssuz		 * indicating the "unknown" case.
2354151539Ssuz		 */
2355151539Ssuz		printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2356151539Ssuz		return (64);
2357151539Ssuz	}
2358151539Ssuz}
2359151539Ssuz
2360186119Sqingli#include <sys/sysctl.h>
2361186119Sqingli
2362186119Sqinglistruct in6_llentry {
2363186119Sqingli	struct llentry		base;
2364186119Sqingli	struct sockaddr_in6	l3_addr6;
2365186119Sqingli};
2366186119Sqingli
2367186119Sqinglistatic struct llentry *
2368186119Sqingliin6_lltable_new(const struct sockaddr *l3addr, u_int flags)
2369186119Sqingli{
2370186119Sqingli	struct in6_llentry *lle;
2371186119Sqingli
2372186119Sqingli	lle = malloc(sizeof(struct in6_llentry), M_LLTABLE,
2373186119Sqingli	    M_DONTWAIT | M_ZERO);
2374186119Sqingli	if (lle == NULL)		/* NB: caller generates msg */
2375186119Sqingli		return NULL;
2376186119Sqingli
2377186119Sqingli	lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr;
2378186119Sqingli	lle->base.lle_refcnt = 1;
2379186119Sqingli	LLE_LOCK_INIT(&lle->base);
2380216022Sbz	callout_init_rw(&lle->base.ln_timer_ch, &lle->base.lle_lock,
2381216022Sbz	    CALLOUT_RETURNUNLOCKED);
2382216022Sbz
2383186119Sqingli	return &lle->base;
2384186119Sqingli}
2385186119Sqingli
2386186119Sqingli/*
2387186119Sqingli * Deletes an address from the address table.
2388186119Sqingli * This function is called by the timer functions
2389186119Sqingli * such as arptimer() and nd6_llinfo_timer(), and
2390186119Sqingli * the caller does the locking.
2391186119Sqingli */
2392186119Sqinglistatic void
2393186119Sqingliin6_lltable_free(struct lltable *llt, struct llentry *lle)
2394186119Sqingli{
2395186150Skmacy	LLE_WUNLOCK(lle);
2396186150Skmacy	LLE_LOCK_DESTROY(lle);
2397186119Sqingli	free(lle, M_LLTABLE);
2398186119Sqingli}
2399186119Sqingli
2400192476Sqinglistatic void
2401192476Sqingliin6_lltable_prefix_free(struct lltable *llt,
2402192476Sqingli			const struct sockaddr *prefix,
2403222143Sqingli			const struct sockaddr *mask,
2404222143Sqingli			u_int flags)
2405192476Sqingli{
2406192476Sqingli	const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
2407192476Sqingli	const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
2408192476Sqingli	struct llentry *lle, *next;
2409192476Sqingli	register int i;
2410192476Sqingli
2411222143Sqingli	/*
2412222143Sqingli	 * (flags & LLE_STATIC) means deleting all entries
2413222143Sqingli	 * including static ND6 entries
2414222143Sqingli	 */
2415192476Sqingli	for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
2416192476Sqingli		LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
2417192476Sqingli			if (IN6_ARE_MASKED_ADDR_EQUAL(
2418192476Sqingli				    &((struct sockaddr_in6 *)L3_ADDR(lle))->sin6_addr,
2419192476Sqingli				    &pfx->sin6_addr,
2420222143Sqingli				    &msk->sin6_addr) &&
2421222143Sqingli			    ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))) {
2422206481Sbz				int canceled;
2423206481Sbz
2424206481Sbz				canceled = callout_drain(&lle->la_timer);
2425192476Sqingli				LLE_WLOCK(lle);
2426206481Sbz				if (canceled)
2427206481Sbz					LLE_REMREF(lle);
2428192476Sqingli				llentry_free(lle);
2429192476Sqingli			}
2430192476Sqingli		}
2431192476Sqingli	}
2432192476Sqingli}
2433192476Sqingli
2434186119Sqinglistatic int
2435201282Sqingliin6_lltable_rtcheck(struct ifnet *ifp,
2436201282Sqingli		    u_int flags,
2437201282Sqingli		    const struct sockaddr *l3addr)
2438186119Sqingli{
2439186119Sqingli	struct rtentry *rt;
2440186119Sqingli	char ip6buf[INET6_ADDRSTRLEN];
2441186119Sqingli
2442186119Sqingli	KASSERT(l3addr->sa_family == AF_INET6,
2443186119Sqingli	    ("sin_family %d", l3addr->sa_family));
2444186119Sqingli
2445186119Sqingli	/* XXX rtalloc1 should take a const param */
2446186119Sqingli	rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
2447186119Sqingli	if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2448186119Sqingli		struct ifaddr *ifa;
2449186119Sqingli		/*
2450186119Sqingli		 * Create an ND6 cache for an IPv6 neighbor
2451186119Sqingli		 * that is not covered by our own prefix.
2452186119Sqingli		 */
2453186119Sqingli		/* XXX ifaof_ifpforaddr should take a const param */
2454186119Sqingli		ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp);
2455186119Sqingli		if (ifa != NULL) {
2456194760Srwatson			ifa_free(ifa);
2457186119Sqingli			if (rt != NULL)
2458187946Sbz				RTFREE_LOCKED(rt);
2459186119Sqingli			return 0;
2460186119Sqingli		}
2461186119Sqingli		log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2462186119Sqingli		    ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr));
2463186119Sqingli		if (rt != NULL)
2464187946Sbz			RTFREE_LOCKED(rt);
2465186119Sqingli		return EINVAL;
2466186119Sqingli	}
2467187946Sbz	RTFREE_LOCKED(rt);
2468186119Sqingli	return 0;
2469186119Sqingli}
2470186119Sqingli
2471186119Sqinglistatic struct llentry *
2472186119Sqingliin6_lltable_lookup(struct lltable *llt, u_int flags,
2473186119Sqingli	const struct sockaddr *l3addr)
2474186119Sqingli{
2475186119Sqingli	const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2476186119Sqingli	struct ifnet *ifp = llt->llt_ifp;
2477186119Sqingli	struct llentry *lle;
2478186119Sqingli	struct llentries *lleh;
2479186119Sqingli	u_int hashkey;
2480186119Sqingli
2481186119Sqingli	IF_AFDATA_LOCK_ASSERT(ifp);
2482186119Sqingli	KASSERT(l3addr->sa_family == AF_INET6,
2483186119Sqingli	    ("sin_family %d", l3addr->sa_family));
2484186119Sqingli
2485186119Sqingli	hashkey = sin6->sin6_addr.s6_addr32[3];
2486186119Sqingli	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
2487186119Sqingli	LIST_FOREACH(lle, lleh, lle_next) {
2488186708Sqingli		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle);
2489186119Sqingli		if (lle->la_flags & LLE_DELETED)
2490186119Sqingli			continue;
2491186708Sqingli		if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr,
2492186708Sqingli			 sizeof(struct in6_addr)) == 0)
2493186119Sqingli			break;
2494186119Sqingli	}
2495186119Sqingli
2496186119Sqingli	if (lle == NULL) {
2497186119Sqingli		if (!(flags & LLE_CREATE))
2498186119Sqingli			return (NULL);
2499186119Sqingli		/*
2500186119Sqingli		 * A route that covers the given address must have
2501186119Sqingli		 * been installed 1st because we are doing a resolution,
2502186119Sqingli		 * verify this.
2503186119Sqingli		 */
2504186119Sqingli		if (!(flags & LLE_IFADDR) &&
2505201282Sqingli		    in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2506186119Sqingli			return NULL;
2507186119Sqingli
2508186119Sqingli		lle = in6_lltable_new(l3addr, flags);
2509186119Sqingli		if (lle == NULL) {
2510186119Sqingli			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2511186119Sqingli			return NULL;
2512186119Sqingli		}
2513186119Sqingli		lle->la_flags = flags & ~LLE_CREATE;
2514186119Sqingli		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
2515186119Sqingli			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
2516186119Sqingli			lle->la_flags |= (LLE_VALID | LLE_STATIC);
2517186119Sqingli		}
2518186119Sqingli
2519186119Sqingli		lle->lle_tbl  = llt;
2520186119Sqingli		lle->lle_head = lleh;
2521186119Sqingli		LIST_INSERT_HEAD(lleh, lle, lle_next);
2522186119Sqingli	} else if (flags & LLE_DELETE) {
2523186392Sqingli		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
2524186392Sqingli			LLE_WLOCK(lle);
2525186392Sqingli			lle->la_flags = LLE_DELETED;
2526186392Sqingli			LLE_WUNLOCK(lle);
2527198418Sqingli#ifdef DIAGNOSTIC
2528186392Sqingli			log(LOG_INFO, "ifaddr cache = %p  is deleted\n", lle);
2529186119Sqingli#endif
2530186392Sqingli		}
2531186119Sqingli		lle = (void *)-1;
2532186119Sqingli	}
2533186119Sqingli	if (LLE_IS_VALID(lle)) {
2534186119Sqingli		if (flags & LLE_EXCLUSIVE)
2535186119Sqingli			LLE_WLOCK(lle);
2536186119Sqingli		else
2537186119Sqingli			LLE_RLOCK(lle);
2538186119Sqingli	}
2539186119Sqingli	return (lle);
2540186119Sqingli}
2541186119Sqingli
2542186119Sqinglistatic int
2543186119Sqingliin6_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
2544186119Sqingli{
2545186119Sqingli	struct ifnet *ifp = llt->llt_ifp;
2546186119Sqingli	struct llentry *lle;
2547186119Sqingli	/* XXX stack use */
2548186119Sqingli	struct {
2549186119Sqingli		struct rt_msghdr	rtm;
2550186119Sqingli		struct sockaddr_in6	sin6;
2551186119Sqingli		/*
2552186119Sqingli		 * ndp.c assumes that sdl is word aligned
2553186119Sqingli		 */
2554186119Sqingli#ifdef __LP64__
2555186119Sqingli		uint32_t		pad;
2556186119Sqingli#endif
2557186119Sqingli		struct sockaddr_dl	sdl;
2558186119Sqingli	} ndpc;
2559186119Sqingli	int i, error;
2560186119Sqingli
2561196864Sqingli	if (ifp->if_flags & IFF_LOOPBACK)
2562196864Sqingli		return 0;
2563196864Sqingli
2564196535Srwatson	LLTABLE_LOCK_ASSERT();
2565186119Sqingli
2566186119Sqingli	error = 0;
2567186119Sqingli	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2568186119Sqingli		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
2569186119Sqingli			struct sockaddr_dl *sdl;
2570186119Sqingli
2571186119Sqingli			/* skip deleted or invalid entries */
2572186119Sqingli			if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
2573186119Sqingli				continue;
2574186980Sbz			/* Skip if jailed and not a valid IP of the prison. */
2575188144Sjamie			if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
2576186980Sbz				continue;
2577186119Sqingli			/*
2578186119Sqingli			 * produce a msg made of:
2579186119Sqingli			 *  struct rt_msghdr;
2580186119Sqingli			 *  struct sockaddr_in6 (IPv6)
2581186119Sqingli			 *  struct sockaddr_dl;
2582186119Sqingli			 */
2583186119Sqingli			bzero(&ndpc, sizeof(ndpc));
2584186119Sqingli			ndpc.rtm.rtm_msglen = sizeof(ndpc);
2585187094Sqingli			ndpc.rtm.rtm_version = RTM_VERSION;
2586187094Sqingli			ndpc.rtm.rtm_type = RTM_GET;
2587187094Sqingli			ndpc.rtm.rtm_flags = RTF_UP;
2588187094Sqingli			ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2589186119Sqingli			ndpc.sin6.sin6_family = AF_INET6;
2590186119Sqingli			ndpc.sin6.sin6_len = sizeof(ndpc.sin6);
2591186119Sqingli			bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle));
2592186119Sqingli
2593186119Sqingli			/* publish */
2594186119Sqingli			if (lle->la_flags & LLE_PUB)
2595186119Sqingli				ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2596186119Sqingli
2597186119Sqingli			sdl = &ndpc.sdl;
2598186119Sqingli			sdl->sdl_family = AF_LINK;
2599186119Sqingli			sdl->sdl_len = sizeof(*sdl);
2600186119Sqingli			sdl->sdl_alen = ifp->if_addrlen;
2601186119Sqingli			sdl->sdl_index = ifp->if_index;
2602186119Sqingli			sdl->sdl_type = ifp->if_type;
2603186119Sqingli			bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2604186119Sqingli			ndpc.rtm.rtm_rmx.rmx_expire =
2605186119Sqingli			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
2606186500Sqingli			ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2607186119Sqingli			if (lle->la_flags & LLE_STATIC)
2608186119Sqingli				ndpc.rtm.rtm_flags |= RTF_STATIC;
2609186119Sqingli			ndpc.rtm.rtm_index = ifp->if_index;
2610186119Sqingli			error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2611186119Sqingli			if (error)
2612186119Sqingli				break;
2613186119Sqingli		}
2614186119Sqingli	}
2615186119Sqingli	return error;
2616186119Sqingli}
2617186119Sqingli
2618121161Sumevoid *
2619171259Sdelphijin6_domifattach(struct ifnet *ifp)
2620121161Sume{
2621121161Sume	struct in6_ifextra *ext;
2622121161Sume
2623121161Sume	ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2624121161Sume	bzero(ext, sizeof(*ext));
2625121161Sume
2626121161Sume	ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2627121161Sume	    M_IFADDR, M_WAITOK);
2628121161Sume	bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2629121161Sume
2630121161Sume	ext->icmp6_ifstat =
2631121161Sume	    (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2632121161Sume	    M_IFADDR, M_WAITOK);
2633121161Sume	bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2634121161Sume
2635121161Sume	ext->nd_ifinfo = nd6_ifattach(ifp);
2636121161Sume	ext->scope6_id = scope6_ifattach(ifp);
2637186119Sqingli	ext->lltable = lltable_init(ifp, AF_INET6);
2638186119Sqingli	if (ext->lltable != NULL) {
2639186119Sqingli		ext->lltable->llt_new = in6_lltable_new;
2640186119Sqingli		ext->lltable->llt_free = in6_lltable_free;
2641192476Sqingli		ext->lltable->llt_prefix_free = in6_lltable_prefix_free;
2642186119Sqingli		ext->lltable->llt_rtcheck = in6_lltable_rtcheck;
2643186119Sqingli		ext->lltable->llt_lookup = in6_lltable_lookup;
2644186119Sqingli		ext->lltable->llt_dump = in6_lltable_dump;
2645186119Sqingli	}
2646191672Sbms
2647191672Sbms	ext->mld_ifinfo = mld_domifattach(ifp);
2648191672Sbms
2649121161Sume	return ext;
2650121161Sume}
2651121161Sume
2652121161Sumevoid
2653171259Sdelphijin6_domifdetach(struct ifnet *ifp, void *aux)
2654121161Sume{
2655121161Sume	struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2656121161Sume
2657191672Sbms	mld_domifdetach(ifp);
2658121161Sume	scope6_ifdetach(ext->scope6_id);
2659121161Sume	nd6_ifdetach(ext->nd_ifinfo);
2660186119Sqingli	lltable_free(ext->lltable);
2661121161Sume	free(ext->in6_ifstat, M_IFADDR);
2662121161Sume	free(ext->icmp6_ifstat, M_IFADDR);
2663121161Sume	free(ext, M_IFADDR);
2664121161Sume}
2665121161Sume
266653541Sshin/*
266795023Ssuz * Convert sockaddr_in6 to sockaddr_in.  Original sockaddr_in6 must be
266853541Sshin * v4 mapped addr or v4 compat addr
266953541Sshin */
267053541Sshinvoid
267153541Sshinin6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
267253541Sshin{
2673171259Sdelphij
267453541Sshin	bzero(sin, sizeof(*sin));
267553541Sshin	sin->sin_len = sizeof(struct sockaddr_in);
267653541Sshin	sin->sin_family = AF_INET;
267753541Sshin	sin->sin_port = sin6->sin6_port;
2678120891Sume	sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
267953541Sshin}
268053541Sshin
268153541Sshin/* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
268253541Sshinvoid
268353541Sshinin6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
268453541Sshin{
268553541Sshin	bzero(sin6, sizeof(*sin6));
268653541Sshin	sin6->sin6_len = sizeof(struct sockaddr_in6);
268753541Sshin	sin6->sin6_family = AF_INET6;
268853541Sshin	sin6->sin6_port = sin->sin_port;
268953541Sshin	sin6->sin6_addr.s6_addr32[0] = 0;
269053541Sshin	sin6->sin6_addr.s6_addr32[1] = 0;
269153541Sshin	sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
269253541Sshin	sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
269353541Sshin}
269453541Sshin
269553541Sshin/* Convert sockaddr_in6 into sockaddr_in. */
269653541Sshinvoid
269753541Sshinin6_sin6_2_sin_in_sock(struct sockaddr *nam)
269853541Sshin{
269953541Sshin	struct sockaddr_in *sin_p;
270053541Sshin	struct sockaddr_in6 sin6;
270153541Sshin
270253541Sshin	/*
270353541Sshin	 * Save original sockaddr_in6 addr and convert it
270453541Sshin	 * to sockaddr_in.
270553541Sshin	 */
270653541Sshin	sin6 = *(struct sockaddr_in6 *)nam;
270753541Sshin	sin_p = (struct sockaddr_in *)nam;
270853541Sshin	in6_sin6_2_sin(sin_p, &sin6);
270953541Sshin}
271053541Sshin
271153541Sshin/* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
271253541Sshinvoid
271353541Sshinin6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
271453541Sshin{
271553541Sshin	struct sockaddr_in *sin_p;
271653541Sshin	struct sockaddr_in6 *sin6_p;
271753541Sshin
2718184205Sdes	sin6_p = malloc(sizeof *sin6_p, M_SONAME,
2719111119Simp	       M_WAITOK);
272053541Sshin	sin_p = (struct sockaddr_in *)*nam;
272153541Sshin	in6_sin_2_v4mapsin6(sin_p, sin6_p);
2722184205Sdes	free(*nam, M_SONAME);
272353541Sshin	*nam = (struct sockaddr *)sin6_p;
272453541Sshin}
2725