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