xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.lib/in.mpathd/mpd_tables.c (revision 10649:ab3ce9d83b84)
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 /*
228485SPeter.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);
488485SPeter.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 
718485SPeter.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
phyint_init(void)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 *
phyint_lookup(const char * name)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 
1048485SPeter.Memishian@Sun.COM /*
1058485SPeter.Memishian@Sun.COM  * Lookup a phyint in the group that has the same hardware address as `pi', or
1068485SPeter.Memishian@Sun.COM  * NULL if there's none.  If `online_only' is set, then only online phyints
1078485SPeter.Memishian@Sun.COM  * are considered when matching.  Otherwise, phyints that had been offlined
1088485SPeter.Memishian@Sun.COM  * due to a duplicate hardware address will also be considered.
1098485SPeter.Memishian@Sun.COM  */
1108485SPeter.Memishian@Sun.COM static struct phyint *
phyint_lookup_hwaddr(struct phyint * pi,boolean_t online_only)1118485SPeter.Memishian@Sun.COM phyint_lookup_hwaddr(struct phyint *pi, boolean_t online_only)
1128485SPeter.Memishian@Sun.COM {
1138485SPeter.Memishian@Sun.COM 	struct phyint *pi2;
1148485SPeter.Memishian@Sun.COM 
1158485SPeter.Memishian@Sun.COM 	if (pi->pi_group == phyint_anongroup)
1168485SPeter.Memishian@Sun.COM 		return (NULL);
1178485SPeter.Memishian@Sun.COM 
1188485SPeter.Memishian@Sun.COM 	for (pi2 = pi->pi_group->pg_phyint; pi2 != NULL; pi2 = pi2->pi_pgnext) {
1198485SPeter.Memishian@Sun.COM 		if (pi2 == pi)
1208485SPeter.Memishian@Sun.COM 			continue;
1218485SPeter.Memishian@Sun.COM 
1228485SPeter.Memishian@Sun.COM 		/*
1238485SPeter.Memishian@Sun.COM 		 * NOTE: even when online_only is B_FALSE, we ignore phyints
1248485SPeter.Memishian@Sun.COM 		 * that are administratively offline (rather than offline
1258485SPeter.Memishian@Sun.COM 		 * because they're dups); when they're brought back online,
1268485SPeter.Memishian@Sun.COM 		 * they'll be flagged as dups if need be.
1278485SPeter.Memishian@Sun.COM 		 */
1288485SPeter.Memishian@Sun.COM 		if (pi2->pi_state == PI_OFFLINE &&
1298485SPeter.Memishian@Sun.COM 		    (online_only || !pi2->pi_hwaddrdup))
1308485SPeter.Memishian@Sun.COM 			continue;
1318485SPeter.Memishian@Sun.COM 
1328485SPeter.Memishian@Sun.COM 		if (pi2->pi_hwaddrlen == pi->pi_hwaddrlen &&
1338485SPeter.Memishian@Sun.COM 		    bcmp(pi2->pi_hwaddr, pi->pi_hwaddr, pi->pi_hwaddrlen) == 0)
1348485SPeter.Memishian@Sun.COM 			return (pi2);
1358485SPeter.Memishian@Sun.COM 	}
1368485SPeter.Memishian@Sun.COM 	return (NULL);
1378485SPeter.Memishian@Sun.COM }
1388485SPeter.Memishian@Sun.COM 
1398485SPeter.Memishian@Sun.COM /*
1408485SPeter.Memishian@Sun.COM  * Respond to DLPI notifications.  Currently, this only processes physical
1418485SPeter.Memishian@Sun.COM  * address changes for the phyint passed via `arg' by onlining or offlining
1428485SPeter.Memishian@Sun.COM  * phyints in the group.
1438485SPeter.Memishian@Sun.COM  */
1448485SPeter.Memishian@Sun.COM /* ARGSUSED */
1458485SPeter.Memishian@Sun.COM static void
phyint_link_notify(dlpi_handle_t dh,dlpi_notifyinfo_t * dnip,void * arg)1468485SPeter.Memishian@Sun.COM phyint_link_notify(dlpi_handle_t dh, dlpi_notifyinfo_t *dnip, void *arg)
1478485SPeter.Memishian@Sun.COM {
1488485SPeter.Memishian@Sun.COM 	struct phyint *pi = arg;
1498485SPeter.Memishian@Sun.COM 	struct phyint *oduppi = NULL, *duppi = NULL;
1508485SPeter.Memishian@Sun.COM 
1518485SPeter.Memishian@Sun.COM 	assert((dnip->dni_note & pi->pi_notes) != 0);
1528485SPeter.Memishian@Sun.COM 
1538485SPeter.Memishian@Sun.COM 	if (dnip->dni_note != DL_NOTE_PHYS_ADDR)
1548485SPeter.Memishian@Sun.COM 		return;
1558485SPeter.Memishian@Sun.COM 
1568485SPeter.Memishian@Sun.COM 	assert(dnip->dni_physaddrlen <= DLPI_PHYSADDR_MAX);
1578485SPeter.Memishian@Sun.COM 
1588485SPeter.Memishian@Sun.COM 	/*
1598485SPeter.Memishian@Sun.COM 	 * If our hardware address hasn't changed, there's nothing to do.
1608485SPeter.Memishian@Sun.COM 	 */
1618485SPeter.Memishian@Sun.COM 	if (pi->pi_hwaddrlen == dnip->dni_physaddrlen &&
1628485SPeter.Memishian@Sun.COM 	    bcmp(pi->pi_hwaddr, dnip->dni_physaddr, pi->pi_hwaddrlen) == 0)
1638485SPeter.Memishian@Sun.COM 		return;
1648485SPeter.Memishian@Sun.COM 
1658485SPeter.Memishian@Sun.COM 	oduppi = phyint_lookup_hwaddr(pi, _B_FALSE);
1668485SPeter.Memishian@Sun.COM 	pi->pi_hwaddrlen = dnip->dni_physaddrlen;
1678485SPeter.Memishian@Sun.COM 	(void) memcpy(pi->pi_hwaddr, dnip->dni_physaddr, pi->pi_hwaddrlen);
1688485SPeter.Memishian@Sun.COM 	duppi = phyint_lookup_hwaddr(pi, _B_FALSE);
1698485SPeter.Memishian@Sun.COM 
1708485SPeter.Memishian@Sun.COM 	if (oduppi != NULL || pi->pi_hwaddrdup) {
1718485SPeter.Memishian@Sun.COM 		/*
1728485SPeter.Memishian@Sun.COM 		 * Our old hardware address was a duplicate.  If we'd been
1738485SPeter.Memishian@Sun.COM 		 * offlined because of it, and our new hardware address is not
1748485SPeter.Memishian@Sun.COM 		 * a duplicate, then bring us online.  Otherwise, `oduppi'
1758485SPeter.Memishian@Sun.COM 		 * must've been the one brought offline; bring it online.
1768485SPeter.Memishian@Sun.COM 		 */
1778485SPeter.Memishian@Sun.COM 		if (pi->pi_hwaddrdup) {
1788485SPeter.Memishian@Sun.COM 			if (duppi == NULL)
1798485SPeter.Memishian@Sun.COM 				(void) phyint_undo_offline(pi);
1808485SPeter.Memishian@Sun.COM 		} else {
1818485SPeter.Memishian@Sun.COM 			assert(oduppi->pi_hwaddrdup);
1828485SPeter.Memishian@Sun.COM 			(void) phyint_undo_offline(oduppi);
1838485SPeter.Memishian@Sun.COM 		}
1848485SPeter.Memishian@Sun.COM 	}
1858485SPeter.Memishian@Sun.COM 
1868485SPeter.Memishian@Sun.COM 	if (duppi != NULL && !pi->pi_hwaddrdup) {
1878485SPeter.Memishian@Sun.COM 		/*
1888485SPeter.Memishian@Sun.COM 		 * Our new hardware address was a duplicate and we're not
1898485SPeter.Memishian@Sun.COM 		 * yet flagged as a duplicate; bring us offline.
1908485SPeter.Memishian@Sun.COM 		 */
1918485SPeter.Memishian@Sun.COM 		pi->pi_hwaddrdup = _B_TRUE;
1928485SPeter.Memishian@Sun.COM 		(void) phyint_offline(pi, 0);
1938485SPeter.Memishian@Sun.COM 	}
1948485SPeter.Memishian@Sun.COM }
1958485SPeter.Memishian@Sun.COM 
1968485SPeter.Memishian@Sun.COM /*
1978485SPeter.Memishian@Sun.COM  * Initialize information about the underlying link for `pi', and set us
1988485SPeter.Memishian@Sun.COM  * up to be notified about future changes.  Returns _B_TRUE on success.
1998485SPeter.Memishian@Sun.COM  */
2008485SPeter.Memishian@Sun.COM boolean_t
phyint_link_init(struct phyint * pi)2018485SPeter.Memishian@Sun.COM phyint_link_init(struct phyint *pi)
2028485SPeter.Memishian@Sun.COM {
2038485SPeter.Memishian@Sun.COM 	int retval;
2048485SPeter.Memishian@Sun.COM 	uint_t notes;
2058485SPeter.Memishian@Sun.COM 	const char *errmsg;
2068485SPeter.Memishian@Sun.COM 	dlpi_notifyid_t id;
2078485SPeter.Memishian@Sun.COM 
2088485SPeter.Memishian@Sun.COM 	pi->pi_notes = 0;
2098485SPeter.Memishian@Sun.COM 	retval = dlpi_open(pi->pi_name, &pi->pi_dh, 0);
2108485SPeter.Memishian@Sun.COM 	if (retval != DLPI_SUCCESS) {
2118485SPeter.Memishian@Sun.COM 		pi->pi_dh = NULL;
2128485SPeter.Memishian@Sun.COM 		errmsg = "cannot open";
2138485SPeter.Memishian@Sun.COM 		goto failed;
2148485SPeter.Memishian@Sun.COM 	}
2158485SPeter.Memishian@Sun.COM 
2168485SPeter.Memishian@Sun.COM 	pi->pi_hwaddrlen = DLPI_PHYSADDR_MAX;
2178485SPeter.Memishian@Sun.COM 	retval = dlpi_get_physaddr(pi->pi_dh, DL_CURR_PHYS_ADDR, pi->pi_hwaddr,
2188485SPeter.Memishian@Sun.COM 	    &pi->pi_hwaddrlen);
2198485SPeter.Memishian@Sun.COM 	if (retval != DLPI_SUCCESS) {
2208485SPeter.Memishian@Sun.COM 		errmsg = "cannot get hardware address";
2218485SPeter.Memishian@Sun.COM 		goto failed;
2228485SPeter.Memishian@Sun.COM 	}
2238485SPeter.Memishian@Sun.COM 
2248485SPeter.Memishian@Sun.COM 	/*
2258485SPeter.Memishian@Sun.COM 	 * Check if the link supports DLPI link state notifications.  For
2268485SPeter.Memishian@Sun.COM 	 * historical reasons, the actual changes are tracked through routing
2278485SPeter.Memishian@Sun.COM 	 * sockets, so we immediately disable the notification upon success.
2288485SPeter.Memishian@Sun.COM 	 */
2298485SPeter.Memishian@Sun.COM 	notes = DL_NOTE_LINK_UP | DL_NOTE_LINK_DOWN;
2308485SPeter.Memishian@Sun.COM 	retval = dlpi_enabnotify(pi->pi_dh, notes, phyint_link_notify, pi, &id);
2318485SPeter.Memishian@Sun.COM 	if (retval == DLPI_SUCCESS) {
2328485SPeter.Memishian@Sun.COM 		(void) dlpi_disabnotify(pi->pi_dh, id, NULL);
2338485SPeter.Memishian@Sun.COM 		pi->pi_notes |= notes;
2348485SPeter.Memishian@Sun.COM 	}
2358485SPeter.Memishian@Sun.COM 
2368485SPeter.Memishian@Sun.COM 	/*
2378485SPeter.Memishian@Sun.COM 	 * Enable notification of hardware address changes to keep pi_hwaddr
2388485SPeter.Memishian@Sun.COM 	 * up-to-date and track if we need to offline/undo-offline phyints.
2398485SPeter.Memishian@Sun.COM 	 */
2408485SPeter.Memishian@Sun.COM 	notes = DL_NOTE_PHYS_ADDR;
2418485SPeter.Memishian@Sun.COM 	retval = dlpi_enabnotify(pi->pi_dh, notes, phyint_link_notify, pi, &id);
2428485SPeter.Memishian@Sun.COM 	if (retval == DLPI_SUCCESS && poll_add(dlpi_fd(pi->pi_dh)) == 0)
2438485SPeter.Memishian@Sun.COM 		pi->pi_notes |= notes;
2448485SPeter.Memishian@Sun.COM 
2458485SPeter.Memishian@Sun.COM 	return (_B_TRUE);
2468485SPeter.Memishian@Sun.COM failed:
2478485SPeter.Memishian@Sun.COM 	logerr("%s: %s: %s\n", pi->pi_name, errmsg, dlpi_strerror(retval));
2488485SPeter.Memishian@Sun.COM 	if (pi->pi_dh != NULL) {
2498485SPeter.Memishian@Sun.COM 		dlpi_close(pi->pi_dh);
2508485SPeter.Memishian@Sun.COM 		pi->pi_dh = NULL;
2518485SPeter.Memishian@Sun.COM 	}
2528485SPeter.Memishian@Sun.COM 	return (_B_FALSE);
2538485SPeter.Memishian@Sun.COM }
2548485SPeter.Memishian@Sun.COM 
2558485SPeter.Memishian@Sun.COM /*
2568485SPeter.Memishian@Sun.COM  * Close use of link on `pi'.
2578485SPeter.Memishian@Sun.COM  */
2588485SPeter.Memishian@Sun.COM void
phyint_link_close(struct phyint * pi)2598485SPeter.Memishian@Sun.COM phyint_link_close(struct phyint *pi)
2608485SPeter.Memishian@Sun.COM {
2618485SPeter.Memishian@Sun.COM 	if (pi->pi_notes & DL_NOTE_PHYS_ADDR) {
2628485SPeter.Memishian@Sun.COM 		(void) poll_remove(dlpi_fd(pi->pi_dh));
2638485SPeter.Memishian@Sun.COM 		pi->pi_notes &= ~DL_NOTE_PHYS_ADDR;
2648485SPeter.Memishian@Sun.COM 	}
2658485SPeter.Memishian@Sun.COM 
2668485SPeter.Memishian@Sun.COM 	/*
2678485SPeter.Memishian@Sun.COM 	 * NOTE: we don't clear pi_notes here so that iflinkstate() can still
2688485SPeter.Memishian@Sun.COM 	 * properly report the link state even when offline (which is possible
2698485SPeter.Memishian@Sun.COM 	 * since we use IFF_RUNNING to track link state).
2708485SPeter.Memishian@Sun.COM 	 */
2718485SPeter.Memishian@Sun.COM 	dlpi_close(pi->pi_dh);
2728485SPeter.Memishian@Sun.COM 	pi->pi_dh = NULL;
2738485SPeter.Memishian@Sun.COM }
2748485SPeter.Memishian@Sun.COM 
2750Sstevel@tonic-gate /* Return the phyint instance with the given name and the given family */
2760Sstevel@tonic-gate struct phyint_instance *
phyint_inst_lookup(int af,char * name)2770Sstevel@tonic-gate phyint_inst_lookup(int af, char *name)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate 	struct phyint *pi;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	if (debug & D_PHYINT)
2820Sstevel@tonic-gate 		logdebug("phyint_inst_lookup(%s %s)\n", AF_STR(af), name);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	assert(af == AF_INET || af == AF_INET6);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	pi = phyint_lookup(name);
2870Sstevel@tonic-gate 	if (pi == NULL)
2880Sstevel@tonic-gate 		return (NULL);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	return (PHYINT_INSTANCE(pi, af));
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2938485SPeter.Memishian@Sun.COM struct phyint_group *
phyint_group_lookup(const char * pg_name)2940Sstevel@tonic-gate phyint_group_lookup(const char *pg_name)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	struct phyint_group *pg;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	if (debug & D_PHYINT)
2990Sstevel@tonic-gate 		logdebug("phyint_group_lookup(%s)\n", pg_name);
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	for (pg = phyint_groups; pg != NULL; pg = pg->pg_next) {
3020Sstevel@tonic-gate 		if (strncmp(pg->pg_name, pg_name, sizeof (pg->pg_name)) == 0)
3030Sstevel@tonic-gate 			break;
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	return (pg);
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate /*
3090Sstevel@tonic-gate  * Insert the phyint in the linked list of all phyints. If the phyint belongs
3100Sstevel@tonic-gate  * to some group, insert it in the phyint group list.
3110Sstevel@tonic-gate  */
3120Sstevel@tonic-gate static void
phyint_insert(struct phyint * pi,struct phyint_group * pg)3130Sstevel@tonic-gate phyint_insert(struct phyint *pi, struct phyint_group *pg)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate 	if (debug & D_PHYINT)
3160Sstevel@tonic-gate 		logdebug("phyint_insert(%s '%s')\n", pi->pi_name, pg->pg_name);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	/* Insert the phyint at the head of the 'all phyints' list */
3190Sstevel@tonic-gate 	pi->pi_next = phyints;
3200Sstevel@tonic-gate 	pi->pi_prev = NULL;
3210Sstevel@tonic-gate 	if (phyints != NULL)
3220Sstevel@tonic-gate 		phyints->pi_prev = pi;
3230Sstevel@tonic-gate 	phyints = pi;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	/*
3260Sstevel@tonic-gate 	 * Insert the phyint at the head of the 'phyint_group members' list
3270Sstevel@tonic-gate 	 * of the phyint group to which it belongs.
3280Sstevel@tonic-gate 	 */
3290Sstevel@tonic-gate 	pi->pi_pgnext = NULL;
3300Sstevel@tonic-gate 	pi->pi_pgprev = NULL;
3310Sstevel@tonic-gate 	pi->pi_group = pg;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	pi->pi_pgnext = pg->pg_phyint;
3340Sstevel@tonic-gate 	if (pi->pi_pgnext != NULL)
3350Sstevel@tonic-gate 		pi->pi_pgnext->pi_pgprev = pi;
3360Sstevel@tonic-gate 	pg->pg_phyint = pi;
3370Sstevel@tonic-gate 
3388485SPeter.Memishian@Sun.COM 	/* Refresh the group state now that this phyint has been added */
3398485SPeter.Memishian@Sun.COM 	phyint_group_refresh_state(pg);
3408485SPeter.Memishian@Sun.COM 
3410Sstevel@tonic-gate 	pg->pg_sig++;
3420Sstevel@tonic-gate 	(void) phyint_group_member_event(pg, pi, IPMP_IF_ADD);
3430Sstevel@tonic-gate }
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate /* Insert the phyint instance in the linked list of all phyint instances. */
3460Sstevel@tonic-gate static void
phyint_inst_insert(struct phyint_instance * pii)3470Sstevel@tonic-gate phyint_inst_insert(struct phyint_instance *pii)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	if (debug & D_PHYINT) {
3500Sstevel@tonic-gate 		logdebug("phyint_inst_insert(%s %s)\n",
3510Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name);
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	/*
3550Sstevel@tonic-gate 	 * Insert the phyint at the head of the 'all phyint instances' list.
3560Sstevel@tonic-gate 	 */
3570Sstevel@tonic-gate 	pii->pii_next = phyint_instances;
3580Sstevel@tonic-gate 	pii->pii_prev = NULL;
3590Sstevel@tonic-gate 	if (phyint_instances != NULL)
3600Sstevel@tonic-gate 		phyint_instances->pii_prev = pii;
3610Sstevel@tonic-gate 	phyint_instances = pii;
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate  * Create a new phyint with the given parameters. Also insert it into
3660Sstevel@tonic-gate  * the list of all phyints and the list of phyint group members by calling
3670Sstevel@tonic-gate  * phyint_insert().
3680Sstevel@tonic-gate  */
3690Sstevel@tonic-gate static struct phyint *
phyint_create(char * pi_name,struct phyint_group * pg,uint_t ifindex,uint64_t flags)3700Sstevel@tonic-gate phyint_create(char *pi_name, struct phyint_group *pg, uint_t ifindex,
3710Sstevel@tonic-gate     uint64_t flags)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate 	struct phyint *pi;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	pi = calloc(1, sizeof (struct phyint));
3760Sstevel@tonic-gate 	if (pi == NULL) {
3770Sstevel@tonic-gate 		logperror("phyint_create: calloc");
3780Sstevel@tonic-gate 		return (NULL);
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	/*
3828485SPeter.Memishian@Sun.COM 	 * Record the phyint values.
3830Sstevel@tonic-gate 	 */
3844770Smeem 	(void) strlcpy(pi->pi_name, pi_name, sizeof (pi->pi_name));
3854770Smeem 	pi->pi_taddrthresh = getcurrentsec() + TESTADDR_CONF_TIME;
3860Sstevel@tonic-gate 	pi->pi_ifindex = ifindex;
3878485SPeter.Memishian@Sun.COM 	pi->pi_icmpid = htons(((getpid() & 0xFF) << 8) | (ifindex & 0xFF));
3888485SPeter.Memishian@Sun.COM 
38910290SPeter.Memishian@Sun.COM 	pi->pi_state = PI_INIT;
3900Sstevel@tonic-gate 	pi->pi_flags = PHYINT_FLAGS(flags);
3918485SPeter.Memishian@Sun.COM 
3920Sstevel@tonic-gate 	/*
3938700SPeter.Memishian@Sun.COM 	 * Initialize the link state.  The link state is initialized to
3940Sstevel@tonic-gate 	 * up, so that if the link is down when IPMP starts monitoring
3950Sstevel@tonic-gate 	 * the interface, it will appear as though there has been a
3960Sstevel@tonic-gate 	 * transition from the link up to link down.  This avoids
3970Sstevel@tonic-gate 	 * having to treat this situation as a special case.
3980Sstevel@tonic-gate 	 */
3990Sstevel@tonic-gate 	INIT_LINK_STATE(pi);
4000Sstevel@tonic-gate 
4018485SPeter.Memishian@Sun.COM 	if (!phyint_link_init(pi)) {
4028485SPeter.Memishian@Sun.COM 		free(pi);
4038485SPeter.Memishian@Sun.COM 		return (NULL);
4048485SPeter.Memishian@Sun.COM 	}
4058485SPeter.Memishian@Sun.COM 
4060Sstevel@tonic-gate 	/*
4070Sstevel@tonic-gate 	 * Insert the phyint in the list of all phyints, and the
4080Sstevel@tonic-gate 	 * list of phyint group members
4090Sstevel@tonic-gate 	 */
4100Sstevel@tonic-gate 	phyint_insert(pi, pg);
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	return (pi);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate /*
4160Sstevel@tonic-gate  * Create a new phyint instance belonging to the phyint 'pi' and address
4170Sstevel@tonic-gate  * family 'af'. Also insert it into the list of all phyint instances by
4180Sstevel@tonic-gate  * calling phyint_inst_insert().
4190Sstevel@tonic-gate  */
4200Sstevel@tonic-gate static struct phyint_instance *
phyint_inst_create(struct phyint * pi,int af)4210Sstevel@tonic-gate phyint_inst_create(struct phyint *pi, int af)
4220Sstevel@tonic-gate {
4230Sstevel@tonic-gate 	struct phyint_instance *pii;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	pii = calloc(1, sizeof (struct phyint_instance));
4260Sstevel@tonic-gate 	if (pii == NULL) {
4270Sstevel@tonic-gate 		logperror("phyint_inst_create: calloc");
4280Sstevel@tonic-gate 		return (NULL);
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/*
4320Sstevel@tonic-gate 	 * Attach the phyint instance to the phyint.
4330Sstevel@tonic-gate 	 * Set the back pointers as well
4340Sstevel@tonic-gate 	 */
4350Sstevel@tonic-gate 	pii->pii_phyint = pi;
4360Sstevel@tonic-gate 	if (af == AF_INET)
4370Sstevel@tonic-gate 		pi->pi_v4 = pii;
4380Sstevel@tonic-gate 	else
4390Sstevel@tonic-gate 		pi->pi_v6 = pii;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	pii->pii_in_use = 1;
4420Sstevel@tonic-gate 	pii->pii_probe_sock = -1;
4430Sstevel@tonic-gate 	pii->pii_snxt = 1;
4440Sstevel@tonic-gate 	pii->pii_af = af;
4450Sstevel@tonic-gate 	pii->pii_fd_hrtime = gethrtime() +
4460Sstevel@tonic-gate 	    (FAILURE_DETECTION_QP * (hrtime_t)NANOSEC);
4470Sstevel@tonic-gate 	pii->pii_flags = pi->pi_flags;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	/* Insert the phyint instance in the list of all phyint instances. */
4500Sstevel@tonic-gate 	phyint_inst_insert(pii);
4510Sstevel@tonic-gate 	return (pii);
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate /*
4550Sstevel@tonic-gate  * Change the state of phyint `pi' to state `state'.
4560Sstevel@tonic-gate  */
4570Sstevel@tonic-gate void
phyint_chstate(struct phyint * pi,enum pi_state state)4580Sstevel@tonic-gate phyint_chstate(struct phyint *pi, enum pi_state state)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	/*
4610Sstevel@tonic-gate 	 * To simplify things, some callers always set a given state
4620Sstevel@tonic-gate 	 * regardless of the previous state of the phyint (e.g., setting
4630Sstevel@tonic-gate 	 * PI_RUNNING when it's already set).  We shouldn't bother
4640Sstevel@tonic-gate 	 * generating an event or consuming a signature for these, since
4650Sstevel@tonic-gate 	 * the actual state of the interface is unchanged.
4660Sstevel@tonic-gate 	 */
4670Sstevel@tonic-gate 	if (pi->pi_state == state)
4680Sstevel@tonic-gate 		return;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	pi->pi_state = state;
4718485SPeter.Memishian@Sun.COM 	phyint_changed(pi);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate /*
4758485SPeter.Memishian@Sun.COM  * Note that `pi' has changed state.
4760Sstevel@tonic-gate  */
4770Sstevel@tonic-gate void
phyint_changed(struct phyint * pi)4788485SPeter.Memishian@Sun.COM phyint_changed(struct phyint *pi)
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate 	pi->pi_group->pg_sig++;
4810Sstevel@tonic-gate 	(void) phyint_state_event(pi->pi_group, pi);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate /*
4850Sstevel@tonic-gate  * Insert the phyint group in the linked list of all phyint groups
4860Sstevel@tonic-gate  * at the head of the list
4870Sstevel@tonic-gate  */
4888485SPeter.Memishian@Sun.COM void
phyint_group_insert(struct phyint_group * pg)4890Sstevel@tonic-gate phyint_group_insert(struct phyint_group *pg)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate 	pg->pg_next = phyint_groups;
4920Sstevel@tonic-gate 	pg->pg_prev = NULL;
4930Sstevel@tonic-gate 	if (phyint_groups != NULL)
4940Sstevel@tonic-gate 		phyint_groups->pg_prev = pg;
4950Sstevel@tonic-gate 	phyint_groups = pg;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	phyint_grouplistsig++;
4980Sstevel@tonic-gate 	(void) phyint_group_change_event(pg, IPMP_GROUP_ADD);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate /*
5020Sstevel@tonic-gate  * Create a new phyint group called 'name'.
5030Sstevel@tonic-gate  */
5048485SPeter.Memishian@Sun.COM struct phyint_group *
phyint_group_create(const char * name)5050Sstevel@tonic-gate phyint_group_create(const char *name)
5060Sstevel@tonic-gate {
5070Sstevel@tonic-gate 	struct	phyint_group *pg;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	if (debug & D_PHYINT)
5100Sstevel@tonic-gate 		logdebug("phyint_group_create(%s)\n", name);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	pg = calloc(1, sizeof (struct phyint_group));
5130Sstevel@tonic-gate 	if (pg == NULL) {
5140Sstevel@tonic-gate 		logperror("phyint_group_create: calloc");
5150Sstevel@tonic-gate 		return (NULL);
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 
5184770Smeem 	(void) strlcpy(pg->pg_name, name, sizeof (pg->pg_name));
5190Sstevel@tonic-gate 	pg->pg_sig = gensig();
5200Sstevel@tonic-gate 	pg->pg_fdt = user_failure_detection_time;
5210Sstevel@tonic-gate 	pg->pg_probeint = user_probe_interval;
5228485SPeter.Memishian@Sun.COM 	pg->pg_in_use = _B_TRUE;
5238485SPeter.Memishian@Sun.COM 
5248485SPeter.Memishian@Sun.COM 	/*
5258485SPeter.Memishian@Sun.COM 	 * Normal groups always start in the PG_FAILED state since they
5268485SPeter.Memishian@Sun.COM 	 * have no active interfaces.  In contrast, anonymous groups are
5278485SPeter.Memishian@Sun.COM 	 * heterogeneous and thus always PG_OK.
5288485SPeter.Memishian@Sun.COM 	 */
5298485SPeter.Memishian@Sun.COM 	pg->pg_state = (name[0] == '\0' ? PG_OK : PG_FAILED);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	return (pg);
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate /*
5350Sstevel@tonic-gate  * Change the state of the phyint group `pg' to state `state'.
5360Sstevel@tonic-gate  */
5370Sstevel@tonic-gate void
phyint_group_chstate(struct phyint_group * pg,enum pg_state state)5380Sstevel@tonic-gate phyint_group_chstate(struct phyint_group *pg, enum pg_state state)
5390Sstevel@tonic-gate {
5400Sstevel@tonic-gate 	assert(pg != phyint_anongroup);
5410Sstevel@tonic-gate 
5428485SPeter.Memishian@Sun.COM 	/*
5438485SPeter.Memishian@Sun.COM 	 * To simplify things, some callers always set a given state
5448485SPeter.Memishian@Sun.COM 	 * regardless of the previous state of the group (e.g., setting
5458485SPeter.Memishian@Sun.COM 	 * PG_DEGRADED when it's already set).  We shouldn't bother
5468485SPeter.Memishian@Sun.COM 	 * generating an event or consuming a signature for these, since
5478485SPeter.Memishian@Sun.COM 	 * the actual state of the group is unchanged.
5488485SPeter.Memishian@Sun.COM 	 */
5498485SPeter.Memishian@Sun.COM 	if (pg->pg_state == state)
5508485SPeter.Memishian@Sun.COM 		return;
5518485SPeter.Memishian@Sun.COM 
5528485SPeter.Memishian@Sun.COM 	pg->pg_state = state;
5538485SPeter.Memishian@Sun.COM 
5540Sstevel@tonic-gate 	switch (state) {
5550Sstevel@tonic-gate 	case PG_FAILED:
5560Sstevel@tonic-gate 		/*
5570Sstevel@tonic-gate 		 * We can never know with certainty that a group has
5580Sstevel@tonic-gate 		 * failed.  It is possible that all known targets have
5590Sstevel@tonic-gate 		 * failed simultaneously, and new targets have come up
5600Sstevel@tonic-gate 		 * instead. If the targets are routers then router
5610Sstevel@tonic-gate 		 * discovery will kick in, and we will see the new routers
5620Sstevel@tonic-gate 		 * thru routing socket messages. But if the targets are
5630Sstevel@tonic-gate 		 * hosts, we have to discover it by multicast.	So flush
5640Sstevel@tonic-gate 		 * all the host targets. The next probe will send out a
5650Sstevel@tonic-gate 		 * multicast echo request. If this is a group failure, we
5668485SPeter.Memishian@Sun.COM 		 * will still not see any response, otherwise the group
5678485SPeter.Memishian@Sun.COM 		 * will be repaired after we get NUM_PROBE_REPAIRS
5688485SPeter.Memishian@Sun.COM 		 * consecutive unicast replies on any phyint.
5690Sstevel@tonic-gate 		 */
5700Sstevel@tonic-gate 		target_flush_hosts(pg);
5710Sstevel@tonic-gate 		break;
5720Sstevel@tonic-gate 
5738485SPeter.Memishian@Sun.COM 	case PG_OK:
5748485SPeter.Memishian@Sun.COM 	case PG_DEGRADED:
5750Sstevel@tonic-gate 		break;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	default:
5780Sstevel@tonic-gate 		logerr("phyint_group_chstate: invalid group state %d; "
5790Sstevel@tonic-gate 		    "aborting\n", state);
5800Sstevel@tonic-gate 		abort();
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	pg->pg_sig++;
5840Sstevel@tonic-gate 	(void) phyint_group_state_event(pg);
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate /*
5880Sstevel@tonic-gate  * Create a new phyint instance and initialize it from the values supplied by
5890Sstevel@tonic-gate  * the kernel. Always check for ENXIO before logging any error, because the
5900Sstevel@tonic-gate  * interface could have vanished after completion of SIOCGLIFCONF.
5910Sstevel@tonic-gate  * Return values:
5920Sstevel@tonic-gate  *	pointer to the phyint instance on success
5930Sstevel@tonic-gate  *	NULL on failure Eg. if the phyint instance is not found in the kernel
5940Sstevel@tonic-gate  */
5950Sstevel@tonic-gate struct phyint_instance *
phyint_inst_init_from_k(int af,char * pi_name)5960Sstevel@tonic-gate phyint_inst_init_from_k(int af, char *pi_name)
5970Sstevel@tonic-gate {
5980Sstevel@tonic-gate 	char	pg_name[LIFNAMSIZ + 1];
5990Sstevel@tonic-gate 	int	ifsock;
6000Sstevel@tonic-gate 	uint_t	ifindex;
6010Sstevel@tonic-gate 	uint64_t	flags;
6020Sstevel@tonic-gate 	struct lifreq	lifr;
6030Sstevel@tonic-gate 	struct phyint	*pi;
6040Sstevel@tonic-gate 	struct phyint_instance	*pii;
6050Sstevel@tonic-gate 	boolean_t	pi_created;
6060Sstevel@tonic-gate 	struct phyint_group	*pg;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate retry:
6090Sstevel@tonic-gate 	pii = NULL;
6100Sstevel@tonic-gate 	pi = NULL;
6110Sstevel@tonic-gate 	pg = NULL;
6120Sstevel@tonic-gate 	pi_created = _B_FALSE;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	if (debug & D_PHYINT) {
6150Sstevel@tonic-gate 		logdebug("phyint_inst_init_from_k(%s %s)\n",
6160Sstevel@tonic-gate 		    AF_STR(af), pi_name);
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	assert(af == AF_INET || af == AF_INET6);
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	/* Get the socket for doing ioctls */
6220Sstevel@tonic-gate 	ifsock = (af == AF_INET) ? ifsock_v4 : ifsock_v6;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	/*
6258485SPeter.Memishian@Sun.COM 	 * Get the interface flags.  Ignore virtual interfaces, IPMP
6268485SPeter.Memishian@Sun.COM 	 * meta-interfaces, point-to-point interfaces, and interfaces
6278485SPeter.Memishian@Sun.COM 	 * that can't support multicast.
6280Sstevel@tonic-gate 	 */
6298485SPeter.Memishian@Sun.COM 	(void) strlcpy(lifr.lifr_name, pi_name, sizeof (lifr.lifr_name));
6300Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
6310Sstevel@tonic-gate 		if (errno != ENXIO) {
6320Sstevel@tonic-gate 			logperror("phyint_inst_init_from_k:"
6330Sstevel@tonic-gate 			    " ioctl (get flags)");
6340Sstevel@tonic-gate 		}
6350Sstevel@tonic-gate 		return (NULL);
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate 	flags = lifr.lifr_flags;
6388485SPeter.Memishian@Sun.COM 	if (!(flags & IFF_MULTICAST) ||
6398485SPeter.Memishian@Sun.COM 	    (flags & (IFF_VIRTUAL|IFF_IPMP|IFF_POINTOPOINT)))
6400Sstevel@tonic-gate 		return (NULL);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	/*
6430Sstevel@tonic-gate 	 * Get the ifindex for recording later in our tables, in case we need
6440Sstevel@tonic-gate 	 * to create a new phyint.
6450Sstevel@tonic-gate 	 */
6460Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFINDEX, (char *)&lifr) < 0) {
6470Sstevel@tonic-gate 		if (errno != ENXIO) {
6480Sstevel@tonic-gate 			logperror("phyint_inst_init_from_k: "
6490Sstevel@tonic-gate 			    " ioctl (get lifindex)");
6500Sstevel@tonic-gate 		}
6510Sstevel@tonic-gate 		return (NULL);
6520Sstevel@tonic-gate 	}
6530Sstevel@tonic-gate 	ifindex = lifr.lifr_index;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	/*
6560Sstevel@tonic-gate 	 * Get the phyint group name of this phyint, from the kernel.
6570Sstevel@tonic-gate 	 */
6580Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFGROUPNAME, (char *)&lifr) < 0) {
6590Sstevel@tonic-gate 		if (errno != ENXIO) {
6600Sstevel@tonic-gate 			logperror("phyint_inst_init_from_k: "
6610Sstevel@tonic-gate 			    "ioctl (get group name)");
6620Sstevel@tonic-gate 		}
6630Sstevel@tonic-gate 		return (NULL);
6640Sstevel@tonic-gate 	}
6658485SPeter.Memishian@Sun.COM 	(void) strlcpy(pg_name, lifr.lifr_groupname, sizeof (pg_name));
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	/*
6680Sstevel@tonic-gate 	 * If the phyint is not part of any group, pg_name is the
6690Sstevel@tonic-gate 	 * null string. If 'track_all_phyints' is false, there is no
6700Sstevel@tonic-gate 	 * need to create a phyint.
6710Sstevel@tonic-gate 	 */
6720Sstevel@tonic-gate 	if (pg_name[0] == '\0' && !track_all_phyints) {
6730Sstevel@tonic-gate 		/*
6748485SPeter.Memishian@Sun.COM 		 * If the IFF_FAILED, IFF_INACTIVE, or IFF_OFFLINE flags are
6758485SPeter.Memishian@Sun.COM 		 * set, reset them. These flags shouldn't be set if in.mpathd
6768485SPeter.Memishian@Sun.COM 		 * isn't tracking the interface.
6770Sstevel@tonic-gate 		 */
6788485SPeter.Memishian@Sun.COM 		if ((flags & (IFF_FAILED | IFF_INACTIVE | IFF_OFFLINE))) {
6798485SPeter.Memishian@Sun.COM 			lifr.lifr_flags = flags &
6808485SPeter.Memishian@Sun.COM 			    ~(IFF_FAILED | IFF_INACTIVE | IFF_OFFLINE);
6810Sstevel@tonic-gate 			if (ioctl(ifsock, SIOCSLIFFLAGS, (char *)&lifr) < 0) {
6820Sstevel@tonic-gate 				if (errno != ENXIO) {
6830Sstevel@tonic-gate 					logperror("phyint_inst_init_from_k:"
6840Sstevel@tonic-gate 					    " ioctl (set flags)");
6850Sstevel@tonic-gate 				}
6860Sstevel@tonic-gate 			}
6870Sstevel@tonic-gate 		}
6880Sstevel@tonic-gate 		return (NULL);
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	/*
6928485SPeter.Memishian@Sun.COM 	 * We need to create a new phyint instance.  We may also need to
6938485SPeter.Memishian@Sun.COM 	 * create the group if e.g. the SIOCGLIFCONF loop in initifs() found
6948485SPeter.Memishian@Sun.COM 	 * an underlying interface before it found its IPMP meta-interface.
6958485SPeter.Memishian@Sun.COM 	 * Note that we keep any created groups even if phyint_inst_from_k()
6968485SPeter.Memishian@Sun.COM 	 * fails since a group's existence is not dependent on the ability of
6978485SPeter.Memishian@Sun.COM 	 * in.mpathd to the track the group's interfaces.
6980Sstevel@tonic-gate 	 */
6998485SPeter.Memishian@Sun.COM 	if ((pg = phyint_group_lookup(pg_name)) == NULL) {
7008485SPeter.Memishian@Sun.COM 		if ((pg = phyint_group_create(pg_name)) == NULL) {
7018485SPeter.Memishian@Sun.COM 			logerr("phyint_inst_init_from_k: cannot create group "
7028485SPeter.Memishian@Sun.COM 			    "%s\n", pg_name);
7030Sstevel@tonic-gate 			return (NULL);
7040Sstevel@tonic-gate 		}
7050Sstevel@tonic-gate 		phyint_group_insert(pg);
7060Sstevel@tonic-gate 	}
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	/*
7090Sstevel@tonic-gate 	 * Lookup the phyint. If the phyint does not exist create it.
7100Sstevel@tonic-gate 	 */
7110Sstevel@tonic-gate 	pi = phyint_lookup(pi_name);
7120Sstevel@tonic-gate 	if (pi == NULL) {
7130Sstevel@tonic-gate 		pi = phyint_create(pi_name, pg, ifindex, flags);
7140Sstevel@tonic-gate 		if (pi == NULL) {
7150Sstevel@tonic-gate 			logerr("phyint_inst_init_from_k:"
7160Sstevel@tonic-gate 			    " unable to create phyint %s\n", pi_name);
7170Sstevel@tonic-gate 			return (NULL);
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate 		pi_created = _B_TRUE;
7200Sstevel@tonic-gate 	} else {
7210Sstevel@tonic-gate 		/* The phyint exists already. */
7220Sstevel@tonic-gate 		assert(pi_created == _B_FALSE);
7230Sstevel@tonic-gate 		/*
7240Sstevel@tonic-gate 		 * Normally we should see consistent values for the IPv4 and
7250Sstevel@tonic-gate 		 * IPv6 instances, for phyint properties. If we don't, it
7260Sstevel@tonic-gate 		 * means things have changed underneath us, and we should
7270Sstevel@tonic-gate 		 * resync our tables with the kernel. Check whether the
7280Sstevel@tonic-gate 		 * interface index has changed. If so, it is most likely
7290Sstevel@tonic-gate 		 * the interface has been unplumbed and replumbed,
7300Sstevel@tonic-gate 		 * while we are yet to update our tables. Do it now.
7310Sstevel@tonic-gate 		 */
7320Sstevel@tonic-gate 		if (pi->pi_ifindex != ifindex) {
7330Sstevel@tonic-gate 			phyint_inst_delete(PHYINT_INSTANCE(pi, AF_OTHER(af)));
7340Sstevel@tonic-gate 			goto retry;
7350Sstevel@tonic-gate 		}
7360Sstevel@tonic-gate 		assert(PHYINT_INSTANCE(pi, af) == NULL);
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 		/*
7390Sstevel@tonic-gate 		 * If the group name seen by the IPv4 and IPv6 instances
7400Sstevel@tonic-gate 		 * are different, it is most likely the groupname has
7410Sstevel@tonic-gate 		 * changed, while we are yet to update our tables. Do it now.
7420Sstevel@tonic-gate 		 */
7430Sstevel@tonic-gate 		if (strcmp(pi->pi_group->pg_name, pg_name) != 0) {
7440Sstevel@tonic-gate 			phyint_inst_delete(PHYINT_INSTANCE(pi,
7450Sstevel@tonic-gate 			    AF_OTHER(af)));
7460Sstevel@tonic-gate 			goto retry;
7470Sstevel@tonic-gate 		}
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	/*
7510Sstevel@tonic-gate 	 * Create a new phyint instance, corresponding to the 'af'
7520Sstevel@tonic-gate 	 * passed in.
7530Sstevel@tonic-gate 	 */
7540Sstevel@tonic-gate 	pii = phyint_inst_create(pi, af);
7550Sstevel@tonic-gate 	if (pii == NULL) {
7560Sstevel@tonic-gate 		logerr("phyint_inst_init_from_k: unable to create"
7570Sstevel@tonic-gate 		    "phyint inst %s\n", pi->pi_name);
7588485SPeter.Memishian@Sun.COM 		if (pi_created)
7590Sstevel@tonic-gate 			phyint_delete(pi);
7608485SPeter.Memishian@Sun.COM 
7618485SPeter.Memishian@Sun.COM 		return (NULL);
7628485SPeter.Memishian@Sun.COM 	}
7638485SPeter.Memishian@Sun.COM 
764*10649SPeter.Memishian@Sun.COM 	/*
765*10649SPeter.Memishian@Sun.COM 	 * NOTE: the change_pif_flags() implementation requires a phyint
766*10649SPeter.Memishian@Sun.COM 	 * instance before it can function, so a number of tasks that would
767*10649SPeter.Memishian@Sun.COM 	 * otherwise be done in phyint_create() are deferred to here.
768*10649SPeter.Memishian@Sun.COM 	 */
7698485SPeter.Memishian@Sun.COM 	if (pi_created) {
7708485SPeter.Memishian@Sun.COM 		/*
771*10649SPeter.Memishian@Sun.COM 		 * If the interface is offline, set the state to PI_OFFLINE.
772*10649SPeter.Memishian@Sun.COM 		 * Otherwise, optimistically consider this interface running.
773*10649SPeter.Memishian@Sun.COM 		 * Later (in process_link_state_changes()), we will adjust
774*10649SPeter.Memishian@Sun.COM 		 * this to match the current state of the link.  Further, if
775*10649SPeter.Memishian@Sun.COM 		 * test addresses are subsequently assigned, we will
776*10649SPeter.Memishian@Sun.COM 		 * transition to PI_NOTARGETS and then to either PI_RUNNING or
777*10649SPeter.Memishian@Sun.COM 		 * PI_FAILED depending on the probe results.
778*10649SPeter.Memishian@Sun.COM 		 */
779*10649SPeter.Memishian@Sun.COM 		if (pi->pi_flags & IFF_OFFLINE) {
780*10649SPeter.Memishian@Sun.COM 			phyint_chstate(pi, PI_OFFLINE);
781*10649SPeter.Memishian@Sun.COM 		} else {
782*10649SPeter.Memishian@Sun.COM 			/* calls phyint_chstate() */
783*10649SPeter.Memishian@Sun.COM 			phyint_transition_to_running(pi);
784*10649SPeter.Memishian@Sun.COM 		}
785*10649SPeter.Memishian@Sun.COM 
786*10649SPeter.Memishian@Sun.COM 		/*
787*10649SPeter.Memishian@Sun.COM 		 * If this a standby phyint, determine whether it should be
788*10649SPeter.Memishian@Sun.COM 		 * IFF_INACTIVE.
789*10649SPeter.Memishian@Sun.COM 		 */
790*10649SPeter.Memishian@Sun.COM 		if (pi->pi_flags & IFF_STANDBY)
791*10649SPeter.Memishian@Sun.COM 			phyint_standby_refresh_inactive(pi);
792*10649SPeter.Memishian@Sun.COM 
793*10649SPeter.Memishian@Sun.COM 		/*
7948485SPeter.Memishian@Sun.COM 		 * If this phyint does not have a unique hardware address in its
795*10649SPeter.Memishian@Sun.COM 		 * group, offline it.
7968485SPeter.Memishian@Sun.COM 		 */
7978485SPeter.Memishian@Sun.COM 		if (phyint_lookup_hwaddr(pi, _B_TRUE) != NULL) {
7988485SPeter.Memishian@Sun.COM 			pi->pi_hwaddrdup = _B_TRUE;
7998485SPeter.Memishian@Sun.COM 			(void) phyint_offline(pi, 0);
8000Sstevel@tonic-gate 		}
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	return (pii);
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate /*
8072074Smeem  * Bind pii_probe_sock to the address associated with pii_probe_logint.
8082074Smeem  * This socket will be used for sending and receiving ICMP/ICMPv6 probes to
8092074Smeem  * targets. Do the common part in this function, and complete the
8102074Smeem  * initializations by calling the protocol specific functions
8110Sstevel@tonic-gate  * phyint_inst_v{4,6}_sockinit() respectively.
8120Sstevel@tonic-gate  *
8130Sstevel@tonic-gate  * Return values: _B_TRUE/_B_FALSE for success or failure respectively.
8140Sstevel@tonic-gate  */
8150Sstevel@tonic-gate boolean_t
phyint_inst_sockinit(struct phyint_instance * pii)8160Sstevel@tonic-gate phyint_inst_sockinit(struct phyint_instance *pii)
8170Sstevel@tonic-gate {
8180Sstevel@tonic-gate 	boolean_t success;
8190Sstevel@tonic-gate 	struct phyint_group *pg;
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	if (debug & D_PHYINT) {
8220Sstevel@tonic-gate 		logdebug("phyint_inst_sockinit(%s %s)\n",
8230Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name);
8240Sstevel@tonic-gate 	}
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	assert(pii->pii_probe_logint != NULL);
8270Sstevel@tonic-gate 	assert(pii->pii_probe_logint->li_flags & IFF_UP);
8282074Smeem 	assert(pii->pii_probe_logint->li_flags & IFF_NOFAILOVER);
8290Sstevel@tonic-gate 	assert(pii->pii_af == AF_INET || pii->pii_af == AF_INET6);
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	/*
8320Sstevel@tonic-gate 	 * If the socket is already bound, close pii_probe_sock
8330Sstevel@tonic-gate 	 */
8340Sstevel@tonic-gate 	if (pii->pii_probe_sock != -1)
8350Sstevel@tonic-gate 		close_probe_socket(pii, _B_TRUE);
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	/*
8380Sstevel@tonic-gate 	 * If the phyint is not part of a named group and track_all_phyints is
8390Sstevel@tonic-gate 	 * false, simply return.
8400Sstevel@tonic-gate 	 */
8410Sstevel@tonic-gate 	pg = pii->pii_phyint->pi_group;
8420Sstevel@tonic-gate 	if (pg == phyint_anongroup && !track_all_phyints) {
8430Sstevel@tonic-gate 		if (debug & D_PHYINT)
8440Sstevel@tonic-gate 			logdebug("phyint_inst_sockinit: no group\n");
8450Sstevel@tonic-gate 		return (_B_FALSE);
8460Sstevel@tonic-gate 	}
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	/*
8490Sstevel@tonic-gate 	 * Initialize the socket by calling the protocol specific function.
8500Sstevel@tonic-gate 	 * If it succeeds, add the socket to the poll list.
8510Sstevel@tonic-gate 	 */
8520Sstevel@tonic-gate 	if (pii->pii_af == AF_INET6)
8530Sstevel@tonic-gate 		success = phyint_inst_v6_sockinit(pii);
8540Sstevel@tonic-gate 	else
8550Sstevel@tonic-gate 		success = phyint_inst_v4_sockinit(pii);
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	if (success && (poll_add(pii->pii_probe_sock) == 0))
8580Sstevel@tonic-gate 		return (_B_TRUE);
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	/* Something failed, cleanup and return false */
8610Sstevel@tonic-gate 	if (pii->pii_probe_sock != -1)
8620Sstevel@tonic-gate 		close_probe_socket(pii, _B_FALSE);
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	return (_B_FALSE);
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate /*
8680Sstevel@tonic-gate  * IPv6 specific part in initializing the pii_probe_sock. This socket is
8690Sstevel@tonic-gate  * used to send/receive ICMPv6 probe packets.
8700Sstevel@tonic-gate  */
8710Sstevel@tonic-gate static boolean_t
phyint_inst_v6_sockinit(struct phyint_instance * pii)8720Sstevel@tonic-gate phyint_inst_v6_sockinit(struct phyint_instance *pii)
8730Sstevel@tonic-gate {
8740Sstevel@tonic-gate 	icmp6_filter_t filter;
8750Sstevel@tonic-gate 	int hopcount = 1;
8768485SPeter.Memishian@Sun.COM 	int off = 0;
8778485SPeter.Memishian@Sun.COM 	int on = 1;
8780Sstevel@tonic-gate 	struct	sockaddr_in6	testaddr;
87910377SGeorge.Shepherd@Sun.COM 	int flags;
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	/*
8820Sstevel@tonic-gate 	 * Open a raw socket with ICMPv6 protocol.
8830Sstevel@tonic-gate 	 *
8848485SPeter.Memishian@Sun.COM 	 * Use IPV6_BOUND_IF to make sure that probes are sent and received on
8858485SPeter.Memishian@Sun.COM 	 * the specified phyint only.  Bind to the test address to ensure that
8868485SPeter.Memishian@Sun.COM 	 * the responses are sent to the specified phyint.
8870Sstevel@tonic-gate 	 *
8880Sstevel@tonic-gate 	 * Set the hopcount to 1 so that probe packets are not routed.
8890Sstevel@tonic-gate 	 * Disable multicast loopback. Set the receive filter to
8900Sstevel@tonic-gate 	 * receive only ICMPv6 echo replies.
8910Sstevel@tonic-gate 	 */
8920Sstevel@tonic-gate 	pii->pii_probe_sock = socket(pii->pii_af, SOCK_RAW, IPPROTO_ICMPV6);
8930Sstevel@tonic-gate 	if (pii->pii_probe_sock < 0) {
8940Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: socket");
8950Sstevel@tonic-gate 		return (_B_FALSE);
8968485SPeter.Memishian@Sun.COM 	}
8970Sstevel@tonic-gate 
89810377SGeorge.Shepherd@Sun.COM 	/*
89910377SGeorge.Shepherd@Sun.COM 	 * Probes must not block in case of lower layer issues.
90010377SGeorge.Shepherd@Sun.COM 	 */
90110377SGeorge.Shepherd@Sun.COM 	if ((flags = fcntl(pii->pii_probe_sock, F_GETFL, 0)) == -1) {
90210377SGeorge.Shepherd@Sun.COM 		logperror_pii(pii, "phyint_inst_v6_sockinit: fcntl"
90310377SGeorge.Shepherd@Sun.COM 		    " F_GETFL");
90410377SGeorge.Shepherd@Sun.COM 		return (_B_FALSE);
90510377SGeorge.Shepherd@Sun.COM 	}
90610377SGeorge.Shepherd@Sun.COM 	if (fcntl(pii->pii_probe_sock, F_SETFL,
90710377SGeorge.Shepherd@Sun.COM 	    flags | O_NONBLOCK) == -1) {
90810377SGeorge.Shepherd@Sun.COM 		logperror_pii(pii, "phyint_inst_v6_sockinit: fcntl"
90910377SGeorge.Shepherd@Sun.COM 		    " F_SETFL O_NONBLOCK");
91010377SGeorge.Shepherd@Sun.COM 		return (_B_FALSE);
91110377SGeorge.Shepherd@Sun.COM 	}
91210377SGeorge.Shepherd@Sun.COM 
9130Sstevel@tonic-gate 	bzero(&testaddr, sizeof (testaddr));
9140Sstevel@tonic-gate 	testaddr.sin6_family = AF_INET6;
9150Sstevel@tonic-gate 	testaddr.sin6_port = 0;
9160Sstevel@tonic-gate 	testaddr.sin6_addr = pii->pii_probe_logint->li_addr;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	if (bind(pii->pii_probe_sock, (struct sockaddr *)&testaddr,
9190Sstevel@tonic-gate 	    sizeof (testaddr)) < 0) {
9200Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: IPv6 bind");
9210Sstevel@tonic-gate 		return (_B_FALSE);
9220Sstevel@tonic-gate 	}
9230Sstevel@tonic-gate 
9248485SPeter.Memishian@Sun.COM 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IPV6, IPV6_MULTICAST_IF,
9250Sstevel@tonic-gate 	    (char *)&pii->pii_ifindex, sizeof (uint_t)) < 0) {
9260Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
9278485SPeter.Memishian@Sun.COM 		    " IPV6_MULTICAST_IF");
9288485SPeter.Memishian@Sun.COM 		return (_B_FALSE);
9298485SPeter.Memishian@Sun.COM 	}
9308485SPeter.Memishian@Sun.COM 
9318485SPeter.Memishian@Sun.COM 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IPV6, IPV6_BOUND_IF,
9328485SPeter.Memishian@Sun.COM 	    &pii->pii_ifindex, sizeof (uint_t)) < 0) {
9338485SPeter.Memishian@Sun.COM 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
9348485SPeter.Memishian@Sun.COM 		    " IPV6_BOUND_IF");
9350Sstevel@tonic-gate 		return (_B_FALSE);
9360Sstevel@tonic-gate 	}
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
9390Sstevel@tonic-gate 	    (char *)&hopcount, sizeof (hopcount)) < 0) {
9400Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
9410Sstevel@tonic-gate 		    " IPV6_UNICAST_HOPS");
9420Sstevel@tonic-gate 		return (_B_FALSE);
9430Sstevel@tonic-gate 	}
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
9460Sstevel@tonic-gate 	    (char *)&hopcount, sizeof (hopcount)) < 0) {
9470Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
9480Sstevel@tonic-gate 		    " IPV6_MULTICAST_HOPS");
9490Sstevel@tonic-gate 		return (_B_FALSE);
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
9538485SPeter.Memishian@Sun.COM 	    (char *)&off, sizeof (off)) < 0) {
9540Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
9550Sstevel@tonic-gate 		    " IPV6_MULTICAST_LOOP");
9560Sstevel@tonic-gate 		return (_B_FALSE);
9570Sstevel@tonic-gate 	}
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	/*
9600Sstevel@tonic-gate 	 * Filter out so that we only receive ICMP echo replies
9610Sstevel@tonic-gate 	 */
9620Sstevel@tonic-gate 	ICMP6_FILTER_SETBLOCKALL(&filter);
9630Sstevel@tonic-gate 	ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_ICMPV6, ICMP6_FILTER,
9660Sstevel@tonic-gate 	    (char *)&filter, sizeof (filter)) < 0) {
9670Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
9680Sstevel@tonic-gate 		    " ICMP6_FILTER");
9690Sstevel@tonic-gate 		return (_B_FALSE);
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 
9728485SPeter.Memishian@Sun.COM 	/* Enable receipt of hoplimit */
9730Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
9748485SPeter.Memishian@Sun.COM 	    &on, sizeof (on)) < 0) {
9750Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
9760Sstevel@tonic-gate 		    " IPV6_RECVHOPLIMIT");
9770Sstevel@tonic-gate 		return (_B_FALSE);
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 
9808485SPeter.Memishian@Sun.COM 	/* Enable receipt of timestamp */
9818485SPeter.Memishian@Sun.COM 	if (setsockopt(pii->pii_probe_sock, SOL_SOCKET, SO_TIMESTAMP,
9828485SPeter.Memishian@Sun.COM 	    &on, sizeof (on)) < 0) {
9838485SPeter.Memishian@Sun.COM 		logperror_pii(pii, "phyint_inst_v6_sockinit: setsockopt"
9848485SPeter.Memishian@Sun.COM 		    " SO_TIMESTAMP");
9858485SPeter.Memishian@Sun.COM 		return (_B_FALSE);
9868485SPeter.Memishian@Sun.COM 	}
9878485SPeter.Memishian@Sun.COM 
9880Sstevel@tonic-gate 	return (_B_TRUE);
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate /*
9920Sstevel@tonic-gate  * IPv4 specific part in initializing the pii_probe_sock. This socket is
9930Sstevel@tonic-gate  * used to send/receive ICMPv4 probe packets.
9940Sstevel@tonic-gate  */
9950Sstevel@tonic-gate static boolean_t
phyint_inst_v4_sockinit(struct phyint_instance * pii)9960Sstevel@tonic-gate phyint_inst_v4_sockinit(struct phyint_instance *pii)
9970Sstevel@tonic-gate {
9980Sstevel@tonic-gate 	struct sockaddr_in  testaddr;
9998485SPeter.Memishian@Sun.COM 	char	char_off = 0;
10000Sstevel@tonic-gate 	int	ttl = 1;
10010Sstevel@tonic-gate 	char	char_ttl = 1;
10028485SPeter.Memishian@Sun.COM 	int	on = 1;
100310377SGeorge.Shepherd@Sun.COM 	int	flags;
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	/*
10060Sstevel@tonic-gate 	 * Open a raw socket with ICMPv4 protocol.
10070Sstevel@tonic-gate 	 *
10088485SPeter.Memishian@Sun.COM 	 * Use IP_BOUND_IF to make sure that probes are sent and received on
10098485SPeter.Memishian@Sun.COM 	 * the specified phyint only.  Bind to the test address to ensure that
10108485SPeter.Memishian@Sun.COM 	 * the responses are sent to the specified phyint.
10110Sstevel@tonic-gate 	 *
10120Sstevel@tonic-gate 	 * Set the ttl to 1 so that probe packets are not routed.
10138485SPeter.Memishian@Sun.COM 	 * Disable multicast loopback.  Enable receipt of timestamp.
10140Sstevel@tonic-gate 	 */
10150Sstevel@tonic-gate 	pii->pii_probe_sock = socket(pii->pii_af, SOCK_RAW, IPPROTO_ICMP);
10160Sstevel@tonic-gate 	if (pii->pii_probe_sock < 0) {
10170Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v4_sockinit: socket");
10180Sstevel@tonic-gate 		return (_B_FALSE);
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 
102110377SGeorge.Shepherd@Sun.COM 	/*
102210377SGeorge.Shepherd@Sun.COM 	 * Probes must not block in case of lower layer issues.
102310377SGeorge.Shepherd@Sun.COM 	 */
102410377SGeorge.Shepherd@Sun.COM 	if ((flags = fcntl(pii->pii_probe_sock, F_GETFL, 0)) == -1) {
102510377SGeorge.Shepherd@Sun.COM 		logperror_pii(pii, "phyint_inst_v4_sockinit: fcntl"
102610377SGeorge.Shepherd@Sun.COM 		    " F_GETFL");
102710377SGeorge.Shepherd@Sun.COM 		return (_B_FALSE);
102810377SGeorge.Shepherd@Sun.COM 	}
102910377SGeorge.Shepherd@Sun.COM 	if (fcntl(pii->pii_probe_sock, F_SETFL,
103010377SGeorge.Shepherd@Sun.COM 	    flags | O_NONBLOCK) == -1) {
103110377SGeorge.Shepherd@Sun.COM 		logperror_pii(pii, "phyint_inst_v4_sockinit: fcntl"
103210377SGeorge.Shepherd@Sun.COM 		    " F_SETFL O_NONBLOCK");
103310377SGeorge.Shepherd@Sun.COM 		return (_B_FALSE);
103410377SGeorge.Shepherd@Sun.COM 	}
103510377SGeorge.Shepherd@Sun.COM 
10360Sstevel@tonic-gate 	bzero(&testaddr, sizeof (testaddr));
10370Sstevel@tonic-gate 	testaddr.sin_family = AF_INET;
10380Sstevel@tonic-gate 	testaddr.sin_port = 0;
10390Sstevel@tonic-gate 	IN6_V4MAPPED_TO_INADDR(&pii->pii_probe_logint->li_addr,
10400Sstevel@tonic-gate 	    &testaddr.sin_addr);
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	if (bind(pii->pii_probe_sock, (struct sockaddr *)&testaddr,
10430Sstevel@tonic-gate 	    sizeof (testaddr)) < 0) {
10440Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v4_sockinit: IPv4 bind");
10450Sstevel@tonic-gate 		return (_B_FALSE);
10460Sstevel@tonic-gate 	}
10470Sstevel@tonic-gate 
10488485SPeter.Memishian@Sun.COM 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IP, IP_BOUND_IF,
10498485SPeter.Memishian@Sun.COM 	    &pii->pii_ifindex, sizeof (uint_t)) < 0) {
10508485SPeter.Memishian@Sun.COM 		logperror_pii(pii, "phyint_inst_v4_sockinit: setsockopt"
10518485SPeter.Memishian@Sun.COM 		    " IP_BOUND_IF");
10528485SPeter.Memishian@Sun.COM 		return (_B_FALSE);
10538485SPeter.Memishian@Sun.COM 	}
10548485SPeter.Memishian@Sun.COM 
10558485SPeter.Memishian@Sun.COM 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IP, IP_MULTICAST_IF,
10560Sstevel@tonic-gate 	    (char *)&testaddr.sin_addr, sizeof (struct in_addr)) < 0) {
10570Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v4_sockinit: setsockopt"
10588485SPeter.Memishian@Sun.COM 		    " IP_MULTICAST_IF");
10590Sstevel@tonic-gate 		return (_B_FALSE);
10600Sstevel@tonic-gate 	}
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IP, IP_TTL,
10630Sstevel@tonic-gate 	    (char *)&ttl, sizeof (ttl)) < 0) {
10640Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v4_sockinit: setsockopt"
10650Sstevel@tonic-gate 		    " IP_TTL");
10660Sstevel@tonic-gate 		return (_B_FALSE);
10670Sstevel@tonic-gate 	}
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IP, IP_MULTICAST_LOOP,
10708485SPeter.Memishian@Sun.COM 	    (char *)&char_off, sizeof (char_off)) == -1) {
10710Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v4_sockinit: setsockopt"
10720Sstevel@tonic-gate 		    " IP_MULTICAST_LOOP");
10730Sstevel@tonic-gate 		return (_B_FALSE);
10740Sstevel@tonic-gate 	}
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	if (setsockopt(pii->pii_probe_sock, IPPROTO_IP, IP_MULTICAST_TTL,
10770Sstevel@tonic-gate 	    (char *)&char_ttl, sizeof (char_ttl)) == -1) {
10780Sstevel@tonic-gate 		logperror_pii(pii, "phyint_inst_v4_sockinit: setsockopt"
10790Sstevel@tonic-gate 		    " IP_MULTICAST_TTL");
10800Sstevel@tonic-gate 		return (_B_FALSE);
10810Sstevel@tonic-gate 	}
10820Sstevel@tonic-gate 
10838485SPeter.Memishian@Sun.COM 	if (setsockopt(pii->pii_probe_sock, SOL_SOCKET, SO_TIMESTAMP, &on,
10848485SPeter.Memishian@Sun.COM 	    sizeof (on)) < 0) {
10858485SPeter.Memishian@Sun.COM 		logperror_pii(pii, "phyint_inst_v4_sockinit: setsockopt"
10868485SPeter.Memishian@Sun.COM 		    " SO_TIMESTAMP");
10878485SPeter.Memishian@Sun.COM 		return (_B_FALSE);
10888485SPeter.Memishian@Sun.COM 	}
10898485SPeter.Memishian@Sun.COM 
10900Sstevel@tonic-gate 	return (_B_TRUE);
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate /*
10940Sstevel@tonic-gate  * Remove the phyint group from the list of 'all phyint groups'
10950Sstevel@tonic-gate  * and free it.
10960Sstevel@tonic-gate  */
10978485SPeter.Memishian@Sun.COM void
phyint_group_delete(struct phyint_group * pg)10980Sstevel@tonic-gate phyint_group_delete(struct phyint_group *pg)
10990Sstevel@tonic-gate {
11000Sstevel@tonic-gate 	/*
11010Sstevel@tonic-gate 	 * The anonymous group always exists, even when empty.
11020Sstevel@tonic-gate 	 */
11030Sstevel@tonic-gate 	if (pg == phyint_anongroup)
11040Sstevel@tonic-gate 		return;
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	if (debug & D_PHYINT)
11070Sstevel@tonic-gate 		logdebug("phyint_group_delete('%s')\n", pg->pg_name);
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	/*
11100Sstevel@tonic-gate 	 * The phyint group must be empty, and must not have any phyints.
11110Sstevel@tonic-gate 	 * The phyint group must be in the list of all phyint groups
11120Sstevel@tonic-gate 	 */
11130Sstevel@tonic-gate 	assert(pg->pg_phyint == NULL);
11140Sstevel@tonic-gate 	assert(phyint_groups == pg || pg->pg_prev != NULL);
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	if (pg->pg_prev != NULL)
11170Sstevel@tonic-gate 		pg->pg_prev->pg_next = pg->pg_next;
11180Sstevel@tonic-gate 	else
11190Sstevel@tonic-gate 		phyint_groups = pg->pg_next;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	if (pg->pg_next != NULL)
11220Sstevel@tonic-gate 		pg->pg_next->pg_prev = pg->pg_prev;
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	pg->pg_next = NULL;
11250Sstevel@tonic-gate 	pg->pg_prev = NULL;
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	phyint_grouplistsig++;
11280Sstevel@tonic-gate 	(void) phyint_group_change_event(pg, IPMP_GROUP_REMOVE);
11290Sstevel@tonic-gate 
11308485SPeter.Memishian@Sun.COM 	addrlist_free(&pg->pg_addrs);
11310Sstevel@tonic-gate 	free(pg);
11320Sstevel@tonic-gate }
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate /*
11358485SPeter.Memishian@Sun.COM  * Refresh the state of `pg' based on its current members.
11368485SPeter.Memishian@Sun.COM  */
11378485SPeter.Memishian@Sun.COM void
phyint_group_refresh_state(struct phyint_group * pg)11388485SPeter.Memishian@Sun.COM phyint_group_refresh_state(struct phyint_group *pg)
11398485SPeter.Memishian@Sun.COM {
11408485SPeter.Memishian@Sun.COM 	enum pg_state state;
11418485SPeter.Memishian@Sun.COM 	enum pg_state origstate = pg->pg_state;
11428485SPeter.Memishian@Sun.COM 	struct phyint *pi, *usablepi;
11438485SPeter.Memishian@Sun.COM 	uint_t nif = 0, nusable = 0;
11448485SPeter.Memishian@Sun.COM 
11458485SPeter.Memishian@Sun.COM 	/*
11468485SPeter.Memishian@Sun.COM 	 * Anonymous groups never change state.
11478485SPeter.Memishian@Sun.COM 	 */
11488485SPeter.Memishian@Sun.COM 	if (pg == phyint_anongroup)
11498485SPeter.Memishian@Sun.COM 		return;
11508485SPeter.Memishian@Sun.COM 
11518485SPeter.Memishian@Sun.COM 	for (pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext) {
11528485SPeter.Memishian@Sun.COM 		nif++;
11538485SPeter.Memishian@Sun.COM 		if (phyint_is_usable(pi)) {
11548485SPeter.Memishian@Sun.COM 			nusable++;
11558485SPeter.Memishian@Sun.COM 			usablepi = pi;
11568485SPeter.Memishian@Sun.COM 		}
11578485SPeter.Memishian@Sun.COM 	}
11588485SPeter.Memishian@Sun.COM 
11598485SPeter.Memishian@Sun.COM 	if (nusable == 0)
11608485SPeter.Memishian@Sun.COM 		state = PG_FAILED;
11618485SPeter.Memishian@Sun.COM 	else if (nif == nusable)
11628485SPeter.Memishian@Sun.COM 		state = PG_OK;
11638485SPeter.Memishian@Sun.COM 	else
11648485SPeter.Memishian@Sun.COM 		state = PG_DEGRADED;
11658485SPeter.Memishian@Sun.COM 
11668485SPeter.Memishian@Sun.COM 	phyint_group_chstate(pg, state);
11678485SPeter.Memishian@Sun.COM 
11688485SPeter.Memishian@Sun.COM 	/*
11698485SPeter.Memishian@Sun.COM 	 * If we're shutting down, skip logging messages since otherwise our
11708485SPeter.Memishian@Sun.COM 	 * shutdown housecleaning will make us report that groups are unusable.
11718485SPeter.Memishian@Sun.COM 	 */
11728485SPeter.Memishian@Sun.COM 	if (cleanup_started)
11738485SPeter.Memishian@Sun.COM 		return;
11748485SPeter.Memishian@Sun.COM 
11758485SPeter.Memishian@Sun.COM 	/*
11768485SPeter.Memishian@Sun.COM 	 * NOTE: We use pg_failmsg_printed rather than origstate since
11778485SPeter.Memishian@Sun.COM 	 * otherwise at startup we'll log a "now usable" message when the
11788485SPeter.Memishian@Sun.COM 	 * first usable phyint is added to an empty group.
11798485SPeter.Memishian@Sun.COM 	 */
11808485SPeter.Memishian@Sun.COM 	if (state != PG_FAILED && pg->pg_failmsg_printed) {
11818485SPeter.Memishian@Sun.COM 		assert(origstate == PG_FAILED);
11828485SPeter.Memishian@Sun.COM 		logerr("At least 1 IP interface (%s) in group %s is now "
11838485SPeter.Memishian@Sun.COM 		    "usable\n", usablepi->pi_name, pg->pg_name);
11848485SPeter.Memishian@Sun.COM 		pg->pg_failmsg_printed = _B_FALSE;
11858485SPeter.Memishian@Sun.COM 	} else if (origstate != PG_FAILED && state == PG_FAILED) {
11868485SPeter.Memishian@Sun.COM 		logerr("All IP interfaces in group %s are now unusable\n",
11878485SPeter.Memishian@Sun.COM 		    pg->pg_name);
11888485SPeter.Memishian@Sun.COM 		pg->pg_failmsg_printed = _B_TRUE;
11898485SPeter.Memishian@Sun.COM 	}
11908485SPeter.Memishian@Sun.COM }
11918485SPeter.Memishian@Sun.COM 
11928485SPeter.Memishian@Sun.COM /*
11930Sstevel@tonic-gate  * Extract information from the kernel about the desired phyint.
11940Sstevel@tonic-gate  * Look only for properties of the phyint and not properties of logints.
11950Sstevel@tonic-gate  * Take appropriate action on the changes.
11960Sstevel@tonic-gate  * Return codes:
11970Sstevel@tonic-gate  *	PI_OK
11980Sstevel@tonic-gate  *		The phyint exists in the kernel and matches our knowledge
11990Sstevel@tonic-gate  *		of the phyint.
12000Sstevel@tonic-gate  *	PI_DELETED
12010Sstevel@tonic-gate  *		The phyint has vanished in the kernel.
12020Sstevel@tonic-gate  *	PI_IFINDEX_CHANGED
12030Sstevel@tonic-gate  *		The phyint's interface index has changed.
12040Sstevel@tonic-gate  *		Ask the caller to delete and recreate the phyint.
12050Sstevel@tonic-gate  *	PI_IOCTL_ERROR
12060Sstevel@tonic-gate  *		Some ioctl error. Don't change anything.
12070Sstevel@tonic-gate  *	PI_GROUP_CHANGED
12080Sstevel@tonic-gate  *		The phyint has changed group.
12090Sstevel@tonic-gate  */
12100Sstevel@tonic-gate int
phyint_inst_update_from_k(struct phyint_instance * pii)12110Sstevel@tonic-gate phyint_inst_update_from_k(struct phyint_instance *pii)
12120Sstevel@tonic-gate {
12130Sstevel@tonic-gate 	struct lifreq lifr;
12140Sstevel@tonic-gate 	int	ifsock;
12150Sstevel@tonic-gate 	struct phyint *pi;
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	pi = pii->pii_phyint;
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	if (debug & D_PHYINT) {
12200Sstevel@tonic-gate 		logdebug("phyint_inst_update_from_k(%s %s)\n",
12210Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pi->pi_name);
12220Sstevel@tonic-gate 	}
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	/*
12250Sstevel@tonic-gate 	 * Get the ifindex from the kernel, for comparison with the
12260Sstevel@tonic-gate 	 * value in our tables.
12270Sstevel@tonic-gate 	 */
12280Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, pi->pi_name, sizeof (lifr.lifr_name));
12290Sstevel@tonic-gate 	lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	ifsock = (pii->pii_af == AF_INET) ? ifsock_v4 : ifsock_v6;
12320Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFINDEX, &lifr) < 0) {
12330Sstevel@tonic-gate 		if (errno == ENXIO) {
12340Sstevel@tonic-gate 			return (PI_DELETED);
12350Sstevel@tonic-gate 		} else {
12360Sstevel@tonic-gate 			logperror_pii(pii, "phyint_inst_update_from_k:"
12370Sstevel@tonic-gate 			    " ioctl (get lifindex)");
12380Sstevel@tonic-gate 			return (PI_IOCTL_ERROR);
12390Sstevel@tonic-gate 		}
12400Sstevel@tonic-gate 	}
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	if (lifr.lifr_index != pi->pi_ifindex) {
12430Sstevel@tonic-gate 		/*
12440Sstevel@tonic-gate 		 * The index has changed. Most likely the interface has
12450Sstevel@tonic-gate 		 * been unplumbed and replumbed. Ask the caller to take
12460Sstevel@tonic-gate 		 * appropriate action.
12470Sstevel@tonic-gate 		 */
12480Sstevel@tonic-gate 		if (debug & D_PHYINT) {
12490Sstevel@tonic-gate 			logdebug("phyint_inst_update_from_k:"
12500Sstevel@tonic-gate 			    " old index %d new index %d\n",
12510Sstevel@tonic-gate 			    pi->pi_ifindex, lifr.lifr_index);
12520Sstevel@tonic-gate 		}
12530Sstevel@tonic-gate 		return (PI_IFINDEX_CHANGED);
12540Sstevel@tonic-gate 	}
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	/*
12570Sstevel@tonic-gate 	 * Get the group name from the kernel, for comparison with
12580Sstevel@tonic-gate 	 * the value in our tables.
12590Sstevel@tonic-gate 	 */
12600Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFGROUPNAME, &lifr) < 0) {
12610Sstevel@tonic-gate 		if (errno == ENXIO) {
12620Sstevel@tonic-gate 			return (PI_DELETED);
12630Sstevel@tonic-gate 		} else {
12640Sstevel@tonic-gate 			logperror_pii(pii, "phyint_inst_update_from_k:"
12650Sstevel@tonic-gate 			    " ioctl (get groupname)");
12660Sstevel@tonic-gate 			return (PI_IOCTL_ERROR);
12670Sstevel@tonic-gate 		}
12680Sstevel@tonic-gate 	}
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	/*
12710Sstevel@tonic-gate 	 * If the phyint has changed group i.e. if the phyint group name
12720Sstevel@tonic-gate 	 * returned by the kernel is different, ask the caller to delete
12730Sstevel@tonic-gate 	 * and recreate the phyint in the right group
12740Sstevel@tonic-gate 	 */
12750Sstevel@tonic-gate 	if (strcmp(lifr.lifr_groupname, pi->pi_group->pg_name) != 0) {
12760Sstevel@tonic-gate 		/* Groupname has changed */
12770Sstevel@tonic-gate 		if (debug & D_PHYINT) {
12780Sstevel@tonic-gate 			logdebug("phyint_inst_update_from_k:"
12790Sstevel@tonic-gate 			    " groupname change\n");
12800Sstevel@tonic-gate 		}
12810Sstevel@tonic-gate 		return (PI_GROUP_CHANGED);
12820Sstevel@tonic-gate 	}
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	/*
12850Sstevel@tonic-gate 	 * Get the current phyint flags from the kernel, and determine what
12860Sstevel@tonic-gate 	 * flags have changed by comparing against our tables.	Note that the
12870Sstevel@tonic-gate 	 * IFF_INACTIVE processing in initifs() relies on this call to ensure
12880Sstevel@tonic-gate 	 * that IFF_INACTIVE is really still set on the interface.
12890Sstevel@tonic-gate 	 */
12900Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFFLAGS, &lifr) < 0) {
12910Sstevel@tonic-gate 		if (errno == ENXIO) {
12920Sstevel@tonic-gate 			return (PI_DELETED);
12930Sstevel@tonic-gate 		} else {
12940Sstevel@tonic-gate 			logperror_pii(pii, "phyint_inst_update_from_k: "
12950Sstevel@tonic-gate 			    " ioctl (get flags)");
12960Sstevel@tonic-gate 			return (PI_IOCTL_ERROR);
12970Sstevel@tonic-gate 		}
12980Sstevel@tonic-gate 	}
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	pi->pi_flags = PHYINT_FLAGS(lifr.lifr_flags);
13010Sstevel@tonic-gate 	if (pi->pi_v4 != NULL)
13020Sstevel@tonic-gate 		pi->pi_v4->pii_flags = pi->pi_flags;
13030Sstevel@tonic-gate 	if (pi->pi_v6 != NULL)
13040Sstevel@tonic-gate 		pi->pi_v6->pii_flags = pi->pi_flags;
13050Sstevel@tonic-gate 
13068485SPeter.Memishian@Sun.COM 	/*
13078485SPeter.Memishian@Sun.COM 	 * Make sure the IFF_FAILED flag is set if and only if we think
13088485SPeter.Memishian@Sun.COM 	 * the interface should be failed.
13098485SPeter.Memishian@Sun.COM 	 */
13100Sstevel@tonic-gate 	if (pi->pi_flags & IFF_FAILED) {
13118485SPeter.Memishian@Sun.COM 		if (pi->pi_state == PI_RUNNING)
13128485SPeter.Memishian@Sun.COM 			(void) change_pif_flags(pi, 0, IFF_FAILED);
13130Sstevel@tonic-gate 	} else {
13148485SPeter.Memishian@Sun.COM 		if (pi->pi_state == PI_FAILED)
13158485SPeter.Memishian@Sun.COM 			(void) change_pif_flags(pi, IFF_FAILED, IFF_INACTIVE);
13160Sstevel@tonic-gate 	}
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	/* No change in phyint status */
13190Sstevel@tonic-gate 	return (PI_OK);
13200Sstevel@tonic-gate }
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate /*
13230Sstevel@tonic-gate  * Delete the phyint. Remove it from the list of all phyints, and the
13248485SPeter.Memishian@Sun.COM  * list of phyint group members.
13250Sstevel@tonic-gate  */
13260Sstevel@tonic-gate static void
phyint_delete(struct phyint * pi)13270Sstevel@tonic-gate phyint_delete(struct phyint *pi)
13280Sstevel@tonic-gate {
1329*10649SPeter.Memishian@Sun.COM 	boolean_t active;
13308485SPeter.Memishian@Sun.COM 	struct phyint *pi2;
13310Sstevel@tonic-gate 	struct phyint_group *pg = pi->pi_group;
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	if (debug & D_PHYINT)
13340Sstevel@tonic-gate 		logdebug("phyint_delete(%s)\n", pi->pi_name);
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	/* Both IPv4 and IPv6 phyint instances must have been deleted. */
13370Sstevel@tonic-gate 	assert(pi->pi_v4 == NULL && pi->pi_v6 == NULL);
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	/*
13400Sstevel@tonic-gate 	 * The phyint must belong to a group.
13410Sstevel@tonic-gate 	 */
13420Sstevel@tonic-gate 	assert(pg->pg_phyint == pi || pi->pi_pgprev != NULL);
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	/* The phyint must be in the list of all phyints */
13450Sstevel@tonic-gate 	assert(phyints == pi || pi->pi_prev != NULL);
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	/* Remove the phyint from the phyint group list */
13480Sstevel@tonic-gate 	pg->pg_sig++;
13490Sstevel@tonic-gate 	(void) phyint_group_member_event(pg, pi, IPMP_IF_REMOVE);
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	if (pi->pi_pgprev == NULL) {
13520Sstevel@tonic-gate 		/* Phyint is the 1st in the phyint group list */
13530Sstevel@tonic-gate 		pg->pg_phyint = pi->pi_pgnext;
13540Sstevel@tonic-gate 	} else {
13550Sstevel@tonic-gate 		pi->pi_pgprev->pi_pgnext = pi->pi_pgnext;
13560Sstevel@tonic-gate 	}
13570Sstevel@tonic-gate 	if (pi->pi_pgnext != NULL)
13580Sstevel@tonic-gate 		pi->pi_pgnext->pi_pgprev = pi->pi_pgprev;
13590Sstevel@tonic-gate 	pi->pi_pgnext = NULL;
13600Sstevel@tonic-gate 	pi->pi_pgprev = NULL;
13610Sstevel@tonic-gate 
13628485SPeter.Memishian@Sun.COM 	/* Refresh the group state now that this phyint has been removed */
13638485SPeter.Memishian@Sun.COM 	phyint_group_refresh_state(pg);
13648485SPeter.Memishian@Sun.COM 
13650Sstevel@tonic-gate 	/* Remove the phyint from the global list of phyints */
13660Sstevel@tonic-gate 	if (pi->pi_prev == NULL) {
13670Sstevel@tonic-gate 		/* Phyint is the 1st in the list */
13680Sstevel@tonic-gate 		phyints = pi->pi_next;
13690Sstevel@tonic-gate 	} else {
13700Sstevel@tonic-gate 		pi->pi_prev->pi_next = pi->pi_next;
13710Sstevel@tonic-gate 	}
13720Sstevel@tonic-gate 	if (pi->pi_next != NULL)
13730Sstevel@tonic-gate 		pi->pi_next->pi_prev = pi->pi_prev;
13740Sstevel@tonic-gate 	pi->pi_next = NULL;
13750Sstevel@tonic-gate 	pi->pi_prev = NULL;
13760Sstevel@tonic-gate 
13778485SPeter.Memishian@Sun.COM 	/*
13788485SPeter.Memishian@Sun.COM 	 * See if another phyint in the group had been offlined because
13798485SPeter.Memishian@Sun.COM 	 * it was a dup of `pi' -- and if so, online it.
13808485SPeter.Memishian@Sun.COM 	 */
13818485SPeter.Memishian@Sun.COM 	if (!pi->pi_hwaddrdup &&
13828485SPeter.Memishian@Sun.COM 	    (pi2 = phyint_lookup_hwaddr(pi, _B_FALSE)) != NULL) {
13838485SPeter.Memishian@Sun.COM 		assert(pi2->pi_hwaddrdup);
13848485SPeter.Memishian@Sun.COM 		(void) phyint_undo_offline(pi2);
13858485SPeter.Memishian@Sun.COM 	}
1386*10649SPeter.Memishian@Sun.COM 
1387*10649SPeter.Memishian@Sun.COM 	/*
1388*10649SPeter.Memishian@Sun.COM 	 * If the interface was in a named group and was either an active
1389*10649SPeter.Memishian@Sun.COM 	 * standby or the last active interface, try to activate another
1390*10649SPeter.Memishian@Sun.COM 	 * interface to compensate.
1391*10649SPeter.Memishian@Sun.COM 	 */
1392*10649SPeter.Memishian@Sun.COM 	if (pg != phyint_anongroup) {
1393*10649SPeter.Memishian@Sun.COM 		active = _B_FALSE;
1394*10649SPeter.Memishian@Sun.COM 		for (pi2 = pg->pg_phyint; pi2 != NULL; pi2 = pi2->pi_pgnext) {
1395*10649SPeter.Memishian@Sun.COM 			if (phyint_is_functioning(pi2) &&
1396*10649SPeter.Memishian@Sun.COM 			    !(pi2->pi_flags & IFF_INACTIVE)) {
1397*10649SPeter.Memishian@Sun.COM 				active = _B_TRUE;
1398*10649SPeter.Memishian@Sun.COM 				break;
1399*10649SPeter.Memishian@Sun.COM 			}
1400*10649SPeter.Memishian@Sun.COM 		}
1401*10649SPeter.Memishian@Sun.COM 
1402*10649SPeter.Memishian@Sun.COM 		if (!active ||
1403*10649SPeter.Memishian@Sun.COM 		    (pi->pi_flags & (IFF_STANDBY|IFF_INACTIVE)) == IFF_STANDBY)
1404*10649SPeter.Memishian@Sun.COM 			phyint_activate_another(pi);
1405*10649SPeter.Memishian@Sun.COM 	}
1406*10649SPeter.Memishian@Sun.COM 
14078485SPeter.Memishian@Sun.COM 	phyint_link_close(pi);
14080Sstevel@tonic-gate 	free(pi);
14098485SPeter.Memishian@Sun.COM }
14108485SPeter.Memishian@Sun.COM 
14118485SPeter.Memishian@Sun.COM /*
14128485SPeter.Memishian@Sun.COM  * Offline phyint `pi' if at least `minred' usable interfaces remain in the
14138485SPeter.Memishian@Sun.COM  * group.  Returns an IPMP error code.
14148485SPeter.Memishian@Sun.COM  */
14158485SPeter.Memishian@Sun.COM int
phyint_offline(struct phyint * pi,uint_t minred)14168485SPeter.Memishian@Sun.COM phyint_offline(struct phyint *pi, uint_t minred)
14178485SPeter.Memishian@Sun.COM {
14188700SPeter.Memishian@Sun.COM 	boolean_t was_active;
14198485SPeter.Memishian@Sun.COM 	unsigned int nusable = 0;
14208485SPeter.Memishian@Sun.COM 	struct phyint *pi2;
14218485SPeter.Memishian@Sun.COM 	struct phyint_group *pg = pi->pi_group;
14228485SPeter.Memishian@Sun.COM 
14238485SPeter.Memishian@Sun.COM 	/*
14248485SPeter.Memishian@Sun.COM 	 * Verify that enough usable interfaces in the group would remain.
14258485SPeter.Memishian@Sun.COM 	 * As a special case, if the group has failed, allow any non-offline
14268485SPeter.Memishian@Sun.COM 	 * phyints to be offlined.
14278485SPeter.Memishian@Sun.COM 	 */
14288485SPeter.Memishian@Sun.COM 	if (pg != phyint_anongroup) {
14298485SPeter.Memishian@Sun.COM 		for (pi2 = pg->pg_phyint; pi2 != NULL; pi2 = pi2->pi_pgnext) {
14308485SPeter.Memishian@Sun.COM 			if (pi2 == pi)
14318485SPeter.Memishian@Sun.COM 				continue;
14328485SPeter.Memishian@Sun.COM 			if (phyint_is_usable(pi2) ||
14338485SPeter.Memishian@Sun.COM 			    (GROUP_FAILED(pg) && pi2->pi_state != PI_OFFLINE))
14348485SPeter.Memishian@Sun.COM 				nusable++;
14358485SPeter.Memishian@Sun.COM 		}
14368485SPeter.Memishian@Sun.COM 	}
14378485SPeter.Memishian@Sun.COM 	if (nusable < minred)
14388485SPeter.Memishian@Sun.COM 		return (IPMP_EMINRED);
14398485SPeter.Memishian@Sun.COM 
14408700SPeter.Memishian@Sun.COM 	was_active = ((pi->pi_flags & IFF_INACTIVE) == 0);
14418700SPeter.Memishian@Sun.COM 
14428700SPeter.Memishian@Sun.COM 	if (!change_pif_flags(pi, IFF_OFFLINE, IFF_INACTIVE))
14438485SPeter.Memishian@Sun.COM 		return (IPMP_FAILURE);
14448485SPeter.Memishian@Sun.COM 
14458485SPeter.Memishian@Sun.COM 	/*
14468485SPeter.Memishian@Sun.COM 	 * The interface is now offline, so stop probing it.  Note that
14478485SPeter.Memishian@Sun.COM 	 * if_mpadm(1M) will down the test addresses, after receiving a
14488485SPeter.Memishian@Sun.COM 	 * success reply from us. The routing socket message will then make us
14498485SPeter.Memishian@Sun.COM 	 * close the socket used for sending probes. But it is more logical
14508485SPeter.Memishian@Sun.COM 	 * that an offlined interface must not be probed, even if it has test
14518485SPeter.Memishian@Sun.COM 	 * addresses.
14528485SPeter.Memishian@Sun.COM 	 *
14538485SPeter.Memishian@Sun.COM 	 * NOTE: stop_probing() also sets PI_OFFLINE.
14548485SPeter.Memishian@Sun.COM 	 */
14558485SPeter.Memishian@Sun.COM 	stop_probing(pi);
14568485SPeter.Memishian@Sun.COM 
14578485SPeter.Memishian@Sun.COM 	/*
14588485SPeter.Memishian@Sun.COM 	 * If we're offlining the phyint because it has a duplicate hardware
14598485SPeter.Memishian@Sun.COM 	 * address, print a warning -- and leave the link open so that we can
14608485SPeter.Memishian@Sun.COM 	 * be notified of hardware address changes that make it usable again.
14618485SPeter.Memishian@Sun.COM 	 * Otherwise, close the link so that we won't prevent a detach.
14628485SPeter.Memishian@Sun.COM 	 */
14638485SPeter.Memishian@Sun.COM 	if (pi->pi_hwaddrdup) {
14648485SPeter.Memishian@Sun.COM 		logerr("IP interface %s has a hardware address which is not "
14658485SPeter.Memishian@Sun.COM 		    "unique in group %s; offlining\n", pi->pi_name,
14668485SPeter.Memishian@Sun.COM 		    pg->pg_name);
14678485SPeter.Memishian@Sun.COM 	} else {
14688485SPeter.Memishian@Sun.COM 		phyint_link_close(pi);
14698485SPeter.Memishian@Sun.COM 	}
14708485SPeter.Memishian@Sun.COM 
14718485SPeter.Memishian@Sun.COM 	/*
14728485SPeter.Memishian@Sun.COM 	 * If this phyint was preventing another phyint with a duplicate
14738485SPeter.Memishian@Sun.COM 	 * hardware address from being online, bring that one online now.
14748485SPeter.Memishian@Sun.COM 	 */
14758485SPeter.Memishian@Sun.COM 	if (!pi->pi_hwaddrdup &&
14768485SPeter.Memishian@Sun.COM 	    (pi2 = phyint_lookup_hwaddr(pi, _B_FALSE)) != NULL) {
14778485SPeter.Memishian@Sun.COM 		assert(pi2->pi_hwaddrdup);
14788485SPeter.Memishian@Sun.COM 		(void) phyint_undo_offline(pi2);
14798485SPeter.Memishian@Sun.COM 	}
14808485SPeter.Memishian@Sun.COM 
14818485SPeter.Memishian@Sun.COM 	/*
14828485SPeter.Memishian@Sun.COM 	 * If this interface was active, try to activate another INACTIVE
14838485SPeter.Memishian@Sun.COM 	 * interface in the group.
14848485SPeter.Memishian@Sun.COM 	 */
14858700SPeter.Memishian@Sun.COM 	if (was_active)
14868485SPeter.Memishian@Sun.COM 		phyint_activate_another(pi);
14878485SPeter.Memishian@Sun.COM 
14888485SPeter.Memishian@Sun.COM 	return (IPMP_SUCCESS);
14898485SPeter.Memishian@Sun.COM }
14908485SPeter.Memishian@Sun.COM 
14918485SPeter.Memishian@Sun.COM /*
14928485SPeter.Memishian@Sun.COM  * Undo a previous offline of `pi'.  Returns an IPMP error code.
14938485SPeter.Memishian@Sun.COM  */
14948485SPeter.Memishian@Sun.COM int
phyint_undo_offline(struct phyint * pi)14958485SPeter.Memishian@Sun.COM phyint_undo_offline(struct phyint *pi)
14968485SPeter.Memishian@Sun.COM {
14978485SPeter.Memishian@Sun.COM 	if (pi->pi_state != PI_OFFLINE) {
14988485SPeter.Memishian@Sun.COM 		errno = EINVAL;
14998485SPeter.Memishian@Sun.COM 		return (IPMP_FAILURE);
15008485SPeter.Memishian@Sun.COM 	}
15018485SPeter.Memishian@Sun.COM 
15028485SPeter.Memishian@Sun.COM 	/*
15038485SPeter.Memishian@Sun.COM 	 * If necessary, reinitialize our link information and verify that its
15048485SPeter.Memishian@Sun.COM 	 * hardware address is still unique across the group.
15058485SPeter.Memishian@Sun.COM 	 */
15068485SPeter.Memishian@Sun.COM 	if (pi->pi_dh == NULL && !phyint_link_init(pi)) {
15078485SPeter.Memishian@Sun.COM 		errno = EIO;
15088485SPeter.Memishian@Sun.COM 		return (IPMP_FAILURE);
15098485SPeter.Memishian@Sun.COM 	}
15108485SPeter.Memishian@Sun.COM 
15118485SPeter.Memishian@Sun.COM 	if (phyint_lookup_hwaddr(pi, _B_TRUE) != NULL) {
15128485SPeter.Memishian@Sun.COM 		pi->pi_hwaddrdup = _B_TRUE;
15138485SPeter.Memishian@Sun.COM 		return (IPMP_EHWADDRDUP);
15148485SPeter.Memishian@Sun.COM 	}
15158485SPeter.Memishian@Sun.COM 
15168485SPeter.Memishian@Sun.COM 	if (pi->pi_hwaddrdup) {
15178485SPeter.Memishian@Sun.COM 		logerr("IP interface %s now has a unique hardware address in "
15188485SPeter.Memishian@Sun.COM 		    "group %s; onlining\n", pi->pi_name, pi->pi_group->pg_name);
15198485SPeter.Memishian@Sun.COM 		pi->pi_hwaddrdup = _B_FALSE;
15208485SPeter.Memishian@Sun.COM 	}
15218485SPeter.Memishian@Sun.COM 
15228485SPeter.Memishian@Sun.COM 	if (!change_pif_flags(pi, 0, IFF_OFFLINE))
15238485SPeter.Memishian@Sun.COM 		return (IPMP_FAILURE);
15248485SPeter.Memishian@Sun.COM 
15258485SPeter.Memishian@Sun.COM 	/*
15268485SPeter.Memishian@Sun.COM 	 * While the interface was offline, it may have failed (e.g. the link
15278485SPeter.Memishian@Sun.COM 	 * may have gone down).  phyint_inst_check_for_failure() will have
15288485SPeter.Memishian@Sun.COM 	 * already set pi_flags with IFF_FAILED, so we can use that to decide
15298485SPeter.Memishian@Sun.COM 	 * whether the phyint should transition to running.  Note that after
15308485SPeter.Memishian@Sun.COM 	 * we transition to running, we will start sending probes again (if
15318485SPeter.Memishian@Sun.COM 	 * test addresses are configured), which may also reveal that the
15328485SPeter.Memishian@Sun.COM 	 * interface is in fact failed.
15338485SPeter.Memishian@Sun.COM 	 */
15348485SPeter.Memishian@Sun.COM 	if (pi->pi_flags & IFF_FAILED) {
15358485SPeter.Memishian@Sun.COM 		phyint_chstate(pi, PI_FAILED);
15368485SPeter.Memishian@Sun.COM 	} else {
15378485SPeter.Memishian@Sun.COM 		/* calls phyint_chstate() */
15388485SPeter.Memishian@Sun.COM 		phyint_transition_to_running(pi);
15398485SPeter.Memishian@Sun.COM 	}
15408485SPeter.Memishian@Sun.COM 
15418485SPeter.Memishian@Sun.COM 	/*
15428485SPeter.Memishian@Sun.COM 	 * Give the requestor time to configure test addresses before
15438485SPeter.Memishian@Sun.COM 	 * complaining that they're missing.
15448485SPeter.Memishian@Sun.COM 	 */
15458485SPeter.Memishian@Sun.COM 	pi->pi_taddrthresh = getcurrentsec() + TESTADDR_CONF_TIME;
15468485SPeter.Memishian@Sun.COM 
15478485SPeter.Memishian@Sun.COM 	return (IPMP_SUCCESS);
15480Sstevel@tonic-gate }
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate /*
15510Sstevel@tonic-gate  * Delete (unlink and free), the phyint instance.
15520Sstevel@tonic-gate  */
15530Sstevel@tonic-gate void
phyint_inst_delete(struct phyint_instance * pii)15540Sstevel@tonic-gate phyint_inst_delete(struct phyint_instance *pii)
15550Sstevel@tonic-gate {
15560Sstevel@tonic-gate 	struct phyint *pi = pii->pii_phyint;
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	assert(pi != NULL);
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 	if (debug & D_PHYINT) {
15610Sstevel@tonic-gate 		logdebug("phyint_inst_delete(%s %s)\n",
15620Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pi->pi_name);
15630Sstevel@tonic-gate 	}
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	/*
15660Sstevel@tonic-gate 	 * If the phyint instance has associated probe targets
15670Sstevel@tonic-gate 	 * delete all the targets
15680Sstevel@tonic-gate 	 */
15690Sstevel@tonic-gate 	while (pii->pii_targets != NULL)
15700Sstevel@tonic-gate 		target_delete(pii->pii_targets);
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	/*
15730Sstevel@tonic-gate 	 * Delete all the logints associated with this phyint
15740Sstevel@tonic-gate 	 * instance.
15750Sstevel@tonic-gate 	 */
15760Sstevel@tonic-gate 	while (pii->pii_logint != NULL)
15770Sstevel@tonic-gate 		logint_delete(pii->pii_logint);
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	/*
15802074Smeem 	 * Close the socket used to send probes to targets from this phyint.
15810Sstevel@tonic-gate 	 */
15820Sstevel@tonic-gate 	if (pii->pii_probe_sock != -1)
15830Sstevel@tonic-gate 		close_probe_socket(pii, _B_TRUE);
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 	/*
15860Sstevel@tonic-gate 	 * Phyint instance must be in the list of all phyint instances.
15870Sstevel@tonic-gate 	 * Remove phyint instance from the global list of phyint instances.
15880Sstevel@tonic-gate 	 */
15890Sstevel@tonic-gate 	assert(phyint_instances == pii || pii->pii_prev != NULL);
15900Sstevel@tonic-gate 	if (pii->pii_prev == NULL) {
15910Sstevel@tonic-gate 		/* Phyint is the 1st in the list */
15920Sstevel@tonic-gate 		phyint_instances = pii->pii_next;
15930Sstevel@tonic-gate 	} else {
15940Sstevel@tonic-gate 		pii->pii_prev->pii_next = pii->pii_next;
15950Sstevel@tonic-gate 	}
15960Sstevel@tonic-gate 	if (pii->pii_next != NULL)
15970Sstevel@tonic-gate 		pii->pii_next->pii_prev = pii->pii_prev;
15980Sstevel@tonic-gate 	pii->pii_next = NULL;
15990Sstevel@tonic-gate 	pii->pii_prev = NULL;
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	/*
16020Sstevel@tonic-gate 	 * Reset the phyint instance pointer in the phyint.
16030Sstevel@tonic-gate 	 * If this is the last phyint instance (being deleted) on this
16040Sstevel@tonic-gate 	 * phyint, then delete the phyint.
16050Sstevel@tonic-gate 	 */
16060Sstevel@tonic-gate 	if (pii->pii_af == AF_INET)
16070Sstevel@tonic-gate 		pi->pi_v4 = NULL;
16080Sstevel@tonic-gate 	else
16090Sstevel@tonic-gate 		pi->pi_v6 = NULL;
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	if (pi->pi_v4 == NULL && pi->pi_v6 == NULL)
16120Sstevel@tonic-gate 		phyint_delete(pi);
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	free(pii);
16150Sstevel@tonic-gate }
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate static void
phyint_inst_print(struct phyint_instance * pii)16180Sstevel@tonic-gate phyint_inst_print(struct phyint_instance *pii)
16190Sstevel@tonic-gate {
16200Sstevel@tonic-gate 	struct logint *li;
16210Sstevel@tonic-gate 	struct target *tg;
16220Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
16230Sstevel@tonic-gate 	int most_recent;
16240Sstevel@tonic-gate 	int i;
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	if (pii->pii_phyint == NULL) {
16270Sstevel@tonic-gate 		logdebug("pii->pi_phyint NULL can't print\n");
16280Sstevel@tonic-gate 		return;
16290Sstevel@tonic-gate 	}
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	logdebug("\nPhyint instance: %s %s index %u state %x flags %llx	 "
16328485SPeter.Memishian@Sun.COM 	    "sock %x in_use %d\n",
16330Sstevel@tonic-gate 	    AF_STR(pii->pii_af), pii->pii_name, pii->pii_ifindex,
16340Sstevel@tonic-gate 	    pii->pii_state, pii->pii_phyint->pi_flags, pii->pii_probe_sock,
16358485SPeter.Memishian@Sun.COM 	    pii->pii_in_use);
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 	for (li = pii->pii_logint; li != NULL; li = li->li_next)
16380Sstevel@tonic-gate 		logint_print(li);
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 	logdebug("\n");
16410Sstevel@tonic-gate 	for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next)
16420Sstevel@tonic-gate 		target_print(tg);
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 	if (pii->pii_targets == NULL)
16450Sstevel@tonic-gate 		logdebug("pi_targets NULL\n");
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	if (pii->pii_target_next != NULL) {
16480Sstevel@tonic-gate 		logdebug("pi_target_next %s %s\n", AF_STR(pii->pii_af),
16490Sstevel@tonic-gate 		    pr_addr(pii->pii_af, pii->pii_target_next->tg_address,
16504770Smeem 		    abuf, sizeof (abuf)));
16510Sstevel@tonic-gate 	} else {
16520Sstevel@tonic-gate 		logdebug("pi_target_next NULL\n");
16530Sstevel@tonic-gate 	}
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	if (pii->pii_rtt_target_next != NULL) {
16560Sstevel@tonic-gate 		logdebug("pi_rtt_target_next %s %s\n", AF_STR(pii->pii_af),
16570Sstevel@tonic-gate 		    pr_addr(pii->pii_af, pii->pii_rtt_target_next->tg_address,
16584770Smeem 		    abuf, sizeof (abuf)));
16590Sstevel@tonic-gate 	} else {
16600Sstevel@tonic-gate 		logdebug("pi_rtt_target_next NULL\n");
16610Sstevel@tonic-gate 	}
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 	if (pii->pii_targets != NULL) {
16640Sstevel@tonic-gate 		most_recent = PROBE_INDEX_PREV(pii->pii_probe_next);
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 		i = most_recent;
16670Sstevel@tonic-gate 		do {
16680Sstevel@tonic-gate 			if (pii->pii_probes[i].pr_target != NULL) {
16690Sstevel@tonic-gate 				logdebug("#%d target %s ", i,
16700Sstevel@tonic-gate 				    pr_addr(pii->pii_af,
16710Sstevel@tonic-gate 				    pii->pii_probes[i].pr_target->tg_address,
16720Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
16730Sstevel@tonic-gate 			} else {
16740Sstevel@tonic-gate 				logdebug("#%d target NULL ", i);
16750Sstevel@tonic-gate 			}
16768485SPeter.Memishian@Sun.COM 			logdebug("time_start %lld status %d "
16778485SPeter.Memishian@Sun.COM 			    "time_ackproc %lld time_lost %u",
16788485SPeter.Memishian@Sun.COM 			    pii->pii_probes[i].pr_hrtime_start,
16790Sstevel@tonic-gate 			    pii->pii_probes[i].pr_status,
16808485SPeter.Memishian@Sun.COM 			    pii->pii_probes[i].pr_hrtime_ackproc,
16810Sstevel@tonic-gate 			    pii->pii_probes[i].pr_time_lost);
16820Sstevel@tonic-gate 			i = PROBE_INDEX_PREV(i);
16830Sstevel@tonic-gate 		} while (i != most_recent);
16840Sstevel@tonic-gate 	}
16850Sstevel@tonic-gate }
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate /*
16880Sstevel@tonic-gate  * Lookup a logint based on the logical interface name, on the given
16890Sstevel@tonic-gate  * phyint instance.
16900Sstevel@tonic-gate  */
16910Sstevel@tonic-gate static struct logint *
logint_lookup(struct phyint_instance * pii,char * name)16920Sstevel@tonic-gate logint_lookup(struct phyint_instance *pii, char *name)
16930Sstevel@tonic-gate {
16940Sstevel@tonic-gate 	struct logint *li;
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 	if (debug & D_LOGINT) {
16970Sstevel@tonic-gate 		logdebug("logint_lookup(%s, %s)\n",
16980Sstevel@tonic-gate 		    AF_STR(pii->pii_af), name);
16990Sstevel@tonic-gate 	}
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate 	for (li = pii->pii_logint; li != NULL; li = li->li_next) {
17020Sstevel@tonic-gate 		if (strncmp(name, li->li_name, sizeof (li->li_name)) == 0)
17030Sstevel@tonic-gate 			break;
17040Sstevel@tonic-gate 	}
17050Sstevel@tonic-gate 	return (li);
17060Sstevel@tonic-gate }
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate /*
17090Sstevel@tonic-gate  * Insert a logint at the head of the list of logints of the given
17100Sstevel@tonic-gate  * phyint instance
17110Sstevel@tonic-gate  */
17120Sstevel@tonic-gate static void
logint_insert(struct phyint_instance * pii,struct logint * li)17130Sstevel@tonic-gate logint_insert(struct phyint_instance *pii, struct logint *li)
17140Sstevel@tonic-gate {
17150Sstevel@tonic-gate 	li->li_next = pii->pii_logint;
17160Sstevel@tonic-gate 	li->li_prev = NULL;
17170Sstevel@tonic-gate 	if (pii->pii_logint != NULL)
17180Sstevel@tonic-gate 		pii->pii_logint->li_prev = li;
17190Sstevel@tonic-gate 	pii->pii_logint = li;
17200Sstevel@tonic-gate 	li->li_phyint_inst = pii;
17210Sstevel@tonic-gate }
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate /*
17240Sstevel@tonic-gate  * Create a new named logint, on the specified phyint instance.
17250Sstevel@tonic-gate  */
17260Sstevel@tonic-gate static struct logint *
logint_create(struct phyint_instance * pii,char * name)17270Sstevel@tonic-gate logint_create(struct phyint_instance *pii, char *name)
17280Sstevel@tonic-gate {
17290Sstevel@tonic-gate 	struct logint *li;
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 	if (debug & D_LOGINT) {
17320Sstevel@tonic-gate 		logdebug("logint_create(%s %s %s)\n",
17330Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name, name);
17340Sstevel@tonic-gate 	}
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 	li = calloc(1, sizeof (struct logint));
17370Sstevel@tonic-gate 	if (li == NULL) {
17380Sstevel@tonic-gate 		logperror("logint_create: calloc");
17390Sstevel@tonic-gate 		return (NULL);
17400Sstevel@tonic-gate 	}
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate 	(void) strncpy(li->li_name, name, sizeof (li->li_name));
17430Sstevel@tonic-gate 	li->li_name[sizeof (li->li_name) - 1] = '\0';
17440Sstevel@tonic-gate 	logint_insert(pii, li);
17450Sstevel@tonic-gate 	return (li);
17460Sstevel@tonic-gate }
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate /*
17490Sstevel@tonic-gate  * Initialize the logint based on the data returned by the kernel.
17500Sstevel@tonic-gate  */
17510Sstevel@tonic-gate void
logint_init_from_k(struct phyint_instance * pii,char * li_name)17520Sstevel@tonic-gate logint_init_from_k(struct phyint_instance *pii, char *li_name)
17530Sstevel@tonic-gate {
17540Sstevel@tonic-gate 	int	ifsock;
17550Sstevel@tonic-gate 	uint64_t flags;
17560Sstevel@tonic-gate 	uint64_t saved_flags;
17570Sstevel@tonic-gate 	struct	logint	*li;
17580Sstevel@tonic-gate 	struct lifreq	lifr;
17590Sstevel@tonic-gate 	struct in6_addr	test_subnet;
17600Sstevel@tonic-gate 	struct in6_addr	testaddr;
17610Sstevel@tonic-gate 	int	test_subnet_len;
17620Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
17630Sstevel@tonic-gate 	struct sockaddr_in	*sin;
17640Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
17650Sstevel@tonic-gate 	boolean_t  ptp = _B_FALSE;
17660Sstevel@tonic-gate 	struct in6_addr tgaddr;
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate 	if (debug & D_LOGINT) {
17690Sstevel@tonic-gate 		logdebug("logint_init_from_k(%s %s)\n",
17700Sstevel@tonic-gate 		    AF_STR(pii->pii_af), li_name);
17710Sstevel@tonic-gate 	}
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate 	/* Get the socket for doing ioctls */
17740Sstevel@tonic-gate 	ifsock = (pii->pii_af == AF_INET) ? ifsock_v4 : ifsock_v6;
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	/*
17770Sstevel@tonic-gate 	 * Get the flags from the kernel. Also serves as a check whether
17780Sstevel@tonic-gate 	 * the logical still exists. If it doesn't exist, no need to proceed
17790Sstevel@tonic-gate 	 * any further. li_in_use will make the caller clean up the logint
17800Sstevel@tonic-gate 	 */
17810Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, li_name, sizeof (lifr.lifr_name));
17820Sstevel@tonic-gate 	lifr.lifr_name[sizeof (lifr.lifr_name) - 1] = '\0';
17830Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFFLAGS, (char *)&lifr) < 0) {
17840Sstevel@tonic-gate 		/* Interface may have vanished */
17850Sstevel@tonic-gate 		if (errno != ENXIO) {
17860Sstevel@tonic-gate 			logperror_pii(pii, "logint_init_from_k: "
17870Sstevel@tonic-gate 			    "ioctl (get flags)");
17880Sstevel@tonic-gate 		}
17890Sstevel@tonic-gate 		return;
17900Sstevel@tonic-gate 	}
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 	flags = lifr.lifr_flags;
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	/*
17950Sstevel@tonic-gate 	 * Verified the logint exists. Now lookup the logint in our tables.
17960Sstevel@tonic-gate 	 * If it does not exist, create a new logint.
17970Sstevel@tonic-gate 	 */
17980Sstevel@tonic-gate 	li = logint_lookup(pii, li_name);
17990Sstevel@tonic-gate 	if (li == NULL) {
18000Sstevel@tonic-gate 		li = logint_create(pii, li_name);
18010Sstevel@tonic-gate 		if (li == NULL) {
18020Sstevel@tonic-gate 			/*
18030Sstevel@tonic-gate 			 * Pretend the interface does not exist
18040Sstevel@tonic-gate 			 * in the kernel
18050Sstevel@tonic-gate 			 */
18060Sstevel@tonic-gate 			return;
18070Sstevel@tonic-gate 		}
18080Sstevel@tonic-gate 	}
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate 	/*
18110Sstevel@tonic-gate 	 * Update li->li_flags with the new flags, after saving the old
18120Sstevel@tonic-gate 	 * value. This is used later to check what flags has changed and
18130Sstevel@tonic-gate 	 * take any action
18140Sstevel@tonic-gate 	 */
18150Sstevel@tonic-gate 	saved_flags = li->li_flags;
18160Sstevel@tonic-gate 	li->li_flags = flags;
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 	/*
18190Sstevel@tonic-gate 	 * Get the address, prefix, prefixlength and update the logint.
18200Sstevel@tonic-gate 	 * Check if anything has changed. If the logint used for the
18210Sstevel@tonic-gate 	 * test address has changed, take suitable action.
18220Sstevel@tonic-gate 	 */
18230Sstevel@tonic-gate 	if (ioctl(ifsock, SIOCGLIFADDR, (char *)&lifr) < 0) {
18240Sstevel@tonic-gate 		/* Interface may have vanished */
18250Sstevel@tonic-gate 		if (errno != ENXIO) {
18260Sstevel@tonic-gate 			logperror_li(li, "logint_init_from_k: (get addr)");
18270Sstevel@tonic-gate 		}
18280Sstevel@tonic-gate 		goto error;
18290Sstevel@tonic-gate 	}
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 	if (pii->pii_af == AF_INET) {
18320Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
18330Sstevel@tonic-gate 		IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &testaddr);
18340Sstevel@tonic-gate 	} else {
18350Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
18360Sstevel@tonic-gate 		testaddr = sin6->sin6_addr;
18370Sstevel@tonic-gate 	}
18380Sstevel@tonic-gate 
18398485SPeter.Memishian@Sun.COM 	if (ioctl(ifsock, SIOCGLIFSUBNET, (char *)&lifr) < 0) {
18408485SPeter.Memishian@Sun.COM 		/* Interface may have vanished */
18418485SPeter.Memishian@Sun.COM 		if (errno != ENXIO)
18428485SPeter.Memishian@Sun.COM 			logperror_li(li, "logint_init_from_k: (get subnet)");
18438485SPeter.Memishian@Sun.COM 		goto error;
18448485SPeter.Memishian@Sun.COM 	}
18458485SPeter.Memishian@Sun.COM 	if (lifr.lifr_subnet.ss_family == AF_INET6) {
18468485SPeter.Memishian@Sun.COM 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_subnet;
18478485SPeter.Memishian@Sun.COM 		test_subnet = sin6->sin6_addr;
18488485SPeter.Memishian@Sun.COM 		test_subnet_len = lifr.lifr_addrlen;
18490Sstevel@tonic-gate 	} else {
18508485SPeter.Memishian@Sun.COM 		sin = (struct sockaddr_in *)&lifr.lifr_subnet;
18518485SPeter.Memishian@Sun.COM 		IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &test_subnet);
18528485SPeter.Memishian@Sun.COM 		test_subnet_len = lifr.lifr_addrlen + (IPV6_ABITS - IP_ABITS);
18530Sstevel@tonic-gate 	}
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 	/*
18560Sstevel@tonic-gate 	 * If this is the logint corresponding to the test address used for
18570Sstevel@tonic-gate 	 * sending probes, then if anything significant has changed we need to
18580Sstevel@tonic-gate 	 * determine the test address again.  We ignore changes to the
18590Sstevel@tonic-gate 	 * IFF_FAILED and IFF_RUNNING flags since those happen as a matter of
18600Sstevel@tonic-gate 	 * course.
18610Sstevel@tonic-gate 	 */
18620Sstevel@tonic-gate 	if (pii->pii_probe_logint == li) {
18630Sstevel@tonic-gate 		if (((li->li_flags ^ saved_flags) &
18640Sstevel@tonic-gate 		    ~(IFF_FAILED | IFF_RUNNING)) != 0 ||
18650Sstevel@tonic-gate 		    !IN6_ARE_ADDR_EQUAL(&testaddr, &li->li_addr) ||
18660Sstevel@tonic-gate 		    (!ptp && !IN6_ARE_ADDR_EQUAL(&test_subnet,
18674770Smeem 		    &li->li_subnet)) ||
18680Sstevel@tonic-gate 		    (!ptp && test_subnet_len != li->li_subnet_len) ||
18690Sstevel@tonic-gate 		    (ptp && !IN6_ARE_ADDR_EQUAL(&tgaddr, &li->li_dstaddr))) {
18700Sstevel@tonic-gate 			/*
18710Sstevel@tonic-gate 			 * Something significant that affects the testaddress
18720Sstevel@tonic-gate 			 * has changed. Redo the testaddress selection later on
18730Sstevel@tonic-gate 			 * in select_test_ifs(). For now do the cleanup and
18740Sstevel@tonic-gate 			 * set pii_probe_logint to NULL.
18750Sstevel@tonic-gate 			 */
18760Sstevel@tonic-gate 			if (pii->pii_probe_sock != -1)
18770Sstevel@tonic-gate 				close_probe_socket(pii, _B_TRUE);
18780Sstevel@tonic-gate 			pii->pii_probe_logint = NULL;
18790Sstevel@tonic-gate 		}
18800Sstevel@tonic-gate 	}
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 	/* Update the logint with the values obtained from the kernel.	*/
18840Sstevel@tonic-gate 	li->li_addr = testaddr;
18850Sstevel@tonic-gate 	li->li_in_use = 1;
18860Sstevel@tonic-gate 	if (ptp) {
18870Sstevel@tonic-gate 		li->li_dstaddr = tgaddr;
18880Sstevel@tonic-gate 		li->li_subnet_len = (pii->pii_af == AF_INET) ?
18890Sstevel@tonic-gate 		    IP_ABITS : IPV6_ABITS;
18900Sstevel@tonic-gate 	} else {
18910Sstevel@tonic-gate 		li->li_subnet = test_subnet;
18920Sstevel@tonic-gate 		li->li_subnet_len = test_subnet_len;
18930Sstevel@tonic-gate 	}
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 	if (debug & D_LOGINT)
18960Sstevel@tonic-gate 		logint_print(li);
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	return;
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate error:
19010Sstevel@tonic-gate 	logerr("logint_init_from_k: IGNORED %s %s %s addr %s\n",
19020Sstevel@tonic-gate 	    AF_STR(pii->pii_af), pii->pii_name, li->li_name,
19030Sstevel@tonic-gate 	    pr_addr(pii->pii_af, testaddr, abuf, sizeof (abuf)));
19040Sstevel@tonic-gate 	logint_delete(li);
19050Sstevel@tonic-gate }
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate /*
19080Sstevel@tonic-gate  * Delete (unlink and free) a logint.
19090Sstevel@tonic-gate  */
19100Sstevel@tonic-gate void
logint_delete(struct logint * li)19110Sstevel@tonic-gate logint_delete(struct logint *li)
19120Sstevel@tonic-gate {
19130Sstevel@tonic-gate 	struct phyint_instance *pii;
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	pii = li->li_phyint_inst;
19160Sstevel@tonic-gate 	assert(pii != NULL);
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate 	if (debug & D_LOGINT) {
19190Sstevel@tonic-gate 		int af;
19200Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 		af = pii->pii_af;
19230Sstevel@tonic-gate 		logdebug("logint_delete(%s %s %s/%u)\n",
19240Sstevel@tonic-gate 		    AF_STR(af), li->li_name,
19250Sstevel@tonic-gate 		    pr_addr(af, li->li_addr, abuf, sizeof (abuf)),
19260Sstevel@tonic-gate 		    li->li_subnet_len);
19270Sstevel@tonic-gate 	}
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 	/* logint must be in the list of logints */
19300Sstevel@tonic-gate 	assert(pii->pii_logint == li || li->li_prev != NULL);
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 	/* Remove the logint from the list of logints  */
19330Sstevel@tonic-gate 	if (li->li_prev == NULL) {
19340Sstevel@tonic-gate 		/* logint is the 1st in the list */
19350Sstevel@tonic-gate 		pii->pii_logint = li->li_next;
19360Sstevel@tonic-gate 	} else {
19370Sstevel@tonic-gate 		li->li_prev->li_next = li->li_next;
19380Sstevel@tonic-gate 	}
19390Sstevel@tonic-gate 	if (li->li_next != NULL)
19400Sstevel@tonic-gate 		li->li_next->li_prev = li->li_prev;
19410Sstevel@tonic-gate 	li->li_next = NULL;
19420Sstevel@tonic-gate 	li->li_prev = NULL;
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 	/*
19452074Smeem 	 * If this logint is also being used for probing, then close the
19462074Smeem 	 * associated socket, if it exists.
19470Sstevel@tonic-gate 	 */
19480Sstevel@tonic-gate 	if (pii->pii_probe_logint == li) {
19490Sstevel@tonic-gate 		if (pii->pii_probe_sock != -1)
19500Sstevel@tonic-gate 			close_probe_socket(pii, _B_TRUE);
19510Sstevel@tonic-gate 		pii->pii_probe_logint = NULL;
19520Sstevel@tonic-gate 	}
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 	free(li);
19550Sstevel@tonic-gate }
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate static void
logint_print(struct logint * li)19580Sstevel@tonic-gate logint_print(struct logint *li)
19590Sstevel@tonic-gate {
19600Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
19618485SPeter.Memishian@Sun.COM 	int af = li->li_phyint_inst->pii_af;
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 	logdebug("logint: %s %s addr %s/%u", AF_STR(af), li->li_name,
19640Sstevel@tonic-gate 	    pr_addr(af, li->li_addr, abuf, sizeof (abuf)), li->li_subnet_len);
19650Sstevel@tonic-gate 
19668485SPeter.Memishian@Sun.COM 	logdebug("\tFlags: %llx in_use %d\n", li->li_flags, li->li_in_use);
19670Sstevel@tonic-gate }
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate char *
pr_addr(int af,struct in6_addr addr,char * abuf,int len)19700Sstevel@tonic-gate pr_addr(int af, struct in6_addr addr, char *abuf, int len)
19710Sstevel@tonic-gate {
19720Sstevel@tonic-gate 	struct in_addr	addr_v4;
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 	if (af == AF_INET) {
19750Sstevel@tonic-gate 		IN6_V4MAPPED_TO_INADDR(&addr, &addr_v4);
19760Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, (void *)&addr_v4, abuf, len);
19770Sstevel@tonic-gate 	} else {
19780Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, (void *)&addr, abuf, len);
19790Sstevel@tonic-gate 	}
19800Sstevel@tonic-gate 	return (abuf);
19810Sstevel@tonic-gate }
19820Sstevel@tonic-gate 
19838485SPeter.Memishian@Sun.COM /*
19848485SPeter.Memishian@Sun.COM  * Fill in the sockaddr_storage pointed to by `ssp' with the IP address
19858485SPeter.Memishian@Sun.COM  * represented by the [`af',`addr'] pair.  Needed because in.mpathd internally
19868485SPeter.Memishian@Sun.COM  * stores all addresses as in6_addrs, but we don't want to expose that.
19878485SPeter.Memishian@Sun.COM  */
19888485SPeter.Memishian@Sun.COM void
addr2storage(int af,const struct in6_addr * addr,struct sockaddr_storage * ssp)19898485SPeter.Memishian@Sun.COM addr2storage(int af, const struct in6_addr *addr, struct sockaddr_storage *ssp)
19908485SPeter.Memishian@Sun.COM {
19918485SPeter.Memishian@Sun.COM 	struct sockaddr_in *sinp = (struct sockaddr_in *)ssp;
19928485SPeter.Memishian@Sun.COM 	struct sockaddr_in6 *sin6p = (struct sockaddr_in6 *)ssp;
19938485SPeter.Memishian@Sun.COM 
19948485SPeter.Memishian@Sun.COM 	assert(af == AF_INET || af == AF_INET6);
19958485SPeter.Memishian@Sun.COM 
19968485SPeter.Memishian@Sun.COM 	switch (af) {
19978485SPeter.Memishian@Sun.COM 	case AF_INET:
19988485SPeter.Memishian@Sun.COM 		(void) memset(sinp, 0, sizeof (*sinp));
19998485SPeter.Memishian@Sun.COM 		sinp->sin_family = AF_INET;
20008485SPeter.Memishian@Sun.COM 		IN6_V4MAPPED_TO_INADDR(addr, &sinp->sin_addr);
20018485SPeter.Memishian@Sun.COM 		break;
20028485SPeter.Memishian@Sun.COM 	case AF_INET6:
20038485SPeter.Memishian@Sun.COM 		(void) memset(sin6p, 0, sizeof (*sin6p));
20048485SPeter.Memishian@Sun.COM 		sin6p->sin6_family = AF_INET6;
20058485SPeter.Memishian@Sun.COM 		sin6p->sin6_addr = *addr;
20068485SPeter.Memishian@Sun.COM 		break;
20078485SPeter.Memishian@Sun.COM 	}
20088485SPeter.Memishian@Sun.COM }
20098485SPeter.Memishian@Sun.COM 
20100Sstevel@tonic-gate /* Lookup target on its address */
20110Sstevel@tonic-gate struct target *
target_lookup(struct phyint_instance * pii,struct in6_addr addr)20120Sstevel@tonic-gate target_lookup(struct phyint_instance *pii, struct in6_addr addr)
20130Sstevel@tonic-gate {
20140Sstevel@tonic-gate 	struct target *tg;
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 	if (debug & D_TARGET) {
20170Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 		logdebug("target_lookup(%s %s): addr %s\n",
20200Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name,
20210Sstevel@tonic-gate 		    pr_addr(pii->pii_af, addr, abuf, sizeof (abuf)));
20220Sstevel@tonic-gate 	}
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 	for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
20250Sstevel@tonic-gate 		if (IN6_ARE_ADDR_EQUAL(&tg->tg_address, &addr))
20260Sstevel@tonic-gate 			break;
20270Sstevel@tonic-gate 	}
20280Sstevel@tonic-gate 	return (tg);
20290Sstevel@tonic-gate }
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate /*
20320Sstevel@tonic-gate  * Find and return the next active target, for the next probe.
20330Sstevel@tonic-gate  * If no active targets are available, return NULL.
20340Sstevel@tonic-gate  */
20350Sstevel@tonic-gate struct target *
target_next(struct target * tg)20360Sstevel@tonic-gate target_next(struct target *tg)
20370Sstevel@tonic-gate {
20380Sstevel@tonic-gate 	struct	phyint_instance	*pii = tg->tg_phyint_inst;
20390Sstevel@tonic-gate 	struct	target	*marker = tg;
20400Sstevel@tonic-gate 	hrtime_t now;
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 	now = gethrtime();
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 	/*
20450Sstevel@tonic-gate 	 * Target must be in the list of targets for this phyint
20460Sstevel@tonic-gate 	 * instance.
20470Sstevel@tonic-gate 	 */
20480Sstevel@tonic-gate 	assert(pii->pii_targets == tg || tg->tg_prev != NULL);
20490Sstevel@tonic-gate 	assert(pii->pii_targets != NULL);
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 	/* Return the next active target */
20520Sstevel@tonic-gate 	do {
20530Sstevel@tonic-gate 		/*
20540Sstevel@tonic-gate 		 * Go to the next target. If we hit the end,
20550Sstevel@tonic-gate 		 * reset the ptr to the head
20560Sstevel@tonic-gate 		 */
20570Sstevel@tonic-gate 		tg = tg->tg_next;
20580Sstevel@tonic-gate 		if (tg == NULL)
20590Sstevel@tonic-gate 			tg = pii->pii_targets;
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 		assert(TG_STATUS_VALID(tg->tg_status));
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 		switch (tg->tg_status) {
20640Sstevel@tonic-gate 		case TG_ACTIVE:
20650Sstevel@tonic-gate 			return (tg);
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 		case TG_UNUSED:
20680Sstevel@tonic-gate 			assert(pii->pii_targets_are_routers);
20690Sstevel@tonic-gate 			if (pii->pii_ntargets < MAX_PROBE_TARGETS) {
20700Sstevel@tonic-gate 				/*
20710Sstevel@tonic-gate 				 * Bubble up the unused target to active
20720Sstevel@tonic-gate 				 */
20730Sstevel@tonic-gate 				tg->tg_status = TG_ACTIVE;
20740Sstevel@tonic-gate 				pii->pii_ntargets++;
20750Sstevel@tonic-gate 				return (tg);
20760Sstevel@tonic-gate 			}
20770Sstevel@tonic-gate 			break;
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate 		case TG_SLOW:
20800Sstevel@tonic-gate 			assert(pii->pii_targets_are_routers);
20810Sstevel@tonic-gate 			if (tg->tg_latime + MIN_RECOVERY_TIME < now) {
20820Sstevel@tonic-gate 				/*
20830Sstevel@tonic-gate 				 * Bubble up the slow target to unused
20840Sstevel@tonic-gate 				 */
20850Sstevel@tonic-gate 				tg->tg_status = TG_UNUSED;
20860Sstevel@tonic-gate 			}
20870Sstevel@tonic-gate 			break;
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 		case TG_DEAD:
20900Sstevel@tonic-gate 			assert(pii->pii_targets_are_routers);
20910Sstevel@tonic-gate 			if (tg->tg_latime + MIN_RECOVERY_TIME < now) {
20920Sstevel@tonic-gate 				/*
20930Sstevel@tonic-gate 				 * Bubble up the dead target to slow
20940Sstevel@tonic-gate 				 */
20950Sstevel@tonic-gate 				tg->tg_status = TG_SLOW;
20960Sstevel@tonic-gate 				tg->tg_latime = now;
20970Sstevel@tonic-gate 			}
20980Sstevel@tonic-gate 			break;
20990Sstevel@tonic-gate 		}
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	} while (tg != marker);
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 	return (NULL);
21040Sstevel@tonic-gate }
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate /*
21070Sstevel@tonic-gate  * Select the best available target, that is not already TG_ACTIVE,
21080Sstevel@tonic-gate  * for the caller. The caller will determine whether it wants to
21090Sstevel@tonic-gate  * make the returned target TG_ACTIVE.
21100Sstevel@tonic-gate  * The selection order is as follows.
21110Sstevel@tonic-gate  * 1. pick a TG_UNSED target, if it exists.
21120Sstevel@tonic-gate  * 2. else pick a TG_SLOW target that has recovered, if it exists
21130Sstevel@tonic-gate  * 3. else pick any TG_SLOW target, if it exists
21140Sstevel@tonic-gate  * 4. else pick a TG_DEAD target that has recovered, if it exists
21150Sstevel@tonic-gate  * 5. else pick any TG_DEAD target, if it exists
21160Sstevel@tonic-gate  * 6. else return null
21170Sstevel@tonic-gate  */
21180Sstevel@tonic-gate static struct target *
target_select_best(struct phyint_instance * pii)21190Sstevel@tonic-gate target_select_best(struct phyint_instance *pii)
21200Sstevel@tonic-gate {
21210Sstevel@tonic-gate 	struct target *tg;
21220Sstevel@tonic-gate 	struct target *slow = NULL;
21230Sstevel@tonic-gate 	struct target *dead = NULL;
21240Sstevel@tonic-gate 	struct target *slow_recovered = NULL;
21250Sstevel@tonic-gate 	struct target *dead_recovered = NULL;
21260Sstevel@tonic-gate 	hrtime_t now;
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate 	now = gethrtime();
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 	for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
21310Sstevel@tonic-gate 		assert(TG_STATUS_VALID(tg->tg_status));
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 		switch (tg->tg_status) {
21340Sstevel@tonic-gate 		case TG_UNUSED:
21350Sstevel@tonic-gate 			return (tg);
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 		case TG_SLOW:
21380Sstevel@tonic-gate 			if (tg->tg_latime + MIN_RECOVERY_TIME < now) {
21390Sstevel@tonic-gate 				slow_recovered = tg;
21400Sstevel@tonic-gate 				/*
21418485SPeter.Memishian@Sun.COM 				 * Promote the slow_recovered to unused
21420Sstevel@tonic-gate 				 */
21430Sstevel@tonic-gate 				tg->tg_status = TG_UNUSED;
21440Sstevel@tonic-gate 			} else {
21450Sstevel@tonic-gate 				slow = tg;
21460Sstevel@tonic-gate 			}
21470Sstevel@tonic-gate 			break;
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate 		case TG_DEAD:
21500Sstevel@tonic-gate 			if (tg->tg_latime + MIN_RECOVERY_TIME < now) {
21510Sstevel@tonic-gate 				dead_recovered = tg;
21520Sstevel@tonic-gate 				/*
21538485SPeter.Memishian@Sun.COM 				 * Promote the dead_recovered to slow
21540Sstevel@tonic-gate 				 */
21550Sstevel@tonic-gate 				tg->tg_status = TG_SLOW;
21560Sstevel@tonic-gate 				tg->tg_latime = now;
21570Sstevel@tonic-gate 			} else {
21580Sstevel@tonic-gate 				dead = tg;
21590Sstevel@tonic-gate 			}
21600Sstevel@tonic-gate 			break;
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate 		default:
21630Sstevel@tonic-gate 			break;
21640Sstevel@tonic-gate 		}
21650Sstevel@tonic-gate 	}
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 	if (slow_recovered != NULL)
21680Sstevel@tonic-gate 		return (slow_recovered);
21690Sstevel@tonic-gate 	else if (slow != NULL)
21700Sstevel@tonic-gate 		return (slow);
21710Sstevel@tonic-gate 	else if (dead_recovered != NULL)
21720Sstevel@tonic-gate 		return (dead_recovered);
21730Sstevel@tonic-gate 	else
21740Sstevel@tonic-gate 		return (dead);
21750Sstevel@tonic-gate }
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate /*
21780Sstevel@tonic-gate  * Some target was deleted. If we don't have even MIN_PROBE_TARGETS
21790Sstevel@tonic-gate  * that are active, pick the next best below.
21800Sstevel@tonic-gate  */
21810Sstevel@tonic-gate static void
target_activate_all(struct phyint_instance * pii)21820Sstevel@tonic-gate target_activate_all(struct phyint_instance *pii)
21830Sstevel@tonic-gate {
21840Sstevel@tonic-gate 	struct target *tg;
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 	assert(pii->pii_ntargets == 0);
21870Sstevel@tonic-gate 	assert(pii->pii_target_next == NULL);
21880Sstevel@tonic-gate 	assert(pii->pii_rtt_target_next == NULL);
21890Sstevel@tonic-gate 	assert(pii->pii_targets_are_routers);
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	while (pii->pii_ntargets < MIN_PROBE_TARGETS) {
21920Sstevel@tonic-gate 		tg = target_select_best(pii);
21930Sstevel@tonic-gate 		if (tg == NULL) {
21940Sstevel@tonic-gate 			/* We are out of targets */
21950Sstevel@tonic-gate 			return;
21960Sstevel@tonic-gate 		}
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 		assert(TG_STATUS_VALID(tg->tg_status));
21990Sstevel@tonic-gate 		assert(tg->tg_status != TG_ACTIVE);
22000Sstevel@tonic-gate 		tg->tg_status = TG_ACTIVE;
22010Sstevel@tonic-gate 		pii->pii_ntargets++;
22020Sstevel@tonic-gate 		if (pii->pii_target_next == NULL) {
22030Sstevel@tonic-gate 			pii->pii_target_next = tg;
22040Sstevel@tonic-gate 			pii->pii_rtt_target_next = tg;
22050Sstevel@tonic-gate 		}
22060Sstevel@tonic-gate 	}
22070Sstevel@tonic-gate }
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate static struct target *
target_first(struct phyint_instance * pii)22100Sstevel@tonic-gate target_first(struct phyint_instance *pii)
22110Sstevel@tonic-gate {
22120Sstevel@tonic-gate 	struct target *tg;
22130Sstevel@tonic-gate 
22140Sstevel@tonic-gate 	for (tg = pii->pii_targets; tg != NULL; tg = tg->tg_next) {
22150Sstevel@tonic-gate 		assert(TG_STATUS_VALID(tg->tg_status));
22160Sstevel@tonic-gate 		if (tg->tg_status == TG_ACTIVE)
22170Sstevel@tonic-gate 			break;
22180Sstevel@tonic-gate 	}
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 	return (tg);
22210Sstevel@tonic-gate }
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate /*
22240Sstevel@tonic-gate  * Create a default target entry.
22250Sstevel@tonic-gate  */
22260Sstevel@tonic-gate void
target_create(struct phyint_instance * pii,struct in6_addr addr,boolean_t is_router)22270Sstevel@tonic-gate target_create(struct phyint_instance *pii, struct in6_addr addr,
22280Sstevel@tonic-gate     boolean_t is_router)
22290Sstevel@tonic-gate {
22300Sstevel@tonic-gate 	struct target *tg;
22310Sstevel@tonic-gate 	struct phyint *pi;
22320Sstevel@tonic-gate 	struct logint *li;
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate 	if (debug & D_TARGET) {
22350Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 		logdebug("target_create(%s %s, %s)\n",
22380Sstevel@tonic-gate 		    AF_STR(pii->pii_af), pii->pii_name,
22390Sstevel@tonic-gate 		    pr_addr(pii->pii_af, addr, abuf, sizeof (abuf)));
22400Sstevel@tonic-gate 	}
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate 	/*
22430Sstevel@tonic-gate 	 * If the test address is not yet initialized, do not add
22440Sstevel@tonic-gate 	 * any target, since we cannot determine whether the target
22450Sstevel@tonic-gate 	 * belongs to the same subnet as the test address.
22460Sstevel@tonic-gate 	 */
22470Sstevel@tonic-gate 	li = pii->pii_probe_logint;
22480Sstevel@tonic-gate 	if (li == NULL)
22490Sstevel@tonic-gate 		return;
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 	/*
22520Sstevel@tonic-gate 	 * If there are multiple subnets associated with an interface, then
22538485SPeter.Memishian@Sun.COM 	 * add the target to this phyint instance only if it belongs to the
22548485SPeter.Memishian@Sun.COM 	 * same subnet as the test address.  This assures us that we will
22558485SPeter.Memishian@Sun.COM 	 * be able to reach this target through our routing table.
22560Sstevel@tonic-gate 	 */
22570Sstevel@tonic-gate 	if (!prefix_equal(li->li_subnet, addr, li->li_subnet_len))
22580Sstevel@tonic-gate 		return;
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 	if (pii->pii_targets != NULL) {
22610Sstevel@tonic-gate 		assert(pii->pii_ntargets <= MAX_PROBE_TARGETS);
22620Sstevel@tonic-gate 		if (is_router) {
22630Sstevel@tonic-gate 			if (!pii->pii_targets_are_routers) {
22640Sstevel@tonic-gate 				/*
22650Sstevel@tonic-gate 				 * Prefer router over hosts. Using hosts is a
22660Sstevel@tonic-gate 				 * fallback mechanism, hence delete all host
22670Sstevel@tonic-gate 				 * targets.
22680Sstevel@tonic-gate 				 */
22690Sstevel@tonic-gate 				while (pii->pii_targets != NULL)
22700Sstevel@tonic-gate 					target_delete(pii->pii_targets);
22710Sstevel@tonic-gate 			}
22720Sstevel@tonic-gate 		} else {
22730Sstevel@tonic-gate 			/*
22740Sstevel@tonic-gate 			 * Routers take precedence over hosts. If this
22750Sstevel@tonic-gate 			 * is a router list and we are trying to add a
22760Sstevel@tonic-gate 			 * host, just return. If this is a host list
22770Sstevel@tonic-gate 			 * and if we have sufficient targets, just return
22780Sstevel@tonic-gate 			 */
22790Sstevel@tonic-gate 			if (pii->pii_targets_are_routers ||
22800Sstevel@tonic-gate 			    pii->pii_ntargets == MAX_PROBE_TARGETS)
22810Sstevel@tonic-gate 				return;
22820Sstevel@tonic-gate 		}
22830Sstevel@tonic-gate 	}
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 	tg = calloc(1, sizeof (struct target));
22860Sstevel@tonic-gate 	if (tg == NULL) {
22870Sstevel@tonic-gate 		logperror("target_create: calloc");
22880Sstevel@tonic-gate 		return;
22890Sstevel@tonic-gate 	}
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 	tg->tg_phyint_inst = pii;
22920Sstevel@tonic-gate 	tg->tg_address = addr;
22930Sstevel@tonic-gate 	tg->tg_in_use = 1;
22940Sstevel@tonic-gate 	tg->tg_rtt_sa = -1;
22950Sstevel@tonic-gate 	tg->tg_num_deferred = 0;
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 	/*
22980Sstevel@tonic-gate 	 * If this is the first target, set 'pii_targets_are_routers'
22990Sstevel@tonic-gate 	 * The list of targets is either a list of hosts or list or
23000Sstevel@tonic-gate 	 * routers, but not a mix.
23010Sstevel@tonic-gate 	 */
23020Sstevel@tonic-gate 	if (pii->pii_targets == NULL) {
23030Sstevel@tonic-gate 		assert(pii->pii_ntargets == 0);
23040Sstevel@tonic-gate 		assert(pii->pii_target_next == NULL);
23050Sstevel@tonic-gate 		assert(pii->pii_rtt_target_next == NULL);
23060Sstevel@tonic-gate 		pii->pii_targets_are_routers = is_router ? 1 : 0;
23070Sstevel@tonic-gate 	}
23080Sstevel@tonic-gate 
23090Sstevel@tonic-gate 	if (pii->pii_ntargets == MAX_PROBE_TARGETS) {
23100Sstevel@tonic-gate 		assert(pii->pii_targets_are_routers);
23110Sstevel@tonic-gate 		assert(pii->pii_target_next != NULL);
23120Sstevel@tonic-gate 		assert(pii->pii_rtt_target_next != NULL);
23130Sstevel@tonic-gate 		tg->tg_status = TG_UNUSED;
23140Sstevel@tonic-gate 	} else {
23150Sstevel@tonic-gate 		if (pii->pii_ntargets == 0) {
23160Sstevel@tonic-gate 			assert(pii->pii_target_next == NULL);
23170Sstevel@tonic-gate 			pii->pii_target_next = tg;
23180Sstevel@tonic-gate 			pii->pii_rtt_target_next = tg;
23190Sstevel@tonic-gate 		}
23200Sstevel@tonic-gate 		pii->pii_ntargets++;
23210Sstevel@tonic-gate 		tg->tg_status = TG_ACTIVE;
23220Sstevel@tonic-gate 	}
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate 	target_insert(pii, tg);
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate 	/*
23272074Smeem 	 * Change state to PI_RUNNING if this phyint instance is capable of
23282074Smeem 	 * sending and receiving probes -- that is, if we know of at least 1
23292074Smeem 	 * target, and this phyint instance is probe-capable.  For more
23302074Smeem 	 * details, see the phyint state diagram in mpd_probe.c.
23310Sstevel@tonic-gate 	 */
23320Sstevel@tonic-gate 	pi = pii->pii_phyint;
23330Sstevel@tonic-gate 	if (pi->pi_state == PI_NOTARGETS && PROBE_CAPABLE(pii)) {
23340Sstevel@tonic-gate 		if (pi->pi_flags & IFF_FAILED)
23350Sstevel@tonic-gate 			phyint_chstate(pi, PI_FAILED);
23360Sstevel@tonic-gate 		else
23370Sstevel@tonic-gate 			phyint_chstate(pi, PI_RUNNING);
23380Sstevel@tonic-gate 	}
23390Sstevel@tonic-gate }
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate /*
23420Sstevel@tonic-gate  * Add the target address named by `addr' to phyint instance `pii' if it does
23430Sstevel@tonic-gate  * not already exist.  If the target is a router, `is_router' should be set to
23440Sstevel@tonic-gate  * B_TRUE.
23450Sstevel@tonic-gate  */
23460Sstevel@tonic-gate void
target_add(struct phyint_instance * pii,struct in6_addr addr,boolean_t is_router)23470Sstevel@tonic-gate target_add(struct phyint_instance *pii, struct in6_addr addr,
23480Sstevel@tonic-gate     boolean_t is_router)
23490Sstevel@tonic-gate {
23500Sstevel@tonic-gate 	struct target *tg;
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate 	if (pii == NULL)
23530Sstevel@tonic-gate 		return;
23540Sstevel@tonic-gate 
23550Sstevel@tonic-gate 	tg = target_lookup(pii, addr);
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate 	/*
23580Sstevel@tonic-gate 	 * If the target does not exist, create it; target_create() will set
23598485SPeter.Memishian@Sun.COM 	 * tg_in_use to true.  Even if it exists already, if it's a router
23608485SPeter.Memishian@Sun.COM 	 * target and we'd previously learned of it through multicast, then we
23618485SPeter.Memishian@Sun.COM 	 * need to recreate it as a router target.  Otherwise, just set
23628485SPeter.Memishian@Sun.COM 	 * tg_in_use to to true so that init_router_targets() won't delete it.
23630Sstevel@tonic-gate 	 */
23648485SPeter.Memishian@Sun.COM 	if (tg == NULL || (is_router && !pii->pii_targets_are_routers))
23650Sstevel@tonic-gate 		target_create(pii, addr, is_router);
23660Sstevel@tonic-gate 	else if (is_router)
23670Sstevel@tonic-gate 		tg->tg_in_use = 1;
23680Sstevel@tonic-gate }
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate /*
23710Sstevel@tonic-gate  * Insert target at head of linked list of targets for the associated
23720Sstevel@tonic-gate  * phyint instance
23730Sstevel@tonic-gate  */
23740Sstevel@tonic-gate static void
target_insert(struct phyint_instance * pii,struct target * tg)23750Sstevel@tonic-gate target_insert(struct phyint_instance *pii, struct target *tg)
23760Sstevel@tonic-gate {
23770Sstevel@tonic-gate 	tg->tg_next = pii->pii_targets;
23780Sstevel@tonic-gate 	tg->tg_prev = NULL;
23790Sstevel@tonic-gate 	if (tg->tg_next != NULL)
23800Sstevel@tonic-gate 		tg->tg_next->tg_prev = tg;
23810Sstevel@tonic-gate 	pii->pii_targets = tg;
23820Sstevel@tonic-gate }
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate /*
23850Sstevel@tonic-gate  * Delete a target (unlink and free).
23860Sstevel@tonic-gate  */
23870Sstevel@tonic-gate void
target_delete(struct target * tg)23880Sstevel@tonic-gate target_delete(struct target *tg)
23890Sstevel@tonic-gate {
23900Sstevel@tonic-gate 	int af;
23910Sstevel@tonic-gate 	struct phyint_instance	*pii;
23920Sstevel@tonic-gate 	struct phyint_instance	*pii_other;
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	pii = tg->tg_phyint_inst;
23950Sstevel@tonic-gate 	af = pii->pii_af;
23960Sstevel@tonic-gate 
23970Sstevel@tonic-gate 	if (debug & D_TARGET) {
23980Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
23990Sstevel@tonic-gate 
24000Sstevel@tonic-gate 		logdebug("target_delete(%s %s, %s)\n",
24010Sstevel@tonic-gate 		    AF_STR(af), pii->pii_name,
24020Sstevel@tonic-gate 		    pr_addr(af, tg->tg_address, abuf, sizeof (abuf)));
24030Sstevel@tonic-gate 	}
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 	/*
24060Sstevel@tonic-gate 	 * Target must be in the list of targets for this phyint
24070Sstevel@tonic-gate 	 * instance.
24080Sstevel@tonic-gate 	 */
24090Sstevel@tonic-gate 	assert(pii->pii_targets == tg || tg->tg_prev != NULL);
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate 	/*
24120Sstevel@tonic-gate 	 * Reset all references to 'tg' in the probe information
24130Sstevel@tonic-gate 	 * for this phyint.
24140Sstevel@tonic-gate 	 */
24150Sstevel@tonic-gate 	reset_pii_probes(pii, tg);
24160Sstevel@tonic-gate 
24170Sstevel@tonic-gate 	/*
24180Sstevel@tonic-gate 	 * Remove this target from the list of targets of this
24190Sstevel@tonic-gate 	 * phyint instance.
24200Sstevel@tonic-gate 	 */
24210Sstevel@tonic-gate 	if (tg->tg_prev == NULL) {
24220Sstevel@tonic-gate 		pii->pii_targets = tg->tg_next;
24230Sstevel@tonic-gate 	} else {
24240Sstevel@tonic-gate 		tg->tg_prev->tg_next = tg->tg_next;
24250Sstevel@tonic-gate 	}
24260Sstevel@tonic-gate 
24270Sstevel@tonic-gate 	if (tg->tg_next != NULL)
24280Sstevel@tonic-gate 		tg->tg_next->tg_prev = tg->tg_prev;
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate 	tg->tg_next = NULL;
24310Sstevel@tonic-gate 	tg->tg_prev = NULL;
24320Sstevel@tonic-gate 
24330Sstevel@tonic-gate 	if (tg->tg_status == TG_ACTIVE)
24340Sstevel@tonic-gate 		pii->pii_ntargets--;
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 	/*
24370Sstevel@tonic-gate 	 * Adjust the next target to probe, if it points to
24380Sstevel@tonic-gate 	 * to the currently deleted target.
24390Sstevel@tonic-gate 	 */
24400Sstevel@tonic-gate 	if (pii->pii_target_next == tg)
24410Sstevel@tonic-gate 		pii->pii_target_next = target_first(pii);
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 	if (pii->pii_rtt_target_next == tg)
24440Sstevel@tonic-gate 		pii->pii_rtt_target_next = target_first(pii);
24450Sstevel@tonic-gate 
24460Sstevel@tonic-gate 	free(tg);
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 	/*
24490Sstevel@tonic-gate 	 * The number of active targets pii_ntargets == 0 iff
24500Sstevel@tonic-gate 	 * the next active target pii->pii_target_next == NULL
24510Sstevel@tonic-gate 	 */
24520Sstevel@tonic-gate 	if (pii->pii_ntargets != 0) {
24530Sstevel@tonic-gate 		assert(pii->pii_target_next != NULL);
24540Sstevel@tonic-gate 		assert(pii->pii_rtt_target_next != NULL);
24550Sstevel@tonic-gate 		assert(pii->pii_target_next->tg_status == TG_ACTIVE);
24560Sstevel@tonic-gate 		assert(pii->pii_rtt_target_next->tg_status == TG_ACTIVE);
24570Sstevel@tonic-gate 		return;
24580Sstevel@tonic-gate 	}
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate 	/* At this point, we don't have any active targets. */
24610Sstevel@tonic-gate 	assert(pii->pii_target_next == NULL);
24620Sstevel@tonic-gate 	assert(pii->pii_rtt_target_next == NULL);
24630Sstevel@tonic-gate 
24640Sstevel@tonic-gate 	if (pii->pii_targets_are_routers) {
24650Sstevel@tonic-gate 		/*
24660Sstevel@tonic-gate 		 * Activate any TG_SLOW or TG_DEAD router targets,
24670Sstevel@tonic-gate 		 * since we don't have any other targets
24680Sstevel@tonic-gate 		 */
24690Sstevel@tonic-gate 		target_activate_all(pii);
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate 		if (pii->pii_ntargets != 0) {
24720Sstevel@tonic-gate 			assert(pii->pii_target_next != NULL);
24730Sstevel@tonic-gate 			assert(pii->pii_rtt_target_next != NULL);
24740Sstevel@tonic-gate 			assert(pii->pii_target_next->tg_status == TG_ACTIVE);
24750Sstevel@tonic-gate 			assert(pii->pii_rtt_target_next->tg_status ==
24760Sstevel@tonic-gate 			    TG_ACTIVE);
24770Sstevel@tonic-gate 			return;
24780Sstevel@tonic-gate 		}
24790Sstevel@tonic-gate 	}
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 	/*
24820Sstevel@tonic-gate 	 * If we still don't have any active targets, the list must
24830Sstevel@tonic-gate 	 * must be really empty. There aren't even TG_SLOW or TG_DEAD
24840Sstevel@tonic-gate 	 * targets. Zero out the probe stats since it will not be
24850Sstevel@tonic-gate 	 * relevant any longer.
24860Sstevel@tonic-gate 	 */
24870Sstevel@tonic-gate 	assert(pii->pii_targets == NULL);
24888485SPeter.Memishian@Sun.COM 	pii->pii_targets_are_routers = _B_FALSE;
24890Sstevel@tonic-gate 	clear_pii_probe_stats(pii);
24900Sstevel@tonic-gate 	pii_other = phyint_inst_other(pii);
24910Sstevel@tonic-gate 
24920Sstevel@tonic-gate 	/*
24938485SPeter.Memishian@Sun.COM 	 * If there are no targets on both instances and the interface would
24948485SPeter.Memishian@Sun.COM 	 * otherwise be considered PI_RUNNING, go back to PI_NOTARGETS state,
24958485SPeter.Memishian@Sun.COM 	 * since we cannot probe this phyint any more.  For more details,
24968485SPeter.Memishian@Sun.COM 	 * please see phyint state diagram in mpd_probe.c.
24970Sstevel@tonic-gate 	 */
24988485SPeter.Memishian@Sun.COM 	if (!PROBE_CAPABLE(pii_other) && LINK_UP(pii->pii_phyint) &&
24994770Smeem 	    pii->pii_phyint->pi_state != PI_OFFLINE)
25000Sstevel@tonic-gate 		phyint_chstate(pii->pii_phyint, PI_NOTARGETS);
25010Sstevel@tonic-gate }
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate /*
25040Sstevel@tonic-gate  * Flush the target list of every phyint in the group, if the list
25050Sstevel@tonic-gate  * is a host target list. This is called if group failure is suspected.
25060Sstevel@tonic-gate  * If all targets have failed, multicast will subsequently discover new
25070Sstevel@tonic-gate  * targets. Else it is a group failure.
25080Sstevel@tonic-gate  * Note: This function is a no-op if the list is a router target list.
25090Sstevel@tonic-gate  */
25100Sstevel@tonic-gate static void
target_flush_hosts(struct phyint_group * pg)25110Sstevel@tonic-gate target_flush_hosts(struct phyint_group *pg)
25120Sstevel@tonic-gate {
25130Sstevel@tonic-gate 	struct phyint *pi;
25140Sstevel@tonic-gate 	struct phyint_instance *pii;
25150Sstevel@tonic-gate 
25160Sstevel@tonic-gate 	if (debug & D_TARGET)
25170Sstevel@tonic-gate 		logdebug("target_flush_hosts(%s)\n", pg->pg_name);
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 	for (pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext) {
25200Sstevel@tonic-gate 		pii = pi->pi_v4;
25210Sstevel@tonic-gate 		if (pii != NULL && !pii->pii_targets_are_routers) {
25220Sstevel@tonic-gate 			/*
25230Sstevel@tonic-gate 			 * Delete all the targets. When the list becomes
25240Sstevel@tonic-gate 			 * empty, target_delete() will set pii->pii_targets
25250Sstevel@tonic-gate 			 * to NULL.
25260Sstevel@tonic-gate 			 */
25270Sstevel@tonic-gate 			while (pii->pii_targets != NULL)
25280Sstevel@tonic-gate 				target_delete(pii->pii_targets);
25290Sstevel@tonic-gate 		}
25300Sstevel@tonic-gate 		pii = pi->pi_v6;
25310Sstevel@tonic-gate 		if (pii != NULL && !pii->pii_targets_are_routers) {
25320Sstevel@tonic-gate 			/*
25330Sstevel@tonic-gate 			 * Delete all the targets. When the list becomes
25340Sstevel@tonic-gate 			 * empty, target_delete() will set pii->pii_targets
25350Sstevel@tonic-gate 			 * to NULL.
25360Sstevel@tonic-gate 			 */
25370Sstevel@tonic-gate 			while (pii->pii_targets != NULL)
25380Sstevel@tonic-gate 				target_delete(pii->pii_targets);
25390Sstevel@tonic-gate 		}
25400Sstevel@tonic-gate 	}
25410Sstevel@tonic-gate }
25420Sstevel@tonic-gate 
25430Sstevel@tonic-gate /*
25440Sstevel@tonic-gate  * Reset all references to 'target' in the probe info, as this target is
25450Sstevel@tonic-gate  * being deleted. The pr_target field is guaranteed to be non-null if
25460Sstevel@tonic-gate  * pr_status is PR_UNACKED. So we change the pr_status to PR_LOST, so that
25470Sstevel@tonic-gate  * pr_target will not be accessed unconditionally.
25480Sstevel@tonic-gate  */
25490Sstevel@tonic-gate static void
reset_pii_probes(struct phyint_instance * pii,struct target * tg)25500Sstevel@tonic-gate reset_pii_probes(struct phyint_instance *pii, struct target *tg)
25510Sstevel@tonic-gate {
25520Sstevel@tonic-gate 	int i;
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate 	for (i = 0; i < PROBE_STATS_COUNT; i++) {
25550Sstevel@tonic-gate 		if (pii->pii_probes[i].pr_target == tg) {
25568485SPeter.Memishian@Sun.COM 			if (pii->pii_probes[i].pr_status == PR_UNACKED) {
25578485SPeter.Memishian@Sun.COM 				probe_chstate(&pii->pii_probes[i], pii,
25588485SPeter.Memishian@Sun.COM 				    PR_LOST);
25598485SPeter.Memishian@Sun.COM 			}
25600Sstevel@tonic-gate 			pii->pii_probes[i].pr_target = NULL;
25610Sstevel@tonic-gate 		}
25620Sstevel@tonic-gate 	}
25630Sstevel@tonic-gate }
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate /*
25660Sstevel@tonic-gate  * Clear the probe statistics array.
25670Sstevel@tonic-gate  */
25680Sstevel@tonic-gate void
clear_pii_probe_stats(struct phyint_instance * pii)25690Sstevel@tonic-gate clear_pii_probe_stats(struct phyint_instance *pii)
25700Sstevel@tonic-gate {
25710Sstevel@tonic-gate 	bzero(pii->pii_probes, sizeof (struct probe_stats) * PROBE_STATS_COUNT);
25720Sstevel@tonic-gate 	/* Reset the next probe index in the probe stats array */
25730Sstevel@tonic-gate 	pii->pii_probe_next = 0;
25740Sstevel@tonic-gate }
25750Sstevel@tonic-gate 
25760Sstevel@tonic-gate static void
target_print(struct target * tg)25770Sstevel@tonic-gate target_print(struct target *tg)
25780Sstevel@tonic-gate {
25790Sstevel@tonic-gate 	char	abuf[INET6_ADDRSTRLEN];
25800Sstevel@tonic-gate 	char	buf[128];
25810Sstevel@tonic-gate 	char	buf2[128];
25820Sstevel@tonic-gate 	int	af;
25830Sstevel@tonic-gate 	int	i;
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 	af = tg->tg_phyint_inst->pii_af;
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate 	logdebug("Target on %s %s addr %s\n"
25888485SPeter.Memishian@Sun.COM 	    "status %d rtt_sa %lld rtt_sd %lld crtt %d tg_in_use %d\n",
25890Sstevel@tonic-gate 	    AF_STR(af), tg->tg_phyint_inst->pii_name,
25900Sstevel@tonic-gate 	    pr_addr(af, tg->tg_address, abuf, sizeof (abuf)),
25910Sstevel@tonic-gate 	    tg->tg_status, tg->tg_rtt_sa, tg->tg_rtt_sd,
25920Sstevel@tonic-gate 	    tg->tg_crtt, tg->tg_in_use);
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate 	buf[0] = '\0';
25950Sstevel@tonic-gate 	for (i = 0; i < tg->tg_num_deferred; i++) {
25960Sstevel@tonic-gate 		(void) snprintf(buf2, sizeof (buf2), " %dms",
25970Sstevel@tonic-gate 		    tg->tg_deferred[i]);
25980Sstevel@tonic-gate 		(void) strlcat(buf, buf2, sizeof (buf));
25990Sstevel@tonic-gate 	}
26000Sstevel@tonic-gate 	logdebug("deferred rtts:%s\n", buf);
26010Sstevel@tonic-gate }
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate void
phyint_inst_print_all(void)26040Sstevel@tonic-gate phyint_inst_print_all(void)
26050Sstevel@tonic-gate {
26060Sstevel@tonic-gate 	struct phyint_instance *pii;
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate 	for (pii = phyint_instances; pii != NULL; pii = pii->pii_next) {
26090Sstevel@tonic-gate 		phyint_inst_print(pii);
26100Sstevel@tonic-gate 	}
26110Sstevel@tonic-gate }
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate /*
26140Sstevel@tonic-gate  * Compare two prefixes that have the same prefix length.
26150Sstevel@tonic-gate  * Fails if the prefix length is unreasonable.
26160Sstevel@tonic-gate  */
26178485SPeter.Memishian@Sun.COM boolean_t
prefix_equal(struct in6_addr p1,struct in6_addr p2,uint_t prefix_len)26188485SPeter.Memishian@Sun.COM prefix_equal(struct in6_addr p1, struct in6_addr p2, uint_t prefix_len)
26190Sstevel@tonic-gate {
26200Sstevel@tonic-gate 	uchar_t mask;
26210Sstevel@tonic-gate 	int j;
26220Sstevel@tonic-gate 
26238485SPeter.Memishian@Sun.COM 	if (prefix_len > IPV6_ABITS)
26240Sstevel@tonic-gate 		return (_B_FALSE);
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate 	for (j = 0; prefix_len > 8; prefix_len -= 8, j++)
26270Sstevel@tonic-gate 		if (p1.s6_addr[j] != p2.s6_addr[j])
26280Sstevel@tonic-gate 			return (_B_FALSE);
26290Sstevel@tonic-gate 
26300Sstevel@tonic-gate 	/* Make the N leftmost bits one */
26310Sstevel@tonic-gate 	mask = 0xff << (8 - prefix_len);
26320Sstevel@tonic-gate 	if ((p1.s6_addr[j] & mask) != (p2.s6_addr[j] & mask))
26330Sstevel@tonic-gate 		return (_B_FALSE);
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 	return (_B_TRUE);
26360Sstevel@tonic-gate }
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate /*
26398485SPeter.Memishian@Sun.COM  * Get the number of UP logints on phyint `pi'.
26400Sstevel@tonic-gate  */
26418485SPeter.Memishian@Sun.COM static int
logint_upcount(struct phyint * pi)26420Sstevel@tonic-gate logint_upcount(struct phyint *pi)
26430Sstevel@tonic-gate {
26440Sstevel@tonic-gate 	struct	logint	*li;
26450Sstevel@tonic-gate 	int count = 0;
26460Sstevel@tonic-gate 
26478485SPeter.Memishian@Sun.COM 	if (pi->pi_v4 != NULL) {
26488485SPeter.Memishian@Sun.COM 		for (li = pi->pi_v4->pii_logint; li != NULL; li = li->li_next) {
26498485SPeter.Memishian@Sun.COM 			if (li->li_flags & IFF_UP)
26500Sstevel@tonic-gate 				count++;
26510Sstevel@tonic-gate 		}
26520Sstevel@tonic-gate 	}
26530Sstevel@tonic-gate 
26548485SPeter.Memishian@Sun.COM 	if (pi->pi_v6 != NULL) {
26558485SPeter.Memishian@Sun.COM 		for (li = pi->pi_v6->pii_logint; li != NULL; li = li->li_next) {
26568485SPeter.Memishian@Sun.COM 			if (li->li_flags & IFF_UP)
26570Sstevel@tonic-gate 				count++;
26580Sstevel@tonic-gate 		}
26590Sstevel@tonic-gate 	}
26600Sstevel@tonic-gate 
26610Sstevel@tonic-gate 	return (count);
26620Sstevel@tonic-gate }
26630Sstevel@tonic-gate 
26640Sstevel@tonic-gate /*
26650Sstevel@tonic-gate  * Get the phyint instance with the other (IPv4 / IPv6) protocol
26660Sstevel@tonic-gate  */
26670Sstevel@tonic-gate struct phyint_instance *
phyint_inst_other(struct phyint_instance * pii)26680Sstevel@tonic-gate phyint_inst_other(struct phyint_instance *pii)
26690Sstevel@tonic-gate {
26700Sstevel@tonic-gate 	if (pii->pii_af == AF_INET)
26710Sstevel@tonic-gate 		return (pii->pii_phyint->pi_v6);
26720Sstevel@tonic-gate 	else
26730Sstevel@tonic-gate 		return (pii->pii_phyint->pi_v4);
26740Sstevel@tonic-gate }
26750Sstevel@tonic-gate 
26760Sstevel@tonic-gate /*
26778485SPeter.Memishian@Sun.COM  * Check whether a phyint is functioning.
26788485SPeter.Memishian@Sun.COM  */
2679*10649SPeter.Memishian@Sun.COM boolean_t
phyint_is_functioning(struct phyint * pi)26808485SPeter.Memishian@Sun.COM phyint_is_functioning(struct phyint *pi)
26818485SPeter.Memishian@Sun.COM {
26828485SPeter.Memishian@Sun.COM 	if (pi->pi_state == PI_RUNNING)
26838485SPeter.Memishian@Sun.COM 		return (_B_TRUE);
26848485SPeter.Memishian@Sun.COM 	return (pi->pi_state == PI_NOTARGETS && !(pi->pi_flags & IFF_FAILED));
26858485SPeter.Memishian@Sun.COM }
26868485SPeter.Memishian@Sun.COM 
26878485SPeter.Memishian@Sun.COM /*
26888485SPeter.Memishian@Sun.COM  * Check whether a phyint is usable.
26898485SPeter.Memishian@Sun.COM  */
2690*10649SPeter.Memishian@Sun.COM boolean_t
phyint_is_usable(struct phyint * pi)26918485SPeter.Memishian@Sun.COM phyint_is_usable(struct phyint *pi)
26928485SPeter.Memishian@Sun.COM {
26938485SPeter.Memishian@Sun.COM 	if (logint_upcount(pi) == 0)
26948485SPeter.Memishian@Sun.COM 		return (_B_FALSE);
26958485SPeter.Memishian@Sun.COM 	return (phyint_is_functioning(pi));
26968485SPeter.Memishian@Sun.COM }
26978485SPeter.Memishian@Sun.COM 
26988485SPeter.Memishian@Sun.COM /*
26990Sstevel@tonic-gate  * Post an EC_IPMP sysevent of subclass `subclass' and attributes `nvl'.
27000Sstevel@tonic-gate  * Before sending the event, it prepends the current version of the IPMP
27010Sstevel@tonic-gate  * sysevent API.  Returns 0 on success, -1 on failure (in either case,
27020Sstevel@tonic-gate  * `nvl' is freed).
27030Sstevel@tonic-gate  */
27040Sstevel@tonic-gate static int
post_event(const char * subclass,nvlist_t * nvl)27050Sstevel@tonic-gate post_event(const char *subclass, nvlist_t *nvl)
27060Sstevel@tonic-gate {
27078485SPeter.Memishian@Sun.COM 	static evchan_t *evchp = NULL;
27080Sstevel@tonic-gate 
27094262Smeem 	/*
27108485SPeter.Memishian@Sun.COM 	 * Initialize the event channel if we haven't already done so.
27114262Smeem 	 */
27128485SPeter.Memishian@Sun.COM 	if (evchp == NULL) {
27138485SPeter.Memishian@Sun.COM 		errno = sysevent_evc_bind(IPMP_EVENT_CHAN, &evchp, EVCH_CREAT);
27148485SPeter.Memishian@Sun.COM 		if (errno != 0) {
27158485SPeter.Memishian@Sun.COM 			logerr("cannot create event channel `%s': %s\n",
27168485SPeter.Memishian@Sun.COM 			    IPMP_EVENT_CHAN, strerror(errno));
27178485SPeter.Memishian@Sun.COM 			goto failed;
27188485SPeter.Memishian@Sun.COM 		}
27194262Smeem 	}
27204262Smeem 
27210Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_EVENT_VERSION,
27220Sstevel@tonic-gate 	    IPMP_EVENT_CUR_VERSION);
27230Sstevel@tonic-gate 	if (errno != 0) {
27240Sstevel@tonic-gate 		logerr("cannot create `%s' event: %s", subclass,
27250Sstevel@tonic-gate 		    strerror(errno));
27260Sstevel@tonic-gate 		goto failed;
27270Sstevel@tonic-gate 	}
27280Sstevel@tonic-gate 
27298485SPeter.Memishian@Sun.COM 	errno = sysevent_evc_publish(evchp, EC_IPMP, subclass, "com.sun",
27308485SPeter.Memishian@Sun.COM 	    "in.mpathd", nvl, EVCH_NOSLEEP);
27318485SPeter.Memishian@Sun.COM 	if (errno != 0) {
27320Sstevel@tonic-gate 		logerr("cannot send `%s' event: %s\n", subclass,
27330Sstevel@tonic-gate 		    strerror(errno));
27340Sstevel@tonic-gate 		goto failed;
27350Sstevel@tonic-gate 	}
27360Sstevel@tonic-gate 
27370Sstevel@tonic-gate 	nvlist_free(nvl);
27380Sstevel@tonic-gate 	return (0);
27390Sstevel@tonic-gate failed:
27400Sstevel@tonic-gate 	nvlist_free(nvl);
27410Sstevel@tonic-gate 	return (-1);
27420Sstevel@tonic-gate }
27430Sstevel@tonic-gate 
27440Sstevel@tonic-gate /*
27450Sstevel@tonic-gate  * Return the external IPMP state associated with phyint `pi'.
27460Sstevel@tonic-gate  */
27470Sstevel@tonic-gate static ipmp_if_state_t
ifstate(struct phyint * pi)27480Sstevel@tonic-gate ifstate(struct phyint *pi)
27490Sstevel@tonic-gate {
27500Sstevel@tonic-gate 	switch (pi->pi_state) {
275110290SPeter.Memishian@Sun.COM 	case PI_INIT:
275210290SPeter.Memishian@Sun.COM 		return (IPMP_IF_UNKNOWN);
275310290SPeter.Memishian@Sun.COM 
27540Sstevel@tonic-gate 	case PI_NOTARGETS:
27558485SPeter.Memishian@Sun.COM 		if (pi->pi_flags & IFF_FAILED)
27568485SPeter.Memishian@Sun.COM 			return (IPMP_IF_FAILED);
27570Sstevel@tonic-gate 		return (IPMP_IF_UNKNOWN);
27580Sstevel@tonic-gate 
27590Sstevel@tonic-gate 	case PI_OFFLINE:
27600Sstevel@tonic-gate 		return (IPMP_IF_OFFLINE);
27610Sstevel@tonic-gate 
27620Sstevel@tonic-gate 	case PI_FAILED:
27630Sstevel@tonic-gate 		return (IPMP_IF_FAILED);
27640Sstevel@tonic-gate 
27650Sstevel@tonic-gate 	case PI_RUNNING:
27660Sstevel@tonic-gate 		return (IPMP_IF_OK);
27670Sstevel@tonic-gate 	}
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate 	logerr("ifstate: unknown state %d; aborting\n", pi->pi_state);
27700Sstevel@tonic-gate 	abort();
27710Sstevel@tonic-gate 	/* NOTREACHED */
27720Sstevel@tonic-gate }
27730Sstevel@tonic-gate 
27740Sstevel@tonic-gate /*
27750Sstevel@tonic-gate  * Return the external IPMP interface type associated with phyint `pi'.
27760Sstevel@tonic-gate  */
27770Sstevel@tonic-gate static ipmp_if_type_t
iftype(struct phyint * pi)27780Sstevel@tonic-gate iftype(struct phyint *pi)
27790Sstevel@tonic-gate {
27800Sstevel@tonic-gate 	if (pi->pi_flags & IFF_STANDBY)
27810Sstevel@tonic-gate 		return (IPMP_IF_STANDBY);
27820Sstevel@tonic-gate 	else
27830Sstevel@tonic-gate 		return (IPMP_IF_NORMAL);
27840Sstevel@tonic-gate }
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate /*
27878485SPeter.Memishian@Sun.COM  * Return the external IPMP link state associated with phyint `pi'.
27888485SPeter.Memishian@Sun.COM  */
27898485SPeter.Memishian@Sun.COM static ipmp_if_linkstate_t
iflinkstate(struct phyint * pi)27908485SPeter.Memishian@Sun.COM iflinkstate(struct phyint *pi)
27918485SPeter.Memishian@Sun.COM {
27928485SPeter.Memishian@Sun.COM 	if (!(pi->pi_notes & (DL_NOTE_LINK_UP|DL_NOTE_LINK_DOWN)))
27938485SPeter.Memishian@Sun.COM 		return (IPMP_LINK_UNKNOWN);
27948485SPeter.Memishian@Sun.COM 
27958485SPeter.Memishian@Sun.COM 	return (LINK_DOWN(pi) ? IPMP_LINK_DOWN : IPMP_LINK_UP);
27968485SPeter.Memishian@Sun.COM }
27978485SPeter.Memishian@Sun.COM 
27988485SPeter.Memishian@Sun.COM /*
27998485SPeter.Memishian@Sun.COM  * Return the external IPMP probe state associated with phyint `pi'.
28008485SPeter.Memishian@Sun.COM  */
28018485SPeter.Memishian@Sun.COM static ipmp_if_probestate_t
ifprobestate(struct phyint * pi)28028485SPeter.Memishian@Sun.COM ifprobestate(struct phyint *pi)
28038485SPeter.Memishian@Sun.COM {
28048485SPeter.Memishian@Sun.COM 	if (!PROBE_ENABLED(pi->pi_v4) && !PROBE_ENABLED(pi->pi_v6))
28058485SPeter.Memishian@Sun.COM 		return (IPMP_PROBE_DISABLED);
28068485SPeter.Memishian@Sun.COM 
28078485SPeter.Memishian@Sun.COM 	if (pi->pi_state == PI_FAILED)
28088485SPeter.Memishian@Sun.COM 		return (IPMP_PROBE_FAILED);
28098485SPeter.Memishian@Sun.COM 
28108485SPeter.Memishian@Sun.COM 	if (!PROBE_CAPABLE(pi->pi_v4) && !PROBE_CAPABLE(pi->pi_v6))
28118485SPeter.Memishian@Sun.COM 		return (IPMP_PROBE_UNKNOWN);
28128485SPeter.Memishian@Sun.COM 
28138485SPeter.Memishian@Sun.COM 	return (IPMP_PROBE_OK);
28148485SPeter.Memishian@Sun.COM }
28158485SPeter.Memishian@Sun.COM 
28168485SPeter.Memishian@Sun.COM /*
28178485SPeter.Memishian@Sun.COM  * Return the external IPMP target mode associated with phyint instance `pii'.
28188485SPeter.Memishian@Sun.COM  */
28198485SPeter.Memishian@Sun.COM static ipmp_if_targmode_t
iftargmode(struct phyint_instance * pii)28208485SPeter.Memishian@Sun.COM iftargmode(struct phyint_instance *pii)
28218485SPeter.Memishian@Sun.COM {
28228485SPeter.Memishian@Sun.COM 	if (!PROBE_ENABLED(pii))
28238485SPeter.Memishian@Sun.COM 		return (IPMP_TARG_DISABLED);
28248485SPeter.Memishian@Sun.COM 	else if (pii->pii_targets_are_routers)
28258485SPeter.Memishian@Sun.COM 		return (IPMP_TARG_ROUTES);
28268485SPeter.Memishian@Sun.COM 	else
28278485SPeter.Memishian@Sun.COM 		return (IPMP_TARG_MULTICAST);
28288485SPeter.Memishian@Sun.COM }
28298485SPeter.Memishian@Sun.COM 
28308485SPeter.Memishian@Sun.COM /*
28318485SPeter.Memishian@Sun.COM  * Return the external IPMP flags associated with phyint `pi'.
28328485SPeter.Memishian@Sun.COM  */
28338485SPeter.Memishian@Sun.COM static ipmp_if_flags_t
ifflags(struct phyint * pi)28348485SPeter.Memishian@Sun.COM ifflags(struct phyint *pi)
28358485SPeter.Memishian@Sun.COM {
28368485SPeter.Memishian@Sun.COM 	ipmp_if_flags_t flags = 0;
28378485SPeter.Memishian@Sun.COM 
28388485SPeter.Memishian@Sun.COM 	if (logint_upcount(pi) == 0)
28398485SPeter.Memishian@Sun.COM 		flags |= IPMP_IFFLAG_DOWN;
28408485SPeter.Memishian@Sun.COM 	if (pi->pi_flags & IFF_INACTIVE)
28418485SPeter.Memishian@Sun.COM 		flags |= IPMP_IFFLAG_INACTIVE;
28428485SPeter.Memishian@Sun.COM 	if (pi->pi_hwaddrdup)
28438485SPeter.Memishian@Sun.COM 		flags |= IPMP_IFFLAG_HWADDRDUP;
28448485SPeter.Memishian@Sun.COM 	if (phyint_is_functioning(pi) && flags == 0)
28458485SPeter.Memishian@Sun.COM 		flags |= IPMP_IFFLAG_ACTIVE;
28468485SPeter.Memishian@Sun.COM 
28478485SPeter.Memishian@Sun.COM 	return (flags);
28488485SPeter.Memishian@Sun.COM }
28498485SPeter.Memishian@Sun.COM 
28508485SPeter.Memishian@Sun.COM /*
28518485SPeter.Memishian@Sun.COM  * Store the test address used on phyint instance `pii' in `ssp'.  If there's
28528485SPeter.Memishian@Sun.COM  * no test address, 0.0.0.0 is stored.
28538485SPeter.Memishian@Sun.COM  */
28548485SPeter.Memishian@Sun.COM static struct sockaddr_storage *
iftestaddr(struct phyint_instance * pii,struct sockaddr_storage * ssp)28558485SPeter.Memishian@Sun.COM iftestaddr(struct phyint_instance *pii, struct sockaddr_storage *ssp)
28568485SPeter.Memishian@Sun.COM {
28578485SPeter.Memishian@Sun.COM 	if (PROBE_ENABLED(pii))
28588485SPeter.Memishian@Sun.COM 		addr2storage(pii->pii_af, &pii->pii_probe_logint->li_addr, ssp);
28598485SPeter.Memishian@Sun.COM 	else
28608485SPeter.Memishian@Sun.COM 		addr2storage(AF_INET6, &in6addr_any, ssp);
28618485SPeter.Memishian@Sun.COM 
28628485SPeter.Memishian@Sun.COM 	return (ssp);
28638485SPeter.Memishian@Sun.COM }
28648485SPeter.Memishian@Sun.COM 
28658485SPeter.Memishian@Sun.COM /*
28660Sstevel@tonic-gate  * Return the external IPMP group state associated with phyint group `pg'.
28670Sstevel@tonic-gate  */
28680Sstevel@tonic-gate static ipmp_group_state_t
groupstate(struct phyint_group * pg)28690Sstevel@tonic-gate groupstate(struct phyint_group *pg)
28700Sstevel@tonic-gate {
28718485SPeter.Memishian@Sun.COM 	switch (pg->pg_state) {
28728485SPeter.Memishian@Sun.COM 	case PG_FAILED:
28738485SPeter.Memishian@Sun.COM 		return (IPMP_GROUP_FAILED);
28748485SPeter.Memishian@Sun.COM 	case PG_DEGRADED:
28758485SPeter.Memishian@Sun.COM 		return (IPMP_GROUP_DEGRADED);
28768485SPeter.Memishian@Sun.COM 	case PG_OK:
28778485SPeter.Memishian@Sun.COM 		return (IPMP_GROUP_OK);
28788485SPeter.Memishian@Sun.COM 	}
28798485SPeter.Memishian@Sun.COM 
28808485SPeter.Memishian@Sun.COM 	logerr("groupstate: unknown state %d; aborting\n", pg->pg_state);
28818485SPeter.Memishian@Sun.COM 	abort();
28828485SPeter.Memishian@Sun.COM 	/* NOTREACHED */
28838485SPeter.Memishian@Sun.COM }
28848485SPeter.Memishian@Sun.COM 
28858485SPeter.Memishian@Sun.COM /*
28868485SPeter.Memishian@Sun.COM  * Return the external IPMP probe state associated with probe `ps'.
28878485SPeter.Memishian@Sun.COM  */
28888485SPeter.Memishian@Sun.COM static ipmp_probe_state_t
probestate(struct probe_stats * ps)28898485SPeter.Memishian@Sun.COM probestate(struct probe_stats *ps)
28908485SPeter.Memishian@Sun.COM {
28918485SPeter.Memishian@Sun.COM 	switch (ps->pr_status) {
28928485SPeter.Memishian@Sun.COM 	case PR_UNUSED:
28938485SPeter.Memishian@Sun.COM 	case PR_LOST:
28948485SPeter.Memishian@Sun.COM 		return (IPMP_PROBE_LOST);
28958485SPeter.Memishian@Sun.COM 	case PR_UNACKED:
28968485SPeter.Memishian@Sun.COM 		return (IPMP_PROBE_SENT);
28978485SPeter.Memishian@Sun.COM 	case PR_ACKED:
28988485SPeter.Memishian@Sun.COM 		return (IPMP_PROBE_ACKED);
28998485SPeter.Memishian@Sun.COM 	}
29008485SPeter.Memishian@Sun.COM 
29018485SPeter.Memishian@Sun.COM 	logerr("probestate: unknown state %d; aborting\n", ps->pr_status);
29028485SPeter.Memishian@Sun.COM 	abort();
29038485SPeter.Memishian@Sun.COM 	/* NOTREACHED */
29048485SPeter.Memishian@Sun.COM }
29058485SPeter.Memishian@Sun.COM 
29068485SPeter.Memishian@Sun.COM /*
29078485SPeter.Memishian@Sun.COM  * Generate an ESC_IPMP_PROBE_STATE sysevent for the probe described by `pr'
29088485SPeter.Memishian@Sun.COM  * on phyint instance `pii'.  Returns 0 on success, -1 on failure.
29098485SPeter.Memishian@Sun.COM  */
29108485SPeter.Memishian@Sun.COM int
probe_state_event(struct probe_stats * pr,struct phyint_instance * pii)29118485SPeter.Memishian@Sun.COM probe_state_event(struct probe_stats *pr, struct phyint_instance *pii)
29128485SPeter.Memishian@Sun.COM {
29138485SPeter.Memishian@Sun.COM 	nvlist_t *nvl;
29148485SPeter.Memishian@Sun.COM 	hrtime_t proc_time = 0, recv_time = 0;
29158485SPeter.Memishian@Sun.COM 	struct sockaddr_storage ss;
29168485SPeter.Memishian@Sun.COM 	struct target *tg = pr->pr_target;
29179416SPeter.Memishian@Sun.COM 	int64_t rttavg, rttdev;
29188485SPeter.Memishian@Sun.COM 
29198485SPeter.Memishian@Sun.COM 	errno = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
29208485SPeter.Memishian@Sun.COM 	if (errno != 0) {
29218485SPeter.Memishian@Sun.COM 		logperror("cannot create `interface change' event");
29228485SPeter.Memishian@Sun.COM 		return (-1);
29238485SPeter.Memishian@Sun.COM 	}
29248485SPeter.Memishian@Sun.COM 
29258485SPeter.Memishian@Sun.COM 	errno = nvlist_add_uint32(nvl, IPMP_PROBE_ID, pr->pr_id);
29268485SPeter.Memishian@Sun.COM 	if (errno != 0)
29278485SPeter.Memishian@Sun.COM 		goto failed;
29288485SPeter.Memishian@Sun.COM 
29298485SPeter.Memishian@Sun.COM 	errno = nvlist_add_string(nvl, IPMP_IF_NAME, pii->pii_phyint->pi_name);
29308485SPeter.Memishian@Sun.COM 	if (errno != 0)
29318485SPeter.Memishian@Sun.COM 		goto failed;
29328485SPeter.Memishian@Sun.COM 
29338485SPeter.Memishian@Sun.COM 	errno = nvlist_add_uint32(nvl, IPMP_PROBE_STATE, probestate(pr));
29348485SPeter.Memishian@Sun.COM 	if (errno != 0)
29358485SPeter.Memishian@Sun.COM 		goto failed;
29368485SPeter.Memishian@Sun.COM 
29378485SPeter.Memishian@Sun.COM 	errno = nvlist_add_hrtime(nvl, IPMP_PROBE_START_TIME,
29388485SPeter.Memishian@Sun.COM 	    pr->pr_hrtime_start);
29398485SPeter.Memishian@Sun.COM 	if (errno != 0)
29408485SPeter.Memishian@Sun.COM 		goto failed;
29418485SPeter.Memishian@Sun.COM 
29428485SPeter.Memishian@Sun.COM 	errno = nvlist_add_hrtime(nvl, IPMP_PROBE_SENT_TIME,
29438485SPeter.Memishian@Sun.COM 	    pr->pr_hrtime_sent);
29448485SPeter.Memishian@Sun.COM 	if (errno != 0)
29458485SPeter.Memishian@Sun.COM 		goto failed;
29468485SPeter.Memishian@Sun.COM 
29478485SPeter.Memishian@Sun.COM 	if (pr->pr_status == PR_ACKED) {
29488485SPeter.Memishian@Sun.COM 		recv_time = pr->pr_hrtime_ackrecv;
29498485SPeter.Memishian@Sun.COM 		proc_time = pr->pr_hrtime_ackproc;
29508485SPeter.Memishian@Sun.COM 	}
29518485SPeter.Memishian@Sun.COM 
29528485SPeter.Memishian@Sun.COM 	errno = nvlist_add_hrtime(nvl, IPMP_PROBE_ACKRECV_TIME, recv_time);
29538485SPeter.Memishian@Sun.COM 	if (errno != 0)
29548485SPeter.Memishian@Sun.COM 		goto failed;
29558485SPeter.Memishian@Sun.COM 
29568485SPeter.Memishian@Sun.COM 	errno = nvlist_add_hrtime(nvl, IPMP_PROBE_ACKPROC_TIME, proc_time);
29578485SPeter.Memishian@Sun.COM 	if (errno != 0)
29588485SPeter.Memishian@Sun.COM 		goto failed;
29598485SPeter.Memishian@Sun.COM 
29608485SPeter.Memishian@Sun.COM 	if (tg != NULL)
29618485SPeter.Memishian@Sun.COM 		addr2storage(pii->pii_af, &tg->tg_address, &ss);
29628485SPeter.Memishian@Sun.COM 	else
29638485SPeter.Memishian@Sun.COM 		addr2storage(pii->pii_af, &in6addr_any, &ss);
29648485SPeter.Memishian@Sun.COM 
29658485SPeter.Memishian@Sun.COM 	errno = nvlist_add_byte_array(nvl, IPMP_PROBE_TARGET, (uchar_t *)&ss,
29668485SPeter.Memishian@Sun.COM 	    sizeof (ss));
29678485SPeter.Memishian@Sun.COM 	if (errno != 0)
29688485SPeter.Memishian@Sun.COM 		goto failed;
29698485SPeter.Memishian@Sun.COM 
29709416SPeter.Memishian@Sun.COM 	rttavg = (tg != NULL) ? (tg->tg_rtt_sa / 8) : 0;
29719416SPeter.Memishian@Sun.COM 	errno = nvlist_add_int64(nvl, IPMP_PROBE_TARGET_RTTAVG, rttavg);
29728485SPeter.Memishian@Sun.COM 	if (errno != 0)
29738485SPeter.Memishian@Sun.COM 		goto failed;
29748485SPeter.Memishian@Sun.COM 
29759416SPeter.Memishian@Sun.COM 	rttdev = (tg != NULL) ? (tg->tg_rtt_sd / 4) : 0;
29769416SPeter.Memishian@Sun.COM 	errno = nvlist_add_int64(nvl, IPMP_PROBE_TARGET_RTTDEV, rttdev);
29778485SPeter.Memishian@Sun.COM 	if (errno != 0)
29788485SPeter.Memishian@Sun.COM 		goto failed;
29798485SPeter.Memishian@Sun.COM 
29808485SPeter.Memishian@Sun.COM 	return (post_event(ESC_IPMP_PROBE_STATE, nvl));
29818485SPeter.Memishian@Sun.COM failed:
29828485SPeter.Memishian@Sun.COM 	logperror("cannot create `probe state' event");
29838485SPeter.Memishian@Sun.COM 	nvlist_free(nvl);
29848485SPeter.Memishian@Sun.COM 	return (-1);
29850Sstevel@tonic-gate }
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate /*
29880Sstevel@tonic-gate  * Generate an ESC_IPMP_GROUP_STATE sysevent for phyint group `pg'.
29890Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
29900Sstevel@tonic-gate  */
29910Sstevel@tonic-gate static int
phyint_group_state_event(struct phyint_group * pg)29920Sstevel@tonic-gate phyint_group_state_event(struct phyint_group *pg)
29930Sstevel@tonic-gate {
29940Sstevel@tonic-gate 	nvlist_t	*nvl;
29950Sstevel@tonic-gate 
29960Sstevel@tonic-gate 	errno = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
29970Sstevel@tonic-gate 	if (errno != 0) {
29980Sstevel@tonic-gate 		logperror("cannot create `group state change' event");
29990Sstevel@tonic-gate 		return (-1);
30000Sstevel@tonic-gate 	}
30010Sstevel@tonic-gate 
30020Sstevel@tonic-gate 	errno = nvlist_add_string(nvl, IPMP_GROUP_NAME, pg->pg_name);
30030Sstevel@tonic-gate 	if (errno != 0)
30040Sstevel@tonic-gate 		goto failed;
30050Sstevel@tonic-gate 
30060Sstevel@tonic-gate 	errno = nvlist_add_uint64(nvl, IPMP_GROUP_SIGNATURE, pg->pg_sig);
30070Sstevel@tonic-gate 	if (errno != 0)
30080Sstevel@tonic-gate 		goto failed;
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_GROUP_STATE, groupstate(pg));
30110Sstevel@tonic-gate 	if (errno != 0)
30120Sstevel@tonic-gate 		goto failed;
30130Sstevel@tonic-gate 
30140Sstevel@tonic-gate 	return (post_event(ESC_IPMP_GROUP_STATE, nvl));
30150Sstevel@tonic-gate failed:
30160Sstevel@tonic-gate 	logperror("cannot create `group state change' event");
30170Sstevel@tonic-gate 	nvlist_free(nvl);
30180Sstevel@tonic-gate 	return (-1);
30190Sstevel@tonic-gate }
30200Sstevel@tonic-gate 
30210Sstevel@tonic-gate /*
30220Sstevel@tonic-gate  * Generate an ESC_IPMP_GROUP_CHANGE sysevent of type `op' for phyint group
30230Sstevel@tonic-gate  * `pg'.  Returns 0 on success, -1 on failure.
30240Sstevel@tonic-gate  */
30250Sstevel@tonic-gate static int
phyint_group_change_event(struct phyint_group * pg,ipmp_group_op_t op)30260Sstevel@tonic-gate phyint_group_change_event(struct phyint_group *pg, ipmp_group_op_t op)
30270Sstevel@tonic-gate {
30280Sstevel@tonic-gate 	nvlist_t *nvl;
30290Sstevel@tonic-gate 
30300Sstevel@tonic-gate 	errno = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
30310Sstevel@tonic-gate 	if (errno != 0) {
30320Sstevel@tonic-gate 		logperror("cannot create `group change' event");
30330Sstevel@tonic-gate 		return (-1);
30340Sstevel@tonic-gate 	}
30350Sstevel@tonic-gate 
30360Sstevel@tonic-gate 	errno = nvlist_add_string(nvl, IPMP_GROUP_NAME, pg->pg_name);
30370Sstevel@tonic-gate 	if (errno != 0)
30380Sstevel@tonic-gate 		goto failed;
30390Sstevel@tonic-gate 
30400Sstevel@tonic-gate 	errno = nvlist_add_uint64(nvl, IPMP_GROUP_SIGNATURE, pg->pg_sig);
30410Sstevel@tonic-gate 	if (errno != 0)
30420Sstevel@tonic-gate 		goto failed;
30430Sstevel@tonic-gate 
30440Sstevel@tonic-gate 	errno = nvlist_add_uint64(nvl, IPMP_GROUPLIST_SIGNATURE,
30450Sstevel@tonic-gate 	    phyint_grouplistsig);
30460Sstevel@tonic-gate 	if (errno != 0)
30470Sstevel@tonic-gate 		goto failed;
30480Sstevel@tonic-gate 
30490Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_GROUP_OPERATION, op);
30500Sstevel@tonic-gate 	if (errno != 0)
30510Sstevel@tonic-gate 		goto failed;
30520Sstevel@tonic-gate 
30530Sstevel@tonic-gate 	return (post_event(ESC_IPMP_GROUP_CHANGE, nvl));
30540Sstevel@tonic-gate failed:
30550Sstevel@tonic-gate 	logperror("cannot create `group change' event");
30560Sstevel@tonic-gate 	nvlist_free(nvl);
30570Sstevel@tonic-gate 	return (-1);
30580Sstevel@tonic-gate }
30590Sstevel@tonic-gate 
30600Sstevel@tonic-gate /*
30610Sstevel@tonic-gate  * Generate an ESC_IPMP_GROUP_MEMBER_CHANGE sysevent for phyint `pi' in
30620Sstevel@tonic-gate  * group `pg'.	Returns 0 on success, -1 on failure.
30630Sstevel@tonic-gate  */
30640Sstevel@tonic-gate static int
phyint_group_member_event(struct phyint_group * pg,struct phyint * pi,ipmp_if_op_t op)30650Sstevel@tonic-gate phyint_group_member_event(struct phyint_group *pg, struct phyint *pi,
30660Sstevel@tonic-gate     ipmp_if_op_t op)
30670Sstevel@tonic-gate {
30680Sstevel@tonic-gate 	nvlist_t *nvl;
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 	errno = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
30710Sstevel@tonic-gate 	if (errno != 0) {
30720Sstevel@tonic-gate 		logperror("cannot create `group member change' event");
30730Sstevel@tonic-gate 		return (-1);
30740Sstevel@tonic-gate 	}
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 	errno = nvlist_add_string(nvl, IPMP_GROUP_NAME, pg->pg_name);
30770Sstevel@tonic-gate 	if (errno != 0)
30780Sstevel@tonic-gate 		goto failed;
30790Sstevel@tonic-gate 
30800Sstevel@tonic-gate 	errno = nvlist_add_uint64(nvl, IPMP_GROUP_SIGNATURE, pg->pg_sig);
30810Sstevel@tonic-gate 	if (errno != 0)
30820Sstevel@tonic-gate 		goto failed;
30830Sstevel@tonic-gate 
30840Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_IF_OPERATION, op);
30850Sstevel@tonic-gate 	if (errno != 0)
30860Sstevel@tonic-gate 		goto failed;
30870Sstevel@tonic-gate 
30880Sstevel@tonic-gate 	errno = nvlist_add_string(nvl, IPMP_IF_NAME, pi->pi_name);
30890Sstevel@tonic-gate 	if (errno != 0)
30900Sstevel@tonic-gate 		goto failed;
30910Sstevel@tonic-gate 
30920Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_IF_TYPE, iftype(pi));
30930Sstevel@tonic-gate 	if (errno != 0)
30940Sstevel@tonic-gate 		goto failed;
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_IF_STATE, ifstate(pi));
30970Sstevel@tonic-gate 	if (errno != 0)
30980Sstevel@tonic-gate 		goto failed;
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate 	return (post_event(ESC_IPMP_GROUP_MEMBER_CHANGE, nvl));
31010Sstevel@tonic-gate failed:
31020Sstevel@tonic-gate 	logperror("cannot create `group member change' event");
31030Sstevel@tonic-gate 	nvlist_free(nvl);
31040Sstevel@tonic-gate 	return (-1);
31050Sstevel@tonic-gate 
31060Sstevel@tonic-gate }
31070Sstevel@tonic-gate 
31080Sstevel@tonic-gate /*
31090Sstevel@tonic-gate  * Generate an ESC_IPMP_IF_CHANGE sysevent for phyint `pi' in group `pg'.
31100Sstevel@tonic-gate  * Returns 0 on success, -1 on failure.
31110Sstevel@tonic-gate  */
31120Sstevel@tonic-gate static int
phyint_state_event(struct phyint_group * pg,struct phyint * pi)31130Sstevel@tonic-gate phyint_state_event(struct phyint_group *pg, struct phyint *pi)
31140Sstevel@tonic-gate {
31150Sstevel@tonic-gate 	nvlist_t *nvl;
31160Sstevel@tonic-gate 
31170Sstevel@tonic-gate 	errno = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
31180Sstevel@tonic-gate 	if (errno != 0) {
31190Sstevel@tonic-gate 		logperror("cannot create `interface change' event");
31200Sstevel@tonic-gate 		return (-1);
31210Sstevel@tonic-gate 	}
31220Sstevel@tonic-gate 
31230Sstevel@tonic-gate 	errno = nvlist_add_string(nvl, IPMP_GROUP_NAME, pg->pg_name);
31240Sstevel@tonic-gate 	if (errno != 0)
31250Sstevel@tonic-gate 		goto failed;
31260Sstevel@tonic-gate 
31270Sstevel@tonic-gate 	errno = nvlist_add_uint64(nvl, IPMP_GROUP_SIGNATURE, pg->pg_sig);
31280Sstevel@tonic-gate 	if (errno != 0)
31290Sstevel@tonic-gate 		goto failed;
31300Sstevel@tonic-gate 
31310Sstevel@tonic-gate 	errno = nvlist_add_string(nvl, IPMP_IF_NAME, pi->pi_name);
31320Sstevel@tonic-gate 	if (errno != 0)
31330Sstevel@tonic-gate 		goto failed;
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_IF_TYPE, iftype(pi));
31360Sstevel@tonic-gate 	if (errno != 0)
31370Sstevel@tonic-gate 		goto failed;
31380Sstevel@tonic-gate 
31390Sstevel@tonic-gate 	errno = nvlist_add_uint32(nvl, IPMP_IF_STATE, ifstate(pi));
31400Sstevel@tonic-gate 	if (errno != 0)
31410Sstevel@tonic-gate 		goto failed;
31420Sstevel@tonic-gate 
31430Sstevel@tonic-gate 	return (post_event(ESC_IPMP_IF_CHANGE, nvl));
31440Sstevel@tonic-gate failed:
31450Sstevel@tonic-gate 	logperror("cannot create `interface change' event");
31460Sstevel@tonic-gate 	nvlist_free(nvl);
31470Sstevel@tonic-gate 	return (-1);
31480Sstevel@tonic-gate 
31490Sstevel@tonic-gate }
31500Sstevel@tonic-gate 
31510Sstevel@tonic-gate /*
31520Sstevel@tonic-gate  * Generate a signature for use.  The signature is conceptually divided
31530Sstevel@tonic-gate  * into two pieces: a random 16-bit "generation number" and a 48-bit
31540Sstevel@tonic-gate  * monotonically increasing integer.  The generation number protects
31550Sstevel@tonic-gate  * against stale updates to entities (e.g., IPMP groups) that have been
31560Sstevel@tonic-gate  * deleted and since recreated.
31570Sstevel@tonic-gate  */
31580Sstevel@tonic-gate static uint64_t
gensig(void)31590Sstevel@tonic-gate gensig(void)
31600Sstevel@tonic-gate {
31610Sstevel@tonic-gate 	static int seeded = 0;
31620Sstevel@tonic-gate 
31630Sstevel@tonic-gate 	if (seeded == 0) {
31640Sstevel@tonic-gate 		srand48((long)gethrtime());
31650Sstevel@tonic-gate 		seeded++;
31660Sstevel@tonic-gate 	}
31670Sstevel@tonic-gate 
31680Sstevel@tonic-gate 	return ((uint64_t)lrand48() << 48 | 1);
31690Sstevel@tonic-gate }
31700Sstevel@tonic-gate 
31710Sstevel@tonic-gate /*
31720Sstevel@tonic-gate  * Store the information associated with group `grname' into a dynamically
31730Sstevel@tonic-gate  * allocated structure pointed to by `*grinfopp'.  Returns an IPMP error code.
31740Sstevel@tonic-gate  */
31750Sstevel@tonic-gate unsigned int
getgroupinfo(const char * grname,ipmp_groupinfo_t ** grinfopp)31760Sstevel@tonic-gate getgroupinfo(const char *grname, ipmp_groupinfo_t **grinfopp)
31770Sstevel@tonic-gate {
31788485SPeter.Memishian@Sun.COM 	struct phyint		*pi;
31790Sstevel@tonic-gate 	struct phyint_group	*pg;
31800Sstevel@tonic-gate 	char			(*ifs)[LIFNAMSIZ];
31818485SPeter.Memishian@Sun.COM 	unsigned int		i, j;
31828485SPeter.Memishian@Sun.COM 	unsigned int		nif = 0, naddr = 0;
31838485SPeter.Memishian@Sun.COM 	lifgroupinfo_t		lifgr;
31848485SPeter.Memishian@Sun.COM 	addrlist_t		*addrp;
31858485SPeter.Memishian@Sun.COM 	struct sockaddr_storage	*addrs;
31868485SPeter.Memishian@Sun.COM 	int			fdt = 0;
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate 	pg = phyint_group_lookup(grname);
31890Sstevel@tonic-gate 	if (pg == NULL)
31900Sstevel@tonic-gate 		return (IPMP_EUNKGROUP);
31910Sstevel@tonic-gate 
31920Sstevel@tonic-gate 	/*
31930Sstevel@tonic-gate 	 * Tally up the number of interfaces, allocate an array to hold them,
31948485SPeter.Memishian@Sun.COM 	 * and insert their names into the array.  While we're at it, if any
31958485SPeter.Memishian@Sun.COM 	 * interface is actually enabled to send probes, save the group fdt.
31960Sstevel@tonic-gate 	 */
31978485SPeter.Memishian@Sun.COM 	for (pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext)
31980Sstevel@tonic-gate 		nif++;
31990Sstevel@tonic-gate 
32000Sstevel@tonic-gate 	ifs = alloca(nif * sizeof (*ifs));
32010Sstevel@tonic-gate 	for (i = 0, pi = pg->pg_phyint; pi != NULL; pi = pi->pi_pgnext, i++) {
32020Sstevel@tonic-gate 		assert(i < nif);
32030Sstevel@tonic-gate 		(void) strlcpy(ifs[i], pi->pi_name, LIFNAMSIZ);
32048485SPeter.Memishian@Sun.COM 		if (PROBE_ENABLED(pi->pi_v4) || PROBE_ENABLED(pi->pi_v6))
32058485SPeter.Memishian@Sun.COM 			fdt = pg->pg_fdt;
32060Sstevel@tonic-gate 	}
32070Sstevel@tonic-gate 	assert(i == nif);
32080Sstevel@tonic-gate 
32098485SPeter.Memishian@Sun.COM 	/*
32108485SPeter.Memishian@Sun.COM 	 * If this is the anonymous group, there's no other information to
32118485SPeter.Memishian@Sun.COM 	 * collect (since there's no IPMP interface).
32128485SPeter.Memishian@Sun.COM 	 */
32138485SPeter.Memishian@Sun.COM 	if (pg == phyint_anongroup) {
32148485SPeter.Memishian@Sun.COM 		*grinfopp = ipmp_groupinfo_create(pg->pg_name, pg->pg_sig, fdt,
32158485SPeter.Memishian@Sun.COM 		    groupstate(pg), nif, ifs, "", "", "", "", 0, NULL);
32168485SPeter.Memishian@Sun.COM 		return (*grinfopp == NULL ? IPMP_ENOMEM : IPMP_SUCCESS);
32178485SPeter.Memishian@Sun.COM 	}
32188485SPeter.Memishian@Sun.COM 
32198485SPeter.Memishian@Sun.COM 	/*
32208485SPeter.Memishian@Sun.COM 	 * Grab some additional information about the group from the kernel.
32218485SPeter.Memishian@Sun.COM 	 * (NOTE: since SIOCGLIFGROUPINFO does not look up by interface name,
32228485SPeter.Memishian@Sun.COM 	 * we can use ifsock_v4 even for a V6-only group.)
32238485SPeter.Memishian@Sun.COM 	 */
32248485SPeter.Memishian@Sun.COM 	(void) strlcpy(lifgr.gi_grname, grname, LIFGRNAMSIZ);
32258485SPeter.Memishian@Sun.COM 	if (ioctl(ifsock_v4, SIOCGLIFGROUPINFO, &lifgr) == -1) {
32268485SPeter.Memishian@Sun.COM 		if (errno == ENOENT)
32278485SPeter.Memishian@Sun.COM 			return (IPMP_EUNKGROUP);
32288485SPeter.Memishian@Sun.COM 
32298485SPeter.Memishian@Sun.COM 		logperror("getgroupinfo: SIOCGLIFGROUPINFO");
32308485SPeter.Memishian@Sun.COM 		return (IPMP_FAILURE);
32318485SPeter.Memishian@Sun.COM 	}
32328485SPeter.Memishian@Sun.COM 
32338485SPeter.Memishian@Sun.COM 	/*
32348485SPeter.Memishian@Sun.COM 	 * Tally up the number of data addresses, allocate an array to hold
32358485SPeter.Memishian@Sun.COM 	 * them, and insert their values into the array.
32368485SPeter.Memishian@Sun.COM 	 */
32378485SPeter.Memishian@Sun.COM 	for (addrp = pg->pg_addrs; addrp != NULL; addrp = addrp->al_next)
32388485SPeter.Memishian@Sun.COM 		naddr++;
32398485SPeter.Memishian@Sun.COM 
32408485SPeter.Memishian@Sun.COM 	addrs = alloca(naddr * sizeof (*addrs));
32418485SPeter.Memishian@Sun.COM 	i = 0;
32428485SPeter.Memishian@Sun.COM 	for (addrp = pg->pg_addrs; addrp != NULL; addrp = addrp->al_next) {
32438485SPeter.Memishian@Sun.COM 		/*
32448485SPeter.Memishian@Sun.COM 		 * It's possible to have duplicate addresses (if some are
32458485SPeter.Memishian@Sun.COM 		 * down).  Weed the dups out to avoid confusing consumers.
32468485SPeter.Memishian@Sun.COM 		 * (If groups start having tons of addresses, we'll need a
32478485SPeter.Memishian@Sun.COM 		 * better algorithm here.)
32488485SPeter.Memishian@Sun.COM 		 */
32498485SPeter.Memishian@Sun.COM 		for (j = 0; j < i; j++) {
32508485SPeter.Memishian@Sun.COM 			if (sockaddrcmp(&addrs[j], &addrp->al_addr))
32518485SPeter.Memishian@Sun.COM 				break;
32528485SPeter.Memishian@Sun.COM 		}
32538485SPeter.Memishian@Sun.COM 		if (j == i) {
32548485SPeter.Memishian@Sun.COM 			assert(i < naddr);
32558485SPeter.Memishian@Sun.COM 			addrs[i++] = addrp->al_addr;
32568485SPeter.Memishian@Sun.COM 		}
32578485SPeter.Memishian@Sun.COM 	}
32588485SPeter.Memishian@Sun.COM 	naddr = i;
32598485SPeter.Memishian@Sun.COM 
32608485SPeter.Memishian@Sun.COM 	*grinfopp = ipmp_groupinfo_create(pg->pg_name, pg->pg_sig, fdt,
32618485SPeter.Memishian@Sun.COM 	    groupstate(pg), nif, ifs, lifgr.gi_grifname, lifgr.gi_m4ifname,
32628485SPeter.Memishian@Sun.COM 	    lifgr.gi_m6ifname, lifgr.gi_bcifname, naddr, addrs);
32630Sstevel@tonic-gate 	return (*grinfopp == NULL ? IPMP_ENOMEM : IPMP_SUCCESS);
32640Sstevel@tonic-gate }
32650Sstevel@tonic-gate 
32660Sstevel@tonic-gate /*
32678485SPeter.Memishian@Sun.COM  * Store the target information associated with phyint instance `pii' into a
32688485SPeter.Memishian@Sun.COM  * dynamically allocated structure pointed to by `*targinfopp'.  Returns an
32698485SPeter.Memishian@Sun.COM  * IPMP error code.
32708485SPeter.Memishian@Sun.COM  */
32718485SPeter.Memishian@Sun.COM unsigned int
gettarginfo(struct phyint_instance * pii,const char * name,ipmp_targinfo_t ** targinfopp)32728485SPeter.Memishian@Sun.COM gettarginfo(struct phyint_instance *pii, const char *name,
32738485SPeter.Memishian@Sun.COM     ipmp_targinfo_t **targinfopp)
32748485SPeter.Memishian@Sun.COM {
32758485SPeter.Memishian@Sun.COM 	uint_t ntarg = 0;
32768485SPeter.Memishian@Sun.COM 	struct target *tg;
32778485SPeter.Memishian@Sun.COM 	struct sockaddr_storage	ss;
32788485SPeter.Memishian@Sun.COM 	struct sockaddr_storage *targs = NULL;
32798485SPeter.Memishian@Sun.COM 
32808485SPeter.Memishian@Sun.COM 	if (PROBE_CAPABLE(pii)) {
32818485SPeter.Memishian@Sun.COM 		targs = alloca(pii->pii_ntargets * sizeof (*targs));
32828485SPeter.Memishian@Sun.COM 		tg = pii->pii_target_next;
32838485SPeter.Memishian@Sun.COM 		do {
32848485SPeter.Memishian@Sun.COM 			if (tg->tg_status == TG_ACTIVE) {
32858485SPeter.Memishian@Sun.COM 				assert(ntarg < pii->pii_ntargets);
32868485SPeter.Memishian@Sun.COM 				addr2storage(pii->pii_af, &tg->tg_address,
32878485SPeter.Memishian@Sun.COM 				    &targs[ntarg++]);
32888485SPeter.Memishian@Sun.COM 			}
32898485SPeter.Memishian@Sun.COM 			if ((tg = tg->tg_next) == NULL)
32908485SPeter.Memishian@Sun.COM 				tg = pii->pii_targets;
32918485SPeter.Memishian@Sun.COM 		} while (tg != pii->pii_target_next);
32928485SPeter.Memishian@Sun.COM 
32938485SPeter.Memishian@Sun.COM 		assert(ntarg == pii->pii_ntargets);
32948485SPeter.Memishian@Sun.COM 	}
32958485SPeter.Memishian@Sun.COM 
32968485SPeter.Memishian@Sun.COM 	*targinfopp = ipmp_targinfo_create(name, iftestaddr(pii, &ss),
32978485SPeter.Memishian@Sun.COM 	    iftargmode(pii), ntarg, targs);
32988485SPeter.Memishian@Sun.COM 	return (*targinfopp == NULL ? IPMP_ENOMEM : IPMP_SUCCESS);
32998485SPeter.Memishian@Sun.COM }
33008485SPeter.Memishian@Sun.COM 
33018485SPeter.Memishian@Sun.COM /*
33020Sstevel@tonic-gate  * Store the information associated with interface `ifname' into a dynamically
33030Sstevel@tonic-gate  * allocated structure pointed to by `*ifinfopp'.  Returns an IPMP error code.
33040Sstevel@tonic-gate  */
33050Sstevel@tonic-gate unsigned int
getifinfo(const char * ifname,ipmp_ifinfo_t ** ifinfopp)33060Sstevel@tonic-gate getifinfo(const char *ifname, ipmp_ifinfo_t **ifinfopp)
33070Sstevel@tonic-gate {
33088485SPeter.Memishian@Sun.COM 	int		retval;
33090Sstevel@tonic-gate 	struct phyint	*pi;
33108485SPeter.Memishian@Sun.COM 	ipmp_targinfo_t	*targinfo4;
33118485SPeter.Memishian@Sun.COM 	ipmp_targinfo_t	*targinfo6;
33120Sstevel@tonic-gate 
33130Sstevel@tonic-gate 	pi = phyint_lookup(ifname);
33140Sstevel@tonic-gate 	if (pi == NULL)
33150Sstevel@tonic-gate 		return (IPMP_EUNKIF);
33160Sstevel@tonic-gate 
33178485SPeter.Memishian@Sun.COM 	if ((retval = gettarginfo(pi->pi_v4, pi->pi_name, &targinfo4)) != 0 ||
33188485SPeter.Memishian@Sun.COM 	    (retval = gettarginfo(pi->pi_v6, pi->pi_name, &targinfo6)) != 0)
33198485SPeter.Memishian@Sun.COM 		goto out;
33208485SPeter.Memishian@Sun.COM 
33210Sstevel@tonic-gate 	*ifinfopp = ipmp_ifinfo_create(pi->pi_name, pi->pi_group->pg_name,
33228485SPeter.Memishian@Sun.COM 	    ifstate(pi), iftype(pi), iflinkstate(pi), ifprobestate(pi),
33238485SPeter.Memishian@Sun.COM 	    ifflags(pi), targinfo4, targinfo6);
33248485SPeter.Memishian@Sun.COM 	retval = (*ifinfopp == NULL ? IPMP_ENOMEM : IPMP_SUCCESS);
33258485SPeter.Memishian@Sun.COM out:
33268485SPeter.Memishian@Sun.COM 	if (targinfo4 != NULL)
33278485SPeter.Memishian@Sun.COM 		ipmp_freetarginfo(targinfo4);
33288485SPeter.Memishian@Sun.COM 	if (targinfo6 != NULL)
33298485SPeter.Memishian@Sun.COM 		ipmp_freetarginfo(targinfo6);
33308485SPeter.Memishian@Sun.COM 	return (retval);
33310Sstevel@tonic-gate }
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate /*
33340Sstevel@tonic-gate  * Store the current list of IPMP groups into a dynamically allocated
33350Sstevel@tonic-gate  * structure pointed to by `*grlistpp'.	 Returns an IPMP error code.
33360Sstevel@tonic-gate  */
33370Sstevel@tonic-gate unsigned int
getgrouplist(ipmp_grouplist_t ** grlistpp)33380Sstevel@tonic-gate getgrouplist(ipmp_grouplist_t **grlistpp)
33390Sstevel@tonic-gate {
33400Sstevel@tonic-gate 	struct phyint_group	*pg;
33410Sstevel@tonic-gate 	char			(*groups)[LIFGRNAMSIZ];
33420Sstevel@tonic-gate 	unsigned int		i, ngroup;
33430Sstevel@tonic-gate 
33440Sstevel@tonic-gate 	/*
33450Sstevel@tonic-gate 	 * Tally up the number of groups, allocate an array to hold them, and
33460Sstevel@tonic-gate 	 * insert their names into the array.
33470Sstevel@tonic-gate 	 */
33480Sstevel@tonic-gate 	for (ngroup = 0, pg = phyint_groups; pg != NULL; pg = pg->pg_next)
33490Sstevel@tonic-gate 		ngroup++;
33500Sstevel@tonic-gate 
33510Sstevel@tonic-gate 	groups = alloca(ngroup * sizeof (*groups));
33520Sstevel@tonic-gate 	for (i = 0, pg = phyint_groups; pg != NULL; pg = pg->pg_next, i++) {
33530Sstevel@tonic-gate 		assert(i < ngroup);
33540Sstevel@tonic-gate 		(void) strlcpy(groups[i], pg->pg_name, LIFGRNAMSIZ);
33550Sstevel@tonic-gate 	}
33560Sstevel@tonic-gate 	assert(i == ngroup);
33570Sstevel@tonic-gate 
33580Sstevel@tonic-gate 	*grlistpp = ipmp_grouplist_create(phyint_grouplistsig, ngroup, groups);
33590Sstevel@tonic-gate 	return (*grlistpp == NULL ? IPMP_ENOMEM : IPMP_SUCCESS);
33600Sstevel@tonic-gate }
33610Sstevel@tonic-gate 
33620Sstevel@tonic-gate /*
33638485SPeter.Memishian@Sun.COM  * Store the address information for `ssp' (in group `grname') into a
33648485SPeter.Memishian@Sun.COM  * dynamically allocated structure pointed to by `*adinfopp'.  Returns an IPMP
33658485SPeter.Memishian@Sun.COM  * error code.  (We'd call this function getaddrinfo(), but it would conflict
33668485SPeter.Memishian@Sun.COM  * with getaddrinfo(3SOCKET)).
33678485SPeter.Memishian@Sun.COM  */
33688485SPeter.Memishian@Sun.COM unsigned int
getgraddrinfo(const char * grname,struct sockaddr_storage * ssp,ipmp_addrinfo_t ** adinfopp)33698485SPeter.Memishian@Sun.COM getgraddrinfo(const char *grname, struct sockaddr_storage *ssp,
33708485SPeter.Memishian@Sun.COM     ipmp_addrinfo_t **adinfopp)
33718485SPeter.Memishian@Sun.COM {
33728485SPeter.Memishian@Sun.COM 	int ifsock;
33738485SPeter.Memishian@Sun.COM 	addrlist_t *addrp, *addrmatchp = NULL;
33748485SPeter.Memishian@Sun.COM 	ipmp_addr_state_t state;
33758485SPeter.Memishian@Sun.COM 	const char *binding = "";
33768485SPeter.Memishian@Sun.COM 	struct lifreq lifr;
33778485SPeter.Memishian@Sun.COM 	struct phyint_group *pg;
33788485SPeter.Memishian@Sun.COM 
33798485SPeter.Memishian@Sun.COM 	if ((pg = phyint_group_lookup(grname)) == NULL)
33808485SPeter.Memishian@Sun.COM 		return (IPMP_EUNKADDR);
33818485SPeter.Memishian@Sun.COM 
33828485SPeter.Memishian@Sun.COM 	/*
33838485SPeter.Memishian@Sun.COM 	 * Walk through the data addresses, and find a match.  Note that since
33848485SPeter.Memishian@Sun.COM 	 * some of the addresses may be down, more than one may match.  We
33858485SPeter.Memishian@Sun.COM 	 * prefer an up address (if one exists).
33868485SPeter.Memishian@Sun.COM 	 */
33878485SPeter.Memishian@Sun.COM 	for (addrp = pg->pg_addrs; addrp != NULL; addrp = addrp->al_next) {
33888485SPeter.Memishian@Sun.COM 		if (sockaddrcmp(ssp, &addrp->al_addr)) {
33898485SPeter.Memishian@Sun.COM 			addrmatchp = addrp;
33908485SPeter.Memishian@Sun.COM 			if (addrmatchp->al_flags & IFF_UP)
33918485SPeter.Memishian@Sun.COM 				break;
33928485SPeter.Memishian@Sun.COM 		}
33938485SPeter.Memishian@Sun.COM 	}
33948485SPeter.Memishian@Sun.COM 
33958485SPeter.Memishian@Sun.COM 	if (addrmatchp == NULL)
33968485SPeter.Memishian@Sun.COM 		return (IPMP_EUNKADDR);
33978485SPeter.Memishian@Sun.COM 
33988485SPeter.Memishian@Sun.COM 	state = (addrmatchp->al_flags & IFF_UP) ? IPMP_ADDR_UP : IPMP_ADDR_DOWN;
33998485SPeter.Memishian@Sun.COM 	if (state == IPMP_ADDR_UP) {
34008485SPeter.Memishian@Sun.COM 		ifsock = (ssp->ss_family == AF_INET) ? ifsock_v4 : ifsock_v6;
34018485SPeter.Memishian@Sun.COM 		(void) strlcpy(lifr.lifr_name, addrmatchp->al_name, LIFNAMSIZ);
34028485SPeter.Memishian@Sun.COM 		if (ioctl(ifsock, SIOCGLIFBINDING, &lifr) >= 0)
34038485SPeter.Memishian@Sun.COM 			binding = lifr.lifr_binding;
34048485SPeter.Memishian@Sun.COM 	}
34058485SPeter.Memishian@Sun.COM 
34068485SPeter.Memishian@Sun.COM 	*adinfopp = ipmp_addrinfo_create(ssp, state, pg->pg_name, binding);
34078485SPeter.Memishian@Sun.COM 	return (*adinfopp == NULL ? IPMP_ENOMEM : IPMP_SUCCESS);
34088485SPeter.Memishian@Sun.COM }
34098485SPeter.Memishian@Sun.COM 
34108485SPeter.Memishian@Sun.COM /*
34110Sstevel@tonic-gate  * Store a snapshot of the IPMP subsystem into a dynamically allocated
34120Sstevel@tonic-gate  * structure pointed to by `*snapp'.  Returns an IPMP error code.
34130Sstevel@tonic-gate  */
34140Sstevel@tonic-gate unsigned int
getsnap(ipmp_snap_t ** snapp)34150Sstevel@tonic-gate getsnap(ipmp_snap_t **snapp)
34160Sstevel@tonic-gate {
34170Sstevel@tonic-gate 	ipmp_grouplist_t	*grlistp;
34180Sstevel@tonic-gate 	ipmp_groupinfo_t	*grinfop;
34198485SPeter.Memishian@Sun.COM 	ipmp_addrinfo_t		*adinfop;
34208485SPeter.Memishian@Sun.COM 	ipmp_addrlist_t		*adlistp;
34210Sstevel@tonic-gate 	ipmp_ifinfo_t		*ifinfop;
34220Sstevel@tonic-gate 	ipmp_snap_t		*snap;
34230Sstevel@tonic-gate 	struct phyint		*pi;
34248485SPeter.Memishian@Sun.COM 	unsigned int		i, j;
34250Sstevel@tonic-gate 	int			retval;
34260Sstevel@tonic-gate 
34270Sstevel@tonic-gate 	snap = ipmp_snap_create();
34280Sstevel@tonic-gate 	if (snap == NULL)
34290Sstevel@tonic-gate 		return (IPMP_ENOMEM);
34300Sstevel@tonic-gate 
34310Sstevel@tonic-gate 	/*
34320Sstevel@tonic-gate 	 * Add group list.
34330Sstevel@tonic-gate 	 */
34340Sstevel@tonic-gate 	retval = getgrouplist(&snap->sn_grlistp);
34358485SPeter.Memishian@Sun.COM 	if (retval != IPMP_SUCCESS)
34368485SPeter.Memishian@Sun.COM 		goto failed;
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate 	/*
34398485SPeter.Memishian@Sun.COM 	 * Add information for each group in the list, along with all of its
34408485SPeter.Memishian@Sun.COM 	 * data addresses.
34410Sstevel@tonic-gate 	 */
34420Sstevel@tonic-gate 	grlistp = snap->sn_grlistp;
34430Sstevel@tonic-gate 	for (i = 0; i < grlistp->gl_ngroup; i++) {
34440Sstevel@tonic-gate 		retval = getgroupinfo(grlistp->gl_groups[i], &grinfop);
34458485SPeter.Memishian@Sun.COM 		if (retval != IPMP_SUCCESS)
34468485SPeter.Memishian@Sun.COM 			goto failed;
34478485SPeter.Memishian@Sun.COM 
34480Sstevel@tonic-gate 		retval = ipmp_snap_addgroupinfo(snap, grinfop);
34490Sstevel@tonic-gate 		if (retval != IPMP_SUCCESS) {
34500Sstevel@tonic-gate 			ipmp_freegroupinfo(grinfop);
34518485SPeter.Memishian@Sun.COM 			goto failed;
34528485SPeter.Memishian@Sun.COM 		}
34538485SPeter.Memishian@Sun.COM 
34548485SPeter.Memishian@Sun.COM 		adlistp = grinfop->gr_adlistp;
34558485SPeter.Memishian@Sun.COM 		for (j = 0; j < adlistp->al_naddr; j++) {
34568485SPeter.Memishian@Sun.COM 			retval = getgraddrinfo(grinfop->gr_name,
34578485SPeter.Memishian@Sun.COM 			    &adlistp->al_addrs[j], &adinfop);
34588485SPeter.Memishian@Sun.COM 			if (retval != IPMP_SUCCESS)
34598485SPeter.Memishian@Sun.COM 				goto failed;
34608485SPeter.Memishian@Sun.COM 
34618485SPeter.Memishian@Sun.COM 			retval = ipmp_snap_addaddrinfo(snap, adinfop);
34628485SPeter.Memishian@Sun.COM 			if (retval != IPMP_SUCCESS) {
34638485SPeter.Memishian@Sun.COM 				ipmp_freeaddrinfo(adinfop);
34648485SPeter.Memishian@Sun.COM 				goto failed;
34658485SPeter.Memishian@Sun.COM 			}
34660Sstevel@tonic-gate 		}
34670Sstevel@tonic-gate 	}
34680Sstevel@tonic-gate 
34690Sstevel@tonic-gate 	/*
34700Sstevel@tonic-gate 	 * Add information for each configured phyint.
34710Sstevel@tonic-gate 	 */
34720Sstevel@tonic-gate 	for (pi = phyints; pi != NULL; pi = pi->pi_next) {
34730Sstevel@tonic-gate 		retval = getifinfo(pi->pi_name, &ifinfop);
34748485SPeter.Memishian@Sun.COM 		if (retval != IPMP_SUCCESS)
34758485SPeter.Memishian@Sun.COM 			goto failed;
34768485SPeter.Memishian@Sun.COM 
34770Sstevel@tonic-gate 		retval = ipmp_snap_addifinfo(snap, ifinfop);
34780Sstevel@tonic-gate 		if (retval != IPMP_SUCCESS) {
34790Sstevel@tonic-gate 			ipmp_freeifinfo(ifinfop);
34808485SPeter.Memishian@Sun.COM 			goto failed;
34810Sstevel@tonic-gate 		}
34820Sstevel@tonic-gate 	}
34830Sstevel@tonic-gate 
34840Sstevel@tonic-gate 	*snapp = snap;
34850Sstevel@tonic-gate 	return (IPMP_SUCCESS);
34868485SPeter.Memishian@Sun.COM failed:
34878485SPeter.Memishian@Sun.COM 	ipmp_snap_free(snap);
34888485SPeter.Memishian@Sun.COM 	return (retval);
34890Sstevel@tonic-gate }
3490