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$");
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>
98228571Sglebius#include <netinet/ip_carp.h>
9953541Sshin
10062587Sitojun#include <netinet/ip6.h>
10153541Sshin#include <netinet6/ip6_var.h>
10295023Ssuz#include <netinet6/nd6.h>
10353541Sshin#include <netinet6/mld6_var.h>
10462587Sitojun#include <netinet6/ip6_mroute.h>
10553541Sshin#include <netinet6/in6_ifattach.h>
10662587Sitojun#include <netinet6/scope6_var.h>
10778064Sume#include <netinet6/in6_pcb.h>
10862587Sitojun
109250251ShrsVNET_DECLARE(int, icmp6_nodeinfo_oldmcprefix);
110250251Shrs#define V_icmp6_nodeinfo_oldmcprefix	VNET(icmp6_nodeinfo_oldmcprefix)
111250251Shrs
11253541Sshin/*
11353541Sshin * Definitions of some costant IP6 addresses.
11453541Sshin */
11562587Sitojunconst struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
11662587Sitojunconst struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
11762587Sitojunconst struct in6_addr in6addr_nodelocal_allnodes =
11853541Sshin	IN6ADDR_NODELOCAL_ALLNODES_INIT;
11962587Sitojunconst struct in6_addr in6addr_linklocal_allnodes =
12053541Sshin	IN6ADDR_LINKLOCAL_ALLNODES_INIT;
12162587Sitojunconst struct in6_addr in6addr_linklocal_allrouters =
12253541Sshin	IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
123191672Sbmsconst struct in6_addr in6addr_linklocal_allv2routers =
124191672Sbms	IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT;
12553541Sshin
12662587Sitojunconst struct in6_addr in6mask0 = IN6MASK0;
12762587Sitojunconst struct in6_addr in6mask32 = IN6MASK32;
12862587Sitojunconst struct in6_addr in6mask64 = IN6MASK64;
12962587Sitojunconst struct in6_addr in6mask96 = IN6MASK96;
13062587Sitojunconst struct in6_addr in6mask128 = IN6MASK128;
13153541Sshin
132126552Sumeconst struct sockaddr_in6 sa6_any =
133126552Sume	{ sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 };
13478064Sume
135241916Sdelphijstatic int in6_lifaddr_ioctl(struct socket *, u_long, caddr_t,
136241916Sdelphij	struct ifnet *, struct thread *);
137241916Sdelphijstatic int in6_ifinit(struct ifnet *, struct in6_ifaddr *,
138241916Sdelphij	struct sockaddr_in6 *, int);
139175162Sobrienstatic void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *);
14053541Sshin
14183934Sbrooksint	(*faithprefix_p)(struct in6_addr *);
14283934Sbrooks
143226338Sglebius#define ifa2ia6(ifa)	((struct in6_ifaddr *)(ifa))
144226338Sglebius#define ia62ifa(ia6)	(&((ia6)->ia_ifa))
145120891Sume
146226338Sglebiusvoid
147226338Sglebiusin6_ifaddloop(struct ifaddr *ifa)
148226338Sglebius{
149226338Sglebius	struct sockaddr_dl gateway;
150226338Sglebius	struct sockaddr_in6 mask, addr;
151226338Sglebius	struct rtentry rt;
152226338Sglebius	struct in6_ifaddr *ia;
153226338Sglebius	struct ifnet *ifp;
154226338Sglebius	struct llentry *ln;
15578064Sume
156226338Sglebius	ia = ifa2ia6(ifa);
157226338Sglebius	ifp = ifa->ifa_ifp;
158284016Sae	/*
159284016Sae	 * initialize for rtmsg generation
160284016Sae	 */
161284016Sae	bzero(&gateway, sizeof(gateway));
162284016Sae	gateway.sdl_len = sizeof(gateway);
163284016Sae	gateway.sdl_family = AF_LINK;
164284016Sae	if (nd6_need_cache(ifp) != 0) {
165284016Sae		IF_AFDATA_LOCK(ifp);
166284016Sae		ifa->ifa_rtrequest = nd6_rtrequest;
167284016Sae		ln = lla_lookup(LLTABLE6(ifp), (LLE_CREATE | LLE_IFADDR |
168284016Sae		    LLE_EXCLUSIVE), (struct sockaddr *)&ia->ia_addr);
169284016Sae		IF_AFDATA_UNLOCK(ifp);
170284016Sae		if (ln != NULL) {
171284016Sae			ln->la_expire = 0;  /* for IPv6 this means permanent */
172284016Sae			ln->ln_state = ND6_LLINFO_REACHABLE;
173284016Sae
174284016Sae			gateway.sdl_alen = 6;
175284016Sae			memcpy(gateway.sdl_data, &ln->ll_addr.mac_aligned,
176284016Sae			    sizeof(ln->ll_addr));
177284016Sae			LLE_WUNLOCK(ln);
178284016Sae		}
179226338Sglebius	}
180226338Sglebius	bzero(&rt, sizeof(rt));
181226338Sglebius	rt.rt_gateway = (struct sockaddr *)&gateway;
182226338Sglebius	memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
183226338Sglebius	memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
184226338Sglebius	rt_mask(&rt) = (struct sockaddr *)&mask;
185226338Sglebius	rt_key(&rt) = (struct sockaddr *)&addr;
186226338Sglebius	rt.rt_flags = RTF_UP | RTF_HOST | RTF_STATIC;
187231852Sbz	/* Announce arrival of local address to all FIBs. */
188226338Sglebius	rt_newaddrmsg(RTM_ADD, ifa, 0, &rt);
189226338Sglebius}
190226338Sglebius
191226338Sglebiusvoid
192226338Sglebiusin6_ifremloop(struct ifaddr *ifa)
193226338Sglebius{
194226338Sglebius	struct sockaddr_dl gateway;
195226338Sglebius	struct sockaddr_in6 mask, addr;
196226338Sglebius	struct rtentry rt0;
197226338Sglebius	struct in6_ifaddr *ia;
198226338Sglebius	struct ifnet *ifp;
199226338Sglebius
200226338Sglebius	ia = ifa2ia6(ifa);
201226338Sglebius	ifp = ifa->ifa_ifp;
202249742Soleg	memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
203249742Soleg	memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
204249742Soleg	lltable_prefix_free(AF_INET6, (struct sockaddr *)&addr,
205249742Soleg	            (struct sockaddr *)&mask, LLE_STATIC);
206226338Sglebius
207226338Sglebius	/*
208226338Sglebius	 * initialize for rtmsg generation
209226338Sglebius	 */
210226338Sglebius	bzero(&gateway, sizeof(gateway));
211226338Sglebius	gateway.sdl_len = sizeof(gateway);
212226338Sglebius	gateway.sdl_family = AF_LINK;
213226338Sglebius	gateway.sdl_nlen = 0;
214226338Sglebius	gateway.sdl_alen = ifp->if_addrlen;
215226338Sglebius	bzero(&rt0, sizeof(rt0));
216226338Sglebius	rt0.rt_gateway = (struct sockaddr *)&gateway;
217226338Sglebius	rt_mask(&rt0) = (struct sockaddr *)&mask;
218226338Sglebius	rt_key(&rt0) = (struct sockaddr *)&addr;
219226338Sglebius	rt0.rt_flags = RTF_HOST | RTF_STATIC;
220231852Sbz	/* Announce removal of local address to all FIBs. */
221226338Sglebius	rt_newaddrmsg(RTM_DELETE, ifa, 0, &rt0);
222226338Sglebius}
223226338Sglebius
22453541Sshinint
225171259Sdelphijin6_mask2len(struct in6_addr *mask, u_char *lim0)
22653541Sshin{
22778064Sume	int x = 0, y;
22878064Sume	u_char *lim = lim0, *p;
22953541Sshin
230120891Sume	/* ignore the scope_id part */
231120891Sume	if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask))
23278064Sume		lim = (u_char *)mask + sizeof(*mask);
23378064Sume	for (p = (u_char *)mask; p < lim; x++, p++) {
23478064Sume		if (*p != 0xff)
23553541Sshin			break;
23653541Sshin	}
23753541Sshin	y = 0;
23878064Sume	if (p < lim) {
23953541Sshin		for (y = 0; y < 8; y++) {
24078064Sume			if ((*p & (0x80 >> y)) == 0)
24153541Sshin				break;
24253541Sshin		}
24353541Sshin	}
24478064Sume
24578064Sume	/*
24678064Sume	 * when the limit pointer is given, do a stricter check on the
24778064Sume	 * remaining bits.
24878064Sume	 */
24978064Sume	if (p < lim) {
25078064Sume		if (y != 0 && (*p & (0x00ff >> y)) != 0)
251120856Sume			return (-1);
25278064Sume		for (p = p + 1; p < lim; p++)
25378064Sume			if (*p != 0)
254120856Sume				return (-1);
25578064Sume	}
256120891Sume
25753541Sshin	return x * 8 + y;
25853541Sshin}
25953541Sshin
260207268Skib#ifdef COMPAT_FREEBSD32
261207268Skibstruct in6_ndifreq32 {
262238945Sglebius	char ifname[IFNAMSIZ];
263238945Sglebius	uint32_t ifindex;
264207268Skib};
265238945Sglebius#define	SIOCGDEFIFACE32_IN6	_IOWR('i', 86, struct in6_ndifreq32)
266207268Skib#endif
267207268Skib
26853541Sshinint
269171259Sdelphijin6_control(struct socket *so, u_long cmd, caddr_t data,
270171259Sdelphij    struct ifnet *ifp, struct thread *td)
27153541Sshin{
27253541Sshin	struct	in6_ifreq *ifr = (struct in6_ifreq *)data;
27378064Sume	struct	in6_ifaddr *ia = NULL;
27453541Sshin	struct	in6_aliasreq *ifra = (struct in6_aliasreq *)data;
275151539Ssuz	struct sockaddr_in6 *sa6;
276228571Sglebius	int carp_attached = 0;
277164033Srwatson	int error;
278228768Sglebius	u_long ocmd = cmd;
27953541Sshin
280228768Sglebius	/*
281228768Sglebius	 * Compat to make pre-10.x ifconfig(8) operable.
282228768Sglebius	 */
283228768Sglebius	if (cmd == OSIOCAIFADDR_IN6)
284228768Sglebius		cmd = SIOCAIFADDR_IN6;
285228768Sglebius
28662587Sitojun	switch (cmd) {
28762587Sitojun	case SIOCGETSGCNT_IN6:
28862587Sitojun	case SIOCGETMIFCNT_IN6:
289238967Sglebius		/*
290231852Sbz		 * XXX mrt_ioctl has a 3rd, unused, FIB argument in route.c.
291231852Sbz		 * We cannot see how that would be needed, so do not adjust the
292231852Sbz		 * KPI blindly; more likely should clean up the IPv4 variant.
293231852Sbz		 */
294166938Sbms		return (mrt6_ioctl ? mrt6_ioctl(cmd, data) : EOPNOTSUPP);
29562587Sitojun	}
29653541Sshin
297271185Smarkj	switch (cmd) {
298121742Sume	case SIOCAADDRCTL_POLICY:
299121742Sume	case SIOCDADDRCTL_POLICY:
300164033Srwatson		if (td != NULL) {
301164033Srwatson			error = priv_check(td, PRIV_NETINET_ADDRCTRL6);
302164033Srwatson			if (error)
303164033Srwatson				return (error);
304164033Srwatson		}
305121742Sume		return (in6_src_ioctl(cmd, data));
306121742Sume	}
307121742Sume
30862587Sitojun	if (ifp == NULL)
309120856Sume		return (EOPNOTSUPP);
31053541Sshin
31153541Sshin	switch (cmd) {
31253541Sshin	case SIOCSNDFLUSH_IN6:
31353541Sshin	case SIOCSPFXFLUSH_IN6:
31453541Sshin	case SIOCSRTRFLUSH_IN6:
31562587Sitojun	case SIOCSDEFIFACE_IN6:
31662587Sitojun	case SIOCSIFINFO_FLAGS:
317193893Scperciva	case SIOCSIFINFO_IN6:
318164033Srwatson		if (td != NULL) {
319164033Srwatson			error = priv_check(td, PRIV_NETINET_ND6);
320164033Srwatson			if (error)
321164033Srwatson				return (error);
322164033Srwatson		}
323120891Sume		/* FALLTHROUGH */
32478064Sume	case OSIOCGIFINFO_IN6:
32553541Sshin	case SIOCGIFINFO_IN6:
32653541Sshin	case SIOCGNBRINFO_IN6:
32762587Sitojun	case SIOCGDEFIFACE_IN6:
328120856Sume		return (nd6_ioctl(cmd, data, ifp));
329207268Skib
330207268Skib#ifdef COMPAT_FREEBSD32
331207268Skib	case SIOCGDEFIFACE32_IN6:
332207268Skib		{
333207268Skib			struct in6_ndifreq ndif;
334207268Skib			struct in6_ndifreq32 *ndif32;
335207268Skib
336207268Skib			error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif,
337207268Skib			    ifp);
338207268Skib			if (error)
339207268Skib				return (error);
340207268Skib			ndif32 = (struct in6_ndifreq32 *)data;
341207268Skib			ndif32->ifindex = ndif.ifindex;
342207268Skib			return (0);
343207268Skib		}
344207268Skib#endif
34553541Sshin	}
34653541Sshin
34753541Sshin	switch (cmd) {
34853541Sshin	case SIOCSIFPREFIX_IN6:
34953541Sshin	case SIOCDIFPREFIX_IN6:
35053541Sshin	case SIOCAIFPREFIX_IN6:
35153541Sshin	case SIOCCIFPREFIX_IN6:
35253541Sshin	case SIOCSGIFPREFIX_IN6:
35353541Sshin	case SIOCGIFPREFIX_IN6:
35478064Sume		log(LOG_NOTICE,
35578064Sume		    "prefix ioctls are now invalidated. "
35678064Sume		    "please use ifconfig.\n");
357120856Sume		return (EOPNOTSUPP);
35853541Sshin	}
35953541Sshin
36095023Ssuz	switch (cmd) {
36162587Sitojun	case SIOCSSCOPE6:
362164033Srwatson		if (td != NULL) {
363164033Srwatson			error = priv_check(td, PRIV_NETINET_SCOPE6);
364164033Srwatson			if (error)
365164033Srwatson				return (error);
366164033Srwatson		}
367271185Smarkj		/* FALLTHROUGH */
36862587Sitojun	case SIOCGSCOPE6:
36962587Sitojun	case SIOCGSCOPE6DEF:
370271185Smarkj		return (scope6_ioctl(cmd, data, ifp));
37162587Sitojun	}
37262587Sitojun
37353541Sshin	switch (cmd) {
37453541Sshin	case SIOCALIFADDR:
375175630Sbz		if (td != NULL) {
376175630Sbz			error = priv_check(td, PRIV_NET_ADDIFADDR);
377175630Sbz			if (error)
378175630Sbz				return (error);
379175630Sbz		}
380175630Sbz		return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
381175630Sbz
38253541Sshin	case SIOCDLIFADDR:
383164033Srwatson		if (td != NULL) {
384175630Sbz			error = priv_check(td, PRIV_NET_DELIFADDR);
385164033Srwatson			if (error)
386164033Srwatson				return (error);
387164033Srwatson		}
388120891Sume		/* FALLTHROUGH */
38953541Sshin	case SIOCGLIFADDR:
39083366Sjulian		return in6_lifaddr_ioctl(so, cmd, data, ifp, td);
39153541Sshin	}
39253541Sshin
39353541Sshin	/*
39453541Sshin	 * Find address for this interface, if it exists.
395151539Ssuz	 *
396151539Ssuz	 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation
397151539Ssuz	 * only, and used the first interface address as the target of other
398151539Ssuz	 * operations (without checking ifra_addr).  This was because netinet
399151539Ssuz	 * code/API assumed at most 1 interface address per interface.
400151539Ssuz	 * Since IPv6 allows a node to assign multiple addresses
401151539Ssuz	 * on a single interface, we almost always look and check the
402151539Ssuz	 * presence of ifra_addr, and reject invalid ones here.
403151539Ssuz	 * It also decreases duplicated code among SIOC*_IN6 operations.
40453541Sshin	 */
405151539Ssuz	switch (cmd) {
406151539Ssuz	case SIOCAIFADDR_IN6:
407151539Ssuz	case SIOCSIFPHYADDR_IN6:
408151539Ssuz		sa6 = &ifra->ifra_addr;
409151539Ssuz		break;
410151539Ssuz	case SIOCSIFADDR_IN6:
411151539Ssuz	case SIOCGIFADDR_IN6:
412151539Ssuz	case SIOCSIFDSTADDR_IN6:
413151539Ssuz	case SIOCSIFNETMASK_IN6:
414151539Ssuz	case SIOCGIFDSTADDR_IN6:
415151539Ssuz	case SIOCGIFNETMASK_IN6:
416151539Ssuz	case SIOCDIFADDR_IN6:
417151539Ssuz	case SIOCGIFPSRCADDR_IN6:
418151539Ssuz	case SIOCGIFPDSTADDR_IN6:
419151539Ssuz	case SIOCGIFAFLAG_IN6:
420151539Ssuz	case SIOCSNDFLUSH_IN6:
421151539Ssuz	case SIOCSPFXFLUSH_IN6:
422151539Ssuz	case SIOCSRTRFLUSH_IN6:
423151539Ssuz	case SIOCGIFALIFETIME_IN6:
424151539Ssuz	case SIOCSIFALIFETIME_IN6:
425151539Ssuz	case SIOCGIFSTAT_IN6:
426151539Ssuz	case SIOCGIFSTAT_ICMP6:
427151539Ssuz		sa6 = &ifr->ifr_addr;
428151539Ssuz		break;
429255442Sdes	case SIOCSIFADDR:
430255442Sdes	case SIOCSIFBRDADDR:
431255442Sdes	case SIOCSIFDSTADDR:
432255442Sdes	case SIOCSIFNETMASK:
433255442Sdes		/*
434255442Sdes		 * Although we should pass any non-INET6 ioctl requests
435255442Sdes		 * down to driver, we filter some legacy INET requests.
436255442Sdes		 * Drivers trust SIOCSIFADDR et al to come from an already
437255442Sdes		 * privileged layer, and do not perform any credentials
438255442Sdes		 * checks or input validation.
439255442Sdes		 */
440255442Sdes		return (EINVAL);
441151539Ssuz	default:
442151539Ssuz		sa6 = NULL;
443151539Ssuz		break;
444151539Ssuz	}
445151539Ssuz	if (sa6 && sa6->sin6_family == AF_INET6) {
446151539Ssuz		if (sa6->sin6_scope_id != 0)
447151539Ssuz			error = sa6_embedscope(sa6, 0);
448148385Sume		else
449151539Ssuz			error = in6_setscope(&sa6->sin6_addr, ifp, NULL);
450148385Sume		if (error != 0)
451148385Sume			return (error);
452188144Sjamie		if (td != NULL && (error = prison_check_ip6(td->td_ucred,
453188144Sjamie		    &sa6->sin6_addr)) != 0)
454188144Sjamie			return (error);
455151539Ssuz		ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr);
456151539Ssuz	} else
457151539Ssuz		ia = NULL;
45853541Sshin
45953541Sshin	switch (cmd) {
46078064Sume	case SIOCSIFADDR_IN6:
46178064Sume	case SIOCSIFDSTADDR_IN6:
46278064Sume	case SIOCSIFNETMASK_IN6:
46378064Sume		/*
46478064Sume		 * Since IPv6 allows a node to assign multiple addresses
465151465Ssuz		 * on a single interface, SIOCSIFxxx ioctls are deprecated.
46678064Sume		 */
46778064Sume		/* we decided to obsolete this command (20000704) */
468194760Srwatson		error = EINVAL;
469194760Srwatson		goto out;
47053541Sshin
47153541Sshin	case SIOCDIFADDR_IN6:
47262587Sitojun		/*
47378064Sume		 * for IPv4, we look for existing in_ifaddr here to allow
474151465Ssuz		 * "ifconfig if0 delete" to remove the first IPv4 address on
475151465Ssuz		 * the interface.  For IPv6, as the spec allows multiple
476151465Ssuz		 * interface address from the day one, we consider "remove the
477151465Ssuz		 * first one" semantics to be not preferable.
47862587Sitojun		 */
479194760Srwatson		if (ia == NULL) {
480194760Srwatson			error = EADDRNOTAVAIL;
481194760Srwatson			goto out;
482194760Srwatson		}
48353541Sshin		/* FALLTHROUGH */
48453541Sshin	case SIOCAIFADDR_IN6:
48562587Sitojun		/*
48678064Sume		 * We always require users to specify a valid IPv6 address for
48778064Sume		 * the corresponding operation.
48862587Sitojun		 */
48978064Sume		if (ifra->ifra_addr.sin6_family != AF_INET6 ||
490194760Srwatson		    ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) {
491194760Srwatson			error = EAFNOSUPPORT;
492194760Srwatson			goto out;
493194760Srwatson		}
49453541Sshin
495164033Srwatson		if (td != NULL) {
496238945Sglebius			error = priv_check(td, (cmd == SIOCDIFADDR_IN6) ?
497175630Sbz			    PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
498164033Srwatson			if (error)
499194760Srwatson				goto out;
500164033Srwatson		}
501271185Smarkj		/* FALLTHROUGH */
502271185Smarkj	case SIOCGIFSTAT_IN6:
503271185Smarkj	case SIOCGIFSTAT_ICMP6:
504271185Smarkj		if (ifp->if_afdata[AF_INET6] == NULL) {
505271185Smarkj			error = EPFNOSUPPORT;
506271185Smarkj			goto out;
507271185Smarkj		}
50853541Sshin		break;
50953541Sshin
51053541Sshin	case SIOCGIFADDR_IN6:
51153541Sshin		/* This interface is basically deprecated. use SIOCGIFCONF. */
512120891Sume		/* FALLTHROUGH */
51353541Sshin	case SIOCGIFAFLAG_IN6:
51453541Sshin	case SIOCGIFNETMASK_IN6:
51553541Sshin	case SIOCGIFDSTADDR_IN6:
51653541Sshin	case SIOCGIFALIFETIME_IN6:
51753541Sshin		/* must think again about its semantics */
518194760Srwatson		if (ia == NULL) {
519194760Srwatson			error = EADDRNOTAVAIL;
520194760Srwatson			goto out;
521194760Srwatson		}
52253541Sshin		break;
523194760Srwatson
52453541Sshin	case SIOCSIFALIFETIME_IN6:
52553541Sshin	    {
52653541Sshin		struct in6_addrlifetime *lt;
52753541Sshin
528164033Srwatson		if (td != NULL) {
529164033Srwatson			error = priv_check(td, PRIV_NETINET_ALIFETIME6);
530164033Srwatson			if (error)
531194760Srwatson				goto out;
532164033Srwatson		}
533194760Srwatson		if (ia == NULL) {
534194760Srwatson			error = EADDRNOTAVAIL;
535194760Srwatson			goto out;
536194760Srwatson		}
53753541Sshin		/* sanity for overflow - beware unsigned */
53853541Sshin		lt = &ifr->ifr_ifru.ifru_lifetime;
539126552Sume		if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
540253970Shrs		    lt->ia6t_vltime + time_uptime < time_uptime) {
541194760Srwatson			error = EINVAL;
542194760Srwatson			goto out;
54353541Sshin		}
544126552Sume		if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
545253970Shrs		    lt->ia6t_pltime + time_uptime < time_uptime) {
546194760Srwatson			error = EINVAL;
547194760Srwatson			goto out;
54853541Sshin		}
54953541Sshin		break;
55053541Sshin	    }
55153541Sshin	}
55253541Sshin
55353541Sshin	switch (cmd) {
55453541Sshin	case SIOCGIFADDR_IN6:
55553541Sshin		ifr->ifr_addr = ia->ia_addr;
556148385Sume		if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0)
557194760Srwatson			goto out;
55853541Sshin		break;
55953541Sshin
56053541Sshin	case SIOCGIFDSTADDR_IN6:
561194760Srwatson		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
562194760Srwatson			error = EINVAL;
563194760Srwatson			goto out;
564194760Srwatson		}
56562587Sitojun		/*
56662587Sitojun		 * XXX: should we check if ifa_dstaddr is NULL and return
56762587Sitojun		 * an error?
56862587Sitojun		 */
56953541Sshin		ifr->ifr_dstaddr = ia->ia_dstaddr;
570148385Sume		if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0)
571194760Srwatson			goto out;
57253541Sshin		break;
57353541Sshin
57453541Sshin	case SIOCGIFNETMASK_IN6:
57553541Sshin		ifr->ifr_addr = ia->ia_prefixmask;
57653541Sshin		break;
57753541Sshin
57853541Sshin	case SIOCGIFAFLAG_IN6:
57953541Sshin		ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
58053541Sshin		break;
58153541Sshin
58253541Sshin	case SIOCGIFSTAT_IN6:
583253086Sae		COUNTER_ARRAY_COPY(((struct in6_ifextra *)
584253086Sae		    ifp->if_afdata[AF_INET6])->in6_ifstat,
585253086Sae		    &ifr->ifr_ifru.ifru_stat,
586253086Sae		    sizeof(struct in6_ifstat) / sizeof(uint64_t));
58753541Sshin		break;
58853541Sshin
58953541Sshin	case SIOCGIFSTAT_ICMP6:
590253086Sae		COUNTER_ARRAY_COPY(((struct in6_ifextra *)
591253086Sae		    ifp->if_afdata[AF_INET6])->icmp6_ifstat,
592253086Sae		    &ifr->ifr_ifru.ifru_icmp6stat,
593253086Sae		    sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
59453541Sshin		break;
59553541Sshin
59653541Sshin	case SIOCGIFALIFETIME_IN6:
59753541Sshin		ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
598151539Ssuz		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
599151539Ssuz			time_t maxexpire;
600151539Ssuz			struct in6_addrlifetime *retlt =
601151539Ssuz			    &ifr->ifr_ifru.ifru_lifetime;
602151539Ssuz
603151539Ssuz			/*
604151539Ssuz			 * XXX: adjust expiration time assuming time_t is
605151539Ssuz			 * signed.
606151539Ssuz			 */
607151539Ssuz			maxexpire = (-1) &
608151546Ssuz			    ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
609151539Ssuz			if (ia->ia6_lifetime.ia6t_vltime <
610151539Ssuz			    maxexpire - ia->ia6_updatetime) {
611151539Ssuz				retlt->ia6t_expire = ia->ia6_updatetime +
612151539Ssuz				    ia->ia6_lifetime.ia6t_vltime;
613151539Ssuz			} else
614151539Ssuz				retlt->ia6t_expire = maxexpire;
615151539Ssuz		}
616151539Ssuz		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
617151539Ssuz			time_t maxexpire;
618151539Ssuz			struct in6_addrlifetime *retlt =
619151539Ssuz			    &ifr->ifr_ifru.ifru_lifetime;
620151539Ssuz
621151539Ssuz			/*
622151539Ssuz			 * XXX: adjust expiration time assuming time_t is
623151539Ssuz			 * signed.
624151539Ssuz			 */
625151539Ssuz			maxexpire = (-1) &
626151546Ssuz			    ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
627151539Ssuz			if (ia->ia6_lifetime.ia6t_pltime <
628151539Ssuz			    maxexpire - ia->ia6_updatetime) {
629151539Ssuz				retlt->ia6t_preferred = ia->ia6_updatetime +
630151539Ssuz				    ia->ia6_lifetime.ia6t_pltime;
631151539Ssuz			} else
632151539Ssuz				retlt->ia6t_preferred = maxexpire;
633151539Ssuz		}
63453541Sshin		break;
63553541Sshin
63653541Sshin	case SIOCSIFALIFETIME_IN6:
63753541Sshin		ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
63853541Sshin		/* for sanity */
63953541Sshin		if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
64053541Sshin			ia->ia6_lifetime.ia6t_expire =
641253970Shrs				time_uptime + ia->ia6_lifetime.ia6t_vltime;
64253541Sshin		} else
64353541Sshin			ia->ia6_lifetime.ia6t_expire = 0;
64453541Sshin		if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
64553541Sshin			ia->ia6_lifetime.ia6t_preferred =
646253970Shrs				time_uptime + ia->ia6_lifetime.ia6t_pltime;
64753541Sshin		} else
64853541Sshin			ia->ia6_lifetime.ia6t_preferred = 0;
64953541Sshin		break;
65053541Sshin
65178064Sume	case SIOCAIFADDR_IN6:
65278064Sume	{
653194760Srwatson		int i;
654151539Ssuz		struct nd_prefixctl pr0;
655151539Ssuz		struct nd_prefix *pr;
65678064Sume
65762587Sitojun		/*
65878064Sume		 * first, make or update the interface address structure,
65978064Sume		 * and link it to the list.
66062587Sitojun		 */
661151539Ssuz		if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0)
662194760Srwatson			goto out;
663194760Srwatson		if (ia != NULL)
664194760Srwatson			ifa_free(&ia->ia_ifa);
665151915Ssuz		if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr))
666151915Ssuz		    == NULL) {
667171260Sdelphij			/*
668151915Ssuz			 * this can happen when the user specify the 0 valid
669151915Ssuz			 * lifetime.
670151915Ssuz			 */
671151915Ssuz			break;
672151915Ssuz		}
67353541Sshin
674228768Sglebius		if (cmd == ocmd && ifra->ifra_vhid > 0) {
675228571Sglebius			if (carp_attach_p != NULL)
676228571Sglebius				error = (*carp_attach_p)(&ia->ia_ifa,
677228571Sglebius				    ifra->ifra_vhid);
678228571Sglebius			else
679228571Sglebius				error = EPROTONOSUPPORT;
680228571Sglebius			if (error)
681228571Sglebius				goto out;
682228571Sglebius			else
683228571Sglebius				carp_attached = 1;
684228571Sglebius		}
685228571Sglebius
68678064Sume		/*
68778064Sume		 * then, make the prefix on-link on the interface.
68878064Sume		 * XXX: we'd rather create the prefix before the address, but
68978064Sume		 * we need at least one address to install the corresponding
69078064Sume		 * interface route, so we configure the address first.
69178064Sume		 */
69278064Sume
69378064Sume		/*
69478064Sume		 * convert mask to prefix length (prefixmask has already
69578064Sume		 * been validated in in6_update_ifa().
69678064Sume		 */
69778064Sume		bzero(&pr0, sizeof(pr0));
69878064Sume		pr0.ndpr_ifp = ifp;
69978064Sume		pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
700120891Sume		    NULL);
701120891Sume		if (pr0.ndpr_plen == 128) {
702285822Shrs			/* we don't need to install a host route. */
703285822Shrs			goto aifaddr_out;
704120891Sume		}
70578064Sume		pr0.ndpr_prefix = ifra->ifra_addr;
70678064Sume		/* apply the mask for safety. */
70778064Sume		for (i = 0; i < 4; i++) {
70878064Sume			pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
709120891Sume			    ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
71078064Sume		}
71178064Sume		/*
71295023Ssuz		 * XXX: since we don't have an API to set prefix (not address)
71395023Ssuz		 * lifetimes, we just use the same lifetimes as addresses.
71495023Ssuz		 * The (temporarily) installed lifetimes can be overridden by
71595023Ssuz		 * later advertised RAs (when accept_rtadv is non 0), which is
71695023Ssuz		 * an intended behavior.
71778064Sume		 */
71878064Sume		pr0.ndpr_raf_onlink = 1; /* should be configurable? */
71978064Sume		pr0.ndpr_raf_auto =
720120891Sume		    ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
72178064Sume		pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
72278064Sume		pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
72378064Sume
724120891Sume		/* add the prefix if not yet. */
72578064Sume		if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
72678064Sume			/*
72778064Sume			 * nd6_prelist_add will install the corresponding
72878064Sume			 * interface route.
72978064Sume			 */
730228571Sglebius			if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0) {
731228571Sglebius				if (carp_attached)
732228571Sglebius					(*carp_detach_p)(&ia->ia_ifa);
733194760Srwatson				goto out;
734228571Sglebius			}
73578064Sume		}
73678064Sume
737151915Ssuz		/* relate the address to the prefix */
738151915Ssuz		if (ia->ia6_ndpr == NULL) {
739151915Ssuz			ia->ia6_ndpr = pr;
740151915Ssuz			pr->ndpr_refcnt++;
74178064Sume
74278064Sume			/*
743151915Ssuz			 * If this is the first autoconf address from the
744151915Ssuz			 * prefix, create a temporary address as well
745151915Ssuz			 * (when required).
74678064Sume			 */
747151915Ssuz			if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
748181803Sbz			    V_ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
749151915Ssuz				int e;
750151915Ssuz				if ((e = in6_tmpifadd(ia, 1, 0)) != 0) {
751151915Ssuz					log(LOG_NOTICE, "in6_control: failed "
752151915Ssuz					    "to create a temporary address, "
753151915Ssuz					    "errno=%d\n", e);
754151915Ssuz				}
755151915Ssuz			}
75662587Sitojun		}
757151915Ssuz
758151915Ssuz		/*
759151915Ssuz		 * this might affect the status of autoconfigured addresses,
760151915Ssuz		 * that is, this address might make other addresses detached.
761151915Ssuz		 */
762151915Ssuz		pfxlist_onlink_check();
763285822Shrsaifaddr_out:
764285822Shrs		if (error != 0 || ia == NULL)
765285822Shrs			break;
766285822Shrs		/*
767285822Shrs		 * Try to clear the flag when a new IPv6 address is added
768285822Shrs		 * onto an IFDISABLED interface and it succeeds.
769285822Shrs		 */
770285822Shrs		if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) {
771285822Shrs			struct in6_ndireq nd;
772222730Shrs
773285822Shrs			memset(&nd, 0, sizeof(nd));
774285822Shrs			nd.ndi.flags = ND_IFINFO(ifp)->flags;
775285822Shrs			nd.ndi.flags &= ~ND6_IFF_IFDISABLED;
776285822Shrs			if (nd6_ioctl(SIOCSIFINFO_FLAGS, (caddr_t)&nd, ifp) < 0)
777285822Shrs				log(LOG_NOTICE, "SIOCAIFADDR_IN6: "
778285822Shrs				    "SIOCSIFINFO_FLAGS for -ifdisabled "
779285822Shrs				    "failed.");
780285822Shrs			/*
781285822Shrs			 * Ignore failure of clearing the flag intentionally.
782285822Shrs			 * The failure means address duplication was detected.
783285822Shrs			 */
784222730Shrs		}
785285822Shrs		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
78678064Sume		break;
78778064Sume	}
78862587Sitojun
78978064Sume	case SIOCDIFADDR_IN6:
79078064Sume	{
791151539Ssuz		struct nd_prefix *pr;
79278064Sume
79378064Sume		/*
79478064Sume		 * If the address being deleted is the only one that owns
79578064Sume		 * the corresponding prefix, expire the prefix as well.
796120891Sume		 * XXX: theoretically, we don't have to worry about such
79778064Sume		 * relationship, since we separate the address management
79878064Sume		 * and the prefix management.  We do this, however, to provide
79978064Sume		 * as much backward compatibility as possible in terms of
80078064Sume		 * the ioctl operation.
801151915Ssuz		 * Note that in6_purgeaddr() will decrement ndpr_refcnt.
80278064Sume		 */
803151915Ssuz		pr = ia->ia6_ndpr;
80478064Sume		in6_purgeaddr(&ia->ia_ifa);
805151915Ssuz		if (pr && pr->ndpr_refcnt == 0)
806151915Ssuz			prelist_remove(pr);
807126264Smlaier		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
80853541Sshin		break;
80978064Sume	}
81053541Sshin
81178064Sume	default:
812271185Smarkj		if (ifp->if_ioctl == NULL) {
813194760Srwatson			error = EOPNOTSUPP;
814194760Srwatson			goto out;
815194760Srwatson		}
816194760Srwatson		error = (*ifp->if_ioctl)(ifp, cmd, data);
817194760Srwatson		goto out;
81878064Sume	}
81953541Sshin
820194760Srwatson	error = 0;
821194760Srwatsonout:
822194760Srwatson	if (ia != NULL)
823194760Srwatson		ifa_free(&ia->ia_ifa);
824194760Srwatson	return (error);
82578064Sume}
82653541Sshin
827231852Sbz
82878064Sume/*
829231852Sbz * Join necessary multicast groups.  Factored out from in6_update_ifa().
830231852Sbz * This entire work should only be done once, for the default FIB.
831231852Sbz */
832231852Sbzstatic int
833231852Sbzin6_update_ifa_join_mc(struct ifnet *ifp, struct in6_aliasreq *ifra,
834231852Sbz    struct in6_ifaddr *ia, int flags, struct in6_multi **in6m_sol)
835231852Sbz{
836231852Sbz	char ip6buf[INET6_ADDRSTRLEN];
837231852Sbz	struct sockaddr_in6 mltaddr, mltmask;
838231852Sbz	struct in6_addr llsol;
839231852Sbz	struct in6_multi_mship *imm;
840231852Sbz	struct rtentry *rt;
841231852Sbz	int delay, error;
842231852Sbz
843231852Sbz	KASSERT(in6m_sol != NULL, ("%s: in6m_sol is NULL", __func__));
844231852Sbz
845231852Sbz	/* Join solicited multicast addr for new host id. */
846231852Sbz	bzero(&llsol, sizeof(struct in6_addr));
847231852Sbz	llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
848231852Sbz	llsol.s6_addr32[1] = 0;
849231852Sbz	llsol.s6_addr32[2] = htonl(1);
850231852Sbz	llsol.s6_addr32[3] = ifra->ifra_addr.sin6_addr.s6_addr32[3];
851231852Sbz	llsol.s6_addr8[12] = 0xff;
852231852Sbz	if ((error = in6_setscope(&llsol, ifp, NULL)) != 0) {
853231852Sbz		/* XXX: should not happen */
854231852Sbz		log(LOG_ERR, "%s: in6_setscope failed\n", __func__);
855231852Sbz		goto cleanup;
856231852Sbz	}
857231852Sbz	delay = 0;
858231852Sbz	if ((flags & IN6_IFAUPDATE_DADDELAY)) {
859231852Sbz		/*
860231852Sbz		 * We need a random delay for DAD on the address being
861231852Sbz		 * configured.  It also means delaying transmission of the
862231852Sbz		 * corresponding MLD report to avoid report collision.
863231852Sbz		 * [RFC 4861, Section 6.3.7]
864231852Sbz		 */
865231852Sbz		delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
866231852Sbz	}
867231852Sbz	imm = in6_joingroup(ifp, &llsol, &error, delay);
868231852Sbz	if (imm == NULL) {
869231852Sbz		nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
870231852Sbz		    "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &llsol),
871231852Sbz		    if_name(ifp), error));
872231852Sbz		goto cleanup;
873231852Sbz	}
874231852Sbz	LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
875231852Sbz	*in6m_sol = imm->i6mm_maddr;
876231852Sbz
877231852Sbz	bzero(&mltmask, sizeof(mltmask));
878231852Sbz	mltmask.sin6_len = sizeof(struct sockaddr_in6);
879231852Sbz	mltmask.sin6_family = AF_INET6;
880231852Sbz	mltmask.sin6_addr = in6mask32;
881231852Sbz#define	MLTMASK_LEN  4	/* mltmask's masklen (=32bit=4octet) */
882231852Sbz
883231852Sbz	/*
884231852Sbz	 * Join link-local all-nodes address.
885231852Sbz	 */
886231852Sbz	bzero(&mltaddr, sizeof(mltaddr));
887231852Sbz	mltaddr.sin6_len = sizeof(struct sockaddr_in6);
888231852Sbz	mltaddr.sin6_family = AF_INET6;
889231852Sbz	mltaddr.sin6_addr = in6addr_linklocal_allnodes;
890231852Sbz	if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
891231852Sbz		goto cleanup; /* XXX: should not fail */
892231852Sbz
893231852Sbz	/*
894231852Sbz	 * XXX: do we really need this automatic routes?  We should probably
895231852Sbz	 * reconsider this stuff.  Most applications actually do not need the
896231852Sbz	 * routes, since they usually specify the outgoing interface.
897231852Sbz	 */
898231852Sbz	rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
899231852Sbz	if (rt != NULL) {
900231852Sbz		/* XXX: only works in !SCOPEDROUTING case. */
901231852Sbz		if (memcmp(&mltaddr.sin6_addr,
902231852Sbz		    &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
903231852Sbz		    MLTMASK_LEN)) {
904231852Sbz			RTFREE_LOCKED(rt);
905231852Sbz			rt = NULL;
906231852Sbz		}
907231852Sbz	}
908231852Sbz	if (rt == NULL) {
909231852Sbz		error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
910231852Sbz		    (struct sockaddr *)&ia->ia_addr,
911231852Sbz		    (struct sockaddr *)&mltmask, RTF_UP,
912231852Sbz		    (struct rtentry **)0, RT_DEFAULT_FIB);
913231852Sbz		if (error)
914231852Sbz			goto cleanup;
915231852Sbz	} else
916231852Sbz		RTFREE_LOCKED(rt);
917231852Sbz
918231852Sbz	imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
919231852Sbz	if (imm == NULL) {
920231852Sbz		nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
921231852Sbz		    "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
922231852Sbz		    &mltaddr.sin6_addr), if_name(ifp), error));
923231852Sbz		goto cleanup;
924231852Sbz	}
925231852Sbz	LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
926231852Sbz
927231852Sbz	/*
928231852Sbz	 * Join node information group address.
929231852Sbz	 */
930231852Sbz	delay = 0;
931231852Sbz	if ((flags & IN6_IFAUPDATE_DADDELAY)) {
932231852Sbz		/*
933231852Sbz		 * The spec does not say anything about delay for this group,
934231852Sbz		 * but the same logic should apply.
935231852Sbz		 */
936231852Sbz		delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz);
937231852Sbz	}
938231852Sbz	if (in6_nigroup(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
939231852Sbz		/* XXX jinmei */
940231852Sbz		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, delay);
941231852Sbz		if (imm == NULL)
942231852Sbz			nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
943231852Sbz			    "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
944231852Sbz			    &mltaddr.sin6_addr), if_name(ifp), error));
945231852Sbz			/* XXX not very fatal, go on... */
946231852Sbz		else
947231852Sbz			LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
948231852Sbz	}
949250251Shrs	if (V_icmp6_nodeinfo_oldmcprefix &&
950250251Shrs	     in6_nigroup_oldmcprefix(ifp, NULL, -1, &mltaddr.sin6_addr) == 0) {
951250251Shrs		imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, delay);
952250251Shrs		if (imm == NULL)
953250251Shrs			nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
954250251Shrs			    "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
955250251Shrs			    &mltaddr.sin6_addr), if_name(ifp), error));
956250251Shrs			/* XXX not very fatal, go on... */
957250251Shrs		else
958250251Shrs			LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
959250251Shrs	}
960231852Sbz
961231852Sbz	/*
962231852Sbz	 * Join interface-local all-nodes address.
963231852Sbz	 * (ff01::1%ifN, and ff01::%ifN/32)
964231852Sbz	 */
965231852Sbz	mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
966231852Sbz	if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
967231852Sbz		goto cleanup; /* XXX: should not fail */
968231852Sbz	/* XXX: again, do we really need the route? */
969231852Sbz	rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
970231852Sbz	if (rt != NULL) {
971231852Sbz		if (memcmp(&mltaddr.sin6_addr,
972231852Sbz		    &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
973231852Sbz		    MLTMASK_LEN)) {
974231852Sbz			RTFREE_LOCKED(rt);
975231852Sbz			rt = NULL;
976231852Sbz		}
977231852Sbz	}
978231852Sbz	if (rt == NULL) {
979231852Sbz		error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&mltaddr,
980231852Sbz		    (struct sockaddr *)&ia->ia_addr,
981231852Sbz		    (struct sockaddr *)&mltmask, RTF_UP,
982231852Sbz		    (struct rtentry **)0, RT_DEFAULT_FIB);
983231852Sbz		if (error)
984231852Sbz			goto cleanup;
985231852Sbz	} else
986231852Sbz		RTFREE_LOCKED(rt);
987231852Sbz
988231852Sbz	imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0);
989231852Sbz	if (imm == NULL) {
990231852Sbz		nd6log((LOG_WARNING, "%s: addmulti failed for %s on %s "
991231852Sbz		    "(errno=%d)\n", __func__, ip6_sprintf(ip6buf,
992231852Sbz		    &mltaddr.sin6_addr), if_name(ifp), error));
993231852Sbz		goto cleanup;
994231852Sbz	}
995231852Sbz	LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain);
996231852Sbz#undef	MLTMASK_LEN
997231852Sbz
998231852Sbzcleanup:
999231852Sbz	return (error);
1000231852Sbz}
1001231852Sbz
1002231852Sbz/*
100378064Sume * Update parameters of an IPv6 interface address.
100478064Sume * If necessary, a new entry is created and linked into address chains.
100578064Sume * This function is separated from in6_control().
100678064Sume */
100778064Sumeint
1008171259Sdelphijin6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
1009171259Sdelphij    struct in6_ifaddr *ia, int flags)
101078064Sume{
101178064Sume	int error = 0, hostIsNew = 0, plen = -1;
101278064Sume	struct sockaddr_in6 dst6;
101378064Sume	struct in6_addrlifetime *lt;
1014151539Ssuz	struct in6_multi *in6m_sol;
1015151539Ssuz	int delay;
1016165118Sbz	char ip6buf[INET6_ADDRSTRLEN];
101778064Sume
101878064Sume	/* Validate parameters */
101978064Sume	if (ifp == NULL || ifra == NULL) /* this maybe redundant */
1020120856Sume		return (EINVAL);
102178064Sume
102278064Sume	/*
102378064Sume	 * The destination address for a p2p link must have a family
102478064Sume	 * of AF_UNSPEC or AF_INET6.
102578064Sume	 */
102678064Sume	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
102778064Sume	    ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
102878064Sume	    ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
1029120856Sume		return (EAFNOSUPPORT);
103078064Sume	/*
103178064Sume	 * validate ifra_prefixmask.  don't check sin6_family, netmask
103278064Sume	 * does not carry fields other than sin6_len.
103378064Sume	 */
103478064Sume	if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
1035120856Sume		return (EINVAL);
103678064Sume	/*
103778064Sume	 * Because the IPv6 address architecture is classless, we require
103878064Sume	 * users to specify a (non 0) prefix length (mask) for a new address.
103978064Sume	 * We also require the prefix (when specified) mask is valid, and thus
104078064Sume	 * reject a non-consecutive mask.
104178064Sume	 */
104278064Sume	if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
1043120856Sume		return (EINVAL);
104478064Sume	if (ifra->ifra_prefixmask.sin6_len != 0) {
104578064Sume		plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
1046120891Sume		    (u_char *)&ifra->ifra_prefixmask +
1047120891Sume		    ifra->ifra_prefixmask.sin6_len);
104878064Sume		if (plen <= 0)
1049120856Sume			return (EINVAL);
1050120891Sume	} else {
105162587Sitojun		/*
105295023Ssuz		 * In this case, ia must not be NULL.  We just use its prefix
105378064Sume		 * length.
105462587Sitojun		 */
105578064Sume		plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
105678064Sume	}
105778064Sume	/*
105878064Sume	 * If the destination address on a p2p interface is specified,
105978064Sume	 * and the address is a scoped one, validate/set the scope
106078064Sume	 * zone identifier.
106178064Sume	 */
106278064Sume	dst6 = ifra->ifra_dstaddr;
1063120891Sume	if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 &&
106478064Sume	    (dst6.sin6_family == AF_INET6)) {
1065148385Sume		struct in6_addr in6_tmp;
1066126552Sume		u_int32_t zoneid;
106778064Sume
1068148385Sume		in6_tmp = dst6.sin6_addr;
1069148385Sume		if (in6_setscope(&in6_tmp, ifp, &zoneid))
1070148385Sume			return (EINVAL); /* XXX: should be impossible */
1071148385Sume
1072148385Sume		if (dst6.sin6_scope_id != 0) {
1073148385Sume			if (dst6.sin6_scope_id != zoneid)
1074148385Sume				return (EINVAL);
1075148385Sume		} else		/* user omit to specify the ID. */
1076126552Sume			dst6.sin6_scope_id = zoneid;
1077148385Sume
1078148385Sume		/* convert into the internal form */
1079148385Sume		if (sa6_embedscope(&dst6, 0))
1080148385Sume			return (EINVAL); /* XXX: should be impossible */
108178064Sume	}
108278064Sume	/*
108378064Sume	 * The destination address can be specified only for a p2p or a
108478064Sume	 * loopback interface.  If specified, the corresponding prefix length
108578064Sume	 * must be 128.
108678064Sume	 */
108778064Sume	if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
108878064Sume		if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) {
1089126552Sume			/* XXX: noisy message */
1090122059Sume			nd6log((LOG_INFO, "in6_update_ifa: a destination can "
1091122059Sume			    "be specified for a p2p or a loopback IF only\n"));
1092120856Sume			return (EINVAL);
109378064Sume		}
109478064Sume		if (plen != 128) {
1095122059Sume			nd6log((LOG_INFO, "in6_update_ifa: prefixlen should "
1096122059Sume			    "be 128 when dstaddr is specified\n"));
1097120856Sume			return (EINVAL);
109878064Sume		}
109978064Sume	}
110078064Sume	/* lifetime consistency check */
110178064Sume	lt = &ifra->ifra_lifetime;
1102151539Ssuz	if (lt->ia6t_pltime > lt->ia6t_vltime)
1103151539Ssuz		return (EINVAL);
110478064Sume	if (lt->ia6t_vltime == 0) {
110562587Sitojun		/*
110678064Sume		 * the following log might be noisy, but this is a typical
110778064Sume		 * configuration mistake or a tool's bug.
110862587Sitojun		 */
1109122059Sume		nd6log((LOG_INFO,
111078064Sume		    "in6_update_ifa: valid lifetime is 0 for %s\n",
1111165118Sbz		    ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr)));
1112151539Ssuz
1113151539Ssuz		if (ia == NULL)
1114151539Ssuz			return (0); /* there's nothing to do */
111578064Sume	}
111662587Sitojun
111778064Sume	/*
111878064Sume	 * If this is a new address, allocate a new ifaddr and link it
111978064Sume	 * into chains.
112078064Sume	 */
112178064Sume	if (ia == NULL) {
112278064Sume		hostIsNew = 1;
112379763Sume		/*
112479763Sume		 * When in6_update_ifa() is called in a process of a received
1125120891Sume		 * RA, it is called under an interrupt context.  So, we should
1126120891Sume		 * call malloc with M_NOWAIT.
112779763Sume		 */
1128120891Sume		ia = (struct in6_ifaddr *) malloc(sizeof(*ia), M_IFADDR,
1129120891Sume		    M_NOWAIT);
113078064Sume		if (ia == NULL)
113178064Sume			return (ENOBUFS);
113278064Sume		bzero((caddr_t)ia, sizeof(*ia));
1133194602Srwatson		ifa_init(&ia->ia_ifa);
1134170202Sjinmei		LIST_INIT(&ia->ia6_memberships);
1135151539Ssuz		/* Initialize the address and masks, and put time stamp */
113678064Sume		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
113778064Sume		ia->ia_addr.sin6_family = AF_INET6;
113878064Sume		ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
1139253970Shrs		ia->ia6_createtime = time_uptime;
114078064Sume		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
114178064Sume			/*
114278064Sume			 * XXX: some functions expect that ifa_dstaddr is not
114378064Sume			 * NULL for p2p interfaces.
114478064Sume			 */
1145120891Sume			ia->ia_ifa.ifa_dstaddr =
1146120891Sume			    (struct sockaddr *)&ia->ia_dstaddr;
114778064Sume		} else {
114878064Sume			ia->ia_ifa.ifa_dstaddr = NULL;
114953541Sshin		}
1150108033Shsu		ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
115178064Sume		ia->ia_ifp = ifp;
1152194760Srwatson		ifa_ref(&ia->ia_ifa);			/* if_addrhead */
1153229621Sjhb		IF_ADDR_WLOCK(ifp);
1154191340Srwatson		TAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1155229621Sjhb		IF_ADDR_WUNLOCK(ifp);
1156194907Srwatson
1157194971Srwatson		ifa_ref(&ia->ia_ifa);			/* in6_ifaddrhead */
1158194971Srwatson		IN6_IFADDR_WLOCK();
1159194907Srwatson		TAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link);
1160244272Sae		LIST_INSERT_HEAD(IN6ADDR_HASH(&ifra->ifra_addr.sin6_addr),
1161244272Sae		    ia, ia6_hash);
1162194971Srwatson		IN6_IFADDR_WUNLOCK();
116378064Sume	}
116478064Sume
1165151539Ssuz	/* update timestamp */
1166253970Shrs	ia->ia6_updatetime = time_uptime;
1167151539Ssuz
116878064Sume	/* set prefix mask */
116978064Sume	if (ifra->ifra_prefixmask.sin6_len) {
117078064Sume		/*
117178064Sume		 * We prohibit changing the prefix length of an existing
117278064Sume		 * address, because
117378064Sume		 * + such an operation should be rare in IPv6, and
117478064Sume		 * + the operation would confuse prefix management.
117578064Sume		 */
117678064Sume		if (ia->ia_prefixmask.sin6_len &&
117778064Sume		    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1178122059Sume			nd6log((LOG_INFO, "in6_update_ifa: the prefix length of an"
117978064Sume			    " existing (%s) address should not be changed\n",
1180165118Sbz			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
118178064Sume			error = EINVAL;
118278064Sume			goto unlink;
118353541Sshin		}
118478064Sume		ia->ia_prefixmask = ifra->ifra_prefixmask;
1185250815Smelifaro		ia->ia_prefixmask.sin6_family = AF_INET6;
118678064Sume	}
118778064Sume
118878064Sume	/*
118978064Sume	 * If a new destination address is specified, scrub the old one and
119078064Sume	 * install the new destination.  Note that the interface must be
1191120891Sume	 * p2p or loopback (see the check above.)
119278064Sume	 */
119378064Sume	if (dst6.sin6_family == AF_INET6 &&
1194120891Sume	    !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, &ia->ia_dstaddr.sin6_addr)) {
119578064Sume		int e;
119678064Sume
119778064Sume		if ((ia->ia_flags & IFA_ROUTE) != 0 &&
1198120891Sume		    (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST)) != 0) {
1199122059Sume			nd6log((LOG_ERR, "in6_update_ifa: failed to remove "
120078064Sume			    "a route to the old destination: %s\n",
1201165118Sbz			    ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr)));
120278064Sume			/* proceed anyway... */
1203120891Sume		} else
120478064Sume			ia->ia_flags &= ~IFA_ROUTE;
120578064Sume		ia->ia_dstaddr = dst6;
120678064Sume	}
120753541Sshin
1208148385Sume	/*
1209148385Sume	 * Set lifetimes.  We do not refer to ia6t_expire and ia6t_preferred
1210148385Sume	 * to see if the address is deprecated or invalidated, but initialize
1211148385Sume	 * these members for applications.
1212148385Sume	 */
1213148385Sume	ia->ia6_lifetime = ifra->ifra_lifetime;
1214148385Sume	if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1215148385Sume		ia->ia6_lifetime.ia6t_expire =
1216253970Shrs		    time_uptime + ia->ia6_lifetime.ia6t_vltime;
1217148385Sume	} else
1218148385Sume		ia->ia6_lifetime.ia6t_expire = 0;
1219148385Sume	if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1220148385Sume		ia->ia6_lifetime.ia6t_preferred =
1221253970Shrs		    time_uptime + ia->ia6_lifetime.ia6t_pltime;
1222148385Sume	} else
1223148385Sume		ia->ia6_lifetime.ia6t_preferred = 0;
1224148385Sume
122578064Sume	/* reset the interface and routing table appropriately. */
122678064Sume	if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
122778064Sume		goto unlink;
122878064Sume
122978064Sume	/*
1230148385Sume	 * configure address flags.
1231148385Sume	 */
1232148385Sume	ia->ia6_flags = ifra->ifra_flags;
1233148385Sume	/*
1234148385Sume	 * backward compatibility - if IN6_IFF_DEPRECATED is set from the
1235148385Sume	 * userland, make it deprecated.
1236148385Sume	 */
1237148385Sume	if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) {
1238148385Sume		ia->ia6_lifetime.ia6t_pltime = 0;
1239253970Shrs		ia->ia6_lifetime.ia6t_preferred = time_uptime;
1240148385Sume	}
1241148385Sume	/*
1242151539Ssuz	 * Make the address tentative before joining multicast addresses,
1243151539Ssuz	 * so that corresponding MLD responses would not have a tentative
1244151539Ssuz	 * source address.
1245148385Sume	 */
1246151539Ssuz	ia->ia6_flags &= ~IN6_IFF_DUPLICATED;	/* safety */
1247148385Sume
1248287732Shrs	/*
1249287732Shrs	 * DAD should be performed for an new address or addresses on
1250287732Shrs	 * an interface with ND6_IFF_IFDISABLED.
1251287732Shrs	 */
1252287732Shrs	if (in6if_do_dad(ifp) &&
1253287732Shrs	    (hostIsNew || (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)))
1254197138Shrs		ia->ia6_flags |= IN6_IFF_TENTATIVE;
1255197138Shrs
1256148385Sume	/*
1257148385Sume	 * We are done if we have simply modified an existing address.
1258148385Sume	 */
1259148385Sume	if (!hostIsNew)
1260148385Sume		return (error);
1261148385Sume
1262148385Sume	/*
126378064Sume	 * Beyond this point, we should call in6_purgeaddr upon an error,
1264120891Sume	 * not just go to unlink.
126578064Sume	 */
126678064Sume
1267231852Sbz	/* Join necessary multicast groups. */
1268151539Ssuz	in6m_sol = NULL;
126978064Sume	if ((ifp->if_flags & IFF_MULTICAST) != 0) {
1270231852Sbz		error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol);
1271231852Sbz		if (error)
1272148385Sume			goto cleanup;
127378064Sume	}
127453541Sshin
1275287734Shrs	/* Perform DAD, if the address is TENTATIVE. */
1276287734Shrs	if ((ia->ia6_flags & IN6_IFF_TENTATIVE)) {
1277151539Ssuz		int mindelay, maxdelay;
1278151539Ssuz
1279151539Ssuz		delay = 0;
1280151539Ssuz		if ((flags & IN6_IFAUPDATE_DADDELAY)) {
1281151539Ssuz			/*
1282151539Ssuz			 * We need to impose a delay before sending an NS
1283151539Ssuz			 * for DAD.  Check if we also needed a delay for the
1284151539Ssuz			 * corresponding MLD message.  If we did, the delay
1285151539Ssuz			 * should be larger than the MLD delay (this could be
1286151539Ssuz			 * relaxed a bit, but this simple logic is at least
1287151539Ssuz			 * safe).
1288191672Sbms			 * XXX: Break data hiding guidelines and look at
1289191672Sbms			 * state for the solicited multicast group.
1290151539Ssuz			 */
1291151539Ssuz			mindelay = 0;
1292151539Ssuz			if (in6m_sol != NULL &&
1293191672Sbms			    in6m_sol->in6m_state == MLD_REPORTING_MEMBER) {
1294151539Ssuz				mindelay = in6m_sol->in6m_timer;
1295151539Ssuz			}
1296151539Ssuz			maxdelay = MAX_RTR_SOLICITATION_DELAY * hz;
1297151539Ssuz			if (maxdelay - mindelay == 0)
1298151539Ssuz				delay = 0;
1299151539Ssuz			else {
1300151539Ssuz				delay =
1301151539Ssuz				    (arc4random() % (maxdelay - mindelay)) +
1302151539Ssuz				    mindelay;
1303151539Ssuz			}
1304151539Ssuz		}
1305151539Ssuz		nd6_dad_start((struct ifaddr *)ia, delay);
1306151539Ssuz	}
1307151539Ssuz
1308194760Srwatson	KASSERT(hostIsNew, ("in6_update_ifa: !hostIsNew"));
1309194760Srwatson	ifa_free(&ia->ia_ifa);
1310120856Sume	return (error);
131178064Sume
131278064Sume  unlink:
131378064Sume	/*
131478064Sume	 * XXX: if a change of an existing address failed, keep the entry
131578064Sume	 * anyway.
131678064Sume	 */
1317194760Srwatson	if (hostIsNew) {
1318194943Srwatson		in6_unlink_ifa(ia, ifp);
1319194760Srwatson		ifa_free(&ia->ia_ifa);
1320194760Srwatson	}
1321120856Sume	return (error);
1322148385Sume
1323148385Sume  cleanup:
1324194760Srwatson	KASSERT(hostIsNew, ("in6_update_ifa: cleanup: !hostIsNew"));
1325194760Srwatson	ifa_free(&ia->ia_ifa);
1326148385Sume	in6_purgeaddr(&ia->ia_ifa);
1327148385Sume	return error;
132853541Sshin}
132953541Sshin
1330231852Sbz/*
1331231852Sbz * Leave multicast groups.  Factored out from in6_purgeaddr().
1332231852Sbz * This entire work should only be done once, for the default FIB.
1333231852Sbz */
1334231852Sbzstatic int
1335231852Sbzin6_purgeaddr_mc(struct ifnet *ifp, struct in6_ifaddr *ia, struct ifaddr *ifa0)
133662587Sitojun{
1337231852Sbz	struct sockaddr_in6 mltaddr, mltmask;
1338170202Sjinmei	struct in6_multi_mship *imm;
1339192282Sqingli	struct rtentry *rt;
1340237571Sdelphij	struct sockaddr_in6 sin6;
1341231852Sbz	int error;
134262587Sitojun
1343192282Sqingli	/*
1344231852Sbz	 * Leave from multicast groups we have joined for the interface.
1345192282Sqingli	 */
1346228966Sjhb	while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) {
1347170202Sjinmei		LIST_REMOVE(imm, i6mm_chain);
1348170202Sjinmei		in6_leavegroup(imm);
134962587Sitojun	}
135062587Sitojun
1351192282Sqingli	/*
1352231852Sbz	 * Remove the link-local all-nodes address.
1353192282Sqingli	 */
1354192282Sqingli	bzero(&mltmask, sizeof(mltmask));
1355192282Sqingli	mltmask.sin6_len = sizeof(struct sockaddr_in6);
1356192282Sqingli	mltmask.sin6_family = AF_INET6;
1357192282Sqingli	mltmask.sin6_addr = in6mask32;
1358192282Sqingli
1359192282Sqingli	bzero(&mltaddr, sizeof(mltaddr));
1360192282Sqingli	mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1361192282Sqingli	mltaddr.sin6_family = AF_INET6;
1362192282Sqingli	mltaddr.sin6_addr = in6addr_linklocal_allnodes;
1363192282Sqingli
1364230494Sbz	if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1365231852Sbz		return (error);
1366192282Sqingli
1367237571Sdelphij	/*
1368237571Sdelphij	 * As for the mltaddr above, proactively prepare the sin6 to avoid
1369237571Sdelphij	 * rtentry un- and re-locking.
1370237571Sdelphij	 */
1371237571Sdelphij	if (ifa0 != NULL) {
1372237571Sdelphij		bzero(&sin6, sizeof(sin6));
1373237571Sdelphij		sin6.sin6_len = sizeof(sin6);
1374237571Sdelphij		sin6.sin6_family = AF_INET6;
1375238945Sglebius		memcpy(&sin6.sin6_addr, &satosin6(ifa0->ifa_addr)->sin6_addr,
1376237571Sdelphij		    sizeof(sin6.sin6_addr));
1377238222Sbz		error = in6_setscope(&sin6.sin6_addr, ifa0->ifa_ifp, NULL);
1378238222Sbz		if (error != 0)
1379238222Sbz			return (error);
1380237571Sdelphij	}
1381237571Sdelphij
1382231852Sbz	rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
1383192282Sqingli	if (rt != NULL && rt->rt_gateway != NULL &&
1384238945Sglebius	    (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1385192282Sqingli		    &ia->ia_addr.sin6_addr,
1386192282Sqingli		    sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1387238945Sglebius		/*
1388231852Sbz		 * If no more IPv6 address exists on this interface then
1389231852Sbz		 * remove the multicast address route.
1390192282Sqingli		 */
1391192282Sqingli		if (ifa0 == NULL) {
1392238945Sglebius			memcpy(&mltaddr.sin6_addr,
1393238945Sglebius			    &satosin6(rt_key(rt))->sin6_addr,
1394238945Sglebius			    sizeof(mltaddr.sin6_addr));
1395192282Sqingli			RTFREE_LOCKED(rt);
1396231852Sbz			error = in6_rtrequest(RTM_DELETE,
1397231852Sbz			    (struct sockaddr *)&mltaddr,
1398231852Sbz			    (struct sockaddr *)&ia->ia_addr,
1399231852Sbz			    (struct sockaddr *)&mltmask, RTF_UP,
1400231852Sbz			    (struct rtentry **)0, RT_DEFAULT_FIB);
1401192282Sqingli			if (error)
1402231852Sbz				log(LOG_INFO, "%s: link-local all-nodes "
1403231852Sbz				    "multicast address deletion error\n",
1404231852Sbz				    __func__);
1405192282Sqingli		} else {
1406192282Sqingli			/*
1407231852Sbz			 * Replace the gateway of the route.
1408192282Sqingli			 */
1409237571Sdelphij			memcpy(rt->rt_gateway, &sin6, sizeof(sin6));
1410192282Sqingli			RTFREE_LOCKED(rt);
1411192282Sqingli		}
1412192282Sqingli	} else {
1413192282Sqingli		if (rt != NULL)
1414192282Sqingli			RTFREE_LOCKED(rt);
1415192282Sqingli	}
1416192282Sqingli
1417192282Sqingli	/*
1418231852Sbz	 * Remove the node-local all-nodes address.
1419192282Sqingli	 */
1420192282Sqingli	mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
1421231852Sbz	if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0)
1422231852Sbz		return (error);
1423192282Sqingli
1424231852Sbz	rt = in6_rtalloc1((struct sockaddr *)&mltaddr, 0, 0UL, RT_DEFAULT_FIB);
1425192282Sqingli	if (rt != NULL && rt->rt_gateway != NULL &&
1426238945Sglebius	    (memcmp(&satosin6(rt->rt_gateway)->sin6_addr,
1427192282Sqingli		    &ia->ia_addr.sin6_addr,
1428192282Sqingli		    sizeof(ia->ia_addr.sin6_addr)) == 0)) {
1429238945Sglebius		/*
1430231852Sbz		 * If no more IPv6 address exists on this interface then
1431231852Sbz		 * remove the multicast address route.
1432192282Sqingli		 */
1433192282Sqingli		if (ifa0 == NULL) {
1434238945Sglebius			memcpy(&mltaddr.sin6_addr,
1435238945Sglebius			    &satosin6(rt_key(rt))->sin6_addr,
1436238945Sglebius			    sizeof(mltaddr.sin6_addr));
1437192282Sqingli
1438192282Sqingli			RTFREE_LOCKED(rt);
1439231852Sbz			error = in6_rtrequest(RTM_DELETE,
1440231852Sbz			    (struct sockaddr *)&mltaddr,
1441231852Sbz			    (struct sockaddr *)&ia->ia_addr,
1442231852Sbz			    (struct sockaddr *)&mltmask, RTF_UP,
1443231852Sbz			    (struct rtentry **)0, RT_DEFAULT_FIB);
1444192282Sqingli			if (error)
1445231852Sbz				log(LOG_INFO, "%s: node-local all-nodes"
1446231852Sbz				    "multicast address deletion error\n",
1447231852Sbz				    __func__);
1448192282Sqingli		} else {
1449192282Sqingli			/*
1450231852Sbz			 * Replace the gateway of the route.
1451192282Sqingli			 */
1452237571Sdelphij			memcpy(rt->rt_gateway, &sin6, sizeof(sin6));
1453192282Sqingli			RTFREE_LOCKED(rt);
1454192282Sqingli		}
1455192282Sqingli	} else {
1456192282Sqingli		if (rt != NULL)
1457192282Sqingli			RTFREE_LOCKED(rt);
1458192282Sqingli	}
1459192282Sqingli
1460231852Sbz	return (0);
1461231852Sbz}
1462231852Sbz
1463231852Sbzvoid
1464231852Sbzin6_purgeaddr(struct ifaddr *ifa)
1465231852Sbz{
1466231852Sbz	struct ifnet *ifp = ifa->ifa_ifp;
1467231852Sbz	struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1468231852Sbz	int plen, error;
1469231852Sbz	struct ifaddr *ifa0;
1470231852Sbz
1471231852Sbz	if (ifa->ifa_carp)
1472231852Sbz		(*carp_detach_p)(ifa);
1473231852Sbz
1474231852Sbz	/*
1475231852Sbz	 * find another IPv6 address as the gateway for the
1476231852Sbz	 * link-local and node-local all-nodes multicast
1477231852Sbz	 * address routes
1478231852Sbz	 */
1479231852Sbz	IF_ADDR_RLOCK(ifp);
1480231852Sbz	TAILQ_FOREACH(ifa0, &ifp->if_addrhead, ifa_link) {
1481231852Sbz		if ((ifa0->ifa_addr->sa_family != AF_INET6) ||
1482231852Sbz		    memcmp(&satosin6(ifa0->ifa_addr)->sin6_addr,
1483238945Sglebius		    &ia->ia_addr.sin6_addr, sizeof(struct in6_addr)) == 0)
1484231852Sbz			continue;
1485231852Sbz		else
1486231852Sbz			break;
1487231852Sbz	}
1488230506Sbz	if (ifa0 != NULL)
1489231852Sbz		ifa_ref(ifa0);
1490231852Sbz	IF_ADDR_RUNLOCK(ifp);
1491231852Sbz
1492231852Sbz	/*
1493231852Sbz	 * Remove the loopback route to the interface address.
1494238945Sglebius	 * The check for the current setting of "nd6_useloopback"
1495231852Sbz	 * is not needed.
1496231852Sbz	 */
1497231852Sbz	if (ia->ia_flags & IFA_RTSELF) {
1498231852Sbz		error = ifa_del_loopback_route((struct ifaddr *)ia,
1499238945Sglebius		    (struct sockaddr *)&ia->ia_addr);
1500231852Sbz		if (error == 0)
1501231852Sbz			ia->ia_flags &= ~IFA_RTSELF;
1502231852Sbz	}
1503231852Sbz
1504231852Sbz	/* stop DAD processing */
1505231852Sbz	nd6_dad_stop(ifa);
1506231852Sbz
1507231852Sbz	/* Remove local address entry from lltable. */
1508231852Sbz	in6_ifremloop(ifa);
1509231852Sbz
1510231852Sbz	/* Leave multicast groups. */
1511231852Sbz	error = in6_purgeaddr_mc(ifp, ia, ifa0);
1512231852Sbz
1513231852Sbz	if (ifa0 != NULL)
1514230506Sbz		ifa_free(ifa0);
1515192282Sqingli
1516192282Sqingli	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1517192282Sqingli	if ((ia->ia_flags & IFA_ROUTE) && plen == 128) {
1518231852Sbz		error = rtinit(&(ia->ia_ifa), RTM_DELETE, ia->ia_flags |
1519231852Sbz		    (ia->ia_dstaddr.sin6_family == AF_INET6) ? RTF_HOST : 0);
1520192282Sqingli		if (error != 0)
1521231852Sbz			log(LOG_INFO, "%s: err=%d, destination address delete "
1522231852Sbz			    "failed\n", __func__, error);
1523192282Sqingli		ia->ia_flags &= ~IFA_ROUTE;
1524192282Sqingli	}
1525192282Sqingli
152678064Sume	in6_unlink_ifa(ia, ifp);
152778064Sume}
152878064Sume
152978064Sumestatic void
1530171259Sdelphijin6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
153178064Sume{
1532278620Sae	char ip6buf[INET6_ADDRSTRLEN];
153378064Sume
1534229621Sjhb	IF_ADDR_WLOCK(ifp);
1535191340Srwatson	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
1536229621Sjhb	IF_ADDR_WUNLOCK(ifp);
1537194760Srwatson	ifa_free(&ia->ia_ifa);			/* if_addrhead */
153862587Sitojun
1539195102Srwatson	/*
1540195102Srwatson	 * Defer the release of what might be the last reference to the
1541195102Srwatson	 * in6_ifaddr so that it can't be freed before the remainder of the
1542195102Srwatson	 * cleanup.
1543195102Srwatson	 */
1544194971Srwatson	IN6_IFADDR_WLOCK();
1545194907Srwatson	TAILQ_REMOVE(&V_in6_ifaddrhead, ia, ia_link);
1546244272Sae	LIST_REMOVE(ia, ia6_hash);
1547194971Srwatson	IN6_IFADDR_WUNLOCK();
154862587Sitojun
154962587Sitojun	/*
1550151915Ssuz	 * Release the reference to the base prefix.  There should be a
1551151915Ssuz	 * positive reference.
155262587Sitojun	 */
1553194907Srwatson	if (ia->ia6_ndpr == NULL) {
1554151915Ssuz		nd6log((LOG_NOTICE,
1555151915Ssuz		    "in6_unlink_ifa: autoconf'ed address "
1556278620Sae		    "%s has no prefix\n", ip6_sprintf(ip6buf, IA6_IN6(ia))));
1557151915Ssuz	} else {
1558194907Srwatson		ia->ia6_ndpr->ndpr_refcnt--;
1559194907Srwatson		ia->ia6_ndpr = NULL;
1560151915Ssuz	}
156162587Sitojun
1562151915Ssuz	/*
1563151915Ssuz	 * Also, if the address being removed is autoconf'ed, call
1564151915Ssuz	 * pfxlist_onlink_check() since the release might affect the status of
1565171260Sdelphij	 * other (detached) addresses.
1566151915Ssuz	 */
1567194907Srwatson	if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) {
156878064Sume		pfxlist_onlink_check();
156962587Sitojun	}
1570195102Srwatson	ifa_free(&ia->ia_ifa);			/* in6_ifaddrhead */
157162587Sitojun}
157262587Sitojun
157378064Sumevoid
1574171259Sdelphijin6_purgeif(struct ifnet *ifp)
157578064Sume{
157678064Sume	struct ifaddr *ifa, *nifa;
157778064Sume
1578191340Srwatson	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
157978064Sume		if (ifa->ifa_addr->sa_family != AF_INET6)
158078064Sume			continue;
158178064Sume		in6_purgeaddr(ifa);
158278064Sume	}
158378064Sume
158478064Sume	in6_ifdetach(ifp);
158578064Sume}
158678064Sume
158753541Sshin/*
158853541Sshin * SIOC[GAD]LIFADDR.
158962744Sgrog *	SIOCGLIFADDR: get first address. (?)
159053541Sshin *	SIOCGLIFADDR with IFLR_PREFIX:
159153541Sshin *		get first address that matches the specified prefix.
159253541Sshin *	SIOCALIFADDR: add the specified address.
159353541Sshin *	SIOCALIFADDR with IFLR_PREFIX:
159453541Sshin *		add the specified prefix, filling hostid part from
159553541Sshin *		the first link-local address.  prefixlen must be <= 64.
159653541Sshin *	SIOCDLIFADDR: delete the specified address.
159753541Sshin *	SIOCDLIFADDR with IFLR_PREFIX:
159853541Sshin *		delete the first address that matches the specified prefix.
159953541Sshin * return values:
160053541Sshin *	EINVAL on invalid parameters
160153541Sshin *	EADDRNOTAVAIL on prefix match failed/specified address not found
160253541Sshin *	other values may be returned from in6_ioctl()
160353541Sshin *
160453541Sshin * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
160553541Sshin * this is to accomodate address naming scheme other than RFC2374,
160653541Sshin * in the future.
160753541Sshin * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
160853541Sshin * address encoding scheme. (see figure on page 8)
1609287734Shrs * Notifies other subsystems about address change/arrival:
1610287734Shrs * 1) Notifies device handler on the first IPv6 address assignment
1611287734Shrs * 2) Handle routing table changes for P2P links and route
1612287734Shrs * 3) Handle routing table changes for address host route
161353541Sshin */
161453541Sshinstatic int
1615171259Sdelphijin6_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
1616171259Sdelphij    struct ifnet *ifp, struct thread *td)
161753541Sshin{
161853541Sshin	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
161953541Sshin	struct ifaddr *ifa;
162062587Sitojun	struct sockaddr *sa;
162153541Sshin
162253541Sshin	/* sanity checks */
162353541Sshin	if (!data || !ifp) {
162453541Sshin		panic("invalid argument to in6_lifaddr_ioctl");
1625120891Sume		/* NOTREACHED */
162653541Sshin	}
162753541Sshin
162853541Sshin	switch (cmd) {
162953541Sshin	case SIOCGLIFADDR:
163053541Sshin		/* address must be specified on GET with IFLR_PREFIX */
163153541Sshin		if ((iflr->flags & IFLR_PREFIX) == 0)
163253541Sshin			break;
163395023Ssuz		/* FALLTHROUGH */
163453541Sshin	case SIOCALIFADDR:
163553541Sshin	case SIOCDLIFADDR:
163653541Sshin		/* address must be specified on ADD and DELETE */
163762587Sitojun		sa = (struct sockaddr *)&iflr->addr;
163862587Sitojun		if (sa->sa_family != AF_INET6)
163953541Sshin			return EINVAL;
164062587Sitojun		if (sa->sa_len != sizeof(struct sockaddr_in6))
164153541Sshin			return EINVAL;
164253541Sshin		/* XXX need improvement */
164362587Sitojun		sa = (struct sockaddr *)&iflr->dstaddr;
164462587Sitojun		if (sa->sa_family && sa->sa_family != AF_INET6)
164553541Sshin			return EINVAL;
164662587Sitojun		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
164753541Sshin			return EINVAL;
164853541Sshin		break;
164995023Ssuz	default: /* shouldn't happen */
165062587Sitojun#if 0
165162587Sitojun		panic("invalid cmd to in6_lifaddr_ioctl");
165295023Ssuz		/* NOTREACHED */
165362587Sitojun#else
165453541Sshin		return EOPNOTSUPP;
165562587Sitojun#endif
165653541Sshin	}
165753541Sshin	if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
165853541Sshin		return EINVAL;
165953541Sshin
166053541Sshin	switch (cmd) {
166153541Sshin	case SIOCALIFADDR:
166253541Sshin	    {
166353541Sshin		struct in6_aliasreq ifra;
166453541Sshin		struct in6_addr *hostid = NULL;
166553541Sshin		int prefixlen;
166653541Sshin
1667194760Srwatson		ifa = NULL;
166853541Sshin		if ((iflr->flags & IFLR_PREFIX) != 0) {
166953541Sshin			struct sockaddr_in6 *sin6;
167053541Sshin
167153541Sshin			/*
167253541Sshin			 * hostid is to fill in the hostid part of the
167353541Sshin			 * address.  hostid points to the first link-local
167453541Sshin			 * address attached to the interface.
167553541Sshin			 */
167662587Sitojun			ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
167753541Sshin			if (!ifa)
167853541Sshin				return EADDRNOTAVAIL;
167953541Sshin			hostid = IFA_IN6(ifa);
168053541Sshin
1681171260Sdelphij			/* prefixlen must be <= 64. */
1682236615Sbz			if (64 < iflr->prefixlen) {
1683236615Sbz				if (ifa != NULL)
1684236615Sbz					ifa_free(ifa);
168553541Sshin				return EINVAL;
1686236615Sbz			}
168753541Sshin			prefixlen = iflr->prefixlen;
168853541Sshin
168953541Sshin			/* hostid part must be zero. */
169053541Sshin			sin6 = (struct sockaddr_in6 *)&iflr->addr;
1691126552Sume			if (sin6->sin6_addr.s6_addr32[2] != 0 ||
1692126552Sume			    sin6->sin6_addr.s6_addr32[3] != 0) {
1693236615Sbz				if (ifa != NULL)
1694236615Sbz					ifa_free(ifa);
169553541Sshin				return EINVAL;
169653541Sshin			}
169753541Sshin		} else
169853541Sshin			prefixlen = iflr->prefixlen;
169953541Sshin
170053541Sshin		/* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
170153541Sshin		bzero(&ifra, sizeof(ifra));
1702120891Sume		bcopy(iflr->iflr_name, ifra.ifra_name, sizeof(ifra.ifra_name));
170353541Sshin
170462587Sitojun		bcopy(&iflr->addr, &ifra.ifra_addr,
1705120891Sume		    ((struct sockaddr *)&iflr->addr)->sa_len);
170653541Sshin		if (hostid) {
170753541Sshin			/* fill in hostid part */
170853541Sshin			ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1709120891Sume			    hostid->s6_addr32[2];
171053541Sshin			ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1711120891Sume			    hostid->s6_addr32[3];
171253541Sshin		}
171353541Sshin
1714120891Sume		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */
171553541Sshin			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1716120891Sume			    ((struct sockaddr *)&iflr->dstaddr)->sa_len);
171753541Sshin			if (hostid) {
171853541Sshin				ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1719120891Sume				    hostid->s6_addr32[2];
172053541Sshin				ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1721120891Sume				    hostid->s6_addr32[3];
172253541Sshin			}
172353541Sshin		}
1724194760Srwatson		if (ifa != NULL)
1725194760Srwatson			ifa_free(ifa);
172653541Sshin
172753541Sshin		ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
1728121168Sume		in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
172953541Sshin
173053541Sshin		ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
173183366Sjulian		return in6_control(so, SIOCAIFADDR_IN6, (caddr_t)&ifra, ifp, td);
173253541Sshin	    }
173353541Sshin	case SIOCGLIFADDR:
173453541Sshin	case SIOCDLIFADDR:
173553541Sshin	    {
173653541Sshin		struct in6_ifaddr *ia;
173753541Sshin		struct in6_addr mask, candidate, match;
173853541Sshin		struct sockaddr_in6 *sin6;
173953541Sshin		int cmp;
174053541Sshin
174153541Sshin		bzero(&mask, sizeof(mask));
174253541Sshin		if (iflr->flags & IFLR_PREFIX) {
174353541Sshin			/* lookup a prefix rather than address. */
1744121168Sume			in6_prefixlen2mask(&mask, iflr->prefixlen);
174553541Sshin
174653541Sshin			sin6 = (struct sockaddr_in6 *)&iflr->addr;
174753541Sshin			bcopy(&sin6->sin6_addr, &match, sizeof(match));
174853541Sshin			match.s6_addr32[0] &= mask.s6_addr32[0];
174953541Sshin			match.s6_addr32[1] &= mask.s6_addr32[1];
175053541Sshin			match.s6_addr32[2] &= mask.s6_addr32[2];
175153541Sshin			match.s6_addr32[3] &= mask.s6_addr32[3];
175253541Sshin
175353541Sshin			/* if you set extra bits, that's wrong */
175453541Sshin			if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
175553541Sshin				return EINVAL;
175653541Sshin
175753541Sshin			cmp = 1;
175853541Sshin		} else {
175953541Sshin			if (cmd == SIOCGLIFADDR) {
176053541Sshin				/* on getting an address, take the 1st match */
176195023Ssuz				cmp = 0;	/* XXX */
176253541Sshin			} else {
176353541Sshin				/* on deleting an address, do exact match */
1764121168Sume				in6_prefixlen2mask(&mask, 128);
176553541Sshin				sin6 = (struct sockaddr_in6 *)&iflr->addr;
176653541Sshin				bcopy(&sin6->sin6_addr, &match, sizeof(match));
176753541Sshin
176853541Sshin				cmp = 1;
176953541Sshin			}
177053541Sshin		}
177153541Sshin
1772229621Sjhb		IF_ADDR_RLOCK(ifp);
1773191340Srwatson		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
177453541Sshin			if (ifa->ifa_addr->sa_family != AF_INET6)
177553541Sshin				continue;
177653541Sshin			if (!cmp)
177753541Sshin				break;
177878064Sume
177978064Sume			/*
178078064Sume			 * XXX: this is adhoc, but is necessary to allow
178178064Sume			 * a user to specify fe80::/64 (not /10) for a
178278064Sume			 * link-local address.
178378064Sume			 */
1784148385Sume			bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
1785148385Sume			in6_clearscope(&candidate);
178653541Sshin			candidate.s6_addr32[0] &= mask.s6_addr32[0];
178753541Sshin			candidate.s6_addr32[1] &= mask.s6_addr32[1];
178853541Sshin			candidate.s6_addr32[2] &= mask.s6_addr32[2];
178953541Sshin			candidate.s6_addr32[3] &= mask.s6_addr32[3];
179053541Sshin			if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
179153541Sshin				break;
179253541Sshin		}
1793229414Sjhb		if (ifa != NULL)
1794229414Sjhb			ifa_ref(ifa);
1795229621Sjhb		IF_ADDR_RUNLOCK(ifp);
179653541Sshin		if (!ifa)
179753541Sshin			return EADDRNOTAVAIL;
179853541Sshin		ia = ifa2ia6(ifa);
179953541Sshin
180053541Sshin		if (cmd == SIOCGLIFADDR) {
1801148385Sume			int error;
180278064Sume
180353541Sshin			/* fill in the if_laddrreq structure */
180453541Sshin			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
1805148385Sume			error = sa6_recoverscope(
1806148385Sume			    (struct sockaddr_in6 *)&iflr->addr);
1807229414Sjhb			if (error != 0) {
1808229414Sjhb				ifa_free(ifa);
1809148385Sume				return (error);
1810229414Sjhb			}
1811148385Sume
181253541Sshin			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
181353541Sshin				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1814120891Sume				    ia->ia_dstaddr.sin6_len);
1815148385Sume				error = sa6_recoverscope(
1816148385Sume				    (struct sockaddr_in6 *)&iflr->dstaddr);
1817229414Sjhb				if (error != 0) {
1818229414Sjhb					ifa_free(ifa);
1819148385Sume					return (error);
1820229414Sjhb				}
182153541Sshin			} else
182253541Sshin				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
182353541Sshin
182453541Sshin			iflr->prefixlen =
1825120891Sume			    in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
182653541Sshin
182795023Ssuz			iflr->flags = ia->ia6_flags;	/* XXX */
1828229414Sjhb			ifa_free(ifa);
182953541Sshin
183053541Sshin			return 0;
183153541Sshin		} else {
183253541Sshin			struct in6_aliasreq ifra;
183353541Sshin
183453541Sshin			/* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
183553541Sshin			bzero(&ifra, sizeof(ifra));
183653541Sshin			bcopy(iflr->iflr_name, ifra.ifra_name,
1837120891Sume			    sizeof(ifra.ifra_name));
183853541Sshin
183953541Sshin			bcopy(&ia->ia_addr, &ifra.ifra_addr,
1840120891Sume			    ia->ia_addr.sin6_len);
184153541Sshin			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
184253541Sshin				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
1843120891Sume				    ia->ia_dstaddr.sin6_len);
184462587Sitojun			} else {
184562587Sitojun				bzero(&ifra.ifra_dstaddr,
184662587Sitojun				    sizeof(ifra.ifra_dstaddr));
184753541Sshin			}
184853541Sshin			bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
1849120891Sume			    ia->ia_prefixmask.sin6_len);
185053541Sshin
185153541Sshin			ifra.ifra_flags = ia->ia6_flags;
1852229414Sjhb			ifa_free(ifa);
185353541Sshin			return in6_control(so, SIOCDIFADDR_IN6, (caddr_t)&ifra,
1854120891Sume			    ifp, td);
185553541Sshin		}
185653541Sshin	    }
185753541Sshin	}
185853541Sshin
185995023Ssuz	return EOPNOTSUPP;	/* just for safety */
186053541Sshin}
186153541Sshin
186253541Sshin/*
1863231852Sbz * Initialize an interface's IPv6 address and routing table entry.
186453541Sshin */
186578064Sumestatic int
1866171259Sdelphijin6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia,
1867171259Sdelphij    struct sockaddr_in6 *sin6, int newhost)
186853541Sshin{
186978064Sume	int	error = 0, plen, ifacount = 0;
187078064Sume	struct ifaddr *ifa;
187153541Sshin
187253541Sshin	/*
187353541Sshin	 * Give the interface a chance to initialize
187453541Sshin	 * if this is its first address,
187553541Sshin	 * and to validate the address if necessary.
187653541Sshin	 */
1877229621Sjhb	IF_ADDR_RLOCK(ifp);
1878191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
187978064Sume		if (ifa->ifa_addr->sa_family != AF_INET6)
188078064Sume			continue;
188178064Sume		ifacount++;
188278064Sume	}
1883229621Sjhb	IF_ADDR_RUNLOCK(ifp);
188478064Sume
188578064Sume	ia->ia_addr = *sin6;
188678064Sume
1887146883Siedowse	if (ifacount <= 1 && ifp->if_ioctl) {
1888146883Siedowse		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
1889241686Sandre		if (error)
1890146883Siedowse			return (error);
189153541Sshin	}
189253541Sshin
189378064Sume	ia->ia_ifa.ifa_metric = ifp->if_metric;
189453541Sshin
189578064Sume	/* we could do in(6)_socktrim here, but just omit it at this moment. */
189678064Sume
189753541Sshin	/*
189878064Sume	 * Special case:
1899124337Sume	 * If a new destination address is specified for a point-to-point
190078064Sume	 * interface, install a route to the destination as an interface
1901238945Sglebius	 * direct route.
1902124337Sume	 * XXX: the logic below rejects assigning multiple addresses on a p2p
1903159390Sgnn	 * interface that share the same destination.
190453541Sshin	 */
190578064Sume	plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1906196152Sqingli	if (!(ia->ia_flags & IFA_ROUTE) && plen == 128 &&
1907196152Sqingli	    ia->ia_dstaddr.sin6_family == AF_INET6) {
1908159390Sgnn		int rtflags = RTF_UP | RTF_HOST;
1909231852Sbz		error = rtinit(&ia->ia_ifa, RTM_ADD, ia->ia_flags | rtflags);
1910231852Sbz		if (error)
1911120856Sume			return (error);
191278064Sume		ia->ia_flags |= IFA_ROUTE;
1913226453Sqingli		/*
1914226453Sqingli		 * Handle the case for ::1 .
1915226453Sqingli		 */
1916226453Sqingli		if (ifp->if_flags & IFF_LOOPBACK)
1917226453Sqingli			ia->ia_flags |= IFA_RTSELF;
191853541Sshin	}
191953541Sshin
1920195643Sqingli	/*
1921195643Sqingli	 * add a loopback route to self
1922195643Sqingli	 */
1923226453Sqingli	if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) {
1924197227Sqingli		error = ifa_add_loopback_route((struct ifaddr *)ia,
1925238945Sglebius		    (struct sockaddr *)&ia->ia_addr);
1926201282Sqingli		if (error == 0)
1927201282Sqingli			ia->ia_flags |= IFA_RTSELF;
1928195643Sqingli	}
1929195643Sqingli
1930231852Sbz	/* Add local address to lltable, if necessary (ex. on p2p link). */
1931226338Sglebius	if (newhost)
1932226338Sglebius		in6_ifaddloop(&(ia->ia_ifa));
193353541Sshin
1934120856Sume	return (error);
193553541Sshin}
193653541Sshin
193753541Sshin/*
193853541Sshin * Find an IPv6 interface link-local address specific to an interface.
1939194760Srwatson * ifaddr is returned referenced.
194053541Sshin */
194153541Sshinstruct in6_ifaddr *
1942171259Sdelphijin6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
194353541Sshin{
194478064Sume	struct ifaddr *ifa;
194553541Sshin
1946229621Sjhb	IF_ADDR_RLOCK(ifp);
1947191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
194853541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
194953541Sshin			continue;
195062587Sitojun		if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
195162587Sitojun			if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1952238945Sglebius			    ignoreflags) != 0)
195362587Sitojun				continue;
1954194760Srwatson			ifa_ref(ifa);
195553541Sshin			break;
195662587Sitojun		}
195753541Sshin	}
1958229621Sjhb	IF_ADDR_RUNLOCK(ifp);
195953541Sshin
1960120856Sume	return ((struct in6_ifaddr *)ifa);
196153541Sshin}
196253541Sshin
196353541Sshin
196453541Sshin/*
196553541Sshin * find the internet address corresponding to a given interface and address.
1966194760Srwatson * ifaddr is returned referenced.
196753541Sshin */
196853541Sshinstruct in6_ifaddr *
1969171259Sdelphijin6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
197053541Sshin{
197178064Sume	struct ifaddr *ifa;
197253541Sshin
1973229621Sjhb	IF_ADDR_RLOCK(ifp);
1974191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
197553541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
197653541Sshin			continue;
1977194760Srwatson		if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) {
1978194760Srwatson			ifa_ref(ifa);
197953541Sshin			break;
1980194760Srwatson		}
198153541Sshin	}
1982229621Sjhb	IF_ADDR_RUNLOCK(ifp);
198353541Sshin
1984120856Sume	return ((struct in6_ifaddr *)ifa);
198553541Sshin}
198653541Sshin
198753541Sshin/*
1988252511Shrs * Find a link-local scoped address on ifp and return it if any.
1989252511Shrs */
1990252511Shrsstruct in6_ifaddr *
1991252511Shrsin6ifa_llaonifp(struct ifnet *ifp)
1992252511Shrs{
1993252511Shrs	struct sockaddr_in6 *sin6;
1994252511Shrs	struct ifaddr *ifa;
1995252511Shrs
1996252511Shrs	if (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)
1997252511Shrs		return (NULL);
1998294503Sbz	IF_ADDR_RLOCK(ifp);
1999252511Shrs	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2000252511Shrs		if (ifa->ifa_addr->sa_family != AF_INET6)
2001252511Shrs			continue;
2002252511Shrs		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
2003252511Shrs		if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
2004252511Shrs		    IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
2005252511Shrs		    IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
2006252511Shrs			break;
2007252511Shrs	}
2008294503Sbz	IF_ADDR_RUNLOCK(ifp);
2009252511Shrs
2010252511Shrs	return ((struct in6_ifaddr *)ifa);
2011252511Shrs}
2012252511Shrs
2013252511Shrs/*
2014165118Sbz * Convert IP6 address to printable (loggable) representation. Caller
2015165118Sbz * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long.
201653541Sshin */
201753541Sshinstatic char digits[] = "0123456789abcdef";
201853541Sshinchar *
2019165118Sbzip6_sprintf(char *ip6buf, const struct in6_addr *addr)
202053541Sshin{
2021208284Salfred	int i, cnt = 0, maxcnt = 0, idx = 0, index = 0;
202278064Sume	char *cp;
2023126552Sume	const u_int16_t *a = (const u_int16_t *)addr;
2024126552Sume	const u_int8_t *d;
2025165287Sbz	int dcolon = 0, zero = 0;
202653541Sshin
2027165118Sbz	cp = ip6buf;
202853541Sshin
202953541Sshin	for (i = 0; i < 8; i++) {
2030208284Salfred		if (*(a + i) == 0) {
2031208284Salfred			cnt++;
2032208284Salfred			if (cnt == 1)
2033208284Salfred				idx = i;
2034208284Salfred		}
2035208284Salfred		else if (maxcnt < cnt) {
2036208284Salfred			maxcnt = cnt;
2037208284Salfred			index = idx;
2038208284Salfred			cnt = 0;
2039208284Salfred		}
2040208284Salfred	}
2041208284Salfred	if (maxcnt < cnt) {
2042208284Salfred		maxcnt = cnt;
2043208284Salfred		index = idx;
2044208284Salfred	}
2045208284Salfred
2046208284Salfred	for (i = 0; i < 8; i++) {
204753541Sshin		if (dcolon == 1) {
204853541Sshin			if (*a == 0) {
204953541Sshin				if (i == 7)
205053541Sshin					*cp++ = ':';
205153541Sshin				a++;
205253541Sshin				continue;
205353541Sshin			} else
205453541Sshin				dcolon = 2;
205553541Sshin		}
205653541Sshin		if (*a == 0) {
2057208284Salfred			if (dcolon == 0 && *(a + 1) == 0 && i == index) {
205853541Sshin				if (i == 0)
205953541Sshin					*cp++ = ':';
206053541Sshin				*cp++ = ':';
206153541Sshin				dcolon = 1;
206253541Sshin			} else {
206353541Sshin				*cp++ = '0';
206453541Sshin				*cp++ = ':';
206553541Sshin			}
206653541Sshin			a++;
206753541Sshin			continue;
206853541Sshin		}
206991346Salfred		d = (const u_char *)a;
2070165287Sbz		/* Try to eliminate leading zeros in printout like in :0001. */
2071165287Sbz		zero = 1;
2072165287Sbz		*cp = digits[*d >> 4];
2073165287Sbz		if (*cp != '0') {
2074165287Sbz			zero = 0;
2075165287Sbz			cp++;
2076165287Sbz		}
2077165287Sbz		*cp = digits[*d++ & 0xf];
2078165287Sbz		if (zero == 0 || (*cp != '0')) {
2079165287Sbz			zero = 0;
2080165287Sbz			cp++;
2081165287Sbz		}
2082165287Sbz		*cp = digits[*d >> 4];
2083165287Sbz		if (zero == 0 || (*cp != '0')) {
2084165287Sbz			zero = 0;
2085165287Sbz			cp++;
2086165287Sbz		}
208753541Sshin		*cp++ = digits[*d & 0xf];
208853541Sshin		*cp++ = ':';
208953541Sshin		a++;
209053541Sshin	}
2091165118Sbz	*--cp = '\0';
2092165118Sbz	return (ip6buf);
209353541Sshin}
209453541Sshin
209553541Sshinint
2096171259Sdelphijin6_localaddr(struct in6_addr *in6)
209753541Sshin{
209853541Sshin	struct in6_ifaddr *ia;
209953541Sshin
210053541Sshin	if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
210153541Sshin		return 1;
210253541Sshin
2103194971Srwatson	IN6_IFADDR_RLOCK();
2104194907Srwatson	TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
210553541Sshin		if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
2106120891Sume		    &ia->ia_prefixmask.sin6_addr)) {
2107194971Srwatson			IN6_IFADDR_RUNLOCK();
210853541Sshin			return 1;
2109120891Sume		}
2110120891Sume	}
2111194971Srwatson	IN6_IFADDR_RUNLOCK();
211253541Sshin
211353541Sshin	return (0);
211453541Sshin}
211553541Sshin
2116225043Sbz/*
2117225043Sbz * Return 1 if an internet address is for the local host and configured
2118225043Sbz * on one of its interfaces.
2119225043Sbz */
212078064Sumeint
2121225043Sbzin6_localip(struct in6_addr *in6)
2122225043Sbz{
2123225043Sbz	struct in6_ifaddr *ia;
2124225043Sbz
2125225043Sbz	IN6_IFADDR_RLOCK();
2126244272Sae	LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) {
2127225043Sbz		if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) {
2128225043Sbz			IN6_IFADDR_RUNLOCK();
2129225043Sbz			return (1);
2130225043Sbz		}
2131225043Sbz	}
2132225043Sbz	IN6_IFADDR_RUNLOCK();
2133225043Sbz	return (0);
2134225043Sbz}
2135225043Sbz
2136225043Sbzint
2137171259Sdelphijin6_is_addr_deprecated(struct sockaddr_in6 *sa6)
213878064Sume{
213978064Sume	struct in6_ifaddr *ia;
214078064Sume
2141194971Srwatson	IN6_IFADDR_RLOCK();
2142244272Sae	LIST_FOREACH(ia, IN6ADDR_HASH(&sa6->sin6_addr), ia6_hash) {
2143244272Sae		if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), &sa6->sin6_addr)) {
2144244272Sae			if (ia->ia6_flags & IN6_IFF_DEPRECATED) {
2145244272Sae				IN6_IFADDR_RUNLOCK();
2146244272Sae				return (1); /* true */
2147244272Sae			}
2148244272Sae			break;
2149194971Srwatson		}
215078064Sume	}
2151194971Srwatson	IN6_IFADDR_RUNLOCK();
215278064Sume
2153120856Sume	return (0);		/* false */
215478064Sume}
215578064Sume
215653541Sshin/*
215753541Sshin * return length of part which dst and src are equal
215853541Sshin * hard coding...
215953541Sshin */
216053541Sshinint
2161171259Sdelphijin6_matchlen(struct in6_addr *src, struct in6_addr *dst)
216253541Sshin{
216353541Sshin	int match = 0;
216453541Sshin	u_char *s = (u_char *)src, *d = (u_char *)dst;
216553541Sshin	u_char *lim = s + 16, r;
216653541Sshin
216753541Sshin	while (s < lim)
216853541Sshin		if ((r = (*d++ ^ *s++)) != 0) {
216953541Sshin			while (r < 128) {
217053541Sshin				match++;
217153541Sshin				r <<= 1;
217253541Sshin			}
217353541Sshin			break;
217453541Sshin		} else
217553541Sshin			match += 8;
217653541Sshin	return match;
217753541Sshin}
217853541Sshin
217962587Sitojun/* XXX: to be scope conscious */
218053541Sshinint
2181171259Sdelphijin6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
218253541Sshin{
218353541Sshin	int bytelen, bitlen;
218453541Sshin
218553541Sshin	/* sanity check */
218653541Sshin	if (0 > len || len > 128) {
218753541Sshin		log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
218853541Sshin		    len);
2189120856Sume		return (0);
219053541Sshin	}
219153541Sshin
219253541Sshin	bytelen = len / 8;
219353541Sshin	bitlen = len % 8;
219453541Sshin
219553541Sshin	if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
2196120856Sume		return (0);
2197126184Scperciva	if (bitlen != 0 &&
2198126184Scperciva	    p1->s6_addr[bytelen] >> (8 - bitlen) !=
219953541Sshin	    p2->s6_addr[bytelen] >> (8 - bitlen))
2200120856Sume		return (0);
220153541Sshin
2202120856Sume	return (1);
220353541Sshin}
220453541Sshin
220553541Sshinvoid
2206171259Sdelphijin6_prefixlen2mask(struct in6_addr *maskp, int len)
220753541Sshin{
220853541Sshin	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
220953541Sshin	int bytelen, bitlen, i;
221053541Sshin
221153541Sshin	/* sanity check */
221253541Sshin	if (0 > len || len > 128) {
221353541Sshin		log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
221453541Sshin		    len);
221553541Sshin		return;
221653541Sshin	}
221753541Sshin
221853541Sshin	bzero(maskp, sizeof(*maskp));
221953541Sshin	bytelen = len / 8;
222053541Sshin	bitlen = len % 8;
222153541Sshin	for (i = 0; i < bytelen; i++)
222253541Sshin		maskp->s6_addr[i] = 0xff;
222353541Sshin	if (bitlen)
222453541Sshin		maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
222553541Sshin}
222653541Sshin
222753541Sshin/*
222853541Sshin * return the best address out of the same scope. if no address was
222953541Sshin * found, return the first valid address from designated IF.
223053541Sshin */
223153541Sshinstruct in6_ifaddr *
2232171259Sdelphijin6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
223353541Sshin{
223453541Sshin	int dst_scope =	in6_addrscope(dst), blen = -1, tlen;
223553541Sshin	struct ifaddr *ifa;
223653541Sshin	struct in6_ifaddr *besta = 0;
223795023Ssuz	struct in6_ifaddr *dep[2];	/* last-resort: deprecated */
223853541Sshin
223953541Sshin	dep[0] = dep[1] = NULL;
224053541Sshin
224153541Sshin	/*
224253541Sshin	 * We first look for addresses in the same scope.
224353541Sshin	 * If there is one, return it.
224453541Sshin	 * If two or more, return one which matches the dst longest.
224553541Sshin	 * If none, return one of global addresses assigned other ifs.
224653541Sshin	 */
2247229621Sjhb	IF_ADDR_RLOCK(ifp);
2248191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
224953541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
225053541Sshin			continue;
225153541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
225253541Sshin			continue; /* XXX: is there any case to allow anycast? */
225353541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
225453541Sshin			continue; /* don't use this interface */
225553541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
225653541Sshin			continue;
225753541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2258181803Sbz			if (V_ip6_use_deprecated)
225953541Sshin				dep[0] = (struct in6_ifaddr *)ifa;
226053541Sshin			continue;
226153541Sshin		}
226253541Sshin
226353541Sshin		if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
226453541Sshin			/*
226553541Sshin			 * call in6_matchlen() as few as possible
226653541Sshin			 */
226753541Sshin			if (besta) {
226853541Sshin				if (blen == -1)
226953541Sshin					blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
227053541Sshin				tlen = in6_matchlen(IFA_IN6(ifa), dst);
227153541Sshin				if (tlen > blen) {
227253541Sshin					blen = tlen;
227353541Sshin					besta = (struct in6_ifaddr *)ifa;
227453541Sshin				}
227553541Sshin			} else
227653541Sshin				besta = (struct in6_ifaddr *)ifa;
227753541Sshin		}
227853541Sshin	}
2279191323Srwatson	if (besta) {
2280194760Srwatson		ifa_ref(&besta->ia_ifa);
2281229621Sjhb		IF_ADDR_RUNLOCK(ifp);
2282120856Sume		return (besta);
2283191323Srwatson	}
228453541Sshin
2285191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
228653541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
228753541Sshin			continue;
228853541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
228953541Sshin			continue; /* XXX: is there any case to allow anycast? */
229053541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
229153541Sshin			continue; /* don't use this interface */
229253541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
229353541Sshin			continue;
229453541Sshin		if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2295181803Sbz			if (V_ip6_use_deprecated)
229653541Sshin				dep[1] = (struct in6_ifaddr *)ifa;
229753541Sshin			continue;
229853541Sshin		}
229953541Sshin
2300194760Srwatson		if (ifa != NULL)
2301194760Srwatson			ifa_ref(ifa);
2302229621Sjhb		IF_ADDR_RUNLOCK(ifp);
230353541Sshin		return (struct in6_ifaddr *)ifa;
230453541Sshin	}
230553541Sshin
230653541Sshin	/* use the last-resort values, that are, deprecated addresses */
2307236327Semax	if (dep[0]) {
2308236327Semax		ifa_ref((struct ifaddr *)dep[0]);
2309236327Semax		IF_ADDR_RUNLOCK(ifp);
231053541Sshin		return dep[0];
2311236327Semax	}
2312236327Semax	if (dep[1]) {
2313236327Semax		ifa_ref((struct ifaddr *)dep[1]);
2314236327Semax		IF_ADDR_RUNLOCK(ifp);
231553541Sshin		return dep[1];
2316236327Semax	}
231753541Sshin
2318236327Semax	IF_ADDR_RUNLOCK(ifp);
231953541Sshin	return NULL;
232053541Sshin}
232153541Sshin
232253541Sshin/*
232353541Sshin * perform DAD when interface becomes IFF_UP.
232453541Sshin */
232553541Sshinvoid
2326171259Sdelphijin6_if_up(struct ifnet *ifp)
232753541Sshin{
232853541Sshin	struct ifaddr *ifa;
232953541Sshin	struct in6_ifaddr *ia;
233053541Sshin
2331229621Sjhb	IF_ADDR_RLOCK(ifp);
2332191340Srwatson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
233353541Sshin		if (ifa->ifa_addr->sa_family != AF_INET6)
233453541Sshin			continue;
233553541Sshin		ia = (struct in6_ifaddr *)ifa;
2336151539Ssuz		if (ia->ia6_flags & IN6_IFF_TENTATIVE) {
2337151539Ssuz			/*
2338151539Ssuz			 * The TENTATIVE flag was likely set by hand
2339151539Ssuz			 * beforehand, implicitly indicating the need for DAD.
2340151539Ssuz			 * We may be able to skip the random delay in this
2341151539Ssuz			 * case, but we impose delays just in case.
2342151539Ssuz			 */
2343151539Ssuz			nd6_dad_start(ifa,
2344151539Ssuz			    arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz));
2345151539Ssuz		}
234653541Sshin	}
2347229621Sjhb	IF_ADDR_RUNLOCK(ifp);
2348151539Ssuz
2349151539Ssuz	/*
2350151539Ssuz	 * special cases, like 6to4, are handled in in6_ifattach
2351151539Ssuz	 */
2352151539Ssuz	in6_ifattach(ifp, NULL);
235353541Sshin}
235453541Sshin
235578064Sumeint
2356171259Sdelphijin6if_do_dad(struct ifnet *ifp)
235778064Sume{
235878064Sume	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2359120856Sume		return (0);
236078064Sume
2361282622Shiren	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) ||
2362282622Shiren	    (ND_IFINFO(ifp)->flags & ND6_IFF_NO_DAD))
2363197138Shrs		return (0);
2364197138Shrs
236578064Sume	switch (ifp->if_type) {
236678064Sume#ifdef IFT_DUMMY
236778064Sume	case IFT_DUMMY:
236878064Sume#endif
236978064Sume	case IFT_FAITH:
237078064Sume		/*
237178064Sume		 * These interfaces do not have the IFF_LOOPBACK flag,
237278064Sume		 * but loop packets back.  We do not have to do DAD on such
237378064Sume		 * interfaces.  We should even omit it, because loop-backed
237478064Sume		 * NS would confuse the DAD procedure.
237578064Sume		 */
2376120856Sume		return (0);
237778064Sume	default:
237878064Sume		/*
237978064Sume		 * Our DAD routine requires the interface up and running.
238078064Sume		 * However, some interfaces can be up before the RUNNING
238178064Sume		 * status.  Additionaly, users may try to assign addresses
238278064Sume		 * before the interface becomes up (or running).
2383288109Sgarga		 * This function returns EAGAIN in that case.
2384287734Shrs		 * The caller should mark "tentative" on the address instead of
2385287734Shrs		 * performing DAD immediately.
238678064Sume		 */
2387148887Srwatson		if (!((ifp->if_flags & IFF_UP) &&
2388148887Srwatson		    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
2389287734Shrs			return (EAGAIN);
239078064Sume
2391120856Sume		return (1);
239278064Sume	}
239378064Sume}
239478064Sume
239553541Sshin/*
239653541Sshin * Calculate max IPv6 MTU through all the interfaces and store it
239753541Sshin * to in6_maxmtu.
239853541Sshin */
239953541Sshinvoid
2400171259Sdelphijin6_setmaxmtu(void)
240153541Sshin{
240253541Sshin	unsigned long maxmtu = 0;
240353541Sshin	struct ifnet *ifp;
240453541Sshin
2405196481Srwatson	IFNET_RLOCK_NOSLEEP();
2406228966Sjhb	TAILQ_FOREACH(ifp, &V_ifnet, if_list) {
2407121283Sume		/* this function can be called during ifnet initialization */
2408121283Sume		if (!ifp->if_afdata[AF_INET6])
2409121283Sume			continue;
241053541Sshin		if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
2411121283Sume		    IN6_LINKMTU(ifp) > maxmtu)
2412121283Sume			maxmtu = IN6_LINKMTU(ifp);
241353541Sshin	}
2414196481Srwatson	IFNET_RUNLOCK_NOSLEEP();
2415238945Sglebius	if (maxmtu)	/* update only when maxmtu is positive */
2416181803Sbz		V_in6_maxmtu = maxmtu;
241753541Sshin}
241853541Sshin
2419151539Ssuz/*
2420151539Ssuz * Provide the length of interface identifiers to be used for the link attached
2421151539Ssuz * to the given interface.  The length should be defined in "IPv6 over
2422151539Ssuz * xxx-link" document.  Note that address architecture might also define
2423151539Ssuz * the length for a particular set of address prefixes, regardless of the
2424151539Ssuz * link type.  As clarified in rfc2462bis, those two definitions should be
2425151539Ssuz * consistent, and those really are as of August 2004.
2426151539Ssuz */
2427151539Ssuzint
2428171259Sdelphijin6_if2idlen(struct ifnet *ifp)
2429151539Ssuz{
2430151539Ssuz	switch (ifp->if_type) {
2431151539Ssuz	case IFT_ETHER:		/* RFC2464 */
2432151539Ssuz#ifdef IFT_PROPVIRTUAL
2433151539Ssuz	case IFT_PROPVIRTUAL:	/* XXX: no RFC. treat it as ether */
2434151539Ssuz#endif
2435151539Ssuz#ifdef IFT_L2VLAN
2436151539Ssuz	case IFT_L2VLAN:	/* ditto */
2437151539Ssuz#endif
2438151539Ssuz#ifdef IFT_IEEE80211
2439151539Ssuz	case IFT_IEEE80211:	/* ditto */
2440151539Ssuz#endif
2441151539Ssuz#ifdef IFT_MIP
2442151539Ssuz	case IFT_MIP:	/* ditto */
2443151539Ssuz#endif
2444293358Swollman	case IFT_BRIDGE:	/* bridge(4) only does Ethernet-like links */
2445219819Sjeff	case IFT_INFINIBAND:
2446151539Ssuz		return (64);
2447151539Ssuz	case IFT_FDDI:		/* RFC2467 */
2448151539Ssuz		return (64);
2449151539Ssuz	case IFT_ISO88025:	/* RFC2470 (IPv6 over Token Ring) */
2450151539Ssuz		return (64);
2451151539Ssuz	case IFT_PPP:		/* RFC2472 */
2452151539Ssuz		return (64);
2453151539Ssuz	case IFT_ARCNET:	/* RFC2497 */
2454151539Ssuz		return (64);
2455151539Ssuz	case IFT_FRELAY:	/* RFC2590 */
2456151539Ssuz		return (64);
2457151539Ssuz	case IFT_IEEE1394:	/* RFC3146 */
2458151539Ssuz		return (64);
2459151539Ssuz	case IFT_GIF:
2460151539Ssuz		return (64);	/* draft-ietf-v6ops-mech-v2-07 */
2461151539Ssuz	case IFT_LOOP:
2462151539Ssuz		return (64);	/* XXX: is this really correct? */
2463151539Ssuz	default:
2464151539Ssuz		/*
2465151539Ssuz		 * Unknown link type:
2466151539Ssuz		 * It might be controversial to use the today's common constant
2467151539Ssuz		 * of 64 for these cases unconditionally.  For full compliance,
2468151539Ssuz		 * we should return an error in this case.  On the other hand,
2469151539Ssuz		 * if we simply miss the standard for the link type or a new
2470151539Ssuz		 * standard is defined for a new link type, the IFID length
2471151539Ssuz		 * is very likely to be the common constant.  As a compromise,
2472151539Ssuz		 * we always use the constant, but make an explicit notice
2473151539Ssuz		 * indicating the "unknown" case.
2474151539Ssuz		 */
2475151539Ssuz		printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type);
2476151539Ssuz		return (64);
2477151539Ssuz	}
2478151539Ssuz}
2479151539Ssuz
2480186119Sqingli#include <sys/sysctl.h>
2481186119Sqingli
2482186119Sqinglistruct in6_llentry {
2483186119Sqingli	struct llentry		base;
2484186119Sqingli	struct sockaddr_in6	l3_addr6;
2485186119Sqingli};
2486186119Sqingli
2487232054Skmacy/*
2488232054Skmacy * Deletes an address from the address table.
2489232054Skmacy * This function is called by the timer functions
2490232054Skmacy * such as arptimer() and nd6_llinfo_timer(), and
2491232054Skmacy * the caller does the locking.
2492232054Skmacy */
2493232054Skmacystatic void
2494232054Skmacyin6_lltable_free(struct lltable *llt, struct llentry *lle)
2495232054Skmacy{
2496232054Skmacy	LLE_WUNLOCK(lle);
2497232054Skmacy	LLE_LOCK_DESTROY(lle);
2498232054Skmacy	free(lle, M_LLTABLE);
2499232054Skmacy}
2500232054Skmacy
2501186119Sqinglistatic struct llentry *
2502186119Sqingliin6_lltable_new(const struct sockaddr *l3addr, u_int flags)
2503186119Sqingli{
2504186119Sqingli	struct in6_llentry *lle;
2505186119Sqingli
2506238945Sglebius	lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
2507186119Sqingli	if (lle == NULL)		/* NB: caller generates msg */
2508186119Sqingli		return NULL;
2509186119Sqingli
2510186119Sqingli	lle->l3_addr6 = *(const struct sockaddr_in6 *)l3addr;
2511186119Sqingli	lle->base.lle_refcnt = 1;
2512232054Skmacy	lle->base.lle_free = in6_lltable_free;
2513186119Sqingli	LLE_LOCK_INIT(&lle->base);
2514278801Srrs	callout_init(&lle->base.ln_timer_ch, 1);
2515216022Sbz
2516238945Sglebius	return (&lle->base);
2517186119Sqingli}
2518186119Sqingli
2519186119Sqinglistatic void
2520238945Sglebiusin6_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
2521238945Sglebius    const struct sockaddr *mask, u_int flags)
2522192476Sqingli{
2523192476Sqingli	const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix;
2524192476Sqingli	const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask;
2525192476Sqingli	struct llentry *lle, *next;
2526238945Sglebius	int i;
2527192476Sqingli
2528222143Sqingli	/*
2529238945Sglebius	 * (flags & LLE_STATIC) means deleting all entries
2530238945Sglebius	 * including static ND6 entries.
2531222143Sqingli	 */
2532238990Sglebius	IF_AFDATA_WLOCK(llt->llt_ifp);
2533238945Sglebius	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2534192476Sqingli		LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
2535192476Sqingli			if (IN6_ARE_MASKED_ADDR_EQUAL(
2536238990Sglebius			    &satosin6(L3_ADDR(lle))->sin6_addr,
2537238990Sglebius			    &pfx->sin6_addr, &msk->sin6_addr) &&
2538238990Sglebius			    ((flags & LLE_STATIC) ||
2539238990Sglebius			    !(lle->la_flags & LLE_STATIC))) {
2540192476Sqingli				LLE_WLOCK(lle);
2541238990Sglebius				if (callout_stop(&lle->la_timer))
2542206481Sbz					LLE_REMREF(lle);
2543192476Sqingli				llentry_free(lle);
2544192476Sqingli			}
2545192476Sqingli		}
2546192476Sqingli	}
2547238990Sglebius	IF_AFDATA_WUNLOCK(llt->llt_ifp);
2548192476Sqingli}
2549192476Sqingli
2550186119Sqinglistatic int
2551238945Sglebiusin6_lltable_rtcheck(struct ifnet *ifp,
2552238945Sglebius		    u_int flags,
2553201282Sqingli		    const struct sockaddr *l3addr)
2554186119Sqingli{
2555186119Sqingli	struct rtentry *rt;
2556186119Sqingli	char ip6buf[INET6_ADDRSTRLEN];
2557186119Sqingli
2558186119Sqingli	KASSERT(l3addr->sa_family == AF_INET6,
2559186119Sqingli	    ("sin_family %d", l3addr->sa_family));
2560186119Sqingli
2561231852Sbz	/* Our local addresses are always only installed on the default FIB. */
2562186119Sqingli	/* XXX rtalloc1 should take a const param */
2563231852Sbz	rt = in6_rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0,
2564231852Sbz	    RT_DEFAULT_FIB);
2565186119Sqingli	if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
2566186119Sqingli		struct ifaddr *ifa;
2567238945Sglebius		/*
2568238945Sglebius		 * Create an ND6 cache for an IPv6 neighbor
2569186119Sqingli		 * that is not covered by our own prefix.
2570186119Sqingli		 */
2571186119Sqingli		/* XXX ifaof_ifpforaddr should take a const param */
2572186119Sqingli		ifa = ifaof_ifpforaddr(__DECONST(struct sockaddr *, l3addr), ifp);
2573186119Sqingli		if (ifa != NULL) {
2574194760Srwatson			ifa_free(ifa);
2575186119Sqingli			if (rt != NULL)
2576187946Sbz				RTFREE_LOCKED(rt);
2577186119Sqingli			return 0;
2578186119Sqingli		}
2579186119Sqingli		log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n",
2580186119Sqingli		    ip6_sprintf(ip6buf, &((const struct sockaddr_in6 *)l3addr)->sin6_addr));
2581186119Sqingli		if (rt != NULL)
2582187946Sbz			RTFREE_LOCKED(rt);
2583186119Sqingli		return EINVAL;
2584186119Sqingli	}
2585187946Sbz	RTFREE_LOCKED(rt);
2586186119Sqingli	return 0;
2587186119Sqingli}
2588186119Sqingli
2589186119Sqinglistatic struct llentry *
2590186119Sqingliin6_lltable_lookup(struct lltable *llt, u_int flags,
2591186119Sqingli	const struct sockaddr *l3addr)
2592186119Sqingli{
2593186119Sqingli	const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr;
2594186119Sqingli	struct ifnet *ifp = llt->llt_ifp;
2595186119Sqingli	struct llentry *lle;
2596186119Sqingli	struct llentries *lleh;
2597186119Sqingli	u_int hashkey;
2598186119Sqingli
2599186119Sqingli	IF_AFDATA_LOCK_ASSERT(ifp);
2600186119Sqingli	KASSERT(l3addr->sa_family == AF_INET6,
2601186119Sqingli	    ("sin_family %d", l3addr->sa_family));
2602186119Sqingli
2603186119Sqingli	hashkey = sin6->sin6_addr.s6_addr32[3];
2604186119Sqingli	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
2605186119Sqingli	LIST_FOREACH(lle, lleh, lle_next) {
2606186708Sqingli		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)L3_ADDR(lle);
2607186119Sqingli		if (lle->la_flags & LLE_DELETED)
2608186119Sqingli			continue;
2609238945Sglebius		if (bcmp(&sa6->sin6_addr, &sin6->sin6_addr,
2610238945Sglebius		    sizeof(struct in6_addr)) == 0)
2611186119Sqingli			break;
2612186119Sqingli	}
2613186119Sqingli
2614186119Sqingli	if (lle == NULL) {
2615186119Sqingli		if (!(flags & LLE_CREATE))
2616186119Sqingli			return (NULL);
2617260504Sae		IF_AFDATA_WLOCK_ASSERT(ifp);
2618186119Sqingli		/*
2619186119Sqingli		 * A route that covers the given address must have
2620186119Sqingli		 * been installed 1st because we are doing a resolution,
2621186119Sqingli		 * verify this.
2622186119Sqingli		 */
2623186119Sqingli		if (!(flags & LLE_IFADDR) &&
2624201282Sqingli		    in6_lltable_rtcheck(ifp, flags, l3addr) != 0)
2625186119Sqingli			return NULL;
2626186119Sqingli
2627186119Sqingli		lle = in6_lltable_new(l3addr, flags);
2628186119Sqingli		if (lle == NULL) {
2629186119Sqingli			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
2630186119Sqingli			return NULL;
2631186119Sqingli		}
2632186119Sqingli		lle->la_flags = flags & ~LLE_CREATE;
2633186119Sqingli		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
2634186119Sqingli			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
2635186119Sqingli			lle->la_flags |= (LLE_VALID | LLE_STATIC);
2636186119Sqingli		}
2637186119Sqingli
2638186119Sqingli		lle->lle_tbl  = llt;
2639186119Sqingli		lle->lle_head = lleh;
2640238990Sglebius		lle->la_flags |= LLE_LINKED;
2641186119Sqingli		LIST_INSERT_HEAD(lleh, lle, lle_next);
2642186119Sqingli	} else if (flags & LLE_DELETE) {
2643186392Sqingli		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
2644186392Sqingli			LLE_WLOCK(lle);
2645238990Sglebius			lle->la_flags |= LLE_DELETED;
2646286316Sae			EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
2647198418Sqingli#ifdef DIAGNOSTIC
2648249742Soleg			log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
2649238967Sglebius#endif
2650249742Soleg			if ((lle->la_flags &
2651249742Soleg			    (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
2652249742Soleg				llentry_free(lle);
2653249742Soleg			else
2654249742Soleg				LLE_WUNLOCK(lle);
2655186392Sqingli		}
2656186119Sqingli		lle = (void *)-1;
2657186119Sqingli	}
2658186119Sqingli	if (LLE_IS_VALID(lle)) {
2659186119Sqingli		if (flags & LLE_EXCLUSIVE)
2660186119Sqingli			LLE_WLOCK(lle);
2661186119Sqingli		else
2662186119Sqingli			LLE_RLOCK(lle);
2663186119Sqingli	}
2664186119Sqingli	return (lle);
2665186119Sqingli}
2666186119Sqingli
2667186119Sqinglistatic int
2668186119Sqingliin6_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
2669186119Sqingli{
2670186119Sqingli	struct ifnet *ifp = llt->llt_ifp;
2671186119Sqingli	struct llentry *lle;
2672186119Sqingli	/* XXX stack use */
2673186119Sqingli	struct {
2674186119Sqingli		struct rt_msghdr	rtm;
2675186119Sqingli		struct sockaddr_in6	sin6;
2676186119Sqingli		/*
2677186119Sqingli		 * ndp.c assumes that sdl is word aligned
2678186119Sqingli		 */
2679186119Sqingli#ifdef __LP64__
2680186119Sqingli		uint32_t		pad;
2681186119Sqingli#endif
2682186119Sqingli		struct sockaddr_dl	sdl;
2683186119Sqingli	} ndpc;
2684186119Sqingli	int i, error;
2685186119Sqingli
2686196864Sqingli	if (ifp->if_flags & IFF_LOOPBACK)
2687196864Sqingli		return 0;
2688196864Sqingli
2689196535Srwatson	LLTABLE_LOCK_ASSERT();
2690186119Sqingli
2691186119Sqingli	error = 0;
2692186119Sqingli	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
2693186119Sqingli		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
2694186119Sqingli			struct sockaddr_dl *sdl;
2695186119Sqingli
2696186119Sqingli			/* skip deleted or invalid entries */
2697186119Sqingli			if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
2698186119Sqingli				continue;
2699186980Sbz			/* Skip if jailed and not a valid IP of the prison. */
2700188144Sjamie			if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
2701186980Sbz				continue;
2702186119Sqingli			/*
2703186119Sqingli			 * produce a msg made of:
2704186119Sqingli			 *  struct rt_msghdr;
2705186119Sqingli			 *  struct sockaddr_in6 (IPv6)
2706186119Sqingli			 *  struct sockaddr_dl;
2707186119Sqingli			 */
2708186119Sqingli			bzero(&ndpc, sizeof(ndpc));
2709186119Sqingli			ndpc.rtm.rtm_msglen = sizeof(ndpc);
2710187094Sqingli			ndpc.rtm.rtm_version = RTM_VERSION;
2711187094Sqingli			ndpc.rtm.rtm_type = RTM_GET;
2712187094Sqingli			ndpc.rtm.rtm_flags = RTF_UP;
2713187094Sqingli			ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
2714186119Sqingli			ndpc.sin6.sin6_family = AF_INET6;
2715186119Sqingli			ndpc.sin6.sin6_len = sizeof(ndpc.sin6);
2716186119Sqingli			bcopy(L3_ADDR(lle), &ndpc.sin6, L3_ADDR_LEN(lle));
2717243903Shrs			if (V_deembed_scopeid)
2718243903Shrs				sa6_recoverscope(&ndpc.sin6);
2719186119Sqingli
2720186119Sqingli			/* publish */
2721186119Sqingli			if (lle->la_flags & LLE_PUB)
2722186119Sqingli				ndpc.rtm.rtm_flags |= RTF_ANNOUNCE;
2723186119Sqingli
2724186119Sqingli			sdl = &ndpc.sdl;
2725186119Sqingli			sdl->sdl_family = AF_LINK;
2726186119Sqingli			sdl->sdl_len = sizeof(*sdl);
2727186119Sqingli			sdl->sdl_alen = ifp->if_addrlen;
2728186119Sqingli			sdl->sdl_index = ifp->if_index;
2729186119Sqingli			sdl->sdl_type = ifp->if_type;
2730186119Sqingli			bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
2731186119Sqingli			ndpc.rtm.rtm_rmx.rmx_expire =
2732186119Sqingli			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
2733186500Sqingli			ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
2734186119Sqingli			if (lle->la_flags & LLE_STATIC)
2735186119Sqingli				ndpc.rtm.rtm_flags |= RTF_STATIC;
2736186119Sqingli			ndpc.rtm.rtm_index = ifp->if_index;
2737186119Sqingli			error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc));
2738186119Sqingli			if (error)
2739186119Sqingli				break;
2740186119Sqingli		}
2741186119Sqingli	}
2742186119Sqingli	return error;
2743186119Sqingli}
2744186119Sqingli
2745121161Sumevoid *
2746171259Sdelphijin6_domifattach(struct ifnet *ifp)
2747121161Sume{
2748121161Sume	struct in6_ifextra *ext;
2749121161Sume
2750253841Shrs	/* There are not IPv6-capable interfaces. */
2751253841Shrs	switch (ifp->if_type) {
2752253841Shrs	case IFT_PFLOG:
2753253841Shrs	case IFT_PFSYNC:
2754253841Shrs	case IFT_USB:
2755253841Shrs		return (NULL);
2756253841Shrs	}
2757121161Sume	ext = (struct in6_ifextra *)malloc(sizeof(*ext), M_IFADDR, M_WAITOK);
2758121161Sume	bzero(ext, sizeof(*ext));
2759121161Sume
2760253101Sae	ext->in6_ifstat = malloc(sizeof(counter_u64_t) *
2761253101Sae	    sizeof(struct in6_ifstat) / sizeof(uint64_t), M_IFADDR, M_WAITOK);
2762253086Sae	COUNTER_ARRAY_ALLOC(ext->in6_ifstat,
2763253086Sae	    sizeof(struct in6_ifstat) / sizeof(uint64_t), M_WAITOK);
2764121161Sume
2765253101Sae	ext->icmp6_ifstat = malloc(sizeof(counter_u64_t) *
2766253101Sae	    sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_IFADDR,
2767253086Sae	    M_WAITOK);
2768253086Sae	COUNTER_ARRAY_ALLOC(ext->icmp6_ifstat,
2769253086Sae	    sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_WAITOK);
2770121161Sume
2771121161Sume	ext->nd_ifinfo = nd6_ifattach(ifp);
2772121161Sume	ext->scope6_id = scope6_ifattach(ifp);
2773186119Sqingli	ext->lltable = lltable_init(ifp, AF_INET6);
2774186119Sqingli	if (ext->lltable != NULL) {
2775192476Sqingli		ext->lltable->llt_prefix_free = in6_lltable_prefix_free;
2776186119Sqingli		ext->lltable->llt_lookup = in6_lltable_lookup;
2777186119Sqingli		ext->lltable->llt_dump = in6_lltable_dump;
2778186119Sqingli	}
2779191672Sbms
2780191672Sbms	ext->mld_ifinfo = mld_domifattach(ifp);
2781191672Sbms
2782121161Sume	return ext;
2783121161Sume}
2784121161Sume
2785121161Sumevoid
2786171259Sdelphijin6_domifdetach(struct ifnet *ifp, void *aux)
2787121161Sume{
2788121161Sume	struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2789121161Sume
2790191672Sbms	mld_domifdetach(ifp);
2791121161Sume	scope6_ifdetach(ext->scope6_id);
2792121161Sume	nd6_ifdetach(ext->nd_ifinfo);
2793186119Sqingli	lltable_free(ext->lltable);
2794253086Sae	COUNTER_ARRAY_FREE(ext->in6_ifstat,
2795253086Sae	    sizeof(struct in6_ifstat) / sizeof(uint64_t));
2796121161Sume	free(ext->in6_ifstat, M_IFADDR);
2797253086Sae	COUNTER_ARRAY_FREE(ext->icmp6_ifstat,
2798253086Sae	    sizeof(struct icmp6_ifstat) / sizeof(uint64_t));
2799121161Sume	free(ext->icmp6_ifstat, M_IFADDR);
2800121161Sume	free(ext, M_IFADDR);
2801121161Sume}
2802121161Sume
280353541Sshin/*
280495023Ssuz * Convert sockaddr_in6 to sockaddr_in.  Original sockaddr_in6 must be
280553541Sshin * v4 mapped addr or v4 compat addr
280653541Sshin */
280753541Sshinvoid
280853541Sshinin6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
280953541Sshin{
2810171259Sdelphij
281153541Sshin	bzero(sin, sizeof(*sin));
281253541Sshin	sin->sin_len = sizeof(struct sockaddr_in);
281353541Sshin	sin->sin_family = AF_INET;
281453541Sshin	sin->sin_port = sin6->sin6_port;
2815120891Sume	sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
281653541Sshin}
281753541Sshin
281853541Sshin/* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */
281953541Sshinvoid
282053541Sshinin6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
282153541Sshin{
282253541Sshin	bzero(sin6, sizeof(*sin6));
282353541Sshin	sin6->sin6_len = sizeof(struct sockaddr_in6);
282453541Sshin	sin6->sin6_family = AF_INET6;
282553541Sshin	sin6->sin6_port = sin->sin_port;
282653541Sshin	sin6->sin6_addr.s6_addr32[0] = 0;
282753541Sshin	sin6->sin6_addr.s6_addr32[1] = 0;
282853541Sshin	sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP;
282953541Sshin	sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
283053541Sshin}
283153541Sshin
283253541Sshin/* Convert sockaddr_in6 into sockaddr_in. */
283353541Sshinvoid
283453541Sshinin6_sin6_2_sin_in_sock(struct sockaddr *nam)
283553541Sshin{
283653541Sshin	struct sockaddr_in *sin_p;
283753541Sshin	struct sockaddr_in6 sin6;
283853541Sshin
283953541Sshin	/*
284053541Sshin	 * Save original sockaddr_in6 addr and convert it
284153541Sshin	 * to sockaddr_in.
284253541Sshin	 */
284353541Sshin	sin6 = *(struct sockaddr_in6 *)nam;
284453541Sshin	sin_p = (struct sockaddr_in *)nam;
284553541Sshin	in6_sin6_2_sin(sin_p, &sin6);
284653541Sshin}
284753541Sshin
284853541Sshin/* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */
284953541Sshinvoid
285053541Sshinin6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam)
285153541Sshin{
285253541Sshin	struct sockaddr_in *sin_p;
285353541Sshin	struct sockaddr_in6 *sin6_p;
285453541Sshin
2855238945Sglebius	sin6_p = malloc(sizeof *sin6_p, M_SONAME, M_WAITOK);
285653541Sshin	sin_p = (struct sockaddr_in *)*nam;
285753541Sshin	in6_sin_2_v4mapsin6(sin_p, sin6_p);
2858184205Sdes	free(*nam, M_SONAME);
285953541Sshin	*nam = (struct sockaddr *)sin6_p;
286053541Sshin}
2861