xref: /titanic_44/usr/src/lib/libdladm/common/linkprop.c (revision 0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02b)
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 	dld_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_prop_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	DLD_PROPBUF_SIZE(v)	sizeof (dld_ioc_prop_t) + (v) - 1
195 
196 
197 static dld_public_prop_t dld_prop[] = {
198 	{ DLD_PROP_DUPLEX,	sizeof (link_duplex_t),
199 	    "duplex",		"link duplex mode" },
200 
201 	{DLD_PROP_SPEED,	sizeof (uint64_t),
202 	    "speed",		"link speed (bps)" },
203 
204 	{ DLD_PROP_STATUS,	sizeof (link_state_t),
205 	    "state",		"link up/down" },
206 
207 	{ DLD_PROP_AUTONEG,	sizeof (uint8_t),
208 	    "adv_autoneg_cap",	"Advertised auto-negotiation" },
209 
210 	{ DLD_PROP_MTU,		sizeof (uint32_t),
211 	    "mtu",		"current link mtu" },
212 
213 	{ DLD_PROP_FLOWCTRL,	sizeof (link_flowctrl_t),
214 	    "flowctrl",		"flowcontrol" },
215 
216 	{ DLD_PROP_ADV_1000FDX_CAP, sizeof (uint8_t),
217 	    "adv_1000fdx_cap",	"Adv 1000 Mbps fdx" },
218 
219 	{ DLD_PROP_EN_1000FDX_CAP, sizeof (uint8_t),
220 	    "en_1000fdx_cap",	"Enable 1000 Mbps fdx" },
221 
222 	{ DLD_PROP_ADV_1000HDX_CAP, sizeof (uint8_t),
223 	    "adv_1000hdx_cap", "Adv 1000 Mbps hdx" },
224 
225 	{ DLD_PROP_EN_1000HDX_CAP, sizeof (uint8_t),
226 	    "en_1000hdx_cap",	"Enable 1000 Mbps hdx" },
227 
228 	{ DLD_PROP_ADV_100FDX_CAP, sizeof (uint8_t),
229 	    "adv_100fdx_cap",	"Adv 100 Mbps fdx" },
230 
231 	{ DLD_PROP_EN_100FDX_CAP, sizeof (uint8_t),
232 	    "en_100fdx_cap",	"Enable 100 Mbps fdx" },
233 
234 	{ DLD_PROP_ADV_100HDX_CAP, sizeof (uint8_t),
235 	    "adv_100hdx_cap",	"Adv 100 Mbps hdx" },
236 
237 	{ DLD_PROP_EN_100HDX_CAP, sizeof (uint8_t),
238 	    "en_100hdx_cap",	"Enable 100 Mbps hdx" },
239 
240 	{ DLD_PROP_ADV_10FDX_CAP, sizeof (uint8_t),
241 	    "adv_10fdx_cap",	"Adv 10 Mbps fdx" },
242 
243 	{ DLD_PROP_EN_10FDX_CAP, sizeof (uint8_t),
244 	    "en_10fdx_cap",	"Enable 10 Mbps fdx" },
245 
246 	{ DLD_PROP_ADV_10HDX_CAP, sizeof (uint8_t),
247 	    "adv_10hdx_cap",	"Adv 10 Mbps hdx" },
248 
249 	{ DLD_PROP_EN_10HDX_CAP, sizeof (uint8_t),
250 	    "en_10hdx_cap",	"Enable 10 Mbps hdx" },
251 
252 	{ DLD_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 = DLD_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 		if (pdp->pd_defval.vd_name == NULL) {
700 			status = DLADM_STATUS_NOTSUP;
701 			break;
702 		}
703 
704 		if (strlen(pdp->pd_defval.vd_name) == 0) {
705 			status = pdp->pd_get(pdp, linkid, prop_val, val_cntp,
706 			    media, dld_flags);
707 		} else {
708 			(void) strcpy(*prop_val, pdp->pd_defval.vd_name);
709 		}
710 		*val_cntp = 1;
711 		break;
712 
713 	case DLADM_PROP_VAL_MODIFIABLE:
714 		if (pdp->pd_getmod != NULL) {
715 			status = pdp->pd_getmod(pdp, linkid, prop_val,
716 			    val_cntp, media, dld_flags);
717 			break;
718 		}
719 		cnt = pdp->pd_noptval;
720 		if (cnt == 0) {
721 			status = DLADM_STATUS_NOTSUP;
722 		} else if (cnt > *val_cntp) {
723 			status = DLADM_STATUS_TOOSMALL;
724 		} else {
725 			for (i = 0; i < cnt; i++) {
726 				(void) strcpy(prop_val[i],
727 				    pdp->pd_optval[i].vd_name);
728 			}
729 			*val_cntp = cnt;
730 		}
731 		break;
732 	case DLADM_PROP_VAL_PERSISTENT:
733 		if (pdp->pd_flags & PD_TEMPONLY)
734 			return (DLADM_STATUS_TEMPONLY);
735 		status = i_dladm_get_linkprop_db(linkid, prop_name,
736 		    prop_val, val_cntp);
737 		break;
738 	default:
739 		status = DLADM_STATUS_BADARG;
740 		break;
741 	}
742 
743 	return (status);
744 }
745 
746 /*ARGSUSED*/
747 static int
748 i_dladm_init_one_prop(datalink_id_t linkid, const char *prop_name, void *arg)
749 {
750 	char	*buf, **propvals;
751 	uint_t	i, valcnt = DLADM_MAX_PROP_VALCNT;
752 
753 	if ((buf = malloc((sizeof (char *) + DLADM_PROP_VAL_MAX) *
754 	    DLADM_MAX_PROP_VALCNT)) == NULL) {
755 		return (DLADM_WALK_CONTINUE);
756 	}
757 
758 	propvals = (char **)(void *)buf;
759 	for (i = 0; i < valcnt; i++) {
760 		propvals[i] = buf +
761 		    sizeof (char *) * DLADM_MAX_PROP_VALCNT +
762 		    i * DLADM_PROP_VAL_MAX;
763 	}
764 
765 	if (dladm_get_linkprop(linkid, DLADM_PROP_VAL_PERSISTENT, prop_name,
766 	    propvals, &valcnt) != DLADM_STATUS_OK) {
767 		goto done;
768 	}
769 
770 	(void) dladm_set_linkprop(linkid, prop_name, propvals, valcnt,
771 	    DLADM_OPT_ACTIVE);
772 
773 done:
774 	if (buf != NULL)
775 		free(buf);
776 
777 	return (DLADM_WALK_CONTINUE);
778 }
779 
780 /*ARGSUSED*/
781 static int
782 i_dladm_init_linkprop(datalink_id_t linkid, void *arg)
783 {
784 	(void) dladm_init_linkprop(linkid);
785 	return (DLADM_WALK_CONTINUE);
786 }
787 
788 dladm_status_t
789 dladm_init_linkprop(datalink_id_t linkid)
790 {
791 	if (linkid == DATALINK_ALL_LINKID) {
792 		(void) dladm_walk_datalink_id(i_dladm_init_linkprop, NULL,
793 		    DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE,
794 		    DLADM_OPT_PERSIST);
795 	} else {
796 		(void) dladm_walk_linkprop(linkid, NULL, i_dladm_init_one_prop);
797 	}
798 	return (DLADM_STATUS_OK);
799 }
800 
801 /* ARGSUSED */
802 static dladm_status_t
803 do_get_zone(struct prop_desc *pd, datalink_id_t linkid,
804     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
805 {
806 	char		zone_name[ZONENAME_MAX];
807 	zoneid_t	zid;
808 	dladm_status_t	status;
809 
810 	if (flags != 0)
811 		return (DLADM_STATUS_NOTSUP);
812 
813 	status = dladm_getzid(linkid, &zid);
814 	if (status != DLADM_STATUS_OK)
815 		return (status);
816 
817 	*val_cnt = 1;
818 	if (zid != GLOBAL_ZONEID) {
819 		if (getzonenamebyid(zid, zone_name, sizeof (zone_name)) < 0)
820 			return (dladm_errno2status(errno));
821 
822 		(void) strncpy(*prop_val, zone_name, DLADM_PROP_VAL_MAX);
823 	} else {
824 		*prop_val[0] = '\0';
825 	}
826 
827 	return (DLADM_STATUS_OK);
828 }
829 
830 typedef int (*zone_get_devroot_t)(char *, char *, size_t);
831 
832 static int
833 i_dladm_get_zone_dev(char *zone_name, char *dev, size_t devlen)
834 {
835 	char			root[MAXPATHLEN];
836 	zone_get_devroot_t	real_zone_get_devroot;
837 	void			*dlhandle;
838 	void			*sym;
839 	int			ret;
840 
841 	if ((dlhandle = dlopen("libzonecfg.so.1", RTLD_LAZY)) == NULL)
842 		return (-1);
843 
844 	if ((sym = dlsym(dlhandle, "zone_get_devroot")) == NULL) {
845 		(void) dlclose(dlhandle);
846 		return (-1);
847 	}
848 
849 	real_zone_get_devroot = (zone_get_devroot_t)sym;
850 
851 	if ((ret = real_zone_get_devroot(zone_name, root, sizeof (root))) == 0)
852 		(void) snprintf(dev, devlen, "%s%s", root, "/dev");
853 	(void) dlclose(dlhandle);
854 	return (ret);
855 }
856 
857 static dladm_status_t
858 i_dladm_update_deventry(zoneid_t zid, datalink_id_t linkid, boolean_t add)
859 {
860 	char		path[MAXPATHLEN];
861 	char		name[MAXLINKNAMELEN];
862 	di_prof_t	prof = NULL;
863 	char		zone_name[ZONENAME_MAX];
864 	dladm_status_t	status;
865 	int		ret;
866 
867 	if (getzonenamebyid(zid, zone_name, sizeof (zone_name)) < 0)
868 		return (dladm_errno2status(errno));
869 	if (i_dladm_get_zone_dev(zone_name, path, sizeof (path)) != 0)
870 		return (dladm_errno2status(errno));
871 	if (di_prof_init(path, &prof) != 0)
872 		return (dladm_errno2status(errno));
873 
874 	status = dladm_linkid2legacyname(linkid, name, MAXLINKNAMELEN);
875 	if (status != DLADM_STATUS_OK)
876 		goto cleanup;
877 
878 	if (add)
879 		ret = di_prof_add_dev(prof, name);
880 	else
881 		ret = di_prof_add_exclude(prof, name);
882 
883 	if (ret != 0) {
884 		status = dladm_errno2status(errno);
885 		goto cleanup;
886 	}
887 
888 	if (di_prof_commit(prof) != 0)
889 		status = dladm_errno2status(errno);
890 cleanup:
891 	if (prof)
892 		di_prof_fini(prof);
893 
894 	return (status);
895 }
896 
897 /* ARGSUSED */
898 static dladm_status_t
899 do_set_zone(prop_desc_t *pd, datalink_id_t linkid, val_desc_t *vdp,
900     uint_t val_cnt, uint_t flags, datalink_media_t media)
901 {
902 	dladm_status_t	status;
903 	zoneid_t	zid_old, zid_new;
904 	char		link[MAXLINKNAMELEN];
905 
906 	if (val_cnt != 1)
907 		return (DLADM_STATUS_BADVALCNT);
908 
909 	status = dladm_getzid(linkid, &zid_old);
910 	if (status != DLADM_STATUS_OK)
911 		return (status);
912 
913 	/* Do nothing if setting to current value */
914 	zid_new = vdp->vd_val;
915 	if (zid_new == zid_old)
916 		return (DLADM_STATUS_OK);
917 
918 	if ((status = dladm_datalink_id2info(linkid, NULL, NULL, NULL,
919 	    link, MAXLINKNAMELEN)) != DLADM_STATUS_OK) {
920 		return (status);
921 	}
922 
923 	if (zid_new != GLOBAL_ZONEID) {
924 		/*
925 		 * If the new zoneid is the global zone, we could destroy
926 		 * the link (in the case of an implicitly-created VLAN) as a
927 		 * result of the dladm_setzid() operation. In that case,
928 		 * we defer the operation to the end of this function to avoid
929 		 * recreating the VLAN and getting a different linkid during
930 		 * the rollback if other operation fails.
931 		 *
932 		 * Otherwise, dladm_setzid() will hold a reference to the
933 		 * link and prevent a link renaming, so we need to do it
934 		 * before other operations.
935 		 */
936 		status = dladm_setzid(link, zid_new);
937 		if (status != DLADM_STATUS_OK)
938 			return (status);
939 	}
940 
941 	if (zid_old != GLOBAL_ZONEID) {
942 		if (zone_remove_datalink(zid_old, link) != 0 &&
943 		    errno != ENXIO) {
944 			status = dladm_errno2status(errno);
945 			goto rollback1;
946 		}
947 
948 		/*
949 		 * It is okay to fail to update the /dev entry (some
950 		 * vanity-named links do not have a /dev entry).
951 		 */
952 		(void) i_dladm_update_deventry(zid_old, linkid, B_FALSE);
953 	}
954 
955 	if (zid_new != GLOBAL_ZONEID) {
956 		if (zone_add_datalink(zid_new, link) != 0) {
957 			status = dladm_errno2status(errno);
958 			goto rollback2;
959 		}
960 
961 		(void) i_dladm_update_deventry(zid_new, linkid, B_TRUE);
962 	} else {
963 		status = dladm_setzid(link, zid_new);
964 		if (status != DLADM_STATUS_OK)
965 			goto rollback2;
966 	}
967 
968 	return (DLADM_STATUS_OK);
969 
970 rollback2:
971 	if (zid_old != GLOBAL_ZONEID)
972 		(void) i_dladm_update_deventry(zid_old, linkid, B_TRUE);
973 	if (zid_old != GLOBAL_ZONEID)
974 		(void) zone_add_datalink(zid_old, link);
975 rollback1:
976 	if (zid_new != GLOBAL_ZONEID)
977 		(void) dladm_setzid(link, zid_old);
978 	return (status);
979 }
980 
981 /* ARGSUSED */
982 static dladm_status_t
983 do_check_zone(struct prop_desc *pd, datalink_id_t linkid, char **prop_val,
984     uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
985 {
986 	zoneid_t	zid;
987 
988 	if (val_cnt != 1)
989 		return (DLADM_STATUS_BADVALCNT);
990 
991 	if ((zid = getzoneidbyname(*prop_val)) == -1)
992 		return (DLADM_STATUS_BADVAL);
993 
994 	if (zid != GLOBAL_ZONEID) {
995 		ushort_t	flags;
996 
997 		if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags,
998 		    sizeof (flags)) < 0) {
999 			return (dladm_errno2status(errno));
1000 		}
1001 
1002 		if (!(flags & ZF_NET_EXCL)) {
1003 			return (DLADM_STATUS_BADVAL);
1004 		}
1005 	}
1006 
1007 	vdp->vd_val = zid;
1008 	return (DLADM_STATUS_OK);
1009 }
1010 
1011 /* ARGSUSED */
1012 static dladm_status_t
1013 do_get_autopush(struct prop_desc *pd, datalink_id_t linkid,
1014     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1015 {
1016 	dld_ioc_ap_t	dia;
1017 	int		fd, i, len;
1018 
1019 	if (flags & DLD_DEFAULT)
1020 		return (DLADM_STATUS_NOTSUP);
1021 
1022 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
1023 		return (dladm_errno2status(errno));
1024 
1025 	*val_cnt = 1;
1026 	dia.dia_linkid = linkid;
1027 	if (i_dladm_ioctl(fd, DLDIOC_GETAUTOPUSH, &dia, sizeof (dia)) < 0) {
1028 		(*prop_val)[0] = '\0';
1029 		goto done;
1030 	}
1031 
1032 	for (i = 0, len = 0; i < dia.dia_npush; i++) {
1033 		if (i != 0) {
1034 			(void) snprintf(*prop_val + len,
1035 			    DLADM_PROP_VAL_MAX - len, "%c", AP_DELIMITER);
1036 			len += 1;
1037 		}
1038 		(void) snprintf(*prop_val + len, DLADM_PROP_VAL_MAX - len,
1039 		    "%s", dia.dia_aplist[i]);
1040 		len += strlen(dia.dia_aplist[i]);
1041 		if (dia.dia_anchor - 1 == i) {
1042 			(void) snprintf(*prop_val + len,
1043 			    DLADM_PROP_VAL_MAX - len, "%c%s", AP_DELIMITER,
1044 			    AP_ANCHOR);
1045 			len += (strlen(AP_ANCHOR) + 1);
1046 		}
1047 	}
1048 
1049 done:
1050 	(void) close(fd);
1051 	return (DLADM_STATUS_OK);
1052 }
1053 
1054 /* ARGSUSED */
1055 static dladm_status_t
1056 do_set_autopush(prop_desc_t *pd, datalink_id_t linkid, val_desc_t *vdp,
1057     uint_t val_cnt, uint_t flags, datalink_media_t media)
1058 {
1059 	dld_ioc_ap_t		dia;
1060 	struct dlautopush	*dlap = (struct dlautopush *)vdp->vd_val;
1061 	dladm_status_t		status = DLADM_STATUS_OK;
1062 	int			fd, i;
1063 	int			ic_cmd;
1064 
1065 	if (val_cnt != 1)
1066 		return (DLADM_STATUS_BADVALCNT);
1067 
1068 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0)
1069 		return (dladm_errno2status(errno));
1070 
1071 	dia.dia_linkid = linkid;
1072 	if (dlap != NULL) {
1073 		dia.dia_anchor = dlap->dap_anchor;
1074 		dia.dia_npush = dlap->dap_npush;
1075 		for (i = 0; i < dia.dia_npush; i++) {
1076 			(void) strlcpy(dia.dia_aplist[i], dlap->dap_aplist[i],
1077 			    FMNAMESZ+1);
1078 		}
1079 		ic_cmd = DLDIOC_SETAUTOPUSH;
1080 	} else {
1081 		ic_cmd = DLDIOC_CLRAUTOPUSH;
1082 	}
1083 
1084 	if (i_dladm_ioctl(fd, ic_cmd, &dia, sizeof (dia)) < 0)
1085 		status = dladm_errno2status(errno);
1086 
1087 	(void) close(fd);
1088 	return (status);
1089 }
1090 
1091 /*
1092  * Add the specified module to the dlautopush structure; returns a
1093  * DLADM_STATUS_* code.
1094  */
1095 dladm_status_t
1096 i_dladm_add_ap_module(const char *module, struct dlautopush *dlap)
1097 {
1098 	if ((strlen(module) == 0) || (strlen(module) > FMNAMESZ))
1099 		return (DLADM_STATUS_BADVAL);
1100 
1101 	if (strncasecmp(module, AP_ANCHOR, strlen(AP_ANCHOR)) == 0) {
1102 		/*
1103 		 * We don't allow multiple anchors, and the anchor must
1104 		 * be after at least one module.
1105 		 */
1106 		if (dlap->dap_anchor != 0)
1107 			return (DLADM_STATUS_BADVAL);
1108 		if (dlap->dap_npush == 0)
1109 			return (DLADM_STATUS_BADVAL);
1110 
1111 		dlap->dap_anchor = dlap->dap_npush;
1112 		return (DLADM_STATUS_OK);
1113 	}
1114 	if (dlap->dap_npush > MAXAPUSH)
1115 		return (DLADM_STATUS_BADVALCNT);
1116 
1117 	(void) strlcpy(dlap->dap_aplist[dlap->dap_npush++], module,
1118 	    FMNAMESZ + 1);
1119 
1120 	return (DLADM_STATUS_OK);
1121 }
1122 
1123 /*
1124  * Currently, both '.' and ' '(space) can be used as the delimiters between
1125  * autopush modules. The former is used in dladm set-linkprop, and the
1126  * latter is used in the autopush(1M) file.
1127  */
1128 /* ARGSUSED */
1129 static dladm_status_t
1130 do_check_autopush(struct prop_desc *pd, datalink_id_t linkid, char **prop_val,
1131     uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1132 {
1133 	char			*module;
1134 	struct dlautopush	*dlap;
1135 	dladm_status_t		status;
1136 	char			val[DLADM_PROP_VAL_MAX];
1137 	char			delimiters[4];
1138 
1139 	if (val_cnt != 1)
1140 		return (DLADM_STATUS_BADVALCNT);
1141 
1142 	dlap = malloc(sizeof (struct dlautopush));
1143 	if (dlap == NULL)
1144 		return (DLADM_STATUS_NOMEM);
1145 
1146 	(void) memset(dlap, 0, sizeof (struct dlautopush));
1147 	(void) snprintf(delimiters, 4, " %c\n", AP_DELIMITER);
1148 	bcopy(*prop_val, val, DLADM_PROP_VAL_MAX);
1149 	module = strtok(val, delimiters);
1150 	while (module != NULL) {
1151 		status = i_dladm_add_ap_module(module, dlap);
1152 		if (status != DLADM_STATUS_OK)
1153 			return (status);
1154 		module = strtok(NULL, delimiters);
1155 	}
1156 
1157 	vdp->vd_val = (uintptr_t)dlap;
1158 	return (DLADM_STATUS_OK);
1159 }
1160 
1161 /* ARGSUSED */
1162 static dladm_status_t
1163 do_get_rate_common(struct prop_desc *pd, datalink_id_t linkid,
1164     char **prop_val, uint_t *val_cnt, uint_t id)
1165 {
1166 	wl_rates_t	*wrp;
1167 	uint_t		i;
1168 	wldp_t		*gbuf = NULL;
1169 	dladm_status_t	status = DLADM_STATUS_OK;
1170 
1171 	if ((gbuf = malloc(MAX_BUF_LEN)) == NULL) {
1172 		status = DLADM_STATUS_NOMEM;
1173 		goto done;
1174 	}
1175 
1176 	status = i_dladm_wlan_get_ioctl(linkid, gbuf, id);
1177 	if (status != DLADM_STATUS_OK)
1178 		goto done;
1179 
1180 	wrp = (wl_rates_t *)gbuf->wldp_buf;
1181 	if (wrp->wl_rates_num > *val_cnt) {
1182 		status = DLADM_STATUS_TOOSMALL;
1183 		goto done;
1184 	}
1185 
1186 	if (wrp->wl_rates_rates[0] == 0) {
1187 		prop_val[0][0] = '\0';
1188 		*val_cnt = 1;
1189 		goto done;
1190 	}
1191 
1192 	for (i = 0; i < wrp->wl_rates_num; i++) {
1193 		(void) snprintf(prop_val[i], DLADM_STRSIZE, "%.*f",
1194 		    wrp->wl_rates_rates[i] % 2,
1195 		    (float)wrp->wl_rates_rates[i] / 2);
1196 	}
1197 	*val_cnt = wrp->wl_rates_num;
1198 
1199 done:
1200 	free(gbuf);
1201 	return (status);
1202 }
1203 
1204 static dladm_status_t
1205 do_get_rate_prop(struct prop_desc *pd, datalink_id_t linkid,
1206     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1207 {
1208 	if (media != DL_WIFI)
1209 		return (dld_speed_get(pd, linkid, prop_val, val_cnt, flags));
1210 
1211 	return (do_get_rate_common(pd, linkid, prop_val, val_cnt,
1212 	    WL_DESIRED_RATES));
1213 }
1214 
1215 /* ARGSUSED */
1216 static dladm_status_t
1217 do_get_rate_mod(struct prop_desc *pd, datalink_id_t linkid,
1218     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1219 {
1220 	switch (media) {
1221 	case DL_ETHER:
1222 		/*
1223 		 * Speed for ethernet links is unbounded. E.g., 802.11b
1224 		 * links can have a speed of 5.5 Gbps.
1225 		 */
1226 		return (DLADM_STATUS_NOTSUP);
1227 
1228 	case DL_WIFI:
1229 		return (do_get_rate_common(pd, linkid, prop_val, val_cnt,
1230 		    WL_SUPPORTED_RATES));
1231 	default:
1232 		return (DLADM_STATUS_BADARG);
1233 	}
1234 }
1235 
1236 static dladm_status_t
1237 do_set_rate(datalink_id_t linkid, dladm_wlan_rates_t *rates)
1238 {
1239 	int		i;
1240 	uint_t		len;
1241 	wldp_t		*gbuf;
1242 	wl_rates_t	*wrp;
1243 	dladm_status_t	status = DLADM_STATUS_OK;
1244 
1245 	if ((gbuf = malloc(MAX_BUF_LEN)) == NULL)
1246 		return (DLADM_STATUS_NOMEM);
1247 
1248 	(void) memset(gbuf, 0, MAX_BUF_LEN);
1249 
1250 	wrp = (wl_rates_t *)gbuf->wldp_buf;
1251 	for (i = 0; i < rates->wr_cnt; i++)
1252 		wrp->wl_rates_rates[i] = rates->wr_rates[i];
1253 	wrp->wl_rates_num = rates->wr_cnt;
1254 
1255 	len = offsetof(wl_rates_t, wl_rates_rates) +
1256 	    (rates->wr_cnt * sizeof (char)) + WIFI_BUF_OFFSET;
1257 	status = i_dladm_wlan_ioctl(linkid, gbuf, WL_DESIRED_RATES, len,
1258 	    WLAN_SET_PARAM, len);
1259 
1260 	free(gbuf);
1261 	return (status);
1262 }
1263 
1264 /* ARGSUSED */
1265 static dladm_status_t
1266 do_set_rate_prop(prop_desc_t *pd, datalink_id_t linkid,
1267     val_desc_t *vdp, uint_t val_cnt, uint_t flags, datalink_media_t media)
1268 {
1269 	dladm_wlan_rates_t	rates;
1270 	dladm_status_t		status;
1271 
1272 	/*
1273 	 * can currently set rate on WIFI links only.
1274 	 */
1275 	if (media != DL_WIFI)
1276 		return (DLADM_STATUS_PROPRDONLY);
1277 
1278 	if (val_cnt != 1)
1279 		return (DLADM_STATUS_BADVALCNT);
1280 
1281 	rates.wr_cnt = 1;
1282 	rates.wr_rates[0] = vdp[0].vd_val;
1283 
1284 	status = do_set_rate(linkid, &rates);
1285 
1286 done:
1287 	return (status);
1288 }
1289 
1290 /* ARGSUSED */
1291 static dladm_status_t
1292 do_check_rate(struct prop_desc *pd, datalink_id_t linkid, char **prop_val,
1293     uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1294 {
1295 	int		i;
1296 	uint_t		modval_cnt = MAX_SUPPORT_RATES;
1297 	char		*buf, **modval;
1298 	dladm_status_t	status;
1299 
1300 	if (val_cnt != 1)
1301 		return (DLADM_STATUS_BADVALCNT);
1302 
1303 	buf = malloc((sizeof (char *) + DLADM_STRSIZE) *
1304 	    MAX_SUPPORT_RATES);
1305 	if (buf == NULL) {
1306 		status = DLADM_STATUS_NOMEM;
1307 		goto done;
1308 	}
1309 
1310 	modval = (char **)(void *)buf;
1311 	for (i = 0; i < MAX_SUPPORT_RATES; i++) {
1312 		modval[i] = buf + sizeof (char *) * MAX_SUPPORT_RATES +
1313 		    i * DLADM_STRSIZE;
1314 	}
1315 
1316 	status = do_get_rate_mod(NULL, linkid, modval, &modval_cnt, media, 0);
1317 	if (status != DLADM_STATUS_OK)
1318 		goto done;
1319 
1320 	for (i = 0; i < modval_cnt; i++) {
1321 		if (strcasecmp(*prop_val, modval[i]) == 0) {
1322 			vdp->vd_val = (uintptr_t)(uint_t)
1323 			    (atof(*prop_val) * 2);
1324 			status = DLADM_STATUS_OK;
1325 			break;
1326 		}
1327 	}
1328 	if (i == modval_cnt)
1329 		status = DLADM_STATUS_BADVAL;
1330 done:
1331 	free(buf);
1332 	return (status);
1333 }
1334 
1335 static dladm_status_t
1336 do_get_phyconf(datalink_id_t linkid, wldp_t *gbuf)
1337 {
1338 	return (i_dladm_wlan_get_ioctl(linkid, gbuf, WL_PHY_CONFIG));
1339 }
1340 
1341 /* ARGSUSED */
1342 static dladm_status_t
1343 do_get_channel_prop(struct prop_desc *pd, datalink_id_t linkid,
1344     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1345 {
1346 	uint32_t	channel;
1347 	wldp_t		*gbuf;
1348 	dladm_status_t	status = DLADM_STATUS_OK;
1349 
1350 	if ((gbuf = malloc(MAX_BUF_LEN)) == NULL)
1351 		return (DLADM_STATUS_NOMEM);
1352 
1353 	if ((status = do_get_phyconf(linkid, gbuf)) != DLADM_STATUS_OK)
1354 		goto done;
1355 
1356 	if (!i_dladm_wlan_convert_chan((wl_phy_conf_t *)gbuf->wldp_buf,
1357 	    &channel)) {
1358 		status = DLADM_STATUS_NOTFOUND;
1359 		goto done;
1360 	}
1361 
1362 	(void) snprintf(*prop_val, DLADM_STRSIZE, "%u", channel);
1363 	*val_cnt = 1;
1364 
1365 done:
1366 	free(gbuf);
1367 	return (status);
1368 }
1369 
1370 static dladm_status_t
1371 do_get_powermode(datalink_id_t linkid, wldp_t *gbuf)
1372 {
1373 	return (i_dladm_wlan_get_ioctl(linkid, gbuf, WL_POWER_MODE));
1374 }
1375 
1376 /* ARGSUSED */
1377 static dladm_status_t
1378 do_get_powermode_prop(struct prop_desc *pd, datalink_id_t linkid,
1379     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1380 {
1381 	wl_ps_mode_t	*mode;
1382 	const char	*s;
1383 	wldp_t		*gbuf;
1384 	dladm_status_t	status = DLADM_STATUS_OK;
1385 
1386 	if ((gbuf = malloc(MAX_BUF_LEN)) == NULL)
1387 		return (DLADM_STATUS_NOMEM);
1388 
1389 	if ((status = do_get_powermode(linkid, gbuf)) != DLADM_STATUS_OK)
1390 		goto done;
1391 
1392 	mode = (wl_ps_mode_t *)(gbuf->wldp_buf);
1393 	switch (mode->wl_ps_mode) {
1394 	case WL_PM_AM:
1395 		s = "off";
1396 		break;
1397 	case WL_PM_MPS:
1398 		s = "max";
1399 		break;
1400 	case WL_PM_FAST:
1401 		s = "fast";
1402 		break;
1403 	default:
1404 		status = DLADM_STATUS_NOTFOUND;
1405 		goto done;
1406 	}
1407 	(void) snprintf(*prop_val, DLADM_STRSIZE, "%s", s);
1408 	*val_cnt = 1;
1409 
1410 done:
1411 	free(gbuf);
1412 	return (status);
1413 }
1414 
1415 static dladm_status_t
1416 do_set_powermode(datalink_id_t linkid, dladm_wlan_powermode_t *pm)
1417 {
1418 	wl_ps_mode_t    ps_mode;
1419 
1420 	(void) memset(&ps_mode, 0xff, sizeof (ps_mode));
1421 
1422 	switch (*pm) {
1423 	case DLADM_WLAN_PM_OFF:
1424 		ps_mode.wl_ps_mode = WL_PM_AM;
1425 		break;
1426 	case DLADM_WLAN_PM_MAX:
1427 		ps_mode.wl_ps_mode = WL_PM_MPS;
1428 		break;
1429 	case DLADM_WLAN_PM_FAST:
1430 		ps_mode.wl_ps_mode = WL_PM_FAST;
1431 		break;
1432 	default:
1433 		return (DLADM_STATUS_NOTSUP);
1434 	}
1435 	return (i_dladm_wlan_set_ioctl(linkid, WL_POWER_MODE, &ps_mode,
1436 	    sizeof (ps_mode)));
1437 }
1438 
1439 /* ARGSUSED */
1440 static dladm_status_t
1441 do_set_powermode_prop(prop_desc_t *pd, datalink_id_t linkid,
1442     val_desc_t *vdp, uint_t val_cnt, uint_t flags, datalink_media_t media)
1443 {
1444 	dladm_wlan_powermode_t powermode = (dladm_wlan_powermode_t)vdp->vd_val;
1445 	dladm_status_t status;
1446 
1447 	if (val_cnt != 1)
1448 		return (DLADM_STATUS_BADVALCNT);
1449 
1450 	status = do_set_powermode(linkid, &powermode);
1451 
1452 	return (status);
1453 }
1454 
1455 static dladm_status_t
1456 do_get_radio(datalink_id_t linkid, wldp_t *gbuf)
1457 {
1458 	return (i_dladm_wlan_get_ioctl(linkid, gbuf, WL_RADIO));
1459 }
1460 
1461 /* ARGSUSED */
1462 static dladm_status_t
1463 do_get_radio_prop(struct prop_desc *pd, datalink_id_t linkid,
1464     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1465 {
1466 	wl_radio_t	radio;
1467 	const char	*s;
1468 	wldp_t		*gbuf;
1469 	dladm_status_t	status = DLADM_STATUS_OK;
1470 
1471 	if ((gbuf = malloc(MAX_BUF_LEN)) == NULL)
1472 		return (DLADM_STATUS_NOMEM);
1473 
1474 	if ((status = do_get_radio(linkid, gbuf)) != DLADM_STATUS_OK)
1475 		goto done;
1476 
1477 	radio = *(wl_radio_t *)(gbuf->wldp_buf);
1478 	switch (radio) {
1479 	case B_TRUE:
1480 		s = "on";
1481 		break;
1482 	case B_FALSE:
1483 		s = "off";
1484 		break;
1485 	default:
1486 		status = DLADM_STATUS_NOTFOUND;
1487 		goto done;
1488 	}
1489 	(void) snprintf(*prop_val, DLADM_STRSIZE, "%s", s);
1490 	*val_cnt = 1;
1491 
1492 done:
1493 	free(gbuf);
1494 	return (status);
1495 }
1496 
1497 static dladm_status_t
1498 do_set_radio(datalink_id_t linkid, dladm_wlan_radio_t *radio)
1499 {
1500 	wl_radio_t r;
1501 
1502 	switch (*radio) {
1503 	case DLADM_WLAN_RADIO_ON:
1504 		r = B_TRUE;
1505 		break;
1506 	case DLADM_WLAN_RADIO_OFF:
1507 		r = B_FALSE;
1508 		break;
1509 	default:
1510 		return (DLADM_STATUS_NOTSUP);
1511 	}
1512 	return (i_dladm_wlan_set_ioctl(linkid, WL_RADIO, &r, sizeof (r)));
1513 }
1514 
1515 /* ARGSUSED */
1516 static dladm_status_t
1517 do_set_radio_prop(prop_desc_t *pd, datalink_id_t linkid,
1518     val_desc_t *vdp, uint_t val_cnt, uint_t fags, datalink_media_t media)
1519 {
1520 	dladm_wlan_radio_t radio = (dladm_wlan_radio_t)vdp->vd_val;
1521 	dladm_status_t status;
1522 
1523 	if (val_cnt != 1)
1524 		return (DLADM_STATUS_BADVALCNT);
1525 
1526 	status = do_set_radio(linkid, &radio);
1527 
1528 	return (status);
1529 }
1530 
1531 static dladm_status_t
1532 i_dladm_set_linkprop_db(datalink_id_t linkid, const char *prop_name,
1533     char **prop_val, uint_t val_cnt)
1534 {
1535 	char		buf[MAXLINELEN];
1536 	int		i;
1537 	dladm_conf_t	conf;
1538 	dladm_status_t	status;
1539 
1540 	status = dladm_read_conf(linkid, &conf);
1541 	if (status != DLADM_STATUS_OK)
1542 		return (status);
1543 
1544 	/*
1545 	 * reset case.
1546 	 */
1547 	if (val_cnt == 0) {
1548 		status = dladm_unset_conf_field(conf, prop_name);
1549 		if (status == DLADM_STATUS_OK)
1550 			status = dladm_write_conf(conf);
1551 		goto done;
1552 	}
1553 
1554 	buf[0] = '\0';
1555 	for (i = 0; i < val_cnt; i++) {
1556 		(void) strlcat(buf, prop_val[i], MAXLINELEN);
1557 		if (i != val_cnt - 1)
1558 			(void) strlcat(buf, ",", MAXLINELEN);
1559 	}
1560 
1561 	status = dladm_set_conf_field(conf, prop_name, DLADM_TYPE_STR, buf);
1562 	if (status == DLADM_STATUS_OK)
1563 		status = dladm_write_conf(conf);
1564 
1565 done:
1566 	dladm_destroy_conf(conf);
1567 	return (status);
1568 }
1569 
1570 static dladm_status_t
1571 i_dladm_get_linkprop_db(datalink_id_t linkid, const char *prop_name,
1572     char **prop_val, uint_t *val_cntp)
1573 {
1574 	char		buf[MAXLINELEN], *str;
1575 	uint_t		cnt = 0;
1576 	dladm_conf_t	conf;
1577 	dladm_status_t	status;
1578 
1579 	status = dladm_read_conf(linkid, &conf);
1580 	if (status != DLADM_STATUS_OK)
1581 		return (status);
1582 
1583 	status = dladm_get_conf_field(conf, prop_name, buf, MAXLINELEN);
1584 	if (status != DLADM_STATUS_OK)
1585 		goto done;
1586 
1587 	str = strtok(buf, ",");
1588 	while (str != NULL) {
1589 		if (cnt == *val_cntp) {
1590 			status = DLADM_STATUS_TOOSMALL;
1591 			goto done;
1592 		}
1593 		(void) strlcpy(prop_val[cnt++], str, DLADM_PROP_VAL_MAX);
1594 		str = strtok(NULL, ",");
1595 	}
1596 
1597 	*val_cntp = cnt;
1598 
1599 done:
1600 	dladm_destroy_conf(conf);
1601 	return (status);
1602 }
1603 
1604 static dld_public_prop_t *
1605 dladm_name2prop(const char *prop_name)
1606 {
1607 	dld_public_prop_t *p;
1608 
1609 	for (p = dld_prop; p->pp_id != DLD_PROP_PRIVATE; p++) {
1610 		if (strcmp(p->pp_name, prop_name) == 0)
1611 			break;
1612 	}
1613 	return (p);
1614 }
1615 
1616 
1617 static dld_ioc_prop_t *
1618 dld_buf_alloc(size_t valsize, datalink_id_t linkid, const char *prop_name,
1619     uint_t flags, dladm_status_t *status)
1620 {
1621 	int dsize;
1622 	dld_ioc_prop_t *dip;
1623 	dld_public_prop_t *p;
1624 
1625 	*status = DLADM_STATUS_OK;
1626 	p = dladm_name2prop(prop_name);
1627 	if (p->pp_id != DLD_PROP_PRIVATE)
1628 		valsize = p->pp_valsize;
1629 
1630 	dsize = DLD_PROPBUF_SIZE(valsize);
1631 	dip = malloc(dsize);
1632 	if (dip == NULL) {
1633 		*status = DLADM_STATUS_NOMEM;
1634 		return (NULL);
1635 	}
1636 	bzero(dip, dsize);
1637 	dip->pr_valsize = valsize;
1638 	(void) strlcpy(dip->pr_name, prop_name, sizeof (dip->pr_name));
1639 	dip->pr_version = DLD_PROP_VERSION;
1640 	dip->pr_linkid = linkid;
1641 	dip->pr_num = p->pp_id;
1642 	dip->pr_flags = flags;
1643 	return (dip);
1644 }
1645 
1646 /* ARGSUSED */
1647 static dladm_status_t
1648 dld_set_public_prop(prop_desc_t *pd, datalink_id_t linkid,
1649     val_desc_t *vdp, uint_t val_cnt, uint_t flags, datalink_media_t media)
1650 {
1651 	dld_ioc_prop_t	*dip;
1652 	int		fd, dsize;
1653 	dladm_status_t	status = DLADM_STATUS_OK;
1654 	uint8_t		u8;
1655 	uint16_t	u16;
1656 	uint32_t	u32;
1657 	void		*val;
1658 
1659 	dip = dld_buf_alloc(0, linkid, pd->pd_name, 0, &status);
1660 	if (dip == NULL)
1661 		return (status);
1662 
1663 	if (pd->pd_flags & PD_CHECK_ALLOC)
1664 		val = (void *)vdp->vd_val;
1665 	else {
1666 		/*
1667 		 * Currently all 1/2/4-byte size properties are byte/word/int.
1668 		 * No need (yet) to distinguish these from arrays of same size.
1669 		 */
1670 		switch (dip->pr_valsize) {
1671 		case 1:
1672 			u8 = vdp->vd_val;
1673 			val = &u8;
1674 			break;
1675 		case 2:
1676 			u16 = vdp->vd_val;
1677 			val = &u16;
1678 			break;
1679 		case 4:
1680 			u32 = vdp->vd_val;
1681 			val = &u32;
1682 			break;
1683 		default:
1684 			val = &vdp->vd_val;
1685 			break;
1686 		}
1687 	}
1688 
1689 	(void) memcpy(dip->pr_val, val, dip->pr_valsize);
1690 	dsize = DLD_PROPBUF_SIZE(dip->pr_valsize);
1691 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0) {
1692 		status = dladm_errno2status(errno);
1693 		goto done;
1694 	}
1695 	if (i_dladm_ioctl(fd, DLDIOCSETPROP, dip, dsize) < 0)
1696 		status = dladm_errno2status(errno);
1697 
1698 	(void) close(fd);
1699 done:
1700 	free(dip);
1701 	return (status);
1702 }
1703 
1704 static dld_ioc_prop_t *
1705 dld_get_public_prop(datalink_id_t linkid, char *prop_name, uint_t flags,
1706     dladm_status_t *status)
1707 {
1708 	int fd, dsize;
1709 	dld_ioc_prop_t *dip = NULL;
1710 
1711 	*status = DLADM_STATUS_OK;
1712 
1713 	dip = dld_buf_alloc(0, linkid, prop_name, flags, status);
1714 	if (dip == NULL)
1715 		return (NULL);
1716 
1717 	dsize = DLD_PROPBUF_SIZE(dip->pr_valsize);
1718 
1719 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0) {
1720 		*status = dladm_errno2status(errno);
1721 		goto done;
1722 	}
1723 	if (i_dladm_ioctl(fd, DLDIOCGETPROP, dip, dsize) < 0) {
1724 		*status = dladm_errno2status(errno);
1725 	}
1726 
1727 	(void) close(fd);
1728 done:
1729 	if (*status != DLADM_STATUS_OK) {
1730 		free(dip);
1731 		return (NULL);
1732 	}
1733 	return (dip);
1734 }
1735 
1736 /* ARGSUSED */
1737 static dladm_status_t
1738 dld_defmtu_check(struct prop_desc *pd, datalink_id_t linkid, char **prop_val,
1739     uint_t val_cnt, val_desc_t *v, datalink_media_t media)
1740 {
1741 	if (val_cnt != 1)
1742 		return (DLADM_STATUS_BADVAL);
1743 	v->vd_val = atoi(prop_val[0]);
1744 	return (DLADM_STATUS_OK);
1745 }
1746 
1747 /* ARGSUSED */
1748 static dladm_status_t
1749 dld_duplex_get(struct prop_desc *pd, datalink_id_t linkid,
1750     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1751 {
1752 	link_duplex_t   link_duplex;
1753 	dladm_status_t  status;
1754 
1755 	if (flags & DLD_DEFAULT)
1756 		return (DLADM_STATUS_NOTSUP);
1757 
1758 	if ((status = dladm_get_single_mac_stat(linkid, "link_duplex",
1759 	    KSTAT_DATA_UINT32, &link_duplex)) != 0)
1760 		return (status);
1761 
1762 	switch (link_duplex) {
1763 	case LINK_DUPLEX_FULL:
1764 		(void) strcpy(*prop_val, "full");
1765 		break;
1766 	case LINK_DUPLEX_HALF:
1767 		(void) strcpy(*prop_val, "half");
1768 		break;
1769 	default:
1770 		(void) strcpy(*prop_val, "unknown");
1771 		break;
1772 	}
1773 	*val_cnt = 1;
1774 	return (DLADM_STATUS_OK);
1775 }
1776 
1777 /* ARGSUSED */
1778 static dladm_status_t
1779 dld_speed_get(struct prop_desc *pd, datalink_id_t linkid,
1780     char **prop_val, uint_t *val_cnt, uint_t flags)
1781 {
1782 	uint64_t	ifspeed = 0;
1783 	dladm_status_t status;
1784 
1785 	if (flags & DLD_DEFAULT)
1786 		return (DLADM_STATUS_NOTSUP);
1787 
1788 	if ((status = dladm_get_single_mac_stat(linkid, "ifspeed",
1789 	    KSTAT_DATA_UINT64, &ifspeed)) != 0)
1790 		return (status);
1791 
1792 	if ((ifspeed % 1000000) != 0) {
1793 		(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX,
1794 		    "%llf", ifspeed / (float)1000000); /* Mbps */
1795 	} else {
1796 		(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX,
1797 		    "%llu", ifspeed / 1000000); /* Mbps */
1798 	}
1799 	*val_cnt = 1;
1800 	return (DLADM_STATUS_OK);
1801 }
1802 
1803 /* ARGSUSED */
1804 static dladm_status_t
1805 dld_status_get(struct prop_desc *pd, datalink_id_t linkid,
1806     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1807 {
1808 	link_state_t	link_state;
1809 	dladm_status_t	status;
1810 	uchar_t 	*cp;
1811 	dld_ioc_prop_t  *dip;
1812 
1813 	if (flags & DLD_DEFAULT)
1814 		return (DLADM_STATUS_NOTSUP);
1815 	dip = dld_get_public_prop(linkid, pd->pd_name, flags, &status);
1816 	if (status != DLADM_STATUS_OK)
1817 		return (status);
1818 	cp = (uchar_t *)dip->pr_val;
1819 	(void) memcpy(&link_state, cp, sizeof (link_state));
1820 
1821 	switch (link_state) {
1822 	case LINK_STATE_UP:
1823 		(void) strcpy(*prop_val, "up");
1824 		break;
1825 	case LINK_STATE_DOWN:
1826 		(void) strcpy(*prop_val, "down");
1827 		break;
1828 	default:
1829 		(void) strcpy(*prop_val, "unknown");
1830 		break;
1831 	}
1832 	*val_cnt = 1;
1833 	free(dip);
1834 	return (DLADM_STATUS_OK);
1835 }
1836 
1837 /* ARGSUSED */
1838 static dladm_status_t
1839 dld_binary_get(struct prop_desc *pd, datalink_id_t linkid,
1840     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1841 {
1842 	dld_ioc_prop_t *dip;
1843 	dladm_status_t status;
1844 
1845 	dip = dld_get_public_prop(linkid, pd->pd_name, flags, &status);
1846 	if (dip == NULL)
1847 		return (status);
1848 	(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX, "%x", dip->pr_val[0]);
1849 	free(dip);
1850 	*val_cnt = 1;
1851 	return (DLADM_STATUS_OK);
1852 }
1853 
1854 /* ARGSUSED */
1855 static dladm_status_t
1856 dld_uint32_get(struct prop_desc *pd, datalink_id_t linkid,
1857     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1858 {
1859 	dld_ioc_prop_t *dip;
1860 	uint32_t v  = 0;
1861 	uchar_t *cp;
1862 	dladm_status_t status;
1863 
1864 	dip = dld_get_public_prop(linkid, pd->pd_name, flags, &status);
1865 	if (dip == NULL)
1866 		return (status);
1867 	cp = (uchar_t *)dip->pr_val;
1868 	(void) memcpy(&v, cp, sizeof (v));
1869 	(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX, "%ld", v);
1870 	free(dip);
1871 	*val_cnt = 1;
1872 	return (DLADM_STATUS_OK);
1873 }
1874 
1875 /* ARGSUSED */
1876 static dladm_status_t
1877 dld_flowctl_get(struct prop_desc *pd, datalink_id_t linkid,
1878     char **prop_val, uint_t *val_cnt, datalink_media_t media, uint_t flags)
1879 {
1880 	dld_ioc_prop_t *dip;
1881 	link_flowctrl_t v;
1882 	dladm_status_t status;
1883 	uchar_t *cp;
1884 
1885 	dip = dld_get_public_prop(linkid, pd->pd_name, flags, &status);
1886 	if (dip == NULL)
1887 		return (status);
1888 	cp = (uchar_t *)dip->pr_val;
1889 	(void) memcpy(&v, cp, sizeof (v));
1890 	switch (v) {
1891 	case LINK_FLOWCTRL_NONE:
1892 		(void) sprintf(*prop_val, "no");
1893 		break;
1894 	case LINK_FLOWCTRL_RX:
1895 		(void) sprintf(*prop_val, "rx");
1896 		break;
1897 	case LINK_FLOWCTRL_TX:
1898 		(void) sprintf(*prop_val, "tx");
1899 		break;
1900 	case LINK_FLOWCTRL_BI:
1901 		(void) sprintf(*prop_val, "bi");
1902 		break;
1903 	}
1904 	free(dip);
1905 	*val_cnt = 1;
1906 	return (DLADM_STATUS_OK);
1907 }
1908 
1909 
1910 /* ARGSUSED */
1911 static dladm_status_t
1912 dld_set_prop(datalink_id_t linkid, const char *prop_name,
1913     char **prop_val, uint_t val_cnt, uint_t flags)
1914 {
1915 	int		fd, i, slen;
1916 	int 		bufsize = 0, dsize;
1917 	dld_ioc_prop_t *dip = NULL;
1918 	uchar_t 	*dp;
1919 	dld_public_prop_t *p;
1920 	dladm_status_t	status = DLADM_STATUS_OK;
1921 
1922 	if ((prop_name == NULL && prop_val != NULL) ||
1923 	    (prop_val != NULL && val_cnt == 0))
1924 		return (DLADM_STATUS_BADARG);
1925 	p = dladm_name2prop(prop_name);
1926 	if (p->pp_id != DLD_PROP_PRIVATE)
1927 		return (DLADM_STATUS_BADARG);
1928 
1929 	/*
1930 	 * private properties: all parsing is done in the kernel.
1931 	 * allocate a enough space for each property + its separator (',').
1932 	 */
1933 	for (i = 0; i < val_cnt; i++) {
1934 		bufsize += strlen(prop_val[i]) + 1;
1935 	}
1936 
1937 	if (prop_val == NULL) {
1938 		/*
1939 		 * getting default value. so use more buffer space.
1940 		 */
1941 		bufsize += 1024;
1942 	}
1943 
1944 	dip = dld_buf_alloc(bufsize + 1, linkid, prop_name,
1945 	    (prop_val != NULL ? 0 : DLD_DEFAULT), &status);
1946 	if (dip == NULL)
1947 		return (status);
1948 
1949 	dp = (uchar_t *)dip->pr_val;
1950 	dsize = sizeof (dld_ioc_prop_t) + bufsize;
1951 	slen = 0;
1952 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0) {
1953 		status = dladm_errno2status(errno);
1954 		goto done;
1955 	}
1956 	if (prop_val == NULL) {
1957 		if (i_dladm_ioctl(fd, DLDIOCGETPROP, dip, dsize) < 0) {
1958 			status = dladm_errno2status(errno);
1959 			goto done;
1960 		}
1961 	} else {
1962 		for (i = 0; i < val_cnt; i++) {
1963 			int plen = 0;
1964 
1965 			plen = strlen(prop_val[i]);
1966 			bcopy(prop_val[i], dp, plen);
1967 			slen += plen;
1968 			/*
1969 			 * add a "," separator and update dp.
1970 			 */
1971 			if (i != (val_cnt -1))
1972 				dp[slen++] = ',';
1973 			dp += (plen + 1);
1974 		}
1975 	}
1976 	if (i_dladm_ioctl(fd, DLDIOCSETPROP, dip, dsize) < 0) {
1977 		status = dladm_errno2status(errno);
1978 	}
1979 
1980 done:
1981 	if (fd > 0)
1982 		(void) close(fd);
1983 	free(dip);
1984 	return (status);
1985 }
1986 
1987 static dladm_status_t
1988 dld_get_prop(datalink_id_t linkid, const char *prop_name,
1989     char **prop_val, uint_t *val_cnt, dladm_prop_type_t type, uint_t dld_flags)
1990 {
1991 	int		fd;
1992 	dladm_status_t  status = DLADM_STATUS_OK;
1993 	uint_t 		dsize;
1994 	dld_ioc_prop_t *dip = NULL;
1995 	dld_public_prop_t *p;
1996 	char tmp = '\0';
1997 
1998 	if ((prop_name == NULL && prop_val != NULL) ||
1999 	    (prop_val != NULL && val_cnt == 0))
2000 		return (DLADM_STATUS_BADARG);
2001 
2002 	p = dladm_name2prop(prop_name);
2003 	if (p->pp_id != DLD_PROP_PRIVATE)
2004 		return (DLADM_STATUS_BADARG);
2005 
2006 	if (type == DLADM_PROP_VAL_MODIFIABLE) {
2007 		*prop_val = &tmp;
2008 		*val_cnt = 1;
2009 		return (DLADM_STATUS_OK);
2010 	}
2011 
2012 	/*
2013 	 * private properties: all parsing is done in the kernel.
2014 	 */
2015 	dip = dld_buf_alloc(1024, linkid, prop_name, dld_flags, &status);
2016 	if (dip == NULL)
2017 		return (status);
2018 	dsize = DLD_PROPBUF_SIZE(dip->pr_valsize);
2019 
2020 	if ((fd = open(DLD_CONTROL_DEV, O_RDWR)) < 0) {
2021 		free(dip);
2022 		return (DLADM_STATUS_BADARG);
2023 	}
2024 
2025 	if ((status = i_dladm_ioctl(fd, DLDIOCGETPROP, dip, dsize)) < 0) {
2026 		status = dladm_errno2status(errno);
2027 	} else {
2028 		(void) strncpy(*prop_val, dip->pr_val, DLADM_PROP_VAL_MAX);
2029 		*val_cnt = 1;
2030 	}
2031 
2032 	(void) close(fd);
2033 	free(dip);
2034 	return (status);
2035 }
2036 
2037 
2038 static dladm_status_t
2039 i_dladm_getset_defval(prop_desc_t *pdp, datalink_id_t linkid,
2040     datalink_media_t media, uint_t flags)
2041 {
2042 	dladm_status_t status;
2043 	char **prop_vals = NULL, *buf;
2044 	size_t bufsize;
2045 	uint_t cnt;
2046 	int i;
2047 
2048 	/*
2049 	 * Allocate buffer needed for prop_vals array. We can have at most
2050 	 * DLADM_MAX_PROP_VALCNT char *prop_vals[] entries, where
2051 	 * each entry has max size DLADM_PROP_VAL_MAX
2052 	 */
2053 	bufsize =
2054 	    (sizeof (char *) + DLADM_PROP_VAL_MAX) * DLADM_MAX_PROP_VALCNT;
2055 	buf = malloc(bufsize);
2056 	prop_vals = (char **)(void *)buf;
2057 	for (i = 0; i < DLADM_MAX_PROP_VALCNT; i++) {
2058 		prop_vals[i] = buf +
2059 		    sizeof (char *) * DLADM_MAX_PROP_VALCNT +
2060 		    i * DLADM_PROP_VAL_MAX;
2061 	}
2062 	status = pdp->pd_get(pdp, linkid, prop_vals, &cnt, media, DLD_DEFAULT);
2063 	if (status == DLADM_STATUS_OK) {
2064 		status = i_dladm_set_single_prop(linkid, pdp->pd_class,
2065 		    media, pdp, prop_vals, cnt, flags);
2066 	}
2067 	free(buf);
2068 	return (status);
2069 }
2070