xref: /illumos-gate/usr/src/uts/common/inet/ip/ip_multi.c (revision a7acbe81c8aa5c4dfa236e25a6a7fd17f96eca65)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/stream.h>
31 #include <sys/dlpi.h>
32 #include <sys/stropts.h>
33 #include <sys/strsun.h>
34 #include <sys/ddi.h>
35 #include <sys/cmn_err.h>
36 #include <sys/sdt.h>
37 #include <sys/zone.h>
38 
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <net/if.h>
43 #include <sys/systm.h>
44 #include <sys/strsubr.h>
45 #include <net/route.h>
46 #include <netinet/in.h>
47 #include <net/if_dl.h>
48 #include <netinet/ip6.h>
49 #include <netinet/icmp6.h>
50 
51 #include <inet/common.h>
52 #include <inet/mi.h>
53 #include <inet/nd.h>
54 #include <inet/arp.h>
55 #include <inet/ip.h>
56 #include <inet/ip6.h>
57 #include <inet/ip_if.h>
58 #include <inet/ip_ndp.h>
59 #include <inet/ip_multi.h>
60 #include <inet/ipclassifier.h>
61 #include <inet/ipsec_impl.h>
62 #include <inet/sctp_ip.h>
63 #include <inet/ip_listutils.h>
64 #include <inet/udp_impl.h>
65 
66 /* igmpv3/mldv2 source filter manipulation */
67 static void	ilm_bld_flists(conn_t *conn, void *arg);
68 static void	ilm_gen_filter(ilm_t *ilm, mcast_record_t *fmode,
69     slist_t *flist);
70 
71 static ilm_t	*ilm_add_v6(ipif_t *ipif, const in6_addr_t *group,
72     ilg_stat_t ilgstat, mcast_record_t ilg_fmode, slist_t *ilg_flist,
73     int orig_ifindex, zoneid_t zoneid);
74 static void	ilm_delete(ilm_t *ilm);
75 static int	ip_ll_addmulti_v6(ipif_t *ipif, const in6_addr_t *group);
76 static int	ip_ll_delmulti_v6(ipif_t *ipif, const in6_addr_t *group);
77 static ilg_t	*ilg_lookup_ill_index_v6(conn_t *connp,
78     const in6_addr_t *v6group, int index);
79 static ilg_t	*ilg_lookup_ipif(conn_t *connp, ipaddr_t group,
80     ipif_t *ipif);
81 static int	ilg_add(conn_t *connp, ipaddr_t group, ipif_t *ipif,
82     mcast_record_t fmode, ipaddr_t src);
83 static int	ilg_add_v6(conn_t *connp, const in6_addr_t *group, ill_t *ill,
84     mcast_record_t fmode, const in6_addr_t *v6src);
85 static void	ilg_delete(conn_t *connp, ilg_t *ilg, const in6_addr_t *src);
86 static mblk_t	*ill_create_dl(ill_t *ill, uint32_t dl_primitive,
87     uint32_t length, uint32_t *addr_lenp, uint32_t *addr_offp);
88 static mblk_t	*ill_create_squery(ill_t *ill, ipaddr_t ipaddr,
89     uint32_t addrlen, uint32_t addroff, mblk_t *mp_tail);
90 static void	conn_ilg_reap(conn_t *connp);
91 static int	ip_opt_delete_group_excl(conn_t *connp, ipaddr_t group,
92     ipif_t *ipif, mcast_record_t fmode, ipaddr_t src);
93 static int	ip_opt_delete_group_excl_v6(conn_t *connp,
94     const in6_addr_t *v6group, ill_t *ill, mcast_record_t fmode,
95     const in6_addr_t *v6src);
96 
97 /*
98  * MT notes:
99  *
100  * Multicast joins operate on both the ilg and ilm structures. Multiple
101  * threads operating on an conn (socket) trying to do multicast joins
102  * need to synchronize  when operating on the ilg. Multiple threads
103  * potentially operating on different conn (socket endpoints) trying to
104  * do multicast joins could eventually end up trying to manipulate the
105  * ilm simulatenously and need to synchronize on the access to the ilm.
106  * Both are amenable to standard Solaris MT techniques, but it would be
107  * complex to handle a failover or failback which needs to manipulate
108  * ilg/ilms if an applications can also simultaenously join/leave
109  * multicast groups. Hence multicast join/leave also go through the ipsq_t
110  * serialization.
111  *
112  * Multicast joins and leaves are single-threaded per phyint/IPMP group
113  * using the ipsq serialization mechanism.
114  *
115  * An ilm is an IP data structure used to track multicast join/leave.
116  * An ilm is associated with a <multicast group, ipif> tuple in IPv4 and
117  * with just <multicast group> in IPv6. ilm_refcnt is the number of ilg's
118  * referencing the ilm. ilms are created / destroyed only as writer. ilms
119  * are not passed around, instead they are looked up and used under the
120  * ill_lock or as writer. So we don't need a dynamic refcount of the number
121  * of threads holding reference to an ilm.
122  *
123  * Multicast Join operation:
124  *
125  * The first step is to determine the ipif (v4) or ill (v6) on which
126  * the join operation is to be done. The join is done after becoming
127  * exclusive on the ipsq associated with the ipif or ill. The conn->conn_ilg
128  * and ill->ill_ilm are thus accessed and modified exclusively per ill.
129  * Multiple threads can attempt to join simultaneously on different ipif/ill
130  * on the same conn. In this case the ipsq serialization does not help in
131  * protecting the ilg. It is the conn_lock that is used to protect the ilg.
132  * The conn_lock also protects all the ilg_t members.
133  *
134  * Leave operation.
135  *
136  * Similar to the join operation, the first step is to determine the ipif
137  * or ill (v6) on which the leave operation is to be done. The leave operation
138  * is done after becoming exclusive on the ipsq associated with the ipif or ill.
139  * As with join ilg modification is done under the protection of the conn lock.
140  */
141 
142 #define	IPSQ_ENTER_IPIF(ipif, connp, first_mp, func, ipsq, type)	\
143 	ASSERT(connp != NULL);					\
144 	(ipsq) = ipsq_try_enter((ipif), NULL, CONNP_TO_WQ(connp),	\
145 	    (first_mp), (func), (type), B_TRUE);		\
146 	if ((ipsq) == NULL) {					\
147 		ipif_refrele(ipif);				\
148 		return (EINPROGRESS);				\
149 	}
150 
151 #define	IPSQ_ENTER_ILL(ill, connp, first_mp, func, ipsq, type)	\
152 	ASSERT(connp != NULL);					\
153 	(ipsq) = ipsq_try_enter(NULL, ill, CONNP_TO_WQ(connp),	\
154 	    (first_mp),	(func), (type), B_TRUE);		\
155 	if ((ipsq) == NULL) {					\
156 		ill_refrele(ill);				\
157 		return (EINPROGRESS);				\
158 	}
159 
160 #define	IPSQ_EXIT(ipsq)	\
161 	if (ipsq != NULL)	\
162 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
163 
164 #define	ILG_WALKER_HOLD(connp)	(connp)->conn_ilg_walker_cnt++
165 
166 #define	ILG_WALKER_RELE(connp)				\
167 	{						\
168 		(connp)->conn_ilg_walker_cnt--;		\
169 		if ((connp)->conn_ilg_walker_cnt == 0)	\
170 			conn_ilg_reap(connp);		\
171 	}
172 
173 static void
174 conn_ilg_reap(conn_t *connp)
175 {
176 	int	to;
177 	int	from;
178 	ilg_t	*ilg;
179 
180 	ASSERT(MUTEX_HELD(&connp->conn_lock));
181 
182 	to = 0;
183 	from = 0;
184 	while (from < connp->conn_ilg_inuse) {
185 		if (connp->conn_ilg[from].ilg_flags & ILG_DELETED) {
186 			ilg = &connp->conn_ilg[from];
187 			FREE_SLIST(ilg->ilg_filter);
188 			ilg->ilg_flags &= ~ILG_DELETED;
189 			from++;
190 			continue;
191 		}
192 		if (to != from)
193 			connp->conn_ilg[to] = connp->conn_ilg[from];
194 		to++;
195 		from++;
196 	}
197 
198 	connp->conn_ilg_inuse = to;
199 
200 	if (connp->conn_ilg_inuse == 0) {
201 		mi_free((char *)connp->conn_ilg);
202 		connp->conn_ilg = NULL;
203 		cv_broadcast(&connp->conn_refcv);
204 	}
205 }
206 
207 #define	GETSTRUCT(structure, number)	\
208 	((structure *)mi_zalloc(sizeof (structure) * (number)))
209 
210 #define	ILG_ALLOC_CHUNK	16
211 
212 /*
213  * Returns a pointer to the next available ilg in conn_ilg.  Allocs more
214  * buffers in size of ILG_ALLOC_CHUNK ilgs when needed, and updates conn's
215  * ilg tracking fields appropriately (conn_ilg_inuse reflects usage of the
216  * returned ilg).  Returns NULL on failure (ENOMEM).
217  *
218  * Assumes connp->conn_lock is held.
219  */
220 static ilg_t *
221 conn_ilg_alloc(conn_t *connp)
222 {
223 	ilg_t *new, *ret;
224 	int curcnt;
225 
226 	ASSERT(MUTEX_HELD(&connp->conn_lock));
227 	ASSERT(connp->conn_ilg_inuse <= connp->conn_ilg_allocated);
228 
229 	if (connp->conn_ilg == NULL) {
230 		connp->conn_ilg = GETSTRUCT(ilg_t, ILG_ALLOC_CHUNK);
231 		if (connp->conn_ilg == NULL)
232 			return (NULL);
233 		connp->conn_ilg_allocated = ILG_ALLOC_CHUNK;
234 		connp->conn_ilg_inuse = 0;
235 	}
236 	if (connp->conn_ilg_inuse == connp->conn_ilg_allocated) {
237 		if (connp->conn_ilg_walker_cnt != 0) {
238 			/*
239 			 * XXX We cannot grow the array at this point
240 			 * because a list walker could be in progress, and
241 			 * we cannot wipe out the existing array until the
242 			 * walker is done. Just return NULL for now.
243 			 * ilg_delete_all() will have to be changed when
244 			 * this logic is changed.
245 			 */
246 			return (NULL);
247 		}
248 		curcnt = connp->conn_ilg_allocated;
249 		new = GETSTRUCT(ilg_t, curcnt + ILG_ALLOC_CHUNK);
250 		if (new == NULL)
251 			return (NULL);
252 		bcopy(connp->conn_ilg, new, sizeof (ilg_t) * curcnt);
253 		mi_free((char *)connp->conn_ilg);
254 		connp->conn_ilg = new;
255 		connp->conn_ilg_allocated += ILG_ALLOC_CHUNK;
256 	}
257 
258 	ret = &connp->conn_ilg[connp->conn_ilg_inuse++];
259 	ASSERT((ret->ilg_flags & ILG_DELETED) == 0);
260 	bzero(ret, sizeof (*ret));
261 	return (ret);
262 }
263 
264 typedef struct ilm_fbld_s {
265 	ilm_t		*fbld_ilm;
266 	int		fbld_in_cnt;
267 	int		fbld_ex_cnt;
268 	slist_t		fbld_in;
269 	slist_t		fbld_ex;
270 	boolean_t	fbld_in_overflow;
271 } ilm_fbld_t;
272 
273 static void
274 ilm_bld_flists(conn_t *conn, void *arg)
275 {
276 	int i;
277 	ilm_fbld_t *fbld = (ilm_fbld_t *)(arg);
278 	ilm_t *ilm = fbld->fbld_ilm;
279 	in6_addr_t *v6group = &ilm->ilm_v6addr;
280 
281 	if (conn->conn_ilg_inuse == 0)
282 		return;
283 
284 	/*
285 	 * Since we can't break out of the ipcl_walk once started, we still
286 	 * have to look at every conn.  But if we've already found one
287 	 * (EXCLUDE, NULL) list, there's no need to keep checking individual
288 	 * ilgs--that will be our state.
289 	 */
290 	if (fbld->fbld_ex_cnt > 0 && fbld->fbld_ex.sl_numsrc == 0)
291 		return;
292 
293 	/*
294 	 * Check this conn's ilgs to see if any are interested in our
295 	 * ilm (group, interface match).  If so, update the master
296 	 * include and exclude lists we're building in the fbld struct
297 	 * with this ilg's filter info.
298 	 */
299 	mutex_enter(&conn->conn_lock);
300 	for (i = 0; i < conn->conn_ilg_inuse; i++) {
301 		ilg_t *ilg = &conn->conn_ilg[i];
302 		if ((ilg->ilg_ill == ilm->ilm_ill) &&
303 		    (ilg->ilg_ipif == ilm->ilm_ipif) &&
304 		    IN6_ARE_ADDR_EQUAL(&ilg->ilg_v6group, v6group)) {
305 			if (ilg->ilg_fmode == MODE_IS_INCLUDE) {
306 				fbld->fbld_in_cnt++;
307 				if (!fbld->fbld_in_overflow)
308 					l_union_in_a(&fbld->fbld_in,
309 					    ilg->ilg_filter,
310 					    &fbld->fbld_in_overflow);
311 			} else {
312 				fbld->fbld_ex_cnt++;
313 				/*
314 				 * On the first exclude list, don't try to do
315 				 * an intersection, as the master exclude list
316 				 * is intentionally empty.  If the master list
317 				 * is still empty on later iterations, that
318 				 * means we have at least one ilg with an empty
319 				 * exclude list, so that should be reflected
320 				 * when we take the intersection.
321 				 */
322 				if (fbld->fbld_ex_cnt == 1) {
323 					if (ilg->ilg_filter != NULL)
324 						l_copy(ilg->ilg_filter,
325 						    &fbld->fbld_ex);
326 				} else {
327 					l_intersection_in_a(&fbld->fbld_ex,
328 					    ilg->ilg_filter);
329 				}
330 			}
331 			/* there will only be one match, so break now. */
332 			break;
333 		}
334 	}
335 	mutex_exit(&conn->conn_lock);
336 }
337 
338 static void
339 ilm_gen_filter(ilm_t *ilm, mcast_record_t *fmode, slist_t *flist)
340 {
341 	ilm_fbld_t fbld;
342 	ip_stack_t *ipst = ilm->ilm_ipst;
343 
344 	fbld.fbld_ilm = ilm;
345 	fbld.fbld_in_cnt = fbld.fbld_ex_cnt = 0;
346 	fbld.fbld_in.sl_numsrc = fbld.fbld_ex.sl_numsrc = 0;
347 	fbld.fbld_in_overflow = B_FALSE;
348 
349 	/* first, construct our master include and exclude lists */
350 	ipcl_walk(ilm_bld_flists, (caddr_t)&fbld, ipst);
351 
352 	/* now use those master lists to generate the interface filter */
353 
354 	/* if include list overflowed, filter is (EXCLUDE, NULL) */
355 	if (fbld.fbld_in_overflow) {
356 		*fmode = MODE_IS_EXCLUDE;
357 		flist->sl_numsrc = 0;
358 		return;
359 	}
360 
361 	/* if nobody interested, interface filter is (INCLUDE, NULL) */
362 	if (fbld.fbld_in_cnt == 0 && fbld.fbld_ex_cnt == 0) {
363 		*fmode = MODE_IS_INCLUDE;
364 		flist->sl_numsrc = 0;
365 		return;
366 	}
367 
368 	/*
369 	 * If there are no exclude lists, then the interface filter
370 	 * is INCLUDE, with its filter list equal to fbld_in.  A single
371 	 * exclude list makes the interface filter EXCLUDE, with its
372 	 * filter list equal to (fbld_ex - fbld_in).
373 	 */
374 	if (fbld.fbld_ex_cnt == 0) {
375 		*fmode = MODE_IS_INCLUDE;
376 		l_copy(&fbld.fbld_in, flist);
377 	} else {
378 		*fmode = MODE_IS_EXCLUDE;
379 		l_difference(&fbld.fbld_ex, &fbld.fbld_in, flist);
380 	}
381 }
382 
383 /*
384  * If the given interface has failed, choose a new one to join on so
385  * that we continue to receive packets.  ilg_orig_ifindex remembers
386  * what the application used to join on so that we know the ilg to
387  * delete even though we change the ill here.  Callers will store the
388  * ilg returned from this function in ilg_ill.  Thus when we receive
389  * a packet on ilg_ill, conn_wantpacket_v6 will deliver the packets.
390  *
391  * This function must be called as writer so we can walk the group
392  * list and examine flags without holding a lock.
393  */
394 ill_t *
395 ip_choose_multi_ill(ill_t *ill, const in6_addr_t *grp)
396 {
397 	ill_t	*till;
398 	ill_group_t *illgrp = ill->ill_group;
399 
400 	ASSERT(IAM_WRITER_ILL(ill));
401 
402 	if (IN6_IS_ADDR_UNSPECIFIED(grp) || illgrp == NULL)
403 		return (ill);
404 
405 	if ((ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE)) == 0)
406 		return (ill);
407 
408 	till = illgrp->illgrp_ill;
409 	while (till != NULL &&
410 	    (till->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE))) {
411 		till = till->ill_group_next;
412 	}
413 	if (till != NULL)
414 		return (till);
415 
416 	return (ill);
417 }
418 
419 static int
420 ilm_update_add(ilm_t *ilm, ilg_stat_t ilgstat, slist_t *ilg_flist,
421     boolean_t isv6)
422 {
423 	mcast_record_t fmode;
424 	slist_t *flist;
425 	boolean_t fdefault;
426 	char buf[INET6_ADDRSTRLEN];
427 	ill_t *ill = isv6 ? ilm->ilm_ill : ilm->ilm_ipif->ipif_ill;
428 
429 	/*
430 	 * There are several cases where the ilm's filter state
431 	 * defaults to (EXCLUDE, NULL):
432 	 *	- we've had previous joins without associated ilgs
433 	 *	- this join has no associated ilg
434 	 *	- the ilg's filter state is (EXCLUDE, NULL)
435 	 */
436 	fdefault = (ilm->ilm_no_ilg_cnt > 0) ||
437 	    (ilgstat == ILGSTAT_NONE) || SLIST_IS_EMPTY(ilg_flist);
438 
439 	/* attempt mallocs (if needed) before doing anything else */
440 	if ((flist = l_alloc()) == NULL)
441 		return (ENOMEM);
442 	if (!fdefault && ilm->ilm_filter == NULL) {
443 		ilm->ilm_filter = l_alloc();
444 		if (ilm->ilm_filter == NULL) {
445 			l_free(flist);
446 			return (ENOMEM);
447 		}
448 	}
449 
450 	if (ilgstat != ILGSTAT_CHANGE)
451 		ilm->ilm_refcnt++;
452 
453 	if (ilgstat == ILGSTAT_NONE)
454 		ilm->ilm_no_ilg_cnt++;
455 
456 	/*
457 	 * Determine new filter state.  If it's not the default
458 	 * (EXCLUDE, NULL), we must walk the conn list to find
459 	 * any ilgs interested in this group, and re-build the
460 	 * ilm filter.
461 	 */
462 	if (fdefault) {
463 		fmode = MODE_IS_EXCLUDE;
464 		flist->sl_numsrc = 0;
465 	} else {
466 		ilm_gen_filter(ilm, &fmode, flist);
467 	}
468 
469 	/* make sure state actually changed; nothing to do if not. */
470 	if ((ilm->ilm_fmode == fmode) &&
471 	    !lists_are_different(ilm->ilm_filter, flist)) {
472 		l_free(flist);
473 		return (0);
474 	}
475 
476 	/* send the state change report */
477 	if (!IS_LOOPBACK(ill)) {
478 		if (isv6)
479 			mld_statechange(ilm, fmode, flist);
480 		else
481 			igmp_statechange(ilm, fmode, flist);
482 	}
483 
484 	/* update the ilm state */
485 	ilm->ilm_fmode = fmode;
486 	if (flist->sl_numsrc > 0)
487 		l_copy(flist, ilm->ilm_filter);
488 	else
489 		CLEAR_SLIST(ilm->ilm_filter);
490 
491 	ip1dbg(("ilm_update: new if filter mode %d, group %s\n", ilm->ilm_fmode,
492 	    inet_ntop(AF_INET6, &ilm->ilm_v6addr, buf, sizeof (buf))));
493 
494 	l_free(flist);
495 	return (0);
496 }
497 
498 static int
499 ilm_update_del(ilm_t *ilm, boolean_t isv6)
500 {
501 	mcast_record_t fmode;
502 	slist_t *flist;
503 	ill_t *ill = isv6 ? ilm->ilm_ill : ilm->ilm_ipif->ipif_ill;
504 
505 	ip1dbg(("ilm_update_del: still %d left; updating state\n",
506 	    ilm->ilm_refcnt));
507 
508 	if ((flist = l_alloc()) == NULL)
509 		return (ENOMEM);
510 
511 	/*
512 	 * If present, the ilg in question has already either been
513 	 * updated or removed from our list; so all we need to do
514 	 * now is walk the list to update the ilm filter state.
515 	 *
516 	 * Skip the list walk if we have any no-ilg joins, which
517 	 * cause the filter state to revert to (EXCLUDE, NULL).
518 	 */
519 	if (ilm->ilm_no_ilg_cnt != 0) {
520 		fmode = MODE_IS_EXCLUDE;
521 		flist->sl_numsrc = 0;
522 	} else {
523 		ilm_gen_filter(ilm, &fmode, flist);
524 	}
525 
526 	/* check to see if state needs to be updated */
527 	if ((ilm->ilm_fmode == fmode) &&
528 	    (!lists_are_different(ilm->ilm_filter, flist))) {
529 		l_free(flist);
530 		return (0);
531 	}
532 
533 	if (!IS_LOOPBACK(ill)) {
534 		if (isv6)
535 			mld_statechange(ilm, fmode, flist);
536 		else
537 			igmp_statechange(ilm, fmode, flist);
538 	}
539 
540 	ilm->ilm_fmode = fmode;
541 	if (flist->sl_numsrc > 0) {
542 		if (ilm->ilm_filter == NULL) {
543 			ilm->ilm_filter = l_alloc();
544 			if (ilm->ilm_filter == NULL) {
545 				char buf[INET6_ADDRSTRLEN];
546 				ip1dbg(("ilm_update_del: failed to alloc ilm "
547 				    "filter; no source filtering for %s on %s",
548 				    inet_ntop(AF_INET6, &ilm->ilm_v6addr,
549 				    buf, sizeof (buf)), ill->ill_name));
550 				ilm->ilm_fmode = MODE_IS_EXCLUDE;
551 				l_free(flist);
552 				return (0);
553 			}
554 		}
555 		l_copy(flist, ilm->ilm_filter);
556 	} else {
557 		CLEAR_SLIST(ilm->ilm_filter);
558 	}
559 
560 	l_free(flist);
561 	return (0);
562 }
563 
564 /*
565  * INADDR_ANY means all multicast addresses. This is only used
566  * by the multicast router.
567  * INADDR_ANY is stored as IPv6 unspecified addr.
568  */
569 int
570 ip_addmulti(ipaddr_t group, ipif_t *ipif, ilg_stat_t ilgstat,
571     mcast_record_t ilg_fmode, slist_t *ilg_flist)
572 {
573 	ill_t	*ill = ipif->ipif_ill;
574 	ilm_t 	*ilm;
575 	in6_addr_t v6group;
576 	int	ret;
577 
578 	ASSERT(IAM_WRITER_IPIF(ipif));
579 
580 	if (!CLASSD(group) && group != INADDR_ANY)
581 		return (EINVAL);
582 
583 	/*
584 	 * INADDR_ANY is represented as the IPv6 unspecifed addr.
585 	 */
586 	if (group == INADDR_ANY)
587 		v6group = ipv6_all_zeros;
588 	else
589 		IN6_IPADDR_TO_V4MAPPED(group, &v6group);
590 
591 	mutex_enter(&ill->ill_lock);
592 	ilm = ilm_lookup_ipif(ipif, group);
593 	mutex_exit(&ill->ill_lock);
594 	/*
595 	 * Since we are writer, we know the ilm_flags itself cannot
596 	 * change at this point, and ilm_lookup_ipif would not have
597 	 * returned a DELETED ilm. However, the data path can free
598 	 * ilm->next via ilm_walker_cleanup() so we can safely
599 	 * access anything in ilm except ilm_next (for safe access to
600 	 * ilm_next we'd have  to take the ill_lock).
601 	 */
602 	if (ilm != NULL)
603 		return (ilm_update_add(ilm, ilgstat, ilg_flist, B_FALSE));
604 
605 	/*
606 	 * ilms are associated with ipifs in IPv4. It moves with the
607 	 * ipif if the ipif moves to a new ill when the interface
608 	 * fails. Thus we really don't check whether the ipif_ill
609 	 * has failed like in IPv6. If it has FAILED the ipif
610 	 * will move (daemon will move it) and hence the ilm, if the
611 	 * ipif is not IPIF_NOFAILOVER. For the IPIF_NOFAILOVER ipifs,
612 	 * we continue to receive in the same place even if the
613 	 * interface fails.
614 	 */
615 	ilm = ilm_add_v6(ipif, &v6group, ilgstat, ilg_fmode, ilg_flist,
616 	    ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid);
617 	if (ilm == NULL)
618 		return (ENOMEM);
619 
620 	if (group == INADDR_ANY) {
621 		/*
622 		 * Check how many ipif's have members in this group -
623 		 * if more then one we should not tell the driver to join
624 		 * this time
625 		 */
626 		if (ilm_numentries_v6(ill, &v6group) > 1)
627 			return (0);
628 		if (ill->ill_group == NULL)
629 			ret = ip_join_allmulti(ipif);
630 		else
631 			ret = ill_nominate_mcast_rcv(ill->ill_group);
632 		if (ret != 0)
633 			ilm_delete(ilm);
634 		return (ret);
635 	}
636 
637 	if (!IS_LOOPBACK(ill))
638 		igmp_joingroup(ilm);
639 
640 	if (ilm_numentries_v6(ill, &v6group) > 1)
641 		return (0);
642 
643 	ret = ip_ll_addmulti_v6(ipif, &v6group);
644 	if (ret != 0)
645 		ilm_delete(ilm);
646 	return (ret);
647 }
648 
649 /*
650  * The unspecified address means all multicast addresses.
651  * This is only used by the multicast router.
652  *
653  * ill identifies the interface to join on; it may not match the
654  * interface requested by the application of a failover has taken
655  * place.  orig_ifindex always identifies the interface requested
656  * by the app.
657  *
658  * ilgstat tells us if there's an ilg associated with this join,
659  * and if so, if it's a new ilg or a change to an existing one.
660  * ilg_fmode and ilg_flist give us the current filter state of
661  * the ilg (and will be EXCLUDE {NULL} in the case of no ilg).
662  */
663 int
664 ip_addmulti_v6(const in6_addr_t *v6group, ill_t *ill, int orig_ifindex,
665     zoneid_t zoneid, ilg_stat_t ilgstat, mcast_record_t ilg_fmode,
666     slist_t *ilg_flist)
667 {
668 	ilm_t	*ilm;
669 	int	ret;
670 
671 	ASSERT(IAM_WRITER_ILL(ill));
672 
673 	if (!IN6_IS_ADDR_MULTICAST(v6group) &&
674 	    !IN6_IS_ADDR_UNSPECIFIED(v6group)) {
675 		return (EINVAL);
676 	}
677 
678 	/*
679 	 * An ilm is uniquely identified by the tuple of (group, ill,
680 	 * orig_ill).  group is the multicast group address, ill is
681 	 * the interface on which it is currently joined, and orig_ill
682 	 * is the interface on which the application requested the
683 	 * join.  orig_ill and ill are the same unless orig_ill has
684 	 * failed over.
685 	 *
686 	 * Both orig_ill and ill are required, which means we may have
687 	 * 2 ilms on an ill for the same group, but with different
688 	 * orig_ills.  These must be kept separate, so that when failback
689 	 * occurs, the appropriate ilms are moved back to their orig_ill
690 	 * without disrupting memberships on the ill to which they had
691 	 * been moved.
692 	 *
693 	 * In order to track orig_ill, we store orig_ifindex in the
694 	 * ilm and ilg.
695 	 */
696 	mutex_enter(&ill->ill_lock);
697 	ilm = ilm_lookup_ill_index_v6(ill, v6group, orig_ifindex, zoneid);
698 	mutex_exit(&ill->ill_lock);
699 	if (ilm != NULL)
700 		return (ilm_update_add(ilm, ilgstat, ilg_flist, B_TRUE));
701 
702 	/*
703 	 * We need to remember where the application really wanted
704 	 * to join. This will be used later if we want to failback
705 	 * to the original interface.
706 	 */
707 	ilm = ilm_add_v6(ill->ill_ipif, v6group, ilgstat, ilg_fmode,
708 	    ilg_flist, orig_ifindex, zoneid);
709 	if (ilm == NULL)
710 		return (ENOMEM);
711 
712 	if (IN6_IS_ADDR_UNSPECIFIED(v6group)) {
713 		/*
714 		 * Check how many ipif's that have members in this group -
715 		 * if more then one we should not tell the driver to join
716 		 * this time
717 		 */
718 		if (ilm_numentries_v6(ill, v6group) > 1)
719 			return (0);
720 		if (ill->ill_group == NULL)
721 			ret = ip_join_allmulti(ill->ill_ipif);
722 		else
723 			ret = ill_nominate_mcast_rcv(ill->ill_group);
724 
725 		if (ret != 0)
726 			ilm_delete(ilm);
727 		return (ret);
728 	}
729 
730 	if (!IS_LOOPBACK(ill))
731 		mld_joingroup(ilm);
732 
733 	/*
734 	 * If we have more then one we should not tell the driver
735 	 * to join this time.
736 	 */
737 	if (ilm_numentries_v6(ill, v6group) > 1)
738 		return (0);
739 
740 	ret = ip_ll_addmulti_v6(ill->ill_ipif, v6group);
741 	if (ret != 0)
742 		ilm_delete(ilm);
743 	return (ret);
744 }
745 
746 /*
747  * Send a multicast request to the driver for enabling multicast reception
748  * for v6groupp address. The caller has already checked whether it is
749  * appropriate to send one or not.
750  */
751 int
752 ip_ll_send_enabmulti_req(ill_t *ill, const in6_addr_t *v6groupp)
753 {
754 	mblk_t	*mp;
755 	uint32_t addrlen, addroff;
756 	char	group_buf[INET6_ADDRSTRLEN];
757 
758 	ASSERT(IAM_WRITER_ILL(ill));
759 
760 	/*
761 	 * Create a AR_ENTRY_SQUERY message with a dl_enabmulti_req tacked
762 	 * on.
763 	 */
764 	mp = ill_create_dl(ill, DL_ENABMULTI_REQ, sizeof (dl_enabmulti_req_t),
765 	    &addrlen, &addroff);
766 	if (!mp)
767 		return (ENOMEM);
768 	if (IN6_IS_ADDR_V4MAPPED(v6groupp)) {
769 		ipaddr_t v4group;
770 
771 		IN6_V4MAPPED_TO_IPADDR(v6groupp, v4group);
772 		/*
773 		 * NOTE!!!
774 		 * The "addroff" passed in here was calculated by
775 		 * ill_create_dl(), and will be used by ill_create_squery()
776 		 * to perform some twisted coding magic. It is the offset
777 		 * into the dl_xxx_req of the hw addr. Here, it will be
778 		 * added to b_wptr - b_rptr to create a magic number that
779 		 * is not an offset into this squery mblk.
780 		 * The actual hardware address will be accessed only in the
781 		 * dl_xxx_req, not in the squery. More importantly,
782 		 * that hardware address can *only* be accessed in this
783 		 * mblk chain by calling mi_offset_param_c(), which uses
784 		 * the magic number in the squery hw offset field to go
785 		 * to the *next* mblk (the dl_xxx_req), subtract the
786 		 * (b_wptr - b_rptr), and find the actual offset into
787 		 * the dl_xxx_req.
788 		 * Any method that depends on using the
789 		 * offset field in the dl_disabmulti_req or squery
790 		 * to find either hardware address will similarly fail.
791 		 *
792 		 * Look in ar_entry_squery() in arp.c to see how this offset
793 		 * is used.
794 		 */
795 		mp = ill_create_squery(ill, v4group, addrlen, addroff, mp);
796 		if (!mp)
797 			return (ENOMEM);
798 		ip1dbg(("ip_ll_send_enabmulti_req: IPv4 putnext %s on %s\n",
799 		    inet_ntop(AF_INET6, v6groupp, group_buf,
800 		    sizeof (group_buf)),
801 		    ill->ill_name));
802 		putnext(ill->ill_rq, mp);
803 	} else {
804 		ip1dbg(("ip_ll_send_enabmulti_req: IPv6 ndp_mcastreq %s on"
805 		    " %s\n",
806 		    inet_ntop(AF_INET6, v6groupp, group_buf,
807 		    sizeof (group_buf)),
808 		    ill->ill_name));
809 		return (ndp_mcastreq(ill, v6groupp, addrlen, addroff, mp));
810 	}
811 	return (0);
812 }
813 
814 /*
815  * Send a multicast request to the driver for enabling multicast
816  * membership for v6group if appropriate.
817  */
818 static int
819 ip_ll_addmulti_v6(ipif_t *ipif, const in6_addr_t *v6groupp)
820 {
821 	ill_t	*ill = ipif->ipif_ill;
822 
823 	ASSERT(IAM_WRITER_IPIF(ipif));
824 
825 	if (ill->ill_net_type != IRE_IF_RESOLVER ||
826 	    ipif->ipif_flags & IPIF_POINTOPOINT) {
827 		ip1dbg(("ip_ll_addmulti_v6: not resolver\n"));
828 		return (0);	/* Must be IRE_IF_NORESOLVER */
829 	}
830 
831 	if (ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
832 		ip1dbg(("ip_ll_addmulti_v6: MULTI_BCAST\n"));
833 		return (0);
834 	}
835 	if (!ill->ill_dl_up) {
836 		/*
837 		 * Nobody there. All multicast addresses will be re-joined
838 		 * when we get the DL_BIND_ACK bringing the interface up.
839 		 */
840 		ip1dbg(("ip_ll_addmulti_v6: nobody up\n"));
841 		return (0);
842 	}
843 	return (ip_ll_send_enabmulti_req(ill, v6groupp));
844 }
845 
846 /*
847  * INADDR_ANY means all multicast addresses. This is only used
848  * by the multicast router.
849  * INADDR_ANY is stored as the IPv6 unspecifed addr.
850  */
851 int
852 ip_delmulti(ipaddr_t group, ipif_t *ipif, boolean_t no_ilg, boolean_t leaving)
853 {
854 	ill_t	*ill = ipif->ipif_ill;
855 	ilm_t *ilm;
856 	in6_addr_t v6group;
857 	int	ret;
858 
859 	ASSERT(IAM_WRITER_IPIF(ipif));
860 
861 	if (!CLASSD(group) && group != INADDR_ANY)
862 		return (EINVAL);
863 
864 	/*
865 	 * INADDR_ANY is represented as the IPv6 unspecifed addr.
866 	 */
867 	if (group == INADDR_ANY)
868 		v6group = ipv6_all_zeros;
869 	else
870 		IN6_IPADDR_TO_V4MAPPED(group, &v6group);
871 
872 	/*
873 	 * Look for a match on the ipif.
874 	 * (IP_DROP_MEMBERSHIP specifies an ipif using an IP address).
875 	 */
876 	mutex_enter(&ill->ill_lock);
877 	ilm = ilm_lookup_ipif(ipif, group);
878 	mutex_exit(&ill->ill_lock);
879 	if (ilm == NULL)
880 		return (ENOENT);
881 
882 	/* Update counters */
883 	if (no_ilg)
884 		ilm->ilm_no_ilg_cnt--;
885 
886 	if (leaving)
887 		ilm->ilm_refcnt--;
888 
889 	if (ilm->ilm_refcnt > 0)
890 		return (ilm_update_del(ilm, B_FALSE));
891 
892 	if (group == INADDR_ANY) {
893 		ilm_delete(ilm);
894 		/*
895 		 * Check how many ipif's that have members in this group -
896 		 * if there are still some left then don't tell the driver
897 		 * to drop it.
898 		 */
899 		if (ilm_numentries_v6(ill, &v6group) != 0)
900 			return (0);
901 
902 		/*
903 		 * If we never joined, then don't leave.  This can happen
904 		 * if we're in an IPMP group, since only one ill per IPMP
905 		 * group receives all multicast packets.
906 		 */
907 		if (!ill->ill_join_allmulti) {
908 			ASSERT(ill->ill_group != NULL);
909 			return (0);
910 		}
911 
912 		ret = ip_leave_allmulti(ipif);
913 		if (ill->ill_group != NULL)
914 			(void) ill_nominate_mcast_rcv(ill->ill_group);
915 		return (ret);
916 	}
917 
918 	if (!IS_LOOPBACK(ill))
919 		igmp_leavegroup(ilm);
920 
921 	ilm_delete(ilm);
922 	/*
923 	 * Check how many ipif's that have members in this group -
924 	 * if there are still some left then don't tell the driver
925 	 * to drop it.
926 	 */
927 	if (ilm_numentries_v6(ill, &v6group) != 0)
928 		return (0);
929 	return (ip_ll_delmulti_v6(ipif, &v6group));
930 }
931 
932 /*
933  * The unspecified address means all multicast addresses.
934  * This is only used by the multicast router.
935  */
936 int
937 ip_delmulti_v6(const in6_addr_t *v6group, ill_t *ill, int orig_ifindex,
938     zoneid_t zoneid, boolean_t no_ilg, boolean_t leaving)
939 {
940 	ipif_t	*ipif;
941 	ilm_t *ilm;
942 	int	ret;
943 
944 	ASSERT(IAM_WRITER_ILL(ill));
945 
946 	if (!IN6_IS_ADDR_MULTICAST(v6group) &&
947 	    !IN6_IS_ADDR_UNSPECIFIED(v6group))
948 		return (EINVAL);
949 
950 	/*
951 	 * Look for a match on the ill.
952 	 * (IPV6_LEAVE_GROUP specifies an ill using an ifindex).
953 	 *
954 	 * Similar to ip_addmulti_v6, we should always look using
955 	 * the orig_ifindex.
956 	 *
957 	 * 1) If orig_ifindex is different from ill's ifindex
958 	 *    we should have an ilm with orig_ifindex created in
959 	 *    ip_addmulti_v6. We should delete that here.
960 	 *
961 	 * 2) If orig_ifindex is same as ill's ifindex, we should
962 	 *    not delete the ilm that is temporarily here because of
963 	 *    a FAILOVER. Those ilms will have a ilm_orig_ifindex
964 	 *    different from ill's ifindex.
965 	 *
966 	 * Thus, always lookup using orig_ifindex.
967 	 */
968 	mutex_enter(&ill->ill_lock);
969 	ilm = ilm_lookup_ill_index_v6(ill, v6group, orig_ifindex, zoneid);
970 	mutex_exit(&ill->ill_lock);
971 	if (ilm == NULL)
972 		return (ENOENT);
973 
974 	ASSERT(ilm->ilm_ill == ill);
975 
976 	ipif = ill->ill_ipif;
977 
978 	/* Update counters */
979 	if (no_ilg)
980 		ilm->ilm_no_ilg_cnt--;
981 
982 	if (leaving)
983 		ilm->ilm_refcnt--;
984 
985 	if (ilm->ilm_refcnt > 0)
986 		return (ilm_update_del(ilm, B_TRUE));
987 
988 	if (IN6_IS_ADDR_UNSPECIFIED(v6group)) {
989 		ilm_delete(ilm);
990 		/*
991 		 * Check how many ipif's that have members in this group -
992 		 * if there are still some left then don't tell the driver
993 		 * to drop it.
994 		 */
995 		if (ilm_numentries_v6(ill, v6group) != 0)
996 			return (0);
997 
998 		/*
999 		 * If we never joined, then don't leave.  This can happen
1000 		 * if we're in an IPMP group, since only one ill per IPMP
1001 		 * group receives all multicast packets.
1002 		 */
1003 		if (!ill->ill_join_allmulti) {
1004 			ASSERT(ill->ill_group != NULL);
1005 			return (0);
1006 		}
1007 
1008 		ret = ip_leave_allmulti(ipif);
1009 		if (ill->ill_group != NULL)
1010 			(void) ill_nominate_mcast_rcv(ill->ill_group);
1011 		return (ret);
1012 	}
1013 
1014 	if (!IS_LOOPBACK(ill))
1015 		mld_leavegroup(ilm);
1016 
1017 	ilm_delete(ilm);
1018 	/*
1019 	 * Check how many ipif's that have members in this group -
1020 	 * if there are still some left then don't tell the driver
1021 	 * to drop it.
1022 	 */
1023 	if (ilm_numentries_v6(ill, v6group) != 0)
1024 		return (0);
1025 	return (ip_ll_delmulti_v6(ipif, v6group));
1026 }
1027 
1028 /*
1029  * Send a multicast request to the driver for disabling multicast reception
1030  * for v6groupp address. The caller has already checked whether it is
1031  * appropriate to send one or not.
1032  */
1033 int
1034 ip_ll_send_disabmulti_req(ill_t *ill, const in6_addr_t *v6groupp)
1035 {
1036 	mblk_t	*mp;
1037 	char	group_buf[INET6_ADDRSTRLEN];
1038 	uint32_t	addrlen, addroff;
1039 
1040 	ASSERT(IAM_WRITER_ILL(ill));
1041 	/*
1042 	 * Create a AR_ENTRY_SQUERY message with a dl_disabmulti_req tacked
1043 	 * on.
1044 	 */
1045 	mp = ill_create_dl(ill, DL_DISABMULTI_REQ,
1046 	    sizeof (dl_disabmulti_req_t), &addrlen, &addroff);
1047 
1048 	if (!mp)
1049 		return (ENOMEM);
1050 
1051 	if (IN6_IS_ADDR_V4MAPPED(v6groupp)) {
1052 		ipaddr_t v4group;
1053 
1054 		IN6_V4MAPPED_TO_IPADDR(v6groupp, v4group);
1055 		/*
1056 		 * NOTE!!!
1057 		 * The "addroff" passed in here was calculated by
1058 		 * ill_create_dl(), and will be used by ill_create_squery()
1059 		 * to perform some twisted coding magic. It is the offset
1060 		 * into the dl_xxx_req of the hw addr. Here, it will be
1061 		 * added to b_wptr - b_rptr to create a magic number that
1062 		 * is not an offset into this mblk.
1063 		 *
1064 		 * Please see the comment in ip_ll_send)enabmulti_req()
1065 		 * for a complete explanation.
1066 		 *
1067 		 * Look in ar_entry_squery() in arp.c to see how this offset
1068 		 * is used.
1069 		 */
1070 		mp = ill_create_squery(ill, v4group, addrlen, addroff, mp);
1071 		if (!mp)
1072 			return (ENOMEM);
1073 		ip1dbg(("ip_ll_send_disabmulti_req: IPv4 putnext %s on %s\n",
1074 		    inet_ntop(AF_INET6, v6groupp, group_buf,
1075 		    sizeof (group_buf)),
1076 		    ill->ill_name));
1077 		putnext(ill->ill_rq, mp);
1078 	} else {
1079 		ip1dbg(("ip_ll_send_disabmulti_req: IPv6 ndp_mcastreq %s on"
1080 		    " %s\n",
1081 		    inet_ntop(AF_INET6, v6groupp, group_buf,
1082 		    sizeof (group_buf)),
1083 		    ill->ill_name));
1084 		return (ndp_mcastreq(ill, v6groupp, addrlen, addroff, mp));
1085 	}
1086 	return (0);
1087 }
1088 
1089 /*
1090  * Send a multicast request to the driver for disabling multicast
1091  * membership for v6group if appropriate.
1092  */
1093 static int
1094 ip_ll_delmulti_v6(ipif_t *ipif, const in6_addr_t *v6group)
1095 {
1096 	ill_t	*ill = ipif->ipif_ill;
1097 
1098 	ASSERT(IAM_WRITER_IPIF(ipif));
1099 
1100 	if (ill->ill_net_type != IRE_IF_RESOLVER ||
1101 	    ipif->ipif_flags & IPIF_POINTOPOINT) {
1102 		return (0);	/* Must be IRE_IF_NORESOLVER */
1103 	}
1104 	if (ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) {
1105 		ip1dbg(("ip_ll_delmulti_v6: MULTI_BCAST\n"));
1106 		return (0);
1107 	}
1108 	if (!ill->ill_dl_up) {
1109 		/*
1110 		 * Nobody there. All multicast addresses will be re-joined
1111 		 * when we get the DL_BIND_ACK bringing the interface up.
1112 		 */
1113 		ip1dbg(("ip_ll_delmulti_v6: nobody up\n"));
1114 		return (0);
1115 	}
1116 	return (ip_ll_send_disabmulti_req(ill, v6group));
1117 }
1118 
1119 /*
1120  * Make the driver pass up all multicast packets
1121  *
1122  * With ill groups, the caller makes sure that there is only
1123  * one ill joining the allmulti group.
1124  */
1125 int
1126 ip_join_allmulti(ipif_t *ipif)
1127 {
1128 	ill_t	*ill = ipif->ipif_ill;
1129 	mblk_t	*mp;
1130 	uint32_t	addrlen, addroff;
1131 
1132 	ASSERT(IAM_WRITER_IPIF(ipif));
1133 
1134 	if (!ill->ill_dl_up) {
1135 		/*
1136 		 * Nobody there. All multicast addresses will be re-joined
1137 		 * when we get the DL_BIND_ACK bringing the interface up.
1138 		 */
1139 		return (0);
1140 	}
1141 
1142 	ASSERT(!ill->ill_join_allmulti);
1143 
1144 	/*
1145 	 * Create a DL_PROMISCON_REQ message and send it directly to
1146 	 * the DLPI provider.  We don't need to do this for certain
1147 	 * media types for which we never need to turn promiscuous
1148 	 * mode on.
1149 	 */
1150 	if ((ill->ill_net_type == IRE_IF_RESOLVER) &&
1151 	    !(ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST)) {
1152 		mp = ill_create_dl(ill, DL_PROMISCON_REQ,
1153 		    sizeof (dl_promiscon_req_t), &addrlen, &addroff);
1154 		if (mp == NULL)
1155 			return (ENOMEM);
1156 		ill_dlpi_send(ill, mp);
1157 	}
1158 
1159 	ill->ill_join_allmulti = B_TRUE;
1160 	return (0);
1161 }
1162 
1163 /*
1164  * Make the driver stop passing up all multicast packets
1165  *
1166  * With ill groups, we need to nominate some other ill as
1167  * this ipif->ipif_ill is leaving the group.
1168  */
1169 int
1170 ip_leave_allmulti(ipif_t *ipif)
1171 {
1172 	ill_t	*ill = ipif->ipif_ill;
1173 	mblk_t	*mp;
1174 	uint32_t	addrlen, addroff;
1175 
1176 	ASSERT(IAM_WRITER_IPIF(ipif));
1177 
1178 	if (!ill->ill_dl_up) {
1179 		/*
1180 		 * Nobody there. All multicast addresses will be re-joined
1181 		 * when we get the DL_BIND_ACK bringing the interface up.
1182 		 */
1183 		return (0);
1184 	}
1185 
1186 	ASSERT(ill->ill_join_allmulti);
1187 
1188 	/*
1189 	 * Create a DL_PROMISCOFF_REQ message and send it directly to
1190 	 * the DLPI provider.  We don't need to do this for certain
1191 	 * media types for which we never need to turn promiscuous
1192 	 * mode on.
1193 	 */
1194 	if ((ill->ill_net_type == IRE_IF_RESOLVER) &&
1195 	    !(ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST)) {
1196 		mp = ill_create_dl(ill, DL_PROMISCOFF_REQ,
1197 		    sizeof (dl_promiscoff_req_t), &addrlen, &addroff);
1198 		if (mp == NULL)
1199 			return (ENOMEM);
1200 		ill_dlpi_send(ill, mp);
1201 	}
1202 
1203 	ill->ill_join_allmulti = B_FALSE;
1204 	return (0);
1205 }
1206 
1207 /*
1208  * Copy mp_orig and pass it in as a local message.
1209  */
1210 void
1211 ip_multicast_loopback(queue_t *q, ill_t *ill, mblk_t *mp_orig, int fanout_flags,
1212     zoneid_t zoneid)
1213 {
1214 	mblk_t	*mp;
1215 	mblk_t	*ipsec_mp;
1216 	ipha_t	*iph;
1217 	ip_stack_t *ipst = ill->ill_ipst;
1218 
1219 	if (DB_TYPE(mp_orig) == M_DATA &&
1220 	    ((ipha_t *)mp_orig->b_rptr)->ipha_protocol == IPPROTO_UDP) {
1221 		uint_t hdrsz;
1222 
1223 		hdrsz = IPH_HDR_LENGTH((ipha_t *)mp_orig->b_rptr) +
1224 		    sizeof (udpha_t);
1225 		ASSERT(MBLKL(mp_orig) >= hdrsz);
1226 
1227 		if (((mp = allocb(hdrsz, BPRI_MED)) != NULL) &&
1228 		    (mp_orig = dupmsg(mp_orig)) != NULL) {
1229 			bcopy(mp_orig->b_rptr, mp->b_rptr, hdrsz);
1230 			mp->b_wptr += hdrsz;
1231 			mp->b_cont = mp_orig;
1232 			mp_orig->b_rptr += hdrsz;
1233 			if (is_system_labeled() && DB_CRED(mp_orig) != NULL)
1234 				mblk_setcred(mp, DB_CRED(mp_orig));
1235 			if (MBLKL(mp_orig) == 0) {
1236 				mp->b_cont = mp_orig->b_cont;
1237 				mp_orig->b_cont = NULL;
1238 				freeb(mp_orig);
1239 			}
1240 		} else if (mp != NULL) {
1241 			freeb(mp);
1242 			mp = NULL;
1243 		}
1244 	} else {
1245 		mp = ip_copymsg(mp_orig); /* No refcnt on ipsec_out netstack */
1246 	}
1247 
1248 	if (mp == NULL)
1249 		return;
1250 	if (DB_TYPE(mp) == M_CTL) {
1251 		ipsec_mp = mp;
1252 		mp = mp->b_cont;
1253 	} else {
1254 		ipsec_mp = mp;
1255 	}
1256 
1257 	iph = (ipha_t *)mp->b_rptr;
1258 
1259 	DTRACE_PROBE4(ip4__loopback__out__start,
1260 	    ill_t *, NULL, ill_t *, ill,
1261 	    ipha_t *, iph, mblk_t *, ipsec_mp);
1262 
1263 	FW_HOOKS(ipst->ips_ip4_loopback_out_event,
1264 	    ipst->ips_ipv4firewall_loopback_out,
1265 	    NULL, ill, iph, ipsec_mp, mp, HPE_MULTICAST, ipst);
1266 
1267 	DTRACE_PROBE1(ip4__loopback__out__end, mblk_t *, ipsec_mp);
1268 
1269 	if (ipsec_mp != NULL)
1270 		ip_wput_local(q, ill, iph, ipsec_mp, NULL,
1271 		    fanout_flags, zoneid);
1272 }
1273 
1274 static area_t	ip_aresq_template = {
1275 	AR_ENTRY_SQUERY,		/* cmd */
1276 	sizeof (area_t)+IP_ADDR_LEN,	/* name offset */
1277 	sizeof (area_t),	/* name len (filled by ill_arp_alloc) */
1278 	IP_ARP_PROTO_TYPE,		/* protocol, from arps perspective */
1279 	sizeof (area_t),			/* proto addr offset */
1280 	IP_ADDR_LEN,			/* proto addr_length */
1281 	0,				/* proto mask offset */
1282 	/* Rest is initialized when used */
1283 	0,				/* flags */
1284 	0,				/* hw addr offset */
1285 	0,				/* hw addr length */
1286 };
1287 
1288 static mblk_t *
1289 ill_create_squery(ill_t *ill, ipaddr_t ipaddr, uint32_t addrlen,
1290     uint32_t addroff, mblk_t *mp_tail)
1291 {
1292 	mblk_t	*mp;
1293 	area_t	*area;
1294 
1295 	mp = ill_arp_alloc(ill, (uchar_t *)&ip_aresq_template,
1296 	    (caddr_t)&ipaddr);
1297 	if (!mp) {
1298 		freemsg(mp_tail);
1299 		return (NULL);
1300 	}
1301 	area = (area_t *)mp->b_rptr;
1302 	area->area_hw_addr_length = addrlen;
1303 	area->area_hw_addr_offset = mp->b_wptr - mp->b_rptr + addroff;
1304 	/*
1305 	 * NOTE!
1306 	 *
1307 	 * The area_hw_addr_offset, as can be seen, does not hold the
1308 	 * actual hardware address offset. Rather, it holds the offset
1309 	 * to the hw addr in the dl_xxx_req in mp_tail, modified by
1310 	 * adding (mp->b_wptr - mp->b_rptr). This allows the function
1311 	 * mi_offset_paramc() to find the hardware address in the
1312 	 * *second* mblk (dl_xxx_req), not this mblk.
1313 	 *
1314 	 * Using mi_offset_paramc() is thus the *only* way to access
1315 	 * the dl_xxx_hw address.
1316 	 *
1317 	 * The squery hw address should *not* be accessed.
1318 	 *
1319 	 * See ar_entry_squery() in arp.c for an example of how all this works.
1320 	 */
1321 
1322 	mp->b_cont = mp_tail;
1323 	return (mp);
1324 }
1325 
1326 /*
1327  * Create a dlpi message with room for phys+sap. When we come back in
1328  * ip_wput_ctl() we will strip the sap for those primitives which
1329  * only need a physical address.
1330  */
1331 static mblk_t *
1332 ill_create_dl(ill_t *ill, uint32_t dl_primitive, uint32_t length,
1333     uint32_t *addr_lenp, uint32_t *addr_offp)
1334 {
1335 	mblk_t	*mp;
1336 	uint32_t	hw_addr_length;
1337 	char		*cp;
1338 	uint32_t	offset;
1339 	uint32_t 	size;
1340 
1341 	*addr_lenp = *addr_offp = 0;
1342 
1343 	hw_addr_length = ill->ill_phys_addr_length;
1344 	if (!hw_addr_length) {
1345 		ip0dbg(("ip_create_dl: hw addr length = 0\n"));
1346 		return (NULL);
1347 	}
1348 
1349 	size = length;
1350 	switch (dl_primitive) {
1351 	case DL_ENABMULTI_REQ:
1352 	case DL_DISABMULTI_REQ:
1353 		size += hw_addr_length;
1354 		break;
1355 	case DL_PROMISCON_REQ:
1356 	case DL_PROMISCOFF_REQ:
1357 		break;
1358 	default:
1359 		return (NULL);
1360 	}
1361 	mp = allocb(size, BPRI_HI);
1362 	if (!mp)
1363 		return (NULL);
1364 	mp->b_wptr += size;
1365 	mp->b_datap->db_type = M_PROTO;
1366 
1367 	cp = (char *)mp->b_rptr;
1368 	offset = length;
1369 
1370 	switch (dl_primitive) {
1371 	case DL_ENABMULTI_REQ: {
1372 		dl_enabmulti_req_t *dl = (dl_enabmulti_req_t *)cp;
1373 
1374 		dl->dl_primitive = dl_primitive;
1375 		dl->dl_addr_offset = offset;
1376 		*addr_lenp = dl->dl_addr_length = hw_addr_length;
1377 		*addr_offp = offset;
1378 		break;
1379 	}
1380 	case DL_DISABMULTI_REQ: {
1381 		dl_disabmulti_req_t *dl = (dl_disabmulti_req_t *)cp;
1382 
1383 		dl->dl_primitive = dl_primitive;
1384 		dl->dl_addr_offset = offset;
1385 		*addr_lenp = dl->dl_addr_length = hw_addr_length;
1386 		*addr_offp = offset;
1387 		break;
1388 	}
1389 	case DL_PROMISCON_REQ:
1390 	case DL_PROMISCOFF_REQ: {
1391 		dl_promiscon_req_t *dl = (dl_promiscon_req_t *)cp;
1392 
1393 		dl->dl_primitive = dl_primitive;
1394 		dl->dl_level = DL_PROMISC_MULTI;
1395 		break;
1396 	}
1397 	}
1398 	ip1dbg(("ill_create_dl: addr_len %d, addr_off %d\n",
1399 	    *addr_lenp, *addr_offp));
1400 	return (mp);
1401 }
1402 
1403 void
1404 ip_wput_ctl(queue_t *q, mblk_t *mp_orig)
1405 {
1406 	ill_t	*ill = (ill_t *)q->q_ptr;
1407 	mblk_t	*mp = mp_orig;
1408 	area_t	*area = (area_t *)mp->b_rptr;
1409 
1410 	/* Check that we have a AR_ENTRY_SQUERY with a tacked on mblk */
1411 	if (MBLKL(mp) < sizeof (area_t) || mp->b_cont == NULL ||
1412 	    area->area_cmd != AR_ENTRY_SQUERY) {
1413 		putnext(q, mp);
1414 		return;
1415 	}
1416 	mp = mp->b_cont;
1417 
1418 	/*
1419 	 * Update dl_addr_length and dl_addr_offset for primitives that
1420 	 * have physical addresses as opposed to full saps
1421 	 */
1422 	switch (((union DL_primitives *)mp->b_rptr)->dl_primitive) {
1423 	case DL_ENABMULTI_REQ:
1424 		/* Track the state if this is the first enabmulti */
1425 		if (ill->ill_dlpi_multicast_state == IDS_UNKNOWN)
1426 			ill->ill_dlpi_multicast_state = IDS_INPROGRESS;
1427 		ip1dbg(("ip_wput_ctl: ENABMULTI\n"));
1428 		break;
1429 	case DL_DISABMULTI_REQ:
1430 		ip1dbg(("ip_wput_ctl: DISABMULTI\n"));
1431 		break;
1432 	default:
1433 		ip1dbg(("ip_wput_ctl: default\n"));
1434 		break;
1435 	}
1436 	freeb(mp_orig);
1437 	ill_dlpi_send(ill, mp);
1438 }
1439 
1440 /*
1441  * Rejoin any groups which have been explicitly joined by the application (we
1442  * left all explicitly joined groups as part of ill_leave_multicast() prior to
1443  * bringing the interface down).  Note that because groups can be joined and
1444  * left while an interface is down, this may not be the same set of groups
1445  * that we left in ill_leave_multicast().
1446  */
1447 void
1448 ill_recover_multicast(ill_t *ill)
1449 {
1450 	ilm_t	*ilm;
1451 	char    addrbuf[INET6_ADDRSTRLEN];
1452 
1453 	ASSERT(IAM_WRITER_ILL(ill));
1454 	ILM_WALKER_HOLD(ill);
1455 	for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
1456 		/*
1457 		 * Check how many ipif's that have members in this group -
1458 		 * if more then one we make sure that this entry is first
1459 		 * in the list.
1460 		 */
1461 		if (ilm_numentries_v6(ill, &ilm->ilm_v6addr) > 1 &&
1462 		    ilm_lookup_ill_v6(ill, &ilm->ilm_v6addr, ALL_ZONES) != ilm)
1463 			continue;
1464 		ip1dbg(("ill_recover_multicast: %s\n",
1465 		    inet_ntop(AF_INET6, &ilm->ilm_v6addr, addrbuf,
1466 		    sizeof (addrbuf))));
1467 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
1468 			if (ill->ill_group == NULL) {
1469 				(void) ip_join_allmulti(ill->ill_ipif);
1470 			} else {
1471 				/*
1472 				 * We don't want to join on this ill,
1473 				 * if somebody else in the group has
1474 				 * already been nominated.
1475 				 */
1476 				(void) ill_nominate_mcast_rcv(ill->ill_group);
1477 			}
1478 		} else {
1479 			(void) ip_ll_addmulti_v6(ill->ill_ipif,
1480 			    &ilm->ilm_v6addr);
1481 		}
1482 	}
1483 	ILM_WALKER_RELE(ill);
1484 }
1485 
1486 /*
1487  * The opposite of ill_recover_multicast() -- leaves all multicast groups
1488  * that were explicitly joined.  Note that both these functions could be
1489  * disposed of if we enhanced ARP to allow us to handle DL_DISABMULTI_REQ
1490  * and DL_ENABMULTI_REQ messages when an interface is down.
1491  */
1492 void
1493 ill_leave_multicast(ill_t *ill)
1494 {
1495 	ilm_t	*ilm;
1496 	char    addrbuf[INET6_ADDRSTRLEN];
1497 
1498 	ASSERT(IAM_WRITER_ILL(ill));
1499 	ILM_WALKER_HOLD(ill);
1500 	for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
1501 		/*
1502 		 * Check how many ipif's that have members in this group -
1503 		 * if more then one we make sure that this entry is first
1504 		 * in the list.
1505 		 */
1506 		if (ilm_numentries_v6(ill, &ilm->ilm_v6addr) > 1 &&
1507 		    ilm_lookup_ill_v6(ill, &ilm->ilm_v6addr, ALL_ZONES) != ilm)
1508 			continue;
1509 		ip1dbg(("ill_leave_multicast: %s\n",
1510 		    inet_ntop(AF_INET6, &ilm->ilm_v6addr, addrbuf,
1511 		    sizeof (addrbuf))));
1512 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) {
1513 			(void) ip_leave_allmulti(ill->ill_ipif);
1514 			/*
1515 			 * If we were part of an IPMP group, then
1516 			 * ill_handoff_responsibility() has already
1517 			 * nominated a new member (so we don't).
1518 			 */
1519 			ASSERT(ill->ill_group == NULL);
1520 		} else {
1521 			(void) ip_ll_delmulti_v6(ill->ill_ipif,
1522 			    &ilm->ilm_v6addr);
1523 		}
1524 	}
1525 	ILM_WALKER_RELE(ill);
1526 }
1527 
1528 /* Find an ilm for matching the ill */
1529 ilm_t *
1530 ilm_lookup_ill(ill_t *ill, ipaddr_t group, zoneid_t zoneid)
1531 {
1532 	in6_addr_t	v6group;
1533 
1534 	ASSERT(ill->ill_ilm_walker_cnt != 0 || MUTEX_HELD(&ill->ill_lock));
1535 	/*
1536 	 * INADDR_ANY is represented as the IPv6 unspecifed addr.
1537 	 */
1538 	if (group == INADDR_ANY)
1539 		v6group = ipv6_all_zeros;
1540 	else
1541 		IN6_IPADDR_TO_V4MAPPED(group, &v6group);
1542 
1543 	return (ilm_lookup_ill_v6(ill, &v6group, zoneid));
1544 }
1545 
1546 /*
1547  * Find an ilm for matching the ill. All the ilm lookup functions
1548  * ignore ILM_DELETED ilms. These have been logically deleted, and
1549  * igmp and linklayer disable multicast have been done. Only mi_free
1550  * yet to be done. Still there in the list due to ilm_walkers. The
1551  * last walker will release it.
1552  */
1553 ilm_t *
1554 ilm_lookup_ill_v6(ill_t *ill, const in6_addr_t *v6group, zoneid_t zoneid)
1555 {
1556 	ilm_t	*ilm;
1557 
1558 	ASSERT(ill->ill_ilm_walker_cnt != 0 || MUTEX_HELD(&ill->ill_lock));
1559 
1560 	for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
1561 		if (ilm->ilm_flags & ILM_DELETED)
1562 			continue;
1563 		if (IN6_ARE_ADDR_EQUAL(&ilm->ilm_v6addr, v6group) &&
1564 		    (zoneid == ALL_ZONES || zoneid == ilm->ilm_zoneid))
1565 			return (ilm);
1566 	}
1567 	return (NULL);
1568 }
1569 
1570 ilm_t *
1571 ilm_lookup_ill_index_v6(ill_t *ill, const in6_addr_t *v6group, int index,
1572     zoneid_t zoneid)
1573 {
1574 	ilm_t *ilm;
1575 
1576 	ASSERT(ill->ill_ilm_walker_cnt != 0 || MUTEX_HELD(&ill->ill_lock));
1577 
1578 	for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
1579 		if (ilm->ilm_flags & ILM_DELETED)
1580 			continue;
1581 		if (IN6_ARE_ADDR_EQUAL(&ilm->ilm_v6addr, v6group) &&
1582 		    (zoneid == ALL_ZONES || zoneid == ilm->ilm_zoneid) &&
1583 		    ilm->ilm_orig_ifindex == index) {
1584 			return (ilm);
1585 		}
1586 	}
1587 	return (NULL);
1588 }
1589 
1590 
1591 /*
1592  * Found an ilm for the ipif. Only needed for IPv4 which does
1593  * ipif specific socket options.
1594  */
1595 ilm_t *
1596 ilm_lookup_ipif(ipif_t *ipif, ipaddr_t group)
1597 {
1598 	ill_t	*ill = ipif->ipif_ill;
1599 	ilm_t	*ilm;
1600 	in6_addr_t	v6group;
1601 
1602 	ASSERT(ill->ill_ilm_walker_cnt != 0 || MUTEX_HELD(&ill->ill_lock));
1603 	/*
1604 	 * INADDR_ANY is represented as the IPv6 unspecifed addr.
1605 	 */
1606 	if (group == INADDR_ANY)
1607 		v6group = ipv6_all_zeros;
1608 	else
1609 		IN6_IPADDR_TO_V4MAPPED(group, &v6group);
1610 
1611 	for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
1612 		if (ilm->ilm_flags & ILM_DELETED)
1613 			continue;
1614 		if (ilm->ilm_ipif == ipif &&
1615 		    IN6_ARE_ADDR_EQUAL(&ilm->ilm_v6addr, &v6group))
1616 			return (ilm);
1617 	}
1618 	return (NULL);
1619 }
1620 
1621 /*
1622  * How many members on this ill?
1623  */
1624 int
1625 ilm_numentries_v6(ill_t *ill, const in6_addr_t *v6group)
1626 {
1627 	ilm_t	*ilm;
1628 	int i = 0;
1629 
1630 	mutex_enter(&ill->ill_lock);
1631 	for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
1632 		if (ilm->ilm_flags & ILM_DELETED)
1633 			continue;
1634 		if (IN6_ARE_ADDR_EQUAL(&ilm->ilm_v6addr, v6group)) {
1635 			i++;
1636 		}
1637 	}
1638 	mutex_exit(&ill->ill_lock);
1639 	return (i);
1640 }
1641 
1642 /* Caller guarantees that the group is not already on the list */
1643 static ilm_t *
1644 ilm_add_v6(ipif_t *ipif, const in6_addr_t *v6group, ilg_stat_t ilgstat,
1645     mcast_record_t ilg_fmode, slist_t *ilg_flist, int orig_ifindex,
1646     zoneid_t zoneid)
1647 {
1648 	ill_t	*ill = ipif->ipif_ill;
1649 	ilm_t	*ilm;
1650 	ilm_t	*ilm_cur;
1651 	ilm_t	**ilm_ptpn;
1652 
1653 	ASSERT(IAM_WRITER_IPIF(ipif));
1654 
1655 	ilm = GETSTRUCT(ilm_t, 1);
1656 	if (ilm == NULL)
1657 		return (NULL);
1658 	if (ilgstat != ILGSTAT_NONE && !SLIST_IS_EMPTY(ilg_flist)) {
1659 		ilm->ilm_filter = l_alloc();
1660 		if (ilm->ilm_filter == NULL) {
1661 			mi_free(ilm);
1662 			return (NULL);
1663 		}
1664 	}
1665 	ilm->ilm_v6addr = *v6group;
1666 	ilm->ilm_refcnt = 1;
1667 	ilm->ilm_zoneid = zoneid;
1668 	ilm->ilm_timer = INFINITY;
1669 	ilm->ilm_rtx.rtx_timer = INFINITY;
1670 
1671 	/*
1672 	 * IPv4 Multicast groups are joined using ipif.
1673 	 * IPv6 Multicast groups are joined using ill.
1674 	 */
1675 	if (ill->ill_isv6) {
1676 		ilm->ilm_ill = ill;
1677 		ilm->ilm_ipif = NULL;
1678 		DTRACE_PROBE3(ill__incr__cnt, (ill_t *), ill,
1679 		    (char *), "ilm", (void *), ilm);
1680 		ill->ill_ilm_cnt++;
1681 	} else {
1682 		ASSERT(ilm->ilm_zoneid == ipif->ipif_zoneid);
1683 		ilm->ilm_ipif = ipif;
1684 		ilm->ilm_ill = NULL;
1685 		DTRACE_PROBE3(ipif__incr__cnt, (ipif_t *), ipif,
1686 		    (char *), "ilm", (void *), ilm);
1687 		ipif->ipif_ilm_cnt++;
1688 	}
1689 	ASSERT(ill->ill_ipst);
1690 	ilm->ilm_ipst = ill->ill_ipst;	/* No netstack_hold */
1691 
1692 	/*
1693 	 * After this if ilm moves to a new ill, we don't change
1694 	 * the ilm_orig_ifindex. Thus, if ill_index != ilm_orig_ifindex,
1695 	 * it has been moved. Indexes don't match even when the application
1696 	 * wants to join on a FAILED/INACTIVE interface because we choose
1697 	 * a new interface to join in. This is considered as an implicit
1698 	 * move.
1699 	 */
1700 	ilm->ilm_orig_ifindex = orig_ifindex;
1701 
1702 	ASSERT(!(ipif->ipif_state_flags & IPIF_CONDEMNED));
1703 	ASSERT(!(ill->ill_state_flags & ILL_CONDEMNED));
1704 
1705 	/*
1706 	 * Grab lock to give consistent view to readers
1707 	 */
1708 	mutex_enter(&ill->ill_lock);
1709 	/*
1710 	 * All ilms in the same zone are contiguous in the ill_ilm list.
1711 	 * The loops in ip_proto_input() and ip_wput_local() use this to avoid
1712 	 * sending duplicates up when two applications in the same zone join the
1713 	 * same group on different logical interfaces.
1714 	 */
1715 	ilm_cur = ill->ill_ilm;
1716 	ilm_ptpn = &ill->ill_ilm;
1717 	while (ilm_cur != NULL && ilm_cur->ilm_zoneid != ilm->ilm_zoneid) {
1718 		ilm_ptpn = &ilm_cur->ilm_next;
1719 		ilm_cur = ilm_cur->ilm_next;
1720 	}
1721 	ilm->ilm_next = ilm_cur;
1722 	*ilm_ptpn = ilm;
1723 
1724 	/*
1725 	 * If we have an associated ilg, use its filter state; if not,
1726 	 * default to (EXCLUDE, NULL) and set no_ilg_cnt to track this.
1727 	 */
1728 	if (ilgstat != ILGSTAT_NONE) {
1729 		if (!SLIST_IS_EMPTY(ilg_flist))
1730 			l_copy(ilg_flist, ilm->ilm_filter);
1731 		ilm->ilm_fmode = ilg_fmode;
1732 	} else {
1733 		ilm->ilm_no_ilg_cnt = 1;
1734 		ilm->ilm_fmode = MODE_IS_EXCLUDE;
1735 	}
1736 
1737 	mutex_exit(&ill->ill_lock);
1738 	return (ilm);
1739 }
1740 
1741 void
1742 ilm_inactive(ilm_t *ilm)
1743 {
1744 	FREE_SLIST(ilm->ilm_filter);
1745 	FREE_SLIST(ilm->ilm_pendsrcs);
1746 	FREE_SLIST(ilm->ilm_rtx.rtx_allow);
1747 	FREE_SLIST(ilm->ilm_rtx.rtx_block);
1748 	ilm->ilm_ipst = NULL;
1749 	mi_free((char *)ilm);
1750 }
1751 
1752 void
1753 ilm_walker_cleanup(ill_t *ill)
1754 {
1755 	ilm_t	**ilmp;
1756 	ilm_t	*ilm;
1757 	boolean_t need_wakeup = B_FALSE;
1758 
1759 	ASSERT(MUTEX_HELD(&ill->ill_lock));
1760 	ASSERT(ill->ill_ilm_walker_cnt == 0);
1761 
1762 	ilmp = &ill->ill_ilm;
1763 	while (*ilmp != NULL) {
1764 		if ((*ilmp)->ilm_flags & ILM_DELETED) {
1765 			ilm = *ilmp;
1766 			*ilmp = ilm->ilm_next;
1767 			/*
1768 			 * check if there are any pending FREE or unplumb
1769 			 * operations that need to be restarted.
1770 			 */
1771 			if (ilm->ilm_ipif != NULL) {
1772 				/*
1773 				 * IPv4 ilms hold a ref on the ipif.
1774 				 */
1775 				DTRACE_PROBE3(ipif__decr__cnt,
1776 				    (ipif_t *), ilm->ilm_ipif,
1777 				    (char *), "ilm", (void *), ilm);
1778 				ilm->ilm_ipif->ipif_ilm_cnt--;
1779 				if (IPIF_FREE_OK(ilm->ilm_ipif))
1780 					need_wakeup = B_TRUE;
1781 			} else {
1782 				/*
1783 				 * IPv6 ilms hold a ref on the ill.
1784 				 */
1785 				ASSERT(ilm->ilm_ill == ill);
1786 				DTRACE_PROBE3(ill__decr__cnt,
1787 				    (ill_t *), ill,
1788 				    (char *), "ilm", (void *), ilm);
1789 				ASSERT(ill->ill_ilm_cnt > 0);
1790 				ill->ill_ilm_cnt--;
1791 				if (ILL_FREE_OK(ill))
1792 					need_wakeup = B_TRUE;
1793 			}
1794 			ilm_inactive(ilm); /* frees ilm */
1795 		} else {
1796 			ilmp = &(*ilmp)->ilm_next;
1797 		}
1798 	}
1799 	ill->ill_ilm_cleanup_reqd = 0;
1800 	if (need_wakeup)
1801 		ipif_ill_refrele_tail(ill);
1802 	else
1803 		mutex_exit(&ill->ill_lock);
1804 }
1805 
1806 /*
1807  * Unlink ilm and free it.
1808  */
1809 static void
1810 ilm_delete(ilm_t *ilm)
1811 {
1812 	ill_t		*ill;
1813 	ilm_t		**ilmp;
1814 	boolean_t	need_wakeup;
1815 
1816 
1817 	if (ilm->ilm_ipif != NULL) {
1818 		ASSERT(IAM_WRITER_IPIF(ilm->ilm_ipif));
1819 		ASSERT(ilm->ilm_ill == NULL);
1820 		ill = ilm->ilm_ipif->ipif_ill;
1821 		ASSERT(!ill->ill_isv6);
1822 	} else {
1823 		ASSERT(IAM_WRITER_ILL(ilm->ilm_ill));
1824 		ASSERT(ilm->ilm_ipif == NULL);
1825 		ill = ilm->ilm_ill;
1826 		ASSERT(ill->ill_isv6);
1827 	}
1828 	/*
1829 	 * Delete under lock protection so that readers don't stumble
1830 	 * on bad ilm_next
1831 	 */
1832 	mutex_enter(&ill->ill_lock);
1833 	if (ill->ill_ilm_walker_cnt != 0) {
1834 		ilm->ilm_flags |= ILM_DELETED;
1835 		ill->ill_ilm_cleanup_reqd = 1;
1836 		mutex_exit(&ill->ill_lock);
1837 		return;
1838 	}
1839 
1840 	for (ilmp = &ill->ill_ilm; *ilmp != ilm; ilmp = &(*ilmp)->ilm_next)
1841 				;
1842 	*ilmp = ilm->ilm_next;
1843 
1844 	/*
1845 	 * if we are the last reference to the ipif (for IPv4 ilms)
1846 	 * or the ill (for IPv6 ilms), we may need to wakeup any
1847 	 * pending FREE or unplumb operations.
1848 	 */
1849 	need_wakeup = B_FALSE;
1850 	if (ilm->ilm_ipif != NULL) {
1851 		DTRACE_PROBE3(ipif__decr__cnt, (ipif_t *), ilm->ilm_ipif,
1852 		    (char *), "ilm", (void *), ilm);
1853 		ilm->ilm_ipif->ipif_ilm_cnt--;
1854 		if (IPIF_FREE_OK(ilm->ilm_ipif))
1855 			need_wakeup = B_TRUE;
1856 	} else {
1857 		DTRACE_PROBE3(ill__decr__cnt, (ill_t *), ill,
1858 		    (char *), "ilm", (void *), ilm);
1859 		ASSERT(ill->ill_ilm_cnt > 0);
1860 		ill->ill_ilm_cnt--;
1861 		if (ILL_FREE_OK(ill))
1862 			need_wakeup = B_TRUE;
1863 	}
1864 
1865 	ilm_inactive(ilm); /* frees this ilm */
1866 
1867 	if (need_wakeup) {
1868 		/* drops ill lock */
1869 		ipif_ill_refrele_tail(ill);
1870 	} else {
1871 		mutex_exit(&ill->ill_lock);
1872 	}
1873 }
1874 
1875 
1876 /*
1877  * Looks up the appropriate ipif given a v4 multicast group and interface
1878  * address.  On success, returns 0, with *ipifpp pointing to the found
1879  * struct.  On failure, returns an errno and *ipifpp is NULL.
1880  */
1881 int
1882 ip_opt_check(conn_t *connp, ipaddr_t group, ipaddr_t src, ipaddr_t ifaddr,
1883     uint_t *ifindexp, mblk_t *first_mp, ipsq_func_t func, ipif_t **ipifpp)
1884 {
1885 	ipif_t *ipif;
1886 	int err = 0;
1887 	zoneid_t zoneid;
1888 	ip_stack_t	*ipst =  connp->conn_netstack->netstack_ip;
1889 
1890 	if (!CLASSD(group) || CLASSD(src)) {
1891 		return (EINVAL);
1892 	}
1893 	*ipifpp = NULL;
1894 
1895 	zoneid = IPCL_ZONEID(connp);
1896 
1897 	ASSERT(!(ifaddr != INADDR_ANY && ifindexp != NULL && *ifindexp != 0));
1898 	if (ifaddr != INADDR_ANY) {
1899 		ipif = ipif_lookup_addr(ifaddr, NULL, zoneid,
1900 		    CONNP_TO_WQ(connp), first_mp, func, &err, ipst);
1901 		if (err != 0 && err != EINPROGRESS)
1902 			err = EADDRNOTAVAIL;
1903 	} else if (ifindexp != NULL && *ifindexp != 0) {
1904 		ipif = ipif_lookup_on_ifindex(*ifindexp, B_FALSE, zoneid,
1905 		    CONNP_TO_WQ(connp), first_mp, func, &err, ipst);
1906 	} else {
1907 		ipif = ipif_lookup_group(group, zoneid, ipst);
1908 		if (ipif == NULL)
1909 			return (EADDRNOTAVAIL);
1910 	}
1911 	if (ipif == NULL)
1912 		return (err);
1913 
1914 	*ipifpp = ipif;
1915 	return (0);
1916 }
1917 
1918 /*
1919  * Looks up the appropriate ill (or ipif if v4mapped) given an interface
1920  * index and IPv6 multicast group.  On success, returns 0, with *illpp (or
1921  * *ipifpp if v4mapped) pointing to the found struct.  On failure, returns
1922  * an errno and *illpp and *ipifpp are undefined.
1923  */
1924 int
1925 ip_opt_check_v6(conn_t *connp, const in6_addr_t *v6group, ipaddr_t *v4group,
1926     const in6_addr_t *v6src, ipaddr_t *v4src, boolean_t *isv6, int ifindex,
1927     mblk_t *first_mp, ipsq_func_t func, ill_t **illpp, ipif_t **ipifpp)
1928 {
1929 	boolean_t src_unspec;
1930 	ill_t *ill = NULL;
1931 	ipif_t *ipif = NULL;
1932 	int err;
1933 	zoneid_t zoneid = connp->conn_zoneid;
1934 	queue_t *wq = CONNP_TO_WQ(connp);
1935 	ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
1936 
1937 	src_unspec = IN6_IS_ADDR_UNSPECIFIED(v6src);
1938 
1939 	if (IN6_IS_ADDR_V4MAPPED(v6group)) {
1940 		if (!IN6_IS_ADDR_V4MAPPED(v6src) && !src_unspec)
1941 			return (EINVAL);
1942 		IN6_V4MAPPED_TO_IPADDR(v6group, *v4group);
1943 		if (src_unspec) {
1944 			*v4src = INADDR_ANY;
1945 		} else {
1946 			IN6_V4MAPPED_TO_IPADDR(v6src, *v4src);
1947 		}
1948 		if (!CLASSD(*v4group) || CLASSD(*v4src))
1949 			return (EINVAL);
1950 		*ipifpp = NULL;
1951 		*isv6 = B_FALSE;
1952 	} else {
1953 		if (IN6_IS_ADDR_V4MAPPED(v6src) && !src_unspec)
1954 			return (EINVAL);
1955 		if (!IN6_IS_ADDR_MULTICAST(v6group) ||
1956 		    IN6_IS_ADDR_MULTICAST(v6src)) {
1957 			return (EINVAL);
1958 		}
1959 		*illpp = NULL;
1960 		*isv6 = B_TRUE;
1961 	}
1962 
1963 	if (ifindex == 0) {
1964 		if (*isv6)
1965 			ill = ill_lookup_group_v6(v6group, zoneid, ipst);
1966 		else
1967 			ipif = ipif_lookup_group(*v4group, zoneid, ipst);
1968 		if (ill == NULL && ipif == NULL)
1969 			return (EADDRNOTAVAIL);
1970 	} else {
1971 		if (*isv6) {
1972 			ill = ill_lookup_on_ifindex(ifindex, B_TRUE,
1973 			    wq, first_mp, func, &err, ipst);
1974 			if (ill != NULL &&
1975 			    !ipif_lookup_zoneid(ill, zoneid, 0, NULL)) {
1976 				ill_refrele(ill);
1977 				ill = NULL;
1978 				err = EADDRNOTAVAIL;
1979 			}
1980 		} else {
1981 			ipif = ipif_lookup_on_ifindex(ifindex, B_FALSE,
1982 			    zoneid, wq, first_mp, func, &err, ipst);
1983 		}
1984 		if (ill == NULL && ipif == NULL)
1985 			return (err);
1986 	}
1987 
1988 	*ipifpp = ipif;
1989 	*illpp = ill;
1990 	return (0);
1991 }
1992 
1993 static int
1994 ip_get_srcfilter(conn_t *connp, struct group_filter *gf,
1995     struct ip_msfilter *imsf, ipaddr_t grp, ipif_t *ipif, boolean_t isv4mapped)
1996 {
1997 	ilg_t *ilg;
1998 	int i, numsrc, fmode, outsrcs;
1999 	struct sockaddr_in *sin;
2000 	struct sockaddr_in6 *sin6;
2001 	struct in_addr *addrp;
2002 	slist_t *fp;
2003 	boolean_t is_v4only_api;
2004 
2005 	mutex_enter(&connp->conn_lock);
2006 
2007 	ilg = ilg_lookup_ipif(connp, grp, ipif);
2008 	if (ilg == NULL) {
2009 		mutex_exit(&connp->conn_lock);
2010 		return (EADDRNOTAVAIL);
2011 	}
2012 
2013 	if (gf == NULL) {
2014 		ASSERT(imsf != NULL);
2015 		ASSERT(!isv4mapped);
2016 		is_v4only_api = B_TRUE;
2017 		outsrcs = imsf->imsf_numsrc;
2018 	} else {
2019 		ASSERT(imsf == NULL);
2020 		is_v4only_api = B_FALSE;
2021 		outsrcs = gf->gf_numsrc;
2022 	}
2023 
2024 	/*
2025 	 * In the kernel, we use the state definitions MODE_IS_[IN|EX]CLUDE
2026 	 * to identify the filter mode; but the API uses MCAST_[IN|EX]CLUDE.
2027 	 * So we need to translate here.
2028 	 */
2029 	fmode = (ilg->ilg_fmode == MODE_IS_INCLUDE) ?
2030 	    MCAST_INCLUDE : MCAST_EXCLUDE;
2031 	if ((fp = ilg->ilg_filter) == NULL) {
2032 		numsrc = 0;
2033 	} else {
2034 		for (i = 0; i < outsrcs; i++) {
2035 			if (i == fp->sl_numsrc)
2036 				break;
2037 			if (isv4mapped) {
2038 				sin6 = (struct sockaddr_in6 *)&gf->gf_slist[i];
2039 				sin6->sin6_family = AF_INET6;
2040 				sin6->sin6_addr = fp->sl_addr[i];
2041 			} else {
2042 				if (is_v4only_api) {
2043 					addrp = &imsf->imsf_slist[i];
2044 				} else {
2045 					sin = (struct sockaddr_in *)
2046 					    &gf->gf_slist[i];
2047 					sin->sin_family = AF_INET;
2048 					addrp = &sin->sin_addr;
2049 				}
2050 				IN6_V4MAPPED_TO_INADDR(&fp->sl_addr[i], addrp);
2051 			}
2052 		}
2053 		numsrc = fp->sl_numsrc;
2054 	}
2055 
2056 	if (is_v4only_api) {
2057 		imsf->imsf_numsrc = numsrc;
2058 		imsf->imsf_fmode = fmode;
2059 	} else {
2060 		gf->gf_numsrc = numsrc;
2061 		gf->gf_fmode = fmode;
2062 	}
2063 
2064 	mutex_exit(&connp->conn_lock);
2065 
2066 	return (0);
2067 }
2068 
2069 static int
2070 ip_get_srcfilter_v6(conn_t *connp, struct group_filter *gf,
2071     const struct in6_addr *grp, ill_t *ill)
2072 {
2073 	ilg_t *ilg;
2074 	int i;
2075 	struct sockaddr_storage *sl;
2076 	struct sockaddr_in6 *sin6;
2077 	slist_t *fp;
2078 
2079 	mutex_enter(&connp->conn_lock);
2080 
2081 	ilg = ilg_lookup_ill_v6(connp, grp, ill);
2082 	if (ilg == NULL) {
2083 		mutex_exit(&connp->conn_lock);
2084 		return (EADDRNOTAVAIL);
2085 	}
2086 
2087 	/*
2088 	 * In the kernel, we use the state definitions MODE_IS_[IN|EX]CLUDE
2089 	 * to identify the filter mode; but the API uses MCAST_[IN|EX]CLUDE.
2090 	 * So we need to translate here.
2091 	 */
2092 	gf->gf_fmode = (ilg->ilg_fmode == MODE_IS_INCLUDE) ?
2093 	    MCAST_INCLUDE : MCAST_EXCLUDE;
2094 	if ((fp = ilg->ilg_filter) == NULL) {
2095 		gf->gf_numsrc = 0;
2096 	} else {
2097 		for (i = 0, sl = gf->gf_slist; i < gf->gf_numsrc; i++, sl++) {
2098 			if (i == fp->sl_numsrc)
2099 				break;
2100 			sin6 = (struct sockaddr_in6 *)sl;
2101 			sin6->sin6_family = AF_INET6;
2102 			sin6->sin6_addr = fp->sl_addr[i];
2103 		}
2104 		gf->gf_numsrc = fp->sl_numsrc;
2105 	}
2106 
2107 	mutex_exit(&connp->conn_lock);
2108 
2109 	return (0);
2110 }
2111 
2112 static int
2113 ip_set_srcfilter(conn_t *connp, struct group_filter *gf,
2114     struct ip_msfilter *imsf, ipaddr_t grp, ipif_t *ipif, boolean_t isv4mapped)
2115 {
2116 	ilg_t *ilg;
2117 	int i, err, insrcs, infmode, new_fmode;
2118 	struct sockaddr_in *sin;
2119 	struct sockaddr_in6 *sin6;
2120 	struct in_addr *addrp;
2121 	slist_t *orig_filter = NULL;
2122 	slist_t *new_filter = NULL;
2123 	mcast_record_t orig_fmode;
2124 	boolean_t leave_grp, is_v4only_api;
2125 	ilg_stat_t ilgstat;
2126 
2127 	if (gf == NULL) {
2128 		ASSERT(imsf != NULL);
2129 		ASSERT(!isv4mapped);
2130 		is_v4only_api = B_TRUE;
2131 		insrcs = imsf->imsf_numsrc;
2132 		infmode = imsf->imsf_fmode;
2133 	} else {
2134 		ASSERT(imsf == NULL);
2135 		is_v4only_api = B_FALSE;
2136 		insrcs = gf->gf_numsrc;
2137 		infmode = gf->gf_fmode;
2138 	}
2139 
2140 	/* Make sure we can handle the source list */
2141 	if (insrcs > MAX_FILTER_SIZE)
2142 		return (ENOBUFS);
2143 
2144 	/*
2145 	 * setting the filter to (INCLUDE, NULL) is treated
2146 	 * as a request to leave the group.
2147 	 */
2148 	leave_grp = (infmode == MCAST_INCLUDE && insrcs == 0);
2149 
2150 	ASSERT(IAM_WRITER_IPIF(ipif));
2151 
2152 	mutex_enter(&connp->conn_lock);
2153 
2154 	ilg = ilg_lookup_ipif(connp, grp, ipif);
2155 	if (ilg == NULL) {
2156 		/*
2157 		 * if the request was actually to leave, and we
2158 		 * didn't find an ilg, there's nothing to do.
2159 		 */
2160 		if (!leave_grp)
2161 			ilg = conn_ilg_alloc(connp);
2162 		if (leave_grp || ilg == NULL) {
2163 			mutex_exit(&connp->conn_lock);
2164 			return (leave_grp ? 0 : ENOMEM);
2165 		}
2166 		ilgstat = ILGSTAT_NEW;
2167 		IN6_IPADDR_TO_V4MAPPED(grp, &ilg->ilg_v6group);
2168 		ilg->ilg_ipif = ipif;
2169 		ilg->ilg_ill = NULL;
2170 		ilg->ilg_orig_ifindex = 0;
2171 	} else if (leave_grp) {
2172 		ilg_delete(connp, ilg, NULL);
2173 		mutex_exit(&connp->conn_lock);
2174 		(void) ip_delmulti(grp, ipif, B_FALSE, B_TRUE);
2175 		return (0);
2176 	} else {
2177 		ilgstat = ILGSTAT_CHANGE;
2178 		/* Preserve existing state in case ip_addmulti() fails */
2179 		orig_fmode = ilg->ilg_fmode;
2180 		if (ilg->ilg_filter == NULL) {
2181 			orig_filter = NULL;
2182 		} else {
2183 			orig_filter = l_alloc_copy(ilg->ilg_filter);
2184 			if (orig_filter == NULL) {
2185 				mutex_exit(&connp->conn_lock);
2186 				return (ENOMEM);
2187 			}
2188 		}
2189 	}
2190 
2191 	/*
2192 	 * Alloc buffer to copy new state into (see below) before
2193 	 * we make any changes, so we can bail if it fails.
2194 	 */
2195 	if ((new_filter = l_alloc()) == NULL) {
2196 		mutex_exit(&connp->conn_lock);
2197 		err = ENOMEM;
2198 		goto free_and_exit;
2199 	}
2200 
2201 	if (insrcs == 0) {
2202 		CLEAR_SLIST(ilg->ilg_filter);
2203 	} else {
2204 		slist_t *fp;
2205 		if (ilg->ilg_filter == NULL) {
2206 			fp = l_alloc();
2207 			if (fp == NULL) {
2208 				if (ilgstat == ILGSTAT_NEW)
2209 					ilg_delete(connp, ilg, NULL);
2210 				mutex_exit(&connp->conn_lock);
2211 				err = ENOMEM;
2212 				goto free_and_exit;
2213 			}
2214 		} else {
2215 			fp = ilg->ilg_filter;
2216 		}
2217 		for (i = 0; i < insrcs; i++) {
2218 			if (isv4mapped) {
2219 				sin6 = (struct sockaddr_in6 *)&gf->gf_slist[i];
2220 				fp->sl_addr[i] = sin6->sin6_addr;
2221 			} else {
2222 				if (is_v4only_api) {
2223 					addrp = &imsf->imsf_slist[i];
2224 				} else {
2225 					sin = (struct sockaddr_in *)
2226 					    &gf->gf_slist[i];
2227 					addrp = &sin->sin_addr;
2228 				}
2229 				IN6_INADDR_TO_V4MAPPED(addrp, &fp->sl_addr[i]);
2230 			}
2231 		}
2232 		fp->sl_numsrc = insrcs;
2233 		ilg->ilg_filter = fp;
2234 	}
2235 	/*
2236 	 * In the kernel, we use the state definitions MODE_IS_[IN|EX]CLUDE
2237 	 * to identify the filter mode; but the API uses MCAST_[IN|EX]CLUDE.
2238 	 * So we need to translate here.
2239 	 */
2240 	ilg->ilg_fmode = (infmode == MCAST_INCLUDE) ?
2241 	    MODE_IS_INCLUDE : MODE_IS_EXCLUDE;
2242 
2243 	/*
2244 	 * Save copy of ilg's filter state to pass to other functions,
2245 	 * so we can release conn_lock now.
2246 	 */
2247 	new_fmode = ilg->ilg_fmode;
2248 	l_copy(ilg->ilg_filter, new_filter);
2249 
2250 	mutex_exit(&connp->conn_lock);
2251 
2252 	err = ip_addmulti(grp, ipif, ilgstat, new_fmode, new_filter);
2253 	if (err != 0) {
2254 		/*
2255 		 * Restore the original filter state, or delete the
2256 		 * newly-created ilg.  We need to look up the ilg
2257 		 * again, though, since we've not been holding the
2258 		 * conn_lock.
2259 		 */
2260 		mutex_enter(&connp->conn_lock);
2261 		ilg = ilg_lookup_ipif(connp, grp, ipif);
2262 		ASSERT(ilg != NULL);
2263 		if (ilgstat == ILGSTAT_NEW) {
2264 			ilg_delete(connp, ilg, NULL);
2265 		} else {
2266 			ilg->ilg_fmode = orig_fmode;
2267 			if (SLIST_IS_EMPTY(orig_filter)) {
2268 				CLEAR_SLIST(ilg->ilg_filter);
2269 			} else {
2270 				/*
2271 				 * We didn't free the filter, even if we
2272 				 * were trying to make the source list empty;
2273 				 * so if orig_filter isn't empty, the ilg
2274 				 * must still have a filter alloc'd.
2275 				 */
2276 				l_copy(orig_filter, ilg->ilg_filter);
2277 			}
2278 		}
2279 		mutex_exit(&connp->conn_lock);
2280 	}
2281 
2282 free_and_exit:
2283 	l_free(orig_filter);
2284 	l_free(new_filter);
2285 
2286 	return (err);
2287 }
2288 
2289 static int
2290 ip_set_srcfilter_v6(conn_t *connp, struct group_filter *gf,
2291     const struct in6_addr *grp, ill_t *ill)
2292 {
2293 	ilg_t *ilg;
2294 	int i, orig_ifindex, orig_fmode, new_fmode, err;
2295 	slist_t *orig_filter = NULL;
2296 	slist_t *new_filter = NULL;
2297 	struct sockaddr_storage *sl;
2298 	struct sockaddr_in6 *sin6;
2299 	boolean_t leave_grp;
2300 	ilg_stat_t ilgstat;
2301 
2302 	/* Make sure we can handle the source list */
2303 	if (gf->gf_numsrc > MAX_FILTER_SIZE)
2304 		return (ENOBUFS);
2305 
2306 	/*
2307 	 * setting the filter to (INCLUDE, NULL) is treated
2308 	 * as a request to leave the group.
2309 	 */
2310 	leave_grp = (gf->gf_fmode == MCAST_INCLUDE && gf->gf_numsrc == 0);
2311 
2312 	ASSERT(IAM_WRITER_ILL(ill));
2313 
2314 	/*
2315 	 * Use the ifindex to do the lookup.  We can't use the ill
2316 	 * directly because ilg_ill could point to a different ill
2317 	 * if things have moved.
2318 	 */
2319 	orig_ifindex = ill->ill_phyint->phyint_ifindex;
2320 
2321 	mutex_enter(&connp->conn_lock);
2322 	ilg = ilg_lookup_ill_index_v6(connp, grp, orig_ifindex);
2323 	if (ilg == NULL) {
2324 		/*
2325 		 * if the request was actually to leave, and we
2326 		 * didn't find an ilg, there's nothing to do.
2327 		 */
2328 		if (!leave_grp)
2329 			ilg = conn_ilg_alloc(connp);
2330 		if (leave_grp || ilg == NULL) {
2331 			mutex_exit(&connp->conn_lock);
2332 			return (leave_grp ? 0 : ENOMEM);
2333 		}
2334 		ilgstat = ILGSTAT_NEW;
2335 		ilg->ilg_v6group = *grp;
2336 		ilg->ilg_ipif = NULL;
2337 		/*
2338 		 * Choose our target ill to join on. This might be
2339 		 * different from the ill we've been given if it's
2340 		 * currently down and part of a group.
2341 		 *
2342 		 * new ill is not refheld; we are writer.
2343 		 */
2344 		ill = ip_choose_multi_ill(ill, grp);
2345 		ASSERT(!(ill->ill_state_flags & ILL_CONDEMNED));
2346 		ilg->ilg_ill = ill;
2347 		/*
2348 		 * Remember the index that we joined on, so that we can
2349 		 * successfully delete them later on and also search for
2350 		 * duplicates if the application wants to join again.
2351 		 */
2352 		ilg->ilg_orig_ifindex = orig_ifindex;
2353 	} else if (leave_grp) {
2354 		/*
2355 		 * Use the ilg's current ill for the deletion,
2356 		 * we might have failed over.
2357 		 */
2358 		ill = ilg->ilg_ill;
2359 		ilg_delete(connp, ilg, NULL);
2360 		mutex_exit(&connp->conn_lock);
2361 		(void) ip_delmulti_v6(grp, ill, orig_ifindex,
2362 		    connp->conn_zoneid, B_FALSE, B_TRUE);
2363 		return (0);
2364 	} else {
2365 		ilgstat = ILGSTAT_CHANGE;
2366 		/*
2367 		 * The current ill might be different from the one we were
2368 		 * asked to join on (if failover has occurred); we should
2369 		 * join on the ill stored in the ilg.  The original ill
2370 		 * is noted in ilg_orig_ifindex, which matched our request.
2371 		 */
2372 		ill = ilg->ilg_ill;
2373 		/* preserve existing state in case ip_addmulti() fails */
2374 		orig_fmode = ilg->ilg_fmode;
2375 		if (ilg->ilg_filter == NULL) {
2376 			orig_filter = NULL;
2377 		} else {
2378 			orig_filter = l_alloc_copy(ilg->ilg_filter);
2379 			if (orig_filter == NULL) {
2380 				mutex_exit(&connp->conn_lock);
2381 				return (ENOMEM);
2382 			}
2383 		}
2384 	}
2385 
2386 	/*
2387 	 * Alloc buffer to copy new state into (see below) before
2388 	 * we make any changes, so we can bail if it fails.
2389 	 */
2390 	if ((new_filter = l_alloc()) == NULL) {
2391 		mutex_exit(&connp->conn_lock);
2392 		err = ENOMEM;
2393 		goto free_and_exit;
2394 	}
2395 
2396 	if (gf->gf_numsrc == 0) {
2397 		CLEAR_SLIST(ilg->ilg_filter);
2398 	} else {
2399 		slist_t *fp;
2400 		if (ilg->ilg_filter == NULL) {
2401 			fp = l_alloc();
2402 			if (fp == NULL) {
2403 				if (ilgstat == ILGSTAT_NEW)
2404 					ilg_delete(connp, ilg, NULL);
2405 				mutex_exit(&connp->conn_lock);
2406 				err = ENOMEM;
2407 				goto free_and_exit;
2408 			}
2409 		} else {
2410 			fp = ilg->ilg_filter;
2411 		}
2412 		for (i = 0, sl = gf->gf_slist; i < gf->gf_numsrc; i++, sl++) {
2413 			sin6 = (struct sockaddr_in6 *)sl;
2414 			fp->sl_addr[i] = sin6->sin6_addr;
2415 		}
2416 		fp->sl_numsrc = gf->gf_numsrc;
2417 		ilg->ilg_filter = fp;
2418 	}
2419 	/*
2420 	 * In the kernel, we use the state definitions MODE_IS_[IN|EX]CLUDE
2421 	 * to identify the filter mode; but the API uses MCAST_[IN|EX]CLUDE.
2422 	 * So we need to translate here.
2423 	 */
2424 	ilg->ilg_fmode = (gf->gf_fmode == MCAST_INCLUDE) ?
2425 	    MODE_IS_INCLUDE : MODE_IS_EXCLUDE;
2426 
2427 	/*
2428 	 * Save copy of ilg's filter state to pass to other functions,
2429 	 * so we can release conn_lock now.
2430 	 */
2431 	new_fmode = ilg->ilg_fmode;
2432 	l_copy(ilg->ilg_filter, new_filter);
2433 
2434 	mutex_exit(&connp->conn_lock);
2435 
2436 	err = ip_addmulti_v6(grp, ill, orig_ifindex, connp->conn_zoneid,
2437 	    ilgstat, new_fmode, new_filter);
2438 	if (err != 0) {
2439 		/*
2440 		 * Restore the original filter state, or delete the
2441 		 * newly-created ilg.  We need to look up the ilg
2442 		 * again, though, since we've not been holding the
2443 		 * conn_lock.
2444 		 */
2445 		mutex_enter(&connp->conn_lock);
2446 		ilg = ilg_lookup_ill_index_v6(connp, grp, orig_ifindex);
2447 		ASSERT(ilg != NULL);
2448 		if (ilgstat == ILGSTAT_NEW) {
2449 			ilg_delete(connp, ilg, NULL);
2450 		} else {
2451 			ilg->ilg_fmode = orig_fmode;
2452 			if (SLIST_IS_EMPTY(orig_filter)) {
2453 				CLEAR_SLIST(ilg->ilg_filter);
2454 			} else {
2455 				/*
2456 				 * We didn't free the filter, even if we
2457 				 * were trying to make the source list empty;
2458 				 * so if orig_filter isn't empty, the ilg
2459 				 * must still have a filter alloc'd.
2460 				 */
2461 				l_copy(orig_filter, ilg->ilg_filter);
2462 			}
2463 		}
2464 		mutex_exit(&connp->conn_lock);
2465 	}
2466 
2467 free_and_exit:
2468 	l_free(orig_filter);
2469 	l_free(new_filter);
2470 
2471 	return (err);
2472 }
2473 
2474 /*
2475  * Process the SIOC[GS]MSFILTER and SIOC[GS]IPMSFILTER ioctls.
2476  */
2477 /* ARGSUSED */
2478 int
2479 ip_sioctl_msfilter(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
2480     ip_ioctl_cmd_t *ipip, void *ifreq)
2481 {
2482 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
2483 	/* existence verified in ip_wput_nondata() */
2484 	mblk_t *data_mp = mp->b_cont->b_cont;
2485 	int datalen, err, cmd, minsize;
2486 	int expsize = 0;
2487 	conn_t *connp;
2488 	boolean_t isv6, is_v4only_api, getcmd;
2489 	struct sockaddr_in *gsin;
2490 	struct sockaddr_in6 *gsin6;
2491 	ipaddr_t v4grp;
2492 	in6_addr_t v6grp;
2493 	struct group_filter *gf = NULL;
2494 	struct ip_msfilter *imsf = NULL;
2495 	mblk_t *ndp;
2496 
2497 	if (data_mp->b_cont != NULL) {
2498 		if ((ndp = msgpullup(data_mp, -1)) == NULL)
2499 			return (ENOMEM);
2500 		freemsg(data_mp);
2501 		data_mp = ndp;
2502 		mp->b_cont->b_cont = data_mp;
2503 	}
2504 
2505 	cmd = iocp->ioc_cmd;
2506 	getcmd = (cmd == SIOCGIPMSFILTER || cmd == SIOCGMSFILTER);
2507 	is_v4only_api = (cmd == SIOCGIPMSFILTER || cmd == SIOCSIPMSFILTER);
2508 	minsize = (is_v4only_api) ? IP_MSFILTER_SIZE(0) : GROUP_FILTER_SIZE(0);
2509 	datalen = MBLKL(data_mp);
2510 
2511 	if (datalen < minsize)
2512 		return (EINVAL);
2513 
2514 	/*
2515 	 * now we know we have at least have the initial structure,
2516 	 * but need to check for the source list array.
2517 	 */
2518 	if (is_v4only_api) {
2519 		imsf = (struct ip_msfilter *)data_mp->b_rptr;
2520 		isv6 = B_FALSE;
2521 		expsize = IP_MSFILTER_SIZE(imsf->imsf_numsrc);
2522 	} else {
2523 		gf = (struct group_filter *)data_mp->b_rptr;
2524 		if (gf->gf_group.ss_family == AF_INET6) {
2525 			gsin6 = (struct sockaddr_in6 *)&gf->gf_group;
2526 			isv6 = !(IN6_IS_ADDR_V4MAPPED(&gsin6->sin6_addr));
2527 		} else {
2528 			isv6 = B_FALSE;
2529 		}
2530 		expsize = GROUP_FILTER_SIZE(gf->gf_numsrc);
2531 	}
2532 	if (datalen < expsize)
2533 		return (EINVAL);
2534 
2535 	connp = Q_TO_CONN(q);
2536 
2537 	/* operation not supported on the virtual network interface */
2538 	if (IS_VNI(ipif->ipif_ill))
2539 		return (EINVAL);
2540 
2541 	if (isv6) {
2542 		ill_t *ill = ipif->ipif_ill;
2543 		ill_refhold(ill);
2544 
2545 		gsin6 = (struct sockaddr_in6 *)&gf->gf_group;
2546 		v6grp = gsin6->sin6_addr;
2547 		if (getcmd)
2548 			err = ip_get_srcfilter_v6(connp, gf, &v6grp, ill);
2549 		else
2550 			err = ip_set_srcfilter_v6(connp, gf, &v6grp, ill);
2551 
2552 		ill_refrele(ill);
2553 	} else {
2554 		boolean_t isv4mapped = B_FALSE;
2555 		if (is_v4only_api) {
2556 			v4grp = (ipaddr_t)imsf->imsf_multiaddr.s_addr;
2557 		} else {
2558 			if (gf->gf_group.ss_family == AF_INET) {
2559 				gsin = (struct sockaddr_in *)&gf->gf_group;
2560 				v4grp = (ipaddr_t)gsin->sin_addr.s_addr;
2561 			} else {
2562 				gsin6 = (struct sockaddr_in6 *)&gf->gf_group;
2563 				IN6_V4MAPPED_TO_IPADDR(&gsin6->sin6_addr,
2564 				    v4grp);
2565 				isv4mapped = B_TRUE;
2566 			}
2567 		}
2568 		if (getcmd)
2569 			err = ip_get_srcfilter(connp, gf, imsf, v4grp, ipif,
2570 			    isv4mapped);
2571 		else
2572 			err = ip_set_srcfilter(connp, gf, imsf, v4grp, ipif,
2573 			    isv4mapped);
2574 	}
2575 
2576 	return (err);
2577 }
2578 
2579 /*
2580  * Finds the ipif based on information in the ioctl headers.  Needed to make
2581  * ip_process_ioctl() happy (it needs to know the ipif for IPI_WR-flagged
2582  * ioctls prior to calling the ioctl's handler function).
2583  */
2584 int
2585 ip_extract_msfilter(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
2586     cmd_info_t *ci, ipsq_func_t func)
2587 {
2588 	int cmd = ipip->ipi_cmd;
2589 	int err = 0;
2590 	conn_t *connp;
2591 	ipif_t *ipif;
2592 	/* caller has verified this mblk exists */
2593 	char *dbuf = (char *)mp->b_cont->b_cont->b_rptr;
2594 	struct ip_msfilter *imsf;
2595 	struct group_filter *gf;
2596 	ipaddr_t v4addr, v4grp;
2597 	in6_addr_t v6grp;
2598 	uint32_t index;
2599 	zoneid_t zoneid;
2600 	ip_stack_t *ipst;
2601 
2602 	connp = Q_TO_CONN(q);
2603 	zoneid = connp->conn_zoneid;
2604 	ipst = connp->conn_netstack->netstack_ip;
2605 
2606 	/* don't allow multicast operations on a tcp conn */
2607 	if (IPCL_IS_TCP(connp))
2608 		return (ENOPROTOOPT);
2609 
2610 	if (cmd == SIOCSIPMSFILTER || cmd == SIOCGIPMSFILTER) {
2611 		/* don't allow v4-specific ioctls on v6 socket */
2612 		if (connp->conn_af_isv6)
2613 			return (EAFNOSUPPORT);
2614 
2615 		imsf = (struct ip_msfilter *)dbuf;
2616 		v4addr = imsf->imsf_interface.s_addr;
2617 		v4grp = imsf->imsf_multiaddr.s_addr;
2618 		if (v4addr == INADDR_ANY) {
2619 			ipif = ipif_lookup_group(v4grp, zoneid, ipst);
2620 			if (ipif == NULL)
2621 				err = EADDRNOTAVAIL;
2622 		} else {
2623 			ipif = ipif_lookup_addr(v4addr, NULL, zoneid, q, mp,
2624 			    func, &err, ipst);
2625 		}
2626 	} else {
2627 		boolean_t isv6 = B_FALSE;
2628 		gf = (struct group_filter *)dbuf;
2629 		index = gf->gf_interface;
2630 		if (gf->gf_group.ss_family == AF_INET6) {
2631 			struct sockaddr_in6 *sin6;
2632 			sin6 = (struct sockaddr_in6 *)&gf->gf_group;
2633 			v6grp = sin6->sin6_addr;
2634 			if (IN6_IS_ADDR_V4MAPPED(&v6grp))
2635 				IN6_V4MAPPED_TO_IPADDR(&v6grp, v4grp);
2636 			else
2637 				isv6 = B_TRUE;
2638 		} else if (gf->gf_group.ss_family == AF_INET) {
2639 			struct sockaddr_in *sin;
2640 			sin = (struct sockaddr_in *)&gf->gf_group;
2641 			v4grp = sin->sin_addr.s_addr;
2642 		} else {
2643 			return (EAFNOSUPPORT);
2644 		}
2645 		if (index == 0) {
2646 			if (isv6) {
2647 				ipif = ipif_lookup_group_v6(&v6grp, zoneid,
2648 				    ipst);
2649 			} else {
2650 				ipif = ipif_lookup_group(v4grp, zoneid, ipst);
2651 			}
2652 			if (ipif == NULL)
2653 				err = EADDRNOTAVAIL;
2654 		} else {
2655 			ipif = ipif_lookup_on_ifindex(index, isv6, zoneid,
2656 			    q, mp, func, &err, ipst);
2657 		}
2658 	}
2659 
2660 	ci->ci_ipif = ipif;
2661 	return (err);
2662 }
2663 
2664 /*
2665  * The structures used for the SIOC*MSFILTER ioctls usually must be copied
2666  * in in two stages, as the first copyin tells us the size of the attached
2667  * source buffer.  This function is called by ip_wput_nondata() after the
2668  * first copyin has completed; it figures out how big the second stage
2669  * needs to be, and kicks it off.
2670  *
2671  * In some cases (numsrc < 2), the second copyin is not needed as the
2672  * first one gets a complete structure containing 1 source addr.
2673  *
2674  * The function returns 0 if a second copyin has been started (i.e. there's
2675  * no more work to be done right now), or 1 if the second copyin is not
2676  * needed and ip_wput_nondata() can continue its processing.
2677  */
2678 int
2679 ip_copyin_msfilter(queue_t *q, mblk_t *mp)
2680 {
2681 	struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
2682 	int cmd = iocp->ioc_cmd;
2683 	/* validity of this checked in ip_wput_nondata() */
2684 	mblk_t *mp1 = mp->b_cont->b_cont;
2685 	int copysize = 0;
2686 	int offset;
2687 
2688 	if (cmd == SIOCSMSFILTER || cmd == SIOCGMSFILTER) {
2689 		struct group_filter *gf = (struct group_filter *)mp1->b_rptr;
2690 		if (gf->gf_numsrc >= 2) {
2691 			offset = sizeof (struct group_filter);
2692 			copysize = GROUP_FILTER_SIZE(gf->gf_numsrc) - offset;
2693 		}
2694 	} else {
2695 		struct ip_msfilter *imsf = (struct ip_msfilter *)mp1->b_rptr;
2696 		if (imsf->imsf_numsrc >= 2) {
2697 			offset = sizeof (struct ip_msfilter);
2698 			copysize = IP_MSFILTER_SIZE(imsf->imsf_numsrc) - offset;
2699 		}
2700 	}
2701 	if (copysize > 0) {
2702 		mi_copyin_n(q, mp, offset, copysize);
2703 		return (0);
2704 	}
2705 	return (1);
2706 }
2707 
2708 /*
2709  * Handle the following optmgmt:
2710  *	IP_ADD_MEMBERSHIP		must not have joined already
2711  *	MCAST_JOIN_GROUP		must not have joined already
2712  *	IP_BLOCK_SOURCE			must have joined already
2713  *	MCAST_BLOCK_SOURCE		must have joined already
2714  *	IP_JOIN_SOURCE_GROUP		may have joined already
2715  *	MCAST_JOIN_SOURCE_GROUP		may have joined already
2716  *
2717  * fmode and src parameters may be used to determine which option is
2718  * being set, as follows (the IP_* and MCAST_* versions of each option
2719  * are functionally equivalent):
2720  *	opt			fmode			src
2721  *	IP_ADD_MEMBERSHIP	MODE_IS_EXCLUDE		INADDR_ANY
2722  *	MCAST_JOIN_GROUP	MODE_IS_EXCLUDE		INADDR_ANY
2723  *	IP_BLOCK_SOURCE		MODE_IS_EXCLUDE		v4 addr
2724  *	MCAST_BLOCK_SOURCE	MODE_IS_EXCLUDE		v4 addr
2725  *	IP_JOIN_SOURCE_GROUP	MODE_IS_INCLUDE		v4 addr
2726  *	MCAST_JOIN_SOURCE_GROUP	MODE_IS_INCLUDE		v4 addr
2727  *
2728  * Changing the filter mode is not allowed; if a matching ilg already
2729  * exists and fmode != ilg->ilg_fmode, EINVAL is returned.
2730  *
2731  * Verifies that there is a source address of appropriate scope for
2732  * the group; if not, EADDRNOTAVAIL is returned.
2733  *
2734  * The interface to be used may be identified by an address or by an
2735  * index.  A pointer to the index is passed; if it is NULL, use the
2736  * address, otherwise, use the index.
2737  */
2738 int
2739 ip_opt_add_group(conn_t *connp, boolean_t checkonly, ipaddr_t group,
2740     ipaddr_t ifaddr, uint_t *ifindexp, mcast_record_t fmode, ipaddr_t src,
2741     mblk_t *first_mp)
2742 {
2743 	ipif_t	*ipif;
2744 	ipsq_t	*ipsq;
2745 	int err = 0;
2746 	ill_t	*ill;
2747 
2748 	err = ip_opt_check(connp, group, src, ifaddr, ifindexp, first_mp,
2749 	    ip_restart_optmgmt, &ipif);
2750 	if (err != 0) {
2751 		if (err != EINPROGRESS) {
2752 			ip1dbg(("ip_opt_add_group: no ipif for group 0x%x, "
2753 			    "ifaddr 0x%x, ifindex %d\n", ntohl(group),
2754 			    ntohl(ifaddr), (ifindexp == NULL) ? 0 : *ifindexp));
2755 		}
2756 		return (err);
2757 	}
2758 	ASSERT(ipif != NULL);
2759 
2760 	ill = ipif->ipif_ill;
2761 	/* Operation not supported on a virtual network interface */
2762 	if (IS_VNI(ill)) {
2763 		ipif_refrele(ipif);
2764 		return (EINVAL);
2765 	}
2766 
2767 	if (checkonly) {
2768 		/*
2769 		 * do not do operation, just pretend to - new T_CHECK
2770 		 * semantics. The error return case above if encountered
2771 		 * considered a good enough "check" here.
2772 		 */
2773 		ipif_refrele(ipif);
2774 		return (0);
2775 	}
2776 
2777 	IPSQ_ENTER_IPIF(ipif, connp, first_mp, ip_restart_optmgmt, ipsq,
2778 	    NEW_OP);
2779 
2780 	/* unspecified source addr => no source filtering */
2781 	err = ilg_add(connp, group, ipif, fmode, src);
2782 
2783 	IPSQ_EXIT(ipsq);
2784 
2785 	ipif_refrele(ipif);
2786 	return (err);
2787 }
2788 
2789 /*
2790  * Handle the following optmgmt:
2791  *	IPV6_JOIN_GROUP			must not have joined already
2792  *	MCAST_JOIN_GROUP		must not have joined already
2793  *	MCAST_BLOCK_SOURCE		must have joined already
2794  *	MCAST_JOIN_SOURCE_GROUP		may have joined already
2795  *
2796  * fmode and src parameters may be used to determine which option is
2797  * being set, as follows (IPV6_JOIN_GROUP and MCAST_JOIN_GROUP options
2798  * are functionally equivalent):
2799  *	opt			fmode			v6src
2800  *	IPV6_JOIN_GROUP		MODE_IS_EXCLUDE		unspecified
2801  *	MCAST_JOIN_GROUP	MODE_IS_EXCLUDE		unspecified
2802  *	MCAST_BLOCK_SOURCE	MODE_IS_EXCLUDE		v6 addr
2803  *	MCAST_JOIN_SOURCE_GROUP	MODE_IS_INCLUDE		v6 addr
2804  *
2805  * Changing the filter mode is not allowed; if a matching ilg already
2806  * exists and fmode != ilg->ilg_fmode, EINVAL is returned.
2807  *
2808  * Verifies that there is a source address of appropriate scope for
2809  * the group; if not, EADDRNOTAVAIL is returned.
2810  *
2811  * Handles IPv4-mapped IPv6 multicast addresses by associating them
2812  * with the link-local ipif.  Assumes that if v6group is v4-mapped,
2813  * v6src is also v4-mapped.
2814  */
2815 int
2816 ip_opt_add_group_v6(conn_t *connp, boolean_t checkonly,
2817     const in6_addr_t *v6group, int ifindex, mcast_record_t fmode,
2818     const in6_addr_t *v6src, mblk_t *first_mp)
2819 {
2820 	ill_t *ill;
2821 	ipif_t	*ipif;
2822 	char buf[INET6_ADDRSTRLEN];
2823 	ipaddr_t v4group, v4src;
2824 	boolean_t isv6;
2825 	ipsq_t	*ipsq;
2826 	int	err;
2827 
2828 	err = ip_opt_check_v6(connp, v6group, &v4group, v6src, &v4src, &isv6,
2829 	    ifindex, first_mp, ip_restart_optmgmt, &ill, &ipif);
2830 	if (err != 0) {
2831 		if (err != EINPROGRESS) {
2832 			ip1dbg(("ip_opt_add_group_v6: no ill for group %s/"
2833 			    "index %d\n", inet_ntop(AF_INET6, v6group, buf,
2834 			    sizeof (buf)), ifindex));
2835 		}
2836 		return (err);
2837 	}
2838 	ASSERT((!isv6 && ipif != NULL) || (isv6 && ill != NULL));
2839 
2840 	/* operation is not supported on the virtual network interface */
2841 	if (isv6) {
2842 		if (IS_VNI(ill)) {
2843 			ill_refrele(ill);
2844 			return (EINVAL);
2845 		}
2846 	} else {
2847 		if (IS_VNI(ipif->ipif_ill)) {
2848 			ipif_refrele(ipif);
2849 			return (EINVAL);
2850 		}
2851 	}
2852 
2853 	if (checkonly) {
2854 		/*
2855 		 * do not do operation, just pretend to - new T_CHECK
2856 		 * semantics. The error return case above if encountered
2857 		 * considered a good enough "check" here.
2858 		 */
2859 		if (isv6)
2860 			ill_refrele(ill);
2861 		else
2862 			ipif_refrele(ipif);
2863 		return (0);
2864 	}
2865 
2866 	if (!isv6) {
2867 		IPSQ_ENTER_IPIF(ipif, connp, first_mp, ip_restart_optmgmt,
2868 		    ipsq, NEW_OP);
2869 		err = ilg_add(connp, v4group, ipif, fmode, v4src);
2870 		IPSQ_EXIT(ipsq);
2871 		ipif_refrele(ipif);
2872 	} else {
2873 		IPSQ_ENTER_ILL(ill, connp, first_mp, ip_restart_optmgmt,
2874 		    ipsq, NEW_OP);
2875 		err = ilg_add_v6(connp, v6group, ill, fmode, v6src);
2876 		IPSQ_EXIT(ipsq);
2877 		ill_refrele(ill);
2878 	}
2879 
2880 	return (err);
2881 }
2882 
2883 static int
2884 ip_opt_delete_group_excl(conn_t *connp, ipaddr_t group, ipif_t *ipif,
2885     mcast_record_t fmode, ipaddr_t src)
2886 {
2887 	ilg_t	*ilg;
2888 	in6_addr_t v6src;
2889 	boolean_t leaving = B_FALSE;
2890 
2891 	ASSERT(IAM_WRITER_IPIF(ipif));
2892 
2893 	/*
2894 	 * The ilg is valid only while we hold the conn lock. Once we drop
2895 	 * the lock, another thread can locate another ilg on this connp,
2896 	 * but on a different ipif, and delete it, and cause the ilg array
2897 	 * to be reallocated and copied. Hence do the ilg_delete before
2898 	 * dropping the lock.
2899 	 */
2900 	mutex_enter(&connp->conn_lock);
2901 	ilg = ilg_lookup_ipif(connp, group, ipif);
2902 	if ((ilg == NULL) || (ilg->ilg_flags & ILG_DELETED)) {
2903 		mutex_exit(&connp->conn_lock);
2904 		return (EADDRNOTAVAIL);
2905 	}
2906 
2907 	/*
2908 	 * Decide if we're actually deleting the ilg or just removing a
2909 	 * source filter address; if just removing an addr, make sure we
2910 	 * aren't trying to change the filter mode, and that the addr is
2911 	 * actually in our filter list already.  If we're removing the
2912 	 * last src in an include list, just delete the ilg.
2913 	 */
2914 	if (src == INADDR_ANY) {
2915 		v6src = ipv6_all_zeros;
2916 		leaving = B_TRUE;
2917 	} else {
2918 		int err = 0;
2919 		IN6_IPADDR_TO_V4MAPPED(src, &v6src);
2920 		if (fmode != ilg->ilg_fmode)
2921 			err = EINVAL;
2922 		else if (ilg->ilg_filter == NULL ||
2923 		    !list_has_addr(ilg->ilg_filter, &v6src))
2924 			err = EADDRNOTAVAIL;
2925 		if (err != 0) {
2926 			mutex_exit(&connp->conn_lock);
2927 			return (err);
2928 		}
2929 		if (fmode == MODE_IS_INCLUDE &&
2930 		    ilg->ilg_filter->sl_numsrc == 1) {
2931 			v6src = ipv6_all_zeros;
2932 			leaving = B_TRUE;
2933 		}
2934 	}
2935 
2936 	ilg_delete(connp, ilg, &v6src);
2937 	mutex_exit(&connp->conn_lock);
2938 
2939 	(void) ip_delmulti(group, ipif, B_FALSE, leaving);
2940 	return (0);
2941 }
2942 
2943 static int
2944 ip_opt_delete_group_excl_v6(conn_t *connp, const in6_addr_t *v6group,
2945     ill_t *ill, mcast_record_t fmode, const in6_addr_t *v6src)
2946 {
2947 	ilg_t	*ilg;
2948 	ill_t	*ilg_ill;
2949 	uint_t	ilg_orig_ifindex;
2950 	boolean_t leaving = B_TRUE;
2951 
2952 	ASSERT(IAM_WRITER_ILL(ill));
2953 
2954 	/*
2955 	 * Use the index that we originally used to join. We can't
2956 	 * use the ill directly because ilg_ill could point to
2957 	 * a new ill if things have moved.
2958 	 */
2959 	mutex_enter(&connp->conn_lock);
2960 	ilg = ilg_lookup_ill_index_v6(connp, v6group,
2961 	    ill->ill_phyint->phyint_ifindex);
2962 	if ((ilg == NULL) || (ilg->ilg_flags & ILG_DELETED)) {
2963 		mutex_exit(&connp->conn_lock);
2964 		return (EADDRNOTAVAIL);
2965 	}
2966 
2967 	/*
2968 	 * Decide if we're actually deleting the ilg or just removing a
2969 	 * source filter address; if just removing an addr, make sure we
2970 	 * aren't trying to change the filter mode, and that the addr is
2971 	 * actually in our filter list already.  If we're removing the
2972 	 * last src in an include list, just delete the ilg.
2973 	 */
2974 	if (!IN6_IS_ADDR_UNSPECIFIED(v6src)) {
2975 		int err = 0;
2976 		if (fmode != ilg->ilg_fmode)
2977 			err = EINVAL;
2978 		else if (ilg->ilg_filter == NULL ||
2979 		    !list_has_addr(ilg->ilg_filter, v6src))
2980 			err = EADDRNOTAVAIL;
2981 		if (err != 0) {
2982 			mutex_exit(&connp->conn_lock);
2983 			return (err);
2984 		}
2985 		if (fmode == MODE_IS_INCLUDE &&
2986 		    ilg->ilg_filter->sl_numsrc == 1)
2987 			v6src = NULL;
2988 		else
2989 			leaving = B_FALSE;
2990 	}
2991 
2992 	ilg_ill = ilg->ilg_ill;
2993 	ilg_orig_ifindex = ilg->ilg_orig_ifindex;
2994 	ilg_delete(connp, ilg, v6src);
2995 	mutex_exit(&connp->conn_lock);
2996 	(void) ip_delmulti_v6(v6group, ilg_ill, ilg_orig_ifindex,
2997 	    connp->conn_zoneid, B_FALSE, leaving);
2998 
2999 	return (0);
3000 }
3001 
3002 /*
3003  * Handle the following optmgmt:
3004  *	IP_DROP_MEMBERSHIP		will leave
3005  *	MCAST_LEAVE_GROUP		will leave
3006  *	IP_UNBLOCK_SOURCE		will not leave
3007  *	MCAST_UNBLOCK_SOURCE		will not leave
3008  *	IP_LEAVE_SOURCE_GROUP		may leave (if leaving last source)
3009  *	MCAST_LEAVE_SOURCE_GROUP	may leave (if leaving last source)
3010  *
3011  * fmode and src parameters may be used to determine which option is
3012  * being set, as follows (the IP_* and MCAST_* versions of each option
3013  * are functionally equivalent):
3014  *	opt			 fmode			src
3015  *	IP_DROP_MEMBERSHIP	 MODE_IS_INCLUDE	INADDR_ANY
3016  *	MCAST_LEAVE_GROUP	 MODE_IS_INCLUDE	INADDR_ANY
3017  *	IP_UNBLOCK_SOURCE	 MODE_IS_EXCLUDE	v4 addr
3018  *	MCAST_UNBLOCK_SOURCE	 MODE_IS_EXCLUDE	v4 addr
3019  *	IP_LEAVE_SOURCE_GROUP	 MODE_IS_INCLUDE	v4 addr
3020  *	MCAST_LEAVE_SOURCE_GROUP MODE_IS_INCLUDE	v4 addr
3021  *
3022  * Changing the filter mode is not allowed; if a matching ilg already
3023  * exists and fmode != ilg->ilg_fmode, EINVAL is returned.
3024  *
3025  * The interface to be used may be identified by an address or by an
3026  * index.  A pointer to the index is passed; if it is NULL, use the
3027  * address, otherwise, use the index.
3028  */
3029 int
3030 ip_opt_delete_group(conn_t *connp, boolean_t checkonly, ipaddr_t group,
3031     ipaddr_t ifaddr, uint_t *ifindexp, mcast_record_t fmode, ipaddr_t src,
3032     mblk_t *first_mp)
3033 {
3034 	ipif_t	*ipif;
3035 	ipsq_t	*ipsq;
3036 	int	err;
3037 	ill_t	*ill;
3038 
3039 	err = ip_opt_check(connp, group, src, ifaddr, ifindexp, first_mp,
3040 	    ip_restart_optmgmt, &ipif);
3041 	if (err != 0) {
3042 		if (err != EINPROGRESS) {
3043 			ip1dbg(("ip_opt_delete_group: no ipif for group "
3044 			    "0x%x, ifaddr 0x%x\n",
3045 			    (int)ntohl(group), (int)ntohl(ifaddr)));
3046 		}
3047 		return (err);
3048 	}
3049 	ASSERT(ipif != NULL);
3050 
3051 	ill = ipif->ipif_ill;
3052 	/* Operation not supported on a virtual network interface */
3053 	if (IS_VNI(ill)) {
3054 		ipif_refrele(ipif);
3055 		return (EINVAL);
3056 	}
3057 
3058 	if (checkonly) {
3059 		/*
3060 		 * do not do operation, just pretend to - new T_CHECK
3061 		 * semantics. The error return case above if encountered
3062 		 * considered a good enough "check" here.
3063 		 */
3064 		ipif_refrele(ipif);
3065 		return (0);
3066 	}
3067 
3068 	IPSQ_ENTER_IPIF(ipif, connp, first_mp, ip_restart_optmgmt, ipsq,
3069 	    NEW_OP);
3070 	err = ip_opt_delete_group_excl(connp, group, ipif, fmode, src);
3071 	IPSQ_EXIT(ipsq);
3072 
3073 	ipif_refrele(ipif);
3074 	return (err);
3075 }
3076 
3077 /*
3078  * Handle the following optmgmt:
3079  *	IPV6_LEAVE_GROUP		will leave
3080  *	MCAST_LEAVE_GROUP		will leave
3081  *	MCAST_UNBLOCK_SOURCE		will not leave
3082  *	MCAST_LEAVE_SOURCE_GROUP	may leave (if leaving last source)
3083  *
3084  * fmode and src parameters may be used to determine which option is
3085  * being set, as follows (IPV6_LEAVE_GROUP and MCAST_LEAVE_GROUP options
3086  * are functionally equivalent):
3087  *	opt			 fmode			v6src
3088  *	IPV6_LEAVE_GROUP	 MODE_IS_INCLUDE	unspecified
3089  *	MCAST_LEAVE_GROUP	 MODE_IS_INCLUDE	unspecified
3090  *	MCAST_UNBLOCK_SOURCE	 MODE_IS_EXCLUDE	v6 addr
3091  *	MCAST_LEAVE_SOURCE_GROUP MODE_IS_INCLUDE	v6 addr
3092  *
3093  * Changing the filter mode is not allowed; if a matching ilg already
3094  * exists and fmode != ilg->ilg_fmode, EINVAL is returned.
3095  *
3096  * Handles IPv4-mapped IPv6 multicast addresses by associating them
3097  * with the link-local ipif.  Assumes that if v6group is v4-mapped,
3098  * v6src is also v4-mapped.
3099  */
3100 int
3101 ip_opt_delete_group_v6(conn_t *connp, boolean_t checkonly,
3102     const in6_addr_t *v6group, int ifindex, mcast_record_t fmode,
3103     const in6_addr_t *v6src, mblk_t *first_mp)
3104 {
3105 	ill_t *ill;
3106 	ipif_t	*ipif;
3107 	char	buf[INET6_ADDRSTRLEN];
3108 	ipaddr_t v4group, v4src;
3109 	boolean_t isv6;
3110 	ipsq_t	*ipsq;
3111 	int	err;
3112 
3113 	err = ip_opt_check_v6(connp, v6group, &v4group, v6src, &v4src, &isv6,
3114 	    ifindex, first_mp, ip_restart_optmgmt, &ill, &ipif);
3115 	if (err != 0) {
3116 		if (err != EINPROGRESS) {
3117 			ip1dbg(("ip_opt_delete_group_v6: no ill for group %s/"
3118 			    "index %d\n", inet_ntop(AF_INET6, v6group, buf,
3119 			    sizeof (buf)), ifindex));
3120 		}
3121 		return (err);
3122 	}
3123 	ASSERT((isv6 && ill != NULL) || (!isv6 && ipif != NULL));
3124 
3125 	/* operation is not supported on the virtual network interface */
3126 	if (isv6) {
3127 		if (IS_VNI(ill)) {
3128 			ill_refrele(ill);
3129 			return (EINVAL);
3130 		}
3131 	} else {
3132 		if (IS_VNI(ipif->ipif_ill)) {
3133 			ipif_refrele(ipif);
3134 			return (EINVAL);
3135 		}
3136 	}
3137 
3138 	if (checkonly) {
3139 		/*
3140 		 * do not do operation, just pretend to - new T_CHECK
3141 		 * semantics. The error return case above if encountered
3142 		 * considered a good enough "check" here.
3143 		 */
3144 		if (isv6)
3145 			ill_refrele(ill);
3146 		else
3147 			ipif_refrele(ipif);
3148 		return (0);
3149 	}
3150 
3151 	if (!isv6) {
3152 		IPSQ_ENTER_IPIF(ipif, connp, first_mp, ip_restart_optmgmt,
3153 		    ipsq, NEW_OP);
3154 		err = ip_opt_delete_group_excl(connp, v4group, ipif, fmode,
3155 		    v4src);
3156 		IPSQ_EXIT(ipsq);
3157 		ipif_refrele(ipif);
3158 	} else {
3159 		IPSQ_ENTER_ILL(ill, connp, first_mp, ip_restart_optmgmt,
3160 		    ipsq, NEW_OP);
3161 		err = ip_opt_delete_group_excl_v6(connp, v6group, ill, fmode,
3162 		    v6src);
3163 		IPSQ_EXIT(ipsq);
3164 		ill_refrele(ill);
3165 	}
3166 
3167 	return (err);
3168 }
3169 
3170 /*
3171  * Group mgmt for upper conn that passes things down
3172  * to the interface multicast list (and DLPI)
3173  * These routines can handle new style options that specify an interface name
3174  * as opposed to an interface address (needed for general handling of
3175  * unnumbered interfaces.)
3176  */
3177 
3178 /*
3179  * Add a group to an upper conn group data structure and pass things down
3180  * to the interface multicast list (and DLPI)
3181  */
3182 static int
3183 ilg_add(conn_t *connp, ipaddr_t group, ipif_t *ipif, mcast_record_t fmode,
3184     ipaddr_t src)
3185 {
3186 	int	error = 0;
3187 	ill_t	*ill;
3188 	ilg_t	*ilg;
3189 	ilg_stat_t ilgstat;
3190 	slist_t	*new_filter = NULL;
3191 	int	new_fmode;
3192 
3193 	ASSERT(IAM_WRITER_IPIF(ipif));
3194 
3195 	ill = ipif->ipif_ill;
3196 
3197 	if (!(ill->ill_flags & ILLF_MULTICAST))
3198 		return (EADDRNOTAVAIL);
3199 
3200 	/*
3201 	 * conn_ilg[] is protected by conn_lock. Need to hold the conn_lock
3202 	 * to walk the conn_ilg[] list in ilg_lookup_ipif(); also needed to
3203 	 * serialize 2 threads doing join (sock, group1, hme0:0) and
3204 	 * (sock, group2, hme1:0) where hme0 and hme1 map to different ipsqs,
3205 	 * but both operations happen on the same conn.
3206 	 */
3207 	mutex_enter(&connp->conn_lock);
3208 	ilg = ilg_lookup_ipif(connp, group, ipif);
3209 
3210 	/*
3211 	 * Depending on the option we're handling, may or may not be okay
3212 	 * if group has already been added.  Figure out our rules based
3213 	 * on fmode and src params.  Also make sure there's enough room
3214 	 * in the filter if we're adding a source to an existing filter.
3215 	 */
3216 	if (src == INADDR_ANY) {
3217 		/* we're joining for all sources, must not have joined */
3218 		if (ilg != NULL)
3219 			error = EADDRINUSE;
3220 	} else {
3221 		if (fmode == MODE_IS_EXCLUDE) {
3222 			/* (excl {addr}) => block source, must have joined */
3223 			if (ilg == NULL)
3224 				error = EADDRNOTAVAIL;
3225 		}
3226 		/* (incl {addr}) => join source, may have joined */
3227 
3228 		if (ilg != NULL &&
3229 		    SLIST_CNT(ilg->ilg_filter) == MAX_FILTER_SIZE)
3230 			error = ENOBUFS;
3231 	}
3232 	if (error != 0) {
3233 		mutex_exit(&connp->conn_lock);
3234 		return (error);
3235 	}
3236 
3237 	ASSERT(!(ipif->ipif_state_flags & IPIF_CONDEMNED));
3238 
3239 	/*
3240 	 * Alloc buffer to copy new state into (see below) before
3241 	 * we make any changes, so we can bail if it fails.
3242 	 */
3243 	if ((new_filter = l_alloc()) == NULL) {
3244 		mutex_exit(&connp->conn_lock);
3245 		return (ENOMEM);
3246 	}
3247 
3248 	if (ilg == NULL) {
3249 		ilgstat = ILGSTAT_NEW;
3250 		if ((ilg = conn_ilg_alloc(connp)) == NULL) {
3251 			mutex_exit(&connp->conn_lock);
3252 			l_free(new_filter);
3253 			return (ENOMEM);
3254 		}
3255 		if (src != INADDR_ANY) {
3256 			ilg->ilg_filter = l_alloc();
3257 			if (ilg->ilg_filter == NULL) {
3258 				ilg_delete(connp, ilg, NULL);
3259 				mutex_exit(&connp->conn_lock);
3260 				l_free(new_filter);
3261 				return (ENOMEM);
3262 			}
3263 			ilg->ilg_filter->sl_numsrc = 1;
3264 			IN6_IPADDR_TO_V4MAPPED(src,
3265 			    &ilg->ilg_filter->sl_addr[0]);
3266 		}
3267 		if (group == INADDR_ANY) {
3268 			ilg->ilg_v6group = ipv6_all_zeros;
3269 		} else {
3270 			IN6_IPADDR_TO_V4MAPPED(group, &ilg->ilg_v6group);
3271 		}
3272 		ilg->ilg_ipif = ipif;
3273 		ilg->ilg_ill = NULL;
3274 		ilg->ilg_orig_ifindex = 0;
3275 		ilg->ilg_fmode = fmode;
3276 	} else {
3277 		int index;
3278 		in6_addr_t v6src;
3279 		ilgstat = ILGSTAT_CHANGE;
3280 		if (ilg->ilg_fmode != fmode || src == INADDR_ANY) {
3281 			mutex_exit(&connp->conn_lock);
3282 			l_free(new_filter);
3283 			return (EINVAL);
3284 		}
3285 		if (ilg->ilg_filter == NULL) {
3286 			ilg->ilg_filter = l_alloc();
3287 			if (ilg->ilg_filter == NULL) {
3288 				mutex_exit(&connp->conn_lock);
3289 				l_free(new_filter);
3290 				return (ENOMEM);
3291 			}
3292 		}
3293 		IN6_IPADDR_TO_V4MAPPED(src, &v6src);
3294 		if (list_has_addr(ilg->ilg_filter, &v6src)) {
3295 			mutex_exit(&connp->conn_lock);
3296 			l_free(new_filter);
3297 			return (EADDRNOTAVAIL);
3298 		}
3299 		index = ilg->ilg_filter->sl_numsrc++;
3300 		ilg->ilg_filter->sl_addr[index] = v6src;
3301 	}
3302 
3303 	/*
3304 	 * Save copy of ilg's filter state to pass to other functions,
3305 	 * so we can release conn_lock now.
3306 	 */
3307 	new_fmode = ilg->ilg_fmode;
3308 	l_copy(ilg->ilg_filter, new_filter);
3309 
3310 	mutex_exit(&connp->conn_lock);
3311 
3312 	error = ip_addmulti(group, ipif, ilgstat, new_fmode, new_filter);
3313 	if (error != 0) {
3314 		/*
3315 		 * Need to undo what we did before calling ip_addmulti()!
3316 		 * Must look up the ilg again since we've not been holding
3317 		 * conn_lock.
3318 		 */
3319 		in6_addr_t v6src;
3320 		if (ilgstat == ILGSTAT_NEW)
3321 			v6src = ipv6_all_zeros;
3322 		else
3323 			IN6_IPADDR_TO_V4MAPPED(src, &v6src);
3324 		mutex_enter(&connp->conn_lock);
3325 		ilg = ilg_lookup_ipif(connp, group, ipif);
3326 		ASSERT(ilg != NULL);
3327 		ilg_delete(connp, ilg, &v6src);
3328 		mutex_exit(&connp->conn_lock);
3329 		l_free(new_filter);
3330 		return (error);
3331 	}
3332 
3333 	l_free(new_filter);
3334 	return (0);
3335 }
3336 
3337 static int
3338 ilg_add_v6(conn_t *connp, const in6_addr_t *v6group, ill_t *ill,
3339     mcast_record_t fmode, const in6_addr_t *v6src)
3340 {
3341 	int	error = 0;
3342 	int	orig_ifindex;
3343 	ilg_t	*ilg;
3344 	ilg_stat_t ilgstat;
3345 	slist_t	*new_filter = NULL;
3346 	int	new_fmode;
3347 
3348 	ASSERT(IAM_WRITER_ILL(ill));
3349 
3350 	if (!(ill->ill_flags & ILLF_MULTICAST))
3351 		return (EADDRNOTAVAIL);
3352 
3353 	/*
3354 	 * conn_lock protects the ilg list.  Serializes 2 threads doing
3355 	 * join (sock, group1, hme0) and (sock, group2, hme1) where hme0
3356 	 * and hme1 map to different ipsq's, but both operations happen
3357 	 * on the same conn.
3358 	 */
3359 	mutex_enter(&connp->conn_lock);
3360 
3361 	/*
3362 	 * Use the ifindex to do the lookup. We can't use the ill
3363 	 * directly because ilg_ill could point to a different ill if
3364 	 * things have moved.
3365 	 */
3366 	orig_ifindex = ill->ill_phyint->phyint_ifindex;
3367 	ilg = ilg_lookup_ill_index_v6(connp, v6group, orig_ifindex);
3368 
3369 	/*
3370 	 * Depending on the option we're handling, may or may not be okay
3371 	 * if group has already been added.  Figure out our rules based
3372 	 * on fmode and src params.  Also make sure there's enough room
3373 	 * in the filter if we're adding a source to an existing filter.
3374 	 */
3375 	if (IN6_IS_ADDR_UNSPECIFIED(v6src)) {
3376 		/* we're joining for all sources, must not have joined */
3377 		if (ilg != NULL)
3378 			error = EADDRINUSE;
3379 	} else {
3380 		if (fmode == MODE_IS_EXCLUDE) {
3381 			/* (excl {addr}) => block source, must have joined */
3382 			if (ilg == NULL)
3383 				error = EADDRNOTAVAIL;
3384 		}
3385 		/* (incl {addr}) => join source, may have joined */
3386 
3387 		if (ilg != NULL &&
3388 		    SLIST_CNT(ilg->ilg_filter) == MAX_FILTER_SIZE)
3389 			error = ENOBUFS;
3390 	}
3391 	if (error != 0) {
3392 		mutex_exit(&connp->conn_lock);
3393 		return (error);
3394 	}
3395 
3396 	/*
3397 	 * Alloc buffer to copy new state into (see below) before
3398 	 * we make any changes, so we can bail if it fails.
3399 	 */
3400 	if ((new_filter = l_alloc()) == NULL) {
3401 		mutex_exit(&connp->conn_lock);
3402 		return (ENOMEM);
3403 	}
3404 
3405 	if (ilg == NULL) {
3406 		if ((ilg = conn_ilg_alloc(connp)) == NULL) {
3407 			mutex_exit(&connp->conn_lock);
3408 			l_free(new_filter);
3409 			return (ENOMEM);
3410 		}
3411 		if (!IN6_IS_ADDR_UNSPECIFIED(v6src)) {
3412 			ilg->ilg_filter = l_alloc();
3413 			if (ilg->ilg_filter == NULL) {
3414 				ilg_delete(connp, ilg, NULL);
3415 				mutex_exit(&connp->conn_lock);
3416 				l_free(new_filter);
3417 				return (ENOMEM);
3418 			}
3419 			ilg->ilg_filter->sl_numsrc = 1;
3420 			ilg->ilg_filter->sl_addr[0] = *v6src;
3421 		}
3422 		ilgstat = ILGSTAT_NEW;
3423 		ilg->ilg_v6group = *v6group;
3424 		ilg->ilg_fmode = fmode;
3425 		ilg->ilg_ipif = NULL;
3426 		/*
3427 		 * Choose our target ill to join on. This might be different
3428 		 * from the ill we've been given if it's currently down and
3429 		 * part of a group.
3430 		 *
3431 		 * new ill is not refheld; we are writer.
3432 		 */
3433 		ill = ip_choose_multi_ill(ill, v6group);
3434 		ASSERT(!(ill->ill_state_flags & ILL_CONDEMNED));
3435 		ilg->ilg_ill = ill;
3436 		/*
3437 		 * Remember the orig_ifindex that we joined on, so that we
3438 		 * can successfully delete them later on and also search
3439 		 * for duplicates if the application wants to join again.
3440 		 */
3441 		ilg->ilg_orig_ifindex = orig_ifindex;
3442 	} else {
3443 		int index;
3444 		if (ilg->ilg_fmode != fmode || IN6_IS_ADDR_UNSPECIFIED(v6src)) {
3445 			mutex_exit(&connp->conn_lock);
3446 			l_free(new_filter);
3447 			return (EINVAL);
3448 		}
3449 		if (ilg->ilg_filter == NULL) {
3450 			ilg->ilg_filter = l_alloc();
3451 			if (ilg->ilg_filter == NULL) {
3452 				mutex_exit(&connp->conn_lock);
3453 				l_free(new_filter);
3454 				return (ENOMEM);
3455 			}
3456 		}
3457 		if (list_has_addr(ilg->ilg_filter, v6src)) {
3458 			mutex_exit(&connp->conn_lock);
3459 			l_free(new_filter);
3460 			return (EADDRNOTAVAIL);
3461 		}
3462 		ilgstat = ILGSTAT_CHANGE;
3463 		index = ilg->ilg_filter->sl_numsrc++;
3464 		ilg->ilg_filter->sl_addr[index] = *v6src;
3465 		/*
3466 		 * The current ill might be different from the one we were
3467 		 * asked to join on (if failover has occurred); we should
3468 		 * join on the ill stored in the ilg.  The original ill
3469 		 * is noted in ilg_orig_ifindex, which matched our request.
3470 		 */
3471 		ill = ilg->ilg_ill;
3472 	}
3473 
3474 	/*
3475 	 * Save copy of ilg's filter state to pass to other functions,
3476 	 * so we can release conn_lock now.
3477 	 */
3478 	new_fmode = ilg->ilg_fmode;
3479 	l_copy(ilg->ilg_filter, new_filter);
3480 
3481 	mutex_exit(&connp->conn_lock);
3482 
3483 	/*
3484 	 * Now update the ill. We wait to do this until after the ilg
3485 	 * has been updated because we need to update the src filter
3486 	 * info for the ill, which involves looking at the status of
3487 	 * all the ilgs associated with this group/interface pair.
3488 	 */
3489 	error = ip_addmulti_v6(v6group, ill, orig_ifindex, connp->conn_zoneid,
3490 	    ilgstat, new_fmode, new_filter);
3491 	if (error != 0) {
3492 		/*
3493 		 * But because we waited, we have to undo the ilg update
3494 		 * if ip_addmulti_v6() fails.  We also must lookup ilg
3495 		 * again, since we've not been holding conn_lock.
3496 		 */
3497 		in6_addr_t delsrc =
3498 		    (ilgstat == ILGSTAT_NEW) ? ipv6_all_zeros : *v6src;
3499 		mutex_enter(&connp->conn_lock);
3500 		ilg = ilg_lookup_ill_index_v6(connp, v6group, orig_ifindex);
3501 		ASSERT(ilg != NULL);
3502 		ilg_delete(connp, ilg, &delsrc);
3503 		mutex_exit(&connp->conn_lock);
3504 		l_free(new_filter);
3505 		return (error);
3506 	}
3507 
3508 	l_free(new_filter);
3509 
3510 	return (0);
3511 }
3512 
3513 /*
3514  * Find an IPv4 ilg matching group, ill and source
3515  */
3516 ilg_t *
3517 ilg_lookup_ill_withsrc(conn_t *connp, ipaddr_t group, ipaddr_t src, ill_t *ill)
3518 {
3519 	in6_addr_t v6group, v6src;
3520 	int i;
3521 	boolean_t isinlist;
3522 	ilg_t *ilg;
3523 	ipif_t *ipif;
3524 	ill_t *ilg_ill;
3525 
3526 	ASSERT(MUTEX_HELD(&connp->conn_lock));
3527 
3528 	/*
3529 	 * INADDR_ANY is represented as the IPv6 unspecified addr.
3530 	 */
3531 	if (group == INADDR_ANY)
3532 		v6group = ipv6_all_zeros;
3533 	else
3534 		IN6_IPADDR_TO_V4MAPPED(group, &v6group);
3535 
3536 	for (i = 0; i < connp->conn_ilg_inuse; i++) {
3537 		ilg = &connp->conn_ilg[i];
3538 		if ((ipif = ilg->ilg_ipif) == NULL ||
3539 		    (ilg->ilg_flags & ILG_DELETED) != 0)
3540 			continue;
3541 		ASSERT(ilg->ilg_ill == NULL);
3542 		ilg_ill = ipif->ipif_ill;
3543 		ASSERT(!ilg_ill->ill_isv6);
3544 		if (ilg_ill == ill &&
3545 		    IN6_ARE_ADDR_EQUAL(&ilg->ilg_v6group, &v6group)) {
3546 			if (SLIST_IS_EMPTY(ilg->ilg_filter)) {
3547 				/* no source filter, so this is a match */
3548 				return (ilg);
3549 			}
3550 			break;
3551 		}
3552 	}
3553 	if (i == connp->conn_ilg_inuse)
3554 		return (NULL);
3555 
3556 	/*
3557 	 * we have an ilg with matching ill and group; but
3558 	 * the ilg has a source list that we must check.
3559 	 */
3560 	IN6_IPADDR_TO_V4MAPPED(src, &v6src);
3561 	isinlist = B_FALSE;
3562 	for (i = 0; i < ilg->ilg_filter->sl_numsrc; i++) {
3563 		if (IN6_ARE_ADDR_EQUAL(&v6src, &ilg->ilg_filter->sl_addr[i])) {
3564 			isinlist = B_TRUE;
3565 			break;
3566 		}
3567 	}
3568 
3569 	if ((isinlist && ilg->ilg_fmode == MODE_IS_INCLUDE) ||
3570 	    (!isinlist && ilg->ilg_fmode == MODE_IS_EXCLUDE))
3571 		return (ilg);
3572 
3573 	return (NULL);
3574 }
3575 
3576 /*
3577  * Find an IPv6 ilg matching group, ill, and source
3578  */
3579 ilg_t *
3580 ilg_lookup_ill_withsrc_v6(conn_t *connp, const in6_addr_t *v6group,
3581     const in6_addr_t *v6src, ill_t *ill)
3582 {
3583 	int i;
3584 	boolean_t isinlist;
3585 	ilg_t *ilg;
3586 	ill_t *ilg_ill;
3587 
3588 	ASSERT(MUTEX_HELD(&connp->conn_lock));
3589 
3590 	for (i = 0; i < connp->conn_ilg_inuse; i++) {
3591 		ilg = &connp->conn_ilg[i];
3592 		if ((ilg_ill = ilg->ilg_ill) == NULL ||
3593 		    (ilg->ilg_flags & ILG_DELETED) != 0)
3594 			continue;
3595 		ASSERT(ilg->ilg_ipif == NULL);
3596 		ASSERT(ilg_ill->ill_isv6);
3597 		if (ilg_ill == ill &&
3598 		    IN6_ARE_ADDR_EQUAL(&ilg->ilg_v6group, v6group)) {
3599 			if (SLIST_IS_EMPTY(ilg->ilg_filter)) {
3600 				/* no source filter, so this is a match */
3601 				return (ilg);
3602 			}
3603 			break;
3604 		}
3605 	}
3606 	if (i == connp->conn_ilg_inuse)
3607 		return (NULL);
3608 
3609 	/*
3610 	 * we have an ilg with matching ill and group; but
3611 	 * the ilg has a source list that we must check.
3612 	 */
3613 	isinlist = B_FALSE;
3614 	for (i = 0; i < ilg->ilg_filter->sl_numsrc; i++) {
3615 		if (IN6_ARE_ADDR_EQUAL(v6src, &ilg->ilg_filter->sl_addr[i])) {
3616 			isinlist = B_TRUE;
3617 			break;
3618 		}
3619 	}
3620 
3621 	if ((isinlist && ilg->ilg_fmode == MODE_IS_INCLUDE) ||
3622 	    (!isinlist && ilg->ilg_fmode == MODE_IS_EXCLUDE))
3623 		return (ilg);
3624 
3625 	return (NULL);
3626 }
3627 
3628 /*
3629  * Get the ilg whose ilg_orig_ifindex is associated with ifindex.
3630  * This is useful when the interface fails and we have moved
3631  * to a new ill, but still would like to locate using the index
3632  * that we originally used to join. Used only for IPv6 currently.
3633  */
3634 static ilg_t *
3635 ilg_lookup_ill_index_v6(conn_t *connp, const in6_addr_t *v6group, int ifindex)
3636 {
3637 	ilg_t	*ilg;
3638 	int	i;
3639 
3640 	ASSERT(MUTEX_HELD(&connp->conn_lock));
3641 	for (i = 0; i < connp->conn_ilg_inuse; i++) {
3642 		ilg = &connp->conn_ilg[i];
3643 		if (ilg->ilg_ill == NULL ||
3644 		    (ilg->ilg_flags & ILG_DELETED) != 0)
3645 			continue;
3646 		/* ilg_ipif is NULL for V6 */
3647 		ASSERT(ilg->ilg_ipif == NULL);
3648 		ASSERT(ilg->ilg_orig_ifindex != 0);
3649 		if (IN6_ARE_ADDR_EQUAL(&ilg->ilg_v6group, v6group) &&
3650 		    ilg->ilg_orig_ifindex == ifindex) {
3651 			return (ilg);
3652 		}
3653 	}
3654 	return (NULL);
3655 }
3656 
3657 /*
3658  * Find an IPv6 ilg matching group and ill
3659  */
3660 ilg_t *
3661 ilg_lookup_ill_v6(conn_t *connp, const in6_addr_t *v6group, ill_t *ill)
3662 {
3663 	ilg_t	*ilg;
3664 	int	i;
3665 	ill_t 	*mem_ill;
3666 
3667 	ASSERT(MUTEX_HELD(&connp->conn_lock));
3668 
3669 	for (i = 0; i < connp->conn_ilg_inuse; i++) {
3670 		ilg = &connp->conn_ilg[i];
3671 		if ((mem_ill = ilg->ilg_ill) == NULL ||
3672 		    (ilg->ilg_flags & ILG_DELETED) != 0)
3673 			continue;
3674 		ASSERT(ilg->ilg_ipif == NULL);
3675 		ASSERT(mem_ill->ill_isv6);
3676 		if (mem_ill == ill &&
3677 		    IN6_ARE_ADDR_EQUAL(&ilg->ilg_v6group, v6group))
3678 			return (ilg);
3679 	}
3680 	return (NULL);
3681 }
3682 
3683 /*
3684  * Find an IPv4 ilg matching group and ipif
3685  */
3686 static ilg_t *
3687 ilg_lookup_ipif(conn_t *connp, ipaddr_t group, ipif_t *ipif)
3688 {
3689 	in6_addr_t v6group;
3690 	int	i;
3691 	ilg_t	*ilg;
3692 
3693 	ASSERT(MUTEX_HELD(&connp->conn_lock));
3694 	ASSERT(!ipif->ipif_ill->ill_isv6);
3695 
3696 	if (group == INADDR_ANY)
3697 		v6group = ipv6_all_zeros;
3698 	else
3699 		IN6_IPADDR_TO_V4MAPPED(group, &v6group);
3700 
3701 	for (i = 0; i < connp->conn_ilg_inuse; i++) {
3702 		ilg = &connp->conn_ilg[i];
3703 		if ((ilg->ilg_flags & ILG_DELETED) == 0 &&
3704 		    IN6_ARE_ADDR_EQUAL(&ilg->ilg_v6group, &v6group) &&
3705 		    ilg->ilg_ipif == ipif)
3706 			return (ilg);
3707 	}
3708 	return (NULL);
3709 }
3710 
3711 /*
3712  * If a source address is passed in (src != NULL and src is not
3713  * unspecified), remove the specified src addr from the given ilg's
3714  * filter list, else delete the ilg.
3715  */
3716 static void
3717 ilg_delete(conn_t *connp, ilg_t *ilg, const in6_addr_t *src)
3718 {
3719 	int	i;
3720 
3721 	ASSERT((ilg->ilg_ipif != NULL) ^ (ilg->ilg_ill != NULL));
3722 	ASSERT(ilg->ilg_ipif == NULL || IAM_WRITER_IPIF(ilg->ilg_ipif));
3723 	ASSERT(ilg->ilg_ill == NULL || IAM_WRITER_ILL(ilg->ilg_ill));
3724 	ASSERT(MUTEX_HELD(&connp->conn_lock));
3725 	ASSERT(!(ilg->ilg_flags & ILG_DELETED));
3726 
3727 	if (src == NULL || IN6_IS_ADDR_UNSPECIFIED(src)) {
3728 		if (connp->conn_ilg_walker_cnt != 0) {
3729 			ilg->ilg_flags |= ILG_DELETED;
3730 			return;
3731 		}
3732 
3733 		FREE_SLIST(ilg->ilg_filter);
3734 
3735 		i = ilg - &connp->conn_ilg[0];
3736 		ASSERT(i >= 0 && i < connp->conn_ilg_inuse);
3737 
3738 		/* Move other entries up one step */
3739 		connp->conn_ilg_inuse--;
3740 		for (; i < connp->conn_ilg_inuse; i++)
3741 			connp->conn_ilg[i] = connp->conn_ilg[i+1];
3742 
3743 		if (connp->conn_ilg_inuse == 0) {
3744 			mi_free((char *)connp->conn_ilg);
3745 			connp->conn_ilg = NULL;
3746 			cv_broadcast(&connp->conn_refcv);
3747 		}
3748 	} else {
3749 		l_remove(ilg->ilg_filter, src);
3750 	}
3751 }
3752 
3753 /*
3754  * Called from conn close. No new ilg can be added or removed.
3755  * because CONN_CLOSING has been set by ip_close. ilg_add / ilg_delete
3756  * will return error if conn has started closing.
3757  */
3758 void
3759 ilg_delete_all(conn_t *connp)
3760 {
3761 	int	i;
3762 	ipif_t	*ipif = NULL;
3763 	ill_t	*ill = NULL;
3764 	ilg_t	*ilg;
3765 	in6_addr_t v6group;
3766 	boolean_t success;
3767 	ipsq_t	*ipsq;
3768 	int	orig_ifindex;
3769 
3770 	mutex_enter(&connp->conn_lock);
3771 retry:
3772 	ILG_WALKER_HOLD(connp);
3773 	for (i = connp->conn_ilg_inuse - 1; i >= 0; ) {
3774 		ilg = &connp->conn_ilg[i];
3775 		/*
3776 		 * Since this walk is not atomic (we drop the
3777 		 * conn_lock and wait in ipsq_enter) we need
3778 		 * to check for the ILG_DELETED flag.
3779 		 */
3780 		if (ilg->ilg_flags & ILG_DELETED) {
3781 			/* Go to the next ilg */
3782 			i--;
3783 			continue;
3784 		}
3785 		v6group = ilg->ilg_v6group;
3786 
3787 		if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
3788 			ipif = ilg->ilg_ipif;
3789 			ill = ipif->ipif_ill;
3790 		} else {
3791 			ipif = NULL;
3792 			ill = ilg->ilg_ill;
3793 		}
3794 		/*
3795 		 * We may not be able to refhold the ill if the ill/ipif
3796 		 * is changing. But we need to make sure that the ill will
3797 		 * not vanish. So we just bump up the ill_waiter count.
3798 		 * If we are unable to do even that, then the ill is closing,
3799 		 * in which case the unplumb thread will handle the cleanup,
3800 		 * and we move on to the next ilg.
3801 		 */
3802 		if (!ill_waiter_inc(ill)) {
3803 			/* Go to the next ilg */
3804 			i--;
3805 			continue;
3806 		}
3807 		mutex_exit(&connp->conn_lock);
3808 		/*
3809 		 * To prevent deadlock between ill close which waits inside
3810 		 * the perimeter, and conn close, ipsq_enter returns error,
3811 		 * the moment ILL_CONDEMNED is set, in which case ill close
3812 		 * takes responsibility to cleanup the ilgs. Note that we
3813 		 * have not yet set condemned flag, otherwise the conn can't
3814 		 * be refheld for cleanup by those routines and it would be
3815 		 * a mutual deadlock.
3816 		 */
3817 		success = ipsq_enter(ill, B_FALSE);
3818 		ipsq = ill->ill_phyint->phyint_ipsq;
3819 		ill_waiter_dcr(ill);
3820 		mutex_enter(&connp->conn_lock);
3821 		if (!success) {
3822 			/* Go to the next ilg */
3823 			i--;
3824 			continue;
3825 		}
3826 
3827 		/*
3828 		 * Make sure that nothing has changed under. For eg.
3829 		 * a failover/failback can change ilg_ill while we were
3830 		 * waiting to become exclusive above
3831 		 */
3832 		if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
3833 			ipif = ilg->ilg_ipif;
3834 			ill = ipif->ipif_ill;
3835 		} else {
3836 			ipif = NULL;
3837 			ill = ilg->ilg_ill;
3838 		}
3839 		if (!IAM_WRITER_ILL(ill) || (ilg->ilg_flags & ILG_DELETED)) {
3840 			/*
3841 			 * The ilg has changed under us probably due
3842 			 * to a failover or unplumb. Retry on the same ilg.
3843 			 */
3844 			mutex_exit(&connp->conn_lock);
3845 			ipsq_exit(ipsq, B_TRUE, B_TRUE);
3846 			mutex_enter(&connp->conn_lock);
3847 			continue;
3848 		}
3849 		v6group = ilg->ilg_v6group;
3850 		orig_ifindex = ilg->ilg_orig_ifindex;
3851 		ilg_delete(connp, ilg, NULL);
3852 		mutex_exit(&connp->conn_lock);
3853 
3854 		if (ipif != NULL)
3855 			(void) ip_delmulti(V4_PART_OF_V6(v6group), ipif,
3856 			    B_FALSE, B_TRUE);
3857 
3858 		else
3859 			(void) ip_delmulti_v6(&v6group, ill, orig_ifindex,
3860 			    connp->conn_zoneid, B_FALSE, B_TRUE);
3861 
3862 		ipsq_exit(ipsq, B_TRUE, B_TRUE);
3863 		mutex_enter(&connp->conn_lock);
3864 		/* Go to the next ilg */
3865 		i--;
3866 	}
3867 	ILG_WALKER_RELE(connp);
3868 
3869 	/* If any ill was skipped above wait and retry */
3870 	if (connp->conn_ilg_inuse != 0) {
3871 		cv_wait(&connp->conn_refcv, &connp->conn_lock);
3872 		goto retry;
3873 	}
3874 	mutex_exit(&connp->conn_lock);
3875 }
3876 
3877 /*
3878  * Called from ill close by ipcl_walk for clearing conn_ilg and
3879  * conn_multicast_ipif for a given ipif. conn is held by caller.
3880  * Note that ipcl_walk only walks conns that are not yet condemned.
3881  * condemned conns can't be refheld. For this reason, conn must become clean
3882  * first, i.e. it must not refer to any ill/ire/ipif and then only set
3883  * condemned flag.
3884  */
3885 static void
3886 conn_delete_ipif(conn_t *connp, caddr_t arg)
3887 {
3888 	ipif_t	*ipif = (ipif_t *)arg;
3889 	int	i;
3890 	char	group_buf1[INET6_ADDRSTRLEN];
3891 	char	group_buf2[INET6_ADDRSTRLEN];
3892 	ipaddr_t group;
3893 	ilg_t	*ilg;
3894 
3895 	/*
3896 	 * Even though conn_ilg_inuse can change while we are in this loop,
3897 	 * i.e.ilgs can be created or deleted on this connp, no new ilgs can
3898 	 * be created or deleted for this connp, on this ill, since this ill
3899 	 * is the perimeter. So we won't miss any ilg in this cleanup.
3900 	 */
3901 	mutex_enter(&connp->conn_lock);
3902 
3903 	/*
3904 	 * Increment the walker count, so that ilg repacking does not
3905 	 * occur while we are in the loop.
3906 	 */
3907 	ILG_WALKER_HOLD(connp);
3908 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
3909 		ilg = &connp->conn_ilg[i];
3910 		if (ilg->ilg_ipif != ipif || (ilg->ilg_flags & ILG_DELETED))
3911 			continue;
3912 		/*
3913 		 * ip_close cannot be cleaning this ilg at the same time.
3914 		 * since it also has to execute in this ill's perimeter which
3915 		 * we are now holding. Only a clean conn can be condemned.
3916 		 */
3917 		ASSERT(!(connp->conn_state_flags & CONN_CONDEMNED));
3918 
3919 		/* Blow away the membership */
3920 		ip1dbg(("conn_delete_ilg_ipif: %s on %s (%s)\n",
3921 		    inet_ntop(AF_INET6, &connp->conn_ilg[i].ilg_v6group,
3922 		    group_buf1, sizeof (group_buf1)),
3923 		    inet_ntop(AF_INET6, &ipif->ipif_v6lcl_addr,
3924 		    group_buf2, sizeof (group_buf2)),
3925 		    ipif->ipif_ill->ill_name));
3926 
3927 		/* ilg_ipif is NULL for V6, so we won't be here */
3928 		ASSERT(IN6_IS_ADDR_V4MAPPED(&ilg->ilg_v6group));
3929 
3930 		group = V4_PART_OF_V6(ilg->ilg_v6group);
3931 		ilg_delete(connp, &connp->conn_ilg[i], NULL);
3932 		mutex_exit(&connp->conn_lock);
3933 
3934 		(void) ip_delmulti(group, ipif, B_FALSE, B_TRUE);
3935 		mutex_enter(&connp->conn_lock);
3936 	}
3937 
3938 	/*
3939 	 * If we are the last walker, need to physically delete the
3940 	 * ilgs and repack.
3941 	 */
3942 	ILG_WALKER_RELE(connp);
3943 
3944 	if (connp->conn_multicast_ipif == ipif) {
3945 		/* Revert to late binding */
3946 		connp->conn_multicast_ipif = NULL;
3947 	}
3948 	mutex_exit(&connp->conn_lock);
3949 
3950 	conn_delete_ire(connp, (caddr_t)ipif);
3951 }
3952 
3953 /*
3954  * Called from ill close by ipcl_walk for clearing conn_ilg and
3955  * conn_multicast_ill for a given ill. conn is held by caller.
3956  * Note that ipcl_walk only walks conns that are not yet condemned.
3957  * condemned conns can't be refheld. For this reason, conn must become clean
3958  * first, i.e. it must not refer to any ill/ire/ipif and then only set
3959  * condemned flag.
3960  */
3961 static void
3962 conn_delete_ill(conn_t *connp, caddr_t arg)
3963 {
3964 	ill_t	*ill = (ill_t *)arg;
3965 	int	i;
3966 	char	group_buf[INET6_ADDRSTRLEN];
3967 	in6_addr_t v6group;
3968 	int	orig_ifindex;
3969 	ilg_t	*ilg;
3970 
3971 	/*
3972 	 * Even though conn_ilg_inuse can change while we are in this loop,
3973 	 * no new ilgs can be created/deleted for this connp, on this
3974 	 * ill, since this ill is the perimeter. So we won't miss any ilg
3975 	 * in this cleanup.
3976 	 */
3977 	mutex_enter(&connp->conn_lock);
3978 
3979 	/*
3980 	 * Increment the walker count, so that ilg repacking does not
3981 	 * occur while we are in the loop.
3982 	 */
3983 	ILG_WALKER_HOLD(connp);
3984 	for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) {
3985 		ilg = &connp->conn_ilg[i];
3986 		if ((ilg->ilg_ill == ill) && !(ilg->ilg_flags & ILG_DELETED)) {
3987 			/*
3988 			 * ip_close cannot be cleaning this ilg at the same
3989 			 * time, since it also has to execute in this ill's
3990 			 * perimeter which we are now holding. Only a clean
3991 			 * conn can be condemned.
3992 			 */
3993 			ASSERT(!(connp->conn_state_flags & CONN_CONDEMNED));
3994 
3995 			/* Blow away the membership */
3996 			ip1dbg(("conn_delete_ilg_ill: %s on %s\n",
3997 			    inet_ntop(AF_INET6, &ilg->ilg_v6group,
3998 			    group_buf, sizeof (group_buf)),
3999 			    ill->ill_name));
4000 
4001 			v6group = ilg->ilg_v6group;
4002 			orig_ifindex = ilg->ilg_orig_ifindex;
4003 			ilg_delete(connp, ilg, NULL);
4004 			mutex_exit(&connp->conn_lock);
4005 
4006 			(void) ip_delmulti_v6(&v6group, ill, orig_ifindex,
4007 			    connp->conn_zoneid, B_FALSE, B_TRUE);
4008 			mutex_enter(&connp->conn_lock);
4009 		}
4010 	}
4011 	/*
4012 	 * If we are the last walker, need to physically delete the
4013 	 * ilgs and repack.
4014 	 */
4015 	ILG_WALKER_RELE(connp);
4016 
4017 	if (connp->conn_multicast_ill == ill) {
4018 		/* Revert to late binding */
4019 		connp->conn_multicast_ill = NULL;
4020 		connp->conn_orig_multicast_ifindex = 0;
4021 	}
4022 	mutex_exit(&connp->conn_lock);
4023 }
4024 
4025 /*
4026  * Called when an ipif is unplumbed to make sure that there are no
4027  * dangling conn references to that ipif.
4028  * Handles ilg_ipif and conn_multicast_ipif
4029  */
4030 void
4031 reset_conn_ipif(ipif)
4032 	ipif_t	*ipif;
4033 {
4034 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
4035 
4036 	ipcl_walk(conn_delete_ipif, (caddr_t)ipif, ipst);
4037 }
4038 
4039 /*
4040  * Called when an ill is unplumbed to make sure that there are no
4041  * dangling conn references to that ill.
4042  * Handles ilg_ill, conn_multicast_ill.
4043  */
4044 void
4045 reset_conn_ill(ill_t *ill)
4046 {
4047 	ip_stack_t	*ipst = ill->ill_ipst;
4048 
4049 	ipcl_walk(conn_delete_ill, (caddr_t)ill, ipst);
4050 }
4051 
4052 #ifdef DEBUG
4053 /*
4054  * Walk functions walk all the interfaces in the system to make
4055  * sure that there is no refernece to the ipif or ill that is
4056  * going away.
4057  */
4058 int
4059 ilm_walk_ill(ill_t *ill)
4060 {
4061 	int cnt = 0;
4062 	ill_t *till;
4063 	ilm_t *ilm;
4064 	ill_walk_context_t ctx;
4065 	ip_stack_t	*ipst = ill->ill_ipst;
4066 
4067 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4068 	till = ILL_START_WALK_ALL(&ctx, ipst);
4069 	for (; till != NULL; till = ill_next(&ctx, till)) {
4070 		mutex_enter(&till->ill_lock);
4071 		for (ilm = till->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
4072 			if (ilm->ilm_ill == ill) {
4073 				cnt++;
4074 			}
4075 		}
4076 		mutex_exit(&till->ill_lock);
4077 	}
4078 	rw_exit(&ipst->ips_ill_g_lock);
4079 
4080 	return (cnt);
4081 }
4082 
4083 /*
4084  * This function is called before the ipif is freed.
4085  */
4086 int
4087 ilm_walk_ipif(ipif_t *ipif)
4088 {
4089 	int cnt = 0;
4090 	ill_t *till;
4091 	ilm_t *ilm;
4092 	ill_walk_context_t ctx;
4093 	ip_stack_t	*ipst = ipif->ipif_ill->ill_ipst;
4094 
4095 	till = ILL_START_WALK_ALL(&ctx, ipst);
4096 	for (; till != NULL; till = ill_next(&ctx, till)) {
4097 		mutex_enter(&till->ill_lock);
4098 		for (ilm = till->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
4099 			if (ilm->ilm_ipif == ipif) {
4100 					cnt++;
4101 			}
4102 		}
4103 		mutex_exit(&till->ill_lock);
4104 	}
4105 	return (cnt);
4106 }
4107 #endif
4108