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