xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.lib/in.mpathd/mpd_tables.c (revision 8485:633e5b5eb268)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52074Smeem  * Common Development and Distribution License (the "License").
62074Smeem  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8485SPeter.Memishian@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include "mpd_defs.h"
270Sstevel@tonic-gate #include "mpd_tables.h"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Global list of phyints, phyint instances, phyint groups and the anonymous
310Sstevel@tonic-gate  * group; the latter is initialized in phyint_init().
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate struct phyint *phyints = NULL;
340Sstevel@tonic-gate struct phyint_instance	*phyint_instances = NULL;
350Sstevel@tonic-gate struct phyint_group *phyint_groups = NULL;
360Sstevel@tonic-gate struct phyint_group *phyint_anongroup;
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate  * Grouplist signature; initialized in phyint_init().
400Sstevel@tonic-gate  */
410Sstevel@tonic-gate static uint64_t phyint_grouplistsig;
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static void phyint_inst_insert(struct phyint_instance *pii);
440Sstevel@tonic-gate static void phyint_inst_print(struct phyint_instance *pii);
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static void phyint_insert(struct phyint *pi, struct phyint_group *pg);
470Sstevel@tonic-gate static void phyint_delete(struct phyint *pi);
48*8485SPeter.Memishian@Sun.COM static boolean_t phyint_is_usable(struct phyint *pi);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static void logint_print(struct logint *li);
510Sstevel@tonic-gate static void logint_insert(struct phyint_instance *pii, struct logint *li);
520Sstevel@tonic-gate static struct logint *logint_lookup(struct phyint_instance *pii, char *li_name);
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static void target_print(struct target *tg);
550Sstevel@tonic-gate static void target_insert(struct phyint_instance *pii, struct target *tg);
560Sstevel@tonic-gate static struct target *target_first(struct phyint_instance *pii);
570Sstevel@tonic-gate static struct target *target_select_best(struct phyint_instance *pii);
580Sstevel@tonic-gate static void target_flush_hosts(struct phyint_group *pg);
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static void reset_pii_probes(struct phyint_instance *pii, struct target *tg);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static boolean_t phyint_inst_v6_sockinit(struct phyint_instance *pii);
630Sstevel@tonic-gate static boolean_t phyint_inst_v4_sockinit(struct phyint_instance *pii);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate static int phyint_state_event(struct phyint_group *pg, struct phyint *pi);
660Sstevel@tonic-gate static int phyint_group_state_event(struct phyint_group *pg);
670Sstevel@tonic-gate static int phyint_group_change_event(struct phyint_group *pg, ipmp_group_op_t);
680Sstevel@tonic-gate static int phyint_group_member_event(struct phyint_group *pg, struct phyint *pi,
690Sstevel@tonic-gate     ipmp_if_op_t op);
700Sstevel@tonic-gate 
71*8485SPeter.Memishian@Sun.COM static int logint_upcount(struct phyint *pi);
720Sstevel@tonic-gate static uint64_t gensig(void);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /* Initialize any per-file global state.  Returns 0 on success, -1 on failure */
750Sstevel@tonic-gate int
760Sstevel@tonic-gate phyint_init(void)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate 	phyint_grouplistsig = gensig();
790Sstevel@tonic-gate 	if (track_all_phyints) {
800Sstevel@tonic-gate 		phyint_anongroup = phyint_group_create("");
810Sstevel@tonic-gate 		if (phyint_anongroup == NULL)
820Sstevel@tonic-gate 			return (-1);
830Sstevel@tonic-gate 		phyint_group_insert(phyint_anongroup);
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 	return (0);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /* Return the phyint with the given name */
890Sstevel@tonic-gate struct phyint *
900Sstevel@tonic-gate phyint_lookup(const char *name)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	struct phyint *pi;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	if (debug & D_PHYINT)
950Sstevel@tonic-gate 		logdebug("phyint_lookup(%s)\n", name);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
980Sstevel@tonic-gate 		if (strncmp(pi->pi_name, name, sizeof (pi->pi_name)) == 0)
990Sstevel@tonic-gate 			break;
1000Sstevel@tonic-gate 	}
1010Sstevel@tonic-gate 	return (pi);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
104*8485SPeter.Memishian@Sun.COM /*
105*8485SPeter.Memishian@Sun.COM  * Lookup a phyint in the group that has the same hardware address as `pi', or
106*8485SPeter.Memishian@Sun.COM  * NULL if there's none.  If `online_only' is set, then only online phyints
107*8485SPeter.Memishian@Sun.COM  * are considered when matching.  Otherwise, phyints that had been offlined
108*8485SPeter.Memishian@Sun.COM  * due to a duplicate hardware address will also be considered.
109*8485SPeter.Memishian@Sun.COM  */
110*8485SPeter.Memishian@Sun.COM static struct phyint *
111*8485SPeter.Memishian@Sun.COM phyint_lookup_hwaddr(struct phyint *pi, boolean_t online_only)
112*8485SPeter.Memishian@Sun.COM {
113*8485SPeter.Memishian@Sun.COM 	struct phyint *pi2;
114*8485SPeter.Memishian@Sun.COM 
115*8485SPeter.Memishian@Sun.COM 	if (pi->pi_group == phyint_anongroup)
116*8485SPeter.Memishian@Sun.COM 		return (NULL);
117*8485SPeter.Memishian@Sun.COM 
118*8485SPeter.Memishian@Sun.COM 	for (pi2 = pi->pi_group->pg_phyint; pi2 != NULL; pi2 = pi2->pi_pgnext) {
119*8485SPeter.Memishian@Sun.COM 		if (pi2 == pi)
120*8485SPeter.Memishian@Sun.COM 			continue;
121*8485SPeter.Memishian@Sun.COM 
122*8485SPeter.Memishian@Sun.COM 		/*
123*8485SPeter.Memishian@Sun.COM 		 * NOTE: even when online_only is B_FALSE, we ignore phyints
124*8485SPeter.Memishian@Sun.COM 		 * that are administratively offline (rather than offline
125*8485SPeter.Memishian@Sun.COM 		 * because they're dups); when they're brought back online,
126*8485SPeter.Memishian@Sun.COM 		 * they'll be flagged as dups if need be.
127*8485SPeter.Memishian@Sun.COM 		 */
128*8485SPeter.Memishian@Sun.COM 		if (pi2->pi_state == PI_OFFLINE &&
129*8485SPeter.Memishian@Sun.COM 		    (online_only || !pi2->pi_hwaddrdup))
130*8485SPeter.Memishian@Sun.COM 			continue;
131*8485SPeter.Memishian@Sun.COM 
132*8485SPeter.Memishian@Sun.COM 		if (pi2->pi_hwaddrlen == pi->pi_hwaddrlen &&
133*8485SPeter.Memishian@Sun.COM 		    bcmp(pi2->pi_hwaddr, pi->pi_hwaddr, pi->pi_hwaddrlen) == 0)
134*8485SPeter.Memishian@Sun.COM 			return (pi2);
135*8485SPeter.Memishian@Sun.COM 	}
136*8485SPeter.Memishian@Sun.COM 	return (NULL);
137*8485SPeter.Memishian@Sun.COM }
138*8485SPeter.Memishian@Sun.COM 
139*8485SPeter.Memishian@Sun.COM /*
140*8485SPeter.Memishian@Sun.COM  * Respond to DLPI notifications.  Currently, this only processes physical
141*8485SPeter.Memishian@Sun.COM  * address changes for the phyint passed via `arg' by onlining or offlining
142*8485SPeter.Memishian@Sun.COM  * phyints in the group.
143*8485SPeter.Memishian@Sun.COM  */
144*8485SPeter.Memishian@Sun.COM /* ARGSUSED */
145*8485SPeter.Memishian@Sun.COM static void
146*8485SPeter.Memishian@Sun.COM phyint_link_notify(dlpi_handle_t dh, dlpi_notifyinfo_t *dnip, void *arg)
147*8485SPeter.Memishian@Sun.COM {
148*8485SPeter.Memishian@Sun.COM 	struct phyint *pi = arg;
149*8485SPeter.Memishian@Sun.COM 	struct phyint *oduppi = NULL, *duppi = NULL;
150*8485SPeter.Memishian@Sun.COM 
151*8485SPeter.Memishian@Sun.COM 	assert((dnip->dni_note & pi->pi_notes) != 0);
152*8485SPeter.Memishian@Sun.COM 
153*8485SPeter.Memishian@Sun.COM 	if (dnip->dni_note != DL_NOTE_PHYS_ADDR)
154*8485SPeter.Memishian@Sun.COM 		return;
155*8485SPeter.Memishian@Sun.COM 
156*8485SPeter.Memishian@Sun.COM 	assert(dnip->dni_physaddrlen <= DLPI_PHYSADDR_MAX);
157*8485SPeter.Memishian@Sun.COM 
158*8485SPeter.Memishian@Sun.COM 	/*
159*8485SPeter.Memishian@Sun.COM 	 * If our hardware address hasn't changed, there's nothing to do.
160*8485SPeter.Memishian@Sun.COM 	 */
161*8485SPeter.Memishian@Sun.COM 	if (pi->pi_hwaddrlen == dnip->dni_physaddrlen &&
162*8485SPeter.Memishian@Sun.COM 	    bcmp(pi->pi_hwaddr, dnip->dni_physaddr, pi->pi_hwaddrlen) == 0)
163*8485SPeter.Memishian@Sun.COM 		return;
164*8485SPeter.Memishian@Sun.COM 
165*8485SPeter.Memishian@Sun.COM 	oduppi = phyint_lookup_hwaddr(pi, _B_FALSE);
166*8485SPeter.Memishian@Sun.COM 	pi->pi_hwaddrlen = dnip->dni_physaddrlen;
167*8485SPeter.Memishian@Sun.COM 	(void) memcpy(pi->pi_hwaddr, dnip->dni_physaddr, pi->pi_hwaddrlen);
168*8485SPeter.Memishian@Sun.COM 	duppi = phyint_lookup_hwaddr(pi, _B_FALSE);
169*8485SPeter.Memishian@Sun.COM 
170*8485SPeter.Memishian@Sun.COM 	if (oduppi != NULL || pi->pi_hwaddrdup) {
171*8485SPeter.Memishian@Sun.COM 		/*
172*8485SPeter.Memishian@Sun.COM 		 * Our old hardware address was a duplicate.  If we'd been
173*8485SPeter.Memishian@Sun.COM 		 * offlined because of it, and our new hardware address is not
174*8485SPeter.Memishian@Sun.COM 		 * a duplicate, then bring us online.  Otherwise, `oduppi'
175*8485SPeter.Memishian@Sun.COM 		 * must've been the one brought offline; bring it online.
176*8485SPeter.Memishian@Sun.COM 		 */
177*8485SPeter.Memishian@Sun.COM 		if (pi->pi_hwaddrdup) {
178*8485SPeter.Memishian@Sun.COM 			if (duppi == NULL)
179*8485SPeter.Memishian@Sun.COM 				(void) phyint_undo_offline(pi);
180*8485SPeter.Memishian@Sun.COM 		} else {
181*8485SPeter.Memishian@Sun.COM 			assert(oduppi->pi_hwaddrdup);
182*8485SPeter.Memishian@Sun.COM 			(void) phyint_undo_offline(oduppi);
183*8485SPeter.Memishian@Sun.COM 		}
184*8485SPeter.Memishian@Sun.COM 	}
185*8485SPeter.Memishian@Sun.COM 
186*8485SPeter.Memishian@Sun.COM 	if (duppi != NULL && !pi->pi_hwaddrdup) {
187*8485SPeter.Memishian@Sun.COM 		/*
188*8485SPeter.Memishian@Sun.COM 		 * Our new hardware address was a duplicate and we're not
189*8485SPeter.Memishian@Sun.COM 		 * yet flagged as a duplicate; bring us offline.
190*8485SPeter.Memishian@Sun.COM 		 */
191*8485SPeter.Memishian@Sun.COM 		pi->pi_hwaddrdup = _B_TRUE;
192*8485SPeter.Memishian@Sun.COM 		(void) phyint_offline(pi, 0);
193*8485SPeter.Memishian@Sun.COM 	}
194*8485SPeter.Memishian@Sun.COM }
195*8485SPeter.Memishian@Sun.COM 
196*8485SPeter.Memishian@Sun.COM /*
197*8485SPeter.Memishian@Sun.COM  * Initialize information about the underlying link for `pi', and set us
198*8485SPeter.Memishian@Sun.COM  * up to be notified about future changes.  Returns _B_TRUE on success.
199*8485SPeter.Memishian@Sun.COM  */
200*8485SPeter.Memishian@Sun.COM boolean_t
201*8485SPeter.Memishian@Sun.COM phyint_link_init(struct phyint *pi)
202*8485SPeter.Memishian@Sun.COM {
203*8485SPeter.Memishian@Sun.COM 	int retval;
204*8485SPeter.Memishian@Sun.COM 	uint_t notes;
205*8485SPeter.Memishian@Sun.COM 	const char *errmsg;
206*8485SPeter.Memishian@Sun.COM 	dlpi_notifyid_t id;
207*8485SPeter.Memishian@Sun.COM 
208*8485SPeter.Memishian@Sun.COM 	pi->pi_notes = 0;
209*8485SPeter.Memishian@Sun.COM 	retval = dlpi_open(pi->pi_name, &pi->pi_dh, 0);
210*8485SPeter.Memishian@Sun.COM 	if (retval != DLPI_SUCCESS) {
211*8485SPeter.Memishian@Sun.COM 		pi->pi_dh = NULL;
212*8485SPeter.Memishian@Sun.COM 		errmsg = "cannot open";
213*8485SPeter.Memishian@Sun.COM 		goto failed;
214*8485SPeter.Memishian@Sun.COM 	}
215*8485SPeter.Memishian@Sun.COM 
216*8485SPeter.Memishian@Sun.COM 	pi->pi_hwaddrlen = DLPI_PHYSADDR_MAX;
217*8485SPeter.Memishian@Sun.COM 	retval = dlpi_get_physaddr(pi->pi_dh, DL_CURR_PHYS_ADDR, pi->pi_hwaddr,
218*8485SPeter.Memishian@Sun.COM 	    &pi->pi_hwaddrlen);
219*8485SPeter.Memishian@Sun.COM 	if (retval != DLPI_SUCCESS) {
220*8485SPeter.Memishian@Sun.COM 		errmsg = "cannot get hardware address";
221*8485SPeter.Memishian@Sun.COM 		goto failed;
222*8485SPeter.Memishian@Sun.COM 	}
223*8485SPeter.Memishian@Sun.COM 
224*8485SPeter.Memishian@Sun.COM 	retval = dlpi_bind(pi->pi_dh, DLPI_ANY_SAP, NULL);
225*8485SPeter.Memishian@Sun.COM 	if (retval != DLPI_SUCCESS) {
226*8485SPeter.Memishian@Sun.COM 		errmsg = "cannot bind to DLPI_ANY_SAP";
227*8485SPeter.Memishian@Sun.COM 		goto failed;
228*8485SPeter.Memishian@Sun.COM 	}
229*8485SPeter.Memishian@Sun.COM 
230*8485SPeter.Memishian@Sun.COM 	/*
231*8485SPeter.Memishian@Sun.COM 	 * Check if the link supports DLPI link state notifications.  For
232*8485SPeter.Memishian@Sun.COM 	 * historical reasons, the actual changes are tracked through routing
233*8485SPeter.Memishian@Sun.COM 	 * sockets, so we immediately disable the notification upon success.
234*8485SPeter.Memishian@Sun.COM 	 */
235*8485SPeter.Memishian@Sun.COM 	notes = DL_NOTE_LINK_UP | DL_NOTE_LINK_DOWN;
236*8485SPeter.Memishian@Sun.COM 	retval = dlpi_enabnotify(pi->pi_dh, notes, phyint_link_notify, pi, &id);
237*8485SPeter.Memishian@Sun.COM 	if (retval == DLPI_SUCCESS) {
238*8485SPeter.Memishian@Sun.COM 		(void) dlpi_disabnotify(pi->pi_dh, id, NULL);
239*8485SPeter.Memishian@Sun.COM 		pi->pi_notes |= notes;
240*8485SPeter.Memishian@Sun.COM 	}
241*8485SPeter.Memishian@Sun.COM 
242*8485SPeter.Memishian@Sun.COM 	/*
243*8485SPeter.Memishian@Sun.COM 	 * Enable notification of hardware address changes to keep pi_hwaddr
244*8485SPeter.Memishian@Sun.COM 	 * up-to-date and track if we need to offline/undo-offline phyints.
245*8485SPeter.Memishian@Sun.COM 	 */
246*8485SPeter.Memishian@Sun.COM 	notes = DL_NOTE_PHYS_ADDR;
247*8485SPeter.Memishian@Sun.COM 	retval = dlpi_enabnotify(pi->pi_dh, notes, phyint_link_notify, pi, &id);
248*8485SPeter.Memishian@Sun.COM 	if (retval == DLPI_SUCCESS && poll_add(dlpi_fd(pi->pi_dh)) == 0)
249*8485SPeter.Memishian@Sun.COM 		pi->pi_notes |= notes;
250*8485SPeter.Memishian@Sun.COM 
251*8485SPeter.Memishian@Sun.COM 	return (_B_TRUE);
252*8485SPeter.Memishian@Sun.COM failed:
253*8485SPeter.Memishian@Sun.COM 	logerr("%s: %s: %s\n", pi->pi_name, errmsg, dlpi_strerror(retval));
254*8485SPeter.Memishian@Sun.COM 	if (pi->pi_dh != NULL) {
255*8485SPeter.Memishian@Sun.COM 		dlpi_close(pi->pi_dh);
256*8485SPeter.Memishian@Sun.COM 		pi->pi_dh = NULL;
257*8485SPeter.Memishian@Sun.COM 	}
258*8485SPeter.Memishian@Sun.COM 	return (_B_FALSE);
259*8485SPeter.Memishian@Sun.COM }
260*8485SPeter.Memishian@Sun.COM 
261*8485SPeter.Memishian@Sun.COM /*
262*8485SPeter.Memishian@Sun.COM  * Close use of link on `pi'.
263*8485SPeter.Memishian@Sun.COM  */
264*8485SPeter.Memishian@Sun.COM void
265*8485SPeter.Memishian@Sun.COM phyint_link_close(struct phyint *pi)
266*8485SPeter.Memishian@Sun.COM {
267*8485SPeter.Memishian@Sun.COM 	if (pi->pi_notes & DL_NOTE_PHYS_ADDR) {
268*8485SPeter.Memishian@Sun.COM 		(void) poll_remove(dlpi_fd(pi->pi_dh));
269*8485SPeter.Memishian@Sun.COM 		pi->pi_notes &= ~DL_NOTE_PHYS_ADDR;
270*8485SPeter.Memishian@Sun.COM 	}
271*8485SPeter.Memishian@Sun.COM 
272*8485SPeter.Memishian@Sun.COM 	/*
273*8485SPeter.Memishian@Sun.COM 	 * NOTE: we don't clear pi_notes here so that iflinkstate() can still
274*8485SPeter.Memishian@Sun.COM 	 * properly report the link state even when offline (which is possible
275*8485SPeter.Memishian@Sun.COM 	 * since we use IFF_RUNNING to track link state).
276*8485SPeter.Memishian@Sun.COM 	 */
277*8485SPeter.Memishian@Sun.COM 	dlpi_close(pi->pi_dh);
278*8485SPeter.Memishian@Sun.COM 	pi->pi_dh = NULL;
279*8485SPeter.Memishian@Sun.COM }
280*8485SPeter.Memishian@Sun.COM 
2810Sstevel@tonic-gate /* Return the phyint instance with the given name and the given family */
2820Sstevel@tonic-gate struct phyint_instance *
2830Sstevel@tonic-gate phyint_inst_lookup(int af, char *name)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	struct phyint *pi;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	if (debug & D_PHYINT)
2880Sstevel@tonic-gate 		logdebug("phyint_inst_lookup(%s %s)\n", AF_STR(af), name);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	assert(af == AF_INET || af == AF_INET6);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	pi = phyint_lookup(name);
2930Sstevel@tonic-gate 	if (pi == NULL)
2940Sstevel@tonic-gate 		return (NULL);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	return (PHYINT_INSTANCE(pi, af));
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate 
299*8485SPeter.Memishian@Sun.COM struct phyint_group *
3000Sstevel@tonic-gate phyint_group_lookup(const char *pg_name)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate 	struct phyint_group *pg;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	if (debug & D_PHYINT)
3050Sstevel@tonic-gate 		logdebug("phyint_group_lookup(%s)\n", pg_name);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	for (pg = phyint_groups; pg != NULL; pg = pg->pg_next) {
3080Sstevel@tonic-gate 		if (strncmp(pg->pg_name, pg_name, sizeof (pg->pg_name)) == 0)
3090Sstevel@tonic-gate 			break;
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 	return (pg);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate  * Insert the phyint in the linked list of all phyints. If the phyint belongs
3160Sstevel@tonic-gate  * to some group, insert it in the phyint group list.
3170Sstevel@tonic-gate  */
3180Sstevel@tonic-gate static void
3190Sstevel@tonic-gate phyint_insert(struct phyint *pi, struct phyint_group *pg)
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate 	if (debug & D_PHYINT)
3220Sstevel@tonic-gate 		logdebug("phyint_insert(%s '%s')\n", pi->pi_name, pg->pg_name);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	/* Insert the phyint at the head of the 'all phyints' list */
3250Sstevel@tonic-gate 	pi->pi_next = phyints;
3260Sstevel@tonic-gate 	pi->pi_prev = NULL;
3270Sstevel@tonic-gate 	if (phyints != NULL)
3280Sstevel@tonic-gate 		phyints->pi_prev = pi;
3290Sstevel@tonic-gate 	phyints = pi;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	/*
3320Sstevel@tonic-gate 	 * Insert the phyint at the head of the 'phyint_group members' list
3330Sstevel@tonic-gate 	 * of the phyint group to which it belongs.
3340Sstevel@tonic-gate 	 */
3350Sstevel@tonic-gate 	pi->pi_pgnext = NULL;
3360Sstevel@tonic-gate 	pi->pi_pgprev = NULL;
3370Sstevel@tonic-gate 	pi->pi_group = pg;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	pi->pi_pgnext = pg->pg_phyint;
3400Sstevel@tonic-gate 	if (pi->pi_pgnext != NULL)
3410Sstevel@tonic-gate 		pi->pi_pgnext->pi_pgprev = pi;
3420Sstevel@tonic-gate 	pg->pg_phyint = pi;
3430Sstevel@tonic-gate 
344*8485SPeter.Memishian@Sun.COM 	/* Refresh the group state now that this phyint has been added */
345*8485SPeter.Memishian@Sun.COM 	phyint_group_refresh_state(pg);
346*8485SPeter.Memishian@Sun.COM 
3470Sstevel@tonic-gate 	pg->pg_sig++;
3480Sstevel@tonic-gate 	(void) phyint_group_member_event(pg, pi, IPMP_IF_ADD);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate /* Insert the phyint instance in the linked list of all phyint instances. */
3520Sstevel@tonic-gate static void
3530Sstevel@tonic-gate phyint_inst_insert(struct phyint_instance *pii)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate 	if (debug & D_PHYINT) {
3560Sstevel@tonic-gate 		logdebug("phyint_inst_insert(%s %s)\n",
3570Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name);
3580Sstevel@tonic-gate 	}
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	/*
3610Sstevel@tonic-gate 	 * Insert the phyint at the head of the 'all phyint instances' list.
3620Sstevel@tonic-gate 	 */
3630Sstevel@tonic-gate 	pii->pii_next = phyint_instances;
3640Sstevel@tonic-gate 	pii->pii_prev = NULL;
3650Sstevel@tonic-gate 	if (phyint_instances != NULL)
3660Sstevel@tonic-gate 		phyint_instances->pii_prev = pii;
3670Sstevel@tonic-gate 	phyint_instances = pii;
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate  * Create a new phyint with the given parameters. Also insert it into
3720Sstevel@tonic-gate  * the list of all phyints and the list of phyint group members by calling
3730Sstevel@tonic-gate  * phyint_insert().
3740Sstevel@tonic-gate  */
3750Sstevel@tonic-gate static struct phyint *
3760Sstevel@tonic-gate phyint_create(char *pi_name, struct phyint_group *pg, uint_t ifindex,
3770Sstevel@tonic-gate     uint64_t flags)
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate 	struct phyint *pi;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	pi = calloc(1, sizeof (struct phyint));
3820Sstevel@tonic-gate 	if (pi == NULL) {
3830Sstevel@tonic-gate 		logperror("phyint_create: calloc");
3840Sstevel@tonic-gate 		return (NULL);
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	/*
388*8485SPeter.Memishian@Sun.COM 	 * Record the phyint values.
3890Sstevel@tonic-gate 	 */
3904770Smeem 	(void) strlcpy(pi->pi_name, pi_name, sizeof (pi->pi_name));
3914770Smeem 	pi->pi_taddrthresh = getcurrentsec() + TESTADDR_CONF_TIME;
3920Sstevel@tonic-gate 	pi->pi_ifindex = ifindex;
393*8485SPeter.Memishian@Sun.COM 	pi->pi_icmpid = htons(((getpid() & 0xFF) << 8) | (ifindex & 0xFF));
394*8485SPeter.Memishian@Sun.COM 
3950Sstevel@tonic-gate 	/*
396*8485SPeter.Memishian@Sun.COM 	 * If the interface is offline, we set the state to PI_OFFLINE.
397*8485SPeter.Memishian@Sun.COM 	 * Otherwise, we optimistically start in the PI_RUNNING state.  Later
398*8485SPeter.Memishian@Sun.COM 	 * (in process_link_state_changes()), we will adjust this to match the
3990Sstevel@tonic-gate 	 * current state of the link.  Further, if test addresses are
4000Sstevel@tonic-gate 	 * subsequently assigned, we will transition to PI_NOTARGETS and then
401*8485SPeter.Memishian@Sun.COM 	 * to either PI_RUNNING or PI_FAILED depending on the probe results.
4020Sstevel@tonic-gate 	 */
403*8485SPeter.Memishian@Sun.COM 	pi->pi_state = (flags & IFF_OFFLINE) ? PI_OFFLINE : PI_RUNNING;
4040Sstevel@tonic-gate 	pi->pi_flags = PHYINT_FLAGS(flags);
405*8485SPeter.Memishian@Sun.COM 
4060Sstevel@tonic-gate 	/*
4070Sstevel@tonic-gate 	 * Initialise the link state.  The link state is initialised to
4080Sstevel@tonic-gate 	 * up, so that if the link is down when IPMP starts monitoring
4090Sstevel@tonic-gate 	 * the interface, it will appear as though there has been a
4100Sstevel@tonic-gate 	 * transition from the link up to link down.  This avoids
4110Sstevel@tonic-gate 	 * having to treat this situation as a special case.
4120Sstevel@tonic-gate 	 */
4130Sstevel@tonic-gate 	INIT_LINK_STATE(pi);
4140Sstevel@tonic-gate 
415*8485SPeter.Memishian@Sun.COM 	if (!phyint_link_init(pi)) {
416*8485SPeter.Memishian@Sun.COM 		free(pi);
417*8485SPeter.Memishian@Sun.COM 		return (NULL);
418*8485SPeter.Memishian@Sun.COM 	}
419*8485SPeter.Memishian@Sun.COM 
4200Sstevel@tonic-gate 	/*
4210Sstevel@tonic-gate 	 * Insert the phyint in the list of all phyints, and the
4220Sstevel@tonic-gate 	 * list of phyint group members
4230Sstevel@tonic-gate 	 */
4240Sstevel@tonic-gate 	phyint_insert(pi, pg);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	return (pi);
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate /*
4300Sstevel@tonic-gate  * Create a new phyint instance belonging to the phyint 'pi' and address
4310Sstevel@tonic-gate  * family 'af'. Also insert it into the list of all phyint instances by
4320Sstevel@tonic-gate  * calling phyint_inst_insert().
4330Sstevel@tonic-gate  */
4340Sstevel@tonic-gate static struct phyint_instance *
4350Sstevel@tonic-gate phyint_inst_create(struct phyint *pi, int af)
4360Sstevel@tonic-gate {
4370Sstevel@tonic-gate 	struct phyint_instance *pii;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	pii = calloc(1, sizeof (struct phyint_instance));
4400Sstevel@tonic-gate 	if (pii == NULL) {
4410Sstevel@tonic-gate 		logperror("phyint_inst_create: calloc");
4420Sstevel@tonic-gate 		return (NULL);
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	/*
4460Sstevel@tonic-gate 	 * Attach the phyint instance to the phyint.
4470Sstevel@tonic-gate 	 * Set the back pointers as well
4480Sstevel@tonic-gate 	 */
4490Sstevel@tonic-gate 	pii->pii_phyint = pi;
4500Sstevel@tonic-gate 	if (af == AF_INET)
4510Sstevel@tonic-gate 		pi->pi_v4 = pii;
4520Sstevel@tonic-gate 	else
4530Sstevel@tonic-gate 		pi->pi_v6 = pii;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	pii->pii_in_use = 1;
4560Sstevel@tonic-gate 	pii->pii_probe_sock = -1;
4570Sstevel@tonic-gate 	pii->pii_snxt = 1;
4580Sstevel@tonic-gate 	pii->pii_af = af;
4590Sstevel@tonic-gate 	pii->pii_fd_hrtime = gethrtime() +
4600Sstevel@tonic-gate 	    (FAILURE_DETECTION_QP * (hrtime_t)NANOSEC);
4610Sstevel@tonic-gate 	pii->pii_flags = pi->pi_flags;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	/* Insert the phyint instance in the list of all phyint instances. */
4640Sstevel@tonic-gate 	phyint_inst_insert(pii);
4650Sstevel@tonic-gate 	return (pii);
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate /*
4690Sstevel@tonic-gate  * Change the state of phyint `pi' to state `state'.
4700Sstevel@tonic-gate  */
4710Sstevel@tonic-gate void
4720Sstevel@tonic-gate phyint_chstate(struct phyint *pi, enum pi_state state)
4730Sstevel@tonic-gate {
4740Sstevel@tonic-gate 	/*
4750Sstevel@tonic-gate 	 * To simplify things, some callers always set a given state
4760Sstevel@tonic-gate 	 * regardless of the previous state of the phyint (e.g., setting
4770Sstevel@tonic-gate 	 * PI_RUNNING when it's already set).  We shouldn't bother
4780Sstevel@tonic-gate 	 * generating an event or consuming a signature for these, since
4790Sstevel@tonic-gate 	 * the actual state of the interface is unchanged.
4800Sstevel@tonic-gate 	 */
4810Sstevel@tonic-gate 	if (pi->pi_state == state)
4820Sstevel@tonic-gate 		return;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	pi->pi_state = state;
485*8485SPeter.Memishian@Sun.COM 	phyint_changed(pi);
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate /*
489*8485SPeter.Memishian@Sun.COM  * Note that `pi' has changed state.
4900Sstevel@tonic-gate  */
4910Sstevel@tonic-gate void
492*8485SPeter.Memishian@Sun.COM phyint_changed(struct phyint *pi)
4930Sstevel@tonic-gate {
4940Sstevel@tonic-gate 	pi->pi_group->pg_sig++;
4950Sstevel@tonic-gate 	(void) phyint_state_event(pi->pi_group, pi);
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate /*
4990Sstevel@tonic-gate  * Insert the phyint group in the linked list of all phyint groups
5000Sstevel@tonic-gate  * at the head of the list
5010Sstevel@tonic-gate  */
502*8485SPeter.Memishian@Sun.COM void
5030Sstevel@tonic-gate phyint_group_insert(struct phyint_group *pg)
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate 	pg->pg_next = phyint_groups;
5060Sstevel@tonic-gate 	pg->pg_prev = NULL;
5070Sstevel@tonic-gate 	if (phyint_groups != NULL)
5080Sstevel@tonic-gate 		phyint_groups->pg_prev = pg;
5090Sstevel@tonic-gate 	phyint_groups = pg;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	phyint_grouplistsig++;
5120Sstevel@tonic-gate 	(void) phyint_group_change_event(pg, IPMP_GROUP_ADD);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate  * Create a new phyint group called 'name'.
5170Sstevel@tonic-gate  */
518*8485SPeter.Memishian@Sun.COM struct phyint_group *
5190Sstevel@tonic-gate phyint_group_create(const char *name)
5200Sstevel@tonic-gate {
5210Sstevel@tonic-gate 	struct	phyint_group *pg;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	if (debug & D_PHYINT)
5240Sstevel@tonic-gate 		logdebug("phyint_group_create(%s)\n", name);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	pg = calloc(1, sizeof (struct phyint_group));
5270Sstevel@tonic-gate 	if (pg == NULL) {
5280Sstevel@tonic-gate 		logperror("phyint_group_create: calloc");
5290Sstevel@tonic-gate 		return (NULL);
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 
5324770Smeem 	(void) strlcpy(pg->pg_name, name, sizeof (pg->pg_name));
5330Sstevel@tonic-gate 	pg->pg_sig = gensig();
5340Sstevel@tonic-gate 	pg->pg_fdt = user_failure_detection_time;
5350Sstevel@tonic-gate 	pg->pg_probeint = user_probe_interval;
536*8485SPeter.Memishian@Sun.COM 	pg->pg_in_use = _B_TRUE;
537*8485SPeter.Memishian@Sun.COM 
538*8485SPeter.Memishian@Sun.COM 	/*
539*8485SPeter.Memishian@Sun.COM 	 * Normal groups always start in the PG_FAILED state since they
540*8485SPeter.Memishian@Sun.COM 	 * have no active interfaces.  In contrast, anonymous groups are
541*8485SPeter.Memishian@Sun.COM 	 * heterogeneous and thus always PG_OK.
542*8485SPeter.Memishian@Sun.COM 	 */
543*8485SPeter.Memishian@Sun.COM 	pg->pg_state = (name[0] == '\0' ? PG_OK : PG_FAILED);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	return (pg);
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate /*
5490Sstevel@tonic-gate  * Change the state of the phyint group `pg' to state `state'.
5500Sstevel@tonic-gate  */
5510Sstevel@tonic-gate void
5520Sstevel@tonic-gate phyint_group_chstate(struct phyint_group *pg, enum pg_state state)
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate 	assert(pg != phyint_anongroup);
5550Sstevel@tonic-gate 
556*8485SPeter.Memishian@Sun.COM 	/*
557*8485SPeter.Memishian@Sun.COM 	 * To simplify things, some callers always set a given state
558*8485SPeter.Memishian@Sun.COM 	 * regardless of the previous state of the group (e.g., setting
559*8485SPeter.Memishian@Sun.COM 	 * PG_DEGRADED when it's already set).  We shouldn't bother
560*8485SPeter.Memishian@Sun.COM 	 * generating an event or consuming a signature for these, since
561*8485SPeter.Memishian@Sun.COM 	 * the actual state of the group is unchanged.
562*8485SPeter.Memishian@Sun.COM 	 */
563*8485SPeter.Memishian@Sun.COM 	if (pg->pg_state == state)
564*8485SPeter.Memishian@Sun.COM 		return;
565*8485SPeter.Memishian@Sun.COM 
566*8485SPeter.Memishian@Sun.COM 	pg->pg_state = state;
567*8485SPeter.Memishian@Sun.COM 
5680Sstevel@tonic-gate 	switch (state) {
5690Sstevel@tonic-gate 	case PG_FAILED:
5700Sstevel@tonic-gate 		/*
5710Sstevel@tonic-gate 		 * We can never know with certainty that a group has
5720Sstevel@tonic-gate 		 * failed.  It is possible that all known targets have
5730Sstevel@tonic-gate 		 * failed simultaneously, and new targets have come up
5740Sstevel@tonic-gate 		 * instead. If the targets are routers then router
5750Sstevel@tonic-gate 		 * discovery will kick in, and we will see the new routers
5760Sstevel@tonic-gate 		 * thru routing socket messages. But if the targets are
5770Sstevel@tonic-gate 		 * hosts, we have to discover it by multicast.	So flush
5780Sstevel@tonic-gate 		 * all the host targets. The next probe will send out a
5790Sstevel@tonic-gate 		 * multicast echo request. If this is a group failure, we
580*8485SPeter.Memishian@Sun.COM 		 * will still not see any response, otherwise the group
581*8485SPeter.Memishian@Sun.COM 		 * will be repaired after we get NUM_PROBE_REPAIRS
582*8485SPeter.Memishian@Sun.COM 		 * consecutive unicast replies on any phyint.
5830Sstevel@tonic-gate 		 */
5840Sstevel@tonic-gate 		target_flush_hosts(pg);
5850Sstevel@tonic-gate 		break;
5860Sstevel@tonic-gate 
587*8485SPeter.Memishian@Sun.COM 	case PG_OK:
588*8485SPeter.Memishian@Sun.COM 	case PG_DEGRADED:
5890Sstevel@tonic-gate 		break;
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	default:
5920Sstevel@tonic-gate 		logerr("phyint_group_chstate: invalid group state %d; "
5930Sstevel@tonic-gate 		    "aborting\n", state);
5940Sstevel@tonic-gate 		abort();
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	pg->pg_sig++;
5980Sstevel@tonic-gate 	(void) phyint_group_state_event(pg);
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate /*
6020Sstevel@tonic-gate  * Create a new phyint instance and initialize it from the values supplied by
6030Sstevel@tonic-gate  * the kernel. Always check for ENXIO before logging any error, because the
6040Sstevel@tonic-gate  * interface could have vanished after completion of SIOCGLIFCONF.
6050Sstevel@tonic-gate  * Return values:
6060Sstevel@tonic-gate  *	pointer to the phyint instance on success
6070Sstevel@tonic-gate  *	NULL on failure Eg. if the phyint instance is not found in the kernel
6080Sstevel@tonic-gate  */
6090Sstevel@tonic-gate struct phyint_instance *
6100Sstevel@tonic-gate phyint_inst_init_from_k(int af, char *pi_name)
6110Sstevel@tonic-gate {
6120Sstevel@tonic-gate 	char	pg_name[LIFNAMSIZ + 1];
6130Sstevel@tonic-gate 	int	ifsock;
6140Sstevel@tonic-gate 	uint_t	ifindex;
6150Sstevel@tonic-gate 	uint64_t	flags;
6160Sstevel@tonic-gate 	struct lifreq	lifr;
6170Sstevel@tonic-gate 	struct phyint	*pi;
6180Sstevel@tonic-gate 	struct phyint_instance	*pii;
6190Sstevel@tonic-gate 	boolean_t	pi_created;
6200Sstevel@tonic-gate 	struct phyint_group	*pg;
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate retry:
6230Sstevel@tonic-gate 	pii = NULL;
6240Sstevel@tonic-gate 	pi = NULL;
6250Sstevel@tonic-gate 	pg = NULL;
6260Sstevel@tonic-gate 	pi_created = _B_FALSE;
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	if (debug & D_PHYINT) {
6290Sstevel@tonic-gate 		logdebug("phyint_inst_init_from_k(%s %s)\n",
6300Sstevel@tonic-gate 		    AF_STR(af), pi_name);
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	assert(af == AF_INET || af == AF_INET6);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	/* Get the socket for doing ioctls */
6360Sstevel@tonic-gate 	ifsock = (af == AF_INET) ? ifsock_v4 : ifsock_v6;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/*
639*8485SPeter.Memishian@Sun.COM 	 * Get the interface flags.  Ignore virtual interfaces, IPMP
640*8485SPeter.Memishian@Sun.COM 	 * meta-interfaces, point-to-point interfaces, and interfaces
641*8485SPeter.Memishian@Sun.COM 	 * that can't support multicast.
6420Sstevel@tonic-gate 	 */
643*8485SPeter.Memishian@Sun.COM 	(void) strlcpy(lifr.lifr_name, pi_name, sizeof (lifr.lifr_name));
6440Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
6450Sstevel@tonic-gate 		if (errno != ENXIO) {
6460Sstevel@tonic-gate 			logperror("phyint_inst_init_from_k:"
6470Sstevel@tonic-gate 			    " ioctl (get flags)");
6480Sstevel@tonic-gate 		}
6490Sstevel@tonic-gate 		return (NULL);
6500Sstevel@tonic-gate 	}
6510Sstevel@tonic-gate 	flags = lifr.lifr_flags;
652*8485SPeter.Memishian@Sun.COM 	if (!(flags & IFF_MULTICAST) ||
653*8485SPeter.Memishian@Sun.COM 	    (flags & (IFF_VIRTUAL|IFF_IPMP|IFF_POINTOPOINT)))
6540Sstevel@tonic-gate 		return (NULL);
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	/*
6570Sstevel@tonic-gate 	 * Get the ifindex for recording later in our tables, in case we need
6580Sstevel@tonic-gate 	 * to create a new phyint.
6590Sstevel@tonic-gate 	 */
6600Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFINDEX, (char *)&lifr) < 0) {
6610Sstevel@tonic-gate 		if (errno != ENXIO) {
6620Sstevel@tonic-gate 			logperror("phyint_inst_init_from_k: "
6630Sstevel@tonic-gate 			    " ioctl (get lifindex)");
6640Sstevel@tonic-gate 		}
6650Sstevel@tonic-gate 		return (NULL);
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 	ifindex = lifr.lifr_index;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	/*
6700Sstevel@tonic-gate 	 * Get the phyint group name of this phyint, from the kernel.
6710Sstevel@tonic-gate 	 */
6720Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFGROUPNAME, (char *)&lifr) < 0) {
6730Sstevel@tonic-gate 		if (errno != ENXIO) {
6740Sstevel@tonic-gate 			logperror("phyint_inst_init_from_k: "
6750Sstevel@tonic-gate 			    "ioctl (get group name)");
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 		return (NULL);
6780Sstevel@tonic-gate 	}
679*8485SPeter.Memishian@Sun.COM 	(void) strlcpy(pg_name, lifr.lifr_groupname, sizeof (pg_name));
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	/*
6820Sstevel@tonic-gate 	 * If the phyint is not part of any group, pg_name is the
6830Sstevel@tonic-gate 	 * null string. If 'track_all_phyints' is false, there is no
6840Sstevel@tonic-gate 	 * need to create a phyint.
6850Sstevel@tonic-gate 	 */
6860Sstevel@tonic-gate 	if (pg_name[0] == '\0' && !track_all_phyints) {
6870Sstevel@tonic-gate 		/*
688*8485SPeter.Memishian@Sun.COM 		 * If the IFF_FAILED, IFF_INACTIVE, or IFF_OFFLINE flags are
689*8485SPeter.Memishian@Sun.COM 		 * set, reset them. These flags shouldn't be set if in.mpathd
690*8485SPeter.Memishian@Sun.COM 		 * isn't tracking the interface.
6910Sstevel@tonic-gate 		 */
692*8485SPeter.Memishian@Sun.COM 		if ((flags & (IFF_FAILED | IFF_INACTIVE | IFF_OFFLINE))) {
693*8485SPeter.Memishian@Sun.COM 			lifr.lifr_flags = flags &
694*8485SPeter.Memishian@Sun.COM 			    ~(IFF_FAILED | IFF_INACTIVE | IFF_OFFLINE);
6950Sstevel@tonic-gate 			if (ioctl(ifsock, SIOCSLIFFLAGS, (char *)&lifr) < 0) {
6960Sstevel@tonic-gate 				if (errno != ENXIO) {
6970Sstevel@tonic-gate 					logperror("phyint_inst_init_from_k:"
6980Sstevel@tonic-gate 					    " ioctl (set flags)");
6990Sstevel@tonic-gate 				}
7000Sstevel@tonic-gate 			}
7010Sstevel@tonic-gate 		}
7020Sstevel@tonic-gate 		return (NULL);
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	/*
706*8485SPeter.Memishian@Sun.COM 	 * We need to create a new phyint instance.  We may also need to
707*8485SPeter.Memishian@Sun.COM 	 * create the group if e.g. the SIOCGLIFCONF loop in initifs() found
708*8485SPeter.Memishian@Sun.COM 	 * an underlying interface before it found its IPMP meta-interface.
709*8485SPeter.Memishian@Sun.COM 	 * Note that we keep any created groups even if phyint_inst_from_k()
710*8485SPeter.Memishian@Sun.COM 	 * fails since a group's existence is not dependent on the ability of
711*8485SPeter.Memishian@Sun.COM 	 * in.mpathd to the track the group's interfaces.
7120Sstevel@tonic-gate 	 */
713*8485SPeter.Memishian@Sun.COM 	if ((pg = phyint_group_lookup(pg_name)) == NULL) {
714*8485SPeter.Memishian@Sun.COM 		if ((pg = phyint_group_create(pg_name)) == NULL) {
715*8485SPeter.Memishian@Sun.COM 			logerr("phyint_inst_init_from_k: cannot create group "
716*8485SPeter.Memishian@Sun.COM 			    "%s\n", pg_name);
7170Sstevel@tonic-gate 			return (NULL);
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate 		phyint_group_insert(pg);
7200Sstevel@tonic-gate 	}
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	/*
7230Sstevel@tonic-gate 	 * Lookup the phyint. If the phyint does not exist create it.
7240Sstevel@tonic-gate 	 */
7250Sstevel@tonic-gate 	pi = phyint_lookup(pi_name);
7260Sstevel@tonic-gate 	if (pi == NULL) {
7270Sstevel@tonic-gate 		pi = phyint_create(pi_name, pg, ifindex, flags);
7280Sstevel@tonic-gate 		if (pi == NULL) {
7290Sstevel@tonic-gate 			logerr("phyint_inst_init_from_k:"
7300Sstevel@tonic-gate 			    " unable to create phyint %s\n", pi_name);
7310Sstevel@tonic-gate 			return (NULL);
7320Sstevel@tonic-gate 		}
7330Sstevel@tonic-gate 		pi_created = _B_TRUE;
7340Sstevel@tonic-gate 	} else {
7350Sstevel@tonic-gate 		/* The phyint exists already. */
7360Sstevel@tonic-gate 		assert(pi_created == _B_FALSE);
7370Sstevel@tonic-gate 		/*
7380Sstevel@tonic-gate 		 * Normally we should see consistent values for the IPv4 and
7390Sstevel@tonic-gate 		 * IPv6 instances, for phyint properties. If we don't, it
7400Sstevel@tonic-gate 		 * means things have changed underneath us, and we should
7410Sstevel@tonic-gate 		 * resync our tables with the kernel. Check whether the
7420Sstevel@tonic-gate 		 * interface index has changed. If so, it is most likely
7430Sstevel@tonic-gate 		 * the interface has been unplumbed and replumbed,
7440Sstevel@tonic-gate 		 * while we are yet to update our tables. Do it now.
7450Sstevel@tonic-gate 		 */
7460Sstevel@tonic-gate 		if (pi->pi_ifindex != ifindex) {
7470Sstevel@tonic-gate 			phyint_inst_delete(PHYINT_INSTANCE(pi, AF_OTHER(af)));
7480Sstevel@tonic-gate 			goto retry;
7490Sstevel@tonic-gate 		}
7500Sstevel@tonic-gate 		assert(PHYINT_INSTANCE(pi, af) == NULL);
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 		/*
7530Sstevel@tonic-gate 		 * If the group name seen by the IPv4 and IPv6 instances
7540Sstevel@tonic-gate 		 * are different, it is most likely the groupname has
7550Sstevel@tonic-gate 		 * changed, while we are yet to update our tables. Do it now.
7560Sstevel@tonic-gate 		 */
7570Sstevel@tonic-gate 		if (strcmp(pi->pi_group->pg_name, pg_name) != 0) {
7580Sstevel@tonic-gate 			phyint_inst_delete(PHYINT_INSTANCE(pi,
7590Sstevel@tonic-gate 			    AF_OTHER(af)));
7600Sstevel@tonic-gate 			goto retry;
7610Sstevel@tonic-gate 		}
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	/*
7650Sstevel@tonic-gate 	 * Create a new phyint instance, corresponding to the 'af'
7660Sstevel@tonic-gate 	 * passed in.
7670Sstevel@tonic-gate 	 */
7680Sstevel@tonic-gate 	pii = phyint_inst_create(pi, af);
7690Sstevel@tonic-gate 	if (pii == NULL) {
7700Sstevel@tonic-gate 		logerr("phyint_inst_init_from_k: unable to create"
7710Sstevel@tonic-gate 		    "phyint inst %s\n", pi->pi_name);
772*8485SPeter.Memishian@Sun.COM 		if (pi_created)
7730Sstevel@tonic-gate 			phyint_delete(pi);
774*8485SPeter.Memishian@Sun.COM 
775*8485SPeter.Memishian@Sun.COM 		return (NULL);
776*8485SPeter.Memishian@Sun.COM 	}
777*8485SPeter.Memishian@Sun.COM 
778*8485SPeter.Memishian@Sun.COM 	if (pi_created) {
779*8485SPeter.Memishian@Sun.COM 		/*
780*8485SPeter.Memishian@Sun.COM 		 * If this phyint does not have a unique hardware address in its
781*8485SPeter.Memishian@Sun.COM 		 * group, offline it.  (The change_pif_flags() implementation
782*8485SPeter.Memishian@Sun.COM 		 * requires that we defer this until after the phyint_instance
783*8485SPeter.Memishian@Sun.COM 		 * is created.)
784*8485SPeter.Memishian@Sun.COM 		 */
785*8485SPeter.Memishian@Sun.COM 		if (phyint_lookup_hwaddr(pi, _B_TRUE) != NULL) {
786*8485SPeter.Memishian@Sun.COM 			pi->pi_hwaddrdup = _B_TRUE;
787*8485SPeter.Memishian@Sun.COM 			(void) phyint_offline(pi, 0);
7880Sstevel@tonic-gate 		}
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	return (pii);
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate /*
7952074Smeem  * Bind pii_probe_sock to the address associated with pii_probe_logint.
7962074Smeem  * This socket will be used for sending and receiving ICMP/ICMPv6 probes to
7972074Smeem  * targets. Do the common part in this function, and complete the
7982074Smeem  * initializations by calling the protocol specific functions
7990Sstevel@tonic-gate  * phyint_inst_v{4,6}_sockinit() respectively.
8000Sstevel@tonic-gate  *
8010Sstevel@tonic-gate  * Return values: _B_TRUE/_B_FALSE for success or failure respectively.
8020Sstevel@tonic-gate  */
8030Sstevel@tonic-gate boolean_t
8040Sstevel@tonic-gate phyint_inst_sockinit(struct phyint_instance *pii)
8050Sstevel@tonic-gate {
8060Sstevel@tonic-gate 	boolean_t success;
8070Sstevel@tonic-gate 	struct phyint_group *pg;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	if (debug & D_PHYINT) {
8100Sstevel@tonic-gate 		logdebug("phyint_inst_sockinit(%s %s)\n",
8110Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name);
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	assert(pii->pii_probe_logint != NULL);
8150Sstevel@tonic-gate 	assert(pii->pii_probe_logint->li_flags & IFF_UP);
8162074Smeem 	assert(pii->pii_probe_logint->li_flags & IFF_NOFAILOVER);
8170Sstevel@tonic-gate 	assert(pii->pii_af == AF_INET || pii->pii_af == AF_INET6);
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	/*
8200Sstevel@tonic-gate 	 * If the socket is already bound, close pii_probe_sock
8210Sstevel@tonic-gate 	 */
8220Sstevel@tonic-gate 	if (pii->pii_probe_sock != -1)
8230Sstevel@tonic-gate 		close_probe_socket(pii, _B_TRUE);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	/*
8260Sstevel@tonic-gate 	 * If the phyint is not part of a named group and track_all_phyints is
8270Sstevel@tonic-gate 	 * false, simply return.
8280Sstevel@tonic-gate 	 */
8290Sstevel@tonic-gate 	pg = pii->pii_phyint->pi_group;
8300Sstevel@tonic-gate 	if (pg == phyint_anongroup && !track_all_phyints) {
8310Sstevel@tonic-gate 		if (debug & D_PHYINT)
8320Sstevel@tonic-gate 			logdebug("phyint_inst_sockinit: no group\n");
8330Sstevel@tonic-gate 		return (_B_FALSE);
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	/*
8370Sstevel@tonic-gate 	 * Initialize the socket by calling the protocol specific function.
8380Sstevel@tonic-gate 	 * If it succeeds, add the socket to the poll list.
8390Sstevel@tonic-gate 	 */
8400Sstevel@tonic-gate 	if (pii->pii_af == AF_INET6)
8410Sstevel@tonic-gate 		success = phyint_inst_v6_sockinit(pii);
8420Sstevel@tonic-gate 	else
8430Sstevel@tonic-gate 		success = phyint_inst_v4_sockinit(pii);
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	if (success && (poll_add(pii->pii_probe_sock) == 0))
8460Sstevel@tonic-gate 		return (_B_TRUE);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	/* Something failed, cleanup and return false */
8490Sstevel@tonic-gate 	if (pii->pii_probe_sock != -1)
8500Sstevel@tonic-gate 		close_probe_socket(pii, _B_FALSE);
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	return (_B_FALSE);
8530Sstevel@tonic-gate }
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate /*
8560Sstevel@tonic-gate  * IPv6 specific part in initializing the pii_probe_sock. This socket is
8570Sstevel@tonic-gate  * used to send/receive ICMPv6 probe packets.
8580Sstevel@tonic-gate  */
8590Sstevel@tonic-gate static boolean_t
8600Sstevel@tonic-gate phyint_inst_v6_sockinit(struct phyint_instance *pii)
8610Sstevel@tonic-gate {
8620Sstevel@tonic-gate 	icmp6_filter_t filter;
8630Sstevel@tonic-gate 	int hopcount = 1;
864*8485SPeter.Memishian@Sun.COM 	int off = 0;
865*8485SPeter.Memishian@Sun.COM 	int on = 1;
8660Sstevel@tonic-gate 	struct	sockaddr_in6	testaddr;
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	/*
8690Sstevel@tonic-gate 	 * Open a raw socket with ICMPv6 protocol.
8700Sstevel@tonic-gate 	 *
871*8485SPeter.Memishian@Sun.COM 	 * Use IPV6_BOUND_IF to make sure that probes are sent and received on
872*8485SPeter.Memishian@Sun.COM 	 * the specified phyint only.  Bind to the test address to ensure that
873*8485SPeter.Memishian@Sun.COM 	 * the responses are sent to the specified phyint.
8740Sstevel@tonic-gate 	 *
8750Sstevel@tonic-gate 	 * Set the hopcount to 1 so that probe packets are not routed.
8760Sstevel@tonic-gate 	 * Disable multicast loopback. Set the receive filter to
8770Sstevel@tonic-gate 	 * receive only ICMPv6 echo replies.
8780Sstevel@tonic-gate 	 */
8790Sstevel@tonic-gate 	pii->pii_probe_sock = socket(pii->pii_af, SOCK_RAW, IPPROTO_ICMPV6);
8800Sstevel@tonic-gate 	if (pii->pii_probe_sock < 0) {
8810Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: socket");
8820Sstevel@tonic-gate 		return (_B_FALSE);
883*8485SPeter.Memishian@Sun.COM 	}
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	bzero(&testaddr, sizeof (testaddr));
8860Sstevel@tonic-gate 	testaddr.sin6_family = AF_INET6;
8870Sstevel@tonic-gate 	testaddr.sin6_port = 0;
8880Sstevel@tonic-gate 	testaddr.sin6_addr = pii->pii_probe_logint->li_addr;
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	if (bind(pii->pii_probe_sock, (struct sockaddr *)&testaddr,
8910Sstevel@tonic-gate 	    sizeof (testaddr)) < 0) {
8920Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: IPv6 bind");
8930Sstevel@tonic-gate 		return (_B_FALSE);
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate 
896*8485SPeter.Memishian@Sun.COM 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IPV6, IPV6_MULTICAST_IF,
8970Sstevel@tonic-gate 	    (char *)&pii->pii_ifindex, sizeof (uint_t)) < 0) {
8980Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
899*8485SPeter.Memishian@Sun.COM 		    " IPV6_MULTICAST_IF");
900*8485SPeter.Memishian@Sun.COM 		return (_B_FALSE);
901*8485SPeter.Memishian@Sun.COM 	}
902*8485SPeter.Memishian@Sun.COM 
903*8485SPeter.Memishian@Sun.COM 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IPV6, IPV6_BOUND_IF,
904*8485SPeter.Memishian@Sun.COM 	    &pii->pii_ifindex, sizeof (uint_t)) < 0) {
905*8485SPeter.Memishian@Sun.COM 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
906*8485SPeter.Memishian@Sun.COM 		    " IPV6_BOUND_IF");
9070Sstevel@tonic-gate 		return (_B_FALSE);
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
9110Sstevel@tonic-gate 	    (char *)&hopcount, sizeof (hopcount)) < 0) {
9120Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
9130Sstevel@tonic-gate 		    " IPV6_UNICAST_HOPS");
9140Sstevel@tonic-gate 		return (_B_FALSE);
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
9180Sstevel@tonic-gate 	    (char *)&hopcount, sizeof (hopcount)) < 0) {
9190Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
9200Sstevel@tonic-gate 		    " IPV6_MULTICAST_HOPS");
9210Sstevel@tonic-gate 		return (_B_FALSE);
9220Sstevel@tonic-gate 	}
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
925*8485SPeter.Memishian@Sun.COM 	    (char *)&off, sizeof (off)) < 0) {
9260Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
9270Sstevel@tonic-gate 		    " IPV6_MULTICAST_LOOP");
9280Sstevel@tonic-gate 		return (_B_FALSE);
9290Sstevel@tonic-gate 	}
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	/*
9320Sstevel@tonic-gate 	 * Filter out so that we only receive ICMP echo replies
9330Sstevel@tonic-gate 	 */
9340Sstevel@tonic-gate 	ICMP6_FILTER_SETBLOCKALL(&filter);
9350Sstevel@tonic-gate 	ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter);
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_ICMPV6, ICMP6_FILTER,
9380Sstevel@tonic-gate 	    (char *)&filter, sizeof (filter)) < 0) {
9390Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
9400Sstevel@tonic-gate 		    " ICMP6_FILTER");
9410Sstevel@tonic-gate 		return (_B_FALSE);
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 
944*8485SPeter.Memishian@Sun.COM 	/* Enable receipt of hoplimit */
9450Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
946*8485SPeter.Memishian@Sun.COM 	    &on, sizeof (on)) < 0) {
9470Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
9480Sstevel@tonic-gate 		    " IPV6_RECVHOPLIMIT");
9490Sstevel@tonic-gate 		return (_B_FALSE);
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 
952*8485SPeter.Memishian@Sun.COM 	/* Enable receipt of timestamp */
953*8485SPeter.Memishian@Sun.COM 	if (setsockopt(pii->pii_probe_sock, SOL_SOCKET, SO_TIMESTAMP,
954*8485SPeter.Memishian@Sun.COM 	    &on, sizeof (on)) < 0) {
955*8485SPeter.Memishian@Sun.COM 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
956*8485SPeter.Memishian@Sun.COM 		    " SO_TIMESTAMP");
957*8485SPeter.Memishian@Sun.COM 		return (_B_FALSE);
958*8485SPeter.Memishian@Sun.COM 	}
959*8485SPeter.Memishian@Sun.COM 
9600Sstevel@tonic-gate 	return (_B_TRUE);
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate /*
9640Sstevel@tonic-gate  * IPv4 specific part in initializing the pii_probe_sock. This socket is
9650Sstevel@tonic-gate  * used to send/receive ICMPv4 probe packets.
9660Sstevel@tonic-gate  */
9670Sstevel@tonic-gate static boolean_t
9680Sstevel@tonic-gate phyint_inst_v4_sockinit(struct phyint_instance *pii)
9690Sstevel@tonic-gate {
9700Sstevel@tonic-gate 	struct sockaddr_in  testaddr;
971*8485SPeter.Memishian@Sun.COM 	char	char_off = 0;
9720Sstevel@tonic-gate 	int	ttl = 1;
9730Sstevel@tonic-gate 	char	char_ttl = 1;
974*8485SPeter.Memishian@Sun.COM 	int	on = 1;
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	/*
9770Sstevel@tonic-gate 	 * Open a raw socket with ICMPv4 protocol.
9780Sstevel@tonic-gate 	 *
979*8485SPeter.Memishian@Sun.COM 	 * Use IP_BOUND_IF to make sure that probes are sent and received on
980*8485SPeter.Memishian@Sun.COM 	 * the specified phyint only.  Bind to the test address to ensure that
981*8485SPeter.Memishian@Sun.COM 	 * the responses are sent to the specified phyint.
9820Sstevel@tonic-gate 	 *
9830Sstevel@tonic-gate 	 * Set the ttl to 1 so that probe packets are not routed.
984*8485SPeter.Memishian@Sun.COM 	 * Disable multicast loopback.  Enable receipt of timestamp.
9850Sstevel@tonic-gate 	 */
9860Sstevel@tonic-gate 	pii->pii_probe_sock = socket(pii->pii_af, SOCK_RAW, IPPROTO_ICMP);
9870Sstevel@tonic-gate 	if (pii->pii_probe_sock < 0) {
9880Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v4_sockinit: socket");
9890Sstevel@tonic-gate 		return (_B_FALSE);
9900Sstevel@tonic-gate 	}
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	bzero(&testaddr, sizeof (testaddr));
9930Sstevel@tonic-gate 	testaddr.sin_family = AF_INET;
9940Sstevel@tonic-gate 	testaddr.sin_port = 0;
9950Sstevel@tonic-gate 	IN6_V4MAPPED_TO_INADDR(&pii->pii_probe_logint->li_addr,
9960Sstevel@tonic-gate 	    &testaddr.sin_addr);
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	if (bind(pii->pii_probe_sock, (struct sockaddr *)&testaddr,
9990Sstevel@tonic-gate 	    sizeof (testaddr)) < 0) {
10000Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v4_sockinit: IPv4 bind");
10010Sstevel@tonic-gate 		return (_B_FALSE);
10020Sstevel@tonic-gate 	}
10030Sstevel@tonic-gate 
1004*8485SPeter.Memishian@Sun.COM 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IP, IP_BOUND_IF,
1005*8485SPeter.Memishian@Sun.COM 	    &pii->pii_ifindex, sizeof (uint_t)) < 0) {
1006*8485SPeter.Memishian@Sun.COM 		logperror_pii(pii, "phyint_inst_v4_sockinit: setsockopt"
1007*8485SPeter.Memishian@Sun.COM 		    " IP_BOUND_IF");
1008*8485SPeter.Memishian@Sun.COM 		return (_B_FALSE);
1009*8485SPeter.Memishian@Sun.COM 	}
1010*8485SPeter.Memishian@Sun.COM 
1011*8485SPeter.Memishian@Sun.COM 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IP, IP_MULTICAST_IF,
10120Sstevel@tonic-gate 	    (char *)&testaddr.sin_addr, sizeof (struct in_addr)) < 0) {
10130Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v4_sockinit: setsockopt"
1014*8485SPeter.Memishian@Sun.COM 		    " IP_MULTICAST_IF");
10150Sstevel@tonic-gate 		return (_B_FALSE);
10160Sstevel@tonic-gate 	}
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IP, IP_TTL,
10190Sstevel@tonic-gate 	    (char *)&ttl, sizeof (ttl)) < 0) {
10200Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v4_sockinit: setsockopt"
10210Sstevel@tonic-gate 		    " IP_TTL");
10220Sstevel@tonic-gate 		return (_B_FALSE);
10230Sstevel@tonic-gate 	}
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IP, IP_MULTICAST_LOOP,
1026*8485SPeter.Memishian@Sun.COM 	    (char *)&char_off, sizeof (char_off)) == -1) {
10270Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v4_sockinit: setsockopt"
10280Sstevel@tonic-gate 		    " IP_MULTICAST_LOOP");
10290Sstevel@tonic-gate 		return (_B_FALSE);
10300Sstevel@tonic-gate 	}
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IP, IP_MULTICAST_TTL,
10330Sstevel@tonic-gate 	    (char *)&char_ttl, sizeof (char_ttl)) == -1) {
10340Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v4_sockinit: setsockopt"
10350Sstevel@tonic-gate 		    " IP_MULTICAST_TTL");
10360Sstevel@tonic-gate 		return (_B_FALSE);
10370Sstevel@tonic-gate 	}
10380Sstevel@tonic-gate 
1039*8485SPeter.Memishian@Sun.COM 	if (setsockopt(pii->pii_probe_sock, SOL_SOCKET, SO_TIMESTAMP, &on,
1040*8485SPeter.Memishian@Sun.COM 	    sizeof (on)) < 0) {
1041*8485SPeter.Memishian@Sun.COM 		logperror_pii(pii, "phyint_inst_v4_sockinit: setsockopt"
1042*8485SPeter.Memishian@Sun.COM 		    " SO_TIMESTAMP");
1043*8485SPeter.Memishian@Sun.COM 		return (_B_FALSE);
1044*8485SPeter.Memishian@Sun.COM 	}
1045*8485SPeter.Memishian@Sun.COM 
10460Sstevel@tonic-gate 	return (_B_TRUE);
10470Sstevel@tonic-gate }
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate /*
10500Sstevel@tonic-gate  * Remove the phyint group from the list of 'all phyint groups'
10510Sstevel@tonic-gate  * and free it.
10520Sstevel@tonic-gate  */
1053*8485SPeter.Memishian@Sun.COM void
10540Sstevel@tonic-gate phyint_group_delete(struct phyint_group *pg)
10550Sstevel@tonic-gate {
10560Sstevel@tonic-gate 	/*
10570Sstevel@tonic-gate 	 * The anonymous group always exists, even when empty.
10580Sstevel@tonic-gate 	 */
10590Sstevel@tonic-gate 	if (pg == phyint_anongroup)
10600Sstevel@tonic-gate 		return;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	if (debug & D_PHYINT)
10630Sstevel@tonic-gate 		logdebug("phyint_group_delete('%s')\n", pg->pg_name);
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	/*
10660Sstevel@tonic-gate 	 * The phyint group must be empty, and must not have any phyints.
10670Sstevel@tonic-gate 	 * The phyint group must be in the list of all phyint groups
10680Sstevel@tonic-gate 	 */
10690Sstevel@tonic-gate 	assert(pg->pg_phyint == NULL);
10700Sstevel@tonic-gate 	assert(phyint_groups == pg || pg->pg_prev != NULL);
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	if (pg->pg_prev != NULL)
10730Sstevel@tonic-gate 		pg->pg_prev->pg_next = pg->pg_next;
10740Sstevel@tonic-gate 	else
10750Sstevel@tonic-gate 		phyint_groups = pg->pg_next;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	if (pg->pg_next != NULL)
10780Sstevel@tonic-gate 		pg->pg_next->pg_prev = pg->pg_prev;
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	pg->pg_next = NULL;
10810Sstevel@tonic-gate 	pg->pg_prev = NULL;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	phyint_grouplistsig++;
10840Sstevel@tonic-gate 	(void) phyint_group_change_event(pg, IPMP_GROUP_REMOVE);
10850Sstevel@tonic-gate 
1086*8485SPeter.Memishian@Sun.COM 	addrlist_free(&pg->pg_addrs);
10870Sstevel@tonic-gate 	free(pg);
10880Sstevel@tonic-gate }
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate /*
1091*8485SPeter.Memishian@Sun.COM  * Refresh the state of `pg' based on its current members.
1092*8485SPeter.Memishian@Sun.COM  */
1093*8485SPeter.Memishian@Sun.COM void
1094*8485SPeter.Memishian@Sun.COM phyint_group_refresh_state(struct phyint_group *pg)
1095*8485SPeter.Memishian@Sun.COM {
1096*8485SPeter.Memishian@Sun.COM 	enum pg_state state;
1097*8485SPeter.Memishian@Sun.COM 	enum pg_state origstate = pg->pg_state;
1098*8485SPeter.Memishian@Sun.COM 	struct phyint *pi, *usablepi;
1099*8485SPeter.Memishian@Sun.COM 	uint_t nif = 0, nusable = 0;
1100*8485SPeter.Memishian@Sun.COM 
1101*8485SPeter.Memishian@Sun.COM 	/*
1102*8485SPeter.Memishian@Sun.COM 	 * Anonymous groups never change state.
1103*8485SPeter.Memishian@Sun.COM 	 */
1104*8485SPeter.Memishian@Sun.COM 	if (pg == phyint_anongroup)
1105*8485SPeter.Memishian@Sun.COM 		return;
1106*8485SPeter.Memishian@Sun.COM 
1107*8485SPeter.Memishian@Sun.COM 	for (pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext) {
1108*8485SPeter.Memishian@Sun.COM 		nif++;
1109*8485SPeter.Memishian@Sun.COM 		if (phyint_is_usable(pi)) {
1110*8485SPeter.Memishian@Sun.COM 			nusable++;
1111*8485SPeter.Memishian@Sun.COM 			usablepi = pi;
1112*8485SPeter.Memishian@Sun.COM 		}
1113*8485SPeter.Memishian@Sun.COM 	}
1114*8485SPeter.Memishian@Sun.COM 
1115*8485SPeter.Memishian@Sun.COM 	if (nusable == 0)
1116*8485SPeter.Memishian@Sun.COM 		state = PG_FAILED;
1117*8485SPeter.Memishian@Sun.COM 	else if (nif == nusable)
1118*8485SPeter.Memishian@Sun.COM 		state = PG_OK;
1119*8485SPeter.Memishian@Sun.COM 	else
1120*8485SPeter.Memishian@Sun.COM 		state = PG_DEGRADED;
1121*8485SPeter.Memishian@Sun.COM 
1122*8485SPeter.Memishian@Sun.COM 	phyint_group_chstate(pg, state);
1123*8485SPeter.Memishian@Sun.COM 
1124*8485SPeter.Memishian@Sun.COM 	/*
1125*8485SPeter.Memishian@Sun.COM 	 * If we're shutting down, skip logging messages since otherwise our
1126*8485SPeter.Memishian@Sun.COM 	 * shutdown housecleaning will make us report that groups are unusable.
1127*8485SPeter.Memishian@Sun.COM 	 */
1128*8485SPeter.Memishian@Sun.COM 	if (cleanup_started)
1129*8485SPeter.Memishian@Sun.COM 		return;
1130*8485SPeter.Memishian@Sun.COM 
1131*8485SPeter.Memishian@Sun.COM 	/*
1132*8485SPeter.Memishian@Sun.COM 	 * NOTE: We use pg_failmsg_printed rather than origstate since
1133*8485SPeter.Memishian@Sun.COM 	 * otherwise at startup we'll log a "now usable" message when the
1134*8485SPeter.Memishian@Sun.COM 	 * first usable phyint is added to an empty group.
1135*8485SPeter.Memishian@Sun.COM 	 */
1136*8485SPeter.Memishian@Sun.COM 	if (state != PG_FAILED && pg->pg_failmsg_printed) {
1137*8485SPeter.Memishian@Sun.COM 		assert(origstate == PG_FAILED);
1138*8485SPeter.Memishian@Sun.COM 		logerr("At least 1 IP interface (%s) in group %s is now "
1139*8485SPeter.Memishian@Sun.COM 		    "usable\n", usablepi->pi_name, pg->pg_name);
1140*8485SPeter.Memishian@Sun.COM 		pg->pg_failmsg_printed = _B_FALSE;
1141*8485SPeter.Memishian@Sun.COM 	} else if (origstate != PG_FAILED && state == PG_FAILED) {
1142*8485SPeter.Memishian@Sun.COM 		logerr("All IP interfaces in group %s are now unusable\n",
1143*8485SPeter.Memishian@Sun.COM 		    pg->pg_name);
1144*8485SPeter.Memishian@Sun.COM 		pg->pg_failmsg_printed = _B_TRUE;
1145*8485SPeter.Memishian@Sun.COM 	}
1146*8485SPeter.Memishian@Sun.COM }
1147*8485SPeter.Memishian@Sun.COM 
1148*8485SPeter.Memishian@Sun.COM /*
11490Sstevel@tonic-gate  * Extract information from the kernel about the desired phyint.
11500Sstevel@tonic-gate  * Look only for properties of the phyint and not properties of logints.
11510Sstevel@tonic-gate  * Take appropriate action on the changes.
11520Sstevel@tonic-gate  * Return codes:
11530Sstevel@tonic-gate  *	PI_OK
11540Sstevel@tonic-gate  *		The phyint exists in the kernel and matches our knowledge
11550Sstevel@tonic-gate  *		of the phyint.
11560Sstevel@tonic-gate  *	PI_DELETED
11570Sstevel@tonic-gate  *		The phyint has vanished in the kernel.
11580Sstevel@tonic-gate  *	PI_IFINDEX_CHANGED
11590Sstevel@tonic-gate  *		The phyint's interface index has changed.
11600Sstevel@tonic-gate  *		Ask the caller to delete and recreate the phyint.
11610Sstevel@tonic-gate  *	PI_IOCTL_ERROR
11620Sstevel@tonic-gate  *		Some ioctl error. Don't change anything.
11630Sstevel@tonic-gate  *	PI_GROUP_CHANGED
11640Sstevel@tonic-gate  *		The phyint has changed group.
11650Sstevel@tonic-gate  */
11660Sstevel@tonic-gate int
11670Sstevel@tonic-gate phyint_inst_update_from_k(struct phyint_instance *pii)
11680Sstevel@tonic-gate {
11690Sstevel@tonic-gate 	struct lifreq lifr;
11700Sstevel@tonic-gate 	int	ifsock;
11710Sstevel@tonic-gate 	struct phyint *pi;
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	pi = pii->pii_phyint;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	if (debug & D_PHYINT) {
11760Sstevel@tonic-gate 		logdebug("phyint_inst_update_from_k(%s %s)\n",
11770Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pi->pi_name);
11780Sstevel@tonic-gate 	}
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	/*
11810Sstevel@tonic-gate 	 * Get the ifindex from the kernel, for comparison with the
11820Sstevel@tonic-gate 	 * value in our tables.
11830Sstevel@tonic-gate 	 */
11840Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, pi->pi_name, sizeof (lifr.lifr_name));
11850Sstevel@tonic-gate 	lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	ifsock = (pii->pii_af == AF_INET) ? ifsock_v4 : ifsock_v6;
11880Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFINDEX, &lifr) < 0) {
11890Sstevel@tonic-gate 		if (errno == ENXIO) {
11900Sstevel@tonic-gate 			return (PI_DELETED);
11910Sstevel@tonic-gate 		} else {
11920Sstevel@tonic-gate 			logperror_pii(pii, "phyint_inst_update_from_k:"
11930Sstevel@tonic-gate 			    " ioctl (get lifindex)");
11940Sstevel@tonic-gate 			return (PI_IOCTL_ERROR);
11950Sstevel@tonic-gate 		}
11960Sstevel@tonic-gate 	}
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	if (lifr.lifr_index != pi->pi_ifindex) {
11990Sstevel@tonic-gate 		/*
12000Sstevel@tonic-gate 		 * The index has changed. Most likely the interface has
12010Sstevel@tonic-gate 		 * been unplumbed and replumbed. Ask the caller to take
12020Sstevel@tonic-gate 		 * appropriate action.
12030Sstevel@tonic-gate 		 */
12040Sstevel@tonic-gate 		if (debug & D_PHYINT) {
12050Sstevel@tonic-gate 			logdebug("phyint_inst_update_from_k:"
12060Sstevel@tonic-gate 			    " old index %d new index %d\n",
12070Sstevel@tonic-gate 			    pi->pi_ifindex, lifr.lifr_index);
12080Sstevel@tonic-gate 		}
12090Sstevel@tonic-gate 		return (PI_IFINDEX_CHANGED);
12100Sstevel@tonic-gate 	}
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	/*
12130Sstevel@tonic-gate 	 * Get the group name from the kernel, for comparison with
12140Sstevel@tonic-gate 	 * the value in our tables.
12150Sstevel@tonic-gate 	 */
12160Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFGROUPNAME, &lifr) < 0) {
12170Sstevel@tonic-gate 		if (errno == ENXIO) {
12180Sstevel@tonic-gate 			return (PI_DELETED);
12190Sstevel@tonic-gate 		} else {
12200Sstevel@tonic-gate 			logperror_pii(pii, "phyint_inst_update_from_k:"
12210Sstevel@tonic-gate 			    " ioctl (get groupname)");
12220Sstevel@tonic-gate 			return (PI_IOCTL_ERROR);
12230Sstevel@tonic-gate 		}
12240Sstevel@tonic-gate 	}
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	/*
12270Sstevel@tonic-gate 	 * If the phyint has changed group i.e. if the phyint group name
12280Sstevel@tonic-gate 	 * returned by the kernel is different, ask the caller to delete
12290Sstevel@tonic-gate 	 * and recreate the phyint in the right group
12300Sstevel@tonic-gate 	 */
12310Sstevel@tonic-gate 	if (strcmp(lifr.lifr_groupname, pi->pi_group->pg_name) != 0) {
12320Sstevel@tonic-gate 		/* Groupname has changed */
12330Sstevel@tonic-gate 		if (debug & D_PHYINT) {
12340Sstevel@tonic-gate 			logdebug("phyint_inst_update_from_k:"
12350Sstevel@tonic-gate 			    " groupname change\n");
12360Sstevel@tonic-gate 		}
12370Sstevel@tonic-gate 		return (PI_GROUP_CHANGED);
12380Sstevel@tonic-gate 	}
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	/*
12410Sstevel@tonic-gate 	 * Get the current phyint flags from the kernel, and determine what
12420Sstevel@tonic-gate 	 * flags have changed by comparing against our tables.	Note that the
12430Sstevel@tonic-gate 	 * IFF_INACTIVE processing in initifs() relies on this call to ensure
12440Sstevel@tonic-gate 	 * that IFF_INACTIVE is really still set on the interface.
12450Sstevel@tonic-gate 	 */
12460Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFFLAGS, &lifr) < 0) {
12470Sstevel@tonic-gate 		if (errno == ENXIO) {
12480Sstevel@tonic-gate 			return (PI_DELETED);
12490Sstevel@tonic-gate 		} else {
12500Sstevel@tonic-gate 			logperror_pii(pii, "phyint_inst_update_from_k: "
12510Sstevel@tonic-gate 			    " ioctl (get flags)");
12520Sstevel@tonic-gate 			return (PI_IOCTL_ERROR);
12530Sstevel@tonic-gate 		}
12540Sstevel@tonic-gate 	}
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	pi->pi_flags = PHYINT_FLAGS(lifr.lifr_flags);
12570Sstevel@tonic-gate 	if (pi->pi_v4 != NULL)
12580Sstevel@tonic-gate 		pi->pi_v4->pii_flags = pi->pi_flags;
12590Sstevel@tonic-gate 	if (pi->pi_v6 != NULL)
12600Sstevel@tonic-gate 		pi->pi_v6->pii_flags = pi->pi_flags;
12610Sstevel@tonic-gate 
1262*8485SPeter.Memishian@Sun.COM 	/*
1263*8485SPeter.Memishian@Sun.COM 	 * Make sure the IFF_FAILED flag is set if and only if we think
1264*8485SPeter.Memishian@Sun.COM 	 * the interface should be failed.
1265*8485SPeter.Memishian@Sun.COM 	 */
12660Sstevel@tonic-gate 	if (pi->pi_flags & IFF_FAILED) {
1267*8485SPeter.Memishian@Sun.COM 		if (pi->pi_state == PI_RUNNING)
1268*8485SPeter.Memishian@Sun.COM 			(void) change_pif_flags(pi, 0, IFF_FAILED);
12690Sstevel@tonic-gate 	} else {
1270*8485SPeter.Memishian@Sun.COM 		if (pi->pi_state == PI_FAILED)
1271*8485SPeter.Memishian@Sun.COM 			(void) change_pif_flags(pi, IFF_FAILED, IFF_INACTIVE);
12720Sstevel@tonic-gate 	}
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	/* No change in phyint status */
12750Sstevel@tonic-gate 	return (PI_OK);
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate /*
12790Sstevel@tonic-gate  * Delete the phyint. Remove it from the list of all phyints, and the
1280*8485SPeter.Memishian@Sun.COM  * list of phyint group members.
12810Sstevel@tonic-gate  */
12820Sstevel@tonic-gate static void
12830Sstevel@tonic-gate phyint_delete(struct phyint *pi)
12840Sstevel@tonic-gate {
1285*8485SPeter.Memishian@Sun.COM 	struct phyint *pi2;
12860Sstevel@tonic-gate 	struct phyint_group *pg = pi->pi_group;
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	if (debug & D_PHYINT)
12890Sstevel@tonic-gate 		logdebug("phyint_delete(%s)\n", pi->pi_name);
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	/* Both IPv4 and IPv6 phyint instances must have been deleted. */
12920Sstevel@tonic-gate 	assert(pi->pi_v4 == NULL && pi->pi_v6 == NULL);
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 	/*
12950Sstevel@tonic-gate 	 * The phyint must belong to a group.
12960Sstevel@tonic-gate 	 */
12970Sstevel@tonic-gate 	assert(pg->pg_phyint == pi || pi->pi_pgprev != NULL);
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	/* The phyint must be in the list of all phyints */
13000Sstevel@tonic-gate 	assert(phyints == pi || pi->pi_prev != NULL);
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	/* Remove the phyint from the phyint group list */
13030Sstevel@tonic-gate 	pg->pg_sig++;
13040Sstevel@tonic-gate 	(void) phyint_group_member_event(pg, pi, IPMP_IF_REMOVE);
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 	if (pi->pi_pgprev == NULL) {
13070Sstevel@tonic-gate 		/* Phyint is the 1st in the phyint group list */
13080Sstevel@tonic-gate 		pg->pg_phyint = pi->pi_pgnext;
13090Sstevel@tonic-gate 	} else {
13100Sstevel@tonic-gate 		pi->pi_pgprev->pi_pgnext = pi->pi_pgnext;
13110Sstevel@tonic-gate 	}
13120Sstevel@tonic-gate 	if (pi->pi_pgnext != NULL)
13130Sstevel@tonic-gate 		pi->pi_pgnext->pi_pgprev = pi->pi_pgprev;
13140Sstevel@tonic-gate 	pi->pi_pgnext = NULL;
13150Sstevel@tonic-gate 	pi->pi_pgprev = NULL;
13160Sstevel@tonic-gate 
1317*8485SPeter.Memishian@Sun.COM 	/* Refresh the group state now that this phyint has been removed */
1318*8485SPeter.Memishian@Sun.COM 	phyint_group_refresh_state(pg);
1319*8485SPeter.Memishian@Sun.COM 
13200Sstevel@tonic-gate 	/* Remove the phyint from the global list of phyints */
13210Sstevel@tonic-gate 	if (pi->pi_prev == NULL) {
13220Sstevel@tonic-gate 		/* Phyint is the 1st in the list */
13230Sstevel@tonic-gate 		phyints = pi->pi_next;
13240Sstevel@tonic-gate 	} else {
13250Sstevel@tonic-gate 		pi->pi_prev->pi_next = pi->pi_next;
13260Sstevel@tonic-gate 	}
13270Sstevel@tonic-gate 	if (pi->pi_next != NULL)
13280Sstevel@tonic-gate 		pi->pi_next->pi_prev = pi->pi_prev;
13290Sstevel@tonic-gate 	pi->pi_next = NULL;
13300Sstevel@tonic-gate 	pi->pi_prev = NULL;
13310Sstevel@tonic-gate 
1332*8485SPeter.Memishian@Sun.COM 	/*
1333*8485SPeter.Memishian@Sun.COM 	 * See if another phyint in the group had been offlined because
1334*8485SPeter.Memishian@Sun.COM 	 * it was a dup of `pi' -- and if so, online it.
1335*8485SPeter.Memishian@Sun.COM 	 */
1336*8485SPeter.Memishian@Sun.COM 	if (!pi->pi_hwaddrdup &&
1337*8485SPeter.Memishian@Sun.COM 	    (pi2 = phyint_lookup_hwaddr(pi, _B_FALSE)) != NULL) {
1338*8485SPeter.Memishian@Sun.COM 		assert(pi2->pi_hwaddrdup);
1339*8485SPeter.Memishian@Sun.COM 		(void) phyint_undo_offline(pi2);
1340*8485SPeter.Memishian@Sun.COM 	}
1341*8485SPeter.Memishian@Sun.COM 	phyint_link_close(pi);
13420Sstevel@tonic-gate 	free(pi);
1343*8485SPeter.Memishian@Sun.COM }
1344*8485SPeter.Memishian@Sun.COM 
1345*8485SPeter.Memishian@Sun.COM /*
1346*8485SPeter.Memishian@Sun.COM  * Offline phyint `pi' if at least `minred' usable interfaces remain in the
1347*8485SPeter.Memishian@Sun.COM  * group.  Returns an IPMP error code.
1348*8485SPeter.Memishian@Sun.COM  */
1349*8485SPeter.Memishian@Sun.COM int
1350*8485SPeter.Memishian@Sun.COM phyint_offline(struct phyint *pi, uint_t minred)
1351*8485SPeter.Memishian@Sun.COM {
1352*8485SPeter.Memishian@Sun.COM 	unsigned int nusable = 0;
1353*8485SPeter.Memishian@Sun.COM 	struct phyint *pi2;
1354*8485SPeter.Memishian@Sun.COM 	struct phyint_group *pg = pi->pi_group;
1355*8485SPeter.Memishian@Sun.COM 
1356*8485SPeter.Memishian@Sun.COM 	/*
1357*8485SPeter.Memishian@Sun.COM 	 * Verify that enough usable interfaces in the group would remain.
1358*8485SPeter.Memishian@Sun.COM 	 * As a special case, if the group has failed, allow any non-offline
1359*8485SPeter.Memishian@Sun.COM 	 * phyints to be offlined.
1360*8485SPeter.Memishian@Sun.COM 	 */
1361*8485SPeter.Memishian@Sun.COM 	if (pg != phyint_anongroup) {
1362*8485SPeter.Memishian@Sun.COM 		for (pi2 = pg->pg_phyint; pi2 != NULL; pi2 = pi2->pi_pgnext) {
1363*8485SPeter.Memishian@Sun.COM 			if (pi2 == pi)
1364*8485SPeter.Memishian@Sun.COM 				continue;
1365*8485SPeter.Memishian@Sun.COM 			if (phyint_is_usable(pi2) ||
1366*8485SPeter.Memishian@Sun.COM 			    (GROUP_FAILED(pg) && pi2->pi_state != PI_OFFLINE))
1367*8485SPeter.Memishian@Sun.COM 				nusable++;
1368*8485SPeter.Memishian@Sun.COM 		}
1369*8485SPeter.Memishian@Sun.COM 	}
1370*8485SPeter.Memishian@Sun.COM 	if (nusable < minred)
1371*8485SPeter.Memishian@Sun.COM 		return (IPMP_EMINRED);
1372*8485SPeter.Memishian@Sun.COM 
1373*8485SPeter.Memishian@Sun.COM 	if (!change_pif_flags(pi, IFF_OFFLINE, 0))
1374*8485SPeter.Memishian@Sun.COM 		return (IPMP_FAILURE);
1375*8485SPeter.Memishian@Sun.COM 
1376*8485SPeter.Memishian@Sun.COM 	/*
1377*8485SPeter.Memishian@Sun.COM 	 * The interface is now offline, so stop probing it.  Note that
1378*8485SPeter.Memishian@Sun.COM 	 * if_mpadm(1M) will down the test addresses, after receiving a
1379*8485SPeter.Memishian@Sun.COM 	 * success reply from us. The routing socket message will then make us
1380*8485SPeter.Memishian@Sun.COM 	 * close the socket used for sending probes. But it is more logical
1381*8485SPeter.Memishian@Sun.COM 	 * that an offlined interface must not be probed, even if it has test
1382*8485SPeter.Memishian@Sun.COM 	 * addresses.
1383*8485SPeter.Memishian@Sun.COM 	 *
1384*8485SPeter.Memishian@Sun.COM 	 * NOTE: stop_probing() also sets PI_OFFLINE.
1385*8485SPeter.Memishian@Sun.COM 	 */
1386*8485SPeter.Memishian@Sun.COM 	stop_probing(pi);
1387*8485SPeter.Memishian@Sun.COM 
1388*8485SPeter.Memishian@Sun.COM 	/*
1389*8485SPeter.Memishian@Sun.COM 	 * If we're offlining the phyint because it has a duplicate hardware
1390*8485SPeter.Memishian@Sun.COM 	 * address, print a warning -- and leave the link open so that we can
1391*8485SPeter.Memishian@Sun.COM 	 * be notified of hardware address changes that make it usable again.
1392*8485SPeter.Memishian@Sun.COM 	 * Otherwise, close the link so that we won't prevent a detach.
1393*8485SPeter.Memishian@Sun.COM 	 */
1394*8485SPeter.Memishian@Sun.COM 	if (pi->pi_hwaddrdup) {
1395*8485SPeter.Memishian@Sun.COM 		logerr("IP interface %s has a hardware address which is not "
1396*8485SPeter.Memishian@Sun.COM 		    "unique in group %s; offlining\n", pi->pi_name,
1397*8485SPeter.Memishian@Sun.COM 		    pg->pg_name);
1398*8485SPeter.Memishian@Sun.COM 	} else {
1399*8485SPeter.Memishian@Sun.COM 		phyint_link_close(pi);
1400*8485SPeter.Memishian@Sun.COM 	}
1401*8485SPeter.Memishian@Sun.COM 
1402*8485SPeter.Memishian@Sun.COM 	/*
1403*8485SPeter.Memishian@Sun.COM 	 * If this phyint was preventing another phyint with a duplicate
1404*8485SPeter.Memishian@Sun.COM 	 * hardware address from being online, bring that one online now.
1405*8485SPeter.Memishian@Sun.COM 	 */
1406*8485SPeter.Memishian@Sun.COM 	if (!pi->pi_hwaddrdup &&
1407*8485SPeter.Memishian@Sun.COM 	    (pi2 = phyint_lookup_hwaddr(pi, _B_FALSE)) != NULL) {
1408*8485SPeter.Memishian@Sun.COM 		assert(pi2->pi_hwaddrdup);
1409*8485SPeter.Memishian@Sun.COM 		(void) phyint_undo_offline(pi2);
1410*8485SPeter.Memishian@Sun.COM 	}
1411*8485SPeter.Memishian@Sun.COM 
1412*8485SPeter.Memishian@Sun.COM 	/*
1413*8485SPeter.Memishian@Sun.COM 	 * If this interface was active, try to activate another INACTIVE
1414*8485SPeter.Memishian@Sun.COM 	 * interface in the group.
1415*8485SPeter.Memishian@Sun.COM 	 */
1416*8485SPeter.Memishian@Sun.COM 	if (!(pi->pi_flags & IFF_INACTIVE))
1417*8485SPeter.Memishian@Sun.COM 		phyint_activate_another(pi);
1418*8485SPeter.Memishian@Sun.COM 
1419*8485SPeter.Memishian@Sun.COM 	return (IPMP_SUCCESS);
1420*8485SPeter.Memishian@Sun.COM }
1421*8485SPeter.Memishian@Sun.COM 
1422*8485SPeter.Memishian@Sun.COM /*
1423*8485SPeter.Memishian@Sun.COM  * Undo a previous offline of `pi'.  Returns an IPMP error code.
1424*8485SPeter.Memishian@Sun.COM  */
1425*8485SPeter.Memishian@Sun.COM int
1426*8485SPeter.Memishian@Sun.COM phyint_undo_offline(struct phyint *pi)
1427*8485SPeter.Memishian@Sun.COM {
1428*8485SPeter.Memishian@Sun.COM 	if (pi->pi_state != PI_OFFLINE) {
1429*8485SPeter.Memishian@Sun.COM 		errno = EINVAL;
1430*8485SPeter.Memishian@Sun.COM 		return (IPMP_FAILURE);
1431*8485SPeter.Memishian@Sun.COM 	}
1432*8485SPeter.Memishian@Sun.COM 
1433*8485SPeter.Memishian@Sun.COM 	/*
1434*8485SPeter.Memishian@Sun.COM 	 * If necessary, reinitialize our link information and verify that its
1435*8485SPeter.Memishian@Sun.COM 	 * hardware address is still unique across the group.
1436*8485SPeter.Memishian@Sun.COM 	 */
1437*8485SPeter.Memishian@Sun.COM 	if (pi->pi_dh == NULL && !phyint_link_init(pi)) {
1438*8485SPeter.Memishian@Sun.COM 		errno = EIO;
1439*8485SPeter.Memishian@Sun.COM 		return (IPMP_FAILURE);
1440*8485SPeter.Memishian@Sun.COM 	}
1441*8485SPeter.Memishian@Sun.COM 
1442*8485SPeter.Memishian@Sun.COM 	if (phyint_lookup_hwaddr(pi, _B_TRUE) != NULL) {
1443*8485SPeter.Memishian@Sun.COM 		pi->pi_hwaddrdup = _B_TRUE;
1444*8485SPeter.Memishian@Sun.COM 		return (IPMP_EHWADDRDUP);
1445*8485SPeter.Memishian@Sun.COM 	}
1446*8485SPeter.Memishian@Sun.COM 
1447*8485SPeter.Memishian@Sun.COM 	if (pi->pi_hwaddrdup) {
1448*8485SPeter.Memishian@Sun.COM 		logerr("IP interface %s now has a unique hardware address in "
1449*8485SPeter.Memishian@Sun.COM 		    "group %s; onlining\n", pi->pi_name, pi->pi_group->pg_name);
1450*8485SPeter.Memishian@Sun.COM 		pi->pi_hwaddrdup = _B_FALSE;
1451*8485SPeter.Memishian@Sun.COM 	}
1452*8485SPeter.Memishian@Sun.COM 
1453*8485SPeter.Memishian@Sun.COM 	if (!change_pif_flags(pi, 0, IFF_OFFLINE))
1454*8485SPeter.Memishian@Sun.COM 		return (IPMP_FAILURE);
1455*8485SPeter.Memishian@Sun.COM 
1456*8485SPeter.Memishian@Sun.COM 	/*
1457*8485SPeter.Memishian@Sun.COM 	 * While the interface was offline, it may have failed (e.g. the link
1458*8485SPeter.Memishian@Sun.COM 	 * may have gone down).  phyint_inst_check_for_failure() will have
1459*8485SPeter.Memishian@Sun.COM 	 * already set pi_flags with IFF_FAILED, so we can use that to decide
1460*8485SPeter.Memishian@Sun.COM 	 * whether the phyint should transition to running.  Note that after
1461*8485SPeter.Memishian@Sun.COM 	 * we transition to running, we will start sending probes again (if
1462*8485SPeter.Memishian@Sun.COM 	 * test addresses are configured), which may also reveal that the
1463*8485SPeter.Memishian@Sun.COM 	 * interface is in fact failed.
1464*8485SPeter.Memishian@Sun.COM 	 */
1465*8485SPeter.Memishian@Sun.COM 	if (pi->pi_flags & IFF_FAILED) {
1466*8485SPeter.Memishian@Sun.COM 		phyint_chstate(pi, PI_FAILED);
1467*8485SPeter.Memishian@Sun.COM 	} else {
1468*8485SPeter.Memishian@Sun.COM 		/* calls phyint_chstate() */
1469*8485SPeter.Memishian@Sun.COM 		phyint_transition_to_running(pi);
1470*8485SPeter.Memishian@Sun.COM 	}
1471*8485SPeter.Memishian@Sun.COM 
1472*8485SPeter.Memishian@Sun.COM 	/*
1473*8485SPeter.Memishian@Sun.COM 	 * Give the requestor time to configure test addresses before
1474*8485SPeter.Memishian@Sun.COM 	 * complaining that they're missing.
1475*8485SPeter.Memishian@Sun.COM 	 */
1476*8485SPeter.Memishian@Sun.COM 	pi->pi_taddrthresh = getcurrentsec() + TESTADDR_CONF_TIME;
1477*8485SPeter.Memishian@Sun.COM 
1478*8485SPeter.Memishian@Sun.COM 	return (IPMP_SUCCESS);
14790Sstevel@tonic-gate }
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate /*
14820Sstevel@tonic-gate  * Delete (unlink and free), the phyint instance.
14830Sstevel@tonic-gate  */
14840Sstevel@tonic-gate void
14850Sstevel@tonic-gate phyint_inst_delete(struct phyint_instance *pii)
14860Sstevel@tonic-gate {
14870Sstevel@tonic-gate 	struct phyint *pi = pii->pii_phyint;
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 	assert(pi != NULL);
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate 	if (debug & D_PHYINT) {
14920Sstevel@tonic-gate 		logdebug("phyint_inst_delete(%s %s)\n",
14930Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pi->pi_name);
14940Sstevel@tonic-gate 	}
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	/*
14970Sstevel@tonic-gate 	 * If the phyint instance has associated probe targets
14980Sstevel@tonic-gate 	 * delete all the targets
14990Sstevel@tonic-gate 	 */
15000Sstevel@tonic-gate 	while (pii->pii_targets != NULL)
15010Sstevel@tonic-gate 		target_delete(pii->pii_targets);
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 	/*
15040Sstevel@tonic-gate 	 * Delete all the logints associated with this phyint
15050Sstevel@tonic-gate 	 * instance.
15060Sstevel@tonic-gate 	 */
15070Sstevel@tonic-gate 	while (pii->pii_logint != NULL)
15080Sstevel@tonic-gate 		logint_delete(pii->pii_logint);
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	/*
15112074Smeem 	 * Close the socket used to send probes to targets from this phyint.
15120Sstevel@tonic-gate 	 */
15130Sstevel@tonic-gate 	if (pii->pii_probe_sock != -1)
15140Sstevel@tonic-gate 		close_probe_socket(pii, _B_TRUE);
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	/*
15170Sstevel@tonic-gate 	 * Phyint instance must be in the list of all phyint instances.
15180Sstevel@tonic-gate 	 * Remove phyint instance from the global list of phyint instances.
15190Sstevel@tonic-gate 	 */
15200Sstevel@tonic-gate 	assert(phyint_instances == pii || pii->pii_prev != NULL);
15210Sstevel@tonic-gate 	if (pii->pii_prev == NULL) {
15220Sstevel@tonic-gate 		/* Phyint is the 1st in the list */
15230Sstevel@tonic-gate 		phyint_instances = pii->pii_next;
15240Sstevel@tonic-gate 	} else {
15250Sstevel@tonic-gate 		pii->pii_prev->pii_next = pii->pii_next;
15260Sstevel@tonic-gate 	}
15270Sstevel@tonic-gate 	if (pii->pii_next != NULL)
15280Sstevel@tonic-gate 		pii->pii_next->pii_prev = pii->pii_prev;
15290Sstevel@tonic-gate 	pii->pii_next = NULL;
15300Sstevel@tonic-gate 	pii->pii_prev = NULL;
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	/*
15330Sstevel@tonic-gate 	 * Reset the phyint instance pointer in the phyint.
15340Sstevel@tonic-gate 	 * If this is the last phyint instance (being deleted) on this
15350Sstevel@tonic-gate 	 * phyint, then delete the phyint.
15360Sstevel@tonic-gate 	 */
15370Sstevel@tonic-gate 	if (pii->pii_af == AF_INET)
15380Sstevel@tonic-gate 		pi->pi_v4 = NULL;
15390Sstevel@tonic-gate 	else
15400Sstevel@tonic-gate 		pi->pi_v6 = NULL;
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	if (pi->pi_v4 == NULL && pi->pi_v6 == NULL)
15430Sstevel@tonic-gate 		phyint_delete(pi);
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 	free(pii);
15460Sstevel@tonic-gate }
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate static void
15490Sstevel@tonic-gate phyint_inst_print(struct phyint_instance *pii)
15500Sstevel@tonic-gate {
15510Sstevel@tonic-gate 	struct logint *li;
15520Sstevel@tonic-gate 	struct target *tg;
15530Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
15540Sstevel@tonic-gate 	int most_recent;
15550Sstevel@tonic-gate 	int i;
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	if (pii->pii_phyint == NULL) {
15580Sstevel@tonic-gate 		logdebug("pii->pi_phyint NULL can't print\n");
15590Sstevel@tonic-gate 		return;
15600Sstevel@tonic-gate 	}
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 	logdebug("\nPhyint instance: %s %s index %u state %x flags %llx	 "
1563*8485SPeter.Memishian@Sun.COM 	    "sock %x in_use %d\n",
15640Sstevel@tonic-gate 	    AF_STR(pii->pii_af), pii->pii_name, pii->pii_ifindex,
15650Sstevel@tonic-gate 	    pii->pii_state, pii->pii_phyint->pi_flags, pii->pii_probe_sock,
1566*8485SPeter.Memishian@Sun.COM 	    pii->pii_in_use);
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 	for (li = pii->pii_logint; li != NULL; li = li->li_next)
15690Sstevel@tonic-gate 		logint_print(li);
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	logdebug("\n");
15720Sstevel@tonic-gate 	for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next)
15730Sstevel@tonic-gate 		target_print(tg);
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	if (pii->pii_targets == NULL)
15760Sstevel@tonic-gate 		logdebug("pi_targets NULL\n");
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 	if (pii->pii_target_next != NULL) {
15790Sstevel@tonic-gate 		logdebug("pi_target_next %s %s\n", AF_STR(pii->pii_af),
15800Sstevel@tonic-gate 		    pr_addr(pii->pii_af, pii->pii_target_next->tg_address,
15814770Smeem 		    abuf, sizeof (abuf)));
15820Sstevel@tonic-gate 	} else {
15830Sstevel@tonic-gate 		logdebug("pi_target_next NULL\n");
15840Sstevel@tonic-gate 	}
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	if (pii->pii_rtt_target_next != NULL) {
15870Sstevel@tonic-gate 		logdebug("pi_rtt_target_next %s %s\n", AF_STR(pii->pii_af),
15880Sstevel@tonic-gate 		    pr_addr(pii->pii_af, pii->pii_rtt_target_next->tg_address,
15894770Smeem 		    abuf, sizeof (abuf)));
15900Sstevel@tonic-gate 	} else {
15910Sstevel@tonic-gate 		logdebug("pi_rtt_target_next NULL\n");
15920Sstevel@tonic-gate 	}
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	if (pii->pii_targets != NULL) {
15950Sstevel@tonic-gate 		most_recent = PROBE_INDEX_PREV(pii->pii_probe_next);
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 		i = most_recent;
15980Sstevel@tonic-gate 		do {
15990Sstevel@tonic-gate 			if (pii->pii_probes[i].pr_target != NULL) {
16000Sstevel@tonic-gate 				logdebug("#%d target %s ", i,
16010Sstevel@tonic-gate 				    pr_addr(pii->pii_af,
16020Sstevel@tonic-gate 				    pii->pii_probes[i].pr_target->tg_address,
16030Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
16040Sstevel@tonic-gate 			} else {
16050Sstevel@tonic-gate 				logdebug("#%d target NULL ", i);
16060Sstevel@tonic-gate 			}
1607*8485SPeter.Memishian@Sun.COM 			logdebug("time_start %lld status %d "
1608*8485SPeter.Memishian@Sun.COM 			    "time_ackproc %lld time_lost %u",
1609*8485SPeter.Memishian@Sun.COM 			    pii->pii_probes[i].pr_hrtime_start,
16100Sstevel@tonic-gate 			    pii->pii_probes[i].pr_status,
1611*8485SPeter.Memishian@Sun.COM 			    pii->pii_probes[i].pr_hrtime_ackproc,
16120Sstevel@tonic-gate 			    pii->pii_probes[i].pr_time_lost);
16130Sstevel@tonic-gate 			i = PROBE_INDEX_PREV(i);
16140Sstevel@tonic-gate 		} while (i != most_recent);
16150Sstevel@tonic-gate 	}
16160Sstevel@tonic-gate }
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate /*
16190Sstevel@tonic-gate  * Lookup a logint based on the logical interface name, on the given
16200Sstevel@tonic-gate  * phyint instance.
16210Sstevel@tonic-gate  */
16220Sstevel@tonic-gate static struct logint *
16230Sstevel@tonic-gate logint_lookup(struct phyint_instance *pii, char *name)
16240Sstevel@tonic-gate {
16250Sstevel@tonic-gate 	struct logint *li;
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 	if (debug & D_LOGINT) {
16280Sstevel@tonic-gate 		logdebug("logint_lookup(%s, %s)\n",
16290Sstevel@tonic-gate 		    AF_STR(pii->pii_af), name);
16300Sstevel@tonic-gate 	}
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	for (li = pii->pii_logint; li != NULL; li = li->li_next) {
16330Sstevel@tonic-gate 		if (strncmp(name, li->li_name, sizeof (li->li_name)) == 0)
16340Sstevel@tonic-gate 			break;
16350Sstevel@tonic-gate 	}
16360Sstevel@tonic-gate 	return (li);
16370Sstevel@tonic-gate }
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate /*
16400Sstevel@tonic-gate  * Insert a logint at the head of the list of logints of the given
16410Sstevel@tonic-gate  * phyint instance
16420Sstevel@tonic-gate  */
16430Sstevel@tonic-gate static void
16440Sstevel@tonic-gate logint_insert(struct phyint_instance *pii, struct logint *li)
16450Sstevel@tonic-gate {
16460Sstevel@tonic-gate 	li->li_next = pii->pii_logint;
16470Sstevel@tonic-gate 	li->li_prev = NULL;
16480Sstevel@tonic-gate 	if (pii->pii_logint != NULL)
16490Sstevel@tonic-gate 		pii->pii_logint->li_prev = li;
16500Sstevel@tonic-gate 	pii->pii_logint = li;
16510Sstevel@tonic-gate 	li->li_phyint_inst = pii;
16520Sstevel@tonic-gate }
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate /*
16550Sstevel@tonic-gate  * Create a new named logint, on the specified phyint instance.
16560Sstevel@tonic-gate  */
16570Sstevel@tonic-gate static struct logint *
16580Sstevel@tonic-gate logint_create(struct phyint_instance *pii, char *name)
16590Sstevel@tonic-gate {
16600Sstevel@tonic-gate 	struct logint *li;
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 	if (debug & D_LOGINT) {
16630Sstevel@tonic-gate 		logdebug("logint_create(%s %s %s)\n",
16640Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name, name);
16650Sstevel@tonic-gate 	}
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 	li = calloc(1, sizeof (struct logint));
16680Sstevel@tonic-gate 	if (li == NULL) {
16690Sstevel@tonic-gate 		logperror("logint_create: calloc");
16700Sstevel@tonic-gate 		return (NULL);
16710Sstevel@tonic-gate 	}
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	(void) strncpy(li->li_name, name, sizeof (li->li_name));
16740Sstevel@tonic-gate 	li->li_name[sizeof (li->li_name) - 1] = '\0';
16750Sstevel@tonic-gate 	logint_insert(pii, li);
16760Sstevel@tonic-gate 	return (li);
16770Sstevel@tonic-gate }
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate /*
16800Sstevel@tonic-gate  * Initialize the logint based on the data returned by the kernel.
16810Sstevel@tonic-gate  */
16820Sstevel@tonic-gate void
16830Sstevel@tonic-gate logint_init_from_k(struct phyint_instance *pii, char *li_name)
16840Sstevel@tonic-gate {
16850Sstevel@tonic-gate 	int	ifsock;
16860Sstevel@tonic-gate 	uint64_t flags;
16870Sstevel@tonic-gate 	uint64_t saved_flags;
16880Sstevel@tonic-gate 	struct	logint	*li;
16890Sstevel@tonic-gate 	struct lifreq	lifr;
16900Sstevel@tonic-gate 	struct in6_addr	test_subnet;
16910Sstevel@tonic-gate 	struct in6_addr	testaddr;
16920Sstevel@tonic-gate 	int	test_subnet_len;
16930Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
16940Sstevel@tonic-gate 	struct sockaddr_in	*sin;
16950Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
16960Sstevel@tonic-gate 	boolean_t  ptp = _B_FALSE;
16970Sstevel@tonic-gate 	struct in6_addr tgaddr;
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	if (debug & D_LOGINT) {
17000Sstevel@tonic-gate 		logdebug("logint_init_from_k(%s %s)\n",
17010Sstevel@tonic-gate 		    AF_STR(pii->pii_af), li_name);
17020Sstevel@tonic-gate 	}
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	/* Get the socket for doing ioctls */
17050Sstevel@tonic-gate 	ifsock = (pii->pii_af == AF_INET) ? ifsock_v4 : ifsock_v6;
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	/*
17080Sstevel@tonic-gate 	 * Get the flags from the kernel. Also serves as a check whether
17090Sstevel@tonic-gate 	 * the logical still exists. If it doesn't exist, no need to proceed
17100Sstevel@tonic-gate 	 * any further. li_in_use will make the caller clean up the logint
17110Sstevel@tonic-gate 	 */
17120Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, li_name, sizeof (lifr.lifr_name));
17130Sstevel@tonic-gate 	lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
17140Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
17150Sstevel@tonic-gate 		/* Interface may have vanished */
17160Sstevel@tonic-gate 		if (errno != ENXIO) {
17170Sstevel@tonic-gate 			logperror_pii(pii, "logint_init_from_k: "
17180Sstevel@tonic-gate 			    "ioctl (get flags)");
17190Sstevel@tonic-gate 		}
17200Sstevel@tonic-gate 		return;
17210Sstevel@tonic-gate 	}
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	flags = lifr.lifr_flags;
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 	/*
17260Sstevel@tonic-gate 	 * Verified the logint exists. Now lookup the logint in our tables.
17270Sstevel@tonic-gate 	 * If it does not exist, create a new logint.
17280Sstevel@tonic-gate 	 */
17290Sstevel@tonic-gate 	li = logint_lookup(pii, li_name);
17300Sstevel@tonic-gate 	if (li == NULL) {
17310Sstevel@tonic-gate 		li = logint_create(pii, li_name);
17320Sstevel@tonic-gate 		if (li == NULL) {
17330Sstevel@tonic-gate 			/*
17340Sstevel@tonic-gate 			 * Pretend the interface does not exist
17350Sstevel@tonic-gate 			 * in the kernel
17360Sstevel@tonic-gate 			 */
17370Sstevel@tonic-gate 			return;
17380Sstevel@tonic-gate 		}
17390Sstevel@tonic-gate 	}
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 	/*
17420Sstevel@tonic-gate 	 * Update li->li_flags with the new flags, after saving the old
17430Sstevel@tonic-gate 	 * value. This is used later to check what flags has changed and
17440Sstevel@tonic-gate 	 * take any action
17450Sstevel@tonic-gate 	 */
17460Sstevel@tonic-gate 	saved_flags = li->li_flags;
17470Sstevel@tonic-gate 	li->li_flags = flags;
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 	/*
17500Sstevel@tonic-gate 	 * Get the address, prefix, prefixlength and update the logint.
17510Sstevel@tonic-gate 	 * Check if anything has changed. If the logint used for the
17520Sstevel@tonic-gate 	 * test address has changed, take suitable action.
17530Sstevel@tonic-gate 	 */
17540Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFADDR, (char *)&lifr) < 0) {
17550Sstevel@tonic-gate 		/* Interface may have vanished */
17560Sstevel@tonic-gate 		if (errno != ENXIO) {
17570Sstevel@tonic-gate 			logperror_li(li, "logint_init_from_k: (get addr)");
17580Sstevel@tonic-gate 		}
17590Sstevel@tonic-gate 		goto error;
17600Sstevel@tonic-gate 	}
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	if (pii->pii_af == AF_INET) {
17630Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
17640Sstevel@tonic-gate 		IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &testaddr);
17650Sstevel@tonic-gate 	} else {
17660Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
17670Sstevel@tonic-gate 		testaddr = sin6->sin6_addr;
17680Sstevel@tonic-gate 	}
17690Sstevel@tonic-gate 
1770*8485SPeter.Memishian@Sun.COM 	if (ioctl(ifsock, SIOCGLIFSUBNET, (char *)&lifr) < 0) {
1771*8485SPeter.Memishian@Sun.COM 		/* Interface may have vanished */
1772*8485SPeter.Memishian@Sun.COM 		if (errno != ENXIO)
1773*8485SPeter.Memishian@Sun.COM 			logperror_li(li, "logint_init_from_k: (get subnet)");
1774*8485SPeter.Memishian@Sun.COM 		goto error;
1775*8485SPeter.Memishian@Sun.COM 	}
1776*8485SPeter.Memishian@Sun.COM 	if (lifr.lifr_subnet.ss_family == AF_INET6) {
1777*8485SPeter.Memishian@Sun.COM 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_subnet;
1778*8485SPeter.Memishian@Sun.COM 		test_subnet = sin6->sin6_addr;
1779*8485SPeter.Memishian@Sun.COM 		test_subnet_len = lifr.lifr_addrlen;
17800Sstevel@tonic-gate 	} else {
1781*8485SPeter.Memishian@Sun.COM 		sin = (struct sockaddr_in *)&lifr.lifr_subnet;
1782*8485SPeter.Memishian@Sun.COM 		IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &test_subnet);
1783*8485SPeter.Memishian@Sun.COM 		test_subnet_len = lifr.lifr_addrlen + (IPV6_ABITS - IP_ABITS);
17840Sstevel@tonic-gate 	}
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 	/*
17870Sstevel@tonic-gate 	 * If this is the logint corresponding to the test address used for
17880Sstevel@tonic-gate 	 * sending probes, then if anything significant has changed we need to
17890Sstevel@tonic-gate 	 * determine the test address again.  We ignore changes to the
17900Sstevel@tonic-gate 	 * IFF_FAILED and IFF_RUNNING flags since those happen as a matter of
17910Sstevel@tonic-gate 	 * course.
17920Sstevel@tonic-gate 	 */
17930Sstevel@tonic-gate 	if (pii->pii_probe_logint == li) {
17940Sstevel@tonic-gate 		if (((li->li_flags ^ saved_flags) &
17950Sstevel@tonic-gate 		    ~(IFF_FAILED | IFF_RUNNING)) != 0 ||
17960Sstevel@tonic-gate 		    !IN6_ARE_ADDR_EQUAL(&testaddr, &li->li_addr) ||
17970Sstevel@tonic-gate 		    (!ptp && !IN6_ARE_ADDR_EQUAL(&test_subnet,
17984770Smeem 		    &li->li_subnet)) ||
17990Sstevel@tonic-gate 		    (!ptp && test_subnet_len != li->li_subnet_len) ||
18000Sstevel@tonic-gate 		    (ptp && !IN6_ARE_ADDR_EQUAL(&tgaddr, &li->li_dstaddr))) {
18010Sstevel@tonic-gate 			/*
18020Sstevel@tonic-gate 			 * Something significant that affects the testaddress
18030Sstevel@tonic-gate 			 * has changed. Redo the testaddress selection later on
18040Sstevel@tonic-gate 			 * in select_test_ifs(). For now do the cleanup and
18050Sstevel@tonic-gate 			 * set pii_probe_logint to NULL.
18060Sstevel@tonic-gate 			 */
18070Sstevel@tonic-gate 			if (pii->pii_probe_sock != -1)
18080Sstevel@tonic-gate 				close_probe_socket(pii, _B_TRUE);
18090Sstevel@tonic-gate 			pii->pii_probe_logint = NULL;
18100Sstevel@tonic-gate 		}
18110Sstevel@tonic-gate 	}
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 	/* Update the logint with the values obtained from the kernel.	*/
18150Sstevel@tonic-gate 	li->li_addr = testaddr;
18160Sstevel@tonic-gate 	li->li_in_use = 1;
18170Sstevel@tonic-gate 	if (ptp) {
18180Sstevel@tonic-gate 		li->li_dstaddr = tgaddr;
18190Sstevel@tonic-gate 		li->li_subnet_len = (pii->pii_af == AF_INET) ?
18200Sstevel@tonic-gate 		    IP_ABITS : IPV6_ABITS;
18210Sstevel@tonic-gate 	} else {
18220Sstevel@tonic-gate 		li->li_subnet = test_subnet;
18230Sstevel@tonic-gate 		li->li_subnet_len = test_subnet_len;
18240Sstevel@tonic-gate 	}
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	if (debug & D_LOGINT)
18270Sstevel@tonic-gate 		logint_print(li);
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 	return;
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate error:
18320Sstevel@tonic-gate 	logerr("logint_init_from_k: IGNORED %s %s %s addr %s\n",
18330Sstevel@tonic-gate 	    AF_STR(pii->pii_af), pii->pii_name, li->li_name,
18340Sstevel@tonic-gate 	    pr_addr(pii->pii_af, testaddr, abuf, sizeof (abuf)));
18350Sstevel@tonic-gate 	logint_delete(li);
18360Sstevel@tonic-gate }
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate /*
18390Sstevel@tonic-gate  * Delete (unlink and free) a logint.
18400Sstevel@tonic-gate  */
18410Sstevel@tonic-gate void
18420Sstevel@tonic-gate logint_delete(struct logint *li)
18430Sstevel@tonic-gate {
18440Sstevel@tonic-gate 	struct phyint_instance *pii;
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	pii = li->li_phyint_inst;
18470Sstevel@tonic-gate 	assert(pii != NULL);
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	if (debug & D_LOGINT) {
18500Sstevel@tonic-gate 		int af;
18510Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 		af = pii->pii_af;
18540Sstevel@tonic-gate 		logdebug("logint_delete(%s %s %s/%u)\n",
18550Sstevel@tonic-gate 		    AF_STR(af), li->li_name,
18560Sstevel@tonic-gate 		    pr_addr(af, li->li_addr, abuf, sizeof (abuf)),
18570Sstevel@tonic-gate 		    li->li_subnet_len);
18580Sstevel@tonic-gate 	}
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	/* logint must be in the list of logints */
18610Sstevel@tonic-gate 	assert(pii->pii_logint == li || li->li_prev != NULL);
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 	/* Remove the logint from the list of logints  */
18640Sstevel@tonic-gate 	if (li->li_prev == NULL) {
18650Sstevel@tonic-gate 		/* logint is the 1st in the list */
18660Sstevel@tonic-gate 		pii->pii_logint = li->li_next;
18670Sstevel@tonic-gate 	} else {
18680Sstevel@tonic-gate 		li->li_prev->li_next = li->li_next;
18690Sstevel@tonic-gate 	}
18700Sstevel@tonic-gate 	if (li->li_next != NULL)
18710Sstevel@tonic-gate 		li->li_next->li_prev = li->li_prev;
18720Sstevel@tonic-gate 	li->li_next = NULL;
18730Sstevel@tonic-gate 	li->li_prev = NULL;
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 	/*
18762074Smeem 	 * If this logint is also being used for probing, then close the
18772074Smeem 	 * associated socket, if it exists.
18780Sstevel@tonic-gate 	 */
18790Sstevel@tonic-gate 	if (pii->pii_probe_logint == li) {
18800Sstevel@tonic-gate 		if (pii->pii_probe_sock != -1)
18810Sstevel@tonic-gate 			close_probe_socket(pii, _B_TRUE);
18820Sstevel@tonic-gate 		pii->pii_probe_logint = NULL;
18830Sstevel@tonic-gate 	}
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	free(li);
18860Sstevel@tonic-gate }
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate static void
18890Sstevel@tonic-gate logint_print(struct logint *li)
18900Sstevel@tonic-gate {
18910Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
1892*8485SPeter.Memishian@Sun.COM 	int af = li->li_phyint_inst->pii_af;
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 	logdebug("logint: %s %s addr %s/%u", AF_STR(af), li->li_name,
18950Sstevel@tonic-gate 	    pr_addr(af, li->li_addr, abuf, sizeof (abuf)), li->li_subnet_len);
18960Sstevel@tonic-gate 
1897*8485SPeter.Memishian@Sun.COM 	logdebug("\tFlags: %llx in_use %d\n", li->li_flags, li->li_in_use);
18980Sstevel@tonic-gate }
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate char *
19010Sstevel@tonic-gate pr_addr(int af, struct in6_addr addr, char *abuf, int len)
19020Sstevel@tonic-gate {
19030Sstevel@tonic-gate 	struct in_addr	addr_v4;
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 	if (af == AF_INET) {
19060Sstevel@tonic-gate 		IN6_V4MAPPED_TO_INADDR(&addr, &addr_v4);
19070Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, (void *)&addr_v4, abuf, len);
19080Sstevel@tonic-gate 	} else {
19090Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, (void *)&addr, abuf, len);
19100Sstevel@tonic-gate 	}
19110Sstevel@tonic-gate 	return (abuf);
19120Sstevel@tonic-gate }
19130Sstevel@tonic-gate 
1914*8485SPeter.Memishian@Sun.COM /*
1915*8485SPeter.Memishian@Sun.COM  * Fill in the sockaddr_storage pointed to by `ssp' with the IP address
1916*8485SPeter.Memishian@Sun.COM  * represented by the [`af',`addr'] pair.  Needed because in.mpathd internally
1917*8485SPeter.Memishian@Sun.COM  * stores all addresses as in6_addrs, but we don't want to expose that.
1918*8485SPeter.Memishian@Sun.COM  */
1919*8485SPeter.Memishian@Sun.COM void
1920*8485SPeter.Memishian@Sun.COM addr2storage(int af, const struct in6_addr *addr, struct sockaddr_storage *ssp)
1921*8485SPeter.Memishian@Sun.COM {
1922*8485SPeter.Memishian@Sun.COM 	struct sockaddr_in *sinp = (struct sockaddr_in *)ssp;
1923*8485SPeter.Memishian@Sun.COM 	struct sockaddr_in6 *sin6p = (struct sockaddr_in6 *)ssp;
1924*8485SPeter.Memishian@Sun.COM 
1925*8485SPeter.Memishian@Sun.COM 	assert(af == AF_INET || af == AF_INET6);
1926*8485SPeter.Memishian@Sun.COM 
1927*8485SPeter.Memishian@Sun.COM 	switch (af) {
1928*8485SPeter.Memishian@Sun.COM 	case AF_INET:
1929*8485SPeter.Memishian@Sun.COM 		(void) memset(sinp, 0, sizeof (*sinp));
1930*8485SPeter.Memishian@Sun.COM 		sinp->sin_family = AF_INET;
1931*8485SPeter.Memishian@Sun.COM 		IN6_V4MAPPED_TO_INADDR(addr, &sinp->sin_addr);
1932*8485SPeter.Memishian@Sun.COM 		break;
1933*8485SPeter.Memishian@Sun.COM 	case AF_INET6:
1934*8485SPeter.Memishian@Sun.COM 		(void) memset(sin6p, 0, sizeof (*sin6p));
1935*8485SPeter.Memishian@Sun.COM 		sin6p->sin6_family = AF_INET6;
1936*8485SPeter.Memishian@Sun.COM 		sin6p->sin6_addr = *addr;
1937*8485SPeter.Memishian@Sun.COM 		break;
1938*8485SPeter.Memishian@Sun.COM 	}
1939*8485SPeter.Memishian@Sun.COM }
1940*8485SPeter.Memishian@Sun.COM 
19410Sstevel@tonic-gate /* Lookup target on its address */
19420Sstevel@tonic-gate struct target *
19430Sstevel@tonic-gate target_lookup(struct phyint_instance *pii, struct in6_addr addr)
19440Sstevel@tonic-gate {
19450Sstevel@tonic-gate 	struct target *tg;
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 	if (debug & D_TARGET) {
19480Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 		logdebug("target_lookup(%s %s): addr %s\n",
19510Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name,
19520Sstevel@tonic-gate 		    pr_addr(pii->pii_af, addr, abuf, sizeof (abuf)));
19530Sstevel@tonic-gate 	}
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 	for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
19560Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&tg->tg_address, &addr))
19570Sstevel@tonic-gate 			break;
19580Sstevel@tonic-gate 	}
19590Sstevel@tonic-gate 	return (tg);
19600Sstevel@tonic-gate }
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate /*
19630Sstevel@tonic-gate  * Find and return the next active target, for the next probe.
19640Sstevel@tonic-gate  * If no active targets are available, return NULL.
19650Sstevel@tonic-gate  */
19660Sstevel@tonic-gate struct target *
19670Sstevel@tonic-gate target_next(struct target *tg)
19680Sstevel@tonic-gate {
19690Sstevel@tonic-gate 	struct	phyint_instance	*pii = tg->tg_phyint_inst;
19700Sstevel@tonic-gate 	struct	target	*marker = tg;
19710Sstevel@tonic-gate 	hrtime_t now;
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 	now = gethrtime();
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 	/*
19760Sstevel@tonic-gate 	 * Target must be in the list of targets for this phyint
19770Sstevel@tonic-gate 	 * instance.
19780Sstevel@tonic-gate 	 */
19790Sstevel@tonic-gate 	assert(pii->pii_targets == tg || tg->tg_prev != NULL);
19800Sstevel@tonic-gate 	assert(pii->pii_targets != NULL);
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 	/* Return the next active target */
19830Sstevel@tonic-gate 	do {
19840Sstevel@tonic-gate 		/*
19850Sstevel@tonic-gate 		 * Go to the next target. If we hit the end,
19860Sstevel@tonic-gate 		 * reset the ptr to the head
19870Sstevel@tonic-gate 		 */
19880Sstevel@tonic-gate 		tg = tg->tg_next;
19890Sstevel@tonic-gate 		if (tg == NULL)
19900Sstevel@tonic-gate 			tg = pii->pii_targets;
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 		assert(TG_STATUS_VALID(tg->tg_status));
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 		switch (tg->tg_status) {
19950Sstevel@tonic-gate 		case TG_ACTIVE:
19960Sstevel@tonic-gate 			return (tg);
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate 		case TG_UNUSED:
19990Sstevel@tonic-gate 			assert(pii->pii_targets_are_routers);
20000Sstevel@tonic-gate 			if (pii->pii_ntargets < MAX_PROBE_TARGETS) {
20010Sstevel@tonic-gate 				/*
20020Sstevel@tonic-gate 				 * Bubble up the unused target to active
20030Sstevel@tonic-gate 				 */
20040Sstevel@tonic-gate 				tg->tg_status = TG_ACTIVE;
20050Sstevel@tonic-gate 				pii->pii_ntargets++;
20060Sstevel@tonic-gate 				return (tg);
20070Sstevel@tonic-gate 			}
20080Sstevel@tonic-gate 			break;
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 		case TG_SLOW:
20110Sstevel@tonic-gate 			assert(pii->pii_targets_are_routers);
20120Sstevel@tonic-gate 			if (tg->tg_latime + MIN_RECOVERY_TIME < now) {
20130Sstevel@tonic-gate 				/*
20140Sstevel@tonic-gate 				 * Bubble up the slow target to unused
20150Sstevel@tonic-gate 				 */
20160Sstevel@tonic-gate 				tg->tg_status = TG_UNUSED;
20170Sstevel@tonic-gate 			}
20180Sstevel@tonic-gate 			break;
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate 		case TG_DEAD:
20210Sstevel@tonic-gate 			assert(pii->pii_targets_are_routers);
20220Sstevel@tonic-gate 			if (tg->tg_latime + MIN_RECOVERY_TIME < now) {
20230Sstevel@tonic-gate 				/*
20240Sstevel@tonic-gate 				 * Bubble up the dead target to slow
20250Sstevel@tonic-gate 				 */
20260Sstevel@tonic-gate 				tg->tg_status = TG_SLOW;
20270Sstevel@tonic-gate 				tg->tg_latime = now;
20280Sstevel@tonic-gate 			}
20290Sstevel@tonic-gate 			break;
20300Sstevel@tonic-gate 		}
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate 	} while (tg != marker);
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 	return (NULL);
20350Sstevel@tonic-gate }
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate /*
20380Sstevel@tonic-gate  * Select the best available target, that is not already TG_ACTIVE,
20390Sstevel@tonic-gate  * for the caller. The caller will determine whether it wants to
20400Sstevel@tonic-gate  * make the returned target TG_ACTIVE.
20410Sstevel@tonic-gate  * The selection order is as follows.
20420Sstevel@tonic-gate  * 1. pick a TG_UNSED target, if it exists.
20430Sstevel@tonic-gate  * 2. else pick a TG_SLOW target that has recovered, if it exists
20440Sstevel@tonic-gate  * 3. else pick any TG_SLOW target, if it exists
20450Sstevel@tonic-gate  * 4. else pick a TG_DEAD target that has recovered, if it exists
20460Sstevel@tonic-gate  * 5. else pick any TG_DEAD target, if it exists
20470Sstevel@tonic-gate  * 6. else return null
20480Sstevel@tonic-gate  */
20490Sstevel@tonic-gate static struct target *
20500Sstevel@tonic-gate target_select_best(struct phyint_instance *pii)
20510Sstevel@tonic-gate {
20520Sstevel@tonic-gate 	struct target *tg;
20530Sstevel@tonic-gate 	struct target *slow = NULL;
20540Sstevel@tonic-gate 	struct target *dead = NULL;
20550Sstevel@tonic-gate 	struct target *slow_recovered = NULL;
20560Sstevel@tonic-gate 	struct target *dead_recovered = NULL;
20570Sstevel@tonic-gate 	hrtime_t now;
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate 	now = gethrtime();
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 	for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
20620Sstevel@tonic-gate 		assert(TG_STATUS_VALID(tg->tg_status));
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 		switch (tg->tg_status) {
20650Sstevel@tonic-gate 		case TG_UNUSED:
20660Sstevel@tonic-gate 			return (tg);
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 		case TG_SLOW:
20690Sstevel@tonic-gate 			if (tg->tg_latime + MIN_RECOVERY_TIME < now) {
20700Sstevel@tonic-gate 				slow_recovered = tg;
20710Sstevel@tonic-gate 				/*
2072*8485SPeter.Memishian@Sun.COM 				 * Promote the slow_recovered to unused
20730Sstevel@tonic-gate 				 */
20740Sstevel@tonic-gate 				tg->tg_status = TG_UNUSED;
20750Sstevel@tonic-gate 			} else {
20760Sstevel@tonic-gate 				slow = tg;
20770Sstevel@tonic-gate 			}
20780Sstevel@tonic-gate 			break;
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 		case TG_DEAD:
20810Sstevel@tonic-gate 			if (tg->tg_latime + MIN_RECOVERY_TIME < now) {
20820Sstevel@tonic-gate 				dead_recovered = tg;
20830Sstevel@tonic-gate 				/*
2084*8485SPeter.Memishian@Sun.COM 				 * Promote the dead_recovered to slow
20850Sstevel@tonic-gate 				 */
20860Sstevel@tonic-gate 				tg->tg_status = TG_SLOW;
20870Sstevel@tonic-gate 				tg->tg_latime = now;
20880Sstevel@tonic-gate 			} else {
20890Sstevel@tonic-gate 				dead = tg;
20900Sstevel@tonic-gate 			}
20910Sstevel@tonic-gate 			break;
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 		default:
20940Sstevel@tonic-gate 			break;
20950Sstevel@tonic-gate 		}
20960Sstevel@tonic-gate 	}
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 	if (slow_recovered != NULL)
20990Sstevel@tonic-gate 		return (slow_recovered);
21000Sstevel@tonic-gate 	else if (slow != NULL)
21010Sstevel@tonic-gate 		return (slow);
21020Sstevel@tonic-gate 	else if (dead_recovered != NULL)
21030Sstevel@tonic-gate 		return (dead_recovered);
21040Sstevel@tonic-gate 	else
21050Sstevel@tonic-gate 		return (dead);
21060Sstevel@tonic-gate }
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate /*
21090Sstevel@tonic-gate  * Some target was deleted. If we don't have even MIN_PROBE_TARGETS
21100Sstevel@tonic-gate  * that are active, pick the next best below.
21110Sstevel@tonic-gate  */
21120Sstevel@tonic-gate static void
21130Sstevel@tonic-gate target_activate_all(struct phyint_instance *pii)
21140Sstevel@tonic-gate {
21150Sstevel@tonic-gate 	struct target *tg;
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 	assert(pii->pii_ntargets == 0);
21180Sstevel@tonic-gate 	assert(pii->pii_target_next == NULL);
21190Sstevel@tonic-gate 	assert(pii->pii_rtt_target_next == NULL);
21200Sstevel@tonic-gate 	assert(pii->pii_targets_are_routers);
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 	while (pii->pii_ntargets < MIN_PROBE_TARGETS) {
21230Sstevel@tonic-gate 		tg = target_select_best(pii);
21240Sstevel@tonic-gate 		if (tg == NULL) {
21250Sstevel@tonic-gate 			/* We are out of targets */
21260Sstevel@tonic-gate 			return;
21270Sstevel@tonic-gate 		}
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 		assert(TG_STATUS_VALID(tg->tg_status));
21300Sstevel@tonic-gate 		assert(tg->tg_status != TG_ACTIVE);
21310Sstevel@tonic-gate 		tg->tg_status = TG_ACTIVE;
21320Sstevel@tonic-gate 		pii->pii_ntargets++;
21330Sstevel@tonic-gate 		if (pii->pii_target_next == NULL) {
21340Sstevel@tonic-gate 			pii->pii_target_next = tg;
21350Sstevel@tonic-gate 			pii->pii_rtt_target_next = tg;
21360Sstevel@tonic-gate 		}
21370Sstevel@tonic-gate 	}
21380Sstevel@tonic-gate }
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate static struct target *
21410Sstevel@tonic-gate target_first(struct phyint_instance *pii)
21420Sstevel@tonic-gate {
21430Sstevel@tonic-gate 	struct target *tg;
21440Sstevel@tonic-gate 
21450Sstevel@tonic-gate 	for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
21460Sstevel@tonic-gate 		assert(TG_STATUS_VALID(tg->tg_status));
21470Sstevel@tonic-gate 		if (tg->tg_status == TG_ACTIVE)
21480Sstevel@tonic-gate 			break;
21490Sstevel@tonic-gate 	}
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 	return (tg);
21520Sstevel@tonic-gate }
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate /*
21550Sstevel@tonic-gate  * Create a default target entry.
21560Sstevel@tonic-gate  */
21570Sstevel@tonic-gate void
21580Sstevel@tonic-gate target_create(struct phyint_instance *pii, struct in6_addr addr,
21590Sstevel@tonic-gate     boolean_t is_router)
21600Sstevel@tonic-gate {
21610Sstevel@tonic-gate 	struct target *tg;
21620Sstevel@tonic-gate 	struct phyint *pi;
21630Sstevel@tonic-gate 	struct logint *li;
21640Sstevel@tonic-gate 
21650Sstevel@tonic-gate 	if (debug & D_TARGET) {
21660Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 		logdebug("target_create(%s %s, %s)\n",
21690Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name,
21700Sstevel@tonic-gate 		    pr_addr(pii->pii_af, addr, abuf, sizeof (abuf)));
21710Sstevel@tonic-gate 	}
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 	/*
21740Sstevel@tonic-gate 	 * If the test address is not yet initialized, do not add
21750Sstevel@tonic-gate 	 * any target, since we cannot determine whether the target
21760Sstevel@tonic-gate 	 * belongs to the same subnet as the test address.
21770Sstevel@tonic-gate 	 */
21780Sstevel@tonic-gate 	li = pii->pii_probe_logint;
21790Sstevel@tonic-gate 	if (li == NULL)
21800Sstevel@tonic-gate 		return;
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 	/*
21830Sstevel@tonic-gate 	 * If there are multiple subnets associated with an interface, then
2184*8485SPeter.Memishian@Sun.COM 	 * add the target to this phyint instance only if it belongs to the
2185*8485SPeter.Memishian@Sun.COM 	 * same subnet as the test address.  This assures us that we will
2186*8485SPeter.Memishian@Sun.COM 	 * be able to reach this target through our routing table.
21870Sstevel@tonic-gate 	 */
21880Sstevel@tonic-gate 	if (!prefix_equal(li->li_subnet, addr, li->li_subnet_len))
21890Sstevel@tonic-gate 		return;
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	if (pii->pii_targets != NULL) {
21920Sstevel@tonic-gate 		assert(pii->pii_ntargets <= MAX_PROBE_TARGETS);
21930Sstevel@tonic-gate 		if (is_router) {
21940Sstevel@tonic-gate 			if (!pii->pii_targets_are_routers) {
21950Sstevel@tonic-gate 				/*
21960Sstevel@tonic-gate 				 * Prefer router over hosts. Using hosts is a
21970Sstevel@tonic-gate 				 * fallback mechanism, hence delete all host
21980Sstevel@tonic-gate 				 * targets.
21990Sstevel@tonic-gate 				 */
22000Sstevel@tonic-gate 				while (pii->pii_targets != NULL)
22010Sstevel@tonic-gate 					target_delete(pii->pii_targets);
22020Sstevel@tonic-gate 			}
22030Sstevel@tonic-gate 		} else {
22040Sstevel@tonic-gate 			/*
22050Sstevel@tonic-gate 			 * Routers take precedence over hosts. If this
22060Sstevel@tonic-gate 			 * is a router list and we are trying to add a
22070Sstevel@tonic-gate 			 * host, just return. If this is a host list
22080Sstevel@tonic-gate 			 * and if we have sufficient targets, just return
22090Sstevel@tonic-gate 			 */
22100Sstevel@tonic-gate 			if (pii->pii_targets_are_routers ||
22110Sstevel@tonic-gate 			    pii->pii_ntargets == MAX_PROBE_TARGETS)
22120Sstevel@tonic-gate 				return;
22130Sstevel@tonic-gate 		}
22140Sstevel@tonic-gate 	}
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 	tg = calloc(1, sizeof (struct target));
22170Sstevel@tonic-gate 	if (tg == NULL) {
22180Sstevel@tonic-gate 		logperror("target_create: calloc");
22190Sstevel@tonic-gate 		return;
22200Sstevel@tonic-gate 	}
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 	tg->tg_phyint_inst = pii;
22230Sstevel@tonic-gate 	tg->tg_address = addr;
22240Sstevel@tonic-gate 	tg->tg_in_use = 1;
22250Sstevel@tonic-gate 	tg->tg_rtt_sa = -1;
22260Sstevel@tonic-gate 	tg->tg_num_deferred = 0;
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate 	/*
22290Sstevel@tonic-gate 	 * If this is the first target, set 'pii_targets_are_routers'
22300Sstevel@tonic-gate 	 * The list of targets is either a list of hosts or list or
22310Sstevel@tonic-gate 	 * routers, but not a mix.
22320Sstevel@tonic-gate 	 */
22330Sstevel@tonic-gate 	if (pii->pii_targets == NULL) {
22340Sstevel@tonic-gate 		assert(pii->pii_ntargets == 0);
22350Sstevel@tonic-gate 		assert(pii->pii_target_next == NULL);
22360Sstevel@tonic-gate 		assert(pii->pii_rtt_target_next == NULL);
22370Sstevel@tonic-gate 		pii->pii_targets_are_routers = is_router ? 1 : 0;
22380Sstevel@tonic-gate 	}
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate 	if (pii->pii_ntargets == MAX_PROBE_TARGETS) {
22410Sstevel@tonic-gate 		assert(pii->pii_targets_are_routers);
22420Sstevel@tonic-gate 		assert(pii->pii_target_next != NULL);
22430Sstevel@tonic-gate 		assert(pii->pii_rtt_target_next != NULL);
22440Sstevel@tonic-gate 		tg->tg_status = TG_UNUSED;
22450Sstevel@tonic-gate 	} else {
22460Sstevel@tonic-gate 		if (pii->pii_ntargets == 0) {
22470Sstevel@tonic-gate 			assert(pii->pii_target_next == NULL);
22480Sstevel@tonic-gate 			pii->pii_target_next = tg;
22490Sstevel@tonic-gate 			pii->pii_rtt_target_next = tg;
22500Sstevel@tonic-gate 		}
22510Sstevel@tonic-gate 		pii->pii_ntargets++;
22520Sstevel@tonic-gate 		tg->tg_status = TG_ACTIVE;
22530Sstevel@tonic-gate 	}
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 	target_insert(pii, tg);
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 	/*
22582074Smeem 	 * Change state to PI_RUNNING if this phyint instance is capable of
22592074Smeem 	 * sending and receiving probes -- that is, if we know of at least 1
22602074Smeem 	 * target, and this phyint instance is probe-capable.  For more
22612074Smeem 	 * details, see the phyint state diagram in mpd_probe.c.
22620Sstevel@tonic-gate 	 */
22630Sstevel@tonic-gate 	pi = pii->pii_phyint;
22640Sstevel@tonic-gate 	if (pi->pi_state == PI_NOTARGETS && PROBE_CAPABLE(pii)) {
22650Sstevel@tonic-gate 		if (pi->pi_flags & IFF_FAILED)
22660Sstevel@tonic-gate 			phyint_chstate(pi, PI_FAILED);
22670Sstevel@tonic-gate 		else
22680Sstevel@tonic-gate 			phyint_chstate(pi, PI_RUNNING);
22690Sstevel@tonic-gate 	}
22700Sstevel@tonic-gate }
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate /*
22730Sstevel@tonic-gate  * Add the target address named by `addr' to phyint instance `pii' if it does
22740Sstevel@tonic-gate  * not already exist.  If the target is a router, `is_router' should be set to
22750Sstevel@tonic-gate  * B_TRUE.
22760Sstevel@tonic-gate  */
22770Sstevel@tonic-gate void
22780Sstevel@tonic-gate target_add(struct phyint_instance *pii, struct in6_addr addr,
22790Sstevel@tonic-gate     boolean_t is_router)
22800Sstevel@tonic-gate {
22810Sstevel@tonic-gate 	struct target *tg;
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate 	if (pii == NULL)
22840Sstevel@tonic-gate 		return;
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate 	tg = target_lookup(pii, addr);
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 	/*
22890Sstevel@tonic-gate 	 * If the target does not exist, create it; target_create() will set
2290*8485SPeter.Memishian@Sun.COM 	 * tg_in_use to true.  Even if it exists already, if it's a router
2291*8485SPeter.Memishian@Sun.COM 	 * target and we'd previously learned of it through multicast, then we
2292*8485SPeter.Memishian@Sun.COM 	 * need to recreate it as a router target.  Otherwise, just set
2293*8485SPeter.Memishian@Sun.COM 	 * tg_in_use to to true so that init_router_targets() won't delete it.
22940Sstevel@tonic-gate 	 */
2295*8485SPeter.Memishian@Sun.COM 	if (tg == NULL || (is_router && !pii->pii_targets_are_routers))
22960Sstevel@tonic-gate 		target_create(pii, addr, is_router);
22970Sstevel@tonic-gate 	else if (is_router)
22980Sstevel@tonic-gate 		tg->tg_in_use = 1;
22990Sstevel@tonic-gate }
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate /*
23020Sstevel@tonic-gate  * Insert target at head of linked list of targets for the associated
23030Sstevel@tonic-gate  * phyint instance
23040Sstevel@tonic-gate  */
23050Sstevel@tonic-gate static void
23060Sstevel@tonic-gate target_insert(struct phyint_instance *pii, struct target *tg)
23070Sstevel@tonic-gate {
23080Sstevel@tonic-gate 	tg->tg_next = pii->pii_targets;
23090Sstevel@tonic-gate 	tg->tg_prev = NULL;
23100Sstevel@tonic-gate 	if (tg->tg_next != NULL)
23110Sstevel@tonic-gate 		tg->tg_next->tg_prev = tg;
23120Sstevel@tonic-gate 	pii->pii_targets = tg;
23130Sstevel@tonic-gate }
23140Sstevel@tonic-gate 
23150Sstevel@tonic-gate /*
23160Sstevel@tonic-gate  * Delete a target (unlink and free).
23170Sstevel@tonic-gate  */
23180Sstevel@tonic-gate void
23190Sstevel@tonic-gate target_delete(struct target *tg)
23200Sstevel@tonic-gate {
23210Sstevel@tonic-gate 	int af;
23220Sstevel@tonic-gate 	struct phyint_instance	*pii;
23230Sstevel@tonic-gate 	struct phyint_instance	*pii_other;
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate 	pii = tg->tg_phyint_inst;
23260Sstevel@tonic-gate 	af = pii->pii_af;
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 	if (debug & D_TARGET) {
23290Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
23300Sstevel@tonic-gate 
23310Sstevel@tonic-gate 		logdebug("target_delete(%s %s, %s)\n",
23320Sstevel@tonic-gate 		    AF_STR(af), pii->pii_name,
23330Sstevel@tonic-gate 		    pr_addr(af, tg->tg_address, abuf, sizeof (abuf)));
23340Sstevel@tonic-gate 	}
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 	/*
23370Sstevel@tonic-gate 	 * Target must be in the list of targets for this phyint
23380Sstevel@tonic-gate 	 * instance.
23390Sstevel@tonic-gate 	 */
23400Sstevel@tonic-gate 	assert(pii->pii_targets == tg || tg->tg_prev != NULL);
23410Sstevel@tonic-gate 
23420Sstevel@tonic-gate 	/*
23430Sstevel@tonic-gate 	 * Reset all references to 'tg' in the probe information
23440Sstevel@tonic-gate 	 * for this phyint.
23450Sstevel@tonic-gate 	 */
23460Sstevel@tonic-gate 	reset_pii_probes(pii, tg);
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate 	/*
23490Sstevel@tonic-gate 	 * Remove this target from the list of targets of this
23500Sstevel@tonic-gate 	 * phyint instance.
23510Sstevel@tonic-gate 	 */
23520Sstevel@tonic-gate 	if (tg->tg_prev == NULL) {
23530Sstevel@tonic-gate 		pii->pii_targets = tg->tg_next;
23540Sstevel@tonic-gate 	} else {
23550Sstevel@tonic-gate 		tg->tg_prev->tg_next = tg->tg_next;
23560Sstevel@tonic-gate 	}
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate 	if (tg->tg_next != NULL)
23590Sstevel@tonic-gate 		tg->tg_next->tg_prev = tg->tg_prev;
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate 	tg->tg_next = NULL;
23620Sstevel@tonic-gate 	tg->tg_prev = NULL;
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate 	if (tg->tg_status == TG_ACTIVE)
23650Sstevel@tonic-gate 		pii->pii_ntargets--;
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 	/*
23680Sstevel@tonic-gate 	 * Adjust the next target to probe, if it points to
23690Sstevel@tonic-gate 	 * to the currently deleted target.
23700Sstevel@tonic-gate 	 */
23710Sstevel@tonic-gate 	if (pii->pii_target_next == tg)
23720Sstevel@tonic-gate 		pii->pii_target_next = target_first(pii);
23730Sstevel@tonic-gate 
23740Sstevel@tonic-gate 	if (pii->pii_rtt_target_next == tg)
23750Sstevel@tonic-gate 		pii->pii_rtt_target_next = target_first(pii);
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate 	free(tg);
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate 	/*
23800Sstevel@tonic-gate 	 * The number of active targets pii_ntargets == 0 iff
23810Sstevel@tonic-gate 	 * the next active target pii->pii_target_next == NULL
23820Sstevel@tonic-gate 	 */
23830Sstevel@tonic-gate 	if (pii->pii_ntargets != 0) {
23840Sstevel@tonic-gate 		assert(pii->pii_target_next != NULL);
23850Sstevel@tonic-gate 		assert(pii->pii_rtt_target_next != NULL);
23860Sstevel@tonic-gate 		assert(pii->pii_target_next->tg_status == TG_ACTIVE);
23870Sstevel@tonic-gate 		assert(pii->pii_rtt_target_next->tg_status == TG_ACTIVE);
23880Sstevel@tonic-gate 		return;
23890Sstevel@tonic-gate 	}
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 	/* At this point, we don't have any active targets. */
23920Sstevel@tonic-gate 	assert(pii->pii_target_next == NULL);
23930Sstevel@tonic-gate 	assert(pii->pii_rtt_target_next == NULL);
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate 	if (pii->pii_targets_are_routers) {
23960Sstevel@tonic-gate 		/*
23970Sstevel@tonic-gate 		 * Activate any TG_SLOW or TG_DEAD router targets,
23980Sstevel@tonic-gate 		 * since we don't have any other targets
23990Sstevel@tonic-gate 		 */
24000Sstevel@tonic-gate 		target_activate_all(pii);
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate 		if (pii->pii_ntargets != 0) {
24030Sstevel@tonic-gate 			assert(pii->pii_target_next != NULL);
24040Sstevel@tonic-gate 			assert(pii->pii_rtt_target_next != NULL);
24050Sstevel@tonic-gate 			assert(pii->pii_target_next->tg_status == TG_ACTIVE);
24060Sstevel@tonic-gate 			assert(pii->pii_rtt_target_next->tg_status ==
24070Sstevel@tonic-gate 			    TG_ACTIVE);
24080Sstevel@tonic-gate 			return;
24090Sstevel@tonic-gate 		}
24100Sstevel@tonic-gate 	}
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 	/*
24130Sstevel@tonic-gate 	 * If we still don't have any active targets, the list must
24140Sstevel@tonic-gate 	 * must be really empty. There aren't even TG_SLOW or TG_DEAD
24150Sstevel@tonic-gate 	 * targets. Zero out the probe stats since it will not be
24160Sstevel@tonic-gate 	 * relevant any longer.
24170Sstevel@tonic-gate 	 */
24180Sstevel@tonic-gate 	assert(pii->pii_targets == NULL);
2419*8485SPeter.Memishian@Sun.COM 	pii->pii_targets_are_routers = _B_FALSE;
24200Sstevel@tonic-gate 	clear_pii_probe_stats(pii);
24210Sstevel@tonic-gate 	pii_other = phyint_inst_other(pii);
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 	/*
2424*8485SPeter.Memishian@Sun.COM 	 * If there are no targets on both instances and the interface would
2425*8485SPeter.Memishian@Sun.COM 	 * otherwise be considered PI_RUNNING, go back to PI_NOTARGETS state,
2426*8485SPeter.Memishian@Sun.COM 	 * since we cannot probe this phyint any more.  For more details,
2427*8485SPeter.Memishian@Sun.COM 	 * please see phyint state diagram in mpd_probe.c.
24280Sstevel@tonic-gate 	 */
2429*8485SPeter.Memishian@Sun.COM 	if (!PROBE_CAPABLE(pii_other) && LINK_UP(pii->pii_phyint) &&
24304770Smeem 	    pii->pii_phyint->pi_state != PI_OFFLINE)
24310Sstevel@tonic-gate 		phyint_chstate(pii->pii_phyint, PI_NOTARGETS);
24320Sstevel@tonic-gate }
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate /*
24350Sstevel@tonic-gate  * Flush the target list of every phyint in the group, if the list
24360Sstevel@tonic-gate  * is a host target list. This is called if group failure is suspected.
24370Sstevel@tonic-gate  * If all targets have failed, multicast will subsequently discover new
24380Sstevel@tonic-gate  * targets. Else it is a group failure.
24390Sstevel@tonic-gate  * Note: This function is a no-op if the list is a router target list.
24400Sstevel@tonic-gate  */
24410Sstevel@tonic-gate static void
24420Sstevel@tonic-gate target_flush_hosts(struct phyint_group *pg)
24430Sstevel@tonic-gate {
24440Sstevel@tonic-gate 	struct phyint *pi;
24450Sstevel@tonic-gate 	struct phyint_instance *pii;
24460Sstevel@tonic-gate 
24470Sstevel@tonic-gate 	if (debug & D_TARGET)
24480Sstevel@tonic-gate 		logdebug("target_flush_hosts(%s)\n", pg->pg_name);
24490Sstevel@tonic-gate 
24500Sstevel@tonic-gate 	for (pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext) {
24510Sstevel@tonic-gate 		pii = pi->pi_v4;
24520Sstevel@tonic-gate 		if (pii != NULL && !pii->pii_targets_are_routers) {
24530Sstevel@tonic-gate 			/*
24540Sstevel@tonic-gate 			 * Delete all the targets. When the list becomes
24550Sstevel@tonic-gate 			 * empty, target_delete() will set pii->pii_targets
24560Sstevel@tonic-gate 			 * to NULL.
24570Sstevel@tonic-gate 			 */
24580Sstevel@tonic-gate 			while (pii->pii_targets != NULL)
24590Sstevel@tonic-gate 				target_delete(pii->pii_targets);
24600Sstevel@tonic-gate 		}
24610Sstevel@tonic-gate 		pii = pi->pi_v6;
24620Sstevel@tonic-gate 		if (pii != NULL && !pii->pii_targets_are_routers) {
24630Sstevel@tonic-gate 			/*
24640Sstevel@tonic-gate 			 * Delete all the targets. When the list becomes
24650Sstevel@tonic-gate 			 * empty, target_delete() will set pii->pii_targets
24660Sstevel@tonic-gate 			 * to NULL.
24670Sstevel@tonic-gate 			 */
24680Sstevel@tonic-gate 			while (pii->pii_targets != NULL)
24690Sstevel@tonic-gate 				target_delete(pii->pii_targets);
24700Sstevel@tonic-gate 		}
24710Sstevel@tonic-gate 	}
24720Sstevel@tonic-gate }
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate /*
24750Sstevel@tonic-gate  * Reset all references to 'target' in the probe info, as this target is
24760Sstevel@tonic-gate  * being deleted. The pr_target field is guaranteed to be non-null if
24770Sstevel@tonic-gate  * pr_status is PR_UNACKED. So we change the pr_status to PR_LOST, so that
24780Sstevel@tonic-gate  * pr_target will not be accessed unconditionally.
24790Sstevel@tonic-gate  */
24800Sstevel@tonic-gate static void
24810Sstevel@tonic-gate reset_pii_probes(struct phyint_instance *pii, struct target *tg)
24820Sstevel@tonic-gate {
24830Sstevel@tonic-gate 	int i;
24840Sstevel@tonic-gate 
24850Sstevel@tonic-gate 	for (i = 0; i < PROBE_STATS_COUNT; i++) {
24860Sstevel@tonic-gate 		if (pii->pii_probes[i].pr_target == tg) {
2487*8485SPeter.Memishian@Sun.COM 			if (pii->pii_probes[i].pr_status == PR_UNACKED) {
2488*8485SPeter.Memishian@Sun.COM 				probe_chstate(&pii->pii_probes[i], pii,
2489*8485SPeter.Memishian@Sun.COM 				    PR_LOST);
2490*8485SPeter.Memishian@Sun.COM 			}
24910Sstevel@tonic-gate 			pii->pii_probes[i].pr_target = NULL;
24920Sstevel@tonic-gate 		}
24930Sstevel@tonic-gate 	}
24940Sstevel@tonic-gate 
24950Sstevel@tonic-gate }
24960Sstevel@tonic-gate 
24970Sstevel@tonic-gate /*
24980Sstevel@tonic-gate  * Clear the probe statistics array.
24990Sstevel@tonic-gate  */
25000Sstevel@tonic-gate void
25010Sstevel@tonic-gate clear_pii_probe_stats(struct phyint_instance *pii)
25020Sstevel@tonic-gate {
25030Sstevel@tonic-gate 	bzero(pii->pii_probes, sizeof (struct probe_stats) * PROBE_STATS_COUNT);
25040Sstevel@tonic-gate 	/* Reset the next probe index in the probe stats array */
25050Sstevel@tonic-gate 	pii->pii_probe_next = 0;
25060Sstevel@tonic-gate }
25070Sstevel@tonic-gate 
25080Sstevel@tonic-gate static void
25090Sstevel@tonic-gate target_print(struct target *tg)
25100Sstevel@tonic-gate {
25110Sstevel@tonic-gate 	char	abuf[INET6_ADDRSTRLEN];
25120Sstevel@tonic-gate 	char	buf[128];
25130Sstevel@tonic-gate 	char	buf2[128];
25140Sstevel@tonic-gate 	int	af;
25150Sstevel@tonic-gate 	int	i;
25160Sstevel@tonic-gate 
25170Sstevel@tonic-gate 	af = tg->tg_phyint_inst->pii_af;
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 	logdebug("Target on %s %s addr %s\n"
2520*8485SPeter.Memishian@Sun.COM 	    "status %d rtt_sa %lld rtt_sd %lld crtt %d tg_in_use %d\n",
25210Sstevel@tonic-gate 	    AF_STR(af), tg->tg_phyint_inst->pii_name,
25220Sstevel@tonic-gate 	    pr_addr(af, tg->tg_address, abuf, sizeof (abuf)),
25230Sstevel@tonic-gate 	    tg->tg_status, tg->tg_rtt_sa, tg->tg_rtt_sd,
25240Sstevel@tonic-gate 	    tg->tg_crtt, tg->tg_in_use);
25250Sstevel@tonic-gate 
25260Sstevel@tonic-gate 	buf[0] = '\0';
25270Sstevel@tonic-gate 	for (i = 0; i < tg->tg_num_deferred; i++) {
25280Sstevel@tonic-gate 		(void) snprintf(buf2, sizeof (buf2), " %dms",
25290Sstevel@tonic-gate 		    tg->tg_deferred[i]);
25300Sstevel@tonic-gate 		(void) strlcat(buf, buf2, sizeof (buf));
25310Sstevel@tonic-gate 	}
25320Sstevel@tonic-gate 	logdebug("deferred rtts:%s\n", buf);
25330Sstevel@tonic-gate }
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate void
25360Sstevel@tonic-gate phyint_inst_print_all(void)
25370Sstevel@tonic-gate {
25380Sstevel@tonic-gate 	struct phyint_instance *pii;
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 	for (pii = phyint_instances; pii != NULL; pii = pii->pii_next) {
25410Sstevel@tonic-gate 		phyint_inst_print(pii);
25420Sstevel@tonic-gate 	}
25430Sstevel@tonic-gate }
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate /*
25460Sstevel@tonic-gate  * Compare two prefixes that have the same prefix length.
25470Sstevel@tonic-gate  * Fails if the prefix length is unreasonable.
25480Sstevel@tonic-gate  */
2549*8485SPeter.Memishian@Sun.COM boolean_t
2550*8485SPeter.Memishian@Sun.COM prefix_equal(struct in6_addr p1, struct in6_addr p2, uint_t prefix_len)
25510Sstevel@tonic-gate {
25520Sstevel@tonic-gate 	uchar_t mask;
25530Sstevel@tonic-gate 	int j;
25540Sstevel@tonic-gate 
2555*8485SPeter.Memishian@Sun.COM 	if (prefix_len > IPV6_ABITS)
25560Sstevel@tonic-gate 		return (_B_FALSE);
25570Sstevel@tonic-gate 
25580Sstevel@tonic-gate 	for (j = 0; prefix_len > 8; prefix_len -= 8, j++)
25590Sstevel@tonic-gate 		if (p1.s6_addr[j] != p2.s6_addr[j])
25600Sstevel@tonic-gate 			return (_B_FALSE);
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 	/* Make the N leftmost bits one */
25630Sstevel@tonic-gate 	mask = 0xff << (8 - prefix_len);
25640Sstevel@tonic-gate 	if ((p1.s6_addr[j] & mask) != (p2.s6_addr[j] & mask))
25650Sstevel@tonic-gate 		return (_B_FALSE);
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate 	return (_B_TRUE);
25680Sstevel@tonic-gate }
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate /*
2571*8485SPeter.Memishian@Sun.COM  * Get the number of UP logints on phyint `pi'.
25720Sstevel@tonic-gate  */
2573*8485SPeter.Memishian@Sun.COM static int
25740Sstevel@tonic-gate logint_upcount(struct phyint *pi)
25750Sstevel@tonic-gate {
25760Sstevel@tonic-gate 	struct	logint	*li;
25770Sstevel@tonic-gate 	int count = 0;
25780Sstevel@tonic-gate 
2579*8485SPeter.Memishian@Sun.COM 	if (pi->pi_v4 != NULL) {
2580*8485SPeter.Memishian@Sun.COM 		for (li = pi->pi_v4->pii_logint; li != NULL; li = li->li_next) {
2581*8485SPeter.Memishian@Sun.COM 			if (li->li_flags & IFF_UP)
25820Sstevel@tonic-gate 				count++;
25830Sstevel@tonic-gate 		}
25840Sstevel@tonic-gate 	}
25850Sstevel@tonic-gate 
2586*8485SPeter.Memishian@Sun.COM 	if (pi->pi_v6 != NULL) {
2587*8485SPeter.Memishian@Sun.COM 		for (li = pi->pi_v6->pii_logint; li != NULL; li = li->li_next) {
2588*8485SPeter.Memishian@Sun.COM 			if (li->li_flags & IFF_UP)
25890Sstevel@tonic-gate 				count++;
25900Sstevel@tonic-gate 		}
25910Sstevel@tonic-gate 	}
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 	return (count);
25940Sstevel@tonic-gate }
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate /*
25970Sstevel@tonic-gate  * Get the phyint instance with the other (IPv4 / IPv6) protocol
25980Sstevel@tonic-gate  */
25990Sstevel@tonic-gate struct phyint_instance *
26000Sstevel@tonic-gate phyint_inst_other(struct phyint_instance *pii)
26010Sstevel@tonic-gate {
26020Sstevel@tonic-gate 	if (pii->pii_af == AF_INET)
26030Sstevel@tonic-gate 		return (pii->pii_phyint->pi_v6);
26040Sstevel@tonic-gate 	else
26050Sstevel@tonic-gate 		return (pii->pii_phyint->pi_v4);
26060Sstevel@tonic-gate }
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate /*
2609*8485SPeter.Memishian@Sun.COM  * Check whether a phyint is functioning.
2610*8485SPeter.Memishian@Sun.COM  */
2611*8485SPeter.Memishian@Sun.COM static boolean_t
2612*8485SPeter.Memishian@Sun.COM phyint_is_functioning(struct phyint *pi)
2613*8485SPeter.Memishian@Sun.COM {
2614*8485SPeter.Memishian@Sun.COM 	if (pi->pi_state == PI_RUNNING)
2615*8485SPeter.Memishian@Sun.COM 		return (_B_TRUE);
2616*8485SPeter.Memishian@Sun.COM 	return (pi->pi_state == PI_NOTARGETS && !(pi->pi_flags & IFF_FAILED));
2617*8485SPeter.Memishian@Sun.COM }
2618*8485SPeter.Memishian@Sun.COM 
2619*8485SPeter.Memishian@Sun.COM /*
2620*8485SPeter.Memishian@Sun.COM  * Check whether a phyint is usable.
2621*8485SPeter.Memishian@Sun.COM  */
2622*8485SPeter.Memishian@Sun.COM static boolean_t
2623*8485SPeter.Memishian@Sun.COM phyint_is_usable(struct phyint *pi)
2624*8485SPeter.Memishian@Sun.COM {
2625*8485SPeter.Memishian@Sun.COM 	if (logint_upcount(pi) == 0)
2626*8485SPeter.Memishian@Sun.COM 		return (_B_FALSE);
2627*8485SPeter.Memishian@Sun.COM 	return (phyint_is_functioning(pi));
2628*8485SPeter.Memishian@Sun.COM }
2629*8485SPeter.Memishian@Sun.COM 
2630*8485SPeter.Memishian@Sun.COM /*
26310Sstevel@tonic-gate  * Post an EC_IPMP sysevent of subclass `subclass' and attributes `nvl'.
26320Sstevel@tonic-gate  * Before sending the event, it prepends the current version of the IPMP
26330Sstevel@tonic-gate  * sysevent API.  Returns 0 on success, -1 on failure (in either case,
26340Sstevel@tonic-gate  * `nvl' is freed).
26350Sstevel@tonic-gate  */
26360Sstevel@tonic-gate static int
26370Sstevel@tonic-gate post_event(const char *subclass, nvlist_t *nvl)
26380Sstevel@tonic-gate {
2639*8485SPeter.Memishian@Sun.COM 	static evchan_t *evchp = NULL;
26400Sstevel@tonic-gate 
26414262Smeem 	/*
2642*8485SPeter.Memishian@Sun.COM 	 * Initialize the event channel if we haven't already done so.
26434262Smeem 	 */
2644*8485SPeter.Memishian@Sun.COM 	if (evchp == NULL) {
2645*8485SPeter.Memishian@Sun.COM 		errno = sysevent_evc_bind(IPMP_EVENT_CHAN, &evchp, EVCH_CREAT);
2646*8485SPeter.Memishian@Sun.COM 		if (errno != 0) {
2647*8485SPeter.Memishian@Sun.COM 			logerr("cannot create event channel `%s': %s\n",
2648*8485SPeter.Memishian@Sun.COM 			    IPMP_EVENT_CHAN, strerror(errno));
2649*8485SPeter.Memishian@Sun.COM 			goto failed;
2650*8485SPeter.Memishian@Sun.COM 		}
26514262Smeem 	}
26524262Smeem 
26530Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_EVENT_VERSION,
26540Sstevel@tonic-gate 	    IPMP_EVENT_CUR_VERSION);
26550Sstevel@tonic-gate 	if (errno != 0) {
26560Sstevel@tonic-gate 		logerr("cannot create `%s' event: %s", subclass,
26570Sstevel@tonic-gate 		    strerror(errno));
26580Sstevel@tonic-gate 		goto failed;
26590Sstevel@tonic-gate 	}
26600Sstevel@tonic-gate 
2661*8485SPeter.Memishian@Sun.COM 	errno = sysevent_evc_publish(evchp, EC_IPMP, subclass, "com.sun",
2662*8485SPeter.Memishian@Sun.COM 	    "in.mpathd", nvl, EVCH_NOSLEEP);
2663*8485SPeter.Memishian@Sun.COM 	if (errno != 0) {
26640Sstevel@tonic-gate 		logerr("cannot send `%s' event: %s\n", subclass,
26650Sstevel@tonic-gate 		    strerror(errno));
26660Sstevel@tonic-gate 		goto failed;
26670Sstevel@tonic-gate 	}
26680Sstevel@tonic-gate 
26690Sstevel@tonic-gate 	nvlist_free(nvl);
26700Sstevel@tonic-gate 	return (0);
26710Sstevel@tonic-gate failed:
26720Sstevel@tonic-gate 	nvlist_free(nvl);
26730Sstevel@tonic-gate 	return (-1);
26740Sstevel@tonic-gate }
26750Sstevel@tonic-gate 
26760Sstevel@tonic-gate /*
26770Sstevel@tonic-gate  * Return the external IPMP state associated with phyint `pi'.
26780Sstevel@tonic-gate  */
26790Sstevel@tonic-gate static ipmp_if_state_t
26800Sstevel@tonic-gate ifstate(struct phyint *pi)
26810Sstevel@tonic-gate {
26820Sstevel@tonic-gate 	switch (pi->pi_state) {
26830Sstevel@tonic-gate 	case PI_NOTARGETS:
2684*8485SPeter.Memishian@Sun.COM 		if (pi->pi_flags & IFF_FAILED)
2685*8485SPeter.Memishian@Sun.COM 			return (IPMP_IF_FAILED);
26860Sstevel@tonic-gate 		return (IPMP_IF_UNKNOWN);
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate 	case PI_OFFLINE:
26890Sstevel@tonic-gate 		return (IPMP_IF_OFFLINE);
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate 	case PI_FAILED:
26920Sstevel@tonic-gate 		return (IPMP_IF_FAILED);
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate 	case PI_RUNNING:
26950Sstevel@tonic-gate 		return (IPMP_IF_OK);
26960Sstevel@tonic-gate 	}
26970Sstevel@tonic-gate 
26980Sstevel@tonic-gate 	logerr("ifstate: unknown state %d; aborting\n", pi->pi_state);
26990Sstevel@tonic-gate 	abort();
27000Sstevel@tonic-gate 	/* NOTREACHED */
27010Sstevel@tonic-gate }
27020Sstevel@tonic-gate 
27030Sstevel@tonic-gate /*
27040Sstevel@tonic-gate  * Return the external IPMP interface type associated with phyint `pi'.
27050Sstevel@tonic-gate  */
27060Sstevel@tonic-gate static ipmp_if_type_t
27070Sstevel@tonic-gate iftype(struct phyint *pi)
27080Sstevel@tonic-gate {
27090Sstevel@tonic-gate 	if (pi->pi_flags & IFF_STANDBY)
27100Sstevel@tonic-gate 		return (IPMP_IF_STANDBY);
27110Sstevel@tonic-gate 	else
27120Sstevel@tonic-gate 		return (IPMP_IF_NORMAL);
27130Sstevel@tonic-gate }
27140Sstevel@tonic-gate 
27150Sstevel@tonic-gate /*
2716*8485SPeter.Memishian@Sun.COM  * Return the external IPMP link state associated with phyint `pi'.
2717*8485SPeter.Memishian@Sun.COM  */
2718*8485SPeter.Memishian@Sun.COM static ipmp_if_linkstate_t
2719*8485SPeter.Memishian@Sun.COM iflinkstate(struct phyint *pi)
2720*8485SPeter.Memishian@Sun.COM {
2721*8485SPeter.Memishian@Sun.COM 	if (!(pi->pi_notes & (DL_NOTE_LINK_UP|DL_NOTE_LINK_DOWN)))
2722*8485SPeter.Memishian@Sun.COM 		return (IPMP_LINK_UNKNOWN);
2723*8485SPeter.Memishian@Sun.COM 
2724*8485SPeter.Memishian@Sun.COM 	return (LINK_DOWN(pi) ? IPMP_LINK_DOWN : IPMP_LINK_UP);
2725*8485SPeter.Memishian@Sun.COM }
2726*8485SPeter.Memishian@Sun.COM 
2727*8485SPeter.Memishian@Sun.COM /*
2728*8485SPeter.Memishian@Sun.COM  * Return the external IPMP probe state associated with phyint `pi'.
2729*8485SPeter.Memishian@Sun.COM  */
2730*8485SPeter.Memishian@Sun.COM static ipmp_if_probestate_t
2731*8485SPeter.Memishian@Sun.COM ifprobestate(struct phyint *pi)
2732*8485SPeter.Memishian@Sun.COM {
2733*8485SPeter.Memishian@Sun.COM 	if (!PROBE_ENABLED(pi->pi_v4) && !PROBE_ENABLED(pi->pi_v6))
2734*8485SPeter.Memishian@Sun.COM 		return (IPMP_PROBE_DISABLED);
2735*8485SPeter.Memishian@Sun.COM 
2736*8485SPeter.Memishian@Sun.COM 	if (pi->pi_state == PI_FAILED)
2737*8485SPeter.Memishian@Sun.COM 		return (IPMP_PROBE_FAILED);
2738*8485SPeter.Memishian@Sun.COM 
2739*8485SPeter.Memishian@Sun.COM 	if (!PROBE_CAPABLE(pi->pi_v4) && !PROBE_CAPABLE(pi->pi_v6))
2740*8485SPeter.Memishian@Sun.COM 		return (IPMP_PROBE_UNKNOWN);
2741*8485SPeter.Memishian@Sun.COM 
2742*8485SPeter.Memishian@Sun.COM 	return (IPMP_PROBE_OK);
2743*8485SPeter.Memishian@Sun.COM }
2744*8485SPeter.Memishian@Sun.COM 
2745*8485SPeter.Memishian@Sun.COM /*
2746*8485SPeter.Memishian@Sun.COM  * Return the external IPMP target mode associated with phyint instance `pii'.
2747*8485SPeter.Memishian@Sun.COM  */
2748*8485SPeter.Memishian@Sun.COM static ipmp_if_targmode_t
2749*8485SPeter.Memishian@Sun.COM iftargmode(struct phyint_instance *pii)
2750*8485SPeter.Memishian@Sun.COM {
2751*8485SPeter.Memishian@Sun.COM 	if (!PROBE_ENABLED(pii))
2752*8485SPeter.Memishian@Sun.COM 		return (IPMP_TARG_DISABLED);
2753*8485SPeter.Memishian@Sun.COM 	else if (pii->pii_targets_are_routers)
2754*8485SPeter.Memishian@Sun.COM 		return (IPMP_TARG_ROUTES);
2755*8485SPeter.Memishian@Sun.COM 	else
2756*8485SPeter.Memishian@Sun.COM 		return (IPMP_TARG_MULTICAST);
2757*8485SPeter.Memishian@Sun.COM }
2758*8485SPeter.Memishian@Sun.COM 
2759*8485SPeter.Memishian@Sun.COM /*
2760*8485SPeter.Memishian@Sun.COM  * Return the external IPMP flags associated with phyint `pi'.
2761*8485SPeter.Memishian@Sun.COM  */
2762*8485SPeter.Memishian@Sun.COM static ipmp_if_flags_t
2763*8485SPeter.Memishian@Sun.COM ifflags(struct phyint *pi)
2764*8485SPeter.Memishian@Sun.COM {
2765*8485SPeter.Memishian@Sun.COM 	ipmp_if_flags_t flags = 0;
2766*8485SPeter.Memishian@Sun.COM 
2767*8485SPeter.Memishian@Sun.COM 	if (logint_upcount(pi) == 0)
2768*8485SPeter.Memishian@Sun.COM 		flags |= IPMP_IFFLAG_DOWN;
2769*8485SPeter.Memishian@Sun.COM 	if (pi->pi_flags & IFF_INACTIVE)
2770*8485SPeter.Memishian@Sun.COM 		flags |= IPMP_IFFLAG_INACTIVE;
2771*8485SPeter.Memishian@Sun.COM 	if (pi->pi_hwaddrdup)
2772*8485SPeter.Memishian@Sun.COM 		flags |= IPMP_IFFLAG_HWADDRDUP;
2773*8485SPeter.Memishian@Sun.COM 	if (phyint_is_functioning(pi) && flags == 0)
2774*8485SPeter.Memishian@Sun.COM 		flags |= IPMP_IFFLAG_ACTIVE;
2775*8485SPeter.Memishian@Sun.COM 
2776*8485SPeter.Memishian@Sun.COM 	return (flags);
2777*8485SPeter.Memishian@Sun.COM }
2778*8485SPeter.Memishian@Sun.COM 
2779*8485SPeter.Memishian@Sun.COM /*
2780*8485SPeter.Memishian@Sun.COM  * Store the test address used on phyint instance `pii' in `ssp'.  If there's
2781*8485SPeter.Memishian@Sun.COM  * no test address, 0.0.0.0 is stored.
2782*8485SPeter.Memishian@Sun.COM  */
2783*8485SPeter.Memishian@Sun.COM static struct sockaddr_storage *
2784*8485SPeter.Memishian@Sun.COM iftestaddr(struct phyint_instance *pii, struct sockaddr_storage *ssp)
2785*8485SPeter.Memishian@Sun.COM {
2786*8485SPeter.Memishian@Sun.COM 	if (PROBE_ENABLED(pii))
2787*8485SPeter.Memishian@Sun.COM 		addr2storage(pii->pii_af, &pii->pii_probe_logint->li_addr, ssp);
2788*8485SPeter.Memishian@Sun.COM 	else
2789*8485SPeter.Memishian@Sun.COM 		addr2storage(AF_INET6, &in6addr_any, ssp);
2790*8485SPeter.Memishian@Sun.COM 
2791*8485SPeter.Memishian@Sun.COM 	return (ssp);
2792*8485SPeter.Memishian@Sun.COM }
2793*8485SPeter.Memishian@Sun.COM 
2794*8485SPeter.Memishian@Sun.COM /*
27950Sstevel@tonic-gate  * Return the external IPMP group state associated with phyint group `pg'.
27960Sstevel@tonic-gate  */
27970Sstevel@tonic-gate static ipmp_group_state_t
27980Sstevel@tonic-gate groupstate(struct phyint_group *pg)
27990Sstevel@tonic-gate {
2800*8485SPeter.Memishian@Sun.COM 	switch (pg->pg_state) {
2801*8485SPeter.Memishian@Sun.COM 	case PG_FAILED:
2802*8485SPeter.Memishian@Sun.COM 		return (IPMP_GROUP_FAILED);
2803*8485SPeter.Memishian@Sun.COM 	case PG_DEGRADED:
2804*8485SPeter.Memishian@Sun.COM 		return (IPMP_GROUP_DEGRADED);
2805*8485SPeter.Memishian@Sun.COM 	case PG_OK:
2806*8485SPeter.Memishian@Sun.COM 		return (IPMP_GROUP_OK);
2807*8485SPeter.Memishian@Sun.COM 	}
2808*8485SPeter.Memishian@Sun.COM 
2809*8485SPeter.Memishian@Sun.COM 	logerr("groupstate: unknown state %d; aborting\n", pg->pg_state);
2810*8485SPeter.Memishian@Sun.COM 	abort();
2811*8485SPeter.Memishian@Sun.COM 	/* NOTREACHED */
2812*8485SPeter.Memishian@Sun.COM }
2813*8485SPeter.Memishian@Sun.COM 
2814*8485SPeter.Memishian@Sun.COM /*
2815*8485SPeter.Memishian@Sun.COM  * Return the external IPMP probe state associated with probe `ps'.
2816*8485SPeter.Memishian@Sun.COM  */
2817*8485SPeter.Memishian@Sun.COM static ipmp_probe_state_t
2818*8485SPeter.Memishian@Sun.COM probestate(struct probe_stats *ps)
2819*8485SPeter.Memishian@Sun.COM {
2820*8485SPeter.Memishian@Sun.COM 	switch (ps->pr_status) {
2821*8485SPeter.Memishian@Sun.COM 	case PR_UNUSED:
2822*8485SPeter.Memishian@Sun.COM 	case PR_LOST:
2823*8485SPeter.Memishian@Sun.COM 		return (IPMP_PROBE_LOST);
2824*8485SPeter.Memishian@Sun.COM 	case PR_UNACKED:
2825*8485SPeter.Memishian@Sun.COM 		return (IPMP_PROBE_SENT);
2826*8485SPeter.Memishian@Sun.COM 	case PR_ACKED:
2827*8485SPeter.Memishian@Sun.COM 		return (IPMP_PROBE_ACKED);
2828*8485SPeter.Memishian@Sun.COM 	}
2829*8485SPeter.Memishian@Sun.COM 
2830*8485SPeter.Memishian@Sun.COM 	logerr("probestate: unknown state %d; aborting\n", ps->pr_status);
2831*8485SPeter.Memishian@Sun.COM 	abort();
2832*8485SPeter.Memishian@Sun.COM 	/* NOTREACHED */
2833*8485SPeter.Memishian@Sun.COM }
2834*8485SPeter.Memishian@Sun.COM 
2835*8485SPeter.Memishian@Sun.COM /*
2836*8485SPeter.Memishian@Sun.COM  * Generate an ESC_IPMP_PROBE_STATE sysevent for the probe described by `pr'
2837*8485SPeter.Memishian@Sun.COM  * on phyint instance `pii'.  Returns 0 on success, -1 on failure.
2838*8485SPeter.Memishian@Sun.COM  */
2839*8485SPeter.Memishian@Sun.COM int
2840*8485SPeter.Memishian@Sun.COM probe_state_event(struct probe_stats *pr, struct phyint_instance *pii)
2841*8485SPeter.Memishian@Sun.COM {
2842*8485SPeter.Memishian@Sun.COM 	nvlist_t *nvl;
2843*8485SPeter.Memishian@Sun.COM 	hrtime_t proc_time = 0, recv_time = 0;
2844*8485SPeter.Memishian@Sun.COM 	struct sockaddr_storage ss;
2845*8485SPeter.Memishian@Sun.COM 	struct target *tg = pr->pr_target;
2846*8485SPeter.Memishian@Sun.COM 
2847*8485SPeter.Memishian@Sun.COM 	errno = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
2848*8485SPeter.Memishian@Sun.COM 	if (errno != 0) {
2849*8485SPeter.Memishian@Sun.COM 		logperror("cannot create `interface change' event");
2850*8485SPeter.Memishian@Sun.COM 		return (-1);
2851*8485SPeter.Memishian@Sun.COM 	}
2852*8485SPeter.Memishian@Sun.COM 
2853*8485SPeter.Memishian@Sun.COM 	errno = nvlist_add_uint32(nvl, IPMP_PROBE_ID, pr->pr_id);
2854*8485SPeter.Memishian@Sun.COM 	if (errno != 0)
2855*8485SPeter.Memishian@Sun.COM 		goto failed;
2856*8485SPeter.Memishian@Sun.COM 
2857*8485SPeter.Memishian@Sun.COM 	errno = nvlist_add_string(nvl, IPMP_IF_NAME, pii->pii_phyint->pi_name);
2858*8485SPeter.Memishian@Sun.COM 	if (errno != 0)
2859*8485SPeter.Memishian@Sun.COM 		goto failed;
2860*8485SPeter.Memishian@Sun.COM 
2861*8485SPeter.Memishian@Sun.COM 	errno = nvlist_add_uint32(nvl, IPMP_PROBE_STATE, probestate(pr));
2862*8485SPeter.Memishian@Sun.COM 	if (errno != 0)
2863*8485SPeter.Memishian@Sun.COM 		goto failed;
2864*8485SPeter.Memishian@Sun.COM 
2865*8485SPeter.Memishian@Sun.COM 	errno = nvlist_add_hrtime(nvl, IPMP_PROBE_START_TIME,
2866*8485SPeter.Memishian@Sun.COM 	    pr->pr_hrtime_start);
2867*8485SPeter.Memishian@Sun.COM 	if (errno != 0)
2868*8485SPeter.Memishian@Sun.COM 		goto failed;
2869*8485SPeter.Memishian@Sun.COM 
2870*8485SPeter.Memishian@Sun.COM 	errno = nvlist_add_hrtime(nvl, IPMP_PROBE_SENT_TIME,
2871*8485SPeter.Memishian@Sun.COM 	    pr->pr_hrtime_sent);
2872*8485SPeter.Memishian@Sun.COM 	if (errno != 0)
2873*8485SPeter.Memishian@Sun.COM 		goto failed;
2874*8485SPeter.Memishian@Sun.COM 
2875*8485SPeter.Memishian@Sun.COM 	if (pr->pr_status == PR_ACKED) {
2876*8485SPeter.Memishian@Sun.COM 		recv_time = pr->pr_hrtime_ackrecv;
2877*8485SPeter.Memishian@Sun.COM 		proc_time = pr->pr_hrtime_ackproc;
2878*8485SPeter.Memishian@Sun.COM 	}
2879*8485SPeter.Memishian@Sun.COM 
2880*8485SPeter.Memishian@Sun.COM 	errno = nvlist_add_hrtime(nvl, IPMP_PROBE_ACKRECV_TIME, recv_time);
2881*8485SPeter.Memishian@Sun.COM 	if (errno != 0)
2882*8485SPeter.Memishian@Sun.COM 		goto failed;
2883*8485SPeter.Memishian@Sun.COM 
2884*8485SPeter.Memishian@Sun.COM 	errno = nvlist_add_hrtime(nvl, IPMP_PROBE_ACKPROC_TIME, proc_time);
2885*8485SPeter.Memishian@Sun.COM 	if (errno != 0)
2886*8485SPeter.Memishian@Sun.COM 		goto failed;
2887*8485SPeter.Memishian@Sun.COM 
2888*8485SPeter.Memishian@Sun.COM 	if (tg != NULL)
2889*8485SPeter.Memishian@Sun.COM 		addr2storage(pii->pii_af, &tg->tg_address, &ss);
2890*8485SPeter.Memishian@Sun.COM 	else
2891*8485SPeter.Memishian@Sun.COM 		addr2storage(pii->pii_af, &in6addr_any, &ss);
2892*8485SPeter.Memishian@Sun.COM 
2893*8485SPeter.Memishian@Sun.COM 	errno = nvlist_add_byte_array(nvl, IPMP_PROBE_TARGET, (uchar_t *)&ss,
2894*8485SPeter.Memishian@Sun.COM 	    sizeof (ss));
2895*8485SPeter.Memishian@Sun.COM 	if (errno != 0)
2896*8485SPeter.Memishian@Sun.COM 		goto failed;
2897*8485SPeter.Memishian@Sun.COM 
2898*8485SPeter.Memishian@Sun.COM 	errno = nvlist_add_int64(nvl, IPMP_PROBE_TARGET_RTTAVG,
2899*8485SPeter.Memishian@Sun.COM 	    tg->tg_rtt_sa / 8);
2900*8485SPeter.Memishian@Sun.COM 	if (errno != 0)
2901*8485SPeter.Memishian@Sun.COM 		goto failed;
2902*8485SPeter.Memishian@Sun.COM 
2903*8485SPeter.Memishian@Sun.COM 	errno = nvlist_add_int64(nvl, IPMP_PROBE_TARGET_RTTDEV,
2904*8485SPeter.Memishian@Sun.COM 	    tg->tg_rtt_sd / 4);
2905*8485SPeter.Memishian@Sun.COM 	if (errno != 0)
2906*8485SPeter.Memishian@Sun.COM 		goto failed;
2907*8485SPeter.Memishian@Sun.COM 
2908*8485SPeter.Memishian@Sun.COM 	return (post_event(ESC_IPMP_PROBE_STATE, nvl));
2909*8485SPeter.Memishian@Sun.COM failed:
2910*8485SPeter.Memishian@Sun.COM 	logperror("cannot create `probe state' event");
2911*8485SPeter.Memishian@Sun.COM 	nvlist_free(nvl);
2912*8485SPeter.Memishian@Sun.COM 	return (-1);
29130Sstevel@tonic-gate }
29140Sstevel@tonic-gate 
29150Sstevel@tonic-gate /*
29160Sstevel@tonic-gate  * Generate an ESC_IPMP_GROUP_STATE sysevent for phyint group `pg'.
29170Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
29180Sstevel@tonic-gate  */
29190Sstevel@tonic-gate static int
29200Sstevel@tonic-gate phyint_group_state_event(struct phyint_group *pg)
29210Sstevel@tonic-gate {
29220Sstevel@tonic-gate 	nvlist_t	*nvl;
29230Sstevel@tonic-gate 
29240Sstevel@tonic-gate 	errno = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
29250Sstevel@tonic-gate 	if (errno != 0) {
29260Sstevel@tonic-gate 		logperror("cannot create `group state change' event");
29270Sstevel@tonic-gate 		return (-1);
29280Sstevel@tonic-gate 	}
29290Sstevel@tonic-gate 
29300Sstevel@tonic-gate 	errno = nvlist_add_string(nvl, IPMP_GROUP_NAME, pg->pg_name);
29310Sstevel@tonic-gate 	if (errno != 0)
29320Sstevel@tonic-gate 		goto failed;
29330Sstevel@tonic-gate 
29340Sstevel@tonic-gate 	errno = nvlist_add_uint64(nvl, IPMP_GROUP_SIGNATURE, pg->pg_sig);
29350Sstevel@tonic-gate 	if (errno != 0)
29360Sstevel@tonic-gate 		goto failed;
29370Sstevel@tonic-gate 
29380Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_GROUP_STATE, groupstate(pg));
29390Sstevel@tonic-gate 	if (errno != 0)
29400Sstevel@tonic-gate 		goto failed;
29410Sstevel@tonic-gate 
29420Sstevel@tonic-gate 	return (post_event(ESC_IPMP_GROUP_STATE, nvl));
29430Sstevel@tonic-gate failed:
29440Sstevel@tonic-gate 	logperror("cannot create `group state change' event");
29450Sstevel@tonic-gate 	nvlist_free(nvl);
29460Sstevel@tonic-gate 	return (-1);
29470Sstevel@tonic-gate }
29480Sstevel@tonic-gate 
29490Sstevel@tonic-gate /*
29500Sstevel@tonic-gate  * Generate an ESC_IPMP_GROUP_CHANGE sysevent of type `op' for phyint group
29510Sstevel@tonic-gate  * `pg'.  Returns 0 on success, -1 on failure.
29520Sstevel@tonic-gate  */
29530Sstevel@tonic-gate static int
29540Sstevel@tonic-gate phyint_group_change_event(struct phyint_group *pg, ipmp_group_op_t op)
29550Sstevel@tonic-gate {
29560Sstevel@tonic-gate 	nvlist_t *nvl;
29570Sstevel@tonic-gate 
29580Sstevel@tonic-gate 	errno = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
29590Sstevel@tonic-gate 	if (errno != 0) {
29600Sstevel@tonic-gate 		logperror("cannot create `group change' event");
29610Sstevel@tonic-gate 		return (-1);
29620Sstevel@tonic-gate 	}
29630Sstevel@tonic-gate 
29640Sstevel@tonic-gate 	errno = nvlist_add_string(nvl, IPMP_GROUP_NAME, pg->pg_name);
29650Sstevel@tonic-gate 	if (errno != 0)
29660Sstevel@tonic-gate 		goto failed;
29670Sstevel@tonic-gate 
29680Sstevel@tonic-gate 	errno = nvlist_add_uint64(nvl, IPMP_GROUP_SIGNATURE, pg->pg_sig);
29690Sstevel@tonic-gate 	if (errno != 0)
29700Sstevel@tonic-gate 		goto failed;
29710Sstevel@tonic-gate 
29720Sstevel@tonic-gate 	errno = nvlist_add_uint64(nvl, IPMP_GROUPLIST_SIGNATURE,
29730Sstevel@tonic-gate 	    phyint_grouplistsig);
29740Sstevel@tonic-gate 	if (errno != 0)
29750Sstevel@tonic-gate 		goto failed;
29760Sstevel@tonic-gate 
29770Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_GROUP_OPERATION, op);
29780Sstevel@tonic-gate 	if (errno != 0)
29790Sstevel@tonic-gate 		goto failed;
29800Sstevel@tonic-gate 
29810Sstevel@tonic-gate 	return (post_event(ESC_IPMP_GROUP_CHANGE, nvl));
29820Sstevel@tonic-gate failed:
29830Sstevel@tonic-gate 	logperror("cannot create `group change' event");
29840Sstevel@tonic-gate 	nvlist_free(nvl);
29850Sstevel@tonic-gate 	return (-1);
29860Sstevel@tonic-gate }
29870Sstevel@tonic-gate 
29880Sstevel@tonic-gate /*
29890Sstevel@tonic-gate  * Generate an ESC_IPMP_GROUP_MEMBER_CHANGE sysevent for phyint `pi' in
29900Sstevel@tonic-gate  * group `pg'.	Returns 0 on success, -1 on failure.
29910Sstevel@tonic-gate  */
29920Sstevel@tonic-gate static int
29930Sstevel@tonic-gate phyint_group_member_event(struct phyint_group *pg, struct phyint *pi,
29940Sstevel@tonic-gate     ipmp_if_op_t op)
29950Sstevel@tonic-gate {
29960Sstevel@tonic-gate 	nvlist_t *nvl;
29970Sstevel@tonic-gate 
29980Sstevel@tonic-gate 	errno = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
29990Sstevel@tonic-gate 	if (errno != 0) {
30000Sstevel@tonic-gate 		logperror("cannot create `group member change' event");
30010Sstevel@tonic-gate 		return (-1);
30020Sstevel@tonic-gate 	}
30030Sstevel@tonic-gate 
30040Sstevel@tonic-gate 	errno = nvlist_add_string(nvl, IPMP_GROUP_NAME, pg->pg_name);
30050Sstevel@tonic-gate 	if (errno != 0)
30060Sstevel@tonic-gate 		goto failed;
30070Sstevel@tonic-gate 
30080Sstevel@tonic-gate 	errno = nvlist_add_uint64(nvl, IPMP_GROUP_SIGNATURE, pg->pg_sig);
30090Sstevel@tonic-gate 	if (errno != 0)
30100Sstevel@tonic-gate 		goto failed;
30110Sstevel@tonic-gate 
30120Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_IF_OPERATION, op);
30130Sstevel@tonic-gate 	if (errno != 0)
30140Sstevel@tonic-gate 		goto failed;
30150Sstevel@tonic-gate 
30160Sstevel@tonic-gate 	errno = nvlist_add_string(nvl, IPMP_IF_NAME, pi->pi_name);
30170Sstevel@tonic-gate 	if (errno != 0)
30180Sstevel@tonic-gate 		goto failed;
30190Sstevel@tonic-gate 
30200Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_IF_TYPE, iftype(pi));
30210Sstevel@tonic-gate 	if (errno != 0)
30220Sstevel@tonic-gate 		goto failed;
30230Sstevel@tonic-gate 
30240Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_IF_STATE, ifstate(pi));
30250Sstevel@tonic-gate 	if (errno != 0)
30260Sstevel@tonic-gate 		goto failed;
30270Sstevel@tonic-gate 
30280Sstevel@tonic-gate 	return (post_event(ESC_IPMP_GROUP_MEMBER_CHANGE, nvl));
30290Sstevel@tonic-gate failed:
30300Sstevel@tonic-gate 	logperror("cannot create `group member change' event");
30310Sstevel@tonic-gate 	nvlist_free(nvl);
30320Sstevel@tonic-gate 	return (-1);
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate }
30350Sstevel@tonic-gate 
30360Sstevel@tonic-gate /*
30370Sstevel@tonic-gate  * Generate an ESC_IPMP_IF_CHANGE sysevent for phyint `pi' in group `pg'.
30380Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
30390Sstevel@tonic-gate  */
30400Sstevel@tonic-gate static int
30410Sstevel@tonic-gate phyint_state_event(struct phyint_group *pg, struct phyint *pi)
30420Sstevel@tonic-gate {
30430Sstevel@tonic-gate 	nvlist_t *nvl;
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate 	errno = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
30460Sstevel@tonic-gate 	if (errno != 0) {
30470Sstevel@tonic-gate 		logperror("cannot create `interface change' event");
30480Sstevel@tonic-gate 		return (-1);
30490Sstevel@tonic-gate 	}
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate 	errno = nvlist_add_string(nvl, IPMP_GROUP_NAME, pg->pg_name);
30520Sstevel@tonic-gate 	if (errno != 0)
30530Sstevel@tonic-gate 		goto failed;
30540Sstevel@tonic-gate 
30550Sstevel@tonic-gate 	errno = nvlist_add_uint64(nvl, IPMP_GROUP_SIGNATURE, pg->pg_sig);
30560Sstevel@tonic-gate 	if (errno != 0)
30570Sstevel@tonic-gate 		goto failed;
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate 	errno = nvlist_add_string(nvl, IPMP_IF_NAME, pi->pi_name);
30600Sstevel@tonic-gate 	if (errno != 0)
30610Sstevel@tonic-gate 		goto failed;
30620Sstevel@tonic-gate 
30630Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_IF_TYPE, iftype(pi));
30640Sstevel@tonic-gate 	if (errno != 0)
30650Sstevel@tonic-gate 		goto failed;
30660Sstevel@tonic-gate 
30670Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_IF_STATE, ifstate(pi));
30680Sstevel@tonic-gate 	if (errno != 0)
30690Sstevel@tonic-gate 		goto failed;
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate 	return (post_event(ESC_IPMP_IF_CHANGE, nvl));
30720Sstevel@tonic-gate failed:
30730Sstevel@tonic-gate 	logperror("cannot create `interface change' event");
30740Sstevel@tonic-gate 	nvlist_free(nvl);
30750Sstevel@tonic-gate 	return (-1);
30760Sstevel@tonic-gate 
30770Sstevel@tonic-gate }
30780Sstevel@tonic-gate 
30790Sstevel@tonic-gate /*
30800Sstevel@tonic-gate  * Generate a signature for use.  The signature is conceptually divided
30810Sstevel@tonic-gate  * into two pieces: a random 16-bit "generation number" and a 48-bit
30820Sstevel@tonic-gate  * monotonically increasing integer.  The generation number protects
30830Sstevel@tonic-gate  * against stale updates to entities (e.g., IPMP groups) that have been
30840Sstevel@tonic-gate  * deleted and since recreated.
30850Sstevel@tonic-gate  */
30860Sstevel@tonic-gate static uint64_t
30870Sstevel@tonic-gate gensig(void)
30880Sstevel@tonic-gate {
30890Sstevel@tonic-gate 	static int seeded = 0;
30900Sstevel@tonic-gate 
30910Sstevel@tonic-gate 	if (seeded == 0) {
30920Sstevel@tonic-gate 		srand48((long)gethrtime());
30930Sstevel@tonic-gate 		seeded++;
30940Sstevel@tonic-gate 	}
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate 	return ((uint64_t)lrand48() << 48 | 1);
30970Sstevel@tonic-gate }
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate /*
31000Sstevel@tonic-gate  * Store the information associated with group `grname' into a dynamically
31010Sstevel@tonic-gate  * allocated structure pointed to by `*grinfopp'.  Returns an IPMP error code.
31020Sstevel@tonic-gate  */
31030Sstevel@tonic-gate unsigned int
31040Sstevel@tonic-gate getgroupinfo(const char *grname, ipmp_groupinfo_t **grinfopp)
31050Sstevel@tonic-gate {
3106*8485SPeter.Memishian@Sun.COM 	struct phyint		*pi;
31070Sstevel@tonic-gate 	struct phyint_group	*pg;
31080Sstevel@tonic-gate 	char			(*ifs)[LIFNAMSIZ];
3109*8485SPeter.Memishian@Sun.COM 	unsigned int		i, j;
3110*8485SPeter.Memishian@Sun.COM 	unsigned int		nif = 0, naddr = 0;
3111*8485SPeter.Memishian@Sun.COM 	lifgroupinfo_t		lifgr;
3112*8485SPeter.Memishian@Sun.COM 	addrlist_t		*addrp;
3113*8485SPeter.Memishian@Sun.COM 	struct sockaddr_storage	*addrs;
3114*8485SPeter.Memishian@Sun.COM 	int			fdt = 0;
31150Sstevel@tonic-gate 
31160Sstevel@tonic-gate 	pg = phyint_group_lookup(grname);
31170Sstevel@tonic-gate 	if (pg == NULL)
31180Sstevel@tonic-gate 		return (IPMP_EUNKGROUP);
31190Sstevel@tonic-gate 
31200Sstevel@tonic-gate 	/*
31210Sstevel@tonic-gate 	 * Tally up the number of interfaces, allocate an array to hold them,
3122*8485SPeter.Memishian@Sun.COM 	 * and insert their names into the array.  While we're at it, if any
3123*8485SPeter.Memishian@Sun.COM 	 * interface is actually enabled to send probes, save the group fdt.
31240Sstevel@tonic-gate 	 */
3125*8485SPeter.Memishian@Sun.COM 	for (pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext)
31260Sstevel@tonic-gate 		nif++;
31270Sstevel@tonic-gate 
31280Sstevel@tonic-gate 	ifs = alloca(nif * sizeof (*ifs));
31290Sstevel@tonic-gate 	for (i = 0, pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext, i++) {
31300Sstevel@tonic-gate 		assert(i < nif);
31310Sstevel@tonic-gate 		(void) strlcpy(ifs[i], pi->pi_name, LIFNAMSIZ);
3132*8485SPeter.Memishian@Sun.COM 		if (PROBE_ENABLED(pi->pi_v4) || PROBE_ENABLED(pi->pi_v6))
3133*8485SPeter.Memishian@Sun.COM 			fdt = pg->pg_fdt;
31340Sstevel@tonic-gate 	}
31350Sstevel@tonic-gate 	assert(i == nif);
31360Sstevel@tonic-gate 
3137*8485SPeter.Memishian@Sun.COM 	/*
3138*8485SPeter.Memishian@Sun.COM 	 * If this is the anonymous group, there's no other information to
3139*8485SPeter.Memishian@Sun.COM 	 * collect (since there's no IPMP interface).
3140*8485SPeter.Memishian@Sun.COM 	 */
3141*8485SPeter.Memishian@Sun.COM 	if (pg == phyint_anongroup) {
3142*8485SPeter.Memishian@Sun.COM 		*grinfopp = ipmp_groupinfo_create(pg->pg_name, pg->pg_sig, fdt,
3143*8485SPeter.Memishian@Sun.COM 		    groupstate(pg), nif, ifs, "", "", "", "", 0, NULL);
3144*8485SPeter.Memishian@Sun.COM 		return (*grinfopp == NULL ? IPMP_ENOMEM : IPMP_SUCCESS);
3145*8485SPeter.Memishian@Sun.COM 	}
3146*8485SPeter.Memishian@Sun.COM 
3147*8485SPeter.Memishian@Sun.COM 	/*
3148*8485SPeter.Memishian@Sun.COM 	 * Grab some additional information about the group from the kernel.
3149*8485SPeter.Memishian@Sun.COM 	 * (NOTE: since SIOCGLIFGROUPINFO does not look up by interface name,
3150*8485SPeter.Memishian@Sun.COM 	 * we can use ifsock_v4 even for a V6-only group.)
3151*8485SPeter.Memishian@Sun.COM 	 */
3152*8485SPeter.Memishian@Sun.COM 	(void) strlcpy(lifgr.gi_grname, grname, LIFGRNAMSIZ);
3153*8485SPeter.Memishian@Sun.COM 	if (ioctl(ifsock_v4, SIOCGLIFGROUPINFO, &lifgr) == -1) {
3154*8485SPeter.Memishian@Sun.COM 		if (errno == ENOENT)
3155*8485SPeter.Memishian@Sun.COM 			return (IPMP_EUNKGROUP);
3156*8485SPeter.Memishian@Sun.COM 
3157*8485SPeter.Memishian@Sun.COM 		logperror("getgroupinfo: SIOCGLIFGROUPINFO");
3158*8485SPeter.Memishian@Sun.COM 		return (IPMP_FAILURE);
3159*8485SPeter.Memishian@Sun.COM 	}
3160*8485SPeter.Memishian@Sun.COM 
3161*8485SPeter.Memishian@Sun.COM 	/*
3162*8485SPeter.Memishian@Sun.COM 	 * Tally up the number of data addresses, allocate an array to hold
3163*8485SPeter.Memishian@Sun.COM 	 * them, and insert their values into the array.
3164*8485SPeter.Memishian@Sun.COM 	 */
3165*8485SPeter.Memishian@Sun.COM 	for (addrp = pg->pg_addrs; addrp != NULL; addrp = addrp->al_next)
3166*8485SPeter.Memishian@Sun.COM 		naddr++;
3167*8485SPeter.Memishian@Sun.COM 
3168*8485SPeter.Memishian@Sun.COM 	addrs = alloca(naddr * sizeof (*addrs));
3169*8485SPeter.Memishian@Sun.COM 	i = 0;
3170*8485SPeter.Memishian@Sun.COM 	for (addrp = pg->pg_addrs; addrp != NULL; addrp = addrp->al_next) {
3171*8485SPeter.Memishian@Sun.COM 		/*
3172*8485SPeter.Memishian@Sun.COM 		 * It's possible to have duplicate addresses (if some are
3173*8485SPeter.Memishian@Sun.COM 		 * down).  Weed the dups out to avoid confusing consumers.
3174*8485SPeter.Memishian@Sun.COM 		 * (If groups start having tons of addresses, we'll need a
3175*8485SPeter.Memishian@Sun.COM 		 * better algorithm here.)
3176*8485SPeter.Memishian@Sun.COM 		 */
3177*8485SPeter.Memishian@Sun.COM 		for (j = 0; j < i; j++) {
3178*8485SPeter.Memishian@Sun.COM 			if (sockaddrcmp(&addrs[j], &addrp->al_addr))
3179*8485SPeter.Memishian@Sun.COM 				break;
3180*8485SPeter.Memishian@Sun.COM 		}
3181*8485SPeter.Memishian@Sun.COM 		if (j == i) {
3182*8485SPeter.Memishian@Sun.COM 			assert(i < naddr);
3183*8485SPeter.Memishian@Sun.COM 			addrs[i++] = addrp->al_addr;
3184*8485SPeter.Memishian@Sun.COM 		}
3185*8485SPeter.Memishian@Sun.COM 	}
3186*8485SPeter.Memishian@Sun.COM 	naddr = i;
3187*8485SPeter.Memishian@Sun.COM 
3188*8485SPeter.Memishian@Sun.COM 	*grinfopp = ipmp_groupinfo_create(pg->pg_name, pg->pg_sig, fdt,
3189*8485SPeter.Memishian@Sun.COM 	    groupstate(pg), nif, ifs, lifgr.gi_grifname, lifgr.gi_m4ifname,
3190*8485SPeter.Memishian@Sun.COM 	    lifgr.gi_m6ifname, lifgr.gi_bcifname, naddr, addrs);
31910Sstevel@tonic-gate 	return (*grinfopp == NULL ? IPMP_ENOMEM : IPMP_SUCCESS);
31920Sstevel@tonic-gate }
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate /*
3195*8485SPeter.Memishian@Sun.COM  * Store the target information associated with phyint instance `pii' into a
3196*8485SPeter.Memishian@Sun.COM  * dynamically allocated structure pointed to by `*targinfopp'.  Returns an
3197*8485SPeter.Memishian@Sun.COM  * IPMP error code.
3198*8485SPeter.Memishian@Sun.COM  */
3199*8485SPeter.Memishian@Sun.COM unsigned int
3200*8485SPeter.Memishian@Sun.COM gettarginfo(struct phyint_instance *pii, const char *name,
3201*8485SPeter.Memishian@Sun.COM     ipmp_targinfo_t **targinfopp)
3202*8485SPeter.Memishian@Sun.COM {
3203*8485SPeter.Memishian@Sun.COM 	uint_t ntarg = 0;
3204*8485SPeter.Memishian@Sun.COM 	struct target *tg;
3205*8485SPeter.Memishian@Sun.COM 	struct sockaddr_storage	ss;
3206*8485SPeter.Memishian@Sun.COM 	struct sockaddr_storage *targs = NULL;
3207*8485SPeter.Memishian@Sun.COM 
3208*8485SPeter.Memishian@Sun.COM 	if (PROBE_CAPABLE(pii)) {
3209*8485SPeter.Memishian@Sun.COM 		targs = alloca(pii->pii_ntargets * sizeof (*targs));
3210*8485SPeter.Memishian@Sun.COM 		tg = pii->pii_target_next;
3211*8485SPeter.Memishian@Sun.COM 		do {
3212*8485SPeter.Memishian@Sun.COM 			if (tg->tg_status == TG_ACTIVE) {
3213*8485SPeter.Memishian@Sun.COM 				assert(ntarg < pii->pii_ntargets);
3214*8485SPeter.Memishian@Sun.COM 				addr2storage(pii->pii_af, &tg->tg_address,
3215*8485SPeter.Memishian@Sun.COM 				    &targs[ntarg++]);
3216*8485SPeter.Memishian@Sun.COM 			}
3217*8485SPeter.Memishian@Sun.COM 			if ((tg = tg->tg_next) == NULL)
3218*8485SPeter.Memishian@Sun.COM 				tg = pii->pii_targets;
3219*8485SPeter.Memishian@Sun.COM 		} while (tg != pii->pii_target_next);
3220*8485SPeter.Memishian@Sun.COM 
3221*8485SPeter.Memishian@Sun.COM 		assert(ntarg == pii->pii_ntargets);
3222*8485SPeter.Memishian@Sun.COM 	}
3223*8485SPeter.Memishian@Sun.COM 
3224*8485SPeter.Memishian@Sun.COM 	*targinfopp = ipmp_targinfo_create(name, iftestaddr(pii, &ss),
3225*8485SPeter.Memishian@Sun.COM 	    iftargmode(pii), ntarg, targs);
3226*8485SPeter.Memishian@Sun.COM 	return (*targinfopp == NULL ? IPMP_ENOMEM : IPMP_SUCCESS);
3227*8485SPeter.Memishian@Sun.COM }
3228*8485SPeter.Memishian@Sun.COM 
3229*8485SPeter.Memishian@Sun.COM /*
32300Sstevel@tonic-gate  * Store the information associated with interface `ifname' into a dynamically
32310Sstevel@tonic-gate  * allocated structure pointed to by `*ifinfopp'.  Returns an IPMP error code.
32320Sstevel@tonic-gate  */
32330Sstevel@tonic-gate unsigned int
32340Sstevel@tonic-gate getifinfo(const char *ifname, ipmp_ifinfo_t **ifinfopp)
32350Sstevel@tonic-gate {
3236*8485SPeter.Memishian@Sun.COM 	int		retval;
32370Sstevel@tonic-gate 	struct phyint	*pi;
3238*8485SPeter.Memishian@Sun.COM 	ipmp_targinfo_t	*targinfo4;
3239*8485SPeter.Memishian@Sun.COM 	ipmp_targinfo_t	*targinfo6;
32400Sstevel@tonic-gate 
32410Sstevel@tonic-gate 	pi = phyint_lookup(ifname);
32420Sstevel@tonic-gate 	if (pi == NULL)
32430Sstevel@tonic-gate 		return (IPMP_EUNKIF);
32440Sstevel@tonic-gate 
3245*8485SPeter.Memishian@Sun.COM 	if ((retval = gettarginfo(pi->pi_v4, pi->pi_name, &targinfo4)) != 0 ||
3246*8485SPeter.Memishian@Sun.COM 	    (retval = gettarginfo(pi->pi_v6, pi->pi_name, &targinfo6)) != 0)
3247*8485SPeter.Memishian@Sun.COM 		goto out;
3248*8485SPeter.Memishian@Sun.COM 
32490Sstevel@tonic-gate 	*ifinfopp = ipmp_ifinfo_create(pi->pi_name, pi->pi_group->pg_name,
3250*8485SPeter.Memishian@Sun.COM 	    ifstate(pi), iftype(pi), iflinkstate(pi), ifprobestate(pi),
3251*8485SPeter.Memishian@Sun.COM 	    ifflags(pi), targinfo4, targinfo6);
3252*8485SPeter.Memishian@Sun.COM 	retval = (*ifinfopp == NULL ? IPMP_ENOMEM : IPMP_SUCCESS);
3253*8485SPeter.Memishian@Sun.COM out:
3254*8485SPeter.Memishian@Sun.COM 	if (targinfo4 != NULL)
3255*8485SPeter.Memishian@Sun.COM 		ipmp_freetarginfo(targinfo4);
3256*8485SPeter.Memishian@Sun.COM 	if (targinfo6 != NULL)
3257*8485SPeter.Memishian@Sun.COM 		ipmp_freetarginfo(targinfo6);
3258*8485SPeter.Memishian@Sun.COM 	return (retval);
32590Sstevel@tonic-gate }
32600Sstevel@tonic-gate 
32610Sstevel@tonic-gate /*
32620Sstevel@tonic-gate  * Store the current list of IPMP groups into a dynamically allocated
32630Sstevel@tonic-gate  * structure pointed to by `*grlistpp'.	 Returns an IPMP error code.
32640Sstevel@tonic-gate  */
32650Sstevel@tonic-gate unsigned int
32660Sstevel@tonic-gate getgrouplist(ipmp_grouplist_t **grlistpp)
32670Sstevel@tonic-gate {
32680Sstevel@tonic-gate 	struct phyint_group	*pg;
32690Sstevel@tonic-gate 	char			(*groups)[LIFGRNAMSIZ];
32700Sstevel@tonic-gate 	unsigned int		i, ngroup;
32710Sstevel@tonic-gate 
32720Sstevel@tonic-gate 	/*
32730Sstevel@tonic-gate 	 * Tally up the number of groups, allocate an array to hold them, and
32740Sstevel@tonic-gate 	 * insert their names into the array.
32750Sstevel@tonic-gate 	 */
32760Sstevel@tonic-gate 	for (ngroup = 0, pg = phyint_groups; pg != NULL; pg = pg->pg_next)
32770Sstevel@tonic-gate 		ngroup++;
32780Sstevel@tonic-gate 
32790Sstevel@tonic-gate 	groups = alloca(ngroup * sizeof (*groups));
32800Sstevel@tonic-gate 	for (i = 0, pg = phyint_groups; pg != NULL; pg = pg->pg_next, i++) {
32810Sstevel@tonic-gate 		assert(i < ngroup);
32820Sstevel@tonic-gate 		(void) strlcpy(groups[i], pg->pg_name, LIFGRNAMSIZ);
32830Sstevel@tonic-gate 	}
32840Sstevel@tonic-gate 	assert(i == ngroup);
32850Sstevel@tonic-gate 
32860Sstevel@tonic-gate 	*grlistpp = ipmp_grouplist_create(phyint_grouplistsig, ngroup, groups);
32870Sstevel@tonic-gate 	return (*grlistpp == NULL ? IPMP_ENOMEM : IPMP_SUCCESS);
32880Sstevel@tonic-gate }
32890Sstevel@tonic-gate 
32900Sstevel@tonic-gate /*
3291*8485SPeter.Memishian@Sun.COM  * Store the address information for `ssp' (in group `grname') into a
3292*8485SPeter.Memishian@Sun.COM  * dynamically allocated structure pointed to by `*adinfopp'.  Returns an IPMP
3293*8485SPeter.Memishian@Sun.COM  * error code.  (We'd call this function getaddrinfo(), but it would conflict
3294*8485SPeter.Memishian@Sun.COM  * with getaddrinfo(3SOCKET)).
3295*8485SPeter.Memishian@Sun.COM  */
3296*8485SPeter.Memishian@Sun.COM unsigned int
3297*8485SPeter.Memishian@Sun.COM getgraddrinfo(const char *grname, struct sockaddr_storage *ssp,
3298*8485SPeter.Memishian@Sun.COM     ipmp_addrinfo_t **adinfopp)
3299*8485SPeter.Memishian@Sun.COM {
3300*8485SPeter.Memishian@Sun.COM 	int ifsock;
3301*8485SPeter.Memishian@Sun.COM 	addrlist_t *addrp, *addrmatchp = NULL;
3302*8485SPeter.Memishian@Sun.COM 	ipmp_addr_state_t state;
3303*8485SPeter.Memishian@Sun.COM 	const char *binding = "";
3304*8485SPeter.Memishian@Sun.COM 	struct lifreq lifr;
3305*8485SPeter.Memishian@Sun.COM 	struct phyint_group *pg;
3306*8485SPeter.Memishian@Sun.COM 
3307*8485SPeter.Memishian@Sun.COM 	if ((pg = phyint_group_lookup(grname)) == NULL)
3308*8485SPeter.Memishian@Sun.COM 		return (IPMP_EUNKADDR);
3309*8485SPeter.Memishian@Sun.COM 
3310*8485SPeter.Memishian@Sun.COM 	/*
3311*8485SPeter.Memishian@Sun.COM 	 * Walk through the data addresses, and find a match.  Note that since
3312*8485SPeter.Memishian@Sun.COM 	 * some of the addresses may be down, more than one may match.  We
3313*8485SPeter.Memishian@Sun.COM 	 * prefer an up address (if one exists).
3314*8485SPeter.Memishian@Sun.COM 	 */
3315*8485SPeter.Memishian@Sun.COM 	for (addrp = pg->pg_addrs; addrp != NULL; addrp = addrp->al_next) {
3316*8485SPeter.Memishian@Sun.COM 		if (sockaddrcmp(ssp, &addrp->al_addr)) {
3317*8485SPeter.Memishian@Sun.COM 			addrmatchp = addrp;
3318*8485SPeter.Memishian@Sun.COM 			if (addrmatchp->al_flags & IFF_UP)
3319*8485SPeter.Memishian@Sun.COM 				break;
3320*8485SPeter.Memishian@Sun.COM 		}
3321*8485SPeter.Memishian@Sun.COM 	}
3322*8485SPeter.Memishian@Sun.COM 
3323*8485SPeter.Memishian@Sun.COM 	if (addrmatchp == NULL)
3324*8485SPeter.Memishian@Sun.COM 		return (IPMP_EUNKADDR);
3325*8485SPeter.Memishian@Sun.COM 
3326*8485SPeter.Memishian@Sun.COM 	state = (addrmatchp->al_flags & IFF_UP) ? IPMP_ADDR_UP : IPMP_ADDR_DOWN;
3327*8485SPeter.Memishian@Sun.COM 	if (state == IPMP_ADDR_UP) {
3328*8485SPeter.Memishian@Sun.COM 		ifsock = (ssp->ss_family == AF_INET) ? ifsock_v4 : ifsock_v6;
3329*8485SPeter.Memishian@Sun.COM 		(void) strlcpy(lifr.lifr_name, addrmatchp->al_name, LIFNAMSIZ);
3330*8485SPeter.Memishian@Sun.COM 		if (ioctl(ifsock, SIOCGLIFBINDING, &lifr) >= 0)
3331*8485SPeter.Memishian@Sun.COM 			binding = lifr.lifr_binding;
3332*8485SPeter.Memishian@Sun.COM 	}
3333*8485SPeter.Memishian@Sun.COM 
3334*8485SPeter.Memishian@Sun.COM 	*adinfopp = ipmp_addrinfo_create(ssp, state, pg->pg_name, binding);
3335*8485SPeter.Memishian@Sun.COM 	return (*adinfopp == NULL ? IPMP_ENOMEM : IPMP_SUCCESS);
3336*8485SPeter.Memishian@Sun.COM }
3337*8485SPeter.Memishian@Sun.COM 
3338*8485SPeter.Memishian@Sun.COM /*
33390Sstevel@tonic-gate  * Store a snapshot of the IPMP subsystem into a dynamically allocated
33400Sstevel@tonic-gate  * structure pointed to by `*snapp'.  Returns an IPMP error code.
33410Sstevel@tonic-gate  */
33420Sstevel@tonic-gate unsigned int
33430Sstevel@tonic-gate getsnap(ipmp_snap_t **snapp)
33440Sstevel@tonic-gate {
33450Sstevel@tonic-gate 	ipmp_grouplist_t	*grlistp;
33460Sstevel@tonic-gate 	ipmp_groupinfo_t	*grinfop;
3347*8485SPeter.Memishian@Sun.COM 	ipmp_addrinfo_t		*adinfop;
3348*8485SPeter.Memishian@Sun.COM 	ipmp_addrlist_t		*adlistp;
33490Sstevel@tonic-gate 	ipmp_ifinfo_t		*ifinfop;
33500Sstevel@tonic-gate 	ipmp_snap_t		*snap;
33510Sstevel@tonic-gate 	struct phyint		*pi;
3352*8485SPeter.Memishian@Sun.COM 	unsigned int		i, j;
33530Sstevel@tonic-gate 	int			retval;
33540Sstevel@tonic-gate 
33550Sstevel@tonic-gate 	snap = ipmp_snap_create();
33560Sstevel@tonic-gate 	if (snap == NULL)
33570Sstevel@tonic-gate 		return (IPMP_ENOMEM);
33580Sstevel@tonic-gate 
33590Sstevel@tonic-gate 	/*
33600Sstevel@tonic-gate 	 * Add group list.
33610Sstevel@tonic-gate 	 */
33620Sstevel@tonic-gate 	retval = getgrouplist(&snap->sn_grlistp);
3363*8485SPeter.Memishian@Sun.COM 	if (retval != IPMP_SUCCESS)
3364*8485SPeter.Memishian@Sun.COM 		goto failed;
33650Sstevel@tonic-gate 
33660Sstevel@tonic-gate 	/*
3367*8485SPeter.Memishian@Sun.COM 	 * Add information for each group in the list, along with all of its
3368*8485SPeter.Memishian@Sun.COM 	 * data addresses.
33690Sstevel@tonic-gate 	 */
33700Sstevel@tonic-gate 	grlistp = snap->sn_grlistp;
33710Sstevel@tonic-gate 	for (i = 0; i < grlistp->gl_ngroup; i++) {
33720Sstevel@tonic-gate 		retval = getgroupinfo(grlistp->gl_groups[i], &grinfop);
3373*8485SPeter.Memishian@Sun.COM 		if (retval != IPMP_SUCCESS)
3374*8485SPeter.Memishian@Sun.COM 			goto failed;
3375*8485SPeter.Memishian@Sun.COM 
33760Sstevel@tonic-gate 		retval = ipmp_snap_addgroupinfo(snap, grinfop);
33770Sstevel@tonic-gate 		if (retval != IPMP_SUCCESS) {
33780Sstevel@tonic-gate 			ipmp_freegroupinfo(grinfop);
3379*8485SPeter.Memishian@Sun.COM 			goto failed;
3380*8485SPeter.Memishian@Sun.COM 		}
3381*8485SPeter.Memishian@Sun.COM 
3382*8485SPeter.Memishian@Sun.COM 		adlistp = grinfop->gr_adlistp;
3383*8485SPeter.Memishian@Sun.COM 		for (j = 0; j < adlistp->al_naddr; j++) {
3384*8485SPeter.Memishian@Sun.COM 			retval = getgraddrinfo(grinfop->gr_name,
3385*8485SPeter.Memishian@Sun.COM 			    &adlistp->al_addrs[j], &adinfop);
3386*8485SPeter.Memishian@Sun.COM 			if (retval != IPMP_SUCCESS)
3387*8485SPeter.Memishian@Sun.COM 				goto failed;
3388*8485SPeter.Memishian@Sun.COM 
3389*8485SPeter.Memishian@Sun.COM 			retval = ipmp_snap_addaddrinfo(snap, adinfop);
3390*8485SPeter.Memishian@Sun.COM 			if (retval != IPMP_SUCCESS) {
3391*8485SPeter.Memishian@Sun.COM 				ipmp_freeaddrinfo(adinfop);
3392*8485SPeter.Memishian@Sun.COM 				goto failed;
3393*8485SPeter.Memishian@Sun.COM 			}
33940Sstevel@tonic-gate 		}
33950Sstevel@tonic-gate 	}
33960Sstevel@tonic-gate 
33970Sstevel@tonic-gate 	/*
33980Sstevel@tonic-gate 	 * Add information for each configured phyint.
33990Sstevel@tonic-gate 	 */
34000Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
34010Sstevel@tonic-gate 		retval = getifinfo(pi->pi_name, &ifinfop);
3402*8485SPeter.Memishian@Sun.COM 		if (retval != IPMP_SUCCESS)
3403*8485SPeter.Memishian@Sun.COM 			goto failed;
3404*8485SPeter.Memishian@Sun.COM 
34050Sstevel@tonic-gate 		retval = ipmp_snap_addifinfo(snap, ifinfop);
34060Sstevel@tonic-gate 		if (retval != IPMP_SUCCESS) {
34070Sstevel@tonic-gate 			ipmp_freeifinfo(ifinfop);
3408*8485SPeter.Memishian@Sun.COM 			goto failed;
34090Sstevel@tonic-gate 		}
34100Sstevel@tonic-gate 	}
34110Sstevel@tonic-gate 
34120Sstevel@tonic-gate 	*snapp = snap;
34130Sstevel@tonic-gate 	return (IPMP_SUCCESS);
3414*8485SPeter.Memishian@Sun.COM failed:
3415*8485SPeter.Memishian@Sun.COM 	ipmp_snap_free(snap);
3416*8485SPeter.Memishian@Sun.COM 	return (retval);
34170Sstevel@tonic-gate }
3418