xref: /titanic_41/usr/src/lib/libdladm/common/linkprop.c (revision 6f786ace10b9c0c7c5515e525fb660fbccfda6a3)
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, i_dladm_range_get;
137 
138 static pd_setf_t	do_set_zone, do_set_rate_prop,
139 			do_set_powermode_prop, do_set_radio_prop,
140 			i_dladm_set_public_prop, do_set_res, do_set_cpus;
141 
142 static pd_checkf_t	do_check_zone, do_check_autopush, do_check_rate,
143 			i_dladm_defmtu_check, do_check_maxbw, do_check_cpus,
144 			do_check_priority;
145 
146 static dladm_status_t	i_dladm_speed_get(dladm_handle_t, prop_desc_t *,
147 			    datalink_id_t, char **, uint_t *, uint_t, uint_t *);
148 static dladm_status_t	i_dladm_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, i_dladm_range_get,
425 	    i_dladm_uint32_get, 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 	else if (type == DLADM_PROP_VAL_MODIFIABLE)
815 		dld_flags |= MAC_PROP_POSSIBLE;
816 
817 	if (linkid == DATALINK_INVALID_LINKID || prop_name == NULL ||
818 	    prop_val == NULL || val_cntp == NULL || *val_cntp == 0)
819 		return (DLADM_STATUS_BADARG);
820 
821 	for (i = 0; i < DLADM_MAX_PROPS; i++)
822 		if (strcasecmp(prop_name, prop_table[i].pd_name) == 0)
823 			break;
824 
825 	if (i == DLADM_MAX_PROPS) {
826 		if (prop_name[0] == '_') {
827 			/*
828 			 * private property.
829 			 */
830 			if (type == DLADM_PROP_VAL_PERSISTENT)
831 				return (i_dladm_get_linkprop_db(handle, linkid,
832 				    prop_name, prop_val, val_cntp));
833 			else
834 				return (i_dladm_get_priv_prop(handle, linkid,
835 				    prop_name, prop_val, val_cntp, type,
836 				    dld_flags));
837 		} else {
838 			return (DLADM_STATUS_NOTFOUND);
839 		}
840 	}
841 
842 	pdp = &prop_table[i];
843 
844 	status = dladm_datalink_id2info(handle, linkid, NULL, &class, &media,
845 	    NULL, 0);
846 	if (status != DLADM_STATUS_OK)
847 		return (status);
848 
849 	if (!(pdp->pd_class & class))
850 		return (DLADM_STATUS_BADARG);
851 
852 	if (!DATALINK_MEDIA_ACCEPTED(pdp->pd_dmedia, media))
853 		return (DLADM_STATUS_BADARG);
854 
855 	switch (type) {
856 	case DLADM_PROP_VAL_CURRENT:
857 		status = pdp->pd_get(handle, pdp, linkid, prop_val, val_cntp,
858 		    media, dld_flags, &perm_flags);
859 		break;
860 
861 	case DLADM_PROP_VAL_PERM:
862 		if (pdp->pd_set == NULL) {
863 			perm_flags = MAC_PROP_PERM_READ;
864 		} else {
865 			status = pdp->pd_get(handle, pdp, linkid, prop_val,
866 			    val_cntp, media, dld_flags, &perm_flags);
867 		}
868 
869 		*prop_val[0] = '\0';
870 		*val_cntp = 1;
871 		if (status == DLADM_STATUS_OK)
872 			(void) dladm_perm2str(perm_flags, *prop_val);
873 		break;
874 
875 	case DLADM_PROP_VAL_DEFAULT:
876 		/*
877 		 * If defaults are not defined for the property,
878 		 * pd_defval.vd_name should be null. If the driver
879 		 * has to be contacted for the value, vd_name should
880 		 * be the empty string (""). Otherwise, dladm will
881 		 * just print whatever is in the table.
882 		 */
883 		if (pdp->pd_defval.vd_name == NULL) {
884 			status = DLADM_STATUS_NOTSUP;
885 			break;
886 		}
887 
888 		if (strlen(pdp->pd_defval.vd_name) == 0) {
889 			status = pdp->pd_get(handle, pdp, linkid, prop_val,
890 			    val_cntp, media, dld_flags, &perm_flags);
891 		} else {
892 			(void) strcpy(*prop_val, pdp->pd_defval.vd_name);
893 		}
894 		*val_cntp = 1;
895 		break;
896 
897 	case DLADM_PROP_VAL_MODIFIABLE:
898 		if (pdp->pd_getmod != NULL) {
899 			status = pdp->pd_getmod(handle, pdp, linkid, prop_val,
900 			    val_cntp, media, dld_flags, &perm_flags);
901 			break;
902 		}
903 		cnt = pdp->pd_noptval;
904 		if (cnt == 0) {
905 			status = DLADM_STATUS_NOTSUP;
906 		} else if (cnt > *val_cntp) {
907 			status = DLADM_STATUS_TOOSMALL;
908 		} else {
909 			for (i = 0; i < cnt; i++) {
910 				(void) strcpy(prop_val[i],
911 				    pdp->pd_optval[i].vd_name);
912 			}
913 			*val_cntp = cnt;
914 		}
915 		break;
916 	case DLADM_PROP_VAL_PERSISTENT:
917 		if (pdp->pd_flags & PD_TEMPONLY)
918 			return (DLADM_STATUS_TEMPONLY);
919 		status = i_dladm_get_linkprop_db(handle, linkid, prop_name,
920 		    prop_val, val_cntp);
921 		break;
922 	default:
923 		status = DLADM_STATUS_BADARG;
924 		break;
925 	}
926 
927 	return (status);
928 }
929 
930 /*ARGSUSED*/
931 static int
932 i_dladm_init_one_prop(dladm_handle_t handle, datalink_id_t linkid,
933     const char *prop_name, void *arg)
934 {
935 	char	*buf, **propvals;
936 	uint_t	i, valcnt = DLADM_MAX_PROP_VALCNT;
937 
938 	if ((buf = malloc((sizeof (char *) + DLADM_PROP_VAL_MAX) *
939 	    DLADM_MAX_PROP_VALCNT)) == NULL) {
940 		return (DLADM_WALK_CONTINUE);
941 	}
942 
943 	propvals = (char **)(void *)buf;
944 	for (i = 0; i < valcnt; i++) {
945 		propvals[i] = buf +
946 		    sizeof (char *) * DLADM_MAX_PROP_VALCNT +
947 		    i * DLADM_PROP_VAL_MAX;
948 	}
949 
950 	if (dladm_get_linkprop(handle, linkid, DLADM_PROP_VAL_PERSISTENT,
951 	    prop_name, propvals, &valcnt) != DLADM_STATUS_OK) {
952 		goto done;
953 	}
954 
955 	(void) dladm_set_linkprop(handle, linkid, prop_name, propvals, valcnt,
956 	    DLADM_OPT_ACTIVE);
957 
958 done:
959 	if (buf != NULL)
960 		free(buf);
961 
962 	return (DLADM_WALK_CONTINUE);
963 }
964 
965 /*ARGSUSED*/
966 static int
967 i_dladm_init_linkprop(dladm_handle_t handle, datalink_id_t linkid, void *arg)
968 {
969 	datalink_class_t	class;
970 	dladm_status_t		status;
971 
972 	status = dladm_datalink_id2info(handle, linkid, NULL, &class, NULL,
973 	    NULL, 0);
974 	if (status != DLADM_STATUS_OK)
975 		return (DLADM_WALK_TERMINATE);
976 
977 	if ((class & (DATALINK_CLASS_VNIC | DATALINK_CLASS_VLAN)) == 0)
978 		(void) dladm_init_linkprop(handle, linkid, B_TRUE);
979 
980 	return (DLADM_WALK_CONTINUE);
981 }
982 
983 dladm_status_t
984 dladm_init_linkprop(dladm_handle_t handle, datalink_id_t linkid,
985     boolean_t any_media)
986 {
987 	datalink_media_t	dmedia;
988 	uint32_t		media;
989 
990 	dmedia = any_media ? DATALINK_ANY_MEDIATYPE : DL_WIFI;
991 
992 	if (linkid == DATALINK_ALL_LINKID) {
993 		(void) dladm_walk_datalink_id(i_dladm_init_linkprop, handle,
994 		    NULL, DATALINK_CLASS_ALL, dmedia, DLADM_OPT_PERSIST);
995 	} else if (any_media ||
996 	    ((dladm_datalink_id2info(handle, linkid, NULL, NULL, &media, NULL,
997 	    0) == DLADM_STATUS_OK) &&
998 	    DATALINK_MEDIA_ACCEPTED(dmedia, media))) {
999 		(void) dladm_walk_linkprop(handle, linkid, NULL,
1000 		    i_dladm_init_one_prop);
1001 	}
1002 	return (DLADM_STATUS_OK);
1003 }
1004 
1005 /* ARGSUSED */
1006 static dladm_status_t
1007 do_get_zone(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1008     char **prop_val, uint_t *val_cnt, datalink_media_t media,
1009     uint_t flags, uint_t *perm_flags)
1010 {
1011 	char			zone_name[ZONENAME_MAX];
1012 	zoneid_t		zid;
1013 	dladm_status_t		status;
1014 	char			*cp;
1015 	dld_ioc_macprop_t	*dip;
1016 
1017 	if (flags != 0)
1018 		return (DLADM_STATUS_NOTSUP);
1019 
1020 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
1021 	    &status, perm_flags);
1022 	if (status != DLADM_STATUS_OK)
1023 		return (status);
1024 
1025 	cp = dip->pr_val;
1026 	(void) memcpy(&zid, cp, sizeof (zid));
1027 	free(dip);
1028 
1029 	*val_cnt = 1;
1030 	if (zid != GLOBAL_ZONEID) {
1031 		if (getzonenamebyid(zid, zone_name, sizeof (zone_name)) < 0) {
1032 			return (dladm_errno2status(errno));
1033 		}
1034 
1035 		(void) strncpy(*prop_val, zone_name, DLADM_PROP_VAL_MAX);
1036 	} else {
1037 		*prop_val[0] = '\0';
1038 	}
1039 
1040 	return (DLADM_STATUS_OK);
1041 }
1042 
1043 typedef int (*zone_get_devroot_t)(char *, char *, size_t);
1044 
1045 static int
1046 i_dladm_get_zone_dev(char *zone_name, char *dev, size_t devlen)
1047 {
1048 	char			root[MAXPATHLEN];
1049 	zone_get_devroot_t	real_zone_get_devroot;
1050 	void			*dlhandle;
1051 	void			*sym;
1052 	int			ret;
1053 
1054 	if ((dlhandle = dlopen("libzonecfg.so.1", RTLD_LAZY)) == NULL)
1055 		return (-1);
1056 
1057 	if ((sym = dlsym(dlhandle, "zone_get_devroot")) == NULL) {
1058 		(void) dlclose(dlhandle);
1059 		return (-1);
1060 	}
1061 
1062 	real_zone_get_devroot = (zone_get_devroot_t)sym;
1063 
1064 	if ((ret = real_zone_get_devroot(zone_name, root, sizeof (root))) == 0)
1065 		(void) snprintf(dev, devlen, "%s%s", root, "/dev");
1066 	(void) dlclose(dlhandle);
1067 	return (ret);
1068 }
1069 
1070 static dladm_status_t
1071 i_dladm_update_deventry(dladm_handle_t handle, zoneid_t zid,
1072     datalink_id_t linkid, boolean_t add)
1073 {
1074 	char		path[MAXPATHLEN];
1075 	char		name[MAXLINKNAMELEN];
1076 	di_prof_t	prof = NULL;
1077 	char		zone_name[ZONENAME_MAX];
1078 	dladm_status_t	status;
1079 	int		ret;
1080 
1081 	if (getzonenamebyid(zid, zone_name, sizeof (zone_name)) < 0)
1082 		return (dladm_errno2status(errno));
1083 	if (i_dladm_get_zone_dev(zone_name, path, sizeof (path)) != 0)
1084 		return (dladm_errno2status(errno));
1085 	if (di_prof_init(path, &prof) != 0)
1086 		return (dladm_errno2status(errno));
1087 
1088 	status = dladm_linkid2legacyname(handle, linkid, name, MAXLINKNAMELEN);
1089 	if (status != DLADM_STATUS_OK)
1090 		goto cleanup;
1091 
1092 	if (add)
1093 		ret = di_prof_add_dev(prof, name);
1094 	else
1095 		ret = di_prof_add_exclude(prof, name);
1096 
1097 	if (ret != 0) {
1098 		status = dladm_errno2status(errno);
1099 		goto cleanup;
1100 	}
1101 
1102 	if (di_prof_commit(prof) != 0)
1103 		status = dladm_errno2status(errno);
1104 cleanup:
1105 	if (prof)
1106 		di_prof_fini(prof);
1107 
1108 	return (status);
1109 }
1110 
1111 /* ARGSUSED */
1112 static dladm_status_t
1113 do_set_zone(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1114     val_desc_t *vdp, uint_t val_cnt, uint_t flags, datalink_media_t media)
1115 {
1116 	dladm_status_t		status = DLADM_STATUS_OK;
1117 	zoneid_t		zid_old, zid_new;
1118 	char			link[MAXLINKNAMELEN];
1119 	char			*cp;
1120 	dld_ioc_macprop_t	*dip;
1121 	dld_ioc_zid_t		*dzp;
1122 
1123 	if (val_cnt != 1)
1124 		return (DLADM_STATUS_BADVALCNT);
1125 
1126 	dzp = (dld_ioc_zid_t *)vdp->vd_val;
1127 
1128 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
1129 	    &status, NULL);
1130 	if (status != DLADM_STATUS_OK)
1131 		return (status);
1132 
1133 	cp = dip->pr_val;
1134 	(void) memcpy(&zid_old, cp, sizeof (zid_old));
1135 	free(dip);
1136 
1137 	zid_new = dzp->diz_zid;
1138 	(void) strlcpy(link, dzp->diz_link, MAXLINKNAMELEN);
1139 
1140 	/* Do nothing if setting to current value */
1141 	if (zid_new == zid_old)
1142 		return (status);
1143 
1144 	if (zid_new != GLOBAL_ZONEID) {
1145 		/*
1146 		 * If the new zoneid is the global zone, we could destroy
1147 		 * the link (in the case of an implicitly-created VLAN) as a
1148 		 * result of setting the zoneid. In that case, we defer the
1149 		 * operation to the end of this function to avoid recreating
1150 		 * the VLAN and getting a different linkid during the rollback
1151 		 * if other operation fails.
1152 		 *
1153 		 * Otherwise, this operation will hold a reference to the
1154 		 * link and prevent a link renaming, so we need to do it
1155 		 * before other operations.
1156 		 */
1157 		status = i_dladm_set_public_prop(handle, pdp, linkid, vdp,
1158 		    val_cnt, flags, media);
1159 		if (status != DLADM_STATUS_OK)
1160 			return (status);
1161 	}
1162 
1163 	if (zid_old != GLOBAL_ZONEID) {
1164 		if (zone_remove_datalink(zid_old, link) != 0 &&
1165 		    errno != ENXIO) {
1166 			status = dladm_errno2status(errno);
1167 			goto rollback1;
1168 		}
1169 
1170 		/*
1171 		 * It is okay to fail to update the /dev entry (some
1172 		 * vanity-named links do not have a /dev entry).
1173 		 */
1174 		(void) i_dladm_update_deventry(handle, zid_old, linkid,
1175 		    B_FALSE);
1176 	}
1177 
1178 	if (zid_new != GLOBAL_ZONEID) {
1179 		if (zone_add_datalink(zid_new, link) != 0) {
1180 			status = dladm_errno2status(errno);
1181 			goto rollback2;
1182 		}
1183 
1184 		(void) i_dladm_update_deventry(handle, zid_new, linkid, B_TRUE);
1185 	} else {
1186 		status = i_dladm_set_public_prop(handle, pdp, linkid, vdp,
1187 		    val_cnt, flags, media);
1188 		if (status != DLADM_STATUS_OK)
1189 			goto rollback2;
1190 	}
1191 
1192 	return (DLADM_STATUS_OK);
1193 
1194 rollback2:
1195 	if (zid_old != GLOBAL_ZONEID)
1196 		(void) i_dladm_update_deventry(handle, zid_old, linkid, B_TRUE);
1197 	if (zid_old != GLOBAL_ZONEID)
1198 		(void) zone_add_datalink(zid_old, link);
1199 rollback1:
1200 	if (zid_new != GLOBAL_ZONEID) {
1201 		dzp->diz_zid = zid_old;
1202 		(void) i_dladm_set_public_prop(handle, pdp, linkid, vdp,
1203 		    val_cnt, flags, media);
1204 	}
1205 
1206 	return (status);
1207 }
1208 
1209 /* ARGSUSED */
1210 static dladm_status_t
1211 do_check_zone(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1212     char **prop_val, uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1213 {
1214 	char		*zone_name;
1215 	char		linkname[MAXLINKNAMELEN];
1216 	zoneid_t	zoneid;
1217 	dladm_status_t	status = DLADM_STATUS_OK;
1218 	dld_ioc_zid_t	*dzp;
1219 
1220 	if (val_cnt != 1)
1221 		return (DLADM_STATUS_BADVALCNT);
1222 
1223 	dzp = malloc(sizeof (dld_ioc_zid_t));
1224 	if (dzp == NULL)
1225 		return (DLADM_STATUS_NOMEM);
1226 
1227 	if ((status = dladm_datalink_id2info(handle, linkid, NULL, NULL, NULL,
1228 	    linkname, MAXLINKNAMELEN)) != DLADM_STATUS_OK) {
1229 		goto done;
1230 	}
1231 
1232 	zone_name = (prop_val != NULL) ? *prop_val : GLOBAL_ZONENAME;
1233 	if (strlen(linkname) > MAXLINKNAMELEN) {
1234 		status = DLADM_STATUS_BADVAL;
1235 		goto done;
1236 	}
1237 
1238 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
1239 		status = DLADM_STATUS_BADVAL;
1240 		goto done;
1241 	}
1242 
1243 	if (zoneid != GLOBAL_ZONEID) {
1244 		ushort_t	flags;
1245 
1246 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
1247 		    sizeof (flags)) < 0) {
1248 			status = dladm_errno2status(errno);
1249 			goto done;
1250 		}
1251 
1252 		if (!(flags & ZF_NET_EXCL)) {
1253 			status = DLADM_STATUS_BADVAL;
1254 			goto done;
1255 		}
1256 	}
1257 
1258 	(void) memset(dzp, 0, sizeof (dld_ioc_zid_t));
1259 
1260 	dzp->diz_zid = zoneid;
1261 	(void) strlcpy(dzp->diz_link, linkname, MAXLINKNAMELEN);
1262 
1263 	vdp->vd_val = (uintptr_t)dzp;
1264 	return (DLADM_STATUS_OK);
1265 done:
1266 	free(dzp);
1267 	return (status);
1268 }
1269 
1270 /* ARGSUSED */
1271 static dladm_status_t
1272 i_dladm_maxbw_get(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1273     char **prop_val, uint_t *val_cnt, datalink_media_t media,
1274     uint_t flags, uint_t *perm_flags)
1275 {
1276 	dld_ioc_macprop_t	*dip;
1277 	mac_resource_props_t	mrp;
1278 	dladm_status_t		status;
1279 
1280 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
1281 	    &status, perm_flags);
1282 	if (dip == NULL)
1283 		return (status);
1284 
1285 	bcopy(dip->pr_val, &mrp, sizeof (mac_resource_props_t));
1286 	free(dip);
1287 
1288 	if ((mrp.mrp_mask & MRP_MAXBW) == 0) {
1289 		(*prop_val)[0] = '\0';
1290 	} else {
1291 		(void) dladm_bw2str(mrp.mrp_maxbw, prop_val[0]);
1292 	}
1293 	*val_cnt = 1;
1294 	return (DLADM_STATUS_OK);
1295 }
1296 
1297 /* ARGSUSED */
1298 static dladm_status_t
1299 do_check_maxbw(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1300     char **prop_val, uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1301 {
1302 	uint64_t	*maxbw;
1303 	dladm_status_t	status = DLADM_STATUS_OK;
1304 
1305 	if (val_cnt != 1)
1306 		return (DLADM_STATUS_BADVALCNT);
1307 
1308 	maxbw = malloc(sizeof (uint64_t));
1309 	if (maxbw == NULL)
1310 		return (DLADM_STATUS_NOMEM);
1311 
1312 	status = dladm_str2bw(*prop_val, maxbw);
1313 	if (status != DLADM_STATUS_OK) {
1314 		free(maxbw);
1315 		return (status);
1316 	}
1317 
1318 	if ((*maxbw < MRP_MAXBW_MINVAL) && (*maxbw != 0)) {
1319 		free(maxbw);
1320 		return (DLADM_STATUS_MINMAXBW);
1321 	}
1322 
1323 	vdp->vd_val = (uintptr_t)maxbw;
1324 	return (DLADM_STATUS_OK);
1325 }
1326 
1327 /* ARGSUSED */
1328 dladm_status_t
1329 do_extract_maxbw(val_desc_t *vdp, void *arg, uint_t cnt)
1330 {
1331 	mac_resource_props_t *mrp = (mac_resource_props_t *)arg;
1332 
1333 	bcopy((char *)vdp->vd_val, &mrp->mrp_maxbw, sizeof (uint64_t));
1334 	mrp->mrp_mask |= MRP_MAXBW;
1335 
1336 	return (DLADM_STATUS_OK);
1337 }
1338 
1339 /* ARGSUSED */
1340 static dladm_status_t
1341 i_dladm_cpus_get(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1342     char **prop_val, uint_t *val_cnt, datalink_media_t media,
1343     uint_t flags, uint_t *perm_flags)
1344 {
1345 	dld_ioc_macprop_t	*dip;
1346 	mac_resource_props_t	mrp;
1347 	int			i;
1348 	uint32_t		ncpus;
1349 	uchar_t			*cp;
1350 	dladm_status_t		status;
1351 
1352 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
1353 	    &status, perm_flags);
1354 	if (dip == NULL)
1355 		return (status);
1356 
1357 	cp = (uchar_t *)dip->pr_val;
1358 	(void) memcpy(&mrp, cp, sizeof (mac_resource_props_t));
1359 	free(dip);
1360 
1361 	ncpus = mrp.mrp_ncpus;
1362 
1363 	if (ncpus > *val_cnt)
1364 		return (DLADM_STATUS_TOOSMALL);
1365 
1366 	if (ncpus == 0) {
1367 		(*prop_val)[0] = '\0';
1368 		*val_cnt = 1;
1369 		return (DLADM_STATUS_OK);
1370 	}
1371 
1372 	*val_cnt = ncpus;
1373 	for (i = 0; i < ncpus; i++) {
1374 		(void) snprintf(prop_val[i], DLADM_PROP_VAL_MAX,
1375 		    "%u", mrp.mrp_cpu[i]);
1376 	}
1377 	return (DLADM_STATUS_OK);
1378 }
1379 
1380 /* ARGSUSED */
1381 static dladm_status_t
1382 do_set_res(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1383     val_desc_t *vdp, uint_t val_cnt, uint_t flags, datalink_media_t media)
1384 {
1385 	mac_resource_props_t	mrp;
1386 	dladm_status_t		status = DLADM_STATUS_OK;
1387 	dld_ioc_macprop_t	*dip;
1388 
1389 	bzero(&mrp, sizeof (mac_resource_props_t));
1390 	dip = i_dladm_buf_alloc_by_name(0, linkid, pdp->pd_name,
1391 	    flags, &status);
1392 
1393 	if (dip == NULL)
1394 		return (status);
1395 
1396 	if (vdp->vd_val == RESET_VAL) {
1397 		switch (dip->pr_num) {
1398 		case MAC_PROP_MAXBW:
1399 			mrp.mrp_maxbw = MRP_MAXBW_RESETVAL;
1400 			mrp.mrp_mask = MRP_MAXBW;
1401 			break;
1402 		case MAC_PROP_PRIO:
1403 			mrp.mrp_priority = MPL_RESET;
1404 			mrp.mrp_mask = MRP_PRIORITY;
1405 			break;
1406 		default:
1407 			free(dip);
1408 			return (DLADM_STATUS_BADARG);
1409 		}
1410 	} else {
1411 		switch (dip->pr_num) {
1412 		case MAC_PROP_MAXBW:
1413 			bcopy((void *)vdp->vd_val, &mrp.mrp_maxbw,
1414 			    sizeof (uint64_t));
1415 			mrp.mrp_mask = MRP_MAXBW;
1416 			break;
1417 		case MAC_PROP_PRIO:
1418 			bcopy((void *)vdp->vd_val, &mrp.mrp_priority,
1419 			    sizeof (mac_priority_level_t));
1420 			mrp.mrp_mask = MRP_PRIORITY;
1421 			break;
1422 		default:
1423 			free(dip);
1424 			return (DLADM_STATUS_BADARG);
1425 		}
1426 	}
1427 
1428 	(void) memcpy(dip->pr_val, &mrp, dip->pr_valsize);
1429 	status = i_dladm_macprop(handle, dip, B_TRUE);
1430 	free(dip);
1431 	return (status);
1432 }
1433 
1434 /* ARGSUSED */
1435 static dladm_status_t
1436 do_set_cpus(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1437     val_desc_t *vdp, uint_t val_cnt, uint_t flags, datalink_media_t media)
1438 {
1439 	mac_resource_props_t	mrp;
1440 	dladm_status_t		status;
1441 	dld_ioc_macprop_t	*dip;
1442 	datalink_class_t	class;
1443 
1444 	/*
1445 	 * CPU bindings can be set on VNIC and regular physical links.
1446 	 * However VNICs fails the dladm_phys_info test(). So apply
1447 	 * the phys_info test only on physical links.
1448 	 */
1449 	if ((status = dladm_datalink_id2info(handle, linkid, NULL, &class,
1450 	    NULL, NULL, 0)) != DLADM_STATUS_OK) {
1451 		return (status);
1452 	}
1453 
1454 	/*
1455 	 * We set intr_cpu to -1. The interrupt will be retargetted,
1456 	 * if possible when the setup is complete in MAC.
1457 	 */
1458 	bzero(&mrp, sizeof (mac_resource_props_t));
1459 	mrp.mrp_mask = MRP_CPUS;
1460 	if (vdp != NULL && vdp->vd_val != RESET_VAL) {
1461 		mac_resource_props_t	*vmrp;
1462 
1463 		vmrp = (mac_resource_props_t *)vdp->vd_val;
1464 		if (vmrp->mrp_ncpus > 0) {
1465 			bcopy(vmrp, &mrp, sizeof (mac_resource_props_t));
1466 			mrp.mrp_mask = MRP_CPUS;
1467 		}
1468 		mrp.mrp_mask |= MRP_CPUS_USERSPEC;
1469 		mrp.mrp_fanout_mode = MCM_CPUS;
1470 		mrp.mrp_intr_cpu = -1;
1471 	}
1472 
1473 	dip = i_dladm_buf_alloc_by_name(0, linkid, pdp->pd_name,
1474 	    flags, &status);
1475 	if (dip == NULL)
1476 		return (status);
1477 
1478 	(void) memcpy(dip->pr_val, &mrp, dip->pr_valsize);
1479 	status = i_dladm_macprop(handle, dip, B_TRUE);
1480 	free(dip);
1481 	return (status);
1482 }
1483 
1484 /* ARGSUSED */
1485 static dladm_status_t
1486 do_check_cpus(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1487     char **prop_val, uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1488 {
1489 	uint32_t		cpuid;
1490 	int			i, j, rc;
1491 	long			nproc = sysconf(_SC_NPROCESSORS_CONF);
1492 	mac_resource_props_t	*mrp;
1493 
1494 	mrp = malloc(sizeof (mac_resource_props_t));
1495 	if (mrp == NULL)
1496 		return (DLADM_STATUS_NOMEM);
1497 
1498 	for (i = 0; i < val_cnt; i++) {
1499 		errno = 0;
1500 		cpuid = strtol(prop_val[i], (char **)NULL, 10);
1501 		if (errno != 0 || cpuid >= nproc) {
1502 			free(mrp);
1503 			return (DLADM_STATUS_CPUMAX);
1504 		}
1505 		rc = p_online(cpuid, P_STATUS);
1506 		if (rc < 1) {
1507 			free(mrp);
1508 			return (DLADM_STATUS_CPUERR);
1509 		}
1510 		if (rc != P_ONLINE) {
1511 			free(mrp);
1512 			return (DLADM_STATUS_CPUNOTONLINE);
1513 		}
1514 		mrp->mrp_cpu[i] = cpuid;
1515 	}
1516 	mrp->mrp_ncpus = (uint32_t)val_cnt;
1517 
1518 	/* Check for duplicates */
1519 	for (i = 0; i < val_cnt; i++) {
1520 		for (j = 0; j < val_cnt; j++) {
1521 			if (i != j && mrp->mrp_cpu[i] == mrp->mrp_cpu[j]) {
1522 				free(mrp);
1523 				return (DLADM_STATUS_BADARG);
1524 			}
1525 		}
1526 	}
1527 	vdp->vd_val = (uintptr_t)mrp;
1528 
1529 	return (DLADM_STATUS_OK);
1530 }
1531 
1532 /* ARGSUSED */
1533 dladm_status_t
1534 do_extract_cpus(val_desc_t *vdp, void *arg, uint_t cnt)
1535 {
1536 	mac_resource_props_t	*mrp = (mac_resource_props_t *)arg;
1537 	mac_resource_props_t	*vmrp = (mac_resource_props_t *)vdp->vd_val;
1538 	int			i;
1539 
1540 	for (i = 0; i < vmrp->mrp_ncpus; i++) {
1541 		mrp->mrp_cpu[i] = vmrp->mrp_cpu[i];
1542 	}
1543 	mrp->mrp_ncpus = vmrp->mrp_ncpus;
1544 	mrp->mrp_mask |= (MRP_CPUS|MRP_CPUS_USERSPEC);
1545 	mrp->mrp_fanout_mode = MCM_CPUS;
1546 	mrp->mrp_intr_cpu = -1;
1547 
1548 	return (DLADM_STATUS_OK);
1549 }
1550 
1551 /* ARGSUSED */
1552 static dladm_status_t
1553 i_dladm_priority_get(dladm_handle_t handle, prop_desc_t *pdp,
1554     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
1555     datalink_media_t media, uint_t flags, uint_t *perm_flags)
1556 {
1557 	dld_ioc_macprop_t	*dip;
1558 	mac_resource_props_t	mrp;
1559 	mac_priority_level_t	pri;
1560 	dladm_status_t		status;
1561 
1562 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
1563 	    &status, perm_flags);
1564 	if (dip == NULL)
1565 		return (status);
1566 
1567 	bcopy(dip->pr_val, &mrp, sizeof (mac_resource_props_t));
1568 	free(dip);
1569 
1570 	pri = ((mrp.mrp_mask & MRP_PRIORITY) == 0) ? MPL_HIGH :
1571 	    mrp.mrp_priority;
1572 
1573 	(void) dladm_pri2str(pri, prop_val[0]);
1574 	*val_cnt = 1;
1575 	return (DLADM_STATUS_OK);
1576 }
1577 
1578 /* ARGSUSED */
1579 static dladm_status_t
1580 do_check_priority(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1581     char **prop_val, uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1582 {
1583 	mac_priority_level_t	*pri;
1584 	dladm_status_t	status = DLADM_STATUS_OK;
1585 
1586 	if (val_cnt != 1)
1587 		return (DLADM_STATUS_BADVALCNT);
1588 
1589 	pri = malloc(sizeof (mac_priority_level_t));
1590 	if (pri == NULL)
1591 		return (DLADM_STATUS_NOMEM);
1592 
1593 	status = dladm_str2pri(*prop_val, pri);
1594 	if (status != DLADM_STATUS_OK) {
1595 		free(pri);
1596 		return (status);
1597 	}
1598 
1599 	if (*pri < MPL_LOW || *pri > MPL_HIGH) {
1600 		free(pri);
1601 		return (DLADM_STATUS_BADVAL);
1602 	}
1603 
1604 	vdp->vd_val = (uintptr_t)pri;
1605 	return (DLADM_STATUS_OK);
1606 }
1607 
1608 /* ARGSUSED */
1609 dladm_status_t
1610 do_extract_priority(val_desc_t *vdp, void *arg, uint_t cnt)
1611 {
1612 	mac_resource_props_t *mrp = (mac_resource_props_t *)arg;
1613 
1614 	bcopy((char *)vdp->vd_val, &mrp->mrp_priority,
1615 	    sizeof (mac_priority_level_t));
1616 	mrp->mrp_mask |= MRP_PRIORITY;
1617 
1618 	return (DLADM_STATUS_OK);
1619 }
1620 
1621 /* ARGSUSED */
1622 static dladm_status_t
1623 do_get_autopush(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1624     char **prop_val, uint_t *val_cnt, datalink_media_t media,
1625     uint_t flags, uint_t *perm_flags)
1626 {
1627 	struct		dlautopush dlap;
1628 	int		i, len;
1629 	dladm_status_t	status;
1630 	dld_ioc_macprop_t	*dip;
1631 
1632 	if (flags & MAC_PROP_DEFAULT)
1633 		return (DLADM_STATUS_NOTDEFINED);
1634 
1635 	*val_cnt = 1;
1636 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
1637 	    &status, perm_flags);
1638 	if (dip == NULL) {
1639 		(*prop_val)[0] = '\0';
1640 		return (DLADM_STATUS_OK);
1641 	}
1642 	(void) memcpy(&dlap, dip->pr_val, sizeof (dlap));
1643 
1644 	for (i = 0, len = 0; i < dlap.dap_npush; i++) {
1645 		if (i != 0) {
1646 			(void) snprintf(*prop_val + len,
1647 			    DLADM_PROP_VAL_MAX - len, "%c", AP_DELIMITER);
1648 			len += 1;
1649 		}
1650 		(void) snprintf(*prop_val + len, DLADM_PROP_VAL_MAX - len,
1651 		    "%s", dlap.dap_aplist[i]);
1652 		len += strlen(dlap.dap_aplist[i]);
1653 		if (dlap.dap_anchor - 1 == i) {
1654 			(void) snprintf(*prop_val + len,
1655 			    DLADM_PROP_VAL_MAX - len, "%c%s", AP_DELIMITER,
1656 			    AP_ANCHOR);
1657 			len += (strlen(AP_ANCHOR) + 1);
1658 		}
1659 	}
1660 	free(dip);
1661 done:
1662 	return (DLADM_STATUS_OK);
1663 }
1664 
1665 /*
1666  * Add the specified module to the dlautopush structure; returns a
1667  * DLADM_STATUS_* code.
1668  */
1669 dladm_status_t
1670 i_dladm_add_ap_module(const char *module, struct dlautopush *dlap)
1671 {
1672 	if ((strlen(module) == 0) || (strlen(module) > FMNAMESZ))
1673 		return (DLADM_STATUS_BADVAL);
1674 
1675 	if (strncasecmp(module, AP_ANCHOR, strlen(AP_ANCHOR)) == 0) {
1676 		/*
1677 		 * We don't allow multiple anchors, and the anchor must
1678 		 * be after at least one module.
1679 		 */
1680 		if (dlap->dap_anchor != 0)
1681 			return (DLADM_STATUS_BADVAL);
1682 		if (dlap->dap_npush == 0)
1683 			return (DLADM_STATUS_BADVAL);
1684 
1685 		dlap->dap_anchor = dlap->dap_npush;
1686 		return (DLADM_STATUS_OK);
1687 	}
1688 	if (dlap->dap_npush >= MAXAPUSH)
1689 		return (DLADM_STATUS_BADVALCNT);
1690 
1691 	(void) strlcpy(dlap->dap_aplist[dlap->dap_npush++], module,
1692 	    FMNAMESZ + 1);
1693 
1694 	return (DLADM_STATUS_OK);
1695 }
1696 
1697 /*
1698  * Currently, both '.' and ' '(space) can be used as the delimiters between
1699  * autopush modules. The former is used in dladm set-linkprop, and the
1700  * latter is used in the autopush(1M) file.
1701  */
1702 /* ARGSUSED */
1703 static dladm_status_t
1704 do_check_autopush(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1705     char **prop_val, uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1706 {
1707 	char			*module;
1708 	struct dlautopush	*dlap;
1709 	dladm_status_t		status;
1710 	char			val[DLADM_PROP_VAL_MAX];
1711 	char			delimiters[4];
1712 
1713 	if (val_cnt != 1)
1714 		return (DLADM_STATUS_BADVALCNT);
1715 
1716 	if (prop_val != NULL) {
1717 		dlap = malloc(sizeof (struct dlautopush));
1718 		if (dlap == NULL)
1719 			return (DLADM_STATUS_NOMEM);
1720 
1721 		(void) memset(dlap, 0, sizeof (struct dlautopush));
1722 		(void) snprintf(delimiters, 4, " %c\n", AP_DELIMITER);
1723 		bcopy(*prop_val, val, DLADM_PROP_VAL_MAX);
1724 		module = strtok(val, delimiters);
1725 		while (module != NULL) {
1726 			status = i_dladm_add_ap_module(module, dlap);
1727 			if (status != DLADM_STATUS_OK)
1728 				return (status);
1729 			module = strtok(NULL, delimiters);
1730 		}
1731 
1732 		vdp->vd_val = (uintptr_t)dlap;
1733 	} else {
1734 		vdp->vd_val = 0;
1735 	}
1736 	return (DLADM_STATUS_OK);
1737 }
1738 
1739 #define	WLDP_BUFSIZE (MAX_BUF_LEN - WIFI_BUF_OFFSET)
1740 
1741 /* ARGSUSED */
1742 static dladm_status_t
1743 do_get_rate_common(dladm_handle_t handle, prop_desc_t *pdp,
1744     datalink_id_t linkid, char **prop_val, uint_t *val_cnt, uint_t id,
1745     uint_t *perm_flags)
1746 {
1747 	wl_rates_t	*wrp;
1748 	uint_t		i;
1749 	dladm_status_t	status = DLADM_STATUS_OK;
1750 
1751 	wrp = malloc(WLDP_BUFSIZE);
1752 	if (wrp == NULL)
1753 		return (DLADM_STATUS_NOMEM);
1754 
1755 	status = i_dladm_wlan_param(handle, linkid, wrp, id, WLDP_BUFSIZE,
1756 	    B_FALSE);
1757 	if (status != DLADM_STATUS_OK)
1758 		goto done;
1759 
1760 	if (wrp->wl_rates_num > *val_cnt) {
1761 		status = DLADM_STATUS_TOOSMALL;
1762 		goto done;
1763 	}
1764 
1765 	if (wrp->wl_rates_rates[0] == 0) {
1766 		prop_val[0][0] = '\0';
1767 		*val_cnt = 1;
1768 		goto done;
1769 	}
1770 
1771 	for (i = 0; i < wrp->wl_rates_num; i++) {
1772 		(void) snprintf(prop_val[i], DLADM_STRSIZE, "%.*f",
1773 		    wrp->wl_rates_rates[i] % 2,
1774 		    (float)wrp->wl_rates_rates[i] / 2);
1775 	}
1776 	*val_cnt = wrp->wl_rates_num;
1777 	*perm_flags = MAC_PROP_PERM_RW;
1778 
1779 done:
1780 	free(wrp);
1781 	return (status);
1782 }
1783 
1784 static dladm_status_t
1785 do_get_rate_prop(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1786     char **prop_val, uint_t *val_cnt, datalink_media_t media,
1787     uint_t flags, uint_t *perm_flags)
1788 {
1789 	if (media != DL_WIFI) {
1790 		return (i_dladm_speed_get(handle, pdp, linkid, prop_val,
1791 		    val_cnt, flags, perm_flags));
1792 	}
1793 
1794 	return (do_get_rate_common(handle, pdp, linkid, prop_val, val_cnt,
1795 	    MAC_PROP_WL_DESIRED_RATES, perm_flags));
1796 }
1797 
1798 /* ARGSUSED */
1799 static dladm_status_t
1800 do_get_rate_mod(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1801     char **prop_val, uint_t *val_cnt, datalink_media_t media,
1802     uint_t flags, uint_t *perm_flags)
1803 {
1804 	switch (media) {
1805 	case DL_ETHER:
1806 		/*
1807 		 * Speed for ethernet links is unbounded. E.g., 802.11b
1808 		 * links can have a speed of 5.5 Gbps.
1809 		 */
1810 		return (DLADM_STATUS_NOTSUP);
1811 
1812 	case DL_WIFI:
1813 		return (do_get_rate_common(handle, pdp, linkid, prop_val,
1814 		    val_cnt, MAC_PROP_WL_SUPPORTED_RATES, perm_flags));
1815 	default:
1816 		return (DLADM_STATUS_BADARG);
1817 	}
1818 }
1819 
1820 static dladm_status_t
1821 do_set_rate(dladm_handle_t handle, datalink_id_t linkid,
1822     dladm_wlan_rates_t *rates)
1823 {
1824 	int		i;
1825 	uint_t		len;
1826 	wl_rates_t	*wrp;
1827 	dladm_status_t	status = DLADM_STATUS_OK;
1828 
1829 	wrp = malloc(WLDP_BUFSIZE);
1830 	if (wrp == NULL)
1831 		return (DLADM_STATUS_NOMEM);
1832 
1833 	bzero(wrp, WLDP_BUFSIZE);
1834 	for (i = 0; i < rates->wr_cnt; i++)
1835 		wrp->wl_rates_rates[i] = rates->wr_rates[i];
1836 	wrp->wl_rates_num = rates->wr_cnt;
1837 
1838 	len = offsetof(wl_rates_t, wl_rates_rates) +
1839 	    (rates->wr_cnt * sizeof (char)) + WIFI_BUF_OFFSET;
1840 	status = i_dladm_wlan_param(handle, linkid, wrp,
1841 	    MAC_PROP_WL_DESIRED_RATES, len, B_TRUE);
1842 
1843 	free(wrp);
1844 	return (status);
1845 }
1846 
1847 /* ARGSUSED */
1848 static dladm_status_t
1849 do_set_rate_prop(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1850     val_desc_t *vdp, uint_t val_cnt, uint_t flags, datalink_media_t media)
1851 {
1852 	dladm_wlan_rates_t	rates;
1853 	dladm_status_t		status;
1854 
1855 	/*
1856 	 * can currently set rate on WIFI links only.
1857 	 */
1858 	if (media != DL_WIFI)
1859 		return (DLADM_STATUS_PROPRDONLY);
1860 
1861 	if (val_cnt != 1)
1862 		return (DLADM_STATUS_BADVALCNT);
1863 
1864 	rates.wr_cnt = 1;
1865 	rates.wr_rates[0] = vdp[0].vd_val;
1866 
1867 	status = do_set_rate(handle, linkid, &rates);
1868 
1869 done:
1870 	return (status);
1871 }
1872 
1873 /* ARGSUSED */
1874 static dladm_status_t
1875 do_check_rate(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
1876     char **prop_val, uint_t val_cnt, val_desc_t *vdp, datalink_media_t media)
1877 {
1878 	int		i;
1879 	uint_t		modval_cnt = MAX_SUPPORT_RATES;
1880 	char		*buf, **modval;
1881 	dladm_status_t	status;
1882 	uint_t 		perm_flags;
1883 
1884 	if (val_cnt != 1)
1885 		return (DLADM_STATUS_BADVALCNT);
1886 
1887 	buf = malloc((sizeof (char *) + DLADM_STRSIZE) *
1888 	    MAX_SUPPORT_RATES);
1889 	if (buf == NULL) {
1890 		status = DLADM_STATUS_NOMEM;
1891 		goto done;
1892 	}
1893 
1894 	modval = (char **)(void *)buf;
1895 	for (i = 0; i < MAX_SUPPORT_RATES; i++) {
1896 		modval[i] = buf + sizeof (char *) * MAX_SUPPORT_RATES +
1897 		    i * DLADM_STRSIZE;
1898 	}
1899 
1900 	status = do_get_rate_mod(handle, NULL, linkid, modval, &modval_cnt,
1901 	    media, 0, &perm_flags);
1902 	if (status != DLADM_STATUS_OK)
1903 		goto done;
1904 
1905 	for (i = 0; i < modval_cnt; i++) {
1906 		if (strcasecmp(*prop_val, modval[i]) == 0) {
1907 			vdp->vd_val = (uintptr_t)(uint_t)
1908 			    (atof(*prop_val) * 2);
1909 			status = DLADM_STATUS_OK;
1910 			break;
1911 		}
1912 	}
1913 	if (i == modval_cnt)
1914 		status = DLADM_STATUS_BADVAL;
1915 done:
1916 	free(buf);
1917 	return (status);
1918 }
1919 
1920 static dladm_status_t
1921 do_get_phyconf(dladm_handle_t handle, datalink_id_t linkid, void *buf,
1922     int buflen)
1923 {
1924 	return (i_dladm_wlan_param(handle, linkid, buf, MAC_PROP_WL_PHY_CONFIG,
1925 	    buflen, B_FALSE));
1926 }
1927 
1928 /* ARGSUSED */
1929 static dladm_status_t
1930 do_get_channel_prop(dladm_handle_t handle, prop_desc_t *pdp,
1931     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
1932     datalink_media_t media, uint_t flags, uint_t *perm_flags)
1933 {
1934 	uint32_t	channel;
1935 	char		buf[WLDP_BUFSIZE];
1936 	dladm_status_t	status = DLADM_STATUS_OK;
1937 	wl_phy_conf_t	wl_phy_conf;
1938 
1939 	if ((status = do_get_phyconf(handle, linkid, buf, sizeof (buf)))
1940 	    != DLADM_STATUS_OK)
1941 		goto done;
1942 
1943 	(void) memcpy(&wl_phy_conf, buf, sizeof (wl_phy_conf));
1944 	if (!i_dladm_wlan_convert_chan(&wl_phy_conf, &channel)) {
1945 		status = DLADM_STATUS_NOTFOUND;
1946 		goto done;
1947 	}
1948 
1949 	(void) snprintf(*prop_val, DLADM_STRSIZE, "%u", channel);
1950 	*val_cnt = 1;
1951 	*perm_flags = MAC_PROP_PERM_READ;
1952 done:
1953 	return (status);
1954 }
1955 
1956 static dladm_status_t
1957 do_get_powermode(dladm_handle_t handle, datalink_id_t linkid, void *buf,
1958     int buflen)
1959 {
1960 	return (i_dladm_wlan_param(handle, linkid, buf, MAC_PROP_WL_POWER_MODE,
1961 	    buflen, B_FALSE));
1962 }
1963 
1964 /* ARGSUSED */
1965 static dladm_status_t
1966 do_get_powermode_prop(dladm_handle_t handle, prop_desc_t *pdp,
1967     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
1968     datalink_media_t media, uint_t flags, uint_t *perm_flags)
1969 {
1970 	wl_ps_mode_t	mode;
1971 	const char	*s;
1972 	char		buf[WLDP_BUFSIZE];
1973 	dladm_status_t	status = DLADM_STATUS_OK;
1974 
1975 	if ((status = do_get_powermode(handle, linkid, buf, sizeof (buf)))
1976 	    != DLADM_STATUS_OK)
1977 		goto done;
1978 
1979 	(void) memcpy(&mode, buf, sizeof (mode));
1980 	switch (mode.wl_ps_mode) {
1981 	case WL_PM_AM:
1982 		s = "off";
1983 		break;
1984 	case WL_PM_MPS:
1985 		s = "max";
1986 		break;
1987 	case WL_PM_FAST:
1988 		s = "fast";
1989 		break;
1990 	default:
1991 		status = DLADM_STATUS_NOTFOUND;
1992 		goto done;
1993 	}
1994 	(void) snprintf(*prop_val, DLADM_STRSIZE, "%s", s);
1995 	*val_cnt = 1;
1996 	*perm_flags = MAC_PROP_PERM_RW;
1997 done:
1998 	return (status);
1999 }
2000 
2001 static dladm_status_t
2002 do_set_powermode(dladm_handle_t handle, datalink_id_t linkid,
2003     dladm_wlan_powermode_t *pm)
2004 {
2005 	wl_ps_mode_t    ps_mode;
2006 
2007 	(void) memset(&ps_mode, 0xff, sizeof (ps_mode));
2008 
2009 	switch (*pm) {
2010 	case DLADM_WLAN_PM_OFF:
2011 		ps_mode.wl_ps_mode = WL_PM_AM;
2012 		break;
2013 	case DLADM_WLAN_PM_MAX:
2014 		ps_mode.wl_ps_mode = WL_PM_MPS;
2015 		break;
2016 	case DLADM_WLAN_PM_FAST:
2017 		ps_mode.wl_ps_mode = WL_PM_FAST;
2018 		break;
2019 	default:
2020 		return (DLADM_STATUS_NOTSUP);
2021 	}
2022 	return (i_dladm_wlan_param(handle, linkid, &ps_mode,
2023 	    MAC_PROP_WL_POWER_MODE, sizeof (ps_mode), B_TRUE));
2024 }
2025 
2026 /* ARGSUSED */
2027 static dladm_status_t
2028 do_set_powermode_prop(dladm_handle_t handle, prop_desc_t *pdp,
2029     datalink_id_t linkid, val_desc_t *vdp, uint_t val_cnt, uint_t flags,
2030     datalink_media_t media)
2031 {
2032 	dladm_wlan_powermode_t powermode = (dladm_wlan_powermode_t)vdp->vd_val;
2033 	dladm_status_t status;
2034 
2035 	if (val_cnt != 1)
2036 		return (DLADM_STATUS_BADVALCNT);
2037 
2038 	status = do_set_powermode(handle, linkid, &powermode);
2039 
2040 	return (status);
2041 }
2042 
2043 static dladm_status_t
2044 do_get_radio(dladm_handle_t handle, datalink_id_t linkid, void *buf, int buflen)
2045 {
2046 	return (i_dladm_wlan_param(handle, linkid, buf, MAC_PROP_WL_RADIO,
2047 	    buflen, B_FALSE));
2048 }
2049 
2050 /* ARGSUSED */
2051 static dladm_status_t
2052 do_get_radio_prop(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
2053     char **prop_val, uint_t *val_cnt, datalink_media_t media,
2054     uint_t flags, uint_t *perm_flags)
2055 {
2056 	wl_radio_t	radio;
2057 	const char	*s;
2058 	char		buf[WLDP_BUFSIZE];
2059 	dladm_status_t	status = DLADM_STATUS_OK;
2060 
2061 	if ((status = do_get_radio(handle, linkid, buf, sizeof (buf)))
2062 	    != DLADM_STATUS_OK)
2063 		goto done;
2064 
2065 	(void) memcpy(&radio, buf, sizeof (radio));
2066 	switch (radio) {
2067 	case B_TRUE:
2068 		s = "on";
2069 		break;
2070 	case B_FALSE:
2071 		s = "off";
2072 		break;
2073 	default:
2074 		status = DLADM_STATUS_NOTFOUND;
2075 		goto done;
2076 	}
2077 	(void) snprintf(*prop_val, DLADM_STRSIZE, "%s", s);
2078 	*val_cnt = 1;
2079 	*perm_flags = MAC_PROP_PERM_RW;
2080 done:
2081 	return (status);
2082 }
2083 
2084 static dladm_status_t
2085 do_set_radio(dladm_handle_t handle, datalink_id_t linkid,
2086     dladm_wlan_radio_t *radio)
2087 {
2088 	wl_radio_t r;
2089 
2090 	switch (*radio) {
2091 	case DLADM_WLAN_RADIO_ON:
2092 		r = B_TRUE;
2093 		break;
2094 	case DLADM_WLAN_RADIO_OFF:
2095 		r = B_FALSE;
2096 		break;
2097 	default:
2098 		return (DLADM_STATUS_NOTSUP);
2099 	}
2100 	return (i_dladm_wlan_param(handle, linkid, &r, MAC_PROP_WL_RADIO,
2101 	    sizeof (r), B_TRUE));
2102 }
2103 
2104 /* ARGSUSED */
2105 static dladm_status_t
2106 do_set_radio_prop(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
2107     val_desc_t *vdp, uint_t val_cnt, uint_t fags, datalink_media_t media)
2108 {
2109 	dladm_wlan_radio_t radio = (dladm_wlan_radio_t)vdp->vd_val;
2110 	dladm_status_t status;
2111 
2112 	if (val_cnt != 1)
2113 		return (DLADM_STATUS_BADVALCNT);
2114 
2115 	status = do_set_radio(handle, linkid, &radio);
2116 
2117 	return (status);
2118 }
2119 
2120 static dladm_status_t
2121 i_dladm_set_linkprop_db(dladm_handle_t handle, datalink_id_t linkid,
2122     const char *prop_name, char **prop_val, uint_t val_cnt)
2123 {
2124 	char		buf[MAXLINELEN];
2125 	int		i;
2126 	dladm_conf_t	conf;
2127 	dladm_status_t	status;
2128 
2129 	status = dladm_read_conf(handle, linkid, &conf);
2130 	if (status != DLADM_STATUS_OK)
2131 		return (status);
2132 
2133 	/*
2134 	 * reset case.
2135 	 */
2136 	if (val_cnt == 0) {
2137 		status = dladm_unset_conf_field(handle, conf, prop_name);
2138 		if (status == DLADM_STATUS_OK)
2139 			status = dladm_write_conf(handle, conf);
2140 		goto done;
2141 	}
2142 
2143 	buf[0] = '\0';
2144 	for (i = 0; i < val_cnt; i++) {
2145 		(void) strlcat(buf, prop_val[i], MAXLINELEN);
2146 		if (i != val_cnt - 1)
2147 			(void) strlcat(buf, ",", MAXLINELEN);
2148 	}
2149 
2150 	status = dladm_set_conf_field(handle, conf, prop_name, DLADM_TYPE_STR,
2151 	    buf);
2152 	if (status == DLADM_STATUS_OK)
2153 		status = dladm_write_conf(handle, conf);
2154 
2155 done:
2156 	dladm_destroy_conf(handle, conf);
2157 	return (status);
2158 }
2159 
2160 static dladm_status_t
2161 i_dladm_get_linkprop_db(dladm_handle_t handle, datalink_id_t linkid,
2162     const char *prop_name, char **prop_val, uint_t *val_cntp)
2163 {
2164 	char		buf[MAXLINELEN], *str;
2165 	uint_t		cnt = 0;
2166 	dladm_conf_t	conf;
2167 	dladm_status_t	status;
2168 
2169 	status = dladm_read_conf(handle, linkid, &conf);
2170 	if (status != DLADM_STATUS_OK)
2171 		return (status);
2172 
2173 	status = dladm_get_conf_field(handle, conf, prop_name, buf, MAXLINELEN);
2174 	if (status != DLADM_STATUS_OK)
2175 		goto done;
2176 
2177 	str = strtok(buf, ",");
2178 	while (str != NULL) {
2179 		if (cnt == *val_cntp) {
2180 			status = DLADM_STATUS_TOOSMALL;
2181 			goto done;
2182 		}
2183 		(void) strlcpy(prop_val[cnt++], str, DLADM_PROP_VAL_MAX);
2184 		str = strtok(NULL, ",");
2185 	}
2186 
2187 	*val_cntp = cnt;
2188 
2189 done:
2190 	dladm_destroy_conf(handle, conf);
2191 	return (status);
2192 }
2193 
2194 /*
2195  * Walk persistent private link properties of a link.
2196  */
2197 static dladm_status_t
2198 i_dladm_walk_linkprop_priv_db(dladm_handle_t handle, datalink_id_t linkid,
2199     void *arg, int (*func)(dladm_handle_t, datalink_id_t, const char *, void *))
2200 {
2201 	dladm_status_t		status;
2202 	dladm_conf_t		conf;
2203 	char			last_attr[MAXLINKATTRLEN];
2204 	char			attr[MAXLINKATTRLEN];
2205 	char			attrval[MAXLINKATTRVALLEN];
2206 	size_t			attrsz;
2207 
2208 	if (linkid == DATALINK_INVALID_LINKID || func == NULL)
2209 		return (DLADM_STATUS_BADARG);
2210 
2211 	status = dladm_read_conf(handle, linkid, &conf);
2212 	if (status != DLADM_STATUS_OK)
2213 		return (status);
2214 
2215 	last_attr[0] = '\0';
2216 	while ((status = dladm_getnext_conf_linkprop(handle, conf, last_attr,
2217 	    attr, attrval, MAXLINKATTRVALLEN, &attrsz)) == DLADM_STATUS_OK) {
2218 		if (attr[0] == '_') {
2219 			if (func(handle, linkid, attr, arg) ==
2220 			    DLADM_WALK_TERMINATE)
2221 				break;
2222 		}
2223 		(void) strlcpy(last_attr, attr, MAXLINKATTRLEN);
2224 	}
2225 
2226 	dladm_destroy_conf(handle, conf);
2227 	return (DLADM_STATUS_OK);
2228 }
2229 
2230 static link_attr_t *
2231 dladm_name2prop(const char *prop_name)
2232 {
2233 	link_attr_t *p;
2234 
2235 	for (p = link_attr; p->pp_id != MAC_PROP_PRIVATE; p++) {
2236 		if (strcmp(p->pp_name, prop_name) == 0)
2237 			break;
2238 	}
2239 	return (p);
2240 }
2241 
2242 static link_attr_t *
2243 dladm_id2prop(mac_prop_id_t propid)
2244 {
2245 	link_attr_t *p;
2246 
2247 	for (p = link_attr; p->pp_id != MAC_PROP_PRIVATE; p++) {
2248 		if (p->pp_id == propid)
2249 			break;
2250 	}
2251 	return (p);
2252 }
2253 
2254 static dld_ioc_macprop_t *
2255 i_dladm_buf_alloc_impl(size_t valsize, datalink_id_t linkid,
2256     const char *prop_name, mac_prop_id_t propid, uint_t flags,
2257     dladm_status_t *status)
2258 {
2259 	int dsize;
2260 	dld_ioc_macprop_t *dip;
2261 
2262 	*status = DLADM_STATUS_OK;
2263 	dsize = MAC_PROP_BUFSIZE(valsize);
2264 	dip = malloc(dsize);
2265 	if (dip == NULL) {
2266 		*status = DLADM_STATUS_NOMEM;
2267 		return (NULL);
2268 	}
2269 	bzero(dip, dsize);
2270 	dip->pr_valsize = valsize;
2271 	(void) strlcpy(dip->pr_name, prop_name, sizeof (dip->pr_name));
2272 	dip->pr_version = MAC_PROP_VERSION;
2273 	dip->pr_linkid = linkid;
2274 	dip->pr_num = propid;
2275 	dip->pr_flags = flags;
2276 	return (dip);
2277 }
2278 
2279 static dld_ioc_macprop_t *
2280 i_dladm_buf_alloc_by_name(size_t valsize, datalink_id_t linkid,
2281     const char *prop_name, uint_t flags, dladm_status_t *status)
2282 {
2283 	link_attr_t *p;
2284 
2285 	p = dladm_name2prop(prop_name);
2286 	valsize = MAX(p->pp_valsize, valsize);
2287 	return (i_dladm_buf_alloc_impl(valsize, linkid, prop_name, p->pp_id,
2288 	    flags, status));
2289 }
2290 
2291 static dld_ioc_macprop_t *
2292 i_dladm_buf_alloc_by_id(size_t valsize, datalink_id_t linkid,
2293     mac_prop_id_t propid, uint_t flags, dladm_status_t *status)
2294 {
2295 	link_attr_t *p;
2296 
2297 	p = dladm_id2prop(propid);
2298 	valsize = MAX(p->pp_valsize, valsize);
2299 	return (i_dladm_buf_alloc_impl(valsize, linkid, p->pp_name, propid,
2300 	    flags, status));
2301 }
2302 
2303 /* ARGSUSED */
2304 static dladm_status_t
2305 i_dladm_set_public_prop(dladm_handle_t handle, prop_desc_t *pdp,
2306     datalink_id_t linkid, val_desc_t *vdp, uint_t val_cnt, uint_t flags,
2307     datalink_media_t media)
2308 {
2309 	dld_ioc_macprop_t	*dip;
2310 	dladm_status_t	status = DLADM_STATUS_OK;
2311 	uint8_t		u8;
2312 	uint16_t	u16;
2313 	uint32_t	u32;
2314 	void		*val;
2315 
2316 	dip = i_dladm_buf_alloc_by_name(0, linkid, pdp->pd_name, 0, &status);
2317 	if (dip == NULL)
2318 		return (status);
2319 
2320 	if (pdp->pd_flags & PD_CHECK_ALLOC)
2321 		val = (void *)vdp->vd_val;
2322 	else {
2323 		/*
2324 		 * Currently all 1/2/4-byte size properties are byte/word/int.
2325 		 * No need (yet) to distinguish these from arrays of same size.
2326 		 */
2327 		switch (dip->pr_valsize) {
2328 		case 1:
2329 			u8 = vdp->vd_val;
2330 			val = &u8;
2331 			break;
2332 		case 2:
2333 			u16 = vdp->vd_val;
2334 			val = &u16;
2335 			break;
2336 		case 4:
2337 			u32 = vdp->vd_val;
2338 			val = &u32;
2339 			break;
2340 		default:
2341 			val = &vdp->vd_val;
2342 			break;
2343 		}
2344 	}
2345 
2346 	if (val != NULL)
2347 		(void) memcpy(dip->pr_val, val, dip->pr_valsize);
2348 	else
2349 		dip->pr_valsize = 0;
2350 
2351 	status = i_dladm_macprop(handle, dip, B_TRUE);
2352 
2353 done:
2354 	free(dip);
2355 	return (status);
2356 }
2357 
2358 dladm_status_t
2359 i_dladm_macprop(dladm_handle_t handle, void *dip, boolean_t set)
2360 {
2361 	dladm_status_t status = DLADM_STATUS_OK;
2362 
2363 	if (ioctl(dladm_dld_fd(handle),
2364 	    (set ? DLDIOC_SETMACPROP : DLDIOC_GETMACPROP), dip))
2365 		status = dladm_errno2status(errno);
2366 
2367 	return (status);
2368 }
2369 
2370 static dld_ioc_macprop_t *
2371 i_dladm_get_public_prop(dladm_handle_t handle, datalink_id_t linkid,
2372     char *prop_name, uint_t flags, dladm_status_t *status, uint_t *perm_flags)
2373 {
2374 	dld_ioc_macprop_t *dip = NULL;
2375 
2376 	dip = i_dladm_buf_alloc_by_name(0, linkid, prop_name, flags, status);
2377 	if (dip == NULL)
2378 		return (NULL);
2379 
2380 	*status = i_dladm_macprop(handle, dip, B_FALSE);
2381 	if (*status != DLADM_STATUS_OK) {
2382 		free(dip);
2383 		return (NULL);
2384 	}
2385 	if (perm_flags != NULL)
2386 		*perm_flags = dip->pr_perm_flags;
2387 
2388 	return (dip);
2389 }
2390 
2391 /* ARGSUSED */
2392 static dladm_status_t
2393 i_dladm_defmtu_check(dladm_handle_t handle, prop_desc_t *pdp,
2394     datalink_id_t linkid, char **prop_val, uint_t val_cnt, val_desc_t *v,
2395     datalink_media_t media)
2396 {
2397 	if (val_cnt != 1)
2398 		return (DLADM_STATUS_BADVAL);
2399 	v->vd_val = atoi(prop_val[0]);
2400 	return (DLADM_STATUS_OK);
2401 }
2402 
2403 /* ARGSUSED */
2404 static dladm_status_t
2405 i_dladm_duplex_get(dladm_handle_t handle, prop_desc_t *pdp,
2406     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
2407     datalink_media_t media, uint_t flags, uint_t *perm_flags)
2408 {
2409 	link_duplex_t   link_duplex;
2410 	dladm_status_t  status;
2411 
2412 	if ((status = dladm_get_single_mac_stat(handle, linkid, "link_duplex",
2413 	    KSTAT_DATA_UINT32, &link_duplex)) != 0)
2414 		return (status);
2415 
2416 	switch (link_duplex) {
2417 	case LINK_DUPLEX_FULL:
2418 		(void) strcpy(*prop_val, "full");
2419 		break;
2420 	case LINK_DUPLEX_HALF:
2421 		(void) strcpy(*prop_val, "half");
2422 		break;
2423 	default:
2424 		(void) strcpy(*prop_val, "unknown");
2425 		break;
2426 	}
2427 	*val_cnt = 1;
2428 	return (DLADM_STATUS_OK);
2429 }
2430 
2431 /* ARGSUSED */
2432 static dladm_status_t
2433 i_dladm_speed_get(dladm_handle_t handle, prop_desc_t *pdp, datalink_id_t linkid,
2434     char **prop_val, uint_t *val_cnt, uint_t flags, uint_t *perm_flags)
2435 {
2436 	uint64_t	ifspeed = 0;
2437 	dladm_status_t status;
2438 
2439 	if ((status = dladm_get_single_mac_stat(handle, linkid, "ifspeed",
2440 	    KSTAT_DATA_UINT64, &ifspeed)) != 0)
2441 		return (status);
2442 
2443 	if ((ifspeed % 1000000) != 0) {
2444 		(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX,
2445 		    "%llf", ifspeed / (float)1000000); /* Mbps */
2446 	} else {
2447 		(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX,
2448 		    "%llu", ifspeed / 1000000); /* Mbps */
2449 	}
2450 	*val_cnt = 1;
2451 	*perm_flags = MAC_PROP_PERM_READ;
2452 	return (DLADM_STATUS_OK);
2453 }
2454 
2455 /* ARGSUSED */
2456 static dladm_status_t
2457 i_dladm_status_get(dladm_handle_t handle, prop_desc_t *pdp,
2458     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
2459     datalink_media_t media, uint_t flags, uint_t *perm_flags)
2460 {
2461 	link_state_t		link_state;
2462 	dladm_status_t		status;
2463 
2464 	status = i_dladm_get_state(handle, linkid, &link_state);
2465 	if (status != DLADM_STATUS_OK)
2466 		return (status);
2467 
2468 	switch (link_state) {
2469 	case LINK_STATE_UP:
2470 		(void) strcpy(*prop_val, "up");
2471 		break;
2472 	case LINK_STATE_DOWN:
2473 		(void) strcpy(*prop_val, "down");
2474 		break;
2475 	default:
2476 		(void) strcpy(*prop_val, "unknown");
2477 		break;
2478 	}
2479 	*val_cnt = 1;
2480 	*perm_flags = MAC_PROP_PERM_READ;
2481 	return (DLADM_STATUS_OK);
2482 }
2483 
2484 /* ARGSUSED */
2485 static dladm_status_t
2486 i_dladm_binary_get(dladm_handle_t handle, prop_desc_t *pdp,
2487     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
2488     datalink_media_t media, uint_t flags, uint_t *perm_flags)
2489 {
2490 	dld_ioc_macprop_t *dip;
2491 	dladm_status_t status;
2492 
2493 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
2494 	    &status, perm_flags);
2495 	if (dip == NULL)
2496 		return (status);
2497 
2498 	(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX, "%x", dip->pr_val[0]);
2499 	free(dip);
2500 	*val_cnt = 1;
2501 	return (DLADM_STATUS_OK);
2502 }
2503 
2504 /* ARGSUSED */
2505 static dladm_status_t
2506 i_dladm_uint32_get(dladm_handle_t handle, prop_desc_t *pdp,
2507     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
2508     datalink_media_t media, uint_t flags, uint_t *perm_flags)
2509 {
2510 	dld_ioc_macprop_t *dip;
2511 	uint32_t v = 0;
2512 	uchar_t *cp;
2513 	dladm_status_t status;
2514 
2515 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
2516 	    &status, perm_flags);
2517 	if (dip == NULL)
2518 		return (status);
2519 
2520 	cp = (uchar_t *)dip->pr_val;
2521 	(void) memcpy(&v, cp, sizeof (v));
2522 	(void) snprintf(*prop_val, DLADM_PROP_VAL_MAX, "%ld", v);
2523 	free(dip);
2524 	*val_cnt = 1;
2525 	return (DLADM_STATUS_OK);
2526 }
2527 
2528 /*
2529  * Determines the size of the structure that needs to be sent to drivers
2530  * for retrieving the property range values.
2531  */
2532 static int
2533 i_dladm_range_size(mac_propval_range_t *r, size_t *sz)
2534 {
2535 	uint_t count = r->mpr_count;
2536 
2537 	*sz = sizeof (mac_propval_range_t);
2538 	--count;
2539 
2540 	switch (r->mpr_type) {
2541 	case MAC_PROPVAL_UINT32:
2542 		*sz += (count * sizeof (mac_propval_uint32_range_t));
2543 		return (0);
2544 	default:
2545 		break;
2546 	}
2547 	*sz = 0;
2548 	return (EINVAL);
2549 }
2550 
2551 /* ARGSUSED */
2552 static dladm_status_t
2553 i_dladm_range_get(dladm_handle_t handle, prop_desc_t *pdp,
2554     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
2555     datalink_media_t media, uint_t flags, uint_t *perm_flags)
2556 {
2557 	dld_ioc_macprop_t *dip;
2558 	dladm_status_t status = DLADM_STATUS_OK;
2559 	size_t	sz;
2560 	mac_propval_range_t *rangep;
2561 
2562 	sz = sizeof (mac_propval_range_t);
2563 
2564 	/*
2565 	 * As caller we don't know number of value ranges, the driver
2566 	 * supports. To begin with we assume that number to be 1. If the
2567 	 * buffer size is insufficient, driver returns back with the
2568 	 * actual count of value ranges. See mac.h for more details.
2569 	 */
2570 retry:
2571 	if ((dip = i_dladm_buf_alloc_by_name(sz, linkid, pdp->pd_name, flags,
2572 	    &status)) == NULL)
2573 		return (status);
2574 
2575 	status = i_dladm_macprop(handle, dip, B_FALSE);
2576 	if (status != DLADM_STATUS_OK) {
2577 		if (status == DLADM_STATUS_TOOSMALL) {
2578 			int err;
2579 
2580 			rangep = (mac_propval_range_t *)(void *)&dip->pr_val;
2581 			if ((err = i_dladm_range_size(rangep, &sz)) == 0) {
2582 				free(dip);
2583 				goto retry;
2584 			} else {
2585 				status = dladm_errno2status(err);
2586 			}
2587 		}
2588 		free(dip);
2589 		return (status);
2590 	}
2591 	rangep = (mac_propval_range_t *)(void *)&dip->pr_val;
2592 
2593 	switch (rangep->mpr_type) {
2594 	case MAC_PROPVAL_UINT32: {
2595 		mac_propval_uint32_range_t *ur;
2596 		uint_t	count = rangep->mpr_count, i;
2597 
2598 		ur = &rangep->range_uint32[0];
2599 
2600 		for (i = 0; i < count; i++, ur++) {
2601 			if (ur->mpur_min == ur->mpur_max) {
2602 				(void) snprintf(prop_val[i], DLADM_PROP_VAL_MAX,
2603 				    "%ld", ur->mpur_min);
2604 			} else {
2605 				(void) snprintf(prop_val[i], DLADM_PROP_VAL_MAX,
2606 				    "%ld-%ld", ur->mpur_min, ur->mpur_max);
2607 			}
2608 		}
2609 		*val_cnt = count;
2610 		break;
2611 	}
2612 	default:
2613 		status = DLADM_STATUS_BADARG;
2614 		break;
2615 	}
2616 	free(dip);
2617 	return (status);
2618 }
2619 
2620 /* ARGSUSED */
2621 static dladm_status_t
2622 i_dladm_tagmode_get(dladm_handle_t handle, prop_desc_t *pdp,
2623     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
2624     datalink_media_t media, uint_t flags, uint_t *perm_flags)
2625 {
2626 	dld_ioc_macprop_t	*dip;
2627 	link_tagmode_t		mode;
2628 	dladm_status_t		status;
2629 
2630 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
2631 	    &status, perm_flags);
2632 	if (dip == NULL)
2633 		return (status);
2634 	(void) memcpy(&mode, dip->pr_val, sizeof (mode));
2635 	free(dip);
2636 
2637 	switch (mode) {
2638 	case LINK_TAGMODE_NORMAL:
2639 		(void) strlcpy(*prop_val, "normal", DLADM_PROP_VAL_MAX);
2640 		break;
2641 	case LINK_TAGMODE_VLANONLY:
2642 		(void) strlcpy(*prop_val, "vlanonly", DLADM_PROP_VAL_MAX);
2643 		break;
2644 	default:
2645 		(void) strlcpy(*prop_val, "unknown", DLADM_PROP_VAL_MAX);
2646 	}
2647 	*val_cnt = 1;
2648 	return (DLADM_STATUS_OK);
2649 }
2650 
2651 /* ARGSUSED */
2652 static dladm_status_t
2653 i_dladm_flowctl_get(dladm_handle_t handle, prop_desc_t *pdp,
2654     datalink_id_t linkid, char **prop_val, uint_t *val_cnt,
2655     datalink_media_t media, uint_t flags, uint_t *perm_flags)
2656 {
2657 	dld_ioc_macprop_t *dip;
2658 	link_flowctrl_t v;
2659 	dladm_status_t status;
2660 	uchar_t *cp;
2661 
2662 	dip = i_dladm_get_public_prop(handle, linkid, pdp->pd_name, flags,
2663 	    &status, perm_flags);
2664 	if (dip == NULL)
2665 		return (status);
2666 
2667 	cp = (uchar_t *)dip->pr_val;
2668 	(void) memcpy(&v, cp, sizeof (v));
2669 	switch (v) {
2670 	case LINK_FLOWCTRL_NONE:
2671 		(void) sprintf(*prop_val, "no");
2672 		break;
2673 	case LINK_FLOWCTRL_RX:
2674 		(void) sprintf(*prop_val, "rx");
2675 		break;
2676 	case LINK_FLOWCTRL_TX:
2677 		(void) sprintf(*prop_val, "tx");
2678 		break;
2679 	case LINK_FLOWCTRL_BI:
2680 		(void) sprintf(*prop_val, "bi");
2681 		break;
2682 	}
2683 	free(dip);
2684 	*val_cnt = 1;
2685 	return (DLADM_STATUS_OK);
2686 }
2687 
2688 
2689 /* ARGSUSED */
2690 static dladm_status_t
2691 i_dladm_set_prop(dladm_handle_t handle, datalink_id_t linkid,
2692     const char *prop_name, char **prop_val, uint_t val_cnt, uint_t flags)
2693 
2694 {
2695 	int		i, slen;
2696 	int 		bufsize = 0;
2697 	dld_ioc_macprop_t *dip = NULL;
2698 	uchar_t 	*dp;
2699 	link_attr_t *p;
2700 	dladm_status_t	status = DLADM_STATUS_OK;
2701 
2702 	if ((prop_name == NULL && prop_val != NULL) ||
2703 	    (prop_val != NULL && val_cnt == 0))
2704 		return (DLADM_STATUS_BADARG);
2705 	p = dladm_name2prop(prop_name);
2706 	if (p->pp_id != MAC_PROP_PRIVATE)
2707 		return (DLADM_STATUS_BADARG);
2708 
2709 	/*
2710 	 * private properties: all parsing is done in the kernel.
2711 	 * allocate a enough space for each property + its separator (',').
2712 	 */
2713 	for (i = 0; i < val_cnt; i++) {
2714 		bufsize += strlen(prop_val[i]) + 1;
2715 	}
2716 
2717 	if (prop_val == NULL) {
2718 		/*
2719 		 * getting default value. so use more buffer space.
2720 		 */
2721 		bufsize += DLADM_PROP_BUF_CHUNK;
2722 	}
2723 
2724 	dip = i_dladm_buf_alloc_by_name(bufsize + 1, linkid, prop_name,
2725 	    (prop_val != NULL ? 0 : MAC_PROP_DEFAULT), &status);
2726 	if (dip == NULL)
2727 		return (status);
2728 
2729 	dp = (uchar_t *)dip->pr_val;
2730 	slen = 0;
2731 
2732 	if (prop_val == NULL) {
2733 		status = i_dladm_macprop(handle, dip, B_FALSE);
2734 		dip->pr_flags = 0;
2735 	} else {
2736 		for (i = 0; i < val_cnt; i++) {
2737 			int plen = 0;
2738 
2739 			plen = strlen(prop_val[i]);
2740 			bcopy(prop_val[i], dp, plen);
2741 			slen += plen;
2742 			/*
2743 			 * add a "," separator and update dp.
2744 			 */
2745 			if (i != (val_cnt -1))
2746 				dp[slen++] = ',';
2747 			dp += (plen + 1);
2748 		}
2749 	}
2750 	if (status == DLADM_STATUS_OK)
2751 		status = i_dladm_macprop(handle, dip, B_TRUE);
2752 
2753 	free(dip);
2754 	return (status);
2755 }
2756 
2757 static dladm_status_t
2758 i_dladm_get_priv_prop(dladm_handle_t handle, datalink_id_t linkid,
2759     const char *prop_name, char **prop_val, uint_t *val_cnt,
2760     dladm_prop_type_t type, uint_t dld_flags)
2761 {
2762 	dladm_status_t	status = DLADM_STATUS_OK;
2763 	dld_ioc_macprop_t *dip = NULL;
2764 	link_attr_t *p;
2765 
2766 	if ((prop_name == NULL && prop_val != NULL) ||
2767 	    (prop_val != NULL && val_cnt == 0))
2768 		return (DLADM_STATUS_BADARG);
2769 
2770 	p = dladm_name2prop(prop_name);
2771 	if (p->pp_id != MAC_PROP_PRIVATE)
2772 		return (DLADM_STATUS_BADARG);
2773 
2774 	/*
2775 	 * private properties: all parsing is done in the kernel.
2776 	 */
2777 	dip = i_dladm_buf_alloc_by_name(DLADM_PROP_BUF_CHUNK, linkid, prop_name,
2778 	    dld_flags, &status);
2779 	if (dip == NULL)
2780 		return (status);
2781 
2782 	if ((status = i_dladm_macprop(handle, dip, B_FALSE)) ==
2783 	    DLADM_STATUS_OK) {
2784 		if (type == DLADM_PROP_VAL_PERM) {
2785 			(void) dladm_perm2str(dip->pr_perm_flags, *prop_val);
2786 		} else if (type == DLADM_PROP_VAL_MODIFIABLE) {
2787 			*prop_val[0] = '\0';
2788 		} else {
2789 			(void) strncpy(*prop_val, dip->pr_val,
2790 			    DLADM_PROP_VAL_MAX);
2791 		}
2792 		*val_cnt = 1;
2793 	} else if ((status == DLADM_STATUS_NOTSUP) &&
2794 	    (type == DLADM_PROP_VAL_CURRENT)) {
2795 		status = DLADM_STATUS_NOTFOUND;
2796 	}
2797 	free(dip);
2798 	return (status);
2799 }
2800 
2801 
2802 static dladm_status_t
2803 i_dladm_getset_defval(dladm_handle_t handle, prop_desc_t *pdp,
2804     datalink_id_t linkid, datalink_media_t media, uint_t flags)
2805 {
2806 	dladm_status_t status;
2807 	char **prop_vals = NULL, *buf;
2808 	size_t bufsize;
2809 	uint_t cnt;
2810 	int i;
2811 	uint_t perm_flags;
2812 
2813 	/*
2814 	 * Allocate buffer needed for prop_vals array. We can have at most
2815 	 * DLADM_MAX_PROP_VALCNT char *prop_vals[] entries, where
2816 	 * each entry has max size DLADM_PROP_VAL_MAX
2817 	 */
2818 	bufsize =
2819 	    (sizeof (char *) + DLADM_PROP_VAL_MAX) * DLADM_MAX_PROP_VALCNT;
2820 	buf = malloc(bufsize);
2821 	prop_vals = (char **)(void *)buf;
2822 	for (i = 0; i < DLADM_MAX_PROP_VALCNT; i++) {
2823 		prop_vals[i] = buf +
2824 		    sizeof (char *) * DLADM_MAX_PROP_VALCNT +
2825 		    i * DLADM_PROP_VAL_MAX;
2826 	}
2827 
2828 	/*
2829 	 * For properties which have pdp->pd_defval.vd_name as a non-empty
2830 	 * string, the "" itself is used to reset the property (exceptions
2831 	 * are zone and autopush, which populate vdp->vd_val). So
2832 	 * libdladm can copy pdp->pd_defval over to the val_desc_t passed
2833 	 * down on the setprop using the global values in the table. For
2834 	 * other cases (vd_name is ""), doing reset-linkprop will cause
2835 	 * libdladm to do a getprop to find the default value and then do
2836 	 * a setprop to reset the value to default.
2837 	 */
2838 	status = pdp->pd_get(handle, pdp, linkid, prop_vals, &cnt, media,
2839 	    MAC_PROP_DEFAULT, &perm_flags);
2840 	if (status == DLADM_STATUS_OK) {
2841 		if (perm_flags == MAC_PROP_PERM_RW) {
2842 			status = i_dladm_set_single_prop(handle, linkid,
2843 			    pdp->pd_class, media, pdp, prop_vals, cnt, flags);
2844 		}
2845 		else
2846 			status = DLADM_STATUS_NOTSUP;
2847 	}
2848 	free(buf);
2849 	return (status);
2850 }
2851 
2852 int
2853 macprop_to_wifi(mac_prop_id_t wl_prop)
2854 {
2855 	switch (wl_prop) {
2856 	case MAC_PROP_WL_ESSID:
2857 		return (WL_ESSID);
2858 	case MAC_PROP_WL_BSSID:
2859 		return (WL_BSSID);
2860 	case MAC_PROP_WL_BSSTYPE:
2861 		return (WL_BSS_TYPE);
2862 	case MAC_PROP_WL_LINKSTATUS:
2863 		return (WL_LINKSTATUS);
2864 	case MAC_PROP_WL_DESIRED_RATES:
2865 		return (WL_DESIRED_RATES);
2866 	case MAC_PROP_WL_SUPPORTED_RATES:
2867 		return (WL_SUPPORTED_RATES);
2868 	case MAC_PROP_WL_AUTH_MODE:
2869 		return (WL_AUTH_MODE);
2870 	case MAC_PROP_WL_ENCRYPTION:
2871 		return (WL_ENCRYPTION);
2872 	case MAC_PROP_WL_RSSI:
2873 		return (WL_RSSI);
2874 	case MAC_PROP_WL_PHY_CONFIG:
2875 		return (WL_PHY_CONFIG);
2876 	case MAC_PROP_WL_CAPABILITY:
2877 		return (WL_CAPABILITY);
2878 	case MAC_PROP_WL_WPA:
2879 		return (WL_WPA);
2880 	case MAC_PROP_WL_SCANRESULTS:
2881 		return (WL_SCANRESULTS);
2882 	case MAC_PROP_WL_POWER_MODE:
2883 		return (WL_POWER_MODE);
2884 	case MAC_PROP_WL_RADIO:
2885 		return (WL_RADIO);
2886 	case MAC_PROP_WL_ESS_LIST:
2887 		return (WL_ESS_LIST);
2888 	case MAC_PROP_WL_KEY_TAB:
2889 		return (WL_WEP_KEY_TAB);
2890 	case MAC_PROP_WL_CREATE_IBSS:
2891 		return (WL_CREATE_IBSS);
2892 	case MAC_PROP_WL_SETOPTIE:
2893 		return (WL_SETOPTIE);
2894 	case MAC_PROP_WL_DELKEY:
2895 		return (WL_DELKEY);
2896 	case MAC_PROP_WL_KEY:
2897 		return (WL_KEY);
2898 	case MAC_PROP_WL_MLME:
2899 		return (WL_MLME);
2900 	default:
2901 		return (-1);
2902 	}
2903 }
2904 
2905 dladm_status_t
2906 i_dladm_wlan_param(dladm_handle_t handle, datalink_id_t linkid, void *buf,
2907     mac_prop_id_t cmd, size_t len, boolean_t set)
2908 {
2909 	uint32_t		flags;
2910 	dladm_status_t		status;
2911 	uint32_t		media;
2912 	dld_ioc_macprop_t	*dip;
2913 	void			*dp;
2914 
2915 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, NULL,
2916 	    &media, NULL, 0)) != DLADM_STATUS_OK) {
2917 		return (status);
2918 	}
2919 
2920 	if (media != DL_WIFI)
2921 		return (DLADM_STATUS_BADARG);
2922 
2923 	if (!(flags & DLADM_OPT_ACTIVE))
2924 		return (DLADM_STATUS_TEMPONLY);
2925 
2926 	if (len == (MAX_BUF_LEN - WIFI_BUF_OFFSET))
2927 		len = MAX_BUF_LEN - sizeof (dld_ioc_macprop_t) - 1;
2928 
2929 	dip = i_dladm_buf_alloc_by_id(len, linkid, cmd, 0, &status);
2930 	if (dip == NULL)
2931 		return (DLADM_STATUS_NOMEM);
2932 
2933 	dp = (uchar_t *)dip->pr_val;
2934 	if (set)
2935 		(void) memcpy(dp, buf, len);
2936 
2937 	status = i_dladm_macprop(handle, dip, set);
2938 	if (status == DLADM_STATUS_NOTSUP) {
2939 		if (set) {
2940 			status = i_dladm_wlan_set_legacy_ioctl(handle, linkid,
2941 			    buf, len, macprop_to_wifi(cmd));
2942 		} else {
2943 			status = i_dladm_wlan_get_legacy_ioctl(handle, linkid,
2944 			    buf, len, macprop_to_wifi(cmd));
2945 		}
2946 	} else if (status == DLADM_STATUS_OK) {
2947 		if (!set)
2948 			(void) memcpy(buf, dp, len);
2949 	}
2950 
2951 	free(dip);
2952 	return (status);
2953 }
2954 
2955 static dladm_status_t
2956 i_dladm_wlan_get_legacy_ioctl(dladm_handle_t handle, datalink_id_t linkid,
2957     void *buf, uint_t buflen, uint_t id)
2958 {
2959 	wldp_t *gbuf;
2960 	dladm_status_t status;
2961 
2962 	if ((gbuf = malloc(MAX_BUF_LEN)) == NULL)
2963 		return (DLADM_STATUS_NOMEM);
2964 
2965 	(void) memset(gbuf, 0, MAX_BUF_LEN);
2966 	status = i_dladm_wlan_legacy_ioctl(handle, linkid, gbuf, id,
2967 	    MAX_BUF_LEN, WLAN_GET_PARAM, sizeof (wldp_t));
2968 	if (status == DLADM_STATUS_OK)
2969 		(void) memcpy(buf, gbuf->wldp_buf, buflen);
2970 
2971 	free(gbuf);
2972 	return (status);
2973 }
2974 
2975 static dladm_status_t
2976 i_dladm_wlan_set_legacy_ioctl(dladm_handle_t handle, datalink_id_t linkid,
2977     void *buf, uint_t buflen, uint_t id)
2978 {
2979 	wldp_t *gbuf;
2980 	dladm_status_t status = DLADM_STATUS_OK;
2981 
2982 	if ((gbuf = malloc(MAX_BUF_LEN)) == NULL)
2983 		return (DLADM_STATUS_NOMEM);
2984 
2985 	(void) memset(gbuf, 0, MAX_BUF_LEN);
2986 	(void) memcpy(gbuf->wldp_buf, buf, buflen);
2987 	buflen += WIFI_BUF_OFFSET;
2988 	status = i_dladm_wlan_legacy_ioctl(handle, linkid, gbuf, id, buflen,
2989 	    WLAN_SET_PARAM, buflen);
2990 
2991 	free(gbuf);
2992 	return (status);
2993 }
2994 
2995 dladm_status_t
2996 dladm_parse_link_props(char *str, dladm_arg_list_t **listp, boolean_t novalues)
2997 {
2998 	return (dladm_parse_args(str, listp, novalues));
2999 }
3000 
3001 /*
3002  * Retrieve the one link property from the database
3003  */
3004 /*ARGSUSED*/
3005 static int
3006 i_dladm_get_one_prop(dladm_handle_t handle, datalink_id_t linkid,
3007     const char *prop_name, void *arg)
3008 {
3009 	dladm_arg_list_t	*proplist = arg;
3010 	dladm_arg_info_t	*aip = NULL;
3011 
3012 	aip = &proplist->al_info[proplist->al_count];
3013 	/*
3014 	 * it is fine to point to prop_name since prop_name points to the
3015 	 * prop_table[n].pd_name.
3016 	 */
3017 	aip->ai_name = prop_name;
3018 
3019 	(void) dladm_get_linkprop(handle, linkid, DLADM_PROP_VAL_PERSISTENT,
3020 	    prop_name, aip->ai_val, &aip->ai_count);
3021 
3022 	if (aip->ai_count != 0)
3023 		proplist->al_count++;
3024 
3025 	return (DLADM_WALK_CONTINUE);
3026 }
3027 
3028 
3029 /*
3030  * Retrieve all link properties for a link from the database and
3031  * return a property list.
3032  */
3033 dladm_status_t
3034 dladm_link_get_proplist(dladm_handle_t handle, datalink_id_t linkid,
3035     dladm_arg_list_t **listp)
3036 {
3037 	dladm_arg_list_t	*list;
3038 	dladm_status_t		status = DLADM_STATUS_OK;
3039 
3040 	list = calloc(1, sizeof (dladm_arg_list_t));
3041 	if (list == NULL)
3042 		return (dladm_errno2status(errno));
3043 
3044 	status = dladm_walk_linkprop(handle, linkid, list,
3045 	    i_dladm_get_one_prop);
3046 
3047 	*listp = list;
3048 	return (status);
3049 }
3050 
3051 /*
3052  * Retrieve the named property from a proplist, check the value and
3053  * convert to a kernel structure.
3054  */
3055 static dladm_status_t
3056 i_dladm_link_proplist_extract_one(dladm_handle_t handle,
3057     dladm_arg_list_t *proplist, const char *name, void *val)
3058 {
3059 	dladm_status_t		status;
3060 	dladm_arg_info_t	*aip = NULL;
3061 	int			i, j;
3062 
3063 	/* Find named property in proplist */
3064 	for (i = 0; i < proplist->al_count; i++) {
3065 		aip = &proplist->al_info[i];
3066 		if (strcasecmp(aip->ai_name, name) == 0)
3067 			break;
3068 	}
3069 
3070 	/* Property not in list */
3071 	if (i == proplist->al_count)
3072 		return (DLADM_STATUS_OK);
3073 
3074 	for (i = 0; i < DLADM_MAX_PROPS; i++) {
3075 		prop_desc_t	*pdp = &prop_table[i];
3076 		val_desc_t	*vdp;
3077 
3078 		vdp = malloc(sizeof (val_desc_t) * aip->ai_count);
3079 		if (vdp == NULL)
3080 			return (DLADM_STATUS_NOMEM);
3081 
3082 		if (strcasecmp(aip->ai_name, pdp->pd_name) != 0)
3083 			continue;
3084 
3085 		if (aip->ai_val == NULL)
3086 			return (DLADM_STATUS_BADARG);
3087 
3088 		/* Check property value */
3089 		if (pdp->pd_check != NULL) {
3090 			status = pdp->pd_check(handle, pdp, 0, aip->ai_val,
3091 			    aip->ai_count, vdp, 0);
3092 		} else {
3093 			status = DLADM_STATUS_BADARG;
3094 		}
3095 
3096 		if (status != DLADM_STATUS_OK)
3097 			return (status);
3098 
3099 		for (j = 0; j < DLADM_MAX_RSRC_PROP; j++) {
3100 			resource_prop_t	*rpp = &rsrc_prop_table[j];
3101 
3102 			if (strcasecmp(aip->ai_name, rpp->rp_name) != 0)
3103 				continue;
3104 
3105 			/* Extract kernel structure */
3106 			if (rpp->rp_extract != NULL) {
3107 				status = rpp->rp_extract(vdp, val,
3108 				    aip->ai_count);
3109 			} else {
3110 				status = DLADM_STATUS_BADARG;
3111 			}
3112 			break;
3113 		}
3114 
3115 		if (status != DLADM_STATUS_OK)
3116 			return (status);
3117 
3118 		break;
3119 	}
3120 	return (status);
3121 }
3122 
3123 /*
3124  * Extract properties from a proplist and convert to mac_resource_props_t.
3125  */
3126 dladm_status_t
3127 dladm_link_proplist_extract(dladm_handle_t handle, dladm_arg_list_t *proplist,
3128     mac_resource_props_t *mrp)
3129 {
3130 	dladm_status_t	status = DLADM_STATUS_OK;
3131 
3132 	status = i_dladm_link_proplist_extract_one(handle, proplist, "maxbw",
3133 	    mrp);
3134 	if (status != DLADM_STATUS_OK)
3135 		return (status);
3136 	status = i_dladm_link_proplist_extract_one(handle, proplist, "priority",
3137 	    mrp);
3138 	if (status != DLADM_STATUS_OK)
3139 		return (status);
3140 	status = i_dladm_link_proplist_extract_one(handle, proplist, "cpus",
3141 	    mrp);
3142 	if (status != DLADM_STATUS_OK)
3143 		return (status);
3144 	return (status);
3145 }
3146 
3147 static const char *
3148 dladm_perm2str(uint_t perm, char *buf)
3149 {
3150 	(void) snprintf(buf, DLADM_STRSIZE, "%c%c",
3151 	    ((perm & MAC_PROP_PERM_READ) != 0) ? 'r' : '-',
3152 	    ((perm & MAC_PROP_PERM_WRITE) != 0) ? 'w' : '-');
3153 	return (buf);
3154 }
3155 
3156 dladm_status_t
3157 i_dladm_get_state(dladm_handle_t handle, datalink_id_t linkid,
3158     link_state_t *state)
3159 {
3160 	dld_ioc_macprop_t	*dip;
3161 	dladm_status_t		status;
3162 	uint_t			perms;
3163 
3164 	dip = i_dladm_get_public_prop(handle, linkid, "state", 0, &status,
3165 	    &perms);
3166 	if (status != DLADM_STATUS_OK)
3167 		return (status);
3168 	(void) memcpy(state, dip->pr_val, sizeof (*state));
3169 	free(dip);
3170 	return (status);
3171 }
3172 
3173 boolean_t
3174 dladm_attr_is_linkprop(const char *name)
3175 {
3176 	/* non-property attribute names */
3177 	const char *nonprop[] = {
3178 		/* dlmgmtd core attributes */
3179 		"name",
3180 		"class",
3181 		"media",
3182 		FPHYMAJ,
3183 		FPHYINST,
3184 		FDEVNAME,
3185 
3186 		/* other attributes for vlan, aggr, etc */
3187 		DLADM_ATTR_NAMES
3188 	};
3189 	boolean_t	is_nonprop = B_FALSE;
3190 	int		i;
3191 
3192 	for (i = 0; i < sizeof (nonprop) / sizeof (nonprop[0]); i++) {
3193 		if (strcmp(name, nonprop[i]) == 0) {
3194 			is_nonprop = B_TRUE;
3195 			break;
3196 		}
3197 	}
3198 
3199 	return (!is_nonprop);
3200 }
3201