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>
40*8275SEric Cheng #include <sys/dld.h>
415895Syz147064 #include <libdllink.h>
425895Syz147064 #include <libdlvlan.h>
433871Syz147064 #include <libdlaggr.h>
443871Syz147064 #include <libdladm_impl.h>
453871Syz147064 
463871Syz147064 /*
473871Syz147064  * Link Aggregation Administration Library.
483871Syz147064  *
493871Syz147064  * This library is used by administration tools such as dladm(1M) to
503871Syz147064  * configure link aggregations.
513871Syz147064  */
523871Syz147064 
533871Syz147064 /* Limits on buffer size for LAIOC_INFO request */
543871Syz147064 #define	MIN_INFO_SIZE (4*1024)
553871Syz147064 #define	MAX_INFO_SIZE (128*1024)
563871Syz147064 
575895Syz147064 static uchar_t	zero_mac[] = {0, 0, 0, 0, 0, 0};
585895Syz147064 #define	VALID_PORT_MAC(mac)						\
595895Syz147064 	(((mac) != NULL) && (bcmp(zero_mac, (mac), ETHERADDRL) != 0) &&	\
605895Syz147064 	(!(mac)[0] & 0x01))
613871Syz147064 
625895Syz147064 #define	PORT_DELIMITER	'.'
633871Syz147064 
645895Syz147064 #define	WRITE_PORT(portstr, portid, size) {			\
655895Syz147064 	char pstr[LINKID_STR_WIDTH + 2];			\
665895Syz147064 	(void) snprintf(pstr, LINKID_STR_WIDTH + 2, "%d%c",	\
675895Syz147064 	    (portid), PORT_DELIMITER);				\
685895Syz147064 	(void) strlcat((portstr), pstr, (size));		\
695895Syz147064 }
703871Syz147064 
715895Syz147064 #define	READ_PORT(portstr, portid, status) {			\
725895Syz147064 	errno = 0;						\
735895Syz147064 	(status) = DLADM_STATUS_OK;				\
745895Syz147064 	(portid) = (int)strtol((portstr), &(portstr), 10);	\
755895Syz147064 	if (errno != 0 || *(portstr) != PORT_DELIMITER) {	\
765895Syz147064 		(status) = DLADM_STATUS_REPOSITORYINVAL;	\
775895Syz147064 	} else {						\
785895Syz147064 		/* Skip the delimiter. */			\
795895Syz147064 		(portstr)++;					\
805895Syz147064 	}							\
815895Syz147064 }
823871Syz147064 
833871Syz147064 typedef struct dladm_aggr_modify_attr {
843871Syz147064 	uint32_t	ld_policy;
853871Syz147064 	boolean_t	ld_mac_fixed;
863871Syz147064 	uchar_t		ld_mac[ETHERADDRL];
873871Syz147064 	aggr_lacp_mode_t ld_lacp_mode;
883871Syz147064 	aggr_lacp_timer_t ld_lacp_timer;
893871Syz147064 } dladm_aggr_modify_attr_t;
903871Syz147064 
913871Syz147064 typedef struct policy_s {
923871Syz147064 	char		*pol_name;
933871Syz147064 	uint32_t	policy;
943871Syz147064 } policy_t;
953871Syz147064 
963871Syz147064 static policy_t policies[] = {
973871Syz147064 	{"L2",		AGGR_POLICY_L2},
983871Syz147064 	{"L3",		AGGR_POLICY_L3},
993871Syz147064 	{"L4",		AGGR_POLICY_L4}};
1003871Syz147064 
1013871Syz147064 #define	NPOLICIES	(sizeof (policies) / sizeof (policy_t))
1023871Syz147064 
1033871Syz147064 typedef struct dladm_aggr_lacpmode_s {
1043871Syz147064 	char		*mode_str;
1053871Syz147064 	aggr_lacp_mode_t mode_id;
1063871Syz147064 } dladm_aggr_lacpmode_t;
1073871Syz147064 
1083871Syz147064 static dladm_aggr_lacpmode_t lacp_modes[] = {
1093871Syz147064 	{"off", AGGR_LACP_OFF},
1103871Syz147064 	{"active", AGGR_LACP_ACTIVE},
1113871Syz147064 	{"passive", AGGR_LACP_PASSIVE}};
1123871Syz147064 
1133871Syz147064 #define	NLACP_MODES	(sizeof (lacp_modes) / sizeof (dladm_aggr_lacpmode_t))
1143871Syz147064 
1153871Syz147064 typedef struct dladm_aggr_lacptimer_s {
1163871Syz147064 	char		*lt_str;
1173871Syz147064 	aggr_lacp_timer_t lt_id;
1183871Syz147064 } dladm_aggr_lacptimer_t;
1193871Syz147064 
1203871Syz147064 static dladm_aggr_lacptimer_t lacp_timers[] = {
1213871Syz147064 	{"short", AGGR_LACP_TIMER_SHORT},
1223871Syz147064 	{"long", AGGR_LACP_TIMER_LONG}};
1233871Syz147064 
1243871Syz147064 #define	NLACP_TIMERS	(sizeof (lacp_timers) / sizeof (dladm_aggr_lacptimer_t))
1253871Syz147064 
1263871Syz147064 typedef struct dladm_aggr_port_state {
1273871Syz147064 	char			*state_str;
1283871Syz147064 	aggr_port_state_t	state_id;
1293871Syz147064 } dladm_aggr_port_state_t;
1303871Syz147064 
1313871Syz147064 static dladm_aggr_port_state_t port_states[] = {
1323871Syz147064 	{"standby", AGGR_PORT_STATE_STANDBY },
1333871Syz147064 	{"attached", AGGR_PORT_STATE_ATTACHED }
1343871Syz147064 };
1353871Syz147064 
1363871Syz147064 #define	NPORT_STATES	\
1373871Syz147064 	(sizeof (port_states) / sizeof (dladm_aggr_port_state_t))
1383871Syz147064 
1395895Syz147064 static int
1407408SSebastien.Roy@Sun.COM i_dladm_aggr_ioctl(int cmd, void *ptr)
1415895Syz147064 {
1425895Syz147064 	int err, fd;
1433871Syz147064 
1447408SSebastien.Roy@Sun.COM 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
1453871Syz147064 		return (-1);
1463871Syz147064 
1477408SSebastien.Roy@Sun.COM 	err = ioctl(fd, cmd, ptr);
1485895Syz147064 	(void) close(fd);
1493871Syz147064 
1505895Syz147064 	return (err);
1513871Syz147064 }
1523871Syz147064 
1533871Syz147064 /*
1545895Syz147064  * Caller must free attr.lg_ports. The ptr pointer is advanced while convert
1555895Syz147064  * the laioc_info_t to the dladm_aggr_grp_attr_t structure.
1563871Syz147064  */
1575895Syz147064 static int
1585895Syz147064 i_dladm_aggr_iocp2grpattr(void **ptr, dladm_aggr_grp_attr_t *attrp)
1593871Syz147064 {
1605895Syz147064 	laioc_info_group_t	*grp;
1615895Syz147064 	laioc_info_port_t	*port;
1625895Syz147064 	int			i;
1635895Syz147064 	void			*where = (*ptr);
1645895Syz147064 
1655895Syz147064 	grp = (laioc_info_group_t *)where;
1665895Syz147064 
1675895Syz147064 	attrp->lg_linkid = grp->lg_linkid;
1685895Syz147064 	attrp->lg_key = grp->lg_key;
1695895Syz147064 	attrp->lg_nports = grp->lg_nports;
1705895Syz147064 	attrp->lg_policy = grp->lg_policy;
1715895Syz147064 	attrp->lg_lacp_mode = grp->lg_lacp_mode;
1725895Syz147064 	attrp->lg_lacp_timer = grp->lg_lacp_timer;
1735895Syz147064 	attrp->lg_force = grp->lg_force;
1745895Syz147064 
1755895Syz147064 	bcopy(grp->lg_mac, attrp->lg_mac, ETHERADDRL);
1765895Syz147064 	attrp->lg_mac_fixed = grp->lg_mac_fixed;
1773871Syz147064 
1785895Syz147064 	if ((attrp->lg_ports = malloc(grp->lg_nports *
1795895Syz147064 	    sizeof (dladm_aggr_port_attr_t))) == NULL) {
1805895Syz147064 		errno = ENOMEM;
1815895Syz147064 		goto fail;
1825895Syz147064 	}
1835895Syz147064 
1845895Syz147064 	where = (grp + 1);
1853871Syz147064 
1865895Syz147064 	/*
1875895Syz147064 	 * Go through each port that is part of the group.
1885895Syz147064 	 */
1895895Syz147064 	for (i = 0; i < grp->lg_nports; i++) {
1905895Syz147064 		port = (laioc_info_port_t *)where;
1913871Syz147064 
1925895Syz147064 		attrp->lg_ports[i].lp_linkid = port->lp_linkid;
1935895Syz147064 		bcopy(port->lp_mac, attrp->lg_ports[i].lp_mac, ETHERADDRL);
1945895Syz147064 		attrp->lg_ports[i].lp_state = port->lp_state;
1955895Syz147064 		attrp->lg_ports[i].lp_lacp_state = port->lp_lacp_state;
1965895Syz147064 
1975895Syz147064 		where = (port + 1);
1985895Syz147064 	}
1995895Syz147064 	*ptr = where;
2005895Syz147064 	return (0);
2015895Syz147064 fail:
2025895Syz147064 	return (-1);
2033871Syz147064 }
2043871Syz147064 
2053871Syz147064 /*
2065895Syz147064  * Get active configuration of a specific aggregation.
2075895Syz147064  * Caller must free attrp->la_ports.
2083871Syz147064  */
2095895Syz147064 static dladm_status_t
2105895Syz147064 i_dladm_aggr_info_active(datalink_id_t linkid, dladm_aggr_grp_attr_t *attrp)
2113871Syz147064 {
2123871Syz147064 	laioc_info_t *ioc;
2137408SSebastien.Roy@Sun.COM 	int bufsize;
2145895Syz147064 	void *where;
2155895Syz147064 	dladm_status_t status = DLADM_STATUS_OK;
2163871Syz147064 
2173871Syz147064 	bufsize = MIN_INFO_SIZE;
2183871Syz147064 	ioc = (laioc_info_t *)calloc(1, bufsize);
2195895Syz147064 	if (ioc == NULL)
2205895Syz147064 		return (DLADM_STATUS_NOMEM);
2215895Syz147064 
2225895Syz147064 	ioc->li_group_linkid = linkid;
2233871Syz147064 
2243871Syz147064 tryagain:
2257408SSebastien.Roy@Sun.COM 	ioc->li_bufsize = bufsize;
2267408SSebastien.Roy@Sun.COM 	if (i_dladm_aggr_ioctl(LAIOC_INFO, ioc) != 0) {
2273871Syz147064 		if (errno == ENOSPC) {
2283871Syz147064 			/*
2293871Syz147064 			 * The LAIOC_INFO call failed due to a short
2303871Syz147064 			 * buffer. Reallocate the buffer and try again.
2313871Syz147064 			 */
2323871Syz147064 			bufsize *= 2;
2333871Syz147064 			if (bufsize <= MAX_INFO_SIZE) {
2343871Syz147064 				ioc = (laioc_info_t *)realloc(ioc, bufsize);
2353871Syz147064 				if (ioc != NULL) {
2363871Syz147064 					bzero(ioc, sizeof (bufsize));
2373871Syz147064 					goto tryagain;
2383871Syz147064 				}
2393871Syz147064 			}
2403871Syz147064 		}
2415895Syz147064 		status = dladm_errno2status(errno);
2423871Syz147064 		goto bail;
2433871Syz147064 	}
2443871Syz147064 
2453871Syz147064 	/*
2463871Syz147064 	 * Go through each group returned by the aggregation driver.
2473871Syz147064 	 */
2483871Syz147064 	where = (char *)(ioc + 1);
2495895Syz147064 	if (i_dladm_aggr_iocp2grpattr(&where, attrp) != 0) {
2505895Syz147064 		status = dladm_errno2status(errno);
2515895Syz147064 		goto bail;
2523871Syz147064 	}
2533871Syz147064 
2543871Syz147064 bail:
2553871Syz147064 	free(ioc);
2563871Syz147064 	return (status);
2573871Syz147064 }
2583871Syz147064 
2593871Syz147064 /*
2605895Syz147064  * Get configuration information of a specific aggregation.
2615895Syz147064  * Caller must free attrp->la_ports.
2623871Syz147064  */
2633871Syz147064 static dladm_status_t
2645895Syz147064 i_dladm_aggr_info_persist(datalink_id_t linkid, dladm_aggr_grp_attr_t *attrp)
2653871Syz147064 {
2665895Syz147064 	dladm_conf_t	conf;
2675895Syz147064 	uint32_t	nports, i;
2685895Syz147064 	char		*portstr, *next;
2695895Syz147064 	dladm_status_t	status;
2705895Syz147064 	uint64_t	u64;
2715895Syz147064 	int		size;
2725895Syz147064 	char		macstr[ETHERADDRL * 3];
2735895Syz147064 
2745895Syz147064 	attrp->lg_linkid = linkid;
2755895Syz147064 	if ((status = dladm_read_conf(linkid, &conf)) != DLADM_STATUS_OK)
2765895Syz147064 		return (status);
2775895Syz147064 
2785895Syz147064 	status = dladm_get_conf_field(conf, FKEY, &u64, sizeof (u64));
2795895Syz147064 	if (status != DLADM_STATUS_OK)
2805895Syz147064 		goto done;
2815895Syz147064 	attrp->lg_key = (uint16_t)u64;
2825895Syz147064 
2835895Syz147064 	status = dladm_get_conf_field(conf, FPOLICY, &u64, sizeof (u64));
2845895Syz147064 	if (status != DLADM_STATUS_OK)
2855895Syz147064 		goto done;
2865895Syz147064 	attrp->lg_policy = (uint32_t)u64;
2875895Syz147064 
2885895Syz147064 	status = dladm_get_conf_field(conf, FFIXMACADDR, &attrp->lg_mac_fixed,
2895895Syz147064 	    sizeof (boolean_t));
2905895Syz147064 	if (status != DLADM_STATUS_OK)
2915895Syz147064 		goto done;
2925895Syz147064 
2935895Syz147064 	if (attrp->lg_mac_fixed) {
2945895Syz147064 		boolean_t fixed;
2953871Syz147064 
2965895Syz147064 		if ((status = dladm_get_conf_field(conf, FMACADDR, macstr,
2975895Syz147064 		    sizeof (macstr))) != DLADM_STATUS_OK) {
2985895Syz147064 			goto done;
2995895Syz147064 		}
3005895Syz147064 		if (!dladm_aggr_str2macaddr(macstr, &fixed, attrp->lg_mac)) {
3015895Syz147064 			status = DLADM_STATUS_REPOSITORYINVAL;
3025895Syz147064 			goto done;
3035895Syz147064 		}
3045895Syz147064 	}
3055895Syz147064 
3065895Syz147064 	status = dladm_get_conf_field(conf, FFORCE, &attrp->lg_force,
3075895Syz147064 	    sizeof (boolean_t));
3085895Syz147064 	if (status != DLADM_STATUS_OK)
3095895Syz147064 		goto done;
3105895Syz147064 
3115895Syz147064 	status = dladm_get_conf_field(conf, FLACPMODE, &u64, sizeof (u64));
3125895Syz147064 	if (status != DLADM_STATUS_OK)
3135895Syz147064 		goto done;
3145895Syz147064 	attrp->lg_lacp_mode = (aggr_lacp_mode_t)u64;
3155895Syz147064 
3165895Syz147064 	status = dladm_get_conf_field(conf, FLACPTIMER, &u64, sizeof (u64));
3175895Syz147064 	if (status != DLADM_STATUS_OK)
3185895Syz147064 		goto done;
3195895Syz147064 	attrp->lg_lacp_timer = (aggr_lacp_timer_t)u64;
3205895Syz147064 
3215895Syz147064 	status = dladm_get_conf_field(conf, FNPORTS, &u64, sizeof (u64));
3225895Syz147064 	if (status != DLADM_STATUS_OK)
3235895Syz147064 		goto done;
3245895Syz147064 	nports = (uint32_t)u64;
3255895Syz147064 	attrp->lg_nports = nports;
3265895Syz147064 
3275895Syz147064 	size = nports * (LINKID_STR_WIDTH + 1) + 1;
3285895Syz147064 	if ((portstr = calloc(1, size)) == NULL) {
3295895Syz147064 		status = DLADM_STATUS_NOMEM;
3305895Syz147064 		goto done;
3315895Syz147064 	}
3325895Syz147064 
3335895Syz147064 	status = dladm_get_conf_field(conf, FPORTS, portstr, size);
3345895Syz147064 	if (status != DLADM_STATUS_OK) {
3355895Syz147064 		free(portstr);
3365895Syz147064 		goto done;
3375895Syz147064 	}
3385895Syz147064 
3395895Syz147064 	if ((attrp->lg_ports = malloc(nports *
3405895Syz147064 	    sizeof (dladm_aggr_port_attr_t))) == NULL) {
3415895Syz147064 		free(portstr);
3423871Syz147064 		status = DLADM_STATUS_NOMEM;
3433871Syz147064 		goto done;
3443871Syz147064 	}
3453871Syz147064 
3465895Syz147064 	for (next = portstr, i = 0; i < nports; i++) {
3475895Syz147064 		READ_PORT(next, attrp->lg_ports[i].lp_linkid, status);
3485895Syz147064 		if (status != DLADM_STATUS_OK) {
3495895Syz147064 			free(portstr);
3505895Syz147064 			free(attrp->lg_ports);
3515895Syz147064 			goto done;
3525895Syz147064 		}
3535895Syz147064 	}
3545895Syz147064 	free(portstr);
3555895Syz147064 
3565895Syz147064 done:
3575895Syz147064 	dladm_destroy_conf(conf);
3585895Syz147064 	return (status);
3595895Syz147064 }
3605895Syz147064 
3615895Syz147064 dladm_status_t
3625895Syz147064 dladm_aggr_info(datalink_id_t linkid, dladm_aggr_grp_attr_t *attrp,
3635895Syz147064     uint32_t flags)
3645895Syz147064 {
3655895Syz147064 	assert(flags == DLADM_OPT_ACTIVE || flags == DLADM_OPT_PERSIST);
3665895Syz147064 	if (flags == DLADM_OPT_ACTIVE)
3675895Syz147064 		return (i_dladm_aggr_info_active(linkid, attrp));
3685895Syz147064 	else
3695895Syz147064 		return (i_dladm_aggr_info_persist(linkid, attrp));
3705895Syz147064 }
3713871Syz147064 
3725895Syz147064 /*
3735895Syz147064  * Add or remove one or more ports to/from an existing link aggregation.
3745895Syz147064  */
3755895Syz147064 static dladm_status_t
3765895Syz147064 i_dladm_aggr_add_rmv(datalink_id_t linkid, uint32_t nports,
3775895Syz147064     dladm_aggr_port_attr_db_t *ports, uint32_t flags, int cmd)
3785895Syz147064 {
3795895Syz147064 	char *orig_portstr = NULL, *portstr = NULL;
3806077Syz147064 	laioc_add_rem_t *iocp = NULL;
3815895Syz147064 	laioc_port_t *ioc_ports;
3825895Syz147064 	uint32_t orig_nports, result_nports, len, i, j;
3835895Syz147064 	dladm_conf_t conf;
3845895Syz147064 	datalink_class_t class;
3855895Syz147064 	dladm_status_t status = DLADM_STATUS_OK;
3865895Syz147064 	int size;
3875895Syz147064 	uint64_t u64;
3885895Syz147064 	uint32_t media;
3895895Syz147064 
3905895Syz147064 	if (nports == 0)
3915895Syz147064 		return (DLADM_STATUS_BADARG);
3925895Syz147064 
3935895Syz147064 	/*
3945895Syz147064 	 * Sanity check - aggregations can only be created over Ethernet
3955895Syz147064 	 * physical links.
3965895Syz147064 	 */
3975895Syz147064 	for (i = 0; i < nports; i++) {
3985895Syz147064 		if ((dladm_datalink_id2info(ports[i].lp_linkid, NULL,
3995895Syz147064 		    &class, &media, NULL, 0) != DLADM_STATUS_OK) ||
4005895Syz147064 		    (class != DATALINK_CLASS_PHYS) || (media != DL_ETHER)) {
4015895Syz147064 			return (DLADM_STATUS_BADARG);
4023871Syz147064 		}
4033871Syz147064 	}
4043871Syz147064 
4055895Syz147064 	/*
4065895Syz147064 	 * First, update the persistent configuration if requested.  We only
4075895Syz147064 	 * need to update the FPORTS and FNPORTS fields of this aggregation.
4085895Syz147064 	 * Note that FPORTS is a list of port linkids separated by
4095895Syz147064 	 * PORT_DELIMITER ('.').
4105895Syz147064 	 */
4115895Syz147064 	if (flags & DLADM_OPT_PERSIST) {
4125895Syz147064 		status = dladm_read_conf(linkid, &conf);
4135895Syz147064 		if (status != DLADM_STATUS_OK)
4145895Syz147064 			return (status);
4155895Syz147064 
4165895Syz147064 		/*
4175895Syz147064 		 * Get the original configuration of FNPORTS and FPORTS.
4185895Syz147064 		 */
4195895Syz147064 		status = dladm_get_conf_field(conf, FNPORTS, &u64,
4205895Syz147064 		    sizeof (u64));
4215895Syz147064 		if (status != DLADM_STATUS_OK)
4225895Syz147064 			goto destroyconf;
4235895Syz147064 		orig_nports = (uint32_t)u64;
4245895Syz147064 
4255895Syz147064 		/*
4265895Syz147064 		 * At least one port needs to be in the aggregation.
4275895Syz147064 		 */
4285895Syz147064 		if ((cmd == LAIOC_REMOVE) && (orig_nports <= nports)) {
4295895Syz147064 			status = DLADM_STATUS_BADARG;
4305895Syz147064 			goto destroyconf;
4315895Syz147064 		}
4325895Syz147064 
4335895Syz147064 		size = orig_nports * (LINKID_STR_WIDTH + 1) + 1;
4345895Syz147064 		if ((orig_portstr = calloc(1, size)) == NULL) {
4355895Syz147064 			status = dladm_errno2status(errno);
4365895Syz147064 			goto destroyconf;
4375895Syz147064 		}
4385895Syz147064 
4395895Syz147064 		status = dladm_get_conf_field(conf, FPORTS, orig_portstr, size);
4405895Syz147064 		if (status != DLADM_STATUS_OK)
4415895Syz147064 			goto destroyconf;
4425895Syz147064 
4435895Syz147064 		result_nports = (cmd == LAIOC_ADD) ? orig_nports + nports :
4445895Syz147064 		    orig_nports;
4455895Syz147064 
4465895Syz147064 		size = result_nports * (LINKID_STR_WIDTH + 1) + 1;
4475895Syz147064 		if ((portstr = calloc(1, size)) == NULL) {
4485895Syz147064 			status = dladm_errno2status(errno);
4495895Syz147064 			goto destroyconf;
4505895Syz147064 		}
4515895Syz147064 
4525895Syz147064 		/*
4535895Syz147064 		 * get the new configuration and set to result_nports and
4545895Syz147064 		 * portstr.
4555895Syz147064 		 */
4565895Syz147064 		if (cmd == LAIOC_ADD) {
4575895Syz147064 			(void) strlcpy(portstr, orig_portstr, size);
4585895Syz147064 			for (i = 0; i < nports; i++)
4595895Syz147064 				WRITE_PORT(portstr, ports[i].lp_linkid, size);
4605895Syz147064 		} else {
4615895Syz147064 			char *next;
4625895Syz147064 			datalink_id_t portid;
4635895Syz147064 			uint32_t remove = 0;
4645895Syz147064 
4655895Syz147064 			for (next = orig_portstr, j = 0; j < orig_nports; j++) {
4665895Syz147064 				/*
4675895Syz147064 				 * Read the portids from the old configuration
4685895Syz147064 				 * one by one.
4695895Syz147064 				 */
4705895Syz147064 				READ_PORT(next, portid, status);
4715895Syz147064 				if (status != DLADM_STATUS_OK) {
4725895Syz147064 					free(portstr);
4735895Syz147064 					goto destroyconf;
4745895Syz147064 				}
4755895Syz147064 
4765895Syz147064 				/*
4775895Syz147064 				 * See whether this port is in the removal
4785895Syz147064 				 * list.  If not, copy to the new config.
4795895Syz147064 				 */
4805895Syz147064 				for (i = 0; i < nports; i++) {
4815895Syz147064 					if (ports[i].lp_linkid == portid)
4825895Syz147064 						break;
4835895Syz147064 				}
4845895Syz147064 				if (i == nports) {
4855895Syz147064 					WRITE_PORT(portstr, portid, size);
4865895Syz147064 				} else {
4875895Syz147064 					remove++;
4885895Syz147064 				}
4895895Syz147064 			}
4905895Syz147064 			if (remove != nports) {
4915895Syz147064 				status = DLADM_STATUS_LINKINVAL;
4925895Syz147064 				free(portstr);
4935895Syz147064 				goto destroyconf;
4945895Syz147064 			}
4955895Syz147064 			result_nports -= nports;
4965895Syz147064 		}
4975895Syz147064 
4985895Syz147064 		u64 = result_nports;
4995895Syz147064 		if ((status = dladm_set_conf_field(conf, FNPORTS,
5005895Syz147064 		    DLADM_TYPE_UINT64, &u64)) != DLADM_STATUS_OK) {
5015895Syz147064 			free(portstr);
5025895Syz147064 			goto destroyconf;
5035895Syz147064 		}
5045895Syz147064 
5055895Syz147064 		status = dladm_set_conf_field(conf, FPORTS, DLADM_TYPE_STR,
5065895Syz147064 		    portstr);
5075895Syz147064 		free(portstr);
5085895Syz147064 		if (status != DLADM_STATUS_OK)
5095895Syz147064 			goto destroyconf;
5105895Syz147064 
5115895Syz147064 		/*
5125895Syz147064 		 * Write the new configuration to the persistent repository.
5135895Syz147064 		 */
5145895Syz147064 		status = dladm_write_conf(conf);
5155895Syz147064 
5165895Syz147064 destroyconf:
5175895Syz147064 		dladm_destroy_conf(conf);
5185895Syz147064 		if (status != DLADM_STATUS_OK) {
5195895Syz147064 			free(orig_portstr);
5205895Syz147064 			return (status);
5215895Syz147064 		}
5225895Syz147064 	}
5235895Syz147064 
5245895Syz147064 	/*
5255895Syz147064 	 * If the caller only requested to update the persistent
5265895Syz147064 	 * configuration, we are done.
5275895Syz147064 	 */
5285895Syz147064 	if (!(flags & DLADM_OPT_ACTIVE))
5295895Syz147064 		goto done;
5305895Syz147064 
5315895Syz147064 	/*
5325895Syz147064 	 * Update the active configuration.
5335895Syz147064 	 */
5345895Syz147064 	len = sizeof (*iocp) + nports * sizeof (laioc_port_t);
5355895Syz147064 	if ((iocp = malloc(len)) == NULL) {
5365895Syz147064 		status = DLADM_STATUS_NOMEM;
5373871Syz147064 		goto done;
5383871Syz147064 	}
5393871Syz147064 
5405895Syz147064 	iocp->la_linkid = linkid;
5415895Syz147064 	iocp->la_nports = nports;
5425895Syz147064 	if (cmd == LAIOC_ADD)
5435895Syz147064 		iocp->la_force = (flags & DLADM_OPT_FORCE);
5443871Syz147064 
5455895Syz147064 	ioc_ports = (laioc_port_t *)(iocp + 1);
5465895Syz147064 	for (i = 0; i < nports; i++)
5475895Syz147064 		ioc_ports[i].lp_linkid = ports[i].lp_linkid;
5485895Syz147064 
5497408SSebastien.Roy@Sun.COM 	if (i_dladm_aggr_ioctl(cmd, iocp) < 0)
5505895Syz147064 		status = dladm_errno2status(errno);
5513871Syz147064 
5523871Syz147064 done:
5533871Syz147064 	free(iocp);
5545895Syz147064 
5555895Syz147064 	/*
5565895Syz147064 	 * If the active configuration update fails, restore the old
5575895Syz147064 	 * persistent configuration if we've changed that.
5585895Syz147064 	 */
5595895Syz147064 	if ((status != DLADM_STATUS_OK) && (flags & DLADM_OPT_PERSIST)) {
5605895Syz147064 		if (dladm_read_conf(linkid, &conf) == DLADM_STATUS_OK) {
5615895Syz147064 			u64 = orig_nports;
5625895Syz147064 			if ((dladm_set_conf_field(conf, FNPORTS,
5635895Syz147064 			    DLADM_TYPE_UINT64, &u64) == DLADM_STATUS_OK) &&
5645895Syz147064 			    (dladm_set_conf_field(conf, FPORTS, DLADM_TYPE_STR,
5655895Syz147064 			    orig_portstr) == DLADM_STATUS_OK)) {
5665895Syz147064 				(void) dladm_write_conf(conf);
5675895Syz147064 			}
5685895Syz147064 			(void) dladm_destroy_conf(conf);
5695895Syz147064 		}
5705895Syz147064 	}
5715895Syz147064 	free(orig_portstr);
5723871Syz147064 	return (status);
5733871Syz147064 }
5743871Syz147064 
5753871Syz147064 /*
5763871Syz147064  * Send a modify command to the link aggregation driver.
5773871Syz147064  */
5783871Syz147064 static dladm_status_t
5795895Syz147064 i_dladm_aggr_modify_sys(datalink_id_t linkid, uint32_t mask,
5803871Syz147064     dladm_aggr_modify_attr_t *attr)
5813871Syz147064 {
5823871Syz147064 	laioc_modify_t ioc;
5833871Syz147064 
5845895Syz147064 	ioc.lu_linkid = linkid;
5853871Syz147064 
5863871Syz147064 	ioc.lu_modify_mask = 0;
5873871Syz147064 	if (mask & DLADM_AGGR_MODIFY_POLICY)
5883871Syz147064 		ioc.lu_modify_mask |= LAIOC_MODIFY_POLICY;
5893871Syz147064 	if (mask & DLADM_AGGR_MODIFY_MAC)
5903871Syz147064 		ioc.lu_modify_mask |= LAIOC_MODIFY_MAC;
5913871Syz147064 	if (mask & DLADM_AGGR_MODIFY_LACP_MODE)
5923871Syz147064 		ioc.lu_modify_mask |= LAIOC_MODIFY_LACP_MODE;
5933871Syz147064 	if (mask & DLADM_AGGR_MODIFY_LACP_TIMER)
5943871Syz147064 		ioc.lu_modify_mask |= LAIOC_MODIFY_LACP_TIMER;
5953871Syz147064 
5963871Syz147064 	ioc.lu_policy = attr->ld_policy;
5973871Syz147064 	ioc.lu_mac_fixed = attr->ld_mac_fixed;
5983871Syz147064 	bcopy(attr->ld_mac, ioc.lu_mac, ETHERADDRL);
5993871Syz147064 	ioc.lu_lacp_mode = attr->ld_lacp_mode;
6003871Syz147064 	ioc.lu_lacp_timer = attr->ld_lacp_timer;
6013871Syz147064 
6027408SSebastien.Roy@Sun.COM 	if (i_dladm_aggr_ioctl(LAIOC_MODIFY, &ioc) < 0) {
6033871Syz147064 		if (errno == EINVAL)
6045895Syz147064 			return (DLADM_STATUS_MACADDRINVAL);
6053871Syz147064 		else
6065895Syz147064 			return (dladm_errno2status(errno));
6075895Syz147064 	} else {
6085895Syz147064 		return (DLADM_STATUS_OK);
6093871Syz147064 	}
6103871Syz147064 }
6113871Syz147064 
6123871Syz147064 /*
6133871Syz147064  * Send a create command to the link aggregation driver.
6143871Syz147064  */
6153871Syz147064 static dladm_status_t
6165895Syz147064 i_dladm_aggr_create_sys(datalink_id_t linkid, uint16_t key, uint32_t nports,
6175895Syz147064     dladm_aggr_port_attr_db_t *ports, uint32_t policy,
6185895Syz147064     boolean_t mac_addr_fixed, const uchar_t *mac_addr,
6195895Syz147064     aggr_lacp_mode_t lacp_mode, aggr_lacp_timer_t lacp_timer, boolean_t force)
6203871Syz147064 {
6217408SSebastien.Roy@Sun.COM 	int i, len;
6225895Syz147064 	laioc_create_t *iocp = NULL;
6235895Syz147064 	laioc_port_t *ioc_ports;
6243871Syz147064 	dladm_status_t status = DLADM_STATUS_OK;
6253871Syz147064 
6265895Syz147064 	len = sizeof (*iocp) + nports * sizeof (laioc_port_t);
6273871Syz147064 	iocp = malloc(len);
6283871Syz147064 	if (iocp == NULL)
6293871Syz147064 		return (DLADM_STATUS_NOMEM);
6303871Syz147064 
6315895Syz147064 	iocp->lc_key = key;
6325895Syz147064 	iocp->lc_linkid = linkid;
6335895Syz147064 	iocp->lc_nports = nports;
6345895Syz147064 	iocp->lc_policy = policy;
6355895Syz147064 	iocp->lc_lacp_mode = lacp_mode;
6365895Syz147064 	iocp->lc_lacp_timer = lacp_timer;
6375895Syz147064 	ioc_ports = (laioc_port_t *)(iocp + 1);
6385895Syz147064 	iocp->lc_force = force;
6393871Syz147064 
6405895Syz147064 	for (i = 0; i < nports; i++)
6415895Syz147064 		ioc_ports[i].lp_linkid = ports[i].lp_linkid;
6425895Syz147064 
6435895Syz147064 	if (mac_addr_fixed && !VALID_PORT_MAC(mac_addr)) {
6445895Syz147064 		status = DLADM_STATUS_MACADDRINVAL;
6455895Syz147064 		goto done;
6463871Syz147064 	}
6473871Syz147064 
6485895Syz147064 	bcopy(mac_addr, iocp->lc_mac, ETHERADDRL);
6495895Syz147064 	iocp->lc_mac_fixed = mac_addr_fixed;
6503871Syz147064 
6517408SSebastien.Roy@Sun.COM 	if (i_dladm_aggr_ioctl(LAIOC_CREATE, iocp) < 0)
6525895Syz147064 		status = dladm_errno2status(errno);
6533871Syz147064 
6545895Syz147064 done:
6553871Syz147064 	free(iocp);
6563871Syz147064 	return (status);
6573871Syz147064 }
6583871Syz147064 
6593871Syz147064 /*
6603871Syz147064  * Invoked to bring up a link aggregation group.
6613871Syz147064  */
6625895Syz147064 static int
6635895Syz147064 i_dladm_aggr_up(datalink_id_t linkid, void *arg)
6643871Syz147064 {
6655895Syz147064 	dladm_status_t *statusp = (dladm_status_t *)arg;
6665895Syz147064 	dladm_aggr_grp_attr_t attr;
6675895Syz147064 	dladm_aggr_port_attr_db_t *ports = NULL;
6685895Syz147064 	uint16_t key = 0;
6695895Syz147064 	int i, j;
6703871Syz147064 	dladm_status_t status;
6713871Syz147064 
6725895Syz147064 	status = dladm_aggr_info(linkid, &attr, DLADM_OPT_PERSIST);
6735895Syz147064 	if (status != DLADM_STATUS_OK) {
6745895Syz147064 		*statusp = status;
6755895Syz147064 		return (DLADM_WALK_CONTINUE);
6765895Syz147064 	}
6773871Syz147064 
6785895Syz147064 	if (attr.lg_key <= AGGR_MAX_KEY)
6795895Syz147064 		key = attr.lg_key;
6805895Syz147064 
6815895Syz147064 	ports = malloc(attr.lg_nports * sizeof (dladm_aggr_port_attr_db_t));
6825895Syz147064 	if (ports == NULL) {
6835895Syz147064 		status = DLADM_STATUS_NOMEM;
6845895Syz147064 		goto done;
6853871Syz147064 	}
6863871Syz147064 
6873871Syz147064 	/*
6885895Syz147064 	 * Validate (and purge) each physical link associated with this
6895895Syz147064 	 * aggregation, if the specific hardware has been removed during
6905895Syz147064 	 * the system shutdown.
6913871Syz147064 	 */
6925895Syz147064 	for (i = 0, j = 0; i < attr.lg_nports; i++) {
6935895Syz147064 		datalink_id_t	portid = attr.lg_ports[i].lp_linkid;
6945895Syz147064 		uint32_t	flags;
6955895Syz147064 		dladm_status_t	s;
6963871Syz147064 
6975895Syz147064 		s = dladm_datalink_id2info(portid, &flags, NULL, NULL, NULL, 0);
6985895Syz147064 		if (s != DLADM_STATUS_OK || !(flags & DLADM_OPT_ACTIVE))
6995895Syz147064 			continue;
7005895Syz147064 
7015895Syz147064 		ports[j++].lp_linkid = portid;
7025895Syz147064 	}
7033871Syz147064 
7045895Syz147064 	if (j == 0) {
7055895Syz147064 		/*
7065895Syz147064 		 * All of the physical links are removed.
7075895Syz147064 		 */
7085895Syz147064 		status = DLADM_STATUS_BADARG;
7095895Syz147064 		goto done;
7103871Syz147064 	}
7113871Syz147064 
7125895Syz147064 	/*
7135895Syz147064 	 * Create active aggregation.
7145895Syz147064 	 */
7155895Syz147064 	if ((status = i_dladm_aggr_create_sys(linkid,
7165895Syz147064 	    key, j, ports, attr.lg_policy, attr.lg_mac_fixed,
7175895Syz147064 	    (const uchar_t *)attr.lg_mac, attr.lg_lacp_mode,
7185895Syz147064 	    attr.lg_lacp_timer, attr.lg_force)) != DLADM_STATUS_OK) {
7195895Syz147064 		goto done;
7205895Syz147064 	}
7213871Syz147064 
7225895Syz147064 	if ((status = dladm_up_datalink_id(linkid)) != DLADM_STATUS_OK) {
7235895Syz147064 		laioc_delete_t ioc;
7245895Syz147064 		ioc.ld_linkid = linkid;
7257408SSebastien.Roy@Sun.COM 		(void) i_dladm_aggr_ioctl(LAIOC_DELETE, &ioc);
7265895Syz147064 		goto done;
7275895Syz147064 	}
7283871Syz147064 
7293871Syz147064 	/*
7305895Syz147064 	 * Reset the active linkprop of this specific link.
7313871Syz147064 	 */
7326916Sartem 	(void) dladm_init_linkprop(linkid, B_FALSE);
7333871Syz147064 
7345895Syz147064 done:
7355895Syz147064 	free(attr.lg_ports);
7365895Syz147064 	free(ports);
7375895Syz147064 
7385895Syz147064 	*statusp = status;
7395895Syz147064 	return (DLADM_WALK_CONTINUE);
7403871Syz147064 }
7413871Syz147064 
7423871Syz147064 /*
7435895Syz147064  * Bring up one aggregation, or all persistent aggregations.  In the latter
7445895Syz147064  * case, the walk may terminate early if bringup of an aggregation fails.
7453871Syz147064  */
7465895Syz147064 dladm_status_t
7475895Syz147064 dladm_aggr_up(datalink_id_t linkid)
7483871Syz147064 {
7493871Syz147064 	dladm_status_t status;
7503871Syz147064 
7515895Syz147064 	if (linkid == DATALINK_ALL_LINKID) {
7525895Syz147064 		(void) dladm_walk_datalink_id(i_dladm_aggr_up, &status,
7535895Syz147064 		    DATALINK_CLASS_AGGR, DATALINK_ANY_MEDIATYPE,
7545895Syz147064 		    DLADM_OPT_PERSIST);
7555895Syz147064 		return (DLADM_STATUS_OK);
7565895Syz147064 	} else {
7575895Syz147064 		(void) i_dladm_aggr_up(linkid, &status);
7583871Syz147064 		return (status);
7593871Syz147064 	}
7603871Syz147064 }
7613871Syz147064 
7623871Syz147064 /*
7633871Syz147064  * Given a policy string, return a policy mask. Returns B_TRUE on
7645895Syz147064  * success, or B_FALSE if an error occurred during parsing.
7653871Syz147064  */
7663871Syz147064 boolean_t
7673871Syz147064 dladm_aggr_str2policy(const char *str, uint32_t *policy)
7683871Syz147064 {
7693871Syz147064 	int i;
7703871Syz147064 	policy_t *pol;
7713871Syz147064 	char *token = NULL;
7723871Syz147064 	char *lasts;
7733871Syz147064 
7743871Syz147064 	*policy = 0;
7753871Syz147064 
7763871Syz147064 	while ((token = strtok_r((token == NULL) ? (char *)str : NULL, ",",
7773871Syz147064 	    &lasts)) != NULL) {
7783871Syz147064 		for (i = 0; i < NPOLICIES; i++) {
7793871Syz147064 			pol = &policies[i];
7803871Syz147064 			if (strcasecmp(token, pol->pol_name) == 0) {
7813871Syz147064 				*policy |= pol->policy;
7823871Syz147064 				break;
7833871Syz147064 			}
7843871Syz147064 		}
7853871Syz147064 		if (i == NPOLICIES)
7863871Syz147064 			return (B_FALSE);
7873871Syz147064 	}
7883871Syz147064 
7893871Syz147064 	return (B_TRUE);
7903871Syz147064 }
7913871Syz147064 
7923871Syz147064 /*
7933871Syz147064  * Given a policy mask, returns a printable string, or NULL if the
7943871Syz147064  * policy mask is invalid. It is the responsibility of the caller to
7953871Syz147064  * free the returned string after use.
7963871Syz147064  */
7973871Syz147064 char *
7983871Syz147064 dladm_aggr_policy2str(uint32_t policy, char *str)
7993871Syz147064 {
8003871Syz147064 	int i, npolicies = 0;
8013871Syz147064 	policy_t *pol;
8023871Syz147064 
8035895Syz147064 	if (str == NULL)
8045895Syz147064 		return (NULL);
8055895Syz147064 
8063871Syz147064 	str[0] = '\0';
8073871Syz147064 
8083871Syz147064 	for (i = 0; i < NPOLICIES; i++) {
8093871Syz147064 		pol = &policies[i];
8103871Syz147064 		if ((policy & pol->policy) != 0) {
8113871Syz147064 			npolicies++;
8123871Syz147064 			if (npolicies > 1)
8135895Syz147064 				(void) strlcat(str, ",", DLADM_STRSIZE);
8145895Syz147064 			(void) strlcat(str, pol->pol_name, DLADM_STRSIZE);
8153871Syz147064 		}
8163871Syz147064 	}
8173871Syz147064 
8183871Syz147064 	return (str);
8193871Syz147064 }
8203871Syz147064 
8213871Syz147064 /*
8223871Syz147064  * Given a MAC address string, return the MAC address in the mac_addr
8233871Syz147064  * array. If the MAC address was not explicitly specified, i.e. is
8243871Syz147064  * equal to 'auto', zero out mac-addr and set mac_fixed to B_TRUE.
8253871Syz147064  * Return B_FALSE if a syntax error was encountered, B_FALSE otherwise.
8263871Syz147064  */
8273871Syz147064 boolean_t
8283871Syz147064 dladm_aggr_str2macaddr(const char *str, boolean_t *mac_fixed, uchar_t *mac_addr)
8293871Syz147064 {
8303871Syz147064 	uchar_t *conv_str;
8313871Syz147064 	int mac_len;
8323871Syz147064 
8333871Syz147064 	*mac_fixed = (strcmp(str, "auto") != 0);
8343871Syz147064 	if (!*mac_fixed) {
8353871Syz147064 		bzero(mac_addr, ETHERADDRL);
8363871Syz147064 		return (B_TRUE);
8373871Syz147064 	}
8383871Syz147064 
8393871Syz147064 	conv_str = _link_aton(str, &mac_len);
8403871Syz147064 	if (conv_str == NULL)
8413871Syz147064 		return (B_FALSE);
8423871Syz147064 
8433871Syz147064 	if (mac_len != ETHERADDRL) {
8443871Syz147064 		free(conv_str);
8453871Syz147064 		return (B_FALSE);
8463871Syz147064 	}
8473871Syz147064 
8483871Syz147064 	if ((bcmp(zero_mac, conv_str, ETHERADDRL) == 0) ||
8493871Syz147064 	    (conv_str[0] & 0x01)) {
8503871Syz147064 		free(conv_str);
8513871Syz147064 		return (B_FALSE);
8523871Syz147064 	}
8533871Syz147064 
8543871Syz147064 	bcopy(conv_str, mac_addr, ETHERADDRL);
8553871Syz147064 	free(conv_str);
8563871Syz147064 
8573871Syz147064 	return (B_TRUE);
8583871Syz147064 }
8593871Syz147064 
8603871Syz147064 /*
8613871Syz147064  * Returns a string containing a printable representation of a MAC address.
8623871Syz147064  */
8633871Syz147064 const char *
8645895Syz147064 dladm_aggr_macaddr2str(const unsigned char *mac, char *buf)
8653871Syz147064 {
8663871Syz147064 	static char unknown_mac[] = {0, 0, 0, 0, 0, 0};
8673871Syz147064 
8683871Syz147064 	if (buf == NULL)
8693871Syz147064 		return (NULL);
8703871Syz147064 
8713871Syz147064 	if (bcmp(unknown_mac, mac, ETHERADDRL) == 0)
8725895Syz147064 		(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
8733871Syz147064 	else
8743871Syz147064 		return (_link_ntoa(mac, buf, ETHERADDRL, IFT_OTHER));
8755895Syz147064 
8765895Syz147064 	return (buf);
8773871Syz147064 }
8783871Syz147064 
8793871Syz147064 /*
8803871Syz147064  * Given a LACP mode string, find the corresponding LACP mode number. Returns
8813871Syz147064  * B_TRUE if a match was found, B_FALSE otherwise.
8823871Syz147064  */
8833871Syz147064 boolean_t
8843871Syz147064 dladm_aggr_str2lacpmode(const char *str, aggr_lacp_mode_t *lacp_mode)
8853871Syz147064 {
8863871Syz147064 	int i;
8873871Syz147064 	dladm_aggr_lacpmode_t *mode;
8883871Syz147064 
8893871Syz147064 	for (i = 0; i < NLACP_MODES; i++) {
8903871Syz147064 		mode = &lacp_modes[i];
8913871Syz147064 		if (strncasecmp(str, mode->mode_str,
8923871Syz147064 		    strlen(mode->mode_str)) == 0) {
8933871Syz147064 			*lacp_mode = mode->mode_id;
8943871Syz147064 			return (B_TRUE);
8953871Syz147064 		}
8963871Syz147064 	}
8973871Syz147064 
8983871Syz147064 	return (B_FALSE);
8993871Syz147064 }
9003871Syz147064 
9013871Syz147064 /*
9023871Syz147064  * Given a LACP mode number, returns a printable string, or NULL if the
9033871Syz147064  * LACP mode number is invalid.
9043871Syz147064  */
9053871Syz147064 const char *
9063871Syz147064 dladm_aggr_lacpmode2str(aggr_lacp_mode_t mode_id, char *buf)
9073871Syz147064 {
9083871Syz147064 	int i;
9093871Syz147064 	dladm_aggr_lacpmode_t *mode;
9103871Syz147064 
9115895Syz147064 	if (buf == NULL)
9125895Syz147064 		return (NULL);
9135895Syz147064 
9143871Syz147064 	for (i = 0; i < NLACP_MODES; i++) {
9153871Syz147064 		mode = &lacp_modes[i];
9163871Syz147064 		if (mode->mode_id == mode_id) {
9173871Syz147064 			(void) snprintf(buf, DLADM_STRSIZE, "%s",
9183871Syz147064 			    mode->mode_str);
9193871Syz147064 			return (buf);
9203871Syz147064 		}
9213871Syz147064 	}
9223871Syz147064 
9233871Syz147064 	(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
9243871Syz147064 	return (buf);
9253871Syz147064 }
9263871Syz147064 
9273871Syz147064 /*
9283871Syz147064  * Given a LACP timer string, find the corresponding LACP timer number. Returns
9293871Syz147064  * B_TRUE if a match was found, B_FALSE otherwise.
9303871Syz147064  */
9313871Syz147064 boolean_t
9323871Syz147064 dladm_aggr_str2lacptimer(const char *str, aggr_lacp_timer_t *lacp_timer)
9333871Syz147064 {
9343871Syz147064 	int i;
9353871Syz147064 	dladm_aggr_lacptimer_t *timer;
9363871Syz147064 
9373871Syz147064 	for (i = 0; i < NLACP_TIMERS; i++) {
9383871Syz147064 		timer = &lacp_timers[i];
9393871Syz147064 		if (strncasecmp(str, timer->lt_str,
9403871Syz147064 		    strlen(timer->lt_str)) == 0) {
9413871Syz147064 			*lacp_timer = timer->lt_id;
9423871Syz147064 			return (B_TRUE);
9433871Syz147064 		}
9443871Syz147064 	}
9453871Syz147064 
9463871Syz147064 	return (B_FALSE);
9473871Syz147064 }
9483871Syz147064 
9493871Syz147064 /*
9503871Syz147064  * Given a LACP timer, returns a printable string, or NULL if the
9513871Syz147064  * LACP timer number is invalid.
9523871Syz147064  */
9533871Syz147064 const char *
9543871Syz147064 dladm_aggr_lacptimer2str(aggr_lacp_timer_t timer_id, char *buf)
9553871Syz147064 {
9563871Syz147064 	int i;
9573871Syz147064 	dladm_aggr_lacptimer_t *timer;
9583871Syz147064 
9595895Syz147064 	if (buf == NULL)
9605895Syz147064 		return (NULL);
9615895Syz147064 
9623871Syz147064 	for (i = 0; i < NLACP_TIMERS; i++) {
9633871Syz147064 		timer = &lacp_timers[i];
9643871Syz147064 		if (timer->lt_id == timer_id) {
9653871Syz147064 			(void) snprintf(buf, DLADM_STRSIZE, "%s",
9663871Syz147064 			    timer->lt_str);
9673871Syz147064 			return (buf);
9683871Syz147064 		}
9693871Syz147064 	}
9703871Syz147064 
9713871Syz147064 	(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
9723871Syz147064 	return (buf);
9733871Syz147064 }
9743871Syz147064 
9753871Syz147064 const char *
9763871Syz147064 dladm_aggr_portstate2str(aggr_port_state_t state_id, char *buf)
9773871Syz147064 {
9783871Syz147064 	int			i;
9795895Syz147064 	dladm_aggr_port_state_t *state;
9805895Syz147064 
9815895Syz147064 	if (buf == NULL)
9825895Syz147064 		return (NULL);
9833871Syz147064 
9843871Syz147064 	for (i = 0; i < NPORT_STATES; i++) {
9853871Syz147064 		state = &port_states[i];
9863871Syz147064 		if (state->state_id == state_id) {
9873871Syz147064 			(void) snprintf(buf, DLADM_STRSIZE, "%s",
9883871Syz147064 			    state->state_str);
9893871Syz147064 			return (buf);
9903871Syz147064 		}
9913871Syz147064 	}
9923871Syz147064 
9933871Syz147064 	(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
9943871Syz147064 	return (buf);
9953871Syz147064 }
9963871Syz147064 
9975895Syz147064 static dladm_status_t
9985895Syz147064 dladm_aggr_persist_aggr_conf(const char *link, datalink_id_t linkid,
9995895Syz147064     uint16_t key, uint32_t nports, dladm_aggr_port_attr_db_t *ports,
10005895Syz147064     uint32_t policy, boolean_t mac_addr_fixed, const uchar_t *mac_addr,
10015895Syz147064     aggr_lacp_mode_t lacp_mode, aggr_lacp_timer_t lacp_timer,
10025895Syz147064     boolean_t force)
10033871Syz147064 {
10045895Syz147064 	dladm_conf_t conf = DLADM_INVALID_CONF;
10055895Syz147064 	char *portstr = NULL;
10065895Syz147064 	char macstr[ETHERADDRL * 3];
10075895Syz147064 	dladm_status_t status;
10085895Syz147064 	int i, size;
10095895Syz147064 	uint64_t u64;
10103871Syz147064 
10115895Syz147064 	if ((status = dladm_create_conf(link, linkid, DATALINK_CLASS_AGGR,
10125895Syz147064 	    DL_ETHER, &conf)) != DLADM_STATUS_OK) {
10133871Syz147064 		return (status);
10143871Syz147064 	}
10153871Syz147064 
10165895Syz147064 	u64 = key;
10175895Syz147064 	status = dladm_set_conf_field(conf, FKEY, DLADM_TYPE_UINT64, &u64);
10185895Syz147064 	if (status != DLADM_STATUS_OK)
10195895Syz147064 		goto done;
10203871Syz147064 
10215895Syz147064 	u64 = nports;
10225895Syz147064 	status = dladm_set_conf_field(conf, FNPORTS, DLADM_TYPE_UINT64, &u64);
10235895Syz147064 	if (status != DLADM_STATUS_OK)
10245895Syz147064 		goto done;
10255895Syz147064 
10265895Syz147064 	size = nports * (LINKID_STR_WIDTH + 1) + 1;
10275895Syz147064 	if ((portstr = calloc(1, size)) == NULL) {
10285895Syz147064 		status = DLADM_STATUS_NOMEM;
10295895Syz147064 		goto done;
10305895Syz147064 	}
10313871Syz147064 
10325895Syz147064 	for (i = 0; i < nports; i++)
10335895Syz147064 		WRITE_PORT(portstr, ports[i].lp_linkid, size);
10345895Syz147064 	status = dladm_set_conf_field(conf, FPORTS, DLADM_TYPE_STR, portstr);
10355895Syz147064 	free(portstr);
10365895Syz147064 
10375895Syz147064 	if (status != DLADM_STATUS_OK)
10385895Syz147064 		goto done;
10393871Syz147064 
10405895Syz147064 	u64 = policy;
10415895Syz147064 	status = dladm_set_conf_field(conf, FPOLICY, DLADM_TYPE_UINT64, &u64);
10425895Syz147064 	if (status != DLADM_STATUS_OK)
10435895Syz147064 		goto done;
10445895Syz147064 
10455895Syz147064 	status = dladm_set_conf_field(conf, FFIXMACADDR, DLADM_TYPE_BOOLEAN,
10465895Syz147064 	    &mac_addr_fixed);
10475895Syz147064 	if (status != DLADM_STATUS_OK)
10485895Syz147064 		goto done;
10495895Syz147064 
10505895Syz147064 	if (mac_addr_fixed) {
10515895Syz147064 		if (!VALID_PORT_MAC(mac_addr)) {
10525895Syz147064 			status = DLADM_STATUS_MACADDRINVAL;
10533871Syz147064 			goto done;
10543871Syz147064 		}
10553871Syz147064 
10565895Syz147064 		(void) dladm_aggr_macaddr2str(mac_addr, macstr);
10575895Syz147064 		status = dladm_set_conf_field(conf, FMACADDR, DLADM_TYPE_STR,
10585895Syz147064 		    macstr);
10595895Syz147064 		if (status != DLADM_STATUS_OK)
10603871Syz147064 			goto done;
10613871Syz147064 	}
10623871Syz147064 
10635895Syz147064 	status = dladm_set_conf_field(conf, FFORCE, DLADM_TYPE_BOOLEAN, &force);
10645895Syz147064 	if (status != DLADM_STATUS_OK)
10655895Syz147064 		goto done;
10665895Syz147064 
10675895Syz147064 	u64 = lacp_mode;
10685895Syz147064 	status = dladm_set_conf_field(conf, FLACPMODE, DLADM_TYPE_UINT64, &u64);
10695895Syz147064 	if (status != DLADM_STATUS_OK)
10705895Syz147064 		goto done;
10715895Syz147064 
10725895Syz147064 	u64 = lacp_timer;
10735895Syz147064 	status = dladm_set_conf_field(conf, FLACPTIMER, DLADM_TYPE_UINT64,
10745895Syz147064 	    &u64);
10755895Syz147064 	if (status != DLADM_STATUS_OK)
10765895Syz147064 		goto done;
10775895Syz147064 
10783871Syz147064 	/*
10795895Syz147064 	 * Commit the link aggregation configuration.
10803871Syz147064 	 */
10815895Syz147064 	status = dladm_write_conf(conf);
10823871Syz147064 
10833871Syz147064 done:
10845895Syz147064 	dladm_destroy_conf(conf);
10853871Syz147064 	return (status);
10863871Syz147064 }
10873871Syz147064 
10883871Syz147064 /*
10893871Syz147064  * Create a new link aggregation group. Update the configuration
10903871Syz147064  * file and bring it up.
10913871Syz147064  */
10923871Syz147064 dladm_status_t
10935895Syz147064 dladm_aggr_create(const char *name, uint16_t key, uint32_t nports,
10943871Syz147064     dladm_aggr_port_attr_db_t *ports, uint32_t policy, boolean_t mac_addr_fixed,
10955895Syz147064     const uchar_t *mac_addr, aggr_lacp_mode_t lacp_mode,
10965895Syz147064     aggr_lacp_timer_t lacp_timer, uint32_t flags)
10973871Syz147064 {
10985895Syz147064 	datalink_id_t linkid = DATALINK_INVALID_LINKID;
10995895Syz147064 	uint32_t media;
11005895Syz147064 	uint32_t i;
11015895Syz147064 	datalink_class_t class;
11023871Syz147064 	dladm_status_t status;
11035895Syz147064 	boolean_t force = (flags & DLADM_OPT_FORCE) ? B_TRUE : B_FALSE;
11043871Syz147064 
11055895Syz147064 	if (key != 0 && key > AGGR_MAX_KEY)
11063871Syz147064 		return (DLADM_STATUS_KEYINVAL);
11073871Syz147064 
11085895Syz147064 	if (nports == 0)
11095895Syz147064 		return (DLADM_STATUS_BADARG);
11105895Syz147064 
11115895Syz147064 	for (i = 0; i < nports; i++) {
11125895Syz147064 		if ((dladm_datalink_id2info(ports[i].lp_linkid, NULL,
11135895Syz147064 		    &class, &media, NULL, 0) != DLADM_STATUS_OK) ||
1114*8275SEric Cheng 		    !((class == DATALINK_CLASS_PHYS) && (media == DL_ETHER))) {
11155895Syz147064 			return (DLADM_STATUS_BADARG);
11165895Syz147064 		}
11175895Syz147064 	}
11185895Syz147064 
11195895Syz147064 	flags &= (DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST);
11205895Syz147064 	if ((status = dladm_create_datalink_id(name, DATALINK_CLASS_AGGR,
11215895Syz147064 	    DL_ETHER, flags, &linkid)) != DLADM_STATUS_OK) {
11225895Syz147064 		goto fail;
11235895Syz147064 	}
11245895Syz147064 
11255895Syz147064 	if ((flags & DLADM_OPT_PERSIST) &&
11265895Syz147064 	    (status = dladm_aggr_persist_aggr_conf(name, linkid, key, nports,
11275895Syz147064 	    ports, policy, mac_addr_fixed, mac_addr, lacp_mode, lacp_timer,
11285895Syz147064 	    force)) != DLADM_STATUS_OK) {
11295895Syz147064 		goto fail;
11305895Syz147064 	}
11315895Syz147064 
11325895Syz147064 	if (!(flags & DLADM_OPT_ACTIVE))
11335895Syz147064 		return (DLADM_STATUS_OK);
11345895Syz147064 
11355895Syz147064 	status = i_dladm_aggr_create_sys(linkid, key, nports, ports, policy,
11365895Syz147064 	    mac_addr_fixed, mac_addr, lacp_mode, lacp_timer, force);
11373871Syz147064 
11385895Syz147064 	if (status != DLADM_STATUS_OK) {
11395895Syz147064 		if (flags & DLADM_OPT_PERSIST)
11405895Syz147064 			(void) dladm_remove_conf(linkid);
11415895Syz147064 		goto fail;
11425895Syz147064 	}
11435895Syz147064 
11445895Syz147064 	return (DLADM_STATUS_OK);
11455895Syz147064 
11465895Syz147064 fail:
11475895Syz147064 	if (linkid != DATALINK_INVALID_LINKID)
11485895Syz147064 		(void) dladm_destroy_datalink_id(linkid, flags);
11495895Syz147064 
11505895Syz147064 	return (status);
11515895Syz147064 }
11525895Syz147064 
11535895Syz147064 static dladm_status_t
11545895Syz147064 i_dladm_aggr_get_aggr_attr(dladm_conf_t conf, uint32_t mask,
11555895Syz147064     dladm_aggr_modify_attr_t *attrp)
11565895Syz147064 {
11575895Syz147064 	dladm_status_t status = DLADM_STATUS_OK;
11585895Syz147064 	char macstr[ETHERADDRL * 3];
11595895Syz147064 	uint64_t u64;
11605895Syz147064 
11615895Syz147064 	if (mask & DLADM_AGGR_MODIFY_POLICY) {
11625895Syz147064 		status = dladm_get_conf_field(conf, FPOLICY, &u64,
11635895Syz147064 		    sizeof (u64));
11645895Syz147064 		if (status != DLADM_STATUS_OK)
11655895Syz147064 			return (status);
11665895Syz147064 		attrp->ld_policy = (uint32_t)u64;
11675895Syz147064 	}
11685895Syz147064 
11695895Syz147064 	if (mask & DLADM_AGGR_MODIFY_MAC) {
11705895Syz147064 		status = dladm_get_conf_field(conf, FFIXMACADDR,
11715895Syz147064 		    &attrp->ld_mac_fixed, sizeof (boolean_t));
11723871Syz147064 		if (status != DLADM_STATUS_OK)
11733871Syz147064 			return (status);
11745895Syz147064 
11755895Syz147064 		if (attrp->ld_mac_fixed) {
11765895Syz147064 			boolean_t fixed;
11775895Syz147064 
11785895Syz147064 			status = dladm_get_conf_field(conf, FMACADDR,
11795895Syz147064 			    macstr, sizeof (macstr));
11805895Syz147064 			if (status != DLADM_STATUS_OK)
11815895Syz147064 				return (status);
11823871Syz147064 
11835895Syz147064 			if (!dladm_aggr_str2macaddr(macstr, &fixed,
11845895Syz147064 			    attrp->ld_mac)) {
11855895Syz147064 				return (DLADM_STATUS_REPOSITORYINVAL);
11865895Syz147064 			}
11875895Syz147064 		}
11885895Syz147064 	}
11893871Syz147064 
11905895Syz147064 	if (mask & DLADM_AGGR_MODIFY_LACP_MODE) {
11915895Syz147064 		status = dladm_get_conf_field(conf, FLACPMODE, &u64,
11925895Syz147064 		    sizeof (u64));
11935895Syz147064 		if (status != DLADM_STATUS_OK)
11945895Syz147064 			return (status);
11955895Syz147064 		attrp->ld_lacp_mode = (aggr_lacp_mode_t)u64;
11965895Syz147064 	}
11975895Syz147064 
11985895Syz147064 	if (mask & DLADM_AGGR_MODIFY_LACP_TIMER) {
11995895Syz147064 		status = dladm_get_conf_field(conf, FLACPTIMER, &u64,
12005895Syz147064 		    sizeof (u64));
12015895Syz147064 		if (status != DLADM_STATUS_OK)
12025895Syz147064 			return (status);
12035895Syz147064 		attrp->ld_lacp_timer = (aggr_lacp_timer_t)u64;
12043871Syz147064 	}
12053871Syz147064 
12065895Syz147064 	return (status);
12075895Syz147064 }
12085895Syz147064 
12095895Syz147064 static dladm_status_t
12105895Syz147064 i_dladm_aggr_set_aggr_attr(dladm_conf_t conf, uint32_t mask,
12115895Syz147064     dladm_aggr_modify_attr_t *attrp)
12125895Syz147064 {
12135895Syz147064 	dladm_status_t status = DLADM_STATUS_OK;
12145895Syz147064 	char macstr[ETHERADDRL * 3];
12155895Syz147064 	uint64_t u64;
12165895Syz147064 
12175895Syz147064 	if (mask & DLADM_AGGR_MODIFY_POLICY) {
12185895Syz147064 		u64 = attrp->ld_policy;
12195895Syz147064 		status = dladm_set_conf_field(conf, FPOLICY, DLADM_TYPE_UINT64,
12205895Syz147064 		    &u64);
12215895Syz147064 		if (status != DLADM_STATUS_OK)
12225895Syz147064 			return (status);
12235895Syz147064 	}
12245895Syz147064 
12255895Syz147064 	if (mask & DLADM_AGGR_MODIFY_MAC) {
12265895Syz147064 		status = dladm_set_conf_field(conf, FFIXMACADDR,
12275895Syz147064 		    DLADM_TYPE_BOOLEAN, &attrp->ld_mac_fixed);
12285895Syz147064 		if (status != DLADM_STATUS_OK)
12295895Syz147064 			return (status);
12303871Syz147064 
12315895Syz147064 		if (attrp->ld_mac_fixed) {
12325895Syz147064 			(void) dladm_aggr_macaddr2str(attrp->ld_mac, macstr);
12335895Syz147064 			status = dladm_set_conf_field(conf, FMACADDR,
12345895Syz147064 			    DLADM_TYPE_STR, macstr);
12355895Syz147064 			if (status != DLADM_STATUS_OK)
12365895Syz147064 				return (status);
12375895Syz147064 		}
12385895Syz147064 	}
12395895Syz147064 
12405895Syz147064 	if (mask & DLADM_AGGR_MODIFY_LACP_MODE) {
12415895Syz147064 		u64 = attrp->ld_lacp_mode;
12425895Syz147064 		status = dladm_set_conf_field(conf, FLACPMODE,
12435895Syz147064 		    DLADM_TYPE_UINT64, &u64);
12445895Syz147064 		if (status != DLADM_STATUS_OK)
12455895Syz147064 			return (status);
12465895Syz147064 	}
12475895Syz147064 
12485895Syz147064 	if (mask & DLADM_AGGR_MODIFY_LACP_TIMER) {
12495895Syz147064 		u64 = attrp->ld_lacp_timer;
12505895Syz147064 		status = dladm_set_conf_field(conf, FLACPTIMER,
12515895Syz147064 		    DLADM_TYPE_UINT64, &u64);
12525895Syz147064 		if (status != DLADM_STATUS_OK)
12535895Syz147064 			return (status);
12545895Syz147064 	}
12553871Syz147064 
12563871Syz147064 	return (status);
12573871Syz147064 }
12583871Syz147064 
12593871Syz147064 /*
12603871Syz147064  * Modify the parameters of an existing link aggregation group. Update
12613871Syz147064  * the configuration file and pass the changes to the kernel.
12623871Syz147064  */
12633871Syz147064 dladm_status_t
12645895Syz147064 dladm_aggr_modify(datalink_id_t linkid, uint32_t modify_mask, uint32_t policy,
12655895Syz147064     boolean_t mac_fixed, const uchar_t *mac_addr, aggr_lacp_mode_t lacp_mode,
12665895Syz147064     aggr_lacp_timer_t lacp_timer, uint32_t flags)
12673871Syz147064 {
12683871Syz147064 	dladm_aggr_modify_attr_t new_attr, old_attr;
12695895Syz147064 	dladm_conf_t conf;
12703871Syz147064 	dladm_status_t status;
12713871Syz147064 
12725895Syz147064 	new_attr.ld_policy = policy;
12735895Syz147064 	new_attr.ld_mac_fixed = mac_fixed;
12745895Syz147064 	new_attr.ld_lacp_mode = lacp_mode;
12755895Syz147064 	new_attr.ld_lacp_timer = lacp_timer;
12765895Syz147064 	bcopy(mac_addr, new_attr.ld_mac, ETHERADDRL);
12775895Syz147064 
12785895Syz147064 	if (flags & DLADM_OPT_PERSIST) {
12795895Syz147064 		status = dladm_read_conf(linkid, &conf);
12805895Syz147064 		if (status != DLADM_STATUS_OK)
12815895Syz147064 			return (status);
12823871Syz147064 
12835895Syz147064 		if ((status = i_dladm_aggr_get_aggr_attr(conf, modify_mask,
12845895Syz147064 		    &old_attr)) != DLADM_STATUS_OK) {
12855895Syz147064 			goto done;
12865895Syz147064 		}
12873871Syz147064 
12885895Syz147064 		if ((status = i_dladm_aggr_set_aggr_attr(conf, modify_mask,
12895895Syz147064 		    &new_attr)) != DLADM_STATUS_OK) {
12905895Syz147064 			goto done;
12915895Syz147064 		}
12925895Syz147064 
12935895Syz147064 		status = dladm_write_conf(conf);
12945895Syz147064 
12955895Syz147064 done:
12965895Syz147064 		dladm_destroy_conf(conf);
12975895Syz147064 		if (status != DLADM_STATUS_OK)
12985895Syz147064 			return (status);
12993871Syz147064 	}
13003871Syz147064 
13015895Syz147064 	if (!(flags & DLADM_OPT_ACTIVE))
13025895Syz147064 		return (DLADM_STATUS_OK);
13033871Syz147064 
13045895Syz147064 	status = i_dladm_aggr_modify_sys(linkid, modify_mask, &new_attr);
13055895Syz147064 	if ((status != DLADM_STATUS_OK) && (flags & DLADM_OPT_PERSIST)) {
13065895Syz147064 		if (dladm_read_conf(linkid, &conf) == DLADM_STATUS_OK) {
13075895Syz147064 			if (i_dladm_aggr_set_aggr_attr(conf, modify_mask,
13085895Syz147064 			    &old_attr) == DLADM_STATUS_OK) {
13095895Syz147064 				(void) dladm_write_conf(conf);
13105895Syz147064 			}
13115895Syz147064 			dladm_destroy_conf(conf);
13125895Syz147064 		}
13133871Syz147064 	}
13143871Syz147064 
13153871Syz147064 	return (status);
13163871Syz147064 }
13173871Syz147064 
13185895Syz147064 typedef struct aggr_held_arg_s {
13195895Syz147064 	datalink_id_t	aggrid;
13205895Syz147064 	boolean_t	isheld;
13215895Syz147064 } aggr_held_arg_t;
13225895Syz147064 
13235895Syz147064 static int
13245895Syz147064 i_dladm_aggr_is_held(datalink_id_t linkid, void *arg)
13255895Syz147064 {
13265895Syz147064 	aggr_held_arg_t		*aggr_held_arg = arg;
13275895Syz147064 	dladm_vlan_attr_t	dva;
13285895Syz147064 
13295895Syz147064 	if (dladm_vlan_info(linkid, &dva, DLADM_OPT_PERSIST) != DLADM_STATUS_OK)
13305895Syz147064 		return (DLADM_WALK_CONTINUE);
13315895Syz147064 
13325895Syz147064 	if (dva.dv_linkid == aggr_held_arg->aggrid) {
13335895Syz147064 		/*
13345895Syz147064 		 * This VLAN is created over the given aggregation.
13355895Syz147064 		 */
13365895Syz147064 		aggr_held_arg->isheld = B_TRUE;
13375895Syz147064 		return (DLADM_WALK_TERMINATE);
13385895Syz147064 	}
13395895Syz147064 	return (DLADM_WALK_CONTINUE);
13405895Syz147064 }
13415895Syz147064 
13423871Syz147064 /*
13435895Syz147064  * Delete a previously created link aggregation group. Either the name "aggr"
13445895Syz147064  * or the "key" is specified.
13453871Syz147064  */
13463871Syz147064 dladm_status_t
13475895Syz147064 dladm_aggr_delete(datalink_id_t linkid, uint32_t flags)
13483871Syz147064 {
13495895Syz147064 	laioc_delete_t ioc;
13505895Syz147064 	datalink_class_t class;
13513871Syz147064 	dladm_status_t status;
13523871Syz147064 
13535895Syz147064 	if ((dladm_datalink_id2info(linkid, NULL, &class, NULL, NULL, 0) !=
13545895Syz147064 	    DLADM_STATUS_OK) || (class != DATALINK_CLASS_AGGR)) {
13555895Syz147064 		return (DLADM_STATUS_BADARG);
13565895Syz147064 	}
13573871Syz147064 
13585895Syz147064 	if (flags & DLADM_OPT_ACTIVE) {
13595895Syz147064 		ioc.ld_linkid = linkid;
13607408SSebastien.Roy@Sun.COM 		if ((i_dladm_aggr_ioctl(LAIOC_DELETE, &ioc) < 0) &&
13615895Syz147064 		    ((errno != ENOENT) || !(flags & DLADM_OPT_PERSIST))) {
13625895Syz147064 			status = dladm_errno2status(errno);
13635895Syz147064 			return (status);
13645895Syz147064 		}
13653871Syz147064 
13663871Syz147064 		/*
13675895Syz147064 		 * Delete ACTIVE linkprop first.
13683871Syz147064 		 */
13695895Syz147064 		(void) dladm_set_linkprop(linkid, NULL, NULL, 0,
13705895Syz147064 		    DLADM_OPT_ACTIVE);
13715895Syz147064 		(void) dladm_destroy_datalink_id(linkid, DLADM_OPT_ACTIVE);
13723871Syz147064 	}
13733871Syz147064 
13745895Syz147064 	/*
13755895Syz147064 	 * If we reach here, it means that the active aggregation has already
13765895Syz147064 	 * been deleted, and there is no active VLANs holding this aggregation.
13775895Syz147064 	 * Now we see whether there is any persistent VLANs holding this
13785895Syz147064 	 * aggregation. If so, we fail the operation.
13795895Syz147064 	 */
13805895Syz147064 	if (flags & DLADM_OPT_PERSIST) {
13815895Syz147064 		aggr_held_arg_t arg;
13825895Syz147064 
13835895Syz147064 		arg.aggrid = linkid;
13845895Syz147064 		arg.isheld = B_FALSE;
13853871Syz147064 
13865895Syz147064 		(void) dladm_walk_datalink_id(i_dladm_aggr_is_held,
13875895Syz147064 		    &arg, DATALINK_CLASS_VLAN, DATALINK_ANY_MEDIATYPE,
13885895Syz147064 		    DLADM_OPT_PERSIST);
13895895Syz147064 		if (arg.isheld)
13905895Syz147064 			return (DLADM_STATUS_LINKBUSY);
13915895Syz147064 
13925895Syz147064 		(void) dladm_destroy_datalink_id(linkid, DLADM_OPT_PERSIST);
13935895Syz147064 		(void) dladm_remove_conf(linkid);
13945895Syz147064 	}
13955895Syz147064 
13965895Syz147064 	return (DLADM_STATUS_OK);
13973871Syz147064 }
13983871Syz147064 
13993871Syz147064 /*
14003871Syz147064  * Add one or more ports to an existing link aggregation.
14013871Syz147064  */
14023871Syz147064 dladm_status_t
14035895Syz147064 dladm_aggr_add(datalink_id_t linkid, uint32_t nports,
14045895Syz147064     dladm_aggr_port_attr_db_t *ports, uint32_t flags)
14053871Syz147064 {
14065895Syz147064 	return (i_dladm_aggr_add_rmv(linkid, nports, ports, flags, LAIOC_ADD));
14073871Syz147064 }
14083871Syz147064 
14093871Syz147064 /*
14103871Syz147064  * Remove one or more ports from an existing link aggregation.
14113871Syz147064  */
14123871Syz147064 dladm_status_t
14135895Syz147064 dladm_aggr_remove(datalink_id_t linkid, uint32_t nports,
14145895Syz147064     dladm_aggr_port_attr_db_t *ports, uint32_t flags)
14153871Syz147064 {
14165895Syz147064 	return (i_dladm_aggr_add_rmv(linkid, nports, ports, flags,
14175895Syz147064 	    LAIOC_REMOVE));
14185895Syz147064 }
14193871Syz147064 
14205895Syz147064 typedef struct i_walk_key_state_s {
14215895Syz147064 	uint16_t key;
14225895Syz147064 	datalink_id_t linkid;
14235895Syz147064 	boolean_t found;
14245895Syz147064 } i_walk_key_state_t;
14253871Syz147064 
14265895Syz147064 static int
14275895Syz147064 i_dladm_walk_key2linkid(datalink_id_t linkid, void *arg)
14285895Syz147064 {
14295895Syz147064 	dladm_conf_t conf;
14305895Syz147064 	uint16_t key;
14315895Syz147064 	dladm_status_t status;
14325895Syz147064 	i_walk_key_state_t *statep = (i_walk_key_state_t *)arg;
14335895Syz147064 	uint64_t u64;
14343871Syz147064 
14355895Syz147064 	if (dladm_read_conf(linkid, &conf) != 0)
14365895Syz147064 		return (DLADM_WALK_CONTINUE);
14375895Syz147064 
14385895Syz147064 	status = dladm_get_conf_field(conf, FKEY, &u64, sizeof (u64));
14395895Syz147064 	key = (uint16_t)u64;
14405895Syz147064 	dladm_destroy_conf(conf);
14415895Syz147064 
14425895Syz147064 	if ((status == DLADM_STATUS_OK) && (key == statep->key)) {
14435895Syz147064 		statep->found = B_TRUE;
14445895Syz147064 		statep->linkid = linkid;
14455895Syz147064 		return (DLADM_WALK_TERMINATE);
14463871Syz147064 	}
14473871Syz147064 
14485895Syz147064 	return (DLADM_WALK_CONTINUE);
14495895Syz147064 }
14505895Syz147064 
14515895Syz147064 dladm_status_t
14525895Syz147064 dladm_key2linkid(uint16_t key, datalink_id_t *linkidp, uint32_t flags)
14535895Syz147064 {
14545895Syz147064 	i_walk_key_state_t state;
14555895Syz147064 
14565895Syz147064 	if (key > AGGR_MAX_KEY)
14575895Syz147064 		return (DLADM_STATUS_NOTFOUND);
14583871Syz147064 
14595895Syz147064 	state.found = B_FALSE;
14605895Syz147064 	state.key = key;
14615895Syz147064 
14625895Syz147064 	(void) dladm_walk_datalink_id(i_dladm_walk_key2linkid, &state,
14635895Syz147064 	    DATALINK_CLASS_AGGR, DATALINK_ANY_MEDIATYPE, flags);
14645895Syz147064 	if (state.found == B_TRUE) {
14655895Syz147064 		*linkidp = state.linkid;
14665895Syz147064 		return (DLADM_STATUS_OK);
14675895Syz147064 	} else {
14685895Syz147064 		return (DLADM_STATUS_NOTFOUND);
14695895Syz147064 	}
14703871Syz147064 }
1471