xref: /titanic_50/usr/src/lib/libdladm/common/linkprop.c (revision 577f4726f2ea5aeaa2cf3dd65aca52869834b137)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdlib.h>
29 #include <strings.h>
30 #include <errno.h>
31 #include <ctype.h>
32 #include <stddef.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/dld.h>
36 #include <sys/zone.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <libdevinfo.h>
40 #include <zone.h>
41 #include <libdllink.h>
42 #include <libdladm_impl.h>
43 #include <libdlwlan_impl.h>
44 #include <libdlwlan.h>
45 #include <libdlvlan.h>
46 #include <dlfcn.h>
47 #include <link.h>
48 #include <inet/wifi_ioctl.h>
49 #include <libdladm.h>
50 #include <sys/param.h>
51 #include <sys/dld.h>
52 #include <inttypes.h>
53 #include <sys/ethernet.h>
54 
55 /*
56  * The linkprop get() callback.
57  * - pd: 	pointer to the struct prop_desc
58  * - propstrp:	a property string array to keep the returned property.
59  *		Caller allocated.
60  * - cntp:	number of returned properties.
61  *		Caller also uses it to indicate how many it expects.
62  */
63 struct prop_desc;
64 
65 typedef dladm_status_t	pd_getf_t(struct prop_desc *pd,
66 			datalink_id_t, char **propstp, uint_t *cntp,
67 			datalink_media_t, uint_t);
68 
69 /*
70  * The linkprop set() callback.
71  * - propval:	a val_desc_t array which keeps the property values to be set.
72  * - cnt:	number of properties to be set.
73  * - flags: 	additional flags passed down the system call.
74  *
75  * pd_set takes val_desc_t given by pd_check(), translates it into
76  * a format suitable for kernel consumption. This may require allocation
77  * of ioctl buffers etc. pd_set() may call another common routine (used
78  * by all other pd_sets) which invokes the ioctl.
79  */
80 typedef dladm_status_t	pd_setf_t(struct prop_desc *, datalink_id_t,
81 			val_desc_t *propval, uint_t cnt, uint_t flags,
82 			datalink_media_t);
83 
84 
85 /*
86  * The linkprop check() callback.
87  * - propstrp:	property string array which keeps the property to be checked.
88  * - cnt:	number of properties.
89  * - propval:	return value; the property values of the given property strings.
90  *
91  * pd_check checks that the input values are valid. It does so by
92  * iteraring through the pd_modval list for the property. If
93  * the modifiable values cannot be expressed as a list, a pd_check
94  * specific to this property can be used. If the input values are
95  * verified to be valid, pd_check allocates a val_desc_t and fills it
96  * with either a val_desc_t found on the pd_modval list or something
97  * generated on the fly.
98  */
99 typedef dladm_status_t	pd_checkf_t(struct prop_desc *pd,
100 			    datalink_id_t, char **propstrp,
101 			    uint_t cnt, val_desc_t *propval,
102 			    datalink_media_t);
103 
104 typedef struct dld_public_prop_s {
105 	mac_prop_id_t	pp_id;
106 	size_t		pp_valsize;
107 	char		*pp_name;
108 	char		*pp_desc;
109 } dld_public_prop_t;
110 
111 static dld_ioc_macprop_t *dld_buf_alloc(size_t, datalink_id_t, const char *,
112 					uint_t, dladm_status_t *);
113 static dladm_status_t dld_set_prop(datalink_id_t, const char *, char **,
114 					uint_t, uint_t);
115 static dladm_status_t dld_get_prop(datalink_id_t, const char *, char **,
116 					uint_t *, dladm_prop_type_t, uint_t);
117 static pd_getf_t	do_get_zone, do_get_autopush, do_get_rate_mod,
118 			do_get_rate_prop, do_get_channel_prop,
119 			do_get_powermode_prop, do_get_radio_prop,
120 			dld_duplex_get, dld_status_get,
121 			dld_binary_get, dld_uint32_get, dld_flowctl_get;
122 static pd_setf_t	do_set_zone, do_set_autopush, do_set_rate_prop,
123 			do_set_powermode_prop, do_set_radio_prop,
124 			dld_set_public_prop;
125 static pd_checkf_t	do_check_zone, do_check_autopush, do_check_rate,
126 			dld_defmtu_check;
127 
128 static dladm_status_t	dld_speed_get(struct prop_desc *, datalink_id_t,
129 			char **, uint_t *, uint_t);
130 
131 typedef struct prop_desc {
132 	/*
133 	 * link property name
134 	 */
135 	char			*pd_name;
136 
137 	/*
138 	 * default property value, can be set to { "", NULL }
139 	 */
140 	val_desc_t		pd_defval;
141 
142 	/*
143 	 * list of optional property values, can be NULL.
144 	 *
145 	 * This is set to non-NULL if there is a list of possible property
146 	 * values.  pd_optval would point to the array of possible values.
147 	 */
148 	val_desc_t		*pd_optval;
149 
150 	/*
151 	 * count of the above optional property values. 0 if pd_optval is NULL.
152 	 */
153 	uint_t			pd_noptval;
154 
155 	/*
156 	 * callback to set link property;
157 	 * set to NULL if this property is read-only
158 	 */
159 	pd_setf_t		*pd_set;
160 
161 	/*
162 	 * callback to get modifiable link property
163 	 */
164 	pd_getf_t		*pd_getmod;
165 
166 	/*
167 	 * callback to get current link property
168 	 */
169 	pd_getf_t		*pd_get;
170 
171 	/*
172 	 * callback to validate link property value, set to NULL if pd_optval
173 	 * is not NULL. In that case, validate the value by comparing it with
174 	 * the pd_optval. Return a val_desc_t array pointer if the value is
175 	 * valid.
176 	 */
177 	pd_checkf_t		*pd_check;
178 
179 	uint_t			pd_flags;
180 #define	PD_TEMPONLY	0x1	/* property is temporary only */
181 #define	PD_CHECK_ALLOC	0x2	/* alloc vd_val as part of pd_check */
182 #define	PD_EMPTY_RESET	0x4	/* Use "" to reset the link property */
183 	/*
184 	 * indicate link classes this property applies to.
185 	 */
186 	datalink_class_t	pd_class;
187 
188 	/*
189 	 * indicate link media type this property applies to.
190 	 */
191 	datalink_media_t	pd_dmedia;
192 } prop_desc_t;
193 
194 #define	MAC_PROP_BUFSIZE(v)	sizeof (dld_ioc_macprop_t) + (v) - 1
195 
196 
197 static dld_public_prop_t dld_prop[] = {
198 	{ MAC_PROP_DUPLEX,	sizeof (link_duplex_t),
199 	    "duplex",		"link duplex mode" },
200 
201 	{MAC_PROP_SPEED,	sizeof (uint64_t),
202 	    "speed",		"link speed (bps)" },
203 
204 	{ MAC_PROP_STATUS,	sizeof (link_state_t),
205 	    "state",		"link up/down" },
206 
207 	{ MAC_PROP_AUTONEG,	sizeof (uint8_t),
208 	    "adv_autoneg_cap",	"Advertised auto-negotiation" },
209 
210 	{ MAC_PROP_MTU,		sizeof (uint32_t),
211 	    "mtu",		"current link mtu" },
212 
213 	{ MAC_PROP_FLOWCTRL,	sizeof (link_flowctrl_t),
214 	    "flowctrl",		"flowcontrol" },
215 
216 	{ MAC_PROP_ADV_1000FDX_CAP, sizeof (uint8_t),
217 	    "adv_1000fdx_cap",	"Adv 1000 Mbps fdx" },
218 
219 	{ MAC_PROP_EN_1000FDX_CAP, sizeof (uint8_t),
220 	    "en_1000fdx_cap",	"Enable 1000 Mbps fdx" },
221 
222 	{ MAC_PROP_ADV_1000HDX_CAP, sizeof (uint8_t),
223 	    "adv_1000hdx_cap", "Adv 1000 Mbps hdx" },
224 
225 	{ MAC_PROP_EN_1000HDX_CAP, sizeof (uint8_t),
226 	    "en_1000hdx_cap",	"Enable 1000 Mbps hdx" },
227 
228 	{ MAC_PROP_ADV_100FDX_CAP, sizeof (uint8_t),
229 	    "adv_100fdx_cap",	"Adv 100 Mbps fdx" },
230 
231 	{ MAC_PROP_EN_100FDX_CAP, sizeof (uint8_t),
232 	    "en_100fdx_cap",	"Enable 100 Mbps fdx" },
233 
234 	{ MAC_PROP_ADV_100HDX_CAP, sizeof (uint8_t),
235 	    "adv_100hdx_cap",	"Adv 100 Mbps hdx" },
236 
237 	{ MAC_PROP_EN_100HDX_CAP, sizeof (uint8_t),
238 	    "en_100hdx_cap",	"Enable 100 Mbps hdx" },
239 
240 	{ MAC_PROP_ADV_10FDX_CAP, sizeof (uint8_t),
241 	    "adv_10fdx_cap",	"Adv 10 Mbps fdx" },
242 
243 	{ MAC_PROP_EN_10FDX_CAP, sizeof (uint8_t),
244 	    "en_10fdx_cap",	"Enable 10 Mbps fdx" },
245 
246 	{ MAC_PROP_ADV_10HDX_CAP, sizeof (uint8_t),
247 	    "adv_10hdx_cap",	"Adv 10 Mbps hdx" },
248 
249 	{ MAC_PROP_EN_10HDX_CAP, sizeof (uint8_t),
250 	    "en_10hdx_cap",	"Enable 10 Mbps hdx" },
251 
252 	{ MAC_PROP_PRIVATE, 0,
253 	    "driver-private",	"" }
254 };
255 
256 static  val_desc_t	link_duplex_vals[] = {
257 	{ "half", 	LINK_DUPLEX_HALF	},
258 	{ "full", 	LINK_DUPLEX_HALF	}
259 };
260 static  val_desc_t	link_status_vals[] = {
261 	{ "up",		LINK_STATE_UP		},
262 	{ "down",	LINK_STATE_DOWN		}
263 };
264 static  val_desc_t	link_01_vals[] = {
265 	{ "1",		1			},
266 	{ "0",		0			}
267 };
268 static  val_desc_t	link_flow_vals[] = {
269 	{ "no",		LINK_FLOWCTRL_NONE	},
270 	{ "tx",		LINK_FLOWCTRL_TX	},
271 	{ "rx",		LINK_FLOWCTRL_RX	},
272 	{ "bi",		LINK_FLOWCTRL_BI	}
273 };
274 
275 #define	VALCNT(vals)    (sizeof ((vals)) / sizeof (val_desc_t))
276 
277 static val_desc_t	dladm_wlan_radio_vals[] = {
278 	{ "on",		DLADM_WLAN_RADIO_ON	},
279 	{ "off",	DLADM_WLAN_RADIO_OFF	}
280 };
281 
282 static val_desc_t	dladm_wlan_powermode_vals[] = {
283 	{ "off",	DLADM_WLAN_PM_OFF	},
284 	{ "fast",	DLADM_WLAN_PM_FAST	},
285 	{ "max",	DLADM_WLAN_PM_MAX	}
286 };
287 
288 static prop_desc_t	prop_table[] = {
289 
290 	{ "channel",	{ NULL, 0 },
291 	    NULL, 0, NULL, NULL,
292 	    do_get_channel_prop, NULL, 0,
293 	    DATALINK_CLASS_PHYS, DL_WIFI },
294 
295 	{ "powermode",	{ "off", DLADM_WLAN_PM_OFF },
296 	    dladm_wlan_powermode_vals, VALCNT(dladm_wlan_powermode_vals),
297 	    do_set_powermode_prop, NULL,
298 	    do_get_powermode_prop, NULL, 0,
299 	    DATALINK_CLASS_PHYS, DL_WIFI },
300 
301 	{ "radio",	{ "on", DLADM_WLAN_RADIO_ON },
302 	    dladm_wlan_radio_vals, VALCNT(dladm_wlan_radio_vals),
303 	    do_set_radio_prop, NULL,
304 	    do_get_radio_prop, NULL, 0,
305 	    DATALINK_CLASS_PHYS, DL_WIFI },
306 
307 	{ "speed",	{ "", 0 }, NULL, 0,
308 	    do_set_rate_prop, do_get_rate_mod,
309 	    do_get_rate_prop, do_check_rate, 0,
310 	    DATALINK_CLASS_PHYS, DATALINK_ANY_MEDIATYPE },
311 
312 	{ "autopush",	{ "", 0 }, NULL, 0,
313 	    do_set_autopush, NULL,
314 	    do_get_autopush, do_check_autopush, PD_CHECK_ALLOC|PD_EMPTY_RESET,
315 	    DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE },
316 
317 	{ "zone",	{ "", 0 }, NULL, 0,
318 	    do_set_zone, NULL,
319 	    do_get_zone, do_check_zone, PD_TEMPONLY|PD_EMPTY_RESET,
320 	    DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE },
321 
322 	{ "duplex", { "", 0 },
323 	    link_duplex_vals, VALCNT(link_duplex_vals),
324 	    NULL, NULL, dld_duplex_get, NULL,
325 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
326 
327 	{ "state", { "up", LINK_STATE_UP },
328 	    link_status_vals, VALCNT(link_status_vals),
329 	    NULL, NULL, dld_status_get, NULL,
330 	    0, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE },
331 
332 	{ "adv_autoneg_cap", { "1", 1 },
333 	    link_01_vals, VALCNT(link_01_vals),
334 	    dld_set_public_prop, NULL, dld_binary_get, NULL,
335 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
336 
337 	{ "mtu", { "", 0 }, NULL, 0,
338 	    dld_set_public_prop, NULL, dld_uint32_get,
339 	    dld_defmtu_check, 0, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE },
340 
341 	{ "flowctrl", { "", 0 },
342 	    link_flow_vals, VALCNT(link_flow_vals),
343 	    dld_set_public_prop, NULL, dld_flowctl_get, NULL,
344 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
345 
346 	{ "adv_1000fdx_cap", { "", 0 },
347 	    link_01_vals, VALCNT(link_01_vals),
348 	    NULL, NULL, dld_binary_get, NULL,
349 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
350 
351 	{ "en_1000fdx_cap", { "", 0 },
352 	    link_01_vals, VALCNT(link_01_vals),
353 	    dld_set_public_prop, NULL, dld_binary_get, NULL,
354 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
355 
356 	{ "adv_1000hdx_cap", { "", 0 },
357 	    link_01_vals, VALCNT(link_01_vals),
358 	    NULL, NULL, dld_binary_get, NULL,
359 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
360 
361 	{ "en_1000hdx_cap", { "", 0 },
362 	    link_01_vals, VALCNT(link_01_vals),
363 	    dld_set_public_prop, NULL, dld_binary_get, NULL,
364 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
365 
366 	{ "adv_100fdx_cap", { "", 0 },
367 	    link_01_vals, VALCNT(link_01_vals),
368 	    NULL, NULL, dld_binary_get, NULL,
369 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
370 
371 	{ "en_100fdx_cap", { "", 0 },
372 	    link_01_vals, VALCNT(link_01_vals),
373 	    dld_set_public_prop, NULL, dld_binary_get, NULL,
374 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
375 
376 	{ "adv_100hdx_cap", { "", 0 },
377 	    link_01_vals, VALCNT(link_01_vals),
378 	    NULL, NULL, dld_binary_get, NULL,
379 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
380 
381 	{ "en_100hdx_cap", { "", 0 },
382 	    link_01_vals, VALCNT(link_01_vals),
383 	    dld_set_public_prop, NULL, dld_binary_get, NULL,
384 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
385 
386 	{ "adv_10fdx_cap", { "", 0 },
387 	    link_01_vals, VALCNT(link_01_vals),
388 	    NULL, NULL, dld_binary_get, NULL,
389 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
390 
391 	{ "en_10fdx_cap", { "", 0 },
392 	    link_01_vals, VALCNT(link_01_vals),
393 	    dld_set_public_prop, NULL, dld_binary_get, NULL,
394 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
395 
396 	{ "adv_10hdx_cap", { "", 0 },
397 	    link_01_vals, VALCNT(link_01_vals),
398 	    NULL, NULL, dld_binary_get, NULL,
399 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
400 
401 	{ "en_10hdx_cap", { "", 0 },
402 	    link_01_vals, VALCNT(link_01_vals),
403 	    dld_set_public_prop, NULL, dld_binary_get, NULL,
404 	    0, DATALINK_CLASS_PHYS, DL_ETHER }
405 
406 };
407 
408 #define	DLADM_MAX_PROPS	(sizeof (prop_table) / sizeof (prop_desc_t))
409 
410 static dladm_status_t	i_dladm_set_linkprop_db(datalink_id_t, const char *,
411 			    char **, uint_t);
412 static dladm_status_t	i_dladm_get_linkprop_db(datalink_id_t, const char *,
413 			    char **, uint_t *);
414 static dladm_status_t	i_dladm_set_single_prop(datalink_id_t, datalink_class_t,
415 			    uint32_t, prop_desc_t *, char **, uint_t, uint_t);
416 static dladm_status_t	i_dladm_set_linkprop(datalink_id_t, const char *,
417 			    char **, uint_t, uint_t);
418 static dladm_status_t	i_dladm_getset_defval(prop_desc_t *, datalink_id_t,
419 			    datalink_media_t, uint_t);
420 /*
421  * Unfortunately, MAX_SCAN_SUPPORT_RATES is too small to allow all
422  * rates to be retrieved. However, we cannot increase it at this
423  * time because it will break binary compatibility with unbundled
424  * WiFi drivers and utilities. So for now we define an additional
425  * constant, MAX_SUPPORT_RATES, to allow all rates to be retrieved.
426  */
427 #define	MAX_SUPPORT_RATES	64
428 
429 #define	AP_ANCHOR	"[anchor]"
430 #define	AP_DELIMITER	'.'
431 
432 static dladm_status_t
433 do_check_prop(prop_desc_t *pdp, char **prop_val, uint_t val_cnt,
434     val_desc_t *vdp)
435 {
436 	int		i, j;
437 	dladm_status_t	status = DLADM_STATUS_OK;
438 
439 	for (j = 0; j < val_cnt; j++) {
440 		for (i = 0; i < pdp->pd_noptval; i++) {
441 			if (strcasecmp(*prop_val,
442 			    pdp->pd_optval[i].vd_name) == 0) {
443 				break;
444 			}
445 		}
446 		if (i == pdp->pd_noptval) {
447 			status = DLADM_STATUS_BADVAL;
448 			goto done;
449 		}
450 		(void) memcpy(vdp + j, &pdp->pd_optval[i], sizeof (val_desc_t));
451 	}
452 
453 done:
454 	return (status);
455 }
456 
457 static dladm_status_t
458 i_dladm_set_single_prop(datalink_id_t linkid, datalink_class_t class,
459     uint32_t media, prop_desc_t *pdp, char **prop_val, uint_t val_cnt,
460     uint_t flags)
461 {
462 	dladm_status_t	status = DLADM_STATUS_OK;
463 	val_desc_t	*vdp = NULL;
464 	boolean_t	needfree = B_FALSE;
465 	uint_t		cnt, i;
466 
467 	if (!(pdp->pd_class & class))
468 		return (DLADM_STATUS_BADARG);
469 
470 	if (!DATALINK_MEDIA_ACCEPTED(pdp->pd_dmedia, media))
471 		return (DLADM_STATUS_BADARG);
472 
473 	if ((flags & DLADM_OPT_PERSIST) && (pdp->pd_flags & PD_TEMPONLY))
474 		return (DLADM_STATUS_TEMPONLY);
475 
476 	if (!(flags & DLADM_OPT_ACTIVE))
477 		return (DLADM_STATUS_OK);
478 
479 	if (pdp->pd_set == NULL)
480 		return (DLADM_STATUS_PROPRDONLY);
481 
482 	if (pdp->pd_flags & PD_CHECK_ALLOC)
483 		needfree = B_TRUE;
484 	else
485 		needfree = B_FALSE;
486 	if (prop_val != NULL) {
487 		vdp = malloc(sizeof (val_desc_t) * val_cnt);
488 		if (vdp == NULL)
489 			return (DLADM_STATUS_NOMEM);
490 
491 
492 		if (pdp->pd_check != NULL) {
493 			status = pdp->pd_check(pdp, linkid, prop_val, val_cnt,
494 			    vdp, media);
495 		} else if (pdp->pd_optval != NULL) {
496 			status = do_check_prop(pdp, prop_val, val_cnt, vdp);
497 		} else {
498 			status = DLADM_STATUS_BADARG;
499 		}
500 
501 		if (status != DLADM_STATUS_OK)
502 			goto done;
503 
504 		cnt = val_cnt;
505 	} else {
506 		if (pdp->pd_defval.vd_name == NULL)
507 			return (DLADM_STATUS_NOTSUP);
508 
509 		if ((pdp->pd_flags & PD_EMPTY_RESET) != 0 ||
510 		    strlen(pdp->pd_defval.vd_name) > 0) {
511 			if ((vdp = malloc(sizeof (val_desc_t))) == NULL)
512 				return (DLADM_STATUS_NOMEM);
513 			(void) memcpy(vdp, &pdp->pd_defval,
514 			    sizeof (val_desc_t));
515 		} else {
516 			status = i_dladm_getset_defval(pdp, linkid,
517 			    media, flags);
518 			return (status);
519 		}
520 		cnt = 1;
521 	}
522 	status = pdp->pd_set(pdp, linkid, vdp, cnt, flags, media);
523 	if (needfree) {
524 		for (i = 0; i < cnt; i++)
525 			free((void *)((val_desc_t *)vdp + i)->vd_val);
526 	}
527 done:
528 	free(vdp);
529 	return (status);
530 }
531 
532 static dladm_status_t
533 i_dladm_set_linkprop(datalink_id_t linkid, const char *prop_name,
534     char **prop_val, uint_t val_cnt, uint_t flags)
535 {
536 	int			i;
537 	boolean_t		found = B_FALSE;
538 	datalink_class_t	class;
539 	uint32_t		media;
540 	dladm_status_t		status = DLADM_STATUS_OK;
541 
542 	status = dladm_datalink_id2info(linkid, NULL, &class, &media, NULL, 0);
543 	if (status != DLADM_STATUS_OK)
544 		return (status);
545 
546 	for (i = 0; i < DLADM_MAX_PROPS; i++) {
547 		prop_desc_t	*pdp = &prop_table[i];
548 		dladm_status_t	s;
549 
550 		if (prop_name != NULL &&
551 		    (strcasecmp(prop_name, pdp->pd_name) != 0))
552 			continue;
553 
554 		found = B_TRUE;
555 		s = i_dladm_set_single_prop(linkid, class, media, pdp, prop_val,
556 		    val_cnt, flags);
557 
558 		if (prop_name != NULL) {
559 			status = s;
560 			break;
561 		} else {
562 			if (s != DLADM_STATUS_OK &&
563 			    s != DLADM_STATUS_NOTSUP)
564 				status = s;
565 		}
566 	}
567 	if (!found) {
568 		if (prop_name[0] == '_') {
569 			/* other private properties */
570 			status = dld_set_prop(linkid, prop_name, prop_val,
571 			    val_cnt, flags);
572 		} else  {
573 			status = DLADM_STATUS_NOTFOUND;
574 		}
575 	}
576 
577 	return (status);
578 }
579 
580 /*
581  * Set/reset link property for specific link
582  */
583 dladm_status_t
584 dladm_set_linkprop(datalink_id_t linkid, const char *prop_name, char **prop_val,
585     uint_t val_cnt, uint_t flags)
586 {
587 	dladm_status_t	status = DLADM_STATUS_OK;
588 
589 	if ((linkid == DATALINK_INVALID_LINKID) || (flags == 0) ||
590 	    (prop_val == NULL && val_cnt > 0) ||
591 	    (prop_val != NULL && val_cnt == 0) ||
592 	    (prop_name == NULL && prop_val != NULL)) {
593 		return (DLADM_STATUS_BADARG);
594 	}
595 
596 	status = i_dladm_set_linkprop(linkid, prop_name, prop_val,
597 	    val_cnt, flags);
598 	if (status != DLADM_STATUS_OK)
599 		return (status);
600 
601 	if (flags & DLADM_OPT_PERSIST) {
602 		status = i_dladm_set_linkprop_db(linkid, prop_name,
603 		    prop_val, val_cnt);
604 	}
605 	return (status);
606 }
607 
608 /*
609  * Walk link properties of the given specific link.
610  */
611 dladm_status_t
612 dladm_walk_linkprop(datalink_id_t linkid, void *arg,
613     int (*func)(datalink_id_t, const char *, void *))
614 {
615 	dladm_status_t		status;
616 	datalink_class_t	class;
617 	uint_t			media;
618 	int			i;
619 
620 	if (linkid == DATALINK_INVALID_LINKID || func == NULL)
621 		return (DLADM_STATUS_BADARG);
622 
623 	status = dladm_datalink_id2info(linkid, NULL, &class, &media, NULL, 0);
624 	if (status != DLADM_STATUS_OK)
625 		return (status);
626 
627 	for (i = 0; i < DLADM_MAX_PROPS; i++) {
628 		if (!(prop_table[i].pd_class & class))
629 			continue;
630 
631 		if (!DATALINK_MEDIA_ACCEPTED(prop_table[i].pd_dmedia, media))
632 			continue;
633 
634 		if (func(linkid, prop_table[i].pd_name, arg) ==
635 		    DLADM_WALK_TERMINATE) {
636 			break;
637 		}
638 	}
639 
640 	return (DLADM_STATUS_OK);
641 }
642 
643 /*
644  * Get linkprop of the given specific link.
645  */
646 dladm_status_t
647 dladm_get_linkprop(datalink_id_t linkid, dladm_prop_type_t type,
648     const char *prop_name, char **prop_val, uint_t *val_cntp)
649 {
650 	dladm_status_t		status = DLADM_STATUS_OK;
651 	datalink_class_t	class;
652 	uint_t			media;
653 	prop_desc_t		*pdp;
654 	uint_t			cnt, dld_flags = 0;
655 	int			i;
656 
657 	if (type == DLADM_PROP_VAL_DEFAULT)
658 		dld_flags = MAC_PROP_DEFAULT;
659 
660 	if (linkid == DATALINK_INVALID_LINKID || prop_name == NULL ||
661 	    prop_val == NULL || val_cntp == NULL || *val_cntp == 0)
662 		return (DLADM_STATUS_BADARG);
663 
664 	for (i = 0; i < DLADM_MAX_PROPS; i++)
665 		if (strcasecmp(prop_name, prop_table[i].pd_name) == 0)
666 			break;
667 
668 	if (i == DLADM_MAX_PROPS) {
669 		if (prop_name[0] == '_') {
670 			/*
671 			 * private property.
672 			 */
673 			return (dld_get_prop(linkid, prop_name,
674 			    prop_val, val_cntp, type, dld_flags));
675 		} else {
676 			return (DLADM_STATUS_NOTFOUND);
677 		}
678 	}
679 
680 	pdp = &prop_table[i];
681 
682 	status = dladm_datalink_id2info(linkid, NULL, &class, &media, NULL, 0);
683 	if (status != DLADM_STATUS_OK)
684 		return (status);
685 
686 	if (!(pdp->pd_class & class))
687 		return (DLADM_STATUS_BADARG);
688 
689 	if (!DATALINK_MEDIA_ACCEPTED(pdp->pd_dmedia, media))
690 		return (DLADM_STATUS_BADARG);
691 
692 	switch (type) {
693 	case DLADM_PROP_VAL_CURRENT:
694 		status = pdp->pd_get(pdp, linkid, prop_val, val_cntp, media,
695 		    dld_flags);
696 		break;
697 
698 	case DLADM_PROP_VAL_DEFAULT:
699 		/*
700 		 * If defaults are not defined for the property,
701 		 * pd_defval.vd_name should be null. If the driver
702 		 * has to be contacted for the value, vd_name should
703 		 * be the empty string (""). Otherwise, dladm will
704 		 * just print whatever is in the table.
705 		 */
706 		if (pdp->pd_defval.vd_name == NULL) {
707 			status = DLADM_STATUS_NOTSUP;
708 			break;
709 		}
710 
711 		if (strlen(pdp->pd_defval.vd_name) == 0) {
712 			status = pdp->pd_get(pdp, linkid, prop_val, val_cntp,
713 			    media, dld_flags);
714 		} else {
715 			(void) strcpy(*prop_val, pdp->pd_defval.vd_name);
716 		}
717 		*val_cntp = 1;
718 		break;
719 
720 	case DLADM_PROP_VAL_MODIFIABLE:
721 		if (pdp->pd_getmod != NULL) {
722 			status = pdp->pd_getmod(pdp, linkid, prop_val,
723 			    val_cntp, media, dld_flags);
724 			break;
725 		}
726 		cnt = pdp->pd_noptval;
727 		if (cnt == 0) {
728 			status = DLADM_STATUS_NOTSUP;
729 		} else if (cnt > *val_cntp) {
730 			status = DLADM_STATUS_TOOSMALL;
731 		} else {
732 			for (i = 0; i < cnt; i++) {
733 				(void) strcpy(prop_val[i],
734 				    pdp->pd_optval[i].vd_name);
735 			}
736 			*val_cntp = cnt;
737 		}
738 		break;
739 	case DLADM_PROP_VAL_PERSISTENT:
740 		if (pdp->pd_flags & PD_TEMPONLY)
741 			return (DLADM_STATUS_TEMPONLY);
742 		status = i_dladm_get_linkprop_db(linkid, prop_name,
743 		    prop_val, val_cntp);
744 		break;
745 	default:
746 		status = DLADM_STATUS_BADARG;
747 		break;
748 	}
749 
750 	return (status);
751 }
752 
753 /*ARGSUSED*/
754 static int
755 i_dladm_init_one_prop(datalink_id_t linkid, const char *prop_name, void *arg)
756 {
757 	char	*buf, **propvals;
758 	uint_t	i, valcnt = DLADM_MAX_PROP_VALCNT;
759 
760 	if ((buf = malloc((sizeof (char *) + DLADM_PROP_VAL_MAX) *
761 	    DLADM_MAX_PROP_VALCNT)) == NULL) {
762 		return (DLADM_WALK_CONTINUE);
763 	}
764 
765 	propvals = (char **)(void *)buf;
766 	for (i = 0; i < valcnt; i++) {
767 		propvals[i] = buf +
768 		    sizeof (char *) * DLADM_MAX_PROP_VALCNT +
769 		    i * DLADM_PROP_VAL_MAX;
770 	}
771 
772 	if (dladm_get_linkprop(linkid, DLADM_PROP_VAL_PERSISTENT, prop_name,
773 	    propvals, &valcnt) != DLADM_STATUS_OK) {
774 		goto done;
775 	}
776 
777 	(void) dladm_set_linkprop(linkid, prop_name, propvals, valcnt,
778 	    DLADM_OPT_ACTIVE);
779 
780 done:
781 	if (buf != NULL)
782 		free(buf);
783 
784 	return (DLADM_WALK_CONTINUE);
785 }
786 
787 /*ARGSUSED*/
788 static int
789 i_dladm_init_linkprop(datalink_id_t linkid, void *arg)
790 {
791 	(void) dladm_init_linkprop(linkid);
792 	return (DLADM_WALK_CONTINUE);
793 }
794 
795 dladm_status_t
796 dladm_init_linkprop(datalink_id_t linkid)
797 {
798 	if (linkid == DATALINK_ALL_LINKID) {
799 		(void) dladm_walk_datalink_id(i_dladm_init_linkprop, NULL,
800 		    DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE,
801 		    DLADM_OPT_PERSIST);
802 	} else {
803 		(void) dladm_walk_linkprop(linkid, NULL, i_dladm_init_one_prop);
804 	}
805 	return (DLADM_STATUS_OK);
806 }
807 
808 /* ARGSUSED */
809 static dladm_status_t
810 do_get_zone(struct prop_desc *pd, datalink_id_t linkid,
811     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
812 {
813 	char		zone_name[ZONENAME_MAX];
814 	zoneid_t	zid;
815 	dladm_status_t	status;
816 
817 	if (flags != 0)
818 		return (DLADM_STATUS_NOTSUP);
819 
820 	status = dladm_getzid(linkid, &zid);
821 	if (status != DLADM_STATUS_OK)
822 		return (status);
823 
824 	*val_cnt = 1;
825 	if (zid != GLOBAL_ZONEID) {
826 		if (getzonenamebyid(zid, zone_name, sizeof (zone_name)) < 0)
827 			return (dladm_errno2status(errno));
828 
829 		(void) strncpy(*prop_val, zone_name, DLADM_PROP_VAL_MAX);
830 	} else {
831 		*prop_val[0] = '\0';
832 	}
833 
834 	return (DLADM_STATUS_OK);
835 }
836 
837 typedef int (*zone_get_devroot_t)(char *, char *, size_t);
838 
839 static int
840 i_dladm_get_zone_dev(char *zone_name, char *dev, size_t devlen)
841 {
842 	char			root[MAXPATHLEN];
843 	zone_get_devroot_t	real_zone_get_devroot;
844 	void			*dlhandle;
845 	void			*sym;
846 	int			ret;
847 
848 	if ((dlhandle = dlopen("libzonecfg.so.1", RTLD_LAZY)) == NULL)
849 		return (-1);
850 
851 	if ((sym = dlsym(dlhandle, "zone_get_devroot")) == NULL) {
852 		(void) dlclose(dlhandle);
853 		return (-1);
854 	}
855 
856 	real_zone_get_devroot = (zone_get_devroot_t)sym;
857 
858 	if ((ret = real_zone_get_devroot(zone_name, root, sizeof (root))) == 0)
859 		(void) snprintf(dev, devlen, "%s%s", root, "/dev");
860 	(void) dlclose(dlhandle);
861 	return (ret);
862 }
863 
864 static dladm_status_t
865 i_dladm_update_deventry(zoneid_t zid, datalink_id_t linkid, boolean_t add)
866 {
867 	char		path[MAXPATHLEN];
868 	char		name[MAXLINKNAMELEN];
869 	di_prof_t	prof = NULL;
870 	char		zone_name[ZONENAME_MAX];
871 	dladm_status_t	status;
872 	int		ret;
873 
874 	if (getzonenamebyid(zid, zone_name, sizeof (zone_name)) < 0)
875 		return (dladm_errno2status(errno));
876 	if (i_dladm_get_zone_dev(zone_name, path, sizeof (path)) != 0)
877 		return (dladm_errno2status(errno));
878 	if (di_prof_init(path, &prof) != 0)
879 		return (dladm_errno2status(errno));
880 
881 	status = dladm_linkid2legacyname(linkid, name, MAXLINKNAMELEN);
882 	if (status != DLADM_STATUS_OK)
883 		goto cleanup;
884 
885 	if (add)
886 		ret = di_prof_add_dev(prof, name);
887 	else
888 		ret = di_prof_add_exclude(prof, name);
889 
890 	if (ret != 0) {
891 		status = dladm_errno2status(errno);
892 		goto cleanup;
893 	}
894 
895 	if (di_prof_commit(prof) != 0)
896 		status = dladm_errno2status(errno);
897 cleanup:
898 	if (prof)
899 		di_prof_fini(prof);
900 
901 	return (status);
902 }
903 
904 /* ARGSUSED */
905 static dladm_status_t
906 do_set_zone(prop_desc_t *pd, datalink_id_t linkid, val_desc_t *vdp,
907     uint_t val_cnt, uint_t flags, datalink_media_t media)
908 {
909 	dladm_status_t	status;
910 	zoneid_t	zid_old, zid_new;
911 	char		link[MAXLINKNAMELEN];
912 
913 	if (val_cnt != 1)
914 		return (DLADM_STATUS_BADVALCNT);
915 
916 	status = dladm_getzid(linkid, &zid_old);
917 	if (status != DLADM_STATUS_OK)
918 		return (status);
919 
920 	/* Do nothing if setting to current value */
921 	zid_new = vdp->vd_val;
922 	if (zid_new == zid_old)
923 		return (DLADM_STATUS_OK);
924 
925 	if ((status = dladm_datalink_id2info(linkid, NULL, NULL, NULL,
926 	    link, MAXLINKNAMELEN)) != DLADM_STATUS_OK) {
927 		return (status);
928 	}
929 
930 	if (zid_new != GLOBAL_ZONEID) {
931 		/*
932 		 * If the new zoneid is the global zone, we could destroy
933 		 * the link (in the case of an implicitly-created VLAN) as a
934 		 * result of the dladm_setzid() operation. In that case,
935 		 * we defer the operation to the end of this function to avoid
936 		 * recreating the VLAN and getting a different linkid during
937 		 * the rollback if other operation fails.
938 		 *
939 		 * Otherwise, dladm_setzid() will hold a reference to the
940 		 * link and prevent a link renaming, so we need to do it
941 		 * before other operations.
942 		 */
943 		status = dladm_setzid(link, zid_new);
944 		if (status != DLADM_STATUS_OK)
945 			return (status);
946 	}
947 
948 	if (zid_old != GLOBAL_ZONEID) {
949 		if (zone_remove_datalink(zid_old, link) != 0 &&
950 		    errno != ENXIO) {
951 			status = dladm_errno2status(errno);
952 			goto rollback1;
953 		}
954 
955 		/*
956 		 * It is okay to fail to update the /dev entry (some
957 		 * vanity-named links do not have a /dev entry).
958 		 */
959 		(void) i_dladm_update_deventry(zid_old, linkid, B_FALSE);
960 	}
961 
962 	if (zid_new != GLOBAL_ZONEID) {
963 		if (zone_add_datalink(zid_new, link) != 0) {
964 			status = dladm_errno2status(errno);
965 			goto rollback2;
966 		}
967 
968 		(void) i_dladm_update_deventry(zid_new, linkid, B_TRUE);
969 	} else {
970 		status = dladm_setzid(link, zid_new);
971 		if (status != DLADM_STATUS_OK)
972 			goto rollback2;
973 	}
974 
975 	return (DLADM_STATUS_OK);
976 
977 rollback2:
978 	if (zid_old != GLOBAL_ZONEID)
979 		(void) i_dladm_update_deventry(zid_old, linkid, B_TRUE);
980 	if (zid_old != GLOBAL_ZONEID)
981 		(void) zone_add_datalink(zid_old, link);
982 rollback1:
983 	if (zid_new != GLOBAL_ZONEID)
984 		(void) dladm_setzid(link, zid_old);
985 	return (status);
986 }
987 
988 /* ARGSUSED */
989 static dladm_status_t
990 do_check_zone(struct prop_desc *pd, datalink_id_t linkid, char **prop_val,
991     uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
992 {
993 	zoneid_t	zid;
994 
995 	if (val_cnt != 1)
996 		return (DLADM_STATUS_BADVALCNT);
997 
998 	if ((zid = getzoneidbyname(*prop_val)) == -1)
999 		return (DLADM_STATUS_BADVAL);
1000 
1001 	if (zid != GLOBAL_ZONEID) {
1002 		ushort_t	flags;
1003 
1004 		if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags,
1005 		    sizeof (flags)) < 0) {
1006 			return (dladm_errno2status(errno));
1007 		}
1008 
1009 		if (!(flags & ZF_NET_EXCL)) {
1010 			return (DLADM_STATUS_BADVAL);
1011 		}
1012 	}
1013 
1014 	vdp->vd_val = zid;
1015 	return (DLADM_STATUS_OK);
1016 }
1017 
1018 /* ARGSUSED */
1019 static dladm_status_t
1020 do_get_autopush(struct prop_desc *pd, datalink_id_t linkid,
1021     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1022 {
1023 	dld_ioc_ap_t	dia;
1024 	int		fd, i, len;
1025 
1026 	if (flags & MAC_PROP_DEFAULT)
1027 		return (DLADM_STATUS_NOTSUP);
1028 
1029 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
1030 		return (dladm_errno2status(errno));
1031 
1032 	*val_cnt = 1;
1033 	dia.dia_linkid = linkid;
1034 	if (i_dladm_ioctl(fd, DLDIOC_GETAUTOPUSH, &dia, sizeof (dia)) < 0) {
1035 		(*prop_val)[0] = '\0';
1036 		goto done;
1037 	}
1038 
1039 	for (i = 0, len = 0; i < dia.dia_npush; i++) {
1040 		if (i != 0) {
1041 			(void) snprintf(*prop_val + len,
1042 			    DLADM_PROP_VAL_MAX - len, "%c", AP_DELIMITER);
1043 			len += 1;
1044 		}
1045 		(void) snprintf(*prop_val + len, DLADM_PROP_VAL_MAX - len,
1046 		    "%s", dia.dia_aplist[i]);
1047 		len += strlen(dia.dia_aplist[i]);
1048 		if (dia.dia_anchor - 1 == i) {
1049 			(void) snprintf(*prop_val + len,
1050 			    DLADM_PROP_VAL_MAX - len, "%c%s", AP_DELIMITER,
1051 			    AP_ANCHOR);
1052 			len += (strlen(AP_ANCHOR) + 1);
1053 		}
1054 	}
1055 
1056 done:
1057 	(void) close(fd);
1058 	return (DLADM_STATUS_OK);
1059 }
1060 
1061 /* ARGSUSED */
1062 static dladm_status_t
1063 do_set_autopush(prop_desc_t *pd, datalink_id_t linkid, val_desc_t *vdp,
1064     uint_t val_cnt, uint_t flags, datalink_media_t media)
1065 {
1066 	dld_ioc_ap_t		dia;
1067 	struct dlautopush	*dlap = (struct dlautopush *)vdp->vd_val;
1068 	dladm_status_t		status = DLADM_STATUS_OK;
1069 	int			fd, i;
1070 	int			ic_cmd;
1071 
1072 	if (val_cnt != 1)
1073 		return (DLADM_STATUS_BADVALCNT);
1074 
1075 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
1076 		return (dladm_errno2status(errno));
1077 
1078 	dia.dia_linkid = linkid;
1079 	if (dlap != NULL) {
1080 		dia.dia_anchor = dlap->dap_anchor;
1081 		dia.dia_npush = dlap->dap_npush;
1082 		for (i = 0; i < dia.dia_npush; i++) {
1083 			(void) strlcpy(dia.dia_aplist[i], dlap->dap_aplist[i],
1084 			    FMNAMESZ+1);
1085 		}
1086 		ic_cmd = DLDIOC_SETAUTOPUSH;
1087 	} else {
1088 		ic_cmd = DLDIOC_CLRAUTOPUSH;
1089 	}
1090 
1091 	if (i_dladm_ioctl(fd, ic_cmd, &dia, sizeof (dia)) < 0)
1092 		status = dladm_errno2status(errno);
1093 
1094 	(void) close(fd);
1095 	return (status);
1096 }
1097 
1098 /*
1099  * Add the specified module to the dlautopush structure; returns a
1100  * DLADM_STATUS_* code.
1101  */
1102 dladm_status_t
1103 i_dladm_add_ap_module(const char *module, struct dlautopush *dlap)
1104 {
1105 	if ((strlen(module) == 0) || (strlen(module) > FMNAMESZ))
1106 		return (DLADM_STATUS_BADVAL);
1107 
1108 	if (strncasecmp(module, AP_ANCHOR, strlen(AP_ANCHOR)) == 0) {
1109 		/*
1110 		 * We don't allow multiple anchors, and the anchor must
1111 		 * be after at least one module.
1112 		 */
1113 		if (dlap->dap_anchor != 0)
1114 			return (DLADM_STATUS_BADVAL);
1115 		if (dlap->dap_npush == 0)
1116 			return (DLADM_STATUS_BADVAL);
1117 
1118 		dlap->dap_anchor = dlap->dap_npush;
1119 		return (DLADM_STATUS_OK);
1120 	}
1121 	if (dlap->dap_npush > MAXAPUSH)
1122 		return (DLADM_STATUS_BADVALCNT);
1123 
1124 	(void) strlcpy(dlap->dap_aplist[dlap->dap_npush++], module,
1125 	    FMNAMESZ + 1);
1126 
1127 	return (DLADM_STATUS_OK);
1128 }
1129 
1130 /*
1131  * Currently, both '.' and ' '(space) can be used as the delimiters between
1132  * autopush modules. The former is used in dladm set-linkprop, and the
1133  * latter is used in the autopush(1M) file.
1134  */
1135 /* ARGSUSED */
1136 static dladm_status_t
1137 do_check_autopush(struct prop_desc *pd, datalink_id_t linkid, char **prop_val,
1138     uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1139 {
1140 	char			*module;
1141 	struct dlautopush	*dlap;
1142 	dladm_status_t		status;
1143 	char			val[DLADM_PROP_VAL_MAX];
1144 	char			delimiters[4];
1145 
1146 	if (val_cnt != 1)
1147 		return (DLADM_STATUS_BADVALCNT);
1148 
1149 	dlap = malloc(sizeof (struct dlautopush));
1150 	if (dlap == NULL)
1151 		return (DLADM_STATUS_NOMEM);
1152 
1153 	(void) memset(dlap, 0, sizeof (struct dlautopush));
1154 	(void) snprintf(delimiters, 4, " %c\n", AP_DELIMITER);
1155 	bcopy(*prop_val, val, DLADM_PROP_VAL_MAX);
1156 	module = strtok(val, delimiters);
1157 	while (module != NULL) {
1158 		status = i_dladm_add_ap_module(module, dlap);
1159 		if (status != DLADM_STATUS_OK)
1160 			return (status);
1161 		module = strtok(NULL, delimiters);
1162 	}
1163 
1164 	vdp->vd_val = (uintptr_t)dlap;
1165 	return (DLADM_STATUS_OK);
1166 }
1167 
1168 /* ARGSUSED */
1169 static dladm_status_t
1170 do_get_rate_common(struct prop_desc *pd, datalink_id_t linkid,
1171     char **prop_val, uint_t *val_cnt, uint_t id)
1172 {
1173 	wl_rates_t	*wrp;
1174 	uint_t		i;
1175 	wldp_t		*gbuf = NULL;
1176 	dladm_status_t	status = DLADM_STATUS_OK;
1177 
1178 	if ((gbuf = malloc(MAX_BUF_LEN)) == NULL) {
1179 		status = DLADM_STATUS_NOMEM;
1180 		goto done;
1181 	}
1182 
1183 	status = i_dladm_wlan_get_ioctl(linkid, gbuf, id);
1184 	if (status != DLADM_STATUS_OK)
1185 		goto done;
1186 
1187 	wrp = (wl_rates_t *)gbuf->wldp_buf;
1188 	if (wrp->wl_rates_num > *val_cnt) {
1189 		status = DLADM_STATUS_TOOSMALL;
1190 		goto done;
1191 	}
1192 
1193 	if (wrp->wl_rates_rates[0] == 0) {
1194 		prop_val[0][0] = '\0';
1195 		*val_cnt = 1;
1196 		goto done;
1197 	}
1198 
1199 	for (i = 0; i < wrp->wl_rates_num; i++) {
1200 		(void) snprintf(prop_val[i], DLADM_STRSIZE, "%.*f",
1201 		    wrp->wl_rates_rates[i] % 2,
1202 		    (float)wrp->wl_rates_rates[i] / 2);
1203 	}
1204 	*val_cnt = wrp->wl_rates_num;
1205 
1206 done:
1207 	free(gbuf);
1208 	return (status);
1209 }
1210 
1211 static dladm_status_t
1212 do_get_rate_prop(struct prop_desc *pd, datalink_id_t linkid,
1213     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1214 {
1215 	if (media != DL_WIFI)
1216 		return (dld_speed_get(pd, linkid, prop_val, val_cnt, flags));
1217 
1218 	return (do_get_rate_common(pd, linkid, prop_val, val_cnt,
1219 	    WL_DESIRED_RATES));
1220 }
1221 
1222 /* ARGSUSED */
1223 static dladm_status_t
1224 do_get_rate_mod(struct prop_desc *pd, datalink_id_t linkid,
1225     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1226 {
1227 	switch (media) {
1228 	case DL_ETHER:
1229 		/*
1230 		 * Speed for ethernet links is unbounded. E.g., 802.11b
1231 		 * links can have a speed of 5.5 Gbps.
1232 		 */
1233 		return (DLADM_STATUS_NOTSUP);
1234 
1235 	case DL_WIFI:
1236 		return (do_get_rate_common(pd, linkid, prop_val, val_cnt,
1237 		    WL_SUPPORTED_RATES));
1238 	default:
1239 		return (DLADM_STATUS_BADARG);
1240 	}
1241 }
1242 
1243 static dladm_status_t
1244 do_set_rate(datalink_id_t linkid, dladm_wlan_rates_t *rates)
1245 {
1246 	int		i;
1247 	uint_t		len;
1248 	wldp_t		*gbuf;
1249 	wl_rates_t	*wrp;
1250 	dladm_status_t	status = DLADM_STATUS_OK;
1251 
1252 	if ((gbuf = malloc(MAX_BUF_LEN)) == NULL)
1253 		return (DLADM_STATUS_NOMEM);
1254 
1255 	(void) memset(gbuf, 0, MAX_BUF_LEN);
1256 
1257 	wrp = (wl_rates_t *)gbuf->wldp_buf;
1258 	for (i = 0; i < rates->wr_cnt; i++)
1259 		wrp->wl_rates_rates[i] = rates->wr_rates[i];
1260 	wrp->wl_rates_num = rates->wr_cnt;
1261 
1262 	len = offsetof(wl_rates_t, wl_rates_rates) +
1263 	    (rates->wr_cnt * sizeof (char)) + WIFI_BUF_OFFSET;
1264 	status = i_dladm_wlan_ioctl(linkid, gbuf, WL_DESIRED_RATES, len,
1265 	    WLAN_SET_PARAM, len);
1266 
1267 	free(gbuf);
1268 	return (status);
1269 }
1270 
1271 /* ARGSUSED */
1272 static dladm_status_t
1273 do_set_rate_prop(prop_desc_t *pd, datalink_id_t linkid,
1274     val_desc_t *vdp, uint_t val_cnt, uint_t flags, datalink_media_t media)
1275 {
1276 	dladm_wlan_rates_t	rates;
1277 	dladm_status_t		status;
1278 
1279 	/*
1280 	 * can currently set rate on WIFI links only.
1281 	 */
1282 	if (media != DL_WIFI)
1283 		return (DLADM_STATUS_PROPRDONLY);
1284 
1285 	if (val_cnt != 1)
1286 		return (DLADM_STATUS_BADVALCNT);
1287 
1288 	rates.wr_cnt = 1;
1289 	rates.wr_rates[0] = vdp[0].vd_val;
1290 
1291 	status = do_set_rate(linkid, &rates);
1292 
1293 done:
1294 	return (status);
1295 }
1296 
1297 /* ARGSUSED */
1298 static dladm_status_t
1299 do_check_rate(struct prop_desc *pd, datalink_id_t linkid, char **prop_val,
1300     uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1301 {
1302 	int		i;
1303 	uint_t		modval_cnt = MAX_SUPPORT_RATES;
1304 	char		*buf, **modval;
1305 	dladm_status_t	status;
1306 
1307 	if (val_cnt != 1)
1308 		return (DLADM_STATUS_BADVALCNT);
1309 
1310 	buf = malloc((sizeof (char *) + DLADM_STRSIZE) *
1311 	    MAX_SUPPORT_RATES);
1312 	if (buf == NULL) {
1313 		status = DLADM_STATUS_NOMEM;
1314 		goto done;
1315 	}
1316 
1317 	modval = (char **)(void *)buf;
1318 	for (i = 0; i < MAX_SUPPORT_RATES; i++) {
1319 		modval[i] = buf + sizeof (char *) * MAX_SUPPORT_RATES +
1320 		    i * DLADM_STRSIZE;
1321 	}
1322 
1323 	status = do_get_rate_mod(NULL, linkid, modval, &modval_cnt, media, 0);
1324 	if (status != DLADM_STATUS_OK)
1325 		goto done;
1326 
1327 	for (i = 0; i < modval_cnt; i++) {
1328 		if (strcasecmp(*prop_val, modval[i]) == 0) {
1329 			vdp->vd_val = (uintptr_t)(uint_t)
1330 			    (atof(*prop_val) * 2);
1331 			status = DLADM_STATUS_OK;
1332 			break;
1333 		}
1334 	}
1335 	if (i == modval_cnt)
1336 		status = DLADM_STATUS_BADVAL;
1337 done:
1338 	free(buf);
1339 	return (status);
1340 }
1341 
1342 static dladm_status_t
1343 do_get_phyconf(datalink_id_t linkid, wldp_t *gbuf)
1344 {
1345 	return (i_dladm_wlan_get_ioctl(linkid, gbuf, WL_PHY_CONFIG));
1346 }
1347 
1348 /* ARGSUSED */
1349 static dladm_status_t
1350 do_get_channel_prop(struct prop_desc *pd, datalink_id_t linkid,
1351     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1352 {
1353 	uint32_t	channel;
1354 	wldp_t		*gbuf;
1355 	dladm_status_t	status = DLADM_STATUS_OK;
1356 
1357 	if ((gbuf = malloc(MAX_BUF_LEN)) == NULL)
1358 		return (DLADM_STATUS_NOMEM);
1359 
1360 	if ((status = do_get_phyconf(linkid, gbuf)) != DLADM_STATUS_OK)
1361 		goto done;
1362 
1363 	if (!i_dladm_wlan_convert_chan((wl_phy_conf_t *)gbuf->wldp_buf,
1364 	    &channel)) {
1365 		status = DLADM_STATUS_NOTFOUND;
1366 		goto done;
1367 	}
1368 
1369 	(void) snprintf(*prop_val, DLADM_STRSIZE, "%u", channel);
1370 	*val_cnt = 1;
1371 
1372 done:
1373 	free(gbuf);
1374 	return (status);
1375 }
1376 
1377 static dladm_status_t
1378 do_get_powermode(datalink_id_t linkid, wldp_t *gbuf)
1379 {
1380 	return (i_dladm_wlan_get_ioctl(linkid, gbuf, WL_POWER_MODE));
1381 }
1382 
1383 /* ARGSUSED */
1384 static dladm_status_t
1385 do_get_powermode_prop(struct prop_desc *pd, datalink_id_t linkid,
1386     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1387 {
1388 	wl_ps_mode_t	*mode;
1389 	const char	*s;
1390 	wldp_t		*gbuf;
1391 	dladm_status_t	status = DLADM_STATUS_OK;
1392 
1393 	if ((gbuf = malloc(MAX_BUF_LEN)) == NULL)
1394 		return (DLADM_STATUS_NOMEM);
1395 
1396 	if ((status = do_get_powermode(linkid, gbuf)) != DLADM_STATUS_OK)
1397 		goto done;
1398 
1399 	mode = (wl_ps_mode_t *)(gbuf->wldp_buf);
1400 	switch (mode->wl_ps_mode) {
1401 	case WL_PM_AM:
1402 		s = "off";
1403 		break;
1404 	case WL_PM_MPS:
1405 		s = "max";
1406 		break;
1407 	case WL_PM_FAST:
1408 		s = "fast";
1409 		break;
1410 	default:
1411 		status = DLADM_STATUS_NOTFOUND;
1412 		goto done;
1413 	}
1414 	(void) snprintf(*prop_val, DLADM_STRSIZE, "%s", s);
1415 	*val_cnt = 1;
1416 
1417 done:
1418 	free(gbuf);
1419 	return (status);
1420 }
1421 
1422 static dladm_status_t
1423 do_set_powermode(datalink_id_t linkid, dladm_wlan_powermode_t *pm)
1424 {
1425 	wl_ps_mode_t    ps_mode;
1426 
1427 	(void) memset(&ps_mode, 0xff, sizeof (ps_mode));
1428 
1429 	switch (*pm) {
1430 	case DLADM_WLAN_PM_OFF:
1431 		ps_mode.wl_ps_mode = WL_PM_AM;
1432 		break;
1433 	case DLADM_WLAN_PM_MAX:
1434 		ps_mode.wl_ps_mode = WL_PM_MPS;
1435 		break;
1436 	case DLADM_WLAN_PM_FAST:
1437 		ps_mode.wl_ps_mode = WL_PM_FAST;
1438 		break;
1439 	default:
1440 		return (DLADM_STATUS_NOTSUP);
1441 	}
1442 	return (i_dladm_wlan_set_ioctl(linkid, WL_POWER_MODE, &ps_mode,
1443 	    sizeof (ps_mode)));
1444 }
1445 
1446 /* ARGSUSED */
1447 static dladm_status_t
1448 do_set_powermode_prop(prop_desc_t *pd, datalink_id_t linkid,
1449     val_desc_t *vdp, uint_t val_cnt, uint_t flags, datalink_media_t media)
1450 {
1451 	dladm_wlan_powermode_t powermode = (dladm_wlan_powermode_t)vdp->vd_val;
1452 	dladm_status_t status;
1453 
1454 	if (val_cnt != 1)
1455 		return (DLADM_STATUS_BADVALCNT);
1456 
1457 	status = do_set_powermode(linkid, &powermode);
1458 
1459 	return (status);
1460 }
1461 
1462 static dladm_status_t
1463 do_get_radio(datalink_id_t linkid, wldp_t *gbuf)
1464 {
1465 	return (i_dladm_wlan_get_ioctl(linkid, gbuf, WL_RADIO));
1466 }
1467 
1468 /* ARGSUSED */
1469 static dladm_status_t
1470 do_get_radio_prop(struct prop_desc *pd, datalink_id_t linkid,
1471     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1472 {
1473 	wl_radio_t	radio;
1474 	const char	*s;
1475 	wldp_t		*gbuf;
1476 	dladm_status_t	status = DLADM_STATUS_OK;
1477 
1478 	if ((gbuf = malloc(MAX_BUF_LEN)) == NULL)
1479 		return (DLADM_STATUS_NOMEM);
1480 
1481 	if ((status = do_get_radio(linkid, gbuf)) != DLADM_STATUS_OK)
1482 		goto done;
1483 
1484 	radio = *(wl_radio_t *)(gbuf->wldp_buf);
1485 	switch (radio) {
1486 	case B_TRUE:
1487 		s = "on";
1488 		break;
1489 	case B_FALSE:
1490 		s = "off";
1491 		break;
1492 	default:
1493 		status = DLADM_STATUS_NOTFOUND;
1494 		goto done;
1495 	}
1496 	(void) snprintf(*prop_val, DLADM_STRSIZE, "%s", s);
1497 	*val_cnt = 1;
1498 
1499 done:
1500 	free(gbuf);
1501 	return (status);
1502 }
1503 
1504 static dladm_status_t
1505 do_set_radio(datalink_id_t linkid, dladm_wlan_radio_t *radio)
1506 {
1507 	wl_radio_t r;
1508 
1509 	switch (*radio) {
1510 	case DLADM_WLAN_RADIO_ON:
1511 		r = B_TRUE;
1512 		break;
1513 	case DLADM_WLAN_RADIO_OFF:
1514 		r = B_FALSE;
1515 		break;
1516 	default:
1517 		return (DLADM_STATUS_NOTSUP);
1518 	}
1519 	return (i_dladm_wlan_set_ioctl(linkid, WL_RADIO, &r, sizeof (r)));
1520 }
1521 
1522 /* ARGSUSED */
1523 static dladm_status_t
1524 do_set_radio_prop(prop_desc_t *pd, datalink_id_t linkid,
1525     val_desc_t *vdp, uint_t val_cnt, uint_t fags, datalink_media_t media)
1526 {
1527 	dladm_wlan_radio_t radio = (dladm_wlan_radio_t)vdp->vd_val;
1528 	dladm_status_t status;
1529 
1530 	if (val_cnt != 1)
1531 		return (DLADM_STATUS_BADVALCNT);
1532 
1533 	status = do_set_radio(linkid, &radio);
1534 
1535 	return (status);
1536 }
1537 
1538 static dladm_status_t
1539 i_dladm_set_linkprop_db(datalink_id_t linkid, const char *prop_name,
1540     char **prop_val, uint_t val_cnt)
1541 {
1542 	char		buf[MAXLINELEN];
1543 	int		i;
1544 	dladm_conf_t	conf;
1545 	dladm_status_t	status;
1546 
1547 	status = dladm_read_conf(linkid, &conf);
1548 	if (status != DLADM_STATUS_OK)
1549 		return (status);
1550 
1551 	/*
1552 	 * reset case.
1553 	 */
1554 	if (val_cnt == 0) {
1555 		status = dladm_unset_conf_field(conf, prop_name);
1556 		if (status == DLADM_STATUS_OK)
1557 			status = dladm_write_conf(conf);
1558 		goto done;
1559 	}
1560 
1561 	buf[0] = '\0';
1562 	for (i = 0; i < val_cnt; i++) {
1563 		(void) strlcat(buf, prop_val[i], MAXLINELEN);
1564 		if (i != val_cnt - 1)
1565 			(void) strlcat(buf, ",", MAXLINELEN);
1566 	}
1567 
1568 	status = dladm_set_conf_field(conf, prop_name, DLADM_TYPE_STR, buf);
1569 	if (status == DLADM_STATUS_OK)
1570 		status = dladm_write_conf(conf);
1571 
1572 done:
1573 	dladm_destroy_conf(conf);
1574 	return (status);
1575 }
1576 
1577 static dladm_status_t
1578 i_dladm_get_linkprop_db(datalink_id_t linkid, const char *prop_name,
1579     char **prop_val, uint_t *val_cntp)
1580 {
1581 	char		buf[MAXLINELEN], *str;
1582 	uint_t		cnt = 0;
1583 	dladm_conf_t	conf;
1584 	dladm_status_t	status;
1585 
1586 	status = dladm_read_conf(linkid, &conf);
1587 	if (status != DLADM_STATUS_OK)
1588 		return (status);
1589 
1590 	status = dladm_get_conf_field(conf, prop_name, buf, MAXLINELEN);
1591 	if (status != DLADM_STATUS_OK)
1592 		goto done;
1593 
1594 	str = strtok(buf, ",");
1595 	while (str != NULL) {
1596 		if (cnt == *val_cntp) {
1597 			status = DLADM_STATUS_TOOSMALL;
1598 			goto done;
1599 		}
1600 		(void) strlcpy(prop_val[cnt++], str, DLADM_PROP_VAL_MAX);
1601 		str = strtok(NULL, ",");
1602 	}
1603 
1604 	*val_cntp = cnt;
1605 
1606 done:
1607 	dladm_destroy_conf(conf);
1608 	return (status);
1609 }
1610 
1611 static dld_public_prop_t *
1612 dladm_name2prop(const char *prop_name)
1613 {
1614 	dld_public_prop_t *p;
1615 
1616 	for (p = dld_prop; p->pp_id != MAC_PROP_PRIVATE; p++) {
1617 		if (strcmp(p->pp_name, prop_name) == 0)
1618 			break;
1619 	}
1620 	return (p);
1621 }
1622 
1623 
1624 static dld_ioc_macprop_t *
1625 dld_buf_alloc(size_t valsize, datalink_id_t linkid, const char *prop_name,
1626     uint_t flags, dladm_status_t *status)
1627 {
1628 	int dsize;
1629 	dld_ioc_macprop_t *dip;
1630 	dld_public_prop_t *p;
1631 
1632 	*status = DLADM_STATUS_OK;
1633 	p = dladm_name2prop(prop_name);
1634 	if (p->pp_id != MAC_PROP_PRIVATE)
1635 		valsize = p->pp_valsize;
1636 
1637 	dsize = MAC_PROP_BUFSIZE(valsize);
1638 	dip = malloc(dsize);
1639 	if (dip == NULL) {
1640 		*status = DLADM_STATUS_NOMEM;
1641 		return (NULL);
1642 	}
1643 	bzero(dip, dsize);
1644 	dip->pr_valsize = valsize;
1645 	(void) strlcpy(dip->pr_name, prop_name, sizeof (dip->pr_name));
1646 	dip->pr_version = MAC_PROP_VERSION;
1647 	dip->pr_linkid = linkid;
1648 	dip->pr_num = p->pp_id;
1649 	dip->pr_flags = flags;
1650 	return (dip);
1651 }
1652 
1653 /* ARGSUSED */
1654 static dladm_status_t
1655 dld_set_public_prop(prop_desc_t *pd, datalink_id_t linkid,
1656     val_desc_t *vdp, uint_t val_cnt, uint_t flags, datalink_media_t media)
1657 {
1658 	dld_ioc_macprop_t	*dip;
1659 	int		fd, dsize;
1660 	dladm_status_t	status = DLADM_STATUS_OK;
1661 	uint8_t		u8;
1662 	uint16_t	u16;
1663 	uint32_t	u32;
1664 	void		*val;
1665 
1666 	dip = dld_buf_alloc(0, linkid, pd->pd_name, 0, &status);
1667 	if (dip == NULL)
1668 		return (status);
1669 
1670 	if (pd->pd_flags & PD_CHECK_ALLOC)
1671 		val = (void *)vdp->vd_val;
1672 	else {
1673 		/*
1674 		 * Currently all 1/2/4-byte size properties are byte/word/int.
1675 		 * No need (yet) to distinguish these from arrays of same size.
1676 		 */
1677 		switch (dip->pr_valsize) {
1678 		case 1:
1679 			u8 = vdp->vd_val;
1680 			val = &u8;
1681 			break;
1682 		case 2:
1683 			u16 = vdp->vd_val;
1684 			val = &u16;
1685 			break;
1686 		case 4:
1687 			u32 = vdp->vd_val;
1688 			val = &u32;
1689 			break;
1690 		default:
1691 			val = &vdp->vd_val;
1692 			break;
1693 		}
1694 	}
1695 
1696 	(void) memcpy(dip->pr_val, val, dip->pr_valsize);
1697 	dsize = MAC_PROP_BUFSIZE(dip->pr_valsize);
1698 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0) {
1699 		status = dladm_errno2status(errno);
1700 		goto done;
1701 	}
1702 	if (i_dladm_ioctl(fd, DLDIOC_SETMACPROP, dip, dsize) < 0)
1703 		status = dladm_errno2status(errno);
1704 
1705 	(void) close(fd);
1706 done:
1707 	free(dip);
1708 	return (status);
1709 }
1710 
1711 static dld_ioc_macprop_t *
1712 dld_get_public_prop(datalink_id_t linkid, char *prop_name, uint_t flags,
1713     dladm_status_t *status)
1714 {
1715 	int fd, dsize;
1716 	dld_ioc_macprop_t *dip = NULL;
1717 
1718 	*status = DLADM_STATUS_OK;
1719 
1720 	dip = dld_buf_alloc(0, linkid, prop_name, flags, status);
1721 	if (dip == NULL)
1722 		return (NULL);
1723 
1724 	dsize = MAC_PROP_BUFSIZE(dip->pr_valsize);
1725 
1726 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0) {
1727 		*status = dladm_errno2status(errno);
1728 		goto done;
1729 	}
1730 	if (i_dladm_ioctl(fd, DLDIOC_GETMACPROP, dip, dsize) < 0) {
1731 		*status = dladm_errno2status(errno);
1732 	}
1733 
1734 	(void) close(fd);
1735 done:
1736 	if (*status != DLADM_STATUS_OK) {
1737 		free(dip);
1738 		return (NULL);
1739 	}
1740 	return (dip);
1741 }
1742 
1743 /* ARGSUSED */
1744 static dladm_status_t
1745 dld_defmtu_check(struct prop_desc *pd, datalink_id_t linkid, char **prop_val,
1746     uint_t val_cnt, val_desc_t *v, datalink_media_t media)
1747 {
1748 	if (val_cnt != 1)
1749 		return (DLADM_STATUS_BADVAL);
1750 	v->vd_val = atoi(prop_val[0]);
1751 	return (DLADM_STATUS_OK);
1752 }
1753 
1754 /* ARGSUSED */
1755 static dladm_status_t
1756 dld_duplex_get(struct prop_desc *pd, datalink_id_t linkid,
1757     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1758 {
1759 	link_duplex_t   link_duplex;
1760 	dladm_status_t  status;
1761 
1762 	if (flags & MAC_PROP_DEFAULT)
1763 		return (DLADM_STATUS_NOTSUP);
1764 
1765 	if ((status = dladm_get_single_mac_stat(linkid, "link_duplex",
1766 	    KSTAT_DATA_UINT32, &link_duplex)) != 0)
1767 		return (status);
1768 
1769 	switch (link_duplex) {
1770 	case LINK_DUPLEX_FULL:
1771 		(void) strcpy(*prop_val, "full");
1772 		break;
1773 	case LINK_DUPLEX_HALF:
1774 		(void) strcpy(*prop_val, "half");
1775 		break;
1776 	default:
1777 		(void) strcpy(*prop_val, "unknown");
1778 		break;
1779 	}
1780 	*val_cnt = 1;
1781 	return (DLADM_STATUS_OK);
1782 }
1783 
1784 /* ARGSUSED */
1785 static dladm_status_t
1786 dld_speed_get(struct prop_desc *pd, datalink_id_t linkid,
1787     char **prop_val, uint_t *val_cnt, uint_t flags)
1788 {
1789 	uint64_t	ifspeed = 0;
1790 	dladm_status_t status;
1791 
1792 	if (flags & MAC_PROP_DEFAULT)
1793 		return (DLADM_STATUS_NOTSUP);
1794 
1795 	if ((status = dladm_get_single_mac_stat(linkid, "ifspeed",
1796 	    KSTAT_DATA_UINT64, &ifspeed)) != 0)
1797 		return (status);
1798 
1799 	if ((ifspeed % 1000000) != 0) {
1800 		(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX,
1801 		    "%llf", ifspeed / (float)1000000); /* Mbps */
1802 	} else {
1803 		(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX,
1804 		    "%llu", ifspeed / 1000000); /* Mbps */
1805 	}
1806 	*val_cnt = 1;
1807 	return (DLADM_STATUS_OK);
1808 }
1809 
1810 /* ARGSUSED */
1811 static dladm_status_t
1812 dld_status_get(struct prop_desc *pd, datalink_id_t linkid,
1813     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1814 {
1815 	link_state_t	link_state;
1816 	dladm_status_t	status;
1817 	uchar_t 	*cp;
1818 	dld_ioc_macprop_t  *dip;
1819 
1820 	if (flags & MAC_PROP_DEFAULT)
1821 		return (DLADM_STATUS_NOTSUP);
1822 	dip = dld_get_public_prop(linkid, pd->pd_name, flags, &status);
1823 	if (status != DLADM_STATUS_OK)
1824 		return (status);
1825 	cp = (uchar_t *)dip->pr_val;
1826 	(void) memcpy(&link_state, cp, sizeof (link_state));
1827 
1828 	switch (link_state) {
1829 	case LINK_STATE_UP:
1830 		(void) strcpy(*prop_val, "up");
1831 		break;
1832 	case LINK_STATE_DOWN:
1833 		(void) strcpy(*prop_val, "down");
1834 		break;
1835 	default:
1836 		(void) strcpy(*prop_val, "unknown");
1837 		break;
1838 	}
1839 	*val_cnt = 1;
1840 	free(dip);
1841 	return (DLADM_STATUS_OK);
1842 }
1843 
1844 /* ARGSUSED */
1845 static dladm_status_t
1846 dld_binary_get(struct prop_desc *pd, datalink_id_t linkid,
1847     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1848 {
1849 	dld_ioc_macprop_t *dip;
1850 	dladm_status_t status;
1851 
1852 	dip = dld_get_public_prop(linkid, pd->pd_name, flags, &status);
1853 	if (dip == NULL)
1854 		return (status);
1855 	(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX, "%x", dip->pr_val[0]);
1856 	free(dip);
1857 	*val_cnt = 1;
1858 	return (DLADM_STATUS_OK);
1859 }
1860 
1861 /* ARGSUSED */
1862 static dladm_status_t
1863 dld_uint32_get(struct prop_desc *pd, datalink_id_t linkid,
1864     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1865 {
1866 	dld_ioc_macprop_t *dip;
1867 	uint32_t v  = 0;
1868 	uchar_t *cp;
1869 	dladm_status_t status;
1870 
1871 	dip = dld_get_public_prop(linkid, pd->pd_name, flags, &status);
1872 	if (dip == NULL)
1873 		return (status);
1874 	cp = (uchar_t *)dip->pr_val;
1875 	(void) memcpy(&v, cp, sizeof (v));
1876 	(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX, "%ld", v);
1877 	free(dip);
1878 	*val_cnt = 1;
1879 	return (DLADM_STATUS_OK);
1880 }
1881 
1882 /* ARGSUSED */
1883 static dladm_status_t
1884 dld_flowctl_get(struct prop_desc *pd, datalink_id_t linkid,
1885     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1886 {
1887 	dld_ioc_macprop_t *dip;
1888 	link_flowctrl_t v;
1889 	dladm_status_t status;
1890 	uchar_t *cp;
1891 
1892 	dip = dld_get_public_prop(linkid, pd->pd_name, flags, &status);
1893 	if (dip == NULL)
1894 		return (status);
1895 	cp = (uchar_t *)dip->pr_val;
1896 	(void) memcpy(&v, cp, sizeof (v));
1897 	switch (v) {
1898 	case LINK_FLOWCTRL_NONE:
1899 		(void) sprintf(*prop_val, "no");
1900 		break;
1901 	case LINK_FLOWCTRL_RX:
1902 		(void) sprintf(*prop_val, "rx");
1903 		break;
1904 	case LINK_FLOWCTRL_TX:
1905 		(void) sprintf(*prop_val, "tx");
1906 		break;
1907 	case LINK_FLOWCTRL_BI:
1908 		(void) sprintf(*prop_val, "bi");
1909 		break;
1910 	}
1911 	free(dip);
1912 	*val_cnt = 1;
1913 	return (DLADM_STATUS_OK);
1914 }
1915 
1916 
1917 /* ARGSUSED */
1918 static dladm_status_t
1919 dld_set_prop(datalink_id_t linkid, const char *prop_name,
1920     char **prop_val, uint_t val_cnt, uint_t flags)
1921 {
1922 	int		fd, i, slen;
1923 	int 		bufsize = 0, dsize;
1924 	dld_ioc_macprop_t *dip = NULL;
1925 	uchar_t 	*dp;
1926 	dld_public_prop_t *p;
1927 	dladm_status_t	status = DLADM_STATUS_OK;
1928 
1929 	if ((prop_name == NULL && prop_val != NULL) ||
1930 	    (prop_val != NULL && val_cnt == 0))
1931 		return (DLADM_STATUS_BADARG);
1932 	p = dladm_name2prop(prop_name);
1933 	if (p->pp_id != MAC_PROP_PRIVATE)
1934 		return (DLADM_STATUS_BADARG);
1935 
1936 	/*
1937 	 * private properties: all parsing is done in the kernel.
1938 	 * allocate a enough space for each property + its separator (',').
1939 	 */
1940 	for (i = 0; i < val_cnt; i++) {
1941 		bufsize += strlen(prop_val[i]) + 1;
1942 	}
1943 
1944 	if (prop_val == NULL) {
1945 		/*
1946 		 * getting default value. so use more buffer space.
1947 		 */
1948 		bufsize += 1024;
1949 	}
1950 
1951 	dip = dld_buf_alloc(bufsize + 1, linkid, prop_name,
1952 	    (prop_val != NULL ? 0 : MAC_PROP_DEFAULT), &status);
1953 	if (dip == NULL)
1954 		return (status);
1955 
1956 	dp = (uchar_t *)dip->pr_val;
1957 	dsize = sizeof (dld_ioc_macprop_t) + bufsize;
1958 	slen = 0;
1959 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0) {
1960 		status = dladm_errno2status(errno);
1961 		goto done;
1962 	}
1963 	if (prop_val == NULL) {
1964 		if (i_dladm_ioctl(fd, DLDIOC_GETMACPROP, dip, dsize) < 0) {
1965 			status = dladm_errno2status(errno);
1966 			goto done;
1967 		}
1968 	} else {
1969 		for (i = 0; i < val_cnt; i++) {
1970 			int plen = 0;
1971 
1972 			plen = strlen(prop_val[i]);
1973 			bcopy(prop_val[i], dp, plen);
1974 			slen += plen;
1975 			/*
1976 			 * add a "," separator and update dp.
1977 			 */
1978 			if (i != (val_cnt -1))
1979 				dp[slen++] = ',';
1980 			dp += (plen + 1);
1981 		}
1982 	}
1983 	if (i_dladm_ioctl(fd, DLDIOC_SETMACPROP, dip, dsize) < 0) {
1984 		status = dladm_errno2status(errno);
1985 	}
1986 
1987 done:
1988 	if (fd > 0)
1989 		(void) close(fd);
1990 	free(dip);
1991 	return (status);
1992 }
1993 
1994 static dladm_status_t
1995 dld_get_prop(datalink_id_t linkid, const char *prop_name,
1996     char **prop_val, uint_t *val_cnt, dladm_prop_type_t type, uint_t dld_flags)
1997 {
1998 	int		fd;
1999 	dladm_status_t  status = DLADM_STATUS_OK;
2000 	uint_t 		dsize;
2001 	dld_ioc_macprop_t *dip = NULL;
2002 	dld_public_prop_t *p;
2003 	char tmp = '\0';
2004 
2005 	if ((prop_name == NULL && prop_val != NULL) ||
2006 	    (prop_val != NULL && val_cnt == 0))
2007 		return (DLADM_STATUS_BADARG);
2008 
2009 	p = dladm_name2prop(prop_name);
2010 	if (p->pp_id != MAC_PROP_PRIVATE)
2011 		return (DLADM_STATUS_BADARG);
2012 
2013 	if (type == DLADM_PROP_VAL_MODIFIABLE) {
2014 		*prop_val = &tmp;
2015 		*val_cnt = 1;
2016 		return (DLADM_STATUS_OK);
2017 	}
2018 
2019 	/*
2020 	 * private properties: all parsing is done in the kernel.
2021 	 */
2022 	dip = dld_buf_alloc(1024, linkid, prop_name, dld_flags, &status);
2023 	if (dip == NULL)
2024 		return (status);
2025 	dsize = MAC_PROP_BUFSIZE(dip->pr_valsize);
2026 
2027 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0) {
2028 		free(dip);
2029 		return (DLADM_STATUS_BADARG);
2030 	}
2031 
2032 	if ((status = i_dladm_ioctl(fd, DLDIOC_GETMACPROP, dip, dsize)) < 0) {
2033 		status = dladm_errno2status(errno);
2034 	} else {
2035 		(void) strncpy(*prop_val, dip->pr_val, DLADM_PROP_VAL_MAX);
2036 		*val_cnt = 1;
2037 	}
2038 
2039 	(void) close(fd);
2040 	free(dip);
2041 	return (status);
2042 }
2043 
2044 
2045 static dladm_status_t
2046 i_dladm_getset_defval(prop_desc_t *pdp, datalink_id_t linkid,
2047     datalink_media_t media, uint_t flags)
2048 {
2049 	dladm_status_t status;
2050 	char **prop_vals = NULL, *buf;
2051 	size_t bufsize;
2052 	uint_t cnt;
2053 	int i;
2054 
2055 	/*
2056 	 * Allocate buffer needed for prop_vals array. We can have at most
2057 	 * DLADM_MAX_PROP_VALCNT char *prop_vals[] entries, where
2058 	 * each entry has max size DLADM_PROP_VAL_MAX
2059 	 */
2060 	bufsize =
2061 	    (sizeof (char *) + DLADM_PROP_VAL_MAX) * DLADM_MAX_PROP_VALCNT;
2062 	buf = malloc(bufsize);
2063 	prop_vals = (char **)(void *)buf;
2064 	for (i = 0; i < DLADM_MAX_PROP_VALCNT; i++) {
2065 		prop_vals[i] = buf +
2066 		    sizeof (char *) * DLADM_MAX_PROP_VALCNT +
2067 		    i * DLADM_PROP_VAL_MAX;
2068 	}
2069 
2070 	/*
2071 	 * PD_EMPTY_RESET is used for properties like zone where the
2072 	 * "" itself is used to reset the property. So libdladm can
2073 	 * copy pdp->pd_defval over to the val_desc_t passed down on
2074 	 * the setprop using the global values in the table. For other
2075 	 * cases (PD_EMPTY_RESET is not set, vd_name is ""), doing
2076 	 * reset-linkprop will cause libdladm to do a getprop to find
2077 	 * the default value and then do a setprop to reset the value
2078 	 * to default.
2079 	 */
2080 	status = pdp->pd_get(pdp, linkid, prop_vals, &cnt, media,
2081 	    MAC_PROP_DEFAULT);
2082 	if (status == DLADM_STATUS_OK) {
2083 		status = i_dladm_set_single_prop(linkid, pdp->pd_class,
2084 		    media, pdp, prop_vals, cnt, flags);
2085 	}
2086 	free(buf);
2087 	return (status);
2088 }
2089