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 	dladm_handle_t dld_handle;
1757 	dladm_status_t status;
1758 	char errmsg[DLADM_STRSIZE];
1759 
1760 	if ((status = dladm_open(&dld_handle)) != DLADM_STATUS_OK) {
1761 		(void) fprintf(stderr,
1762 		    "ifconfig: find_all_global_interfaces failed: %s\n",
1763 		    dladm_status2str(status, errmsg));
1764 		return (-1);
1765 	}
1766 
1767 	(void) dlpi_walk(ni_entry, dld_handle, 0);
1768 
1769 	dladm_close(dld_handle);
1770 
1771 	/*
1772 	 * Now, translate the linked list into
1773 	 * a struct lifreq buffer
1774 	 */
1775 	if (num_ni == 0) {
1776 		lifcp->lifc_family = AF_UNSPEC;
1777 		lifcp->lifc_flags = lifc_flags;
1778 		lifcp->lifc_len = 0;
1779 		lifcp->lifc_buf = NULL;
1780 		return (0);
1781 	}
1782 
1783 	bufsize = num_ni * sizeof (struct lifreq);
1784 	if ((*buf = malloc(bufsize)) == NULL)
1785 		Perror0_exit("find_all_interfaces: malloc failed");
1786 
1787 	lifcp->lifc_family = AF_UNSPEC;
1788 	lifcp->lifc_flags = lifc_flags;
1789 	lifcp->lifc_len = bufsize;
1790 	lifcp->lifc_buf = *buf;
1791 
1792 	for (n = 0, lifrp = lifcp->lifc_req; n < num_ni; n++, lifrp++) {
1793 		nip = ni_list;
1794 		(void) strncpy(lifrp->lifr_name, nip->ni_name,
1795 		    sizeof (lifr.lifr_name));
1796 		ni_list = nip->ni_next;
1797 		free(nip);
1798 	}
1799 	return (0);
1800 }
1801 
1802 /*
1803  * static int find_all_zone_interfaces(struct lifconf *lifcp, char **buf,
1804  *     int64_t lifc_flags)
1805  *
1806  * It finds all interfaces for an exclusive-IP zone, that is all the interfaces
1807  * assigned to it.
1808  *
1809  * It takes in input a pointer to struct lifconf to receive interfaces
1810  * informations, a **char to hold allocated buffer, and a lifc_flags.
1811  *
1812  * Return values:
1813  *  0 = everything OK
1814  * -1 = problem
1815  */
1816 static int
1817 find_all_zone_interfaces(struct lifconf *lifcp, char **buf, int64_t lifc_flags)
1818 {
1819 	zoneid_t zoneid;
1820 	unsigned bufsize;
1821 	char *dlnames, *ptr;
1822 	struct lifreq *lifrp;
1823 	int num_ni_saved, i;
1824 
1825 	zoneid = getzoneid();
1826 
1827 	num_ni = 0;
1828 	if (zone_list_datalink(zoneid, &num_ni, NULL) != 0)
1829 		Perror0_exit("find_all_interfaces: list interfaces failed");
1830 again:
1831 	/* this zone doesn't have any data-links */
1832 	if (num_ni == 0) {
1833 		lifcp->lifc_family = AF_UNSPEC;
1834 		lifcp->lifc_flags = lifc_flags;
1835 		lifcp->lifc_len = 0;
1836 		lifcp->lifc_buf = NULL;
1837 		return (0);
1838 	}
1839 
1840 	dlnames = malloc(num_ni * LIFNAMSIZ);
1841 	if (dlnames == NULL)
1842 		Perror0_exit("find_all_interfaces: out of memory");
1843 	num_ni_saved = num_ni;
1844 
1845 	if (zone_list_datalink(zoneid, &num_ni, dlnames) != 0)
1846 		Perror0_exit("find_all_interfaces: list interfaces failed");
1847 
1848 	if (num_ni_saved < num_ni) {
1849 		/* list increased, try again */
1850 		free(dlnames);
1851 		goto again;
1852 	}
1853 
1854 	/* this zone doesn't have any data-links now */
1855 	if (num_ni == 0) {
1856 		free(dlnames);
1857 		lifcp->lifc_family = AF_UNSPEC;
1858 		lifcp->lifc_flags = lifc_flags;
1859 		lifcp->lifc_len = 0;
1860 		lifcp->lifc_buf = NULL;
1861 		return (0);
1862 	}
1863 
1864 	bufsize = num_ni * sizeof (struct lifreq);
1865 	if ((*buf = malloc(bufsize)) == NULL) {
1866 		free(dlnames);
1867 		Perror0_exit("find_all_interfaces: malloc failed");
1868 	}
1869 
1870 	lifrp = (struct lifreq *)*buf;
1871 	ptr = dlnames;
1872 	for (i = 0; i < num_ni; i++) {
1873 		if (strlcpy(lifrp->lifr_name, ptr, LIFNAMSIZ) >=
1874 		    LIFNAMSIZ)
1875 			Perror0_exit("find_all_interfaces: overflow");
1876 		ptr += LIFNAMSIZ;
1877 		lifrp++;
1878 	}
1879 
1880 	free(dlnames);
1881 	lifcp->lifc_family = AF_UNSPEC;
1882 	lifcp->lifc_flags = lifc_flags;
1883 	lifcp->lifc_len = bufsize;
1884 	lifcp->lifc_buf = *buf;
1885 	return (0);
1886 }
1887 
1888 /*
1889  * Create the next unused logical interface using the original name
1890  * and assign the address (and mask if '/<n>' is part of the address).
1891  * Use the new logical interface for subsequent subcommands by updating
1892  * the name variable.
1893  *
1894  * This allows syntax like:
1895  *	ifconfig le0 addif 109.106.86.130 netmask + up \
1896  *	addif 109.106.86.131 netmask + up
1897  */
1898 /* ARGSUSED */
1899 static int
1900 addif(char *str, int64_t param)
1901 {
1902 	int prefixlen = 0;
1903 	struct sockaddr_storage laddr;
1904 	struct sockaddr_storage mask;
1905 
1906 	(void) strncpy(name, origname, sizeof (name));
1907 
1908 	if (strchr(name, ':') != NULL) {
1909 		(void) fprintf(stderr,
1910 		    "ifconfig: addif: bad physical interface name %s\n",
1911 		    name);
1912 		exit(1);
1913 	}
1914 
1915 	/*
1916 	 * clear so parser will interpret next address as source followed
1917 	 * by possible dest
1918 	 */
1919 	setaddr = 0;
1920 	(*afp->af_getaddr)(str, (struct sockaddr *)&laddr, &prefixlen);
1921 
1922 	switch (prefixlen) {
1923 	case NO_PREFIX:
1924 		/* Nothing there - ok */
1925 		break;
1926 	case BAD_ADDR:
1927 		(void) fprintf(stderr,
1928 		    "ifconfig: Bad prefix length in %s\n", str);
1929 		exit(1);
1930 	default:
1931 		(void) memset(&mask, 0, sizeof (mask));
1932 		mask.ss_family = afp->af_af;
1933 		if (afp->af_af == AF_INET6) {
1934 			struct sockaddr_in6 *sin6;
1935 			sin6 = (struct sockaddr_in6 *)&mask;
1936 			if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
1937 			    (uchar_t *)&sin6->sin6_addr)) {
1938 				(void) fprintf(stderr, "ifconfig: "
1939 				    "Bad prefix length: %d\n",
1940 				    prefixlen);
1941 				exit(1);
1942 			}
1943 		} else {
1944 			struct sockaddr_in *sin;
1945 
1946 			sin = (struct sockaddr_in *)&mask;
1947 			if (!in_prefixlentomask(prefixlen, IP_ABITS,
1948 			    (uchar_t *)&sin->sin_addr)) {
1949 				(void) fprintf(stderr, "ifconfig: "
1950 				    "Bad prefix length: %d\n",
1951 				    prefixlen);
1952 				exit(1);
1953 			}
1954 		}
1955 		g_netmask_set = G_NETMASK_NIL;
1956 		break;
1957 	}
1958 
1959 	/*
1960 	 * This is a "hack" to get around the problem of SIOCLIFADDIF.  The
1961 	 * problem is that this ioctl does not include the netmask when
1962 	 * adding a logical interface.  This is the same problem described
1963 	 * in the ifconfig() comments.  To get around this problem, we first
1964 	 * add the logical interface with a 0 address.  After that, we set
1965 	 * the netmask if provided.  Finally we set the interface address.
1966 	 */
1967 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1968 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
1969 
1970 	/* Note: no need to do DAD here since the interface isn't up yet. */
1971 
1972 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0)
1973 		Perror0_exit("addif: SIOCLIFADDIF");
1974 
1975 	(void) printf("Created new logical interface %s\n",
1976 	    lifr.lifr_name);
1977 	(void) strncpy(name, lifr.lifr_name, sizeof (name));
1978 
1979 	/*
1980 	 * Check and see if any "netmask" command is used and perform the
1981 	 * necessary operation.
1982 	 */
1983 	set_mask_lifreq(&lifr, &laddr, &mask);
1984 	/*
1985 	 * Only set the netmask if "netmask" command is used or a prefix is
1986 	 * provided.
1987 	 */
1988 	if (g_netmask_set == G_NETMASK_SET || prefixlen >= 0) {
1989 		if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
1990 			Perror0_exit("addif: SIOCSLIFNETMASK");
1991 	}
1992 
1993 	/* Finally, we set the interface address. */
1994 	lifr.lifr_addr = laddr;
1995 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1996 		Perror0_exit("SIOCSLIFADDR");
1997 
1998 	/*
1999 	 * let parser know we got a source.
2000 	 * Next address, if given, should be dest
2001 	 */
2002 	setaddr++;
2003 	return (0);
2004 }
2005 
2006 /*
2007  * Remove a logical interface based on its IP address. Unlike addif
2008  * there is no '/<n>' here.
2009  * Verifies that the interface is down before it is removed.
2010  */
2011 /* ARGSUSED */
2012 static int
2013 removeif(char *str, int64_t param)
2014 {
2015 	struct sockaddr_storage laddr;
2016 
2017 	if (strchr(name, ':') != NULL) {
2018 		(void) fprintf(stderr,
2019 		    "ifconfig: removeif: bad physical interface name %s\n",
2020 		    name);
2021 		exit(1);
2022 	}
2023 
2024 	(*afp->af_getaddr)(str, &laddr, NULL);
2025 	lifr.lifr_addr = laddr;
2026 
2027 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2028 	if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) {
2029 		if (errno == EBUSY) {
2030 			/* This can only happen if ipif_id = 0 */
2031 			(void) fprintf(stderr,
2032 			    "ifconfig: removeif: can't remove interface: %s\n",
2033 			    name);
2034 			exit(1);
2035 		}
2036 		Perror0_exit("removeif: SIOCLIFREMOVEIF");
2037 	}
2038 	return (0);
2039 }
2040 
2041 /*
2042  * Set the address token for IPv6.
2043  */
2044 /* ARGSUSED */
2045 static int
2046 setiftoken(char *addr, int64_t param)
2047 {
2048 	int prefixlen = 0;
2049 	struct sockaddr_in6 token;
2050 
2051 	in6_getaddr(addr, (struct sockaddr *)&token, &prefixlen);
2052 	switch (prefixlen) {
2053 	case NO_PREFIX:
2054 		(void) fprintf(stderr,
2055 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
2056 		exit(1);
2057 		/* NOTREACHED */
2058 	case BAD_ADDR:
2059 		(void) fprintf(stderr,
2060 		    "ifconfig: Bad prefix length in %s\n", addr);
2061 		exit(1);
2062 	default:
2063 		break;
2064 	}
2065 	(void) memcpy(&lifr.lifr_addr, &token, sizeof (token));
2066 	lifr.lifr_addrlen = prefixlen;
2067 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2068 	if (ioctl(s, SIOCSLIFTOKEN, (caddr_t)&lifr) < 0)  {
2069 		Perror0_exit("setiftoken: SIOCSLIFTOKEN");
2070 	}
2071 	return (0);
2072 }
2073 
2074 /* ARGSUSED */
2075 static int
2076 setifgroupname(char *grname, int64_t param)
2077 {
2078 	lifgroupinfo_t		lifgr;
2079 	struct lifreq		lifrl;
2080 	ifaddrlistx_t		*ifaddrp, *nextifaddrp;
2081 	ifaddrlistx_t		*ifaddrs = NULL, *downaddrs = NULL;
2082 	int			af;
2083 
2084 	if (debug) {
2085 		(void) printf("Setting groupname %s on interface %s\n",
2086 		    grname, name);
2087 	}
2088 
2089 	(void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ);
2090 	(void) strlcpy(lifrl.lifr_groupname, grname, LIFGRNAMSIZ);
2091 
2092 	while (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) {
2093 		switch (errno) {
2094 		case ENOENT:
2095 			/*
2096 			 * The group doesn't yet exist; create it and repeat.
2097 			 */
2098 			af = afp->af_af;
2099 			if (create_ipmp(grname, af, NULL, _B_TRUE) == -1) {
2100 				if (errno == EEXIST)
2101 					continue;
2102 
2103 				Perror2(grname, "cannot create IPMP group");
2104 				goto fail;
2105 			}
2106 			continue;
2107 
2108 		case EALREADY:
2109 			/*
2110 			 * The interface is already in another group; must
2111 			 * remove existing membership first.
2112 			 */
2113 			lifrl.lifr_groupname[0] = '\0';
2114 			if (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) {
2115 				Perror2(name, "cannot remove existing "
2116 				    "IPMP group membership");
2117 				goto fail;
2118 			}
2119 			(void) strlcpy(lifrl.lifr_groupname, grname,
2120 			    LIFGRNAMSIZ);
2121 			continue;
2122 
2123 		case EAFNOSUPPORT:
2124 			/*
2125 			 * The group exists, but it's not configured with the
2126 			 * address families the interface needs.  Since only
2127 			 * two address families are currently supported, just
2128 			 * configure the "other" address family.  Note that we
2129 			 * may race with group deletion or creation by another
2130 			 * process (ENOENT or EEXIST); in such cases we repeat
2131 			 * our original SIOCSLIFGROUPNAME.
2132 			 */
2133 			(void) strlcpy(lifgr.gi_grname, grname, LIFGRNAMSIZ);
2134 			if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1) {
2135 				if (errno == ENOENT)
2136 					continue;
2137 
2138 				Perror2(grname, "SIOCGLIFGROUPINFO");
2139 				goto fail;
2140 			}
2141 
2142 			af = lifgr.gi_v4 ? AF_INET6 : AF_INET;
2143 			if (create_ipmp(grname, af, lifgr.gi_grifname,
2144 			    _B_TRUE) == -1) {
2145 				if (errno == EEXIST)
2146 					continue;
2147 
2148 				Perror2(grname, "cannot configure IPMP group");
2149 				goto fail;
2150 			}
2151 			continue;
2152 
2153 		case EADDRINUSE:
2154 			/*
2155 			 * Some addresses are in-use (or under control of DAD).
2156 			 * Bring them down and retry the group join operation.
2157 			 * We will bring them back up after the interface has
2158 			 * been placed in the group.
2159 			 */
2160 			if (ifaddrlistx(lifrl.lifr_name, IFF_UP|IFF_DUPLICATE,
2161 			    0, &ifaddrs) == -1) {
2162 				Perror2(grname, "cannot get address list");
2163 				goto fail;
2164 			}
2165 
2166 			ifaddrp = ifaddrs;
2167 			for (; ifaddrp != NULL; ifaddrp = nextifaddrp) {
2168 				if (!ifaddr_down(ifaddrp)) {
2169 					ifaddrs = ifaddrp;
2170 					goto fail;
2171 				}
2172 				nextifaddrp = ifaddrp->ia_next;
2173 				ifaddrp->ia_next = downaddrs;
2174 				downaddrs = ifaddrp;
2175 			}
2176 			ifaddrs = NULL;
2177 			continue;
2178 
2179 		case EADDRNOTAVAIL: {
2180 			/*
2181 			 * Some data addresses are under application control.
2182 			 * For some of these (e.g., ADDRCONF), the application
2183 			 * should remove the address, in which case we retry a
2184 			 * few times (since the application's action is not
2185 			 * atomic with respect to us) before bailing out and
2186 			 * informing the user.
2187 			 */
2188 			int ntries, nappaddr = 0;
2189 			const if_appflags_t *iap = if_appflags_tbl;
2190 
2191 			for (; iap->ia_app != NULL; iap++) {
2192 				ntries = 0;
2193 again:
2194 				if (ifaddrlistx(lifrl.lifr_name, iap->ia_flag,
2195 				    IFF_NOFAILOVER, &ifaddrs) == -1) {
2196 					(void) fprintf(stderr, "ifconfig: %s: "
2197 					    "cannot get data addresses managed "
2198 					    "by %s\n", lifrl.lifr_name,
2199 					    iap->ia_app);
2200 					goto fail;
2201 				}
2202 
2203 				if (ifaddrs == NULL)
2204 					continue;
2205 
2206 				ifaddrlistx_free(ifaddrs);
2207 				ifaddrs = NULL;
2208 
2209 				if (++ntries < iap->ia_tries) {
2210 					(void) poll(NULL, 0, 100);
2211 					goto again;
2212 				}
2213 
2214 				(void) fprintf(stderr, "ifconfig: cannot join "
2215 				    "IPMP group: %s has data addresses managed "
2216 				    "by %s\n", lifrl.lifr_name, iap->ia_app);
2217 				nappaddr++;
2218 			}
2219 			if (nappaddr > 0)
2220 				goto fail;
2221 			continue;
2222 		}
2223 		default:
2224 			Perror2(name, "SIOCSLIFGROUPNAME");
2225 			goto fail;
2226 		}
2227 	}
2228 
2229 	/*
2230 	 * If there were addresses that we had to bring down, it's time to
2231 	 * bring them up again.  As part of bringing them up, the kernel will
2232 	 * automatically move them to the new IPMP interface.
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 	return (0);
2242 fail:
2243 	/*
2244 	 * Attempt to bring back up any interfaces that we downed.
2245 	 */
2246 	for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
2247 		if (!ifaddr_up(ifaddrp) && errno != ENXIO) {
2248 			(void) fprintf(stderr, "ifconfig: cannot bring back up "
2249 			    "%s: %s\n", ifaddrp->ia_name, strerror(errno));
2250 		}
2251 	}
2252 	ifaddrlistx_free(downaddrs);
2253 	ifaddrlistx_free(ifaddrs);
2254 
2255 	/*
2256 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2257 	 * into the exit status, so we're forced to explicitly exit().
2258 	 */
2259 	exit(1);
2260 	/* NOTREACHED */
2261 }
2262 
2263 static boolean_t
2264 modcheck(const char *ifname)
2265 {
2266 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2267 
2268 	if (ioctl(s, SIOCGLIFFLAGS, &lifr) < 0) {
2269 		Perror0("SIOCGLIFFLAGS");
2270 		return (_B_FALSE);
2271 	}
2272 
2273 	if (lifr.lifr_flags & IFF_IPMP) {
2274 		(void) fprintf(stderr, "ifconfig: %s: module operations not"
2275 		    " supported on IPMP interfaces\n", ifname);
2276 		return (_B_FALSE);
2277 	}
2278 	if (lifr.lifr_flags & IFF_VIRTUAL) {
2279 		(void) fprintf(stderr, "ifconfig: %s: module operations not"
2280 		    " supported on virtual IP interfaces\n", ifname);
2281 		return (_B_FALSE);
2282 	}
2283 	return (_B_TRUE);
2284 }
2285 
2286 /*
2287  * To list all the modules above a given network interface.
2288  */
2289 /* ARGSUSED */
2290 static int
2291 modlist(char *null, int64_t param)
2292 {
2293 	int muxid_fd;
2294 	int muxfd;
2295 	int ipfd_lowstr;
2296 	int arpfd_lowstr;
2297 	int num_mods;
2298 	int i;
2299 	struct str_list strlist;
2300 	int orig_arpid;
2301 
2302 	/*
2303 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2304 	 * into the exit status, so we're forced to explicitly exit().
2305 	 */
2306 	if (!modcheck(name))
2307 		exit(1);
2308 
2309 	if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
2310 	    &orig_arpid) < 0) {
2311 		return (-1);
2312 	}
2313 	if ((num_mods = ioctl(ipfd_lowstr, I_LIST, NULL)) < 0) {
2314 		Perror0("cannot I_LIST to get the number of modules");
2315 	} else {
2316 		if (debug > 0) {
2317 			(void) printf("Listing (%d) modules above %s\n",
2318 			    num_mods, name);
2319 		}
2320 
2321 		strlist.sl_nmods = num_mods;
2322 		strlist.sl_modlist = malloc(sizeof (struct str_mlist) *
2323 		    num_mods);
2324 		if (strlist.sl_modlist == NULL) {
2325 			Perror0("cannot malloc");
2326 		} else {
2327 			if (ioctl(ipfd_lowstr, I_LIST, (caddr_t)&strlist) < 0) {
2328 				Perror0("cannot I_LIST for module names");
2329 			} else {
2330 				for (i = 0; i < strlist.sl_nmods; i++) {
2331 					(void) printf("%d %s\n", i,
2332 					    strlist.sl_modlist[i].l_name);
2333 				}
2334 			}
2335 			free(strlist.sl_modlist);
2336 		}
2337 	}
2338 	return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2339 	    orig_arpid));
2340 }
2341 
2342 #define	MODINSERT_OP	'i'
2343 #define	MODREMOVE_OP	'r'
2344 
2345 /*
2346  * To insert a module to the stream of the interface.  It is just a
2347  * wrapper.  The real function is modop().
2348  */
2349 /* ARGSUSED */
2350 static int
2351 modinsert(char *arg, int64_t param)
2352 {
2353 	return (modop(arg, MODINSERT_OP));
2354 }
2355 
2356 /*
2357  * To remove a module from the stream of the interface.  It is just a
2358  * wrapper.  The real function is modop().
2359  */
2360 /* ARGSUSED */
2361 static int
2362 modremove(char *arg, int64_t param)
2363 {
2364 	return (modop(arg, MODREMOVE_OP));
2365 }
2366 
2367 /*
2368  * Open a stream on /dev/udp{,6}, pop off all undesired modules (note that
2369  * the user may have configured autopush to add modules above
2370  * udp), and push the arp module onto the resulting stream.
2371  * This is used to make IP+ARP be able to atomically track the muxid
2372  * for the I_PLINKed STREAMS, thus it isn't related to ARP running the ARP
2373  * protocol.
2374  */
2375 static int
2376 open_arp_on_udp(char *udp_dev_name)
2377 {
2378 	int fd;
2379 
2380 	if ((fd = open(udp_dev_name, O_RDWR)) == -1) {
2381 		Perror2("open", udp_dev_name);
2382 		return (-1);
2383 	}
2384 	errno = 0;
2385 	while (ioctl(fd, I_POP, 0) != -1)
2386 		;
2387 	if (errno != EINVAL) {
2388 		Perror2("pop", udp_dev_name);
2389 	} else if (ioctl(fd, I_PUSH, ARP_MOD_NAME) == -1) {
2390 		Perror2("arp PUSH", udp_dev_name);
2391 	} else {
2392 		return (fd);
2393 	}
2394 	(void) close(fd);
2395 	return (-1);
2396 }
2397 
2398 /*
2399  * Helper function for mod*() functions.  It gets a fd to the lower IP
2400  * stream and I_PUNLINK's the lower stream.  It also initializes the
2401  * global variable lifr.
2402  *
2403  * Param:
2404  *	int *muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2405  *	int *muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2406  *	int *ipfd_lowstr: fd to the lower IP stream.
2407  *	int *arpfd_lowstr: fd to the lower ARP stream.
2408  *
2409  * Return:
2410  *	-1 if operation fails, 0 otherwise.
2411  *
2412  * Please see the big block comment above ifplumb() for the logic of the
2413  * PLINK/PUNLINK
2414  */
2415 static int
2416 ip_domux2fd(int *muxfd, int *muxid_fd, int *ipfd_lowstr, int *arpfd_lowstr,
2417     int *orig_arpid)
2418 {
2419 	uint64_t	flags;
2420 	char		*udp_dev_name;
2421 
2422 	*orig_arpid = 0;
2423 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2424 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2425 		Perror0_exit("status: SIOCGLIFFLAGS");
2426 	}
2427 	flags = lifr.lifr_flags;
2428 	if (flags & IFF_IPV4) {
2429 		udp_dev_name = UDP_DEV_NAME;
2430 	} else if (flags & IFF_IPV6) {
2431 		udp_dev_name = UDP6_DEV_NAME;
2432 	} else {
2433 		return (-1);
2434 	}
2435 
2436 	if ((*muxid_fd = open(udp_dev_name, O_RDWR)) < 0) {
2437 		Perror2("open", udp_dev_name);
2438 		return (-1);
2439 	}
2440 	if (ioctl(*muxid_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
2441 		Perror2("SIOCGLIFMUXID", udp_dev_name);
2442 		return (-1);
2443 	}
2444 	if (debug > 0) {
2445 		(void) printf("ARP_muxid %d IP_muxid %d\n",
2446 		    lifr.lifr_arp_muxid, lifr.lifr_ip_muxid);
2447 	}
2448 
2449 	/*
2450 	 * Use /dev/udp{,6} as the mux to avoid linkcycles.
2451 	 */
2452 	if ((*muxfd = open_arp_on_udp(udp_dev_name)) == -1)
2453 		return (-1);
2454 
2455 	if (lifr.lifr_arp_muxid != 0) {
2456 		if ((*arpfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
2457 		    lifr.lifr_arp_muxid)) < 0) {
2458 			if ((errno == EINVAL) &&
2459 			    (flags & (IFF_NOARP | IFF_IPV6))) {
2460 				/*
2461 				 * Some plumbing utilities set the muxid to
2462 				 * -1 or some invalid value to signify that
2463 				 * there is no arp stream. Set the muxid to 0
2464 				 * before trying to unplumb the IP stream.
2465 				 * IP does not allow the IP stream to be
2466 				 * unplumbed if it sees a non-null arp muxid,
2467 				 * for consistency of IP-ARP streams.
2468 				 */
2469 				*orig_arpid = lifr.lifr_arp_muxid;
2470 				lifr.lifr_arp_muxid = 0;
2471 				(void) ioctl(*muxid_fd, SIOCSLIFMUXID,
2472 				    (caddr_t)&lifr);
2473 				*arpfd_lowstr = -1;
2474 			} else {
2475 				Perror0("_I_MUXID2FD");
2476 				return (-1);
2477 			}
2478 		} else if (ioctl(*muxfd, I_PUNLINK,
2479 		    lifr.lifr_arp_muxid) < 0) {
2480 			Perror2("I_PUNLINK", udp_dev_name);
2481 			return (-1);
2482 		}
2483 	} else {
2484 		*arpfd_lowstr = -1;
2485 	}
2486 
2487 	if ((*ipfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
2488 	    lifr.lifr_ip_muxid)) < 0) {
2489 		Perror0("_I_MUXID2FD");
2490 		/* Undo any changes we made */
2491 		if (*orig_arpid != 0) {
2492 			lifr.lifr_arp_muxid = *orig_arpid;
2493 			(void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
2494 		}
2495 		return (-1);
2496 	}
2497 	if (ioctl(*muxfd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) {
2498 		Perror2("I_PUNLINK", udp_dev_name);
2499 		/* Undo any changes we made */
2500 		if (*orig_arpid != 0) {
2501 			lifr.lifr_arp_muxid = *orig_arpid;
2502 			(void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
2503 		}
2504 		return (-1);
2505 	}
2506 	return (0);
2507 }
2508 
2509 /*
2510  * Helper function for mod*() functions.  It I_PLINK's back the upper and
2511  * lower IP streams.  Note that this function must be called after
2512  * ip_domux2fd().  In ip_domux2fd(), the global variable lifr is initialized
2513  * and ip_plink() needs information in lifr.  So ip_domux2fd() and ip_plink()
2514  * must be called in pairs.
2515  *
2516  * Param:
2517  *	int muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2518  *	int muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2519  *	int ipfd_lowstr: fd to the lower IP stream.
2520  *	int arpfd_lowstr: fd to the lower ARP stream.
2521  *
2522  * Return:
2523  *	-1 if operation fails, 0 otherwise.
2524  *
2525  * Please see the big block comment above ifplumb() for the logic of the
2526  * PLINK/PUNLINK
2527  */
2528 static int
2529 ip_plink(int muxfd, int muxid_fd, int ipfd_lowstr, int arpfd_lowstr,
2530     int orig_arpid)
2531 {
2532 	int ip_muxid;
2533 
2534 	ip_muxid = ioctl(muxfd, I_PLINK, ipfd_lowstr);
2535 	if (ip_muxid < 0) {
2536 		Perror2("I_PLINK", UDP_DEV_NAME);
2537 		return (-1);
2538 	}
2539 
2540 	/*
2541 	 * If there is an arp stream, plink it. If there is no
2542 	 * arp stream, then it is possible that the plumbing
2543 	 * utility could have stored any value in the arp_muxid.
2544 	 * If so, restore it from orig_arpid.
2545 	 */
2546 	if (arpfd_lowstr != -1) {
2547 		if (ioctl(muxfd, I_PLINK, arpfd_lowstr) < 0) {
2548 			Perror2("I_PLINK", UDP_DEV_NAME);
2549 			return (-1);
2550 		}
2551 	} else if (orig_arpid != 0) {
2552 		/* Undo the changes we did in ip_domux2fd */
2553 		lifr.lifr_arp_muxid = orig_arpid;
2554 		lifr.lifr_ip_muxid = ip_muxid;
2555 		(void) ioctl(muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
2556 	}
2557 
2558 	(void) close(muxfd);
2559 	(void) close(muxid_fd);
2560 	return (0);
2561 }
2562 
2563 /*
2564  * The real function to perform module insertion/removal.
2565  *
2566  * Param:
2567  *	char *arg: the argument string module_name@position
2568  *	char op: operation, either MODINSERT_OP or MODREMOVE_OP.
2569  *
2570  * Return:
2571  *	Before doing ip_domux2fd(), this function calls exit(1) in case of
2572  *	error.  After ip_domux2fd() is done, it returns -1 for error, 0
2573  *	otherwise.
2574  */
2575 static int
2576 modop(char *arg, char op)
2577 {
2578 	char *pos_p;
2579 	int muxfd;
2580 	int muxid_fd;
2581 	int ipfd_lowstr;  /* IP stream (lower stream of mux) to be plinked */
2582 	int arpfd_lowstr; /* ARP stream (lower stream of mux) to be plinked */
2583 	struct strmodconf mod;
2584 	char *at_char = "@";
2585 	char *arg_str;
2586 	int orig_arpid;
2587 
2588 	/*
2589 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2590 	 * into the exit status, so we're forced to explicitly exit().
2591 	 */
2592 	if (!modcheck(name))
2593 		exit(1);
2594 
2595 	/* Need to save the original string for -a option. */
2596 	if ((arg_str = malloc(strlen(arg) + 1)) == NULL) {
2597 		Perror0("cannot malloc");
2598 		return (-1);
2599 	}
2600 	(void) strcpy(arg_str, arg);
2601 
2602 	if (*arg_str == *at_char) {
2603 		(void) fprintf(stderr,
2604 		    "ifconfig: must supply a module name\n");
2605 		exit(1);
2606 	}
2607 	mod.mod_name = strtok(arg_str, at_char);
2608 	if (strlen(mod.mod_name) > FMNAMESZ) {
2609 		(void) fprintf(stderr, "ifconfig: module name too long: %s\n",
2610 		    mod.mod_name);
2611 		exit(1);
2612 	}
2613 
2614 	/*
2615 	 * Need to make sure that the core TCP/IP stack modules are not
2616 	 * removed.  Otherwise, "bad" things can happen.  If a module
2617 	 * is removed and inserted back, it loses its old state.  But
2618 	 * the modules above it still have the old state.  E.g. IP assumes
2619 	 * fast data path while tunnel after re-inserted assumes that it can
2620 	 * receive M_DATA only in fast data path for which it does not have
2621 	 * any state.  This is a general caveat of _I_REMOVE/_I_INSERT.
2622 	 */
2623 	if (op == MODREMOVE_OP &&
2624 	    (strcmp(mod.mod_name, ARP_MOD_NAME) == 0 ||
2625 	    strcmp(mod.mod_name, IP_MOD_NAME) == 0 ||
2626 	    strcmp(mod.mod_name, TUN_NAME) == 0 ||
2627 	    strcmp(mod.mod_name, ATUN_NAME) == 0 ||
2628 	    strcmp(mod.mod_name, TUN6TO4_NAME) == 0)) {
2629 		(void) fprintf(stderr, "ifconfig: cannot remove %s\n",
2630 		    mod.mod_name);
2631 		exit(1);
2632 	}
2633 
2634 	if ((pos_p = strtok(NULL, at_char)) == NULL) {
2635 		(void) fprintf(stderr, "ifconfig: must supply a position\n");
2636 		exit(1);
2637 	}
2638 	mod.pos = atoi(pos_p);
2639 
2640 	if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
2641 	    &orig_arpid) < 0) {
2642 		free(arg_str);
2643 		return (-1);
2644 	}
2645 	switch (op) {
2646 	case MODINSERT_OP:
2647 		if (debug > 0) {
2648 			(void) printf("Inserting module %s at %d\n",
2649 			    mod.mod_name, mod.pos);
2650 		}
2651 		if (ioctl(ipfd_lowstr, _I_INSERT, (caddr_t)&mod) < 0) {
2652 			Perror2("fail to insert module", mod.mod_name);
2653 		}
2654 		break;
2655 	case MODREMOVE_OP:
2656 		if (debug > 0) {
2657 			(void) printf("Removing module %s at %d\n",
2658 			    mod.mod_name, mod.pos);
2659 		}
2660 		if (ioctl(ipfd_lowstr, _I_REMOVE, (caddr_t)&mod) < 0) {
2661 			Perror2("fail to remove module", mod.mod_name);
2662 		}
2663 		break;
2664 	default:
2665 		/* Should never get to here. */
2666 		(void) fprintf(stderr, "Unknown operation\n");
2667 		break;
2668 	}
2669 	free(arg_str);
2670 	return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2671 	    orig_arpid));
2672 }
2673 
2674 /*
2675  * Set tunnel source address
2676  */
2677 /* ARGSUSED */
2678 static int
2679 setiftsrc(char *addr, int64_t param)
2680 {
2681 	return (settaddr(addr, icfg_set_tunnel_src));
2682 }
2683 
2684 /*
2685  * Set tunnel destination address
2686  */
2687 /* ARGSUSED */
2688 static int
2689 setiftdst(char *addr, int64_t param)
2690 {
2691 	return (settaddr(addr, icfg_set_tunnel_dest));
2692 }
2693 
2694 /*
2695  * sets tunnels src|dst address.  settaddr() expects the following:
2696  * addr: Points to a printable string containing the address to be
2697  *       set, e.g. 129.153.128.110.
2698  * fn:   Pointer to a libinetcfg routine that will do the actual work.
2699  *       The only valid functions are icfg_set_tunnel_src and
2700  *       icfg_set_tunnel_dest.
2701  */
2702 static int
2703 settaddr(char *addr,
2704     int (*fn)(icfg_handle_t, const struct sockaddr *, socklen_t))
2705 {
2706 	icfg_handle_t handle;
2707 	icfg_if_t interface;
2708 	struct sockaddr_storage laddr;
2709 	int lower;
2710 	int rc;
2711 
2712 	if (strchr(name, ':') != NULL) {
2713 		errno = EPERM;
2714 		Perror0_exit("Tunnel params on logical interfaces");
2715 	}
2716 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
2717 	interface.if_protocol = SOCKET_AF(af);
2718 
2719 	/* Open interface. */
2720 	if ((rc = icfg_open(&handle, &interface)) != ICFG_SUCCESS)
2721 		Perror0_exit((char *)icfg_errmsg(rc));
2722 
2723 	rc = icfg_get_tunnel_lower(handle, &lower);
2724 	if (rc != ICFG_SUCCESS)
2725 		Perror0_exit((char *)icfg_errmsg(rc));
2726 
2727 	if (lower == AF_INET) {
2728 		in_getaddr(addr, (struct sockaddr *)&laddr, NULL);
2729 	} else {
2730 		in6_getaddr(addr, (struct sockaddr *)&laddr, NULL);
2731 	}
2732 
2733 	/* Call fn to do the real work, and close the interface. */
2734 	rc = (*fn)(handle, (struct sockaddr *)&laddr,
2735 	    sizeof (struct sockaddr_storage));
2736 	icfg_close(handle);
2737 
2738 	if (rc != ICFG_SUCCESS)
2739 		Perror0_exit((char *)icfg_errmsg(rc));
2740 
2741 	return (0);
2742 }
2743 
2744 /* Set tunnel encapsulation limit. */
2745 /* ARGSUSED */
2746 static int
2747 set_tun_encap_limit(char *arg, int64_t param)
2748 {
2749 	short limit;
2750 	icfg_if_t interface;
2751 	icfg_handle_t handle;
2752 	int rc;
2753 
2754 	if (strchr(name, ':') != NULL) {
2755 		errno = EPERM;
2756 		Perror0_exit("Tunnel params on logical interfaces");
2757 	}
2758 
2759 	if ((sscanf(arg, "%hd", &limit) != 1) || (limit < 0) ||
2760 	    (limit > 255)) {
2761 		errno = EINVAL;
2762 		Perror0_exit("Invalid encapsulation limit");
2763 	}
2764 
2765 	/* Open interface for configuration. */
2766 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
2767 	interface.if_protocol = SOCKET_AF(af);
2768 	if (icfg_open(&handle, &interface) != ICFG_SUCCESS)
2769 		Perror0_exit("couldn't open interface");
2770 
2771 	rc = icfg_set_tunnel_encaplimit(handle, (int)limit);
2772 	icfg_close(handle);
2773 
2774 	if (rc != ICFG_SUCCESS)
2775 		Perror0_exit("Could not configure tunnel encapsulation limit");
2776 
2777 	return (0);
2778 }
2779 
2780 /* Disable encapsulation limit. */
2781 /* ARGSUSED */
2782 static int
2783 clr_tun_encap_limit(char *arg, int64_t param)
2784 {
2785 	icfg_if_t interface;
2786 	icfg_handle_t handle;
2787 	int rc;
2788 
2789 	if (strchr(name, ':') != NULL) {
2790 		errno = EPERM;
2791 		Perror0_exit("Tunnel params on logical interfaces");
2792 	}
2793 
2794 	/* Open interface for configuration. */
2795 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
2796 	interface.if_protocol = SOCKET_AF(af);
2797 	if (icfg_open(&handle, &interface) != ICFG_SUCCESS)
2798 		Perror0_exit("couldn't open interface");
2799 
2800 	rc = icfg_set_tunnel_encaplimit(handle, -1);
2801 	icfg_close(handle);
2802 
2803 	if (rc != ICFG_SUCCESS)
2804 		Perror0_exit((char *)icfg_errmsg(rc));
2805 
2806 	return (0);
2807 }
2808 
2809 /* Set tunnel hop limit. */
2810 /* ARGSUSED */
2811 static int
2812 set_tun_hop_limit(char *arg, int64_t param)
2813 {
2814 	unsigned short limit;
2815 	icfg_if_t interface;
2816 	icfg_handle_t handle;
2817 	int rc;
2818 
2819 	if (strchr(name, ':') != NULL) {
2820 		errno = EPERM;
2821 		Perror0_exit("Tunnel params on logical interfaces");
2822 	}
2823 
2824 	/*
2825 	 * Check limit here since it's really only an 8-bit unsigned quantity.
2826 	 */
2827 	if ((sscanf(arg, "%hu", &limit) != 1) || (limit > 255)) {
2828 		errno = EINVAL;
2829 		Perror0_exit("Invalid hop limit");
2830 	}
2831 
2832 	/* Open interface for configuration. */
2833 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
2834 	interface.if_protocol = SOCKET_AF(af);
2835 	if (icfg_open(&handle, &interface) != ICFG_SUCCESS)
2836 		Perror0_exit("couldn't open interface");
2837 
2838 	rc = icfg_set_tunnel_hoplimit(handle, (uint8_t)limit);
2839 	icfg_close(handle);
2840 
2841 	if (rc != ICFG_SUCCESS)
2842 		Perror0_exit("Could not configure tunnel hop limit");
2843 
2844 	return (0);
2845 }
2846 
2847 /* Set zone ID */
2848 static int
2849 setzone(char *arg, int64_t param)
2850 {
2851 	zoneid_t zoneid = GLOBAL_ZONEID;
2852 
2853 	if (param == NEXTARG) {
2854 		/* zone must be active */
2855 		if ((zoneid = getzoneidbyname(arg)) == -1) {
2856 			(void) fprintf(stderr,
2857 			    "ifconfig: unknown zone '%s'\n", arg);
2858 			exit(1);
2859 		}
2860 	}
2861 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2862 	lifr.lifr_zoneid = zoneid;
2863 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
2864 		Perror0_exit("SIOCSLIFZONE");
2865 	return (0);
2866 }
2867 
2868 /* Put interface into all zones */
2869 /* ARGSUSED */
2870 static int
2871 setallzones(char *arg, int64_t param)
2872 {
2873 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2874 	lifr.lifr_zoneid = ALL_ZONES;
2875 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
2876 		Perror0_exit("SIOCSLIFZONE");
2877 	return (0);
2878 }
2879 
2880 /* Set source address to use */
2881 /* ARGSUSED */
2882 static int
2883 setifsrc(char *arg, int64_t param)
2884 {
2885 	uint_t ifindex = 0;
2886 	int rval;
2887 
2888 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2889 
2890 	/*
2891 	 * Argument can be either an interface name or "none". The latter means
2892 	 * that any previous selection is cleared.
2893 	 */
2894 
2895 	rval = strcmp(arg, name);
2896 	if (rval == 0) {
2897 		(void) fprintf(stderr,
2898 		    "ifconfig: Cannot specify same interface for usesrc"
2899 		    " group\n");
2900 		exit(1);
2901 	}
2902 
2903 	rval = strcmp(arg, NONE_STR);
2904 	if (rval != 0) {
2905 		if ((ifindex = if_nametoindex(arg)) == 0) {
2906 			(void) strncpy(lifr.lifr_name, arg, LIFNAMSIZ);
2907 			Perror0_exit("Could not get interface index");
2908 		}
2909 		lifr.lifr_index = ifindex;
2910 	} else {
2911 		if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) != 0)
2912 			Perror0_exit("Not a valid usesrc consumer");
2913 		lifr.lifr_index = 0;
2914 	}
2915 
2916 	if (debug)
2917 		(void) printf("setifsrc: lifr_name %s, lifr_index %d\n",
2918 		    lifr.lifr_name, lifr.lifr_index);
2919 
2920 	if (ioctl(s, SIOCSLIFUSESRC, (caddr_t)&lifr) == -1) {
2921 		if (rval == 0)
2922 			Perror0_exit("Cannot reset usesrc group");
2923 		else
2924 			Perror0_exit("Could not set source interface");
2925 	}
2926 
2927 	return (0);
2928 }
2929 
2930 /*
2931  * Print the interface status line associated with `ifname'
2932  */
2933 static void
2934 ifstatus(const char *ifname)
2935 {
2936 	uint64_t flags;
2937 	char if_usesrc_name[LIFNAMSIZ];
2938 	char *newbuf;
2939 	int n, numifs, rval = 0;
2940 	struct lifreq *lifrp;
2941 	struct lifsrcof lifs;
2942 
2943 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2944 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2945 		Perror0_exit("status: SIOCGLIFFLAGS");
2946 	}
2947 	flags = lifr.lifr_flags;
2948 
2949 	/*
2950 	 * In V4 compatibility mode, we don't print the IFF_IPV4 flag or
2951 	 * interfaces with IFF_IPV6 set.
2952 	 */
2953 	if (v4compat) {
2954 		flags &= ~IFF_IPV4;
2955 		if (flags & IFF_IPV6)
2956 			return;
2957 	}
2958 
2959 	(void) printf("%s: ", ifname);
2960 	print_flags(flags);
2961 
2962 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2963 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
2964 		Perror0_exit("status: SIOCGLIFMETRIC");
2965 	} else {
2966 		if (lifr.lifr_metric)
2967 			(void) printf(" metric %d", lifr.lifr_metric);
2968 	}
2969 	if (ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
2970 		(void) printf(" mtu %u", lifr.lifr_mtu);
2971 
2972 	/* don't print index or zone when in compatibility mode */
2973 	if (!v4compat) {
2974 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
2975 			(void) printf(" index %d", lifr.lifr_index);
2976 		/*
2977 		 * Stack instances use GLOBAL_ZONEID for IP data structures
2978 		 * even in the non-global zone.
2979 		 */
2980 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifr) >= 0 &&
2981 		    lifr.lifr_zoneid != getzoneid() &&
2982 		    lifr.lifr_zoneid != GLOBAL_ZONEID) {
2983 			char zone_name[ZONENAME_MAX];
2984 
2985 			if (lifr.lifr_zoneid == ALL_ZONES) {
2986 				(void) printf("\n\tall-zones");
2987 			} else if (getzonenamebyid(lifr.lifr_zoneid, zone_name,
2988 			    sizeof (zone_name)) < 0) {
2989 				(void) printf("\n\tzone %d", lifr.lifr_zoneid);
2990 			} else {
2991 				(void) printf("\n\tzone %s", zone_name);
2992 			}
2993 		}
2994 	}
2995 
2996 	if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) {
2997 		lifs.lifs_ifindex = lifr.lifr_index;
2998 
2999 		/*
3000 		 * Find the number of interfaces that use this interfaces'
3001 		 * address as a source address
3002 		 */
3003 		lifs.lifs_buf = NULL;
3004 		lifs.lifs_maxlen = 0;
3005 		for (;;) {
3006 			/* The first pass will give the bufsize we need */
3007 			rval = ioctl(s, SIOCGLIFSRCOF, (char *)&lifs);
3008 			if (rval < 0) {
3009 				if (lifs.lifs_buf != NULL) {
3010 					free(lifs.lifs_buf);
3011 					lifs.lifs_buf = NULL;
3012 				}
3013 				lifs.lifs_len = 0;
3014 				break;
3015 			}
3016 			if (lifs.lifs_len <= lifs.lifs_maxlen)
3017 				break;
3018 			/* Use kernel's size + a small margin to avoid loops */
3019 			lifs.lifs_maxlen = lifs.lifs_len +
3020 			    5 * sizeof (struct lifreq);
3021 			/* For the first pass, realloc acts like malloc */
3022 			newbuf = realloc(lifs.lifs_buf, lifs.lifs_maxlen);
3023 			if (newbuf == NULL) {
3024 				if (lifs.lifs_buf != NULL) {
3025 					free(lifs.lifs_buf);
3026 					lifs.lifs_buf = NULL;
3027 				}
3028 				lifs.lifs_len = 0;
3029 				break;
3030 			}
3031 			lifs.lifs_buf = newbuf;
3032 		}
3033 
3034 
3035 		numifs = lifs.lifs_len / sizeof (struct lifreq);
3036 		if (numifs > 0) {
3037 			lifrp = lifs.lifs_req;
3038 			(void) printf("\n\tsrcof");
3039 			for (n = numifs; n > 0; n--, lifrp++) {
3040 				(void) printf(" %s", lifrp->lifr_name);
3041 			}
3042 		}
3043 
3044 		if (lifs.lifs_buf != NULL)
3045 			free(lifs.lifs_buf);
3046 	}
3047 
3048 	/* Find the interface whose source address this interface uses */
3049 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
3050 		if (lifr.lifr_index != 0) {
3051 			if (if_indextoname(lifr.lifr_index,
3052 			    if_usesrc_name) == NULL) {
3053 				(void) printf("\n\tusesrc ifIndex %d",
3054 				    lifr.lifr_index);
3055 			} else {
3056 				(void) printf("\n\tusesrc %s", if_usesrc_name);
3057 			}
3058 		}
3059 	}
3060 
3061 	(void) putchar('\n');
3062 }
3063 
3064 
3065 /*
3066  * Print the status of the interface.  If an address family was
3067  * specified, show it and it only; otherwise, show them all.
3068  */
3069 static void
3070 status(void)
3071 {
3072 	struct afswtch *p = afp;
3073 	uint64_t flags;
3074 
3075 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3076 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
3077 		Perror0_exit("status: SIOCGLIFFLAGS");
3078 	}
3079 
3080 	flags = lifr.lifr_flags;
3081 
3082 	/*
3083 	 * Only print the interface status if the address family matches
3084 	 * the interface family flag.
3085 	 */
3086 	if (p != NULL) {
3087 		if (((p->af_af == AF_INET6) && (flags & IFF_IPV4)) ||
3088 		    ((p->af_af == AF_INET) && (flags & IFF_IPV6)))
3089 			return;
3090 	}
3091 
3092 	/*
3093 	 * In V4 compatibility mode, don't print IFF_IPV6 interfaces.
3094 	 */
3095 	if (v4compat && (flags & IFF_IPV6))
3096 		return;
3097 
3098 	ifstatus(name);
3099 
3100 	if (p != NULL) {
3101 		(*p->af_status)(1, flags);
3102 	} else {
3103 		for (p = afs; p->af_name; p++) {
3104 			(void) close(s);
3105 			s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0);
3106 			/* set global af for use in p->af_status */
3107 			af = p->af_af;
3108 			if (s == -1) {
3109 				Perror0_exit("socket");
3110 			}
3111 			(*p->af_status)(0, flags);
3112 		}
3113 
3114 		/*
3115 		 * Historically, 'ether' has been an address family,
3116 		 * so print it here.
3117 		 */
3118 		print_ifether(name);
3119 	}
3120 }
3121 
3122 /*
3123  * Print the status of the interface in a format that can be used to
3124  * reconfigure the interface later. Code stolen from status() above.
3125  */
3126 /* ARGSUSED */
3127 static int
3128 configinfo(char *null, int64_t param)
3129 {
3130 	char *cp;
3131 	struct afswtch *p = afp;
3132 	uint64_t flags;
3133 	char lifname[LIFNAMSIZ];
3134 	char if_usesrc_name[LIFNAMSIZ];
3135 
3136 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3137 
3138 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
3139 		Perror0_exit("status: SIOCGLIFFLAGS");
3140 	}
3141 	flags = lifr.lifr_flags;
3142 
3143 	if (debug) {
3144 		(void) printf("configinfo: name %s flags  0x%llx af_af %d\n",
3145 		    name, flags, p != NULL ? p->af_af : -1);
3146 	}
3147 
3148 	/*
3149 	 * Build the interface name to print (we can't directly use `name'
3150 	 * because one cannot "plumb" ":0" interfaces).
3151 	 */
3152 	(void) strlcpy(lifname, name, LIFNAMSIZ);
3153 	if ((cp = strchr(lifname, ':')) != NULL && atoi(cp + 1) == 0)
3154 		*cp = '\0';
3155 
3156 	/*
3157 	 * if the interface is IPv4
3158 	 *	if we have a IPv6 address family restriction return
3159 	 *		so it won't print
3160 	 *	if we are in IPv4 compatibility mode, clear out IFF_IPV4
3161 	 *		so we don't print it.
3162 	 */
3163 	if (flags & IFF_IPV4) {
3164 		if (p && p->af_af == AF_INET6)
3165 			return (-1);
3166 		if (v4compat)
3167 			flags &= ~IFF_IPV4;
3168 
3169 		(void) printf("%s inet plumb", lifname);
3170 	} else if (flags & IFF_IPV6) {
3171 		/*
3172 		 * else if the interface is IPv6
3173 		 *	if we have a IPv4 address family restriction return
3174 		 *	or we are in IPv4 compatibiltiy mode, return.
3175 		 */
3176 		if (p && p->af_af == AF_INET)
3177 			return (-1);
3178 		if (v4compat)
3179 			return (-1);
3180 
3181 		(void) printf("%s inet6 plumb", lifname);
3182 	}
3183 
3184 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3185 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
3186 		Perror0_exit("configinfo: SIOCGLIFMETRIC");
3187 	} else {
3188 		if (lifr.lifr_metric)
3189 			(void) printf(" metric %d ", lifr.lifr_metric);
3190 	}
3191 	if (((flags & (IFF_VIRTUAL|IFF_LOOPBACK)) != IFF_VIRTUAL) &&
3192 	    ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
3193 		(void) printf(" mtu %d", lifr.lifr_metric);
3194 
3195 	/* Index only applies to the zeroth interface */
3196 	if (lifnum(name) == 0) {
3197 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
3198 			(void) printf(" index %d", lifr.lifr_index);
3199 	}
3200 
3201 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
3202 		if (lifr.lifr_index != 0) {
3203 			if (if_indextoname(lifr.lifr_index,
3204 			    if_usesrc_name) != NULL) {
3205 				(void) printf(" usesrc %s", if_usesrc_name);
3206 			}
3207 		}
3208 	}
3209 
3210 	if (p != NULL) {
3211 		(*p->af_configinfo)(1, flags);
3212 	} else {
3213 		for (p = afs; p->af_name; p++) {
3214 			(void) close(s);
3215 			s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0);
3216 			/* set global af for use in p->af_configinfo */
3217 			af = p->af_af;
3218 			if (s == -1) {
3219 				Perror0_exit("socket");
3220 			}
3221 			(*p->af_configinfo)(0, flags);
3222 		}
3223 	}
3224 
3225 	(void) printf("\n");
3226 	return (0);
3227 }
3228 
3229 static void
3230 print_tsec(struct iftun_req *tparams)
3231 {
3232 	ipsec_req_t *ipsr;
3233 
3234 	(void) printf("\ttunnel security settings  ");
3235 	/*
3236 	 * Deal with versioning, for now just point
3237 	 * an ipsec_req_t at ifta_secinfo.  If versions
3238 	 * change, something else will overlay ifta_secinfo.
3239 	 */
3240 	assert(tparams->ifta_vers == IFTUN_VERSION);
3241 
3242 	if (tparams->ifta_flags & IFTUN_COMPLEX_SECURITY) {
3243 		(void) printf("-->  use 'ipsecconf -ln -i %s'",
3244 		    tparams->ifta_lifr_name);
3245 	} else {
3246 		ipsr = (ipsec_req_t *)(&tparams->ifta_secinfo);
3247 		if (ipsr->ipsr_ah_req & IPSEC_PREF_REQUIRED) {
3248 			(void) printf("ah (%s)  ",
3249 			    rparsealg(ipsr->ipsr_auth_alg, IPSEC_PROTO_AH));
3250 		}
3251 		if (ipsr->ipsr_esp_req & IPSEC_PREF_REQUIRED) {
3252 			(void) printf("esp (%s",
3253 			    rparsealg(ipsr->ipsr_esp_alg, IPSEC_PROTO_ESP));
3254 			(void) printf("/%s)",
3255 			    rparsealg(ipsr->ipsr_esp_auth_alg, IPSEC_PROTO_AH));
3256 		}
3257 	}
3258 	(void) printf("\n");
3259 }
3260 
3261 static void
3262 tun_status(void)
3263 {
3264 	icfg_if_t interface;
3265 	int rc;
3266 	icfg_handle_t handle;
3267 	int protocol;
3268 	char srcbuf[INET6_ADDRSTRLEN];
3269 	char dstbuf[INET6_ADDRSTRLEN];
3270 	boolean_t tabbed;
3271 	uint8_t hoplimit;
3272 	int16_t encaplimit;
3273 	struct sockaddr_storage taddr;
3274 	socklen_t socklen = sizeof (taddr);
3275 
3276 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
3277 	interface.if_protocol = SOCKET_AF(af);
3278 	if ((rc = icfg_open(&handle, &interface)) != ICFG_SUCCESS)
3279 		Perror0_exit((char *)icfg_errmsg(rc));
3280 
3281 	/*
3282 	 * only print tunnel info for lun 0.  If ioctl fails, assume
3283 	 * we are not a tunnel
3284 	 */
3285 	if (strchr(name, ':') != NULL ||
3286 	    icfg_get_tunnel_lower(handle, &protocol) != ICFG_SUCCESS) {
3287 		icfg_close(handle);
3288 		return;
3289 	}
3290 
3291 	switch (protocol) {
3292 	case AF_INET:
3293 		(void) printf("\tinet");
3294 		break;
3295 	case AF_INET6:
3296 		(void) printf("\tinet6");
3297 		break;
3298 	default:
3299 		Perror0_exit("\ttunnel: Illegal lower stream\n\t");
3300 		break;
3301 	}
3302 
3303 	rc = icfg_get_tunnel_src(handle, (struct sockaddr *)&taddr, &socklen);
3304 	if (rc == ICFG_NOT_SET) {
3305 		(void) strlcpy(srcbuf, (protocol == AF_INET) ? "0.0.0.0" :
3306 		    "::", sizeof (srcbuf));
3307 	} else if (rc != ICFG_SUCCESS) {
3308 		Perror0_exit((char *)icfg_errmsg(rc));
3309 	} else {
3310 		rc = icfg_sockaddr_to_str(protocol, (struct sockaddr *)&taddr,
3311 		    srcbuf, sizeof (srcbuf));
3312 		if (rc != ICFG_SUCCESS) {
3313 			Perror0_exit((char *)icfg_errmsg(rc));
3314 		}
3315 	}
3316 
3317 	(void) printf(" tunnel src %s ", srcbuf);
3318 
3319 	rc = icfg_get_tunnel_dest(handle, (struct sockaddr *)&taddr, &socklen);
3320 	if (rc == ICFG_NOT_SET) {
3321 		(void) printf("\n");
3322 	} else {
3323 		rc = icfg_sockaddr_to_str(protocol, (struct sockaddr *)&taddr,
3324 		    dstbuf, sizeof (dstbuf));
3325 		if (rc != ICFG_SUCCESS) {
3326 			Perror0_exit((char *)icfg_errmsg(rc));
3327 		}
3328 		(void) printf("tunnel dst %s\n", dstbuf);
3329 	}
3330 
3331 	if (handle->ifh_tunnel_params != NULL &&
3332 	    (handle->ifh_tunnel_params->ifta_flags & IFTUN_SECURITY))
3333 		print_tsec(handle->ifh_tunnel_params);
3334 
3335 	/*
3336 	 * tabbed indicates tabbed and printed.  Use it tell us whether
3337 	 * to tab and that we've printed something here, so we need a
3338 	 * newline
3339 	 */
3340 	tabbed = _B_FALSE;
3341 
3342 	if (icfg_get_tunnel_hoplimit(handle, &hoplimit) == ICFG_SUCCESS) {
3343 		(void) printf("\ttunnel hop limit %d ", hoplimit);
3344 		tabbed = _B_TRUE;
3345 	}
3346 
3347 	if ((protocol == AF_INET6) &&
3348 	    (icfg_get_tunnel_encaplimit(handle, &encaplimit) ==
3349 	    ICFG_SUCCESS)) {
3350 		if (!tabbed) {
3351 			(void) printf("\t");
3352 			tabbed = _B_TRUE;
3353 		}
3354 		if (encaplimit >= 0) {
3355 			(void) printf("tunnel encapsulation limit %d",
3356 			    encaplimit);
3357 		} else {
3358 			(void) printf("tunnel encapsulation limit disabled");
3359 		}
3360 	}
3361 
3362 	if (tabbed)
3363 		(void) printf("\n");
3364 
3365 	icfg_close(handle);
3366 }
3367 
3368 static void
3369 in_status(int force, uint64_t flags)
3370 {
3371 	struct sockaddr_in *sin, *laddr;
3372 	struct	sockaddr_in netmask = { AF_INET };
3373 
3374 	if (debug)
3375 		(void) printf("in_status(%s) flags 0x%llx\n", name, flags);
3376 
3377 	/* only print status for IPv4 interfaces */
3378 	if (!(flags & IFF_IPV4))
3379 		return;
3380 
3381 	/* if the interface is a tunnel, print the tunnel status */
3382 	tun_status();
3383 
3384 	if (!(flags & IFF_NOLOCAL)) {
3385 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3386 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3387 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3388 			    errno == ENXIO) {
3389 				if (!force)
3390 					return;
3391 				(void) memset(&lifr.lifr_addr, 0,
3392 				    sizeof (lifr.lifr_addr));
3393 			} else
3394 				Perror0_exit("in_status: SIOCGLIFADDR");
3395 		}
3396 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3397 		(void) printf("\tinet %s ", inet_ntoa(sin->sin_addr));
3398 		laddr = sin;
3399 	} else {
3400 		(void) printf("\tinet ");
3401 	}
3402 
3403 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3404 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3405 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3406 		    errno == ENXIO) {
3407 			if (!force)
3408 				return;
3409 			(void) memset(&lifr.lifr_addr, 0,
3410 			    sizeof (lifr.lifr_addr));
3411 		} else {
3412 			Perror0_exit("in_status: SIOCGLIFSUBNET");
3413 		}
3414 	}
3415 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
3416 	if ((flags & IFF_NOLOCAL) ||
3417 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
3418 		(void) printf("subnet %s/%d ", inet_ntoa(sin->sin_addr),
3419 		    lifr.lifr_addrlen);
3420 	}
3421 	if (sin->sin_family != AF_INET) {
3422 		(void) printf("Wrong family: %d\n", sin->sin_family);
3423 	}
3424 
3425 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3426 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
3427 		if (errno != EADDRNOTAVAIL)
3428 			Perror0_exit("in_status: SIOCGLIFNETMASK");
3429 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
3430 	} else
3431 		netmask.sin_addr =
3432 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
3433 	if (flags & IFF_POINTOPOINT) {
3434 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3435 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3436 			if (errno == EADDRNOTAVAIL)
3437 				(void) memset(&lifr.lifr_addr, 0,
3438 				    sizeof (lifr.lifr_addr));
3439 			else
3440 				Perror0_exit("in_status: SIOCGLIFDSTADDR");
3441 		}
3442 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
3443 		(void) printf("--> %s ", inet_ntoa(sin->sin_addr));
3444 	}
3445 	(void) printf("netmask %x ", ntohl(netmask.sin_addr.s_addr));
3446 	if (flags & IFF_BROADCAST) {
3447 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3448 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
3449 			if (errno == EADDRNOTAVAIL)
3450 				(void) memset(&lifr.lifr_addr, 0,
3451 				    sizeof (lifr.lifr_addr));
3452 			else
3453 				Perror0_exit("in_status: SIOCGLIFBRDADDR");
3454 		}
3455 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3456 		if (sin->sin_addr.s_addr != 0) {
3457 			(void) printf("broadcast %s",
3458 			    inet_ntoa(sin->sin_addr));
3459 		}
3460 	}
3461 	/* If there is a groupname, print it for only the physical interface */
3462 	if (strchr(name, ':') == NULL) {
3463 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3464 		    lifr.lifr_groupname[0] != '\0') {
3465 			(void) printf("\n\tgroupname %s", lifr.lifr_groupname);
3466 		}
3467 	}
3468 	(void) putchar('\n');
3469 }
3470 
3471 static void
3472 in6_status(int force, uint64_t flags)
3473 {
3474 	char abuf[INET6_ADDRSTRLEN];
3475 	struct sockaddr_in6 *sin6, *laddr6;
3476 
3477 	if (debug)
3478 		(void) printf("in6_status(%s) flags 0x%llx\n", name, flags);
3479 
3480 	if (!(flags & IFF_IPV6))
3481 		return;
3482 
3483 	/* if the interface is a tunnel, print the tunnel status */
3484 	tun_status();
3485 
3486 	if (!(flags & IFF_NOLOCAL)) {
3487 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3488 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3489 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3490 			    errno == ENXIO) {
3491 				if (!force)
3492 					return;
3493 				(void) memset(&lifr.lifr_addr, 0,
3494 				    sizeof (lifr.lifr_addr));
3495 			} else
3496 				Perror0_exit("in_status6: SIOCGLIFADDR");
3497 		}
3498 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3499 		(void) printf("\tinet6 %s/%d ",
3500 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3501 		    abuf, sizeof (abuf)),
3502 		    lifr.lifr_addrlen);
3503 		laddr6 = sin6;
3504 	} else {
3505 		(void) printf("\tinet6 ");
3506 	}
3507 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3508 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3509 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3510 		    errno == ENXIO) {
3511 			if (!force)
3512 				return;
3513 			(void) memset(&lifr.lifr_addr, 0,
3514 			    sizeof (lifr.lifr_addr));
3515 		} else
3516 			Perror0_exit("in_status6: SIOCGLIFSUBNET");
3517 	}
3518 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3519 	if ((flags & IFF_NOLOCAL) ||
3520 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
3521 		(void) printf("subnet %s/%d ",
3522 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3523 		    abuf, sizeof (abuf)),
3524 		    lifr.lifr_addrlen);
3525 	}
3526 	if (sin6->sin6_family != AF_INET6) {
3527 		(void) printf("Wrong family: %d\n", sin6->sin6_family);
3528 	}
3529 	if (flags & IFF_POINTOPOINT) {
3530 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3531 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3532 			if (errno == EADDRNOTAVAIL)
3533 				(void) memset(&lifr.lifr_addr, 0,
3534 				    sizeof (lifr.lifr_addr));
3535 			else
3536 				Perror0_exit("in_status6: SIOCGLIFDSTADDR");
3537 		}
3538 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
3539 		(void) printf("--> %s ",
3540 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3541 		    abuf, sizeof (abuf)));
3542 	}
3543 	if (verbose) {
3544 		(void) putchar('\n');
3545 		(void) putchar('\t');
3546 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3547 		if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
3548 			if (errno == EADDRNOTAVAIL || errno == EINVAL)
3549 				(void) memset(&lifr.lifr_addr, 0,
3550 				    sizeof (lifr.lifr_addr));
3551 			else
3552 				Perror0_exit("in_status6: SIOCGLIFTOKEN");
3553 		} else {
3554 			sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3555 			(void) printf("token %s/%d ",
3556 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3557 			    abuf, sizeof (abuf)),
3558 			    lifr.lifr_addrlen);
3559 		}
3560 		if (ioctl(s, SIOCGLIFLNKINFO, (caddr_t)&lifr) < 0) {
3561 			if (errno != EINVAL) {
3562 				Perror0_exit("in_status6: SIOCGLIFLNKINFO");
3563 			}
3564 		} else {
3565 			(void) printf("maxhops %u, reachtime %u ms, "
3566 			    "reachretrans %u ms, maxmtu %u ",
3567 			    lifr.lifr_ifinfo.lir_maxhops,
3568 			    lifr.lifr_ifinfo.lir_reachtime,
3569 			    lifr.lifr_ifinfo.lir_reachretrans,
3570 			    lifr.lifr_ifinfo.lir_maxmtu);
3571 		}
3572 	}
3573 	/* If there is a groupname, print it for only the physical interface */
3574 	if (strchr(name, ':') == NULL) {
3575 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3576 		    lifr.lifr_groupname[0] != '\0') {
3577 			(void) printf("\n\tgroupname %s", lifr.lifr_groupname);
3578 		}
3579 	}
3580 	(void) putchar('\n');
3581 }
3582 
3583 static void
3584 in_configinfo(int force, uint64_t flags)
3585 {
3586 	struct sockaddr_in *sin, *laddr;
3587 	struct	sockaddr_in netmask = { AF_INET };
3588 
3589 	if (debug)
3590 		(void) printf("in_configinfo(%s) flags 0x%llx\n", name, flags);
3591 
3592 	/* only configinfo info for IPv4 interfaces */
3593 	if (!(flags & IFF_IPV4))
3594 		return;
3595 
3596 	if (!(flags & IFF_NOLOCAL)) {
3597 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3598 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3599 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3600 			    errno == ENXIO) {
3601 				if (!force)
3602 					return;
3603 				(void) memset(&lifr.lifr_addr, 0,
3604 				    sizeof (lifr.lifr_addr));
3605 			} else
3606 				Perror0_exit("in_configinfo: SIOCGLIFADDR");
3607 		}
3608 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3609 		(void) printf(" set %s ", inet_ntoa(sin->sin_addr));
3610 		laddr = sin;
3611 	}
3612 
3613 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3614 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3615 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3616 		    errno == ENXIO) {
3617 			if (!force)
3618 				return;
3619 			(void) memset(&lifr.lifr_addr, 0,
3620 			    sizeof (lifr.lifr_addr));
3621 		} else {
3622 			Perror0_exit("in_configinfo: SIOCGLIFSUBNET");
3623 		}
3624 	}
3625 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
3626 
3627 	if ((flags & IFF_NOLOCAL) ||
3628 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
3629 		(void) printf(" subnet %s/%d ", inet_ntoa(sin->sin_addr),
3630 		    lifr.lifr_addrlen);
3631 	}
3632 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3633 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
3634 		if (errno != EADDRNOTAVAIL)
3635 			Perror0_exit("in_configinfo: SIOCGLIFNETMASK");
3636 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
3637 	} else
3638 		netmask.sin_addr =
3639 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
3640 	if (flags & IFF_POINTOPOINT) {
3641 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3642 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3643 			if (errno == EADDRNOTAVAIL)
3644 				(void) memset(&lifr.lifr_addr, 0,
3645 				    sizeof (lifr.lifr_addr));
3646 			else
3647 				Perror0_exit("in_configinfo: SIOCGLIFDSTADDR");
3648 		}
3649 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
3650 		(void) printf(" destination %s ", inet_ntoa(sin->sin_addr));
3651 	}
3652 	(void) printf(" netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
3653 	if (flags & IFF_BROADCAST) {
3654 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3655 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
3656 			if (errno == EADDRNOTAVAIL)
3657 				(void) memset(&lifr.lifr_addr, 0,
3658 				    sizeof (lifr.lifr_addr));
3659 			else
3660 				Perror0_exit("in_configinfo: SIOCGLIFBRDADDR");
3661 		}
3662 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3663 		if (sin->sin_addr.s_addr != 0) {
3664 			(void) printf(" broadcast %s ",
3665 			    inet_ntoa(sin->sin_addr));
3666 		}
3667 	}
3668 
3669 	/* If there is a groupname, print it for only the zeroth interface */
3670 	if (lifnum(name) == 0) {
3671 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3672 		    lifr.lifr_groupname[0] != '\0') {
3673 			(void) printf(" group %s ", lifr.lifr_groupname);
3674 		}
3675 	}
3676 
3677 	/* Print flags to configure */
3678 	print_config_flags(AF_INET, flags);
3679 }
3680 
3681 static void
3682 in6_configinfo(int force, uint64_t flags)
3683 {
3684 	char abuf[INET6_ADDRSTRLEN];
3685 	struct sockaddr_in6 *sin6, *laddr6;
3686 
3687 	if (debug)
3688 		(void) printf("in6_configinfo(%s) flags 0x%llx\n", name,
3689 		    flags);
3690 
3691 	if (!(flags & IFF_IPV6))
3692 		return;
3693 
3694 	if (!(flags & IFF_NOLOCAL)) {
3695 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3696 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3697 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3698 			    errno == ENXIO) {
3699 				if (!force)
3700 					return;
3701 				(void) memset(&lifr.lifr_addr, 0,
3702 				    sizeof (lifr.lifr_addr));
3703 			} else
3704 				Perror0_exit("in6_configinfo: SIOCGLIFADDR");
3705 		}
3706 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3707 		(void) printf(" set %s/%d ",
3708 		    inet_ntop(AF_INET6, &sin6->sin6_addr, abuf, sizeof (abuf)),
3709 		    lifr.lifr_addrlen);
3710 		laddr6 = sin6;
3711 	}
3712 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3713 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3714 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3715 		    errno == ENXIO) {
3716 			if (!force)
3717 				return;
3718 			(void) memset(&lifr.lifr_addr, 0,
3719 			    sizeof (lifr.lifr_addr));
3720 		} else
3721 			Perror0_exit("in6_configinfo: SIOCGLIFSUBNET");
3722 	}
3723 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3724 	if ((flags & IFF_NOLOCAL) ||
3725 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
3726 		(void) printf(" subnet %s/%d ",
3727 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3728 		    abuf, sizeof (abuf)),
3729 		    lifr.lifr_addrlen);
3730 	}
3731 
3732 	if (flags & IFF_POINTOPOINT) {
3733 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3734 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3735 			if (errno == EADDRNOTAVAIL)
3736 				(void) memset(&lifr.lifr_addr, 0,
3737 				    sizeof (lifr.lifr_addr));
3738 			else
3739 				Perror0_exit("in6_configinfo: SIOCGLIFDSTADDR");
3740 		}
3741 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
3742 		(void) printf(" destination %s ",
3743 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3744 		    abuf, sizeof (abuf)));
3745 	}
3746 
3747 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3748 	if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
3749 		if (errno == EADDRNOTAVAIL || errno == EINVAL)
3750 			(void) memset(&lifr.lifr_addr, 0,
3751 			    sizeof (lifr.lifr_addr));
3752 		else
3753 			Perror0_exit("in6_configinfo: SIOCGLIFTOKEN");
3754 	} else {
3755 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3756 		(void) printf(" token %s/%d ",
3757 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3758 		    abuf, sizeof (abuf)),
3759 		    lifr.lifr_addrlen);
3760 	}
3761 
3762 	/* If there is a groupname, print it for only the zeroth interface */
3763 	if (lifnum(name) == 0) {
3764 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3765 		    lifr.lifr_groupname[0] != '\0') {
3766 			(void) printf(" group %s ", lifr.lifr_groupname);
3767 		}
3768 	}
3769 
3770 	/* Print flags to configure */
3771 	print_config_flags(AF_INET6, flags);
3772 }
3773 
3774 /*
3775  * We need to plink both the arp-device stream and the arp-ip-device stream.
3776  * However the muxid is stored only in IP. Plumbing 2 streams individually
3777  * is not atomic, and if ifconfig is killed, the resulting plumbing can
3778  * be inconsistent. For eg. if only the arp stream is plumbed, we have lost
3779  * the muxid, and the half-baked plumbing can neither be unplumbed nor
3780  * replumbed, thus requiring a reboot. To avoid the above the following
3781  * scheme is used.
3782  *
3783  * Ifconfig asks IP to enforce atomicity of plumbing the arp and IP streams.
3784  * This is done by pushing arp on to the mux (/dev/udp). ARP adds some
3785  * extra information in the I_PLINK and I_PUNLINK ioctls to let IP know
3786  * that the plumbing/unplumbing has to be done atomically. Ifconfig plumbs
3787  * the IP stream first, and unplumbs it last. The kernel (IP) does not
3788  * allow IP stream to be unplumbed without unplumbing arp stream. Similarly
3789  * it does not allow arp stream to be plumbed before IP stream is plumbed.
3790  * There is no need to use SIOCSLIFMUXID, since the whole operation is atomic,
3791  * and IP uses the info in the I_PLINK message to get the muxid.
3792  *
3793  * a. STREAMS does not allow us to use /dev/ip itself as the mux. So we use
3794  *    /dev/udp{,6}.
3795  * b. SIOCGLIFMUXID returns the muxid corresponding to the V4 or V6 stream
3796  *    depending on the open i.e. V4 vs V6 open. So we need to use /dev/udp
3797  *    or /dev/udp6 for SIOCGLIFMUXID and SIOCSLIFMUXID.
3798  * c. We need to push ARP in order to get the required kernel support for
3799  *    atomic plumbings. The actual work done by ARP is explained in arp.c
3800  *    Without pushing ARP, we will still be able to plumb/unplumb. But
3801  *    it is not atomic, and is supported by the kernel for backward
3802  *    compatibility for other utilities like atmifconfig etc. In this case
3803  *    the utility must use SIOCSLIFMUXID.
3804  */
3805 static int
3806 ifplumb(const char *linkname, const char *ifname, boolean_t genppa, int af)
3807 {
3808 	int	arp_muxid = -1, ip_muxid;
3809 	int	mux_fd, ip_fd, arp_fd;
3810 	int 	retval;
3811 	char	*udp_dev_name;
3812 	uint64_t flags;
3813 	uint_t	dlpi_flags;
3814 	dlpi_handle_t	dh_arp, dh_ip;
3815 
3816 	/*
3817 	 * Always dlpi_open() with DLPI_NOATTACH because the IP and ARP module
3818 	 * will do the attach themselves for DLPI style-2 links.
3819 	 */
3820 	dlpi_flags = DLPI_NOATTACH;
3821 
3822 	/*
3823 	 * If `linkname' is the special token IPMPSTUB, then this is a request
3824 	 * to create an IPMP interface atop /dev/ipmpstub0.  (We can't simply
3825 	 * pass "ipmpstub0" as `linkname' since an admin *could* have a normal
3826 	 * vanity-named link named "ipmpstub0" that they'd like to plumb.)
3827 	 */
3828 	if (linkname == IPMPSTUB) {
3829 		linkname = "ipmpstub0";
3830 		dlpi_flags |= DLPI_DEVONLY;
3831 	}
3832 
3833 	retval = dlpi_open(linkname, &dh_ip, dlpi_flags);
3834 	if (retval != DLPI_SUCCESS)
3835 		Perrdlpi_exit("cannot open link", linkname, retval);
3836 
3837 	if (debug) {
3838 		(void) printf("ifconfig: ifplumb: link %s, ifname %s, "
3839 		    "genppa %u\n", linkname, ifname, genppa);
3840 	}
3841 
3842 	ip_fd = dlpi_fd(dh_ip);
3843 	if (ioctl(ip_fd, I_PUSH, IP_MOD_NAME) == -1)
3844 		Perror2_exit("I_PUSH", IP_MOD_NAME);
3845 
3846 	/*
3847 	 * Push the ARP module onto the interface stream. IP uses
3848 	 * this to send resolution requests up to ARP. We need to
3849 	 * do this before the SLIFNAME ioctl is sent down because
3850 	 * the interface becomes publicly known as soon as the SLIFNAME
3851 	 * ioctl completes. Thus some other process trying to bring up
3852 	 * the interface after SLIFNAME but before we have pushed ARP
3853 	 * could hang. We pop the module again later if it is not needed.
3854 	 */
3855 	if (ioctl(ip_fd, I_PUSH, ARP_MOD_NAME) == -1)
3856 		Perror2_exit("I_PUSH", ARP_MOD_NAME);
3857 
3858 	/*
3859 	 * Prepare to set IFF_IPV4/IFF_IPV6 flags as part of SIOCSLIFNAME.
3860 	 * (At this point in time the kernel also allows an override of the
3861 	 * IFF_CANTCHANGE flags.)
3862 	 */
3863 	lifr.lifr_name[0] = '\0';
3864 	if (ioctl(ip_fd, SIOCGLIFFLAGS, (char *)&lifr) == -1)
3865 		Perror0_exit("ifplumb: SIOCGLIFFLAGS");
3866 
3867 	if (af == AF_INET6) {
3868 		flags = lifr.lifr_flags | IFF_IPV6;
3869 		flags &= ~(IFF_BROADCAST | IFF_IPV4);
3870 	} else {
3871 		flags = lifr.lifr_flags | IFF_IPV4;
3872 		flags &= ~IFF_IPV6;
3873 	}
3874 
3875 	/*
3876 	 * Set the interface name.  If we've been asked to generate the PPA,
3877 	 * then find the lowest available PPA (only currently used for IPMP
3878 	 * interfaces).  Otherwise, use the interface name as-is.
3879 	 */
3880 	if (genppa) {
3881 		int ppa;
3882 
3883 		/*
3884 		 * We'd like to just set lifr_ppa to UINT_MAX and have the
3885 		 * kernel pick a PPA.  Unfortunately, that would mishandle
3886 		 * two cases:
3887 		 *
3888 		 *	1. If the PPA is available but the groupname is taken
3889 		 *	   (e.g., the "ipmp2" IP interface name is available
3890 		 *	   but the "ipmp2" groupname is taken) then the
3891 		 *	   auto-assignment by the kernel will fail.
3892 		 *
3893 		 *	2. If we're creating (e.g.) an IPv6-only IPMP
3894 		 *	   interface, and there's already an IPv4-only IPMP
3895 		 *	   interface, the kernel will allow us to accidentally
3896 		 *	   reuse the IPv6 IPMP interface name (since
3897 		 *	   SIOCSLIFNAME uniqueness is per-interface-type).
3898 		 *	   This will cause administrative confusion.
3899 		 *
3900 		 * Thus, we instead take a brute-force approach of checking
3901 		 * whether the IPv4 or IPv6 name is already in-use before
3902 		 * attempting the SIOCSLIFNAME.  As per (1) above, the
3903 		 * SIOCSLIFNAME may still fail, in which case we just proceed
3904 		 * to the next one.  If this approach becomes too slow, we
3905 		 * can add a new SIOC* to handle this case in the kernel.
3906 		 */
3907 		for (ppa = 0; ppa < UINT_MAX; ppa++) {
3908 			(void) snprintf(lifr.lifr_name, LIFNAMSIZ, "%s%d",
3909 			    ifname, ppa);
3910 
3911 			if (ioctl(s4, SIOCGLIFFLAGS, &lifr) != -1 ||
3912 			    errno != ENXIO)
3913 				continue;
3914 
3915 			if (ioctl(s6, SIOCGLIFFLAGS, &lifr) != -1 ||
3916 			    errno != ENXIO)
3917 				continue;
3918 
3919 			lifr.lifr_ppa = ppa;
3920 			lifr.lifr_flags = flags;
3921 			retval = ioctl(ip_fd, SIOCSLIFNAME, &lifr);
3922 			if (retval != -1 || errno != EEXIST)
3923 				break;
3924 		}
3925 	} else {
3926 		ifspec_t ifsp;
3927 
3928 		/*
3929 		 * The interface name could have come from the command-line;
3930 		 * check it.
3931 		 */
3932 		if (!ifparse_ifspec(ifname, &ifsp) || ifsp.ifsp_lunvalid)
3933 			Perror2_exit("invalid IP interface name", ifname);
3934 
3935 		/*
3936 		 * Before we call SIOCSLIFNAME, ensure that the IPMP group
3937 		 * interface for this address family exists.  Otherwise, the
3938 		 * kernel will kick the interface out of the group when we do
3939 		 * the SIOCSLIFNAME.
3940 		 *
3941 		 * Example: suppose bge0 is plumbed for IPv4 and in group "a".
3942 		 * If we're now plumbing bge0 for IPv6, but the IPMP group
3943 		 * interface for "a" is not plumbed for IPv6, the SIOCSLIFNAME
3944 		 * will kick bge0 out of group "a", which is undesired.
3945 		 */
3946 		if (create_ipmp_peer(af, ifname) == -1) {
3947 			(void) fprintf(stderr, "ifconfig: warning: cannot "
3948 			    "create %s IPMP group; %s will be removed from "
3949 			    "group\n", af == AF_INET ? "IPv4" : "IPv6", ifname);
3950 		}
3951 
3952 		lifr.lifr_ppa = ifsp.ifsp_ppa;
3953 		lifr.lifr_flags = flags;
3954 		(void) strlcpy(lifr.lifr_name, ifname, LIFNAMSIZ);
3955 		retval = ioctl(ip_fd, SIOCSLIFNAME, &lifr);
3956 	}
3957 
3958 	if (retval == -1) {
3959 		if (errno != EEXIST)
3960 			Perror0_exit("SIOCSLIFNAME for ip");
3961 		/*
3962 		 * This difference between the way we behave for EEXIST
3963 		 * and that with other errors exists to preserve legacy
3964 		 * behaviour. Earlier when foreachinterface() and matchif()
3965 		 * were doing the duplicate interface name checks, for
3966 		 * already existing interfaces, inetplumb() returned "0".
3967 		 * To preserve this behaviour, Perror0() and return are
3968 		 * called for EEXIST.
3969 		 */
3970 		Perror0("SIOCSLIFNAME for ip");
3971 		return (-1);
3972 	}
3973 
3974 	/* Get the full set of existing flags for this stream */
3975 	if (ioctl(ip_fd, SIOCGLIFFLAGS, (char *)&lifr) == -1)
3976 		Perror0_exit("ifplumb: SIOCGLIFFLAGS");
3977 
3978 	if (debug) {
3979 		(void) printf("ifconfig: ifplumb: %s got flags:\n",
3980 		    lifr.lifr_name);
3981 		print_flags(lifr.lifr_flags);
3982 		(void) putchar('\n');
3983 	}
3984 
3985 	/* Check if arp is not actually needed */
3986 	if (lifr.lifr_flags & (IFF_NOARP|IFF_IPV6)) {
3987 		if (ioctl(ip_fd, I_POP, 0) == -1)
3988 			Perror2_exit("I_POP", ARP_MOD_NAME);
3989 	}
3990 
3991 	/*
3992 	 * Open "/dev/udp" for use as a multiplexor to PLINK the
3993 	 * interface stream under. We use "/dev/udp" instead of "/dev/ip"
3994 	 * since STREAMS will not let you PLINK a driver under itself,
3995 	 * and "/dev/ip" is typically the driver at the bottom of
3996 	 * the stream for tunneling interfaces.
3997 	 */
3998 	if (af == AF_INET6)
3999 		udp_dev_name = UDP6_DEV_NAME;
4000 	else
4001 		udp_dev_name = UDP_DEV_NAME;
4002 	if ((mux_fd = open_arp_on_udp(udp_dev_name)) == -1)
4003 		exit(EXIT_FAILURE);
4004 
4005 	/* Check if arp is not needed */
4006 	if (lifr.lifr_flags & (IFF_NOARP|IFF_IPV6)) {
4007 		/*
4008 		 * PLINK the interface stream so that ifconfig can exit
4009 		 * without tearing down the stream.
4010 		 */
4011 		if ((ip_muxid = ioctl(mux_fd, I_PLINK, ip_fd)) == -1)
4012 			Perror0_exit("I_PLINK for ip");
4013 		(void) close(mux_fd);
4014 		return (lifr.lifr_ppa);
4015 	}
4016 
4017 	/*
4018 	 * This interface does use ARP, so set up a separate stream
4019 	 * from the interface to ARP.
4020 	 *
4021 	 * Note: modules specified by the user are pushed
4022 	 * only on the interface stream, not on the ARP stream.
4023 	 */
4024 	if (debug)
4025 		(void) printf("ifconfig: ifplumb: interface %s", ifname);
4026 
4027 	retval = dlpi_open(linkname, &dh_arp, dlpi_flags);
4028 	if (retval != DLPI_SUCCESS)
4029 		Perrdlpi_exit("cannot open link", linkname, retval);
4030 
4031 	arp_fd = dlpi_fd(dh_arp);
4032 	if (ioctl(arp_fd, I_PUSH, ARP_MOD_NAME) == -1)
4033 		Perror2_exit("I_PUSH", ARP_MOD_NAME);
4034 
4035 	/*
4036 	 * Tell ARP the name and unit number for this interface.
4037 	 * Note that arp has no support for transparent ioctls.
4038 	 */
4039 	if (strioctl(arp_fd, SIOCSLIFNAME, &lifr, sizeof (lifr)) == -1) {
4040 		if (errno != EEXIST)
4041 			Perror0_exit("SIOCSLIFNAME for arp");
4042 		Perror0("SIOCSLIFNAME for arp");
4043 		goto out;
4044 	}
4045 
4046 	/*
4047 	 * PLINK the IP and ARP streams so that ifconfig can exit
4048 	 * without tearing down the stream.
4049 	 */
4050 	if ((ip_muxid = ioctl(mux_fd, I_PLINK, ip_fd)) == -1)
4051 		Perror0_exit("I_PLINK for ip");
4052 	if ((arp_muxid = ioctl(mux_fd, I_PLINK, arp_fd)) == -1) {
4053 		(void) ioctl(mux_fd, I_PUNLINK, ip_muxid);
4054 		Perror0_exit("I_PLINK for arp");
4055 	}
4056 
4057 	if (debug)
4058 		(void) printf("arp muxid = %d\n", arp_muxid);
4059 out:
4060 	dlpi_close(dh_ip);
4061 	dlpi_close(dh_arp);
4062 	(void) close(mux_fd);
4063 	return (lifr.lifr_ppa);
4064 }
4065 
4066 /*
4067  * If this is a physical interface then remove it.
4068  * If it is a logical interface name use SIOCLIFREMOVEIF to
4069  * remove it. In both cases fail if it doesn't exist.
4070  */
4071 /* ARGSUSED */
4072 static int
4073 inetunplumb(char *arg, int64_t param)
4074 {
4075 	int ip_muxid, arp_muxid;
4076 	int mux_fd;
4077 	int muxid_fd;
4078 	char *udp_dev_name;
4079 	char *strptr;
4080 	uint64_t flags;
4081 	boolean_t changed_arp_muxid = _B_FALSE;
4082 	int save_errno;
4083 	boolean_t v6 = (afp->af_af == AF_INET6);
4084 
4085 	strptr = strchr(name, ':');
4086 	if (strptr != NULL || strcmp(name, LOOPBACK_IF) == 0) {
4087 		/* Can't unplumb logical interface zero */
4088 		if (strptr != NULL && strcmp(strptr, ":0") == 0) {
4089 			(void) fprintf(stderr, "ifconfig: unplumb:"
4090 			    " Cannot unplumb %s: Invalid interface\n", name);
4091 			exit(1);
4092 		}
4093 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
4094 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
4095 
4096 		if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0)
4097 			Perror0_exit("unplumb: SIOCLIFREMOVEIF");
4098 		return (0);
4099 	}
4100 
4101 	/*
4102 	 * We used /dev/udp or udp6 to set up the mux. So we have to use
4103 	 * the same now for PUNLINK also.
4104 	 */
4105 	if (v6)
4106 		udp_dev_name = UDP6_DEV_NAME;
4107 	else
4108 		udp_dev_name = UDP_DEV_NAME;
4109 
4110 	if ((muxid_fd = open(udp_dev_name, O_RDWR)) == -1)
4111 		exit(EXIT_FAILURE);
4112 
4113 	if ((mux_fd = open_arp_on_udp(udp_dev_name)) == -1)
4114 		exit(EXIT_FAILURE);
4115 
4116 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
4117 	if (ioctl(muxid_fd, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
4118 		Perror0_exit("unplumb: SIOCGLIFFLAGS");
4119 	}
4120 	flags = lifr.lifr_flags;
4121 
4122 	if (flags & IFF_IPMP) {
4123 		lifgroupinfo_t lifgr;
4124 		ifaddrlistx_t *ifaddrs, *ifaddrp;
4125 
4126 		/*
4127 		 * The kernel will fail the I_PUNLINK if the group still has
4128 		 * members, but check now to provide a better error message.
4129 		 */
4130 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) == -1)
4131 			Perror0_exit("unplumb: SIOCGLIFGROUPNAME");
4132 
4133 		(void) strlcpy(lifgr.gi_grname, lifr.lifr_groupname,
4134 		    LIFGRNAMSIZ);
4135 		if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1)
4136 			Perror0_exit("unplumb: SIOCGLIFGROUPINFO");
4137 
4138 		if ((v6 && lifgr.gi_nv6 != 0) || (!v6 && lifgr.gi_nv4 != 0)) {
4139 			(void) fprintf(stderr, "ifconfig: %s: cannot unplumb:"
4140 			    " IPMP group is not empty\n", name);
4141 			exit(1);
4142 		}
4143 
4144 		/*
4145 		 * The kernel will fail the I_PUNLINK if the IPMP interface
4146 		 * has administratively up addresses; bring 'em down.
4147 		 */
4148 		if (ifaddrlistx(name, IFF_UP|IFF_DUPLICATE, 0, &ifaddrs) == -1)
4149 			Perror2_exit(name, "cannot get address list");
4150 
4151 		ifaddrp = ifaddrs;
4152 		for (; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
4153 			if (((ifaddrp->ia_flags & IFF_IPV6) && !v6) ||
4154 			    (!(ifaddrp->ia_flags & IFF_IPV6) && v6))
4155 				continue;
4156 
4157 			if (!ifaddr_down(ifaddrp)) {
4158 				Perror2_exit(ifaddrp->ia_name,
4159 				    "cannot bring down");
4160 			}
4161 		}
4162 		ifaddrlistx_free(ifaddrs);
4163 	}
4164 
4165 	if (ioctl(muxid_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
4166 		Perror0_exit("unplumb: SIOCGLIFMUXID");
4167 	}
4168 	arp_muxid = lifr.lifr_arp_muxid;
4169 	ip_muxid = lifr.lifr_ip_muxid;
4170 	/*
4171 	 * We don't have a good way of knowing whether the arp stream is
4172 	 * plumbed. We can't rely on IFF_NOARP because someone could
4173 	 * have turned it off later using "ifconfig xxx -arp".
4174 	 */
4175 	if (arp_muxid != 0) {
4176 		if (debug)
4177 			(void) printf("arp_muxid %d\n", arp_muxid);
4178 		if (ioctl(mux_fd, I_PUNLINK, arp_muxid) < 0) {
4179 			if ((errno == EINVAL) &&
4180 			    (flags & (IFF_NOARP | IFF_IPV6))) {
4181 				/*
4182 				 * Some plumbing utilities set the muxid to
4183 				 * -1 or some invalid value to signify that
4184 				 * there is no arp stream. Set the muxid to 0
4185 				 * before trying to unplumb the IP stream.
4186 				 * IP does not allow the IP stream to be
4187 				 * unplumbed if it sees a non-null arp muxid,
4188 				 * for consistency of IP-ARP streams.
4189 				 */
4190 				lifr.lifr_arp_muxid = 0;
4191 				(void) ioctl(muxid_fd, SIOCSLIFMUXID,
4192 				    (caddr_t)&lifr);
4193 				changed_arp_muxid = _B_TRUE;
4194 			} else {
4195 				Perror0("I_PUNLINK for arp");
4196 			}
4197 		}
4198 	}
4199 	if (debug)
4200 		(void) printf("ip_muxid %d\n", ip_muxid);
4201 
4202 	if (ioctl(mux_fd, I_PUNLINK, ip_muxid) < 0) {
4203 		if (changed_arp_muxid) {
4204 			/*
4205 			 * Some error occurred, and we need to restore
4206 			 * everything back to what it was.
4207 			 */
4208 			save_errno = errno;
4209 			lifr.lifr_arp_muxid = arp_muxid;
4210 			lifr.lifr_ip_muxid = ip_muxid;
4211 			(void) ioctl(muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
4212 			errno = save_errno;
4213 		}
4214 		Perror0_exit("I_PUNLINK for ip");
4215 	}
4216 	(void) close(mux_fd);
4217 	(void) close(muxid_fd);
4218 	return (0);
4219 }
4220 
4221 /*
4222  * If this is a physical interface then create it unless it is already
4223  * present. If it is a logical interface name use SIOCLIFADDIF to
4224  * create and (and fail it if already exists.)
4225  * As a special case send SIOCLIFADDIF for the loopback interface. This
4226  * is needed since there is no other notion of plumbing the loopback
4227  * interface.
4228  */
4229 /* ARGSUSED */
4230 static int
4231 inetplumb(char *arg, int64_t param)
4232 {
4233 	char		*strptr;
4234 	boolean_t	islo;
4235 	zoneid_t	zoneid;
4236 
4237 	strptr = strchr(name, ':');
4238 	islo = (strcmp(name, LOOPBACK_IF) == 0);
4239 
4240 	if (strptr != NULL || islo) {
4241 		(void) memset(&lifr, 0, sizeof (lifr));
4242 		(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
4243 		if (islo && ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0) {
4244 			if (debug) {
4245 				(void) fprintf(stderr,
4246 				    "ifconfig: %s already exists\n", name);
4247 			}
4248 			return (0);
4249 		}
4250 		if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
4251 			if (errno == EEXIST) {
4252 				if (debug) {
4253 					(void) fprintf(stderr,
4254 					    "ifconfig: %s already exists\n",
4255 					    name);
4256 				}
4257 			} else {
4258 				Perror2_exit("plumb: SIOCLIFADDIF", name);
4259 			}
4260 		}
4261 		return (0);
4262 	}
4263 
4264 	/*
4265 	 * For global zone, check if the interface is used by a non-global
4266 	 * zone, note that the non-global zones doesn't need this check,
4267 	 * because zoneadm has taken care of this when the zone boots.
4268 	 */
4269 	zoneid = getzoneid();
4270 	if (zoneid == GLOBAL_ZONEID) {
4271 		int ret;
4272 
4273 		zoneid = ALL_ZONES;
4274 		ret = zone_check_datalink(&zoneid, name);
4275 		if (ret == 0) {
4276 			char zonename[ZONENAME_MAX];
4277 
4278 			(void) getzonenamebyid(zoneid, zonename, ZONENAME_MAX);
4279 			(void) fprintf(stderr, "%s is used by non-global"
4280 			    "zone: %s\n", name, zonename);
4281 			return (1);
4282 		}
4283 	}
4284 
4285 	if (debug)
4286 		(void) printf("inetplumb: %s af %d\n", name, afp->af_af);
4287 
4288 	(void) ifplumb(name, name, _B_FALSE, afp->af_af);
4289 	return (0);
4290 }
4291 
4292 /* ARGSUSED */
4293 static int
4294 inetipmp(char *arg, int64_t param)
4295 {
4296 	int retval;
4297 
4298 	/*
4299 	 * Treat e.g. "ifconfig ipmp0:2 ipmp" as "ifconfig ipmp0:2 plumb".
4300 	 * Otherwise, try to create the requested IPMP interface.
4301 	 */
4302 	if (strchr(name, ':') != NULL)
4303 		retval = inetplumb(arg, param);
4304 	else
4305 		retval = create_ipmp(name, afp->af_af, name, _B_FALSE);
4306 
4307 	/*
4308 	 * We'd return -1, but foreachinterface() doesn't propagate the error
4309 	 * into the exit status, so we're forced to explicitly exit().
4310 	 */
4311 	if (retval == -1)
4312 		exit(1);
4313 	return (0);
4314 }
4315 
4316 /*
4317  * Create an IPMP group `grname' with address family `af'.  If `ifname' is
4318  * non-NULL, it specifies the interface name to use.  Otherwise, use the name
4319  * ipmpN, where N corresponds to the lowest available integer.  If `implicit'
4320  * is set, then the group is being created as a side-effect of placing an
4321  * underlying interface in a group.  Also start in.mpathd if necessary.
4322  */
4323 static int
4324 create_ipmp(const char *grname, int af, const char *ifname, boolean_t implicit)
4325 {
4326 	int ppa;
4327 	static int ipmp_daemon_started;
4328 
4329 	if (debug) {
4330 		(void) printf("create_ipmp: ifname %s grname %s af %d\n",
4331 		    ifname != NULL ? ifname : "NULL", grname, af);
4332 	}
4333 
4334 	if (ifname != NULL)
4335 		ppa = ifplumb(IPMPSTUB, ifname, _B_FALSE, af);
4336 	else
4337 		ppa = ifplumb(IPMPSTUB, "ipmp", _B_TRUE, af);
4338 
4339 	if (ppa == -1) {
4340 		Perror2(grname, "cannot create IPMP interface");
4341 		return (-1);
4342 	}
4343 
4344 	if (ifname != NULL)
4345 		(void) strlcpy(lifr.lifr_name, ifname, LIFNAMSIZ);
4346 	else
4347 		(void) snprintf(lifr.lifr_name, LIFNAMSIZ, "ipmp%d", ppa);
4348 
4349 	/*
4350 	 * To preserve backward-compatibility, always bring up the link-local
4351 	 * address for implicitly-created IPv6 IPMP interfaces.
4352 	 */
4353 	if (implicit && af == AF_INET6) {
4354 		if (ioctl(s6, SIOCGLIFFLAGS, &lifr) == 0) {
4355 			lifr.lifr_flags |= IFF_UP;
4356 			(void) ioctl(s6, SIOCSLIFFLAGS, &lifr);
4357 		}
4358 	}
4359 
4360 	/*
4361 	 * If the caller requested a different group name, issue a
4362 	 * SIOCSLIFGROUPNAME on the new IPMP interface.
4363 	 */
4364 	if (strcmp(lifr.lifr_name, grname) != 0) {
4365 		(void) strlcpy(lifr.lifr_groupname, grname, LIFGRNAMSIZ);
4366 		if (ioctl(s, SIOCSLIFGROUPNAME, &lifr) == -1) {
4367 			Perror0("SIOCSLIFGROUPNAME");
4368 			return (-1);
4369 		}
4370 	}
4371 
4372 	/*
4373 	 * If we haven't done so yet, ensure in.mpathd is started.
4374 	 */
4375 	if (ipmp_daemon_started++ == 0)
4376 		start_ipmp_daemon();
4377 
4378 	return (0);
4379 }
4380 
4381 /*
4382  * Check if `ifname' is plumbed and in an IPMP group on its "other" address
4383  * family.  If so, create a matching IPMP group for address family `af'.
4384  */
4385 static int
4386 create_ipmp_peer(int af, const char *ifname)
4387 {
4388 	int		fd;
4389 	lifgroupinfo_t	lifgr;
4390 
4391 	assert(af == AF_INET || af == AF_INET6);
4392 
4393 	/*
4394 	 * Get the socket for the "other" address family.
4395 	 */
4396 	fd = (af == AF_INET) ? s6 : s4;
4397 
4398 	(void) strlcpy(lifr.lifr_name, ifname, LIFNAMSIZ);
4399 	if (ioctl(fd, SIOCGLIFGROUPNAME, &lifr) != 0)
4400 		return (0);
4401 
4402 	(void) strlcpy(lifgr.gi_grname, lifr.lifr_groupname, LIFGRNAMSIZ);
4403 	if (ioctl(fd, SIOCGLIFGROUPINFO, &lifgr) != 0)
4404 		return (0);
4405 
4406 	/*
4407 	 * If `ifname' *is* the IPMP group interface, or if the relevant
4408 	 * address family is already configured, then there's nothing to do.
4409 	 */
4410 	if (strcmp(lifgr.gi_grifname, ifname) == 0 ||
4411 	    (af == AF_INET && lifgr.gi_v4) || (af == AF_INET6 && lifgr.gi_v6))
4412 		return (0);
4413 
4414 	return (create_ipmp(lifgr.gi_grname, af, lifgr.gi_grifname, _B_TRUE));
4415 }
4416 
4417 /*
4418  * Start in.mpathd if it's not already running.
4419  */
4420 static void
4421 start_ipmp_daemon(void)
4422 {
4423 	int retval;
4424 	ipmp_handle_t ipmp_handle;
4425 
4426 	/*
4427 	 * Ping in.mpathd to see if it's running already.
4428 	 */
4429 	if ((retval = ipmp_open(&ipmp_handle)) != IPMP_SUCCESS) {
4430 		(void) fprintf(stderr, "ifconfig: cannot create IPMP handle: "
4431 		    "%s\n", ipmp_errmsg(retval));
4432 		return;
4433 	}
4434 
4435 	retval = ipmp_ping_daemon(ipmp_handle);
4436 	ipmp_close(ipmp_handle);
4437 
4438 	switch (retval) {
4439 	case IPMP_ENOMPATHD:
4440 		break;
4441 	case IPMP_SUCCESS:
4442 		return;
4443 	default:
4444 		(void) fprintf(stderr, "ifconfig: cannot ping in.mpathd: %s\n",
4445 		    ipmp_errmsg(retval));
4446 		break;
4447 	}
4448 
4449 	/*
4450 	 * Start in.mpathd.  Note that in.mpathd will handle multiple
4451 	 * incarnations (ipmp_ping_daemon() is just an optimization) so we
4452 	 * don't need to worry about racing with another ifconfig process.
4453 	 */
4454 	switch (fork()) {
4455 	case -1:
4456 		Perror0_exit("start_ipmp_daemon: fork");
4457 		/* NOTREACHED */
4458 	case 0:
4459 		(void) execl(MPATHD_PATH, MPATHD_PATH, NULL);
4460 		_exit(1);
4461 		/* NOTREACHED */
4462 	default:
4463 		break;
4464 	}
4465 }
4466 
4467 /*
4468  * Bring the address named by `ifaddrp' up or down.  Doesn't trust any mutable
4469  * values in ia_flags since they may be stale.
4470  */
4471 static boolean_t
4472 ifaddr_op(ifaddrlistx_t *ifaddrp, boolean_t up)
4473 {
4474 	struct lifreq	lifrl;	/* Local lifreq struct */
4475 	int		fd = (ifaddrp->ia_flags & IFF_IPV4) ? s4 : s6;
4476 
4477 	(void) memset(&lifrl, 0, sizeof (lifrl));
4478 	(void) strlcpy(lifrl.lifr_name, ifaddrp->ia_name, LIFNAMSIZ);
4479 	if (ioctl(fd, SIOCGLIFFLAGS, &lifrl) == -1)
4480 		return (_B_FALSE);
4481 
4482 	if (up) {
4483 		lifrl.lifr_flags |= IFF_UP;
4484 	} else {
4485 		/*
4486 		 * If we've been asked to bring down an IFF_DUPLICATE address,
4487 		 * then get the address and set it.  This will cause IP to
4488 		 * clear IFF_DUPLICATE and stop the automatic recovery timer.
4489 		 */
4490 		if (lifrl.lifr_flags & IFF_DUPLICATE) {
4491 			return (ioctl(fd, SIOCGLIFADDR, &lifrl) != -1 &&
4492 			    ioctl(fd, SIOCSLIFADDR, &lifrl) != -1);
4493 		}
4494 		lifrl.lifr_flags &= ~IFF_UP;
4495 	}
4496 	return (ioctl(fd, SIOCSLIFFLAGS, &lifrl) == 0);
4497 }
4498 
4499 static boolean_t
4500 ifaddr_up(ifaddrlistx_t *ifaddrp)
4501 {
4502 	return (ifaddr_op(ifaddrp, _B_TRUE));
4503 }
4504 
4505 static boolean_t
4506 ifaddr_down(ifaddrlistx_t *ifaddrp)
4507 {
4508 	return (ifaddr_op(ifaddrp, _B_FALSE));
4509 }
4510 
4511 void
4512 Perror0(const char *cmd)
4513 {
4514 	Perror2(cmd, lifr.lifr_name);
4515 }
4516 
4517 void
4518 Perror0_exit(const char *cmd)
4519 {
4520 	Perror0(cmd);
4521 	exit(1);
4522 	/* NOTREACHED */
4523 }
4524 
4525 void
4526 Perror2(const char *cmd, const char *str)
4527 {
4528 	int error = errno;
4529 
4530 	(void) fprintf(stderr, "ifconfig: %s: ", cmd);
4531 
4532 	switch (error) {
4533 	case ENXIO:
4534 		(void) fprintf(stderr, "%s: no such interface\n", str);
4535 		break;
4536 	case EPERM:
4537 		(void) fprintf(stderr, "%s: permission denied\n", str);
4538 		break;
4539 	case EEXIST:
4540 		(void) fprintf(stderr, "%s: already exists\n", str);
4541 		break;
4542 	default:
4543 		errno = error;
4544 		perror(str);
4545 	}
4546 }
4547 
4548 /*
4549  * Print out error message (Perror2()) and exit
4550  */
4551 void
4552 Perror2_exit(const char *cmd, const char *str)
4553 {
4554 	Perror2(cmd, str);
4555 	exit(1);
4556 	/* NOTREACHED */
4557 }
4558 
4559 void
4560 Perrdlpi(const char *cmd, const char *linkname, int err)
4561 {
4562 	(void) fprintf(stderr, "ifconfig: %s \"%s\": %s\n", cmd,
4563 	    linkname, dlpi_strerror(err));
4564 }
4565 
4566 /*
4567  * Print out error message (Perrdlpi()) and exit
4568  */
4569 void
4570 Perrdlpi_exit(const char *cmd, const char *linkname, int err)
4571 {
4572 	Perrdlpi(cmd, linkname, err);
4573 	exit(1);
4574 }
4575 
4576 /*
4577  * If the last argument is non-NULL allow a <addr>/<n> syntax and
4578  * pass out <n> in *plenp.
4579  * If <n> doesn't parse return BAD_ADDR as *plenp.
4580  * If no /<n> is present return NO_PREFIX as *plenp.
4581  */
4582 static void
4583 in_getaddr(char *s, struct sockaddr *saddr, int *plenp)
4584 {
4585 	/* LINTED: alignment */
4586 	struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
4587 	struct hostent *hp;
4588 	struct netent *np;
4589 	char str[BUFSIZ];
4590 	int error_num;
4591 
4592 	(void) strncpy(str, s, sizeof (str));
4593 
4594 	/*
4595 	 * Look for '/'<n> is plenp
4596 	 */
4597 	if (plenp != NULL) {
4598 		char *cp;
4599 
4600 		*plenp = in_getprefixlen(str, _B_TRUE, IP_ABITS);
4601 		if (*plenp == BAD_ADDR)
4602 			return;
4603 		cp = strchr(str, '/');
4604 		if (cp != NULL)
4605 			*cp = '\0';
4606 	} else if (strchr(str, '/') != NULL) {
4607 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
4608 		exit(1);
4609 	}
4610 
4611 	(void) memset(sin, 0, sizeof (*sin));
4612 
4613 	/*
4614 	 *	Try to catch attempts to set the broadcast address to all 1's.
4615 	 */
4616 	if (strcmp(str, "255.255.255.255") == 0 ||
4617 	    (strtoul(str, (char **)NULL, 0) == 0xffffffffUL)) {
4618 		sin->sin_family = AF_INET;
4619 		sin->sin_addr.s_addr = 0xffffffff;
4620 		return;
4621 	}
4622 
4623 	hp = getipnodebyname(str, AF_INET, 0, &error_num);
4624 	if (hp) {
4625 		sin->sin_family = hp->h_addrtype;
4626 		(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
4627 		freehostent(hp);
4628 		return;
4629 	}
4630 	np = getnetbyname(str);
4631 	if (np) {
4632 		sin->sin_family = np->n_addrtype;
4633 		sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
4634 		return;
4635 	}
4636 	if (error_num == TRY_AGAIN) {
4637 		(void) fprintf(stderr, "ifconfig: %s: bad address "
4638 		    "(try again later)\n", s);
4639 	} else {
4640 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
4641 	}
4642 	exit(1);
4643 }
4644 
4645 /*
4646  * If the last argument is non-NULL allow a <addr>/<n> syntax and
4647  * pass out <n> in *plenp.
4648  * If <n> doesn't parse return BAD_ADDR as *plenp.
4649  * If no /<n> is present return NO_PREFIX as *plenp.
4650  */
4651 static void
4652 in6_getaddr(char *s, struct sockaddr *saddr, int *plenp)
4653 {
4654 	/* LINTED: alignment */
4655 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)saddr;
4656 	struct hostent *hp;
4657 	char str[BUFSIZ];
4658 	int error_num;
4659 
4660 	(void) strncpy(str, s, sizeof (str));
4661 
4662 	/*
4663 	 * Look for '/'<n> is plenp
4664 	 */
4665 	if (plenp != NULL) {
4666 		char *cp;
4667 
4668 		*plenp = in_getprefixlen(str, _B_TRUE, IPV6_ABITS);
4669 		if (*plenp == BAD_ADDR)
4670 			return;
4671 		cp = strchr(str, '/');
4672 		if (cp != NULL)
4673 			*cp = '\0';
4674 	} else if (strchr(str, '/') != NULL) {
4675 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
4676 		exit(1);
4677 	}
4678 
4679 	(void) memset(sin6, 0, sizeof (*sin6));
4680 
4681 	hp = getipnodebyname(str, AF_INET6, 0, &error_num);
4682 	if (hp) {
4683 		sin6->sin6_family = hp->h_addrtype;
4684 		(void) memcpy(&sin6->sin6_addr, hp->h_addr, hp->h_length);
4685 		freehostent(hp);
4686 		return;
4687 	}
4688 	if (error_num == TRY_AGAIN) {
4689 		(void) fprintf(stderr, "ifconfig: %s: bad address "
4690 		    "(try again later)\n", s);
4691 	} else {
4692 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
4693 	}
4694 	exit(1);
4695 }
4696 
4697 /*
4698  * If "slash" is zero this parses the whole string as
4699  * an integer. With "slash" non zero it parses the tail part as an integer.
4700  *
4701  * If it is not a valid integer this returns BAD_ADDR.
4702  * If there is /<n> present this returns NO_PREFIX.
4703  */
4704 static int
4705 in_getprefixlen(char *addr, boolean_t slash, int max_plen)
4706 {
4707 	int prefixlen;
4708 	char *str, *end;
4709 
4710 	if (slash) {
4711 		str = strchr(addr, '/');
4712 		if (str == NULL)
4713 			return (NO_PREFIX);
4714 		str++;
4715 	} else
4716 		str = addr;
4717 
4718 	prefixlen = strtol(str, &end, 10);
4719 	if (prefixlen < 0)
4720 		return (BAD_ADDR);
4721 	if (str == end)
4722 		return (BAD_ADDR);
4723 	if (max_plen != 0 && max_plen < prefixlen)
4724 		return (BAD_ADDR);
4725 	return (prefixlen);
4726 }
4727 
4728 /*
4729  * Convert a prefix length to a mask.
4730  * Returns 1 if ok. 0 otherwise.
4731  * Assumes the mask array is zero'ed by the caller.
4732  */
4733 static boolean_t
4734 in_prefixlentomask(int prefixlen, int maxlen, uchar_t *mask)
4735 {
4736 	if (prefixlen < 0 || prefixlen > maxlen)
4737 		return (0);
4738 
4739 	while (prefixlen > 0) {
4740 		if (prefixlen >= 8) {
4741 			*mask++ = 0xFF;
4742 			prefixlen -= 8;
4743 			continue;
4744 		}
4745 		*mask |= 1 << (8 - prefixlen);
4746 		prefixlen--;
4747 	}
4748 	return (1);
4749 }
4750 
4751 static void
4752 print_flags(uint64_t flags)
4753 {
4754 	boolean_t first = _B_TRUE;
4755 	int cnt, i;
4756 
4757 	(void) printf("flags=%llx", flags);
4758 	cnt = sizeof (if_flags_tbl) / sizeof (if_flags_t);
4759 	for (i = 0; i < cnt; i++) {
4760 		if (flags & if_flags_tbl[i].iff_value) {
4761 			if (first) {
4762 				(void) printf("<");
4763 				first = _B_FALSE;
4764 			} else {
4765 				/*
4766 				 * It has to be here and not with the
4767 				 * printf below because for the last one,
4768 				 * we don't want a comma before the ">".
4769 				 */
4770 				(void) printf(",");
4771 			}
4772 			(void) printf("%s", if_flags_tbl[i].iff_name);
4773 		}
4774 	}
4775 	if (!first)
4776 		(void) printf(">");
4777 }
4778 
4779 static void
4780 print_config_flags(int af, uint64_t flags)
4781 {
4782 	if_config_cmd_t *cmdp;
4783 
4784 	for (cmdp = if_config_cmd_tbl; cmdp->iff_flag != 0; cmdp++) {
4785 		if ((flags & cmdp->iff_flag) &&
4786 		    (cmdp->iff_af == AF_UNSPEC || cmdp->iff_af == af)) {
4787 			(void) printf("%s ", cmdp->iff_name);
4788 		}
4789 	}
4790 }
4791 
4792 /*
4793  * Use the configured directory lookup mechanism (e.g. files/NIS/NIS+/...)
4794  * to find the network mask.  Returns true if we found one to set.
4795  *
4796  * The parameter addr_set controls whether we should get the address of
4797  * the working interface for the netmask query.  If addr_set is true,
4798  * we will use the address provided.  Otherwise, we will find the working
4799  * interface's address and use it instead.
4800  */
4801 static boolean_t
4802 in_getmask(struct sockaddr_in *saddr, boolean_t addr_set)
4803 {
4804 	struct sockaddr_in ifaddr;
4805 
4806 	/*
4807 	 * Read the address from the interface if it is not passed in.
4808 	 */
4809 	if (!addr_set) {
4810 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
4811 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
4812 			if (errno != EADDRNOTAVAIL) {
4813 				(void) fprintf(stderr, "Need net number for "
4814 				    "mask\n");
4815 			}
4816 			return (_B_FALSE);
4817 		}
4818 		ifaddr = *((struct sockaddr_in *)&lifr.lifr_addr);
4819 	} else {
4820 		ifaddr.sin_addr = saddr->sin_addr;
4821 	}
4822 	if (getnetmaskbyaddr(ifaddr.sin_addr, &saddr->sin_addr) == 0) {
4823 		saddr->sin_family = AF_INET;
4824 		return (_B_TRUE);
4825 	}
4826 	return (_B_FALSE);
4827 }
4828 
4829 static int
4830 lifnum(const char *ifname)
4831 {
4832 	const char *cp;
4833 
4834 	if ((cp = strchr(ifname, ':')) == NULL)
4835 		return (0);
4836 	else
4837 		return (atoi(cp + 1));
4838 }
4839 
4840 static int
4841 strioctl(int s, int cmd, void *buf, int buflen)
4842 {
4843 	struct strioctl ioc;
4844 
4845 	(void) memset(&ioc, 0, sizeof (ioc));
4846 	ioc.ic_cmd = cmd;
4847 	ioc.ic_timout = 0;
4848 	ioc.ic_len = buflen;
4849 	ioc.ic_dp = buf;
4850 	return (ioctl(s, I_STR, (char *)&ioc));
4851 }
4852 
4853 static void
4854 add_ni(const char *name)
4855 {
4856 	ni_t **pp;
4857 	ni_t *p;
4858 
4859 	for (pp = &ni_list; (p = *pp) != NULL; pp = &(p->ni_next)) {
4860 		if (strcmp(p->ni_name, name) == 0) {
4861 			if (debug > 2)
4862 				(void) fprintf(stderr, "'%s' is a duplicate\n",
4863 				    name);
4864 			return;
4865 		}
4866 	}
4867 
4868 	if (debug > 2)
4869 		(void) fprintf(stderr, "adding '%s'\n",
4870 		    name);
4871 
4872 	if ((p = malloc(sizeof (ni_t))) == NULL)
4873 		return;
4874 
4875 	(void) strlcpy(p->ni_name, name, sizeof (p->ni_name));
4876 	p->ni_next = NULL;
4877 
4878 	*pp = p;
4879 	num_ni++;
4880 }
4881 
4882 static boolean_t
4883 ni_entry(const char *linkname, void *arg)
4884 {
4885 	dlpi_handle_t	dh;
4886 	datalink_class_t class;
4887 
4888 	(void) dladm_name2info(arg, linkname, NULL, NULL, &class, NULL);
4889 
4890 	if (class == DATALINK_CLASS_ETHERSTUB)
4891 		return (_B_FALSE);
4892 	if (dlpi_open(linkname, &dh, 0) != DLPI_SUCCESS)
4893 		return (_B_FALSE);
4894 
4895 	add_ni(linkname);
4896 
4897 	dlpi_close(dh);
4898 	return (_B_FALSE);
4899 }
4900 
4901 /*
4902  * dhcp-related routines
4903  */
4904 
4905 static int
4906 setifdhcp(const char *caller, const char *ifname, int argc, char *argv[])
4907 {
4908 	dhcp_ipc_request_t	*request;
4909 	dhcp_ipc_reply_t	*reply	= NULL;
4910 	int			timeout = DHCP_IPC_WAIT_DEFAULT;
4911 	dhcp_ipc_type_t		type	= DHCP_START;
4912 	int			error;
4913 	boolean_t		is_primary = _B_FALSE;
4914 	boolean_t		started = _B_FALSE;
4915 
4916 	for (argv++; --argc > 0; argv++) {
4917 
4918 		if (strcmp(*argv, "primary") == 0) {
4919 			is_primary = _B_TRUE;
4920 			continue;
4921 		}
4922 
4923 		if (strcmp(*argv, "wait") == 0) {
4924 			if (--argc <= 0) {
4925 				usage();
4926 				return (DHCP_EXIT_BADARGS);
4927 			}
4928 			argv++;
4929 
4930 			if (strcmp(*argv, "forever") == 0) {
4931 				timeout = DHCP_IPC_WAIT_FOREVER;
4932 				continue;
4933 			}
4934 
4935 			if (sscanf(*argv, "%d", &timeout) != 1) {
4936 				usage();
4937 				return (DHCP_EXIT_BADARGS);
4938 			}
4939 
4940 			if (timeout < 0) {
4941 				usage();
4942 				return (DHCP_EXIT_BADARGS);
4943 			}
4944 			continue;
4945 		}
4946 
4947 		type = dhcp_string_to_request(*argv);
4948 		if (type == -1) {
4949 			usage();
4950 			return (DHCP_EXIT_BADARGS);
4951 		}
4952 	}
4953 
4954 	/*
4955 	 * Only try to start agent on start or inform; in all other cases it
4956 	 * has to already be running for anything to make sense.
4957 	 */
4958 	if (type == DHCP_START || type == DHCP_INFORM) {
4959 		if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1) {
4960 			(void) fprintf(stderr, "%s: unable to start %s\n",
4961 			    caller, DHCP_AGENT_PATH);
4962 			return (DHCP_EXIT_FAILURE);
4963 		}
4964 		started = _B_TRUE;
4965 	}
4966 
4967 	if (is_primary)
4968 		type |= DHCP_PRIMARY;
4969 
4970 	if (af != AF_INET)
4971 		type |= DHCP_V6;
4972 
4973 	request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
4974 	if (request == NULL) {
4975 		(void) fprintf(stderr, "%s: out of memory\n", caller);
4976 		return (DHCP_EXIT_SYSTEM);
4977 	}
4978 
4979 	error = dhcp_ipc_make_request(request, &reply, timeout);
4980 	if (error != 0) {
4981 		free(request);
4982 		/*
4983 		 * Re-map connect error to not under control if we didn't try a
4984 		 * start operation, as this has to be true and results in a
4985 		 * clearer message, not to mention preserving compatibility
4986 		 * with the days when we always started dhcpagent for every
4987 		 * request.
4988 		 */
4989 		if (error == DHCP_IPC_E_CONNECT && !started)
4990 			error = DHCP_IPC_E_UNKIF;
4991 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
4992 		    dhcp_ipc_strerror(error));
4993 		return (DHCP_EXIT_FAILURE);
4994 	}
4995 
4996 	error = reply->return_code;
4997 	if (error != 0) {
4998 		free(request);
4999 		free(reply);
5000 
5001 		if (error == DHCP_IPC_E_TIMEOUT && timeout == 0)
5002 			return (DHCP_EXIT_SUCCESS);
5003 
5004 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
5005 		    dhcp_ipc_strerror(error));
5006 
5007 		if (error == DHCP_IPC_E_TIMEOUT)
5008 			return (DHCP_EXIT_TIMEOUT);
5009 		else
5010 			return (DHCP_EXIT_IF_FAILURE);
5011 	}
5012 
5013 	if (DHCP_IPC_CMD(type) == DHCP_STATUS) {
5014 		(void) printf("%s", dhcp_status_hdr_string());
5015 		(void) printf("%s", dhcp_status_reply_to_string(reply));
5016 	}
5017 
5018 	free(request);
5019 	free(reply);
5020 	return (DHCP_EXIT_SUCCESS);
5021 }
5022 
5023 static void
5024 usage(void)
5025 {
5026 	(void) fprintf(stderr,
5027 	    "usage: ifconfig <interface> | -a[ 4 | 6 | D ][ u | d ][ Z ]\n");
5028 
5029 	(void) fprintf(stderr, "%s",
5030 	    "\t[ <addr_family> ]\n"
5031 	    "\t[ <address>[/<prefix_length>] [ <dest_address> ] ]\n"
5032 	    "\t[ set [ <address>][/<prefix_length>] ]"
5033 	    " [ <address>/<prefix_length>] ]\n"
5034 	    "\t[ destination <dest_address> ]\n"
5035 	    "\t[ addif <address>[/<prefix_length>]"
5036 	    "  [ <dest_address> ] ]\n"
5037 	    "\t[ removeif <address>[/<prefix_length>] ]\n"
5038 	    "\t[ arp | -arp ]\n"
5039 	    "\t[ auto-revarp ]\n"
5040 	    "\t[ broadcast <broad_addr> ]\n"
5041 	    "\t[ index <if_index> ]\n"
5042 	    "\t[ metric <n> ] [ mtu <n> ]\n"
5043 	    "\t[ netmask <mask> ]\n"
5044 	    "\t[ plumb ] [ unplumb ]\n"
5045 	    "\t[ preferred | -preferred ]\n"
5046 	    "\t[ private | -private ]\n"
5047 	    "\t[ local | -local ]\n"
5048 	    "\t[ router | -router ]\n"
5049 	    "\t[ subnet <subnet_address>]\n"
5050 	    "\t[ trailers | -trailers ]\n"
5051 	    "\t[ token <address>/<prefix_length> ]\n"
5052 	    "\t[ tsrc <tunnel_src_address> ]\n"
5053 	    "\t[ tdst <tunnel_dest_address> ]\n"
5054 	    "\t[ auth_algs <tunnel_AH_authentication_algorithm> ]\n"
5055 	    "\t[ encr_algs <tunnel_ESP_encryption_algorithm> ]\n"
5056 	    "\t[ encr_auth_algs <tunnel_ESP_authentication_algorithm> ]\n"
5057 	    "\t[ up ] [ down ]\n"
5058 	    "\t[ xmit | -xmit ]\n"
5059 	    "\t[ modlist ]\n"
5060 	    "\t[ modinsert <module_name@position> ]\n"
5061 	    "\t[ modremove <module_name@position> ]\n"
5062 	    "\t[ ipmp ]\n"
5063 	    "\t[ group <groupname>] | [ group \"\"]\n"
5064 	    "\t[ deprecated | -deprecated ]\n"
5065 	    "\t[ standby | -standby ]\n"
5066 	    "\t[ failover | -failover ]\n"
5067 	    "\t[ zone <zonename> | -zone ]\n"
5068 	    "\t[ usesrc <interface> ]\n"
5069 	    "\t[ all-zones ]\n");
5070 
5071 	(void) fprintf(stderr, "or\n");
5072 	(void) fprintf(stderr,
5073 	    "\tifconfig <interface> |  -a[ 4 | 6 | D ] [ u | d ]\n");
5074 
5075 	(void) fprintf(stderr, "%s", "\tauto-dhcp | dhcp\n"
5076 	    "\t[ wait <time> | forever ]\n\t[ primary ]\n"
5077 	    "\tstart | drop | ping | release | status | inform\n");
5078 }
5079