xref: /illumos-gate/usr/src/cmd/dladm/dladm.c (revision 076ad4c710ebdb269f6341db447a83b5781f0b05)
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 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2016 Nexenta Systems, Inc.
25  * Copyright (c) 2015 Joyent, Inc. All rights reserved.
26  * Copyright 2020 Peter Tribble.
27  * Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
28  * Copyright 2021 RackTop Systems, Inc.
29  */
30 
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <dlfcn.h>
34 #include <locale.h>
35 #include <signal.h>
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #include <fcntl.h>
39 #include <string.h>
40 #include <stropts.h>
41 #include <sys/stat.h>
42 #include <errno.h>
43 #include <kstat.h>
44 #include <strings.h>
45 #include <getopt.h>
46 #include <unistd.h>
47 #include <priv.h>
48 #include <limits.h>
49 #include <termios.h>
50 #include <pwd.h>
51 #include <auth_attr.h>
52 #include <auth_list.h>
53 #include <libintl.h>
54 #include <libdevinfo.h>
55 #include <libdlpi.h>
56 #include <libdladm.h>
57 #include <libdllink.h>
58 #include <libdlstat.h>
59 #include <libdlaggr.h>
60 #include <libdlwlan.h>
61 #include <libdlvlan.h>
62 #include <libdlvnic.h>
63 #include <libdlib.h>
64 #include <libdlether.h>
65 #include <libdliptun.h>
66 #include <libdlsim.h>
67 #include <libdlbridge.h>
68 #include <libdloverlay.h>
69 #include <libinetutil.h>
70 #include <libvrrpadm.h>
71 #include <bsm/adt.h>
72 #include <bsm/adt_event.h>
73 #include <libdlvnic.h>
74 #include <sys/types.h>
75 #include <sys/socket.h>
76 #include <sys/ib/ib_types.h>
77 #include <sys/processor.h>
78 #include <netinet/in.h>
79 #include <arpa/inet.h>
80 #include <net/if_types.h>
81 #include <stddef.h>
82 #include <stp_in.h>
83 #include <ofmt.h>
84 #include <libcustr.h>
85 
86 #define	MAXPORT			256
87 #define	MAXVNIC			256
88 #define	BUFLEN(lim, ptr)	(((lim) > (ptr)) ? ((lim) - (ptr)) : 0)
89 #define	MAXLINELEN		1024
90 #define	SMF_UPGRADE_FILE		"/var/svc/profile/upgrade"
91 #define	SMF_UPGRADEDATALINK_FILE	"/var/svc/profile/upgrade_datalink"
92 #define	SMF_DLADM_UPGRADE_MSG		" # added by dladm(8)"
93 #define	DLADM_DEFAULT_COL	80
94 
95 /*
96  * used by the wifi show-* commands to set up ofmt_field_t structures.
97  */
98 #define	WIFI_CMD_SCAN		0x00000001
99 #define	WIFI_CMD_SHOW		0x00000002
100 #define	WIFI_CMD_ALL		(WIFI_CMD_SCAN | WIFI_CMD_SHOW)
101 
102 /* No larger than pktsum_t */
103 typedef struct brsum_s {
104 	uint64_t	drops;
105 	uint64_t	forward_dir;
106 	uint64_t	forward_mb;
107 	uint64_t	forward_unk;
108 	uint64_t	recv;
109 	uint64_t	sent;
110 } brsum_t;
111 
112 /* No larger than pktsum_t */
113 typedef struct brlsum_s {
114 	uint32_t	cfgbpdu;
115 	uint32_t	tcnbpdu;
116 	uint32_t	rstpbpdu;
117 	uint32_t	txbpdu;
118 	uint64_t	drops;
119 	uint64_t	recv;
120 	uint64_t	xmit;
121 } brlsum_t;
122 
123 typedef struct show_state {
124 	boolean_t	ls_firstonly;
125 	boolean_t	ls_donefirst;
126 	pktsum_t	ls_prevstats;
127 	uint32_t	ls_flags;
128 	dladm_status_t	ls_status;
129 	ofmt_handle_t	ls_ofmt;
130 	boolean_t	ls_parsable;
131 	boolean_t	ls_mac;
132 	boolean_t	ls_hwgrp;
133 } show_state_t;
134 
135 typedef struct show_grp_state {
136 	pktsum_t	gs_prevstats[MAXPORT];
137 	uint32_t	gs_flags;
138 	dladm_status_t	gs_status;
139 	boolean_t	gs_parsable;
140 	boolean_t	gs_lacp;
141 	boolean_t	gs_extended;
142 	boolean_t	gs_stats;
143 	boolean_t	gs_firstonly;
144 	boolean_t	gs_donefirst;
145 	ofmt_handle_t	gs_ofmt;
146 } show_grp_state_t;
147 
148 typedef struct show_vnic_state {
149 	datalink_id_t	vs_vnic_id;
150 	datalink_id_t	vs_link_id;
151 	char		vs_vnic[MAXLINKNAMELEN];
152 	char		vs_link[MAXLINKNAMELEN];
153 	boolean_t	vs_parsable;
154 	boolean_t	vs_found;
155 	boolean_t	vs_firstonly;
156 	boolean_t	vs_donefirst;
157 	boolean_t	vs_stats;
158 	boolean_t	vs_printstats;
159 	pktsum_t	vs_totalstats;
160 	pktsum_t	vs_prevstats[MAXVNIC];
161 	boolean_t	vs_etherstub;
162 	dladm_status_t	vs_status;
163 	uint32_t	vs_flags;
164 	ofmt_handle_t	vs_ofmt;
165 } show_vnic_state_t;
166 
167 typedef struct show_part_state {
168 	datalink_id_t	ps_over_id;
169 	char		ps_part[MAXLINKNAMELEN];
170 	boolean_t	ps_parsable;
171 	boolean_t	ps_found;
172 	dladm_status_t	ps_status;
173 	uint32_t	ps_flags;
174 	ofmt_handle_t	ps_ofmt;
175 } show_part_state_t;
176 
177 typedef struct show_ib_state {
178 	datalink_id_t	is_link_id;
179 	char		is_link[MAXLINKNAMELEN];
180 	boolean_t	is_parsable;
181 	dladm_status_t	is_status;
182 	uint32_t	is_flags;
183 	ofmt_handle_t	is_ofmt;
184 } show_ib_state_t;
185 
186 typedef struct show_usage_state_s {
187 	boolean_t	us_plot;
188 	boolean_t	us_parsable;
189 	boolean_t	us_printheader;
190 	boolean_t	us_first;
191 	boolean_t	us_showall;
192 	ofmt_handle_t	us_ofmt;
193 } show_usage_state_t;
194 
195 typedef struct show_overlay_request_s {
196 	boolean_t	sor_failed;
197 	ofmt_handle_t	sor_ofmt;
198 } show_overlay_request_t;
199 
200 /*
201  * callback functions for printing output and error diagnostics.
202  */
203 static ofmt_cb_t print_default_cb, print_link_stats_cb, print_linkprop_cb;
204 static ofmt_cb_t print_lacp_cb, print_phys_one_mac_cb;
205 static ofmt_cb_t print_xaggr_cb, print_aggr_stats_cb;
206 static ofmt_cb_t print_phys_one_hwgrp_cb, print_wlan_attr_cb;
207 static ofmt_cb_t print_wifi_status_cb, print_link_attr_cb;
208 static ofmt_cb_t print_overlay_cb, print_overlay_fma_cb, print_overlay_targ_cb;
209 
210 typedef void cmdfunc_t(int, char **, const char *);
211 
212 static cmdfunc_t do_help;
213 static cmdfunc_t do_show_link, do_show_wifi, do_show_phys;
214 static cmdfunc_t do_create_aggr, do_delete_aggr, do_add_aggr, do_remove_aggr;
215 static cmdfunc_t do_modify_aggr, do_show_aggr, do_up_aggr;
216 static cmdfunc_t do_scan_wifi, do_connect_wifi, do_disconnect_wifi;
217 static cmdfunc_t do_show_linkprop, do_set_linkprop, do_reset_linkprop;
218 static cmdfunc_t do_create_secobj, do_delete_secobj, do_show_secobj;
219 static cmdfunc_t do_init_linkprop, do_init_secobj;
220 static cmdfunc_t do_create_vlan, do_delete_vlan, do_up_vlan, do_show_vlan;
221 static cmdfunc_t do_rename_link, do_delete_phys, do_init_phys;
222 static cmdfunc_t do_show_linkmap;
223 static cmdfunc_t do_show_ether;
224 static cmdfunc_t do_create_vnic, do_delete_vnic, do_show_vnic;
225 static cmdfunc_t do_up_vnic;
226 static cmdfunc_t do_create_part, do_delete_part, do_show_part, do_show_ib;
227 static cmdfunc_t do_up_part;
228 static cmdfunc_t do_create_etherstub, do_delete_etherstub, do_show_etherstub;
229 static cmdfunc_t do_create_simnet, do_modify_simnet;
230 static cmdfunc_t do_delete_simnet, do_show_simnet, do_up_simnet;
231 static cmdfunc_t do_show_usage;
232 static cmdfunc_t do_create_bridge, do_modify_bridge, do_delete_bridge;
233 static cmdfunc_t do_add_bridge, do_remove_bridge, do_show_bridge;
234 static cmdfunc_t do_create_iptun, do_modify_iptun, do_delete_iptun;
235 static cmdfunc_t do_show_iptun, do_up_iptun, do_down_iptun;
236 static cmdfunc_t do_create_overlay, do_delete_overlay, do_modify_overlay;
237 static cmdfunc_t do_show_overlay, do_up_overlay;
238 
239 static void	do_up_vnic_common(int, char **, const char *, boolean_t);
240 
241 static int show_part(dladm_handle_t, datalink_id_t, void *);
242 
243 static void	altroot_cmd(char *, int, char **);
244 static int	show_linkprop_onelink(dladm_handle_t, datalink_id_t, void *);
245 
246 static void	link_stats(datalink_id_t, uint_t, char *, show_state_t *);
247 static void	aggr_stats(datalink_id_t, show_grp_state_t *, uint_t);
248 static void	vnic_stats(show_vnic_state_t *, uint32_t);
249 
250 static int	get_one_kstat(const char *, const char *, uint8_t,
251 		    void *, boolean_t);
252 static void	get_mac_stats(const char *, pktsum_t *);
253 static void	get_link_stats(const char *, pktsum_t *);
254 static uint64_t	get_ifspeed(const char *, boolean_t);
255 static const char	*get_linkstate(const char *, boolean_t, char *);
256 static const char	*get_linkduplex(const char *, boolean_t, char *);
257 
258 static iptun_type_t	iptun_gettypebyname(char *);
259 static const char	*iptun_gettypebyvalue(iptun_type_t);
260 static dladm_status_t	print_iptun(dladm_handle_t, datalink_id_t,
261 			    show_state_t *);
262 static int	print_iptun_walker(dladm_handle_t, datalink_id_t, void *);
263 
264 static int	show_etherprop(dladm_handle_t, datalink_id_t, void *);
265 static void	show_ether_xprop(void *, dladm_ether_info_t *);
266 static boolean_t	link_is_ether(const char *, datalink_id_t *);
267 
268 static boolean_t str2int(const char *, int *);
269 static void	die(const char *, ...);
270 static void	die_optdup(int);
271 static void	die_opterr(int, int, const char *);
272 static void	die_dlerr(dladm_status_t, const char *, ...);
273 static void	die_dlerrlist(dladm_status_t, dladm_errlist_t *,
274     const char *, ...);
275 static void	warn(const char *, ...);
276 static void	warn_dlerr(dladm_status_t, const char *, ...);
277 static void	warn_dlerrlist(dladm_errlist_t *);
278 
279 typedef struct	cmd {
280 	char		*c_name;
281 	cmdfunc_t	*c_fn;
282 	const char	*c_usage;
283 } cmd_t;
284 
285 static cmd_t	cmds[] = {
286 	{ "help",		do_help,		NULL		},
287 	{ "rename-link",	do_rename_link,
288 	    "    rename-link      <oldlink> <newlink>"			},
289 	{ "show-link",		do_show_link,
290 	    "    show-link        [-pP] [-o <field>,..] [-s [-i <interval>]] "
291 	    "[<link>]\n"						},
292 	{ "create-aggr",	do_create_aggr,
293 	    "    create-aggr      [-t] [-P <policy>] [-L <mode>] [-T <time>] "
294 	    "[-u <address>]\n"
295 	    "\t\t     -l <link> [-l <link>...] <link>"			},
296 	{ "delete-aggr",	do_delete_aggr,
297 	    "    delete-aggr      [-t] <link>"				},
298 	{ "add-aggr",		do_add_aggr,
299 	    "    add-aggr         [-t] -l <link> [-l <link>...] <link>" },
300 	{ "remove-aggr",	do_remove_aggr,
301 	    "    remove-aggr      [-t] -l <link> [-l <link>...] <link>" },
302 	{ "modify-aggr",	do_modify_aggr,
303 	    "    modify-aggr      [-t] [-P <policy>] [-L <mode>] [-T <time>] "
304 	    "[-u <address>]\n"
305 	    "\t\t     <link>"						},
306 	{ "show-aggr",		do_show_aggr,
307 	    "    show-aggr        [-pPLx] [-o <field>,..] [-s [-i <interval>]] "
308 	    "[<link>]\n"						},
309 	{ "up-aggr",		do_up_aggr,	NULL			},
310 	{ "scan-wifi",		do_scan_wifi,
311 	    "    scan-wifi        [-p] [-o <field>,...] [<link>]"	},
312 	{ "connect-wifi",	do_connect_wifi,
313 	    "    connect-wifi     [-e <essid>] [-i <bssid>] [-k <key>,...] "
314 	    "[-s wep|wpa]\n"
315 	    "\t\t     [-a open|shared] [-b bss|ibss] [-c] [-m a|b|g] "
316 	    "[-T <time>]\n"
317 	    "\t\t     [<link>]"						},
318 	{ "disconnect-wifi",	do_disconnect_wifi,
319 	    "    disconnect-wifi  [-a] [<link>]"			},
320 	{ "show-wifi",		do_show_wifi,
321 	    "    show-wifi        [-p] [-o <field>,...] [<link>]\n"	},
322 	{ "set-linkprop",	do_set_linkprop,
323 	    "    set-linkprop     [-t] -p <prop>=<value>[,...] <name>"	},
324 	{ "reset-linkprop",	do_reset_linkprop,
325 	    "    reset-linkprop   [-t] [-p <prop>,...] <name>"		},
326 	{ "show-linkprop",	do_show_linkprop,
327 	    "    show-linkprop    [-cP] [-o <field>,...] [-p <prop>,...] "
328 	    "<name>\n"							},
329 	{ "show-ether",		do_show_ether,
330 	    "    show-ether       [-px][-o <field>,...] <link>\n"	},
331 	{ "create-secobj",	do_create_secobj,
332 	    "    create-secobj    [-t] [-f <file>] -c <class> <secobj>"	},
333 	{ "delete-secobj",	do_delete_secobj,
334 	    "    delete-secobj    [-t] <secobj>[,...]"			},
335 	{ "show-secobj",	do_show_secobj,
336 	    "    show-secobj      [-pP] [-o <field>,...] [<secobj>,...]\n" },
337 	{ "init-linkprop",	do_init_linkprop,	NULL		},
338 	{ "init-secobj",	do_init_secobj,		NULL		},
339 	{ "create-vlan",	do_create_vlan,
340 	    "    create-vlan      [-ft] -l <link> -v <vid> [link]"	},
341 	{ "delete-vlan",	do_delete_vlan,
342 	    "    delete-vlan      [-t] <link>"				},
343 	{ "show-vlan",		do_show_vlan,
344 	    "    show-vlan        [-pP] [-o <field>,..] [<link>]\n"	},
345 	{ "up-vlan",		do_up_vlan,		NULL		},
346 	{ "create-iptun",	do_create_iptun,
347 	    "    create-iptun     [-t] -T <type> "
348 	    "[-a {local|remote}=<addr>,...] <link>]" },
349 	{ "delete-iptun",	do_delete_iptun,
350 	    "    delete-iptun     [-t] <link>"				},
351 	{ "modify-iptun",	do_modify_iptun,
352 	    "    modify-iptun     [-t] -a {local|remote}=<addr>,... <link>" },
353 	{ "show-iptun",		do_show_iptun,
354 	    "    show-iptun       [-pP] [-o <field>,..] [<link>]\n"	},
355 	{ "up-iptun",		do_up_iptun,		NULL		},
356 	{ "down-iptun",		do_down_iptun,		NULL		},
357 	{ "delete-phys",	do_delete_phys,
358 	    "    delete-phys      <link>"				},
359 	{ "show-phys",		do_show_phys,
360 	    "    show-phys        [-m | -H | -P] [[-p] [-o <field>[,...]] "
361 	    "[<link>]\n"						},
362 	{ "init-phys",		do_init_phys,		NULL		},
363 	{ "show-linkmap",	do_show_linkmap,	NULL		},
364 	{ "create-vnic",	do_create_vnic,
365 	    "    create-vnic      [-t] -l <link> [-m <value> | auto |\n"
366 	    "\t\t     {factory [-n <slot-id>]} | {random [-r <prefix>]} |\n"
367 	    "\t\t     {vrrp -V <vrid> -A {inet | inet6}} [-v <vid> [-f]]\n"
368 	    "\t\t     [-p <prop>=<value>[,...]] <vnic-link>"	},
369 	{ "delete-vnic",	do_delete_vnic,
370 	    "    delete-vnic      [-t] <vnic-link>"			},
371 	{ "show-vnic",		do_show_vnic,
372 	    "    show-vnic        [-pP] [-l <link>] [-s [-i <interval>]] "
373 	    "[<link>]\n"						},
374 	{ "up-vnic",		do_up_vnic,		NULL		},
375 	{ "create-part",	do_create_part,
376 	    "    create-part      [-t] [-f] -l <link> [-P <pkey>]\n"
377 	    "\t\t     [-R <root-dir>] <part-link>"			},
378 	{ "delete-part",	do_delete_part,
379 	    "    delete-part      [-t] [-R <root-dir>] <part-link>"},
380 	{ "show-part",		do_show_part,
381 	    "    show-part        [-pP] [-o <field>,...][-l <linkover>]\n"
382 	    "\t\t     [<part-link>]"		},
383 	{ "show-ib",		do_show_ib,
384 	    "    show-ib          [-p] [-o <field>,...] [<link>]\n"	},
385 	{ "up-part",		do_up_part,		NULL		},
386 	{ "create-etherstub",	do_create_etherstub,
387 	    "    create-etherstub [-t] <link>"				},
388 	{ "delete-etherstub",	do_delete_etherstub,
389 	    "    delete-etherstub [-t] <link>"				},
390 	{ "show-etherstub",	do_show_etherstub,
391 	    "    show-etherstub   [-t] [<link>]\n"			},
392 	{ "create-simnet",	do_create_simnet,	NULL		},
393 	{ "modify-simnet",	do_modify_simnet,	NULL		},
394 	{ "delete-simnet",	do_delete_simnet,	NULL		},
395 	{ "show-simnet",	do_show_simnet,		NULL		},
396 	{ "up-simnet",		do_up_simnet,		NULL		},
397 	{ "create-bridge",	do_create_bridge,
398 	    "    create-bridge    [-R <root-dir>] [-P <protect>] "
399 	    "[-p <priority>]\n"
400 	    "\t\t     [-m <max-age>] [-h <hello-time>] [-d <forward-delay>]\n"
401 	    "\t\t     [-f <force-protocol>] [-l <link>]... <bridge>"	},
402 	{ "modify-bridge",	do_modify_bridge,
403 	    "    modify-bridge    [-R <root-dir>] [-P <protect>] "
404 	    "[-p <priority>]\n"
405 	    "\t\t     [-m <max-age>] [-h <hello-time>] [-d <forward-delay>]\n"
406 	    "\t\t     [-f <force-protocol>] <bridge>"			},
407 	{ "delete-bridge",	do_delete_bridge,
408 	    "    delete-bridge    [-R <root-dir>] <bridge>"		},
409 	{ "add-bridge",		do_add_bridge,
410 	    "    add-bridge       [-R <root-dir>] -l <link> [-l <link>]... "
411 	    "<bridge>"							},
412 	{ "remove-bridge",	do_remove_bridge,
413 	    "    remove-bridge    [-R <root-dir>] -l <link> [-l <link>]... "
414 	    "<bridge>"							},
415 	{ "show-bridge",	do_show_bridge,
416 	    "    show-bridge      [-p] [-o <field>,...] [-s [-i <interval>]] "
417 	    "[<bridge>]\n"
418 	    "    show-bridge      -l [-p] [-o <field>,...] [-s [-i <interval>]]"
419 	    " <bridge>\n"
420 	    "    show-bridge      -f [-p] [-o <field>,...] [-s [-i <interval>]]"
421 	    " <bridge>\n"
422 	    "    show-bridge      -t [-p] [-o <field>,...] [-s [-i <interval>]]"
423 	    " <bridge>\n"						},
424 	{ "create-overlay",	do_create_overlay,
425 	    "    create-overlay   [-t] -e <encap> -s <search> -v <vnetid>\n"
426 	    "\t\t     [ -p <prop>=<value>[,...]] <overlay>"	},
427 	{ "delete-overlay",	do_delete_overlay,
428 	    "    delete-overlay   [-t] <overlay>"			},
429 	{ "modify-overlay",	do_modify_overlay,
430 	    "    modify-overlay   -d mac | -f | -s mac=ip:port "
431 	    "<overlay>"						},
432 	{ "show-overlay",	do_show_overlay,
433 	    "    show-overlay     [-f | -t] [[-p] -o <field>,...] "
434 	    "[<overlay>]\n"						},
435 	{ "up-overlay",		do_up_overlay,		NULL		},
436 	{ "show-usage",		do_show_usage,
437 	    "    show-usage       [-a] [-d | -F <format>] "
438 	    "[-s <DD/MM/YYYY,HH:MM:SS>]\n"
439 	    "\t\t     [-e <DD/MM/YYYY,HH:MM:SS>] -f <logfile> [<link>]"	}
440 };
441 
442 static const struct option lopts[] = {
443 	{"vlan-id",	required_argument,	0, 'v'},
444 	{"output",	required_argument,	0, 'o'},
445 	{"dev",		required_argument,	0, 'd'},
446 	{"policy",	required_argument,	0, 'P'},
447 	{"lacp-mode",	required_argument,	0, 'L'},
448 	{"lacp-timer",	required_argument,	0, 'T'},
449 	{"unicast",	required_argument,	0, 'u'},
450 	{"temporary",	no_argument,		0, 't'},
451 	{"root-dir",	required_argument,	0, 'R'},
452 	{"link",	required_argument,	0, 'l'},
453 	{"forcible",	no_argument,		0, 'f'},
454 	{"bw-limit",	required_argument,	0, 'b'},
455 	{"mac-address",	required_argument,	0, 'm'},
456 	{"slot",	required_argument,	0, 'n'},
457 	{ NULL, 0, NULL, 0 }
458 };
459 
460 static const struct option show_lopts[] = {
461 	{"statistics",	no_argument,		0, 's'},
462 	{"continuous",	no_argument,		0, 'S'},
463 	{"interval",	required_argument,	0, 'i'},
464 	{"parsable",	no_argument,		0, 'p'},
465 	{"parseable",	no_argument,		0, 'p'},
466 	{"extended",	no_argument,		0, 'x'},
467 	{"output",	required_argument,	0, 'o'},
468 	{"persistent",	no_argument,		0, 'P'},
469 	{"lacp",	no_argument,		0, 'L'},
470 	{ NULL, 0, NULL, 0 }
471 };
472 
473 static const struct option iptun_lopts[] = {
474 	{"output",	required_argument,	0, 'o'},
475 	{"tunnel-type",	required_argument,	0, 'T'},
476 	{"address",	required_argument,	0, 'a'},
477 	{"root-dir",	required_argument,	0, 'R'},
478 	{"parsable",	no_argument,		0, 'p'},
479 	{"parseable",	no_argument,		0, 'p'},
480 	{"persistent",	no_argument,		0, 'P'},
481 	{ NULL, 0, NULL, 0 }
482 };
483 
484 static char * const iptun_addropts[] = {
485 #define	IPTUN_LOCAL	0
486 	"local",
487 #define	IPTUN_REMOTE	1
488 	"remote",
489 	NULL};
490 
491 static const struct {
492 	const char	*type_name;
493 	iptun_type_t	type_value;
494 } iptun_types[] = {
495 	{"ipv4",	IPTUN_TYPE_IPV4},
496 	{"ipv6",	IPTUN_TYPE_IPV6},
497 	{"6to4",	IPTUN_TYPE_6TO4},
498 	{NULL,		0}
499 };
500 
501 static const struct option prop_longopts[] = {
502 	{"temporary",	no_argument,		0, 't'  },
503 	{"output",	required_argument,	0, 'o'  },
504 	{"root-dir",	required_argument,	0, 'R'  },
505 	{"prop",	required_argument,	0, 'p'  },
506 	{"parsable",	no_argument,		0, 'c'  },
507 	{"parseable",	no_argument,		0, 'c'  },
508 	{"persistent",	no_argument,		0, 'P'  },
509 	{ NULL, 0, NULL, 0 }
510 };
511 
512 static const struct option wifi_longopts[] = {
513 	{"parsable",	no_argument,		0, 'p'  },
514 	{"parseable",	no_argument,		0, 'p'  },
515 	{"output",	required_argument,	0, 'o'  },
516 	{"essid",	required_argument,	0, 'e'  },
517 	{"bsstype",	required_argument,	0, 'b'  },
518 	{"mode",	required_argument,	0, 'm'  },
519 	{"key",		required_argument,	0, 'k'  },
520 	{"sec",		required_argument,	0, 's'  },
521 	{"auth",	required_argument,	0, 'a'  },
522 	{"create-ibss",	required_argument,	0, 'c'  },
523 	{"timeout",	required_argument,	0, 'T'  },
524 	{"all-links",	no_argument,		0, 'a'  },
525 	{"temporary",	no_argument,		0, 't'  },
526 	{"root-dir",	required_argument,	0, 'R'  },
527 	{"persistent",	no_argument,		0, 'P'  },
528 	{"file",	required_argument,	0, 'f'  },
529 	{ NULL, 0, NULL, 0 }
530 };
531 
532 static const struct option showeth_lopts[] = {
533 	{"parsable",	no_argument,		0, 'p'	},
534 	{"parseable",	no_argument,		0, 'p'	},
535 	{"extended",	no_argument,		0, 'x'	},
536 	{"output",	required_argument,	0, 'o'	},
537 	{ NULL, 0, NULL, 0 }
538 };
539 
540 static const struct option vnic_lopts[] = {
541 	{"temporary",	no_argument,		0, 't'	},
542 	{"root-dir",	required_argument,	0, 'R'	},
543 	{"dev",		required_argument,	0, 'd'	},
544 	{"mac-address",	required_argument,	0, 'm'	},
545 	{"cpus",	required_argument,	0, 'c'	},
546 	{"bw-limit",	required_argument,	0, 'b'	},
547 	{"slot",	required_argument,	0, 'n'	},
548 	{"mac-prefix",	required_argument,	0, 'r'	},
549 	{"vrid",	required_argument,	0, 'V'	},
550 	{"address-family",	required_argument,	0, 'A'	},
551 	{ NULL, 0, NULL, 0 }
552 };
553 
554 static const struct option part_lopts[] = {
555 	{"temporary",	no_argument,		0, 't'  },
556 	{"pkey",	required_argument,	0, 'P'  },
557 	{"link",	required_argument,	0, 'l'  },
558 	{"force",	no_argument,		0, 'f'  },
559 	{"root-dir",	required_argument,	0, 'R'  },
560 	{"prop",	required_argument,	0, 'p'  },
561 	{ NULL, 0, NULL, 0 }
562 };
563 
564 static const struct option show_part_lopts[] = {
565 	{"parsable",	no_argument,		0, 'p'  },
566 	{"parseable",	no_argument,		0, 'p'  },
567 	{"link",	required_argument,	0, 'l'  },
568 	{"persistent",	no_argument,		0, 'P'  },
569 	{"output",	required_argument,	0, 'o'  },
570 	{ NULL, 0, NULL, 0 }
571 };
572 
573 static const struct option etherstub_lopts[] = {
574 	{"temporary",	no_argument,		0, 't'	},
575 	{"root-dir",	required_argument,	0, 'R'	},
576 	{ NULL, 0, NULL, 0 }
577 };
578 
579 static const struct option usage_opts[] = {
580 	{"file",	required_argument,	0, 'f'	},
581 	{"format",	required_argument,	0, 'F'	},
582 	{"start",	required_argument,	0, 's'	},
583 	{"stop",	required_argument,	0, 'e'	},
584 	{ NULL, 0, NULL, 0 }
585 };
586 
587 static const struct option simnet_lopts[] = {
588 	{"temporary",	no_argument,		0, 't'	},
589 	{"root-dir",	required_argument,	0, 'R'	},
590 	{"media",	required_argument,	0, 'm'	},
591 	{"peer",	required_argument,	0, 'p'	},
592 	{ NULL, 0, NULL, 0 }
593 };
594 
595 static const struct option bridge_lopts[] = {
596 	{ "protect",		required_argument,	0, 'P' },
597 	{ "root-dir",		required_argument,	0, 'R'	},
598 	{ "forward-delay",	required_argument,	0, 'd'	},
599 	{ "force-protocol",	required_argument,	0, 'f'	},
600 	{ "hello-time",		required_argument,	0, 'h'	},
601 	{ "link",		required_argument,	0, 'l'	},
602 	{ "max-age",		required_argument,	0, 'm'	},
603 	{ "priority",		required_argument,	0, 'p'	},
604 	{ NULL, 0, NULL, 0 }
605 };
606 
607 static const struct option bridge_show_lopts[] = {
608 	{ "forwarding", no_argument,		0, 'f' },
609 	{ "interval",	required_argument,	0, 'i' },
610 	{ "link",	no_argument,		0, 'l' },
611 	{ "output",	required_argument,	0, 'o' },
612 	{ "parsable",	no_argument,		0, 'p' },
613 	{ "parseable",	no_argument,		0, 'p' },
614 	{ "statistics",	no_argument,		0, 's' },
615 	{ "trill",	no_argument,		0, 't' },
616 	{ NULL, 0, NULL, 0 }
617 };
618 
619 /*
620  * structures for 'dladm show-ether'
621  */
622 static const char *ptype[] = {LEI_ATTR_NAMES};
623 
624 typedef struct ether_fields_buf_s
625 {
626 	char	eth_link[15];
627 	char	eth_ptype[8];
628 	char	eth_state[8];
629 	char	eth_autoneg[5];
630 	char	eth_spdx[31];
631 	char	eth_pause[6];
632 	char	eth_rem_fault[16];
633 } ether_fields_buf_t;
634 
635 static const ofmt_field_t ether_fields[] = {
636 /* name,	field width,	offset	    callback */
637 { "LINK",	16,
638 	offsetof(ether_fields_buf_t, eth_link), print_default_cb},
639 { "PTYPE",	9,
640 	offsetof(ether_fields_buf_t, eth_ptype), print_default_cb},
641 { "STATE",	9,
642 	offsetof(ether_fields_buf_t, eth_state),
643 	print_default_cb},
644 { "AUTO",	6,
645 	offsetof(ether_fields_buf_t, eth_autoneg), print_default_cb},
646 { "SPEED-DUPLEX", 32,
647 	offsetof(ether_fields_buf_t, eth_spdx), print_default_cb},
648 { "PAUSE",	7,
649 	offsetof(ether_fields_buf_t, eth_pause), print_default_cb},
650 { "REM_FAULT",	17,
651 	offsetof(ether_fields_buf_t, eth_rem_fault), print_default_cb},
652 {NULL,		0,
653 	0,	NULL}}
654 ;
655 
656 typedef struct print_ether_state {
657 	const char	*es_link;
658 	boolean_t	es_parsable;
659 	boolean_t	es_header;
660 	boolean_t	es_extended;
661 	ofmt_handle_t	es_ofmt;
662 } print_ether_state_t;
663 
664 /*
665  * structures for 'dladm show-link -s' (print statistics)
666  */
667 typedef enum {
668 	LINK_S_LINK,
669 	LINK_S_IPKTS,
670 	LINK_S_RBYTES,
671 	LINK_S_IERRORS,
672 	LINK_S_OPKTS,
673 	LINK_S_OBYTES,
674 	LINK_S_OERRORS
675 } link_s_field_index_t;
676 
677 static const ofmt_field_t link_s_fields[] = {
678 /* name,	field width,	index,		callback	*/
679 { "LINK",	15,		LINK_S_LINK,	print_link_stats_cb},
680 { "IPACKETS",	10,		LINK_S_IPKTS,	print_link_stats_cb},
681 { "RBYTES",	8,		LINK_S_RBYTES,	print_link_stats_cb},
682 { "IERRORS",	10,		LINK_S_IERRORS,	print_link_stats_cb},
683 { "OPACKETS",	12,		LINK_S_OPKTS,	print_link_stats_cb},
684 { "OBYTES",	12,		LINK_S_OBYTES,	print_link_stats_cb},
685 { "OERRORS",	8,		LINK_S_OERRORS,	print_link_stats_cb},
686 { NULL,		0,		0,		NULL}};
687 
688 typedef struct link_args_s {
689 	char		*link_s_link;
690 	pktsum_t	*link_s_psum;
691 } link_args_t;
692 
693 /*
694  * buffer used by print functions for show-{link,phys,vlan} commands.
695  */
696 typedef struct link_fields_buf_s {
697 	char link_name[MAXLINKNAMELEN];
698 	char link_class[DLADM_STRSIZE];
699 	char link_mtu[11];
700 	char link_state[DLADM_STRSIZE];
701 	char link_bridge[MAXLINKNAMELEN * MAXPORT];
702 	char link_over[MAXLINKNAMELEN * MAXPORT];
703 	char link_phys_state[DLADM_STRSIZE];
704 	char link_phys_media[DLADM_STRSIZE];
705 	char link_phys_speed[DLADM_STRSIZE];
706 	char link_phys_duplex[DLPI_LINKNAME_MAX];
707 	char link_phys_device[DLPI_LINKNAME_MAX];
708 	char link_flags[6];
709 	char link_vlan_vid[6];
710 } link_fields_buf_t;
711 
712 /*
713  * structures for 'dladm show-link'
714  */
715 static const ofmt_field_t link_fields[] = {
716 /* name,	field width,	index,	callback */
717 { "LINK",	12,
718 	offsetof(link_fields_buf_t, link_name), print_default_cb},
719 { "CLASS",	10,
720 	offsetof(link_fields_buf_t, link_class), print_default_cb},
721 { "MTU",	7,
722 	offsetof(link_fields_buf_t, link_mtu), print_default_cb},
723 { "STATE",	9,
724 	offsetof(link_fields_buf_t, link_state), print_default_cb},
725 { "BRIDGE",	11,
726     offsetof(link_fields_buf_t, link_bridge), print_default_cb},
727 { "OVER",	30,
728 	offsetof(link_fields_buf_t, link_over), print_default_cb},
729 { NULL,		0, 0, NULL}}
730 ;
731 
732 /*
733  * structures for 'dladm show-aggr'
734  */
735 typedef struct laggr_fields_buf_s {
736 	char laggr_name[DLPI_LINKNAME_MAX];
737 	char laggr_policy[9];
738 	char laggr_addrpolicy[ETHERADDRL * 3 + 3];
739 	char laggr_lacpactivity[14];
740 	char laggr_lacptimer[DLADM_STRSIZE];
741 	char laggr_flags[7];
742 } laggr_fields_buf_t;
743 
744 typedef struct laggr_args_s {
745 	int			laggr_lport; /* -1 indicates the aggr itself */
746 	const char		*laggr_link;
747 	dladm_aggr_grp_attr_t	*laggr_ginfop;
748 	dladm_status_t		*laggr_status;
749 	pktsum_t		*laggr_pktsumtot; /* -s only */
750 	pktsum_t		*laggr_diffstats; /* -s only */
751 	boolean_t		laggr_parsable;
752 } laggr_args_t;
753 
754 static const ofmt_field_t laggr_fields[] = {
755 /* name,	field width,	offset,	callback */
756 { "LINK",	16,
757 	offsetof(laggr_fields_buf_t, laggr_name), print_default_cb},
758 { "POLICY",	9,
759 	offsetof(laggr_fields_buf_t, laggr_policy), print_default_cb},
760 { "ADDRPOLICY",	ETHERADDRL * 3 + 3,
761 	offsetof(laggr_fields_buf_t, laggr_addrpolicy), print_default_cb},
762 { "LACPACTIVITY", 14,
763 	offsetof(laggr_fields_buf_t, laggr_lacpactivity), print_default_cb},
764 { "LACPTIMER",	12,
765 	offsetof(laggr_fields_buf_t, laggr_lacptimer), print_default_cb},
766 { "FLAGS",	8,
767 	offsetof(laggr_fields_buf_t, laggr_flags), print_default_cb},
768 { NULL,		0, 0, NULL}}
769 ;
770 
771 /*
772  * structures for 'dladm show-aggr -x'.
773  */
774 typedef enum {
775 	AGGR_X_LINK,
776 	AGGR_X_PORT,
777 	AGGR_X_SPEED,
778 	AGGR_X_DUPLEX,
779 	AGGR_X_STATE,
780 	AGGR_X_ADDRESS,
781 	AGGR_X_PORTSTATE
782 } aggr_x_field_index_t;
783 
784 static const ofmt_field_t aggr_x_fields[] = {
785 /* name,	field width,	index		callback */
786 { "LINK",	12,	AGGR_X_LINK,		print_xaggr_cb},
787 { "PORT",	15,	AGGR_X_PORT,		print_xaggr_cb},
788 { "SPEED",	9,	AGGR_X_SPEED,		print_xaggr_cb},
789 { "DUPLEX",	9,	AGGR_X_DUPLEX,		print_xaggr_cb},
790 { "STATE",	9,	AGGR_X_STATE,		print_xaggr_cb},
791 { "ADDRESS",	19,	AGGR_X_ADDRESS,		print_xaggr_cb},
792 { "PORTSTATE",	16,	AGGR_X_PORTSTATE,	print_xaggr_cb},
793 { NULL,		0,	0,			NULL}}
794 ;
795 
796 /*
797  * structures for 'dladm show-aggr -s'.
798  */
799 typedef enum {
800 	AGGR_S_LINK,
801 	AGGR_S_PORT,
802 	AGGR_S_IPKTS,
803 	AGGR_S_RBYTES,
804 	AGGR_S_OPKTS,
805 	AGGR_S_OBYTES,
806 	AGGR_S_IPKTDIST,
807 	AGGR_S_OPKTDIST
808 } aggr_s_field_index_t;
809 
810 static const ofmt_field_t aggr_s_fields[] = {
811 { "LINK",		12,	AGGR_S_LINK, print_aggr_stats_cb},
812 { "PORT",		10,	AGGR_S_PORT, print_aggr_stats_cb},
813 { "IPACKETS",		8,	AGGR_S_IPKTS, print_aggr_stats_cb},
814 { "RBYTES",		8,	AGGR_S_RBYTES, print_aggr_stats_cb},
815 { "OPACKETS",		8,	AGGR_S_OPKTS, print_aggr_stats_cb},
816 { "OBYTES",		8,	AGGR_S_OBYTES, print_aggr_stats_cb},
817 { "IPKTDIST",		9,	AGGR_S_IPKTDIST, print_aggr_stats_cb},
818 { "OPKTDIST",		15,	AGGR_S_OPKTDIST, print_aggr_stats_cb},
819 { NULL,			0,	0,		NULL}}
820 ;
821 
822 /*
823  * structures for 'dladm show-aggr -L'.
824  */
825 typedef enum {
826 	AGGR_L_LINK,
827 	AGGR_L_PORT,
828 	AGGR_L_AGGREGATABLE,
829 	AGGR_L_SYNC,
830 	AGGR_L_COLL,
831 	AGGR_L_DIST,
832 	AGGR_L_DEFAULTED,
833 	AGGR_L_EXPIRED
834 } aggr_l_field_index_t;
835 
836 static const ofmt_field_t aggr_l_fields[] = {
837 /* name,		field width,	index */
838 { "LINK",		12,	AGGR_L_LINK,		print_lacp_cb},
839 { "PORT",		13,	AGGR_L_PORT,		print_lacp_cb},
840 { "AGGREGATABLE",	13,	AGGR_L_AGGREGATABLE,	print_lacp_cb},
841 { "SYNC",		5,	AGGR_L_SYNC,		print_lacp_cb},
842 { "COLL",		5,	AGGR_L_COLL,		print_lacp_cb},
843 { "DIST",		5,	AGGR_L_DIST,		print_lacp_cb},
844 { "DEFAULTED",		10,	AGGR_L_DEFAULTED,	print_lacp_cb},
845 { "EXPIRED",		15,	AGGR_L_EXPIRED,		print_lacp_cb},
846 { NULL,			0,	0,			NULL}}
847 ;
848 
849 /*
850  * structures for 'dladm show-phys'
851  */
852 
853 static const ofmt_field_t phys_fields[] = {
854 /* name,	field width,	offset */
855 { "LINK",	13,
856 	offsetof(link_fields_buf_t, link_name), print_default_cb},
857 { "MEDIA",	21,
858 	offsetof(link_fields_buf_t, link_phys_media), print_default_cb},
859 { "STATE",	11,
860 	offsetof(link_fields_buf_t, link_phys_state), print_default_cb},
861 { "SPEED",	9,
862 	offsetof(link_fields_buf_t, link_phys_speed), print_default_cb},
863 { "DUPLEX",	9,
864 	offsetof(link_fields_buf_t, link_phys_duplex), print_default_cb},
865 { "DEVICE",	13,
866 	offsetof(link_fields_buf_t, link_phys_device), print_default_cb},
867 { "FLAGS",	7,
868 	offsetof(link_fields_buf_t, link_flags), print_default_cb},
869 { NULL,		0, 0, NULL}}
870 ;
871 
872 /*
873  * structures for 'dladm show-phys -m'
874  */
875 
876 typedef enum {
877 	PHYS_M_LINK,
878 	PHYS_M_SLOT,
879 	PHYS_M_ADDRESS,
880 	PHYS_M_INUSE,
881 	PHYS_M_CLIENT
882 } phys_m_field_index_t;
883 
884 static const ofmt_field_t phys_m_fields[] = {
885 /* name,	field width,	offset */
886 { "LINK",	13,	PHYS_M_LINK,	print_phys_one_mac_cb},
887 { "SLOT",	9,	PHYS_M_SLOT,	print_phys_one_mac_cb},
888 { "ADDRESS",	19,	PHYS_M_ADDRESS,	print_phys_one_mac_cb},
889 { "INUSE",	5,	PHYS_M_INUSE,	print_phys_one_mac_cb},
890 { "CLIENT",	13,	PHYS_M_CLIENT,	print_phys_one_mac_cb},
891 { NULL,		0,	0,		NULL}}
892 ;
893 
894 /*
895  * structures for 'dladm show-phys -H'
896  */
897 
898 typedef enum {
899 	PHYS_H_LINK,
900 	PHYS_H_RINGTYPE,
901 	PHYS_H_RINGS,
902 	PHYS_H_CLIENTS
903 } phys_h_field_index_t;
904 
905 #define	RINGSTRLEN	21
906 
907 static const ofmt_field_t phys_h_fields[] = {
908 { "LINK",	13,	PHYS_H_LINK,	print_phys_one_hwgrp_cb},
909 { "RINGTYPE",	9,	PHYS_H_RINGTYPE,	print_phys_one_hwgrp_cb},
910 { "RINGS",	RINGSTRLEN,	PHYS_H_RINGS,	print_phys_one_hwgrp_cb},
911 { "CLIENTS",	24,	PHYS_H_CLIENTS,	print_phys_one_hwgrp_cb},
912 { NULL,		0,	0,		NULL}}
913 ;
914 
915 /*
916  * structures for 'dladm show-vlan'
917  */
918 static const ofmt_field_t vlan_fields[] = {
919 { "LINK",	16,
920 	offsetof(link_fields_buf_t, link_name), print_default_cb},
921 { "VID",	9,
922 	offsetof(link_fields_buf_t, link_vlan_vid), print_default_cb},
923 { "OVER",	13,
924 	offsetof(link_fields_buf_t, link_over), print_default_cb},
925 { "FLAGS",	7,
926 	offsetof(link_fields_buf_t, link_flags), print_default_cb},
927 { NULL,		0, 0, NULL}}
928 ;
929 
930 /*
931  * structures common to 'dladm scan-wifi' and 'dladm show-wifi'
932  * callback will be determined in parse_wifi_fields.
933  */
934 static ofmt_field_t wifi_common_fields[] = {
935 { "LINK",	11, 0,				NULL},
936 { "ESSID",	20, DLADM_WLAN_ATTR_ESSID,	NULL},
937 { "BSSID",	18, DLADM_WLAN_ATTR_BSSID,	NULL},
938 { "IBSSID",	18, DLADM_WLAN_ATTR_BSSID,	NULL},
939 { "MODE",	7,  DLADM_WLAN_ATTR_MODE,	NULL},
940 { "SPEED",	7,  DLADM_WLAN_ATTR_SPEED,	NULL},
941 { "BSSTYPE",	9,  DLADM_WLAN_ATTR_BSSTYPE,	NULL},
942 { "SEC",	7,  DLADM_WLAN_ATTR_SECMODE,	NULL},
943 { "STRENGTH",	11, DLADM_WLAN_ATTR_STRENGTH,	NULL},
944 { NULL,		0,  0,				NULL}};
945 
946 /*
947  * the 'show-wifi' command supports all the fields in wifi_common_fields
948  * plus the AUTH and STATUS fields.
949  */
950 static ofmt_field_t wifi_show_fields[A_CNT(wifi_common_fields) + 2] = {
951 { "AUTH",	9,  DLADM_WLAN_ATTR_AUTH,	NULL},
952 { "STATUS",	18, DLADM_WLAN_LINKATTR_STATUS,	print_wifi_status_cb},
953 /* copy wifi_common_fields here */
954 };
955 
956 static char *all_scan_wifi_fields =
957 	"link,essid,bssid,sec,strength,mode,speed,bsstype";
958 static char *all_show_wifi_fields =
959 	"link,status,essid,sec,strength,mode,speed,auth,bssid,bsstype";
960 static char *def_scan_wifi_fields =
961 	"link,essid,bssid,sec,strength,mode,speed";
962 static char *def_show_wifi_fields =
963 	"link,status,essid,sec,strength,mode,speed";
964 
965 /*
966  * structures for 'dladm show-linkprop'
967  */
968 typedef enum {
969 	LINKPROP_LINK,
970 	LINKPROP_PROPERTY,
971 	LINKPROP_PERM,
972 	LINKPROP_VALUE,
973 	LINKPROP_DEFAULT,
974 	LINKPROP_POSSIBLE
975 } linkprop_field_index_t;
976 
977 static const ofmt_field_t linkprop_fields[] = {
978 /* name,	field width,  index */
979 { "LINK",	13,	LINKPROP_LINK,		print_linkprop_cb},
980 { "PROPERTY",	16,	LINKPROP_PROPERTY,	print_linkprop_cb},
981 { "PERM",	5,	LINKPROP_PERM,		print_linkprop_cb},
982 { "VALUE",	15,	LINKPROP_VALUE,		print_linkprop_cb},
983 { "DEFAULT",	15,	LINKPROP_DEFAULT,	print_linkprop_cb},
984 { "POSSIBLE",	20,	LINKPROP_POSSIBLE,	print_linkprop_cb},
985 { NULL,		0,	0,			NULL}}
986 ;
987 
988 #define	MAX_PROP_LINE		512
989 
990 typedef struct show_linkprop_state {
991 	char			ls_link[MAXLINKNAMELEN];
992 	char			*ls_line;
993 	char			**ls_propvals;
994 	dladm_arg_list_t	*ls_proplist;
995 	boolean_t		ls_parsable;
996 	boolean_t		ls_persist;
997 	boolean_t		ls_header;
998 	dladm_status_t		ls_status;
999 	dladm_status_t		ls_retstatus;
1000 	ofmt_handle_t		ls_ofmt;
1001 } show_linkprop_state_t;
1002 
1003 typedef struct set_linkprop_state {
1004 	const char		*ls_name;
1005 	boolean_t		ls_reset;
1006 	boolean_t		ls_temp;
1007 	dladm_status_t		ls_status;
1008 } set_linkprop_state_t;
1009 
1010 typedef struct linkprop_args_s {
1011 	show_linkprop_state_t	*ls_state;
1012 	char			*ls_propname;
1013 	datalink_id_t		ls_linkid;
1014 } linkprop_args_t;
1015 
1016 /*
1017  * structures for 'dladm show-secobj'
1018  */
1019 typedef struct secobj_fields_buf_s {
1020 	char			ss_obj_name[DLADM_SECOBJ_VAL_MAX];
1021 	char			ss_class[20];
1022 	char			ss_val[30];
1023 } secobj_fields_buf_t;
1024 
1025 static const ofmt_field_t secobj_fields[] = {
1026 { "OBJECT",	21,
1027 	offsetof(secobj_fields_buf_t, ss_obj_name), print_default_cb},
1028 { "CLASS",	21,
1029 	offsetof(secobj_fields_buf_t, ss_class), print_default_cb},
1030 { "VALUE",	31,
1031 	offsetof(secobj_fields_buf_t, ss_val), print_default_cb},
1032 { NULL,		0, 0, NULL}}
1033 ;
1034 
1035 /*
1036  * structures for 'dladm show-vnic'
1037  */
1038 typedef struct vnic_fields_buf_s
1039 {
1040 	char vnic_link[DLPI_LINKNAME_MAX];
1041 	char vnic_over[DLPI_LINKNAME_MAX];
1042 	char vnic_speed[10];
1043 	char vnic_macaddr[18];
1044 	char vnic_macaddrtype[19];
1045 	char vnic_vid[6];
1046 } vnic_fields_buf_t;
1047 
1048 static const ofmt_field_t vnic_fields[] = {
1049 { "LINK",		13,
1050 	offsetof(vnic_fields_buf_t, vnic_link),	print_default_cb},
1051 { "OVER",		13,
1052 	offsetof(vnic_fields_buf_t, vnic_over),	print_default_cb},
1053 { "SPEED",		9,
1054 	offsetof(vnic_fields_buf_t, vnic_speed), print_default_cb},
1055 { "MACADDRESS",		18,
1056 	offsetof(vnic_fields_buf_t, vnic_macaddr), print_default_cb},
1057 { "MACADDRTYPE",	20,
1058 	offsetof(vnic_fields_buf_t, vnic_macaddrtype), print_default_cb},
1059 { "VID",		7,
1060 	offsetof(vnic_fields_buf_t, vnic_vid), print_default_cb},
1061 { NULL,			0, 0, NULL}}
1062 ;
1063 
1064 /*
1065  * structures for 'dladm show-ib'
1066  */
1067 typedef struct ib_fields_buf_s
1068 {
1069 	char ib_link[DLPI_LINKNAME_MAX];
1070 	char ib_hcaguid[17];
1071 	char ib_portguid[17];
1072 	char ib_portnum[4];
1073 	char ib_state[6];
1074 	char ib_pkeys[MAXPKEYSTRSZ];
1075 } ib_fields_buf_t;
1076 
1077 static const ofmt_field_t ib_fields[] = {
1078 { "LINK",		13,
1079 	offsetof(ib_fields_buf_t, ib_link),	print_default_cb},
1080 { "HCAGUID",		IBGUIDSTRLEN,
1081 	offsetof(ib_fields_buf_t, ib_hcaguid),	print_default_cb},
1082 { "PORTGUID",		IBGUIDSTRLEN,
1083 	offsetof(ib_fields_buf_t, ib_portguid),	print_default_cb},
1084 { "PORT",		IBPORTSTRLEN,
1085 	offsetof(ib_fields_buf_t, ib_portnum), print_default_cb},
1086 { "STATE",		7,
1087 	offsetof(ib_fields_buf_t, ib_state), print_default_cb},
1088 { "PKEYS",	18,
1089 	offsetof(ib_fields_buf_t, ib_pkeys), print_default_cb},
1090 { NULL,			0, 0, NULL}};
1091 
1092 /*
1093  * structures for 'dladm show-part'
1094  */
1095 typedef struct part_fields_buf_s
1096 {
1097 	char part_link[DLPI_LINKNAME_MAX];
1098 	char part_pkey[5];
1099 	char part_over[DLPI_LINKNAME_MAX];
1100 	char part_state[8];
1101 	char part_flags[5];
1102 } part_fields_buf_t;
1103 
1104 static const ofmt_field_t part_fields[] = {
1105 { "LINK",		13,
1106 	offsetof(part_fields_buf_t, part_link),	print_default_cb},
1107 { "PKEY",		MAXPKEYLEN,
1108 	offsetof(part_fields_buf_t, part_pkey),	print_default_cb},
1109 { "OVER",		13,
1110 	offsetof(part_fields_buf_t, part_over), print_default_cb},
1111 { "STATE",		9,
1112 	offsetof(part_fields_buf_t, part_state), print_default_cb},
1113 { "FLAGS",	5,
1114 	offsetof(part_fields_buf_t, part_flags), print_default_cb},
1115 { NULL,			0, 0, NULL}};
1116 
1117 /*
1118  * structures for 'dladm show-simnet'
1119  */
1120 typedef struct simnet_fields_buf_s
1121 {
1122 	char simnet_name[DLPI_LINKNAME_MAX];
1123 	char simnet_media[DLADM_STRSIZE];
1124 	char simnet_macaddr[18];
1125 	char simnet_otherlink[DLPI_LINKNAME_MAX];
1126 } simnet_fields_buf_t;
1127 
1128 static const ofmt_field_t simnet_fields[] = {
1129 { "LINK",		12,
1130 	offsetof(simnet_fields_buf_t, simnet_name), print_default_cb},
1131 { "MEDIA",		20,
1132 	offsetof(simnet_fields_buf_t, simnet_media), print_default_cb},
1133 { "MACADDRESS",		18,
1134 	offsetof(simnet_fields_buf_t, simnet_macaddr), print_default_cb},
1135 { "OTHERLINK",		12,
1136 	offsetof(simnet_fields_buf_t, simnet_otherlink), print_default_cb},
1137 { NULL,			0, 0, NULL}}
1138 ;
1139 
1140 /*
1141  * structures for 'dladm show-usage'
1142  */
1143 
1144 typedef struct  usage_fields_buf_s {
1145 	char	usage_link[12];
1146 	char	usage_duration[10];
1147 	char	usage_ipackets[9];
1148 	char	usage_rbytes[10];
1149 	char	usage_opackets[9];
1150 	char	usage_obytes[10];
1151 	char	usage_bandwidth[15];
1152 } usage_fields_buf_t;
1153 
1154 static const ofmt_field_t usage_fields[] = {
1155 { "LINK",	13,
1156 	offsetof(usage_fields_buf_t, usage_link), print_default_cb},
1157 { "DURATION",	11,
1158 	offsetof(usage_fields_buf_t, usage_duration), print_default_cb},
1159 { "IPACKETS",	10,
1160 	offsetof(usage_fields_buf_t, usage_ipackets), print_default_cb},
1161 { "RBYTES",	11,
1162 	offsetof(usage_fields_buf_t, usage_rbytes), print_default_cb},
1163 { "OPACKETS",	10,
1164 	offsetof(usage_fields_buf_t, usage_opackets), print_default_cb},
1165 { "OBYTES",	11,
1166 	offsetof(usage_fields_buf_t, usage_obytes), print_default_cb},
1167 { "BANDWIDTH",	16,
1168 	offsetof(usage_fields_buf_t, usage_bandwidth), print_default_cb},
1169 { NULL,		0, 0, NULL}}
1170 ;
1171 
1172 
1173 /*
1174  * structures for 'dladm show-usage link'
1175  */
1176 
1177 typedef struct  usage_l_fields_buf_s {
1178 	char	usage_l_link[12];
1179 	char	usage_l_stime[13];
1180 	char	usage_l_etime[13];
1181 	char	usage_l_rbytes[8];
1182 	char	usage_l_obytes[8];
1183 	char	usage_l_bandwidth[15];
1184 } usage_l_fields_buf_t;
1185 
1186 static const ofmt_field_t usage_l_fields[] = {
1187 /* name,	field width,	offset */
1188 { "LINK",	13,
1189 	offsetof(usage_l_fields_buf_t, usage_l_link), print_default_cb},
1190 { "START",	14,
1191 	offsetof(usage_l_fields_buf_t, usage_l_stime), print_default_cb},
1192 { "END",	14,
1193 	offsetof(usage_l_fields_buf_t, usage_l_etime), print_default_cb},
1194 { "RBYTES",	9,
1195 	offsetof(usage_l_fields_buf_t, usage_l_rbytes), print_default_cb},
1196 { "OBYTES",	9,
1197 	offsetof(usage_l_fields_buf_t, usage_l_obytes), print_default_cb},
1198 { "BANDWIDTH",	16,
1199 	offsetof(usage_l_fields_buf_t, usage_l_bandwidth), print_default_cb},
1200 { NULL,		0, 0, NULL}}
1201 ;
1202 
1203 /* IPTUN_*FLAG_INDEX values are indices into iptun_flags below. */
1204 enum { IPTUN_SFLAG_INDEX, IPTUN_IFLAG_INDEX, IPTUN_NUM_FLAGS };
1205 
1206 /*
1207  * structures for 'dladm show-iptun'
1208  */
1209 typedef struct iptun_fields_buf_s {
1210 	char	iptun_name[MAXLINKNAMELEN];
1211 	char	iptun_type[5];
1212 	char	iptun_laddr[NI_MAXHOST];
1213 	char	iptun_raddr[NI_MAXHOST];
1214 	char	iptun_flags[IPTUN_NUM_FLAGS + 1];
1215 } iptun_fields_buf_t;
1216 
1217 static const ofmt_field_t iptun_fields[] = {
1218 { "LINK",	16,
1219 	offsetof(iptun_fields_buf_t, iptun_name), print_default_cb },
1220 { "TYPE",	6,
1221 	offsetof(iptun_fields_buf_t, iptun_type), print_default_cb },
1222 { "FLAGS",	7,
1223 	offsetof(iptun_fields_buf_t, iptun_flags), print_default_cb },
1224 { "LOCAL",	20,
1225 	offsetof(iptun_fields_buf_t, iptun_laddr), print_default_cb },
1226 { "REMOTE",	20,
1227 	offsetof(iptun_fields_buf_t, iptun_raddr), print_default_cb },
1228 { NULL, 0, 0, NULL}
1229 };
1230 
1231 /*
1232  * structures for 'dladm show-bridge'.  These are based on sections 14.8.1.1.3
1233  * and 14.8.1.2.2 of IEEE 802.1D-2004.
1234  */
1235 typedef struct bridge_fields_buf_s {
1236 	char bridge_name[MAXLINKNAMELEN]; /* 14.4.1.2.3(b) */
1237 	char bridge_protect[7];		/* stp or trill */
1238 	char bridge_address[24];	/* 17.18.3, 7.12.5, 14.4.1.2.3(a) */
1239 	char bridge_priority[7];	/* 17.18.3 9.2.5 - only upper 4 bits */
1240 	char bridge_bmaxage[7];		/* 17.18.4 configured */
1241 	char bridge_bhellotime[7];	/* 17.18.4 configured */
1242 	char bridge_bfwddelay[7];	/* 17.18.4 configured */
1243 	char bridge_forceproto[3];	/* 17.13.4 configured */
1244 	char bridge_tctime[12];		/* 14.8.1.1.3(b) */
1245 	char bridge_tccount[12];	/* 17.17.8 */
1246 	char bridge_tchange[12];	/* 17.17.8 */
1247 	char bridge_desroot[24];	/* 17.18.6 priority "/" MAC */
1248 	char bridge_rootcost[12];	/* 17.18.6 */
1249 	char bridge_rootport[12];	/* 17.18.6 */
1250 	char bridge_maxage[7];		/* 17.18.7 for root */
1251 	char bridge_hellotime[7];	/* 17.13.6 for root */
1252 	char bridge_fwddelay[7];	/* 17.13.5 for root */
1253 	char bridge_holdtime[12];	/* 17.13.12 for root */
1254 } bridge_fields_buf_t;
1255 
1256 static ofmt_field_t bridge_fields[] = {
1257 /* name,	field width,	offset,	callback	*/
1258 { "BRIDGE",	12,
1259     offsetof(bridge_fields_buf_t, bridge_name), print_default_cb },
1260 { "PROTECT",	8,
1261     offsetof(bridge_fields_buf_t, bridge_protect), print_default_cb },
1262 { "ADDRESS",	19,
1263     offsetof(bridge_fields_buf_t, bridge_address), print_default_cb },
1264 { "PRIORITY",	9,
1265     offsetof(bridge_fields_buf_t, bridge_priority), print_default_cb },
1266 { "BMAXAGE",	8,
1267     offsetof(bridge_fields_buf_t, bridge_bmaxage), print_default_cb },
1268 { "BHELLOTIME",	11,
1269     offsetof(bridge_fields_buf_t, bridge_bhellotime), print_default_cb },
1270 { "BFWDDELAY",	10,
1271     offsetof(bridge_fields_buf_t, bridge_bfwddelay), print_default_cb },
1272 { "FORCEPROTO",	11,
1273     offsetof(bridge_fields_buf_t, bridge_forceproto), print_default_cb },
1274 { "TCTIME",	10,
1275     offsetof(bridge_fields_buf_t, bridge_tctime), print_default_cb },
1276 { "TCCOUNT",	10,
1277     offsetof(bridge_fields_buf_t, bridge_tccount), print_default_cb },
1278 { "TCHANGE",	10,
1279     offsetof(bridge_fields_buf_t, bridge_tchange), print_default_cb },
1280 { "DESROOT",	23,
1281     offsetof(bridge_fields_buf_t, bridge_desroot), print_default_cb },
1282 { "ROOTCOST",	11,
1283     offsetof(bridge_fields_buf_t, bridge_rootcost), print_default_cb },
1284 { "ROOTPORT",	11,
1285     offsetof(bridge_fields_buf_t, bridge_rootport), print_default_cb },
1286 { "MAXAGE",	8,
1287     offsetof(bridge_fields_buf_t, bridge_maxage), print_default_cb },
1288 { "HELLOTIME",	10,
1289     offsetof(bridge_fields_buf_t, bridge_hellotime), print_default_cb },
1290 { "FWDDELAY",	9,
1291     offsetof(bridge_fields_buf_t, bridge_fwddelay), print_default_cb },
1292 { "HOLDTIME",	9,
1293     offsetof(bridge_fields_buf_t, bridge_holdtime), print_default_cb },
1294 { NULL,		0, 0, NULL}};
1295 
1296 /*
1297  * structures for 'dladm show-bridge -l'.  These are based on 14.4.1.2.3 and
1298  * 14.8.2.1.3 of IEEE 802.1D-2004.
1299  */
1300 typedef struct bridge_link_fields_buf_s {
1301 	char bridgel_link[MAXLINKNAMELEN];
1302 	char bridgel_index[7];			/* 14.4.1.2.3(d1) */
1303 	char bridgel_state[11];			/* 14.8.2.1.3(b) */
1304 	char bridgel_uptime[7];			/* 14.8.2.1.3(a) */
1305 	char bridgel_opercost[7]		/* 14.8.2.1.3(d) */;
1306 	char bridgel_operp2p[4];		/* 14.8.2.1.3(p) */
1307 	char bridgel_operedge[4];		/* 14.8.2.1.3(k) */
1308 	char bridgel_desroot[23];		/* 14.8.2.1.3(e) */
1309 	char bridgel_descost[12];		/* 14.8.2.1.3(f) */
1310 	char bridgel_desbridge[23];		/* 14.8.2.1.3(g) */
1311 	char bridgel_desport[7];		/* 14.8.2.1.3(h) */
1312 	char bridgel_tcack[4];			/* 14.8.2.1.3(i) */
1313 } bridge_link_fields_buf_t;
1314 
1315 static ofmt_field_t bridge_link_fields[] = {
1316 /* name,	field width,	offset,	callback	*/
1317 { "LINK",		12,
1318     offsetof(bridge_link_fields_buf_t, bridgel_link), print_default_cb },
1319 { "INDEX",	8,
1320     offsetof(bridge_link_fields_buf_t, bridgel_index), print_default_cb },
1321 { "STATE",	12,
1322     offsetof(bridge_link_fields_buf_t, bridgel_state), print_default_cb },
1323 { "UPTIME",	8,
1324     offsetof(bridge_link_fields_buf_t, bridgel_uptime), print_default_cb },
1325 { "OPERCOST",	9,
1326     offsetof(bridge_link_fields_buf_t, bridgel_opercost), print_default_cb },
1327 { "OPERP2P",	8,
1328     offsetof(bridge_link_fields_buf_t, bridgel_operp2p), print_default_cb },
1329 { "OPEREDGE",	9,
1330     offsetof(bridge_link_fields_buf_t, bridgel_operedge), print_default_cb },
1331 { "DESROOT",	22,
1332     offsetof(bridge_link_fields_buf_t, bridgel_desroot), print_default_cb },
1333 { "DESCOST",	11,
1334     offsetof(bridge_link_fields_buf_t, bridgel_descost), print_default_cb },
1335 { "DESBRIDGE",	22,
1336     offsetof(bridge_link_fields_buf_t, bridgel_desbridge), print_default_cb },
1337 { "DESPORT",	8,
1338     offsetof(bridge_link_fields_buf_t, bridgel_desport), print_default_cb },
1339 { "TCACK",	6,
1340     offsetof(bridge_link_fields_buf_t, bridgel_tcack), print_default_cb },
1341 { NULL,		0, 0, NULL}};
1342 
1343 /*
1344  * structures for 'dladm show-bridge -s'.  These are not based on IEEE
1345  * 802.1D-2004.
1346  */
1347 #define	ULONG_DIG	(((sizeof (ulong_t) * NBBY) * 3 / 10) + 1)
1348 #define	UINT64_DIG	(((sizeof (uint64_t) * NBBY) * 3 / 10) + 1)
1349 typedef struct bridge_statfields_buf_s {
1350 	char bridges_name[MAXLINKNAMELEN];
1351 	char bridges_drops[UINT64_DIG];
1352 	char bridges_forwards[UINT64_DIG];
1353 	char bridges_mbcast[UINT64_DIG];
1354 	char bridges_unknown[UINT64_DIG];
1355 	char bridges_recv[UINT64_DIG];
1356 	char bridges_sent[UINT64_DIG];
1357 } bridge_statfields_buf_t;
1358 
1359 static ofmt_field_t bridge_statfields[] = {
1360 /* name,	field width,	offset,	callback	*/
1361 { "BRIDGE",	12,
1362     offsetof(bridge_statfields_buf_t, bridges_name), print_default_cb },
1363 { "DROPS",	12,
1364     offsetof(bridge_statfields_buf_t, bridges_drops), print_default_cb },
1365 { "FORWARDS",	12,
1366     offsetof(bridge_statfields_buf_t, bridges_forwards), print_default_cb },
1367 { "MBCAST",	12,
1368     offsetof(bridge_statfields_buf_t, bridges_mbcast), print_default_cb },
1369 { "UNKNOWN",	12,
1370     offsetof(bridge_statfields_buf_t, bridges_unknown), print_default_cb },
1371 { "RECV",	12,
1372     offsetof(bridge_statfields_buf_t, bridges_recv), print_default_cb },
1373 { "SENT",	12,
1374     offsetof(bridge_statfields_buf_t, bridges_sent), print_default_cb },
1375 { NULL,		0, 0, NULL}};
1376 
1377 /*
1378  * structures for 'dladm show-bridge -s -l'.  These are based in part on
1379  * section 14.6.1.1.3 of IEEE 802.1D-2004.
1380  */
1381 typedef struct bridge_link_statfields_buf_s {
1382 	char bridgels_link[MAXLINKNAMELEN];
1383 	char bridgels_cfgbpdu[ULONG_DIG];
1384 	char bridgels_tcnbpdu[ULONG_DIG];
1385 	char bridgels_rstpbpdu[ULONG_DIG];
1386 	char bridgels_txbpdu[ULONG_DIG];
1387 	char bridgels_drops[UINT64_DIG];	/* 14.6.1.1.3(d) */
1388 	char bridgels_recv[UINT64_DIG];		/* 14.6.1.1.3(a) */
1389 	char bridgels_xmit[UINT64_DIG];		/* 14.6.1.1.3(c) */
1390 } bridge_link_statfields_buf_t;
1391 
1392 static ofmt_field_t bridge_link_statfields[] = {
1393 /* name,	field width,	offset,	callback	*/
1394 { "LINK",	12,
1395     offsetof(bridge_link_statfields_buf_t, bridgels_link), print_default_cb },
1396 { "CFGBPDU",	9,
1397     offsetof(bridge_link_statfields_buf_t, bridgels_cfgbpdu),
1398     print_default_cb },
1399 { "TCNBPDU",	9,
1400     offsetof(bridge_link_statfields_buf_t, bridgels_tcnbpdu),
1401     print_default_cb },
1402 { "RSTPBPDU",	9,
1403     offsetof(bridge_link_statfields_buf_t, bridgels_rstpbpdu),
1404     print_default_cb },
1405 { "TXBPDU",	9,
1406     offsetof(bridge_link_statfields_buf_t, bridgels_txbpdu), print_default_cb },
1407 { "DROPS",	9,
1408     offsetof(bridge_link_statfields_buf_t, bridgels_drops), print_default_cb },
1409 { "RECV",	9,
1410     offsetof(bridge_link_statfields_buf_t, bridgels_recv), print_default_cb },
1411 { "XMIT",	9,
1412     offsetof(bridge_link_statfields_buf_t, bridgels_xmit), print_default_cb },
1413 { NULL,		0, 0, NULL}};
1414 
1415 /*
1416  * structures for 'dladm show-bridge -f'.  These are based in part on
1417  * section  14.7.6.3.3 of IEEE 802.1D-2004.
1418  */
1419 typedef struct bridge_fwd_fields_buf_s {
1420 	char bridgef_dest[18];			/* 14.7.6.3.3(a) */
1421 	char bridgef_age[8];
1422 	char bridgef_flags[6];
1423 	char bridgef_output[MAXLINKNAMELEN];	/* 14.7.6.3.3(c) */
1424 } bridge_fwd_fields_buf_t;
1425 
1426 static ofmt_field_t bridge_fwd_fields[] = {
1427 /* name,	field width,	offset,	callback	*/
1428 { "DEST",	17,
1429     offsetof(bridge_fwd_fields_buf_t, bridgef_dest), print_default_cb },
1430 { "AGE",	7,
1431     offsetof(bridge_fwd_fields_buf_t, bridgef_age), print_default_cb },
1432 { "FLAGS",	6,
1433     offsetof(bridge_fwd_fields_buf_t, bridgef_flags), print_default_cb },
1434 { "OUTPUT",	12,
1435     offsetof(bridge_fwd_fields_buf_t, bridgef_output), print_default_cb },
1436 { NULL,		0, 0, NULL}};
1437 
1438 /*
1439  * structures for 'dladm show-bridge -t'.
1440  */
1441 typedef struct bridge_trill_fields_buf_s {
1442 	char bridget_nick[6];
1443 	char bridget_flags[6];
1444 	char bridget_link[MAXLINKNAMELEN];
1445 	char bridget_nexthop[18];
1446 } bridge_trill_fields_buf_t;
1447 
1448 static ofmt_field_t bridge_trill_fields[] = {
1449 /* name,	field width,	offset,	callback	*/
1450 { "NICK",	5,
1451     offsetof(bridge_trill_fields_buf_t, bridget_nick), print_default_cb },
1452 { "FLAGS",	6,
1453     offsetof(bridge_trill_fields_buf_t, bridget_flags), print_default_cb },
1454 { "LINK",	12,
1455     offsetof(bridge_trill_fields_buf_t, bridget_link), print_default_cb },
1456 { "NEXTHOP",	17,
1457     offsetof(bridge_trill_fields_buf_t, bridget_nexthop), print_default_cb },
1458 { NULL,		0, 0, NULL}};
1459 
1460 static const struct option overlay_create_lopts[] = {
1461 	{ "encap",	required_argument,	NULL,	'e' },
1462 	{ "prop",	required_argument,	NULL,	'p' },
1463 	{ "search",	required_argument,	NULL,	's' },
1464 	{ "temporary",	no_argument,		NULL,	't' },
1465 	{ "vnetid",	required_argument,	NULL,	'v' },
1466 	{ NULL,		0,			NULL,	0 }
1467 };
1468 
1469 static const struct option overlay_modify_lopts[] = {
1470 	{ "delete-entry",	required_argument,	NULL,	'd' },
1471 	{ "flush-table",	no_argument,		NULL,	'f' },
1472 	{ "set-entry",		required_argument,	NULL,	's' },
1473 	{ NULL,			0,			NULL,	0 }
1474 };
1475 
1476 static const struct option overlay_show_lopts[] = {
1477 	{ "fma",	no_argument,		NULL,	'f' },
1478 	{ "target",	no_argument,		NULL,	't' },
1479 	{ "parsable",	no_argument,		NULL,	'p' },
1480 	{ "parseable",	no_argument,		NULL,	'p' },
1481 	{ "output",	required_argument,	NULL,	'o' },
1482 	{ NULL,		0,			NULL,	0 }
1483 };
1484 
1485 /*
1486  * Structures for dladm show-overlay
1487  */
1488 typedef enum {
1489 	OVERLAY_LINK,
1490 	OVERLAY_PROPERTY,
1491 	OVERLAY_PERM,
1492 	OVERLAY_REQ,
1493 	OVERLAY_VALUE,
1494 	OVERLAY_DEFAULT,
1495 	OVERLAY_POSSIBLE
1496 } overlay_field_index_t;
1497 
1498 static const ofmt_field_t overlay_fields[] = {
1499 /* name,	field width,  index */
1500 { "LINK",	19,	OVERLAY_LINK,		print_overlay_cb },
1501 { "PROPERTY",	19,	OVERLAY_PROPERTY,	print_overlay_cb },
1502 { "PERM",	5,	OVERLAY_PERM,		print_overlay_cb },
1503 { "REQ",	4,	OVERLAY_REQ,		print_overlay_cb },
1504 { "VALUE",	11,	OVERLAY_VALUE,		print_overlay_cb },
1505 { "DEFAULT",	10,	OVERLAY_DEFAULT,	print_overlay_cb },
1506 { "POSSIBLE",	10,	OVERLAY_POSSIBLE,	print_overlay_cb },
1507 { NULL,		0,	0,	NULL }
1508 };
1509 
1510 typedef enum {
1511 	OVERLAY_FMA_LINK,
1512 	OVERLAY_FMA_STATUS,
1513 	OVERLAY_FMA_DETAILS
1514 } overlay_fma_field_index_t;
1515 
1516 static const ofmt_field_t overlay_fma_fields[] = {
1517 { "LINK",	20,	OVERLAY_FMA_LINK,	print_overlay_fma_cb },
1518 { "STATUS",	8,	OVERLAY_FMA_STATUS,	print_overlay_fma_cb },
1519 { "DETAILS",	52,	OVERLAY_FMA_DETAILS,	print_overlay_fma_cb },
1520 { NULL,		0,	0,			NULL }
1521 };
1522 
1523 typedef enum {
1524 	OVERLAY_TARG_LINK,
1525 	OVERLAY_TARG_TARGET,
1526 	OVERLAY_TARG_DEST
1527 } overlay_targ_field_index_t;
1528 
1529 static const ofmt_field_t overlay_targ_fields[] = {
1530 { "LINK",		20,	OVERLAY_TARG_LINK,	print_overlay_targ_cb },
1531 { "TARGET",		18,	OVERLAY_TARG_TARGET,	print_overlay_targ_cb },
1532 { "DESTINATION",	42,	OVERLAY_TARG_DEST,	print_overlay_targ_cb },
1533 { NULL,			0,	0,			NULL }
1534 };
1535 
1536 static char *progname;
1537 static sig_atomic_t signalled;
1538 
1539 /*
1540  * Handle to libdladm.  Opened in main() before the sub-command
1541  * specific function is called.
1542  */
1543 static dladm_handle_t handle = NULL;
1544 
1545 /*
1546  * Global error list that all routines can use. It's initialized by the main
1547  * code.
1548  */
1549 static dladm_errlist_t errlist;
1550 
1551 #define	DLADM_ETHERSTUB_NAME	"etherstub"
1552 #define	DLADM_IS_ETHERSTUB(id)	(id == DATALINK_INVALID_LINKID)
1553 
1554 static void
1555 usage_text(void)
1556 {
1557 	int	i;
1558 	cmd_t	*cmdp;
1559 	(void) fprintf(stderr, gettext("usage:  dladm <subcommand> <args> ..."
1560 	    "\n"));
1561 	for (i = 0; i < sizeof (cmds) / sizeof (cmds[0]); i++) {
1562 		cmdp = &cmds[i];
1563 		if (cmdp->c_usage != NULL)
1564 			(void) fprintf(stderr, "%s\n", gettext(cmdp->c_usage));
1565 	}
1566 }
1567 
1568 static void
1569 usage(void)
1570 {
1571 	usage_text();
1572 
1573 	/* close dladm handle if it was opened */
1574 	if (handle != NULL)
1575 		dladm_close(handle);
1576 
1577 	exit(EXIT_FAILURE);
1578 }
1579 
1580 static void
1581 do_help(int argc __unused, char *argv[] __unused, const char *use __unused)
1582 {
1583 	usage_text();
1584 }
1585 
1586 int
1587 main(int argc, char *argv[])
1588 {
1589 	int	i;
1590 	cmd_t	*cmdp;
1591 	dladm_status_t status;
1592 
1593 	(void) setlocale(LC_ALL, "");
1594 #if !defined(TEXT_DOMAIN)
1595 #define	TEXT_DOMAIN "SYS_TEST"
1596 #endif
1597 	(void) textdomain(TEXT_DOMAIN);
1598 
1599 	if ((progname = strrchr(argv[0], '/')) == NULL)
1600 		progname = argv[0];
1601 	else
1602 		progname++;
1603 
1604 	if (argc < 2) {
1605 		argv[1] = "show-link";
1606 		argc = 2;
1607 	}
1608 
1609 	for (i = 0; i < sizeof (cmds) / sizeof (cmds[0]); i++) {
1610 		cmdp = &cmds[i];
1611 		if (strcmp(argv[1], cmdp->c_name) == 0) {
1612 			/* Open the libdladm handle */
1613 			if ((status = dladm_open(&handle)) != DLADM_STATUS_OK) {
1614 				die_dlerr(status,
1615 				    "could not open /dev/dld");
1616 			}
1617 
1618 			dladm_errlist_init(&errlist);
1619 
1620 			cmdp->c_fn(argc - 1, &argv[1], cmdp->c_usage);
1621 
1622 			dladm_close(handle);
1623 			return (EXIT_SUCCESS);
1624 		}
1625 	}
1626 
1627 	(void) fprintf(stderr, gettext("%s: unknown subcommand '%s'\n"),
1628 	    progname, argv[1]);
1629 	usage();
1630 	return (EXIT_FAILURE);
1631 }
1632 
1633 /*ARGSUSED*/
1634 static int
1635 show_usage_date(dladm_usage_t *usage, void *arg)
1636 {
1637 	show_usage_state_t	*state = (show_usage_state_t *)arg;
1638 	time_t			stime;
1639 	char			timebuf[20];
1640 	dladm_status_t		status;
1641 	uint32_t		flags;
1642 
1643 	/*
1644 	 * Only show usage information for existing links unless '-a'
1645 	 * is specified.
1646 	 */
1647 	if (!state->us_showall) {
1648 		if ((status = dladm_name2info(handle, usage->du_name,
1649 		    NULL, &flags, NULL, NULL)) != DLADM_STATUS_OK) {
1650 			return (status);
1651 		}
1652 		if ((flags & DLADM_OPT_ACTIVE) == 0)
1653 			return (DLADM_STATUS_LINKINVAL);
1654 	}
1655 
1656 	stime = usage->du_stime;
1657 	(void) strftime(timebuf, sizeof (timebuf), "%m/%d/%Y",
1658 	    localtime(&stime));
1659 	(void) printf("%s\n", timebuf);
1660 
1661 	return (DLADM_STATUS_OK);
1662 }
1663 
1664 static int
1665 show_usage_time(dladm_usage_t *usage, void *arg)
1666 {
1667 	show_usage_state_t	*state = (show_usage_state_t *)arg;
1668 	char			buf[DLADM_STRSIZE];
1669 	usage_l_fields_buf_t	ubuf;
1670 	time_t			time;
1671 	double			bw;
1672 	dladm_status_t		status;
1673 	uint32_t		flags;
1674 
1675 	/*
1676 	 * Only show usage information for existing links unless '-a'
1677 	 * is specified.
1678 	 */
1679 	if (!state->us_showall) {
1680 		if ((status = dladm_name2info(handle, usage->du_name,
1681 		    NULL, &flags, NULL, NULL)) != DLADM_STATUS_OK) {
1682 			return (status);
1683 		}
1684 		if ((flags & DLADM_OPT_ACTIVE) == 0)
1685 			return (DLADM_STATUS_LINKINVAL);
1686 	}
1687 
1688 	if (state->us_plot) {
1689 		if (!state->us_printheader) {
1690 			if (state->us_first) {
1691 				(void) printf("# Time");
1692 				state->us_first = B_FALSE;
1693 			}
1694 			(void) printf(" %s", usage->du_name);
1695 			if (usage->du_last) {
1696 				(void) printf("\n");
1697 				state->us_first = B_TRUE;
1698 				state->us_printheader = B_TRUE;
1699 			}
1700 		} else {
1701 			if (state->us_first) {
1702 				time = usage->du_etime;
1703 				(void) strftime(buf, sizeof (buf), "%T",
1704 				    localtime(&time));
1705 				state->us_first = B_FALSE;
1706 				(void) printf("%s", buf);
1707 			}
1708 			bw = (double)usage->du_bandwidth/1000;
1709 			(void) printf(" %.2f", bw);
1710 			if (usage->du_last) {
1711 				(void) printf("\n");
1712 				state->us_first = B_TRUE;
1713 			}
1714 		}
1715 		return (DLADM_STATUS_OK);
1716 	}
1717 
1718 	bzero(&ubuf, sizeof (ubuf));
1719 
1720 	(void) snprintf(ubuf.usage_l_link, sizeof (ubuf.usage_l_link), "%s",
1721 	    usage->du_name);
1722 	time = usage->du_stime;
1723 	(void) strftime(buf, sizeof (buf), "%T", localtime(&time));
1724 	(void) snprintf(ubuf.usage_l_stime, sizeof (ubuf.usage_l_stime), "%s",
1725 	    buf);
1726 	time = usage->du_etime;
1727 	(void) strftime(buf, sizeof (buf), "%T", localtime(&time));
1728 	(void) snprintf(ubuf.usage_l_etime, sizeof (ubuf.usage_l_etime), "%s",
1729 	    buf);
1730 	(void) snprintf(ubuf.usage_l_rbytes, sizeof (ubuf.usage_l_rbytes),
1731 	    "%llu", usage->du_rbytes);
1732 	(void) snprintf(ubuf.usage_l_obytes, sizeof (ubuf.usage_l_obytes),
1733 	    "%llu", usage->du_obytes);
1734 	(void) snprintf(ubuf.usage_l_bandwidth, sizeof (ubuf.usage_l_bandwidth),
1735 	    "%s Mbps", dladm_bw2str(usage->du_bandwidth, buf));
1736 
1737 	ofmt_print(state->us_ofmt, &ubuf);
1738 	return (DLADM_STATUS_OK);
1739 }
1740 
1741 static int
1742 show_usage_res(dladm_usage_t *usage, void *arg)
1743 {
1744 	show_usage_state_t	*state = (show_usage_state_t *)arg;
1745 	char			buf[DLADM_STRSIZE];
1746 	usage_fields_buf_t	ubuf;
1747 	dladm_status_t		status;
1748 	uint32_t		flags;
1749 
1750 	/*
1751 	 * Only show usage information for existing links unless '-a'
1752 	 * is specified.
1753 	 */
1754 	if (!state->us_showall) {
1755 		if ((status = dladm_name2info(handle, usage->du_name,
1756 		    NULL, &flags, NULL, NULL)) != DLADM_STATUS_OK) {
1757 			return (status);
1758 		}
1759 		if ((flags & DLADM_OPT_ACTIVE) == 0)
1760 			return (DLADM_STATUS_LINKINVAL);
1761 	}
1762 
1763 	bzero(&ubuf, sizeof (ubuf));
1764 
1765 	(void) snprintf(ubuf.usage_link, sizeof (ubuf.usage_link), "%s",
1766 	    usage->du_name);
1767 	(void) snprintf(ubuf.usage_duration, sizeof (ubuf.usage_duration),
1768 	    "%llu", usage->du_duration);
1769 	(void) snprintf(ubuf.usage_ipackets, sizeof (ubuf.usage_ipackets),
1770 	    "%llu", usage->du_ipackets);
1771 	(void) snprintf(ubuf.usage_rbytes, sizeof (ubuf.usage_rbytes),
1772 	    "%llu", usage->du_rbytes);
1773 	(void) snprintf(ubuf.usage_opackets, sizeof (ubuf.usage_opackets),
1774 	    "%llu", usage->du_opackets);
1775 	(void) snprintf(ubuf.usage_obytes, sizeof (ubuf.usage_obytes),
1776 	    "%llu", usage->du_obytes);
1777 	(void) snprintf(ubuf.usage_bandwidth, sizeof (ubuf.usage_bandwidth),
1778 	    "%s Mbps", dladm_bw2str(usage->du_bandwidth, buf));
1779 
1780 	ofmt_print(state->us_ofmt, &ubuf);
1781 
1782 	return (DLADM_STATUS_OK);
1783 }
1784 
1785 static boolean_t
1786 valid_formatspec(char *formatspec_str)
1787 {
1788 	if (strcmp(formatspec_str, "gnuplot") == 0)
1789 		return (B_TRUE);
1790 	return (B_FALSE);
1791 
1792 }
1793 
1794 /*ARGSUSED*/
1795 static void
1796 do_show_usage(int argc, char *argv[], const char *use)
1797 {
1798 	char			*file = NULL;
1799 	int			opt;
1800 	dladm_status_t		status;
1801 	boolean_t		d_arg = B_FALSE;
1802 	char			*stime = NULL;
1803 	char			*etime = NULL;
1804 	char			*resource = NULL;
1805 	show_usage_state_t	state;
1806 	boolean_t		o_arg = B_FALSE;
1807 	boolean_t		F_arg = B_FALSE;
1808 	char			*fields_str = NULL;
1809 	char			*formatspec_str = NULL;
1810 	char			*all_l_fields =
1811 	    "link,start,end,rbytes,obytes,bandwidth";
1812 	ofmt_handle_t		ofmt;
1813 	ofmt_status_t		oferr;
1814 	uint_t			ofmtflags = 0;
1815 
1816 	bzero(&state, sizeof (show_usage_state_t));
1817 	state.us_parsable = B_FALSE;
1818 	state.us_printheader = B_FALSE;
1819 	state.us_plot = B_FALSE;
1820 	state.us_first = B_TRUE;
1821 
1822 	while ((opt = getopt_long(argc, argv, "das:e:o:f:F:",
1823 	    usage_opts, NULL)) != -1) {
1824 		switch (opt) {
1825 		case 'd':
1826 			d_arg = B_TRUE;
1827 			break;
1828 		case 'a':
1829 			state.us_showall = B_TRUE;
1830 			break;
1831 		case 'f':
1832 			file = optarg;
1833 			break;
1834 		case 's':
1835 			stime = optarg;
1836 			break;
1837 		case 'e':
1838 			etime = optarg;
1839 			break;
1840 		case 'o':
1841 			o_arg = B_TRUE;
1842 			fields_str = optarg;
1843 			break;
1844 		case 'F':
1845 			state.us_plot = F_arg = B_TRUE;
1846 			formatspec_str = optarg;
1847 			break;
1848 		default:
1849 			die_opterr(optopt, opt, use);
1850 			break;
1851 		}
1852 	}
1853 
1854 	if (file == NULL)
1855 		die("show-usage requires a file");
1856 
1857 	if (optind == (argc-1)) {
1858 		uint32_t	flags;
1859 
1860 		resource = argv[optind];
1861 		if (!state.us_showall &&
1862 		    (((status = dladm_name2info(handle, resource, NULL, &flags,
1863 		    NULL, NULL)) != DLADM_STATUS_OK) ||
1864 		    ((flags & DLADM_OPT_ACTIVE) == 0))) {
1865 			die("invalid link: '%s'", resource);
1866 		}
1867 	}
1868 
1869 	if (F_arg && d_arg)
1870 		die("incompatible -d and -F options");
1871 
1872 	if (F_arg && valid_formatspec(formatspec_str) == B_FALSE)
1873 		die("Format specifier %s not supported", formatspec_str);
1874 
1875 	if (state.us_parsable)
1876 		ofmtflags |= OFMT_PARSABLE;
1877 
1878 	if (resource == NULL && stime == NULL && etime == NULL) {
1879 		oferr = ofmt_open(fields_str, usage_fields, ofmtflags, 0,
1880 		    &ofmt);
1881 	} else {
1882 		if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0))
1883 			fields_str = all_l_fields;
1884 		oferr = ofmt_open(fields_str, usage_l_fields, ofmtflags, 0,
1885 		    &ofmt);
1886 
1887 	}
1888 	ofmt_check(oferr, state.us_parsable, ofmt, die, warn);
1889 	state.us_ofmt = ofmt;
1890 
1891 	if (d_arg) {
1892 		/* Print log dates */
1893 		status = dladm_usage_dates(show_usage_date,
1894 		    DLADM_LOGTYPE_LINK, file, resource, &state);
1895 	} else if (resource == NULL && stime == NULL && etime == NULL &&
1896 	    !F_arg) {
1897 		/* Print summary */
1898 		status = dladm_usage_summary(show_usage_res,
1899 		    DLADM_LOGTYPE_LINK, file, &state);
1900 	} else if (resource != NULL) {
1901 		/* Print log entries for named resource */
1902 		status = dladm_walk_usage_res(show_usage_time,
1903 		    DLADM_LOGTYPE_LINK, file, resource, stime, etime, &state);
1904 	} else {
1905 		/* Print time and information for each link */
1906 		status = dladm_walk_usage_time(show_usage_time,
1907 		    DLADM_LOGTYPE_LINK, file, stime, etime, &state);
1908 	}
1909 
1910 	if (status != DLADM_STATUS_OK)
1911 		die_dlerr(status, "show-usage");
1912 	ofmt_close(ofmt);
1913 }
1914 
1915 static void
1916 do_create_aggr(int argc, char *argv[], const char *use)
1917 {
1918 	int			option;
1919 	int			key = 0;
1920 	uint32_t		policy = AGGR_POLICY_L4;
1921 	aggr_lacp_mode_t	lacp_mode = AGGR_LACP_OFF;
1922 	aggr_lacp_timer_t	lacp_timer = AGGR_LACP_TIMER_SHORT;
1923 	dladm_aggr_port_attr_db_t	port[MAXPORT];
1924 	uint_t			n, ndev, nlink;
1925 	uint8_t			mac_addr[ETHERADDRL];
1926 	boolean_t		mac_addr_fixed = B_FALSE;
1927 	boolean_t		P_arg = B_FALSE;
1928 	boolean_t		l_arg = B_FALSE;
1929 	boolean_t		u_arg = B_FALSE;
1930 	boolean_t		T_arg = B_FALSE;
1931 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
1932 	char			*altroot = NULL;
1933 	char			name[MAXLINKNAMELEN];
1934 	char			*devs[MAXPORT];
1935 	char			*links[MAXPORT];
1936 	dladm_status_t		status;
1937 	dladm_status_t		pstatus;
1938 	char			propstr[DLADM_STRSIZE];
1939 	dladm_arg_list_t	*proplist = NULL;
1940 	int			i;
1941 	datalink_id_t		linkid;
1942 
1943 	ndev = nlink = opterr = 0;
1944 	bzero(propstr, DLADM_STRSIZE);
1945 
1946 	while ((option = getopt_long(argc, argv, ":d:l:L:P:R:tfu:T:p:",
1947 	    lopts, NULL)) != -1) {
1948 		switch (option) {
1949 		case 'd':
1950 			if (ndev + nlink >= MAXPORT)
1951 				die("too many ports specified");
1952 
1953 			devs[ndev++] = optarg;
1954 			break;
1955 		case 'P':
1956 			if (P_arg)
1957 				die_optdup(option);
1958 
1959 			P_arg = B_TRUE;
1960 			if (!dladm_aggr_str2policy(optarg, &policy))
1961 				die("invalid policy '%s'", optarg);
1962 			break;
1963 		case 'u':
1964 			if (u_arg)
1965 				die_optdup(option);
1966 
1967 			u_arg = B_TRUE;
1968 			if (!dladm_aggr_str2macaddr(optarg, &mac_addr_fixed,
1969 			    mac_addr))
1970 				die("invalid MAC address '%s'", optarg);
1971 			break;
1972 		case 'l':
1973 			if (isdigit(optarg[strlen(optarg) - 1])) {
1974 
1975 				/*
1976 				 * Ended with digit, possibly a link name.
1977 				 */
1978 				if (ndev + nlink >= MAXPORT)
1979 					die("too many ports specified");
1980 
1981 				links[nlink++] = optarg;
1982 				break;
1983 			}
1984 			/* FALLTHROUGH */
1985 		case 'L':
1986 			if (l_arg)
1987 				die_optdup(option);
1988 
1989 			l_arg = B_TRUE;
1990 			if (!dladm_aggr_str2lacpmode(optarg, &lacp_mode))
1991 				die("invalid LACP mode '%s'", optarg);
1992 			break;
1993 		case 'T':
1994 			if (T_arg)
1995 				die_optdup(option);
1996 
1997 			T_arg = B_TRUE;
1998 			if (!dladm_aggr_str2lacptimer(optarg, &lacp_timer))
1999 				die("invalid LACP timer value '%s'", optarg);
2000 			break;
2001 		case 't':
2002 			flags &= ~DLADM_OPT_PERSIST;
2003 			break;
2004 		case 'f':
2005 			flags |= DLADM_OPT_FORCE;
2006 			break;
2007 		case 'R':
2008 			altroot = optarg;
2009 			break;
2010 		case 'p':
2011 			(void) strlcat(propstr, optarg, DLADM_STRSIZE);
2012 			if (strlcat(propstr, ",", DLADM_STRSIZE) >=
2013 			    DLADM_STRSIZE)
2014 				die("property list too long '%s'", propstr);
2015 			break;
2016 
2017 		default:
2018 			die_opterr(optopt, option, use);
2019 			break;
2020 		}
2021 	}
2022 
2023 	if (ndev + nlink == 0)
2024 		usage();
2025 
2026 	/* get key value or the aggregation name (required last argument) */
2027 	if (optind != (argc-1))
2028 		usage();
2029 
2030 	if (!str2int(argv[optind], &key)) {
2031 		if (strlcpy(name, argv[optind], MAXLINKNAMELEN) >=
2032 		    MAXLINKNAMELEN) {
2033 			die("link name too long '%s'", argv[optind]);
2034 		}
2035 
2036 		if (!dladm_valid_linkname(name))
2037 			die("invalid link name '%s'", argv[optind]);
2038 	} else {
2039 		(void) snprintf(name, MAXLINKNAMELEN, "aggr%d", key);
2040 	}
2041 
2042 	if (altroot != NULL)
2043 		altroot_cmd(altroot, argc, argv);
2044 
2045 	for (n = 0; n < ndev; n++) {
2046 		if ((status = dladm_dev2linkid(handle, devs[n],
2047 		    &port[n].lp_linkid)) != DLADM_STATUS_OK) {
2048 			die_dlerr(status, "invalid dev name '%s'", devs[n]);
2049 		}
2050 	}
2051 
2052 	for (n = 0; n < nlink; n++) {
2053 		if ((status = dladm_name2info(handle, links[n],
2054 		    &port[ndev + n].lp_linkid, NULL, NULL, NULL)) !=
2055 		    DLADM_STATUS_OK) {
2056 			die_dlerr(status, "invalid link name '%s'", links[n]);
2057 		}
2058 	}
2059 
2060 	status = dladm_aggr_create(handle, name, key, ndev + nlink, port,
2061 	    policy, mac_addr_fixed, (const uchar_t *)mac_addr, lacp_mode,
2062 	    lacp_timer, flags);
2063 	if (status != DLADM_STATUS_OK)
2064 		goto done;
2065 
2066 	if (dladm_parse_link_props(propstr, &proplist, B_FALSE)
2067 	    != DLADM_STATUS_OK)
2068 		die("invalid aggregation property");
2069 
2070 	if (proplist == NULL)
2071 		return;
2072 
2073 	status = dladm_name2info(handle, name, &linkid, NULL, NULL, NULL);
2074 	if (status != DLADM_STATUS_OK)
2075 		goto done;
2076 
2077 	for (i = 0; i < proplist->al_count; i++) {
2078 		dladm_arg_info_t	*aip = &proplist->al_info[i];
2079 
2080 		pstatus = dladm_set_linkprop(handle, linkid, aip->ai_name,
2081 		    aip->ai_val, aip->ai_count, flags);
2082 
2083 		if (pstatus != DLADM_STATUS_OK) {
2084 			die_dlerr(pstatus,
2085 			    "aggr creation succeeded but "
2086 			    "could not set property '%s'", aip->ai_name);
2087 		}
2088 	}
2089 done:
2090 	dladm_free_props(proplist);
2091 	if (status != DLADM_STATUS_OK) {
2092 		if (status == DLADM_STATUS_NONOTIF) {
2093 			die("not all links have link up/down detection; must "
2094 			    "use -f (see dladm(8))");
2095 		} else {
2096 			die_dlerr(status, "create operation failed");
2097 		}
2098 	}
2099 }
2100 
2101 /*
2102  * arg is either the key or the aggr name. Validate it and convert it to
2103  * the linkid if altroot is NULL.
2104  */
2105 static dladm_status_t
2106 i_dladm_aggr_get_linkid(const char *altroot, const char *arg,
2107     datalink_id_t *linkidp, uint32_t flags)
2108 {
2109 	int		key = 0;
2110 	char		*aggr = NULL;
2111 	dladm_status_t	status;
2112 
2113 	if (!str2int(arg, &key))
2114 		aggr = (char *)arg;
2115 
2116 	if (aggr == NULL && key == 0)
2117 		return (DLADM_STATUS_LINKINVAL);
2118 
2119 	if (altroot != NULL)
2120 		return (DLADM_STATUS_OK);
2121 
2122 	if (aggr != NULL) {
2123 		status = dladm_name2info(handle, aggr, linkidp, NULL, NULL,
2124 		    NULL);
2125 	} else {
2126 		status = dladm_key2linkid(handle, key, linkidp, flags);
2127 	}
2128 
2129 	return (status);
2130 }
2131 
2132 static void
2133 do_delete_aggr(int argc, char *argv[], const char *use)
2134 {
2135 	int			option;
2136 	char			*altroot = NULL;
2137 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
2138 	dladm_status_t		status;
2139 	datalink_id_t		linkid;
2140 
2141 	opterr = 0;
2142 	while ((option = getopt_long(argc, argv, ":R:t", lopts, NULL)) != -1) {
2143 		switch (option) {
2144 		case 't':
2145 			flags &= ~DLADM_OPT_PERSIST;
2146 			break;
2147 		case 'R':
2148 			altroot = optarg;
2149 			break;
2150 		default:
2151 			die_opterr(optopt, option, use);
2152 			break;
2153 		}
2154 	}
2155 
2156 	/* get key value or the aggregation name (required last argument) */
2157 	if (optind != (argc-1))
2158 		usage();
2159 
2160 	status = i_dladm_aggr_get_linkid(altroot, argv[optind], &linkid, flags);
2161 	if (status != DLADM_STATUS_OK)
2162 		goto done;
2163 
2164 	if (altroot != NULL)
2165 		altroot_cmd(altroot, argc, argv);
2166 
2167 	status = dladm_aggr_delete(handle, linkid, flags);
2168 done:
2169 	if (status != DLADM_STATUS_OK)
2170 		die_dlerr(status, "delete operation failed");
2171 }
2172 
2173 static void
2174 do_add_aggr(int argc, char *argv[], const char *use)
2175 {
2176 	int			option;
2177 	uint_t			n, ndev, nlink;
2178 	char			*altroot = NULL;
2179 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
2180 	datalink_id_t		linkid;
2181 	dladm_status_t		status;
2182 	dladm_aggr_port_attr_db_t	port[MAXPORT];
2183 	char			*devs[MAXPORT];
2184 	char			*links[MAXPORT];
2185 
2186 	ndev = nlink = opterr = 0;
2187 	while ((option = getopt_long(argc, argv, ":d:l:R:tf", lopts,
2188 	    NULL)) != -1) {
2189 		switch (option) {
2190 		case 'd':
2191 			if (ndev + nlink >= MAXPORT)
2192 				die("too many ports specified");
2193 
2194 			devs[ndev++] = optarg;
2195 			break;
2196 		case 'l':
2197 			if (ndev + nlink >= MAXPORT)
2198 				die("too many ports specified");
2199 
2200 			links[nlink++] = optarg;
2201 			break;
2202 		case 't':
2203 			flags &= ~DLADM_OPT_PERSIST;
2204 			break;
2205 		case 'f':
2206 			flags |= DLADM_OPT_FORCE;
2207 			break;
2208 		case 'R':
2209 			altroot = optarg;
2210 			break;
2211 		default:
2212 			die_opterr(optopt, option, use);
2213 			break;
2214 		}
2215 	}
2216 
2217 	if (ndev + nlink == 0)
2218 		usage();
2219 
2220 	/* get key value or the aggregation name (required last argument) */
2221 	if (optind != (argc-1))
2222 		usage();
2223 
2224 	if ((status = i_dladm_aggr_get_linkid(altroot, argv[optind], &linkid,
2225 	    flags & (DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST))) !=
2226 	    DLADM_STATUS_OK) {
2227 		goto done;
2228 	}
2229 
2230 	if (altroot != NULL)
2231 		altroot_cmd(altroot, argc, argv);
2232 
2233 	for (n = 0; n < ndev; n++) {
2234 		if ((status = dladm_dev2linkid(handle, devs[n],
2235 		    &(port[n].lp_linkid))) != DLADM_STATUS_OK) {
2236 			die_dlerr(status, "invalid <dev> '%s'", devs[n]);
2237 		}
2238 	}
2239 
2240 	for (n = 0; n < nlink; n++) {
2241 		if ((status = dladm_name2info(handle, links[n],
2242 		    &port[n + ndev].lp_linkid, NULL, NULL, NULL)) !=
2243 		    DLADM_STATUS_OK) {
2244 			die_dlerr(status, "invalid <link> '%s'", links[n]);
2245 		}
2246 	}
2247 
2248 	status = dladm_aggr_add(handle, linkid, ndev + nlink, port, flags);
2249 done:
2250 	if (status != DLADM_STATUS_OK) {
2251 		/*
2252 		 * checking DLADM_STATUS_NOTSUP is a temporary workaround
2253 		 * and should be removed once 6399681 is fixed.
2254 		 */
2255 		if (status == DLADM_STATUS_NOTSUP) {
2256 			die("add operation failed: link capabilities don't "
2257 			    "match");
2258 		} else if (status == DLADM_STATUS_NONOTIF) {
2259 			die("not all links have link up/down detection; must "
2260 			    "use -f (see dladm(8))");
2261 		} else {
2262 			die_dlerr(status, "add operation failed");
2263 		}
2264 	}
2265 }
2266 
2267 static void
2268 do_remove_aggr(int argc, char *argv[], const char *use)
2269 {
2270 	int				option;
2271 	dladm_aggr_port_attr_db_t	port[MAXPORT];
2272 	uint_t				n, ndev, nlink;
2273 	char				*devs[MAXPORT];
2274 	char				*links[MAXPORT];
2275 	char				*altroot = NULL;
2276 	uint32_t			flags;
2277 	datalink_id_t			linkid;
2278 	dladm_status_t			status;
2279 
2280 	flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
2281 	ndev = nlink = opterr = 0;
2282 	while ((option = getopt_long(argc, argv, ":d:l:R:t",
2283 	    lopts, NULL)) != -1) {
2284 		switch (option) {
2285 		case 'd':
2286 			if (ndev + nlink >= MAXPORT)
2287 				die("too many ports specified");
2288 
2289 			devs[ndev++] = optarg;
2290 			break;
2291 		case 'l':
2292 			if (ndev + nlink >= MAXPORT)
2293 				die("too many ports specified");
2294 
2295 			links[nlink++] = optarg;
2296 			break;
2297 		case 't':
2298 			flags &= ~DLADM_OPT_PERSIST;
2299 			break;
2300 		case 'R':
2301 			altroot = optarg;
2302 			break;
2303 		default:
2304 			die_opterr(optopt, option, use);
2305 			break;
2306 		}
2307 	}
2308 
2309 	if (ndev + nlink == 0)
2310 		usage();
2311 
2312 	/* get key value or the aggregation name (required last argument) */
2313 	if (optind != (argc-1))
2314 		usage();
2315 
2316 	status = i_dladm_aggr_get_linkid(altroot, argv[optind], &linkid, flags);
2317 	if (status != DLADM_STATUS_OK)
2318 		goto done;
2319 
2320 	if (altroot != NULL)
2321 		altroot_cmd(altroot, argc, argv);
2322 
2323 	for (n = 0; n < ndev; n++) {
2324 		if ((status = dladm_dev2linkid(handle, devs[n],
2325 		    &(port[n].lp_linkid))) != DLADM_STATUS_OK) {
2326 			die_dlerr(status, "invalid <dev> '%s'", devs[n]);
2327 		}
2328 	}
2329 
2330 	for (n = 0; n < nlink; n++) {
2331 		if ((status = dladm_name2info(handle, links[n],
2332 		    &port[n + ndev].lp_linkid, NULL, NULL, NULL)) !=
2333 		    DLADM_STATUS_OK) {
2334 			die_dlerr(status, "invalid <link> '%s'", links[n]);
2335 		}
2336 	}
2337 
2338 	status = dladm_aggr_remove(handle, linkid, ndev + nlink, port, flags);
2339 done:
2340 	if (status != DLADM_STATUS_OK)
2341 		die_dlerr(status, "remove operation failed");
2342 }
2343 
2344 static void
2345 do_modify_aggr(int argc, char *argv[], const char *use)
2346 {
2347 	int			option;
2348 	uint32_t		policy = AGGR_POLICY_L4;
2349 	aggr_lacp_mode_t	lacp_mode = AGGR_LACP_OFF;
2350 	aggr_lacp_timer_t	lacp_timer = AGGR_LACP_TIMER_SHORT;
2351 	uint8_t			mac_addr[ETHERADDRL];
2352 	boolean_t		mac_addr_fixed = B_FALSE;
2353 	uint8_t			modify_mask = 0;
2354 	char			*altroot = NULL;
2355 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
2356 	datalink_id_t		linkid;
2357 	dladm_status_t		status;
2358 
2359 	opterr = 0;
2360 	while ((option = getopt_long(argc, argv, ":L:l:P:R:tu:T:", lopts,
2361 	    NULL)) != -1) {
2362 		switch (option) {
2363 		case 'P':
2364 			if (modify_mask & DLADM_AGGR_MODIFY_POLICY)
2365 				die_optdup(option);
2366 
2367 			modify_mask |= DLADM_AGGR_MODIFY_POLICY;
2368 
2369 			if (!dladm_aggr_str2policy(optarg, &policy))
2370 				die("invalid policy '%s'", optarg);
2371 			break;
2372 		case 'u':
2373 			if (modify_mask & DLADM_AGGR_MODIFY_MAC)
2374 				die_optdup(option);
2375 
2376 			modify_mask |= DLADM_AGGR_MODIFY_MAC;
2377 
2378 			if (!dladm_aggr_str2macaddr(optarg, &mac_addr_fixed,
2379 			    mac_addr))
2380 				die("invalid MAC address '%s'", optarg);
2381 			break;
2382 		case 'l':
2383 		case 'L':
2384 			if (modify_mask & DLADM_AGGR_MODIFY_LACP_MODE)
2385 				die_optdup(option);
2386 
2387 			modify_mask |= DLADM_AGGR_MODIFY_LACP_MODE;
2388 
2389 			if (!dladm_aggr_str2lacpmode(optarg, &lacp_mode))
2390 				die("invalid LACP mode '%s'", optarg);
2391 			break;
2392 		case 'T':
2393 			if (modify_mask & DLADM_AGGR_MODIFY_LACP_TIMER)
2394 				die_optdup(option);
2395 
2396 			modify_mask |= DLADM_AGGR_MODIFY_LACP_TIMER;
2397 
2398 			if (!dladm_aggr_str2lacptimer(optarg, &lacp_timer))
2399 				die("invalid LACP timer value '%s'", optarg);
2400 			break;
2401 		case 't':
2402 			flags &= ~DLADM_OPT_PERSIST;
2403 			break;
2404 		case 'R':
2405 			altroot = optarg;
2406 			break;
2407 		default:
2408 			die_opterr(optopt, option, use);
2409 			break;
2410 		}
2411 	}
2412 
2413 	if (modify_mask == 0)
2414 		die("at least one of the -PulT options must be specified");
2415 
2416 	/* get key value or the aggregation name (required last argument) */
2417 	if (optind != (argc-1))
2418 		usage();
2419 
2420 	status = i_dladm_aggr_get_linkid(altroot, argv[optind], &linkid, flags);
2421 	if (status != DLADM_STATUS_OK)
2422 		goto done;
2423 
2424 	if (altroot != NULL)
2425 		altroot_cmd(altroot, argc, argv);
2426 
2427 	status = dladm_aggr_modify(handle, linkid, modify_mask, policy,
2428 	    mac_addr_fixed, (const uchar_t *)mac_addr, lacp_mode, lacp_timer,
2429 	    flags);
2430 
2431 done:
2432 	if (status != DLADM_STATUS_OK)
2433 		die_dlerr(status, "modify operation failed");
2434 }
2435 
2436 /*ARGSUSED*/
2437 static void
2438 do_up_aggr(int argc, char *argv[], const char *use)
2439 {
2440 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
2441 	dladm_status_t	status;
2442 
2443 	/*
2444 	 * get the key or the name of the aggregation (optional last argument)
2445 	 */
2446 	if (argc == 2) {
2447 		if ((status = i_dladm_aggr_get_linkid(NULL, argv[1], &linkid,
2448 		    DLADM_OPT_PERSIST)) != DLADM_STATUS_OK)
2449 			goto done;
2450 	} else if (argc > 2) {
2451 		usage();
2452 	}
2453 
2454 	status = dladm_aggr_up(handle, linkid);
2455 done:
2456 	if (status != DLADM_STATUS_OK) {
2457 		if (argc == 2) {
2458 			die_dlerr(status,
2459 			    "could not bring up aggregation '%s'", argv[1]);
2460 		} else {
2461 			die_dlerr(status, "could not bring aggregations up");
2462 		}
2463 	}
2464 }
2465 
2466 static void
2467 do_create_vlan(int argc, char *argv[], const char *use)
2468 {
2469 	char			*link = NULL;
2470 	char			drv[DLPI_LINKNAME_MAX];
2471 	uint_t			ppa;
2472 	datalink_id_t		linkid;
2473 	datalink_id_t		dev_linkid;
2474 	int			vid = 0;
2475 	int			option;
2476 	uint32_t		flags = (DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST);
2477 	char			*altroot = NULL;
2478 	char			vlan[MAXLINKNAMELEN];
2479 	char			propstr[DLADM_STRSIZE];
2480 	dladm_arg_list_t	*proplist = NULL;
2481 	dladm_status_t		status;
2482 
2483 	opterr = 0;
2484 	bzero(propstr, DLADM_STRSIZE);
2485 
2486 	while ((option = getopt_long(argc, argv, ":tfR:l:v:p:",
2487 	    lopts, NULL)) != -1) {
2488 		switch (option) {
2489 		case 'v':
2490 			if (vid != 0)
2491 				die_optdup(option);
2492 
2493 			if (!str2int(optarg, &vid) || vid < 1 || vid > 4094)
2494 				die("invalid VLAN identifier '%s'", optarg);
2495 
2496 			break;
2497 		case 'l':
2498 			if (link != NULL)
2499 				die_optdup(option);
2500 
2501 			link = optarg;
2502 			break;
2503 		case 't':
2504 			flags &= ~DLADM_OPT_PERSIST;
2505 			break;
2506 		case 'R':
2507 			altroot = optarg;
2508 			break;
2509 		case 'p':
2510 			(void) strlcat(propstr, optarg, DLADM_STRSIZE);
2511 			if (strlcat(propstr, ",", DLADM_STRSIZE) >=
2512 			    DLADM_STRSIZE)
2513 				die("property list too long '%s'", propstr);
2514 			break;
2515 		case 'f':
2516 			flags |= DLADM_OPT_FORCE;
2517 			break;
2518 		default:
2519 			die_opterr(optopt, option, use);
2520 			break;
2521 		}
2522 	}
2523 
2524 	/* get vlan name if there is any */
2525 	if ((vid == 0) || (link == NULL) || (argc - optind > 1))
2526 		usage();
2527 
2528 	if (optind == (argc - 1)) {
2529 		if (strlcpy(vlan, argv[optind], MAXLINKNAMELEN) >=
2530 		    MAXLINKNAMELEN) {
2531 			die("vlan name too long '%s'", argv[optind]);
2532 		}
2533 	} else {
2534 		if ((dlpi_parselink(link, drv, &ppa) != DLPI_SUCCESS) ||
2535 		    (ppa >= 1000) ||
2536 		    (dlpi_makelink(vlan, drv, vid * 1000 + ppa) !=
2537 		    DLPI_SUCCESS)) {
2538 			die("invalid link name '%s'", link);
2539 		}
2540 	}
2541 
2542 	if (altroot != NULL)
2543 		altroot_cmd(altroot, argc, argv);
2544 
2545 	if (dladm_name2info(handle, link, &dev_linkid, NULL, NULL, NULL) !=
2546 	    DLADM_STATUS_OK) {
2547 		die("invalid link name '%s'", link);
2548 	}
2549 
2550 	if (dladm_parse_link_props(propstr, &proplist, B_FALSE)
2551 	    != DLADM_STATUS_OK)
2552 		die("invalid vlan property");
2553 
2554 	status = dladm_vlan_create(handle, vlan, dev_linkid, vid, proplist,
2555 	    flags, &linkid);
2556 	switch (status) {
2557 	case DLADM_STATUS_OK:
2558 		break;
2559 
2560 	case DLADM_STATUS_NOTSUP:
2561 		die("VLAN over '%s' may require lowered MTU; must use -f (see "
2562 		    "dladm(8))", link);
2563 		break;
2564 
2565 	case DLADM_STATUS_LINKBUSY:
2566 		die("VLAN over '%s' may not use default_tag ID "
2567 		    "(see dladm(8))", link);
2568 		break;
2569 
2570 	default:
2571 		die_dlerr(status, "create operation failed");
2572 	}
2573 }
2574 
2575 static void
2576 do_delete_vlan(int argc, char *argv[], const char *use)
2577 {
2578 	int		option;
2579 	uint32_t	flags = (DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST);
2580 	char		*altroot = NULL;
2581 	datalink_id_t	linkid;
2582 	dladm_status_t	status;
2583 
2584 	opterr = 0;
2585 	while ((option = getopt_long(argc, argv, ":R:t", lopts, NULL)) != -1) {
2586 		switch (option) {
2587 		case 't':
2588 			flags &= ~DLADM_OPT_PERSIST;
2589 			break;
2590 		case 'R':
2591 			altroot = optarg;
2592 			break;
2593 		default:
2594 			die_opterr(optopt, option, use);
2595 			break;
2596 		}
2597 	}
2598 
2599 	/* get VLAN link name (required last argument) */
2600 	if (optind != (argc - 1))
2601 		usage();
2602 
2603 	if (altroot != NULL)
2604 		altroot_cmd(altroot, argc, argv);
2605 
2606 	status = dladm_name2info(handle, argv[optind], &linkid, NULL, NULL,
2607 	    NULL);
2608 	if (status != DLADM_STATUS_OK)
2609 		goto done;
2610 
2611 	status = dladm_vlan_delete(handle, linkid, flags);
2612 done:
2613 	if (status != DLADM_STATUS_OK)
2614 		die_dlerr(status, "delete operation failed");
2615 }
2616 
2617 /*ARGSUSED*/
2618 static void
2619 do_up_vlan(int argc, char *argv[], const char *use)
2620 {
2621 	do_up_vnic_common(argc, argv, use, B_TRUE);
2622 }
2623 
2624 static void
2625 do_rename_link(int argc, char *argv[], const char *use)
2626 {
2627 	int		option;
2628 	char		*link1, *link2;
2629 	char		*altroot = NULL;
2630 	dladm_status_t	status;
2631 
2632 	opterr = 0;
2633 	while ((option = getopt_long(argc, argv, ":R:", lopts, NULL)) != -1) {
2634 		switch (option) {
2635 		case 'R':
2636 			altroot = optarg;
2637 			break;
2638 		default:
2639 			die_opterr(optopt, option, use);
2640 			break;
2641 		}
2642 	}
2643 
2644 	/* get link1 and link2 name (required the last 2 arguments) */
2645 	if (optind != (argc - 2))
2646 		usage();
2647 
2648 	if (altroot != NULL)
2649 		altroot_cmd(altroot, argc, argv);
2650 
2651 	link1 = argv[optind++];
2652 	link2 = argv[optind];
2653 	if ((status = dladm_rename_link(handle, link1, link2)) !=
2654 	    DLADM_STATUS_OK)
2655 		die_dlerr(status, "rename operation failed");
2656 }
2657 
2658 /*ARGSUSED*/
2659 static void
2660 do_delete_phys(int argc, char *argv[], const char *use)
2661 {
2662 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
2663 	dladm_status_t	status;
2664 
2665 	/* get link name (required the last argument) */
2666 	if (argc > 2)
2667 		usage();
2668 
2669 	if (argc == 2) {
2670 		if ((status = dladm_name2info(handle, argv[1], &linkid, NULL,
2671 		    NULL, NULL)) != DLADM_STATUS_OK)
2672 			die_dlerr(status, "cannot delete '%s'", argv[1]);
2673 	}
2674 
2675 	if ((status = dladm_phys_delete(handle, linkid)) != DLADM_STATUS_OK) {
2676 		if (argc == 2)
2677 			die_dlerr(status, "cannot delete '%s'", argv[1]);
2678 		else
2679 			die_dlerr(status, "delete operation failed");
2680 	}
2681 }
2682 
2683 /*ARGSUSED*/
2684 static int
2685 i_dladm_walk_linkmap(dladm_handle_t dh, datalink_id_t linkid, void *arg)
2686 {
2687 	char			name[MAXLINKNAMELEN];
2688 	char			mediabuf[DLADM_STRSIZE];
2689 	char			classbuf[DLADM_STRSIZE];
2690 	datalink_class_t	class;
2691 	uint32_t		media;
2692 	uint32_t		flags;
2693 
2694 	if (dladm_datalink_id2info(dh, linkid, &flags, &class, &media, name,
2695 	    MAXLINKNAMELEN) == DLADM_STATUS_OK) {
2696 		(void) dladm_class2str(class, classbuf);
2697 		(void) dladm_media2str(media, mediabuf);
2698 		(void) printf("%-12s%8d  %-12s%-20s %6d\n", name,
2699 		    linkid, classbuf, mediabuf, flags);
2700 	}
2701 	return (DLADM_WALK_CONTINUE);
2702 }
2703 
2704 /*ARGSUSED*/
2705 static void
2706 do_show_linkmap(int argc, char *argv[], const char *use)
2707 {
2708 	if (argc != 1)
2709 		die("invalid arguments");
2710 
2711 	(void) printf("%-12s%8s  %-12s%-20s %6s\n", "NAME", "LINKID",
2712 	    "CLASS", "MEDIA", "FLAGS");
2713 
2714 	(void) dladm_walk_datalink_id(i_dladm_walk_linkmap, handle, NULL,
2715 	    DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE,
2716 	    DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST);
2717 }
2718 
2719 /*
2720  * Delete inactive physical links.
2721  */
2722 /*ARGSUSED*/
2723 static int
2724 purge_phys(dladm_handle_t dh, datalink_id_t linkid, void *arg)
2725 {
2726 	datalink_class_t	class;
2727 	uint32_t		flags;
2728 
2729 	if (dladm_datalink_id2info(dh, linkid, &flags, &class, NULL, NULL, 0)
2730 	    != DLADM_STATUS_OK) {
2731 		return (DLADM_WALK_CONTINUE);
2732 	}
2733 
2734 	if (class == DATALINK_CLASS_PHYS && !(flags & DLADM_OPT_ACTIVE))
2735 		(void) dladm_phys_delete(dh, linkid);
2736 
2737 	return (DLADM_WALK_CONTINUE);
2738 }
2739 
2740 /*ARGSUSED*/
2741 static void
2742 do_init_phys(int argc, char *argv[], const char *use)
2743 {
2744 	di_node_t	devtree;
2745 
2746 	if (argc > 1)
2747 		usage();
2748 
2749 	/*
2750 	 * Force all the devices to attach, therefore all the network physical
2751 	 * devices can be known to the dlmgmtd daemon.
2752 	 */
2753 	if ((devtree = di_init("/", DINFOFORCE | DINFOSUBTREE)) != DI_NODE_NIL)
2754 		di_fini(devtree);
2755 
2756 	(void) dladm_walk_datalink_id(purge_phys, handle, NULL,
2757 	    DATALINK_CLASS_PHYS, DATALINK_ANY_MEDIATYPE, DLADM_OPT_PERSIST);
2758 }
2759 
2760 /*
2761  * Print the active topology information.
2762  */
2763 void
2764 print_link_topology(show_state_t *state, datalink_id_t linkid,
2765     datalink_class_t class, link_fields_buf_t *lbuf)
2766 {
2767 	uint32_t	flags = state->ls_flags;
2768 	dladm_status_t	status;
2769 	char		tmpbuf[MAXLINKNAMELEN];
2770 
2771 	lbuf->link_over[0] = '\0';
2772 	lbuf->link_bridge[0] = '\0';
2773 
2774 	switch (class) {
2775 	case DATALINK_CLASS_AGGR:
2776 	case DATALINK_CLASS_PHYS:
2777 	case DATALINK_CLASS_ETHERSTUB:
2778 		status = dladm_bridge_getlink(handle, linkid, lbuf->link_bridge,
2779 		    sizeof (lbuf->link_bridge));
2780 		if (status != DLADM_STATUS_OK &&
2781 		    status != DLADM_STATUS_NOTFOUND)
2782 			(void) strcpy(lbuf->link_bridge, "?");
2783 		break;
2784 	}
2785 
2786 	switch (class) {
2787 	case DATALINK_CLASS_VLAN: {
2788 		dladm_vlan_attr_t	vinfo;
2789 
2790 		if (dladm_vlan_info(handle, linkid, &vinfo, flags) !=
2791 		    DLADM_STATUS_OK) {
2792 			(void) strcpy(lbuf->link_over, "?");
2793 			break;
2794 		}
2795 		if (dladm_datalink_id2info(handle, vinfo.dv_linkid, NULL, NULL,
2796 		    NULL, lbuf->link_over, sizeof (lbuf->link_over)) !=
2797 		    DLADM_STATUS_OK)
2798 			(void) strcpy(lbuf->link_over, "?");
2799 		break;
2800 	}
2801 	case DATALINK_CLASS_AGGR: {
2802 		dladm_aggr_grp_attr_t	ginfo;
2803 		int			i;
2804 
2805 		if (dladm_aggr_info(handle, linkid, &ginfo, flags) !=
2806 		    DLADM_STATUS_OK || ginfo.lg_nports == 0) {
2807 			(void) strcpy(lbuf->link_over, "?");
2808 			break;
2809 		}
2810 		for (i = 0; i < ginfo.lg_nports; i++) {
2811 			if (dladm_datalink_id2info(handle,
2812 			    ginfo.lg_ports[i].lp_linkid, NULL, NULL, NULL,
2813 			    tmpbuf, sizeof (tmpbuf)) != DLADM_STATUS_OK) {
2814 				(void) strcpy(lbuf->link_over, "?");
2815 				break;
2816 			}
2817 			(void) strlcat(lbuf->link_over, tmpbuf,
2818 			    sizeof (lbuf->link_over));
2819 			if (i != (ginfo.lg_nports - 1)) {
2820 				(void) strlcat(lbuf->link_over, ",",
2821 				    sizeof (lbuf->link_over));
2822 			}
2823 		}
2824 		free(ginfo.lg_ports);
2825 		break;
2826 	}
2827 	case DATALINK_CLASS_VNIC: {
2828 		dladm_vnic_attr_t	vinfo;
2829 
2830 		if (dladm_vnic_info(handle, linkid, &vinfo, flags) !=
2831 		    DLADM_STATUS_OK) {
2832 			(void) strcpy(lbuf->link_over, "?");
2833 			break;
2834 		}
2835 		if (dladm_datalink_id2info(handle, vinfo.va_link_id, NULL, NULL,
2836 		    NULL, lbuf->link_over, sizeof (lbuf->link_over)) !=
2837 		    DLADM_STATUS_OK)
2838 			(void) strcpy(lbuf->link_over, "?");
2839 		break;
2840 	}
2841 
2842 	case DATALINK_CLASS_PART: {
2843 		dladm_part_attr_t	pinfo;
2844 
2845 		if (dladm_part_info(handle, linkid, &pinfo, flags) !=
2846 		    DLADM_STATUS_OK) {
2847 			(void) strcpy(lbuf->link_over, "?");
2848 			break;
2849 		}
2850 		if (dladm_datalink_id2info(handle, pinfo.dia_physlinkid, NULL,
2851 		    NULL, NULL, lbuf->link_over, sizeof (lbuf->link_over)) !=
2852 		    DLADM_STATUS_OK)
2853 			(void) strcpy(lbuf->link_over, "?");
2854 		break;
2855 	}
2856 
2857 	case DATALINK_CLASS_BRIDGE: {
2858 		datalink_id_t *dlp;
2859 		uint_t i, nports;
2860 
2861 		if (dladm_datalink_id2info(handle, linkid, NULL, NULL,
2862 		    NULL, tmpbuf, sizeof (tmpbuf)) != DLADM_STATUS_OK) {
2863 			(void) strcpy(lbuf->link_over, "?");
2864 			break;
2865 		}
2866 		if (tmpbuf[0] != '\0')
2867 			tmpbuf[strlen(tmpbuf) - 1] = '\0';
2868 		dlp = dladm_bridge_get_portlist(tmpbuf, &nports);
2869 		if (dlp == NULL) {
2870 			(void) strcpy(lbuf->link_over, "?");
2871 			break;
2872 		}
2873 		for (i = 0; i < nports; i++) {
2874 			if (dladm_datalink_id2info(handle, dlp[i], NULL,
2875 			    NULL, NULL, tmpbuf, sizeof (tmpbuf)) !=
2876 			    DLADM_STATUS_OK) {
2877 				(void) strcpy(lbuf->link_over, "?");
2878 				break;
2879 			}
2880 			(void) strlcat(lbuf->link_over, tmpbuf,
2881 			    sizeof (lbuf->link_over));
2882 			if (i != nports - 1) {
2883 				(void) strlcat(lbuf->link_over, ",",
2884 				    sizeof (lbuf->link_over));
2885 			}
2886 		}
2887 		dladm_bridge_free_portlist(dlp);
2888 		break;
2889 	}
2890 
2891 	case DATALINK_CLASS_SIMNET: {
2892 		dladm_simnet_attr_t	slinfo;
2893 
2894 		if (dladm_simnet_info(handle, linkid, &slinfo, flags) !=
2895 		    DLADM_STATUS_OK) {
2896 			(void) strcpy(lbuf->link_over, "?");
2897 			break;
2898 		}
2899 		if (slinfo.sna_peer_link_id != DATALINK_INVALID_LINKID) {
2900 			if (dladm_datalink_id2info(handle,
2901 			    slinfo.sna_peer_link_id, NULL, NULL, NULL,
2902 			    lbuf->link_over, sizeof (lbuf->link_over)) !=
2903 			    DLADM_STATUS_OK)
2904 				(void) strcpy(lbuf->link_over, "?");
2905 		}
2906 		break;
2907 	}
2908 	}
2909 }
2910 
2911 static dladm_status_t
2912 print_link(show_state_t *state, datalink_id_t linkid, link_fields_buf_t *lbuf)
2913 {
2914 	char			link[MAXLINKNAMELEN];
2915 	datalink_class_t	class;
2916 	uint_t			mtu;
2917 	uint32_t		flags;
2918 	dladm_status_t		status;
2919 
2920 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, &class,
2921 	    NULL, link, sizeof (link))) != DLADM_STATUS_OK) {
2922 		goto done;
2923 	}
2924 
2925 	if (!(state->ls_flags & flags)) {
2926 		status = DLADM_STATUS_NOTFOUND;
2927 		goto done;
2928 	}
2929 
2930 	(void) snprintf(lbuf->link_name, sizeof (lbuf->link_name),
2931 	    "%s", link);
2932 	(void) dladm_class2str(class, lbuf->link_class);
2933 
2934 	if (state->ls_flags == DLADM_OPT_ACTIVE) {
2935 		dladm_attr_t	dlattr;
2936 
2937 		if (class == DATALINK_CLASS_PHYS) {
2938 			dladm_phys_attr_t	dpa;
2939 			dlpi_handle_t		dh;
2940 			dlpi_info_t		dlinfo;
2941 
2942 			if ((status = dladm_phys_info(handle, linkid, &dpa,
2943 			    DLADM_OPT_ACTIVE)) != DLADM_STATUS_OK) {
2944 				goto done;
2945 			}
2946 
2947 			if (!dpa.dp_novanity)
2948 				goto link_mtu;
2949 
2950 			/*
2951 			 * This is a physical link that does not have
2952 			 * vanity naming support.
2953 			 */
2954 			if (dlpi_open(dpa.dp_dev, &dh, DLPI_DEVONLY) !=
2955 			    DLPI_SUCCESS) {
2956 				status = DLADM_STATUS_NOTFOUND;
2957 				goto done;
2958 			}
2959 
2960 			if (dlpi_info(dh, &dlinfo, 0) != DLPI_SUCCESS) {
2961 				dlpi_close(dh);
2962 				status = DLADM_STATUS_BADARG;
2963 				goto done;
2964 			}
2965 
2966 			dlpi_close(dh);
2967 			mtu = dlinfo.di_max_sdu;
2968 		} else {
2969 link_mtu:
2970 			status = dladm_info(handle, linkid, &dlattr);
2971 			if (status != DLADM_STATUS_OK)
2972 				goto done;
2973 			mtu = dlattr.da_max_sdu;
2974 		}
2975 
2976 		(void) snprintf(lbuf->link_mtu, sizeof (lbuf->link_mtu),
2977 		    "%u", mtu);
2978 		(void) get_linkstate(link, B_TRUE, lbuf->link_state);
2979 	}
2980 
2981 	print_link_topology(state, linkid, class, lbuf);
2982 done:
2983 	return (status);
2984 }
2985 
2986 /* ARGSUSED */
2987 static int
2988 show_link(dladm_handle_t dh, datalink_id_t linkid, void *arg)
2989 {
2990 	show_state_t		*state = (show_state_t *)arg;
2991 	dladm_status_t		status;
2992 	link_fields_buf_t	lbuf;
2993 
2994 	/*
2995 	 * first get all the link attributes into lbuf;
2996 	 */
2997 	bzero(&lbuf, sizeof (link_fields_buf_t));
2998 	if ((status = print_link(state, linkid, &lbuf)) == DLADM_STATUS_OK)
2999 		ofmt_print(state->ls_ofmt, &lbuf);
3000 	state->ls_status = status;
3001 	return (DLADM_WALK_CONTINUE);
3002 }
3003 
3004 static boolean_t
3005 print_link_stats_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
3006 {
3007 	link_args_t *largs = ofarg->ofmt_cbarg;
3008 	pktsum_t *diff_stats = largs->link_s_psum;
3009 
3010 	switch (ofarg->ofmt_id) {
3011 	case LINK_S_LINK:
3012 		(void) snprintf(buf, bufsize, "%s", largs->link_s_link);
3013 		break;
3014 	case LINK_S_IPKTS:
3015 		(void) snprintf(buf, bufsize, "%llu", diff_stats->ipackets);
3016 		break;
3017 	case LINK_S_RBYTES:
3018 		(void) snprintf(buf, bufsize, "%llu", diff_stats->rbytes);
3019 		break;
3020 	case LINK_S_IERRORS:
3021 		(void) snprintf(buf, bufsize, "%u", diff_stats->ierrors);
3022 		break;
3023 	case LINK_S_OPKTS:
3024 		(void) snprintf(buf, bufsize, "%llu", diff_stats->opackets);
3025 		break;
3026 	case LINK_S_OBYTES:
3027 		(void) snprintf(buf, bufsize, "%llu", diff_stats->obytes);
3028 		break;
3029 	case LINK_S_OERRORS:
3030 		(void) snprintf(buf, bufsize, "%u", diff_stats->oerrors);
3031 		break;
3032 	default:
3033 		die("invalid input");
3034 		break;
3035 	}
3036 	return (B_TRUE);
3037 }
3038 
3039 static int
3040 show_link_stats(dladm_handle_t dh, datalink_id_t linkid, void *arg)
3041 {
3042 	char			link[DLPI_LINKNAME_MAX];
3043 	datalink_class_t	class;
3044 	show_state_t		*state = arg;
3045 	pktsum_t		stats, diff_stats;
3046 	dladm_phys_attr_t	dpa;
3047 	link_args_t		largs;
3048 
3049 	if (state->ls_firstonly) {
3050 		if (state->ls_donefirst)
3051 			return (DLADM_WALK_CONTINUE);
3052 		state->ls_donefirst = B_TRUE;
3053 	} else {
3054 		bzero(&state->ls_prevstats, sizeof (state->ls_prevstats));
3055 	}
3056 
3057 	if (dladm_datalink_id2info(dh, linkid, NULL, &class, NULL, link,
3058 	    DLPI_LINKNAME_MAX) != DLADM_STATUS_OK) {
3059 		return (DLADM_WALK_CONTINUE);
3060 	}
3061 
3062 	if (class == DATALINK_CLASS_PHYS) {
3063 		if (dladm_phys_info(dh, linkid, &dpa, DLADM_OPT_ACTIVE) !=
3064 		    DLADM_STATUS_OK) {
3065 			return (DLADM_WALK_CONTINUE);
3066 		}
3067 		if (dpa.dp_novanity)
3068 			get_mac_stats(dpa.dp_dev, &stats);
3069 		else
3070 			get_link_stats(link, &stats);
3071 	} else {
3072 		get_link_stats(link, &stats);
3073 	}
3074 	dladm_stats_diff(&diff_stats, &stats, &state->ls_prevstats);
3075 
3076 	largs.link_s_link = link;
3077 	largs.link_s_psum = &diff_stats;
3078 	ofmt_print(state->ls_ofmt, &largs);
3079 
3080 	state->ls_prevstats = stats;
3081 	return (DLADM_WALK_CONTINUE);
3082 }
3083 
3084 
3085 static dladm_status_t
3086 print_aggr_info(show_grp_state_t *state, const char *link,
3087     dladm_aggr_grp_attr_t *ginfop)
3088 {
3089 	char			addr_str[ETHERADDRL * 3];
3090 	laggr_fields_buf_t	lbuf;
3091 
3092 	(void) snprintf(lbuf.laggr_name, sizeof (lbuf.laggr_name),
3093 	    "%s", link);
3094 
3095 	(void) dladm_aggr_policy2str(ginfop->lg_policy,
3096 	    lbuf.laggr_policy);
3097 
3098 	if (ginfop->lg_mac_fixed) {
3099 		(void) dladm_aggr_macaddr2str(ginfop->lg_mac, addr_str);
3100 		(void) snprintf(lbuf.laggr_addrpolicy,
3101 		    sizeof (lbuf.laggr_addrpolicy), "fixed (%s)", addr_str);
3102 	} else {
3103 		(void) snprintf(lbuf.laggr_addrpolicy,
3104 		    sizeof (lbuf.laggr_addrpolicy), "auto");
3105 	}
3106 
3107 	(void) dladm_aggr_lacpmode2str(ginfop->lg_lacp_mode,
3108 	    lbuf.laggr_lacpactivity);
3109 	(void) dladm_aggr_lacptimer2str(ginfop->lg_lacp_timer,
3110 	    lbuf.laggr_lacptimer);
3111 	(void) snprintf(lbuf.laggr_flags, sizeof (lbuf.laggr_flags), "%c----",
3112 	    ginfop->lg_force ? 'f' : '-');
3113 
3114 	ofmt_print(state->gs_ofmt, &lbuf);
3115 
3116 	return (DLADM_STATUS_OK);
3117 }
3118 
3119 static boolean_t
3120 print_xaggr_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
3121 {
3122 	const laggr_args_t	*l = ofarg->ofmt_cbarg;
3123 	boolean_t		is_port = (l->laggr_lport >= 0);
3124 	char			tmpbuf[DLADM_STRSIZE];
3125 	const char		*objname;
3126 	dladm_aggr_port_attr_t	*portp = NULL;
3127 	dladm_phys_attr_t	dpa;
3128 
3129 	if (is_port) {
3130 		portp = &(l->laggr_ginfop->lg_ports[l->laggr_lport]);
3131 		if (dladm_phys_info(handle, portp->lp_linkid, &dpa,
3132 		    DLADM_OPT_ACTIVE) != DLADM_STATUS_OK)
3133 			objname = "?";
3134 		else
3135 			objname = dpa.dp_dev;
3136 	} else {
3137 		objname = l->laggr_link;
3138 	}
3139 
3140 	switch (ofarg->ofmt_id) {
3141 	case AGGR_X_LINK:
3142 		(void) snprintf(buf, bufsize, "%s",
3143 		    (is_port && !l->laggr_parsable ? " " : l->laggr_link));
3144 		break;
3145 	case AGGR_X_PORT:
3146 		if (is_port) {
3147 			if (dladm_datalink_id2info(handle, portp->lp_linkid,
3148 			    NULL, NULL, NULL, buf, bufsize) != DLADM_STATUS_OK)
3149 				(void) sprintf(buf, "?");
3150 		}
3151 		break;
3152 
3153 	case AGGR_X_SPEED:
3154 		(void) snprintf(buf, bufsize, "%uMb",
3155 		    (uint_t)((get_ifspeed(objname, !is_port)) / 1000000ull));
3156 		break;
3157 
3158 	case AGGR_X_DUPLEX:
3159 		(void) get_linkduplex(objname, !is_port, tmpbuf);
3160 		(void) strlcpy(buf, tmpbuf, bufsize);
3161 		break;
3162 
3163 	case AGGR_X_STATE:
3164 		(void) get_linkstate(objname, !is_port, tmpbuf);
3165 		(void) strlcpy(buf, tmpbuf, bufsize);
3166 		break;
3167 	case AGGR_X_ADDRESS:
3168 		(void) dladm_aggr_macaddr2str(
3169 		    (is_port ? portp->lp_mac : l->laggr_ginfop->lg_mac),
3170 		    tmpbuf);
3171 		(void) strlcpy(buf, tmpbuf, bufsize);
3172 		break;
3173 	case AGGR_X_PORTSTATE:
3174 		if (is_port) {
3175 			(void) dladm_aggr_portstate2str(portp->lp_state,
3176 			    tmpbuf);
3177 			(void) strlcpy(buf, tmpbuf, bufsize);
3178 		}
3179 		break;
3180 	}
3181 err:
3182 	*(l->laggr_status) = DLADM_STATUS_OK;
3183 	return (B_TRUE);
3184 }
3185 
3186 static dladm_status_t
3187 print_aggr_extended(show_grp_state_t *state, const char *link,
3188     dladm_aggr_grp_attr_t *ginfop)
3189 {
3190 	int			i;
3191 	dladm_status_t		status;
3192 	laggr_args_t		largs;
3193 
3194 	largs.laggr_lport = -1;
3195 	largs.laggr_link = link;
3196 	largs.laggr_ginfop = ginfop;
3197 	largs.laggr_status = &status;
3198 	largs.laggr_parsable = state->gs_parsable;
3199 
3200 	ofmt_print(state->gs_ofmt, &largs);
3201 
3202 	if (status != DLADM_STATUS_OK)
3203 		goto done;
3204 
3205 	for (i = 0; i < ginfop->lg_nports; i++) {
3206 		largs.laggr_lport = i;
3207 		ofmt_print(state->gs_ofmt, &largs);
3208 		if (status != DLADM_STATUS_OK)
3209 			goto done;
3210 	}
3211 
3212 	status = DLADM_STATUS_OK;
3213 done:
3214 	return (status);
3215 }
3216 
3217 static boolean_t
3218 print_lacp_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
3219 {
3220 	const laggr_args_t	*l = ofarg->ofmt_cbarg;
3221 	int			portnum;
3222 	boolean_t		is_port = (l->laggr_lport >= 0);
3223 	dladm_aggr_port_attr_t	*portp;
3224 	aggr_lacp_state_t	*lstate;
3225 
3226 	if (!is_port)
3227 		return (B_FALSE); /* cannot happen! */
3228 
3229 	portnum = l->laggr_lport;
3230 	portp = &(l->laggr_ginfop->lg_ports[portnum]);
3231 	lstate = &(portp->lp_lacp_state);
3232 
3233 	switch (ofarg->ofmt_id) {
3234 	case AGGR_L_LINK:
3235 		(void) snprintf(buf, bufsize, "%s",
3236 		    (portnum > 0 ? "" : l->laggr_link));
3237 		break;
3238 
3239 	case AGGR_L_PORT:
3240 		if (dladm_datalink_id2info(handle, portp->lp_linkid, NULL, NULL,
3241 		    NULL, buf, bufsize) != DLADM_STATUS_OK)
3242 			(void) sprintf(buf, "?");
3243 		break;
3244 
3245 	case AGGR_L_AGGREGATABLE:
3246 		(void) snprintf(buf, bufsize, "%s",
3247 		    (lstate->bit.aggregation ? "yes" : "no"));
3248 		break;
3249 
3250 	case AGGR_L_SYNC:
3251 		(void) snprintf(buf, bufsize, "%s",
3252 		    (lstate->bit.sync ? "yes" : "no"));
3253 		break;
3254 
3255 	case AGGR_L_COLL:
3256 		(void) snprintf(buf, bufsize, "%s",
3257 		    (lstate->bit.collecting ? "yes" : "no"));
3258 		break;
3259 
3260 	case AGGR_L_DIST:
3261 		(void) snprintf(buf, bufsize, "%s",
3262 		    (lstate->bit.distributing ? "yes" : "no"));
3263 		break;
3264 
3265 	case AGGR_L_DEFAULTED:
3266 		(void) snprintf(buf, bufsize, "%s",
3267 		    (lstate->bit.defaulted ? "yes" : "no"));
3268 		break;
3269 
3270 	case AGGR_L_EXPIRED:
3271 		(void) snprintf(buf, bufsize, "%s",
3272 		    (lstate->bit.expired ? "yes" : "no"));
3273 		break;
3274 	}
3275 
3276 	*(l->laggr_status) = DLADM_STATUS_OK;
3277 	return (B_TRUE);
3278 }
3279 
3280 static dladm_status_t
3281 print_aggr_lacp(show_grp_state_t *state, const char *link,
3282     dladm_aggr_grp_attr_t *ginfop)
3283 {
3284 	int		i;
3285 	dladm_status_t	status;
3286 	laggr_args_t	largs;
3287 
3288 	largs.laggr_link = link;
3289 	largs.laggr_ginfop = ginfop;
3290 	largs.laggr_status = &status;
3291 
3292 	for (i = 0; i < ginfop->lg_nports; i++) {
3293 		largs.laggr_lport = i;
3294 		ofmt_print(state->gs_ofmt, &largs);
3295 		if (status != DLADM_STATUS_OK)
3296 			goto done;
3297 	}
3298 
3299 	status = DLADM_STATUS_OK;
3300 done:
3301 	return (status);
3302 }
3303 
3304 static boolean_t
3305 print_aggr_stats_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
3306 {
3307 	const laggr_args_t	*l = ofarg->ofmt_cbarg;
3308 	int			portnum;
3309 	boolean_t		is_port = (l->laggr_lport >= 0);
3310 	dladm_aggr_port_attr_t	*portp;
3311 	dladm_status_t		*stat, status;
3312 	pktsum_t		*diff_stats = NULL;
3313 
3314 	stat = l->laggr_status;
3315 	*stat = DLADM_STATUS_OK;
3316 
3317 	if (is_port) {
3318 		portnum = l->laggr_lport;
3319 		portp = &(l->laggr_ginfop->lg_ports[portnum]);
3320 
3321 		if ((status = dladm_datalink_id2info(handle,
3322 		    portp->lp_linkid, NULL, NULL, NULL, buf, bufsize)) !=
3323 		    DLADM_STATUS_OK) {
3324 			goto err;
3325 		}
3326 		diff_stats = l->laggr_diffstats;
3327 		if (diff_stats == NULL) {
3328 			status = DLADM_STATUS_BADVAL;
3329 			goto err;
3330 		}
3331 	}
3332 
3333 	switch (ofarg->ofmt_id) {
3334 	case AGGR_S_LINK:
3335 		(void) snprintf(buf, bufsize, "%s",
3336 		    (is_port ? "" : l->laggr_link));
3337 		break;
3338 	case AGGR_S_PORT:
3339 		/*
3340 		 * if (is_port), buf has port name. Otherwise we print
3341 		 * STR_UNDEF_VAL
3342 		 */
3343 		break;
3344 
3345 	case AGGR_S_IPKTS:
3346 		if (is_port) {
3347 			(void) snprintf(buf, bufsize, "%llu",
3348 			    diff_stats->ipackets);
3349 		} else {
3350 			(void) snprintf(buf, bufsize, "%llu",
3351 			    l->laggr_pktsumtot->ipackets);
3352 		}
3353 		break;
3354 
3355 	case AGGR_S_RBYTES:
3356 		if (is_port) {
3357 			(void) snprintf(buf, bufsize, "%llu",
3358 			    diff_stats->rbytes);
3359 		} else {
3360 			(void) snprintf(buf, bufsize, "%llu",
3361 			    l->laggr_pktsumtot->rbytes);
3362 		}
3363 		break;
3364 
3365 	case AGGR_S_OPKTS:
3366 		if (is_port) {
3367 			(void) snprintf(buf, bufsize, "%llu",
3368 			    diff_stats->opackets);
3369 		} else {
3370 			(void) snprintf(buf, bufsize, "%llu",
3371 			    l->laggr_pktsumtot->opackets);
3372 		}
3373 		break;
3374 	case AGGR_S_OBYTES:
3375 		if (is_port) {
3376 			(void) snprintf(buf, bufsize, "%llu",
3377 			    diff_stats->obytes);
3378 		} else {
3379 			(void) snprintf(buf, bufsize, "%llu",
3380 			    l->laggr_pktsumtot->obytes);
3381 		}
3382 		break;
3383 
3384 	case AGGR_S_IPKTDIST:
3385 		if (is_port) {
3386 			(void) snprintf(buf, bufsize, "%-6.1f",
3387 			    (double)diff_stats->ipackets/
3388 			    (double)l->laggr_pktsumtot->ipackets * 100);
3389 		}
3390 		break;
3391 	case AGGR_S_OPKTDIST:
3392 		if (is_port) {
3393 			(void) snprintf(buf, bufsize, "%-6.1f",
3394 			    (double)diff_stats->opackets/
3395 			    (double)l->laggr_pktsumtot->opackets * 100);
3396 		}
3397 		break;
3398 	}
3399 	return (B_TRUE);
3400 
3401 err:
3402 	*stat = status;
3403 	return (B_TRUE);
3404 }
3405 
3406 static dladm_status_t
3407 print_aggr_stats(show_grp_state_t *state, const char *link,
3408     dladm_aggr_grp_attr_t *ginfop)
3409 {
3410 	dladm_phys_attr_t	dpa;
3411 	dladm_aggr_port_attr_t	*portp;
3412 	pktsum_t		pktsumtot, *port_stat;
3413 	dladm_status_t		status;
3414 	int			i;
3415 	laggr_args_t		largs;
3416 
3417 	/* sum the ports statistics */
3418 	bzero(&pktsumtot, sizeof (pktsumtot));
3419 
3420 	/* Allocate memory to keep stats of each port */
3421 	port_stat = malloc(ginfop->lg_nports * sizeof (pktsum_t));
3422 	if (port_stat == NULL) {
3423 		/* Bail out; no memory */
3424 		return (DLADM_STATUS_NOMEM);
3425 	}
3426 
3427 
3428 	for (i = 0; i < ginfop->lg_nports; i++) {
3429 
3430 		portp = &(ginfop->lg_ports[i]);
3431 		if ((status = dladm_phys_info(handle, portp->lp_linkid, &dpa,
3432 		    DLADM_OPT_ACTIVE)) != DLADM_STATUS_OK) {
3433 			goto done;
3434 		}
3435 
3436 		get_mac_stats(dpa.dp_dev, &port_stat[i]);
3437 
3438 		/*
3439 		 * Let's re-use gs_prevstats[] to store the difference of the
3440 		 * counters since last use. We will store the new stats from
3441 		 * port_stat[] once we have the stats displayed.
3442 		 */
3443 
3444 		dladm_stats_diff(&state->gs_prevstats[i], &port_stat[i],
3445 		    &state->gs_prevstats[i]);
3446 		dladm_stats_total(&pktsumtot, &pktsumtot,
3447 		    &state->gs_prevstats[i]);
3448 	}
3449 
3450 	largs.laggr_lport = -1;
3451 	largs.laggr_link = link;
3452 	largs.laggr_ginfop = ginfop;
3453 	largs.laggr_status = &status;
3454 	largs.laggr_pktsumtot = &pktsumtot;
3455 
3456 	ofmt_print(state->gs_ofmt, &largs);
3457 
3458 	if (status != DLADM_STATUS_OK)
3459 		goto done;
3460 
3461 	for (i = 0; i < ginfop->lg_nports; i++) {
3462 		largs.laggr_lport = i;
3463 		largs.laggr_diffstats = &state->gs_prevstats[i];
3464 		ofmt_print(state->gs_ofmt, &largs);
3465 		if (status != DLADM_STATUS_OK)
3466 			goto done;
3467 	}
3468 
3469 	status = DLADM_STATUS_OK;
3470 	for (i = 0; i < ginfop->lg_nports; i++)
3471 		state->gs_prevstats[i] = port_stat[i];
3472 
3473 done:
3474 	free(port_stat);
3475 	return (status);
3476 }
3477 
3478 static dladm_status_t
3479 print_aggr(show_grp_state_t *state, datalink_id_t linkid)
3480 {
3481 	char			link[MAXLINKNAMELEN];
3482 	dladm_aggr_grp_attr_t	ginfo;
3483 	uint32_t		flags;
3484 	dladm_status_t		status;
3485 
3486 	bzero(&ginfo, sizeof (dladm_aggr_grp_attr_t));
3487 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, NULL,
3488 	    NULL, link, MAXLINKNAMELEN)) != DLADM_STATUS_OK) {
3489 		return (status);
3490 	}
3491 
3492 	if (!(state->gs_flags & flags))
3493 		return (DLADM_STATUS_NOTFOUND);
3494 
3495 	status = dladm_aggr_info(handle, linkid, &ginfo, state->gs_flags);
3496 	if (status != DLADM_STATUS_OK)
3497 		return (status);
3498 
3499 	if (state->gs_lacp)
3500 		status = print_aggr_lacp(state, link, &ginfo);
3501 	else if (state->gs_extended)
3502 		status = print_aggr_extended(state, link, &ginfo);
3503 	else if (state->gs_stats)
3504 		status = print_aggr_stats(state, link, &ginfo);
3505 	else
3506 		status = print_aggr_info(state, link, &ginfo);
3507 
3508 done:
3509 	free(ginfo.lg_ports);
3510 	return (status);
3511 }
3512 
3513 /* ARGSUSED */
3514 static int
3515 show_aggr(dladm_handle_t dh, datalink_id_t linkid, void *arg)
3516 {
3517 	show_grp_state_t	*state = arg;
3518 
3519 	state->gs_status = print_aggr(state, linkid);
3520 	return (DLADM_WALK_CONTINUE);
3521 }
3522 
3523 static void
3524 do_show_link(int argc, char *argv[], const char *use)
3525 {
3526 	int		option;
3527 	boolean_t	s_arg = B_FALSE;
3528 	boolean_t	i_arg = B_FALSE;
3529 	uint32_t	flags = DLADM_OPT_ACTIVE;
3530 	boolean_t	p_arg = B_FALSE;
3531 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
3532 	char		linkname[MAXLINKNAMELEN];
3533 	uint32_t	interval = 0;
3534 	show_state_t	state;
3535 	dladm_status_t	status;
3536 	boolean_t	o_arg = B_FALSE;
3537 	char		*fields_str = NULL;
3538 	char		*all_active_fields = "link,class,mtu,state,bridge,over";
3539 	char		*all_inactive_fields = "link,class,bridge,over";
3540 	char		*allstat_fields =
3541 	    "link,ipackets,rbytes,ierrors,opackets,obytes,oerrors";
3542 	ofmt_handle_t	ofmt;
3543 	ofmt_status_t	oferr;
3544 	uint_t		ofmtflags = 0;
3545 
3546 	bzero(&state, sizeof (state));
3547 
3548 	opterr = 0;
3549 	while ((option = getopt_long(argc, argv, ":pPsi:o:",
3550 	    show_lopts, NULL)) != -1) {
3551 		switch (option) {
3552 		case 'p':
3553 			if (p_arg)
3554 				die_optdup(option);
3555 
3556 			p_arg = B_TRUE;
3557 			break;
3558 		case 's':
3559 			if (s_arg)
3560 				die_optdup(option);
3561 
3562 			s_arg = B_TRUE;
3563 			break;
3564 		case 'P':
3565 			if (flags != DLADM_OPT_ACTIVE)
3566 				die_optdup(option);
3567 
3568 			flags = DLADM_OPT_PERSIST;
3569 			break;
3570 		case 'o':
3571 			o_arg = B_TRUE;
3572 			fields_str = optarg;
3573 			break;
3574 		case 'i':
3575 			if (i_arg)
3576 				die_optdup(option);
3577 
3578 			i_arg = B_TRUE;
3579 			if (!dladm_str2interval(optarg, &interval))
3580 				die("invalid interval value '%s'", optarg);
3581 			break;
3582 		default:
3583 			die_opterr(optopt, option, use);
3584 			break;
3585 		}
3586 	}
3587 
3588 	if (i_arg && !s_arg)
3589 		die("the option -i can be used only with -s");
3590 
3591 	if (s_arg && flags != DLADM_OPT_ACTIVE)
3592 		die("the option -P cannot be used with -s");
3593 
3594 	/* get link name (optional last argument) */
3595 	if (optind == (argc-1)) {
3596 		uint32_t	f;
3597 
3598 		if (strlcpy(linkname, argv[optind], MAXLINKNAMELEN) >=
3599 		    MAXLINKNAMELEN)
3600 			die("link name too long");
3601 		if ((status = dladm_name2info(handle, linkname, &linkid, &f,
3602 		    NULL, NULL)) != DLADM_STATUS_OK) {
3603 			die_dlerr(status, "link %s is not valid", linkname);
3604 		}
3605 
3606 		if (!(f & flags)) {
3607 			die_dlerr(DLADM_STATUS_BADARG, "link %s is %s",
3608 			    argv[optind], flags == DLADM_OPT_PERSIST ?
3609 			    "a temporary link" : "temporarily removed");
3610 		}
3611 	} else if (optind != argc) {
3612 		usage();
3613 	}
3614 
3615 	if (p_arg && !o_arg)
3616 		die("-p requires -o");
3617 
3618 	if (p_arg && strcasecmp(fields_str, "all") == 0)
3619 		die("\"-o all\" is invalid with -p");
3620 
3621 	if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0)) {
3622 		if (s_arg)
3623 			fields_str = allstat_fields;
3624 		else if (flags & DLADM_OPT_ACTIVE)
3625 			fields_str = all_active_fields;
3626 		else
3627 			fields_str = all_inactive_fields;
3628 	}
3629 
3630 	state.ls_parsable = p_arg;
3631 	state.ls_flags = flags;
3632 	state.ls_donefirst = B_FALSE;
3633 
3634 	if (s_arg) {
3635 		link_stats(linkid, interval, fields_str, &state);
3636 		return;
3637 	}
3638 	if (state.ls_parsable)
3639 		ofmtflags |= OFMT_PARSABLE;
3640 	else
3641 		ofmtflags |= OFMT_WRAP;
3642 
3643 	oferr = ofmt_open(fields_str, link_fields, ofmtflags, 0, &ofmt);
3644 	ofmt_check(oferr, state.ls_parsable, ofmt, die, warn);
3645 	state.ls_ofmt = ofmt;
3646 
3647 	if (linkid == DATALINK_ALL_LINKID) {
3648 		(void) dladm_walk_datalink_id(show_link, handle, &state,
3649 		    DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE, flags);
3650 	} else {
3651 		(void) show_link(handle, linkid, &state);
3652 		if (state.ls_status != DLADM_STATUS_OK) {
3653 			die_dlerr(state.ls_status, "failed to show link %s",
3654 			    argv[optind]);
3655 		}
3656 	}
3657 	ofmt_close(ofmt);
3658 }
3659 
3660 static void
3661 do_show_aggr(int argc, char *argv[], const char *use)
3662 {
3663 	boolean_t		L_arg = B_FALSE;
3664 	boolean_t		s_arg = B_FALSE;
3665 	boolean_t		i_arg = B_FALSE;
3666 	boolean_t		p_arg = B_FALSE;
3667 	boolean_t		x_arg = B_FALSE;
3668 	show_grp_state_t	state;
3669 	uint32_t		flags = DLADM_OPT_ACTIVE;
3670 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
3671 	int			option;
3672 	uint32_t		interval = 0;
3673 	int			key;
3674 	dladm_status_t		status;
3675 	boolean_t		o_arg = B_FALSE;
3676 	char			*fields_str = NULL;
3677 	char			*all_fields =
3678 	    "link,policy,addrpolicy,lacpactivity,lacptimer,flags";
3679 	char			*all_lacp_fields =
3680 	    "link,port,aggregatable,sync,coll,dist,defaulted,expired";
3681 	char			*all_stats_fields =
3682 	    "link,port,ipackets,rbytes,opackets,obytes,ipktdist,opktdist";
3683 	char			*all_extended_fields =
3684 	    "link,port,speed,duplex,state,address,portstate";
3685 	const ofmt_field_t	*pf;
3686 	ofmt_handle_t		ofmt;
3687 	ofmt_status_t		oferr;
3688 	uint_t			ofmtflags = 0;
3689 
3690 	opterr = 0;
3691 	while ((option = getopt_long(argc, argv, ":LpPxsi:o:",
3692 	    show_lopts, NULL)) != -1) {
3693 		switch (option) {
3694 		case 'L':
3695 			if (L_arg)
3696 				die_optdup(option);
3697 
3698 			L_arg = B_TRUE;
3699 			break;
3700 		case 'p':
3701 			if (p_arg)
3702 				die_optdup(option);
3703 
3704 			p_arg = B_TRUE;
3705 			break;
3706 		case 'x':
3707 			if (x_arg)
3708 				die_optdup(option);
3709 
3710 			x_arg = B_TRUE;
3711 			break;
3712 		case 'P':
3713 			if (flags != DLADM_OPT_ACTIVE)
3714 				die_optdup(option);
3715 
3716 			flags = DLADM_OPT_PERSIST;
3717 			break;
3718 		case 's':
3719 			if (s_arg)
3720 				die_optdup(option);
3721 
3722 			s_arg = B_TRUE;
3723 			break;
3724 		case 'o':
3725 			o_arg = B_TRUE;
3726 			fields_str = optarg;
3727 			break;
3728 		case 'i':
3729 			if (i_arg)
3730 				die_optdup(option);
3731 
3732 			i_arg = B_TRUE;
3733 			if (!dladm_str2interval(optarg, &interval))
3734 				die("invalid interval value '%s'", optarg);
3735 			break;
3736 		default:
3737 			die_opterr(optopt, option, use);
3738 			break;
3739 		}
3740 	}
3741 
3742 	if (p_arg && !o_arg)
3743 		die("-p requires -o");
3744 
3745 	if (p_arg && strcasecmp(fields_str, "all") == 0)
3746 		die("\"-o all\" is invalid with -p");
3747 
3748 	if (i_arg && !s_arg)
3749 		die("the option -i can be used only with -s");
3750 
3751 	if (s_arg && (L_arg || p_arg || x_arg || flags != DLADM_OPT_ACTIVE)) {
3752 		die("the option -%c cannot be used with -s",
3753 		    L_arg ? 'L' : (p_arg ? 'p' : (x_arg ? 'x' : 'P')));
3754 	}
3755 
3756 	if (L_arg && flags != DLADM_OPT_ACTIVE)
3757 		die("the option -P cannot be used with -L");
3758 
3759 	if (x_arg && (L_arg || flags != DLADM_OPT_ACTIVE))
3760 		die("the option -%c cannot be used with -x", L_arg ? 'L' : 'P');
3761 
3762 	/* get aggregation key or aggrname (optional last argument) */
3763 	if (optind == (argc-1)) {
3764 		if (!str2int(argv[optind], &key)) {
3765 			status = dladm_name2info(handle, argv[optind],
3766 			    &linkid, NULL, NULL, NULL);
3767 		} else {
3768 			status = dladm_key2linkid(handle, (uint16_t)key,
3769 			    &linkid, DLADM_OPT_ACTIVE);
3770 		}
3771 
3772 		if (status != DLADM_STATUS_OK)
3773 			die("non-existent aggregation '%s'", argv[optind]);
3774 
3775 	} else if (optind != argc) {
3776 		usage();
3777 	}
3778 
3779 	bzero(&state, sizeof (state));
3780 	state.gs_lacp = L_arg;
3781 	state.gs_stats = s_arg;
3782 	state.gs_flags = flags;
3783 	state.gs_parsable = p_arg;
3784 	state.gs_extended = x_arg;
3785 
3786 	if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0)) {
3787 		if (state.gs_lacp)
3788 			fields_str = all_lacp_fields;
3789 		else if (state.gs_stats)
3790 			fields_str = all_stats_fields;
3791 		else if (state.gs_extended)
3792 			fields_str = all_extended_fields;
3793 		else
3794 			fields_str = all_fields;
3795 	}
3796 
3797 	if (state.gs_lacp) {
3798 		pf = aggr_l_fields;
3799 	} else if (state.gs_stats) {
3800 		pf = aggr_s_fields;
3801 	} else if (state.gs_extended) {
3802 		pf = aggr_x_fields;
3803 	} else {
3804 		pf = laggr_fields;
3805 	}
3806 
3807 	if (state.gs_parsable)
3808 		ofmtflags |= OFMT_PARSABLE;
3809 	oferr = ofmt_open(fields_str, pf, ofmtflags, 0, &ofmt);
3810 	ofmt_check(oferr, state.gs_parsable, ofmt, die, warn);
3811 	state.gs_ofmt = ofmt;
3812 
3813 	if (s_arg) {
3814 		aggr_stats(linkid, &state, interval);
3815 		ofmt_close(ofmt);
3816 		return;
3817 	}
3818 
3819 	if (linkid == DATALINK_ALL_LINKID) {
3820 		(void) dladm_walk_datalink_id(show_aggr, handle, &state,
3821 		    DATALINK_CLASS_AGGR, DATALINK_ANY_MEDIATYPE, flags);
3822 	} else {
3823 		(void) show_aggr(handle, linkid, &state);
3824 		if (state.gs_status != DLADM_STATUS_OK) {
3825 			die_dlerr(state.gs_status, "failed to show aggr %s",
3826 			    argv[optind]);
3827 		}
3828 	}
3829 	ofmt_close(ofmt);
3830 }
3831 
3832 static dladm_status_t
3833 print_phys_default(show_state_t *state, datalink_id_t linkid,
3834     const char *link, uint32_t flags, uint32_t media)
3835 {
3836 	dladm_phys_attr_t dpa;
3837 	dladm_status_t status;
3838 	link_fields_buf_t pattr;
3839 
3840 	status = dladm_phys_info(handle, linkid, &dpa, state->ls_flags);
3841 	if (status != DLADM_STATUS_OK)
3842 		goto done;
3843 
3844 	bzero(&pattr, sizeof (pattr));
3845 	(void) snprintf(pattr.link_phys_device,
3846 	    sizeof (pattr.link_phys_device), "%s", dpa.dp_dev);
3847 	(void) dladm_media2str(media, pattr.link_phys_media);
3848 	if (state->ls_flags == DLADM_OPT_ACTIVE) {
3849 		boolean_t	islink;
3850 
3851 		if (!dpa.dp_novanity) {
3852 			(void) strlcpy(pattr.link_name, link,
3853 			    sizeof (pattr.link_name));
3854 			islink = B_TRUE;
3855 		} else {
3856 			/*
3857 			 * This is a physical link that does not have
3858 			 * vanity naming support.
3859 			 */
3860 			(void) strlcpy(pattr.link_name, dpa.dp_dev,
3861 			    sizeof (pattr.link_name));
3862 			islink = B_FALSE;
3863 		}
3864 
3865 		(void) get_linkstate(pattr.link_name, islink,
3866 		    pattr.link_phys_state);
3867 		(void) snprintf(pattr.link_phys_speed,
3868 		    sizeof (pattr.link_phys_speed), "%u",
3869 		    (uint_t)((get_ifspeed(pattr.link_name,
3870 		    islink)) / 1000000ull));
3871 		(void) get_linkduplex(pattr.link_name, islink,
3872 		    pattr.link_phys_duplex);
3873 	} else {
3874 		(void) snprintf(pattr.link_name, sizeof (pattr.link_name),
3875 		    "%s", link);
3876 		(void) snprintf(pattr.link_flags, sizeof (pattr.link_flags),
3877 		    "%c----", flags & DLADM_OPT_ACTIVE ? '-' : 'r');
3878 	}
3879 
3880 	ofmt_print(state->ls_ofmt, &pattr);
3881 
3882 done:
3883 	return (status);
3884 }
3885 
3886 typedef struct {
3887 	show_state_t	*ms_state;
3888 	char		*ms_link;
3889 	dladm_macaddr_attr_t *ms_mac_attr;
3890 } print_phys_mac_state_t;
3891 
3892 /*
3893  *  callback for ofmt_print()
3894  */
3895 static boolean_t
3896 print_phys_one_mac_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
3897 {
3898 	print_phys_mac_state_t *mac_state = ofarg->ofmt_cbarg;
3899 	dladm_macaddr_attr_t *attr = mac_state->ms_mac_attr;
3900 	boolean_t is_primary = (attr->ma_slot == 0);
3901 	boolean_t is_parsable = mac_state->ms_state->ls_parsable;
3902 
3903 	switch (ofarg->ofmt_id) {
3904 	case PHYS_M_LINK:
3905 		(void) snprintf(buf, bufsize, "%s",
3906 		    (is_primary || is_parsable) ? mac_state->ms_link : " ");
3907 		break;
3908 	case PHYS_M_SLOT:
3909 		if (is_primary)
3910 			(void) snprintf(buf, bufsize, gettext("primary"));
3911 		else
3912 			(void) snprintf(buf, bufsize, "%d", attr->ma_slot);
3913 		break;
3914 	case PHYS_M_ADDRESS:
3915 		(void) dladm_aggr_macaddr2str(attr->ma_addr, buf);
3916 		break;
3917 	case PHYS_M_INUSE:
3918 		(void) snprintf(buf, bufsize, "%s",
3919 		    attr->ma_flags & DLADM_MACADDR_USED ? gettext("yes") :
3920 		    gettext("no"));
3921 		break;
3922 	case PHYS_M_CLIENT:
3923 		/*
3924 		 * CR 6678526: resolve link id to actual link name if
3925 		 * it is valid.
3926 		 */
3927 		(void) snprintf(buf, bufsize, "%s", attr->ma_client_name);
3928 		break;
3929 	}
3930 
3931 	return (B_TRUE);
3932 }
3933 
3934 typedef struct {
3935 	show_state_t	*hs_state;
3936 	char		*hs_link;
3937 	dladm_hwgrp_attr_t *hs_grp_attr;
3938 } print_phys_hwgrp_state_t;
3939 
3940 static boolean_t
3941 print_phys_one_hwgrp_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
3942 {
3943 	int		i;
3944 	boolean_t	first = B_TRUE;
3945 	int		start = -1;
3946 	int		end = -1;
3947 	char		ringstr[RINGSTRLEN];
3948 	char		ringsubstr[RINGSTRLEN];
3949 
3950 	print_phys_hwgrp_state_t *hg_state = ofarg->ofmt_cbarg;
3951 	dladm_hwgrp_attr_t *attr = hg_state->hs_grp_attr;
3952 
3953 	switch (ofarg->ofmt_id) {
3954 	case PHYS_H_LINK:
3955 		(void) snprintf(buf, bufsize, "%s", attr->hg_link_name);
3956 		break;
3957 	case PHYS_H_RINGTYPE:
3958 		(void) snprintf(buf, bufsize, "%s",
3959 		    attr->hg_grp_type == DLADM_HWGRP_TYPE_RX ? "RX" : "TX");
3960 		break;
3961 	case PHYS_H_RINGS:
3962 		ringstr[0] = '\0';
3963 		for (i = 0; i < attr->hg_n_rings; i++) {
3964 			uint_t	index = attr->hg_rings[i];
3965 
3966 			if (start == -1) {
3967 				start = index;
3968 				end = index;
3969 			} else if (index == end + 1) {
3970 				end = index;
3971 			} else {
3972 				if (start == end) {
3973 					if (first) {
3974 						(void) snprintf(
3975 						    ringsubstr,
3976 						    RINGSTRLEN, "%d",
3977 						    start);
3978 						first = B_FALSE;
3979 					} else {
3980 						(void) snprintf(
3981 						    ringsubstr,
3982 						    RINGSTRLEN, ",%d",
3983 						    start);
3984 					}
3985 				} else {
3986 					if (first) {
3987 						(void) snprintf(
3988 						    ringsubstr,
3989 						    RINGSTRLEN,
3990 						    "%d-%d",
3991 						    start, end);
3992 						first = B_FALSE;
3993 					} else {
3994 						(void) snprintf(
3995 						    ringsubstr,
3996 						    RINGSTRLEN,
3997 						    ",%d-%d",
3998 						    start, end);
3999 					}
4000 				}
4001 				(void) strlcat(ringstr, ringsubstr,
4002 				    RINGSTRLEN);
4003 				start = index;
4004 				end = index;
4005 			}
4006 		}
4007 		/* The last one */
4008 		if (start != -1) {
4009 			if (first) {
4010 				if (start == end) {
4011 					(void) snprintf(buf, bufsize, "%d",
4012 					    start);
4013 				} else {
4014 					(void) snprintf(buf, bufsize, "%d-%d",
4015 					    start, end);
4016 				}
4017 			} else {
4018 				if (start == end) {
4019 					(void) snprintf(ringsubstr, RINGSTRLEN,
4020 					    ",%d", start);
4021 				} else {
4022 					(void) snprintf(ringsubstr, RINGSTRLEN,
4023 					    ",%d-%d", start, end);
4024 				}
4025 				(void) strlcat(ringstr, ringsubstr, RINGSTRLEN);
4026 				(void) snprintf(buf, bufsize, "%s", ringstr);
4027 			}
4028 		}
4029 		break;
4030 	case PHYS_H_CLIENTS:
4031 		if (attr->hg_client_names[0] == '\0') {
4032 			(void) snprintf(buf, bufsize, "--");
4033 		} else {
4034 			(void) snprintf(buf, bufsize, "%s ",
4035 			    attr->hg_client_names);
4036 		}
4037 		break;
4038 	}
4039 
4040 	return (B_TRUE);
4041 }
4042 
4043 /*
4044  * callback for dladm_walk_macaddr, invoked for each MAC address slot
4045  */
4046 static boolean_t
4047 print_phys_mac_callback(void *arg, dladm_macaddr_attr_t *attr)
4048 {
4049 	print_phys_mac_state_t *mac_state = arg;
4050 	show_state_t *state = mac_state->ms_state;
4051 
4052 	mac_state->ms_mac_attr = attr;
4053 	ofmt_print(state->ls_ofmt, mac_state);
4054 
4055 	return (B_TRUE);
4056 }
4057 
4058 /*
4059  * invoked by show-phys -m for each physical data-link
4060  */
4061 static dladm_status_t
4062 print_phys_mac(show_state_t *state, datalink_id_t linkid, char *link)
4063 {
4064 	print_phys_mac_state_t mac_state;
4065 
4066 	mac_state.ms_state = state;
4067 	mac_state.ms_link = link;
4068 
4069 	return (dladm_walk_macaddr(handle, linkid, &mac_state,
4070 	    print_phys_mac_callback));
4071 }
4072 
4073 /*
4074  * callback for dladm_walk_hwgrp, invoked for each MAC hwgrp
4075  */
4076 static boolean_t
4077 print_phys_hwgrp_callback(void *arg, dladm_hwgrp_attr_t *attr)
4078 {
4079 	print_phys_hwgrp_state_t *hwgrp_state = arg;
4080 	show_state_t *state = hwgrp_state->hs_state;
4081 
4082 	hwgrp_state->hs_grp_attr = attr;
4083 	ofmt_print(state->ls_ofmt, hwgrp_state);
4084 
4085 	return (B_TRUE);
4086 }
4087 
4088 /* invoked by show-phys -H for each physical data-link */
4089 static dladm_status_t
4090 print_phys_hwgrp(show_state_t *state, datalink_id_t linkid, char *link)
4091 {
4092 	print_phys_hwgrp_state_t hwgrp_state;
4093 
4094 	hwgrp_state.hs_state = state;
4095 	hwgrp_state.hs_link = link;
4096 	return (dladm_walk_hwgrp(handle, linkid, &hwgrp_state,
4097 	    print_phys_hwgrp_callback));
4098 }
4099 
4100 /*
4101  * Parse the "local=<laddr>,remote=<raddr>" sub-options for the -a option of
4102  * *-iptun subcommands.
4103  */
4104 static void
4105 iptun_process_addrarg(char *addrarg, iptun_params_t *params)
4106 {
4107 	char *addrval;
4108 
4109 	while (*addrarg != '\0') {
4110 		switch (getsubopt(&addrarg, iptun_addropts, &addrval)) {
4111 		case IPTUN_LOCAL:
4112 			if (addrval == NULL)
4113 				die("tunnel source address value is missing");
4114 			params->iptun_param_flags |= IPTUN_PARAM_LADDR;
4115 			if (strlcpy(params->iptun_param_laddr, addrval,
4116 			    sizeof (params->iptun_param_laddr)) >=
4117 			    sizeof (params->iptun_param_laddr))
4118 				die("tunnel source address is too long");
4119 			break;
4120 		case IPTUN_REMOTE:
4121 			if (addrval == NULL)
4122 				die("tunnel destination address value "
4123 				    "is missing");
4124 			params->iptun_param_flags |= IPTUN_PARAM_RADDR;
4125 			if (strlcpy(params->iptun_param_raddr, addrval,
4126 			    sizeof (params->iptun_param_raddr)) >=
4127 			    sizeof (params->iptun_param_raddr))
4128 				die("tunnel destination address is too long");
4129 			break;
4130 		default:
4131 			die("invalid address type: %s", addrval);
4132 			break;
4133 		}
4134 	}
4135 }
4136 
4137 /*
4138  * Convenience routine to process iptun-create/modify/delete subcommand
4139  * arguments.
4140  */
4141 static void
4142 iptun_process_args(int argc, char *argv[], const char *opts,
4143     iptun_params_t *params, uint32_t *flags, char *name, const char *use)
4144 {
4145 	int	option;
4146 	char	*altroot = NULL;
4147 
4148 	if (params != NULL)
4149 		bzero(params, sizeof (*params));
4150 	*flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
4151 
4152 	opterr = 0;
4153 	while ((option = getopt_long(argc, argv, opts, iptun_lopts, NULL)) !=
4154 	    -1) {
4155 		switch (option) {
4156 		case 'a':
4157 			iptun_process_addrarg(optarg, params);
4158 			break;
4159 		case 'R':
4160 			altroot = optarg;
4161 			break;
4162 		case 't':
4163 			*flags &= ~DLADM_OPT_PERSIST;
4164 			break;
4165 		case 'T':
4166 			params->iptun_param_type = iptun_gettypebyname(optarg);
4167 			if (params->iptun_param_type == IPTUN_TYPE_UNKNOWN)
4168 				die("unknown tunnel type: %s", optarg);
4169 			params->iptun_param_flags |= IPTUN_PARAM_TYPE;
4170 			break;
4171 		default:
4172 			die_opterr(optopt, option, use);
4173 			break;
4174 		}
4175 	}
4176 
4177 	/* Get the required tunnel name argument. */
4178 	if (argc - optind != 1)
4179 		usage();
4180 
4181 	if (strlcpy(name, argv[optind], MAXLINKNAMELEN) >= MAXLINKNAMELEN)
4182 		die("tunnel name is too long");
4183 
4184 	if (altroot != NULL)
4185 		altroot_cmd(altroot, argc, argv);
4186 }
4187 
4188 static void
4189 do_create_iptun(int argc, char *argv[], const char *use)
4190 {
4191 	iptun_params_t	params;
4192 	dladm_status_t	status;
4193 	uint32_t	flags;
4194 	char		name[MAXLINKNAMELEN];
4195 
4196 	iptun_process_args(argc, argv, ":a:R:tT:", &params, &flags, name,
4197 	    use);
4198 
4199 	status = dladm_iptun_create(handle, name, &params, flags);
4200 	if (status != DLADM_STATUS_OK)
4201 		die_dlerr(status, "could not create tunnel");
4202 }
4203 
4204 static void
4205 do_delete_iptun(int argc, char *argv[], const char *use)
4206 {
4207 	uint32_t	flags;
4208 	datalink_id_t	linkid;
4209 	dladm_status_t	status;
4210 	char		name[MAXLINKNAMELEN];
4211 
4212 	iptun_process_args(argc, argv, ":R:t", NULL, &flags, name, use);
4213 
4214 	status = dladm_name2info(handle, name, &linkid, NULL, NULL, NULL);
4215 	if (status != DLADM_STATUS_OK)
4216 		die_dlerr(status, "could not delete tunnel");
4217 	status = dladm_iptun_delete(handle, linkid, flags);
4218 	if (status != DLADM_STATUS_OK)
4219 		die_dlerr(status, "could not delete tunnel");
4220 }
4221 
4222 static void
4223 do_modify_iptun(int argc, char *argv[], const char *use)
4224 {
4225 	iptun_params_t	params;
4226 	uint32_t	flags;
4227 	dladm_status_t	status;
4228 	char		name[MAXLINKNAMELEN];
4229 
4230 	iptun_process_args(argc, argv, ":a:R:t", &params, &flags, name, use);
4231 
4232 	if ((status = dladm_name2info(handle, name, &params.iptun_param_linkid,
4233 	    NULL, NULL, NULL)) != DLADM_STATUS_OK)
4234 		die_dlerr(status, "could not modify tunnel");
4235 	status = dladm_iptun_modify(handle, &params, flags);
4236 	if (status != DLADM_STATUS_OK)
4237 		die_dlerr(status, "could not modify tunnel");
4238 }
4239 
4240 static void
4241 do_show_iptun(int argc, char *argv[], const char *use)
4242 {
4243 	char		option;
4244 	datalink_id_t	linkid;
4245 	uint32_t	flags = DLADM_OPT_ACTIVE;
4246 	char		*name = NULL;
4247 	dladm_status_t	status;
4248 	const char	*fields_str = NULL;
4249 	show_state_t	state;
4250 	ofmt_handle_t	ofmt;
4251 	ofmt_status_t	oferr;
4252 	uint_t		ofmtflags = 0;
4253 
4254 	bzero(&state, sizeof (state));
4255 	opterr = 0;
4256 	while ((option = getopt_long(argc, argv, ":pPo:",
4257 	    iptun_lopts, NULL)) != -1) {
4258 		switch (option) {
4259 		case 'o':
4260 			fields_str = optarg;
4261 			break;
4262 		case 'p':
4263 			state.ls_parsable = B_TRUE;
4264 			ofmtflags = OFMT_PARSABLE;
4265 			break;
4266 		case 'P':
4267 			flags = DLADM_OPT_PERSIST;
4268 			break;
4269 		default:
4270 			die_opterr(optopt, option, use);
4271 			break;
4272 		}
4273 	}
4274 
4275 	/*
4276 	 * Get the optional tunnel name argument.  If there is one, it must
4277 	 * be the last thing remaining on the command-line.
4278 	 */
4279 	if (argc - optind > 1)
4280 		die(gettext(use));
4281 	if (argc - optind == 1)
4282 		name = argv[optind];
4283 
4284 	oferr = ofmt_open(fields_str, iptun_fields, ofmtflags,
4285 	    DLADM_DEFAULT_COL, &ofmt);
4286 	ofmt_check(oferr, state.ls_parsable, ofmt, die, warn);
4287 
4288 	state.ls_ofmt = ofmt;
4289 	state.ls_flags = flags;
4290 
4291 	if (name == NULL) {
4292 		(void) dladm_walk_datalink_id(print_iptun_walker, handle,
4293 		    &state, DATALINK_CLASS_IPTUN, DATALINK_ANY_MEDIATYPE,
4294 		    flags);
4295 		status = state.ls_status;
4296 	} else {
4297 		if ((status = dladm_name2info(handle, name, &linkid, NULL, NULL,
4298 		    NULL)) == DLADM_STATUS_OK)
4299 			status = print_iptun(handle, linkid, &state);
4300 	}
4301 
4302 	if (status != DLADM_STATUS_OK)
4303 		die_dlerr(status, "unable to obtain tunnel status");
4304 }
4305 
4306 /* ARGSUSED */
4307 static void
4308 do_up_iptun(int argc, char *argv[], const char *use)
4309 {
4310 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
4311 	dladm_status_t	status = DLADM_STATUS_OK;
4312 
4313 	/*
4314 	 * Get the optional tunnel name argument.  If there is one, it must
4315 	 * be the last thing remaining on the command-line.
4316 	 */
4317 	if (argc - optind > 1)
4318 		usage();
4319 	if (argc - optind == 1) {
4320 		status = dladm_name2info(handle, argv[optind], &linkid, NULL,
4321 		    NULL, NULL);
4322 	}
4323 	if (status == DLADM_STATUS_OK)
4324 		status = dladm_iptun_up(handle, linkid);
4325 	if (status != DLADM_STATUS_OK)
4326 		die_dlerr(status, "unable to configure IP tunnel links");
4327 }
4328 
4329 /* ARGSUSED */
4330 static void
4331 do_down_iptun(int argc, char *argv[], const char *use)
4332 {
4333 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
4334 	dladm_status_t	status = DLADM_STATUS_OK;
4335 
4336 	/*
4337 	 * Get the optional tunnel name argument.  If there is one, it must
4338 	 * be the last thing remaining on the command-line.
4339 	 */
4340 	if (argc - optind > 1)
4341 		usage();
4342 	if (argc - optind == 1) {
4343 		status = dladm_name2info(handle, argv[optind], &linkid, NULL,
4344 		    NULL, NULL);
4345 	}
4346 	if (status == DLADM_STATUS_OK)
4347 		status = dladm_iptun_down(handle, linkid);
4348 	if (status != DLADM_STATUS_OK)
4349 		die_dlerr(status, "unable to bring down IP tunnel links");
4350 }
4351 
4352 static iptun_type_t
4353 iptun_gettypebyname(char *typestr)
4354 {
4355 	int i;
4356 
4357 	for (i = 0; iptun_types[i].type_name != NULL; i++) {
4358 		if (strncmp(iptun_types[i].type_name, typestr,
4359 		    strlen(iptun_types[i].type_name)) == 0) {
4360 			return (iptun_types[i].type_value);
4361 		}
4362 	}
4363 	return (IPTUN_TYPE_UNKNOWN);
4364 }
4365 
4366 static const char *
4367 iptun_gettypebyvalue(iptun_type_t type)
4368 {
4369 	int i;
4370 
4371 	for (i = 0; iptun_types[i].type_name != NULL; i++) {
4372 		if (iptun_types[i].type_value == type)
4373 			return (iptun_types[i].type_name);
4374 	}
4375 	return (NULL);
4376 }
4377 
4378 static dladm_status_t
4379 print_iptun(dladm_handle_t dh, datalink_id_t linkid, show_state_t *state)
4380 {
4381 	dladm_status_t		status;
4382 	iptun_params_t		params;
4383 	iptun_fields_buf_t	lbuf;
4384 	const char		*laddr;
4385 	const char		*raddr;
4386 
4387 	params.iptun_param_linkid = linkid;
4388 	status = dladm_iptun_getparams(dh, &params, state->ls_flags);
4389 	if (status != DLADM_STATUS_OK)
4390 		return (status);
4391 
4392 	/* LINK */
4393 	status = dladm_datalink_id2info(dh, linkid, NULL, NULL, NULL,
4394 	    lbuf.iptun_name, sizeof (lbuf.iptun_name));
4395 	if (status != DLADM_STATUS_OK)
4396 		return (status);
4397 
4398 	/* TYPE */
4399 	(void) strlcpy(lbuf.iptun_type,
4400 	    iptun_gettypebyvalue(params.iptun_param_type),
4401 	    sizeof (lbuf.iptun_type));
4402 
4403 	/* FLAGS */
4404 	(void) memset(lbuf.iptun_flags, '-', IPTUN_NUM_FLAGS);
4405 	lbuf.iptun_flags[IPTUN_NUM_FLAGS] = '\0';
4406 	if (params.iptun_param_flags & IPTUN_PARAM_IPSECPOL)
4407 		lbuf.iptun_flags[IPTUN_SFLAG_INDEX] = 's';
4408 	if (params.iptun_param_flags & IPTUN_PARAM_IMPLICIT)
4409 		lbuf.iptun_flags[IPTUN_IFLAG_INDEX] = 'i';
4410 
4411 	/* LOCAL */
4412 	if (params.iptun_param_flags & IPTUN_PARAM_LADDR)
4413 		laddr = params.iptun_param_laddr;
4414 	else
4415 		laddr = (state->ls_parsable) ? "" : "--";
4416 	(void) strlcpy(lbuf.iptun_laddr, laddr, sizeof (lbuf.iptun_laddr));
4417 
4418 	/* REMOTE */
4419 	if (params.iptun_param_flags & IPTUN_PARAM_RADDR)
4420 		raddr = params.iptun_param_raddr;
4421 	else
4422 		raddr = (state->ls_parsable) ? "" : "--";
4423 	(void) strlcpy(lbuf.iptun_raddr, raddr, sizeof (lbuf.iptun_raddr));
4424 
4425 	ofmt_print(state->ls_ofmt, &lbuf);
4426 
4427 	return (DLADM_STATUS_OK);
4428 }
4429 
4430 static int
4431 print_iptun_walker(dladm_handle_t dh, datalink_id_t linkid, void *arg)
4432 {
4433 	((show_state_t *)arg)->ls_status = print_iptun(dh, linkid, arg);
4434 	return (DLADM_WALK_CONTINUE);
4435 }
4436 
4437 static dladm_status_t
4438 print_phys(show_state_t *state, datalink_id_t linkid)
4439 {
4440 	char			link[MAXLINKNAMELEN];
4441 	uint32_t		flags;
4442 	dladm_status_t		status;
4443 	datalink_class_t	class;
4444 	uint32_t		media;
4445 
4446 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, &class,
4447 	    &media, link, MAXLINKNAMELEN)) != DLADM_STATUS_OK) {
4448 		goto done;
4449 	}
4450 
4451 	if (class != DATALINK_CLASS_PHYS) {
4452 		status = DLADM_STATUS_BADARG;
4453 		goto done;
4454 	}
4455 
4456 	if (!(state->ls_flags & flags)) {
4457 		status = DLADM_STATUS_NOTFOUND;
4458 		goto done;
4459 	}
4460 
4461 	if (state->ls_mac)
4462 		status = print_phys_mac(state, linkid, link);
4463 	else if (state->ls_hwgrp)
4464 		status = print_phys_hwgrp(state, linkid, link);
4465 	else
4466 		status = print_phys_default(state, linkid, link, flags, media);
4467 
4468 done:
4469 	return (status);
4470 }
4471 
4472 /* ARGSUSED */
4473 static int
4474 show_phys(dladm_handle_t dh, datalink_id_t linkid, void *arg)
4475 {
4476 	show_state_t	*state = arg;
4477 
4478 	state->ls_status = print_phys(state, linkid);
4479 	return (DLADM_WALK_CONTINUE);
4480 }
4481 
4482 /*
4483  * Print the active topology information.
4484  */
4485 static dladm_status_t
4486 print_vlan(show_state_t *state, datalink_id_t linkid, link_fields_buf_t *l)
4487 {
4488 	dladm_vlan_attr_t	vinfo;
4489 	uint32_t		flags;
4490 	dladm_status_t		status;
4491 
4492 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, NULL, NULL,
4493 	    l->link_name, sizeof (l->link_name))) != DLADM_STATUS_OK) {
4494 		goto done;
4495 	}
4496 
4497 	if (!(state->ls_flags & flags)) {
4498 		status = DLADM_STATUS_NOTFOUND;
4499 		goto done;
4500 	}
4501 
4502 	if ((status = dladm_vlan_info(handle, linkid, &vinfo,
4503 	    state->ls_flags)) != DLADM_STATUS_OK ||
4504 	    (status = dladm_datalink_id2info(handle, vinfo.dv_linkid, NULL,
4505 	    NULL, NULL, l->link_over, sizeof (l->link_over))) !=
4506 	    DLADM_STATUS_OK) {
4507 		goto done;
4508 	}
4509 
4510 	(void) snprintf(l->link_vlan_vid, sizeof (l->link_vlan_vid), "%d",
4511 	    vinfo.dv_vid);
4512 	(void) snprintf(l->link_flags, sizeof (l->link_flags), "%c----",
4513 	    vinfo.dv_force ? 'f' : '-');
4514 
4515 done:
4516 	return (status);
4517 }
4518 
4519 /* ARGSUSED */
4520 static int
4521 show_vlan(dladm_handle_t dh, datalink_id_t linkid, void *arg)
4522 {
4523 	show_state_t		*state = arg;
4524 	dladm_status_t		status;
4525 	link_fields_buf_t	lbuf;
4526 
4527 	bzero(&lbuf, sizeof (link_fields_buf_t));
4528 	status = print_vlan(state, linkid, &lbuf);
4529 	if (status != DLADM_STATUS_OK)
4530 		goto done;
4531 
4532 	ofmt_print(state->ls_ofmt, &lbuf);
4533 
4534 done:
4535 	state->ls_status = status;
4536 	return (DLADM_WALK_CONTINUE);
4537 }
4538 
4539 static void
4540 do_show_phys(int argc, char *argv[], const char *use)
4541 {
4542 	int		option;
4543 	uint32_t	flags = DLADM_OPT_ACTIVE;
4544 	boolean_t	p_arg = B_FALSE;
4545 	boolean_t	o_arg = B_FALSE;
4546 	boolean_t	m_arg = B_FALSE;
4547 	boolean_t	H_arg = B_FALSE;
4548 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
4549 	show_state_t	state;
4550 	dladm_status_t	status;
4551 	char		*fields_str = NULL;
4552 	char		*all_active_fields =
4553 	    "link,media,state,speed,duplex,device";
4554 	char		*all_inactive_fields = "link,device,media,flags";
4555 	char		*all_mac_fields = "link,slot,address,inuse,client";
4556 	char		*all_hwgrp_fields = "link,ringtype,rings,clients";
4557 	const ofmt_field_t *pf;
4558 	ofmt_handle_t	ofmt;
4559 	ofmt_status_t	oferr;
4560 	uint_t		ofmtflags = 0;
4561 
4562 	bzero(&state, sizeof (state));
4563 	opterr = 0;
4564 	while ((option = getopt_long(argc, argv, ":pPo:mH",
4565 	    show_lopts, NULL)) != -1) {
4566 		switch (option) {
4567 		case 'p':
4568 			if (p_arg)
4569 				die_optdup(option);
4570 
4571 			p_arg = B_TRUE;
4572 			break;
4573 		case 'P':
4574 			if (flags != DLADM_OPT_ACTIVE)
4575 				die_optdup(option);
4576 
4577 			flags = DLADM_OPT_PERSIST;
4578 			break;
4579 		case 'o':
4580 			o_arg = B_TRUE;
4581 			fields_str = optarg;
4582 			break;
4583 		case 'm':
4584 			m_arg = B_TRUE;
4585 			break;
4586 		case 'H':
4587 			H_arg = B_TRUE;
4588 			break;
4589 		default:
4590 			die_opterr(optopt, option, use);
4591 			break;
4592 		}
4593 	}
4594 
4595 	if (p_arg && !o_arg)
4596 		die("-p requires -o");
4597 
4598 	if (m_arg && H_arg)
4599 		die("-m cannot combine with -H");
4600 
4601 	if (p_arg && strcasecmp(fields_str, "all") == 0)
4602 		die("\"-o all\" is invalid with -p");
4603 
4604 	/* get link name (optional last argument) */
4605 	if (optind == (argc-1)) {
4606 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
4607 		    NULL, NULL, NULL)) != DLADM_STATUS_OK) {
4608 			die_dlerr(status, "link %s is not valid", argv[optind]);
4609 		}
4610 	} else if (optind != argc) {
4611 		usage();
4612 	}
4613 
4614 	state.ls_parsable = p_arg;
4615 	state.ls_flags = flags;
4616 	state.ls_donefirst = B_FALSE;
4617 	state.ls_mac = m_arg;
4618 	state.ls_hwgrp = H_arg;
4619 
4620 	if (m_arg && !(flags & DLADM_OPT_ACTIVE)) {
4621 		/*
4622 		 * We can only display the factory MAC addresses of
4623 		 * active data-links.
4624 		 */
4625 		die("-m not compatible with -P");
4626 	}
4627 
4628 	if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0)) {
4629 		if (state.ls_mac)
4630 			fields_str = all_mac_fields;
4631 		else if (state.ls_hwgrp)
4632 			fields_str = all_hwgrp_fields;
4633 		else if (state.ls_flags & DLADM_OPT_ACTIVE) {
4634 			fields_str = all_active_fields;
4635 		} else {
4636 			fields_str = all_inactive_fields;
4637 		}
4638 	}
4639 
4640 	if (state.ls_mac) {
4641 		pf = phys_m_fields;
4642 	} else if (state.ls_hwgrp) {
4643 		pf = phys_h_fields;
4644 	} else {
4645 		pf = phys_fields;
4646 	}
4647 
4648 	if (state.ls_parsable)
4649 		ofmtflags |= OFMT_PARSABLE;
4650 	oferr = ofmt_open(fields_str, pf, ofmtflags, 0, &ofmt);
4651 	ofmt_check(oferr, state.ls_parsable, ofmt, die, warn);
4652 	state.ls_ofmt = ofmt;
4653 
4654 	if (linkid == DATALINK_ALL_LINKID) {
4655 		(void) dladm_walk_datalink_id(show_phys, handle, &state,
4656 		    DATALINK_CLASS_PHYS, DATALINK_ANY_MEDIATYPE, flags);
4657 	} else {
4658 		(void) show_phys(handle, linkid, &state);
4659 		if (state.ls_status != DLADM_STATUS_OK) {
4660 			die_dlerr(state.ls_status,
4661 			    "failed to show physical link %s", argv[optind]);
4662 		}
4663 	}
4664 	ofmt_close(ofmt);
4665 }
4666 
4667 static void
4668 do_show_vlan(int argc, char *argv[], const char *use)
4669 {
4670 	int		option;
4671 	uint32_t	flags = DLADM_OPT_ACTIVE;
4672 	boolean_t	p_arg = B_FALSE;
4673 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
4674 	show_state_t	state;
4675 	dladm_status_t	status;
4676 	boolean_t	o_arg = B_FALSE;
4677 	char		*fields_str = NULL;
4678 	ofmt_handle_t	ofmt;
4679 	ofmt_status_t	oferr;
4680 	uint_t		ofmtflags = 0;
4681 
4682 	bzero(&state, sizeof (state));
4683 
4684 	opterr = 0;
4685 	while ((option = getopt_long(argc, argv, ":pPo:",
4686 	    show_lopts, NULL)) != -1) {
4687 		switch (option) {
4688 		case 'p':
4689 			if (p_arg)
4690 				die_optdup(option);
4691 
4692 			p_arg = B_TRUE;
4693 			break;
4694 		case 'P':
4695 			if (flags != DLADM_OPT_ACTIVE)
4696 				die_optdup(option);
4697 
4698 			flags = DLADM_OPT_PERSIST;
4699 			break;
4700 		case 'o':
4701 			o_arg = B_TRUE;
4702 			fields_str = optarg;
4703 			break;
4704 		default:
4705 			die_opterr(optopt, option, use);
4706 			break;
4707 		}
4708 	}
4709 
4710 	/* get link name (optional last argument) */
4711 	if (optind == (argc-1)) {
4712 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
4713 		    NULL, NULL, NULL)) != DLADM_STATUS_OK) {
4714 			die_dlerr(status, "link %s is not valid", argv[optind]);
4715 		}
4716 	} else if (optind != argc) {
4717 		usage();
4718 	}
4719 
4720 	state.ls_parsable = p_arg;
4721 	state.ls_flags = flags;
4722 	state.ls_donefirst = B_FALSE;
4723 
4724 	if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0))
4725 		fields_str = NULL;
4726 
4727 	if (state.ls_parsable)
4728 		ofmtflags |= OFMT_PARSABLE;
4729 	oferr = ofmt_open(fields_str, vlan_fields, ofmtflags, 0, &ofmt);
4730 	ofmt_check(oferr, state.ls_parsable, ofmt, die, warn);
4731 	state.ls_ofmt = ofmt;
4732 
4733 	if (linkid == DATALINK_ALL_LINKID) {
4734 		(void) dladm_walk_datalink_id(show_vlan, handle, &state,
4735 		    DATALINK_CLASS_VLAN, DATALINK_ANY_MEDIATYPE, flags);
4736 	} else {
4737 		(void) show_vlan(handle, linkid, &state);
4738 		if (state.ls_status != DLADM_STATUS_OK) {
4739 			die_dlerr(state.ls_status, "failed to show vlan %s",
4740 			    argv[optind]);
4741 		}
4742 	}
4743 	ofmt_close(ofmt);
4744 }
4745 
4746 static void
4747 do_create_vnic(int argc, char *argv[], const char *use)
4748 {
4749 	datalink_id_t		linkid, dev_linkid;
4750 	char			devname[MAXLINKNAMELEN];
4751 	char			name[MAXLINKNAMELEN];
4752 	boolean_t		l_arg = B_FALSE;
4753 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
4754 	char			*altroot = NULL;
4755 	int			option;
4756 	char			*endp = NULL;
4757 	dladm_status_t		status;
4758 	vnic_mac_addr_type_t	mac_addr_type = VNIC_MAC_ADDR_TYPE_UNKNOWN;
4759 	uchar_t			*mac_addr = NULL;
4760 	int			mac_slot = -1;
4761 	uint_t			maclen = 0, mac_prefix_len = 0;
4762 	char			propstr[DLADM_STRSIZE];
4763 	dladm_arg_list_t	*proplist = NULL;
4764 	int			vid = 0;
4765 	int			af = AF_UNSPEC;
4766 	vrid_t			vrid = VRRP_VRID_NONE;
4767 
4768 	opterr = 0;
4769 	bzero(propstr, DLADM_STRSIZE);
4770 
4771 	while ((option = getopt_long(argc, argv, ":tfR:l:m:n:p:r:v:V:A:H",
4772 	    vnic_lopts, NULL)) != -1) {
4773 		switch (option) {
4774 		case 't':
4775 			flags &= ~DLADM_OPT_PERSIST;
4776 			break;
4777 		case 'R':
4778 			altroot = optarg;
4779 			break;
4780 		case 'l':
4781 			if (strlcpy(devname, optarg, MAXLINKNAMELEN) >=
4782 			    MAXLINKNAMELEN)
4783 				die("link name too long");
4784 			l_arg = B_TRUE;
4785 			break;
4786 		case 'm':
4787 			if (mac_addr_type != VNIC_MAC_ADDR_TYPE_UNKNOWN)
4788 				die("cannot specify -m option twice");
4789 
4790 			if (strcmp(optarg, "fixed") == 0) {
4791 				/*
4792 				 * A fixed MAC address must be specified
4793 				 * by its value, not by the keyword 'fixed'.
4794 				 */
4795 				die("'fixed' is not a valid MAC address");
4796 			}
4797 			if (dladm_vnic_str2macaddrtype(optarg,
4798 			    &mac_addr_type) != DLADM_STATUS_OK) {
4799 				mac_addr_type = VNIC_MAC_ADDR_TYPE_FIXED;
4800 				/* MAC address specified by value */
4801 				mac_addr = _link_aton(optarg, (int *)&maclen);
4802 				if (mac_addr == NULL) {
4803 					if (maclen == (uint_t)-1)
4804 						die("invalid MAC address");
4805 					else
4806 						die("out of memory");
4807 				}
4808 			}
4809 			break;
4810 		case 'n':
4811 			errno = 0;
4812 			mac_slot = (int)strtol(optarg, &endp, 10);
4813 			if (errno != 0 || *endp != '\0')
4814 				die("invalid slot number");
4815 			break;
4816 		case 'p':
4817 			(void) strlcat(propstr, optarg, DLADM_STRSIZE);
4818 			if (strlcat(propstr, ",", DLADM_STRSIZE) >=
4819 			    DLADM_STRSIZE)
4820 				die("property list too long '%s'", propstr);
4821 			break;
4822 		case 'r':
4823 			mac_addr = _link_aton(optarg, (int *)&mac_prefix_len);
4824 			if (mac_addr == NULL) {
4825 				if (mac_prefix_len == (uint_t)-1)
4826 					die("invalid MAC address");
4827 				else
4828 					die("out of memory");
4829 			}
4830 			break;
4831 		case 'V':
4832 			if (!str2int(optarg, (int *)&vrid) ||
4833 			    vrid < VRRP_VRID_MIN || vrid > VRRP_VRID_MAX) {
4834 				die("invalid VRRP identifier '%s'", optarg);
4835 			}
4836 
4837 			break;
4838 		case 'A':
4839 			if (strcmp(optarg, "inet") == 0)
4840 				af = AF_INET;
4841 			else if (strcmp(optarg, "inet6") == 0)
4842 				af = AF_INET6;
4843 			else
4844 				die("invalid address family '%s'", optarg);
4845 			break;
4846 		case 'v':
4847 			if (vid != 0)
4848 				die_optdup(option);
4849 
4850 			if (!str2int(optarg, &vid) || vid < 1 || vid > 4094)
4851 				die("invalid VLAN identifier '%s'", optarg);
4852 
4853 			break;
4854 		case 'f':
4855 			flags |= DLADM_OPT_FORCE;
4856 			break;
4857 		default:
4858 			die_opterr(optopt, option, use);
4859 		}
4860 	}
4861 
4862 	if (mac_addr_type == VNIC_MAC_ADDR_TYPE_UNKNOWN)
4863 		mac_addr_type = VNIC_MAC_ADDR_TYPE_AUTO;
4864 
4865 	/*
4866 	 * 'f' - force, flag can be specified only with 'v' - vlan.
4867 	 */
4868 	if ((flags & DLADM_OPT_FORCE) != 0 && vid == 0)
4869 		die("-f option can only be used with -v");
4870 
4871 	if (mac_prefix_len != 0 && mac_addr_type != VNIC_MAC_ADDR_TYPE_RANDOM &&
4872 	    mac_addr_type != VNIC_MAC_ADDR_TYPE_FIXED)
4873 		usage();
4874 
4875 	if (mac_addr_type == VNIC_MAC_ADDR_TYPE_VRID) {
4876 		if (vrid == VRRP_VRID_NONE || af == AF_UNSPEC ||
4877 		    mac_addr != NULL || maclen != 0 || mac_slot != -1 ||
4878 		    mac_prefix_len != 0) {
4879 			usage();
4880 		}
4881 	} else if ((af != AF_UNSPEC || vrid != VRRP_VRID_NONE)) {
4882 		usage();
4883 	}
4884 
4885 	/* check required options */
4886 	if (!l_arg)
4887 		usage();
4888 
4889 	if (mac_slot != -1 && mac_addr_type != VNIC_MAC_ADDR_TYPE_FACTORY)
4890 		usage();
4891 
4892 	/* the VNIC id is the required operand */
4893 	if (optind != (argc - 1))
4894 		usage();
4895 
4896 	if (strlcpy(name, argv[optind], MAXLINKNAMELEN) >= MAXLINKNAMELEN)
4897 		die("link name too long '%s'", argv[optind]);
4898 
4899 	if (!dladm_valid_linkname(name))
4900 		die("invalid link name '%s'", argv[optind]);
4901 
4902 	if (altroot != NULL)
4903 		altroot_cmd(altroot, argc, argv);
4904 
4905 	if (dladm_name2info(handle, devname, &dev_linkid, NULL, NULL, NULL) !=
4906 	    DLADM_STATUS_OK)
4907 		die("invalid link name '%s'", devname);
4908 
4909 	if (dladm_parse_link_props(propstr, &proplist, B_FALSE)
4910 	    != DLADM_STATUS_OK)
4911 		die("invalid vnic property");
4912 
4913 	status = dladm_vnic_create(handle, name, dev_linkid, mac_addr_type,
4914 	    mac_addr, maclen, &mac_slot, mac_prefix_len, vid, vrid, af,
4915 	    &linkid, proplist, &errlist, flags);
4916 	switch (status) {
4917 	case DLADM_STATUS_OK:
4918 		break;
4919 
4920 	case DLADM_STATUS_LINKBUSY:
4921 		die("VLAN over '%s' may not use default_tag ID "
4922 		    "(see dladm(8))", devname);
4923 		break;
4924 
4925 	default:
4926 		die_dlerrlist(status, &errlist, "vnic creation over %s failed",
4927 		    devname);
4928 	}
4929 
4930 	dladm_free_props(proplist);
4931 	free(mac_addr);
4932 }
4933 
4934 static void
4935 do_etherstub_check(const char *name, datalink_id_t linkid, boolean_t etherstub,
4936     uint32_t flags)
4937 {
4938 	boolean_t is_etherstub;
4939 	dladm_vnic_attr_t attr;
4940 
4941 	if (dladm_vnic_info(handle, linkid, &attr, flags) != DLADM_STATUS_OK) {
4942 		/*
4943 		 * Let the delete continue anyway.
4944 		 */
4945 		return;
4946 	}
4947 	is_etherstub = (attr.va_link_id == DATALINK_INVALID_LINKID);
4948 	if (is_etherstub != etherstub) {
4949 		die("'%s' is not %s", name,
4950 		    (is_etherstub ? "a vnic" : "an etherstub"));
4951 	}
4952 }
4953 
4954 static void
4955 do_delete_vnic_common(int argc, char *argv[], const char *use,
4956     boolean_t etherstub)
4957 {
4958 	int option;
4959 	uint32_t flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
4960 	datalink_id_t linkid;
4961 	char *altroot = NULL;
4962 	dladm_status_t status;
4963 
4964 	opterr = 0;
4965 	while ((option = getopt_long(argc, argv, ":R:t", lopts,
4966 	    NULL)) != -1) {
4967 		switch (option) {
4968 		case 't':
4969 			flags &= ~DLADM_OPT_PERSIST;
4970 			break;
4971 		case 'R':
4972 			altroot = optarg;
4973 			break;
4974 		default:
4975 			die_opterr(optopt, option, use);
4976 		}
4977 	}
4978 
4979 	/* get vnic name (required last argument) */
4980 	if (optind != (argc - 1))
4981 		usage();
4982 
4983 	if (altroot != NULL)
4984 		altroot_cmd(altroot, argc, argv);
4985 
4986 	status = dladm_name2info(handle, argv[optind], &linkid, NULL, NULL,
4987 	    NULL);
4988 	if (status != DLADM_STATUS_OK)
4989 		die("invalid link name '%s'", argv[optind]);
4990 
4991 	if ((flags & DLADM_OPT_ACTIVE) != 0) {
4992 		do_etherstub_check(argv[optind], linkid, etherstub,
4993 		    DLADM_OPT_ACTIVE);
4994 	}
4995 	if ((flags & DLADM_OPT_PERSIST) != 0) {
4996 		do_etherstub_check(argv[optind], linkid, etherstub,
4997 		    DLADM_OPT_PERSIST);
4998 	}
4999 
5000 	status = dladm_vnic_delete(handle, linkid, flags);
5001 	if (status != DLADM_STATUS_OK)
5002 		die_dlerr(status, "vnic deletion failed");
5003 }
5004 
5005 static void
5006 do_delete_vnic(int argc, char *argv[], const char *use)
5007 {
5008 	do_delete_vnic_common(argc, argv, use, B_FALSE);
5009 }
5010 
5011 /* ARGSUSED */
5012 static void
5013 do_up_vnic_common(int argc, char *argv[], const char *use, boolean_t vlan)
5014 {
5015 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
5016 	dladm_status_t	status;
5017 	char		*type;
5018 
5019 	type = vlan ? "vlan" : "vnic";
5020 
5021 	/*
5022 	 * get the id or the name of the vnic/vlan (optional last argument)
5023 	 */
5024 	if (argc == 2) {
5025 		status = dladm_name2info(handle, argv[1], &linkid, NULL, NULL,
5026 		    NULL);
5027 		if (status != DLADM_STATUS_OK)
5028 			goto done;
5029 
5030 	} else if (argc > 2) {
5031 		usage();
5032 	}
5033 
5034 	if (vlan)
5035 		status = dladm_vlan_up(handle, linkid);
5036 	else
5037 		status = dladm_vnic_up(handle, linkid, 0);
5038 
5039 done:
5040 	if (status != DLADM_STATUS_OK) {
5041 		if (argc == 2) {
5042 			die_dlerr(status,
5043 			    "could not bring up %s '%s'", type, argv[1]);
5044 		} else {
5045 			die_dlerr(status, "could not bring %ss up", type);
5046 		}
5047 	}
5048 }
5049 
5050 static void
5051 do_up_vnic(int argc, char *argv[], const char *use)
5052 {
5053 	do_up_vnic_common(argc, argv, use, B_FALSE);
5054 }
5055 
5056 static void
5057 dump_vnics_head(const char *dev)
5058 {
5059 	if (strlen(dev))
5060 		(void) printf("%s", dev);
5061 
5062 	(void) printf("\tipackets  rbytes      opackets  obytes          ");
5063 
5064 	if (strlen(dev))
5065 		(void) printf("%%ipkts  %%opkts\n");
5066 	else
5067 		(void) printf("\n");
5068 }
5069 
5070 static void
5071 dump_vnic_stat(const char *name, datalink_id_t vnic_id,
5072     show_vnic_state_t *state, pktsum_t *vnic_stats, pktsum_t *tot_stats)
5073 {
5074 	pktsum_t	diff_stats;
5075 	pktsum_t	*old_stats = &state->vs_prevstats[vnic_id];
5076 
5077 	dladm_stats_diff(&diff_stats, vnic_stats, old_stats);
5078 
5079 	(void) printf("%s", name);
5080 
5081 	(void) printf("\t%-10llu", diff_stats.ipackets);
5082 	(void) printf("%-12llu", diff_stats.rbytes);
5083 	(void) printf("%-10llu", diff_stats.opackets);
5084 	(void) printf("%-12llu", diff_stats.obytes);
5085 
5086 	if (tot_stats) {
5087 		if (tot_stats->ipackets == 0) {
5088 			(void) printf("\t-");
5089 		} else {
5090 			(void) printf("\t%-6.1f", (double)diff_stats.ipackets/
5091 			    (double)tot_stats->ipackets * 100);
5092 		}
5093 		if (tot_stats->opackets == 0) {
5094 			(void) printf("\t-");
5095 		} else {
5096 			(void) printf("\t%-6.1f", (double)diff_stats.opackets/
5097 			    (double)tot_stats->opackets * 100);
5098 		}
5099 	}
5100 	(void) printf("\n");
5101 
5102 	*old_stats = *vnic_stats;
5103 }
5104 
5105 /*
5106  * Called from the walker dladm_vnic_walk_sys() for each vnic to display
5107  * vnic information or statistics.
5108  */
5109 static dladm_status_t
5110 print_vnic(show_vnic_state_t *state, datalink_id_t linkid)
5111 {
5112 	dladm_vnic_attr_t	attr, *vnic = &attr;
5113 	dladm_status_t		status;
5114 	boolean_t		is_etherstub;
5115 	char			devname[MAXLINKNAMELEN];
5116 	char			vnic_name[MAXLINKNAMELEN];
5117 	char			mstr[MAXMACADDRLEN * 3];
5118 	vnic_fields_buf_t	vbuf;
5119 
5120 	if ((status = dladm_vnic_info(handle, linkid, vnic, state->vs_flags)) !=
5121 	    DLADM_STATUS_OK)
5122 		return (status);
5123 
5124 	is_etherstub = (vnic->va_link_id == DATALINK_INVALID_LINKID);
5125 	if (state->vs_etherstub != is_etherstub) {
5126 		/*
5127 		 * Want all etherstub but it's not one, or want
5128 		 * non-etherstub and it's one.
5129 		 */
5130 		return (DLADM_STATUS_OK);
5131 	}
5132 
5133 	if (state->vs_link_id != DATALINK_ALL_LINKID) {
5134 		if (state->vs_link_id != vnic->va_link_id)
5135 			return (DLADM_STATUS_OK);
5136 	}
5137 
5138 	if (dladm_datalink_id2info(handle, linkid, NULL, NULL,
5139 	    NULL, vnic_name, sizeof (vnic_name)) != DLADM_STATUS_OK)
5140 		return (DLADM_STATUS_BADARG);
5141 
5142 	bzero(devname, sizeof (devname));
5143 	if (!is_etherstub &&
5144 	    dladm_datalink_id2info(handle, vnic->va_link_id, NULL, NULL,
5145 	    NULL, devname, sizeof (devname)) != DLADM_STATUS_OK)
5146 		(void) sprintf(devname, "?");
5147 
5148 	state->vs_found = B_TRUE;
5149 	if (state->vs_stats) {
5150 		/* print vnic statistics */
5151 		pktsum_t vnic_stats;
5152 
5153 		if (state->vs_firstonly) {
5154 			if (state->vs_donefirst)
5155 				return (0);
5156 			state->vs_donefirst = B_TRUE;
5157 		}
5158 
5159 		if (!state->vs_printstats) {
5160 			/*
5161 			 * get vnic statistics and add to the sum for the
5162 			 * named device.
5163 			 */
5164 			get_link_stats(vnic_name, &vnic_stats);
5165 			dladm_stats_total(&state->vs_totalstats, &vnic_stats,
5166 			    &state->vs_prevstats[vnic->va_vnic_id]);
5167 		} else {
5168 			/* get and print vnic statistics */
5169 			get_link_stats(vnic_name, &vnic_stats);
5170 			dump_vnic_stat(vnic_name, linkid, state, &vnic_stats,
5171 			    &state->vs_totalstats);
5172 		}
5173 		return (DLADM_STATUS_OK);
5174 	} else {
5175 		(void) snprintf(vbuf.vnic_link, sizeof (vbuf.vnic_link),
5176 		    "%s", vnic_name);
5177 
5178 		if (!is_etherstub) {
5179 
5180 			(void) snprintf(vbuf.vnic_over, sizeof (vbuf.vnic_over),
5181 			    "%s", devname);
5182 			(void) snprintf(vbuf.vnic_speed,
5183 			    sizeof (vbuf.vnic_speed), "%u",
5184 			    (uint_t)((get_ifspeed(vnic_name, B_TRUE))
5185 			    / 1000000ull));
5186 
5187 			switch (vnic->va_mac_addr_type) {
5188 			case VNIC_MAC_ADDR_TYPE_FIXED:
5189 			case VNIC_MAC_ADDR_TYPE_PRIMARY:
5190 				(void) snprintf(vbuf.vnic_macaddrtype,
5191 				    sizeof (vbuf.vnic_macaddrtype),
5192 				    gettext("fixed"));
5193 				break;
5194 			case VNIC_MAC_ADDR_TYPE_RANDOM:
5195 				(void) snprintf(vbuf.vnic_macaddrtype,
5196 				    sizeof (vbuf.vnic_macaddrtype),
5197 				    gettext("random"));
5198 				break;
5199 			case VNIC_MAC_ADDR_TYPE_FACTORY:
5200 				(void) snprintf(vbuf.vnic_macaddrtype,
5201 				    sizeof (vbuf.vnic_macaddrtype),
5202 				    gettext("factory, slot %d"),
5203 				    vnic->va_mac_slot);
5204 				break;
5205 			case VNIC_MAC_ADDR_TYPE_VRID:
5206 				(void) snprintf(vbuf.vnic_macaddrtype,
5207 				    sizeof (vbuf.vnic_macaddrtype),
5208 				    gettext("vrrp, %d/%s"),
5209 				    vnic->va_vrid, vnic->va_af == AF_INET ?
5210 				    "inet" : "inet6");
5211 				break;
5212 			}
5213 
5214 			if (strlen(vbuf.vnic_macaddrtype) > 0) {
5215 				(void) snprintf(vbuf.vnic_macaddr,
5216 				    sizeof (vbuf.vnic_macaddr), "%s",
5217 				    dladm_aggr_macaddr2str(vnic->va_mac_addr,
5218 				    mstr));
5219 			}
5220 
5221 			(void) snprintf(vbuf.vnic_vid, sizeof (vbuf.vnic_vid),
5222 			    "%d", vnic->va_vid);
5223 		}
5224 
5225 		ofmt_print(state->vs_ofmt, &vbuf);
5226 
5227 		return (DLADM_STATUS_OK);
5228 	}
5229 }
5230 
5231 /* ARGSUSED */
5232 static int
5233 show_vnic(dladm_handle_t dh, datalink_id_t linkid, void *arg)
5234 {
5235 	show_vnic_state_t	*state = arg;
5236 
5237 	state->vs_status = print_vnic(state, linkid);
5238 	return (DLADM_WALK_CONTINUE);
5239 }
5240 
5241 static void
5242 do_show_vnic_common(int argc, char *argv[], const char *use,
5243     boolean_t etherstub)
5244 {
5245 	int			option;
5246 	boolean_t		s_arg = B_FALSE;
5247 	boolean_t		i_arg = B_FALSE;
5248 	boolean_t		l_arg = B_FALSE;
5249 	uint32_t		interval = 0, flags = DLADM_OPT_ACTIVE;
5250 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
5251 	datalink_id_t		dev_linkid = DATALINK_ALL_LINKID;
5252 	show_vnic_state_t	state;
5253 	dladm_status_t		status;
5254 	boolean_t		o_arg = B_FALSE;
5255 	char			*fields_str = NULL;
5256 	const ofmt_field_t	*pf;
5257 	char			*all_e_fields = "link";
5258 	ofmt_handle_t		ofmt;
5259 	ofmt_status_t		oferr;
5260 	uint_t			ofmtflags = 0;
5261 
5262 	bzero(&state, sizeof (state));
5263 	opterr = 0;
5264 	while ((option = getopt_long(argc, argv, ":pPl:si:o:", lopts,
5265 	    NULL)) != -1) {
5266 		switch (option) {
5267 		case 'p':
5268 			state.vs_parsable = B_TRUE;
5269 			break;
5270 		case 'P':
5271 			flags = DLADM_OPT_PERSIST;
5272 			break;
5273 		case 'l':
5274 			if (etherstub)
5275 				die("option not supported for this command");
5276 
5277 			if (strlcpy(state.vs_link, optarg, MAXLINKNAMELEN) >=
5278 			    MAXLINKNAMELEN)
5279 				die("link name too long");
5280 
5281 			l_arg = B_TRUE;
5282 			break;
5283 		case 's':
5284 			if (s_arg) {
5285 				die("the option -s cannot be specified "
5286 				    "more than once");
5287 			}
5288 			s_arg = B_TRUE;
5289 			break;
5290 		case 'i':
5291 			if (i_arg) {
5292 				die("the option -i cannot be specified "
5293 				    "more than once");
5294 			}
5295 			i_arg = B_TRUE;
5296 			if (!dladm_str2interval(optarg, &interval))
5297 				die("invalid interval value '%s'", optarg);
5298 			break;
5299 		case 'o':
5300 			o_arg = B_TRUE;
5301 			fields_str = optarg;
5302 			break;
5303 		default:
5304 			die_opterr(optopt, option, use);
5305 		}
5306 	}
5307 
5308 	if (i_arg && !s_arg)
5309 		die("the option -i can be used only with -s");
5310 
5311 	/* get vnic ID (optional last argument) */
5312 	if (optind == (argc - 1)) {
5313 		status = dladm_name2info(handle, argv[optind], &linkid, NULL,
5314 		    NULL, NULL);
5315 		if (status != DLADM_STATUS_OK) {
5316 			die_dlerr(status, "invalid vnic name '%s'",
5317 			    argv[optind]);
5318 		}
5319 		(void) strlcpy(state.vs_vnic, argv[optind], MAXLINKNAMELEN);
5320 	} else if (optind != argc) {
5321 		usage();
5322 	}
5323 
5324 	if (l_arg) {
5325 		status = dladm_name2info(handle, state.vs_link, &dev_linkid,
5326 		    NULL, NULL, NULL);
5327 		if (status != DLADM_STATUS_OK) {
5328 			die_dlerr(status, "invalid link name '%s'",
5329 			    state.vs_link);
5330 		}
5331 	}
5332 
5333 	state.vs_vnic_id = linkid;
5334 	state.vs_link_id = dev_linkid;
5335 	state.vs_etherstub = etherstub;
5336 	state.vs_found = B_FALSE;
5337 	state.vs_flags = flags;
5338 
5339 	if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0)) {
5340 		if (etherstub)
5341 			fields_str = all_e_fields;
5342 	}
5343 	pf = vnic_fields;
5344 
5345 	if (state.vs_parsable)
5346 		ofmtflags |= OFMT_PARSABLE;
5347 	oferr = ofmt_open(fields_str, pf, ofmtflags, 0, &ofmt);
5348 	ofmt_check(oferr, state.vs_parsable, ofmt, die, warn);
5349 	state.vs_ofmt = ofmt;
5350 
5351 	if (s_arg) {
5352 		/* Display vnic statistics */
5353 		vnic_stats(&state, interval);
5354 		ofmt_close(ofmt);
5355 		return;
5356 	}
5357 
5358 	/* Display vnic information */
5359 	state.vs_donefirst = B_FALSE;
5360 
5361 	if (linkid == DATALINK_ALL_LINKID) {
5362 		(void) dladm_walk_datalink_id(show_vnic, handle, &state,
5363 		    DATALINK_CLASS_VNIC | DATALINK_CLASS_ETHERSTUB,
5364 		    DATALINK_ANY_MEDIATYPE, flags);
5365 	} else {
5366 		(void) show_vnic(handle, linkid, &state);
5367 		if (state.vs_status != DLADM_STATUS_OK) {
5368 			ofmt_close(ofmt);
5369 			die_dlerr(state.vs_status, "failed to show vnic '%s'",
5370 			    state.vs_vnic);
5371 		}
5372 	}
5373 	ofmt_close(ofmt);
5374 }
5375 
5376 static void
5377 do_show_vnic(int argc, char *argv[], const char *use)
5378 {
5379 	do_show_vnic_common(argc, argv, use, B_FALSE);
5380 }
5381 
5382 static void
5383 do_create_etherstub(int argc, char *argv[], const char *use)
5384 {
5385 	uint32_t flags;
5386 	char *altroot = NULL;
5387 	int option;
5388 	dladm_status_t status;
5389 	char name[MAXLINKNAMELEN];
5390 	uchar_t mac_addr[ETHERADDRL];
5391 
5392 	name[0] = '\0';
5393 	bzero(mac_addr, sizeof (mac_addr));
5394 	flags = DLADM_OPT_ANCHOR | DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
5395 
5396 	opterr = 0;
5397 	while ((option = getopt_long(argc, argv, "tR:",
5398 	    etherstub_lopts, NULL)) != -1) {
5399 		switch (option) {
5400 		case 't':
5401 			flags &= ~DLADM_OPT_PERSIST;
5402 			break;
5403 		case 'R':
5404 			altroot = optarg;
5405 			break;
5406 		default:
5407 			die_opterr(optopt, option, use);
5408 		}
5409 	}
5410 
5411 	/* the etherstub id is the required operand */
5412 	if (optind != (argc - 1))
5413 		usage();
5414 
5415 	if (strlcpy(name, argv[optind], MAXLINKNAMELEN) >= MAXLINKNAMELEN)
5416 		die("link name too long '%s'", argv[optind]);
5417 
5418 	if (!dladm_valid_linkname(name))
5419 		die("invalid link name '%s'", argv[optind]);
5420 
5421 	if (altroot != NULL)
5422 		altroot_cmd(altroot, argc, argv);
5423 
5424 	status = dladm_vnic_create(handle, name, DATALINK_INVALID_LINKID,
5425 	    VNIC_MAC_ADDR_TYPE_AUTO, mac_addr, ETHERADDRL, NULL, 0, 0,
5426 	    VRRP_VRID_NONE, AF_UNSPEC, NULL, NULL, &errlist, flags);
5427 	if (status != DLADM_STATUS_OK)
5428 		die_dlerr(status, "etherstub creation failed");
5429 }
5430 
5431 static void
5432 do_delete_etherstub(int argc, char *argv[], const char *use)
5433 {
5434 	do_delete_vnic_common(argc, argv, use, B_TRUE);
5435 }
5436 
5437 /* ARGSUSED */
5438 static void
5439 do_show_etherstub(int argc, char *argv[], const char *use)
5440 {
5441 	do_show_vnic_common(argc, argv, use, B_TRUE);
5442 }
5443 
5444 /* ARGSUSED */
5445 static void
5446 do_up_simnet(int argc, char *argv[], const char *use)
5447 {
5448 	(void) dladm_simnet_up(handle, DATALINK_ALL_LINKID, 0);
5449 }
5450 
5451 static void
5452 do_create_simnet(int argc, char *argv[], const char *use)
5453 {
5454 	uint32_t flags;
5455 	char *altroot = NULL;
5456 	char *media = NULL;
5457 	uint32_t mtype = DL_ETHER;
5458 	int option;
5459 	dladm_status_t status;
5460 	char name[MAXLINKNAMELEN];
5461 
5462 	name[0] = '\0';
5463 	flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
5464 
5465 	opterr = 0;
5466 	while ((option = getopt_long(argc, argv, ":tR:m:",
5467 	    simnet_lopts, NULL)) != -1) {
5468 		switch (option) {
5469 		case 't':
5470 			flags &= ~DLADM_OPT_PERSIST;
5471 			break;
5472 		case 'R':
5473 			altroot = optarg;
5474 			break;
5475 		case 'm':
5476 			media = optarg;
5477 			break;
5478 		default:
5479 			die_opterr(optopt, option, use);
5480 		}
5481 	}
5482 
5483 	/* the simnet id is the required operand */
5484 	if (optind != (argc - 1))
5485 		usage();
5486 
5487 	if (strlcpy(name, argv[optind], MAXLINKNAMELEN) >= MAXLINKNAMELEN)
5488 		die("link name too long '%s'", argv[optind]);
5489 
5490 	if (!dladm_valid_linkname(name))
5491 		die("invalid link name '%s'", name);
5492 
5493 	if (media != NULL) {
5494 		mtype = dladm_str2media(media);
5495 		if (mtype != DL_ETHER && mtype != DL_WIFI)
5496 			die("media type '%s' is not supported", media);
5497 	}
5498 
5499 	if (altroot != NULL)
5500 		altroot_cmd(altroot, argc, argv);
5501 
5502 	status = dladm_simnet_create(handle, name, mtype, flags);
5503 	if (status != DLADM_STATUS_OK)
5504 		die_dlerr(status, "simnet creation failed");
5505 }
5506 
5507 static void
5508 do_delete_simnet(int argc, char *argv[], const char *use)
5509 {
5510 	int option;
5511 	uint32_t flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
5512 	datalink_id_t linkid;
5513 	char *altroot = NULL;
5514 	dladm_status_t status;
5515 	dladm_simnet_attr_t slinfo;
5516 
5517 	opterr = 0;
5518 	while ((option = getopt_long(argc, argv, ":tR:", simnet_lopts,
5519 	    NULL)) != -1) {
5520 		switch (option) {
5521 		case 't':
5522 			flags &= ~DLADM_OPT_PERSIST;
5523 			break;
5524 		case 'R':
5525 			altroot = optarg;
5526 			break;
5527 		default:
5528 			die_opterr(optopt, option, use);
5529 		}
5530 	}
5531 
5532 	/* get simnet name (required last argument) */
5533 	if (optind != (argc - 1))
5534 		usage();
5535 
5536 	if (!dladm_valid_linkname(argv[optind]))
5537 		die("invalid link name '%s'", argv[optind]);
5538 
5539 	if (altroot != NULL)
5540 		altroot_cmd(altroot, argc, argv);
5541 
5542 	status = dladm_name2info(handle, argv[optind], &linkid, NULL, NULL,
5543 	    NULL);
5544 	if (status != DLADM_STATUS_OK)
5545 		die("simnet '%s' not found", argv[optind]);
5546 
5547 	if ((status = dladm_simnet_info(handle, linkid, &slinfo,
5548 	    flags)) != DLADM_STATUS_OK)
5549 		die_dlerr(status, "failed to retrieve simnet information");
5550 
5551 	status = dladm_simnet_delete(handle, linkid, flags);
5552 	if (status != DLADM_STATUS_OK)
5553 		die_dlerr(status, "simnet deletion failed");
5554 }
5555 
5556 static void
5557 do_modify_simnet(int argc, char *argv[], const char *use)
5558 {
5559 	int option;
5560 	uint32_t flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
5561 	datalink_id_t linkid;
5562 	datalink_id_t peer_linkid;
5563 	char *altroot = NULL;
5564 	dladm_status_t status;
5565 	boolean_t p_arg = B_FALSE;
5566 
5567 	opterr = 0;
5568 	while ((option = getopt_long(argc, argv, ":tR:p:", simnet_lopts,
5569 	    NULL)) != -1) {
5570 		switch (option) {
5571 		case 't':
5572 			flags &= ~DLADM_OPT_PERSIST;
5573 			break;
5574 		case 'R':
5575 			altroot = optarg;
5576 			break;
5577 		case 'p':
5578 			if (p_arg)
5579 				die_optdup(option);
5580 			p_arg = B_TRUE;
5581 			if (strcasecmp(optarg, "none") == 0)
5582 				peer_linkid = DATALINK_INVALID_LINKID;
5583 			else if (dladm_name2info(handle, optarg, &peer_linkid,
5584 			    NULL, NULL, NULL) != DLADM_STATUS_OK)
5585 				die("invalid peer link name '%s'", optarg);
5586 			break;
5587 		default:
5588 			die_opterr(optopt, option, use);
5589 		}
5590 	}
5591 
5592 	/* get simnet name (required last argument) */
5593 	if (optind != (argc - 1))
5594 		usage();
5595 
5596 	/* Nothing to do if no peer link argument */
5597 	if (!p_arg)
5598 		return;
5599 
5600 	if (altroot != NULL)
5601 		altroot_cmd(altroot, argc, argv);
5602 
5603 	status = dladm_name2info(handle, argv[optind], &linkid, NULL, NULL,
5604 	    NULL);
5605 	if (status != DLADM_STATUS_OK)
5606 		die("invalid link name '%s'", argv[optind]);
5607 
5608 	status = dladm_simnet_modify(handle, linkid, peer_linkid, flags);
5609 	if (status != DLADM_STATUS_OK)
5610 		die_dlerr(status, "simnet modification failed");
5611 }
5612 
5613 static dladm_status_t
5614 print_simnet(show_state_t *state, datalink_id_t linkid)
5615 {
5616 	dladm_simnet_attr_t	slinfo;
5617 	uint32_t		flags;
5618 	dladm_status_t		status;
5619 	simnet_fields_buf_t	slbuf;
5620 	char			mstr[ETHERADDRL * 3];
5621 
5622 	bzero(&slbuf, sizeof (slbuf));
5623 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, NULL, NULL,
5624 	    slbuf.simnet_name, sizeof (slbuf.simnet_name)))
5625 	    != DLADM_STATUS_OK)
5626 		return (status);
5627 
5628 	if (!(state->ls_flags & flags))
5629 		return (DLADM_STATUS_NOTFOUND);
5630 
5631 	if ((status = dladm_simnet_info(handle, linkid, &slinfo,
5632 	    state->ls_flags)) != DLADM_STATUS_OK)
5633 		return (status);
5634 
5635 	if (slinfo.sna_peer_link_id != DATALINK_INVALID_LINKID &&
5636 	    (status = dladm_datalink_id2info(handle, slinfo.sna_peer_link_id,
5637 	    NULL, NULL, NULL, slbuf.simnet_otherlink,
5638 	    sizeof (slbuf.simnet_otherlink))) !=
5639 	    DLADM_STATUS_OK)
5640 		return (status);
5641 
5642 	if (slinfo.sna_mac_len > sizeof (slbuf.simnet_macaddr))
5643 		return (DLADM_STATUS_BADVAL);
5644 
5645 	(void) strlcpy(slbuf.simnet_macaddr,
5646 	    dladm_aggr_macaddr2str(slinfo.sna_mac_addr, mstr),
5647 	    sizeof (slbuf.simnet_macaddr));
5648 	(void) dladm_media2str(slinfo.sna_type, slbuf.simnet_media);
5649 
5650 	ofmt_print(state->ls_ofmt, &slbuf);
5651 	return (status);
5652 }
5653 
5654 /* ARGSUSED */
5655 static int
5656 show_simnet(dladm_handle_t dh, datalink_id_t linkid, void *arg)
5657 {
5658 	show_state_t		*state = arg;
5659 
5660 	state->ls_status = print_simnet(state, linkid);
5661 	return (DLADM_WALK_CONTINUE);
5662 }
5663 
5664 static void
5665 do_show_simnet(int argc, char *argv[], const char *use)
5666 {
5667 	int		option;
5668 	uint32_t	flags = DLADM_OPT_ACTIVE;
5669 	boolean_t	p_arg = B_FALSE;
5670 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
5671 	show_state_t	state;
5672 	dladm_status_t	status;
5673 	boolean_t	o_arg = B_FALSE;
5674 	ofmt_handle_t	ofmt;
5675 	ofmt_status_t	oferr;
5676 	char		*all_fields = "link,media,macaddress,otherlink";
5677 	char		*fields_str = all_fields;
5678 	uint_t		ofmtflags = 0;
5679 
5680 	bzero(&state, sizeof (state));
5681 
5682 	opterr = 0;
5683 	while ((option = getopt_long(argc, argv, ":pPo:",
5684 	    show_lopts, NULL)) != -1) {
5685 		switch (option) {
5686 		case 'p':
5687 			if (p_arg)
5688 				die_optdup(option);
5689 
5690 			p_arg = B_TRUE;
5691 			state.ls_parsable = p_arg;
5692 			break;
5693 		case 'P':
5694 			if (flags != DLADM_OPT_ACTIVE)
5695 				die_optdup(option);
5696 
5697 			flags = DLADM_OPT_PERSIST;
5698 			break;
5699 		case 'o':
5700 			o_arg = B_TRUE;
5701 			fields_str = optarg;
5702 			break;
5703 		default:
5704 			die_opterr(optopt, option, use);
5705 			break;
5706 		}
5707 	}
5708 
5709 	if (p_arg && !o_arg)
5710 		die("-p requires -o");
5711 
5712 	if (strcasecmp(fields_str, "all") == 0) {
5713 		if (p_arg)
5714 			die("\"-o all\" is invalid with -p");
5715 		fields_str = all_fields;
5716 	}
5717 
5718 	/* get link name (optional last argument) */
5719 	if (optind == (argc-1)) {
5720 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
5721 		    NULL, NULL, NULL)) != DLADM_STATUS_OK) {
5722 			die_dlerr(status, "link %s is not valid", argv[optind]);
5723 		}
5724 	} else if (optind != argc) {
5725 		usage();
5726 	}
5727 
5728 	state.ls_flags = flags;
5729 	state.ls_donefirst = B_FALSE;
5730 	if (state.ls_parsable)
5731 		ofmtflags |= OFMT_PARSABLE;
5732 	oferr = ofmt_open(fields_str, simnet_fields, ofmtflags, 0, &ofmt);
5733 	ofmt_check(oferr, state.ls_parsable, ofmt, die, warn);
5734 	state.ls_ofmt = ofmt;
5735 
5736 	if (linkid == DATALINK_ALL_LINKID) {
5737 		(void) dladm_walk_datalink_id(show_simnet, handle, &state,
5738 		    DATALINK_CLASS_SIMNET, DATALINK_ANY_MEDIATYPE, flags);
5739 	} else {
5740 		(void) show_simnet(handle, linkid, &state);
5741 		if (state.ls_status != DLADM_STATUS_OK) {
5742 			ofmt_close(ofmt);
5743 			die_dlerr(state.ls_status, "failed to show simnet %s",
5744 			    argv[optind]);
5745 		}
5746 	}
5747 	ofmt_close(ofmt);
5748 }
5749 
5750 static void
5751 link_stats(datalink_id_t linkid, uint_t interval, char *fields_str,
5752     show_state_t *state)
5753 {
5754 	ofmt_handle_t	ofmt;
5755 	ofmt_status_t	oferr;
5756 	uint_t		ofmtflags = 0;
5757 
5758 	if (state->ls_parsable)
5759 		ofmtflags |= OFMT_PARSABLE;
5760 	oferr = ofmt_open(fields_str, link_s_fields, ofmtflags, 0, &ofmt);
5761 	ofmt_check(oferr, state->ls_parsable, ofmt, die, warn);
5762 	state->ls_ofmt = ofmt;
5763 
5764 	/*
5765 	 * If an interval is specified, continuously show the stats
5766 	 * only for the first MAC port.
5767 	 */
5768 	state->ls_firstonly = (interval != 0);
5769 
5770 	for (;;) {
5771 		state->ls_donefirst = B_FALSE;
5772 		if (linkid == DATALINK_ALL_LINKID) {
5773 			(void) dladm_walk_datalink_id(show_link_stats, handle,
5774 			    state, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE,
5775 			    DLADM_OPT_ACTIVE);
5776 		} else {
5777 			(void) show_link_stats(handle, linkid, state);
5778 		}
5779 
5780 		if (interval == 0)
5781 			break;
5782 
5783 		(void) fflush(stdout);
5784 		(void) sleep(interval);
5785 	}
5786 	ofmt_close(ofmt);
5787 }
5788 
5789 static void
5790 aggr_stats(datalink_id_t linkid, show_grp_state_t *state, uint_t interval)
5791 {
5792 	/*
5793 	 * If an interval is specified, continuously show the stats
5794 	 * only for the first group.
5795 	 */
5796 	state->gs_firstonly = (interval != 0);
5797 
5798 	for (;;) {
5799 		state->gs_donefirst = B_FALSE;
5800 		if (linkid == DATALINK_ALL_LINKID)
5801 			(void) dladm_walk_datalink_id(show_aggr, handle, state,
5802 			    DATALINK_CLASS_AGGR, DATALINK_ANY_MEDIATYPE,
5803 			    DLADM_OPT_ACTIVE);
5804 		else
5805 			(void) show_aggr(handle, linkid, state);
5806 
5807 		if (interval == 0)
5808 			break;
5809 
5810 		(void) fflush(stdout);
5811 		(void) sleep(interval);
5812 	}
5813 }
5814 
5815 /* ARGSUSED */
5816 static void
5817 vnic_stats(show_vnic_state_t *sp, uint32_t interval)
5818 {
5819 	show_vnic_state_t	state;
5820 	boolean_t		specific_link, specific_dev;
5821 
5822 	/* Display vnic statistics */
5823 	dump_vnics_head(sp->vs_link);
5824 
5825 	bzero(&state, sizeof (state));
5826 	state.vs_stats = B_TRUE;
5827 	state.vs_vnic_id = sp->vs_vnic_id;
5828 	state.vs_link_id = sp->vs_link_id;
5829 
5830 	/*
5831 	 * If an interval is specified, and a vnic ID is not specified,
5832 	 * continuously show the stats only for the first vnic.
5833 	 */
5834 	specific_link = (sp->vs_vnic_id != DATALINK_ALL_LINKID);
5835 	specific_dev = (sp->vs_link_id != DATALINK_ALL_LINKID);
5836 
5837 	for (;;) {
5838 		/* Get stats for each vnic */
5839 		state.vs_found = B_FALSE;
5840 		state.vs_donefirst = B_FALSE;
5841 		state.vs_printstats = B_FALSE;
5842 		state.vs_flags = DLADM_OPT_ACTIVE;
5843 
5844 		if (!specific_link) {
5845 			(void) dladm_walk_datalink_id(show_vnic, handle, &state,
5846 			    DATALINK_CLASS_VNIC, DATALINK_ANY_MEDIATYPE,
5847 			    DLADM_OPT_ACTIVE);
5848 		} else {
5849 			(void) show_vnic(handle, sp->vs_vnic_id, &state);
5850 			if (state.vs_status != DLADM_STATUS_OK) {
5851 				die_dlerr(state.vs_status,
5852 				    "failed to show vnic '%s'", sp->vs_vnic);
5853 			}
5854 		}
5855 
5856 		if (specific_link && !state.vs_found)
5857 			die("non-existent vnic '%s'", sp->vs_vnic);
5858 		if (specific_dev && !state.vs_found)
5859 			die("device %s has no vnics", sp->vs_link);
5860 
5861 		/* Show totals */
5862 		if ((specific_link | specific_dev) && !interval) {
5863 			(void) printf("Total");
5864 			(void) printf("\t%-10llu",
5865 			    state.vs_totalstats.ipackets);
5866 			(void) printf("%-12llu",
5867 			    state.vs_totalstats.rbytes);
5868 			(void) printf("%-10llu",
5869 			    state.vs_totalstats.opackets);
5870 			(void) printf("%-12llu\n",
5871 			    state.vs_totalstats.obytes);
5872 		}
5873 
5874 		/* Show stats for each vnic */
5875 		state.vs_donefirst = B_FALSE;
5876 		state.vs_printstats = B_TRUE;
5877 
5878 		if (!specific_link) {
5879 			(void) dladm_walk_datalink_id(show_vnic, handle, &state,
5880 			    DATALINK_CLASS_VNIC, DATALINK_ANY_MEDIATYPE,
5881 			    DLADM_OPT_ACTIVE);
5882 		} else {
5883 			(void) show_vnic(handle, sp->vs_vnic_id, &state);
5884 			if (state.vs_status != DLADM_STATUS_OK) {
5885 				die_dlerr(state.vs_status,
5886 				    "failed to show vnic '%s'", sp->vs_vnic);
5887 			}
5888 		}
5889 
5890 		if (interval == 0)
5891 			break;
5892 
5893 		(void) fflush(stdout);
5894 		(void) sleep(interval);
5895 	}
5896 }
5897 
5898 static void
5899 get_mac_stats(const char *dev, pktsum_t *stats)
5900 {
5901 	kstat_ctl_t	*kcp;
5902 	kstat_t		*ksp;
5903 	char module[DLPI_LINKNAME_MAX];
5904 	uint_t instance;
5905 
5906 
5907 	bzero(stats, sizeof (*stats));
5908 
5909 	if (dlpi_parselink(dev, module, &instance) != DLPI_SUCCESS)
5910 		return;
5911 
5912 	if ((kcp = kstat_open()) == NULL) {
5913 		warn("kstat open operation failed");
5914 		return;
5915 	}
5916 
5917 	ksp = dladm_kstat_lookup(kcp, module, instance, "mac", NULL);
5918 	if (ksp != NULL)
5919 		dladm_get_stats(kcp, ksp, stats);
5920 
5921 	(void) kstat_close(kcp);
5922 
5923 }
5924 
5925 static void
5926 get_link_stats(const char *link, pktsum_t *stats)
5927 {
5928 	kstat_ctl_t	*kcp;
5929 	kstat_t		*ksp;
5930 
5931 	bzero(stats, sizeof (*stats));
5932 
5933 	if ((kcp = kstat_open()) == NULL) {
5934 		warn("kstat_open operation failed");
5935 		return;
5936 	}
5937 
5938 	ksp = dladm_kstat_lookup(kcp, "link", 0, link, NULL);
5939 
5940 	if (ksp != NULL)
5941 		dladm_get_stats(kcp, ksp, stats);
5942 
5943 	(void) kstat_close(kcp);
5944 }
5945 
5946 static int
5947 query_kstat(char *module, int instance, const char *name, const char *stat,
5948     uint8_t type, void *val)
5949 {
5950 	kstat_ctl_t	*kcp;
5951 	kstat_t		*ksp;
5952 
5953 	if ((kcp = kstat_open()) == NULL) {
5954 		warn("kstat open operation failed");
5955 		return (-1);
5956 	}
5957 
5958 	if ((ksp = kstat_lookup(kcp, module, instance, (char *)name)) == NULL) {
5959 		/*
5960 		 * The kstat query could fail if the underlying MAC
5961 		 * driver was already detached.
5962 		 */
5963 		goto bail;
5964 	}
5965 
5966 	if (kstat_read(kcp, ksp, NULL) == -1) {
5967 		warn("kstat read failed");
5968 		goto bail;
5969 	}
5970 
5971 	if (dladm_kstat_value(ksp, stat, type, val) < 0)
5972 		goto bail;
5973 
5974 	(void) kstat_close(kcp);
5975 	return (0);
5976 
5977 bail:
5978 	(void) kstat_close(kcp);
5979 	return (-1);
5980 }
5981 
5982 static int
5983 get_one_kstat(const char *name, const char *stat, uint8_t type,
5984     void *val, boolean_t islink)
5985 {
5986 	char		module[DLPI_LINKNAME_MAX];
5987 	uint_t		instance;
5988 
5989 	if (islink) {
5990 		return (query_kstat("link", 0, name, stat, type, val));
5991 	} else {
5992 		if (dlpi_parselink(name, module, &instance) != DLPI_SUCCESS)
5993 			return (-1);
5994 
5995 		return (query_kstat(module, instance, "mac", stat, type, val));
5996 	}
5997 }
5998 
5999 static uint64_t
6000 get_ifspeed(const char *name, boolean_t islink)
6001 {
6002 	uint64_t ifspeed = 0;
6003 
6004 	(void) get_one_kstat(name, "ifspeed", KSTAT_DATA_UINT64,
6005 	    &ifspeed, islink);
6006 
6007 	return (ifspeed);
6008 }
6009 
6010 static const char *
6011 get_linkstate(const char *name, boolean_t islink, char *buf)
6012 {
6013 	link_state_t	linkstate;
6014 
6015 	if (get_one_kstat(name, "link_state", KSTAT_DATA_UINT32,
6016 	    &linkstate, islink) != 0) {
6017 		(void) strlcpy(buf, "?", DLADM_STRSIZE);
6018 		return (buf);
6019 	}
6020 	return (dladm_linkstate2str(linkstate, buf));
6021 }
6022 
6023 static const char *
6024 get_linkduplex(const char *name, boolean_t islink, char *buf)
6025 {
6026 	link_duplex_t	linkduplex;
6027 
6028 	if (get_one_kstat(name, "link_duplex", KSTAT_DATA_UINT32,
6029 	    &linkduplex, islink) != 0) {
6030 		(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
6031 		return (buf);
6032 	}
6033 
6034 	return (dladm_linkduplex2str(linkduplex, buf));
6035 }
6036 
6037 static int
6038 parse_wifi_fields(char *str, ofmt_handle_t *ofmt, uint_t cmdtype,
6039     boolean_t parsable)
6040 {
6041 	ofmt_field_t	*template, *of;
6042 	ofmt_cb_t	*fn;
6043 	ofmt_status_t	oferr;
6044 
6045 	if (cmdtype == WIFI_CMD_SCAN) {
6046 		template = wifi_common_fields;
6047 		if (str == NULL)
6048 			str = def_scan_wifi_fields;
6049 		if (strcasecmp(str, "all") == 0)
6050 			str = all_scan_wifi_fields;
6051 		fn = print_wlan_attr_cb;
6052 	} else if (cmdtype == WIFI_CMD_SHOW) {
6053 		bcopy(wifi_common_fields, &wifi_show_fields[2],
6054 		    sizeof (wifi_common_fields));
6055 		template = wifi_show_fields;
6056 		if (str == NULL)
6057 			str = def_show_wifi_fields;
6058 		if (strcasecmp(str, "all") == 0)
6059 			str = all_show_wifi_fields;
6060 		fn = print_link_attr_cb;
6061 	} else {
6062 		return (-1);
6063 	}
6064 
6065 	for (of = template; of->of_name != NULL; of++) {
6066 		if (of->of_cb == NULL)
6067 			of->of_cb = fn;
6068 	}
6069 
6070 	oferr = ofmt_open(str, template, (parsable ? OFMT_PARSABLE : 0),
6071 	    0, ofmt);
6072 	ofmt_check(oferr, parsable, *ofmt, die, warn);
6073 	return (0);
6074 }
6075 
6076 typedef struct print_wifi_state {
6077 	char		*ws_link;
6078 	boolean_t	ws_parsable;
6079 	boolean_t	ws_header;
6080 	ofmt_handle_t	ws_ofmt;
6081 } print_wifi_state_t;
6082 
6083 typedef struct  wlan_scan_args_s {
6084 	print_wifi_state_t	*ws_state;
6085 	void			*ws_attr;
6086 } wlan_scan_args_t;
6087 
6088 static boolean_t
6089 print_wlan_attr_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
6090 {
6091 	wlan_scan_args_t	*w = ofarg->ofmt_cbarg;
6092 	print_wifi_state_t	*statep = w->ws_state;
6093 	dladm_wlan_attr_t	*attrp = w->ws_attr;
6094 	char			tmpbuf[DLADM_STRSIZE];
6095 
6096 	if (ofarg->ofmt_id == 0) {
6097 		(void) strlcpy(buf, (char *)statep->ws_link, bufsize);
6098 		return (B_TRUE);
6099 	}
6100 
6101 	if ((ofarg->ofmt_id & attrp->wa_valid) == 0)
6102 		return (B_TRUE);
6103 
6104 	switch (ofarg->ofmt_id) {
6105 	case DLADM_WLAN_ATTR_ESSID:
6106 		(void) dladm_wlan_essid2str(&attrp->wa_essid, tmpbuf);
6107 		break;
6108 	case DLADM_WLAN_ATTR_BSSID:
6109 		(void) dladm_wlan_bssid2str(&attrp->wa_bssid, tmpbuf);
6110 		break;
6111 	case DLADM_WLAN_ATTR_SECMODE:
6112 		(void) dladm_wlan_secmode2str(&attrp->wa_secmode, tmpbuf);
6113 		break;
6114 	case DLADM_WLAN_ATTR_STRENGTH:
6115 		(void) dladm_wlan_strength2str(&attrp->wa_strength, tmpbuf);
6116 		break;
6117 	case DLADM_WLAN_ATTR_MODE:
6118 		(void) dladm_wlan_mode2str(&attrp->wa_mode, tmpbuf);
6119 		break;
6120 	case DLADM_WLAN_ATTR_SPEED:
6121 		(void) dladm_wlan_speed2str(&attrp->wa_speed, tmpbuf);
6122 		(void) strlcat(tmpbuf, "Mb", sizeof (tmpbuf));
6123 		break;
6124 	case DLADM_WLAN_ATTR_AUTH:
6125 		(void) dladm_wlan_auth2str(&attrp->wa_auth, tmpbuf);
6126 		break;
6127 	case DLADM_WLAN_ATTR_BSSTYPE:
6128 		(void) dladm_wlan_bsstype2str(&attrp->wa_bsstype, tmpbuf);
6129 		break;
6130 	}
6131 	(void) strlcpy(buf, tmpbuf, bufsize);
6132 
6133 	return (B_TRUE);
6134 }
6135 
6136 static boolean_t
6137 print_scan_results(void *arg, dladm_wlan_attr_t *attrp)
6138 {
6139 	print_wifi_state_t	*statep = arg;
6140 	wlan_scan_args_t	warg;
6141 
6142 	bzero(&warg, sizeof (warg));
6143 	warg.ws_state = statep;
6144 	warg.ws_attr = attrp;
6145 	ofmt_print(statep->ws_ofmt, &warg);
6146 	return (B_TRUE);
6147 }
6148 
6149 static int
6150 scan_wifi(dladm_handle_t dh, datalink_id_t linkid, void *arg)
6151 {
6152 	print_wifi_state_t	*statep = arg;
6153 	dladm_status_t		status;
6154 	char			link[MAXLINKNAMELEN];
6155 
6156 	if ((status = dladm_datalink_id2info(dh, linkid, NULL, NULL, NULL, link,
6157 	    sizeof (link))) != DLADM_STATUS_OK) {
6158 		return (DLADM_WALK_CONTINUE);
6159 	}
6160 
6161 	statep->ws_link = link;
6162 	status = dladm_wlan_scan(dh, linkid, statep, print_scan_results);
6163 	if (status != DLADM_STATUS_OK)
6164 		die_dlerr(status, "cannot scan link '%s'", statep->ws_link);
6165 
6166 	return (DLADM_WALK_CONTINUE);
6167 }
6168 
6169 static boolean_t
6170 print_wifi_status_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
6171 {
6172 	static char		tmpbuf[DLADM_STRSIZE];
6173 	wlan_scan_args_t	*w = ofarg->ofmt_cbarg;
6174 	dladm_wlan_linkattr_t	*attrp = w->ws_attr;
6175 
6176 	if ((ofarg->ofmt_id & attrp->la_valid) != 0) {
6177 		(void) dladm_wlan_linkstatus2str(&attrp->la_status, tmpbuf);
6178 		(void) strlcpy(buf, tmpbuf, bufsize);
6179 	}
6180 	return (B_TRUE);
6181 }
6182 
6183 static boolean_t
6184 print_link_attr_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
6185 {
6186 	wlan_scan_args_t	*w = ofarg->ofmt_cbarg, w1;
6187 	print_wifi_state_t	*statep = w->ws_state;
6188 	dladm_wlan_linkattr_t	*attrp = w->ws_attr;
6189 
6190 	bzero(&w1, sizeof (w1));
6191 	w1.ws_state = statep;
6192 	w1.ws_attr = &attrp->la_wlan_attr;
6193 	ofarg->ofmt_cbarg = &w1;
6194 	return (print_wlan_attr_cb(ofarg, buf, bufsize));
6195 }
6196 
6197 static int
6198 show_wifi(dladm_handle_t dh, datalink_id_t linkid, void *arg)
6199 {
6200 	print_wifi_state_t	*statep = arg;
6201 	dladm_wlan_linkattr_t	attr;
6202 	dladm_status_t		status;
6203 	char			link[MAXLINKNAMELEN];
6204 	wlan_scan_args_t	warg;
6205 
6206 	if ((status = dladm_datalink_id2info(dh, linkid, NULL, NULL, NULL, link,
6207 	    sizeof (link))) != DLADM_STATUS_OK) {
6208 		return (DLADM_WALK_CONTINUE);
6209 	}
6210 
6211 	/* dladm_wlan_get_linkattr() memsets attr with 0 */
6212 	status = dladm_wlan_get_linkattr(dh, linkid, &attr);
6213 	if (status != DLADM_STATUS_OK)
6214 		die_dlerr(status, "cannot get link attributes for %s", link);
6215 
6216 	statep->ws_link = link;
6217 
6218 	bzero(&warg, sizeof (warg));
6219 	warg.ws_state = statep;
6220 	warg.ws_attr = &attr;
6221 	ofmt_print(statep->ws_ofmt, &warg);
6222 	return (DLADM_WALK_CONTINUE);
6223 }
6224 
6225 static void
6226 do_display_wifi(int argc, char **argv, int cmd, const char *use)
6227 {
6228 	int			option;
6229 	char			*fields_str = NULL;
6230 	int		(*callback)(dladm_handle_t, datalink_id_t, void *);
6231 	print_wifi_state_t	state;
6232 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
6233 	dladm_status_t		status;
6234 
6235 	if (cmd == WIFI_CMD_SCAN)
6236 		callback = scan_wifi;
6237 	else if (cmd == WIFI_CMD_SHOW)
6238 		callback = show_wifi;
6239 	else
6240 		return;
6241 
6242 	state.ws_parsable = B_FALSE;
6243 	state.ws_header = B_TRUE;
6244 	opterr = 0;
6245 	while ((option = getopt_long(argc, argv, ":o:p",
6246 	    wifi_longopts, NULL)) != -1) {
6247 		switch (option) {
6248 		case 'o':
6249 			fields_str = optarg;
6250 			break;
6251 		case 'p':
6252 			state.ws_parsable = B_TRUE;
6253 			break;
6254 		default:
6255 			die_opterr(optopt, option, use);
6256 		}
6257 	}
6258 
6259 	if (state.ws_parsable && fields_str == NULL)
6260 		die("-p requires -o");
6261 
6262 	if (state.ws_parsable && strcasecmp(fields_str, "all") == 0)
6263 		die("\"-o all\" is invalid with -p");
6264 
6265 	if (optind == (argc - 1)) {
6266 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
6267 		    NULL, NULL, NULL)) != DLADM_STATUS_OK) {
6268 			die_dlerr(status, "link %s is not valid", argv[optind]);
6269 		}
6270 	} else if (optind != argc) {
6271 		usage();
6272 	}
6273 
6274 	if (parse_wifi_fields(fields_str, &state.ws_ofmt, cmd,
6275 	    state.ws_parsable) < 0)
6276 		die("invalid field(s) specified");
6277 
6278 	if (linkid == DATALINK_ALL_LINKID) {
6279 		(void) dladm_walk_datalink_id(callback, handle, &state,
6280 		    DATALINK_CLASS_PHYS | DATALINK_CLASS_SIMNET,
6281 		    DL_WIFI, DLADM_OPT_ACTIVE);
6282 	} else {
6283 		(void) (*callback)(handle, linkid, &state);
6284 	}
6285 	ofmt_close(state.ws_ofmt);
6286 }
6287 
6288 static void
6289 do_scan_wifi(int argc, char **argv, const char *use)
6290 {
6291 	do_display_wifi(argc, argv, WIFI_CMD_SCAN, use);
6292 }
6293 
6294 static void
6295 do_show_wifi(int argc, char **argv, const char *use)
6296 {
6297 	do_display_wifi(argc, argv, WIFI_CMD_SHOW, use);
6298 }
6299 
6300 typedef struct wlan_count_attr {
6301 	uint_t		wc_count;
6302 	datalink_id_t	wc_linkid;
6303 } wlan_count_attr_t;
6304 
6305 /* ARGSUSED */
6306 static int
6307 do_count_wlan(dladm_handle_t dh, datalink_id_t linkid, void *arg)
6308 {
6309 	wlan_count_attr_t *cp = arg;
6310 
6311 	if (cp->wc_count == 0)
6312 		cp->wc_linkid = linkid;
6313 	cp->wc_count++;
6314 	return (DLADM_WALK_CONTINUE);
6315 }
6316 
6317 static int
6318 parse_wlan_keys(char *str, dladm_wlan_key_t **keys, uint_t *key_countp)
6319 {
6320 	uint_t			i;
6321 	dladm_wlan_key_t	*wk;
6322 	int			nfields = 1;
6323 	char			*field, *token, *lasts = NULL, c;
6324 
6325 	token = str;
6326 	while ((c = *token++) != '\0') {
6327 		if (c == ',')
6328 			nfields++;
6329 	}
6330 	token = strdup(str);
6331 	if (token == NULL)
6332 		return (-1);
6333 
6334 	wk = malloc(nfields * sizeof (dladm_wlan_key_t));
6335 	if (wk == NULL)
6336 		goto fail;
6337 
6338 	token = str;
6339 	for (i = 0; i < nfields; i++) {
6340 		char			*s;
6341 		dladm_secobj_class_t	class;
6342 		dladm_status_t		status;
6343 
6344 		field = strtok_r(token, ",", &lasts);
6345 		token = NULL;
6346 
6347 		(void) strlcpy(wk[i].wk_name, field,
6348 		    DLADM_WLAN_MAX_KEYNAME_LEN);
6349 
6350 		wk[i].wk_idx = 1;
6351 		if ((s = strrchr(wk[i].wk_name, ':')) != NULL) {
6352 			if (s[1] == '\0' || s[2] != '\0' || !isdigit(s[1]))
6353 				goto fail;
6354 
6355 			wk[i].wk_idx = (uint_t)(s[1] - '0');
6356 			*s = '\0';
6357 		}
6358 		wk[i].wk_len = DLADM_WLAN_MAX_KEY_LEN;
6359 
6360 		status = dladm_get_secobj(handle, wk[i].wk_name, &class,
6361 		    wk[i].wk_val, &wk[i].wk_len, 0);
6362 		if (status != DLADM_STATUS_OK) {
6363 			if (status == DLADM_STATUS_NOTFOUND) {
6364 				status = dladm_get_secobj(handle, wk[i].wk_name,
6365 				    &class, wk[i].wk_val, &wk[i].wk_len,
6366 				    DLADM_OPT_PERSIST);
6367 			}
6368 			if (status != DLADM_STATUS_OK)
6369 				goto fail;
6370 		}
6371 		wk[i].wk_class = class;
6372 	}
6373 	*keys = wk;
6374 	*key_countp = i;
6375 	free(token);
6376 	return (0);
6377 fail:
6378 	free(wk);
6379 	free(token);
6380 	return (-1);
6381 }
6382 
6383 static void
6384 do_connect_wifi(int argc, char **argv, const char *use)
6385 {
6386 	int			option;
6387 	dladm_wlan_attr_t	attr, *attrp;
6388 	dladm_status_t		status = DLADM_STATUS_OK;
6389 	int			timeout = DLADM_WLAN_CONNECT_TIMEOUT_DEFAULT;
6390 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
6391 	dladm_wlan_key_t	*keys = NULL;
6392 	uint_t			key_count = 0;
6393 	uint_t			flags = 0;
6394 	dladm_wlan_secmode_t	keysecmode = DLADM_WLAN_SECMODE_NONE;
6395 	char			buf[DLADM_STRSIZE];
6396 
6397 	opterr = 0;
6398 	(void) memset(&attr, 0, sizeof (attr));
6399 	while ((option = getopt_long(argc, argv, ":e:i:a:m:b:s:k:T:c",
6400 	    wifi_longopts, NULL)) != -1) {
6401 		switch (option) {
6402 		case 'e':
6403 			status = dladm_wlan_str2essid(optarg, &attr.wa_essid);
6404 			if (status != DLADM_STATUS_OK)
6405 				die("invalid ESSID '%s'", optarg);
6406 
6407 			attr.wa_valid |= DLADM_WLAN_ATTR_ESSID;
6408 			/*
6409 			 * Try to connect without doing a scan.
6410 			 */
6411 			flags |= DLADM_WLAN_CONNECT_NOSCAN;
6412 			break;
6413 		case 'i':
6414 			status = dladm_wlan_str2bssid(optarg, &attr.wa_bssid);
6415 			if (status != DLADM_STATUS_OK)
6416 				die("invalid BSSID %s", optarg);
6417 
6418 			attr.wa_valid |= DLADM_WLAN_ATTR_BSSID;
6419 			break;
6420 		case 'a':
6421 			status = dladm_wlan_str2auth(optarg, &attr.wa_auth);
6422 			if (status != DLADM_STATUS_OK)
6423 				die("invalid authentication mode '%s'", optarg);
6424 
6425 			attr.wa_valid |= DLADM_WLAN_ATTR_AUTH;
6426 			break;
6427 		case 'm':
6428 			status = dladm_wlan_str2mode(optarg, &attr.wa_mode);
6429 			if (status != DLADM_STATUS_OK)
6430 				die("invalid mode '%s'", optarg);
6431 
6432 			attr.wa_valid |= DLADM_WLAN_ATTR_MODE;
6433 			break;
6434 		case 'b':
6435 			if ((status = dladm_wlan_str2bsstype(optarg,
6436 			    &attr.wa_bsstype)) != DLADM_STATUS_OK) {
6437 				die("invalid bsstype '%s'", optarg);
6438 			}
6439 
6440 			attr.wa_valid |= DLADM_WLAN_ATTR_BSSTYPE;
6441 			break;
6442 		case 's':
6443 			if ((status = dladm_wlan_str2secmode(optarg,
6444 			    &attr.wa_secmode)) != DLADM_STATUS_OK) {
6445 				die("invalid security mode '%s'", optarg);
6446 			}
6447 
6448 			attr.wa_valid |= DLADM_WLAN_ATTR_SECMODE;
6449 			break;
6450 		case 'k':
6451 			if (parse_wlan_keys(optarg, &keys, &key_count) < 0)
6452 				die("invalid key(s) '%s'", optarg);
6453 
6454 			if (keys[0].wk_class == DLADM_SECOBJ_CLASS_WEP)
6455 				keysecmode = DLADM_WLAN_SECMODE_WEP;
6456 			else
6457 				keysecmode = DLADM_WLAN_SECMODE_WPA;
6458 			break;
6459 		case 'T':
6460 			if (strcasecmp(optarg, "forever") == 0) {
6461 				timeout = -1;
6462 				break;
6463 			}
6464 			if (!str2int(optarg, &timeout) || timeout < 0)
6465 				die("invalid timeout value '%s'", optarg);
6466 			break;
6467 		case 'c':
6468 			flags |= DLADM_WLAN_CONNECT_CREATEIBSS;
6469 			flags |= DLADM_WLAN_CONNECT_CREATEIBSS;
6470 			break;
6471 		default:
6472 			die_opterr(optopt, option, use);
6473 			break;
6474 		}
6475 	}
6476 
6477 	if (keysecmode == DLADM_WLAN_SECMODE_NONE) {
6478 		if ((attr.wa_valid & DLADM_WLAN_ATTR_SECMODE) != 0) {
6479 			die("key required for security mode '%s'",
6480 			    dladm_wlan_secmode2str(&attr.wa_secmode, buf));
6481 		}
6482 	} else {
6483 		if ((attr.wa_valid & DLADM_WLAN_ATTR_SECMODE) != 0 &&
6484 		    attr.wa_secmode != keysecmode)
6485 			die("incompatible -s and -k options");
6486 		attr.wa_valid |= DLADM_WLAN_ATTR_SECMODE;
6487 		attr.wa_secmode = keysecmode;
6488 	}
6489 
6490 	if (optind == (argc - 1)) {
6491 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
6492 		    NULL, NULL, NULL)) != DLADM_STATUS_OK) {
6493 			die_dlerr(status, "link %s is not valid", argv[optind]);
6494 		}
6495 	} else if (optind != argc) {
6496 		usage();
6497 	}
6498 
6499 	if (linkid == DATALINK_ALL_LINKID) {
6500 		wlan_count_attr_t wcattr;
6501 
6502 		wcattr.wc_linkid = DATALINK_INVALID_LINKID;
6503 		wcattr.wc_count = 0;
6504 		(void) dladm_walk_datalink_id(do_count_wlan, handle, &wcattr,
6505 		    DATALINK_CLASS_PHYS | DATALINK_CLASS_SIMNET,
6506 		    DL_WIFI, DLADM_OPT_ACTIVE);
6507 		if (wcattr.wc_count == 0) {
6508 			die("no wifi links are available");
6509 		} else if (wcattr.wc_count > 1) {
6510 			die("link name is required when more than one wifi "
6511 			    "link is available");
6512 		}
6513 		linkid = wcattr.wc_linkid;
6514 	}
6515 	attrp = (attr.wa_valid == 0) ? NULL : &attr;
6516 again:
6517 	if ((status = dladm_wlan_connect(handle, linkid, attrp, timeout, keys,
6518 	    key_count, flags)) != DLADM_STATUS_OK) {
6519 		if ((flags & DLADM_WLAN_CONNECT_NOSCAN) != 0) {
6520 			/*
6521 			 * Try again with scanning and filtering.
6522 			 */
6523 			flags &= ~DLADM_WLAN_CONNECT_NOSCAN;
6524 			goto again;
6525 		}
6526 
6527 		if (status == DLADM_STATUS_NOTFOUND) {
6528 			if (attr.wa_valid == 0) {
6529 				die("no wifi networks are available");
6530 			} else {
6531 				die("no wifi networks with the specified "
6532 				    "criteria are available");
6533 			}
6534 		}
6535 		die_dlerr(status, "cannot connect");
6536 	}
6537 	free(keys);
6538 }
6539 
6540 /* ARGSUSED */
6541 static int
6542 do_all_disconnect_wifi(dladm_handle_t dh, datalink_id_t linkid, void *arg)
6543 {
6544 	dladm_status_t	status;
6545 
6546 	status = dladm_wlan_disconnect(dh, linkid);
6547 	if (status != DLADM_STATUS_OK)
6548 		warn_dlerr(status, "cannot disconnect link");
6549 
6550 	return (DLADM_WALK_CONTINUE);
6551 }
6552 
6553 static void
6554 do_disconnect_wifi(int argc, char **argv, const char *use)
6555 {
6556 	int			option;
6557 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
6558 	boolean_t		all_links = B_FALSE;
6559 	dladm_status_t		status;
6560 	wlan_count_attr_t	wcattr;
6561 
6562 	opterr = 0;
6563 	while ((option = getopt_long(argc, argv, ":a",
6564 	    wifi_longopts, NULL)) != -1) {
6565 		switch (option) {
6566 		case 'a':
6567 			all_links = B_TRUE;
6568 			break;
6569 		default:
6570 			die_opterr(optopt, option, use);
6571 			break;
6572 		}
6573 	}
6574 
6575 	if (optind == (argc - 1)) {
6576 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
6577 		    NULL, NULL, NULL)) != DLADM_STATUS_OK) {
6578 			die_dlerr(status, "link %s is not valid", argv[optind]);
6579 		}
6580 	} else if (optind != argc) {
6581 		usage();
6582 	}
6583 
6584 	if (linkid == DATALINK_ALL_LINKID) {
6585 		if (!all_links) {
6586 			wcattr.wc_linkid = linkid;
6587 			wcattr.wc_count = 0;
6588 			(void) dladm_walk_datalink_id(do_count_wlan, handle,
6589 			    &wcattr,
6590 			    DATALINK_CLASS_PHYS | DATALINK_CLASS_SIMNET,
6591 			    DL_WIFI, DLADM_OPT_ACTIVE);
6592 			if (wcattr.wc_count == 0) {
6593 				die("no wifi links are available");
6594 			} else if (wcattr.wc_count > 1) {
6595 				die("link name is required when more than "
6596 				    "one wifi link is available");
6597 			}
6598 			linkid = wcattr.wc_linkid;
6599 		} else {
6600 			(void) dladm_walk_datalink_id(do_all_disconnect_wifi,
6601 			    handle, NULL,
6602 			    DATALINK_CLASS_PHYS | DATALINK_CLASS_SIMNET,
6603 			    DL_WIFI, DLADM_OPT_ACTIVE);
6604 			return;
6605 		}
6606 	}
6607 	status = dladm_wlan_disconnect(handle, linkid);
6608 	if (status != DLADM_STATUS_OK)
6609 		die_dlerr(status, "cannot disconnect");
6610 }
6611 
6612 static void
6613 print_linkprop(datalink_id_t linkid, show_linkprop_state_t *statep,
6614     const char *propname, dladm_prop_type_t type, const char *format,
6615     char **pptr)
6616 {
6617 	int		i;
6618 	char		*ptr, *lim;
6619 	char		buf[DLADM_STRSIZE];
6620 	char		*unknown = "--", *notsup = "";
6621 	char		**propvals = statep->ls_propvals;
6622 	uint_t		valcnt = DLADM_MAX_PROP_VALCNT;
6623 	dladm_status_t	status;
6624 
6625 	status = dladm_get_linkprop(handle, linkid, type, propname, propvals,
6626 	    &valcnt);
6627 	if (status != DLADM_STATUS_OK) {
6628 		if (status == DLADM_STATUS_TEMPONLY) {
6629 			if (type == DLADM_PROP_VAL_MODIFIABLE &&
6630 			    statep->ls_persist) {
6631 				valcnt = 1;
6632 				propvals = &unknown;
6633 			} else {
6634 				statep->ls_status = status;
6635 				statep->ls_retstatus = status;
6636 				return;
6637 			}
6638 		} else if (status == DLADM_STATUS_NOTSUP ||
6639 		    statep->ls_persist) {
6640 			valcnt = 1;
6641 			if (type == DLADM_PROP_VAL_CURRENT ||
6642 			    type == DLADM_PROP_VAL_PERM)
6643 				propvals = &unknown;
6644 			else
6645 				propvals = &notsup;
6646 		} else if (status == DLADM_STATUS_NOTDEFINED) {
6647 			propvals = &notsup; /* STR_UNDEF_VAL */
6648 		} else {
6649 			if (statep->ls_proplist &&
6650 			    statep->ls_status == DLADM_STATUS_OK) {
6651 				warn_dlerr(status,
6652 				    "cannot get link property '%s' for %s",
6653 				    propname, statep->ls_link);
6654 			}
6655 			statep->ls_status = status;
6656 			statep->ls_retstatus = status;
6657 			return;
6658 		}
6659 	}
6660 
6661 	statep->ls_status = DLADM_STATUS_OK;
6662 
6663 	buf[0] = '\0';
6664 	ptr = buf;
6665 	lim = buf + DLADM_STRSIZE;
6666 	for (i = 0; i < valcnt; i++) {
6667 		if (propvals[i][0] == '\0' && !statep->ls_parsable)
6668 			ptr += snprintf(ptr, lim - ptr, "--,");
6669 		else
6670 			ptr += snprintf(ptr, lim - ptr, "%s,", propvals[i]);
6671 		if (ptr >= lim)
6672 			break;
6673 	}
6674 	if (valcnt > 0)
6675 		buf[strlen(buf) - 1] = '\0';
6676 
6677 	lim = statep->ls_line + MAX_PROP_LINE;
6678 	if (statep->ls_parsable) {
6679 		*pptr += snprintf(*pptr, lim - *pptr,
6680 		    "%s", buf);
6681 	} else {
6682 		*pptr += snprintf(*pptr, lim - *pptr, format, buf);
6683 	}
6684 }
6685 
6686 static boolean_t
6687 print_linkprop_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
6688 {
6689 	linkprop_args_t		*arg = ofarg->ofmt_cbarg;
6690 	char			*propname = arg->ls_propname;
6691 	show_linkprop_state_t	*statep = arg->ls_state;
6692 	char			*ptr = statep->ls_line;
6693 	char			*lim = ptr + MAX_PROP_LINE;
6694 	datalink_id_t		linkid = arg->ls_linkid;
6695 
6696 	switch (ofarg->ofmt_id) {
6697 	case LINKPROP_LINK:
6698 		(void) snprintf(ptr, lim - ptr, "%s", statep->ls_link);
6699 		break;
6700 	case LINKPROP_PROPERTY:
6701 		(void) snprintf(ptr, lim - ptr, "%s", propname);
6702 		break;
6703 	case LINKPROP_VALUE:
6704 		print_linkprop(linkid, statep, propname,
6705 		    statep->ls_persist ? DLADM_PROP_VAL_PERSISTENT :
6706 		    DLADM_PROP_VAL_CURRENT, "%s", &ptr);
6707 		/*
6708 		 * If we failed to query the link property, for example, query
6709 		 * the persistent value of a non-persistable link property,
6710 		 * simply skip the output.
6711 		 */
6712 		if (statep->ls_status != DLADM_STATUS_OK) {
6713 			/*
6714 			 * Ignore the temponly error when we skip printing
6715 			 * link properties to avoid returning failure on exit.
6716 			 */
6717 			if (statep->ls_retstatus == DLADM_STATUS_TEMPONLY)
6718 				statep->ls_retstatus = DLADM_STATUS_OK;
6719 			goto skip;
6720 		}
6721 		ptr = statep->ls_line;
6722 		break;
6723 	case LINKPROP_PERM:
6724 		print_linkprop(linkid, statep, propname,
6725 		    DLADM_PROP_VAL_PERM, "%s", &ptr);
6726 		if (statep->ls_status != DLADM_STATUS_OK)
6727 			goto skip;
6728 		ptr = statep->ls_line;
6729 		break;
6730 	case LINKPROP_DEFAULT:
6731 		print_linkprop(linkid, statep, propname,
6732 		    DLADM_PROP_VAL_DEFAULT, "%s", &ptr);
6733 		if (statep->ls_status != DLADM_STATUS_OK)
6734 			goto skip;
6735 		ptr = statep->ls_line;
6736 		break;
6737 	case LINKPROP_POSSIBLE:
6738 		print_linkprop(linkid, statep, propname,
6739 		    DLADM_PROP_VAL_MODIFIABLE, "%s ", &ptr);
6740 		if (statep->ls_status != DLADM_STATUS_OK)
6741 			goto skip;
6742 		ptr = statep->ls_line;
6743 		break;
6744 	default:
6745 		die("invalid input");
6746 		break;
6747 	}
6748 	(void) strlcpy(buf, ptr, bufsize);
6749 	return (B_TRUE);
6750 skip:
6751 	return ((statep->ls_status == DLADM_STATUS_OK) ?
6752 	    B_TRUE : B_FALSE);
6753 }
6754 
6755 static boolean_t
6756 linkprop_is_supported(datalink_id_t  linkid, const char *propname,
6757     show_linkprop_state_t *statep)
6758 {
6759 	dladm_status_t	status;
6760 	uint_t		valcnt = DLADM_MAX_PROP_VALCNT;
6761 
6762 	/* if used with -p flag, always print output */
6763 	if (statep->ls_proplist != NULL)
6764 		return (B_TRUE);
6765 
6766 	status = dladm_get_linkprop(handle, linkid, DLADM_PROP_VAL_DEFAULT,
6767 	    propname, statep->ls_propvals, &valcnt);
6768 
6769 	if (status == DLADM_STATUS_OK)
6770 		return (B_TRUE);
6771 
6772 	/*
6773 	 * A system wide default value is not available for the
6774 	 * property. Check if current value can be retrieved.
6775 	 */
6776 	status = dladm_get_linkprop(handle, linkid, DLADM_PROP_VAL_CURRENT,
6777 	    propname, statep->ls_propvals, &valcnt);
6778 
6779 	return (status == DLADM_STATUS_OK);
6780 }
6781 
6782 /* ARGSUSED */
6783 static int
6784 show_linkprop(dladm_handle_t dh, datalink_id_t linkid, const char *propname,
6785     void *arg)
6786 {
6787 	show_linkprop_state_t	*statep = arg;
6788 	linkprop_args_t		ls_arg;
6789 
6790 	bzero(&ls_arg, sizeof (ls_arg));
6791 	ls_arg.ls_state = statep;
6792 	ls_arg.ls_propname = (char *)propname;
6793 	ls_arg.ls_linkid = linkid;
6794 
6795 	/*
6796 	 * This will need to be fixed when kernel interfaces are added
6797 	 * to enable walking of all known private properties. For now,
6798 	 * we are limited to walking persistent private properties only.
6799 	 */
6800 	if ((propname[0] == '_') && !statep->ls_persist &&
6801 	    (statep->ls_proplist == NULL))
6802 		return (DLADM_WALK_CONTINUE);
6803 	if (!statep->ls_parsable &&
6804 	    !linkprop_is_supported(linkid, propname, statep))
6805 		return (DLADM_WALK_CONTINUE);
6806 
6807 	ofmt_print(statep->ls_ofmt, &ls_arg);
6808 
6809 	return (DLADM_WALK_CONTINUE);
6810 }
6811 
6812 static void
6813 do_show_linkprop(int argc, char **argv, const char *use)
6814 {
6815 	int			option;
6816 	char			propstr[DLADM_STRSIZE];
6817 	dladm_arg_list_t	*proplist = NULL;
6818 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
6819 	show_linkprop_state_t	state;
6820 	uint32_t		flags = DLADM_OPT_ACTIVE;
6821 	dladm_status_t		status;
6822 	char			*fields_str = NULL;
6823 	ofmt_handle_t		ofmt;
6824 	ofmt_status_t		oferr;
6825 	uint_t			ofmtflags = 0;
6826 
6827 	bzero(propstr, DLADM_STRSIZE);
6828 	opterr = 0;
6829 	state.ls_propvals = NULL;
6830 	state.ls_line = NULL;
6831 	state.ls_parsable = B_FALSE;
6832 	state.ls_persist = B_FALSE;
6833 	state.ls_header = B_TRUE;
6834 	state.ls_retstatus = DLADM_STATUS_OK;
6835 
6836 	while ((option = getopt_long(argc, argv, ":p:cPo:",
6837 	    prop_longopts, NULL)) != -1) {
6838 		switch (option) {
6839 		case 'p':
6840 			(void) strlcat(propstr, optarg, DLADM_STRSIZE);
6841 			if (strlcat(propstr, ",", DLADM_STRSIZE) >=
6842 			    DLADM_STRSIZE)
6843 				die("property list too long '%s'", propstr);
6844 			break;
6845 		case 'c':
6846 			state.ls_parsable = B_TRUE;
6847 			break;
6848 		case 'P':
6849 			state.ls_persist = B_TRUE;
6850 			flags = DLADM_OPT_PERSIST;
6851 			break;
6852 		case 'o':
6853 			fields_str = optarg;
6854 			break;
6855 		default:
6856 			die_opterr(optopt, option, use);
6857 			break;
6858 		}
6859 	}
6860 
6861 	if (optind == (argc - 1)) {
6862 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
6863 		    NULL, NULL, NULL)) != DLADM_STATUS_OK) {
6864 			die_dlerr(status, "link %s is not valid", argv[optind]);
6865 		}
6866 	} else if (optind != argc) {
6867 		usage();
6868 	}
6869 
6870 	if (dladm_parse_link_props(propstr, &proplist, B_TRUE)
6871 	    != DLADM_STATUS_OK)
6872 		die("invalid link properties specified");
6873 	state.ls_proplist = proplist;
6874 	state.ls_status = DLADM_STATUS_OK;
6875 
6876 	if (state.ls_parsable)
6877 		ofmtflags |= OFMT_PARSABLE;
6878 	else
6879 		ofmtflags |= OFMT_WRAP;
6880 
6881 	oferr = ofmt_open(fields_str, linkprop_fields, ofmtflags, 0, &ofmt);
6882 	ofmt_check(oferr, state.ls_parsable, ofmt, die, warn);
6883 	state.ls_ofmt = ofmt;
6884 
6885 	if (linkid == DATALINK_ALL_LINKID) {
6886 		(void) dladm_walk_datalink_id(show_linkprop_onelink, handle,
6887 		    &state, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE, flags);
6888 	} else {
6889 		(void) show_linkprop_onelink(handle, linkid, &state);
6890 	}
6891 	ofmt_close(ofmt);
6892 	dladm_free_props(proplist);
6893 
6894 	if (state.ls_retstatus != DLADM_STATUS_OK) {
6895 		dladm_close(handle);
6896 		exit(EXIT_FAILURE);
6897 	}
6898 }
6899 
6900 static int
6901 show_linkprop_onelink(dladm_handle_t hdl, datalink_id_t linkid, void *arg)
6902 {
6903 	int			i;
6904 	char			*buf;
6905 	uint32_t		flags;
6906 	dladm_arg_list_t	*proplist = NULL;
6907 	show_linkprop_state_t	*statep = arg;
6908 	dlpi_handle_t		dh = NULL;
6909 
6910 	statep->ls_status = DLADM_STATUS_OK;
6911 
6912 	if (dladm_datalink_id2info(hdl, linkid, &flags, NULL, NULL,
6913 	    statep->ls_link, MAXLINKNAMELEN) != DLADM_STATUS_OK) {
6914 		statep->ls_status = DLADM_STATUS_NOTFOUND;
6915 		return (DLADM_WALK_CONTINUE);
6916 	}
6917 
6918 	if ((statep->ls_persist && !(flags & DLADM_OPT_PERSIST)) ||
6919 	    (!statep->ls_persist && !(flags & DLADM_OPT_ACTIVE))) {
6920 		statep->ls_status = DLADM_STATUS_BADARG;
6921 		return (DLADM_WALK_CONTINUE);
6922 	}
6923 
6924 	proplist = statep->ls_proplist;
6925 
6926 	/*
6927 	 * When some WiFi links are opened for the first time, their hardware
6928 	 * automatically scans for APs and does other slow operations.	Thus,
6929 	 * if there are no open links, the retrieval of link properties
6930 	 * (below) will proceed slowly unless we hold the link open.
6931 	 *
6932 	 * Note that failure of dlpi_open() does not necessarily mean invalid
6933 	 * link properties, because dlpi_open() may fail because of incorrect
6934 	 * autopush configuration. Therefore, we ingore the return value of
6935 	 * dlpi_open().
6936 	 */
6937 	if (!statep->ls_persist)
6938 		(void) dlpi_open(statep->ls_link, &dh, 0);
6939 
6940 	buf = malloc((sizeof (char *) + DLADM_PROP_VAL_MAX) *
6941 	    DLADM_MAX_PROP_VALCNT + MAX_PROP_LINE);
6942 	if (buf == NULL)
6943 		die("insufficient memory");
6944 
6945 	statep->ls_propvals = (char **)(void *)buf;
6946 	for (i = 0; i < DLADM_MAX_PROP_VALCNT; i++) {
6947 		statep->ls_propvals[i] = buf +
6948 		    sizeof (char *) * DLADM_MAX_PROP_VALCNT +
6949 		    i * DLADM_PROP_VAL_MAX;
6950 	}
6951 	statep->ls_line = buf +
6952 	    (sizeof (char *) + DLADM_PROP_VAL_MAX) * DLADM_MAX_PROP_VALCNT;
6953 
6954 	if (proplist != NULL) {
6955 		for (i = 0; i < proplist->al_count; i++) {
6956 			(void) show_linkprop(hdl, linkid,
6957 			    proplist->al_info[i].ai_name, statep);
6958 		}
6959 	} else {
6960 		(void) dladm_walk_linkprop(hdl, linkid, statep,
6961 		    show_linkprop);
6962 	}
6963 	if (dh != NULL)
6964 		dlpi_close(dh);
6965 	free(buf);
6966 	return (DLADM_WALK_CONTINUE);
6967 }
6968 
6969 static int
6970 reset_one_linkprop(dladm_handle_t dh, datalink_id_t linkid,
6971     const char *propname, void *arg)
6972 {
6973 	set_linkprop_state_t	*statep = arg;
6974 	dladm_status_t		status;
6975 
6976 	status = dladm_set_linkprop(dh, linkid, propname, NULL, 0,
6977 	    DLADM_OPT_ACTIVE | (statep->ls_temp ? 0 : DLADM_OPT_PERSIST));
6978 	if (status != DLADM_STATUS_OK &&
6979 	    status != DLADM_STATUS_PROPRDONLY &&
6980 	    status != DLADM_STATUS_NOTSUP) {
6981 		warn_dlerr(status, "cannot reset link property '%s' on '%s'",
6982 		    propname, statep->ls_name);
6983 		statep->ls_status = status;
6984 	}
6985 
6986 	return (DLADM_WALK_CONTINUE);
6987 }
6988 
6989 static void
6990 set_linkprop(int argc, char **argv, boolean_t reset, const char *use)
6991 {
6992 	int			i, option;
6993 	char			errmsg[DLADM_STRSIZE];
6994 	char			*altroot = NULL;
6995 	datalink_id_t		linkid;
6996 	boolean_t		temp = B_FALSE;
6997 	dladm_status_t		status = DLADM_STATUS_OK;
6998 	char			propstr[DLADM_STRSIZE];
6999 	dladm_arg_list_t	*proplist = NULL;
7000 
7001 	opterr = 0;
7002 	bzero(propstr, DLADM_STRSIZE);
7003 
7004 	while ((option = getopt_long(argc, argv, ":p:R:t",
7005 	    prop_longopts, NULL)) != -1) {
7006 		switch (option) {
7007 		case 'p':
7008 			(void) strlcat(propstr, optarg, DLADM_STRSIZE);
7009 			if (strlcat(propstr, ",", DLADM_STRSIZE) >=
7010 			    DLADM_STRSIZE)
7011 				die("property list too long '%s'", propstr);
7012 			break;
7013 		case 't':
7014 			temp = B_TRUE;
7015 			break;
7016 		case 'R':
7017 			altroot = optarg;
7018 			break;
7019 		default:
7020 			die_opterr(optopt, option, use);
7021 
7022 		}
7023 	}
7024 
7025 	/* get link name (required last argument) */
7026 	if (optind != (argc - 1))
7027 		usage();
7028 
7029 	if (dladm_parse_link_props(propstr, &proplist, reset) !=
7030 	    DLADM_STATUS_OK)
7031 		die("invalid link properties specified");
7032 
7033 	if (proplist == NULL && !reset)
7034 		die("link property must be specified");
7035 
7036 	if (altroot != NULL) {
7037 		dladm_free_props(proplist);
7038 		altroot_cmd(altroot, argc, argv);
7039 	}
7040 
7041 	status = dladm_name2info(handle, argv[optind], &linkid, NULL, NULL,
7042 	    NULL);
7043 	if (status != DLADM_STATUS_OK)
7044 		die_dlerr(status, "link %s is not valid", argv[optind]);
7045 
7046 	if (proplist == NULL) {
7047 		set_linkprop_state_t	state;
7048 
7049 		state.ls_name = argv[optind];
7050 		state.ls_reset = reset;
7051 		state.ls_temp = temp;
7052 		state.ls_status = DLADM_STATUS_OK;
7053 
7054 		(void) dladm_walk_linkprop(handle, linkid, &state,
7055 		    reset_one_linkprop);
7056 
7057 		status = state.ls_status;
7058 		goto done;
7059 	}
7060 
7061 	for (i = 0; i < proplist->al_count; i++) {
7062 		dladm_arg_info_t	*aip = &proplist->al_info[i];
7063 		char		**val;
7064 		uint_t		count;
7065 
7066 		if (reset) {
7067 			val = NULL;
7068 			count = 0;
7069 		} else {
7070 			val = aip->ai_val;
7071 			count = aip->ai_count;
7072 			if (count == 0) {
7073 				warn("no value specified for '%s'",
7074 				    aip->ai_name);
7075 				status = DLADM_STATUS_BADARG;
7076 				continue;
7077 			}
7078 		}
7079 		status = dladm_set_linkprop(handle, linkid, aip->ai_name, val,
7080 		    count, DLADM_OPT_ACTIVE | (temp ? 0 : DLADM_OPT_PERSIST));
7081 		switch (status) {
7082 		case DLADM_STATUS_OK:
7083 			break;
7084 		case DLADM_STATUS_NOTFOUND:
7085 			warn("invalid link property '%s'", aip->ai_name);
7086 			break;
7087 		case DLADM_STATUS_BADVAL: {
7088 			int		j;
7089 			char		*ptr, *lim;
7090 			char		**propvals = NULL;
7091 			uint_t		valcnt = DLADM_MAX_PROP_VALCNT;
7092 			dladm_status_t	s;
7093 
7094 			ptr = malloc((sizeof (char *) +
7095 			    DLADM_PROP_VAL_MAX) * DLADM_MAX_PROP_VALCNT +
7096 			    MAX_PROP_LINE);
7097 
7098 			propvals = (char **)(void *)ptr;
7099 			if (propvals == NULL)
7100 				die("insufficient memory");
7101 
7102 			for (j = 0; j < DLADM_MAX_PROP_VALCNT; j++) {
7103 				propvals[j] = ptr + sizeof (char *) *
7104 				    DLADM_MAX_PROP_VALCNT +
7105 				    j * DLADM_PROP_VAL_MAX;
7106 			}
7107 			s = dladm_get_linkprop(handle, linkid,
7108 			    DLADM_PROP_VAL_MODIFIABLE, aip->ai_name, propvals,
7109 			    &valcnt);
7110 
7111 			if (s != DLADM_STATUS_OK) {
7112 				warn_dlerr(status, "cannot set link property "
7113 				    "'%s' on '%s'", aip->ai_name, argv[optind]);
7114 				free(propvals);
7115 				break;
7116 			}
7117 
7118 			ptr = errmsg;
7119 			lim = ptr + DLADM_STRSIZE;
7120 			*ptr = '\0';
7121 			for (j = 0; j < valcnt; j++) {
7122 				ptr += snprintf(ptr, lim - ptr, "%s,",
7123 				    propvals[j]);
7124 				if (ptr >= lim)
7125 					break;
7126 			}
7127 			if (ptr > errmsg) {
7128 				*(ptr - 1) = '\0';
7129 				warn("link property '%s' must be one of: %s",
7130 				    aip->ai_name, errmsg);
7131 			} else
7132 				warn("invalid link property '%s'", *val);
7133 			free(propvals);
7134 			break;
7135 		}
7136 		default:
7137 			if (reset) {
7138 				warn_dlerr(status, "cannot reset link property "
7139 				    "'%s' on '%s'", aip->ai_name, argv[optind]);
7140 			} else {
7141 				warn_dlerr(status, "cannot set link property "
7142 				    "'%s' on '%s'", aip->ai_name, argv[optind]);
7143 			}
7144 			break;
7145 		}
7146 	}
7147 done:
7148 	dladm_free_props(proplist);
7149 	if (status != DLADM_STATUS_OK) {
7150 		dladm_close(handle);
7151 		exit(EXIT_FAILURE);
7152 	}
7153 }
7154 
7155 static void
7156 do_set_linkprop(int argc, char **argv, const char *use)
7157 {
7158 	set_linkprop(argc, argv, B_FALSE, use);
7159 }
7160 
7161 static void
7162 do_reset_linkprop(int argc, char **argv, const char *use)
7163 {
7164 	set_linkprop(argc, argv, B_TRUE, use);
7165 }
7166 
7167 static int
7168 convert_secobj(char *buf, uint_t len, uint8_t *obj_val, uint_t *obj_lenp,
7169     dladm_secobj_class_t class)
7170 {
7171 	int error = 0;
7172 
7173 	if (class == DLADM_SECOBJ_CLASS_WPA) {
7174 		if (len < 8 || len > 63)
7175 			return (EINVAL);
7176 		(void) memcpy(obj_val, buf, len);
7177 		*obj_lenp = len;
7178 		return (error);
7179 	}
7180 
7181 	if (class == DLADM_SECOBJ_CLASS_WEP) {
7182 		switch (len) {
7183 		case 5:			/* ASCII key sizes */
7184 		case 13:
7185 			(void) memcpy(obj_val, buf, len);
7186 			*obj_lenp = len;
7187 			break;
7188 		case 10:		/* Hex key sizes, not preceded by 0x */
7189 		case 26:
7190 			error = hexascii_to_octet(buf, len, obj_val, obj_lenp);
7191 			break;
7192 		case 12:		/* Hex key sizes, preceded by 0x */
7193 		case 28:
7194 			if (strncmp(buf, "0x", 2) != 0)
7195 				return (EINVAL);
7196 			error = hexascii_to_octet(buf + 2, len - 2,
7197 			    obj_val, obj_lenp);
7198 			break;
7199 		default:
7200 			return (EINVAL);
7201 		}
7202 		return (error);
7203 	}
7204 
7205 	return (ENOENT);
7206 }
7207 
7208 static void
7209 defersig(int sig)
7210 {
7211 	signalled = sig;
7212 }
7213 
7214 static int
7215 get_secobj_from_tty(uint_t try, const char *objname, char *buf)
7216 {
7217 	uint_t		len = 0;
7218 	int		c;
7219 	struct termios	stored, current;
7220 	void		(*sigfunc)(int);
7221 
7222 	/*
7223 	 * Turn off echo -- but before we do so, defer SIGINT handling
7224 	 * so that a ^C doesn't leave the terminal corrupted.
7225 	 */
7226 	sigfunc = signal(SIGINT, defersig);
7227 	(void) fflush(stdin);
7228 	(void) tcgetattr(0, &stored);
7229 	current = stored;
7230 	current.c_lflag &= ~(ICANON|ECHO);
7231 	current.c_cc[VTIME] = 0;
7232 	current.c_cc[VMIN] = 1;
7233 	(void) tcsetattr(0, TCSANOW, &current);
7234 again:
7235 	if (try == 1)
7236 		(void) printf(gettext("provide value for '%s': "), objname);
7237 	else
7238 		(void) printf(gettext("confirm value for '%s': "), objname);
7239 
7240 	(void) fflush(stdout);
7241 	while (signalled == 0) {
7242 		c = getchar();
7243 		if (c == '\n' || c == '\r') {
7244 			if (len != 0)
7245 				break;
7246 			(void) putchar('\n');
7247 			goto again;
7248 		}
7249 
7250 		buf[len++] = c;
7251 		if (len >= DLADM_SECOBJ_VAL_MAX - 1)
7252 			break;
7253 		(void) putchar('*');
7254 	}
7255 
7256 	(void) putchar('\n');
7257 	(void) fflush(stdin);
7258 
7259 	/*
7260 	 * Restore terminal setting and handle deferred signals.
7261 	 */
7262 	(void) tcsetattr(0, TCSANOW, &stored);
7263 
7264 	(void) signal(SIGINT, sigfunc);
7265 	if (signalled != 0)
7266 		(void) kill(getpid(), signalled);
7267 
7268 	return (len);
7269 }
7270 
7271 static int
7272 get_secobj_val(char *obj_name, uint8_t *obj_val, uint_t *obj_lenp,
7273     dladm_secobj_class_t class, FILE *filep)
7274 {
7275 	int		rval;
7276 	uint_t		len = 0, len2;
7277 	char		buf[DLADM_SECOBJ_VAL_MAX], buf2[DLADM_SECOBJ_VAL_MAX];
7278 
7279 	if (filep == NULL) {
7280 		len = get_secobj_from_tty(1, obj_name, buf);
7281 		rval = convert_secobj(buf, len, obj_val, obj_lenp, class);
7282 		if (rval == 0) {
7283 			len2 = get_secobj_from_tty(2, obj_name, buf2);
7284 			if (len != len2 || memcmp(buf, buf2, len) != 0)
7285 				rval = ENOTSUP;
7286 		}
7287 		return (rval);
7288 	} else {
7289 		for (;;) {
7290 			if (fgets(buf, sizeof (buf), filep) == NULL)
7291 				break;
7292 			if (isspace(buf[0]))
7293 				continue;
7294 
7295 			len = strlen(buf);
7296 			if (buf[len - 1] == '\n') {
7297 				buf[len - 1] = '\0';
7298 				len--;
7299 			}
7300 			break;
7301 		}
7302 		(void) fclose(filep);
7303 	}
7304 	return (convert_secobj(buf, len, obj_val, obj_lenp, class));
7305 }
7306 
7307 static boolean_t
7308 check_auth(const char *auth)
7309 {
7310 	struct passwd	*pw;
7311 
7312 	if ((pw = getpwuid(getuid())) == NULL)
7313 		return (B_FALSE);
7314 
7315 	return (chkauthattr(auth, pw->pw_name) != 0);
7316 }
7317 
7318 static void
7319 audit_secobj(char *auth, char *class, char *obj,
7320     boolean_t success, boolean_t create)
7321 {
7322 	adt_session_data_t	*ah;
7323 	adt_event_data_t	*event;
7324 	au_event_t		flag;
7325 	char			*errstr;
7326 
7327 	if (create) {
7328 		flag = ADT_dladm_create_secobj;
7329 		errstr = "ADT_dladm_create_secobj";
7330 	} else {
7331 		flag = ADT_dladm_delete_secobj;
7332 		errstr = "ADT_dladm_delete_secobj";
7333 	}
7334 
7335 	if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0)
7336 		die("adt_start_session: %s", strerror(errno));
7337 
7338 	if ((event = adt_alloc_event(ah, flag)) == NULL)
7339 		die("adt_alloc_event (%s): %s", errstr, strerror(errno));
7340 
7341 	/* fill in audit info */
7342 	if (create) {
7343 		event->adt_dladm_create_secobj.auth_used = auth;
7344 		event->adt_dladm_create_secobj.obj_class = class;
7345 		event->adt_dladm_create_secobj.obj_name = obj;
7346 	} else {
7347 		event->adt_dladm_delete_secobj.auth_used = auth;
7348 		event->adt_dladm_delete_secobj.obj_class = class;
7349 		event->adt_dladm_delete_secobj.obj_name = obj;
7350 	}
7351 
7352 	if (success) {
7353 		if (adt_put_event(event, ADT_SUCCESS, ADT_SUCCESS) != 0) {
7354 			die("adt_put_event (%s, success): %s", errstr,
7355 			    strerror(errno));
7356 		}
7357 	} else {
7358 		if (adt_put_event(event, ADT_FAILURE,
7359 		    ADT_FAIL_VALUE_AUTH) != 0) {
7360 			die("adt_put_event: (%s, failure): %s", errstr,
7361 			    strerror(errno));
7362 		}
7363 	}
7364 
7365 	adt_free_event(event);
7366 	(void) adt_end_session(ah);
7367 }
7368 
7369 static void
7370 do_create_secobj(int argc, char **argv, const char *use)
7371 {
7372 	int			option, rval;
7373 	FILE			*filep = NULL;
7374 	char			*obj_name = NULL;
7375 	char			*class_name = NULL;
7376 	uint8_t			obj_val[DLADM_SECOBJ_VAL_MAX];
7377 	uint_t			obj_len;
7378 	boolean_t		success, temp = B_FALSE;
7379 	dladm_status_t		status;
7380 	dladm_secobj_class_t	class = -1;
7381 	uid_t			euid;
7382 
7383 	opterr = 0;
7384 	(void) memset(obj_val, 0, DLADM_SECOBJ_VAL_MAX);
7385 	while ((option = getopt_long(argc, argv, ":f:c:R:t",
7386 	    wifi_longopts, NULL)) != -1) {
7387 		switch (option) {
7388 		case 'f':
7389 			euid = geteuid();
7390 			(void) seteuid(getuid());
7391 			filep = fopen(optarg, "r");
7392 			if (filep == NULL) {
7393 				die("cannot open %s: %s", optarg,
7394 				    strerror(errno));
7395 			}
7396 			(void) seteuid(euid);
7397 			break;
7398 		case 'c':
7399 			class_name = optarg;
7400 			status = dladm_str2secobjclass(optarg, &class);
7401 			if (status != DLADM_STATUS_OK) {
7402 				die("invalid secure object class '%s', "
7403 				    "valid values are: wep, wpa", optarg);
7404 			}
7405 			break;
7406 		case 't':
7407 			temp = B_TRUE;
7408 			break;
7409 		case 'R':
7410 			status = dladm_set_rootdir(optarg);
7411 			if (status != DLADM_STATUS_OK) {
7412 				die_dlerr(status, "invalid directory "
7413 				    "specified");
7414 			}
7415 			break;
7416 		default:
7417 			die_opterr(optopt, option, use);
7418 			break;
7419 		}
7420 	}
7421 
7422 	if (optind == (argc - 1))
7423 		obj_name = argv[optind];
7424 	else if (optind != argc)
7425 		usage();
7426 
7427 	if (class == -1)
7428 		die("secure object class required");
7429 
7430 	if (obj_name == NULL)
7431 		die("secure object name required");
7432 
7433 	if (!dladm_valid_secobj_name(obj_name))
7434 		die("invalid secure object name '%s'", obj_name);
7435 
7436 	success = check_auth(LINK_SEC_AUTH);
7437 	audit_secobj(LINK_SEC_AUTH, class_name, obj_name, success, B_TRUE);
7438 	if (!success)
7439 		die("authorization '%s' is required", LINK_SEC_AUTH);
7440 
7441 	rval = get_secobj_val(obj_name, obj_val, &obj_len, class, filep);
7442 	if (rval != 0) {
7443 		switch (rval) {
7444 		case ENOENT:
7445 			die("invalid secure object class");
7446 			break;
7447 		case EINVAL:
7448 			die("invalid secure object value");
7449 			break;
7450 		case ENOTSUP:
7451 			die("verification failed");
7452 			break;
7453 		default:
7454 			die("invalid secure object: %s", strerror(rval));
7455 			break;
7456 		}
7457 	}
7458 
7459 	status = dladm_set_secobj(handle, obj_name, class, obj_val, obj_len,
7460 	    DLADM_OPT_CREATE | DLADM_OPT_ACTIVE);
7461 	if (status != DLADM_STATUS_OK) {
7462 		die_dlerr(status, "could not create secure object '%s'",
7463 		    obj_name);
7464 	}
7465 	if (temp)
7466 		return;
7467 
7468 	status = dladm_set_secobj(handle, obj_name, class, obj_val, obj_len,
7469 	    DLADM_OPT_PERSIST);
7470 	if (status != DLADM_STATUS_OK) {
7471 		warn_dlerr(status, "could not persistently create secure "
7472 		    "object '%s'", obj_name);
7473 	}
7474 }
7475 
7476 static void
7477 do_delete_secobj(int argc, char **argv, const char *use)
7478 {
7479 	int		i, option;
7480 	boolean_t	temp = B_FALSE;
7481 	boolean_t	success;
7482 	dladm_status_t	status, pstatus;
7483 	int		nfields = 1;
7484 	char		*field, *token, *lasts = NULL, c;
7485 
7486 	opterr = 0;
7487 	status = pstatus = DLADM_STATUS_OK;
7488 	while ((option = getopt_long(argc, argv, ":R:t",
7489 	    wifi_longopts, NULL)) != -1) {
7490 		switch (option) {
7491 		case 't':
7492 			temp = B_TRUE;
7493 			break;
7494 		case 'R':
7495 			status = dladm_set_rootdir(optarg);
7496 			if (status != DLADM_STATUS_OK) {
7497 				die_dlerr(status, "invalid directory "
7498 				    "specified");
7499 			}
7500 			break;
7501 		default:
7502 			die_opterr(optopt, option, use);
7503 			break;
7504 		}
7505 	}
7506 
7507 	if (optind != (argc - 1))
7508 		die("secure object name required");
7509 
7510 	token = argv[optind];
7511 	while ((c = *token++) != '\0') {
7512 		if (c == ',')
7513 			nfields++;
7514 	}
7515 	token = strdup(argv[optind]);
7516 	if (token == NULL)
7517 		die("no memory");
7518 
7519 	success = check_auth(LINK_SEC_AUTH);
7520 	audit_secobj(LINK_SEC_AUTH, "unknown", argv[optind], success, B_FALSE);
7521 	if (!success)
7522 		die("authorization '%s' is required", LINK_SEC_AUTH);
7523 
7524 	for (i = 0; i < nfields; i++) {
7525 
7526 		field = strtok_r(token, ",", &lasts);
7527 		token = NULL;
7528 		status = dladm_unset_secobj(handle, field, DLADM_OPT_ACTIVE);
7529 		if (!temp) {
7530 			pstatus = dladm_unset_secobj(handle, field,
7531 			    DLADM_OPT_PERSIST);
7532 		} else {
7533 			pstatus = DLADM_STATUS_OK;
7534 		}
7535 
7536 		if (status != DLADM_STATUS_OK) {
7537 			warn_dlerr(status, "could not delete secure object "
7538 			    "'%s'", field);
7539 		}
7540 		if (pstatus != DLADM_STATUS_OK) {
7541 			warn_dlerr(pstatus, "could not persistently delete "
7542 			    "secure object '%s'", field);
7543 		}
7544 	}
7545 	free(token);
7546 
7547 	if (status != DLADM_STATUS_OK || pstatus != DLADM_STATUS_OK) {
7548 		dladm_close(handle);
7549 		exit(EXIT_FAILURE);
7550 	}
7551 }
7552 
7553 typedef struct show_secobj_state {
7554 	boolean_t	ss_persist;
7555 	boolean_t	ss_parsable;
7556 	boolean_t	ss_header;
7557 	ofmt_handle_t	ss_ofmt;
7558 } show_secobj_state_t;
7559 
7560 
7561 static boolean_t
7562 show_secobj(dladm_handle_t dh, void *arg, const char *obj_name)
7563 {
7564 	uint_t			obj_len = DLADM_SECOBJ_VAL_MAX;
7565 	uint8_t			obj_val[DLADM_SECOBJ_VAL_MAX];
7566 	char			buf[DLADM_STRSIZE];
7567 	uint_t			flags = 0;
7568 	dladm_secobj_class_t	class;
7569 	show_secobj_state_t	*statep = arg;
7570 	dladm_status_t		status;
7571 	secobj_fields_buf_t	sbuf;
7572 
7573 	bzero(&sbuf, sizeof (secobj_fields_buf_t));
7574 	if (statep->ss_persist)
7575 		flags |= DLADM_OPT_PERSIST;
7576 
7577 	status = dladm_get_secobj(dh, obj_name, &class, obj_val, &obj_len,
7578 	    flags);
7579 	if (status != DLADM_STATUS_OK)
7580 		die_dlerr(status, "cannot get secure object '%s'", obj_name);
7581 
7582 	(void) snprintf(sbuf.ss_obj_name, sizeof (sbuf.ss_obj_name),
7583 	    obj_name);
7584 	(void) dladm_secobjclass2str(class, buf);
7585 	(void) snprintf(sbuf.ss_class, sizeof (sbuf.ss_class), "%s", buf);
7586 	if (getuid() == 0) {
7587 		char	val[DLADM_SECOBJ_VAL_MAX * 2];
7588 		uint_t	len = sizeof (val);
7589 
7590 		if (octet_to_hexascii(obj_val, obj_len, val, &len) == 0)
7591 			(void) snprintf(sbuf.ss_val,
7592 			    sizeof (sbuf.ss_val), "%s", val);
7593 	}
7594 	ofmt_print(statep->ss_ofmt, &sbuf);
7595 	return (B_TRUE);
7596 }
7597 
7598 static void
7599 do_show_secobj(int argc, char **argv, const char *use)
7600 {
7601 	int			option;
7602 	show_secobj_state_t	state;
7603 	dladm_status_t		status;
7604 	boolean_t		o_arg = B_FALSE;
7605 	uint_t			i;
7606 	uint_t			flags;
7607 	char			*fields_str = NULL;
7608 	char			*def_fields = "object,class";
7609 	char			*all_fields = "object,class,value";
7610 	char			*field, *token, *lasts = NULL, c;
7611 	ofmt_handle_t		ofmt;
7612 	ofmt_status_t		oferr;
7613 	uint_t			ofmtflags = 0;
7614 
7615 	opterr = 0;
7616 	bzero(&state, sizeof (state));
7617 	state.ss_parsable = B_FALSE;
7618 	fields_str = def_fields;
7619 	state.ss_persist = B_FALSE;
7620 	state.ss_parsable = B_FALSE;
7621 	state.ss_header = B_TRUE;
7622 	while ((option = getopt_long(argc, argv, ":pPo:",
7623 	    wifi_longopts, NULL)) != -1) {
7624 		switch (option) {
7625 		case 'p':
7626 			state.ss_parsable = B_TRUE;
7627 			break;
7628 		case 'P':
7629 			state.ss_persist = B_TRUE;
7630 			break;
7631 		case 'o':
7632 			o_arg = B_TRUE;
7633 			if (strcasecmp(optarg, "all") == 0)
7634 				fields_str = all_fields;
7635 			else
7636 				fields_str = optarg;
7637 			break;
7638 		default:
7639 			die_opterr(optopt, option, use);
7640 			break;
7641 		}
7642 	}
7643 
7644 	if (state.ss_parsable && !o_arg)
7645 		die("option -c requires -o");
7646 
7647 	if (state.ss_parsable && fields_str == all_fields)
7648 		die("\"-o all\" is invalid with -p");
7649 
7650 	if (state.ss_parsable)
7651 		ofmtflags |= OFMT_PARSABLE;
7652 	oferr = ofmt_open(fields_str, secobj_fields, ofmtflags, 0, &ofmt);
7653 	ofmt_check(oferr, state.ss_parsable, ofmt, die, warn);
7654 	state.ss_ofmt = ofmt;
7655 
7656 	flags = state.ss_persist ? DLADM_OPT_PERSIST : 0;
7657 
7658 	if (optind == (argc - 1)) {
7659 		uint_t obj_fields = 1;
7660 
7661 		token = argv[optind];
7662 		if (token == NULL)
7663 			die("secure object name required");
7664 		while ((c = *token++) != '\0') {
7665 			if (c == ',')
7666 				obj_fields++;
7667 		}
7668 		token = strdup(argv[optind]);
7669 		if (token == NULL)
7670 			die("no memory");
7671 		for (i = 0; i < obj_fields; i++) {
7672 			field = strtok_r(token, ",", &lasts);
7673 			token = NULL;
7674 			if (!show_secobj(handle, &state, field))
7675 				break;
7676 		}
7677 		free(token);
7678 		ofmt_close(ofmt);
7679 		return;
7680 	} else if (optind != argc)
7681 		usage();
7682 
7683 	status = dladm_walk_secobj(handle, &state, show_secobj, flags);
7684 
7685 	if (status != DLADM_STATUS_OK)
7686 		die_dlerr(status, "show-secobj");
7687 	ofmt_close(ofmt);
7688 }
7689 
7690 /*ARGSUSED*/
7691 static int
7692 i_dladm_init_linkprop(dladm_handle_t dh, datalink_id_t linkid, void *arg)
7693 {
7694 	(void) dladm_init_linkprop(dh, linkid, B_TRUE);
7695 	return (DLADM_WALK_CONTINUE);
7696 }
7697 
7698 /*ARGSUSED*/
7699 void
7700 do_init_linkprop(int argc, char **argv, const char *use)
7701 {
7702 	int			option;
7703 	dladm_status_t		status;
7704 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
7705 	datalink_media_t	media = DATALINK_ANY_MEDIATYPE;
7706 	uint_t			any_media = B_TRUE;
7707 
7708 	opterr = 0;
7709 	while ((option = getopt(argc, argv, ":w")) != -1) {
7710 		switch (option) {
7711 		case 'w':
7712 			media = DL_WIFI;
7713 			any_media = B_FALSE;
7714 			break;
7715 		default:
7716 			/*
7717 			 * Because init-linkprop is not a public command,
7718 			 * print the usage instead.
7719 			 */
7720 			usage();
7721 			break;
7722 		}
7723 	}
7724 
7725 	if (optind == (argc - 1)) {
7726 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
7727 		    NULL, NULL, NULL)) != DLADM_STATUS_OK)
7728 			die_dlerr(status, "link %s is not valid", argv[optind]);
7729 	} else if (optind != argc) {
7730 		usage();
7731 	}
7732 
7733 	if (linkid == DATALINK_ALL_LINKID) {
7734 		/*
7735 		 * linkprops of links of other classes have been initialized as
7736 		 * part of the dladm up-xxx operation.
7737 		 */
7738 		(void) dladm_walk_datalink_id(i_dladm_init_linkprop, handle,
7739 		    NULL, DATALINK_CLASS_PHYS, media, DLADM_OPT_PERSIST);
7740 	} else {
7741 		(void) dladm_init_linkprop(handle, linkid, any_media);
7742 	}
7743 }
7744 
7745 static void
7746 do_show_ether(int argc, char **argv, const char *use)
7747 {
7748 	int			option;
7749 	datalink_id_t		linkid;
7750 	print_ether_state_t	state;
7751 	char			*fields_str = NULL;
7752 	ofmt_handle_t		ofmt;
7753 	ofmt_status_t		oferr;
7754 	uint_t			ofmtflags = 0;
7755 
7756 	bzero(&state, sizeof (state));
7757 	state.es_link = NULL;
7758 	state.es_parsable = B_FALSE;
7759 
7760 	while ((option = getopt_long(argc, argv, "o:px",
7761 	    showeth_lopts, NULL)) != -1) {
7762 		switch (option) {
7763 			case 'x':
7764 				state.es_extended = B_TRUE;
7765 				break;
7766 			case 'p':
7767 				state.es_parsable = B_TRUE;
7768 				break;
7769 			case 'o':
7770 				fields_str = optarg;
7771 				break;
7772 			default:
7773 				die_opterr(optopt, option, use);
7774 				break;
7775 		}
7776 	}
7777 
7778 	if (optind == (argc - 1))
7779 		state.es_link = argv[optind];
7780 
7781 	if (state.es_parsable)
7782 		ofmtflags |= OFMT_PARSABLE;
7783 	oferr = ofmt_open(fields_str, ether_fields, ofmtflags,
7784 	    DLADM_DEFAULT_COL, &ofmt);
7785 	ofmt_check(oferr, state.es_parsable, ofmt, die, warn);
7786 	state.es_ofmt = ofmt;
7787 
7788 	if (state.es_link == NULL) {
7789 		(void) dladm_walk_datalink_id(show_etherprop, handle, &state,
7790 		    DATALINK_CLASS_PHYS, DL_ETHER, DLADM_OPT_ACTIVE);
7791 	} else {
7792 		if (!link_is_ether(state.es_link, &linkid))
7793 			die("invalid link specified");
7794 		(void) show_etherprop(handle, linkid, &state);
7795 	}
7796 	ofmt_close(ofmt);
7797 }
7798 
7799 static int
7800 show_etherprop(dladm_handle_t dh, datalink_id_t linkid, void *arg)
7801 {
7802 	print_ether_state_t	*statep = arg;
7803 	ether_fields_buf_t	ebuf;
7804 	dladm_ether_info_t	eattr;
7805 	dladm_status_t		status;
7806 
7807 	bzero(&ebuf, sizeof (ether_fields_buf_t));
7808 	if (dladm_datalink_id2info(dh, linkid, NULL, NULL, NULL,
7809 	    ebuf.eth_link, sizeof (ebuf.eth_link)) != DLADM_STATUS_OK) {
7810 		return (DLADM_WALK_CONTINUE);
7811 	}
7812 
7813 	status = dladm_ether_info(dh, linkid, &eattr);
7814 	if (status != DLADM_STATUS_OK) {
7815 		return (DLADM_WALK_CONTINUE);
7816 	}
7817 
7818 	(void) strlcpy(ebuf.eth_ptype, "current", sizeof (ebuf.eth_ptype));
7819 
7820 	(void) dladm_ether_autoneg2str(ebuf.eth_autoneg,
7821 	    sizeof (ebuf.eth_autoneg), &eattr, CURRENT);
7822 	(void) dladm_ether_pause2str(ebuf.eth_pause,
7823 	    sizeof (ebuf.eth_pause), &eattr, CURRENT);
7824 	(void) dladm_ether_spdx2str(ebuf.eth_spdx,
7825 	    sizeof (ebuf.eth_spdx), &eattr, CURRENT);
7826 	(void) strlcpy(ebuf.eth_state,
7827 	    dladm_linkstate2str(eattr.lei_state, ebuf.eth_state),
7828 	    sizeof (ebuf.eth_state));
7829 	(void) strlcpy(ebuf.eth_rem_fault,
7830 	    (eattr.lei_attr[CURRENT].le_fault ? "fault" : "none"),
7831 	    sizeof (ebuf.eth_rem_fault));
7832 
7833 	ofmt_print(statep->es_ofmt, &ebuf);
7834 
7835 	if (statep->es_extended)
7836 		show_ether_xprop(arg, &eattr);
7837 
7838 	dladm_ether_info_done(&eattr);
7839 	return (DLADM_WALK_CONTINUE);
7840 }
7841 
7842 /* ARGSUSED */
7843 static void
7844 do_init_secobj(int argc, char **argv, const char *use)
7845 {
7846 	dladm_status_t	status;
7847 
7848 	status = dladm_init_secobj(handle);
7849 	if (status != DLADM_STATUS_OK)
7850 		die_dlerr(status, "secure object initialization failed");
7851 }
7852 
7853 enum bridge_func {
7854 	brCreate, brAdd, brModify
7855 };
7856 
7857 static void
7858 create_modify_add_bridge(int argc, char **argv, const char *use,
7859     enum bridge_func func)
7860 {
7861 	int			option;
7862 	uint_t			n, i, nlink;
7863 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
7864 	char			*altroot = NULL;
7865 	char			*links[MAXPORT];
7866 	datalink_id_t		linkids[MAXPORT];
7867 	dladm_status_t		status;
7868 	const char		*bridge;
7869 	UID_STP_CFG_T		cfg, cfg_old;
7870 	dladm_bridge_prot_t	brprot = DLADM_BRIDGE_PROT_UNKNOWN;
7871 	dladm_bridge_prot_t	brprot_old;
7872 
7873 	/* Set up the default configuration values */
7874 	cfg.field_mask = 0;
7875 	cfg.bridge_priority = DEF_BR_PRIO;
7876 	cfg.max_age = DEF_BR_MAXAGE;
7877 	cfg.hello_time = DEF_BR_HELLOT;
7878 	cfg.forward_delay = DEF_BR_FWDELAY;
7879 	cfg.force_version = DEF_FORCE_VERS;
7880 
7881 	nlink = opterr = 0;
7882 	while ((option = getopt_long(argc, argv, ":P:R:d:f:h:l:m:p:",
7883 	    bridge_lopts, NULL)) != -1) {
7884 		switch (option) {
7885 		case 'P':
7886 			if (func == brAdd)
7887 				die_opterr(optopt, option, use);
7888 			status = dladm_bridge_str2prot(optarg, &brprot);
7889 			if (status != DLADM_STATUS_OK)
7890 				die_dlerr(status, "protection %s", optarg);
7891 			break;
7892 		case 'R':
7893 			altroot = optarg;
7894 			break;
7895 		case 'd':
7896 			if (func == brAdd)
7897 				die_opterr(optopt, option, use);
7898 			if (cfg.field_mask & BR_CFG_DELAY)
7899 				die("forwarding delay set more than once");
7900 			if (!str2int(optarg, &cfg.forward_delay) ||
7901 			    cfg.forward_delay < MIN_BR_FWDELAY ||
7902 			    cfg.forward_delay > MAX_BR_FWDELAY)
7903 				die("incorrect forwarding delay");
7904 			cfg.field_mask |= BR_CFG_DELAY;
7905 			break;
7906 		case 'f':
7907 			if (func == brAdd)
7908 				die_opterr(optopt, option, use);
7909 			if (cfg.field_mask & BR_CFG_FORCE_VER)
7910 				die("force protocol set more than once");
7911 			if (!str2int(optarg, &cfg.force_version) ||
7912 			    cfg.force_version < 0)
7913 				die("incorrect force protocol");
7914 			cfg.field_mask |= BR_CFG_FORCE_VER;
7915 			break;
7916 		case 'h':
7917 			if (func == brAdd)
7918 				die_opterr(optopt, option, use);
7919 			if (cfg.field_mask & BR_CFG_HELLO)
7920 				die("hello time set more than once");
7921 			if (!str2int(optarg, &cfg.hello_time) ||
7922 			    cfg.hello_time < MIN_BR_HELLOT ||
7923 			    cfg.hello_time > MAX_BR_HELLOT)
7924 				die("incorrect hello time");
7925 			cfg.field_mask |= BR_CFG_HELLO;
7926 			break;
7927 		case 'l':
7928 			if (func == brModify)
7929 				die_opterr(optopt, option, use);
7930 			if (nlink >= MAXPORT)
7931 				die("too many links specified");
7932 			links[nlink++] = optarg;
7933 			break;
7934 		case 'm':
7935 			if (func == brAdd)
7936 				die_opterr(optopt, option, use);
7937 			if (cfg.field_mask & BR_CFG_AGE)
7938 				die("max age set more than once");
7939 			if (!str2int(optarg, &cfg.max_age) ||
7940 			    cfg.max_age < MIN_BR_MAXAGE ||
7941 			    cfg.max_age > MAX_BR_MAXAGE)
7942 				die("incorrect max age");
7943 			cfg.field_mask |= BR_CFG_AGE;
7944 			break;
7945 		case 'p':
7946 			if (func == brAdd)
7947 				die_opterr(optopt, option, use);
7948 			if (cfg.field_mask & BR_CFG_PRIO)
7949 				die("priority set more than once");
7950 			if (!str2int(optarg, &cfg.bridge_priority) ||
7951 			    cfg.bridge_priority < MIN_BR_PRIO ||
7952 			    cfg.bridge_priority > MAX_BR_PRIO)
7953 				die("incorrect priority");
7954 			cfg.bridge_priority &= 0xF000;
7955 			cfg.field_mask |= BR_CFG_PRIO;
7956 			break;
7957 		default:
7958 			die_opterr(optopt, option, use);
7959 			break;
7960 		}
7961 	}
7962 
7963 	/* get the bridge name (required last argument) */
7964 	if (optind != (argc-1))
7965 		usage();
7966 
7967 	bridge = argv[optind];
7968 	if (!dladm_valid_bridgename(bridge))
7969 		die("invalid bridge name '%s'", bridge);
7970 
7971 	/*
7972 	 * Get the current properties, if any, and merge in with changes.  This
7973 	 * is necessary (even with the field_mask feature) so that the
7974 	 * value-checking macros will produce the right results with proposed
7975 	 * changes to existing configuration.  We only need it for those
7976 	 * parameters, though.
7977 	 */
7978 	(void) dladm_bridge_get_properties(bridge, &cfg_old, &brprot_old);
7979 	if (brprot == DLADM_BRIDGE_PROT_UNKNOWN)
7980 		brprot = brprot_old;
7981 	if (!(cfg.field_mask & BR_CFG_AGE))
7982 		cfg.max_age = cfg_old.max_age;
7983 	if (!(cfg.field_mask & BR_CFG_HELLO))
7984 		cfg.hello_time = cfg_old.hello_time;
7985 	if (!(cfg.field_mask & BR_CFG_DELAY))
7986 		cfg.forward_delay = cfg_old.forward_delay;
7987 
7988 	if (!CHECK_BRIDGE_CONFIG(cfg)) {
7989 		warn("illegal forward delay / max age / hello time "
7990 		    "combination");
7991 		if (NO_MAXAGE(cfg)) {
7992 			die("no max age possible: need forward delay >= %d or "
7993 			    "hello time <= %d", MIN_FWDELAY_NOM(cfg),
7994 			    MAX_HELLOTIME_NOM(cfg));
7995 		} else if (SMALL_MAXAGE(cfg)) {
7996 			if (CAPPED_MAXAGE(cfg))
7997 				die("max age too small: need age >= %d and "
7998 				    "<= %d or hello time <= %d",
7999 				    MIN_MAXAGE(cfg), MAX_MAXAGE(cfg),
8000 				    MAX_HELLOTIME(cfg));
8001 			else
8002 				die("max age too small: need age >= %d or "
8003 				    "hello time <= %d",
8004 				    MIN_MAXAGE(cfg), MAX_HELLOTIME(cfg));
8005 		} else if (FLOORED_MAXAGE(cfg)) {
8006 			die("max age too large: need age >= %d and <= %d or "
8007 			    "forward delay >= %d",
8008 			    MIN_MAXAGE(cfg), MAX_MAXAGE(cfg),
8009 			    MIN_FWDELAY(cfg));
8010 		} else {
8011 			die("max age too large: need age <= %d or forward "
8012 			    "delay >= %d",
8013 			    MAX_MAXAGE(cfg), MIN_FWDELAY(cfg));
8014 		}
8015 	}
8016 
8017 	if (altroot != NULL)
8018 		altroot_cmd(altroot, argc, argv);
8019 
8020 	for (n = 0; n < nlink; n++) {
8021 		datalink_class_t class;
8022 		uint32_t media;
8023 		char pointless[DLADM_STRSIZE];
8024 
8025 		if (dladm_name2info(handle, links[n], &linkids[n], NULL, &class,
8026 		    &media) != DLADM_STATUS_OK)
8027 			die("invalid link name '%s'", links[n]);
8028 		if (class & ~(DATALINK_CLASS_PHYS | DATALINK_CLASS_AGGR |
8029 		    DATALINK_CLASS_ETHERSTUB | DATALINK_CLASS_SIMNET))
8030 			die("%s %s cannot be bridged",
8031 			    dladm_class2str(class, pointless), links[n]);
8032 		if (media != DL_ETHER && media != DL_100VG &&
8033 		    media != DL_ETH_CSMA && media != DL_100BT)
8034 			die("%s interface %s cannot be bridged",
8035 			    dladm_media2str(media, pointless), links[n]);
8036 	}
8037 
8038 	if (func == brCreate)
8039 		flags |= DLADM_OPT_CREATE;
8040 
8041 	if (func != brAdd) {
8042 		status = dladm_bridge_configure(handle, bridge, &cfg, brprot,
8043 		    flags);
8044 		if (status != DLADM_STATUS_OK)
8045 			die_dlerr(status, "create operation failed");
8046 	}
8047 
8048 	status = DLADM_STATUS_OK;
8049 	for (n = 0; n < nlink; n++) {
8050 		status = dladm_bridge_setlink(handle, linkids[n], bridge);
8051 		if (status != DLADM_STATUS_OK)
8052 			break;
8053 	}
8054 
8055 	if (n >= nlink) {
8056 		/*
8057 		 * We were successful.  If we're creating a new bridge, then
8058 		 * there's just one more step: enabling.  If we're modifying or
8059 		 * just adding links, then we're done.
8060 		 */
8061 		if (func != brCreate ||
8062 		    (status = dladm_bridge_enable(bridge)) == DLADM_STATUS_OK)
8063 			return;
8064 	}
8065 
8066 	/* clean up the partial configuration */
8067 	for (i = 0; i < n; i++)
8068 		(void) dladm_bridge_setlink(handle, linkids[i], "");
8069 
8070 	/* if failure for brCreate, then delete the bridge */
8071 	if (func == brCreate)
8072 		(void) dladm_bridge_delete(handle, bridge, flags);
8073 
8074 	if (n < nlink)
8075 		die_dlerr(status, "unable to add link %s to bridge %s",
8076 		    links[n], bridge);
8077 	else
8078 		die_dlerr(status, "unable to enable bridge %s", bridge);
8079 }
8080 
8081 static void
8082 do_create_bridge(int argc, char **argv, const char *use)
8083 {
8084 	create_modify_add_bridge(argc, argv, use, brCreate);
8085 }
8086 
8087 static void
8088 do_modify_bridge(int argc, char **argv, const char *use)
8089 {
8090 	create_modify_add_bridge(argc, argv, use, brModify);
8091 }
8092 
8093 static void
8094 do_add_bridge(int argc, char **argv, const char *use)
8095 {
8096 	create_modify_add_bridge(argc, argv, use, brAdd);
8097 }
8098 
8099 static void
8100 do_delete_bridge(int argc, char **argv, const char *use)
8101 {
8102 	char			option;
8103 	char			*altroot = NULL;
8104 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
8105 	dladm_status_t		status;
8106 
8107 	opterr = 0;
8108 	while ((option = getopt_long(argc, argv, ":R:", bridge_lopts, NULL)) !=
8109 	    -1) {
8110 		switch (option) {
8111 		case 'R':
8112 			altroot = optarg;
8113 			break;
8114 		default:
8115 			die_opterr(optopt, option, use);
8116 			break;
8117 		}
8118 	}
8119 
8120 	/* get the bridge name (required last argument) */
8121 	if (optind != (argc-1))
8122 		usage();
8123 
8124 	if (altroot != NULL)
8125 		altroot_cmd(altroot, argc, argv);
8126 
8127 	status = dladm_bridge_delete(handle, argv[optind], flags);
8128 	if (status != DLADM_STATUS_OK)
8129 		die_dlerr(status, "delete operation failed");
8130 }
8131 
8132 static void
8133 do_remove_bridge(int argc, char **argv, const char *use)
8134 {
8135 	char		option;
8136 	uint_t		n, nlink;
8137 	char		*links[MAXPORT];
8138 	datalink_id_t	linkids[MAXPORT];
8139 	char		*altroot = NULL;
8140 	dladm_status_t	status;
8141 	boolean_t	removed_one;
8142 
8143 	nlink = opterr = 0;
8144 	while ((option = getopt_long(argc, argv, ":R:l:", bridge_lopts,
8145 	    NULL)) != -1) {
8146 		switch (option) {
8147 		case 'R':
8148 			altroot = optarg;
8149 			break;
8150 		case 'l':
8151 			if (nlink >= MAXPORT)
8152 				die("too many links specified");
8153 			links[nlink++] = optarg;
8154 			break;
8155 		default:
8156 			die_opterr(optopt, option, use);
8157 			break;
8158 		}
8159 	}
8160 
8161 	if (nlink == 0)
8162 		usage();
8163 
8164 	/* get the bridge name (required last argument) */
8165 	if (optind != (argc-1))
8166 		usage();
8167 
8168 	if (altroot != NULL)
8169 		altroot_cmd(altroot, argc, argv);
8170 
8171 	for (n = 0; n < nlink; n++) {
8172 		char bridge[MAXLINKNAMELEN];
8173 
8174 		if (dladm_name2info(handle, links[n], &linkids[n], NULL, NULL,
8175 		    NULL) != DLADM_STATUS_OK)
8176 			die("invalid link name '%s'", links[n]);
8177 		status = dladm_bridge_getlink(handle, linkids[n], bridge,
8178 		    sizeof (bridge));
8179 		if (status != DLADM_STATUS_OK &&
8180 		    status != DLADM_STATUS_NOTFOUND) {
8181 			die_dlerr(status, "cannot get bridge status on %s",
8182 			    links[n]);
8183 		}
8184 		if (status == DLADM_STATUS_NOTFOUND ||
8185 		    strcmp(bridge, argv[optind]) != 0)
8186 			die("link %s is not on bridge %s", links[n],
8187 			    argv[optind]);
8188 	}
8189 
8190 	removed_one = B_FALSE;
8191 	for (n = 0; n < nlink; n++) {
8192 		status = dladm_bridge_setlink(handle, linkids[n], "");
8193 		if (status == DLADM_STATUS_OK) {
8194 			removed_one = B_TRUE;
8195 		} else {
8196 			warn_dlerr(status,
8197 			    "cannot remove link %s from bridge %s",
8198 			    links[n], argv[optind]);
8199 		}
8200 	}
8201 	if (!removed_one)
8202 		die("unable to remove any links from bridge %s", argv[optind]);
8203 }
8204 
8205 static void
8206 fmt_int(char *buf, size_t buflen, int value, int runvalue,
8207     boolean_t printstar)
8208 {
8209 	(void) snprintf(buf, buflen, "%d", value);
8210 	if (value != runvalue && printstar)
8211 		(void) strlcat(buf, "*", buflen);
8212 }
8213 
8214 static void
8215 fmt_bridge_id(char *buf, size_t buflen, UID_BRIDGE_ID_T *bid)
8216 {
8217 	(void) snprintf(buf, buflen, "%u/%x:%x:%x:%x:%x:%x", bid->prio,
8218 	    bid->addr[0], bid->addr[1], bid->addr[2], bid->addr[3],
8219 	    bid->addr[4], bid->addr[5]);
8220 }
8221 
8222 static dladm_status_t
8223 print_bridge(show_state_t *state, datalink_id_t linkid,
8224     bridge_fields_buf_t *bbuf)
8225 {
8226 	char			link[MAXLINKNAMELEN];
8227 	datalink_class_t	class;
8228 	uint32_t		flags;
8229 	dladm_status_t		status;
8230 	UID_STP_CFG_T		smfcfg, runcfg;
8231 	UID_STP_STATE_T		stpstate;
8232 	dladm_bridge_prot_t	smfprot, runprot;
8233 
8234 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, &class,
8235 	    NULL, link, sizeof (link))) != DLADM_STATUS_OK)
8236 		return (status);
8237 
8238 	if (!(state->ls_flags & flags))
8239 		return (DLADM_STATUS_NOTFOUND);
8240 
8241 	/* Convert observability node name back to bridge name */
8242 	if (!dladm_observe_to_bridge(link))
8243 		return (DLADM_STATUS_NOTFOUND);
8244 	(void) strlcpy(bbuf->bridge_name, link, sizeof (bbuf->bridge_name));
8245 
8246 	/*
8247 	 * If the running value differs from the one in SMF, and parsable
8248 	 * output is not requested, then we show the running value with an
8249 	 * asterisk.
8250 	 */
8251 	(void) dladm_bridge_get_properties(bbuf->bridge_name, &smfcfg,
8252 	    &smfprot);
8253 	(void) dladm_bridge_run_properties(bbuf->bridge_name, &runcfg,
8254 	    &runprot);
8255 	(void) snprintf(bbuf->bridge_protect, sizeof (bbuf->bridge_protect),
8256 	    "%s%s", state->ls_parsable || smfprot == runprot ? "" : "*",
8257 	    dladm_bridge_prot2str(runprot));
8258 	fmt_int(bbuf->bridge_priority, sizeof (bbuf->bridge_priority),
8259 	    smfcfg.bridge_priority, runcfg.bridge_priority,
8260 	    !state->ls_parsable && (runcfg.field_mask & BR_CFG_AGE));
8261 	fmt_int(bbuf->bridge_bmaxage, sizeof (bbuf->bridge_bmaxage),
8262 	    smfcfg.max_age, runcfg.max_age,
8263 	    !state->ls_parsable && (runcfg.field_mask & BR_CFG_AGE));
8264 	fmt_int(bbuf->bridge_bhellotime,
8265 	    sizeof (bbuf->bridge_bhellotime), smfcfg.hello_time,
8266 	    runcfg.hello_time,
8267 	    !state->ls_parsable && (runcfg.field_mask & BR_CFG_HELLO));
8268 	fmt_int(bbuf->bridge_bfwddelay, sizeof (bbuf->bridge_bfwddelay),
8269 	    smfcfg.forward_delay, runcfg.forward_delay,
8270 	    !state->ls_parsable && (runcfg.field_mask & BR_CFG_DELAY));
8271 	fmt_int(bbuf->bridge_forceproto, sizeof (bbuf->bridge_forceproto),
8272 	    smfcfg.force_version, runcfg.force_version,
8273 	    !state->ls_parsable && (runcfg.field_mask & BR_CFG_FORCE_VER));
8274 	fmt_int(bbuf->bridge_holdtime, sizeof (bbuf->bridge_holdtime),
8275 	    smfcfg.hold_time, runcfg.hold_time,
8276 	    !state->ls_parsable && (runcfg.field_mask & BR_CFG_HOLD_TIME));
8277 
8278 	if (dladm_bridge_state(bbuf->bridge_name, &stpstate) ==
8279 	    DLADM_STATUS_OK) {
8280 		fmt_bridge_id(bbuf->bridge_address,
8281 		    sizeof (bbuf->bridge_address), &stpstate.bridge_id);
8282 		(void) snprintf(bbuf->bridge_tctime,
8283 		    sizeof (bbuf->bridge_tctime), "%lu",
8284 		    stpstate.timeSince_Topo_Change);
8285 		(void) snprintf(bbuf->bridge_tccount,
8286 		    sizeof (bbuf->bridge_tccount), "%lu",
8287 		    stpstate.Topo_Change_Count);
8288 		(void) snprintf(bbuf->bridge_tchange,
8289 		    sizeof (bbuf->bridge_tchange), "%u", stpstate.Topo_Change);
8290 		fmt_bridge_id(bbuf->bridge_desroot,
8291 		    sizeof (bbuf->bridge_desroot), &stpstate.designated_root);
8292 		(void) snprintf(bbuf->bridge_rootcost,
8293 		    sizeof (bbuf->bridge_rootcost), "%lu",
8294 		    stpstate.root_path_cost);
8295 		(void) snprintf(bbuf->bridge_rootport,
8296 		    sizeof (bbuf->bridge_rootport), "%u", stpstate.root_port);
8297 		(void) snprintf(bbuf->bridge_maxage,
8298 		    sizeof (bbuf->bridge_maxage), "%d", stpstate.max_age);
8299 		(void) snprintf(bbuf->bridge_hellotime,
8300 		    sizeof (bbuf->bridge_hellotime), "%d", stpstate.hello_time);
8301 		(void) snprintf(bbuf->bridge_fwddelay,
8302 		    sizeof (bbuf->bridge_fwddelay), "%d",
8303 		    stpstate.forward_delay);
8304 	}
8305 	return (DLADM_STATUS_OK);
8306 }
8307 
8308 static dladm_status_t
8309 print_bridge_stats(show_state_t *state, datalink_id_t linkid,
8310     bridge_statfields_buf_t *bbuf)
8311 {
8312 	char			link[MAXLINKNAMELEN];
8313 	datalink_class_t	class;
8314 	uint32_t		flags;
8315 	dladm_status_t		status;
8316 	kstat_ctl_t		*kcp;
8317 	kstat_t			*ksp;
8318 	brsum_t			*brsum = (brsum_t *)&state->ls_prevstats;
8319 	brsum_t			newval;
8320 
8321 #ifndef lint
8322 	/* This is a compile-time assertion; optimizer normally fixes this */
8323 	extern void brsum_t_is_too_large(void);
8324 
8325 	if (sizeof (*brsum) > sizeof (state->ls_prevstats))
8326 		brsum_t_is_too_large();
8327 #endif
8328 
8329 	if (state->ls_firstonly) {
8330 		if (state->ls_donefirst)
8331 			return (DLADM_WALK_CONTINUE);
8332 		state->ls_donefirst = B_TRUE;
8333 	} else {
8334 		bzero(brsum, sizeof (*brsum));
8335 	}
8336 	bzero(&newval, sizeof (newval));
8337 
8338 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, &class,
8339 	    NULL, link, sizeof (link))) != DLADM_STATUS_OK)
8340 		return (status);
8341 
8342 	if (!(state->ls_flags & flags))
8343 		return (DLADM_STATUS_NOTFOUND);
8344 
8345 	if ((kcp = kstat_open()) == NULL) {
8346 		warn("kstat open operation failed");
8347 		return (DLADM_STATUS_OK);
8348 	}
8349 	if ((ksp = kstat_lookup(kcp, "bridge", 0, link)) != NULL &&
8350 	    kstat_read(kcp, ksp, NULL) != -1) {
8351 		if (dladm_kstat_value(ksp, "drops", KSTAT_DATA_UINT64,
8352 		    &newval.drops) == DLADM_STATUS_OK) {
8353 			(void) snprintf(bbuf->bridges_drops,
8354 			    sizeof (bbuf->bridges_drops), "%llu",
8355 			    newval.drops - brsum->drops);
8356 		}
8357 		if (dladm_kstat_value(ksp, "forward_direct", KSTAT_DATA_UINT64,
8358 		    &newval.forward_dir) == DLADM_STATUS_OK) {
8359 			(void) snprintf(bbuf->bridges_forwards,
8360 			    sizeof (bbuf->bridges_forwards), "%llu",
8361 			    newval.forward_dir - brsum->forward_dir);
8362 		}
8363 		if (dladm_kstat_value(ksp, "forward_mbcast", KSTAT_DATA_UINT64,
8364 		    &newval.forward_mb) == DLADM_STATUS_OK) {
8365 			(void) snprintf(bbuf->bridges_mbcast,
8366 			    sizeof (bbuf->bridges_mbcast), "%llu",
8367 			    newval.forward_mb - brsum->forward_mb);
8368 		}
8369 		if (dladm_kstat_value(ksp, "forward_unknown", KSTAT_DATA_UINT64,
8370 		    &newval.forward_unk) == DLADM_STATUS_OK) {
8371 			(void) snprintf(bbuf->bridges_unknown,
8372 			    sizeof (bbuf->bridges_unknown), "%llu",
8373 			    newval.forward_unk - brsum->forward_unk);
8374 		}
8375 		if (dladm_kstat_value(ksp, "recv", KSTAT_DATA_UINT64,
8376 		    &newval.recv) == DLADM_STATUS_OK) {
8377 			(void) snprintf(bbuf->bridges_recv,
8378 			    sizeof (bbuf->bridges_recv), "%llu",
8379 			    newval.recv - brsum->recv);
8380 		}
8381 		if (dladm_kstat_value(ksp, "sent", KSTAT_DATA_UINT64,
8382 		    &newval.sent) == DLADM_STATUS_OK) {
8383 			(void) snprintf(bbuf->bridges_sent,
8384 			    sizeof (bbuf->bridges_sent), "%llu",
8385 			    newval.sent - brsum->sent);
8386 		}
8387 	}
8388 	(void) kstat_close(kcp);
8389 
8390 	/* Convert observability node name back to bridge name */
8391 	if (!dladm_observe_to_bridge(link))
8392 		return (DLADM_STATUS_NOTFOUND);
8393 	(void) strlcpy(bbuf->bridges_name, link, sizeof (bbuf->bridges_name));
8394 
8395 	*brsum = newval;
8396 
8397 	return (DLADM_STATUS_OK);
8398 }
8399 
8400 /*
8401  * This structure carries around extra state information for the show-bridge
8402  * command and allows us to use common support functions.
8403  */
8404 typedef struct {
8405 	show_state_t	state;
8406 	boolean_t	show_stats;
8407 	const char	*bridge;
8408 } show_brstate_t;
8409 
8410 /* ARGSUSED */
8411 static int
8412 show_bridge(dladm_handle_t handle, datalink_id_t linkid, void *arg)
8413 {
8414 	show_brstate_t	*brstate = arg;
8415 	void *buf;
8416 
8417 	if (brstate->show_stats) {
8418 		bridge_statfields_buf_t bbuf;
8419 
8420 		bzero(&bbuf, sizeof (bbuf));
8421 		brstate->state.ls_status = print_bridge_stats(&brstate->state,
8422 		    linkid, &bbuf);
8423 		buf = &bbuf;
8424 	} else {
8425 		bridge_fields_buf_t bbuf;
8426 
8427 		bzero(&bbuf, sizeof (bbuf));
8428 		brstate->state.ls_status = print_bridge(&brstate->state, linkid,
8429 		    &bbuf);
8430 		buf = &bbuf;
8431 	}
8432 	if (brstate->state.ls_status == DLADM_STATUS_OK)
8433 		ofmt_print(brstate->state.ls_ofmt, buf);
8434 	return (DLADM_WALK_CONTINUE);
8435 }
8436 
8437 static void
8438 fmt_bool(char *buf, size_t buflen, int val)
8439 {
8440 	(void) strlcpy(buf, val ? "yes" : "no", buflen);
8441 }
8442 
8443 static dladm_status_t
8444 print_bridge_link(show_state_t *state, datalink_id_t linkid,
8445     bridge_link_fields_buf_t *bbuf)
8446 {
8447 	datalink_class_t	class;
8448 	uint32_t		flags;
8449 	dladm_status_t		status;
8450 	UID_STP_PORT_STATE_T	stpstate;
8451 
8452 	status = dladm_datalink_id2info(handle, linkid, &flags, &class, NULL,
8453 	    bbuf->bridgel_link, sizeof (bbuf->bridgel_link));
8454 	if (status != DLADM_STATUS_OK)
8455 		return (status);
8456 
8457 	if (!(state->ls_flags & flags))
8458 		return (DLADM_STATUS_NOTFOUND);
8459 
8460 	if (dladm_bridge_link_state(handle, linkid, &stpstate) ==
8461 	    DLADM_STATUS_OK) {
8462 		(void) snprintf(bbuf->bridgel_index,
8463 		    sizeof (bbuf->bridgel_index), "%u", stpstate.port_no);
8464 		if (dlsym(RTLD_PROBE, "STP_IN_state2str")) {
8465 			(void) strlcpy(bbuf->bridgel_state,
8466 			    STP_IN_state2str(stpstate.state),
8467 			    sizeof (bbuf->bridgel_state));
8468 		} else {
8469 			(void) snprintf(bbuf->bridgel_state,
8470 			    sizeof (bbuf->bridgel_state), "%u",
8471 			    stpstate.state);
8472 		}
8473 		(void) snprintf(bbuf->bridgel_uptime,
8474 		    sizeof (bbuf->bridgel_uptime), "%lu", stpstate.uptime);
8475 		(void) snprintf(bbuf->bridgel_opercost,
8476 		    sizeof (bbuf->bridgel_opercost), "%lu",
8477 		    stpstate.oper_port_path_cost);
8478 		fmt_bool(bbuf->bridgel_operp2p, sizeof (bbuf->bridgel_operp2p),
8479 		    stpstate.oper_point2point);
8480 		fmt_bool(bbuf->bridgel_operedge,
8481 		    sizeof (bbuf->bridgel_operedge), stpstate.oper_edge);
8482 		fmt_bridge_id(bbuf->bridgel_desroot,
8483 		    sizeof (bbuf->bridgel_desroot), &stpstate.designated_root);
8484 		(void) snprintf(bbuf->bridgel_descost,
8485 		    sizeof (bbuf->bridgel_descost), "%lu",
8486 		    stpstate.designated_cost);
8487 		fmt_bridge_id(bbuf->bridgel_desbridge,
8488 		    sizeof (bbuf->bridgel_desbridge),
8489 		    &stpstate.designated_bridge);
8490 		(void) snprintf(bbuf->bridgel_desport,
8491 		    sizeof (bbuf->bridgel_desport), "%u",
8492 		    stpstate.designated_port);
8493 		fmt_bool(bbuf->bridgel_tcack, sizeof (bbuf->bridgel_tcack),
8494 		    stpstate.top_change_ack);
8495 	}
8496 	return (DLADM_STATUS_OK);
8497 }
8498 
8499 static dladm_status_t
8500 print_bridge_link_stats(show_state_t *state, datalink_id_t linkid,
8501     bridge_link_statfields_buf_t *bbuf)
8502 {
8503 	datalink_class_t	class;
8504 	uint32_t		flags;
8505 	dladm_status_t		status;
8506 	UID_STP_PORT_STATE_T	stpstate;
8507 	kstat_ctl_t		*kcp;
8508 	kstat_t			*ksp;
8509 	char			bridge[MAXLINKNAMELEN];
8510 	char			kstatname[MAXLINKNAMELEN*2 + 1];
8511 	brlsum_t		*brlsum = (brlsum_t *)&state->ls_prevstats;
8512 	brlsum_t		newval;
8513 
8514 #ifndef lint
8515 	/* This is a compile-time assertion; optimizer normally fixes this */
8516 	extern void brlsum_t_is_too_large(void);
8517 
8518 	if (sizeof (*brlsum) > sizeof (state->ls_prevstats))
8519 		brlsum_t_is_too_large();
8520 #endif
8521 
8522 	if (state->ls_firstonly) {
8523 		if (state->ls_donefirst)
8524 			return (DLADM_WALK_CONTINUE);
8525 		state->ls_donefirst = B_TRUE;
8526 	} else {
8527 		bzero(brlsum, sizeof (*brlsum));
8528 	}
8529 	bzero(&newval, sizeof (newval));
8530 
8531 	status = dladm_datalink_id2info(handle, linkid, &flags, &class, NULL,
8532 	    bbuf->bridgels_link, sizeof (bbuf->bridgels_link));
8533 	if (status != DLADM_STATUS_OK)
8534 		return (status);
8535 
8536 	if (!(state->ls_flags & flags))
8537 		return (DLADM_STATUS_NOTFOUND);
8538 
8539 	if (dladm_bridge_link_state(handle, linkid, &stpstate) ==
8540 	    DLADM_STATUS_OK) {
8541 		newval.cfgbpdu = stpstate.rx_cfg_bpdu_cnt;
8542 		newval.tcnbpdu = stpstate.rx_tcn_bpdu_cnt;
8543 		newval.rstpbpdu = stpstate.rx_rstp_bpdu_cnt;
8544 		newval.txbpdu = stpstate.txCount;
8545 
8546 		(void) snprintf(bbuf->bridgels_cfgbpdu,
8547 		    sizeof (bbuf->bridgels_cfgbpdu), "%lu",
8548 		    newval.cfgbpdu - brlsum->cfgbpdu);
8549 		(void) snprintf(bbuf->bridgels_tcnbpdu,
8550 		    sizeof (bbuf->bridgels_tcnbpdu), "%lu",
8551 		    newval.tcnbpdu - brlsum->tcnbpdu);
8552 		(void) snprintf(bbuf->bridgels_rstpbpdu,
8553 		    sizeof (bbuf->bridgels_rstpbpdu), "%lu",
8554 		    newval.rstpbpdu - brlsum->rstpbpdu);
8555 		(void) snprintf(bbuf->bridgels_txbpdu,
8556 		    sizeof (bbuf->bridgels_txbpdu), "%lu",
8557 		    newval.txbpdu - brlsum->txbpdu);
8558 	}
8559 
8560 	if ((status = dladm_bridge_getlink(handle, linkid, bridge,
8561 	    sizeof (bridge))) != DLADM_STATUS_OK)
8562 		goto bls_out;
8563 	(void) snprintf(kstatname, sizeof (kstatname), "%s0-%s", bridge,
8564 	    bbuf->bridgels_link);
8565 	if ((kcp = kstat_open()) == NULL) {
8566 		warn("kstat open operation failed");
8567 		goto bls_out;
8568 	}
8569 	if ((ksp = kstat_lookup(kcp, "bridge", 0, kstatname)) != NULL &&
8570 	    kstat_read(kcp, ksp, NULL) != -1) {
8571 		if (dladm_kstat_value(ksp, "drops", KSTAT_DATA_UINT64,
8572 		    &newval.drops) != -1) {
8573 			(void) snprintf(bbuf->bridgels_drops,
8574 			    sizeof (bbuf->bridgels_drops), "%llu",
8575 			    newval.drops - brlsum->drops);
8576 		}
8577 		if (dladm_kstat_value(ksp, "recv", KSTAT_DATA_UINT64,
8578 		    &newval.recv) != -1) {
8579 			(void) snprintf(bbuf->bridgels_recv,
8580 			    sizeof (bbuf->bridgels_recv), "%llu",
8581 			    newval.recv - brlsum->recv);
8582 		}
8583 		if (dladm_kstat_value(ksp, "xmit", KSTAT_DATA_UINT64,
8584 		    &newval.xmit) != -1) {
8585 			(void) snprintf(bbuf->bridgels_xmit,
8586 			    sizeof (bbuf->bridgels_xmit), "%llu",
8587 			    newval.xmit - brlsum->xmit);
8588 		}
8589 	}
8590 	(void) kstat_close(kcp);
8591 bls_out:
8592 	*brlsum = newval;
8593 
8594 	return (status);
8595 }
8596 
8597 static void
8598 show_bridge_link(datalink_id_t linkid, show_brstate_t *brstate)
8599 {
8600 	void *buf;
8601 
8602 	if (brstate->show_stats) {
8603 		bridge_link_statfields_buf_t bbuf;
8604 
8605 		bzero(&bbuf, sizeof (bbuf));
8606 		brstate->state.ls_status = print_bridge_link_stats(
8607 		    &brstate->state, linkid, &bbuf);
8608 		buf = &bbuf;
8609 	} else {
8610 		bridge_link_fields_buf_t bbuf;
8611 
8612 		bzero(&bbuf, sizeof (bbuf));
8613 		brstate->state.ls_status = print_bridge_link(&brstate->state,
8614 		    linkid, &bbuf);
8615 		buf = &bbuf;
8616 	}
8617 	if (brstate->state.ls_status == DLADM_STATUS_OK)
8618 		ofmt_print(brstate->state.ls_ofmt, buf);
8619 }
8620 
8621 /* ARGSUSED */
8622 static int
8623 show_bridge_link_walk(dladm_handle_t handle, datalink_id_t linkid, void *arg)
8624 {
8625 	show_brstate_t	*brstate = arg;
8626 	char bridge[MAXLINKNAMELEN];
8627 
8628 	if (dladm_bridge_getlink(handle, linkid, bridge, sizeof (bridge)) ==
8629 	    DLADM_STATUS_OK && strcmp(bridge, brstate->bridge) == 0) {
8630 		show_bridge_link(linkid, brstate);
8631 	}
8632 	return (DLADM_WALK_CONTINUE);
8633 }
8634 
8635 static void
8636 show_bridge_fwd(dladm_handle_t handle, bridge_listfwd_t *blf,
8637     show_state_t *state)
8638 {
8639 	bridge_fwd_fields_buf_t bbuf;
8640 
8641 	bzero(&bbuf, sizeof (bbuf));
8642 	(void) snprintf(bbuf.bridgef_dest, sizeof (bbuf.bridgef_dest),
8643 	    "%s", ether_ntoa((struct ether_addr *)blf->blf_dest));
8644 	if (blf->blf_is_local) {
8645 		(void) strlcpy(bbuf.bridgef_flags, "L",
8646 		    sizeof (bbuf.bridgef_flags));
8647 	} else {
8648 		(void) snprintf(bbuf.bridgef_age, sizeof (bbuf.bridgef_age),
8649 		    "%2d.%03d", blf->blf_ms_age / 1000, blf->blf_ms_age % 1000);
8650 		if (blf->blf_trill_nick != 0) {
8651 			(void) snprintf(bbuf.bridgef_output,
8652 			    sizeof (bbuf.bridgef_output), "%u",
8653 			    blf->blf_trill_nick);
8654 		}
8655 	}
8656 	if (blf->blf_linkid != DATALINK_INVALID_LINKID &&
8657 	    blf->blf_trill_nick == 0) {
8658 		state->ls_status = dladm_datalink_id2info(handle,
8659 		    blf->blf_linkid, NULL, NULL, NULL, bbuf.bridgef_output,
8660 		    sizeof (bbuf.bridgef_output));
8661 	}
8662 	if (state->ls_status == DLADM_STATUS_OK)
8663 		ofmt_print(state->ls_ofmt, &bbuf);
8664 }
8665 
8666 static void
8667 show_bridge_trillnick(trill_listnick_t *tln, show_state_t *state)
8668 {
8669 	bridge_trill_fields_buf_t bbuf;
8670 
8671 	bzero(&bbuf, sizeof (bbuf));
8672 	(void) snprintf(bbuf.bridget_nick, sizeof (bbuf.bridget_nick),
8673 	    "%u", tln->tln_nick);
8674 	if (tln->tln_ours) {
8675 		(void) strlcpy(bbuf.bridget_flags, "L",
8676 		    sizeof (bbuf.bridget_flags));
8677 	} else {
8678 		state->ls_status = dladm_datalink_id2info(handle,
8679 		    tln->tln_linkid, NULL, NULL, NULL, bbuf.bridget_link,
8680 		    sizeof (bbuf.bridget_link));
8681 		(void) snprintf(bbuf.bridget_nexthop,
8682 		    sizeof (bbuf.bridget_nexthop), "%s",
8683 		    ether_ntoa((struct ether_addr *)tln->tln_nexthop));
8684 	}
8685 	if (state->ls_status == DLADM_STATUS_OK)
8686 		ofmt_print(state->ls_ofmt, &bbuf);
8687 }
8688 
8689 static void
8690 do_show_bridge(int argc, char **argv, const char *use)
8691 {
8692 	int		option;
8693 	enum {
8694 		bridgeMode, linkMode, fwdMode, trillMode
8695 	}		op_mode = bridgeMode;
8696 	uint32_t	flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
8697 	boolean_t	parsable = B_FALSE;
8698 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
8699 	int		interval = 0;
8700 	show_brstate_t	brstate;
8701 	dladm_status_t	status;
8702 	char		*fields_str = NULL;
8703 	/* default: bridge-related data */
8704 	char		*all_fields = "bridge,protect,address,priority,bmaxage,"
8705 	    "bhellotime,bfwddelay,forceproto,tctime,tccount,tchange,"
8706 	    "desroot,rootcost,rootport,maxage,hellotime,fwddelay,holdtime";
8707 	char		*default_fields = "bridge,protect,address,priority,"
8708 	    "desroot";
8709 	char		*all_statfields = "bridge,drops,forwards,mbcast,"
8710 	    "unknown,recv,sent";
8711 	char		*default_statfields = "bridge,drops,forwards,mbcast,"
8712 	    "unknown";
8713 	/* -l: link-related data */
8714 	char		*all_link_fields = "link,index,state,uptime,opercost,"
8715 	    "operp2p,operedge,desroot,descost,desbridge,desport,tcack";
8716 	char		*default_link_fields = "link,state,uptime,desroot";
8717 	char		*all_link_statfields = "link,cfgbpdu,tcnbpdu,rstpbpdu,"
8718 	    "txbpdu,drops,recv,xmit";
8719 	char		*default_link_statfields = "link,drops,recv,xmit";
8720 	/* -f: bridge forwarding table related data */
8721 	char		*default_fwd_fields = "dest,age,flags,output";
8722 	/* -t: TRILL nickname table related data */
8723 	char		*default_trill_fields = "nick,flags,link,nexthop";
8724 	char		*default_str;
8725 	char		*all_str = NULL;
8726 	ofmt_field_t	*field_arr = NULL;
8727 	ofmt_handle_t	ofmt;
8728 	ofmt_status_t	oferr;
8729 	uint_t		ofmtflags = 0;
8730 
8731 	bzero(&brstate, sizeof (brstate));
8732 
8733 	opterr = 0;
8734 	while ((option = getopt_long(argc, argv, ":fi:lo:pst",
8735 	    bridge_show_lopts, NULL)) != -1) {
8736 		switch (option) {
8737 		case 'f':
8738 			if (op_mode != bridgeMode && op_mode != fwdMode)
8739 				die("-f is incompatible with -l or -t");
8740 			op_mode = fwdMode;
8741 			break;
8742 		case 'i':
8743 			if (interval != 0)
8744 				die_optdup(option);
8745 			if (!str2int(optarg, &interval) || interval == 0)
8746 				die("invalid interval value '%s'", optarg);
8747 			break;
8748 		case 'l':
8749 			if (op_mode != bridgeMode && op_mode != linkMode)
8750 				die("-l is incompatible with -f or -t");
8751 			op_mode = linkMode;
8752 			break;
8753 		case 'o':
8754 			fields_str = optarg;
8755 			break;
8756 		case 'p':
8757 			if (parsable)
8758 				die_optdup(option);
8759 			parsable = B_TRUE;
8760 			break;
8761 		case 's':
8762 			if (brstate.show_stats)
8763 				die_optdup(option);
8764 			brstate.show_stats = B_TRUE;
8765 			break;
8766 		case 't':
8767 			if (op_mode != bridgeMode && op_mode != trillMode)
8768 				die("-t is incompatible with -f or -l");
8769 			op_mode = trillMode;
8770 			break;
8771 		default:
8772 			die_opterr(optopt, option, use);
8773 			break;
8774 		}
8775 	}
8776 
8777 	if (interval != 0 && !brstate.show_stats)
8778 		die("the -i option can be used only with -s");
8779 
8780 	if ((op_mode == fwdMode || op_mode == trillMode) && brstate.show_stats)
8781 		die("the -f/-t and -s options cannot be used together");
8782 
8783 	/* get the bridge name (optional last argument) */
8784 	if (optind == (argc-1)) {
8785 		char lname[MAXLINKNAMELEN];
8786 		uint32_t lnkflg;
8787 		datalink_class_t class;
8788 
8789 		brstate.bridge = argv[optind];
8790 		(void) snprintf(lname, sizeof (lname), "%s0", brstate.bridge);
8791 		if ((status = dladm_name2info(handle, lname, &linkid, &lnkflg,
8792 		    &class, NULL)) != DLADM_STATUS_OK) {
8793 			die_dlerr(status, "bridge %s is not valid",
8794 			    brstate.bridge);
8795 		}
8796 
8797 		if (class != DATALINK_CLASS_BRIDGE)
8798 			die("%s is not a bridge", brstate.bridge);
8799 
8800 		if (!(lnkflg & flags)) {
8801 			die_dlerr(DLADM_STATUS_BADARG,
8802 			    "bridge %s is temporarily removed", brstate.bridge);
8803 		}
8804 	} else if (optind != argc) {
8805 		usage();
8806 	} else if (op_mode != bridgeMode) {
8807 		die("bridge name required for -l, -f, or -t");
8808 		return;
8809 	}
8810 
8811 	brstate.state.ls_parsable = parsable;
8812 	brstate.state.ls_flags = flags;
8813 	brstate.state.ls_firstonly = (interval != 0);
8814 
8815 	switch (op_mode) {
8816 	case bridgeMode:
8817 		if (brstate.show_stats) {
8818 			default_str = default_statfields;
8819 			all_str = all_statfields;
8820 			field_arr = bridge_statfields;
8821 		} else {
8822 			default_str = default_fields;
8823 			all_str = all_fields;
8824 			field_arr = bridge_fields;
8825 		}
8826 		break;
8827 
8828 	case linkMode:
8829 		if (brstate.show_stats) {
8830 			default_str = default_link_statfields;
8831 			all_str = all_link_statfields;
8832 			field_arr = bridge_link_statfields;
8833 		} else {
8834 			default_str = default_link_fields;
8835 			all_str = all_link_fields;
8836 			field_arr = bridge_link_fields;
8837 		}
8838 		break;
8839 
8840 	case fwdMode:
8841 		default_str = all_str = default_fwd_fields;
8842 		field_arr = bridge_fwd_fields;
8843 		break;
8844 
8845 	case trillMode:
8846 		default_str = all_str = default_trill_fields;
8847 		field_arr = bridge_trill_fields;
8848 		break;
8849 
8850 	default:
8851 		die("unknown operations mode: %d", op_mode);
8852 	}
8853 
8854 	if (fields_str == NULL)
8855 		fields_str = default_str;
8856 	else if (strcasecmp(fields_str, "all") == 0)
8857 		fields_str = all_str;
8858 
8859 	if (parsable)
8860 		ofmtflags |= OFMT_PARSABLE;
8861 	oferr = ofmt_open(fields_str, field_arr, ofmtflags, 0, &ofmt);
8862 	ofmt_check(oferr, brstate.state.ls_parsable, ofmt, die, warn);
8863 	brstate.state.ls_ofmt = ofmt;
8864 
8865 	for (;;) {
8866 		brstate.state.ls_donefirst = B_FALSE;
8867 		switch (op_mode) {
8868 		case bridgeMode:
8869 			if (linkid == DATALINK_ALL_LINKID) {
8870 				(void) dladm_walk_datalink_id(show_bridge,
8871 				    handle, &brstate, DATALINK_CLASS_BRIDGE,
8872 				    DATALINK_ANY_MEDIATYPE, flags);
8873 			} else {
8874 				(void) show_bridge(handle, linkid, &brstate);
8875 				if (brstate.state.ls_status !=
8876 				    DLADM_STATUS_OK) {
8877 					die_dlerr(brstate.state.ls_status,
8878 					    "failed to show bridge %s",
8879 					    brstate.bridge);
8880 				}
8881 			}
8882 			break;
8883 
8884 		case linkMode: {
8885 			datalink_id_t *dlp;
8886 			uint_t i, nlinks;
8887 
8888 			dlp = dladm_bridge_get_portlist(brstate.bridge,
8889 			    &nlinks);
8890 			if (dlp != NULL) {
8891 				for (i = 0; i < nlinks; i++)
8892 					show_bridge_link(dlp[i], &brstate);
8893 				dladm_bridge_free_portlist(dlp);
8894 			} else if (errno == ENOENT) {
8895 				/* bridge not running; iterate on libdladm */
8896 				(void) dladm_walk_datalink_id(
8897 				    show_bridge_link_walk, handle,
8898 				    &brstate, DATALINK_CLASS_PHYS |
8899 				    DATALINK_CLASS_AGGR |
8900 				    DATALINK_CLASS_ETHERSTUB,
8901 				    DATALINK_ANY_MEDIATYPE, flags);
8902 			} else {
8903 				die("unable to get port list for bridge %s: %s",
8904 				    brstate.bridge, strerror(errno));
8905 			}
8906 			break;
8907 		}
8908 
8909 		case fwdMode: {
8910 			bridge_listfwd_t *blf;
8911 			uint_t i, nfwd;
8912 
8913 			blf = dladm_bridge_get_fwdtable(handle, brstate.bridge,
8914 			    &nfwd);
8915 			if (blf == NULL) {
8916 				die("unable to get forwarding entries for "
8917 				    "bridge %s", brstate.bridge);
8918 			} else {
8919 				for (i = 0; i < nfwd; i++)
8920 					show_bridge_fwd(handle, blf + i,
8921 					    &brstate.state);
8922 				dladm_bridge_free_fwdtable(blf);
8923 			}
8924 			break;
8925 		}
8926 
8927 		case trillMode: {
8928 			trill_listnick_t *tln;
8929 			uint_t i, nnick;
8930 
8931 			tln = dladm_bridge_get_trillnick(brstate.bridge,
8932 			    &nnick);
8933 			if (tln == NULL) {
8934 				if (errno == ENOENT)
8935 					die("bridge %s is not running TRILL",
8936 					    brstate.bridge);
8937 				else
8938 					die("unable to get TRILL nickname "
8939 					    "entries for bridge %s",
8940 					    brstate.bridge);
8941 			} else {
8942 				for (i = 0; i < nnick; i++)
8943 					show_bridge_trillnick(tln + i,
8944 					    &brstate.state);
8945 				dladm_bridge_free_trillnick(tln);
8946 			}
8947 			break;
8948 		}
8949 		}
8950 		if (interval == 0)
8951 			break;
8952 		(void) sleep(interval);
8953 	}
8954 }
8955 
8956 /*
8957  * "-R" option support. It is used for live upgrading. Append dladm commands
8958  * to a upgrade script which will be run when the alternative root boots up:
8959  *
8960  * - If the /etc/dladm/datalink.conf file exists on the alternative root,
8961  * append dladm commands to the <altroot>/var/svc/profile/upgrade_datalink
8962  * script. This script will be run as part of the network/physical service.
8963  * We cannot defer this to /var/svc/profile/upgrade because then the
8964  * configuration will not be able to take effect before network/physical
8965  * plumbs various interfaces.
8966  *
8967  * - If the /etc/dladm/datalink.conf file does not exist on the alternative
8968  * root, append dladm commands to the <altroot>/var/svc/profile/upgrade script,
8969  * which will be run in the manifest-import service.
8970  *
8971  * Note that the SMF team is considering to move the manifest-import service
8972  * to be run at the very begining of boot. Once that is done, the need for
8973  * the /var/svc/profile/upgrade_datalink script will not exist any more.
8974  */
8975 static void
8976 altroot_cmd(char *altroot, int argc, char *argv[])
8977 {
8978 	char		path[MAXPATHLEN];
8979 	struct stat	stbuf;
8980 	FILE		*fp;
8981 	int		i;
8982 
8983 	/*
8984 	 * Check for the existence of the /etc/dladm/datalink.conf
8985 	 * configuration file, and determine the name of script file.
8986 	 */
8987 	(void) snprintf(path, MAXPATHLEN, "/%s/etc/dladm/datalink.conf",
8988 	    altroot);
8989 	if (stat(path, &stbuf) < 0) {
8990 		(void) snprintf(path, MAXPATHLEN, "/%s/%s", altroot,
8991 		    SMF_UPGRADE_FILE);
8992 	} else {
8993 		(void) snprintf(path, MAXPATHLEN, "/%s/%s", altroot,
8994 		    SMF_UPGRADEDATALINK_FILE);
8995 	}
8996 
8997 	if ((fp = fopen(path, "a+")) == NULL)
8998 		die("operation not supported on %s", altroot);
8999 
9000 	(void) fprintf(fp, "/sbin/dladm ");
9001 	for (i = 0; i < argc; i++) {
9002 		/*
9003 		 * Directly write to the file if it is not the "-R <altroot>"
9004 		 * option. In which case, skip it.
9005 		 */
9006 		if (strcmp(argv[i], "-R") != 0)
9007 			(void) fprintf(fp, "%s ", argv[i]);
9008 		else
9009 			i ++;
9010 	}
9011 	(void) fprintf(fp, "%s\n", SMF_DLADM_UPGRADE_MSG);
9012 	(void) fclose(fp);
9013 	dladm_close(handle);
9014 	exit(EXIT_SUCCESS);
9015 }
9016 
9017 /*
9018  * Convert the string to an integer. Note that the string must not have any
9019  * trailing non-integer characters.
9020  */
9021 static boolean_t
9022 str2int(const char *str, int *valp)
9023 {
9024 	int	val;
9025 	char	*endp = NULL;
9026 
9027 	errno = 0;
9028 	val = strtol(str, &endp, 10);
9029 	if (errno != 0 || *endp != '\0')
9030 		return (B_FALSE);
9031 
9032 	*valp = val;
9033 	return (B_TRUE);
9034 }
9035 
9036 /* PRINTFLIKE1 */
9037 static void
9038 warn(const char *format, ...)
9039 {
9040 	va_list alist;
9041 
9042 	format = gettext(format);
9043 	(void) fprintf(stderr, "%s: warning: ", progname);
9044 
9045 	va_start(alist, format);
9046 	(void) vfprintf(stderr, format, alist);
9047 	va_end(alist);
9048 
9049 	(void) putc('\n', stderr);
9050 }
9051 
9052 /* PRINTFLIKE2 */
9053 static void
9054 warn_dlerr(dladm_status_t err, const char *format, ...)
9055 {
9056 	va_list alist;
9057 	char	errmsg[DLADM_STRSIZE];
9058 
9059 	format = gettext(format);
9060 	(void) fprintf(stderr, gettext("%s: warning: "), progname);
9061 
9062 	va_start(alist, format);
9063 	(void) vfprintf(stderr, format, alist);
9064 	va_end(alist);
9065 	(void) fprintf(stderr, ": %s\n", dladm_status2str(err, errmsg));
9066 }
9067 
9068 static void
9069 warn_dlerrlist(dladm_errlist_t *errlist)
9070 {
9071 	if (errlist != NULL && errlist->el_count > 0) {
9072 		int i;
9073 		for (i = 0; i < errlist->el_count; i++) {
9074 			(void) fprintf(stderr, gettext("%s: warning: "),
9075 			    progname);
9076 
9077 			(void) fprintf(stderr, "%s\n",
9078 			    gettext(errlist->el_errs[i]));
9079 		}
9080 	}
9081 }
9082 
9083 /*
9084  * Also closes the dladm handle if it is not NULL.
9085  */
9086 /* PRINTFLIKE2 */
9087 static void
9088 die_dlerr(dladm_status_t err, const char *format, ...)
9089 {
9090 	va_list alist;
9091 	char	errmsg[DLADM_STRSIZE];
9092 
9093 	format = gettext(format);
9094 	(void) fprintf(stderr, "%s: ", progname);
9095 
9096 	va_start(alist, format);
9097 	(void) vfprintf(stderr, format, alist);
9098 	va_end(alist);
9099 	(void) fprintf(stderr, ": %s\n", dladm_status2str(err, errmsg));
9100 
9101 	/* close dladm handle if it was opened */
9102 	if (handle != NULL)
9103 		dladm_close(handle);
9104 
9105 	exit(EXIT_FAILURE);
9106 }
9107 
9108 /*
9109  * Like die_dlerr, but uses the errlist for additional information.
9110  */
9111 /* PRINTFLIKE3 */
9112 static void
9113 die_dlerrlist(dladm_status_t err, dladm_errlist_t *errlist,
9114     const char *format, ...)
9115 {
9116 	va_list	alist;
9117 	char	errmsg[DLADM_STRSIZE];
9118 
9119 	warn_dlerrlist(errlist);
9120 	format = gettext(format);
9121 	(void) fprintf(stderr, "%s: ", progname);
9122 
9123 	va_start(alist, format);
9124 	(void) vfprintf(stderr, format, alist);
9125 	va_end(alist);
9126 	(void) fprintf(stderr, ": %s\n", dladm_status2str(err, errmsg));
9127 
9128 	/* close dladm handle if it was opened */
9129 	if (handle != NULL)
9130 		dladm_close(handle);
9131 
9132 	exit(EXIT_FAILURE);
9133 
9134 }
9135 
9136 /* PRINTFLIKE1 */
9137 static void
9138 die(const char *format, ...)
9139 {
9140 	va_list alist;
9141 
9142 	format = gettext(format);
9143 	(void) fprintf(stderr, "%s: ", progname);
9144 
9145 	va_start(alist, format);
9146 	(void) vfprintf(stderr, format, alist);
9147 	va_end(alist);
9148 
9149 	(void) putc('\n', stderr);
9150 
9151 	/* close dladm handle if it was opened */
9152 	if (handle != NULL)
9153 		dladm_close(handle);
9154 
9155 	exit(EXIT_FAILURE);
9156 }
9157 
9158 static void
9159 die_optdup(int opt)
9160 {
9161 	die("the option -%c cannot be specified more than once", opt);
9162 }
9163 
9164 static void
9165 die_opterr(int opt, int opterr, const char *usage)
9166 {
9167 	switch (opterr) {
9168 	case ':':
9169 		die("option '-%c' requires a value\nusage: %s", opt,
9170 		    gettext(usage));
9171 		break;
9172 	case '?':
9173 	default:
9174 		die("unrecognized option '-%c'\nusage: %s", opt,
9175 		    gettext(usage));
9176 		break;
9177 	}
9178 }
9179 
9180 static void
9181 show_ether_xprop(void *arg, dladm_ether_info_t *eattr)
9182 {
9183 	print_ether_state_t	*statep = arg;
9184 	ether_fields_buf_t	ebuf;
9185 	int			i;
9186 
9187 	for (i = CAPABLE; i <= PEERADV; i++)  {
9188 		bzero(&ebuf, sizeof (ebuf));
9189 		(void) strlcpy(ebuf.eth_ptype, ptype[i],
9190 		    sizeof (ebuf.eth_ptype));
9191 		(void) dladm_ether_autoneg2str(ebuf.eth_autoneg,
9192 		    sizeof (ebuf.eth_autoneg), eattr, i);
9193 		(void) dladm_ether_spdx2str(ebuf.eth_spdx,
9194 		    sizeof (ebuf.eth_spdx), eattr, i);
9195 		(void) dladm_ether_pause2str(ebuf.eth_pause,
9196 		    sizeof (ebuf.eth_pause), eattr, i);
9197 		(void) strlcpy(ebuf.eth_rem_fault,
9198 		    (eattr->lei_attr[i].le_fault ? "fault" : "none"),
9199 		    sizeof (ebuf.eth_rem_fault));
9200 		ofmt_print(statep->es_ofmt, &ebuf);
9201 	}
9202 
9203 }
9204 
9205 static boolean_t
9206 link_is_ether(const char *link, datalink_id_t *linkid)
9207 {
9208 	uint32_t media;
9209 	datalink_class_t class;
9210 
9211 	if (dladm_name2info(handle, link, linkid, NULL, &class, &media) ==
9212 	    DLADM_STATUS_OK) {
9213 		if (class == DATALINK_CLASS_PHYS && media == DL_ETHER)
9214 			return (B_TRUE);
9215 	}
9216 	return (B_FALSE);
9217 }
9218 
9219 /*
9220  * default output callback function that, when invoked,
9221  * prints string which is offset by ofmt_arg->ofmt_id within buf.
9222  */
9223 static boolean_t
9224 print_default_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
9225 {
9226 	char *value;
9227 
9228 	value = (char *)ofarg->ofmt_cbarg + ofarg->ofmt_id;
9229 	(void) strlcpy(buf, value, bufsize);
9230 	return (B_TRUE);
9231 }
9232 
9233 /*
9234  * Called from the walker dladm_walk_datalink_id() for each IB partition to
9235  * display IB partition specific information.
9236  */
9237 static dladm_status_t
9238 print_part(show_part_state_t *state, datalink_id_t linkid)
9239 {
9240 	dladm_part_attr_t	attr;
9241 	dladm_status_t		status;
9242 	dladm_conf_t		conf;
9243 	char			part_over[MAXLINKNAMELEN];
9244 	char			part_name[MAXLINKNAMELEN];
9245 	part_fields_buf_t	pbuf;
9246 	boolean_t		force_in_conf = B_FALSE;
9247 
9248 	/*
9249 	 * Get the information about the IB partition from the partition
9250 	 * datlink ID 'linkid'.
9251 	 */
9252 	if ((status = dladm_part_info(handle, linkid, &attr, state->ps_flags))
9253 	    != DLADM_STATUS_OK)
9254 		return (status);
9255 
9256 	/*
9257 	 * If an IB Phys link name was provided on the command line we have
9258 	 * the Phys link's datalink ID in the ps_over_id field of the state
9259 	 * structure. Proceed only if the IB partition represented by 'linkid'
9260 	 * was created over Phys link denoted by ps_over_id. The
9261 	 * 'dia_physlinkid' field of dladm_part_attr_t represents the IB Phys
9262 	 * link over which the partition was created.
9263 	 */
9264 	if (state->ps_over_id != DATALINK_ALL_LINKID)
9265 		if (state->ps_over_id != attr.dia_physlinkid)
9266 			return (DLADM_STATUS_OK);
9267 
9268 	/*
9269 	 * The linkid argument passed to this function is the datalink ID
9270 	 * of the IB Partition. Get the partitions name from this linkid.
9271 	 */
9272 	if (dladm_datalink_id2info(handle, linkid, NULL, NULL,
9273 	    NULL, part_name, sizeof (part_name)) != DLADM_STATUS_OK)
9274 		return (DLADM_STATUS_BADARG);
9275 
9276 	bzero(part_over, sizeof (part_over));
9277 
9278 	/*
9279 	 * The 'dia_physlinkid' field contains the datalink ID of the IB Phys
9280 	 * link over which the partition was created. Use this linkid to get the
9281 	 * linkover field.
9282 	 */
9283 	if (dladm_datalink_id2info(handle, attr.dia_physlinkid, NULL, NULL,
9284 	    NULL, part_over, sizeof (part_over)) != DLADM_STATUS_OK)
9285 		(void) sprintf(part_over, "?");
9286 	state->ps_found = B_TRUE;
9287 
9288 	/*
9289 	 * Read the FFORCE field from this datalink's persistent configuration
9290 	 * database line to determine if this datalink was created forcibly.
9291 	 * If this datalink is a temporary datalink, then it will not have an
9292 	 * entry in the persistent configuration, so check if force create flag
9293 	 * is set in the partition attributes.
9294 	 *
9295 	 * We need this two level check since persistent partitions brought up
9296 	 * by up-part during boot will have force create flag always set, since
9297 	 * we want up-part to always succeed even if the port is currently down
9298 	 * or P_Key is not yet available in the subnet.
9299 	 */
9300 	if ((status = dladm_getsnap_conf(handle, linkid, &conf)) ==
9301 	    DLADM_STATUS_OK) {
9302 		(void) dladm_get_conf_field(handle, conf, FFORCE,
9303 		    &force_in_conf, sizeof (boolean_t));
9304 		dladm_destroy_conf(handle, conf);
9305 	} else if (status == DLADM_STATUS_NOTFOUND) {
9306 		/*
9307 		 * for a temp link the force create flag will determine
9308 		 * whether it was created with force flag.
9309 		 */
9310 		force_in_conf = ((attr.dia_flags & DLADM_PART_FORCE_CREATE)
9311 		    != 0);
9312 	}
9313 
9314 	(void) snprintf(pbuf.part_link, sizeof (pbuf.part_link),
9315 	    "%s", part_name);
9316 
9317 	(void) snprintf(pbuf.part_over, sizeof (pbuf.part_over),
9318 	    "%s", part_over);
9319 
9320 	(void) snprintf(pbuf.part_pkey, sizeof (pbuf.part_pkey),
9321 	    "%X", attr.dia_pkey);
9322 
9323 	(void) get_linkstate(pbuf.part_link, B_TRUE, pbuf.part_state);
9324 
9325 	(void) snprintf(pbuf.part_flags, sizeof (pbuf.part_flags),
9326 	    "%c----", force_in_conf ? 'f' : '-');
9327 
9328 	ofmt_print(state->ps_ofmt, &pbuf);
9329 
9330 	return (DLADM_STATUS_OK);
9331 }
9332 
9333 /* ARGSUSED */
9334 static int
9335 show_part(dladm_handle_t dh, datalink_id_t linkid, void *arg)
9336 {
9337 	((show_part_state_t *)arg)->ps_status = print_part(arg, linkid);
9338 	return (DLADM_WALK_CONTINUE);
9339 }
9340 
9341 /*
9342  * Show the information about the IB partition objects.
9343  */
9344 static void
9345 do_show_part(int argc, char *argv[], const char *use)
9346 {
9347 	int			option;
9348 	boolean_t		l_arg = B_FALSE;
9349 	uint32_t		flags = DLADM_OPT_ACTIVE;
9350 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
9351 	datalink_id_t		over_linkid = DATALINK_ALL_LINKID;
9352 	char			over_link[MAXLINKNAMELEN];
9353 	show_part_state_t	state;
9354 	dladm_status_t		status;
9355 	boolean_t		o_arg = B_FALSE;
9356 	char			*fields_str = NULL;
9357 	ofmt_handle_t		ofmt;
9358 	ofmt_status_t		oferr;
9359 	uint_t			ofmtflags = 0;
9360 
9361 	bzero(&state, sizeof (state));
9362 	opterr = 0;
9363 	while ((option = getopt_long(argc, argv, ":pPl:o:", show_part_lopts,
9364 	    NULL)) != -1) {
9365 		switch (option) {
9366 		case 'p':
9367 			state.ps_parsable = B_TRUE;
9368 			break;
9369 		case 'P':
9370 			flags = DLADM_OPT_PERSIST;
9371 			break;
9372 		case 'l':
9373 			/*
9374 			 * The data link ID of the IB Phys link. When this
9375 			 * argument is provided we list only the partition
9376 			 * objects created over this IB Phys link.
9377 			 */
9378 			if (strlcpy(over_link, optarg, MAXLINKNAMELEN) >=
9379 			    MAXLINKNAMELEN)
9380 				die("link name too long");
9381 
9382 			l_arg = B_TRUE;
9383 			break;
9384 		case 'o':
9385 			o_arg = B_TRUE;
9386 			fields_str = optarg;
9387 			break;
9388 		default:
9389 			die_opterr(optopt, option, use);
9390 		}
9391 	}
9392 
9393 	/*
9394 	 * Get the partition ID (optional last argument).
9395 	 */
9396 	if (optind == (argc - 1)) {
9397 		status = dladm_name2info(handle, argv[optind], &linkid, NULL,
9398 		    NULL, NULL);
9399 		if (status != DLADM_STATUS_OK) {
9400 			die_dlerr(status, "invalid partition link name '%s'",
9401 			    argv[optind]);
9402 		}
9403 		(void) strlcpy(state.ps_part, argv[optind], MAXLINKNAMELEN);
9404 	} else if (optind != argc) {
9405 		usage();
9406 	}
9407 
9408 	if (state.ps_parsable && !o_arg)
9409 		die("-p requires -o");
9410 
9411 	/*
9412 	 * If an IB Phys link name was provided as an argument, then get its
9413 	 * datalink ID.
9414 	 */
9415 	if (l_arg) {
9416 		status = dladm_name2info(handle, over_link, &over_linkid, NULL,
9417 		    NULL, NULL);
9418 		if (status != DLADM_STATUS_OK) {
9419 			die_dlerr(status, "invalid link name '%s'", over_link);
9420 		}
9421 	}
9422 
9423 	state.ps_over_id = over_linkid; /* IB Phys link ID */
9424 	state.ps_found = B_FALSE;
9425 	state.ps_flags = flags;
9426 
9427 	if (state.ps_parsable)
9428 		ofmtflags |= OFMT_PARSABLE;
9429 	oferr = ofmt_open(fields_str, part_fields, ofmtflags, 0, &ofmt);
9430 	ofmt_check(oferr, state.ps_parsable, ofmt, die, warn);
9431 	state.ps_ofmt = ofmt;
9432 
9433 	/*
9434 	 * If a specific IB partition name was not provided as an argument,
9435 	 * walk all the datalinks and display the information for all
9436 	 * IB partitions. If IB Phys link was provided limit it to only
9437 	 * IB partitions created over that IB Phys link.
9438 	 */
9439 	if (linkid == DATALINK_ALL_LINKID) {
9440 		(void) dladm_walk_datalink_id(show_part, handle, &state,
9441 		    DATALINK_CLASS_PART, DATALINK_ANY_MEDIATYPE, flags);
9442 	} else {
9443 		(void) show_part(handle, linkid, &state);
9444 		if (state.ps_status != DLADM_STATUS_OK) {
9445 			ofmt_close(ofmt);
9446 			die_dlerr(state.ps_status, "failed to show IB partition"
9447 			    " '%s'", state.ps_part);
9448 		}
9449 	}
9450 	ofmt_close(ofmt);
9451 }
9452 
9453 
9454 /*
9455  * Called from the walker dladm_walk_datalink_id() for each IB Phys link to
9456  * display IB specific information for these Phys links.
9457  */
9458 static dladm_status_t
9459 print_ib(show_ib_state_t *state, datalink_id_t phys_linkid)
9460 {
9461 	dladm_ib_attr_t		attr;
9462 	dladm_status_t		status;
9463 	char			linkname[MAXLINKNAMELEN];
9464 	char			pkeystr[MAXPKEYLEN];
9465 	int			i;
9466 	ib_fields_buf_t		ibuf;
9467 
9468 	bzero(&attr, sizeof (attr));
9469 
9470 	/*
9471 	 * Get the attributes of the IB Phys link from active/Persistent config
9472 	 * based on the flag passed.
9473 	 */
9474 	if ((status = dladm_ib_info(handle, phys_linkid, &attr,
9475 	    state->is_flags)) != DLADM_STATUS_OK)
9476 		return (status);
9477 
9478 	if ((state->is_link_id != DATALINK_ALL_LINKID) && (state->is_link_id
9479 	    != attr.dia_physlinkid)) {
9480 		dladm_free_ib_info(&attr);
9481 		return (DLADM_STATUS_OK);
9482 	}
9483 
9484 	/*
9485 	 * Get the data link name for the phys_linkid. If we are doing show-ib
9486 	 * for all IB Phys links, we have only the datalink IDs not the
9487 	 * datalink name.
9488 	 */
9489 	if (dladm_datalink_id2info(handle, phys_linkid, NULL, NULL, NULL,
9490 	    linkname, MAXLINKNAMELEN) != DLADM_STATUS_OK)
9491 		return (status);
9492 
9493 	(void) snprintf(ibuf.ib_link, sizeof (ibuf.ib_link),
9494 	    "%s", linkname);
9495 
9496 	(void) snprintf(ibuf.ib_portnum, sizeof (ibuf.ib_portnum),
9497 	    "%d", attr.dia_portnum);
9498 
9499 	(void) snprintf(ibuf.ib_hcaguid, sizeof (ibuf.ib_hcaguid),
9500 	    "%llX", attr.dia_hca_guid);
9501 
9502 	(void) snprintf(ibuf.ib_portguid, sizeof (ibuf.ib_portguid),
9503 	    "%llX", attr.dia_port_guid);
9504 
9505 	(void) get_linkstate(linkname, B_TRUE, ibuf.ib_state);
9506 
9507 	/*
9508 	 * Create a comma separated list of pkeys from the pkey table returned
9509 	 * by the IP over IB driver instance.
9510 	 */
9511 	bzero(ibuf.ib_pkeys, attr.dia_port_pkey_tbl_sz * sizeof (ib_pkey_t));
9512 	for (i = 0; i < attr.dia_port_pkey_tbl_sz; i++) {
9513 		if (attr.dia_port_pkeys[i] != IB_PKEY_INVALID_FULL &&
9514 		    attr.dia_port_pkeys[i] != IB_PKEY_INVALID_LIMITED) {
9515 			if (i == 0)
9516 				(void) snprintf(pkeystr, MAXPKEYLEN, "%X",
9517 				    attr.dia_port_pkeys[i]);
9518 			else
9519 				(void) snprintf(pkeystr, MAXPKEYLEN, ",%X",
9520 				    attr.dia_port_pkeys[i]);
9521 			(void) strlcat(ibuf.ib_pkeys, pkeystr, MAXPKEYSTRSZ);
9522 		}
9523 	}
9524 
9525 	dladm_free_ib_info(&attr);
9526 
9527 	ofmt_print(state->is_ofmt, &ibuf);
9528 
9529 	return (DLADM_STATUS_OK);
9530 }
9531 
9532 /* ARGSUSED */
9533 static int
9534 show_ib(dladm_handle_t dh, datalink_id_t linkid, void *arg)
9535 {
9536 	((show_ib_state_t *)arg)->is_status = print_ib(arg, linkid);
9537 	return (DLADM_WALK_CONTINUE);
9538 }
9539 
9540 /*
9541  * Show the properties of one/all IB Phys links. This is different from
9542  * show-phys command since this will display IB specific information about the
9543  * Phys link like, HCA GUID, PORT GUID, PKEYS active for this port etc.
9544  */
9545 static void
9546 do_show_ib(int argc, char *argv[], const char *use)
9547 {
9548 	int			option;
9549 	uint32_t		flags = DLADM_OPT_ACTIVE;
9550 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
9551 	show_ib_state_t		state;
9552 	dladm_status_t		status;
9553 	boolean_t		o_arg = B_FALSE;
9554 	char			*fields_str = NULL;
9555 	ofmt_handle_t		ofmt;
9556 	ofmt_status_t		oferr;
9557 	uint_t			ofmtflags = 0;
9558 
9559 	bzero(&state, sizeof (state));
9560 	opterr = 0;
9561 	while ((option = getopt_long(argc, argv, ":po:", show_lopts,
9562 	    NULL)) != -1) {
9563 		switch (option) {
9564 		case 'p':
9565 			state.is_parsable = B_TRUE;
9566 			break;
9567 		case 'o':
9568 			o_arg = B_TRUE;
9569 			fields_str = optarg;
9570 			break;
9571 		default:
9572 			die_opterr(optopt, option, use);
9573 		}
9574 	}
9575 
9576 	/* get IB Phys link ID (optional last argument) */
9577 	if (optind == (argc - 1)) {
9578 		status = dladm_name2info(handle, argv[optind], &linkid, NULL,
9579 		    NULL, NULL);
9580 		if (status != DLADM_STATUS_OK) {
9581 			die_dlerr(status, "invalid IB port name '%s'",
9582 			    argv[optind]);
9583 		}
9584 		(void) strlcpy(state.is_link, argv[optind], MAXLINKNAMELEN);
9585 	} else if (optind != argc) {
9586 		usage();
9587 	}
9588 
9589 	if (state.is_parsable && !o_arg)
9590 		die("-p requires -o");
9591 
9592 	/*
9593 	 * linkid is the data link ID of the IB Phys link. By default it will
9594 	 * be DATALINK_ALL_LINKID.
9595 	 */
9596 	state.is_link_id = linkid;
9597 	state.is_flags = flags;
9598 
9599 	if (state.is_parsable)
9600 		ofmtflags |= OFMT_PARSABLE;
9601 	oferr = ofmt_open(fields_str, ib_fields, ofmtflags, 0, &ofmt);
9602 	ofmt_check(oferr, state.is_parsable, ofmt, die, warn);
9603 	state.is_ofmt = ofmt;
9604 
9605 	/*
9606 	 * If we are going to display the information for all IB Phys links
9607 	 * then we'll walk through all the datalinks for datalinks of Phys
9608 	 * class and media type IB.
9609 	 */
9610 	if (linkid == DATALINK_ALL_LINKID) {
9611 		(void) dladm_walk_datalink_id(show_ib, handle, &state,
9612 		    DATALINK_CLASS_PHYS, DL_IB, flags);
9613 	} else {
9614 		/*
9615 		 * We need to display the information only for the IB phys link
9616 		 * linkid. Call show_ib for this link.
9617 		 */
9618 		(void) show_ib(handle, linkid, &state);
9619 		if (state.is_status != DLADM_STATUS_OK) {
9620 			ofmt_close(ofmt);
9621 			die_dlerr(state.is_status, "failed to show IB Phys link"
9622 			    " '%s'", state.is_link);
9623 		}
9624 	}
9625 	ofmt_close(ofmt);
9626 }
9627 
9628 /*
9629  * Create an IP over Infiniband partition object over an IB Phys link. The IB
9630  * Phys link is associated with an Infiniband HCA port. The IB partition object
9631  * is created over a port, pkey combination. This partition object represents
9632  * an instance of IP over IB interface.
9633  */
9634 /* ARGSUSED */
9635 static void
9636 do_create_part(int argc, char *argv[], const char *use)
9637 {
9638 	int		status, option;
9639 	int		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
9640 	char		*pname;
9641 	char		*l_arg = NULL;
9642 	char		*altroot = NULL;
9643 	datalink_id_t	physlinkid = 0;
9644 	datalink_id_t	partlinkid = 0;
9645 	unsigned long	opt_pkey;
9646 	ib_pkey_t	pkey = 0;
9647 	char		*endp = NULL;
9648 	char		propstr[DLADM_STRSIZE];
9649 	dladm_arg_list_t	*proplist = NULL;
9650 
9651 	propstr[0] = '\0';
9652 	while ((option = getopt_long(argc, argv, ":tfl:P:R:p:",
9653 	    part_lopts, NULL)) != -1) {
9654 		switch (option) {
9655 		case 't':
9656 			/*
9657 			 * Create a temporary IB partition object. This
9658 			 * instance is not entered into the persistent database
9659 			 * so it will not be recreated automatically on a
9660 			 * reboot.
9661 			 */
9662 			flags &= ~DLADM_OPT_PERSIST;
9663 			break;
9664 		case 'l':
9665 			/*
9666 			 * The IB phys link over which the partition object will
9667 			 * be created.
9668 			 */
9669 			l_arg = optarg;
9670 			break;
9671 		case 'R':
9672 			altroot = optarg;
9673 			break;
9674 		case 'p':
9675 			(void) strlcat(propstr, optarg, DLADM_STRSIZE);
9676 			if (strlcat(propstr, ",", DLADM_STRSIZE) >=
9677 			    DLADM_STRSIZE)
9678 				die("property list too long '%s'", propstr);
9679 			break;
9680 		case 'P':
9681 			/*
9682 			 * The P_Key for the port, pkey tuple of the partition
9683 			 * object. This P_Key should exist in the IB subnet.
9684 			 * The partition creation for a non-existent P_Key will
9685 			 * fail unless the -f option is used.
9686 			 *
9687 			 * The P_Key is expected to be a hexadecimal number.
9688 			 */
9689 			opt_pkey = strtoul(optarg, &endp, 16);
9690 			if (errno == ERANGE || opt_pkey > USHRT_MAX ||
9691 			    *endp != '\0')
9692 				die("Invalid pkey");
9693 
9694 			pkey = (ib_pkey_t)opt_pkey;
9695 			break;
9696 		case 'f':
9697 			flags |= DLADM_OPT_FORCE;
9698 			break;
9699 		default:
9700 			die_opterr(optopt, option, use);
9701 			break;
9702 		}
9703 	}
9704 
9705 	/* check required options */
9706 	if (!l_arg)
9707 		usage();
9708 
9709 	/* the partition name is a required operand */
9710 	if (optind != (argc - 1))
9711 		usage();
9712 
9713 	pname = argv[argc - 1];
9714 
9715 	/*
9716 	 * Verify that the partition object's name is in the valid link name
9717 	 * format.
9718 	 */
9719 	if (!dladm_valid_linkname(pname))
9720 		die("Invalid link name '%s'", pname);
9721 
9722 	/* pkey is a mandatory argument */
9723 	if (pkey == 0)
9724 		usage();
9725 
9726 	if (altroot != NULL)
9727 		altroot_cmd(altroot, argc, argv);
9728 
9729 	/*
9730 	 * Get the data link id of the IB Phys link over which we will be
9731 	 * creating partition object.
9732 	 */
9733 	if (dladm_name2info(handle, l_arg,
9734 	    &physlinkid, NULL, NULL, NULL) != DLADM_STATUS_OK)
9735 		die("invalid link name '%s'", l_arg);
9736 
9737 	/*
9738 	 * parse the property list provided with -p option.
9739 	 */
9740 	if (dladm_parse_link_props(propstr, &proplist, B_FALSE)
9741 	    != DLADM_STATUS_OK)
9742 		die("invalid IB partition property");
9743 
9744 	/*
9745 	 * Call the library routine to create the partition object.
9746 	 */
9747 	status = dladm_part_create(handle, physlinkid, pkey, flags, pname,
9748 	    &partlinkid, proplist);
9749 	if (status != DLADM_STATUS_OK)
9750 		die_dlerr(status,
9751 		    "partition %x creation over %s failed", pkey, l_arg);
9752 }
9753 
9754 /*
9755  * Delete an IP over Infiniband partition object. The partition object should
9756  * be unplumbed before attempting the delete.
9757  */
9758 static void
9759 do_delete_part(int argc, char *argv[], const char *use)
9760 {
9761 	int option, flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
9762 	int status;
9763 	char *altroot = NULL;
9764 	datalink_id_t	partid;
9765 
9766 	opterr = 0;
9767 	while ((option = getopt_long(argc, argv, "R:t", part_lopts,
9768 	    NULL)) != -1) {
9769 		switch (option) {
9770 		case 't':
9771 			flags &= ~DLADM_OPT_PERSIST;
9772 			break;
9773 		case 'R':
9774 			altroot = optarg;
9775 			break;
9776 		default:
9777 			die_opterr(optopt, option, use);
9778 		}
9779 	}
9780 
9781 	/* get partition name (required last argument) */
9782 	if (optind != (argc - 1))
9783 		usage();
9784 
9785 	if (altroot != NULL)
9786 		altroot_cmd(altroot, argc, argv);
9787 
9788 	/*
9789 	 * Get the data link id of the partition object given the partition
9790 	 * name.
9791 	 */
9792 	status = dladm_name2info(handle, argv[optind], &partid, NULL, NULL,
9793 	    NULL);
9794 	if (status != DLADM_STATUS_OK)
9795 		die("invalid link name '%s'", argv[optind]);
9796 
9797 	/*
9798 	 * Call the library routine to delete the IB partition. This will
9799 	 * result in the IB partition object and all its resources getting
9800 	 * deleted.
9801 	 */
9802 	status = dladm_part_delete(handle, partid, flags);
9803 	if (status != DLADM_STATUS_OK)
9804 		die_dlerr(status, "%s: partition deletion failed",
9805 		    argv[optind]);
9806 }
9807 
9808 /*
9809  * Bring up all or one IB partition already present in the persistent database
9810  * but not active yet.
9811  *
9812  * This sub-command is used during the system boot up to bring up all IB
9813  * partitions present in the persistent database. This is similar to a
9814  * create partition except that, the partitions are always created even if the
9815  * HCA port is down or P_Key is not present in the IB subnet. This is similar
9816  * to using the 'force' option while creating the partition except that the 'f'
9817  * flag will be set in the flags field only if the create-part for this command
9818  * was called with '-f' option.
9819  */
9820 /* ARGSUSED */
9821 static void
9822 do_up_part(int argc, char *argv[], const char *use)
9823 {
9824 	datalink_id_t	partid = DATALINK_ALL_LINKID;
9825 	dladm_status_t status;
9826 
9827 	/*
9828 	 * If a partition name was passed as an argument, get its data link
9829 	 * id. By default we'll attempt to bring up all IB partition data
9830 	 * links.
9831 	 */
9832 	if (argc == 2) {
9833 		status = dladm_name2info(handle, argv[argc - 1], &partid, NULL,
9834 		    NULL, NULL);
9835 		if (status != DLADM_STATUS_OK)
9836 			return;
9837 	} else if (argc > 2) {
9838 		usage();
9839 	}
9840 
9841 	(void) dladm_part_up(handle, partid, 0);
9842 }
9843 
9844 static void
9845 do_create_overlay(int argc, char *argv[], const char *use)
9846 {
9847 	int			opt;
9848 	char			*encap = NULL, *endp, *search = NULL;
9849 	char			name[MAXLINKNAMELEN];
9850 	dladm_status_t		status;
9851 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
9852 	uint64_t		vid = 0;
9853 	boolean_t		havevid = B_FALSE;
9854 	char			propstr[DLADM_STRSIZE];
9855 	dladm_arg_list_t	*proplist = NULL;
9856 
9857 	bzero(propstr, sizeof (propstr));
9858 	while ((opt = getopt_long(argc, argv, ":te:v:p:s:",
9859 	    overlay_create_lopts, NULL)) != -1) {
9860 		switch (opt) {
9861 		case 'e':
9862 			encap = optarg;
9863 			break;
9864 		case 's':
9865 			search = optarg;
9866 			break;
9867 		case 't':
9868 			flags &= ~DLADM_OPT_PERSIST;
9869 			break;
9870 		case 'p':
9871 			(void) strlcat(propstr, optarg, DLADM_STRSIZE);
9872 			if (strlcat(propstr, ",", DLADM_STRSIZE) >=
9873 			    DLADM_STRSIZE)
9874 				die("property list too long '%s'", propstr);
9875 			break;
9876 		case 'v':
9877 			vid = strtoul(optarg, &endp, 10);
9878 			if (*endp != '\0' || (vid == 0 && errno == EINVAL))
9879 				die("couldn't parse virtual networkd id: %s",
9880 				    optarg);
9881 			if (vid == ULONG_MAX && errno == ERANGE)
9882 				die("virtual networkd id too large: %s",
9883 				    optarg);
9884 			havevid = B_TRUE;
9885 			break;
9886 		default:
9887 			die_opterr(optopt, opt, use);
9888 		}
9889 	}
9890 
9891 	if (havevid == B_FALSE)
9892 		die("missing required virtual network id");
9893 
9894 	if (encap == NULL)
9895 		die("missing required encapsulation plugin");
9896 
9897 	if (search == NULL)
9898 		die("missing required search plugin");
9899 
9900 	if (optind != (argc - 1))
9901 		die("missing device name");
9902 
9903 	if (strlcpy(name, argv[optind], MAXLINKNAMELEN) >= MAXLINKNAMELEN)
9904 		die("link name too long '%s'", argv[optind]);
9905 
9906 	if (!dladm_valid_linkname(name))
9907 		die("invalid link name '%s'", argv[optind]);
9908 
9909 	if (strlen(encap) + 1 > MAXLINKNAMELEN)
9910 		die("encapsulation plugin name too long '%s'", encap);
9911 
9912 	if (strlen(search) + 1 > MAXLINKNAMELEN)
9913 		die("search plugin name too long '%s'", encap);
9914 
9915 	if (dladm_parse_link_props(propstr, &proplist, B_FALSE)
9916 	    != DLADM_STATUS_OK)
9917 		die("invalid overlay property");
9918 
9919 	status = dladm_overlay_create(handle, name, encap, search, vid,
9920 	    proplist, &errlist, flags);
9921 	dladm_free_props(proplist);
9922 	if (status != DLADM_STATUS_OK) {
9923 		die_dlerrlist(status, &errlist, "overlay creation failed");
9924 	}
9925 }
9926 
9927 /* ARGSUSED */
9928 static void
9929 do_delete_overlay(int argc, char *argv[], const char *use)
9930 {
9931 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
9932 	dladm_status_t	status;
9933 	uint32_t flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
9934 	int option;
9935 
9936 	opterr = 0;
9937 	while ((option = getopt_long(argc, argv, ":t", lopts,
9938 	    NULL)) != -1) {
9939 		switch (option) {
9940 		case 't':
9941 			flags &= ~DLADM_OPT_PERSIST;
9942 			break;
9943 		default:
9944 			die_opterr(optopt, option, use);
9945 		}
9946 	}
9947 
9948 	/* get overlay name (required last argument) */
9949 	if (optind != (argc - 1))
9950 		usage();
9951 
9952 	status = dladm_name2info(handle, argv[optind], &linkid,
9953 	    NULL, NULL, NULL);
9954 	if (status != DLADM_STATUS_OK)
9955 		die_dlerr(status, "failed to delete %s", argv[optind]);
9956 
9957 	status = dladm_overlay_delete(handle, linkid, flags);
9958 	if (status != DLADM_STATUS_OK)
9959 		die_dlerr(status, "failed to delete %s", argv[optind]);
9960 }
9961 
9962 typedef struct showoverlay_state {
9963 	ofmt_handle_t		sho_ofmt;
9964 	const char		*sho_linkname;
9965 	dladm_overlay_propinfo_handle_t sho_info;
9966 	uint8_t			sho_value[DLADM_OVERLAY_PROP_SIZEMAX];
9967 	uint32_t		sho_size;
9968 } showoverlay_state_t;
9969 
9970 typedef struct showoverlay_fma_state {
9971 	ofmt_handle_t		shof_ofmt;
9972 	const char		*shof_linkname;
9973 	dladm_overlay_status_t	*shof_status;
9974 } showoverlay_fma_state_t;
9975 
9976 typedef struct showoverlay_targ_state {
9977 	ofmt_handle_t			shot_ofmt;
9978 	const char			*shot_linkname;
9979 	const struct ether_addr		*shot_key;
9980 	const dladm_overlay_point_t	*shot_point;
9981 } showoverlay_targ_state_t;
9982 
9983 static void
9984 print_overlay_value(char *outbuf, uint_t bufsize, uint_t type, const void *pbuf,
9985     const size_t psize)
9986 {
9987 	const struct in6_addr *ipv6;
9988 	struct in_addr ip;
9989 
9990 	switch (type) {
9991 	case OVERLAY_PROP_T_INT:
9992 		if (psize != 1 && psize != 2 && psize != 4 && psize != 8) {
9993 			(void) snprintf(outbuf, bufsize, "?");
9994 			break;
9995 		}
9996 		if (psize == 1)
9997 			(void) snprintf(outbuf, bufsize, "%d", *(int8_t *)pbuf);
9998 		if (psize == 2)
9999 			(void) snprintf(outbuf, bufsize, "%d",
10000 			    *(int16_t *)pbuf);
10001 		if (psize == 4)
10002 			(void) snprintf(outbuf, bufsize, "%d",
10003 			    *(int32_t *)pbuf);
10004 		if (psize == 8)
10005 			(void) snprintf(outbuf, bufsize, "%d",
10006 			    *(int64_t *)pbuf);
10007 		break;
10008 	case OVERLAY_PROP_T_UINT:
10009 		if (psize != 1 && psize != 2 && psize != 4 && psize != 8) {
10010 			(void) snprintf(outbuf, bufsize, "?");
10011 			break;
10012 		}
10013 		if (psize == 1)
10014 			(void) snprintf(outbuf, bufsize, "%d",
10015 			    *(uint8_t *)pbuf);
10016 		if (psize == 2)
10017 			(void) snprintf(outbuf, bufsize, "%d",
10018 			    *(uint16_t *)pbuf);
10019 		if (psize == 4)
10020 			(void) snprintf(outbuf, bufsize, "%d",
10021 			    *(uint32_t *)pbuf);
10022 		if (psize == 8)
10023 			(void) snprintf(outbuf, bufsize, "%d",
10024 			    *(uint64_t *)pbuf);
10025 		break;
10026 	case OVERLAY_PROP_T_IP:
10027 		if (psize != sizeof (struct in6_addr)) {
10028 			warn("malformed overlay IP property: %d bytes\n",
10029 			    psize);
10030 			(void) snprintf(outbuf, bufsize, "--");
10031 			break;
10032 		}
10033 
10034 		ipv6 = pbuf;
10035 		if (IN6_IS_ADDR_V4MAPPED(ipv6)) {
10036 			IN6_V4MAPPED_TO_INADDR(ipv6, &ip);
10037 			if (inet_ntop(AF_INET, &ip, outbuf, bufsize) == NULL) {
10038 				warn("malformed overlay IP property\n");
10039 				(void) snprintf(outbuf, bufsize, "--");
10040 				break;
10041 			}
10042 		} else {
10043 			if (inet_ntop(AF_INET6, ipv6, outbuf, bufsize) ==
10044 			    NULL) {
10045 				warn("malformed overlay IP property\n");
10046 				(void) snprintf(outbuf, bufsize, "--");
10047 				break;
10048 			}
10049 		}
10050 
10051 		break;
10052 	case OVERLAY_PROP_T_STRING:
10053 		(void) snprintf(outbuf, bufsize, "%s", pbuf);
10054 		break;
10055 	default:
10056 		abort();
10057 	}
10058 
10059 	return;
10060 
10061 }
10062 
10063 static boolean_t
10064 print_overlay_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
10065 {
10066 	dladm_status_t			status;
10067 	showoverlay_state_t		*sp = ofarg->ofmt_cbarg;
10068 	dladm_overlay_propinfo_handle_t	infop = sp->sho_info;
10069 	const char			*pname;
10070 	uint_t				type, prot;
10071 	const void			*def;
10072 	uint32_t			defsize;
10073 	const mac_propval_range_t	*rangep;
10074 
10075 	if ((status = dladm_overlay_prop_info(infop, &pname, &type, &prot, &def,
10076 	    &defsize, &rangep)) != DLADM_STATUS_OK) {
10077 		warn_dlerr(status, "failed to get get property info");
10078 		return (B_TRUE);
10079 	}
10080 
10081 	switch (ofarg->ofmt_id) {
10082 	case OVERLAY_LINK:
10083 		(void) snprintf(buf, bufsize, "%s", sp->sho_linkname);
10084 		break;
10085 	case OVERLAY_PROPERTY:
10086 		(void) snprintf(buf, bufsize, "%s", pname);
10087 		break;
10088 	case OVERLAY_PERM:
10089 		if ((prot & OVERLAY_PROP_PERM_RW) == OVERLAY_PROP_PERM_RW) {
10090 			(void) snprintf(buf, bufsize, "%s", "rw");
10091 		} else if ((prot & OVERLAY_PROP_PERM_RW) ==
10092 		    OVERLAY_PROP_PERM_READ) {
10093 			(void) snprintf(buf, bufsize, "%s", "r-");
10094 		} else {
10095 			(void) snprintf(buf, bufsize, "%s", "--");
10096 		}
10097 		break;
10098 	case OVERLAY_REQ:
10099 		(void) snprintf(buf, bufsize, "%s",
10100 		    prot & OVERLAY_PROP_PERM_REQ ? "y" : "-");
10101 		break;
10102 	case OVERLAY_VALUE:
10103 		if (sp->sho_size == 0) {
10104 			(void) snprintf(buf, bufsize, "%s", "--");
10105 		} else {
10106 			print_overlay_value(buf, bufsize, type, sp->sho_value,
10107 			    sp->sho_size);
10108 		}
10109 		break;
10110 	case OVERLAY_DEFAULT:
10111 		if (defsize == 0) {
10112 			(void) snprintf(buf, bufsize, "%s", "--");
10113 		} else {
10114 			print_overlay_value(buf, bufsize, type, def, defsize);
10115 		}
10116 		break;
10117 	case OVERLAY_POSSIBLE: {
10118 		int i;
10119 		char **vals, *ptr, *lim;
10120 		if (rangep->mpr_count == 0) {
10121 			(void) snprintf(buf, bufsize, "%s", "--");
10122 			break;
10123 		}
10124 
10125 		vals = malloc((sizeof (char *) + DLADM_PROP_VAL_MAX) *
10126 		    rangep->mpr_count);
10127 		if (vals == NULL)
10128 			die("insufficient memory");
10129 		for (i = 0; i < rangep->mpr_count; i++) {
10130 			vals[i] = (char *)vals + sizeof (char *) *
10131 			    rangep->mpr_count + i * DLADM_MAX_PROP_VALCNT;
10132 		}
10133 
10134 		if (dladm_range2strs(rangep, vals) != 0) {
10135 			free(vals);
10136 			(void) snprintf(buf, bufsize, "%s", "?");
10137 			break;
10138 		}
10139 
10140 		ptr = buf;
10141 		lim = buf + bufsize;
10142 		for (i = 0; i < rangep->mpr_count; i++) {
10143 			ptr += snprintf(ptr, lim - ptr, "%s,", vals[i]);
10144 			if (ptr >= lim)
10145 				break;
10146 		}
10147 		if (rangep->mpr_count > 0)
10148 			buf[strlen(buf) - 1] = '\0';
10149 		free(vals);
10150 		break;
10151 	}
10152 	default:
10153 		abort();
10154 	}
10155 	return (B_TRUE);
10156 }
10157 
10158 static int
10159 dladm_overlay_show_one(dladm_handle_t handle, datalink_id_t linkid,
10160     dladm_overlay_propinfo_handle_t phdl, void *arg)
10161 {
10162 	showoverlay_state_t *sp = arg;
10163 	sp->sho_info = phdl;
10164 
10165 	sp->sho_size = sizeof (sp->sho_value);
10166 	if (dladm_overlay_get_prop(handle, linkid, phdl, &sp->sho_value,
10167 	    &sp->sho_size) != DLADM_STATUS_OK)
10168 		return (DLADM_WALK_CONTINUE);
10169 
10170 	ofmt_print(sp->sho_ofmt, sp);
10171 	return (DLADM_WALK_CONTINUE);
10172 }
10173 
10174 static int
10175 show_one_overlay(dladm_handle_t hdl, datalink_id_t linkid, void *arg)
10176 {
10177 	char			buf[MAXLINKNAMELEN];
10178 	dladm_status_t		info_status;
10179 	showoverlay_state_t	state;
10180 	datalink_class_t	class;
10181 	show_overlay_request_t	*req = arg;
10182 
10183 	if ((info_status = dladm_datalink_id2info(hdl, linkid, NULL, &class,
10184 	    NULL, buf, MAXLINKNAMELEN)) != DLADM_STATUS_OK) {
10185 		warn_dlerr(info_status, "failed to get info for "
10186 		    "datalink id %u", linkid);
10187 		req->sor_failed = B_TRUE;
10188 		return (DLADM_WALK_CONTINUE);
10189 	}
10190 
10191 	if (class != DATALINK_CLASS_OVERLAY) {
10192 		warn("%s is not an overlay", buf);
10193 		req->sor_failed = B_TRUE;
10194 		return (DLADM_WALK_CONTINUE);
10195 	}
10196 
10197 	state.sho_linkname = buf;
10198 	state.sho_ofmt = req->sor_ofmt;
10199 
10200 	dladm_errlist_reset(&errlist);
10201 	(void) dladm_overlay_walk_prop(handle, linkid, dladm_overlay_show_one,
10202 	    &state, &errlist);
10203 	warn_dlerrlist(&errlist);
10204 	if (errlist.el_count) {
10205 		req->sor_failed = B_TRUE;
10206 	}
10207 
10208 	return (DLADM_WALK_CONTINUE);
10209 }
10210 
10211 static boolean_t
10212 print_overlay_targ_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
10213 {
10214 	char				keybuf[ETHERADDRSTRL];
10215 	const showoverlay_targ_state_t	*shot = ofarg->ofmt_cbarg;
10216 	const dladm_overlay_point_t	*point = shot->shot_point;
10217 	char				macbuf[ETHERADDRSTRL];
10218 	char				ipbuf[INET6_ADDRSTRLEN];
10219 	custr_t				*cus;
10220 
10221 	switch (ofarg->ofmt_id) {
10222 	case OVERLAY_TARG_LINK:
10223 		(void) snprintf(buf, bufsize, shot->shot_linkname);
10224 		break;
10225 	case OVERLAY_TARG_TARGET:
10226 		if ((point->dop_flags & DLADM_OVERLAY_F_DEFAULT) != 0) {
10227 			(void) snprintf(buf, bufsize, "*:*:*:*:*:*");
10228 		} else {
10229 			if (ether_ntoa_r(shot->shot_key, keybuf) == NULL) {
10230 				warn("encountered malformed mac address key\n");
10231 				return (B_FALSE);
10232 			}
10233 			(void) snprintf(buf, bufsize, "%s", keybuf);
10234 		}
10235 		break;
10236 	case OVERLAY_TARG_DEST:
10237 		if (custr_alloc_buf(&cus, buf, bufsize) != 0) {
10238 			die("ran out of memory for printing the overlay "
10239 			    "target destination");
10240 		}
10241 
10242 		if (point->dop_dest & OVERLAY_PLUGIN_D_ETHERNET) {
10243 			if (ether_ntoa_r(&point->dop_mac, macbuf) == NULL) {
10244 				warn("encountered malformed mac address target "
10245 				    "for key %s\n", keybuf);
10246 				return (B_FALSE);
10247 			}
10248 			(void) custr_append(cus, macbuf);
10249 		}
10250 
10251 		if (point->dop_dest & OVERLAY_PLUGIN_D_IP) {
10252 			if (IN6_IS_ADDR_V4MAPPED(&point->dop_ip)) {
10253 				struct in_addr v4;
10254 				IN6_V4MAPPED_TO_INADDR(&point->dop_ip, &v4);
10255 				if (inet_ntop(AF_INET, &v4, ipbuf,
10256 				    sizeof (ipbuf)) == NULL)
10257 					abort();
10258 			} else if (inet_ntop(AF_INET6, &point->dop_ip, ipbuf,
10259 			    sizeof (ipbuf)) == NULL) {
10260 				/*
10261 				 * The only failures we should get are
10262 				 * EAFNOSUPPORT and ENOSPC because of buffer
10263 				 * exhaustion. In either of these cases, that
10264 				 * means something has gone horribly wrong.
10265 				 */
10266 				abort();
10267 			}
10268 			if (point->dop_dest & OVERLAY_PLUGIN_D_ETHERNET)
10269 				(void) custr_appendc(cus, ',');
10270 			(void) custr_append(cus, ipbuf);
10271 		}
10272 
10273 		if (point->dop_dest & OVERLAY_PLUGIN_D_PORT) {
10274 			if (point->dop_dest & OVERLAY_PLUGIN_D_IP)
10275 				(void) custr_appendc(cus, ':');
10276 			else if (point->dop_dest & OVERLAY_PLUGIN_D_ETHERNET)
10277 				(void) custr_appendc(cus, ',');
10278 			(void) custr_append_printf(cus, "%u", point->dop_port);
10279 		}
10280 
10281 		custr_free(cus);
10282 
10283 		break;
10284 	}
10285 	return (B_TRUE);
10286 }
10287 
10288 /* ARGSUSED */
10289 static int
10290 show_one_overlay_table_entry(dladm_handle_t handle, datalink_id_t linkid,
10291     const struct ether_addr *key, const dladm_overlay_point_t *point, void *arg)
10292 {
10293 	showoverlay_targ_state_t	*shot = arg;
10294 
10295 	shot->shot_key = key;
10296 	shot->shot_point = point;
10297 	ofmt_print(shot->shot_ofmt, shot);
10298 
10299 	return (DLADM_WALK_CONTINUE);
10300 }
10301 
10302 /* ARGSUSED */
10303 static int
10304 show_one_overlay_table(dladm_handle_t handle, datalink_id_t linkid, void *arg)
10305 {
10306 	char				linkbuf[MAXLINKNAMELEN];
10307 	dladm_status_t			info_status;
10308 	showoverlay_targ_state_t	shot;
10309 	datalink_class_t		class;
10310 	show_overlay_request_t		*req = arg;
10311 
10312 	if ((info_status = dladm_datalink_id2info(handle, linkid, NULL, &class,
10313 	    NULL, linkbuf, MAXLINKNAMELEN)) != DLADM_STATUS_OK) {
10314 		warn_dlerr(info_status, "failed to get info for "
10315 		    "datalink id %u", linkid);
10316 		req->sor_failed = B_TRUE;
10317 		return (DLADM_WALK_CONTINUE);
10318 	}
10319 
10320 	if (class != DATALINK_CLASS_OVERLAY) {
10321 		warn("%s is not an overlay", linkbuf);
10322 		req->sor_failed = B_TRUE;
10323 		return (DLADM_WALK_CONTINUE);
10324 	}
10325 
10326 	shot.shot_ofmt = req->sor_ofmt;
10327 	shot.shot_linkname = linkbuf;
10328 
10329 	(void) dladm_overlay_walk_cache(handle, linkid,
10330 	    show_one_overlay_table_entry, &shot);
10331 
10332 	return (DLADM_WALK_CONTINUE);
10333 }
10334 
10335 static boolean_t
10336 print_overlay_fma_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
10337 {
10338 	showoverlay_fma_state_t	*shof = ofarg->ofmt_cbarg;
10339 	dladm_overlay_status_t	*st = shof->shof_status;
10340 
10341 	switch (ofarg->ofmt_id) {
10342 	case OVERLAY_FMA_LINK:
10343 		(void) snprintf(buf, bufsize, "%s", shof->shof_linkname);
10344 		break;
10345 	case OVERLAY_FMA_STATUS:
10346 		(void) snprintf(buf, bufsize, st->dos_degraded == B_TRUE ?
10347 		    "DEGRADED": "ONLINE");
10348 		break;
10349 	case OVERLAY_FMA_DETAILS:
10350 		(void) snprintf(buf, bufsize, "%s", st->dos_degraded == B_TRUE ?
10351 		    st->dos_fmamsg : "-");
10352 		break;
10353 	default:
10354 		abort();
10355 	}
10356 	return (B_TRUE);
10357 }
10358 
10359 /* ARGSUSED */
10360 static void
10361 show_one_overlay_fma_cb(dladm_handle_t handle, datalink_id_t linkid,
10362     dladm_overlay_status_t *stat, void *arg)
10363 {
10364 	showoverlay_fma_state_t	*shof = arg;
10365 	shof->shof_status = stat;
10366 	ofmt_print(shof->shof_ofmt, shof);
10367 }
10368 
10369 
10370 static int
10371 show_one_overlay_fma(dladm_handle_t handle, datalink_id_t linkid, void *arg)
10372 {
10373 	dladm_status_t		status;
10374 	char			linkbuf[MAXLINKNAMELEN];
10375 	datalink_class_t	class;
10376 	showoverlay_fma_state_t	shof;
10377 	show_overlay_request_t	*req = arg;
10378 
10379 	if (dladm_datalink_id2info(handle, linkid, NULL, &class, NULL, linkbuf,
10380 	    MAXLINKNAMELEN) != DLADM_STATUS_OK ||
10381 	    class != DATALINK_CLASS_OVERLAY) {
10382 		die("datalink %s is not an overlay device\n", linkbuf);
10383 	}
10384 
10385 	shof.shof_ofmt = req->sor_ofmt;
10386 	shof.shof_linkname = linkbuf;
10387 
10388 	status = dladm_overlay_status(handle, linkid,
10389 	    show_one_overlay_fma_cb, &shof);
10390 	if (status != DLADM_STATUS_OK)
10391 		die_dlerr(status, "failed to obtain device status for %s",
10392 		    linkbuf);
10393 
10394 	return (DLADM_WALK_CONTINUE);
10395 }
10396 
10397 static void
10398 do_show_overlay(int argc, char *argv[], const char *use)
10399 {
10400 	int			i, opt;
10401 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
10402 	dladm_status_t		status;
10403 	int			(*funcp)(dladm_handle_t, datalink_id_t, void *);
10404 	char			*fields_str = NULL;
10405 	const ofmt_field_t	*fieldsp;
10406 	ofmt_status_t		oferr;
10407 	boolean_t		parse;
10408 	show_overlay_request_t	req;
10409 	uint_t			ofmtflags;
10410 	int			err;
10411 
10412 	funcp = show_one_overlay;
10413 	fieldsp = overlay_fields;
10414 	parse = B_FALSE;
10415 	req.sor_failed = B_FALSE;
10416 	ofmtflags = OFMT_WRAP;
10417 	while ((opt = getopt_long(argc, argv, ":o:pft", overlay_show_lopts,
10418 	    NULL)) != -1) {
10419 		switch (opt) {
10420 		case 'f':
10421 			funcp = show_one_overlay_fma;
10422 			fieldsp = overlay_fma_fields;
10423 			break;
10424 		case 'o':
10425 			fields_str = optarg;
10426 			break;
10427 		case 'p':
10428 			parse = B_TRUE;
10429 			ofmtflags = OFMT_PARSABLE;
10430 			break;
10431 		case 't':
10432 			funcp = show_one_overlay_table;
10433 			fieldsp = overlay_targ_fields;
10434 			break;
10435 		default:
10436 			die_opterr(optopt, opt, use);
10437 		}
10438 	}
10439 
10440 	if (fields_str != NULL && strcasecmp(fields_str, "all") == 0)
10441 		fields_str = NULL;
10442 
10443 	oferr = ofmt_open(fields_str, fieldsp, ofmtflags, 0, &req.sor_ofmt);
10444 	ofmt_check(oferr, parse, req.sor_ofmt, die, warn);
10445 
10446 	err = 0;
10447 	if (argc > optind) {
10448 		for (i = optind; i < argc; i++) {
10449 			status = dladm_name2info(handle, argv[i], &linkid,
10450 			    NULL, NULL, NULL);
10451 			if (status != DLADM_STATUS_OK) {
10452 				warn_dlerr(status, "failed to find %s",
10453 				    argv[i]);
10454 				err = 1;
10455 				continue;
10456 			}
10457 			(void) funcp(handle, linkid, &req);
10458 		}
10459 	} else {
10460 		(void) dladm_walk_datalink_id(funcp, handle, &req,
10461 		    DATALINK_CLASS_OVERLAY, DATALINK_ANY_MEDIATYPE,
10462 		    DLADM_OPT_ACTIVE);
10463 	}
10464 	if (req.sor_failed) {
10465 		err = 1;
10466 	}
10467 	ofmt_close(req.sor_ofmt);
10468 
10469 	exit(err);
10470 }
10471 
10472 static void
10473 do_modify_overlay(int argc, char *argv[], const char *use)
10474 {
10475 	int			opt, ocnt = 0;
10476 	boolean_t		flush, set, delete;
10477 	struct ether_addr	e;
10478 	char			*dest = NULL;
10479 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
10480 	dladm_status_t		status;
10481 
10482 	flush = set = delete = B_FALSE;
10483 	while ((opt = getopt_long(argc, argv, ":fd:s:", overlay_modify_lopts,
10484 	    NULL)) != -1) {
10485 		switch (opt) {
10486 		case 'd':
10487 			if (delete == B_TRUE)
10488 				die_optdup('d');
10489 			delete = B_TRUE;
10490 			ocnt++;
10491 			if (ether_aton_r(optarg, &e) == NULL)
10492 				die("invalid mac address: %s\n", optarg);
10493 			break;
10494 		case 'f':
10495 			if (flush == B_TRUE)
10496 				die_optdup('f');
10497 			flush = B_TRUE;
10498 			ocnt++;
10499 			break;
10500 		case 's':
10501 			if (set == B_TRUE)
10502 				die_optdup('s');
10503 			set = B_TRUE;
10504 			ocnt++;
10505 			dest = strchr(optarg, '=');
10506 			*dest = '\0';
10507 			dest++;
10508 			if (dest == NULL)
10509 				die("malformed value, expected mac=dest, "
10510 				    "got: %s\n", optarg);
10511 			if (ether_aton_r(optarg, &e) == NULL)
10512 				die("invalid mac address: %s\n", optarg);
10513 			break;
10514 		default:
10515 			die_opterr(optopt, opt, use);
10516 		}
10517 	}
10518 
10519 	if (ocnt == 0)
10520 		die("need to specify one of -d, -f, or -s");
10521 	if (ocnt > 1)
10522 		die("only one of -d, -f, or -s may be used");
10523 
10524 	if (argv[optind] == NULL)
10525 		die("missing required overlay device\n");
10526 	if (argc > optind + 1)
10527 		die("only one overlay device may be specified\n");
10528 
10529 	status = dladm_name2info(handle, argv[optind], &linkid, NULL, NULL,
10530 	    NULL);
10531 	if (status != DLADM_STATUS_OK) {
10532 		die_dlerr(status, "failed to find overlay %s", argv[optind]);
10533 	}
10534 
10535 	if (flush == B_TRUE) {
10536 		status = dladm_overlay_cache_flush(handle, linkid);
10537 		if (status != DLADM_STATUS_OK)
10538 			die_dlerr(status, "failed to flush target cache for "
10539 			    "overlay %s", argv[optind]);
10540 	}
10541 
10542 	if (delete == B_TRUE) {
10543 		status = dladm_overlay_cache_delete(handle, linkid, &e);
10544 		if (status != DLADM_STATUS_OK)
10545 			die_dlerr(status, "failed to flush target %s from "
10546 			    "overlay target cache %s", optarg, argv[optind]);
10547 	}
10548 
10549 	if (set == B_TRUE) {
10550 		status = dladm_overlay_cache_set(handle, linkid, &e, dest);
10551 		if (status != DLADM_STATUS_OK)
10552 			die_dlerr(status, "failed to set target %s for overlay "
10553 			    "target cache %s", optarg, argv[optind]);
10554 	}
10555 
10556 }
10557 
10558 static void
10559 do_up_overlay(int argc, char *argv[], const char *use)
10560 {
10561 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
10562 	dladm_status_t	status;
10563 
10564 	/*
10565 	 * get the id or the name of the overlay (optional last argument)
10566 	 */
10567 	if (argc == 2) {
10568 		status = dladm_name2info(handle, argv[1], &linkid, NULL, NULL,
10569 		    NULL);
10570 		if (status != DLADM_STATUS_OK)
10571 			goto done;
10572 	} else if (argc > 2) {
10573 		usage();
10574 	}
10575 
10576 	status = dladm_overlay_up(handle, linkid, &errlist);
10577 
10578 done:
10579 	if (status != DLADM_STATUS_OK) {
10580 		if (argc == 2) {
10581 			die_dlerrlist(status, &errlist,
10582 			    "could not bring up overlay '%s'", argv[1]);
10583 		} else {
10584 			die_dlerrlist(status, &errlist,
10585 			    "could not bring overlays up");
10586 		}
10587 	}
10588 }
10589