in6.c revision 219819
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 219819 2011-03-21 09:40:01Z jeff $");
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();
655126264Smlaier		if (error == 0 && ia)
656126264Smlaier			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
65778064Sume		break;
65878064Sume	}
65962587Sitojun
66078064Sume	case SIOCDIFADDR_IN6:
66178064Sume	{
662151539Ssuz		struct nd_prefix *pr;
66378064Sume
66478064Sume		/*
66578064Sume		 * If the address being deleted is the only one that owns
66678064Sume		 * the corresponding prefix, expire the prefix as well.
667120891Sume		 * XXX: theoretically, we don't have to worry about such
66878064Sume		 * relationship, since we separate the address management
66978064Sume		 * and the prefix management.  We do this, however, to provide
67078064Sume		 * as much backward compatibility as possible in terms of
67178064Sume		 * the ioctl operation.
672151915Ssuz		 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
67378064Sume		 */
674151915Ssuz		pr = ia->ia6_ndpr;
67578064Sume		in6_purgeaddr(&ia->ia_ifa);
676151915Ssuz		if (pr && pr->ndpr_refcnt == 0)
677151915Ssuz			prelist_remove(pr);
678126264Smlaier		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
67953541Sshin		break;
68078064Sume	}
68153541Sshin
68278064Sume	default:
683194760Srwatson		if (ifp == NULL || ifp->if_ioctl == 0) {
684194760Srwatson			error = EOPNOTSUPP;
685194760Srwatson			goto out;
686194760Srwatson		}
687194760Srwatson		error = (*ifp->if_ioctl)(ifp, cmd, data);
688194760Srwatson		goto out;
68978064Sume	}
69053541Sshin
691194760Srwatson	error = 0;
692194760Srwatsonout:
693194760Srwatson	if (ia != NULL)
694194760Srwatson		ifa_free(&ia->ia_ifa);
695194760Srwatson	return (error);
69678064Sume}
69753541Sshin
69878064Sume/*
69978064Sume * Update parameters of an IPv6 interface address.
70078064Sume * If necessary, a new entry is created and linked into address chains.
70178064Sume * This function is separated from in6_control().
70278064Sume * XXX: should this be performed under splnet()?
70378064Sume */
70478064Sumeint
705171259Sdelphijin6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
706171259Sdelphij    struct in6_ifaddr *ia, int flags)
70778064Sume{
70878064Sume	int error = 0, hostIsNew = 0, plen = -1;
70978064Sume	struct sockaddr_in6 dst6;
71078064Sume	struct in6_addrlifetime *lt;
711151539Ssuz	struct in6_multi_mship *imm;
712151539Ssuz	struct in6_multi *in6m_sol;
713148385Sume	struct rtentry *rt;
714151539Ssuz	int delay;
715165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
71678064Sume
71778064Sume	/* Validate parameters */
71878064Sume	if (ifp == NULL || ifra == NULL) /* this maybe redundant */
719120856Sume		return (EINVAL);
72078064Sume
72178064Sume	/*
72278064Sume	 * The destination address for a p2p link must have a family
72378064Sume	 * of AF_UNSPEC or AF_INET6.
72478064Sume	 */
72578064Sume	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
72678064Sume	    ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
72778064Sume	    ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
728120856Sume		return (EAFNOSUPPORT);
72978064Sume	/*
73078064Sume	 * validate ifra_prefixmask.  don't check sin6_family, netmask
73178064Sume	 * does not carry fields other than sin6_len.
73278064Sume	 */
73378064Sume	if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
734120856Sume		return (EINVAL);
73578064Sume	/*
73678064Sume	 * Because the IPv6 address architecture is classless, we require
73778064Sume	 * users to specify a (non 0) prefix length (mask) for a new address.
73878064Sume	 * We also require the prefix (when specified) mask is valid, and thus
73978064Sume	 * reject a non-consecutive mask.
74078064Sume	 */
74178064Sume	if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
742120856Sume		return (EINVAL);
74378064Sume	if (ifra->ifra_prefixmask.sin6_len != 0) {
74478064Sume		plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
745120891Sume		    (u_char *)&ifra->ifra_prefixmask +
746120891Sume		    ifra->ifra_prefixmask.sin6_len);
74778064Sume		if (plen <= 0)
748120856Sume			return (EINVAL);
749120891Sume	} else {
75062587Sitojun		/*
75195023Ssuz		 * In this case, ia must not be NULL.  We just use its prefix
75278064Sume		 * length.
75362587Sitojun		 */
75478064Sume		plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
75578064Sume	}
75678064Sume	/*
75778064Sume	 * If the destination address on a p2p interface is specified,
75878064Sume	 * and the address is a scoped one, validate/set the scope
75978064Sume	 * zone identifier.
76078064Sume	 */
76178064Sume	dst6 = ifra->ifra_dstaddr;
762120891Sume	if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
76378064Sume	    (dst6.sin6_family == AF_INET6)) {
764148385Sume		struct in6_addr in6_tmp;
765126552Sume		u_int32_t zoneid;
76678064Sume
767148385Sume		in6_tmp = dst6.sin6_addr;
768148385Sume		if (in6_setscope(&in6_tmp, ifp, &zoneid))
769148385Sume			return (EINVAL); /* XXX: should be impossible */
770148385Sume
771148385Sume		if (dst6.sin6_scope_id != 0) {
772148385Sume			if (dst6.sin6_scope_id != zoneid)
773148385Sume				return (EINVAL);
774148385Sume		} else		/* user omit to specify the ID. */
775126552Sume			dst6.sin6_scope_id = zoneid;
776148385Sume
777148385Sume		/* convert into the internal form */
778148385Sume		if (sa6_embedscope(&dst6, 0))
779148385Sume			return (EINVAL); /* XXX: should be impossible */
78078064Sume	}
78178064Sume	/*
78278064Sume	 * The destination address can be specified only for a p2p or a
78378064Sume	 * loopback interface.  If specified, the corresponding prefix length
78478064Sume	 * must be 128.
78578064Sume	 */
78678064Sume	if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
78778064Sume		if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
788126552Sume			/* XXX: noisy message */
789122059Sume			nd6log((LOG_INFO, "in6_update_ifa: a destination can "
790122059Sume			    "be specified for a p2p or a loopback IF only\n"));
791120856Sume			return (EINVAL);
79278064Sume		}
79378064Sume		if (plen != 128) {
794122059Sume			nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
795122059Sume			    "be 128 when dstaddr is specified\n"));
796120856Sume			return (EINVAL);
79778064Sume		}
79878064Sume	}
79978064Sume	/* lifetime consistency check */
80078064Sume	lt = &ifra->ifra_lifetime;
801151539Ssuz	if (lt->ia6t_pltime > lt->ia6t_vltime)
802151539Ssuz		return (EINVAL);
80378064Sume	if (lt->ia6t_vltime == 0) {
80462587Sitojun		/*
80578064Sume		 * the following log might be noisy, but this is a typical
80678064Sume		 * configuration mistake or a tool's bug.
80762587Sitojun		 */
808122059Sume		nd6log((LOG_INFO,
80978064Sume		    "in6_update_ifa: valid lifetime is 0 for %s\n",
810165118Sbz		    ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
811151539Ssuz
812151539Ssuz		if (ia == NULL)
813151539Ssuz			return (0); /* there's nothing to do */
81478064Sume	}
81562587Sitojun
81678064Sume	/*
81778064Sume	 * If this is a new address, allocate a new ifaddr and link it
81878064Sume	 * into chains.
81978064Sume	 */
82078064Sume	if (ia == NULL) {
82178064Sume		hostIsNew = 1;
82279763Sume		/*
82379763Sume		 * When in6_update_ifa() is called in a process of a received
824120891Sume		 * RA, it is called under an interrupt context.  So, we should
825120891Sume		 * call malloc with M_NOWAIT.
82679763Sume		 */
827120891Sume		ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
828120891Sume		    M_NOWAIT);
82978064Sume		if (ia == NULL)
83078064Sume			return (ENOBUFS);
83178064Sume		bzero((caddr_t)ia, sizeof(*ia));
832194602Srwatson		ifa_init(&ia->ia_ifa);
833170202Sjinmei		LIST_INIT(&ia->ia6_memberships);
834151539Ssuz		/* Initialize the address and masks, and put time stamp */
83578064Sume		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
83678064Sume		ia->ia_addr.sin6_family = AF_INET6;
83778064Sume		ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
838151539Ssuz		ia->ia6_createtime = time_second;
83978064Sume		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
84078064Sume			/*
84178064Sume			 * XXX: some functions expect that ifa_dstaddr is not
84278064Sume			 * NULL for p2p interfaces.
84378064Sume			 */
844120891Sume			ia->ia_ifa.ifa_dstaddr =
845120891Sume			    (struct sockaddr *)&ia->ia_dstaddr;
84678064Sume		} else {
84778064Sume			ia->ia_ifa.ifa_dstaddr = NULL;
84853541Sshin		}
849108033Shsu		ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
85078064Sume		ia->ia_ifp = ifp;
851194760Srwatson		ifa_ref(&ia->ia_ifa);			/* if_addrhead */
852191323Srwatson		IF_ADDR_LOCK(ifp);
853191340Srwatson		TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
854191323Srwatson		IF_ADDR_UNLOCK(ifp);
855194907Srwatson
856194971Srwatson		ifa_ref(&ia->ia_ifa);			/* in6_ifaddrhead */
857194971Srwatson		IN6_IFADDR_WLOCK();
858194907Srwatson		TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
859194971Srwatson		IN6_IFADDR_WUNLOCK();
86078064Sume	}
86178064Sume
862151539Ssuz	/* update timestamp */
863151539Ssuz	ia->ia6_updatetime = time_second;
864151539Ssuz
86578064Sume	/* set prefix mask */
86678064Sume	if (ifra->ifra_prefixmask.sin6_len) {
86778064Sume		/*
86878064Sume		 * We prohibit changing the prefix length of an existing
86978064Sume		 * address, because
87078064Sume		 * + such an operation should be rare in IPv6, and
87178064Sume		 * + the operation would confuse prefix management.
87278064Sume		 */
87378064Sume		if (ia->ia_prefixmask.sin6_len &&
87478064Sume		    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
875122059Sume			nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
87678064Sume			    " existing (%s) address should not be changed\n",
877165118Sbz			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
87878064Sume			error = EINVAL;
87978064Sume			goto unlink;
88053541Sshin		}
88178064Sume		ia->ia_prefixmask = ifra->ifra_prefixmask;
88278064Sume	}
88378064Sume
88478064Sume	/*
88578064Sume	 * If a new destination address is specified, scrub the old one and
88678064Sume	 * install the new destination.  Note that the interface must be
887120891Sume	 * p2p or loopback (see the check above.)
88878064Sume	 */
88978064Sume	if (dst6.sin6_family == AF_INET6 &&
890120891Sume	    !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
89178064Sume		int e;
89278064Sume
89378064Sume		if ((ia->ia_flags & IFA_ROUTE) != 0 &&
894120891Sume		    (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
895122059Sume			nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
89678064Sume			    "a route to the old destination: %s\n",
897165118Sbz			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
89878064Sume			/* proceed anyway... */
899120891Sume		} else
90078064Sume			ia->ia_flags &= ~IFA_ROUTE;
90178064Sume		ia->ia_dstaddr = dst6;
90278064Sume	}
90353541Sshin
904148385Sume	/*
905148385Sume	 * Set lifetimes.  We do not refer to ia6t_expire and ia6t_preferred
906148385Sume	 * to see if the address is deprecated or invalidated, but initialize
907148385Sume	 * these members for applications.
908148385Sume	 */
909148385Sume	ia->ia6_lifetime = ifra->ifra_lifetime;
910148385Sume	if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
911148385Sume		ia->ia6_lifetime.ia6t_expire =
912148385Sume		    time_second + ia->ia6_lifetime.ia6t_vltime;
913148385Sume	} else
914148385Sume		ia->ia6_lifetime.ia6t_expire = 0;
915148385Sume	if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
916148385Sume		ia->ia6_lifetime.ia6t_preferred =
917148385Sume		    time_second + ia->ia6_lifetime.ia6t_pltime;
918148385Sume	} else
919148385Sume		ia->ia6_lifetime.ia6t_preferred = 0;
920148385Sume
92178064Sume	/* reset the interface and routing table appropriately. */
92278064Sume	if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
92378064Sume		goto unlink;
92478064Sume
92578064Sume	/*
926148385Sume	 * configure address flags.
927148385Sume	 */
928148385Sume	ia->ia6_flags = ifra->ifra_flags;
929148385Sume	/*
930148385Sume	 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
931148385Sume	 * userland, make it deprecated.
932148385Sume	 */
933148385Sume	if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
934148385Sume		ia->ia6_lifetime.ia6t_pltime = 0;
935148385Sume		ia->ia6_lifetime.ia6t_preferred = time_second;
936148385Sume	}
937148385Sume	/*
938151539Ssuz	 * Make the address tentative before joining multicast addresses,
939151539Ssuz	 * so that corresponding MLD responses would not have a tentative
940151539Ssuz	 * source address.
941148385Sume	 */
942151539Ssuz	ia->ia6_flags &= ~IN6_IFF_DUPLICATED;	/* safety */
943151539Ssuz	if (hostIsNew && in6if_do_dad(ifp))
944148385Sume		ia->ia6_flags |= IN6_IFF_TENTATIVE;
945148385Sume
946197138Shrs	/* DAD should be performed after ND6_IFF_IFDISABLED is cleared. */
947197138Shrs	if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
948197138Shrs		ia->ia6_flags |= IN6_IFF_TENTATIVE;
949197138Shrs
950148385Sume	/*
951148385Sume	 * We are done if we have simply modified an existing address.
952148385Sume	 */
953148385Sume	if (!hostIsNew)
954148385Sume		return (error);
955148385Sume
956148385Sume	/*
95778064Sume	 * Beyond this point, we should call in6_purgeaddr upon an error,
958120891Sume	 * not just go to unlink.
95978064Sume	 */
96078064Sume
961151465Ssuz	/* Join necessary multicast groups */
962151539Ssuz	in6m_sol = NULL;
96378064Sume	if ((ifp->if_flags & IFF_MULTICAST) != 0) {
96478064Sume		struct sockaddr_in6 mltaddr, mltmask;
965148385Sume		struct in6_addr llsol;
96678064Sume
967148385Sume		/* join solicited multicast addr for new host id */
968148385Sume		bzero(&llsol, sizeof(struct in6_addr));
969151465Ssuz		llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
970148385Sume		llsol.s6_addr32[1] = 0;
971148385Sume		llsol.s6_addr32[2] = htonl(1);
972148385Sume		llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
973148385Sume		llsol.s6_addr8[12] = 0xff;
974148385Sume		if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
975148385Sume			/* XXX: should not happen */
976148385Sume			log(LOG_ERR, "in6_update_ifa: "
977148385Sume			    "in6_setscope failed\n");
978148385Sume			goto cleanup;
97953541Sshin		}
980151539Ssuz		delay = 0;
981151539Ssuz		if ((flags & IN6_IFAUPDATE_DADDELAY)) {
982151539Ssuz			/*
983151539Ssuz			 * We need a random delay for DAD on the address
984151539Ssuz			 * being configured.  It also means delaying
985151539Ssuz			 * transmission of the corresponding MLD report to
986151539Ssuz			 * avoid report collision.
987197138Shrs			 * [RFC 4861, Section 6.3.7]
988151539Ssuz			 */
989151539Ssuz			delay = arc4random() %
990151539Ssuz			    (MAX_RTR_SOLICITATION_DELAY * hz);
991151539Ssuz		}
992151539Ssuz		imm = in6_joingroup(ifp, &llsol, &error, delay);
993170202Sjinmei		if (imm == NULL) {
994148385Sume			nd6log((LOG_WARNING,
995148385Sume			    "in6_update_ifa: addmulti failed for "
996148385Sume			    "%s on %s (errno=%d)\n",
997165118Sbz			    ip6_sprintf(ip6buf, &llsol), if_name(ifp),
998148385Sume			    error));
999195102Srwatson			goto cleanup;
1000148385Sume		}
1001170202Sjinmei		LIST_INSERT_HEAD(&ia->ia6_memberships,
1002170202Sjinmei		    imm, i6mm_chain);
1003151539Ssuz		in6m_sol = imm->i6mm_maddr;
100453541Sshin
100578064Sume		bzero(&mltmask, sizeof(mltmask));
100678064Sume		mltmask.sin6_len = sizeof(struct sockaddr_in6);
100778064Sume		mltmask.sin6_family = AF_INET6;
100878064Sume		mltmask.sin6_addr = in6mask32;
1009151465Ssuz#define	MLTMASK_LEN  4	/* mltmask's masklen (=32bit=4octet) */
101053541Sshin
101153541Sshin		/*
101278064Sume		 * join link-local all-nodes address
101371207Sitojun		 */
101478064Sume		bzero(&mltaddr, sizeof(mltaddr));
101578064Sume		mltaddr.sin6_len = sizeof(struct sockaddr_in6);
101678064Sume		mltaddr.sin6_family = AF_INET6;
101778064Sume		mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1018148385Sume		if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1019148385Sume		    0)
1020148385Sume			goto cleanup; /* XXX: should not fail */
102171207Sitojun
1022148385Sume		/*
1023148385Sume		 * XXX: do we really need this automatic routes?
1024148385Sume		 * We should probably reconsider this stuff.  Most applications
1025148385Sume		 * actually do not need the routes, since they usually specify
1026148385Sume		 * the outgoing interface.
1027148385Sume		 */
1028148385Sume		rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1029148385Sume		if (rt) {
1030174375Sjulian			/* XXX: only works in !SCOPEDROUTING case. */
1031148385Sume			if (memcmp(&mltaddr.sin6_addr,
1032148385Sume			    &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1033151465Ssuz			    MLTMASK_LEN)) {
1034148385Sume				RTFREE_LOCKED(rt);
1035148385Sume				rt = NULL;
1036148385Sume			}
1037148385Sume		}
1038148385Sume		if (!rt) {
1039148385Sume			error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1040148385Sume			    (struct sockaddr *)&ia->ia_addr,
1041186119Sqingli			    (struct sockaddr *)&mltmask, RTF_UP,
1042148385Sume			    (struct rtentry **)0);
1043148385Sume			if (error)
1044148385Sume				goto cleanup;
1045151539Ssuz		} else {
1046151539Ssuz			RTFREE_LOCKED(rt);
1047151539Ssuz		}
104878064Sume
1049151539Ssuz		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1050151539Ssuz		if (!imm) {
1051151539Ssuz			nd6log((LOG_WARNING,
1052151539Ssuz			    "in6_update_ifa: addmulti failed for "
1053151539Ssuz			    "%s on %s (errno=%d)\n",
1054165118Sbz			    ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1055151539Ssuz			    if_name(ifp), error));
1056151539Ssuz			goto cleanup;
1057151539Ssuz		}
1058170202Sjinmei		LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1059151539Ssuz
106071207Sitojun		/*
106178064Sume		 * join node information group address
106253541Sshin		 */
1063151539Ssuz		delay = 0;
1064151539Ssuz		if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1065151539Ssuz			/*
1066151539Ssuz			 * The spec doesn't say anything about delay for this
1067151539Ssuz			 * group, but the same logic should apply.
1068151539Ssuz			 */
1069151539Ssuz			delay = arc4random() %
1070151539Ssuz			    (MAX_RTR_SOLICITATION_DELAY * hz);
1071151539Ssuz		}
1072192895Sjamie		if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
1073151539Ssuz			imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error,
1074151539Ssuz			    delay); /* XXX jinmei */
1075151539Ssuz			if (!imm) {
1076151539Ssuz				nd6log((LOG_WARNING, "in6_update_ifa: "
1077151539Ssuz				    "addmulti failed for %s on %s "
1078151539Ssuz				    "(errno=%d)\n",
1079165118Sbz				    ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1080151539Ssuz				    if_name(ifp), error));
1081151539Ssuz				/* XXX not very fatal, go on... */
1082170202Sjinmei			} else {
1083170202Sjinmei				LIST_INSERT_HEAD(&ia->ia6_memberships,
1084170202Sjinmei				    imm, i6mm_chain);
108562587Sitojun			}
1086192895Sjamie		}
108753541Sshin
108878064Sume		/*
1089148385Sume		 * join interface-local all-nodes address.
1090148385Sume		 * (ff01::1%ifN, and ff01::%ifN/32)
109178064Sume		 */
1092148385Sume		mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1093148385Sume		if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL))
1094148385Sume		    != 0)
1095148385Sume			goto cleanup; /* XXX: should not fail */
1096148385Sume		/* XXX: again, do we really need the route? */
1097148385Sume		rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1098148385Sume		if (rt) {
1099148385Sume			if (memcmp(&mltaddr.sin6_addr,
1100148385Sume			    &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
1101151465Ssuz			    MLTMASK_LEN)) {
1102148385Sume				RTFREE_LOCKED(rt);
1103148385Sume				rt = NULL;
1104148385Sume			}
1105148385Sume		}
1106148385Sume		if (!rt) {
1107148385Sume			error = rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
1108148385Sume			    (struct sockaddr *)&ia->ia_addr,
1109186119Sqingli			    (struct sockaddr *)&mltmask, RTF_UP,
1110148385Sume			    (struct rtentry **)0);
1111148385Sume			if (error)
1112148385Sume				goto cleanup;
1113148385Sume		} else
1114148385Sume			RTFREE_LOCKED(rt);
111581115Sume
1116151539Ssuz		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
1117151539Ssuz		if (!imm) {
1118151539Ssuz			nd6log((LOG_WARNING, "in6_update_ifa: "
1119151539Ssuz			    "addmulti failed for %s on %s "
1120151539Ssuz			    "(errno=%d)\n",
1121165118Sbz			    ip6_sprintf(ip6buf, &mltaddr.sin6_addr),
1122151539Ssuz			    if_name(ifp), error));
1123151539Ssuz			goto cleanup;
1124151539Ssuz		}
1125170202Sjinmei		LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
1126151465Ssuz#undef	MLTMASK_LEN
112778064Sume	}
112853541Sshin
1129151539Ssuz	/*
1130151539Ssuz	 * Perform DAD, if needed.
1131151539Ssuz	 * XXX It may be of use, if we can administratively
1132151539Ssuz	 * disable DAD.
1133151539Ssuz	 */
1134194760Srwatson	if (in6if_do_dad(ifp) && ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) &&
1135151539Ssuz	    (ia->ia6_flags & IN6_IFF_TENTATIVE))
1136151539Ssuz	{
1137151539Ssuz		int mindelay, maxdelay;
1138151539Ssuz
1139151539Ssuz		delay = 0;
1140151539Ssuz		if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1141151539Ssuz			/*
1142151539Ssuz			 * We need to impose a delay before sending an NS
1143151539Ssuz			 * for DAD.  Check if we also needed a delay for the
1144151539Ssuz			 * corresponding MLD message.  If we did, the delay
1145151539Ssuz			 * should be larger than the MLD delay (this could be
1146151539Ssuz			 * relaxed a bit, but this simple logic is at least
1147151539Ssuz			 * safe).
1148191672Sbms			 * XXX: Break data hiding guidelines and look at
1149191672Sbms			 * state for the solicited multicast group.
1150151539Ssuz			 */
1151151539Ssuz			mindelay = 0;
1152151539Ssuz			if (in6m_sol != NULL &&
1153191672Sbms			    in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1154151539Ssuz				mindelay = in6m_sol->in6m_timer;
1155151539Ssuz			}
1156151539Ssuz			maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1157151539Ssuz			if (maxdelay - mindelay == 0)
1158151539Ssuz				delay = 0;
1159151539Ssuz			else {
1160151539Ssuz				delay =
1161151539Ssuz				    (arc4random() % (maxdelay - mindelay)) +
1162151539Ssuz				    mindelay;
1163151539Ssuz			}
1164151539Ssuz		}
1165151539Ssuz		nd6_dad_start((struct ifaddr *)ia, delay);
1166151539Ssuz	}
1167151539Ssuz
1168194760Srwatson	KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew"));
1169194760Srwatson	ifa_free(&ia->ia_ifa);
1170120856Sume	return (error);
117178064Sume
117278064Sume  unlink:
117378064Sume	/*
117478064Sume	 * XXX: if a change of an existing address failed, keep the entry
117578064Sume	 * anyway.
117678064Sume	 */
1177194760Srwatson	if (hostIsNew) {
1178194943Srwatson		in6_unlink_ifa(ia, ifp);
1179194760Srwatson		ifa_free(&ia->ia_ifa);
1180194760Srwatson	}
1181120856Sume	return (error);
1182148385Sume
1183148385Sume  cleanup:
1184194760Srwatson	KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew"));
1185194760Srwatson	ifa_free(&ia->ia_ifa);
1186148385Sume	in6_purgeaddr(&ia->ia_ifa);
1187148385Sume	return error;
118853541Sshin}
118953541Sshin
119062587Sitojunvoid
1191171259Sdelphijin6_purgeaddr(struct ifaddr *ifa)
119262587Sitojun{
119378064Sume	struct ifnet *ifp = ifa->ifa_ifp;
119478064Sume	struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1195170202Sjinmei	struct in6_multi_mship *imm;
1196192282Sqingli	struct sockaddr_in6 mltaddr, mltmask;
1197192282Sqingli	struct rtentry rt0;
1198192282Sqingli	struct sockaddr_dl gateway;
1199192282Sqingli	struct sockaddr_in6 mask, addr;
1200192282Sqingli	int plen, error;
1201192282Sqingli	struct rtentry *rt;
1202192282Sqingli	struct ifaddr *ifa0, *nifa;
120362587Sitojun
1204192282Sqingli	/*
1205192282Sqingli	 * find another IPv6 address as the gateway for the
1206192282Sqingli	 * link-local and node-local all-nodes multicast
1207192282Sqingli	 * address routes
1208192282Sqingli	 */
1209194760Srwatson	IF_ADDR_LOCK(ifp);
1210192282Sqingli	TAILQ_FOREACH_SAFE(ifa0, &ifp->if_addrhead, ifa_link, nifa) {
1211192282Sqingli		if ((ifa0->ifa_addr->sa_family != AF_INET6) ||
1212192282Sqingli		    memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr,
1213192282Sqingli			   &ia->ia_addr.sin6_addr,
1214192282Sqingli			   sizeof(struct in6_addr)) == 0)
1215192282Sqingli			continue;
1216192282Sqingli		else
1217192282Sqingli			break;
1218192282Sqingli	}
1219194760Srwatson	if (ifa0 != NULL)
1220194760Srwatson		ifa_ref(ifa0);
1221194760Srwatson	IF_ADDR_UNLOCK(ifp);
1222192282Sqingli
1223195914Sqingli	/*
1224195914Sqingli	 * Remove the loopback route to the interface address.
1225196871Sqingli	 * The check for the current setting of "nd6_useloopback"
1226196871Sqingli	 * is not needed.
1227195914Sqingli	 */
1228201282Sqingli	if (ia->ia_flags & IFA_RTSELF) {
1229201282Sqingli		error = ifa_del_loopback_route((struct ifaddr *)ia,
1230201282Sqingli				       (struct sockaddr *)&ia->ia_addr);
1231201282Sqingli		if (error == 0)
1232201282Sqingli			ia->ia_flags &= ~IFA_RTSELF;
1233201282Sqingli	}
1234195643Sqingli
123578064Sume	/* stop DAD processing */
123678064Sume	nd6_dad_stop(ifa);
123762587Sitojun
1238186119Sqingli	IF_AFDATA_LOCK(ifp);
1239186119Sqingli	lla_lookup(LLTABLE6(ifp), (LLE_DELETE | LLE_IFADDR),
1240186119Sqingli	    (struct sockaddr *)&ia->ia_addr);
1241186119Sqingli	IF_AFDATA_UNLOCK(ifp);
1242192282Sqingli
124378064Sume	/*
1244192282Sqingli	 * initialize for rtmsg generation
1245192282Sqingli	 */
1246192282Sqingli	bzero(&gateway, sizeof(gateway));
1247192282Sqingli	gateway.sdl_len = sizeof(gateway);
1248192282Sqingli	gateway.sdl_family = AF_LINK;
1249192282Sqingli	gateway.sdl_nlen = 0;
1250192282Sqingli	gateway.sdl_alen = ifp->if_addrlen;
1251192282Sqingli	/* */
1252192282Sqingli	bzero(&rt0, sizeof(rt0));
1253192282Sqingli	rt0.rt_gateway = (struct sockaddr *)&gateway;
1254192282Sqingli	memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
1255192282Sqingli	memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
1256192282Sqingli	rt_mask(&rt0) = (struct sockaddr *)&mask;
1257192282Sqingli	rt_key(&rt0) = (struct sockaddr *)&addr;
1258192282Sqingli	rt0.rt_flags = RTF_HOST | RTF_STATIC;
1259192282Sqingli	rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0);
1260192282Sqingli
1261192282Sqingli	/*
1262170202Sjinmei	 * leave from multicast groups we have joined for the interface
1263170202Sjinmei	 */
1264170202Sjinmei	while ((imm = ia->ia6_memberships.lh_first) != NULL) {
1265170202Sjinmei		LIST_REMOVE(imm, i6mm_chain);
1266170202Sjinmei		in6_leavegroup(imm);
126762587Sitojun	}
126862587Sitojun
1269192282Sqingli	/*
1270192282Sqingli	 * remove the link-local all-nodes address
1271192282Sqingli	 */
1272192282Sqingli	bzero(&mltmask, sizeof(mltmask));
1273192282Sqingli	mltmask.sin6_len = sizeof(struct sockaddr_in6);
1274192282Sqingli	mltmask.sin6_family = AF_INET6;
1275192282Sqingli	mltmask.sin6_addr = in6mask32;
1276192282Sqingli
1277192282Sqingli	bzero(&mltaddr, sizeof(mltaddr));
1278192282Sqingli	mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1279192282Sqingli	mltaddr.sin6_family = AF_INET6;
1280192282Sqingli	mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1281192282Sqingli
1282192282Sqingli	if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1283192282Sqingli	    0)
1284192282Sqingli		goto cleanup;
1285192282Sqingli
1286192282Sqingli	rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1287192282Sqingli	if (rt != NULL && rt->rt_gateway != NULL &&
1288192282Sqingli	    (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1289192282Sqingli		    &ia->ia_addr.sin6_addr,
1290192282Sqingli		    sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1291192282Sqingli		/*
1292192282Sqingli		 * if no more IPv6 address exists on this interface
1293192282Sqingli		 * then remove the multicast address route
1294192282Sqingli		 */
1295192282Sqingli		if (ifa0 == NULL) {
1296192282Sqingli			memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1297192282Sqingli			       sizeof(mltaddr.sin6_addr));
1298192282Sqingli			RTFREE_LOCKED(rt);
1299192282Sqingli			error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr,
1300192282Sqingli					  (struct sockaddr *)&ia->ia_addr,
1301192282Sqingli					  (struct sockaddr *)&mltmask, RTF_UP,
1302192282Sqingli					  (struct rtentry **)0);
1303192282Sqingli			if (error)
1304192282Sqingli				log(LOG_INFO, "in6_purgeaddr: link-local all-nodes"
1305192282Sqingli				    "multicast address deletion error\n");
1306192282Sqingli		} else {
1307192282Sqingli			/*
1308192282Sqingli			 * replace the gateway of the route
1309192282Sqingli			 */
1310192282Sqingli			struct sockaddr_in6 sa;
1311192282Sqingli
1312192282Sqingli			bzero(&sa, sizeof(sa));
1313192282Sqingli			sa.sin6_len = sizeof(struct sockaddr_in6);
1314192282Sqingli			sa.sin6_family = AF_INET6;
1315192282Sqingli			memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1316192282Sqingli			       sizeof(sa.sin6_addr));
1317192282Sqingli			in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1318192282Sqingli			memcpy(rt->rt_gateway, &sa, sizeof(sa));
1319192282Sqingli			RTFREE_LOCKED(rt);
1320192282Sqingli		}
1321192282Sqingli	} else {
1322192282Sqingli		if (rt != NULL)
1323192282Sqingli			RTFREE_LOCKED(rt);
1324192282Sqingli	}
1325192282Sqingli
1326192282Sqingli	/*
1327192282Sqingli	 * remove the node-local all-nodes address
1328192282Sqingli	 */
1329192282Sqingli	mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1330192282Sqingli	if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) !=
1331192282Sqingli	    0)
1332192282Sqingli		goto cleanup;
1333192282Sqingli
1334192282Sqingli	rt = rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL);
1335192282Sqingli	if (rt != NULL && rt->rt_gateway != NULL &&
1336192282Sqingli	    (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1337192282Sqingli		    &ia->ia_addr.sin6_addr,
1338192282Sqingli		    sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1339192282Sqingli		/*
1340192282Sqingli		 * if no more IPv6 address exists on this interface
1341192282Sqingli		 * then remove the multicast address route
1342192282Sqingli		 */
1343192282Sqingli		if (ifa0 == NULL) {
1344192282Sqingli			memcpy(&mltaddr.sin6_addr, &satosin6(rt_key(rt))->sin6_addr,
1345192282Sqingli			       sizeof(mltaddr.sin6_addr));
1346192282Sqingli
1347192282Sqingli			RTFREE_LOCKED(rt);
1348192282Sqingli			error = rtrequest(RTM_DELETE, (struct sockaddr *)&mltaddr,
1349192282Sqingli					  (struct sockaddr *)&ia->ia_addr,
1350192282Sqingli					  (struct sockaddr *)&mltmask, RTF_UP,
1351192282Sqingli					  (struct rtentry **)0);
1352192282Sqingli
1353192282Sqingli			if (error)
1354192282Sqingli				log(LOG_INFO, "in6_purgeaddr: node-local all-nodes"
1355192282Sqingli				    "multicast address deletion error\n");
1356192282Sqingli		} else {
1357192282Sqingli			/*
1358192282Sqingli			 * replace the gateway of the route
1359192282Sqingli			 */
1360192282Sqingli			struct sockaddr_in6 sa;
1361192282Sqingli
1362192282Sqingli			bzero(&sa, sizeof(sa));
1363192282Sqingli			sa.sin6_len = sizeof(struct sockaddr_in6);
1364192282Sqingli			sa.sin6_family = AF_INET6;
1365192282Sqingli			memcpy(&sa.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1366192282Sqingli			       sizeof(sa.sin6_addr));
1367192282Sqingli			in6_setscope(&sa.sin6_addr, ifa0->ifa_ifp, NULL);
1368192282Sqingli			memcpy(rt->rt_gateway, &sa, sizeof(sa));
1369192282Sqingli			RTFREE_LOCKED(rt);
1370192282Sqingli		}
1371192282Sqingli	} else {
1372192282Sqingli		if (rt != NULL)
1373192282Sqingli			RTFREE_LOCKED(rt);
1374192282Sqingli	}
1375192282Sqingli
1376192282Sqinglicleanup:
1377192282Sqingli
1378192282Sqingli	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1379192282Sqingli	if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1380192282Sqingli		int error;
1381192282Sqingli		struct sockaddr *dstaddr;
1382192282Sqingli
1383192282Sqingli		/*
1384192282Sqingli		 * use the interface address if configuring an
1385192282Sqingli		 * interface address with a /128 prefix len
1386192282Sqingli		 */
1387192282Sqingli		if (ia->ia_dstaddr.sin6_family == AF_INET6)
1388192282Sqingli			dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
1389192282Sqingli		else
1390192282Sqingli			dstaddr = (struct sockaddr *)&ia->ia_addr;
1391192282Sqingli
1392192282Sqingli		error = rtrequest(RTM_DELETE,
1393192282Sqingli		    (struct sockaddr *)dstaddr,
1394192282Sqingli		    (struct sockaddr *)&ia->ia_addr,
1395192282Sqingli		    (struct sockaddr *)&ia->ia_prefixmask,
1396192282Sqingli		    ia->ia_flags | RTF_HOST, NULL);
1397192282Sqingli		if (error != 0)
1398192282Sqingli			return;
1399192282Sqingli		ia->ia_flags &= ~IFA_ROUTE;
1400192282Sqingli	}
1401194760Srwatson	if (ifa0 != NULL)
1402194760Srwatson		ifa_free(ifa0);
1403192282Sqingli
140478064Sume	in6_unlink_ifa(ia, ifp);
140578064Sume}
140678064Sume
140778064Sumestatic void
1408171259Sdelphijin6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
140978064Sume{
141078064Sume	int	s = splnet();
141178064Sume
1412191323Srwatson	IF_ADDR_LOCK(ifp);
1413191340Srwatson	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1414191323Srwatson	IF_ADDR_UNLOCK(ifp);
1415194760Srwatson	ifa_free(&ia->ia_ifa);			/* if_addrhead */
141662587Sitojun
1417195102Srwatson	/*
1418195102Srwatson	 * Defer the release of what might be the last reference to the
1419195102Srwatson	 * in6_ifaddr so that it can't be freed before the remainder of the
1420195102Srwatson	 * cleanup.
1421195102Srwatson	 */
1422194971Srwatson	IN6_IFADDR_WLOCK();
1423194907Srwatson	TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1424194971Srwatson	IN6_IFADDR_WUNLOCK();
142562587Sitojun
142662587Sitojun	/*
1427151915Ssuz	 * Release the reference to the base prefix.  There should be a
1428151915Ssuz	 * positive reference.
142962587Sitojun	 */
1430194907Srwatson	if (ia->ia6_ndpr == NULL) {
1431151915Ssuz		nd6log((LOG_NOTICE,
1432151915Ssuz		    "in6_unlink_ifa: autoconf'ed address "
1433194907Srwatson		    "%p has no prefix\n", ia));
1434151915Ssuz	} else {
1435194907Srwatson		ia->ia6_ndpr->ndpr_refcnt--;
1436194907Srwatson		ia->ia6_ndpr = NULL;
1437151915Ssuz	}
143862587Sitojun
1439151915Ssuz	/*
1440151915Ssuz	 * Also, if the address being removed is autoconf'ed, call
1441151915Ssuz	 * pfxlist_onlink_check() since the release might affect the status of
1442171260Sdelphij	 * other (detached) addresses.
1443151915Ssuz	 */
1444194907Srwatson	if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
144578064Sume		pfxlist_onlink_check();
144662587Sitojun	}
1447195102Srwatson	ifa_free(&ia->ia_ifa);			/* in6_ifaddrhead */
144878064Sume	splx(s);
144962587Sitojun}
145062587Sitojun
145178064Sumevoid
1452171259Sdelphijin6_purgeif(struct ifnet *ifp)
145378064Sume{
145478064Sume	struct ifaddr *ifa, *nifa;
145578064Sume
1456191340Srwatson	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
145778064Sume		if (ifa->ifa_addr->sa_family != AF_INET6)
145878064Sume			continue;
145978064Sume		in6_purgeaddr(ifa);
146078064Sume	}
146178064Sume
146278064Sume	in6_ifdetach(ifp);
146378064Sume}
146478064Sume
146553541Sshin/*
146653541Sshin * SIOC[GAD]LIFADDR.
146762744Sgrog *	SIOCGLIFADDR: get first address. (?)
146853541Sshin *	SIOCGLIFADDR with IFLR_PREFIX:
146953541Sshin *		get first address that matches the specified prefix.
147053541Sshin *	SIOCALIFADDR: add the specified address.
147153541Sshin *	SIOCALIFADDR with IFLR_PREFIX:
147253541Sshin *		add the specified prefix, filling hostid part from
147353541Sshin *		the first link-local address.  prefixlen must be <= 64.
147453541Sshin *	SIOCDLIFADDR: delete the specified address.
147553541Sshin *	SIOCDLIFADDR with IFLR_PREFIX:
147653541Sshin *		delete the first address that matches the specified prefix.
147753541Sshin * return values:
147853541Sshin *	EINVAL on invalid parameters
147953541Sshin *	EADDRNOTAVAIL on prefix match failed/specified address not found
148053541Sshin *	other values may be returned from in6_ioctl()
148153541Sshin *
148253541Sshin * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
148353541Sshin * this is to accomodate address naming scheme other than RFC2374,
148453541Sshin * in the future.
148553541Sshin * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
148653541Sshin * address encoding scheme. (see figure on page 8)
148753541Sshin */
148853541Sshinstatic int
1489171259Sdelphijin6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1490171259Sdelphij    struct ifnet *ifp, struct thread *td)
149153541Sshin{
149253541Sshin	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
149353541Sshin	struct ifaddr *ifa;
149462587Sitojun	struct sockaddr *sa;
149553541Sshin
149653541Sshin	/* sanity checks */
149753541Sshin	if (!data || !ifp) {
149853541Sshin		panic("invalid argument to in6_lifaddr_ioctl");
1499120891Sume		/* NOTREACHED */
150053541Sshin	}
150153541Sshin
150253541Sshin	switch (cmd) {
150353541Sshin	case SIOCGLIFADDR:
150453541Sshin		/* address must be specified on GET with IFLR_PREFIX */
150553541Sshin		if ((iflr->flags & IFLR_PREFIX) == 0)
150653541Sshin			break;
150795023Ssuz		/* FALLTHROUGH */
150853541Sshin	case SIOCALIFADDR:
150953541Sshin	case SIOCDLIFADDR:
151053541Sshin		/* address must be specified on ADD and DELETE */
151162587Sitojun		sa = (struct sockaddr *)&iflr->addr;
151262587Sitojun		if (sa->sa_family != AF_INET6)
151353541Sshin			return EINVAL;
151462587Sitojun		if (sa->sa_len != sizeof(struct sockaddr_in6))
151553541Sshin			return EINVAL;
151653541Sshin		/* XXX need improvement */
151762587Sitojun		sa = (struct sockaddr *)&iflr->dstaddr;
151862587Sitojun		if (sa->sa_family && sa->sa_family != AF_INET6)
151953541Sshin			return EINVAL;
152062587Sitojun		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
152153541Sshin			return EINVAL;
152253541Sshin		break;
152395023Ssuz	default: /* shouldn't happen */
152462587Sitojun#if 0
152562587Sitojun		panic("invalid cmd to in6_lifaddr_ioctl");
152695023Ssuz		/* NOTREACHED */
152762587Sitojun#else
152853541Sshin		return EOPNOTSUPP;
152962587Sitojun#endif
153053541Sshin	}
153153541Sshin	if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
153253541Sshin		return EINVAL;
153353541Sshin
153453541Sshin	switch (cmd) {
153553541Sshin	case SIOCALIFADDR:
153653541Sshin	    {
153753541Sshin		struct in6_aliasreq ifra;
153853541Sshin		struct in6_addr *hostid = NULL;
153953541Sshin		int prefixlen;
154053541Sshin
1541194760Srwatson		ifa = NULL;
154253541Sshin		if ((iflr->flags & IFLR_PREFIX) != 0) {
154353541Sshin			struct sockaddr_in6 *sin6;
154453541Sshin
154553541Sshin			/*
154653541Sshin			 * hostid is to fill in the hostid part of the
154753541Sshin			 * address.  hostid points to the first link-local
154853541Sshin			 * address attached to the interface.
154953541Sshin			 */
155062587Sitojun			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
155153541Sshin			if (!ifa)
155253541Sshin				return EADDRNOTAVAIL;
155353541Sshin			hostid = IFA_IN6(ifa);
155453541Sshin
1555171260Sdelphij			/* prefixlen must be <= 64. */
155653541Sshin			if (64 < iflr->prefixlen)
155753541Sshin				return EINVAL;
155853541Sshin			prefixlen = iflr->prefixlen;
155953541Sshin
156053541Sshin			/* hostid part must be zero. */
156153541Sshin			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1562126552Sume			if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1563126552Sume			    sin6->sin6_addr.s6_addr32[3] != 0) {
156453541Sshin				return EINVAL;
156553541Sshin			}
156653541Sshin		} else
156753541Sshin			prefixlen = iflr->prefixlen;
156853541Sshin
156953541Sshin		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
157053541Sshin		bzero(&ifra, sizeof(ifra));
1571120891Sume		bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
157253541Sshin
157362587Sitojun		bcopy(&iflr->addr, &ifra.ifra_addr,
1574120891Sume		    ((struct sockaddr *)&iflr->addr)->sa_len);
157553541Sshin		if (hostid) {
157653541Sshin			/* fill in hostid part */
157753541Sshin			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1578120891Sume			    hostid->s6_addr32[2];
157953541Sshin			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1580120891Sume			    hostid->s6_addr32[3];
158153541Sshin		}
158253541Sshin
1583120891Sume		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
158453541Sshin			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1585120891Sume			    ((struct sockaddr *)&iflr->dstaddr)->sa_len);
158653541Sshin			if (hostid) {
158753541Sshin				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1588120891Sume				    hostid->s6_addr32[2];
158953541Sshin				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1590120891Sume				    hostid->s6_addr32[3];
159153541Sshin			}
159253541Sshin		}
1593194760Srwatson		if (ifa != NULL)
1594194760Srwatson			ifa_free(ifa);
159553541Sshin
159653541Sshin		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1597121168Sume		in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
159853541Sshin
159953541Sshin		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
160083366Sjulian		return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
160153541Sshin	    }
160253541Sshin	case SIOCGLIFADDR:
160353541Sshin	case SIOCDLIFADDR:
160453541Sshin	    {
160553541Sshin		struct in6_ifaddr *ia;
160653541Sshin		struct in6_addr mask, candidate, match;
160753541Sshin		struct sockaddr_in6 *sin6;
160853541Sshin		int cmp;
160953541Sshin
161053541Sshin		bzero(&mask, sizeof(mask));
161153541Sshin		if (iflr->flags & IFLR_PREFIX) {
161253541Sshin			/* lookup a prefix rather than address. */
1613121168Sume			in6_prefixlen2mask(&mask, iflr->prefixlen);
161453541Sshin
161553541Sshin			sin6 = (struct sockaddr_in6 *)&iflr->addr;
161653541Sshin			bcopy(&sin6->sin6_addr, &match, sizeof(match));
161753541Sshin			match.s6_addr32[0] &= mask.s6_addr32[0];
161853541Sshin			match.s6_addr32[1] &= mask.s6_addr32[1];
161953541Sshin			match.s6_addr32[2] &= mask.s6_addr32[2];
162053541Sshin			match.s6_addr32[3] &= mask.s6_addr32[3];
162153541Sshin
162253541Sshin			/* if you set extra bits, that's wrong */
162353541Sshin			if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
162453541Sshin				return EINVAL;
162553541Sshin
162653541Sshin			cmp = 1;
162753541Sshin		} else {
162853541Sshin			if (cmd == SIOCGLIFADDR) {
162953541Sshin				/* on getting an address, take the 1st match */
163095023Ssuz				cmp = 0;	/* XXX */
163153541Sshin			} else {
163253541Sshin				/* on deleting an address, do exact match */
1633121168Sume				in6_prefixlen2mask(&mask, 128);
163453541Sshin				sin6 = (struct sockaddr_in6 *)&iflr->addr;
163553541Sshin				bcopy(&sin6->sin6_addr, &match, sizeof(match));
163653541Sshin
163753541Sshin				cmp = 1;
163853541Sshin			}
163953541Sshin		}
164053541Sshin
1641191323Srwatson		IF_ADDR_LOCK(ifp);
1642191340Srwatson		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
164353541Sshin			if (ifa->ifa_addr->sa_family != AF_INET6)
164453541Sshin				continue;
164553541Sshin			if (!cmp)
164653541Sshin				break;
164778064Sume
164878064Sume			/*
164978064Sume			 * XXX: this is adhoc, but is necessary to allow
165078064Sume			 * a user to specify fe80::/64 (not /10) for a
165178064Sume			 * link-local address.
165278064Sume			 */
1653148385Sume			bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1654148385Sume			in6_clearscope(&candidate);
165553541Sshin			candidate.s6_addr32[0] &= mask.s6_addr32[0];
165653541Sshin			candidate.s6_addr32[1] &= mask.s6_addr32[1];
165753541Sshin			candidate.s6_addr32[2] &= mask.s6_addr32[2];
165853541Sshin			candidate.s6_addr32[3] &= mask.s6_addr32[3];
165953541Sshin			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
166053541Sshin				break;
166153541Sshin		}
1662191323Srwatson		IF_ADDR_UNLOCK(ifp);
166353541Sshin		if (!ifa)
166453541Sshin			return EADDRNOTAVAIL;
166553541Sshin		ia = ifa2ia6(ifa);
166653541Sshin
166753541Sshin		if (cmd == SIOCGLIFADDR) {
1668148385Sume			int error;
166978064Sume
167053541Sshin			/* fill in the if_laddrreq structure */
167153541Sshin			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1672148385Sume			error = sa6_recoverscope(
1673148385Sume			    (struct sockaddr_in6 *)&iflr->addr);
1674148385Sume			if (error != 0)
1675148385Sume				return (error);
1676148385Sume
167753541Sshin			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
167853541Sshin				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1679120891Sume				    ia->ia_dstaddr.sin6_len);
1680148385Sume				error = sa6_recoverscope(
1681148385Sume				    (struct sockaddr_in6 *)&iflr->dstaddr);
1682148385Sume				if (error != 0)
1683148385Sume					return (error);
168453541Sshin			} else
168553541Sshin				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
168653541Sshin
168753541Sshin			iflr->prefixlen =
1688120891Sume			    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
168953541Sshin
169095023Ssuz			iflr->flags = ia->ia6_flags;	/* XXX */
169153541Sshin
169253541Sshin			return 0;
169353541Sshin		} else {
169453541Sshin			struct in6_aliasreq ifra;
169553541Sshin
169653541Sshin			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
169753541Sshin			bzero(&ifra, sizeof(ifra));
169853541Sshin			bcopy(iflr->iflr_name, ifra.ifra_name,
1699120891Sume			    sizeof(ifra.ifra_name));
170053541Sshin
170153541Sshin			bcopy(&ia->ia_addr, &ifra.ifra_addr,
1702120891Sume			    ia->ia_addr.sin6_len);
170353541Sshin			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
170453541Sshin				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1705120891Sume				    ia->ia_dstaddr.sin6_len);
170662587Sitojun			} else {
170762587Sitojun				bzero(&ifra.ifra_dstaddr,
170862587Sitojun				    sizeof(ifra.ifra_dstaddr));
170953541Sshin			}
171053541Sshin			bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1711120891Sume			    ia->ia_prefixmask.sin6_len);
171253541Sshin
171353541Sshin			ifra.ifra_flags = ia->ia6_flags;
171453541Sshin			return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1715120891Sume			    ifp, td);
171653541Sshin		}
171753541Sshin	    }
171853541Sshin	}
171953541Sshin
172095023Ssuz	return EOPNOTSUPP;	/* just for safety */
172153541Sshin}
172253541Sshin
172353541Sshin/*
172453541Sshin * Initialize an interface's intetnet6 address
172553541Sshin * and routing table entry.
172653541Sshin */
172778064Sumestatic int
1728171259Sdelphijin6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1729171259Sdelphij    struct sockaddr_in6 *sin6, int newhost)
173053541Sshin{
173178064Sume	int	error = 0, plen, ifacount = 0;
173253541Sshin	int	s = splimp();
173378064Sume	struct ifaddr *ifa;
173453541Sshin
173553541Sshin	/*
173653541Sshin	 * Give the interface a chance to initialize
173753541Sshin	 * if this is its first address,
173853541Sshin	 * and to validate the address if necessary.
173953541Sshin	 */
1740191323Srwatson	IF_ADDR_LOCK(ifp);
1741191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
174278064Sume		if (ifa->ifa_addr->sa_family != AF_INET6)
174378064Sume			continue;
174478064Sume		ifacount++;
174578064Sume	}
1746191323Srwatson	IF_ADDR_UNLOCK(ifp);
174778064Sume
174878064Sume	ia->ia_addr = *sin6;
174978064Sume
1750146883Siedowse	if (ifacount <= 1 && ifp->if_ioctl) {
1751146883Siedowse		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1752146883Siedowse		if (error) {
1753146883Siedowse			splx(s);
1754146883Siedowse			return (error);
1755146883Siedowse		}
175653541Sshin	}
175778064Sume	splx(s);
175853541Sshin
175978064Sume	ia->ia_ifa.ifa_metric = ifp->if_metric;
176053541Sshin
176178064Sume	/* we could do in(6)_socktrim here, but just omit it at this moment. */
176278064Sume
176353541Sshin	/*
176478064Sume	 * Special case:
1765124337Sume	 * If a new destination address is specified for a point-to-point
176678064Sume	 * interface, install a route to the destination as an interface
1767186119Sqingli	 * direct route.
1768124337Sume	 * XXX: the logic below rejects assigning multiple addresses on a p2p
1769159390Sgnn	 * interface that share the same destination.
177053541Sshin	 */
177178064Sume	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1772196152Sqingli	if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1773196152Sqingli	    ia->ia_dstaddr.sin6_family == AF_INET6) {
1774159390Sgnn		int rtflags = RTF_UP | RTF_HOST;
1775159390Sgnn
1776178888Sjulian		error = rtrequest(RTM_ADD,
1777196152Sqingli		    (struct sockaddr *)&ia->ia_dstaddr,
1778159390Sgnn		    (struct sockaddr *)&ia->ia_addr,
1779159390Sgnn		    (struct sockaddr *)&ia->ia_prefixmask,
1780186708Sqingli		    ia->ia_flags | rtflags, NULL);
1781159390Sgnn		if (error != 0)
1782120856Sume			return (error);
178378064Sume		ia->ia_flags |= IFA_ROUTE;
178453541Sshin	}
178553541Sshin
1786195643Sqingli	/*
1787195643Sqingli	 * add a loopback route to self
1788195643Sqingli	 */
1789196871Sqingli	if (!(ia->ia_flags & IFA_ROUTE)
1790196871Sqingli	    && (V_nd6_useloopback
1791196871Sqingli		|| (ifp->if_flags & IFF_LOOPBACK))) {
1792197227Sqingli		error = ifa_add_loopback_route((struct ifaddr *)ia,
1793197227Sqingli				       (struct sockaddr *)&ia->ia_addr);
1794201282Sqingli		if (error == 0)
1795201282Sqingli			ia->ia_flags |= IFA_RTSELF;
1796195643Sqingli	}
1797195643Sqingli
179895023Ssuz	/* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1799186119Sqingli	if (newhost) {
1800186119Sqingli		struct llentry *ln;
1801192282Sqingli		struct rtentry rt;
1802192282Sqingli		struct sockaddr_dl gateway;
1803192282Sqingli		struct sockaddr_in6 mask, addr;
180453541Sshin
1805186119Sqingli		IF_AFDATA_LOCK(ifp);
1806186119Sqingli		ia->ia_ifa.ifa_rtrequest = NULL;
1807186119Sqingli
1808186119Sqingli		/* XXX QL
1809186119Sqingli		 * we need to report rt_newaddrmsg
1810186119Sqingli		 */
1811186119Sqingli		ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR | LLE_EXCLUSIVE),
1812186119Sqingli		    (struct sockaddr *)&ia->ia_addr);
1813186119Sqingli		IF_AFDATA_UNLOCK(ifp);
1814186158Skmacy		if (ln != NULL) {
1815186119Sqingli			ln->la_expire = 0;  /* for IPv6 this means permanent */
1816186119Sqingli			ln->ln_state = ND6_LLINFO_REACHABLE;
1817192282Sqingli			/*
1818192282Sqingli			 * initialize for rtmsg generation
1819192282Sqingli			 */
1820192282Sqingli			bzero(&gateway, sizeof(gateway));
1821192282Sqingli			gateway.sdl_len = sizeof(gateway);
1822192282Sqingli			gateway.sdl_family = AF_LINK;
1823192282Sqingli			gateway.sdl_nlen = 0;
1824192282Sqingli			gateway.sdl_alen = 6;
1825192282Sqingli			memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned, sizeof(ln->ll_addr));
1826192282Sqingli			/* */
1827186119Sqingli			LLE_WUNLOCK(ln);
1828186119Sqingli		}
1829192282Sqingli
1830192282Sqingli		bzero(&rt, sizeof(rt));
1831192282Sqingli		rt.rt_gateway = (struct sockaddr *)&gateway;
1832192282Sqingli		memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
1833192282Sqingli		memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
1834192282Sqingli		rt_mask(&rt) = (struct sockaddr *)&mask;
1835192282Sqingli		rt_key(&rt) = (struct sockaddr *)&addr;
1836192282Sqingli		rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC;
1837192282Sqingli		rt_newaddrmsg(RTM_ADD, &ia->ia_ifa, 0, &rt);
1838186119Sqingli	}
1839186119Sqingli
1840120856Sume	return (error);
184153541Sshin}
184253541Sshin
184353541Sshin/*
184453541Sshin * Find an IPv6 interface link-local address specific to an interface.
1845194760Srwatson * ifaddr is returned referenced.
184653541Sshin */
184753541Sshinstruct in6_ifaddr *
1848171259Sdelphijin6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
184953541Sshin{
185078064Sume	struct ifaddr *ifa;
185153541Sshin
1852191323Srwatson	IF_ADDR_LOCK(ifp);
1853191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
185453541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
185553541Sshin			continue;
185662587Sitojun		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
185762587Sitojun			if ((((struct in6_ifaddr *)ifa)->ia6_flags &
185862587Sitojun			     ignoreflags) != 0)
185962587Sitojun				continue;
1860194760Srwatson			ifa_ref(ifa);
186153541Sshin			break;
186262587Sitojun		}
186353541Sshin	}
1864191323Srwatson	IF_ADDR_UNLOCK(ifp);
186553541Sshin
1866120856Sume	return ((struct in6_ifaddr *)ifa);
186753541Sshin}
186853541Sshin
186953541Sshin
187053541Sshin/*
187153541Sshin * find the internet address corresponding to a given interface and address.
1872194760Srwatson * ifaddr is returned referenced.
187353541Sshin */
187453541Sshinstruct in6_ifaddr *
1875171259Sdelphijin6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
187653541Sshin{
187778064Sume	struct ifaddr *ifa;
187853541Sshin
1879191337Srwatson	IF_ADDR_LOCK(ifp);
1880191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
188153541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
188253541Sshin			continue;
1883194760Srwatson		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1884194760Srwatson			ifa_ref(ifa);
188553541Sshin			break;
1886194760Srwatson		}
188753541Sshin	}
1888191337Srwatson	IF_ADDR_UNLOCK(ifp);
188953541Sshin
1890120856Sume	return ((struct in6_ifaddr *)ifa);
189153541Sshin}
189253541Sshin
189353541Sshin/*
1894165118Sbz * Convert IP6 address to printable (loggable) representation. Caller
1895165118Sbz * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
189653541Sshin */
189753541Sshinstatic char digits[] = "0123456789abcdef";
189853541Sshinchar *
1899165118Sbzip6_sprintf(char *ip6buf, const struct in6_addr *addr)
190053541Sshin{
1901208284Salfred	int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
190278064Sume	char *cp;
1903126552Sume	const u_int16_t *a = (const u_int16_t *)addr;
1904126552Sume	const u_int8_t *d;
1905165287Sbz	int dcolon = 0, zero = 0;
190653541Sshin
1907165118Sbz	cp = ip6buf;
190853541Sshin
190953541Sshin	for (i = 0; i < 8; i++) {
1910208284Salfred		if (*(a + i) == 0) {
1911208284Salfred			cnt++;
1912208284Salfred			if (cnt == 1)
1913208284Salfred				idx = i;
1914208284Salfred		}
1915208284Salfred		else if (maxcnt < cnt) {
1916208284Salfred			maxcnt = cnt;
1917208284Salfred			index = idx;
1918208284Salfred			cnt = 0;
1919208284Salfred		}
1920208284Salfred	}
1921208284Salfred	if (maxcnt < cnt) {
1922208284Salfred		maxcnt = cnt;
1923208284Salfred		index = idx;
1924208284Salfred	}
1925208284Salfred
1926208284Salfred	for (i = 0; i < 8; i++) {
192753541Sshin		if (dcolon == 1) {
192853541Sshin			if (*a == 0) {
192953541Sshin				if (i == 7)
193053541Sshin					*cp++ = ':';
193153541Sshin				a++;
193253541Sshin				continue;
193353541Sshin			} else
193453541Sshin				dcolon = 2;
193553541Sshin		}
193653541Sshin		if (*a == 0) {
1937208284Salfred			if (dcolon == 0 && *(a + 1) == 0 && i == index) {
193853541Sshin				if (i == 0)
193953541Sshin					*cp++ = ':';
194053541Sshin				*cp++ = ':';
194153541Sshin				dcolon = 1;
194253541Sshin			} else {
194353541Sshin				*cp++ = '0';
194453541Sshin				*cp++ = ':';
194553541Sshin			}
194653541Sshin			a++;
194753541Sshin			continue;
194853541Sshin		}
194991346Salfred		d = (const u_char *)a;
1950165287Sbz		/* Try to eliminate leading zeros in printout like in :0001. */
1951165287Sbz		zero = 1;
1952165287Sbz		*cp = digits[*d >> 4];
1953165287Sbz		if (*cp != '0') {
1954165287Sbz			zero = 0;
1955165287Sbz			cp++;
1956165287Sbz		}
1957165287Sbz		*cp = digits[*d++ & 0xf];
1958165287Sbz		if (zero == 0 || (*cp != '0')) {
1959165287Sbz			zero = 0;
1960165287Sbz			cp++;
1961165287Sbz		}
1962165287Sbz		*cp = digits[*d >> 4];
1963165287Sbz		if (zero == 0 || (*cp != '0')) {
1964165287Sbz			zero = 0;
1965165287Sbz			cp++;
1966165287Sbz		}
196753541Sshin		*cp++ = digits[*d & 0xf];
196853541Sshin		*cp++ = ':';
196953541Sshin		a++;
197053541Sshin	}
1971165118Sbz	*--cp = '\0';
1972165118Sbz	return (ip6buf);
197353541Sshin}
197453541Sshin
197553541Sshinint
1976171259Sdelphijin6_localaddr(struct in6_addr *in6)
197753541Sshin{
197853541Sshin	struct in6_ifaddr *ia;
197953541Sshin
198053541Sshin	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
198153541Sshin		return 1;
198253541Sshin
1983194971Srwatson	IN6_IFADDR_RLOCK();
1984194907Srwatson	TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
198553541Sshin		if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1986120891Sume		    &ia->ia_prefixmask.sin6_addr)) {
1987194971Srwatson			IN6_IFADDR_RUNLOCK();
198853541Sshin			return 1;
1989120891Sume		}
1990120891Sume	}
1991194971Srwatson	IN6_IFADDR_RUNLOCK();
199253541Sshin
199353541Sshin	return (0);
199453541Sshin}
199553541Sshin
199678064Sumeint
1997171259Sdelphijin6_is_addr_deprecated(struct sockaddr_in6 *sa6)
199878064Sume{
199978064Sume	struct in6_ifaddr *ia;
200078064Sume
2001194971Srwatson	IN6_IFADDR_RLOCK();
2002194907Srwatson	TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
200378064Sume		if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
200478064Sume				       &sa6->sin6_addr) &&
2005194971Srwatson		    (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) {
2006194971Srwatson			IN6_IFADDR_RUNLOCK();
2007120856Sume			return (1); /* true */
2008194971Srwatson		}
200978064Sume
201078064Sume		/* XXX: do we still have to go thru the rest of the list? */
201178064Sume	}
2012194971Srwatson	IN6_IFADDR_RUNLOCK();
201378064Sume
2014120856Sume	return (0);		/* false */
201578064Sume}
201678064Sume
201753541Sshin/*
201853541Sshin * return length of part which dst and src are equal
201953541Sshin * hard coding...
202053541Sshin */
202153541Sshinint
2022171259Sdelphijin6_matchlen(struct in6_addr *src, struct in6_addr *dst)
202353541Sshin{
202453541Sshin	int match = 0;
202553541Sshin	u_char *s = (u_char *)src, *d = (u_char *)dst;
202653541Sshin	u_char *lim = s + 16, r;
202753541Sshin
202853541Sshin	while (s < lim)
202953541Sshin		if ((r = (*d++ ^ *s++)) != 0) {
203053541Sshin			while (r < 128) {
203153541Sshin				match++;
203253541Sshin				r <<= 1;
203353541Sshin			}
203453541Sshin			break;
203553541Sshin		} else
203653541Sshin			match += 8;
203753541Sshin	return match;
203853541Sshin}
203953541Sshin
204062587Sitojun/* XXX: to be scope conscious */
204153541Sshinint
2042171259Sdelphijin6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
204353541Sshin{
204453541Sshin	int bytelen, bitlen;
204553541Sshin
204653541Sshin	/* sanity check */
204753541Sshin	if (0 > len || len > 128) {
204853541Sshin		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
204953541Sshin		    len);
2050120856Sume		return (0);
205153541Sshin	}
205253541Sshin
205353541Sshin	bytelen = len / 8;
205453541Sshin	bitlen = len % 8;
205553541Sshin
205653541Sshin	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2057120856Sume		return (0);
2058126184Scperciva	if (bitlen != 0 &&
2059126184Scperciva	    p1->s6_addr[bytelen] >> (8 - bitlen) !=
206053541Sshin	    p2->s6_addr[bytelen] >> (8 - bitlen))
2061120856Sume		return (0);
206253541Sshin
2063120856Sume	return (1);
206453541Sshin}
206553541Sshin
206653541Sshinvoid
2067171259Sdelphijin6_prefixlen2mask(struct in6_addr *maskp, int len)
206853541Sshin{
206953541Sshin	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
207053541Sshin	int bytelen, bitlen, i;
207153541Sshin
207253541Sshin	/* sanity check */
207353541Sshin	if (0 > len || len > 128) {
207453541Sshin		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
207553541Sshin		    len);
207653541Sshin		return;
207753541Sshin	}
207853541Sshin
207953541Sshin	bzero(maskp, sizeof(*maskp));
208053541Sshin	bytelen = len / 8;
208153541Sshin	bitlen = len % 8;
208253541Sshin	for (i = 0; i < bytelen; i++)
208353541Sshin		maskp->s6_addr[i] = 0xff;
208453541Sshin	if (bitlen)
208553541Sshin		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
208653541Sshin}
208753541Sshin
208853541Sshin/*
208953541Sshin * return the best address out of the same scope. if no address was
209053541Sshin * found, return the first valid address from designated IF.
209153541Sshin */
209253541Sshinstruct in6_ifaddr *
2093171259Sdelphijin6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
209453541Sshin{
209553541Sshin	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
209653541Sshin	struct ifaddr *ifa;
209753541Sshin	struct in6_ifaddr *besta = 0;
209895023Ssuz	struct in6_ifaddr *dep[2];	/* last-resort: deprecated */
209953541Sshin
210053541Sshin	dep[0] = dep[1] = NULL;
210153541Sshin
210253541Sshin	/*
210353541Sshin	 * We first look for addresses in the same scope.
210453541Sshin	 * If there is one, return it.
210553541Sshin	 * If two or more, return one which matches the dst longest.
210653541Sshin	 * If none, return one of global addresses assigned other ifs.
210753541Sshin	 */
2108191323Srwatson	IF_ADDR_LOCK(ifp);
2109191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
211053541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
211153541Sshin			continue;
211253541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
211353541Sshin			continue; /* XXX: is there any case to allow anycast? */
211453541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
211553541Sshin			continue; /* don't use this interface */
211653541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
211753541Sshin			continue;
211853541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2119181803Sbz			if (V_ip6_use_deprecated)
212053541Sshin				dep[0] = (struct in6_ifaddr *)ifa;
212153541Sshin			continue;
212253541Sshin		}
212353541Sshin
212453541Sshin		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
212553541Sshin			/*
212653541Sshin			 * call in6_matchlen() as few as possible
212753541Sshin			 */
212853541Sshin			if (besta) {
212953541Sshin				if (blen == -1)
213053541Sshin					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
213153541Sshin				tlen = in6_matchlen(IFA_IN6(ifa), dst);
213253541Sshin				if (tlen > blen) {
213353541Sshin					blen = tlen;
213453541Sshin					besta = (struct in6_ifaddr *)ifa;
213553541Sshin				}
213653541Sshin			} else
213753541Sshin				besta = (struct in6_ifaddr *)ifa;
213853541Sshin		}
213953541Sshin	}
2140191323Srwatson	if (besta) {
2141194760Srwatson		ifa_ref(&besta->ia_ifa);
2142191323Srwatson		IF_ADDR_UNLOCK(ifp);
2143120856Sume		return (besta);
2144191323Srwatson	}
2145194971Srwatson	IF_ADDR_UNLOCK(ifp);
214653541Sshin
2147194971Srwatson	IN6_IFADDR_RLOCK();
2148191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
214953541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
215053541Sshin			continue;
215153541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
215253541Sshin			continue; /* XXX: is there any case to allow anycast? */
215353541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
215453541Sshin			continue; /* don't use this interface */
215553541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
215653541Sshin			continue;
215753541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2158181803Sbz			if (V_ip6_use_deprecated)
215953541Sshin				dep[1] = (struct in6_ifaddr *)ifa;
216053541Sshin			continue;
216153541Sshin		}
216253541Sshin
2163194760Srwatson		if (ifa != NULL)
2164194760Srwatson			ifa_ref(ifa);
2165194971Srwatson		IN6_IFADDR_RUNLOCK();
216653541Sshin		return (struct in6_ifaddr *)ifa;
216753541Sshin	}
2168194971Srwatson	IN6_IFADDR_RUNLOCK();
216953541Sshin
217053541Sshin	/* use the last-resort values, that are, deprecated addresses */
217153541Sshin	if (dep[0])
217253541Sshin		return dep[0];
217353541Sshin	if (dep[1])
217453541Sshin		return dep[1];
217553541Sshin
217653541Sshin	return NULL;
217753541Sshin}
217853541Sshin
217953541Sshin/*
218053541Sshin * perform DAD when interface becomes IFF_UP.
218153541Sshin */
218253541Sshinvoid
2183171259Sdelphijin6_if_up(struct ifnet *ifp)
218453541Sshin{
218553541Sshin	struct ifaddr *ifa;
218653541Sshin	struct in6_ifaddr *ia;
218753541Sshin
2188191323Srwatson	IF_ADDR_LOCK(ifp);
2189191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
219053541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
219153541Sshin			continue;
219253541Sshin		ia = (struct in6_ifaddr *)ifa;
2193151539Ssuz		if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2194151539Ssuz			/*
2195151539Ssuz			 * The TENTATIVE flag was likely set by hand
2196151539Ssuz			 * beforehand, implicitly indicating the need for DAD.
2197151539Ssuz			 * We may be able to skip the random delay in this
2198151539Ssuz			 * case, but we impose delays just in case.
2199151539Ssuz			 */
2200151539Ssuz			nd6_dad_start(ifa,
2201151539Ssuz			    arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2202151539Ssuz		}
220353541Sshin	}
2204191323Srwatson	IF_ADDR_UNLOCK(ifp);
2205151539Ssuz
2206151539Ssuz	/*
2207151539Ssuz	 * special cases, like 6to4, are handled in in6_ifattach
2208151539Ssuz	 */
2209151539Ssuz	in6_ifattach(ifp, NULL);
221053541Sshin}
221153541Sshin
221278064Sumeint
2213171259Sdelphijin6if_do_dad(struct ifnet *ifp)
221478064Sume{
221578064Sume	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2216120856Sume		return (0);
221778064Sume
2218197138Shrs	if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
2219197138Shrs		return (0);
2220197138Shrs
222178064Sume	switch (ifp->if_type) {
222278064Sume#ifdef IFT_DUMMY
222378064Sume	case IFT_DUMMY:
222478064Sume#endif
222578064Sume	case IFT_FAITH:
222678064Sume		/*
222778064Sume		 * These interfaces do not have the IFF_LOOPBACK flag,
222878064Sume		 * but loop packets back.  We do not have to do DAD on such
222978064Sume		 * interfaces.  We should even omit it, because loop-backed
223078064Sume		 * NS would confuse the DAD procedure.
223178064Sume		 */
2232120856Sume		return (0);
223378064Sume	default:
223478064Sume		/*
223578064Sume		 * Our DAD routine requires the interface up and running.
223678064Sume		 * However, some interfaces can be up before the RUNNING
223778064Sume		 * status.  Additionaly, users may try to assign addresses
223878064Sume		 * before the interface becomes up (or running).
223978064Sume		 * We simply skip DAD in such a case as a work around.
224078064Sume		 * XXX: we should rather mark "tentative" on such addresses,
224178064Sume		 * and do DAD after the interface becomes ready.
224278064Sume		 */
2243148887Srwatson		if (!((ifp->if_flags & IFF_UP) &&
2244148887Srwatson		    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2245120856Sume			return (0);
224678064Sume
2247120856Sume		return (1);
224878064Sume	}
224978064Sume}
225078064Sume
225153541Sshin/*
225253541Sshin * Calculate max IPv6 MTU through all the interfaces and store it
225353541Sshin * to in6_maxmtu.
225453541Sshin */
225553541Sshinvoid
2256171259Sdelphijin6_setmaxmtu(void)
225753541Sshin{
225853541Sshin	unsigned long maxmtu = 0;
225953541Sshin	struct ifnet *ifp;
226053541Sshin
2261196481Srwatson	IFNET_RLOCK_NOSLEEP();
2262181887Sjulian	for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
2263181888Sjulian	    ifp = TAILQ_NEXT(ifp, if_list)) {
2264121283Sume		/* this function can be called during ifnet initialization */
2265121283Sume		if (!ifp->if_afdata[AF_INET6])
2266121283Sume			continue;
226753541Sshin		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2268121283Sume		    IN6_LINKMTU(ifp) > maxmtu)
2269121283Sume			maxmtu = IN6_LINKMTU(ifp);
227053541Sshin	}
2271196481Srwatson	IFNET_RUNLOCK_NOSLEEP();
2272120891Sume	if (maxmtu)	     /* update only when maxmtu is positive */
2273181803Sbz		V_in6_maxmtu = maxmtu;
227453541Sshin}
227553541Sshin
2276151539Ssuz/*
2277151539Ssuz * Provide the length of interface identifiers to be used for the link attached
2278151539Ssuz * to the given interface.  The length should be defined in "IPv6 over
2279151539Ssuz * xxx-link" document.  Note that address architecture might also define
2280151539Ssuz * the length for a particular set of address prefixes, regardless of the
2281151539Ssuz * link type.  As clarified in rfc2462bis, those two definitions should be
2282151539Ssuz * consistent, and those really are as of August 2004.
2283151539Ssuz */
2284151539Ssuzint
2285171259Sdelphijin6_if2idlen(struct ifnet *ifp)
2286151539Ssuz{
2287151539Ssuz	switch (ifp->if_type) {
2288151539Ssuz	case IFT_ETHER:		/* RFC2464 */
2289151539Ssuz#ifdef IFT_PROPVIRTUAL
2290151539Ssuz	case IFT_PROPVIRTUAL:	/* XXX: no RFC. treat it as ether */
2291151539Ssuz#endif
2292151539Ssuz#ifdef IFT_L2VLAN
2293151539Ssuz	case IFT_L2VLAN:	/* ditto */
2294151539Ssuz#endif
2295151539Ssuz#ifdef IFT_IEEE80211
2296151539Ssuz	case IFT_IEEE80211:	/* ditto */
2297151539Ssuz#endif
2298151539Ssuz#ifdef IFT_MIP
2299151539Ssuz	case IFT_MIP:	/* ditto */
2300151539Ssuz#endif
2301219819Sjeff	case IFT_INFINIBAND:
2302151539Ssuz		return (64);
2303151539Ssuz	case IFT_FDDI:		/* RFC2467 */
2304151539Ssuz		return (64);
2305151539Ssuz	case IFT_ISO88025:	/* RFC2470 (IPv6 over Token Ring) */
2306151539Ssuz		return (64);
2307151539Ssuz	case IFT_PPP:		/* RFC2472 */
2308151539Ssuz		return (64);
2309151539Ssuz	case IFT_ARCNET:	/* RFC2497 */
2310151539Ssuz		return (64);
2311151539Ssuz	case IFT_FRELAY:	/* RFC2590 */
2312151539Ssuz		return (64);
2313151539Ssuz	case IFT_IEEE1394:	/* RFC3146 */
2314151539Ssuz		return (64);
2315151539Ssuz	case IFT_GIF:
2316151539Ssuz		return (64);	/* draft-ietf-v6ops-mech-v2-07 */
2317151539Ssuz	case IFT_LOOP:
2318151539Ssuz		return (64);	/* XXX: is this really correct? */
2319151539Ssuz	default:
2320151539Ssuz		/*
2321151539Ssuz		 * Unknown link type:
2322151539Ssuz		 * It might be controversial to use the today's common constant
2323151539Ssuz		 * of 64 for these cases unconditionally.  For full compliance,
2324151539Ssuz		 * we should return an error in this case.  On the other hand,
2325151539Ssuz		 * if we simply miss the standard for the link type or a new
2326151539Ssuz		 * standard is defined for a new link type, the IFID length
2327151539Ssuz		 * is very likely to be the common constant.  As a compromise,
2328151539Ssuz		 * we always use the constant, but make an explicit notice
2329151539Ssuz		 * indicating the "unknown" case.
2330151539Ssuz		 */
2331151539Ssuz		printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2332151539Ssuz		return (64);
2333151539Ssuz	}
2334151539Ssuz}
2335151539Ssuz
2336186119Sqingli#include <sys/sysctl.h>
2337186119Sqingli
2338186119Sqinglistruct in6_llentry {
2339186119Sqingli	struct llentry		base;
2340186119Sqingli	struct sockaddr_in6	l3_addr6;
2341186119Sqingli};
2342186119Sqingli
2343186119Sqinglistatic struct llentry *
2344186119Sqingliin6_lltable_new(const struct sockaddr *l3addr, u_int flags)
2345186119Sqingli{
2346186119Sqingli	struct in6_llentry *lle;
2347186119Sqingli
2348186119Sqingli	lle = malloc(sizeof(struct in6_llentry), M_LLTABLE,
2349186119Sqingli	    M_DONTWAIT | M_ZERO);
2350186119Sqingli	if (lle == NULL)		/* NB: caller generates msg */
2351186119Sqingli		return NULL;
2352186119Sqingli
2353186119Sqingli	lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr;
2354186119Sqingli	lle->base.lle_refcnt = 1;
2355186119Sqingli	LLE_LOCK_INIT(&lle->base);
2356216022Sbz	callout_init_rw(&lle->base.ln_timer_ch, &lle->base.lle_lock,
2357216022Sbz	    CALLOUT_RETURNUNLOCKED);
2358216022Sbz
2359186119Sqingli	return &lle->base;
2360186119Sqingli}
2361186119Sqingli
2362186119Sqingli/*
2363186119Sqingli * Deletes an address from the address table.
2364186119Sqingli * This function is called by the timer functions
2365186119Sqingli * such as arptimer() and nd6_llinfo_timer(), and
2366186119Sqingli * the caller does the locking.
2367186119Sqingli */
2368186119Sqinglistatic void
2369186119Sqingliin6_lltable_free(struct lltable *llt, struct llentry *lle)
2370186119Sqingli{
2371186150Skmacy	LLE_WUNLOCK(lle);
2372186150Skmacy	LLE_LOCK_DESTROY(lle);
2373186119Sqingli	free(lle, M_LLTABLE);
2374186119Sqingli}
2375186119Sqingli
2376192476Sqinglistatic void
2377192476Sqingliin6_lltable_prefix_free(struct lltable *llt,
2378192476Sqingli			const struct sockaddr *prefix,
2379192476Sqingli			const struct sockaddr *mask)
2380192476Sqingli{
2381192476Sqingli	const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
2382192476Sqingli	const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
2383192476Sqingli	struct llentry *lle, *next;
2384192476Sqingli	register int i;
2385192476Sqingli
2386192476Sqingli	for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
2387192476Sqingli		LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
2388192476Sqingli			if (IN6_ARE_MASKED_ADDR_EQUAL(
2389192476Sqingli				    &((struct sockaddr_in6 *)L3_ADDR(lle))->sin6_addr,
2390192476Sqingli				    &pfx->sin6_addr,
2391192476Sqingli				    &msk->sin6_addr)) {
2392206481Sbz				int canceled;
2393206481Sbz
2394206481Sbz				canceled = callout_drain(&lle->la_timer);
2395192476Sqingli				LLE_WLOCK(lle);
2396206481Sbz				if (canceled)
2397206481Sbz					LLE_REMREF(lle);
2398192476Sqingli				llentry_free(lle);
2399192476Sqingli			}
2400192476Sqingli		}
2401192476Sqingli	}
2402192476Sqingli}
2403192476Sqingli
2404186119Sqinglistatic int
2405201282Sqingliin6_lltable_rtcheck(struct ifnet *ifp,
2406201282Sqingli		    u_int flags,
2407201282Sqingli		    const struct sockaddr *l3addr)
2408186119Sqingli{
2409186119Sqingli	struct rtentry *rt;
2410186119Sqingli	char ip6buf[INET6_ADDRSTRLEN];
2411186119Sqingli
2412186119Sqingli	KASSERT(l3addr->sa_family == AF_INET6,
2413186119Sqingli	    ("sin_family %d", l3addr->sa_family));
2414186119Sqingli
2415186119Sqingli	/* XXX rtalloc1 should take a const param */
2416186119Sqingli	rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
2417186119Sqingli	if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2418186119Sqingli		struct ifaddr *ifa;
2419186119Sqingli		/*
2420186119Sqingli		 * Create an ND6 cache for an IPv6 neighbor
2421186119Sqingli		 * that is not covered by our own prefix.
2422186119Sqingli		 */
2423186119Sqingli		/* XXX ifaof_ifpforaddr should take a const param */
2424186119Sqingli		ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp);
2425186119Sqingli		if (ifa != NULL) {
2426194760Srwatson			ifa_free(ifa);
2427186119Sqingli			if (rt != NULL)
2428187946Sbz				RTFREE_LOCKED(rt);
2429186119Sqingli			return 0;
2430186119Sqingli		}
2431186119Sqingli		log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2432186119Sqingli		    ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr));
2433186119Sqingli		if (rt != NULL)
2434187946Sbz			RTFREE_LOCKED(rt);
2435186119Sqingli		return EINVAL;
2436186119Sqingli	}
2437187946Sbz	RTFREE_LOCKED(rt);
2438186119Sqingli	return 0;
2439186119Sqingli}
2440186119Sqingli
2441186119Sqinglistatic struct llentry *
2442186119Sqingliin6_lltable_lookup(struct lltable *llt, u_int flags,
2443186119Sqingli	const struct sockaddr *l3addr)
2444186119Sqingli{
2445186119Sqingli	const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2446186119Sqingli	struct ifnet *ifp = llt->llt_ifp;
2447186119Sqingli	struct llentry *lle;
2448186119Sqingli	struct llentries *lleh;
2449186119Sqingli	u_int hashkey;
2450186119Sqingli
2451186119Sqingli	IF_AFDATA_LOCK_ASSERT(ifp);
2452186119Sqingli	KASSERT(l3addr->sa_family == AF_INET6,
2453186119Sqingli	    ("sin_family %d", l3addr->sa_family));
2454186119Sqingli
2455186119Sqingli	hashkey = sin6->sin6_addr.s6_addr32[3];
2456186119Sqingli	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
2457186119Sqingli	LIST_FOREACH(lle, lleh, lle_next) {
2458186708Sqingli		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle);
2459186119Sqingli		if (lle->la_flags & LLE_DELETED)
2460186119Sqingli			continue;
2461186708Sqingli		if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr,
2462186708Sqingli			 sizeof(struct in6_addr)) == 0)
2463186119Sqingli			break;
2464186119Sqingli	}
2465186119Sqingli
2466186119Sqingli	if (lle == NULL) {
2467186119Sqingli		if (!(flags & LLE_CREATE))
2468186119Sqingli			return (NULL);
2469186119Sqingli		/*
2470186119Sqingli		 * A route that covers the given address must have
2471186119Sqingli		 * been installed 1st because we are doing a resolution,
2472186119Sqingli		 * verify this.
2473186119Sqingli		 */
2474186119Sqingli		if (!(flags & LLE_IFADDR) &&
2475201282Sqingli		    in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2476186119Sqingli			return NULL;
2477186119Sqingli
2478186119Sqingli		lle = in6_lltable_new(l3addr, flags);
2479186119Sqingli		if (lle == NULL) {
2480186119Sqingli			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2481186119Sqingli			return NULL;
2482186119Sqingli		}
2483186119Sqingli		lle->la_flags = flags & ~LLE_CREATE;
2484186119Sqingli		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
2485186119Sqingli			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
2486186119Sqingli			lle->la_flags |= (LLE_VALID | LLE_STATIC);
2487186119Sqingli		}
2488186119Sqingli
2489186119Sqingli		lle->lle_tbl  = llt;
2490186119Sqingli		lle->lle_head = lleh;
2491186119Sqingli		LIST_INSERT_HEAD(lleh, lle, lle_next);
2492186119Sqingli	} else if (flags & LLE_DELETE) {
2493186392Sqingli		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
2494186392Sqingli			LLE_WLOCK(lle);
2495186392Sqingli			lle->la_flags = LLE_DELETED;
2496186392Sqingli			LLE_WUNLOCK(lle);
2497198418Sqingli#ifdef DIAGNOSTIC
2498186392Sqingli			log(LOG_INFO, "ifaddr cache = %p  is deleted\n", lle);
2499186119Sqingli#endif
2500186392Sqingli		}
2501186119Sqingli		lle = (void *)-1;
2502186119Sqingli	}
2503186119Sqingli	if (LLE_IS_VALID(lle)) {
2504186119Sqingli		if (flags & LLE_EXCLUSIVE)
2505186119Sqingli			LLE_WLOCK(lle);
2506186119Sqingli		else
2507186119Sqingli			LLE_RLOCK(lle);
2508186119Sqingli	}
2509186119Sqingli	return (lle);
2510186119Sqingli}
2511186119Sqingli
2512186119Sqinglistatic int
2513186119Sqingliin6_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
2514186119Sqingli{
2515186119Sqingli	struct ifnet *ifp = llt->llt_ifp;
2516186119Sqingli	struct llentry *lle;
2517186119Sqingli	/* XXX stack use */
2518186119Sqingli	struct {
2519186119Sqingli		struct rt_msghdr	rtm;
2520186119Sqingli		struct sockaddr_in6	sin6;
2521186119Sqingli		/*
2522186119Sqingli		 * ndp.c assumes that sdl is word aligned
2523186119Sqingli		 */
2524186119Sqingli#ifdef __LP64__
2525186119Sqingli		uint32_t		pad;
2526186119Sqingli#endif
2527186119Sqingli		struct sockaddr_dl	sdl;
2528186119Sqingli	} ndpc;
2529186119Sqingli	int i, error;
2530186119Sqingli
2531196864Sqingli	if (ifp->if_flags & IFF_LOOPBACK)
2532196864Sqingli		return 0;
2533196864Sqingli
2534196535Srwatson	LLTABLE_LOCK_ASSERT();
2535186119Sqingli
2536186119Sqingli	error = 0;
2537186119Sqingli	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2538186119Sqingli		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
2539186119Sqingli			struct sockaddr_dl *sdl;
2540186119Sqingli
2541186119Sqingli			/* skip deleted or invalid entries */
2542186119Sqingli			if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
2543186119Sqingli				continue;
2544186980Sbz			/* Skip if jailed and not a valid IP of the prison. */
2545188144Sjamie			if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
2546186980Sbz				continue;
2547186119Sqingli			/*
2548186119Sqingli			 * produce a msg made of:
2549186119Sqingli			 *  struct rt_msghdr;
2550186119Sqingli			 *  struct sockaddr_in6 (IPv6)
2551186119Sqingli			 *  struct sockaddr_dl;
2552186119Sqingli			 */
2553186119Sqingli			bzero(&ndpc, sizeof(ndpc));
2554186119Sqingli			ndpc.rtm.rtm_msglen = sizeof(ndpc);
2555187094Sqingli			ndpc.rtm.rtm_version = RTM_VERSION;
2556187094Sqingli			ndpc.rtm.rtm_type = RTM_GET;
2557187094Sqingli			ndpc.rtm.rtm_flags = RTF_UP;
2558187094Sqingli			ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2559186119Sqingli			ndpc.sin6.sin6_family = AF_INET6;
2560186119Sqingli			ndpc.sin6.sin6_len = sizeof(ndpc.sin6);
2561186119Sqingli			bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle));
2562186119Sqingli
2563186119Sqingli			/* publish */
2564186119Sqingli			if (lle->la_flags & LLE_PUB)
2565186119Sqingli				ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2566186119Sqingli
2567186119Sqingli			sdl = &ndpc.sdl;
2568186119Sqingli			sdl->sdl_family = AF_LINK;
2569186119Sqingli			sdl->sdl_len = sizeof(*sdl);
2570186119Sqingli			sdl->sdl_alen = ifp->if_addrlen;
2571186119Sqingli			sdl->sdl_index = ifp->if_index;
2572186119Sqingli			sdl->sdl_type = ifp->if_type;
2573186119Sqingli			bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2574186119Sqingli			ndpc.rtm.rtm_rmx.rmx_expire =
2575186119Sqingli			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
2576186500Sqingli			ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2577186119Sqingli			if (lle->la_flags & LLE_STATIC)
2578186119Sqingli				ndpc.rtm.rtm_flags |= RTF_STATIC;
2579186119Sqingli			ndpc.rtm.rtm_index = ifp->if_index;
2580186119Sqingli			error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2581186119Sqingli			if (error)
2582186119Sqingli				break;
2583186119Sqingli		}
2584186119Sqingli	}
2585186119Sqingli	return error;
2586186119Sqingli}
2587186119Sqingli
2588121161Sumevoid *
2589171259Sdelphijin6_domifattach(struct ifnet *ifp)
2590121161Sume{
2591121161Sume	struct in6_ifextra *ext;
2592121161Sume
2593121161Sume	ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2594121161Sume	bzero(ext, sizeof(*ext));
2595121161Sume
2596121161Sume	ext->in6_ifstat = (struct in6_ifstat *)malloc(sizeof(struct in6_ifstat),
2597121161Sume	    M_IFADDR, M_WAITOK);
2598121161Sume	bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2599121161Sume
2600121161Sume	ext->icmp6_ifstat =
2601121161Sume	    (struct icmp6_ifstat *)malloc(sizeof(struct icmp6_ifstat),
2602121161Sume	    M_IFADDR, M_WAITOK);
2603121161Sume	bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2604121161Sume
2605121161Sume	ext->nd_ifinfo = nd6_ifattach(ifp);
2606121161Sume	ext->scope6_id = scope6_ifattach(ifp);
2607186119Sqingli	ext->lltable = lltable_init(ifp, AF_INET6);
2608186119Sqingli	if (ext->lltable != NULL) {
2609186119Sqingli		ext->lltable->llt_new = in6_lltable_new;
2610186119Sqingli		ext->lltable->llt_free = in6_lltable_free;
2611192476Sqingli		ext->lltable->llt_prefix_free = in6_lltable_prefix_free;
2612186119Sqingli		ext->lltable->llt_rtcheck = in6_lltable_rtcheck;
2613186119Sqingli		ext->lltable->llt_lookup = in6_lltable_lookup;
2614186119Sqingli		ext->lltable->llt_dump = in6_lltable_dump;
2615186119Sqingli	}
2616191672Sbms
2617191672Sbms	ext->mld_ifinfo = mld_domifattach(ifp);
2618191672Sbms
2619121161Sume	return ext;
2620121161Sume}
2621121161Sume
2622121161Sumevoid
2623171259Sdelphijin6_domifdetach(struct ifnet *ifp, void *aux)
2624121161Sume{
2625121161Sume	struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2626121161Sume
2627191672Sbms	mld_domifdetach(ifp);
2628121161Sume	scope6_ifdetach(ext->scope6_id);
2629121161Sume	nd6_ifdetach(ext->nd_ifinfo);
2630186119Sqingli	lltable_free(ext->lltable);
2631121161Sume	free(ext->in6_ifstat, M_IFADDR);
2632121161Sume	free(ext->icmp6_ifstat, M_IFADDR);
2633121161Sume	free(ext, M_IFADDR);
2634121161Sume}
2635121161Sume
263653541Sshin/*
263795023Ssuz * Convert sockaddr_in6 to sockaddr_in.  Original sockaddr_in6 must be
263853541Sshin * v4 mapped addr or v4 compat addr
263953541Sshin */
264053541Sshinvoid
264153541Sshinin6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
264253541Sshin{
2643171259Sdelphij
264453541Sshin	bzero(sin, sizeof(*sin));
264553541Sshin	sin->sin_len = sizeof(struct sockaddr_in);
264653541Sshin	sin->sin_family = AF_INET;
264753541Sshin	sin->sin_port = sin6->sin6_port;
2648120891Sume	sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
264953541Sshin}
265053541Sshin
265153541Sshin/* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
265253541Sshinvoid
265353541Sshinin6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
265453541Sshin{
265553541Sshin	bzero(sin6, sizeof(*sin6));
265653541Sshin	sin6->sin6_len = sizeof(struct sockaddr_in6);
265753541Sshin	sin6->sin6_family = AF_INET6;
265853541Sshin	sin6->sin6_port = sin->sin_port;
265953541Sshin	sin6->sin6_addr.s6_addr32[0] = 0;
266053541Sshin	sin6->sin6_addr.s6_addr32[1] = 0;
266153541Sshin	sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
266253541Sshin	sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
266353541Sshin}
266453541Sshin
266553541Sshin/* Convert sockaddr_in6 into sockaddr_in. */
266653541Sshinvoid
266753541Sshinin6_sin6_2_sin_in_sock(struct sockaddr *nam)
266853541Sshin{
266953541Sshin	struct sockaddr_in *sin_p;
267053541Sshin	struct sockaddr_in6 sin6;
267153541Sshin
267253541Sshin	/*
267353541Sshin	 * Save original sockaddr_in6 addr and convert it
267453541Sshin	 * to sockaddr_in.
267553541Sshin	 */
267653541Sshin	sin6 = *(struct sockaddr_in6 *)nam;
267753541Sshin	sin_p = (struct sockaddr_in *)nam;
267853541Sshin	in6_sin6_2_sin(sin_p, &sin6);
267953541Sshin}
268053541Sshin
268153541Sshin/* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
268253541Sshinvoid
268353541Sshinin6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
268453541Sshin{
268553541Sshin	struct sockaddr_in *sin_p;
268653541Sshin	struct sockaddr_in6 *sin6_p;
268753541Sshin
2688184205Sdes	sin6_p = malloc(sizeof *sin6_p, M_SONAME,
2689111119Simp	       M_WAITOK);
269053541Sshin	sin_p = (struct sockaddr_in *)*nam;
269153541Sshin	in6_sin_2_v4mapsin6(sin_p, sin6_p);
2692184205Sdes	free(*nam, M_SONAME);
269353541Sshin	*nam = (struct sockaddr *)sin6_p;
269453541Sshin}
2695