xref: /titanic_44/usr/src/cmd/cmd-inet/usr.sbin/ifconfig/ifconfig.c (revision 8339b41da2395f0525c46ceedfeb01961893ec44)
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 		if (strchr(addr, '/') == NULL) {
999 			/*
1000 			 * lifr.lifr_addr, which is updated by set_mask_lifreq()
1001 			 * will contain the right mask to use.
1002 			 */
1003 			prefixlen = mask2plen(&lifr.lifr_addr);
1004 			(void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d",
1005 			    addrstr, prefixlen);
1006 			addr = cidraddr;
1007 		} else {
1008 			addr = addrstr;
1009 		}
1010 		istatus = ipadm_set_addr(ipaddr, addr, af);
1011 		if (istatus != IPADM_SUCCESS)
1012 			ipadmerr_exit(istatus, "could not set address");
1013 		/*
1014 		 * let parser know we got a source.
1015 		 * Next address, if given, should be dest
1016 		 */
1017 		setaddr++;
1018 
1019 		/*
1020 		 * address will be set by the parser after nextarg has
1021 		 * been scanned
1022 		 */
1023 		return (0);
1024 	}
1025 
1026 	/* Tell parser that an address was set */
1027 	setaddr++;
1028 	/* save copy of netmask to restore in case of error */
1029 	(void) strncpy(lifr1.lifr_name, name, sizeof (lifr1.lifr_name));
1030 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr1) < 0)
1031 		Perror0_exit("SIOCGLIFNETMASK");
1032 	sav_netmask = lifr1.lifr_addr;
1033 
1034 	/*
1035 	 * If setting the address and not the mask, clear any existing mask
1036 	 * and the kernel will then assign the default (netmask has been set
1037 	 * to 0 in this case).  If setting both (either by using a prefix or
1038 	 * using the netmask command), set the mask first, so the address will
1039 	 * be interpreted correctly.
1040 	 */
1041 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1042 	/* lifr.lifr_addr already contains netmask from set_mask_lifreq() */
1043 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
1044 		Perror0_exit("SIOCSLIFNETMASK");
1045 
1046 	if (debug) {
1047 		char abuf[INET6_ADDRSTRLEN];
1048 		void *addr = (afp->af_af == AF_INET) ?
1049 		    (void *)&((struct sockaddr_in *)&laddr)->sin_addr :
1050 		    (void *)&((struct sockaddr_in6 *)&laddr)->sin6_addr;
1051 
1052 		(void) printf("Setting %s af %d addr %s\n",
1053 		    lifr.lifr_name, afp->af_af,
1054 		    inet_ntop(afp->af_af, addr, abuf, sizeof (abuf)));
1055 	}
1056 	lifr.lifr_addr = laddr;
1057 	lifr.lifr_addr.ss_family = afp->af_af;
1058 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
1059 		/*
1060 		 * Restore the netmask
1061 		 */
1062 		int saverr = errno;
1063 
1064 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1065 		lifr.lifr_addr = sav_netmask;
1066 		(void) ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr);
1067 		errno = saverr;
1068 		Perror0_exit("SIOCSLIFADDR");
1069 	}
1070 
1071 	return (0);
1072 }
1073 
1074 /*
1075  * The following functions are stolen from the ipseckey(1m) program.
1076  * Perhaps they should be somewhere common, but for now, we just maintain
1077  * two versions.  We do this because of the different semantics for which
1078  * algorithms we select ("requested" for ifconfig vs. "actual" for key).
1079  */
1080 
1081 static ulong_t
1082 parsenum(char *num)
1083 {
1084 	ulong_t rc;
1085 	char *end = NULL;
1086 
1087 	errno = 0;
1088 	rc = strtoul(num, &end, 0);
1089 	if (errno != 0 || end == num || *end != '\0') {
1090 		rc = (ulong_t)-1;
1091 	}
1092 
1093 	return (rc);
1094 }
1095 
1096 /*
1097  * Parse and reverse parse possible algorithm values, include numbers.
1098  * Mostly stolen from ipseckey.c. See the comments above parsenum() for why
1099  * this isn't common to ipseckey.c.
1100  *
1101  * NOTE: Static buffer in this function for the return value.  Since ifconfig
1102  *       isn't multithreaded, this isn't a huge problem.
1103  */
1104 
1105 #define	NBUF_SIZE 20	/* Enough to print a large integer. */
1106 
1107 static char *
1108 rparsealg(uint8_t alg_value, int proto_num)
1109 {
1110 	struct ipsecalgent *alg;
1111 	static char numprint[128];	/* Enough to hold an algorithm name. */
1112 
1113 	/*
1114 	 * Special cases for "any" and "none"
1115 	 * The kernel needs to be able to distinguish between "any"
1116 	 * and "none" and the APIs are underdefined in this area for auth.
1117 	 */
1118 	if (proto_num == IPSEC_PROTO_AH) {
1119 		if (alg_value == SADB_AALG_NONE)
1120 			return ("none");
1121 		if (alg_value == SADB_AALG_ANY)
1122 			return ("any");
1123 	}
1124 
1125 	alg = getipsecalgbynum(alg_value, proto_num, NULL);
1126 	if (alg != NULL) {
1127 		(void) strlcpy(numprint, alg->a_names[0], sizeof (numprint));
1128 		freeipsecalgent(alg);
1129 	} else {
1130 		(void) snprintf(numprint, sizeof (numprint), "%d", alg_value);
1131 	}
1132 
1133 	return (numprint);
1134 }
1135 
1136 static uint_t
1137 parsealg(char *algname, int proto_num)
1138 {
1139 	struct ipsecalgent *alg;
1140 	ulong_t invalue;
1141 
1142 	if (algname == NULL) {
1143 		(void) fprintf(stderr, "ifconfig: Unexpected end of command "
1144 		    "line.\n");
1145 		exit(1);
1146 	}
1147 
1148 	/*
1149 	 * Special-case "none" and "any".
1150 	 * Use strcasecmp because its length is bounded.
1151 	 */
1152 	if (strcasecmp("none", algname) == 0) {
1153 		return ((proto_num == IPSEC_PROTO_ESP) ?
1154 		    NO_ESP_EALG : NO_ESP_AALG);
1155 	}
1156 	if ((strcasecmp("any", algname) == 0) && (proto_num == IPSEC_PROTO_AH))
1157 		return (SADB_AALG_ANY);
1158 
1159 	alg = getipsecalgbyname(algname, proto_num, NULL);
1160 	if (alg != NULL) {
1161 		invalue = alg->a_alg_num;
1162 		freeipsecalgent(alg);
1163 		return ((uint_t)invalue);
1164 	}
1165 
1166 	/*
1167 	 * Since algorithms can be loaded during kernel run-time, check for
1168 	 * numeric algorithm values too.
1169 	 */
1170 	invalue = parsenum(algname);
1171 	if ((invalue & (ulong_t)0xff) == invalue)
1172 		return ((uint_t)invalue);
1173 
1174 	(void) fprintf(stderr, "ifconfig: %s algorithm type %s unknown.\n",
1175 	    (proto_num == IPSEC_PROTO_ESP) ?
1176 	    "Encryption" : "Authentication", algname);
1177 	exit(1);
1178 	/* NOTREACHED */
1179 }
1180 
1181 /*
1182  * Actual ifconfig functions to set tunnel security properties.
1183  */
1184 
1185 enum ipsec_alg_type { ESP_ENCR_ALG = 1, ESP_AUTH_ALG, AH_AUTH_ALG };
1186 
1187 static int
1188 set_tun_algs(int which_alg, int alg)
1189 {
1190 	boolean_t	encr_alg_set = _B_FALSE;
1191 	iptun_params_t	params;
1192 	dladm_status_t	status;
1193 	ipsec_req_t	*ipsr;
1194 
1195 	if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN,
1196 	    &params.iptun_param_linkid)) != DLADM_STATUS_OK)
1197 		goto done;
1198 
1199 	status = dladm_iptun_getparams(dlh, &params, DLADM_OPT_ACTIVE);
1200 	if (status != DLADM_STATUS_OK)
1201 		goto done;
1202 
1203 	ipsr = &params.iptun_param_secinfo;
1204 
1205 	/*
1206 	 * If I'm just starting off this ifconfig, I want a clean slate,
1207 	 * otherwise, I've captured the current tunnel security settings.
1208 	 * In the case of continuation, I merely add to the settings.
1209 	 */
1210 	if (!(params.iptun_param_flags & IPTUN_PARAM_SECINFO))
1211 		(void) memset(ipsr, 0, sizeof (*ipsr));
1212 
1213 	/* We're only modifying the IPsec information */
1214 	params.iptun_param_flags = IPTUN_PARAM_SECINFO;
1215 
1216 	switch (which_alg) {
1217 	case ESP_ENCR_ALG:
1218 		if (alg == NO_ESP_EALG) {
1219 			if (ipsr->ipsr_esp_auth_alg == SADB_AALG_NONE)
1220 				ipsr->ipsr_esp_req = 0;
1221 			ipsr->ipsr_esp_alg = SADB_EALG_NONE;
1222 
1223 			/* Let the user specify NULL encryption implicitly. */
1224 			if (ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) {
1225 				encr_alg_set = _B_TRUE;
1226 				ipsr->ipsr_esp_alg = SADB_EALG_NULL;
1227 			}
1228 		} else {
1229 			encr_alg_set = _B_TRUE;
1230 			ipsr->ipsr_esp_req =
1231 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
1232 			ipsr->ipsr_esp_alg = alg;
1233 		}
1234 		break;
1235 	case ESP_AUTH_ALG:
1236 		if (alg == NO_ESP_AALG) {
1237 			if ((ipsr->ipsr_esp_alg == SADB_EALG_NONE ||
1238 			    ipsr->ipsr_esp_alg == SADB_EALG_NULL) &&
1239 			    !encr_alg_set)
1240 				ipsr->ipsr_esp_req = 0;
1241 			ipsr->ipsr_esp_auth_alg = SADB_AALG_NONE;
1242 		} else {
1243 			ipsr->ipsr_esp_req =
1244 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
1245 			ipsr->ipsr_esp_auth_alg = alg;
1246 
1247 			/* Let the user specify NULL encryption implicitly. */
1248 			if (ipsr->ipsr_esp_alg == SADB_EALG_NONE &&
1249 			    !encr_alg_set)
1250 				ipsr->ipsr_esp_alg = SADB_EALG_NULL;
1251 		}
1252 		break;
1253 	case AH_AUTH_ALG:
1254 		if (alg == NO_AH_AALG) {
1255 			ipsr->ipsr_ah_req = 0;
1256 			ipsr->ipsr_auth_alg = SADB_AALG_NONE;
1257 		} else {
1258 			ipsr->ipsr_ah_req =
1259 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
1260 			ipsr->ipsr_auth_alg = alg;
1261 		}
1262 		break;
1263 		/* Will never hit DEFAULT */
1264 	}
1265 
1266 	status = dladm_iptun_modify(dlh, &params, DLADM_OPT_ACTIVE);
1267 
1268 done:
1269 	if (status != DLADM_STATUS_OK)
1270 		dladmerr_exit(status, name);
1271 	else {
1272 		ipsec_policy_set = _B_TRUE;
1273 		if ((ipsr->ipsr_esp_req != 0 &&
1274 		    ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) ||
1275 		    (ipsr->ipsr_ah_req != 0 &&
1276 		    ipsr->ipsr_auth_alg != SADB_AALG_NONE))
1277 			ipsec_auth_covered = _B_TRUE;
1278 	}
1279 	return (0);
1280 }
1281 
1282 /* ARGSUSED */
1283 static int
1284 set_tun_esp_encr_alg(char *addr, int64_t param)
1285 {
1286 	return (set_tun_algs(ESP_ENCR_ALG,
1287 	    parsealg(addr, IPSEC_PROTO_ESP)));
1288 }
1289 
1290 /* ARGSUSED */
1291 static int
1292 set_tun_esp_auth_alg(char *addr, int64_t param)
1293 {
1294 	return (set_tun_algs(ESP_AUTH_ALG,
1295 	    parsealg(addr, IPSEC_PROTO_AH)));
1296 }
1297 
1298 /* ARGSUSED */
1299 static int
1300 set_tun_ah_alg(char *addr, int64_t param)
1301 {
1302 	return (set_tun_algs(AH_AUTH_ALG,
1303 	    parsealg(addr, IPSEC_PROTO_AH)));
1304 }
1305 
1306 /* ARGSUSED */
1307 static int
1308 setifrevarp(char *arg, int64_t param)
1309 {
1310 	struct sockaddr_in	laddr;
1311 
1312 	if (afp->af_af == AF_INET6) {
1313 		(void) fprintf(stderr,
1314 		    "ifconfig: revarp not possible on IPv6 interface %s\n",
1315 		    name);
1316 		exit(1);
1317 	}
1318 	if (doifrevarp(name, &laddr)) {
1319 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1320 		laddr.sin_family = AF_INET;
1321 		(void) memcpy(&lifr.lifr_addr, &laddr, sizeof (laddr));
1322 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1323 			Perror0_exit("SIOCSLIFADDR");
1324 	}
1325 	return (0);
1326 }
1327 
1328 /* ARGSUSED */
1329 static int
1330 setifsubnet(char *addr, int64_t param)
1331 {
1332 	int prefixlen = 0;
1333 	struct	sockaddr_storage subnet;
1334 
1335 	(*afp->af_getaddr)(addr, &subnet, &prefixlen);
1336 
1337 	switch (prefixlen) {
1338 	case NO_PREFIX:
1339 		(void) fprintf(stderr,
1340 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
1341 		exit(1);
1342 		/* NOTREACHED */
1343 	case BAD_ADDR:
1344 		(void) fprintf(stderr,
1345 		    "ifconfig: Bad prefix length in %s\n", addr);
1346 		exit(1);
1347 	default:
1348 		break;
1349 	}
1350 
1351 	lifr.lifr_addr = subnet;
1352 	lifr.lifr_addrlen = prefixlen;
1353 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1354 	if (ioctl(s, SIOCSLIFSUBNET, (caddr_t)&lifr) < 0)
1355 		Perror0_exit("SIOCSLIFSUBNET");
1356 
1357 	return (0);
1358 }
1359 
1360 /* ARGSUSED */
1361 static int
1362 setifnetmask(char *addr, int64_t param)
1363 {
1364 	struct sockaddr_in netmask;
1365 
1366 	assert(afp->af_af != AF_INET6);
1367 
1368 	if (strcmp(addr, "+") == 0) {
1369 		if (!in_getmask(&netmask, _B_FALSE))
1370 			return (0);
1371 		(void) printf("Setting netmask of %s to %s\n", name,
1372 		    inet_ntoa(netmask.sin_addr));
1373 	} else {
1374 		in_getaddr(addr, (struct sockaddr *)&netmask, NULL);
1375 	}
1376 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1377 	(void) memcpy(&lifr.lifr_addr, &netmask, sizeof (netmask));
1378 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
1379 		Perror0_exit("SIOCSLIFNETMASK");
1380 	return (0);
1381 }
1382 
1383 /*
1384  * Parse '/<n>' as a netmask.
1385  */
1386 /* ARGSUSED */
1387 static int
1388 setifprefixlen(char *addr, int64_t param)
1389 {
1390 	int prefixlen;
1391 	int af = afp->af_af;
1392 
1393 	prefixlen = in_getprefixlen(addr, _B_TRUE,
1394 	    (af == AF_INET) ? IP_ABITS : IPV6_ABITS);
1395 	if (prefixlen < 0) {
1396 		(void) fprintf(stderr,
1397 		    "ifconfig: Bad prefix length in %s\n", addr);
1398 		exit(1);
1399 	}
1400 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
1401 	lifr.lifr_addr.ss_family = af;
1402 	if (af == AF_INET6) {
1403 		struct sockaddr_in6 *sin6;
1404 
1405 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
1406 		if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
1407 		    (uchar_t *)&sin6->sin6_addr)) {
1408 			(void) fprintf(stderr, "ifconfig: "
1409 			    "Bad prefix length: %d\n",
1410 			    prefixlen);
1411 			exit(1);
1412 		}
1413 	} else if (af == AF_INET) {
1414 		struct sockaddr_in *sin;
1415 
1416 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
1417 		if (!in_prefixlentomask(prefixlen, IP_ABITS,
1418 		    (uchar_t *)&sin->sin_addr)) {
1419 			(void) fprintf(stderr, "ifconfig: "
1420 			    "Bad prefix length: %d\n",
1421 			    prefixlen);
1422 			exit(1);
1423 		}
1424 	} else {
1425 		(void) fprintf(stderr, "ifconfig: setting prefix only supported"
1426 		    " for address family inet or inet6\n");
1427 		exit(1);
1428 	}
1429 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1430 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
1431 		Perror0_exit("SIOCSLIFNETMASK");
1432 	return (0);
1433 }
1434 
1435 /* ARGSUSED */
1436 static int
1437 setifbroadaddr(char *addr, int64_t param)
1438 {
1439 	struct	sockaddr_in broadaddr;
1440 
1441 	assert(afp->af_af != AF_INET6);
1442 
1443 	if (strcmp(addr, "+") == 0) {
1444 		/*
1445 		 * This doesn't set the broadcast address at all. Rather, it
1446 		 * gets, then sets the interface's address, relying on the fact
1447 		 * that resetting the address will reset the broadcast address.
1448 		 */
1449 		(void) strncpy(lifr.lifr_name, name,
1450 		    sizeof (lifr.lifr_name));
1451 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
1452 			if (errno != EADDRNOTAVAIL)
1453 				Perror0_exit("SIOCGLIFADDR");
1454 			return (0);
1455 		}
1456 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1457 			Perror0_exit("SIOCGLIFADDR");
1458 
1459 		return (0);
1460 	}
1461 	in_getaddr(addr, (struct sockaddr *)&broadaddr, NULL);
1462 
1463 	(void) memcpy(&lifr.lifr_addr, &broadaddr, sizeof (broadaddr));
1464 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1465 	if (ioctl(s, SIOCSLIFBRDADDR, (caddr_t)&lifr) < 0)
1466 		Perror0_exit("SIOCSLIFBRDADDR");
1467 	return (0);
1468 }
1469 
1470 /*
1471  * set interface destination address
1472  */
1473 /* ARGSUSED */
1474 static int
1475 setifdstaddr(char *addr, int64_t param)
1476 {
1477 	(*afp->af_getaddr)(addr, (struct sockaddr *)&lifr.lifr_addr, NULL);
1478 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1479 	if (ioctl(s, SIOCSLIFDSTADDR, (caddr_t)&lifr) < 0)
1480 		Perror0_exit("setifdstaddr: SIOCSLIFDSTADDR");
1481 	return (0);
1482 }
1483 
1484 /* ARGSUSED */
1485 static int
1486 setifflags(char *val, int64_t value)
1487 {
1488 	struct lifreq lifrl;	/* local lifreq struct */
1489 	boolean_t bringup = _B_FALSE;
1490 
1491 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1492 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0)
1493 		Perror0_exit("setifflags: SIOCGLIFFLAGS");
1494 
1495 	if (value < 0) {
1496 		value = -value;
1497 
1498 		if ((value & IFF_NOFAILOVER) && (lifr.lifr_flags & IFF_UP)) {
1499 			/*
1500 			 * The kernel does not allow administratively up test
1501 			 * addresses to be converted to data addresses.  Bring
1502 			 * the address down first, then bring it up after it's
1503 			 * been converted to a data address.
1504 			 */
1505 			lifr.lifr_flags &= ~IFF_UP;
1506 			(void) ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr);
1507 			bringup = _B_TRUE;
1508 		}
1509 
1510 		lifr.lifr_flags &= ~value;
1511 		if ((value & (IFF_UP | IFF_NOFAILOVER)) &&
1512 		    (lifr.lifr_flags & IFF_DUPLICATE)) {
1513 			/*
1514 			 * If the user is trying to mark an interface with a
1515 			 * duplicate address as "down," or convert a duplicate
1516 			 * test address to a data address, then fetch the
1517 			 * address and set it.  This will cause IP to clear
1518 			 * the IFF_DUPLICATE flag and stop the automatic
1519 			 * recovery timer.
1520 			 */
1521 			value = lifr.lifr_flags;
1522 			if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0)
1523 				(void) ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr);
1524 			lifr.lifr_flags = value;
1525 		}
1526 	} else {
1527 		lifr.lifr_flags |= value;
1528 	}
1529 
1530 	/*
1531 	 * If we're about to bring up an underlying physical IPv6 interface in
1532 	 * an IPMP group, ensure the IPv6 IPMP interface is also up.  This is
1533 	 * for backward compatibility with legacy configurations in which
1534 	 * there are no explicit hostname files for IPMP interfaces.  (For
1535 	 * IPv4, this is automatically handled by the kernel when migrating
1536 	 * the underlying interface's data address to the IPMP interface.)
1537 	 */
1538 	(void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ);
1539 
1540 	if (lifnum(lifr.lifr_name) == 0 &&
1541 	    (lifr.lifr_flags & (IFF_UP|IFF_IPV6)) == (IFF_UP|IFF_IPV6) &&
1542 	    ioctl(s, SIOCGLIFGROUPNAME, &lifrl) == 0 &&
1543 	    lifrl.lifr_groupname[0] != '\0') {
1544 		lifgroupinfo_t lifgr;
1545 
1546 		(void) strlcpy(lifgr.gi_grname, lifrl.lifr_groupname,
1547 		    LIFGRNAMSIZ);
1548 		if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1)
1549 			Perror0_exit("setifflags: SIOCGLIFGROUPINFO");
1550 
1551 		(void) strlcpy(lifrl.lifr_name, lifgr.gi_grifname, LIFNAMSIZ);
1552 		if (ioctl(s, SIOCGLIFFLAGS, &lifrl) == -1)
1553 			Perror0_exit("setifflags: SIOCGLIFFLAGS");
1554 		if (!(lifrl.lifr_flags & IFF_UP)) {
1555 			lifrl.lifr_flags |= IFF_UP;
1556 			if (ioctl(s, SIOCSLIFFLAGS, &lifrl) == -1)
1557 				Perror0_exit("setifflags: SIOCSLIFFLAGS");
1558 		}
1559 	}
1560 
1561 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1562 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0)
1563 		Perror0_exit("setifflags: SIOCSLIFFLAGS");
1564 
1565 	if (bringup) {
1566 		lifr.lifr_flags |= IFF_UP;
1567 		if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0)
1568 			Perror0_exit("setifflags: SIOCSLIFFLAGS IFF_UP");
1569 	}
1570 
1571 	return (0);
1572 }
1573 
1574 /* ARGSUSED */
1575 static int
1576 setifmetric(char *val, int64_t param)
1577 {
1578 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1579 	lifr.lifr_metric = atoi(val);
1580 	if (ioctl(s, SIOCSLIFMETRIC, (caddr_t)&lifr) < 0)
1581 		Perror0_exit("setifmetric: SIOCSLIFMETRIC");
1582 	return (0);
1583 }
1584 
1585 /* ARGSUSED */
1586 static int
1587 setifmtu(char *val, int64_t param)
1588 {
1589 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1590 	lifr.lifr_mtu = atoi(val);
1591 	if (ioctl(s, SIOCSLIFMTU, (caddr_t)&lifr) < 0)
1592 		Perror0_exit("setifmtu: SIOCSLIFMTU");
1593 	return (0);
1594 }
1595 
1596 /* ARGSUSED */
1597 static int
1598 setifindex(char *val, int64_t param)
1599 {
1600 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1601 	lifr.lifr_index = atoi(val);
1602 	if (ioctl(s, SIOCSLIFINDEX, (caddr_t)&lifr) < 0)
1603 		Perror0_exit("setifindex: SIOCSLIFINDEX");
1604 	return (0);
1605 }
1606 
1607 /* ARGSUSED */
1608 static void
1609 notifycb(dlpi_handle_t dh, dlpi_notifyinfo_t *dnip, void *arg)
1610 {
1611 }
1612 
1613 /* ARGSUSED */
1614 static int
1615 setifether(char *addr, int64_t param)
1616 {
1617 	uchar_t		*hwaddr;
1618 	int		hwaddrlen;
1619 	int		retval;
1620 	ifaddrlistx_t	*ifaddrp, *ifaddrs = NULL;
1621 	dlpi_handle_t	dh;
1622 	dlpi_notifyid_t id;
1623 
1624 	if (addr == NULL) {
1625 		ifstatus(name);
1626 		print_ifether(name);
1627 		return (0);
1628 	}
1629 
1630 	/*
1631 	 * if the IP interface in the arguments is a logical
1632 	 * interface, exit with an error now.
1633 	 */
1634 	if (strchr(name, ':') != NULL) {
1635 		(void) fprintf(stderr, "ifconfig: cannot change"
1636 		    " ethernet address of a logical interface\n");
1637 		exit(1);
1638 	}
1639 
1640 	if ((hwaddr = _link_aton(addr, &hwaddrlen)) == NULL) {
1641 		if (hwaddrlen == -1)
1642 			(void) fprintf(stderr,
1643 			    "ifconfig: bad ethernet address\n");
1644 		else
1645 			(void) fprintf(stderr, "ifconfig: malloc() failed\n");
1646 		exit(1);
1647 	}
1648 
1649 	if ((retval = dlpi_open(name, &dh, 0)) != DLPI_SUCCESS)
1650 		Perrdlpi_exit("cannot dlpi_open() link", name, retval);
1651 
1652 	retval = dlpi_enabnotify(dh, DL_NOTE_PHYS_ADDR, notifycb, NULL, &id);
1653 	if (retval == DLPI_SUCCESS) {
1654 		(void) dlpi_disabnotify(dh, id, NULL);
1655 	} else {
1656 		/*
1657 		 * This link does not support DL_NOTE_PHYS_ADDR: bring down
1658 		 * all of the addresses to flush the old hardware address
1659 		 * information out of IP.
1660 		 *
1661 		 * NOTE: Skipping this when DL_NOTE_PHYS_ADDR is supported is
1662 		 * more than an optimization: in.mpathd will set IFF_OFFLINE
1663 		 * if it's notified and the new address is a duplicate of
1664 		 * another in the group -- but the flags manipulation in
1665 		 * ifaddr_{down,up}() cannot be atomic and thus might clobber
1666 		 * IFF_OFFLINE, confusing in.mpathd.
1667 		 */
1668 		if (ifaddrlistx(name, IFF_UP, 0, &ifaddrs) == -1)
1669 			Perror2_exit(name, "cannot get address list");
1670 
1671 		ifaddrp = ifaddrs;
1672 		for (; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
1673 			if (!ifaddr_down(ifaddrp)) {
1674 				Perror2_exit(ifaddrp->ia_name,
1675 				    "cannot bring down");
1676 			}
1677 		}
1678 	}
1679 
1680 	/*
1681 	 * Change the hardware address.
1682 	 */
1683 	retval = dlpi_set_physaddr(dh, DL_CURR_PHYS_ADDR, hwaddr, hwaddrlen);
1684 	if (retval != DLPI_SUCCESS) {
1685 		(void) fprintf(stderr,
1686 		    "ifconfig: failed setting mac address on %s\n", name);
1687 	}
1688 	dlpi_close(dh);
1689 
1690 	/*
1691 	 * If any addresses were brought down before changing the hardware
1692 	 * address, bring them up again.
1693 	 */
1694 	for (ifaddrp = ifaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
1695 		if (!ifaddr_up(ifaddrp))
1696 			Perror2_exit(ifaddrp->ia_name, "cannot bring up");
1697 	}
1698 	ifaddrlistx_free(ifaddrs);
1699 
1700 	return (0);
1701 }
1702 
1703 /*
1704  * Print an interface's Ethernet address, if it has one.
1705  */
1706 static void
1707 print_ifether(const char *ifname)
1708 {
1709 	int fd;
1710 
1711 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1712 
1713 	fd = socket(AF_INET, SOCK_DGRAM, 0);
1714 	if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
1715 		/*
1716 		 * It's possible the interface is only configured for
1717 		 * IPv6; check again with AF_INET6.
1718 		 */
1719 		(void) close(fd);
1720 		fd = socket(AF_INET6, SOCK_DGRAM, 0);
1721 		if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
1722 			(void) close(fd);
1723 			return;
1724 		}
1725 	}
1726 	(void) close(fd);
1727 
1728 	/* VNI and IPMP interfaces don't have MAC addresses */
1729 	if (lifr.lifr_flags & (IFF_VIRTUAL|IFF_IPMP))
1730 		return;
1731 
1732 	/* IP tunnels also don't have Ethernet-like MAC addresses */
1733 	if (ifconfig_dladm_open(ifname, DATALINK_CLASS_IPTUN, NULL) ==
1734 	    DLADM_STATUS_OK)
1735 		return;
1736 
1737 	dlpi_print_address(ifname);
1738 }
1739 
1740 /*
1741  * static int find_all_interfaces(struct lifconf *lifcp, char **buf,
1742  *     int64_t lifc_flags)
1743  *
1744  * It finds all active data links.
1745  *
1746  * It takes in input a pointer to struct lifconf to receive interfaces
1747  * informations, a **char to hold allocated buffer, and a lifc_flags.
1748  *
1749  * Return values:
1750  *  0 = everything OK
1751  * -1 = problem
1752  */
1753 static int
1754 find_all_interfaces(struct lifconf *lifcp, char **buf, int64_t lifc_flags)
1755 {
1756 	unsigned bufsize;
1757 	int n;
1758 	ni_t *nip;
1759 	struct lifreq *lifrp;
1760 	dladm_status_t status;
1761 
1762 	if (!dlh_opened) {
1763 		status = ifconfig_dladm_open(NULL, 0, NULL);
1764 		if (status != DLADM_STATUS_OK)
1765 			dladmerr_exit(status, "unable to open dladm handle");
1766 	}
1767 
1768 	(void) dlpi_walk(ni_entry, dlh, 0);
1769 
1770 	/* Now, translate the linked list into a struct lifreq buffer */
1771 	if (num_ni == 0) {
1772 		lifcp->lifc_family = AF_UNSPEC;
1773 		lifcp->lifc_flags = lifc_flags;
1774 		lifcp->lifc_len = 0;
1775 		lifcp->lifc_buf = NULL;
1776 		return (0);
1777 	}
1778 
1779 	bufsize = num_ni * sizeof (struct lifreq);
1780 	if ((*buf = malloc(bufsize)) == NULL)
1781 		Perror0_exit("find_all_interfaces: malloc failed");
1782 
1783 	lifcp->lifc_family = AF_UNSPEC;
1784 	lifcp->lifc_flags = lifc_flags;
1785 	lifcp->lifc_len = bufsize;
1786 	lifcp->lifc_buf = *buf;
1787 
1788 	for (n = 0, lifrp = lifcp->lifc_req; n < num_ni; n++, lifrp++) {
1789 		nip = ni_list;
1790 		(void) strncpy(lifrp->lifr_name, nip->ni_name,
1791 		    sizeof (lifr.lifr_name));
1792 		ni_list = nip->ni_next;
1793 		free(nip);
1794 	}
1795 	return (0);
1796 }
1797 
1798 /*
1799  * Create the next unused logical interface using the original name
1800  * and assign the address (and mask if '/<n>' is part of the address).
1801  * Use the new logical interface for subsequent subcommands by updating
1802  * the name variable.
1803  *
1804  * This allows syntax like:
1805  *	ifconfig le0 addif 109.106.86.130 netmask + up \
1806  *	addif 109.106.86.131 netmask + up
1807  */
1808 /* ARGSUSED */
1809 static int
1810 addif(char *str, int64_t param)
1811 {
1812 	int prefixlen = 0;
1813 	struct sockaddr_storage laddr;
1814 	struct sockaddr_storage mask;
1815 	ipadm_status_t istatus;
1816 	char cidraddr[BUFSIZ];
1817 
1818 	(void) strncpy(name, origname, sizeof (name));
1819 
1820 	if (strchr(name, ':') != NULL) {
1821 		(void) fprintf(stderr,
1822 		    "ifconfig: addif: bad physical interface name %s\n",
1823 		    name);
1824 		exit(1);
1825 	}
1826 
1827 	/*
1828 	 * clear so parser will interpret next address as source followed
1829 	 * by possible dest
1830 	 */
1831 	setaddr = 0;
1832 	(*afp->af_getaddr)(str, (struct sockaddr *)&laddr, &prefixlen);
1833 
1834 	switch (prefixlen) {
1835 	case NO_PREFIX:
1836 		/* Nothing there - ok */
1837 		break;
1838 	case BAD_ADDR:
1839 		(void) fprintf(stderr,
1840 		    "ifconfig: Bad prefix length in %s\n", str);
1841 		exit(1);
1842 	default:
1843 		(void) memset(&mask, 0, sizeof (mask));
1844 		mask.ss_family = afp->af_af;
1845 		if (afp->af_af == AF_INET6) {
1846 			struct sockaddr_in6 *sin6;
1847 			sin6 = (struct sockaddr_in6 *)&mask;
1848 			if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
1849 			    (uchar_t *)&sin6->sin6_addr)) {
1850 				(void) fprintf(stderr, "ifconfig: "
1851 				    "Bad prefix length: %d\n",
1852 				    prefixlen);
1853 				exit(1);
1854 			}
1855 		} else {
1856 			struct sockaddr_in *sin;
1857 
1858 			sin = (struct sockaddr_in *)&mask;
1859 			if (!in_prefixlentomask(prefixlen, IP_ABITS,
1860 			    (uchar_t *)&sin->sin_addr)) {
1861 				(void) fprintf(stderr, "ifconfig: "
1862 				    "Bad prefix length: %d\n",
1863 				    prefixlen);
1864 				exit(1);
1865 			}
1866 		}
1867 		g_netmask_set = G_NETMASK_NIL;
1868 		break;
1869 	}
1870 
1871 	/*
1872 	 * This is a "hack" to get around the problem of SIOCLIFADDIF.  The
1873 	 * problem is that this ioctl does not include the netmask when
1874 	 * adding a logical interface.  This is the same problem described
1875 	 * in the ifconfig() comments.  To get around this problem, we first
1876 	 * add the logical interface with a 0 address.  After that, we set
1877 	 * the netmask if provided.  Finally we set the interface address.
1878 	 */
1879 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1880 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
1881 
1882 	/* Note: no need to do DAD here since the interface isn't up yet. */
1883 
1884 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0)
1885 		Perror0_exit("addif: SIOCLIFADDIF");
1886 
1887 	(void) printf("Created new logical interface %s\n",
1888 	    lifr.lifr_name);
1889 	(void) strncpy(name, lifr.lifr_name, sizeof (name));
1890 
1891 	/*
1892 	 * Check and see if any "netmask" command is used and perform the
1893 	 * necessary operation.
1894 	 */
1895 	set_mask_lifreq(&lifr, &laddr, &mask);
1896 
1897 	/* This check is temporary until libipadm supports IPMP interfaces. */
1898 	if (ifconfig_use_libipadm(s, name)) {
1899 		/*
1900 		 * We added the logical interface above before calling
1901 		 * ipadm_create_addr(), because, with IPH_LEGACY, we need
1902 		 * to do an addif for `ifconfig ce0 addif <addr>' but not for
1903 		 * `ifconfig ce0 <addr>'. libipadm does not have a flag to
1904 		 * to differentiate between these two cases. To keep it simple,
1905 		 * we always create the logical interface and pass it to
1906 		 * libipadm instead of requiring libipadm to addif for some
1907 		 * cases and not do addif for other cases.
1908 		 */
1909 		istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name,
1910 		    &ipaddr);
1911 		if (istatus != IPADM_SUCCESS)
1912 			ipadmerr_exit(istatus, "addif");
1913 
1914 		if (strchr(str, '/') == NULL) {
1915 			/*
1916 			 * lifr.lifr_addr, which is updated by set_mask_lifreq()
1917 			 * will contain the right mask to use.
1918 			 */
1919 			prefixlen = mask2plen(&lifr.lifr_addr);
1920 			(void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d",
1921 			    str, prefixlen);
1922 			str = cidraddr;
1923 		}
1924 		istatus = ipadm_set_addr(ipaddr, str, af);
1925 		if (istatus != IPADM_SUCCESS)
1926 			ipadmerr_exit(istatus, "could not set address");
1927 		setaddr++;
1928 		/*
1929 		 * address will be set by the parser after nextarg
1930 		 * has been scanned
1931 		 */
1932 		return (0);
1933 	}
1934 
1935 	/*
1936 	 * Only set the netmask if "netmask" command is used or a prefix is
1937 	 * provided.
1938 	 */
1939 	if (g_netmask_set == G_NETMASK_SET || prefixlen >= 0) {
1940 		/*
1941 		 * lifr.lifr_addr already contains netmask from
1942 		 * set_mask_lifreq().
1943 		 */
1944 		if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
1945 			Perror0_exit("addif: SIOCSLIFNETMASK");
1946 	}
1947 
1948 	/* Finally, we set the interface address. */
1949 	lifr.lifr_addr = laddr;
1950 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1951 		Perror0_exit("SIOCSLIFADDR");
1952 
1953 	/*
1954 	 * let parser know we got a source.
1955 	 * Next address, if given, should be dest
1956 	 */
1957 	setaddr++;
1958 	return (0);
1959 }
1960 
1961 /*
1962  * Remove a logical interface based on its IP address. Unlike addif
1963  * there is no '/<n>' here.
1964  * Verifies that the interface is down before it is removed.
1965  */
1966 /* ARGSUSED */
1967 static int
1968 removeif(char *str, int64_t param)
1969 {
1970 	struct sockaddr_storage laddr;
1971 	ipadm_status_t istatus;
1972 	ipadm_addr_info_t *ainfo, *ainfop;
1973 
1974 	if (strchr(name, ':') != NULL) {
1975 		(void) fprintf(stderr,
1976 		    "ifconfig: removeif: bad physical interface name %s\n",
1977 		    name);
1978 		exit(1);
1979 	}
1980 
1981 	(*afp->af_getaddr)(str, &laddr, NULL);
1982 
1983 	/*
1984 	 * Following check is temporary until libipadm supports
1985 	 * IPMP interfaces.
1986 	 */
1987 	if (!ifconfig_use_libipadm(s, name))
1988 		goto delete;
1989 
1990 	/*
1991 	 * Get all addresses and search this address among the active
1992 	 * addresses. If an address object was found, delete using
1993 	 * ipadm_delete_addr().
1994 	 */
1995 	istatus = ipadm_addr_info(iph, name, &ainfo, 0, LIFC_DEFAULT);
1996 	if (istatus != IPADM_SUCCESS)
1997 		ipadmerr_exit(istatus, "removeif");
1998 
1999 	for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop))
2000 		if (sockaddrcmp(ainfop->ia_ifa.ifa_addr, &laddr))
2001 			break;
2002 
2003 	if (ainfop != NULL && ainfop->ia_aobjname[0] != '\0') {
2004 		istatus = ipadm_delete_addr(iph, ainfop->ia_aobjname,
2005 		    IPADM_OPT_ACTIVE);
2006 		if (istatus != IPADM_SUCCESS)
2007 			ipadmerr_exit(istatus, "could not delete address");
2008 		ipadm_free_addr_info(ainfo);
2009 		return (0);
2010 	}
2011 	ipadm_free_addr_info(ainfo);
2012 
2013 delete:
2014 	/*
2015 	 * An address object for this address was not found in ipadm.
2016 	 * Delete with SIOCLIFREMOVEIF.
2017 	 */
2018 	lifr.lifr_addr = laddr;
2019 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2020 	if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) {
2021 		if (errno == EBUSY) {
2022 			/* This can only happen if ipif_id = 0 */
2023 			(void) fprintf(stderr,
2024 			    "ifconfig: removeif: can't remove interface: %s\n",
2025 			    name);
2026 			exit(1);
2027 		}
2028 		Perror0_exit("removeif: SIOCLIFREMOVEIF");
2029 	}
2030 	return (0);
2031 }
2032 
2033 /*
2034  * Set the address token for IPv6.
2035  */
2036 /* ARGSUSED */
2037 static int
2038 setiftoken(char *addr, int64_t param)
2039 {
2040 	int prefixlen = 0;
2041 	struct sockaddr_in6 token;
2042 
2043 	in6_getaddr(addr, (struct sockaddr *)&token, &prefixlen);
2044 	switch (prefixlen) {
2045 	case NO_PREFIX:
2046 		(void) fprintf(stderr,
2047 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
2048 		exit(1);
2049 		/* NOTREACHED */
2050 	case BAD_ADDR:
2051 		(void) fprintf(stderr,
2052 		    "ifconfig: Bad prefix length in %s\n", addr);
2053 		exit(1);
2054 	default:
2055 		break;
2056 	}
2057 	(void) memcpy(&lifr.lifr_addr, &token, sizeof (token));
2058 	lifr.lifr_addrlen = prefixlen;
2059 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2060 	if (ioctl(s, SIOCSLIFTOKEN, (caddr_t)&lifr) < 0)  {
2061 		Perror0_exit("setiftoken: SIOCSLIFTOKEN");
2062 	}
2063 	return (0);
2064 }
2065 
2066 /* ARGSUSED */
2067 static int
2068 setifgroupname(char *grname, int64_t param)
2069 {
2070 	lifgroupinfo_t		lifgr;
2071 	struct lifreq		lifrl;
2072 	ifaddrlistx_t		*ifaddrp, *nextifaddrp;
2073 	ifaddrlistx_t		*ifaddrs = NULL, *downaddrs = NULL;
2074 	int			af;
2075 
2076 	if (debug) {
2077 		(void) printf("Setting groupname %s on interface %s\n",
2078 		    grname, name);
2079 	}
2080 
2081 	(void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ);
2082 	(void) strlcpy(lifrl.lifr_groupname, grname, LIFGRNAMSIZ);
2083 
2084 	while (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) {
2085 		switch (errno) {
2086 		case ENOENT:
2087 			/*
2088 			 * The group doesn't yet exist; create it and repeat.
2089 			 */
2090 			af = afp->af_af;
2091 			if (create_ipmp(grname, af, NULL, _B_TRUE) == -1) {
2092 				if (errno == EEXIST)
2093 					continue;
2094 
2095 				Perror2(grname, "cannot create IPMP group");
2096 				goto fail;
2097 			}
2098 			continue;
2099 
2100 		case EALREADY:
2101 			/*
2102 			 * The interface is already in another group; must
2103 			 * remove existing membership first.
2104 			 */
2105 			lifrl.lifr_groupname[0] = '\0';
2106 			if (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) {
2107 				Perror2(name, "cannot remove existing "
2108 				    "IPMP group membership");
2109 				goto fail;
2110 			}
2111 			(void) strlcpy(lifrl.lifr_groupname, grname,
2112 			    LIFGRNAMSIZ);
2113 			continue;
2114 
2115 		case EAFNOSUPPORT:
2116 			/*
2117 			 * The group exists, but it's not configured with the
2118 			 * address families the interface needs.  Since only
2119 			 * two address families are currently supported, just
2120 			 * configure the "other" address family.  Note that we
2121 			 * may race with group deletion or creation by another
2122 			 * process (ENOENT or EEXIST); in such cases we repeat
2123 			 * our original SIOCSLIFGROUPNAME.
2124 			 */
2125 			(void) strlcpy(lifgr.gi_grname, grname, LIFGRNAMSIZ);
2126 			if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1) {
2127 				if (errno == ENOENT)
2128 					continue;
2129 
2130 				Perror2(grname, "SIOCGLIFGROUPINFO");
2131 				goto fail;
2132 			}
2133 
2134 			af = lifgr.gi_v4 ? AF_INET6 : AF_INET;
2135 			if (create_ipmp(grname, af, lifgr.gi_grifname,
2136 			    _B_TRUE) == -1) {
2137 				if (errno == EEXIST)
2138 					continue;
2139 
2140 				Perror2(grname, "cannot configure IPMP group");
2141 				goto fail;
2142 			}
2143 			continue;
2144 
2145 		case EADDRINUSE:
2146 			/*
2147 			 * Some addresses are in-use (or under control of DAD).
2148 			 * Bring them down and retry the group join operation.
2149 			 * We will bring them back up after the interface has
2150 			 * been placed in the group.
2151 			 */
2152 			if (ifaddrlistx(lifrl.lifr_name, IFF_UP|IFF_DUPLICATE,
2153 			    0, &ifaddrs) == -1) {
2154 				Perror2(grname, "cannot get address list");
2155 				goto fail;
2156 			}
2157 
2158 			ifaddrp = ifaddrs;
2159 			for (; ifaddrp != NULL; ifaddrp = nextifaddrp) {
2160 				if (!ifaddr_down(ifaddrp)) {
2161 					ifaddrs = ifaddrp;
2162 					goto fail;
2163 				}
2164 				nextifaddrp = ifaddrp->ia_next;
2165 				ifaddrp->ia_next = downaddrs;
2166 				downaddrs = ifaddrp;
2167 			}
2168 			ifaddrs = NULL;
2169 			continue;
2170 
2171 		case EADDRNOTAVAIL: {
2172 			/*
2173 			 * Some data addresses are under application control.
2174 			 * For some of these (e.g., ADDRCONF), the application
2175 			 * should remove the address, in which case we retry a
2176 			 * few times (since the application's action is not
2177 			 * atomic with respect to us) before bailing out and
2178 			 * informing the user.
2179 			 */
2180 			int ntries, nappaddr = 0;
2181 			const if_appflags_t *iap = if_appflags_tbl;
2182 
2183 			for (; iap->ia_app != NULL; iap++) {
2184 				ntries = 0;
2185 again:
2186 				if (ifaddrlistx(lifrl.lifr_name, iap->ia_flag,
2187 				    IFF_NOFAILOVER, &ifaddrs) == -1) {
2188 					(void) fprintf(stderr, "ifconfig: %s: "
2189 					    "cannot get data addresses managed "
2190 					    "by %s\n", lifrl.lifr_name,
2191 					    iap->ia_app);
2192 					goto fail;
2193 				}
2194 
2195 				if (ifaddrs == NULL)
2196 					continue;
2197 
2198 				ifaddrlistx_free(ifaddrs);
2199 				ifaddrs = NULL;
2200 
2201 				if (++ntries < iap->ia_tries) {
2202 					(void) poll(NULL, 0, 100);
2203 					goto again;
2204 				}
2205 
2206 				(void) fprintf(stderr, "ifconfig: cannot join "
2207 				    "IPMP group: %s has data addresses managed "
2208 				    "by %s\n", lifrl.lifr_name, iap->ia_app);
2209 				nappaddr++;
2210 			}
2211 			if (nappaddr > 0)
2212 				goto fail;
2213 			continue;
2214 		}
2215 		default:
2216 			Perror2(name, "SIOCSLIFGROUPNAME");
2217 			goto fail;
2218 		}
2219 	}
2220 
2221 	/*
2222 	 * If the interface being moved is under the control of `ipmgmtd(1M)'
2223 	 * dameon then we should inform the daemon about this move, so that
2224 	 * the daemon can delete the state associated with this interface.
2225 	 *
2226 	 * This workaround is needed until the IPMP support in ipadm(1M).
2227 	 */
2228 	ipadm_if_move(iph, name);
2229 
2230 	/*
2231 	 * If there were addresses that we had to bring down, it's time to
2232 	 * bring them up again.  As part of bringing them up, the kernel will
2233 	 * automatically move them to the new IPMP interface.
2234 	 */
2235 	for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
2236 		if (!ifaddr_up(ifaddrp) && errno != ENXIO) {
2237 			(void) fprintf(stderr, "ifconfig: cannot bring back up "
2238 			    "%s: %s\n", ifaddrp->ia_name, strerror(errno));
2239 		}
2240 	}
2241 	ifaddrlistx_free(downaddrs);
2242 	return (0);
2243 fail:
2244 	/*
2245 	 * Attempt to bring back up any interfaces that we downed.
2246 	 */
2247 	for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
2248 		if (!ifaddr_up(ifaddrp) && errno != ENXIO) {
2249 			(void) fprintf(stderr, "ifconfig: cannot bring back up "
2250 			    "%s: %s\n", ifaddrp->ia_name, strerror(errno));
2251 		}
2252 	}
2253 	ifaddrlistx_free(downaddrs);
2254 	ifaddrlistx_free(ifaddrs);
2255 
2256 	/*
2257 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2258 	 * into the exit status, so we're forced to explicitly exit().
2259 	 */
2260 	exit(1);
2261 	/* NOTREACHED */
2262 }
2263 
2264 static boolean_t
2265 modcheck(const char *ifname)
2266 {
2267 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2268 
2269 	if (ioctl(s, SIOCGLIFFLAGS, &lifr) < 0) {
2270 		Perror0("SIOCGLIFFLAGS");
2271 		return (_B_FALSE);
2272 	}
2273 
2274 	if (lifr.lifr_flags & IFF_IPMP) {
2275 		(void) fprintf(stderr, "ifconfig: %s: module operations not"
2276 		    " supported on IPMP interfaces\n", ifname);
2277 		return (_B_FALSE);
2278 	}
2279 	if (lifr.lifr_flags & IFF_VIRTUAL) {
2280 		(void) fprintf(stderr, "ifconfig: %s: module operations not"
2281 		    " supported on virtual IP interfaces\n", ifname);
2282 		return (_B_FALSE);
2283 	}
2284 	return (_B_TRUE);
2285 }
2286 
2287 /*
2288  * To list all the modules above a given network interface.
2289  */
2290 /* ARGSUSED */
2291 static int
2292 modlist(char *null, int64_t param)
2293 {
2294 	int muxid_fd;
2295 	int muxfd;
2296 	int ipfd_lowstr;
2297 	int arpfd_lowstr;
2298 	int num_mods;
2299 	int i;
2300 	struct str_list strlist;
2301 	int orig_arpid;
2302 
2303 	/*
2304 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2305 	 * into the exit status, so we're forced to explicitly exit().
2306 	 */
2307 	if (!modcheck(name))
2308 		exit(1);
2309 
2310 	if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
2311 	    &orig_arpid) < 0) {
2312 		return (-1);
2313 	}
2314 	if ((num_mods = ioctl(ipfd_lowstr, I_LIST, NULL)) < 0) {
2315 		Perror0("cannot I_LIST to get the number of modules");
2316 	} else {
2317 		if (debug > 0) {
2318 			(void) printf("Listing (%d) modules above %s\n",
2319 			    num_mods, name);
2320 		}
2321 
2322 		strlist.sl_nmods = num_mods;
2323 		strlist.sl_modlist = malloc(sizeof (struct str_mlist) *
2324 		    num_mods);
2325 		if (strlist.sl_modlist == NULL) {
2326 			Perror0("cannot malloc");
2327 		} else {
2328 			if (ioctl(ipfd_lowstr, I_LIST, (caddr_t)&strlist) < 0) {
2329 				Perror0("cannot I_LIST for module names");
2330 			} else {
2331 				for (i = 0; i < strlist.sl_nmods; i++) {
2332 					(void) printf("%d %s\n", i,
2333 					    strlist.sl_modlist[i].l_name);
2334 				}
2335 			}
2336 			free(strlist.sl_modlist);
2337 		}
2338 	}
2339 	return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2340 	    orig_arpid));
2341 }
2342 
2343 #define	MODINSERT_OP	'i'
2344 #define	MODREMOVE_OP	'r'
2345 
2346 /*
2347  * To insert a module to the stream of the interface.  It is just a
2348  * wrapper.  The real function is modop().
2349  */
2350 /* ARGSUSED */
2351 static int
2352 modinsert(char *arg, int64_t param)
2353 {
2354 	return (modop(arg, MODINSERT_OP));
2355 }
2356 
2357 /*
2358  * To remove a module from the stream of the interface.  It is just a
2359  * wrapper.  The real function is modop().
2360  */
2361 /* ARGSUSED */
2362 static int
2363 modremove(char *arg, int64_t param)
2364 {
2365 	return (modop(arg, MODREMOVE_OP));
2366 }
2367 
2368 /*
2369  * Helper function for mod*() functions.  It gets a fd to the lower IP
2370  * stream and I_PUNLINK's the lower stream.  It also initializes the
2371  * global variable lifr.
2372  *
2373  * Param:
2374  *	int *muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2375  *	int *muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2376  *	int *ipfd_lowstr: fd to the lower IP stream.
2377  *	int *arpfd_lowstr: fd to the lower ARP stream.
2378  *
2379  * Return:
2380  *	-1 if operation fails, 0 otherwise.
2381  *
2382  * Please see the big block comment above ifplumb() for the logic of the
2383  * PLINK/PUNLINK
2384  */
2385 static int
2386 ip_domux2fd(int *muxfd, int *muxid_fd, int *ipfd_lowstr, int *arpfd_lowstr,
2387     int *orig_arpid)
2388 {
2389 	uint64_t	flags;
2390 	char		*udp_dev_name;
2391 
2392 	*orig_arpid = 0;
2393 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2394 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2395 		Perror0_exit("status: SIOCGLIFFLAGS");
2396 	}
2397 	flags = lifr.lifr_flags;
2398 	if (flags & IFF_IPV4) {
2399 		udp_dev_name = UDP_DEV_NAME;
2400 	} else if (flags & IFF_IPV6) {
2401 		udp_dev_name = UDP6_DEV_NAME;
2402 	} else {
2403 		return (-1);
2404 	}
2405 
2406 	if ((*muxid_fd = open(udp_dev_name, O_RDWR)) < 0) {
2407 		Perror2("open", udp_dev_name);
2408 		return (-1);
2409 	}
2410 	if (ioctl(*muxid_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
2411 		Perror2("SIOCGLIFMUXID", udp_dev_name);
2412 		return (-1);
2413 	}
2414 	if (debug > 0) {
2415 		(void) printf("ARP_muxid %d IP_muxid %d\n",
2416 		    lifr.lifr_arp_muxid, lifr.lifr_ip_muxid);
2417 	}
2418 
2419 	/*
2420 	 * Use /dev/udp{,6} as the mux to avoid linkcycles.
2421 	 */
2422 	if (ipadm_open_arp_on_udp(udp_dev_name, muxfd) != IPADM_SUCCESS)
2423 		return (-1);
2424 
2425 	if (lifr.lifr_arp_muxid != 0) {
2426 		if ((*arpfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
2427 		    lifr.lifr_arp_muxid)) < 0) {
2428 			if ((errno == EINVAL) &&
2429 			    (flags & (IFF_NOARP | IFF_IPV6))) {
2430 				/*
2431 				 * Some plumbing utilities set the muxid to
2432 				 * -1 or some invalid value to signify that
2433 				 * there is no arp stream. Set the muxid to 0
2434 				 * before trying to unplumb the IP stream.
2435 				 * IP does not allow the IP stream to be
2436 				 * unplumbed if it sees a non-null arp muxid,
2437 				 * for consistency of IP-ARP streams.
2438 				 */
2439 				*orig_arpid = lifr.lifr_arp_muxid;
2440 				lifr.lifr_arp_muxid = 0;
2441 				(void) ioctl(*muxid_fd, SIOCSLIFMUXID,
2442 				    (caddr_t)&lifr);
2443 				*arpfd_lowstr = -1;
2444 			} else {
2445 				Perror0("_I_MUXID2FD");
2446 				return (-1);
2447 			}
2448 		} else if (ioctl(*muxfd, I_PUNLINK,
2449 		    lifr.lifr_arp_muxid) < 0) {
2450 			Perror2("I_PUNLINK", udp_dev_name);
2451 			return (-1);
2452 		}
2453 	} else {
2454 		*arpfd_lowstr = -1;
2455 	}
2456 
2457 	if ((*ipfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
2458 	    lifr.lifr_ip_muxid)) < 0) {
2459 		Perror0("_I_MUXID2FD");
2460 		/* Undo any changes we made */
2461 		if (*orig_arpid != 0) {
2462 			lifr.lifr_arp_muxid = *orig_arpid;
2463 			(void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
2464 		}
2465 		return (-1);
2466 	}
2467 	if (ioctl(*muxfd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) {
2468 		Perror2("I_PUNLINK", udp_dev_name);
2469 		/* Undo any changes we made */
2470 		if (*orig_arpid != 0) {
2471 			lifr.lifr_arp_muxid = *orig_arpid;
2472 			(void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
2473 		}
2474 		return (-1);
2475 	}
2476 	return (0);
2477 }
2478 
2479 /*
2480  * Helper function for mod*() functions.  It I_PLINK's back the upper and
2481  * lower IP streams.  Note that this function must be called after
2482  * ip_domux2fd().  In ip_domux2fd(), the global variable lifr is initialized
2483  * and ip_plink() needs information in lifr.  So ip_domux2fd() and ip_plink()
2484  * must be called in pairs.
2485  *
2486  * Param:
2487  *	int muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2488  *	int muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2489  *	int ipfd_lowstr: fd to the lower IP stream.
2490  *	int arpfd_lowstr: fd to the lower ARP stream.
2491  *
2492  * Return:
2493  *	-1 if operation fails, 0 otherwise.
2494  *
2495  * Please see the big block comment above ifplumb() for the logic of the
2496  * PLINK/PUNLINK
2497  */
2498 static int
2499 ip_plink(int muxfd, int muxid_fd, int ipfd_lowstr, int arpfd_lowstr,
2500     int orig_arpid)
2501 {
2502 	int ip_muxid;
2503 
2504 	ip_muxid = ioctl(muxfd, I_PLINK, ipfd_lowstr);
2505 	if (ip_muxid < 0) {
2506 		Perror2("I_PLINK", UDP_DEV_NAME);
2507 		return (-1);
2508 	}
2509 
2510 	/*
2511 	 * If there is an arp stream, plink it. If there is no
2512 	 * arp stream, then it is possible that the plumbing
2513 	 * utility could have stored any value in the arp_muxid.
2514 	 * If so, restore it from orig_arpid.
2515 	 */
2516 	if (arpfd_lowstr != -1) {
2517 		if (ioctl(muxfd, I_PLINK, arpfd_lowstr) < 0) {
2518 			Perror2("I_PLINK", UDP_DEV_NAME);
2519 			return (-1);
2520 		}
2521 	} else if (orig_arpid != 0) {
2522 		/* Undo the changes we did in ip_domux2fd */
2523 		lifr.lifr_arp_muxid = orig_arpid;
2524 		lifr.lifr_ip_muxid = ip_muxid;
2525 		(void) ioctl(muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
2526 	}
2527 
2528 	(void) close(muxfd);
2529 	(void) close(muxid_fd);
2530 	return (0);
2531 }
2532 
2533 /*
2534  * The real function to perform module insertion/removal.
2535  *
2536  * Param:
2537  *	char *arg: the argument string module_name@position
2538  *	char op: operation, either MODINSERT_OP or MODREMOVE_OP.
2539  *
2540  * Return:
2541  *	Before doing ip_domux2fd(), this function calls exit(1) in case of
2542  *	error.  After ip_domux2fd() is done, it returns -1 for error, 0
2543  *	otherwise.
2544  */
2545 static int
2546 modop(char *arg, char op)
2547 {
2548 	char *pos_p;
2549 	int muxfd;
2550 	int muxid_fd;
2551 	int ipfd_lowstr;  /* IP stream (lower stream of mux) to be plinked */
2552 	int arpfd_lowstr; /* ARP stream (lower stream of mux) to be plinked */
2553 	struct strmodconf mod;
2554 	char *at_char = "@";
2555 	char *arg_str;
2556 	int orig_arpid;
2557 
2558 	/*
2559 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2560 	 * into the exit status, so we're forced to explicitly exit().
2561 	 */
2562 	if (!modcheck(name))
2563 		exit(1);
2564 
2565 	/* Need to save the original string for -a option. */
2566 	if ((arg_str = malloc(strlen(arg) + 1)) == NULL) {
2567 		Perror0("cannot malloc");
2568 		return (-1);
2569 	}
2570 	(void) strcpy(arg_str, arg);
2571 
2572 	if (*arg_str == *at_char) {
2573 		(void) fprintf(stderr,
2574 		    "ifconfig: must supply a module name\n");
2575 		exit(1);
2576 	}
2577 	mod.mod_name = strtok(arg_str, at_char);
2578 	if (strlen(mod.mod_name) > FMNAMESZ) {
2579 		(void) fprintf(stderr, "ifconfig: module name too long: %s\n",
2580 		    mod.mod_name);
2581 		exit(1);
2582 	}
2583 
2584 	/*
2585 	 * Need to make sure that the core TCP/IP stack modules are not
2586 	 * removed.  Otherwise, "bad" things can happen.  If a module
2587 	 * is removed and inserted back, it loses its old state.  But
2588 	 * the modules above it still have the old state.  E.g. IP assumes
2589 	 * fast data path while tunnel after re-inserted assumes that it can
2590 	 * receive M_DATA only in fast data path for which it does not have
2591 	 * any state.  This is a general caveat of _I_REMOVE/_I_INSERT.
2592 	 */
2593 	if (op == MODREMOVE_OP &&
2594 	    (strcmp(mod.mod_name, ARP_MOD_NAME) == 0 ||
2595 	    strcmp(mod.mod_name, IP_MOD_NAME) == 0)) {
2596 		(void) fprintf(stderr, "ifconfig: cannot remove %s\n",
2597 		    mod.mod_name);
2598 		exit(1);
2599 	}
2600 
2601 	if ((pos_p = strtok(NULL, at_char)) == NULL) {
2602 		(void) fprintf(stderr, "ifconfig: must supply a position\n");
2603 		exit(1);
2604 	}
2605 	mod.pos = atoi(pos_p);
2606 
2607 	if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
2608 	    &orig_arpid) < 0) {
2609 		free(arg_str);
2610 		return (-1);
2611 	}
2612 	switch (op) {
2613 	case MODINSERT_OP:
2614 		if (debug > 0) {
2615 			(void) printf("Inserting module %s at %d\n",
2616 			    mod.mod_name, mod.pos);
2617 		}
2618 		if (ioctl(ipfd_lowstr, _I_INSERT, (caddr_t)&mod) < 0) {
2619 			Perror2("fail to insert module", mod.mod_name);
2620 		}
2621 		break;
2622 	case MODREMOVE_OP:
2623 		if (debug > 0) {
2624 			(void) printf("Removing module %s at %d\n",
2625 			    mod.mod_name, mod.pos);
2626 		}
2627 		if (ioctl(ipfd_lowstr, _I_REMOVE, (caddr_t)&mod) < 0) {
2628 			Perror2("fail to remove module", mod.mod_name);
2629 		}
2630 		break;
2631 	default:
2632 		/* Should never get to here. */
2633 		(void) fprintf(stderr, "Unknown operation\n");
2634 		break;
2635 	}
2636 	free(arg_str);
2637 	return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2638 	    orig_arpid));
2639 }
2640 
2641 static int
2642 modify_tun(iptun_params_t *params)
2643 {
2644 	dladm_status_t status;
2645 
2646 	if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN,
2647 	    &params->iptun_param_linkid)) == DLADM_STATUS_OK)
2648 		status = dladm_iptun_modify(dlh, params, DLADM_OPT_ACTIVE);
2649 	if (status != DLADM_STATUS_OK)
2650 		dladmerr_exit(status, name);
2651 	return (0);
2652 }
2653 
2654 /*
2655  * Set tunnel source address
2656  */
2657 /* ARGSUSED */
2658 static int
2659 setiftsrc(char *addr, int64_t param)
2660 {
2661 	iptun_params_t params;
2662 
2663 	params.iptun_param_flags = IPTUN_PARAM_LADDR;
2664 	(void) strlcpy(params.iptun_param_laddr, addr,
2665 	    sizeof (params.iptun_param_laddr));
2666 	return (modify_tun(&params));
2667 }
2668 
2669 /*
2670  * Set tunnel destination address
2671  */
2672 /* ARGSUSED */
2673 static int
2674 setiftdst(char *addr, int64_t param)
2675 {
2676 	iptun_params_t params;
2677 
2678 	params.iptun_param_flags = IPTUN_PARAM_RADDR;
2679 	(void) strlcpy(params.iptun_param_raddr, addr,
2680 	    sizeof (params.iptun_param_raddr));
2681 	return (modify_tun(&params));
2682 }
2683 
2684 static int
2685 set_tun_prop(const char *propname, char *value)
2686 {
2687 	dladm_status_t	status;
2688 	datalink_id_t	linkid;
2689 
2690 	status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid);
2691 	if (status == DLADM_STATUS_OK) {
2692 		status = dladm_set_linkprop(dlh, linkid, propname, &value, 1,
2693 		    DLADM_OPT_ACTIVE);
2694 	}
2695 	if (status != DLADM_STATUS_OK)
2696 		dladmerr_exit(status, name);
2697 	return (0);
2698 }
2699 
2700 /* Set tunnel encapsulation limit. */
2701 /* ARGSUSED */
2702 static int
2703 set_tun_encap_limit(char *arg, int64_t param)
2704 {
2705 	return (set_tun_prop("encaplimit", arg));
2706 }
2707 
2708 /* Disable encapsulation limit. */
2709 /* ARGSUSED */
2710 static int
2711 clr_tun_encap_limit(char *arg, int64_t param)
2712 {
2713 	return (set_tun_encap_limit("-1", 0));
2714 }
2715 
2716 /* Set tunnel hop limit. */
2717 /* ARGSUSED */
2718 static int
2719 set_tun_hop_limit(char *arg, int64_t param)
2720 {
2721 	return (set_tun_prop("hoplimit", arg));
2722 }
2723 
2724 /* Set zone ID */
2725 static int
2726 setzone(char *arg, int64_t param)
2727 {
2728 	zoneid_t zoneid = GLOBAL_ZONEID;
2729 
2730 	if (param == NEXTARG) {
2731 		/* zone must be active */
2732 		if ((zoneid = getzoneidbyname(arg)) == -1) {
2733 			(void) fprintf(stderr,
2734 			    "ifconfig: unknown zone '%s'\n", arg);
2735 			exit(1);
2736 		}
2737 	}
2738 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2739 	lifr.lifr_zoneid = zoneid;
2740 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
2741 		Perror0_exit("SIOCSLIFZONE");
2742 	return (0);
2743 }
2744 
2745 /* Put interface into all zones */
2746 /* ARGSUSED */
2747 static int
2748 setallzones(char *arg, int64_t param)
2749 {
2750 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2751 	lifr.lifr_zoneid = ALL_ZONES;
2752 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
2753 		Perror0_exit("SIOCSLIFZONE");
2754 	return (0);
2755 }
2756 
2757 /* Set source address to use */
2758 /* ARGSUSED */
2759 static int
2760 setifsrc(char *arg, int64_t param)
2761 {
2762 	uint_t ifindex = 0;
2763 	int rval;
2764 
2765 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2766 
2767 	/*
2768 	 * Argument can be either an interface name or "none". The latter means
2769 	 * that any previous selection is cleared.
2770 	 */
2771 
2772 	if (strchr(arg, ':') != NULL) {
2773 		(void) fprintf(stderr,
2774 		    "ifconfig: Cannot specify logical interface for usesrc \n");
2775 		exit(1);
2776 	}
2777 
2778 	rval = strcmp(arg, NONE_STR);
2779 	if (rval != 0) {
2780 		if ((ifindex = if_nametoindex(arg)) == 0) {
2781 			(void) strncpy(lifr.lifr_name, arg, LIFNAMSIZ);
2782 			Perror0_exit("Could not get interface index");
2783 		}
2784 		lifr.lifr_index = ifindex;
2785 	} else {
2786 		if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) != 0)
2787 			Perror0_exit("Not a valid usesrc consumer");
2788 		lifr.lifr_index = 0;
2789 	}
2790 
2791 	if (debug)
2792 		(void) printf("setifsrc: lifr_name %s, lifr_index %d\n",
2793 		    lifr.lifr_name, lifr.lifr_index);
2794 
2795 	if (ioctl(s, SIOCSLIFUSESRC, (caddr_t)&lifr) == -1) {
2796 		if (rval == 0)
2797 			Perror0_exit("Cannot reset usesrc group");
2798 		else
2799 			Perror0_exit("Could not set source interface");
2800 	}
2801 
2802 	return (0);
2803 }
2804 
2805 /*
2806  * Print the interface status line associated with `ifname'
2807  */
2808 static void
2809 ifstatus(const char *ifname)
2810 {
2811 	uint64_t flags;
2812 	char if_usesrc_name[LIFNAMSIZ];
2813 	char *newbuf;
2814 	int n, numifs, rval = 0;
2815 	struct lifreq *lifrp;
2816 	struct lifsrcof lifs;
2817 
2818 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2819 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2820 		Perror0_exit("status: SIOCGLIFFLAGS");
2821 	}
2822 	flags = lifr.lifr_flags;
2823 
2824 	/*
2825 	 * In V4 compatibility mode, we don't print the IFF_IPV4 flag or
2826 	 * interfaces with IFF_IPV6 set.
2827 	 */
2828 	if (v4compat) {
2829 		flags &= ~IFF_IPV4;
2830 		if (flags & IFF_IPV6)
2831 			return;
2832 	}
2833 
2834 	(void) printf("%s: ", ifname);
2835 	print_flags(flags);
2836 
2837 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2838 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
2839 		Perror0_exit("status: SIOCGLIFMETRIC");
2840 	} else {
2841 		if (lifr.lifr_metric)
2842 			(void) printf(" metric %d", lifr.lifr_metric);
2843 	}
2844 	if (ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
2845 		(void) printf(" mtu %u", lifr.lifr_mtu);
2846 
2847 	/* don't print index or zone when in compatibility mode */
2848 	if (!v4compat) {
2849 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
2850 			(void) printf(" index %d", lifr.lifr_index);
2851 		/*
2852 		 * Stack instances use GLOBAL_ZONEID for IP data structures
2853 		 * even in the non-global zone.
2854 		 */
2855 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifr) >= 0 &&
2856 		    lifr.lifr_zoneid != getzoneid() &&
2857 		    lifr.lifr_zoneid != GLOBAL_ZONEID) {
2858 			char zone_name[ZONENAME_MAX];
2859 
2860 			if (lifr.lifr_zoneid == ALL_ZONES) {
2861 				(void) printf("\n\tall-zones");
2862 			} else if (getzonenamebyid(lifr.lifr_zoneid, zone_name,
2863 			    sizeof (zone_name)) < 0) {
2864 				(void) printf("\n\tzone %d", lifr.lifr_zoneid);
2865 			} else {
2866 				(void) printf("\n\tzone %s", zone_name);
2867 			}
2868 		}
2869 	}
2870 
2871 	if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) {
2872 		lifs.lifs_ifindex = lifr.lifr_index;
2873 
2874 		/*
2875 		 * Find the number of interfaces that use this interfaces'
2876 		 * address as a source address
2877 		 */
2878 		lifs.lifs_buf = NULL;
2879 		lifs.lifs_maxlen = 0;
2880 		for (;;) {
2881 			/* The first pass will give the bufsize we need */
2882 			rval = ioctl(s, SIOCGLIFSRCOF, (char *)&lifs);
2883 			if (rval < 0) {
2884 				if (lifs.lifs_buf != NULL) {
2885 					free(lifs.lifs_buf);
2886 					lifs.lifs_buf = NULL;
2887 				}
2888 				lifs.lifs_len = 0;
2889 				break;
2890 			}
2891 			if (lifs.lifs_len <= lifs.lifs_maxlen)
2892 				break;
2893 			/* Use kernel's size + a small margin to avoid loops */
2894 			lifs.lifs_maxlen = lifs.lifs_len +
2895 			    5 * sizeof (struct lifreq);
2896 			/* For the first pass, realloc acts like malloc */
2897 			newbuf = realloc(lifs.lifs_buf, lifs.lifs_maxlen);
2898 			if (newbuf == NULL) {
2899 				if (lifs.lifs_buf != NULL) {
2900 					free(lifs.lifs_buf);
2901 					lifs.lifs_buf = NULL;
2902 				}
2903 				lifs.lifs_len = 0;
2904 				break;
2905 			}
2906 			lifs.lifs_buf = newbuf;
2907 		}
2908 
2909 
2910 		numifs = lifs.lifs_len / sizeof (struct lifreq);
2911 		if (numifs > 0) {
2912 			lifrp = lifs.lifs_req;
2913 			(void) printf("\n\tsrcof");
2914 			for (n = numifs; n > 0; n--, lifrp++) {
2915 				(void) printf(" %s", lifrp->lifr_name);
2916 			}
2917 		}
2918 
2919 		if (lifs.lifs_buf != NULL)
2920 			free(lifs.lifs_buf);
2921 	}
2922 
2923 	/* Find the interface whose source address this interface uses */
2924 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
2925 		if (lifr.lifr_index != 0) {
2926 			if (if_indextoname(lifr.lifr_index,
2927 			    if_usesrc_name) == NULL) {
2928 				(void) printf("\n\tusesrc ifIndex %d",
2929 				    lifr.lifr_index);
2930 			} else {
2931 				(void) printf("\n\tusesrc %s", if_usesrc_name);
2932 			}
2933 		}
2934 	}
2935 
2936 	(void) putchar('\n');
2937 }
2938 
2939 /*
2940  * Print the status of the interface.  If an address family was
2941  * specified, show it and it only; otherwise, show them all.
2942  */
2943 static void
2944 status(void)
2945 {
2946 	struct afswtch	*p = afp;
2947 	uint64_t	flags;
2948 	datalink_id_t	linkid;
2949 
2950 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2951 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2952 		Perror0_exit("status: SIOCGLIFFLAGS");
2953 	}
2954 
2955 	flags = lifr.lifr_flags;
2956 
2957 	/*
2958 	 * Only print the interface status if the address family matches
2959 	 * the interface family flag.
2960 	 */
2961 	if (p != NULL) {
2962 		if (((p->af_af == AF_INET6) && (flags & IFF_IPV4)) ||
2963 		    ((p->af_af == AF_INET) && (flags & IFF_IPV6)))
2964 			return;
2965 	}
2966 
2967 	/*
2968 	 * In V4 compatibility mode, don't print IFF_IPV6 interfaces.
2969 	 */
2970 	if (v4compat && (flags & IFF_IPV6))
2971 		return;
2972 
2973 	ifstatus(name);
2974 
2975 	if (ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid) ==
2976 	    DLADM_STATUS_OK)
2977 		tun_status(linkid);
2978 
2979 	if (p != NULL) {
2980 		(*p->af_status)(1, flags);
2981 	} else {
2982 		for (p = afs; p->af_name; p++) {
2983 			/* set global af for use in p->af_status */
2984 			af = p->af_af;
2985 			(*p->af_status)(0, flags);
2986 		}
2987 
2988 		/*
2989 		 * Historically, 'ether' has been an address family,
2990 		 * so print it here.
2991 		 */
2992 		print_ifether(name);
2993 	}
2994 }
2995 
2996 /*
2997  * Print the status of the interface in a format that can be used to
2998  * reconfigure the interface later. Code stolen from status() above.
2999  */
3000 /* ARGSUSED */
3001 static int
3002 configinfo(char *null, int64_t param)
3003 {
3004 	char *cp;
3005 	struct afswtch *p = afp;
3006 	uint64_t flags;
3007 	char lifname[LIFNAMSIZ];
3008 	char if_usesrc_name[LIFNAMSIZ];
3009 
3010 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3011 
3012 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
3013 		Perror0_exit("status: SIOCGLIFFLAGS");
3014 	}
3015 	flags = lifr.lifr_flags;
3016 
3017 	if (debug) {
3018 		(void) printf("configinfo: name %s flags  0x%llx af_af %d\n",
3019 		    name, flags, p != NULL ? p->af_af : -1);
3020 	}
3021 
3022 	/*
3023 	 * Build the interface name to print (we can't directly use `name'
3024 	 * because one cannot "plumb" ":0" interfaces).
3025 	 */
3026 	(void) strlcpy(lifname, name, LIFNAMSIZ);
3027 	if ((cp = strchr(lifname, ':')) != NULL && atoi(cp + 1) == 0)
3028 		*cp = '\0';
3029 
3030 	/*
3031 	 * if the interface is IPv4
3032 	 *	if we have a IPv6 address family restriction return
3033 	 *		so it won't print
3034 	 *	if we are in IPv4 compatibility mode, clear out IFF_IPV4
3035 	 *		so we don't print it.
3036 	 */
3037 	if (flags & IFF_IPV4) {
3038 		if (p && p->af_af == AF_INET6)
3039 			return (-1);
3040 		if (v4compat)
3041 			flags &= ~IFF_IPV4;
3042 
3043 		(void) printf("%s inet plumb", lifname);
3044 	} else if (flags & IFF_IPV6) {
3045 		/*
3046 		 * else if the interface is IPv6
3047 		 *	if we have a IPv4 address family restriction return
3048 		 *	or we are in IPv4 compatibiltiy mode, return.
3049 		 */
3050 		if (p && p->af_af == AF_INET)
3051 			return (-1);
3052 		if (v4compat)
3053 			return (-1);
3054 
3055 		(void) printf("%s inet6 plumb", lifname);
3056 	}
3057 
3058 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3059 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
3060 		Perror0_exit("configinfo: SIOCGLIFMETRIC");
3061 	} else {
3062 		if (lifr.lifr_metric)
3063 			(void) printf(" metric %d ", lifr.lifr_metric);
3064 	}
3065 	if (((flags & (IFF_VIRTUAL|IFF_LOOPBACK)) != IFF_VIRTUAL) &&
3066 	    ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
3067 		(void) printf(" mtu %d", lifr.lifr_metric);
3068 
3069 	/* Index only applies to the zeroth interface */
3070 	if (lifnum(name) == 0) {
3071 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
3072 			(void) printf(" index %d", lifr.lifr_index);
3073 	}
3074 
3075 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
3076 		if (lifr.lifr_index != 0) {
3077 			if (if_indextoname(lifr.lifr_index,
3078 			    if_usesrc_name) != NULL) {
3079 				(void) printf(" usesrc %s", if_usesrc_name);
3080 			}
3081 		}
3082 	}
3083 
3084 	if (p != NULL) {
3085 		(*p->af_configinfo)(1, flags);
3086 	} else {
3087 		for (p = afs; p->af_name; p++) {
3088 			(void) close(s);
3089 			s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0);
3090 			/* set global af for use in p->af_configinfo */
3091 			af = p->af_af;
3092 			if (s == -1) {
3093 				Perror0_exit("socket");
3094 			}
3095 			(*p->af_configinfo)(0, flags);
3096 		}
3097 	}
3098 
3099 	(void) putchar('\n');
3100 	return (0);
3101 }
3102 
3103 static void
3104 print_tsec(iptun_params_t *params)
3105 {
3106 	ipsec_req_t *ipsr;
3107 
3108 	(void) printf("\ttunnel security settings  ");
3109 	if (!(params->iptun_param_flags & IPTUN_PARAM_SECINFO)) {
3110 		(void) printf("-->  use 'ipsecconf -ln -i %s'", name);
3111 	} else {
3112 		ipsr = &params->iptun_param_secinfo;
3113 		if (ipsr->ipsr_ah_req & IPSEC_PREF_REQUIRED) {
3114 			(void) printf("ah (%s)  ",
3115 			    rparsealg(ipsr->ipsr_auth_alg, IPSEC_PROTO_AH));
3116 		}
3117 		if (ipsr->ipsr_esp_req & IPSEC_PREF_REQUIRED) {
3118 			(void) printf("esp (%s",
3119 			    rparsealg(ipsr->ipsr_esp_alg, IPSEC_PROTO_ESP));
3120 			(void) printf("/%s)",
3121 			    rparsealg(ipsr->ipsr_esp_auth_alg, IPSEC_PROTO_AH));
3122 		}
3123 	}
3124 	(void) printf("\n");
3125 }
3126 
3127 static void
3128 tun_status(datalink_id_t linkid)
3129 {
3130 	iptun_params_t	params;
3131 	char		propval[DLADM_PROP_VAL_MAX];
3132 	char		*valptr[1];
3133 	uint_t		valcnt = 1;
3134 	boolean_t	tabbed = _B_FALSE;
3135 
3136 	params.iptun_param_linkid = linkid;
3137 
3138 	/* If dladm_iptun_getparams() fails, assume we are not a tunnel. */
3139 	assert(dlh_opened);
3140 	if (dladm_iptun_getparams(dlh, &params, DLADM_OPT_ACTIVE) !=
3141 	    DLADM_STATUS_OK)
3142 		return;
3143 
3144 	switch (params.iptun_param_type) {
3145 	case IPTUN_TYPE_IPV4:
3146 	case IPTUN_TYPE_6TO4:
3147 		(void) printf("\tinet");
3148 		break;
3149 	case IPTUN_TYPE_IPV6:
3150 		(void) printf("\tinet6");
3151 		break;
3152 	default:
3153 		dladmerr_exit(DLADM_STATUS_IPTUNTYPE, name);
3154 		break;
3155 	}
3156 
3157 	/*
3158 	 * There is always a source address.  If it hasn't been explicitly
3159 	 * set, the API will pass back a buffer containing the unspecified
3160 	 * address.
3161 	 */
3162 	(void) printf(" tunnel src %s ", params.iptun_param_laddr);
3163 
3164 	if (params.iptun_param_flags & IPTUN_PARAM_RADDR)
3165 		(void) printf("tunnel dst %s\n", params.iptun_param_raddr);
3166 	else
3167 		(void) putchar('\n');
3168 
3169 	if (params.iptun_param_flags & IPTUN_PARAM_IPSECPOL)
3170 		print_tsec(&params);
3171 
3172 	valptr[0] = propval;
3173 	if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT, "hoplimit",
3174 	    (char **)valptr, &valcnt) == DLADM_STATUS_OK) {
3175 		(void) printf("\ttunnel hop limit %s ", propval);
3176 		tabbed = _B_TRUE;
3177 	}
3178 
3179 	if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT,
3180 	    "encaplimit", (char **)valptr, &valcnt) == DLADM_STATUS_OK) {
3181 		uint32_t elim;
3182 
3183 		if (!tabbed) {
3184 			(void) putchar('\t');
3185 			tabbed = _B_TRUE;
3186 		}
3187 		elim = strtol(propval, NULL, 10);
3188 		if (elim > 0)
3189 			(void) printf("tunnel encapsulation limit %s", propval);
3190 		else
3191 			(void) printf("tunnel encapsulation limit disabled");
3192 	}
3193 
3194 	if (tabbed)
3195 		(void) putchar('\n');
3196 }
3197 
3198 static void
3199 in_status(int force, uint64_t flags)
3200 {
3201 	struct sockaddr_in	*sin, *laddr;
3202 	struct sockaddr_in	netmask = { AF_INET };
3203 
3204 	if (debug)
3205 		(void) printf("in_status(%s) flags 0x%llx\n", name, flags);
3206 
3207 	/* only print status for IPv4 interfaces */
3208 	if (!(flags & IFF_IPV4))
3209 		return;
3210 
3211 	if (!(flags & IFF_NOLOCAL)) {
3212 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3213 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3214 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3215 			    errno == ENXIO) {
3216 				if (!force)
3217 					return;
3218 				(void) memset(&lifr.lifr_addr, 0,
3219 				    sizeof (lifr.lifr_addr));
3220 			} else
3221 				Perror0_exit("in_status: SIOCGLIFADDR");
3222 		}
3223 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3224 		(void) printf("\tinet %s ", inet_ntoa(sin->sin_addr));
3225 		laddr = sin;
3226 	} else {
3227 		(void) printf("\tinet ");
3228 	}
3229 
3230 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3231 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3232 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3233 		    errno == ENXIO) {
3234 			if (!force)
3235 				return;
3236 			(void) memset(&lifr.lifr_addr, 0,
3237 			    sizeof (lifr.lifr_addr));
3238 		} else {
3239 			Perror0_exit("in_status: SIOCGLIFSUBNET");
3240 		}
3241 	}
3242 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
3243 	if ((flags & IFF_NOLOCAL) ||
3244 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
3245 		(void) printf("subnet %s/%d ", inet_ntoa(sin->sin_addr),
3246 		    lifr.lifr_addrlen);
3247 	}
3248 	if (sin->sin_family != AF_INET) {
3249 		(void) printf("Wrong family: %d\n", sin->sin_family);
3250 	}
3251 
3252 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3253 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
3254 		if (errno != EADDRNOTAVAIL)
3255 			Perror0_exit("in_status: SIOCGLIFNETMASK");
3256 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
3257 	} else
3258 		netmask.sin_addr =
3259 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
3260 	if (flags & IFF_POINTOPOINT) {
3261 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3262 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3263 			if (errno == EADDRNOTAVAIL)
3264 				(void) memset(&lifr.lifr_addr, 0,
3265 				    sizeof (lifr.lifr_addr));
3266 			else
3267 				Perror0_exit("in_status: SIOCGLIFDSTADDR");
3268 		}
3269 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
3270 		(void) printf("--> %s ", inet_ntoa(sin->sin_addr));
3271 	}
3272 	(void) printf("netmask %x ", ntohl(netmask.sin_addr.s_addr));
3273 	if (flags & IFF_BROADCAST) {
3274 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3275 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
3276 			if (errno == EADDRNOTAVAIL)
3277 				(void) memset(&lifr.lifr_addr, 0,
3278 				    sizeof (lifr.lifr_addr));
3279 			else
3280 				Perror0_exit("in_status: SIOCGLIFBRDADDR");
3281 		}
3282 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3283 		if (sin->sin_addr.s_addr != 0) {
3284 			(void) printf("broadcast %s",
3285 			    inet_ntoa(sin->sin_addr));
3286 		}
3287 	}
3288 	/* If there is a groupname, print it for only the physical interface */
3289 	if (strchr(name, ':') == NULL) {
3290 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3291 		    lifr.lifr_groupname[0] != '\0') {
3292 			(void) printf("\n\tgroupname %s", lifr.lifr_groupname);
3293 		}
3294 	}
3295 	(void) putchar('\n');
3296 }
3297 
3298 static void
3299 in6_status(int force, uint64_t flags)
3300 {
3301 	char			abuf[INET6_ADDRSTRLEN];
3302 	struct sockaddr_in6	*sin6, *laddr6;
3303 
3304 	if (debug)
3305 		(void) printf("in6_status(%s) flags 0x%llx\n", name, flags);
3306 
3307 	if (!(flags & IFF_IPV6))
3308 		return;
3309 
3310 	if (!(flags & IFF_NOLOCAL)) {
3311 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3312 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3313 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3314 			    errno == ENXIO) {
3315 				if (!force)
3316 					return;
3317 				(void) memset(&lifr.lifr_addr, 0,
3318 				    sizeof (lifr.lifr_addr));
3319 			} else
3320 				Perror0_exit("in_status6: SIOCGLIFADDR");
3321 		}
3322 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3323 		(void) printf("\tinet6 %s/%d ",
3324 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3325 		    abuf, sizeof (abuf)),
3326 		    lifr.lifr_addrlen);
3327 		laddr6 = sin6;
3328 	} else {
3329 		(void) printf("\tinet6 ");
3330 	}
3331 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3332 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3333 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3334 		    errno == ENXIO) {
3335 			if (!force)
3336 				return;
3337 			(void) memset(&lifr.lifr_addr, 0,
3338 			    sizeof (lifr.lifr_addr));
3339 		} else
3340 			Perror0_exit("in_status6: SIOCGLIFSUBNET");
3341 	}
3342 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3343 	if ((flags & IFF_NOLOCAL) ||
3344 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
3345 		(void) printf("subnet %s/%d ",
3346 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3347 		    abuf, sizeof (abuf)),
3348 		    lifr.lifr_addrlen);
3349 	}
3350 	if (sin6->sin6_family != AF_INET6) {
3351 		(void) printf("Wrong family: %d\n", sin6->sin6_family);
3352 	}
3353 	if (flags & IFF_POINTOPOINT) {
3354 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3355 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3356 			if (errno == EADDRNOTAVAIL)
3357 				(void) memset(&lifr.lifr_addr, 0,
3358 				    sizeof (lifr.lifr_addr));
3359 			else
3360 				Perror0_exit("in_status6: SIOCGLIFDSTADDR");
3361 		}
3362 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
3363 		(void) printf("--> %s ",
3364 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3365 		    abuf, sizeof (abuf)));
3366 	}
3367 	if (verbose) {
3368 		(void) putchar('\n');
3369 		(void) putchar('\t');
3370 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3371 		if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
3372 			if (errno == EADDRNOTAVAIL || errno == EINVAL)
3373 				(void) memset(&lifr.lifr_addr, 0,
3374 				    sizeof (lifr.lifr_addr));
3375 			else
3376 				Perror0_exit("in_status6: SIOCGLIFTOKEN");
3377 		} else {
3378 			sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3379 			(void) printf("token %s/%d ",
3380 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3381 			    abuf, sizeof (abuf)),
3382 			    lifr.lifr_addrlen);
3383 		}
3384 		if (ioctl(s, SIOCGLIFLNKINFO, (caddr_t)&lifr) < 0) {
3385 			if (errno != EINVAL) {
3386 				Perror0_exit("in_status6: SIOCGLIFLNKINFO");
3387 			}
3388 		} else {
3389 			(void) printf("maxhops %u, reachtime %u ms, "
3390 			    "reachretrans %u ms, maxmtu %u ",
3391 			    lifr.lifr_ifinfo.lir_maxhops,
3392 			    lifr.lifr_ifinfo.lir_reachtime,
3393 			    lifr.lifr_ifinfo.lir_reachretrans,
3394 			    lifr.lifr_ifinfo.lir_maxmtu);
3395 		}
3396 	}
3397 	/* If there is a groupname, print it for only the physical interface */
3398 	if (strchr(name, ':') == NULL) {
3399 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3400 		    lifr.lifr_groupname[0] != '\0') {
3401 			(void) printf("\n\tgroupname %s", lifr.lifr_groupname);
3402 		}
3403 	}
3404 	(void) putchar('\n');
3405 }
3406 
3407 static void
3408 in_configinfo(int force, uint64_t flags)
3409 {
3410 	struct sockaddr_in *sin, *laddr;
3411 	struct	sockaddr_in netmask = { AF_INET };
3412 
3413 	if (debug)
3414 		(void) printf("in_configinfo(%s) flags 0x%llx\n", name, flags);
3415 
3416 	/* only configinfo info for IPv4 interfaces */
3417 	if (!(flags & IFF_IPV4))
3418 		return;
3419 
3420 	if (!(flags & IFF_NOLOCAL)) {
3421 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3422 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3423 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3424 			    errno == ENXIO) {
3425 				if (!force)
3426 					return;
3427 				(void) memset(&lifr.lifr_addr, 0,
3428 				    sizeof (lifr.lifr_addr));
3429 			} else
3430 				Perror0_exit("in_configinfo: SIOCGLIFADDR");
3431 		}
3432 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3433 		(void) printf(" set %s ", inet_ntoa(sin->sin_addr));
3434 		laddr = sin;
3435 	}
3436 
3437 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3438 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3439 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3440 		    errno == ENXIO) {
3441 			if (!force)
3442 				return;
3443 			(void) memset(&lifr.lifr_addr, 0,
3444 			    sizeof (lifr.lifr_addr));
3445 		} else {
3446 			Perror0_exit("in_configinfo: SIOCGLIFSUBNET");
3447 		}
3448 	}
3449 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
3450 
3451 	if ((flags & IFF_NOLOCAL) ||
3452 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
3453 		(void) printf(" subnet %s/%d ", inet_ntoa(sin->sin_addr),
3454 		    lifr.lifr_addrlen);
3455 	}
3456 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3457 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
3458 		if (errno != EADDRNOTAVAIL)
3459 			Perror0_exit("in_configinfo: SIOCGLIFNETMASK");
3460 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
3461 	} else
3462 		netmask.sin_addr =
3463 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
3464 	if (flags & IFF_POINTOPOINT) {
3465 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3466 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3467 			if (errno == EADDRNOTAVAIL)
3468 				(void) memset(&lifr.lifr_addr, 0,
3469 				    sizeof (lifr.lifr_addr));
3470 			else
3471 				Perror0_exit("in_configinfo: SIOCGLIFDSTADDR");
3472 		}
3473 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
3474 		(void) printf(" destination %s ", inet_ntoa(sin->sin_addr));
3475 	}
3476 	(void) printf(" netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
3477 	if (flags & IFF_BROADCAST) {
3478 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3479 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
3480 			if (errno == EADDRNOTAVAIL)
3481 				(void) memset(&lifr.lifr_addr, 0,
3482 				    sizeof (lifr.lifr_addr));
3483 			else
3484 				Perror0_exit("in_configinfo: SIOCGLIFBRDADDR");
3485 		}
3486 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3487 		if (sin->sin_addr.s_addr != 0) {
3488 			(void) printf(" broadcast %s ",
3489 			    inet_ntoa(sin->sin_addr));
3490 		}
3491 	}
3492 
3493 	/* If there is a groupname, print it for only the zeroth interface */
3494 	if (lifnum(name) == 0) {
3495 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3496 		    lifr.lifr_groupname[0] != '\0') {
3497 			(void) printf(" group %s ", lifr.lifr_groupname);
3498 		}
3499 	}
3500 
3501 	/* Print flags to configure */
3502 	print_config_flags(AF_INET, flags);
3503 }
3504 
3505 static void
3506 in6_configinfo(int force, uint64_t flags)
3507 {
3508 	char abuf[INET6_ADDRSTRLEN];
3509 	struct sockaddr_in6 *sin6, *laddr6;
3510 
3511 	if (debug)
3512 		(void) printf("in6_configinfo(%s) flags 0x%llx\n", name,
3513 		    flags);
3514 
3515 	if (!(flags & IFF_IPV6))
3516 		return;
3517 
3518 	if (!(flags & IFF_NOLOCAL)) {
3519 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3520 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3521 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3522 			    errno == ENXIO) {
3523 				if (!force)
3524 					return;
3525 				(void) memset(&lifr.lifr_addr, 0,
3526 				    sizeof (lifr.lifr_addr));
3527 			} else
3528 				Perror0_exit("in6_configinfo: SIOCGLIFADDR");
3529 		}
3530 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3531 		(void) printf(" set %s/%d ",
3532 		    inet_ntop(AF_INET6, &sin6->sin6_addr, abuf, sizeof (abuf)),
3533 		    lifr.lifr_addrlen);
3534 		laddr6 = sin6;
3535 	}
3536 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3537 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3538 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3539 		    errno == ENXIO) {
3540 			if (!force)
3541 				return;
3542 			(void) memset(&lifr.lifr_addr, 0,
3543 			    sizeof (lifr.lifr_addr));
3544 		} else
3545 			Perror0_exit("in6_configinfo: SIOCGLIFSUBNET");
3546 	}
3547 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3548 	if ((flags & IFF_NOLOCAL) ||
3549 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
3550 		(void) printf(" subnet %s/%d ",
3551 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3552 		    abuf, sizeof (abuf)),
3553 		    lifr.lifr_addrlen);
3554 	}
3555 
3556 	if (flags & IFF_POINTOPOINT) {
3557 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3558 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3559 			if (errno == EADDRNOTAVAIL)
3560 				(void) memset(&lifr.lifr_addr, 0,
3561 				    sizeof (lifr.lifr_addr));
3562 			else
3563 				Perror0_exit("in6_configinfo: SIOCGLIFDSTADDR");
3564 		}
3565 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
3566 		(void) printf(" destination %s ",
3567 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3568 		    abuf, sizeof (abuf)));
3569 	}
3570 
3571 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3572 	if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
3573 		if (errno == EADDRNOTAVAIL || errno == EINVAL)
3574 			(void) memset(&lifr.lifr_addr, 0,
3575 			    sizeof (lifr.lifr_addr));
3576 		else
3577 			Perror0_exit("in6_configinfo: SIOCGLIFTOKEN");
3578 	} else {
3579 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3580 		(void) printf(" token %s/%d ",
3581 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3582 		    abuf, sizeof (abuf)),
3583 		    lifr.lifr_addrlen);
3584 	}
3585 
3586 	/* If there is a groupname, print it for only the zeroth interface */
3587 	if (lifnum(name) == 0) {
3588 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3589 		    lifr.lifr_groupname[0] != '\0') {
3590 			(void) printf(" group %s ", lifr.lifr_groupname);
3591 		}
3592 	}
3593 
3594 	/* Print flags to configure */
3595 	print_config_flags(AF_INET6, flags);
3596 }
3597 
3598 /*
3599  * If this is a physical interface then remove it.
3600  * If it is a logical interface name use SIOCLIFREMOVEIF to
3601  * remove it. In both cases fail if it doesn't exist.
3602  */
3603 /* ARGSUSED */
3604 static int
3605 inetunplumb(char *arg, int64_t param)
3606 {
3607 	ipadm_status_t	istatus;
3608 
3609 	istatus = ipadm_delete_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE);
3610 	if (istatus != IPADM_SUCCESS) {
3611 		(void) fprintf(stderr, "ifconfig: cannot unplumb %s: %s\n",
3612 		    name, ipadm_status2str(istatus));
3613 		exit(1);
3614 	}
3615 
3616 	return (0);
3617 }
3618 
3619 /*
3620  * Create the interface in `name', using ipadm_create_if(). If `name' is a
3621  * logical interface or loopback interface, ipadm_create_if() uses
3622  * SIOCLIFADDIF to create it.
3623  */
3624 /* ARGSUSED */
3625 static int
3626 inetplumb(char *arg, int64_t param)
3627 {
3628 	ipadm_status_t	istatus;
3629 
3630 	istatus = ipadm_create_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE);
3631 	if (istatus != IPADM_SUCCESS) {
3632 		(void) fprintf(stderr, "ifconfig: cannot plumb %s: %s\n",
3633 		    name, ipadm_status2str(istatus));
3634 		if (istatus != IPADM_IF_EXISTS)
3635 			exit(1);
3636 	}
3637 	return (0);
3638 }
3639 
3640 /* ARGSUSED */
3641 static int
3642 inetipmp(char *arg, int64_t param)
3643 {
3644 	int retval;
3645 
3646 	/*
3647 	 * Treat e.g. "ifconfig ipmp0:2 ipmp" as "ifconfig ipmp0:2 plumb".
3648 	 * Otherwise, try to create the requested IPMP interface.
3649 	 */
3650 	if (strchr(name, ':') != NULL)
3651 		retval = inetplumb(arg, param);
3652 	else
3653 		retval = create_ipmp(name, afp->af_af, name, _B_FALSE);
3654 
3655 	/*
3656 	 * We'd return -1, but foreachinterface() doesn't propagate the error
3657 	 * into the exit status, so we're forced to explicitly exit().
3658 	 */
3659 	if (retval == -1)
3660 		exit(1);
3661 	return (0);
3662 }
3663 
3664 /*
3665  * Create an IPMP group `grname' with address family `af'.  If `ifname' is
3666  * non-NULL, it specifies the interface name to use.  Otherwise, use the name
3667  * ipmpN, where N corresponds to the lowest available integer.  If `implicit'
3668  * is set, then the group is being created as a side-effect of placing an
3669  * underlying interface in a group.  Also start in.mpathd if necessary.
3670  */
3671 static int
3672 create_ipmp(const char *grname, int af, const char *ifname, boolean_t implicit)
3673 {
3674 	static int ipmp_daemon_started;
3675 	uint32_t flags = IPADM_OPT_IPMP|IPADM_OPT_ACTIVE;
3676 	ipadm_status_t istatus;
3677 
3678 	if (debug) {
3679 		(void) printf("create_ipmp: ifname %s grname %s af %d\n",
3680 		    ifname != NULL ? ifname : "NULL", grname, af);
3681 	}
3682 
3683 	/*
3684 	 * ipadm_create_if() creates the IPMP interface and fills in the
3685 	 * ppa in lifr.lifr_name, if `ifname'="ipmp".
3686 	 */
3687 	(void) strlcpy(lifr.lifr_name, (ifname ? ifname : "ipmp"),
3688 	    sizeof (lifr.lifr_name));
3689 	if (ifname == NULL)
3690 		flags |= IPADM_OPT_GENPPA;
3691 	istatus = ipadm_create_if(iph, lifr.lifr_name, af, flags);
3692 	if (istatus != IPADM_SUCCESS) {
3693 		(void) fprintf(stderr, "ifconfig: cannot create IPMP interface "
3694 		    "%s: %s\n", grname, ipadm_status2str(istatus));
3695 		return (-1);
3696 	}
3697 
3698 	/*
3699 	 * To preserve backward-compatibility, always bring up the link-local
3700 	 * address for implicitly-created IPv6 IPMP interfaces.
3701 	 */
3702 	if (implicit && af == AF_INET6) {
3703 		if (ioctl(s6, SIOCGLIFFLAGS, &lifr) == 0) {
3704 			lifr.lifr_flags |= IFF_UP;
3705 			(void) ioctl(s6, SIOCSLIFFLAGS, &lifr);
3706 		}
3707 	}
3708 
3709 	/*
3710 	 * If the caller requested a different group name, issue a
3711 	 * SIOCSLIFGROUPNAME on the new IPMP interface.
3712 	 */
3713 	if (strcmp(lifr.lifr_name, grname) != 0) {
3714 		(void) strlcpy(lifr.lifr_groupname, grname, LIFGRNAMSIZ);
3715 		if (ioctl(s, SIOCSLIFGROUPNAME, &lifr) == -1) {
3716 			Perror0("SIOCSLIFGROUPNAME");
3717 			return (-1);
3718 		}
3719 	}
3720 
3721 	/*
3722 	 * If we haven't done so yet, ensure in.mpathd is started.
3723 	 */
3724 	if (ipmp_daemon_started++ == 0)
3725 		start_ipmp_daemon();
3726 
3727 	return (0);
3728 }
3729 
3730 /*
3731  * Start in.mpathd if it's not already running.
3732  */
3733 static void
3734 start_ipmp_daemon(void)
3735 {
3736 	int retval;
3737 	ipmp_handle_t ipmp_handle;
3738 
3739 	/*
3740 	 * Ping in.mpathd to see if it's running already.
3741 	 */
3742 	if ((retval = ipmp_open(&ipmp_handle)) != IPMP_SUCCESS) {
3743 		(void) fprintf(stderr, "ifconfig: cannot create IPMP handle: "
3744 		    "%s\n", ipmp_errmsg(retval));
3745 		return;
3746 	}
3747 
3748 	retval = ipmp_ping_daemon(ipmp_handle);
3749 	ipmp_close(ipmp_handle);
3750 
3751 	switch (retval) {
3752 	case IPMP_ENOMPATHD:
3753 		break;
3754 	case IPMP_SUCCESS:
3755 		return;
3756 	default:
3757 		(void) fprintf(stderr, "ifconfig: cannot ping in.mpathd: %s\n",
3758 		    ipmp_errmsg(retval));
3759 		break;
3760 	}
3761 
3762 	/*
3763 	 * Start in.mpathd.  Note that in.mpathd will handle multiple
3764 	 * incarnations (ipmp_ping_daemon() is just an optimization) so we
3765 	 * don't need to worry about racing with another ifconfig process.
3766 	 */
3767 	switch (fork()) {
3768 	case -1:
3769 		Perror0_exit("start_ipmp_daemon: fork");
3770 		/* NOTREACHED */
3771 	case 0:
3772 		(void) execl(MPATHD_PATH, MPATHD_PATH, NULL);
3773 		_exit(1);
3774 		/* NOTREACHED */
3775 	default:
3776 		break;
3777 	}
3778 }
3779 
3780 /*
3781  * Bring the address named by `ifaddrp' up or down.  Doesn't trust any mutable
3782  * values in ia_flags since they may be stale.
3783  */
3784 static boolean_t
3785 ifaddr_op(ifaddrlistx_t *ifaddrp, boolean_t up)
3786 {
3787 	struct lifreq	lifrl;	/* Local lifreq struct */
3788 	int		fd = (ifaddrp->ia_flags & IFF_IPV4) ? s4 : s6;
3789 
3790 	(void) memset(&lifrl, 0, sizeof (lifrl));
3791 	(void) strlcpy(lifrl.lifr_name, ifaddrp->ia_name, LIFNAMSIZ);
3792 	if (ioctl(fd, SIOCGLIFFLAGS, &lifrl) == -1)
3793 		return (_B_FALSE);
3794 
3795 	if (up)
3796 		lifrl.lifr_flags |= IFF_UP;
3797 	else
3798 		lifrl.lifr_flags &= ~IFF_UP;
3799 
3800 	if (ioctl(fd, SIOCSLIFFLAGS, &lifrl) == -1)
3801 		return (_B_FALSE);
3802 
3803 	/*
3804 	 * If we're trying to bring the address down, ensure that DAD activity
3805 	 * (observable by IFF_DUPLICATE) has also been stopped.
3806 	 */
3807 	if (!up && ioctl(fd, SIOCGLIFFLAGS, &lifrl) != -1 &&
3808 	    lifrl.lifr_flags & IFF_DUPLICATE) {
3809 		if (ioctl(fd, SIOCGLIFADDR, &lifrl) == -1 ||
3810 		    ioctl(fd, SIOCSLIFADDR, &lifrl) == -1) {
3811 			return (_B_FALSE);
3812 		}
3813 	}
3814 	return (_B_TRUE);
3815 }
3816 
3817 static boolean_t
3818 ifaddr_up(ifaddrlistx_t *ifaddrp)
3819 {
3820 	return (ifaddr_op(ifaddrp, _B_TRUE));
3821 }
3822 
3823 static boolean_t
3824 ifaddr_down(ifaddrlistx_t *ifaddrp)
3825 {
3826 	return (ifaddr_op(ifaddrp, _B_FALSE));
3827 }
3828 
3829 /*
3830  * Open the global libdladm handle "dlh" if it isn't already opened.  The
3831  * caller may optionally supply a link name to obtain its linkid.  If a link
3832  * of a specific class or classes is required, reqclass specifies the class
3833  * mask.
3834  */
3835 static dladm_status_t
3836 ifconfig_dladm_open(const char *name, datalink_class_t reqclass,
3837     datalink_id_t *linkid)
3838 {
3839 	dladm_status_t status = DLADM_STATUS_OK;
3840 	datalink_class_t class;
3841 
3842 	if (!dlh_opened) {
3843 		if ((status = dladm_open(&dlh)) != DLADM_STATUS_OK)
3844 			return (status);
3845 		dlh_opened = _B_TRUE;
3846 	}
3847 	if (name != NULL) {
3848 		status = dladm_name2info(dlh, name, linkid, NULL, &class, NULL);
3849 		if (status == DLADM_STATUS_OK) {
3850 			if (!(class & reqclass))
3851 				status = DLADM_STATUS_LINKINVAL;
3852 		}
3853 	}
3854 	return (status);
3855 }
3856 
3857 /*
3858  * This function checks if we can use libipadm API's. We will only
3859  * call libipadm functions for non-IPMP interfaces. This check is
3860  * temporary until libipadm supports IPMP interfaces.
3861  */
3862 static boolean_t
3863 ifconfig_use_libipadm(int s, const char *lifname)
3864 {
3865 	struct lifreq lifr1;
3866 
3867 	(void) strlcpy(lifr1.lifr_name, lifname, sizeof (lifr1.lifr_name));
3868 	if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr1) < 0) {
3869 		(void) strncpy(lifr.lifr_name, lifname,
3870 		    sizeof (lifr.lifr_name));
3871 		Perror0_exit("error");
3872 	}
3873 
3874 	return (lifr1.lifr_groupname[0] == '\0');
3875 }
3876 
3877 static void
3878 ipadmerr_exit(ipadm_status_t status, const char *str)
3879 {
3880 	(void) fprintf(stderr, "ifconfig: %s: %s\n", str,
3881 	    ipadm_status2str(status));
3882 	exit(1);
3883 }
3884 
3885 static void
3886 dladmerr_exit(dladm_status_t status, const char *str)
3887 {
3888 	char errstr[DLADM_STRSIZE];
3889 
3890 	(void) fprintf(stderr, "%s: %s\n", str,
3891 	    dladm_status2str(status, errstr));
3892 	exit(1);
3893 }
3894 
3895 void
3896 Perror0(const char *cmd)
3897 {
3898 	Perror2(cmd, lifr.lifr_name);
3899 }
3900 
3901 void
3902 Perror0_exit(const char *cmd)
3903 {
3904 	Perror0(cmd);
3905 	exit(1);
3906 }
3907 
3908 void
3909 Perror2(const char *cmd, const char *str)
3910 {
3911 	int error = errno;
3912 
3913 	(void) fprintf(stderr, "ifconfig: %s: ", cmd);
3914 
3915 	switch (error) {
3916 	case ENXIO:
3917 		(void) fprintf(stderr, "%s: no such interface\n", str);
3918 		break;
3919 	case EPERM:
3920 		(void) fprintf(stderr, "%s: permission denied\n", str);
3921 		break;
3922 	case EEXIST:
3923 		(void) fprintf(stderr, "%s: already exists\n", str);
3924 		break;
3925 	case ENAMETOOLONG:
3926 		(void) fprintf(stderr, "%s: interface name too long\n", str);
3927 		break;
3928 	case ERANGE:
3929 		(void) fprintf(stderr, "%s: logical interface id is outside "
3930 		    "allowed range\n", str);
3931 		break;
3932 	default:
3933 		errno = error;
3934 		perror(str);
3935 	}
3936 }
3937 
3938 /*
3939  * Print out error message (Perror2()) and exit
3940  */
3941 void
3942 Perror2_exit(const char *cmd, const char *str)
3943 {
3944 	Perror2(cmd, str);
3945 	exit(1);
3946 	/* NOTREACHED */
3947 }
3948 
3949 void
3950 Perrdlpi(const char *cmd, const char *linkname, int err)
3951 {
3952 	(void) fprintf(stderr, "ifconfig: %s \"%s\": %s\n", cmd,
3953 	    linkname, dlpi_strerror(err));
3954 }
3955 
3956 /*
3957  * Print out error message (Perrdlpi()) and exit
3958  */
3959 void
3960 Perrdlpi_exit(const char *cmd, const char *linkname, int err)
3961 {
3962 	Perrdlpi(cmd, linkname, err);
3963 	exit(1);
3964 }
3965 
3966 /*
3967  * If the last argument is non-NULL allow a <addr>/<n> syntax and
3968  * pass out <n> in *plenp.
3969  * If <n> doesn't parse return BAD_ADDR as *plenp.
3970  * If no /<n> is present return NO_PREFIX as *plenp.
3971  */
3972 static void
3973 in_getaddr(char *s, struct sockaddr *saddr, int *plenp)
3974 {
3975 	/* LINTED: alignment */
3976 	struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
3977 	struct hostent *hp;
3978 	struct netent *np;
3979 	char str[BUFSIZ];
3980 	int error_num;
3981 
3982 	(void) strncpy(str, s, sizeof (str));
3983 
3984 	/*
3985 	 * Look for '/'<n> is plenp
3986 	 */
3987 	if (plenp != NULL) {
3988 		char *cp;
3989 
3990 		*plenp = in_getprefixlen(str, _B_TRUE, IP_ABITS);
3991 		if (*plenp == BAD_ADDR)
3992 			return;
3993 		cp = strchr(str, '/');
3994 		if (cp != NULL)
3995 			*cp = '\0';
3996 	} else if (strchr(str, '/') != NULL) {
3997 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
3998 		exit(1);
3999 	}
4000 
4001 	(void) memset(sin, 0, sizeof (*sin));
4002 
4003 	/*
4004 	 *	Try to catch attempts to set the broadcast address to all 1's.
4005 	 */
4006 	if (strcmp(str, "255.255.255.255") == 0 ||
4007 	    (strtoul(str, (char **)NULL, 0) == 0xffffffffUL)) {
4008 		sin->sin_family = AF_INET;
4009 		sin->sin_addr.s_addr = 0xffffffff;
4010 		return;
4011 	}
4012 
4013 	hp = getipnodebyname(str, AF_INET, 0, &error_num);
4014 	if (hp) {
4015 		sin->sin_family = hp->h_addrtype;
4016 		(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
4017 		freehostent(hp);
4018 		return;
4019 	}
4020 	np = getnetbyname(str);
4021 	if (np) {
4022 		sin->sin_family = np->n_addrtype;
4023 		sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
4024 		return;
4025 	}
4026 	if (error_num == TRY_AGAIN) {
4027 		(void) fprintf(stderr, "ifconfig: %s: bad address "
4028 		    "(try again later)\n", s);
4029 	} else {
4030 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
4031 	}
4032 	exit(1);
4033 }
4034 
4035 /*
4036  * If the last argument is non-NULL allow a <addr>/<n> syntax and
4037  * pass out <n> in *plenp.
4038  * If <n> doesn't parse return BAD_ADDR as *plenp.
4039  * If no /<n> is present return NO_PREFIX as *plenp.
4040  */
4041 static void
4042 in6_getaddr(char *s, struct sockaddr *saddr, int *plenp)
4043 {
4044 	/* LINTED: alignment */
4045 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)saddr;
4046 	struct hostent *hp;
4047 	char str[BUFSIZ];
4048 	int error_num;
4049 
4050 	(void) strncpy(str, s, sizeof (str));
4051 
4052 	/*
4053 	 * Look for '/'<n> is plenp
4054 	 */
4055 	if (plenp != NULL) {
4056 		char *cp;
4057 
4058 		*plenp = in_getprefixlen(str, _B_TRUE, IPV6_ABITS);
4059 		if (*plenp == BAD_ADDR)
4060 			return;
4061 		cp = strchr(str, '/');
4062 		if (cp != NULL)
4063 			*cp = '\0';
4064 	} else if (strchr(str, '/') != NULL) {
4065 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
4066 		exit(1);
4067 	}
4068 
4069 	(void) memset(sin6, 0, sizeof (*sin6));
4070 
4071 	hp = getipnodebyname(str, AF_INET6, 0, &error_num);
4072 	if (hp) {
4073 		sin6->sin6_family = hp->h_addrtype;
4074 		(void) memcpy(&sin6->sin6_addr, hp->h_addr, hp->h_length);
4075 		freehostent(hp);
4076 		return;
4077 	}
4078 	if (error_num == TRY_AGAIN) {
4079 		(void) fprintf(stderr, "ifconfig: %s: bad address "
4080 		    "(try again later)\n", s);
4081 	} else {
4082 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
4083 	}
4084 	exit(1);
4085 }
4086 
4087 /*
4088  * If "slash" is zero this parses the whole string as
4089  * an integer. With "slash" non zero it parses the tail part as an integer.
4090  *
4091  * If it is not a valid integer this returns BAD_ADDR.
4092  * If there is /<n> present this returns NO_PREFIX.
4093  */
4094 static int
4095 in_getprefixlen(char *addr, boolean_t slash, int max_plen)
4096 {
4097 	int prefixlen;
4098 	char *str, *end;
4099 
4100 	if (slash) {
4101 		str = strchr(addr, '/');
4102 		if (str == NULL)
4103 			return (NO_PREFIX);
4104 		str++;
4105 	} else
4106 		str = addr;
4107 
4108 	prefixlen = strtol(str, &end, 10);
4109 	if (prefixlen < 0)
4110 		return (BAD_ADDR);
4111 	if (str == end)
4112 		return (BAD_ADDR);
4113 	if (max_plen != 0 && max_plen < prefixlen)
4114 		return (BAD_ADDR);
4115 	return (prefixlen);
4116 }
4117 
4118 /*
4119  * Convert a prefix length to a mask.
4120  * Returns 1 if ok. 0 otherwise.
4121  * Assumes the mask array is zero'ed by the caller.
4122  */
4123 static boolean_t
4124 in_prefixlentomask(int prefixlen, int maxlen, uchar_t *mask)
4125 {
4126 	if (prefixlen < 0 || prefixlen > maxlen)
4127 		return (0);
4128 
4129 	while (prefixlen > 0) {
4130 		if (prefixlen >= 8) {
4131 			*mask++ = 0xFF;
4132 			prefixlen -= 8;
4133 			continue;
4134 		}
4135 		*mask |= 1 << (8 - prefixlen);
4136 		prefixlen--;
4137 	}
4138 	return (1);
4139 }
4140 
4141 static void
4142 print_flags(uint64_t flags)
4143 {
4144 	boolean_t first = _B_TRUE;
4145 	int cnt, i;
4146 
4147 	(void) printf("flags=%llx", flags);
4148 	cnt = sizeof (if_flags_tbl) / sizeof (if_flags_t);
4149 	for (i = 0; i < cnt; i++) {
4150 		if (flags & if_flags_tbl[i].iff_value) {
4151 			if (first) {
4152 				(void) printf("<");
4153 				first = _B_FALSE;
4154 			} else {
4155 				/*
4156 				 * It has to be here and not with the
4157 				 * printf below because for the last one,
4158 				 * we don't want a comma before the ">".
4159 				 */
4160 				(void) printf(",");
4161 			}
4162 			(void) printf("%s", if_flags_tbl[i].iff_name);
4163 		}
4164 	}
4165 	if (!first)
4166 		(void) printf(">");
4167 }
4168 
4169 static void
4170 print_config_flags(int af, uint64_t flags)
4171 {
4172 	if_config_cmd_t *cmdp;
4173 
4174 	for (cmdp = if_config_cmd_tbl; cmdp->iff_flag != 0; cmdp++) {
4175 		if ((flags & cmdp->iff_flag) &&
4176 		    (cmdp->iff_af == AF_UNSPEC || cmdp->iff_af == af)) {
4177 			(void) printf("%s ", cmdp->iff_name);
4178 		}
4179 	}
4180 }
4181 
4182 /*
4183  * Use the configured directory lookup mechanism (e.g. files/NIS/...)
4184  * to find the network mask.  Returns true if we found one to set.
4185  *
4186  * The parameter addr_set controls whether we should get the address of
4187  * the working interface for the netmask query.  If addr_set is true,
4188  * we will use the address provided.  Otherwise, we will find the working
4189  * interface's address and use it instead.
4190  */
4191 static boolean_t
4192 in_getmask(struct sockaddr_in *saddr, boolean_t addr_set)
4193 {
4194 	struct sockaddr_in ifaddr;
4195 
4196 	/*
4197 	 * Read the address from the interface if it is not passed in.
4198 	 */
4199 	if (!addr_set) {
4200 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
4201 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
4202 			if (errno != EADDRNOTAVAIL) {
4203 				(void) fprintf(stderr, "Need net number for "
4204 				    "mask\n");
4205 			}
4206 			return (_B_FALSE);
4207 		}
4208 		ifaddr = *((struct sockaddr_in *)&lifr.lifr_addr);
4209 	} else {
4210 		ifaddr.sin_addr = saddr->sin_addr;
4211 	}
4212 	if (getnetmaskbyaddr(ifaddr.sin_addr, &saddr->sin_addr) == 0) {
4213 		saddr->sin_family = AF_INET;
4214 		return (_B_TRUE);
4215 	}
4216 	return (_B_FALSE);
4217 }
4218 
4219 static int
4220 lifnum(const char *ifname)
4221 {
4222 	const char *cp;
4223 
4224 	if ((cp = strchr(ifname, ':')) == NULL)
4225 		return (0);
4226 	else
4227 		return (atoi(cp + 1));
4228 }
4229 
4230 static void
4231 add_ni(const char *name)
4232 {
4233 	ni_t **pp;
4234 	ni_t *p;
4235 
4236 	for (pp = &ni_list; (p = *pp) != NULL; pp = &(p->ni_next)) {
4237 		if (strcmp(p->ni_name, name) == 0) {
4238 			if (debug > 2)
4239 				(void) fprintf(stderr, "'%s' is a duplicate\n",
4240 				    name);
4241 			return;
4242 		}
4243 	}
4244 
4245 	if (debug > 2)
4246 		(void) fprintf(stderr, "adding '%s'\n",
4247 		    name);
4248 
4249 	if ((p = malloc(sizeof (ni_t))) == NULL)
4250 		return;
4251 
4252 	(void) strlcpy(p->ni_name, name, sizeof (p->ni_name));
4253 	p->ni_next = NULL;
4254 
4255 	*pp = p;
4256 	num_ni++;
4257 }
4258 
4259 static boolean_t
4260 ni_entry(const char *linkname, void *arg)
4261 {
4262 	dlpi_handle_t	dh;
4263 	datalink_class_t class;
4264 
4265 	(void) dladm_name2info(arg, linkname, NULL, NULL, &class, NULL);
4266 
4267 	if (class == DATALINK_CLASS_ETHERSTUB)
4268 		return (_B_FALSE);
4269 	if (dlpi_open(linkname, &dh, 0) != DLPI_SUCCESS)
4270 		return (_B_FALSE);
4271 
4272 	add_ni(linkname);
4273 
4274 	dlpi_close(dh);
4275 	return (_B_FALSE);
4276 }
4277 
4278 /*
4279  * dhcp-related routines
4280  */
4281 
4282 static int
4283 setifdhcp(const char *caller, const char *ifname, int argc, char *argv[])
4284 {
4285 	dhcp_ipc_request_t	*request;
4286 	dhcp_ipc_reply_t	*reply	= NULL;
4287 	int			timeout = DHCP_IPC_WAIT_DEFAULT;
4288 	dhcp_ipc_type_t		type	= DHCP_START;
4289 	int			error;
4290 	boolean_t		is_primary = _B_FALSE;
4291 	boolean_t		started = _B_FALSE;
4292 
4293 	for (argv++; --argc > 0; argv++) {
4294 
4295 		if (strcmp(*argv, "primary") == 0) {
4296 			is_primary = _B_TRUE;
4297 			continue;
4298 		}
4299 
4300 		if (strcmp(*argv, "wait") == 0) {
4301 			if (--argc <= 0) {
4302 				usage();
4303 				return (DHCP_EXIT_BADARGS);
4304 			}
4305 			argv++;
4306 
4307 			if (strcmp(*argv, "forever") == 0) {
4308 				timeout = DHCP_IPC_WAIT_FOREVER;
4309 				continue;
4310 			}
4311 
4312 			if (sscanf(*argv, "%d", &timeout) != 1) {
4313 				usage();
4314 				return (DHCP_EXIT_BADARGS);
4315 			}
4316 
4317 			if (timeout < 0) {
4318 				usage();
4319 				return (DHCP_EXIT_BADARGS);
4320 			}
4321 			continue;
4322 		}
4323 
4324 		type = dhcp_string_to_request(*argv);
4325 		if (type == -1) {
4326 			usage();
4327 			return (DHCP_EXIT_BADARGS);
4328 		}
4329 	}
4330 
4331 	/*
4332 	 * Only try to start agent on start or inform; in all other cases it
4333 	 * has to already be running for anything to make sense.
4334 	 */
4335 	if (type == DHCP_START || type == DHCP_INFORM) {
4336 		if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1) {
4337 			(void) fprintf(stderr, "%s: unable to start %s\n",
4338 			    caller, DHCP_AGENT_PATH);
4339 			return (DHCP_EXIT_FAILURE);
4340 		}
4341 		started = _B_TRUE;
4342 	}
4343 
4344 	if (is_primary)
4345 		type |= DHCP_PRIMARY;
4346 
4347 	if (af != AF_INET)
4348 		type |= DHCP_V6;
4349 
4350 	request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
4351 	if (request == NULL) {
4352 		(void) fprintf(stderr, "%s: out of memory\n", caller);
4353 		return (DHCP_EXIT_SYSTEM);
4354 	}
4355 
4356 	error = dhcp_ipc_make_request(request, &reply, timeout);
4357 	if (error != 0) {
4358 		free(request);
4359 		/*
4360 		 * Re-map connect error to not under control if we didn't try a
4361 		 * start operation, as this has to be true and results in a
4362 		 * clearer message, not to mention preserving compatibility
4363 		 * with the days when we always started dhcpagent for every
4364 		 * request.
4365 		 */
4366 		if (error == DHCP_IPC_E_CONNECT && !started)
4367 			error = DHCP_IPC_E_UNKIF;
4368 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
4369 		    dhcp_ipc_strerror(error));
4370 		return (DHCP_EXIT_FAILURE);
4371 	}
4372 
4373 	error = reply->return_code;
4374 	if (error != 0) {
4375 		free(request);
4376 		free(reply);
4377 
4378 		if (error == DHCP_IPC_E_TIMEOUT && timeout == 0)
4379 			return (DHCP_EXIT_SUCCESS);
4380 
4381 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
4382 		    dhcp_ipc_strerror(error));
4383 
4384 		if (error == DHCP_IPC_E_TIMEOUT)
4385 			return (DHCP_EXIT_TIMEOUT);
4386 		else
4387 			return (DHCP_EXIT_IF_FAILURE);
4388 	}
4389 
4390 	if (DHCP_IPC_CMD(type) == DHCP_STATUS) {
4391 		(void) printf("%s", dhcp_status_hdr_string());
4392 		(void) printf("%s", dhcp_status_reply_to_string(reply));
4393 	}
4394 
4395 	free(request);
4396 	free(reply);
4397 	return (DHCP_EXIT_SUCCESS);
4398 }
4399 
4400 static void
4401 usage(void)
4402 {
4403 	(void) fprintf(stderr,
4404 	    "usage: ifconfig <interface> | -a[ 4 | 6 | D ][ u | d ][ Z ]\n");
4405 
4406 	(void) fprintf(stderr, "%s",
4407 	    "\t[ <addr_family> ]\n"
4408 	    "\t[ <address>[/<prefix_length>] [ <dest_address> ] ]\n"
4409 	    "\t[ set [ <address>][/<prefix_length>] ]"
4410 	    " [ <address>/<prefix_length>] ]\n"
4411 	    "\t[ destination <dest_address> ]\n"
4412 	    "\t[ addif <address>[/<prefix_length>]"
4413 	    "  [ <dest_address> ] ]\n"
4414 	    "\t[ removeif <address>[/<prefix_length>] ]\n"
4415 	    "\t[ arp | -arp ]\n"
4416 	    "\t[ auto-revarp ]\n"
4417 	    "\t[ broadcast <broad_addr> ]\n"
4418 	    "\t[ index <if_index> ]\n"
4419 	    "\t[ metric <n> ] [ mtu <n> ]\n"
4420 	    "\t[ netmask <mask> ]\n"
4421 	    "\t[ plumb ] [ unplumb ]\n"
4422 	    "\t[ preferred | -preferred ]\n"
4423 	    "\t[ private | -private ]\n"
4424 	    "\t[ local | -local ]\n"
4425 	    "\t[ router | -router ]\n"
4426 	    "\t[ subnet <subnet_address>]\n"
4427 	    "\t[ trailers | -trailers ]\n"
4428 	    "\t[ token <address>/<prefix_length> ]\n"
4429 	    "\t[ tsrc <tunnel_src_address> ]\n"
4430 	    "\t[ tdst <tunnel_dest_address> ]\n"
4431 	    "\t[ auth_algs <tunnel_AH_authentication_algorithm> ]\n"
4432 	    "\t[ encr_algs <tunnel_ESP_encryption_algorithm> ]\n"
4433 	    "\t[ encr_auth_algs <tunnel_ESP_authentication_algorithm> ]\n"
4434 	    "\t[ up ] [ down ]\n"
4435 	    "\t[ xmit | -xmit ]\n"
4436 	    "\t[ modlist ]\n"
4437 	    "\t[ modinsert <module_name@position> ]\n"
4438 	    "\t[ modremove <module_name@position> ]\n"
4439 	    "\t[ ipmp ]\n"
4440 	    "\t[ group <groupname>] | [ group \"\"]\n"
4441 	    "\t[ deprecated | -deprecated ]\n"
4442 	    "\t[ standby | -standby ]\n"
4443 	    "\t[ failover | -failover ]\n"
4444 	    "\t[ zone <zonename> | -zone ]\n"
4445 	    "\t[ usesrc <interface> ]\n"
4446 	    "\t[ all-zones ]\n");
4447 
4448 	(void) fprintf(stderr, "or\n");
4449 	(void) fprintf(stderr,
4450 	    "\tifconfig <interface> |  -a[ 4 | 6 | D ] [ u | d ]\n");
4451 
4452 	(void) fprintf(stderr, "%s", "\tauto-dhcp | dhcp\n"
4453 	    "\t[ wait <time> | forever ]\n\t[ primary ]\n"
4454 	    "\tstart | drop | ping | release | status | inform\n");
4455 }
4456