xref: /titanic_51/usr/src/cmd/cmd-inet/usr.sbin/ifconfig/ifconfig.c (revision f3680c6ce4e182fbab2b5d7ea0f9989e952b81d2)
1 /*
2  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
3  */
4 /*
5  * Copyright (c) 1983 Regents of the University of California.
6  * All rights reserved.  The Berkeley software License Agreement
7  * specifies the terms and conditions for redistribution.
8  */
9 
10 #include "defs.h"
11 #include "strings.h"
12 #include "ifconfig.h"
13 #include <compat.h>
14 #include <libdlpi.h>
15 #include <libdllink.h>
16 #include <libdliptun.h>
17 #include <libdllink.h>
18 #include <inet/ip.h>
19 #include <inet/ipsec_impl.h>
20 #include <libipadm.h>
21 #include <ifaddrs.h>
22 #include <libsocket_priv.h>
23 
24 #define	LOOPBACK_IF	"lo0"
25 #define	NONE_STR	"none"
26 #define	ARP_MOD_NAME	"arp"
27 #define	LIFC_DEFAULT	(LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES |\
28 			LIFC_UNDER_IPMP)
29 
30 typedef struct if_flags {
31 	uint64_t iff_value;
32 	char	*iff_name;
33 } if_flags_t;
34 
35 static if_flags_t	if_flags_tbl[] = {
36 	{ IFF_UP,		"UP" },
37 	{ IFF_BROADCAST,	"BROADCAST" },
38 	{ IFF_DEBUG,		"DEBUG" },
39 	{ IFF_LOOPBACK,		"LOOPBACK" },
40 	{ IFF_POINTOPOINT,	"POINTOPOINT" },
41 	{ IFF_NOTRAILERS,	"NOTRAILERS" },
42 	{ IFF_RUNNING,		"RUNNING" },
43 	{ IFF_NOARP,		"NOARP" },
44 	{ IFF_PROMISC,		"PROMISC" },
45 	{ IFF_ALLMULTI,		"ALLMULTI" },
46 	{ IFF_INTELLIGENT,	"INTELLIGENT" },
47 	{ IFF_MULTICAST,	"MULTICAST" },
48 	{ IFF_MULTI_BCAST,	"MULTI_BCAST" },
49 	{ IFF_UNNUMBERED,	"UNNUMBERED" },
50 	{ IFF_DHCPRUNNING,	"DHCP" },
51 	{ IFF_PRIVATE,		"PRIVATE" },
52 	{ IFF_NOXMIT,		"NOXMIT" },
53 	{ IFF_NOLOCAL,		"NOLOCAL" },
54 	{ IFF_DEPRECATED,	"DEPRECATED" },
55 	{ IFF_ADDRCONF,		"ADDRCONF" },
56 	{ IFF_ROUTER,		"ROUTER" },
57 	{ IFF_NONUD,		"NONUD" },
58 	{ IFF_ANYCAST,		"ANYCAST" },
59 	{ IFF_NORTEXCH,		"NORTEXCH" },
60 	{ IFF_IPV4,		"IPv4" },
61 	{ IFF_IPV6,		"IPv6" },
62 	{ IFF_NOFAILOVER,	"NOFAILOVER" },
63 	{ IFF_FAILED,		"FAILED" },
64 	{ IFF_STANDBY,		"STANDBY" },
65 	{ IFF_INACTIVE,		"INACTIVE" },
66 	{ IFF_OFFLINE,		"OFFLINE" },
67 	{ IFF_XRESOLV,		"XRESOLV" },
68 	{ IFF_COS_ENABLED,	"CoS" },
69 	{ IFF_PREFERRED,	"PREFERRED" },
70 	{ IFF_TEMPORARY,	"TEMPORARY" },
71 	{ IFF_FIXEDMTU,		"FIXEDMTU" },
72 	{ IFF_VIRTUAL,		"VIRTUAL" },
73 	{ IFF_DUPLICATE,	"DUPLICATE" },
74 	{ IFF_IPMP,		"IPMP"},
75 	{ IFF_VRRP,		"VRRP"},
76 	{ IFF_NOACCEPT,		"NOACCEPT"}
77 };
78 
79 typedef struct {
80 	const char		*ia_app;
81 	uint64_t		ia_flag;
82 	uint_t			ia_tries;
83 } if_appflags_t;
84 
85 static const if_appflags_t if_appflags_tbl[] = {
86 	{ "dhcpagent(1M)",	IFF_DHCPRUNNING,	1 },
87 	{ "in.ndpd(1M)",	IFF_ADDRCONF,		3 },
88 	{  NULL,		0,			0 }
89 };
90 
91 static dladm_handle_t	dlh;
92 boolean_t		dlh_opened;
93 static struct		lifreq lifr;
94 /* current interface name a particular function is accessing */
95 static char		name[LIFNAMSIZ];
96 /* foreach interface saved name */
97 static char		origname[LIFNAMSIZ];
98 static int		setaddr;
99 static boolean_t	setaddr_done = _B_FALSE;
100 static boolean_t	ipsec_policy_set;
101 static boolean_t	ipsec_auth_covered;
102 static ipadm_handle_t	iph;
103 static ipadm_addrobj_t	ipaddr;
104 
105 /*
106  * Make sure the algorithm variables hold more than the sizeof an algorithm
107  * in PF_KEY.  (For now, more than a uint8_t.)  The NO_***_?ALG indicates that
108  * there was no algorithm requested, and in the ipsec_req that service should
109  * be disabled.  (E.g. if ah_aalg remains NO_AH_AALG, then AH will be
110  * disabled on that tunnel.)
111  */
112 #define	NO_AH_AALG 256
113 #define	NO_ESP_AALG 256
114 #define	NO_ESP_EALG 256
115 
116 int	s, s4, s6;
117 int	af = AF_INET;	/* default address family */
118 int	debug = 0;
119 int	all = 0;	/* setifdhcp() needs to know this */
120 int	verbose = 0;
121 int	v4compat = 0;	/* Compatible printing format */
122 
123 /*
124  * Function prototypes for command functions.
125  */
126 static int	addif(char *arg, int64_t param);
127 static int	inetipmp(char *arg, int64_t param);
128 static int	inetplumb(char *arg, int64_t param);
129 static int	inetunplumb(char *arg, int64_t param);
130 static int	removeif(char *arg, int64_t param);
131 static int	setdebugflag(char *arg, int64_t param);
132 static int	setifaddr(char *arg, int64_t param);
133 static int	setifbroadaddr(char *arg, int64_t param);
134 static int	setifdstaddr(char *arg, int64_t param);
135 static int	setifether(char *arg, int64_t param);
136 static int	setifflags(char *arg, int64_t param);
137 static int	setifindex(char *arg, int64_t param);
138 static int	setifmetric(char *arg, int64_t param);
139 static int	setifmtu(char *arg, int64_t param);
140 static int	setifnetmask(char *arg, int64_t param);
141 static int	setifprefixlen(char *arg, int64_t param);
142 static int	setifrevarp(char *arg, int64_t param);
143 static int	setifsubnet(char *arg, int64_t param);
144 static int	setiftdst(char *arg, int64_t param);
145 static int	setiftoken(char *arg, int64_t param);
146 static int	setiftsrc(char *arg, int64_t param);
147 static int	setverboseflag(char *arg, int64_t param);
148 static int	set_tun_ah_alg(char *arg, int64_t param);
149 static int	set_tun_esp_auth_alg(char *arg, int64_t param);
150 static int	set_tun_esp_encr_alg(char *arg, int64_t param);
151 static int	modlist(char *arg, int64_t param);
152 static int	modinsert(char *arg, int64_t param);
153 static int	modremove(char *arg, int64_t param);
154 static int	setifgroupname(char *arg, int64_t param);
155 static int	configinfo(char *arg, int64_t param);
156 static void	print_config_flags(int af, uint64_t flags);
157 static void	print_flags(uint64_t flags);
158 static void	print_ifether(const char *ifname);
159 static int	set_tun_encap_limit(char *arg, int64_t param);
160 static int	clr_tun_encap_limit(char *arg, int64_t param);
161 static int	set_tun_hop_limit(char *arg, int64_t param);
162 static int	setzone(char *arg, int64_t param);
163 static int	setallzones(char *arg, int64_t param);
164 static int	setifsrc(char *arg, int64_t param);
165 static int	lifnum(const char *ifname);
166 static void	plumball(int, char **, int64_t, int64_t, int64_t);
167 
168 /*
169  * Address family specific function prototypes.
170  */
171 static void	in_getaddr(char *s, struct sockaddr *saddr, int *plenp);
172 static void	in_status(int force, uint64_t flags);
173 static void	in_configinfo(int force, uint64_t flags);
174 static void	in6_getaddr(char *s, struct sockaddr *saddr, int *plenp);
175 static void	in6_status(int force, uint64_t flags);
176 static void	in6_configinfo(int force, uint64_t flags);
177 
178 /*
179  * Misc support functions
180  */
181 static boolean_t	ni_entry(const char *, void *);
182 static void		foreachinterface(int argc, char *argv[],
183 			    int af, int64_t onflags, int64_t offflags,
184 			    int64_t lifc_flags);
185 static void		ifconfig(int argc, char *argv[], int af,
186 			    struct ifaddrs *ifa);
187 static boolean_t	in_getmask(struct sockaddr_in *saddr,
188 			    boolean_t addr_set);
189 static int		in_getprefixlen(char *addr, boolean_t slash, int plen);
190 static boolean_t	in_prefixlentomask(int prefixlen, int maxlen,
191 			    uchar_t *mask);
192 static void		status(void);
193 static void		ifstatus(const char *ifname);
194 static void		tun_status(datalink_id_t);
195 static void		usage(void);
196 static int		setifdhcp(const char *caller, const char *ifname,
197 			    int argc, char *argv[]);
198 static int		ip_domux2fd(int *, int *, int *, int *, int *);
199 static int		ip_plink(int, int, int, int, int);
200 static int		modop(char *arg, char op);
201 static int		find_all_interfaces(struct lifconf *lifcp, char **buf,
202 			    int64_t lifc_flags);
203 static int		create_ipmp(const char *grname, int af,
204 			    const char *ifname, boolean_t implicit);
205 static void		start_ipmp_daemon(void);
206 static boolean_t 	ifaddr_up(ifaddrlistx_t *ifaddrp);
207 static boolean_t 	ifaddr_down(ifaddrlistx_t *ifaddrp);
208 static dladm_status_t	ifconfig_dladm_open(const char *, datalink_class_t,
209 			    datalink_id_t *);
210 static void		dladmerr_exit(dladm_status_t status, const char *str);
211 static void		ipadmerr_exit(ipadm_status_t status, const char *str);
212 static boolean_t	ifconfig_use_libipadm(int, const char *);
213 
214 #define	max(a, b)	((a) < (b) ? (b) : (a))
215 
216 /*
217  * DHCP_EXIT_IF_FAILURE indicates that the operation failed, but if there
218  * are more interfaces to act on (i.e., ifconfig was invoked with -a), keep
219  * on going rather than exit with an error.
220  */
221 
222 #define	DHCP_EXIT_IF_FAILURE	-1
223 
224 #define	NEXTARG		0xffffff	/* command takes an argument */
225 #define	OPTARG		0xfffffe 	/* command takes an optional argument */
226 #define	AF_ANY		(-1)
227 
228 /* Refer to the comments in ifconfig() on the netmask "hack" */
229 #define	NETMASK_CMD	"netmask"
230 struct sockaddr_storage	g_netmask;
231 enum { G_NETMASK_NIL, G_NETMASK_PENDING, G_NETMASK_SET }
232     g_netmask_set = G_NETMASK_NIL;
233 
234 struct	cmd {
235 	char		*c_name;
236 	int64_t		c_parameter;	/* NEXTARG means next argv */
237 	int		(*c_func)(char *, int64_t);
238 	int		c_abortonfail;	/* don't continue parsing args */
239 					/* for the current interface */
240 	int	c_af;			/* address family restrictions */
241 } cmds[] = {
242 	{ "up",		IFF_UP,		setifflags,	0,	AF_ANY },
243 	{ "down",	-IFF_UP,	setifflags,	0,	AF_ANY },
244 	{ "trailers",	-IFF_NOTRAILERS, setifflags,	0,	AF_ANY },
245 	{ "-trailers",	IFF_NOTRAILERS,	setifflags,	0,	AF_ANY },
246 	{ "arp",	-IFF_NOARP,	setifflags,	0,	AF_INET },
247 	{ "-arp",	IFF_NOARP,	setifflags,	0,	AF_INET },
248 	{ "router",	IFF_ROUTER,	setifflags,	0,	AF_ANY },
249 	{ "-router",	-IFF_ROUTER,	setifflags,	0,	AF_ANY },
250 	{ "private",	IFF_PRIVATE,	setifflags,	0,	AF_ANY },
251 	{ "-private",	-IFF_PRIVATE,	setifflags,	0,	AF_ANY },
252 	{ "xmit",	-IFF_NOXMIT,	setifflags,	0,	AF_ANY },
253 	{ "-xmit",	IFF_NOXMIT,	setifflags,	0,	AF_ANY },
254 	{ "-nud",	IFF_NONUD,	setifflags,	0,	AF_INET6 },
255 	{ "nud",	-IFF_NONUD,	setifflags,	0,	AF_INET6 },
256 	{ "anycast",	IFF_ANYCAST,	setifflags,	0,	AF_ANY },
257 	{ "-anycast",	-IFF_ANYCAST,	setifflags,	0,	AF_ANY },
258 	{ "local",	-IFF_NOLOCAL,	setifflags,	0,	AF_ANY },
259 	{ "-local",	IFF_NOLOCAL,	setifflags,	0,	AF_ANY },
260 	{ "deprecated",	IFF_DEPRECATED,	setifflags,	0,	AF_ANY },
261 	{ "-deprecated", -IFF_DEPRECATED, setifflags,	0,	AF_ANY },
262 	{ "preferred",	IFF_PREFERRED,	setifflags,	0,	AF_INET6 },
263 	{ "-preferred",	-IFF_PREFERRED,	setifflags,	0,	AF_INET6 },
264 	{ "debug",	0,		setdebugflag,	0,	AF_ANY },
265 	{ "verbose",	0,		setverboseflag,	0,	AF_ANY },
266 	{ NETMASK_CMD,	NEXTARG,	setifnetmask,	0,	AF_INET },
267 	{ "metric",	NEXTARG,	setifmetric,	0,	AF_ANY },
268 	{ "mtu",	NEXTARG,	setifmtu,	0,	AF_ANY },
269 	{ "index",	NEXTARG,	setifindex,	0,	AF_ANY },
270 	{ "broadcast",	NEXTARG,	setifbroadaddr,	0,	AF_INET },
271 	{ "auto-revarp", 0,		setifrevarp,	1,	AF_INET },
272 	{ "ipmp",	0,		inetipmp,	1,	AF_ANY },
273 	{ "plumb",	0,		inetplumb,	1,	AF_ANY },
274 	{ "unplumb",	0,		inetunplumb,	0,	AF_ANY },
275 	{ "subnet",	NEXTARG,	setifsubnet,	0,	AF_ANY },
276 	{ "token",	NEXTARG,	setiftoken,	0,	AF_INET6 },
277 	{ "tsrc",	NEXTARG,	setiftsrc,	0,	AF_ANY },
278 	{ "tdst",	NEXTARG,	setiftdst,	0,	AF_ANY },
279 	{ "encr_auth_algs", NEXTARG,	set_tun_esp_auth_alg, 0, AF_ANY },
280 	{ "encr_algs",	NEXTARG,	set_tun_esp_encr_alg, 0, AF_ANY },
281 	{ "auth_algs",	NEXTARG,	set_tun_ah_alg,	0,	AF_ANY },
282 	{ "addif",	NEXTARG,	addif,		1,	AF_ANY },
283 	{ "removeif",	NEXTARG,	removeif,	1,	AF_ANY },
284 	{ "modlist",	0,		modlist,	1,	AF_ANY },
285 	{ "modinsert",	NEXTARG,	modinsert,	1,	AF_ANY },
286 	{ "modremove",	NEXTARG,	modremove,	1,	AF_ANY },
287 	{ "failover",	-IFF_NOFAILOVER, setifflags,	1,	AF_ANY },
288 	{ "-failover",	IFF_NOFAILOVER, setifflags,	1,	AF_ANY },
289 	{ "standby",	IFF_STANDBY,	setifflags,	1,	AF_ANY },
290 	{ "-standby",	-IFF_STANDBY,	setifflags,	1,	AF_ANY },
291 	{ "failed",	IFF_FAILED,	setifflags,	1,	AF_ANY },
292 	{ "-failed",	-IFF_FAILED,	setifflags,	1,	AF_ANY },
293 	{ "group",	NEXTARG,	setifgroupname,	1,	AF_ANY },
294 	{ "configinfo",	0,		configinfo,	1,	AF_ANY },
295 	{ "encaplimit",	NEXTARG,	set_tun_encap_limit,	0, AF_ANY },
296 	{ "-encaplimit", 0,		clr_tun_encap_limit,	0, AF_ANY },
297 	{ "thoplimit",	NEXTARG,	set_tun_hop_limit,	0, AF_ANY },
298 	{ "set",	NEXTARG,	setifaddr,	0,	AF_ANY },
299 	{ "destination", NEXTARG,	setifdstaddr,	0,	AF_ANY },
300 	{ "zone",	NEXTARG,	setzone,	0,	AF_ANY },
301 	{ "-zone",	0,		setzone,	0,	AF_ANY },
302 	{ "all-zones",	0,		setallzones,	0,	AF_ANY },
303 	{ "ether",	OPTARG,		setifether,	0,	AF_ANY },
304 	{ "usesrc",	NEXTARG,	setifsrc,	0,	AF_ANY },
305 
306 	/*
307 	 * NOTE: any additions to this table must also be applied to ifparse
308 	 *	(usr/src/cmd/cmd-inet/sbin/ifparse/ifparse.c)
309 	 */
310 
311 	{ 0,		0,		setifaddr,	0,	AF_ANY },
312 	{ 0,		0,		setifdstaddr,	0,	AF_ANY },
313 	{ 0,		0,		0,		0,	0 },
314 };
315 
316 
317 typedef struct if_config_cmd {
318 	uint64_t	iff_flag;
319 	int		iff_af;
320 	char		*iff_name;
321 } if_config_cmd_t;
322 
323 /*
324  * NOTE: print_config_flags() processes this table in order, so we put "up"
325  * last so that we can be sure "-failover" will take effect first.  Otherwise,
326  * IPMP test addresses will erroneously migrate to the IPMP interface.
327  */
328 static if_config_cmd_t	if_config_cmd_tbl[] = {
329 	{ IFF_NOTRAILERS,	AF_UNSPEC,	"-trailers"	},
330 	{ IFF_PRIVATE,		AF_UNSPEC,	"private"	},
331 	{ IFF_NOXMIT,		AF_UNSPEC,	"-xmit"		},
332 	{ IFF_ANYCAST,		AF_INET6,	"anycast"	},
333 	{ IFF_NOLOCAL,		AF_UNSPEC,	"-local"	},
334 	{ IFF_DEPRECATED,	AF_UNSPEC,	"deprecated"	},
335 	{ IFF_NOFAILOVER,	AF_UNSPEC,	"-failover"	},
336 	{ IFF_STANDBY,		AF_UNSPEC,	"standby"	},
337 	{ IFF_FAILED,		AF_UNSPEC,	"failed"	},
338 	{ IFF_PREFERRED,	AF_UNSPEC,	"preferred"	},
339 	{ IFF_NONUD,		AF_INET6,	"-nud"		},
340 	{ IFF_NOARP,		AF_INET,	"-arp"		},
341 	{ IFF_UP,		AF_UNSPEC, 	"up" 		},
342 	{ 0,			0,		NULL		},
343 };
344 
345 typedef struct ni {
346 	char		ni_name[LIFNAMSIZ];
347 	struct ni	*ni_next;
348 } ni_t;
349 
350 static ni_t	*ni_list = NULL;
351 static int	num_ni = 0;
352 
353 /* End defines and structure definitions for ifconfig -a plumb */
354 
355 /* Known address families */
356 struct afswtch {
357 	char *af_name;
358 	short af_af;
359 	void (*af_status)();
360 	void (*af_getaddr)();
361 	void (*af_configinfo)();
362 } afs[] = {
363 	{ "inet", AF_INET, in_status, in_getaddr, in_configinfo },
364 	{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_configinfo },
365 	{ 0, 0,	0, 0, 0 }
366 };
367 
368 #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
369 
370 struct afswtch *afp;	/* the address family being set or asked about */
371 
372 int
373 main(int argc, char *argv[])
374 {
375 	int64_t		lifc_flags;
376 	char		*default_ip_str;
377 	ipadm_status_t	istatus;
378 
379 	lifc_flags = LIFC_DEFAULT;
380 
381 	if (argc < 2) {
382 		usage();
383 		exit(1);
384 	}
385 	argc--, argv++;
386 	if (strlen(*argv) > sizeof (name) - 1) {
387 		(void) fprintf(stderr, "%s: interface name too long\n", *argv);
388 		exit(1);
389 	}
390 	(void) strncpy(name, *argv, sizeof (name));
391 	name[sizeof (name) - 1] = '\0';
392 	(void) strncpy(origname, name, sizeof (origname));	/* For addif */
393 	default_ip_str = NULL;
394 	v4compat = get_compat_flag(&default_ip_str);
395 	if (v4compat == DEFAULT_PROT_BAD_VALUE) {
396 		(void) fprintf(stderr,
397 		    "ifconfig: %s: Bad value for %s in %s\n", default_ip_str,
398 		    DEFAULT_IP, INET_DEFAULT_FILE);
399 		free(default_ip_str);
400 		exit(2);
401 	}
402 	free(default_ip_str);
403 	argc--, argv++;
404 	if (argc > 0) {
405 		struct afswtch *myafp;
406 
407 		for (myafp = afp = afs; myafp->af_name; myafp++) {
408 			if (strcmp(myafp->af_name, *argv) == 0) {
409 				afp = myafp; argc--; argv++;
410 				break;
411 			}
412 		}
413 		af = lifr.lifr_addr.ss_family = afp->af_af;
414 		if (af == AF_INET6) {
415 			v4compat = 0;
416 		}
417 	}
418 
419 	s = socket(SOCKET_AF(af), SOCK_DGRAM, 0);
420 	s4 = socket(AF_INET, SOCK_DGRAM, 0);
421 	s6 = socket(AF_INET6, SOCK_DGRAM, 0);
422 	if (s == -1 || s4 == -1 || s6 == -1)
423 		Perror0_exit("socket");
424 	/*
425 	 * Open the global libipadm handle. The flag IPH_LEGACY has to
426 	 * be specified to indicate that logical interface names will
427 	 * be used during interface creation and address creation.
428 	 */
429 	if ((istatus = ipadm_open(&iph, IPH_LEGACY)) != IPADM_SUCCESS)
430 		ipadmerr_exit(istatus, "unable to open handle to libipadm");
431 
432 	/*
433 	 * Special interface names is any combination of these flags.
434 	 * Note that due to the ifconfig syntax they have to be combined
435 	 * as a single '-' option.
436 	 *	-a	All interfaces
437 	 *	-u	"up" interfaces
438 	 *	-d	"down" interfaces
439 	 *	-D	Interfaces not controlled by DHCP
440 	 *	-4	IPv4 interfaces
441 	 *	-6	IPv6 interfaces
442 	 *	-X	Turn on debug (not documented)
443 	 *	-v	Turn on verbose
444 	 *	-Z	Only interfaces in caller's zone
445 	 */
446 
447 	if (name[0] == '-') {
448 		/* One or more options */
449 		int64_t onflags = 0;
450 		int64_t offflags = 0;
451 		int c;
452 		char *av[2] = { "ifconfig", name };
453 
454 		while ((c = getopt(2, av, "audDXZ46v")) != -1) {
455 			switch ((char)c) {
456 			case 'a':
457 				all = 1;
458 				break;
459 			case 'u':
460 				onflags |= IFF_UP;
461 				break;
462 			case 'd':
463 				offflags |= IFF_UP;
464 				break;
465 			case 'D':
466 				offflags |= IFF_DHCPRUNNING;
467 				break;
468 			case 'X':
469 				debug += 3;
470 				break;
471 			case 'Z':
472 				lifc_flags &= ~LIFC_ALLZONES;
473 				break;
474 			case '4':
475 				/*
476 				 * -4 is not a compatable flag, therefore
477 				 * we assume they want v4compat turned off
478 				 */
479 				v4compat = 0;
480 				onflags |= IFF_IPV4;
481 				break;
482 			case '6':
483 				/*
484 				 * If they want IPv6, well then we'll assume
485 				 * they don't want IPv4 compat
486 				 */
487 				v4compat = 0;
488 				onflags |= IFF_IPV6;
489 				break;
490 			case 'v':
491 				verbose = 1;
492 				break;
493 			case '?':
494 				usage();
495 				exit(1);
496 			}
497 		}
498 		if (!all) {
499 			(void) fprintf(stderr,
500 			    "ifconfig: %s: no such interface\n", name);
501 			exit(1);
502 		}
503 		foreachinterface(argc, argv, af, onflags, offflags,
504 		    lifc_flags);
505 	} else {
506 		ifconfig(argc, argv, af, NULL);
507 	}
508 	ipadm_close(iph);
509 	return (0);
510 }
511 
512 /*
513  * For each interface, call ifconfig(argc, argv, af, ifa).
514  * Only call function if onflags and offflags are set or clear, respectively,
515  * in the interfaces flags field.
516  */
517 static void
518 foreachinterface(int argc, char *argv[], int af,
519     int64_t onflags, int64_t offflags, int64_t lifc_flags)
520 {
521 	ipadm_addr_info_t *ainfo, *ainfop;
522 	struct ifaddrs *ifa;
523 	ipadm_status_t istatus;
524 
525 	/*
526 	 * Special case:
527 	 * ifconfig -a plumb should find all network interfaces in the current
528 	 * zone.
529 	 */
530 	if (argc > 0 && (strcmp(*argv, "plumb") == 0)) {
531 		plumball(argc, argv, onflags, offflags, lifc_flags);
532 		return;
533 	}
534 	/* Get all addresses in kernel including addresses that are zero. */
535 	istatus = ipadm_addr_info(iph, NULL, &ainfo, IPADM_OPT_ZEROADDR,
536 	    lifc_flags);
537 	if (istatus != IPADM_SUCCESS)
538 		ipadmerr_exit(istatus, "could not get addresses from kernel");
539 
540 	/*
541 	 * For each logical interface, call ifconfig() with the
542 	 * given arguments.
543 	 */
544 	for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop)) {
545 		if (ainfop->ia_state == IFA_DISABLED)
546 			continue;
547 		ifa = &ainfop->ia_ifa;
548 		if (onflags || offflags) {
549 			if ((ifa->ifa_flags & onflags) != onflags)
550 				continue;
551 			if ((~ifa->ifa_flags & offflags) != offflags)
552 				continue;
553 		}
554 		s = (ifa->ifa_addr->ss_family == AF_INET ? s4 : s6);
555 		(void) strncpy(name, ifa->ifa_name, sizeof (name));
556 		(void) strncpy(origname, name, sizeof (origname));
557 		ifconfig(argc, argv, af, ifa);
558 	}
559 	ipadm_free_addr_info(ainfo);
560 }
561 
562 /*
563  * Used for `ifconfig -a plumb'. Finds all datalinks and plumbs the interface.
564  */
565 static void
566 plumball(int argc, char *argv[], int64_t onflags, int64_t offflags,
567     int64_t lifc_flags)
568 {
569 	int n;
570 	struct lifreq *lifrp;
571 	struct lifconf lifc;
572 	char *buf;
573 
574 	if (onflags != 0 || offflags != 0) {
575 		(void) fprintf(stderr, "ifconfig: invalid syntax used to "
576 		    "plumb all interfaces.\n");
577 		exit(1);
578 	}
579 
580 	if (find_all_interfaces(&lifc, &buf, lifc_flags) != 0 ||
581 	    lifc.lifc_len == 0)
582 		return;
583 
584 	lifrp = lifc.lifc_req;
585 	for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) {
586 		/*
587 		 * Reset global state
588 		 * setaddr: Used by parser to tear apart source and dest
589 		 * name and origname contain the name of the 'current'
590 		 * interface.
591 		 */
592 		setaddr = 0;
593 		(void) strncpy(name, lifrp->lifr_name, sizeof (name));
594 		(void) strncpy(origname, name, sizeof (origname));
595 		ifconfig(argc, argv, af, NULL);
596 	}
597 }
598 
599 /*
600  * Parses the interface name and the command in argv[]. Calls the
601  * appropriate callback function for the given command from `cmds[]'
602  * table.
603  * If there is no command specified, it prints all addresses.
604  */
605 static void
606 ifconfig(int argc, char *argv[], int af, struct ifaddrs *ifa)
607 {
608 	static boolean_t scan_netmask = _B_FALSE;
609 	int ret;
610 	ipadm_status_t istatus;
611 	struct lifreq lifr;
612 
613 	if (argc == 0) {
614 		status();
615 		return;
616 	}
617 
618 	if (strcmp(*argv, "auto-dhcp") == 0 || strcmp(*argv, "dhcp") == 0) {
619 		/*
620 		 * Some errors are ignored in the case where more than one
621 		 * interface is being operated on.
622 		 */
623 		ret = setifdhcp("ifconfig", name, argc, argv);
624 		if (ret == DHCP_EXIT_IF_FAILURE) {
625 			if (!all)
626 				exit(DHCP_EXIT_FAILURE);
627 		} else if (ret != DHCP_EXIT_SUCCESS) {
628 			exit(ret);
629 		}
630 		return;
631 	}
632 
633 	/*
634 	 * The following is a "hack" to get around the existing interface
635 	 * setting mechanism.  Currently, each interface attribute,
636 	 * such as address, netmask, broadcast, ... is set separately.  But
637 	 * sometimes two or more attributes must be set together.  For
638 	 * example, setting an address without a netmask does not make sense.
639 	 * Yet they can be set separately for IPv4 address using the current
640 	 * ifconfig(1M) syntax.  The kernel then "infers" the correct netmask
641 	 * using the deprecated "IP address classes."  This is simply not
642 	 * correct.
643 	 *
644 	 * The "hack" below is to go thru the whole command list looking for
645 	 * the netmask command first.  Then use this netmask to set the
646 	 * address.  This does not provide an extensible way to accommodate
647 	 * future need for setting more than one attributes together.
648 	 *
649 	 * Note that if the "netmask" command argument is a "+", we need
650 	 * to save this info and do the query after we know the address to
651 	 * be set.  The reason is that if "addif" is used, the working
652 	 * interface name will be changed later when the logical interface
653 	 * is created.  In in_getmask(), if an address is not provided,
654 	 * it will use the working interface's address to do the query.
655 	 * It will be wrong now as we don't know the logical interface's name.
656 	 *
657 	 * ifconfig(1M) is too overloaded and the code is so convoluted
658 	 * that it is "safer" not to re-architect the code to fix the above
659 	 * issue, hence this "hack."  We may be better off to have a new
660 	 * command with better syntax for configuring network interface
661 	 * parameters...
662 	 */
663 	if (!scan_netmask && afp->af_af == AF_INET) {
664 		int	largc;
665 		char	**largv;
666 
667 		/* Only go thru the command list once to find the netmask. */
668 		scan_netmask = _B_TRUE;
669 
670 		/*
671 		 * Currently, if multiple netmask commands are specified, the
672 		 * last one will be used as the final netmask.  So we need
673 		 * to scan the whole list to preserve this behavior.
674 		 */
675 		for (largc = argc, largv = argv; largc > 0; largc--, largv++) {
676 			if (strcmp(*largv, NETMASK_CMD) == 0) {
677 				if (--largc == 0)
678 					break;
679 				largv++;
680 				if (strcmp(*largv, "+") == 0) {
681 					g_netmask_set = G_NETMASK_PENDING;
682 				} else {
683 					in_getaddr(*largv, (struct sockaddr *)
684 					    &g_netmask, NULL);
685 					g_netmask_set = G_NETMASK_SET;
686 				}
687 				/* Continue the scan. */
688 			}
689 		}
690 	}
691 
692 	while (argc > 0) {
693 		struct cmd *p;
694 		boolean_t found_cmd;
695 
696 		if (debug)
697 			(void) printf("ifconfig: argv %s\n", *argv);
698 
699 		found_cmd = _B_FALSE;
700 		for (p = cmds; p->c_func; p++) {
701 			if (p->c_name) {
702 				if (strcmp(*argv, p->c_name) == 0) {
703 					/*
704 					 * indicate that the command was
705 					 * found and check to see if
706 					 * the address family is valid
707 					 */
708 					found_cmd = _B_TRUE;
709 					if (p->c_af == AF_ANY ||
710 					    af == p->c_af)
711 						break;
712 				}
713 			} else {
714 				if (p->c_af == AF_ANY ||
715 				    af == p->c_af)
716 					break;
717 			}
718 		}
719 		/*
720 		 * If we found the keyword, but the address family
721 		 * did not match spit out an error
722 		 */
723 		if (found_cmd && p->c_name == 0) {
724 			(void) fprintf(stderr, "ifconfig: Operation %s not"
725 			    " supported for %s\n", *argv, afp->af_name);
726 			exit(1);
727 		}
728 		/*
729 		 * else (no keyword found), we assume it's an address
730 		 * of some sort
731 		 */
732 		if (setaddr && ipaddr != NULL) {
733 			/*
734 			 * We must have already filled in a source address in
735 			 * `ipaddr' and we now got a destination address.
736 			 * Fill it in `ipaddr' and call libipadm to create
737 			 * the static address.
738 			 */
739 			if (p->c_name == 0) {
740 				istatus = ipadm_set_dst_addr(ipaddr, *argv,
741 				    (p->c_af == AF_ANY ? AF_UNSPEC : af));
742 				if (istatus != IPADM_SUCCESS) {
743 					ipadmerr_exit(istatus, "could not "
744 					    "set destination address");
745 				}
746 				/*
747 				 * finished processing dstaddr, so reset setaddr
748 				 */
749 				setaddr = 0;
750 			}
751 			/*
752 			 * Both source and destination address are in `ipaddr'.
753 			 * Add the address by calling libipadm.
754 			 */
755 			istatus = ipadm_create_addr(iph, ipaddr,
756 			    IPADM_OPT_ACTIVE);
757 			if (istatus != IPADM_SUCCESS)
758 				goto createfailed;
759 			ipadm_destroy_addrobj(ipaddr);
760 			ipaddr = NULL;
761 			setaddr_done = _B_TRUE;
762 			if (p->c_name == 0) {
763 				/* move parser along */
764 				argc--, argv++;
765 				continue;
766 			}
767 		}
768 		if (p->c_name == 0 && setaddr_done) {
769 			/*
770 			 * catch odd commands like
771 			 * "ifconfig <intf> addr1 addr2 addr3 addr4 up"
772 			 */
773 			(void) fprintf(stderr, "%s",
774 			    "ifconfig: cannot configure more than two "
775 			    "addresses in one command\n");
776 			exit(1);
777 		}
778 		if (p->c_func) {
779 			if (p->c_af == AF_INET6) {
780 				v4compat = 0;
781 			}
782 			if (p->c_parameter == NEXTARG ||
783 			    p->c_parameter == OPTARG) {
784 				argc--, argv++;
785 				if (argc == 0 && p->c_parameter == NEXTARG) {
786 					(void) fprintf(stderr,
787 					    "ifconfig: no argument for %s\n",
788 					    p->c_name);
789 					exit(1);
790 				}
791 			}
792 			/*
793 			 *	Call the function if:
794 			 *
795 			 *		there's no address family
796 			 *		restriction
797 			 *	OR
798 			 *		we don't know the address yet
799 			 *		(because we were called from
800 			 *		main)
801 			 *	OR
802 			 *		there is a restriction AND
803 			 *		the address families match
804 			 */
805 			if ((p->c_af == AF_ANY)	||
806 			    (ifa == NULL) ||
807 			    (ifa->ifa_addr->ss_family == p->c_af)) {
808 				ret = (*p->c_func)(*argv, p->c_parameter);
809 				/*
810 				 *	If c_func failed and we should
811 				 *	abort processing for this
812 				 *	interface on failure, return
813 				 *	now rather than going on to
814 				 *	process other commands for
815 				 *	the same interface.
816 				 */
817 				if (ret != 0 && p->c_abortonfail)
818 					return;
819 			}
820 		}
821 		argc--, argv++;
822 	}
823 
824 	if (setaddr && ipaddr != NULL) {
825 		/*
826 		 * Only the source address was provided, which was already
827 		 * set in `ipaddr'. Add the address by calling libipadm.
828 		 */
829 		istatus = ipadm_create_addr(iph, ipaddr, IPADM_OPT_ACTIVE);
830 		if (istatus != IPADM_SUCCESS)
831 			goto createfailed;
832 		ipadm_destroy_addrobj(ipaddr);
833 		ipaddr = NULL;
834 		setaddr_done = _B_TRUE;
835 	}
836 
837 	/* Check to see if there's a security hole in the tunnel setup. */
838 	if (ipsec_policy_set && !ipsec_auth_covered) {
839 		(void) fprintf(stderr, "ifconfig: WARNING: tunnel with only "
840 		    "ESP and no authentication.\n");
841 	}
842 	return;
843 
844 createfailed:
845 	(void) fprintf(stderr, "ifconfig: could not create address:% s\n",
846 	    ipadm_status2str(istatus));
847 	/* Remove the newly created logical interface. */
848 	if (strcmp(name, origname) != 0) {
849 		assert(strchr(name, ':') != NULL);
850 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
851 		(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
852 	}
853 	exit(1);
854 }
855 
856 /* ARGSUSED */
857 static int
858 setdebugflag(char *val, int64_t arg)
859 {
860 	debug++;
861 	return (0);
862 }
863 
864 /* ARGSUSED */
865 static int
866 setverboseflag(char *val, int64_t arg)
867 {
868 	verbose++;
869 	return (0);
870 }
871 
872 /*
873  * This function fills in the given lifreq's lifr_addr field based on
874  * g_netmask_set.
875  */
876 static void
877 set_mask_lifreq(struct lifreq *lifr, struct sockaddr_storage *addr,
878     struct sockaddr_storage *mask)
879 {
880 	assert(addr != NULL);
881 	assert(mask != NULL);
882 
883 	switch (g_netmask_set) {
884 	case G_NETMASK_SET:
885 		lifr->lifr_addr = g_netmask;
886 		break;
887 
888 	case G_NETMASK_PENDING:
889 		/*
890 		 * "+" is used as the argument to "netmask" command.  Query
891 		 * the database on the correct netmask based on the address to
892 		 * be set.
893 		 */
894 		assert(afp->af_af == AF_INET);
895 		g_netmask = *addr;
896 		if (!in_getmask((struct sockaddr_in *)&g_netmask, _B_TRUE)) {
897 			lifr->lifr_addr = *mask;
898 			g_netmask_set = G_NETMASK_NIL;
899 		} else {
900 			lifr->lifr_addr = g_netmask;
901 			g_netmask_set = G_NETMASK_SET;
902 		}
903 		break;
904 
905 	case G_NETMASK_NIL:
906 	default:
907 		lifr->lifr_addr = *mask;
908 		break;
909 	}
910 }
911 
912 /*
913  * Set the interface address. Handles <addr>, <addr>/<n> as well as /<n>
914  * syntax for setting the address, the address plus netmask, and just
915  * the netmask respectively.
916  */
917 /* ARGSUSED */
918 static int
919 setifaddr(char *addr, int64_t param)
920 {
921 	ipadm_status_t istatus;
922 	int prefixlen = 0;
923 	struct  lifreq lifr1;
924 	struct	sockaddr_storage laddr;
925 	struct	sockaddr_storage netmask;
926 	struct	sockaddr_in6 *sin6;
927 	struct	sockaddr_in *sin;
928 	struct	sockaddr_storage sav_netmask;
929 	char cidraddr[BUFSIZ];
930 
931 	if (addr[0] == '/')
932 		return (setifprefixlen(addr, 0));
933 
934 	(*afp->af_getaddr)(addr, (struct sockaddr *)&laddr, &prefixlen);
935 
936 	(void) memset(&netmask, 0, sizeof (netmask));
937 	netmask.ss_family = afp->af_af;
938 	switch (prefixlen) {
939 	case NO_PREFIX:
940 		/* Nothing there - ok */
941 		break;
942 	case BAD_ADDR:
943 		(void) fprintf(stderr, "ifconfig: Bad prefix length in %s\n",
944 		    addr);
945 		exit(1);
946 	default:
947 		if (afp->af_af == AF_INET6) {
948 			sin6 = (struct sockaddr_in6 *)&netmask;
949 			if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
950 			    (uchar_t *)&sin6->sin6_addr)) {
951 				(void) fprintf(stderr, "ifconfig: "
952 				    "Bad prefix length: %d\n",
953 				    prefixlen);
954 				exit(1);
955 			}
956 		} else {
957 			sin = (struct sockaddr_in *)&netmask;
958 			if (!in_prefixlentomask(prefixlen, IP_ABITS,
959 			    (uchar_t *)&sin->sin_addr)) {
960 				(void) fprintf(stderr, "ifconfig: "
961 				    "Bad prefix length: %d\n",
962 				    prefixlen);
963 				exit(1);
964 			}
965 		}
966 		/*
967 		 * Just in case of funny setting of both prefix and netmask,
968 		 * prefix should override the netmask command.
969 		 */
970 		g_netmask_set = G_NETMASK_NIL;
971 		break;
972 	}
973 
974 	/*
975 	 * Check and see if any "netmask" command is used and perform the
976 	 * necessary operation.
977 	 */
978 	set_mask_lifreq(&lifr, &laddr, &netmask);
979 
980 	/* This check is temporary until libipadm supports IPMP interfaces. */
981 	if (ifconfig_use_libipadm(s, name)) {
982 		char	addrstr[INET6_ADDRSTRLEN];
983 
984 		if (af == AF_INET) {
985 			sin = (struct sockaddr_in *)&laddr;
986 			(void) inet_ntop(AF_INET, &sin->sin_addr, addrstr,
987 			    sizeof (addrstr));
988 		} else {
989 			sin6 = (struct sockaddr_in6 *)&laddr;
990 			(void) inet_ntop(AF_INET6, &sin6->sin6_addr, addrstr,
991 			    sizeof (addrstr));
992 		}
993 		istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name,
994 		    &ipaddr);
995 		if (istatus != IPADM_SUCCESS)
996 			ipadmerr_exit(istatus, "setifaddr");
997 
998 		/*
999 		 * lifr.lifr_addr, which is updated by set_mask_lifreq()
1000 		 * will contain the right mask to use.
1001 		 */
1002 		prefixlen = mask2plen(&lifr.lifr_addr);
1003 		(void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d",
1004 		    addrstr, prefixlen);
1005 
1006 		istatus = ipadm_set_addr(ipaddr, cidraddr, af);
1007 		if (istatus != IPADM_SUCCESS)
1008 			ipadmerr_exit(istatus, "could not set address");
1009 		/*
1010 		 * let parser know we got a source.
1011 		 * Next address, if given, should be dest
1012 		 */
1013 		setaddr++;
1014 
1015 		/*
1016 		 * address will be set by the parser after nextarg has
1017 		 * been scanned
1018 		 */
1019 		return (0);
1020 	}
1021 
1022 	/* Tell parser that an address was set */
1023 	setaddr++;
1024 	/* save copy of netmask to restore in case of error */
1025 	(void) strncpy(lifr1.lifr_name, name, sizeof (lifr1.lifr_name));
1026 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr1) < 0)
1027 		Perror0_exit("SIOCGLIFNETMASK");
1028 	sav_netmask = lifr1.lifr_addr;
1029 
1030 	/*
1031 	 * If setting the address and not the mask, clear any existing mask
1032 	 * and the kernel will then assign the default (netmask has been set
1033 	 * to 0 in this case).  If setting both (either by using a prefix or
1034 	 * using the netmask command), set the mask first, so the address will
1035 	 * be interpreted correctly.
1036 	 */
1037 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1038 	/* lifr.lifr_addr already contains netmask from set_mask_lifreq() */
1039 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
1040 		Perror0_exit("SIOCSLIFNETMASK");
1041 
1042 	if (debug) {
1043 		char abuf[INET6_ADDRSTRLEN];
1044 		void *addr = (afp->af_af == AF_INET) ?
1045 		    (void *)&((struct sockaddr_in *)&laddr)->sin_addr :
1046 		    (void *)&((struct sockaddr_in6 *)&laddr)->sin6_addr;
1047 
1048 		(void) printf("Setting %s af %d addr %s\n",
1049 		    lifr.lifr_name, afp->af_af,
1050 		    inet_ntop(afp->af_af, addr, abuf, sizeof (abuf)));
1051 	}
1052 	lifr.lifr_addr = laddr;
1053 	lifr.lifr_addr.ss_family = afp->af_af;
1054 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
1055 		/*
1056 		 * Restore the netmask
1057 		 */
1058 		int saverr = errno;
1059 
1060 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1061 		lifr.lifr_addr = sav_netmask;
1062 		(void) ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr);
1063 		errno = saverr;
1064 		Perror0_exit("SIOCSLIFADDR");
1065 	}
1066 
1067 	return (0);
1068 }
1069 
1070 /*
1071  * The following functions are stolen from the ipseckey(1m) program.
1072  * Perhaps they should be somewhere common, but for now, we just maintain
1073  * two versions.  We do this because of the different semantics for which
1074  * algorithms we select ("requested" for ifconfig vs. "actual" for key).
1075  */
1076 
1077 static ulong_t
1078 parsenum(char *num)
1079 {
1080 	ulong_t rc;
1081 	char *end = NULL;
1082 
1083 	errno = 0;
1084 	rc = strtoul(num, &end, 0);
1085 	if (errno != 0 || end == num || *end != '\0') {
1086 		rc = (ulong_t)-1;
1087 	}
1088 
1089 	return (rc);
1090 }
1091 
1092 /*
1093  * Parse and reverse parse possible algorithm values, include numbers.
1094  * Mostly stolen from ipseckey.c. See the comments above parsenum() for why
1095  * this isn't common to ipseckey.c.
1096  *
1097  * NOTE: Static buffer in this function for the return value.  Since ifconfig
1098  *       isn't multithreaded, this isn't a huge problem.
1099  */
1100 
1101 #define	NBUF_SIZE 20	/* Enough to print a large integer. */
1102 
1103 static char *
1104 rparsealg(uint8_t alg_value, int proto_num)
1105 {
1106 	struct ipsecalgent *alg;
1107 	static char numprint[128];	/* Enough to hold an algorithm name. */
1108 
1109 	/*
1110 	 * Special cases for "any" and "none"
1111 	 * The kernel needs to be able to distinguish between "any"
1112 	 * and "none" and the APIs are underdefined in this area for auth.
1113 	 */
1114 	if (proto_num == IPSEC_PROTO_AH) {
1115 		if (alg_value == SADB_AALG_NONE)
1116 			return ("none");
1117 		if (alg_value == SADB_AALG_ANY)
1118 			return ("any");
1119 	}
1120 
1121 	alg = getipsecalgbynum(alg_value, proto_num, NULL);
1122 	if (alg != NULL) {
1123 		(void) strlcpy(numprint, alg->a_names[0], sizeof (numprint));
1124 		freeipsecalgent(alg);
1125 	} else {
1126 		(void) snprintf(numprint, sizeof (numprint), "%d", alg_value);
1127 	}
1128 
1129 	return (numprint);
1130 }
1131 
1132 static uint_t
1133 parsealg(char *algname, int proto_num)
1134 {
1135 	struct ipsecalgent *alg;
1136 	ulong_t invalue;
1137 
1138 	if (algname == NULL) {
1139 		(void) fprintf(stderr, "ifconfig: Unexpected end of command "
1140 		    "line.\n");
1141 		exit(1);
1142 	}
1143 
1144 	/*
1145 	 * Special-case "none" and "any".
1146 	 * Use strcasecmp because its length is bounded.
1147 	 */
1148 	if (strcasecmp("none", algname) == 0) {
1149 		return ((proto_num == IPSEC_PROTO_ESP) ?
1150 		    NO_ESP_EALG : NO_ESP_AALG);
1151 	}
1152 	if ((strcasecmp("any", algname) == 0) && (proto_num == IPSEC_PROTO_AH))
1153 		return (SADB_AALG_ANY);
1154 
1155 	alg = getipsecalgbyname(algname, proto_num, NULL);
1156 	if (alg != NULL) {
1157 		invalue = alg->a_alg_num;
1158 		freeipsecalgent(alg);
1159 		return ((uint_t)invalue);
1160 	}
1161 
1162 	/*
1163 	 * Since algorithms can be loaded during kernel run-time, check for
1164 	 * numeric algorithm values too.
1165 	 */
1166 	invalue = parsenum(algname);
1167 	if ((invalue & (ulong_t)0xff) == invalue)
1168 		return ((uint_t)invalue);
1169 
1170 	(void) fprintf(stderr, "ifconfig: %s algorithm type %s unknown.\n",
1171 	    (proto_num == IPSEC_PROTO_ESP) ?
1172 	    "Encryption" : "Authentication", algname);
1173 	exit(1);
1174 	/* NOTREACHED */
1175 }
1176 
1177 /*
1178  * Actual ifconfig functions to set tunnel security properties.
1179  */
1180 
1181 enum ipsec_alg_type { ESP_ENCR_ALG = 1, ESP_AUTH_ALG, AH_AUTH_ALG };
1182 
1183 static int
1184 set_tun_algs(int which_alg, int alg)
1185 {
1186 	boolean_t	encr_alg_set = _B_FALSE;
1187 	iptun_params_t	params;
1188 	dladm_status_t	status;
1189 	ipsec_req_t	*ipsr;
1190 
1191 	if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN,
1192 	    &params.iptun_param_linkid)) != DLADM_STATUS_OK)
1193 		goto done;
1194 
1195 	status = dladm_iptun_getparams(dlh, &params, DLADM_OPT_ACTIVE);
1196 	if (status != DLADM_STATUS_OK)
1197 		goto done;
1198 
1199 	ipsr = &params.iptun_param_secinfo;
1200 
1201 	/*
1202 	 * If I'm just starting off this ifconfig, I want a clean slate,
1203 	 * otherwise, I've captured the current tunnel security settings.
1204 	 * In the case of continuation, I merely add to the settings.
1205 	 */
1206 	if (!(params.iptun_param_flags & IPTUN_PARAM_SECINFO))
1207 		(void) memset(ipsr, 0, sizeof (*ipsr));
1208 
1209 	/* We're only modifying the IPsec information */
1210 	params.iptun_param_flags = IPTUN_PARAM_SECINFO;
1211 
1212 	switch (which_alg) {
1213 	case ESP_ENCR_ALG:
1214 		if (alg == NO_ESP_EALG) {
1215 			if (ipsr->ipsr_esp_auth_alg == SADB_AALG_NONE)
1216 				ipsr->ipsr_esp_req = 0;
1217 			ipsr->ipsr_esp_alg = SADB_EALG_NONE;
1218 
1219 			/* Let the user specify NULL encryption implicitly. */
1220 			if (ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) {
1221 				encr_alg_set = _B_TRUE;
1222 				ipsr->ipsr_esp_alg = SADB_EALG_NULL;
1223 			}
1224 		} else {
1225 			encr_alg_set = _B_TRUE;
1226 			ipsr->ipsr_esp_req =
1227 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
1228 			ipsr->ipsr_esp_alg = alg;
1229 		}
1230 		break;
1231 	case ESP_AUTH_ALG:
1232 		if (alg == NO_ESP_AALG) {
1233 			if ((ipsr->ipsr_esp_alg == SADB_EALG_NONE ||
1234 			    ipsr->ipsr_esp_alg == SADB_EALG_NULL) &&
1235 			    !encr_alg_set)
1236 				ipsr->ipsr_esp_req = 0;
1237 			ipsr->ipsr_esp_auth_alg = SADB_AALG_NONE;
1238 		} else {
1239 			ipsr->ipsr_esp_req =
1240 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
1241 			ipsr->ipsr_esp_auth_alg = alg;
1242 
1243 			/* Let the user specify NULL encryption implicitly. */
1244 			if (ipsr->ipsr_esp_alg == SADB_EALG_NONE &&
1245 			    !encr_alg_set)
1246 				ipsr->ipsr_esp_alg = SADB_EALG_NULL;
1247 		}
1248 		break;
1249 	case AH_AUTH_ALG:
1250 		if (alg == NO_AH_AALG) {
1251 			ipsr->ipsr_ah_req = 0;
1252 			ipsr->ipsr_auth_alg = SADB_AALG_NONE;
1253 		} else {
1254 			ipsr->ipsr_ah_req =
1255 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
1256 			ipsr->ipsr_auth_alg = alg;
1257 		}
1258 		break;
1259 		/* Will never hit DEFAULT */
1260 	}
1261 
1262 	status = dladm_iptun_modify(dlh, &params, DLADM_OPT_ACTIVE);
1263 
1264 done:
1265 	if (status != DLADM_STATUS_OK)
1266 		dladmerr_exit(status, name);
1267 	else {
1268 		ipsec_policy_set = _B_TRUE;
1269 		if ((ipsr->ipsr_esp_req != 0 &&
1270 		    ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) ||
1271 		    (ipsr->ipsr_ah_req != 0 &&
1272 		    ipsr->ipsr_auth_alg != SADB_AALG_NONE))
1273 			ipsec_auth_covered = _B_TRUE;
1274 	}
1275 	return (0);
1276 }
1277 
1278 /* ARGSUSED */
1279 static int
1280 set_tun_esp_encr_alg(char *addr, int64_t param)
1281 {
1282 	return (set_tun_algs(ESP_ENCR_ALG,
1283 	    parsealg(addr, IPSEC_PROTO_ESP)));
1284 }
1285 
1286 /* ARGSUSED */
1287 static int
1288 set_tun_esp_auth_alg(char *addr, int64_t param)
1289 {
1290 	return (set_tun_algs(ESP_AUTH_ALG,
1291 	    parsealg(addr, IPSEC_PROTO_AH)));
1292 }
1293 
1294 /* ARGSUSED */
1295 static int
1296 set_tun_ah_alg(char *addr, int64_t param)
1297 {
1298 	return (set_tun_algs(AH_AUTH_ALG,
1299 	    parsealg(addr, IPSEC_PROTO_AH)));
1300 }
1301 
1302 /* ARGSUSED */
1303 static int
1304 setifrevarp(char *arg, int64_t param)
1305 {
1306 	struct sockaddr_in	laddr;
1307 
1308 	if (afp->af_af == AF_INET6) {
1309 		(void) fprintf(stderr,
1310 		    "ifconfig: revarp not possible on IPv6 interface %s\n",
1311 		    name);
1312 		exit(1);
1313 	}
1314 	if (doifrevarp(name, &laddr)) {
1315 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1316 		laddr.sin_family = AF_INET;
1317 		(void) memcpy(&lifr.lifr_addr, &laddr, sizeof (laddr));
1318 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1319 			Perror0_exit("SIOCSLIFADDR");
1320 	}
1321 	return (0);
1322 }
1323 
1324 /* ARGSUSED */
1325 static int
1326 setifsubnet(char *addr, int64_t param)
1327 {
1328 	int prefixlen = 0;
1329 	struct	sockaddr_storage subnet;
1330 
1331 	(*afp->af_getaddr)(addr, &subnet, &prefixlen);
1332 
1333 	switch (prefixlen) {
1334 	case NO_PREFIX:
1335 		(void) fprintf(stderr,
1336 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
1337 		exit(1);
1338 		/* NOTREACHED */
1339 	case BAD_ADDR:
1340 		(void) fprintf(stderr,
1341 		    "ifconfig: Bad prefix length in %s\n", addr);
1342 		exit(1);
1343 	default:
1344 		break;
1345 	}
1346 
1347 	lifr.lifr_addr = subnet;
1348 	lifr.lifr_addrlen = prefixlen;
1349 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1350 	if (ioctl(s, SIOCSLIFSUBNET, (caddr_t)&lifr) < 0)
1351 		Perror0_exit("SIOCSLIFSUBNET");
1352 
1353 	return (0);
1354 }
1355 
1356 /* ARGSUSED */
1357 static int
1358 setifnetmask(char *addr, int64_t param)
1359 {
1360 	struct sockaddr_in netmask;
1361 
1362 	assert(afp->af_af != AF_INET6);
1363 
1364 	if (strcmp(addr, "+") == 0) {
1365 		if (!in_getmask(&netmask, _B_FALSE))
1366 			return (0);
1367 		(void) printf("Setting netmask of %s to %s\n", name,
1368 		    inet_ntoa(netmask.sin_addr));
1369 	} else {
1370 		in_getaddr(addr, (struct sockaddr *)&netmask, NULL);
1371 	}
1372 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1373 	(void) memcpy(&lifr.lifr_addr, &netmask, sizeof (netmask));
1374 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
1375 		Perror0_exit("SIOCSLIFNETMASK");
1376 	return (0);
1377 }
1378 
1379 /*
1380  * Parse '/<n>' as a netmask.
1381  */
1382 /* ARGSUSED */
1383 static int
1384 setifprefixlen(char *addr, int64_t param)
1385 {
1386 	int prefixlen;
1387 	int af = afp->af_af;
1388 
1389 	prefixlen = in_getprefixlen(addr, _B_TRUE,
1390 	    (af == AF_INET) ? IP_ABITS : IPV6_ABITS);
1391 	if (prefixlen < 0) {
1392 		(void) fprintf(stderr,
1393 		    "ifconfig: Bad prefix length in %s\n", addr);
1394 		exit(1);
1395 	}
1396 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
1397 	lifr.lifr_addr.ss_family = af;
1398 	if (af == AF_INET6) {
1399 		struct sockaddr_in6 *sin6;
1400 
1401 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
1402 		if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
1403 		    (uchar_t *)&sin6->sin6_addr)) {
1404 			(void) fprintf(stderr, "ifconfig: "
1405 			    "Bad prefix length: %d\n",
1406 			    prefixlen);
1407 			exit(1);
1408 		}
1409 	} else if (af == AF_INET) {
1410 		struct sockaddr_in *sin;
1411 
1412 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
1413 		if (!in_prefixlentomask(prefixlen, IP_ABITS,
1414 		    (uchar_t *)&sin->sin_addr)) {
1415 			(void) fprintf(stderr, "ifconfig: "
1416 			    "Bad prefix length: %d\n",
1417 			    prefixlen);
1418 			exit(1);
1419 		}
1420 	} else {
1421 		(void) fprintf(stderr, "ifconfig: setting prefix only supported"
1422 		    " for address family inet or inet6\n");
1423 		exit(1);
1424 	}
1425 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1426 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
1427 		Perror0_exit("SIOCSLIFNETMASK");
1428 	return (0);
1429 }
1430 
1431 /* ARGSUSED */
1432 static int
1433 setifbroadaddr(char *addr, int64_t param)
1434 {
1435 	struct	sockaddr_in broadaddr;
1436 
1437 	assert(afp->af_af != AF_INET6);
1438 
1439 	if (strcmp(addr, "+") == 0) {
1440 		/*
1441 		 * This doesn't set the broadcast address at all. Rather, it
1442 		 * gets, then sets the interface's address, relying on the fact
1443 		 * that resetting the address will reset the broadcast address.
1444 		 */
1445 		(void) strncpy(lifr.lifr_name, name,
1446 		    sizeof (lifr.lifr_name));
1447 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
1448 			if (errno != EADDRNOTAVAIL)
1449 				Perror0_exit("SIOCGLIFADDR");
1450 			return (0);
1451 		}
1452 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1453 			Perror0_exit("SIOCGLIFADDR");
1454 
1455 		return (0);
1456 	}
1457 	in_getaddr(addr, (struct sockaddr *)&broadaddr, NULL);
1458 
1459 	(void) memcpy(&lifr.lifr_addr, &broadaddr, sizeof (broadaddr));
1460 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1461 	if (ioctl(s, SIOCSLIFBRDADDR, (caddr_t)&lifr) < 0)
1462 		Perror0_exit("SIOCSLIFBRDADDR");
1463 	return (0);
1464 }
1465 
1466 /*
1467  * set interface destination address
1468  */
1469 /* ARGSUSED */
1470 static int
1471 setifdstaddr(char *addr, int64_t param)
1472 {
1473 	(*afp->af_getaddr)(addr, (struct sockaddr *)&lifr.lifr_addr, NULL);
1474 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1475 	if (ioctl(s, SIOCSLIFDSTADDR, (caddr_t)&lifr) < 0)
1476 		Perror0_exit("setifdstaddr: SIOCSLIFDSTADDR");
1477 	return (0);
1478 }
1479 
1480 /* ARGSUSED */
1481 static int
1482 setifflags(char *val, int64_t value)
1483 {
1484 	struct lifreq lifrl;	/* local lifreq struct */
1485 	boolean_t bringup = _B_FALSE;
1486 
1487 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1488 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0)
1489 		Perror0_exit("setifflags: SIOCGLIFFLAGS");
1490 
1491 	if (value < 0) {
1492 		value = -value;
1493 
1494 		if ((value & IFF_NOFAILOVER) && (lifr.lifr_flags & IFF_UP)) {
1495 			/*
1496 			 * The kernel does not allow administratively up test
1497 			 * addresses to be converted to data addresses.  Bring
1498 			 * the address down first, then bring it up after it's
1499 			 * been converted to a data address.
1500 			 */
1501 			lifr.lifr_flags &= ~IFF_UP;
1502 			(void) ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr);
1503 			bringup = _B_TRUE;
1504 		}
1505 
1506 		lifr.lifr_flags &= ~value;
1507 		if ((value & (IFF_UP | IFF_NOFAILOVER)) &&
1508 		    (lifr.lifr_flags & IFF_DUPLICATE)) {
1509 			/*
1510 			 * If the user is trying to mark an interface with a
1511 			 * duplicate address as "down," or convert a duplicate
1512 			 * test address to a data address, then fetch the
1513 			 * address and set it.  This will cause IP to clear
1514 			 * the IFF_DUPLICATE flag and stop the automatic
1515 			 * recovery timer.
1516 			 */
1517 			value = lifr.lifr_flags;
1518 			if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0)
1519 				(void) ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr);
1520 			lifr.lifr_flags = value;
1521 		}
1522 	} else {
1523 		lifr.lifr_flags |= value;
1524 	}
1525 
1526 	/*
1527 	 * If we're about to bring up an underlying physical IPv6 interface in
1528 	 * an IPMP group, ensure the IPv6 IPMP interface is also up.  This is
1529 	 * for backward compatibility with legacy configurations in which
1530 	 * there are no explicit hostname files for IPMP interfaces.  (For
1531 	 * IPv4, this is automatically handled by the kernel when migrating
1532 	 * the underlying interface's data address to the IPMP interface.)
1533 	 */
1534 	(void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ);
1535 
1536 	if (lifnum(lifr.lifr_name) == 0 &&
1537 	    (lifr.lifr_flags & (IFF_UP|IFF_IPV6)) == (IFF_UP|IFF_IPV6) &&
1538 	    ioctl(s, SIOCGLIFGROUPNAME, &lifrl) == 0 &&
1539 	    lifrl.lifr_groupname[0] != '\0') {
1540 		lifgroupinfo_t lifgr;
1541 
1542 		(void) strlcpy(lifgr.gi_grname, lifrl.lifr_groupname,
1543 		    LIFGRNAMSIZ);
1544 		if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1)
1545 			Perror0_exit("setifflags: SIOCGLIFGROUPINFO");
1546 
1547 		(void) strlcpy(lifrl.lifr_name, lifgr.gi_grifname, LIFNAMSIZ);
1548 		if (ioctl(s, SIOCGLIFFLAGS, &lifrl) == -1)
1549 			Perror0_exit("setifflags: SIOCGLIFFLAGS");
1550 		if (!(lifrl.lifr_flags & IFF_UP)) {
1551 			lifrl.lifr_flags |= IFF_UP;
1552 			if (ioctl(s, SIOCSLIFFLAGS, &lifrl) == -1)
1553 				Perror0_exit("setifflags: SIOCSLIFFLAGS");
1554 		}
1555 	}
1556 
1557 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1558 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0)
1559 		Perror0_exit("setifflags: SIOCSLIFFLAGS");
1560 
1561 	if (bringup) {
1562 		lifr.lifr_flags |= IFF_UP;
1563 		if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0)
1564 			Perror0_exit("setifflags: SIOCSLIFFLAGS IFF_UP");
1565 	}
1566 
1567 	return (0);
1568 }
1569 
1570 /* ARGSUSED */
1571 static int
1572 setifmetric(char *val, int64_t param)
1573 {
1574 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1575 	lifr.lifr_metric = atoi(val);
1576 	if (ioctl(s, SIOCSLIFMETRIC, (caddr_t)&lifr) < 0)
1577 		Perror0_exit("setifmetric: SIOCSLIFMETRIC");
1578 	return (0);
1579 }
1580 
1581 /* ARGSUSED */
1582 static int
1583 setifmtu(char *val, int64_t param)
1584 {
1585 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1586 	lifr.lifr_mtu = atoi(val);
1587 	if (ioctl(s, SIOCSLIFMTU, (caddr_t)&lifr) < 0)
1588 		Perror0_exit("setifmtu: SIOCSLIFMTU");
1589 	return (0);
1590 }
1591 
1592 /* ARGSUSED */
1593 static int
1594 setifindex(char *val, int64_t param)
1595 {
1596 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1597 	lifr.lifr_index = atoi(val);
1598 	if (ioctl(s, SIOCSLIFINDEX, (caddr_t)&lifr) < 0)
1599 		Perror0_exit("setifindex: SIOCSLIFINDEX");
1600 	return (0);
1601 }
1602 
1603 /* ARGSUSED */
1604 static void
1605 notifycb(dlpi_handle_t dh, dlpi_notifyinfo_t *dnip, void *arg)
1606 {
1607 }
1608 
1609 /* ARGSUSED */
1610 static int
1611 setifether(char *addr, int64_t param)
1612 {
1613 	uchar_t		*hwaddr;
1614 	int		hwaddrlen;
1615 	int		retval;
1616 	ifaddrlistx_t	*ifaddrp, *ifaddrs = NULL;
1617 	dlpi_handle_t	dh;
1618 	dlpi_notifyid_t id;
1619 
1620 	if (addr == NULL) {
1621 		ifstatus(name);
1622 		print_ifether(name);
1623 		return (0);
1624 	}
1625 
1626 	/*
1627 	 * if the IP interface in the arguments is a logical
1628 	 * interface, exit with an error now.
1629 	 */
1630 	if (strchr(name, ':') != NULL) {
1631 		(void) fprintf(stderr, "ifconfig: cannot change"
1632 		    " ethernet address of a logical interface\n");
1633 		exit(1);
1634 	}
1635 
1636 	if ((hwaddr = _link_aton(addr, &hwaddrlen)) == NULL) {
1637 		if (hwaddrlen == -1)
1638 			(void) fprintf(stderr,
1639 			    "ifconfig: bad ethernet address\n");
1640 		else
1641 			(void) fprintf(stderr, "ifconfig: malloc() failed\n");
1642 		exit(1);
1643 	}
1644 
1645 	if ((retval = dlpi_open(name, &dh, 0)) != DLPI_SUCCESS)
1646 		Perrdlpi_exit("cannot dlpi_open() link", name, retval);
1647 
1648 	retval = dlpi_enabnotify(dh, DL_NOTE_PHYS_ADDR, notifycb, NULL, &id);
1649 	if (retval == DLPI_SUCCESS) {
1650 		(void) dlpi_disabnotify(dh, id, NULL);
1651 	} else {
1652 		/*
1653 		 * This link does not support DL_NOTE_PHYS_ADDR: bring down
1654 		 * all of the addresses to flush the old hardware address
1655 		 * information out of IP.
1656 		 *
1657 		 * NOTE: Skipping this when DL_NOTE_PHYS_ADDR is supported is
1658 		 * more than an optimization: in.mpathd will set IFF_OFFLINE
1659 		 * if it's notified and the new address is a duplicate of
1660 		 * another in the group -- but the flags manipulation in
1661 		 * ifaddr_{down,up}() cannot be atomic and thus might clobber
1662 		 * IFF_OFFLINE, confusing in.mpathd.
1663 		 */
1664 		if (ifaddrlistx(name, IFF_UP, 0, &ifaddrs) == -1)
1665 			Perror2_exit(name, "cannot get address list");
1666 
1667 		ifaddrp = ifaddrs;
1668 		for (; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
1669 			if (!ifaddr_down(ifaddrp)) {
1670 				Perror2_exit(ifaddrp->ia_name,
1671 				    "cannot bring down");
1672 			}
1673 		}
1674 	}
1675 
1676 	/*
1677 	 * Change the hardware address.
1678 	 */
1679 	retval = dlpi_set_physaddr(dh, DL_CURR_PHYS_ADDR, hwaddr, hwaddrlen);
1680 	if (retval != DLPI_SUCCESS) {
1681 		(void) fprintf(stderr,
1682 		    "ifconfig: failed setting mac address on %s\n", name);
1683 	}
1684 	dlpi_close(dh);
1685 
1686 	/*
1687 	 * If any addresses were brought down before changing the hardware
1688 	 * address, bring them up again.
1689 	 */
1690 	for (ifaddrp = ifaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
1691 		if (!ifaddr_up(ifaddrp))
1692 			Perror2_exit(ifaddrp->ia_name, "cannot bring up");
1693 	}
1694 	ifaddrlistx_free(ifaddrs);
1695 
1696 	return (0);
1697 }
1698 
1699 /*
1700  * Print an interface's Ethernet address, if it has one.
1701  */
1702 static void
1703 print_ifether(const char *ifname)
1704 {
1705 	int fd;
1706 
1707 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1708 
1709 	fd = socket(AF_INET, SOCK_DGRAM, 0);
1710 	if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
1711 		/*
1712 		 * It's possible the interface is only configured for
1713 		 * IPv6; check again with AF_INET6.
1714 		 */
1715 		(void) close(fd);
1716 		fd = socket(AF_INET6, SOCK_DGRAM, 0);
1717 		if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
1718 			(void) close(fd);
1719 			return;
1720 		}
1721 	}
1722 	(void) close(fd);
1723 
1724 	/* VNI and IPMP interfaces don't have MAC addresses */
1725 	if (lifr.lifr_flags & (IFF_VIRTUAL|IFF_IPMP))
1726 		return;
1727 
1728 	/* IP tunnels also don't have Ethernet-like MAC addresses */
1729 	if (ifconfig_dladm_open(ifname, DATALINK_CLASS_IPTUN, NULL) ==
1730 	    DLADM_STATUS_OK)
1731 		return;
1732 
1733 	dlpi_print_address(ifname);
1734 }
1735 
1736 /*
1737  * static int find_all_interfaces(struct lifconf *lifcp, char **buf,
1738  *     int64_t lifc_flags)
1739  *
1740  * It finds all active data links.
1741  *
1742  * It takes in input a pointer to struct lifconf to receive interfaces
1743  * informations, a **char to hold allocated buffer, and a lifc_flags.
1744  *
1745  * Return values:
1746  *  0 = everything OK
1747  * -1 = problem
1748  */
1749 static int
1750 find_all_interfaces(struct lifconf *lifcp, char **buf, int64_t lifc_flags)
1751 {
1752 	unsigned bufsize;
1753 	int n;
1754 	ni_t *nip;
1755 	struct lifreq *lifrp;
1756 	dladm_status_t status;
1757 
1758 	if (!dlh_opened) {
1759 		status = ifconfig_dladm_open(NULL, 0, NULL);
1760 		if (status != DLADM_STATUS_OK)
1761 			dladmerr_exit(status, "unable to open dladm handle");
1762 	}
1763 
1764 	(void) dlpi_walk(ni_entry, dlh, 0);
1765 
1766 	/* Now, translate the linked list into a struct lifreq buffer */
1767 	if (num_ni == 0) {
1768 		lifcp->lifc_family = AF_UNSPEC;
1769 		lifcp->lifc_flags = lifc_flags;
1770 		lifcp->lifc_len = 0;
1771 		lifcp->lifc_buf = NULL;
1772 		return (0);
1773 	}
1774 
1775 	bufsize = num_ni * sizeof (struct lifreq);
1776 	if ((*buf = malloc(bufsize)) == NULL)
1777 		Perror0_exit("find_all_interfaces: malloc failed");
1778 
1779 	lifcp->lifc_family = AF_UNSPEC;
1780 	lifcp->lifc_flags = lifc_flags;
1781 	lifcp->lifc_len = bufsize;
1782 	lifcp->lifc_buf = *buf;
1783 
1784 	for (n = 0, lifrp = lifcp->lifc_req; n < num_ni; n++, lifrp++) {
1785 		nip = ni_list;
1786 		(void) strncpy(lifrp->lifr_name, nip->ni_name,
1787 		    sizeof (lifr.lifr_name));
1788 		ni_list = nip->ni_next;
1789 		free(nip);
1790 	}
1791 	return (0);
1792 }
1793 
1794 /*
1795  * Create the next unused logical interface using the original name
1796  * and assign the address (and mask if '/<n>' is part of the address).
1797  * Use the new logical interface for subsequent subcommands by updating
1798  * the name variable.
1799  *
1800  * This allows syntax like:
1801  *	ifconfig le0 addif 109.106.86.130 netmask + up \
1802  *	addif 109.106.86.131 netmask + up
1803  */
1804 /* ARGSUSED */
1805 static int
1806 addif(char *str, int64_t param)
1807 {
1808 	int prefixlen = 0;
1809 	struct sockaddr_storage laddr;
1810 	struct sockaddr_storage mask;
1811 	ipadm_status_t istatus;
1812 	char cidraddr[BUFSIZ];
1813 
1814 	(void) strncpy(name, origname, sizeof (name));
1815 
1816 	if (strchr(name, ':') != NULL) {
1817 		(void) fprintf(stderr,
1818 		    "ifconfig: addif: bad physical interface name %s\n",
1819 		    name);
1820 		exit(1);
1821 	}
1822 
1823 	/*
1824 	 * clear so parser will interpret next address as source followed
1825 	 * by possible dest
1826 	 */
1827 	setaddr = 0;
1828 	(*afp->af_getaddr)(str, (struct sockaddr *)&laddr, &prefixlen);
1829 
1830 	switch (prefixlen) {
1831 	case NO_PREFIX:
1832 		/* Nothing there - ok */
1833 		break;
1834 	case BAD_ADDR:
1835 		(void) fprintf(stderr,
1836 		    "ifconfig: Bad prefix length in %s\n", str);
1837 		exit(1);
1838 	default:
1839 		(void) memset(&mask, 0, sizeof (mask));
1840 		mask.ss_family = afp->af_af;
1841 		if (afp->af_af == AF_INET6) {
1842 			struct sockaddr_in6 *sin6;
1843 			sin6 = (struct sockaddr_in6 *)&mask;
1844 			if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
1845 			    (uchar_t *)&sin6->sin6_addr)) {
1846 				(void) fprintf(stderr, "ifconfig: "
1847 				    "Bad prefix length: %d\n",
1848 				    prefixlen);
1849 				exit(1);
1850 			}
1851 		} else {
1852 			struct sockaddr_in *sin;
1853 
1854 			sin = (struct sockaddr_in *)&mask;
1855 			if (!in_prefixlentomask(prefixlen, IP_ABITS,
1856 			    (uchar_t *)&sin->sin_addr)) {
1857 				(void) fprintf(stderr, "ifconfig: "
1858 				    "Bad prefix length: %d\n",
1859 				    prefixlen);
1860 				exit(1);
1861 			}
1862 		}
1863 		g_netmask_set = G_NETMASK_NIL;
1864 		break;
1865 	}
1866 
1867 	/*
1868 	 * This is a "hack" to get around the problem of SIOCLIFADDIF.  The
1869 	 * problem is that this ioctl does not include the netmask when
1870 	 * adding a logical interface.  This is the same problem described
1871 	 * in the ifconfig() comments.  To get around this problem, we first
1872 	 * add the logical interface with a 0 address.  After that, we set
1873 	 * the netmask if provided.  Finally we set the interface address.
1874 	 */
1875 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1876 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
1877 
1878 	/* Note: no need to do DAD here since the interface isn't up yet. */
1879 
1880 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0)
1881 		Perror0_exit("addif: SIOCLIFADDIF");
1882 
1883 	(void) printf("Created new logical interface %s\n",
1884 	    lifr.lifr_name);
1885 	(void) strncpy(name, lifr.lifr_name, sizeof (name));
1886 
1887 	/*
1888 	 * Check and see if any "netmask" command is used and perform the
1889 	 * necessary operation.
1890 	 */
1891 	set_mask_lifreq(&lifr, &laddr, &mask);
1892 
1893 	/* This check is temporary until libipadm supports IPMP interfaces. */
1894 	if (ifconfig_use_libipadm(s, name)) {
1895 		/*
1896 		 * We added the logical interface above before calling
1897 		 * ipadm_create_addr(), because, with IPH_LEGACY, we need
1898 		 * to do an addif for `ifconfig ce0 addif <addr>' but not for
1899 		 * `ifconfig ce0 <addr>'. libipadm does not have a flag to
1900 		 * to differentiate between these two cases. To keep it simple,
1901 		 * we always create the logical interface and pass it to
1902 		 * libipadm instead of requiring libipadm to addif for some
1903 		 * cases and not do addif for other cases.
1904 		 */
1905 		istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name,
1906 		    &ipaddr);
1907 		if (istatus != IPADM_SUCCESS)
1908 			ipadmerr_exit(istatus, "addif");
1909 
1910 		if (strchr(str, '/') == NULL) {
1911 			/*
1912 			 * lifr.lifr_addr, which is updated by set_mask_lifreq()
1913 			 * will contain the right mask to use.
1914 			 */
1915 			prefixlen = mask2plen(&lifr.lifr_addr);
1916 			(void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d",
1917 			    str, prefixlen);
1918 			str = cidraddr;
1919 		}
1920 		istatus = ipadm_set_addr(ipaddr, str, af);
1921 		if (istatus != IPADM_SUCCESS)
1922 			ipadmerr_exit(istatus, "could not set address");
1923 		setaddr++;
1924 		/*
1925 		 * address will be set by the parser after nextarg
1926 		 * has been scanned
1927 		 */
1928 		return (0);
1929 	}
1930 
1931 	/*
1932 	 * Only set the netmask if "netmask" command is used or a prefix is
1933 	 * provided.
1934 	 */
1935 	if (g_netmask_set == G_NETMASK_SET || prefixlen >= 0) {
1936 		/*
1937 		 * lifr.lifr_addr already contains netmask from
1938 		 * set_mask_lifreq().
1939 		 */
1940 		if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
1941 			Perror0_exit("addif: SIOCSLIFNETMASK");
1942 	}
1943 
1944 	/* Finally, we set the interface address. */
1945 	lifr.lifr_addr = laddr;
1946 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1947 		Perror0_exit("SIOCSLIFADDR");
1948 
1949 	/*
1950 	 * let parser know we got a source.
1951 	 * Next address, if given, should be dest
1952 	 */
1953 	setaddr++;
1954 	return (0);
1955 }
1956 
1957 /*
1958  * Remove a logical interface based on its IP address. Unlike addif
1959  * there is no '/<n>' here.
1960  * Verifies that the interface is down before it is removed.
1961  */
1962 /* ARGSUSED */
1963 static int
1964 removeif(char *str, int64_t param)
1965 {
1966 	struct sockaddr_storage laddr;
1967 	ipadm_status_t istatus;
1968 	ipadm_addr_info_t *ainfo, *ainfop;
1969 
1970 	if (strchr(name, ':') != NULL) {
1971 		(void) fprintf(stderr,
1972 		    "ifconfig: removeif: bad physical interface name %s\n",
1973 		    name);
1974 		exit(1);
1975 	}
1976 
1977 	(*afp->af_getaddr)(str, &laddr, NULL);
1978 
1979 	/*
1980 	 * Following check is temporary until libipadm supports
1981 	 * IPMP interfaces.
1982 	 */
1983 	if (!ifconfig_use_libipadm(s, name))
1984 		goto delete;
1985 
1986 	/*
1987 	 * Get all addresses and search this address among the active
1988 	 * addresses. If an address object was found, delete using
1989 	 * ipadm_delete_addr().
1990 	 */
1991 	istatus = ipadm_addr_info(iph, name, &ainfo, 0, LIFC_DEFAULT);
1992 	if (istatus != IPADM_SUCCESS)
1993 		ipadmerr_exit(istatus, "removeif");
1994 
1995 	for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop))
1996 		if (sockaddrcmp(ainfop->ia_ifa.ifa_addr, &laddr))
1997 			break;
1998 
1999 	if (ainfop != NULL && ainfop->ia_aobjname[0] != '\0') {
2000 		istatus = ipadm_delete_addr(iph, ainfop->ia_aobjname,
2001 		    IPADM_OPT_ACTIVE);
2002 		if (istatus != IPADM_SUCCESS)
2003 			ipadmerr_exit(istatus, "could not delete address");
2004 		ipadm_free_addr_info(ainfo);
2005 		return (0);
2006 	}
2007 	ipadm_free_addr_info(ainfo);
2008 
2009 delete:
2010 	/*
2011 	 * An address object for this address was not found in ipadm.
2012 	 * Delete with SIOCLIFREMOVEIF.
2013 	 */
2014 	lifr.lifr_addr = laddr;
2015 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2016 	if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) {
2017 		if (errno == EBUSY) {
2018 			/* This can only happen if ipif_id = 0 */
2019 			(void) fprintf(stderr,
2020 			    "ifconfig: removeif: can't remove interface: %s\n",
2021 			    name);
2022 			exit(1);
2023 		}
2024 		Perror0_exit("removeif: SIOCLIFREMOVEIF");
2025 	}
2026 	return (0);
2027 }
2028 
2029 /*
2030  * Set the address token for IPv6.
2031  */
2032 /* ARGSUSED */
2033 static int
2034 setiftoken(char *addr, int64_t param)
2035 {
2036 	int prefixlen = 0;
2037 	struct sockaddr_in6 token;
2038 
2039 	in6_getaddr(addr, (struct sockaddr *)&token, &prefixlen);
2040 	switch (prefixlen) {
2041 	case NO_PREFIX:
2042 		(void) fprintf(stderr,
2043 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
2044 		exit(1);
2045 		/* NOTREACHED */
2046 	case BAD_ADDR:
2047 		(void) fprintf(stderr,
2048 		    "ifconfig: Bad prefix length in %s\n", addr);
2049 		exit(1);
2050 	default:
2051 		break;
2052 	}
2053 	(void) memcpy(&lifr.lifr_addr, &token, sizeof (token));
2054 	lifr.lifr_addrlen = prefixlen;
2055 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2056 	if (ioctl(s, SIOCSLIFTOKEN, (caddr_t)&lifr) < 0)  {
2057 		Perror0_exit("setiftoken: SIOCSLIFTOKEN");
2058 	}
2059 	return (0);
2060 }
2061 
2062 /* ARGSUSED */
2063 static int
2064 setifgroupname(char *grname, int64_t param)
2065 {
2066 	lifgroupinfo_t		lifgr;
2067 	struct lifreq		lifrl;
2068 	ifaddrlistx_t		*ifaddrp, *nextifaddrp;
2069 	ifaddrlistx_t		*ifaddrs = NULL, *downaddrs = NULL;
2070 	int			af;
2071 
2072 	if (debug) {
2073 		(void) printf("Setting groupname %s on interface %s\n",
2074 		    grname, name);
2075 	}
2076 
2077 	(void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ);
2078 	(void) strlcpy(lifrl.lifr_groupname, grname, LIFGRNAMSIZ);
2079 
2080 	while (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) {
2081 		switch (errno) {
2082 		case ENOENT:
2083 			/*
2084 			 * The group doesn't yet exist; create it and repeat.
2085 			 */
2086 			af = afp->af_af;
2087 			if (create_ipmp(grname, af, NULL, _B_TRUE) == -1) {
2088 				if (errno == EEXIST)
2089 					continue;
2090 
2091 				Perror2(grname, "cannot create IPMP group");
2092 				goto fail;
2093 			}
2094 			continue;
2095 
2096 		case EALREADY:
2097 			/*
2098 			 * The interface is already in another group; must
2099 			 * remove existing membership first.
2100 			 */
2101 			lifrl.lifr_groupname[0] = '\0';
2102 			if (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) {
2103 				Perror2(name, "cannot remove existing "
2104 				    "IPMP group membership");
2105 				goto fail;
2106 			}
2107 			(void) strlcpy(lifrl.lifr_groupname, grname,
2108 			    LIFGRNAMSIZ);
2109 			continue;
2110 
2111 		case EAFNOSUPPORT:
2112 			/*
2113 			 * The group exists, but it's not configured with the
2114 			 * address families the interface needs.  Since only
2115 			 * two address families are currently supported, just
2116 			 * configure the "other" address family.  Note that we
2117 			 * may race with group deletion or creation by another
2118 			 * process (ENOENT or EEXIST); in such cases we repeat
2119 			 * our original SIOCSLIFGROUPNAME.
2120 			 */
2121 			(void) strlcpy(lifgr.gi_grname, grname, LIFGRNAMSIZ);
2122 			if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1) {
2123 				if (errno == ENOENT)
2124 					continue;
2125 
2126 				Perror2(grname, "SIOCGLIFGROUPINFO");
2127 				goto fail;
2128 			}
2129 
2130 			af = lifgr.gi_v4 ? AF_INET6 : AF_INET;
2131 			if (create_ipmp(grname, af, lifgr.gi_grifname,
2132 			    _B_TRUE) == -1) {
2133 				if (errno == EEXIST)
2134 					continue;
2135 
2136 				Perror2(grname, "cannot configure IPMP group");
2137 				goto fail;
2138 			}
2139 			continue;
2140 
2141 		case EADDRINUSE:
2142 			/*
2143 			 * Some addresses are in-use (or under control of DAD).
2144 			 * Bring them down and retry the group join operation.
2145 			 * We will bring them back up after the interface has
2146 			 * been placed in the group.
2147 			 */
2148 			if (ifaddrlistx(lifrl.lifr_name, IFF_UP|IFF_DUPLICATE,
2149 			    0, &ifaddrs) == -1) {
2150 				Perror2(grname, "cannot get address list");
2151 				goto fail;
2152 			}
2153 
2154 			ifaddrp = ifaddrs;
2155 			for (; ifaddrp != NULL; ifaddrp = nextifaddrp) {
2156 				if (!ifaddr_down(ifaddrp)) {
2157 					ifaddrs = ifaddrp;
2158 					goto fail;
2159 				}
2160 				nextifaddrp = ifaddrp->ia_next;
2161 				ifaddrp->ia_next = downaddrs;
2162 				downaddrs = ifaddrp;
2163 			}
2164 			ifaddrs = NULL;
2165 			continue;
2166 
2167 		case EADDRNOTAVAIL: {
2168 			/*
2169 			 * Some data addresses are under application control.
2170 			 * For some of these (e.g., ADDRCONF), the application
2171 			 * should remove the address, in which case we retry a
2172 			 * few times (since the application's action is not
2173 			 * atomic with respect to us) before bailing out and
2174 			 * informing the user.
2175 			 */
2176 			int ntries, nappaddr = 0;
2177 			const if_appflags_t *iap = if_appflags_tbl;
2178 
2179 			for (; iap->ia_app != NULL; iap++) {
2180 				ntries = 0;
2181 again:
2182 				if (ifaddrlistx(lifrl.lifr_name, iap->ia_flag,
2183 				    IFF_NOFAILOVER, &ifaddrs) == -1) {
2184 					(void) fprintf(stderr, "ifconfig: %s: "
2185 					    "cannot get data addresses managed "
2186 					    "by %s\n", lifrl.lifr_name,
2187 					    iap->ia_app);
2188 					goto fail;
2189 				}
2190 
2191 				if (ifaddrs == NULL)
2192 					continue;
2193 
2194 				ifaddrlistx_free(ifaddrs);
2195 				ifaddrs = NULL;
2196 
2197 				if (++ntries < iap->ia_tries) {
2198 					(void) poll(NULL, 0, 100);
2199 					goto again;
2200 				}
2201 
2202 				(void) fprintf(stderr, "ifconfig: cannot join "
2203 				    "IPMP group: %s has data addresses managed "
2204 				    "by %s\n", lifrl.lifr_name, iap->ia_app);
2205 				nappaddr++;
2206 			}
2207 			if (nappaddr > 0)
2208 				goto fail;
2209 			continue;
2210 		}
2211 		default:
2212 			Perror2(name, "SIOCSLIFGROUPNAME");
2213 			goto fail;
2214 		}
2215 	}
2216 
2217 	/*
2218 	 * If the interface being moved is under the control of `ipmgmtd(1M)'
2219 	 * dameon then we should inform the daemon about this move, so that
2220 	 * the daemon can delete the state associated with this interface.
2221 	 *
2222 	 * This workaround is needed until the IPMP support in ipadm(1M).
2223 	 */
2224 	ipadm_if_move(iph, name);
2225 
2226 	/*
2227 	 * If there were addresses that we had to bring down, it's time to
2228 	 * bring them up again.  As part of bringing them up, the kernel will
2229 	 * automatically move them to the new IPMP interface.
2230 	 */
2231 	for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
2232 		if (!ifaddr_up(ifaddrp) && errno != ENXIO) {
2233 			(void) fprintf(stderr, "ifconfig: cannot bring back up "
2234 			    "%s: %s\n", ifaddrp->ia_name, strerror(errno));
2235 		}
2236 	}
2237 	ifaddrlistx_free(downaddrs);
2238 	return (0);
2239 fail:
2240 	/*
2241 	 * Attempt to bring back up any interfaces that we downed.
2242 	 */
2243 	for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
2244 		if (!ifaddr_up(ifaddrp) && errno != ENXIO) {
2245 			(void) fprintf(stderr, "ifconfig: cannot bring back up "
2246 			    "%s: %s\n", ifaddrp->ia_name, strerror(errno));
2247 		}
2248 	}
2249 	ifaddrlistx_free(downaddrs);
2250 	ifaddrlistx_free(ifaddrs);
2251 
2252 	/*
2253 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2254 	 * into the exit status, so we're forced to explicitly exit().
2255 	 */
2256 	exit(1);
2257 	/* NOTREACHED */
2258 }
2259 
2260 static boolean_t
2261 modcheck(const char *ifname)
2262 {
2263 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2264 
2265 	if (ioctl(s, SIOCGLIFFLAGS, &lifr) < 0) {
2266 		Perror0("SIOCGLIFFLAGS");
2267 		return (_B_FALSE);
2268 	}
2269 
2270 	if (lifr.lifr_flags & IFF_IPMP) {
2271 		(void) fprintf(stderr, "ifconfig: %s: module operations not"
2272 		    " supported on IPMP interfaces\n", ifname);
2273 		return (_B_FALSE);
2274 	}
2275 	if (lifr.lifr_flags & IFF_VIRTUAL) {
2276 		(void) fprintf(stderr, "ifconfig: %s: module operations not"
2277 		    " supported on virtual IP interfaces\n", ifname);
2278 		return (_B_FALSE);
2279 	}
2280 	return (_B_TRUE);
2281 }
2282 
2283 /*
2284  * To list all the modules above a given network interface.
2285  */
2286 /* ARGSUSED */
2287 static int
2288 modlist(char *null, int64_t param)
2289 {
2290 	int muxid_fd;
2291 	int muxfd;
2292 	int ipfd_lowstr;
2293 	int arpfd_lowstr;
2294 	int num_mods;
2295 	int i;
2296 	struct str_list strlist;
2297 	int orig_arpid;
2298 
2299 	/*
2300 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2301 	 * into the exit status, so we're forced to explicitly exit().
2302 	 */
2303 	if (!modcheck(name))
2304 		exit(1);
2305 
2306 	if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
2307 	    &orig_arpid) < 0) {
2308 		return (-1);
2309 	}
2310 	if ((num_mods = ioctl(ipfd_lowstr, I_LIST, NULL)) < 0) {
2311 		Perror0("cannot I_LIST to get the number of modules");
2312 	} else {
2313 		if (debug > 0) {
2314 			(void) printf("Listing (%d) modules above %s\n",
2315 			    num_mods, name);
2316 		}
2317 
2318 		strlist.sl_nmods = num_mods;
2319 		strlist.sl_modlist = malloc(sizeof (struct str_mlist) *
2320 		    num_mods);
2321 		if (strlist.sl_modlist == NULL) {
2322 			Perror0("cannot malloc");
2323 		} else {
2324 			if (ioctl(ipfd_lowstr, I_LIST, (caddr_t)&strlist) < 0) {
2325 				Perror0("cannot I_LIST for module names");
2326 			} else {
2327 				for (i = 0; i < strlist.sl_nmods; i++) {
2328 					(void) printf("%d %s\n", i,
2329 					    strlist.sl_modlist[i].l_name);
2330 				}
2331 			}
2332 			free(strlist.sl_modlist);
2333 		}
2334 	}
2335 	return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2336 	    orig_arpid));
2337 }
2338 
2339 #define	MODINSERT_OP	'i'
2340 #define	MODREMOVE_OP	'r'
2341 
2342 /*
2343  * To insert a module to the stream of the interface.  It is just a
2344  * wrapper.  The real function is modop().
2345  */
2346 /* ARGSUSED */
2347 static int
2348 modinsert(char *arg, int64_t param)
2349 {
2350 	return (modop(arg, MODINSERT_OP));
2351 }
2352 
2353 /*
2354  * To remove a module from the stream of the interface.  It is just a
2355  * wrapper.  The real function is modop().
2356  */
2357 /* ARGSUSED */
2358 static int
2359 modremove(char *arg, int64_t param)
2360 {
2361 	return (modop(arg, MODREMOVE_OP));
2362 }
2363 
2364 /*
2365  * Helper function for mod*() functions.  It gets a fd to the lower IP
2366  * stream and I_PUNLINK's the lower stream.  It also initializes the
2367  * global variable lifr.
2368  *
2369  * Param:
2370  *	int *muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2371  *	int *muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2372  *	int *ipfd_lowstr: fd to the lower IP stream.
2373  *	int *arpfd_lowstr: fd to the lower ARP stream.
2374  *
2375  * Return:
2376  *	-1 if operation fails, 0 otherwise.
2377  *
2378  * Please see the big block comment above ifplumb() for the logic of the
2379  * PLINK/PUNLINK
2380  */
2381 static int
2382 ip_domux2fd(int *muxfd, int *muxid_fd, int *ipfd_lowstr, int *arpfd_lowstr,
2383     int *orig_arpid)
2384 {
2385 	uint64_t	flags;
2386 	char		*udp_dev_name;
2387 
2388 	*orig_arpid = 0;
2389 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2390 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2391 		Perror0_exit("status: SIOCGLIFFLAGS");
2392 	}
2393 	flags = lifr.lifr_flags;
2394 	if (flags & IFF_IPV4) {
2395 		udp_dev_name = UDP_DEV_NAME;
2396 	} else if (flags & IFF_IPV6) {
2397 		udp_dev_name = UDP6_DEV_NAME;
2398 	} else {
2399 		return (-1);
2400 	}
2401 
2402 	if ((*muxid_fd = open(udp_dev_name, O_RDWR)) < 0) {
2403 		Perror2("open", udp_dev_name);
2404 		return (-1);
2405 	}
2406 	if (ioctl(*muxid_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
2407 		Perror2("SIOCGLIFMUXID", udp_dev_name);
2408 		return (-1);
2409 	}
2410 	if (debug > 0) {
2411 		(void) printf("ARP_muxid %d IP_muxid %d\n",
2412 		    lifr.lifr_arp_muxid, lifr.lifr_ip_muxid);
2413 	}
2414 
2415 	/*
2416 	 * Use /dev/udp{,6} as the mux to avoid linkcycles.
2417 	 */
2418 	if (ipadm_open_arp_on_udp(udp_dev_name, muxfd) != IPADM_SUCCESS)
2419 		return (-1);
2420 
2421 	if (lifr.lifr_arp_muxid != 0) {
2422 		if ((*arpfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
2423 		    lifr.lifr_arp_muxid)) < 0) {
2424 			if ((errno == EINVAL) &&
2425 			    (flags & (IFF_NOARP | IFF_IPV6))) {
2426 				/*
2427 				 * Some plumbing utilities set the muxid to
2428 				 * -1 or some invalid value to signify that
2429 				 * there is no arp stream. Set the muxid to 0
2430 				 * before trying to unplumb the IP stream.
2431 				 * IP does not allow the IP stream to be
2432 				 * unplumbed if it sees a non-null arp muxid,
2433 				 * for consistency of IP-ARP streams.
2434 				 */
2435 				*orig_arpid = lifr.lifr_arp_muxid;
2436 				lifr.lifr_arp_muxid = 0;
2437 				(void) ioctl(*muxid_fd, SIOCSLIFMUXID,
2438 				    (caddr_t)&lifr);
2439 				*arpfd_lowstr = -1;
2440 			} else {
2441 				Perror0("_I_MUXID2FD");
2442 				return (-1);
2443 			}
2444 		} else if (ioctl(*muxfd, I_PUNLINK,
2445 		    lifr.lifr_arp_muxid) < 0) {
2446 			Perror2("I_PUNLINK", udp_dev_name);
2447 			return (-1);
2448 		}
2449 	} else {
2450 		*arpfd_lowstr = -1;
2451 	}
2452 
2453 	if ((*ipfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
2454 	    lifr.lifr_ip_muxid)) < 0) {
2455 		Perror0("_I_MUXID2FD");
2456 		/* Undo any changes we made */
2457 		if (*orig_arpid != 0) {
2458 			lifr.lifr_arp_muxid = *orig_arpid;
2459 			(void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
2460 		}
2461 		return (-1);
2462 	}
2463 	if (ioctl(*muxfd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) {
2464 		Perror2("I_PUNLINK", udp_dev_name);
2465 		/* Undo any changes we made */
2466 		if (*orig_arpid != 0) {
2467 			lifr.lifr_arp_muxid = *orig_arpid;
2468 			(void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
2469 		}
2470 		return (-1);
2471 	}
2472 	return (0);
2473 }
2474 
2475 /*
2476  * Helper function for mod*() functions.  It I_PLINK's back the upper and
2477  * lower IP streams.  Note that this function must be called after
2478  * ip_domux2fd().  In ip_domux2fd(), the global variable lifr is initialized
2479  * and ip_plink() needs information in lifr.  So ip_domux2fd() and ip_plink()
2480  * must be called in pairs.
2481  *
2482  * Param:
2483  *	int muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2484  *	int muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2485  *	int ipfd_lowstr: fd to the lower IP stream.
2486  *	int arpfd_lowstr: fd to the lower ARP stream.
2487  *
2488  * Return:
2489  *	-1 if operation fails, 0 otherwise.
2490  *
2491  * Please see the big block comment above ifplumb() for the logic of the
2492  * PLINK/PUNLINK
2493  */
2494 static int
2495 ip_plink(int muxfd, int muxid_fd, int ipfd_lowstr, int arpfd_lowstr,
2496     int orig_arpid)
2497 {
2498 	int ip_muxid;
2499 
2500 	ip_muxid = ioctl(muxfd, I_PLINK, ipfd_lowstr);
2501 	if (ip_muxid < 0) {
2502 		Perror2("I_PLINK", UDP_DEV_NAME);
2503 		return (-1);
2504 	}
2505 
2506 	/*
2507 	 * If there is an arp stream, plink it. If there is no
2508 	 * arp stream, then it is possible that the plumbing
2509 	 * utility could have stored any value in the arp_muxid.
2510 	 * If so, restore it from orig_arpid.
2511 	 */
2512 	if (arpfd_lowstr != -1) {
2513 		if (ioctl(muxfd, I_PLINK, arpfd_lowstr) < 0) {
2514 			Perror2("I_PLINK", UDP_DEV_NAME);
2515 			return (-1);
2516 		}
2517 	} else if (orig_arpid != 0) {
2518 		/* Undo the changes we did in ip_domux2fd */
2519 		lifr.lifr_arp_muxid = orig_arpid;
2520 		lifr.lifr_ip_muxid = ip_muxid;
2521 		(void) ioctl(muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
2522 	}
2523 
2524 	(void) close(muxfd);
2525 	(void) close(muxid_fd);
2526 	return (0);
2527 }
2528 
2529 /*
2530  * The real function to perform module insertion/removal.
2531  *
2532  * Param:
2533  *	char *arg: the argument string module_name@position
2534  *	char op: operation, either MODINSERT_OP or MODREMOVE_OP.
2535  *
2536  * Return:
2537  *	Before doing ip_domux2fd(), this function calls exit(1) in case of
2538  *	error.  After ip_domux2fd() is done, it returns -1 for error, 0
2539  *	otherwise.
2540  */
2541 static int
2542 modop(char *arg, char op)
2543 {
2544 	char *pos_p;
2545 	int muxfd;
2546 	int muxid_fd;
2547 	int ipfd_lowstr;  /* IP stream (lower stream of mux) to be plinked */
2548 	int arpfd_lowstr; /* ARP stream (lower stream of mux) to be plinked */
2549 	struct strmodconf mod;
2550 	char *at_char = "@";
2551 	char *arg_str;
2552 	int orig_arpid;
2553 
2554 	/*
2555 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2556 	 * into the exit status, so we're forced to explicitly exit().
2557 	 */
2558 	if (!modcheck(name))
2559 		exit(1);
2560 
2561 	/* Need to save the original string for -a option. */
2562 	if ((arg_str = malloc(strlen(arg) + 1)) == NULL) {
2563 		Perror0("cannot malloc");
2564 		return (-1);
2565 	}
2566 	(void) strcpy(arg_str, arg);
2567 
2568 	if (*arg_str == *at_char) {
2569 		(void) fprintf(stderr,
2570 		    "ifconfig: must supply a module name\n");
2571 		exit(1);
2572 	}
2573 	mod.mod_name = strtok(arg_str, at_char);
2574 	if (strlen(mod.mod_name) > FMNAMESZ) {
2575 		(void) fprintf(stderr, "ifconfig: module name too long: %s\n",
2576 		    mod.mod_name);
2577 		exit(1);
2578 	}
2579 
2580 	/*
2581 	 * Need to make sure that the core TCP/IP stack modules are not
2582 	 * removed.  Otherwise, "bad" things can happen.  If a module
2583 	 * is removed and inserted back, it loses its old state.  But
2584 	 * the modules above it still have the old state.  E.g. IP assumes
2585 	 * fast data path while tunnel after re-inserted assumes that it can
2586 	 * receive M_DATA only in fast data path for which it does not have
2587 	 * any state.  This is a general caveat of _I_REMOVE/_I_INSERT.
2588 	 */
2589 	if (op == MODREMOVE_OP &&
2590 	    (strcmp(mod.mod_name, ARP_MOD_NAME) == 0 ||
2591 	    strcmp(mod.mod_name, IP_MOD_NAME) == 0)) {
2592 		(void) fprintf(stderr, "ifconfig: cannot remove %s\n",
2593 		    mod.mod_name);
2594 		exit(1);
2595 	}
2596 
2597 	if ((pos_p = strtok(NULL, at_char)) == NULL) {
2598 		(void) fprintf(stderr, "ifconfig: must supply a position\n");
2599 		exit(1);
2600 	}
2601 	mod.pos = atoi(pos_p);
2602 
2603 	if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
2604 	    &orig_arpid) < 0) {
2605 		free(arg_str);
2606 		return (-1);
2607 	}
2608 	switch (op) {
2609 	case MODINSERT_OP:
2610 		if (debug > 0) {
2611 			(void) printf("Inserting module %s at %d\n",
2612 			    mod.mod_name, mod.pos);
2613 		}
2614 		if (ioctl(ipfd_lowstr, _I_INSERT, (caddr_t)&mod) < 0) {
2615 			Perror2("fail to insert module", mod.mod_name);
2616 		}
2617 		break;
2618 	case MODREMOVE_OP:
2619 		if (debug > 0) {
2620 			(void) printf("Removing module %s at %d\n",
2621 			    mod.mod_name, mod.pos);
2622 		}
2623 		if (ioctl(ipfd_lowstr, _I_REMOVE, (caddr_t)&mod) < 0) {
2624 			Perror2("fail to remove module", mod.mod_name);
2625 		}
2626 		break;
2627 	default:
2628 		/* Should never get to here. */
2629 		(void) fprintf(stderr, "Unknown operation\n");
2630 		break;
2631 	}
2632 	free(arg_str);
2633 	return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2634 	    orig_arpid));
2635 }
2636 
2637 static int
2638 modify_tun(iptun_params_t *params)
2639 {
2640 	dladm_status_t status;
2641 
2642 	if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN,
2643 	    &params->iptun_param_linkid)) == DLADM_STATUS_OK)
2644 		status = dladm_iptun_modify(dlh, params, DLADM_OPT_ACTIVE);
2645 	if (status != DLADM_STATUS_OK)
2646 		dladmerr_exit(status, name);
2647 	return (0);
2648 }
2649 
2650 /*
2651  * Set tunnel source address
2652  */
2653 /* ARGSUSED */
2654 static int
2655 setiftsrc(char *addr, int64_t param)
2656 {
2657 	iptun_params_t params;
2658 
2659 	params.iptun_param_flags = IPTUN_PARAM_LADDR;
2660 	(void) strlcpy(params.iptun_param_laddr, addr,
2661 	    sizeof (params.iptun_param_laddr));
2662 	return (modify_tun(&params));
2663 }
2664 
2665 /*
2666  * Set tunnel destination address
2667  */
2668 /* ARGSUSED */
2669 static int
2670 setiftdst(char *addr, int64_t param)
2671 {
2672 	iptun_params_t params;
2673 
2674 	params.iptun_param_flags = IPTUN_PARAM_RADDR;
2675 	(void) strlcpy(params.iptun_param_raddr, addr,
2676 	    sizeof (params.iptun_param_raddr));
2677 	return (modify_tun(&params));
2678 }
2679 
2680 static int
2681 set_tun_prop(const char *propname, char *value)
2682 {
2683 	dladm_status_t	status;
2684 	datalink_id_t	linkid;
2685 
2686 	status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid);
2687 	if (status == DLADM_STATUS_OK) {
2688 		status = dladm_set_linkprop(dlh, linkid, propname, &value, 1,
2689 		    DLADM_OPT_ACTIVE);
2690 	}
2691 	if (status != DLADM_STATUS_OK)
2692 		dladmerr_exit(status, name);
2693 	return (0);
2694 }
2695 
2696 /* Set tunnel encapsulation limit. */
2697 /* ARGSUSED */
2698 static int
2699 set_tun_encap_limit(char *arg, int64_t param)
2700 {
2701 	return (set_tun_prop("encaplimit", arg));
2702 }
2703 
2704 /* Disable encapsulation limit. */
2705 /* ARGSUSED */
2706 static int
2707 clr_tun_encap_limit(char *arg, int64_t param)
2708 {
2709 	return (set_tun_encap_limit("-1", 0));
2710 }
2711 
2712 /* Set tunnel hop limit. */
2713 /* ARGSUSED */
2714 static int
2715 set_tun_hop_limit(char *arg, int64_t param)
2716 {
2717 	return (set_tun_prop("hoplimit", arg));
2718 }
2719 
2720 /* Set zone ID */
2721 static int
2722 setzone(char *arg, int64_t param)
2723 {
2724 	zoneid_t zoneid = GLOBAL_ZONEID;
2725 
2726 	if (param == NEXTARG) {
2727 		/* zone must be active */
2728 		if ((zoneid = getzoneidbyname(arg)) == -1) {
2729 			(void) fprintf(stderr,
2730 			    "ifconfig: unknown zone '%s'\n", arg);
2731 			exit(1);
2732 		}
2733 	}
2734 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2735 	lifr.lifr_zoneid = zoneid;
2736 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
2737 		Perror0_exit("SIOCSLIFZONE");
2738 	return (0);
2739 }
2740 
2741 /* Put interface into all zones */
2742 /* ARGSUSED */
2743 static int
2744 setallzones(char *arg, int64_t param)
2745 {
2746 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2747 	lifr.lifr_zoneid = ALL_ZONES;
2748 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
2749 		Perror0_exit("SIOCSLIFZONE");
2750 	return (0);
2751 }
2752 
2753 /* Set source address to use */
2754 /* ARGSUSED */
2755 static int
2756 setifsrc(char *arg, int64_t param)
2757 {
2758 	uint_t ifindex = 0;
2759 	int rval;
2760 
2761 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2762 
2763 	/*
2764 	 * Argument can be either an interface name or "none". The latter means
2765 	 * that any previous selection is cleared.
2766 	 */
2767 
2768 	if (strchr(arg, ':') != NULL) {
2769 		(void) fprintf(stderr,
2770 		    "ifconfig: Cannot specify logical interface for usesrc \n");
2771 		exit(1);
2772 	}
2773 
2774 	rval = strcmp(arg, NONE_STR);
2775 	if (rval != 0) {
2776 		if ((ifindex = if_nametoindex(arg)) == 0) {
2777 			(void) strncpy(lifr.lifr_name, arg, LIFNAMSIZ);
2778 			Perror0_exit("Could not get interface index");
2779 		}
2780 		lifr.lifr_index = ifindex;
2781 	} else {
2782 		if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) != 0)
2783 			Perror0_exit("Not a valid usesrc consumer");
2784 		lifr.lifr_index = 0;
2785 	}
2786 
2787 	if (debug)
2788 		(void) printf("setifsrc: lifr_name %s, lifr_index %d\n",
2789 		    lifr.lifr_name, lifr.lifr_index);
2790 
2791 	if (ioctl(s, SIOCSLIFUSESRC, (caddr_t)&lifr) == -1) {
2792 		if (rval == 0)
2793 			Perror0_exit("Cannot reset usesrc group");
2794 		else
2795 			Perror0_exit("Could not set source interface");
2796 	}
2797 
2798 	return (0);
2799 }
2800 
2801 /*
2802  * Print the interface status line associated with `ifname'
2803  */
2804 static void
2805 ifstatus(const char *ifname)
2806 {
2807 	uint64_t flags;
2808 	char if_usesrc_name[LIFNAMSIZ];
2809 	char *newbuf;
2810 	int n, numifs, rval = 0;
2811 	struct lifreq *lifrp;
2812 	struct lifsrcof lifs;
2813 
2814 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2815 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2816 		Perror0_exit("status: SIOCGLIFFLAGS");
2817 	}
2818 	flags = lifr.lifr_flags;
2819 
2820 	/*
2821 	 * In V4 compatibility mode, we don't print the IFF_IPV4 flag or
2822 	 * interfaces with IFF_IPV6 set.
2823 	 */
2824 	if (v4compat) {
2825 		flags &= ~IFF_IPV4;
2826 		if (flags & IFF_IPV6)
2827 			return;
2828 	}
2829 
2830 	(void) printf("%s: ", ifname);
2831 	print_flags(flags);
2832 
2833 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2834 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
2835 		Perror0_exit("status: SIOCGLIFMETRIC");
2836 	} else {
2837 		if (lifr.lifr_metric)
2838 			(void) printf(" metric %d", lifr.lifr_metric);
2839 	}
2840 	if (ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
2841 		(void) printf(" mtu %u", lifr.lifr_mtu);
2842 
2843 	/* don't print index or zone when in compatibility mode */
2844 	if (!v4compat) {
2845 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
2846 			(void) printf(" index %d", lifr.lifr_index);
2847 		/*
2848 		 * Stack instances use GLOBAL_ZONEID for IP data structures
2849 		 * even in the non-global zone.
2850 		 */
2851 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifr) >= 0 &&
2852 		    lifr.lifr_zoneid != getzoneid() &&
2853 		    lifr.lifr_zoneid != GLOBAL_ZONEID) {
2854 			char zone_name[ZONENAME_MAX];
2855 
2856 			if (lifr.lifr_zoneid == ALL_ZONES) {
2857 				(void) printf("\n\tall-zones");
2858 			} else if (getzonenamebyid(lifr.lifr_zoneid, zone_name,
2859 			    sizeof (zone_name)) < 0) {
2860 				(void) printf("\n\tzone %d", lifr.lifr_zoneid);
2861 			} else {
2862 				(void) printf("\n\tzone %s", zone_name);
2863 			}
2864 		}
2865 	}
2866 
2867 	if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) {
2868 		lifs.lifs_ifindex = lifr.lifr_index;
2869 
2870 		/*
2871 		 * Find the number of interfaces that use this interfaces'
2872 		 * address as a source address
2873 		 */
2874 		lifs.lifs_buf = NULL;
2875 		lifs.lifs_maxlen = 0;
2876 		for (;;) {
2877 			/* The first pass will give the bufsize we need */
2878 			rval = ioctl(s, SIOCGLIFSRCOF, (char *)&lifs);
2879 			if (rval < 0) {
2880 				if (lifs.lifs_buf != NULL) {
2881 					free(lifs.lifs_buf);
2882 					lifs.lifs_buf = NULL;
2883 				}
2884 				lifs.lifs_len = 0;
2885 				break;
2886 			}
2887 			if (lifs.lifs_len <= lifs.lifs_maxlen)
2888 				break;
2889 			/* Use kernel's size + a small margin to avoid loops */
2890 			lifs.lifs_maxlen = lifs.lifs_len +
2891 			    5 * sizeof (struct lifreq);
2892 			/* For the first pass, realloc acts like malloc */
2893 			newbuf = realloc(lifs.lifs_buf, lifs.lifs_maxlen);
2894 			if (newbuf == NULL) {
2895 				if (lifs.lifs_buf != NULL) {
2896 					free(lifs.lifs_buf);
2897 					lifs.lifs_buf = NULL;
2898 				}
2899 				lifs.lifs_len = 0;
2900 				break;
2901 			}
2902 			lifs.lifs_buf = newbuf;
2903 		}
2904 
2905 
2906 		numifs = lifs.lifs_len / sizeof (struct lifreq);
2907 		if (numifs > 0) {
2908 			lifrp = lifs.lifs_req;
2909 			(void) printf("\n\tsrcof");
2910 			for (n = numifs; n > 0; n--, lifrp++) {
2911 				(void) printf(" %s", lifrp->lifr_name);
2912 			}
2913 		}
2914 
2915 		if (lifs.lifs_buf != NULL)
2916 			free(lifs.lifs_buf);
2917 	}
2918 
2919 	/* Find the interface whose source address this interface uses */
2920 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
2921 		if (lifr.lifr_index != 0) {
2922 			if (if_indextoname(lifr.lifr_index,
2923 			    if_usesrc_name) == NULL) {
2924 				(void) printf("\n\tusesrc ifIndex %d",
2925 				    lifr.lifr_index);
2926 			} else {
2927 				(void) printf("\n\tusesrc %s", if_usesrc_name);
2928 			}
2929 		}
2930 	}
2931 
2932 	(void) putchar('\n');
2933 }
2934 
2935 /*
2936  * Print the status of the interface.  If an address family was
2937  * specified, show it and it only; otherwise, show them all.
2938  */
2939 static void
2940 status(void)
2941 {
2942 	struct afswtch	*p = afp;
2943 	uint64_t	flags;
2944 	datalink_id_t	linkid;
2945 
2946 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2947 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2948 		Perror0_exit("status: SIOCGLIFFLAGS");
2949 	}
2950 
2951 	flags = lifr.lifr_flags;
2952 
2953 	/*
2954 	 * Only print the interface status if the address family matches
2955 	 * the interface family flag.
2956 	 */
2957 	if (p != NULL) {
2958 		if (((p->af_af == AF_INET6) && (flags & IFF_IPV4)) ||
2959 		    ((p->af_af == AF_INET) && (flags & IFF_IPV6)))
2960 			return;
2961 	}
2962 
2963 	/*
2964 	 * In V4 compatibility mode, don't print IFF_IPV6 interfaces.
2965 	 */
2966 	if (v4compat && (flags & IFF_IPV6))
2967 		return;
2968 
2969 	ifstatus(name);
2970 
2971 	if (ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid) ==
2972 	    DLADM_STATUS_OK)
2973 		tun_status(linkid);
2974 
2975 	if (p != NULL) {
2976 		(*p->af_status)(1, flags);
2977 	} else {
2978 		for (p = afs; p->af_name; p++) {
2979 			/* set global af for use in p->af_status */
2980 			af = p->af_af;
2981 			(*p->af_status)(0, flags);
2982 		}
2983 
2984 		/*
2985 		 * Historically, 'ether' has been an address family,
2986 		 * so print it here.
2987 		 */
2988 		print_ifether(name);
2989 	}
2990 }
2991 
2992 /*
2993  * Print the status of the interface in a format that can be used to
2994  * reconfigure the interface later. Code stolen from status() above.
2995  */
2996 /* ARGSUSED */
2997 static int
2998 configinfo(char *null, int64_t param)
2999 {
3000 	char *cp;
3001 	struct afswtch *p = afp;
3002 	uint64_t flags;
3003 	char lifname[LIFNAMSIZ];
3004 	char if_usesrc_name[LIFNAMSIZ];
3005 
3006 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3007 
3008 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
3009 		Perror0_exit("status: SIOCGLIFFLAGS");
3010 	}
3011 	flags = lifr.lifr_flags;
3012 
3013 	if (debug) {
3014 		(void) printf("configinfo: name %s flags  0x%llx af_af %d\n",
3015 		    name, flags, p != NULL ? p->af_af : -1);
3016 	}
3017 
3018 	/*
3019 	 * Build the interface name to print (we can't directly use `name'
3020 	 * because one cannot "plumb" ":0" interfaces).
3021 	 */
3022 	(void) strlcpy(lifname, name, LIFNAMSIZ);
3023 	if ((cp = strchr(lifname, ':')) != NULL && atoi(cp + 1) == 0)
3024 		*cp = '\0';
3025 
3026 	/*
3027 	 * if the interface is IPv4
3028 	 *	if we have a IPv6 address family restriction return
3029 	 *		so it won't print
3030 	 *	if we are in IPv4 compatibility mode, clear out IFF_IPV4
3031 	 *		so we don't print it.
3032 	 */
3033 	if (flags & IFF_IPV4) {
3034 		if (p && p->af_af == AF_INET6)
3035 			return (-1);
3036 		if (v4compat)
3037 			flags &= ~IFF_IPV4;
3038 
3039 		(void) printf("%s inet plumb", lifname);
3040 	} else if (flags & IFF_IPV6) {
3041 		/*
3042 		 * else if the interface is IPv6
3043 		 *	if we have a IPv4 address family restriction return
3044 		 *	or we are in IPv4 compatibiltiy mode, return.
3045 		 */
3046 		if (p && p->af_af == AF_INET)
3047 			return (-1);
3048 		if (v4compat)
3049 			return (-1);
3050 
3051 		(void) printf("%s inet6 plumb", lifname);
3052 	}
3053 
3054 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3055 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
3056 		Perror0_exit("configinfo: SIOCGLIFMETRIC");
3057 	} else {
3058 		if (lifr.lifr_metric)
3059 			(void) printf(" metric %d ", lifr.lifr_metric);
3060 	}
3061 	if (((flags & (IFF_VIRTUAL|IFF_LOOPBACK)) != IFF_VIRTUAL) &&
3062 	    ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
3063 		(void) printf(" mtu %d", lifr.lifr_metric);
3064 
3065 	/* Index only applies to the zeroth interface */
3066 	if (lifnum(name) == 0) {
3067 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
3068 			(void) printf(" index %d", lifr.lifr_index);
3069 	}
3070 
3071 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
3072 		if (lifr.lifr_index != 0) {
3073 			if (if_indextoname(lifr.lifr_index,
3074 			    if_usesrc_name) != NULL) {
3075 				(void) printf(" usesrc %s", if_usesrc_name);
3076 			}
3077 		}
3078 	}
3079 
3080 	if (p != NULL) {
3081 		(*p->af_configinfo)(1, flags);
3082 	} else {
3083 		for (p = afs; p->af_name; p++) {
3084 			(void) close(s);
3085 			s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0);
3086 			/* set global af for use in p->af_configinfo */
3087 			af = p->af_af;
3088 			if (s == -1) {
3089 				Perror0_exit("socket");
3090 			}
3091 			(*p->af_configinfo)(0, flags);
3092 		}
3093 	}
3094 
3095 	(void) putchar('\n');
3096 	return (0);
3097 }
3098 
3099 static void
3100 print_tsec(iptun_params_t *params)
3101 {
3102 	ipsec_req_t *ipsr;
3103 
3104 	(void) printf("\ttunnel security settings  ");
3105 	if (!(params->iptun_param_flags & IPTUN_PARAM_SECINFO)) {
3106 		(void) printf("-->  use 'ipsecconf -ln -i %s'", name);
3107 	} else {
3108 		ipsr = &params->iptun_param_secinfo;
3109 		if (ipsr->ipsr_ah_req & IPSEC_PREF_REQUIRED) {
3110 			(void) printf("ah (%s)  ",
3111 			    rparsealg(ipsr->ipsr_auth_alg, IPSEC_PROTO_AH));
3112 		}
3113 		if (ipsr->ipsr_esp_req & IPSEC_PREF_REQUIRED) {
3114 			(void) printf("esp (%s",
3115 			    rparsealg(ipsr->ipsr_esp_alg, IPSEC_PROTO_ESP));
3116 			(void) printf("/%s)",
3117 			    rparsealg(ipsr->ipsr_esp_auth_alg, IPSEC_PROTO_AH));
3118 		}
3119 	}
3120 	(void) printf("\n");
3121 }
3122 
3123 static void
3124 tun_status(datalink_id_t linkid)
3125 {
3126 	iptun_params_t	params;
3127 	char		propval[DLADM_PROP_VAL_MAX];
3128 	char		*valptr[1];
3129 	uint_t		valcnt = 1;
3130 	boolean_t	tabbed = _B_FALSE;
3131 
3132 	params.iptun_param_linkid = linkid;
3133 
3134 	/* If dladm_iptun_getparams() fails, assume we are not a tunnel. */
3135 	assert(dlh_opened);
3136 	if (dladm_iptun_getparams(dlh, &params, DLADM_OPT_ACTIVE) !=
3137 	    DLADM_STATUS_OK)
3138 		return;
3139 
3140 	switch (params.iptun_param_type) {
3141 	case IPTUN_TYPE_IPV4:
3142 	case IPTUN_TYPE_6TO4:
3143 		(void) printf("\tinet");
3144 		break;
3145 	case IPTUN_TYPE_IPV6:
3146 		(void) printf("\tinet6");
3147 		break;
3148 	default:
3149 		dladmerr_exit(DLADM_STATUS_IPTUNTYPE, name);
3150 		break;
3151 	}
3152 
3153 	/*
3154 	 * There is always a source address.  If it hasn't been explicitly
3155 	 * set, the API will pass back a buffer containing the unspecified
3156 	 * address.
3157 	 */
3158 	(void) printf(" tunnel src %s ", params.iptun_param_laddr);
3159 
3160 	if (params.iptun_param_flags & IPTUN_PARAM_RADDR)
3161 		(void) printf("tunnel dst %s\n", params.iptun_param_raddr);
3162 	else
3163 		(void) putchar('\n');
3164 
3165 	if (params.iptun_param_flags & IPTUN_PARAM_IPSECPOL)
3166 		print_tsec(&params);
3167 
3168 	valptr[0] = propval;
3169 	if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT, "hoplimit",
3170 	    (char **)valptr, &valcnt) == DLADM_STATUS_OK) {
3171 		(void) printf("\ttunnel hop limit %s ", propval);
3172 		tabbed = _B_TRUE;
3173 	}
3174 
3175 	if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT,
3176 	    "encaplimit", (char **)valptr, &valcnt) == DLADM_STATUS_OK) {
3177 		uint32_t elim;
3178 
3179 		if (!tabbed) {
3180 			(void) putchar('\t');
3181 			tabbed = _B_TRUE;
3182 		}
3183 		elim = strtol(propval, NULL, 10);
3184 		if (elim > 0)
3185 			(void) printf("tunnel encapsulation limit %s", propval);
3186 		else
3187 			(void) printf("tunnel encapsulation limit disabled");
3188 	}
3189 
3190 	if (tabbed)
3191 		(void) putchar('\n');
3192 }
3193 
3194 static void
3195 in_status(int force, uint64_t flags)
3196 {
3197 	struct sockaddr_in	*sin, *laddr;
3198 	struct sockaddr_in	netmask = { AF_INET };
3199 
3200 	if (debug)
3201 		(void) printf("in_status(%s) flags 0x%llx\n", name, flags);
3202 
3203 	/* only print status for IPv4 interfaces */
3204 	if (!(flags & IFF_IPV4))
3205 		return;
3206 
3207 	if (!(flags & IFF_NOLOCAL)) {
3208 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3209 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3210 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3211 			    errno == ENXIO) {
3212 				if (!force)
3213 					return;
3214 				(void) memset(&lifr.lifr_addr, 0,
3215 				    sizeof (lifr.lifr_addr));
3216 			} else
3217 				Perror0_exit("in_status: SIOCGLIFADDR");
3218 		}
3219 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3220 		(void) printf("\tinet %s ", inet_ntoa(sin->sin_addr));
3221 		laddr = sin;
3222 	} else {
3223 		(void) printf("\tinet ");
3224 	}
3225 
3226 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3227 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3228 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3229 		    errno == ENXIO) {
3230 			if (!force)
3231 				return;
3232 			(void) memset(&lifr.lifr_addr, 0,
3233 			    sizeof (lifr.lifr_addr));
3234 		} else {
3235 			Perror0_exit("in_status: SIOCGLIFSUBNET");
3236 		}
3237 	}
3238 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
3239 	if ((flags & IFF_NOLOCAL) ||
3240 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
3241 		(void) printf("subnet %s/%d ", inet_ntoa(sin->sin_addr),
3242 		    lifr.lifr_addrlen);
3243 	}
3244 	if (sin->sin_family != AF_INET) {
3245 		(void) printf("Wrong family: %d\n", sin->sin_family);
3246 	}
3247 
3248 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3249 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
3250 		if (errno != EADDRNOTAVAIL)
3251 			Perror0_exit("in_status: SIOCGLIFNETMASK");
3252 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
3253 	} else
3254 		netmask.sin_addr =
3255 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
3256 	if (flags & IFF_POINTOPOINT) {
3257 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3258 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3259 			if (errno == EADDRNOTAVAIL)
3260 				(void) memset(&lifr.lifr_addr, 0,
3261 				    sizeof (lifr.lifr_addr));
3262 			else
3263 				Perror0_exit("in_status: SIOCGLIFDSTADDR");
3264 		}
3265 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
3266 		(void) printf("--> %s ", inet_ntoa(sin->sin_addr));
3267 	}
3268 	(void) printf("netmask %x ", ntohl(netmask.sin_addr.s_addr));
3269 	if (flags & IFF_BROADCAST) {
3270 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3271 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
3272 			if (errno == EADDRNOTAVAIL)
3273 				(void) memset(&lifr.lifr_addr, 0,
3274 				    sizeof (lifr.lifr_addr));
3275 			else
3276 				Perror0_exit("in_status: SIOCGLIFBRDADDR");
3277 		}
3278 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3279 		if (sin->sin_addr.s_addr != 0) {
3280 			(void) printf("broadcast %s",
3281 			    inet_ntoa(sin->sin_addr));
3282 		}
3283 	}
3284 	/* If there is a groupname, print it for only the physical interface */
3285 	if (strchr(name, ':') == NULL) {
3286 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3287 		    lifr.lifr_groupname[0] != '\0') {
3288 			(void) printf("\n\tgroupname %s", lifr.lifr_groupname);
3289 		}
3290 	}
3291 	(void) putchar('\n');
3292 }
3293 
3294 static void
3295 in6_status(int force, uint64_t flags)
3296 {
3297 	char			abuf[INET6_ADDRSTRLEN];
3298 	struct sockaddr_in6	*sin6, *laddr6;
3299 
3300 	if (debug)
3301 		(void) printf("in6_status(%s) flags 0x%llx\n", name, flags);
3302 
3303 	if (!(flags & IFF_IPV6))
3304 		return;
3305 
3306 	if (!(flags & IFF_NOLOCAL)) {
3307 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3308 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3309 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3310 			    errno == ENXIO) {
3311 				if (!force)
3312 					return;
3313 				(void) memset(&lifr.lifr_addr, 0,
3314 				    sizeof (lifr.lifr_addr));
3315 			} else
3316 				Perror0_exit("in_status6: SIOCGLIFADDR");
3317 		}
3318 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3319 		(void) printf("\tinet6 %s/%d ",
3320 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3321 		    abuf, sizeof (abuf)),
3322 		    lifr.lifr_addrlen);
3323 		laddr6 = sin6;
3324 	} else {
3325 		(void) printf("\tinet6 ");
3326 	}
3327 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3328 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3329 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3330 		    errno == ENXIO) {
3331 			if (!force)
3332 				return;
3333 			(void) memset(&lifr.lifr_addr, 0,
3334 			    sizeof (lifr.lifr_addr));
3335 		} else
3336 			Perror0_exit("in_status6: SIOCGLIFSUBNET");
3337 	}
3338 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3339 	if ((flags & IFF_NOLOCAL) ||
3340 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
3341 		(void) printf("subnet %s/%d ",
3342 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3343 		    abuf, sizeof (abuf)),
3344 		    lifr.lifr_addrlen);
3345 	}
3346 	if (sin6->sin6_family != AF_INET6) {
3347 		(void) printf("Wrong family: %d\n", sin6->sin6_family);
3348 	}
3349 	if (flags & IFF_POINTOPOINT) {
3350 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3351 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3352 			if (errno == EADDRNOTAVAIL)
3353 				(void) memset(&lifr.lifr_addr, 0,
3354 				    sizeof (lifr.lifr_addr));
3355 			else
3356 				Perror0_exit("in_status6: SIOCGLIFDSTADDR");
3357 		}
3358 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
3359 		(void) printf("--> %s ",
3360 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3361 		    abuf, sizeof (abuf)));
3362 	}
3363 	if (verbose) {
3364 		(void) putchar('\n');
3365 		(void) putchar('\t');
3366 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3367 		if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
3368 			if (errno == EADDRNOTAVAIL || errno == EINVAL)
3369 				(void) memset(&lifr.lifr_addr, 0,
3370 				    sizeof (lifr.lifr_addr));
3371 			else
3372 				Perror0_exit("in_status6: SIOCGLIFTOKEN");
3373 		} else {
3374 			sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3375 			(void) printf("token %s/%d ",
3376 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3377 			    abuf, sizeof (abuf)),
3378 			    lifr.lifr_addrlen);
3379 		}
3380 		if (ioctl(s, SIOCGLIFLNKINFO, (caddr_t)&lifr) < 0) {
3381 			if (errno != EINVAL) {
3382 				Perror0_exit("in_status6: SIOCGLIFLNKINFO");
3383 			}
3384 		} else {
3385 			(void) printf("maxhops %u, reachtime %u ms, "
3386 			    "reachretrans %u ms, maxmtu %u ",
3387 			    lifr.lifr_ifinfo.lir_maxhops,
3388 			    lifr.lifr_ifinfo.lir_reachtime,
3389 			    lifr.lifr_ifinfo.lir_reachretrans,
3390 			    lifr.lifr_ifinfo.lir_maxmtu);
3391 		}
3392 	}
3393 	/* If there is a groupname, print it for only the physical interface */
3394 	if (strchr(name, ':') == NULL) {
3395 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3396 		    lifr.lifr_groupname[0] != '\0') {
3397 			(void) printf("\n\tgroupname %s", lifr.lifr_groupname);
3398 		}
3399 	}
3400 	(void) putchar('\n');
3401 }
3402 
3403 static void
3404 in_configinfo(int force, uint64_t flags)
3405 {
3406 	struct sockaddr_in *sin, *laddr;
3407 	struct	sockaddr_in netmask = { AF_INET };
3408 
3409 	if (debug)
3410 		(void) printf("in_configinfo(%s) flags 0x%llx\n", name, flags);
3411 
3412 	/* only configinfo info for IPv4 interfaces */
3413 	if (!(flags & IFF_IPV4))
3414 		return;
3415 
3416 	if (!(flags & IFF_NOLOCAL)) {
3417 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3418 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3419 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3420 			    errno == ENXIO) {
3421 				if (!force)
3422 					return;
3423 				(void) memset(&lifr.lifr_addr, 0,
3424 				    sizeof (lifr.lifr_addr));
3425 			} else
3426 				Perror0_exit("in_configinfo: SIOCGLIFADDR");
3427 		}
3428 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3429 		(void) printf(" set %s ", inet_ntoa(sin->sin_addr));
3430 		laddr = sin;
3431 	}
3432 
3433 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3434 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3435 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3436 		    errno == ENXIO) {
3437 			if (!force)
3438 				return;
3439 			(void) memset(&lifr.lifr_addr, 0,
3440 			    sizeof (lifr.lifr_addr));
3441 		} else {
3442 			Perror0_exit("in_configinfo: SIOCGLIFSUBNET");
3443 		}
3444 	}
3445 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
3446 
3447 	if ((flags & IFF_NOLOCAL) ||
3448 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
3449 		(void) printf(" subnet %s/%d ", inet_ntoa(sin->sin_addr),
3450 		    lifr.lifr_addrlen);
3451 	}
3452 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3453 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
3454 		if (errno != EADDRNOTAVAIL)
3455 			Perror0_exit("in_configinfo: SIOCGLIFNETMASK");
3456 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
3457 	} else
3458 		netmask.sin_addr =
3459 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
3460 	if (flags & IFF_POINTOPOINT) {
3461 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3462 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3463 			if (errno == EADDRNOTAVAIL)
3464 				(void) memset(&lifr.lifr_addr, 0,
3465 				    sizeof (lifr.lifr_addr));
3466 			else
3467 				Perror0_exit("in_configinfo: SIOCGLIFDSTADDR");
3468 		}
3469 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
3470 		(void) printf(" destination %s ", inet_ntoa(sin->sin_addr));
3471 	}
3472 	(void) printf(" netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
3473 	if (flags & IFF_BROADCAST) {
3474 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3475 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
3476 			if (errno == EADDRNOTAVAIL)
3477 				(void) memset(&lifr.lifr_addr, 0,
3478 				    sizeof (lifr.lifr_addr));
3479 			else
3480 				Perror0_exit("in_configinfo: SIOCGLIFBRDADDR");
3481 		}
3482 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3483 		if (sin->sin_addr.s_addr != 0) {
3484 			(void) printf(" broadcast %s ",
3485 			    inet_ntoa(sin->sin_addr));
3486 		}
3487 	}
3488 
3489 	/* If there is a groupname, print it for only the zeroth interface */
3490 	if (lifnum(name) == 0) {
3491 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3492 		    lifr.lifr_groupname[0] != '\0') {
3493 			(void) printf(" group %s ", lifr.lifr_groupname);
3494 		}
3495 	}
3496 
3497 	/* Print flags to configure */
3498 	print_config_flags(AF_INET, flags);
3499 }
3500 
3501 static void
3502 in6_configinfo(int force, uint64_t flags)
3503 {
3504 	char abuf[INET6_ADDRSTRLEN];
3505 	struct sockaddr_in6 *sin6, *laddr6;
3506 
3507 	if (debug)
3508 		(void) printf("in6_configinfo(%s) flags 0x%llx\n", name,
3509 		    flags);
3510 
3511 	if (!(flags & IFF_IPV6))
3512 		return;
3513 
3514 	if (!(flags & IFF_NOLOCAL)) {
3515 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3516 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3517 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3518 			    errno == ENXIO) {
3519 				if (!force)
3520 					return;
3521 				(void) memset(&lifr.lifr_addr, 0,
3522 				    sizeof (lifr.lifr_addr));
3523 			} else
3524 				Perror0_exit("in6_configinfo: SIOCGLIFADDR");
3525 		}
3526 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3527 		(void) printf(" set %s/%d ",
3528 		    inet_ntop(AF_INET6, &sin6->sin6_addr, abuf, sizeof (abuf)),
3529 		    lifr.lifr_addrlen);
3530 		laddr6 = sin6;
3531 	}
3532 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3533 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3534 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3535 		    errno == ENXIO) {
3536 			if (!force)
3537 				return;
3538 			(void) memset(&lifr.lifr_addr, 0,
3539 			    sizeof (lifr.lifr_addr));
3540 		} else
3541 			Perror0_exit("in6_configinfo: SIOCGLIFSUBNET");
3542 	}
3543 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3544 	if ((flags & IFF_NOLOCAL) ||
3545 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
3546 		(void) printf(" subnet %s/%d ",
3547 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3548 		    abuf, sizeof (abuf)),
3549 		    lifr.lifr_addrlen);
3550 	}
3551 
3552 	if (flags & IFF_POINTOPOINT) {
3553 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3554 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3555 			if (errno == EADDRNOTAVAIL)
3556 				(void) memset(&lifr.lifr_addr, 0,
3557 				    sizeof (lifr.lifr_addr));
3558 			else
3559 				Perror0_exit("in6_configinfo: SIOCGLIFDSTADDR");
3560 		}
3561 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
3562 		(void) printf(" destination %s ",
3563 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3564 		    abuf, sizeof (abuf)));
3565 	}
3566 
3567 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3568 	if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
3569 		if (errno == EADDRNOTAVAIL || errno == EINVAL)
3570 			(void) memset(&lifr.lifr_addr, 0,
3571 			    sizeof (lifr.lifr_addr));
3572 		else
3573 			Perror0_exit("in6_configinfo: SIOCGLIFTOKEN");
3574 	} else {
3575 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3576 		(void) printf(" token %s/%d ",
3577 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3578 		    abuf, sizeof (abuf)),
3579 		    lifr.lifr_addrlen);
3580 	}
3581 
3582 	/* If there is a groupname, print it for only the zeroth interface */
3583 	if (lifnum(name) == 0) {
3584 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3585 		    lifr.lifr_groupname[0] != '\0') {
3586 			(void) printf(" group %s ", lifr.lifr_groupname);
3587 		}
3588 	}
3589 
3590 	/* Print flags to configure */
3591 	print_config_flags(AF_INET6, flags);
3592 }
3593 
3594 /*
3595  * If this is a physical interface then remove it.
3596  * If it is a logical interface name use SIOCLIFREMOVEIF to
3597  * remove it. In both cases fail if it doesn't exist.
3598  */
3599 /* ARGSUSED */
3600 static int
3601 inetunplumb(char *arg, int64_t param)
3602 {
3603 	ipadm_status_t	istatus;
3604 
3605 	istatus = ipadm_delete_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE);
3606 	if (istatus != IPADM_SUCCESS) {
3607 		(void) fprintf(stderr, "ifconfig: cannot unplumb %s: %s\n",
3608 		    name, ipadm_status2str(istatus));
3609 		exit(1);
3610 	}
3611 
3612 	return (0);
3613 }
3614 
3615 /*
3616  * Create the interface in `name', using ipadm_create_if(). If `name' is a
3617  * logical interface or loopback interface, ipadm_create_if() uses
3618  * SIOCLIFADDIF to create it.
3619  */
3620 /* ARGSUSED */
3621 static int
3622 inetplumb(char *arg, int64_t param)
3623 {
3624 	ipadm_status_t	istatus;
3625 
3626 	istatus = ipadm_create_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE);
3627 	if (istatus != IPADM_SUCCESS) {
3628 		(void) fprintf(stderr, "ifconfig: cannot plumb %s: %s\n",
3629 		    name, ipadm_status2str(istatus));
3630 		if (istatus != IPADM_IF_EXISTS)
3631 			exit(1);
3632 	}
3633 	return (0);
3634 }
3635 
3636 /* ARGSUSED */
3637 static int
3638 inetipmp(char *arg, int64_t param)
3639 {
3640 	int retval;
3641 
3642 	/*
3643 	 * Treat e.g. "ifconfig ipmp0:2 ipmp" as "ifconfig ipmp0:2 plumb".
3644 	 * Otherwise, try to create the requested IPMP interface.
3645 	 */
3646 	if (strchr(name, ':') != NULL)
3647 		retval = inetplumb(arg, param);
3648 	else
3649 		retval = create_ipmp(name, afp->af_af, name, _B_FALSE);
3650 
3651 	/*
3652 	 * We'd return -1, but foreachinterface() doesn't propagate the error
3653 	 * into the exit status, so we're forced to explicitly exit().
3654 	 */
3655 	if (retval == -1)
3656 		exit(1);
3657 	return (0);
3658 }
3659 
3660 /*
3661  * Create an IPMP group `grname' with address family `af'.  If `ifname' is
3662  * non-NULL, it specifies the interface name to use.  Otherwise, use the name
3663  * ipmpN, where N corresponds to the lowest available integer.  If `implicit'
3664  * is set, then the group is being created as a side-effect of placing an
3665  * underlying interface in a group.  Also start in.mpathd if necessary.
3666  */
3667 static int
3668 create_ipmp(const char *grname, int af, const char *ifname, boolean_t implicit)
3669 {
3670 	static int ipmp_daemon_started;
3671 	uint32_t flags = IPADM_OPT_IPMP|IPADM_OPT_ACTIVE;
3672 	ipadm_status_t istatus;
3673 
3674 	if (debug) {
3675 		(void) printf("create_ipmp: ifname %s grname %s af %d\n",
3676 		    ifname != NULL ? ifname : "NULL", grname, af);
3677 	}
3678 
3679 	/*
3680 	 * ipadm_create_if() creates the IPMP interface and fills in the
3681 	 * ppa in lifr.lifr_name, if `ifname'="ipmp".
3682 	 */
3683 	(void) strlcpy(lifr.lifr_name, (ifname ? ifname : "ipmp"),
3684 	    sizeof (lifr.lifr_name));
3685 	if (ifname == NULL)
3686 		flags |= IPADM_OPT_GENPPA;
3687 	istatus = ipadm_create_if(iph, lifr.lifr_name, af, flags);
3688 	if (istatus != IPADM_SUCCESS) {
3689 		(void) fprintf(stderr, "ifconfig: cannot create IPMP interface "
3690 		    "%s: %s\n", grname, ipadm_status2str(istatus));
3691 		return (-1);
3692 	}
3693 
3694 	/*
3695 	 * To preserve backward-compatibility, always bring up the link-local
3696 	 * address for implicitly-created IPv6 IPMP interfaces.
3697 	 */
3698 	if (implicit && af == AF_INET6) {
3699 		if (ioctl(s6, SIOCGLIFFLAGS, &lifr) == 0) {
3700 			lifr.lifr_flags |= IFF_UP;
3701 			(void) ioctl(s6, SIOCSLIFFLAGS, &lifr);
3702 		}
3703 	}
3704 
3705 	/*
3706 	 * If the caller requested a different group name, issue a
3707 	 * SIOCSLIFGROUPNAME on the new IPMP interface.
3708 	 */
3709 	if (strcmp(lifr.lifr_name, grname) != 0) {
3710 		(void) strlcpy(lifr.lifr_groupname, grname, LIFGRNAMSIZ);
3711 		if (ioctl(s, SIOCSLIFGROUPNAME, &lifr) == -1) {
3712 			Perror0("SIOCSLIFGROUPNAME");
3713 			return (-1);
3714 		}
3715 	}
3716 
3717 	/*
3718 	 * If we haven't done so yet, ensure in.mpathd is started.
3719 	 */
3720 	if (ipmp_daemon_started++ == 0)
3721 		start_ipmp_daemon();
3722 
3723 	return (0);
3724 }
3725 
3726 /*
3727  * Start in.mpathd if it's not already running.
3728  */
3729 static void
3730 start_ipmp_daemon(void)
3731 {
3732 	int retval;
3733 	ipmp_handle_t ipmp_handle;
3734 
3735 	/*
3736 	 * Ping in.mpathd to see if it's running already.
3737 	 */
3738 	if ((retval = ipmp_open(&ipmp_handle)) != IPMP_SUCCESS) {
3739 		(void) fprintf(stderr, "ifconfig: cannot create IPMP handle: "
3740 		    "%s\n", ipmp_errmsg(retval));
3741 		return;
3742 	}
3743 
3744 	retval = ipmp_ping_daemon(ipmp_handle);
3745 	ipmp_close(ipmp_handle);
3746 
3747 	switch (retval) {
3748 	case IPMP_ENOMPATHD:
3749 		break;
3750 	case IPMP_SUCCESS:
3751 		return;
3752 	default:
3753 		(void) fprintf(stderr, "ifconfig: cannot ping in.mpathd: %s\n",
3754 		    ipmp_errmsg(retval));
3755 		break;
3756 	}
3757 
3758 	/*
3759 	 * Start in.mpathd.  Note that in.mpathd will handle multiple
3760 	 * incarnations (ipmp_ping_daemon() is just an optimization) so we
3761 	 * don't need to worry about racing with another ifconfig process.
3762 	 */
3763 	switch (fork()) {
3764 	case -1:
3765 		Perror0_exit("start_ipmp_daemon: fork");
3766 		/* NOTREACHED */
3767 	case 0:
3768 		(void) execl(MPATHD_PATH, MPATHD_PATH, NULL);
3769 		_exit(1);
3770 		/* NOTREACHED */
3771 	default:
3772 		break;
3773 	}
3774 }
3775 
3776 /*
3777  * Bring the address named by `ifaddrp' up or down.  Doesn't trust any mutable
3778  * values in ia_flags since they may be stale.
3779  */
3780 static boolean_t
3781 ifaddr_op(ifaddrlistx_t *ifaddrp, boolean_t up)
3782 {
3783 	struct lifreq	lifrl;	/* Local lifreq struct */
3784 	int		fd = (ifaddrp->ia_flags & IFF_IPV4) ? s4 : s6;
3785 
3786 	(void) memset(&lifrl, 0, sizeof (lifrl));
3787 	(void) strlcpy(lifrl.lifr_name, ifaddrp->ia_name, LIFNAMSIZ);
3788 	if (ioctl(fd, SIOCGLIFFLAGS, &lifrl) == -1)
3789 		return (_B_FALSE);
3790 
3791 	if (up)
3792 		lifrl.lifr_flags |= IFF_UP;
3793 	else
3794 		lifrl.lifr_flags &= ~IFF_UP;
3795 
3796 	if (ioctl(fd, SIOCSLIFFLAGS, &lifrl) == -1)
3797 		return (_B_FALSE);
3798 
3799 	/*
3800 	 * If we're trying to bring the address down, ensure that DAD activity
3801 	 * (observable by IFF_DUPLICATE) has also been stopped.
3802 	 */
3803 	if (!up && ioctl(fd, SIOCGLIFFLAGS, &lifrl) != -1 &&
3804 	    lifrl.lifr_flags & IFF_DUPLICATE) {
3805 		if (ioctl(fd, SIOCGLIFADDR, &lifrl) == -1 ||
3806 		    ioctl(fd, SIOCSLIFADDR, &lifrl) == -1) {
3807 			return (_B_FALSE);
3808 		}
3809 	}
3810 	return (_B_TRUE);
3811 }
3812 
3813 static boolean_t
3814 ifaddr_up(ifaddrlistx_t *ifaddrp)
3815 {
3816 	return (ifaddr_op(ifaddrp, _B_TRUE));
3817 }
3818 
3819 static boolean_t
3820 ifaddr_down(ifaddrlistx_t *ifaddrp)
3821 {
3822 	return (ifaddr_op(ifaddrp, _B_FALSE));
3823 }
3824 
3825 /*
3826  * Open the global libdladm handle "dlh" if it isn't already opened.  The
3827  * caller may optionally supply a link name to obtain its linkid.  If a link
3828  * of a specific class or classes is required, reqclass specifies the class
3829  * mask.
3830  */
3831 static dladm_status_t
3832 ifconfig_dladm_open(const char *name, datalink_class_t reqclass,
3833     datalink_id_t *linkid)
3834 {
3835 	dladm_status_t status = DLADM_STATUS_OK;
3836 	datalink_class_t class;
3837 
3838 	if (!dlh_opened) {
3839 		if ((status = dladm_open(&dlh)) != DLADM_STATUS_OK)
3840 			return (status);
3841 		dlh_opened = _B_TRUE;
3842 	}
3843 	if (name != NULL) {
3844 		status = dladm_name2info(dlh, name, linkid, NULL, &class, NULL);
3845 		if (status == DLADM_STATUS_OK) {
3846 			if (!(class & reqclass))
3847 				status = DLADM_STATUS_LINKINVAL;
3848 		}
3849 	}
3850 	return (status);
3851 }
3852 
3853 /*
3854  * This function checks if we can use libipadm API's. We will only
3855  * call libipadm functions for non-IPMP interfaces. This check is
3856  * temporary until libipadm supports IPMP interfaces.
3857  */
3858 static boolean_t
3859 ifconfig_use_libipadm(int s, const char *lifname)
3860 {
3861 	struct lifreq lifr1;
3862 
3863 	(void) strlcpy(lifr1.lifr_name, lifname, sizeof (lifr1.lifr_name));
3864 	if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr1) < 0) {
3865 		(void) strncpy(lifr.lifr_name, lifname,
3866 		    sizeof (lifr.lifr_name));
3867 		Perror0_exit("error");
3868 	}
3869 
3870 	return (lifr1.lifr_groupname[0] == '\0');
3871 }
3872 
3873 static void
3874 ipadmerr_exit(ipadm_status_t status, const char *str)
3875 {
3876 	(void) fprintf(stderr, "ifconfig: %s: %s\n", str,
3877 	    ipadm_status2str(status));
3878 	exit(1);
3879 }
3880 
3881 static void
3882 dladmerr_exit(dladm_status_t status, const char *str)
3883 {
3884 	char errstr[DLADM_STRSIZE];
3885 
3886 	(void) fprintf(stderr, "%s: %s\n", str,
3887 	    dladm_status2str(status, errstr));
3888 	exit(1);
3889 }
3890 
3891 void
3892 Perror0(const char *cmd)
3893 {
3894 	Perror2(cmd, lifr.lifr_name);
3895 }
3896 
3897 void
3898 Perror0_exit(const char *cmd)
3899 {
3900 	Perror0(cmd);
3901 	exit(1);
3902 }
3903 
3904 void
3905 Perror2(const char *cmd, const char *str)
3906 {
3907 	int error = errno;
3908 
3909 	(void) fprintf(stderr, "ifconfig: %s: ", cmd);
3910 
3911 	switch (error) {
3912 	case ENXIO:
3913 		(void) fprintf(stderr, "%s: no such interface\n", str);
3914 		break;
3915 	case EPERM:
3916 		(void) fprintf(stderr, "%s: permission denied\n", str);
3917 		break;
3918 	case EEXIST:
3919 		(void) fprintf(stderr, "%s: already exists\n", str);
3920 		break;
3921 	case ENAMETOOLONG:
3922 		(void) fprintf(stderr, "%s: interface name too long\n", str);
3923 		break;
3924 	case ERANGE:
3925 		(void) fprintf(stderr, "%s: logical interface id is outside "
3926 		    "allowed range\n", str);
3927 		break;
3928 	default:
3929 		errno = error;
3930 		perror(str);
3931 	}
3932 }
3933 
3934 /*
3935  * Print out error message (Perror2()) and exit
3936  */
3937 void
3938 Perror2_exit(const char *cmd, const char *str)
3939 {
3940 	Perror2(cmd, str);
3941 	exit(1);
3942 	/* NOTREACHED */
3943 }
3944 
3945 void
3946 Perrdlpi(const char *cmd, const char *linkname, int err)
3947 {
3948 	(void) fprintf(stderr, "ifconfig: %s \"%s\": %s\n", cmd,
3949 	    linkname, dlpi_strerror(err));
3950 }
3951 
3952 /*
3953  * Print out error message (Perrdlpi()) and exit
3954  */
3955 void
3956 Perrdlpi_exit(const char *cmd, const char *linkname, int err)
3957 {
3958 	Perrdlpi(cmd, linkname, err);
3959 	exit(1);
3960 }
3961 
3962 /*
3963  * If the last argument is non-NULL allow a <addr>/<n> syntax and
3964  * pass out <n> in *plenp.
3965  * If <n> doesn't parse return BAD_ADDR as *plenp.
3966  * If no /<n> is present return NO_PREFIX as *plenp.
3967  */
3968 static void
3969 in_getaddr(char *s, struct sockaddr *saddr, int *plenp)
3970 {
3971 	/* LINTED: alignment */
3972 	struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
3973 	struct hostent *hp;
3974 	struct netent *np;
3975 	char str[BUFSIZ];
3976 	int error_num;
3977 
3978 	(void) strncpy(str, s, sizeof (str));
3979 
3980 	/*
3981 	 * Look for '/'<n> is plenp
3982 	 */
3983 	if (plenp != NULL) {
3984 		char *cp;
3985 
3986 		*plenp = in_getprefixlen(str, _B_TRUE, IP_ABITS);
3987 		if (*plenp == BAD_ADDR)
3988 			return;
3989 		cp = strchr(str, '/');
3990 		if (cp != NULL)
3991 			*cp = '\0';
3992 	} else if (strchr(str, '/') != NULL) {
3993 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
3994 		exit(1);
3995 	}
3996 
3997 	(void) memset(sin, 0, sizeof (*sin));
3998 
3999 	/*
4000 	 *	Try to catch attempts to set the broadcast address to all 1's.
4001 	 */
4002 	if (strcmp(str, "255.255.255.255") == 0 ||
4003 	    (strtoul(str, (char **)NULL, 0) == 0xffffffffUL)) {
4004 		sin->sin_family = AF_INET;
4005 		sin->sin_addr.s_addr = 0xffffffff;
4006 		return;
4007 	}
4008 
4009 	hp = getipnodebyname(str, AF_INET, 0, &error_num);
4010 	if (hp) {
4011 		sin->sin_family = hp->h_addrtype;
4012 		(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
4013 		freehostent(hp);
4014 		return;
4015 	}
4016 	np = getnetbyname(str);
4017 	if (np) {
4018 		sin->sin_family = np->n_addrtype;
4019 		sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
4020 		return;
4021 	}
4022 	if (error_num == TRY_AGAIN) {
4023 		(void) fprintf(stderr, "ifconfig: %s: bad address "
4024 		    "(try again later)\n", s);
4025 	} else {
4026 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
4027 	}
4028 	exit(1);
4029 }
4030 
4031 /*
4032  * If the last argument is non-NULL allow a <addr>/<n> syntax and
4033  * pass out <n> in *plenp.
4034  * If <n> doesn't parse return BAD_ADDR as *plenp.
4035  * If no /<n> is present return NO_PREFIX as *plenp.
4036  */
4037 static void
4038 in6_getaddr(char *s, struct sockaddr *saddr, int *plenp)
4039 {
4040 	/* LINTED: alignment */
4041 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)saddr;
4042 	struct hostent *hp;
4043 	char str[BUFSIZ];
4044 	int error_num;
4045 
4046 	(void) strncpy(str, s, sizeof (str));
4047 
4048 	/*
4049 	 * Look for '/'<n> is plenp
4050 	 */
4051 	if (plenp != NULL) {
4052 		char *cp;
4053 
4054 		*plenp = in_getprefixlen(str, _B_TRUE, IPV6_ABITS);
4055 		if (*plenp == BAD_ADDR)
4056 			return;
4057 		cp = strchr(str, '/');
4058 		if (cp != NULL)
4059 			*cp = '\0';
4060 	} else if (strchr(str, '/') != NULL) {
4061 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
4062 		exit(1);
4063 	}
4064 
4065 	(void) memset(sin6, 0, sizeof (*sin6));
4066 
4067 	hp = getipnodebyname(str, AF_INET6, 0, &error_num);
4068 	if (hp) {
4069 		sin6->sin6_family = hp->h_addrtype;
4070 		(void) memcpy(&sin6->sin6_addr, hp->h_addr, hp->h_length);
4071 		freehostent(hp);
4072 		return;
4073 	}
4074 	if (error_num == TRY_AGAIN) {
4075 		(void) fprintf(stderr, "ifconfig: %s: bad address "
4076 		    "(try again later)\n", s);
4077 	} else {
4078 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
4079 	}
4080 	exit(1);
4081 }
4082 
4083 /*
4084  * If "slash" is zero this parses the whole string as
4085  * an integer. With "slash" non zero it parses the tail part as an integer.
4086  *
4087  * If it is not a valid integer this returns BAD_ADDR.
4088  * If there is /<n> present this returns NO_PREFIX.
4089  */
4090 static int
4091 in_getprefixlen(char *addr, boolean_t slash, int max_plen)
4092 {
4093 	int prefixlen;
4094 	char *str, *end;
4095 
4096 	if (slash) {
4097 		str = strchr(addr, '/');
4098 		if (str == NULL)
4099 			return (NO_PREFIX);
4100 		str++;
4101 	} else
4102 		str = addr;
4103 
4104 	prefixlen = strtol(str, &end, 10);
4105 	if (prefixlen < 0)
4106 		return (BAD_ADDR);
4107 	if (str == end)
4108 		return (BAD_ADDR);
4109 	if (max_plen != 0 && max_plen < prefixlen)
4110 		return (BAD_ADDR);
4111 	return (prefixlen);
4112 }
4113 
4114 /*
4115  * Convert a prefix length to a mask.
4116  * Returns 1 if ok. 0 otherwise.
4117  * Assumes the mask array is zero'ed by the caller.
4118  */
4119 static boolean_t
4120 in_prefixlentomask(int prefixlen, int maxlen, uchar_t *mask)
4121 {
4122 	if (prefixlen < 0 || prefixlen > maxlen)
4123 		return (0);
4124 
4125 	while (prefixlen > 0) {
4126 		if (prefixlen >= 8) {
4127 			*mask++ = 0xFF;
4128 			prefixlen -= 8;
4129 			continue;
4130 		}
4131 		*mask |= 1 << (8 - prefixlen);
4132 		prefixlen--;
4133 	}
4134 	return (1);
4135 }
4136 
4137 static void
4138 print_flags(uint64_t flags)
4139 {
4140 	boolean_t first = _B_TRUE;
4141 	int cnt, i;
4142 
4143 	(void) printf("flags=%llx", flags);
4144 	cnt = sizeof (if_flags_tbl) / sizeof (if_flags_t);
4145 	for (i = 0; i < cnt; i++) {
4146 		if (flags & if_flags_tbl[i].iff_value) {
4147 			if (first) {
4148 				(void) printf("<");
4149 				first = _B_FALSE;
4150 			} else {
4151 				/*
4152 				 * It has to be here and not with the
4153 				 * printf below because for the last one,
4154 				 * we don't want a comma before the ">".
4155 				 */
4156 				(void) printf(",");
4157 			}
4158 			(void) printf("%s", if_flags_tbl[i].iff_name);
4159 		}
4160 	}
4161 	if (!first)
4162 		(void) printf(">");
4163 }
4164 
4165 static void
4166 print_config_flags(int af, uint64_t flags)
4167 {
4168 	if_config_cmd_t *cmdp;
4169 
4170 	for (cmdp = if_config_cmd_tbl; cmdp->iff_flag != 0; cmdp++) {
4171 		if ((flags & cmdp->iff_flag) &&
4172 		    (cmdp->iff_af == AF_UNSPEC || cmdp->iff_af == af)) {
4173 			(void) printf("%s ", cmdp->iff_name);
4174 		}
4175 	}
4176 }
4177 
4178 /*
4179  * Use the configured directory lookup mechanism (e.g. files/NIS/...)
4180  * to find the network mask.  Returns true if we found one to set.
4181  *
4182  * The parameter addr_set controls whether we should get the address of
4183  * the working interface for the netmask query.  If addr_set is true,
4184  * we will use the address provided.  Otherwise, we will find the working
4185  * interface's address and use it instead.
4186  */
4187 static boolean_t
4188 in_getmask(struct sockaddr_in *saddr, boolean_t addr_set)
4189 {
4190 	struct sockaddr_in ifaddr;
4191 
4192 	/*
4193 	 * Read the address from the interface if it is not passed in.
4194 	 */
4195 	if (!addr_set) {
4196 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
4197 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
4198 			if (errno != EADDRNOTAVAIL) {
4199 				(void) fprintf(stderr, "Need net number for "
4200 				    "mask\n");
4201 			}
4202 			return (_B_FALSE);
4203 		}
4204 		ifaddr = *((struct sockaddr_in *)&lifr.lifr_addr);
4205 	} else {
4206 		ifaddr.sin_addr = saddr->sin_addr;
4207 	}
4208 	if (getnetmaskbyaddr(ifaddr.sin_addr, &saddr->sin_addr) == 0) {
4209 		saddr->sin_family = AF_INET;
4210 		return (_B_TRUE);
4211 	}
4212 	return (_B_FALSE);
4213 }
4214 
4215 static int
4216 lifnum(const char *ifname)
4217 {
4218 	const char *cp;
4219 
4220 	if ((cp = strchr(ifname, ':')) == NULL)
4221 		return (0);
4222 	else
4223 		return (atoi(cp + 1));
4224 }
4225 
4226 static void
4227 add_ni(const char *name)
4228 {
4229 	ni_t **pp;
4230 	ni_t *p;
4231 
4232 	for (pp = &ni_list; (p = *pp) != NULL; pp = &(p->ni_next)) {
4233 		if (strcmp(p->ni_name, name) == 0) {
4234 			if (debug > 2)
4235 				(void) fprintf(stderr, "'%s' is a duplicate\n",
4236 				    name);
4237 			return;
4238 		}
4239 	}
4240 
4241 	if (debug > 2)
4242 		(void) fprintf(stderr, "adding '%s'\n",
4243 		    name);
4244 
4245 	if ((p = malloc(sizeof (ni_t))) == NULL)
4246 		return;
4247 
4248 	(void) strlcpy(p->ni_name, name, sizeof (p->ni_name));
4249 	p->ni_next = NULL;
4250 
4251 	*pp = p;
4252 	num_ni++;
4253 }
4254 
4255 static boolean_t
4256 ni_entry(const char *linkname, void *arg)
4257 {
4258 	dlpi_handle_t	dh;
4259 	datalink_class_t class;
4260 
4261 	(void) dladm_name2info(arg, linkname, NULL, NULL, &class, NULL);
4262 
4263 	if (class == DATALINK_CLASS_ETHERSTUB)
4264 		return (_B_FALSE);
4265 	if (dlpi_open(linkname, &dh, 0) != DLPI_SUCCESS)
4266 		return (_B_FALSE);
4267 
4268 	add_ni(linkname);
4269 
4270 	dlpi_close(dh);
4271 	return (_B_FALSE);
4272 }
4273 
4274 /*
4275  * dhcp-related routines
4276  */
4277 
4278 static int
4279 setifdhcp(const char *caller, const char *ifname, int argc, char *argv[])
4280 {
4281 	dhcp_ipc_request_t	*request;
4282 	dhcp_ipc_reply_t	*reply	= NULL;
4283 	int			timeout = DHCP_IPC_WAIT_DEFAULT;
4284 	dhcp_ipc_type_t		type	= DHCP_START;
4285 	int			error;
4286 	boolean_t		is_primary = _B_FALSE;
4287 	boolean_t		started = _B_FALSE;
4288 
4289 	for (argv++; --argc > 0; argv++) {
4290 
4291 		if (strcmp(*argv, "primary") == 0) {
4292 			is_primary = _B_TRUE;
4293 			continue;
4294 		}
4295 
4296 		if (strcmp(*argv, "wait") == 0) {
4297 			if (--argc <= 0) {
4298 				usage();
4299 				return (DHCP_EXIT_BADARGS);
4300 			}
4301 			argv++;
4302 
4303 			if (strcmp(*argv, "forever") == 0) {
4304 				timeout = DHCP_IPC_WAIT_FOREVER;
4305 				continue;
4306 			}
4307 
4308 			if (sscanf(*argv, "%d", &timeout) != 1) {
4309 				usage();
4310 				return (DHCP_EXIT_BADARGS);
4311 			}
4312 
4313 			if (timeout < 0) {
4314 				usage();
4315 				return (DHCP_EXIT_BADARGS);
4316 			}
4317 			continue;
4318 		}
4319 
4320 		type = dhcp_string_to_request(*argv);
4321 		if (type == -1) {
4322 			usage();
4323 			return (DHCP_EXIT_BADARGS);
4324 		}
4325 	}
4326 
4327 	/*
4328 	 * Only try to start agent on start or inform; in all other cases it
4329 	 * has to already be running for anything to make sense.
4330 	 */
4331 	if (type == DHCP_START || type == DHCP_INFORM) {
4332 		if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1) {
4333 			(void) fprintf(stderr, "%s: unable to start %s\n",
4334 			    caller, DHCP_AGENT_PATH);
4335 			return (DHCP_EXIT_FAILURE);
4336 		}
4337 		started = _B_TRUE;
4338 	}
4339 
4340 	if (is_primary)
4341 		type |= DHCP_PRIMARY;
4342 
4343 	if (af != AF_INET)
4344 		type |= DHCP_V6;
4345 
4346 	request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
4347 	if (request == NULL) {
4348 		(void) fprintf(stderr, "%s: out of memory\n", caller);
4349 		return (DHCP_EXIT_SYSTEM);
4350 	}
4351 
4352 	error = dhcp_ipc_make_request(request, &reply, timeout);
4353 	if (error != 0) {
4354 		free(request);
4355 		/*
4356 		 * Re-map connect error to not under control if we didn't try a
4357 		 * start operation, as this has to be true and results in a
4358 		 * clearer message, not to mention preserving compatibility
4359 		 * with the days when we always started dhcpagent for every
4360 		 * request.
4361 		 */
4362 		if (error == DHCP_IPC_E_CONNECT && !started)
4363 			error = DHCP_IPC_E_UNKIF;
4364 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
4365 		    dhcp_ipc_strerror(error));
4366 		return (DHCP_EXIT_FAILURE);
4367 	}
4368 
4369 	error = reply->return_code;
4370 	if (error != 0) {
4371 		free(request);
4372 		free(reply);
4373 
4374 		if (error == DHCP_IPC_E_TIMEOUT && timeout == 0)
4375 			return (DHCP_EXIT_SUCCESS);
4376 
4377 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
4378 		    dhcp_ipc_strerror(error));
4379 
4380 		if (error == DHCP_IPC_E_TIMEOUT)
4381 			return (DHCP_EXIT_TIMEOUT);
4382 		else
4383 			return (DHCP_EXIT_IF_FAILURE);
4384 	}
4385 
4386 	if (DHCP_IPC_CMD(type) == DHCP_STATUS) {
4387 		(void) printf("%s", dhcp_status_hdr_string());
4388 		(void) printf("%s", dhcp_status_reply_to_string(reply));
4389 	}
4390 
4391 	free(request);
4392 	free(reply);
4393 	return (DHCP_EXIT_SUCCESS);
4394 }
4395 
4396 static void
4397 usage(void)
4398 {
4399 	(void) fprintf(stderr,
4400 	    "usage: ifconfig <interface> | -a[ 4 | 6 | D ][ u | d ][ Z ]\n");
4401 
4402 	(void) fprintf(stderr, "%s",
4403 	    "\t[ <addr_family> ]\n"
4404 	    "\t[ <address>[/<prefix_length>] [ <dest_address> ] ]\n"
4405 	    "\t[ set [ <address>][/<prefix_length>] ]"
4406 	    " [ <address>/<prefix_length>] ]\n"
4407 	    "\t[ destination <dest_address> ]\n"
4408 	    "\t[ addif <address>[/<prefix_length>]"
4409 	    "  [ <dest_address> ] ]\n"
4410 	    "\t[ removeif <address>[/<prefix_length>] ]\n"
4411 	    "\t[ arp | -arp ]\n"
4412 	    "\t[ auto-revarp ]\n"
4413 	    "\t[ broadcast <broad_addr> ]\n"
4414 	    "\t[ index <if_index> ]\n"
4415 	    "\t[ metric <n> ] [ mtu <n> ]\n"
4416 	    "\t[ netmask <mask> ]\n"
4417 	    "\t[ plumb ] [ unplumb ]\n"
4418 	    "\t[ preferred | -preferred ]\n"
4419 	    "\t[ private | -private ]\n"
4420 	    "\t[ local | -local ]\n"
4421 	    "\t[ router | -router ]\n"
4422 	    "\t[ subnet <subnet_address>]\n"
4423 	    "\t[ trailers | -trailers ]\n"
4424 	    "\t[ token <address>/<prefix_length> ]\n"
4425 	    "\t[ tsrc <tunnel_src_address> ]\n"
4426 	    "\t[ tdst <tunnel_dest_address> ]\n"
4427 	    "\t[ auth_algs <tunnel_AH_authentication_algorithm> ]\n"
4428 	    "\t[ encr_algs <tunnel_ESP_encryption_algorithm> ]\n"
4429 	    "\t[ encr_auth_algs <tunnel_ESP_authentication_algorithm> ]\n"
4430 	    "\t[ up ] [ down ]\n"
4431 	    "\t[ xmit | -xmit ]\n"
4432 	    "\t[ modlist ]\n"
4433 	    "\t[ modinsert <module_name@position> ]\n"
4434 	    "\t[ modremove <module_name@position> ]\n"
4435 	    "\t[ ipmp ]\n"
4436 	    "\t[ group <groupname>] | [ group \"\"]\n"
4437 	    "\t[ deprecated | -deprecated ]\n"
4438 	    "\t[ standby | -standby ]\n"
4439 	    "\t[ failover | -failover ]\n"
4440 	    "\t[ zone <zonename> | -zone ]\n"
4441 	    "\t[ usesrc <interface> ]\n"
4442 	    "\t[ all-zones ]\n");
4443 
4444 	(void) fprintf(stderr, "or\n");
4445 	(void) fprintf(stderr,
4446 	    "\tifconfig <interface> |  -a[ 4 | 6 | D ] [ u | d ]\n");
4447 
4448 	(void) fprintf(stderr, "%s", "\tauto-dhcp | dhcp\n"
4449 	    "\t[ wait <time> | forever ]\n\t[ primary ]\n"
4450 	    "\tstart | drop | ping | release | status | inform\n");
4451 }
4452