xref: /titanic_50/usr/src/uts/common/inet/ip/ip_ndp.c (revision b86efd96f8acd85ddaa930a2f0c1d664237e4aaf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/stream.h>
30 #include <sys/stropts.h>
31 #include <sys/strsun.h>
32 #include <sys/sysmacros.h>
33 #include <sys/errno.h>
34 #include <sys/dlpi.h>
35 #include <sys/socket.h>
36 #include <sys/ddi.h>
37 #include <sys/sunddi.h>
38 #include <sys/cmn_err.h>
39 #include <sys/debug.h>
40 #include <sys/vtrace.h>
41 #include <sys/kmem.h>
42 #include <sys/zone.h>
43 #include <sys/ethernet.h>
44 #include <sys/sdt.h>
45 
46 #include <net/if.h>
47 #include <net/if_types.h>
48 #include <net/if_dl.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/ip6.h>
52 #include <netinet/icmp6.h>
53 
54 #include <inet/common.h>
55 #include <inet/mi.h>
56 #include <inet/mib2.h>
57 #include <inet/nd.h>
58 #include <inet/ip.h>
59 #include <inet/ip_impl.h>
60 #include <inet/ip_if.h>
61 #include <inet/ip_ire.h>
62 #include <inet/ip_rts.h>
63 #include <inet/ip6.h>
64 #include <inet/ip_ndp.h>
65 #include <inet/ipsec_impl.h>
66 #include <inet/ipsec_info.h>
67 #include <inet/sctp_ip.h>
68 
69 /*
70  * Function names with nce_ prefix are static while function
71  * names with ndp_ prefix are used by rest of the IP.
72  *
73  * Lock ordering:
74  *
75  *	ndp_g_lock -> ill_lock -> nce_lock
76  *
77  * The ndp_g_lock protects the NCE hash (nce_hash_tbl, NCE_HASH_PTR) and
78  * nce_next.  Nce_lock protects the contents of the NCE (particularly
79  * nce_refcnt).
80  */
81 
82 static	boolean_t nce_cmp_ll_addr(const nce_t *nce, const uchar_t *new_ll_addr,
83     uint32_t ll_addr_len);
84 static	void	nce_fastpath(nce_t *nce);
85 static	void	nce_ire_delete(nce_t *nce);
86 static	void	nce_ire_delete1(ire_t *ire, char *nce_arg);
87 static	void 	nce_set_ll(nce_t *nce, uchar_t *ll_addr);
88 static	nce_t	*nce_lookup_addr(ill_t *, const in6_addr_t *, nce_t *);
89 static	nce_t	*nce_lookup_mapping(ill_t *ill, const in6_addr_t *addr);
90 static	void	nce_make_mapping(nce_t *nce, uchar_t *addrpos,
91     uchar_t *addr);
92 static	int	nce_set_multicast(ill_t *ill, const in6_addr_t *addr);
93 static	void	nce_queue_mp(nce_t *nce, mblk_t *mp);
94 static	void	nce_report1(nce_t *nce, uchar_t *mp_arg);
95 static	mblk_t	*nce_udreq_alloc(ill_t *ill);
96 static	void	nce_update(nce_t *nce, uint16_t new_state,
97     uchar_t *new_ll_addr);
98 static	uint32_t	nce_solicit(nce_t *nce, mblk_t *mp);
99 static	boolean_t	nce_xmit(ill_t *ill, uint32_t operation,
100     ill_t *hwaddr_ill, boolean_t use_lla_addr, const in6_addr_t *sender,
101     const in6_addr_t *target, int flag);
102 extern void	th_trace_rrecord(th_trace_t *);
103 static	int	ndp_lookup_then_add_v6(ill_t *, uchar_t *,
104     const in6_addr_t *, const in6_addr_t *, const in6_addr_t *,
105     uint32_t, uint16_t, uint16_t, nce_t **, mblk_t *, mblk_t *);
106 static	int	ndp_lookup_then_add_v4(ill_t *, uchar_t *,
107     const in_addr_t *, const in_addr_t *, const in_addr_t *,
108     uint32_t, uint16_t, uint16_t, nce_t **, mblk_t *, mblk_t *);
109 static	int	ndp_add_v6(ill_t *, uchar_t *, const in6_addr_t *,
110     const in6_addr_t *, const in6_addr_t *, uint32_t, uint16_t, uint16_t,
111     nce_t **);
112 static	int	ndp_add_v4(ill_t *, uchar_t *, const in_addr_t *,
113     const in_addr_t *, const in_addr_t *, uint32_t, uint16_t, uint16_t,
114     nce_t **, mblk_t *, mblk_t *);
115 
116 
117 #ifdef NCE_DEBUG
118 void	nce_trace_inactive(nce_t *);
119 #endif
120 
121 ndp_g_t ndp4, ndp6;
122 
123 #define	NCE_HASH_PTR_V4(addr) \
124 	(&(ndp4.nce_hash_tbl[IRE_ADDR_HASH(addr, NCE_TABLE_SIZE)]))
125 
126 #define	NCE_HASH_PTR_V6(addr) \
127 	(&(ndp6.nce_hash_tbl[NCE_ADDR_HASH_V6(addr, NCE_TABLE_SIZE)]))
128 
129 /*
130  * Compute default flags to use for an advertisement of this nce's address.
131  */
132 static int
133 nce_advert_flags(const nce_t *nce)
134 {
135 	int flag = 0;
136 
137 	if (nce->nce_flags & NCE_F_ISROUTER)
138 		flag |= NDP_ISROUTER;
139 	if (!(nce->nce_flags & NCE_F_PROXY))
140 		flag |= NDP_ORIDE;
141 	return (flag);
142 }
143 
144 int
145 ndp_add(ill_t *ill, uchar_t *hw_addr, const void *addr,
146     const void *mask, const void *extract_mask,
147     uint32_t hw_extract_start, uint16_t flags, uint16_t state,
148     nce_t **newnce, mblk_t *fp_mp, mblk_t *res_mp)
149 {
150 	int status;
151 
152 	if (ill->ill_isv6)
153 		status = ndp_add_v6(ill, hw_addr, (in6_addr_t *)addr,
154 		    (in6_addr_t *)mask, (in6_addr_t *)extract_mask,
155 		    hw_extract_start, flags, state, newnce);
156 	else
157 		status = ndp_add_v4(ill, hw_addr, (in_addr_t *)addr,
158 		    (in_addr_t *)mask, (in_addr_t *)extract_mask,
159 		    hw_extract_start, flags, state, newnce, fp_mp, res_mp);
160 	return (status);
161 }
162 
163 /* Non-tunable probe interval, based on link capabilities */
164 #define	ILL_PROBE_INTERVAL(ill)	((ill)->ill_note_link ? 150 : 1500)
165 
166 /*
167  * NDP Cache Entry creation routine.
168  * Mapped entries will never do NUD .
169  * This routine must always be called with ndp6.ndp_g_lock held.
170  * Prior to return, nce_refcnt is incremented.
171  */
172 static int
173 ndp_add_v6(ill_t *ill, uchar_t *hw_addr, const in6_addr_t *addr,
174     const in6_addr_t *mask, const in6_addr_t *extract_mask,
175     uint32_t hw_extract_start, uint16_t flags, uint16_t state,
176     nce_t **newnce)
177 {
178 	static	nce_t		nce_nil;
179 	nce_t		*nce;
180 	mblk_t		*mp;
181 	mblk_t		*template;
182 	nce_t		**ncep;
183 	int		err;
184 	boolean_t	dropped = B_FALSE;
185 
186 	ASSERT(MUTEX_HELD(&ndp6.ndp_g_lock));
187 	ASSERT(ill != NULL && ill->ill_isv6);
188 	if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
189 		ip0dbg(("ndp_add: no addr\n"));
190 		return (EINVAL);
191 	}
192 	if ((flags & ~NCE_EXTERNAL_FLAGS_MASK)) {
193 		ip0dbg(("ndp_add: flags = %x\n", (int)flags));
194 		return (EINVAL);
195 	}
196 	if (IN6_IS_ADDR_UNSPECIFIED(extract_mask) &&
197 	    (flags & NCE_F_MAPPING)) {
198 		ip0dbg(("ndp_add: extract mask zero for mapping"));
199 		return (EINVAL);
200 	}
201 	/*
202 	 * Allocate the mblk to hold the nce.
203 	 *
204 	 * XXX This can come out of a separate cache - nce_cache.
205 	 * We don't need the mp anymore as there are no more
206 	 * "qwriter"s
207 	 */
208 	mp = allocb(sizeof (nce_t), BPRI_MED);
209 	if (mp == NULL)
210 		return (ENOMEM);
211 
212 	nce = (nce_t *)mp->b_rptr;
213 	mp->b_wptr = (uchar_t *)&nce[1];
214 	*nce = nce_nil;
215 
216 	/*
217 	 * This one holds link layer address
218 	 */
219 	if (ill->ill_net_type == IRE_IF_RESOLVER) {
220 		template = nce_udreq_alloc(ill);
221 	} else {
222 		if (ill->ill_resolver_mp == NULL) {
223 			freeb(mp);
224 			return (EINVAL);
225 		}
226 		ASSERT((ill->ill_net_type == IRE_IF_NORESOLVER));
227 		template = copyb(ill->ill_resolver_mp);
228 	}
229 	if (template == NULL) {
230 		freeb(mp);
231 		return (ENOMEM);
232 	}
233 	nce->nce_ill = ill;
234 	nce->nce_ipversion = IPV6_VERSION;
235 	nce->nce_flags = flags;
236 	nce->nce_state = state;
237 	nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT;
238 	nce->nce_rcnt = ill->ill_xmit_count;
239 	nce->nce_addr = *addr;
240 	nce->nce_mask = *mask;
241 	nce->nce_extract_mask = *extract_mask;
242 	nce->nce_ll_extract_start = hw_extract_start;
243 	nce->nce_fp_mp = NULL;
244 	nce->nce_res_mp = template;
245 	if (state == ND_REACHABLE)
246 		nce->nce_last = TICK_TO_MSEC(lbolt64);
247 	else
248 		nce->nce_last = 0;
249 	nce->nce_qd_mp = NULL;
250 	nce->nce_mp = mp;
251 	if (hw_addr != NULL)
252 		nce_set_ll(nce, hw_addr);
253 	/* This one is for nce getting created */
254 	nce->nce_refcnt = 1;
255 	mutex_init(&nce->nce_lock, NULL, MUTEX_DEFAULT, NULL);
256 	if (nce->nce_flags & NCE_F_MAPPING) {
257 		ASSERT(IN6_IS_ADDR_MULTICAST(addr));
258 		ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&nce->nce_mask));
259 		ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&nce->nce_extract_mask));
260 		ncep = &ndp6.nce_mask_entries;
261 	} else {
262 		ncep = ((nce_t **)NCE_HASH_PTR_V6(*addr));
263 	}
264 
265 #ifdef NCE_DEBUG
266 	bzero(nce->nce_trace, sizeof (th_trace_t *) * IP_TR_HASH_MAX);
267 #endif
268 	/*
269 	 * Atomically ensure that the ill is not CONDEMNED, before
270 	 * adding the NCE.
271 	 */
272 	mutex_enter(&ill->ill_lock);
273 	if (ill->ill_state_flags & ILL_CONDEMNED) {
274 		mutex_exit(&ill->ill_lock);
275 		freeb(mp);
276 		freeb(template);
277 		return (EINVAL);
278 	}
279 	if ((nce->nce_next = *ncep) != NULL)
280 		nce->nce_next->nce_ptpn = &nce->nce_next;
281 	*ncep = nce;
282 	nce->nce_ptpn = ncep;
283 	*newnce = nce;
284 	/* This one is for nce being used by an active thread */
285 	NCE_REFHOLD(*newnce);
286 
287 	/* Bump up the number of nce's referencing this ill */
288 	ill->ill_nce_cnt++;
289 	mutex_exit(&ill->ill_lock);
290 
291 	err = 0;
292 	if ((flags & NCE_F_PERMANENT) && state == ND_PROBE) {
293 		mutex_enter(&nce->nce_lock);
294 		mutex_exit(&ndp6.ndp_g_lock);
295 		nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT;
296 		mutex_exit(&nce->nce_lock);
297 		dropped = nce_xmit(ill, ND_NEIGHBOR_SOLICIT, NULL, B_FALSE,
298 		    &ipv6_all_zeros, addr, NDP_PROBE);
299 		if (dropped) {
300 			mutex_enter(&nce->nce_lock);
301 			nce->nce_pcnt++;
302 			mutex_exit(&nce->nce_lock);
303 		}
304 		NDP_RESTART_TIMER(nce, ILL_PROBE_INTERVAL(ill));
305 		mutex_enter(&ndp6.ndp_g_lock);
306 		err = EINPROGRESS;
307 	} else if (flags & NCE_F_UNSOL_ADV) {
308 		/*
309 		 * We account for the transmit below by assigning one
310 		 * less than the ndd variable. Subsequent decrements
311 		 * are done in ndp_timer.
312 		 */
313 		mutex_enter(&nce->nce_lock);
314 		mutex_exit(&ndp6.ndp_g_lock);
315 		nce->nce_unsolicit_count = ip_ndp_unsolicit_count - 1;
316 		mutex_exit(&nce->nce_lock);
317 		dropped = nce_xmit(ill,
318 		    ND_NEIGHBOR_ADVERT,
319 		    ill,	/* ill to be used for extracting ill_nd_lla */
320 		    B_TRUE,	/* use ill_nd_lla */
321 		    addr,	/* Source and target of the advertisement pkt */
322 		    &ipv6_all_hosts_mcast, /* Destination of the packet */
323 		    nce_advert_flags(nce));
324 		mutex_enter(&nce->nce_lock);
325 		if (dropped)
326 			nce->nce_unsolicit_count++;
327 		if (nce->nce_unsolicit_count != 0) {
328 			nce->nce_timeout_id = timeout(ndp_timer, nce,
329 			    MSEC_TO_TICK(ip_ndp_unsolicit_interval));
330 		}
331 		mutex_exit(&nce->nce_lock);
332 		mutex_enter(&ndp6.ndp_g_lock);
333 	}
334 	/*
335 	 * If the hw_addr is NULL, typically for ND_INCOMPLETE nces, then
336 	 * we call nce_fastpath as soon as the nce is resolved in ndp_process.
337 	 * We call nce_fastpath from nce_update if the link layer address of
338 	 * the peer changes from nce_update
339 	 */
340 	if (hw_addr != NULL || ill->ill_net_type == IRE_IF_NORESOLVER)
341 		nce_fastpath(nce);
342 	return (err);
343 }
344 
345 int
346 ndp_lookup_then_add(ill_t *ill, uchar_t *hw_addr, const void *addr,
347     const void *mask, const void *extract_mask,
348     uint32_t hw_extract_start, uint16_t flags, uint16_t state,
349     nce_t **newnce, mblk_t *fp_mp, mblk_t *res_mp)
350 {
351 	int status;
352 
353 	if (ill->ill_isv6) {
354 		status = ndp_lookup_then_add_v6(ill, hw_addr,
355 		    (in6_addr_t *)addr, (in6_addr_t *)mask,
356 		    (in6_addr_t *)extract_mask, hw_extract_start, flags,
357 		    state, newnce, fp_mp, res_mp);
358 	} else  {
359 		status = ndp_lookup_then_add_v4(ill, hw_addr,
360 		    (in_addr_t *)addr, (in_addr_t *)mask,
361 		    (in_addr_t *)extract_mask, hw_extract_start, flags,
362 		    state, newnce, fp_mp, res_mp);
363 	}
364 
365 	return (status);
366 }
367 
368 static int
369 ndp_lookup_then_add_v6(ill_t *ill, uchar_t *hw_addr, const in6_addr_t *addr,
370     const in6_addr_t *mask, const in6_addr_t *extract_mask,
371     uint32_t hw_extract_start, uint16_t flags, uint16_t state,
372     nce_t **newnce, mblk_t *fp_mp, mblk_t *res_mp)
373 {
374 	int	err = 0;
375 	nce_t	*nce;
376 
377 	ASSERT(ill != NULL && ill->ill_isv6);
378 	mutex_enter(&ndp6.ndp_g_lock);
379 	nce = *((nce_t **)NCE_HASH_PTR_V6(*addr)); /* head of v6 hash table */
380 	nce = nce_lookup_addr(ill, addr, nce);
381 	if (nce == NULL) {
382 		err = ndp_add(ill,
383 		    hw_addr,
384 		    addr,
385 		    mask,
386 		    extract_mask,
387 		    hw_extract_start,
388 		    flags,
389 		    state,
390 		    newnce,
391 		    fp_mp,
392 		    res_mp);
393 	} else {
394 		*newnce = nce;
395 		err = EEXIST;
396 	}
397 	mutex_exit(&ndp6.ndp_g_lock);
398 	return (err);
399 }
400 
401 /*
402  * Remove all the CONDEMNED nces from the appropriate hash table.
403  * We create a private list of NCEs, these may have ires pointing
404  * to them, so the list will be passed through to clean up dependent
405  * ires and only then we can do NCE_REFRELE which can make NCE inactive.
406  */
407 static void
408 nce_remove(ndp_g_t *ndp, nce_t *nce, nce_t **free_nce_list)
409 {
410 	nce_t *nce1;
411 	nce_t **ptpn;
412 
413 	ASSERT(MUTEX_HELD(&ndp->ndp_g_lock));
414 	ASSERT(ndp->ndp_g_walker == 0);
415 	for (; nce; nce = nce1) {
416 		nce1 = nce->nce_next;
417 		mutex_enter(&nce->nce_lock);
418 		if (nce->nce_flags & NCE_F_CONDEMNED) {
419 			ptpn = nce->nce_ptpn;
420 			nce1 = nce->nce_next;
421 			if (nce1 != NULL)
422 				nce1->nce_ptpn = ptpn;
423 			*ptpn = nce1;
424 			nce->nce_ptpn = NULL;
425 			nce->nce_next = NULL;
426 			nce->nce_next = *free_nce_list;
427 			*free_nce_list = nce;
428 		}
429 		mutex_exit(&nce->nce_lock);
430 	}
431 }
432 
433 /*
434  * 1. Mark the nce CONDEMNED. This ensures that no new nce_lookup()
435  *    will return this NCE. Also no new IREs will be created that
436  *    point to this NCE (See ire_add_v6).  Also no new timeouts will
437  *    be started (See NDP_RESTART_TIMER).
438  * 2. Cancel any currently running timeouts.
439  * 3. If there is an ndp walker, return. The walker will do the cleanup.
440  *    This ensures that walkers see a consistent list of NCEs while walking.
441  * 4. Otherwise remove the NCE from the list of NCEs
442  * 5. Delete all IREs pointing to this NCE.
443  */
444 void
445 ndp_delete(nce_t *nce)
446 {
447 	nce_t	**ptpn;
448 	nce_t	*nce1;
449 	int	ipversion = nce->nce_ipversion;
450 	ndp_g_t *ndp = (ipversion == IPV4_VERSION ? &ndp4 : &ndp6);
451 
452 	/* Serialize deletes */
453 	mutex_enter(&nce->nce_lock);
454 	if (nce->nce_flags & NCE_F_CONDEMNED) {
455 		/* Some other thread is doing the delete */
456 		mutex_exit(&nce->nce_lock);
457 		return;
458 	}
459 	/*
460 	 * Caller has a refhold. Also 1 ref for being in the list. Thus
461 	 * refcnt has to be >= 2
462 	 */
463 	ASSERT(nce->nce_refcnt >= 2);
464 	nce->nce_flags |= NCE_F_CONDEMNED;
465 	mutex_exit(&nce->nce_lock);
466 
467 	nce_fastpath_list_delete(nce);
468 
469 	/*
470 	 * Cancel any running timer. Timeout can't be restarted
471 	 * since CONDEMNED is set. Can't hold nce_lock across untimeout.
472 	 * Passing invalid timeout id is fine.
473 	 */
474 	if (nce->nce_timeout_id != 0) {
475 		(void) untimeout(nce->nce_timeout_id);
476 		nce->nce_timeout_id = 0;
477 	}
478 
479 	mutex_enter(&ndp->ndp_g_lock);
480 	if (nce->nce_ptpn == NULL) {
481 		/*
482 		 * The last ndp walker has already removed this nce from
483 		 * the list after we marked the nce CONDEMNED and before
484 		 * we grabbed the global lock.
485 		 */
486 		mutex_exit(&ndp->ndp_g_lock);
487 		return;
488 	}
489 	if (ndp->ndp_g_walker > 0) {
490 		/*
491 		 * Can't unlink. The walker will clean up
492 		 */
493 		ndp->ndp_g_walker_cleanup = B_TRUE;
494 		mutex_exit(&ndp->ndp_g_lock);
495 		return;
496 	}
497 
498 	/*
499 	 * Now remove the nce from the list. NDP_RESTART_TIMER won't restart
500 	 * the timer since it is marked CONDEMNED.
501 	 */
502 	ptpn = nce->nce_ptpn;
503 	nce1 = nce->nce_next;
504 	if (nce1 != NULL)
505 		nce1->nce_ptpn = ptpn;
506 	*ptpn = nce1;
507 	nce->nce_ptpn = NULL;
508 	nce->nce_next = NULL;
509 	mutex_exit(&ndp->ndp_g_lock);
510 
511 	nce_ire_delete(nce);
512 }
513 
514 void
515 ndp_inactive(nce_t *nce)
516 {
517 	mblk_t		**mpp;
518 	ill_t		*ill;
519 
520 	ASSERT(nce->nce_refcnt == 0);
521 	ASSERT(MUTEX_HELD(&nce->nce_lock));
522 	ASSERT(nce->nce_fastpath == NULL);
523 
524 	/* Free all nce allocated messages */
525 	mpp = &nce->nce_first_mp_to_free;
526 	do {
527 		while (*mpp != NULL) {
528 			mblk_t  *mp;
529 
530 			mp = *mpp;
531 			*mpp = mp->b_next;
532 
533 			inet_freemsg(mp);
534 		}
535 	} while (mpp++ != &nce->nce_last_mp_to_free);
536 
537 #ifdef NCE_DEBUG
538 	nce_trace_inactive(nce);
539 #endif
540 
541 	ill = nce->nce_ill;
542 	mutex_enter(&ill->ill_lock);
543 	ill->ill_nce_cnt--;
544 	/*
545 	 * If the number of nce's associated with this ill have dropped
546 	 * to zero, check whether we need to restart any operation that
547 	 * is waiting for this to happen.
548 	 */
549 	if (ill->ill_nce_cnt == 0) {
550 		/* ipif_ill_refrele_tail drops the ill_lock */
551 		ipif_ill_refrele_tail(ill);
552 	} else {
553 		mutex_exit(&ill->ill_lock);
554 	}
555 	mutex_destroy(&nce->nce_lock);
556 	if (nce->nce_mp != NULL)
557 		inet_freemsg(nce->nce_mp);
558 }
559 
560 /*
561  * ndp_walk routine.  Delete the nce if it is associated with the ill
562  * that is going away.  Always called as a writer.
563  */
564 void
565 ndp_delete_per_ill(nce_t *nce, uchar_t *arg)
566 {
567 	if ((nce != NULL) && nce->nce_ill == (ill_t *)arg) {
568 		ndp_delete(nce);
569 	}
570 }
571 
572 /*
573  * Walk a list of to be inactive NCEs and blow away all the ires.
574  */
575 static void
576 nce_ire_delete_list(nce_t *nce)
577 {
578 	nce_t *nce_next;
579 
580 	ASSERT(nce != NULL);
581 	while (nce != NULL) {
582 		nce_next = nce->nce_next;
583 		nce->nce_next = NULL;
584 
585 		/*
586 		 * It is possible for the last ndp walker (this thread)
587 		 * to come here after ndp_delete has marked the nce CONDEMNED
588 		 * and before it has removed the nce from the fastpath list
589 		 * or called untimeout. So we need to do it here. It is safe
590 		 * for both ndp_delete and this thread to do it twice or
591 		 * even simultaneously since each of the threads has a
592 		 * reference on the nce.
593 		 */
594 		nce_fastpath_list_delete(nce);
595 		/*
596 		 * Cancel any running timer. Timeout can't be restarted
597 		 * since CONDEMNED is set. Can't hold nce_lock across untimeout.
598 		 * Passing invalid timeout id is fine.
599 		 */
600 		if (nce->nce_timeout_id != 0) {
601 			(void) untimeout(nce->nce_timeout_id);
602 			nce->nce_timeout_id = 0;
603 		}
604 		/*
605 		 * We might hit this func thus in the v4 case:
606 		 * ipif_down->ipif_ndp_down->ndp_walk
607 		 */
608 
609 		if (nce->nce_ipversion == IPV4_VERSION) {
610 			ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE,
611 			    IRE_CACHE, nce_ire_delete1,
612 			    (char *)nce, nce->nce_ill);
613 		} else {
614 			ASSERT(nce->nce_ipversion == IPV6_VERSION);
615 			ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE,
616 			    IRE_CACHE, nce_ire_delete1,
617 			    (char *)nce, nce->nce_ill);
618 		}
619 		NCE_REFRELE_NOTR(nce);
620 		nce = nce_next;
621 	}
622 }
623 
624 /*
625  * Delete an ire when the nce goes away.
626  */
627 /* ARGSUSED */
628 static void
629 nce_ire_delete(nce_t *nce)
630 {
631 	if (nce->nce_ipversion == IPV6_VERSION) {
632 		ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_CACHE,
633 		    nce_ire_delete1, (char *)nce, nce->nce_ill);
634 		NCE_REFRELE_NOTR(nce);
635 	} else {
636 		ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE, IRE_CACHE,
637 		    nce_ire_delete1, (char *)nce, nce->nce_ill);
638 		NCE_REFRELE_NOTR(nce);
639 	}
640 }
641 
642 /*
643  * ire_walk routine used to delete every IRE that shares this nce
644  */
645 static void
646 nce_ire_delete1(ire_t *ire, char *nce_arg)
647 {
648 	nce_t	*nce = (nce_t *)nce_arg;
649 
650 	ASSERT(ire->ire_type == IRE_CACHE);
651 
652 	if (ire->ire_nce == nce) {
653 		ASSERT(ire->ire_ipversion == nce->nce_ipversion);
654 		ire_delete(ire);
655 	}
656 }
657 
658 /*
659  * Restart DAD on given NCE.  Returns B_TRUE if DAD has been restarted.
660  */
661 boolean_t
662 ndp_restart_dad(nce_t *nce)
663 {
664 	boolean_t started;
665 	boolean_t dropped;
666 
667 	if (nce == NULL)
668 		return (B_FALSE);
669 	mutex_enter(&nce->nce_lock);
670 	if (nce->nce_state == ND_PROBE) {
671 		mutex_exit(&nce->nce_lock);
672 		started = B_TRUE;
673 	} else if (nce->nce_state == ND_REACHABLE) {
674 		nce->nce_state = ND_PROBE;
675 		nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT - 1;
676 		mutex_exit(&nce->nce_lock);
677 		dropped = nce_xmit(nce->nce_ill, ND_NEIGHBOR_SOLICIT, NULL,
678 		    B_FALSE, &ipv6_all_zeros, &nce->nce_addr, NDP_PROBE);
679 		if (dropped) {
680 			mutex_enter(&nce->nce_lock);
681 			nce->nce_pcnt++;
682 			mutex_exit(&nce->nce_lock);
683 		}
684 		NDP_RESTART_TIMER(nce, ILL_PROBE_INTERVAL(nce->nce_ill));
685 		started = B_TRUE;
686 	} else {
687 		mutex_exit(&nce->nce_lock);
688 		started = B_FALSE;
689 	}
690 	return (started);
691 }
692 
693 /*
694  * IPv6 Cache entry lookup.  Try to find an nce matching the parameters passed.
695  * If one is found, the refcnt on the nce will be incremented.
696  */
697 nce_t *
698 ndp_lookup_v6(ill_t *ill, const in6_addr_t *addr, boolean_t caller_holds_lock)
699 {
700 	nce_t	*nce;
701 
702 	ASSERT(ill != NULL && ill->ill_isv6);
703 	if (!caller_holds_lock) {
704 		mutex_enter(&ndp6.ndp_g_lock);
705 	}
706 	nce = *((nce_t **)NCE_HASH_PTR_V6(*addr)); /* head of v6 hash table */
707 	nce = nce_lookup_addr(ill, addr, nce);
708 	if (nce == NULL)
709 		nce = nce_lookup_mapping(ill, addr);
710 	if (!caller_holds_lock)
711 		mutex_exit(&ndp6.ndp_g_lock);
712 	return (nce);
713 }
714 /*
715  * IPv4 Cache entry lookup.  Try to find an nce matching the parameters passed.
716  * If one is found, the refcnt on the nce will be incremented.
717  * Since multicast mappings are handled in arp, there are no nce_mcast_entries
718  * so we skip the nce_lookup_mapping call.
719  * XXX TODO: if the nce is found to be ND_STALE, ndp_delete it and return NULL
720  */
721 nce_t *
722 ndp_lookup_v4(ill_t *ill, const in_addr_t *addr, boolean_t caller_holds_lock)
723 {
724 	nce_t	*nce;
725 	in6_addr_t addr6;
726 
727 	if (!caller_holds_lock) {
728 		mutex_enter(&ndp4.ndp_g_lock);
729 	}
730 	nce = *((nce_t **)NCE_HASH_PTR_V4(*addr)); /* head of v6 hash table */
731 	IN6_IPADDR_TO_V4MAPPED(*addr, &addr6);
732 	nce = nce_lookup_addr(ill, &addr6, nce);
733 	if (!caller_holds_lock)
734 		mutex_exit(&ndp4.ndp_g_lock);
735 	return (nce);
736 }
737 
738 /*
739  * Cache entry lookup.  Try to find an nce matching the parameters passed.
740  * Look only for exact entries (no mappings).  If an nce is found, increment
741  * the hold count on that nce. The caller passes in the start of the
742  * appropriate hash table, and must be holding the appropriate global
743  * lock (ndp_g_lock).
744  */
745 static nce_t *
746 nce_lookup_addr(ill_t *ill, const in6_addr_t *addr, nce_t *nce)
747 {
748 	ndp_g_t *ndp = (ill->ill_isv6 ? &ndp6 : &ndp4);
749 
750 	ASSERT(ill != NULL);
751 	ASSERT(MUTEX_HELD(&ndp->ndp_g_lock));
752 	if (IN6_IS_ADDR_UNSPECIFIED(addr))
753 		return (NULL);
754 	for (; nce != NULL; nce = nce->nce_next) {
755 		if (nce->nce_ill == ill) {
756 			if (IN6_ARE_ADDR_EQUAL(&nce->nce_addr, addr) &&
757 			    IN6_ARE_ADDR_EQUAL(&nce->nce_mask,
758 			    &ipv6_all_ones)) {
759 				mutex_enter(&nce->nce_lock);
760 				if (!(nce->nce_flags & NCE_F_CONDEMNED)) {
761 					NCE_REFHOLD_LOCKED(nce);
762 					mutex_exit(&nce->nce_lock);
763 					break;
764 				}
765 				mutex_exit(&nce->nce_lock);
766 			}
767 		}
768 	}
769 	return (nce);
770 }
771 
772 /*
773  * Cache entry lookup.  Try to find an nce matching the parameters passed.
774  * Look only for mappings.
775  */
776 static nce_t *
777 nce_lookup_mapping(ill_t *ill, const in6_addr_t *addr)
778 {
779 	nce_t	*nce;
780 
781 	ASSERT(ill != NULL && ill->ill_isv6);
782 	ASSERT(MUTEX_HELD(&ndp6.ndp_g_lock));
783 	if (!IN6_IS_ADDR_MULTICAST(addr))
784 		return (NULL);
785 	nce = ndp6.nce_mask_entries;
786 	for (; nce != NULL; nce = nce->nce_next)
787 		if (nce->nce_ill == ill &&
788 		    (V6_MASK_EQ(*addr, nce->nce_mask, nce->nce_addr))) {
789 			mutex_enter(&nce->nce_lock);
790 			if (!(nce->nce_flags & NCE_F_CONDEMNED)) {
791 				NCE_REFHOLD_LOCKED(nce);
792 				mutex_exit(&nce->nce_lock);
793 				break;
794 			}
795 			mutex_exit(&nce->nce_lock);
796 		}
797 	return (nce);
798 }
799 
800 /*
801  * Process passed in parameters either from an incoming packet or via
802  * user ioctl.
803  */
804 void
805 ndp_process(nce_t *nce, uchar_t *hw_addr, uint32_t flag, boolean_t is_adv)
806 {
807 	ill_t	*ill = nce->nce_ill;
808 	uint32_t hw_addr_len = ill->ill_nd_lla_len;
809 	mblk_t	*mp;
810 	boolean_t ll_updated = B_FALSE;
811 	boolean_t ll_changed;
812 
813 	ASSERT(nce->nce_ipversion == IPV6_VERSION);
814 	/*
815 	 * No updates of link layer address or the neighbor state is
816 	 * allowed, when the cache is in NONUD state.  This still
817 	 * allows for responding to reachability solicitation.
818 	 */
819 	mutex_enter(&nce->nce_lock);
820 	if (nce->nce_state == ND_INCOMPLETE) {
821 		if (hw_addr == NULL) {
822 			mutex_exit(&nce->nce_lock);
823 			return;
824 		}
825 		nce_set_ll(nce, hw_addr);
826 		/*
827 		 * Update nce state and send the queued packets
828 		 * back to ip this time ire will be added.
829 		 */
830 		if (flag & ND_NA_FLAG_SOLICITED) {
831 			nce_update(nce, ND_REACHABLE, NULL);
832 		} else {
833 			nce_update(nce, ND_STALE, NULL);
834 		}
835 		mutex_exit(&nce->nce_lock);
836 		nce_fastpath(nce);
837 		mutex_enter(&nce->nce_lock);
838 		mp = nce->nce_qd_mp;
839 		nce->nce_qd_mp = NULL;
840 		mutex_exit(&nce->nce_lock);
841 		while (mp != NULL) {
842 			mblk_t *nxt_mp, *data_mp;
843 
844 			nxt_mp = mp->b_next;
845 			mp->b_next = NULL;
846 
847 			if (mp->b_datap->db_type == M_CTL)
848 				data_mp = mp->b_cont;
849 			else
850 				data_mp = mp;
851 			if (data_mp->b_prev != NULL) {
852 				ill_t   *inbound_ill;
853 				queue_t *fwdq = NULL;
854 				uint_t ifindex;
855 
856 				ifindex = (uint_t)(uintptr_t)data_mp->b_prev;
857 				inbound_ill = ill_lookup_on_ifindex(ifindex,
858 				    B_TRUE, NULL, NULL, NULL, NULL);
859 				if (inbound_ill == NULL) {
860 					data_mp->b_prev = NULL;
861 					freemsg(mp);
862 					return;
863 				} else {
864 					fwdq = inbound_ill->ill_rq;
865 				}
866 				data_mp->b_prev = NULL;
867 				/*
868 				 * Send a forwarded packet back into ip_rput_v6
869 				 * just as in ire_send_v6().
870 				 * Extract the queue from b_prev (set in
871 				 * ip_rput_data_v6).
872 				 */
873 				if (fwdq != NULL) {
874 					/*
875 					 * Forwarded packets hop count will
876 					 * get decremented in ip_rput_data_v6
877 					 */
878 					if (data_mp != mp)
879 						freeb(mp);
880 					put(fwdq, data_mp);
881 				} else {
882 					/*
883 					 * Send locally originated packets back
884 					 * into * ip_wput_v6.
885 					 */
886 					put(ill->ill_wq, mp);
887 				}
888 				ill_refrele(inbound_ill);
889 			} else {
890 				put(ill->ill_wq, mp);
891 			}
892 			mp = nxt_mp;
893 		}
894 		return;
895 	}
896 	ll_changed = nce_cmp_ll_addr(nce, hw_addr, hw_addr_len);
897 	if (!is_adv) {
898 		/* If this is a SOLICITATION request only */
899 		if (ll_changed)
900 			nce_update(nce, ND_STALE, hw_addr);
901 		mutex_exit(&nce->nce_lock);
902 		return;
903 	}
904 	if (!(flag & ND_NA_FLAG_OVERRIDE) && ll_changed) {
905 		/* If in any other state than REACHABLE, ignore */
906 		if (nce->nce_state == ND_REACHABLE) {
907 			nce_update(nce, ND_STALE, NULL);
908 		}
909 		mutex_exit(&nce->nce_lock);
910 		return;
911 	} else {
912 		if (ll_changed) {
913 			nce_update(nce, ND_UNCHANGED, hw_addr);
914 			ll_updated = B_TRUE;
915 		}
916 		if (flag & ND_NA_FLAG_SOLICITED) {
917 			nce_update(nce, ND_REACHABLE, NULL);
918 		} else {
919 			if (ll_updated) {
920 				nce_update(nce, ND_STALE, NULL);
921 			}
922 		}
923 		mutex_exit(&nce->nce_lock);
924 		if (!(flag & ND_NA_FLAG_ROUTER) && (nce->nce_flags &
925 		    NCE_F_ISROUTER)) {
926 			ire_t *ire;
927 
928 			/*
929 			 * Router turned to host.  We need to remove the
930 			 * entry as well as any default route that may be
931 			 * using this as a next hop.  This is required by
932 			 * section 7.2.5 of RFC 2461.
933 			 */
934 			ire = ire_ftable_lookup_v6(&ipv6_all_zeros,
935 			    &ipv6_all_zeros, &nce->nce_addr, IRE_DEFAULT,
936 			    nce->nce_ill->ill_ipif, NULL, ALL_ZONES, 0, NULL,
937 			    MATCH_IRE_ILL | MATCH_IRE_TYPE | MATCH_IRE_GW |
938 			    MATCH_IRE_DEFAULT);
939 			if (ire != NULL) {
940 				ip_rts_rtmsg(RTM_DELETE, ire, 0);
941 				ire_delete(ire);
942 				ire_refrele(ire);
943 			}
944 			ndp_delete(nce);
945 		}
946 	}
947 }
948 
949 /*
950  * Pass arg1 to the pfi supplied, along with each nce in existence.
951  * ndp_walk() places a REFHOLD on the nce and drops the lock when
952  * walking the hash list.
953  */
954 void
955 ndp_walk_common(ndp_g_t *ndp, ill_t *ill, pfi_t pfi, void *arg1,
956     boolean_t trace)
957 {
958 
959 	nce_t	*nce;
960 	nce_t	*nce1;
961 	nce_t	**ncep;
962 	nce_t	*free_nce_list = NULL;
963 
964 	mutex_enter(&ndp->ndp_g_lock);
965 	/* Prevent ndp_delete from unlink and free of NCE */
966 	ndp->ndp_g_walker++;
967 	mutex_exit(&ndp->ndp_g_lock);
968 	for (ncep = ndp->nce_hash_tbl;
969 	    ncep < A_END(ndp->nce_hash_tbl); ncep++) {
970 		for (nce = *ncep; nce != NULL; nce = nce1) {
971 			nce1 = nce->nce_next;
972 			if (ill == NULL || nce->nce_ill == ill) {
973 				if (trace) {
974 					NCE_REFHOLD(nce);
975 					(*pfi)(nce, arg1);
976 					NCE_REFRELE(nce);
977 				} else {
978 					NCE_REFHOLD_NOTR(nce);
979 					(*pfi)(nce, arg1);
980 					NCE_REFRELE_NOTR(nce);
981 				}
982 			}
983 		}
984 	}
985 	for (nce = ndp->nce_mask_entries; nce != NULL; nce = nce1) {
986 		nce1 = nce->nce_next;
987 		if (ill == NULL || nce->nce_ill == ill) {
988 			if (trace) {
989 				NCE_REFHOLD(nce);
990 				(*pfi)(nce, arg1);
991 				NCE_REFRELE(nce);
992 			} else {
993 				NCE_REFHOLD_NOTR(nce);
994 				(*pfi)(nce, arg1);
995 				NCE_REFRELE_NOTR(nce);
996 			}
997 		}
998 	}
999 	mutex_enter(&ndp->ndp_g_lock);
1000 	ndp->ndp_g_walker--;
1001 	/*
1002 	 * While NCE's are removed from global list they are placed
1003 	 * in a private list, to be passed to nce_ire_delete_list().
1004 	 * The reason is, there may be ires pointing to this nce
1005 	 * which needs to cleaned up.
1006 	 */
1007 	if (ndp->ndp_g_walker_cleanup && ndp->ndp_g_walker == 0) {
1008 		/* Time to delete condemned entries */
1009 		for (ncep = ndp->nce_hash_tbl;
1010 		    ncep < A_END(ndp->nce_hash_tbl); ncep++) {
1011 			nce = *ncep;
1012 			if (nce != NULL) {
1013 				nce_remove(ndp, nce, &free_nce_list);
1014 			}
1015 		}
1016 		nce = ndp->nce_mask_entries;
1017 		if (nce != NULL) {
1018 			nce_remove(ndp, nce, &free_nce_list);
1019 		}
1020 		ndp->ndp_g_walker_cleanup = B_FALSE;
1021 	}
1022 	mutex_exit(&ndp->ndp_g_lock);
1023 
1024 	if (free_nce_list != NULL) {
1025 		nce_ire_delete_list(free_nce_list);
1026 	}
1027 }
1028 
1029 void
1030 ndp_walk(ill_t *ill, pfi_t pfi, void *arg1)
1031 {
1032 	ndp_walk_common(&ndp4, ill, pfi, arg1, B_TRUE);
1033 	ndp_walk_common(&ndp6, ill, pfi, arg1, B_TRUE);
1034 }
1035 
1036 /*
1037  * Process resolve requests.  Handles both mapped entries
1038  * as well as cases that needs to be send out on the wire.
1039  * Lookup a NCE for a given IRE.  Regardless of whether one exists
1040  * or one is created, we defer making ire point to nce until the
1041  * ire is actually added at which point the nce_refcnt on the nce is
1042  * incremented.  This is done primarily to have symmetry between ire_add()
1043  * and ire_delete() which decrements the nce_refcnt, when an ire is deleted.
1044  */
1045 int
1046 ndp_resolver(ill_t *ill, const in6_addr_t *dst, mblk_t *mp, zoneid_t zoneid)
1047 {
1048 	nce_t		*nce;
1049 	int		err = 0;
1050 	uint32_t	ms;
1051 	mblk_t		*mp_nce = NULL;
1052 
1053 	ASSERT(ill != NULL);
1054 	ASSERT(ill->ill_isv6);
1055 	if (IN6_IS_ADDR_MULTICAST(dst)) {
1056 		err = nce_set_multicast(ill, dst);
1057 		return (err);
1058 	}
1059 	err = ndp_lookup_then_add(ill,
1060 	    NULL,	/* No hardware address */
1061 	    dst,
1062 	    &ipv6_all_ones,
1063 	    &ipv6_all_zeros,
1064 	    0,
1065 	    (ill->ill_flags & ILLF_NONUD) ? NCE_F_NONUD : 0,
1066 	    ND_INCOMPLETE,
1067 	    &nce,
1068 	    NULL, /* let ndp_add figure out fastpath mp and dlureq_mp for v6 */
1069 	    NULL);
1070 
1071 	switch (err) {
1072 	case 0:
1073 		/*
1074 		 * New cache entry was created. Make sure that the state
1075 		 * is not ND_INCOMPLETE. It can be in some other state
1076 		 * even before we send out the solicitation as we could
1077 		 * get un-solicited advertisements.
1078 		 *
1079 		 * If this is an XRESOLV interface, simply return 0,
1080 		 * since we don't want to solicit just yet.
1081 		 */
1082 		if (ill->ill_flags & ILLF_XRESOLV) {
1083 			NCE_REFRELE(nce);
1084 			return (0);
1085 		}
1086 		rw_enter(&ill_g_lock, RW_READER);
1087 		mutex_enter(&nce->nce_lock);
1088 		if (nce->nce_state != ND_INCOMPLETE) {
1089 			mutex_exit(&nce->nce_lock);
1090 			rw_exit(&ill_g_lock);
1091 			NCE_REFRELE(nce);
1092 			return (0);
1093 		}
1094 		mp_nce = ip_prepend_zoneid(mp, zoneid);
1095 		if (mp_nce == NULL) {
1096 			/* The caller will free mp */
1097 			mutex_exit(&nce->nce_lock);
1098 			rw_exit(&ill_g_lock);
1099 			ndp_delete(nce);
1100 			NCE_REFRELE(nce);
1101 			return (ENOMEM);
1102 		}
1103 		ms = nce_solicit(nce, mp_nce);
1104 		rw_exit(&ill_g_lock);
1105 		if (ms == 0) {
1106 			/* The caller will free mp */
1107 			if (mp_nce != mp)
1108 				freeb(mp_nce);
1109 			mutex_exit(&nce->nce_lock);
1110 			ndp_delete(nce);
1111 			NCE_REFRELE(nce);
1112 			return (EBUSY);
1113 		}
1114 		mutex_exit(&nce->nce_lock);
1115 		NDP_RESTART_TIMER(nce, (clock_t)ms);
1116 		NCE_REFRELE(nce);
1117 		return (EINPROGRESS);
1118 	case EEXIST:
1119 		/* Resolution in progress just queue the packet */
1120 		mutex_enter(&nce->nce_lock);
1121 		if (nce->nce_state == ND_INCOMPLETE) {
1122 			mp_nce = ip_prepend_zoneid(mp, zoneid);
1123 			if (mp_nce == NULL) {
1124 				err = ENOMEM;
1125 			} else {
1126 				nce_queue_mp(nce, mp_nce);
1127 				err = EINPROGRESS;
1128 			}
1129 		} else {
1130 			/*
1131 			 * Any other state implies we have
1132 			 * a nce but IRE needs to be added ...
1133 			 * ire_add_v6() will take care of the
1134 			 * the case when the nce becomes CONDEMNED
1135 			 * before the ire is added to the table.
1136 			 */
1137 			err = 0;
1138 		}
1139 		mutex_exit(&nce->nce_lock);
1140 		NCE_REFRELE(nce);
1141 		break;
1142 	default:
1143 		ip1dbg(("ndp_resolver: Can't create NCE %d\n", err));
1144 		break;
1145 	}
1146 	return (err);
1147 }
1148 
1149 /*
1150  * When there is no resolver, the link layer template is passed in
1151  * the IRE.
1152  * Lookup a NCE for a given IRE.  Regardless of whether one exists
1153  * or one is created, we defer making ire point to nce until the
1154  * ire is actually added at which point the nce_refcnt on the nce is
1155  * incremented.  This is done primarily to have symmetry between ire_add()
1156  * and ire_delete() which decrements the nce_refcnt, when an ire is deleted.
1157  */
1158 int
1159 ndp_noresolver(ill_t *ill, const in6_addr_t *dst)
1160 {
1161 	nce_t		*nce;
1162 	int		err = 0;
1163 
1164 	ASSERT(ill != NULL);
1165 	ASSERT(ill->ill_isv6);
1166 	if (IN6_IS_ADDR_MULTICAST(dst)) {
1167 		err = nce_set_multicast(ill, dst);
1168 		return (err);
1169 	}
1170 
1171 	err = ndp_lookup_then_add(ill,
1172 	    NULL,	/* hardware address */
1173 	    dst,
1174 	    &ipv6_all_ones,
1175 	    &ipv6_all_zeros,
1176 	    0,
1177 	    (ill->ill_flags & ILLF_NONUD) ? NCE_F_NONUD : 0,
1178 	    ND_REACHABLE,
1179 	    &nce,
1180 	    NULL, /* let ndp_add figure out fp_mp/dlureq_mp for v6 */
1181 	    NULL);
1182 
1183 	switch (err) {
1184 	case 0:
1185 		/*
1186 		 * Cache entry with a proper resolver cookie was
1187 		 * created.
1188 		 */
1189 		NCE_REFRELE(nce);
1190 		break;
1191 	case EEXIST:
1192 		err = 0;
1193 		NCE_REFRELE(nce);
1194 		break;
1195 	default:
1196 		ip1dbg(("ndp_noresolver: Can't create NCE %d\n", err));
1197 		break;
1198 	}
1199 	return (err);
1200 }
1201 
1202 /*
1203  * For each interface an entry is added for the unspecified multicast group.
1204  * Here that mapping is used to form the multicast cache entry for a particular
1205  * multicast destination.
1206  */
1207 static int
1208 nce_set_multicast(ill_t *ill, const in6_addr_t *dst)
1209 {
1210 	nce_t		*mnce;	/* Multicast mapping entry */
1211 	nce_t		*nce;
1212 	uchar_t		*hw_addr = NULL;
1213 	int		err = 0;
1214 
1215 	ASSERT(ill != NULL);
1216 	ASSERT(ill->ill_isv6);
1217 	ASSERT(!(IN6_IS_ADDR_UNSPECIFIED(dst)));
1218 
1219 	mutex_enter(&ndp6.ndp_g_lock);
1220 	nce = *((nce_t **)NCE_HASH_PTR_V6(*dst));
1221 	nce = nce_lookup_addr(ill, dst, nce);
1222 	if (nce != NULL) {
1223 		mutex_exit(&ndp6.ndp_g_lock);
1224 		NCE_REFRELE(nce);
1225 		return (0);
1226 	}
1227 	/* No entry, now lookup for a mapping this should never fail */
1228 	mnce = nce_lookup_mapping(ill, dst);
1229 	if (mnce == NULL) {
1230 		/* Something broken for the interface. */
1231 		mutex_exit(&ndp6.ndp_g_lock);
1232 		return (ESRCH);
1233 	}
1234 	ASSERT(mnce->nce_flags & NCE_F_MAPPING);
1235 	if (ill->ill_net_type == IRE_IF_RESOLVER) {
1236 		/*
1237 		 * For IRE_IF_RESOLVER a hardware mapping can be
1238 		 * generated, for IRE_IF_NORESOLVER, resolution cookie
1239 		 * in the ill is copied in ndp_add().
1240 		 */
1241 		hw_addr = kmem_alloc(ill->ill_nd_lla_len, KM_NOSLEEP);
1242 		if (hw_addr == NULL) {
1243 			mutex_exit(&ndp6.ndp_g_lock);
1244 			NCE_REFRELE(mnce);
1245 			return (ENOMEM);
1246 		}
1247 		nce_make_mapping(mnce, hw_addr, (uchar_t *)dst);
1248 	}
1249 	NCE_REFRELE(mnce);
1250 	/*
1251 	 * IRE_IF_NORESOLVER type simply copies the resolution
1252 	 * cookie passed in.  So no hw_addr is needed.
1253 	 */
1254 	err = ndp_add(ill,
1255 	    hw_addr,
1256 	    dst,
1257 	    &ipv6_all_ones,
1258 	    &ipv6_all_zeros,
1259 	    0,
1260 	    NCE_F_NONUD,
1261 	    ND_REACHABLE,
1262 	    &nce,
1263 	    NULL,
1264 	    NULL);
1265 	mutex_exit(&ndp6.ndp_g_lock);
1266 	if (hw_addr != NULL)
1267 		kmem_free(hw_addr, ill->ill_nd_lla_len);
1268 	if (err != 0) {
1269 		ip1dbg(("nce_set_multicast: create failed" "%d\n", err));
1270 		return (err);
1271 	}
1272 	NCE_REFRELE(nce);
1273 	return (0);
1274 }
1275 
1276 /*
1277  * Return the link layer address, and any flags of a nce.
1278  */
1279 int
1280 ndp_query(ill_t *ill, struct lif_nd_req *lnr)
1281 {
1282 	nce_t		*nce;
1283 	in6_addr_t	*addr;
1284 	sin6_t		*sin6;
1285 	dl_unitdata_req_t	*dl;
1286 
1287 	ASSERT(ill != NULL && ill->ill_isv6);
1288 	sin6 = (sin6_t *)&lnr->lnr_addr;
1289 	addr =  &sin6->sin6_addr;
1290 
1291 	nce = ndp_lookup_v6(ill, addr, B_FALSE);
1292 	if (nce == NULL)
1293 		return (ESRCH);
1294 	/* If in INCOMPLETE state, no link layer address is available yet */
1295 	if (nce->nce_state == ND_INCOMPLETE)
1296 		goto done;
1297 	dl = (dl_unitdata_req_t *)nce->nce_res_mp->b_rptr;
1298 	if (ill->ill_flags & ILLF_XRESOLV)
1299 		lnr->lnr_hdw_len = dl->dl_dest_addr_length;
1300 	else
1301 		lnr->lnr_hdw_len = ill->ill_nd_lla_len;
1302 	ASSERT(NCE_LL_ADDR_OFFSET(ill) + lnr->lnr_hdw_len <=
1303 	    sizeof (lnr->lnr_hdw_addr));
1304 	bcopy(nce->nce_res_mp->b_rptr + NCE_LL_ADDR_OFFSET(ill),
1305 	    (uchar_t *)&lnr->lnr_hdw_addr, lnr->lnr_hdw_len);
1306 	if (nce->nce_flags & NCE_F_ISROUTER)
1307 		lnr->lnr_flags = NDF_ISROUTER_ON;
1308 	if (nce->nce_flags & NCE_F_PROXY)
1309 		lnr->lnr_flags |= NDF_PROXY_ON;
1310 	if (nce->nce_flags & NCE_F_ANYCAST)
1311 		lnr->lnr_flags |= NDF_ANYCAST_ON;
1312 done:
1313 	NCE_REFRELE(nce);
1314 	return (0);
1315 }
1316 
1317 /*
1318  * Send Enable/Disable multicast reqs to driver.
1319  */
1320 int
1321 ndp_mcastreq(ill_t *ill, const in6_addr_t *addr, uint32_t hw_addr_len,
1322     uint32_t hw_addr_offset, mblk_t *mp)
1323 {
1324 	nce_t		*nce;
1325 	uchar_t		*hw_addr;
1326 
1327 	ASSERT(ill != NULL && ill->ill_isv6);
1328 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
1329 	hw_addr = mi_offset_paramc(mp, hw_addr_offset, hw_addr_len);
1330 	if (hw_addr == NULL || !IN6_IS_ADDR_MULTICAST(addr)) {
1331 		freemsg(mp);
1332 		return (EINVAL);
1333 	}
1334 	mutex_enter(&ndp6.ndp_g_lock);
1335 	nce = nce_lookup_mapping(ill, addr);
1336 	if (nce == NULL) {
1337 		mutex_exit(&ndp6.ndp_g_lock);
1338 		freemsg(mp);
1339 		return (ESRCH);
1340 	}
1341 	mutex_exit(&ndp6.ndp_g_lock);
1342 	/*
1343 	 * Update dl_addr_length and dl_addr_offset for primitives that
1344 	 * have physical addresses as opposed to full saps
1345 	 */
1346 	switch (((union DL_primitives *)mp->b_rptr)->dl_primitive) {
1347 	case DL_ENABMULTI_REQ:
1348 		/* Track the state if this is the first enabmulti */
1349 		if (ill->ill_dlpi_multicast_state == IDS_UNKNOWN)
1350 			ill->ill_dlpi_multicast_state = IDS_INPROGRESS;
1351 		ip1dbg(("ndp_mcastreq: ENABMULTI\n"));
1352 		break;
1353 	case DL_DISABMULTI_REQ:
1354 		ip1dbg(("ndp_mcastreq: DISABMULTI\n"));
1355 		break;
1356 	default:
1357 		NCE_REFRELE(nce);
1358 		ip1dbg(("ndp_mcastreq: default\n"));
1359 		return (EINVAL);
1360 	}
1361 	nce_make_mapping(nce, hw_addr, (uchar_t *)addr);
1362 	NCE_REFRELE(nce);
1363 	putnext(ill->ill_wq, mp);
1364 	return (0);
1365 }
1366 
1367 /*
1368  * Send a neighbor solicitation.
1369  * Returns number of milliseconds after which we should either rexmit or abort.
1370  * Return of zero means we should abort.
1371  * The caller holds the nce_lock to protect nce_qd_mp and nce_rcnt.
1372  *
1373  * NOTE: This routine drops nce_lock (and later reacquires it) when sending
1374  * the packet.
1375  * NOTE: This routine does not consume mp.
1376  */
1377 uint32_t
1378 nce_solicit(nce_t *nce, mblk_t *mp)
1379 {
1380 	ill_t		*ill;
1381 	ill_t		*src_ill;
1382 	ip6_t		*ip6h;
1383 	in6_addr_t	src;
1384 	in6_addr_t	dst;
1385 	ipif_t		*ipif;
1386 	ip6i_t		*ip6i;
1387 	boolean_t	dropped = B_FALSE;
1388 
1389 	ASSERT(RW_READ_HELD(&ill_g_lock));
1390 	ASSERT(MUTEX_HELD(&nce->nce_lock));
1391 	ill = nce->nce_ill;
1392 	ASSERT(ill != NULL);
1393 
1394 	if (nce->nce_rcnt == 0) {
1395 		return (0);
1396 	}
1397 
1398 	if (mp == NULL) {
1399 		ASSERT(nce->nce_qd_mp != NULL);
1400 		mp = nce->nce_qd_mp;
1401 	} else {
1402 		nce_queue_mp(nce, mp);
1403 	}
1404 
1405 	/* Handle ip_newroute_v6 giving us IPSEC packets */
1406 	if (mp->b_datap->db_type == M_CTL)
1407 		mp = mp->b_cont;
1408 
1409 	ip6h = (ip6_t *)mp->b_rptr;
1410 	if (ip6h->ip6_nxt == IPPROTO_RAW) {
1411 		/*
1412 		 * This message should have been pulled up already in
1413 		 * ip_wput_v6. We can't do pullups here because the message
1414 		 * could be from the nce_qd_mp which could have b_next/b_prev
1415 		 * non-NULL.
1416 		 */
1417 		ip6i = (ip6i_t *)ip6h;
1418 		ASSERT((mp->b_wptr - (uchar_t *)ip6i) >=
1419 			    sizeof (ip6i_t) + IPV6_HDR_LEN);
1420 		ip6h = (ip6_t *)(mp->b_rptr + sizeof (ip6i_t));
1421 	}
1422 	src = ip6h->ip6_src;
1423 	/*
1424 	 * If the src of outgoing packet is one of the assigned interface
1425 	 * addresses use it, otherwise we will pick the source address below.
1426 	 */
1427 	src_ill = ill;
1428 	if (!IN6_IS_ADDR_UNSPECIFIED(&src)) {
1429 		if (ill->ill_group != NULL)
1430 			src_ill = ill->ill_group->illgrp_ill;
1431 		for (; src_ill != NULL; src_ill = src_ill->ill_group_next) {
1432 			for (ipif = src_ill->ill_ipif; ipif != NULL;
1433 			    ipif = ipif->ipif_next) {
1434 				if (IN6_ARE_ADDR_EQUAL(&src,
1435 				    &ipif->ipif_v6lcl_addr)) {
1436 					break;
1437 				}
1438 			}
1439 			if (ipif != NULL)
1440 				break;
1441 		}
1442 		/*
1443 		 * If no relevant ipif can be found, then it's not one of our
1444 		 * addresses.  Reset to :: and let nce_xmit.  If an ipif can be
1445 		 * found, but it's not yet done with DAD verification, then
1446 		 * just postpone this transmission until later.
1447 		 */
1448 		if (src_ill == NULL)
1449 			src = ipv6_all_zeros;
1450 		else if (!ipif->ipif_addr_ready)
1451 			return (ill->ill_reachable_retrans_time);
1452 	}
1453 	dst = nce->nce_addr;
1454 	/*
1455 	 * If source address is unspecified, nce_xmit will choose
1456 	 * one for us and initialize the hardware address also
1457 	 * appropriately.
1458 	 */
1459 	if (IN6_IS_ADDR_UNSPECIFIED(&src))
1460 		src_ill = NULL;
1461 	nce->nce_rcnt--;
1462 	mutex_exit(&nce->nce_lock);
1463 	rw_exit(&ill_g_lock);
1464 	dropped = nce_xmit(ill, ND_NEIGHBOR_SOLICIT, src_ill, B_TRUE, &src,
1465 	    &dst, 0);
1466 	rw_enter(&ill_g_lock, RW_READER);
1467 	mutex_enter(&nce->nce_lock);
1468 	if (dropped)
1469 		nce->nce_rcnt++;
1470 	return (ill->ill_reachable_retrans_time);
1471 }
1472 
1473 /*
1474  * Attempt to recover an address on an interface that's been marked as a
1475  * duplicate.  Because NCEs are destroyed when the interface goes down, there's
1476  * no easy way to just probe the address and have the right thing happen if
1477  * it's no longer in use.  Instead, we just bring it up normally and allow the
1478  * regular interface start-up logic to probe for a remaining duplicate and take
1479  * us back down if necessary.
1480  * Neither DHCP nor temporary addresses arrive here; they're excluded by
1481  * ip_ndp_excl.
1482  */
1483 /* ARGSUSED */
1484 static void
1485 ip_ndp_recover(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg)
1486 {
1487 	ill_t	*ill = rq->q_ptr;
1488 	ipif_t	*ipif;
1489 	in6_addr_t *addr = (in6_addr_t *)mp->b_rptr;
1490 
1491 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1492 		/*
1493 		 * We do not support recovery of proxy ARP'd interfaces,
1494 		 * because the system lacks a complete proxy ARP mechanism.
1495 		 */
1496 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) ||
1497 		    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, addr)) {
1498 			continue;
1499 		}
1500 
1501 		/*
1502 		 * If we have already recovered, then ignore.
1503 		 */
1504 		mutex_enter(&ill->ill_lock);
1505 		if (!(ipif->ipif_flags & IPIF_DUPLICATE)) {
1506 			mutex_exit(&ill->ill_lock);
1507 			continue;
1508 		}
1509 
1510 		ipif->ipif_flags &= ~IPIF_DUPLICATE;
1511 		ill->ill_ipif_dup_count--;
1512 		mutex_exit(&ill->ill_lock);
1513 		ipif->ipif_was_dup = B_TRUE;
1514 
1515 		if (ipif_ndp_up(ipif, addr, B_FALSE) != EINPROGRESS)
1516 			(void) ipif_up_done_v6(ipif);
1517 	}
1518 	freeb(mp);
1519 }
1520 
1521 /*
1522  * Attempt to recover an IPv6 interface that's been shut down as a duplicate.
1523  * As long as someone else holds the address, the interface will stay down.
1524  * When that conflict goes away, the interface is brought back up.  This is
1525  * done so that accidental shutdowns of addresses aren't made permanent.  Your
1526  * server will recover from a failure.
1527  *
1528  * For DHCP and temporary addresses, recovery is not done in the kernel.
1529  * Instead, it's handled by user space processes (dhcpagent and in.ndpd).
1530  *
1531  * This function is entered on a timer expiry; the ID is in ipif_recovery_id.
1532  */
1533 static void
1534 ipif6_dup_recovery(void *arg)
1535 {
1536 	ipif_t *ipif = arg;
1537 
1538 	ipif->ipif_recovery_id = 0;
1539 	if (!(ipif->ipif_flags & IPIF_DUPLICATE))
1540 		return;
1541 
1542 	/* If the link is down, we'll retry this later */
1543 	if (!(ipif->ipif_ill->ill_phyint->phyint_flags & PHYI_RUNNING))
1544 		return;
1545 
1546 	ndp_do_recovery(ipif);
1547 }
1548 
1549 /*
1550  * Perform interface recovery by forcing the duplicate interfaces up and
1551  * allowing the system to determine which ones should stay up.
1552  *
1553  * Called both by recovery timer expiry and link-up notification.
1554  */
1555 void
1556 ndp_do_recovery(ipif_t *ipif)
1557 {
1558 	ill_t *ill = ipif->ipif_ill;
1559 	mblk_t *mp;
1560 
1561 	mp = allocb(sizeof (ipif->ipif_v6lcl_addr), BPRI_MED);
1562 	if (mp == NULL) {
1563 		ipif->ipif_recovery_id = timeout(ipif6_dup_recovery,
1564 		    ipif, MSEC_TO_TICK(ip_dup_recovery));
1565 	} else {
1566 		bcopy(&ipif->ipif_v6lcl_addr, mp->b_rptr,
1567 		    sizeof (ipif->ipif_v6lcl_addr));
1568 		ill_refhold(ill);
1569 		(void) qwriter_ip(NULL, ill, ill->ill_rq, mp, ip_ndp_recover,
1570 		    CUR_OP, B_FALSE);
1571 	}
1572 }
1573 
1574 /*
1575  * Find the solicitation in the given message, and extract printable details
1576  * (MAC and IP addresses) from it.
1577  */
1578 static nd_neighbor_solicit_t *
1579 ip_ndp_find_solicitation(mblk_t *mp, mblk_t *dl_mp, ill_t *ill, char *hbuf,
1580     size_t hlen, char *sbuf, size_t slen, uchar_t **haddr)
1581 {
1582 	nd_neighbor_solicit_t *ns;
1583 	ip6_t *ip6h;
1584 	uchar_t *addr;
1585 	int alen;
1586 
1587 	alen = 0;
1588 	ip6h = (ip6_t *)mp->b_rptr;
1589 	if (dl_mp == NULL) {
1590 		nd_opt_hdr_t *opt;
1591 		int nslen;
1592 
1593 		/*
1594 		 * If it's from the fast-path, then it can't be a probe
1595 		 * message, and thus must include the source linkaddr option.
1596 		 * Extract that here.
1597 		 */
1598 		ns = (nd_neighbor_solicit_t *)((char *)ip6h + IPV6_HDR_LEN);
1599 		nslen = mp->b_wptr - (uchar_t *)ns;
1600 		if ((nslen -= sizeof (*ns)) > 0) {
1601 			opt = ndp_get_option((nd_opt_hdr_t *)(ns + 1), nslen,
1602 			    ND_OPT_SOURCE_LINKADDR);
1603 			if (opt != NULL &&
1604 			    opt->nd_opt_len * 8 - sizeof (*opt) >=
1605 			    ill->ill_nd_lla_len) {
1606 				addr = (uchar_t *)(opt + 1);
1607 				alen = ill->ill_nd_lla_len;
1608 			}
1609 		}
1610 		/*
1611 		 * We cheat a bit here for the sake of printing usable log
1612 		 * messages in the rare case where the reply we got was unicast
1613 		 * without a source linkaddr option, and the interface is in
1614 		 * fastpath mode.  (Sigh.)
1615 		 */
1616 		if (alen == 0 && ill->ill_type == IFT_ETHER &&
1617 		    MBLKHEAD(mp) >= sizeof (struct ether_header)) {
1618 			struct ether_header *pether;
1619 
1620 			pether = (struct ether_header *)((char *)ip6h -
1621 			    sizeof (*pether));
1622 			addr = pether->ether_shost.ether_addr_octet;
1623 			alen = ETHERADDRL;
1624 		}
1625 	} else {
1626 		dl_unitdata_ind_t *dlu;
1627 
1628 		dlu = (dl_unitdata_ind_t *)dl_mp->b_rptr;
1629 		alen = dlu->dl_src_addr_length;
1630 		if (alen > 0 && dlu->dl_src_addr_offset >= sizeof (*dlu) &&
1631 		    dlu->dl_src_addr_offset + alen <= MBLKL(dl_mp)) {
1632 			addr = dl_mp->b_rptr + dlu->dl_src_addr_offset;
1633 			if (ill->ill_sap_length < 0) {
1634 				alen += ill->ill_sap_length;
1635 			} else {
1636 				addr += ill->ill_sap_length;
1637 				alen -= ill->ill_sap_length;
1638 			}
1639 		}
1640 	}
1641 	if (alen > 0) {
1642 		*haddr = addr;
1643 		(void) mac_colon_addr(addr, alen, hbuf, hlen);
1644 	} else {
1645 		*haddr = NULL;
1646 		(void) strcpy(hbuf, "?");
1647 	}
1648 	ns = (nd_neighbor_solicit_t *)((char *)ip6h + IPV6_HDR_LEN);
1649 	(void) inet_ntop(AF_INET6, &ns->nd_ns_target, sbuf, slen);
1650 	return (ns);
1651 }
1652 
1653 /*
1654  * This is for exclusive changes due to NDP duplicate address detection
1655  * failure.
1656  */
1657 /* ARGSUSED */
1658 static void
1659 ip_ndp_excl(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg)
1660 {
1661 	ill_t	*ill = rq->q_ptr;
1662 	ipif_t	*ipif;
1663 	char ibuf[LIFNAMSIZ + 10];	/* 10 digits for logical i/f number */
1664 	char hbuf[MAC_STR_LEN];
1665 	char sbuf[INET6_ADDRSTRLEN];
1666 	nd_neighbor_solicit_t *ns;
1667 	mblk_t *dl_mp = NULL;
1668 	uchar_t *haddr;
1669 
1670 	if (DB_TYPE(mp) != M_DATA) {
1671 		dl_mp = mp;
1672 		mp = mp->b_cont;
1673 	}
1674 	ns = ip_ndp_find_solicitation(mp, dl_mp, ill, hbuf, sizeof (hbuf), sbuf,
1675 	    sizeof (sbuf), &haddr);
1676 	if (haddr != NULL &&
1677 	    bcmp(haddr, ill->ill_phys_addr, ill->ill_phys_addr_length) == 0) {
1678 		/*
1679 		 * Ignore conflicts generated by misbehaving switches that just
1680 		 * reflect our own messages back to us.
1681 		 */
1682 		goto ignore_conflict;
1683 	}
1684 	(void) strlcpy(ibuf, ill->ill_name, sizeof (ibuf));
1685 	for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1686 
1687 		if ((ipif->ipif_flags & IPIF_POINTOPOINT) ||
1688 		    !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
1689 		    &ns->nd_ns_target)) {
1690 			continue;
1691 		}
1692 
1693 		/* If it's already marked, then don't do anything. */
1694 		if (ipif->ipif_flags & IPIF_DUPLICATE)
1695 			continue;
1696 
1697 		/*
1698 		 * If this is a failure during duplicate recovery, then don't
1699 		 * complain.  It may take a long time to recover.
1700 		 */
1701 		if (!ipif->ipif_was_dup) {
1702 			if (ipif->ipif_id != 0) {
1703 				(void) snprintf(ibuf + ill->ill_name_length - 1,
1704 				    sizeof (ibuf) - ill->ill_name_length + 1,
1705 				    ":%d", ipif->ipif_id);
1706 			}
1707 			cmn_err(CE_WARN, "%s has duplicate address %s (in "
1708 			    "use by %s); disabled", ibuf, sbuf, hbuf);
1709 		}
1710 		mutex_enter(&ill->ill_lock);
1711 		ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
1712 		ipif->ipif_flags |= IPIF_DUPLICATE;
1713 		ill->ill_ipif_dup_count++;
1714 		mutex_exit(&ill->ill_lock);
1715 		(void) ipif_down(ipif, NULL, NULL);
1716 		ipif_down_tail(ipif);
1717 		if (!(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) &&
1718 		    ill->ill_net_type == IRE_IF_RESOLVER &&
1719 		    ip_dup_recovery > 0)
1720 			ipif->ipif_recovery_id = timeout(ipif6_dup_recovery,
1721 			    ipif, MSEC_TO_TICK(ip_dup_recovery));
1722 	}
1723 ignore_conflict:
1724 	if (dl_mp != NULL)
1725 		freeb(dl_mp);
1726 	freemsg(mp);
1727 }
1728 
1729 /*
1730  * Handle failure by tearing down the ipifs with the specified address.  Note
1731  * that tearing down the ipif also means deleting the nce through ipif_down, so
1732  * it's not possible to do recovery by just restarting the nce timer.  Instead,
1733  * we start a timer on the ipif.
1734  */
1735 static void
1736 ip_ndp_failure(ill_t *ill, mblk_t *mp, mblk_t *dl_mp, nce_t *nce)
1737 {
1738 	if ((mp = copymsg(mp)) != NULL) {
1739 		if (dl_mp == NULL)
1740 			dl_mp = mp;
1741 		else if ((dl_mp = copyb(dl_mp)) != NULL)
1742 			dl_mp->b_cont = mp;
1743 		if (dl_mp == NULL) {
1744 			freemsg(mp);
1745 		} else {
1746 			ill_refhold(ill);
1747 			(void) qwriter_ip(NULL, ill, ill->ill_rq, dl_mp,
1748 			    ip_ndp_excl, CUR_OP, B_FALSE);
1749 		}
1750 	}
1751 	ndp_delete(nce);
1752 }
1753 
1754 /*
1755  * Handle a discovered conflict: some other system is advertising that it owns
1756  * one of our IP addresses.  We need to defend ourselves, or just shut down the
1757  * interface.
1758  */
1759 static void
1760 ip_ndp_conflict(ill_t *ill, mblk_t *mp, mblk_t *dl_mp, nce_t *nce)
1761 {
1762 	ipif_t *ipif;
1763 	uint32_t now;
1764 	uint_t maxdefense;
1765 	uint_t defs;
1766 
1767 	ipif = ipif_lookup_addr_v6(&nce->nce_addr, ill, ALL_ZONES, NULL, NULL,
1768 	    NULL, NULL);
1769 	if (ipif == NULL)
1770 		return;
1771 	/*
1772 	 * First, figure out if this address is disposable.
1773 	 */
1774 	if (ipif->ipif_flags & (IPIF_DHCPRUNNING | IPIF_TEMPORARY))
1775 		maxdefense = ip_max_temp_defend;
1776 	else
1777 		maxdefense = ip_max_defend;
1778 
1779 	/*
1780 	 * Now figure out how many times we've defended ourselves.  Ignore
1781 	 * defenses that happened long in the past.
1782 	 */
1783 	now = gethrestime_sec();
1784 	mutex_enter(&nce->nce_lock);
1785 	if ((defs = nce->nce_defense_count) > 0 &&
1786 	    now - nce->nce_defense_time > ip_defend_interval) {
1787 		nce->nce_defense_count = defs = 0;
1788 	}
1789 	nce->nce_defense_count++;
1790 	nce->nce_defense_time = now;
1791 	mutex_exit(&nce->nce_lock);
1792 	ipif_refrele(ipif);
1793 
1794 	/*
1795 	 * If we've defended ourselves too many times already, then give up and
1796 	 * tear down the interface(s) using this address.  Otherwise, defend by
1797 	 * sending out an unsolicited Neighbor Advertisement.
1798 	 */
1799 	if (defs >= maxdefense) {
1800 		ip_ndp_failure(ill, mp, dl_mp, nce);
1801 	} else {
1802 		char hbuf[MAC_STR_LEN];
1803 		char sbuf[INET6_ADDRSTRLEN];
1804 		uchar_t *haddr;
1805 
1806 		(void) ip_ndp_find_solicitation(mp, dl_mp, ill, hbuf,
1807 		    sizeof (hbuf), sbuf, sizeof (sbuf), &haddr);
1808 		cmn_err(CE_WARN, "node %s is using our IP address %s on %s",
1809 		    hbuf, sbuf, ill->ill_name);
1810 		(void) nce_xmit(ill, ND_NEIGHBOR_ADVERT, ill, B_FALSE,
1811 		    &nce->nce_addr, &ipv6_all_hosts_mcast,
1812 		    nce_advert_flags(nce));
1813 	}
1814 }
1815 
1816 static void
1817 ndp_input_solicit(ill_t *ill, mblk_t *mp, mblk_t *dl_mp)
1818 {
1819 	nd_neighbor_solicit_t *ns;
1820 	uint32_t	hlen = ill->ill_nd_lla_len;
1821 	uchar_t		*haddr = NULL;
1822 	icmp6_t		*icmp_nd;
1823 	ip6_t		*ip6h;
1824 	nce_t		*our_nce = NULL;
1825 	in6_addr_t	target;
1826 	in6_addr_t	src;
1827 	int		len;
1828 	int		flag = 0;
1829 	nd_opt_hdr_t	*opt = NULL;
1830 	boolean_t	bad_solicit = B_FALSE;
1831 	mib2_ipv6IfIcmpEntry_t	*mib = ill->ill_icmp6_mib;
1832 
1833 	ip6h = (ip6_t *)mp->b_rptr;
1834 	icmp_nd = (icmp6_t *)(mp->b_rptr + IPV6_HDR_LEN);
1835 	len = mp->b_wptr - mp->b_rptr - IPV6_HDR_LEN;
1836 	src = ip6h->ip6_src;
1837 	ns = (nd_neighbor_solicit_t *)icmp_nd;
1838 	target = ns->nd_ns_target;
1839 	if (IN6_IS_ADDR_MULTICAST(&target)) {
1840 		if (ip_debug > 2) {
1841 			/* ip1dbg */
1842 			pr_addr_dbg("ndp_input_solicit: Target is"
1843 			    " multicast! %s\n", AF_INET6, &target);
1844 		}
1845 		bad_solicit = B_TRUE;
1846 		goto done;
1847 	}
1848 	if (len > sizeof (nd_neighbor_solicit_t)) {
1849 		/* Options present */
1850 		opt = (nd_opt_hdr_t *)&ns[1];
1851 		len -= sizeof (nd_neighbor_solicit_t);
1852 		if (!ndp_verify_optlen(opt, len)) {
1853 			ip1dbg(("ndp_input_solicit: Bad opt len\n"));
1854 			bad_solicit = B_TRUE;
1855 			goto done;
1856 		}
1857 	}
1858 	if (IN6_IS_ADDR_UNSPECIFIED(&src)) {
1859 		/* Check to see if this is a valid DAD solicitation */
1860 		if (!IN6_IS_ADDR_MC_SOLICITEDNODE(&ip6h->ip6_dst)) {
1861 			if (ip_debug > 2) {
1862 				/* ip1dbg */
1863 				pr_addr_dbg("ndp_input_solicit: IPv6 "
1864 				    "Destination is not solicited node "
1865 				    "multicast %s\n", AF_INET6,
1866 				    &ip6h->ip6_dst);
1867 			}
1868 			bad_solicit = B_TRUE;
1869 			goto done;
1870 		}
1871 	}
1872 
1873 	our_nce = ndp_lookup_v6(ill, &target, B_FALSE);
1874 	/*
1875 	 * If this is a valid Solicitation, a permanent
1876 	 * entry should exist in the cache
1877 	 */
1878 	if (our_nce == NULL ||
1879 	    !(our_nce->nce_flags & NCE_F_PERMANENT)) {
1880 		ip1dbg(("ndp_input_solicit: Wrong target in NS?!"
1881 		    "ifname=%s ", ill->ill_name));
1882 		if (ip_debug > 2) {
1883 			/* ip1dbg */
1884 			pr_addr_dbg(" dst %s\n", AF_INET6, &target);
1885 		}
1886 		bad_solicit = B_TRUE;
1887 		goto done;
1888 	}
1889 
1890 	/* At this point we should have a verified NS per spec */
1891 	if (opt != NULL) {
1892 		opt = ndp_get_option(opt, len, ND_OPT_SOURCE_LINKADDR);
1893 		if (opt != NULL) {
1894 			haddr = (uchar_t *)&opt[1];
1895 			if (hlen > opt->nd_opt_len * 8 - sizeof (*opt) ||
1896 			    hlen == 0) {
1897 				ip1dbg(("ndp_input_advert: bad SLLA\n"));
1898 				bad_solicit = B_TRUE;
1899 				goto done;
1900 			}
1901 		}
1902 	}
1903 
1904 	/* If sending directly to peer, set the unicast flag */
1905 	if (!IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst))
1906 		flag |= NDP_UNICAST;
1907 
1908 	/*
1909 	 * Create/update the entry for the soliciting node.
1910 	 * or respond to outstanding queries, don't if
1911 	 * the source is unspecified address.
1912 	 */
1913 	if (!IN6_IS_ADDR_UNSPECIFIED(&src)) {
1914 		int	err;
1915 		nce_t	*nnce;
1916 
1917 		ASSERT(ill->ill_isv6);
1918 		/*
1919 		 * Regular solicitations *must* include the Source Link-Layer
1920 		 * Address option.  Ignore messages that do not.
1921 		 */
1922 		if (haddr == NULL && IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) {
1923 			ip1dbg(("ndp_input_solicit: source link-layer address "
1924 			    "option missing with a specified source.\n"));
1925 			bad_solicit = B_TRUE;
1926 			goto done;
1927 		}
1928 
1929 		/*
1930 		 * This is a regular solicitation.  If we're still in the
1931 		 * process of verifying the address, then don't respond at all
1932 		 * and don't keep track of the sender.
1933 		 */
1934 		if (our_nce->nce_state == ND_PROBE)
1935 			goto done;
1936 
1937 		/*
1938 		 * If the solicitation doesn't have sender hardware address
1939 		 * (legal for unicast solicitation), then process without
1940 		 * installing the return NCE.  Either we already know it, or
1941 		 * we'll be forced to look it up when (and if) we reply to the
1942 		 * packet.
1943 		 */
1944 		if (haddr == NULL)
1945 			goto no_source;
1946 
1947 		err = ndp_lookup_then_add(ill,
1948 		    haddr,
1949 		    &src,	/* Soliciting nodes address */
1950 		    &ipv6_all_ones,
1951 		    &ipv6_all_zeros,
1952 		    0,
1953 		    0,
1954 		    ND_STALE,
1955 		    &nnce,
1956 		    NULL,
1957 		    NULL);
1958 		switch (err) {
1959 		case 0:
1960 			/* done with this entry */
1961 			NCE_REFRELE(nnce);
1962 			break;
1963 		case EEXIST:
1964 			/*
1965 			 * B_FALSE indicates this is not an
1966 			 * an advertisement.
1967 			 */
1968 			ndp_process(nnce, haddr, 0, B_FALSE);
1969 			NCE_REFRELE(nnce);
1970 			break;
1971 		default:
1972 			ip1dbg(("ndp_input_solicit: Can't create NCE %d\n",
1973 			    err));
1974 			goto done;
1975 		}
1976 no_source:
1977 		flag |= NDP_SOLICITED;
1978 	} else {
1979 		/*
1980 		 * No source link layer address option should be present in a
1981 		 * valid DAD request.
1982 		 */
1983 		if (haddr != NULL) {
1984 			ip1dbg(("ndp_input_solicit: source link-layer address "
1985 			    "option present with an unspecified source.\n"));
1986 			bad_solicit = B_TRUE;
1987 			goto done;
1988 		}
1989 		if (our_nce->nce_state == ND_PROBE) {
1990 			/*
1991 			 * Internally looped-back probes won't have DLPI
1992 			 * attached to them.  External ones (which are sent by
1993 			 * multicast) always will.  Just ignore our own
1994 			 * transmissions.
1995 			 */
1996 			if (dl_mp != NULL) {
1997 				/*
1998 				 * If someone else is probing our address, then
1999 				 * we've crossed wires.  Declare failure.
2000 				 */
2001 				ip_ndp_failure(ill, mp, dl_mp, our_nce);
2002 			}
2003 			goto done;
2004 		}
2005 		/*
2006 		 * This is a DAD probe.  Multicast the advertisement to the
2007 		 * all-nodes address.
2008 		 */
2009 		src = ipv6_all_hosts_mcast;
2010 	}
2011 	flag |= nce_advert_flags(our_nce);
2012 	/* Response to a solicitation */
2013 	(void) nce_xmit(ill,
2014 	    ND_NEIGHBOR_ADVERT,
2015 	    ill,	/* ill to be used for extracting ill_nd_lla */
2016 	    B_TRUE,	/* use ill_nd_lla */
2017 	    &target,	/* Source and target of the advertisement pkt */
2018 	    &src,	/* IP Destination (source of original pkt) */
2019 	    flag);
2020 done:
2021 	if (bad_solicit)
2022 		BUMP_MIB(mib, ipv6IfIcmpInBadNeighborSolicitations);
2023 	if (our_nce != NULL)
2024 		NCE_REFRELE(our_nce);
2025 }
2026 
2027 void
2028 ndp_input_advert(ill_t *ill, mblk_t *mp, mblk_t *dl_mp)
2029 {
2030 	nd_neighbor_advert_t *na;
2031 	uint32_t	hlen = ill->ill_nd_lla_len;
2032 	uchar_t		*haddr = NULL;
2033 	icmp6_t		*icmp_nd;
2034 	ip6_t		*ip6h;
2035 	nce_t		*dst_nce = NULL;
2036 	in6_addr_t	target;
2037 	nd_opt_hdr_t	*opt = NULL;
2038 	int		len;
2039 	mib2_ipv6IfIcmpEntry_t	*mib = ill->ill_icmp6_mib;
2040 
2041 	ip6h = (ip6_t *)mp->b_rptr;
2042 	icmp_nd = (icmp6_t *)(mp->b_rptr + IPV6_HDR_LEN);
2043 	len = mp->b_wptr - mp->b_rptr - IPV6_HDR_LEN;
2044 	na = (nd_neighbor_advert_t *)icmp_nd;
2045 	if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst) &&
2046 	    (na->nd_na_flags_reserved & ND_NA_FLAG_SOLICITED)) {
2047 		ip1dbg(("ndp_input_advert: Target is multicast but the "
2048 		    "solicited flag is not zero\n"));
2049 		BUMP_MIB(mib, ipv6IfIcmpInBadNeighborAdvertisements);
2050 		return;
2051 	}
2052 	target = na->nd_na_target;
2053 	if (IN6_IS_ADDR_MULTICAST(&target)) {
2054 		ip1dbg(("ndp_input_advert: Target is multicast!\n"));
2055 		BUMP_MIB(mib, ipv6IfIcmpInBadNeighborAdvertisements);
2056 		return;
2057 	}
2058 	if (len > sizeof (nd_neighbor_advert_t)) {
2059 		opt = (nd_opt_hdr_t *)&na[1];
2060 		if (!ndp_verify_optlen(opt,
2061 		    len - sizeof (nd_neighbor_advert_t))) {
2062 			ip1dbg(("ndp_input_advert: cannot verify SLLA\n"));
2063 			BUMP_MIB(mib, ipv6IfIcmpInBadNeighborAdvertisements);
2064 			return;
2065 		}
2066 		/* At this point we have a verified NA per spec */
2067 		len -= sizeof (nd_neighbor_advert_t);
2068 		opt = ndp_get_option(opt, len, ND_OPT_TARGET_LINKADDR);
2069 		if (opt != NULL) {
2070 			haddr = (uchar_t *)&opt[1];
2071 			if (hlen > opt->nd_opt_len * 8 - sizeof (*opt) ||
2072 			    hlen == 0) {
2073 				ip1dbg(("ndp_input_advert: bad SLLA\n"));
2074 				BUMP_MIB(mib,
2075 				    ipv6IfIcmpInBadNeighborAdvertisements);
2076 				return;
2077 			}
2078 		}
2079 	}
2080 
2081 	/*
2082 	 * If this interface is part of the group look at all the
2083 	 * ills in the group.
2084 	 */
2085 	rw_enter(&ill_g_lock, RW_READER);
2086 	if (ill->ill_group != NULL)
2087 		ill = ill->ill_group->illgrp_ill;
2088 
2089 	for (; ill != NULL; ill = ill->ill_group_next) {
2090 		mutex_enter(&ill->ill_lock);
2091 		if (!ILL_CAN_LOOKUP(ill)) {
2092 			mutex_exit(&ill->ill_lock);
2093 			continue;
2094 		}
2095 		ill_refhold_locked(ill);
2096 		mutex_exit(&ill->ill_lock);
2097 		dst_nce = ndp_lookup_v6(ill, &target, B_FALSE);
2098 		/* We have to drop the lock since ndp_process calls put* */
2099 		rw_exit(&ill_g_lock);
2100 		if (dst_nce != NULL) {
2101 			if ((dst_nce->nce_flags & NCE_F_PERMANENT) &&
2102 			    dst_nce->nce_state == ND_PROBE) {
2103 				/*
2104 				 * Someone else sent an advertisement for an
2105 				 * address that we're trying to configure.
2106 				 * Tear it down.  Note that dl_mp might be NULL
2107 				 * if we're getting a unicast reply.  This
2108 				 * isn't typically done (multicast is the norm
2109 				 * in response to a probe), but ip_ndp_failure
2110 				 * will handle the dl_mp == NULL case as well.
2111 				 */
2112 				ip_ndp_failure(ill, mp, dl_mp, dst_nce);
2113 			} else if (dst_nce->nce_flags & NCE_F_PERMANENT) {
2114 				/*
2115 				 * Someone just announced one of our local
2116 				 * addresses.  If it wasn't us, then this is a
2117 				 * conflict.  Defend the address or shut it
2118 				 * down.
2119 				 */
2120 				if (dl_mp != NULL &&
2121 				    (haddr == NULL ||
2122 				    nce_cmp_ll_addr(dst_nce, haddr,
2123 				    ill->ill_nd_lla_len))) {
2124 					ip_ndp_conflict(ill, mp, dl_mp,
2125 					    dst_nce);
2126 				}
2127 			} else {
2128 				if (na->nd_na_flags_reserved &
2129 				    ND_NA_FLAG_ROUTER) {
2130 					dst_nce->nce_flags |= NCE_F_ISROUTER;
2131 				}
2132 				/* B_TRUE indicates this an advertisement */
2133 				ndp_process(dst_nce, haddr,
2134 				    na->nd_na_flags_reserved, B_TRUE);
2135 			}
2136 			NCE_REFRELE(dst_nce);
2137 		}
2138 		rw_enter(&ill_g_lock, RW_READER);
2139 		ill_refrele(ill);
2140 	}
2141 	rw_exit(&ill_g_lock);
2142 }
2143 
2144 /*
2145  * Process NDP neighbor solicitation/advertisement messages.
2146  * The checksum has already checked o.k before reaching here.
2147  */
2148 void
2149 ndp_input(ill_t *ill, mblk_t *mp, mblk_t *dl_mp)
2150 {
2151 	icmp6_t		*icmp_nd;
2152 	ip6_t		*ip6h;
2153 	int		len;
2154 	mib2_ipv6IfIcmpEntry_t	*mib = ill->ill_icmp6_mib;
2155 
2156 
2157 	if (!pullupmsg(mp, -1)) {
2158 		ip1dbg(("ndp_input: pullupmsg failed\n"));
2159 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2160 		goto done;
2161 	}
2162 	ip6h = (ip6_t *)mp->b_rptr;
2163 	if (ip6h->ip6_hops != IPV6_MAX_HOPS) {
2164 		ip1dbg(("ndp_input: hoplimit != IPV6_MAX_HOPS\n"));
2165 		BUMP_MIB(mib, ipv6IfIcmpBadHoplimit);
2166 		goto done;
2167 	}
2168 	/*
2169 	 * NDP does not accept any extension headers between the
2170 	 * IP header and the ICMP header since e.g. a routing
2171 	 * header could be dangerous.
2172 	 * This assumes that any AH or ESP headers are removed
2173 	 * by ip prior to passing the packet to ndp_input.
2174 	 */
2175 	if (ip6h->ip6_nxt != IPPROTO_ICMPV6) {
2176 		ip1dbg(("ndp_input: Wrong next header 0x%x\n",
2177 		    ip6h->ip6_nxt));
2178 		BUMP_MIB(mib, ipv6IfIcmpInErrors);
2179 		goto done;
2180 	}
2181 	icmp_nd = (icmp6_t *)(mp->b_rptr + IPV6_HDR_LEN);
2182 	ASSERT(icmp_nd->icmp6_type == ND_NEIGHBOR_SOLICIT ||
2183 	    icmp_nd->icmp6_type == ND_NEIGHBOR_ADVERT);
2184 	if (icmp_nd->icmp6_code != 0) {
2185 		ip1dbg(("ndp_input: icmp6 code != 0 \n"));
2186 		BUMP_MIB(mib, ipv6IfIcmpInErrors);
2187 		goto done;
2188 	}
2189 	len = mp->b_wptr - mp->b_rptr - IPV6_HDR_LEN;
2190 	/*
2191 	 * Make sure packet length is large enough for either
2192 	 * a NS or a NA icmp packet.
2193 	 */
2194 	if (len <  sizeof (struct icmp6_hdr) + sizeof (struct in6_addr)) {
2195 		ip1dbg(("ndp_input: packet too short\n"));
2196 		BUMP_MIB(mib, ipv6IfIcmpInErrors);
2197 		goto done;
2198 	}
2199 	if (icmp_nd->icmp6_type == ND_NEIGHBOR_SOLICIT) {
2200 		ndp_input_solicit(ill, mp, dl_mp);
2201 	} else {
2202 		ndp_input_advert(ill, mp, dl_mp);
2203 	}
2204 done:
2205 	freemsg(mp);
2206 }
2207 
2208 /*
2209  * nce_xmit is called to form and transmit a ND solicitation or
2210  * advertisement ICMP packet.
2211  *
2212  * If the source address is unspecified and this isn't a probe (used for
2213  * duplicate address detection), an appropriate source address and link layer
2214  * address will be chosen here.  The link layer address option is included if
2215  * the source is specified (i.e., all non-probe packets), and omitted (per the
2216  * specification) otherwise.
2217  *
2218  * It returns B_FALSE only if it does a successful put() to the
2219  * corresponding ill's ill_wq otherwise returns B_TRUE.
2220  */
2221 static boolean_t
2222 nce_xmit(ill_t *ill, uint32_t operation, ill_t *hwaddr_ill,
2223     boolean_t use_nd_lla, const in6_addr_t *sender, const in6_addr_t *target,
2224     int flag)
2225 {
2226 	uint32_t	len;
2227 	icmp6_t 	*icmp6;
2228 	mblk_t		*mp;
2229 	ip6_t		*ip6h;
2230 	nd_opt_hdr_t	*opt;
2231 	uint_t		plen;
2232 	ip6i_t		*ip6i;
2233 	ipif_t		*src_ipif = NULL;
2234 	uint8_t		*hw_addr;
2235 
2236 	/*
2237 	 * If we have a unspecified source(sender) address, select a
2238 	 * proper source address for the solicitation here itself so
2239 	 * that we can initialize the h/w address correctly. This is
2240 	 * needed for interface groups as source address can come from
2241 	 * the whole group and the h/w address initialized from ill will
2242 	 * be wrong if the source address comes from a different ill.
2243 	 *
2244 	 * Note that the NA never comes here with the unspecified source
2245 	 * address. The following asserts that whenever the source
2246 	 * address is specified, the haddr also should be specified.
2247 	 */
2248 	ASSERT(IN6_IS_ADDR_UNSPECIFIED(sender) || (hwaddr_ill != NULL));
2249 
2250 	if (IN6_IS_ADDR_UNSPECIFIED(sender) && !(flag & NDP_PROBE)) {
2251 		ASSERT(operation != ND_NEIGHBOR_ADVERT);
2252 		/*
2253 		 * Pick a source address for this solicitation, but
2254 		 * restrict the selection to addresses assigned to the
2255 		 * output interface (or interface group).  We do this
2256 		 * because the destination will create a neighbor cache
2257 		 * entry for the source address of this packet, so the
2258 		 * source address had better be a valid neighbor.
2259 		 */
2260 		src_ipif = ipif_select_source_v6(ill, target, RESTRICT_TO_ILL,
2261 		    IPV6_PREFER_SRC_DEFAULT, GLOBAL_ZONEID);
2262 		if (src_ipif == NULL) {
2263 			char buf[INET6_ADDRSTRLEN];
2264 
2265 			ip1dbg(("nce_xmit: No source ipif for dst %s\n",
2266 			    inet_ntop(AF_INET6, (char *)target, buf,
2267 			    sizeof (buf))));
2268 			return (B_TRUE);
2269 		}
2270 		sender = &src_ipif->ipif_v6src_addr;
2271 		hwaddr_ill = src_ipif->ipif_ill;
2272 	}
2273 
2274 	/*
2275 	 * Always make sure that the NS/NA packets don't get load
2276 	 * spread. This is needed so that the probe packets sent
2277 	 * by the in.mpathd daemon can really go out on the desired
2278 	 * interface. Probe packets are made to go out on a desired
2279 	 * interface by including a ip6i with ATTACH_IF flag. As these
2280 	 * packets indirectly end up sending/receiving NS/NA packets
2281 	 * (neighbor doing NUD), we have to make sure that NA
2282 	 * also go out on the same interface.
2283 	 */
2284 	plen = (sizeof (nd_opt_hdr_t) + ill->ill_nd_lla_len + 7) / 8;
2285 	len = IPV6_HDR_LEN + sizeof (ip6i_t) + sizeof (nd_neighbor_advert_t) +
2286 	    plen * 8;
2287 	mp = allocb(len,  BPRI_LO);
2288 	if (mp == NULL) {
2289 		if (src_ipif != NULL)
2290 			ipif_refrele(src_ipif);
2291 		return (B_TRUE);
2292 	}
2293 	bzero((char *)mp->b_rptr, len);
2294 	mp->b_wptr = mp->b_rptr + len;
2295 
2296 	ip6i = (ip6i_t *)mp->b_rptr;
2297 	ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
2298 	ip6i->ip6i_nxt = IPPROTO_RAW;
2299 	ip6i->ip6i_flags = IP6I_ATTACH_IF | IP6I_HOPLIMIT;
2300 	if (flag & NDP_PROBE)
2301 		ip6i->ip6i_flags |= IP6I_UNSPEC_SRC;
2302 	ip6i->ip6i_ifindex = ill->ill_phyint->phyint_ifindex;
2303 
2304 	ip6h = (ip6_t *)(mp->b_rptr + sizeof (ip6i_t));
2305 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
2306 	ip6h->ip6_plen = htons(len - IPV6_HDR_LEN - sizeof (ip6i_t));
2307 	ip6h->ip6_nxt = IPPROTO_ICMPV6;
2308 	ip6h->ip6_hops = IPV6_MAX_HOPS;
2309 	ip6h->ip6_dst = *target;
2310 	icmp6 = (icmp6_t *)&ip6h[1];
2311 
2312 	opt = (nd_opt_hdr_t *)((uint8_t *)ip6h + IPV6_HDR_LEN +
2313 	    sizeof (nd_neighbor_advert_t));
2314 
2315 	if (operation == ND_NEIGHBOR_SOLICIT) {
2316 		nd_neighbor_solicit_t *ns = (nd_neighbor_solicit_t *)icmp6;
2317 
2318 		if (!(flag & NDP_PROBE))
2319 			opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
2320 		ip6h->ip6_src = *sender;
2321 		ns->nd_ns_target = *target;
2322 		if (!(flag & NDP_UNICAST)) {
2323 			/* Form multicast address of the target */
2324 			ip6h->ip6_dst = ipv6_solicited_node_mcast;
2325 			ip6h->ip6_dst.s6_addr32[3] |=
2326 			    ns->nd_ns_target.s6_addr32[3];
2327 		}
2328 	} else {
2329 		nd_neighbor_advert_t *na = (nd_neighbor_advert_t *)icmp6;
2330 
2331 		ASSERT(!(flag & NDP_PROBE));
2332 		opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2333 		ip6h->ip6_src = *sender;
2334 		na->nd_na_target = *sender;
2335 		if (flag & NDP_ISROUTER)
2336 			na->nd_na_flags_reserved |= ND_NA_FLAG_ROUTER;
2337 		if (flag & NDP_SOLICITED)
2338 			na->nd_na_flags_reserved |= ND_NA_FLAG_SOLICITED;
2339 		if (flag & NDP_ORIDE)
2340 			na->nd_na_flags_reserved |= ND_NA_FLAG_OVERRIDE;
2341 	}
2342 
2343 	hw_addr = NULL;
2344 	if (!(flag & NDP_PROBE)) {
2345 		mutex_enter(&hwaddr_ill->ill_lock);
2346 		hw_addr = use_nd_lla ? hwaddr_ill->ill_nd_lla :
2347 		    hwaddr_ill->ill_phys_addr;
2348 		if (hw_addr != NULL) {
2349 			/* Fill in link layer address and option len */
2350 			opt->nd_opt_len = (uint8_t)plen;
2351 			bcopy(hw_addr, &opt[1], hwaddr_ill->ill_nd_lla_len);
2352 		}
2353 		mutex_exit(&hwaddr_ill->ill_lock);
2354 	}
2355 	if (hw_addr == NULL) {
2356 		/* If there's no link layer address option, then strip it. */
2357 		len -= plen * 8;
2358 		mp->b_wptr = mp->b_rptr + len;
2359 		ip6h->ip6_plen = htons(len - IPV6_HDR_LEN - sizeof (ip6i_t));
2360 	}
2361 
2362 	icmp6->icmp6_type = (uint8_t)operation;
2363 	icmp6->icmp6_code = 0;
2364 	/*
2365 	 * Prepare for checksum by putting icmp length in the icmp
2366 	 * checksum field. The checksum is calculated in ip_wput_v6.
2367 	 */
2368 	icmp6->icmp6_cksum = ip6h->ip6_plen;
2369 
2370 	if (src_ipif != NULL)
2371 		ipif_refrele(src_ipif);
2372 	if (canput(ill->ill_wq)) {
2373 		put(ill->ill_wq, mp);
2374 		return (B_FALSE);
2375 	}
2376 	freemsg(mp);
2377 	return (B_TRUE);
2378 }
2379 
2380 /*
2381  * Make a link layer address (does not include the SAP) from an nce.
2382  * To form the link layer address, use the last four bytes of ipv6
2383  * address passed in and the fixed offset stored in nce.
2384  */
2385 static void
2386 nce_make_mapping(nce_t *nce, uchar_t *addrpos, uchar_t *addr)
2387 {
2388 	uchar_t *mask, *to;
2389 	ill_t	*ill = nce->nce_ill;
2390 	int 	len;
2391 
2392 	if (ill->ill_net_type == IRE_IF_NORESOLVER)
2393 		return;
2394 	ASSERT(nce->nce_res_mp != NULL);
2395 	ASSERT(ill->ill_net_type == IRE_IF_RESOLVER);
2396 	ASSERT(nce->nce_flags & NCE_F_MAPPING);
2397 	ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&nce->nce_extract_mask));
2398 	ASSERT(addr != NULL);
2399 	bcopy(nce->nce_res_mp->b_rptr + NCE_LL_ADDR_OFFSET(ill),
2400 	    addrpos, ill->ill_nd_lla_len);
2401 	len = MIN((int)ill->ill_nd_lla_len - nce->nce_ll_extract_start,
2402 	    IPV6_ADDR_LEN);
2403 	mask = (uchar_t *)&nce->nce_extract_mask;
2404 	mask += (IPV6_ADDR_LEN - len);
2405 	addr += (IPV6_ADDR_LEN - len);
2406 	to = addrpos + nce->nce_ll_extract_start;
2407 	while (len-- > 0)
2408 		*to++ |= *mask++ & *addr++;
2409 }
2410 
2411 /*
2412  * Pass a cache report back out via NDD.
2413  */
2414 /* ARGSUSED */
2415 int
2416 ndp_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr)
2417 {
2418 	(void) mi_mpprintf(mp, "ifname      hardware addr    flags"
2419 			"     proto addr/mask");
2420 	ndp_walk(NULL, (pfi_t)nce_report1, (uchar_t *)mp);
2421 	return (0);
2422 }
2423 
2424 /*
2425  * Add a single line to the NDP Cache Entry Report.
2426  */
2427 static void
2428 nce_report1(nce_t *nce, uchar_t *mp_arg)
2429 {
2430 	ill_t		*ill = nce->nce_ill;
2431 	char		local_buf[INET6_ADDRSTRLEN];
2432 	uchar_t		flags_buf[10];
2433 	uint32_t	flags = nce->nce_flags;
2434 	mblk_t		*mp = (mblk_t *)mp_arg;
2435 	uchar_t		*h;
2436 	uchar_t		*m = flags_buf;
2437 	in6_addr_t	v6addr;
2438 
2439 	/*
2440 	 * Lock the nce to protect nce_res_mp from being changed
2441 	 * if an external resolver address resolution completes
2442 	 * while nce_res_mp is being accessed here.
2443 	 *
2444 	 * Deal with all address formats, not just Ethernet-specific
2445 	 * In addition, make sure that the mblk has enough space
2446 	 * before writing to it. If is doesn't, allocate a new one.
2447 	 */
2448 	if (nce->nce_ipversion == IPV4_VERSION)
2449 		/* Don't include v4 nce_ts in NDP cache entry report */
2450 		return;
2451 
2452 	ASSERT(ill != NULL);
2453 	v6addr = nce->nce_mask;
2454 	if (flags & NCE_F_PERMANENT)
2455 		*m++ = 'P';
2456 	if (flags & NCE_F_ISROUTER)
2457 		*m++ = 'R';
2458 	if (flags & NCE_F_MAPPING)
2459 		*m++ = 'M';
2460 	*m = '\0';
2461 
2462 	if (ill->ill_net_type == IRE_IF_RESOLVER) {
2463 		size_t		addrlen;
2464 		char		*addr_buf;
2465 		dl_unitdata_req_t	*dl;
2466 
2467 		mutex_enter(&nce->nce_lock);
2468 		h = nce->nce_res_mp->b_rptr + NCE_LL_ADDR_OFFSET(ill);
2469 		dl = (dl_unitdata_req_t *)nce->nce_res_mp->b_rptr;
2470 		if (ill->ill_flags & ILLF_XRESOLV)
2471 			addrlen = (3 * (dl->dl_dest_addr_length));
2472 		else
2473 			addrlen = (3 * (ill->ill_nd_lla_len));
2474 		if (addrlen <= 0) {
2475 			mutex_exit(&nce->nce_lock);
2476 			(void) mi_mpprintf(mp,
2477 			    "%8s %9s %5s %s/%d",
2478 			    ill->ill_name,
2479 			    "None",
2480 			    (uchar_t *)&flags_buf,
2481 			    inet_ntop(AF_INET6, (char *)&nce->nce_addr,
2482 				(char *)local_buf, sizeof (local_buf)),
2483 				ip_mask_to_plen_v6(&v6addr));
2484 		} else {
2485 			/*
2486 			 * Convert the hardware/lla address to ascii
2487 			 */
2488 			addr_buf = kmem_zalloc(addrlen, KM_NOSLEEP);
2489 			if (addr_buf == NULL) {
2490 				mutex_exit(&nce->nce_lock);
2491 				return;
2492 			}
2493 			(void) mac_colon_addr((uint8_t *)h,
2494 			    (ill->ill_flags & ILLF_XRESOLV) ?
2495 			    dl->dl_dest_addr_length : ill->ill_nd_lla_len,
2496 			    addr_buf, addrlen);
2497 			mutex_exit(&nce->nce_lock);
2498 			(void) mi_mpprintf(mp, "%8s %17s %5s %s/%d",
2499 			    ill->ill_name, addr_buf, (uchar_t *)&flags_buf,
2500 			    inet_ntop(AF_INET6, (char *)&nce->nce_addr,
2501 				(char *)local_buf, sizeof (local_buf)),
2502 				ip_mask_to_plen_v6(&v6addr));
2503 			kmem_free(addr_buf, addrlen);
2504 		}
2505 	} else {
2506 		(void) mi_mpprintf(mp,
2507 		    "%8s %9s %5s %s/%d",
2508 		    ill->ill_name,
2509 		    "None",
2510 		    (uchar_t *)&flags_buf,
2511 		    inet_ntop(AF_INET6, (char *)&nce->nce_addr,
2512 			(char *)local_buf, sizeof (local_buf)),
2513 			ip_mask_to_plen_v6(&v6addr));
2514 	}
2515 }
2516 
2517 mblk_t *
2518 nce_udreq_alloc(ill_t *ill)
2519 {
2520 	mblk_t	*template_mp = NULL;
2521 	dl_unitdata_req_t *dlur;
2522 	int	sap_length;
2523 
2524 	ASSERT(ill->ill_isv6);
2525 
2526 	sap_length = ill->ill_sap_length;
2527 	template_mp = ip_dlpi_alloc(sizeof (dl_unitdata_req_t) +
2528 	    ill->ill_nd_lla_len + ABS(sap_length), DL_UNITDATA_REQ);
2529 	if (template_mp == NULL)
2530 		return (NULL);
2531 
2532 	dlur = (dl_unitdata_req_t *)template_mp->b_rptr;
2533 	dlur->dl_priority.dl_min = 0;
2534 	dlur->dl_priority.dl_max = 0;
2535 	dlur->dl_dest_addr_length = ABS(sap_length) + ill->ill_nd_lla_len;
2536 	dlur->dl_dest_addr_offset = sizeof (dl_unitdata_req_t);
2537 
2538 	/* Copy in the SAP value. */
2539 	NCE_LL_SAP_COPY(ill, template_mp);
2540 
2541 	return (template_mp);
2542 }
2543 
2544 /*
2545  * NDP retransmit timer.
2546  * This timer goes off when:
2547  * a. It is time to retransmit NS for resolver.
2548  * b. It is time to send reachability probes.
2549  */
2550 void
2551 ndp_timer(void *arg)
2552 {
2553 	nce_t		*nce = arg;
2554 	ill_t		*ill = nce->nce_ill;
2555 	uint32_t	ms;
2556 	char		addrbuf[INET6_ADDRSTRLEN];
2557 	mblk_t		*mp;
2558 	boolean_t	dropped = B_FALSE;
2559 
2560 	/*
2561 	 * The timer has to be cancelled by ndp_delete before doing the final
2562 	 * refrele. So the NCE is guaranteed to exist when the timer runs
2563 	 * until it clears the timeout_id. Before clearing the timeout_id
2564 	 * bump up the refcnt so that we can continue to use the nce
2565 	 */
2566 	ASSERT(nce != NULL);
2567 
2568 	/*
2569 	 * Grab the ill_g_lock now itself to avoid lock order problems.
2570 	 * nce_solicit needs ill_g_lock to be able to traverse ills
2571 	 */
2572 	rw_enter(&ill_g_lock, RW_READER);
2573 	mutex_enter(&nce->nce_lock);
2574 	NCE_REFHOLD_LOCKED(nce);
2575 	nce->nce_timeout_id = 0;
2576 
2577 	/*
2578 	 * Check the reachability state first.
2579 	 */
2580 	switch (nce->nce_state) {
2581 	case ND_DELAY:
2582 		rw_exit(&ill_g_lock);
2583 		nce->nce_state = ND_PROBE;
2584 		mutex_exit(&nce->nce_lock);
2585 		(void) nce_xmit(ill, ND_NEIGHBOR_SOLICIT, NULL, B_FALSE,
2586 		    &ipv6_all_zeros, &nce->nce_addr, NDP_UNICAST);
2587 		if (ip_debug > 3) {
2588 			/* ip2dbg */
2589 			pr_addr_dbg("ndp_timer: state for %s changed "
2590 			    "to PROBE\n", AF_INET6, &nce->nce_addr);
2591 		}
2592 		NDP_RESTART_TIMER(nce, ill->ill_reachable_retrans_time);
2593 		NCE_REFRELE(nce);
2594 		return;
2595 	case ND_PROBE:
2596 		/* must be retransmit timer */
2597 		rw_exit(&ill_g_lock);
2598 		nce->nce_pcnt--;
2599 		ASSERT(nce->nce_pcnt < ND_MAX_UNICAST_SOLICIT &&
2600 		    nce->nce_pcnt >= -1);
2601 		if (nce->nce_pcnt > 0) {
2602 			/*
2603 			 * As per RFC2461, the nce gets deleted after
2604 			 * MAX_UNICAST_SOLICIT unsuccessful re-transmissions.
2605 			 * Note that the first unicast solicitation is sent
2606 			 * during the DELAY state.
2607 			 */
2608 			ip2dbg(("ndp_timer: pcount=%x dst %s\n",
2609 			    nce->nce_pcnt, inet_ntop(AF_INET6, &nce->nce_addr,
2610 			    addrbuf, sizeof (addrbuf))));
2611 			mutex_exit(&nce->nce_lock);
2612 			dropped = nce_xmit(ill, ND_NEIGHBOR_SOLICIT, NULL,
2613 			    B_FALSE, &ipv6_all_zeros, &nce->nce_addr,
2614 			    (nce->nce_flags & NCE_F_PERMANENT) ? NDP_PROBE :
2615 			    NDP_UNICAST);
2616 			if (dropped) {
2617 				mutex_enter(&nce->nce_lock);
2618 				nce->nce_pcnt++;
2619 				mutex_exit(&nce->nce_lock);
2620 			}
2621 			NDP_RESTART_TIMER(nce, ILL_PROBE_INTERVAL(ill));
2622 		} else if (nce->nce_pcnt < 0) {
2623 			/* No hope, delete the nce */
2624 			nce->nce_state = ND_UNREACHABLE;
2625 			mutex_exit(&nce->nce_lock);
2626 			if (ip_debug > 2) {
2627 				/* ip1dbg */
2628 				pr_addr_dbg("ndp_timer: Delete IRE for"
2629 				    " dst %s\n", AF_INET6, &nce->nce_addr);
2630 			}
2631 			ndp_delete(nce);
2632 		} else if (!(nce->nce_flags & NCE_F_PERMANENT)) {
2633 			/* Wait RetransTimer, before deleting the entry */
2634 			ip2dbg(("ndp_timer: pcount=%x dst %s\n",
2635 			    nce->nce_pcnt, inet_ntop(AF_INET6,
2636 			    &nce->nce_addr, addrbuf, sizeof (addrbuf))));
2637 			mutex_exit(&nce->nce_lock);
2638 			/* Wait one interval before killing */
2639 			NDP_RESTART_TIMER(nce, ill->ill_reachable_retrans_time);
2640 		} else if (ill->ill_phyint->phyint_flags & PHYI_RUNNING) {
2641 			ipif_t *ipif;
2642 
2643 			/*
2644 			 * We're done probing, and we can now declare this
2645 			 * address to be usable.  Let IP know that it's ok to
2646 			 * use.
2647 			 */
2648 			nce->nce_state = ND_REACHABLE;
2649 			mutex_exit(&nce->nce_lock);
2650 			ipif = ipif_lookup_addr_v6(&nce->nce_addr, ill,
2651 			    ALL_ZONES, NULL, NULL, NULL, NULL);
2652 			if (ipif != NULL) {
2653 				if (ipif->ipif_was_dup) {
2654 					char ibuf[LIFNAMSIZ + 10];
2655 					char sbuf[INET6_ADDRSTRLEN];
2656 
2657 					ipif->ipif_was_dup = B_FALSE;
2658 					(void) strlcpy(ibuf, ill->ill_name,
2659 					    sizeof (ibuf));
2660 					(void) inet_ntop(AF_INET6,
2661 					    &ipif->ipif_v6lcl_addr,
2662 					    sbuf, sizeof (sbuf));
2663 					if (ipif->ipif_id != 0) {
2664 						(void) snprintf(ibuf +
2665 						    ill->ill_name_length - 1,
2666 						    sizeof (ibuf) -
2667 						    ill->ill_name_length + 1,
2668 						    ":%d", ipif->ipif_id);
2669 					}
2670 					cmn_err(CE_NOTE, "recovered address "
2671 					    "%s on %s", sbuf, ibuf);
2672 				}
2673 				if ((ipif->ipif_flags & IPIF_UP) &&
2674 				    !ipif->ipif_addr_ready) {
2675 					ip_rts_ifmsg(ipif);
2676 					ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
2677 					sctp_update_ipif(ipif, SCTP_IPIF_UP);
2678 				}
2679 				ipif->ipif_addr_ready = 1;
2680 				ipif_refrele(ipif);
2681 			}
2682 			/* Begin defending our new address */
2683 			nce->nce_unsolicit_count = 0;
2684 			dropped = nce_xmit(ill, ND_NEIGHBOR_ADVERT, ill,
2685 			    B_FALSE, &nce->nce_addr, &ipv6_all_hosts_mcast,
2686 			    nce_advert_flags(nce));
2687 			if (dropped) {
2688 				nce->nce_unsolicit_count = 1;
2689 				NDP_RESTART_TIMER(nce,
2690 				    ip_ndp_unsolicit_interval);
2691 			} else if (ip_ndp_defense_interval != 0) {
2692 				NDP_RESTART_TIMER(nce, ip_ndp_defense_interval);
2693 			}
2694 		} else {
2695 			/*
2696 			 * This is an address we're probing to be our own, but
2697 			 * the ill is down.  Wait until it comes back before
2698 			 * doing anything, but switch to reachable state so
2699 			 * that the restart will work.
2700 			 */
2701 			nce->nce_state = ND_REACHABLE;
2702 			mutex_exit(&nce->nce_lock);
2703 		}
2704 		NCE_REFRELE(nce);
2705 		return;
2706 	case ND_INCOMPLETE:
2707 		/*
2708 		 * Must be resolvers retransmit timer.
2709 		 */
2710 		for (mp = nce->nce_qd_mp; mp != NULL; mp = mp->b_next) {
2711 			ip6i_t	*ip6i;
2712 			ip6_t	*ip6h;
2713 			mblk_t *data_mp;
2714 
2715 			/*
2716 			 * Walk the list of packets queued, and see if there
2717 			 * are any multipathing probe packets. Such packets
2718 			 * are always queued at the head. Since this is a
2719 			 * retransmit timer firing, mark such packets as
2720 			 * delayed in ND resolution. This info will be used
2721 			 * in ip_wput_v6(). Multipathing probe packets will
2722 			 * always have an ip6i_t. Once we hit a packet without
2723 			 * it, we can break out of this loop.
2724 			 */
2725 			if (mp->b_datap->db_type == M_CTL)
2726 				data_mp = mp->b_cont;
2727 			else
2728 				data_mp = mp;
2729 
2730 			ip6h = (ip6_t *)data_mp->b_rptr;
2731 			if (ip6h->ip6_nxt != IPPROTO_RAW)
2732 				break;
2733 
2734 			/*
2735 			 * This message should have been pulled up already in
2736 			 * ip_wput_v6. We can't do pullups here because the
2737 			 * b_next/b_prev is non-NULL.
2738 			 */
2739 			ip6i = (ip6i_t *)ip6h;
2740 			ASSERT((data_mp->b_wptr - (uchar_t *)ip6i) >=
2741 			    sizeof (ip6i_t) + IPV6_HDR_LEN);
2742 
2743 			/* Mark this packet as delayed due to ND resolution */
2744 			if (ip6i->ip6i_flags & IP6I_DROP_IFDELAYED)
2745 				ip6i->ip6i_flags |= IP6I_ND_DELAYED;
2746 		}
2747 		if (nce->nce_qd_mp != NULL) {
2748 			ms = nce_solicit(nce, NULL);
2749 			rw_exit(&ill_g_lock);
2750 			if (ms == 0) {
2751 				if (nce->nce_state != ND_REACHABLE) {
2752 					mutex_exit(&nce->nce_lock);
2753 					nce_resolv_failed(nce);
2754 					ndp_delete(nce);
2755 				} else {
2756 					mutex_exit(&nce->nce_lock);
2757 				}
2758 			} else {
2759 				mutex_exit(&nce->nce_lock);
2760 				NDP_RESTART_TIMER(nce, (clock_t)ms);
2761 			}
2762 			NCE_REFRELE(nce);
2763 			return;
2764 		}
2765 		mutex_exit(&nce->nce_lock);
2766 		rw_exit(&ill_g_lock);
2767 		NCE_REFRELE(nce);
2768 		break;
2769 	case ND_REACHABLE :
2770 		rw_exit(&ill_g_lock);
2771 		if (((nce->nce_flags & NCE_F_UNSOL_ADV) &&
2772 		    nce->nce_unsolicit_count != 0) ||
2773 		    ((nce->nce_flags & NCE_F_PERMANENT) &&
2774 		    ip_ndp_defense_interval != 0)) {
2775 			if (nce->nce_unsolicit_count > 0)
2776 				nce->nce_unsolicit_count--;
2777 			mutex_exit(&nce->nce_lock);
2778 			dropped = nce_xmit(ill,
2779 			    ND_NEIGHBOR_ADVERT,
2780 			    ill,	/* ill to be used for hw addr */
2781 			    B_FALSE,	/* use ill_phys_addr */
2782 			    &nce->nce_addr,
2783 			    &ipv6_all_hosts_mcast,
2784 			    nce_advert_flags(nce));
2785 			if (dropped) {
2786 				mutex_enter(&nce->nce_lock);
2787 				nce->nce_unsolicit_count++;
2788 				mutex_exit(&nce->nce_lock);
2789 			}
2790 			if (nce->nce_unsolicit_count != 0) {
2791 				NDP_RESTART_TIMER(nce,
2792 				    ip_ndp_unsolicit_interval);
2793 			} else {
2794 				NDP_RESTART_TIMER(nce,
2795 				    ip_ndp_defense_interval);
2796 			}
2797 		} else {
2798 			mutex_exit(&nce->nce_lock);
2799 		}
2800 		NCE_REFRELE(nce);
2801 		break;
2802 	default:
2803 		rw_exit(&ill_g_lock);
2804 		mutex_exit(&nce->nce_lock);
2805 		NCE_REFRELE(nce);
2806 		break;
2807 	}
2808 }
2809 
2810 /*
2811  * Set a link layer address from the ll_addr passed in.
2812  * Copy SAP from ill.
2813  */
2814 static void
2815 nce_set_ll(nce_t *nce, uchar_t *ll_addr)
2816 {
2817 	ill_t	*ill = nce->nce_ill;
2818 	uchar_t	*woffset;
2819 
2820 	ASSERT(ll_addr != NULL);
2821 	/* Always called before fast_path_probe */
2822 	ASSERT(nce->nce_fp_mp == NULL);
2823 	if (ill->ill_sap_length != 0) {
2824 		/*
2825 		 * Copy the SAP type specified in the
2826 		 * request into the xmit template.
2827 		 */
2828 		NCE_LL_SAP_COPY(ill, nce->nce_res_mp);
2829 	}
2830 	if (ill->ill_phys_addr_length > 0) {
2831 		/*
2832 		 * The bcopy() below used to be called for the physical address
2833 		 * length rather than the link layer address length. For
2834 		 * ethernet and many other media, the phys_addr and lla are
2835 		 * identical.
2836 		 * However, with xresolv interfaces being introduced, the
2837 		 * phys_addr and lla are no longer the same, and the physical
2838 		 * address may not have any useful meaning, so we use the lla
2839 		 * for IPv6 address resolution and destination addressing.
2840 		 *
2841 		 * For PPP or other interfaces with a zero length
2842 		 * physical address, don't do anything here.
2843 		 * The bcopy() with a zero phys_addr length was previously
2844 		 * a no-op for interfaces with a zero-length physical address.
2845 		 * Using the lla for them would change the way they operate.
2846 		 * Doing nothing in such cases preserves expected behavior.
2847 		 */
2848 		woffset = nce->nce_res_mp->b_rptr + NCE_LL_ADDR_OFFSET(ill);
2849 		bcopy(ll_addr, woffset, ill->ill_nd_lla_len);
2850 	}
2851 }
2852 
2853 static boolean_t
2854 nce_cmp_ll_addr(const nce_t *nce, const uchar_t *ll_addr, uint32_t ll_addr_len)
2855 {
2856 	ill_t	*ill = nce->nce_ill;
2857 	uchar_t	*ll_offset;
2858 
2859 	ASSERT(nce->nce_res_mp != NULL);
2860 	if (ll_addr == NULL)
2861 		return (B_FALSE);
2862 	ll_offset = nce->nce_res_mp->b_rptr + NCE_LL_ADDR_OFFSET(ill);
2863 	if (bcmp(ll_addr, ll_offset, ll_addr_len) != 0)
2864 		return (B_TRUE);
2865 	return (B_FALSE);
2866 }
2867 
2868 /*
2869  * Updates the link layer address or the reachability state of
2870  * a cache entry.  Reset probe counter if needed.
2871  */
2872 static void
2873 nce_update(nce_t *nce, uint16_t new_state, uchar_t *new_ll_addr)
2874 {
2875 	ill_t	*ill = nce->nce_ill;
2876 	boolean_t need_stop_timer = B_FALSE;
2877 	boolean_t need_fastpath_update = B_FALSE;
2878 
2879 	ASSERT(MUTEX_HELD(&nce->nce_lock));
2880 	ASSERT(nce->nce_ipversion == IPV6_VERSION);
2881 	/*
2882 	 * If this interface does not do NUD, there is no point
2883 	 * in allowing an update to the cache entry.  Although
2884 	 * we will respond to NS.
2885 	 * The only time we accept an update for a resolver when
2886 	 * NUD is turned off is when it has just been created.
2887 	 * Non-Resolvers will always be created as REACHABLE.
2888 	 */
2889 	if (new_state != ND_UNCHANGED) {
2890 		if ((nce->nce_flags & NCE_F_NONUD) &&
2891 		    (nce->nce_state != ND_INCOMPLETE))
2892 			return;
2893 		ASSERT((int16_t)new_state >= ND_STATE_VALID_MIN);
2894 		ASSERT((int16_t)new_state <= ND_STATE_VALID_MAX);
2895 		need_stop_timer = B_TRUE;
2896 		if (new_state == ND_REACHABLE)
2897 			nce->nce_last = TICK_TO_MSEC(lbolt64);
2898 		else {
2899 			/* We force NUD in this case */
2900 			nce->nce_last = 0;
2901 		}
2902 		nce->nce_state = new_state;
2903 		nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT;
2904 	}
2905 	/*
2906 	 * In case of fast path we need to free the the fastpath
2907 	 * M_DATA and do another probe.  Otherwise we can just
2908 	 * overwrite the DL_UNITDATA_REQ data, noting we'll lose
2909 	 * whatever packets that happens to be transmitting at the time.
2910 	 */
2911 	if (new_ll_addr != NULL) {
2912 		ASSERT(nce->nce_res_mp->b_rptr + NCE_LL_ADDR_OFFSET(ill) +
2913 		    ill->ill_nd_lla_len <= nce->nce_res_mp->b_wptr);
2914 		bcopy(new_ll_addr, nce->nce_res_mp->b_rptr +
2915 		    NCE_LL_ADDR_OFFSET(ill), ill->ill_nd_lla_len);
2916 		if (nce->nce_fp_mp != NULL) {
2917 			freemsg(nce->nce_fp_mp);
2918 			nce->nce_fp_mp = NULL;
2919 		}
2920 		need_fastpath_update = B_TRUE;
2921 	}
2922 	mutex_exit(&nce->nce_lock);
2923 	if (need_stop_timer) {
2924 		(void) untimeout(nce->nce_timeout_id);
2925 		nce->nce_timeout_id = 0;
2926 	}
2927 	if (need_fastpath_update)
2928 		nce_fastpath(nce);
2929 	mutex_enter(&nce->nce_lock);
2930 }
2931 
2932 void
2933 nce_queue_mp_common(nce_t *nce, mblk_t *mp, boolean_t head_insert)
2934 {
2935 	uint_t	count = 0;
2936 	mblk_t  **mpp;
2937 
2938 	ASSERT(MUTEX_HELD(&nce->nce_lock));
2939 
2940 	for (mpp = &nce->nce_qd_mp; *mpp != NULL;
2941 	    mpp = &(*mpp)->b_next) {
2942 		if (++count >
2943 		    nce->nce_ill->ill_max_buf) {
2944 			mblk_t *tmp = nce->nce_qd_mp->b_next;
2945 
2946 			nce->nce_qd_mp->b_next = NULL;
2947 			nce->nce_qd_mp->b_prev = NULL;
2948 			freemsg(nce->nce_qd_mp);
2949 			nce->nce_qd_mp = tmp;
2950 		}
2951 	}
2952 	/* put this on the list */
2953 	if (head_insert) {
2954 		mp->b_next = nce->nce_qd_mp;
2955 		nce->nce_qd_mp = mp;
2956 	} else {
2957 		*mpp = mp;
2958 	}
2959 }
2960 
2961 static void
2962 nce_queue_mp(nce_t *nce, mblk_t *mp)
2963 {
2964 	boolean_t head_insert = B_FALSE;
2965 	ip6_t	*ip6h;
2966 	ip6i_t	*ip6i;
2967 	mblk_t *data_mp;
2968 
2969 	ASSERT(MUTEX_HELD(&nce->nce_lock));
2970 
2971 	if (mp->b_datap->db_type == M_CTL)
2972 		data_mp = mp->b_cont;
2973 	else
2974 		data_mp = mp;
2975 	ip6h = (ip6_t *)data_mp->b_rptr;
2976 	if (ip6h->ip6_nxt == IPPROTO_RAW) {
2977 		/*
2978 		 * This message should have been pulled up already in
2979 		 * ip_wput_v6. We can't do pullups here because the message
2980 		 * could be from the nce_qd_mp which could have b_next/b_prev
2981 		 * non-NULL.
2982 		 */
2983 		ip6i = (ip6i_t *)ip6h;
2984 		ASSERT((data_mp->b_wptr - (uchar_t *)ip6i) >=
2985 			    sizeof (ip6i_t) + IPV6_HDR_LEN);
2986 		/*
2987 		 * Multipathing probe packets have IP6I_DROP_IFDELAYED set.
2988 		 * This has 2 aspects mentioned below.
2989 		 * 1. Perform head insertion in the nce_qd_mp for these packets.
2990 		 * This ensures that next retransmit of ND solicitation
2991 		 * will use the interface specified by the probe packet,
2992 		 * for both NS and NA. This corresponds to the src address
2993 		 * in the IPv6 packet. If we insert at tail, we will be
2994 		 * depending on the packet at the head for successful
2995 		 * ND resolution. This is not reliable, because the interface
2996 		 * on which the NA arrives could be different from the interface
2997 		 * on which the NS was sent, and if the receiving interface is
2998 		 * failed, it will appear that the sending interface is also
2999 		 * failed, causing in.mpathd to misdiagnose this as link
3000 		 * failure.
3001 		 * 2. Drop the original packet, if the ND resolution did not
3002 		 * succeed in the first attempt. However we will create the
3003 		 * nce and the ire, as soon as the ND resolution succeeds.
3004 		 * We don't gain anything by queueing multiple probe packets
3005 		 * and sending them back-to-back once resolution succeeds.
3006 		 * It is sufficient to send just 1 packet after ND resolution
3007 		 * succeeds. Since mpathd is sending down probe packets at a
3008 		 * constant rate, we don't need to send the queued packet. We
3009 		 * need to queue it only for NDP resolution. The benefit of
3010 		 * dropping the probe packets that were delayed in ND
3011 		 * resolution, is that in.mpathd will not see inflated
3012 		 * RTT. If the ND resolution does not succeed within
3013 		 * in.mpathd's failure detection time, mpathd may detect
3014 		 * a failure, and it does not matter whether the packet
3015 		 * was queued or dropped.
3016 		 */
3017 		if (ip6i->ip6i_flags & IP6I_DROP_IFDELAYED)
3018 			head_insert = B_TRUE;
3019 	}
3020 
3021 	nce_queue_mp_common(nce, mp, head_insert);
3022 }
3023 
3024 /*
3025  * Called when address resolution failed due to a timeout.
3026  * Send an ICMP unreachable in response to all queued packets.
3027  */
3028 void
3029 nce_resolv_failed(nce_t *nce)
3030 {
3031 	mblk_t	*mp, *nxt_mp, *first_mp;
3032 	char	buf[INET6_ADDRSTRLEN];
3033 	ip6_t *ip6h;
3034 	zoneid_t zoneid = GLOBAL_ZONEID;
3035 
3036 	ip1dbg(("nce_resolv_failed: dst %s\n",
3037 	    inet_ntop(AF_INET6, (char *)&nce->nce_addr, buf, sizeof (buf))));
3038 	mutex_enter(&nce->nce_lock);
3039 	mp = nce->nce_qd_mp;
3040 	nce->nce_qd_mp = NULL;
3041 	mutex_exit(&nce->nce_lock);
3042 	while (mp != NULL) {
3043 		nxt_mp = mp->b_next;
3044 		mp->b_next = NULL;
3045 		mp->b_prev = NULL;
3046 
3047 		first_mp = mp;
3048 		if (mp->b_datap->db_type == M_CTL) {
3049 			ipsec_out_t *io = (ipsec_out_t *)mp->b_rptr;
3050 			ASSERT(io->ipsec_out_type == IPSEC_OUT);
3051 			zoneid = io->ipsec_out_zoneid;
3052 			ASSERT(zoneid != ALL_ZONES);
3053 			mp = mp->b_cont;
3054 		}
3055 
3056 		ip6h = (ip6_t *)mp->b_rptr;
3057 		if (ip6h->ip6_nxt == IPPROTO_RAW) {
3058 			ip6i_t *ip6i;
3059 			/*
3060 			 * This message should have been pulled up already
3061 			 * in ip_wput_v6. ip_hdr_complete_v6 assumes that
3062 			 * the header is pulled up.
3063 			 */
3064 			ip6i = (ip6i_t *)ip6h;
3065 			ASSERT((mp->b_wptr - (uchar_t *)ip6i) >=
3066 			    sizeof (ip6i_t) + IPV6_HDR_LEN);
3067 			mp->b_rptr += sizeof (ip6i_t);
3068 		}
3069 		/*
3070 		 * Ignore failure since icmp_unreachable_v6 will silently
3071 		 * drop packets with an unspecified source address.
3072 		 */
3073 		(void) ip_hdr_complete_v6((ip6_t *)mp->b_rptr, zoneid);
3074 		icmp_unreachable_v6(nce->nce_ill->ill_wq, first_mp,
3075 		    ICMP6_DST_UNREACH_ADDR, B_FALSE, B_FALSE, zoneid);
3076 		mp = nxt_mp;
3077 	}
3078 }
3079 
3080 /*
3081  * Called by SIOCSNDP* ioctl to add/change an nce entry
3082  * and the corresponding attributes.
3083  * Disallow states other than ND_REACHABLE or ND_STALE.
3084  */
3085 int
3086 ndp_sioc_update(ill_t *ill, lif_nd_req_t *lnr)
3087 {
3088 	sin6_t		*sin6;
3089 	in6_addr_t	*addr;
3090 	nce_t		*nce;
3091 	int		err;
3092 	uint16_t	new_flags = 0;
3093 	uint16_t	old_flags = 0;
3094 	int		inflags = lnr->lnr_flags;
3095 
3096 	ASSERT(ill->ill_isv6);
3097 	if ((lnr->lnr_state_create != ND_REACHABLE) &&
3098 	    (lnr->lnr_state_create != ND_STALE))
3099 		return (EINVAL);
3100 
3101 	sin6 = (sin6_t *)&lnr->lnr_addr;
3102 	addr = &sin6->sin6_addr;
3103 
3104 	mutex_enter(&ndp6.ndp_g_lock);
3105 	/* We know it can not be mapping so just look in the hash table */
3106 	nce = *((nce_t **)NCE_HASH_PTR_V6(*addr));
3107 	nce = nce_lookup_addr(ill, addr, nce);
3108 	if (nce != NULL)
3109 		new_flags = nce->nce_flags;
3110 
3111 	switch (inflags & (NDF_ISROUTER_ON|NDF_ISROUTER_OFF)) {
3112 	case NDF_ISROUTER_ON:
3113 		new_flags |= NCE_F_ISROUTER;
3114 		break;
3115 	case NDF_ISROUTER_OFF:
3116 		new_flags &= ~NCE_F_ISROUTER;
3117 		break;
3118 	case (NDF_ISROUTER_OFF|NDF_ISROUTER_ON):
3119 		mutex_exit(&ndp6.ndp_g_lock);
3120 		if (nce != NULL)
3121 			NCE_REFRELE(nce);
3122 		return (EINVAL);
3123 	}
3124 
3125 	switch (inflags & (NDF_ANYCAST_ON|NDF_ANYCAST_OFF)) {
3126 	case NDF_ANYCAST_ON:
3127 		new_flags |= NCE_F_ANYCAST;
3128 		break;
3129 	case NDF_ANYCAST_OFF:
3130 		new_flags &= ~NCE_F_ANYCAST;
3131 		break;
3132 	case (NDF_ANYCAST_OFF|NDF_ANYCAST_ON):
3133 		mutex_exit(&ndp6.ndp_g_lock);
3134 		if (nce != NULL)
3135 			NCE_REFRELE(nce);
3136 		return (EINVAL);
3137 	}
3138 
3139 	switch (inflags & (NDF_PROXY_ON|NDF_PROXY_OFF)) {
3140 	case NDF_PROXY_ON:
3141 		new_flags |= NCE_F_PROXY;
3142 		break;
3143 	case NDF_PROXY_OFF:
3144 		new_flags &= ~NCE_F_PROXY;
3145 		break;
3146 	case (NDF_PROXY_OFF|NDF_PROXY_ON):
3147 		mutex_exit(&ndp6.ndp_g_lock);
3148 		if (nce != NULL)
3149 			NCE_REFRELE(nce);
3150 		return (EINVAL);
3151 	}
3152 
3153 	if (nce == NULL) {
3154 		err = ndp_add(ill,
3155 		    (uchar_t *)lnr->lnr_hdw_addr,
3156 		    addr,
3157 		    &ipv6_all_ones,
3158 		    &ipv6_all_zeros,
3159 		    0,
3160 		    new_flags,
3161 		    lnr->lnr_state_create,
3162 		    &nce,
3163 		    NULL,
3164 		    NULL);
3165 		if (err != 0) {
3166 			mutex_exit(&ndp6.ndp_g_lock);
3167 			ip1dbg(("ndp_sioc_update: Can't create NCE %d\n", err));
3168 			return (err);
3169 		}
3170 	}
3171 	old_flags = nce->nce_flags;
3172 	if (old_flags & NCE_F_ISROUTER && !(new_flags & NCE_F_ISROUTER)) {
3173 		/*
3174 		 * Router turned to host, delete all ires.
3175 		 * XXX Just delete the entry, but we need to add too.
3176 		 */
3177 		nce->nce_flags &= ~NCE_F_ISROUTER;
3178 		mutex_exit(&ndp6.ndp_g_lock);
3179 		ndp_delete(nce);
3180 		NCE_REFRELE(nce);
3181 		return (0);
3182 	}
3183 	mutex_exit(&ndp6.ndp_g_lock);
3184 
3185 	mutex_enter(&nce->nce_lock);
3186 	nce->nce_flags = new_flags;
3187 	mutex_exit(&nce->nce_lock);
3188 	/*
3189 	 * Note that we ignore the state at this point, which
3190 	 * should be either STALE or REACHABLE.  Instead we let
3191 	 * the link layer address passed in to determine the state
3192 	 * much like incoming packets.
3193 	 */
3194 	ndp_process(nce, (uchar_t *)lnr->lnr_hdw_addr, 0, B_FALSE);
3195 	NCE_REFRELE(nce);
3196 	return (0);
3197 }
3198 
3199 /*
3200  * If the device driver supports it, we make nce_fp_mp to have
3201  * an M_DATA prepend.  Otherwise nce_fp_mp will be null.
3202  * The caller insures there is hold on nce for this function.
3203  * Note that since ill_fastpath_probe() copies the mblk there is
3204  * no need for the hold beyond this function.
3205  */
3206 static void
3207 nce_fastpath(nce_t *nce)
3208 {
3209 	ill_t	*ill = nce->nce_ill;
3210 	int res;
3211 
3212 	ASSERT(ill != NULL);
3213 	if (nce->nce_fp_mp != NULL) {
3214 		/* Already contains fastpath info */
3215 		return;
3216 	}
3217 	if (nce->nce_res_mp != NULL) {
3218 		nce_fastpath_list_add(nce);
3219 		res = ill_fastpath_probe(ill, nce->nce_res_mp);
3220 		/*
3221 		 * EAGAIN is an indication of a transient error
3222 		 * i.e. allocation failure etc. leave the nce in the list it
3223 		 * will be updated when another probe happens for another ire
3224 		 * if not it will be taken out of the list when the ire is
3225 		 * deleted.
3226 		 */
3227 
3228 		if (res != 0 && res != EAGAIN)
3229 			nce_fastpath_list_delete(nce);
3230 	}
3231 }
3232 
3233 /*
3234  * Drain the list of nce's waiting for fastpath response.
3235  */
3236 void
3237 nce_fastpath_list_dispatch(ill_t *ill, boolean_t (*func)(nce_t *, void  *),
3238     void *arg)
3239 {
3240 
3241 	nce_t *next_nce;
3242 	nce_t *current_nce;
3243 	nce_t *first_nce;
3244 	nce_t *prev_nce = NULL;
3245 
3246 	ASSERT(ill != NULL && ill->ill_isv6);
3247 
3248 	mutex_enter(&ill->ill_lock);
3249 	first_nce = current_nce = (nce_t *)ill->ill_fastpath_list;
3250 	while (current_nce != (nce_t *)&ill->ill_fastpath_list) {
3251 		next_nce = current_nce->nce_fastpath;
3252 		/*
3253 		 * Take it off the list if we're flushing, or if the callback
3254 		 * routine tells us to do so.  Otherwise, leave the nce in the
3255 		 * fastpath list to handle any pending response from the lower
3256 		 * layer.  We can't drain the list when the callback routine
3257 		 * comparison failed, because the response is asynchronous in
3258 		 * nature, and may not arrive in the same order as the list
3259 		 * insertion.
3260 		 */
3261 		if (func == NULL || func(current_nce, arg)) {
3262 			current_nce->nce_fastpath = NULL;
3263 			if (current_nce == first_nce)
3264 				ill->ill_fastpath_list = first_nce = next_nce;
3265 			else
3266 				prev_nce->nce_fastpath = next_nce;
3267 		} else {
3268 			/* previous element that is still in the list */
3269 			prev_nce = current_nce;
3270 		}
3271 		current_nce = next_nce;
3272 	}
3273 	mutex_exit(&ill->ill_lock);
3274 }
3275 
3276 /*
3277  * Add nce to the nce fastpath list.
3278  */
3279 void
3280 nce_fastpath_list_add(nce_t *nce)
3281 {
3282 	ill_t *ill;
3283 
3284 	ill = nce->nce_ill;
3285 	ASSERT(ill != NULL && ill->ill_isv6);
3286 
3287 	mutex_enter(&ill->ill_lock);
3288 	mutex_enter(&nce->nce_lock);
3289 
3290 	/*
3291 	 * if nce has not been deleted and
3292 	 * is not already in the list add it.
3293 	 */
3294 	if (!(nce->nce_flags & NCE_F_CONDEMNED) &&
3295 	    (nce->nce_fastpath == NULL)) {
3296 		nce->nce_fastpath = (nce_t *)ill->ill_fastpath_list;
3297 		ill->ill_fastpath_list = nce;
3298 	}
3299 
3300 	mutex_exit(&nce->nce_lock);
3301 	mutex_exit(&ill->ill_lock);
3302 }
3303 
3304 /*
3305  * remove nce from the nce fastpath list.
3306  */
3307 void
3308 nce_fastpath_list_delete(nce_t *nce)
3309 {
3310 	nce_t *nce_ptr;
3311 
3312 	ill_t *ill;
3313 
3314 	ill = nce->nce_ill;
3315 	ASSERT(ill != NULL);
3316 	if (!ill->ill_isv6)  {
3317 		/*
3318 		 * v4 nce_t's do not have nce_fastpath set.
3319 		 */
3320 		return;
3321 	}
3322 
3323 	mutex_enter(&ill->ill_lock);
3324 	if (nce->nce_fastpath == NULL)
3325 		goto done;
3326 
3327 	ASSERT(ill->ill_fastpath_list != &ill->ill_fastpath_list);
3328 
3329 	if (ill->ill_fastpath_list == nce) {
3330 		ill->ill_fastpath_list = nce->nce_fastpath;
3331 	} else {
3332 		nce_ptr = ill->ill_fastpath_list;
3333 		while (nce_ptr != (nce_t *)&ill->ill_fastpath_list) {
3334 			if (nce_ptr->nce_fastpath == nce) {
3335 				nce_ptr->nce_fastpath = nce->nce_fastpath;
3336 				break;
3337 			}
3338 			nce_ptr = nce_ptr->nce_fastpath;
3339 		}
3340 	}
3341 
3342 	nce->nce_fastpath = NULL;
3343 done:
3344 	mutex_exit(&ill->ill_lock);
3345 }
3346 
3347 /*
3348  * Update all NCE's that are not in fastpath mode and
3349  * have an nce_fp_mp that matches mp. mp->b_cont contains
3350  * the fastpath header.
3351  *
3352  * Returns TRUE if entry should be dequeued, or FALSE otherwise.
3353  */
3354 boolean_t
3355 ndp_fastpath_update(nce_t *nce, void *arg)
3356 {
3357 	mblk_t 	*mp, *fp_mp;
3358 	uchar_t	*mp_rptr, *ud_mp_rptr;
3359 	mblk_t	*ud_mp = nce->nce_res_mp;
3360 	ptrdiff_t	cmplen;
3361 
3362 	if (nce->nce_flags & NCE_F_MAPPING)
3363 		return (B_TRUE);
3364 	if ((nce->nce_fp_mp != NULL) || (ud_mp == NULL))
3365 		return (B_TRUE);
3366 
3367 	ip2dbg(("ndp_fastpath_update: trying\n"));
3368 	mp = (mblk_t *)arg;
3369 	mp_rptr = mp->b_rptr;
3370 	cmplen = mp->b_wptr - mp_rptr;
3371 	ASSERT(cmplen >= 0);
3372 	ud_mp_rptr = ud_mp->b_rptr;
3373 	/*
3374 	 * The nce is locked here to prevent any other threads
3375 	 * from accessing and changing nce_res_mp when the IPv6 address
3376 	 * becomes resolved to an lla while we're in the middle
3377 	 * of looking at and comparing the hardware address (lla).
3378 	 * It is also locked to prevent multiple threads in nce_fastpath_update
3379 	 * from examining nce_res_mp atthe same time.
3380 	 */
3381 	mutex_enter(&nce->nce_lock);
3382 	if (ud_mp->b_wptr - ud_mp_rptr != cmplen ||
3383 	    bcmp((char *)mp_rptr, (char *)ud_mp_rptr, cmplen) != 0) {
3384 		mutex_exit(&nce->nce_lock);
3385 		/*
3386 		 * Don't take the ire off the fastpath list yet,
3387 		 * since the response may come later.
3388 		 */
3389 		return (B_FALSE);
3390 	}
3391 	/* Matched - install mp as the fastpath mp */
3392 	ip1dbg(("ndp_fastpath_update: match\n"));
3393 	fp_mp = dupb(mp->b_cont);
3394 	if (fp_mp != NULL) {
3395 		nce->nce_fp_mp = fp_mp;
3396 	}
3397 	mutex_exit(&nce->nce_lock);
3398 	return (B_TRUE);
3399 }
3400 
3401 /*
3402  * This function handles the DL_NOTE_FASTPATH_FLUSH notification from
3403  * driver.  Note that it assumes IP is exclusive...
3404  */
3405 /* ARGSUSED */
3406 void
3407 ndp_fastpath_flush(nce_t *nce, char *arg)
3408 {
3409 	if (nce->nce_flags & NCE_F_MAPPING)
3410 		return;
3411 	/* No fastpath info? */
3412 	if (nce->nce_fp_mp == NULL || nce->nce_res_mp == NULL)
3413 		return;
3414 
3415 	/* Just delete the NCE... */
3416 	ndp_delete(nce);
3417 }
3418 
3419 /*
3420  * Return a pointer to a given option in the packet.
3421  * Assumes that option part of the packet have already been validated.
3422  */
3423 nd_opt_hdr_t *
3424 ndp_get_option(nd_opt_hdr_t *opt, int optlen, int opt_type)
3425 {
3426 	while (optlen > 0) {
3427 		if (opt->nd_opt_type == opt_type)
3428 			return (opt);
3429 		optlen -= 8 * opt->nd_opt_len;
3430 		opt = (struct nd_opt_hdr *)((char *)opt + 8 * opt->nd_opt_len);
3431 	}
3432 	return (NULL);
3433 }
3434 
3435 /*
3436  * Verify all option lengths present are > 0, also check to see
3437  * if the option lengths and packet length are consistent.
3438  */
3439 boolean_t
3440 ndp_verify_optlen(nd_opt_hdr_t *opt, int optlen)
3441 {
3442 	ASSERT(opt != NULL);
3443 	while (optlen > 0) {
3444 		if (opt->nd_opt_len == 0)
3445 			return (B_FALSE);
3446 		optlen -= 8 * opt->nd_opt_len;
3447 		if (optlen < 0)
3448 			return (B_FALSE);
3449 		opt = (struct nd_opt_hdr *)((char *)opt + 8 * opt->nd_opt_len);
3450 	}
3451 	return (B_TRUE);
3452 }
3453 
3454 /*
3455  * ndp_walk function.
3456  * Free a fraction of the NCE cache entries.
3457  * A fraction of zero means to not free any in that category.
3458  */
3459 void
3460 ndp_cache_reclaim(nce_t *nce, char *arg)
3461 {
3462 	nce_cache_reclaim_t *ncr = (nce_cache_reclaim_t *)arg;
3463 	uint_t	rand;
3464 
3465 	if (nce->nce_flags & NCE_F_PERMANENT)
3466 		return;
3467 
3468 	rand = (uint_t)lbolt +
3469 	    NCE_ADDR_HASH_V6(nce->nce_addr, NCE_TABLE_SIZE);
3470 	if (ncr->ncr_host != 0 &&
3471 	    (rand/ncr->ncr_host)*ncr->ncr_host == rand) {
3472 		ndp_delete(nce);
3473 		return;
3474 	}
3475 }
3476 
3477 /*
3478  * ndp_walk function.
3479  * Count the number of NCEs that can be deleted.
3480  * These would be hosts but not routers.
3481  */
3482 void
3483 ndp_cache_count(nce_t *nce, char *arg)
3484 {
3485 	ncc_cache_count_t *ncc = (ncc_cache_count_t *)arg;
3486 
3487 	if (nce->nce_flags & NCE_F_PERMANENT)
3488 		return;
3489 
3490 	ncc->ncc_total++;
3491 	if (!(nce->nce_flags & NCE_F_ISROUTER))
3492 		ncc->ncc_host++;
3493 }
3494 
3495 #ifdef NCE_DEBUG
3496 th_trace_t *
3497 th_trace_nce_lookup(nce_t *nce)
3498 {
3499 	int bucket_id;
3500 	th_trace_t *th_trace;
3501 
3502 	ASSERT(MUTEX_HELD(&nce->nce_lock));
3503 
3504 	bucket_id = IP_TR_HASH(curthread);
3505 	ASSERT(bucket_id < IP_TR_HASH_MAX);
3506 
3507 	for (th_trace = nce->nce_trace[bucket_id]; th_trace != NULL;
3508 	    th_trace = th_trace->th_next) {
3509 		if (th_trace->th_id == curthread)
3510 			return (th_trace);
3511 	}
3512 	return (NULL);
3513 }
3514 
3515 void
3516 nce_trace_ref(nce_t *nce)
3517 {
3518 	int bucket_id;
3519 	th_trace_t *th_trace;
3520 
3521 	/*
3522 	 * Attempt to locate the trace buffer for the curthread.
3523 	 * If it does not exist, then allocate a new trace buffer
3524 	 * and link it in list of trace bufs for this ipif, at the head
3525 	 */
3526 	ASSERT(MUTEX_HELD(&nce->nce_lock));
3527 
3528 	if (nce->nce_trace_disable == B_TRUE)
3529 		return;
3530 
3531 	th_trace = th_trace_nce_lookup(nce);
3532 	if (th_trace == NULL) {
3533 		bucket_id = IP_TR_HASH(curthread);
3534 		th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t),
3535 		    KM_NOSLEEP);
3536 		if (th_trace == NULL) {
3537 			nce->nce_trace_disable = B_TRUE;
3538 			nce_trace_inactive(nce);
3539 			return;
3540 		}
3541 		th_trace->th_id = curthread;
3542 		th_trace->th_next = nce->nce_trace[bucket_id];
3543 		th_trace->th_prev = &nce->nce_trace[bucket_id];
3544 		if (th_trace->th_next != NULL)
3545 			th_trace->th_next->th_prev = &th_trace->th_next;
3546 		nce->nce_trace[bucket_id] = th_trace;
3547 	}
3548 	ASSERT(th_trace->th_refcnt < TR_BUF_MAX - 1);
3549 	th_trace->th_refcnt++;
3550 	th_trace_rrecord(th_trace);
3551 }
3552 
3553 void
3554 nce_untrace_ref(nce_t *nce)
3555 {
3556 	th_trace_t *th_trace;
3557 
3558 	ASSERT(MUTEX_HELD(&nce->nce_lock));
3559 
3560 	if (nce->nce_trace_disable == B_TRUE)
3561 		return;
3562 
3563 	th_trace = th_trace_nce_lookup(nce);
3564 	ASSERT(th_trace != NULL && th_trace->th_refcnt > 0);
3565 
3566 	th_trace_rrecord(th_trace);
3567 	th_trace->th_refcnt--;
3568 }
3569 
3570 void
3571 nce_trace_inactive(nce_t *nce)
3572 {
3573 	th_trace_t *th_trace;
3574 	int i;
3575 
3576 	ASSERT(MUTEX_HELD(&nce->nce_lock));
3577 
3578 	for (i = 0; i < IP_TR_HASH_MAX; i++) {
3579 		while (nce->nce_trace[i] != NULL) {
3580 			th_trace = nce->nce_trace[i];
3581 
3582 			/* unlink th_trace and free it */
3583 			nce->nce_trace[i] = th_trace->th_next;
3584 			if (th_trace->th_next != NULL)
3585 				th_trace->th_next->th_prev =
3586 				    &nce->nce_trace[i];
3587 
3588 			th_trace->th_next = NULL;
3589 			th_trace->th_prev = NULL;
3590 			kmem_free(th_trace, sizeof (th_trace_t));
3591 		}
3592 	}
3593 
3594 }
3595 
3596 /* ARGSUSED */
3597 int
3598 nce_thread_exit(nce_t *nce, caddr_t arg)
3599 {
3600 	th_trace_t	*th_trace;
3601 
3602 	mutex_enter(&nce->nce_lock);
3603 	th_trace = th_trace_nce_lookup(nce);
3604 
3605 	if (th_trace == NULL) {
3606 		mutex_exit(&nce->nce_lock);
3607 		return (0);
3608 	}
3609 
3610 	ASSERT(th_trace->th_refcnt == 0);
3611 
3612 	/* unlink th_trace and free it */
3613 	*th_trace->th_prev = th_trace->th_next;
3614 	if (th_trace->th_next != NULL)
3615 		th_trace->th_next->th_prev = th_trace->th_prev;
3616 	th_trace->th_next = NULL;
3617 	th_trace->th_prev = NULL;
3618 	kmem_free(th_trace, sizeof (th_trace_t));
3619 	mutex_exit(&nce->nce_lock);
3620 	return (0);
3621 }
3622 #endif
3623 
3624 /*
3625  * Called when address resolution fails due to a timeout.
3626  * Send an ICMP unreachable in response to all queued packets.
3627  */
3628 void
3629 arp_resolv_failed(nce_t *nce)
3630 {
3631 	mblk_t	*mp, *nxt_mp, *first_mp;
3632 	char	buf[INET6_ADDRSTRLEN];
3633 	zoneid_t zoneid = GLOBAL_ZONEID;
3634 	struct in_addr ipv4addr;
3635 
3636 	IN6_V4MAPPED_TO_INADDR(&nce->nce_addr, &ipv4addr);
3637 	ip3dbg(("arp_resolv_failed: dst %s\n",
3638 	    inet_ntop(AF_INET, &ipv4addr, buf, sizeof (buf))));
3639 	mutex_enter(&nce->nce_lock);
3640 	mp = nce->nce_qd_mp;
3641 	nce->nce_qd_mp = NULL;
3642 	mutex_exit(&nce->nce_lock);
3643 
3644 	while (mp != NULL) {
3645 		nxt_mp = mp->b_next;
3646 		mp->b_next = NULL;
3647 		mp->b_prev = NULL;
3648 
3649 		first_mp = mp;
3650 		/*
3651 		 * Send icmp unreachable messages
3652 		 * to the hosts.
3653 		 */
3654 		(void) ip_hdr_complete((ipha_t *)mp->b_rptr, zoneid);
3655 		ip3dbg(("arp_resolv_failed: Calling icmp_unreachable\n"));
3656 		icmp_unreachable(nce->nce_ill->ill_wq, first_mp,
3657 		    ICMP_HOST_UNREACHABLE, zoneid);
3658 		mp = nxt_mp;
3659 	}
3660 }
3661 
3662 static int
3663 ndp_lookup_then_add_v4(ill_t *ill, uchar_t *hw_addr, const in_addr_t *addr,
3664     const in_addr_t *mask, const in_addr_t *extract_mask,
3665     uint32_t hw_extract_start, uint16_t flags, uint16_t state,
3666     nce_t **newnce, mblk_t *fp_mp, mblk_t *res_mp)
3667 {
3668 	int	err = 0;
3669 	nce_t	*nce;
3670 	in6_addr_t addr6;
3671 
3672 	mutex_enter(&ndp4.ndp_g_lock);
3673 	nce = *((nce_t **)NCE_HASH_PTR_V4(*addr));
3674 	IN6_IPADDR_TO_V4MAPPED(*addr, &addr6);
3675 	nce = nce_lookup_addr(ill, &addr6, nce);
3676 	if (nce == NULL) {
3677 		err = ndp_add_v4(ill,
3678 		    hw_addr,
3679 		    addr,
3680 		    mask,
3681 		    extract_mask,
3682 		    hw_extract_start,
3683 		    flags,
3684 		    state,
3685 		    newnce,
3686 		    fp_mp,
3687 		    res_mp);
3688 	} else {
3689 		*newnce = nce;
3690 		err = EEXIST;
3691 	}
3692 	mutex_exit(&ndp4.ndp_g_lock);
3693 	return (err);
3694 }
3695 
3696 /*
3697  * NDP Cache Entry creation routine for IPv4.
3698  * Mapped entries are handled in arp.
3699  * This routine must always be called with ndp4.ndp_g_lock held.
3700  * Prior to return, nce_refcnt is incremented.
3701  */
3702 static int
3703 ndp_add_v4(ill_t *ill, uchar_t *hw_addr, const in_addr_t *addr,
3704     const in_addr_t *mask, const in_addr_t *extract_mask,
3705     uint32_t hw_extract_start, uint16_t flags, uint16_t state,
3706     nce_t **newnce, mblk_t *fp_mp, mblk_t *res_mp)
3707 {
3708 	static	nce_t		nce_nil;
3709 	nce_t		*nce;
3710 	mblk_t		*mp;
3711 	mblk_t		*template;
3712 	nce_t		**ncep;
3713 
3714 	ASSERT(MUTEX_HELD(&ndp4.ndp_g_lock));
3715 	ASSERT(ill != NULL);
3716 	if ((flags & ~NCE_EXTERNAL_FLAGS_MASK)) {
3717 		return (EINVAL);
3718 	}
3719 	ASSERT((flags & NCE_F_MAPPING) == 0);
3720 	ASSERT(extract_mask == NULL);
3721 	/*
3722 	 * Allocate the mblk to hold the nce.
3723 	 */
3724 	mp = allocb(sizeof (nce_t), BPRI_MED);
3725 	if (mp == NULL)
3726 		return (ENOMEM);
3727 
3728 	nce = (nce_t *)mp->b_rptr;
3729 	mp->b_wptr = (uchar_t *)&nce[1];
3730 	*nce = nce_nil;
3731 
3732 	/*
3733 	 * This one holds link layer address; if res_mp has been provided
3734 	 * by the caller, accept it without any further checks. Otherwise,
3735 	 * for V4, we fill it up with ill_resolver_mp here, then in
3736 	 * in ire_arpresolve(), we fill it up with the ARP query
3737 	 * once its formulated.
3738 	 */
3739 	if (res_mp != NULL) {
3740 		template = res_mp;
3741 	} else  {
3742 		if (ill->ill_resolver_mp == NULL) {
3743 			freeb(mp);
3744 			return (EINVAL);
3745 		}
3746 		template = copyb(ill->ill_resolver_mp);
3747 	}
3748 	if (template == NULL) {
3749 		freeb(mp);
3750 		return (ENOMEM);
3751 	}
3752 	nce->nce_ill = ill;
3753 	nce->nce_ipversion = IPV4_VERSION;
3754 	nce->nce_flags = flags;
3755 	nce->nce_state = state;
3756 	nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT;
3757 	nce->nce_rcnt = ill->ill_xmit_count;
3758 	IN6_IPADDR_TO_V4MAPPED(*addr, &nce->nce_addr);
3759 	if (*mask == IP_HOST_MASK) {
3760 		nce->nce_mask = ipv6_all_ones;
3761 	} else  {
3762 		IN6_IPADDR_TO_V4MAPPED(*mask, &nce->nce_mask);
3763 	}
3764 	nce->nce_extract_mask = ipv6_all_zeros;
3765 	nce->nce_ll_extract_start = hw_extract_start;
3766 	nce->nce_fp_mp = (fp_mp? fp_mp : NULL);
3767 	nce->nce_res_mp = template;
3768 	if (state == ND_REACHABLE)
3769 		nce->nce_last = TICK_TO_MSEC(lbolt64);
3770 	else
3771 		nce->nce_last = 0;
3772 	nce->nce_qd_mp = NULL;
3773 	nce->nce_mp = mp;
3774 	if (hw_addr != NULL)
3775 		nce_set_ll(nce, hw_addr);
3776 	/* This one is for nce getting created */
3777 	nce->nce_refcnt = 1;
3778 	mutex_init(&nce->nce_lock, NULL, MUTEX_DEFAULT, NULL);
3779 	ncep = ((nce_t **)NCE_HASH_PTR_V4(*addr));
3780 
3781 #ifdef NCE_DEBUG
3782 	bzero(nce->nce_trace, sizeof (th_trace_t *) * IP_TR_HASH_MAX);
3783 #endif
3784 	/*
3785 	 * Atomically ensure that the ill is not CONDEMNED, before
3786 	 * adding the NCE.
3787 	 */
3788 	mutex_enter(&ill->ill_lock);
3789 	if (ill->ill_state_flags & ILL_CONDEMNED) {
3790 		mutex_exit(&ill->ill_lock);
3791 		freeb(mp);
3792 		if (res_mp == NULL) {
3793 			/*
3794 			 * template was locally allocated. need to free it.
3795 			 */
3796 			freeb(template);
3797 		}
3798 		return (EINVAL);
3799 	}
3800 	if ((nce->nce_next = *ncep) != NULL)
3801 		nce->nce_next->nce_ptpn = &nce->nce_next;
3802 	*ncep = nce;
3803 	nce->nce_ptpn = ncep;
3804 	*newnce = nce;
3805 	/* This one is for nce being used by an active thread */
3806 	NCE_REFHOLD(*newnce);
3807 
3808 	/* Bump up the number of nce's referencing this ill */
3809 	ill->ill_nce_cnt++;
3810 	mutex_exit(&ill->ill_lock);
3811 	return (0);
3812 }
3813 
3814 void
3815 ndp_flush_qd_mp(nce_t *nce)
3816 {
3817 	mblk_t *qd_mp, *qd_next;
3818 
3819 	ASSERT(MUTEX_HELD(&nce->nce_lock));
3820 	qd_mp = nce->nce_qd_mp;
3821 	nce->nce_qd_mp = NULL;
3822 	while (qd_mp != NULL) {
3823 		qd_next = qd_mp->b_next;
3824 		qd_mp->b_next = NULL;
3825 		qd_mp->b_prev = NULL;
3826 		freemsg(qd_mp);
3827 		qd_mp = qd_next;
3828 	}
3829 }
3830 
3831 nce_t *
3832 nce_reinit(nce_t *nce)
3833 {
3834 	nce_t *newnce = NULL;
3835 	in_addr_t nce_addr, nce_mask;
3836 
3837 	IN6_V4MAPPED_TO_IPADDR(&nce->nce_addr, nce_addr);
3838 	IN6_V4MAPPED_TO_IPADDR(&nce->nce_mask, nce_mask);
3839 	/*
3840 	 * delete the old one. this will get rid of any ire's pointing
3841 	 * at this nce.
3842 	 */
3843 	ndp_delete(nce);
3844 	/*
3845 	 * create a new nce with the same addr and mask.
3846 	 */
3847 	mutex_enter(&ndp4.ndp_g_lock);
3848 	(void) ndp_add_v4(nce->nce_ill, NULL, &nce_addr, &nce_mask, NULL, 0, 0,
3849 	    ND_INITIAL, &newnce, NULL, NULL);
3850 	mutex_exit(&ndp4.ndp_g_lock);
3851 	/*
3852 	 * refrele the old nce.
3853 	 */
3854 	NCE_REFRELE(nce);
3855 	return (newnce);
3856 }
3857 
3858 /*
3859  * ndp_walk routine to delete all entries that have a given destination or
3860  * gateway address and cached link layer (MAC) address.  This is used when ARP
3861  * informs us that a network-to-link-layer mapping may have changed.
3862  */
3863 void
3864 nce_delete_hw_changed(nce_t *nce, void *arg)
3865 {
3866 	nce_hw_map_t *hwm = arg;
3867 	mblk_t *mp;
3868 	dl_unitdata_req_t *dlu;
3869 	uchar_t *macaddr;
3870 	ill_t *ill;
3871 	int saplen;
3872 	ipaddr_t nce_addr;
3873 
3874 	if (nce->nce_state != ND_REACHABLE)
3875 		return;
3876 
3877 	IN6_V4MAPPED_TO_IPADDR(&nce->nce_addr, nce_addr);
3878 	if (nce_addr != hwm->hwm_addr)
3879 		return;
3880 
3881 	mutex_enter(&nce->nce_lock);
3882 	if ((mp = nce->nce_res_mp) == NULL) {
3883 		mutex_exit(&nce->nce_lock);
3884 		return;
3885 	}
3886 	dlu = (dl_unitdata_req_t *)mp->b_rptr;
3887 	macaddr = (uchar_t *)(dlu + 1);
3888 	ill = nce->nce_ill;
3889 	if ((saplen = ill->ill_sap_length) > 0)
3890 		macaddr += saplen;
3891 	else
3892 		saplen = -saplen;
3893 
3894 	/*
3895 	 * If the hardware address is unchanged, then leave this one alone.
3896 	 * Note that saplen == abs(saplen) now.
3897 	 */
3898 	if (hwm->hwm_hwlen == dlu->dl_dest_addr_length - saplen &&
3899 	    bcmp(hwm->hwm_hwaddr, macaddr, hwm->hwm_hwlen) == 0) {
3900 		mutex_exit(&nce->nce_lock);
3901 		return;
3902 	}
3903 	mutex_exit(&nce->nce_lock);
3904 
3905 	DTRACE_PROBE1(nce__hw__deleted, nce_t *, nce);
3906 	ndp_delete(nce);
3907 }
3908 
3909 /*
3910  * This function verifies whether a given IPv4 address is potentially known to
3911  * the NCE subsystem.  If so, then ARP must not delete the corresponding ace_t,
3912  * so that it can continue to look for hardware changes on that address.
3913  */
3914 boolean_t
3915 ndp_lookup_ipaddr(in_addr_t addr)
3916 {
3917 	nce_t		*nce;
3918 	struct in_addr	nceaddr;
3919 
3920 	if (addr == INADDR_ANY)
3921 		return (B_FALSE);
3922 
3923 	mutex_enter(&ndp4.ndp_g_lock);
3924 	nce = *(nce_t **)NCE_HASH_PTR_V4(addr);
3925 	for (; nce != NULL; nce = nce->nce_next) {
3926 		/* Note that only v4 mapped entries are in the table. */
3927 		IN6_V4MAPPED_TO_INADDR(&nce->nce_addr, &nceaddr);
3928 		if (addr == nceaddr.s_addr &&
3929 		    IN6_ARE_ADDR_EQUAL(&nce->nce_mask, &ipv6_all_ones)) {
3930 			/* Single flag check; no lock needed */
3931 			if (!(nce->nce_flags & NCE_F_CONDEMNED))
3932 				break;
3933 		}
3934 	}
3935 	mutex_exit(&ndp4.ndp_g_lock);
3936 	return (nce != NULL);
3937 }
3938