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