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