15895Syz147064 /*
25895Syz147064  * CDDL HEADER START
35895Syz147064  *
45895Syz147064  * The contents of this file are subject to the terms of the
55895Syz147064  * Common Development and Distribution License (the "License").
65895Syz147064  * You may not use this file except in compliance with the License.
75895Syz147064  *
85895Syz147064  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95895Syz147064  * or http://www.opensolaris.org/os/licensing.
105895Syz147064  * See the License for the specific language governing permissions
115895Syz147064  * and limitations under the License.
125895Syz147064  *
135895Syz147064  * When distributing Covered Code, include this CDDL HEADER in each
145895Syz147064  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155895Syz147064  * If applicable, add the following below this CDDL HEADER, with the
165895Syz147064  * fields enclosed by brackets "[]" replaced with your own identifying
175895Syz147064  * information: Portions Copyright [yyyy] [name of copyright owner]
185895Syz147064  *
195895Syz147064  * CDDL HEADER END
205895Syz147064  */
215895Syz147064 /*
225895Syz147064  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235895Syz147064  * Use is subject to license terms.
245895Syz147064  */
255895Syz147064 
265895Syz147064 #pragma ident	"%Z%%M%	%I%	%E% SMI"
275895Syz147064 
285895Syz147064 #include <door.h>
295895Syz147064 #include <errno.h>
305895Syz147064 #include <assert.h>
315895Syz147064 #include <stdio.h>
325895Syz147064 #include <stdlib.h>
335895Syz147064 #include <unistd.h>
345895Syz147064 #include <string.h>
355895Syz147064 #include <strings.h>
365895Syz147064 #include <sys/types.h>
375895Syz147064 #include <sys/stat.h>
385895Syz147064 #include <sys/aggr.h>
395895Syz147064 #include <fcntl.h>
405895Syz147064 #include <libdladm.h>
415895Syz147064 #include <libdladm_impl.h>
425895Syz147064 #include <libdllink.h>
435895Syz147064 #include <libdlmgmt.h>
445895Syz147064 
455895Syz147064 /*
465895Syz147064  * Table of data type sizes indexed by dladm_datatype_t.
475895Syz147064  */
485895Syz147064 static size_t dladm_datatype_size[] = {
495895Syz147064 	0,				/* DLADM_TYPE_STR, use strnlen() */
505895Syz147064 	sizeof (boolean_t),		/* DLADM_TYPE_BOOLEAN */
515895Syz147064 	sizeof (uint64_t)		/* DLADM_TYPE_UINT64 */
525895Syz147064 };
535895Syz147064 
545895Syz147064 static dladm_status_t
55*6263Sseb dladm_door_call(void *arg, size_t asize, void *rbuf, size_t rsize)
565895Syz147064 {
575895Syz147064 	door_arg_t	darg;
585895Syz147064 	int		fd;
595895Syz147064 	dladm_status_t	status = DLADM_STATUS_OK;
605895Syz147064 
615895Syz147064 	if ((fd = open(DLMGMT_DOOR, O_RDONLY)) == -1)
625895Syz147064 		return (dladm_errno2status(errno));
635895Syz147064 
645895Syz147064 	darg.data_ptr	= arg;
655895Syz147064 	darg.data_size	= asize;
665895Syz147064 	darg.desc_ptr	= NULL;
675895Syz147064 	darg.desc_num	= 0;
685895Syz147064 	darg.rbuf	= rbuf;
69*6263Sseb 	darg.rsize	= rsize;
705895Syz147064 
715895Syz147064 	if (door_call(fd, &darg) == -1)
725895Syz147064 		status = dladm_errno2status(errno);
735895Syz147064 	(void) close(fd);
745895Syz147064 
755895Syz147064 	if (status != DLADM_STATUS_OK)
765895Syz147064 		return (status);
775895Syz147064 
785895Syz147064 	if (darg.rbuf != rbuf) {
795895Syz147064 		/*
805895Syz147064 		 * The size of the input rbuf is not big enough so that
815895Syz147064 		 * the door allocate the rbuf itself. In this case, simply
825895Syz147064 		 * think something wrong with the door call.
835895Syz147064 		 */
845895Syz147064 		(void) munmap(darg.rbuf, darg.rsize);
855895Syz147064 		return (DLADM_STATUS_TOOSMALL);
865895Syz147064 	}
87*6263Sseb 	if (darg.rsize != rsize)
885895Syz147064 		return (DLADM_STATUS_FAILED);
895895Syz147064 
90*6263Sseb 	return (dladm_errno2status(((dlmgmt_retval_t *)rbuf)->lr_err));
915895Syz147064 }
925895Syz147064 
935895Syz147064 /*
945895Syz147064  * Allocate a new linkid with the given name. Return the new linkid.
955895Syz147064  */
965895Syz147064 dladm_status_t
975895Syz147064 dladm_create_datalink_id(const char *link, datalink_class_t class,
985895Syz147064     uint32_t media, uint32_t flags, datalink_id_t *linkidp)
995895Syz147064 {
100*6263Sseb 	dlmgmt_door_createid_t	createid;
1015895Syz147064 	dlmgmt_createid_retval_t retval;
102*6263Sseb 	uint32_t		dlmgmt_flags;
103*6263Sseb 	dladm_status_t		status;
1045895Syz147064 
105*6263Sseb 	if (link == NULL || class == DATALINK_CLASS_ALL ||
1065895Syz147064 	    !(flags & (DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST)) ||
1075895Syz147064 	    linkidp == NULL) {
1085895Syz147064 		return (DLADM_STATUS_BADARG);
1095895Syz147064 	}
1105895Syz147064 
1115895Syz147064 	dlmgmt_flags = (flags & DLADM_OPT_ACTIVE) ? DLMGMT_ACTIVE : 0;
1125895Syz147064 	dlmgmt_flags |= (flags & DLADM_OPT_PERSIST) ? DLMGMT_PERSIST : 0;
1135895Syz147064 
1145895Syz147064 	(void) strlcpy(createid.ld_link, link, MAXLINKNAMELEN);
1155895Syz147064 	createid.ld_class = class;
1165895Syz147064 	createid.ld_media = media;
1175895Syz147064 	createid.ld_flags = dlmgmt_flags;
1185895Syz147064 	createid.ld_cmd = DLMGMT_CMD_CREATE_LINKID;
1195895Syz147064 	createid.ld_prefix = (flags & DLADM_OPT_PREFIX);
1205895Syz147064 
121*6263Sseb 	if ((status = dladm_door_call(&createid, sizeof (createid), &retval,
122*6263Sseb 	    sizeof (retval))) == DLADM_STATUS_OK) {
123*6263Sseb 		*linkidp = retval.lr_linkid;
124*6263Sseb 	}
125*6263Sseb 	return (status);
1265895Syz147064 }
1275895Syz147064 
1285895Syz147064 /*
1295895Syz147064  * Destroy the given link ID.
1305895Syz147064  */
1315895Syz147064 dladm_status_t
1325895Syz147064 dladm_destroy_datalink_id(datalink_id_t linkid, uint32_t flags)
1335895Syz147064 {
1345895Syz147064 	dlmgmt_door_destroyid_t		destroyid;
1355895Syz147064 	dlmgmt_destroyid_retval_t	retval;
1365895Syz147064 	uint32_t			dlmgmt_flags;
1375895Syz147064 
1385895Syz147064 	dlmgmt_flags = (flags & DLADM_OPT_ACTIVE) ? DLMGMT_ACTIVE : 0;
1395895Syz147064 	dlmgmt_flags |= ((flags & DLADM_OPT_PERSIST) ? DLMGMT_PERSIST : 0);
1405895Syz147064 
1415895Syz147064 	destroyid.ld_cmd = DLMGMT_CMD_DESTROY_LINKID;
1425895Syz147064 	destroyid.ld_linkid = linkid;
1435895Syz147064 	destroyid.ld_flags = dlmgmt_flags;
1445895Syz147064 
145*6263Sseb 	return (dladm_door_call(&destroyid, sizeof (destroyid),
146*6263Sseb 	    &retval, sizeof (retval)));
1475895Syz147064 }
1485895Syz147064 
1495895Syz147064 /*
1505895Syz147064  * Remap a given link ID to a new name.
1515895Syz147064  */
1525895Syz147064 dladm_status_t
1535895Syz147064 dladm_remap_datalink_id(datalink_id_t linkid, const char *link)
1545895Syz147064 {
1555895Syz147064 	dlmgmt_door_remapid_t	remapid;
1565895Syz147064 	dlmgmt_remapid_retval_t	retval;
1575895Syz147064 
1585895Syz147064 	remapid.ld_cmd = DLMGMT_CMD_REMAP_LINKID;
1595895Syz147064 	remapid.ld_linkid = linkid;
1605895Syz147064 	(void) strlcpy(remapid.ld_link, link, MAXLINKNAMELEN);
1615895Syz147064 
162*6263Sseb 	return (dladm_door_call(&remapid, sizeof (remapid),
163*6263Sseb 	    &retval, sizeof (retval)));
1645895Syz147064 }
1655895Syz147064 
1665895Syz147064 /*
1675895Syz147064  * Make a given link ID active.
1685895Syz147064  */
1695895Syz147064 dladm_status_t
1705895Syz147064 dladm_up_datalink_id(datalink_id_t linkid)
1715895Syz147064 {
172*6263Sseb 	dlmgmt_door_upid_t	upid;
173*6263Sseb 	dlmgmt_upid_retval_t	retval;
1745895Syz147064 
1755895Syz147064 	upid.ld_cmd = DLMGMT_CMD_UP_LINKID;
1765895Syz147064 	upid.ld_linkid = linkid;
1775895Syz147064 
178*6263Sseb 	return (dladm_door_call(&upid, sizeof (upid),
179*6263Sseb 	    &retval, sizeof (retval)));
1805895Syz147064 }
1815895Syz147064 
1825895Syz147064 /*
1835895Syz147064  * Create a new link with the given name.  Return the new link's handle
1845895Syz147064  */
1855895Syz147064 dladm_status_t
1865895Syz147064 dladm_create_conf(const char *link, datalink_id_t linkid,
1875895Syz147064     datalink_class_t class, uint32_t media, dladm_conf_t *confp)
1885895Syz147064 {
189*6263Sseb 	dlmgmt_door_createconf_t	createconf;
190*6263Sseb 	dlmgmt_createconf_retval_t	retval;
191*6263Sseb 	dladm_status_t			status;
1925895Syz147064 
193*6263Sseb 	if (link == NULL || confp == NULL)
1945895Syz147064 		return (DLADM_STATUS_BADARG);
1955895Syz147064 
1965895Syz147064 	(void) strlcpy(createconf.ld_link, link, MAXLINKNAMELEN);
1975895Syz147064 	createconf.ld_class = class;
1985895Syz147064 	createconf.ld_media = media;
1995895Syz147064 	createconf.ld_linkid = linkid;
2005895Syz147064 	createconf.ld_cmd = DLMGMT_CMD_CREATECONF;
2015895Syz147064 
202*6263Sseb 	if ((status = dladm_door_call(&createconf, sizeof (createconf),
203*6263Sseb 	    &retval, sizeof (retval))) == DLADM_STATUS_OK) {
204*6263Sseb 		*confp = retval.lr_conf;
205*6263Sseb 	}
206*6263Sseb 	return (status);
2075895Syz147064 }
2085895Syz147064 
2095895Syz147064 /*
2105895Syz147064  * An active physical link reported by the dlmgmtd daemon might not be active
2115895Syz147064  * anymore as this link might be removed during system shutdown. Check its
2125895Syz147064  * real status by calling dladm_phys_info().
2135895Syz147064  */
2145895Syz147064 dladm_status_t
2155895Syz147064 i_dladm_phys_status(datalink_id_t linkid, uint32_t *flagsp)
2165895Syz147064 {
2175895Syz147064 	dladm_phys_attr_t	dpa;
2185895Syz147064 	dladm_status_t		status;
2195895Syz147064 
2205895Syz147064 	assert((*flagsp) & DLMGMT_ACTIVE);
2215895Syz147064 
2225895Syz147064 	status = dladm_phys_info(linkid, &dpa, DLADM_OPT_ACTIVE);
2235895Syz147064 	if (status == DLADM_STATUS_NOTFOUND) {
2245895Syz147064 		/*
2255895Syz147064 		 * No active status, this link was removed. Update its status
2265895Syz147064 		 * in the daemon and delete all active linkprops.
2275895Syz147064 		 */
2285895Syz147064 		(void) dladm_destroy_datalink_id(linkid, DLADM_OPT_ACTIVE);
2295895Syz147064 		(void) dladm_set_linkprop(linkid, NULL, NULL, 0,
2305895Syz147064 		    DLADM_OPT_ACTIVE);
2315895Syz147064 
2325895Syz147064 		(*flagsp) &= ~DLMGMT_ACTIVE;
2335895Syz147064 		status = DLADM_STATUS_OK;
2345895Syz147064 	}
2355895Syz147064 	return (status);
2365895Syz147064 }
2375895Syz147064 
2385895Syz147064 /*
2395895Syz147064  * Walk each entry in the data link configuration repository and
2405895Syz147064  * call fn on the linkid and arg.
2415895Syz147064  */
2425895Syz147064 dladm_status_t
2435895Syz147064 dladm_walk_datalink_id(int (*fn)(datalink_id_t, void *), void *argp,
2445895Syz147064     datalink_class_t class, datalink_media_t dmedia, uint32_t flags)
2455895Syz147064 {
2465895Syz147064 	dlmgmt_door_getnext_t	getnext;
2475895Syz147064 	dlmgmt_getnext_retval_t	retval;
2485895Syz147064 	uint32_t 		dlmgmt_flags;
2495895Syz147064 	datalink_id_t		linkid = DATALINK_INVALID_LINKID;
2505895Syz147064 	dladm_status_t		status = DLADM_STATUS_OK;
2515895Syz147064 
2525895Syz147064 	if (fn == NULL)
2535895Syz147064 		return (DLADM_STATUS_BADARG);
2545895Syz147064 
2555895Syz147064 	dlmgmt_flags = (flags & DLADM_OPT_ACTIVE) ? DLMGMT_ACTIVE : 0;
2565895Syz147064 	dlmgmt_flags |= ((flags & DLADM_OPT_PERSIST) ? DLMGMT_PERSIST : 0);
2575895Syz147064 
2585895Syz147064 	getnext.ld_cmd = DLMGMT_CMD_GETNEXT;
2595895Syz147064 	getnext.ld_class = class;
2605895Syz147064 	getnext.ld_dmedia = dmedia;
2615895Syz147064 	getnext.ld_flags = dlmgmt_flags;
2625895Syz147064 
2635895Syz147064 	do {
2645895Syz147064 		getnext.ld_linkid = linkid;
265*6263Sseb 		if ((status = dladm_door_call(&getnext, sizeof (getnext),
266*6263Sseb 		    &retval, sizeof (retval))) != DLADM_STATUS_OK) {
2675895Syz147064 			/*
2685895Syz147064 			 * done with walking
2695895Syz147064 			 */
2705895Syz147064 			break;
2715895Syz147064 		}
2725895Syz147064 
2735895Syz147064 		linkid = retval.lr_linkid;
2745895Syz147064 		if ((retval.lr_class == DATALINK_CLASS_PHYS) &&
2755895Syz147064 		    (retval.lr_flags & DLMGMT_ACTIVE)) {
2765895Syz147064 			/*
2775895Syz147064 			 * An active physical link reported by the dlmgmtd
2785895Syz147064 			 * daemon might not be active anymore. Check its
2795895Syz147064 			 * real status.
2805895Syz147064 			 */
2815895Syz147064 			if (i_dladm_phys_status(linkid, &retval.lr_flags) !=
2825895Syz147064 			    DLADM_STATUS_OK) {
2835895Syz147064 				continue;
2845895Syz147064 			}
2855895Syz147064 
2865895Syz147064 			if (!(dlmgmt_flags & retval.lr_flags))
2875895Syz147064 				continue;
2885895Syz147064 		}
2895895Syz147064 
2905895Syz147064 		if (fn(linkid, argp) == DLADM_WALK_TERMINATE)
2915895Syz147064 			break;
2925895Syz147064 	} while (linkid != DATALINK_INVALID_LINKID);
2935895Syz147064 
2945895Syz147064 	return (status);
2955895Syz147064 }
2965895Syz147064 
2975895Syz147064 /*
2985895Syz147064  * Get the link properties structure for the given link.
2995895Syz147064  */
3005895Syz147064 dladm_status_t
3015895Syz147064 dladm_read_conf(datalink_id_t linkid, dladm_conf_t *confp)
3025895Syz147064 {
303*6263Sseb 	dlmgmt_door_readconf_t		readconf;
3045895Syz147064 	dlmgmt_readconf_retval_t	retval;
3055895Syz147064 	dladm_status_t			status;
3065895Syz147064 
3075895Syz147064 	if (linkid == DATALINK_INVALID_LINKID || confp == NULL)
3085895Syz147064 		return (DLADM_STATUS_BADARG);
3095895Syz147064 
3105895Syz147064 	readconf.ld_linkid = linkid;
3115895Syz147064 	readconf.ld_cmd = DLMGMT_CMD_READCONF;
3125895Syz147064 
313*6263Sseb 	if ((status = dladm_door_call(&readconf, sizeof (readconf),
314*6263Sseb 	    &retval, sizeof (retval))) == DLADM_STATUS_OK) {
315*6263Sseb 		*confp = retval.lr_conf;
316*6263Sseb 	}
317*6263Sseb 	return (status);
3185895Syz147064 }
3195895Syz147064 
3205895Syz147064 /*
3215895Syz147064  * Commit the given link to the data link configuration repository so
3225895Syz147064  * that it will persist across reboots.
3235895Syz147064  */
3245895Syz147064 dladm_status_t
3255895Syz147064 dladm_write_conf(dladm_conf_t conf)
3265895Syz147064 {
3275895Syz147064 	dlmgmt_door_writeconf_t		writeconf;
3285895Syz147064 	dlmgmt_writeconf_retval_t	retval;
3295895Syz147064 
3305895Syz147064 	if (conf == DLADM_INVALID_CONF)
3315895Syz147064 		return (DLADM_STATUS_BADARG);
3325895Syz147064 
3335895Syz147064 	writeconf.ld_cmd = DLMGMT_CMD_WRITECONF;
3345895Syz147064 	writeconf.ld_conf = conf;
3355895Syz147064 
336*6263Sseb 	return (dladm_door_call(&writeconf, sizeof (writeconf),
337*6263Sseb 	    &retval, sizeof (retval)));
3385895Syz147064 }
3395895Syz147064 
3405895Syz147064 /*
3415895Syz147064  * Given a link ID and a key, get the matching information from
3425895Syz147064  * data link configuration repository.
3435895Syz147064  */
3445895Syz147064 dladm_status_t
3455895Syz147064 dladm_get_conf_field(dladm_conf_t conf, const char *attr, void *attrval,
3465895Syz147064     size_t attrsz)
3475895Syz147064 {
348*6263Sseb 	dlmgmt_door_getattr_t	getattr;
349*6263Sseb 	dlmgmt_getattr_retval_t	retval;
350*6263Sseb 	dladm_status_t		status;
3515895Syz147064 
3525895Syz147064 	if (conf == DLADM_INVALID_CONF || attrval == NULL ||
353*6263Sseb 	    attrsz == 0 || attr == NULL) {
3545895Syz147064 		return (DLADM_STATUS_BADARG);
3555895Syz147064 	}
3565895Syz147064 
3575895Syz147064 	getattr.ld_cmd = DLMGMT_CMD_GETATTR;
3585895Syz147064 	getattr.ld_conf = conf;
3595895Syz147064 	(void) strlcpy(getattr.ld_attr, attr, MAXLINKATTRLEN);
3605895Syz147064 
361*6263Sseb 	if ((status = dladm_door_call(&getattr, sizeof (getattr), &retval,
362*6263Sseb 	    sizeof (retval))) != DLADM_STATUS_OK) {
363*6263Sseb 		return (status);
364*6263Sseb 	}
3655895Syz147064 
366*6263Sseb 	if (retval.lr_attrsz > attrsz)
367*6263Sseb 		return (DLADM_STATUS_TOOSMALL);
3685895Syz147064 
369*6263Sseb 	bcopy(retval.lr_attrval, attrval, retval.lr_attrsz);
370*6263Sseb 	return (DLADM_STATUS_OK);
3715895Syz147064 }
3725895Syz147064 
3735895Syz147064 /*
3745895Syz147064  * Get the link ID that is associated with the given name.
3755895Syz147064  */
3765895Syz147064 dladm_status_t
3775895Syz147064 dladm_name2info(const char *link, datalink_id_t *linkidp, uint32_t *flagp,
3785895Syz147064     datalink_class_t *classp, uint32_t *mediap)
3795895Syz147064 {
3805895Syz147064 	dlmgmt_door_getlinkid_t		getlinkid;
3815895Syz147064 	dlmgmt_getlinkid_retval_t	retval;
3825895Syz147064 	datalink_id_t			linkid;
3835895Syz147064 	dladm_status_t			status;
3845895Syz147064 
3855895Syz147064 	getlinkid.ld_cmd = DLMGMT_CMD_GETLINKID;
3865895Syz147064 	(void) strlcpy(getlinkid.ld_link, link, MAXLINKNAMELEN);
3875895Syz147064 
388*6263Sseb 	if ((status = dladm_door_call(&getlinkid, sizeof (getlinkid),
389*6263Sseb 	    &retval, sizeof (retval))) != DLADM_STATUS_OK) {
3905895Syz147064 		return (status);
391*6263Sseb 	}
3925895Syz147064 
3935895Syz147064 	linkid = retval.lr_linkid;
3945895Syz147064 	if (retval.lr_class == DATALINK_CLASS_PHYS &&
3955895Syz147064 	    retval.lr_flags & DLMGMT_ACTIVE) {
3965895Syz147064 		/*
3975895Syz147064 		 * An active physical link reported by the dlmgmtd daemon
3985895Syz147064 		 * might not be active anymore. Check and set its real status.
3995895Syz147064 		 */
4005895Syz147064 		status = i_dladm_phys_status(linkid, &retval.lr_flags);
4015895Syz147064 		if (status != DLADM_STATUS_OK)
4025895Syz147064 			return (status);
4035895Syz147064 	}
4045895Syz147064 
4055895Syz147064 	if (linkidp != NULL)
4065895Syz147064 		*linkidp = linkid;
4075895Syz147064 	if (flagp != NULL) {
4085895Syz147064 		*flagp = retval.lr_flags & DLMGMT_ACTIVE ? DLADM_OPT_ACTIVE : 0;
4095895Syz147064 		*flagp |= (retval.lr_flags & DLMGMT_PERSIST) ?
4105895Syz147064 		    DLADM_OPT_PERSIST : 0;
4115895Syz147064 	}
4125895Syz147064 	if (classp != NULL)
4135895Syz147064 		*classp = retval.lr_class;
4145895Syz147064 	if (mediap != NULL)
4155895Syz147064 		*mediap = retval.lr_media;
4165895Syz147064 
4175895Syz147064 	return (DLADM_STATUS_OK);
4185895Syz147064 }
4195895Syz147064 
4205895Syz147064 /*
4215895Syz147064  * Get the link name that is associated with the given id.
4225895Syz147064  */
4235895Syz147064 dladm_status_t
4245895Syz147064 dladm_datalink_id2info(datalink_id_t linkid, uint32_t *flagp,
4255895Syz147064     datalink_class_t *classp, uint32_t *mediap, char *link, size_t len)
4265895Syz147064 {
427*6263Sseb 	dlmgmt_door_getname_t	getname;
428*6263Sseb 	dlmgmt_getname_retval_t	retval;
429*6263Sseb 	dladm_status_t		status;
4305895Syz147064 
4315895Syz147064 	if ((linkid == DATALINK_INVALID_LINKID) || (link != NULL && len == 0) ||
4325895Syz147064 	    (link == NULL && len != 0)) {
4335895Syz147064 		return (DLADM_STATUS_BADARG);
4345895Syz147064 	}
4355895Syz147064 
4365895Syz147064 	getname.ld_cmd = DLMGMT_CMD_GETNAME;
4375895Syz147064 	getname.ld_linkid = linkid;
438*6263Sseb 	if ((status = dladm_door_call(&getname, sizeof (getname), &retval,
439*6263Sseb 	    sizeof (retval))) != DLADM_STATUS_OK) {
4405895Syz147064 		return (status);
441*6263Sseb 	}
4425895Syz147064 
443*6263Sseb 	if (len != 0 && (strlen(retval.lr_link) + 1 > len))
4445895Syz147064 		return (DLADM_STATUS_TOOSMALL);
4455895Syz147064 
4465895Syz147064 	if (retval.lr_class == DATALINK_CLASS_PHYS &&
4475895Syz147064 	    retval.lr_flags & DLMGMT_ACTIVE) {
4485895Syz147064 		/*
4495895Syz147064 		 * An active physical link reported by the dlmgmtd daemon
4505895Syz147064 		 * might not be active anymore. Check and set its real status.
4515895Syz147064 		 */
4525895Syz147064 		status = i_dladm_phys_status(linkid, &retval.lr_flags);
4535895Syz147064 		if (status != DLADM_STATUS_OK)
4545895Syz147064 			return (status);
4555895Syz147064 	}
4565895Syz147064 
4575895Syz147064 	if (link != NULL)
4585895Syz147064 		(void) strlcpy(link, retval.lr_link, len);
4595895Syz147064 	if (classp != NULL)
4605895Syz147064 		*classp = retval.lr_class;
4615895Syz147064 	if (mediap != NULL)
4625895Syz147064 		*mediap = retval.lr_media;
4635895Syz147064 	if (flagp != NULL) {
4645895Syz147064 		*flagp = retval.lr_flags & DLMGMT_ACTIVE ?
4655895Syz147064 		    DLADM_OPT_ACTIVE : 0;
4665895Syz147064 		*flagp |= (retval.lr_flags & DLMGMT_PERSIST) ?
4675895Syz147064 		    DLADM_OPT_PERSIST : 0;
4685895Syz147064 	}
4695895Syz147064 	return (DLADM_STATUS_OK);
4705895Syz147064 }
4715895Syz147064 
4725895Syz147064 /*
4735895Syz147064  * Set the given attr with the given attrval for the given link.
4745895Syz147064  */
4755895Syz147064 dladm_status_t
4765895Syz147064 dladm_set_conf_field(dladm_conf_t conf, const char *attr,
4775895Syz147064     dladm_datatype_t type, const void *attrval)
4785895Syz147064 {
479*6263Sseb 	dlmgmt_door_setattr_t	setattr;
480*6263Sseb 	dlmgmt_setattr_retval_t	retval;
481*6263Sseb 	size_t			attrsz;
4825895Syz147064 
483*6263Sseb 	if (attr == NULL || attrval == NULL)
4845895Syz147064 		return (DLADM_STATUS_BADARG);
4855895Syz147064 
4865895Syz147064 	if (type == DLADM_TYPE_STR)
4875895Syz147064 		attrsz = strlen(attrval) + 1;
4885895Syz147064 	else
4895895Syz147064 		attrsz = dladm_datatype_size[type];
4905895Syz147064 
491*6263Sseb 	if (attrsz > MAXLINKATTRVALLEN)
492*6263Sseb 		return (DLADM_STATUS_TOOSMALL);
4935895Syz147064 
494*6263Sseb 	setattr.ld_cmd = DLMGMT_CMD_SETATTR;
495*6263Sseb 	setattr.ld_conf = conf;
496*6263Sseb 	(void) strlcpy(setattr.ld_attr, attr, MAXLINKATTRLEN);
497*6263Sseb 	setattr.ld_attrsz = attrsz;
498*6263Sseb 	setattr.ld_type = type;
499*6263Sseb 	bcopy(attrval, &setattr.ld_attrval, attrsz);
5005895Syz147064 
501*6263Sseb 	return (dladm_door_call(&setattr, sizeof (setattr),
502*6263Sseb 	    &retval, sizeof (retval)));
5035895Syz147064 }
5045895Syz147064 
5055895Syz147064 /*
5065895Syz147064  * Unset the given attr the given link.
5075895Syz147064  */
5085895Syz147064 dladm_status_t
5095895Syz147064 dladm_unset_conf_field(dladm_conf_t conf, const char *attr)
5105895Syz147064 {
5115895Syz147064 	dlmgmt_door_unsetattr_t		unsetattr;
5125895Syz147064 	dlmgmt_unsetattr_retval_t	retval;
5135895Syz147064 
514*6263Sseb 	if (attr == NULL)
5155895Syz147064 		return (DLADM_STATUS_BADARG);
5165895Syz147064 
5175895Syz147064 	unsetattr.ld_cmd = DLMGMT_CMD_UNSETATTR;
5185895Syz147064 	unsetattr.ld_conf = conf;
5195895Syz147064 	(void) strlcpy(unsetattr.ld_attr, attr, MAXLINKATTRLEN);
5205895Syz147064 
521*6263Sseb 	return (dladm_door_call(&unsetattr, sizeof (unsetattr),
522*6263Sseb 	    &retval, sizeof (retval)));
5235895Syz147064 }
5245895Syz147064 
5255895Syz147064 /*
5265895Syz147064  * Remove the given link ID and its entry from the data link configuration
5275895Syz147064  * repository.
5285895Syz147064  */
5295895Syz147064 dladm_status_t
5305895Syz147064 dladm_remove_conf(datalink_id_t linkid)
5315895Syz147064 {
5325895Syz147064 	dlmgmt_door_removeconf_t	removeconf;
5335895Syz147064 	dlmgmt_removeconf_retval_t	retval;
5345895Syz147064 
5355895Syz147064 	removeconf.ld_cmd = DLMGMT_CMD_REMOVECONF;
5365895Syz147064 	removeconf.ld_linkid = linkid;
5375895Syz147064 
538*6263Sseb 	return (dladm_door_call(&removeconf, sizeof (removeconf),
539*6263Sseb 	    &retval, sizeof (retval)));
5405895Syz147064 }
5415895Syz147064 
5425895Syz147064 /*
5435895Syz147064  * Free the contents of the link structure.
5445895Syz147064  */
5455895Syz147064 void
5465895Syz147064 dladm_destroy_conf(dladm_conf_t conf)
5475895Syz147064 {
5485895Syz147064 	dlmgmt_door_destroyconf_t	destroyconf;
5495895Syz147064 	dlmgmt_destroyconf_retval_t	retval;
5505895Syz147064 
5515895Syz147064 	if (conf == DLADM_INVALID_CONF)
5525895Syz147064 		return;
5535895Syz147064 
5545895Syz147064 	destroyconf.ld_cmd = DLMGMT_CMD_DESTROYCONF;
5555895Syz147064 	destroyconf.ld_conf = conf;
5565895Syz147064 
557*6263Sseb 	(void) dladm_door_call(&destroyconf, sizeof (destroyconf),
558*6263Sseb 	    &retval, sizeof (retval));
5595895Syz147064 }
560