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