xref: /illumos-gate/usr/src/uts/common/inet/ip/igmp.c (revision f4b3ec61)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*f4b3ec61Sdh  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate /* Copyright (c) 1990 Mentat Inc. */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Internet Group Management Protocol (IGMP) routines.
317c478bd9Sstevel@tonic-gate  * Multicast Listener Discovery Protocol (MLD) routines.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * Written by Steve Deering, Stanford, May 1988.
347c478bd9Sstevel@tonic-gate  * Modified by Rosen Sharma, Stanford, Aug 1994.
357c478bd9Sstevel@tonic-gate  * Modified by Bill Fenner, Xerox PARC, Feb. 1995.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * MULTICAST 3.5.1.1
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/stream.h>
427c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
437c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
447c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
457c478bd9Sstevel@tonic-gate #include <sys/systm.h>
467c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
477c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
487c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
497c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
507c478bd9Sstevel@tonic-gate #include <sys/zone.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include <sys/param.h>
537c478bd9Sstevel@tonic-gate #include <sys/socket.h>
547c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
557c478bd9Sstevel@tonic-gate #include <net/if.h>
567c478bd9Sstevel@tonic-gate #include <net/route.h>
577c478bd9Sstevel@tonic-gate #include <netinet/in.h>
587c478bd9Sstevel@tonic-gate #include <netinet/igmp_var.h>
597c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
607c478bd9Sstevel@tonic-gate #include <netinet/icmp6.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <inet/common.h>
637c478bd9Sstevel@tonic-gate #include <inet/mi.h>
647c478bd9Sstevel@tonic-gate #include <inet/nd.h>
657c478bd9Sstevel@tonic-gate #include <inet/ip.h>
667c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
677c478bd9Sstevel@tonic-gate #include <inet/ip_multi.h>
687c478bd9Sstevel@tonic-gate #include <inet/ip_listutils.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <netinet/igmp.h>
717c478bd9Sstevel@tonic-gate #include <inet/ip_if.h>
727c478bd9Sstevel@tonic-gate #include <net/pfkeyv2.h>
737c478bd9Sstevel@tonic-gate #include <inet/ipsec_info.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static uint_t	igmp_query_in(ipha_t *ipha, igmpa_t *igmpa, ill_t *ill);
767c478bd9Sstevel@tonic-gate static uint_t	igmpv3_query_in(igmp3qa_t *igmp3qa, ill_t *ill, int igmplen);
777c478bd9Sstevel@tonic-gate static uint_t	mld_query_in(mld_hdr_t *mldh, ill_t *ill);
787c478bd9Sstevel@tonic-gate static uint_t	mldv2_query_in(mld2q_t *mld2q, ill_t *ill, int mldlen);
797c478bd9Sstevel@tonic-gate static void	igmp_sendpkt(ilm_t *ilm, uchar_t type, ipaddr_t addr);
807c478bd9Sstevel@tonic-gate static void	mld_sendpkt(ilm_t *ilm, uchar_t type, const in6_addr_t *v6addr);
817c478bd9Sstevel@tonic-gate static void	igmpv3_sendrpt(ipif_t *ipif, mrec_t *reclist);
827c478bd9Sstevel@tonic-gate static void	mldv2_sendrpt(ill_t *ill, mrec_t *reclist);
837c478bd9Sstevel@tonic-gate static mrec_t	*mcast_bldmrec(mcast_record_t type, in6_addr_t *grp,
847c478bd9Sstevel@tonic-gate 		    slist_t *srclist, mrec_t *next);
857c478bd9Sstevel@tonic-gate static void	mcast_init_rtx(ill_t *ill, rtx_state_t *rtxp,
867c478bd9Sstevel@tonic-gate 		    mcast_record_t rtype, slist_t *flist);
877c478bd9Sstevel@tonic-gate static mrec_t	*mcast_merge_rtx(ilm_t *ilm, mrec_t *rp, slist_t *flist);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Macros used to do timer len conversions.  Timer values are always
927c478bd9Sstevel@tonic-gate  * stored and passed to the timer functions as milliseconds; but the
937c478bd9Sstevel@tonic-gate  * default values and values from the wire may not be.
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  * And yes, it's obscure, but decisecond is easier to abbreviate than
967c478bd9Sstevel@tonic-gate  * "tenths of a second".
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate #define	DSEC_TO_MSEC(dsec)	((dsec) * 100)
997c478bd9Sstevel@tonic-gate #define	SEC_TO_MSEC(sec)	((sec) * 1000)
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * The first multicast join will trigger the igmp timers / mld timers
1037c478bd9Sstevel@tonic-gate  * The unit for next is milliseconds.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate void
106*f4b3ec61Sdh igmp_start_timers(unsigned next, ip_stack_t *ipst)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	int	time_left;
1097c478bd9Sstevel@tonic-gate 	int	ret;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	ASSERT(next != 0 && next != INFINITY);
1127c478bd9Sstevel@tonic-gate 
113*f4b3ec61Sdh 	mutex_enter(&ipst->ips_igmp_timer_lock);
1147c478bd9Sstevel@tonic-gate 
115*f4b3ec61Sdh 	if (ipst->ips_igmp_timer_setter_active) {
1167c478bd9Sstevel@tonic-gate 		/*
1177c478bd9Sstevel@tonic-gate 		 * Serialize timer setters, one at a time. If the
1187c478bd9Sstevel@tonic-gate 		 * timer is currently being set by someone,
1197c478bd9Sstevel@tonic-gate 		 * just record the next time when it has to be
1207c478bd9Sstevel@tonic-gate 		 * invoked and return. The current setter will
1217c478bd9Sstevel@tonic-gate 		 * take care.
1227c478bd9Sstevel@tonic-gate 		 */
123*f4b3ec61Sdh 		ipst->ips_igmp_time_to_next =
124*f4b3ec61Sdh 		    MIN(ipst->ips_igmp_time_to_next, next);
125*f4b3ec61Sdh 		mutex_exit(&ipst->ips_igmp_timer_lock);
1267c478bd9Sstevel@tonic-gate 		return;
1277c478bd9Sstevel@tonic-gate 	} else {
128*f4b3ec61Sdh 		ipst->ips_igmp_timer_setter_active = B_TRUE;
1297c478bd9Sstevel@tonic-gate 	}
130*f4b3ec61Sdh 	if (ipst->ips_igmp_timeout_id == 0) {
1317c478bd9Sstevel@tonic-gate 		/*
1327c478bd9Sstevel@tonic-gate 		 * The timer is inactive. We need to start a timer
1337c478bd9Sstevel@tonic-gate 		 */
134*f4b3ec61Sdh 		ipst->ips_igmp_time_to_next = next;
135*f4b3ec61Sdh 		ipst->ips_igmp_timeout_id = timeout(igmp_timeout_handler,
136*f4b3ec61Sdh 		    (void *)ipst, MSEC_TO_TICK(ipst->ips_igmp_time_to_next));
137*f4b3ec61Sdh 		ipst->ips_igmp_timer_setter_active = B_FALSE;
138*f4b3ec61Sdh 		mutex_exit(&ipst->ips_igmp_timer_lock);
1397c478bd9Sstevel@tonic-gate 		return;
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	/*
1437c478bd9Sstevel@tonic-gate 	 * The timer was scheduled sometime back for firing in
1447c478bd9Sstevel@tonic-gate 	 * 'igmp_time_to_next' ms and is active. We need to
1457c478bd9Sstevel@tonic-gate 	 * reschedule the timeout if the new 'next' will happen
1467c478bd9Sstevel@tonic-gate 	 * earlier than the currently scheduled timeout
1477c478bd9Sstevel@tonic-gate 	 */
148*f4b3ec61Sdh 	time_left = ipst->ips_igmp_timer_fired_last +
149*f4b3ec61Sdh 	    MSEC_TO_TICK(ipst->ips_igmp_time_to_next) - ddi_get_lbolt();
1507c478bd9Sstevel@tonic-gate 	if (time_left < MSEC_TO_TICK(next)) {
151*f4b3ec61Sdh 		ipst->ips_igmp_timer_setter_active = B_FALSE;
152*f4b3ec61Sdh 		mutex_exit(&ipst->ips_igmp_timer_lock);
1537c478bd9Sstevel@tonic-gate 		return;
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
156*f4b3ec61Sdh 	mutex_exit(&ipst->ips_igmp_timer_lock);
157*f4b3ec61Sdh 	ret = untimeout(ipst->ips_igmp_timeout_id);
158*f4b3ec61Sdh 	mutex_enter(&ipst->ips_igmp_timer_lock);
1597c478bd9Sstevel@tonic-gate 	/*
1607c478bd9Sstevel@tonic-gate 	 * The timeout was cancelled, or the timeout handler
1617c478bd9Sstevel@tonic-gate 	 * completed, while we were blocked in the untimeout.
1627c478bd9Sstevel@tonic-gate 	 * No other thread could have set the timer meanwhile
1637c478bd9Sstevel@tonic-gate 	 * since we serialized all the timer setters. Thus
1647c478bd9Sstevel@tonic-gate 	 * no timer is currently active nor executing nor will
1657c478bd9Sstevel@tonic-gate 	 * any timer fire in the future. We start the timer now
1667c478bd9Sstevel@tonic-gate 	 * if needed.
1677c478bd9Sstevel@tonic-gate 	 */
1687c478bd9Sstevel@tonic-gate 	if (ret == -1) {
169*f4b3ec61Sdh 		ASSERT(ipst->ips_igmp_timeout_id == 0);
1707c478bd9Sstevel@tonic-gate 	} else {
171*f4b3ec61Sdh 		ASSERT(ipst->ips_igmp_timeout_id != 0);
172*f4b3ec61Sdh 		ipst->ips_igmp_timeout_id = 0;
1737c478bd9Sstevel@tonic-gate 	}
174*f4b3ec61Sdh 	if (ipst->ips_igmp_time_to_next != 0) {
175*f4b3ec61Sdh 		ipst->ips_igmp_time_to_next =
176*f4b3ec61Sdh 		    MIN(ipst->ips_igmp_time_to_next, next);
177*f4b3ec61Sdh 		ipst->ips_igmp_timeout_id = timeout(igmp_timeout_handler,
178*f4b3ec61Sdh 		    (void *)ipst, MSEC_TO_TICK(ipst->ips_igmp_time_to_next));
1797c478bd9Sstevel@tonic-gate 	}
180*f4b3ec61Sdh 	ipst->ips_igmp_timer_setter_active = B_FALSE;
181*f4b3ec61Sdh 	mutex_exit(&ipst->ips_igmp_timer_lock);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * mld_start_timers:
1867c478bd9Sstevel@tonic-gate  * The unit for next is milliseconds.
1877c478bd9Sstevel@tonic-gate  */
1887c478bd9Sstevel@tonic-gate void
189*f4b3ec61Sdh mld_start_timers(unsigned next, ip_stack_t *ipst)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	int	time_left;
1927c478bd9Sstevel@tonic-gate 	int	ret;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	ASSERT(next != 0 && next != INFINITY);
1957c478bd9Sstevel@tonic-gate 
196*f4b3ec61Sdh 	mutex_enter(&ipst->ips_mld_timer_lock);
197*f4b3ec61Sdh 	if (ipst->ips_mld_timer_setter_active) {
1987c478bd9Sstevel@tonic-gate 		/*
1997c478bd9Sstevel@tonic-gate 		 * Serialize timer setters, one at a time. If the
2007c478bd9Sstevel@tonic-gate 		 * timer is currently being set by someone,
2017c478bd9Sstevel@tonic-gate 		 * just record the next time when it has to be
2027c478bd9Sstevel@tonic-gate 		 * invoked and return. The current setter will
2037c478bd9Sstevel@tonic-gate 		 * take care.
2047c478bd9Sstevel@tonic-gate 		 */
205*f4b3ec61Sdh 		ipst->ips_mld_time_to_next =
206*f4b3ec61Sdh 		    MIN(ipst->ips_mld_time_to_next, next);
207*f4b3ec61Sdh 		mutex_exit(&ipst->ips_mld_timer_lock);
2087c478bd9Sstevel@tonic-gate 		return;
2097c478bd9Sstevel@tonic-gate 	} else {
210*f4b3ec61Sdh 		ipst->ips_mld_timer_setter_active = B_TRUE;
2117c478bd9Sstevel@tonic-gate 	}
212*f4b3ec61Sdh 	if (ipst->ips_mld_timeout_id == 0) {
2137c478bd9Sstevel@tonic-gate 		/*
2147c478bd9Sstevel@tonic-gate 		 * The timer is inactive. We need to start a timer
2157c478bd9Sstevel@tonic-gate 		 */
216*f4b3ec61Sdh 		ipst->ips_mld_time_to_next = next;
217*f4b3ec61Sdh 		ipst->ips_mld_timeout_id = timeout(mld_timeout_handler,
218*f4b3ec61Sdh 		    (void *)ipst, MSEC_TO_TICK(ipst->ips_mld_time_to_next));
219*f4b3ec61Sdh 		ipst->ips_mld_timer_setter_active = B_FALSE;
220*f4b3ec61Sdh 		mutex_exit(&ipst->ips_mld_timer_lock);
2217c478bd9Sstevel@tonic-gate 		return;
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * The timer was scheduled sometime back for firing in
2267c478bd9Sstevel@tonic-gate 	 * 'igmp_time_to_next' ms and is active. We need to
2277c478bd9Sstevel@tonic-gate 	 * reschedule the timeout if the new 'next' will happen
2287c478bd9Sstevel@tonic-gate 	 * earlier than the currently scheduled timeout
2297c478bd9Sstevel@tonic-gate 	 */
230*f4b3ec61Sdh 	time_left = ipst->ips_mld_timer_fired_last +
231*f4b3ec61Sdh 	    MSEC_TO_TICK(ipst->ips_mld_time_to_next) - ddi_get_lbolt();
2327c478bd9Sstevel@tonic-gate 	if (time_left < MSEC_TO_TICK(next)) {
233*f4b3ec61Sdh 		ipst->ips_mld_timer_setter_active = B_FALSE;
234*f4b3ec61Sdh 		mutex_exit(&ipst->ips_mld_timer_lock);
2357c478bd9Sstevel@tonic-gate 		return;
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
238*f4b3ec61Sdh 	mutex_exit(&ipst->ips_mld_timer_lock);
239*f4b3ec61Sdh 	ret = untimeout(ipst->ips_mld_timeout_id);
240*f4b3ec61Sdh 	mutex_enter(&ipst->ips_mld_timer_lock);
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 * The timeout was cancelled, or the timeout handler
2437c478bd9Sstevel@tonic-gate 	 * completed, while we were blocked in the untimeout.
2447c478bd9Sstevel@tonic-gate 	 * No other thread could have set the timer meanwhile
2457c478bd9Sstevel@tonic-gate 	 * since we serialized all the timer setters. Thus
2467c478bd9Sstevel@tonic-gate 	 * no timer is currently active nor executing nor will
2477c478bd9Sstevel@tonic-gate 	 * any timer fire in the future. We start the timer now
2487c478bd9Sstevel@tonic-gate 	 * if needed.
2497c478bd9Sstevel@tonic-gate 	 */
2507c478bd9Sstevel@tonic-gate 	if (ret == -1) {
251*f4b3ec61Sdh 		ASSERT(ipst->ips_mld_timeout_id == 0);
2527c478bd9Sstevel@tonic-gate 	} else {
253*f4b3ec61Sdh 		ASSERT(ipst->ips_mld_timeout_id != 0);
254*f4b3ec61Sdh 		ipst->ips_mld_timeout_id = 0;
2557c478bd9Sstevel@tonic-gate 	}
256*f4b3ec61Sdh 	if (ipst->ips_mld_time_to_next != 0) {
257*f4b3ec61Sdh 		ipst->ips_mld_time_to_next =
258*f4b3ec61Sdh 		    MIN(ipst->ips_mld_time_to_next, next);
259*f4b3ec61Sdh 		ipst->ips_mld_timeout_id = timeout(mld_timeout_handler,
260*f4b3ec61Sdh 		    (void *)ipst, MSEC_TO_TICK(ipst->ips_mld_time_to_next));
2617c478bd9Sstevel@tonic-gate 	}
262*f4b3ec61Sdh 	ipst->ips_mld_timer_setter_active = B_FALSE;
263*f4b3ec61Sdh 	mutex_exit(&ipst->ips_mld_timer_lock);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * igmp_input:
2686226e9d9Sethindra  * Return NULL for a bad packet that is discarded here.
2696226e9d9Sethindra  * Return mp if the message is OK and should be handed to "raw" receivers.
2707c478bd9Sstevel@tonic-gate  * Callers of igmp_input() may need to reinitialize variables that were copied
2717c478bd9Sstevel@tonic-gate  * from the mblk as this calls pullupmsg().
2727c478bd9Sstevel@tonic-gate  */
2737c478bd9Sstevel@tonic-gate /* ARGSUSED */
2746226e9d9Sethindra mblk_t *
2757c478bd9Sstevel@tonic-gate igmp_input(queue_t *q, mblk_t *mp, ill_t *ill)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	igmpa_t 	*igmpa;
2787c478bd9Sstevel@tonic-gate 	ipha_t		*ipha = (ipha_t *)(mp->b_rptr);
2797c478bd9Sstevel@tonic-gate 	int		iphlen, igmplen, mblklen;
2807c478bd9Sstevel@tonic-gate 	ilm_t 		*ilm;
2817c478bd9Sstevel@tonic-gate 	uint32_t	src, dst;
2827c478bd9Sstevel@tonic-gate 	uint32_t 	group;
2837c478bd9Sstevel@tonic-gate 	uint_t		next;
2847c478bd9Sstevel@tonic-gate 	ipif_t 		*ipif;
285*f4b3ec61Sdh 	ip_stack_t	 *ipst;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	ASSERT(ill != NULL);
2887c478bd9Sstevel@tonic-gate 	ASSERT(!ill->ill_isv6);
289*f4b3ec61Sdh 	ipst = ill->ill_ipst;
290*f4b3ec61Sdh 	++ipst->ips_igmpstat.igps_rcv_total;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	mblklen = MBLKL(mp);
2937c478bd9Sstevel@tonic-gate 	if (mblklen < 1 || mblklen < (iphlen = IPH_HDR_LENGTH(ipha))) {
294*f4b3ec61Sdh 		++ipst->ips_igmpstat.igps_rcv_tooshort;
2956226e9d9Sethindra 		goto bad_pkt;
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 	igmplen = ntohs(ipha->ipha_length) - iphlen;
2987c478bd9Sstevel@tonic-gate 	/*
2997c478bd9Sstevel@tonic-gate 	 * Since msg sizes are more variable with v3, just pullup the
3007c478bd9Sstevel@tonic-gate 	 * whole thing now.
3017c478bd9Sstevel@tonic-gate 	 */
3027c478bd9Sstevel@tonic-gate 	if (MBLKL(mp) < (igmplen + iphlen)) {
3037c478bd9Sstevel@tonic-gate 		mblk_t *mp1;
3047c478bd9Sstevel@tonic-gate 		if ((mp1 = msgpullup(mp, -1)) == NULL) {
305*f4b3ec61Sdh 			++ipst->ips_igmpstat.igps_rcv_tooshort;
3066226e9d9Sethindra 			goto bad_pkt;
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 		freemsg(mp);
3097c478bd9Sstevel@tonic-gate 		mp = mp1;
3107c478bd9Sstevel@tonic-gate 		ipha = (ipha_t *)(mp->b_rptr);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/*
3147c478bd9Sstevel@tonic-gate 	 * Validate lengths
3157c478bd9Sstevel@tonic-gate 	 */
3167c478bd9Sstevel@tonic-gate 	if (igmplen < IGMP_MINLEN) {
317*f4b3ec61Sdh 		++ipst->ips_igmpstat.igps_rcv_tooshort;
3186226e9d9Sethindra 		goto bad_pkt;
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 	/*
3217c478bd9Sstevel@tonic-gate 	 * Validate checksum
3227c478bd9Sstevel@tonic-gate 	 */
3237c478bd9Sstevel@tonic-gate 	if (IP_CSUM(mp, iphlen, 0)) {
324*f4b3ec61Sdh 		++ipst->ips_igmpstat.igps_rcv_badsum;
3256226e9d9Sethindra 		goto bad_pkt;
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	igmpa = (igmpa_t *)(&mp->b_rptr[iphlen]);
3297c478bd9Sstevel@tonic-gate 	src = ipha->ipha_src;
3307c478bd9Sstevel@tonic-gate 	dst = ipha->ipha_dst;
3317c478bd9Sstevel@tonic-gate 	if (ip_debug > 1)
3327c478bd9Sstevel@tonic-gate 		(void) mi_strlog(ill->ill_rq, 1, SL_TRACE,
3337c478bd9Sstevel@tonic-gate 		    "igmp_input: src 0x%x, dst 0x%x on %s\n",
3347c478bd9Sstevel@tonic-gate 		    (int)ntohl(src), (int)ntohl(dst),
3357c478bd9Sstevel@tonic-gate 		    ill->ill_name);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	switch (igmpa->igmpa_type) {
3387c478bd9Sstevel@tonic-gate 	case IGMP_MEMBERSHIP_QUERY:
3397c478bd9Sstevel@tonic-gate 		/*
3407c478bd9Sstevel@tonic-gate 		 * packet length differentiates between v1/v2 and v3
3417c478bd9Sstevel@tonic-gate 		 * v1/v2 should be exactly 8 octets long; v3 is >= 12
3427c478bd9Sstevel@tonic-gate 		 */
3437c478bd9Sstevel@tonic-gate 		if (igmplen == IGMP_MINLEN) {
3447c478bd9Sstevel@tonic-gate 			next = igmp_query_in(ipha, igmpa, ill);
3457c478bd9Sstevel@tonic-gate 		} else if (igmplen >= IGMP_V3_QUERY_MINLEN) {
3467c478bd9Sstevel@tonic-gate 			next = igmpv3_query_in((igmp3qa_t *)igmpa, ill,
3477c478bd9Sstevel@tonic-gate 			    igmplen);
3487c478bd9Sstevel@tonic-gate 		} else {
349*f4b3ec61Sdh 			++ipst->ips_igmpstat.igps_rcv_tooshort;
3506226e9d9Sethindra 			goto bad_pkt;
3517c478bd9Sstevel@tonic-gate 		}
3526226e9d9Sethindra 		if (next == 0)
3536226e9d9Sethindra 			goto bad_pkt;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 		if (next != INFINITY)
356*f4b3ec61Sdh 			igmp_start_timers(next, ipst);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 		break;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	case IGMP_V1_MEMBERSHIP_REPORT:
3617c478bd9Sstevel@tonic-gate 	case IGMP_V2_MEMBERSHIP_REPORT:
3627c478bd9Sstevel@tonic-gate 		/*
3637c478bd9Sstevel@tonic-gate 		 * For fast leave to work, we have to know that we are the
3647c478bd9Sstevel@tonic-gate 		 * last person to send a report for this group. Reports
3657c478bd9Sstevel@tonic-gate 		 * generated by us are looped back since we could potentially
3667c478bd9Sstevel@tonic-gate 		 * be a multicast router, so discard reports sourced by me.
3677c478bd9Sstevel@tonic-gate 		 */
3687c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
3697c478bd9Sstevel@tonic-gate 		for (ipif = ill->ill_ipif; ipif != NULL;
3707c478bd9Sstevel@tonic-gate 		    ipif = ipif->ipif_next) {
3717c478bd9Sstevel@tonic-gate 			if (ipif->ipif_lcl_addr == src) {
3727c478bd9Sstevel@tonic-gate 				if (ip_debug > 1) {
3737c478bd9Sstevel@tonic-gate 					(void) mi_strlog(ill->ill_rq,
3747c478bd9Sstevel@tonic-gate 					    1,
3757c478bd9Sstevel@tonic-gate 					    SL_TRACE,
3767c478bd9Sstevel@tonic-gate 					    "igmp_input: we are only "
3777c478bd9Sstevel@tonic-gate 					    "member src 0x%x ipif_local 0x%x",
3787c478bd9Sstevel@tonic-gate 					    (int)ntohl(src),
3797c478bd9Sstevel@tonic-gate 					    (int)
3807c478bd9Sstevel@tonic-gate 					    ntohl(ipif->ipif_lcl_addr));
3817c478bd9Sstevel@tonic-gate 				}
3827c478bd9Sstevel@tonic-gate 				mutex_exit(&ill->ill_lock);
3836226e9d9Sethindra 				return (mp);
3847c478bd9Sstevel@tonic-gate 			}
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
3877c478bd9Sstevel@tonic-gate 
388*f4b3ec61Sdh 		++ipst->ips_igmpstat.igps_rcv_reports;
3897c478bd9Sstevel@tonic-gate 		group = igmpa->igmpa_group;
3907c478bd9Sstevel@tonic-gate 		if (!CLASSD(group)) {
391*f4b3ec61Sdh 			++ipst->ips_igmpstat.igps_rcv_badreports;
3926226e9d9Sethindra 			goto bad_pkt;
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		/*
3967c478bd9Sstevel@tonic-gate 		 * KLUDGE: if the IP source address of the report has an
3977c478bd9Sstevel@tonic-gate 		 * unspecified (i.e., zero) subnet number, as is allowed for
3987c478bd9Sstevel@tonic-gate 		 * a booting host, replace it with the correct subnet number
3997c478bd9Sstevel@tonic-gate 		 * so that a process-level multicast routing demon can
4007c478bd9Sstevel@tonic-gate 		 * determine which subnet it arrived from.  This is necessary
4017c478bd9Sstevel@tonic-gate 		 * to compensate for the lack of any way for a process to
4027c478bd9Sstevel@tonic-gate 		 * determine the arrival interface of an incoming packet.
4037c478bd9Sstevel@tonic-gate 		 *
4047c478bd9Sstevel@tonic-gate 		 * Requires that a copy of *this* message it passed up
4057c478bd9Sstevel@tonic-gate 		 * to the raw interface which is done by our caller.
4067c478bd9Sstevel@tonic-gate 		 */
4077c478bd9Sstevel@tonic-gate 		if ((src & htonl(0xFF000000U)) == 0) {	/* Minimum net mask */
4087c478bd9Sstevel@tonic-gate 			/* Pick the first ipif on this ill */
4097c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
4107c478bd9Sstevel@tonic-gate 			src = ill->ill_ipif->ipif_subnet;
4117c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
4127c478bd9Sstevel@tonic-gate 			ip1dbg(("igmp_input: changed src to 0x%x\n",
4137c478bd9Sstevel@tonic-gate 			    (int)ntohl(src)));
4147c478bd9Sstevel@tonic-gate 			ipha->ipha_src = src;
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		/*
4187c478bd9Sstevel@tonic-gate 		 * If we belong to the group being reported, and
4197c478bd9Sstevel@tonic-gate 		 * we are a 'Delaying member' in the RFC terminology,
4207c478bd9Sstevel@tonic-gate 		 * stop our timer for that group and 'clear flag' i.e.
4217c478bd9Sstevel@tonic-gate 		 * mark as IGMP_OTHERMEMBER. Do this for all logical
4227c478bd9Sstevel@tonic-gate 		 * interfaces on the given physical interface.
4237c478bd9Sstevel@tonic-gate 		 */
4247c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
4257c478bd9Sstevel@tonic-gate 		for (ipif = ill->ill_ipif; ipif != NULL;
4267c478bd9Sstevel@tonic-gate 		    ipif = ipif->ipif_next) {
4277c478bd9Sstevel@tonic-gate 			ilm = ilm_lookup_ipif(ipif, group);
4287c478bd9Sstevel@tonic-gate 			if (ilm != NULL) {
429*f4b3ec61Sdh 				++ipst->ips_igmpstat.igps_rcv_ourreports;
4307c478bd9Sstevel@tonic-gate 				ilm->ilm_timer = INFINITY;
4317c478bd9Sstevel@tonic-gate 				ilm->ilm_state = IGMP_OTHERMEMBER;
4327c478bd9Sstevel@tonic-gate 			}
4337c478bd9Sstevel@tonic-gate 		} /* for */
4347c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
4357c478bd9Sstevel@tonic-gate 		break;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	case IGMP_V3_MEMBERSHIP_REPORT:
4387c478bd9Sstevel@tonic-gate 		/*
4397c478bd9Sstevel@tonic-gate 		 * Currently nothing to do here; IGMP router is not
4407c478bd9Sstevel@tonic-gate 		 * implemented in ip, and v3 hosts don't pay attention
4417c478bd9Sstevel@tonic-gate 		 * to membership reports.
4427c478bd9Sstevel@tonic-gate 		 */
4437c478bd9Sstevel@tonic-gate 		break;
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 	/*
4467c478bd9Sstevel@tonic-gate 	 * Pass all valid IGMP packets up to any process(es) listening
4477c478bd9Sstevel@tonic-gate 	 * on a raw IGMP socket. Do not free the packet.
4487c478bd9Sstevel@tonic-gate 	 */
4496226e9d9Sethindra 	return (mp);
4506226e9d9Sethindra 
4516226e9d9Sethindra bad_pkt:
4526226e9d9Sethindra 	freemsg(mp);
4536226e9d9Sethindra 	return (NULL);
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate static uint_t
4577c478bd9Sstevel@tonic-gate igmp_query_in(ipha_t *ipha, igmpa_t *igmpa, ill_t *ill)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	ilm_t	*ilm;
4607c478bd9Sstevel@tonic-gate 	int	timer;
4617c478bd9Sstevel@tonic-gate 	uint_t	next;
462*f4b3ec61Sdh 	ip_stack_t	 *ipst;
4637c478bd9Sstevel@tonic-gate 
464*f4b3ec61Sdh 	ipst = ill->ill_ipst;
465*f4b3ec61Sdh 	++ipst->ips_igmpstat.igps_rcv_queries;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	/*
4687c478bd9Sstevel@tonic-gate 	 * In the IGMPv2 specification, there are 3 states and a flag.
4697c478bd9Sstevel@tonic-gate 	 *
4707c478bd9Sstevel@tonic-gate 	 * In Non-Member state, we simply don't have a membership record.
4717c478bd9Sstevel@tonic-gate 	 * In Delaying Member state, our timer is running (ilm->ilm_timer
4727c478bd9Sstevel@tonic-gate 	 * < INFINITY).  In Idle Member state, our timer is not running
4737c478bd9Sstevel@tonic-gate 	 * (ilm->ilm_timer == INFINITY).
4747c478bd9Sstevel@tonic-gate 	 *
4757c478bd9Sstevel@tonic-gate 	 * The flag is ilm->ilm_state, it is set to IGMP_OTHERMEMBER if
4767c478bd9Sstevel@tonic-gate 	 * we have heard a report from another member, or IGMP_IREPORTEDLAST
4777c478bd9Sstevel@tonic-gate 	 * if I sent the last report.
4787c478bd9Sstevel@tonic-gate 	 */
4797c478bd9Sstevel@tonic-gate 	if (igmpa->igmpa_code == 0) {
4807c478bd9Sstevel@tonic-gate 		/*
4817c478bd9Sstevel@tonic-gate 		 * Query from an old router.
4827c478bd9Sstevel@tonic-gate 		 * Remember that the querier on this interface is old,
4837c478bd9Sstevel@tonic-gate 		 * and set the timer to the value in RFC 1112.
4847c478bd9Sstevel@tonic-gate 		 */
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
4887c478bd9Sstevel@tonic-gate 		ill->ill_mcast_v1_time = 0;
4897c478bd9Sstevel@tonic-gate 		ill->ill_mcast_v1_tset = 1;
4907c478bd9Sstevel@tonic-gate 		if (ill->ill_mcast_type != IGMP_V1_ROUTER) {
4917c478bd9Sstevel@tonic-gate 			ip1dbg(("Received IGMPv1 Query on %s, switching mode "
4927c478bd9Sstevel@tonic-gate 			    "to IGMP_V1_ROUTER\n", ill->ill_name));
4937c478bd9Sstevel@tonic-gate 			atomic_add_16(&ill->ill_ifptr->illif_mcast_v1, 1);
4947c478bd9Sstevel@tonic-gate 			ill->ill_mcast_type = IGMP_V1_ROUTER;
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		timer = SEC_TO_MSEC(IGMP_MAX_HOST_REPORT_DELAY);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 		if (ipha->ipha_dst != htonl(INADDR_ALLHOSTS_GROUP) ||
5017c478bd9Sstevel@tonic-gate 		    igmpa->igmpa_group != 0) {
502*f4b3ec61Sdh 			++ipst->ips_igmpstat.igps_rcv_badqueries;
5037c478bd9Sstevel@tonic-gate 			return (0);
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	} else {
5077c478bd9Sstevel@tonic-gate 		in_addr_t group;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 		/*
5107c478bd9Sstevel@tonic-gate 		 * Query from a new router
5117c478bd9Sstevel@tonic-gate 		 * Simply do a validity check
5127c478bd9Sstevel@tonic-gate 		 */
5137c478bd9Sstevel@tonic-gate 		group = igmpa->igmpa_group;
5147c478bd9Sstevel@tonic-gate 		if (group != 0 && (!CLASSD(group))) {
515*f4b3ec61Sdh 			++ipst->ips_igmpstat.igps_rcv_badqueries;
5167c478bd9Sstevel@tonic-gate 			return (0);
5177c478bd9Sstevel@tonic-gate 		}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 		/*
5207c478bd9Sstevel@tonic-gate 		 * Switch interface state to v2 on receipt of a v2 query
5217c478bd9Sstevel@tonic-gate 		 * ONLY IF current state is v3.  Let things be if current
5227c478bd9Sstevel@tonic-gate 		 * state if v1 but do reset the v2-querier-present timer.
5237c478bd9Sstevel@tonic-gate 		 */
5247c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
5257c478bd9Sstevel@tonic-gate 		if (ill->ill_mcast_type == IGMP_V3_ROUTER) {
5267c478bd9Sstevel@tonic-gate 			ip1dbg(("Received IGMPv2 Query on %s, switching mode "
5277c478bd9Sstevel@tonic-gate 			    "to IGMP_V2_ROUTER", ill->ill_name));
5287c478bd9Sstevel@tonic-gate 			atomic_add_16(&ill->ill_ifptr->illif_mcast_v2, 1);
5297c478bd9Sstevel@tonic-gate 			ill->ill_mcast_type = IGMP_V2_ROUTER;
5307c478bd9Sstevel@tonic-gate 		}
5317c478bd9Sstevel@tonic-gate 		ill->ill_mcast_v2_time = 0;
5327c478bd9Sstevel@tonic-gate 		ill->ill_mcast_v2_tset = 1;
5337c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 		timer = DSEC_TO_MSEC((int)igmpa->igmpa_code);
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	if (ip_debug > 1) {
5397c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
5407c478bd9Sstevel@tonic-gate 		(void) mi_strlog(ill->ill_rq, 1, SL_TRACE,
5417c478bd9Sstevel@tonic-gate 		    "igmp_input: TIMER = igmp_code %d igmp_type 0x%x",
5427c478bd9Sstevel@tonic-gate 		    (int)ntohs(igmpa->igmpa_code),
5437c478bd9Sstevel@tonic-gate 		    (int)ntohs(igmpa->igmpa_type));
5447c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/*
5487c478bd9Sstevel@tonic-gate 	 * -Start the timers in all of our membership records
5497c478bd9Sstevel@tonic-gate 	 *  for the physical interface on which the query
5507c478bd9Sstevel@tonic-gate 	 *  arrived, excluding those that belong to the "all
5517c478bd9Sstevel@tonic-gate 	 *  hosts" group (224.0.0.1).
5527c478bd9Sstevel@tonic-gate 	 *
5537c478bd9Sstevel@tonic-gate 	 * -Restart any timer that is already running but has
5547c478bd9Sstevel@tonic-gate 	 *  a value longer than the requested timeout.
5557c478bd9Sstevel@tonic-gate 	 *
5567c478bd9Sstevel@tonic-gate 	 * -Use the value specified in the query message as
5577c478bd9Sstevel@tonic-gate 	 *  the maximum timeout.
5587c478bd9Sstevel@tonic-gate 	 */
5597c478bd9Sstevel@tonic-gate 	next = (unsigned)INFINITY;
5607c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
5617c478bd9Sstevel@tonic-gate 	for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 		/*
5647c478bd9Sstevel@tonic-gate 		 * A multicast router joins INADDR_ANY address
5657c478bd9Sstevel@tonic-gate 		 * to enable promiscuous reception of all
5667c478bd9Sstevel@tonic-gate 		 * mcasts from the interface. This INADDR_ANY
5677c478bd9Sstevel@tonic-gate 		 * is stored in the ilm_v6addr as V6 unspec addr
5687c478bd9Sstevel@tonic-gate 		 */
5697c478bd9Sstevel@tonic-gate 		if (!IN6_IS_ADDR_V4MAPPED(&ilm->ilm_v6addr))
5707c478bd9Sstevel@tonic-gate 			continue;
5717c478bd9Sstevel@tonic-gate 		if (ilm->ilm_addr == htonl(INADDR_ANY))
5727c478bd9Sstevel@tonic-gate 			continue;
5737c478bd9Sstevel@tonic-gate 		if (ilm->ilm_addr != htonl(INADDR_ALLHOSTS_GROUP) &&
5747c478bd9Sstevel@tonic-gate 		    (igmpa->igmpa_group == 0) ||
5757c478bd9Sstevel@tonic-gate 		    (igmpa->igmpa_group == ilm->ilm_addr)) {
5767c478bd9Sstevel@tonic-gate 			if (ilm->ilm_timer > timer) {
5777c478bd9Sstevel@tonic-gate 				MCAST_RANDOM_DELAY(ilm->ilm_timer, timer);
5787c478bd9Sstevel@tonic-gate 				if (ilm->ilm_timer < next)
5797c478bd9Sstevel@tonic-gate 					next = ilm->ilm_timer;
5807c478bd9Sstevel@tonic-gate 			}
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	return (next);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate static uint_t
5897c478bd9Sstevel@tonic-gate igmpv3_query_in(igmp3qa_t *igmp3qa, ill_t *ill, int igmplen)
5907c478bd9Sstevel@tonic-gate {
5917c478bd9Sstevel@tonic-gate 	uint_t		i, next, mrd, qqi, timer, delay, numsrc;
5927c478bd9Sstevel@tonic-gate 	ilm_t		*ilm;
5937c478bd9Sstevel@tonic-gate 	ipaddr_t	*src_array;
5947c478bd9Sstevel@tonic-gate 	uint8_t		qrv;
595*f4b3ec61Sdh 	ip_stack_t	 *ipst;
5967c478bd9Sstevel@tonic-gate 
597*f4b3ec61Sdh 	ipst = ill->ill_ipst;
5987c478bd9Sstevel@tonic-gate 	/* make sure numsrc matches packet size */
5997c478bd9Sstevel@tonic-gate 	numsrc = ntohs(igmp3qa->igmp3qa_numsrc);
6007c478bd9Sstevel@tonic-gate 	if (igmplen < IGMP_V3_QUERY_MINLEN + (numsrc * sizeof (ipaddr_t))) {
601*f4b3ec61Sdh 		++ipst->ips_igmpstat.igps_rcv_tooshort;
6027c478bd9Sstevel@tonic-gate 		return (0);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 	src_array = (ipaddr_t *)&igmp3qa[1];
6057c478bd9Sstevel@tonic-gate 
606*f4b3ec61Sdh 	++ipst->ips_igmpstat.igps_rcv_queries;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	if ((mrd = (uint_t)igmp3qa->igmp3qa_mxrc) >= IGMP_V3_MAXRT_FPMIN) {
6097c478bd9Sstevel@tonic-gate 		uint_t hdrval, mant, exp;
6107c478bd9Sstevel@tonic-gate 		hdrval = (uint_t)igmp3qa->igmp3qa_mxrc;
6117c478bd9Sstevel@tonic-gate 		mant = hdrval & IGMP_V3_MAXRT_MANT_MASK;
6127c478bd9Sstevel@tonic-gate 		exp = (hdrval & IGMP_V3_MAXRT_EXP_MASK) >> 4;
6137c478bd9Sstevel@tonic-gate 		mrd = (mant | 0x10) << (exp + 3);
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 	if (mrd == 0)
6167c478bd9Sstevel@tonic-gate 		mrd = MCAST_DEF_QUERY_RESP_INTERVAL;
6177c478bd9Sstevel@tonic-gate 	timer = DSEC_TO_MSEC(mrd);
6187c478bd9Sstevel@tonic-gate 	MCAST_RANDOM_DELAY(delay, timer);
6197c478bd9Sstevel@tonic-gate 	next = (unsigned)INFINITY;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	if ((qrv = igmp3qa->igmp3qa_sqrv & IGMP_V3_RV_MASK) == 0)
6227c478bd9Sstevel@tonic-gate 		ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
6237c478bd9Sstevel@tonic-gate 	else
6247c478bd9Sstevel@tonic-gate 		ill->ill_mcast_rv = qrv;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	if ((qqi = (uint_t)igmp3qa->igmp3qa_qqic) >= IGMP_V3_QQI_FPMIN) {
6277c478bd9Sstevel@tonic-gate 		uint_t hdrval, mant, exp;
6287c478bd9Sstevel@tonic-gate 		hdrval = (uint_t)igmp3qa->igmp3qa_qqic;
6297c478bd9Sstevel@tonic-gate 		mant = hdrval & IGMP_V3_QQI_MANT_MASK;
6307c478bd9Sstevel@tonic-gate 		exp = (hdrval & IGMP_V3_QQI_EXP_MASK) >> 4;
6317c478bd9Sstevel@tonic-gate 		qqi = (mant | 0x10) << (exp + 3);
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 	ill->ill_mcast_qi = (qqi == 0) ? MCAST_DEF_QUERY_INTERVAL : qqi;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	/*
6367c478bd9Sstevel@tonic-gate 	 * If we have a pending general query response that's scheduled
6377c478bd9Sstevel@tonic-gate 	 * sooner than the delay we calculated for this response, then
6387c478bd9Sstevel@tonic-gate 	 * no action is required (RFC3376 section 5.2 rule 1)
6397c478bd9Sstevel@tonic-gate 	 */
6407c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
6417c478bd9Sstevel@tonic-gate 	if (ill->ill_global_timer < delay) {
6427c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
6437c478bd9Sstevel@tonic-gate 		return (next);
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	/*
6487c478bd9Sstevel@tonic-gate 	 * Now take action depending upon query type:
6497c478bd9Sstevel@tonic-gate 	 * general, group specific, or group/source specific.
6507c478bd9Sstevel@tonic-gate 	 */
6517c478bd9Sstevel@tonic-gate 	if ((numsrc == 0) && (igmp3qa->igmp3qa_group == INADDR_ANY)) {
6527c478bd9Sstevel@tonic-gate 		/*
6537c478bd9Sstevel@tonic-gate 		 * general query
6547c478bd9Sstevel@tonic-gate 		 * We know global timer is either not running or is
6557c478bd9Sstevel@tonic-gate 		 * greater than our calculated delay, so reset it to
6567c478bd9Sstevel@tonic-gate 		 * our delay (random value in range [0, response time]).
6577c478bd9Sstevel@tonic-gate 		 */
6587c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
6597c478bd9Sstevel@tonic-gate 		ill->ill_global_timer = delay;
6607c478bd9Sstevel@tonic-gate 		next = ill->ill_global_timer;
6617c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	} else {
6647c478bd9Sstevel@tonic-gate 		/* group or group/source specific query */
6657c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
6667c478bd9Sstevel@tonic-gate 		for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
6677c478bd9Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED(&ilm->ilm_v6addr) ||
6687c478bd9Sstevel@tonic-gate 			    (ilm->ilm_addr == htonl(INADDR_ANY)) ||
6697c478bd9Sstevel@tonic-gate 			    (ilm->ilm_addr == htonl(INADDR_ALLHOSTS_GROUP)) ||
6707c478bd9Sstevel@tonic-gate 			    (igmp3qa->igmp3qa_group != ilm->ilm_addr))
6717c478bd9Sstevel@tonic-gate 				continue;
6727c478bd9Sstevel@tonic-gate 			/*
6737c478bd9Sstevel@tonic-gate 			 * If the query is group specific or we have a
6747c478bd9Sstevel@tonic-gate 			 * pending group specific query, the response is
6757c478bd9Sstevel@tonic-gate 			 * group specific (pending sources list should be
6767c478bd9Sstevel@tonic-gate 			 * empty).  Otherwise, need to update the pending
6777c478bd9Sstevel@tonic-gate 			 * sources list for the group and source specific
6787c478bd9Sstevel@tonic-gate 			 * response.
6797c478bd9Sstevel@tonic-gate 			 */
6807c478bd9Sstevel@tonic-gate 			if (numsrc == 0 || (ilm->ilm_timer < INFINITY &&
6817c478bd9Sstevel@tonic-gate 			    SLIST_IS_EMPTY(ilm->ilm_pendsrcs))) {
6827c478bd9Sstevel@tonic-gate group_query:
6837c478bd9Sstevel@tonic-gate 				FREE_SLIST(ilm->ilm_pendsrcs);
6847c478bd9Sstevel@tonic-gate 				ilm->ilm_pendsrcs = NULL;
6857c478bd9Sstevel@tonic-gate 			} else {
6867c478bd9Sstevel@tonic-gate 				boolean_t overflow;
6877c478bd9Sstevel@tonic-gate 				slist_t *pktl;
6887c478bd9Sstevel@tonic-gate 				if (numsrc > MAX_FILTER_SIZE ||
6897c478bd9Sstevel@tonic-gate 				    (ilm->ilm_pendsrcs == NULL &&
6907c478bd9Sstevel@tonic-gate 				    (ilm->ilm_pendsrcs = l_alloc()) == NULL)) {
6917c478bd9Sstevel@tonic-gate 					/*
6927c478bd9Sstevel@tonic-gate 					 * We've been sent more sources than
6937c478bd9Sstevel@tonic-gate 					 * we can deal with; or we can't deal
6947c478bd9Sstevel@tonic-gate 					 * with a source list at all.  Revert
6957c478bd9Sstevel@tonic-gate 					 * to a group specific query.
6967c478bd9Sstevel@tonic-gate 					 */
6977c478bd9Sstevel@tonic-gate 					goto group_query;
6987c478bd9Sstevel@tonic-gate 				}
6997c478bd9Sstevel@tonic-gate 				if ((pktl = l_alloc()) == NULL)
7007c478bd9Sstevel@tonic-gate 					goto group_query;
7017c478bd9Sstevel@tonic-gate 				pktl->sl_numsrc = numsrc;
7027c478bd9Sstevel@tonic-gate 				for (i = 0; i < numsrc; i++)
7037c478bd9Sstevel@tonic-gate 					IN6_IPADDR_TO_V4MAPPED(src_array[i],
7047c478bd9Sstevel@tonic-gate 					    &(pktl->sl_addr[i]));
7057c478bd9Sstevel@tonic-gate 				l_union_in_a(ilm->ilm_pendsrcs, pktl,
7067c478bd9Sstevel@tonic-gate 				    &overflow);
7077c478bd9Sstevel@tonic-gate 				l_free(pktl);
7087c478bd9Sstevel@tonic-gate 				if (overflow)
7097c478bd9Sstevel@tonic-gate 					goto group_query;
7107c478bd9Sstevel@tonic-gate 			}
7117c478bd9Sstevel@tonic-gate 			/* choose soonest timer */
7127c478bd9Sstevel@tonic-gate 			ilm->ilm_timer = MIN(ilm->ilm_timer, delay);
7137c478bd9Sstevel@tonic-gate 			if (ilm->ilm_timer < next)
7147c478bd9Sstevel@tonic-gate 				next = ilm->ilm_timer;
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	return (next);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate void
7237c478bd9Sstevel@tonic-gate igmp_joingroup(ilm_t *ilm)
7247c478bd9Sstevel@tonic-gate {
7257c478bd9Sstevel@tonic-gate 	ill_t	*ill;
726*f4b3ec61Sdh 	ip_stack_t	*ipst = ilm->ilm_ipst;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	ill = ilm->ilm_ipif->ipif_ill;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	ASSERT(IAM_WRITER_ILL(ill));
7317c478bd9Sstevel@tonic-gate 	ASSERT(ilm->ilm_ill == NULL && !ilm->ilm_ipif->ipif_isv6);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
7347c478bd9Sstevel@tonic-gate 	if (ilm->ilm_addr == htonl(INADDR_ALLHOSTS_GROUP)) {
7357c478bd9Sstevel@tonic-gate 		ilm->ilm_rtx.rtx_timer = INFINITY;
7367c478bd9Sstevel@tonic-gate 		ilm->ilm_state = IGMP_OTHERMEMBER;
7377c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
7387c478bd9Sstevel@tonic-gate 	} else {
7397c478bd9Sstevel@tonic-gate 		ip1dbg(("Querier mode %d, sending report, group %x\n",
7407c478bd9Sstevel@tonic-gate 		    ill->ill_mcast_type, htonl(ilm->ilm_addr)));
7417c478bd9Sstevel@tonic-gate 		if (ill->ill_mcast_type == IGMP_V1_ROUTER) {
7427c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
7437c478bd9Sstevel@tonic-gate 			igmp_sendpkt(ilm, IGMP_V1_MEMBERSHIP_REPORT, 0);
7447c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
7457c478bd9Sstevel@tonic-gate 		} else if (ill->ill_mcast_type == IGMP_V2_ROUTER) {
7467c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
7477c478bd9Sstevel@tonic-gate 			igmp_sendpkt(ilm, IGMP_V2_MEMBERSHIP_REPORT, 0);
7487c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
7497c478bd9Sstevel@tonic-gate 		} else if (ill->ill_mcast_type == IGMP_V3_ROUTER) {
7507c478bd9Sstevel@tonic-gate 			mrec_t *rp;
7517c478bd9Sstevel@tonic-gate 			mcast_record_t rtype;
7527c478bd9Sstevel@tonic-gate 			/*
7537c478bd9Sstevel@tonic-gate 			 * The possible state changes we need to handle here:
7547c478bd9Sstevel@tonic-gate 			 *   Old State	New State	Report
7557c478bd9Sstevel@tonic-gate 			 *
7567c478bd9Sstevel@tonic-gate 			 *   INCLUDE(0)	INCLUDE(X)	ALLOW(X),BLOCK(0)
7577c478bd9Sstevel@tonic-gate 			 *   INCLUDE(0)	EXCLUDE(X)	TO_EX(X)
7587c478bd9Sstevel@tonic-gate 			 *
7597c478bd9Sstevel@tonic-gate 			 * No need to send the BLOCK(0) report; ALLOW(X)
7607c478bd9Sstevel@tonic-gate 			 * is enough.
7617c478bd9Sstevel@tonic-gate 			 */
7627c478bd9Sstevel@tonic-gate 			rtype = (ilm->ilm_fmode == MODE_IS_INCLUDE) ?
7637c478bd9Sstevel@tonic-gate 			    ALLOW_NEW_SOURCES : CHANGE_TO_EXCLUDE;
7647c478bd9Sstevel@tonic-gate 			rp = mcast_bldmrec(rtype, &ilm->ilm_v6addr,
7657c478bd9Sstevel@tonic-gate 			    ilm->ilm_filter, NULL);
7667c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
7677c478bd9Sstevel@tonic-gate 			igmpv3_sendrpt(ilm->ilm_ipif, rp);
7687c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
7697c478bd9Sstevel@tonic-gate 			/*
7707c478bd9Sstevel@tonic-gate 			 * Set up retransmission state.  Timer is set below,
7717c478bd9Sstevel@tonic-gate 			 * for both v3 and older versions.
7727c478bd9Sstevel@tonic-gate 			 */
7737c478bd9Sstevel@tonic-gate 			mcast_init_rtx(ill, &ilm->ilm_rtx, rtype,
7747c478bd9Sstevel@tonic-gate 			    ilm->ilm_filter);
7757c478bd9Sstevel@tonic-gate 		}
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		/* Set the ilm timer value */
7787c478bd9Sstevel@tonic-gate 		MCAST_RANDOM_DELAY(ilm->ilm_rtx.rtx_timer,
7797c478bd9Sstevel@tonic-gate 		    SEC_TO_MSEC(IGMP_MAX_HOST_REPORT_DELAY));
7807c478bd9Sstevel@tonic-gate 		ilm->ilm_state = IGMP_IREPORTEDLAST;
7817c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 		/*
7847c478bd9Sstevel@tonic-gate 		 * To avoid deadlock, we don't call igmp_start_timers from
7857c478bd9Sstevel@tonic-gate 		 * here. igmp_start_timers needs to call untimeout, and we
7867c478bd9Sstevel@tonic-gate 		 * can't hold the ipsq across untimeout since
7877c478bd9Sstevel@tonic-gate 		 * igmp_timeout_handler could be blocking trying to
7887c478bd9Sstevel@tonic-gate 		 * acquire the ipsq. Instead we start the timer after we get
7897c478bd9Sstevel@tonic-gate 		 * out of the ipsq in ipsq_exit.
7907c478bd9Sstevel@tonic-gate 		 */
791*f4b3ec61Sdh 		mutex_enter(&ipst->ips_igmp_timer_lock);
792*f4b3ec61Sdh 		ipst->ips_igmp_deferred_next = MIN(ilm->ilm_rtx.rtx_timer,
793*f4b3ec61Sdh 		    ipst->ips_igmp_deferred_next);
794*f4b3ec61Sdh 		mutex_exit(&ipst->ips_igmp_timer_lock);
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	if (ip_debug > 1) {
7987c478bd9Sstevel@tonic-gate 		(void) mi_strlog(ilm->ilm_ipif->ipif_ill->ill_rq, 1, SL_TRACE,
7997c478bd9Sstevel@tonic-gate 		    "igmp_joingroup: multicast_type %d timer %d",
8007c478bd9Sstevel@tonic-gate 		    (ilm->ilm_ipif->ipif_ill->ill_mcast_type),
8017c478bd9Sstevel@tonic-gate 		    (int)ntohl(ilm->ilm_rtx.rtx_timer));
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate void
8067c478bd9Sstevel@tonic-gate mld_joingroup(ilm_t *ilm)
8077c478bd9Sstevel@tonic-gate {
8087c478bd9Sstevel@tonic-gate 	ill_t	*ill;
809*f4b3ec61Sdh 	ip_stack_t	*ipst = ilm->ilm_ipst;
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	ill = ilm->ilm_ill;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	ASSERT(IAM_WRITER_ILL(ill));
8147c478bd9Sstevel@tonic-gate 	ASSERT(ilm->ilm_ipif == NULL && ill->ill_isv6);
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
8177c478bd9Sstevel@tonic-gate 	if (IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast, &ilm->ilm_v6addr)) {
8187c478bd9Sstevel@tonic-gate 		ilm->ilm_rtx.rtx_timer = INFINITY;
8197c478bd9Sstevel@tonic-gate 		ilm->ilm_state = IGMP_OTHERMEMBER;
8207c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
8217c478bd9Sstevel@tonic-gate 	} else {
8227c478bd9Sstevel@tonic-gate 		if (ill->ill_mcast_type == MLD_V1_ROUTER) {
8237c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
8247c478bd9Sstevel@tonic-gate 			mld_sendpkt(ilm, MLD_LISTENER_REPORT, NULL);
8257c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
8267c478bd9Sstevel@tonic-gate 		} else {
8277c478bd9Sstevel@tonic-gate 			mrec_t *rp;
8287c478bd9Sstevel@tonic-gate 			mcast_record_t rtype;
8297c478bd9Sstevel@tonic-gate 			/*
8307c478bd9Sstevel@tonic-gate 			 * The possible state changes we need to handle here:
8317c478bd9Sstevel@tonic-gate 			 *	Old State   New State	Report
8327c478bd9Sstevel@tonic-gate 			 *
8337c478bd9Sstevel@tonic-gate 			 *	INCLUDE(0)  INCLUDE(X)	ALLOW(X),BLOCK(0)
8347c478bd9Sstevel@tonic-gate 			 *	INCLUDE(0)  EXCLUDE(X)	TO_EX(X)
8357c478bd9Sstevel@tonic-gate 			 *
8367c478bd9Sstevel@tonic-gate 			 * No need to send the BLOCK(0) report; ALLOW(X)
8377c478bd9Sstevel@tonic-gate 			 * is enough
8387c478bd9Sstevel@tonic-gate 			 */
8397c478bd9Sstevel@tonic-gate 			rtype = (ilm->ilm_fmode == MODE_IS_INCLUDE) ?
8407c478bd9Sstevel@tonic-gate 			    ALLOW_NEW_SOURCES : CHANGE_TO_EXCLUDE;
8417c478bd9Sstevel@tonic-gate 			rp = mcast_bldmrec(rtype, &ilm->ilm_v6addr,
8427c478bd9Sstevel@tonic-gate 			    ilm->ilm_filter, NULL);
8437c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
8447c478bd9Sstevel@tonic-gate 			mldv2_sendrpt(ill, rp);
8457c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
8467c478bd9Sstevel@tonic-gate 			/*
8477c478bd9Sstevel@tonic-gate 			 * Set up retransmission state.  Timer is set below,
8487c478bd9Sstevel@tonic-gate 			 * for both v2 and v1.
8497c478bd9Sstevel@tonic-gate 			 */
8507c478bd9Sstevel@tonic-gate 			mcast_init_rtx(ill, &ilm->ilm_rtx, rtype,
8517c478bd9Sstevel@tonic-gate 			    ilm->ilm_filter);
8527c478bd9Sstevel@tonic-gate 		}
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 		/* Set the ilm timer value */
8557c478bd9Sstevel@tonic-gate 		ASSERT(ill->ill_mcast_type != MLD_V2_ROUTER ||
8567c478bd9Sstevel@tonic-gate 		    ilm->ilm_rtx.rtx_cnt > 0);
8577c478bd9Sstevel@tonic-gate 		MCAST_RANDOM_DELAY(ilm->ilm_rtx.rtx_timer,
8587c478bd9Sstevel@tonic-gate 		    SEC_TO_MSEC(ICMP6_MAX_HOST_REPORT_DELAY));
8597c478bd9Sstevel@tonic-gate 		ilm->ilm_state = IGMP_IREPORTEDLAST;
8607c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 		/*
8637c478bd9Sstevel@tonic-gate 		 * To avoid deadlock, we don't call mld_start_timers from
8647c478bd9Sstevel@tonic-gate 		 * here. mld_start_timers needs to call untimeout, and we
8657c478bd9Sstevel@tonic-gate 		 * can't hold the ipsq (i.e. the lock) across untimeout
8667c478bd9Sstevel@tonic-gate 		 * since mld_timeout_handler could be blocking trying to
8677c478bd9Sstevel@tonic-gate 		 * acquire the ipsq. Instead we start the timer after we get
8687c478bd9Sstevel@tonic-gate 		 * out of the ipsq in ipsq_exit
8697c478bd9Sstevel@tonic-gate 		 */
870*f4b3ec61Sdh 		mutex_enter(&ipst->ips_mld_timer_lock);
871*f4b3ec61Sdh 		ipst->ips_mld_deferred_next = MIN(ilm->ilm_rtx.rtx_timer,
872*f4b3ec61Sdh 		    ipst->ips_mld_deferred_next);
873*f4b3ec61Sdh 		mutex_exit(&ipst->ips_mld_timer_lock);
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	if (ip_debug > 1) {
8777c478bd9Sstevel@tonic-gate 		(void) mi_strlog(ilm->ilm_ill->ill_rq, 1, SL_TRACE,
8787c478bd9Sstevel@tonic-gate 		    "mld_joingroup: multicast_type %d timer %d",
8797c478bd9Sstevel@tonic-gate 		    (ilm->ilm_ill->ill_mcast_type),
8807c478bd9Sstevel@tonic-gate 		    (int)ntohl(ilm->ilm_rtx.rtx_timer));
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate }
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate void
8857c478bd9Sstevel@tonic-gate igmp_leavegroup(ilm_t *ilm)
8867c478bd9Sstevel@tonic-gate {
8877c478bd9Sstevel@tonic-gate 	ill_t *ill = ilm->ilm_ipif->ipif_ill;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	ASSERT(ilm->ilm_ill == NULL);
8907c478bd9Sstevel@tonic-gate 	ASSERT(!ill->ill_isv6);
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
8937c478bd9Sstevel@tonic-gate 	if (ilm->ilm_state == IGMP_IREPORTEDLAST &&
8947c478bd9Sstevel@tonic-gate 	    ill->ill_mcast_type == IGMP_V2_ROUTER &&
8957c478bd9Sstevel@tonic-gate 	    (ilm->ilm_addr != htonl(INADDR_ALLHOSTS_GROUP))) {
8967c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
8977c478bd9Sstevel@tonic-gate 		igmp_sendpkt(ilm, IGMP_V2_LEAVE_GROUP,
8987c478bd9Sstevel@tonic-gate 		    (htonl(INADDR_ALLRTRS_GROUP)));
8997c478bd9Sstevel@tonic-gate 		return;
9007c478bd9Sstevel@tonic-gate 	} else if ((ill->ill_mcast_type == IGMP_V3_ROUTER) &&
9017c478bd9Sstevel@tonic-gate 	    (ilm->ilm_addr != htonl(INADDR_ALLHOSTS_GROUP))) {
9027c478bd9Sstevel@tonic-gate 		mrec_t *rp;
9037c478bd9Sstevel@tonic-gate 		/*
9047c478bd9Sstevel@tonic-gate 		 * The possible state changes we need to handle here:
9057c478bd9Sstevel@tonic-gate 		 *	Old State	New State	Report
9067c478bd9Sstevel@tonic-gate 		 *
9077c478bd9Sstevel@tonic-gate 		 *	INCLUDE(X)	INCLUDE(0)	ALLOW(0),BLOCK(X)
9087c478bd9Sstevel@tonic-gate 		 *	EXCLUDE(X)	INCLUDE(0)	TO_IN(0)
9097c478bd9Sstevel@tonic-gate 		 *
9107c478bd9Sstevel@tonic-gate 		 * No need to send the ALLOW(0) report; BLOCK(X) is enough
9117c478bd9Sstevel@tonic-gate 		 */
9127c478bd9Sstevel@tonic-gate 		if (ilm->ilm_fmode == MODE_IS_INCLUDE) {
9137c478bd9Sstevel@tonic-gate 			rp = mcast_bldmrec(BLOCK_OLD_SOURCES, &ilm->ilm_v6addr,
9147c478bd9Sstevel@tonic-gate 			    ilm->ilm_filter, NULL);
9157c478bd9Sstevel@tonic-gate 		} else {
9167c478bd9Sstevel@tonic-gate 			rp = mcast_bldmrec(CHANGE_TO_INCLUDE, &ilm->ilm_v6addr,
9177c478bd9Sstevel@tonic-gate 			    NULL, NULL);
9187c478bd9Sstevel@tonic-gate 		}
9197c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
9207c478bd9Sstevel@tonic-gate 		igmpv3_sendrpt(ilm->ilm_ipif, rp);
9217c478bd9Sstevel@tonic-gate 		return;
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate void
9277c478bd9Sstevel@tonic-gate mld_leavegroup(ilm_t *ilm)
9287c478bd9Sstevel@tonic-gate {
9297c478bd9Sstevel@tonic-gate 	ill_t *ill = ilm->ilm_ill;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	ASSERT(ilm->ilm_ipif == NULL);
9327c478bd9Sstevel@tonic-gate 	ASSERT(ill->ill_isv6);
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
9357c478bd9Sstevel@tonic-gate 	if (ilm->ilm_state == IGMP_IREPORTEDLAST &&
9367c478bd9Sstevel@tonic-gate 	    ill->ill_mcast_type == MLD_V1_ROUTER &&
9377c478bd9Sstevel@tonic-gate 	    (!IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast, &ilm->ilm_v6addr))) {
9387c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
9397c478bd9Sstevel@tonic-gate 		mld_sendpkt(ilm, MLD_LISTENER_REDUCTION, &ipv6_all_rtrs_mcast);
9407c478bd9Sstevel@tonic-gate 		return;
9417c478bd9Sstevel@tonic-gate 	} else if ((ill->ill_mcast_type == MLD_V2_ROUTER) &&
9427c478bd9Sstevel@tonic-gate 	    (!IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast, &ilm->ilm_v6addr))) {
9437c478bd9Sstevel@tonic-gate 		mrec_t *rp;
9447c478bd9Sstevel@tonic-gate 		/*
9457c478bd9Sstevel@tonic-gate 		 * The possible state changes we need to handle here:
9467c478bd9Sstevel@tonic-gate 		 *	Old State	New State	Report
9477c478bd9Sstevel@tonic-gate 		 *
9487c478bd9Sstevel@tonic-gate 		 *	INCLUDE(X)	INCLUDE(0)	ALLOW(0),BLOCK(X)
9497c478bd9Sstevel@tonic-gate 		 *	EXCLUDE(X)	INCLUDE(0)	TO_IN(0)
9507c478bd9Sstevel@tonic-gate 		 *
9517c478bd9Sstevel@tonic-gate 		 * No need to send the ALLOW(0) report; BLOCK(X) is enough
9527c478bd9Sstevel@tonic-gate 		 */
9537c478bd9Sstevel@tonic-gate 		if (ilm->ilm_fmode == MODE_IS_INCLUDE) {
9547c478bd9Sstevel@tonic-gate 			rp = mcast_bldmrec(BLOCK_OLD_SOURCES, &ilm->ilm_v6addr,
9557c478bd9Sstevel@tonic-gate 			    ilm->ilm_filter, NULL);
9567c478bd9Sstevel@tonic-gate 		} else {
9577c478bd9Sstevel@tonic-gate 			rp = mcast_bldmrec(CHANGE_TO_INCLUDE, &ilm->ilm_v6addr,
9587c478bd9Sstevel@tonic-gate 			    NULL, NULL);
9597c478bd9Sstevel@tonic-gate 		}
9607c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
9617c478bd9Sstevel@tonic-gate 		mldv2_sendrpt(ill, rp);
9627c478bd9Sstevel@tonic-gate 		return;
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate void
9687c478bd9Sstevel@tonic-gate igmp_statechange(ilm_t *ilm, mcast_record_t fmode, slist_t *flist)
9697c478bd9Sstevel@tonic-gate {
9707c478bd9Sstevel@tonic-gate 	ill_t *ill;
9717c478bd9Sstevel@tonic-gate 	mrec_t *rp;
972*f4b3ec61Sdh 	ip_stack_t	*ipst = ilm->ilm_ipst;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	ASSERT(ilm != NULL);
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	/* state change reports should only be sent if the router is v3 */
9777c478bd9Sstevel@tonic-gate 	if (ilm->ilm_ipif->ipif_ill->ill_mcast_type != IGMP_V3_ROUTER)
9787c478bd9Sstevel@tonic-gate 		return;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	if (ilm->ilm_ill == NULL) {
9817c478bd9Sstevel@tonic-gate 		ASSERT(ilm->ilm_ipif != NULL);
9827c478bd9Sstevel@tonic-gate 		ill = ilm->ilm_ipif->ipif_ill;
9837c478bd9Sstevel@tonic-gate 	} else {
9847c478bd9Sstevel@tonic-gate 		ill = ilm->ilm_ill;
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	/*
9907c478bd9Sstevel@tonic-gate 	 * Compare existing(old) state with the new state and prepare
9917c478bd9Sstevel@tonic-gate 	 * State Change Report, according to the rules in RFC 3376:
9927c478bd9Sstevel@tonic-gate 	 *
9937c478bd9Sstevel@tonic-gate 	 *	Old State	New State	State Change Report
9947c478bd9Sstevel@tonic-gate 	 *
9957c478bd9Sstevel@tonic-gate 	 *	INCLUDE(A)	INCLUDE(B)	ALLOW(B-A),BLOCK(A-B)
9967c478bd9Sstevel@tonic-gate 	 *	EXCLUDE(A)	EXCLUDE(B)	ALLOW(A-B),BLOCK(B-A)
9977c478bd9Sstevel@tonic-gate 	 *	INCLUDE(A)	EXCLUDE(B)	TO_EX(B)
9987c478bd9Sstevel@tonic-gate 	 *	EXCLUDE(A)	INCLUDE(B)	TO_IN(B)
9997c478bd9Sstevel@tonic-gate 	 */
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	if (ilm->ilm_fmode == fmode) {
10027c478bd9Sstevel@tonic-gate 		slist_t	*a_minus_b = NULL, *b_minus_a = NULL;
10037c478bd9Sstevel@tonic-gate 		slist_t *allow, *block;
10047c478bd9Sstevel@tonic-gate 		if (((a_minus_b = l_alloc()) == NULL) ||
10057c478bd9Sstevel@tonic-gate 		    ((b_minus_a = l_alloc()) == NULL)) {
10067c478bd9Sstevel@tonic-gate 			l_free(a_minus_b);
10077c478bd9Sstevel@tonic-gate 			if (ilm->ilm_fmode == MODE_IS_INCLUDE)
10087c478bd9Sstevel@tonic-gate 				goto send_to_ex;
10097c478bd9Sstevel@tonic-gate 			else
10107c478bd9Sstevel@tonic-gate 				goto send_to_in;
10117c478bd9Sstevel@tonic-gate 		}
10127c478bd9Sstevel@tonic-gate 		l_difference(ilm->ilm_filter, flist, a_minus_b);
10137c478bd9Sstevel@tonic-gate 		l_difference(flist, ilm->ilm_filter, b_minus_a);
10147c478bd9Sstevel@tonic-gate 		if (ilm->ilm_fmode == MODE_IS_INCLUDE) {
10157c478bd9Sstevel@tonic-gate 			allow = b_minus_a;
10167c478bd9Sstevel@tonic-gate 			block = a_minus_b;
10177c478bd9Sstevel@tonic-gate 		} else {
10187c478bd9Sstevel@tonic-gate 			allow = a_minus_b;
10197c478bd9Sstevel@tonic-gate 			block = b_minus_a;
10207c478bd9Sstevel@tonic-gate 		}
10217c478bd9Sstevel@tonic-gate 		rp = NULL;
10227c478bd9Sstevel@tonic-gate 		if (!SLIST_IS_EMPTY(allow))
10237c478bd9Sstevel@tonic-gate 			rp = mcast_bldmrec(ALLOW_NEW_SOURCES, &ilm->ilm_v6addr,
10247c478bd9Sstevel@tonic-gate 			    allow, rp);
10257c478bd9Sstevel@tonic-gate 		if (!SLIST_IS_EMPTY(block))
10267c478bd9Sstevel@tonic-gate 			rp = mcast_bldmrec(BLOCK_OLD_SOURCES, &ilm->ilm_v6addr,
10277c478bd9Sstevel@tonic-gate 			    block, rp);
10287c478bd9Sstevel@tonic-gate 		l_free(a_minus_b);
10297c478bd9Sstevel@tonic-gate 		l_free(b_minus_a);
10307c478bd9Sstevel@tonic-gate 	} else if (ilm->ilm_fmode == MODE_IS_INCLUDE) {
10317c478bd9Sstevel@tonic-gate send_to_ex:
10327c478bd9Sstevel@tonic-gate 		rp = mcast_bldmrec(CHANGE_TO_EXCLUDE, &ilm->ilm_v6addr, flist,
10337c478bd9Sstevel@tonic-gate 		    NULL);
10347c478bd9Sstevel@tonic-gate 	} else {
10357c478bd9Sstevel@tonic-gate send_to_in:
10367c478bd9Sstevel@tonic-gate 		rp = mcast_bldmrec(CHANGE_TO_INCLUDE, &ilm->ilm_v6addr, flist,
10377c478bd9Sstevel@tonic-gate 		    NULL);
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	/*
10417c478bd9Sstevel@tonic-gate 	 * Need to set up retransmission state; merge the new info with the
10427c478bd9Sstevel@tonic-gate 	 * current state (which may be null).  If the timer is not currently
10437c478bd9Sstevel@tonic-gate 	 * running, start it (need to do a delayed start of the timer as
10447c478bd9Sstevel@tonic-gate 	 * we're currently in the sq).
10457c478bd9Sstevel@tonic-gate 	 */
10467c478bd9Sstevel@tonic-gate 	rp = mcast_merge_rtx(ilm, rp, flist);
10477c478bd9Sstevel@tonic-gate 	if (ilm->ilm_rtx.rtx_timer == INFINITY) {
10487c478bd9Sstevel@tonic-gate 		MCAST_RANDOM_DELAY(ilm->ilm_rtx.rtx_timer,
10497c478bd9Sstevel@tonic-gate 		    SEC_TO_MSEC(IGMP_MAX_HOST_REPORT_DELAY));
1050*f4b3ec61Sdh 		mutex_enter(&ipst->ips_igmp_timer_lock);
1051*f4b3ec61Sdh 		ipst->ips_igmp_deferred_next = MIN(ipst->ips_igmp_deferred_next,
10527c478bd9Sstevel@tonic-gate 		    ilm->ilm_rtx.rtx_timer);
1053*f4b3ec61Sdh 		mutex_exit(&ipst->ips_igmp_timer_lock);
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
10577c478bd9Sstevel@tonic-gate 	igmpv3_sendrpt(ilm->ilm_ipif, rp);
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate void
10617c478bd9Sstevel@tonic-gate mld_statechange(ilm_t *ilm, mcast_record_t fmode, slist_t *flist)
10627c478bd9Sstevel@tonic-gate {
10637c478bd9Sstevel@tonic-gate 	ill_t *ill;
10647c478bd9Sstevel@tonic-gate 	mrec_t *rp = NULL;
1065*f4b3ec61Sdh 	ip_stack_t	*ipst = ilm->ilm_ipst;
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	ASSERT(ilm != NULL);
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	ill = ilm->ilm_ill;
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	/* only need to send if we have an mldv2-capable router */
10727c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
10737c478bd9Sstevel@tonic-gate 	if (ill->ill_mcast_type != MLD_V2_ROUTER) {
10747c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
10757c478bd9Sstevel@tonic-gate 		return;
10767c478bd9Sstevel@tonic-gate 	}
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	/*
10797c478bd9Sstevel@tonic-gate 	 * Compare existing (old) state with the new state passed in
10807c478bd9Sstevel@tonic-gate 	 * and send appropriate MLDv2 State Change Report.
10817c478bd9Sstevel@tonic-gate 	 *
10827c478bd9Sstevel@tonic-gate 	 *	Old State	New State	State Change Report
10837c478bd9Sstevel@tonic-gate 	 *
10847c478bd9Sstevel@tonic-gate 	 *	INCLUDE(A)	INCLUDE(B)	ALLOW(B-A),BLOCK(A-B)
10857c478bd9Sstevel@tonic-gate 	 *	EXCLUDE(A)	EXCLUDE(B)	ALLOW(A-B),BLOCK(B-A)
10867c478bd9Sstevel@tonic-gate 	 *	INCLUDE(A)	EXCLUDE(B)	TO_EX(B)
10877c478bd9Sstevel@tonic-gate 	 *	EXCLUDE(A)	INCLUDE(B)	TO_IN(B)
10887c478bd9Sstevel@tonic-gate 	 */
10897c478bd9Sstevel@tonic-gate 	if (ilm->ilm_fmode == fmode) {
10907c478bd9Sstevel@tonic-gate 		slist_t	*a_minus_b = NULL, *b_minus_a = NULL;
10917c478bd9Sstevel@tonic-gate 		slist_t *allow, *block;
10927c478bd9Sstevel@tonic-gate 		if (((a_minus_b = l_alloc()) == NULL) ||
10937c478bd9Sstevel@tonic-gate 		    ((b_minus_a = l_alloc()) == NULL)) {
10947c478bd9Sstevel@tonic-gate 			l_free(a_minus_b);
10957c478bd9Sstevel@tonic-gate 			if (ilm->ilm_fmode == MODE_IS_INCLUDE)
10967c478bd9Sstevel@tonic-gate 				goto send_to_ex;
10977c478bd9Sstevel@tonic-gate 			else
10987c478bd9Sstevel@tonic-gate 				goto send_to_in;
10997c478bd9Sstevel@tonic-gate 		}
11007c478bd9Sstevel@tonic-gate 		l_difference(ilm->ilm_filter, flist, a_minus_b);
11017c478bd9Sstevel@tonic-gate 		l_difference(flist, ilm->ilm_filter, b_minus_a);
11027c478bd9Sstevel@tonic-gate 		if (ilm->ilm_fmode == MODE_IS_INCLUDE) {
11037c478bd9Sstevel@tonic-gate 			allow = b_minus_a;
11047c478bd9Sstevel@tonic-gate 			block = a_minus_b;
11057c478bd9Sstevel@tonic-gate 		} else {
11067c478bd9Sstevel@tonic-gate 			allow = a_minus_b;
11077c478bd9Sstevel@tonic-gate 			block = b_minus_a;
11087c478bd9Sstevel@tonic-gate 		}
11097c478bd9Sstevel@tonic-gate 		if (!SLIST_IS_EMPTY(allow))
11107c478bd9Sstevel@tonic-gate 			rp = mcast_bldmrec(ALLOW_NEW_SOURCES, &ilm->ilm_v6addr,
11117c478bd9Sstevel@tonic-gate 			    allow, rp);
11127c478bd9Sstevel@tonic-gate 		if (!SLIST_IS_EMPTY(block))
11137c478bd9Sstevel@tonic-gate 			rp = mcast_bldmrec(BLOCK_OLD_SOURCES, &ilm->ilm_v6addr,
11147c478bd9Sstevel@tonic-gate 			    block, rp);
11157c478bd9Sstevel@tonic-gate 		l_free(a_minus_b);
11167c478bd9Sstevel@tonic-gate 		l_free(b_minus_a);
11177c478bd9Sstevel@tonic-gate 	} else if (ilm->ilm_fmode == MODE_IS_INCLUDE) {
11187c478bd9Sstevel@tonic-gate send_to_ex:
11197c478bd9Sstevel@tonic-gate 		rp = mcast_bldmrec(CHANGE_TO_EXCLUDE, &ilm->ilm_v6addr, flist,
11207c478bd9Sstevel@tonic-gate 		    NULL);
11217c478bd9Sstevel@tonic-gate 	} else {
11227c478bd9Sstevel@tonic-gate send_to_in:
11237c478bd9Sstevel@tonic-gate 		rp = mcast_bldmrec(CHANGE_TO_INCLUDE, &ilm->ilm_v6addr, flist,
11247c478bd9Sstevel@tonic-gate 		    NULL);
11257c478bd9Sstevel@tonic-gate 	}
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	/*
11287c478bd9Sstevel@tonic-gate 	 * Need to set up retransmission state; merge the new info with the
11297c478bd9Sstevel@tonic-gate 	 * current state (which may be null).  If the timer is not currently
11307c478bd9Sstevel@tonic-gate 	 * running, start it (need to do a deferred start of the timer as
11317c478bd9Sstevel@tonic-gate 	 * we're currently in the sq).
11327c478bd9Sstevel@tonic-gate 	 */
11337c478bd9Sstevel@tonic-gate 	rp = mcast_merge_rtx(ilm, rp, flist);
11347c478bd9Sstevel@tonic-gate 	ASSERT(ilm->ilm_rtx.rtx_cnt > 0);
11357c478bd9Sstevel@tonic-gate 	if (ilm->ilm_rtx.rtx_timer == INFINITY) {
11367c478bd9Sstevel@tonic-gate 		MCAST_RANDOM_DELAY(ilm->ilm_rtx.rtx_timer,
11377c478bd9Sstevel@tonic-gate 		    SEC_TO_MSEC(ICMP6_MAX_HOST_REPORT_DELAY));
1138*f4b3ec61Sdh 		mutex_enter(&ipst->ips_mld_timer_lock);
1139*f4b3ec61Sdh 		ipst->ips_mld_deferred_next =
1140*f4b3ec61Sdh 		    MIN(ipst->ips_mld_deferred_next, ilm->ilm_rtx.rtx_timer);
1141*f4b3ec61Sdh 		mutex_exit(&ipst->ips_mld_timer_lock);
11427c478bd9Sstevel@tonic-gate 	}
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
11457c478bd9Sstevel@tonic-gate 	mldv2_sendrpt(ill, rp);
11467c478bd9Sstevel@tonic-gate }
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate uint_t
11497c478bd9Sstevel@tonic-gate igmp_timeout_handler_per_ill(ill_t *ill, int elapsed)
11507c478bd9Sstevel@tonic-gate {
11517c478bd9Sstevel@tonic-gate 	uint_t	next = INFINITY;
11527c478bd9Sstevel@tonic-gate 	ilm_t	*ilm;
11537c478bd9Sstevel@tonic-gate 	ipif_t	*ipif;
11547c478bd9Sstevel@tonic-gate 	mrec_t	*rp = NULL;
11557c478bd9Sstevel@tonic-gate 	mrec_t	*rtxrp = NULL;
11567c478bd9Sstevel@tonic-gate 	rtx_state_t *rtxp;
11577c478bd9Sstevel@tonic-gate 	mcast_record_t	rtype;
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	ASSERT(IAM_WRITER_ILL(ill));
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	/* First check the global timer on this interface */
11647c478bd9Sstevel@tonic-gate 	if (ill->ill_global_timer == INFINITY)
11657c478bd9Sstevel@tonic-gate 		goto per_ilm_timer;
11667c478bd9Sstevel@tonic-gate 	if (ill->ill_global_timer <= elapsed) {
11677c478bd9Sstevel@tonic-gate 		ill->ill_global_timer = INFINITY;
11687c478bd9Sstevel@tonic-gate 		/*
11697c478bd9Sstevel@tonic-gate 		 * Send report for each group on this interface.
11707c478bd9Sstevel@tonic-gate 		 * Since we just set the global timer (received a v3 general
11717c478bd9Sstevel@tonic-gate 		 * query), need to skip the all hosts addr (224.0.0.1), per
11727c478bd9Sstevel@tonic-gate 		 * RFC 3376 section 5.
11737c478bd9Sstevel@tonic-gate 		 */
11747c478bd9Sstevel@tonic-gate 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
11757c478bd9Sstevel@tonic-gate 			if (ilm->ilm_addr == htonl(INADDR_ALLHOSTS_GROUP))
11767c478bd9Sstevel@tonic-gate 				continue;
11777c478bd9Sstevel@tonic-gate 			ASSERT(ilm->ilm_ipif != NULL);
11787c478bd9Sstevel@tonic-gate 			ilm->ilm_ipif->ipif_igmp_rpt =
11797c478bd9Sstevel@tonic-gate 			    mcast_bldmrec(ilm->ilm_fmode, &ilm->ilm_v6addr,
11807c478bd9Sstevel@tonic-gate 			    ilm->ilm_filter, ilm->ilm_ipif->ipif_igmp_rpt);
11817c478bd9Sstevel@tonic-gate 			/*
11827c478bd9Sstevel@tonic-gate 			 * Since we're sending a report on this group, okay
11837c478bd9Sstevel@tonic-gate 			 * to delete pending group-specific timers.  Note
11847c478bd9Sstevel@tonic-gate 			 * that group-specific retransmit timers still need
11857c478bd9Sstevel@tonic-gate 			 * to be checked in the per_ilm_timer for-loop.
11867c478bd9Sstevel@tonic-gate 			 */
11877c478bd9Sstevel@tonic-gate 			ilm->ilm_timer = INFINITY;
11887c478bd9Sstevel@tonic-gate 			ilm->ilm_state = IGMP_IREPORTEDLAST;
11897c478bd9Sstevel@tonic-gate 			FREE_SLIST(ilm->ilm_pendsrcs);
11907c478bd9Sstevel@tonic-gate 			ilm->ilm_pendsrcs = NULL;
11917c478bd9Sstevel@tonic-gate 		}
11927c478bd9Sstevel@tonic-gate 		/*
11937c478bd9Sstevel@tonic-gate 		 * We've built per-ipif mrec lists; walk the ill's ipif list
11947c478bd9Sstevel@tonic-gate 		 * and send a report for each ipif that has an mrec list.
11957c478bd9Sstevel@tonic-gate 		 */
11967c478bd9Sstevel@tonic-gate 		for (ipif = ill->ill_ipif; ipif != NULL;
11977c478bd9Sstevel@tonic-gate 		    ipif = ipif->ipif_next) {
11987c478bd9Sstevel@tonic-gate 			if (ipif->ipif_igmp_rpt == NULL)
11997c478bd9Sstevel@tonic-gate 				continue;
12007c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
12017c478bd9Sstevel@tonic-gate 			igmpv3_sendrpt(ipif, ipif->ipif_igmp_rpt);
12027c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
12037c478bd9Sstevel@tonic-gate 			/* mrec list was freed by igmpv3_sendrpt() */
12047c478bd9Sstevel@tonic-gate 			ipif->ipif_igmp_rpt = NULL;
12057c478bd9Sstevel@tonic-gate 		}
12067c478bd9Sstevel@tonic-gate 	} else {
12077c478bd9Sstevel@tonic-gate 		ill->ill_global_timer -= elapsed;
12087c478bd9Sstevel@tonic-gate 		if (ill->ill_global_timer < next)
12097c478bd9Sstevel@tonic-gate 			next = ill->ill_global_timer;
12107c478bd9Sstevel@tonic-gate 	}
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate per_ilm_timer:
12137c478bd9Sstevel@tonic-gate 	for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
12147c478bd9Sstevel@tonic-gate 		if (ilm->ilm_timer == INFINITY)
12157c478bd9Sstevel@tonic-gate 			goto per_ilm_rtxtimer;
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 		if (ilm->ilm_timer > elapsed) {
12187c478bd9Sstevel@tonic-gate 			ilm->ilm_timer -= elapsed;
12197c478bd9Sstevel@tonic-gate 			if (ilm->ilm_timer < next)
12207c478bd9Sstevel@tonic-gate 				next = ilm->ilm_timer;
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 			if (ip_debug > 1) {
12237c478bd9Sstevel@tonic-gate 				(void) mi_strlog(ill->ill_rq, 1, SL_TRACE,
12247c478bd9Sstevel@tonic-gate 				    "igmp_timo_hlr 2: ilm_timr %d elap %d "
12257c478bd9Sstevel@tonic-gate 				    "typ %d nxt %d",
12267c478bd9Sstevel@tonic-gate 				    (int)ntohl(ilm->ilm_timer), elapsed,
12277c478bd9Sstevel@tonic-gate 				    (ill->ill_mcast_type), next);
12287c478bd9Sstevel@tonic-gate 			}
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 			goto per_ilm_rtxtimer;
12317c478bd9Sstevel@tonic-gate 		}
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 		/* the timer has expired, need to take action */
12347c478bd9Sstevel@tonic-gate 		ilm->ilm_timer = INFINITY;
12357c478bd9Sstevel@tonic-gate 		ilm->ilm_state = IGMP_IREPORTEDLAST;
12367c478bd9Sstevel@tonic-gate 		if (ill->ill_mcast_type == IGMP_V1_ROUTER) {
12377c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
12387c478bd9Sstevel@tonic-gate 			igmp_sendpkt(ilm, IGMP_V1_MEMBERSHIP_REPORT, 0);
12397c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
12407c478bd9Sstevel@tonic-gate 		} else if (ill->ill_mcast_type == IGMP_V2_ROUTER) {
12417c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
12427c478bd9Sstevel@tonic-gate 			igmp_sendpkt(ilm, IGMP_V2_MEMBERSHIP_REPORT, 0);
12437c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
12447c478bd9Sstevel@tonic-gate 		} else {
12457c478bd9Sstevel@tonic-gate 			slist_t *rsp;
12467c478bd9Sstevel@tonic-gate 			if (!SLIST_IS_EMPTY(ilm->ilm_pendsrcs) &&
12477c478bd9Sstevel@tonic-gate 			    (rsp = l_alloc()) != NULL) {
12487c478bd9Sstevel@tonic-gate 				/*
12497c478bd9Sstevel@tonic-gate 				 * Contents of reply depend on pending
12507c478bd9Sstevel@tonic-gate 				 * requested source list.
12517c478bd9Sstevel@tonic-gate 				 */
12527c478bd9Sstevel@tonic-gate 				if (ilm->ilm_fmode == MODE_IS_INCLUDE) {
12537c478bd9Sstevel@tonic-gate 					l_intersection(ilm->ilm_filter,
12547c478bd9Sstevel@tonic-gate 					    ilm->ilm_pendsrcs, rsp);
12557c478bd9Sstevel@tonic-gate 				} else {
12567c478bd9Sstevel@tonic-gate 					l_difference(ilm->ilm_pendsrcs,
12577c478bd9Sstevel@tonic-gate 					    ilm->ilm_filter, rsp);
12587c478bd9Sstevel@tonic-gate 				}
12597c478bd9Sstevel@tonic-gate 				FREE_SLIST(ilm->ilm_pendsrcs);
12607c478bd9Sstevel@tonic-gate 				ilm->ilm_pendsrcs = NULL;
12617c478bd9Sstevel@tonic-gate 				if (!SLIST_IS_EMPTY(rsp))
12627c478bd9Sstevel@tonic-gate 					rp = mcast_bldmrec(MODE_IS_INCLUDE,
12637c478bd9Sstevel@tonic-gate 					    &ilm->ilm_v6addr, rsp, rp);
12647c478bd9Sstevel@tonic-gate 				FREE_SLIST(rsp);
12657c478bd9Sstevel@tonic-gate 			} else {
12667c478bd9Sstevel@tonic-gate 				/*
12677c478bd9Sstevel@tonic-gate 				 * Either the pending request is just group-
12687c478bd9Sstevel@tonic-gate 				 * specific, or we couldn't get the resources
12697c478bd9Sstevel@tonic-gate 				 * (rsp) to build a source-specific reply.
12707c478bd9Sstevel@tonic-gate 				 */
12717c478bd9Sstevel@tonic-gate 				rp = mcast_bldmrec(ilm->ilm_fmode,
12727c478bd9Sstevel@tonic-gate 				    &ilm->ilm_v6addr, ilm->ilm_filter, rp);
12737c478bd9Sstevel@tonic-gate 			}
12747c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
12757c478bd9Sstevel@tonic-gate 			igmpv3_sendrpt(ill->ill_ipif, rp);
12767c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
12777c478bd9Sstevel@tonic-gate 			rp = NULL;
12787c478bd9Sstevel@tonic-gate 		}
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 		if (ip_debug > 1) {
12817c478bd9Sstevel@tonic-gate 			(void) mi_strlog(ill->ill_rq, 1, SL_TRACE,
12827c478bd9Sstevel@tonic-gate 			    "igmp_timo_hlr 1: ilm_timr %d elap %d "
12837c478bd9Sstevel@tonic-gate 			    "typ %d nxt %d",
12847c478bd9Sstevel@tonic-gate 			    (int)ntohl(ilm->ilm_timer), elapsed,
12857c478bd9Sstevel@tonic-gate 			    (ill->ill_mcast_type), next);
12867c478bd9Sstevel@tonic-gate 		}
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate per_ilm_rtxtimer:
12897c478bd9Sstevel@tonic-gate 		rtxp = &ilm->ilm_rtx;
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 		if (rtxp->rtx_timer == INFINITY)
12927c478bd9Sstevel@tonic-gate 			continue;
12937c478bd9Sstevel@tonic-gate 		if (rtxp->rtx_timer > elapsed) {
12947c478bd9Sstevel@tonic-gate 			rtxp->rtx_timer -= elapsed;
12957c478bd9Sstevel@tonic-gate 			if (rtxp->rtx_timer < next)
12967c478bd9Sstevel@tonic-gate 				next = rtxp->rtx_timer;
12977c478bd9Sstevel@tonic-gate 			continue;
12987c478bd9Sstevel@tonic-gate 		}
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 		rtxp->rtx_timer = INFINITY;
13017c478bd9Sstevel@tonic-gate 		ilm->ilm_state = IGMP_IREPORTEDLAST;
13027c478bd9Sstevel@tonic-gate 		if (ill->ill_mcast_type == IGMP_V1_ROUTER) {
13037c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
13047c478bd9Sstevel@tonic-gate 			igmp_sendpkt(ilm, IGMP_V1_MEMBERSHIP_REPORT, 0);
13057c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
13067c478bd9Sstevel@tonic-gate 			continue;
13077c478bd9Sstevel@tonic-gate 		} else if (ill->ill_mcast_type == IGMP_V2_ROUTER) {
13087c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
13097c478bd9Sstevel@tonic-gate 			igmp_sendpkt(ilm, IGMP_V2_MEMBERSHIP_REPORT, 0);
13107c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
13117c478bd9Sstevel@tonic-gate 			continue;
13127c478bd9Sstevel@tonic-gate 		}
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 		/*
13157c478bd9Sstevel@tonic-gate 		 * The retransmit timer has popped, and our router is
13167c478bd9Sstevel@tonic-gate 		 * IGMPv3.  We have to delve into the retransmit state
13177c478bd9Sstevel@tonic-gate 		 * stored in the ilm.
13187c478bd9Sstevel@tonic-gate 		 *
13197c478bd9Sstevel@tonic-gate 		 * Decrement the retransmit count.  If the fmode rtx
13207c478bd9Sstevel@tonic-gate 		 * count is active, decrement it, and send a filter
13217c478bd9Sstevel@tonic-gate 		 * mode change report with the ilm's source list.
13227c478bd9Sstevel@tonic-gate 		 * Otherwise, send a source list change report with
13237c478bd9Sstevel@tonic-gate 		 * the current retransmit lists.
13247c478bd9Sstevel@tonic-gate 		 */
13257c478bd9Sstevel@tonic-gate 		ASSERT(rtxp->rtx_cnt > 0);
13267c478bd9Sstevel@tonic-gate 		ASSERT(rtxp->rtx_cnt >= rtxp->rtx_fmode_cnt);
13277c478bd9Sstevel@tonic-gate 		rtxp->rtx_cnt--;
13287c478bd9Sstevel@tonic-gate 		if (rtxp->rtx_fmode_cnt > 0) {
13297c478bd9Sstevel@tonic-gate 			rtxp->rtx_fmode_cnt--;
13307c478bd9Sstevel@tonic-gate 			rtype = (ilm->ilm_fmode == MODE_IS_INCLUDE) ?
13317c478bd9Sstevel@tonic-gate 			    CHANGE_TO_INCLUDE : CHANGE_TO_EXCLUDE;
13327c478bd9Sstevel@tonic-gate 			rtxrp = mcast_bldmrec(rtype, &ilm->ilm_v6addr,
13337c478bd9Sstevel@tonic-gate 			    ilm->ilm_filter, rtxrp);
13347c478bd9Sstevel@tonic-gate 		} else {
13357c478bd9Sstevel@tonic-gate 			rtxrp = mcast_bldmrec(ALLOW_NEW_SOURCES,
13367c478bd9Sstevel@tonic-gate 			    &ilm->ilm_v6addr, rtxp->rtx_allow, rtxrp);
13377c478bd9Sstevel@tonic-gate 			rtxrp = mcast_bldmrec(BLOCK_OLD_SOURCES,
13387c478bd9Sstevel@tonic-gate 			    &ilm->ilm_v6addr, rtxp->rtx_block, rtxrp);
13397c478bd9Sstevel@tonic-gate 		}
13407c478bd9Sstevel@tonic-gate 		if (rtxp->rtx_cnt > 0) {
13417c478bd9Sstevel@tonic-gate 			MCAST_RANDOM_DELAY(rtxp->rtx_timer,
13427c478bd9Sstevel@tonic-gate 			    SEC_TO_MSEC(IGMP_MAX_HOST_REPORT_DELAY));
13437c478bd9Sstevel@tonic-gate 			if (rtxp->rtx_timer < next)
13447c478bd9Sstevel@tonic-gate 				next = rtxp->rtx_timer;
13457c478bd9Sstevel@tonic-gate 		} else {
13467c478bd9Sstevel@tonic-gate 			CLEAR_SLIST(rtxp->rtx_allow);
13477c478bd9Sstevel@tonic-gate 			CLEAR_SLIST(rtxp->rtx_block);
13487c478bd9Sstevel@tonic-gate 		}
13497c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
13507c478bd9Sstevel@tonic-gate 		igmpv3_sendrpt(ilm->ilm_ipif, rtxrp);
13517c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
13527c478bd9Sstevel@tonic-gate 		rtxrp = NULL;
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	return (next);
13587c478bd9Sstevel@tonic-gate }
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate /*
13617c478bd9Sstevel@tonic-gate  * igmp_timeout_handler:
13627c478bd9Sstevel@tonic-gate  * Called when there are timeout events, every next * TMEOUT_INTERVAL (tick).
13637c478bd9Sstevel@tonic-gate  * Returns number of ticks to next event (or 0 if none).
13647c478bd9Sstevel@tonic-gate  *
13657c478bd9Sstevel@tonic-gate  * As part of multicast join and leave igmp we may need to send out an
13667c478bd9Sstevel@tonic-gate  * igmp request. The igmp related state variables in the ilm are protected
13677c478bd9Sstevel@tonic-gate  * by ill_lock. A single global igmp timer is used to track igmp timeouts.
13687c478bd9Sstevel@tonic-gate  * igmp_timer_lock protects the global igmp_timeout_id. igmp_start_timers
13697c478bd9Sstevel@tonic-gate  * starts the igmp timer if needed. It serializes multiple threads trying to
13707c478bd9Sstevel@tonic-gate  * simultaneously start the timer using the igmp_timer_setter_active flag.
13717c478bd9Sstevel@tonic-gate  *
13727c478bd9Sstevel@tonic-gate  * igmp_input() receives igmp queries and responds to the queries
13737c478bd9Sstevel@tonic-gate  * in a delayed fashion by posting a timer i.e. it calls igmp_start_timers().
13747c478bd9Sstevel@tonic-gate  * Later the igmp_timer fires, the timeout handler igmp_timerout_handler()
13757c478bd9Sstevel@tonic-gate  * performs the action exclusively after entering each ill's ipsq as writer.
13767c478bd9Sstevel@tonic-gate  * The actual igmp timeout handler needs to run in the ipsq since it has to
13777c478bd9Sstevel@tonic-gate  * access the ilm's and we don't want another exclusive operation like
13787c478bd9Sstevel@tonic-gate  * say an IPMP failover to be simultaneously moving the ilms from one ill to
13797c478bd9Sstevel@tonic-gate  * another.
13807c478bd9Sstevel@tonic-gate  *
13817c478bd9Sstevel@tonic-gate  * The igmp_slowtimeo() function is called thru another timer.
13827c478bd9Sstevel@tonic-gate  * igmp_slowtimeout_lock protects the igmp_slowtimeout_id
13837c478bd9Sstevel@tonic-gate  */
13847c478bd9Sstevel@tonic-gate void
13857c478bd9Sstevel@tonic-gate igmp_timeout_handler(void *arg)
13867c478bd9Sstevel@tonic-gate {
13877c478bd9Sstevel@tonic-gate 	ill_t	*ill;
13887c478bd9Sstevel@tonic-gate 	int	elapsed;	/* Since last call */
13897c478bd9Sstevel@tonic-gate 	uint_t  global_next = INFINITY;
13907c478bd9Sstevel@tonic-gate 	uint_t  next;
13917c478bd9Sstevel@tonic-gate 	ill_walk_context_t ctx;
13927c478bd9Sstevel@tonic-gate 	boolean_t success;
1393*f4b3ec61Sdh 	ip_stack_t *ipst = (ip_stack_t *)arg;
1394*f4b3ec61Sdh 
1395*f4b3ec61Sdh 	ASSERT(arg != NULL);
1396*f4b3ec61Sdh 	mutex_enter(&ipst->ips_igmp_timer_lock);
1397*f4b3ec61Sdh 	ASSERT(ipst->ips_igmp_timeout_id != 0);
1398*f4b3ec61Sdh 	ipst->ips_igmp_timer_fired_last = ddi_get_lbolt();
1399*f4b3ec61Sdh 	elapsed = ipst->ips_igmp_time_to_next;
1400*f4b3ec61Sdh 	ipst->ips_igmp_time_to_next = 0;
1401*f4b3ec61Sdh 	mutex_exit(&ipst->ips_igmp_timer_lock);
1402*f4b3ec61Sdh 
1403*f4b3ec61Sdh 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
1404*f4b3ec61Sdh 	ill = ILL_START_WALK_V4(&ctx, ipst);
14057c478bd9Sstevel@tonic-gate 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
14067c478bd9Sstevel@tonic-gate 		ASSERT(!ill->ill_isv6);
14077c478bd9Sstevel@tonic-gate 		/*
14087c478bd9Sstevel@tonic-gate 		 * We may not be able to refhold the ill if the ill/ipif
14097c478bd9Sstevel@tonic-gate 		 * is changing. But we need to make sure that the ill will
14107c478bd9Sstevel@tonic-gate 		 * not vanish. So we just bump up the ill_waiter count.
14117c478bd9Sstevel@tonic-gate 		 */
14127c478bd9Sstevel@tonic-gate 		if (!ill_waiter_inc(ill))
14137c478bd9Sstevel@tonic-gate 			continue;
1414*f4b3ec61Sdh 		rw_exit(&ipst->ips_ill_g_lock);
14157c478bd9Sstevel@tonic-gate 		success = ipsq_enter(ill, B_TRUE);
14167c478bd9Sstevel@tonic-gate 		if (success) {
14177c478bd9Sstevel@tonic-gate 			next = igmp_timeout_handler_per_ill(ill, elapsed);
14187c478bd9Sstevel@tonic-gate 			if (next < global_next)
14197c478bd9Sstevel@tonic-gate 				global_next = next;
14207c478bd9Sstevel@tonic-gate 			ipsq_exit(ill->ill_phyint->phyint_ipsq, B_FALSE,
14217c478bd9Sstevel@tonic-gate 			    B_TRUE);
14227c478bd9Sstevel@tonic-gate 		}
1423*f4b3ec61Sdh 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14247c478bd9Sstevel@tonic-gate 		ill_waiter_dcr(ill);
14257c478bd9Sstevel@tonic-gate 	}
1426*f4b3ec61Sdh 	rw_exit(&ipst->ips_ill_g_lock);
14277c478bd9Sstevel@tonic-gate 
1428*f4b3ec61Sdh 	mutex_enter(&ipst->ips_igmp_timer_lock);
1429*f4b3ec61Sdh 	ASSERT(ipst->ips_igmp_timeout_id != 0);
1430*f4b3ec61Sdh 	ipst->ips_igmp_timeout_id = 0;
1431*f4b3ec61Sdh 	mutex_exit(&ipst->ips_igmp_timer_lock);
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	if (global_next != INFINITY)
1434*f4b3ec61Sdh 		igmp_start_timers(global_next, ipst);
14357c478bd9Sstevel@tonic-gate }
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate /*
14387c478bd9Sstevel@tonic-gate  * mld_timeout_handler:
14397c478bd9Sstevel@tonic-gate  * Called when there are timeout events, every next (tick).
14407c478bd9Sstevel@tonic-gate  * Returns number of ticks to next event (or 0 if none).
14417c478bd9Sstevel@tonic-gate  */
14427c478bd9Sstevel@tonic-gate /* ARGSUSED */
14437c478bd9Sstevel@tonic-gate uint_t
14447c478bd9Sstevel@tonic-gate mld_timeout_handler_per_ill(ill_t *ill, int elapsed)
14457c478bd9Sstevel@tonic-gate {
14467c478bd9Sstevel@tonic-gate 	ilm_t 	*ilm;
14477c478bd9Sstevel@tonic-gate 	uint_t	next = INFINITY;
14487c478bd9Sstevel@tonic-gate 	mrec_t	*rp, *rtxrp;
14497c478bd9Sstevel@tonic-gate 	rtx_state_t *rtxp;
14507c478bd9Sstevel@tonic-gate 	mcast_record_t	rtype;
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	ASSERT(IAM_WRITER_ILL(ill));
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	/*
14577c478bd9Sstevel@tonic-gate 	 * First check the global timer on this interface; the global timer
14587c478bd9Sstevel@tonic-gate 	 * is not used for MLDv1, so if it's set we can assume we're v2.
14597c478bd9Sstevel@tonic-gate 	 */
14607c478bd9Sstevel@tonic-gate 	if (ill->ill_global_timer == INFINITY)
14617c478bd9Sstevel@tonic-gate 		goto per_ilm_timer;
14627c478bd9Sstevel@tonic-gate 	if (ill->ill_global_timer <= elapsed) {
14637c478bd9Sstevel@tonic-gate 		ill->ill_global_timer = INFINITY;
14647c478bd9Sstevel@tonic-gate 		/*
14657c478bd9Sstevel@tonic-gate 		 * Send report for each group on this interface.
14667c478bd9Sstevel@tonic-gate 		 * Since we just set the global timer (received a v2 general
14677c478bd9Sstevel@tonic-gate 		 * query), need to skip the all hosts addr (ff02::1), per
14687c478bd9Sstevel@tonic-gate 		 * RFC 3810 section 6.
14697c478bd9Sstevel@tonic-gate 		 */
14707c478bd9Sstevel@tonic-gate 		rp = NULL;
14717c478bd9Sstevel@tonic-gate 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
14727c478bd9Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(&ilm->ilm_v6addr,
14737c478bd9Sstevel@tonic-gate 			    &ipv6_all_hosts_mcast))
14747c478bd9Sstevel@tonic-gate 				continue;
14757c478bd9Sstevel@tonic-gate 			rp = mcast_bldmrec(ilm->ilm_fmode, &ilm->ilm_v6addr,
14767c478bd9Sstevel@tonic-gate 			    ilm->ilm_filter, rp);
14777c478bd9Sstevel@tonic-gate 			/*
14787c478bd9Sstevel@tonic-gate 			 * Since we're sending a report on this group, okay
14797c478bd9Sstevel@tonic-gate 			 * to delete pending group-specific timers.  Note
14807c478bd9Sstevel@tonic-gate 			 * that group-specific retransmit timers still need
14817c478bd9Sstevel@tonic-gate 			 * to be checked in the per_ilm_timer for-loop.
14827c478bd9Sstevel@tonic-gate 			 */
14837c478bd9Sstevel@tonic-gate 			ilm->ilm_timer = INFINITY;
14847c478bd9Sstevel@tonic-gate 			ilm->ilm_state = IGMP_IREPORTEDLAST;
14857c478bd9Sstevel@tonic-gate 			FREE_SLIST(ilm->ilm_pendsrcs);
14867c478bd9Sstevel@tonic-gate 			ilm->ilm_pendsrcs = NULL;
14877c478bd9Sstevel@tonic-gate 		}
14887c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
14897c478bd9Sstevel@tonic-gate 		mldv2_sendrpt(ill, rp);
14907c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
14917c478bd9Sstevel@tonic-gate 	} else {
14927c478bd9Sstevel@tonic-gate 		ill->ill_global_timer -= elapsed;
14937c478bd9Sstevel@tonic-gate 		if (ill->ill_global_timer < next)
14947c478bd9Sstevel@tonic-gate 			next = ill->ill_global_timer;
14957c478bd9Sstevel@tonic-gate 	}
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate per_ilm_timer:
14987c478bd9Sstevel@tonic-gate 	rp = rtxrp = NULL;
14997c478bd9Sstevel@tonic-gate 	for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
15007c478bd9Sstevel@tonic-gate 		if (ilm->ilm_timer == INFINITY)
15017c478bd9Sstevel@tonic-gate 			goto per_ilm_rtxtimer;
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 		if (ilm->ilm_timer > elapsed) {
15047c478bd9Sstevel@tonic-gate 			ilm->ilm_timer -= elapsed;
15057c478bd9Sstevel@tonic-gate 			if (ilm->ilm_timer < next)
15067c478bd9Sstevel@tonic-gate 				next = ilm->ilm_timer;
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 			if (ip_debug > 1) {
15097c478bd9Sstevel@tonic-gate 				(void) mi_strlog(ill->ill_rq, 1, SL_TRACE,
15107c478bd9Sstevel@tonic-gate 				    "igmp_timo_hlr 2: ilm_timr"
15117c478bd9Sstevel@tonic-gate 				    " %d elap %d typ %d nxt %d",
15127c478bd9Sstevel@tonic-gate 				    (int)ntohl(ilm->ilm_timer), elapsed,
15137c478bd9Sstevel@tonic-gate 				    (ill->ill_mcast_type), next);
15147c478bd9Sstevel@tonic-gate 			}
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 			goto per_ilm_rtxtimer;
15177c478bd9Sstevel@tonic-gate 		}
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 		/* the timer has expired, need to take action */
15207c478bd9Sstevel@tonic-gate 		ilm->ilm_timer = INFINITY;
15217c478bd9Sstevel@tonic-gate 		ilm->ilm_state = IGMP_IREPORTEDLAST;
15227c478bd9Sstevel@tonic-gate 		if (ill->ill_mcast_type == MLD_V1_ROUTER) {
15237c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
15247c478bd9Sstevel@tonic-gate 			mld_sendpkt(ilm, MLD_LISTENER_REPORT, NULL);
15257c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
15267c478bd9Sstevel@tonic-gate 		} else {
15277c478bd9Sstevel@tonic-gate 			slist_t *rsp;
15287c478bd9Sstevel@tonic-gate 			if (!SLIST_IS_EMPTY(ilm->ilm_pendsrcs) &&
15297c478bd9Sstevel@tonic-gate 			    (rsp = l_alloc()) != NULL) {
15307c478bd9Sstevel@tonic-gate 				/*
15317c478bd9Sstevel@tonic-gate 				 * Contents of reply depend on pending
15327c478bd9Sstevel@tonic-gate 				 * requested source list.
15337c478bd9Sstevel@tonic-gate 				 */
15347c478bd9Sstevel@tonic-gate 				if (ilm->ilm_fmode == MODE_IS_INCLUDE) {
15357c478bd9Sstevel@tonic-gate 					l_intersection(ilm->ilm_filter,
15367c478bd9Sstevel@tonic-gate 					    ilm->ilm_pendsrcs, rsp);
15377c478bd9Sstevel@tonic-gate 				} else {
15387c478bd9Sstevel@tonic-gate 					l_difference(ilm->ilm_pendsrcs,
15397c478bd9Sstevel@tonic-gate 					    ilm->ilm_filter, rsp);
15407c478bd9Sstevel@tonic-gate 				}
15417c478bd9Sstevel@tonic-gate 				FREE_SLIST(ilm->ilm_pendsrcs);
15427c478bd9Sstevel@tonic-gate 				ilm->ilm_pendsrcs = NULL;
15437c478bd9Sstevel@tonic-gate 				if (!SLIST_IS_EMPTY(rsp))
15447c478bd9Sstevel@tonic-gate 					rp = mcast_bldmrec(MODE_IS_INCLUDE,
15457c478bd9Sstevel@tonic-gate 					    &ilm->ilm_v6addr, rsp, rp);
15467c478bd9Sstevel@tonic-gate 				FREE_SLIST(rsp);
15477c478bd9Sstevel@tonic-gate 			} else {
15487c478bd9Sstevel@tonic-gate 				rp = mcast_bldmrec(ilm->ilm_fmode,
15497c478bd9Sstevel@tonic-gate 				    &ilm->ilm_v6addr, ilm->ilm_filter, rp);
15507c478bd9Sstevel@tonic-gate 			}
15517c478bd9Sstevel@tonic-gate 		}
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 		if (ip_debug > 1) {
15547c478bd9Sstevel@tonic-gate 			(void) mi_strlog(ill->ill_rq, 1, SL_TRACE,
15557c478bd9Sstevel@tonic-gate 			    "igmp_timo_hlr 1: ilm_timr %d elap %d "
15567c478bd9Sstevel@tonic-gate 			    "typ %d nxt %d",
15577c478bd9Sstevel@tonic-gate 			    (int)ntohl(ilm->ilm_timer), elapsed,
15587c478bd9Sstevel@tonic-gate 			    (ill->ill_mcast_type), next);
15597c478bd9Sstevel@tonic-gate 		}
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate per_ilm_rtxtimer:
15627c478bd9Sstevel@tonic-gate 		rtxp = &ilm->ilm_rtx;
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 		if (rtxp->rtx_timer == INFINITY)
15657c478bd9Sstevel@tonic-gate 			continue;
15667c478bd9Sstevel@tonic-gate 		if (rtxp->rtx_timer > elapsed) {
15677c478bd9Sstevel@tonic-gate 			rtxp->rtx_timer -= elapsed;
15687c478bd9Sstevel@tonic-gate 			if (rtxp->rtx_timer < next)
15697c478bd9Sstevel@tonic-gate 				next = rtxp->rtx_timer;
15707c478bd9Sstevel@tonic-gate 			continue;
15717c478bd9Sstevel@tonic-gate 		}
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 		rtxp->rtx_timer = INFINITY;
15747c478bd9Sstevel@tonic-gate 		ilm->ilm_state = IGMP_IREPORTEDLAST;
15757c478bd9Sstevel@tonic-gate 		if (ill->ill_mcast_type == MLD_V1_ROUTER) {
15767c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
15777c478bd9Sstevel@tonic-gate 			mld_sendpkt(ilm, MLD_LISTENER_REPORT, NULL);
15787c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
15797c478bd9Sstevel@tonic-gate 			continue;
15807c478bd9Sstevel@tonic-gate 		}
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 		/*
15837c478bd9Sstevel@tonic-gate 		 * The retransmit timer has popped, and our router is
15847c478bd9Sstevel@tonic-gate 		 * MLDv2.  We have to delve into the retransmit state
15857c478bd9Sstevel@tonic-gate 		 * stored in the ilm.
15867c478bd9Sstevel@tonic-gate 		 *
15877c478bd9Sstevel@tonic-gate 		 * Decrement the retransmit count.  If the fmode rtx
15887c478bd9Sstevel@tonic-gate 		 * count is active, decrement it, and send a filter
15897c478bd9Sstevel@tonic-gate 		 * mode change report with the ilm's source list.
15907c478bd9Sstevel@tonic-gate 		 * Otherwise, send a source list change report with
15917c478bd9Sstevel@tonic-gate 		 * the current retransmit lists.
15927c478bd9Sstevel@tonic-gate 		 */
15937c478bd9Sstevel@tonic-gate 		ASSERT(rtxp->rtx_cnt > 0);
15947c478bd9Sstevel@tonic-gate 		ASSERT(rtxp->rtx_cnt >= rtxp->rtx_fmode_cnt);
15957c478bd9Sstevel@tonic-gate 		rtxp->rtx_cnt--;
15967c478bd9Sstevel@tonic-gate 		if (rtxp->rtx_fmode_cnt > 0) {
15977c478bd9Sstevel@tonic-gate 			rtxp->rtx_fmode_cnt--;
15987c478bd9Sstevel@tonic-gate 			rtype = (ilm->ilm_fmode == MODE_IS_INCLUDE) ?
15997c478bd9Sstevel@tonic-gate 			    CHANGE_TO_INCLUDE : CHANGE_TO_EXCLUDE;
16007c478bd9Sstevel@tonic-gate 			rtxrp = mcast_bldmrec(rtype, &ilm->ilm_v6addr,
16017c478bd9Sstevel@tonic-gate 			    ilm->ilm_filter, rtxrp);
16027c478bd9Sstevel@tonic-gate 		} else {
16037c478bd9Sstevel@tonic-gate 			rtxrp = mcast_bldmrec(ALLOW_NEW_SOURCES,
16047c478bd9Sstevel@tonic-gate 			    &ilm->ilm_v6addr, rtxp->rtx_allow, rtxrp);
16057c478bd9Sstevel@tonic-gate 			rtxrp = mcast_bldmrec(BLOCK_OLD_SOURCES,
16067c478bd9Sstevel@tonic-gate 			    &ilm->ilm_v6addr, rtxp->rtx_block, rtxrp);
16077c478bd9Sstevel@tonic-gate 		}
16087c478bd9Sstevel@tonic-gate 		if (rtxp->rtx_cnt > 0) {
16097c478bd9Sstevel@tonic-gate 			MCAST_RANDOM_DELAY(rtxp->rtx_timer,
16107c478bd9Sstevel@tonic-gate 			    SEC_TO_MSEC(ICMP6_MAX_HOST_REPORT_DELAY));
16117c478bd9Sstevel@tonic-gate 			if (rtxp->rtx_timer < next)
16127c478bd9Sstevel@tonic-gate 				next = rtxp->rtx_timer;
16137c478bd9Sstevel@tonic-gate 		} else {
16147c478bd9Sstevel@tonic-gate 			CLEAR_SLIST(rtxp->rtx_allow);
16157c478bd9Sstevel@tonic-gate 			CLEAR_SLIST(rtxp->rtx_block);
16167c478bd9Sstevel@tonic-gate 		}
16177c478bd9Sstevel@tonic-gate 	}
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	if (ill->ill_mcast_type == MLD_V2_ROUTER) {
16207c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
16217c478bd9Sstevel@tonic-gate 		mldv2_sendrpt(ill, rp);
16227c478bd9Sstevel@tonic-gate 		mldv2_sendrpt(ill, rtxrp);
16237c478bd9Sstevel@tonic-gate 		return (next);
16247c478bd9Sstevel@tonic-gate 	}
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	return (next);
16297c478bd9Sstevel@tonic-gate }
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate /*
16327c478bd9Sstevel@tonic-gate  * mld_timeout_handler:
16337c478bd9Sstevel@tonic-gate  * Called when there are timeout events, every next * TMEOUT_INTERVAL (tick).
16347c478bd9Sstevel@tonic-gate  * Returns number of ticks to next event (or 0 if none).
16357c478bd9Sstevel@tonic-gate  * MT issues are same as igmp_timeout_handler
16367c478bd9Sstevel@tonic-gate  */
16377c478bd9Sstevel@tonic-gate void
16387c478bd9Sstevel@tonic-gate mld_timeout_handler(void *arg)
16397c478bd9Sstevel@tonic-gate {
16407c478bd9Sstevel@tonic-gate 	ill_t	*ill;
16417c478bd9Sstevel@tonic-gate 	int	elapsed;	/* Since last call */
16427c478bd9Sstevel@tonic-gate 	uint_t  global_next = INFINITY;
16437c478bd9Sstevel@tonic-gate 	uint_t  next;
16447c478bd9Sstevel@tonic-gate 	ill_walk_context_t ctx;
16457c478bd9Sstevel@tonic-gate 	boolean_t success;
1646*f4b3ec61Sdh 	ip_stack_t *ipst = (ip_stack_t *)arg;
1647*f4b3ec61Sdh 
1648*f4b3ec61Sdh 	ASSERT(arg != NULL);
1649*f4b3ec61Sdh 	mutex_enter(&ipst->ips_mld_timer_lock);
1650*f4b3ec61Sdh 	ASSERT(ipst->ips_mld_timeout_id != 0);
1651*f4b3ec61Sdh 	ipst->ips_mld_timer_fired_last = ddi_get_lbolt();
1652*f4b3ec61Sdh 	elapsed = ipst->ips_mld_time_to_next;
1653*f4b3ec61Sdh 	ipst->ips_mld_time_to_next = 0;
1654*f4b3ec61Sdh 	mutex_exit(&ipst->ips_mld_timer_lock);
1655*f4b3ec61Sdh 
1656*f4b3ec61Sdh 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
1657*f4b3ec61Sdh 	ill = ILL_START_WALK_V6(&ctx, ipst);
16587c478bd9Sstevel@tonic-gate 	for (; ill != NULL; ill = ill_next(&ctx, ill)) {
16597c478bd9Sstevel@tonic-gate 		ASSERT(ill->ill_isv6);
16607c478bd9Sstevel@tonic-gate 		/*
16617c478bd9Sstevel@tonic-gate 		 * We may not be able to refhold the ill if the ill/ipif
16627c478bd9Sstevel@tonic-gate 		 * is changing. But we need to make sure that the ill will
16637c478bd9Sstevel@tonic-gate 		 * not vanish. So we just bump up the ill_waiter count.
16647c478bd9Sstevel@tonic-gate 		 */
16657c478bd9Sstevel@tonic-gate 		if (!ill_waiter_inc(ill))
16667c478bd9Sstevel@tonic-gate 			continue;
1667*f4b3ec61Sdh 		rw_exit(&ipst->ips_ill_g_lock);
16687c478bd9Sstevel@tonic-gate 		success = ipsq_enter(ill, B_TRUE);
16697c478bd9Sstevel@tonic-gate 		if (success) {
16707c478bd9Sstevel@tonic-gate 			next = mld_timeout_handler_per_ill(ill, elapsed);
16717c478bd9Sstevel@tonic-gate 			if (next < global_next)
16727c478bd9Sstevel@tonic-gate 				global_next = next;
16737c478bd9Sstevel@tonic-gate 			ipsq_exit(ill->ill_phyint->phyint_ipsq, B_TRUE,
16747c478bd9Sstevel@tonic-gate 			    B_FALSE);
16757c478bd9Sstevel@tonic-gate 		}
1676*f4b3ec61Sdh 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
16777c478bd9Sstevel@tonic-gate 		ill_waiter_dcr(ill);
16787c478bd9Sstevel@tonic-gate 	}
1679*f4b3ec61Sdh 	rw_exit(&ipst->ips_ill_g_lock);
16807c478bd9Sstevel@tonic-gate 
1681*f4b3ec61Sdh 	mutex_enter(&ipst->ips_mld_timer_lock);
1682*f4b3ec61Sdh 	ASSERT(ipst->ips_mld_timeout_id != 0);
1683*f4b3ec61Sdh 	ipst->ips_mld_timeout_id = 0;
1684*f4b3ec61Sdh 	mutex_exit(&ipst->ips_mld_timer_lock);
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	if (global_next != INFINITY)
1687*f4b3ec61Sdh 		mld_start_timers(global_next, ipst);
16887c478bd9Sstevel@tonic-gate }
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate /*
16917c478bd9Sstevel@tonic-gate  * Calculate the Older Version Querier Present timeout value, in number
16927c478bd9Sstevel@tonic-gate  * of slowtimo intervals, for the given ill.
16937c478bd9Sstevel@tonic-gate  */
16947c478bd9Sstevel@tonic-gate #define	OVQP(ill) \
16957c478bd9Sstevel@tonic-gate 	((1000 * (((ill)->ill_mcast_rv * (ill)->ill_mcast_qi) \
16967c478bd9Sstevel@tonic-gate 	+ MCAST_QUERY_RESP_INTERVAL)) / MCAST_SLOWTIMO_INTERVAL)
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate /*
16997c478bd9Sstevel@tonic-gate  * igmp_slowtimo:
17007c478bd9Sstevel@tonic-gate  * - Resets to new router if we didnt we hear from the router
17017c478bd9Sstevel@tonic-gate  *   in IGMP_AGE_THRESHOLD seconds.
17027c478bd9Sstevel@tonic-gate  * - Resets slowtimeout.
17037c478bd9Sstevel@tonic-gate  */
17047c478bd9Sstevel@tonic-gate void
17057c478bd9Sstevel@tonic-gate igmp_slowtimo(void *arg)
17067c478bd9Sstevel@tonic-gate {
17077c478bd9Sstevel@tonic-gate 	ill_t	*ill;
17087c478bd9Sstevel@tonic-gate 	ill_if_t *ifp;
17097c478bd9Sstevel@tonic-gate 	avl_tree_t *avl_tree;
1710*f4b3ec61Sdh 	ip_stack_t *ipst = (ip_stack_t *)arg;
17117c478bd9Sstevel@tonic-gate 
1712*f4b3ec61Sdh 	ASSERT(arg != NULL);
17137c478bd9Sstevel@tonic-gate 	/* Hold the ill_g_lock so that we can safely walk the ill list */
1714*f4b3ec61Sdh 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	/*
17177c478bd9Sstevel@tonic-gate 	 * The ill_if_t list is circular, hence the odd loop parameters.
17187c478bd9Sstevel@tonic-gate 	 *
17197c478bd9Sstevel@tonic-gate 	 * We can't use the ILL_START_WALK and ill_next() wrappers for this
17207c478bd9Sstevel@tonic-gate 	 * walk, as we need to check the illif_mcast_* fields in the ill_if_t
17217c478bd9Sstevel@tonic-gate 	 * structure (allowing us to skip if none of the instances have timers
17227c478bd9Sstevel@tonic-gate 	 * running).
17237c478bd9Sstevel@tonic-gate 	 */
1724*f4b3ec61Sdh 	for (ifp = IP_V4_ILL_G_LIST(ipst);
1725*f4b3ec61Sdh 	    ifp != (ill_if_t *)&IP_V4_ILL_G_LIST(ipst);
17267c478bd9Sstevel@tonic-gate 	    ifp = ifp->illif_next) {
17277c478bd9Sstevel@tonic-gate 		/*
17287c478bd9Sstevel@tonic-gate 		 * illif_mcast_v[12] are set using atomics. If an ill hears
17297c478bd9Sstevel@tonic-gate 		 * a V1 or V2 query now and we miss seeing the count now,
17307c478bd9Sstevel@tonic-gate 		 * we will see it the next time igmp_slowtimo is called.
17317c478bd9Sstevel@tonic-gate 		 */
17327c478bd9Sstevel@tonic-gate 		if (ifp->illif_mcast_v1 == 0 && ifp->illif_mcast_v2 == 0)
17337c478bd9Sstevel@tonic-gate 			continue;
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 		avl_tree = &ifp->illif_avl_by_ppa;
17367c478bd9Sstevel@tonic-gate 		for (ill = avl_first(avl_tree); ill != NULL;
17377c478bd9Sstevel@tonic-gate 		    ill = avl_walk(avl_tree, ill, AVL_AFTER)) {
17387c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
17397c478bd9Sstevel@tonic-gate 			if (ill->ill_mcast_v1_tset == 1)
17407c478bd9Sstevel@tonic-gate 				ill->ill_mcast_v1_time++;
17417c478bd9Sstevel@tonic-gate 			if (ill->ill_mcast_v2_tset == 1)
17427c478bd9Sstevel@tonic-gate 				ill->ill_mcast_v2_time++;
17437c478bd9Sstevel@tonic-gate 			if (ill->ill_mcast_type == IGMP_V1_ROUTER) {
17447c478bd9Sstevel@tonic-gate 				if (ill->ill_mcast_v1_time >= OVQP(ill)) {
17457c478bd9Sstevel@tonic-gate 					if (ill->ill_mcast_v2_tset > 0) {
17467c478bd9Sstevel@tonic-gate 						ip1dbg(("V1 query timer "
17477c478bd9Sstevel@tonic-gate 						    "expired on %s; switching "
17487c478bd9Sstevel@tonic-gate 						    "mode to IGMP_V2\n",
17497c478bd9Sstevel@tonic-gate 						    ill->ill_name));
17507c478bd9Sstevel@tonic-gate 						ill->ill_mcast_type =
17517c478bd9Sstevel@tonic-gate 						    IGMP_V2_ROUTER;
17527c478bd9Sstevel@tonic-gate 					} else {
17537c478bd9Sstevel@tonic-gate 						ip1dbg(("V1 query timer "
17547c478bd9Sstevel@tonic-gate 						    "expired on %s; switching "
17557c478bd9Sstevel@tonic-gate 						    "mode to IGMP_V3\n",
17567c478bd9Sstevel@tonic-gate 						    ill->ill_name));
17577c478bd9Sstevel@tonic-gate 						ill->ill_mcast_type =
17587c478bd9Sstevel@tonic-gate 						    IGMP_V3_ROUTER;
17597c478bd9Sstevel@tonic-gate 					}
17607c478bd9Sstevel@tonic-gate 					ill->ill_mcast_v1_time = 0;
17617c478bd9Sstevel@tonic-gate 					ill->ill_mcast_v1_tset = 0;
17627c478bd9Sstevel@tonic-gate 					atomic_add_16(&ifp->illif_mcast_v1, -1);
17637c478bd9Sstevel@tonic-gate 				}
17647c478bd9Sstevel@tonic-gate 			}
17657c478bd9Sstevel@tonic-gate 			if (ill->ill_mcast_type == IGMP_V2_ROUTER) {
17667c478bd9Sstevel@tonic-gate 				if (ill->ill_mcast_v2_time >= OVQP(ill)) {
17677c478bd9Sstevel@tonic-gate 					ip1dbg(("V2 query timer expired on "
17687c478bd9Sstevel@tonic-gate 					    "%s; switching mode to IGMP_V3\n",
17697c478bd9Sstevel@tonic-gate 					    ill->ill_name));
17707c478bd9Sstevel@tonic-gate 					ill->ill_mcast_type = IGMP_V3_ROUTER;
17717c478bd9Sstevel@tonic-gate 					ill->ill_mcast_v2_time = 0;
17727c478bd9Sstevel@tonic-gate 					ill->ill_mcast_v2_tset = 0;
17737c478bd9Sstevel@tonic-gate 					atomic_add_16(&ifp->illif_mcast_v2, -1);
17747c478bd9Sstevel@tonic-gate 				}
17757c478bd9Sstevel@tonic-gate 			}
17767c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
17777c478bd9Sstevel@tonic-gate 		}
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	}
1780*f4b3ec61Sdh 	rw_exit(&ipst->ips_ill_g_lock);
1781*f4b3ec61Sdh 	mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
1782*f4b3ec61Sdh 	ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo, (void *)ipst,
17837c478bd9Sstevel@tonic-gate 		MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
1784*f4b3ec61Sdh 	mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
17857c478bd9Sstevel@tonic-gate }
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate /*
17887c478bd9Sstevel@tonic-gate  * mld_slowtimo:
17897c478bd9Sstevel@tonic-gate  * - Resets to newer version if we didn't hear from the older version router
17907c478bd9Sstevel@tonic-gate  *   in MLD_AGE_THRESHOLD seconds.
17917c478bd9Sstevel@tonic-gate  * - Restarts slowtimeout.
17927c478bd9Sstevel@tonic-gate  */
17937c478bd9Sstevel@tonic-gate /* ARGSUSED */
17947c478bd9Sstevel@tonic-gate void
17957c478bd9Sstevel@tonic-gate mld_slowtimo(void *arg)
17967c478bd9Sstevel@tonic-gate {
17977c478bd9Sstevel@tonic-gate 	ill_t *ill;
17987c478bd9Sstevel@tonic-gate 	ill_if_t *ifp;
17997c478bd9Sstevel@tonic-gate 	avl_tree_t *avl_tree;
1800*f4b3ec61Sdh 	ip_stack_t *ipst = (ip_stack_t *)arg;
18017c478bd9Sstevel@tonic-gate 
1802*f4b3ec61Sdh 	ASSERT(arg != NULL);
18037c478bd9Sstevel@tonic-gate 	/* See comments in igmp_slowtimo() above... */
1804*f4b3ec61Sdh 	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
1805*f4b3ec61Sdh 	for (ifp = IP_V6_ILL_G_LIST(ipst);
1806*f4b3ec61Sdh 	    ifp != (ill_if_t *)&IP_V6_ILL_G_LIST(ipst);
18077c478bd9Sstevel@tonic-gate 	    ifp = ifp->illif_next) {
18087c478bd9Sstevel@tonic-gate 		if (ifp->illif_mcast_v1 == 0)
18097c478bd9Sstevel@tonic-gate 			continue;
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 		avl_tree = &ifp->illif_avl_by_ppa;
18127c478bd9Sstevel@tonic-gate 		for (ill = avl_first(avl_tree); ill != NULL;
18137c478bd9Sstevel@tonic-gate 		    ill = avl_walk(avl_tree, ill, AVL_AFTER)) {
18147c478bd9Sstevel@tonic-gate 			mutex_enter(&ill->ill_lock);
18157c478bd9Sstevel@tonic-gate 			if (ill->ill_mcast_v1_tset == 1)
18167c478bd9Sstevel@tonic-gate 				ill->ill_mcast_v1_time++;
18177c478bd9Sstevel@tonic-gate 			if (ill->ill_mcast_type == MLD_V1_ROUTER) {
18187c478bd9Sstevel@tonic-gate 				if (ill->ill_mcast_v1_time >= OVQP(ill)) {
18197c478bd9Sstevel@tonic-gate 					ip1dbg(("MLD query timer expired on"
18207c478bd9Sstevel@tonic-gate 					    " %s; switching mode to MLD_V2\n",
18217c478bd9Sstevel@tonic-gate 					    ill->ill_name));
18227c478bd9Sstevel@tonic-gate 					ill->ill_mcast_type = MLD_V2_ROUTER;
18237c478bd9Sstevel@tonic-gate 					ill->ill_mcast_v1_time = 0;
18247c478bd9Sstevel@tonic-gate 					ill->ill_mcast_v1_tset = 0;
18257c478bd9Sstevel@tonic-gate 					atomic_add_16(&ifp->illif_mcast_v1, -1);
18267c478bd9Sstevel@tonic-gate 				}
18277c478bd9Sstevel@tonic-gate 			}
18287c478bd9Sstevel@tonic-gate 			mutex_exit(&ill->ill_lock);
18297c478bd9Sstevel@tonic-gate 		}
18307c478bd9Sstevel@tonic-gate 	}
1831*f4b3ec61Sdh 	rw_exit(&ipst->ips_ill_g_lock);
1832*f4b3ec61Sdh 	mutex_enter(&ipst->ips_mld_slowtimeout_lock);
1833*f4b3ec61Sdh 	ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo, (void *)ipst,
18347c478bd9Sstevel@tonic-gate 	    MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
1835*f4b3ec61Sdh 	mutex_exit(&ipst->ips_mld_slowtimeout_lock);
18367c478bd9Sstevel@tonic-gate }
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate /*
18397c478bd9Sstevel@tonic-gate  * igmp_sendpkt:
18407c478bd9Sstevel@tonic-gate  * This will send to ip_wput like icmp_inbound.
18417c478bd9Sstevel@tonic-gate  * Note that the lower ill (on which the membership is kept) is used
18427c478bd9Sstevel@tonic-gate  * as an upper ill to pass in the multicast parameters.
18437c478bd9Sstevel@tonic-gate  */
18447c478bd9Sstevel@tonic-gate static void
18457c478bd9Sstevel@tonic-gate igmp_sendpkt(ilm_t *ilm, uchar_t type, ipaddr_t addr)
18467c478bd9Sstevel@tonic-gate {
18477c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
18487c478bd9Sstevel@tonic-gate 	igmpa_t	*igmpa;
18497c478bd9Sstevel@tonic-gate 	uint8_t *rtralert;
18507c478bd9Sstevel@tonic-gate 	ipha_t	*ipha;
18517c478bd9Sstevel@tonic-gate 	int	hdrlen = sizeof (ipha_t) + RTRALERT_LEN;
18527c478bd9Sstevel@tonic-gate 	size_t	size  = hdrlen + sizeof (igmpa_t);
18537c478bd9Sstevel@tonic-gate 	ipif_t 	*ipif = ilm->ilm_ipif;
18547c478bd9Sstevel@tonic-gate 	ill_t 	*ill  = ipif->ipif_ill;	/* Will be the "lower" ill */
18557c478bd9Sstevel@tonic-gate 	mblk_t	*first_mp;
18567c478bd9Sstevel@tonic-gate 	ipsec_out_t *io;
185745916cd2Sjpk 	zoneid_t zoneid;
1858*f4b3ec61Sdh 	ip_stack_t *ipst = ill->ill_ipst;
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	/*
18617c478bd9Sstevel@tonic-gate 	 * We need to make sure this packet goes out on an ipif. If
18627c478bd9Sstevel@tonic-gate 	 * there is some global policy match in ip_wput_ire, we need
18637c478bd9Sstevel@tonic-gate 	 * to get to the right interface after IPSEC processing.
18647c478bd9Sstevel@tonic-gate 	 * To make sure this multicast packet goes out on the right
18657c478bd9Sstevel@tonic-gate 	 * interface, we attach an ipsec_out and initialize ill_index
18667c478bd9Sstevel@tonic-gate 	 * like we did in ip_wput. To make sure that this packet does
18677c478bd9Sstevel@tonic-gate 	 * not get forwarded on other interfaces or looped back, we
18687c478bd9Sstevel@tonic-gate 	 * set ipsec_out_dontroute to B_TRUE and ipsec_out_multicast_loop
18697c478bd9Sstevel@tonic-gate 	 * to B_FALSE.
18707c478bd9Sstevel@tonic-gate 	 *
18717c478bd9Sstevel@tonic-gate 	 * We also need to make sure that this does not get load balanced
18727c478bd9Sstevel@tonic-gate 	 * if it hits ip_newroute_ipif. So, we initialize ipsec_out_attach_if
18737c478bd9Sstevel@tonic-gate 	 * here. If it gets load balanced, switches supporting igmp snooping
18747c478bd9Sstevel@tonic-gate 	 * will send the packet that it receives for this multicast group
18757c478bd9Sstevel@tonic-gate 	 * to the interface that we are sending on. As we have joined the
18767c478bd9Sstevel@tonic-gate 	 * multicast group on this ill, by sending the packet out on this
18777c478bd9Sstevel@tonic-gate 	 * ill, we receive all the packets back on this ill.
18787c478bd9Sstevel@tonic-gate 	 */
18797c478bd9Sstevel@tonic-gate 	first_mp = allocb(sizeof (ipsec_info_t), BPRI_HI);
18807c478bd9Sstevel@tonic-gate 	if (first_mp == NULL)
18817c478bd9Sstevel@tonic-gate 		return;
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 	first_mp->b_datap->db_type = M_CTL;
18847c478bd9Sstevel@tonic-gate 	first_mp->b_wptr += sizeof (ipsec_info_t);
18857c478bd9Sstevel@tonic-gate 	bzero(first_mp->b_rptr, sizeof (ipsec_info_t));
18867c478bd9Sstevel@tonic-gate 	/* ipsec_out_secure is B_FALSE now */
18877c478bd9Sstevel@tonic-gate 	io = (ipsec_out_t *)first_mp->b_rptr;
18887c478bd9Sstevel@tonic-gate 	io->ipsec_out_type = IPSEC_OUT;
18897c478bd9Sstevel@tonic-gate 	io->ipsec_out_len = sizeof (ipsec_out_t);
18907c478bd9Sstevel@tonic-gate 	io->ipsec_out_use_global_policy = B_TRUE;
18917c478bd9Sstevel@tonic-gate 	io->ipsec_out_ill_index = ill->ill_phyint->phyint_ifindex;
18927c478bd9Sstevel@tonic-gate 	io->ipsec_out_attach_if = B_TRUE;
18937c478bd9Sstevel@tonic-gate 	io->ipsec_out_multicast_loop = B_FALSE;
18947c478bd9Sstevel@tonic-gate 	io->ipsec_out_dontroute = B_TRUE;
189545916cd2Sjpk 	if ((zoneid = ilm->ilm_zoneid) == ALL_ZONES)
189645916cd2Sjpk 		zoneid = GLOBAL_ZONEID;
189745916cd2Sjpk 	io->ipsec_out_zoneid = zoneid;
1898*f4b3ec61Sdh 	io->ipsec_out_ns = ipst->ips_netstack;	/* No netstack_hold */
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 	mp = allocb(size, BPRI_HI);
19017c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
19027c478bd9Sstevel@tonic-gate 		freemsg(first_mp);
19037c478bd9Sstevel@tonic-gate 		return;
19047c478bd9Sstevel@tonic-gate 	}
19057c478bd9Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + size;
19067c478bd9Sstevel@tonic-gate 	first_mp->b_cont = mp;
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
19097c478bd9Sstevel@tonic-gate 	rtralert = (uint8_t *)&(ipha[1]);
19107c478bd9Sstevel@tonic-gate 	igmpa = (igmpa_t *)&(rtralert[RTRALERT_LEN]);
19117c478bd9Sstevel@tonic-gate 	igmpa->igmpa_type   = type;
19127c478bd9Sstevel@tonic-gate 	igmpa->igmpa_code   = 0;
19137c478bd9Sstevel@tonic-gate 	igmpa->igmpa_group  = ilm->ilm_addr;
19147c478bd9Sstevel@tonic-gate 	igmpa->igmpa_cksum  = 0;
19157c478bd9Sstevel@tonic-gate 	igmpa->igmpa_cksum  = IP_CSUM(mp, hdrlen, 0);
1916ff550d0eSmasputra 	if (igmpa->igmpa_cksum == 0)
1917ff550d0eSmasputra 		igmpa->igmpa_cksum = 0xffff;
19187c478bd9Sstevel@tonic-gate 
1919c0ad9723Srk 	rtralert[0] = IPOPT_COPY | IPOPT_RTRALERT;
19207c478bd9Sstevel@tonic-gate 	rtralert[1] = RTRALERT_LEN;
19217c478bd9Sstevel@tonic-gate 	rtralert[2] = 0;
19227c478bd9Sstevel@tonic-gate 	rtralert[3] = 0;
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	ipha->ipha_version_and_hdr_length = (IP_VERSION << 4)
19257c478bd9Sstevel@tonic-gate 	    | (IP_SIMPLE_HDR_LENGTH_IN_WORDS + RTRALERT_LEN_IN_WORDS);
19267c478bd9Sstevel@tonic-gate 	ipha->ipha_type_of_service 	= 0;
19277c478bd9Sstevel@tonic-gate 	ipha->ipha_length = htons(size);
19287c478bd9Sstevel@tonic-gate 	ipha->ipha_ident = 0;
19297c478bd9Sstevel@tonic-gate 	ipha->ipha_fragment_offset_and_flags = 0;
19307c478bd9Sstevel@tonic-gate 	ipha->ipha_ttl 		= IGMP_TTL;
19317c478bd9Sstevel@tonic-gate 	ipha->ipha_protocol 	= IPPROTO_IGMP;
19327c478bd9Sstevel@tonic-gate 	ipha->ipha_hdr_checksum 	= 0;
19337c478bd9Sstevel@tonic-gate 	ipha->ipha_dst 		= addr ? addr : igmpa->igmpa_group;
19347c478bd9Sstevel@tonic-gate 	ipha->ipha_src 		= ipif->ipif_src_addr;
19357c478bd9Sstevel@tonic-gate 	/*
19367c478bd9Sstevel@tonic-gate 	 * Request loopback of the report if we are acting as a multicast
19377c478bd9Sstevel@tonic-gate 	 * router, so that the process-level routing demon can hear it.
19387c478bd9Sstevel@tonic-gate 	 */
19397c478bd9Sstevel@tonic-gate 	/*
19407c478bd9Sstevel@tonic-gate 	 * This will run multiple times for the same group if there are members
19417c478bd9Sstevel@tonic-gate 	 * on the same group for multiple ipif's on the same ill. The
19427c478bd9Sstevel@tonic-gate 	 * igmp_input code will suppress this due to the loopback thus we
19437c478bd9Sstevel@tonic-gate 	 * always loopback membership report.
19447c478bd9Sstevel@tonic-gate 	 */
19457c478bd9Sstevel@tonic-gate 	ASSERT(ill->ill_rq != NULL);
19467c478bd9Sstevel@tonic-gate 	ip_multicast_loopback(ill->ill_rq, ill, first_mp, 0, ilm->ilm_zoneid);
19477c478bd9Sstevel@tonic-gate 
19485597b60aSnordmark 	ip_wput_multicast(ill->ill_wq, first_mp, ipif, zoneid);
19497c478bd9Sstevel@tonic-gate 
1950*f4b3ec61Sdh 	++ipst->ips_igmpstat.igps_snd_reports;
19517c478bd9Sstevel@tonic-gate }
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate /*
19547c478bd9Sstevel@tonic-gate  * Sends an IGMP_V3_MEMBERSHIP_REPORT message out the ill associated
19557c478bd9Sstevel@tonic-gate  * with the passed-in ipif.  The report will contain one group record
19567c478bd9Sstevel@tonic-gate  * for each element of reclist.  If this causes packet length to
19577c478bd9Sstevel@tonic-gate  * exceed ipif->ipif_ill->ill_max_frag, multiple reports are sent.
19587c478bd9Sstevel@tonic-gate  * reclist is assumed to be made up of buffers allocated by mcast_bldmrec(),
19597c478bd9Sstevel@tonic-gate  * and those buffers are freed here.
19607c478bd9Sstevel@tonic-gate  */
19617c478bd9Sstevel@tonic-gate static void
19627c478bd9Sstevel@tonic-gate igmpv3_sendrpt(ipif_t *ipif, mrec_t *reclist)
19637c478bd9Sstevel@tonic-gate {
19647c478bd9Sstevel@tonic-gate 	ipsec_out_t *io;
19657c478bd9Sstevel@tonic-gate 	igmp3ra_t *igmp3ra;
19667c478bd9Sstevel@tonic-gate 	grphdra_t *grphdr;
19677c478bd9Sstevel@tonic-gate 	mblk_t *first_mp, *mp;
19687c478bd9Sstevel@tonic-gate 	ipha_t *ipha;
19697c478bd9Sstevel@tonic-gate 	uint8_t *rtralert;
19707c478bd9Sstevel@tonic-gate 	ipaddr_t *src_array;
19717c478bd9Sstevel@tonic-gate 	int i, j, numrec, more_src_cnt;
19727c478bd9Sstevel@tonic-gate 	size_t hdrsize, size, rsize;
19737c478bd9Sstevel@tonic-gate 	ill_t *ill = ipif->ipif_ill;
19747c478bd9Sstevel@tonic-gate 	mrec_t *rp, *cur_reclist;
19757c478bd9Sstevel@tonic-gate 	mrec_t *next_reclist = reclist;
19767c478bd9Sstevel@tonic-gate 	boolean_t morepkts;
197745916cd2Sjpk 	zoneid_t zoneid;
1978*f4b3ec61Sdh 	ip_stack_t	 *ipst = ill->ill_ipst;
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 	/* if there aren't any records, there's nothing to send */
19817c478bd9Sstevel@tonic-gate 	if (reclist == NULL)
19827c478bd9Sstevel@tonic-gate 		return;
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	hdrsize = sizeof (ipha_t) + RTRALERT_LEN;
19857c478bd9Sstevel@tonic-gate nextpkt:
19867c478bd9Sstevel@tonic-gate 	size = hdrsize + sizeof (igmp3ra_t);
19877c478bd9Sstevel@tonic-gate 	morepkts = B_FALSE;
19887c478bd9Sstevel@tonic-gate 	more_src_cnt = 0;
19897c478bd9Sstevel@tonic-gate 	cur_reclist = next_reclist;
19907c478bd9Sstevel@tonic-gate 	numrec = 0;
19917c478bd9Sstevel@tonic-gate 	for (rp = cur_reclist; rp != NULL; rp = rp->mrec_next) {
19927c478bd9Sstevel@tonic-gate 		rsize = sizeof (grphdra_t) +
19937c478bd9Sstevel@tonic-gate 		    (rp->mrec_srcs.sl_numsrc * sizeof (ipaddr_t));
19947c478bd9Sstevel@tonic-gate 		if (size + rsize > ill->ill_max_frag) {
19957c478bd9Sstevel@tonic-gate 			if (rp == cur_reclist) {
19967c478bd9Sstevel@tonic-gate 				/*
19977c478bd9Sstevel@tonic-gate 				 * If the first mrec we looked at is too big
19987c478bd9Sstevel@tonic-gate 				 * to fit in a single packet (i.e the source
19997c478bd9Sstevel@tonic-gate 				 * list is too big), we must either truncate
20007c478bd9Sstevel@tonic-gate 				 * the list (if TO_EX or IS_EX), or send
20017c478bd9Sstevel@tonic-gate 				 * multiple reports for the same group (all
20027c478bd9Sstevel@tonic-gate 				 * other types).
20037c478bd9Sstevel@tonic-gate 				 */
20047c478bd9Sstevel@tonic-gate 				int srcspace, srcsperpkt;
20057c478bd9Sstevel@tonic-gate 				srcspace = ill->ill_max_frag - (size +
20067c478bd9Sstevel@tonic-gate 				    sizeof (grphdra_t));
20077c478bd9Sstevel@tonic-gate 				srcsperpkt = srcspace / sizeof (ipaddr_t);
20087c478bd9Sstevel@tonic-gate 				/*
20097c478bd9Sstevel@tonic-gate 				 * Increment size and numrec, because we will
20107c478bd9Sstevel@tonic-gate 				 * be sending a record for the mrec we're
20117c478bd9Sstevel@tonic-gate 				 * looking at now.
20127c478bd9Sstevel@tonic-gate 				 */
20137c478bd9Sstevel@tonic-gate 				size += sizeof (grphdra_t) +
20147c478bd9Sstevel@tonic-gate 				    (srcsperpkt * sizeof (ipaddr_t));
20157c478bd9Sstevel@tonic-gate 				numrec++;
20167c478bd9Sstevel@tonic-gate 				if (rp->mrec_type == MODE_IS_EXCLUDE ||
20177c478bd9Sstevel@tonic-gate 				    rp->mrec_type == CHANGE_TO_EXCLUDE) {
20187c478bd9Sstevel@tonic-gate 					rp->mrec_srcs.sl_numsrc = srcsperpkt;
20197c478bd9Sstevel@tonic-gate 					if (rp->mrec_next == NULL) {
20207c478bd9Sstevel@tonic-gate 						/* no more packets to send */
20217c478bd9Sstevel@tonic-gate 						break;
20227c478bd9Sstevel@tonic-gate 					} else {
20237c478bd9Sstevel@tonic-gate 						/*
20247c478bd9Sstevel@tonic-gate 						 * more packets, but we're
20257c478bd9Sstevel@tonic-gate 						 * done with this mrec.
20267c478bd9Sstevel@tonic-gate 						 */
20277c478bd9Sstevel@tonic-gate 						next_reclist = rp->mrec_next;
20287c478bd9Sstevel@tonic-gate 					}
20297c478bd9Sstevel@tonic-gate 				} else {
20307c478bd9Sstevel@tonic-gate 					more_src_cnt = rp->mrec_srcs.sl_numsrc
20317c478bd9Sstevel@tonic-gate 					    - srcsperpkt;
20327c478bd9Sstevel@tonic-gate 					rp->mrec_srcs.sl_numsrc = srcsperpkt;
20337c478bd9Sstevel@tonic-gate 					/*
20347c478bd9Sstevel@tonic-gate 					 * We'll fix up this mrec (remove the
20357c478bd9Sstevel@tonic-gate 					 * srcs we've already sent) before
20367c478bd9Sstevel@tonic-gate 					 * returning to nextpkt above.
20377c478bd9Sstevel@tonic-gate 					 */
20387c478bd9Sstevel@tonic-gate 					next_reclist = rp;
20397c478bd9Sstevel@tonic-gate 				}
20407c478bd9Sstevel@tonic-gate 			} else {
20417c478bd9Sstevel@tonic-gate 				next_reclist = rp;
20427c478bd9Sstevel@tonic-gate 			}
20437c478bd9Sstevel@tonic-gate 			morepkts = B_TRUE;
20447c478bd9Sstevel@tonic-gate 			break;
20457c478bd9Sstevel@tonic-gate 		}
20467c478bd9Sstevel@tonic-gate 		size += rsize;
20477c478bd9Sstevel@tonic-gate 		numrec++;
20487c478bd9Sstevel@tonic-gate 	}
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 	/*
20517c478bd9Sstevel@tonic-gate 	 * See comments in igmp_sendpkt() about initializing for ipsec and
20527c478bd9Sstevel@tonic-gate 	 * load balancing requirements.
20537c478bd9Sstevel@tonic-gate 	 */
20547c478bd9Sstevel@tonic-gate 	first_mp = allocb(sizeof (ipsec_info_t), BPRI_HI);
20557c478bd9Sstevel@tonic-gate 	if (first_mp == NULL)
20567c478bd9Sstevel@tonic-gate 		goto free_reclist;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	first_mp->b_datap->db_type = M_CTL;
20597c478bd9Sstevel@tonic-gate 	first_mp->b_wptr += sizeof (ipsec_info_t);
20607c478bd9Sstevel@tonic-gate 	bzero(first_mp->b_rptr, sizeof (ipsec_info_t));
20617c478bd9Sstevel@tonic-gate 	/* ipsec_out_secure is B_FALSE now */
20627c478bd9Sstevel@tonic-gate 	io = (ipsec_out_t *)first_mp->b_rptr;
20637c478bd9Sstevel@tonic-gate 	io->ipsec_out_type = IPSEC_OUT;
20647c478bd9Sstevel@tonic-gate 	io->ipsec_out_len = sizeof (ipsec_out_t);
20657c478bd9Sstevel@tonic-gate 	io->ipsec_out_use_global_policy = B_TRUE;
20667c478bd9Sstevel@tonic-gate 	io->ipsec_out_ill_index = ill->ill_phyint->phyint_ifindex;
20677c478bd9Sstevel@tonic-gate 	io->ipsec_out_attach_if = B_TRUE;
20687c478bd9Sstevel@tonic-gate 	io->ipsec_out_multicast_loop = B_FALSE;
20697c478bd9Sstevel@tonic-gate 	io->ipsec_out_dontroute = B_TRUE;
207045916cd2Sjpk 	if ((zoneid = ipif->ipif_zoneid) == ALL_ZONES)
207145916cd2Sjpk 		zoneid = GLOBAL_ZONEID;
207245916cd2Sjpk 	io->ipsec_out_zoneid = zoneid;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	mp = allocb(size, BPRI_HI);
20757c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
20767c478bd9Sstevel@tonic-gate 		freemsg(first_mp);
20777c478bd9Sstevel@tonic-gate 		goto free_reclist;
20787c478bd9Sstevel@tonic-gate 	}
20797c478bd9Sstevel@tonic-gate 	bzero((char *)mp->b_rptr, size);
20807c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(mp->b_rptr + size);
20817c478bd9Sstevel@tonic-gate 	first_mp->b_cont = mp;
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	ipha = (ipha_t *)mp->b_rptr;
20847c478bd9Sstevel@tonic-gate 	rtralert = (uint8_t *)&(ipha[1]);
20857c478bd9Sstevel@tonic-gate 	igmp3ra = (igmp3ra_t *)&(rtralert[RTRALERT_LEN]);
20867c478bd9Sstevel@tonic-gate 	grphdr = (grphdra_t *)&(igmp3ra[1]);
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	rp = cur_reclist;
20897c478bd9Sstevel@tonic-gate 	for (i = 0; i < numrec; i++) {
20907c478bd9Sstevel@tonic-gate 		grphdr->grphdra_type = rp->mrec_type;
20917c478bd9Sstevel@tonic-gate 		grphdr->grphdra_numsrc = htons(rp->mrec_srcs.sl_numsrc);
20927c478bd9Sstevel@tonic-gate 		grphdr->grphdra_group = V4_PART_OF_V6(rp->mrec_group);
20937c478bd9Sstevel@tonic-gate 		src_array = (ipaddr_t *)&(grphdr[1]);
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 		for (j = 0; j < rp->mrec_srcs.sl_numsrc; j++)
20967c478bd9Sstevel@tonic-gate 			src_array[j] = V4_PART_OF_V6(rp->mrec_srcs.sl_addr[j]);
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 		grphdr = (grphdra_t *)&(src_array[j]);
20997c478bd9Sstevel@tonic-gate 		rp = rp->mrec_next;
21007c478bd9Sstevel@tonic-gate 	}
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 	igmp3ra->igmp3ra_type = IGMP_V3_MEMBERSHIP_REPORT;
21037c478bd9Sstevel@tonic-gate 	igmp3ra->igmp3ra_numrec = htons(numrec);
21047c478bd9Sstevel@tonic-gate 	igmp3ra->igmp3ra_cksum = IP_CSUM(mp, hdrsize, 0);
21057c478bd9Sstevel@tonic-gate 
2106c0ad9723Srk 	rtralert[0] = IPOPT_COPY | IPOPT_RTRALERT;
21077c478bd9Sstevel@tonic-gate 	rtralert[1] = RTRALERT_LEN;
21087c478bd9Sstevel@tonic-gate 	rtralert[2] = 0;
21097c478bd9Sstevel@tonic-gate 	rtralert[3] = 0;
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 	ipha->ipha_version_and_hdr_length = IP_VERSION << 4
21127c478bd9Sstevel@tonic-gate 	    | (IP_SIMPLE_HDR_LENGTH_IN_WORDS + RTRALERT_LEN_IN_WORDS);
21137c478bd9Sstevel@tonic-gate 	ipha->ipha_type_of_service = IPTOS_PREC_INTERNETCONTROL;
21147c478bd9Sstevel@tonic-gate 	ipha->ipha_length = htons(size);
21157c478bd9Sstevel@tonic-gate 	ipha->ipha_ttl = IGMP_TTL;
21167c478bd9Sstevel@tonic-gate 	ipha->ipha_protocol = IPPROTO_IGMP;
21177c478bd9Sstevel@tonic-gate 	ipha->ipha_dst = htonl(INADDR_ALLRPTS_GROUP);
21187c478bd9Sstevel@tonic-gate 	ipha->ipha_src = ipif->ipif_src_addr;
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 	/*
21217c478bd9Sstevel@tonic-gate 	 * Request loopback of the report if we are acting as a multicast
21227c478bd9Sstevel@tonic-gate 	 * router, so that the process-level routing daemon can hear it.
21237c478bd9Sstevel@tonic-gate 	 *
21247c478bd9Sstevel@tonic-gate 	 * This will run multiple times for the same group if there are
21257c478bd9Sstevel@tonic-gate 	 * members on the same group for multiple ipifs on the same ill.
21267c478bd9Sstevel@tonic-gate 	 * The igmp_input code will suppress this due to the loopback;
21277c478bd9Sstevel@tonic-gate 	 * thus we always loopback membership report.
21287c478bd9Sstevel@tonic-gate 	 */
21297c478bd9Sstevel@tonic-gate 	ASSERT(ill->ill_rq != NULL);
21307c478bd9Sstevel@tonic-gate 	ip_multicast_loopback(ill->ill_rq, ill, mp, 0, ipif->ipif_zoneid);
21317c478bd9Sstevel@tonic-gate 
21325597b60aSnordmark 	ip_wput_multicast(ill->ill_wq, first_mp, ipif, zoneid);
21337c478bd9Sstevel@tonic-gate 
2134*f4b3ec61Sdh 	++ipst->ips_igmpstat.igps_snd_reports;
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 	if (morepkts) {
21377c478bd9Sstevel@tonic-gate 		if (more_src_cnt > 0) {
21387c478bd9Sstevel@tonic-gate 			int index, mvsize;
21397c478bd9Sstevel@tonic-gate 			slist_t *sl = &next_reclist->mrec_srcs;
21407c478bd9Sstevel@tonic-gate 			index = sl->sl_numsrc;
21417c478bd9Sstevel@tonic-gate 			mvsize = more_src_cnt * sizeof (in6_addr_t);
21427c478bd9Sstevel@tonic-gate 			(void) memmove(&sl->sl_addr[0], &sl->sl_addr[index],
21437c478bd9Sstevel@tonic-gate 			    mvsize);
21447c478bd9Sstevel@tonic-gate 			sl->sl_numsrc = more_src_cnt;
21457c478bd9Sstevel@tonic-gate 		}
21467c478bd9Sstevel@tonic-gate 		goto nextpkt;
21477c478bd9Sstevel@tonic-gate 	}
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate free_reclist:
21507c478bd9Sstevel@tonic-gate 	while (reclist != NULL) {
21517c478bd9Sstevel@tonic-gate 		rp = reclist->mrec_next;
21527c478bd9Sstevel@tonic-gate 		mi_free(reclist);
21537c478bd9Sstevel@tonic-gate 		reclist = rp;
21547c478bd9Sstevel@tonic-gate 	}
21557c478bd9Sstevel@tonic-gate }
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate /*
21587c478bd9Sstevel@tonic-gate  * mld_input:
21597c478bd9Sstevel@tonic-gate  */
21607c478bd9Sstevel@tonic-gate /* ARGSUSED */
21617c478bd9Sstevel@tonic-gate void
21627c478bd9Sstevel@tonic-gate mld_input(queue_t *q, mblk_t *mp, ill_t *ill)
21637c478bd9Sstevel@tonic-gate {
21647c478bd9Sstevel@tonic-gate 	ip6_t		*ip6h = (ip6_t *)(mp->b_rptr);
21657c478bd9Sstevel@tonic-gate 	mld_hdr_t	*mldh;
21667c478bd9Sstevel@tonic-gate 	ilm_t		*ilm;
21677c478bd9Sstevel@tonic-gate 	ipif_t		*ipif;
21687c478bd9Sstevel@tonic-gate 	uint16_t	hdr_length, exthdr_length;
21697c478bd9Sstevel@tonic-gate 	in6_addr_t	*v6group_ptr, *lcladdr_ptr;
21707c478bd9Sstevel@tonic-gate 	uint_t		next;
21717c478bd9Sstevel@tonic-gate 	int		mldlen;
2172*f4b3ec61Sdh 	ip_stack_t	*ipst = ill->ill_ipst;
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInGroupMembTotal);
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 	/* Make sure the src address of the packet is link-local */
21777c478bd9Sstevel@tonic-gate 	if (!(IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src))) {
21787c478bd9Sstevel@tonic-gate 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInErrors);
21797c478bd9Sstevel@tonic-gate 		freemsg(mp);
21807c478bd9Sstevel@tonic-gate 		return;
21817c478bd9Sstevel@tonic-gate 	}
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	if (ip6h->ip6_hlim != 1) {
21847c478bd9Sstevel@tonic-gate 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpBadHoplimit);
21857c478bd9Sstevel@tonic-gate 		freemsg(mp);
21867c478bd9Sstevel@tonic-gate 		return;
21877c478bd9Sstevel@tonic-gate 	}
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate 	/* Get to the icmp header part */
21907c478bd9Sstevel@tonic-gate 	if (ip6h->ip6_nxt != IPPROTO_ICMPV6) {
21917c478bd9Sstevel@tonic-gate 		hdr_length = ip_hdr_length_v6(mp, ip6h);
21927c478bd9Sstevel@tonic-gate 		exthdr_length = hdr_length - IPV6_HDR_LEN;
21937c478bd9Sstevel@tonic-gate 	} else {
21947c478bd9Sstevel@tonic-gate 		hdr_length = IPV6_HDR_LEN;
21957c478bd9Sstevel@tonic-gate 		exthdr_length = 0;
21967c478bd9Sstevel@tonic-gate 	}
21977c478bd9Sstevel@tonic-gate 	mldlen = ntohs(ip6h->ip6_plen) - exthdr_length;
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	/* An MLD packet must at least be 24 octets to be valid */
22007c478bd9Sstevel@tonic-gate 	if (mldlen < MLD_MINLEN) {
22017c478bd9Sstevel@tonic-gate 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInErrors);
22027c478bd9Sstevel@tonic-gate 		freemsg(mp);
22037c478bd9Sstevel@tonic-gate 		return;
22047c478bd9Sstevel@tonic-gate 	}
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 	mldh = (mld_hdr_t *)(&mp->b_rptr[hdr_length]);
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	switch (mldh->mld_type) {
22097c478bd9Sstevel@tonic-gate 	case MLD_LISTENER_QUERY:
22107c478bd9Sstevel@tonic-gate 		/*
22117c478bd9Sstevel@tonic-gate 		 * packet length differentiates between v1 and v2.  v1
22127c478bd9Sstevel@tonic-gate 		 * query should be exactly 24 octets long; v2 is >= 28.
22137c478bd9Sstevel@tonic-gate 		 */
22147c478bd9Sstevel@tonic-gate 		if (mldlen == MLD_MINLEN) {
22157c478bd9Sstevel@tonic-gate 			next = mld_query_in(mldh, ill);
22167c478bd9Sstevel@tonic-gate 		} else if (mldlen >= MLD_V2_QUERY_MINLEN) {
22177c478bd9Sstevel@tonic-gate 			next = mldv2_query_in((mld2q_t *)mldh, ill, mldlen);
22187c478bd9Sstevel@tonic-gate 		} else {
22197c478bd9Sstevel@tonic-gate 			BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInErrors);
22207c478bd9Sstevel@tonic-gate 			freemsg(mp);
22217c478bd9Sstevel@tonic-gate 			return;
22227c478bd9Sstevel@tonic-gate 		}
22237c478bd9Sstevel@tonic-gate 		if (next == 0) {
22247c478bd9Sstevel@tonic-gate 			freemsg(mp);
22257c478bd9Sstevel@tonic-gate 			return;
22267c478bd9Sstevel@tonic-gate 		}
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 		if (next != INFINITY)
2229*f4b3ec61Sdh 			mld_start_timers(next, ipst);
22307c478bd9Sstevel@tonic-gate 		break;
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 	case MLD_LISTENER_REPORT: {
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate 		ASSERT(ill->ill_ipif != NULL);
22357c478bd9Sstevel@tonic-gate 		/*
22367c478bd9Sstevel@tonic-gate 		 * For fast leave to work, we have to know that we are the
22377c478bd9Sstevel@tonic-gate 		 * last person to send a report for this group.  Reports
22387c478bd9Sstevel@tonic-gate 		 * generated by us are looped back since we could potentially
22397c478bd9Sstevel@tonic-gate 		 * be a multicast router, so discard reports sourced by me.
22407c478bd9Sstevel@tonic-gate 		 */
22417c478bd9Sstevel@tonic-gate 		lcladdr_ptr = &(ill->ill_ipif->ipif_v6subnet);
22427c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
22437c478bd9Sstevel@tonic-gate 		for (ipif = ill->ill_ipif; ipif != NULL;
22447c478bd9Sstevel@tonic-gate 		    ipif = ipif->ipif_next) {
22457c478bd9Sstevel@tonic-gate 			if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
22467c478bd9Sstevel@tonic-gate 			    lcladdr_ptr)) {
22477c478bd9Sstevel@tonic-gate 				if (ip_debug > 1) {
22487c478bd9Sstevel@tonic-gate 					char    buf1[INET6_ADDRSTRLEN];
22497c478bd9Sstevel@tonic-gate 					char	buf2[INET6_ADDRSTRLEN];
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 					(void) mi_strlog(ill->ill_rq,
22527c478bd9Sstevel@tonic-gate 					    1,
22537c478bd9Sstevel@tonic-gate 					    SL_TRACE,
22547c478bd9Sstevel@tonic-gate 					    "mld_input: we are only "
22557c478bd9Sstevel@tonic-gate 					    "member src %s ipif_local %s",
22567c478bd9Sstevel@tonic-gate 					    inet_ntop(AF_INET6, lcladdr_ptr,
22577c478bd9Sstevel@tonic-gate 					    buf1, sizeof (buf1)),
22587c478bd9Sstevel@tonic-gate 					    inet_ntop(AF_INET6,
22597c478bd9Sstevel@tonic-gate 					    &ipif->ipif_v6lcl_addr,
22607c478bd9Sstevel@tonic-gate 					    buf2, sizeof (buf2)));
22617c478bd9Sstevel@tonic-gate 				}
22627c478bd9Sstevel@tonic-gate 				mutex_exit(&ill->ill_lock);
22637c478bd9Sstevel@tonic-gate 				freemsg(mp);
22647c478bd9Sstevel@tonic-gate 				return;
22657c478bd9Sstevel@tonic-gate 			}
22667c478bd9Sstevel@tonic-gate 		}
22677c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
22687c478bd9Sstevel@tonic-gate 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInGroupMembResponses);
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 		v6group_ptr = &mldh->mld_addr;
22717c478bd9Sstevel@tonic-gate 		if (!IN6_IS_ADDR_MULTICAST(v6group_ptr)) {
22727c478bd9Sstevel@tonic-gate 			BUMP_MIB(ill->ill_icmp6_mib,
22737c478bd9Sstevel@tonic-gate 			    ipv6IfIcmpInGroupMembBadReports);
22747c478bd9Sstevel@tonic-gate 			freemsg(mp);
22757c478bd9Sstevel@tonic-gate 			return;
22767c478bd9Sstevel@tonic-gate 		}
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 		/*
22807c478bd9Sstevel@tonic-gate 		 * If we belong to the group being reported, and we are a
22817c478bd9Sstevel@tonic-gate 		 * 'Delaying member' per the RFC terminology, stop our timer
22827c478bd9Sstevel@tonic-gate 		 * for that group and 'clear flag' i.e. mark ilm_state as
22837c478bd9Sstevel@tonic-gate 		 * IGMP_OTHERMEMBER. With zones, there can be multiple group
22847c478bd9Sstevel@tonic-gate 		 * membership entries for the same group address (one per zone)
22857c478bd9Sstevel@tonic-gate 		 * so we need to walk the ill_ilm list.
22867c478bd9Sstevel@tonic-gate 		 */
22877c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
22887c478bd9Sstevel@tonic-gate 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
22897c478bd9Sstevel@tonic-gate 			if (!IN6_ARE_ADDR_EQUAL(&ilm->ilm_v6addr, v6group_ptr))
22907c478bd9Sstevel@tonic-gate 			    continue;
22917c478bd9Sstevel@tonic-gate 			BUMP_MIB(ill->ill_icmp6_mib,
22927c478bd9Sstevel@tonic-gate 			    ipv6IfIcmpInGroupMembOurReports);
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 			ilm->ilm_timer = INFINITY;
22957c478bd9Sstevel@tonic-gate 			ilm->ilm_state = IGMP_OTHERMEMBER;
22967c478bd9Sstevel@tonic-gate 		}
22977c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
22987c478bd9Sstevel@tonic-gate 		break;
22997c478bd9Sstevel@tonic-gate 	}
23007c478bd9Sstevel@tonic-gate 	case MLD_LISTENER_REDUCTION:
23017c478bd9Sstevel@tonic-gate 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInGroupMembReductions);
23027c478bd9Sstevel@tonic-gate 		break;
23037c478bd9Sstevel@tonic-gate 	}
23047c478bd9Sstevel@tonic-gate 	/*
23057c478bd9Sstevel@tonic-gate 	 * All MLD packets have already been passed up to any
23067c478bd9Sstevel@tonic-gate 	 * process(es) listening on a ICMP6 raw socket. This
23077c478bd9Sstevel@tonic-gate 	 * has been accomplished in ip_deliver_local_v6 prior to
23087c478bd9Sstevel@tonic-gate 	 * this function call. It is assumed that the multicast daemon
23097c478bd9Sstevel@tonic-gate 	 * will have a SOCK_RAW IPPROTO_ICMPV6 (and presumbly use the
23107c478bd9Sstevel@tonic-gate 	 * ICMP6_FILTER socket option to only receive the MLD messages)
23117c478bd9Sstevel@tonic-gate 	 * Thus we can free the MLD message block here
23127c478bd9Sstevel@tonic-gate 	 */
23137c478bd9Sstevel@tonic-gate 	freemsg(mp);
23147c478bd9Sstevel@tonic-gate }
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate /*
23177c478bd9Sstevel@tonic-gate  * Handles an MLDv1 Listener Query.  Returns 0 on error, or the appropriate
23187c478bd9Sstevel@tonic-gate  * (non-zero, unsigned) timer value to be set on success.
23197c478bd9Sstevel@tonic-gate  */
23207c478bd9Sstevel@tonic-gate static uint_t
23217c478bd9Sstevel@tonic-gate mld_query_in(mld_hdr_t *mldh, ill_t *ill)
23227c478bd9Sstevel@tonic-gate {
23237c478bd9Sstevel@tonic-gate 	ilm_t	*ilm;
23247c478bd9Sstevel@tonic-gate 	int	timer;
23257c478bd9Sstevel@tonic-gate 	uint_t	next;
23267c478bd9Sstevel@tonic-gate 	in6_addr_t *v6group;
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate 	BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInGroupMembQueries);
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 	/*
23317c478bd9Sstevel@tonic-gate 	 * In the MLD specification, there are 3 states and a flag.
23327c478bd9Sstevel@tonic-gate 	 *
23337c478bd9Sstevel@tonic-gate 	 * In Non-Listener state, we simply don't have a membership record.
23347c478bd9Sstevel@tonic-gate 	 * In Delaying state, our timer is running (ilm->ilm_timer < INFINITY)
23357c478bd9Sstevel@tonic-gate 	 * In Idle Member state, our timer is not running (ilm->ilm_timer ==
23367c478bd9Sstevel@tonic-gate 	 * INFINITY)
23377c478bd9Sstevel@tonic-gate 	 *
23387c478bd9Sstevel@tonic-gate 	 * The flag is ilm->ilm_state, it is set to IGMP_OTHERMEMBER if
23397c478bd9Sstevel@tonic-gate 	 * we have heard a report from another member, or IGMP_IREPORTEDLAST
23407c478bd9Sstevel@tonic-gate 	 * if I sent the last report.
23417c478bd9Sstevel@tonic-gate 	 */
23427c478bd9Sstevel@tonic-gate 	v6group = &mldh->mld_addr;
23437c478bd9Sstevel@tonic-gate 	if (!(IN6_IS_ADDR_UNSPECIFIED(v6group)) &&
23447c478bd9Sstevel@tonic-gate 	    ((!IN6_IS_ADDR_MULTICAST(v6group)))) {
23457c478bd9Sstevel@tonic-gate 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInGroupMembBadQueries);
23467c478bd9Sstevel@tonic-gate 		return (0);
23477c478bd9Sstevel@tonic-gate 	}
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 	/* Need to do compatibility mode checking */
23507c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
23517c478bd9Sstevel@tonic-gate 	ill->ill_mcast_v1_time = 0;
23527c478bd9Sstevel@tonic-gate 	ill->ill_mcast_v1_tset = 1;
23537c478bd9Sstevel@tonic-gate 	if (ill->ill_mcast_type == MLD_V2_ROUTER) {
23547c478bd9Sstevel@tonic-gate 		ip1dbg(("Received MLDv1 Query on %s, switching mode to "
23557c478bd9Sstevel@tonic-gate 		    "MLD_V1_ROUTER\n", ill->ill_name));
23567c478bd9Sstevel@tonic-gate 		atomic_add_16(&ill->ill_ifptr->illif_mcast_v1, 1);
23577c478bd9Sstevel@tonic-gate 		ill->ill_mcast_type = MLD_V1_ROUTER;
23587c478bd9Sstevel@tonic-gate 	}
23597c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate 	timer = (int)ntohs(mldh->mld_maxdelay);
23627c478bd9Sstevel@tonic-gate 	if (ip_debug > 1) {
23637c478bd9Sstevel@tonic-gate 		(void) mi_strlog(ill->ill_rq, 1, SL_TRACE,
23647c478bd9Sstevel@tonic-gate 		    "mld_input: TIMER = mld_maxdelay %d mld_type 0x%x",
23657c478bd9Sstevel@tonic-gate 		    timer, (int)mldh->mld_type);
23667c478bd9Sstevel@tonic-gate 	}
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 	/*
23697c478bd9Sstevel@tonic-gate 	 * -Start the timers in all of our membership records for
23707c478bd9Sstevel@tonic-gate 	 * the physical interface on which the query arrived,
23717c478bd9Sstevel@tonic-gate 	 * excl:
23727c478bd9Sstevel@tonic-gate 	 *	1.  those that belong to the "all hosts" group,
23737c478bd9Sstevel@tonic-gate 	 *	2.  those with 0 scope, or 1 node-local scope.
23747c478bd9Sstevel@tonic-gate 	 *
23757c478bd9Sstevel@tonic-gate 	 * -Restart any timer that is already running but has a value
23767c478bd9Sstevel@tonic-gate 	 * longer that the requested timeout.
23777c478bd9Sstevel@tonic-gate 	 * -Use the value specified in the query message as the
23787c478bd9Sstevel@tonic-gate 	 * maximum timeout.
23797c478bd9Sstevel@tonic-gate 	 */
23807c478bd9Sstevel@tonic-gate 	next = INFINITY;
23817c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
23827c478bd9Sstevel@tonic-gate 	for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
23837c478bd9Sstevel@tonic-gate 		ASSERT(!IN6_IS_ADDR_V4MAPPED(&ilm->ilm_v6addr));
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr) ||
23867c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_MC_NODELOCAL(&ilm->ilm_v6addr) ||
23877c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_MC_RESERVED(&ilm->ilm_v6addr))
23887c478bd9Sstevel@tonic-gate 			continue;
23897c478bd9Sstevel@tonic-gate 		if ((!IN6_ARE_ADDR_EQUAL(&ilm->ilm_v6addr,
23907c478bd9Sstevel@tonic-gate 		    &ipv6_all_hosts_mcast)) &&
23917c478bd9Sstevel@tonic-gate 		    (IN6_IS_ADDR_UNSPECIFIED(v6group)) ||
23927c478bd9Sstevel@tonic-gate 		    (IN6_ARE_ADDR_EQUAL(v6group, &ilm->ilm_v6addr))) {
23937c478bd9Sstevel@tonic-gate 			if (timer == 0) {
23947c478bd9Sstevel@tonic-gate 				/* Respond immediately */
23957c478bd9Sstevel@tonic-gate 				ilm->ilm_timer = INFINITY;
23967c478bd9Sstevel@tonic-gate 				ilm->ilm_state = IGMP_IREPORTEDLAST;
23977c478bd9Sstevel@tonic-gate 				mutex_exit(&ill->ill_lock);
23987c478bd9Sstevel@tonic-gate 				mld_sendpkt(ilm, MLD_LISTENER_REPORT, NULL);
23997c478bd9Sstevel@tonic-gate 				mutex_enter(&ill->ill_lock);
24007c478bd9Sstevel@tonic-gate 				break;
24017c478bd9Sstevel@tonic-gate 			}
24027c478bd9Sstevel@tonic-gate 			if (ilm->ilm_timer > timer) {
24037c478bd9Sstevel@tonic-gate 				MCAST_RANDOM_DELAY(ilm->ilm_timer, timer);
24047c478bd9Sstevel@tonic-gate 				if (ilm->ilm_timer < next)
24057c478bd9Sstevel@tonic-gate 					next = ilm->ilm_timer;
24067c478bd9Sstevel@tonic-gate 			}
24077c478bd9Sstevel@tonic-gate 			break;
24087c478bd9Sstevel@tonic-gate 		}
24097c478bd9Sstevel@tonic-gate 	}
24107c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 	return (next);
24137c478bd9Sstevel@tonic-gate }
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate /*
24167c478bd9Sstevel@tonic-gate  * Handles an MLDv2 Listener Query.  On error, returns 0; on success,
24177c478bd9Sstevel@tonic-gate  * returns the appropriate (non-zero, unsigned) timer value (which may
24187c478bd9Sstevel@tonic-gate  * be INFINITY) to be set.
24197c478bd9Sstevel@tonic-gate  */
24207c478bd9Sstevel@tonic-gate static uint_t
24217c478bd9Sstevel@tonic-gate mldv2_query_in(mld2q_t *mld2q, ill_t *ill, int mldlen)
24227c478bd9Sstevel@tonic-gate {
24237c478bd9Sstevel@tonic-gate 	ilm_t	*ilm;
24247c478bd9Sstevel@tonic-gate 	in6_addr_t *v6group, *src_array;
24257c478bd9Sstevel@tonic-gate 	uint_t	next, numsrc, i, mrd, delay, qqi;
24267c478bd9Sstevel@tonic-gate 	uint8_t	qrv;
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate 	v6group = &mld2q->mld2q_addr;
24297c478bd9Sstevel@tonic-gate 	numsrc = ntohs(mld2q->mld2q_numsrc);
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	/* make sure numsrc matches packet size */
24327c478bd9Sstevel@tonic-gate 	if (mldlen < MLD_V2_QUERY_MINLEN + (numsrc * sizeof (in6_addr_t))) {
24337c478bd9Sstevel@tonic-gate 		BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInErrors);
24347c478bd9Sstevel@tonic-gate 		return (0);
24357c478bd9Sstevel@tonic-gate 	}
24367c478bd9Sstevel@tonic-gate 	src_array = (in6_addr_t *)&mld2q[1];
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 	BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInGroupMembQueries);
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 	/* extract Maximum Response Delay from code in header */
24417c478bd9Sstevel@tonic-gate 	mrd = ntohs(mld2q->mld2q_mxrc);
24427c478bd9Sstevel@tonic-gate 	if (mrd >= MLD_V2_MAXRT_FPMIN) {
24437c478bd9Sstevel@tonic-gate 		uint_t hdrval, mant, exp;
24447c478bd9Sstevel@tonic-gate 		hdrval = mrd;
24457c478bd9Sstevel@tonic-gate 		mant = hdrval & MLD_V2_MAXRT_MANT_MASK;
24467c478bd9Sstevel@tonic-gate 		exp = (hdrval & MLD_V2_MAXRT_EXP_MASK) >> 12;
24477c478bd9Sstevel@tonic-gate 		mrd = (mant | 0x1000) << (exp + 3);
24487c478bd9Sstevel@tonic-gate 	}
24497c478bd9Sstevel@tonic-gate 	MCAST_RANDOM_DELAY(delay, mrd);
24507c478bd9Sstevel@tonic-gate 	next = (unsigned)INFINITY;
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 	if ((qrv = mld2q->mld2q_sqrv & MLD_V2_RV_MASK) == 0)
24537c478bd9Sstevel@tonic-gate 		ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
24547c478bd9Sstevel@tonic-gate 	else
24557c478bd9Sstevel@tonic-gate 		ill->ill_mcast_rv = qrv;
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate 	if ((qqi = (uint_t)mld2q->mld2q_qqic) >= MLD_V2_QQI_FPMIN) {
24587c478bd9Sstevel@tonic-gate 		uint_t mant, exp;
24597c478bd9Sstevel@tonic-gate 		mant = qqi & MLD_V2_QQI_MANT_MASK;
24607c478bd9Sstevel@tonic-gate 		exp = (qqi & MLD_V2_QQI_EXP_MASK) >> 12;
24617c478bd9Sstevel@tonic-gate 		qqi = (mant | 0x10) << (exp + 3);
24627c478bd9Sstevel@tonic-gate 	}
24637c478bd9Sstevel@tonic-gate 	ill->ill_mcast_qi = (qqi == 0) ? MCAST_DEF_QUERY_INTERVAL : qqi;
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	/*
24667c478bd9Sstevel@tonic-gate 	 * If we have a pending general query response that's scheduled
24677c478bd9Sstevel@tonic-gate 	 * sooner than the delay we calculated for this response, then
24687c478bd9Sstevel@tonic-gate 	 * no action is required (MLDv2 draft section 6.2 rule 1)
24697c478bd9Sstevel@tonic-gate 	 */
24707c478bd9Sstevel@tonic-gate 	mutex_enter(&ill->ill_lock);
24717c478bd9Sstevel@tonic-gate 	if (ill->ill_global_timer < delay) {
24727c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
24737c478bd9Sstevel@tonic-gate 		return (next);
24747c478bd9Sstevel@tonic-gate 	}
24757c478bd9Sstevel@tonic-gate 	mutex_exit(&ill->ill_lock);
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	/*
24787c478bd9Sstevel@tonic-gate 	 * Now take action depending on query type: general,
24797c478bd9Sstevel@tonic-gate 	 * group specific, or group/source specific.
24807c478bd9Sstevel@tonic-gate 	 */
24817c478bd9Sstevel@tonic-gate 	if ((numsrc == 0) && IN6_IS_ADDR_UNSPECIFIED(v6group)) {
24827c478bd9Sstevel@tonic-gate 		/*
24837c478bd9Sstevel@tonic-gate 		 * general query
24847c478bd9Sstevel@tonic-gate 		 * We know global timer is either not running or is
24857c478bd9Sstevel@tonic-gate 		 * greater than our calculated delay, so reset it to
24867c478bd9Sstevel@tonic-gate 		 * our delay (random value in range [0, response time])
24877c478bd9Sstevel@tonic-gate 		 */
24887c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
24897c478bd9Sstevel@tonic-gate 		ill->ill_global_timer = delay;
24907c478bd9Sstevel@tonic-gate 		next = ill->ill_global_timer;
24917c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
24927c478bd9Sstevel@tonic-gate 
24937c478bd9Sstevel@tonic-gate 	} else {
24947c478bd9Sstevel@tonic-gate 		/* group or group/source specific query */
24957c478bd9Sstevel@tonic-gate 		mutex_enter(&ill->ill_lock);
24967c478bd9Sstevel@tonic-gate 		for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) {
24977c478bd9Sstevel@tonic-gate 			if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr) ||
24987c478bd9Sstevel@tonic-gate 			    IN6_IS_ADDR_MC_NODELOCAL(&ilm->ilm_v6addr) ||
24997c478bd9Sstevel@tonic-gate 			    IN6_IS_ADDR_MC_RESERVED(&ilm->ilm_v6addr) ||
25007c478bd9Sstevel@tonic-gate 			    !IN6_ARE_ADDR_EQUAL(v6group, &ilm->ilm_v6addr))
25017c478bd9Sstevel@tonic-gate 				continue;
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate 			/*
25047c478bd9Sstevel@tonic-gate 			 * If the query is group specific or we have a
25057c478bd9Sstevel@tonic-gate 			 * pending group specific query, the response is
25067c478bd9Sstevel@tonic-gate 			 * group specific (pending sources list should be
25077c478bd9Sstevel@tonic-gate 			 * empty).  Otherwise, need to update the pending
25087c478bd9Sstevel@tonic-gate 			 * sources list for the group and source specific
25097c478bd9Sstevel@tonic-gate 			 * response.
25107c478bd9Sstevel@tonic-gate 			 */
25117c478bd9Sstevel@tonic-gate 			if (numsrc == 0 || (ilm->ilm_timer < INFINITY &&
25127c478bd9Sstevel@tonic-gate 			    SLIST_IS_EMPTY(ilm->ilm_pendsrcs))) {
25137c478bd9Sstevel@tonic-gate group_query:
25147c478bd9Sstevel@tonic-gate 				FREE_SLIST(ilm->ilm_pendsrcs);
25157c478bd9Sstevel@tonic-gate 				ilm->ilm_pendsrcs = NULL;
25167c478bd9Sstevel@tonic-gate 			} else {
25177c478bd9Sstevel@tonic-gate 				boolean_t overflow;
25187c478bd9Sstevel@tonic-gate 				slist_t *pktl;
25197c478bd9Sstevel@tonic-gate 				if (numsrc > MAX_FILTER_SIZE ||
25207c478bd9Sstevel@tonic-gate 				    (ilm->ilm_pendsrcs == NULL &&
25217c478bd9Sstevel@tonic-gate 				    (ilm->ilm_pendsrcs = l_alloc()) == NULL)) {
25227c478bd9Sstevel@tonic-gate 					/*
25237c478bd9Sstevel@tonic-gate 					 * We've been sent more sources than
25247c478bd9Sstevel@tonic-gate 					 * we can deal with; or we can't deal
25257c478bd9Sstevel@tonic-gate 					 * with a source list at all. Revert
25267c478bd9Sstevel@tonic-gate 					 * to a group specific query.
25277c478bd9Sstevel@tonic-gate 					 */
25287c478bd9Sstevel@tonic-gate 					goto group_query;
25297c478bd9Sstevel@tonic-gate 				}
25307c478bd9Sstevel@tonic-gate 				if ((pktl = l_alloc()) == NULL)
25317c478bd9Sstevel@tonic-gate 					goto group_query;
25327c478bd9Sstevel@tonic-gate 				pktl->sl_numsrc = numsrc;
25337c478bd9Sstevel@tonic-gate 				for (i = 0; i < numsrc; i++)
25347c478bd9Sstevel@tonic-gate 					pktl->sl_addr[i] = src_array[i];
25357c478bd9Sstevel@tonic-gate 				l_union_in_a(ilm->ilm_pendsrcs, pktl,
25367c478bd9Sstevel@tonic-gate 				    &overflow);
25377c478bd9Sstevel@tonic-gate 				l_free(pktl);
25387c478bd9Sstevel@tonic-gate 				if (overflow)
25397c478bd9Sstevel@tonic-gate 					goto group_query;
25407c478bd9Sstevel@tonic-gate 			}
25417c478bd9Sstevel@tonic-gate 			/* set timer to soonest value */
25427c478bd9Sstevel@tonic-gate 			ilm->ilm_timer = MIN(ilm->ilm_timer, delay);
25437c478bd9Sstevel@tonic-gate 			if (ilm->ilm_timer < next)
25447c478bd9Sstevel@tonic-gate 				next = ilm->ilm_timer;
25457c478bd9Sstevel@tonic-gate 			break;
25467c478bd9Sstevel@tonic-gate 		}
25477c478bd9Sstevel@tonic-gate 		mutex_exit(&ill->ill_lock);
25487c478bd9Sstevel@tonic-gate 	}
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate 	return (next);
25517c478bd9Sstevel@tonic-gate }
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate /*
25547c478bd9Sstevel@tonic-gate  * Send MLDv1 response packet with hoplimit 1
25557c478bd9Sstevel@tonic-gate  */
25567c478bd9Sstevel@tonic-gate static void
25577c478bd9Sstevel@tonic-gate mld_sendpkt(ilm_t *ilm, uchar_t type, const in6_addr_t *v6addr)
25587c478bd9Sstevel@tonic-gate {
25597c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
25607c478bd9Sstevel@tonic-gate 	mld_hdr_t	*mldh;
25617c478bd9Sstevel@tonic-gate 	ip6_t 		*ip6h;
25627c478bd9Sstevel@tonic-gate 	ip6_hbh_t	*ip6hbh;
25637c478bd9Sstevel@tonic-gate 	struct ip6_opt_router	*ip6router;
25647c478bd9Sstevel@tonic-gate 	size_t		size = IPV6_HDR_LEN + sizeof (mld_hdr_t);
25657c478bd9Sstevel@tonic-gate 	ill_t		*ill = ilm->ilm_ill;   /* Will be the "lower" ill */
25667c478bd9Sstevel@tonic-gate 	ipif_t		*ipif;
25677c478bd9Sstevel@tonic-gate 	ip6i_t		*ip6i;
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate 	/*
25707c478bd9Sstevel@tonic-gate 	 * We need to place a router alert option in this packet.  The length
25717c478bd9Sstevel@tonic-gate 	 * of the options must be a multiple of 8.  The hbh option header is 2
25727c478bd9Sstevel@tonic-gate 	 * bytes followed by the 4 byte router alert option.  That leaves
25737c478bd9Sstevel@tonic-gate 	 * 2 bytes of pad for a total of 8 bytes.
25747c478bd9Sstevel@tonic-gate 	 */
25757c478bd9Sstevel@tonic-gate 	const int	router_alert_length = 8;
25767c478bd9Sstevel@tonic-gate 
25777c478bd9Sstevel@tonic-gate 	ASSERT(ill->ill_isv6);
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 	/*
25807c478bd9Sstevel@tonic-gate 	 * We need to make sure that this packet does not get load balanced.
25817c478bd9Sstevel@tonic-gate 	 * So, we allocate an ip6i_t and set ATTACH_IF. ip_wput_v6 and
25827c478bd9Sstevel@tonic-gate 	 * ip_newroute_ipif_v6 knows how to handle such packets.
25837c478bd9Sstevel@tonic-gate 	 * If it gets load balanced, switches supporting MLD snooping
25847c478bd9Sstevel@tonic-gate 	 * (in the future) will send the packet that it receives for this
25857c478bd9Sstevel@tonic-gate 	 * multicast group to the interface that we are sending on. As we have
25867c478bd9Sstevel@tonic-gate 	 * joined the multicast group on this ill, by sending the packet out
25877c478bd9Sstevel@tonic-gate 	 * on this ill, we receive all the packets back on this ill.
25887c478bd9Sstevel@tonic-gate 	 */
25897c478bd9Sstevel@tonic-gate 	size += sizeof (ip6i_t) + router_alert_length;
25907c478bd9Sstevel@tonic-gate 	mp = allocb(size, BPRI_HI);
25917c478bd9Sstevel@tonic-gate 	if (mp == NULL)
25927c478bd9Sstevel@tonic-gate 		return;
25937c478bd9Sstevel@tonic-gate 	bzero(mp->b_rptr, size);
25947c478bd9Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + size;
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate 	ip6i = (ip6i_t *)mp->b_rptr;
25977c478bd9Sstevel@tonic-gate 	ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
25987c478bd9Sstevel@tonic-gate 	ip6i->ip6i_nxt = IPPROTO_RAW;
25997c478bd9Sstevel@tonic-gate 	ip6i->ip6i_flags = IP6I_ATTACH_IF | IP6I_HOPLIMIT;
26007c478bd9Sstevel@tonic-gate 	ip6i->ip6i_ifindex = ill->ill_phyint->phyint_ifindex;
26017c478bd9Sstevel@tonic-gate 
26027c478bd9Sstevel@tonic-gate 	ip6h = (ip6_t *)&ip6i[1];
26037c478bd9Sstevel@tonic-gate 	ip6hbh = (struct ip6_hbh *)&ip6h[1];
26047c478bd9Sstevel@tonic-gate 	ip6router = (struct ip6_opt_router *)&ip6hbh[1];
26057c478bd9Sstevel@tonic-gate 	/*
26067c478bd9Sstevel@tonic-gate 	 * A zero is a pad option of length 1.  The bzero of the whole packet
26077c478bd9Sstevel@tonic-gate 	 * above will pad between ip6router and mld.
26087c478bd9Sstevel@tonic-gate 	 */
26097c478bd9Sstevel@tonic-gate 	mldh = (mld_hdr_t *)((uint8_t *)ip6hbh + router_alert_length);
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 	mldh->mld_type = type;
26127c478bd9Sstevel@tonic-gate 	mldh->mld_addr = ilm->ilm_v6addr;
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate 	ip6router->ip6or_type = IP6OPT_ROUTER_ALERT;
26157c478bd9Sstevel@tonic-gate 	ip6router->ip6or_len = 2;
26167c478bd9Sstevel@tonic-gate 	ip6router->ip6or_value[0] = 0;
26177c478bd9Sstevel@tonic-gate 	ip6router->ip6or_value[1] = IP6_ALERT_MLD;
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	ip6hbh->ip6h_nxt = IPPROTO_ICMPV6;
26207c478bd9Sstevel@tonic-gate 	ip6hbh->ip6h_len = 0;
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
26237c478bd9Sstevel@tonic-gate 	ip6h->ip6_plen = htons(sizeof (*mldh) + router_alert_length);
26247c478bd9Sstevel@tonic-gate 	ip6h->ip6_nxt = IPPROTO_HOPOPTS;
26257c478bd9Sstevel@tonic-gate 	ip6h->ip6_hops = MLD_HOP_LIMIT;
26267c478bd9Sstevel@tonic-gate 	if (v6addr == NULL)
26277c478bd9Sstevel@tonic-gate 		ip6h->ip6_dst =  ilm->ilm_v6addr;
26287c478bd9Sstevel@tonic-gate 	else
26297c478bd9Sstevel@tonic-gate 		ip6h->ip6_dst = *v6addr;
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 	/* ipif returned by ipif_lookup_zoneid is link-local (if present) */
26327c478bd9Sstevel@tonic-gate 	if (ipif_lookup_zoneid(ill, ilm->ilm_zoneid, IPIF_UP, &ipif)) {
26337c478bd9Sstevel@tonic-gate 		ip6h->ip6_src = ipif->ipif_v6src_addr;
26347c478bd9Sstevel@tonic-gate 		ipif_refrele(ipif);
26357c478bd9Sstevel@tonic-gate 	} else {
26367c478bd9Sstevel@tonic-gate 		/* Otherwise, use IPv6 default address selection. */
26377c478bd9Sstevel@tonic-gate 		ip6h->ip6_src = ipv6_all_zeros;
26387c478bd9Sstevel@tonic-gate 	}
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 	/*
26417c478bd9Sstevel@tonic-gate 	 * Prepare for checksum by putting icmp length in the icmp
26427c478bd9Sstevel@tonic-gate 	 * checksum field. The checksum is calculated in ip_wput_v6.
26437c478bd9Sstevel@tonic-gate 	 */
26447c478bd9Sstevel@tonic-gate 	mldh->mld_cksum = htons(sizeof (*mldh));
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate 	/*
26477c478bd9Sstevel@tonic-gate 	 * ip_wput will automatically loopback the multicast packet to
26487c478bd9Sstevel@tonic-gate 	 * the conn if multicast loopback is enabled.
26497c478bd9Sstevel@tonic-gate 	 * The MIB stats corresponding to this outgoing MLD packet
26507c478bd9Sstevel@tonic-gate 	 * will be accounted for in ip_wput->ip_wput_v6->ip_wput_ire_v6
26517c478bd9Sstevel@tonic-gate 	 * ->icmp_update_out_mib_v6 function call.
26527c478bd9Sstevel@tonic-gate 	 */
26537c478bd9Sstevel@tonic-gate 	(void) ip_output_v6(NULL, mp, ill->ill_wq, IP_WPUT);
26547c478bd9Sstevel@tonic-gate }
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate /*
26577c478bd9Sstevel@tonic-gate  * Sends an MLD_V2_LISTENER_REPORT message out the passed-in ill.  The
26587c478bd9Sstevel@tonic-gate  * report will contain one multicast address record for each element of
26597c478bd9Sstevel@tonic-gate  * reclist.  If this causes packet length to exceed ill->ill_max_frag,
26607c478bd9Sstevel@tonic-gate  * multiple reports are sent.  reclist is assumed to be made up of
26617c478bd9Sstevel@tonic-gate  * buffers allocated by mcast_bldmrec(), and those buffers are freed here.
26627c478bd9Sstevel@tonic-gate  */
26637c478bd9Sstevel@tonic-gate static void
26647c478bd9Sstevel@tonic-gate mldv2_sendrpt(ill_t *ill, mrec_t *reclist)
26657c478bd9Sstevel@tonic-gate {
26667c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
26677c478bd9Sstevel@tonic-gate 	mld2r_t		*mld2r;
26687c478bd9Sstevel@tonic-gate 	mld2mar_t	*mld2mar;
26697c478bd9Sstevel@tonic-gate 	in6_addr_t	*srcarray;
26707c478bd9Sstevel@tonic-gate 	ip6_t		*ip6h;
26717c478bd9Sstevel@tonic-gate 	ip6_hbh_t	*ip6hbh;
26727c478bd9Sstevel@tonic-gate 	ip6i_t		*ip6i;
26737c478bd9Sstevel@tonic-gate 	struct ip6_opt_router	*ip6router;
26747c478bd9Sstevel@tonic-gate 	size_t		size, optlen, padlen, icmpsize, rsize;
26757c478bd9Sstevel@tonic-gate 	ipif_t		*ipif;
26767c478bd9Sstevel@tonic-gate 	int		i, numrec, more_src_cnt;
26777c478bd9Sstevel@tonic-gate 	mrec_t		*rp, *cur_reclist;
26787c478bd9Sstevel@tonic-gate 	mrec_t		*next_reclist = reclist;
26797c478bd9Sstevel@tonic-gate 	boolean_t	morepkts;
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	/* If there aren't any records, there's nothing to send */
26827c478bd9Sstevel@tonic-gate 	if (reclist == NULL)
26837c478bd9Sstevel@tonic-gate 		return;
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 	ASSERT(ill->ill_isv6);
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 	/*
26887c478bd9Sstevel@tonic-gate 	 * Total option length (optlen + padlen) must be a multiple of
26897c478bd9Sstevel@tonic-gate 	 * 8 bytes.  We assume here that optlen <= 8, so the total option
26907c478bd9Sstevel@tonic-gate 	 * length will be 8.  Assert this in case anything ever changes.
26917c478bd9Sstevel@tonic-gate 	 */
26927c478bd9Sstevel@tonic-gate 	optlen = sizeof (ip6_hbh_t) + sizeof (struct ip6_opt_router);
26937c478bd9Sstevel@tonic-gate 	ASSERT(optlen <= 8);
26947c478bd9Sstevel@tonic-gate 	padlen = 8 - optlen;
26957c478bd9Sstevel@tonic-gate nextpkt:
26967c478bd9Sstevel@tonic-gate 	icmpsize = sizeof (mld2r_t);
26977c478bd9Sstevel@tonic-gate 	size = IPV6_HDR_LEN + optlen + padlen + icmpsize;
26987c478bd9Sstevel@tonic-gate 	morepkts = B_FALSE;
26997c478bd9Sstevel@tonic-gate 	more_src_cnt = 0;
27007c478bd9Sstevel@tonic-gate 	for (rp = cur_reclist = next_reclist, numrec = 0; rp != NULL;
27017c478bd9Sstevel@tonic-gate 	    rp = rp->mrec_next, numrec++) {
27027c478bd9Sstevel@tonic-gate 		rsize = sizeof (mld2mar_t) +
27037c478bd9Sstevel@tonic-gate 		    (rp->mrec_srcs.sl_numsrc * sizeof (in6_addr_t));
27047c478bd9Sstevel@tonic-gate 		if (size + rsize > ill->ill_max_frag) {
27057c478bd9Sstevel@tonic-gate 			if (rp == cur_reclist) {
27067c478bd9Sstevel@tonic-gate 				/*
27077c478bd9Sstevel@tonic-gate 				 * If the first mrec we looked at is too big
27087c478bd9Sstevel@tonic-gate 				 * to fit in a single packet (i.e the source
27097c478bd9Sstevel@tonic-gate 				 * list is too big), we must either truncate
27107c478bd9Sstevel@tonic-gate 				 * the list (if TO_EX or IS_EX), or send
27117c478bd9Sstevel@tonic-gate 				 * multiple reports for the same group (all
27127c478bd9Sstevel@tonic-gate 				 * other types).
27137c478bd9Sstevel@tonic-gate 				 */
27147c478bd9Sstevel@tonic-gate 				int srcspace, srcsperpkt;
27157c478bd9Sstevel@tonic-gate 				srcspace = ill->ill_max_frag -
27167c478bd9Sstevel@tonic-gate 				    (size + sizeof (mld2mar_t));
27177c478bd9Sstevel@tonic-gate 				srcsperpkt = srcspace / sizeof (in6_addr_t);
27187c478bd9Sstevel@tonic-gate 				/*
27197c478bd9Sstevel@tonic-gate 				 * Increment icmpsize and size, because we will
27207c478bd9Sstevel@tonic-gate 				 * be sending a record for the mrec we're
27217c478bd9Sstevel@tonic-gate 				 * looking at now.
27227c478bd9Sstevel@tonic-gate 				 */
27237c478bd9Sstevel@tonic-gate 				rsize = sizeof (mld2mar_t) +
27247c478bd9Sstevel@tonic-gate 				    (srcsperpkt * sizeof (in6_addr_t));
27257c478bd9Sstevel@tonic-gate 				icmpsize += rsize;
27267c478bd9Sstevel@tonic-gate 				size += rsize;
27277c478bd9Sstevel@tonic-gate 				if (rp->mrec_type == MODE_IS_EXCLUDE ||
27287c478bd9Sstevel@tonic-gate 				    rp->mrec_type == CHANGE_TO_EXCLUDE) {
27297c478bd9Sstevel@tonic-gate 					rp->mrec_srcs.sl_numsrc = srcsperpkt;
27307c478bd9Sstevel@tonic-gate 					if (rp->mrec_next == NULL) {
27317c478bd9Sstevel@tonic-gate 						/* no more packets to send */
27327c478bd9Sstevel@tonic-gate 						break;
27337c478bd9Sstevel@tonic-gate 					} else {
27347c478bd9Sstevel@tonic-gate 						/*
27357c478bd9Sstevel@tonic-gate 						 * more packets, but we're
27367c478bd9Sstevel@tonic-gate 						 * done with this mrec.
27377c478bd9Sstevel@tonic-gate 						 */
27387c478bd9Sstevel@tonic-gate 						next_reclist = rp->mrec_next;
27397c478bd9Sstevel@tonic-gate 					}
27407c478bd9Sstevel@tonic-gate 				} else {
27417c478bd9Sstevel@tonic-gate 					more_src_cnt = rp->mrec_srcs.sl_numsrc
27427c478bd9Sstevel@tonic-gate 					    - srcsperpkt;
27437c478bd9Sstevel@tonic-gate 					rp->mrec_srcs.sl_numsrc = srcsperpkt;
27447c478bd9Sstevel@tonic-gate 					/*
27457c478bd9Sstevel@tonic-gate 					 * We'll fix up this mrec (remove the
27467c478bd9Sstevel@tonic-gate 					 * srcs we've already sent) before
27477c478bd9Sstevel@tonic-gate 					 * returning to nextpkt above.
27487c478bd9Sstevel@tonic-gate 					 */
27497c478bd9Sstevel@tonic-gate 					next_reclist = rp;
27507c478bd9Sstevel@tonic-gate 				}
27517c478bd9Sstevel@tonic-gate 			} else {
27527c478bd9Sstevel@tonic-gate 				next_reclist = rp;
27537c478bd9Sstevel@tonic-gate 			}
27547c478bd9Sstevel@tonic-gate 			morepkts = B_TRUE;
27557c478bd9Sstevel@tonic-gate 			break;
27567c478bd9Sstevel@tonic-gate 		}
27577c478bd9Sstevel@tonic-gate 		icmpsize += rsize;
27587c478bd9Sstevel@tonic-gate 		size += rsize;
27597c478bd9Sstevel@tonic-gate 	}
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate 	/*
27627c478bd9Sstevel@tonic-gate 	 * We need to make sure that this packet does not get load balanced.
27637c478bd9Sstevel@tonic-gate 	 * So, we allocate an ip6i_t and set ATTACH_IF. ip_wput_v6 and
27647c478bd9Sstevel@tonic-gate 	 * ip_newroute_ipif_v6 know how to handle such packets.
27657c478bd9Sstevel@tonic-gate 	 * If it gets load balanced, switches supporting MLD snooping
27667c478bd9Sstevel@tonic-gate 	 * (in the future) will send the packet that it receives for this
27677c478bd9Sstevel@tonic-gate 	 * multicast group to the interface that we are sending on. As we have
27687c478bd9Sstevel@tonic-gate 	 * joined the multicast group on this ill, by sending the packet out
27697c478bd9Sstevel@tonic-gate 	 * on this ill, we receive all the packets back on this ill.
27707c478bd9Sstevel@tonic-gate 	 */
27717c478bd9Sstevel@tonic-gate 	size += sizeof (ip6i_t);
27727c478bd9Sstevel@tonic-gate 	mp = allocb(size, BPRI_HI);
27737c478bd9Sstevel@tonic-gate 	if (mp == NULL)
27747c478bd9Sstevel@tonic-gate 		goto free_reclist;
27757c478bd9Sstevel@tonic-gate 	bzero(mp->b_rptr, size);
27767c478bd9Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + size;
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate 	ip6i = (ip6i_t *)mp->b_rptr;
27797c478bd9Sstevel@tonic-gate 	ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
27807c478bd9Sstevel@tonic-gate 	ip6i->ip6i_nxt = IPPROTO_RAW;
27817c478bd9Sstevel@tonic-gate 	ip6i->ip6i_flags = IP6I_ATTACH_IF;
27827c478bd9Sstevel@tonic-gate 	ip6i->ip6i_ifindex = ill->ill_phyint->phyint_ifindex;
27837c478bd9Sstevel@tonic-gate 
27847c478bd9Sstevel@tonic-gate 	ip6h = (ip6_t *)&(ip6i[1]);
27857c478bd9Sstevel@tonic-gate 	ip6hbh = (ip6_hbh_t *)&(ip6h[1]);
27867c478bd9Sstevel@tonic-gate 	ip6router = (struct ip6_opt_router *)&(ip6hbh[1]);
27877c478bd9Sstevel@tonic-gate 	mld2r = (mld2r_t *)((uint8_t *)ip6hbh + optlen + padlen);
27887c478bd9Sstevel@tonic-gate 	mld2mar = (mld2mar_t *)&(mld2r[1]);
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 	ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW;
27917c478bd9Sstevel@tonic-gate 	ip6h->ip6_plen = htons(optlen + padlen + icmpsize);
27927c478bd9Sstevel@tonic-gate 	ip6h->ip6_nxt = IPPROTO_HOPOPTS;
27937c478bd9Sstevel@tonic-gate 	ip6h->ip6_hops = MLD_HOP_LIMIT;
27947c478bd9Sstevel@tonic-gate 	ip6h->ip6_dst = ipv6_all_v2rtrs_mcast;
27957c478bd9Sstevel@tonic-gate 	/* ipif returned by ipif_lookup_zoneid is link-local (if present) */
27967c478bd9Sstevel@tonic-gate 	if (ipif_lookup_zoneid(ill, ALL_ZONES, IPIF_UP, &ipif)) {
27977c478bd9Sstevel@tonic-gate 		ip6h->ip6_src = ipif->ipif_v6src_addr;
27987c478bd9Sstevel@tonic-gate 		ipif_refrele(ipif);
27997c478bd9Sstevel@tonic-gate 	} else {
28007c478bd9Sstevel@tonic-gate 		/* otherwise, use IPv6 default address selection. */
28017c478bd9Sstevel@tonic-gate 		ip6h->ip6_src = ipv6_all_zeros;
28027c478bd9Sstevel@tonic-gate 	}
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate 	ip6hbh->ip6h_nxt = IPPROTO_ICMPV6;
28057c478bd9Sstevel@tonic-gate 	/*
28067c478bd9Sstevel@tonic-gate 	 * ip6h_len is the number of 8-byte words, not including the first
28077c478bd9Sstevel@tonic-gate 	 * 8 bytes; we've assumed optlen + padlen == 8 bytes; hence len = 0.
28087c478bd9Sstevel@tonic-gate 	 */
28097c478bd9Sstevel@tonic-gate 	ip6hbh->ip6h_len = 0;
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	ip6router->ip6or_type = IP6OPT_ROUTER_ALERT;
28127c478bd9Sstevel@tonic-gate 	ip6router->ip6or_len = 2;
28137c478bd9Sstevel@tonic-gate 	ip6router->ip6or_value[0] = 0;
28147c478bd9Sstevel@tonic-gate 	ip6router->ip6or_value[1] = IP6_ALERT_MLD;
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate 	mld2r->mld2r_type = MLD_V2_LISTENER_REPORT;
28177c478bd9Sstevel@tonic-gate 	mld2r->mld2r_nummar = htons(numrec);
28187c478bd9Sstevel@tonic-gate 	/*
28197c478bd9Sstevel@tonic-gate 	 * Prepare for the checksum by putting icmp length in the icmp
28207c478bd9Sstevel@tonic-gate 	 * checksum field. The checksum is calculated in ip_wput_v6.
28217c478bd9Sstevel@tonic-gate 	 */
28227c478bd9Sstevel@tonic-gate 	mld2r->mld2r_cksum = htons(icmpsize);
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	for (rp = cur_reclist; rp != NULL; rp = rp->mrec_next) {
28257c478bd9Sstevel@tonic-gate 		mld2mar->mld2mar_type = rp->mrec_type;
28267c478bd9Sstevel@tonic-gate 		mld2mar->mld2mar_auxlen = 0;
28277c478bd9Sstevel@tonic-gate 		mld2mar->mld2mar_numsrc = htons(rp->mrec_srcs.sl_numsrc);
28287c478bd9Sstevel@tonic-gate 		mld2mar->mld2mar_group = rp->mrec_group;
28297c478bd9Sstevel@tonic-gate 		srcarray = (in6_addr_t *)&(mld2mar[1]);
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 		for (i = 0; i < rp->mrec_srcs.sl_numsrc; i++)
28327c478bd9Sstevel@tonic-gate 			srcarray[i] = rp->mrec_srcs.sl_addr[i];
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 		mld2mar = (mld2mar_t *)&(srcarray[i]);
28357c478bd9Sstevel@tonic-gate 	}
28367c478bd9Sstevel@tonic-gate 
28377c478bd9Sstevel@tonic-gate 	/*
28387c478bd9Sstevel@tonic-gate 	 * ip_wput will automatically loopback the multicast packet to
28397c478bd9Sstevel@tonic-gate 	 * the conn if multicast loopback is enabled.
28407c478bd9Sstevel@tonic-gate 	 * The MIB stats corresponding to this outgoing MLD packet
28417c478bd9Sstevel@tonic-gate 	 * will be accounted for in ip_wput->ip_wput_v6->ip_wput_ire_v6
28427c478bd9Sstevel@tonic-gate 	 * ->icmp_update_out_mib_v6 function call.
28437c478bd9Sstevel@tonic-gate 	 */
28447c478bd9Sstevel@tonic-gate 	(void) ip_output_v6(NULL, mp, ill->ill_wq, IP_WPUT);
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate 	if (morepkts) {
28477c478bd9Sstevel@tonic-gate 		if (more_src_cnt > 0) {
28487c478bd9Sstevel@tonic-gate 			int index, mvsize;
28497c478bd9Sstevel@tonic-gate 			slist_t *sl = &next_reclist->mrec_srcs;
28507c478bd9Sstevel@tonic-gate 			index = sl->sl_numsrc;
28517c478bd9Sstevel@tonic-gate 			mvsize = more_src_cnt * sizeof (in6_addr_t);
28527c478bd9Sstevel@tonic-gate 			(void) memmove(&sl->sl_addr[0], &sl->sl_addr[index],
28537c478bd9Sstevel@tonic-gate 			    mvsize);
28547c478bd9Sstevel@tonic-gate 			sl->sl_numsrc = more_src_cnt;
28557c478bd9Sstevel@tonic-gate 		}
28567c478bd9Sstevel@tonic-gate 		goto nextpkt;
28577c478bd9Sstevel@tonic-gate 	}
28587c478bd9Sstevel@tonic-gate 
28597c478bd9Sstevel@tonic-gate free_reclist:
28607c478bd9Sstevel@tonic-gate 	while (reclist != NULL) {
28617c478bd9Sstevel@tonic-gate 		rp = reclist->mrec_next;
28627c478bd9Sstevel@tonic-gate 		mi_free(reclist);
28637c478bd9Sstevel@tonic-gate 		reclist = rp;
28647c478bd9Sstevel@tonic-gate 	}
28657c478bd9Sstevel@tonic-gate }
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate static mrec_t *
28687c478bd9Sstevel@tonic-gate mcast_bldmrec(mcast_record_t type, in6_addr_t *grp, slist_t *srclist,
28697c478bd9Sstevel@tonic-gate     mrec_t *next)
28707c478bd9Sstevel@tonic-gate {
28717c478bd9Sstevel@tonic-gate 	mrec_t *rp;
28727c478bd9Sstevel@tonic-gate 	int i;
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 	if ((type == ALLOW_NEW_SOURCES || type == BLOCK_OLD_SOURCES) &&
28757c478bd9Sstevel@tonic-gate 	    SLIST_IS_EMPTY(srclist))
28767c478bd9Sstevel@tonic-gate 		return (next);
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 	rp = (mrec_t *)mi_alloc(sizeof (mrec_t), BPRI_HI);
28797c478bd9Sstevel@tonic-gate 	if (rp == NULL)
28807c478bd9Sstevel@tonic-gate 		return (next);
28817c478bd9Sstevel@tonic-gate 
28827c478bd9Sstevel@tonic-gate 	rp->mrec_next = next;
28837c478bd9Sstevel@tonic-gate 	rp->mrec_type = type;
28847c478bd9Sstevel@tonic-gate 	rp->mrec_auxlen = 0;
28857c478bd9Sstevel@tonic-gate 	rp->mrec_group = *grp;
28867c478bd9Sstevel@tonic-gate 	if (srclist == NULL) {
28877c478bd9Sstevel@tonic-gate 		rp->mrec_srcs.sl_numsrc = 0;
28887c478bd9Sstevel@tonic-gate 	} else {
28897c478bd9Sstevel@tonic-gate 		rp->mrec_srcs.sl_numsrc = srclist->sl_numsrc;
28907c478bd9Sstevel@tonic-gate 		for (i = 0; i < srclist->sl_numsrc; i++)
28917c478bd9Sstevel@tonic-gate 			rp->mrec_srcs.sl_addr[i] = srclist->sl_addr[i];
28927c478bd9Sstevel@tonic-gate 	}
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 	return (rp);
28957c478bd9Sstevel@tonic-gate }
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate /*
28987c478bd9Sstevel@tonic-gate  * Set up initial retransmit state.  If memory cannot be allocated for
28997c478bd9Sstevel@tonic-gate  * the source lists, simply create as much state as is possible; memory
29007c478bd9Sstevel@tonic-gate  * allocation failures are considered one type of transient error that
29017c478bd9Sstevel@tonic-gate  * the retransmissions are designed to overcome (and if they aren't
29027c478bd9Sstevel@tonic-gate  * transient, there are bigger problems than failing to notify the
29037c478bd9Sstevel@tonic-gate  * router about multicast group membership state changes).
29047c478bd9Sstevel@tonic-gate  */
29057c478bd9Sstevel@tonic-gate static void
29067c478bd9Sstevel@tonic-gate mcast_init_rtx(ill_t *ill, rtx_state_t *rtxp, mcast_record_t rtype,
29077c478bd9Sstevel@tonic-gate     slist_t *flist)
29087c478bd9Sstevel@tonic-gate {
29097c478bd9Sstevel@tonic-gate 	/*
29107c478bd9Sstevel@tonic-gate 	 * There are only three possibilities for rtype:
29117c478bd9Sstevel@tonic-gate 	 *	New join, transition from INCLUDE {} to INCLUDE {flist}
29127c478bd9Sstevel@tonic-gate 	 *	  => rtype is ALLOW_NEW_SOURCES
29137c478bd9Sstevel@tonic-gate 	 *	New join, transition from INCLUDE {} to EXCLUDE {flist}
29147c478bd9Sstevel@tonic-gate 	 *	  => rtype is CHANGE_TO_EXCLUDE
29157c478bd9Sstevel@tonic-gate 	 *	State change that involves a filter mode change
29167c478bd9Sstevel@tonic-gate 	 *	  => rtype is either CHANGE_TO_INCLUDE or CHANGE_TO_EXCLUDE
29177c478bd9Sstevel@tonic-gate 	 */
29187c478bd9Sstevel@tonic-gate 	ASSERT(rtype == CHANGE_TO_EXCLUDE || rtype == CHANGE_TO_INCLUDE ||
29197c478bd9Sstevel@tonic-gate 	    rtype == ALLOW_NEW_SOURCES);
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate 	rtxp->rtx_cnt = ill->ill_mcast_rv;
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate 	switch (rtype) {
29247c478bd9Sstevel@tonic-gate 	case CHANGE_TO_EXCLUDE:
29257c478bd9Sstevel@tonic-gate 		rtxp->rtx_fmode_cnt = ill->ill_mcast_rv;
29267c478bd9Sstevel@tonic-gate 		CLEAR_SLIST(rtxp->rtx_allow);
29277c478bd9Sstevel@tonic-gate 		COPY_SLIST(flist, rtxp->rtx_block);
29287c478bd9Sstevel@tonic-gate 		break;
29297c478bd9Sstevel@tonic-gate 	case ALLOW_NEW_SOURCES:
29307c478bd9Sstevel@tonic-gate 	case CHANGE_TO_INCLUDE:
29317c478bd9Sstevel@tonic-gate 		rtxp->rtx_fmode_cnt =
29327c478bd9Sstevel@tonic-gate 		    rtype == ALLOW_NEW_SOURCES ? 0 : ill->ill_mcast_rv;
29337c478bd9Sstevel@tonic-gate 		CLEAR_SLIST(rtxp->rtx_block);
29347c478bd9Sstevel@tonic-gate 		COPY_SLIST(flist, rtxp->rtx_allow);
29357c478bd9Sstevel@tonic-gate 		break;
29367c478bd9Sstevel@tonic-gate 	}
29377c478bd9Sstevel@tonic-gate }
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate /*
29407c478bd9Sstevel@tonic-gate  * The basic strategy here, as extrapolated from RFC 3810 section 6.1 and
29417c478bd9Sstevel@tonic-gate  * RFC 3376 section 5.1, covers three cases:
29427c478bd9Sstevel@tonic-gate  *	* The current state change is a filter mode change
29437c478bd9Sstevel@tonic-gate  *		Set filter mode retransmit counter; set retransmit allow or
29447c478bd9Sstevel@tonic-gate  *		block list to new source list as appropriate, and clear the
29457c478bd9Sstevel@tonic-gate  *		retransmit list that was not set; send TO_IN or TO_EX with
29467c478bd9Sstevel@tonic-gate  *		new source list.
29477c478bd9Sstevel@tonic-gate  *	* The current state change is a source list change, but the filter
29487c478bd9Sstevel@tonic-gate  *	  mode retransmit counter is > 0
29497c478bd9Sstevel@tonic-gate  *		Decrement filter mode retransmit counter; set retransmit
29507c478bd9Sstevel@tonic-gate  *		allow or block list to  new source list as appropriate,
29517c478bd9Sstevel@tonic-gate  *		and clear the retransmit list that was not set; send TO_IN
29527c478bd9Sstevel@tonic-gate  *		or TO_EX with new source list.
29537c478bd9Sstevel@tonic-gate  *	* The current state change is a source list change, and the filter
29547c478bd9Sstevel@tonic-gate  *	  mode retransmit counter is 0.
29557c478bd9Sstevel@tonic-gate  *		Merge existing rtx allow and block lists with new state:
29567c478bd9Sstevel@tonic-gate  *		  rtx_allow = (new allow + rtx_allow) - new block
29577c478bd9Sstevel@tonic-gate  *		  rtx_block = (new block + rtx_block) - new allow
29587c478bd9Sstevel@tonic-gate  *		Send ALLOW and BLOCK records for new retransmit lists;
29597c478bd9Sstevel@tonic-gate  *		decrement retransmit counter.
29607c478bd9Sstevel@tonic-gate  *
29617c478bd9Sstevel@tonic-gate  * As is the case for mcast_init_rtx(), memory allocation failures are
29627c478bd9Sstevel@tonic-gate  * acceptable; we just create as much state as we can.
29637c478bd9Sstevel@tonic-gate  */
29647c478bd9Sstevel@tonic-gate static mrec_t *
29657c478bd9Sstevel@tonic-gate mcast_merge_rtx(ilm_t *ilm, mrec_t *mreclist, slist_t *flist)
29667c478bd9Sstevel@tonic-gate {
29677c478bd9Sstevel@tonic-gate 	ill_t *ill;
29687c478bd9Sstevel@tonic-gate 	rtx_state_t *rtxp = &ilm->ilm_rtx;
29697c478bd9Sstevel@tonic-gate 	mcast_record_t txtype;
29707c478bd9Sstevel@tonic-gate 	mrec_t *rp, *rpnext, *rtnmrec;
29717c478bd9Sstevel@tonic-gate 	boolean_t ovf;
29727c478bd9Sstevel@tonic-gate 
29737c478bd9Sstevel@tonic-gate 	ill = (ilm->ilm_ill == NULL ? ilm->ilm_ipif->ipif_ill : ilm->ilm_ill);
29747c478bd9Sstevel@tonic-gate 
29757c478bd9Sstevel@tonic-gate 	if (mreclist == NULL)
29767c478bd9Sstevel@tonic-gate 		return (mreclist);
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate 	/*
29797c478bd9Sstevel@tonic-gate 	 * A filter mode change is indicated by a single mrec, which is
29807c478bd9Sstevel@tonic-gate 	 * either TO_IN or TO_EX.  In this case, we just need to set new
29817c478bd9Sstevel@tonic-gate 	 * retransmit state as if this were an initial join.  There is
29827c478bd9Sstevel@tonic-gate 	 * no change to the mrec list.
29837c478bd9Sstevel@tonic-gate 	 */
29847c478bd9Sstevel@tonic-gate 	if (mreclist->mrec_type == CHANGE_TO_INCLUDE ||
29857c478bd9Sstevel@tonic-gate 	    mreclist->mrec_type == CHANGE_TO_EXCLUDE) {
29867c478bd9Sstevel@tonic-gate 		mcast_init_rtx(ill, rtxp, mreclist->mrec_type,
29877c478bd9Sstevel@tonic-gate 		    &mreclist->mrec_srcs);
29887c478bd9Sstevel@tonic-gate 		return (mreclist);
29897c478bd9Sstevel@tonic-gate 	}
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate 	/*
29927c478bd9Sstevel@tonic-gate 	 * Only the source list has changed
29937c478bd9Sstevel@tonic-gate 	 */
29947c478bd9Sstevel@tonic-gate 	rtxp->rtx_cnt = ill->ill_mcast_rv;
29957c478bd9Sstevel@tonic-gate 	if (rtxp->rtx_fmode_cnt > 0) {
29967c478bd9Sstevel@tonic-gate 		/* but we're still sending filter mode change reports */
29977c478bd9Sstevel@tonic-gate 		rtxp->rtx_fmode_cnt--;
29987c478bd9Sstevel@tonic-gate 		if (ilm->ilm_fmode == MODE_IS_INCLUDE) {
29997c478bd9Sstevel@tonic-gate 			CLEAR_SLIST(rtxp->rtx_block);
30007c478bd9Sstevel@tonic-gate 			COPY_SLIST(flist, rtxp->rtx_allow);
30017c478bd9Sstevel@tonic-gate 			txtype = CHANGE_TO_INCLUDE;
30027c478bd9Sstevel@tonic-gate 		} else {
30037c478bd9Sstevel@tonic-gate 			CLEAR_SLIST(rtxp->rtx_allow);
30047c478bd9Sstevel@tonic-gate 			COPY_SLIST(flist, rtxp->rtx_block);
30057c478bd9Sstevel@tonic-gate 			txtype = CHANGE_TO_EXCLUDE;
30067c478bd9Sstevel@tonic-gate 		}
30077c478bd9Sstevel@tonic-gate 		/* overwrite first mrec with new info */
30087c478bd9Sstevel@tonic-gate 		mreclist->mrec_type = txtype;
30097c478bd9Sstevel@tonic-gate 		l_copy(flist, &mreclist->mrec_srcs);
30107c478bd9Sstevel@tonic-gate 		/* then free any remaining mrecs */
30117c478bd9Sstevel@tonic-gate 		for (rp = mreclist->mrec_next; rp != NULL; rp = rpnext) {
30127c478bd9Sstevel@tonic-gate 			rpnext = rp->mrec_next;
30137c478bd9Sstevel@tonic-gate 			mi_free(rp);
30147c478bd9Sstevel@tonic-gate 		}
30157c478bd9Sstevel@tonic-gate 		mreclist->mrec_next = NULL;
30167c478bd9Sstevel@tonic-gate 		rtnmrec = mreclist;
30177c478bd9Sstevel@tonic-gate 	} else {
30187c478bd9Sstevel@tonic-gate 		mrec_t *allow_mrec, *block_mrec;
30197c478bd9Sstevel@tonic-gate 		/*
30207c478bd9Sstevel@tonic-gate 		 * Just send the source change reports; but we need to
30217c478bd9Sstevel@tonic-gate 		 * recalculate the ALLOW and BLOCK lists based on previous
30227c478bd9Sstevel@tonic-gate 		 * state and new changes.
30237c478bd9Sstevel@tonic-gate 		 */
30247c478bd9Sstevel@tonic-gate 		rtnmrec = mreclist;
30257c478bd9Sstevel@tonic-gate 		allow_mrec = block_mrec = NULL;
30267c478bd9Sstevel@tonic-gate 		for (rp = mreclist; rp != NULL; rp = rp->mrec_next) {
30277c478bd9Sstevel@tonic-gate 			ASSERT(rp->mrec_type == ALLOW_NEW_SOURCES ||
30287c478bd9Sstevel@tonic-gate 			    rp->mrec_type == BLOCK_OLD_SOURCES);
30297c478bd9Sstevel@tonic-gate 			if (rp->mrec_type == ALLOW_NEW_SOURCES)
30307c478bd9Sstevel@tonic-gate 				allow_mrec = rp;
30317c478bd9Sstevel@tonic-gate 			else
30327c478bd9Sstevel@tonic-gate 				block_mrec = rp;
30337c478bd9Sstevel@tonic-gate 		}
30347c478bd9Sstevel@tonic-gate 		/*
30357c478bd9Sstevel@tonic-gate 		 * Perform calculations:
30367c478bd9Sstevel@tonic-gate 		 *   new_allow = mrec_allow + (rtx_allow - mrec_block)
30377c478bd9Sstevel@tonic-gate 		 *   new_block = mrec_block + (rtx_block - mrec_allow)
30387c478bd9Sstevel@tonic-gate 		 *
30397c478bd9Sstevel@tonic-gate 		 * Each calc requires two steps, for example:
30407c478bd9Sstevel@tonic-gate 		 *   rtx_allow = rtx_allow - mrec_block;
30417c478bd9Sstevel@tonic-gate 		 *   new_allow = mrec_allow + rtx_allow;
30427c478bd9Sstevel@tonic-gate 		 *
30437c478bd9Sstevel@tonic-gate 		 * Store results in mrec lists, and then copy into rtx lists.
30447c478bd9Sstevel@tonic-gate 		 * We do it in this order in case the rtx list hasn't been
30457c478bd9Sstevel@tonic-gate 		 * alloc'd yet; if it hasn't and our alloc fails, that's okay,
30467c478bd9Sstevel@tonic-gate 		 * Overflows are also okay.
30477c478bd9Sstevel@tonic-gate 		 */
30487c478bd9Sstevel@tonic-gate 		if (block_mrec != NULL) {
30497c478bd9Sstevel@tonic-gate 			l_difference_in_a(rtxp->rtx_allow,
30507c478bd9Sstevel@tonic-gate 			    &block_mrec->mrec_srcs);
30517c478bd9Sstevel@tonic-gate 		}
30527c478bd9Sstevel@tonic-gate 		if (allow_mrec != NULL) {
30537c478bd9Sstevel@tonic-gate 			l_difference_in_a(rtxp->rtx_block,
30547c478bd9Sstevel@tonic-gate 			    &allow_mrec->mrec_srcs);
30557c478bd9Sstevel@tonic-gate 			l_union_in_a(&allow_mrec->mrec_srcs, rtxp->rtx_allow,
30567c478bd9Sstevel@tonic-gate 			    &ovf);
30577c478bd9Sstevel@tonic-gate 		}
30587c478bd9Sstevel@tonic-gate 		if (block_mrec != NULL) {
30597c478bd9Sstevel@tonic-gate 			l_union_in_a(&block_mrec->mrec_srcs, rtxp->rtx_block,
30607c478bd9Sstevel@tonic-gate 			    &ovf);
30617c478bd9Sstevel@tonic-gate 			COPY_SLIST(&block_mrec->mrec_srcs, rtxp->rtx_block);
30627c478bd9Sstevel@tonic-gate 		} else {
30637c478bd9Sstevel@tonic-gate 			rtnmrec = mcast_bldmrec(BLOCK_OLD_SOURCES,
30647c478bd9Sstevel@tonic-gate 			    &ilm->ilm_v6addr, rtxp->rtx_block, allow_mrec);
30657c478bd9Sstevel@tonic-gate 		}
30667c478bd9Sstevel@tonic-gate 		if (allow_mrec != NULL) {
30677c478bd9Sstevel@tonic-gate 			COPY_SLIST(&allow_mrec->mrec_srcs, rtxp->rtx_allow);
30687c478bd9Sstevel@tonic-gate 		} else {
30697c478bd9Sstevel@tonic-gate 			rtnmrec = mcast_bldmrec(ALLOW_NEW_SOURCES,
30707c478bd9Sstevel@tonic-gate 			    &ilm->ilm_v6addr, rtxp->rtx_allow, block_mrec);
30717c478bd9Sstevel@tonic-gate 		}
30727c478bd9Sstevel@tonic-gate 	}
30737c478bd9Sstevel@tonic-gate 
30747c478bd9Sstevel@tonic-gate 	return (rtnmrec);
30757c478bd9Sstevel@tonic-gate }
3076