xref: /titanic_44/usr/src/lib/libdladm/common/linkprop.c (revision 9a4611f412a6b1f7a0bc7d53d2bb046a95daa4bc)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <stdlib.h>
27 #include <strings.h>
28 #include <errno.h>
29 #include <ctype.h>
30 #include <stddef.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/dld.h>
34 #include <sys/zone.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <libdevinfo.h>
38 #include <zone.h>
39 #include <libdllink.h>
40 #include <libdladm_impl.h>
41 #include <libdlwlan_impl.h>
42 #include <libdlwlan.h>
43 #include <libdlvlan.h>
44 #include <libdlvnic.h>
45 #include <libintl.h>
46 #include <dlfcn.h>
47 #include <link.h>
48 #include <inet/wifi_ioctl.h>
49 #include <libdladm.h>
50 #include <libdlstat.h>
51 #include <sys/param.h>
52 #include <sys/debug.h>
53 #include <sys/dld.h>
54 #include <sys/mac_flow.h>
55 #include <inttypes.h>
56 #include <sys/ethernet.h>
57 #include <net/wpa.h>
58 #include <sys/sysmacros.h>
59 
60 /*
61  * The linkprop get() callback.
62  * - pd: 	pointer to the prop_desc_t
63  * - propstrp:	a property string array to keep the returned property.
64  *		Caller allocated.
65  * - cntp:	number of returned properties.
66  *		Caller also uses it to indicate how many it expects.
67  */
68 struct prop_desc;
69 typedef struct prop_desc prop_desc_t;
70 
71 typedef dladm_status_t	pd_getf_t(dladm_handle_t, prop_desc_t *pdp,
72 			datalink_id_t, char **propstp, uint_t *cntp,
73 			datalink_media_t, uint_t, uint_t *);
74 
75 /*
76  * The linkprop set() callback.
77  * - propval:	a val_desc_t array which keeps the property values to be set.
78  * - cnt:	number of properties to be set.
79  * - flags: 	additional flags passed down the system call.
80  *
81  * pd_set takes val_desc_t given by pd_check(), translates it into
82  * a format suitable for kernel consumption. This may require allocation
83  * of ioctl buffers etc. pd_set() may call another common routine (used
84  * by all other pd_sets) which invokes the ioctl.
85  */
86 typedef dladm_status_t	pd_setf_t(dladm_handle_t, prop_desc_t *, datalink_id_t,
87 			    val_desc_t *propval, uint_t cnt, uint_t flags,
88 			    datalink_media_t);
89 
90 /*
91  * The linkprop check() callback.
92  * - propstrp:	property string array which keeps the property to be checked.
93  * - cnt:	number of properties.
94  * - propval:	return value; the property values of the given property strings.
95  *
96  * pd_check checks that the input values are valid. It does so by
97  * iteraring through the pd_modval list for the property. If
98  * the modifiable values cannot be expressed as a list, a pd_check
99  * specific to this property can be used. If the input values are
100  * verified to be valid, pd_check allocates a val_desc_t and fills it
101  * with either a val_desc_t found on the pd_modval list or something
102  * generated on the fly.
103  */
104 typedef dladm_status_t	pd_checkf_t(dladm_handle_t, prop_desc_t *pdp,
105 			    datalink_id_t, char **propstrp, uint_t cnt,
106 			    val_desc_t *propval, datalink_media_t);
107 
108 typedef struct link_attr_s {
109 	mac_prop_id_t	pp_id;
110 	size_t		pp_valsize;
111 	char		*pp_name;
112 } link_attr_t;
113 
114 static dld_ioc_macprop_t *i_dladm_buf_alloc_by_name(size_t, datalink_id_t,
115 			    const char *, uint_t, dladm_status_t *);
116 static dld_ioc_macprop_t *i_dladm_buf_alloc_by_id(size_t, datalink_id_t,
117 			    mac_prop_id_t, uint_t, dladm_status_t *);
118 static dld_ioc_macprop_t *i_dladm_get_public_prop(dladm_handle_t, datalink_id_t,
119 			    char *, uint_t, dladm_status_t *, uint_t *);
120 
121 static dladm_status_t i_dladm_set_private_prop(dladm_handle_t, datalink_id_t,
122 			    const char *, char **, uint_t, uint_t);
123 static dladm_status_t i_dladm_get_priv_prop(dladm_handle_t, datalink_id_t,
124 			    const char *, char **, uint_t *, dladm_prop_type_t,
125 			    uint_t);
126 static link_attr_t *dladm_name2prop(const char *);
127 static link_attr_t *dladm_id2prop(mac_prop_id_t);
128 
129 static pd_getf_t	do_get_zone, do_get_autopush, do_get_rate_mod,
130 			do_get_rate_prop, do_get_channel_prop,
131 			do_get_powermode_prop, do_get_radio_prop,
132 			i_dladm_duplex_get, i_dladm_status_get,
133 			i_dladm_binary_get, i_dladm_uint32_get,
134 			i_dladm_flowctl_get, i_dladm_maxbw_get,
135 			i_dladm_cpus_get, i_dladm_priority_get,
136 			i_dladm_tagmode_get, i_dladm_range_get;
137 
138 static pd_setf_t	do_set_zone, do_set_rate_prop,
139 			do_set_powermode_prop, do_set_radio_prop,
140 			i_dladm_set_public_prop, do_set_res, do_set_cpus;
141 
142 static pd_checkf_t	do_check_zone, do_check_autopush, do_check_rate,
143 			i_dladm_defmtu_check, do_check_maxbw, do_check_cpus,
144 			do_check_priority;
145 
146 static dladm_status_t	i_dladm_speed_get(dladm_handle_t, prop_desc_t *,
147 			    datalink_id_t, char **, uint_t *, uint_t, uint_t *);
148 static dladm_status_t	i_dladm_macprop(dladm_handle_t, void *, boolean_t);
149 static const char	*dladm_perm2str(uint_t, char *);
150 
151 struct prop_desc {
152 	/*
153 	 * link property name
154 	 */
155 	char			*pd_name;
156 
157 	/*
158 	 * default property value, can be set to { "", NULL }
159 	 */
160 	val_desc_t		pd_defval;
161 
162 	/*
163 	 * list of optional property values, can be NULL.
164 	 *
165 	 * This is set to non-NULL if there is a list of possible property
166 	 * values.  pd_optval would point to the array of possible values.
167 	 */
168 	val_desc_t		*pd_optval;
169 
170 	/*
171 	 * count of the above optional property values. 0 if pd_optval is NULL.
172 	 */
173 	uint_t			pd_noptval;
174 
175 	/*
176 	 * callback to set link property;
177 	 * set to NULL if this property is read-only
178 	 */
179 	pd_setf_t		*pd_set;
180 
181 	/*
182 	 * callback to get modifiable link property
183 	 */
184 	pd_getf_t		*pd_getmod;
185 
186 	/*
187 	 * callback to get current link property
188 	 */
189 	pd_getf_t		*pd_get;
190 
191 	/*
192 	 * callback to validate link property value, set to NULL if pd_optval
193 	 * is not NULL. In that case, validate the value by comparing it with
194 	 * the pd_optval. Return a val_desc_t array pointer if the value is
195 	 * valid.
196 	 */
197 	pd_checkf_t		*pd_check;
198 
199 	uint_t			pd_flags;
200 #define	PD_TEMPONLY	0x1	/* property is temporary only */
201 #define	PD_CHECK_ALLOC	0x2	/* alloc vd_val as part of pd_check */
202 	/*
203 	 * indicate link classes this property applies to.
204 	 */
205 	datalink_class_t	pd_class;
206 
207 	/*
208 	 * indicate link media type this property applies to.
209 	 */
210 	datalink_media_t	pd_dmedia;
211 };
212 
213 #define	MAC_PROP_BUFSIZE(v)	sizeof (dld_ioc_macprop_t) + (v) - 1
214 
215 /*
216  * Supported link properties enumerated in the prop_table[] array are
217  * computed using the callback functions in that array. To compute the
218  * property value, multiple distinct system calls may be needed (e.g.,
219  * for wifi speed, we need to issue system calls to get desired/supported
220  * rates). The link_attr[] table enumerates the interfaces to the kernel,
221  * and the type/size of the data passed in the user-kernel interface.
222  */
223 static link_attr_t link_attr[] = {
224 	{ MAC_PROP_DUPLEX,	sizeof (link_duplex_t),	"duplex"},
225 
226 	{ MAC_PROP_SPEED,	sizeof (uint64_t),	"speed"},
227 
228 	{ MAC_PROP_STATUS,	sizeof (link_state_t),	"state"},
229 
230 	{ MAC_PROP_AUTONEG,	sizeof (uint8_t),	"adv_autoneg_cap"},
231 
232 	{ MAC_PROP_MTU,		sizeof (uint32_t),	"mtu"},
233 
234 	{ MAC_PROP_FLOWCTRL,	sizeof (link_flowctrl_t), "flowctrl"},
235 
236 	{ MAC_PROP_ZONE,	sizeof (dld_ioc_zid_t),	"zone"},
237 
238 	{ MAC_PROP_AUTOPUSH,	sizeof (struct dlautopush), "autopush"},
239 
240 	{ MAC_PROP_ADV_10GFDX_CAP, sizeof (uint8_t),	"adv_10gfdx_cap"},
241 
242 	{ MAC_PROP_EN_10GFDX_CAP, sizeof (uint8_t),	"en_10gfdx_cap"},
243 
244 	{ MAC_PROP_ADV_1000FDX_CAP, sizeof (uint8_t),	"adv_1000fdx_cap"},
245 
246 	{ MAC_PROP_EN_1000FDX_CAP, sizeof (uint8_t),	"en_1000fdx_cap"},
247 
248 	{ MAC_PROP_ADV_1000HDX_CAP, sizeof (uint8_t),	"adv_1000hdx_cap"},
249 
250 	{ MAC_PROP_EN_1000HDX_CAP, sizeof (uint8_t),	"en_1000hdx_cap"},
251 
252 	{ MAC_PROP_ADV_100FDX_CAP, sizeof (uint8_t),	"adv_100fdx_cap"},
253 
254 	{ MAC_PROP_EN_100FDX_CAP, sizeof (uint8_t),	"en_100fdx_cap"},
255 
256 	{ MAC_PROP_ADV_100HDX_CAP, sizeof (uint8_t),	"adv_100hdx_cap"},
257 
258 	{ MAC_PROP_EN_100HDX_CAP, sizeof (uint8_t),	"en_100hdx_cap"},
259 
260 	{ MAC_PROP_ADV_10FDX_CAP, sizeof (uint8_t),	"adv_10fdx_cap"},
261 
262 	{ MAC_PROP_EN_10FDX_CAP, sizeof (uint8_t),	"en_10fdx_cap"},
263 
264 	{ MAC_PROP_ADV_10HDX_CAP, sizeof (uint8_t),	"adv_10hdx_cap"},
265 
266 	{ MAC_PROP_EN_10HDX_CAP, sizeof (uint8_t),	"en_10hdx_cap"},
267 
268 	{ MAC_PROP_WL_ESSID,	sizeof (wl_linkstatus_t), "essid"},
269 
270 	{ MAC_PROP_WL_BSSID,	sizeof (wl_bssid_t),	"bssid"},
271 
272 	{ MAC_PROP_WL_BSSTYPE,	sizeof (wl_bss_type_t),	"bsstype"},
273 
274 	{ MAC_PROP_WL_LINKSTATUS, sizeof (wl_linkstatus_t), "wl_linkstatus"},
275 
276 	/* wl_rates_t has variable length */
277 	{ MAC_PROP_WL_DESIRED_RATES, sizeof (wl_rates_t), "desired_rates"},
278 
279 	/* wl_rates_t has variable length */
280 	{ MAC_PROP_WL_SUPPORTED_RATES, sizeof (wl_rates_t), "supported_rates"},
281 
282 	{ MAC_PROP_WL_AUTH_MODE, sizeof (wl_authmode_t), "authmode"},
283 
284 	{ MAC_PROP_WL_ENCRYPTION, sizeof (wl_encryption_t), "encryption"},
285 
286 	{ MAC_PROP_WL_RSSI,	sizeof (wl_rssi_t),	"signal"},
287 
288 	{ MAC_PROP_WL_PHY_CONFIG, sizeof (wl_phy_conf_t), "phy_conf"},
289 
290 	{ MAC_PROP_WL_CAPABILITY, sizeof (wl_capability_t), "capability"},
291 
292 	{ MAC_PROP_WL_WPA,	sizeof (wl_wpa_t),	"wpa"},
293 
294 	/*  wl_wpa_ess_t has variable length */
295 	{ MAC_PROP_WL_SCANRESULTS, sizeof (wl_wpa_ess_t), "scan_results"},
296 
297 	{ MAC_PROP_WL_POWER_MODE, sizeof (wl_ps_mode_t), "powermode"},
298 
299 	{ MAC_PROP_WL_RADIO,	sizeof (dladm_wlan_radio_t), "wl_radio"},
300 
301 	{ MAC_PROP_WL_ESS_LIST, sizeof (wl_ess_list_t),	"wl_ess_list"},
302 
303 	{ MAC_PROP_WL_KEY_TAB,	sizeof (wl_wep_key_tab_t), "wl_wep_key"},
304 
305 	{ MAC_PROP_WL_CREATE_IBSS, sizeof (wl_create_ibss_t), "createibss"},
306 
307 	/* wl_wpa_ie_t has variable length */
308 	{ MAC_PROP_WL_SETOPTIE,	sizeof (wl_wpa_ie_t),	"set_ie"},
309 
310 	{ MAC_PROP_WL_DELKEY,	sizeof (wl_del_key_t),	"wpa_del_key"},
311 
312 	{ MAC_PROP_WL_KEY,	sizeof (wl_key_t),	"wl_key"},
313 
314 	{ MAC_PROP_WL_MLME,	sizeof (wl_mlme_t),	"mlme"},
315 
316 	{ MAC_PROP_MAXBW,	sizeof (mac_resource_props_t),	"maxbw"},
317 
318 	{ MAC_PROP_PRIO,	sizeof (mac_resource_props_t),	"priority"},
319 
320 	{ MAC_PROP_BIND_CPU,	sizeof (mac_resource_props_t),	"cpus"},
321 
322 	{ MAC_PROP_TAGMODE,	sizeof (link_tagmode_t),	"tagmode"},
323 
324 	{ MAC_PROP_PRIVATE,	0,			"driver-private"}
325 
326 };
327 
328 static  val_desc_t	link_duplex_vals[] = {
329 	{ "half", 	LINK_DUPLEX_HALF	},
330 	{ "full", 	LINK_DUPLEX_HALF	}
331 };
332 static  val_desc_t	link_status_vals[] = {
333 	{ "up",		LINK_STATE_UP		},
334 	{ "down",	LINK_STATE_DOWN		}
335 };
336 static  val_desc_t	link_01_vals[] = {
337 	{ "1",		1			},
338 	{ "0",		0			}
339 };
340 static  val_desc_t	link_flow_vals[] = {
341 	{ "no",		LINK_FLOWCTRL_NONE	},
342 	{ "tx",		LINK_FLOWCTRL_TX	},
343 	{ "rx",		LINK_FLOWCTRL_RX	},
344 	{ "bi",		LINK_FLOWCTRL_BI	}
345 };
346 static  val_desc_t	link_priority_vals[] = {
347 	{ "low",	MPL_LOW	},
348 	{ "medium",	MPL_MEDIUM	},
349 	{ "high",	MPL_HIGH	}
350 };
351 
352 static val_desc_t	link_tagmode_vals[] = {
353 	{ "normal",	LINK_TAGMODE_NORMAL	},
354 	{ "vlanonly",	LINK_TAGMODE_VLANONLY	}
355 };
356 
357 static val_desc_t	dladm_wlan_radio_vals[] = {
358 	{ "on",		DLADM_WLAN_RADIO_ON	},
359 	{ "off",	DLADM_WLAN_RADIO_OFF	}
360 };
361 
362 static val_desc_t	dladm_wlan_powermode_vals[] = {
363 	{ "off",	DLADM_WLAN_PM_OFF	},
364 	{ "fast",	DLADM_WLAN_PM_FAST	},
365 	{ "max",	DLADM_WLAN_PM_MAX	}
366 };
367 
368 #define	VALCNT(vals)    (sizeof ((vals)) / sizeof (val_desc_t))
369 #define	RESET_VAL	((uintptr_t)-1)
370 
371 static prop_desc_t	prop_table[] = {
372 	{ "channel",	{ NULL, 0 },
373 	    NULL, 0, NULL, NULL,
374 	    do_get_channel_prop, NULL, 0,
375 	    DATALINK_CLASS_PHYS, DL_WIFI },
376 
377 	{ "powermode",	{ "off", DLADM_WLAN_PM_OFF },
378 	    dladm_wlan_powermode_vals, VALCNT(dladm_wlan_powermode_vals),
379 	    do_set_powermode_prop, NULL,
380 	    do_get_powermode_prop, NULL, 0,
381 	    DATALINK_CLASS_PHYS, DL_WIFI },
382 
383 	{ "radio",	{ "on", DLADM_WLAN_RADIO_ON },
384 	    dladm_wlan_radio_vals, VALCNT(dladm_wlan_radio_vals),
385 	    do_set_radio_prop, NULL,
386 	    do_get_radio_prop, NULL, 0,
387 	    DATALINK_CLASS_PHYS, DL_WIFI },
388 
389 	{ "speed",	{ "", 0 }, NULL, 0,
390 	    do_set_rate_prop, do_get_rate_mod,
391 	    do_get_rate_prop, do_check_rate, 0,
392 	    DATALINK_CLASS_PHYS, DATALINK_ANY_MEDIATYPE },
393 
394 	{ "autopush",	{ "", 0 }, NULL, 0,
395 	    i_dladm_set_public_prop, NULL,
396 	    do_get_autopush, do_check_autopush, PD_CHECK_ALLOC,
397 	    DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE },
398 
399 	{ "zone",	{ "", 0 }, NULL, 0,
400 	    do_set_zone, NULL,
401 	    do_get_zone, do_check_zone, PD_TEMPONLY|PD_CHECK_ALLOC,
402 	    DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE },
403 
404 	{ "duplex",	{ "", 0 },
405 	    link_duplex_vals, VALCNT(link_duplex_vals),
406 	    NULL, NULL, i_dladm_duplex_get, NULL,
407 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
408 
409 	{ "state",	{ "up", LINK_STATE_UP },
410 	    link_status_vals, VALCNT(link_status_vals),
411 	    NULL, NULL, i_dladm_status_get, NULL,
412 	    0, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE },
413 
414 	{ "adv_autoneg_cap", { "", 0 },
415 	    link_01_vals, VALCNT(link_01_vals),
416 	    i_dladm_set_public_prop, NULL, i_dladm_binary_get, NULL,
417 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
418 
419 	{ "mtu", { "", 0 }, NULL, 0,
420 	    i_dladm_set_public_prop, i_dladm_range_get,
421 	    i_dladm_uint32_get, i_dladm_defmtu_check, 0, DATALINK_CLASS_ALL,
422 	    DATALINK_ANY_MEDIATYPE },
423 
424 	{ "flowctrl", { "", 0 },
425 	    link_flow_vals, VALCNT(link_flow_vals),
426 	    i_dladm_set_public_prop, NULL, i_dladm_flowctl_get, NULL,
427 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
428 
429 	{ "adv_10gfdx_cap", { "", 0 },
430 	    link_01_vals, VALCNT(link_01_vals),
431 	    NULL, NULL, i_dladm_binary_get, NULL,
432 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
433 
434 	{ "en_10gfdx_cap", { "", 0 },
435 	    link_01_vals, VALCNT(link_01_vals),
436 	    i_dladm_set_public_prop, NULL, i_dladm_binary_get, NULL,
437 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
438 
439 	{ "adv_1000fdx_cap", { "", 0 },
440 	    link_01_vals, VALCNT(link_01_vals),
441 	    NULL, NULL, i_dladm_binary_get, NULL,
442 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
443 
444 	{ "en_1000fdx_cap", { "", 0 },
445 	    link_01_vals, VALCNT(link_01_vals),
446 	    i_dladm_set_public_prop, NULL, i_dladm_binary_get, NULL,
447 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
448 
449 	{ "adv_1000hdx_cap", { "", 0 },
450 	    link_01_vals, VALCNT(link_01_vals),
451 	    NULL, NULL, i_dladm_binary_get, NULL,
452 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
453 
454 	{ "en_1000hdx_cap", { "", 0 },
455 	    link_01_vals, VALCNT(link_01_vals),
456 	    i_dladm_set_public_prop, NULL, i_dladm_binary_get, NULL,
457 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
458 
459 	{ "adv_100fdx_cap", { "", 0 },
460 	    link_01_vals, VALCNT(link_01_vals),
461 	    NULL, NULL, i_dladm_binary_get, NULL,
462 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
463 
464 	{ "en_100fdx_cap", { "", 0 },
465 	    link_01_vals, VALCNT(link_01_vals),
466 	    i_dladm_set_public_prop, NULL, i_dladm_binary_get, NULL,
467 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
468 
469 	{ "adv_100hdx_cap", { "", 0 },
470 	    link_01_vals, VALCNT(link_01_vals),
471 	    NULL, NULL, i_dladm_binary_get, NULL,
472 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
473 
474 	{ "en_100hdx_cap", { "", 0 },
475 	    link_01_vals, VALCNT(link_01_vals),
476 	    i_dladm_set_public_prop, NULL, i_dladm_binary_get, NULL,
477 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
478 
479 	{ "adv_10fdx_cap", { "", 0 },
480 	    link_01_vals, VALCNT(link_01_vals),
481 	    NULL, NULL, i_dladm_binary_get, NULL,
482 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
483 
484 	{ "en_10fdx_cap", { "", 0 },
485 	    link_01_vals, VALCNT(link_01_vals),
486 	    i_dladm_set_public_prop, NULL, i_dladm_binary_get, NULL,
487 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
488 
489 	{ "adv_10hdx_cap", { "", 0 },
490 	    link_01_vals, VALCNT(link_01_vals),
491 	    NULL, NULL, i_dladm_binary_get, NULL,
492 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
493 
494 	{ "en_10hdx_cap", { "", 0 },
495 	    link_01_vals, VALCNT(link_01_vals),
496 	    i_dladm_set_public_prop, NULL, i_dladm_binary_get, NULL,
497 	    0, DATALINK_CLASS_PHYS, DL_ETHER },
498 
499 	{ "maxbw", { "--", RESET_VAL }, NULL, 0,
500 	    do_set_res, NULL,
501 	    i_dladm_maxbw_get, do_check_maxbw, PD_CHECK_ALLOC,
502 	    DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE },
503 
504 	{ "cpus", { "--", RESET_VAL }, NULL, 0,
505 	    do_set_cpus, NULL,
506 	    i_dladm_cpus_get, do_check_cpus, 0,
507 	    DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE },
508 
509 	{ "priority", { "high", RESET_VAL },
510 	    link_priority_vals, VALCNT(link_priority_vals), do_set_res, NULL,
511 	    i_dladm_priority_get, do_check_priority, PD_CHECK_ALLOC,
512 	    DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE },
513 
514 	{ "tagmode", { "vlanonly", LINK_TAGMODE_VLANONLY },
515 	    link_tagmode_vals, VALCNT(link_tagmode_vals),
516 	    i_dladm_set_public_prop, NULL, i_dladm_tagmode_get,
517 	    NULL, 0,
518 	    DATALINK_CLASS_PHYS | DATALINK_CLASS_AGGR | DATALINK_CLASS_VNIC,
519 	    DL_ETHER }
520 };
521 
522 #define	DLADM_MAX_PROPS	(sizeof (prop_table) / sizeof (prop_desc_t))
523 
524 static resource_prop_t rsrc_prop_table[] = {
525 	{"maxbw",	do_extract_maxbw},
526 	{"priority",	do_extract_priority},
527 	{"cpus",	do_extract_cpus}
528 };
529 #define	DLADM_MAX_RSRC_PROP (sizeof (rsrc_prop_table) / \
530 	sizeof (resource_prop_t))
531 
532 /*
533  * when retrieving  private properties, we pass down a buffer with
534  * DLADM_PROP_BUF_CHUNK of space for the driver to return the property value.
535  */
536 #define	DLADM_PROP_BUF_CHUNK	1024
537 
538 static dladm_status_t	i_dladm_set_linkprop_db(dladm_handle_t, datalink_id_t,
539 			    const char *, char **, uint_t);
540 static dladm_status_t	i_dladm_get_linkprop_db(dladm_handle_t, datalink_id_t,
541 			    const char *, char **, uint_t *);
542 static dladm_status_t	i_dladm_walk_linkprop_priv_db(dladm_handle_t,
543 			    datalink_id_t, void *, int (*)(dladm_handle_t,
544 			    datalink_id_t, const char *, void *));
545 static dladm_status_t	i_dladm_set_single_prop(dladm_handle_t, datalink_id_t,
546 			    datalink_class_t, uint32_t, prop_desc_t *, char **,
547 			    uint_t, uint_t);
548 static dladm_status_t	i_dladm_set_linkprop(dladm_handle_t, datalink_id_t,
549 			    const char *, char **, uint_t, uint_t);
550 static dladm_status_t	i_dladm_getset_defval(dladm_handle_t, prop_desc_t *,
551 			    datalink_id_t, datalink_media_t, uint_t);
552 
553 /*
554  * Unfortunately, MAX_SCAN_SUPPORT_RATES is too small to allow all
555  * rates to be retrieved. However, we cannot increase it at this
556  * time because it will break binary compatibility with unbundled
557  * WiFi drivers and utilities. So for now we define an additional
558  * constant, MAX_SUPPORT_RATES, to allow all rates to be retrieved.
559  */
560 #define	MAX_SUPPORT_RATES	64
561 
562 #define	AP_ANCHOR	"[anchor]"
563 #define	AP_DELIMITER	'.'
564 
565 static dladm_status_t
566 do_check_prop(prop_desc_t *pdp, char **prop_val, uint_t val_cnt,
567     val_desc_t *vdp)
568 {
569 	int		i, j;
570 	dladm_status_t	status = DLADM_STATUS_OK;
571 
572 	for (j = 0; j < val_cnt; j++) {
573 		for (i = 0; i < pdp->pd_noptval; i++) {
574 			if (strcasecmp(*prop_val,
575 			    pdp->pd_optval[i].vd_name) == 0) {
576 				break;
577 			}
578 		}
579 		if (i == pdp->pd_noptval) {
580 			status = DLADM_STATUS_BADVAL;
581 			goto done;
582 		}
583 		(void) memcpy(vdp + j, &pdp->pd_optval[i], sizeof (val_desc_t));
584 	}
585 
586 done:
587 	return (status);
588 }
589 
590 static dladm_status_t
591 i_dladm_set_single_prop(dladm_handle_t handle, datalink_id_t linkid,
592     datalink_class_t class, uint32_t media, prop_desc_t *pdp, char **prop_val,
593     uint_t val_cnt, uint_t flags)
594 {
595 	dladm_status_t	status = DLADM_STATUS_OK;
596 	val_desc_t	*vdp = NULL;
597 	boolean_t	needfree = B_FALSE;
598 	uint_t		cnt, i;
599 
600 	if (!(pdp->pd_class & class))
601 		return (DLADM_STATUS_BADARG);
602 
603 	if (!DATALINK_MEDIA_ACCEPTED(pdp->pd_dmedia, media))
604 		return (DLADM_STATUS_BADARG);
605 
606 	if ((flags & DLADM_OPT_PERSIST) && (pdp->pd_flags & PD_TEMPONLY))
607 		return (DLADM_STATUS_TEMPONLY);
608 
609 	if (!(flags & DLADM_OPT_ACTIVE))
610 		return (DLADM_STATUS_OK);
611 
612 	if (pdp->pd_set == NULL)
613 		return (DLADM_STATUS_PROPRDONLY);
614 
615 	if (prop_val != NULL) {
616 		vdp = malloc(sizeof (val_desc_t) * val_cnt);
617 		if (vdp == NULL)
618 			return (DLADM_STATUS_NOMEM);
619 
620 		if (pdp->pd_check != NULL) {
621 			needfree = ((pdp->pd_flags & PD_CHECK_ALLOC) != 0);
622 			status = pdp->pd_check(handle, pdp, linkid, prop_val,
623 			    val_cnt, vdp, media);
624 		} else if (pdp->pd_optval != NULL) {
625 			status = do_check_prop(pdp, prop_val, val_cnt, vdp);
626 		} else {
627 			status = DLADM_STATUS_BADARG;
628 		}
629 
630 		if (status != DLADM_STATUS_OK)
631 			goto done;
632 
633 		cnt = val_cnt;
634 	} else {
635 		boolean_t	defval = B_FALSE;
636 
637 		if (pdp->pd_defval.vd_name == NULL)
638 			return (DLADM_STATUS_NOTSUP);
639 
640 		cnt = 1;
641 		defval = (strlen(pdp->pd_defval.vd_name) > 0);
642 		if ((pdp->pd_flags & PD_CHECK_ALLOC) != 0 || defval) {
643 			if ((vdp = malloc(sizeof (val_desc_t))) == NULL)
644 				return (DLADM_STATUS_NOMEM);
645 
646 			if (defval) {
647 				(void) memcpy(vdp, &pdp->pd_defval,
648 				    sizeof (val_desc_t));
649 			} else if (pdp->pd_check != NULL) {
650 				status = pdp->pd_check(handle, pdp, linkid,
651 				    prop_val, cnt, vdp, media);
652 				if (status != DLADM_STATUS_OK)
653 					goto done;
654 			}
655 		} else {
656 			status = i_dladm_getset_defval(handle, pdp, linkid,
657 			    media, flags);
658 			return (status);
659 		}
660 	}
661 	status = pdp->pd_set(handle, pdp, linkid, vdp, cnt, flags, media);
662 	if (needfree) {
663 		for (i = 0; i < cnt; i++)
664 			free((void *)((val_desc_t *)vdp + i)->vd_val);
665 	}
666 done:
667 	free(vdp);
668 	return (status);
669 }
670 
671 static dladm_status_t
672 i_dladm_set_linkprop(dladm_handle_t handle, datalink_id_t linkid,
673     const char *prop_name, char **prop_val, uint_t val_cnt, uint_t flags)
674 {
675 	int			i;
676 	boolean_t		found = B_FALSE;
677 	datalink_class_t	class;
678 	uint32_t		media;
679 	dladm_status_t		status = DLADM_STATUS_OK;
680 
681 	status = dladm_datalink_id2info(handle, linkid, NULL, &class, &media,
682 	    NULL, 0);
683 	if (status != DLADM_STATUS_OK)
684 		return (status);
685 
686 	for (i = 0; i < DLADM_MAX_PROPS; i++) {
687 		prop_desc_t	*pdp = &prop_table[i];
688 		dladm_status_t	s;
689 
690 		if (prop_name != NULL &&
691 		    (strcasecmp(prop_name, pdp->pd_name) != 0))
692 			continue;
693 		found = B_TRUE;
694 		s = i_dladm_set_single_prop(handle, linkid, class, media, pdp,
695 		    prop_val, val_cnt, flags);
696 
697 		if (prop_name != NULL) {
698 			status = s;
699 			break;
700 		} else {
701 			if (s != DLADM_STATUS_OK &&
702 			    s != DLADM_STATUS_NOTSUP)
703 				status = s;
704 		}
705 	}
706 	if (!found) {
707 		if (prop_name[0] == '_') {
708 			/* other private properties */
709 			status = i_dladm_set_private_prop(handle, linkid,
710 			    prop_name, prop_val, val_cnt, flags);
711 		} else  {
712 			status = DLADM_STATUS_NOTFOUND;
713 		}
714 	}
715 
716 	return (status);
717 }
718 
719 /*
720  * Set/reset link property for specific link
721  */
722 dladm_status_t
723 dladm_set_linkprop(dladm_handle_t handle, datalink_id_t linkid,
724     const char *prop_name, char **prop_val, uint_t val_cnt, uint_t flags)
725 {
726 	dladm_status_t	status = DLADM_STATUS_OK;
727 
728 	if ((linkid == DATALINK_INVALID_LINKID) || (flags == 0) ||
729 	    (prop_val == NULL && val_cnt > 0) ||
730 	    (prop_val != NULL && val_cnt == 0) ||
731 	    (prop_name == NULL && prop_val != NULL)) {
732 		return (DLADM_STATUS_BADARG);
733 	}
734 
735 	/*
736 	 * Check for valid link property against the flags passed
737 	 * and set the link property when active flag is passed.
738 	 */
739 	status = i_dladm_set_linkprop(handle, linkid, prop_name, prop_val,
740 	    val_cnt, flags);
741 	if (status != DLADM_STATUS_OK)
742 		return (status);
743 
744 	if (flags & DLADM_OPT_PERSIST) {
745 		status = i_dladm_set_linkprop_db(handle, linkid, prop_name,
746 		    prop_val, val_cnt);
747 	}
748 	return (status);
749 }
750 
751 /*
752  * Walk all link properties of the given specific link.
753  *
754  * Note: this function currently lacks the ability to walk _all_ private
755  * properties if the link, because there is no kernel interface to
756  * retrieve all known private property names. Once such an interface
757  * is added, this function should be fixed accordingly.
758  */
759 dladm_status_t
760 dladm_walk_linkprop(dladm_handle_t handle, datalink_id_t linkid, void *arg,
761     int (*func)(dladm_handle_t, datalink_id_t, const char *, void *))
762 {
763 	dladm_status_t		status;
764 	datalink_class_t	class;
765 	uint_t			media;
766 	int			i;
767 
768 	if (linkid == DATALINK_INVALID_LINKID || func == NULL)
769 		return (DLADM_STATUS_BADARG);
770 
771 	status = dladm_datalink_id2info(handle, linkid, NULL, &class, &media,
772 	    NULL, 0);
773 	if (status != DLADM_STATUS_OK)
774 		return (status);
775 
776 	/* public */
777 	for (i = 0; i < DLADM_MAX_PROPS; i++) {
778 		if (!(prop_table[i].pd_class & class))
779 			continue;
780 
781 		if (!DATALINK_MEDIA_ACCEPTED(prop_table[i].pd_dmedia, media))
782 			continue;
783 
784 		if (func(handle, linkid, prop_table[i].pd_name, arg) ==
785 		    DLADM_WALK_TERMINATE) {
786 			break;
787 		}
788 	}
789 
790 	/* private */
791 	status = i_dladm_walk_linkprop_priv_db(handle, linkid, arg, func);
792 
793 	return (status);
794 }
795 
796 /*
797  * Get linkprop of the given specific link.
798  */
799 dladm_status_t
800 dladm_get_linkprop(dladm_handle_t handle, datalink_id_t linkid,
801     dladm_prop_type_t type, const char *prop_name, char **prop_val,
802     uint_t *val_cntp)
803 {
804 	dladm_status_t		status = DLADM_STATUS_OK;
805 	datalink_class_t	class;
806 	uint_t			media;
807 	prop_desc_t		*pdp;
808 	uint_t			cnt, dld_flags = 0;
809 	int			i;
810 	uint_t			perm_flags;
811 
812 	if (type == DLADM_PROP_VAL_DEFAULT)
813 		dld_flags |= MAC_PROP_DEFAULT;
814 	else if (type == DLADM_PROP_VAL_MODIFIABLE)
815 		dld_flags |= MAC_PROP_POSSIBLE;
816 
817 	if (linkid == DATALINK_INVALID_LINKID || prop_name == NULL ||
818 	    prop_val == NULL || val_cntp == NULL || *val_cntp == 0)
819 		return (DLADM_STATUS_BADARG);
820 
821 	for (i = 0; i < DLADM_MAX_PROPS; i++)
822 		if (strcasecmp(prop_name, prop_table[i].pd_name) == 0)
823 			break;
824 
825 	if (i == DLADM_MAX_PROPS) {
826 		if (prop_name[0] == '_') {
827 			/*
828 			 * private property.
829 			 */
830 			if (type == DLADM_PROP_VAL_PERSISTENT)
831 				return (i_dladm_get_linkprop_db(handle, linkid,
832 				    prop_name, prop_val, val_cntp));
833 			else
834 				return (i_dladm_get_priv_prop(handle, linkid,
835 				    prop_name, prop_val, val_cntp, type,
836 				    dld_flags));
837 		} else {
838 			return (DLADM_STATUS_NOTFOUND);
839 		}
840 	}
841 
842 	pdp = &prop_table[i];
843 
844 	status = dladm_datalink_id2info(handle, linkid, NULL, &class, &media,
845 	    NULL, 0);
846 	if (status != DLADM_STATUS_OK)
847 		return (status);
848 
849 	if (!(pdp->pd_class & class))
850 		return (DLADM_STATUS_BADARG);
851 
852 	if (!DATALINK_MEDIA_ACCEPTED(pdp->pd_dmedia, media))
853 		return (DLADM_STATUS_BADARG);
854 
855 	switch (type) {
856 	case DLADM_PROP_VAL_CURRENT:
857 		status = pdp->pd_get(handle, pdp, linkid, prop_val, val_cntp,
858 		    media, dld_flags, &perm_flags);
859 		break;
860 
861 	case DLADM_PROP_VAL_PERM:
862 		if (pdp->pd_set == NULL) {
863 			perm_flags = MAC_PROP_PERM_READ;
864 		} else {
865 			status = pdp->pd_get(handle, pdp, linkid, prop_val,
866 			    val_cntp, media, dld_flags, &perm_flags);
867 		}
868 
869 		*prop_val[0] = '\0';
870 		*val_cntp = 1;
871 		if (status == DLADM_STATUS_OK)
872 			(void) dladm_perm2str(perm_flags, *prop_val);
873 		break;
874 
875 	case DLADM_PROP_VAL_DEFAULT:
876 		/*
877 		 * If defaults are not defined for the property,
878 		 * pd_defval.vd_name should be null. If the driver
879 		 * has to be contacted for the value, vd_name should
880 		 * be the empty string (""). Otherwise, dladm will
881 		 * just print whatever is in the table.
882 		 */
883 		if (pdp->pd_defval.vd_name == NULL) {
884 			status = DLADM_STATUS_NOTSUP;
885 			break;
886 		}
887 
888 		if (strlen(pdp->pd_defval.vd_name) == 0) {
889 			status = pdp->pd_get(handle, pdp, linkid, prop_val,
890 			    val_cntp, media, dld_flags, &perm_flags);
891 		} else {
892 			(void) strcpy(*prop_val, pdp->pd_defval.vd_name);
893 		}
894 		*val_cntp = 1;
895 		break;
896 
897 	case DLADM_PROP_VAL_MODIFIABLE:
898 		if (pdp->pd_getmod != NULL) {
899 			status = pdp->pd_getmod(handle, pdp, linkid, prop_val,
900 			    val_cntp, media, dld_flags, &perm_flags);
901 			break;
902 		}
903 		cnt = pdp->pd_noptval;
904 		if (cnt == 0) {
905 			status = DLADM_STATUS_NOTSUP;
906 		} else if (cnt > *val_cntp) {
907 			status = DLADM_STATUS_TOOSMALL;
908 		} else {
909 			for (i = 0; i < cnt; i++) {
910 				(void) strcpy(prop_val[i],
911 				    pdp->pd_optval[i].vd_name);
912 			}
913 			*val_cntp = cnt;
914 		}
915 		break;
916 	case DLADM_PROP_VAL_PERSISTENT:
917 		if (pdp->pd_flags & PD_TEMPONLY)
918 			return (DLADM_STATUS_TEMPONLY);
919 		status = i_dladm_get_linkprop_db(handle, linkid, prop_name,
920 		    prop_val, val_cntp);
921 		break;
922 	default:
923 		status = DLADM_STATUS_BADARG;
924 		break;
925 	}
926 
927 	return (status);
928 }
929 
930 /*ARGSUSED*/
931 static int
932 i_dladm_init_one_prop(dladm_handle_t handle, datalink_id_t linkid,
933     const char *prop_name, void *arg)
934 {
935 	char	*buf, **propvals;
936 	uint_t	i, valcnt = DLADM_MAX_PROP_VALCNT;
937 
938 	if ((buf = malloc((sizeof (char *) + DLADM_PROP_VAL_MAX) *
939 	    DLADM_MAX_PROP_VALCNT)) == NULL) {
940 		return (DLADM_WALK_CONTINUE);
941 	}
942 
943 	propvals = (char **)(void *)buf;
944 	for (i = 0; i < valcnt; i++) {
945 		propvals[i] = buf +
946 		    sizeof (char *) * DLADM_MAX_PROP_VALCNT +
947 		    i * DLADM_PROP_VAL_MAX;
948 	}
949 
950 	if (dladm_get_linkprop(handle, linkid, DLADM_PROP_VAL_PERSISTENT,
951 	    prop_name, propvals, &valcnt) != DLADM_STATUS_OK) {
952 		goto done;
953 	}
954 
955 	(void) dladm_set_linkprop(handle, linkid, prop_name, propvals, valcnt,
956 	    DLADM_OPT_ACTIVE);
957 
958 done:
959 	if (buf != NULL)
960 		free(buf);
961 
962 	return (DLADM_WALK_CONTINUE);
963 }
964 
965 /*ARGSUSED*/
966 static int
967 i_dladm_init_linkprop(dladm_handle_t handle, datalink_id_t linkid, void *arg)
968 {
969 	datalink_class_t	class;
970 	dladm_status_t		status;
971 
972 	status = dladm_datalink_id2info(handle, linkid, NULL, &class, NULL,
973 	    NULL, 0);
974 	if (status != DLADM_STATUS_OK)
975 		return (DLADM_WALK_TERMINATE);
976 
977 	if ((class & (DATALINK_CLASS_VNIC | DATALINK_CLASS_VLAN)) == 0)
978 		(void) dladm_init_linkprop(handle, linkid, B_TRUE);
979 
980 	return (DLADM_WALK_CONTINUE);
981 }
982 
983 dladm_status_t
984 dladm_init_linkprop(dladm_handle_t handle, datalink_id_t linkid,
985     boolean_t any_media)
986 {
987 	datalink_media_t	dmedia;
988 	uint32_t		media;
989 
990 	dmedia = any_media ? DATALINK_ANY_MEDIATYPE : DL_WIFI;
991 
992 	if (linkid == DATALINK_ALL_LINKID) {
993 		(void) dladm_walk_datalink_id(i_dladm_init_linkprop, handle,
994 		    NULL, DATALINK_CLASS_ALL, dmedia, DLADM_OPT_PERSIST);
995 	} else if (any_media ||
996 	    ((dladm_datalink_id2info(handle, linkid, NULL, NULL, &media, NULL,
997 	    0) == DLADM_STATUS_OK) &&
998 	    DATALINK_MEDIA_ACCEPTED(dmedia, media))) {
999 		(void) dladm_walk_linkprop(handle, linkid, NULL,
1000 		    i_dladm_init_one_prop);
1001 	}
1002 	return (DLADM_STATUS_OK);
1003 }
1004 
1005 /* ARGSUSED */
1006 static dladm_status_t
1007 do_get_zone(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1008     char **prop_val, uint_t *val_cnt, datalink_media_t media,
1009     uint_t flags, uint_t *perm_flags)
1010 {
1011 	char			zone_name[ZONENAME_MAX];
1012 	zoneid_t		zid;
1013 	dladm_status_t		status;
1014 	char			*cp;
1015 	dld_ioc_macprop_t	*dip;
1016 
1017 	if (flags != 0)
1018 		return (DLADM_STATUS_NOTSUP);
1019 
1020 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
1021 	    &status, perm_flags);
1022 	if (status != DLADM_STATUS_OK)
1023 		return (status);
1024 
1025 	cp = dip->pr_val;
1026 	(void) memcpy(&zid, cp, sizeof (zid));
1027 	free(dip);
1028 
1029 	*val_cnt = 1;
1030 	if (zid != GLOBAL_ZONEID) {
1031 		if (getzonenamebyid(zid, zone_name, sizeof (zone_name)) < 0) {
1032 			return (dladm_errno2status(errno));
1033 		}
1034 
1035 		(void) strncpy(*prop_val, zone_name, DLADM_PROP_VAL_MAX);
1036 	} else {
1037 		*prop_val[0] = '\0';
1038 	}
1039 
1040 	return (DLADM_STATUS_OK);
1041 }
1042 
1043 typedef int (*zone_get_devroot_t)(char *, char *, size_t);
1044 
1045 static int
1046 i_dladm_get_zone_dev(char *zone_name, char *dev, size_t devlen)
1047 {
1048 	char			root[MAXPATHLEN];
1049 	zone_get_devroot_t	real_zone_get_devroot;
1050 	void			*dlhandle;
1051 	void			*sym;
1052 	int			ret;
1053 
1054 	if ((dlhandle = dlopen("libzonecfg.so.1", RTLD_LAZY)) == NULL)
1055 		return (-1);
1056 
1057 	if ((sym = dlsym(dlhandle, "zone_get_devroot")) == NULL) {
1058 		(void) dlclose(dlhandle);
1059 		return (-1);
1060 	}
1061 
1062 	real_zone_get_devroot = (zone_get_devroot_t)sym;
1063 
1064 	if ((ret = real_zone_get_devroot(zone_name, root, sizeof (root))) == 0)
1065 		(void) snprintf(dev, devlen, "%s%s", root, "/dev");
1066 	(void) dlclose(dlhandle);
1067 	return (ret);
1068 }
1069 
1070 static dladm_status_t
1071 i_dladm_update_deventry(dladm_handle_t handle, zoneid_t zid,
1072     datalink_id_t linkid, boolean_t add)
1073 {
1074 	char		path[MAXPATHLEN];
1075 	char		name[MAXLINKNAMELEN];
1076 	di_prof_t	prof = NULL;
1077 	char		zone_name[ZONENAME_MAX];
1078 	dladm_status_t	status;
1079 	int		ret;
1080 
1081 	if (getzonenamebyid(zid, zone_name, sizeof (zone_name)) < 0)
1082 		return (dladm_errno2status(errno));
1083 	if (i_dladm_get_zone_dev(zone_name, path, sizeof (path)) != 0)
1084 		return (dladm_errno2status(errno));
1085 	if (di_prof_init(path, &prof) != 0)
1086 		return (dladm_errno2status(errno));
1087 
1088 	status = dladm_linkid2legacyname(handle, linkid, name, MAXLINKNAMELEN);
1089 	if (status != DLADM_STATUS_OK)
1090 		goto cleanup;
1091 
1092 	if (add)
1093 		ret = di_prof_add_dev(prof, name);
1094 	else
1095 		ret = di_prof_add_exclude(prof, name);
1096 
1097 	if (ret != 0) {
1098 		status = dladm_errno2status(errno);
1099 		goto cleanup;
1100 	}
1101 
1102 	if (di_prof_commit(prof) != 0)
1103 		status = dladm_errno2status(errno);
1104 cleanup:
1105 	if (prof)
1106 		di_prof_fini(prof);
1107 
1108 	return (status);
1109 }
1110 
1111 /* ARGSUSED */
1112 static dladm_status_t
1113 do_set_zone(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1114     val_desc_t *vdp, uint_t val_cnt, uint_t flags, datalink_media_t media)
1115 {
1116 	dladm_status_t		status = DLADM_STATUS_OK;
1117 	zoneid_t		zid_old, zid_new;
1118 	char			link[MAXLINKNAMELEN];
1119 	char			*cp;
1120 	dld_ioc_macprop_t	*dip;
1121 	dld_ioc_zid_t		*dzp;
1122 
1123 	if (val_cnt != 1)
1124 		return (DLADM_STATUS_BADVALCNT);
1125 
1126 	dzp = (dld_ioc_zid_t *)vdp->vd_val;
1127 
1128 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
1129 	    &status, NULL);
1130 	if (status != DLADM_STATUS_OK)
1131 		return (status);
1132 
1133 	cp = dip->pr_val;
1134 	(void) memcpy(&zid_old, cp, sizeof (zid_old));
1135 	free(dip);
1136 
1137 	zid_new = dzp->diz_zid;
1138 	(void) strlcpy(link, dzp->diz_link, MAXLINKNAMELEN);
1139 
1140 	/* Do nothing if setting to current value */
1141 	if (zid_new == zid_old)
1142 		return (status);
1143 
1144 	if (zid_new != GLOBAL_ZONEID) {
1145 		/*
1146 		 * If the new zoneid is the global zone, we could destroy
1147 		 * the link (in the case of an implicitly-created VLAN) as a
1148 		 * result of setting the zoneid. In that case, we defer the
1149 		 * operation to the end of this function to avoid recreating
1150 		 * the VLAN and getting a different linkid during the rollback
1151 		 * if other operation fails.
1152 		 *
1153 		 * Otherwise, this operation will hold a reference to the
1154 		 * link and prevent a link renaming, so we need to do it
1155 		 * before other operations.
1156 		 */
1157 		status = i_dladm_set_public_prop(handle, pdp, linkid, vdp,
1158 		    val_cnt, flags, media);
1159 		if (status != DLADM_STATUS_OK)
1160 			return (status);
1161 	}
1162 
1163 	if (zid_old != GLOBAL_ZONEID) {
1164 		if (zone_remove_datalink(zid_old, link) != 0 &&
1165 		    errno != ENXIO) {
1166 			status = dladm_errno2status(errno);
1167 			goto rollback1;
1168 		}
1169 
1170 		/*
1171 		 * It is okay to fail to update the /dev entry (some
1172 		 * vanity-named links do not have a /dev entry).
1173 		 */
1174 		(void) i_dladm_update_deventry(handle, zid_old, linkid,
1175 		    B_FALSE);
1176 	}
1177 
1178 	if (zid_new != GLOBAL_ZONEID) {
1179 		if (zone_add_datalink(zid_new, link) != 0) {
1180 			status = dladm_errno2status(errno);
1181 			goto rollback2;
1182 		}
1183 
1184 		(void) i_dladm_update_deventry(handle, zid_new, linkid, B_TRUE);
1185 	} else {
1186 		status = i_dladm_set_public_prop(handle, pdp, linkid, vdp,
1187 		    val_cnt, flags, media);
1188 		if (status != DLADM_STATUS_OK)
1189 			goto rollback2;
1190 	}
1191 
1192 	return (DLADM_STATUS_OK);
1193 
1194 rollback2:
1195 	if (zid_old != GLOBAL_ZONEID)
1196 		(void) i_dladm_update_deventry(handle, zid_old, linkid, B_TRUE);
1197 	if (zid_old != GLOBAL_ZONEID)
1198 		(void) zone_add_datalink(zid_old, link);
1199 rollback1:
1200 	if (zid_new != GLOBAL_ZONEID) {
1201 		dzp->diz_zid = zid_old;
1202 		(void) i_dladm_set_public_prop(handle, pdp, linkid, vdp,
1203 		    val_cnt, flags, media);
1204 	}
1205 
1206 	return (status);
1207 }
1208 
1209 /* ARGSUSED */
1210 static dladm_status_t
1211 do_check_zone(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1212     char **prop_val, uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1213 {
1214 	char		*zone_name;
1215 	char		linkname[MAXLINKNAMELEN];
1216 	zoneid_t	zoneid;
1217 	dladm_status_t	status = DLADM_STATUS_OK;
1218 	dld_ioc_zid_t	*dzp;
1219 
1220 	if (val_cnt != 1)
1221 		return (DLADM_STATUS_BADVALCNT);
1222 
1223 	dzp = malloc(sizeof (dld_ioc_zid_t));
1224 	if (dzp == NULL)
1225 		return (DLADM_STATUS_NOMEM);
1226 
1227 	if ((status = dladm_datalink_id2info(handle, linkid, NULL, NULL, NULL,
1228 	    linkname, MAXLINKNAMELEN)) != DLADM_STATUS_OK) {
1229 		goto done;
1230 	}
1231 
1232 	zone_name = (prop_val != NULL) ? *prop_val : GLOBAL_ZONENAME;
1233 	if (strlen(linkname) > MAXLINKNAMELEN) {
1234 		status = DLADM_STATUS_BADVAL;
1235 		goto done;
1236 	}
1237 
1238 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
1239 		status = DLADM_STATUS_BADVAL;
1240 		goto done;
1241 	}
1242 
1243 	if (zoneid != GLOBAL_ZONEID) {
1244 		ushort_t	flags;
1245 
1246 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
1247 		    sizeof (flags)) < 0) {
1248 			status = dladm_errno2status(errno);
1249 			goto done;
1250 		}
1251 
1252 		if (!(flags & ZF_NET_EXCL)) {
1253 			status = DLADM_STATUS_BADVAL;
1254 			goto done;
1255 		}
1256 	}
1257 
1258 	(void) memset(dzp, 0, sizeof (dld_ioc_zid_t));
1259 
1260 	dzp->diz_zid = zoneid;
1261 	(void) strlcpy(dzp->diz_link, linkname, MAXLINKNAMELEN);
1262 
1263 	vdp->vd_val = (uintptr_t)dzp;
1264 	return (DLADM_STATUS_OK);
1265 done:
1266 	free(dzp);
1267 	return (status);
1268 }
1269 
1270 /* ARGSUSED */
1271 static dladm_status_t
1272 i_dladm_maxbw_get(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1273     char **prop_val, uint_t *val_cnt, datalink_media_t media,
1274     uint_t flags, uint_t *perm_flags)
1275 {
1276 	dld_ioc_macprop_t	*dip;
1277 	mac_resource_props_t	mrp;
1278 	dladm_status_t		status;
1279 
1280 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
1281 	    &status, perm_flags);
1282 	if (dip == NULL)
1283 		return (status);
1284 
1285 	bcopy(dip->pr_val, &mrp, sizeof (mac_resource_props_t));
1286 	free(dip);
1287 
1288 	if ((mrp.mrp_mask & MRP_MAXBW) == 0) {
1289 		(*prop_val)[0] = '\0';
1290 	} else {
1291 		(void) dladm_bw2str(mrp.mrp_maxbw, prop_val[0]);
1292 	}
1293 	*val_cnt = 1;
1294 	return (DLADM_STATUS_OK);
1295 }
1296 
1297 /* ARGSUSED */
1298 static dladm_status_t
1299 do_check_maxbw(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1300     char **prop_val, uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1301 {
1302 	uint64_t	*maxbw;
1303 	dladm_status_t	status = DLADM_STATUS_OK;
1304 
1305 	if (val_cnt != 1)
1306 		return (DLADM_STATUS_BADVALCNT);
1307 
1308 	maxbw = malloc(sizeof (uint64_t));
1309 	if (maxbw == NULL)
1310 		return (DLADM_STATUS_NOMEM);
1311 
1312 	status = dladm_str2bw(*prop_val, maxbw);
1313 	if (status != DLADM_STATUS_OK) {
1314 		free(maxbw);
1315 		return (status);
1316 	}
1317 
1318 	if ((*maxbw < MRP_MAXBW_MINVAL) && (*maxbw != 0)) {
1319 		free(maxbw);
1320 		return (DLADM_STATUS_MINMAXBW);
1321 	}
1322 
1323 	vdp->vd_val = (uintptr_t)maxbw;
1324 	return (DLADM_STATUS_OK);
1325 }
1326 
1327 /* ARGSUSED */
1328 dladm_status_t
1329 do_extract_maxbw(val_desc_t *vdp, void *arg, uint_t cnt)
1330 {
1331 	mac_resource_props_t *mrp = (mac_resource_props_t *)arg;
1332 
1333 	bcopy((char *)vdp->vd_val, &mrp->mrp_maxbw, sizeof (uint64_t));
1334 	mrp->mrp_mask |= MRP_MAXBW;
1335 
1336 	return (DLADM_STATUS_OK);
1337 }
1338 
1339 /* ARGSUSED */
1340 static dladm_status_t
1341 i_dladm_cpus_get(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1342     char **prop_val, uint_t *val_cnt, datalink_media_t media,
1343     uint_t flags, uint_t *perm_flags)
1344 {
1345 	dld_ioc_macprop_t	*dip;
1346 	mac_resource_props_t	mrp;
1347 	int			i;
1348 	uint32_t		ncpus;
1349 	uchar_t			*cp;
1350 	dladm_status_t		status;
1351 
1352 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
1353 	    &status, perm_flags);
1354 	if (dip == NULL)
1355 		return (status);
1356 
1357 	cp = (uchar_t *)dip->pr_val;
1358 	(void) memcpy(&mrp, cp, sizeof (mac_resource_props_t));
1359 	free(dip);
1360 
1361 	ncpus = mrp.mrp_ncpus;
1362 
1363 	if (ncpus > *val_cnt)
1364 		return (DLADM_STATUS_TOOSMALL);
1365 
1366 	if (ncpus == 0) {
1367 		(*prop_val)[0] = '\0';
1368 		*val_cnt = 1;
1369 		return (DLADM_STATUS_OK);
1370 	}
1371 
1372 	*val_cnt = ncpus;
1373 	for (i = 0; i < ncpus; i++) {
1374 		(void) snprintf(prop_val[i], DLADM_PROP_VAL_MAX,
1375 		    "%u", mrp.mrp_cpu[i]);
1376 	}
1377 	return (DLADM_STATUS_OK);
1378 }
1379 
1380 /* ARGSUSED */
1381 static dladm_status_t
1382 do_set_res(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1383     val_desc_t *vdp, uint_t val_cnt, uint_t flags, datalink_media_t media)
1384 {
1385 	mac_resource_props_t	mrp;
1386 	dladm_status_t		status = DLADM_STATUS_OK;
1387 	dld_ioc_macprop_t	*dip;
1388 
1389 	bzero(&mrp, sizeof (mac_resource_props_t));
1390 	dip = i_dladm_buf_alloc_by_name(0, linkid, pdp->pd_name,
1391 	    flags, &status);
1392 
1393 	if (dip == NULL)
1394 		return (status);
1395 
1396 	if (vdp->vd_val == RESET_VAL) {
1397 		switch (dip->pr_num) {
1398 		case MAC_PROP_MAXBW:
1399 			mrp.mrp_maxbw = MRP_MAXBW_RESETVAL;
1400 			mrp.mrp_mask = MRP_MAXBW;
1401 			break;
1402 		case MAC_PROP_PRIO:
1403 			mrp.mrp_priority = MPL_RESET;
1404 			mrp.mrp_mask = MRP_PRIORITY;
1405 			break;
1406 		default:
1407 			free(dip);
1408 			return (DLADM_STATUS_BADARG);
1409 		}
1410 	} else {
1411 		switch (dip->pr_num) {
1412 		case MAC_PROP_MAXBW:
1413 			bcopy((void *)vdp->vd_val, &mrp.mrp_maxbw,
1414 			    sizeof (uint64_t));
1415 			mrp.mrp_mask = MRP_MAXBW;
1416 			break;
1417 		case MAC_PROP_PRIO:
1418 			bcopy((void *)vdp->vd_val, &mrp.mrp_priority,
1419 			    sizeof (mac_priority_level_t));
1420 			mrp.mrp_mask = MRP_PRIORITY;
1421 			break;
1422 		default:
1423 			free(dip);
1424 			return (DLADM_STATUS_BADARG);
1425 		}
1426 	}
1427 
1428 	(void) memcpy(dip->pr_val, &mrp, dip->pr_valsize);
1429 	status = i_dladm_macprop(handle, dip, B_TRUE);
1430 	free(dip);
1431 	return (status);
1432 }
1433 
1434 /* ARGSUSED */
1435 static dladm_status_t
1436 do_set_cpus(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1437     val_desc_t *vdp, uint_t val_cnt, uint_t flags, datalink_media_t media)
1438 {
1439 	mac_resource_props_t	mrp;
1440 	dladm_status_t		status;
1441 	dld_ioc_macprop_t	*dip;
1442 	datalink_class_t	class;
1443 
1444 	/*
1445 	 * CPU bindings can be set on VNIC and regular physical links.
1446 	 * However VNICs fails the dladm_phys_info test(). So apply
1447 	 * the phys_info test only on physical links.
1448 	 */
1449 	if ((status = dladm_datalink_id2info(handle, linkid, NULL, &class,
1450 	    NULL, NULL, 0)) != DLADM_STATUS_OK) {
1451 		return (status);
1452 	}
1453 
1454 	/*
1455 	 * We set intr_cpu to -1. The interrupt will be retargetted,
1456 	 * if possible when the setup is complete in MAC.
1457 	 */
1458 	bzero(&mrp, sizeof (mac_resource_props_t));
1459 	mrp.mrp_mask = MRP_CPUS;
1460 	if (vdp != NULL && vdp->vd_val != RESET_VAL) {
1461 		mac_resource_props_t	*vmrp;
1462 
1463 		vmrp = (mac_resource_props_t *)vdp->vd_val;
1464 		if (vmrp->mrp_ncpus > 0) {
1465 			bcopy(vmrp, &mrp, sizeof (mac_resource_props_t));
1466 			mrp.mrp_mask = MRP_CPUS;
1467 		}
1468 		mrp.mrp_mask |= MRP_CPUS_USERSPEC;
1469 		mrp.mrp_fanout_mode = MCM_CPUS;
1470 		mrp.mrp_intr_cpu = -1;
1471 	}
1472 
1473 	dip = i_dladm_buf_alloc_by_name(0, linkid, pdp->pd_name,
1474 	    flags, &status);
1475 	if (dip == NULL)
1476 		return (status);
1477 
1478 	(void) memcpy(dip->pr_val, &mrp, dip->pr_valsize);
1479 	status = i_dladm_macprop(handle, dip, B_TRUE);
1480 	free(dip);
1481 	return (status);
1482 }
1483 
1484 /* ARGSUSED */
1485 static dladm_status_t
1486 do_check_cpus(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1487     char **prop_val, uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1488 {
1489 	uint32_t		cpuid;
1490 	int			i, j, rc;
1491 	long			nproc = sysconf(_SC_NPROCESSORS_CONF);
1492 	mac_resource_props_t	*mrp;
1493 
1494 	mrp = malloc(sizeof (mac_resource_props_t));
1495 	if (mrp == NULL)
1496 		return (DLADM_STATUS_NOMEM);
1497 
1498 	for (i = 0; i < val_cnt; i++) {
1499 		errno = 0;
1500 		cpuid = strtol(prop_val[i], (char **)NULL, 10);
1501 		if (errno != 0 || cpuid >= nproc) {
1502 			free(mrp);
1503 			return (DLADM_STATUS_CPUMAX);
1504 		}
1505 		rc = p_online(cpuid, P_STATUS);
1506 		if (rc < 1) {
1507 			free(mrp);
1508 			return (DLADM_STATUS_CPUERR);
1509 		}
1510 		if (rc != P_ONLINE) {
1511 			free(mrp);
1512 			return (DLADM_STATUS_CPUNOTONLINE);
1513 		}
1514 		mrp->mrp_cpu[i] = cpuid;
1515 	}
1516 	mrp->mrp_ncpus = (uint32_t)val_cnt;
1517 
1518 	/* Check for duplicates */
1519 	for (i = 0; i < val_cnt; i++) {
1520 		for (j = 0; j < val_cnt; j++) {
1521 			if (i != j && mrp->mrp_cpu[i] == mrp->mrp_cpu[j]) {
1522 				free(mrp);
1523 				return (DLADM_STATUS_BADARG);
1524 			}
1525 		}
1526 	}
1527 	vdp->vd_val = (uintptr_t)mrp;
1528 
1529 	return (DLADM_STATUS_OK);
1530 }
1531 
1532 /* ARGSUSED */
1533 dladm_status_t
1534 do_extract_cpus(val_desc_t *vdp, void *arg, uint_t cnt)
1535 {
1536 	mac_resource_props_t	*mrp = (mac_resource_props_t *)arg;
1537 	mac_resource_props_t	*vmrp = (mac_resource_props_t *)vdp->vd_val;
1538 	int			i;
1539 
1540 	for (i = 0; i < vmrp->mrp_ncpus; i++) {
1541 		mrp->mrp_cpu[i] = vmrp->mrp_cpu[i];
1542 	}
1543 	mrp->mrp_ncpus = vmrp->mrp_ncpus;
1544 	mrp->mrp_mask |= (MRP_CPUS|MRP_CPUS_USERSPEC);
1545 	mrp->mrp_fanout_mode = MCM_CPUS;
1546 	mrp->mrp_intr_cpu = -1;
1547 
1548 	return (DLADM_STATUS_OK);
1549 }
1550 
1551 /* ARGSUSED */
1552 static dladm_status_t
1553 i_dladm_priority_get(dladm_handle_t handle, prop_desc_t *pdp,
1554     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
1555     datalink_media_t media, uint_t flags, uint_t *perm_flags)
1556 {
1557 	dld_ioc_macprop_t	*dip;
1558 	mac_resource_props_t	mrp;
1559 	mac_priority_level_t	pri;
1560 	dladm_status_t		status;
1561 
1562 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
1563 	    &status, perm_flags);
1564 	if (dip == NULL)
1565 		return (status);
1566 
1567 	bcopy(dip->pr_val, &mrp, sizeof (mac_resource_props_t));
1568 	free(dip);
1569 
1570 	pri = ((mrp.mrp_mask & MRP_PRIORITY) == 0) ? MPL_HIGH :
1571 	    mrp.mrp_priority;
1572 
1573 	(void) dladm_pri2str(pri, prop_val[0]);
1574 	*val_cnt = 1;
1575 	return (DLADM_STATUS_OK);
1576 }
1577 
1578 /* ARGSUSED */
1579 static dladm_status_t
1580 do_check_priority(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1581     char **prop_val, uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1582 {
1583 	mac_priority_level_t	*pri;
1584 	dladm_status_t	status = DLADM_STATUS_OK;
1585 
1586 	if (val_cnt != 1)
1587 		return (DLADM_STATUS_BADVALCNT);
1588 
1589 	pri = malloc(sizeof (mac_priority_level_t));
1590 	if (pri == NULL)
1591 		return (DLADM_STATUS_NOMEM);
1592 
1593 	status = dladm_str2pri(*prop_val, pri);
1594 	if (status != DLADM_STATUS_OK) {
1595 		free(pri);
1596 		return (status);
1597 	}
1598 
1599 	if (*pri < MPL_LOW || *pri > MPL_HIGH) {
1600 		free(pri);
1601 		return (DLADM_STATUS_BADVAL);
1602 	}
1603 
1604 	vdp->vd_val = (uintptr_t)pri;
1605 	return (DLADM_STATUS_OK);
1606 }
1607 
1608 /* ARGSUSED */
1609 dladm_status_t
1610 do_extract_priority(val_desc_t *vdp, void *arg, uint_t cnt)
1611 {
1612 	mac_resource_props_t *mrp = (mac_resource_props_t *)arg;
1613 
1614 	bcopy((char *)vdp->vd_val, &mrp->mrp_priority,
1615 	    sizeof (mac_priority_level_t));
1616 	mrp->mrp_mask |= MRP_PRIORITY;
1617 
1618 	return (DLADM_STATUS_OK);
1619 }
1620 
1621 /* ARGSUSED */
1622 static dladm_status_t
1623 do_get_autopush(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1624     char **prop_val, uint_t *val_cnt, datalink_media_t media,
1625     uint_t flags, uint_t *perm_flags)
1626 {
1627 	struct		dlautopush dlap;
1628 	int		i, len;
1629 	dladm_status_t	status;
1630 	dld_ioc_macprop_t	*dip;
1631 
1632 	if (flags & MAC_PROP_DEFAULT)
1633 		return (DLADM_STATUS_NOTDEFINED);
1634 
1635 	*val_cnt = 1;
1636 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
1637 	    &status, perm_flags);
1638 	if (dip == NULL) {
1639 		(*prop_val)[0] = '\0';
1640 		return (DLADM_STATUS_OK);
1641 	}
1642 	(void) memcpy(&dlap, dip->pr_val, sizeof (dlap));
1643 
1644 	for (i = 0, len = 0; i < dlap.dap_npush; i++) {
1645 		if (i != 0) {
1646 			(void) snprintf(*prop_val + len,
1647 			    DLADM_PROP_VAL_MAX - len, "%c", AP_DELIMITER);
1648 			len += 1;
1649 		}
1650 		(void) snprintf(*prop_val + len, DLADM_PROP_VAL_MAX - len,
1651 		    "%s", dlap.dap_aplist[i]);
1652 		len += strlen(dlap.dap_aplist[i]);
1653 		if (dlap.dap_anchor - 1 == i) {
1654 			(void) snprintf(*prop_val + len,
1655 			    DLADM_PROP_VAL_MAX - len, "%c%s", AP_DELIMITER,
1656 			    AP_ANCHOR);
1657 			len += (strlen(AP_ANCHOR) + 1);
1658 		}
1659 	}
1660 	free(dip);
1661 done:
1662 	return (DLADM_STATUS_OK);
1663 }
1664 
1665 /*
1666  * Add the specified module to the dlautopush structure; returns a
1667  * DLADM_STATUS_* code.
1668  */
1669 dladm_status_t
1670 i_dladm_add_ap_module(const char *module, struct dlautopush *dlap)
1671 {
1672 	if ((strlen(module) == 0) || (strlen(module) > FMNAMESZ))
1673 		return (DLADM_STATUS_BADVAL);
1674 
1675 	if (strncasecmp(module, AP_ANCHOR, strlen(AP_ANCHOR)) == 0) {
1676 		/*
1677 		 * We don't allow multiple anchors, and the anchor must
1678 		 * be after at least one module.
1679 		 */
1680 		if (dlap->dap_anchor != 0)
1681 			return (DLADM_STATUS_BADVAL);
1682 		if (dlap->dap_npush == 0)
1683 			return (DLADM_STATUS_BADVAL);
1684 
1685 		dlap->dap_anchor = dlap->dap_npush;
1686 		return (DLADM_STATUS_OK);
1687 	}
1688 	if (dlap->dap_npush >= MAXAPUSH)
1689 		return (DLADM_STATUS_BADVALCNT);
1690 
1691 	(void) strlcpy(dlap->dap_aplist[dlap->dap_npush++], module,
1692 	    FMNAMESZ + 1);
1693 
1694 	return (DLADM_STATUS_OK);
1695 }
1696 
1697 /*
1698  * Currently, both '.' and ' '(space) can be used as the delimiters between
1699  * autopush modules. The former is used in dladm set-linkprop, and the
1700  * latter is used in the autopush(1M) file.
1701  */
1702 /* ARGSUSED */
1703 static dladm_status_t
1704 do_check_autopush(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1705     char **prop_val, uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1706 {
1707 	char			*module;
1708 	struct dlautopush	*dlap;
1709 	dladm_status_t		status;
1710 	char			val[DLADM_PROP_VAL_MAX];
1711 	char			delimiters[4];
1712 
1713 	if (val_cnt != 1)
1714 		return (DLADM_STATUS_BADVALCNT);
1715 
1716 	if (prop_val != NULL) {
1717 		dlap = malloc(sizeof (struct dlautopush));
1718 		if (dlap == NULL)
1719 			return (DLADM_STATUS_NOMEM);
1720 
1721 		(void) memset(dlap, 0, sizeof (struct dlautopush));
1722 		(void) snprintf(delimiters, 4, " %c\n", AP_DELIMITER);
1723 		bcopy(*prop_val, val, DLADM_PROP_VAL_MAX);
1724 		module = strtok(val, delimiters);
1725 		while (module != NULL) {
1726 			status = i_dladm_add_ap_module(module, dlap);
1727 			if (status != DLADM_STATUS_OK)
1728 				return (status);
1729 			module = strtok(NULL, delimiters);
1730 		}
1731 
1732 		vdp->vd_val = (uintptr_t)dlap;
1733 	} else {
1734 		vdp->vd_val = 0;
1735 	}
1736 	return (DLADM_STATUS_OK);
1737 }
1738 
1739 #define	WLDP_BUFSIZE (MAX_BUF_LEN - WIFI_BUF_OFFSET)
1740 
1741 /* ARGSUSED */
1742 static dladm_status_t
1743 do_get_rate_common(dladm_handle_t handle, prop_desc_t *pdp,
1744     datalink_id_t linkid, char **prop_val, uint_t *val_cnt, uint_t id,
1745     uint_t *perm_flags)
1746 {
1747 	wl_rates_t	*wrp;
1748 	uint_t		i;
1749 	dladm_status_t	status = DLADM_STATUS_OK;
1750 
1751 	wrp = malloc(WLDP_BUFSIZE);
1752 	if (wrp == NULL)
1753 		return (DLADM_STATUS_NOMEM);
1754 
1755 	status = i_dladm_wlan_param(handle, linkid, wrp, id, WLDP_BUFSIZE,
1756 	    B_FALSE);
1757 	if (status != DLADM_STATUS_OK)
1758 		goto done;
1759 
1760 	if (wrp->wl_rates_num > *val_cnt) {
1761 		status = DLADM_STATUS_TOOSMALL;
1762 		goto done;
1763 	}
1764 
1765 	if (wrp->wl_rates_rates[0] == 0) {
1766 		prop_val[0][0] = '\0';
1767 		*val_cnt = 1;
1768 		goto done;
1769 	}
1770 
1771 	for (i = 0; i < wrp->wl_rates_num; i++) {
1772 		(void) snprintf(prop_val[i], DLADM_STRSIZE, "%.*f",
1773 		    wrp->wl_rates_rates[i] % 2,
1774 		    (float)wrp->wl_rates_rates[i] / 2);
1775 	}
1776 	*val_cnt = wrp->wl_rates_num;
1777 	*perm_flags = MAC_PROP_PERM_RW;
1778 
1779 done:
1780 	free(wrp);
1781 	return (status);
1782 }
1783 
1784 static dladm_status_t
1785 do_get_rate_prop(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1786     char **prop_val, uint_t *val_cnt, datalink_media_t media,
1787     uint_t flags, uint_t *perm_flags)
1788 {
1789 	if (media != DL_WIFI) {
1790 		return (i_dladm_speed_get(handle, pdp, linkid, prop_val,
1791 		    val_cnt, flags, perm_flags));
1792 	}
1793 
1794 	return (do_get_rate_common(handle, pdp, linkid, prop_val, val_cnt,
1795 	    MAC_PROP_WL_DESIRED_RATES, perm_flags));
1796 }
1797 
1798 /* ARGSUSED */
1799 static dladm_status_t
1800 do_get_rate_mod(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1801     char **prop_val, uint_t *val_cnt, datalink_media_t media,
1802     uint_t flags, uint_t *perm_flags)
1803 {
1804 	switch (media) {
1805 	case DL_ETHER:
1806 		/*
1807 		 * Speed for ethernet links is unbounded. E.g., 802.11b
1808 		 * links can have a speed of 5.5 Gbps.
1809 		 */
1810 		return (DLADM_STATUS_NOTSUP);
1811 
1812 	case DL_WIFI:
1813 		return (do_get_rate_common(handle, pdp, linkid, prop_val,
1814 		    val_cnt, MAC_PROP_WL_SUPPORTED_RATES, perm_flags));
1815 	default:
1816 		return (DLADM_STATUS_BADARG);
1817 	}
1818 }
1819 
1820 static dladm_status_t
1821 do_set_rate(dladm_handle_t handle, datalink_id_t linkid,
1822     dladm_wlan_rates_t *rates)
1823 {
1824 	int		i;
1825 	uint_t		len;
1826 	wl_rates_t	*wrp;
1827 	dladm_status_t	status = DLADM_STATUS_OK;
1828 
1829 	wrp = malloc(WLDP_BUFSIZE);
1830 	if (wrp == NULL)
1831 		return (DLADM_STATUS_NOMEM);
1832 
1833 	bzero(wrp, WLDP_BUFSIZE);
1834 	for (i = 0; i < rates->wr_cnt; i++)
1835 		wrp->wl_rates_rates[i] = rates->wr_rates[i];
1836 	wrp->wl_rates_num = rates->wr_cnt;
1837 
1838 	len = offsetof(wl_rates_t, wl_rates_rates) +
1839 	    (rates->wr_cnt * sizeof (char)) + WIFI_BUF_OFFSET;
1840 	status = i_dladm_wlan_param(handle, linkid, wrp,
1841 	    MAC_PROP_WL_DESIRED_RATES, len, B_TRUE);
1842 
1843 	free(wrp);
1844 	return (status);
1845 }
1846 
1847 /* ARGSUSED */
1848 static dladm_status_t
1849 do_set_rate_prop(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1850     val_desc_t *vdp, uint_t val_cnt, uint_t flags, datalink_media_t media)
1851 {
1852 	dladm_wlan_rates_t	rates;
1853 	dladm_status_t		status;
1854 
1855 	/*
1856 	 * can currently set rate on WIFI links only.
1857 	 */
1858 	if (media != DL_WIFI)
1859 		return (DLADM_STATUS_PROPRDONLY);
1860 
1861 	if (val_cnt != 1)
1862 		return (DLADM_STATUS_BADVALCNT);
1863 
1864 	rates.wr_cnt = 1;
1865 	rates.wr_rates[0] = vdp[0].vd_val;
1866 
1867 	status = do_set_rate(handle, linkid, &rates);
1868 
1869 done:
1870 	return (status);
1871 }
1872 
1873 /* ARGSUSED */
1874 static dladm_status_t
1875 do_check_rate(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1876     char **prop_val, uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1877 {
1878 	int		i;
1879 	uint_t		modval_cnt = MAX_SUPPORT_RATES;
1880 	char		*buf, **modval;
1881 	dladm_status_t	status;
1882 	uint_t 		perm_flags;
1883 
1884 	if (val_cnt != 1)
1885 		return (DLADM_STATUS_BADVALCNT);
1886 
1887 	buf = malloc((sizeof (char *) + DLADM_STRSIZE) *
1888 	    MAX_SUPPORT_RATES);
1889 	if (buf == NULL) {
1890 		status = DLADM_STATUS_NOMEM;
1891 		goto done;
1892 	}
1893 
1894 	modval = (char **)(void *)buf;
1895 	for (i = 0; i < MAX_SUPPORT_RATES; i++) {
1896 		modval[i] = buf + sizeof (char *) * MAX_SUPPORT_RATES +
1897 		    i * DLADM_STRSIZE;
1898 	}
1899 
1900 	status = do_get_rate_mod(handle, NULL, linkid, modval, &modval_cnt,
1901 	    media, 0, &perm_flags);
1902 	if (status != DLADM_STATUS_OK)
1903 		goto done;
1904 
1905 	for (i = 0; i < modval_cnt; i++) {
1906 		if (strcasecmp(*prop_val, modval[i]) == 0) {
1907 			vdp->vd_val = (uintptr_t)(uint_t)
1908 			    (atof(*prop_val) * 2);
1909 			status = DLADM_STATUS_OK;
1910 			break;
1911 		}
1912 	}
1913 	if (i == modval_cnt)
1914 		status = DLADM_STATUS_BADVAL;
1915 done:
1916 	free(buf);
1917 	return (status);
1918 }
1919 
1920 static dladm_status_t
1921 do_get_phyconf(dladm_handle_t handle, datalink_id_t linkid, void *buf,
1922     int buflen)
1923 {
1924 	return (i_dladm_wlan_param(handle, linkid, buf, MAC_PROP_WL_PHY_CONFIG,
1925 	    buflen, B_FALSE));
1926 }
1927 
1928 /* ARGSUSED */
1929 static dladm_status_t
1930 do_get_channel_prop(dladm_handle_t handle, prop_desc_t *pdp,
1931     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
1932     datalink_media_t media, uint_t flags, uint_t *perm_flags)
1933 {
1934 	uint32_t	channel;
1935 	char		buf[WLDP_BUFSIZE];
1936 	dladm_status_t	status = DLADM_STATUS_OK;
1937 	wl_phy_conf_t	wl_phy_conf;
1938 
1939 	if ((status = do_get_phyconf(handle, linkid, buf, sizeof (buf)))
1940 	    != DLADM_STATUS_OK)
1941 		goto done;
1942 
1943 	(void) memcpy(&wl_phy_conf, buf, sizeof (wl_phy_conf));
1944 	if (!i_dladm_wlan_convert_chan(&wl_phy_conf, &channel)) {
1945 		status = DLADM_STATUS_NOTFOUND;
1946 		goto done;
1947 	}
1948 
1949 	(void) snprintf(*prop_val, DLADM_STRSIZE, "%u", channel);
1950 	*val_cnt = 1;
1951 	*perm_flags = MAC_PROP_PERM_READ;
1952 done:
1953 	return (status);
1954 }
1955 
1956 static dladm_status_t
1957 do_get_powermode(dladm_handle_t handle, datalink_id_t linkid, void *buf,
1958     int buflen)
1959 {
1960 	return (i_dladm_wlan_param(handle, linkid, buf, MAC_PROP_WL_POWER_MODE,
1961 	    buflen, B_FALSE));
1962 }
1963 
1964 /* ARGSUSED */
1965 static dladm_status_t
1966 do_get_powermode_prop(dladm_handle_t handle, prop_desc_t *pdp,
1967     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
1968     datalink_media_t media, uint_t flags, uint_t *perm_flags)
1969 {
1970 	wl_ps_mode_t	mode;
1971 	const char	*s;
1972 	char		buf[WLDP_BUFSIZE];
1973 	dladm_status_t	status = DLADM_STATUS_OK;
1974 
1975 	if ((status = do_get_powermode(handle, linkid, buf, sizeof (buf)))
1976 	    != DLADM_STATUS_OK)
1977 		goto done;
1978 
1979 	(void) memcpy(&mode, buf, sizeof (mode));
1980 	switch (mode.wl_ps_mode) {
1981 	case WL_PM_AM:
1982 		s = "off";
1983 		break;
1984 	case WL_PM_MPS:
1985 		s = "max";
1986 		break;
1987 	case WL_PM_FAST:
1988 		s = "fast";
1989 		break;
1990 	default:
1991 		status = DLADM_STATUS_NOTFOUND;
1992 		goto done;
1993 	}
1994 	(void) snprintf(*prop_val, DLADM_STRSIZE, "%s", s);
1995 	*val_cnt = 1;
1996 	*perm_flags = MAC_PROP_PERM_RW;
1997 done:
1998 	return (status);
1999 }
2000 
2001 static dladm_status_t
2002 do_set_powermode(dladm_handle_t handle, datalink_id_t linkid,
2003     dladm_wlan_powermode_t *pm)
2004 {
2005 	wl_ps_mode_t    ps_mode;
2006 
2007 	(void) memset(&ps_mode, 0xff, sizeof (ps_mode));
2008 
2009 	switch (*pm) {
2010 	case DLADM_WLAN_PM_OFF:
2011 		ps_mode.wl_ps_mode = WL_PM_AM;
2012 		break;
2013 	case DLADM_WLAN_PM_MAX:
2014 		ps_mode.wl_ps_mode = WL_PM_MPS;
2015 		break;
2016 	case DLADM_WLAN_PM_FAST:
2017 		ps_mode.wl_ps_mode = WL_PM_FAST;
2018 		break;
2019 	default:
2020 		return (DLADM_STATUS_NOTSUP);
2021 	}
2022 	return (i_dladm_wlan_param(handle, linkid, &ps_mode,
2023 	    MAC_PROP_WL_POWER_MODE, sizeof (ps_mode), B_TRUE));
2024 }
2025 
2026 /* ARGSUSED */
2027 static dladm_status_t
2028 do_set_powermode_prop(dladm_handle_t handle, prop_desc_t *pdp,
2029     datalink_id_t linkid, val_desc_t *vdp, uint_t val_cnt, uint_t flags,
2030     datalink_media_t media)
2031 {
2032 	dladm_wlan_powermode_t powermode = (dladm_wlan_powermode_t)vdp->vd_val;
2033 	dladm_status_t status;
2034 
2035 	if (val_cnt != 1)
2036 		return (DLADM_STATUS_BADVALCNT);
2037 
2038 	status = do_set_powermode(handle, linkid, &powermode);
2039 
2040 	return (status);
2041 }
2042 
2043 static dladm_status_t
2044 do_get_radio(dladm_handle_t handle, datalink_id_t linkid, void *buf, int buflen)
2045 {
2046 	return (i_dladm_wlan_param(handle, linkid, buf, MAC_PROP_WL_RADIO,
2047 	    buflen, B_FALSE));
2048 }
2049 
2050 /* ARGSUSED */
2051 static dladm_status_t
2052 do_get_radio_prop(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
2053     char **prop_val, uint_t *val_cnt, datalink_media_t media,
2054     uint_t flags, uint_t *perm_flags)
2055 {
2056 	wl_radio_t	radio;
2057 	const char	*s;
2058 	char		buf[WLDP_BUFSIZE];
2059 	dladm_status_t	status = DLADM_STATUS_OK;
2060 
2061 	if ((status = do_get_radio(handle, linkid, buf, sizeof (buf)))
2062 	    != DLADM_STATUS_OK)
2063 		goto done;
2064 
2065 	(void) memcpy(&radio, buf, sizeof (radio));
2066 	switch (radio) {
2067 	case B_TRUE:
2068 		s = "on";
2069 		break;
2070 	case B_FALSE:
2071 		s = "off";
2072 		break;
2073 	default:
2074 		status = DLADM_STATUS_NOTFOUND;
2075 		goto done;
2076 	}
2077 	(void) snprintf(*prop_val, DLADM_STRSIZE, "%s", s);
2078 	*val_cnt = 1;
2079 	*perm_flags = MAC_PROP_PERM_RW;
2080 done:
2081 	return (status);
2082 }
2083 
2084 static dladm_status_t
2085 do_set_radio(dladm_handle_t handle, datalink_id_t linkid,
2086     dladm_wlan_radio_t *radio)
2087 {
2088 	wl_radio_t r;
2089 
2090 	switch (*radio) {
2091 	case DLADM_WLAN_RADIO_ON:
2092 		r = B_TRUE;
2093 		break;
2094 	case DLADM_WLAN_RADIO_OFF:
2095 		r = B_FALSE;
2096 		break;
2097 	default:
2098 		return (DLADM_STATUS_NOTSUP);
2099 	}
2100 	return (i_dladm_wlan_param(handle, linkid, &r, MAC_PROP_WL_RADIO,
2101 	    sizeof (r), B_TRUE));
2102 }
2103 
2104 /* ARGSUSED */
2105 static dladm_status_t
2106 do_set_radio_prop(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
2107     val_desc_t *vdp, uint_t val_cnt, uint_t fags, datalink_media_t media)
2108 {
2109 	dladm_wlan_radio_t radio = (dladm_wlan_radio_t)vdp->vd_val;
2110 	dladm_status_t status;
2111 
2112 	if (val_cnt != 1)
2113 		return (DLADM_STATUS_BADVALCNT);
2114 
2115 	status = do_set_radio(handle, linkid, &radio);
2116 
2117 	return (status);
2118 }
2119 
2120 static dladm_status_t
2121 i_dladm_set_linkprop_db(dladm_handle_t handle, datalink_id_t linkid,
2122     const char *prop_name, char **prop_val, uint_t val_cnt)
2123 {
2124 	char		buf[MAXLINELEN];
2125 	int		i;
2126 	dladm_conf_t	conf;
2127 	dladm_status_t	status;
2128 
2129 	status = dladm_read_conf(handle, linkid, &conf);
2130 	if (status != DLADM_STATUS_OK)
2131 		return (status);
2132 
2133 	/*
2134 	 * reset case.
2135 	 */
2136 	if (val_cnt == 0) {
2137 		status = dladm_unset_conf_field(handle, conf, prop_name);
2138 		if (status == DLADM_STATUS_OK)
2139 			status = dladm_write_conf(handle, conf);
2140 		goto done;
2141 	}
2142 
2143 	buf[0] = '\0';
2144 	for (i = 0; i < val_cnt; i++) {
2145 		(void) strlcat(buf, prop_val[i], MAXLINELEN);
2146 		if (i != val_cnt - 1)
2147 			(void) strlcat(buf, ",", MAXLINELEN);
2148 	}
2149 
2150 	status = dladm_set_conf_field(handle, conf, prop_name, DLADM_TYPE_STR,
2151 	    buf);
2152 	if (status == DLADM_STATUS_OK)
2153 		status = dladm_write_conf(handle, conf);
2154 
2155 done:
2156 	dladm_destroy_conf(handle, conf);
2157 	return (status);
2158 }
2159 
2160 static dladm_status_t
2161 i_dladm_get_linkprop_db(dladm_handle_t handle, datalink_id_t linkid,
2162     const char *prop_name, char **prop_val, uint_t *val_cntp)
2163 {
2164 	char		buf[MAXLINELEN], *str;
2165 	uint_t		cnt = 0;
2166 	dladm_conf_t	conf;
2167 	dladm_status_t	status;
2168 
2169 	status = dladm_read_conf(handle, linkid, &conf);
2170 	if (status != DLADM_STATUS_OK)
2171 		return (status);
2172 
2173 	status = dladm_get_conf_field(handle, conf, prop_name, buf, MAXLINELEN);
2174 	if (status != DLADM_STATUS_OK)
2175 		goto done;
2176 
2177 	str = strtok(buf, ",");
2178 	while (str != NULL) {
2179 		if (cnt == *val_cntp) {
2180 			status = DLADM_STATUS_TOOSMALL;
2181 			goto done;
2182 		}
2183 		(void) strlcpy(prop_val[cnt++], str, DLADM_PROP_VAL_MAX);
2184 		str = strtok(NULL, ",");
2185 	}
2186 
2187 	*val_cntp = cnt;
2188 
2189 done:
2190 	dladm_destroy_conf(handle, conf);
2191 	return (status);
2192 }
2193 
2194 /*
2195  * Walk persistent private link properties of a link.
2196  */
2197 static dladm_status_t
2198 i_dladm_walk_linkprop_priv_db(dladm_handle_t handle, datalink_id_t linkid,
2199     void *arg, int (*func)(dladm_handle_t, datalink_id_t, const char *, void *))
2200 {
2201 	dladm_status_t		status;
2202 	dladm_conf_t		conf;
2203 	char			last_attr[MAXLINKATTRLEN];
2204 	char			attr[MAXLINKATTRLEN];
2205 	char			attrval[MAXLINKATTRVALLEN];
2206 	size_t			attrsz;
2207 
2208 	if (linkid == DATALINK_INVALID_LINKID || func == NULL)
2209 		return (DLADM_STATUS_BADARG);
2210 
2211 	status = dladm_read_conf(handle, linkid, &conf);
2212 	if (status != DLADM_STATUS_OK)
2213 		return (status);
2214 
2215 	last_attr[0] = '\0';
2216 	while ((status = dladm_getnext_conf_linkprop(handle, conf, last_attr,
2217 	    attr, attrval, MAXLINKATTRVALLEN, &attrsz)) == DLADM_STATUS_OK) {
2218 		if (attr[0] == '_') {
2219 			if (func(handle, linkid, attr, arg) ==
2220 			    DLADM_WALK_TERMINATE)
2221 				break;
2222 		}
2223 		(void) strlcpy(last_attr, attr, MAXLINKATTRLEN);
2224 	}
2225 
2226 	dladm_destroy_conf(handle, conf);
2227 	return (DLADM_STATUS_OK);
2228 }
2229 
2230 static link_attr_t *
2231 dladm_name2prop(const char *prop_name)
2232 {
2233 	link_attr_t *p;
2234 
2235 	for (p = link_attr; p->pp_id != MAC_PROP_PRIVATE; p++) {
2236 		if (strcmp(p->pp_name, prop_name) == 0)
2237 			break;
2238 	}
2239 	return (p);
2240 }
2241 
2242 static link_attr_t *
2243 dladm_id2prop(mac_prop_id_t propid)
2244 {
2245 	link_attr_t *p;
2246 
2247 	for (p = link_attr; p->pp_id != MAC_PROP_PRIVATE; p++) {
2248 		if (p->pp_id == propid)
2249 			break;
2250 	}
2251 	return (p);
2252 }
2253 
2254 static dld_ioc_macprop_t *
2255 i_dladm_buf_alloc_impl(size_t valsize, datalink_id_t linkid,
2256     const char *prop_name, mac_prop_id_t propid, uint_t flags,
2257     dladm_status_t *status)
2258 {
2259 	int dsize;
2260 	dld_ioc_macprop_t *dip;
2261 
2262 	*status = DLADM_STATUS_OK;
2263 	dsize = MAC_PROP_BUFSIZE(valsize);
2264 	dip = malloc(dsize);
2265 	if (dip == NULL) {
2266 		*status = DLADM_STATUS_NOMEM;
2267 		return (NULL);
2268 	}
2269 	bzero(dip, dsize);
2270 	dip->pr_valsize = valsize;
2271 	(void) strlcpy(dip->pr_name, prop_name, sizeof (dip->pr_name));
2272 	dip->pr_version = MAC_PROP_VERSION;
2273 	dip->pr_linkid = linkid;
2274 	dip->pr_num = propid;
2275 	dip->pr_flags = flags;
2276 	return (dip);
2277 }
2278 
2279 static dld_ioc_macprop_t *
2280 i_dladm_buf_alloc_by_name(size_t valsize, datalink_id_t linkid,
2281     const char *prop_name, uint_t flags, dladm_status_t *status)
2282 {
2283 	link_attr_t *p;
2284 
2285 	p = dladm_name2prop(prop_name);
2286 	valsize = MAX(p->pp_valsize, valsize);
2287 	return (i_dladm_buf_alloc_impl(valsize, linkid, prop_name, p->pp_id,
2288 	    flags, status));
2289 }
2290 
2291 static dld_ioc_macprop_t *
2292 i_dladm_buf_alloc_by_id(size_t valsize, datalink_id_t linkid,
2293     mac_prop_id_t propid, uint_t flags, dladm_status_t *status)
2294 {
2295 	link_attr_t *p;
2296 
2297 	p = dladm_id2prop(propid);
2298 	valsize = MAX(p->pp_valsize, valsize);
2299 	return (i_dladm_buf_alloc_impl(valsize, linkid, p->pp_name, propid,
2300 	    flags, status));
2301 }
2302 
2303 /* ARGSUSED */
2304 static dladm_status_t
2305 i_dladm_set_public_prop(dladm_handle_t handle, prop_desc_t *pdp,
2306     datalink_id_t linkid, val_desc_t *vdp, uint_t val_cnt, uint_t flags,
2307     datalink_media_t media)
2308 {
2309 	dld_ioc_macprop_t	*dip;
2310 	dladm_status_t	status = DLADM_STATUS_OK;
2311 	uint8_t		u8;
2312 	uint16_t	u16;
2313 	uint32_t	u32;
2314 	void		*val;
2315 
2316 	dip = i_dladm_buf_alloc_by_name(0, linkid, pdp->pd_name, 0, &status);
2317 	if (dip == NULL)
2318 		return (status);
2319 
2320 	if (pdp->pd_flags & PD_CHECK_ALLOC)
2321 		val = (void *)vdp->vd_val;
2322 	else {
2323 		/*
2324 		 * Currently all 1/2/4-byte size properties are byte/word/int.
2325 		 * No need (yet) to distinguish these from arrays of same size.
2326 		 */
2327 		switch (dip->pr_valsize) {
2328 		case 1:
2329 			u8 = vdp->vd_val;
2330 			val = &u8;
2331 			break;
2332 		case 2:
2333 			u16 = vdp->vd_val;
2334 			val = &u16;
2335 			break;
2336 		case 4:
2337 			u32 = vdp->vd_val;
2338 			val = &u32;
2339 			break;
2340 		default:
2341 			val = &vdp->vd_val;
2342 			break;
2343 		}
2344 	}
2345 
2346 	if (val != NULL)
2347 		(void) memcpy(dip->pr_val, val, dip->pr_valsize);
2348 	else
2349 		dip->pr_valsize = 0;
2350 
2351 	status = i_dladm_macprop(handle, dip, B_TRUE);
2352 
2353 done:
2354 	free(dip);
2355 	return (status);
2356 }
2357 
2358 dladm_status_t
2359 i_dladm_macprop(dladm_handle_t handle, void *dip, boolean_t set)
2360 {
2361 	dladm_status_t status = DLADM_STATUS_OK;
2362 
2363 	if (ioctl(dladm_dld_fd(handle),
2364 	    (set ? DLDIOC_SETMACPROP : DLDIOC_GETMACPROP), dip))
2365 		status = dladm_errno2status(errno);
2366 
2367 	return (status);
2368 }
2369 
2370 static dld_ioc_macprop_t *
2371 i_dladm_get_public_prop(dladm_handle_t handle, datalink_id_t linkid,
2372     char *prop_name, uint_t flags, dladm_status_t *status, uint_t *perm_flags)
2373 {
2374 	dld_ioc_macprop_t *dip = NULL;
2375 
2376 	dip = i_dladm_buf_alloc_by_name(0, linkid, prop_name, flags, status);
2377 	if (dip == NULL)
2378 		return (NULL);
2379 
2380 	*status = i_dladm_macprop(handle, dip, B_FALSE);
2381 	if (*status != DLADM_STATUS_OK) {
2382 		free(dip);
2383 		return (NULL);
2384 	}
2385 	if (perm_flags != NULL)
2386 		*perm_flags = dip->pr_perm_flags;
2387 
2388 	return (dip);
2389 }
2390 
2391 /* ARGSUSED */
2392 static dladm_status_t
2393 i_dladm_defmtu_check(dladm_handle_t handle, prop_desc_t *pdp,
2394     datalink_id_t linkid, char **prop_val, uint_t val_cnt, val_desc_t *v,
2395     datalink_media_t media)
2396 {
2397 	if (val_cnt != 1)
2398 		return (DLADM_STATUS_BADVAL);
2399 	v->vd_val = atoi(prop_val[0]);
2400 	return (DLADM_STATUS_OK);
2401 }
2402 
2403 /* ARGSUSED */
2404 static dladm_status_t
2405 i_dladm_duplex_get(dladm_handle_t handle, prop_desc_t *pdp,
2406     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
2407     datalink_media_t media, uint_t flags, uint_t *perm_flags)
2408 {
2409 	link_duplex_t   link_duplex;
2410 	dladm_status_t  status;
2411 
2412 	if ((status = dladm_get_single_mac_stat(handle, linkid, "link_duplex",
2413 	    KSTAT_DATA_UINT32, &link_duplex)) != 0)
2414 		return (status);
2415 
2416 	switch (link_duplex) {
2417 	case LINK_DUPLEX_FULL:
2418 		(void) strcpy(*prop_val, "full");
2419 		break;
2420 	case LINK_DUPLEX_HALF:
2421 		(void) strcpy(*prop_val, "half");
2422 		break;
2423 	default:
2424 		(void) strcpy(*prop_val, "unknown");
2425 		break;
2426 	}
2427 	*val_cnt = 1;
2428 	return (DLADM_STATUS_OK);
2429 }
2430 
2431 /* ARGSUSED */
2432 static dladm_status_t
2433 i_dladm_speed_get(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
2434     char **prop_val, uint_t *val_cnt, uint_t flags, uint_t *perm_flags)
2435 {
2436 	uint64_t	ifspeed = 0;
2437 	dladm_status_t status;
2438 
2439 	if ((status = dladm_get_single_mac_stat(handle, linkid, "ifspeed",
2440 	    KSTAT_DATA_UINT64, &ifspeed)) != 0)
2441 		return (status);
2442 
2443 	if ((ifspeed % 1000000) != 0) {
2444 		(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX,
2445 		    "%llf", ifspeed / (float)1000000); /* Mbps */
2446 	} else {
2447 		(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX,
2448 		    "%llu", ifspeed / 1000000); /* Mbps */
2449 	}
2450 	*val_cnt = 1;
2451 	*perm_flags = MAC_PROP_PERM_READ;
2452 	return (DLADM_STATUS_OK);
2453 }
2454 
2455 /* ARGSUSED */
2456 static dladm_status_t
2457 i_dladm_status_get(dladm_handle_t handle, prop_desc_t *pdp,
2458     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
2459     datalink_media_t media, uint_t flags, uint_t *perm_flags)
2460 {
2461 	link_state_t		link_state;
2462 	dladm_status_t		status;
2463 
2464 	status = i_dladm_get_state(handle, linkid, &link_state);
2465 	if (status != DLADM_STATUS_OK)
2466 		return (status);
2467 
2468 	switch (link_state) {
2469 	case LINK_STATE_UP:
2470 		(void) strcpy(*prop_val, "up");
2471 		break;
2472 	case LINK_STATE_DOWN:
2473 		(void) strcpy(*prop_val, "down");
2474 		break;
2475 	default:
2476 		(void) strcpy(*prop_val, "unknown");
2477 		break;
2478 	}
2479 	*val_cnt = 1;
2480 	*perm_flags = MAC_PROP_PERM_READ;
2481 	return (DLADM_STATUS_OK);
2482 }
2483 
2484 /* ARGSUSED */
2485 static dladm_status_t
2486 i_dladm_binary_get(dladm_handle_t handle, prop_desc_t *pdp,
2487     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
2488     datalink_media_t media, uint_t flags, uint_t *perm_flags)
2489 {
2490 	dld_ioc_macprop_t *dip;
2491 	dladm_status_t status;
2492 
2493 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
2494 	    &status, perm_flags);
2495 	if (dip == NULL)
2496 		return (status);
2497 
2498 	(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX, "%x", dip->pr_val[0]);
2499 	free(dip);
2500 	*val_cnt = 1;
2501 	return (DLADM_STATUS_OK);
2502 }
2503 
2504 /* ARGSUSED */
2505 static dladm_status_t
2506 i_dladm_uint32_get(dladm_handle_t handle, prop_desc_t *pdp,
2507     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
2508     datalink_media_t media, uint_t flags, uint_t *perm_flags)
2509 {
2510 	dld_ioc_macprop_t *dip;
2511 	uint32_t v = 0;
2512 	uchar_t *cp;
2513 	dladm_status_t status;
2514 
2515 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
2516 	    &status, perm_flags);
2517 	if (dip == NULL)
2518 		return (status);
2519 
2520 	cp = (uchar_t *)dip->pr_val;
2521 	(void) memcpy(&v, cp, sizeof (v));
2522 	(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX, "%ld", v);
2523 	free(dip);
2524 	*val_cnt = 1;
2525 	return (DLADM_STATUS_OK);
2526 }
2527 
2528 /*
2529  * Determines the size of the structure that needs to be sent to drivers
2530  * for retrieving the property range values.
2531  */
2532 static int
2533 i_dladm_range_size(mac_propval_range_t *r, size_t *sz)
2534 {
2535 	uint_t count = r->mpr_count;
2536 
2537 	*sz = sizeof (mac_propval_range_t);
2538 	--count;
2539 
2540 	switch (r->mpr_type) {
2541 	case MAC_PROPVAL_UINT32:
2542 		*sz += (count * sizeof (mac_propval_uint32_range_t));
2543 		return (0);
2544 	default:
2545 		break;
2546 	}
2547 	*sz = 0;
2548 	return (EINVAL);
2549 }
2550 
2551 /* ARGSUSED */
2552 static dladm_status_t
2553 i_dladm_range_get(dladm_handle_t handle, prop_desc_t *pdp,
2554     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
2555     datalink_media_t media, uint_t flags, uint_t *perm_flags)
2556 {
2557 	dld_ioc_macprop_t *dip;
2558 	dladm_status_t status = DLADM_STATUS_OK;
2559 	size_t	sz;
2560 	mac_propval_range_t *rangep;
2561 
2562 	sz = sizeof (mac_propval_range_t);
2563 
2564 	/*
2565 	 * As caller we don't know number of value ranges, the driver
2566 	 * supports. To begin with we assume that number to be 1. If the
2567 	 * buffer size is insufficient, driver returns back with the
2568 	 * actual count of value ranges. See mac.h for more details.
2569 	 */
2570 retry:
2571 	if ((dip = i_dladm_buf_alloc_by_name(sz, linkid, pdp->pd_name, flags,
2572 	    &status)) == NULL)
2573 		return (status);
2574 
2575 	status = i_dladm_macprop(handle, dip, B_FALSE);
2576 	if (status != DLADM_STATUS_OK) {
2577 		if (status == DLADM_STATUS_TOOSMALL) {
2578 			int err;
2579 
2580 			rangep = (mac_propval_range_t *)(void *)&dip->pr_val;
2581 			if ((err = i_dladm_range_size(rangep, &sz)) == 0) {
2582 				free(dip);
2583 				goto retry;
2584 			} else {
2585 				status = dladm_errno2status(err);
2586 			}
2587 		}
2588 		free(dip);
2589 		return (status);
2590 	}
2591 	rangep = (mac_propval_range_t *)(void *)&dip->pr_val;
2592 
2593 	switch (rangep->mpr_type) {
2594 	case MAC_PROPVAL_UINT32: {
2595 		mac_propval_uint32_range_t *ur;
2596 		uint_t	count = rangep->mpr_count, i;
2597 
2598 		ur = &rangep->range_uint32[0];
2599 
2600 		for (i = 0; i < count; i++, ur++) {
2601 			if (ur->mpur_min == ur->mpur_max) {
2602 				(void) snprintf(prop_val[i], DLADM_PROP_VAL_MAX,
2603 				    "%ld", ur->mpur_min);
2604 			} else {
2605 				(void) snprintf(prop_val[i], DLADM_PROP_VAL_MAX,
2606 				    "%ld-%ld", ur->mpur_min, ur->mpur_max);
2607 			}
2608 		}
2609 		*val_cnt = count;
2610 		break;
2611 	}
2612 	default:
2613 		status = DLADM_STATUS_BADARG;
2614 		break;
2615 	}
2616 	free(dip);
2617 	return (status);
2618 }
2619 
2620 /* ARGSUSED */
2621 static dladm_status_t
2622 i_dladm_tagmode_get(dladm_handle_t handle, prop_desc_t *pdp,
2623     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
2624     datalink_media_t media, uint_t flags, uint_t *perm_flags)
2625 {
2626 	dld_ioc_macprop_t	*dip;
2627 	link_tagmode_t		mode;
2628 	dladm_status_t		status;
2629 
2630 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
2631 	    &status, perm_flags);
2632 	if (dip == NULL)
2633 		return (status);
2634 	(void) memcpy(&mode, dip->pr_val, sizeof (mode));
2635 	free(dip);
2636 
2637 	switch (mode) {
2638 	case LINK_TAGMODE_NORMAL:
2639 		(void) strlcpy(*prop_val, "normal", DLADM_PROP_VAL_MAX);
2640 		break;
2641 	case LINK_TAGMODE_VLANONLY:
2642 		(void) strlcpy(*prop_val, "vlanonly", DLADM_PROP_VAL_MAX);
2643 		break;
2644 	default:
2645 		(void) strlcpy(*prop_val, "unknown", DLADM_PROP_VAL_MAX);
2646 	}
2647 	*val_cnt = 1;
2648 	return (DLADM_STATUS_OK);
2649 }
2650 
2651 /* ARGSUSED */
2652 static dladm_status_t
2653 i_dladm_flowctl_get(dladm_handle_t handle, prop_desc_t *pdp,
2654     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
2655     datalink_media_t media, uint_t flags, uint_t *perm_flags)
2656 {
2657 	dld_ioc_macprop_t *dip;
2658 	link_flowctrl_t v;
2659 	dladm_status_t status;
2660 	uchar_t *cp;
2661 
2662 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
2663 	    &status, perm_flags);
2664 	if (dip == NULL)
2665 		return (status);
2666 
2667 	cp = (uchar_t *)dip->pr_val;
2668 	(void) memcpy(&v, cp, sizeof (v));
2669 	switch (v) {
2670 	case LINK_FLOWCTRL_NONE:
2671 		(void) sprintf(*prop_val, "no");
2672 		break;
2673 	case LINK_FLOWCTRL_RX:
2674 		(void) sprintf(*prop_val, "rx");
2675 		break;
2676 	case LINK_FLOWCTRL_TX:
2677 		(void) sprintf(*prop_val, "tx");
2678 		break;
2679 	case LINK_FLOWCTRL_BI:
2680 		(void) sprintf(*prop_val, "bi");
2681 		break;
2682 	}
2683 	free(dip);
2684 	*val_cnt = 1;
2685 	return (DLADM_STATUS_OK);
2686 }
2687 
2688 
2689 /* ARGSUSED */
2690 static dladm_status_t
2691 i_dladm_set_private_prop(dladm_handle_t handle, datalink_id_t linkid,
2692     const char *prop_name, char **prop_val, uint_t val_cnt, uint_t flags)
2693 
2694 {
2695 	int		i, slen;
2696 	int 		bufsize = 0;
2697 	dld_ioc_macprop_t *dip = NULL;
2698 	uchar_t 	*dp;
2699 	link_attr_t *p;
2700 	dladm_status_t	status = DLADM_STATUS_OK;
2701 
2702 	if ((prop_name == NULL && prop_val != NULL) ||
2703 	    (prop_val != NULL && val_cnt == 0))
2704 		return (DLADM_STATUS_BADARG);
2705 	p = dladm_name2prop(prop_name);
2706 	if (p->pp_id != MAC_PROP_PRIVATE)
2707 		return (DLADM_STATUS_BADARG);
2708 
2709 	if (!(flags & DLADM_OPT_ACTIVE))
2710 		return (DLADM_STATUS_OK);
2711 
2712 	/*
2713 	 * private properties: all parsing is done in the kernel.
2714 	 * allocate a enough space for each property + its separator (',').
2715 	 */
2716 	for (i = 0; i < val_cnt; i++) {
2717 		bufsize += strlen(prop_val[i]) + 1;
2718 	}
2719 
2720 	if (prop_val == NULL) {
2721 		/*
2722 		 * getting default value. so use more buffer space.
2723 		 */
2724 		bufsize += DLADM_PROP_BUF_CHUNK;
2725 	}
2726 
2727 	dip = i_dladm_buf_alloc_by_name(bufsize + 1, linkid, prop_name,
2728 	    (prop_val != NULL ? 0 : MAC_PROP_DEFAULT), &status);
2729 	if (dip == NULL)
2730 		return (status);
2731 
2732 	dp = (uchar_t *)dip->pr_val;
2733 	slen = 0;
2734 
2735 	if (prop_val == NULL) {
2736 		status = i_dladm_macprop(handle, dip, B_FALSE);
2737 		dip->pr_flags = 0;
2738 	} else {
2739 		for (i = 0; i < val_cnt; i++) {
2740 			int plen = 0;
2741 
2742 			plen = strlen(prop_val[i]);
2743 			bcopy(prop_val[i], dp, plen);
2744 			slen += plen;
2745 			/*
2746 			 * add a "," separator and update dp.
2747 			 */
2748 			if (i != (val_cnt -1))
2749 				dp[slen++] = ',';
2750 			dp += (plen + 1);
2751 		}
2752 	}
2753 	if (status == DLADM_STATUS_OK)
2754 		status = i_dladm_macprop(handle, dip, B_TRUE);
2755 
2756 	free(dip);
2757 	return (status);
2758 }
2759 
2760 static dladm_status_t
2761 i_dladm_get_priv_prop(dladm_handle_t handle, datalink_id_t linkid,
2762     const char *prop_name, char **prop_val, uint_t *val_cnt,
2763     dladm_prop_type_t type, uint_t dld_flags)
2764 {
2765 	dladm_status_t	status = DLADM_STATUS_OK;
2766 	dld_ioc_macprop_t *dip = NULL;
2767 	link_attr_t *p;
2768 
2769 	if ((prop_name == NULL && prop_val != NULL) ||
2770 	    (prop_val != NULL && val_cnt == 0))
2771 		return (DLADM_STATUS_BADARG);
2772 
2773 	p = dladm_name2prop(prop_name);
2774 	if (p->pp_id != MAC_PROP_PRIVATE)
2775 		return (DLADM_STATUS_BADARG);
2776 
2777 	/*
2778 	 * private properties: all parsing is done in the kernel.
2779 	 */
2780 	dip = i_dladm_buf_alloc_by_name(DLADM_PROP_BUF_CHUNK, linkid, prop_name,
2781 	    dld_flags, &status);
2782 	if (dip == NULL)
2783 		return (status);
2784 
2785 	if ((status = i_dladm_macprop(handle, dip, B_FALSE)) ==
2786 	    DLADM_STATUS_OK) {
2787 		if (type == DLADM_PROP_VAL_PERM) {
2788 			(void) dladm_perm2str(dip->pr_perm_flags, *prop_val);
2789 		} else if (type == DLADM_PROP_VAL_MODIFIABLE) {
2790 			*prop_val[0] = '\0';
2791 		} else {
2792 			(void) strncpy(*prop_val, dip->pr_val,
2793 			    DLADM_PROP_VAL_MAX);
2794 		}
2795 		*val_cnt = 1;
2796 	} else if ((status == DLADM_STATUS_NOTSUP) &&
2797 	    (type == DLADM_PROP_VAL_CURRENT)) {
2798 		status = DLADM_STATUS_NOTFOUND;
2799 	}
2800 	free(dip);
2801 	return (status);
2802 }
2803 
2804 
2805 static dladm_status_t
2806 i_dladm_getset_defval(dladm_handle_t handle, prop_desc_t *pdp,
2807     datalink_id_t linkid, datalink_media_t media, uint_t flags)
2808 {
2809 	dladm_status_t status;
2810 	char **prop_vals = NULL, *buf;
2811 	size_t bufsize;
2812 	uint_t cnt;
2813 	int i;
2814 	uint_t perm_flags;
2815 
2816 	/*
2817 	 * Allocate buffer needed for prop_vals array. We can have at most
2818 	 * DLADM_MAX_PROP_VALCNT char *prop_vals[] entries, where
2819 	 * each entry has max size DLADM_PROP_VAL_MAX
2820 	 */
2821 	bufsize =
2822 	    (sizeof (char *) + DLADM_PROP_VAL_MAX) * DLADM_MAX_PROP_VALCNT;
2823 	buf = malloc(bufsize);
2824 	prop_vals = (char **)(void *)buf;
2825 	for (i = 0; i < DLADM_MAX_PROP_VALCNT; i++) {
2826 		prop_vals[i] = buf +
2827 		    sizeof (char *) * DLADM_MAX_PROP_VALCNT +
2828 		    i * DLADM_PROP_VAL_MAX;
2829 	}
2830 
2831 	/*
2832 	 * For properties which have pdp->pd_defval.vd_name as a non-empty
2833 	 * string, the "" itself is used to reset the property (exceptions
2834 	 * are zone and autopush, which populate vdp->vd_val). So
2835 	 * libdladm can copy pdp->pd_defval over to the val_desc_t passed
2836 	 * down on the setprop using the global values in the table. For
2837 	 * other cases (vd_name is ""), doing reset-linkprop will cause
2838 	 * libdladm to do a getprop to find the default value and then do
2839 	 * a setprop to reset the value to default.
2840 	 */
2841 	status = pdp->pd_get(handle, pdp, linkid, prop_vals, &cnt, media,
2842 	    MAC_PROP_DEFAULT, &perm_flags);
2843 	if (status == DLADM_STATUS_OK) {
2844 		if (perm_flags == MAC_PROP_PERM_RW) {
2845 			status = i_dladm_set_single_prop(handle, linkid,
2846 			    pdp->pd_class, media, pdp, prop_vals, cnt, flags);
2847 		}
2848 		else
2849 			status = DLADM_STATUS_NOTSUP;
2850 	}
2851 	free(buf);
2852 	return (status);
2853 }
2854 
2855 dladm_status_t
2856 i_dladm_wlan_param(dladm_handle_t handle, datalink_id_t linkid, void *buf,
2857     mac_prop_id_t cmd, size_t len, boolean_t set)
2858 {
2859 	uint32_t		flags;
2860 	dladm_status_t		status;
2861 	uint32_t		media;
2862 	dld_ioc_macprop_t	*dip;
2863 	void			*dp;
2864 
2865 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, NULL,
2866 	    &media, NULL, 0)) != DLADM_STATUS_OK) {
2867 		return (status);
2868 	}
2869 
2870 	if (media != DL_WIFI)
2871 		return (DLADM_STATUS_BADARG);
2872 
2873 	if (!(flags & DLADM_OPT_ACTIVE))
2874 		return (DLADM_STATUS_TEMPONLY);
2875 
2876 	if (len == (MAX_BUF_LEN - WIFI_BUF_OFFSET))
2877 		len = MAX_BUF_LEN - sizeof (dld_ioc_macprop_t) - 1;
2878 
2879 	dip = i_dladm_buf_alloc_by_id(len, linkid, cmd, 0, &status);
2880 	if (dip == NULL)
2881 		return (DLADM_STATUS_NOMEM);
2882 
2883 	dp = (uchar_t *)dip->pr_val;
2884 	if (set)
2885 		(void) memcpy(dp, buf, len);
2886 
2887 	status = i_dladm_macprop(handle, dip, set);
2888 	if (status == DLADM_STATUS_OK) {
2889 		if (!set)
2890 			(void) memcpy(buf, dp, len);
2891 	}
2892 
2893 	free(dip);
2894 	return (status);
2895 }
2896 
2897 dladm_status_t
2898 dladm_parse_link_props(char *str, dladm_arg_list_t **listp, boolean_t novalues)
2899 {
2900 	return (dladm_parse_args(str, listp, novalues));
2901 }
2902 
2903 /*
2904  * Retrieve the one link property from the database
2905  */
2906 /*ARGSUSED*/
2907 static int
2908 i_dladm_get_one_prop(dladm_handle_t handle, datalink_id_t linkid,
2909     const char *prop_name, void *arg)
2910 {
2911 	dladm_arg_list_t	*proplist = arg;
2912 	dladm_arg_info_t	*aip = NULL;
2913 
2914 	aip = &proplist->al_info[proplist->al_count];
2915 	/*
2916 	 * it is fine to point to prop_name since prop_name points to the
2917 	 * prop_table[n].pd_name.
2918 	 */
2919 	aip->ai_name = prop_name;
2920 
2921 	(void) dladm_get_linkprop(handle, linkid, DLADM_PROP_VAL_PERSISTENT,
2922 	    prop_name, aip->ai_val, &aip->ai_count);
2923 
2924 	if (aip->ai_count != 0)
2925 		proplist->al_count++;
2926 
2927 	return (DLADM_WALK_CONTINUE);
2928 }
2929 
2930 
2931 /*
2932  * Retrieve all link properties for a link from the database and
2933  * return a property list.
2934  */
2935 dladm_status_t
2936 dladm_link_get_proplist(dladm_handle_t handle, datalink_id_t linkid,
2937     dladm_arg_list_t **listp)
2938 {
2939 	dladm_arg_list_t	*list;
2940 	dladm_status_t		status = DLADM_STATUS_OK;
2941 
2942 	list = calloc(1, sizeof (dladm_arg_list_t));
2943 	if (list == NULL)
2944 		return (dladm_errno2status(errno));
2945 
2946 	status = dladm_walk_linkprop(handle, linkid, list,
2947 	    i_dladm_get_one_prop);
2948 
2949 	*listp = list;
2950 	return (status);
2951 }
2952 
2953 /*
2954  * Retrieve the named property from a proplist, check the value and
2955  * convert to a kernel structure.
2956  */
2957 static dladm_status_t
2958 i_dladm_link_proplist_extract_one(dladm_handle_t handle,
2959     dladm_arg_list_t *proplist, const char *name, void *val)
2960 {
2961 	dladm_status_t		status;
2962 	dladm_arg_info_t	*aip = NULL;
2963 	int			i, j;
2964 
2965 	/* Find named property in proplist */
2966 	for (i = 0; i < proplist->al_count; i++) {
2967 		aip = &proplist->al_info[i];
2968 		if (strcasecmp(aip->ai_name, name) == 0)
2969 			break;
2970 	}
2971 
2972 	/* Property not in list */
2973 	if (i == proplist->al_count)
2974 		return (DLADM_STATUS_OK);
2975 
2976 	for (i = 0; i < DLADM_MAX_PROPS; i++) {
2977 		prop_desc_t	*pdp = &prop_table[i];
2978 		val_desc_t	*vdp;
2979 
2980 		vdp = malloc(sizeof (val_desc_t) * aip->ai_count);
2981 		if (vdp == NULL)
2982 			return (DLADM_STATUS_NOMEM);
2983 
2984 		if (strcasecmp(aip->ai_name, pdp->pd_name) != 0)
2985 			continue;
2986 
2987 		if (aip->ai_val == NULL)
2988 			return (DLADM_STATUS_BADARG);
2989 
2990 		/* Check property value */
2991 		if (pdp->pd_check != NULL) {
2992 			status = pdp->pd_check(handle, pdp, 0, aip->ai_val,
2993 			    aip->ai_count, vdp, 0);
2994 		} else {
2995 			status = DLADM_STATUS_BADARG;
2996 		}
2997 
2998 		if (status != DLADM_STATUS_OK)
2999 			return (status);
3000 
3001 		for (j = 0; j < DLADM_MAX_RSRC_PROP; j++) {
3002 			resource_prop_t	*rpp = &rsrc_prop_table[j];
3003 
3004 			if (strcasecmp(aip->ai_name, rpp->rp_name) != 0)
3005 				continue;
3006 
3007 			/* Extract kernel structure */
3008 			if (rpp->rp_extract != NULL) {
3009 				status = rpp->rp_extract(vdp, val,
3010 				    aip->ai_count);
3011 			} else {
3012 				status = DLADM_STATUS_BADARG;
3013 			}
3014 			break;
3015 		}
3016 
3017 		if (status != DLADM_STATUS_OK)
3018 			return (status);
3019 
3020 		break;
3021 	}
3022 	return (status);
3023 }
3024 
3025 /*
3026  * Extract properties from a proplist and convert to mac_resource_props_t.
3027  */
3028 dladm_status_t
3029 dladm_link_proplist_extract(dladm_handle_t handle, dladm_arg_list_t *proplist,
3030     mac_resource_props_t *mrp)
3031 {
3032 	dladm_status_t	status = DLADM_STATUS_OK;
3033 
3034 	status = i_dladm_link_proplist_extract_one(handle, proplist, "maxbw",
3035 	    mrp);
3036 	if (status != DLADM_STATUS_OK)
3037 		return (status);
3038 	status = i_dladm_link_proplist_extract_one(handle, proplist, "priority",
3039 	    mrp);
3040 	if (status != DLADM_STATUS_OK)
3041 		return (status);
3042 	status = i_dladm_link_proplist_extract_one(handle, proplist, "cpus",
3043 	    mrp);
3044 	if (status != DLADM_STATUS_OK)
3045 		return (status);
3046 	return (status);
3047 }
3048 
3049 static const char *
3050 dladm_perm2str(uint_t perm, char *buf)
3051 {
3052 	(void) snprintf(buf, DLADM_STRSIZE, "%c%c",
3053 	    ((perm & MAC_PROP_PERM_READ) != 0) ? 'r' : '-',
3054 	    ((perm & MAC_PROP_PERM_WRITE) != 0) ? 'w' : '-');
3055 	return (buf);
3056 }
3057 
3058 dladm_status_t
3059 i_dladm_get_state(dladm_handle_t handle, datalink_id_t linkid,
3060     link_state_t *state)
3061 {
3062 	dld_ioc_macprop_t	*dip;
3063 	dladm_status_t		status;
3064 	uint_t			perms;
3065 
3066 	dip = i_dladm_get_public_prop(handle, linkid, "state", 0, &status,
3067 	    &perms);
3068 	if (status != DLADM_STATUS_OK)
3069 		return (status);
3070 	(void) memcpy(state, dip->pr_val, sizeof (*state));
3071 	free(dip);
3072 	return (status);
3073 }
3074 
3075 boolean_t
3076 dladm_attr_is_linkprop(const char *name)
3077 {
3078 	/* non-property attribute names */
3079 	const char *nonprop[] = {
3080 		/* dlmgmtd core attributes */
3081 		"name",
3082 		"class",
3083 		"media",
3084 		FPHYMAJ,
3085 		FPHYINST,
3086 		FDEVNAME,
3087 
3088 		/* other attributes for vlan, aggr, etc */
3089 		DLADM_ATTR_NAMES
3090 	};
3091 	boolean_t	is_nonprop = B_FALSE;
3092 	int		i;
3093 
3094 	for (i = 0; i < sizeof (nonprop) / sizeof (nonprop[0]); i++) {
3095 		if (strcmp(name, nonprop[i]) == 0) {
3096 			is_nonprop = B_TRUE;
3097 			break;
3098 		}
3099 	}
3100 
3101 	return (!is_nonprop);
3102 }
3103