xref: /titanic_50/usr/src/cmd/dladm/dladm.c (revision cdcc71c09dccd8c66f98c710f068f0722fe6de56)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <dlfcn.h>
29 #include <locale.h>
30 #include <signal.h>
31 #include <stdarg.h>
32 #include <stdlib.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <stropts.h>
36 #include <sys/stat.h>
37 #include <errno.h>
38 #include <kstat.h>
39 #include <strings.h>
40 #include <getopt.h>
41 #include <unistd.h>
42 #include <priv.h>
43 #include <termios.h>
44 #include <pwd.h>
45 #include <auth_attr.h>
46 #include <auth_list.h>
47 #include <libintl.h>
48 #include <libdevinfo.h>
49 #include <libdlpi.h>
50 #include <libdladm.h>
51 #include <libdllink.h>
52 #include <libdlstat.h>
53 #include <libdlaggr.h>
54 #include <libdlwlan.h>
55 #include <libdlvlan.h>
56 #include <libdlvnic.h>
57 #include <libdlether.h>
58 #include <libdlsim.h>
59 #include <libdlbridge.h>
60 #include <libinetutil.h>
61 #include <bsm/adt.h>
62 #include <bsm/adt_event.h>
63 #include <libdlvnic.h>
64 #include <sys/types.h>
65 #include <sys/socket.h>
66 #include <sys/processor.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <net/if_types.h>
70 #include <stddef.h>
71 #include <stp_in.h>
72 #include <ofmt.h>
73 
74 #define	MAXPORT			256
75 #define	MAXVNIC			256
76 #define	BUFLEN(lim, ptr)	(((lim) > (ptr)) ? ((lim) - (ptr)) : 0)
77 #define	MAXLINELEN		1024
78 #define	SMF_UPGRADE_FILE		"/var/svc/profile/upgrade"
79 #define	SMF_UPGRADEDATALINK_FILE	"/var/svc/profile/upgrade_datalink"
80 #define	SMF_DLADM_UPGRADE_MSG		" # added by dladm(1M)"
81 #define	DLADM_DEFAULT_COL	80
82 
83 /*
84  * used by the wifi show-* commands to set up ofmt_field_t structures.
85  */
86 #define	WIFI_CMD_SCAN		0x00000001
87 #define	WIFI_CMD_SHOW		0x00000002
88 #define	WIFI_CMD_ALL		(WIFI_CMD_SCAN | WIFI_CMD_SHOW)
89 
90 /* No larger than pktsum_t */
91 typedef struct brsum_s {
92 	uint64_t	drops;
93 	uint64_t	forward_dir;
94 	uint64_t	forward_mb;
95 	uint64_t	forward_unk;
96 	uint64_t	recv;
97 	uint64_t	sent;
98 } brsum_t;
99 
100 /* No larger than pktsum_t */
101 typedef struct brlsum_s {
102 	uint32_t	cfgbpdu;
103 	uint32_t	tcnbpdu;
104 	uint32_t	rstpbpdu;
105 	uint32_t	txbpdu;
106 	uint64_t	drops;
107 	uint64_t	recv;
108 	uint64_t	xmit;
109 } brlsum_t;
110 
111 typedef struct show_state {
112 	boolean_t	ls_firstonly;
113 	boolean_t	ls_donefirst;
114 	pktsum_t	ls_prevstats;
115 	uint32_t	ls_flags;
116 	dladm_status_t	ls_status;
117 	ofmt_handle_t	ls_ofmt;
118 	boolean_t	ls_parsable;
119 	boolean_t	ls_mac;
120 	boolean_t	ls_hwgrp;
121 } show_state_t;
122 
123 typedef struct show_grp_state {
124 	pktsum_t	gs_prevstats[MAXPORT];
125 	uint32_t	gs_flags;
126 	dladm_status_t	gs_status;
127 	boolean_t	gs_parsable;
128 	boolean_t	gs_lacp;
129 	boolean_t	gs_extended;
130 	boolean_t	gs_stats;
131 	boolean_t	gs_firstonly;
132 	boolean_t	gs_donefirst;
133 	ofmt_handle_t	gs_ofmt;
134 } show_grp_state_t;
135 
136 typedef struct show_vnic_state {
137 	datalink_id_t	vs_vnic_id;
138 	datalink_id_t	vs_link_id;
139 	char		vs_vnic[MAXLINKNAMELEN];
140 	char		vs_link[MAXLINKNAMELEN];
141 	boolean_t	vs_parsable;
142 	boolean_t	vs_found;
143 	boolean_t	vs_firstonly;
144 	boolean_t	vs_donefirst;
145 	boolean_t	vs_stats;
146 	boolean_t	vs_printstats;
147 	pktsum_t	vs_totalstats;
148 	pktsum_t	vs_prevstats[MAXVNIC];
149 	boolean_t	vs_etherstub;
150 	dladm_status_t	vs_status;
151 	uint32_t	vs_flags;
152 	ofmt_handle_t	vs_ofmt;
153 } show_vnic_state_t;
154 
155 typedef struct show_usage_state_s {
156 	boolean_t	us_plot;
157 	boolean_t	us_parsable;
158 	boolean_t	us_printheader;
159 	boolean_t	us_first;
160 	boolean_t	us_showall;
161 	ofmt_handle_t	us_ofmt;
162 } show_usage_state_t;
163 
164 /*
165  * callback functions for printing output and error diagnostics.
166  */
167 static ofmt_cb_t print_default_cb, print_link_stats_cb, print_linkprop_cb;
168 static ofmt_cb_t print_lacp_cb, print_phys_one_mac_cb;
169 static ofmt_cb_t print_xaggr_cb, print_aggr_stats_cb;
170 static ofmt_cb_t print_phys_one_hwgrp_cb, print_wlan_attr_cb;
171 static ofmt_cb_t print_wifi_status_cb, print_link_attr_cb;
172 static void dladm_ofmt_check(ofmt_status_t, boolean_t, ofmt_handle_t);
173 
174 typedef void cmdfunc_t(int, char **, const char *);
175 
176 static cmdfunc_t do_show_link, do_show_wifi, do_show_phys;
177 static cmdfunc_t do_create_aggr, do_delete_aggr, do_add_aggr, do_remove_aggr;
178 static cmdfunc_t do_modify_aggr, do_show_aggr, do_up_aggr;
179 static cmdfunc_t do_scan_wifi, do_connect_wifi, do_disconnect_wifi;
180 static cmdfunc_t do_show_linkprop, do_set_linkprop, do_reset_linkprop;
181 static cmdfunc_t do_create_secobj, do_delete_secobj, do_show_secobj;
182 static cmdfunc_t do_init_linkprop, do_init_secobj;
183 static cmdfunc_t do_create_vlan, do_delete_vlan, do_up_vlan, do_show_vlan;
184 static cmdfunc_t do_rename_link, do_delete_phys, do_init_phys;
185 static cmdfunc_t do_show_linkmap;
186 static cmdfunc_t do_show_ether;
187 static cmdfunc_t do_create_vnic, do_delete_vnic, do_show_vnic;
188 static cmdfunc_t do_up_vnic;
189 static cmdfunc_t do_create_etherstub, do_delete_etherstub, do_show_etherstub;
190 static cmdfunc_t do_create_simnet, do_modify_simnet;
191 static cmdfunc_t do_delete_simnet, do_show_simnet, do_up_simnet;
192 static cmdfunc_t do_show_usage;
193 static cmdfunc_t do_create_bridge, do_modify_bridge, do_delete_bridge;
194 static cmdfunc_t do_add_bridge, do_remove_bridge, do_show_bridge;
195 
196 static void 	do_up_vnic_common(int, char **, const char *, boolean_t);
197 
198 static void	altroot_cmd(char *, int, char **);
199 static int	show_linkprop_onelink(dladm_handle_t, datalink_id_t, void *);
200 
201 static void	link_stats(datalink_id_t, uint_t, char *, show_state_t *);
202 static void	aggr_stats(datalink_id_t, show_grp_state_t *, uint_t);
203 static void	vnic_stats(show_vnic_state_t *, uint32_t);
204 
205 static int	get_one_kstat(const char *, const char *, uint8_t,
206 		    void *, boolean_t);
207 static void	get_mac_stats(const char *, pktsum_t *);
208 static void	get_link_stats(const char *, pktsum_t *);
209 static uint64_t	get_ifspeed(const char *, boolean_t);
210 static const char	*get_linkstate(const char *, boolean_t, char *);
211 static const char	*get_linkduplex(const char *, boolean_t, char *);
212 
213 static int	show_etherprop(dladm_handle_t, datalink_id_t, void *);
214 static void	show_ether_xprop(void *, dladm_ether_info_t *);
215 static boolean_t	link_is_ether(const char *, datalink_id_t *);
216 
217 static boolean_t str2int(const char *, int *);
218 static void	die(const char *, ...);
219 static void	die_optdup(int);
220 static void	die_opterr(int, int, const char *);
221 static void	die_dlerr(dladm_status_t, const char *, ...);
222 static void	warn(const char *, ...);
223 static void	warn_dlerr(dladm_status_t, const char *, ...);
224 
225 typedef struct	cmd {
226 	char		*c_name;
227 	cmdfunc_t	*c_fn;
228 	const char	*c_usage;
229 } cmd_t;
230 
231 static cmd_t	cmds[] = {
232 	{ "rename-link",	do_rename_link,
233 	    "    rename-link      <oldlink> <newlink>"			},
234 	{ "show-link",		do_show_link,
235 	    "    show-link        [-pP] [-o <field>,..] [-s [-i <interval>]] "
236 	    "[<link>]\n"						},
237 	{ "create-aggr",	do_create_aggr,
238 	    "    create-aggr      [-t] [-P <policy>] [-L <mode>] [-T <time>] "
239 	    "[-u <address>]\n"
240 	    "\t\t     -l <link> [-l <link>...] <link>"			},
241 	{ "delete-aggr",	do_delete_aggr,
242 	    "    delete-aggr      [-t] <link>"				},
243 	{ "add-aggr",		do_add_aggr,
244 	    "    add-aggr         [-t] -l <link> [-l <link>...] <link>" },
245 	{ "remove-aggr",	do_remove_aggr,
246 	    "    remove-aggr      [-t] -l <link> [-l <link>...] <link>" },
247 	{ "modify-aggr",	do_modify_aggr,
248 	    "    modify-aggr      [-t] [-P <policy>] [-L <mode>] [-T <time>] "
249 	    "[-u <address>]\n"
250 	    "\t\t     <link>"						},
251 	{ "show-aggr",		do_show_aggr,
252 	    "    show-aggr        [-pPLx] [-o <field>,..] [-s [-i <interval>]] "
253 	    "[<link>]\n"						},
254 	{ "up-aggr",		do_up_aggr,	NULL			},
255 	{ "scan-wifi",		do_scan_wifi,
256 	    "    scan-wifi        [-p] [-o <field>,...] [<link>]"	},
257 	{ "connect-wifi",	do_connect_wifi,
258 	    "    connect-wifi     [-e <essid>] [-i <bssid>] [-k <key>,...] "
259 	    "[-s wep|wpa]\n"
260 	    "\t\t     [-a open|shared] [-b bss|ibss] [-c] [-m a|b|g] "
261 	    "[-T <time>]\n"
262 	    "\t\t     [<link>]"						},
263 	{ "disconnect-wifi",	do_disconnect_wifi,
264 	    "    disconnect-wifi  [-a] [<link>]"			},
265 	{ "show-wifi",		do_show_wifi,
266 	    "    show-wifi        [-p] [-o <field>,...] [<link>]\n"	},
267 	{ "set-linkprop",	do_set_linkprop,
268 	    "    set-linkprop     [-t] -p <prop>=<value>[,...] <name>"	},
269 	{ "reset-linkprop",	do_reset_linkprop,
270 	    "    reset-linkprop   [-t] [-p <prop>,...] <name>"		},
271 	{ "show-linkprop",	do_show_linkprop,
272 	    "    show-linkprop    [-cP] [-o <field>,...] [-p <prop>,...] "
273 	    "<name>\n"							},
274 	{ "show-ether",		do_show_ether,
275 	    "    show-ether       [-px][-o <field>,...] <link>\n"	},
276 	{ "create-secobj",	do_create_secobj,
277 	    "    create-secobj    [-t] [-f <file>] -c <class> <secobj>"	},
278 	{ "delete-secobj",	do_delete_secobj,
279 	    "    delete-secobj    [-t] <secobj>[,...]"			},
280 	{ "show-secobj",	do_show_secobj,
281 	    "    show-secobj      [-pP] [-o <field>,...] [<secobj>,...]\n" },
282 	{ "init-linkprop",	do_init_linkprop,	NULL		},
283 	{ "init-secobj",	do_init_secobj,		NULL		},
284 	{ "create-vlan", 	do_create_vlan,
285 	    "    create-vlan      [-ft] -l <link> -v <vid> [link]"	},
286 	{ "delete-vlan", 	do_delete_vlan,
287 	    "    delete-vlan      [-t] <link>"				},
288 	{ "show-vlan",		do_show_vlan,
289 	    "    show-vlan        [-pP] [-o <field>,..] [<link>]\n"	},
290 	{ "up-vlan",		do_up_vlan,		NULL		},
291 	{ "delete-phys",	do_delete_phys,
292 	    "    delete-phys      <link>"				},
293 	{ "show-phys",		do_show_phys,
294 	    "    show-phys        [-pP] [-o <field>,..] [-H] [<link>]\n"},
295 	{ "init-phys",		do_init_phys,		NULL		},
296 	{ "show-linkmap",	do_show_linkmap,	NULL		},
297 	{ "create-vnic",	do_create_vnic,
298 	    "    create-vnic      [-t] -l <link> [-m <value> | auto |\n"
299 	    "\t\t     {factory [-n <slot-id>]} | {random [-r <prefix>]}]\n"
300 	    "\t\t     [-v <vid> [-f]] [-p <prop>=<value>[,...]] [-H] "
301 	    "<vnic-link>"						},
302 	{ "delete-vnic",	do_delete_vnic,
303 	    "    delete-vnic      [-t] <vnic-link>"			},
304 	{ "show-vnic",		do_show_vnic,
305 	    "    show-vnic        [-pP] [-l <link>] [-s [-i <interval>]] "
306 	    "[<link>]\n"						},
307 	{ "up-vnic",		do_up_vnic,		NULL		},
308 	{ "create-etherstub",	do_create_etherstub,
309 	    "    create-etherstub [-t] <link>"				},
310 	{ "delete-etherstub",	do_delete_etherstub,
311 	    "    delete-etherstub [-t] <link>"				},
312 	{ "show-etherstub",	do_show_etherstub,
313 	    "    show-etherstub   [-t] [<link>]\n"			},
314 	{ "create-simnet",	do_create_simnet,	NULL		},
315 	{ "modify-simnet",	do_modify_simnet,	NULL		},
316 	{ "delete-simnet",	do_delete_simnet,	NULL		},
317 	{ "show-simnet",	do_show_simnet,		NULL		},
318 	{ "up-simnet",		do_up_simnet,		NULL		},
319 	{ "create-bridge",	do_create_bridge,
320 	    "    create-bridge    [-R <root-dir>] [-P <protect>] "
321 	    "[-p <priority>]\n"
322 	    "\t\t     [-m <max-age>] [-h <hello-time>] [-d <forward-delay>]\n"
323 	    "\t\t     [-f <force-protocol>] [-l <link>]... <bridge>"	},
324 	{ "modify-bridge",	do_modify_bridge,
325 	    "    modify-bridge    [-R <root-dir>] [-P <protect>] "
326 	    "[-p <priority>]\n"
327 	    "\t\t     [-m <max-age>] [-h <hello-time>] [-d <forward-delay>]\n"
328 	    "\t\t     [-f <force-protocol>] <bridge>"			},
329 	{ "delete-bridge",	do_delete_bridge,
330 	    "    delete-bridge    [-R <root-dir>] <bridge>"		},
331 	{ "add-bridge",		do_add_bridge,
332 	    "    add-bridge       [-R <root-dir>] -l <link> [-l <link>]... "
333 	    "<bridge>"							},
334 	{ "remove-bridge",	do_remove_bridge,
335 	    "    remove-bridge    [-R <root-dir>] -l <link> [-l <link>]... "
336 	    "<bridge>"							},
337 	{ "show-bridge",	do_show_bridge,
338 	    "    show-bridge      [-p] [-o <field>,...] [-s [-i <interval>]] "
339 	    "[<bridge>]\n"
340 	    "    show-bridge      -l [-p] [-o <field>,...] [-s [-i <interval>]]"
341 	    " <bridge>\n"
342 	    "    show-bridge      -f [-p] [-o <field>,...] [-s [-i <interval>]]"
343 	    " <bridge>\n"
344 	    "    show-bridge      -t [-p] [-o <field>,...] [-s [-i <interval>]]"
345 	    " <bridge>\n"						},
346 	{ "show-usage",		do_show_usage,
347 	    "    show-usage       [-a] [-d | -F <format>] "
348 	    "[-s <DD/MM/YYYY,HH:MM:SS>]\n"
349 	    "\t\t     [-e <DD/MM/YYYY,HH:MM:SS>] -f <logfile> [<link>]"	}
350 };
351 
352 static const struct option lopts[] = {
353 	{"vlan-id",	required_argument,	0, 'v'},
354 	{"output",	required_argument,	0, 'o'},
355 	{"dev",		required_argument,	0, 'd'},
356 	{"policy",	required_argument,	0, 'P'},
357 	{"lacp-mode",	required_argument,	0, 'L'},
358 	{"lacp-timer",	required_argument,	0, 'T'},
359 	{"unicast",	required_argument,	0, 'u'},
360 	{"temporary",	no_argument,		0, 't'},
361 	{"root-dir",	required_argument,	0, 'R'},
362 	{"link",	required_argument,	0, 'l'},
363 	{"forcible",	no_argument,		0, 'f'},
364 	{"bw-limit",	required_argument,	0, 'b'},
365 	{"mac-address",	required_argument,	0, 'm'},
366 	{"slot",	required_argument,	0, 'n'},
367 	{ 0, 0, 0, 0 }
368 };
369 
370 static const struct option show_lopts[] = {
371 	{"statistics",	no_argument,		0, 's'},
372 	{"continuous",	no_argument,		0, 'S'},
373 	{"interval",	required_argument,	0, 'i'},
374 	{"parsable",	no_argument,		0, 'p'},
375 	{"parseable",	no_argument,		0, 'p'},
376 	{"extended",	no_argument,		0, 'x'},
377 	{"output",	required_argument,	0, 'o'},
378 	{"persistent",	no_argument,		0, 'P'},
379 	{"lacp",	no_argument,		0, 'L'},
380 	{ 0, 0, 0, 0 }
381 };
382 
383 static const struct option prop_longopts[] = {
384 	{"temporary",	no_argument,		0, 't'  },
385 	{"output",	required_argument,	0, 'o'  },
386 	{"root-dir",	required_argument,	0, 'R'  },
387 	{"prop",	required_argument,	0, 'p'  },
388 	{"parsable",	no_argument,		0, 'c'  },
389 	{"parseable",	no_argument,		0, 'c'  },
390 	{"persistent",	no_argument,		0, 'P'  },
391 	{ 0, 0, 0, 0 }
392 };
393 
394 static const struct option wifi_longopts[] = {
395 	{"parsable",	no_argument,		0, 'p'  },
396 	{"parseable",	no_argument,		0, 'p'  },
397 	{"output",	required_argument,	0, 'o'  },
398 	{"essid",	required_argument,	0, 'e'  },
399 	{"bsstype",	required_argument,	0, 'b'  },
400 	{"mode",	required_argument,	0, 'm'  },
401 	{"key",		required_argument,	0, 'k'  },
402 	{"sec",		required_argument,	0, 's'  },
403 	{"auth",	required_argument,	0, 'a'  },
404 	{"create-ibss",	required_argument,	0, 'c'  },
405 	{"timeout",	required_argument,	0, 'T'  },
406 	{"all-links",	no_argument,		0, 'a'  },
407 	{"temporary",	no_argument,		0, 't'  },
408 	{"root-dir",	required_argument,	0, 'R'  },
409 	{"persistent",	no_argument,		0, 'P'  },
410 	{"file",	required_argument,	0, 'f'  },
411 	{ 0, 0, 0, 0 }
412 };
413 static const struct option showeth_lopts[] = {
414 	{"parsable",	no_argument,		0, 'p'	},
415 	{"parseable",	no_argument,		0, 'p'	},
416 	{"extended",	no_argument,		0, 'x'	},
417 	{"output",	required_argument,	0, 'o'	},
418 	{ 0, 0, 0, 0 }
419 };
420 
421 static const struct option vnic_lopts[] = {
422 	{"temporary",	no_argument,		0, 't'	},
423 	{"root-dir",	required_argument,	0, 'R'	},
424 	{"dev",		required_argument,	0, 'd'	},
425 	{"mac-address",	required_argument,	0, 'm'	},
426 	{"cpus",	required_argument,	0, 'c'	},
427 	{"bw-limit",	required_argument,	0, 'b'	},
428 	{"slot",	required_argument,	0, 'n'	},
429 	{"mac-prefix",	required_argument,	0, 'r'	},
430 	{ 0, 0, 0, 0 }
431 };
432 
433 static const struct option etherstub_lopts[] = {
434 	{"temporary",	no_argument,		0, 't'	},
435 	{"root-dir",	required_argument,	0, 'R'	},
436 	{ 0, 0, 0, 0 }
437 };
438 
439 static const struct option usage_opts[] = {
440 	{"file",	required_argument,	0, 'f'	},
441 	{"format",	required_argument,	0, 'F'	},
442 	{"start",	required_argument,	0, 's'	},
443 	{"stop",	required_argument,	0, 'e'	},
444 	{ 0, 0, 0, 0 }
445 };
446 
447 static const struct option simnet_lopts[] = {
448 	{"temporary",	no_argument,		0, 't'	},
449 	{"root-dir",	required_argument,	0, 'R'	},
450 	{"media",	required_argument,	0, 'm'	},
451 	{"peer",	required_argument,	0, 'p'	},
452 	{ 0, 0, 0, 0 }
453 };
454 
455 static const struct option bridge_lopts[] = {
456 	{ "protect",		required_argument,	0, 'P' },
457 	{ "root-dir",		required_argument,	0, 'R'	},
458 	{ "forward-delay",	required_argument,	0, 'd'	},
459 	{ "force-protocol",	required_argument,	0, 'f'	},
460 	{ "hello-time",		required_argument,	0, 'h'	},
461 	{ "link",		required_argument,	0, 'l'	},
462 	{ "max-age",		required_argument,	0, 'm'	},
463 	{ "priority",		required_argument,	0, 'p'	},
464 	{ NULL, NULL, 0, 0 }
465 };
466 
467 static const struct option bridge_show_lopts[] = {
468 	{ "forwarding", no_argument,		0, 'f' },
469 	{ "interval",	required_argument,	0, 'i' },
470 	{ "link",	no_argument,		0, 'l' },
471 	{ "output",	required_argument,	0, 'o' },
472 	{ "parsable",	no_argument,		0, 'p' },
473 	{ "parseable",	no_argument,		0, 'p' },
474 	{ "statistics",	no_argument,		0, 's' },
475 	{ "trill",	no_argument,		0, 't' },
476 	{ 0, 0, 0, 0 }
477 };
478 
479 /*
480  * structures for 'dladm show-ether'
481  */
482 static const char *ptype[] = {LEI_ATTR_NAMES};
483 
484 typedef struct ether_fields_buf_s
485 {
486 	char	eth_link[15];
487 	char	eth_ptype[8];
488 	char	eth_state[8];
489 	char	eth_autoneg[5];
490 	char	eth_spdx[31];
491 	char	eth_pause[6];
492 	char	eth_rem_fault[16];
493 } ether_fields_buf_t;
494 
495 static ofmt_field_t ether_fields[] = {
496 /* name,	field width,	offset	    callback */
497 { "LINK",	16,
498 	offsetof(ether_fields_buf_t, eth_link), print_default_cb},
499 { "PTYPE",	9,
500 	offsetof(ether_fields_buf_t, eth_ptype), print_default_cb},
501 { "STATE",	9,
502 	offsetof(ether_fields_buf_t, eth_state),
503 	print_default_cb},
504 { "AUTO",	6,
505 	offsetof(ether_fields_buf_t, eth_autoneg), print_default_cb},
506 { "SPEED-DUPLEX", 32,
507 	offsetof(ether_fields_buf_t, eth_spdx), print_default_cb},
508 { "PAUSE",	7,
509 	offsetof(ether_fields_buf_t, eth_pause), print_default_cb},
510 { "REM_FAULT",	17,
511 	offsetof(ether_fields_buf_t, eth_rem_fault), print_default_cb},
512 {NULL,		0,
513 	0, 	NULL}}
514 ;
515 
516 typedef struct print_ether_state {
517 	const char	*es_link;
518 	boolean_t	es_parsable;
519 	boolean_t	es_header;
520 	boolean_t	es_extended;
521 	ofmt_handle_t	es_ofmt;
522 } print_ether_state_t;
523 
524 /*
525  * structures for 'dladm show-link -s' (print statistics)
526  */
527 typedef enum {
528 	LINK_S_LINK,
529 	LINK_S_IPKTS,
530 	LINK_S_RBYTES,
531 	LINK_S_IERRORS,
532 	LINK_S_OPKTS,
533 	LINK_S_OBYTES,
534 	LINK_S_OERRORS
535 } link_s_field_index_t;
536 
537 static ofmt_field_t link_s_fields[] = {
538 /* name,	field width,	index,		callback	*/
539 { "LINK",	15,		LINK_S_LINK,	print_link_stats_cb},
540 { "IPACKETS",	10,		LINK_S_IPKTS,	print_link_stats_cb},
541 { "RBYTES",	8,		LINK_S_RBYTES,	print_link_stats_cb},
542 { "IERRORS",	10,		LINK_S_IERRORS,	print_link_stats_cb},
543 { "OPACKETS",	12,		LINK_S_OPKTS,	print_link_stats_cb},
544 { "OBYTES",	12,		LINK_S_OBYTES,	print_link_stats_cb},
545 { "OERRORS",	8,		LINK_S_OERRORS,	print_link_stats_cb}}
546 ;
547 
548 typedef struct link_args_s {
549 	char		*link_s_link;
550 	pktsum_t	*link_s_psum;
551 } link_args_t;
552 
553 /*
554  * buffer used by print functions for show-{link,phys,vlan} commands.
555  */
556 typedef struct link_fields_buf_s {
557 	char link_name[MAXLINKNAMELEN];
558 	char link_class[DLADM_STRSIZE];
559 	char link_mtu[11];
560 	char link_state[DLADM_STRSIZE];
561 	char link_bridge[MAXLINKNAMELEN];
562 	char link_over[MAXLINKNAMELEN];
563 	char link_phys_state[DLADM_STRSIZE];
564 	char link_phys_media[DLADM_STRSIZE];
565 	char link_phys_speed[DLADM_STRSIZE];
566 	char link_phys_duplex[DLPI_LINKNAME_MAX];
567 	char link_phys_device[DLPI_LINKNAME_MAX];
568 	char link_flags[6];
569 	char link_vlan_vid[6];
570 } link_fields_buf_t;
571 
572 /*
573  * structures for 'dladm show-link'
574  */
575 static ofmt_field_t link_fields[] = {
576 /* name,	field width,	index,	callback */
577 { "LINK",	12,
578 	offsetof(link_fields_buf_t, link_name), print_default_cb},
579 { "CLASS",	10,
580 	offsetof(link_fields_buf_t, link_class), print_default_cb},
581 { "MTU",	7,
582 	offsetof(link_fields_buf_t, link_mtu), print_default_cb},
583 { "STATE",	9,
584 	offsetof(link_fields_buf_t, link_state), print_default_cb},
585 { "BRIDGE",	11,
586     offsetof(link_fields_buf_t, link_bridge), print_default_cb},
587 { "OVER",	DLPI_LINKNAME_MAX,
588 	offsetof(link_fields_buf_t, link_over), print_default_cb},
589 { NULL,		0, 0, NULL}}
590 ;
591 
592 /*
593  * structures for 'dladm show-aggr'
594  */
595 typedef struct laggr_fields_buf_s {
596 	char laggr_name[DLPI_LINKNAME_MAX];
597 	char laggr_policy[9];
598 	char laggr_addrpolicy[ETHERADDRL * 3 + 3];
599 	char laggr_lacpactivity[14];
600 	char laggr_lacptimer[DLADM_STRSIZE];
601 	char laggr_flags[7];
602 } laggr_fields_buf_t;
603 
604 typedef struct laggr_args_s {
605 	int			laggr_lport; /* -1 indicates the aggr itself */
606 	const char 		*laggr_link;
607 	dladm_aggr_grp_attr_t	*laggr_ginfop;
608 	dladm_status_t		*laggr_status;
609 	pktsum_t		*laggr_pktsumtot; /* -s only */
610 	pktsum_t		*laggr_diffstats; /* -s only */
611 	boolean_t		laggr_parsable;
612 } laggr_args_t;
613 
614 static ofmt_field_t laggr_fields[] = {
615 /* name,	field width,	offset,	callback */
616 { "LINK",	16,
617 	offsetof(laggr_fields_buf_t, laggr_name), print_default_cb},
618 { "POLICY",	9,
619 	offsetof(laggr_fields_buf_t, laggr_policy), print_default_cb},
620 { "ADDRPOLICY",	ETHERADDRL * 3 + 3,
621 	offsetof(laggr_fields_buf_t, laggr_addrpolicy), print_default_cb},
622 { "LACPACTIVITY", 14,
623 	offsetof(laggr_fields_buf_t, laggr_lacpactivity), print_default_cb},
624 { "LACPTIMER",	12,
625 	offsetof(laggr_fields_buf_t, laggr_lacptimer), print_default_cb},
626 { "FLAGS",	8,
627 	offsetof(laggr_fields_buf_t, laggr_flags), print_default_cb},
628 { NULL,		0, 0, NULL}}
629 ;
630 
631 /*
632  * structures for 'dladm show-aggr -x'.
633  */
634 typedef enum {
635 	AGGR_X_LINK,
636 	AGGR_X_PORT,
637 	AGGR_X_SPEED,
638 	AGGR_X_DUPLEX,
639 	AGGR_X_STATE,
640 	AGGR_X_ADDRESS,
641 	AGGR_X_PORTSTATE
642 } aggr_x_field_index_t;
643 
644 static ofmt_field_t aggr_x_fields[] = {
645 /* name,	field width,	index		callback */
646 { "LINK",	12,	AGGR_X_LINK,		print_xaggr_cb},
647 { "PORT",	15,	AGGR_X_PORT,		print_xaggr_cb},
648 { "SPEED",	5,	AGGR_X_SPEED,		print_xaggr_cb},
649 { "DUPLEX",	10,	AGGR_X_DUPLEX,		print_xaggr_cb},
650 { "STATE",	10,	AGGR_X_STATE,		print_xaggr_cb},
651 { "ADDRESS",	19,	AGGR_X_ADDRESS,		print_xaggr_cb},
652 { "PORTSTATE",	16,	AGGR_X_PORTSTATE,	print_xaggr_cb},
653 { NULL,		0,	0,			NULL}}
654 ;
655 
656 /*
657  * structures for 'dladm show-aggr -s'.
658  */
659 typedef enum {
660 	AGGR_S_LINK,
661 	AGGR_S_PORT,
662 	AGGR_S_IPKTS,
663 	AGGR_S_RBYTES,
664 	AGGR_S_OPKTS,
665 	AGGR_S_OBYTES,
666 	AGGR_S_IPKTDIST,
667 	AGGR_S_OPKTDIST
668 } aggr_s_field_index_t;
669 
670 static ofmt_field_t aggr_s_fields[] = {
671 { "LINK",		12,	AGGR_S_LINK, print_aggr_stats_cb},
672 { "PORT",		10,	AGGR_S_PORT, print_aggr_stats_cb},
673 { "IPACKETS",		8,	AGGR_S_IPKTS, print_aggr_stats_cb},
674 { "RBYTES",		8,	AGGR_S_RBYTES, print_aggr_stats_cb},
675 { "OPACKETS",		8,	AGGR_S_OPKTS, print_aggr_stats_cb},
676 { "OBYTES",		8,	AGGR_S_OBYTES, print_aggr_stats_cb},
677 { "IPKTDIST",		9,	AGGR_S_IPKTDIST, print_aggr_stats_cb},
678 { "OPKTDIST",		15,	AGGR_S_OPKTDIST, print_aggr_stats_cb},
679 { NULL,			0,	0,		NULL}}
680 ;
681 
682 /*
683  * structures for 'dladm show-aggr -L'.
684  */
685 typedef enum {
686 	AGGR_L_LINK,
687 	AGGR_L_PORT,
688 	AGGR_L_AGGREGATABLE,
689 	AGGR_L_SYNC,
690 	AGGR_L_COLL,
691 	AGGR_L_DIST,
692 	AGGR_L_DEFAULTED,
693 	AGGR_L_EXPIRED
694 } aggr_l_field_index_t;
695 
696 static ofmt_field_t aggr_l_fields[] = {
697 /* name,		field width,	index */
698 { "LINK",		12,	AGGR_L_LINK,		print_lacp_cb},
699 { "PORT",		13,	AGGR_L_PORT,		print_lacp_cb},
700 { "AGGREGATABLE",	13,	AGGR_L_AGGREGATABLE,	print_lacp_cb},
701 { "SYNC",		5,	AGGR_L_SYNC,		print_lacp_cb},
702 { "COLL",		5,	AGGR_L_COLL,		print_lacp_cb},
703 { "DIST",		5,	AGGR_L_DIST,		print_lacp_cb},
704 { "DEFAULTED",		10,	AGGR_L_DEFAULTED,	print_lacp_cb},
705 { "EXPIRED",		15,	AGGR_L_EXPIRED,		print_lacp_cb},
706 { NULL,			0,	0,			NULL}}
707 ;
708 
709 /*
710  * structures for 'dladm show-phys'
711  */
712 
713 static ofmt_field_t phys_fields[] = {
714 /* name,	field width,	offset */
715 { "LINK",	13,
716 	offsetof(link_fields_buf_t, link_name), print_default_cb},
717 { "MEDIA",	21,
718 	offsetof(link_fields_buf_t, link_phys_media), print_default_cb},
719 { "STATE",	11,
720 	offsetof(link_fields_buf_t, link_phys_state), print_default_cb},
721 { "SPEED",	7,
722 	offsetof(link_fields_buf_t, link_phys_speed), print_default_cb},
723 { "DUPLEX",	10,
724 	offsetof(link_fields_buf_t, link_phys_duplex), print_default_cb},
725 { "DEVICE",	13,
726 	offsetof(link_fields_buf_t, link_phys_device), print_default_cb},
727 { "FLAGS",	7,
728 	offsetof(link_fields_buf_t, link_flags), print_default_cb},
729 { NULL,		0, NULL, 0}}
730 ;
731 
732 /*
733  * structures for 'dladm show-phys -m'
734  */
735 
736 typedef enum {
737 	PHYS_M_LINK,
738 	PHYS_M_SLOT,
739 	PHYS_M_ADDRESS,
740 	PHYS_M_INUSE,
741 	PHYS_M_CLIENT
742 } phys_m_field_index_t;
743 
744 static ofmt_field_t phys_m_fields[] = {
745 /* name,	field width,	offset */
746 { "LINK",	13,	PHYS_M_LINK,	print_phys_one_mac_cb},
747 { "SLOT",	9,	PHYS_M_SLOT,	print_phys_one_mac_cb},
748 { "ADDRESS",	19,	PHYS_M_ADDRESS,	print_phys_one_mac_cb},
749 { "INUSE",	5,	PHYS_M_INUSE,	print_phys_one_mac_cb},
750 { "CLIENT",	13,	PHYS_M_CLIENT,	print_phys_one_mac_cb},
751 { NULL,		0,	0,		NULL}}
752 ;
753 
754 /*
755  * structures for 'dladm show-phys -H'
756  */
757 
758 typedef enum {
759 	PHYS_H_LINK,
760 	PHYS_H_GROUP,
761 	PHYS_H_GRPTYPE,
762 	PHYS_H_RINGS,
763 	PHYS_H_CLIENTS
764 } phys_h_field_index_t;
765 
766 static ofmt_field_t phys_h_fields[] = {
767 { "LINK",	13,	PHYS_H_LINK,	print_phys_one_hwgrp_cb},
768 { "GROUP",	9,	PHYS_H_GROUP,	print_phys_one_hwgrp_cb},
769 { "GROUPTYPE",	7,	PHYS_H_GRPTYPE,	print_phys_one_hwgrp_cb},
770 { "RINGS",	17,	PHYS_H_RINGS,	print_phys_one_hwgrp_cb},
771 { "CLIENTS",	21,	PHYS_H_CLIENTS,	print_phys_one_hwgrp_cb},
772 { NULL,		0,	0,		NULL}}
773 ;
774 
775 /*
776  * structures for 'dladm show-vlan'
777  */
778 static ofmt_field_t vlan_fields[] = {
779 { "LINK",	16,
780 	offsetof(link_fields_buf_t, link_name), print_default_cb},
781 { "VID",	9,
782 	offsetof(link_fields_buf_t, link_vlan_vid), print_default_cb},
783 { "OVER",	13,
784 	offsetof(link_fields_buf_t, link_over), print_default_cb},
785 { "FLAGS",	7,
786 	offsetof(link_fields_buf_t, link_flags), print_default_cb},
787 { NULL,		0, 0, NULL}}
788 ;
789 
790 /*
791  * structures common to 'dladm scan-wifi' and 'dladm show-wifi'
792  * callback will be determined in parse_wifi_fields.
793  */
794 static ofmt_field_t wifi_common_fields[] = {
795 { "LINK",	11, 0,				NULL},
796 { "ESSID",	20, DLADM_WLAN_ATTR_ESSID,	NULL},
797 { "BSSID",	18, DLADM_WLAN_ATTR_BSSID,	NULL},
798 { "IBSSID",	18, DLADM_WLAN_ATTR_BSSID,	NULL},
799 { "MODE",	7,  DLADM_WLAN_ATTR_MODE,	NULL},
800 { "SPEED",	7,  DLADM_WLAN_ATTR_SPEED,	NULL},
801 { "BSSTYPE",	9,  DLADM_WLAN_ATTR_BSSTYPE,	NULL},
802 { "SEC",	7,  DLADM_WLAN_ATTR_SECMODE,	NULL},
803 { "STRENGTH",	11, DLADM_WLAN_ATTR_STRENGTH,	NULL},
804 { NULL,		0,  0,				NULL}};
805 
806 /*
807  * the 'show-wifi' command supports all the fields in wifi_common_fields
808  * plus the AUTH and STATUS fields.
809  */
810 static ofmt_field_t wifi_show_fields[A_CNT(wifi_common_fields) + 2] = {
811 { "AUTH",	9,  DLADM_WLAN_ATTR_AUTH,	NULL},
812 { "STATUS",	18, DLADM_WLAN_LINKATTR_STATUS,	print_wifi_status_cb},
813 /* copy wifi_common_fields here */
814 };
815 
816 static char *all_scan_wifi_fields =
817 	"link,essid,bssid,sec,strength,mode,speed,bsstype";
818 static char *all_show_wifi_fields =
819 	"link,status,essid,sec,strength,mode,speed,auth,bssid,bsstype";
820 static char *def_scan_wifi_fields =
821 	"link,essid,bssid,sec,strength,mode,speed";
822 static char *def_show_wifi_fields =
823 	"link,status,essid,sec,strength,mode,speed";
824 
825 /*
826  * structures for 'dladm show-linkprop'
827  */
828 typedef enum {
829 	LINKPROP_LINK,
830 	LINKPROP_PROPERTY,
831 	LINKPROP_PERM,
832 	LINKPROP_VALUE,
833 	LINKPROP_DEFAULT,
834 	LINKPROP_POSSIBLE
835 } linkprop_field_index_t;
836 
837 static ofmt_field_t linkprop_fields[] = {
838 /* name,	field width,  index */
839 { "LINK",	13,	LINKPROP_LINK,		print_linkprop_cb},
840 { "PROPERTY",	16,	LINKPROP_PROPERTY,	print_linkprop_cb},
841 { "PERM",	5,	LINKPROP_PERM,		print_linkprop_cb},
842 { "VALUE",	15,	LINKPROP_VALUE,		print_linkprop_cb},
843 { "DEFAULT",	15,	LINKPROP_DEFAULT,	print_linkprop_cb},
844 { "POSSIBLE",	21,	LINKPROP_POSSIBLE,	print_linkprop_cb},
845 { NULL,		0,	0,			NULL}}
846 ;
847 
848 #define	MAX_PROP_LINE		512
849 
850 typedef struct show_linkprop_state {
851 	char			ls_link[MAXLINKNAMELEN];
852 	char			*ls_line;
853 	char			**ls_propvals;
854 	dladm_arg_list_t	*ls_proplist;
855 	boolean_t		ls_parsable;
856 	boolean_t		ls_persist;
857 	boolean_t		ls_header;
858 	dladm_status_t		ls_status;
859 	dladm_status_t		ls_retstatus;
860 	ofmt_handle_t		ls_ofmt;
861 } show_linkprop_state_t;
862 
863 typedef struct set_linkprop_state {
864 	const char		*ls_name;
865 	boolean_t		ls_reset;
866 	boolean_t		ls_temp;
867 	dladm_status_t		ls_status;
868 } set_linkprop_state_t;
869 
870 typedef struct linkprop_args_s {
871 	show_linkprop_state_t	*ls_state;
872 	char			*ls_propname;
873 	datalink_id_t		ls_linkid;
874 } linkprop_args_t;
875 
876 /*
877  * structures for 'dladm show-secobj'
878  */
879 typedef struct secobj_fields_buf_s {
880 	char			ss_obj_name[DLADM_SECOBJ_VAL_MAX];
881 	char			ss_class[20];
882 	char			ss_val[30];
883 } secobj_fields_buf_t;
884 
885 static ofmt_field_t secobj_fields[] = {
886 { "OBJECT",	21,
887 	offsetof(secobj_fields_buf_t, ss_obj_name), print_default_cb},
888 { "CLASS",	21,
889 	offsetof(secobj_fields_buf_t, ss_class), print_default_cb},
890 { "VALUE",	31,
891 	offsetof(secobj_fields_buf_t, ss_val), print_default_cb},
892 { NULL,		0, 0, NULL}}
893 ;
894 
895 /*
896  * structures for 'dladm show-vnic'
897  */
898 typedef struct vnic_fields_buf_s
899 {
900 	char vnic_link[DLPI_LINKNAME_MAX];
901 	char vnic_over[DLPI_LINKNAME_MAX];
902 	char vnic_speed[6];
903 	char vnic_macaddr[18];
904 	char vnic_macaddrtype[19];
905 	char vnic_vid[6];
906 } vnic_fields_buf_t;
907 
908 static ofmt_field_t vnic_fields[] = {
909 { "LINK",		13,
910 	offsetof(vnic_fields_buf_t, vnic_link),	print_default_cb},
911 { "OVER",		13,
912 	offsetof(vnic_fields_buf_t, vnic_over),	print_default_cb},
913 { "SPEED",		7,
914 	offsetof(vnic_fields_buf_t, vnic_speed), print_default_cb},
915 { "MACADDRESS",		18,
916 	offsetof(vnic_fields_buf_t, vnic_macaddr), print_default_cb},
917 { "MACADDRTYPE",	20,
918 	offsetof(vnic_fields_buf_t, vnic_macaddrtype), print_default_cb},
919 { "VID",		7,
920 	offsetof(vnic_fields_buf_t, vnic_vid), print_default_cb},
921 { NULL,			0, 0, NULL}}
922 ;
923 
924 /*
925  * structures for 'dladm show-simnet'
926  */
927 typedef struct simnet_fields_buf_s
928 {
929 	char simnet_name[DLPI_LINKNAME_MAX];
930 	char simnet_media[DLADM_STRSIZE];
931 	char simnet_macaddr[18];
932 	char simnet_otherlink[DLPI_LINKNAME_MAX];
933 } simnet_fields_buf_t;
934 
935 static ofmt_field_t simnet_fields[] = {
936 { "LINK",		12,
937 	offsetof(simnet_fields_buf_t, simnet_name), print_default_cb},
938 { "MEDIA",		20,
939 	offsetof(simnet_fields_buf_t, simnet_media), print_default_cb},
940 { "MACADDRESS",		18,
941 	offsetof(simnet_fields_buf_t, simnet_macaddr), print_default_cb},
942 { "OTHERLINK",		12,
943 	offsetof(simnet_fields_buf_t, simnet_otherlink), print_default_cb},
944 { NULL,			0, 0, NULL}}
945 ;
946 
947 /*
948  * structures for 'dladm show-usage'
949  */
950 
951 typedef struct  usage_fields_buf_s {
952 	char	usage_link[12];
953 	char	usage_duration[10];
954 	char	usage_ipackets[9];
955 	char	usage_rbytes[10];
956 	char	usage_opackets[9];
957 	char	usage_obytes[10];
958 	char	usage_bandwidth[14];
959 } usage_fields_buf_t;
960 
961 static ofmt_field_t usage_fields[] = {
962 { "LINK",	13,
963 	offsetof(usage_fields_buf_t, usage_link), print_default_cb},
964 { "DURATION",	11,
965 	offsetof(usage_fields_buf_t, usage_duration), print_default_cb},
966 { "IPACKETS",	10,
967 	offsetof(usage_fields_buf_t, usage_ipackets), print_default_cb},
968 { "RBYTES",	11,
969 	offsetof(usage_fields_buf_t, usage_rbytes), print_default_cb},
970 { "OPACKETS",	10,
971 	offsetof(usage_fields_buf_t, usage_opackets), print_default_cb},
972 { "OBYTES",	11,
973 	offsetof(usage_fields_buf_t, usage_obytes), print_default_cb},
974 { "BANDWIDTH",	15,
975 	offsetof(usage_fields_buf_t, usage_bandwidth), print_default_cb},
976 { NULL,		0, 0, NULL}}
977 ;
978 
979 
980 /*
981  * structures for 'dladm show-usage link'
982  */
983 
984 typedef struct  usage_l_fields_buf_s {
985 	char	usage_l_link[12];
986 	char	usage_l_stime[13];
987 	char	usage_l_etime[13];
988 	char	usage_l_rbytes[8];
989 	char	usage_l_obytes[8];
990 	char	usage_l_bandwidth[14];
991 } usage_l_fields_buf_t;
992 
993 static ofmt_field_t usage_l_fields[] = {
994 /* name,	field width,	offset */
995 { "LINK",	13,
996 	offsetof(usage_l_fields_buf_t, usage_l_link), print_default_cb},
997 { "START",	14,
998 	offsetof(usage_l_fields_buf_t, usage_l_stime), print_default_cb},
999 { "END",	14,
1000 	offsetof(usage_l_fields_buf_t, usage_l_etime), print_default_cb},
1001 { "RBYTES",	9,
1002 	offsetof(usage_l_fields_buf_t, usage_l_rbytes), print_default_cb},
1003 { "OBYTES",	9,
1004 	offsetof(usage_l_fields_buf_t, usage_l_obytes), print_default_cb},
1005 { "BANDWIDTH",	15,
1006 	offsetof(usage_l_fields_buf_t, usage_l_bandwidth), print_default_cb},
1007 { NULL,		0, 0, NULL}}
1008 ;
1009 
1010 /*
1011  * structures for 'dladm show-bridge'.  These are based on sections 14.8.1.1.3
1012  * and 14.8.1.2.2 of IEEE 802.1D-2004.
1013  */
1014 typedef struct bridge_fields_buf_s {
1015 	char bridge_name[MAXLINKNAMELEN]; /* 14.4.1.2.3(b) */
1016 	char bridge_protect[7];		/* stp or trill */
1017 	char bridge_address[24];	/* 17.18.3, 7.12.5, 14.4.1.2.3(a) */
1018 	char bridge_priority[7];	/* 17.18.3 9.2.5 - only upper 4 bits */
1019 	char bridge_bmaxage[7];		/* 17.18.4 configured */
1020 	char bridge_bhellotime[7];	/* 17.18.4 configured */
1021 	char bridge_bfwddelay[7];	/* 17.18.4 configured */
1022 	char bridge_forceproto[3];	/* 17.13.4 configured */
1023 	char bridge_tctime[12];		/* 14.8.1.1.3(b) */
1024 	char bridge_tccount[12];	/* 17.17.8 */
1025 	char bridge_tchange[12];	/* 17.17.8 */
1026 	char bridge_desroot[24];	/* 17.18.6 priority "/" MAC */
1027 	char bridge_rootcost[12];	/* 17.18.6 */
1028 	char bridge_rootport[12];	/* 17.18.6 */
1029 	char bridge_maxage[7];		/* 17.18.7 for root */
1030 	char bridge_hellotime[7];	/* 17.13.6 for root */
1031 	char bridge_fwddelay[7];	/* 17.13.5 for root */
1032 	char bridge_holdtime[12];	/* 17.13.12 for root */
1033 } bridge_fields_buf_t;
1034 
1035 static ofmt_field_t bridge_fields[] = {
1036 /* name,	field width,	offset,	callback	*/
1037 { "BRIDGE",	12,
1038     offsetof(bridge_fields_buf_t, bridge_name), print_default_cb },
1039 { "PROTECT",	8,
1040     offsetof(bridge_fields_buf_t, bridge_protect), print_default_cb },
1041 { "ADDRESS",	19,
1042     offsetof(bridge_fields_buf_t, bridge_address), print_default_cb },
1043 { "PRIORITY",	9,
1044     offsetof(bridge_fields_buf_t, bridge_priority), print_default_cb },
1045 { "BMAXAGE",	8,
1046     offsetof(bridge_fields_buf_t, bridge_bmaxage), print_default_cb },
1047 { "BHELLOTIME",	11,
1048     offsetof(bridge_fields_buf_t, bridge_bhellotime), print_default_cb },
1049 { "BFWDDELAY",	10,
1050     offsetof(bridge_fields_buf_t, bridge_bfwddelay), print_default_cb },
1051 { "FORCEPROTO",	11,
1052     offsetof(bridge_fields_buf_t, bridge_forceproto), print_default_cb },
1053 { "TCTIME",	10,
1054     offsetof(bridge_fields_buf_t, bridge_tctime), print_default_cb },
1055 { "TCCOUNT",	10,
1056     offsetof(bridge_fields_buf_t, bridge_tccount), print_default_cb },
1057 { "TCHANGE",	10,
1058     offsetof(bridge_fields_buf_t, bridge_tchange), print_default_cb },
1059 { "DESROOT",	23,
1060     offsetof(bridge_fields_buf_t, bridge_desroot), print_default_cb },
1061 { "ROOTCOST",	11,
1062     offsetof(bridge_fields_buf_t, bridge_rootcost), print_default_cb },
1063 { "ROOTPORT",	11,
1064     offsetof(bridge_fields_buf_t, bridge_rootport), print_default_cb },
1065 { "MAXAGE",	8,
1066     offsetof(bridge_fields_buf_t, bridge_maxage), print_default_cb },
1067 { "HELLOTIME",	10,
1068     offsetof(bridge_fields_buf_t, bridge_hellotime), print_default_cb },
1069 { "FWDDELAY",	9,
1070     offsetof(bridge_fields_buf_t, bridge_fwddelay), print_default_cb },
1071 { "HOLDTIME",	9,
1072     offsetof(bridge_fields_buf_t, bridge_holdtime), print_default_cb },
1073 { NULL,		0, 0, NULL}};
1074 
1075 /*
1076  * structures for 'dladm show-bridge -l'.  These are based on 14.4.1.2.3 and
1077  * 14.8.2.1.3 of IEEE 802.1D-2004.
1078  */
1079 typedef struct bridge_link_fields_buf_s {
1080 	char bridgel_link[MAXLINKNAMELEN];
1081 	char bridgel_index[7];			/* 14.4.1.2.3(d1) */
1082 	char bridgel_state[11];			/* 14.8.2.1.3(b) */
1083 	char bridgel_uptime[7];			/* 14.8.2.1.3(a) */
1084 	char bridgel_opercost[7]		/* 14.8.2.1.3(d) */;
1085 	char bridgel_operp2p[4];		/* 14.8.2.1.3(p) */
1086 	char bridgel_operedge[4];		/* 14.8.2.1.3(k) */
1087 	char bridgel_desroot[23];		/* 14.8.2.1.3(e) */
1088 	char bridgel_descost[12];		/* 14.8.2.1.3(f) */
1089 	char bridgel_desbridge[23];		/* 14.8.2.1.3(g) */
1090 	char bridgel_desport[7];		/* 14.8.2.1.3(h) */
1091 	char bridgel_tcack[4];			/* 14.8.2.1.3(i) */
1092 } bridge_link_fields_buf_t;
1093 
1094 static ofmt_field_t bridge_link_fields[] = {
1095 /* name,	field width,	offset,	callback	*/
1096 { "LINK",		12,
1097     offsetof(bridge_link_fields_buf_t, bridgel_link), print_default_cb },
1098 { "INDEX",	8,
1099     offsetof(bridge_link_fields_buf_t, bridgel_index), print_default_cb },
1100 { "STATE",	12,
1101     offsetof(bridge_link_fields_buf_t, bridgel_state), print_default_cb },
1102 { "UPTIME",	8,
1103     offsetof(bridge_link_fields_buf_t, bridgel_uptime), print_default_cb },
1104 { "OPERCOST",	9,
1105     offsetof(bridge_link_fields_buf_t, bridgel_opercost), print_default_cb },
1106 { "OPERP2P",	8,
1107     offsetof(bridge_link_fields_buf_t, bridgel_operp2p), print_default_cb },
1108 { "OPEREDGE",	9,
1109     offsetof(bridge_link_fields_buf_t, bridgel_operedge), print_default_cb },
1110 { "DESROOT",	22,
1111     offsetof(bridge_link_fields_buf_t, bridgel_desroot), print_default_cb },
1112 { "DESCOST",	11,
1113     offsetof(bridge_link_fields_buf_t, bridgel_descost), print_default_cb },
1114 { "DESBRIDGE",	22,
1115     offsetof(bridge_link_fields_buf_t, bridgel_desbridge), print_default_cb },
1116 { "DESPORT",	8,
1117     offsetof(bridge_link_fields_buf_t, bridgel_desport), print_default_cb },
1118 { "TCACK",	6,
1119     offsetof(bridge_link_fields_buf_t, bridgel_tcack), print_default_cb },
1120 { NULL,		0, 0, NULL}};
1121 
1122 /*
1123  * structures for 'dladm show-bridge -s'.  These are not based on IEEE
1124  * 802.1D-2004.
1125  */
1126 #define	ULONG_DIG	(((sizeof (ulong_t) * NBBY) * 3 / 10) + 1)
1127 #define	UINT64_DIG	(((sizeof (uint64_t) * NBBY) * 3 / 10) + 1)
1128 typedef struct bridge_statfields_buf_s {
1129 	char bridges_name[MAXLINKNAMELEN];
1130 	char bridges_drops[UINT64_DIG];
1131 	char bridges_forwards[UINT64_DIG];
1132 	char bridges_mbcast[UINT64_DIG];
1133 	char bridges_unknown[UINT64_DIG];
1134 	char bridges_recv[UINT64_DIG];
1135 	char bridges_sent[UINT64_DIG];
1136 } bridge_statfields_buf_t;
1137 
1138 static ofmt_field_t bridge_statfields[] = {
1139 /* name,	field width,	offset,	callback	*/
1140 { "BRIDGE",	12,
1141     offsetof(bridge_statfields_buf_t, bridges_name), print_default_cb },
1142 { "DROPS",	12,
1143     offsetof(bridge_statfields_buf_t, bridges_drops), print_default_cb },
1144 { "FORWARDS",	12,
1145     offsetof(bridge_statfields_buf_t, bridges_forwards), print_default_cb },
1146 { "MBCAST",	12,
1147     offsetof(bridge_statfields_buf_t, bridges_mbcast), print_default_cb },
1148 { "UNKNOWN",	12,
1149     offsetof(bridge_statfields_buf_t, bridges_unknown), print_default_cb },
1150 { "RECV",	12,
1151     offsetof(bridge_statfields_buf_t, bridges_recv), print_default_cb },
1152 { "SENT",	12,
1153     offsetof(bridge_statfields_buf_t, bridges_sent), print_default_cb },
1154 { NULL,		0, 0, NULL}};
1155 
1156 /*
1157  * structures for 'dladm show-bridge -s -l'.  These are based in part on
1158  * section 14.6.1.1.3 of IEEE 802.1D-2004.
1159  */
1160 typedef struct bridge_link_statfields_buf_s {
1161 	char bridgels_link[MAXLINKNAMELEN];
1162 	char bridgels_cfgbpdu[ULONG_DIG];
1163 	char bridgels_tcnbpdu[ULONG_DIG];
1164 	char bridgels_rstpbpdu[ULONG_DIG];
1165 	char bridgels_txbpdu[ULONG_DIG];
1166 	char bridgels_drops[UINT64_DIG];	/* 14.6.1.1.3(d) */
1167 	char bridgels_recv[UINT64_DIG];		/* 14.6.1.1.3(a) */
1168 	char bridgels_xmit[UINT64_DIG];		/* 14.6.1.1.3(c) */
1169 } bridge_link_statfields_buf_t;
1170 
1171 static ofmt_field_t bridge_link_statfields[] = {
1172 /* name,	field width,	offset,	callback	*/
1173 { "LINK",	12,
1174     offsetof(bridge_link_statfields_buf_t, bridgels_link), print_default_cb },
1175 { "CFGBPDU",	9,
1176     offsetof(bridge_link_statfields_buf_t, bridgels_cfgbpdu),
1177     print_default_cb },
1178 { "TCNBPDU",	9,
1179     offsetof(bridge_link_statfields_buf_t, bridgels_tcnbpdu),
1180     print_default_cb },
1181 { "RSTPBPDU",	9,
1182     offsetof(bridge_link_statfields_buf_t, bridgels_rstpbpdu),
1183     print_default_cb },
1184 { "TXBPDU",	9,
1185     offsetof(bridge_link_statfields_buf_t, bridgels_txbpdu), print_default_cb },
1186 { "DROPS",	9,
1187     offsetof(bridge_link_statfields_buf_t, bridgels_drops), print_default_cb },
1188 { "RECV",	9,
1189     offsetof(bridge_link_statfields_buf_t, bridgels_recv), print_default_cb },
1190 { "XMIT",	9,
1191     offsetof(bridge_link_statfields_buf_t, bridgels_xmit), print_default_cb },
1192 { NULL,		0, 0, NULL}};
1193 
1194 /*
1195  * structures for 'dladm show-bridge -f'.  These are based in part on
1196  * section  14.7.6.3.3 of IEEE 802.1D-2004.
1197  */
1198 typedef struct bridge_fwd_fields_buf_s {
1199 	char bridgef_dest[18];			/* 14.7.6.3.3(a) */
1200 	char bridgef_age[8];
1201 	char bridgef_flags[6];
1202 	char bridgef_output[MAXLINKNAMELEN];	/* 14.7.6.3.3(c) */
1203 } bridge_fwd_fields_buf_t;
1204 
1205 static ofmt_field_t bridge_fwd_fields[] = {
1206 /* name,	field width,	offset,	callback	*/
1207 { "DEST",	17,
1208     offsetof(bridge_fwd_fields_buf_t, bridgef_dest), print_default_cb },
1209 { "AGE",	7,
1210     offsetof(bridge_fwd_fields_buf_t, bridgef_age), print_default_cb },
1211 { "FLAGS",	6,
1212     offsetof(bridge_fwd_fields_buf_t, bridgef_flags), print_default_cb },
1213 { "OUTPUT",	12,
1214     offsetof(bridge_fwd_fields_buf_t, bridgef_output), print_default_cb },
1215 { NULL,		0, 0, NULL}};
1216 
1217 /*
1218  * structures for 'dladm show-bridge -t'.
1219  */
1220 typedef struct bridge_trill_fields_buf_s {
1221 	char bridget_nick[6];
1222 	char bridget_flags[6];
1223 	char bridget_link[MAXLINKNAMELEN];
1224 	char bridget_nexthop[18];
1225 } bridge_trill_fields_buf_t;
1226 
1227 static ofmt_field_t bridge_trill_fields[] = {
1228 /* name,	field width,	offset,	callback	*/
1229 { "NICK",	5,
1230     offsetof(bridge_trill_fields_buf_t, bridget_nick), print_default_cb },
1231 { "FLAGS",	6,
1232     offsetof(bridge_trill_fields_buf_t, bridget_flags), print_default_cb },
1233 { "LINK",	12,
1234     offsetof(bridge_trill_fields_buf_t, bridget_link), print_default_cb },
1235 { "NEXTHOP",	17,
1236     offsetof(bridge_trill_fields_buf_t, bridget_nexthop), print_default_cb },
1237 { NULL,		0, 0, NULL}};
1238 
1239 static char *progname;
1240 static sig_atomic_t signalled;
1241 
1242 /*
1243  * Handle to libdladm.  Opened in main() before the sub-command
1244  * specific function is called.
1245  */
1246 static dladm_handle_t handle = NULL;
1247 
1248 #define	DLADM_ETHERSTUB_NAME	"etherstub"
1249 #define	DLADM_IS_ETHERSTUB(id)	(id == DATALINK_INVALID_LINKID)
1250 
1251 static void
1252 usage(void)
1253 {
1254 	int	i;
1255 	cmd_t	*cmdp;
1256 	(void) fprintf(stderr, gettext("usage:  dladm <subcommand> <args> ..."
1257 	    "\n"));
1258 	for (i = 0; i < sizeof (cmds) / sizeof (cmds[0]); i++) {
1259 		cmdp = &cmds[i];
1260 		if (cmdp->c_usage != NULL)
1261 			(void) fprintf(stderr, "%s\n", gettext(cmdp->c_usage));
1262 	}
1263 
1264 	/* close dladm handle if it was opened */
1265 	if (handle != NULL)
1266 		dladm_close(handle);
1267 
1268 	exit(1);
1269 }
1270 
1271 int
1272 main(int argc, char *argv[])
1273 {
1274 	int	i;
1275 	cmd_t	*cmdp;
1276 	dladm_status_t status;
1277 
1278 	(void) setlocale(LC_ALL, "");
1279 #if !defined(TEXT_DOMAIN)
1280 #define	TEXT_DOMAIN "SYS_TEST"
1281 #endif
1282 	(void) textdomain(TEXT_DOMAIN);
1283 
1284 	progname = argv[0];
1285 
1286 	if (argc < 2)
1287 		usage();
1288 
1289 	for (i = 0; i < sizeof (cmds) / sizeof (cmds[0]); i++) {
1290 		cmdp = &cmds[i];
1291 		if (strcmp(argv[1], cmdp->c_name) == 0) {
1292 			/* Open the libdladm handle */
1293 			if ((status = dladm_open(&handle)) != DLADM_STATUS_OK) {
1294 				die_dlerr(status,
1295 				    "could not open /dev/dld");
1296 			}
1297 
1298 			cmdp->c_fn(argc - 1, &argv[1], cmdp->c_usage);
1299 
1300 			dladm_close(handle);
1301 			exit(0);
1302 		}
1303 	}
1304 
1305 	(void) fprintf(stderr, gettext("%s: unknown subcommand '%s'\n"),
1306 	    progname, argv[1]);
1307 	usage();
1308 
1309 	return (0);
1310 }
1311 
1312 /*ARGSUSED*/
1313 static int
1314 show_usage_date(dladm_usage_t *usage, void *arg)
1315 {
1316 	show_usage_state_t	*state = (show_usage_state_t *)arg;
1317 	time_t			stime;
1318 	char			timebuf[20];
1319 	dladm_status_t		status;
1320 	uint32_t		flags;
1321 
1322 	/*
1323 	 * Only show usage information for existing links unless '-a'
1324 	 * is specified.
1325 	 */
1326 	if (!state->us_showall) {
1327 		if ((status = dladm_name2info(handle, usage->du_name,
1328 		    NULL, &flags, NULL, NULL)) != DLADM_STATUS_OK) {
1329 			return (status);
1330 		}
1331 		if ((flags & DLADM_OPT_ACTIVE) == 0)
1332 			return (DLADM_STATUS_LINKINVAL);
1333 	}
1334 
1335 	stime = usage->du_stime;
1336 	(void) strftime(timebuf, sizeof (timebuf), "%m/%d/%Y",
1337 	    localtime(&stime));
1338 	(void) printf("%s\n", timebuf);
1339 
1340 	return (DLADM_STATUS_OK);
1341 }
1342 
1343 static int
1344 show_usage_time(dladm_usage_t *usage, void *arg)
1345 {
1346 	show_usage_state_t	*state = (show_usage_state_t *)arg;
1347 	char			buf[DLADM_STRSIZE];
1348 	usage_l_fields_buf_t 	ubuf;
1349 	time_t			time;
1350 	double			bw;
1351 	dladm_status_t		status;
1352 	uint32_t		flags;
1353 
1354 	/*
1355 	 * Only show usage information for existing links unless '-a'
1356 	 * is specified.
1357 	 */
1358 	if (!state->us_showall) {
1359 		if ((status = dladm_name2info(handle, usage->du_name,
1360 		    NULL, &flags, NULL, NULL)) != DLADM_STATUS_OK) {
1361 			return (status);
1362 		}
1363 		if ((flags & DLADM_OPT_ACTIVE) == 0)
1364 			return (DLADM_STATUS_LINKINVAL);
1365 	}
1366 
1367 	if (state->us_plot) {
1368 		if (!state->us_printheader) {
1369 			if (state->us_first) {
1370 				(void) printf("# Time");
1371 				state->us_first = B_FALSE;
1372 			}
1373 			(void) printf(" %s", usage->du_name);
1374 			if (usage->du_last) {
1375 				(void) printf("\n");
1376 				state->us_first = B_TRUE;
1377 				state->us_printheader = B_TRUE;
1378 			}
1379 		} else {
1380 			if (state->us_first) {
1381 				time = usage->du_etime;
1382 				(void) strftime(buf, sizeof (buf), "%T",
1383 				    localtime(&time));
1384 				state->us_first = B_FALSE;
1385 				(void) printf("%s", buf);
1386 			}
1387 			bw = (double)usage->du_bandwidth/1000;
1388 			(void) printf(" %.2f", bw);
1389 			if (usage->du_last) {
1390 				(void) printf("\n");
1391 				state->us_first = B_TRUE;
1392 			}
1393 		}
1394 		return (DLADM_STATUS_OK);
1395 	}
1396 
1397 	bzero(&ubuf, sizeof (ubuf));
1398 
1399 	(void) snprintf(ubuf.usage_l_link, sizeof (ubuf.usage_l_link), "%s",
1400 	    usage->du_name);
1401 	time = usage->du_stime;
1402 	(void) strftime(buf, sizeof (buf), "%T", localtime(&time));
1403 	(void) snprintf(ubuf.usage_l_stime, sizeof (ubuf.usage_l_stime), "%s",
1404 	    buf);
1405 	time = usage->du_etime;
1406 	(void) strftime(buf, sizeof (buf), "%T", localtime(&time));
1407 	(void) snprintf(ubuf.usage_l_etime, sizeof (ubuf.usage_l_etime), "%s",
1408 	    buf);
1409 	(void) snprintf(ubuf.usage_l_rbytes, sizeof (ubuf.usage_l_rbytes),
1410 	    "%llu", usage->du_rbytes);
1411 	(void) snprintf(ubuf.usage_l_obytes, sizeof (ubuf.usage_l_obytes),
1412 	    "%llu", usage->du_obytes);
1413 	(void) snprintf(ubuf.usage_l_bandwidth, sizeof (ubuf.usage_l_bandwidth),
1414 	    "%s Mbps", dladm_bw2str(usage->du_bandwidth, buf));
1415 
1416 	ofmt_print(state->us_ofmt, &ubuf);
1417 	return (DLADM_STATUS_OK);
1418 }
1419 
1420 static int
1421 show_usage_res(dladm_usage_t *usage, void *arg)
1422 {
1423 	show_usage_state_t	*state = (show_usage_state_t *)arg;
1424 	char			buf[DLADM_STRSIZE];
1425 	usage_fields_buf_t	ubuf;
1426 	dladm_status_t		status;
1427 	uint32_t		flags;
1428 
1429 	/*
1430 	 * Only show usage information for existing links unless '-a'
1431 	 * is specified.
1432 	 */
1433 	if (!state->us_showall) {
1434 		if ((status = dladm_name2info(handle, usage->du_name,
1435 		    NULL, &flags, NULL, NULL)) != DLADM_STATUS_OK) {
1436 			return (status);
1437 		}
1438 		if ((flags & DLADM_OPT_ACTIVE) == 0)
1439 			return (DLADM_STATUS_LINKINVAL);
1440 	}
1441 
1442 	bzero(&ubuf, sizeof (ubuf));
1443 
1444 	(void) snprintf(ubuf.usage_link, sizeof (ubuf.usage_link), "%s",
1445 	    usage->du_name);
1446 	(void) snprintf(ubuf.usage_duration, sizeof (ubuf.usage_duration),
1447 	    "%llu", usage->du_duration);
1448 	(void) snprintf(ubuf.usage_ipackets, sizeof (ubuf.usage_ipackets),
1449 	    "%llu", usage->du_ipackets);
1450 	(void) snprintf(ubuf.usage_rbytes, sizeof (ubuf.usage_rbytes),
1451 	    "%llu", usage->du_rbytes);
1452 	(void) snprintf(ubuf.usage_opackets, sizeof (ubuf.usage_opackets),
1453 	    "%llu", usage->du_opackets);
1454 	(void) snprintf(ubuf.usage_obytes, sizeof (ubuf.usage_obytes),
1455 	    "%llu", usage->du_obytes);
1456 	(void) snprintf(ubuf.usage_bandwidth, sizeof (ubuf.usage_bandwidth),
1457 	    "%s Mbps", dladm_bw2str(usage->du_bandwidth, buf));
1458 
1459 	ofmt_print(state->us_ofmt, &ubuf);
1460 
1461 	return (DLADM_STATUS_OK);
1462 }
1463 
1464 static boolean_t
1465 valid_formatspec(char *formatspec_str)
1466 {
1467 	if (strcmp(formatspec_str, "gnuplot") == 0)
1468 		return (B_TRUE);
1469 	return (B_FALSE);
1470 
1471 }
1472 
1473 /*ARGSUSED*/
1474 static void
1475 do_show_usage(int argc, char *argv[], const char *use)
1476 {
1477 	char			*file = NULL;
1478 	int			opt;
1479 	dladm_status_t		status;
1480 	boolean_t		d_arg = B_FALSE;
1481 	char			*stime = NULL;
1482 	char			*etime = NULL;
1483 	char			*resource = NULL;
1484 	show_usage_state_t	state;
1485 	boolean_t		o_arg = B_FALSE;
1486 	boolean_t		F_arg = B_FALSE;
1487 	char			*fields_str = NULL;
1488 	char			*formatspec_str = NULL;
1489 	char			*all_l_fields =
1490 	    "link,start,end,rbytes,obytes,bandwidth";
1491 	ofmt_handle_t		ofmt;
1492 	ofmt_status_t		oferr;
1493 	uint_t			ofmtflags = 0;
1494 
1495 	bzero(&state, sizeof (show_usage_state_t));
1496 	state.us_parsable = B_FALSE;
1497 	state.us_printheader = B_FALSE;
1498 	state.us_plot = B_FALSE;
1499 	state.us_first = B_TRUE;
1500 
1501 	while ((opt = getopt_long(argc, argv, "das:e:o:f:F:",
1502 	    usage_opts, NULL)) != -1) {
1503 		switch (opt) {
1504 		case 'd':
1505 			d_arg = B_TRUE;
1506 			break;
1507 		case 'a':
1508 			state.us_showall = B_TRUE;
1509 			break;
1510 		case 'f':
1511 			file = optarg;
1512 			break;
1513 		case 's':
1514 			stime = optarg;
1515 			break;
1516 		case 'e':
1517 			etime = optarg;
1518 			break;
1519 		case 'o':
1520 			o_arg = B_TRUE;
1521 			fields_str = optarg;
1522 			break;
1523 		case 'F':
1524 			state.us_plot = F_arg = B_TRUE;
1525 			formatspec_str = optarg;
1526 			break;
1527 		default:
1528 			die_opterr(optopt, opt, use);
1529 			break;
1530 		}
1531 	}
1532 
1533 	if (file == NULL)
1534 		die("show-usage requires a file");
1535 
1536 	if (optind == (argc-1)) {
1537 		uint32_t 	flags;
1538 
1539 		resource = argv[optind];
1540 		if (!state.us_showall &&
1541 		    (((status = dladm_name2info(handle, resource, NULL, &flags,
1542 		    NULL, NULL)) != DLADM_STATUS_OK) ||
1543 		    ((flags & DLADM_OPT_ACTIVE) == 0))) {
1544 			die("invalid link: '%s'", resource);
1545 		}
1546 	}
1547 
1548 	if (F_arg && d_arg)
1549 		die("incompatible -d and -F options");
1550 
1551 	if (F_arg && valid_formatspec(formatspec_str) == B_FALSE)
1552 		die("Format specifier %s not supported", formatspec_str);
1553 
1554 	if (state.us_parsable)
1555 		ofmtflags |= OFMT_PARSABLE;
1556 
1557 	if (resource == NULL && stime == NULL && etime == NULL) {
1558 		oferr = ofmt_open(fields_str, usage_fields, ofmtflags, 0,
1559 		    &ofmt);
1560 	} else {
1561 		if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0))
1562 			fields_str = all_l_fields;
1563 		oferr = ofmt_open(fields_str, usage_l_fields, ofmtflags, 0,
1564 		    &ofmt);
1565 
1566 	}
1567 	dladm_ofmt_check(oferr, state.us_parsable, ofmt);
1568 	state.us_ofmt = ofmt;
1569 
1570 	if (d_arg) {
1571 		/* Print log dates */
1572 		status = dladm_usage_dates(show_usage_date,
1573 		    DLADM_LOGTYPE_LINK, file, resource, &state);
1574 	} else if (resource == NULL && stime == NULL && etime == NULL &&
1575 	    !F_arg) {
1576 		/* Print summary */
1577 		status = dladm_usage_summary(show_usage_res,
1578 		    DLADM_LOGTYPE_LINK, file, &state);
1579 	} else if (resource != NULL) {
1580 		/* Print log entries for named resource */
1581 		status = dladm_walk_usage_res(show_usage_time,
1582 		    DLADM_LOGTYPE_LINK, file, resource, stime, etime, &state);
1583 	} else {
1584 		/* Print time and information for each link */
1585 		status = dladm_walk_usage_time(show_usage_time,
1586 		    DLADM_LOGTYPE_LINK, file, stime, etime, &state);
1587 	}
1588 
1589 	if (status != DLADM_STATUS_OK)
1590 		die_dlerr(status, "show-usage");
1591 	ofmt_close(ofmt);
1592 }
1593 
1594 static void
1595 do_create_aggr(int argc, char *argv[], const char *use)
1596 {
1597 	int			option;
1598 	int			key = 0;
1599 	uint32_t		policy = AGGR_POLICY_L4;
1600 	aggr_lacp_mode_t	lacp_mode = AGGR_LACP_OFF;
1601 	aggr_lacp_timer_t	lacp_timer = AGGR_LACP_TIMER_SHORT;
1602 	dladm_aggr_port_attr_db_t	port[MAXPORT];
1603 	uint_t			n, ndev, nlink;
1604 	uint8_t			mac_addr[ETHERADDRL];
1605 	boolean_t		mac_addr_fixed = B_FALSE;
1606 	boolean_t		P_arg = B_FALSE;
1607 	boolean_t		l_arg = B_FALSE;
1608 	boolean_t		u_arg = B_FALSE;
1609 	boolean_t		T_arg = B_FALSE;
1610 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
1611 	char			*altroot = NULL;
1612 	char			name[MAXLINKNAMELEN];
1613 	char			*devs[MAXPORT];
1614 	char			*links[MAXPORT];
1615 	dladm_status_t		status;
1616 	dladm_status_t		pstatus;
1617 	char			propstr[DLADM_STRSIZE];
1618 	dladm_arg_list_t	*proplist = NULL;
1619 	int			i;
1620 	datalink_id_t		linkid;
1621 
1622 	ndev = nlink = opterr = 0;
1623 	bzero(propstr, DLADM_STRSIZE);
1624 
1625 	while ((option = getopt_long(argc, argv, ":d:l:L:P:R:tfu:T:p:",
1626 	    lopts, NULL)) != -1) {
1627 		switch (option) {
1628 		case 'd':
1629 			if (ndev + nlink >= MAXPORT)
1630 				die("too many ports specified");
1631 
1632 			devs[ndev++] = optarg;
1633 			break;
1634 		case 'P':
1635 			if (P_arg)
1636 				die_optdup(option);
1637 
1638 			P_arg = B_TRUE;
1639 			if (!dladm_aggr_str2policy(optarg, &policy))
1640 				die("invalid policy '%s'", optarg);
1641 			break;
1642 		case 'u':
1643 			if (u_arg)
1644 				die_optdup(option);
1645 
1646 			u_arg = B_TRUE;
1647 			if (!dladm_aggr_str2macaddr(optarg, &mac_addr_fixed,
1648 			    mac_addr))
1649 				die("invalid MAC address '%s'", optarg);
1650 			break;
1651 		case 'l':
1652 			if (isdigit(optarg[strlen(optarg) - 1])) {
1653 
1654 				/*
1655 				 * Ended with digit, possibly a link name.
1656 				 */
1657 				if (ndev + nlink >= MAXPORT)
1658 					die("too many ports specified");
1659 
1660 				links[nlink++] = optarg;
1661 				break;
1662 			}
1663 			/* FALLTHROUGH */
1664 		case 'L':
1665 			if (l_arg)
1666 				die_optdup(option);
1667 
1668 			l_arg = B_TRUE;
1669 			if (!dladm_aggr_str2lacpmode(optarg, &lacp_mode))
1670 				die("invalid LACP mode '%s'", optarg);
1671 			break;
1672 		case 'T':
1673 			if (T_arg)
1674 				die_optdup(option);
1675 
1676 			T_arg = B_TRUE;
1677 			if (!dladm_aggr_str2lacptimer(optarg, &lacp_timer))
1678 				die("invalid LACP timer value '%s'", optarg);
1679 			break;
1680 		case 't':
1681 			flags &= ~DLADM_OPT_PERSIST;
1682 			break;
1683 		case 'f':
1684 			flags |= DLADM_OPT_FORCE;
1685 			break;
1686 		case 'R':
1687 			altroot = optarg;
1688 			break;
1689 		case 'p':
1690 			(void) strlcat(propstr, optarg, DLADM_STRSIZE);
1691 			if (strlcat(propstr, ",", DLADM_STRSIZE) >=
1692 			    DLADM_STRSIZE)
1693 				die("property list too long '%s'", propstr);
1694 			break;
1695 
1696 		default:
1697 			die_opterr(optopt, option, use);
1698 			break;
1699 		}
1700 	}
1701 
1702 	if (ndev + nlink == 0)
1703 		usage();
1704 
1705 	/* get key value or the aggregation name (required last argument) */
1706 	if (optind != (argc-1))
1707 		usage();
1708 
1709 	if (!str2int(argv[optind], &key)) {
1710 		if (strlcpy(name, argv[optind], MAXLINKNAMELEN) >=
1711 		    MAXLINKNAMELEN) {
1712 			die("link name too long '%s'", argv[optind]);
1713 		}
1714 
1715 		if (!dladm_valid_linkname(name))
1716 			die("invalid link name '%s'", argv[optind]);
1717 	} else {
1718 		(void) snprintf(name, MAXLINKNAMELEN, "aggr%d", key);
1719 	}
1720 
1721 	if (altroot != NULL)
1722 		altroot_cmd(altroot, argc, argv);
1723 
1724 	for (n = 0; n < ndev; n++) {
1725 		if ((status = dladm_dev2linkid(handle, devs[n],
1726 		    &port[n].lp_linkid)) != DLADM_STATUS_OK) {
1727 			die_dlerr(status, "invalid dev name '%s'", devs[n]);
1728 		}
1729 	}
1730 
1731 	for (n = 0; n < nlink; n++) {
1732 		if ((status = dladm_name2info(handle, links[n],
1733 		    &port[ndev + n].lp_linkid, NULL, NULL, NULL)) !=
1734 		    DLADM_STATUS_OK) {
1735 			die_dlerr(status, "invalid link name '%s'", links[n]);
1736 		}
1737 	}
1738 
1739 	status = dladm_aggr_create(handle, name, key, ndev + nlink, port,
1740 	    policy, mac_addr_fixed, (const uchar_t *)mac_addr, lacp_mode,
1741 	    lacp_timer, flags);
1742 	if (status != DLADM_STATUS_OK)
1743 		goto done;
1744 
1745 	if (dladm_parse_link_props(propstr, &proplist, B_FALSE)
1746 	    != DLADM_STATUS_OK)
1747 		die("invalid aggregation property");
1748 
1749 	if (proplist == NULL)
1750 		return;
1751 
1752 	status = dladm_name2info(handle, name, &linkid, NULL, NULL, NULL);
1753 	if (status != DLADM_STATUS_OK)
1754 		goto done;
1755 
1756 	for (i = 0; i < proplist->al_count; i++) {
1757 		dladm_arg_info_t	*aip = &proplist->al_info[i];
1758 
1759 		pstatus = dladm_set_linkprop(handle, linkid, aip->ai_name,
1760 		    aip->ai_val, aip->ai_count, flags);
1761 
1762 		if (pstatus != DLADM_STATUS_OK) {
1763 			die_dlerr(pstatus,
1764 			    "aggr creation succeeded but "
1765 			    "could not set property '%s'", aip->ai_name);
1766 		}
1767 	}
1768 done:
1769 	dladm_free_props(proplist);
1770 	if (status != DLADM_STATUS_OK) {
1771 		if (status == DLADM_STATUS_NONOTIF) {
1772 			die("not all links have link up/down detection; must "
1773 			    "use -f (see dladm(1M))");
1774 		} else {
1775 			die_dlerr(status, "create operation failed");
1776 		}
1777 	}
1778 }
1779 
1780 /*
1781  * arg is either the key or the aggr name. Validate it and convert it to
1782  * the linkid if altroot is NULL.
1783  */
1784 static dladm_status_t
1785 i_dladm_aggr_get_linkid(const char *altroot, const char *arg,
1786     datalink_id_t *linkidp, uint32_t flags)
1787 {
1788 	int		key = 0;
1789 	char		*aggr = NULL;
1790 	dladm_status_t	status;
1791 
1792 	if (!str2int(arg, &key))
1793 		aggr = (char *)arg;
1794 
1795 	if (aggr == NULL && key == 0)
1796 		return (DLADM_STATUS_LINKINVAL);
1797 
1798 	if (altroot != NULL)
1799 		return (DLADM_STATUS_OK);
1800 
1801 	if (aggr != NULL) {
1802 		status = dladm_name2info(handle, aggr, linkidp, NULL, NULL,
1803 		    NULL);
1804 	} else {
1805 		status = dladm_key2linkid(handle, key, linkidp, flags);
1806 	}
1807 
1808 	return (status);
1809 }
1810 
1811 static void
1812 do_delete_aggr(int argc, char *argv[], const char *use)
1813 {
1814 	int			option;
1815 	char			*altroot = NULL;
1816 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
1817 	dladm_status_t		status;
1818 	datalink_id_t		linkid;
1819 
1820 	opterr = 0;
1821 	while ((option = getopt_long(argc, argv, ":R:t", lopts, NULL)) != -1) {
1822 		switch (option) {
1823 		case 't':
1824 			flags &= ~DLADM_OPT_PERSIST;
1825 			break;
1826 		case 'R':
1827 			altroot = optarg;
1828 			break;
1829 		default:
1830 			die_opterr(optopt, option, use);
1831 			break;
1832 		}
1833 	}
1834 
1835 	/* get key value or the aggregation name (required last argument) */
1836 	if (optind != (argc-1))
1837 		usage();
1838 
1839 	status = i_dladm_aggr_get_linkid(altroot, argv[optind], &linkid, flags);
1840 	if (status != DLADM_STATUS_OK)
1841 		goto done;
1842 
1843 	if (altroot != NULL)
1844 		altroot_cmd(altroot, argc, argv);
1845 
1846 	status = dladm_aggr_delete(handle, linkid, flags);
1847 done:
1848 	if (status != DLADM_STATUS_OK)
1849 		die_dlerr(status, "delete operation failed");
1850 }
1851 
1852 static void
1853 do_add_aggr(int argc, char *argv[], const char *use)
1854 {
1855 	int			option;
1856 	uint_t			n, ndev, nlink;
1857 	char			*altroot = NULL;
1858 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
1859 	datalink_id_t		linkid;
1860 	dladm_status_t		status;
1861 	dladm_aggr_port_attr_db_t	port[MAXPORT];
1862 	char			*devs[MAXPORT];
1863 	char			*links[MAXPORT];
1864 
1865 	ndev = nlink = opterr = 0;
1866 	while ((option = getopt_long(argc, argv, ":d:l:R:tf", lopts,
1867 	    NULL)) != -1) {
1868 		switch (option) {
1869 		case 'd':
1870 			if (ndev + nlink >= MAXPORT)
1871 				die("too many ports specified");
1872 
1873 			devs[ndev++] = optarg;
1874 			break;
1875 		case 'l':
1876 			if (ndev + nlink >= MAXPORT)
1877 				die("too many ports specified");
1878 
1879 			links[nlink++] = optarg;
1880 			break;
1881 		case 't':
1882 			flags &= ~DLADM_OPT_PERSIST;
1883 			break;
1884 		case 'f':
1885 			flags |= DLADM_OPT_FORCE;
1886 			break;
1887 		case 'R':
1888 			altroot = optarg;
1889 			break;
1890 		default:
1891 			die_opterr(optopt, option, use);
1892 			break;
1893 		}
1894 	}
1895 
1896 	if (ndev + nlink == 0)
1897 		usage();
1898 
1899 	/* get key value or the aggregation name (required last argument) */
1900 	if (optind != (argc-1))
1901 		usage();
1902 
1903 	if ((status = i_dladm_aggr_get_linkid(altroot, argv[optind], &linkid,
1904 	    flags & (DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST))) !=
1905 	    DLADM_STATUS_OK) {
1906 		goto done;
1907 	}
1908 
1909 	if (altroot != NULL)
1910 		altroot_cmd(altroot, argc, argv);
1911 
1912 	for (n = 0; n < ndev; n++) {
1913 		if ((status = dladm_dev2linkid(handle, devs[n],
1914 		    &(port[n].lp_linkid))) != DLADM_STATUS_OK) {
1915 			die_dlerr(status, "invalid <dev> '%s'", devs[n]);
1916 		}
1917 	}
1918 
1919 	for (n = 0; n < nlink; n++) {
1920 		if ((status = dladm_name2info(handle, links[n],
1921 		    &port[n + ndev].lp_linkid, NULL, NULL, NULL)) !=
1922 		    DLADM_STATUS_OK) {
1923 			die_dlerr(status, "invalid <link> '%s'", links[n]);
1924 		}
1925 	}
1926 
1927 	status = dladm_aggr_add(handle, linkid, ndev + nlink, port, flags);
1928 done:
1929 	if (status != DLADM_STATUS_OK) {
1930 		/*
1931 		 * checking DLADM_STATUS_NOTSUP is a temporary workaround
1932 		 * and should be removed once 6399681 is fixed.
1933 		 */
1934 		if (status == DLADM_STATUS_NOTSUP) {
1935 			(void) fprintf(stderr,
1936 			    gettext("%s: add operation failed: %s\n"),
1937 			    progname,
1938 			    gettext("link capabilities don't match"));
1939 			dladm_close(handle);
1940 			exit(ENOTSUP);
1941 		} else if (status == DLADM_STATUS_NONOTIF) {
1942 			die("not all links have link up/down detection; must "
1943 			    "use -f (see dladm(1M))");
1944 		} else {
1945 			die_dlerr(status, "add operation failed");
1946 		}
1947 	}
1948 }
1949 
1950 static void
1951 do_remove_aggr(int argc, char *argv[], const char *use)
1952 {
1953 	int				option;
1954 	dladm_aggr_port_attr_db_t	port[MAXPORT];
1955 	uint_t				n, ndev, nlink;
1956 	char				*devs[MAXPORT];
1957 	char				*links[MAXPORT];
1958 	char				*altroot = NULL;
1959 	uint32_t			flags;
1960 	datalink_id_t			linkid;
1961 	dladm_status_t			status;
1962 
1963 	flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
1964 	ndev = nlink = opterr = 0;
1965 	while ((option = getopt_long(argc, argv, ":d:l:R:t",
1966 	    lopts, NULL)) != -1) {
1967 		switch (option) {
1968 		case 'd':
1969 			if (ndev + nlink >= MAXPORT)
1970 				die("too many ports specified");
1971 
1972 			devs[ndev++] = optarg;
1973 			break;
1974 		case 'l':
1975 			if (ndev + nlink >= MAXPORT)
1976 				die("too many ports specified");
1977 
1978 			links[nlink++] = optarg;
1979 			break;
1980 		case 't':
1981 			flags &= ~DLADM_OPT_PERSIST;
1982 			break;
1983 		case 'R':
1984 			altroot = optarg;
1985 			break;
1986 		default:
1987 			die_opterr(optopt, option, use);
1988 			break;
1989 		}
1990 	}
1991 
1992 	if (ndev + nlink == 0)
1993 		usage();
1994 
1995 	/* get key value or the aggregation name (required last argument) */
1996 	if (optind != (argc-1))
1997 		usage();
1998 
1999 	status = i_dladm_aggr_get_linkid(altroot, argv[optind], &linkid, flags);
2000 	if (status != DLADM_STATUS_OK)
2001 		goto done;
2002 
2003 	if (altroot != NULL)
2004 		altroot_cmd(altroot, argc, argv);
2005 
2006 	for (n = 0; n < ndev; n++) {
2007 		if ((status = dladm_dev2linkid(handle, devs[n],
2008 		    &(port[n].lp_linkid))) != DLADM_STATUS_OK) {
2009 			die_dlerr(status, "invalid <dev> '%s'", devs[n]);
2010 		}
2011 	}
2012 
2013 	for (n = 0; n < nlink; n++) {
2014 		if ((status = dladm_name2info(handle, links[n],
2015 		    &port[n + ndev].lp_linkid, NULL, NULL, NULL)) !=
2016 		    DLADM_STATUS_OK) {
2017 			die_dlerr(status, "invalid <link> '%s'", links[n]);
2018 		}
2019 	}
2020 
2021 	status = dladm_aggr_remove(handle, linkid, ndev + nlink, port, flags);
2022 done:
2023 	if (status != DLADM_STATUS_OK)
2024 		die_dlerr(status, "remove operation failed");
2025 }
2026 
2027 static void
2028 do_modify_aggr(int argc, char *argv[], const char *use)
2029 {
2030 	int			option;
2031 	uint32_t		policy = AGGR_POLICY_L4;
2032 	aggr_lacp_mode_t	lacp_mode = AGGR_LACP_OFF;
2033 	aggr_lacp_timer_t	lacp_timer = AGGR_LACP_TIMER_SHORT;
2034 	uint8_t			mac_addr[ETHERADDRL];
2035 	boolean_t		mac_addr_fixed = B_FALSE;
2036 	uint8_t			modify_mask = 0;
2037 	char			*altroot = NULL;
2038 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
2039 	datalink_id_t		linkid;
2040 	dladm_status_t		status;
2041 
2042 	opterr = 0;
2043 	while ((option = getopt_long(argc, argv, ":L:l:P:R:tu:T:", lopts,
2044 	    NULL)) != -1) {
2045 		switch (option) {
2046 		case 'P':
2047 			if (modify_mask & DLADM_AGGR_MODIFY_POLICY)
2048 				die_optdup(option);
2049 
2050 			modify_mask |= DLADM_AGGR_MODIFY_POLICY;
2051 
2052 			if (!dladm_aggr_str2policy(optarg, &policy))
2053 				die("invalid policy '%s'", optarg);
2054 			break;
2055 		case 'u':
2056 			if (modify_mask & DLADM_AGGR_MODIFY_MAC)
2057 				die_optdup(option);
2058 
2059 			modify_mask |= DLADM_AGGR_MODIFY_MAC;
2060 
2061 			if (!dladm_aggr_str2macaddr(optarg, &mac_addr_fixed,
2062 			    mac_addr))
2063 				die("invalid MAC address '%s'", optarg);
2064 			break;
2065 		case 'l':
2066 		case 'L':
2067 			if (modify_mask & DLADM_AGGR_MODIFY_LACP_MODE)
2068 				die_optdup(option);
2069 
2070 			modify_mask |= DLADM_AGGR_MODIFY_LACP_MODE;
2071 
2072 			if (!dladm_aggr_str2lacpmode(optarg, &lacp_mode))
2073 				die("invalid LACP mode '%s'", optarg);
2074 			break;
2075 		case 'T':
2076 			if (modify_mask & DLADM_AGGR_MODIFY_LACP_TIMER)
2077 				die_optdup(option);
2078 
2079 			modify_mask |= DLADM_AGGR_MODIFY_LACP_TIMER;
2080 
2081 			if (!dladm_aggr_str2lacptimer(optarg, &lacp_timer))
2082 				die("invalid LACP timer value '%s'", optarg);
2083 			break;
2084 		case 't':
2085 			flags &= ~DLADM_OPT_PERSIST;
2086 			break;
2087 		case 'R':
2088 			altroot = optarg;
2089 			break;
2090 		default:
2091 			die_opterr(optopt, option, use);
2092 			break;
2093 		}
2094 	}
2095 
2096 	if (modify_mask == 0)
2097 		die("at least one of the -PulT options must be specified");
2098 
2099 	/* get key value or the aggregation name (required last argument) */
2100 	if (optind != (argc-1))
2101 		usage();
2102 
2103 	status = i_dladm_aggr_get_linkid(altroot, argv[optind], &linkid, flags);
2104 	if (status != DLADM_STATUS_OK)
2105 		goto done;
2106 
2107 	if (altroot != NULL)
2108 		altroot_cmd(altroot, argc, argv);
2109 
2110 	status = dladm_aggr_modify(handle, linkid, modify_mask, policy,
2111 	    mac_addr_fixed, (const uchar_t *)mac_addr, lacp_mode, lacp_timer,
2112 	    flags);
2113 
2114 done:
2115 	if (status != DLADM_STATUS_OK)
2116 		die_dlerr(status, "modify operation failed");
2117 }
2118 
2119 /*ARGSUSED*/
2120 static void
2121 do_up_aggr(int argc, char *argv[], const char *use)
2122 {
2123 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
2124 	dladm_status_t	status;
2125 
2126 	/*
2127 	 * get the key or the name of the aggregation (optional last argument)
2128 	 */
2129 	if (argc == 2) {
2130 		if ((status = i_dladm_aggr_get_linkid(NULL, argv[1], &linkid,
2131 		    DLADM_OPT_PERSIST)) != DLADM_STATUS_OK)
2132 			goto done;
2133 	} else if (argc > 2) {
2134 		usage();
2135 	}
2136 
2137 	status = dladm_aggr_up(handle, linkid);
2138 done:
2139 	if (status != DLADM_STATUS_OK) {
2140 		if (argc == 2) {
2141 			die_dlerr(status,
2142 			    "could not bring up aggregation '%s'", argv[1]);
2143 		} else {
2144 			die_dlerr(status, "could not bring aggregations up");
2145 		}
2146 	}
2147 }
2148 
2149 static void
2150 do_create_vlan(int argc, char *argv[], const char *use)
2151 {
2152 	char			*link = NULL;
2153 	char			drv[DLPI_LINKNAME_MAX];
2154 	uint_t			ppa;
2155 	datalink_id_t		linkid;
2156 	datalink_id_t		dev_linkid;
2157 	int			vid = 0;
2158 	int			option;
2159 	uint32_t		flags = (DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST);
2160 	char			*altroot = NULL;
2161 	char			vlan[MAXLINKNAMELEN];
2162 	char			propstr[DLADM_STRSIZE];
2163 	dladm_arg_list_t	*proplist = NULL;
2164 	dladm_status_t		status;
2165 
2166 	opterr = 0;
2167 	bzero(propstr, DLADM_STRSIZE);
2168 
2169 	while ((option = getopt_long(argc, argv, ":tfR:l:v:p:",
2170 	    lopts, NULL)) != -1) {
2171 		switch (option) {
2172 		case 'v':
2173 			if (vid != 0)
2174 				die_optdup(option);
2175 
2176 			if (!str2int(optarg, &vid) || vid < 1 || vid > 4094)
2177 				die("invalid VLAN identifier '%s'", optarg);
2178 
2179 			break;
2180 		case 'l':
2181 			if (link != NULL)
2182 				die_optdup(option);
2183 
2184 			link = optarg;
2185 			break;
2186 		case 't':
2187 			flags &= ~DLADM_OPT_PERSIST;
2188 			break;
2189 		case 'R':
2190 			altroot = optarg;
2191 			break;
2192 		case 'p':
2193 			(void) strlcat(propstr, optarg, DLADM_STRSIZE);
2194 			if (strlcat(propstr, ",", DLADM_STRSIZE) >=
2195 			    DLADM_STRSIZE)
2196 				die("property list too long '%s'", propstr);
2197 			break;
2198 		case 'f':
2199 			flags |= DLADM_OPT_FORCE;
2200 			break;
2201 		default:
2202 			die_opterr(optopt, option, use);
2203 			break;
2204 		}
2205 	}
2206 
2207 	/* get vlan name if there is any */
2208 	if ((vid == 0) || (link == NULL) || (argc - optind > 1))
2209 		usage();
2210 
2211 	if (optind == (argc - 1)) {
2212 		if (strlcpy(vlan, argv[optind], MAXLINKNAMELEN) >=
2213 		    MAXLINKNAMELEN) {
2214 			die("vlan name too long '%s'", argv[optind]);
2215 		}
2216 	} else {
2217 		if ((dlpi_parselink(link, drv, &ppa) != DLPI_SUCCESS) ||
2218 		    (ppa >= 1000) ||
2219 		    (dlpi_makelink(vlan, drv, vid * 1000 + ppa) !=
2220 		    DLPI_SUCCESS)) {
2221 			die("invalid link name '%s'", link);
2222 		}
2223 	}
2224 
2225 	if (altroot != NULL)
2226 		altroot_cmd(altroot, argc, argv);
2227 
2228 	if (dladm_name2info(handle, link, &dev_linkid, NULL, NULL, NULL) !=
2229 	    DLADM_STATUS_OK) {
2230 		die("invalid link name '%s'", link);
2231 	}
2232 
2233 	if (dladm_parse_link_props(propstr, &proplist, B_FALSE)
2234 	    != DLADM_STATUS_OK)
2235 		die("invalid vlan property");
2236 
2237 	status = dladm_vlan_create(handle, vlan, dev_linkid, vid, proplist,
2238 	    flags, &linkid);
2239 	switch (status) {
2240 	case DLADM_STATUS_OK:
2241 		break;
2242 
2243 	case DLADM_STATUS_NOTSUP:
2244 		die("VLAN over '%s' may require lowered MTU; must use -f (see "
2245 		    "dladm(1M))", link);
2246 		break;
2247 
2248 	case DLADM_STATUS_LINKBUSY:
2249 		die("VLAN over '%s' may not use default_tag ID", link);
2250 		break;
2251 
2252 	default:
2253 		die_dlerr(status, "create operation failed");
2254 	}
2255 }
2256 
2257 static void
2258 do_delete_vlan(int argc, char *argv[], const char *use)
2259 {
2260 	int		option;
2261 	uint32_t	flags = (DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST);
2262 	char		*altroot = NULL;
2263 	datalink_id_t	linkid;
2264 	dladm_status_t	status;
2265 
2266 	opterr = 0;
2267 	while ((option = getopt_long(argc, argv, ":R:t", lopts, NULL)) != -1) {
2268 		switch (option) {
2269 		case 't':
2270 			flags &= ~DLADM_OPT_PERSIST;
2271 			break;
2272 		case 'R':
2273 			altroot = optarg;
2274 			break;
2275 		default:
2276 			die_opterr(optopt, option, use);
2277 			break;
2278 		}
2279 	}
2280 
2281 	/* get VLAN link name (required last argument) */
2282 	if (optind != (argc - 1))
2283 		usage();
2284 
2285 	if (altroot != NULL)
2286 		altroot_cmd(altroot, argc, argv);
2287 
2288 	status = dladm_name2info(handle, argv[optind], &linkid, NULL, NULL,
2289 	    NULL);
2290 	if (status != DLADM_STATUS_OK)
2291 		goto done;
2292 
2293 	status = dladm_vlan_delete(handle, linkid, flags);
2294 done:
2295 	if (status != DLADM_STATUS_OK)
2296 		die_dlerr(status, "delete operation failed");
2297 }
2298 
2299 /*ARGSUSED*/
2300 static void
2301 do_up_vlan(int argc, char *argv[], const char *use)
2302 {
2303 	do_up_vnic_common(argc, argv, use, B_TRUE);
2304 }
2305 
2306 static void
2307 do_rename_link(int argc, char *argv[], const char *use)
2308 {
2309 	int		option;
2310 	char		*link1, *link2;
2311 	char		*altroot = NULL;
2312 	dladm_status_t	status;
2313 
2314 	opterr = 0;
2315 	while ((option = getopt_long(argc, argv, ":R:", lopts, NULL)) != -1) {
2316 		switch (option) {
2317 		case 'R':
2318 			altroot = optarg;
2319 			break;
2320 		default:
2321 			die_opterr(optopt, option, use);
2322 			break;
2323 		}
2324 	}
2325 
2326 	/* get link1 and link2 name (required the last 2 arguments) */
2327 	if (optind != (argc - 2))
2328 		usage();
2329 
2330 	if (altroot != NULL)
2331 		altroot_cmd(altroot, argc, argv);
2332 
2333 	link1 = argv[optind++];
2334 	link2 = argv[optind];
2335 	if ((status = dladm_rename_link(handle, link1, link2)) !=
2336 	    DLADM_STATUS_OK)
2337 		die_dlerr(status, "rename operation failed");
2338 }
2339 
2340 /*ARGSUSED*/
2341 static void
2342 do_delete_phys(int argc, char *argv[], const char *use)
2343 {
2344 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
2345 	dladm_status_t	status;
2346 
2347 	/* get link name (required the last argument) */
2348 	if (argc > 2)
2349 		usage();
2350 
2351 	if (argc == 2) {
2352 		if ((status = dladm_name2info(handle, argv[1], &linkid, NULL,
2353 		    NULL, NULL)) != DLADM_STATUS_OK)
2354 			die_dlerr(status, "cannot delete '%s'", argv[1]);
2355 	}
2356 
2357 	if ((status = dladm_phys_delete(handle, linkid)) != DLADM_STATUS_OK) {
2358 		if (argc == 2)
2359 			die_dlerr(status, "cannot delete '%s'", argv[1]);
2360 		else
2361 			die_dlerr(status, "delete operation failed");
2362 	}
2363 }
2364 
2365 /*ARGSUSED*/
2366 static int
2367 i_dladm_walk_linkmap(dladm_handle_t dh, datalink_id_t linkid, void *arg)
2368 {
2369 	char			name[MAXLINKNAMELEN];
2370 	char			mediabuf[DLADM_STRSIZE];
2371 	char			classbuf[DLADM_STRSIZE];
2372 	datalink_class_t	class;
2373 	uint32_t		media;
2374 	uint32_t		flags;
2375 
2376 	if (dladm_datalink_id2info(dh, linkid, &flags, &class, &media, name,
2377 	    MAXLINKNAMELEN) == DLADM_STATUS_OK) {
2378 		(void) dladm_class2str(class, classbuf);
2379 		(void) dladm_media2str(media, mediabuf);
2380 		(void) printf("%-12s%8d  %-12s%-20s %6d\n", name,
2381 		    linkid, classbuf, mediabuf, flags);
2382 	}
2383 	return (DLADM_WALK_CONTINUE);
2384 }
2385 
2386 /*ARGSUSED*/
2387 static void
2388 do_show_linkmap(int argc, char *argv[], const char *use)
2389 {
2390 	if (argc != 1)
2391 		die("invalid arguments");
2392 
2393 	(void) printf("%-12s%8s  %-12s%-20s %6s\n", "NAME", "LINKID",
2394 	    "CLASS", "MEDIA", "FLAGS");
2395 
2396 	(void) dladm_walk_datalink_id(i_dladm_walk_linkmap, handle, NULL,
2397 	    DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE,
2398 	    DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST);
2399 }
2400 
2401 /*
2402  * Delete inactive physical links.
2403  */
2404 /*ARGSUSED*/
2405 static int
2406 purge_phys(dladm_handle_t dh, datalink_id_t linkid, void *arg)
2407 {
2408 	datalink_class_t	class;
2409 	uint32_t		flags;
2410 
2411 	if (dladm_datalink_id2info(dh, linkid, &flags, &class, NULL, NULL, 0)
2412 	    != DLADM_STATUS_OK) {
2413 		return (DLADM_WALK_CONTINUE);
2414 	}
2415 
2416 	if (class == DATALINK_CLASS_PHYS && !(flags & DLADM_OPT_ACTIVE))
2417 		(void) dladm_phys_delete(dh, linkid);
2418 
2419 	return (DLADM_WALK_CONTINUE);
2420 }
2421 
2422 /*ARGSUSED*/
2423 static void
2424 do_init_phys(int argc, char *argv[], const char *use)
2425 {
2426 	di_node_t	devtree;
2427 
2428 	if (argc > 1)
2429 		usage();
2430 
2431 	/*
2432 	 * Force all the devices to attach, therefore all the network physical
2433 	 * devices can be known to the dlmgmtd daemon.
2434 	 */
2435 	if ((devtree = di_init("/", DINFOFORCE | DINFOSUBTREE)) != DI_NODE_NIL)
2436 		di_fini(devtree);
2437 
2438 	(void) dladm_walk_datalink_id(purge_phys, handle, NULL,
2439 	    DATALINK_CLASS_PHYS, DATALINK_ANY_MEDIATYPE, DLADM_OPT_PERSIST);
2440 }
2441 
2442 
2443 /*
2444  * Print the active topology information.
2445  */
2446 static dladm_status_t
2447 print_link_topology(show_state_t *state, datalink_id_t linkid,
2448     datalink_class_t class, link_fields_buf_t *lbuf)
2449 {
2450 	uint32_t	flags = state->ls_flags;
2451 	dladm_status_t	status;
2452 	char		tmpbuf[MAXLINKNAMELEN];
2453 
2454 	lbuf->link_over[0] = '\0';
2455 	lbuf->link_bridge[0] = '\0';
2456 
2457 	switch (class) {
2458 	case DATALINK_CLASS_AGGR:
2459 	case DATALINK_CLASS_PHYS:
2460 	case DATALINK_CLASS_ETHERSTUB:
2461 		status = dladm_bridge_getlink(handle, linkid, lbuf->link_bridge,
2462 		    sizeof (lbuf->link_bridge));
2463 		if (status == DLADM_STATUS_OK)
2464 			break;
2465 		if (status != DLADM_STATUS_NOTFOUND)
2466 			return (status);
2467 		break;
2468 	}
2469 
2470 	status = DLADM_STATUS_OK;
2471 	switch (class) {
2472 	case DATALINK_CLASS_VLAN: {
2473 		dladm_vlan_attr_t	vinfo;
2474 
2475 		status = dladm_vlan_info(handle, linkid, &vinfo, flags);
2476 		if (status != DLADM_STATUS_OK)
2477 			break;
2478 		status = dladm_datalink_id2info(handle, vinfo.dv_linkid, NULL,
2479 		    NULL, NULL, lbuf->link_over, sizeof (lbuf->link_over));
2480 		break;
2481 	}
2482 
2483 	case DATALINK_CLASS_AGGR: {
2484 		dladm_aggr_grp_attr_t	ginfo;
2485 		int			i;
2486 
2487 		lbuf->link_over[0] = '\0';
2488 
2489 		status = dladm_aggr_info(handle, linkid, &ginfo, flags);
2490 		if (status != DLADM_STATUS_OK)
2491 			break;
2492 
2493 		if (ginfo.lg_nports == 0) {
2494 			status = DLADM_STATUS_BADVAL;
2495 			break;
2496 		}
2497 		for (i = 0; i < ginfo.lg_nports; i++) {
2498 			status = dladm_datalink_id2info(handle,
2499 			    ginfo.lg_ports[i].lp_linkid, NULL, NULL, NULL,
2500 			    tmpbuf, sizeof (tmpbuf));
2501 			if (status != DLADM_STATUS_OK)
2502 				break;
2503 			(void) strlcat(lbuf->link_over, tmpbuf,
2504 			    sizeof (lbuf->link_over));
2505 			if (i != (ginfo.lg_nports - 1)) {
2506 				(void) strlcat(lbuf->link_over, " ",
2507 				    sizeof (lbuf->link_over));
2508 			}
2509 		}
2510 		free(ginfo.lg_ports);
2511 		break;
2512 	}
2513 
2514 	case DATALINK_CLASS_VNIC: {
2515 		dladm_vnic_attr_t	vinfo;
2516 
2517 		status = dladm_vnic_info(handle, linkid, &vinfo, flags);
2518 		if (status == DLADM_STATUS_OK)
2519 			status = dladm_datalink_id2info(handle,
2520 			    vinfo.va_link_id, NULL, NULL, NULL, lbuf->link_over,
2521 			    sizeof (lbuf->link_over));
2522 		break;
2523 	}
2524 
2525 	case DATALINK_CLASS_BRIDGE: {
2526 		datalink_id_t *dlp;
2527 		uint_t i, nports;
2528 
2529 		status = dladm_datalink_id2info(handle, linkid, NULL, NULL,
2530 		    NULL, tmpbuf, sizeof (tmpbuf));
2531 		if (status != DLADM_STATUS_OK)
2532 			break;
2533 		if (tmpbuf[0] != '\0')
2534 			tmpbuf[strlen(tmpbuf) - 1] = '\0';
2535 		dlp = dladm_bridge_get_portlist(tmpbuf, &nports);
2536 		if (dlp == NULL) {
2537 			status = DLADM_STATUS_BADVAL;
2538 			break;
2539 		}
2540 		lbuf->link_over[0] = '\0';
2541 		for (i = 0; i < nports; i++) {
2542 			status = dladm_datalink_id2info(handle, dlp[i], NULL,
2543 			    NULL, NULL, tmpbuf, sizeof (tmpbuf));
2544 			if (status != DLADM_STATUS_OK)
2545 				break;
2546 			(void) strlcat(lbuf->link_over, tmpbuf,
2547 			    sizeof (lbuf->link_over));
2548 			if (i != nports - 1) {
2549 				(void) strlcat(lbuf->link_over, " ",
2550 				    sizeof (lbuf->link_over));
2551 			}
2552 		}
2553 		dladm_bridge_free_portlist(dlp);
2554 		break;
2555 	}
2556 
2557 	case DATALINK_CLASS_SIMNET: {
2558 		dladm_simnet_attr_t	slinfo;
2559 
2560 		status = dladm_simnet_info(handle, linkid, &slinfo, flags);
2561 		if (status == DLADM_STATUS_OK &&
2562 		    slinfo.sna_peer_link_id != DATALINK_INVALID_LINKID)
2563 			status = dladm_datalink_id2info(handle,
2564 			    slinfo.sna_peer_link_id, NULL, NULL, NULL,
2565 			    lbuf->link_over, sizeof (lbuf->link_over));
2566 		break;
2567 	}
2568 	}
2569 
2570 	return (status);
2571 }
2572 
2573 static dladm_status_t
2574 print_link(show_state_t *state, datalink_id_t linkid, link_fields_buf_t *lbuf)
2575 {
2576 	char			link[MAXLINKNAMELEN];
2577 	datalink_class_t	class;
2578 	uint_t			mtu;
2579 	uint32_t		flags;
2580 	dladm_status_t		status;
2581 
2582 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, &class,
2583 	    NULL, link, sizeof (link))) != DLADM_STATUS_OK) {
2584 		goto done;
2585 	}
2586 
2587 	if (!(state->ls_flags & flags)) {
2588 		status = DLADM_STATUS_NOTFOUND;
2589 		goto done;
2590 	}
2591 
2592 	if (state->ls_flags == DLADM_OPT_ACTIVE) {
2593 		dladm_attr_t	dlattr;
2594 
2595 		if (class == DATALINK_CLASS_PHYS) {
2596 			dladm_phys_attr_t	dpa;
2597 			dlpi_handle_t		dh;
2598 			dlpi_info_t		dlinfo;
2599 
2600 			if ((status = dladm_phys_info(handle, linkid, &dpa,
2601 			    DLADM_OPT_ACTIVE)) != DLADM_STATUS_OK) {
2602 				goto done;
2603 			}
2604 
2605 			if (!dpa.dp_novanity)
2606 				goto link_mtu;
2607 
2608 			/*
2609 			 * This is a physical link that does not have
2610 			 * vanity naming support.
2611 			 */
2612 			if (dlpi_open(dpa.dp_dev, &dh, DLPI_DEVONLY) !=
2613 			    DLPI_SUCCESS) {
2614 				status = DLADM_STATUS_NOTFOUND;
2615 				goto done;
2616 			}
2617 
2618 			if (dlpi_info(dh, &dlinfo, 0) != DLPI_SUCCESS) {
2619 				dlpi_close(dh);
2620 				status = DLADM_STATUS_BADARG;
2621 				goto done;
2622 			}
2623 
2624 			dlpi_close(dh);
2625 			mtu = dlinfo.di_max_sdu;
2626 		} else {
2627 link_mtu:
2628 			status = dladm_info(handle, linkid, &dlattr);
2629 			if (status != DLADM_STATUS_OK)
2630 				goto done;
2631 			mtu = dlattr.da_max_sdu;
2632 		}
2633 	}
2634 
2635 	(void) snprintf(lbuf->link_name, sizeof (lbuf->link_name),
2636 	    "%s", link);
2637 	(void) dladm_class2str(class, lbuf->link_class);
2638 	if (state->ls_flags == DLADM_OPT_ACTIVE) {
2639 		(void) snprintf(lbuf->link_mtu, sizeof (lbuf->link_mtu),
2640 		    "%u", mtu);
2641 		(void) get_linkstate(link, B_TRUE, lbuf->link_state);
2642 	}
2643 
2644 	status = print_link_topology(state, linkid, class, lbuf);
2645 	if (status != DLADM_STATUS_OK)
2646 		goto done;
2647 
2648 done:
2649 	return (status);
2650 }
2651 
2652 /* ARGSUSED */
2653 static int
2654 show_link(dladm_handle_t dh, datalink_id_t linkid, void *arg)
2655 {
2656 	show_state_t		*state = (show_state_t *)arg;
2657 	dladm_status_t		status;
2658 	link_fields_buf_t	lbuf;
2659 
2660 	/*
2661 	 * first get all the link attributes into lbuf;
2662 	 */
2663 	bzero(&lbuf, sizeof (link_fields_buf_t));
2664 	status = print_link(state, linkid, &lbuf);
2665 
2666 	if (status != DLADM_STATUS_OK)
2667 		goto done;
2668 
2669 	ofmt_print(state->ls_ofmt, &lbuf);
2670 
2671 done:
2672 	state->ls_status = status;
2673 	return (DLADM_WALK_CONTINUE);
2674 }
2675 
2676 static boolean_t
2677 print_link_stats_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
2678 {
2679 	link_args_t *largs = ofarg->ofmt_cbarg;
2680 	pktsum_t *diff_stats = largs->link_s_psum;
2681 
2682 	switch (ofarg->ofmt_id) {
2683 	case LINK_S_LINK:
2684 		(void) snprintf(buf, bufsize, "%s", largs->link_s_link);
2685 		break;
2686 	case LINK_S_IPKTS:
2687 		(void) snprintf(buf, bufsize, "%llu", diff_stats->ipackets);
2688 		break;
2689 	case LINK_S_RBYTES:
2690 		(void) snprintf(buf, bufsize, "%llu", diff_stats->rbytes);
2691 		break;
2692 	case LINK_S_IERRORS:
2693 		(void) snprintf(buf, bufsize, "%u", diff_stats->ierrors);
2694 		break;
2695 	case LINK_S_OPKTS:
2696 		(void) snprintf(buf, bufsize, "%llu", diff_stats->opackets);
2697 		break;
2698 	case LINK_S_OBYTES:
2699 		(void) snprintf(buf, bufsize, "%llu", diff_stats->obytes);
2700 		break;
2701 	case LINK_S_OERRORS:
2702 		(void) snprintf(buf, bufsize, "%u", diff_stats->oerrors);
2703 		break;
2704 	default:
2705 		die("invalid input");
2706 		break;
2707 	}
2708 	return (B_TRUE);
2709 }
2710 
2711 static int
2712 show_link_stats(dladm_handle_t dh, datalink_id_t linkid, void *arg)
2713 {
2714 	char			link[DLPI_LINKNAME_MAX];
2715 	datalink_class_t	class;
2716 	show_state_t		*state = (show_state_t *)arg;
2717 	pktsum_t		stats, diff_stats;
2718 	dladm_phys_attr_t	dpa;
2719 	link_args_t		largs;
2720 
2721 	if (state->ls_firstonly) {
2722 		if (state->ls_donefirst)
2723 			return (DLADM_WALK_CONTINUE);
2724 		state->ls_donefirst = B_TRUE;
2725 	} else {
2726 		bzero(&state->ls_prevstats, sizeof (state->ls_prevstats));
2727 	}
2728 
2729 	if (dladm_datalink_id2info(dh, linkid, NULL, &class, NULL, link,
2730 	    DLPI_LINKNAME_MAX) != DLADM_STATUS_OK) {
2731 		return (DLADM_WALK_CONTINUE);
2732 	}
2733 
2734 	if (class == DATALINK_CLASS_PHYS) {
2735 		if (dladm_phys_info(dh, linkid, &dpa, DLADM_OPT_ACTIVE) !=
2736 		    DLADM_STATUS_OK) {
2737 			return (DLADM_WALK_CONTINUE);
2738 		}
2739 		if (dpa.dp_novanity)
2740 			get_mac_stats(dpa.dp_dev, &stats);
2741 		else
2742 			get_link_stats(link, &stats);
2743 	} else {
2744 		get_link_stats(link, &stats);
2745 	}
2746 	dladm_stats_diff(&diff_stats, &stats, &state->ls_prevstats);
2747 
2748 	largs.link_s_link = link;
2749 	largs.link_s_psum = &diff_stats;
2750 	ofmt_print(state->ls_ofmt, &largs);
2751 
2752 	state->ls_prevstats = stats;
2753 	return (DLADM_WALK_CONTINUE);
2754 }
2755 
2756 
2757 static dladm_status_t
2758 print_aggr_info(show_grp_state_t *state, const char *link,
2759     dladm_aggr_grp_attr_t *ginfop)
2760 {
2761 	char			addr_str[ETHERADDRL * 3];
2762 	laggr_fields_buf_t	lbuf;
2763 
2764 	(void) snprintf(lbuf.laggr_name, sizeof (lbuf.laggr_name),
2765 	    "%s", link);
2766 
2767 	(void) dladm_aggr_policy2str(ginfop->lg_policy,
2768 	    lbuf.laggr_policy);
2769 
2770 	if (ginfop->lg_mac_fixed) {
2771 		(void) dladm_aggr_macaddr2str(ginfop->lg_mac, addr_str);
2772 		(void) snprintf(lbuf.laggr_addrpolicy,
2773 		    sizeof (lbuf.laggr_addrpolicy), "fixed (%s)", addr_str);
2774 	} else {
2775 		(void) snprintf(lbuf.laggr_addrpolicy,
2776 		    sizeof (lbuf.laggr_addrpolicy), "auto");
2777 	}
2778 
2779 
2780 	(void) dladm_aggr_lacpmode2str(ginfop->lg_lacp_mode,
2781 	    lbuf.laggr_lacpactivity);
2782 	(void) dladm_aggr_lacptimer2str(ginfop->lg_lacp_timer,
2783 	    lbuf.laggr_lacptimer);
2784 	(void) snprintf(lbuf.laggr_flags, sizeof (lbuf.laggr_flags), "%c----",
2785 	    ginfop->lg_force ? 'f' : '-');
2786 
2787 	ofmt_print(state->gs_ofmt, &lbuf);
2788 
2789 	return (DLADM_STATUS_OK);
2790 }
2791 
2792 static boolean_t
2793 print_xaggr_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
2794 {
2795 	const laggr_args_t 	*l = ofarg->ofmt_cbarg;
2796 	int 			portnum;
2797 	boolean_t		is_port = (l->laggr_lport >= 0);
2798 	static char		tmpbuf[DLADM_STRSIZE];
2799 	dladm_aggr_port_attr_t *portp;
2800 	dladm_phys_attr_t	dpa;
2801 	dladm_status_t		*stat, status = DLADM_STATUS_OK;
2802 
2803 	stat = l->laggr_status;
2804 
2805 	if (is_port) {
2806 		portnum = l->laggr_lport;
2807 		portp = &(l->laggr_ginfop->lg_ports[portnum]);
2808 		if ((status = dladm_datalink_id2info(handle,
2809 		    portp->lp_linkid, NULL, NULL, NULL, buf, bufsize)) !=
2810 		    DLADM_STATUS_OK) {
2811 			goto err;
2812 		}
2813 
2814 		if ((status = dladm_phys_info(handle, portp->lp_linkid,
2815 		    &dpa, DLADM_OPT_ACTIVE)) != DLADM_STATUS_OK) {
2816 			goto err;
2817 		}
2818 	}
2819 
2820 	switch (ofarg->ofmt_id) {
2821 	case AGGR_X_LINK:
2822 		(void) snprintf(buf, bufsize, "%s",
2823 		    (is_port && !l->laggr_parsable ? " " : l->laggr_link));
2824 		break;
2825 	case AGGR_X_PORT:
2826 		if (is_port)
2827 			break;
2828 		*stat = DLADM_STATUS_OK;
2829 		return (B_TRUE);
2830 
2831 	case AGGR_X_SPEED:
2832 		if (is_port) {
2833 			(void) snprintf(buf, bufsize, "%uMb",
2834 			    (uint_t)((get_ifspeed(dpa.dp_dev,
2835 			    B_FALSE)) / 1000000ull));
2836 		} else {
2837 			(void) snprintf(buf, bufsize, "%uMb",
2838 			    (uint_t)((get_ifspeed(l->laggr_link,
2839 			    B_TRUE)) / 1000000ull));
2840 		}
2841 		break;
2842 
2843 	case AGGR_X_DUPLEX:
2844 		if (is_port)
2845 			(void) get_linkduplex(dpa.dp_dev, B_FALSE, tmpbuf);
2846 		else
2847 			(void) get_linkduplex(l->laggr_link, B_TRUE, tmpbuf);
2848 		(void) strlcpy(buf, tmpbuf, bufsize);
2849 		break;
2850 
2851 	case AGGR_X_STATE:
2852 		if (is_port)
2853 			(void) get_linkstate(dpa.dp_dev,  B_FALSE, tmpbuf);
2854 		else
2855 			(void) get_linkstate(l->laggr_link, B_TRUE, tmpbuf);
2856 		(void) strlcpy(buf, tmpbuf, bufsize);
2857 		break;
2858 	case AGGR_X_ADDRESS:
2859 		(void) dladm_aggr_macaddr2str(
2860 		    (is_port ? portp->lp_mac : l->laggr_ginfop->lg_mac),
2861 		    tmpbuf);
2862 		(void) strlcpy(buf, tmpbuf, bufsize);
2863 		break;
2864 	case AGGR_X_PORTSTATE:
2865 		if (is_port) {
2866 			(void) dladm_aggr_portstate2str(portp->lp_state,
2867 			    tmpbuf);
2868 			(void) strlcpy(buf, tmpbuf, bufsize);
2869 		}
2870 		break;
2871 	}
2872 err:
2873 	*stat = status;
2874 	return (B_TRUE);
2875 }
2876 
2877 static dladm_status_t
2878 print_aggr_extended(show_grp_state_t *state, const char *link,
2879     dladm_aggr_grp_attr_t *ginfop)
2880 {
2881 	int			i;
2882 	dladm_status_t		status;
2883 	laggr_args_t		largs;
2884 
2885 	largs.laggr_lport = -1;
2886 	largs.laggr_link = link;
2887 	largs.laggr_ginfop = ginfop;
2888 	largs.laggr_status = &status;
2889 	largs.laggr_parsable = state->gs_parsable;
2890 
2891 	ofmt_print(state->gs_ofmt, &largs);
2892 
2893 	if (status != DLADM_STATUS_OK)
2894 		goto done;
2895 
2896 	for (i = 0; i < ginfop->lg_nports; i++) {
2897 		largs.laggr_lport = i;
2898 		ofmt_print(state->gs_ofmt, &largs);
2899 		if (status != DLADM_STATUS_OK)
2900 			goto done;
2901 	}
2902 
2903 	status = DLADM_STATUS_OK;
2904 done:
2905 	return (status);
2906 }
2907 
2908 static boolean_t
2909 print_lacp_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
2910 {
2911 	const laggr_args_t	*l = ofarg->ofmt_cbarg;
2912 	int			portnum;
2913 	boolean_t		is_port = (l->laggr_lport >= 0);
2914 	dladm_aggr_port_attr_t	*portp;
2915 	dladm_status_t		*stat, status;
2916 	aggr_lacp_state_t	*lstate;
2917 
2918 	if (!is_port) {
2919 		return (B_FALSE); /* cannot happen! */
2920 	}
2921 
2922 	stat = l->laggr_status;
2923 
2924 	portnum = l->laggr_lport;
2925 	portp = &(l->laggr_ginfop->lg_ports[portnum]);
2926 
2927 	if ((status = dladm_datalink_id2info(handle, portp->lp_linkid,
2928 	    NULL, NULL, NULL, buf, bufsize)) != DLADM_STATUS_OK) {
2929 			goto err;
2930 	}
2931 	lstate = &(portp->lp_lacp_state);
2932 
2933 	switch (ofarg->ofmt_id) {
2934 	case AGGR_L_LINK:
2935 		(void) snprintf(buf, bufsize, "%s",
2936 		    (portnum > 0 ? "" : l->laggr_link));
2937 		break;
2938 
2939 	case AGGR_L_PORT:
2940 		/*
2941 		 * buf already contains portname as a result of the
2942 		 * earlier call to dladm_datalink_id2info().
2943 		 */
2944 		break;
2945 
2946 	case AGGR_L_AGGREGATABLE:
2947 		(void) snprintf(buf, bufsize, "%s",
2948 		    (lstate->bit.aggregation ? "yes" : "no"));
2949 		break;
2950 
2951 	case AGGR_L_SYNC:
2952 		(void) snprintf(buf, bufsize, "%s",
2953 		    (lstate->bit.sync ? "yes" : "no"));
2954 		break;
2955 
2956 	case AGGR_L_COLL:
2957 		(void) snprintf(buf, bufsize, "%s",
2958 		    (lstate->bit.collecting ? "yes" : "no"));
2959 		break;
2960 
2961 	case AGGR_L_DIST:
2962 		(void) snprintf(buf, bufsize, "%s",
2963 		    (lstate->bit.distributing ? "yes" : "no"));
2964 		break;
2965 
2966 	case AGGR_L_DEFAULTED:
2967 		(void) snprintf(buf, bufsize, "%s",
2968 		    (lstate->bit.defaulted ? "yes" : "no"));
2969 		break;
2970 
2971 	case AGGR_L_EXPIRED:
2972 		(void) snprintf(buf, bufsize, "%s",
2973 		    (lstate->bit.expired ? "yes" : "no"));
2974 		break;
2975 	}
2976 
2977 	*stat = DLADM_STATUS_OK;
2978 	return (B_TRUE);
2979 
2980 err:
2981 	*stat = status;
2982 	return (B_TRUE);
2983 }
2984 
2985 static dladm_status_t
2986 print_aggr_lacp(show_grp_state_t *state, const char *link,
2987     dladm_aggr_grp_attr_t *ginfop)
2988 {
2989 	int		i;
2990 	dladm_status_t	status;
2991 	laggr_args_t	largs;
2992 
2993 	largs.laggr_link = link;
2994 	largs.laggr_ginfop = ginfop;
2995 	largs.laggr_status = &status;
2996 
2997 	for (i = 0; i < ginfop->lg_nports; i++) {
2998 		largs.laggr_lport = i;
2999 		ofmt_print(state->gs_ofmt, &largs);
3000 		if (status != DLADM_STATUS_OK)
3001 			goto done;
3002 	}
3003 
3004 	status = DLADM_STATUS_OK;
3005 done:
3006 	return (status);
3007 }
3008 
3009 static boolean_t
3010 print_aggr_stats_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
3011 {
3012 	const laggr_args_t	*l = ofarg->ofmt_cbarg;
3013 	int 			portnum;
3014 	boolean_t		is_port = (l->laggr_lport >= 0);
3015 	dladm_aggr_port_attr_t	*portp;
3016 	dladm_status_t		*stat, status;
3017 	pktsum_t		*diff_stats;
3018 
3019 	stat = l->laggr_status;
3020 	*stat = DLADM_STATUS_OK;
3021 
3022 	if (is_port) {
3023 		portnum = l->laggr_lport;
3024 		portp = &(l->laggr_ginfop->lg_ports[portnum]);
3025 
3026 		if ((status = dladm_datalink_id2info(handle,
3027 		    portp->lp_linkid, NULL, NULL, NULL, buf, bufsize)) !=
3028 		    DLADM_STATUS_OK) {
3029 			goto err;
3030 		}
3031 		diff_stats = l->laggr_diffstats;
3032 	}
3033 
3034 	switch (ofarg->ofmt_id) {
3035 	case AGGR_S_LINK:
3036 		(void) snprintf(buf, bufsize, "%s",
3037 		    (is_port ? "" : l->laggr_link));
3038 		break;
3039 	case AGGR_S_PORT:
3040 		/*
3041 		 * if (is_port), buf has port name. Otherwise we print
3042 		 * STR_UNDEF_VAL
3043 		 */
3044 		break;
3045 
3046 	case AGGR_S_IPKTS:
3047 		if (is_port) {
3048 			(void) snprintf(buf, bufsize, "%llu",
3049 			    diff_stats->ipackets);
3050 		} else {
3051 			(void) snprintf(buf, bufsize, "%llu",
3052 			    l->laggr_pktsumtot->ipackets);
3053 		}
3054 		break;
3055 
3056 	case AGGR_S_RBYTES:
3057 		if (is_port) {
3058 			(void) snprintf(buf, bufsize, "%llu",
3059 			    diff_stats->rbytes);
3060 		} else {
3061 			(void) snprintf(buf, bufsize, "%llu",
3062 			    l->laggr_pktsumtot->rbytes);
3063 		}
3064 		break;
3065 
3066 	case AGGR_S_OPKTS:
3067 		if (is_port) {
3068 			(void) snprintf(buf, bufsize, "%llu",
3069 			    diff_stats->opackets);
3070 		} else {
3071 			(void) snprintf(buf, bufsize, "%llu",
3072 			    l->laggr_pktsumtot->opackets);
3073 		}
3074 		break;
3075 	case AGGR_S_OBYTES:
3076 		if (is_port) {
3077 			(void) snprintf(buf, bufsize, "%llu",
3078 			    diff_stats->obytes);
3079 		} else {
3080 			(void) snprintf(buf, bufsize, "%llu",
3081 			    l->laggr_pktsumtot->obytes);
3082 		}
3083 		break;
3084 
3085 	case AGGR_S_IPKTDIST:
3086 		if (is_port) {
3087 			(void) snprintf(buf, bufsize, "%-6.1f",
3088 			    (double)diff_stats->ipackets/
3089 			    (double)l->laggr_pktsumtot->ipackets * 100);
3090 		}
3091 		break;
3092 	case AGGR_S_OPKTDIST:
3093 		if (is_port) {
3094 			(void) snprintf(buf, bufsize, "%-6.1f",
3095 			    (double)diff_stats->opackets/
3096 			    (double)l->laggr_pktsumtot->opackets * 100);
3097 		}
3098 		break;
3099 	}
3100 	return (B_TRUE);
3101 
3102 err:
3103 	*stat = status;
3104 	return (B_TRUE);
3105 }
3106 
3107 static dladm_status_t
3108 print_aggr_stats(show_grp_state_t *state, const char *link,
3109     dladm_aggr_grp_attr_t *ginfop)
3110 {
3111 	dladm_phys_attr_t	dpa;
3112 	dladm_aggr_port_attr_t	*portp;
3113 	pktsum_t		pktsumtot, *port_stat;
3114 	dladm_status_t		status;
3115 	int			i;
3116 	laggr_args_t		largs;
3117 
3118 	/* sum the ports statistics */
3119 	bzero(&pktsumtot, sizeof (pktsumtot));
3120 
3121 	/* Allocate memory to keep stats of each port */
3122 	port_stat = malloc(ginfop->lg_nports * sizeof (pktsum_t));
3123 	if (port_stat == NULL) {
3124 		/* Bail out; no memory */
3125 		return (DLADM_STATUS_NOMEM);
3126 	}
3127 
3128 
3129 	for (i = 0; i < ginfop->lg_nports; i++) {
3130 
3131 		portp = &(ginfop->lg_ports[i]);
3132 		if ((status = dladm_phys_info(handle, portp->lp_linkid, &dpa,
3133 		    DLADM_OPT_ACTIVE)) != DLADM_STATUS_OK) {
3134 			goto done;
3135 		}
3136 
3137 		get_mac_stats(dpa.dp_dev, &port_stat[i]);
3138 
3139 		/*
3140 		 * Let's re-use gs_prevstats[] to store the difference of the
3141 		 * counters since last use. We will store the new stats from
3142 		 * port_stat[] once we have the stats displayed.
3143 		 */
3144 
3145 		dladm_stats_diff(&state->gs_prevstats[i], &port_stat[i],
3146 		    &state->gs_prevstats[i]);
3147 		dladm_stats_total(&pktsumtot, &pktsumtot,
3148 		    &state->gs_prevstats[i]);
3149 	}
3150 
3151 	largs.laggr_lport = -1;
3152 	largs.laggr_link = link;
3153 	largs.laggr_ginfop = ginfop;
3154 	largs.laggr_status = &status;
3155 	largs.laggr_pktsumtot = &pktsumtot;
3156 
3157 	ofmt_print(state->gs_ofmt, &largs);
3158 
3159 	if (status != DLADM_STATUS_OK)
3160 		goto done;
3161 
3162 	for (i = 0; i < ginfop->lg_nports; i++) {
3163 		largs.laggr_lport = i;
3164 		largs.laggr_diffstats = &state->gs_prevstats[i];
3165 		ofmt_print(state->gs_ofmt, &largs);
3166 		if (status != DLADM_STATUS_OK)
3167 			goto done;
3168 	}
3169 
3170 	status = DLADM_STATUS_OK;
3171 	for (i = 0; i < ginfop->lg_nports; i++)
3172 		state->gs_prevstats[i] = port_stat[i];
3173 
3174 done:
3175 	free(port_stat);
3176 	return (status);
3177 }
3178 
3179 static dladm_status_t
3180 print_aggr(show_grp_state_t *state, datalink_id_t linkid)
3181 {
3182 	char			link[MAXLINKNAMELEN];
3183 	dladm_aggr_grp_attr_t	ginfo;
3184 	uint32_t		flags;
3185 	dladm_status_t		status;
3186 
3187 	bzero(&ginfo, sizeof (dladm_aggr_grp_attr_t));
3188 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, NULL,
3189 	    NULL, link, MAXLINKNAMELEN)) != DLADM_STATUS_OK) {
3190 		return (status);
3191 	}
3192 
3193 	if (!(state->gs_flags & flags))
3194 		return (DLADM_STATUS_NOTFOUND);
3195 
3196 	status = dladm_aggr_info(handle, linkid, &ginfo, state->gs_flags);
3197 	if (status != DLADM_STATUS_OK)
3198 		return (status);
3199 
3200 	if (state->gs_lacp)
3201 		status = print_aggr_lacp(state, link, &ginfo);
3202 	else if (state->gs_extended)
3203 		status = print_aggr_extended(state, link, &ginfo);
3204 	else if (state->gs_stats)
3205 		status = print_aggr_stats(state, link, &ginfo);
3206 	else
3207 		status = print_aggr_info(state, link, &ginfo);
3208 
3209 done:
3210 	free(ginfo.lg_ports);
3211 	return (status);
3212 }
3213 
3214 /* ARGSUSED */
3215 static int
3216 show_aggr(dladm_handle_t dh, datalink_id_t linkid, void *arg)
3217 {
3218 	show_grp_state_t	*state = arg;
3219 
3220 	state->gs_status = print_aggr(state, linkid);
3221 	return (DLADM_WALK_CONTINUE);
3222 }
3223 
3224 static void
3225 do_show_link(int argc, char *argv[], const char *use)
3226 {
3227 	int		option;
3228 	boolean_t	s_arg = B_FALSE;
3229 	boolean_t	S_arg = B_FALSE;
3230 	boolean_t	i_arg = B_FALSE;
3231 	uint32_t	flags = DLADM_OPT_ACTIVE;
3232 	boolean_t	p_arg = B_FALSE;
3233 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
3234 	char		linkname[MAXLINKNAMELEN];
3235 	uint32_t	interval = 0;
3236 	show_state_t	state;
3237 	dladm_status_t	status;
3238 	boolean_t	o_arg = B_FALSE;
3239 	char		*fields_str = NULL;
3240 	char		*all_active_fields = "link,class,mtu,state,bridge,over";
3241 	char		*all_inactive_fields = "link,class,bridge,over";
3242 	char		*allstat_fields =
3243 	    "link,ipackets,rbytes,ierrors,opackets,obytes,oerrors";
3244 	ofmt_handle_t	ofmt;
3245 	ofmt_status_t	oferr;
3246 	uint_t		ofmtflags = 0;
3247 
3248 	bzero(&state, sizeof (state));
3249 
3250 	opterr = 0;
3251 	while ((option = getopt_long(argc, argv, ":pPsSi:o:",
3252 	    show_lopts, NULL)) != -1) {
3253 		switch (option) {
3254 		case 'p':
3255 			if (p_arg)
3256 				die_optdup(option);
3257 
3258 			p_arg = B_TRUE;
3259 			break;
3260 		case 's':
3261 			if (s_arg)
3262 				die_optdup(option);
3263 
3264 			s_arg = B_TRUE;
3265 			break;
3266 		case 'P':
3267 			if (flags != DLADM_OPT_ACTIVE)
3268 				die_optdup(option);
3269 
3270 			flags = DLADM_OPT_PERSIST;
3271 			break;
3272 		case 'S':
3273 			if (S_arg)
3274 				die_optdup(option);
3275 
3276 			S_arg = B_TRUE;
3277 			break;
3278 		case 'o':
3279 			o_arg = B_TRUE;
3280 			fields_str = optarg;
3281 			break;
3282 		case 'i':
3283 			if (i_arg)
3284 				die_optdup(option);
3285 
3286 			i_arg = B_TRUE;
3287 			if (!dladm_str2interval(optarg, &interval))
3288 				die("invalid interval value '%s'", optarg);
3289 			break;
3290 		default:
3291 			die_opterr(optopt, option, use);
3292 			break;
3293 		}
3294 	}
3295 
3296 	if (i_arg && !(s_arg || S_arg))
3297 		die("the option -i can be used only with -s or -S");
3298 
3299 	if (s_arg && S_arg)
3300 		die("the -s option cannot be used with -S");
3301 
3302 	if (s_arg && flags != DLADM_OPT_ACTIVE)
3303 		die("the option -P cannot be used with -s");
3304 
3305 	if (S_arg && (p_arg || flags != DLADM_OPT_ACTIVE))
3306 		die("the option -%c cannot be used with -S", p_arg ? 'p' : 'P');
3307 
3308 	/* get link name (optional last argument) */
3309 	if (optind == (argc-1)) {
3310 		uint32_t	f;
3311 
3312 		if (strlcpy(linkname, argv[optind], MAXLINKNAMELEN)
3313 		    >= MAXLINKNAMELEN) {
3314 			(void) fprintf(stderr,
3315 			    gettext("%s: link name too long\n"),
3316 			    progname);
3317 			dladm_close(handle);
3318 			exit(1);
3319 		}
3320 		if ((status = dladm_name2info(handle, linkname, &linkid, &f,
3321 		    NULL, NULL)) != DLADM_STATUS_OK) {
3322 			die_dlerr(status, "link %s is not valid", linkname);
3323 		}
3324 
3325 		if (!(f & flags)) {
3326 			die_dlerr(DLADM_STATUS_BADARG, "link %s is %s",
3327 			    argv[optind], flags == DLADM_OPT_PERSIST ?
3328 			    "a temporary link" : "temporarily removed");
3329 		}
3330 	} else if (optind != argc) {
3331 		usage();
3332 	}
3333 
3334 	if (p_arg && !o_arg)
3335 		die("-p requires -o");
3336 
3337 	if (S_arg) {
3338 		dladm_continuous(handle, linkid, NULL, interval, LINK_REPORT);
3339 		return;
3340 	}
3341 
3342 	if (p_arg && strcasecmp(fields_str, "all") == 0)
3343 		die("\"-o all\" is invalid with -p");
3344 
3345 	if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0)) {
3346 		if (s_arg)
3347 			fields_str = allstat_fields;
3348 		else if (flags & DLADM_OPT_ACTIVE)
3349 			fields_str = all_active_fields;
3350 		else
3351 			fields_str = all_inactive_fields;
3352 	}
3353 
3354 	state.ls_parsable = p_arg;
3355 	state.ls_flags = flags;
3356 	state.ls_donefirst = B_FALSE;
3357 
3358 	if (s_arg) {
3359 		link_stats(linkid, interval, fields_str, &state);
3360 		return;
3361 	}
3362 	if (state.ls_parsable)
3363 		ofmtflags |= OFMT_PARSABLE;
3364 	oferr = ofmt_open(fields_str, link_fields, ofmtflags, 0, &ofmt);
3365 	dladm_ofmt_check(oferr, state.ls_parsable, ofmt);
3366 	state.ls_ofmt = ofmt;
3367 
3368 	if (linkid == DATALINK_ALL_LINKID) {
3369 		(void) dladm_walk_datalink_id(show_link, handle, &state,
3370 		    DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE, flags);
3371 	} else {
3372 		(void) show_link(handle, linkid, &state);
3373 		if (state.ls_status != DLADM_STATUS_OK) {
3374 			die_dlerr(state.ls_status, "failed to show link %s",
3375 			    argv[optind]);
3376 		}
3377 	}
3378 	ofmt_close(ofmt);
3379 }
3380 
3381 static void
3382 do_show_aggr(int argc, char *argv[], const char *use)
3383 {
3384 	boolean_t		L_arg = B_FALSE;
3385 	boolean_t		s_arg = B_FALSE;
3386 	boolean_t		i_arg = B_FALSE;
3387 	boolean_t		p_arg = B_FALSE;
3388 	boolean_t		x_arg = B_FALSE;
3389 	show_grp_state_t	state;
3390 	uint32_t		flags = DLADM_OPT_ACTIVE;
3391 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
3392 	int			option;
3393 	uint32_t		interval = 0;
3394 	int			key;
3395 	dladm_status_t		status;
3396 	boolean_t		o_arg = B_FALSE;
3397 	char			*fields_str = NULL;
3398 	char			*all_fields =
3399 	    "link,policy,addrpolicy,lacpactivity,lacptimer,flags";
3400 	char			*all_lacp_fields =
3401 	    "link,port,aggregatable,sync,coll,dist,defaulted,expired";
3402 	char			*all_stats_fields =
3403 	    "link,port,ipackets,rbytes,opackets,obytes,ipktdist,opktdist";
3404 	char			*all_extended_fields =
3405 	    "link,port,speed,duplex,state,address,portstate";
3406 	ofmt_field_t		*pf;
3407 	ofmt_handle_t		ofmt;
3408 	ofmt_status_t		oferr;
3409 	uint_t			ofmtflags = 0;
3410 
3411 	opterr = 0;
3412 	while ((option = getopt_long(argc, argv, ":LpPxsi:o:",
3413 	    show_lopts, NULL)) != -1) {
3414 		switch (option) {
3415 		case 'L':
3416 			if (L_arg)
3417 				die_optdup(option);
3418 
3419 			L_arg = B_TRUE;
3420 			break;
3421 		case 'p':
3422 			if (p_arg)
3423 				die_optdup(option);
3424 
3425 			p_arg = B_TRUE;
3426 			break;
3427 		case 'x':
3428 			if (x_arg)
3429 				die_optdup(option);
3430 
3431 			x_arg = B_TRUE;
3432 			break;
3433 		case 'P':
3434 			if (flags != DLADM_OPT_ACTIVE)
3435 				die_optdup(option);
3436 
3437 			flags = DLADM_OPT_PERSIST;
3438 			break;
3439 		case 's':
3440 			if (s_arg)
3441 				die_optdup(option);
3442 
3443 			s_arg = B_TRUE;
3444 			break;
3445 		case 'o':
3446 			o_arg = B_TRUE;
3447 			fields_str = optarg;
3448 			break;
3449 		case 'i':
3450 			if (i_arg)
3451 				die_optdup(option);
3452 
3453 			i_arg = B_TRUE;
3454 			if (!dladm_str2interval(optarg, &interval))
3455 				die("invalid interval value '%s'", optarg);
3456 			break;
3457 		default:
3458 			die_opterr(optopt, option, use);
3459 			break;
3460 		}
3461 	}
3462 
3463 	if (p_arg && !o_arg)
3464 		die("-p requires -o");
3465 
3466 	if (p_arg && strcasecmp(fields_str, "all") == 0)
3467 		die("\"-o all\" is invalid with -p");
3468 
3469 	if (i_arg && !s_arg)
3470 		die("the option -i can be used only with -s");
3471 
3472 	if (s_arg && (L_arg || p_arg || x_arg || flags != DLADM_OPT_ACTIVE)) {
3473 		die("the option -%c cannot be used with -s",
3474 		    L_arg ? 'L' : (p_arg ? 'p' : (x_arg ? 'x' : 'P')));
3475 	}
3476 
3477 	if (L_arg && flags != DLADM_OPT_ACTIVE)
3478 		die("the option -P cannot be used with -L");
3479 
3480 	if (x_arg && (L_arg || flags != DLADM_OPT_ACTIVE))
3481 		die("the option -%c cannot be used with -x", L_arg ? 'L' : 'P');
3482 
3483 	/* get aggregation key or aggrname (optional last argument) */
3484 	if (optind == (argc-1)) {
3485 		if (!str2int(argv[optind], &key)) {
3486 			status = dladm_name2info(handle, argv[optind],
3487 			    &linkid, NULL, NULL, NULL);
3488 		} else {
3489 			status = dladm_key2linkid(handle, (uint16_t)key,
3490 			    &linkid, DLADM_OPT_ACTIVE);
3491 		}
3492 
3493 		if (status != DLADM_STATUS_OK)
3494 			die("non-existent aggregation '%s'", argv[optind]);
3495 
3496 	} else if (optind != argc) {
3497 		usage();
3498 	}
3499 
3500 	bzero(&state, sizeof (state));
3501 	state.gs_lacp = L_arg;
3502 	state.gs_stats = s_arg;
3503 	state.gs_flags = flags;
3504 	state.gs_parsable = p_arg;
3505 	state.gs_extended = x_arg;
3506 
3507 	if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0)) {
3508 		if (state.gs_lacp)
3509 			fields_str = all_lacp_fields;
3510 		else if (state.gs_stats)
3511 			fields_str = all_stats_fields;
3512 		else if (state.gs_extended)
3513 			fields_str = all_extended_fields;
3514 		else
3515 			fields_str = all_fields;
3516 	}
3517 
3518 	if (state.gs_lacp) {
3519 		pf = aggr_l_fields;
3520 	} else if (state.gs_stats) {
3521 		pf = aggr_s_fields;
3522 	} else if (state.gs_extended) {
3523 		pf = aggr_x_fields;
3524 	} else {
3525 		pf = laggr_fields;
3526 	}
3527 
3528 	if (state.gs_parsable)
3529 		ofmtflags |= OFMT_PARSABLE;
3530 	oferr = ofmt_open(fields_str, pf, ofmtflags, 0, &ofmt);
3531 	dladm_ofmt_check(oferr, state.gs_parsable, ofmt);
3532 	state.gs_ofmt = ofmt;
3533 
3534 	if (s_arg) {
3535 		aggr_stats(linkid, &state, interval);
3536 		ofmt_close(ofmt);
3537 		return;
3538 	}
3539 
3540 	if (linkid == DATALINK_ALL_LINKID) {
3541 		(void) dladm_walk_datalink_id(show_aggr, handle, &state,
3542 		    DATALINK_CLASS_AGGR, DATALINK_ANY_MEDIATYPE, flags);
3543 	} else {
3544 		(void) show_aggr(handle, linkid, &state);
3545 		if (state.gs_status != DLADM_STATUS_OK) {
3546 			die_dlerr(state.gs_status, "failed to show aggr %s",
3547 			    argv[optind]);
3548 		}
3549 	}
3550 	ofmt_close(ofmt);
3551 }
3552 
3553 static dladm_status_t
3554 print_phys_default(show_state_t *state, datalink_id_t linkid,
3555     const char *link, uint32_t flags, uint32_t media)
3556 {
3557 	dladm_phys_attr_t dpa;
3558 	dladm_status_t status;
3559 	link_fields_buf_t pattr;
3560 
3561 	status = dladm_phys_info(handle, linkid, &dpa, state->ls_flags);
3562 	if (status != DLADM_STATUS_OK)
3563 		goto done;
3564 
3565 	(void) snprintf(pattr.link_phys_device,
3566 	    sizeof (pattr.link_phys_device), "%s", dpa.dp_dev);
3567 	(void) dladm_media2str(media, pattr.link_phys_media);
3568 	if (state->ls_flags == DLADM_OPT_ACTIVE) {
3569 		boolean_t	islink;
3570 
3571 		if (!dpa.dp_novanity) {
3572 			(void) strlcpy(pattr.link_name, link,
3573 			    sizeof (pattr.link_name));
3574 			islink = B_TRUE;
3575 		} else {
3576 			/*
3577 			 * This is a physical link that does not have
3578 			 * vanity naming support.
3579 			 */
3580 			(void) strlcpy(pattr.link_name, dpa.dp_dev,
3581 			    sizeof (pattr.link_name));
3582 			islink = B_FALSE;
3583 		}
3584 
3585 		(void) get_linkstate(pattr.link_name, islink,
3586 		    pattr.link_phys_state);
3587 		(void) snprintf(pattr.link_phys_speed,
3588 		    sizeof (pattr.link_phys_speed), "%u",
3589 		    (uint_t)((get_ifspeed(pattr.link_name,
3590 		    islink)) / 1000000ull));
3591 		(void) get_linkduplex(pattr.link_name, islink,
3592 		    pattr.link_phys_duplex);
3593 	} else {
3594 		(void) snprintf(pattr.link_name, sizeof (pattr.link_name),
3595 		    "%s", link);
3596 		(void) snprintf(pattr.link_flags, sizeof (pattr.link_flags),
3597 		    "%c----", flags & DLADM_OPT_ACTIVE ? '-' : 'r');
3598 	}
3599 
3600 	ofmt_print(state->ls_ofmt, &pattr);
3601 
3602 done:
3603 	return (status);
3604 }
3605 
3606 typedef struct {
3607 	show_state_t	*ms_state;
3608 	char		*ms_link;
3609 	dladm_macaddr_attr_t *ms_mac_attr;
3610 } print_phys_mac_state_t;
3611 
3612 /*
3613  *  callback for ofmt_print()
3614  */
3615 static boolean_t
3616 print_phys_one_mac_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
3617 {
3618 	print_phys_mac_state_t *mac_state = ofarg->ofmt_cbarg;
3619 	dladm_macaddr_attr_t *attr = mac_state->ms_mac_attr;
3620 	boolean_t is_primary = (attr->ma_slot == 0);
3621 	boolean_t is_parsable = mac_state->ms_state->ls_parsable;
3622 
3623 	switch (ofarg->ofmt_id) {
3624 	case PHYS_M_LINK:
3625 		(void) snprintf(buf, bufsize, "%s",
3626 		    (is_primary || is_parsable) ? mac_state->ms_link : " ");
3627 		break;
3628 	case PHYS_M_SLOT:
3629 		if (is_primary)
3630 			(void) snprintf(buf, bufsize, gettext("primary"));
3631 		else
3632 			(void) snprintf(buf, bufsize, "%d", attr->ma_slot);
3633 		break;
3634 	case PHYS_M_ADDRESS:
3635 		(void) dladm_aggr_macaddr2str(attr->ma_addr, buf);
3636 		break;
3637 	case PHYS_M_INUSE:
3638 		(void) snprintf(buf, bufsize, "%s",
3639 		    attr->ma_flags & DLADM_MACADDR_USED ? gettext("yes") :
3640 		    gettext("no"));
3641 		break;
3642 	case PHYS_M_CLIENT:
3643 		/*
3644 		 * CR 6678526: resolve link id to actual link name if
3645 		 * it is valid.
3646 		 */
3647 		(void) snprintf(buf, bufsize, "%s", attr->ma_client_name);
3648 		break;
3649 	}
3650 
3651 	return (B_TRUE);
3652 }
3653 
3654 typedef struct {
3655 	show_state_t	*hs_state;
3656 	char		*hs_link;
3657 	dladm_hwgrp_attr_t *hs_grp_attr;
3658 } print_phys_hwgrp_state_t;
3659 
3660 static boolean_t
3661 print_phys_one_hwgrp_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
3662 {
3663 	print_phys_hwgrp_state_t *hg_state = ofarg->ofmt_cbarg;
3664 	dladm_hwgrp_attr_t *attr = hg_state->hs_grp_attr;
3665 
3666 	switch (ofarg->ofmt_id) {
3667 	case PHYS_H_LINK:
3668 		(void) snprintf(buf, bufsize, "%s", attr->hg_link_name);
3669 		break;
3670 	case PHYS_H_GROUP:
3671 		(void) snprintf(buf, bufsize, "%d", attr->hg_grp_num);
3672 		break;
3673 	case PHYS_H_GRPTYPE:
3674 		(void) snprintf(buf, bufsize, "%s",
3675 		    attr->hg_grp_type == DLADM_HWGRP_TYPE_RX ? "RX" : "TX");
3676 		break;
3677 	case PHYS_H_RINGS:
3678 		(void) snprintf(buf, bufsize, "%d", attr->hg_n_rings);
3679 		break;
3680 	case PHYS_H_CLIENTS:
3681 		if (attr->hg_client_names[0] == '\0') {
3682 			(void) snprintf(buf, bufsize, "--");
3683 		} else {
3684 			(void) snprintf(buf, bufsize, "%s ",
3685 			    attr->hg_client_names);
3686 		}
3687 		break;
3688 	}
3689 
3690 	return (B_TRUE);
3691 }
3692 
3693 /*
3694  * callback for dladm_walk_macaddr, invoked for each MAC address slot
3695  */
3696 static boolean_t
3697 print_phys_mac_callback(void *arg, dladm_macaddr_attr_t *attr)
3698 {
3699 	print_phys_mac_state_t *mac_state = arg;
3700 	show_state_t *state = mac_state->ms_state;
3701 
3702 	mac_state->ms_mac_attr = attr;
3703 	ofmt_print(state->ls_ofmt, mac_state);
3704 
3705 	return (B_TRUE);
3706 }
3707 
3708 /*
3709  * invoked by show-phys -m for each physical data-link
3710  */
3711 static dladm_status_t
3712 print_phys_mac(show_state_t *state, datalink_id_t linkid, char *link)
3713 {
3714 	print_phys_mac_state_t mac_state;
3715 
3716 	mac_state.ms_state = state;
3717 	mac_state.ms_link = link;
3718 
3719 	return (dladm_walk_macaddr(handle, linkid, &mac_state,
3720 	    print_phys_mac_callback));
3721 }
3722 
3723 /*
3724  * callback for dladm_walk_hwgrp, invoked for each MAC hwgrp
3725  */
3726 static boolean_t
3727 print_phys_hwgrp_callback(void *arg, dladm_hwgrp_attr_t *attr)
3728 {
3729 	print_phys_hwgrp_state_t *hwgrp_state = arg;
3730 	show_state_t *state = hwgrp_state->hs_state;
3731 
3732 	hwgrp_state->hs_grp_attr = attr;
3733 	ofmt_print(state->ls_ofmt, hwgrp_state);
3734 
3735 	return (B_TRUE);
3736 }
3737 
3738 /* invoked by show-phys -H for each physical data-link */
3739 static dladm_status_t
3740 print_phys_hwgrp(show_state_t *state, datalink_id_t linkid, char *link)
3741 {
3742 	print_phys_hwgrp_state_t hwgrp_state;
3743 
3744 	hwgrp_state.hs_state = state;
3745 	hwgrp_state.hs_link = link;
3746 	return (dladm_walk_hwgrp(handle, linkid, &hwgrp_state,
3747 	    print_phys_hwgrp_callback));
3748 }
3749 
3750 static dladm_status_t
3751 print_phys(show_state_t *state, datalink_id_t linkid)
3752 {
3753 	char			link[MAXLINKNAMELEN];
3754 	uint32_t		flags;
3755 	dladm_status_t		status;
3756 	datalink_class_t	class;
3757 	uint32_t		media;
3758 
3759 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, &class,
3760 	    &media, link, MAXLINKNAMELEN)) != DLADM_STATUS_OK) {
3761 		goto done;
3762 	}
3763 
3764 	if (class != DATALINK_CLASS_PHYS) {
3765 		status = DLADM_STATUS_BADARG;
3766 		goto done;
3767 	}
3768 
3769 	if (!(state->ls_flags & flags)) {
3770 		status = DLADM_STATUS_NOTFOUND;
3771 		goto done;
3772 	}
3773 
3774 	if (state->ls_mac)
3775 		status = print_phys_mac(state, linkid, link);
3776 	else if (state->ls_hwgrp)
3777 		status = print_phys_hwgrp(state, linkid, link);
3778 	else
3779 		status = print_phys_default(state, linkid, link, flags, media);
3780 
3781 done:
3782 	return (status);
3783 }
3784 
3785 /* ARGSUSED */
3786 static int
3787 show_phys(dladm_handle_t dh, datalink_id_t linkid, void *arg)
3788 {
3789 	show_state_t	*state = arg;
3790 
3791 	state->ls_status = print_phys(state, linkid);
3792 	return (DLADM_WALK_CONTINUE);
3793 }
3794 
3795 /*
3796  * Print the active topology information.
3797  */
3798 static dladm_status_t
3799 print_vlan(show_state_t *state, datalink_id_t linkid, link_fields_buf_t *l)
3800 {
3801 	dladm_vlan_attr_t	vinfo;
3802 	uint32_t		flags;
3803 	dladm_status_t		status;
3804 
3805 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, NULL, NULL,
3806 	    l->link_name, sizeof (l->link_name))) != DLADM_STATUS_OK) {
3807 		goto done;
3808 	}
3809 
3810 	if (!(state->ls_flags & flags)) {
3811 		status = DLADM_STATUS_NOTFOUND;
3812 		goto done;
3813 	}
3814 
3815 	if ((status = dladm_vlan_info(handle, linkid, &vinfo,
3816 	    state->ls_flags)) != DLADM_STATUS_OK ||
3817 	    (status = dladm_datalink_id2info(handle, vinfo.dv_linkid, NULL,
3818 	    NULL, NULL, l->link_over, sizeof (l->link_over))) !=
3819 	    DLADM_STATUS_OK) {
3820 		goto done;
3821 	}
3822 
3823 	(void) snprintf(l->link_vlan_vid, sizeof (l->link_vlan_vid), "%d",
3824 	    vinfo.dv_vid);
3825 	(void) snprintf(l->link_flags, sizeof (l->link_flags), "%c----",
3826 	    vinfo.dv_force ? 'f' : '-');
3827 
3828 done:
3829 	return (status);
3830 }
3831 
3832 /* ARGSUSED */
3833 static int
3834 show_vlan(dladm_handle_t dh, datalink_id_t linkid, void *arg)
3835 {
3836 	show_state_t		*state = arg;
3837 	dladm_status_t		status;
3838 	link_fields_buf_t	lbuf;
3839 
3840 	bzero(&lbuf, sizeof (link_fields_buf_t));
3841 	status = print_vlan(state, linkid, &lbuf);
3842 	if (status != DLADM_STATUS_OK)
3843 		goto done;
3844 
3845 	ofmt_print(state->ls_ofmt, &lbuf);
3846 
3847 done:
3848 	state->ls_status = status;
3849 	return (DLADM_WALK_CONTINUE);
3850 }
3851 
3852 static void
3853 do_show_phys(int argc, char *argv[], const char *use)
3854 {
3855 	int		option;
3856 	uint32_t	flags = DLADM_OPT_ACTIVE;
3857 	boolean_t	p_arg = B_FALSE;
3858 	boolean_t	o_arg = B_FALSE;
3859 	boolean_t	m_arg = B_FALSE;
3860 	boolean_t	H_arg = B_FALSE;
3861 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
3862 	show_state_t	state;
3863 	dladm_status_t	status;
3864 	char		*fields_str = NULL;
3865 	char		*all_active_fields =
3866 	    "link,media,state,speed,duplex,device";
3867 	char		*all_inactive_fields = "link,device,media,flags";
3868 	char		*all_mac_fields = "link,slot,address,inuse,client";
3869 	char		*all_hwgrp_fields =
3870 	    "link,group,grouptype,rings,clients";
3871 	ofmt_field_t	*pf;
3872 	ofmt_handle_t	ofmt;
3873 	ofmt_status_t	oferr;
3874 	uint_t		ofmtflags = 0;
3875 
3876 	bzero(&state, sizeof (state));
3877 	opterr = 0;
3878 	while ((option = getopt_long(argc, argv, ":pPo:mH",
3879 	    show_lopts, NULL)) != -1) {
3880 		switch (option) {
3881 		case 'p':
3882 			if (p_arg)
3883 				die_optdup(option);
3884 
3885 			p_arg = B_TRUE;
3886 			break;
3887 		case 'P':
3888 			if (flags != DLADM_OPT_ACTIVE)
3889 				die_optdup(option);
3890 
3891 			flags = DLADM_OPT_PERSIST;
3892 			break;
3893 		case 'o':
3894 			o_arg = B_TRUE;
3895 			fields_str = optarg;
3896 			break;
3897 		case 'm':
3898 			m_arg = B_TRUE;
3899 			break;
3900 		case 'H':
3901 			H_arg = B_TRUE;
3902 			break;
3903 		default:
3904 			die_opterr(optopt, option, use);
3905 			break;
3906 		}
3907 	}
3908 
3909 	if (p_arg && !o_arg)
3910 		die("-p requires -o");
3911 
3912 	if (m_arg && H_arg)
3913 		die("-m cannot combine with -H");
3914 
3915 	if (p_arg && strcasecmp(fields_str, "all") == 0)
3916 		die("\"-o all\" is invalid with -p");
3917 
3918 	/* get link name (optional last argument) */
3919 	if (optind == (argc-1)) {
3920 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
3921 		    NULL, NULL, NULL)) != DLADM_STATUS_OK) {
3922 			die_dlerr(status, "link %s is not valid", argv[optind]);
3923 		}
3924 	} else if (optind != argc) {
3925 		usage();
3926 	}
3927 
3928 	state.ls_parsable = p_arg;
3929 	state.ls_flags = flags;
3930 	state.ls_donefirst = B_FALSE;
3931 	state.ls_mac = m_arg;
3932 	state.ls_hwgrp = H_arg;
3933 
3934 	if (m_arg && !(flags & DLADM_OPT_ACTIVE)) {
3935 		/*
3936 		 * We can only display the factory MAC addresses of
3937 		 * active data-links.
3938 		 */
3939 		die("-m not compatible with -P");
3940 	}
3941 
3942 	if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0)) {
3943 		if (state.ls_mac)
3944 			fields_str = all_mac_fields;
3945 		else if (state.ls_hwgrp)
3946 			fields_str = all_hwgrp_fields;
3947 		else if (state.ls_flags & DLADM_OPT_ACTIVE) {
3948 			fields_str = all_active_fields;
3949 		} else {
3950 			fields_str = all_inactive_fields;
3951 		}
3952 	}
3953 
3954 	if (state.ls_mac) {
3955 		pf = phys_m_fields;
3956 	} else if (state.ls_hwgrp) {
3957 		pf = phys_h_fields;
3958 	} else {
3959 		pf = phys_fields;
3960 	}
3961 
3962 	if (state.ls_parsable)
3963 		ofmtflags |= OFMT_PARSABLE;
3964 	oferr = ofmt_open(fields_str, pf, ofmtflags, 0, &ofmt);
3965 	dladm_ofmt_check(oferr, state.ls_parsable, ofmt);
3966 	state.ls_ofmt = ofmt;
3967 
3968 	if (linkid == DATALINK_ALL_LINKID) {
3969 		(void) dladm_walk_datalink_id(show_phys, handle, &state,
3970 		    DATALINK_CLASS_PHYS, DATALINK_ANY_MEDIATYPE, flags);
3971 	} else {
3972 		(void) show_phys(handle, linkid, &state);
3973 		if (state.ls_status != DLADM_STATUS_OK) {
3974 			die_dlerr(state.ls_status,
3975 			    "failed to show physical link %s", argv[optind]);
3976 		}
3977 	}
3978 	ofmt_close(ofmt);
3979 }
3980 
3981 static void
3982 do_show_vlan(int argc, char *argv[], const char *use)
3983 {
3984 	int		option;
3985 	uint32_t	flags = DLADM_OPT_ACTIVE;
3986 	boolean_t	p_arg = B_FALSE;
3987 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
3988 	show_state_t	state;
3989 	dladm_status_t	status;
3990 	boolean_t	o_arg = B_FALSE;
3991 	char		*fields_str = NULL;
3992 	ofmt_handle_t	ofmt;
3993 	ofmt_status_t	oferr;
3994 	uint_t		ofmtflags = 0;
3995 
3996 	bzero(&state, sizeof (state));
3997 
3998 	opterr = 0;
3999 	while ((option = getopt_long(argc, argv, ":pPo:",
4000 	    show_lopts, NULL)) != -1) {
4001 		switch (option) {
4002 		case 'p':
4003 			if (p_arg)
4004 				die_optdup(option);
4005 
4006 			p_arg = B_TRUE;
4007 			break;
4008 		case 'P':
4009 			if (flags != DLADM_OPT_ACTIVE)
4010 				die_optdup(option);
4011 
4012 			flags = DLADM_OPT_PERSIST;
4013 			break;
4014 		case 'o':
4015 			o_arg = B_TRUE;
4016 			fields_str = optarg;
4017 			break;
4018 		default:
4019 			die_opterr(optopt, option, use);
4020 			break;
4021 		}
4022 	}
4023 
4024 	/* get link name (optional last argument) */
4025 	if (optind == (argc-1)) {
4026 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
4027 		    NULL, NULL, NULL)) != DLADM_STATUS_OK) {
4028 			die_dlerr(status, "link %s is not valid", argv[optind]);
4029 		}
4030 	} else if (optind != argc) {
4031 		usage();
4032 	}
4033 
4034 	state.ls_parsable = p_arg;
4035 	state.ls_flags = flags;
4036 	state.ls_donefirst = B_FALSE;
4037 
4038 	if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0))
4039 		fields_str = NULL;
4040 
4041 	if (state.ls_parsable)
4042 		ofmtflags |= OFMT_PARSABLE;
4043 	oferr = ofmt_open(fields_str, vlan_fields, ofmtflags, 0, &ofmt);
4044 	dladm_ofmt_check(oferr, state.ls_parsable, ofmt);
4045 	state.ls_ofmt = ofmt;
4046 
4047 	if (linkid == DATALINK_ALL_LINKID) {
4048 		(void) dladm_walk_datalink_id(show_vlan, handle, &state,
4049 		    DATALINK_CLASS_VLAN, DATALINK_ANY_MEDIATYPE, flags);
4050 	} else {
4051 		(void) show_vlan(handle, linkid, &state);
4052 		if (state.ls_status != DLADM_STATUS_OK) {
4053 			die_dlerr(state.ls_status, "failed to show vlan %s",
4054 			    argv[optind]);
4055 		}
4056 	}
4057 	ofmt_close(ofmt);
4058 }
4059 
4060 static void
4061 do_create_vnic(int argc, char *argv[], const char *use)
4062 {
4063 	datalink_id_t		linkid, dev_linkid;
4064 	char			devname[MAXLINKNAMELEN];
4065 	char			name[MAXLINKNAMELEN];
4066 	boolean_t		l_arg = B_FALSE;
4067 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
4068 	char			*altroot = NULL;
4069 	int			option;
4070 	char			*endp = NULL;
4071 	dladm_status_t		status;
4072 	vnic_mac_addr_type_t	mac_addr_type = VNIC_MAC_ADDR_TYPE_AUTO;
4073 	uchar_t			*mac_addr;
4074 	int			mac_slot = -1, maclen = 0, mac_prefix_len = 0;
4075 	char			propstr[DLADM_STRSIZE];
4076 	dladm_arg_list_t	*proplist = NULL;
4077 	int			vid = 0;
4078 
4079 	opterr = 0;
4080 	bzero(propstr, DLADM_STRSIZE);
4081 
4082 	while ((option = getopt_long(argc, argv, ":tfR:l:m:n:p:r:v:H",
4083 	    vnic_lopts, NULL)) != -1) {
4084 		switch (option) {
4085 		case 't':
4086 			flags &= ~DLADM_OPT_PERSIST;
4087 			break;
4088 		case 'R':
4089 			altroot = optarg;
4090 			break;
4091 		case 'l':
4092 			if (strlcpy(devname, optarg, MAXLINKNAMELEN) >=
4093 			    MAXLINKNAMELEN)
4094 				die("link name too long");
4095 			l_arg = B_TRUE;
4096 			break;
4097 		case 'm':
4098 			if (strcmp(optarg, "fixed") == 0) {
4099 				/*
4100 				 * A fixed MAC address must be specified
4101 				 * by its value, not by the keyword 'fixed'.
4102 				 */
4103 				die("'fixed' is not a valid MAC address");
4104 			}
4105 			if (dladm_vnic_str2macaddrtype(optarg,
4106 			    &mac_addr_type) != DLADM_STATUS_OK) {
4107 				mac_addr_type = VNIC_MAC_ADDR_TYPE_FIXED;
4108 				/* MAC address specified by value */
4109 				mac_addr = _link_aton(optarg, &maclen);
4110 				if (mac_addr == NULL) {
4111 					if (maclen == -1)
4112 						die("invalid MAC address");
4113 					else
4114 						die("out of memory");
4115 				}
4116 			}
4117 			break;
4118 		case 'n':
4119 			errno = 0;
4120 			mac_slot = (int)strtol(optarg, &endp, 10);
4121 			if (errno != 0 || *endp != '\0')
4122 				die("invalid slot number");
4123 			break;
4124 		case 'p':
4125 			(void) strlcat(propstr, optarg, DLADM_STRSIZE);
4126 			if (strlcat(propstr, ",", DLADM_STRSIZE) >=
4127 			    DLADM_STRSIZE)
4128 				die("property list too long '%s'", propstr);
4129 			break;
4130 		case 'r':
4131 			mac_addr = _link_aton(optarg, &mac_prefix_len);
4132 			if (mac_addr == NULL) {
4133 				if (mac_prefix_len == -1)
4134 					die("invalid MAC address");
4135 				else
4136 					die("out of memory");
4137 			}
4138 			break;
4139 		case 'v':
4140 			if (vid != 0)
4141 				die_optdup(option);
4142 
4143 			if (!str2int(optarg, &vid) || vid < 1 || vid > 4094)
4144 				die("invalid VLAN identifier '%s'", optarg);
4145 
4146 			break;
4147 		case 'f':
4148 			flags |= DLADM_OPT_FORCE;
4149 			break;
4150 		case 'H':
4151 			flags |= DLADM_OPT_HWRINGS;
4152 			break;
4153 		default:
4154 			die_opterr(optopt, option, use);
4155 		}
4156 	}
4157 
4158 	/*
4159 	 * 'f' - force, flag can be specified only with 'v' - vlan.
4160 	 */
4161 	if ((flags & DLADM_OPT_FORCE) != 0 && vid == 0)
4162 		die("-f option can only be used with -v");
4163 
4164 	if (mac_prefix_len != 0 && mac_addr_type != VNIC_MAC_ADDR_TYPE_RANDOM &&
4165 	    mac_addr_type != VNIC_MAC_ADDR_TYPE_FIXED)
4166 		usage();
4167 
4168 	/* check required options */
4169 	if (!l_arg)
4170 		usage();
4171 
4172 	if (mac_slot != -1 && mac_addr_type != VNIC_MAC_ADDR_TYPE_FACTORY)
4173 		usage();
4174 
4175 	/* the VNIC id is the required operand */
4176 	if (optind != (argc - 1))
4177 		usage();
4178 
4179 	if (strlcpy(name, argv[optind], MAXLINKNAMELEN) >= MAXLINKNAMELEN)
4180 		die("link name too long '%s'", argv[optind]);
4181 
4182 	if (!dladm_valid_linkname(name))
4183 		die("invalid link name '%s'", argv[optind]);
4184 
4185 	if (altroot != NULL)
4186 		altroot_cmd(altroot, argc, argv);
4187 
4188 	if (dladm_name2info(handle, devname, &dev_linkid, NULL, NULL, NULL) !=
4189 	    DLADM_STATUS_OK)
4190 		die("invalid link name '%s'", devname);
4191 
4192 	if (dladm_parse_link_props(propstr, &proplist, B_FALSE)
4193 	    != DLADM_STATUS_OK)
4194 		die("invalid vnic property");
4195 
4196 	status = dladm_vnic_create(handle, name, dev_linkid, mac_addr_type,
4197 	    mac_addr, maclen, &mac_slot, mac_prefix_len, vid, &linkid, proplist,
4198 	    flags);
4199 	if (status != DLADM_STATUS_OK)
4200 		die_dlerr(status, "vnic creation over %s failed", devname);
4201 
4202 	dladm_free_props(proplist);
4203 }
4204 
4205 static void
4206 do_etherstub_check(const char *name, datalink_id_t linkid, boolean_t etherstub,
4207     uint32_t flags)
4208 {
4209 	boolean_t is_etherstub;
4210 	dladm_vnic_attr_t attr;
4211 
4212 	if (dladm_vnic_info(handle, linkid, &attr, flags) != DLADM_STATUS_OK) {
4213 		/*
4214 		 * Let the delete continue anyway.
4215 		 */
4216 		return;
4217 	}
4218 	is_etherstub = (attr.va_link_id == DATALINK_INVALID_LINKID);
4219 	if (is_etherstub != etherstub) {
4220 		die("'%s' is not %s", name,
4221 		    (is_etherstub ? "a vnic" : "an etherstub"));
4222 	}
4223 }
4224 
4225 static void
4226 do_delete_vnic_common(int argc, char *argv[], const char *use,
4227     boolean_t etherstub)
4228 {
4229 	int option;
4230 	uint32_t flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
4231 	datalink_id_t linkid;
4232 	char *altroot = NULL;
4233 	dladm_status_t status;
4234 
4235 	opterr = 0;
4236 	while ((option = getopt_long(argc, argv, ":R:t", lopts,
4237 	    NULL)) != -1) {
4238 		switch (option) {
4239 		case 't':
4240 			flags &= ~DLADM_OPT_PERSIST;
4241 			break;
4242 		case 'R':
4243 			altroot = optarg;
4244 			break;
4245 		default:
4246 			die_opterr(optopt, option, use);
4247 		}
4248 	}
4249 
4250 	/* get vnic name (required last argument) */
4251 	if (optind != (argc - 1))
4252 		usage();
4253 
4254 	if (altroot != NULL)
4255 		altroot_cmd(altroot, argc, argv);
4256 
4257 	status = dladm_name2info(handle, argv[optind], &linkid, NULL, NULL,
4258 	    NULL);
4259 	if (status != DLADM_STATUS_OK)
4260 		die("invalid link name '%s'", argv[optind]);
4261 
4262 	if ((flags & DLADM_OPT_ACTIVE) != 0) {
4263 		do_etherstub_check(argv[optind], linkid, etherstub,
4264 		    DLADM_OPT_ACTIVE);
4265 	}
4266 	if ((flags & DLADM_OPT_PERSIST) != 0) {
4267 		do_etherstub_check(argv[optind], linkid, etherstub,
4268 		    DLADM_OPT_PERSIST);
4269 	}
4270 
4271 	status = dladm_vnic_delete(handle, linkid, flags);
4272 	if (status != DLADM_STATUS_OK)
4273 		die_dlerr(status, "vnic deletion failed");
4274 }
4275 
4276 static void
4277 do_delete_vnic(int argc, char *argv[], const char *use)
4278 {
4279 	do_delete_vnic_common(argc, argv, use, B_FALSE);
4280 }
4281 
4282 /* ARGSUSED */
4283 static void
4284 do_up_vnic_common(int argc, char *argv[], const char *use, boolean_t vlan)
4285 {
4286 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
4287 	dladm_status_t	status;
4288 	char 		*type;
4289 
4290 	type = vlan ? "vlan" : "vnic";
4291 
4292 	/*
4293 	 * get the id or the name of the vnic/vlan (optional last argument)
4294 	 */
4295 	if (argc == 2) {
4296 		status = dladm_name2info(handle, argv[1], &linkid, NULL, NULL,
4297 		    NULL);
4298 		if (status != DLADM_STATUS_OK)
4299 			goto done;
4300 
4301 	} else if (argc > 2) {
4302 		usage();
4303 	}
4304 
4305 	if (vlan)
4306 		status = dladm_vlan_up(handle, linkid);
4307 	else
4308 		status = dladm_vnic_up(handle, linkid, 0);
4309 
4310 done:
4311 	if (status != DLADM_STATUS_OK) {
4312 		if (argc == 2) {
4313 			die_dlerr(status,
4314 			    "could not bring up %s '%s'", type, argv[1]);
4315 		} else {
4316 			die_dlerr(status, "could not bring %ss up", type);
4317 		}
4318 	}
4319 }
4320 
4321 static void
4322 do_up_vnic(int argc, char *argv[], const char *use)
4323 {
4324 	do_up_vnic_common(argc, argv, use, B_FALSE);
4325 }
4326 
4327 static void
4328 dump_vnics_head(const char *dev)
4329 {
4330 	if (strlen(dev))
4331 		(void) printf("%s", dev);
4332 
4333 	(void) printf("\tipackets  rbytes      opackets  obytes          ");
4334 
4335 	if (strlen(dev))
4336 		(void) printf("%%ipkts  %%opkts\n");
4337 	else
4338 		(void) printf("\n");
4339 }
4340 
4341 static void
4342 dump_vnic_stat(const char *name, datalink_id_t vnic_id,
4343     show_vnic_state_t *state, pktsum_t *vnic_stats, pktsum_t *tot_stats)
4344 {
4345 	pktsum_t	diff_stats;
4346 	pktsum_t	*old_stats = &state->vs_prevstats[vnic_id];
4347 
4348 	dladm_stats_diff(&diff_stats, vnic_stats, old_stats);
4349 
4350 	(void) printf("%s", name);
4351 
4352 	(void) printf("\t%-10llu", diff_stats.ipackets);
4353 	(void) printf("%-12llu", diff_stats.rbytes);
4354 	(void) printf("%-10llu", diff_stats.opackets);
4355 	(void) printf("%-12llu", diff_stats.obytes);
4356 
4357 	if (tot_stats) {
4358 		if (tot_stats->ipackets == 0) {
4359 			(void) printf("\t-");
4360 		} else {
4361 			(void) printf("\t%-6.1f", (double)diff_stats.ipackets/
4362 			    (double)tot_stats->ipackets * 100);
4363 		}
4364 		if (tot_stats->opackets == 0) {
4365 			(void) printf("\t-");
4366 		} else {
4367 			(void) printf("\t%-6.1f", (double)diff_stats.opackets/
4368 			    (double)tot_stats->opackets * 100);
4369 		}
4370 	}
4371 	(void) printf("\n");
4372 
4373 	*old_stats = *vnic_stats;
4374 }
4375 
4376 /*
4377  * Called from the walker dladm_vnic_walk_sys() for each vnic to display
4378  * vnic information or statistics.
4379  */
4380 static dladm_status_t
4381 print_vnic(show_vnic_state_t *state, datalink_id_t linkid)
4382 {
4383 	dladm_vnic_attr_t	attr, *vnic = &attr;
4384 	dladm_status_t		status;
4385 	boolean_t		is_etherstub;
4386 	char			devname[MAXLINKNAMELEN];
4387 	char			vnic_name[MAXLINKNAMELEN];
4388 	char			mstr[MAXMACADDRLEN * 3];
4389 	vnic_fields_buf_t	vbuf;
4390 
4391 	if ((status = dladm_vnic_info(handle, linkid, vnic, state->vs_flags)) !=
4392 	    DLADM_STATUS_OK)
4393 		return (status);
4394 
4395 	is_etherstub = (vnic->va_link_id == DATALINK_INVALID_LINKID);
4396 	if (state->vs_etherstub != is_etherstub) {
4397 		/*
4398 		 * Want all etherstub but it's not one, or want
4399 		 * non-etherstub and it's one.
4400 		 */
4401 		return (DLADM_STATUS_OK);
4402 	}
4403 
4404 	if (state->vs_link_id != DATALINK_ALL_LINKID) {
4405 		if (state->vs_link_id != vnic->va_link_id)
4406 			return (DLADM_STATUS_OK);
4407 	}
4408 
4409 	if (dladm_datalink_id2info(handle, linkid, NULL, NULL,
4410 	    NULL, vnic_name, sizeof (vnic_name)) != DLADM_STATUS_OK)
4411 		return (DLADM_STATUS_BADARG);
4412 
4413 	bzero(devname, sizeof (devname));
4414 	if (!is_etherstub &&
4415 	    dladm_datalink_id2info(handle, vnic->va_link_id, NULL, NULL,
4416 	    NULL, devname, sizeof (devname)) != DLADM_STATUS_OK)
4417 		return (DLADM_STATUS_BADARG);
4418 
4419 	state->vs_found = B_TRUE;
4420 	if (state->vs_stats) {
4421 		/* print vnic statistics */
4422 		pktsum_t vnic_stats;
4423 
4424 		if (state->vs_firstonly) {
4425 			if (state->vs_donefirst)
4426 				return (0);
4427 			state->vs_donefirst = B_TRUE;
4428 		}
4429 
4430 		if (!state->vs_printstats) {
4431 			/*
4432 			 * get vnic statistics and add to the sum for the
4433 			 * named device.
4434 			 */
4435 			get_link_stats(vnic_name, &vnic_stats);
4436 			dladm_stats_total(&state->vs_totalstats, &vnic_stats,
4437 			    &state->vs_prevstats[vnic->va_vnic_id]);
4438 		} else {
4439 			/* get and print vnic statistics */
4440 			get_link_stats(vnic_name, &vnic_stats);
4441 			dump_vnic_stat(vnic_name, linkid, state, &vnic_stats,
4442 			    &state->vs_totalstats);
4443 		}
4444 		return (DLADM_STATUS_OK);
4445 	} else {
4446 		(void) snprintf(vbuf.vnic_link, sizeof (vbuf.vnic_link),
4447 		    "%s", vnic_name);
4448 
4449 		if (!is_etherstub) {
4450 
4451 			(void) snprintf(vbuf.vnic_over, sizeof (vbuf.vnic_over),
4452 			    "%s", devname);
4453 			(void) snprintf(vbuf.vnic_speed,
4454 			    sizeof (vbuf.vnic_speed), "%u",
4455 			    (uint_t)((get_ifspeed(vnic_name, B_TRUE))
4456 			    / 1000000ull));
4457 
4458 			switch (vnic->va_mac_addr_type) {
4459 			case VNIC_MAC_ADDR_TYPE_FIXED:
4460 			case VNIC_MAC_ADDR_TYPE_PRIMARY:
4461 				(void) snprintf(vbuf.vnic_macaddrtype,
4462 				    sizeof (vbuf.vnic_macaddrtype),
4463 				    gettext("fixed"));
4464 				break;
4465 			case VNIC_MAC_ADDR_TYPE_RANDOM:
4466 				(void) snprintf(vbuf.vnic_macaddrtype,
4467 				    sizeof (vbuf.vnic_macaddrtype),
4468 				    gettext("random"));
4469 				break;
4470 			case VNIC_MAC_ADDR_TYPE_FACTORY:
4471 				(void) snprintf(vbuf.vnic_macaddrtype,
4472 				    sizeof (vbuf.vnic_macaddrtype),
4473 				    gettext("factory, slot %d"),
4474 				    vnic->va_mac_slot);
4475 				break;
4476 			}
4477 
4478 			if (strlen(vbuf.vnic_macaddrtype) > 0) {
4479 				(void) snprintf(vbuf.vnic_macaddr,
4480 				    sizeof (vbuf.vnic_macaddr), "%s",
4481 				    dladm_aggr_macaddr2str(vnic->va_mac_addr,
4482 				    mstr));
4483 			}
4484 
4485 			(void) snprintf(vbuf.vnic_vid, sizeof (vbuf.vnic_vid),
4486 			    "%d", vnic->va_vid);
4487 		}
4488 
4489 		ofmt_print(state->vs_ofmt, &vbuf);
4490 
4491 		return (DLADM_STATUS_OK);
4492 	}
4493 }
4494 
4495 /* ARGSUSED */
4496 static int
4497 show_vnic(dladm_handle_t dh, datalink_id_t linkid, void *arg)
4498 {
4499 	show_vnic_state_t	*state = arg;
4500 
4501 	state->vs_status = print_vnic(state, linkid);
4502 	return (DLADM_WALK_CONTINUE);
4503 }
4504 
4505 static void
4506 do_show_vnic_common(int argc, char *argv[], const char *use,
4507     boolean_t etherstub)
4508 {
4509 	int			option;
4510 	boolean_t		s_arg = B_FALSE;
4511 	boolean_t		i_arg = B_FALSE;
4512 	boolean_t		l_arg = B_FALSE;
4513 	uint32_t		interval = 0, flags = DLADM_OPT_ACTIVE;
4514 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
4515 	datalink_id_t		dev_linkid = DATALINK_ALL_LINKID;
4516 	show_vnic_state_t	state;
4517 	dladm_status_t		status;
4518 	boolean_t		o_arg = B_FALSE;
4519 	char			*fields_str = NULL;
4520 	ofmt_field_t		*pf;
4521 	char			*all_e_fields = "link";
4522 	ofmt_handle_t		ofmt;
4523 	ofmt_status_t		oferr;
4524 	uint_t			ofmtflags = 0;
4525 
4526 	bzero(&state, sizeof (state));
4527 	opterr = 0;
4528 	while ((option = getopt_long(argc, argv, ":pPl:si:o:", lopts,
4529 	    NULL)) != -1) {
4530 		switch (option) {
4531 		case 'p':
4532 			state.vs_parsable = B_TRUE;
4533 			break;
4534 		case 'P':
4535 			flags = DLADM_OPT_PERSIST;
4536 			break;
4537 		case 'l':
4538 			if (etherstub)
4539 				die("option not supported for this command");
4540 
4541 			if (strlcpy(state.vs_link, optarg, MAXLINKNAMELEN) >=
4542 			    MAXLINKNAMELEN)
4543 				die("link name too long");
4544 
4545 			l_arg = B_TRUE;
4546 			break;
4547 		case 's':
4548 			if (s_arg) {
4549 				die("the option -s cannot be specified "
4550 				    "more than once");
4551 			}
4552 			s_arg = B_TRUE;
4553 			break;
4554 		case 'i':
4555 			if (i_arg) {
4556 				die("the option -i cannot be specified "
4557 				    "more than once");
4558 			}
4559 			i_arg = B_TRUE;
4560 			if (!dladm_str2interval(optarg, &interval))
4561 				die("invalid interval value '%s'", optarg);
4562 			break;
4563 		case 'o':
4564 			o_arg = B_TRUE;
4565 			fields_str = optarg;
4566 			break;
4567 		default:
4568 			die_opterr(optopt, option, use);
4569 		}
4570 	}
4571 
4572 	if (i_arg && !s_arg)
4573 		die("the option -i can be used only with -s");
4574 
4575 	/* get vnic ID (optional last argument) */
4576 	if (optind == (argc - 1)) {
4577 		status = dladm_name2info(handle, argv[optind], &linkid, NULL,
4578 		    NULL, NULL);
4579 		if (status != DLADM_STATUS_OK) {
4580 			die_dlerr(status, "invalid vnic name '%s'",
4581 			    argv[optind]);
4582 		}
4583 		(void) strlcpy(state.vs_vnic, argv[optind], MAXLINKNAMELEN);
4584 	} else if (optind != argc) {
4585 		usage();
4586 	}
4587 
4588 	if (l_arg) {
4589 		status = dladm_name2info(handle, state.vs_link, &dev_linkid,
4590 		    NULL, NULL, NULL);
4591 		if (status != DLADM_STATUS_OK) {
4592 			die_dlerr(status, "invalid link name '%s'",
4593 			    state.vs_link);
4594 		}
4595 	}
4596 
4597 	state.vs_vnic_id = linkid;
4598 	state.vs_link_id = dev_linkid;
4599 	state.vs_etherstub = etherstub;
4600 	state.vs_found = B_FALSE;
4601 	state.vs_flags = flags;
4602 
4603 	if (!o_arg || (o_arg && strcasecmp(fields_str, "all") == 0)) {
4604 		if (etherstub)
4605 			fields_str = all_e_fields;
4606 	}
4607 	pf = vnic_fields;
4608 
4609 	if (state.vs_parsable)
4610 		ofmtflags |= OFMT_PARSABLE;
4611 	oferr = ofmt_open(fields_str, pf, ofmtflags, 0, &ofmt);
4612 	dladm_ofmt_check(oferr, state.vs_parsable, ofmt);
4613 	state.vs_ofmt = ofmt;
4614 
4615 	if (s_arg) {
4616 		/* Display vnic statistics */
4617 		vnic_stats(&state, interval);
4618 		ofmt_close(ofmt);
4619 		return;
4620 	}
4621 
4622 	/* Display vnic information */
4623 	state.vs_donefirst = B_FALSE;
4624 
4625 	if (linkid == DATALINK_ALL_LINKID) {
4626 		(void) dladm_walk_datalink_id(show_vnic, handle, &state,
4627 		    DATALINK_CLASS_VNIC | DATALINK_CLASS_ETHERSTUB,
4628 		    DATALINK_ANY_MEDIATYPE, flags);
4629 	} else {
4630 		(void) show_vnic(handle, linkid, &state);
4631 		if (state.vs_status != DLADM_STATUS_OK) {
4632 			ofmt_close(ofmt);
4633 			die_dlerr(state.vs_status, "failed to show vnic '%s'",
4634 			    state.vs_vnic);
4635 		}
4636 	}
4637 	ofmt_close(ofmt);
4638 }
4639 
4640 static void
4641 do_show_vnic(int argc, char *argv[], const char *use)
4642 {
4643 	do_show_vnic_common(argc, argv, use, B_FALSE);
4644 }
4645 
4646 static void
4647 do_create_etherstub(int argc, char *argv[], const char *use)
4648 {
4649 	uint32_t flags;
4650 	char *altroot = NULL;
4651 	int option;
4652 	dladm_status_t status;
4653 	char name[MAXLINKNAMELEN];
4654 	uchar_t mac_addr[ETHERADDRL];
4655 
4656 	name[0] = '\0';
4657 	bzero(mac_addr, sizeof (mac_addr));
4658 	flags = DLADM_OPT_ANCHOR | DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
4659 
4660 	opterr = 0;
4661 	while ((option = getopt_long(argc, argv, "tR:",
4662 	    etherstub_lopts, NULL)) != -1) {
4663 		switch (option) {
4664 		case 't':
4665 			flags &= ~DLADM_OPT_PERSIST;
4666 			break;
4667 		case 'R':
4668 			altroot = optarg;
4669 			break;
4670 		default:
4671 			die_opterr(optopt, option, use);
4672 		}
4673 	}
4674 
4675 	/* the etherstub id is the required operand */
4676 	if (optind != (argc - 1))
4677 		usage();
4678 
4679 	if (strlcpy(name, argv[optind], MAXLINKNAMELEN) >= MAXLINKNAMELEN)
4680 		die("link name too long '%s'", argv[optind]);
4681 
4682 	if (!dladm_valid_linkname(name))
4683 		die("invalid link name '%s'", argv[optind]);
4684 
4685 	if (altroot != NULL)
4686 		altroot_cmd(altroot, argc, argv);
4687 
4688 	status = dladm_vnic_create(handle, name, DATALINK_INVALID_LINKID,
4689 	    VNIC_MAC_ADDR_TYPE_AUTO, mac_addr, ETHERADDRL, NULL, 0, 0, NULL,
4690 	    NULL, flags);
4691 	if (status != DLADM_STATUS_OK)
4692 		die_dlerr(status, "etherstub creation failed");
4693 }
4694 
4695 static void
4696 do_delete_etherstub(int argc, char *argv[], const char *use)
4697 {
4698 	do_delete_vnic_common(argc, argv, use, B_TRUE);
4699 }
4700 
4701 /* ARGSUSED */
4702 static void
4703 do_show_etherstub(int argc, char *argv[], const char *use)
4704 {
4705 	do_show_vnic_common(argc, argv, use, B_TRUE);
4706 }
4707 
4708 /* ARGSUSED */
4709 static void
4710 do_up_simnet(int argc, char *argv[], const char *use)
4711 {
4712 	(void) dladm_simnet_up(handle, DATALINK_ALL_LINKID, 0);
4713 }
4714 
4715 static void
4716 do_create_simnet(int argc, char *argv[], const char *use)
4717 {
4718 	uint32_t flags;
4719 	char *altroot = NULL;
4720 	char *media = NULL;
4721 	uint32_t mtype = DL_ETHER;
4722 	int option;
4723 	dladm_status_t status;
4724 	char name[MAXLINKNAMELEN];
4725 
4726 	name[0] = '\0';
4727 	flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
4728 
4729 	opterr = 0;
4730 	while ((option = getopt_long(argc, argv, ":tR:m:",
4731 	    simnet_lopts, NULL)) != -1) {
4732 		switch (option) {
4733 		case 't':
4734 			flags &= ~DLADM_OPT_PERSIST;
4735 			break;
4736 		case 'R':
4737 			altroot = optarg;
4738 			break;
4739 		case 'm':
4740 			media = optarg;
4741 			break;
4742 		default:
4743 			die_opterr(optopt, option, use);
4744 		}
4745 	}
4746 
4747 	/* the simnet id is the required operand */
4748 	if (optind != (argc - 1))
4749 		usage();
4750 
4751 	if (strlcpy(name, argv[optind], MAXLINKNAMELEN) >= MAXLINKNAMELEN)
4752 		die("link name too long '%s'", argv[optind]);
4753 
4754 	if (!dladm_valid_linkname(name))
4755 		die("invalid link name '%s'", name);
4756 
4757 	if (media != NULL) {
4758 		mtype = dladm_str2media(media);
4759 		if (mtype != DL_ETHER && mtype != DL_WIFI)
4760 			die("media type '%s' is not supported", media);
4761 	}
4762 
4763 	if (altroot != NULL)
4764 		altroot_cmd(altroot, argc, argv);
4765 
4766 	status = dladm_simnet_create(handle, name, mtype, flags);
4767 	if (status != DLADM_STATUS_OK)
4768 		die_dlerr(status, "simnet creation failed");
4769 }
4770 
4771 static void
4772 do_delete_simnet(int argc, char *argv[], const char *use)
4773 {
4774 	int option;
4775 	uint32_t flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
4776 	datalink_id_t linkid;
4777 	char *altroot = NULL;
4778 	dladm_status_t status;
4779 	dladm_simnet_attr_t slinfo;
4780 
4781 	opterr = 0;
4782 	while ((option = getopt_long(argc, argv, ":tR:", simnet_lopts,
4783 	    NULL)) != -1) {
4784 		switch (option) {
4785 		case 't':
4786 			flags &= ~DLADM_OPT_PERSIST;
4787 			break;
4788 		case 'R':
4789 			altroot = optarg;
4790 			break;
4791 		default:
4792 			die_opterr(optopt, option, use);
4793 		}
4794 	}
4795 
4796 	/* get simnet name (required last argument) */
4797 	if (optind != (argc - 1))
4798 		usage();
4799 
4800 	if (!dladm_valid_linkname(argv[optind]))
4801 		die("invalid link name '%s'", argv[optind]);
4802 
4803 	if (altroot != NULL)
4804 		altroot_cmd(altroot, argc, argv);
4805 
4806 	status = dladm_name2info(handle, argv[optind], &linkid, NULL, NULL,
4807 	    NULL);
4808 	if (status != DLADM_STATUS_OK)
4809 		die("simnet '%s' not found", argv[optind]);
4810 
4811 	if ((status = dladm_simnet_info(handle, linkid, &slinfo,
4812 	    flags)) != DLADM_STATUS_OK)
4813 		die_dlerr(status, "failed to retrieve simnet information");
4814 
4815 	status = dladm_simnet_delete(handle, linkid, flags);
4816 	if (status != DLADM_STATUS_OK)
4817 		die_dlerr(status, "simnet deletion failed");
4818 }
4819 
4820 static void
4821 do_modify_simnet(int argc, char *argv[], const char *use)
4822 {
4823 	int option;
4824 	uint32_t flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
4825 	datalink_id_t linkid;
4826 	datalink_id_t peer_linkid;
4827 	char *altroot = NULL;
4828 	dladm_status_t status;
4829 	boolean_t p_arg = B_FALSE;
4830 
4831 	opterr = 0;
4832 	while ((option = getopt_long(argc, argv, ":tR:p:", simnet_lopts,
4833 	    NULL)) != -1) {
4834 		switch (option) {
4835 		case 't':
4836 			flags &= ~DLADM_OPT_PERSIST;
4837 			break;
4838 		case 'R':
4839 			altroot = optarg;
4840 			break;
4841 		case 'p':
4842 			if (p_arg)
4843 				die_optdup(option);
4844 			p_arg = B_TRUE;
4845 			if (strcasecmp(optarg, "none") == 0)
4846 				peer_linkid = DATALINK_INVALID_LINKID;
4847 			else if (dladm_name2info(handle, optarg, &peer_linkid,
4848 			    NULL, NULL, NULL) != DLADM_STATUS_OK)
4849 				die("invalid peer link name '%s'", optarg);
4850 			break;
4851 		default:
4852 			die_opterr(optopt, option, use);
4853 		}
4854 	}
4855 
4856 	/* get simnet name (required last argument) */
4857 	if (optind != (argc - 1))
4858 		usage();
4859 
4860 	/* Nothing to do if no peer link argument */
4861 	if (!p_arg)
4862 		return;
4863 
4864 	if (altroot != NULL)
4865 		altroot_cmd(altroot, argc, argv);
4866 
4867 	status = dladm_name2info(handle, argv[optind], &linkid, NULL, NULL,
4868 	    NULL);
4869 	if (status != DLADM_STATUS_OK)
4870 		die("invalid link name '%s'", argv[optind]);
4871 
4872 	status = dladm_simnet_modify(handle, linkid, peer_linkid, flags);
4873 	if (status != DLADM_STATUS_OK)
4874 		die_dlerr(status, "simnet modification failed");
4875 }
4876 
4877 static dladm_status_t
4878 print_simnet(show_state_t *state, datalink_id_t linkid)
4879 {
4880 	dladm_simnet_attr_t	slinfo;
4881 	uint32_t		flags;
4882 	dladm_status_t		status;
4883 	simnet_fields_buf_t	slbuf;
4884 	char			mstr[ETHERADDRL * 3];
4885 
4886 	bzero(&slbuf, sizeof (slbuf));
4887 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, NULL, NULL,
4888 	    slbuf.simnet_name, sizeof (slbuf.simnet_name)))
4889 	    != DLADM_STATUS_OK)
4890 		return (status);
4891 
4892 	if (!(state->ls_flags & flags))
4893 		return (DLADM_STATUS_NOTFOUND);
4894 
4895 	if ((status = dladm_simnet_info(handle, linkid, &slinfo,
4896 	    state->ls_flags)) != DLADM_STATUS_OK)
4897 		return (status);
4898 
4899 	if (slinfo.sna_peer_link_id != DATALINK_INVALID_LINKID &&
4900 	    (status = dladm_datalink_id2info(handle, slinfo.sna_peer_link_id,
4901 	    NULL, NULL, NULL, slbuf.simnet_otherlink,
4902 	    sizeof (slbuf.simnet_otherlink))) !=
4903 	    DLADM_STATUS_OK)
4904 		return (status);
4905 
4906 	if (slinfo.sna_mac_len > sizeof (slbuf.simnet_macaddr))
4907 		return (DLADM_STATUS_BADVAL);
4908 
4909 	(void) strlcpy(slbuf.simnet_macaddr,
4910 	    dladm_aggr_macaddr2str(slinfo.sna_mac_addr, mstr),
4911 	    sizeof (slbuf.simnet_macaddr));
4912 	(void) dladm_media2str(slinfo.sna_type, slbuf.simnet_media);
4913 
4914 	ofmt_print(state->ls_ofmt, &slbuf);
4915 	return (status);
4916 }
4917 
4918 /* ARGSUSED */
4919 static int
4920 show_simnet(dladm_handle_t dh, datalink_id_t linkid, void *arg)
4921 {
4922 	show_state_t		*state = arg;
4923 
4924 	state->ls_status = print_simnet(state, linkid);
4925 	return (DLADM_WALK_CONTINUE);
4926 }
4927 
4928 static void
4929 do_show_simnet(int argc, char *argv[], const char *use)
4930 {
4931 	int		option;
4932 	uint32_t	flags = DLADM_OPT_ACTIVE;
4933 	boolean_t	p_arg = B_FALSE;
4934 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
4935 	show_state_t	state;
4936 	dladm_status_t	status;
4937 	boolean_t	o_arg = B_FALSE;
4938 	ofmt_handle_t	ofmt;
4939 	ofmt_status_t	oferr;
4940 	char		*all_fields = "link,media,macaddress,otherlink";
4941 	char		*fields_str = all_fields;
4942 	uint_t		ofmtflags = 0;
4943 
4944 	bzero(&state, sizeof (state));
4945 
4946 	opterr = 0;
4947 	while ((option = getopt_long(argc, argv, ":pPo:",
4948 	    show_lopts, NULL)) != -1) {
4949 		switch (option) {
4950 		case 'p':
4951 			if (p_arg)
4952 				die_optdup(option);
4953 
4954 			p_arg = B_TRUE;
4955 			state.ls_parsable = p_arg;
4956 			break;
4957 		case 'P':
4958 			if (flags != DLADM_OPT_ACTIVE)
4959 				die_optdup(option);
4960 
4961 			flags = DLADM_OPT_PERSIST;
4962 			break;
4963 		case 'o':
4964 			o_arg = B_TRUE;
4965 			fields_str = optarg;
4966 			break;
4967 		default:
4968 			die_opterr(optopt, option, use);
4969 			break;
4970 		}
4971 	}
4972 
4973 	if (p_arg && !o_arg)
4974 		die("-p requires -o");
4975 
4976 	if (strcasecmp(fields_str, "all") == 0) {
4977 		if (p_arg)
4978 			die("\"-o all\" is invalid with -p");
4979 		fields_str = all_fields;
4980 	}
4981 
4982 	/* get link name (optional last argument) */
4983 	if (optind == (argc-1)) {
4984 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
4985 		    NULL, NULL, NULL)) != DLADM_STATUS_OK) {
4986 			die_dlerr(status, "link %s is not valid", argv[optind]);
4987 		}
4988 	} else if (optind != argc) {
4989 		usage();
4990 	}
4991 
4992 	state.ls_flags = flags;
4993 	state.ls_donefirst = B_FALSE;
4994 	if (state.ls_parsable)
4995 		ofmtflags |= OFMT_PARSABLE;
4996 	oferr = ofmt_open(fields_str, simnet_fields, ofmtflags, 0, &ofmt);
4997 	dladm_ofmt_check(oferr, state.ls_parsable, ofmt);
4998 	state.ls_ofmt = ofmt;
4999 
5000 	if (linkid == DATALINK_ALL_LINKID) {
5001 		(void) dladm_walk_datalink_id(show_simnet, handle, &state,
5002 		    DATALINK_CLASS_SIMNET, DATALINK_ANY_MEDIATYPE, flags);
5003 	} else {
5004 		(void) show_simnet(handle, linkid, &state);
5005 		if (state.ls_status != DLADM_STATUS_OK) {
5006 			ofmt_close(ofmt);
5007 			die_dlerr(state.ls_status, "failed to show simnet %s",
5008 			    argv[optind]);
5009 		}
5010 	}
5011 	ofmt_close(ofmt);
5012 }
5013 
5014 static void
5015 link_stats(datalink_id_t linkid, uint_t interval, char *fields_str,
5016     show_state_t *state)
5017 {
5018 	ofmt_handle_t	ofmt;
5019 	ofmt_status_t	oferr;
5020 	uint_t		ofmtflags = 0;
5021 
5022 	if (state->ls_parsable)
5023 		ofmtflags |= OFMT_PARSABLE;
5024 	oferr = ofmt_open(fields_str, link_s_fields, ofmtflags, 0, &ofmt);
5025 	dladm_ofmt_check(oferr, state->ls_parsable, ofmt);
5026 	state->ls_ofmt = ofmt;
5027 
5028 	/*
5029 	 * If an interval is specified, continuously show the stats
5030 	 * only for the first MAC port.
5031 	 */
5032 	state->ls_firstonly = (interval != 0);
5033 
5034 	for (;;) {
5035 		state->ls_donefirst = B_FALSE;
5036 		if (linkid == DATALINK_ALL_LINKID) {
5037 			(void) dladm_walk_datalink_id(show_link_stats, handle,
5038 			    state, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE,
5039 			    DLADM_OPT_ACTIVE);
5040 		} else {
5041 			(void) show_link_stats(handle, linkid, state);
5042 		}
5043 
5044 		if (interval == 0)
5045 			break;
5046 
5047 		(void) fflush(stdout);
5048 		(void) sleep(interval);
5049 	}
5050 	ofmt_close(ofmt);
5051 }
5052 
5053 static void
5054 aggr_stats(datalink_id_t linkid, show_grp_state_t *state, uint_t interval)
5055 {
5056 	/*
5057 	 * If an interval is specified, continuously show the stats
5058 	 * only for the first group.
5059 	 */
5060 	state->gs_firstonly = (interval != 0);
5061 
5062 	for (;;) {
5063 		state->gs_donefirst = B_FALSE;
5064 		if (linkid == DATALINK_ALL_LINKID)
5065 			(void) dladm_walk_datalink_id(show_aggr, handle, state,
5066 			    DATALINK_CLASS_AGGR, DATALINK_ANY_MEDIATYPE,
5067 			    DLADM_OPT_ACTIVE);
5068 		else
5069 			(void) show_aggr(handle, linkid, state);
5070 
5071 		if (interval == 0)
5072 			break;
5073 
5074 		(void) fflush(stdout);
5075 		(void) sleep(interval);
5076 	}
5077 }
5078 
5079 /* ARGSUSED */
5080 static void
5081 vnic_stats(show_vnic_state_t *sp, uint32_t interval)
5082 {
5083 	show_vnic_state_t	state;
5084 	boolean_t		specific_link, specific_dev;
5085 
5086 	/* Display vnic statistics */
5087 	dump_vnics_head(sp->vs_link);
5088 
5089 	bzero(&state, sizeof (state));
5090 	state.vs_stats = B_TRUE;
5091 	state.vs_vnic_id = sp->vs_vnic_id;
5092 	state.vs_link_id = sp->vs_link_id;
5093 
5094 	/*
5095 	 * If an interval is specified, and a vnic ID is not specified,
5096 	 * continuously show the stats only for the first vnic.
5097 	 */
5098 	specific_link = (sp->vs_vnic_id != DATALINK_ALL_LINKID);
5099 	specific_dev = (sp->vs_link_id != DATALINK_ALL_LINKID);
5100 
5101 	for (;;) {
5102 		/* Get stats for each vnic */
5103 		state.vs_found = B_FALSE;
5104 		state.vs_donefirst = B_FALSE;
5105 		state.vs_printstats = B_FALSE;
5106 		state.vs_flags = DLADM_OPT_ACTIVE;
5107 
5108 		if (!specific_link) {
5109 			(void) dladm_walk_datalink_id(show_vnic, handle, &state,
5110 			    DATALINK_CLASS_VNIC, DATALINK_ANY_MEDIATYPE,
5111 			    DLADM_OPT_ACTIVE);
5112 		} else {
5113 			(void) show_vnic(handle, sp->vs_vnic_id, &state);
5114 			if (state.vs_status != DLADM_STATUS_OK) {
5115 				die_dlerr(state.vs_status,
5116 				    "failed to show vnic '%s'", sp->vs_vnic);
5117 			}
5118 		}
5119 
5120 		if (specific_link && !state.vs_found)
5121 			die("non-existent vnic '%s'", sp->vs_vnic);
5122 		if (specific_dev && !state.vs_found)
5123 			die("device %s has no vnics", sp->vs_link);
5124 
5125 		/* Show totals */
5126 		if ((specific_link | specific_dev) && !interval) {
5127 			(void) printf("Total");
5128 			(void) printf("\t%-10llu",
5129 			    state.vs_totalstats.ipackets);
5130 			(void) printf("%-12llu",
5131 			    state.vs_totalstats.rbytes);
5132 			(void) printf("%-10llu",
5133 			    state.vs_totalstats.opackets);
5134 			(void) printf("%-12llu\n",
5135 			    state.vs_totalstats.obytes);
5136 		}
5137 
5138 		/* Show stats for each vnic */
5139 		state.vs_donefirst = B_FALSE;
5140 		state.vs_printstats = B_TRUE;
5141 
5142 		if (!specific_link) {
5143 			(void) dladm_walk_datalink_id(show_vnic, handle, &state,
5144 			    DATALINK_CLASS_VNIC, DATALINK_ANY_MEDIATYPE,
5145 			    DLADM_OPT_ACTIVE);
5146 		} else {
5147 			(void) show_vnic(handle, sp->vs_vnic_id, &state);
5148 			if (state.vs_status != DLADM_STATUS_OK) {
5149 				die_dlerr(state.vs_status,
5150 				    "failed to show vnic '%s'", sp->vs_vnic);
5151 			}
5152 		}
5153 
5154 		if (interval == 0)
5155 			break;
5156 
5157 		(void) fflush(stdout);
5158 		(void) sleep(interval);
5159 	}
5160 }
5161 
5162 static void
5163 get_mac_stats(const char *dev, pktsum_t *stats)
5164 {
5165 	kstat_ctl_t	*kcp;
5166 	kstat_t		*ksp;
5167 	char module[DLPI_LINKNAME_MAX];
5168 	uint_t instance;
5169 
5170 
5171 	bzero(stats, sizeof (*stats));
5172 
5173 	if (dlpi_parselink(dev, module, &instance) != DLPI_SUCCESS)
5174 		return;
5175 
5176 	if ((kcp = kstat_open()) == NULL) {
5177 		warn("kstat open operation failed");
5178 		return;
5179 	}
5180 
5181 	ksp = dladm_kstat_lookup(kcp, module, instance, "mac", NULL);
5182 	if (ksp != NULL)
5183 		dladm_get_stats(kcp, ksp, stats);
5184 
5185 	(void) kstat_close(kcp);
5186 
5187 }
5188 
5189 static void
5190 get_link_stats(const char *link, pktsum_t *stats)
5191 {
5192 	kstat_ctl_t	*kcp;
5193 	kstat_t		*ksp;
5194 
5195 	bzero(stats, sizeof (*stats));
5196 
5197 	if ((kcp = kstat_open()) == NULL) {
5198 		warn("kstat_open operation failed");
5199 		return;
5200 	}
5201 
5202 	ksp = dladm_kstat_lookup(kcp, "link", 0, link, NULL);
5203 
5204 	if (ksp != NULL)
5205 		dladm_get_stats(kcp, ksp, stats);
5206 
5207 	(void) kstat_close(kcp);
5208 }
5209 
5210 static int
5211 query_kstat(char *module, int instance, const char *name, const char *stat,
5212     uint8_t type, void *val)
5213 {
5214 	kstat_ctl_t	*kcp;
5215 	kstat_t		*ksp;
5216 
5217 	if ((kcp = kstat_open()) == NULL) {
5218 		warn("kstat open operation failed");
5219 		return (-1);
5220 	}
5221 
5222 	if ((ksp = kstat_lookup(kcp, module, instance, (char *)name)) == NULL) {
5223 		/*
5224 		 * The kstat query could fail if the underlying MAC
5225 		 * driver was already detached.
5226 		 */
5227 		goto bail;
5228 	}
5229 
5230 	if (kstat_read(kcp, ksp, NULL) == -1) {
5231 		warn("kstat read failed");
5232 		goto bail;
5233 	}
5234 
5235 	if (dladm_kstat_value(ksp, stat, type, val) < 0)
5236 		goto bail;
5237 
5238 	(void) kstat_close(kcp);
5239 	return (0);
5240 
5241 bail:
5242 	(void) kstat_close(kcp);
5243 	return (-1);
5244 }
5245 
5246 static int
5247 get_one_kstat(const char *name, const char *stat, uint8_t type,
5248     void *val, boolean_t islink)
5249 {
5250 	char		module[DLPI_LINKNAME_MAX];
5251 	uint_t		instance;
5252 
5253 	if (islink) {
5254 		return (query_kstat("link", 0, name, stat, type, val));
5255 	} else {
5256 		if (dlpi_parselink(name, module, &instance) != DLPI_SUCCESS)
5257 			return (-1);
5258 
5259 		return (query_kstat(module, instance, "mac", stat, type, val));
5260 	}
5261 }
5262 
5263 static uint64_t
5264 get_ifspeed(const char *name, boolean_t islink)
5265 {
5266 	uint64_t ifspeed = 0;
5267 
5268 	(void) get_one_kstat(name, "ifspeed", KSTAT_DATA_UINT64,
5269 	    &ifspeed, islink);
5270 
5271 	return (ifspeed);
5272 }
5273 
5274 static const char *
5275 get_linkstate(const char *name, boolean_t islink, char *buf)
5276 {
5277 	link_state_t	linkstate;
5278 
5279 	if (get_one_kstat(name, "link_state", KSTAT_DATA_UINT32,
5280 	    &linkstate, islink) != 0) {
5281 		(void) strlcpy(buf, "?", DLADM_STRSIZE);
5282 		return (buf);
5283 	}
5284 	return (dladm_linkstate2str(linkstate, buf));
5285 }
5286 
5287 static const char *
5288 get_linkduplex(const char *name, boolean_t islink, char *buf)
5289 {
5290 	link_duplex_t	linkduplex;
5291 
5292 	if (get_one_kstat(name, "link_duplex", KSTAT_DATA_UINT32,
5293 	    &linkduplex, islink) != 0) {
5294 		(void) strlcpy(buf, "unknown", DLADM_STRSIZE);
5295 		return (buf);
5296 	}
5297 
5298 	return (dladm_linkduplex2str(linkduplex, buf));
5299 }
5300 
5301 static int
5302 parse_wifi_fields(char *str, ofmt_handle_t *ofmt, uint_t cmdtype,
5303     boolean_t parsable)
5304 {
5305 	ofmt_field_t	*template, *of;
5306 	ofmt_cb_t	*fn;
5307 	ofmt_status_t	oferr;
5308 
5309 	if (cmdtype == WIFI_CMD_SCAN) {
5310 		template = wifi_common_fields;
5311 		if (str == NULL)
5312 			str = def_scan_wifi_fields;
5313 		if (strcasecmp(str, "all") == 0)
5314 			str = all_scan_wifi_fields;
5315 		fn = print_wlan_attr_cb;
5316 	} else if (cmdtype == WIFI_CMD_SHOW) {
5317 		bcopy(wifi_common_fields, &wifi_show_fields[2],
5318 		    sizeof (wifi_common_fields));
5319 		template = wifi_show_fields;
5320 		if (str == NULL)
5321 			str = def_show_wifi_fields;
5322 		if (strcasecmp(str, "all") == 0)
5323 			str = all_show_wifi_fields;
5324 		fn = print_link_attr_cb;
5325 	} else {
5326 		return (-1);
5327 	}
5328 
5329 	for (of = template; of->of_name != NULL; of++) {
5330 		if (of->of_cb == NULL)
5331 			of->of_cb = fn;
5332 	}
5333 
5334 	oferr = ofmt_open(str, template, (parsable ? OFMT_PARSABLE : 0),
5335 	    0, ofmt);
5336 	dladm_ofmt_check(oferr, parsable, *ofmt);
5337 	return (0);
5338 }
5339 
5340 typedef struct print_wifi_state {
5341 	char		*ws_link;
5342 	boolean_t	ws_parsable;
5343 	boolean_t	ws_header;
5344 	ofmt_handle_t	ws_ofmt;
5345 } print_wifi_state_t;
5346 
5347 typedef struct  wlan_scan_args_s {
5348 	print_wifi_state_t	*ws_state;
5349 	void			*ws_attr;
5350 } wlan_scan_args_t;
5351 
5352 static boolean_t
5353 print_wlan_attr_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
5354 {
5355 	wlan_scan_args_t	*w = ofarg->ofmt_cbarg;
5356 	print_wifi_state_t	*statep = w->ws_state;
5357 	dladm_wlan_attr_t	*attrp = w->ws_attr;
5358 	char			tmpbuf[DLADM_STRSIZE];
5359 
5360 	if (ofarg->ofmt_id == 0) {
5361 		(void) strlcpy(buf, (char *)statep->ws_link, bufsize);
5362 		return (B_TRUE);
5363 	}
5364 
5365 	if ((ofarg->ofmt_id & attrp->wa_valid) == 0)
5366 		return (B_TRUE);
5367 
5368 	switch (ofarg->ofmt_id) {
5369 	case DLADM_WLAN_ATTR_ESSID:
5370 		(void) dladm_wlan_essid2str(&attrp->wa_essid, tmpbuf);
5371 		break;
5372 	case DLADM_WLAN_ATTR_BSSID:
5373 		(void) dladm_wlan_bssid2str(&attrp->wa_bssid, tmpbuf);
5374 		break;
5375 	case DLADM_WLAN_ATTR_SECMODE:
5376 		(void) dladm_wlan_secmode2str(&attrp->wa_secmode, tmpbuf);
5377 		break;
5378 	case DLADM_WLAN_ATTR_STRENGTH:
5379 		(void) dladm_wlan_strength2str(&attrp->wa_strength, tmpbuf);
5380 		break;
5381 	case DLADM_WLAN_ATTR_MODE:
5382 		(void) dladm_wlan_mode2str(&attrp->wa_mode, tmpbuf);
5383 		break;
5384 	case DLADM_WLAN_ATTR_SPEED:
5385 		(void) dladm_wlan_speed2str(&attrp->wa_speed, tmpbuf);
5386 		(void) strlcat(tmpbuf, "Mb", sizeof (tmpbuf));
5387 		break;
5388 	case DLADM_WLAN_ATTR_AUTH:
5389 		(void) dladm_wlan_auth2str(&attrp->wa_auth, tmpbuf);
5390 		break;
5391 	case DLADM_WLAN_ATTR_BSSTYPE:
5392 		(void) dladm_wlan_bsstype2str(&attrp->wa_bsstype, tmpbuf);
5393 		break;
5394 	}
5395 	(void) strlcpy(buf, tmpbuf, bufsize);
5396 
5397 	return (B_TRUE);
5398 }
5399 
5400 static boolean_t
5401 print_scan_results(void *arg, dladm_wlan_attr_t *attrp)
5402 {
5403 	print_wifi_state_t	*statep = arg;
5404 	wlan_scan_args_t	warg;
5405 
5406 	bzero(&warg, sizeof (warg));
5407 	warg.ws_state = statep;
5408 	warg.ws_attr = attrp;
5409 	ofmt_print(statep->ws_ofmt, &warg);
5410 	return (B_TRUE);
5411 }
5412 
5413 static int
5414 scan_wifi(dladm_handle_t dh, datalink_id_t linkid, void *arg)
5415 {
5416 	print_wifi_state_t	*statep = arg;
5417 	dladm_status_t		status;
5418 	char			link[MAXLINKNAMELEN];
5419 
5420 	if ((status = dladm_datalink_id2info(dh, linkid, NULL, NULL, NULL, link,
5421 	    sizeof (link))) != DLADM_STATUS_OK) {
5422 		return (DLADM_WALK_CONTINUE);
5423 	}
5424 
5425 	statep->ws_link = link;
5426 	status = dladm_wlan_scan(dh, linkid, statep, print_scan_results);
5427 	if (status != DLADM_STATUS_OK)
5428 		die_dlerr(status, "cannot scan link '%s'", statep->ws_link);
5429 
5430 	return (DLADM_WALK_CONTINUE);
5431 }
5432 
5433 static boolean_t
5434 print_wifi_status_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
5435 {
5436 	static char		tmpbuf[DLADM_STRSIZE];
5437 	wlan_scan_args_t	*w = ofarg->ofmt_cbarg;
5438 	dladm_wlan_linkattr_t	*attrp = w->ws_attr;
5439 
5440 	if ((ofarg->ofmt_id & attrp->la_valid) != 0) {
5441 		(void) dladm_wlan_linkstatus2str(&attrp->la_status, tmpbuf);
5442 		(void) strlcpy(buf, tmpbuf, bufsize);
5443 	}
5444 	return (B_TRUE);
5445 }
5446 
5447 static boolean_t
5448 print_link_attr_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
5449 {
5450 	wlan_scan_args_t	*w = ofarg->ofmt_cbarg, w1;
5451 	print_wifi_state_t	*statep = w->ws_state;
5452 	dladm_wlan_linkattr_t	*attrp = w->ws_attr;
5453 
5454 	bzero(&w1, sizeof (w1));
5455 	w1.ws_state = statep;
5456 	w1.ws_attr = &attrp->la_wlan_attr;
5457 	ofarg->ofmt_cbarg = &w1;
5458 	return (print_wlan_attr_cb(ofarg, buf, bufsize));
5459 }
5460 
5461 static int
5462 show_wifi(dladm_handle_t dh, datalink_id_t linkid, void *arg)
5463 {
5464 	print_wifi_state_t	*statep = arg;
5465 	dladm_wlan_linkattr_t	attr;
5466 	dladm_status_t		status;
5467 	char			link[MAXLINKNAMELEN];
5468 	wlan_scan_args_t	warg;
5469 
5470 	if ((status = dladm_datalink_id2info(dh, linkid, NULL, NULL, NULL, link,
5471 	    sizeof (link))) != DLADM_STATUS_OK) {
5472 		return (DLADM_WALK_CONTINUE);
5473 	}
5474 
5475 	/* dladm_wlan_get_linkattr() memsets attr with 0 */
5476 	status = dladm_wlan_get_linkattr(dh, linkid, &attr);
5477 	if (status != DLADM_STATUS_OK)
5478 		die_dlerr(status, "cannot get link attributes for %s", link);
5479 
5480 	statep->ws_link = link;
5481 
5482 	bzero(&warg, sizeof (warg));
5483 	warg.ws_state = statep;
5484 	warg.ws_attr = &attr;
5485 	ofmt_print(statep->ws_ofmt, &warg);
5486 	return (DLADM_WALK_CONTINUE);
5487 }
5488 
5489 static void
5490 do_display_wifi(int argc, char **argv, int cmd, const char *use)
5491 {
5492 	int			option;
5493 	char			*fields_str = NULL;
5494 	int		(*callback)(dladm_handle_t, datalink_id_t, void *);
5495 	print_wifi_state_t	state;
5496 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
5497 	dladm_status_t		status;
5498 
5499 	if (cmd == WIFI_CMD_SCAN)
5500 		callback = scan_wifi;
5501 	else if (cmd == WIFI_CMD_SHOW)
5502 		callback = show_wifi;
5503 	else
5504 		return;
5505 
5506 	state.ws_parsable = B_FALSE;
5507 	state.ws_header = B_TRUE;
5508 	opterr = 0;
5509 	while ((option = getopt_long(argc, argv, ":o:p",
5510 	    wifi_longopts, NULL)) != -1) {
5511 		switch (option) {
5512 		case 'o':
5513 			fields_str = optarg;
5514 			break;
5515 		case 'p':
5516 			state.ws_parsable = B_TRUE;
5517 			break;
5518 		default:
5519 			die_opterr(optopt, option, use);
5520 		}
5521 	}
5522 
5523 	if (state.ws_parsable && fields_str == NULL)
5524 		die("-p requires -o");
5525 
5526 	if (state.ws_parsable && strcasecmp(fields_str, "all") == 0)
5527 		die("\"-o all\" is invalid with -p");
5528 
5529 	if (optind == (argc - 1)) {
5530 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
5531 		    NULL, NULL, NULL)) != DLADM_STATUS_OK) {
5532 			die_dlerr(status, "link %s is not valid", argv[optind]);
5533 		}
5534 	} else if (optind != argc) {
5535 		usage();
5536 	}
5537 
5538 	if (parse_wifi_fields(fields_str, &state.ws_ofmt, cmd,
5539 	    state.ws_parsable) < 0)
5540 		die("invalid field(s) specified");
5541 
5542 	if (linkid == DATALINK_ALL_LINKID) {
5543 		(void) dladm_walk_datalink_id(callback, handle, &state,
5544 		    DATALINK_CLASS_PHYS | DATALINK_CLASS_SIMNET,
5545 		    DL_WIFI, DLADM_OPT_ACTIVE);
5546 	} else {
5547 		(void) (*callback)(handle, linkid, &state);
5548 	}
5549 	ofmt_close(state.ws_ofmt);
5550 }
5551 
5552 static void
5553 do_scan_wifi(int argc, char **argv, const char *use)
5554 {
5555 	do_display_wifi(argc, argv, WIFI_CMD_SCAN, use);
5556 }
5557 
5558 static void
5559 do_show_wifi(int argc, char **argv, const char *use)
5560 {
5561 	do_display_wifi(argc, argv, WIFI_CMD_SHOW, use);
5562 }
5563 
5564 typedef struct wlan_count_attr {
5565 	uint_t		wc_count;
5566 	datalink_id_t	wc_linkid;
5567 } wlan_count_attr_t;
5568 
5569 /* ARGSUSED */
5570 static int
5571 do_count_wlan(dladm_handle_t dh, datalink_id_t linkid, void *arg)
5572 {
5573 	wlan_count_attr_t *cp = arg;
5574 
5575 	if (cp->wc_count == 0)
5576 		cp->wc_linkid = linkid;
5577 	cp->wc_count++;
5578 	return (DLADM_WALK_CONTINUE);
5579 }
5580 
5581 static int
5582 parse_wlan_keys(char *str, dladm_wlan_key_t **keys, uint_t *key_countp)
5583 {
5584 	uint_t			i;
5585 	dladm_wlan_key_t	*wk;
5586 	int			nfields = 1;
5587 	char			*field, *token, *lasts = NULL, c;
5588 
5589 	token = str;
5590 	while ((c = *token++) != NULL) {
5591 		if (c == ',')
5592 			nfields++;
5593 	}
5594 	token = strdup(str);
5595 	if (token == NULL)
5596 		return (-1);
5597 
5598 	wk = malloc(nfields * sizeof (dladm_wlan_key_t));
5599 	if (wk == NULL)
5600 		goto fail;
5601 
5602 	token = str;
5603 	for (i = 0; i < nfields; i++) {
5604 		char			*s;
5605 		dladm_secobj_class_t	class;
5606 		dladm_status_t		status;
5607 
5608 		field = strtok_r(token, ",", &lasts);
5609 		token = NULL;
5610 
5611 		(void) strlcpy(wk[i].wk_name, field,
5612 		    DLADM_WLAN_MAX_KEYNAME_LEN);
5613 
5614 		wk[i].wk_idx = 1;
5615 		if ((s = strrchr(wk[i].wk_name, ':')) != NULL) {
5616 			if (s[1] == '\0' || s[2] != '\0' || !isdigit(s[1]))
5617 				goto fail;
5618 
5619 			wk[i].wk_idx = (uint_t)(s[1] - '0');
5620 			*s = '\0';
5621 		}
5622 		wk[i].wk_len = DLADM_WLAN_MAX_KEY_LEN;
5623 
5624 		status = dladm_get_secobj(handle, wk[i].wk_name, &class,
5625 		    wk[i].wk_val, &wk[i].wk_len, 0);
5626 		if (status != DLADM_STATUS_OK) {
5627 			if (status == DLADM_STATUS_NOTFOUND) {
5628 				status = dladm_get_secobj(handle, wk[i].wk_name,
5629 				    &class, wk[i].wk_val, &wk[i].wk_len,
5630 				    DLADM_OPT_PERSIST);
5631 			}
5632 			if (status != DLADM_STATUS_OK)
5633 				goto fail;
5634 		}
5635 		wk[i].wk_class = class;
5636 	}
5637 	*keys = wk;
5638 	*key_countp = i;
5639 	free(token);
5640 	return (0);
5641 fail:
5642 	free(wk);
5643 	free(token);
5644 	return (-1);
5645 }
5646 
5647 static void
5648 do_connect_wifi(int argc, char **argv, const char *use)
5649 {
5650 	int			option;
5651 	dladm_wlan_attr_t	attr, *attrp;
5652 	dladm_status_t		status = DLADM_STATUS_OK;
5653 	int			timeout = DLADM_WLAN_CONNECT_TIMEOUT_DEFAULT;
5654 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
5655 	dladm_wlan_key_t	*keys = NULL;
5656 	uint_t			key_count = 0;
5657 	uint_t			flags = 0;
5658 	dladm_wlan_secmode_t	keysecmode = DLADM_WLAN_SECMODE_NONE;
5659 	char			buf[DLADM_STRSIZE];
5660 
5661 	opterr = 0;
5662 	(void) memset(&attr, 0, sizeof (attr));
5663 	while ((option = getopt_long(argc, argv, ":e:i:a:m:b:s:k:T:c",
5664 	    wifi_longopts, NULL)) != -1) {
5665 		switch (option) {
5666 		case 'e':
5667 			status = dladm_wlan_str2essid(optarg, &attr.wa_essid);
5668 			if (status != DLADM_STATUS_OK)
5669 				die("invalid ESSID '%s'", optarg);
5670 
5671 			attr.wa_valid |= DLADM_WLAN_ATTR_ESSID;
5672 			/*
5673 			 * Try to connect without doing a scan.
5674 			 */
5675 			flags |= DLADM_WLAN_CONNECT_NOSCAN;
5676 			break;
5677 		case 'i':
5678 			status = dladm_wlan_str2bssid(optarg, &attr.wa_bssid);
5679 			if (status != DLADM_STATUS_OK)
5680 				die("invalid BSSID %s", optarg);
5681 
5682 			attr.wa_valid |= DLADM_WLAN_ATTR_BSSID;
5683 			break;
5684 		case 'a':
5685 			status = dladm_wlan_str2auth(optarg, &attr.wa_auth);
5686 			if (status != DLADM_STATUS_OK)
5687 				die("invalid authentication mode '%s'", optarg);
5688 
5689 			attr.wa_valid |= DLADM_WLAN_ATTR_AUTH;
5690 			break;
5691 		case 'm':
5692 			status = dladm_wlan_str2mode(optarg, &attr.wa_mode);
5693 			if (status != DLADM_STATUS_OK)
5694 				die("invalid mode '%s'", optarg);
5695 
5696 			attr.wa_valid |= DLADM_WLAN_ATTR_MODE;
5697 			break;
5698 		case 'b':
5699 			if ((status = dladm_wlan_str2bsstype(optarg,
5700 			    &attr.wa_bsstype)) != DLADM_STATUS_OK) {
5701 				die("invalid bsstype '%s'", optarg);
5702 			}
5703 
5704 			attr.wa_valid |= DLADM_WLAN_ATTR_BSSTYPE;
5705 			break;
5706 		case 's':
5707 			if ((status = dladm_wlan_str2secmode(optarg,
5708 			    &attr.wa_secmode)) != DLADM_STATUS_OK) {
5709 				die("invalid security mode '%s'", optarg);
5710 			}
5711 
5712 			attr.wa_valid |= DLADM_WLAN_ATTR_SECMODE;
5713 			break;
5714 		case 'k':
5715 			if (parse_wlan_keys(optarg, &keys, &key_count) < 0)
5716 				die("invalid key(s) '%s'", optarg);
5717 
5718 			if (keys[0].wk_class == DLADM_SECOBJ_CLASS_WEP)
5719 				keysecmode = DLADM_WLAN_SECMODE_WEP;
5720 			else
5721 				keysecmode = DLADM_WLAN_SECMODE_WPA;
5722 			break;
5723 		case 'T':
5724 			if (strcasecmp(optarg, "forever") == 0) {
5725 				timeout = -1;
5726 				break;
5727 			}
5728 			if (!str2int(optarg, &timeout) || timeout < 0)
5729 				die("invalid timeout value '%s'", optarg);
5730 			break;
5731 		case 'c':
5732 			flags |= DLADM_WLAN_CONNECT_CREATEIBSS;
5733 			flags |= DLADM_WLAN_CONNECT_CREATEIBSS;
5734 			break;
5735 		default:
5736 			die_opterr(optopt, option, use);
5737 			break;
5738 		}
5739 	}
5740 
5741 	if (keysecmode == DLADM_WLAN_SECMODE_NONE) {
5742 		if ((attr.wa_valid & DLADM_WLAN_ATTR_SECMODE) != 0) {
5743 			die("key required for security mode '%s'",
5744 			    dladm_wlan_secmode2str(&attr.wa_secmode, buf));
5745 		}
5746 	} else {
5747 		if ((attr.wa_valid & DLADM_WLAN_ATTR_SECMODE) != 0 &&
5748 		    attr.wa_secmode != keysecmode)
5749 			die("incompatible -s and -k options");
5750 		attr.wa_valid |= DLADM_WLAN_ATTR_SECMODE;
5751 		attr.wa_secmode = keysecmode;
5752 	}
5753 
5754 	if (optind == (argc - 1)) {
5755 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
5756 		    NULL, NULL, NULL)) != DLADM_STATUS_OK) {
5757 			die_dlerr(status, "link %s is not valid", argv[optind]);
5758 		}
5759 	} else if (optind != argc) {
5760 		usage();
5761 	}
5762 
5763 	if (linkid == DATALINK_ALL_LINKID) {
5764 		wlan_count_attr_t wcattr;
5765 
5766 		wcattr.wc_linkid = DATALINK_INVALID_LINKID;
5767 		wcattr.wc_count = 0;
5768 		(void) dladm_walk_datalink_id(do_count_wlan, handle, &wcattr,
5769 		    DATALINK_CLASS_PHYS | DATALINK_CLASS_SIMNET,
5770 		    DL_WIFI, DLADM_OPT_ACTIVE);
5771 		if (wcattr.wc_count == 0) {
5772 			die("no wifi links are available");
5773 		} else if (wcattr.wc_count > 1) {
5774 			die("link name is required when more than one wifi "
5775 			    "link is available");
5776 		}
5777 		linkid = wcattr.wc_linkid;
5778 	}
5779 	attrp = (attr.wa_valid == 0) ? NULL : &attr;
5780 again:
5781 	if ((status = dladm_wlan_connect(handle, linkid, attrp, timeout, keys,
5782 	    key_count, flags)) != DLADM_STATUS_OK) {
5783 		if ((flags & DLADM_WLAN_CONNECT_NOSCAN) != 0) {
5784 			/*
5785 			 * Try again with scanning and filtering.
5786 			 */
5787 			flags &= ~DLADM_WLAN_CONNECT_NOSCAN;
5788 			goto again;
5789 		}
5790 
5791 		if (status == DLADM_STATUS_NOTFOUND) {
5792 			if (attr.wa_valid == 0) {
5793 				die("no wifi networks are available");
5794 			} else {
5795 				die("no wifi networks with the specified "
5796 				    "criteria are available");
5797 			}
5798 		}
5799 		die_dlerr(status, "cannot connect");
5800 	}
5801 	free(keys);
5802 }
5803 
5804 /* ARGSUSED */
5805 static int
5806 do_all_disconnect_wifi(dladm_handle_t dh, datalink_id_t linkid, void *arg)
5807 {
5808 	dladm_status_t	status;
5809 
5810 	status = dladm_wlan_disconnect(dh, linkid);
5811 	if (status != DLADM_STATUS_OK)
5812 		warn_dlerr(status, "cannot disconnect link");
5813 
5814 	return (DLADM_WALK_CONTINUE);
5815 }
5816 
5817 static void
5818 do_disconnect_wifi(int argc, char **argv, const char *use)
5819 {
5820 	int			option;
5821 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
5822 	boolean_t		all_links = B_FALSE;
5823 	dladm_status_t		status;
5824 	wlan_count_attr_t	wcattr;
5825 
5826 	opterr = 0;
5827 	while ((option = getopt_long(argc, argv, ":a",
5828 	    wifi_longopts, NULL)) != -1) {
5829 		switch (option) {
5830 		case 'a':
5831 			all_links = B_TRUE;
5832 			break;
5833 		default:
5834 			die_opterr(optopt, option, use);
5835 			break;
5836 		}
5837 	}
5838 
5839 	if (optind == (argc - 1)) {
5840 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
5841 		    NULL, NULL, NULL)) != DLADM_STATUS_OK) {
5842 			die_dlerr(status, "link %s is not valid", argv[optind]);
5843 		}
5844 	} else if (optind != argc) {
5845 		usage();
5846 	}
5847 
5848 	if (linkid == DATALINK_ALL_LINKID) {
5849 		if (!all_links) {
5850 			wcattr.wc_linkid = linkid;
5851 			wcattr.wc_count = 0;
5852 			(void) dladm_walk_datalink_id(do_count_wlan, handle,
5853 			    &wcattr,
5854 			    DATALINK_CLASS_PHYS | DATALINK_CLASS_SIMNET,
5855 			    DL_WIFI, DLADM_OPT_ACTIVE);
5856 			if (wcattr.wc_count == 0) {
5857 				die("no wifi links are available");
5858 			} else if (wcattr.wc_count > 1) {
5859 				die("link name is required when more than "
5860 				    "one wifi link is available");
5861 			}
5862 			linkid = wcattr.wc_linkid;
5863 		} else {
5864 			(void) dladm_walk_datalink_id(do_all_disconnect_wifi,
5865 			    handle, NULL,
5866 			    DATALINK_CLASS_PHYS | DATALINK_CLASS_SIMNET,
5867 			    DL_WIFI, DLADM_OPT_ACTIVE);
5868 			return;
5869 		}
5870 	}
5871 	status = dladm_wlan_disconnect(handle, linkid);
5872 	if (status != DLADM_STATUS_OK)
5873 		die_dlerr(status, "cannot disconnect");
5874 }
5875 
5876 static void
5877 print_linkprop(datalink_id_t linkid, show_linkprop_state_t *statep,
5878     const char *propname, dladm_prop_type_t type, const char *format,
5879     char **pptr)
5880 {
5881 	int		i;
5882 	char		*ptr, *lim;
5883 	char		buf[DLADM_STRSIZE];
5884 	char		*unknown = "--", *notsup = "";
5885 	char		**propvals = statep->ls_propvals;
5886 	uint_t		valcnt = DLADM_MAX_PROP_VALCNT;
5887 	dladm_status_t	status;
5888 
5889 	status = dladm_get_linkprop(handle, linkid, type, propname, propvals,
5890 	    &valcnt);
5891 	if (status != DLADM_STATUS_OK) {
5892 		if (status == DLADM_STATUS_TEMPONLY) {
5893 			if (type == DLADM_PROP_VAL_MODIFIABLE &&
5894 			    statep->ls_persist) {
5895 				valcnt = 1;
5896 				propvals = &unknown;
5897 			} else {
5898 				statep->ls_status = status;
5899 				statep->ls_retstatus = status;
5900 				return;
5901 			}
5902 		} else if (status == DLADM_STATUS_NOTSUP ||
5903 		    statep->ls_persist) {
5904 			valcnt = 1;
5905 			if (type == DLADM_PROP_VAL_CURRENT ||
5906 			    type == DLADM_PROP_VAL_PERM)
5907 				propvals = &unknown;
5908 			else
5909 				propvals = &notsup;
5910 		} else if (status == DLADM_STATUS_NOTDEFINED) {
5911 			propvals = &notsup; /* STR_UNDEF_VAL */
5912 		} else {
5913 			if (statep->ls_proplist &&
5914 			    statep->ls_status == DLADM_STATUS_OK) {
5915 				warn_dlerr(status,
5916 				    "cannot get link property '%s' for %s",
5917 				    propname, statep->ls_link);
5918 			}
5919 			statep->ls_status = status;
5920 			statep->ls_retstatus = status;
5921 			return;
5922 		}
5923 	}
5924 
5925 	statep->ls_status = DLADM_STATUS_OK;
5926 
5927 	ptr = buf;
5928 	lim = buf + DLADM_STRSIZE;
5929 	for (i = 0; i < valcnt; i++) {
5930 		if (propvals[i][0] == '\0' && !statep->ls_parsable)
5931 			ptr += snprintf(ptr, lim - ptr, "--,");
5932 		else
5933 			ptr += snprintf(ptr, lim - ptr, "%s,", propvals[i]);
5934 		if (ptr >= lim)
5935 			break;
5936 	}
5937 	if (valcnt > 0)
5938 		buf[strlen(buf) - 1] = '\0';
5939 
5940 	lim = statep->ls_line + MAX_PROP_LINE;
5941 	if (statep->ls_parsable) {
5942 		*pptr += snprintf(*pptr, lim - *pptr,
5943 		    "%s", buf);
5944 	} else {
5945 		*pptr += snprintf(*pptr, lim - *pptr, format, buf);
5946 	}
5947 }
5948 
5949 static boolean_t
5950 print_linkprop_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
5951 {
5952 	linkprop_args_t		*arg = ofarg->ofmt_cbarg;
5953 	char 			*propname = arg->ls_propname;
5954 	show_linkprop_state_t	*statep = arg->ls_state;
5955 	char			*ptr = statep->ls_line;
5956 	char			*lim = ptr + MAX_PROP_LINE;
5957 	datalink_id_t		linkid = arg->ls_linkid;
5958 
5959 	switch (ofarg->ofmt_id) {
5960 	case LINKPROP_LINK:
5961 		(void) snprintf(ptr, lim - ptr, "%s", statep->ls_link);
5962 		break;
5963 	case LINKPROP_PROPERTY:
5964 		(void) snprintf(ptr, lim - ptr, "%s", propname);
5965 		break;
5966 	case LINKPROP_VALUE:
5967 		print_linkprop(linkid, statep, propname,
5968 		    statep->ls_persist ? DLADM_PROP_VAL_PERSISTENT :
5969 		    DLADM_PROP_VAL_CURRENT, "%s", &ptr);
5970 		/*
5971 		 * If we failed to query the link property, for example, query
5972 		 * the persistent value of a non-persistable link property,
5973 		 * simply skip the output.
5974 		 */
5975 		if (statep->ls_status != DLADM_STATUS_OK)
5976 			goto skip;
5977 		ptr = statep->ls_line;
5978 		break;
5979 	case LINKPROP_PERM:
5980 		print_linkprop(linkid, statep, propname,
5981 		    DLADM_PROP_VAL_PERM, "%s", &ptr);
5982 		if (statep->ls_status != DLADM_STATUS_OK)
5983 			goto skip;
5984 		ptr = statep->ls_line;
5985 		break;
5986 	case LINKPROP_DEFAULT:
5987 		print_linkprop(linkid, statep, propname,
5988 		    DLADM_PROP_VAL_DEFAULT, "%s", &ptr);
5989 		if (statep->ls_status != DLADM_STATUS_OK)
5990 			goto skip;
5991 		ptr = statep->ls_line;
5992 		break;
5993 	case LINKPROP_POSSIBLE:
5994 		print_linkprop(linkid, statep, propname,
5995 		    DLADM_PROP_VAL_MODIFIABLE, "%s ", &ptr);
5996 		if (statep->ls_status != DLADM_STATUS_OK)
5997 			goto skip;
5998 		ptr = statep->ls_line;
5999 		break;
6000 	default:
6001 		die("invalid input");
6002 		break;
6003 	}
6004 	(void) strlcpy(buf, ptr, bufsize);
6005 	return (B_TRUE);
6006 skip:
6007 	return ((statep->ls_status == DLADM_STATUS_OK) ?
6008 	    B_TRUE : B_FALSE);
6009 }
6010 
6011 static boolean_t
6012 linkprop_is_supported(datalink_id_t  linkid, const char *propname,
6013     show_linkprop_state_t *statep)
6014 {
6015 	dladm_status_t	status;
6016 	uint_t		valcnt = DLADM_MAX_PROP_VALCNT;
6017 
6018 	/* if used with -p flag, always print output */
6019 	if (statep->ls_proplist != NULL)
6020 		return (B_TRUE);
6021 
6022 	status = dladm_get_linkprop(handle, linkid, DLADM_PROP_VAL_DEFAULT,
6023 	    propname, statep->ls_propvals, &valcnt);
6024 
6025 	if (status == DLADM_STATUS_OK)
6026 		return (B_TRUE);
6027 
6028 	/*
6029 	 * A system wide default value is not available for the
6030 	 * property. Check if current value can be retrieved.
6031 	 */
6032 	status = dladm_get_linkprop(handle, linkid, DLADM_PROP_VAL_CURRENT,
6033 	    propname, statep->ls_propvals, &valcnt);
6034 
6035 	return (status == DLADM_STATUS_OK);
6036 }
6037 
6038 /* ARGSUSED */
6039 static int
6040 show_linkprop(dladm_handle_t dh, datalink_id_t linkid, const char *propname,
6041     void *arg)
6042 {
6043 	show_linkprop_state_t	*statep = arg;
6044 	linkprop_args_t		ls_arg;
6045 
6046 	bzero(&ls_arg, sizeof (ls_arg));
6047 	ls_arg.ls_state = statep;
6048 	ls_arg.ls_propname = (char *)propname;
6049 	ls_arg.ls_linkid = linkid;
6050 
6051 	/*
6052 	 * This will need to be fixed when kernel interfaces are added
6053 	 * to enable walking of all known private properties. For now,
6054 	 * we are limited to walking persistent private properties only.
6055 	 */
6056 	if ((propname[0] == '_') && !statep->ls_persist &&
6057 	    (statep->ls_proplist == NULL))
6058 		return (DLADM_WALK_CONTINUE);
6059 	if (!statep->ls_parsable &&
6060 	    !linkprop_is_supported(linkid, propname, statep))
6061 		return (DLADM_WALK_CONTINUE);
6062 
6063 	ofmt_print(statep->ls_ofmt, &ls_arg);
6064 
6065 	return (DLADM_WALK_CONTINUE);
6066 }
6067 
6068 static void
6069 do_show_linkprop(int argc, char **argv, const char *use)
6070 {
6071 	int			option;
6072 	char			propstr[DLADM_STRSIZE];
6073 	dladm_arg_list_t	*proplist = NULL;
6074 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
6075 	show_linkprop_state_t	state;
6076 	uint32_t		flags = DLADM_OPT_ACTIVE;
6077 	dladm_status_t		status;
6078 	char			*fields_str = NULL;
6079 	ofmt_handle_t		ofmt;
6080 	ofmt_status_t		oferr;
6081 	uint_t			ofmtflags = 0;
6082 
6083 	bzero(propstr, DLADM_STRSIZE);
6084 	opterr = 0;
6085 	state.ls_propvals = NULL;
6086 	state.ls_line = NULL;
6087 	state.ls_parsable = B_FALSE;
6088 	state.ls_persist = B_FALSE;
6089 	state.ls_header = B_TRUE;
6090 	state.ls_retstatus = DLADM_STATUS_OK;
6091 
6092 	while ((option = getopt_long(argc, argv, ":p:cPo:",
6093 	    prop_longopts, NULL)) != -1) {
6094 		switch (option) {
6095 		case 'p':
6096 			(void) strlcat(propstr, optarg, DLADM_STRSIZE);
6097 			if (strlcat(propstr, ",", DLADM_STRSIZE) >=
6098 			    DLADM_STRSIZE)
6099 				die("property list too long '%s'", propstr);
6100 			break;
6101 		case 'c':
6102 			state.ls_parsable = B_TRUE;
6103 			break;
6104 		case 'P':
6105 			state.ls_persist = B_TRUE;
6106 			flags = DLADM_OPT_PERSIST;
6107 			break;
6108 		case 'o':
6109 			fields_str = optarg;
6110 			break;
6111 		default:
6112 			die_opterr(optopt, option, use);
6113 			break;
6114 		}
6115 	}
6116 
6117 	if (optind == (argc - 1)) {
6118 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
6119 		    NULL, NULL, NULL)) != DLADM_STATUS_OK) {
6120 			die_dlerr(status, "link %s is not valid", argv[optind]);
6121 		}
6122 	} else if (optind != argc) {
6123 		usage();
6124 	}
6125 
6126 	if (dladm_parse_link_props(propstr, &proplist, B_TRUE)
6127 	    != DLADM_STATUS_OK)
6128 		die("invalid link properties specified");
6129 	state.ls_proplist = proplist;
6130 	state.ls_status = DLADM_STATUS_OK;
6131 
6132 	if (state.ls_parsable)
6133 		ofmtflags |= OFMT_PARSABLE;
6134 	oferr = ofmt_open(fields_str, linkprop_fields, ofmtflags, 0, &ofmt);
6135 	dladm_ofmt_check(oferr, state.ls_parsable, ofmt);
6136 	state.ls_ofmt = ofmt;
6137 
6138 	if (linkid == DATALINK_ALL_LINKID) {
6139 		(void) dladm_walk_datalink_id(show_linkprop_onelink, handle,
6140 		    &state, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE, flags);
6141 	} else {
6142 		(void) show_linkprop_onelink(handle, linkid, &state);
6143 	}
6144 	ofmt_close(ofmt);
6145 	dladm_free_props(proplist);
6146 
6147 	if (state.ls_retstatus != DLADM_STATUS_OK) {
6148 		dladm_close(handle);
6149 		exit(EXIT_FAILURE);
6150 	}
6151 }
6152 
6153 static int
6154 show_linkprop_onelink(dladm_handle_t hdl, datalink_id_t linkid, void *arg)
6155 {
6156 	int			i;
6157 	char			*buf;
6158 	uint32_t		flags;
6159 	dladm_arg_list_t	*proplist = NULL;
6160 	show_linkprop_state_t	*statep = arg;
6161 	dlpi_handle_t		dh = NULL;
6162 
6163 	statep->ls_status = DLADM_STATUS_OK;
6164 
6165 	if (dladm_datalink_id2info(hdl, linkid, &flags, NULL, NULL,
6166 	    statep->ls_link, MAXLINKNAMELEN) != DLADM_STATUS_OK) {
6167 		statep->ls_status = DLADM_STATUS_NOTFOUND;
6168 		return (DLADM_WALK_CONTINUE);
6169 	}
6170 
6171 	if ((statep->ls_persist && !(flags & DLADM_OPT_PERSIST)) ||
6172 	    (!statep->ls_persist && !(flags & DLADM_OPT_ACTIVE))) {
6173 		statep->ls_status = DLADM_STATUS_BADARG;
6174 		return (DLADM_WALK_CONTINUE);
6175 	}
6176 
6177 	proplist = statep->ls_proplist;
6178 
6179 	/*
6180 	 * When some WiFi links are opened for the first time, their hardware
6181 	 * automatically scans for APs and does other slow operations.	Thus,
6182 	 * if there are no open links, the retrieval of link properties
6183 	 * (below) will proceed slowly unless we hold the link open.
6184 	 *
6185 	 * Note that failure of dlpi_open() does not necessarily mean invalid
6186 	 * link properties, because dlpi_open() may fail because of incorrect
6187 	 * autopush configuration. Therefore, we ingore the return value of
6188 	 * dlpi_open().
6189 	 */
6190 	if (!statep->ls_persist)
6191 		(void) dlpi_open(statep->ls_link, &dh, 0);
6192 
6193 	buf = malloc((sizeof (char *) + DLADM_PROP_VAL_MAX) *
6194 	    DLADM_MAX_PROP_VALCNT + MAX_PROP_LINE);
6195 	if (buf == NULL)
6196 		die("insufficient memory");
6197 
6198 	statep->ls_propvals = (char **)(void *)buf;
6199 	for (i = 0; i < DLADM_MAX_PROP_VALCNT; i++) {
6200 		statep->ls_propvals[i] = buf +
6201 		    sizeof (char *) * DLADM_MAX_PROP_VALCNT +
6202 		    i * DLADM_PROP_VAL_MAX;
6203 	}
6204 	statep->ls_line = buf +
6205 	    (sizeof (char *) + DLADM_PROP_VAL_MAX) * DLADM_MAX_PROP_VALCNT;
6206 
6207 	if (proplist != NULL) {
6208 		for (i = 0; i < proplist->al_count; i++) {
6209 			(void) show_linkprop(hdl, linkid,
6210 			    proplist->al_info[i].ai_name, statep);
6211 		}
6212 	} else {
6213 		(void) dladm_walk_linkprop(hdl, linkid, statep,
6214 		    show_linkprop);
6215 	}
6216 	if (dh != NULL)
6217 		dlpi_close(dh);
6218 	free(buf);
6219 	return (DLADM_WALK_CONTINUE);
6220 }
6221 
6222 static int
6223 reset_one_linkprop(dladm_handle_t dh, datalink_id_t linkid,
6224     const char *propname, void *arg)
6225 {
6226 	set_linkprop_state_t	*statep = arg;
6227 	dladm_status_t		status;
6228 
6229 	status = dladm_set_linkprop(dh, linkid, propname, NULL, 0,
6230 	    DLADM_OPT_ACTIVE | (statep->ls_temp ? 0 : DLADM_OPT_PERSIST));
6231 	if (status != DLADM_STATUS_OK &&
6232 	    status != DLADM_STATUS_PROPRDONLY &&
6233 	    status != DLADM_STATUS_NOTSUP) {
6234 		warn_dlerr(status, "cannot reset link property '%s' on '%s'",
6235 		    propname, statep->ls_name);
6236 		statep->ls_status = status;
6237 	}
6238 
6239 	return (DLADM_WALK_CONTINUE);
6240 }
6241 
6242 static void
6243 set_linkprop(int argc, char **argv, boolean_t reset, const char *use)
6244 {
6245 	int			i, option;
6246 	char			errmsg[DLADM_STRSIZE];
6247 	char			*altroot = NULL;
6248 	datalink_id_t		linkid;
6249 	boolean_t		temp = B_FALSE;
6250 	dladm_status_t		status = DLADM_STATUS_OK;
6251 	char			propstr[DLADM_STRSIZE];
6252 	dladm_arg_list_t	*proplist = NULL;
6253 
6254 	opterr = 0;
6255 	bzero(propstr, DLADM_STRSIZE);
6256 
6257 	while ((option = getopt_long(argc, argv, ":p:R:t",
6258 	    prop_longopts, NULL)) != -1) {
6259 		switch (option) {
6260 		case 'p':
6261 			(void) strlcat(propstr, optarg, DLADM_STRSIZE);
6262 			if (strlcat(propstr, ",", DLADM_STRSIZE) >=
6263 			    DLADM_STRSIZE)
6264 				die("property list too long '%s'", propstr);
6265 			break;
6266 		case 't':
6267 			temp = B_TRUE;
6268 			break;
6269 		case 'R':
6270 			altroot = optarg;
6271 			break;
6272 		default:
6273 			die_opterr(optopt, option, use);
6274 
6275 		}
6276 	}
6277 
6278 	/* get link name (required last argument) */
6279 	if (optind != (argc - 1))
6280 		usage();
6281 
6282 	if (dladm_parse_link_props(propstr, &proplist, reset) !=
6283 	    DLADM_STATUS_OK)
6284 		die("invalid link properties specified");
6285 
6286 	if (proplist == NULL && !reset)
6287 		die("link property must be specified");
6288 
6289 	if (altroot != NULL) {
6290 		dladm_free_props(proplist);
6291 		altroot_cmd(altroot, argc, argv);
6292 	}
6293 
6294 	status = dladm_name2info(handle, argv[optind], &linkid, NULL, NULL,
6295 	    NULL);
6296 	if (status != DLADM_STATUS_OK)
6297 		die_dlerr(status, "link %s is not valid", argv[optind]);
6298 
6299 	if (proplist == NULL) {
6300 		set_linkprop_state_t	state;
6301 
6302 		state.ls_name = argv[optind];
6303 		state.ls_reset = reset;
6304 		state.ls_temp = temp;
6305 		state.ls_status = DLADM_STATUS_OK;
6306 
6307 		(void) dladm_walk_linkprop(handle, linkid, &state,
6308 		    reset_one_linkprop);
6309 
6310 		status = state.ls_status;
6311 		goto done;
6312 	}
6313 
6314 	for (i = 0; i < proplist->al_count; i++) {
6315 		dladm_arg_info_t	*aip = &proplist->al_info[i];
6316 		char		**val;
6317 		uint_t		count;
6318 
6319 		if (reset) {
6320 			val = NULL;
6321 			count = 0;
6322 		} else {
6323 			val = aip->ai_val;
6324 			count = aip->ai_count;
6325 			if (count == 0) {
6326 				warn("no value specified for '%s'",
6327 				    aip->ai_name);
6328 				status = DLADM_STATUS_BADARG;
6329 				continue;
6330 			}
6331 		}
6332 		status = dladm_set_linkprop(handle, linkid, aip->ai_name, val,
6333 		    count, DLADM_OPT_ACTIVE | (temp ? 0 : DLADM_OPT_PERSIST));
6334 		switch (status) {
6335 		case DLADM_STATUS_OK:
6336 			break;
6337 		case DLADM_STATUS_NOTFOUND:
6338 			warn("invalid link property '%s'", aip->ai_name);
6339 			break;
6340 		case DLADM_STATUS_BADVAL: {
6341 			int		j;
6342 			char		*ptr, *lim;
6343 			char		**propvals = NULL;
6344 			uint_t		valcnt = DLADM_MAX_PROP_VALCNT;
6345 			dladm_status_t	s;
6346 
6347 			ptr = malloc((sizeof (char *) +
6348 			    DLADM_PROP_VAL_MAX) * DLADM_MAX_PROP_VALCNT +
6349 			    MAX_PROP_LINE);
6350 
6351 			propvals = (char **)(void *)ptr;
6352 			if (propvals == NULL)
6353 				die("insufficient memory");
6354 
6355 			for (j = 0; j < DLADM_MAX_PROP_VALCNT; j++) {
6356 				propvals[j] = ptr + sizeof (char *) *
6357 				    DLADM_MAX_PROP_VALCNT +
6358 				    j * DLADM_PROP_VAL_MAX;
6359 			}
6360 			s = dladm_get_linkprop(handle, linkid,
6361 			    DLADM_PROP_VAL_MODIFIABLE, aip->ai_name, propvals,
6362 			    &valcnt);
6363 
6364 			if (s != DLADM_STATUS_OK) {
6365 				warn_dlerr(status, "cannot set link property "
6366 				    "'%s' on '%s'", aip->ai_name, argv[optind]);
6367 				free(propvals);
6368 				break;
6369 			}
6370 
6371 			ptr = errmsg;
6372 			lim = ptr + DLADM_STRSIZE;
6373 			*ptr = '\0';
6374 			for (j = 0; j < valcnt; j++) {
6375 				ptr += snprintf(ptr, lim - ptr, "%s,",
6376 				    propvals[j]);
6377 				if (ptr >= lim)
6378 					break;
6379 			}
6380 			if (ptr > errmsg) {
6381 				*(ptr - 1) = '\0';
6382 				warn("link property '%s' must be one of: %s",
6383 				    aip->ai_name, errmsg);
6384 			} else
6385 				warn("invalid link property '%s'", *val);
6386 			free(propvals);
6387 			break;
6388 		}
6389 		default:
6390 			if (reset) {
6391 				warn_dlerr(status, "cannot reset link property "
6392 				    "'%s' on '%s'", aip->ai_name, argv[optind]);
6393 			} else {
6394 				warn_dlerr(status, "cannot set link property "
6395 				    "'%s' on '%s'", aip->ai_name, argv[optind]);
6396 			}
6397 			break;
6398 		}
6399 	}
6400 done:
6401 	dladm_free_props(proplist);
6402 	if (status != DLADM_STATUS_OK) {
6403 		dladm_close(handle);
6404 		exit(1);
6405 	}
6406 }
6407 
6408 static void
6409 do_set_linkprop(int argc, char **argv, const char *use)
6410 {
6411 	set_linkprop(argc, argv, B_FALSE, use);
6412 }
6413 
6414 static void
6415 do_reset_linkprop(int argc, char **argv, const char *use)
6416 {
6417 	set_linkprop(argc, argv, B_TRUE, use);
6418 }
6419 
6420 static int
6421 convert_secobj(char *buf, uint_t len, uint8_t *obj_val, uint_t *obj_lenp,
6422     dladm_secobj_class_t class)
6423 {
6424 	int error = 0;
6425 
6426 	if (class == DLADM_SECOBJ_CLASS_WPA) {
6427 		if (len < 8 || len > 63)
6428 			return (EINVAL);
6429 		(void) memcpy(obj_val, buf, len);
6430 		*obj_lenp = len;
6431 		return (error);
6432 	}
6433 
6434 	if (class == DLADM_SECOBJ_CLASS_WEP) {
6435 		switch (len) {
6436 		case 5:			/* ASCII key sizes */
6437 		case 13:
6438 			(void) memcpy(obj_val, buf, len);
6439 			*obj_lenp = len;
6440 			break;
6441 		case 10:		/* Hex key sizes, not preceded by 0x */
6442 		case 26:
6443 			error = hexascii_to_octet(buf, len, obj_val, obj_lenp);
6444 			break;
6445 		case 12:		/* Hex key sizes, preceded by 0x */
6446 		case 28:
6447 			if (strncmp(buf, "0x", 2) != 0)
6448 				return (EINVAL);
6449 			error = hexascii_to_octet(buf + 2, len - 2,
6450 			    obj_val, obj_lenp);
6451 			break;
6452 		default:
6453 			return (EINVAL);
6454 		}
6455 		return (error);
6456 	}
6457 
6458 	return (ENOENT);
6459 }
6460 
6461 static void
6462 defersig(int sig)
6463 {
6464 	signalled = sig;
6465 }
6466 
6467 static int
6468 get_secobj_from_tty(uint_t try, const char *objname, char *buf)
6469 {
6470 	uint_t		len = 0;
6471 	int		c;
6472 	struct termios	stored, current;
6473 	void		(*sigfunc)(int);
6474 
6475 	/*
6476 	 * Turn off echo -- but before we do so, defer SIGINT handling
6477 	 * so that a ^C doesn't leave the terminal corrupted.
6478 	 */
6479 	sigfunc = signal(SIGINT, defersig);
6480 	(void) fflush(stdin);
6481 	(void) tcgetattr(0, &stored);
6482 	current = stored;
6483 	current.c_lflag &= ~(ICANON|ECHO);
6484 	current.c_cc[VTIME] = 0;
6485 	current.c_cc[VMIN] = 1;
6486 	(void) tcsetattr(0, TCSANOW, &current);
6487 again:
6488 	if (try == 1)
6489 		(void) printf(gettext("provide value for '%s': "), objname);
6490 	else
6491 		(void) printf(gettext("confirm value for '%s': "), objname);
6492 
6493 	(void) fflush(stdout);
6494 	while (signalled == 0) {
6495 		c = getchar();
6496 		if (c == '\n' || c == '\r') {
6497 			if (len != 0)
6498 				break;
6499 			(void) putchar('\n');
6500 			goto again;
6501 		}
6502 
6503 		buf[len++] = c;
6504 		if (len >= DLADM_SECOBJ_VAL_MAX - 1)
6505 			break;
6506 		(void) putchar('*');
6507 	}
6508 
6509 	(void) putchar('\n');
6510 	(void) fflush(stdin);
6511 
6512 	/*
6513 	 * Restore terminal setting and handle deferred signals.
6514 	 */
6515 	(void) tcsetattr(0, TCSANOW, &stored);
6516 
6517 	(void) signal(SIGINT, sigfunc);
6518 	if (signalled != 0)
6519 		(void) kill(getpid(), signalled);
6520 
6521 	return (len);
6522 }
6523 
6524 static int
6525 get_secobj_val(char *obj_name, uint8_t *obj_val, uint_t *obj_lenp,
6526     dladm_secobj_class_t class, FILE *filep)
6527 {
6528 	int		rval;
6529 	uint_t		len, len2;
6530 	char		buf[DLADM_SECOBJ_VAL_MAX], buf2[DLADM_SECOBJ_VAL_MAX];
6531 
6532 	if (filep == NULL) {
6533 		len = get_secobj_from_tty(1, obj_name, buf);
6534 		rval = convert_secobj(buf, len, obj_val, obj_lenp, class);
6535 		if (rval == 0) {
6536 			len2 = get_secobj_from_tty(2, obj_name, buf2);
6537 			if (len != len2 || memcmp(buf, buf2, len) != 0)
6538 				rval = ENOTSUP;
6539 		}
6540 		return (rval);
6541 	} else {
6542 		for (;;) {
6543 			if (fgets(buf, sizeof (buf), filep) == NULL)
6544 				break;
6545 			if (isspace(buf[0]))
6546 				continue;
6547 
6548 			len = strlen(buf);
6549 			if (buf[len - 1] == '\n') {
6550 				buf[len - 1] = '\0';
6551 				len--;
6552 			}
6553 			break;
6554 		}
6555 		(void) fclose(filep);
6556 	}
6557 	return (convert_secobj(buf, len, obj_val, obj_lenp, class));
6558 }
6559 
6560 static boolean_t
6561 check_auth(const char *auth)
6562 {
6563 	struct passwd	*pw;
6564 
6565 	if ((pw = getpwuid(getuid())) == NULL)
6566 		return (B_FALSE);
6567 
6568 	return (chkauthattr(auth, pw->pw_name) != 0);
6569 }
6570 
6571 static void
6572 audit_secobj(char *auth, char *class, char *obj,
6573     boolean_t success, boolean_t create)
6574 {
6575 	adt_session_data_t	*ah;
6576 	adt_event_data_t	*event;
6577 	au_event_t		flag;
6578 	char			*errstr;
6579 
6580 	if (create) {
6581 		flag = ADT_dladm_create_secobj;
6582 		errstr = "ADT_dladm_create_secobj";
6583 	} else {
6584 		flag = ADT_dladm_delete_secobj;
6585 		errstr = "ADT_dladm_delete_secobj";
6586 	}
6587 
6588 	if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0)
6589 		die("adt_start_session: %s", strerror(errno));
6590 
6591 	if ((event = adt_alloc_event(ah, flag)) == NULL)
6592 		die("adt_alloc_event (%s): %s", errstr, strerror(errno));
6593 
6594 	/* fill in audit info */
6595 	if (create) {
6596 		event->adt_dladm_create_secobj.auth_used = auth;
6597 		event->adt_dladm_create_secobj.obj_class = class;
6598 		event->adt_dladm_create_secobj.obj_name = obj;
6599 	} else {
6600 		event->adt_dladm_delete_secobj.auth_used = auth;
6601 		event->adt_dladm_delete_secobj.obj_class = class;
6602 		event->adt_dladm_delete_secobj.obj_name = obj;
6603 	}
6604 
6605 	if (success) {
6606 		if (adt_put_event(event, ADT_SUCCESS, ADT_SUCCESS) != 0) {
6607 			die("adt_put_event (%s, success): %s", errstr,
6608 			    strerror(errno));
6609 		}
6610 	} else {
6611 		if (adt_put_event(event, ADT_FAILURE,
6612 		    ADT_FAIL_VALUE_AUTH) != 0) {
6613 			die("adt_put_event: (%s, failure): %s", errstr,
6614 			    strerror(errno));
6615 		}
6616 	}
6617 
6618 	adt_free_event(event);
6619 	(void) adt_end_session(ah);
6620 }
6621 
6622 #define	MAX_SECOBJS		32
6623 #define	MAX_SECOBJ_NAMELEN	32
6624 static void
6625 do_create_secobj(int argc, char **argv, const char *use)
6626 {
6627 	int			option, rval;
6628 	FILE			*filep = NULL;
6629 	char			*obj_name = NULL;
6630 	char			*class_name = NULL;
6631 	uint8_t			obj_val[DLADM_SECOBJ_VAL_MAX];
6632 	uint_t			obj_len;
6633 	boolean_t		success, temp = B_FALSE;
6634 	dladm_status_t		status;
6635 	dladm_secobj_class_t	class = -1;
6636 	uid_t			euid;
6637 
6638 	opterr = 0;
6639 	(void) memset(obj_val, 0, DLADM_SECOBJ_VAL_MAX);
6640 	while ((option = getopt_long(argc, argv, ":f:c:R:t",
6641 	    wifi_longopts, NULL)) != -1) {
6642 		switch (option) {
6643 		case 'f':
6644 			euid = geteuid();
6645 			(void) seteuid(getuid());
6646 			filep = fopen(optarg, "r");
6647 			if (filep == NULL) {
6648 				die("cannot open %s: %s", optarg,
6649 				    strerror(errno));
6650 			}
6651 			(void) seteuid(euid);
6652 			break;
6653 		case 'c':
6654 			class_name = optarg;
6655 			status = dladm_str2secobjclass(optarg, &class);
6656 			if (status != DLADM_STATUS_OK) {
6657 				die("invalid secure object class '%s', "
6658 				    "valid values are: wep, wpa", optarg);
6659 			}
6660 			break;
6661 		case 't':
6662 			temp = B_TRUE;
6663 			break;
6664 		case 'R':
6665 			status = dladm_set_rootdir(optarg);
6666 			if (status != DLADM_STATUS_OK) {
6667 				die_dlerr(status, "invalid directory "
6668 				    "specified");
6669 			}
6670 			break;
6671 		default:
6672 			die_opterr(optopt, option, use);
6673 			break;
6674 		}
6675 	}
6676 
6677 	if (optind == (argc - 1))
6678 		obj_name = argv[optind];
6679 	else if (optind != argc)
6680 		usage();
6681 
6682 	if (class == -1)
6683 		die("secure object class required");
6684 
6685 	if (obj_name == NULL)
6686 		die("secure object name required");
6687 
6688 	if (!dladm_valid_secobj_name(obj_name))
6689 		die("invalid secure object name '%s'", obj_name);
6690 
6691 	success = check_auth(LINK_SEC_AUTH);
6692 	audit_secobj(LINK_SEC_AUTH, class_name, obj_name, success, B_TRUE);
6693 	if (!success)
6694 		die("authorization '%s' is required", LINK_SEC_AUTH);
6695 
6696 	rval = get_secobj_val(obj_name, obj_val, &obj_len, class, filep);
6697 	if (rval != 0) {
6698 		switch (rval) {
6699 		case ENOENT:
6700 			die("invalid secure object class");
6701 			break;
6702 		case EINVAL:
6703 			die("invalid secure object value");
6704 			break;
6705 		case ENOTSUP:
6706 			die("verification failed");
6707 			break;
6708 		default:
6709 			die("invalid secure object: %s", strerror(rval));
6710 			break;
6711 		}
6712 	}
6713 
6714 	status = dladm_set_secobj(handle, obj_name, class, obj_val, obj_len,
6715 	    DLADM_OPT_CREATE | DLADM_OPT_ACTIVE);
6716 	if (status != DLADM_STATUS_OK) {
6717 		die_dlerr(status, "could not create secure object '%s'",
6718 		    obj_name);
6719 	}
6720 	if (temp)
6721 		return;
6722 
6723 	status = dladm_set_secobj(handle, obj_name, class, obj_val, obj_len,
6724 	    DLADM_OPT_PERSIST);
6725 	if (status != DLADM_STATUS_OK) {
6726 		warn_dlerr(status, "could not persistently create secure "
6727 		    "object '%s'", obj_name);
6728 	}
6729 }
6730 
6731 static void
6732 do_delete_secobj(int argc, char **argv, const char *use)
6733 {
6734 	int		i, option;
6735 	boolean_t	temp = B_FALSE;
6736 	boolean_t	success;
6737 	dladm_status_t	status, pstatus;
6738 	int		nfields = 1;
6739 	char		*field, *token, *lasts = NULL, c;
6740 
6741 	opterr = 0;
6742 	status = pstatus = DLADM_STATUS_OK;
6743 	while ((option = getopt_long(argc, argv, ":R:t",
6744 	    wifi_longopts, NULL)) != -1) {
6745 		switch (option) {
6746 		case 't':
6747 			temp = B_TRUE;
6748 			break;
6749 		case 'R':
6750 			status = dladm_set_rootdir(optarg);
6751 			if (status != DLADM_STATUS_OK) {
6752 				die_dlerr(status, "invalid directory "
6753 				    "specified");
6754 			}
6755 			break;
6756 		default:
6757 			die_opterr(optopt, option, use);
6758 			break;
6759 		}
6760 	}
6761 
6762 	if (optind == (argc - 1)) {
6763 		token = argv[optind];
6764 		if (token == NULL)
6765 			die("secure object name required");
6766 		while ((c = *token++) != NULL) {
6767 			if (c == ',')
6768 				nfields++;
6769 		}
6770 		token = strdup(argv[optind]);
6771 		if (token == NULL)
6772 			die("no memory");
6773 	} else if (optind != argc)
6774 		usage();
6775 
6776 	success = check_auth(LINK_SEC_AUTH);
6777 	audit_secobj(LINK_SEC_AUTH, "unknown", argv[optind], success, B_FALSE);
6778 	if (!success)
6779 		die("authorization '%s' is required", LINK_SEC_AUTH);
6780 
6781 	for (i = 0; i < nfields; i++) {
6782 
6783 		field = strtok_r(token, ",", &lasts);
6784 		token = NULL;
6785 		status = dladm_unset_secobj(handle, field, DLADM_OPT_ACTIVE);
6786 		if (!temp) {
6787 			pstatus = dladm_unset_secobj(handle, field,
6788 			    DLADM_OPT_PERSIST);
6789 		} else {
6790 			pstatus = DLADM_STATUS_OK;
6791 		}
6792 
6793 		if (status != DLADM_STATUS_OK) {
6794 			warn_dlerr(status, "could not delete secure object "
6795 			    "'%s'", field);
6796 		}
6797 		if (pstatus != DLADM_STATUS_OK) {
6798 			warn_dlerr(pstatus, "could not persistently delete "
6799 			    "secure object '%s'", field);
6800 		}
6801 	}
6802 	free(token);
6803 
6804 	if (status != DLADM_STATUS_OK || pstatus != DLADM_STATUS_OK) {
6805 		dladm_close(handle);
6806 		exit(1);
6807 	}
6808 }
6809 
6810 typedef struct show_secobj_state {
6811 	boolean_t	ss_persist;
6812 	boolean_t	ss_parsable;
6813 	boolean_t	ss_header;
6814 	ofmt_handle_t	ss_ofmt;
6815 } show_secobj_state_t;
6816 
6817 
6818 static boolean_t
6819 show_secobj(dladm_handle_t dh, void *arg, const char *obj_name)
6820 {
6821 	uint_t			obj_len = DLADM_SECOBJ_VAL_MAX;
6822 	uint8_t			obj_val[DLADM_SECOBJ_VAL_MAX];
6823 	char			buf[DLADM_STRSIZE];
6824 	uint_t			flags = 0;
6825 	dladm_secobj_class_t	class;
6826 	show_secobj_state_t	*statep = arg;
6827 	dladm_status_t		status;
6828 	secobj_fields_buf_t	sbuf;
6829 
6830 	bzero(&sbuf, sizeof (secobj_fields_buf_t));
6831 	if (statep->ss_persist)
6832 		flags |= DLADM_OPT_PERSIST;
6833 
6834 	status = dladm_get_secobj(dh, obj_name, &class, obj_val, &obj_len,
6835 	    flags);
6836 	if (status != DLADM_STATUS_OK)
6837 		die_dlerr(status, "cannot get secure object '%s'", obj_name);
6838 
6839 	(void) snprintf(sbuf.ss_obj_name, sizeof (sbuf.ss_obj_name),
6840 	    obj_name);
6841 	(void) dladm_secobjclass2str(class, buf);
6842 	(void) snprintf(sbuf.ss_class, sizeof (sbuf.ss_class), "%s", buf);
6843 	if (getuid() == 0) {
6844 		char	val[DLADM_SECOBJ_VAL_MAX * 2];
6845 		uint_t	len = sizeof (val);
6846 
6847 		if (octet_to_hexascii(obj_val, obj_len, val, &len) == 0)
6848 			(void) snprintf(sbuf.ss_val,
6849 			    sizeof (sbuf.ss_val), "%s", val);
6850 	}
6851 	ofmt_print(statep->ss_ofmt, &sbuf);
6852 	return (B_TRUE);
6853 }
6854 
6855 static void
6856 do_show_secobj(int argc, char **argv, const char *use)
6857 {
6858 	int			option;
6859 	show_secobj_state_t	state;
6860 	dladm_status_t		status;
6861 	boolean_t		o_arg = B_FALSE;
6862 	uint_t			i;
6863 	uint_t			flags;
6864 	char			*fields_str = NULL;
6865 	char			*def_fields = "object,class";
6866 	char			*all_fields = "object,class,value";
6867 	char			*field, *token, *lasts = NULL, c;
6868 	ofmt_handle_t		ofmt;
6869 	ofmt_status_t		oferr;
6870 	uint_t			ofmtflags = 0;
6871 
6872 	opterr = 0;
6873 	bzero(&state, sizeof (state));
6874 	state.ss_parsable = B_FALSE;
6875 	fields_str = def_fields;
6876 	state.ss_persist = B_FALSE;
6877 	state.ss_parsable = B_FALSE;
6878 	state.ss_header = B_TRUE;
6879 	while ((option = getopt_long(argc, argv, ":pPo:",
6880 	    wifi_longopts, NULL)) != -1) {
6881 		switch (option) {
6882 		case 'p':
6883 			state.ss_parsable = B_TRUE;
6884 			break;
6885 		case 'P':
6886 			state.ss_persist = B_TRUE;
6887 			break;
6888 		case 'o':
6889 			o_arg = B_TRUE;
6890 			if (strcasecmp(optarg, "all") == 0)
6891 				fields_str = all_fields;
6892 			else
6893 				fields_str = optarg;
6894 			break;
6895 		default:
6896 			die_opterr(optopt, option, use);
6897 			break;
6898 		}
6899 	}
6900 
6901 	if (state.ss_parsable && !o_arg)
6902 		die("option -c requires -o");
6903 
6904 	if (state.ss_parsable && fields_str == all_fields)
6905 		die("\"-o all\" is invalid with -p");
6906 
6907 	if (state.ss_parsable)
6908 		ofmtflags |= OFMT_PARSABLE;
6909 	oferr = ofmt_open(fields_str, secobj_fields, ofmtflags, 0, &ofmt);
6910 	dladm_ofmt_check(oferr, state.ss_parsable, ofmt);
6911 	state.ss_ofmt = ofmt;
6912 
6913 	flags = state.ss_persist ? DLADM_OPT_PERSIST : 0;
6914 
6915 	if (optind == (argc - 1)) {
6916 		uint_t obj_fields = 1;
6917 
6918 		token = argv[optind];
6919 		if (token == NULL)
6920 			die("secure object name required");
6921 		while ((c = *token++) != NULL) {
6922 			if (c == ',')
6923 				obj_fields++;
6924 		}
6925 		token = strdup(argv[optind]);
6926 		if (token == NULL)
6927 			die("no memory");
6928 		for (i = 0; i < obj_fields; i++) {
6929 			field = strtok_r(token, ",", &lasts);
6930 			token = NULL;
6931 			if (!show_secobj(handle, &state, field))
6932 				break;
6933 		}
6934 		free(token);
6935 		ofmt_close(ofmt);
6936 		return;
6937 	} else if (optind != argc)
6938 		usage();
6939 
6940 	status = dladm_walk_secobj(handle, &state, show_secobj, flags);
6941 
6942 	if (status != DLADM_STATUS_OK)
6943 		die_dlerr(status, "show-secobj");
6944 	ofmt_close(ofmt);
6945 }
6946 
6947 /*ARGSUSED*/
6948 static int
6949 i_dladm_init_linkprop(dladm_handle_t dh, datalink_id_t linkid, void *arg)
6950 {
6951 	(void) dladm_init_linkprop(dh, linkid, B_TRUE);
6952 	return (DLADM_WALK_CONTINUE);
6953 }
6954 
6955 /*ARGSUSED*/
6956 void
6957 do_init_linkprop(int argc, char **argv, const char *use)
6958 {
6959 	int			option;
6960 	dladm_status_t		status;
6961 	datalink_id_t		linkid = DATALINK_ALL_LINKID;
6962 	datalink_media_t	media = DATALINK_ANY_MEDIATYPE;
6963 	uint_t			any_media = B_TRUE;
6964 
6965 	opterr = 0;
6966 	while ((option = getopt(argc, argv, ":w")) != -1) {
6967 		switch (option) {
6968 		case 'w':
6969 			media = DL_WIFI;
6970 			any_media = B_FALSE;
6971 			break;
6972 		default:
6973 			/*
6974 			 * Because init-linkprop is not a public command,
6975 			 * print the usage instead.
6976 			 */
6977 			usage();
6978 			break;
6979 		}
6980 	}
6981 
6982 	if (optind == (argc - 1)) {
6983 		if ((status = dladm_name2info(handle, argv[optind], &linkid,
6984 		    NULL, NULL, NULL)) != DLADM_STATUS_OK)
6985 			die_dlerr(status, "link %s is not valid", argv[optind]);
6986 	} else if (optind != argc) {
6987 		usage();
6988 	}
6989 
6990 	if (linkid == DATALINK_ALL_LINKID) {
6991 		/*
6992 		 * linkprops of links of other classes have been initialized as
6993 		 * part of the dladm up-xxx operation.
6994 		 */
6995 		(void) dladm_walk_datalink_id(i_dladm_init_linkprop, handle,
6996 		    NULL, DATALINK_CLASS_PHYS, media, DLADM_OPT_PERSIST);
6997 	} else {
6998 		(void) dladm_init_linkprop(handle, linkid, any_media);
6999 	}
7000 }
7001 
7002 static void
7003 do_show_ether(int argc, char **argv, const char *use)
7004 {
7005 	int 			option;
7006 	datalink_id_t		linkid;
7007 	print_ether_state_t 	state;
7008 	char			*fields_str = NULL;
7009 	ofmt_handle_t		ofmt;
7010 	ofmt_status_t		oferr;
7011 	uint_t			ofmtflags = 0;
7012 
7013 	bzero(&state, sizeof (state));
7014 	state.es_link = NULL;
7015 	state.es_parsable = B_FALSE;
7016 
7017 	while ((option = getopt_long(argc, argv, "o:px",
7018 	    showeth_lopts, NULL)) != -1) {
7019 		switch (option) {
7020 			case 'x':
7021 				state.es_extended = B_TRUE;
7022 				break;
7023 			case 'p':
7024 				state.es_parsable = B_TRUE;
7025 				break;
7026 			case 'o':
7027 				fields_str = optarg;
7028 				break;
7029 			default:
7030 				die_opterr(optopt, option, use);
7031 				break;
7032 		}
7033 	}
7034 
7035 	if (optind == (argc - 1))
7036 		state.es_link = argv[optind];
7037 
7038 	if (state.es_parsable)
7039 		ofmtflags |= OFMT_PARSABLE;
7040 	oferr = ofmt_open(fields_str, ether_fields, ofmtflags,
7041 	    DLADM_DEFAULT_COL, &ofmt);
7042 	dladm_ofmt_check(oferr, state.es_parsable, ofmt);
7043 	state.es_ofmt = ofmt;
7044 
7045 	if (state.es_link == NULL) {
7046 		(void) dladm_walk_datalink_id(show_etherprop, handle, &state,
7047 		    DATALINK_CLASS_PHYS, DL_ETHER, DLADM_OPT_ACTIVE);
7048 	} else {
7049 		if (!link_is_ether(state.es_link, &linkid))
7050 			die("invalid link specified");
7051 		(void) show_etherprop(handle, linkid, &state);
7052 	}
7053 	ofmt_close(ofmt);
7054 }
7055 
7056 static int
7057 show_etherprop(dladm_handle_t dh, datalink_id_t linkid, void *arg)
7058 {
7059 	print_ether_state_t	*statep = arg;
7060 	ether_fields_buf_t	ebuf;
7061 	dladm_ether_info_t	eattr;
7062 	dladm_status_t		status;
7063 
7064 	bzero(&ebuf, sizeof (ether_fields_buf_t));
7065 	if (dladm_datalink_id2info(dh, linkid, NULL, NULL, NULL,
7066 	    ebuf.eth_link, sizeof (ebuf.eth_link)) != DLADM_STATUS_OK) {
7067 		return (DLADM_WALK_CONTINUE);
7068 	}
7069 
7070 	status = dladm_ether_info(dh, linkid, &eattr);
7071 	if (status != DLADM_STATUS_OK)
7072 		goto cleanup;
7073 
7074 	(void) strlcpy(ebuf.eth_ptype, "current", sizeof (ebuf.eth_ptype));
7075 
7076 	(void) dladm_ether_autoneg2str(ebuf.eth_autoneg,
7077 	    sizeof (ebuf.eth_autoneg), &eattr, CURRENT);
7078 	(void) dladm_ether_pause2str(ebuf.eth_pause,
7079 	    sizeof (ebuf.eth_pause), &eattr, CURRENT);
7080 	(void) dladm_ether_spdx2str(ebuf.eth_spdx,
7081 	    sizeof (ebuf.eth_spdx), &eattr, CURRENT);
7082 	(void) strlcpy(ebuf.eth_state,
7083 	    dladm_linkstate2str(eattr.lei_state, ebuf.eth_state),
7084 	    sizeof (ebuf.eth_state));
7085 	(void) strlcpy(ebuf.eth_rem_fault,
7086 	    (eattr.lei_attr[CURRENT].le_fault ? "fault" : "none"),
7087 	    sizeof (ebuf.eth_rem_fault));
7088 
7089 	ofmt_print(statep->es_ofmt, &ebuf);
7090 
7091 	if (statep->es_extended)
7092 		show_ether_xprop(arg, &eattr);
7093 
7094 cleanup:
7095 	dladm_ether_info_done(&eattr);
7096 	return (DLADM_WALK_CONTINUE);
7097 }
7098 
7099 /* ARGSUSED */
7100 static void
7101 do_init_secobj(int argc, char **argv, const char *use)
7102 {
7103 	dladm_status_t	status;
7104 
7105 	status = dladm_init_secobj(handle);
7106 	if (status != DLADM_STATUS_OK)
7107 		die_dlerr(status, "secure object initialization failed");
7108 }
7109 
7110 enum bridge_func {
7111 	brCreate, brAdd, brModify
7112 };
7113 
7114 static void
7115 create_modify_add_bridge(int argc, char **argv, const char *use,
7116     enum bridge_func func)
7117 {
7118 	int			option;
7119 	uint_t			n, i, nlink;
7120 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
7121 	char			*altroot = NULL;
7122 	char			*links[MAXPORT];
7123 	datalink_id_t		linkids[MAXPORT];
7124 	dladm_status_t		status;
7125 	const char		*bridge;
7126 	UID_STP_CFG_T		cfg, cfg_old;
7127 	dladm_bridge_prot_t	brprot = DLADM_BRIDGE_PROT_UNKNOWN;
7128 	dladm_bridge_prot_t	brprot_old;
7129 
7130 	/* Set up the default configuration values */
7131 	cfg.field_mask = 0;
7132 	cfg.bridge_priority = DEF_BR_PRIO;
7133 	cfg.max_age = DEF_BR_MAXAGE;
7134 	cfg.hello_time = DEF_BR_HELLOT;
7135 	cfg.forward_delay = DEF_BR_FWDELAY;
7136 	cfg.force_version = DEF_FORCE_VERS;
7137 
7138 	nlink = opterr = 0;
7139 	while ((option = getopt_long(argc, argv, ":P:R:d:f:h:l:m:p:",
7140 	    bridge_lopts, NULL)) != -1) {
7141 		switch (option) {
7142 		case 'P':
7143 			if (func == brAdd)
7144 				die_opterr(optopt, option, use);
7145 			status = dladm_bridge_str2prot(optarg, &brprot);
7146 			if (status != DLADM_STATUS_OK)
7147 				die_dlerr(status, "protection %s", optarg);
7148 			break;
7149 		case 'R':
7150 			altroot = optarg;
7151 			break;
7152 		case 'd':
7153 			if (func == brAdd)
7154 				die_opterr(optopt, option, use);
7155 			if (cfg.field_mask & BR_CFG_DELAY)
7156 				die("forwarding delay set more than once");
7157 			if (!str2int(optarg, &cfg.forward_delay) ||
7158 			    cfg.forward_delay < MIN_BR_FWDELAY ||
7159 			    cfg.forward_delay > MAX_BR_FWDELAY)
7160 				die("incorrect forwarding delay");
7161 			cfg.field_mask |= BR_CFG_DELAY;
7162 			break;
7163 		case 'f':
7164 			if (func == brAdd)
7165 				die_opterr(optopt, option, use);
7166 			if (cfg.field_mask & BR_CFG_FORCE_VER)
7167 				die("force protocol set more than once");
7168 			if (!str2int(optarg, &cfg.force_version) ||
7169 			    cfg.force_version < 0)
7170 				die("incorrect force protocol");
7171 			cfg.field_mask |= BR_CFG_FORCE_VER;
7172 			break;
7173 		case 'h':
7174 			if (func == brAdd)
7175 				die_opterr(optopt, option, use);
7176 			if (cfg.field_mask & BR_CFG_HELLO)
7177 				die("hello time set more than once");
7178 			if (!str2int(optarg, &cfg.hello_time) ||
7179 			    cfg.hello_time < MIN_BR_HELLOT ||
7180 			    cfg.hello_time > MAX_BR_HELLOT)
7181 				die("incorrect hello time");
7182 			cfg.field_mask |= BR_CFG_HELLO;
7183 			break;
7184 		case 'l':
7185 			if (func == brModify)
7186 				die_opterr(optopt, option, use);
7187 			if (nlink >= MAXPORT)
7188 				die("too many links specified");
7189 			links[nlink++] = optarg;
7190 			break;
7191 		case 'm':
7192 			if (func == brAdd)
7193 				die_opterr(optopt, option, use);
7194 			if (cfg.field_mask & BR_CFG_AGE)
7195 				die("max age set more than once");
7196 			if (!str2int(optarg, &cfg.max_age) ||
7197 			    cfg.max_age < MIN_BR_MAXAGE ||
7198 			    cfg.max_age > MAX_BR_MAXAGE)
7199 				die("incorrect max age");
7200 			cfg.field_mask |= BR_CFG_AGE;
7201 			break;
7202 		case 'p':
7203 			if (func == brAdd)
7204 				die_opterr(optopt, option, use);
7205 			if (cfg.field_mask & BR_CFG_PRIO)
7206 				die("priority set more than once");
7207 			if (!str2int(optarg, &cfg.bridge_priority) ||
7208 			    cfg.bridge_priority < MIN_BR_PRIO ||
7209 			    cfg.bridge_priority > MAX_BR_PRIO)
7210 				die("incorrect priority");
7211 			cfg.bridge_priority &= 0xF000;
7212 			cfg.field_mask |= BR_CFG_PRIO;
7213 			break;
7214 		default:
7215 			die_opterr(optopt, option, use);
7216 			break;
7217 		}
7218 	}
7219 
7220 	/* get the bridge name (required last argument) */
7221 	if (optind != (argc-1))
7222 		usage();
7223 
7224 	bridge = argv[optind];
7225 	if (!dladm_valid_bridgename(bridge))
7226 		die("invalid bridge name '%s'", bridge);
7227 
7228 	/*
7229 	 * Get the current properties, if any, and merge in with changes.  This
7230 	 * is necessary (even with the field_mask feature) so that the
7231 	 * value-checking macros will produce the right results with proposed
7232 	 * changes to existing configuration.  We only need it for those
7233 	 * parameters, though.
7234 	 */
7235 	(void) dladm_bridge_get_properties(bridge, &cfg_old, &brprot_old);
7236 	if (brprot == DLADM_BRIDGE_PROT_UNKNOWN)
7237 		brprot = brprot_old;
7238 	if (!(cfg.field_mask & BR_CFG_AGE))
7239 		cfg.max_age = cfg_old.max_age;
7240 	if (!(cfg.field_mask & BR_CFG_HELLO))
7241 		cfg.hello_time = cfg_old.hello_time;
7242 	if (!(cfg.field_mask & BR_CFG_DELAY))
7243 		cfg.forward_delay = cfg_old.forward_delay;
7244 
7245 	if (!CHECK_BRIDGE_CONFIG(cfg)) {
7246 		warn("illegal forward delay / max age / hello time "
7247 		    "combination");
7248 		if (NO_MAXAGE(cfg)) {
7249 			die("no max age possible: need forward delay >= %d or "
7250 			    "hello time <= %d", MIN_FWDELAY_NOM(cfg),
7251 			    MAX_HELLOTIME_NOM(cfg));
7252 		} else if (SMALL_MAXAGE(cfg)) {
7253 			if (CAPPED_MAXAGE(cfg))
7254 				die("max age too small: need age >= %d and "
7255 				    "<= %d or hello time <= %d",
7256 				    MIN_MAXAGE(cfg), MAX_MAXAGE(cfg),
7257 				    MAX_HELLOTIME(cfg));
7258 			else
7259 				die("max age too small: need age >= %d or "
7260 				    "hello time <= %d",
7261 				    MIN_MAXAGE(cfg), MAX_HELLOTIME(cfg));
7262 		} else if (FLOORED_MAXAGE(cfg)) {
7263 			die("max age too large: need age >= %d and <= %d or "
7264 			    "forward delay >= %d",
7265 			    MIN_MAXAGE(cfg), MAX_MAXAGE(cfg),
7266 			    MIN_FWDELAY(cfg));
7267 		} else {
7268 			die("max age too large: need age <= %d or forward "
7269 			    "delay >= %d",
7270 			    MAX_MAXAGE(cfg), MIN_FWDELAY(cfg));
7271 		}
7272 	}
7273 
7274 	if (altroot != NULL)
7275 		altroot_cmd(altroot, argc, argv);
7276 
7277 	for (n = 0; n < nlink; n++) {
7278 		datalink_class_t class;
7279 		uint32_t media;
7280 		char pointless[DLADM_STRSIZE];
7281 
7282 		if (dladm_name2info(handle, links[n], &linkids[n], NULL, &class,
7283 		    &media) != DLADM_STATUS_OK)
7284 			die("invalid link name '%s'", links[n]);
7285 		if (class & ~(DATALINK_CLASS_PHYS | DATALINK_CLASS_AGGR |
7286 		    DATALINK_CLASS_ETHERSTUB | DATALINK_CLASS_SIMNET))
7287 			die("%s %s cannot be bridged",
7288 			    dladm_class2str(class, pointless), links[n]);
7289 		if (media != DL_ETHER && media != DL_100VG &&
7290 		    media != DL_ETH_CSMA && media != DL_100BT)
7291 			die("%s interface %s cannot be bridged",
7292 			    dladm_media2str(media, pointless), links[n]);
7293 	}
7294 
7295 	if (func == brCreate)
7296 		flags |= DLADM_OPT_CREATE;
7297 
7298 	if (func != brAdd) {
7299 		status = dladm_bridge_configure(handle, bridge, &cfg, brprot,
7300 		    flags);
7301 		if (status != DLADM_STATUS_OK)
7302 			die_dlerr(status, "create operation failed");
7303 	}
7304 
7305 	status = DLADM_STATUS_OK;
7306 	for (n = 0; n < nlink; n++) {
7307 		status = dladm_bridge_setlink(handle, linkids[n], bridge);
7308 		if (status != DLADM_STATUS_OK)
7309 			break;
7310 	}
7311 
7312 	if (n >= nlink) {
7313 		/*
7314 		 * We were successful.  If we're creating a new bridge, then
7315 		 * there's just one more step: enabling.  If we're modifying or
7316 		 * just adding links, then we're done.
7317 		 */
7318 		if (func != brCreate ||
7319 		    (status = dladm_bridge_enable(bridge)) == DLADM_STATUS_OK)
7320 			return;
7321 	}
7322 
7323 	/* clean up the partial configuration */
7324 	for (i = 0; i < n; i++)
7325 		(void) dladm_bridge_setlink(handle, linkids[i], "");
7326 
7327 	/* if failure for brCreate, then delete the bridge */
7328 	if (func == brCreate)
7329 		(void) dladm_bridge_delete(handle, bridge, flags);
7330 
7331 	if (n < nlink)
7332 		die_dlerr(status, "unable to add link %s to bridge %s",
7333 		    links[n], bridge);
7334 	else
7335 		die_dlerr(status, "unable to enable bridge %s", bridge);
7336 }
7337 
7338 static void
7339 do_create_bridge(int argc, char **argv, const char *use)
7340 {
7341 	create_modify_add_bridge(argc, argv, use, brCreate);
7342 }
7343 
7344 static void
7345 do_modify_bridge(int argc, char **argv, const char *use)
7346 {
7347 	create_modify_add_bridge(argc, argv, use, brModify);
7348 }
7349 
7350 static void
7351 do_add_bridge(int argc, char **argv, const char *use)
7352 {
7353 	create_modify_add_bridge(argc, argv, use, brAdd);
7354 }
7355 
7356 static void
7357 do_delete_bridge(int argc, char **argv, const char *use)
7358 {
7359 	char			option;
7360 	char			*altroot = NULL;
7361 	uint32_t		flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
7362 	dladm_status_t		status;
7363 
7364 	opterr = 0;
7365 	while ((option = getopt_long(argc, argv, ":R:", bridge_lopts, NULL)) !=
7366 	    -1) {
7367 		switch (option) {
7368 		case 'R':
7369 			altroot = optarg;
7370 			break;
7371 		default:
7372 			die_opterr(optopt, option, use);
7373 			break;
7374 		}
7375 	}
7376 
7377 	/* get the bridge name (required last argument) */
7378 	if (optind != (argc-1))
7379 		usage();
7380 
7381 	if (altroot != NULL)
7382 		altroot_cmd(altroot, argc, argv);
7383 
7384 	status = dladm_bridge_delete(handle, argv[optind], flags);
7385 	if (status != DLADM_STATUS_OK)
7386 		die_dlerr(status, "delete operation failed");
7387 }
7388 
7389 static void
7390 do_remove_bridge(int argc, char **argv, const char *use)
7391 {
7392 	char		option;
7393 	uint_t		n, nlink;
7394 	char		*links[MAXPORT];
7395 	datalink_id_t	linkids[MAXPORT];
7396 	char		*altroot = NULL;
7397 	dladm_status_t	status;
7398 	boolean_t	removed_one;
7399 
7400 	nlink = opterr = 0;
7401 	while ((option = getopt_long(argc, argv, ":R:l:", bridge_lopts,
7402 	    NULL)) != -1) {
7403 		switch (option) {
7404 		case 'R':
7405 			altroot = optarg;
7406 			break;
7407 		case 'l':
7408 			if (nlink >= MAXPORT)
7409 				die("too many links specified");
7410 			links[nlink++] = optarg;
7411 			break;
7412 		default:
7413 			die_opterr(optopt, option, use);
7414 			break;
7415 		}
7416 	}
7417 
7418 	if (nlink == 0)
7419 		usage();
7420 
7421 	/* get the bridge name (required last argument) */
7422 	if (optind != (argc-1))
7423 		usage();
7424 
7425 	if (altroot != NULL)
7426 		altroot_cmd(altroot, argc, argv);
7427 
7428 	for (n = 0; n < nlink; n++) {
7429 		char bridge[MAXLINKNAMELEN];
7430 
7431 		if (dladm_name2info(handle, links[n], &linkids[n], NULL, NULL,
7432 		    NULL) != DLADM_STATUS_OK)
7433 			die("invalid link name '%s'", links[n]);
7434 		status = dladm_bridge_getlink(handle, linkids[n], bridge,
7435 		    sizeof (bridge));
7436 		if (status != DLADM_STATUS_OK &&
7437 		    status != DLADM_STATUS_NOTFOUND) {
7438 			die_dlerr(status, "cannot get bridge status on %s",
7439 			    links[n]);
7440 		}
7441 		if (status == DLADM_STATUS_NOTFOUND ||
7442 		    strcmp(bridge, argv[optind]) != 0)
7443 			die("link %s is not on bridge %s", links[n],
7444 			    argv[optind]);
7445 	}
7446 
7447 	removed_one = B_FALSE;
7448 	for (n = 0; n < nlink; n++) {
7449 		status = dladm_bridge_setlink(handle, linkids[n], "");
7450 		if (status == DLADM_STATUS_OK) {
7451 			removed_one = B_TRUE;
7452 		} else {
7453 			warn_dlerr(status,
7454 			    "cannot remove link %s from bridge %s",
7455 			    links[n], argv[optind]);
7456 		}
7457 	}
7458 	if (!removed_one)
7459 		die("unable to remove any links from bridge %s", argv[optind]);
7460 }
7461 
7462 static void
7463 fmt_int(char *buf, size_t buflen, int value, int runvalue,
7464     boolean_t printstar)
7465 {
7466 	(void) snprintf(buf, buflen, "%d", value);
7467 	if (value != runvalue && printstar)
7468 		(void) strlcat(buf, "*", buflen);
7469 }
7470 
7471 static void
7472 fmt_bridge_id(char *buf, size_t buflen, UID_BRIDGE_ID_T *bid)
7473 {
7474 	(void) snprintf(buf, buflen, "%u/%x:%x:%x:%x:%x:%x", bid->prio,
7475 	    bid->addr[0], bid->addr[1], bid->addr[2], bid->addr[3],
7476 	    bid->addr[4], bid->addr[5]);
7477 }
7478 
7479 static dladm_status_t
7480 print_bridge(show_state_t *state, datalink_id_t linkid,
7481     bridge_fields_buf_t *bbuf)
7482 {
7483 	char			link[MAXLINKNAMELEN];
7484 	datalink_class_t	class;
7485 	uint32_t		flags;
7486 	dladm_status_t		status;
7487 	UID_STP_CFG_T		smfcfg, runcfg;
7488 	UID_STP_STATE_T		stpstate;
7489 	dladm_bridge_prot_t	smfprot, runprot;
7490 
7491 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, &class,
7492 	    NULL, link, sizeof (link))) != DLADM_STATUS_OK)
7493 		return (status);
7494 
7495 	if (!(state->ls_flags & flags))
7496 		return (DLADM_STATUS_NOTFOUND);
7497 
7498 	/* Convert observability node name back to bridge name */
7499 	if (!dladm_observe_to_bridge(link))
7500 		return (DLADM_STATUS_NOTFOUND);
7501 	(void) strlcpy(bbuf->bridge_name, link, sizeof (bbuf->bridge_name));
7502 
7503 	/*
7504 	 * If the running value differs from the one in SMF, and parsable
7505 	 * output is not requested, then we show the running value with an
7506 	 * asterisk.
7507 	 */
7508 	(void) dladm_bridge_get_properties(bbuf->bridge_name, &smfcfg,
7509 	    &smfprot);
7510 	(void) dladm_bridge_run_properties(bbuf->bridge_name, &runcfg,
7511 	    &runprot);
7512 	(void) snprintf(bbuf->bridge_protect, sizeof (bbuf->bridge_protect),
7513 	    "%s%s", state->ls_parsable || smfprot == runprot ? "" : "*",
7514 	    dladm_bridge_prot2str(runprot));
7515 	fmt_int(bbuf->bridge_priority, sizeof (bbuf->bridge_priority),
7516 	    smfcfg.bridge_priority, runcfg.bridge_priority,
7517 	    !state->ls_parsable && (runcfg.field_mask & BR_CFG_AGE));
7518 	fmt_int(bbuf->bridge_bmaxage, sizeof (bbuf->bridge_bmaxage),
7519 	    smfcfg.max_age, runcfg.max_age,
7520 	    !state->ls_parsable && (runcfg.field_mask & BR_CFG_AGE));
7521 	fmt_int(bbuf->bridge_bhellotime,
7522 	    sizeof (bbuf->bridge_bhellotime), smfcfg.hello_time,
7523 	    runcfg.hello_time,
7524 	    !state->ls_parsable && (runcfg.field_mask & BR_CFG_HELLO));
7525 	fmt_int(bbuf->bridge_bfwddelay, sizeof (bbuf->bridge_bfwddelay),
7526 	    smfcfg.forward_delay, runcfg.forward_delay,
7527 	    !state->ls_parsable && (runcfg.field_mask & BR_CFG_DELAY));
7528 	fmt_int(bbuf->bridge_forceproto, sizeof (bbuf->bridge_forceproto),
7529 	    smfcfg.force_version, runcfg.force_version,
7530 	    !state->ls_parsable && (runcfg.field_mask & BR_CFG_FORCE_VER));
7531 	fmt_int(bbuf->bridge_holdtime, sizeof (bbuf->bridge_holdtime),
7532 	    smfcfg.hold_time, runcfg.hold_time,
7533 	    !state->ls_parsable && (runcfg.field_mask & BR_CFG_HOLD_TIME));
7534 
7535 	if (dladm_bridge_state(bbuf->bridge_name, &stpstate) ==
7536 	    DLADM_STATUS_OK) {
7537 		fmt_bridge_id(bbuf->bridge_address,
7538 		    sizeof (bbuf->bridge_address), &stpstate.bridge_id);
7539 		(void) snprintf(bbuf->bridge_tctime,
7540 		    sizeof (bbuf->bridge_tctime), "%lu",
7541 		    stpstate.timeSince_Topo_Change);
7542 		(void) snprintf(bbuf->bridge_tccount,
7543 		    sizeof (bbuf->bridge_tccount), "%lu",
7544 		    stpstate.Topo_Change_Count);
7545 		(void) snprintf(bbuf->bridge_tchange,
7546 		    sizeof (bbuf->bridge_tchange), "%u", stpstate.Topo_Change);
7547 		fmt_bridge_id(bbuf->bridge_desroot,
7548 		    sizeof (bbuf->bridge_desroot), &stpstate.designated_root);
7549 		(void) snprintf(bbuf->bridge_rootcost,
7550 		    sizeof (bbuf->bridge_rootcost), "%lu",
7551 		    stpstate.root_path_cost);
7552 		(void) snprintf(bbuf->bridge_rootport,
7553 		    sizeof (bbuf->bridge_rootport), "%u", stpstate.root_port);
7554 		(void) snprintf(bbuf->bridge_maxage,
7555 		    sizeof (bbuf->bridge_maxage), "%d", stpstate.max_age);
7556 		(void) snprintf(bbuf->bridge_hellotime,
7557 		    sizeof (bbuf->bridge_hellotime), "%d", stpstate.hello_time);
7558 		(void) snprintf(bbuf->bridge_fwddelay,
7559 		    sizeof (bbuf->bridge_fwddelay), "%d",
7560 		    stpstate.forward_delay);
7561 	}
7562 	return (DLADM_STATUS_OK);
7563 }
7564 
7565 static dladm_status_t
7566 print_bridge_stats(show_state_t *state, datalink_id_t linkid,
7567     bridge_statfields_buf_t *bbuf)
7568 {
7569 	char			link[MAXLINKNAMELEN];
7570 	datalink_class_t	class;
7571 	uint32_t		flags;
7572 	dladm_status_t		status;
7573 	kstat_ctl_t		*kcp;
7574 	kstat_t			*ksp;
7575 	brsum_t			*brsum = (brsum_t *)&state->ls_prevstats;
7576 	brsum_t			newval;
7577 
7578 #ifndef lint
7579 	/* This is a compile-time assertion; optimizer normally fixes this */
7580 	extern void brsum_t_is_too_large(void);
7581 
7582 	if (sizeof (*brsum) > sizeof (state->ls_prevstats))
7583 		brsum_t_is_too_large();
7584 #endif
7585 
7586 	if (state->ls_firstonly) {
7587 		if (state->ls_donefirst)
7588 			return (DLADM_WALK_CONTINUE);
7589 		state->ls_donefirst = B_TRUE;
7590 	} else {
7591 		bzero(brsum, sizeof (*brsum));
7592 	}
7593 	bzero(&newval, sizeof (newval));
7594 
7595 	if ((status = dladm_datalink_id2info(handle, linkid, &flags, &class,
7596 	    NULL, link, sizeof (link))) != DLADM_STATUS_OK)
7597 		return (status);
7598 
7599 	if (!(state->ls_flags & flags))
7600 		return (DLADM_STATUS_NOTFOUND);
7601 
7602 	if ((kcp = kstat_open()) == NULL) {
7603 		warn("kstat open operation failed");
7604 		return (DLADM_STATUS_OK);
7605 	}
7606 	if ((ksp = kstat_lookup(kcp, "bridge", 0, link)) != NULL &&
7607 	    kstat_read(kcp, ksp, NULL) != -1) {
7608 		if (dladm_kstat_value(ksp, "drops", KSTAT_DATA_UINT64,
7609 		    &newval.drops) == DLADM_STATUS_OK) {
7610 			(void) snprintf(bbuf->bridges_drops,
7611 			    sizeof (bbuf->bridges_drops), "%llu",
7612 			    newval.drops - brsum->drops);
7613 		}
7614 		if (dladm_kstat_value(ksp, "forward_direct", KSTAT_DATA_UINT64,
7615 		    &newval.forward_dir) == DLADM_STATUS_OK) {
7616 			(void) snprintf(bbuf->bridges_forwards,
7617 			    sizeof (bbuf->bridges_forwards), "%llu",
7618 			    newval.forward_dir - brsum->forward_dir);
7619 		}
7620 		if (dladm_kstat_value(ksp, "forward_mbcast", KSTAT_DATA_UINT64,
7621 		    &newval.forward_mb) == DLADM_STATUS_OK) {
7622 			(void) snprintf(bbuf->bridges_mbcast,
7623 			    sizeof (bbuf->bridges_mbcast), "%llu",
7624 			    newval.forward_mb - brsum->forward_mb);
7625 		}
7626 		if (dladm_kstat_value(ksp, "forward_unknown", KSTAT_DATA_UINT64,
7627 		    &newval.forward_unk) == DLADM_STATUS_OK) {
7628 			(void) snprintf(bbuf->bridges_unknown,
7629 			    sizeof (bbuf->bridges_unknown), "%llu",
7630 			    newval.forward_unk - brsum->forward_unk);
7631 		}
7632 		if (dladm_kstat_value(ksp, "recv", KSTAT_DATA_UINT64,
7633 		    &newval.recv) == DLADM_STATUS_OK) {
7634 			(void) snprintf(bbuf->bridges_recv,
7635 			    sizeof (bbuf->bridges_recv), "%llu",
7636 			    newval.recv - brsum->recv);
7637 		}
7638 		if (dladm_kstat_value(ksp, "sent", KSTAT_DATA_UINT64,
7639 		    &newval.sent) == DLADM_STATUS_OK) {
7640 			(void) snprintf(bbuf->bridges_sent,
7641 			    sizeof (bbuf->bridges_sent), "%llu",
7642 			    newval.sent - brsum->sent);
7643 		}
7644 	}
7645 	(void) kstat_close(kcp);
7646 
7647 	/* Convert observability node name back to bridge name */
7648 	if (!dladm_observe_to_bridge(link))
7649 		return (DLADM_STATUS_NOTFOUND);
7650 	(void) strlcpy(bbuf->bridges_name, link, sizeof (bbuf->bridges_name));
7651 
7652 	*brsum = newval;
7653 
7654 	return (DLADM_STATUS_OK);
7655 }
7656 
7657 /*
7658  * This structure carries around extra state information for the show-bridge
7659  * command and allows us to use common support functions.
7660  */
7661 typedef struct {
7662 	show_state_t	state;
7663 	boolean_t	show_stats;
7664 	const char	*bridge;
7665 } show_brstate_t;
7666 
7667 /* ARGSUSED */
7668 static int
7669 show_bridge(dladm_handle_t handle, datalink_id_t linkid, void *arg)
7670 {
7671 	show_brstate_t	*brstate = arg;
7672 	void *buf;
7673 
7674 	if (brstate->show_stats) {
7675 		bridge_statfields_buf_t bbuf;
7676 
7677 		bzero(&bbuf, sizeof (bbuf));
7678 		brstate->state.ls_status = print_bridge_stats(&brstate->state,
7679 		    linkid, &bbuf);
7680 		buf = &bbuf;
7681 	} else {
7682 		bridge_fields_buf_t bbuf;
7683 
7684 		bzero(&bbuf, sizeof (bbuf));
7685 		brstate->state.ls_status = print_bridge(&brstate->state, linkid,
7686 		    &bbuf);
7687 		buf = &bbuf;
7688 	}
7689 	if (brstate->state.ls_status == DLADM_STATUS_OK)
7690 		ofmt_print(brstate->state.ls_ofmt, buf);
7691 	return (DLADM_WALK_CONTINUE);
7692 }
7693 
7694 static void
7695 fmt_bool(char *buf, size_t buflen, int val)
7696 {
7697 	(void) strlcpy(buf, val ? "yes" : "no", buflen);
7698 }
7699 
7700 static dladm_status_t
7701 print_bridge_link(show_state_t *state, datalink_id_t linkid,
7702     bridge_link_fields_buf_t *bbuf)
7703 {
7704 	datalink_class_t	class;
7705 	uint32_t		flags;
7706 	dladm_status_t		status;
7707 	UID_STP_PORT_STATE_T	stpstate;
7708 
7709 	status = dladm_datalink_id2info(handle, linkid, &flags, &class, NULL,
7710 	    bbuf->bridgel_link, sizeof (bbuf->bridgel_link));
7711 	if (status != DLADM_STATUS_OK)
7712 		return (status);
7713 
7714 	if (!(state->ls_flags & flags))
7715 		return (DLADM_STATUS_NOTFOUND);
7716 
7717 	if (dladm_bridge_link_state(handle, linkid, &stpstate) ==
7718 	    DLADM_STATUS_OK) {
7719 		(void) snprintf(bbuf->bridgel_index,
7720 		    sizeof (bbuf->bridgel_index), "%u", stpstate.port_no);
7721 		if (dlsym(RTLD_PROBE, "STP_IN_state2str")) {
7722 			(void) strlcpy(bbuf->bridgel_state,
7723 			    STP_IN_state2str(stpstate.state),
7724 			    sizeof (bbuf->bridgel_state));
7725 		} else {
7726 			(void) snprintf(bbuf->bridgel_state,
7727 			    sizeof (bbuf->bridgel_state), "%u",
7728 			    stpstate.state);
7729 		}
7730 		(void) snprintf(bbuf->bridgel_uptime,
7731 		    sizeof (bbuf->bridgel_uptime), "%lu", stpstate.uptime);
7732 		(void) snprintf(bbuf->bridgel_opercost,
7733 		    sizeof (bbuf->bridgel_opercost), "%lu",
7734 		    stpstate.oper_port_path_cost);
7735 		fmt_bool(bbuf->bridgel_operp2p, sizeof (bbuf->bridgel_operp2p),
7736 		    stpstate.oper_point2point);
7737 		fmt_bool(bbuf->bridgel_operedge,
7738 		    sizeof (bbuf->bridgel_operedge), stpstate.oper_edge);
7739 		fmt_bridge_id(bbuf->bridgel_desroot,
7740 		    sizeof (bbuf->bridgel_desroot), &stpstate.designated_root);
7741 		(void) snprintf(bbuf->bridgel_descost,
7742 		    sizeof (bbuf->bridgel_descost), "%lu",
7743 		    stpstate.designated_cost);
7744 		fmt_bridge_id(bbuf->bridgel_desbridge,
7745 		    sizeof (bbuf->bridgel_desbridge),
7746 		    &stpstate.designated_bridge);
7747 		(void) snprintf(bbuf->bridgel_desport,
7748 		    sizeof (bbuf->bridgel_desport), "%u",
7749 		    stpstate.designated_port);
7750 		fmt_bool(bbuf->bridgel_tcack, sizeof (bbuf->bridgel_tcack),
7751 		    stpstate.top_change_ack);
7752 	}
7753 	return (DLADM_STATUS_OK);
7754 }
7755 
7756 static dladm_status_t
7757 print_bridge_link_stats(show_state_t *state, datalink_id_t linkid,
7758     bridge_link_statfields_buf_t *bbuf)
7759 {
7760 	datalink_class_t	class;
7761 	uint32_t		flags;
7762 	dladm_status_t		status;
7763 	UID_STP_PORT_STATE_T	stpstate;
7764 	kstat_ctl_t		*kcp;
7765 	kstat_t			*ksp;
7766 	char			bridge[MAXLINKNAMELEN];
7767 	char			kstatname[MAXLINKNAMELEN*2 + 1];
7768 	brlsum_t		*brlsum = (brlsum_t *)&state->ls_prevstats;
7769 	brlsum_t		newval;
7770 
7771 #ifndef lint
7772 	/* This is a compile-time assertion; optimizer normally fixes this */
7773 	extern void brlsum_t_is_too_large(void);
7774 
7775 	if (sizeof (*brlsum) > sizeof (state->ls_prevstats))
7776 		brlsum_t_is_too_large();
7777 #endif
7778 
7779 	if (state->ls_firstonly) {
7780 		if (state->ls_donefirst)
7781 			return (DLADM_WALK_CONTINUE);
7782 		state->ls_donefirst = B_TRUE;
7783 	} else {
7784 		bzero(brlsum, sizeof (*brlsum));
7785 	}
7786 	bzero(&newval, sizeof (newval));
7787 
7788 	status = dladm_datalink_id2info(handle, linkid, &flags, &class, NULL,
7789 	    bbuf->bridgels_link, sizeof (bbuf->bridgels_link));
7790 	if (status != DLADM_STATUS_OK)
7791 		return (status);
7792 
7793 	if (!(state->ls_flags & flags))
7794 		return (DLADM_STATUS_NOTFOUND);
7795 
7796 	if (dladm_bridge_link_state(handle, linkid, &stpstate) ==
7797 	    DLADM_STATUS_OK) {
7798 		newval.cfgbpdu = stpstate.rx_cfg_bpdu_cnt;
7799 		newval.tcnbpdu = stpstate.rx_tcn_bpdu_cnt;
7800 		newval.rstpbpdu = stpstate.rx_rstp_bpdu_cnt;
7801 		newval.txbpdu = stpstate.txCount;
7802 
7803 		(void) snprintf(bbuf->bridgels_cfgbpdu,
7804 		    sizeof (bbuf->bridgels_cfgbpdu), "%lu",
7805 		    newval.cfgbpdu - brlsum->cfgbpdu);
7806 		(void) snprintf(bbuf->bridgels_tcnbpdu,
7807 		    sizeof (bbuf->bridgels_tcnbpdu), "%lu",
7808 		    newval.tcnbpdu - brlsum->tcnbpdu);
7809 		(void) snprintf(bbuf->bridgels_rstpbpdu,
7810 		    sizeof (bbuf->bridgels_rstpbpdu), "%lu",
7811 		    newval.rstpbpdu - brlsum->rstpbpdu);
7812 		(void) snprintf(bbuf->bridgels_txbpdu,
7813 		    sizeof (bbuf->bridgels_txbpdu), "%lu",
7814 		    newval.txbpdu - brlsum->txbpdu);
7815 	}
7816 
7817 	if ((status = dladm_bridge_getlink(handle, linkid, bridge,
7818 	    sizeof (bridge))) != DLADM_STATUS_OK)
7819 		goto bls_out;
7820 	(void) snprintf(kstatname, sizeof (kstatname), "%s0-%s", bridge,
7821 	    bbuf->bridgels_link);
7822 	if ((kcp = kstat_open()) == NULL) {
7823 		warn("kstat open operation failed");
7824 		goto bls_out;
7825 	}
7826 	if ((ksp = kstat_lookup(kcp, "bridge", 0, kstatname)) != NULL &&
7827 	    kstat_read(kcp, ksp, NULL) != -1) {
7828 		if (dladm_kstat_value(ksp, "drops", KSTAT_DATA_UINT64,
7829 		    &newval.drops) != -1) {
7830 			(void) snprintf(bbuf->bridgels_drops,
7831 			    sizeof (bbuf->bridgels_drops), "%llu",
7832 			    newval.drops - brlsum->drops);
7833 		}
7834 		if (dladm_kstat_value(ksp, "recv", KSTAT_DATA_UINT64,
7835 		    &newval.recv) != -1) {
7836 			(void) snprintf(bbuf->bridgels_recv,
7837 			    sizeof (bbuf->bridgels_recv), "%llu",
7838 			    newval.recv - brlsum->recv);
7839 		}
7840 		if (dladm_kstat_value(ksp, "xmit", KSTAT_DATA_UINT64,
7841 		    &newval.xmit) != -1) {
7842 			(void) snprintf(bbuf->bridgels_xmit,
7843 			    sizeof (bbuf->bridgels_xmit), "%llu",
7844 			    newval.xmit - brlsum->xmit);
7845 		}
7846 	}
7847 	(void) kstat_close(kcp);
7848 bls_out:
7849 	*brlsum = newval;
7850 
7851 	return (status);
7852 }
7853 
7854 static void
7855 show_bridge_link(datalink_id_t linkid, show_brstate_t *brstate)
7856 {
7857 	void *buf;
7858 
7859 	if (brstate->show_stats) {
7860 		bridge_link_statfields_buf_t bbuf;
7861 
7862 		bzero(&bbuf, sizeof (bbuf));
7863 		brstate->state.ls_status = print_bridge_link_stats(
7864 		    &brstate->state, linkid, &bbuf);
7865 		buf = &bbuf;
7866 	} else {
7867 		bridge_link_fields_buf_t bbuf;
7868 
7869 		bzero(&bbuf, sizeof (bbuf));
7870 		brstate->state.ls_status = print_bridge_link(&brstate->state,
7871 		    linkid, &bbuf);
7872 		buf = &bbuf;
7873 	}
7874 	if (brstate->state.ls_status == DLADM_STATUS_OK)
7875 		ofmt_print(brstate->state.ls_ofmt, buf);
7876 }
7877 
7878 /* ARGSUSED */
7879 static int
7880 show_bridge_link_walk(dladm_handle_t handle, datalink_id_t linkid, void *arg)
7881 {
7882 	show_brstate_t	*brstate = arg;
7883 	char bridge[MAXLINKNAMELEN];
7884 
7885 	if (dladm_bridge_getlink(handle, linkid, bridge, sizeof (bridge)) ==
7886 	    DLADM_STATUS_OK && strcmp(bridge, brstate->bridge) == 0) {
7887 		show_bridge_link(linkid, brstate);
7888 	}
7889 	return (DLADM_WALK_CONTINUE);
7890 }
7891 
7892 static void
7893 show_bridge_fwd(dladm_handle_t handle, bridge_listfwd_t *blf,
7894     show_state_t *state)
7895 {
7896 	bridge_fwd_fields_buf_t bbuf;
7897 
7898 	bzero(&bbuf, sizeof (bbuf));
7899 	(void) snprintf(bbuf.bridgef_dest, sizeof (bbuf.bridgef_dest),
7900 	    "%s", ether_ntoa((struct ether_addr *)blf->blf_dest));
7901 	if (blf->blf_is_local) {
7902 		(void) strlcpy(bbuf.bridgef_flags, "L",
7903 		    sizeof (bbuf.bridgef_flags));
7904 	} else {
7905 		(void) snprintf(bbuf.bridgef_age, sizeof (bbuf.bridgef_age),
7906 		    "%2d.%03d", blf->blf_ms_age / 1000, blf->blf_ms_age % 1000);
7907 		if (blf->blf_trill_nick != 0) {
7908 			(void) snprintf(bbuf.bridgef_output,
7909 			    sizeof (bbuf.bridgef_output), "%u",
7910 			    blf->blf_trill_nick);
7911 		}
7912 	}
7913 	if (blf->blf_linkid != DATALINK_INVALID_LINKID &&
7914 	    blf->blf_trill_nick == 0) {
7915 		state->ls_status = dladm_datalink_id2info(handle,
7916 		    blf->blf_linkid, NULL, NULL, NULL, bbuf.bridgef_output,
7917 		    sizeof (bbuf.bridgef_output));
7918 	}
7919 	if (state->ls_status == DLADM_STATUS_OK)
7920 		ofmt_print(state->ls_ofmt, &bbuf);
7921 }
7922 
7923 static void
7924 show_bridge_trillnick(trill_listnick_t *tln, show_state_t *state)
7925 {
7926 	bridge_trill_fields_buf_t bbuf;
7927 
7928 	bzero(&bbuf, sizeof (bbuf));
7929 	(void) snprintf(bbuf.bridget_nick, sizeof (bbuf.bridget_nick),
7930 	    "%u", tln->tln_nick);
7931 	if (tln->tln_ours) {
7932 		(void) strlcpy(bbuf.bridget_flags, "L",
7933 		    sizeof (bbuf.bridget_flags));
7934 	} else {
7935 		state->ls_status = dladm_datalink_id2info(handle,
7936 		    tln->tln_linkid, NULL, NULL, NULL, bbuf.bridget_link,
7937 		    sizeof (bbuf.bridget_link));
7938 		(void) snprintf(bbuf.bridget_nexthop,
7939 		    sizeof (bbuf.bridget_nexthop), "%s",
7940 		    ether_ntoa((struct ether_addr *)tln->tln_nexthop));
7941 	}
7942 	if (state->ls_status == DLADM_STATUS_OK)
7943 		ofmt_print(state->ls_ofmt, &bbuf);
7944 }
7945 
7946 static void
7947 do_show_bridge(int argc, char **argv, const char *use)
7948 {
7949 	int		option;
7950 	enum {
7951 		bridgeMode, linkMode, fwdMode, trillMode
7952 	}		op_mode = bridgeMode;
7953 	uint32_t	flags = DLADM_OPT_ACTIVE | DLADM_OPT_PERSIST;
7954 	boolean_t	parsable = B_FALSE;
7955 	datalink_id_t	linkid = DATALINK_ALL_LINKID;
7956 	int		interval = 0;
7957 	show_brstate_t	brstate;
7958 	dladm_status_t	status;
7959 	char		*fields_str = NULL;
7960 	/* default: bridge-related data */
7961 	char		*all_fields = "bridge,protect,address,priority,bmaxage,"
7962 	    "bhellotime,bfwddelay,forceproto,tctime,tccount,tchange,"
7963 	    "desroot,rootcost,rootport,maxage,hellotime,fwddelay,holdtime";
7964 	char		*default_fields = "bridge,protect,address,priority,"
7965 	    "desroot";
7966 	char		*all_statfields = "bridge,drops,forwards,mbcast,"
7967 	    "unknown,recv,sent";
7968 	char		*default_statfields = "bridge,drops,forwards,mbcast,"
7969 	    "unknown";
7970 	/* -l: link-related data */
7971 	char		*all_link_fields = "link,index,state,uptime,opercost,"
7972 	    "operp2p,operedge,desroot,descost,desbridge,desport,tcack";
7973 	char		*default_link_fields = "link,state,uptime,desroot";
7974 	char		*all_link_statfields = "link,cfgbpdu,tcnbpdu,rstpbpdu,"
7975 	    "txbpdu,drops,recv,xmit";
7976 	char		*default_link_statfields = "link,drops,recv,xmit";
7977 	/* -f: bridge forwarding table related data */
7978 	char		*default_fwd_fields = "dest,age,flags,output";
7979 	/* -t: TRILL nickname table related data */
7980 	char		*default_trill_fields = "nick,flags,link,nexthop";
7981 	char		*default_str;
7982 	char		*all_str;
7983 	ofmt_field_t	*field_arr;
7984 	ofmt_handle_t	ofmt;
7985 	ofmt_status_t	oferr;
7986 	uint_t		ofmtflags = 0;
7987 
7988 	bzero(&brstate, sizeof (brstate));
7989 
7990 	opterr = 0;
7991 	while ((option = getopt_long(argc, argv, ":fi:lo:pst",
7992 	    bridge_show_lopts, NULL)) != -1) {
7993 		switch (option) {
7994 		case 'f':
7995 			if (op_mode != bridgeMode && op_mode != fwdMode)
7996 				die("-f is incompatible with -l or -t");
7997 			op_mode = fwdMode;
7998 			break;
7999 		case 'i':
8000 			if (interval != 0)
8001 				die_optdup(option);
8002 			if (!str2int(optarg, &interval) || interval == 0)
8003 				die("invalid interval value '%s'", optarg);
8004 			break;
8005 		case 'l':
8006 			if (op_mode != bridgeMode && op_mode != linkMode)
8007 				die("-l is incompatible with -f or -t");
8008 			op_mode = linkMode;
8009 			break;
8010 		case 'o':
8011 			fields_str = optarg;
8012 			break;
8013 		case 'p':
8014 			if (parsable)
8015 				die_optdup(option);
8016 			parsable = B_TRUE;
8017 			break;
8018 		case 's':
8019 			if (brstate.show_stats)
8020 				die_optdup(option);
8021 			brstate.show_stats = B_TRUE;
8022 			break;
8023 		case 't':
8024 			if (op_mode != bridgeMode && op_mode != trillMode)
8025 				die("-t is incompatible with -f or -l");
8026 			op_mode = trillMode;
8027 			break;
8028 		default:
8029 			die_opterr(optopt, option, use);
8030 			break;
8031 		}
8032 	}
8033 
8034 	if (interval != 0 && !brstate.show_stats)
8035 		die("the -i option can be used only with -s");
8036 
8037 	if ((op_mode == fwdMode || op_mode == trillMode) && brstate.show_stats)
8038 		die("the -f/-t and -s options cannot be used together");
8039 
8040 	/* get the bridge name (optional last argument) */
8041 	if (optind == (argc-1)) {
8042 		char lname[MAXLINKNAMELEN];
8043 		uint32_t lnkflg;
8044 		datalink_class_t class;
8045 
8046 		brstate.bridge = argv[optind];
8047 		(void) snprintf(lname, sizeof (lname), "%s0", brstate.bridge);
8048 		if ((status = dladm_name2info(handle, lname, &linkid, &lnkflg,
8049 		    &class, NULL)) != DLADM_STATUS_OK) {
8050 			die_dlerr(status, "bridge %s is not valid",
8051 			    brstate.bridge);
8052 		}
8053 
8054 		if (class != DATALINK_CLASS_BRIDGE)
8055 			die("%s is not a bridge", brstate.bridge);
8056 
8057 		if (!(lnkflg & flags)) {
8058 			die_dlerr(DLADM_STATUS_BADARG,
8059 			    "bridge %s is temporarily removed", brstate.bridge);
8060 		}
8061 	} else if (optind != argc) {
8062 		usage();
8063 	} else if (op_mode != bridgeMode) {
8064 		die("bridge name required for -l, -f, or -t");
8065 		return;
8066 	}
8067 
8068 	brstate.state.ls_parsable = parsable;
8069 	brstate.state.ls_flags = flags;
8070 	brstate.state.ls_firstonly = (interval != 0);
8071 
8072 	switch (op_mode) {
8073 	case bridgeMode:
8074 		if (brstate.show_stats) {
8075 			default_str = default_statfields;
8076 			all_str = all_statfields;
8077 			field_arr = bridge_statfields;
8078 		} else {
8079 			default_str = default_fields;
8080 			all_str = all_fields;
8081 			field_arr = bridge_fields;
8082 		}
8083 		break;
8084 
8085 	case linkMode:
8086 		if (brstate.show_stats) {
8087 			default_str = default_link_statfields;
8088 			all_str = all_link_statfields;
8089 			field_arr = bridge_link_statfields;
8090 		} else {
8091 			default_str = default_link_fields;
8092 			all_str = all_link_fields;
8093 			field_arr = bridge_link_fields;
8094 		}
8095 		break;
8096 
8097 	case fwdMode:
8098 		default_str = all_str = default_fwd_fields;
8099 		field_arr = bridge_fwd_fields;
8100 		break;
8101 
8102 	case trillMode:
8103 		default_str = all_str = default_trill_fields;
8104 		field_arr = bridge_trill_fields;
8105 		break;
8106 	}
8107 
8108 	if (fields_str == NULL)
8109 		fields_str = default_str;
8110 	else if (strcasecmp(fields_str, "all") == 0)
8111 		fields_str = all_str;
8112 
8113 	if (parsable)
8114 		ofmtflags |= OFMT_PARSABLE;
8115 	oferr = ofmt_open(fields_str, field_arr, ofmtflags, 0, &ofmt);
8116 	dladm_ofmt_check(oferr, brstate.state.ls_parsable, ofmt);
8117 	brstate.state.ls_ofmt = ofmt;
8118 
8119 	for (;;) {
8120 		brstate.state.ls_donefirst = B_FALSE;
8121 		switch (op_mode) {
8122 		case bridgeMode:
8123 			if (linkid == DATALINK_ALL_LINKID) {
8124 				(void) dladm_walk_datalink_id(show_bridge,
8125 				    handle, &brstate, DATALINK_CLASS_BRIDGE,
8126 				    DATALINK_ANY_MEDIATYPE, flags);
8127 			} else {
8128 				(void) show_bridge(handle, linkid, &brstate);
8129 				if (brstate.state.ls_status !=
8130 				    DLADM_STATUS_OK) {
8131 					die_dlerr(brstate.state.ls_status,
8132 					    "failed to show bridge %s",
8133 					    brstate.bridge);
8134 				}
8135 			}
8136 			break;
8137 
8138 		case linkMode: {
8139 			datalink_id_t *dlp;
8140 			uint_t i, nlinks;
8141 
8142 			dlp = dladm_bridge_get_portlist(brstate.bridge,
8143 			    &nlinks);
8144 			if (dlp != NULL) {
8145 				for (i = 0; i < nlinks; i++)
8146 					show_bridge_link(dlp[i], &brstate);
8147 				dladm_bridge_free_portlist(dlp);
8148 			} else if (errno == ENOENT) {
8149 				/* bridge not running; iterate on libdladm */
8150 				(void) dladm_walk_datalink_id(
8151 				    show_bridge_link_walk, handle,
8152 				    &brstate, DATALINK_CLASS_PHYS |
8153 				    DATALINK_CLASS_AGGR |
8154 				    DATALINK_CLASS_ETHERSTUB,
8155 				    DATALINK_ANY_MEDIATYPE, flags);
8156 			} else {
8157 				die("unable to get port list for bridge %s: %s",
8158 				    brstate.bridge, strerror(errno));
8159 			}
8160 			break;
8161 		}
8162 
8163 		case fwdMode: {
8164 			bridge_listfwd_t *blf;
8165 			uint_t i, nfwd;
8166 
8167 			blf = dladm_bridge_get_fwdtable(handle, brstate.bridge,
8168 			    &nfwd);
8169 			if (blf == NULL) {
8170 				die("unable to get forwarding entries for "
8171 				    "bridge %s", brstate.bridge);
8172 			} else {
8173 				for (i = 0; i < nfwd; i++)
8174 					show_bridge_fwd(handle, blf + i,
8175 					    &brstate.state);
8176 				dladm_bridge_free_fwdtable(blf);
8177 			}
8178 			break;
8179 		}
8180 
8181 		case trillMode: {
8182 			trill_listnick_t *tln;
8183 			uint_t i, nnick;
8184 
8185 			tln = dladm_bridge_get_trillnick(brstate.bridge,
8186 			    &nnick);
8187 			if (tln == NULL) {
8188 				if (errno == ENOENT)
8189 					die("bridge %s is not running TRILL",
8190 					    brstate.bridge);
8191 				else
8192 					die("unable to get TRILL nickname "
8193 					    "entries for bridge %s",
8194 					    brstate.bridge);
8195 			} else {
8196 				for (i = 0; i < nnick; i++)
8197 					show_bridge_trillnick(tln + i,
8198 					    &brstate.state);
8199 				dladm_bridge_free_trillnick(tln);
8200 			}
8201 			break;
8202 		}
8203 		}
8204 		if (interval == 0)
8205 			break;
8206 		(void) sleep(interval);
8207 	}
8208 }
8209 
8210 /*
8211  * "-R" option support. It is used for live upgrading. Append dladm commands
8212  * to a upgrade script which will be run when the alternative root boots up:
8213  *
8214  * - If the /etc/dladm/datalink.conf file exists on the alternative root,
8215  * append dladm commands to the <altroot>/var/svc/profile/upgrade_datalink
8216  * script. This script will be run as part of the network/physical service.
8217  * We cannot defer this to /var/svc/profile/upgrade because then the
8218  * configuration will not be able to take effect before network/physical
8219  * plumbs various interfaces.
8220  *
8221  * - If the /etc/dladm/datalink.conf file does not exist on the alternative
8222  * root, append dladm commands to the <altroot>/var/svc/profile/upgrade script,
8223  * which will be run in the manifest-import service.
8224  *
8225  * Note that the SMF team is considering to move the manifest-import service
8226  * to be run at the very begining of boot. Once that is done, the need for
8227  * the /var/svc/profile/upgrade_datalink script will not exist any more.
8228  */
8229 static void
8230 altroot_cmd(char *altroot, int argc, char *argv[])
8231 {
8232 	char		path[MAXPATHLEN];
8233 	struct stat	stbuf;
8234 	FILE		*fp;
8235 	int		i;
8236 
8237 	/*
8238 	 * Check for the existence of the /etc/dladm/datalink.conf
8239 	 * configuration file, and determine the name of script file.
8240 	 */
8241 	(void) snprintf(path, MAXPATHLEN, "/%s/etc/dladm/datalink.conf",
8242 	    altroot);
8243 	if (stat(path, &stbuf) < 0) {
8244 		(void) snprintf(path, MAXPATHLEN, "/%s/%s", altroot,
8245 		    SMF_UPGRADE_FILE);
8246 	} else {
8247 		(void) snprintf(path, MAXPATHLEN, "/%s/%s", altroot,
8248 		    SMF_UPGRADEDATALINK_FILE);
8249 	}
8250 
8251 	if ((fp = fopen(path, "a+")) == NULL)
8252 		die("operation not supported on %s", altroot);
8253 
8254 	(void) fprintf(fp, "/sbin/dladm ");
8255 	for (i = 0; i < argc; i++) {
8256 		/*
8257 		 * Directly write to the file if it is not the "-R <altroot>"
8258 		 * option. In which case, skip it.
8259 		 */
8260 		if (strcmp(argv[i], "-R") != 0)
8261 			(void) fprintf(fp, "%s ", argv[i]);
8262 		else
8263 			i ++;
8264 	}
8265 	(void) fprintf(fp, "%s\n", SMF_DLADM_UPGRADE_MSG);
8266 	(void) fclose(fp);
8267 	dladm_close(handle);
8268 	exit(0);
8269 }
8270 
8271 /*
8272  * Convert the string to an integer. Note that the string must not have any
8273  * trailing non-integer characters.
8274  */
8275 static boolean_t
8276 str2int(const char *str, int *valp)
8277 {
8278 	int	val;
8279 	char	*endp = NULL;
8280 
8281 	errno = 0;
8282 	val = strtol(str, &endp, 10);
8283 	if (errno != 0 || *endp != '\0')
8284 		return (B_FALSE);
8285 
8286 	*valp = val;
8287 	return (B_TRUE);
8288 }
8289 
8290 /* PRINTFLIKE1 */
8291 static void
8292 warn(const char *format, ...)
8293 {
8294 	va_list alist;
8295 
8296 	format = gettext(format);
8297 	(void) fprintf(stderr, "%s: warning: ", progname);
8298 
8299 	va_start(alist, format);
8300 	(void) vfprintf(stderr, format, alist);
8301 	va_end(alist);
8302 
8303 	(void) putchar('\n');
8304 }
8305 
8306 /* PRINTFLIKE2 */
8307 static void
8308 warn_dlerr(dladm_status_t err, const char *format, ...)
8309 {
8310 	va_list alist;
8311 	char	errmsg[DLADM_STRSIZE];
8312 
8313 	format = gettext(format);
8314 	(void) fprintf(stderr, gettext("%s: warning: "), progname);
8315 
8316 	va_start(alist, format);
8317 	(void) vfprintf(stderr, format, alist);
8318 	va_end(alist);
8319 	(void) fprintf(stderr, ": %s\n", dladm_status2str(err, errmsg));
8320 }
8321 
8322 /*
8323  * Also closes the dladm handle if it is not NULL.
8324  */
8325 /* PRINTFLIKE2 */
8326 static void
8327 die_dlerr(dladm_status_t err, const char *format, ...)
8328 {
8329 	va_list alist;
8330 	char	errmsg[DLADM_STRSIZE];
8331 
8332 	format = gettext(format);
8333 	(void) fprintf(stderr, "%s: ", progname);
8334 
8335 	va_start(alist, format);
8336 	(void) vfprintf(stderr, format, alist);
8337 	va_end(alist);
8338 	(void) fprintf(stderr, ": %s\n", dladm_status2str(err, errmsg));
8339 
8340 	/* close dladm handle if it was opened */
8341 	if (handle != NULL)
8342 		dladm_close(handle);
8343 
8344 	exit(EXIT_FAILURE);
8345 }
8346 
8347 /* PRINTFLIKE1 */
8348 static void
8349 die(const char *format, ...)
8350 {
8351 	va_list alist;
8352 
8353 	format = gettext(format);
8354 	(void) fprintf(stderr, "%s: ", progname);
8355 
8356 	va_start(alist, format);
8357 	(void) vfprintf(stderr, format, alist);
8358 	va_end(alist);
8359 
8360 	(void) putchar('\n');
8361 
8362 	/* close dladm handle if it was opened */
8363 	if (handle != NULL)
8364 		dladm_close(handle);
8365 
8366 	exit(EXIT_FAILURE);
8367 }
8368 
8369 static void
8370 die_optdup(int opt)
8371 {
8372 	die("the option -%c cannot be specified more than once", opt);
8373 }
8374 
8375 static void
8376 die_opterr(int opt, int opterr, const char *usage)
8377 {
8378 	switch (opterr) {
8379 	case ':':
8380 		die("option '-%c' requires a value\nusage: %s", opt,
8381 		    gettext(usage));
8382 		break;
8383 	case '?':
8384 	default:
8385 		die("unrecognized option '-%c'\nusage: %s", opt,
8386 		    gettext(usage));
8387 		break;
8388 	}
8389 }
8390 
8391 static void
8392 show_ether_xprop(void *arg, dladm_ether_info_t *eattr)
8393 {
8394 	print_ether_state_t	*statep = arg;
8395 	ether_fields_buf_t	ebuf;
8396 	int			i;
8397 
8398 	for (i = CAPABLE; i <= PEERADV; i++)  {
8399 		bzero(&ebuf, sizeof (ebuf));
8400 		(void) strlcpy(ebuf.eth_ptype, ptype[i],
8401 		    sizeof (ebuf.eth_ptype));
8402 		(void) dladm_ether_autoneg2str(ebuf.eth_autoneg,
8403 		    sizeof (ebuf.eth_autoneg), eattr, i);
8404 		(void) dladm_ether_spdx2str(ebuf.eth_spdx,
8405 		    sizeof (ebuf.eth_spdx), eattr, i);
8406 		(void) dladm_ether_pause2str(ebuf.eth_pause,
8407 		    sizeof (ebuf.eth_pause), eattr, i);
8408 		(void) strlcpy(ebuf.eth_rem_fault,
8409 		    (eattr->lei_attr[i].le_fault ? "fault" : "none"),
8410 		    sizeof (ebuf.eth_rem_fault));
8411 		ofmt_print(statep->es_ofmt, &ebuf);
8412 	}
8413 
8414 }
8415 
8416 static boolean_t
8417 link_is_ether(const char *link, datalink_id_t *linkid)
8418 {
8419 	uint32_t media;
8420 	datalink_class_t class;
8421 
8422 	if (dladm_name2info(handle, link, linkid, NULL, &class, &media) ==
8423 	    DLADM_STATUS_OK) {
8424 		if (class == DATALINK_CLASS_PHYS && media == DL_ETHER)
8425 			return (B_TRUE);
8426 	}
8427 	return (B_FALSE);
8428 }
8429 
8430 /*
8431  * default output callback function that, when invoked,
8432  * prints string which is offset by ofmt_arg->ofmt_id within buf.
8433  */
8434 static boolean_t
8435 print_default_cb(ofmt_arg_t *ofarg, char *buf, uint_t bufsize)
8436 {
8437 	char *value;
8438 
8439 	value = (char *)ofarg->ofmt_cbarg + ofarg->ofmt_id;
8440 	(void) strlcpy(buf, value, bufsize);
8441 	return (B_TRUE);
8442 }
8443 
8444 static void
8445 dladm_ofmt_check(ofmt_status_t oferr, boolean_t parsable,
8446     ofmt_handle_t ofmt)
8447 {
8448 	char buf[OFMT_BUFSIZE];
8449 
8450 	if (oferr == OFMT_SUCCESS)
8451 		return;
8452 	(void) ofmt_strerror(ofmt, oferr, buf, sizeof (buf));
8453 	/*
8454 	 * All errors are considered fatal in parsable mode.
8455 	 * NOMEM errors are always fatal, regardless of mode.
8456 	 * For other errors, we print diagnostics in human-readable
8457 	 * mode and processs what we can.
8458 	 */
8459 	if (parsable || oferr == OFMT_ENOFIELDS) {
8460 		ofmt_close(ofmt);
8461 		die(buf);
8462 	} else {
8463 		warn(buf);
8464 	}
8465 }
8466