13871Syz147064 /*
23871Syz147064  * CDDL HEADER START
33871Syz147064  *
43871Syz147064  * The contents of this file are subject to the terms of the
53871Syz147064  * Common Development and Distribution License (the "License").
63871Syz147064  * You may not use this file except in compliance with the License.
73871Syz147064  *
83871Syz147064  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93871Syz147064  * or http://www.opensolaris.org/os/licensing.
103871Syz147064  * See the License for the specific language governing permissions
113871Syz147064  * and limitations under the License.
123871Syz147064  *
133871Syz147064  * When distributing Covered Code, include this CDDL HEADER in each
143871Syz147064  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153871Syz147064  * If applicable, add the following below this CDDL HEADER, with the
163871Syz147064  * fields enclosed by brackets "[]" replaced with your own identifying
173871Syz147064  * information: Portions Copyright [yyyy] [name of copyright owner]
183871Syz147064  *
193871Syz147064  * CDDL HEADER END
203871Syz147064  */
213871Syz147064 /*
225895Syz147064  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
233871Syz147064  * Use is subject to license terms.
243871Syz147064  */
253871Syz147064 
263871Syz147064 #include <stdio.h>
273871Syz147064 #include <sys/types.h>
283871Syz147064 #include <sys/stat.h>
293871Syz147064 #include <string.h>
303871Syz147064 #include <fcntl.h>
313871Syz147064 #include <unistd.h>
323871Syz147064 #include <stropts.h>
333871Syz147064 #include <stdlib.h>
343871Syz147064 #include <errno.h>
355895Syz147064 #include <assert.h>
363871Syz147064 #include <strings.h>
373871Syz147064 #include <libintl.h>
383871Syz147064 #include <net/if_types.h>
393871Syz147064 #include <net/if_dl.h>
405895Syz147064 #include <libdllink.h>
415895Syz147064 #include <libdlvlan.h>
423871Syz147064 #include <libdlaggr.h>
433871Syz147064 #include <libdladm_impl.h>
443871Syz147064 
453871Syz147064 /*
463871Syz147064  * Link Aggregation Administration Library.
473871Syz147064  *
483871Syz147064  * This library is used by administration tools such as dladm(1M) to
493871Syz147064  * configure link aggregations.
503871Syz147064  */
513871Syz147064 
523871Syz147064 /* Limits on buffer size for LAIOC_INFO request */
533871Syz147064 #define	MIN_INFO_SIZE (4*1024)
543871Syz147064 #define	MAX_INFO_SIZE (128*1024)
553871Syz147064 
565895Syz147064 static uchar_t	zero_mac[] = {0, 0, 0, 0, 0, 0};
575895Syz147064 #define	VALID_PORT_MAC(mac)						\
585895Syz147064 	(((mac) != NULL) && (bcmp(zero_mac, (mac), ETHERADDRL) != 0) &&	\
595895Syz147064 	(!(mac)[0] & 0x01))
603871Syz147064 
615895Syz147064 #define	PORT_DELIMITER	'.'
623871Syz147064 
635895Syz147064 #define	WRITE_PORT(portstr, portid, size) {			\
645895Syz147064 	char pstr[LINKID_STR_WIDTH + 2];			\
655895Syz147064 	(void) snprintf(pstr, LINKID_STR_WIDTH + 2, "%d%c",	\
665895Syz147064 	    (portid), PORT_DELIMITER);				\
675895Syz147064 	(void) strlcat((portstr), pstr, (size));		\
685895Syz147064 }
693871Syz147064 
705895Syz147064 #define	READ_PORT(portstr, portid, status) {			\
715895Syz147064 	errno = 0;						\
725895Syz147064 	(status) = DLADM_STATUS_OK;				\
735895Syz147064 	(portid) = (int)strtol((portstr), &(portstr), 10);	\
745895Syz147064 	if (errno != 0 || *(portstr) != PORT_DELIMITER) {	\
755895Syz147064 		(status) = DLADM_STATUS_REPOSITORYINVAL;	\
765895Syz147064 	} else {						\
775895Syz147064 		/* Skip the delimiter. */			\
785895Syz147064 		(portstr)++;					\
795895Syz147064 	}							\
805895Syz147064 }
813871Syz147064 
823871Syz147064 typedef struct dladm_aggr_modify_attr {
833871Syz147064 	uint32_t	ld_policy;
843871Syz147064 	boolean_t	ld_mac_fixed;
853871Syz147064 	uchar_t		ld_mac[ETHERADDRL];
863871Syz147064 	aggr_lacp_mode_t ld_lacp_mode;
873871Syz147064 	aggr_lacp_timer_t ld_lacp_timer;
883871Syz147064 } dladm_aggr_modify_attr_t;
893871Syz147064 
903871Syz147064 typedef struct policy_s {
913871Syz147064 	char		*pol_name;
923871Syz147064 	uint32_t	policy;
933871Syz147064 } policy_t;
943871Syz147064 
953871Syz147064 static policy_t policies[] = {
963871Syz147064 	{"L2",		AGGR_POLICY_L2},
973871Syz147064 	{"L3",		AGGR_POLICY_L3},
983871Syz147064 	{"L4",		AGGR_POLICY_L4}};
993871Syz147064 
1003871Syz147064 #define	NPOLICIES	(sizeof (policies) / sizeof (policy_t))
1013871Syz147064 
1023871Syz147064 typedef struct dladm_aggr_lacpmode_s {
1033871Syz147064 	char		*mode_str;
1043871Syz147064 	aggr_lacp_mode_t mode_id;
1053871Syz147064 } dladm_aggr_lacpmode_t;
1063871Syz147064 
1073871Syz147064 static dladm_aggr_lacpmode_t lacp_modes[] = {
1083871Syz147064 	{"off", AGGR_LACP_OFF},
1093871Syz147064 	{"active", AGGR_LACP_ACTIVE},
1103871Syz147064 	{"passive", AGGR_LACP_PASSIVE}};
1113871Syz147064 
1123871Syz147064 #define	NLACP_MODES	(sizeof (lacp_modes) / sizeof (dladm_aggr_lacpmode_t))
1133871Syz147064 
1143871Syz147064 typedef struct dladm_aggr_lacptimer_s {
1153871Syz147064 	char		*lt_str;
1163871Syz147064 	aggr_lacp_timer_t lt_id;
1173871Syz147064 } dladm_aggr_lacptimer_t;
1183871Syz147064 
1193871Syz147064 static dladm_aggr_lacptimer_t lacp_timers[] = {
1203871Syz147064 	{"short", AGGR_LACP_TIMER_SHORT},
1213871Syz147064 	{"long", AGGR_LACP_TIMER_LONG}};
1223871Syz147064 
1233871Syz147064 #define	NLACP_TIMERS	(sizeof (lacp_timers) / sizeof (dladm_aggr_lacptimer_t))
1243871Syz147064 
1253871Syz147064 typedef struct dladm_aggr_port_state {
1263871Syz147064 	char			*state_str;
1273871Syz147064 	aggr_port_state_t	state_id;
1283871Syz147064 } dladm_aggr_port_state_t;
1293871Syz147064 
1303871Syz147064 static dladm_aggr_port_state_t port_states[] = {
1313871Syz147064 	{"standby", AGGR_PORT_STATE_STANDBY },
1323871Syz147064 	{"attached", AGGR_PORT_STATE_ATTACHED }
1333871Syz147064 };
1343871Syz147064 
1353871Syz147064 #define	NPORT_STATES	\
1363871Syz147064 	(sizeof (port_states) / sizeof (dladm_aggr_port_state_t))
1373871Syz147064 
1385895Syz147064 static int
139*7408SSebastien.Roy@Sun.COM i_dladm_aggr_ioctl(int cmd, void *ptr)
1405895Syz147064 {
1415895Syz147064 	int err, fd;
1423871Syz147064 
143*7408SSebastien.Roy@Sun.COM 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
1443871Syz147064 		return (-1);
1453871Syz147064 
146*7408SSebastien.Roy@Sun.COM 	err = ioctl(fd, cmd, ptr);
1475895Syz147064 	(void) close(fd);
1483871Syz147064 
1495895Syz147064 	return (err);
1503871Syz147064 }
1513871Syz147064 
1523871Syz147064 /*
1535895Syz147064  * Caller must free attr.lg_ports. The ptr pointer is advanced while convert
1545895Syz147064  * the laioc_info_t to the dladm_aggr_grp_attr_t structure.
1553871Syz147064  */
1565895Syz147064 static int
1575895Syz147064 i_dladm_aggr_iocp2grpattr(void **ptr, dladm_aggr_grp_attr_t *attrp)
1583871Syz147064 {
1595895Syz147064 	laioc_info_group_t	*grp;
1605895Syz147064 	laioc_info_port_t	*port;
1615895Syz147064 	int			i;
1625895Syz147064 	void			*where = (*ptr);
1635895Syz147064 
1645895Syz147064 	grp = (laioc_info_group_t *)where;
1655895Syz147064 
1665895Syz147064 	attrp->lg_linkid = grp->lg_linkid;
1675895Syz147064 	attrp->lg_key = grp->lg_key;
1685895Syz147064 	attrp->lg_nports = grp->lg_nports;
1695895Syz147064 	attrp->lg_policy = grp->lg_policy;
1705895Syz147064 	attrp->lg_lacp_mode = grp->lg_lacp_mode;
1715895Syz147064 	attrp->lg_lacp_timer = grp->lg_lacp_timer;
1725895Syz147064 	attrp->lg_force = grp->lg_force;
1735895Syz147064 
1745895Syz147064 	bcopy(grp->lg_mac, attrp->lg_mac, ETHERADDRL);
1755895Syz147064 	attrp->lg_mac_fixed = grp->lg_mac_fixed;
1763871Syz147064 
1775895Syz147064 	if ((attrp->lg_ports = malloc(grp->lg_nports *
1785895Syz147064 	    sizeof (dladm_aggr_port_attr_t))) == NULL) {
1795895Syz147064 		errno = ENOMEM;
1805895Syz147064 		goto fail;
1815895Syz147064 	}
1825895Syz147064 
1835895Syz147064 	where = (grp + 1);
1843871Syz147064 
1855895Syz147064 	/*
1865895Syz147064 	 * Go through each port that is part of the group.
1875895Syz147064 	 */
1885895Syz147064 	for (i = 0; i < grp->lg_nports; i++) {
1895895Syz147064 		port = (laioc_info_port_t *)where;
1903871Syz147064 
1915895Syz147064 		attrp->lg_ports[i].lp_linkid = port->lp_linkid;
1925895Syz147064 		bcopy(port->lp_mac, attrp->lg_ports[i].lp_mac, ETHERADDRL);
1935895Syz147064 		attrp->lg_ports[i].lp_state = port->lp_state;
1945895Syz147064 		attrp->lg_ports[i].lp_lacp_state = port->lp_lacp_state;
1955895Syz147064 
1965895Syz147064 		where = (port + 1);
1975895Syz147064 	}
1985895Syz147064 	*ptr = where;
1995895Syz147064 	return (0);
2005895Syz147064 fail:
2015895Syz147064 	return (-1);
2023871Syz147064 }
2033871Syz147064 
2043871Syz147064 /*
2055895Syz147064  * Get active configuration of a specific aggregation.
2065895Syz147064  * Caller must free attrp->la_ports.
2073871Syz147064  */
2085895Syz147064 static dladm_status_t
2095895Syz147064 i_dladm_aggr_info_active(datalink_id_t linkid, dladm_aggr_grp_attr_t *attrp)
2103871Syz147064 {
2113871Syz147064 	laioc_info_t *ioc;
212*7408SSebastien.Roy@Sun.COM 	int bufsize;
2135895Syz147064 	void *where;
2145895Syz147064 	dladm_status_t status = DLADM_STATUS_OK;
2153871Syz147064 
2163871Syz147064 	bufsize = MIN_INFO_SIZE;
2173871Syz147064 	ioc = (laioc_info_t *)calloc(1, bufsize);
2185895Syz147064 	if (ioc == NULL)
2195895Syz147064 		return (DLADM_STATUS_NOMEM);
2205895Syz147064 
2215895Syz147064 	ioc->li_group_linkid = linkid;
2223871Syz147064 
2233871Syz147064 tryagain:
224*7408SSebastien.Roy@Sun.COM 	ioc->li_bufsize = bufsize;
225*7408SSebastien.Roy@Sun.COM 	if (i_dladm_aggr_ioctl(LAIOC_INFO, ioc) != 0) {
2263871Syz147064 		if (errno == ENOSPC) {
2273871Syz147064 			/*
2283871Syz147064 			 * The LAIOC_INFO call failed due to a short
2293871Syz147064 			 * buffer. Reallocate the buffer and try again.
2303871Syz147064 			 */
2313871Syz147064 			bufsize *= 2;
2323871Syz147064 			if (bufsize <= MAX_INFO_SIZE) {
2333871Syz147064 				ioc = (laioc_info_t *)realloc(ioc, bufsize);
2343871Syz147064 				if (ioc != NULL) {
2353871Syz147064 					bzero(ioc, sizeof (bufsize));
2363871Syz147064 					goto tryagain;
2373871Syz147064 				}
2383871Syz147064 			}
2393871Syz147064 		}
2405895Syz147064 		status = dladm_errno2status(errno);
2413871Syz147064 		goto bail;
2423871Syz147064 	}
2433871Syz147064 
2443871Syz147064 	/*
2453871Syz147064 	 * Go through each group returned by the aggregation driver.
2463871Syz147064 	 */
2473871Syz147064 	where = (char *)(ioc + 1);
2485895Syz147064 	if (i_dladm_aggr_iocp2grpattr(&where, attrp) != 0) {
2495895Syz147064 		status = dladm_errno2status(errno);
2505895Syz147064 		goto bail;
2513871Syz147064 	}
2523871Syz147064 
2533871Syz147064 bail:
2543871Syz147064 	free(ioc);
2553871Syz147064 	return (status);
2563871Syz147064 }
2573871Syz147064 
2583871Syz147064 /*
2595895Syz147064  * Get configuration information of a specific aggregation.
2605895Syz147064  * Caller must free attrp->la_ports.
2613871Syz147064  */
2623871Syz147064 static dladm_status_t
2635895Syz147064 i_dladm_aggr_info_persist(datalink_id_t linkid, dladm_aggr_grp_attr_t *attrp)
2643871Syz147064 {
2655895Syz147064 	dladm_conf_t	conf;
2665895Syz147064 	uint32_t	nports, i;
2675895Syz147064 	char		*portstr, *next;
2685895Syz147064 	dladm_status_t	status;
2695895Syz147064 	uint64_t	u64;
2705895Syz147064 	int		size;
2715895Syz147064 	char		macstr[ETHERADDRL * 3];
2725895Syz147064 
2735895Syz147064 	attrp->lg_linkid = linkid;
2745895Syz147064 	if ((status = dladm_read_conf(linkid, &conf)) != DLADM_STATUS_OK)
2755895Syz147064 		return (status);
2765895Syz147064 
2775895Syz147064 	status = dladm_get_conf_field(conf, FKEY, &u64, sizeof (u64));
2785895Syz147064 	if (status != DLADM_STATUS_OK)
2795895Syz147064 		goto done;
2805895Syz147064 	attrp->lg_key = (uint16_t)u64;
2815895Syz147064 
2825895Syz147064 	status = dladm_get_conf_field(conf, FPOLICY, &u64, sizeof (u64));
2835895Syz147064 	if (status != DLADM_STATUS_OK)
2845895Syz147064 		goto done;
2855895Syz147064 	attrp->lg_policy = (uint32_t)u64;
2865895Syz147064 
2875895Syz147064 	status = dladm_get_conf_field(conf, FFIXMACADDR, &attrp->lg_mac_fixed,
2885895Syz147064 	    sizeof (boolean_t));
2895895Syz147064 	if (status != DLADM_STATUS_OK)
2905895Syz147064 		goto done;
2915895Syz147064 
2925895Syz147064 	if (attrp->lg_mac_fixed) {
2935895Syz147064 		boolean_t fixed;
2943871Syz147064 
2955895Syz147064 		if ((status = dladm_get_conf_field(conf, FMACADDR, macstr,
2965895Syz147064 		    sizeof (macstr))) != DLADM_STATUS_OK) {
2975895Syz147064 			goto done;
2985895Syz147064 		}
2995895Syz147064 		if (!dladm_aggr_str2macaddr(macstr, &fixed, attrp->lg_mac)) {
3005895Syz147064 			status = DLADM_STATUS_REPOSITORYINVAL;
3015895Syz147064 			goto done;
3025895Syz147064 		}
3035895Syz147064 	}
3045895Syz147064 
3055895Syz147064 	status = dladm_get_conf_field(conf, FFORCE, &attrp->lg_force,
3065895Syz147064 	    sizeof (boolean_t));
3075895Syz147064 	if (status != DLADM_STATUS_OK)
3085895Syz147064 		goto done;
3095895Syz147064 
3105895Syz147064 	status = dladm_get_conf_field(conf, FLACPMODE, &u64, sizeof (u64));
3115895Syz147064 	if (status != DLADM_STATUS_OK)
3125895Syz147064 		goto done;
3135895Syz147064 	attrp->lg_lacp_mode = (aggr_lacp_mode_t)u64;
3145895Syz147064 
3155895Syz147064 	status = dladm_get_conf_field(conf, FLACPTIMER, &u64, sizeof (u64));
3165895Syz147064 	if (status != DLADM_STATUS_OK)
3175895Syz147064 		goto done;
3185895Syz147064 	attrp->lg_lacp_timer = (aggr_lacp_timer_t)u64;
3195895Syz147064 
3205895Syz147064 	status = dladm_get_conf_field(conf, FNPORTS, &u64, sizeof (u64));
3215895Syz147064 	if (status != DLADM_STATUS_OK)
3225895Syz147064 		goto done;
3235895Syz147064 	nports = (uint32_t)u64;
3245895Syz147064 	attrp->lg_nports = nports;
3255895Syz147064 
3265895Syz147064 	size = nports * (LINKID_STR_WIDTH + 1) + 1;
3275895Syz147064 	if ((portstr = calloc(1, size)) == NULL) {
3285895Syz147064 		status = DLADM_STATUS_NOMEM;
3295895Syz147064 		goto done;
3305895Syz147064 	}
3315895Syz147064 
3325895Syz147064 	status = dladm_get_conf_field(conf, FPORTS, portstr, size);
3335895Syz147064 	if (status != DLADM_STATUS_OK) {
3345895Syz147064 		free(portstr);
3355895Syz147064 		goto done;
3365895Syz147064 	}
3375895Syz147064 
3385895Syz147064 	if ((attrp->lg_ports = malloc(nports *
3395895Syz147064 	    sizeof (dladm_aggr_port_attr_t))) == NULL) {
3405895Syz147064 		free(portstr);
3413871Syz147064 		status = DLADM_STATUS_NOMEM;
3423871Syz147064 		goto done;
3433871Syz147064 	}
3443871Syz147064 
3455895Syz147064 	for (next = portstr, i = 0; i < nports; i++) {
3465895Syz147064 		READ_PORT(next, attrp->lg_ports[i].lp_linkid, status);
3475895Syz147064 		if (status != DLADM_STATUS_OK) {
3485895Syz147064 			free(portstr);
3495895Syz147064 			free(attrp->lg_ports);
3505895Syz147064 			goto done;
3515895Syz147064 		}
3525895Syz147064 	}
3535895Syz147064 	free(portstr);
3545895Syz147064 
3555895Syz147064 done:
3565895Syz147064 	dladm_destroy_conf(conf);
3575895Syz147064 	return (status);
3585895Syz147064 }
3595895Syz147064 
3605895Syz147064 dladm_status_t
3615895Syz147064 dladm_aggr_info(datalink_id_t linkid, dladm_aggr_grp_attr_t *attrp,
3625895Syz147064     uint32_t flags)
3635895Syz147064 {
3645895Syz147064 	assert(flags == DLADM_OPT_ACTIVE || flags == DLADM_OPT_PERSIST);
3655895Syz147064 	if (flags == DLADM_OPT_ACTIVE)
3665895Syz147064 		return (i_dladm_aggr_info_active(linkid, attrp));
3675895Syz147064 	else
3685895Syz147064 		return (i_dladm_aggr_info_persist(linkid, attrp));
3695895Syz147064 }
3703871Syz147064 
3715895Syz147064 /*
3725895Syz147064  * Add or remove one or more ports to/from an existing link aggregation.
3735895Syz147064  */
3745895Syz147064 static dladm_status_t
3755895Syz147064 i_dladm_aggr_add_rmv(datalink_id_t linkid, uint32_t nports,
3765895Syz147064     dladm_aggr_port_attr_db_t *ports, uint32_t flags, int cmd)
3775895Syz147064 {
3785895Syz147064 	char *orig_portstr = NULL, *portstr = NULL;
3796077Syz147064 	laioc_add_rem_t *iocp = NULL;
3805895Syz147064 	laioc_port_t *ioc_ports;
3815895Syz147064 	uint32_t orig_nports, result_nports, len, i, j;
3825895Syz147064 	dladm_conf_t conf;
3835895Syz147064 	datalink_class_t class;
3845895Syz147064 	dladm_status_t status = DLADM_STATUS_OK;
3855895Syz147064 	int size;
3865895Syz147064 	uint64_t u64;
3875895Syz147064 	uint32_t media;
3885895Syz147064 
3895895Syz147064 	if (nports == 0)
3905895Syz147064 		return (DLADM_STATUS_BADARG);
3915895Syz147064 
3925895Syz147064 	/*
3935895Syz147064 	 * Sanity check - aggregations can only be created over Ethernet
3945895Syz147064 	 * physical links.
3955895Syz147064 	 */
3965895Syz147064 	for (i = 0; i < nports; i++) {
3975895Syz147064 		if ((dladm_datalink_id2info(ports[i].lp_linkid, NULL,
3985895Syz147064 		    &class, &media, NULL, 0) != DLADM_STATUS_OK) ||
3995895Syz147064 		    (class != DATALINK_CLASS_PHYS) || (media != DL_ETHER)) {
4005895Syz147064 			return (DLADM_STATUS_BADARG);
4013871Syz147064 		}
4023871Syz147064 	}
4033871Syz147064 
4045895Syz147064 	/*
4055895Syz147064 	 * First, update the persistent configuration if requested.  We only
4065895Syz147064 	 * need to update the FPORTS and FNPORTS fields of this aggregation.
4075895Syz147064 	 * Note that FPORTS is a list of port linkids separated by
4085895Syz147064 	 * PORT_DELIMITER ('.').
4095895Syz147064 	 */
4105895Syz147064 	if (flags & DLADM_OPT_PERSIST) {
4115895Syz147064 		status = dladm_read_conf(linkid, &conf);
4125895Syz147064 		if (status != DLADM_STATUS_OK)
4135895Syz147064 			return (status);
4145895Syz147064 
4155895Syz147064 		/*
4165895Syz147064 		 * Get the original configuration of FNPORTS and FPORTS.
4175895Syz147064 		 */
4185895Syz147064 		status = dladm_get_conf_field(conf, FNPORTS, &u64,
4195895Syz147064 		    sizeof (u64));
4205895Syz147064 		if (status != DLADM_STATUS_OK)
4215895Syz147064 			goto destroyconf;
4225895Syz147064 		orig_nports = (uint32_t)u64;
4235895Syz147064 
4245895Syz147064 		/*
4255895Syz147064 		 * At least one port needs to be in the aggregation.
4265895Syz147064 		 */
4275895Syz147064 		if ((cmd == LAIOC_REMOVE) && (orig_nports <= nports)) {
4285895Syz147064 			status = DLADM_STATUS_BADARG;
4295895Syz147064 			goto destroyconf;
4305895Syz147064 		}
4315895Syz147064 
4325895Syz147064 		size = orig_nports * (LINKID_STR_WIDTH + 1) + 1;
4335895Syz147064 		if ((orig_portstr = calloc(1, size)) == NULL) {
4345895Syz147064 			status = dladm_errno2status(errno);
4355895Syz147064 			goto destroyconf;
4365895Syz147064 		}
4375895Syz147064 
4385895Syz147064 		status = dladm_get_conf_field(conf, FPORTS, orig_portstr, size);
4395895Syz147064 		if (status != DLADM_STATUS_OK)
4405895Syz147064 			goto destroyconf;
4415895Syz147064 
4425895Syz147064 		result_nports = (cmd == LAIOC_ADD) ? orig_nports + nports :
4435895Syz147064 		    orig_nports;
4445895Syz147064 
4455895Syz147064 		size = result_nports * (LINKID_STR_WIDTH + 1) + 1;
4465895Syz147064 		if ((portstr = calloc(1, size)) == NULL) {
4475895Syz147064 			status = dladm_errno2status(errno);
4485895Syz147064 			goto destroyconf;
4495895Syz147064 		}
4505895Syz147064 
4515895Syz147064 		/*
4525895Syz147064 		 * get the new configuration and set to result_nports and
4535895Syz147064 		 * portstr.
4545895Syz147064 		 */
4555895Syz147064 		if (cmd == LAIOC_ADD) {
4565895Syz147064 			(void) strlcpy(portstr, orig_portstr, size);
4575895Syz147064 			for (i = 0; i < nports; i++)
4585895Syz147064 				WRITE_PORT(portstr, ports[i].lp_linkid, size);
4595895Syz147064 		} else {
4605895Syz147064 			char *next;
4615895Syz147064 			datalink_id_t portid;
4625895Syz147064 			uint32_t remove = 0;
4635895Syz147064 
4645895Syz147064 			for (next = orig_portstr, j = 0; j < orig_nports; j++) {
4655895Syz147064 				/*
4665895Syz147064 				 * Read the portids from the old configuration
4675895Syz147064 				 * one by one.
4685895Syz147064 				 */
4695895Syz147064 				READ_PORT(next, portid, status);
4705895Syz147064 				if (status != DLADM_STATUS_OK) {
4715895Syz147064 					free(portstr);
4725895Syz147064 					goto destroyconf;
4735895Syz147064 				}
4745895Syz147064 
4755895Syz147064 				/*
4765895Syz147064 				 * See whether this port is in the removal
4775895Syz147064 				 * list.  If not, copy to the new config.
4785895Syz147064 				 */
4795895Syz147064 				for (i = 0; i < nports; i++) {
4805895Syz147064 					if (ports[i].lp_linkid == portid)
4815895Syz147064 						break;
4825895Syz147064 				}
4835895Syz147064 				if (i == nports) {
4845895Syz147064 					WRITE_PORT(portstr, portid, size);
4855895Syz147064 				} else {
4865895Syz147064 					remove++;
4875895Syz147064 				}
4885895Syz147064 			}
4895895Syz147064 			if (remove != nports) {
4905895Syz147064 				status = DLADM_STATUS_LINKINVAL;
4915895Syz147064 				free(portstr);
4925895Syz147064 				goto destroyconf;
4935895Syz147064 			}
4945895Syz147064 			result_nports -= nports;
4955895Syz147064 		}
4965895Syz147064 
4975895Syz147064 		u64 = result_nports;
4985895Syz147064 		if ((status = dladm_set_conf_field(conf, FNPORTS,
4995895Syz147064 		    DLADM_TYPE_UINT64, &u64)) != DLADM_STATUS_OK) {
5005895Syz147064 			free(portstr);
5015895Syz147064 			goto destroyconf;
5025895Syz147064 		}
5035895Syz147064 
5045895Syz147064 		status = dladm_set_conf_field(conf, FPORTS, DLADM_TYPE_STR,
5055895Syz147064 		    portstr);
5065895Syz147064 		free(portstr);
5075895Syz147064 		if (status != DLADM_STATUS_OK)
5085895Syz147064 			goto destroyconf;
5095895Syz147064 
5105895Syz147064 		/*
5115895Syz147064 		 * Write the new configuration to the persistent repository.
5125895Syz147064 		 */
5135895Syz147064 		status = dladm_write_conf(conf);
5145895Syz147064 
5155895Syz147064 destroyconf:
5165895Syz147064 		dladm_destroy_conf(conf);
5175895Syz147064 		if (status != DLADM_STATUS_OK) {
5185895Syz147064 			free(orig_portstr);
5195895Syz147064 			return (status);
5205895Syz147064 		}
5215895Syz147064 	}
5225895Syz147064 
5235895Syz147064 	/*
5245895Syz147064 	 * If the caller only requested to update the persistent
5255895Syz147064 	 * configuration, we are done.
5265895Syz147064 	 */
5275895Syz147064 	if (!(flags & DLADM_OPT_ACTIVE))
5285895Syz147064 		goto done;
5295895Syz147064 
5305895Syz147064 	/*
5315895Syz147064 	 * Update the active configuration.
5325895Syz147064 	 */
5335895Syz147064 	len = sizeof (*iocp) + nports * sizeof (laioc_port_t);
5345895Syz147064 	if ((iocp = malloc(len)) == NULL) {
5355895Syz147064 		status = DLADM_STATUS_NOMEM;
5363871Syz147064 		goto done;
5373871Syz147064 	}
5383871Syz147064 
5395895Syz147064 	iocp->la_linkid = linkid;
5405895Syz147064 	iocp->la_nports = nports;
5415895Syz147064 	if (cmd == LAIOC_ADD)
5425895Syz147064 		iocp->la_force = (flags & DLADM_OPT_FORCE);
5433871Syz147064 
5445895Syz147064 	ioc_ports = (laioc_port_t *)(iocp + 1);
5455895Syz147064 	for (i = 0; i < nports; i++)
5465895Syz147064 		ioc_ports[i].lp_linkid = ports[i].lp_linkid;
5475895Syz147064 
548*7408SSebastien.Roy@Sun.COM 	if (i_dladm_aggr_ioctl(cmd, iocp) < 0)
5495895Syz147064 		status = dladm_errno2status(errno);
5503871Syz147064 
5513871Syz147064 done:
5523871Syz147064 	free(iocp);
5535895Syz147064 
5545895Syz147064 	/*
5555895Syz147064 	 * If the active configuration update fails, restore the old
5565895Syz147064 	 * persistent configuration if we've changed that.
5575895Syz147064 	 */
5585895Syz147064 	if ((status != DLADM_STATUS_OK) && (flags & DLADM_OPT_PERSIST)) {
5595895Syz147064 		if (dladm_read_conf(linkid, &conf) == DLADM_STATUS_OK) {
5605895Syz147064 			u64 = orig_nports;
5615895Syz147064 			if ((dladm_set_conf_field(conf, FNPORTS,
5625895Syz147064 			    DLADM_TYPE_UINT64, &u64) == DLADM_STATUS_OK) &&
5635895Syz147064 			    (dladm_set_conf_field(conf, FPORTS, DLADM_TYPE_STR,
5645895Syz147064 			    orig_portstr) == DLADM_STATUS_OK)) {
5655895Syz147064 				(void) dladm_write_conf(conf);
5665895Syz147064 			}
5675895Syz147064 			(void) dladm_destroy_conf(conf);
5685895Syz147064 		}
5695895Syz147064 	}
5705895Syz147064 	free(orig_portstr);
5713871Syz147064 	return (status);
5723871Syz147064 }
5733871Syz147064 
5743871Syz147064 /*
5753871Syz147064  * Send a modify command to the link aggregation driver.
5763871Syz147064  */
5773871Syz147064 static dladm_status_t
5785895Syz147064 i_dladm_aggr_modify_sys(datalink_id_t linkid, uint32_t mask,
5793871Syz147064     dladm_aggr_modify_attr_t *attr)
5803871Syz147064 {
5813871Syz147064 	laioc_modify_t ioc;
5823871Syz147064 
5835895Syz147064 	ioc.lu_linkid = linkid;
5843871Syz147064 
5853871Syz147064 	ioc.lu_modify_mask = 0;
5863871Syz147064 	if (mask & DLADM_AGGR_MODIFY_POLICY)
5873871Syz147064 		ioc.lu_modify_mask |= LAIOC_MODIFY_POLICY;
5883871Syz147064 	if (mask & DLADM_AGGR_MODIFY_MAC)
5893871Syz147064 		ioc.lu_modify_mask |= LAIOC_MODIFY_MAC;
5903871Syz147064 	if (mask & DLADM_AGGR_MODIFY_LACP_MODE)
5913871Syz147064 		ioc.lu_modify_mask |= LAIOC_MODIFY_LACP_MODE;
5923871Syz147064 	if (mask & DLADM_AGGR_MODIFY_LACP_TIMER)
5933871Syz147064 		ioc.lu_modify_mask |= LAIOC_MODIFY_LACP_TIMER;
5943871Syz147064 
5953871Syz147064 	ioc.lu_policy = attr->ld_policy;
5963871Syz147064 	ioc.lu_mac_fixed = attr->ld_mac_fixed;
5973871Syz147064 	bcopy(attr->ld_mac, ioc.lu_mac, ETHERADDRL);
5983871Syz147064 	ioc.lu_lacp_mode = attr->ld_lacp_mode;
5993871Syz147064 	ioc.lu_lacp_timer = attr->ld_lacp_timer;
6003871Syz147064 
601*7408SSebastien.Roy@Sun.COM 	if (i_dladm_aggr_ioctl(LAIOC_MODIFY, &ioc) < 0) {
6023871Syz147064 		if (errno == EINVAL)
6035895Syz147064 			return (DLADM_STATUS_MACADDRINVAL);
6043871Syz147064 		else
6055895Syz147064 			return (dladm_errno2status(errno));
6065895Syz147064 	} else {
6075895Syz147064 		return (DLADM_STATUS_OK);
6083871Syz147064 	}
6093871Syz147064 }
6103871Syz147064 
6113871Syz147064 /*
6123871Syz147064  * Send a create command to the link aggregation driver.
6133871Syz147064  */
6143871Syz147064 static dladm_status_t
6155895Syz147064 i_dladm_aggr_create_sys(datalink_id_t linkid, uint16_t key, uint32_t nports,
6165895Syz147064     dladm_aggr_port_attr_db_t *ports, uint32_t policy,
6175895Syz147064     boolean_t mac_addr_fixed, const uchar_t *mac_addr,
6185895Syz147064     aggr_lacp_mode_t lacp_mode, aggr_lacp_timer_t lacp_timer, boolean_t force)
6193871Syz147064 {
620*7408SSebastien.Roy@Sun.COM 	int i, len;
6215895Syz147064 	laioc_create_t *iocp = NULL;
6225895Syz147064 	laioc_port_t *ioc_ports;
6233871Syz147064 	dladm_status_t status = DLADM_STATUS_OK;
6243871Syz147064 
6255895Syz147064 	len = sizeof (*iocp) + nports * sizeof (laioc_port_t);
6263871Syz147064 	iocp = malloc(len);
6273871Syz147064 	if (iocp == NULL)
6283871Syz147064 		return (DLADM_STATUS_NOMEM);
6293871Syz147064 
6305895Syz147064 	iocp->lc_key = key;
6315895Syz147064 	iocp->lc_linkid = linkid;
6325895Syz147064 	iocp->lc_nports = nports;
6335895Syz147064 	iocp->lc_policy = policy;
6345895Syz147064 	iocp->lc_lacp_mode = lacp_mode;
6355895Syz147064 	iocp->lc_lacp_timer = lacp_timer;
6365895Syz147064 	ioc_ports = (laioc_port_t *)(iocp + 1);
6375895Syz147064 	iocp->lc_force = force;
6383871Syz147064 
6395895Syz147064 	for (i = 0; i < nports; i++)
6405895Syz147064 		ioc_ports[i].lp_linkid = ports[i].lp_linkid;
6415895Syz147064 
6425895Syz147064 	if (mac_addr_fixed && !VALID_PORT_MAC(mac_addr)) {
6435895Syz147064 		status = DLADM_STATUS_MACADDRINVAL;
6445895Syz147064 		goto done;
6453871Syz147064 	}
6463871Syz147064 
6475895Syz147064 	bcopy(mac_addr, iocp->lc_mac, ETHERADDRL);
6485895Syz147064 	iocp->lc_mac_fixed = mac_addr_fixed;
6493871Syz147064 
650*7408SSebastien.Roy@Sun.COM 	if (i_dladm_aggr_ioctl(LAIOC_CREATE, iocp) < 0)
6515895Syz147064 		status = dladm_errno2status(errno);
6523871Syz147064 
6535895Syz147064 done:
6543871Syz147064 	free(iocp);
6553871Syz147064 	return (status);
6563871Syz147064 }
6573871Syz147064 
6583871Syz147064 /*
6593871Syz147064  * Invoked to bring up a link aggregation group.
6603871Syz147064  */
6615895Syz147064 static int
6625895Syz147064 i_dladm_aggr_up(datalink_id_t linkid, void *arg)
6633871Syz147064 {
6645895Syz147064 	dladm_status_t *statusp = (dladm_status_t *)arg;
6655895Syz147064 	dladm_aggr_grp_attr_t attr;
6665895Syz147064 	dladm_aggr_port_attr_db_t *ports = NULL;
6675895Syz147064 	uint16_t key = 0;
6685895Syz147064 	int i, j;
6693871Syz147064 	dladm_status_t status;
6703871Syz147064 
6715895Syz147064 	status = dladm_aggr_info(linkid, &attr, DLADM_OPT_PERSIST);
6725895Syz147064 	if (status != DLADM_STATUS_OK) {
6735895Syz147064 		*statusp = status;
6745895Syz147064 		return (DLADM_WALK_CONTINUE);
6755895Syz147064 	}
6763871Syz147064 
6775895Syz147064 	if (attr.lg_key <= AGGR_MAX_KEY)
6785895Syz147064 		key = attr.lg_key;
6795895Syz147064 
6805895Syz147064 	ports = malloc(attr.lg_nports * sizeof (dladm_aggr_port_attr_db_t));
6815895Syz147064 	if (ports == NULL) {
6825895Syz147064 		status = DLADM_STATUS_NOMEM;
6835895Syz147064 		goto done;
6843871Syz147064 	}
6853871Syz147064 
6863871Syz147064 	/*
6875895Syz147064 	 * Validate (and purge) each physical link associated with this
6885895Syz147064 	 * aggregation, if the specific hardware has been removed during
6895895Syz147064 	 * the system shutdown.
6903871Syz147064 	 */
6915895Syz147064 	for (i = 0, j = 0; i < attr.lg_nports; i++) {
6925895Syz147064 		datalink_id_t	portid = attr.lg_ports[i].lp_linkid;
6935895Syz147064 		uint32_t	flags;
6945895Syz147064 		dladm_status_t	s;
6953871Syz147064 
6965895Syz147064 		s = dladm_datalink_id2info(portid, &flags, NULL, NULL, NULL, 0);
6975895Syz147064 		if (s != DLADM_STATUS_OK || !(flags & DLADM_OPT_ACTIVE))
6985895Syz147064 			continue;
6995895Syz147064 
7005895Syz147064 		ports[j++].lp_linkid = portid;
7015895Syz147064 	}
7023871Syz147064 
7035895Syz147064 	if (j == 0) {
7045895Syz147064 		/*
7055895Syz147064 		 * All of the physical links are removed.
7065895Syz147064 		 */
7075895Syz147064 		status = DLADM_STATUS_BADARG;
7085895Syz147064 		goto done;
7093871Syz147064 	}
7103871Syz147064 
7115895Syz147064 	/*
7125895Syz147064 	 * Create active aggregation.
7135895Syz147064 	 */
7145895Syz147064 	if ((status = i_dladm_aggr_create_sys(linkid,
7155895Syz147064 	    key, j, ports, attr.lg_policy, attr.lg_mac_fixed,
7165895Syz147064 	    (const uchar_t *)attr.lg_mac, attr.lg_lacp_mode,
7175895Syz147064 	    attr.lg_lacp_timer, attr.lg_force)) != DLADM_STATUS_OK) {
7185895Syz147064 		goto done;
7195895Syz147064 	}
7203871Syz147064 
7215895Syz147064 	if ((status = dladm_up_datalink_id(linkid)) != DLADM_STATUS_OK) {
7225895Syz147064 		laioc_delete_t ioc;
7235895Syz147064 		ioc.ld_linkid = linkid;
724*7408SSebastien.Roy@Sun.COM 		(void) i_dladm_aggr_ioctl(LAIOC_DELETE, &ioc);
7255895Syz147064 		goto done;
7265895Syz147064 	}
7273871Syz147064 
7283871Syz147064 	/*
7295895Syz147064 	 * Reset the active linkprop of this specific link.
7303871Syz147064 	 */
7316916Sartem 	(void) dladm_init_linkprop(linkid, B_FALSE);
7323871Syz147064 
7335895Syz147064 done:
7345895Syz147064 	free(attr.lg_ports);
7355895Syz147064 	free(ports);
7365895Syz147064 
7375895Syz147064 	*statusp = status;
7385895Syz147064 	return (DLADM_WALK_CONTINUE);
7393871Syz147064 }
7403871Syz147064 
7413871Syz147064 /*
7425895Syz147064  * Bring up one aggregation, or all persistent aggregations.  In the latter
7435895Syz147064  * case, the walk may terminate early if bringup of an aggregation fails.
7443871Syz147064  */
7455895Syz147064 dladm_status_t
7465895Syz147064 dladm_aggr_up(datalink_id_t linkid)
7473871Syz147064 {
7483871Syz147064 	dladm_status_t status;
7493871Syz147064 
7505895Syz147064 	if (linkid == DATALINK_ALL_LINKID) {
7515895Syz147064 		(void) dladm_walk_datalink_id(i_dladm_aggr_up, &status,
7525895Syz147064 		    DATALINK_CLASS_AGGR, DATALINK_ANY_MEDIATYPE,
7535895Syz147064 		    DLADM_OPT_PERSIST);
7545895Syz147064 		return (DLADM_STATUS_OK);
7555895Syz147064 	} else {
7565895Syz147064 		(void) i_dladm_aggr_up(linkid, &status);
7573871Syz147064 		return (status);
7583871Syz147064 	}
7593871Syz147064 }
7603871Syz147064 
7613871Syz147064 /*
7623871Syz147064  * Given a policy string, return a policy mask. Returns B_TRUE on
7635895Syz147064  * success, or B_FALSE if an error occurred during parsing.
7643871Syz147064  */
7653871Syz147064 boolean_t
7663871Syz147064 dladm_aggr_str2policy(const char *str, uint32_t *policy)
7673871Syz147064 {
7683871Syz147064 	int i;
7693871Syz147064 	policy_t *pol;
7703871Syz147064 	char *token = NULL;
7713871Syz147064 	char *lasts;
7723871Syz147064 
7733871Syz147064 	*policy = 0;
7743871Syz147064 
7753871Syz147064 	while ((token = strtok_r((token == NULL) ? (char *)str : NULL, ",",
7763871Syz147064 	    &lasts)) != NULL) {
7773871Syz147064 		for (i = 0; i < NPOLICIES; i++) {
7783871Syz147064 			pol = &policies[i];
7793871Syz147064 			if (strcasecmp(token, pol->pol_name) == 0) {
7803871Syz147064 				*policy |= pol->policy;
7813871Syz147064 				break;
7823871Syz147064 			}
7833871Syz147064 		}
7843871Syz147064 		if (i == NPOLICIES)
7853871Syz147064 			return (B_FALSE);
7863871Syz147064 	}
7873871Syz147064 
7883871Syz147064 	return (B_TRUE);
7893871Syz147064 }
7903871Syz147064 
7913871Syz147064 /*
7923871Syz147064  * Given a policy mask, returns a printable string, or NULL if the
7933871Syz147064  * policy mask is invalid. It is the responsibility of the caller to
7943871Syz147064  * free the returned string after use.
7953871Syz147064  */
7963871Syz147064 char *
7973871Syz147064 dladm_aggr_policy2str(uint32_t policy, char *str)
7983871Syz147064 {
7993871Syz147064 	int i, npolicies = 0;
8003871Syz147064 	policy_t *pol;
8013871Syz147064 
8025895Syz147064 	if (str == NULL)
8035895Syz147064 		return (NULL);
8045895Syz147064 
8053871Syz147064 	str[0] = '\0';
8063871Syz147064 
8073871Syz147064 	for (i = 0; i < NPOLICIES; i++) {
8083871Syz147064 		pol = &policies[i];
8093871Syz147064 		if ((policy & pol->policy) != 0) {
8103871Syz147064 			npolicies++;
8113871Syz147064 			if (npolicies > 1)
8125895Syz147064 				(void) strlcat(str, ",", DLADM_STRSIZE);
8135895Syz147064 			(void) strlcat(str, pol->pol_name, DLADM_STRSIZE);
8143871Syz147064 		}
8153871Syz147064 	}
8163871Syz147064 
8173871Syz147064 	return (str);
8183871Syz147064 }
8193871Syz147064 
8203871Syz147064 /*
8213871Syz147064  * Given a MAC address string, return the MAC address in the mac_addr
8223871Syz147064  * array. If the MAC address was not explicitly specified, i.e. is
8233871Syz147064  * equal to 'auto', zero out mac-addr and set mac_fixed to B_TRUE.
8243871Syz147064  * Return B_FALSE if a syntax error was encountered, B_FALSE otherwise.
8253871Syz147064  */
8263871Syz147064 boolean_t
8273871Syz147064 dladm_aggr_str2macaddr(const char *str, boolean_t *mac_fixed, uchar_t *mac_addr)
8283871Syz147064 {
8293871Syz147064 	uchar_t *conv_str;
8303871Syz147064 	int mac_len;
8313871Syz147064 
8323871Syz147064 	*mac_fixed = (strcmp(str, "auto") != 0);
8333871Syz147064 	if (!*mac_fixed) {
8343871Syz147064 		bzero(mac_addr, ETHERADDRL);
8353871Syz147064 		return (B_TRUE);
8363871Syz147064 	}
8373871Syz147064 
8383871Syz147064 	conv_str = _link_aton(str, &mac_len);
8393871Syz147064 	if (conv_str == NULL)
8403871Syz147064 		return (B_FALSE);
8413871Syz147064 
8423871Syz147064 	if (mac_len != ETHERADDRL) {
8433871Syz147064 		free(conv_str);
8443871Syz147064 		return (B_FALSE);
8453871Syz147064 	}
8463871Syz147064 
8473871Syz147064 	if ((bcmp(zero_mac, conv_str, ETHERADDRL) == 0) ||
8483871Syz147064 	    (conv_str[0] & 0x01)) {
8493871Syz147064 		free(conv_str);
8503871Syz147064 		return (B_FALSE);
8513871Syz147064 	}
8523871Syz147064 
8533871Syz147064 	bcopy(conv_str, mac_addr, ETHERADDRL);
8543871Syz147064 	free(conv_str);
8553871Syz147064 
8563871Syz147064 	return (B_TRUE);
8573871Syz147064 }
8583871Syz147064 
8593871Syz147064 /*
8603871Syz147064  * Returns a string containing a printable representation of a MAC address.
8613871Syz147064  */
8623871Syz147064 const char *
8635895Syz147064 dladm_aggr_macaddr2str(const unsigned char *mac, char *buf)
8643871Syz147064 {
8653871Syz147064 	static char unknown_mac[] = {0, 0, 0, 0, 0, 0};
8663871Syz147064 
8673871Syz147064 	if (buf == NULL)
8683871Syz147064 		return (NULL);
8693871Syz147064 
8703871Syz147064 	if (bcmp(unknown_mac, mac, ETHERADDRL) == 0)
8715895Syz147064 		(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
8723871Syz147064 	else
8733871Syz147064 		return (_link_ntoa(mac, buf, ETHERADDRL, IFT_OTHER));
8745895Syz147064 
8755895Syz147064 	return (buf);
8763871Syz147064 }
8773871Syz147064 
8783871Syz147064 /*
8793871Syz147064  * Given a LACP mode string, find the corresponding LACP mode number. Returns
8803871Syz147064  * B_TRUE if a match was found, B_FALSE otherwise.
8813871Syz147064  */
8823871Syz147064 boolean_t
8833871Syz147064 dladm_aggr_str2lacpmode(const char *str, aggr_lacp_mode_t *lacp_mode)
8843871Syz147064 {
8853871Syz147064 	int i;
8863871Syz147064 	dladm_aggr_lacpmode_t *mode;
8873871Syz147064 
8883871Syz147064 	for (i = 0; i < NLACP_MODES; i++) {
8893871Syz147064 		mode = &lacp_modes[i];
8903871Syz147064 		if (strncasecmp(str, mode->mode_str,
8913871Syz147064 		    strlen(mode->mode_str)) == 0) {
8923871Syz147064 			*lacp_mode = mode->mode_id;
8933871Syz147064 			return (B_TRUE);
8943871Syz147064 		}
8953871Syz147064 	}
8963871Syz147064 
8973871Syz147064 	return (B_FALSE);
8983871Syz147064 }
8993871Syz147064 
9003871Syz147064 /*
9013871Syz147064  * Given a LACP mode number, returns a printable string, or NULL if the
9023871Syz147064  * LACP mode number is invalid.
9033871Syz147064  */
9043871Syz147064 const char *
9053871Syz147064 dladm_aggr_lacpmode2str(aggr_lacp_mode_t mode_id, char *buf)
9063871Syz147064 {
9073871Syz147064 	int i;
9083871Syz147064 	dladm_aggr_lacpmode_t *mode;
9093871Syz147064 
9105895Syz147064 	if (buf == NULL)
9115895Syz147064 		return (NULL);
9125895Syz147064 
9133871Syz147064 	for (i = 0; i < NLACP_MODES; i++) {
9143871Syz147064 		mode = &lacp_modes[i];
9153871Syz147064 		if (mode->mode_id == mode_id) {
9163871Syz147064 			(void) snprintf(buf, DLADM_STRSIZE, "%s",
9173871Syz147064 			    mode->mode_str);
9183871Syz147064 			return (buf);
9193871Syz147064 		}
9203871Syz147064 	}
9213871Syz147064 
9223871Syz147064 	(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
9233871Syz147064 	return (buf);
9243871Syz147064 }
9253871Syz147064 
9263871Syz147064 /*
9273871Syz147064  * Given a LACP timer string, find the corresponding LACP timer number. Returns
9283871Syz147064  * B_TRUE if a match was found, B_FALSE otherwise.
9293871Syz147064  */
9303871Syz147064 boolean_t
9313871Syz147064 dladm_aggr_str2lacptimer(const char *str, aggr_lacp_timer_t *lacp_timer)
9323871Syz147064 {
9333871Syz147064 	int i;
9343871Syz147064 	dladm_aggr_lacptimer_t *timer;
9353871Syz147064 
9363871Syz147064 	for (i = 0; i < NLACP_TIMERS; i++) {
9373871Syz147064 		timer = &lacp_timers[i];
9383871Syz147064 		if (strncasecmp(str, timer->lt_str,
9393871Syz147064 		    strlen(timer->lt_str)) == 0) {
9403871Syz147064 			*lacp_timer = timer->lt_id;
9413871Syz147064 			return (B_TRUE);
9423871Syz147064 		}
9433871Syz147064 	}
9443871Syz147064 
9453871Syz147064 	return (B_FALSE);
9463871Syz147064 }
9473871Syz147064 
9483871Syz147064 /*
9493871Syz147064  * Given a LACP timer, returns a printable string, or NULL if the
9503871Syz147064  * LACP timer number is invalid.
9513871Syz147064  */
9523871Syz147064 const char *
9533871Syz147064 dladm_aggr_lacptimer2str(aggr_lacp_timer_t timer_id, char *buf)
9543871Syz147064 {
9553871Syz147064 	int i;
9563871Syz147064 	dladm_aggr_lacptimer_t *timer;
9573871Syz147064 
9585895Syz147064 	if (buf == NULL)
9595895Syz147064 		return (NULL);
9605895Syz147064 
9613871Syz147064 	for (i = 0; i < NLACP_TIMERS; i++) {
9623871Syz147064 		timer = &lacp_timers[i];
9633871Syz147064 		if (timer->lt_id == timer_id) {
9643871Syz147064 			(void) snprintf(buf, DLADM_STRSIZE, "%s",
9653871Syz147064 			    timer->lt_str);
9663871Syz147064 			return (buf);
9673871Syz147064 		}
9683871Syz147064 	}
9693871Syz147064 
9703871Syz147064 	(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
9713871Syz147064 	return (buf);
9723871Syz147064 }
9733871Syz147064 
9743871Syz147064 const char *
9753871Syz147064 dladm_aggr_portstate2str(aggr_port_state_t state_id, char *buf)
9763871Syz147064 {
9773871Syz147064 	int			i;
9785895Syz147064 	dladm_aggr_port_state_t *state;
9795895Syz147064 
9805895Syz147064 	if (buf == NULL)
9815895Syz147064 		return (NULL);
9823871Syz147064 
9833871Syz147064 	for (i = 0; i < NPORT_STATES; i++) {
9843871Syz147064 		state = &port_states[i];
9853871Syz147064 		if (state->state_id == state_id) {
9863871Syz147064 			(void) snprintf(buf, DLADM_STRSIZE, "%s",
9873871Syz147064 			    state->state_str);
9883871Syz147064 			return (buf);
9893871Syz147064 		}
9903871Syz147064 	}
9913871Syz147064 
9923871Syz147064 	(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
9933871Syz147064 	return (buf);
9943871Syz147064 }
9953871Syz147064 
9965895Syz147064 static dladm_status_t
9975895Syz147064 dladm_aggr_persist_aggr_conf(const char *link, datalink_id_t linkid,
9985895Syz147064     uint16_t key, uint32_t nports, dladm_aggr_port_attr_db_t *ports,
9995895Syz147064     uint32_t policy, boolean_t mac_addr_fixed, const uchar_t *mac_addr,
10005895Syz147064     aggr_lacp_mode_t lacp_mode, aggr_lacp_timer_t lacp_timer,
10015895Syz147064     boolean_t force)
10023871Syz147064 {
10035895Syz147064 	dladm_conf_t conf = DLADM_INVALID_CONF;
10045895Syz147064 	char *portstr = NULL;
10055895Syz147064 	char macstr[ETHERADDRL * 3];
10065895Syz147064 	dladm_status_t status;
10075895Syz147064 	int i, size;
10085895Syz147064 	uint64_t u64;
10093871Syz147064 
10105895Syz147064 	if ((status = dladm_create_conf(link, linkid, DATALINK_CLASS_AGGR,
10115895Syz147064 	    DL_ETHER, &conf)) != DLADM_STATUS_OK) {
10123871Syz147064 		return (status);
10133871Syz147064 	}
10143871Syz147064 
10155895Syz147064 	u64 = key;
10165895Syz147064 	status = dladm_set_conf_field(conf, FKEY, DLADM_TYPE_UINT64, &u64);
10175895Syz147064 	if (status != DLADM_STATUS_OK)
10185895Syz147064 		goto done;
10193871Syz147064 
10205895Syz147064 	u64 = nports;
10215895Syz147064 	status = dladm_set_conf_field(conf, FNPORTS, DLADM_TYPE_UINT64, &u64);
10225895Syz147064 	if (status != DLADM_STATUS_OK)
10235895Syz147064 		goto done;
10245895Syz147064 
10255895Syz147064 	size = nports * (LINKID_STR_WIDTH + 1) + 1;
10265895Syz147064 	if ((portstr = calloc(1, size)) == NULL) {
10275895Syz147064 		status = DLADM_STATUS_NOMEM;
10285895Syz147064 		goto done;
10295895Syz147064 	}
10303871Syz147064 
10315895Syz147064 	for (i = 0; i < nports; i++)
10325895Syz147064 		WRITE_PORT(portstr, ports[i].lp_linkid, size);
10335895Syz147064 	status = dladm_set_conf_field(conf, FPORTS, DLADM_TYPE_STR, portstr);
10345895Syz147064 	free(portstr);
10355895Syz147064 
10365895Syz147064 	if (status != DLADM_STATUS_OK)
10375895Syz147064 		goto done;
10383871Syz147064 
10395895Syz147064 	u64 = policy;
10405895Syz147064 	status = dladm_set_conf_field(conf, FPOLICY, DLADM_TYPE_UINT64, &u64);
10415895Syz147064 	if (status != DLADM_STATUS_OK)
10425895Syz147064 		goto done;
10435895Syz147064 
10445895Syz147064 	status = dladm_set_conf_field(conf, FFIXMACADDR, DLADM_TYPE_BOOLEAN,
10455895Syz147064 	    &mac_addr_fixed);
10465895Syz147064 	if (status != DLADM_STATUS_OK)
10475895Syz147064 		goto done;
10485895Syz147064 
10495895Syz147064 	if (mac_addr_fixed) {
10505895Syz147064 		if (!VALID_PORT_MAC(mac_addr)) {
10515895Syz147064 			status = DLADM_STATUS_MACADDRINVAL;
10523871Syz147064 			goto done;
10533871Syz147064 		}
10543871Syz147064 
10555895Syz147064 		(void) dladm_aggr_macaddr2str(mac_addr, macstr);
10565895Syz147064 		status = dladm_set_conf_field(conf, FMACADDR, DLADM_TYPE_STR,
10575895Syz147064 		    macstr);
10585895Syz147064 		if (status != DLADM_STATUS_OK)
10593871Syz147064 			goto done;
10603871Syz147064 	}
10613871Syz147064 
10625895Syz147064 	status = dladm_set_conf_field(conf, FFORCE, DLADM_TYPE_BOOLEAN, &force);
10635895Syz147064 	if (status != DLADM_STATUS_OK)
10645895Syz147064 		goto done;
10655895Syz147064 
10665895Syz147064 	u64 = lacp_mode;
10675895Syz147064 	status = dladm_set_conf_field(conf, FLACPMODE, DLADM_TYPE_UINT64, &u64);
10685895Syz147064 	if (status != DLADM_STATUS_OK)
10695895Syz147064 		goto done;
10705895Syz147064 
10715895Syz147064 	u64 = lacp_timer;
10725895Syz147064 	status = dladm_set_conf_field(conf, FLACPTIMER, DLADM_TYPE_UINT64,
10735895Syz147064 	    &u64);
10745895Syz147064 	if (status != DLADM_STATUS_OK)
10755895Syz147064 		goto done;
10765895Syz147064 
10773871Syz147064 	/*
10785895Syz147064 	 * Commit the link aggregation configuration.
10793871Syz147064 	 */
10805895Syz147064 	status = dladm_write_conf(conf);
10813871Syz147064 
10823871Syz147064 done:
10835895Syz147064 	dladm_destroy_conf(conf);
10843871Syz147064 	return (status);
10853871Syz147064 }
10863871Syz147064 
10873871Syz147064 /*
10883871Syz147064  * Create a new link aggregation group. Update the configuration
10893871Syz147064  * file and bring it up.
10903871Syz147064  */
10913871Syz147064 dladm_status_t
10925895Syz147064 dladm_aggr_create(const char *name, uint16_t key, uint32_t nports,
10933871Syz147064     dladm_aggr_port_attr_db_t *ports, uint32_t policy, boolean_t mac_addr_fixed,
10945895Syz147064     const uchar_t *mac_addr, aggr_lacp_mode_t lacp_mode,
10955895Syz147064     aggr_lacp_timer_t lacp_timer, uint32_t flags)
10963871Syz147064 {
10975895Syz147064 	datalink_id_t linkid = DATALINK_INVALID_LINKID;
10985895Syz147064 	uint32_t media;
10995895Syz147064 	uint32_t i;
11005895Syz147064 	datalink_class_t class;
11013871Syz147064 	dladm_status_t status;
11025895Syz147064 	boolean_t force = (flags & DLADM_OPT_FORCE) ? B_TRUE : B_FALSE;
11033871Syz147064 
11045895Syz147064 	if (key != 0 && key > AGGR_MAX_KEY)
11053871Syz147064 		return (DLADM_STATUS_KEYINVAL);
11063871Syz147064 
11075895Syz147064 	if (nports == 0)
11085895Syz147064 		return (DLADM_STATUS_BADARG);
11095895Syz147064 
11105895Syz147064 	for (i = 0; i < nports; i++) {
11115895Syz147064 		if ((dladm_datalink_id2info(ports[i].lp_linkid, NULL,
11125895Syz147064 		    &class, &media, NULL, 0) != DLADM_STATUS_OK) ||
11135895Syz147064 		    (class != DATALINK_CLASS_PHYS) && (media != DL_ETHER)) {
11145895Syz147064 			return (DLADM_STATUS_BADARG);
11155895Syz147064 		}
11165895Syz147064 	}
11175895Syz147064 
11185895Syz147064 	flags &= (DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST);
11195895Syz147064 	if ((status = dladm_create_datalink_id(name, DATALINK_CLASS_AGGR,
11205895Syz147064 	    DL_ETHER, flags, &linkid)) != DLADM_STATUS_OK) {
11215895Syz147064 		goto fail;
11225895Syz147064 	}
11235895Syz147064 
11245895Syz147064 	if ((flags & DLADM_OPT_PERSIST) &&
11255895Syz147064 	    (status = dladm_aggr_persist_aggr_conf(name, linkid, key, nports,
11265895Syz147064 	    ports, policy, mac_addr_fixed, mac_addr, lacp_mode, lacp_timer,
11275895Syz147064 	    force)) != DLADM_STATUS_OK) {
11285895Syz147064 		goto fail;
11295895Syz147064 	}
11305895Syz147064 
11315895Syz147064 	if (!(flags & DLADM_OPT_ACTIVE))
11325895Syz147064 		return (DLADM_STATUS_OK);
11335895Syz147064 
11345895Syz147064 	status = i_dladm_aggr_create_sys(linkid, key, nports, ports, policy,
11355895Syz147064 	    mac_addr_fixed, mac_addr, lacp_mode, lacp_timer, force);
11363871Syz147064 
11375895Syz147064 	if (status != DLADM_STATUS_OK) {
11385895Syz147064 		if (flags & DLADM_OPT_PERSIST)
11395895Syz147064 			(void) dladm_remove_conf(linkid);
11405895Syz147064 		goto fail;
11415895Syz147064 	}
11425895Syz147064 
11435895Syz147064 	return (DLADM_STATUS_OK);
11445895Syz147064 
11455895Syz147064 fail:
11465895Syz147064 	if (linkid != DATALINK_INVALID_LINKID)
11475895Syz147064 		(void) dladm_destroy_datalink_id(linkid, flags);
11485895Syz147064 
11495895Syz147064 	return (status);
11505895Syz147064 }
11515895Syz147064 
11525895Syz147064 static dladm_status_t
11535895Syz147064 i_dladm_aggr_get_aggr_attr(dladm_conf_t conf, uint32_t mask,
11545895Syz147064     dladm_aggr_modify_attr_t *attrp)
11555895Syz147064 {
11565895Syz147064 	dladm_status_t status = DLADM_STATUS_OK;
11575895Syz147064 	char macstr[ETHERADDRL * 3];
11585895Syz147064 	uint64_t u64;
11595895Syz147064 
11605895Syz147064 	if (mask & DLADM_AGGR_MODIFY_POLICY) {
11615895Syz147064 		status = dladm_get_conf_field(conf, FPOLICY, &u64,
11625895Syz147064 		    sizeof (u64));
11635895Syz147064 		if (status != DLADM_STATUS_OK)
11645895Syz147064 			return (status);
11655895Syz147064 		attrp->ld_policy = (uint32_t)u64;
11665895Syz147064 	}
11675895Syz147064 
11685895Syz147064 	if (mask & DLADM_AGGR_MODIFY_MAC) {
11695895Syz147064 		status = dladm_get_conf_field(conf, FFIXMACADDR,
11705895Syz147064 		    &attrp->ld_mac_fixed, sizeof (boolean_t));
11713871Syz147064 		if (status != DLADM_STATUS_OK)
11723871Syz147064 			return (status);
11735895Syz147064 
11745895Syz147064 		if (attrp->ld_mac_fixed) {
11755895Syz147064 			boolean_t fixed;
11765895Syz147064 
11775895Syz147064 			status = dladm_get_conf_field(conf, FMACADDR,
11785895Syz147064 			    macstr, sizeof (macstr));
11795895Syz147064 			if (status != DLADM_STATUS_OK)
11805895Syz147064 				return (status);
11813871Syz147064 
11825895Syz147064 			if (!dladm_aggr_str2macaddr(macstr, &fixed,
11835895Syz147064 			    attrp->ld_mac)) {
11845895Syz147064 				return (DLADM_STATUS_REPOSITORYINVAL);
11855895Syz147064 			}
11865895Syz147064 		}
11875895Syz147064 	}
11883871Syz147064 
11895895Syz147064 	if (mask & DLADM_AGGR_MODIFY_LACP_MODE) {
11905895Syz147064 		status = dladm_get_conf_field(conf, FLACPMODE, &u64,
11915895Syz147064 		    sizeof (u64));
11925895Syz147064 		if (status != DLADM_STATUS_OK)
11935895Syz147064 			return (status);
11945895Syz147064 		attrp->ld_lacp_mode = (aggr_lacp_mode_t)u64;
11955895Syz147064 	}
11965895Syz147064 
11975895Syz147064 	if (mask & DLADM_AGGR_MODIFY_LACP_TIMER) {
11985895Syz147064 		status = dladm_get_conf_field(conf, FLACPTIMER, &u64,
11995895Syz147064 		    sizeof (u64));
12005895Syz147064 		if (status != DLADM_STATUS_OK)
12015895Syz147064 			return (status);
12025895Syz147064 		attrp->ld_lacp_timer = (aggr_lacp_timer_t)u64;
12033871Syz147064 	}
12043871Syz147064 
12055895Syz147064 	return (status);
12065895Syz147064 }
12075895Syz147064 
12085895Syz147064 static dladm_status_t
12095895Syz147064 i_dladm_aggr_set_aggr_attr(dladm_conf_t conf, uint32_t mask,
12105895Syz147064     dladm_aggr_modify_attr_t *attrp)
12115895Syz147064 {
12125895Syz147064 	dladm_status_t status = DLADM_STATUS_OK;
12135895Syz147064 	char macstr[ETHERADDRL * 3];
12145895Syz147064 	uint64_t u64;
12155895Syz147064 
12165895Syz147064 	if (mask & DLADM_AGGR_MODIFY_POLICY) {
12175895Syz147064 		u64 = attrp->ld_policy;
12185895Syz147064 		status = dladm_set_conf_field(conf, FPOLICY, DLADM_TYPE_UINT64,
12195895Syz147064 		    &u64);
12205895Syz147064 		if (status != DLADM_STATUS_OK)
12215895Syz147064 			return (status);
12225895Syz147064 	}
12235895Syz147064 
12245895Syz147064 	if (mask & DLADM_AGGR_MODIFY_MAC) {
12255895Syz147064 		status = dladm_set_conf_field(conf, FFIXMACADDR,
12265895Syz147064 		    DLADM_TYPE_BOOLEAN, &attrp->ld_mac_fixed);
12275895Syz147064 		if (status != DLADM_STATUS_OK)
12285895Syz147064 			return (status);
12293871Syz147064 
12305895Syz147064 		if (attrp->ld_mac_fixed) {
12315895Syz147064 			(void) dladm_aggr_macaddr2str(attrp->ld_mac, macstr);
12325895Syz147064 			status = dladm_set_conf_field(conf, FMACADDR,
12335895Syz147064 			    DLADM_TYPE_STR, macstr);
12345895Syz147064 			if (status != DLADM_STATUS_OK)
12355895Syz147064 				return (status);
12365895Syz147064 		}
12375895Syz147064 	}
12385895Syz147064 
12395895Syz147064 	if (mask & DLADM_AGGR_MODIFY_LACP_MODE) {
12405895Syz147064 		u64 = attrp->ld_lacp_mode;
12415895Syz147064 		status = dladm_set_conf_field(conf, FLACPMODE,
12425895Syz147064 		    DLADM_TYPE_UINT64, &u64);
12435895Syz147064 		if (status != DLADM_STATUS_OK)
12445895Syz147064 			return (status);
12455895Syz147064 	}
12465895Syz147064 
12475895Syz147064 	if (mask & DLADM_AGGR_MODIFY_LACP_TIMER) {
12485895Syz147064 		u64 = attrp->ld_lacp_timer;
12495895Syz147064 		status = dladm_set_conf_field(conf, FLACPTIMER,
12505895Syz147064 		    DLADM_TYPE_UINT64, &u64);
12515895Syz147064 		if (status != DLADM_STATUS_OK)
12525895Syz147064 			return (status);
12535895Syz147064 	}
12543871Syz147064 
12553871Syz147064 	return (status);
12563871Syz147064 }
12573871Syz147064 
12583871Syz147064 /*
12593871Syz147064  * Modify the parameters of an existing link aggregation group. Update
12603871Syz147064  * the configuration file and pass the changes to the kernel.
12613871Syz147064  */
12623871Syz147064 dladm_status_t
12635895Syz147064 dladm_aggr_modify(datalink_id_t linkid, uint32_t modify_mask, uint32_t policy,
12645895Syz147064     boolean_t mac_fixed, const uchar_t *mac_addr, aggr_lacp_mode_t lacp_mode,
12655895Syz147064     aggr_lacp_timer_t lacp_timer, uint32_t flags)
12663871Syz147064 {
12673871Syz147064 	dladm_aggr_modify_attr_t new_attr, old_attr;
12685895Syz147064 	dladm_conf_t conf;
12693871Syz147064 	dladm_status_t status;
12703871Syz147064 
12715895Syz147064 	new_attr.ld_policy = policy;
12725895Syz147064 	new_attr.ld_mac_fixed = mac_fixed;
12735895Syz147064 	new_attr.ld_lacp_mode = lacp_mode;
12745895Syz147064 	new_attr.ld_lacp_timer = lacp_timer;
12755895Syz147064 	bcopy(mac_addr, new_attr.ld_mac, ETHERADDRL);
12765895Syz147064 
12775895Syz147064 	if (flags & DLADM_OPT_PERSIST) {
12785895Syz147064 		status = dladm_read_conf(linkid, &conf);
12795895Syz147064 		if (status != DLADM_STATUS_OK)
12805895Syz147064 			return (status);
12813871Syz147064 
12825895Syz147064 		if ((status = i_dladm_aggr_get_aggr_attr(conf, modify_mask,
12835895Syz147064 		    &old_attr)) != DLADM_STATUS_OK) {
12845895Syz147064 			goto done;
12855895Syz147064 		}
12863871Syz147064 
12875895Syz147064 		if ((status = i_dladm_aggr_set_aggr_attr(conf, modify_mask,
12885895Syz147064 		    &new_attr)) != DLADM_STATUS_OK) {
12895895Syz147064 			goto done;
12905895Syz147064 		}
12915895Syz147064 
12925895Syz147064 		status = dladm_write_conf(conf);
12935895Syz147064 
12945895Syz147064 done:
12955895Syz147064 		dladm_destroy_conf(conf);
12965895Syz147064 		if (status != DLADM_STATUS_OK)
12975895Syz147064 			return (status);
12983871Syz147064 	}
12993871Syz147064 
13005895Syz147064 	if (!(flags & DLADM_OPT_ACTIVE))
13015895Syz147064 		return (DLADM_STATUS_OK);
13023871Syz147064 
13035895Syz147064 	status = i_dladm_aggr_modify_sys(linkid, modify_mask, &new_attr);
13045895Syz147064 	if ((status != DLADM_STATUS_OK) && (flags & DLADM_OPT_PERSIST)) {
13055895Syz147064 		if (dladm_read_conf(linkid, &conf) == DLADM_STATUS_OK) {
13065895Syz147064 			if (i_dladm_aggr_set_aggr_attr(conf, modify_mask,
13075895Syz147064 			    &old_attr) == DLADM_STATUS_OK) {
13085895Syz147064 				(void) dladm_write_conf(conf);
13095895Syz147064 			}
13105895Syz147064 			dladm_destroy_conf(conf);
13115895Syz147064 		}
13123871Syz147064 	}
13133871Syz147064 
13143871Syz147064 	return (status);
13153871Syz147064 }
13163871Syz147064 
13175895Syz147064 typedef struct aggr_held_arg_s {
13185895Syz147064 	datalink_id_t	aggrid;
13195895Syz147064 	boolean_t	isheld;
13205895Syz147064 } aggr_held_arg_t;
13215895Syz147064 
13225895Syz147064 static int
13235895Syz147064 i_dladm_aggr_is_held(datalink_id_t linkid, void *arg)
13245895Syz147064 {
13255895Syz147064 	aggr_held_arg_t		*aggr_held_arg = arg;
13265895Syz147064 	dladm_vlan_attr_t	dva;
13275895Syz147064 
13285895Syz147064 	if (dladm_vlan_info(linkid, &dva, DLADM_OPT_PERSIST) != DLADM_STATUS_OK)
13295895Syz147064 		return (DLADM_WALK_CONTINUE);
13305895Syz147064 
13315895Syz147064 	if (dva.dv_linkid == aggr_held_arg->aggrid) {
13325895Syz147064 		/*
13335895Syz147064 		 * This VLAN is created over the given aggregation.
13345895Syz147064 		 */
13355895Syz147064 		aggr_held_arg->isheld = B_TRUE;
13365895Syz147064 		return (DLADM_WALK_TERMINATE);
13375895Syz147064 	}
13385895Syz147064 	return (DLADM_WALK_CONTINUE);
13395895Syz147064 }
13405895Syz147064 
13413871Syz147064 /*
13425895Syz147064  * Delete a previously created link aggregation group. Either the name "aggr"
13435895Syz147064  * or the "key" is specified.
13443871Syz147064  */
13453871Syz147064 dladm_status_t
13465895Syz147064 dladm_aggr_delete(datalink_id_t linkid, uint32_t flags)
13473871Syz147064 {
13485895Syz147064 	laioc_delete_t ioc;
13495895Syz147064 	datalink_class_t class;
13503871Syz147064 	dladm_status_t status;
13513871Syz147064 
13525895Syz147064 	if ((dladm_datalink_id2info(linkid, NULL, &class, NULL, NULL, 0) !=
13535895Syz147064 	    DLADM_STATUS_OK) || (class != DATALINK_CLASS_AGGR)) {
13545895Syz147064 		return (DLADM_STATUS_BADARG);
13555895Syz147064 	}
13563871Syz147064 
13575895Syz147064 	if (flags & DLADM_OPT_ACTIVE) {
13585895Syz147064 		ioc.ld_linkid = linkid;
1359*7408SSebastien.Roy@Sun.COM 		if ((i_dladm_aggr_ioctl(LAIOC_DELETE, &ioc) < 0) &&
13605895Syz147064 		    ((errno != ENOENT) || !(flags & DLADM_OPT_PERSIST))) {
13615895Syz147064 			status = dladm_errno2status(errno);
13625895Syz147064 			return (status);
13635895Syz147064 		}
13643871Syz147064 
13653871Syz147064 		/*
13665895Syz147064 		 * Delete ACTIVE linkprop first.
13673871Syz147064 		 */
13685895Syz147064 		(void) dladm_set_linkprop(linkid, NULL, NULL, 0,
13695895Syz147064 		    DLADM_OPT_ACTIVE);
13705895Syz147064 		(void) dladm_destroy_datalink_id(linkid, DLADM_OPT_ACTIVE);
13713871Syz147064 	}
13723871Syz147064 
13735895Syz147064 	/*
13745895Syz147064 	 * If we reach here, it means that the active aggregation has already
13755895Syz147064 	 * been deleted, and there is no active VLANs holding this aggregation.
13765895Syz147064 	 * Now we see whether there is any persistent VLANs holding this
13775895Syz147064 	 * aggregation. If so, we fail the operation.
13785895Syz147064 	 */
13795895Syz147064 	if (flags & DLADM_OPT_PERSIST) {
13805895Syz147064 		aggr_held_arg_t arg;
13815895Syz147064 
13825895Syz147064 		arg.aggrid = linkid;
13835895Syz147064 		arg.isheld = B_FALSE;
13843871Syz147064 
13855895Syz147064 		(void) dladm_walk_datalink_id(i_dladm_aggr_is_held,
13865895Syz147064 		    &arg, DATALINK_CLASS_VLAN, DATALINK_ANY_MEDIATYPE,
13875895Syz147064 		    DLADM_OPT_PERSIST);
13885895Syz147064 		if (arg.isheld)
13895895Syz147064 			return (DLADM_STATUS_LINKBUSY);
13905895Syz147064 
13915895Syz147064 		(void) dladm_destroy_datalink_id(linkid, DLADM_OPT_PERSIST);
13925895Syz147064 		(void) dladm_remove_conf(linkid);
13935895Syz147064 	}
13945895Syz147064 
13955895Syz147064 	return (DLADM_STATUS_OK);
13963871Syz147064 }
13973871Syz147064 
13983871Syz147064 /*
13993871Syz147064  * Add one or more ports to an existing link aggregation.
14003871Syz147064  */
14013871Syz147064 dladm_status_t
14025895Syz147064 dladm_aggr_add(datalink_id_t linkid, uint32_t nports,
14035895Syz147064     dladm_aggr_port_attr_db_t *ports, uint32_t flags)
14043871Syz147064 {
14055895Syz147064 	return (i_dladm_aggr_add_rmv(linkid, nports, ports, flags, LAIOC_ADD));
14063871Syz147064 }
14073871Syz147064 
14083871Syz147064 /*
14093871Syz147064  * Remove one or more ports from an existing link aggregation.
14103871Syz147064  */
14113871Syz147064 dladm_status_t
14125895Syz147064 dladm_aggr_remove(datalink_id_t linkid, uint32_t nports,
14135895Syz147064     dladm_aggr_port_attr_db_t *ports, uint32_t flags)
14143871Syz147064 {
14155895Syz147064 	return (i_dladm_aggr_add_rmv(linkid, nports, ports, flags,
14165895Syz147064 	    LAIOC_REMOVE));
14175895Syz147064 }
14183871Syz147064 
14195895Syz147064 typedef struct i_walk_key_state_s {
14205895Syz147064 	uint16_t key;
14215895Syz147064 	datalink_id_t linkid;
14225895Syz147064 	boolean_t found;
14235895Syz147064 } i_walk_key_state_t;
14243871Syz147064 
14255895Syz147064 static int
14265895Syz147064 i_dladm_walk_key2linkid(datalink_id_t linkid, void *arg)
14275895Syz147064 {
14285895Syz147064 	dladm_conf_t conf;
14295895Syz147064 	uint16_t key;
14305895Syz147064 	dladm_status_t status;
14315895Syz147064 	i_walk_key_state_t *statep = (i_walk_key_state_t *)arg;
14325895Syz147064 	uint64_t u64;
14333871Syz147064 
14345895Syz147064 	if (dladm_read_conf(linkid, &conf) != 0)
14355895Syz147064 		return (DLADM_WALK_CONTINUE);
14365895Syz147064 
14375895Syz147064 	status = dladm_get_conf_field(conf, FKEY, &u64, sizeof (u64));
14385895Syz147064 	key = (uint16_t)u64;
14395895Syz147064 	dladm_destroy_conf(conf);
14405895Syz147064 
14415895Syz147064 	if ((status == DLADM_STATUS_OK) && (key == statep->key)) {
14425895Syz147064 		statep->found = B_TRUE;
14435895Syz147064 		statep->linkid = linkid;
14445895Syz147064 		return (DLADM_WALK_TERMINATE);
14453871Syz147064 	}
14463871Syz147064 
14475895Syz147064 	return (DLADM_WALK_CONTINUE);
14485895Syz147064 }
14495895Syz147064 
14505895Syz147064 dladm_status_t
14515895Syz147064 dladm_key2linkid(uint16_t key, datalink_id_t *linkidp, uint32_t flags)
14525895Syz147064 {
14535895Syz147064 	i_walk_key_state_t state;
14545895Syz147064 
14555895Syz147064 	if (key > AGGR_MAX_KEY)
14565895Syz147064 		return (DLADM_STATUS_NOTFOUND);
14573871Syz147064 
14585895Syz147064 	state.found = B_FALSE;
14595895Syz147064 	state.key = key;
14605895Syz147064 
14615895Syz147064 	(void) dladm_walk_datalink_id(i_dladm_walk_key2linkid, &state,
14625895Syz147064 	    DATALINK_CLASS_AGGR, DATALINK_ANY_MEDIATYPE, flags);
14635895Syz147064 	if (state.found == B_TRUE) {
14645895Syz147064 		*linkidp = state.linkid;
14655895Syz147064 		return (DLADM_STATUS_OK);
14665895Syz147064 	} else {
14675895Syz147064 		return (DLADM_STATUS_NOTFOUND);
14685895Syz147064 	}
14693871Syz147064 }
1470