17c478bd9Sstevel@tonic-gate /*
245916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate /*
67c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
77c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
87c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
97c478bd9Sstevel@tonic-gate  */
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate #include "defs.h"
147c478bd9Sstevel@tonic-gate #include "strings.h"
157c478bd9Sstevel@tonic-gate #include "ifconfig.h"
167c478bd9Sstevel@tonic-gate #include <compat.h>
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include <sys/dlpi.h>
197c478bd9Sstevel@tonic-gate #include <libdlpi.h>
207c478bd9Sstevel@tonic-gate 
21ff550d0eSmasputra #include <inet/ip.h>
22ff550d0eSmasputra 
237c478bd9Sstevel@tonic-gate #define	LOOPBACK_IF	"lo0"
247c478bd9Sstevel@tonic-gate #define	NONE_STR	"none"
257c478bd9Sstevel@tonic-gate #define	ARP_MOD_NAME	"arp"
267906a3e0Smeem #define	TUN_NAME	"tun"
277906a3e0Smeem #define	ATUN_NAME	"atun"
287906a3e0Smeem #define	TUN6TO4_NAME	"6to4tun"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate typedef struct if_flags {
317c478bd9Sstevel@tonic-gate 	uint64_t iff_value;
327c478bd9Sstevel@tonic-gate 	char	*iff_name;
337c478bd9Sstevel@tonic-gate } if_flags_t;
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate static if_flags_t	if_flags_tbl[] = {
367c478bd9Sstevel@tonic-gate 	{ IFF_UP,		"UP" },
377c478bd9Sstevel@tonic-gate 	{ IFF_BROADCAST,	"BROADCAST" },
387c478bd9Sstevel@tonic-gate 	{ IFF_DEBUG,		"DEBUG" },
397c478bd9Sstevel@tonic-gate 	{ IFF_LOOPBACK,		"LOOPBACK" },
407c478bd9Sstevel@tonic-gate 	{ IFF_POINTOPOINT,	"POINTOPOINT" },
417c478bd9Sstevel@tonic-gate 	{ IFF_NOTRAILERS,	"NOTRAILERS" },
427c478bd9Sstevel@tonic-gate 	{ IFF_RUNNING,		"RUNNING" },
437c478bd9Sstevel@tonic-gate 	{ IFF_NOARP,		"NOARP" },
447c478bd9Sstevel@tonic-gate 	{ IFF_PROMISC,		"PROMISC" },
457c478bd9Sstevel@tonic-gate 	{ IFF_ALLMULTI,		"ALLMULTI" },
467c478bd9Sstevel@tonic-gate 	{ IFF_INTELLIGENT,	"INTELLIGENT" },
477c478bd9Sstevel@tonic-gate 	{ IFF_MULTICAST,	"MULTICAST" },
487c478bd9Sstevel@tonic-gate 	{ IFF_MULTI_BCAST,	"MULTI_BCAST" },
497c478bd9Sstevel@tonic-gate 	{ IFF_UNNUMBERED,	"UNNUMBERED" },
507c478bd9Sstevel@tonic-gate 	{ IFF_DHCPRUNNING,	"DHCP" },
517c478bd9Sstevel@tonic-gate 	{ IFF_PRIVATE,		"PRIVATE" },
527c478bd9Sstevel@tonic-gate 	{ IFF_NOXMIT,		"NOXMIT" },
537c478bd9Sstevel@tonic-gate 	{ IFF_NOLOCAL,		"NOLOCAL" },
547c478bd9Sstevel@tonic-gate 	{ IFF_DEPRECATED,	"DEPRECATED" },
557c478bd9Sstevel@tonic-gate 	{ IFF_ADDRCONF,		"ADDRCONF" },
567c478bd9Sstevel@tonic-gate 	{ IFF_ROUTER,		"ROUTER" },
577c478bd9Sstevel@tonic-gate 	{ IFF_NONUD,		"NONUD" },
587c478bd9Sstevel@tonic-gate 	{ IFF_ANYCAST,		"ANYCAST" },
597c478bd9Sstevel@tonic-gate 	{ IFF_NORTEXCH,		"NORTEXCH" },
607c478bd9Sstevel@tonic-gate 	{ IFF_IPV4,		"IPv4" },
617c478bd9Sstevel@tonic-gate 	{ IFF_IPV6,		"IPv6" },
627c478bd9Sstevel@tonic-gate 	{ IFF_MIPRUNNING,	"MIP" },
637c478bd9Sstevel@tonic-gate 	{ IFF_NOFAILOVER,	"NOFAILOVER" },
647c478bd9Sstevel@tonic-gate 	{ IFF_FAILED,		"FAILED" },
657c478bd9Sstevel@tonic-gate 	{ IFF_STANDBY,		"STANDBY" },
667c478bd9Sstevel@tonic-gate 	{ IFF_INACTIVE,		"INACTIVE" },
677c478bd9Sstevel@tonic-gate 	{ IFF_OFFLINE,		"OFFLINE" },
687c478bd9Sstevel@tonic-gate 	{ IFF_XRESOLV,		"XRESOLV" },
697c478bd9Sstevel@tonic-gate 	{ IFF_COS_ENABLED,	"CoS" },
707c478bd9Sstevel@tonic-gate 	{ IFF_PREFERRED,	"PREFERRED" },
717c478bd9Sstevel@tonic-gate 	{ IFF_TEMPORARY,	"TEMPORARY" },
727c478bd9Sstevel@tonic-gate 	{ IFF_FIXEDMTU,		"FIXEDMTU" },
73*69bb4bb4Scarlsonj 	{ IFF_VIRTUAL,		"VIRTUAL" },
74*69bb4bb4Scarlsonj 	{ IFF_DUPLICATE,	"DUPLICATE" }
757c478bd9Sstevel@tonic-gate };
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static struct	lifreq lifr;
787906a3e0Smeem /* current interface name a particular function is accessing */
797c478bd9Sstevel@tonic-gate static char	name[LIFNAMSIZ];
807c478bd9Sstevel@tonic-gate /* foreach interface saved name */
817c478bd9Sstevel@tonic-gate static char	origname[LIFNAMSIZ];
827c478bd9Sstevel@tonic-gate static char savedname[LIFNAMSIZ];	/* For addif */
837c478bd9Sstevel@tonic-gate static int	setaddr;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * Make sure the algorithm variables hold more than the sizeof an algorithm
877c478bd9Sstevel@tonic-gate  * in PF_KEY.  (For now, more than a uint8_t.)  The NO_***_?ALG indicates that
887c478bd9Sstevel@tonic-gate  * there was no algorithm requested, and in the ipsec_req that service should
897c478bd9Sstevel@tonic-gate  * be disabled.  (E.g. if ah_aalg remains NO_AH_AALG, then AH will be
907c478bd9Sstevel@tonic-gate  * disabled on that tunnel.)
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate #define	NO_AH_AALG 256
937c478bd9Sstevel@tonic-gate #define	NO_ESP_AALG 256
947c478bd9Sstevel@tonic-gate #define	NO_ESP_EALG 256
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * iface_t
987c478bd9Sstevel@tonic-gate  * used by setifether to create a list of interfaces to mark
997c478bd9Sstevel@tonic-gate  * down-up when changing the ethernet address of an interface
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate typedef struct iface {
1027c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
1037c478bd9Sstevel@tonic-gate 	struct iface *next;	/* pointer to the next list element */
1047c478bd9Sstevel@tonic-gate } iface_t;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static	iface_t	*logifs = NULL; /* list of logical interfaces */
1077c478bd9Sstevel@tonic-gate static 	iface_t	*phyif	= NULL;	/* physical interface */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate int	s;
1107c478bd9Sstevel@tonic-gate int	af = AF_INET;	/* default address family */
1117c478bd9Sstevel@tonic-gate int	debug = 0;
1127c478bd9Sstevel@tonic-gate int	all = 0;	/* setifdhcp() needs to know this */
1137c478bd9Sstevel@tonic-gate int	verbose = 0;
1147c478bd9Sstevel@tonic-gate int	v4compat = 0;	/* Compatible printing format */
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Function prototypes for command functions.
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate static int	addif(char *arg, int64_t param);
1207c478bd9Sstevel@tonic-gate static int	inetplumb(char *arg, int64_t param);
1217c478bd9Sstevel@tonic-gate static int	inetunplumb(char *arg, int64_t param);
1227c478bd9Sstevel@tonic-gate static int	removeif(char *arg, int64_t param);
1237c478bd9Sstevel@tonic-gate static int	setdebugflag(char *arg, int64_t param);
1247c478bd9Sstevel@tonic-gate static int	setifaddr(char *arg, int64_t param);
1257c478bd9Sstevel@tonic-gate static int	setifbroadaddr(char *arg, int64_t param);
1267c478bd9Sstevel@tonic-gate static int	setifdstaddr(char *arg, int64_t param);
1277c478bd9Sstevel@tonic-gate static int	setifether(char *arg, int64_t param);
1287c478bd9Sstevel@tonic-gate static int	setifflags(char *arg, int64_t param);
1297c478bd9Sstevel@tonic-gate static int	setifindex(char *arg, int64_t param);
1307c478bd9Sstevel@tonic-gate static int	setifmetric(char *arg, int64_t param);
1317c478bd9Sstevel@tonic-gate static int	setifmtu(char *arg, int64_t param);
1327c478bd9Sstevel@tonic-gate static int	setifnetmask(char *arg, int64_t param);
1337c478bd9Sstevel@tonic-gate static int	setifprefixlen(char *arg, int64_t param);
1347c478bd9Sstevel@tonic-gate static int	setifrevarp(char *arg, int64_t param);
1357c478bd9Sstevel@tonic-gate static int	setifsubnet(char *arg, int64_t param);
1367c478bd9Sstevel@tonic-gate static int	setiftdst(char *arg, int64_t param);
1377c478bd9Sstevel@tonic-gate static int	setiftoken(char *arg, int64_t param);
1387c478bd9Sstevel@tonic-gate static int	setiftsrc(char *arg, int64_t param);
1397c478bd9Sstevel@tonic-gate static int	setverboseflag(char *arg, int64_t param);
1407c478bd9Sstevel@tonic-gate static int	set_tun_ah_alg(char *arg, int64_t param);
1417c478bd9Sstevel@tonic-gate static int	set_tun_esp_auth_alg(char *arg, int64_t param);
1427c478bd9Sstevel@tonic-gate static int	set_tun_esp_encr_alg(char *arg, int64_t param);
1437c478bd9Sstevel@tonic-gate static int	modlist(char *arg, int64_t param);
1447c478bd9Sstevel@tonic-gate static int	modinsert(char *arg, int64_t param);
1457c478bd9Sstevel@tonic-gate static int	modremove(char *arg, int64_t param);
1467c478bd9Sstevel@tonic-gate static int	setifgroupname(char *arg, int64_t param);
1477c478bd9Sstevel@tonic-gate static int	configinfo(char *arg, int64_t param);
1487c478bd9Sstevel@tonic-gate static void	print_config_flags(uint64_t flags);
1497c478bd9Sstevel@tonic-gate static void	print_flags(uint64_t flags);
1507c478bd9Sstevel@tonic-gate static void	print_ifether(char *ifname);
1517c478bd9Sstevel@tonic-gate static int	set_tun_encap_limit(char *arg, int64_t param);
1527c478bd9Sstevel@tonic-gate static int	clr_tun_encap_limit(char *arg, int64_t param);
1537c478bd9Sstevel@tonic-gate static int	set_tun_hop_limit(char *arg, int64_t param);
1547c478bd9Sstevel@tonic-gate static int	setzone(char *arg, int64_t param);
15545916cd2Sjpk static int	setallzones(char *arg, int64_t param);
1567c478bd9Sstevel@tonic-gate static int	setifsrc(char *arg, int64_t param);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * Address family specific function prototypes.
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate static void	in_getaddr(char *s, struct sockaddr *saddr, int *plenp);
1627c478bd9Sstevel@tonic-gate static void	in_status(int force, uint64_t flags);
1637c478bd9Sstevel@tonic-gate static void	in_configinfo(int force, uint64_t flags);
1647c478bd9Sstevel@tonic-gate static void	in6_getaddr(char *s, struct sockaddr *saddr, int *plenp);
1657c478bd9Sstevel@tonic-gate static void	in6_status(int force, uint64_t flags);
1667c478bd9Sstevel@tonic-gate static void	in6_configinfo(int force, uint64_t flags);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Misc support functions
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate static int	devfs_entry(di_node_t node, di_minor_t minor, void *arg);
1727c478bd9Sstevel@tonic-gate static void	foreachinterface(void (*func)(), int argc, char *argv[],
1737c478bd9Sstevel@tonic-gate 		    int af, int64_t onflags, int64_t offflags,
1747c478bd9Sstevel@tonic-gate 		    int64_t lifc_flags);
1757c478bd9Sstevel@tonic-gate static void	ifconfig(int argc, char *argv[], int af, struct lifreq *lifrp);
176dd7a6f5fSkcpoon static boolean_t	in_getmask(struct sockaddr_in *saddr,
177dd7a6f5fSkcpoon 			    boolean_t addr_set);
1787c478bd9Sstevel@tonic-gate static int	in_getprefixlen(char *addr, boolean_t slash, int plen);
1797c478bd9Sstevel@tonic-gate static boolean_t	in_prefixlentomask(int prefixlen, int maxlen,
1807c478bd9Sstevel@tonic-gate 			    uchar_t *mask);
1817c478bd9Sstevel@tonic-gate static int	settaddr(char *, int (*)(icfg_handle_t,
1827c478bd9Sstevel@tonic-gate 			    const struct sockaddr *, socklen_t));
1837c478bd9Sstevel@tonic-gate static void	status(void);
1847c478bd9Sstevel@tonic-gate static void	ifstatus(const char *);
1857c478bd9Sstevel@tonic-gate static void	usage(void);
1867c478bd9Sstevel@tonic-gate static int	strioctl(int s, int cmd, char *buf, int buflen);
1877c478bd9Sstevel@tonic-gate static int	setifdhcp(const char *caller, const char *ifname,
1887c478bd9Sstevel@tonic-gate 		    int argc, char *argv[]);
1897c478bd9Sstevel@tonic-gate static int	ip_domux2fd(int *, int *, int *, int *);
1907c478bd9Sstevel@tonic-gate static int	ip_plink(int, int, int, int);
1917c478bd9Sstevel@tonic-gate static int	modop(char *arg, char op);
1927c478bd9Sstevel@tonic-gate static int	get_lun(char *);
1937c478bd9Sstevel@tonic-gate static void	selectifs(int argc, char *argv[], int af,
1947c478bd9Sstevel@tonic-gate 			struct lifreq *lifrp);
1957c478bd9Sstevel@tonic-gate static int	updownifs(iface_t *ifs, int up);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate #define	max(a, b)	((a) < (b) ? (b) : (a))
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate  * DHCP_EXIT_IF_FAILURE indicates that the operation failed, but if there
2017c478bd9Sstevel@tonic-gate  * are more interfaces to act on (i.e., ifconfig was invoked with -a), keep
2027c478bd9Sstevel@tonic-gate  * on going rather than exit with an error.
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate #define	DHCP_EXIT_IF_FAILURE	-1
2067c478bd9Sstevel@tonic-gate #define	DHCP_IPC_MAX_WAIT	15	/* max seconds to wait to start agent */
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate #define	NEXTARG		0xffffff	/* command takes an argument */
2097c478bd9Sstevel@tonic-gate #define	OPTARG		0xfffffe 	/* command takes an optional argument */
2107c478bd9Sstevel@tonic-gate #define	AF_ANY		(-1)
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /* Refer to the comments in ifconfig() on the netmask "hack" */
2137c478bd9Sstevel@tonic-gate #define	NETMASK_CMD	"netmask"
2147c478bd9Sstevel@tonic-gate struct sockaddr_storage	g_netmask;
215dd7a6f5fSkcpoon enum { G_NETMASK_NIL, G_NETMASK_PENDING, G_NETMASK_SET }
216dd7a6f5fSkcpoon     g_netmask_set = G_NETMASK_NIL;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate struct	cmd {
2197c478bd9Sstevel@tonic-gate 	char		*c_name;
2207c478bd9Sstevel@tonic-gate 	int64_t		c_parameter;	/* NEXTARG means next argv */
2217c478bd9Sstevel@tonic-gate 	int		(*c_func)(char *, int64_t);
2227c478bd9Sstevel@tonic-gate 	int		c_abortonfail;	/* don't continue parsing args */
2237c478bd9Sstevel@tonic-gate 					/* for the current interface */
2247c478bd9Sstevel@tonic-gate 	int	c_af;			/* address family restrictions */
2257c478bd9Sstevel@tonic-gate } cmds[] = {
2267c478bd9Sstevel@tonic-gate 	{ "up",		IFF_UP,		setifflags,	0,	AF_ANY },
2277c478bd9Sstevel@tonic-gate 	{ "down",	-IFF_UP,	setifflags,	0,	AF_ANY },
2287c478bd9Sstevel@tonic-gate 	{ "trailers",	-IFF_NOTRAILERS, setifflags,	0,	AF_ANY },
2297c478bd9Sstevel@tonic-gate 	{ "-trailers",	IFF_NOTRAILERS,	setifflags,	0,	AF_ANY },
2307c478bd9Sstevel@tonic-gate 	{ "arp",	-IFF_NOARP,	setifflags,	0,	AF_INET },
2317c478bd9Sstevel@tonic-gate 	{ "-arp",	IFF_NOARP,	setifflags,	0,	AF_INET },
2327c478bd9Sstevel@tonic-gate 	{ "router",	IFF_ROUTER,	setifflags,	0,	AF_ANY },
2337c478bd9Sstevel@tonic-gate 	{ "-router",	-IFF_ROUTER,	setifflags,	0,	AF_ANY },
2347c478bd9Sstevel@tonic-gate 	{ "private",	IFF_PRIVATE,	setifflags,	0,	AF_ANY },
2357c478bd9Sstevel@tonic-gate 	{ "-private",	-IFF_PRIVATE,	setifflags,	0,	AF_ANY },
2367c478bd9Sstevel@tonic-gate 	{ "xmit",	-IFF_NOXMIT,	setifflags,	0,	AF_ANY },
2377c478bd9Sstevel@tonic-gate 	{ "-xmit",	IFF_NOXMIT,	setifflags,	0,	AF_ANY },
2387c478bd9Sstevel@tonic-gate 	{ "-nud",	IFF_NONUD,	setifflags,	0,	AF_INET6 },
2397c478bd9Sstevel@tonic-gate 	{ "nud",	-IFF_NONUD,	setifflags,	0,	AF_INET6 },
2407c478bd9Sstevel@tonic-gate 	{ "anycast",	IFF_ANYCAST,	setifflags,	0,	AF_ANY },
2417c478bd9Sstevel@tonic-gate 	{ "-anycast",	-IFF_ANYCAST,	setifflags,	0,	AF_ANY },
2427c478bd9Sstevel@tonic-gate 	{ "local",	-IFF_NOLOCAL,	setifflags,	0,	AF_ANY },
2437c478bd9Sstevel@tonic-gate 	{ "-local",	IFF_NOLOCAL,	setifflags,	0,	AF_ANY },
2447c478bd9Sstevel@tonic-gate 	{ "deprecated",	IFF_DEPRECATED,	setifflags,	0,	AF_ANY },
2457c478bd9Sstevel@tonic-gate 	{ "-deprecated", -IFF_DEPRECATED, setifflags,	0,	AF_ANY },
2467c478bd9Sstevel@tonic-gate 	{ "preferred",	IFF_PREFERRED,	setifflags,	0,	AF_INET6 },
2477c478bd9Sstevel@tonic-gate 	{ "-preferred",	-IFF_PREFERRED,	setifflags,	0,	AF_INET6 },
2487c478bd9Sstevel@tonic-gate 	{ "debug",	0,		setdebugflag,	0,	AF_ANY },
2497c478bd9Sstevel@tonic-gate 	{ "verbose",	0,		setverboseflag,	0,	AF_ANY },
2507c478bd9Sstevel@tonic-gate 	{ NETMASK_CMD,	NEXTARG,	setifnetmask,	0,	AF_INET },
2517c478bd9Sstevel@tonic-gate 	{ "metric",	NEXTARG,	setifmetric,	0,	AF_ANY },
2527c478bd9Sstevel@tonic-gate 	{ "mtu",	NEXTARG,	setifmtu,	0,	AF_ANY },
2537c478bd9Sstevel@tonic-gate 	{ "index",	NEXTARG,	setifindex,	0,	AF_ANY },
2547c478bd9Sstevel@tonic-gate 	{ "broadcast",	NEXTARG,	setifbroadaddr,	0,	AF_INET },
2557c478bd9Sstevel@tonic-gate 	{ "auto-revarp", 0,		setifrevarp,	1,	AF_INET },
2567c478bd9Sstevel@tonic-gate 	{ "plumb",	0,		inetplumb,	1,	AF_ANY },
2577c478bd9Sstevel@tonic-gate 	{ "unplumb",	0,		inetunplumb,	0,	AF_ANY },
2587c478bd9Sstevel@tonic-gate 	{ "subnet",	NEXTARG,	setifsubnet,	0,	AF_ANY },
2597c478bd9Sstevel@tonic-gate 	{ "token",	NEXTARG,	setiftoken,	0,	AF_INET6 },
2607c478bd9Sstevel@tonic-gate 	{ "tsrc",	NEXTARG,	setiftsrc,	0,	AF_ANY },
2617c478bd9Sstevel@tonic-gate 	{ "tdst",	NEXTARG,	setiftdst,	0,	AF_ANY },
2627c478bd9Sstevel@tonic-gate 	{ "encr_auth_algs", NEXTARG,	set_tun_esp_auth_alg, 0, AF_ANY },
2637c478bd9Sstevel@tonic-gate 	{ "encr_algs",	NEXTARG,	set_tun_esp_encr_alg, 0, AF_ANY },
2647c478bd9Sstevel@tonic-gate 	{ "auth_algs",	NEXTARG,	set_tun_ah_alg,	0,	AF_ANY },
2657c478bd9Sstevel@tonic-gate 	{ "addif",	NEXTARG,	addif,		1,	AF_ANY },
2667c478bd9Sstevel@tonic-gate 	{ "removeif",	NEXTARG,	removeif,	1,	AF_ANY },
2677c478bd9Sstevel@tonic-gate 	{ "modlist",	0,		modlist,	1,	AF_ANY },
2687c478bd9Sstevel@tonic-gate 	{ "modinsert",	NEXTARG,	modinsert,	1,	AF_ANY },
2697c478bd9Sstevel@tonic-gate 	{ "modremove",	NEXTARG,	modremove,	1,	AF_ANY },
2707c478bd9Sstevel@tonic-gate 	{ "failover",	-IFF_NOFAILOVER, setifflags,	1,	AF_ANY },
2717c478bd9Sstevel@tonic-gate 	{ "-failover",	IFF_NOFAILOVER, setifflags,	1,	AF_ANY },
2727c478bd9Sstevel@tonic-gate 	{ "standby",	IFF_STANDBY,	setifflags,	1,	AF_ANY },
2737c478bd9Sstevel@tonic-gate 	{ "-standby",	-IFF_STANDBY,	setifflags,	1,	AF_ANY },
2747c478bd9Sstevel@tonic-gate 	{ "failed",	IFF_FAILED,	setifflags,	1,	AF_ANY },
2757c478bd9Sstevel@tonic-gate 	{ "-failed",	-IFF_FAILED,	setifflags,	1,	AF_ANY },
2767c478bd9Sstevel@tonic-gate 	{ "group",	NEXTARG,	setifgroupname,	1,	AF_ANY },
2777c478bd9Sstevel@tonic-gate 	{ "configinfo",	0,		configinfo,	1,	AF_ANY },
2787906a3e0Smeem 	{ "encaplimit",	NEXTARG,	set_tun_encap_limit,	0, AF_ANY },
2797906a3e0Smeem 	{ "-encaplimit", 0,		clr_tun_encap_limit,	0, AF_ANY },
2807906a3e0Smeem 	{ "thoplimit",	NEXTARG,	set_tun_hop_limit,	0, AF_ANY },
2817c478bd9Sstevel@tonic-gate 	{ "set",	NEXTARG,	setifaddr,	0,	AF_ANY },
2827c478bd9Sstevel@tonic-gate 	{ "destination", NEXTARG,	setifdstaddr,	0,	AF_ANY },
2837c478bd9Sstevel@tonic-gate 	{ "zone",	NEXTARG,	setzone,	0,	AF_ANY },
2847c478bd9Sstevel@tonic-gate 	{ "-zone",	0,		setzone,	0,	AF_ANY },
28545916cd2Sjpk 	{ "all-zones",	0,		setallzones,	0,	AF_ANY },
2867c478bd9Sstevel@tonic-gate 	{ "ether",	OPTARG,		setifether,	0,	AF_ANY },
2877c478bd9Sstevel@tonic-gate 	{ "usesrc",	NEXTARG,	setifsrc,	0,	AF_ANY },
2887c478bd9Sstevel@tonic-gate 	{ 0,		0,		setifaddr,	0,	AF_ANY },
2897c478bd9Sstevel@tonic-gate 	{ 0,		0,		setifdstaddr,	0,	AF_ANY },
2907c478bd9Sstevel@tonic-gate 	{ 0,		0,		0,		0,	0 },
2917c478bd9Sstevel@tonic-gate };
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate typedef struct if_config_cmd {
2957c478bd9Sstevel@tonic-gate 	uint64_t	iff_flag;
2967c478bd9Sstevel@tonic-gate 	char		*iff_name;
2977c478bd9Sstevel@tonic-gate } if_config_cmd_t;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate static if_config_cmd_t	if_config_cmd_tbl[] = {
3007c478bd9Sstevel@tonic-gate 	{ IFF_UP,		"up" },
3017c478bd9Sstevel@tonic-gate 	{ IFF_NOTRAILERS,	"-trailers" },
3027c478bd9Sstevel@tonic-gate 	{ IFF_PRIVATE,		"private" },
3037c478bd9Sstevel@tonic-gate 	{ IFF_NOXMIT,		"-xmit" },
3047c478bd9Sstevel@tonic-gate 	{ IFF_ANYCAST,		"anycast" },
3057c478bd9Sstevel@tonic-gate 	{ IFF_NOLOCAL,		"-local" },
3067c478bd9Sstevel@tonic-gate 	{ IFF_DEPRECATED,	"deprecated" },
3077c478bd9Sstevel@tonic-gate 	{ IFF_NOFAILOVER,	"-failover" },
3087c478bd9Sstevel@tonic-gate 	{ IFF_STANDBY,		"standby" },
3097c478bd9Sstevel@tonic-gate 	{ IFF_FAILED,		"failed" },
3107c478bd9Sstevel@tonic-gate 	{ IFF_PREFERRED,	"preferred" },
3117c478bd9Sstevel@tonic-gate 	{ 0,			0 },
3127c478bd9Sstevel@tonic-gate };
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate typedef struct ni {
3157c478bd9Sstevel@tonic-gate 	char		ni_name[LIFNAMSIZ];
3167c478bd9Sstevel@tonic-gate 	struct ni	*ni_next;
3177c478bd9Sstevel@tonic-gate } ni_t;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate static ni_t	*ni_list = NULL;
3207c478bd9Sstevel@tonic-gate static int	num_ni = 0;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate /* End defines and structure definitions for ifconfig -a plumb */
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /* Known address families */
3257c478bd9Sstevel@tonic-gate struct afswtch {
3267c478bd9Sstevel@tonic-gate 	char *af_name;
3277c478bd9Sstevel@tonic-gate 	short af_af;
3287c478bd9Sstevel@tonic-gate 	void (*af_status)();
3297c478bd9Sstevel@tonic-gate 	void (*af_getaddr)();
3307c478bd9Sstevel@tonic-gate 	void (*af_configinfo)();
3317c478bd9Sstevel@tonic-gate } afs[] = {
3327c478bd9Sstevel@tonic-gate 	{ "inet", AF_INET, in_status, in_getaddr, in_configinfo },
3337c478bd9Sstevel@tonic-gate 	{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_configinfo },
3347c478bd9Sstevel@tonic-gate 	{ 0, 0,	0, 0, 0 }
3357c478bd9Sstevel@tonic-gate };
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate struct afswtch *afp;	/* the address family being set or asked about */
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate int
3427c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate 	/* Include IFF_NOXMIT, IFF_TEMPORARY and all zone interfaces */
3457c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES;
3467c478bd9Sstevel@tonic-gate 	char *default_ip_str;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	if (argc < 2) {
3497c478bd9Sstevel@tonic-gate 		usage();
3507c478bd9Sstevel@tonic-gate 		exit(1);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	argc--, argv++;
3537c478bd9Sstevel@tonic-gate 	if (strlen(*argv) > sizeof (name) - 1) {
3547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: interface name too long\n", *argv);
3557c478bd9Sstevel@tonic-gate 		exit(1);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	(void) strncpy(name, *argv, sizeof (name));
3587c478bd9Sstevel@tonic-gate 	name[sizeof (name) - 1] = '\0';
3597c478bd9Sstevel@tonic-gate 	(void) strncpy(origname, name, sizeof (origname));	/* For addif */
3607c478bd9Sstevel@tonic-gate 	default_ip_str = NULL;
3617c478bd9Sstevel@tonic-gate 	v4compat = get_compat_flag(&default_ip_str);
3627c478bd9Sstevel@tonic-gate 	if (v4compat == DEFAULT_PROT_BAD_VALUE) {
3637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3647c478bd9Sstevel@tonic-gate 		    "ifconfig: %s: Bad value for %s in %s\n", default_ip_str,
3657c478bd9Sstevel@tonic-gate 		    DEFAULT_IP, INET_DEFAULT_FILE);
3667c478bd9Sstevel@tonic-gate 		free(default_ip_str);
3677c478bd9Sstevel@tonic-gate 		exit(2);
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 	free(default_ip_str);
3707c478bd9Sstevel@tonic-gate 	argc--, argv++;
3717c478bd9Sstevel@tonic-gate 	if (argc > 0) {
3727c478bd9Sstevel@tonic-gate 		struct afswtch *myafp;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 		for (myafp = afp = afs; myafp->af_name; myafp++) {
3757c478bd9Sstevel@tonic-gate 			if (strcmp(myafp->af_name, *argv) == 0) {
3767c478bd9Sstevel@tonic-gate 				afp = myafp; argc--; argv++;
3777c478bd9Sstevel@tonic-gate 				break;
3787c478bd9Sstevel@tonic-gate 			}
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 		af = lifr.lifr_addr.ss_family = afp->af_af;
3817c478bd9Sstevel@tonic-gate 		if (af == AF_INET6) {
3827c478bd9Sstevel@tonic-gate 			v4compat = 0;
3837c478bd9Sstevel@tonic-gate 		}
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	s = socket(SOCKET_AF(af), SOCK_DGRAM, 0);
3877c478bd9Sstevel@tonic-gate 	if (s < 0) {
3887c478bd9Sstevel@tonic-gate 		Perror0_exit("socket");
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/*
3927c478bd9Sstevel@tonic-gate 	 * Special interface names is any combination of these flags.
3937c478bd9Sstevel@tonic-gate 	 * Note that due to the ifconfig syntax they have to be combined
3947c478bd9Sstevel@tonic-gate 	 * as a single '-' option.
3957c478bd9Sstevel@tonic-gate 	 *	-a	All interfaces
3967c478bd9Sstevel@tonic-gate 	 *	-u	"up" interfaces
3977c478bd9Sstevel@tonic-gate 	 *	-d	"down" interfaces
3987c478bd9Sstevel@tonic-gate 	 *	-D	Interfaces not controlled by DHCP
3997c478bd9Sstevel@tonic-gate 	 *	-4	IPv4 interfaces
4007c478bd9Sstevel@tonic-gate 	 *	-6	IPv6 interfaces
4017c478bd9Sstevel@tonic-gate 	 *	-X	Turn on debug (not documented)
4027c478bd9Sstevel@tonic-gate 	 *	-v	Turn on verbose
4037c478bd9Sstevel@tonic-gate 	 *	-Z	Only interfaces in caller's zone
4047c478bd9Sstevel@tonic-gate 	 */
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	if (name[0] == '-') {
4077c478bd9Sstevel@tonic-gate 		/* One or more options */
4087c478bd9Sstevel@tonic-gate 		int64_t onflags = 0;
4097c478bd9Sstevel@tonic-gate 		int64_t offflags = 0;
4107c478bd9Sstevel@tonic-gate 		int c;
4117c478bd9Sstevel@tonic-gate 		char *av[2] = { "ifconfig", name };
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 		while ((c = getopt(2, av, "audDXZ46v")) != -1) {
4147c478bd9Sstevel@tonic-gate 			switch ((char)c) {
4157c478bd9Sstevel@tonic-gate 			case 'a':
4167c478bd9Sstevel@tonic-gate 				all = 1;
4177c478bd9Sstevel@tonic-gate 				break;
4187c478bd9Sstevel@tonic-gate 			case 'u':
4197c478bd9Sstevel@tonic-gate 				onflags |= IFF_UP;
4207c478bd9Sstevel@tonic-gate 				break;
4217c478bd9Sstevel@tonic-gate 			case 'd':
4227c478bd9Sstevel@tonic-gate 				offflags |= IFF_UP;
4237c478bd9Sstevel@tonic-gate 				break;
4247c478bd9Sstevel@tonic-gate 			case 'D':
4257c478bd9Sstevel@tonic-gate 				offflags |= IFF_DHCPRUNNING;
4267c478bd9Sstevel@tonic-gate 				break;
4277c478bd9Sstevel@tonic-gate 			case 'X':
4287c478bd9Sstevel@tonic-gate 				debug += 3;
4297c478bd9Sstevel@tonic-gate 				break;
4307c478bd9Sstevel@tonic-gate 			case 'Z':
4317c478bd9Sstevel@tonic-gate 				lifc_flags &= ~LIFC_ALLZONES;
4327c478bd9Sstevel@tonic-gate 				break;
4337c478bd9Sstevel@tonic-gate 			case '4':
4347c478bd9Sstevel@tonic-gate 				/*
4357c478bd9Sstevel@tonic-gate 				 * -4 is not a compatable flag, therefore
4367c478bd9Sstevel@tonic-gate 				 * we assume they want v4compat turned off
4377c478bd9Sstevel@tonic-gate 				 */
4387c478bd9Sstevel@tonic-gate 				v4compat = 0;
4397c478bd9Sstevel@tonic-gate 				onflags |= IFF_IPV4;
4407c478bd9Sstevel@tonic-gate 				break;
4417c478bd9Sstevel@tonic-gate 			case '6':
4427c478bd9Sstevel@tonic-gate 				/*
4437c478bd9Sstevel@tonic-gate 				 * If they want IPv6, well then we'll assume
4447c478bd9Sstevel@tonic-gate 				 * they don't want IPv4 compat
4457c478bd9Sstevel@tonic-gate 				 */
4467c478bd9Sstevel@tonic-gate 				v4compat = 0;
4477c478bd9Sstevel@tonic-gate 				onflags |= IFF_IPV6;
4487c478bd9Sstevel@tonic-gate 				break;
4497c478bd9Sstevel@tonic-gate 			case 'v':
4507c478bd9Sstevel@tonic-gate 				verbose = 1;
4517c478bd9Sstevel@tonic-gate 				break;
4527c478bd9Sstevel@tonic-gate 			case '?':
4537c478bd9Sstevel@tonic-gate 				usage();
4547c478bd9Sstevel@tonic-gate 				exit(1);
4557c478bd9Sstevel@tonic-gate 			}
4567c478bd9Sstevel@tonic-gate 		}
4577c478bd9Sstevel@tonic-gate 		if (!all) {
4587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4597c478bd9Sstevel@tonic-gate 			    "ifconfig: %s: no such interface\n",
4607c478bd9Sstevel@tonic-gate 			    name);
4617c478bd9Sstevel@tonic-gate 			exit(1);
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 		foreachinterface(ifconfig, argc, argv, af, onflags, offflags,
4647c478bd9Sstevel@tonic-gate 		    lifc_flags);
4657c478bd9Sstevel@tonic-gate 	} else {
4667c478bd9Sstevel@tonic-gate 		ifconfig(argc, argv, af, (struct lifreq *)NULL);
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 	return (0);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate  * For each interface, call (*func)(argc, argv, af, lifrp).
4737c478bd9Sstevel@tonic-gate  * Only call function if onflags and offflags are set or clear, respectively,
4747c478bd9Sstevel@tonic-gate  * in the interfaces flags field.
4757c478bd9Sstevel@tonic-gate  */
4767c478bd9Sstevel@tonic-gate static void
4777c478bd9Sstevel@tonic-gate foreachinterface(void (*func)(), int argc, char *argv[], int af,
4787c478bd9Sstevel@tonic-gate     int64_t onflags, int64_t offflags, int64_t lifc_flags)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	int n;
4817c478bd9Sstevel@tonic-gate 	char *buf;
4827c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
4837c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
4847c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp;
4857c478bd9Sstevel@tonic-gate 	struct lifreq lifrl;	/* Local lifreq struct */
4867c478bd9Sstevel@tonic-gate 	int numifs;
4877c478bd9Sstevel@tonic-gate 	unsigned bufsize;
4887c478bd9Sstevel@tonic-gate 	ni_t *nip;
4897c478bd9Sstevel@tonic-gate 	int plumball = 0;
4907c478bd9Sstevel@tonic-gate 	int save_af = af;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	/*
4937c478bd9Sstevel@tonic-gate 	 * Special case:
4947c478bd9Sstevel@tonic-gate 	 * ifconfig -a plumb should find all network interfaces
4957c478bd9Sstevel@tonic-gate 	 * in the machine by traversing the devinfo tree.
4967c478bd9Sstevel@tonic-gate 	 * Also, there is no need to  SIOCGLIF* ioctls, since
4977c478bd9Sstevel@tonic-gate 	 * those interfaces have already been plumbed
4987c478bd9Sstevel@tonic-gate 	 */
4997c478bd9Sstevel@tonic-gate 	if (argc > 0 && (strcmp(*argv, "plumb") == 0)) {
5007c478bd9Sstevel@tonic-gate 		/*
5017c478bd9Sstevel@tonic-gate 		 * Look through the kernel's devinfo tree for
5027c478bd9Sstevel@tonic-gate 		 * network devices
5037c478bd9Sstevel@tonic-gate 		 */
5047c478bd9Sstevel@tonic-gate 		di_node_t root;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		/*
5077c478bd9Sstevel@tonic-gate 		 * DINFOCACHE is equivalent to DINFOSUBTREE | DINFOMINOR |
5087c478bd9Sstevel@tonic-gate 		 * DINFOPROP | DINFOFORCE.
5097c478bd9Sstevel@tonic-gate 		 */
5107c478bd9Sstevel@tonic-gate 		if ((root = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
5117c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: di_init failed;"
5127c478bd9Sstevel@tonic-gate 			    " check the devinfo driver.\n");
5137c478bd9Sstevel@tonic-gate 			exit(1);
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 		(void) di_walk_minor(root, DDI_NT_NET, DI_CHECK_ALIAS, NULL,
5177c478bd9Sstevel@tonic-gate 		    devfs_entry);
5187c478bd9Sstevel@tonic-gate 		di_fini(root);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 		/*
5217c478bd9Sstevel@tonic-gate 		 * Now, translate the linked list into
5227c478bd9Sstevel@tonic-gate 		 * a struct lifreq buffer
5237c478bd9Sstevel@tonic-gate 		 */
5247c478bd9Sstevel@tonic-gate 		bufsize = num_ni * sizeof (struct lifreq);
5257c478bd9Sstevel@tonic-gate 		if ((buf = malloc(bufsize)) == NULL)
5267c478bd9Sstevel@tonic-gate 			Perror0_exit("foreachinterface: malloc failed");
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 		lifc.lifc_family = AF_UNSPEC;
5297c478bd9Sstevel@tonic-gate 		lifc.lifc_flags = lifc_flags;
5307c478bd9Sstevel@tonic-gate 		lifc.lifc_len = bufsize;
5317c478bd9Sstevel@tonic-gate 		lifc.lifc_buf = buf;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 		for (n = 0, lifrp = lifc.lifc_req; n < num_ni; n++, lifrp++) {
5347c478bd9Sstevel@tonic-gate 			nip = ni_list;
5357c478bd9Sstevel@tonic-gate 			(void) strncpy(lifrp->lifr_name, nip->ni_name,
5367c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_name));
5377c478bd9Sstevel@tonic-gate 			ni_list = nip->ni_next;
5387c478bd9Sstevel@tonic-gate 			free(nip);
5397c478bd9Sstevel@tonic-gate 		}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		plumball = 1;
5427c478bd9Sstevel@tonic-gate 	} else {
5437c478bd9Sstevel@tonic-gate 		lifn.lifn_family = AF_UNSPEC;
5447c478bd9Sstevel@tonic-gate 		lifn.lifn_flags = lifc_flags;
5457c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
5467c478bd9Sstevel@tonic-gate 			Perror0_exit("Could not determine number"
5477c478bd9Sstevel@tonic-gate 			    " of interfaces");
5487c478bd9Sstevel@tonic-gate 		}
5497c478bd9Sstevel@tonic-gate 		numifs = lifn.lifn_count;
5507c478bd9Sstevel@tonic-gate 		if (debug)
5517c478bd9Sstevel@tonic-gate 			(void) printf("ifconfig: %d interfaces\n",  numifs);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 		bufsize = numifs * sizeof (struct lifreq);
5547c478bd9Sstevel@tonic-gate 		if ((buf = malloc(bufsize)) == NULL) {
5557c478bd9Sstevel@tonic-gate 			Perror0("out of memory\n");
5567c478bd9Sstevel@tonic-gate 			(void) close(s);
5577c478bd9Sstevel@tonic-gate 			return;
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 		lifc.lifc_family = AF_UNSPEC;
5617c478bd9Sstevel@tonic-gate 		lifc.lifc_flags = lifc_flags;
5627c478bd9Sstevel@tonic-gate 		lifc.lifc_len = bufsize;
5637c478bd9Sstevel@tonic-gate 		lifc.lifc_buf = buf;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
5667c478bd9Sstevel@tonic-gate 			Perror0("SIOCGLIFCONF");
5677c478bd9Sstevel@tonic-gate 			(void) close(s);
5687c478bd9Sstevel@tonic-gate 			free(buf);
5697c478bd9Sstevel@tonic-gate 			return;
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
5747c478bd9Sstevel@tonic-gate 	for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) {
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 		if (!plumball) {
5777c478bd9Sstevel@tonic-gate 			/*
5787c478bd9Sstevel@tonic-gate 			 * We must close and recreate the socket each time
5797c478bd9Sstevel@tonic-gate 			 * since we don't know what type of socket it is now
5807c478bd9Sstevel@tonic-gate 			 * (each status function may change it).
5817c478bd9Sstevel@tonic-gate 			 */
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 			(void) close(s);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 			af = lifrp->lifr_addr.ss_family;
5867c478bd9Sstevel@tonic-gate 			s = socket(SOCKET_AF(af), SOCK_DGRAM, 0);
5877c478bd9Sstevel@tonic-gate 			if (s == -1) {
5887c478bd9Sstevel@tonic-gate 				/*
5897c478bd9Sstevel@tonic-gate 				 * Perror0() assumes the name to be in the
5907c478bd9Sstevel@tonic-gate 				 * globally defined lifreq structure.
5917c478bd9Sstevel@tonic-gate 				 */
5927c478bd9Sstevel@tonic-gate 				(void) strncpy(lifr.lifr_name,
5937c478bd9Sstevel@tonic-gate 				    lifrp->lifr_name, sizeof (lifr.lifr_name));
5947c478bd9Sstevel@tonic-gate 				Perror0_exit("socket");
5957c478bd9Sstevel@tonic-gate 			}
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 		/*
5997c478bd9Sstevel@tonic-gate 		 * Only service interfaces that match the on and off
6007c478bd9Sstevel@tonic-gate 		 * flags masks.
6017c478bd9Sstevel@tonic-gate 		 */
6027c478bd9Sstevel@tonic-gate 		if (onflags || offflags) {
6037c478bd9Sstevel@tonic-gate 			(void) memset(&lifrl, 0, sizeof (lifrl));
6047c478bd9Sstevel@tonic-gate 			(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
6057c478bd9Sstevel@tonic-gate 				sizeof (lifrl.lifr_name));
6067c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifrl) < 0) {
6077c478bd9Sstevel@tonic-gate 				/*
6087c478bd9Sstevel@tonic-gate 				 * Perror0() assumes the name to be in the
6097c478bd9Sstevel@tonic-gate 				 * globally defined lifreq structure.
6107c478bd9Sstevel@tonic-gate 				 */
6117c478bd9Sstevel@tonic-gate 				(void) strncpy(lifr.lifr_name,
6127c478bd9Sstevel@tonic-gate 				    lifrp->lifr_name, sizeof (lifr.lifr_name));
6137c478bd9Sstevel@tonic-gate 				Perror0_exit("foreachinterface: SIOCGLIFFLAGS");
6147c478bd9Sstevel@tonic-gate 			}
6157c478bd9Sstevel@tonic-gate 			if ((lifrl.lifr_flags & onflags) != onflags)
6167c478bd9Sstevel@tonic-gate 				continue;
6177c478bd9Sstevel@tonic-gate 			if ((~lifrl.lifr_flags & offflags) != offflags)
6187c478bd9Sstevel@tonic-gate 				continue;
6197c478bd9Sstevel@tonic-gate 		}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 		if (!plumball) {
6227c478bd9Sstevel@tonic-gate 			(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
6237c478bd9Sstevel@tonic-gate 			    sizeof (lifrl.lifr_name));
6247c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifrl) < 0) {
6257c478bd9Sstevel@tonic-gate 				/*
6267c478bd9Sstevel@tonic-gate 				 * Perror0() assumes the name to be in the
6277c478bd9Sstevel@tonic-gate 				 * globally defined lifreq structure.
6287c478bd9Sstevel@tonic-gate 				 */
6297c478bd9Sstevel@tonic-gate 				(void) strncpy(lifr.lifr_name,
6307c478bd9Sstevel@tonic-gate 				    lifrp->lifr_name, sizeof (lifr.lifr_name));
6317c478bd9Sstevel@tonic-gate 				Perror0("foreachinterface: SIOCGLIFADDR");
6327c478bd9Sstevel@tonic-gate 				continue;
6337c478bd9Sstevel@tonic-gate 			}
6347c478bd9Sstevel@tonic-gate 			if (lifrl.lifr_addr.ss_family != af) {
6357c478bd9Sstevel@tonic-gate 				/* Switch address family */
6367c478bd9Sstevel@tonic-gate 				af = lifrl.lifr_addr.ss_family;
6377c478bd9Sstevel@tonic-gate 				(void) close(s);
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 				s = socket(SOCKET_AF(af), SOCK_DGRAM, 0);
6407c478bd9Sstevel@tonic-gate 				if (s == -1) {
6417c478bd9Sstevel@tonic-gate 					/*
6427c478bd9Sstevel@tonic-gate 					 * Perror0() assumes the name to be in
6437c478bd9Sstevel@tonic-gate 					 * the globally defined lifreq
6447c478bd9Sstevel@tonic-gate 					 * structure.
6457c478bd9Sstevel@tonic-gate 					 */
6467c478bd9Sstevel@tonic-gate 					(void) strncpy(lifr.lifr_name,
6477c478bd9Sstevel@tonic-gate 					    lifrp->lifr_name,
6487c478bd9Sstevel@tonic-gate 					    sizeof (lifr.lifr_name));
6497c478bd9Sstevel@tonic-gate 					Perror0_exit("socket");
6507c478bd9Sstevel@tonic-gate 				}
6517c478bd9Sstevel@tonic-gate 			}
6527c478bd9Sstevel@tonic-gate 		}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		/*
6557c478bd9Sstevel@tonic-gate 		 * Reset global state
6567c478bd9Sstevel@tonic-gate 		 * setaddr: Used by parser to tear apart source and dest
6577c478bd9Sstevel@tonic-gate 		 * name and origname contain the name of the 'current'
6587c478bd9Sstevel@tonic-gate 		 * interface.
6597c478bd9Sstevel@tonic-gate 		 */
6607c478bd9Sstevel@tonic-gate 		setaddr = 0;
6617c478bd9Sstevel@tonic-gate 		(void) strncpy(name, lifrp->lifr_name, sizeof (name));
6627c478bd9Sstevel@tonic-gate 		(void) strncpy(origname, name, sizeof (origname));
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 		(*func)(argc, argv, save_af, lifrp);
6657c478bd9Sstevel@tonic-gate 		/* the func could have overwritten origname, so restore */
6667c478bd9Sstevel@tonic-gate 		(void) strncpy(name, origname, sizeof (name));
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate 	free(buf);
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate static void
6727c478bd9Sstevel@tonic-gate tun_reality_check(void)
6737c478bd9Sstevel@tonic-gate {
6747c478bd9Sstevel@tonic-gate 	struct iftun_req treq;
6757c478bd9Sstevel@tonic-gate 	ipsec_req_t *ipsr;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	(void) strncpy(treq.ifta_lifr_name, name, sizeof (treq.ifta_lifr_name));
6787c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
6797c478bd9Sstevel@tonic-gate 		/* Return, we don't need to check. */
6807c478bd9Sstevel@tonic-gate 		return;
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGTUNPARAM, (caddr_t)&treq) < 0 ||
6837c478bd9Sstevel@tonic-gate 	    (treq.ifta_flags & IFTUN_SECURITY) == 0) {
6847c478bd9Sstevel@tonic-gate 		/*
6857c478bd9Sstevel@tonic-gate 		 * Either not a tunnel (the SIOCGTUNPARAM fails on
6867c478bd9Sstevel@tonic-gate 		 * non-tunnels), or the security flag is not set.  Either
6877c478bd9Sstevel@tonic-gate 		 * way, return.
6887c478bd9Sstevel@tonic-gate 		 */
6897c478bd9Sstevel@tonic-gate 		return;
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	ipsr = (ipsec_req_t *)&treq.ifta_secinfo;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	if (ipsr->ipsr_esp_req != 0 &&
6957c478bd9Sstevel@tonic-gate 	    ipsr->ipsr_esp_auth_alg == SADB_AALG_NONE &&
6967c478bd9Sstevel@tonic-gate 	    ipsr->ipsr_ah_req == 0)
6977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: WARNING - tunnel with "
6987c478bd9Sstevel@tonic-gate 		    "only ESP and potentially no authentication.\n");
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate /*
7027c478bd9Sstevel@tonic-gate  * for the specified interface call (*func)(argc, argv, af, lifrp).
7037c478bd9Sstevel@tonic-gate  */
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate static void
7067c478bd9Sstevel@tonic-gate ifconfig(int argc, char *argv[], int af, struct lifreq *lifrp)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	static boolean_t scan_netmask = _B_FALSE;
7097c478bd9Sstevel@tonic-gate 	int ret;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	if (argc == 0) {
7127c478bd9Sstevel@tonic-gate 		status();
7137c478bd9Sstevel@tonic-gate 		return;
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	if (strcmp(*argv, "auto-dhcp") == 0 || strcmp(*argv, "dhcp") == 0) {
7177c478bd9Sstevel@tonic-gate 		if (af == AF_INET) {
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 			/*
7207c478bd9Sstevel@tonic-gate 			 * some errors are ignored in the case where
7217c478bd9Sstevel@tonic-gate 			 * more than one interface is being operated on.
7227c478bd9Sstevel@tonic-gate 			 */
7237c478bd9Sstevel@tonic-gate 			ret = setifdhcp("ifconfig", name, argc, argv);
7247c478bd9Sstevel@tonic-gate 			if (ret == DHCP_EXIT_IF_FAILURE) {
7257c478bd9Sstevel@tonic-gate 				if (!all)
7267c478bd9Sstevel@tonic-gate 					exit(DHCP_EXIT_FAILURE);
7277c478bd9Sstevel@tonic-gate 			} else if (ret != DHCP_EXIT_SUCCESS)
7287c478bd9Sstevel@tonic-gate 				exit(ret);
7297c478bd9Sstevel@tonic-gate 		} else
7307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: dhcp not supported "
7317c478bd9Sstevel@tonic-gate 			    "for inet6\n");
7327c478bd9Sstevel@tonic-gate 		return;
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	/*
7367c478bd9Sstevel@tonic-gate 	 * The following is a "hack" to get around the existing interface
7377c478bd9Sstevel@tonic-gate 	 * setting mechanism.  Currently, each interface attribute,
7387c478bd9Sstevel@tonic-gate 	 * such as address, netmask, broadcast, ... is set separately.  But
7397c478bd9Sstevel@tonic-gate 	 * sometimes two or more attributes must be set together.  For
7407c478bd9Sstevel@tonic-gate 	 * example, setting an address without a netmask does not make sense.
7417c478bd9Sstevel@tonic-gate 	 * Yet they can be set separately for IPv4 address using the current
7427c478bd9Sstevel@tonic-gate 	 * ifconfig(1M) syntax.  The kernel then "infers" the correct netmask
7437c478bd9Sstevel@tonic-gate 	 * using the deprecated "IP address classes."  This is simply not
7447c478bd9Sstevel@tonic-gate 	 * correct.
7457c478bd9Sstevel@tonic-gate 	 *
7467c478bd9Sstevel@tonic-gate 	 * The "hack" below is to go thru the whole command list looking for
7477c478bd9Sstevel@tonic-gate 	 * the netmask command first.  Then use this netmask to set the
7487c478bd9Sstevel@tonic-gate 	 * address.  This does not provide an extensible way to accommodate
7497c478bd9Sstevel@tonic-gate 	 * future need for setting more than one attributes together.
7507c478bd9Sstevel@tonic-gate 	 *
751dd7a6f5fSkcpoon 	 * Note that if the "netmask" command argument is a "+", we need
752dd7a6f5fSkcpoon 	 * to save this info and do the query after we know the address to
753dd7a6f5fSkcpoon 	 * be set.  The reason is that if "addif" is used, the working
754dd7a6f5fSkcpoon 	 * interface name will be changed later when the logical interface
755dd7a6f5fSkcpoon 	 * is created.  In in_getmask(), if an address is not provided,
756dd7a6f5fSkcpoon 	 * it will use the working interface's address to do the query.
757dd7a6f5fSkcpoon 	 * It will be wrong now as we don't know the logical interface's name.
758dd7a6f5fSkcpoon 	 *
7597c478bd9Sstevel@tonic-gate 	 * ifconfig(1M) is too overloaded and the code is so convoluted
7607c478bd9Sstevel@tonic-gate 	 * that it is "safer" not to re-architect the code to fix the above
7617c478bd9Sstevel@tonic-gate 	 * issue, hence this "hack."  We may be better off to have a new
7627c478bd9Sstevel@tonic-gate 	 * command with better syntax for configuring network interface
7637c478bd9Sstevel@tonic-gate 	 * parameters...
7647c478bd9Sstevel@tonic-gate 	 */
7657c478bd9Sstevel@tonic-gate 	if (!scan_netmask && afp->af_af == AF_INET) {
7667c478bd9Sstevel@tonic-gate 		int	largc;
7677c478bd9Sstevel@tonic-gate 		char	**largv;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 		/* Only go thru the command list once to find the netmask. */
7707c478bd9Sstevel@tonic-gate 		scan_netmask = _B_TRUE;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 		/*
7737c478bd9Sstevel@tonic-gate 		 * Currently, if multiple netmask commands are specified, the
7747c478bd9Sstevel@tonic-gate 		 * last one will be used as the final netmask.  So we need
7757c478bd9Sstevel@tonic-gate 		 * to scan the whole list to preserve this behavior.
7767c478bd9Sstevel@tonic-gate 		 */
7777c478bd9Sstevel@tonic-gate 		for (largc = argc, largv = argv; largc > 0; largc--, largv++) {
7787c478bd9Sstevel@tonic-gate 			if (strcmp(*largv, NETMASK_CMD) == 0) {
7797c478bd9Sstevel@tonic-gate 				if (--largc == 0)
7807c478bd9Sstevel@tonic-gate 					break;
7817c478bd9Sstevel@tonic-gate 				largv++;
7827c478bd9Sstevel@tonic-gate 				if (strcmp(*largv, "+") == 0) {
783dd7a6f5fSkcpoon 					g_netmask_set = G_NETMASK_PENDING;
7847c478bd9Sstevel@tonic-gate 				} else {
7857c478bd9Sstevel@tonic-gate 					in_getaddr(*largv, (struct sockaddr *)
7867c478bd9Sstevel@tonic-gate 					    &g_netmask, NULL);
787dd7a6f5fSkcpoon 					g_netmask_set = G_NETMASK_SET;
7887c478bd9Sstevel@tonic-gate 				}
7897c478bd9Sstevel@tonic-gate 				/* Continue the scan. */
7907c478bd9Sstevel@tonic-gate 			}
7917c478bd9Sstevel@tonic-gate 		}
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	while (argc > 0) {
7957c478bd9Sstevel@tonic-gate 		struct cmd *p;
7967c478bd9Sstevel@tonic-gate 		boolean_t found_cmd;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 		if (debug)
7997c478bd9Sstevel@tonic-gate 			(void) printf("ifconfig: argv %s\n", *argv);
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 		found_cmd = _B_FALSE;
8027c478bd9Sstevel@tonic-gate 		for (p = cmds; p->c_func; p++) {
8037c478bd9Sstevel@tonic-gate 			if (p->c_name) {
8047c478bd9Sstevel@tonic-gate 				if (strcmp(*argv, p->c_name) == 0) {
8057c478bd9Sstevel@tonic-gate 					/*
8067c478bd9Sstevel@tonic-gate 					 * indicate that the command was
8077c478bd9Sstevel@tonic-gate 					 * found and check to see if
8087c478bd9Sstevel@tonic-gate 					 * the address family is valid
8097c478bd9Sstevel@tonic-gate 					 */
8107c478bd9Sstevel@tonic-gate 					found_cmd = _B_TRUE;
8117c478bd9Sstevel@tonic-gate 					if (p->c_af == AF_ANY ||
8127c478bd9Sstevel@tonic-gate 					    af == p->c_af)
8137c478bd9Sstevel@tonic-gate 						break;
8147c478bd9Sstevel@tonic-gate 				}
8157c478bd9Sstevel@tonic-gate 			} else {
8167c478bd9Sstevel@tonic-gate 				if (p->c_af == AF_ANY ||
8177c478bd9Sstevel@tonic-gate 				    af == p->c_af)
8187c478bd9Sstevel@tonic-gate 					break;
8197c478bd9Sstevel@tonic-gate 			}
8207c478bd9Sstevel@tonic-gate 		}
8217c478bd9Sstevel@tonic-gate 		/*
8227c478bd9Sstevel@tonic-gate 		 * If we found the keyword, but the address family
8237c478bd9Sstevel@tonic-gate 		 * did not match spit out an error
8247c478bd9Sstevel@tonic-gate 		 */
8257c478bd9Sstevel@tonic-gate 		if (found_cmd && p->c_name == 0) {
8267c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: Operation %s not"
8277c478bd9Sstevel@tonic-gate 			    " supported for %s\n", *argv, afp->af_name);
8287c478bd9Sstevel@tonic-gate 			exit(1);
8297c478bd9Sstevel@tonic-gate 		}
8307c478bd9Sstevel@tonic-gate 		/*
8317c478bd9Sstevel@tonic-gate 		 * else (no keyword found), we assume it's an address
8327c478bd9Sstevel@tonic-gate 		 * of some sort
8337c478bd9Sstevel@tonic-gate 		 */
8347c478bd9Sstevel@tonic-gate 		if (p->c_name == 0 && setaddr)
8357c478bd9Sstevel@tonic-gate 			p++;	/* got src, do dst */
8367c478bd9Sstevel@tonic-gate 		if (p->c_func) {
8377c478bd9Sstevel@tonic-gate 			if (p->c_af == AF_INET6) {
8387c478bd9Sstevel@tonic-gate 				v4compat = 0;
8397c478bd9Sstevel@tonic-gate 			}
8407c478bd9Sstevel@tonic-gate 			if (p->c_parameter == NEXTARG ||
8417c478bd9Sstevel@tonic-gate 			    p->c_parameter == OPTARG) {
8427c478bd9Sstevel@tonic-gate 				argc--, argv++;
8437c478bd9Sstevel@tonic-gate 				if (argc == 0 && p->c_parameter == NEXTARG) {
8447c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
8457c478bd9Sstevel@tonic-gate 					    "ifconfig: no argument for %s\n",
8467c478bd9Sstevel@tonic-gate 					    p->c_name);
8477c478bd9Sstevel@tonic-gate 					exit(1);
8487c478bd9Sstevel@tonic-gate 				}
8497c478bd9Sstevel@tonic-gate 			}
8507c478bd9Sstevel@tonic-gate 			/*
8517c478bd9Sstevel@tonic-gate 			 *	Call the function if:
8527c478bd9Sstevel@tonic-gate 			 *
8537c478bd9Sstevel@tonic-gate 			 *		there's no address family
8547c478bd9Sstevel@tonic-gate 			 *		restriction
8557c478bd9Sstevel@tonic-gate 			 *	OR
8567c478bd9Sstevel@tonic-gate 			 *		we don't know the address yet
8577c478bd9Sstevel@tonic-gate 			 *		(because we were called from
8587c478bd9Sstevel@tonic-gate 			 *		main)
8597c478bd9Sstevel@tonic-gate 			 *	OR
8607c478bd9Sstevel@tonic-gate 			 *		there is a restriction AND
8617c478bd9Sstevel@tonic-gate 			 *		the address families match
8627c478bd9Sstevel@tonic-gate 			 */
8637c478bd9Sstevel@tonic-gate 			if ((p->c_af == AF_ANY)	||
8647c478bd9Sstevel@tonic-gate 			    (lifrp == (struct lifreq *)NULL) ||
8657c478bd9Sstevel@tonic-gate 			    (lifrp->lifr_addr.ss_family == p->c_af)) {
8667c478bd9Sstevel@tonic-gate 				ret = (*p->c_func)(*argv, p->c_parameter);
8677c478bd9Sstevel@tonic-gate 				/*
8687c478bd9Sstevel@tonic-gate 				 *	If c_func failed and we should
8697c478bd9Sstevel@tonic-gate 				 *	abort processing for this
8707c478bd9Sstevel@tonic-gate 				 *	interface on failure, return
8717c478bd9Sstevel@tonic-gate 				 *	now rather than going on to
8727c478bd9Sstevel@tonic-gate 				 *	process other commands for
8737c478bd9Sstevel@tonic-gate 				 *	the same interface.
8747c478bd9Sstevel@tonic-gate 				 */
8757c478bd9Sstevel@tonic-gate 				if (ret != 0 && p->c_abortonfail)
8767c478bd9Sstevel@tonic-gate 					return;
8777c478bd9Sstevel@tonic-gate 			}
8787c478bd9Sstevel@tonic-gate 		}
8797c478bd9Sstevel@tonic-gate 		argc--, argv++;
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	/* Check to see if there's a security hole in the tunnel setup. */
8837c478bd9Sstevel@tonic-gate 	tun_reality_check();
8847c478bd9Sstevel@tonic-gate }
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate /* ARGSUSED */
8877c478bd9Sstevel@tonic-gate static int
8887c478bd9Sstevel@tonic-gate setdebugflag(char *val, int64_t arg)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	debug++;
8917c478bd9Sstevel@tonic-gate 	return (0);
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate /* ARGSUSED */
8957c478bd9Sstevel@tonic-gate static int
8967c478bd9Sstevel@tonic-gate setverboseflag(char *val, int64_t arg)
8977c478bd9Sstevel@tonic-gate {
8987c478bd9Sstevel@tonic-gate 	verbose++;
8997c478bd9Sstevel@tonic-gate 	return (0);
9007c478bd9Sstevel@tonic-gate }
9017c478bd9Sstevel@tonic-gate 
902dd7a6f5fSkcpoon /*
903dd7a6f5fSkcpoon  * This function fills in the given lifreq's lifr_addr field based on
904dd7a6f5fSkcpoon  * g_netmask_set.
905dd7a6f5fSkcpoon  */
906dd7a6f5fSkcpoon static void
907dd7a6f5fSkcpoon set_mask_lifreq(struct lifreq *lifr, struct sockaddr_storage *addr,
908dd7a6f5fSkcpoon     struct sockaddr_storage *mask)
909dd7a6f5fSkcpoon {
910dd7a6f5fSkcpoon 	assert(addr != NULL);
911dd7a6f5fSkcpoon 	assert(mask != NULL);
912dd7a6f5fSkcpoon 
913dd7a6f5fSkcpoon 	switch (g_netmask_set) {
914dd7a6f5fSkcpoon 	case G_NETMASK_SET:
915dd7a6f5fSkcpoon 		lifr->lifr_addr = g_netmask;
916dd7a6f5fSkcpoon 		break;
917dd7a6f5fSkcpoon 
918dd7a6f5fSkcpoon 	case G_NETMASK_PENDING:
919dd7a6f5fSkcpoon 		/*
920dd7a6f5fSkcpoon 		 * "+" is used as the argument to "netmask" command.  Query
921dd7a6f5fSkcpoon 		 * the database on the correct netmask based on the address to
922dd7a6f5fSkcpoon 		 * be set.
923dd7a6f5fSkcpoon 		 */
924dd7a6f5fSkcpoon 		assert(afp->af_af == AF_INET);
925dd7a6f5fSkcpoon 		g_netmask = *addr;
926dd7a6f5fSkcpoon 		if (!in_getmask((struct sockaddr_in *)&g_netmask, _B_TRUE)) {
927dd7a6f5fSkcpoon 			lifr->lifr_addr = *mask;
928dd7a6f5fSkcpoon 			g_netmask_set = G_NETMASK_NIL;
929dd7a6f5fSkcpoon 		} else {
930dd7a6f5fSkcpoon 			lifr->lifr_addr = g_netmask;
931dd7a6f5fSkcpoon 			g_netmask_set = G_NETMASK_SET;
932dd7a6f5fSkcpoon 		}
933dd7a6f5fSkcpoon 		break;
934dd7a6f5fSkcpoon 
935dd7a6f5fSkcpoon 	case G_NETMASK_NIL:
936dd7a6f5fSkcpoon 	default:
937dd7a6f5fSkcpoon 		lifr->lifr_addr = *mask;
938dd7a6f5fSkcpoon 		break;
939dd7a6f5fSkcpoon 	}
940dd7a6f5fSkcpoon }
941dd7a6f5fSkcpoon 
9427c478bd9Sstevel@tonic-gate /*
9437c478bd9Sstevel@tonic-gate  * Set the interface address. Handles <addr>, <addr>/<n> as well as /<n>
9447c478bd9Sstevel@tonic-gate  * syntax for setting the address, the address plus netmask, and just
9457c478bd9Sstevel@tonic-gate  * the netmask respectively.
9467c478bd9Sstevel@tonic-gate  */
9477c478bd9Sstevel@tonic-gate /* ARGSUSED */
9487c478bd9Sstevel@tonic-gate static int
9497c478bd9Sstevel@tonic-gate setifaddr(char *addr, int64_t param)
9507c478bd9Sstevel@tonic-gate {
9517c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
9527c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage laddr;
9537c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage netmask;
9547c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 *sin6;
9557c478bd9Sstevel@tonic-gate 	struct	sockaddr_in *sin;
9567c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage sav_netmask;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	if (addr[0] == '/')
9597c478bd9Sstevel@tonic-gate 		return (setifprefixlen(addr, 0));
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, (struct sockaddr *)&laddr, &prefixlen);
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	(void) memset(&netmask, 0, sizeof (netmask));
9647c478bd9Sstevel@tonic-gate 	netmask.ss_family = afp->af_af;
9657c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
9667c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
9677c478bd9Sstevel@tonic-gate 		/* Nothing there - ok */
9687c478bd9Sstevel@tonic-gate 		break;
9697c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
9707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: Bad prefix length in %s\n",
9717c478bd9Sstevel@tonic-gate 		    addr);
9727c478bd9Sstevel@tonic-gate 		exit(1);
9737c478bd9Sstevel@tonic-gate 	default:
9747c478bd9Sstevel@tonic-gate 		if (afp->af_af == AF_INET6) {
9757c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&netmask;
9767906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
9777c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin6->sin6_addr)) {
9787c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
9797c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
9807c478bd9Sstevel@tonic-gate 				    prefixlen);
9817c478bd9Sstevel@tonic-gate 				exit(1);
9827c478bd9Sstevel@tonic-gate 			}
9837c478bd9Sstevel@tonic-gate 		} else {
9847c478bd9Sstevel@tonic-gate 			sin = (struct sockaddr_in *)&netmask;
9857906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IP_ABITS,
9867c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin->sin_addr)) {
9877c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
9887c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
9897c478bd9Sstevel@tonic-gate 				    prefixlen);
9907c478bd9Sstevel@tonic-gate 				exit(1);
9917c478bd9Sstevel@tonic-gate 			}
9927c478bd9Sstevel@tonic-gate 		}
9937c478bd9Sstevel@tonic-gate 		/*
9947c478bd9Sstevel@tonic-gate 		 * Just in case of funny setting of both prefix and netmask,
9957c478bd9Sstevel@tonic-gate 		 * prefix should override the netmask command.
9967c478bd9Sstevel@tonic-gate 		 */
997dd7a6f5fSkcpoon 		g_netmask_set = G_NETMASK_NIL;
9987c478bd9Sstevel@tonic-gate 		break;
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate 	/* Tell parser that an address was set */
10017c478bd9Sstevel@tonic-gate 	setaddr++;
10027c478bd9Sstevel@tonic-gate 	/* save copy of netmask to restore in case of error */
10037c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
10047c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0)
10057c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCGLIFNETMASK");
10067c478bd9Sstevel@tonic-gate 	sav_netmask = lifr.lifr_addr;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	/*
10097c478bd9Sstevel@tonic-gate 	 * If setting the address and not the mask, clear any existing mask
10107c478bd9Sstevel@tonic-gate 	 * and the kernel will then assign the default (netmask has been set
10117c478bd9Sstevel@tonic-gate 	 * to 0 in this case).  If setting both (either by using a prefix or
10127c478bd9Sstevel@tonic-gate 	 * using the netmask command), set the mask first, so the address will
10137c478bd9Sstevel@tonic-gate 	 * be interpreted correctly.
10147c478bd9Sstevel@tonic-gate 	 */
1015dd7a6f5fSkcpoon 	set_mask_lifreq(&lifr, &laddr, &netmask);
10167c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
10177c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if (debug) {
10207c478bd9Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
10217c478bd9Sstevel@tonic-gate 		void *addr = (afp->af_af == AF_INET) ?
10227c478bd9Sstevel@tonic-gate 		    (void *)&((struct sockaddr_in *)&laddr)->sin_addr :
10237c478bd9Sstevel@tonic-gate 		    (void *)&((struct sockaddr_in6 *)&laddr)->sin6_addr;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 		(void) printf("Setting %s af %d addr %s\n",
10267c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, afp->af_af,
10277c478bd9Sstevel@tonic-gate 		    inet_ntop(afp->af_af, addr, abuf, sizeof (abuf)));
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = laddr;
10307c478bd9Sstevel@tonic-gate 	lifr.lifr_addr.ss_family = afp->af_af;
10317c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
10327c478bd9Sstevel@tonic-gate 		/*
10337c478bd9Sstevel@tonic-gate 		 * Restore the netmask
10347c478bd9Sstevel@tonic-gate 		 */
10357c478bd9Sstevel@tonic-gate 		int saverr = errno;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
10387c478bd9Sstevel@tonic-gate 		lifr.lifr_addr = sav_netmask;
10397c478bd9Sstevel@tonic-gate 		(void) ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr);
10407c478bd9Sstevel@tonic-gate 		errno = saverr;
10417c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFADDR");
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	return (0);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate /*
10487c478bd9Sstevel@tonic-gate  * The following functions are stolen from the ipseckey(1m) program.
10497c478bd9Sstevel@tonic-gate  * Perhaps they should be somewhere common, but for now, we just maintain
10507c478bd9Sstevel@tonic-gate  * two versions.  We do this because of the different semantics for which
10517c478bd9Sstevel@tonic-gate  * algorithms we select ("requested" for ifconfig vs. "actual" for key).
10527c478bd9Sstevel@tonic-gate  */
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate static ulong_t
10557c478bd9Sstevel@tonic-gate parsenum(char *num)
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	ulong_t rc;
10587c478bd9Sstevel@tonic-gate 	char *end = NULL;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	errno = 0;
10617c478bd9Sstevel@tonic-gate 	rc = strtoul(num, &end, 0);
10627c478bd9Sstevel@tonic-gate 	if (errno != 0 || end == num || *end != '\0') {
10637c478bd9Sstevel@tonic-gate 		rc = (ulong_t)-1;
10647c478bd9Sstevel@tonic-gate 	}
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	return (rc);
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate /*
10707c478bd9Sstevel@tonic-gate  * Parse and reverse parse possible algorithm values, include numbers.
10717c478bd9Sstevel@tonic-gate  * Mostly stolen from ipseckey.c. See the comments above parsenum() for why
10727c478bd9Sstevel@tonic-gate  * this isn't common to ipseckey.c.
10737c478bd9Sstevel@tonic-gate  *
10747c478bd9Sstevel@tonic-gate  * NOTE: Static buffer in this function for the return value.  Since ifconfig
10757c478bd9Sstevel@tonic-gate  *	 isn't multithreaded, this isn't a huge problem.
10767c478bd9Sstevel@tonic-gate  */
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate #define	NBUF_SIZE 20	/* Enough to print a large integer. */
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate static char *
10817c478bd9Sstevel@tonic-gate rparsealg(uint8_t alg_value, int proto_num)
10827c478bd9Sstevel@tonic-gate {
10837c478bd9Sstevel@tonic-gate 	struct ipsecalgent *alg;
10847c478bd9Sstevel@tonic-gate 	static char numprint[128];	/* Enough to hold an algorithm name. */
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	/* Special-case 0 to return "<any-none>" */
10877c478bd9Sstevel@tonic-gate 	if (alg_value == 0)
10887c478bd9Sstevel@tonic-gate 		return ("<any-none>");
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	alg = getipsecalgbynum(alg_value, proto_num, NULL);
10917c478bd9Sstevel@tonic-gate 	if (alg != NULL) {
10927c478bd9Sstevel@tonic-gate 		(void) strlcpy(numprint, alg->a_names[0], sizeof (numprint));
10937c478bd9Sstevel@tonic-gate 		freeipsecalgent(alg);
10947c478bd9Sstevel@tonic-gate 	} else {
10957c478bd9Sstevel@tonic-gate 		(void) snprintf(numprint, sizeof (numprint), "%d", alg_value);
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	return (numprint);
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate static uint_t
11027c478bd9Sstevel@tonic-gate parsealg(char *algname, int proto_num)
11037c478bd9Sstevel@tonic-gate {
11047c478bd9Sstevel@tonic-gate 	struct ipsecalgent *alg;
11057c478bd9Sstevel@tonic-gate 	ulong_t invalue;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	if (algname == NULL) {
11087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: Unexpected end of command "
11097c478bd9Sstevel@tonic-gate 		    "line.\n");
11107c478bd9Sstevel@tonic-gate 		exit(1);
11117c478bd9Sstevel@tonic-gate 	}
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	/*
11147c478bd9Sstevel@tonic-gate 	 * Special-case "none". Use strcasecmp because its length is
11157c478bd9Sstevel@tonic-gate 	 * bound.
11167c478bd9Sstevel@tonic-gate 	 */
11177c478bd9Sstevel@tonic-gate 	if (strcasecmp("none", algname) == 0) {
11187c478bd9Sstevel@tonic-gate 		return ((proto_num == IPSEC_PROTO_ESP) ?
11197c478bd9Sstevel@tonic-gate 		    NO_ESP_EALG : NO_ESP_AALG);
11207c478bd9Sstevel@tonic-gate 	}
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	alg = getipsecalgbyname(algname, proto_num, NULL);
11237c478bd9Sstevel@tonic-gate 	if (alg != NULL) {
11247c478bd9Sstevel@tonic-gate 		invalue = alg->a_alg_num;
11257c478bd9Sstevel@tonic-gate 		freeipsecalgent(alg);
11267c478bd9Sstevel@tonic-gate 		return ((uint_t)invalue);
11277c478bd9Sstevel@tonic-gate 	}
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	/*
11307c478bd9Sstevel@tonic-gate 	 * Since algorithms can be loaded during kernel run-time, check for
11317c478bd9Sstevel@tonic-gate 	 * numeric algorithm values too.
11327c478bd9Sstevel@tonic-gate 	 */
11337c478bd9Sstevel@tonic-gate 	invalue = parsenum(algname);
11347c478bd9Sstevel@tonic-gate 	if ((invalue & (ulong_t)0xff) == invalue)
11357c478bd9Sstevel@tonic-gate 		return ((uint_t)invalue);
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "ifconfig: %s algorithm type %s unknown.\n",
11387c478bd9Sstevel@tonic-gate 	    (proto_num == IPSEC_PROTO_ESP) ?
11397c478bd9Sstevel@tonic-gate 	    "Encryption" : "Authentication", algname);
11407c478bd9Sstevel@tonic-gate 	exit(1);
11417c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
11427c478bd9Sstevel@tonic-gate }
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate /*
11457c478bd9Sstevel@tonic-gate  * Actual ifconfig functions to set tunnel security properties.
11467c478bd9Sstevel@tonic-gate  */
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate enum ipsec_alg_type { ESP_ENCR_ALG = 1, ESP_AUTH_ALG, AH_AUTH_ALG };
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate boolean_t first_set_tun = _B_TRUE;
11517c478bd9Sstevel@tonic-gate boolean_t encr_alg_set = _B_FALSE;
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate static int
11547c478bd9Sstevel@tonic-gate set_tun_algs(int which_alg, int alg)
11557c478bd9Sstevel@tonic-gate {
11567c478bd9Sstevel@tonic-gate 	struct iftun_req treq;
11577c478bd9Sstevel@tonic-gate 	ipsec_req_t *ipsr;
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	(void) strncpy(treq.ifta_lifr_name, name, sizeof (treq.ifta_lifr_name));
11607c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
11617c478bd9Sstevel@tonic-gate 		errno = EPERM;
11627c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
11637c478bd9Sstevel@tonic-gate 	}
11647c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGTUNPARAM, (caddr_t)&treq) < 0) {
11657c478bd9Sstevel@tonic-gate 		if (errno == EOPNOTSUPP || errno == EINVAL)
11667c478bd9Sstevel@tonic-gate 			Perror0_exit("Not a tunnel");
11677c478bd9Sstevel@tonic-gate 		else Perror0_exit("SIOCGTUNPARAM");
11687c478bd9Sstevel@tonic-gate 	}
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	ipsr = (ipsec_req_t *)&treq.ifta_secinfo;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	if (treq.ifta_vers != IFTUN_VERSION) {
11737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11747c478bd9Sstevel@tonic-gate 		    "Kernel tunnel secinfo version mismatch.\n");
11757c478bd9Sstevel@tonic-gate 		exit(1);
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	/*
11797c478bd9Sstevel@tonic-gate 	 * If I'm just starting off this ifconfig, I want a clean slate,
11807c478bd9Sstevel@tonic-gate 	 * otherwise, I've captured the current tunnel security settings.
11817c478bd9Sstevel@tonic-gate 	 * In the case of continuation, I merely add to the settings.
11827c478bd9Sstevel@tonic-gate 	 */
11837c478bd9Sstevel@tonic-gate 	if (first_set_tun) {
11847c478bd9Sstevel@tonic-gate 		first_set_tun = _B_FALSE;
11857c478bd9Sstevel@tonic-gate 		(void) memset(ipsr, 0, sizeof (*ipsr));
11867c478bd9Sstevel@tonic-gate 	}
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	treq.ifta_flags = IFTUN_SECURITY;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	switch (which_alg) {
11917c478bd9Sstevel@tonic-gate 	case ESP_ENCR_ALG:
11927c478bd9Sstevel@tonic-gate 		if (alg == NO_ESP_EALG) {
11937c478bd9Sstevel@tonic-gate 			if (ipsr->ipsr_esp_auth_alg == SADB_AALG_NONE)
11947c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_req = 0;
11957c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_alg = SADB_EALG_NONE;
11967c478bd9Sstevel@tonic-gate 		} else {
11977c478bd9Sstevel@tonic-gate 			encr_alg_set = _B_TRUE;
11987c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_req =
11997c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12007c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_alg = alg;
12017c478bd9Sstevel@tonic-gate 		}
12027c478bd9Sstevel@tonic-gate 		break;
12037c478bd9Sstevel@tonic-gate 	case ESP_AUTH_ALG:
12047c478bd9Sstevel@tonic-gate 		if (alg == NO_ESP_AALG) {
12057c478bd9Sstevel@tonic-gate 			if (ipsr->ipsr_esp_alg == SADB_EALG_NONE ||
12067c478bd9Sstevel@tonic-gate 			    ipsr->ipsr_esp_alg == SADB_EALG_NULL)
12077c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_req = 0;
12087c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_auth_alg = SADB_AALG_NONE;
12097c478bd9Sstevel@tonic-gate 		} else {
12107c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_req =
12117c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12127c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_auth_alg = alg;
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 			/* Let the user specify NULL encryption implicitly. */
12157c478bd9Sstevel@tonic-gate 			if (ipsr->ipsr_esp_alg == SADB_EALG_NONE &&
12167c478bd9Sstevel@tonic-gate 			    !encr_alg_set)
12177c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_alg = SADB_EALG_NULL;
12187c478bd9Sstevel@tonic-gate 		}
12197c478bd9Sstevel@tonic-gate 		break;
12207c478bd9Sstevel@tonic-gate 	case AH_AUTH_ALG:
12217c478bd9Sstevel@tonic-gate 		if (alg == NO_AH_AALG) {
12227c478bd9Sstevel@tonic-gate 			ipsr->ipsr_ah_req = 0;
12237c478bd9Sstevel@tonic-gate 			ipsr->ipsr_auth_alg = SADB_AALG_NONE;
12247c478bd9Sstevel@tonic-gate 		} else {
12257c478bd9Sstevel@tonic-gate 			ipsr->ipsr_ah_req =
12267c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12277c478bd9Sstevel@tonic-gate 			ipsr->ipsr_auth_alg = alg;
12287c478bd9Sstevel@tonic-gate 		}
12297c478bd9Sstevel@tonic-gate 		break;
12307c478bd9Sstevel@tonic-gate 		/* Will never hit DEFAULT */
12317c478bd9Sstevel@tonic-gate 	}
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSTUNPARAM, (caddr_t)&treq) < 0) {
12347c478bd9Sstevel@tonic-gate 		Perror2_exit("set tunnel security properties",
12357c478bd9Sstevel@tonic-gate 		    treq.ifta_lifr_name);
12367c478bd9Sstevel@tonic-gate 	}
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	return (0);
12397c478bd9Sstevel@tonic-gate }
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate /* ARGSUSED */
12427c478bd9Sstevel@tonic-gate static int
12437c478bd9Sstevel@tonic-gate set_tun_esp_encr_alg(char *addr, int64_t param)
12447c478bd9Sstevel@tonic-gate {
12457c478bd9Sstevel@tonic-gate 	return (set_tun_algs(ESP_ENCR_ALG,
12467c478bd9Sstevel@tonic-gate 		    parsealg(addr, IPSEC_PROTO_ESP)));
12477c478bd9Sstevel@tonic-gate }
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate /* ARGSUSED */
12507c478bd9Sstevel@tonic-gate static int
12517c478bd9Sstevel@tonic-gate set_tun_esp_auth_alg(char *addr, int64_t param)
12527c478bd9Sstevel@tonic-gate {
12537c478bd9Sstevel@tonic-gate 	return (set_tun_algs(ESP_AUTH_ALG,
12547c478bd9Sstevel@tonic-gate 		    parsealg(addr, IPSEC_PROTO_AH)));
12557c478bd9Sstevel@tonic-gate }
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate /* ARGSUSED */
12587c478bd9Sstevel@tonic-gate static int
12597c478bd9Sstevel@tonic-gate set_tun_ah_alg(char *addr, int64_t param)
12607c478bd9Sstevel@tonic-gate {
12617c478bd9Sstevel@tonic-gate 	return (set_tun_algs(AH_AUTH_ALG,
12627c478bd9Sstevel@tonic-gate 		    parsealg(addr, IPSEC_PROTO_AH)));
12637c478bd9Sstevel@tonic-gate }
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate /* ARGSUSED */
12667c478bd9Sstevel@tonic-gate static int
12677c478bd9Sstevel@tonic-gate setifrevarp(char *arg, int64_t param)
12687c478bd9Sstevel@tonic-gate {
12697c478bd9Sstevel@tonic-gate 	struct sockaddr_in	laddr;
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	if (afp->af_af == AF_INET6) {
12727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12737c478bd9Sstevel@tonic-gate 		    "ifconfig: revarp not possible on IPv6 interface %s\n",
12747c478bd9Sstevel@tonic-gate 		    name);
12757c478bd9Sstevel@tonic-gate 		exit(1);
12767c478bd9Sstevel@tonic-gate 	}
12777c478bd9Sstevel@tonic-gate 	if (doifrevarp(name, &laddr)) {
12787c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
12797c478bd9Sstevel@tonic-gate 		laddr.sin_family = AF_INET;
12807c478bd9Sstevel@tonic-gate 		(void) memcpy(&lifr.lifr_addr, &laddr, sizeof (laddr));
12817c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
12827c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCSLIFADDR");
12837c478bd9Sstevel@tonic-gate 	}
12847c478bd9Sstevel@tonic-gate 	return (0);
12857c478bd9Sstevel@tonic-gate }
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate /* ARGSUSED */
12887c478bd9Sstevel@tonic-gate static int
12897c478bd9Sstevel@tonic-gate setifsubnet(char *addr, int64_t param)
12907c478bd9Sstevel@tonic-gate {
12917c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
12927c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage subnet;
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, &subnet, &prefixlen);
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
12977c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
12987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12997c478bd9Sstevel@tonic-gate 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
13007c478bd9Sstevel@tonic-gate 		exit(1);
13017c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
13027c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
13037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13047c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
13057c478bd9Sstevel@tonic-gate 		exit(1);
13067c478bd9Sstevel@tonic-gate 	default:
13077c478bd9Sstevel@tonic-gate 		break;
13087c478bd9Sstevel@tonic-gate 	}
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = subnet;
13117c478bd9Sstevel@tonic-gate 	lifr.lifr_addrlen = prefixlen;
13127c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13137c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFSUBNET, (caddr_t)&lifr) < 0)
13147c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFSUBNET");
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	return (0);
13177c478bd9Sstevel@tonic-gate }
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate /* ARGSUSED */
13207c478bd9Sstevel@tonic-gate static int
13217c478bd9Sstevel@tonic-gate setifnetmask(char *addr, int64_t param)
13227c478bd9Sstevel@tonic-gate {
13237c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask;
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	assert(afp->af_af != AF_INET6);
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	if (strcmp(addr, "+") == 0) {
1328dd7a6f5fSkcpoon 		if (!in_getmask(&netmask, _B_FALSE))
13297c478bd9Sstevel@tonic-gate 			return (0);
1330dd7a6f5fSkcpoon 		(void) printf("Setting netmask of %s to %s\n", name,
1331dd7a6f5fSkcpoon 		    inet_ntoa(netmask.sin_addr));
13327c478bd9Sstevel@tonic-gate 	} else {
13337c478bd9Sstevel@tonic-gate 		in_getaddr(addr, (struct sockaddr *)&netmask, NULL);
13347c478bd9Sstevel@tonic-gate 	}
13357c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13367c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &netmask, sizeof (netmask));
13377c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
13387c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
13397c478bd9Sstevel@tonic-gate 	return (0);
13407c478bd9Sstevel@tonic-gate }
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate /*
13437c478bd9Sstevel@tonic-gate  * Parse '/<n>' as a netmask.
13447c478bd9Sstevel@tonic-gate  */
13457c478bd9Sstevel@tonic-gate /* ARGSUSED */
13467c478bd9Sstevel@tonic-gate static int
13477c478bd9Sstevel@tonic-gate setifprefixlen(char *addr, int64_t param)
13487c478bd9Sstevel@tonic-gate {
13497c478bd9Sstevel@tonic-gate 	int prefixlen;
13507c478bd9Sstevel@tonic-gate 	int af = afp->af_af;
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	prefixlen = in_getprefixlen(addr, _B_TRUE,
13537906a3e0Smeem 	    (af == AF_INET) ? IP_ABITS : IPV6_ABITS);
13547c478bd9Sstevel@tonic-gate 	if (prefixlen < 0) {
13557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13567c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
13577c478bd9Sstevel@tonic-gate 		exit(1);
13587c478bd9Sstevel@tonic-gate 	}
13597c478bd9Sstevel@tonic-gate 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
13607c478bd9Sstevel@tonic-gate 	lifr.lifr_addr.ss_family = af;
13617c478bd9Sstevel@tonic-gate 	if (af == AF_INET6) {
13627c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
13657906a3e0Smeem 		if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
13667c478bd9Sstevel@tonic-gate 		    (uchar_t *)&sin6->sin6_addr)) {
13677c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: "
13687c478bd9Sstevel@tonic-gate 			    "Bad prefix length: %d\n",
13697c478bd9Sstevel@tonic-gate 			    prefixlen);
13707c478bd9Sstevel@tonic-gate 			exit(1);
13717c478bd9Sstevel@tonic-gate 		}
13727c478bd9Sstevel@tonic-gate 	} else if (af == AF_INET) {
13737c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin;
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
13767906a3e0Smeem 		if (!in_prefixlentomask(prefixlen, IP_ABITS,
13777c478bd9Sstevel@tonic-gate 		    (uchar_t *)&sin->sin_addr)) {
13787c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: "
13797c478bd9Sstevel@tonic-gate 			    "Bad prefix length: %d\n",
13807c478bd9Sstevel@tonic-gate 			    prefixlen);
13817c478bd9Sstevel@tonic-gate 			exit(1);
13827c478bd9Sstevel@tonic-gate 		}
13837c478bd9Sstevel@tonic-gate 	} else {
13847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: setting prefix only supported"
13857c478bd9Sstevel@tonic-gate 		    " for address family inet or inet6\n");
13867c478bd9Sstevel@tonic-gate 		exit(1);
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13897c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
13907c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
13917c478bd9Sstevel@tonic-gate 	return (0);
13927c478bd9Sstevel@tonic-gate }
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate /* ARGSUSED */
13957c478bd9Sstevel@tonic-gate static int
13967c478bd9Sstevel@tonic-gate setifbroadaddr(char *addr, int64_t param)
13977c478bd9Sstevel@tonic-gate {
13987c478bd9Sstevel@tonic-gate 	struct	sockaddr_in broadaddr;
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	assert(afp->af_af != AF_INET6);
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 	if (strcmp(addr, "+") == 0) {
14037c478bd9Sstevel@tonic-gate 		/*
14047c478bd9Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
14057c478bd9Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
14067c478bd9Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
14077c478bd9Sstevel@tonic-gate 		 */
14087c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name,
14097c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
14107c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
14117c478bd9Sstevel@tonic-gate 			if (errno != EADDRNOTAVAIL)
14127c478bd9Sstevel@tonic-gate 				Perror0_exit("SIOCGLIFADDR");
14137c478bd9Sstevel@tonic-gate 			return (0);
14147c478bd9Sstevel@tonic-gate 		}
14157c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
14167c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCGLIFADDR");
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 		return (0);
14197c478bd9Sstevel@tonic-gate 	}
14207c478bd9Sstevel@tonic-gate 	in_getaddr(addr, (struct sockaddr *)&broadaddr, NULL);
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &broadaddr, sizeof (broadaddr));
14237c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14247c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFBRDADDR, (caddr_t)&lifr) < 0)
14257c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFBRDADDR");
14267c478bd9Sstevel@tonic-gate 	return (0);
14277c478bd9Sstevel@tonic-gate }
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate /*
14307c478bd9Sstevel@tonic-gate  * set interface destination address
14317c478bd9Sstevel@tonic-gate  */
14327c478bd9Sstevel@tonic-gate /* ARGSUSED */
14337c478bd9Sstevel@tonic-gate static int
14347c478bd9Sstevel@tonic-gate setifdstaddr(char *addr, int64_t param)
14357c478bd9Sstevel@tonic-gate {
14367c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, (struct sockaddr *)&lifr.lifr_addr, NULL);
14377c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14387c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFDSTADDR, (caddr_t)&lifr) < 0)
14397c478bd9Sstevel@tonic-gate 		Perror0_exit("setifdstaddr: SIOCSLIFDSTADDR");
14407c478bd9Sstevel@tonic-gate 	return (0);
14417c478bd9Sstevel@tonic-gate }
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate /* ARGSUSED */
14447c478bd9Sstevel@tonic-gate static int
14457c478bd9Sstevel@tonic-gate setifflags(char *val, int64_t value)
14467c478bd9Sstevel@tonic-gate {
14477c478bd9Sstevel@tonic-gate 	int phyintlen, origphyintlen;
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14507c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0)
14517c478bd9Sstevel@tonic-gate 		Perror0_exit("setifflags: SIOCGLIFFLAGS");
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	if (value == IFF_NOFAILOVER) {
14547c478bd9Sstevel@tonic-gate 		/*
14557c478bd9Sstevel@tonic-gate 		 * Fail if '-failover' is set after a prior addif created the
14567c478bd9Sstevel@tonic-gate 		 * alias on a different interface. This can happen when the
14577c478bd9Sstevel@tonic-gate 		 * interface is part of an IPMP group.
14587c478bd9Sstevel@tonic-gate 		 */
14597c478bd9Sstevel@tonic-gate 		phyintlen = strcspn(name, ":");
14607c478bd9Sstevel@tonic-gate 		origphyintlen = strcspn(origname, ":");
14617c478bd9Sstevel@tonic-gate 		if (phyintlen != origphyintlen ||
14627c478bd9Sstevel@tonic-gate 		    strncmp(name, origname, phyintlen) != 0) {
14637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: can't set -failover "
14647c478bd9Sstevel@tonic-gate 			    "on failed/standby/offlined interface %s\n",
14657c478bd9Sstevel@tonic-gate 			    origname);
14667c478bd9Sstevel@tonic-gate 			exit(1);
14677c478bd9Sstevel@tonic-gate 		}
14687c478bd9Sstevel@tonic-gate 	}
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	if (value < 0) {
14717c478bd9Sstevel@tonic-gate 		value = -value;
14727c478bd9Sstevel@tonic-gate 		lifr.lifr_flags &= ~value;
1473*69bb4bb4Scarlsonj 		if ((value & IFF_UP) && (lifr.lifr_flags & IFF_DUPLICATE)) {
1474*69bb4bb4Scarlsonj 			/*
1475*69bb4bb4Scarlsonj 			 * If the user is trying to mark an interface with a
1476*69bb4bb4Scarlsonj 			 * duplicate address as "down," then fetch the address
1477*69bb4bb4Scarlsonj 			 * and set it.  This will cause IP to clear the
1478*69bb4bb4Scarlsonj 			 * IFF_DUPLICATE flag and stop the automatic recovery
1479*69bb4bb4Scarlsonj 			 * timer.
1480*69bb4bb4Scarlsonj 			 */
1481*69bb4bb4Scarlsonj 			value = lifr.lifr_flags;
1482*69bb4bb4Scarlsonj 			if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0)
1483*69bb4bb4Scarlsonj 				(void) ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr);
1484*69bb4bb4Scarlsonj 			lifr.lifr_flags = value;
1485*69bb4bb4Scarlsonj 		}
1486*69bb4bb4Scarlsonj 	} else {
14877c478bd9Sstevel@tonic-gate 		lifr.lifr_flags |= value;
1488*69bb4bb4Scarlsonj 	}
14897c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14907c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
14917c478bd9Sstevel@tonic-gate 		Perror0_exit("setifflags: SIOCSLIFFLAGS");
14927c478bd9Sstevel@tonic-gate 	}
14937c478bd9Sstevel@tonic-gate 	return (0);
14947c478bd9Sstevel@tonic-gate }
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate /* ARGSUSED */
14977c478bd9Sstevel@tonic-gate static int
14987c478bd9Sstevel@tonic-gate setifmetric(char *val, int64_t param)
14997c478bd9Sstevel@tonic-gate {
15007c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15017c478bd9Sstevel@tonic-gate 	lifr.lifr_metric = atoi(val);
15027c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFMETRIC, (caddr_t)&lifr) < 0)
15037c478bd9Sstevel@tonic-gate 		Perror0_exit("setifmetric: SIOCSLIFMETRIC");
15047c478bd9Sstevel@tonic-gate 	return (0);
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate /* ARGSUSED */
15087c478bd9Sstevel@tonic-gate static int
15097c478bd9Sstevel@tonic-gate setifmtu(char *val, int64_t param)
15107c478bd9Sstevel@tonic-gate {
15117c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15127c478bd9Sstevel@tonic-gate 	lifr.lifr_mtu = atoi(val);
15137c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFMTU, (caddr_t)&lifr) < 0)
15147c478bd9Sstevel@tonic-gate 		Perror0_exit("setifmtu: SIOCSLIFMTU");
15157c478bd9Sstevel@tonic-gate 	return (0);
15167c478bd9Sstevel@tonic-gate }
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate /* ARGSUSED */
15197c478bd9Sstevel@tonic-gate static int
15207c478bd9Sstevel@tonic-gate setifindex(char *val, int64_t param)
15217c478bd9Sstevel@tonic-gate {
15227c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15237c478bd9Sstevel@tonic-gate 	lifr.lifr_index = atoi(val);
15247c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFINDEX, (caddr_t)&lifr) < 0)
15257c478bd9Sstevel@tonic-gate 		Perror0_exit("setifindex: SIOCSLIFINDEX");
15267c478bd9Sstevel@tonic-gate 	return (0);
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate /* ARGSUSED */
15307c478bd9Sstevel@tonic-gate static int
15317c478bd9Sstevel@tonic-gate setifether(char *addr, int64_t param)
15327c478bd9Sstevel@tonic-gate {
15337c478bd9Sstevel@tonic-gate 	uchar_t	*ea;
15347c478bd9Sstevel@tonic-gate 	iface_t	*current;
15357c478bd9Sstevel@tonic-gate 	int	maclen;
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
15387c478bd9Sstevel@tonic-gate 		ifstatus(name);
15397c478bd9Sstevel@tonic-gate 		print_ifether(name);
15407c478bd9Sstevel@tonic-gate 		return (0);
15417c478bd9Sstevel@tonic-gate 	}
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	phyif = NULL;
15447c478bd9Sstevel@tonic-gate 	logifs = NULL;
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	/*
15477c478bd9Sstevel@tonic-gate 	 * if the IP interface in the arguments is a logical
15487c478bd9Sstevel@tonic-gate 	 * interface, exit with an error now.
15497c478bd9Sstevel@tonic-gate 	 */
15507c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
15517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: cannot change"
15527c478bd9Sstevel@tonic-gate 		    " ethernet address of a logical interface\n");
15537c478bd9Sstevel@tonic-gate 		exit(1);
15547c478bd9Sstevel@tonic-gate 	}
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	ea = _link_aton(addr, &maclen);
15577c478bd9Sstevel@tonic-gate 	if (ea == NULL) {
15587c478bd9Sstevel@tonic-gate 		if (maclen == -1)
15597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
15607c478bd9Sstevel@tonic-gate 			    "ifconfig: %s: bad address\n", addr);
15617c478bd9Sstevel@tonic-gate 		else
15627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: malloc() failed\n");
15637c478bd9Sstevel@tonic-gate 		exit(1);
15647c478bd9Sstevel@tonic-gate 	}
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	(void) strncpy(savedname, name, sizeof (savedname));
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	/*
15697c478bd9Sstevel@tonic-gate 	 * Call selectifs only for the IP interfaces that are ipv4.
15707c478bd9Sstevel@tonic-gate 	 * offflags == IFF_IPV6 because you should not change the
15717c478bd9Sstevel@tonic-gate 	 *		Ethernet address of an ipv6 interface
15727c478bd9Sstevel@tonic-gate 	 */
15737c478bd9Sstevel@tonic-gate 	foreachinterface(selectifs, 0, (char **)NULL, 0, 0, IFF_IPV6, 0);
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	/* If physical interface not found, exit now */
15767c478bd9Sstevel@tonic-gate 	if (phyif == NULL) {
15777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
15787c478bd9Sstevel@tonic-gate 		    "ifconfig: interface %s not found\n", savedname);
15797c478bd9Sstevel@tonic-gate 		exit(1);
15807c478bd9Sstevel@tonic-gate 	}
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 	/* Restore */
15837c478bd9Sstevel@tonic-gate 	(void) strncpy(name, savedname, sizeof (name));
15847c478bd9Sstevel@tonic-gate 	(void) strncpy(origname, savedname, sizeof (origname));
15857c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	/*
15887c478bd9Sstevel@tonic-gate 	 * close and reopen the socket
15897c478bd9Sstevel@tonic-gate 	 * we don't know which type of socket we have now
15907c478bd9Sstevel@tonic-gate 	 */
15917c478bd9Sstevel@tonic-gate 	(void) close(s);
15927c478bd9Sstevel@tonic-gate 	s = socket(SOCKET_AF(AF_UNSPEC), SOCK_DGRAM, 0);
15937c478bd9Sstevel@tonic-gate 	if (s < 0) {
15947c478bd9Sstevel@tonic-gate 		Perror0_exit("socket");
15957c478bd9Sstevel@tonic-gate 	}
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	/*
15987c478bd9Sstevel@tonic-gate 	 * mark down the logical interfaces first,
15997c478bd9Sstevel@tonic-gate 	 * and then the physical interface
16007c478bd9Sstevel@tonic-gate 	 */
16017c478bd9Sstevel@tonic-gate 	if (updownifs(logifs, 0) < 0 || updownifs(phyif, 0) < 0) {
16027c478bd9Sstevel@tonic-gate 		Perror0_exit("mark down interface failed");
16037c478bd9Sstevel@tonic-gate 	}
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	/*
16067c478bd9Sstevel@tonic-gate 	 * Change the physical address
16077c478bd9Sstevel@tonic-gate 	 */
16087c478bd9Sstevel@tonic-gate 	if (dlpi_set_address(savedname, ea, maclen) == -1) {
16097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
16107c478bd9Sstevel@tonic-gate 		    "ifconfig: failed setting mac address on %s\n",
16117c478bd9Sstevel@tonic-gate 		    savedname);
16127c478bd9Sstevel@tonic-gate 	}
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	/*
16157c478bd9Sstevel@tonic-gate 	 * if any interfaces were marked down before changing the
16167c478bd9Sstevel@tonic-gate 	 * ethernet address, put them up again.
16177c478bd9Sstevel@tonic-gate 	 * First the physical interface, then the logical ones.
16187c478bd9Sstevel@tonic-gate 	 */
16197c478bd9Sstevel@tonic-gate 	if (updownifs(phyif, 1) < 0 || updownifs(logifs, 1) < 0) {
16207c478bd9Sstevel@tonic-gate 		Perror0_exit("mark down interface failed");
16217c478bd9Sstevel@tonic-gate 	}
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	/* Free the memory allocated by selectifs */
16247c478bd9Sstevel@tonic-gate 	free(phyif);
16257c478bd9Sstevel@tonic-gate 	for (current = logifs; current != NULL; current = logifs) {
16267c478bd9Sstevel@tonic-gate 		logifs = logifs->next;
16277c478bd9Sstevel@tonic-gate 		free(current);
16287c478bd9Sstevel@tonic-gate 	}
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	return (0);
16317c478bd9Sstevel@tonic-gate }
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate /*
16347c478bd9Sstevel@tonic-gate  * Print an interface's Ethernet address, if it has one.
16357c478bd9Sstevel@tonic-gate  */
16367c478bd9Sstevel@tonic-gate static void
16377c478bd9Sstevel@tonic-gate print_ifether(char *ifname)
16387c478bd9Sstevel@tonic-gate {
16397c478bd9Sstevel@tonic-gate 	int		protocol;
16407c478bd9Sstevel@tonic-gate 	icfg_if_t	interface;
16417c478bd9Sstevel@tonic-gate 	icfg_handle_t	handle;
16427c478bd9Sstevel@tonic-gate 	int		fd;
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	fd = socket(AF_INET, SOCK_DGRAM, 0);
16477c478bd9Sstevel@tonic-gate 	if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
16487c478bd9Sstevel@tonic-gate 		/*
16497c478bd9Sstevel@tonic-gate 		 * It's possible the interface is only configured for
16507c478bd9Sstevel@tonic-gate 		 * IPv6; check again with AF_INET6.
16517c478bd9Sstevel@tonic-gate 		 */
16527c478bd9Sstevel@tonic-gate 		(void) close(fd);
16537c478bd9Sstevel@tonic-gate 		fd = socket(AF_INET6, SOCK_DGRAM, 0);
16547c478bd9Sstevel@tonic-gate 		if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
16557c478bd9Sstevel@tonic-gate 			(void) close(fd);
16567c478bd9Sstevel@tonic-gate 			return;
16577c478bd9Sstevel@tonic-gate 		}
16587c478bd9Sstevel@tonic-gate 	}
16597c478bd9Sstevel@tonic-gate 	(void) close(fd);
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	/* Virtual interfaces don't have MAC addresses */
16627c478bd9Sstevel@tonic-gate 	if (lifr.lifr_flags & IFF_VIRTUAL)
16637c478bd9Sstevel@tonic-gate 		return;
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 	/*
16667c478bd9Sstevel@tonic-gate 	 * We must be careful to set if_protocol based on the current
16677c478bd9Sstevel@tonic-gate 	 * properties of the interface.  For instance, if "ip.tun0" is
16687c478bd9Sstevel@tonic-gate 	 * configured only as an IPv6 tunnel, then if_protocol must be
16697c478bd9Sstevel@tonic-gate 	 * set to AF_INET6 or icfg_get_tunnel_lower() will fail and
16707c478bd9Sstevel@tonic-gate 	 * we will falsely conclude that it's not a tunnel.
16717c478bd9Sstevel@tonic-gate 	 */
16727c478bd9Sstevel@tonic-gate 	interface.if_protocol = AF_INET;
16737c478bd9Sstevel@tonic-gate 	if (lifr.lifr_flags & IFF_IPV6)
16747c478bd9Sstevel@tonic-gate 		interface.if_protocol = AF_INET6;
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, ifname, sizeof (interface.if_name));
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 	if (icfg_open(&handle, &interface) == ICFG_SUCCESS) {
16797c478bd9Sstevel@tonic-gate 		if (icfg_get_tunnel_lower(handle, &protocol) == ICFG_SUCCESS) {
16807c478bd9Sstevel@tonic-gate 			/* Tunnel op succeeded -- it's a tunnel so skip */
16817c478bd9Sstevel@tonic-gate 			icfg_close(handle);
16827c478bd9Sstevel@tonic-gate 			return;
16837c478bd9Sstevel@tonic-gate 		}
16847c478bd9Sstevel@tonic-gate 		icfg_close(handle);
16857c478bd9Sstevel@tonic-gate 	}
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 	dlpi_print_address(ifname);
16887c478bd9Sstevel@tonic-gate }
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate /*
16917c478bd9Sstevel@tonic-gate  * static void selectifs(int argc, char *argv[], int af, struct lifreq *rp)
16927c478bd9Sstevel@tonic-gate  *
16937c478bd9Sstevel@tonic-gate  * Called inside setifether() to create a list of interfaces to
16947c478bd9Sstevel@tonic-gate  * mark down/up when changing the Ethernet address.
16957c478bd9Sstevel@tonic-gate  * If the current interface is the physical interface passed
16967c478bd9Sstevel@tonic-gate  * as an argument to ifconfig, update phyif.
16977c478bd9Sstevel@tonic-gate  * If the current interface is a logical interface associated
16987c478bd9Sstevel@tonic-gate  * to the physical interface, add it to the logifs list.
16997c478bd9Sstevel@tonic-gate  */
17007c478bd9Sstevel@tonic-gate /* ARGSUSED */
17017c478bd9Sstevel@tonic-gate static void
17027c478bd9Sstevel@tonic-gate selectifs(int argc, char *argv[], int af, struct lifreq *rp)
17037c478bd9Sstevel@tonic-gate {
17047c478bd9Sstevel@tonic-gate 	char		*colonp;
17057c478bd9Sstevel@tonic-gate 	int		length;
17067c478bd9Sstevel@tonic-gate 	iface_t		*current;
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	/*
17097c478bd9Sstevel@tonic-gate 	 *  savedname=	name of the IP interface to which you want to
17107c478bd9Sstevel@tonic-gate 	 *		change ethernet address
17117c478bd9Sstevel@tonic-gate 	 *  name=	name of the current IP interface
17127c478bd9Sstevel@tonic-gate 	 */
17137c478bd9Sstevel@tonic-gate 	colonp = strchr(name, ':');
17147c478bd9Sstevel@tonic-gate 	if (colonp == NULL)
17157c478bd9Sstevel@tonic-gate 		length = max(strlen(savedname), strlen(name));
17167c478bd9Sstevel@tonic-gate 	else
17177c478bd9Sstevel@tonic-gate 		length = max(strlen(savedname), colonp - name);
17187c478bd9Sstevel@tonic-gate 	if (strncmp(savedname, name, length) == 0) {
17197c478bd9Sstevel@tonic-gate 		(void) strcpy(lifr.lifr_name, name);
17207c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFFLAGS, &lifr) < 0) {
17217c478bd9Sstevel@tonic-gate 			Perror0("selectifs: SIOCGLIFFLAGS");
17227c478bd9Sstevel@tonic-gate 			return;
17237c478bd9Sstevel@tonic-gate 		}
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 		if ((current = malloc(sizeof (iface_t))) == NULL) {
17267c478bd9Sstevel@tonic-gate 			Perror0_exit("selectifs: malloc failed\n");
17277c478bd9Sstevel@tonic-gate 		}
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 		if (colonp == NULL) {
17307c478bd9Sstevel@tonic-gate 			/* this is the physical interface */
17317c478bd9Sstevel@tonic-gate 			phyif = current;
17327c478bd9Sstevel@tonic-gate 			bcopy(&lifr, &phyif->lifr, sizeof (struct lifreq));
17337c478bd9Sstevel@tonic-gate 			phyif->next = NULL;
17347c478bd9Sstevel@tonic-gate 		} else {
17357c478bd9Sstevel@tonic-gate 			/* this is a logical interface */
17367c478bd9Sstevel@tonic-gate 			bcopy(&lifr, &current->lifr, sizeof (struct lifreq));
17377c478bd9Sstevel@tonic-gate 			current->next = logifs;
17387c478bd9Sstevel@tonic-gate 			logifs = current;
17397c478bd9Sstevel@tonic-gate 		}
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate }
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate /*
17447c478bd9Sstevel@tonic-gate  * static int updownifs(iface_t *ifs, int up)
17457c478bd9Sstevel@tonic-gate  *
17467c478bd9Sstevel@tonic-gate  * It takes in input a list of IP interfaces (ifs)
17477c478bd9Sstevel@tonic-gate  * and a flag (up).
17487c478bd9Sstevel@tonic-gate  * It marks each interface in the list down (up = 0)
17497c478bd9Sstevel@tonic-gate  * or up (up > 0). This is done ONLY if the IP
17507c478bd9Sstevel@tonic-gate  * interface was originally up.
17517c478bd9Sstevel@tonic-gate  *
17527c478bd9Sstevel@tonic-gate  * Return values:
17537c478bd9Sstevel@tonic-gate  *  0 = everything OK
17547c478bd9Sstevel@tonic-gate  * -1 = problem
17557c478bd9Sstevel@tonic-gate  */
17567c478bd9Sstevel@tonic-gate static int
17577c478bd9Sstevel@tonic-gate updownifs(iface_t *ifs, int up)
17587c478bd9Sstevel@tonic-gate {
17597c478bd9Sstevel@tonic-gate 	iface_t *current;
17607c478bd9Sstevel@tonic-gate 	int	ret = 0;
17617c478bd9Sstevel@tonic-gate 	int 	save_errno;
17627c478bd9Sstevel@tonic-gate 	char	savename[LIFNAMSIZ];
17637c478bd9Sstevel@tonic-gate 	uint64_t orig_flags;
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	for (current = ifs; current != NULL; current = current->next) {
17667c478bd9Sstevel@tonic-gate 		if (current->lifr.lifr_flags & IFF_UP) {
17677c478bd9Sstevel@tonic-gate 			orig_flags = current->lifr.lifr_flags;
17687c478bd9Sstevel@tonic-gate 			if (!up)
17697c478bd9Sstevel@tonic-gate 				current->lifr.lifr_flags &= ~IFF_UP;
17707c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCSLIFFLAGS, &current->lifr) < 0) {
17717c478bd9Sstevel@tonic-gate 				save_errno = errno;
17727c478bd9Sstevel@tonic-gate 				(void) strcpy(savename,
17737c478bd9Sstevel@tonic-gate 				    current->lifr.lifr_name);
17747c478bd9Sstevel@tonic-gate 				ret = -1;
17757c478bd9Sstevel@tonic-gate 			}
17767c478bd9Sstevel@tonic-gate 			if (!up) /* restore the original flags */
17777c478bd9Sstevel@tonic-gate 				current->lifr.lifr_flags = orig_flags;
17787c478bd9Sstevel@tonic-gate 		}
17797c478bd9Sstevel@tonic-gate 	}
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	if (ret == -1) {
17827c478bd9Sstevel@tonic-gate 		(void) strcpy(lifr.lifr_name, savename);
17837c478bd9Sstevel@tonic-gate 		errno = save_errno;
17847c478bd9Sstevel@tonic-gate 	}
17857c478bd9Sstevel@tonic-gate 	return (ret);
17867c478bd9Sstevel@tonic-gate }
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate /*
17897c478bd9Sstevel@tonic-gate  * Create the next unused logical interface using the original name
17907c478bd9Sstevel@tonic-gate  * and assign the address (and mask if '/<n>' is part of the address).
17917c478bd9Sstevel@tonic-gate  * Use the new logical interface for subsequent subcommands by updating
17927c478bd9Sstevel@tonic-gate  * the name variable.
17937c478bd9Sstevel@tonic-gate  *
17947c478bd9Sstevel@tonic-gate  * This allows syntax like:
17957c478bd9Sstevel@tonic-gate  *	ifconfig le0 addif 109.106.86.130 netmask + up \
17967c478bd9Sstevel@tonic-gate  *	addif 109.106.86.131 netmask + up
17977c478bd9Sstevel@tonic-gate  */
17987c478bd9Sstevel@tonic-gate /* ARGSUSED */
17997c478bd9Sstevel@tonic-gate static int
18007c478bd9Sstevel@tonic-gate addif(char *str, int64_t param)
18017c478bd9Sstevel@tonic-gate {
18027c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
18037c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
18047c478bd9Sstevel@tonic-gate 	struct sockaddr_storage mask;
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	(void) strncpy(name, origname, sizeof (name));
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
18097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18107c478bd9Sstevel@tonic-gate 		    "ifconfig: addif: bad physical interface name %s\n",
18117c478bd9Sstevel@tonic-gate 		    name);
18127c478bd9Sstevel@tonic-gate 		exit(1);
18137c478bd9Sstevel@tonic-gate 	}
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	/*
18167c478bd9Sstevel@tonic-gate 	 * clear so parser will interpret next address as source followed
18177c478bd9Sstevel@tonic-gate 	 * by possible dest
18187c478bd9Sstevel@tonic-gate 	 */
18197c478bd9Sstevel@tonic-gate 	setaddr = 0;
18207c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(str, (struct sockaddr *)&laddr, &prefixlen);
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
18237c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
18247c478bd9Sstevel@tonic-gate 		/* Nothing there - ok */
18257c478bd9Sstevel@tonic-gate 		break;
18267c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
18277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18287c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", str);
18297c478bd9Sstevel@tonic-gate 		exit(1);
18307c478bd9Sstevel@tonic-gate 	default:
18317c478bd9Sstevel@tonic-gate 		(void) memset(&mask, 0, sizeof (mask));
18327c478bd9Sstevel@tonic-gate 		mask.ss_family = afp->af_af;
18337c478bd9Sstevel@tonic-gate 		if (afp->af_af == AF_INET6) {
18347c478bd9Sstevel@tonic-gate 			struct sockaddr_in6 *sin6;
18357c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&mask;
18367906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
18377c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin6->sin6_addr)) {
18387c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
18397c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
18407c478bd9Sstevel@tonic-gate 				    prefixlen);
18417c478bd9Sstevel@tonic-gate 				exit(1);
18427c478bd9Sstevel@tonic-gate 			}
18437c478bd9Sstevel@tonic-gate 		} else {
18447c478bd9Sstevel@tonic-gate 			struct sockaddr_in *sin;
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 			sin = (struct sockaddr_in *)&mask;
18477906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IP_ABITS,
18487c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin->sin_addr)) {
18497c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
18507c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
18517c478bd9Sstevel@tonic-gate 				    prefixlen);
18527c478bd9Sstevel@tonic-gate 				exit(1);
18537c478bd9Sstevel@tonic-gate 			}
18547c478bd9Sstevel@tonic-gate 		}
1855dd7a6f5fSkcpoon 		g_netmask_set = G_NETMASK_NIL;
18567c478bd9Sstevel@tonic-gate 		break;
18577c478bd9Sstevel@tonic-gate 	}
18587c478bd9Sstevel@tonic-gate 
1859dd7a6f5fSkcpoon 	/*
1860dd7a6f5fSkcpoon 	 * This is a "hack" to get around the problem of SIOCLIFADDIF.  The
1861dd7a6f5fSkcpoon 	 * problem is that this ioctl does not include the netmask when
1862dd7a6f5fSkcpoon 	 * adding a logical interface.  This is the same problem described
1863dd7a6f5fSkcpoon 	 * in the ifconfig() comments.  To get around this problem, we first
1864dd7a6f5fSkcpoon 	 * add the logical interface with a 0 address.  After that, we set
1865dd7a6f5fSkcpoon 	 * the netmask if provided.  Finally we set the interface address.
1866dd7a6f5fSkcpoon 	 */
18677c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1868dd7a6f5fSkcpoon 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	/* Note: no need to do DAD here since the interface isn't up yet. */
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0)
18737c478bd9Sstevel@tonic-gate 		Perror0_exit("addif: SIOCLIFADDIF");
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 	(void) printf("Created new logical interface %s\n",
18767c478bd9Sstevel@tonic-gate 	    lifr.lifr_name);
18777c478bd9Sstevel@tonic-gate 	(void) strncpy(name, lifr.lifr_name, sizeof (name));
18787c478bd9Sstevel@tonic-gate 
1879dd7a6f5fSkcpoon 	/*
1880dd7a6f5fSkcpoon 	 * Check and see if any "netmask" command is used and perform the
1881dd7a6f5fSkcpoon 	 * necessary operation.
1882dd7a6f5fSkcpoon 	 */
1883dd7a6f5fSkcpoon 	set_mask_lifreq(&lifr, &laddr, &mask);
1884dd7a6f5fSkcpoon 	/*
1885dd7a6f5fSkcpoon 	 * Only set the netmask if "netmask" command is used or a prefix is
1886dd7a6f5fSkcpoon 	 * provided.
1887dd7a6f5fSkcpoon 	 */
1888dd7a6f5fSkcpoon 	if (g_netmask_set == G_NETMASK_SET || prefixlen >= 0) {
18897c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
18907c478bd9Sstevel@tonic-gate 			Perror0_exit("addif: SIOCSLIFNETMASK");
18917c478bd9Sstevel@tonic-gate 	}
1892dd7a6f5fSkcpoon 
1893dd7a6f5fSkcpoon 	/* Finally, we set the interface address. */
1894dd7a6f5fSkcpoon 	lifr.lifr_addr = laddr;
1895dd7a6f5fSkcpoon 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1896dd7a6f5fSkcpoon 		Perror0_exit("SIOCSLIFADDR");
1897dd7a6f5fSkcpoon 
18987c478bd9Sstevel@tonic-gate 	/*
18997c478bd9Sstevel@tonic-gate 	 * let parser know we got a source.
19007c478bd9Sstevel@tonic-gate 	 * Next address, if given, should be dest
19017c478bd9Sstevel@tonic-gate 	 */
19027c478bd9Sstevel@tonic-gate 	setaddr++;
19037c478bd9Sstevel@tonic-gate 	return (0);
19047c478bd9Sstevel@tonic-gate }
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate /*
19077c478bd9Sstevel@tonic-gate  * Remove a logical interface based on its IP address. Unlike addif
19087c478bd9Sstevel@tonic-gate  * there is no '/<n>' here.
19097c478bd9Sstevel@tonic-gate  * Verifies that the interface is down before it is removed.
19107c478bd9Sstevel@tonic-gate  */
19117c478bd9Sstevel@tonic-gate /* ARGSUSED */
19127c478bd9Sstevel@tonic-gate static int
19137c478bd9Sstevel@tonic-gate removeif(char *str, int64_t param)
19147c478bd9Sstevel@tonic-gate {
19157c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
19187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
19197c478bd9Sstevel@tonic-gate 		    "ifconfig: removeif: bad physical interface name %s\n",
19207c478bd9Sstevel@tonic-gate 		    name);
19217c478bd9Sstevel@tonic-gate 		exit(1);
19227c478bd9Sstevel@tonic-gate 	}
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(str, &laddr, NULL);
19257c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = laddr;
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
19287c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) {
19297c478bd9Sstevel@tonic-gate 		if (errno == EBUSY) {
19307c478bd9Sstevel@tonic-gate 			/* This can only happen if ipif_id = 0 */
19317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
19327c478bd9Sstevel@tonic-gate 			    "ifconfig: removeif: can't remove interface: %s\n",
19337c478bd9Sstevel@tonic-gate 			    name);
19347c478bd9Sstevel@tonic-gate 			exit(1);
19357c478bd9Sstevel@tonic-gate 		}
19367c478bd9Sstevel@tonic-gate 		Perror0_exit("removeif: SIOCLIFREMOVEIF");
19377c478bd9Sstevel@tonic-gate 	}
19387c478bd9Sstevel@tonic-gate 	return (0);
19397c478bd9Sstevel@tonic-gate }
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate /*
19427c478bd9Sstevel@tonic-gate  * Set the address token for IPv6.
19437c478bd9Sstevel@tonic-gate  */
19447c478bd9Sstevel@tonic-gate /* ARGSUSED */
19457c478bd9Sstevel@tonic-gate static int
19467c478bd9Sstevel@tonic-gate setiftoken(char *addr, int64_t param)
19477c478bd9Sstevel@tonic-gate {
19487c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
19497c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 token;
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	in6_getaddr(addr, (struct sockaddr *)&token, &prefixlen);
19527c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
19537c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
19547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
19557c478bd9Sstevel@tonic-gate 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
19567c478bd9Sstevel@tonic-gate 		exit(1);
19577c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
19587c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
19597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
19607c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
19617c478bd9Sstevel@tonic-gate 		exit(1);
19627c478bd9Sstevel@tonic-gate 	default:
19637c478bd9Sstevel@tonic-gate 		break;
19647c478bd9Sstevel@tonic-gate 	}
19657c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &token, sizeof (token));
19667c478bd9Sstevel@tonic-gate 	lifr.lifr_addrlen = prefixlen;
19677c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
19687c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFTOKEN, (caddr_t)&lifr) < 0)  {
19697c478bd9Sstevel@tonic-gate 		Perror0_exit("setiftoken: SIOCSLIFTOKEN");
19707c478bd9Sstevel@tonic-gate 	}
19717c478bd9Sstevel@tonic-gate 	return (0);
19727c478bd9Sstevel@tonic-gate }
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate /*
19757c478bd9Sstevel@tonic-gate  * Return value: 0 on success, -1 on failure.
19767c478bd9Sstevel@tonic-gate  */
19777c478bd9Sstevel@tonic-gate static int
19787c478bd9Sstevel@tonic-gate connect_to_mpathd(int family)
19797c478bd9Sstevel@tonic-gate {
19807c478bd9Sstevel@tonic-gate 	int s;
19817c478bd9Sstevel@tonic-gate 	struct sockaddr_storage ss;
19827c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
19837c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
19847c478bd9Sstevel@tonic-gate 	struct in6_addr loopback_addr = IN6ADDR_LOOPBACK_INIT;
19857c478bd9Sstevel@tonic-gate 	int addrlen;
19867c478bd9Sstevel@tonic-gate 	int ret;
19877c478bd9Sstevel@tonic-gate 	int on;
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 	s = socket(family, SOCK_STREAM, 0);
19907c478bd9Sstevel@tonic-gate 	if (s < 0) {
19917c478bd9Sstevel@tonic-gate 		Perror0_exit("connect_to_mpathd: socket");
19927c478bd9Sstevel@tonic-gate 	}
19937c478bd9Sstevel@tonic-gate 	(void) bzero((char *)&ss, sizeof (ss));
19947c478bd9Sstevel@tonic-gate 	ss.ss_family = family;
19957c478bd9Sstevel@tonic-gate 	/*
19967c478bd9Sstevel@tonic-gate 	 * Need to bind to a privileged port. For non-root, this
19977c478bd9Sstevel@tonic-gate 	 * will fail. in.mpathd verifies that only commands coming
19987c478bd9Sstevel@tonic-gate 	 * from privileged ports succeed so that ordinary users
19997c478bd9Sstevel@tonic-gate 	 * can't connect and start talking to in.mpathd
20007c478bd9Sstevel@tonic-gate 	 */
20017c478bd9Sstevel@tonic-gate 	on = 1;
20027c478bd9Sstevel@tonic-gate 	if (setsockopt(s, IPPROTO_TCP, TCP_ANONPRIVBIND, &on,
20037c478bd9Sstevel@tonic-gate 	    sizeof (on)) < 0) {
20047c478bd9Sstevel@tonic-gate 		Perror0_exit("connect_to_mpathd: setsockopt");
20057c478bd9Sstevel@tonic-gate 	}
20067c478bd9Sstevel@tonic-gate 	switch (family) {
20077c478bd9Sstevel@tonic-gate 	case AF_INET:
20087c478bd9Sstevel@tonic-gate 		sin->sin_port = 0;
20097c478bd9Sstevel@tonic-gate 		sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
20107c478bd9Sstevel@tonic-gate 		addrlen = sizeof (struct sockaddr_in);
20117c478bd9Sstevel@tonic-gate 		break;
20127c478bd9Sstevel@tonic-gate 	case AF_INET6:
20137c478bd9Sstevel@tonic-gate 		sin6->sin6_port = 0;
20147c478bd9Sstevel@tonic-gate 		sin6->sin6_addr = loopback_addr;
20157c478bd9Sstevel@tonic-gate 		addrlen = sizeof (struct sockaddr_in6);
20167c478bd9Sstevel@tonic-gate 		break;
20177c478bd9Sstevel@tonic-gate 	}
20187c478bd9Sstevel@tonic-gate 	ret = bind(s, (struct sockaddr *)&ss, addrlen);
20197c478bd9Sstevel@tonic-gate 	if (ret != 0) {
20207c478bd9Sstevel@tonic-gate 		(void) close(s);
20217c478bd9Sstevel@tonic-gate 		return (-1);
20227c478bd9Sstevel@tonic-gate 	}
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	switch (family) {
20257c478bd9Sstevel@tonic-gate 	case AF_INET:
20267c478bd9Sstevel@tonic-gate 		sin->sin_port = htons(MPATHD_PORT);
20277c478bd9Sstevel@tonic-gate 		break;
20287c478bd9Sstevel@tonic-gate 	case AF_INET6:
20297c478bd9Sstevel@tonic-gate 		sin6->sin6_port = htons(MPATHD_PORT);
20307c478bd9Sstevel@tonic-gate 		break;
20317c478bd9Sstevel@tonic-gate 	}
20327c478bd9Sstevel@tonic-gate 	ret = connect(s, (struct sockaddr *)&ss, addrlen);
20337c478bd9Sstevel@tonic-gate 	(void) close(s);
20347c478bd9Sstevel@tonic-gate 	return (ret);
20357c478bd9Sstevel@tonic-gate }
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate /* ARGSUSED */
20387c478bd9Sstevel@tonic-gate static int
20397c478bd9Sstevel@tonic-gate setifgroupname(char *grpname, int64_t param)
20407c478bd9Sstevel@tonic-gate {
20417c478bd9Sstevel@tonic-gate 	if (debug) {
20427c478bd9Sstevel@tonic-gate 		(void) printf("Setting groupname %s on interface %s\n",
20437c478bd9Sstevel@tonic-gate 		    grpname, name);
20447c478bd9Sstevel@tonic-gate 	}
20457c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
20467c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_groupname, grpname,
20477c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_groupname));
20487c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFGROUPNAME, (caddr_t)&lifr) < 0) {
20497c478bd9Sstevel@tonic-gate 		Perror0_exit("setifgroupname: SIOCSLIFGROUPNAME");
20507c478bd9Sstevel@tonic-gate 	}
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	/*
20537c478bd9Sstevel@tonic-gate 	 * If the SUNW_NO_MPATHD environment variable is set then don't
20547c478bd9Sstevel@tonic-gate 	 * bother starting up in.mpathd.  See PSARC/2002/249 for the
20557c478bd9Sstevel@tonic-gate 	 * depressing details on this bit of stupidity.
20567c478bd9Sstevel@tonic-gate 	 */
20577c478bd9Sstevel@tonic-gate 	if (getenv("SUNW_NO_MPATHD") != NULL) {
20587c478bd9Sstevel@tonic-gate 		return (0);
20597c478bd9Sstevel@tonic-gate 	}
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 	/*
20627c478bd9Sstevel@tonic-gate 	 * Try to connect to in.mpathd using IPv4. If we succeed,
20637c478bd9Sstevel@tonic-gate 	 * we conclude that in.mpathd is running, and quit.
20647c478bd9Sstevel@tonic-gate 	 */
20657c478bd9Sstevel@tonic-gate 	if (connect_to_mpathd(AF_INET) == 0) {
20667c478bd9Sstevel@tonic-gate 		/* connect succeeded, mpathd is already running */
20677c478bd9Sstevel@tonic-gate 		return (0);
20687c478bd9Sstevel@tonic-gate 	}
20697c478bd9Sstevel@tonic-gate 	/*
20707c478bd9Sstevel@tonic-gate 	 * Try to connect to in.mpathd using IPv6. If we succeed,
20717c478bd9Sstevel@tonic-gate 	 * we conclude that in.mpathd is running, and quit.
20727c478bd9Sstevel@tonic-gate 	 */
20737c478bd9Sstevel@tonic-gate 	if (connect_to_mpathd(AF_INET6) == 0) {
20747c478bd9Sstevel@tonic-gate 		/* connect succeeded, mpathd is already running */
20757c478bd9Sstevel@tonic-gate 		return (0);
20767c478bd9Sstevel@tonic-gate 	}
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 	/*
20797c478bd9Sstevel@tonic-gate 	 * in.mpathd may not be running. Start it now. If it is already
20807c478bd9Sstevel@tonic-gate 	 * running, in.mpathd will take care of handling multiple incarnations
20817c478bd9Sstevel@tonic-gate 	 * of itself. ifconfig only tries to optimize performance by not
20827c478bd9Sstevel@tonic-gate 	 * starting another incarnation of in.mpathd.
20837c478bd9Sstevel@tonic-gate 	 */
20847c478bd9Sstevel@tonic-gate 	switch (fork()) {
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	case -1:
20877c478bd9Sstevel@tonic-gate 		Perror0_exit("setifgroupname: fork");
20887c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
20897c478bd9Sstevel@tonic-gate 	case 0:
20907c478bd9Sstevel@tonic-gate 		(void) execl(MPATHD_PATH, MPATHD_PATH, NULL);
20917c478bd9Sstevel@tonic-gate 		_exit(1);
20927c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
20937c478bd9Sstevel@tonic-gate 	default:
20947c478bd9Sstevel@tonic-gate 		return (0);
20957c478bd9Sstevel@tonic-gate 	}
20967c478bd9Sstevel@tonic-gate }
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate /*
21007c478bd9Sstevel@tonic-gate  * To list all the modules above a given network interface.
21017c478bd9Sstevel@tonic-gate  */
21027c478bd9Sstevel@tonic-gate /* ARGSUSED */
21037c478bd9Sstevel@tonic-gate static int
21047c478bd9Sstevel@tonic-gate modlist(char *null, int64_t param)
21057c478bd9Sstevel@tonic-gate {
21067c478bd9Sstevel@tonic-gate 	int muxfd;
21077c478bd9Sstevel@tonic-gate 	int ipfd_lowstr;
21087c478bd9Sstevel@tonic-gate 	int arpfd_lowstr;
21097c478bd9Sstevel@tonic-gate 	int num_mods;
21107c478bd9Sstevel@tonic-gate 	int i;
21117c478bd9Sstevel@tonic-gate 	struct str_list strlist;
21127c478bd9Sstevel@tonic-gate 	int orig_arpid;
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
21157c478bd9Sstevel@tonic-gate 	if (ip_domux2fd(&muxfd, &ipfd_lowstr, &arpfd_lowstr,
21167c478bd9Sstevel@tonic-gate 	    &orig_arpid) < 0) {
21177c478bd9Sstevel@tonic-gate 		return (-1);
21187c478bd9Sstevel@tonic-gate 	}
21197c478bd9Sstevel@tonic-gate 	if ((num_mods = ioctl(ipfd_lowstr, I_LIST, NULL)) < 0) {
21207c478bd9Sstevel@tonic-gate 		Perror0("cannot I_LIST to get the number of modules");
21217c478bd9Sstevel@tonic-gate 	} else {
21227c478bd9Sstevel@tonic-gate 		if (debug > 0) {
21237c478bd9Sstevel@tonic-gate 			(void) printf("Listing (%d) modules above %s\n",
21247c478bd9Sstevel@tonic-gate 			    num_mods, name);
21257c478bd9Sstevel@tonic-gate 		}
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 		strlist.sl_nmods = num_mods;
21287c478bd9Sstevel@tonic-gate 		strlist.sl_modlist = malloc(sizeof (struct str_mlist) *
21297c478bd9Sstevel@tonic-gate 		    num_mods);
21307c478bd9Sstevel@tonic-gate 		if (strlist.sl_modlist == NULL) {
21317c478bd9Sstevel@tonic-gate 			Perror0("cannot malloc");
21327c478bd9Sstevel@tonic-gate 		} else {
21337c478bd9Sstevel@tonic-gate 			if (ioctl(ipfd_lowstr, I_LIST, (caddr_t)&strlist) < 0) {
21347c478bd9Sstevel@tonic-gate 				Perror0("cannot I_LIST for module names");
21357c478bd9Sstevel@tonic-gate 			} else {
21367c478bd9Sstevel@tonic-gate 				for (i = 0; i < strlist.sl_nmods; i++) {
21377c478bd9Sstevel@tonic-gate 				    (void) printf("%d %s\n", i,
21387c478bd9Sstevel@tonic-gate 				    strlist.sl_modlist[i].l_name);
21397c478bd9Sstevel@tonic-gate 				}
21407c478bd9Sstevel@tonic-gate 			}
21417c478bd9Sstevel@tonic-gate 			free(strlist.sl_modlist);
21427c478bd9Sstevel@tonic-gate 		}
21437c478bd9Sstevel@tonic-gate 	}
21447c478bd9Sstevel@tonic-gate 	return (ip_plink(muxfd, ipfd_lowstr, arpfd_lowstr, orig_arpid));
21457c478bd9Sstevel@tonic-gate }
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate #define	MODINSERT_OP	'i'
21487c478bd9Sstevel@tonic-gate #define	MODREMOVE_OP	'r'
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate /*
21517c478bd9Sstevel@tonic-gate  * To insert a module to the stream of the interface.  It is just a
21527c478bd9Sstevel@tonic-gate  * wrapper.  The real function is modop().
21537c478bd9Sstevel@tonic-gate  */
21547c478bd9Sstevel@tonic-gate /* ARGSUSED */
21557c478bd9Sstevel@tonic-gate static int
21567c478bd9Sstevel@tonic-gate modinsert(char *arg, int64_t param)
21577c478bd9Sstevel@tonic-gate {
21587c478bd9Sstevel@tonic-gate 	return (modop(arg, MODINSERT_OP));
21597c478bd9Sstevel@tonic-gate }
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate /*
21627c478bd9Sstevel@tonic-gate  * To remove a module from the stream of the interface.  It is just a
21637c478bd9Sstevel@tonic-gate  * wrapper.  The real function is modop().
21647c478bd9Sstevel@tonic-gate  */
21657c478bd9Sstevel@tonic-gate /* ARGSUSED */
21667c478bd9Sstevel@tonic-gate static int
21677c478bd9Sstevel@tonic-gate modremove(char *arg, int64_t param)
21687c478bd9Sstevel@tonic-gate {
21697c478bd9Sstevel@tonic-gate 	return (modop(arg, MODREMOVE_OP));
21707c478bd9Sstevel@tonic-gate }
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate /*
21737c478bd9Sstevel@tonic-gate  * Open a stream on /dev/udp, pop off all undesired modules (note that
21747c478bd9Sstevel@tonic-gate  * the user may have configured autopush to add modules above or below
21757c478bd9Sstevel@tonic-gate  * udp), and push the arp module onto raw IP.
21767c478bd9Sstevel@tonic-gate  */
21777c478bd9Sstevel@tonic-gate static int
21787c478bd9Sstevel@tonic-gate open_arp_on_udp(char *udp_dev_name)
21797c478bd9Sstevel@tonic-gate {
21807c478bd9Sstevel@tonic-gate 	int fd;
21817c478bd9Sstevel@tonic-gate 	boolean_t popped;
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	if ((fd = open(udp_dev_name, O_RDWR)) == -1) {
21847c478bd9Sstevel@tonic-gate 		Perror2("open", udp_dev_name);
21857c478bd9Sstevel@tonic-gate 		return (-1);
21867c478bd9Sstevel@tonic-gate 	}
21877c478bd9Sstevel@tonic-gate 	errno = 0;
21887c478bd9Sstevel@tonic-gate 	popped = _B_FALSE;
21897c478bd9Sstevel@tonic-gate 	while (ioctl(fd, I_POP, 0) != -1)
21907c478bd9Sstevel@tonic-gate 		popped = _B_TRUE;
21917c478bd9Sstevel@tonic-gate 	if (!popped) {
21927c478bd9Sstevel@tonic-gate 		Perror2("cannot pop", udp_dev_name);
21937c478bd9Sstevel@tonic-gate 	} else if (errno != EINVAL) {
21947c478bd9Sstevel@tonic-gate 		Perror2("pop", udp_dev_name);
21957c478bd9Sstevel@tonic-gate 	} else if (ioctl(fd, I_PUSH, ARP_MOD_NAME) == -1) {
21967c478bd9Sstevel@tonic-gate 		Perror2("arp PUSH", udp_dev_name);
21977c478bd9Sstevel@tonic-gate 	} else {
21987c478bd9Sstevel@tonic-gate 		return (fd);
21997c478bd9Sstevel@tonic-gate 	}
22007c478bd9Sstevel@tonic-gate 	(void) close(fd);
22017c478bd9Sstevel@tonic-gate 	return (-1);
22027c478bd9Sstevel@tonic-gate }
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate /*
22057c478bd9Sstevel@tonic-gate  * Helper function for mod*() functions.  It gets a fd to the lower IP
22067c478bd9Sstevel@tonic-gate  * stream and I_PUNLINK's the lower stream.  It also initializes the
22077c478bd9Sstevel@tonic-gate  * global variable lifr.
22087c478bd9Sstevel@tonic-gate  *
22097c478bd9Sstevel@tonic-gate  * Param:
22107c478bd9Sstevel@tonic-gate  *	int *udp_fd: (referenced) fd to /dev/udp (upper IP stream).
22117c478bd9Sstevel@tonic-gate  *	int *fd: (referenced) fd to the lower IP stream.
22127c478bd9Sstevel@tonic-gate  *
22137c478bd9Sstevel@tonic-gate  * Return:
22147c478bd9Sstevel@tonic-gate  *	-1 if operation fails, 0 otherwise.
22157c478bd9Sstevel@tonic-gate  *
22167c478bd9Sstevel@tonic-gate  * Please see the big block comment above plumb_one_device()
22177c478bd9Sstevel@tonic-gate  * for the logic of the PLINK/PUNLINK
22187c478bd9Sstevel@tonic-gate  */
22197c478bd9Sstevel@tonic-gate static int
22207c478bd9Sstevel@tonic-gate ip_domux2fd(int *muxfd, int *ipfd_lowstr, int *arpfd_lowstr, int *orig_arpid)
22217c478bd9Sstevel@tonic-gate {
22227c478bd9Sstevel@tonic-gate 	int		ip_fd;
22237c478bd9Sstevel@tonic-gate 	uint64_t	flags;
22247c478bd9Sstevel@tonic-gate 	char		*udp_dev_name;
22257c478bd9Sstevel@tonic-gate 	char		*ip_dev_name;
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 	*orig_arpid = 0;
22287c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
22297c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
22307c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
22317c478bd9Sstevel@tonic-gate 	}
22327c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
22337c478bd9Sstevel@tonic-gate 	if (flags & IFF_IPV4) {
22347c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP_DEV_NAME;
22357c478bd9Sstevel@tonic-gate 		ip_dev_name  = IP_DEV_NAME;
22367c478bd9Sstevel@tonic-gate 	} else if (flags & IFF_IPV6) {
22377c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP6_DEV_NAME;
22387c478bd9Sstevel@tonic-gate 		ip_dev_name  = IP6_DEV_NAME;
22397c478bd9Sstevel@tonic-gate 	} else {
22407c478bd9Sstevel@tonic-gate 		return (-1);
22417c478bd9Sstevel@tonic-gate 	}
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 	if ((ip_fd = open(ip_dev_name, O_RDWR)) < 0) {
22447c478bd9Sstevel@tonic-gate 		Perror2("open", ip_dev_name);
22457c478bd9Sstevel@tonic-gate 		return (-1);
22467c478bd9Sstevel@tonic-gate 	}
22477c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
22487c478bd9Sstevel@tonic-gate 		Perror2("SIOCGLIFMUXID", ip_dev_name);
22497c478bd9Sstevel@tonic-gate 		return (-1);
22507c478bd9Sstevel@tonic-gate 	}
22517c478bd9Sstevel@tonic-gate 	if (debug > 0) {
22527c478bd9Sstevel@tonic-gate 		(void) printf("ARP_muxid %d IP_muxid %d\n",
22537c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid, lifr.lifr_ip_muxid);
22547c478bd9Sstevel@tonic-gate 	}
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 	if ((*muxfd = open_arp_on_udp(udp_dev_name)) == -1)
22577c478bd9Sstevel@tonic-gate 		return (-1);
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 	if (lifr.lifr_arp_muxid != 0) {
22607c478bd9Sstevel@tonic-gate 		if ((*arpfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
22617c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid)) < 0) {
22627c478bd9Sstevel@tonic-gate 			if ((errno == EINVAL) &&
22637c478bd9Sstevel@tonic-gate 			    (flags & (IFF_NOARP | IFF_IPV6))) {
22647c478bd9Sstevel@tonic-gate 				/*
22657c478bd9Sstevel@tonic-gate 				 * Some plumbing utilities set the muxid to
22667c478bd9Sstevel@tonic-gate 				 * -1 or some invalid value to signify that
22677c478bd9Sstevel@tonic-gate 				 * there is no arp stream. Set the muxid to 0
22687c478bd9Sstevel@tonic-gate 				 * before trying to unplumb the IP stream.
22697c478bd9Sstevel@tonic-gate 				 * IP does not allow the IP stream to be
22707c478bd9Sstevel@tonic-gate 				 * unplumbed if it sees a non-null arp muxid,
22717c478bd9Sstevel@tonic-gate 				 * for consistency of IP-ARP streams.
22727c478bd9Sstevel@tonic-gate 				 */
22737c478bd9Sstevel@tonic-gate 				*orig_arpid = lifr.lifr_arp_muxid;
22747c478bd9Sstevel@tonic-gate 				lifr.lifr_arp_muxid = 0;
22757c478bd9Sstevel@tonic-gate 				(void) ioctl(*muxfd, SIOCSLIFMUXID,
22767c478bd9Sstevel@tonic-gate 				    (caddr_t)&lifr);
22777c478bd9Sstevel@tonic-gate 				*arpfd_lowstr = -1;
22787c478bd9Sstevel@tonic-gate 			} else {
22797c478bd9Sstevel@tonic-gate 				Perror0("_I_MUXID2FD");
22807c478bd9Sstevel@tonic-gate 				return (-1);
22817c478bd9Sstevel@tonic-gate 			}
22827c478bd9Sstevel@tonic-gate 		} else if (ioctl(*muxfd, I_PUNLINK,
22837c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid) < 0) {
22847c478bd9Sstevel@tonic-gate 			Perror2("I_PUNLINK", udp_dev_name);
22857c478bd9Sstevel@tonic-gate 			return (-1);
22867c478bd9Sstevel@tonic-gate 		}
22877c478bd9Sstevel@tonic-gate 	} else {
22887c478bd9Sstevel@tonic-gate 		*arpfd_lowstr = -1;
22897c478bd9Sstevel@tonic-gate 	}
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate 	if ((*ipfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
22927c478bd9Sstevel@tonic-gate 	    lifr.lifr_ip_muxid)) < 0) {
22937c478bd9Sstevel@tonic-gate 		Perror0("_I_MUXID2FD");
22947c478bd9Sstevel@tonic-gate 		/* Undo any changes we made */
22957c478bd9Sstevel@tonic-gate 		if (*orig_arpid != 0) {
22967c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = *orig_arpid;
22977c478bd9Sstevel@tonic-gate 			(void) ioctl(*muxfd, SIOCSLIFMUXID, (caddr_t)&lifr);
22987c478bd9Sstevel@tonic-gate 		}
22997c478bd9Sstevel@tonic-gate 		return (-1);
23007c478bd9Sstevel@tonic-gate 	}
23017c478bd9Sstevel@tonic-gate 	if (ioctl(*muxfd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) {
23027c478bd9Sstevel@tonic-gate 		Perror2("I_PUNLINK", udp_dev_name);
23037c478bd9Sstevel@tonic-gate 		/* Undo any changes we made */
23047c478bd9Sstevel@tonic-gate 		if (*orig_arpid != 0) {
23057c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = *orig_arpid;
23067c478bd9Sstevel@tonic-gate 			(void) ioctl(*muxfd, SIOCSLIFMUXID, (caddr_t)&lifr);
23077c478bd9Sstevel@tonic-gate 		}
23087c478bd9Sstevel@tonic-gate 		return (-1);
23097c478bd9Sstevel@tonic-gate 	}
23107c478bd9Sstevel@tonic-gate 	return (0);
23117c478bd9Sstevel@tonic-gate }
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate /*
23147c478bd9Sstevel@tonic-gate  * Helper function for mod*() functions.  It I_PLINK's back the upper and
23157c478bd9Sstevel@tonic-gate  * lower IP streams.  Note that this function must be called after
23167c478bd9Sstevel@tonic-gate  * ip_domux2fd().  In ip_domux2fd(), the global variable lifr is initialized
23177c478bd9Sstevel@tonic-gate  * and ip_plink() needs information in lifr.  So ip_domux2fd() and ip_plink()
23187c478bd9Sstevel@tonic-gate  * must be called in pairs.
23197c478bd9Sstevel@tonic-gate  *
23207c478bd9Sstevel@tonic-gate  * Param:
23217c478bd9Sstevel@tonic-gate  *	int udp_fd: fd to /dev/udp (upper IP stream).
23227c478bd9Sstevel@tonic-gate  *	int fd: fd to the lower IP stream.
23237c478bd9Sstevel@tonic-gate  *
23247c478bd9Sstevel@tonic-gate  * Return:
23257c478bd9Sstevel@tonic-gate  *	-1 if operation fails, 0 otherwise.
23267c478bd9Sstevel@tonic-gate  *
23277c478bd9Sstevel@tonic-gate  * Please see the big block comment above plumb_one_device()
23287c478bd9Sstevel@tonic-gate  * for the logic of the PLINK/PUNLINK
23297c478bd9Sstevel@tonic-gate  */
23307c478bd9Sstevel@tonic-gate static int
23317c478bd9Sstevel@tonic-gate ip_plink(int muxfd, int ipfd_lowstr, int arpfd_lowstr, int orig_arpid)
23327c478bd9Sstevel@tonic-gate {
23337c478bd9Sstevel@tonic-gate 	int ip_muxid;
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	ip_muxid = ioctl(muxfd, I_PLINK, ipfd_lowstr);
23367c478bd9Sstevel@tonic-gate 	if (ip_muxid < 0) {
23377c478bd9Sstevel@tonic-gate 		Perror2("I_PLINK", UDP_DEV_NAME);
23387c478bd9Sstevel@tonic-gate 		return (-1);
23397c478bd9Sstevel@tonic-gate 	}
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate 	/*
23427c478bd9Sstevel@tonic-gate 	 * If there is an arp stream, plink it. If there is no
23437c478bd9Sstevel@tonic-gate 	 * arp stream, then it is possible that the plumbing
23447c478bd9Sstevel@tonic-gate 	 * utility could have stored any value in the arp_muxid.
23457c478bd9Sstevel@tonic-gate 	 * If so, restore it from orig_arpid.
23467c478bd9Sstevel@tonic-gate 	 */
23477c478bd9Sstevel@tonic-gate 	if (arpfd_lowstr != -1) {
23487c478bd9Sstevel@tonic-gate 		if (ioctl(muxfd, I_PLINK, arpfd_lowstr) < 0) {
23497c478bd9Sstevel@tonic-gate 			Perror2("I_PLINK", UDP_DEV_NAME);
23507c478bd9Sstevel@tonic-gate 			return (-1);
23517c478bd9Sstevel@tonic-gate 		}
23527c478bd9Sstevel@tonic-gate 	} else if (orig_arpid != 0) {
23537c478bd9Sstevel@tonic-gate 		/* Undo the changes we did in ip_domux2fd */
23547c478bd9Sstevel@tonic-gate 		lifr.lifr_arp_muxid = orig_arpid;
23557c478bd9Sstevel@tonic-gate 		lifr.lifr_ip_muxid = ip_muxid;
23567c478bd9Sstevel@tonic-gate 		(void) ioctl(muxfd, SIOCSLIFMUXID, (caddr_t)&lifr);
23577c478bd9Sstevel@tonic-gate 	}
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 	return (0);
23607c478bd9Sstevel@tonic-gate }
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate /*
23637c478bd9Sstevel@tonic-gate  * The real function to perform module insertion/removal.
23647c478bd9Sstevel@tonic-gate  *
23657c478bd9Sstevel@tonic-gate  * Param:
23667c478bd9Sstevel@tonic-gate  *	char *arg: the argument string module_name@position
23677c478bd9Sstevel@tonic-gate  *	char op: operation, either MODINSERT_OP or MODREMOVE_OP.
23687c478bd9Sstevel@tonic-gate  *
23697c478bd9Sstevel@tonic-gate  * Return:
23707c478bd9Sstevel@tonic-gate  *	Before doing ip_domux2fd(), this function calls exit(1) in case of
23717c478bd9Sstevel@tonic-gate  *	error.  After ip_domux2fd() is done, it returns -1 for error, 0
23727c478bd9Sstevel@tonic-gate  *	otherwise.
23737c478bd9Sstevel@tonic-gate  */
23747c478bd9Sstevel@tonic-gate static int
23757c478bd9Sstevel@tonic-gate modop(char *arg, char op)
23767c478bd9Sstevel@tonic-gate {
23777c478bd9Sstevel@tonic-gate 	char *pos_p;
23787c478bd9Sstevel@tonic-gate 	int muxfd;
23797c478bd9Sstevel@tonic-gate 	int ipfd_lowstr;  /* IP stream (lower stream of mux) to be plinked */
23807c478bd9Sstevel@tonic-gate 	int arpfd_lowstr; /* ARP stream (lower stream of mux) to be plinked */
23817c478bd9Sstevel@tonic-gate 	struct strmodconf mod;
23827c478bd9Sstevel@tonic-gate 	char *at_char = "@";
23837c478bd9Sstevel@tonic-gate 	char *arg_str;
23847c478bd9Sstevel@tonic-gate 	int orig_arpid;
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 	/* Need to save the original string for -a option. */
23897c478bd9Sstevel@tonic-gate 	if ((arg_str = malloc(strlen(arg) + 1)) == NULL) {
23907c478bd9Sstevel@tonic-gate 		Perror0("cannot malloc");
23917c478bd9Sstevel@tonic-gate 		return (-1);
23927c478bd9Sstevel@tonic-gate 	}
23937c478bd9Sstevel@tonic-gate 	(void) strcpy(arg_str, arg);
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 	if (*arg_str == *at_char) {
23967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
23977c478bd9Sstevel@tonic-gate 		    "ifconfig: must supply a module name\n");
23987c478bd9Sstevel@tonic-gate 		exit(1);
23997c478bd9Sstevel@tonic-gate 	}
24007c478bd9Sstevel@tonic-gate 	mod.mod_name = strtok(arg_str, at_char);
24017c478bd9Sstevel@tonic-gate 	if (strlen(mod.mod_name) > FMNAMESZ) {
24027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: module name too long: %s\n",
24037c478bd9Sstevel@tonic-gate 		    mod.mod_name);
24047c478bd9Sstevel@tonic-gate 		exit(1);
24057c478bd9Sstevel@tonic-gate 	}
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	/*
24087c478bd9Sstevel@tonic-gate 	 * Need to make sure that the core TCP/IP stack modules are not
24097c478bd9Sstevel@tonic-gate 	 * removed.  Otherwise, "bad" things can happen.  If a module
24107c478bd9Sstevel@tonic-gate 	 * is removed and inserted back, it loses its old state.  But
24117c478bd9Sstevel@tonic-gate 	 * the modules above it still have the old state.  E.g. IP assumes
24127c478bd9Sstevel@tonic-gate 	 * fast data path while tunnel after re-inserted assumes that it can
24137c478bd9Sstevel@tonic-gate 	 * receive M_DATA only in fast data path for which it does not have
24147c478bd9Sstevel@tonic-gate 	 * any state.  This is a general caveat of _I_REMOVE/_I_INSERT.
24157c478bd9Sstevel@tonic-gate 	 */
24167c478bd9Sstevel@tonic-gate 	if (op == MODREMOVE_OP &&
24177c478bd9Sstevel@tonic-gate 	    (strcmp(mod.mod_name, ARP_MOD_NAME) == 0 ||
24187c478bd9Sstevel@tonic-gate 	    strcmp(mod.mod_name, IP_MOD_NAME) == 0 ||
24197c478bd9Sstevel@tonic-gate 	    strcmp(mod.mod_name, TUN_NAME) == 0 ||
24207c478bd9Sstevel@tonic-gate 	    strcmp(mod.mod_name, ATUN_NAME) == 0 ||
24217c478bd9Sstevel@tonic-gate 	    strcmp(mod.mod_name, TUN6TO4_NAME) == 0)) {
24227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: cannot remove %s\n",
24237c478bd9Sstevel@tonic-gate 		    mod.mod_name);
24247c478bd9Sstevel@tonic-gate 		exit(1);
24257c478bd9Sstevel@tonic-gate 	}
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 	if ((pos_p = strtok(NULL, at_char)) == NULL) {
24287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: must supply a position\n");
24297c478bd9Sstevel@tonic-gate 		exit(1);
24307c478bd9Sstevel@tonic-gate 	}
24317c478bd9Sstevel@tonic-gate 	mod.pos = atoi(pos_p);
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 	if (ip_domux2fd(&muxfd, &ipfd_lowstr, &arpfd_lowstr,
24347c478bd9Sstevel@tonic-gate 	    &orig_arpid) < 0) {
24357c478bd9Sstevel@tonic-gate 		free(arg_str);
24367c478bd9Sstevel@tonic-gate 		return (-1);
24377c478bd9Sstevel@tonic-gate 	}
24387c478bd9Sstevel@tonic-gate 	switch (op) {
24397c478bd9Sstevel@tonic-gate 	case MODINSERT_OP:
24407c478bd9Sstevel@tonic-gate 		if (debug > 0) {
24417c478bd9Sstevel@tonic-gate 			(void) printf("Inserting module %s at %d\n",
24427c478bd9Sstevel@tonic-gate 			    mod.mod_name, mod.pos);
24437c478bd9Sstevel@tonic-gate 		}
24447c478bd9Sstevel@tonic-gate 		if (ioctl(ipfd_lowstr, _I_INSERT, (caddr_t)&mod) < 0) {
24457c478bd9Sstevel@tonic-gate 			Perror2("fail to insert module", mod.mod_name);
24467c478bd9Sstevel@tonic-gate 		}
24477c478bd9Sstevel@tonic-gate 		break;
24487c478bd9Sstevel@tonic-gate 	case MODREMOVE_OP:
24497c478bd9Sstevel@tonic-gate 		if (debug > 0) {
24507c478bd9Sstevel@tonic-gate 			(void) printf("Removing module %s at %d\n",
24517c478bd9Sstevel@tonic-gate 			    mod.mod_name, mod.pos);
24527c478bd9Sstevel@tonic-gate 		}
24537c478bd9Sstevel@tonic-gate 		if (ioctl(ipfd_lowstr, _I_REMOVE, (caddr_t)&mod) < 0) {
24547c478bd9Sstevel@tonic-gate 			Perror2("fail to remove module", mod.mod_name);
24557c478bd9Sstevel@tonic-gate 		}
24567c478bd9Sstevel@tonic-gate 		break;
24577c478bd9Sstevel@tonic-gate 	default:
24587c478bd9Sstevel@tonic-gate 		/* Should never get to here. */
24597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Unknown operation\n");
24607c478bd9Sstevel@tonic-gate 		break;
24617c478bd9Sstevel@tonic-gate 	}
24627c478bd9Sstevel@tonic-gate 	free(arg_str);
24637c478bd9Sstevel@tonic-gate 	return (ip_plink(muxfd, ipfd_lowstr, arpfd_lowstr, orig_arpid));
24647c478bd9Sstevel@tonic-gate }
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate /*
24677c478bd9Sstevel@tonic-gate  * Set tunnel source address
24687c478bd9Sstevel@tonic-gate  */
24697c478bd9Sstevel@tonic-gate /* ARGSUSED */
24707c478bd9Sstevel@tonic-gate static int
24717c478bd9Sstevel@tonic-gate setiftsrc(char *addr, int64_t param)
24727c478bd9Sstevel@tonic-gate {
24737c478bd9Sstevel@tonic-gate 	return (settaddr(addr, icfg_set_tunnel_src));
24747c478bd9Sstevel@tonic-gate }
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate /*
24777c478bd9Sstevel@tonic-gate  * Set tunnel destination address
24787c478bd9Sstevel@tonic-gate  */
24797c478bd9Sstevel@tonic-gate /* ARGSUSED */
24807c478bd9Sstevel@tonic-gate static int
24817c478bd9Sstevel@tonic-gate setiftdst(char *addr, int64_t param)
24827c478bd9Sstevel@tonic-gate {
24837c478bd9Sstevel@tonic-gate 	return (settaddr(addr, icfg_set_tunnel_dest));
24847c478bd9Sstevel@tonic-gate }
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate /*
24877c478bd9Sstevel@tonic-gate  * sets tunnels src|dst address.  settaddr() expects the following:
24887c478bd9Sstevel@tonic-gate  * addr: Points to a printable string containing the address to be
24897c478bd9Sstevel@tonic-gate  *       set, e.g. 129.153.128.110.
24907c478bd9Sstevel@tonic-gate  * fn:   Pointer to a libinetcfg routine that will do the actual work.
24917c478bd9Sstevel@tonic-gate  *       The only valid functions are icfg_set_tunnel_src and
24927c478bd9Sstevel@tonic-gate  *       icfg_set_tunnel_dest.
24937c478bd9Sstevel@tonic-gate  */
24947c478bd9Sstevel@tonic-gate static int
24957c478bd9Sstevel@tonic-gate settaddr(char *addr,
24967c478bd9Sstevel@tonic-gate     int (*fn)(icfg_handle_t, const struct sockaddr *, socklen_t))
24977c478bd9Sstevel@tonic-gate {
24987c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
24997c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
25007c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
25017c478bd9Sstevel@tonic-gate 	int lower;
25027c478bd9Sstevel@tonic-gate 	int rc;
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
25057c478bd9Sstevel@tonic-gate 		errno = EPERM;
25067c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
25077c478bd9Sstevel@tonic-gate 	}
25087c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
25097c478bd9Sstevel@tonic-gate 	interface.if_protocol = SOCKET_AF(af);
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate 	/* Open interface. */
25127c478bd9Sstevel@tonic-gate 	if ((rc = icfg_open(&handle, &interface)) != ICFG_SUCCESS)
25137c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
25147c478bd9Sstevel@tonic-gate 
25157c478bd9Sstevel@tonic-gate 	rc = icfg_get_tunnel_lower(handle, &lower);
25167c478bd9Sstevel@tonic-gate 	if (rc != ICFG_SUCCESS)
25177c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	if (lower == AF_INET) {
25207c478bd9Sstevel@tonic-gate 		in_getaddr(addr, (struct sockaddr *)&laddr, NULL);
25217c478bd9Sstevel@tonic-gate 	} else {
25227c478bd9Sstevel@tonic-gate 		in6_getaddr(addr, (struct sockaddr *)&laddr, NULL);
25237c478bd9Sstevel@tonic-gate 	}
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate 	/* Call fn to do the real work, and close the interface. */
25267c478bd9Sstevel@tonic-gate 	rc = (*fn)(handle, (struct sockaddr *)&laddr,
25277c478bd9Sstevel@tonic-gate 	    sizeof (struct sockaddr_storage));
25287c478bd9Sstevel@tonic-gate 	icfg_close(handle);
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 	if (rc != ICFG_SUCCESS)
25317c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 	return (0);
25347c478bd9Sstevel@tonic-gate }
25357c478bd9Sstevel@tonic-gate 
25367c478bd9Sstevel@tonic-gate /* Set tunnel encapsulation limit. */
25377c478bd9Sstevel@tonic-gate /* ARGSUSED */
25387c478bd9Sstevel@tonic-gate static int
25397c478bd9Sstevel@tonic-gate set_tun_encap_limit(char *arg, int64_t param)
25407c478bd9Sstevel@tonic-gate {
25417c478bd9Sstevel@tonic-gate 	short limit;
25427c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
25437c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
25447c478bd9Sstevel@tonic-gate 	int rc;
25457c478bd9Sstevel@tonic-gate 
25467c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
25477c478bd9Sstevel@tonic-gate 		errno = EPERM;
25487c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
25497c478bd9Sstevel@tonic-gate 	}
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	if ((sscanf(arg, "%hd", &limit) != 1) || (limit < 0) ||
25527c478bd9Sstevel@tonic-gate 	    (limit > 255)) {
25537c478bd9Sstevel@tonic-gate 		errno = EINVAL;
25547c478bd9Sstevel@tonic-gate 		Perror0_exit("Invalid encapsulation limit");
25557c478bd9Sstevel@tonic-gate 	}
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate 	/* Open interface for configuration. */
25587c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
25597c478bd9Sstevel@tonic-gate 	interface.if_protocol = SOCKET_AF(af);
25607c478bd9Sstevel@tonic-gate 	if (icfg_open(&handle, &interface) != ICFG_SUCCESS)
25617c478bd9Sstevel@tonic-gate 		Perror0_exit("couldn't open interface");
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	rc = icfg_set_tunnel_encaplimit(handle, (int)limit);
25647c478bd9Sstevel@tonic-gate 	icfg_close(handle);
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate 	if (rc != ICFG_SUCCESS)
25677c478bd9Sstevel@tonic-gate 		Perror0_exit("Could not configure tunnel encapsulation limit");
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate 	return (0);
25707c478bd9Sstevel@tonic-gate }
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate /* Disable encapsulation limit. */
25737c478bd9Sstevel@tonic-gate /* ARGSUSED */
25747c478bd9Sstevel@tonic-gate static int
25757c478bd9Sstevel@tonic-gate clr_tun_encap_limit(char *arg, int64_t param)
25767c478bd9Sstevel@tonic-gate {
25777c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
25787c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
25797c478bd9Sstevel@tonic-gate 	int rc;
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
25827c478bd9Sstevel@tonic-gate 		errno = EPERM;
25837c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
25847c478bd9Sstevel@tonic-gate 	}
25857c478bd9Sstevel@tonic-gate 
25867c478bd9Sstevel@tonic-gate 	/* Open interface for configuration. */
25877c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
25887c478bd9Sstevel@tonic-gate 	interface.if_protocol = SOCKET_AF(af);
25897c478bd9Sstevel@tonic-gate 	if (icfg_open(&handle, &interface) != ICFG_SUCCESS)
25907c478bd9Sstevel@tonic-gate 		Perror0_exit("couldn't open interface");
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	rc = icfg_set_tunnel_encaplimit(handle, -1);
25937c478bd9Sstevel@tonic-gate 	icfg_close(handle);
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 	if (rc != ICFG_SUCCESS)
25967c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	return (0);
25997c478bd9Sstevel@tonic-gate }
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate /* Set tunnel hop limit. */
26027c478bd9Sstevel@tonic-gate /* ARGSUSED */
26037c478bd9Sstevel@tonic-gate static int
26047c478bd9Sstevel@tonic-gate set_tun_hop_limit(char *arg, int64_t param)
26057c478bd9Sstevel@tonic-gate {
26067c478bd9Sstevel@tonic-gate 	unsigned short limit;
26077c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
26087c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
26097c478bd9Sstevel@tonic-gate 	int rc;
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
26127c478bd9Sstevel@tonic-gate 		errno = EPERM;
26137c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
26147c478bd9Sstevel@tonic-gate 	}
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate 	/*
26177c478bd9Sstevel@tonic-gate 	 * Check limit here since it's really only an 8-bit unsigned quantity.
26187c478bd9Sstevel@tonic-gate 	 */
26197c478bd9Sstevel@tonic-gate 	if ((sscanf(arg, "%hu", &limit) != 1) || (limit > 255)) {
26207c478bd9Sstevel@tonic-gate 		errno = EINVAL;
26217c478bd9Sstevel@tonic-gate 		Perror0_exit("Invalid hop limit");
26227c478bd9Sstevel@tonic-gate 	}
26237c478bd9Sstevel@tonic-gate 
26247c478bd9Sstevel@tonic-gate 	/* Open interface for configuration. */
26257c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
26267c478bd9Sstevel@tonic-gate 	interface.if_protocol = SOCKET_AF(af);
26277c478bd9Sstevel@tonic-gate 	if (icfg_open(&handle, &interface) != ICFG_SUCCESS)
26287c478bd9Sstevel@tonic-gate 		Perror0_exit("couldn't open interface");
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate 	rc = icfg_set_tunnel_hoplimit(handle, (uint8_t)limit);
26317c478bd9Sstevel@tonic-gate 	icfg_close(handle);
26327c478bd9Sstevel@tonic-gate 
26337c478bd9Sstevel@tonic-gate 	if (rc != ICFG_SUCCESS)
26347c478bd9Sstevel@tonic-gate 		Perror0_exit("Could not configure tunnel hop limit");
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 	return (0);
26377c478bd9Sstevel@tonic-gate }
26387c478bd9Sstevel@tonic-gate 
26397c478bd9Sstevel@tonic-gate /* Set zone ID */
26407c478bd9Sstevel@tonic-gate static int
26417c478bd9Sstevel@tonic-gate setzone(char *arg, int64_t param)
26427c478bd9Sstevel@tonic-gate {
26437c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = GLOBAL_ZONEID;
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	if (param == NEXTARG) {
26467c478bd9Sstevel@tonic-gate 		/* zone must be active */
26477c478bd9Sstevel@tonic-gate 		if ((zoneid = getzoneidbyname(arg)) == -1) {
26487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
26497c478bd9Sstevel@tonic-gate 			    "ifconfig: unknown zone '%s'\n", arg);
26507c478bd9Sstevel@tonic-gate 			exit(1);
26517c478bd9Sstevel@tonic-gate 		}
26527c478bd9Sstevel@tonic-gate 	}
26537c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
26547c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zoneid;
26557c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
26567c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFZONE");
26577c478bd9Sstevel@tonic-gate 	return (0);
26587c478bd9Sstevel@tonic-gate }
26597c478bd9Sstevel@tonic-gate 
266045916cd2Sjpk /* Put interface into all zones */
266145916cd2Sjpk /* ARGSUSED */
266245916cd2Sjpk static int
266345916cd2Sjpk setallzones(char *arg, int64_t param)
266445916cd2Sjpk {
266545916cd2Sjpk 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
266645916cd2Sjpk 	lifr.lifr_zoneid = ALL_ZONES;
266745916cd2Sjpk 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
266845916cd2Sjpk 		Perror0_exit("SIOCSLIFZONE");
266945916cd2Sjpk 	return (0);
267045916cd2Sjpk }
267145916cd2Sjpk 
26727c478bd9Sstevel@tonic-gate /* Set source address to use */
26737c478bd9Sstevel@tonic-gate /* ARGSUSED */
26747c478bd9Sstevel@tonic-gate static int
26757c478bd9Sstevel@tonic-gate setifsrc(char *arg, int64_t param)
26767c478bd9Sstevel@tonic-gate {
26777c478bd9Sstevel@tonic-gate 	uint_t ifindex = 0;
26787c478bd9Sstevel@tonic-gate 	int rval;
26797c478bd9Sstevel@tonic-gate 
26807c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate 	/*
26837c478bd9Sstevel@tonic-gate 	 * Argument can be either an interface name or "none". The latter means
26847c478bd9Sstevel@tonic-gate 	 * that any previous selection is cleared.
26857c478bd9Sstevel@tonic-gate 	 */
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 	rval = strcmp(arg, name);
26887c478bd9Sstevel@tonic-gate 	if (rval == 0) {
26897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
26907c478bd9Sstevel@tonic-gate 		    "ifconfig: Cannot specify same interface for usesrc"
26917c478bd9Sstevel@tonic-gate 		    " group\n");
26927c478bd9Sstevel@tonic-gate 		exit(1);
26937c478bd9Sstevel@tonic-gate 	}
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate 	rval = strcmp(arg, NONE_STR);
26967c478bd9Sstevel@tonic-gate 	if (rval != 0) {
26977c478bd9Sstevel@tonic-gate 		if ((ifindex = if_nametoindex(arg)) == 0) {
26987c478bd9Sstevel@tonic-gate 			(void) strncpy(lifr.lifr_name, arg, LIFNAMSIZ);
26997c478bd9Sstevel@tonic-gate 			Perror0_exit("Could not get interface index");
27007c478bd9Sstevel@tonic-gate 		}
27017c478bd9Sstevel@tonic-gate 		lifr.lifr_index = ifindex;
27027c478bd9Sstevel@tonic-gate 	} else {
27037c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) != 0)
27047c478bd9Sstevel@tonic-gate 			Perror0_exit("Not a valid usesrc consumer");
27057c478bd9Sstevel@tonic-gate 		lifr.lifr_index = 0;
27067c478bd9Sstevel@tonic-gate 	}
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 	if (debug)
27097c478bd9Sstevel@tonic-gate 		(void) printf("setifsrc: lifr_name %s, lifr_index %d\n",
27107c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, lifr.lifr_index);
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFUSESRC, (caddr_t)&lifr) == -1) {
27137c478bd9Sstevel@tonic-gate 		if (rval == 0)
27147c478bd9Sstevel@tonic-gate 			Perror0_exit("Cannot reset usesrc group");
27157c478bd9Sstevel@tonic-gate 		else
27167c478bd9Sstevel@tonic-gate 			Perror0_exit("Could not set source interface");
27177c478bd9Sstevel@tonic-gate 	}
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 	return (0);
27207c478bd9Sstevel@tonic-gate }
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate /*
27237c478bd9Sstevel@tonic-gate  * Print the interface status line associated with `ifname'
27247c478bd9Sstevel@tonic-gate  */
27257c478bd9Sstevel@tonic-gate static void
27267c478bd9Sstevel@tonic-gate ifstatus(const char *ifname)
27277c478bd9Sstevel@tonic-gate {
27287c478bd9Sstevel@tonic-gate 	uint64_t flags;
27297c478bd9Sstevel@tonic-gate 	char if_usesrc_name[LIFNAMSIZ];
27307c478bd9Sstevel@tonic-gate 	char *newbuf;
27317c478bd9Sstevel@tonic-gate 	int n, numifs, rval = 0;
27327c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp;
27337c478bd9Sstevel@tonic-gate 	struct lifsrcof lifs;
27347c478bd9Sstevel@tonic-gate 
27357c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
27367c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
27377c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
27387c478bd9Sstevel@tonic-gate 	}
27397c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	/*
27427c478bd9Sstevel@tonic-gate 	 * In V4 compatibility mode, we don't print the IFF_IPV4 flag or
27437c478bd9Sstevel@tonic-gate 	 * interfaces with IFF_IPV6 set.
27447c478bd9Sstevel@tonic-gate 	 */
27457c478bd9Sstevel@tonic-gate 	if (v4compat) {
27467c478bd9Sstevel@tonic-gate 		flags &= ~IFF_IPV4;
27477c478bd9Sstevel@tonic-gate 		if (flags & IFF_IPV6)
27487c478bd9Sstevel@tonic-gate 			return;
27497c478bd9Sstevel@tonic-gate 	}
27507c478bd9Sstevel@tonic-gate 
27517c478bd9Sstevel@tonic-gate 	(void) printf("%s: ", ifname);
27527c478bd9Sstevel@tonic-gate 	print_flags(flags);
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
27557c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
27567c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFMETRIC");
27577c478bd9Sstevel@tonic-gate 	} else {
27587c478bd9Sstevel@tonic-gate 		if (lifr.lifr_metric)
27597c478bd9Sstevel@tonic-gate 			(void) printf(" metric %d", lifr.lifr_metric);
27607c478bd9Sstevel@tonic-gate 	}
27617c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
27627c478bd9Sstevel@tonic-gate 		(void) printf(" mtu %d", lifr.lifr_metric);
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 	/* don't print index or zone when in compatibility mode */
27657c478bd9Sstevel@tonic-gate 	if (!v4compat) {
27667c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
27677c478bd9Sstevel@tonic-gate 			(void) printf(" index %d", lifr.lifr_index);
27687c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifr) >= 0 &&
27697c478bd9Sstevel@tonic-gate 		    lifr.lifr_zoneid != getzoneid()) {
27707c478bd9Sstevel@tonic-gate 			char zone_name[ZONENAME_MAX];
27717c478bd9Sstevel@tonic-gate 
277245916cd2Sjpk 			if (lifr.lifr_zoneid == ALL_ZONES) {
277345916cd2Sjpk 				(void) printf("\n\tall-zones");
277445916cd2Sjpk 			} else if (getzonenamebyid(lifr.lifr_zoneid, zone_name,
27757c478bd9Sstevel@tonic-gate 			    sizeof (zone_name)) < 0) {
27767c478bd9Sstevel@tonic-gate 				(void) printf("\n\tzone %d", lifr.lifr_zoneid);
27777c478bd9Sstevel@tonic-gate 			} else {
27787c478bd9Sstevel@tonic-gate 				(void) printf("\n\tzone %s", zone_name);
27797c478bd9Sstevel@tonic-gate 			}
27807c478bd9Sstevel@tonic-gate 		}
27817c478bd9Sstevel@tonic-gate 	}
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) {
27847c478bd9Sstevel@tonic-gate 		lifs.lifs_ifindex = lifr.lifr_index;
27857c478bd9Sstevel@tonic-gate 
27867c478bd9Sstevel@tonic-gate 		/*
27877c478bd9Sstevel@tonic-gate 		 * Find the number of interfaces that use this interfaces'
27887c478bd9Sstevel@tonic-gate 		 * address as a source address
27897c478bd9Sstevel@tonic-gate 		 */
27907c478bd9Sstevel@tonic-gate 		lifs.lifs_buf = NULL;
27917c478bd9Sstevel@tonic-gate 		lifs.lifs_maxlen = 0;
27927c478bd9Sstevel@tonic-gate 		for (;;) {
27937c478bd9Sstevel@tonic-gate 			/* The first pass will give the bufsize we need */
27947c478bd9Sstevel@tonic-gate 			rval = ioctl(s, SIOCGLIFSRCOF, (char *)&lifs);
27957c478bd9Sstevel@tonic-gate 			if (rval < 0) {
27967c478bd9Sstevel@tonic-gate 				if (lifs.lifs_buf != NULL) {
27977c478bd9Sstevel@tonic-gate 					free(lifs.lifs_buf);
27987c478bd9Sstevel@tonic-gate 					lifs.lifs_buf = NULL;
27997c478bd9Sstevel@tonic-gate 				}
28007c478bd9Sstevel@tonic-gate 				lifs.lifs_len = 0;
28017c478bd9Sstevel@tonic-gate 				break;
28027c478bd9Sstevel@tonic-gate 			}
28037c478bd9Sstevel@tonic-gate 			if (lifs.lifs_len <= lifs.lifs_maxlen)
28047c478bd9Sstevel@tonic-gate 				break;
28057c478bd9Sstevel@tonic-gate 			/* Use kernel's size + a small margin to avoid loops */
28067c478bd9Sstevel@tonic-gate 			lifs.lifs_maxlen = lifs.lifs_len +
28077c478bd9Sstevel@tonic-gate 			    5 * sizeof (struct lifreq);
28087c478bd9Sstevel@tonic-gate 			/* For the first pass, realloc acts like malloc */
28097c478bd9Sstevel@tonic-gate 			newbuf = realloc(lifs.lifs_buf, lifs.lifs_maxlen);
28107c478bd9Sstevel@tonic-gate 			if (newbuf == NULL) {
28117c478bd9Sstevel@tonic-gate 				if (lifs.lifs_buf != NULL) {
28127c478bd9Sstevel@tonic-gate 					free(lifs.lifs_buf);
28137c478bd9Sstevel@tonic-gate 					lifs.lifs_buf = NULL;
28147c478bd9Sstevel@tonic-gate 				}
28157c478bd9Sstevel@tonic-gate 				lifs.lifs_len = 0;
28167c478bd9Sstevel@tonic-gate 				break;
28177c478bd9Sstevel@tonic-gate 			}
28187c478bd9Sstevel@tonic-gate 			lifs.lifs_buf = newbuf;
28197c478bd9Sstevel@tonic-gate 		}
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 		numifs = lifs.lifs_len / sizeof (struct lifreq);
28237c478bd9Sstevel@tonic-gate 		if (numifs > 0) {
28247c478bd9Sstevel@tonic-gate 			lifrp = lifs.lifs_req;
28257c478bd9Sstevel@tonic-gate 			(void) printf("\n\tsrcof");
28267c478bd9Sstevel@tonic-gate 			for (n = numifs; n > 0; n--, lifrp++) {
28277c478bd9Sstevel@tonic-gate 				(void) printf(" %s", lifrp->lifr_name);
28287c478bd9Sstevel@tonic-gate 			}
28297c478bd9Sstevel@tonic-gate 		}
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 		if (lifs.lifs_buf != NULL)
28327c478bd9Sstevel@tonic-gate 			free(lifs.lifs_buf);
28337c478bd9Sstevel@tonic-gate 	}
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	/* Find the interface whose source address this interface uses */
28367c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
28377c478bd9Sstevel@tonic-gate 		if (lifr.lifr_index != 0) {
28387c478bd9Sstevel@tonic-gate 			if (if_indextoname(lifr.lifr_index,
28397c478bd9Sstevel@tonic-gate 			    if_usesrc_name) == NULL) {
28407c478bd9Sstevel@tonic-gate 				(void) printf("\n\tusesrc ifIndex %d",
28417c478bd9Sstevel@tonic-gate 				    lifr.lifr_index);
28427c478bd9Sstevel@tonic-gate 			} else {
28437c478bd9Sstevel@tonic-gate 				(void) printf("\n\tusesrc %s", if_usesrc_name);
28447c478bd9Sstevel@tonic-gate 			}
28457c478bd9Sstevel@tonic-gate 		}
28467c478bd9Sstevel@tonic-gate 	}
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
28497c478bd9Sstevel@tonic-gate }
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate /*
28537c478bd9Sstevel@tonic-gate  * Print the status of the interface.  If an address family was
28547c478bd9Sstevel@tonic-gate  * specified, show it and it only; otherwise, show them all.
28557c478bd9Sstevel@tonic-gate  */
28567c478bd9Sstevel@tonic-gate static void
28577c478bd9Sstevel@tonic-gate status(void)
28587c478bd9Sstevel@tonic-gate {
28597c478bd9Sstevel@tonic-gate 	struct afswtch *p = afp;
28607c478bd9Sstevel@tonic-gate 	uint64_t flags;
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
28637c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
28647c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
28657c478bd9Sstevel@tonic-gate 	}
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate 	/*
28707c478bd9Sstevel@tonic-gate 	 * Only print the interface status if the address family matches
28717c478bd9Sstevel@tonic-gate 	 * the interface family flag.
28727c478bd9Sstevel@tonic-gate 	 */
28737c478bd9Sstevel@tonic-gate 	if (p != NULL) {
28747c478bd9Sstevel@tonic-gate 		if (((p->af_af == AF_INET6) && (flags & IFF_IPV4)) ||
28757c478bd9Sstevel@tonic-gate 		    ((p->af_af == AF_INET) && (flags & IFF_IPV6)))
28767c478bd9Sstevel@tonic-gate 			return;
28777c478bd9Sstevel@tonic-gate 	}
28787c478bd9Sstevel@tonic-gate 
28797c478bd9Sstevel@tonic-gate 	/*
28807c478bd9Sstevel@tonic-gate 	 * In V4 compatibility mode, don't print IFF_IPV6 interfaces.
28817c478bd9Sstevel@tonic-gate 	 */
28827c478bd9Sstevel@tonic-gate 	if (v4compat && (flags & IFF_IPV6))
28837c478bd9Sstevel@tonic-gate 		return;
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 	ifstatus(name);
28867c478bd9Sstevel@tonic-gate 
28877c478bd9Sstevel@tonic-gate 	if (p != NULL) {
28887c478bd9Sstevel@tonic-gate 		(*p->af_status)(1, flags);
28897c478bd9Sstevel@tonic-gate 	} else {
28907c478bd9Sstevel@tonic-gate 		for (p = afs; p->af_name; p++) {
28917c478bd9Sstevel@tonic-gate 			(void) close(s);
28927c478bd9Sstevel@tonic-gate 			s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0);
28937c478bd9Sstevel@tonic-gate 			/* set global af for use in p->af_status */
28947c478bd9Sstevel@tonic-gate 			af = p->af_af;
28957c478bd9Sstevel@tonic-gate 			if (s == -1) {
28967c478bd9Sstevel@tonic-gate 				Perror0_exit("socket");
28977c478bd9Sstevel@tonic-gate 			}
28987c478bd9Sstevel@tonic-gate 			(*p->af_status)(0, flags);
28997c478bd9Sstevel@tonic-gate 		}
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 		/*
29027c478bd9Sstevel@tonic-gate 		 * Historically, 'ether' has been an address family,
29037c478bd9Sstevel@tonic-gate 		 * so print it here.
29047c478bd9Sstevel@tonic-gate 		 */
29057c478bd9Sstevel@tonic-gate 		print_ifether(name);
29067c478bd9Sstevel@tonic-gate 	}
29077c478bd9Sstevel@tonic-gate }
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate /*
29107c478bd9Sstevel@tonic-gate  * Print the status of the interface in a format that can be used to
29117c478bd9Sstevel@tonic-gate  * reconfigure the interface later. Code stolen from status() above.
29127c478bd9Sstevel@tonic-gate  */
29137c478bd9Sstevel@tonic-gate /* ARGSUSED */
29147c478bd9Sstevel@tonic-gate static int
29157c478bd9Sstevel@tonic-gate configinfo(char *null, int64_t param)
29167c478bd9Sstevel@tonic-gate {
29177c478bd9Sstevel@tonic-gate 	struct afswtch *p = afp;
29187c478bd9Sstevel@tonic-gate 	uint64_t flags;
29197c478bd9Sstevel@tonic-gate 	char phydevname[LIFNAMSIZ];
29207c478bd9Sstevel@tonic-gate 	char if_usesrc_name[LIFNAMSIZ];
29217c478bd9Sstevel@tonic-gate 	char *cp;
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
29247c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
29257c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
29267c478bd9Sstevel@tonic-gate 	}
29277c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate 	if (debug) {
29307c478bd9Sstevel@tonic-gate 		(void) printf("configinfo: name %s flags  0x%llx af_af %d\n",
29317c478bd9Sstevel@tonic-gate 		    name, flags, p != NULL ? p->af_af : -1);
29327c478bd9Sstevel@tonic-gate 	}
29337c478bd9Sstevel@tonic-gate 
29347c478bd9Sstevel@tonic-gate 	/* remove LIF component */
29357c478bd9Sstevel@tonic-gate 	(void) strncpy(phydevname, name, sizeof (phydevname));
29367c478bd9Sstevel@tonic-gate 	cp = strchr(phydevname, ':');
29377c478bd9Sstevel@tonic-gate 	if (cp) {
29387c478bd9Sstevel@tonic-gate 		*cp = 0;
29397c478bd9Sstevel@tonic-gate 	}
29407c478bd9Sstevel@tonic-gate 	phydevname[sizeof (phydevname) - 1] = '\0';
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 	/*
29437c478bd9Sstevel@tonic-gate 	 * if the interface is IPv4
29447c478bd9Sstevel@tonic-gate 	 *	if we have a IPv6 address family restriction return
29457c478bd9Sstevel@tonic-gate 	 *		so it won't print
29467c478bd9Sstevel@tonic-gate 	 *	if we are in IPv4 compatibility mode, clear out IFF_IPV4
29477c478bd9Sstevel@tonic-gate 	 *		so we don't print it.
29487c478bd9Sstevel@tonic-gate 	 */
29497c478bd9Sstevel@tonic-gate 	if (flags & IFF_IPV4) {
29507c478bd9Sstevel@tonic-gate 		if (p && p->af_af == AF_INET6)
29517c478bd9Sstevel@tonic-gate 			return (-1);
29527c478bd9Sstevel@tonic-gate 		if (v4compat)
29537c478bd9Sstevel@tonic-gate 			flags &= ~IFF_IPV4;
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 		(void) printf("%s inet plumb", phydevname);
29567c478bd9Sstevel@tonic-gate 	} else if (flags & IFF_IPV6) {
29577c478bd9Sstevel@tonic-gate 		/*
29587c478bd9Sstevel@tonic-gate 		 * else if the interface is IPv6
29597c478bd9Sstevel@tonic-gate 		 *	if we have a IPv4 address family restriction return
29607c478bd9Sstevel@tonic-gate 		 *	or we are in IPv4 compatibiltiy mode, return.
29617c478bd9Sstevel@tonic-gate 		 */
29627c478bd9Sstevel@tonic-gate 		if (p && p->af_af == AF_INET)
29637c478bd9Sstevel@tonic-gate 			return (-1);
29647c478bd9Sstevel@tonic-gate 		if (v4compat)
29657c478bd9Sstevel@tonic-gate 			return (-1);
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate 		(void) printf("%s inet6 plumb", phydevname);
29687c478bd9Sstevel@tonic-gate 	}
29697c478bd9Sstevel@tonic-gate 
29707c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
29717c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
29727c478bd9Sstevel@tonic-gate 		Perror0_exit("configinfo: SIOCGLIFMETRIC");
29737c478bd9Sstevel@tonic-gate 	} else {
29747c478bd9Sstevel@tonic-gate 		if (lifr.lifr_metric)
29757c478bd9Sstevel@tonic-gate 			(void) printf(" metric %d ", lifr.lifr_metric);
29767c478bd9Sstevel@tonic-gate 	}
29777c478bd9Sstevel@tonic-gate 	if (((flags & (IFF_VIRTUAL|IFF_LOOPBACK)) != IFF_VIRTUAL) &&
29787c478bd9Sstevel@tonic-gate 		ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
29797c478bd9Sstevel@tonic-gate 		(void) printf(" mtu %d", lifr.lifr_metric);
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 	/* don't print index when in compatibility mode */
29827c478bd9Sstevel@tonic-gate 	if (!v4compat) {
29837c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
29847c478bd9Sstevel@tonic-gate 			(void) printf(" index %d", lifr.lifr_index);
29857c478bd9Sstevel@tonic-gate 	}
29867c478bd9Sstevel@tonic-gate 
29877c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
29887c478bd9Sstevel@tonic-gate 		if (lifr.lifr_index != 0) {
29897c478bd9Sstevel@tonic-gate 			if (if_indextoname(lifr.lifr_index,
29907c478bd9Sstevel@tonic-gate 			    if_usesrc_name) != NULL) {
29917c478bd9Sstevel@tonic-gate 				(void) printf(" usesrc %s", if_usesrc_name);
29927c478bd9Sstevel@tonic-gate 			}
29937c478bd9Sstevel@tonic-gate 		}
29947c478bd9Sstevel@tonic-gate 	}
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	if (p != NULL) {
29977c478bd9Sstevel@tonic-gate 		(*p->af_configinfo)(1, flags);
29987c478bd9Sstevel@tonic-gate 	} else {
29997c478bd9Sstevel@tonic-gate 		for (p = afs; p->af_name; p++) {
30007c478bd9Sstevel@tonic-gate 			(void) close(s);
30017c478bd9Sstevel@tonic-gate 			s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0);
30027c478bd9Sstevel@tonic-gate 			/* set global af for use in p->af_configinfo */
30037c478bd9Sstevel@tonic-gate 			af = p->af_af;
30047c478bd9Sstevel@tonic-gate 			if (s == -1) {
30057c478bd9Sstevel@tonic-gate 				Perror0_exit("socket");
30067c478bd9Sstevel@tonic-gate 			}
30077c478bd9Sstevel@tonic-gate 			(*p->af_configinfo)(0, flags);
30087c478bd9Sstevel@tonic-gate 		}
30097c478bd9Sstevel@tonic-gate 	}
30107c478bd9Sstevel@tonic-gate 
30117c478bd9Sstevel@tonic-gate 	(void) printf("\n");
30127c478bd9Sstevel@tonic-gate 
30137c478bd9Sstevel@tonic-gate 	return (0);
30147c478bd9Sstevel@tonic-gate }
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate static void
30177c478bd9Sstevel@tonic-gate print_tsec(struct iftun_req *tparams)
30187c478bd9Sstevel@tonic-gate {
30197c478bd9Sstevel@tonic-gate 	ipsec_req_t *ipsr;
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate 	(void) printf("\ttunnel security settings  ");
30227c478bd9Sstevel@tonic-gate 	/*
30237c478bd9Sstevel@tonic-gate 	 * Deal with versioning, for now just point
30247c478bd9Sstevel@tonic-gate 	 * an ipsec_req_t at ifta_secinfo.  If versions
30257c478bd9Sstevel@tonic-gate 	 * change, something else will overlay ifta_secinfo.
30267c478bd9Sstevel@tonic-gate 	 */
30277c478bd9Sstevel@tonic-gate 	assert(tparams->ifta_vers == IFTUN_VERSION);
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate 	ipsr = (ipsec_req_t *)(&tparams->ifta_secinfo);
30307c478bd9Sstevel@tonic-gate 	if (ipsr->ipsr_ah_req & IPSEC_PREF_REQUIRED) {
30317c478bd9Sstevel@tonic-gate 		(void) printf("ah (%s)  ",
30327c478bd9Sstevel@tonic-gate 		    rparsealg(ipsr->ipsr_auth_alg, IPSEC_PROTO_AH));
30337c478bd9Sstevel@tonic-gate 	}
30347c478bd9Sstevel@tonic-gate 	if (ipsr->ipsr_esp_req & IPSEC_PREF_REQUIRED) {
30357c478bd9Sstevel@tonic-gate 		(void) printf("esp (%s",
30367c478bd9Sstevel@tonic-gate 		    rparsealg(ipsr->ipsr_esp_alg, IPSEC_PROTO_ESP));
30377c478bd9Sstevel@tonic-gate 		(void) printf("/%s)",
30387c478bd9Sstevel@tonic-gate 		    rparsealg(ipsr->ipsr_esp_auth_alg, IPSEC_PROTO_AH));
30397c478bd9Sstevel@tonic-gate 	}
30407c478bd9Sstevel@tonic-gate 	(void) printf("\n");
30417c478bd9Sstevel@tonic-gate }
30427c478bd9Sstevel@tonic-gate 
30437c478bd9Sstevel@tonic-gate static void
30447c478bd9Sstevel@tonic-gate tun_status(void)
30457c478bd9Sstevel@tonic-gate {
30467c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
30477c478bd9Sstevel@tonic-gate 	int rc;
30487c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
30497c478bd9Sstevel@tonic-gate 	int protocol;
30507c478bd9Sstevel@tonic-gate 	char srcbuf[INET6_ADDRSTRLEN];
30517c478bd9Sstevel@tonic-gate 	char dstbuf[INET6_ADDRSTRLEN];
30527c478bd9Sstevel@tonic-gate 	boolean_t tabbed;
30537c478bd9Sstevel@tonic-gate 	uint8_t hoplimit;
30547c478bd9Sstevel@tonic-gate 	int16_t encaplimit;
30557c478bd9Sstevel@tonic-gate 	struct sockaddr_storage taddr;
30567c478bd9Sstevel@tonic-gate 	socklen_t socklen = sizeof (taddr);
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
30597c478bd9Sstevel@tonic-gate 	interface.if_protocol = SOCKET_AF(af);
30607c478bd9Sstevel@tonic-gate 	if ((rc = icfg_open(&handle, &interface)) != ICFG_SUCCESS)
30617c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
30627c478bd9Sstevel@tonic-gate 
30637c478bd9Sstevel@tonic-gate 	/*
30647c478bd9Sstevel@tonic-gate 	 * only print tunnel info for lun 0.  If ioctl fails, assume
30657c478bd9Sstevel@tonic-gate 	 * we are not a tunnel
30667c478bd9Sstevel@tonic-gate 	 */
30677c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL ||
30687c478bd9Sstevel@tonic-gate 	    icfg_get_tunnel_lower(handle, &protocol) != ICFG_SUCCESS) {
30697c478bd9Sstevel@tonic-gate 		icfg_close(handle);
30707c478bd9Sstevel@tonic-gate 		return;
30717c478bd9Sstevel@tonic-gate 	}
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 	switch (protocol) {
30747c478bd9Sstevel@tonic-gate 	case AF_INET:
30757c478bd9Sstevel@tonic-gate 		(void) printf("\tinet");
30767c478bd9Sstevel@tonic-gate 		break;
30777c478bd9Sstevel@tonic-gate 	case AF_INET6:
30787c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6");
30797c478bd9Sstevel@tonic-gate 		break;
30807c478bd9Sstevel@tonic-gate 	default:
30817c478bd9Sstevel@tonic-gate 		Perror0_exit("\ttunnel: Illegal lower stream\n\t");
30827c478bd9Sstevel@tonic-gate 		break;
30837c478bd9Sstevel@tonic-gate 	}
30847c478bd9Sstevel@tonic-gate 
30857c478bd9Sstevel@tonic-gate 	rc = icfg_get_tunnel_src(handle, (struct sockaddr *)&taddr, &socklen);
30867c478bd9Sstevel@tonic-gate 	if (rc == ICFG_NOT_SET) {
30877c478bd9Sstevel@tonic-gate 		(void) strlcpy(srcbuf, (protocol == AF_INET) ? "0.0.0.0" :
30887c478bd9Sstevel@tonic-gate 		    "::", sizeof (srcbuf));
30897c478bd9Sstevel@tonic-gate 	} else if (rc != ICFG_SUCCESS) {
30907c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
30917c478bd9Sstevel@tonic-gate 	} else {
30927c478bd9Sstevel@tonic-gate 		rc = icfg_sockaddr_to_str(protocol, (struct sockaddr *)&taddr,
30937c478bd9Sstevel@tonic-gate 		    srcbuf, sizeof (srcbuf));
30947c478bd9Sstevel@tonic-gate 		if (rc != ICFG_SUCCESS) {
30957c478bd9Sstevel@tonic-gate 			Perror0_exit((char *)icfg_errmsg(rc));
30967c478bd9Sstevel@tonic-gate 		}
30977c478bd9Sstevel@tonic-gate 	}
30987c478bd9Sstevel@tonic-gate 
30997c478bd9Sstevel@tonic-gate 	(void) printf(" tunnel src %s ", srcbuf);
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 	rc = icfg_get_tunnel_dest(handle, (struct sockaddr *)&taddr, &socklen);
31027c478bd9Sstevel@tonic-gate 	if (rc == ICFG_NOT_SET) {
31037c478bd9Sstevel@tonic-gate 		(void) printf("\n");
31047c478bd9Sstevel@tonic-gate 	} else {
31057c478bd9Sstevel@tonic-gate 		rc = icfg_sockaddr_to_str(protocol, (struct sockaddr *)&taddr,
31067c478bd9Sstevel@tonic-gate 		    dstbuf, sizeof (dstbuf));
31077c478bd9Sstevel@tonic-gate 		if (rc != ICFG_SUCCESS) {
31087c478bd9Sstevel@tonic-gate 			Perror0_exit((char *)icfg_errmsg(rc));
31097c478bd9Sstevel@tonic-gate 		}
31107c478bd9Sstevel@tonic-gate 		(void) printf("tunnel dst %s\n", dstbuf);
31117c478bd9Sstevel@tonic-gate 	}
31127c478bd9Sstevel@tonic-gate 
31137c478bd9Sstevel@tonic-gate 	if (handle->ifh_tunnel_params != NULL &&
31147c478bd9Sstevel@tonic-gate 	    (handle->ifh_tunnel_params->ifta_flags & IFTUN_SECURITY))
31157c478bd9Sstevel@tonic-gate 		print_tsec(handle->ifh_tunnel_params);
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate 	/*
31187c478bd9Sstevel@tonic-gate 	 * tabbed indicates tabbed and printed.  Use it tell us whether
31197c478bd9Sstevel@tonic-gate 	 * to tab and that we've printed something here, so we need a
31207c478bd9Sstevel@tonic-gate 	 * newline
31217c478bd9Sstevel@tonic-gate 	 */
31227c478bd9Sstevel@tonic-gate 	tabbed = _B_FALSE;
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate 	if (icfg_get_tunnel_hoplimit(handle, &hoplimit) == ICFG_SUCCESS) {
31257c478bd9Sstevel@tonic-gate 		(void) printf("\ttunnel hop limit %d ", hoplimit);
31267c478bd9Sstevel@tonic-gate 		tabbed = _B_TRUE;
31277c478bd9Sstevel@tonic-gate 	}
31287c478bd9Sstevel@tonic-gate 
31297c478bd9Sstevel@tonic-gate 	if ((protocol == AF_INET6) &&
31307c478bd9Sstevel@tonic-gate 	    (icfg_get_tunnel_encaplimit(handle, &encaplimit) ==
31317c478bd9Sstevel@tonic-gate 		ICFG_SUCCESS)) {
31327c478bd9Sstevel@tonic-gate 		if (!tabbed) {
31337c478bd9Sstevel@tonic-gate 			(void) printf("\t");
31347c478bd9Sstevel@tonic-gate 			tabbed = _B_TRUE;
31357c478bd9Sstevel@tonic-gate 		}
31367c478bd9Sstevel@tonic-gate 		if (encaplimit >= 0) {
31377c478bd9Sstevel@tonic-gate 			(void) printf("tunnel encapsulation limit %d",
31387c478bd9Sstevel@tonic-gate 			    encaplimit);
31397c478bd9Sstevel@tonic-gate 		} else {
31407c478bd9Sstevel@tonic-gate 			(void) printf("tunnel encapsulation limit disabled");
31417c478bd9Sstevel@tonic-gate 		}
31427c478bd9Sstevel@tonic-gate 	}
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate 	if (tabbed)
31457c478bd9Sstevel@tonic-gate 		(void) printf("\n");
31467c478bd9Sstevel@tonic-gate 
31477c478bd9Sstevel@tonic-gate 	icfg_close(handle);
31487c478bd9Sstevel@tonic-gate }
31497c478bd9Sstevel@tonic-gate 
31507c478bd9Sstevel@tonic-gate static void
31517c478bd9Sstevel@tonic-gate in_status(int force, uint64_t flags)
31527c478bd9Sstevel@tonic-gate {
31537c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin, *laddr;
31547c478bd9Sstevel@tonic-gate 	struct	sockaddr_in netmask = { AF_INET };
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 	if (debug)
31577c478bd9Sstevel@tonic-gate 		(void) printf("in_status(%s) flags 0x%llx\n", name, flags);
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate 	/* only print status for IPv4 interfaces */
31607c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV4))
31617c478bd9Sstevel@tonic-gate 		return;
31627c478bd9Sstevel@tonic-gate 
31637c478bd9Sstevel@tonic-gate 	/* if the interface is a tunnel, print the tunnel status */
31647c478bd9Sstevel@tonic-gate 	tun_status();
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
31677c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
31687c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
31697c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
31707c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
31717c478bd9Sstevel@tonic-gate 				if (!force)
31727c478bd9Sstevel@tonic-gate 					return;
31737c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
31747c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
31757c478bd9Sstevel@tonic-gate 			} else
31767c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status: SIOCGLIFADDR");
31777c478bd9Sstevel@tonic-gate 		}
31787c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
31797c478bd9Sstevel@tonic-gate 		(void) printf("\tinet %s ", inet_ntoa(sin->sin_addr));
31807c478bd9Sstevel@tonic-gate 		laddr = sin;
31817c478bd9Sstevel@tonic-gate 	} else {
31827c478bd9Sstevel@tonic-gate 		(void) printf("\tinet ");
31837c478bd9Sstevel@tonic-gate 	}
31847c478bd9Sstevel@tonic-gate 
31857c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
31867c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
31877c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
31887c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
31897c478bd9Sstevel@tonic-gate 			if (!force)
31907c478bd9Sstevel@tonic-gate 				return;
31917c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
31927c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
31937c478bd9Sstevel@tonic-gate 		} else {
31947c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status: SIOCGLIFSUBNET");
31957c478bd9Sstevel@tonic-gate 		}
31967c478bd9Sstevel@tonic-gate 	}
31977c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
31987c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
31997c478bd9Sstevel@tonic-gate 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
32007c478bd9Sstevel@tonic-gate 		(void) printf("subnet %s/%d ", inet_ntoa(sin->sin_addr),
32017c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
32027c478bd9Sstevel@tonic-gate 	}
32037c478bd9Sstevel@tonic-gate 	if (sin->sin_family != AF_INET) {
32047c478bd9Sstevel@tonic-gate 		(void) printf("Wrong family: %d\n", sin->sin_family);
32057c478bd9Sstevel@tonic-gate 	}
32067c478bd9Sstevel@tonic-gate 
32077c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32087c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
32097c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL)
32107c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status: SIOCGLIFNETMASK");
32117c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
32127c478bd9Sstevel@tonic-gate 	} else
32137c478bd9Sstevel@tonic-gate 		netmask.sin_addr =
32147c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
32157c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
32167c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32177c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
32187c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
32197c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
32207c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
32217c478bd9Sstevel@tonic-gate 			else
32227c478bd9Sstevel@tonic-gate 			    Perror0_exit("in_status: SIOCGLIFDSTADDR");
32237c478bd9Sstevel@tonic-gate 		}
32247c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
32257c478bd9Sstevel@tonic-gate 		(void) printf("--> %s ", inet_ntoa(sin->sin_addr));
32267c478bd9Sstevel@tonic-gate 	}
32277c478bd9Sstevel@tonic-gate 	(void) printf("netmask %x ", ntohl(netmask.sin_addr.s_addr));
32287c478bd9Sstevel@tonic-gate 	if (flags & IFF_BROADCAST) {
32297c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32307c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
32317c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
32327c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
32337c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
32347c478bd9Sstevel@tonic-gate 			else
32357c478bd9Sstevel@tonic-gate 			    Perror0_exit("in_status: SIOCGLIFBRDADDR");
32367c478bd9Sstevel@tonic-gate 		}
32377c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
32387c478bd9Sstevel@tonic-gate 		if (sin->sin_addr.s_addr != 0) {
32397c478bd9Sstevel@tonic-gate 			(void) printf("broadcast %s",
32407c478bd9Sstevel@tonic-gate 			    inet_ntoa(sin->sin_addr));
32417c478bd9Sstevel@tonic-gate 		}
32427c478bd9Sstevel@tonic-gate 	}
32437c478bd9Sstevel@tonic-gate 	/* If there is a groupname, print it for lun 0 alone */
32447c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') == NULL) {
32457c478bd9Sstevel@tonic-gate 		(void) memset(lifr.lifr_groupname, 0,
32467c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_groupname));
32477c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr) >= 0) {
32487c478bd9Sstevel@tonic-gate 			if (strlen(lifr.lifr_groupname) > 0) {
32497c478bd9Sstevel@tonic-gate 				(void) printf("\n\tgroupname %s",
32507c478bd9Sstevel@tonic-gate 				    lifr.lifr_groupname);
32517c478bd9Sstevel@tonic-gate 			}
32527c478bd9Sstevel@tonic-gate 		}
32537c478bd9Sstevel@tonic-gate 	}
32547c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
32557c478bd9Sstevel@tonic-gate }
32567c478bd9Sstevel@tonic-gate 
32577c478bd9Sstevel@tonic-gate static void
32587c478bd9Sstevel@tonic-gate in6_status(int force, uint64_t flags)
32597c478bd9Sstevel@tonic-gate {
32607c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
32617c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6, *laddr6;
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate 	if (debug)
32647c478bd9Sstevel@tonic-gate 		(void) printf("in6_status(%s) flags 0x%llx\n", name, flags);
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV6))
32677c478bd9Sstevel@tonic-gate 		return;
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 	/* if the interface is a tunnel, print the tunnel status */
32707c478bd9Sstevel@tonic-gate 	tun_status();
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
32737c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32747c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
32757c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
32767c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
32777c478bd9Sstevel@tonic-gate 				if (!force)
32787c478bd9Sstevel@tonic-gate 					return;
32797c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
32807c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
32817c478bd9Sstevel@tonic-gate 			} else
32827c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status6: SIOCGLIFADDR");
32837c478bd9Sstevel@tonic-gate 		}
32847c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
32857c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6 %s/%d ",
32867c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
32877c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
32887c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
32897c478bd9Sstevel@tonic-gate 		laddr6 = sin6;
32907c478bd9Sstevel@tonic-gate 	} else {
32917c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6 ");
32927c478bd9Sstevel@tonic-gate 	}
32937c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32947c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
32957c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
32967c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
32977c478bd9Sstevel@tonic-gate 			if (!force)
32987c478bd9Sstevel@tonic-gate 				return;
32997c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
33007c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
33017c478bd9Sstevel@tonic-gate 		} else
33027c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status6: SIOCGLIFSUBNET");
33037c478bd9Sstevel@tonic-gate 	}
33047c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
33057c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
33067c478bd9Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
33077c478bd9Sstevel@tonic-gate 		(void) printf("subnet %s/%d ",
33087c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33097c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
33107c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
33117c478bd9Sstevel@tonic-gate 	}
33127c478bd9Sstevel@tonic-gate 	if (sin6->sin6_family != AF_INET6) {
33137c478bd9Sstevel@tonic-gate 		(void) printf("Wrong family: %d\n", sin6->sin6_family);
33147c478bd9Sstevel@tonic-gate 	}
33157c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
33167c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33177c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
33187c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
33197c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33207c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33217c478bd9Sstevel@tonic-gate 			else
33227c478bd9Sstevel@tonic-gate 			    Perror0_exit("in_status6: SIOCGLIFDSTADDR");
33237c478bd9Sstevel@tonic-gate 		}
33247c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
33257c478bd9Sstevel@tonic-gate 		(void) printf("--> %s ",
33267c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33277c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)));
33287c478bd9Sstevel@tonic-gate 	}
33297c478bd9Sstevel@tonic-gate 	if (verbose) {
33307c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
33317c478bd9Sstevel@tonic-gate 		(void) putchar('\t');
33327c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33337c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
33347c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EINVAL)
33357c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33367c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33377c478bd9Sstevel@tonic-gate 			else
33387c478bd9Sstevel@tonic-gate 			    Perror0_exit("in_status6: SIOCGLIFTOKEN");
33397c478bd9Sstevel@tonic-gate 		} else {
33407c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
33417c478bd9Sstevel@tonic-gate 			(void) printf("token %s/%d ",
33427c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33437c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
33447c478bd9Sstevel@tonic-gate 			    lifr.lifr_addrlen);
33457c478bd9Sstevel@tonic-gate 		}
33467c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFLNKINFO, (caddr_t)&lifr) < 0) {
33477c478bd9Sstevel@tonic-gate 			if (errno != EINVAL) {
33487c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status6: SIOCGLIFLNKINFO");
33497c478bd9Sstevel@tonic-gate 			}
33507c478bd9Sstevel@tonic-gate 		} else {
33517c478bd9Sstevel@tonic-gate 			(void) printf("maxhops %u, reachtime %u ms, "
33527c478bd9Sstevel@tonic-gate 			    "reachretrans %u ms, maxmtu %u ",
33537c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_maxhops,
33547c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_reachtime,
33557c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_reachretrans,
33567c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_maxmtu);
33577c478bd9Sstevel@tonic-gate 		}
33587c478bd9Sstevel@tonic-gate 	}
33597c478bd9Sstevel@tonic-gate 	/* If there is a groupname, print it for lun 0 alone */
33607c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') == NULL) {
33617c478bd9Sstevel@tonic-gate 		(void) memset(lifr.lifr_groupname, 0,
33627c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_groupname));
33637c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr) >= 0) {
33647c478bd9Sstevel@tonic-gate 			if (strlen(lifr.lifr_groupname) > 0) {
33657c478bd9Sstevel@tonic-gate 				(void) printf("\n\tgroupname %s",
33667c478bd9Sstevel@tonic-gate 				    lifr.lifr_groupname);
33677c478bd9Sstevel@tonic-gate 			}
33687c478bd9Sstevel@tonic-gate 		}
33697c478bd9Sstevel@tonic-gate 	}
33707c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
33717c478bd9Sstevel@tonic-gate }
33727c478bd9Sstevel@tonic-gate 
33737c478bd9Sstevel@tonic-gate static void
33747c478bd9Sstevel@tonic-gate in_configinfo(int force, uint64_t flags)
33757c478bd9Sstevel@tonic-gate {
33767c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin, *laddr;
33777c478bd9Sstevel@tonic-gate 	struct	sockaddr_in netmask = { AF_INET };
33787c478bd9Sstevel@tonic-gate 
33797c478bd9Sstevel@tonic-gate 	if (debug)
33807c478bd9Sstevel@tonic-gate 		(void) printf("in_configinfo(%s) flags 0x%llx\n", name, flags);
33817c478bd9Sstevel@tonic-gate 
33827c478bd9Sstevel@tonic-gate 	/* only configinfo info for IPv4 interfaces */
33837c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV4))
33847c478bd9Sstevel@tonic-gate 		return;
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
33877c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33887c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
33897c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
33907c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
33917c478bd9Sstevel@tonic-gate 				if (!force)
33927c478bd9Sstevel@tonic-gate 					return;
33937c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33947c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33957c478bd9Sstevel@tonic-gate 			} else
33967c478bd9Sstevel@tonic-gate 				Perror0_exit("in_configinfo: SIOCGLIFADDR");
33977c478bd9Sstevel@tonic-gate 		}
33987c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
33997c478bd9Sstevel@tonic-gate 		if (get_lun(name) != 0) {
34007c478bd9Sstevel@tonic-gate 			(void) printf(" addif %s ", inet_ntoa(sin->sin_addr));
34017c478bd9Sstevel@tonic-gate 		} else {
34027c478bd9Sstevel@tonic-gate 			(void) printf(" set %s ", inet_ntoa(sin->sin_addr));
34037c478bd9Sstevel@tonic-gate 		}
34047c478bd9Sstevel@tonic-gate 		laddr = sin;
34057c478bd9Sstevel@tonic-gate 	}
34067c478bd9Sstevel@tonic-gate 
34077c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34087c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
34097c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
34107c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
34117c478bd9Sstevel@tonic-gate 			if (!force)
34127c478bd9Sstevel@tonic-gate 				return;
34137c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
34147c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
34157c478bd9Sstevel@tonic-gate 		} else {
34167c478bd9Sstevel@tonic-gate 			Perror0_exit("in_configinfo: SIOCGLIFSUBNET");
34177c478bd9Sstevel@tonic-gate 		}
34187c478bd9Sstevel@tonic-gate 	}
34197c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
34207c478bd9Sstevel@tonic-gate 
34217c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
34227c478bd9Sstevel@tonic-gate 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
34237c478bd9Sstevel@tonic-gate 		(void) printf(" subnet %s/%d ", inet_ntoa(sin->sin_addr),
34247c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
34257c478bd9Sstevel@tonic-gate 	}
34267c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34277c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
34287c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL)
34297c478bd9Sstevel@tonic-gate 			Perror0_exit("in_configinfo: SIOCGLIFNETMASK");
34307c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
34317c478bd9Sstevel@tonic-gate 	} else
34327c478bd9Sstevel@tonic-gate 		netmask.sin_addr =
34337c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
34347c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
34357c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34367c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
34377c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
34387c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34397c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34407c478bd9Sstevel@tonic-gate 			else
34417c478bd9Sstevel@tonic-gate 			    Perror0_exit("in_configinfo: SIOCGLIFDSTADDR");
34427c478bd9Sstevel@tonic-gate 		}
34437c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
34447c478bd9Sstevel@tonic-gate 		(void) printf(" destination %s ", inet_ntoa(sin->sin_addr));
34457c478bd9Sstevel@tonic-gate 	}
34467c478bd9Sstevel@tonic-gate 	(void) printf(" netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
34477c478bd9Sstevel@tonic-gate 	if (flags & IFF_BROADCAST) {
34487c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34497c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
34507c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
34517c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34527c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34537c478bd9Sstevel@tonic-gate 			else
34547c478bd9Sstevel@tonic-gate 			    Perror0_exit("in_configinfo: SIOCGLIFBRDADDR");
34557c478bd9Sstevel@tonic-gate 		}
34567c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
34577c478bd9Sstevel@tonic-gate 		if (sin->sin_addr.s_addr != 0) {
34587c478bd9Sstevel@tonic-gate 			(void) printf(" broadcast %s ",
34597c478bd9Sstevel@tonic-gate 			    inet_ntoa(sin->sin_addr));
34607c478bd9Sstevel@tonic-gate 		}
34617c478bd9Sstevel@tonic-gate 	}
34627c478bd9Sstevel@tonic-gate 
34637c478bd9Sstevel@tonic-gate 	/* If there is a groupname, print it for lun 0 alone */
34647c478bd9Sstevel@tonic-gate 	if (get_lun(name) == 0) {
34657c478bd9Sstevel@tonic-gate 		(void) memset(lifr.lifr_groupname, 0,
34667c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_groupname));
34677c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr) >= 0) {
34687c478bd9Sstevel@tonic-gate 			if (strlen(lifr.lifr_groupname) > 0) {
34697c478bd9Sstevel@tonic-gate 				(void) printf(" group %s ",
34707c478bd9Sstevel@tonic-gate 				    lifr.lifr_groupname);
34717c478bd9Sstevel@tonic-gate 			}
34727c478bd9Sstevel@tonic-gate 		}
34737c478bd9Sstevel@tonic-gate 	}
34747c478bd9Sstevel@tonic-gate 
34757c478bd9Sstevel@tonic-gate 	/* Print flags to configure */
34767c478bd9Sstevel@tonic-gate 	print_config_flags(flags);
34777c478bd9Sstevel@tonic-gate 
34787c478bd9Sstevel@tonic-gate 	/* IFF_NOARP applies to AF_INET only */
34797c478bd9Sstevel@tonic-gate 	if (flags & IFF_NOARP) {
34807c478bd9Sstevel@tonic-gate 		(void) printf("-arp ");
34817c478bd9Sstevel@tonic-gate 	}
34827c478bd9Sstevel@tonic-gate }
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate static void
34857c478bd9Sstevel@tonic-gate in6_configinfo(int force, uint64_t flags)
34867c478bd9Sstevel@tonic-gate {
34877c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
34887c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6, *laddr6;
34897c478bd9Sstevel@tonic-gate 
34907c478bd9Sstevel@tonic-gate 	if (debug)
34917c478bd9Sstevel@tonic-gate 		(void) printf("in6_configinfo(%s) flags 0x%llx\n", name,
34927c478bd9Sstevel@tonic-gate 		    flags);
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV6))
34957c478bd9Sstevel@tonic-gate 		return;
34967c478bd9Sstevel@tonic-gate 
34977c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
34987c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34997c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
35007c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
35017c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
35027c478bd9Sstevel@tonic-gate 				if (!force)
35037c478bd9Sstevel@tonic-gate 					return;
35047c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
35057c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
35067c478bd9Sstevel@tonic-gate 			} else
35077c478bd9Sstevel@tonic-gate 				Perror0_exit("in6_configinfo: SIOCGLIFADDR");
35087c478bd9Sstevel@tonic-gate 		}
35097c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
35107c478bd9Sstevel@tonic-gate 		if (get_lun(name) != 0) {
35117c478bd9Sstevel@tonic-gate 			(void) printf(" addif %s/%d ",
35127c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35137c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
35147c478bd9Sstevel@tonic-gate 			    lifr.lifr_addrlen);
35157c478bd9Sstevel@tonic-gate 		} else {
35167c478bd9Sstevel@tonic-gate 			(void) printf(" set %s/%d ",
35177c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35187c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
35197c478bd9Sstevel@tonic-gate 			    lifr.lifr_addrlen);
35207c478bd9Sstevel@tonic-gate 		}
35217c478bd9Sstevel@tonic-gate 		laddr6 = sin6;
35227c478bd9Sstevel@tonic-gate 	}
35237c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35247c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
35257c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
35267c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
35277c478bd9Sstevel@tonic-gate 			if (!force)
35287c478bd9Sstevel@tonic-gate 				return;
35297c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
35307c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
35317c478bd9Sstevel@tonic-gate 		} else
35327c478bd9Sstevel@tonic-gate 			Perror0_exit("in6_configinfo: SIOCGLIFSUBNET");
35337c478bd9Sstevel@tonic-gate 	}
35347c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
35357c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
35367c478bd9Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
35377c478bd9Sstevel@tonic-gate 		(void) printf(" subnet %s/%d ",
35387c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35397c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
35407c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
35417c478bd9Sstevel@tonic-gate 	}
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
35447c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35457c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
35467c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
35477c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
35487c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
35497c478bd9Sstevel@tonic-gate 			else
35507c478bd9Sstevel@tonic-gate 			    Perror0_exit("in6_configinfo: SIOCGLIFDSTADDR");
35517c478bd9Sstevel@tonic-gate 		}
35527c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
35537c478bd9Sstevel@tonic-gate 		(void) printf(" destination %s ",
35547c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35557c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)));
35567c478bd9Sstevel@tonic-gate 	}
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35597c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
35607c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EINVAL)
35617c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
35627c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
35637c478bd9Sstevel@tonic-gate 		else
35647c478bd9Sstevel@tonic-gate 		    Perror0_exit("in6_configinfo: SIOCGLIFTOKEN");
35657c478bd9Sstevel@tonic-gate 	} else {
35667c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
35677c478bd9Sstevel@tonic-gate 		(void) printf(" token %s/%d ",
35687c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35697c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
35707c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
35717c478bd9Sstevel@tonic-gate 	}
35727c478bd9Sstevel@tonic-gate 
35737c478bd9Sstevel@tonic-gate 	/* If there is a groupname, print it for lun 0 alone */
35747c478bd9Sstevel@tonic-gate 	if (get_lun(name) == 0) {
35757c478bd9Sstevel@tonic-gate 		(void) memset(lifr.lifr_groupname, 0,
35767c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_groupname));
35777c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr) >= 0) {
35787c478bd9Sstevel@tonic-gate 			if (strlen(lifr.lifr_groupname) > 0) {
35797c478bd9Sstevel@tonic-gate 				(void) printf(" group %s ",
35807c478bd9Sstevel@tonic-gate 				    lifr.lifr_groupname);
35817c478bd9Sstevel@tonic-gate 			}
35827c478bd9Sstevel@tonic-gate 		}
35837c478bd9Sstevel@tonic-gate 	}
35847c478bd9Sstevel@tonic-gate 
35857c478bd9Sstevel@tonic-gate 	/* Print flags to configure */
35867c478bd9Sstevel@tonic-gate 	print_config_flags(flags);
35877c478bd9Sstevel@tonic-gate 
35887c478bd9Sstevel@tonic-gate 	/* IFF_NONUD applies to AF_INET6 only */
35897c478bd9Sstevel@tonic-gate 	if (flags & IFF_NONUD) {
35907c478bd9Sstevel@tonic-gate 		(void) printf("-nud ");
35917c478bd9Sstevel@tonic-gate 	}
35927c478bd9Sstevel@tonic-gate }
35937c478bd9Sstevel@tonic-gate 
35947c478bd9Sstevel@tonic-gate /* ARGSUSED */
35957c478bd9Sstevel@tonic-gate static int
35967c478bd9Sstevel@tonic-gate get_lun(char *rsrc)
35977c478bd9Sstevel@tonic-gate {
35987c478bd9Sstevel@tonic-gate 	char resource[LIFNAMSIZ];
35997c478bd9Sstevel@tonic-gate 	char *cp;
36007c478bd9Sstevel@tonic-gate 
36017c478bd9Sstevel@tonic-gate 	(void) strcpy(resource, rsrc);
36027c478bd9Sstevel@tonic-gate 
36037c478bd9Sstevel@tonic-gate 	/* remove LIF component */
36047c478bd9Sstevel@tonic-gate 	cp = strchr(resource, ':');
36057c478bd9Sstevel@tonic-gate 	if (cp) {
36067c478bd9Sstevel@tonic-gate 		cp++;
36077c478bd9Sstevel@tonic-gate 		return (atoi(cp));
36087c478bd9Sstevel@tonic-gate 	}
36097c478bd9Sstevel@tonic-gate 
36107c478bd9Sstevel@tonic-gate 	return (0);
36117c478bd9Sstevel@tonic-gate }
36127c478bd9Sstevel@tonic-gate 
36137c478bd9Sstevel@tonic-gate /*
36147c478bd9Sstevel@tonic-gate  * We need to plink both the arp-device stream and the arp-ip-device stream.
36157c478bd9Sstevel@tonic-gate  * However the muxid is stored only in IP. Plumbing 2 streams individually
36167c478bd9Sstevel@tonic-gate  * is not atomic, and if ifconfig is killed, the resulting plumbing can
36177c478bd9Sstevel@tonic-gate  * be inconsistent. For eg. if only the arp stream is plumbed, we have lost
36187c478bd9Sstevel@tonic-gate  * the muxid, and the half-baked plumbing can neither be unplumbed nor
36197c478bd9Sstevel@tonic-gate  * replumbed, thus requiring a reboot. To avoid the above the following
36207c478bd9Sstevel@tonic-gate  * scheme is used.
36217c478bd9Sstevel@tonic-gate  *
36227c478bd9Sstevel@tonic-gate  * Ifconfig asks IP to enforce atomicity of plumbing the arp and IP streams.
36237c478bd9Sstevel@tonic-gate  * This is done by pushing arp on to the mux (/dev/udp). ARP adds some
36247c478bd9Sstevel@tonic-gate  * extra information in the I_PLINK and I_PUNLINK ioctls to let IP know
36257c478bd9Sstevel@tonic-gate  * that the plumbing/unplumbing has to be done atomically. Ifconfig plumbs
36267c478bd9Sstevel@tonic-gate  * the IP stream first, and unplumbs it last. The kernel (IP) does not
36277c478bd9Sstevel@tonic-gate  * allow IP stream to be unplumbed without unplumbing arp stream. Similarly
36287c478bd9Sstevel@tonic-gate  * it does not allow arp stream to be plumbed before IP stream is plumbed.
36297c478bd9Sstevel@tonic-gate  * There is no need to use SIOCSLIFMUXID, since the whole operation is atomic,
36307c478bd9Sstevel@tonic-gate  * and IP uses the info in the I_PLINK message to get the muxid.
36317c478bd9Sstevel@tonic-gate  *
36327c478bd9Sstevel@tonic-gate  * a. STREAMS does not allow us to use /dev/ip itself as the mux. So we use
36337c478bd9Sstevel@tonic-gate  *    /dev/udp[6].
36347c478bd9Sstevel@tonic-gate  * b. SIOCGLIFMUXID returns the muxid corresponding to the V4 or V6 stream
36357c478bd9Sstevel@tonic-gate  *    depending on the open i.e. V4 vs V6 open. So we need to use /dev/udp
36367c478bd9Sstevel@tonic-gate  *    or /dev/udp6.
36377c478bd9Sstevel@tonic-gate  * c. We need to push ARP in order to get the required kernel support for
36387c478bd9Sstevel@tonic-gate  *    atomic plumbings. The actual work done by ARP is explained in arp.c
36397c478bd9Sstevel@tonic-gate  *    Without pushing ARP, we will still be able to plumb/unplumb. But
36407c478bd9Sstevel@tonic-gate  *    it is not atomic, and is supported by the kernel for backward
36417c478bd9Sstevel@tonic-gate  *    compatibility for other utilities like atmifconfig etc. In this case
36427c478bd9Sstevel@tonic-gate  *    the utility must use SIOCSLIFMUXID.
36437c478bd9Sstevel@tonic-gate  */
36447c478bd9Sstevel@tonic-gate static void
36457c478bd9Sstevel@tonic-gate plumb_one_device(dlpi_if_attr_t *diap, int ip_fd, int af)
36467c478bd9Sstevel@tonic-gate {
36477c478bd9Sstevel@tonic-gate 	int	arp_fd = -1;
36487c478bd9Sstevel@tonic-gate 	int	arp_muxid = -1, ip_muxid;
36497c478bd9Sstevel@tonic-gate 	int	mux_fd;
36507c478bd9Sstevel@tonic-gate 	char	*udp_dev_name;
36517c478bd9Sstevel@tonic-gate 	dlpi_if_attr_t	dia;
36527c478bd9Sstevel@tonic-gate 
36537c478bd9Sstevel@tonic-gate 	if (debug)
36547c478bd9Sstevel@tonic-gate 		(void) printf("plumb_one_device: ifname %s, ppa %d\n",
36557c478bd9Sstevel@tonic-gate 		    diap->ifname, diap->ppa);
36567c478bd9Sstevel@tonic-gate 
36577c478bd9Sstevel@tonic-gate 	if (diap->style == DL_STYLE2 && dlpi_detach(ip_fd, -1) < 0)
36587c478bd9Sstevel@tonic-gate 		Perror0_exit("dlpi_detach");
36597c478bd9Sstevel@tonic-gate 
36607c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, I_PUSH, IP_MOD_NAME) == -1)
36617c478bd9Sstevel@tonic-gate 		Perror2_exit("I_PUSH", IP_MOD_NAME);
36627c478bd9Sstevel@tonic-gate 
36637c478bd9Sstevel@tonic-gate 	/*
36647c478bd9Sstevel@tonic-gate 	 * Push the ARP module onto the interface stream. IP uses
36657c478bd9Sstevel@tonic-gate 	 * this to send resolution requests up to ARP. We need to
36667c478bd9Sstevel@tonic-gate 	 * do this before the SLIFNAME ioctl is sent down because
36677c478bd9Sstevel@tonic-gate 	 * the interface becomes publicly known as soon as the SLIFNAME
36687c478bd9Sstevel@tonic-gate 	 * ioctl completes. Thus some other process trying to bring up
36697c478bd9Sstevel@tonic-gate 	 * the interface after SLIFNAME but before we have pushed ARP
36707c478bd9Sstevel@tonic-gate 	 * could hang. We pop the module again later if it is not needed.
36717c478bd9Sstevel@tonic-gate 	 */
36727c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, I_PUSH, ARP_MOD_NAME) == -1)
36737c478bd9Sstevel@tonic-gate 		Perror2_exit("I_PUSH", ARP_MOD_NAME);
36747c478bd9Sstevel@tonic-gate 
36757c478bd9Sstevel@tonic-gate 	/*
36767c478bd9Sstevel@tonic-gate 	 * Set IFF_IPV4/IFF_IPV6 flags.
36777c478bd9Sstevel@tonic-gate 	 * At this point in time the kernel also allows an
36787c478bd9Sstevel@tonic-gate 	 * override of the CANTCHANGE flags.
36797c478bd9Sstevel@tonic-gate 	 */
36807c478bd9Sstevel@tonic-gate 
36817c478bd9Sstevel@tonic-gate 	lifr.lifr_name[0] = '\0';
36827c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, SIOCGLIFFLAGS, (char *)&lifr) == -1)
36837c478bd9Sstevel@tonic-gate 		Perror0_exit("plumb_one_device: SIOCGLIFFLAGS");
36847c478bd9Sstevel@tonic-gate 
36857c478bd9Sstevel@tonic-gate 	/* Set the name string and the IFF_IPV* flag */
36867c478bd9Sstevel@tonic-gate 	if (af == AF_INET6) {
36877c478bd9Sstevel@tonic-gate 		lifr.lifr_flags |= IFF_IPV6;
36887c478bd9Sstevel@tonic-gate 		lifr.lifr_flags &= ~(IFF_BROADCAST | IFF_IPV4);
36897c478bd9Sstevel@tonic-gate 	} else {
36907c478bd9Sstevel@tonic-gate 		lifr.lifr_flags |= IFF_IPV4;
36917c478bd9Sstevel@tonic-gate 		lifr.lifr_flags &= ~IFF_IPV6;
36927c478bd9Sstevel@tonic-gate 	}
36937c478bd9Sstevel@tonic-gate 
36947c478bd9Sstevel@tonic-gate 	/* record the device and module names as interface name */
36957c478bd9Sstevel@tonic-gate 	lifr.lifr_ppa = diap->ppa;
36967c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
36977c478bd9Sstevel@tonic-gate 
36987c478bd9Sstevel@tonic-gate 	/* set the interface name */
36997c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, SIOCSLIFNAME, (char *)&lifr) == -1) {
37007c478bd9Sstevel@tonic-gate 		if (errno != EEXIST)
37017c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCSLIFNAME for ip");
37027c478bd9Sstevel@tonic-gate 		/*
37037c478bd9Sstevel@tonic-gate 		 * This difference between the way we behave for EEXIST
37047c478bd9Sstevel@tonic-gate 		 * and that with other errors exists to preserve legacy
37057c478bd9Sstevel@tonic-gate 		 * behaviour. Earlier when foreachinterface() and matcif()
37067c478bd9Sstevel@tonic-gate 		 * were doing the duplicate interface name checks, for
37077c478bd9Sstevel@tonic-gate 		 * already existing interfaces, inetplumb() returned "0".
37087c478bd9Sstevel@tonic-gate 		 * To preserve this behaviour, Perror0() and return are
37097c478bd9Sstevel@tonic-gate 		 * called for EEXIST.
37107c478bd9Sstevel@tonic-gate 		 */
37117c478bd9Sstevel@tonic-gate 		Perror0("SIOCSLIFNAME for ip");
37127c478bd9Sstevel@tonic-gate 		return;
37137c478bd9Sstevel@tonic-gate 	}
37147c478bd9Sstevel@tonic-gate 
37157c478bd9Sstevel@tonic-gate 	/* Get the full set of existing flags for this stream */
37167c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, SIOCGLIFFLAGS, (char *)&lifr) == -1)
37177c478bd9Sstevel@tonic-gate 		Perror0_exit("plumb_one_device: SIOCFLIFFLAGS");
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	if (debug) {
37207c478bd9Sstevel@tonic-gate 		(void) printf("plumb_one_device: %s got flags: \n",
37217c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
37227c478bd9Sstevel@tonic-gate 		print_flags(lifr.lifr_flags);
37237c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
37247c478bd9Sstevel@tonic-gate 	}
37257c478bd9Sstevel@tonic-gate 
37267c478bd9Sstevel@tonic-gate 	/* Check if arp is not actually needed */
37277c478bd9Sstevel@tonic-gate 	if (lifr.lifr_flags & (IFF_NOARP|IFF_IPV6)) {
37287c478bd9Sstevel@tonic-gate 		if (ioctl(ip_fd, I_POP, 0) == -1)
37297c478bd9Sstevel@tonic-gate 			Perror2_exit("I_POP", ARP_MOD_NAME);
37307c478bd9Sstevel@tonic-gate 	}
37317c478bd9Sstevel@tonic-gate 
37327c478bd9Sstevel@tonic-gate 	/*
37337c478bd9Sstevel@tonic-gate 	 * Open "/dev/udp" for use as a multiplexor to PLINK the
37347c478bd9Sstevel@tonic-gate 	 * interface stream under. We use "/dev/udp" instead of "/dev/ip"
37357c478bd9Sstevel@tonic-gate 	 * since STREAMS will not let you PLINK a driver under itself,
37367c478bd9Sstevel@tonic-gate 	 * and "/dev/ip" is typically the driver at the bottom of
37377c478bd9Sstevel@tonic-gate 	 * the stream for tunneling interfaces.
37387c478bd9Sstevel@tonic-gate 	 */
37397c478bd9Sstevel@tonic-gate 	if (af == AF_INET6)
37407c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP6_DEV_NAME;
37417c478bd9Sstevel@tonic-gate 	else
37427c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP_DEV_NAME;
37437c478bd9Sstevel@tonic-gate 	if ((mux_fd = open_arp_on_udp(udp_dev_name)) == -1)
37447c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
37457c478bd9Sstevel@tonic-gate 
37467c478bd9Sstevel@tonic-gate 	/* Check if arp is not needed */
37477c478bd9Sstevel@tonic-gate 	if (lifr.lifr_flags & (IFF_NOARP|IFF_IPV6)) {
37487c478bd9Sstevel@tonic-gate 		/*
37497c478bd9Sstevel@tonic-gate 		 * PLINK the interface stream so that ifconfig can exit
37507c478bd9Sstevel@tonic-gate 		 * without tearing down the stream.
37517c478bd9Sstevel@tonic-gate 		 */
37527c478bd9Sstevel@tonic-gate 		if ((ip_muxid = ioctl(mux_fd, I_PLINK, ip_fd)) == -1) {
37537c478bd9Sstevel@tonic-gate 			Perror0_exit("I_PLINK for ip");
37547c478bd9Sstevel@tonic-gate 		}
37557c478bd9Sstevel@tonic-gate 		(void) close(mux_fd);
37567c478bd9Sstevel@tonic-gate 		return;
37577c478bd9Sstevel@tonic-gate 	}
37587c478bd9Sstevel@tonic-gate 
37597c478bd9Sstevel@tonic-gate 	/*
37607c478bd9Sstevel@tonic-gate 	 * This interface does use ARP, so set up a separate stream
37617c478bd9Sstevel@tonic-gate 	 * from the interface to ARP.
37627c478bd9Sstevel@tonic-gate 	 *
37637c478bd9Sstevel@tonic-gate 	 * Note: modules specified by the user are pushed
37647c478bd9Sstevel@tonic-gate 	 * only on the interface stream, not on the ARP stream.
37657c478bd9Sstevel@tonic-gate 	 */
37667c478bd9Sstevel@tonic-gate 
37677c478bd9Sstevel@tonic-gate 	if (debug)
37687c478bd9Sstevel@tonic-gate 		(void) printf("plumb_one_device: ifname: %s\n",
37697c478bd9Sstevel@tonic-gate 		    diap->ifname);
37707c478bd9Sstevel@tonic-gate 	arp_fd = dlpi_if_open(diap->ifname, &dia, _B_FALSE);
37717c478bd9Sstevel@tonic-gate 
37727c478bd9Sstevel@tonic-gate 	if (dia.style == DL_STYLE2 && dlpi_detach(arp_fd, -1) < 0)
37737c478bd9Sstevel@tonic-gate 		Perror0_exit("dlpi_detach");
37747c478bd9Sstevel@tonic-gate 
37757c478bd9Sstevel@tonic-gate 	if (ioctl(arp_fd, I_PUSH, ARP_MOD_NAME) == -1)
37767c478bd9Sstevel@tonic-gate 		Perror2_exit("I_PUSH", ARP_MOD_NAME);
37777c478bd9Sstevel@tonic-gate 
37787c478bd9Sstevel@tonic-gate 	/*
37797c478bd9Sstevel@tonic-gate 	 * Tell ARP the name and unit number for this interface.
37807c478bd9Sstevel@tonic-gate 	 * Note that arp has no support for transparent ioctls.
37817c478bd9Sstevel@tonic-gate 	 */
37827c478bd9Sstevel@tonic-gate 	if (strioctl(arp_fd, SIOCSLIFNAME, (char *)&lifr,
37837c478bd9Sstevel@tonic-gate 	    sizeof (lifr)) == -1) {
37847c478bd9Sstevel@tonic-gate 		if (errno != EEXIST)
37857c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCSLIFNAME for arp");
37867c478bd9Sstevel@tonic-gate 		Perror0("SIOCSLIFNAME for arp");
37877c478bd9Sstevel@tonic-gate 		(void) close(arp_fd);
37887c478bd9Sstevel@tonic-gate 		(void) close(mux_fd);
37897c478bd9Sstevel@tonic-gate 		return;
37907c478bd9Sstevel@tonic-gate 	}
37917c478bd9Sstevel@tonic-gate 	/*
37927c478bd9Sstevel@tonic-gate 	 * PLINK the IP and ARP streams so that ifconfig can exit
37937c478bd9Sstevel@tonic-gate 	 * without tearing down the stream.
37947c478bd9Sstevel@tonic-gate 	 */
37957c478bd9Sstevel@tonic-gate 	if ((ip_muxid = ioctl(mux_fd, I_PLINK, ip_fd)) == -1) {
37967c478bd9Sstevel@tonic-gate 		Perror0_exit("I_PLINK for ip");
37977c478bd9Sstevel@tonic-gate 	}
37987c478bd9Sstevel@tonic-gate 	if ((arp_muxid = ioctl(mux_fd, I_PLINK, arp_fd)) == -1) {
37997c478bd9Sstevel@tonic-gate 		(void) ioctl(mux_fd, I_PUNLINK, ip_muxid);
38007c478bd9Sstevel@tonic-gate 		Perror0_exit("I_PLINK for arp");
38017c478bd9Sstevel@tonic-gate 	}
38027c478bd9Sstevel@tonic-gate 
38037c478bd9Sstevel@tonic-gate 	if (debug)
38047c478bd9Sstevel@tonic-gate 		(void) printf("arp muxid = %d\n", arp_muxid);
38057c478bd9Sstevel@tonic-gate 	(void) close(arp_fd);
38067c478bd9Sstevel@tonic-gate 	(void) close(mux_fd);
38077c478bd9Sstevel@tonic-gate }
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate 
38107c478bd9Sstevel@tonic-gate /*
38117c478bd9Sstevel@tonic-gate  * If this is a physical interface then remove it.
38127c478bd9Sstevel@tonic-gate  * If it is a logical interface name use SIOCLIFREMOVEIF to
38137c478bd9Sstevel@tonic-gate  * remove it. In both cases fail if it doesn't exist.
38147c478bd9Sstevel@tonic-gate  */
38157c478bd9Sstevel@tonic-gate /* ARGSUSED */
38167c478bd9Sstevel@tonic-gate static int
38177c478bd9Sstevel@tonic-gate inetunplumb(char *arg, int64_t param)
38187c478bd9Sstevel@tonic-gate {
38197c478bd9Sstevel@tonic-gate 	int ip_muxid, arp_muxid;
38207c478bd9Sstevel@tonic-gate 	int mux_fd;
38217c478bd9Sstevel@tonic-gate 	char *udp_dev_name;
38227c478bd9Sstevel@tonic-gate 	char *strptr;
38237c478bd9Sstevel@tonic-gate 	uint64_t flags;
38247c478bd9Sstevel@tonic-gate 	boolean_t changed_arp_muxid = _B_FALSE;
38257c478bd9Sstevel@tonic-gate 	int save_errno;
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate 	strptr = strchr(name, ':');
38287c478bd9Sstevel@tonic-gate 	if (strptr != NULL || strcmp(name, LOOPBACK_IF) == 0) {
38297c478bd9Sstevel@tonic-gate 		/* Can't unplumb logical interface zero */
38307c478bd9Sstevel@tonic-gate 		if (strptr != NULL && strcmp(strptr, ":0") == 0) {
38317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: unplumb:"
38327c478bd9Sstevel@tonic-gate 			    " Cannot unplumb %s: Invalid interface\n", name);
38337c478bd9Sstevel@tonic-gate 			exit(1);
38347c478bd9Sstevel@tonic-gate 		}
38357c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
38367c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
38377c478bd9Sstevel@tonic-gate 
38387c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0)
38397c478bd9Sstevel@tonic-gate 			Perror0_exit("unplumb: SIOCLIFREMOVEIF");
38407c478bd9Sstevel@tonic-gate 		return (0);
38417c478bd9Sstevel@tonic-gate 	}
38427c478bd9Sstevel@tonic-gate 
38437c478bd9Sstevel@tonic-gate 	/*
38447c478bd9Sstevel@tonic-gate 	 * We used /dev/udp or udp6 to set up the mux. So we have to use
38457c478bd9Sstevel@tonic-gate 	 * the same now for PUNLINK also.
38467c478bd9Sstevel@tonic-gate 	 */
38477c478bd9Sstevel@tonic-gate 	if (afp->af_af == AF_INET6)
38487c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP6_DEV_NAME;
38497c478bd9Sstevel@tonic-gate 	else
38507c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP_DEV_NAME;
38517c478bd9Sstevel@tonic-gate 
38527c478bd9Sstevel@tonic-gate 	if ((mux_fd = open_arp_on_udp(udp_dev_name)) == -1)
38537c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
38547c478bd9Sstevel@tonic-gate 
38557c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
38567c478bd9Sstevel@tonic-gate 	if (ioctl(mux_fd, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
38577c478bd9Sstevel@tonic-gate 		Perror0_exit("unplumb: SIOCGLIFFLAGS");
38587c478bd9Sstevel@tonic-gate 	}
38597c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
38607c478bd9Sstevel@tonic-gate 	if (ioctl(mux_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
38617c478bd9Sstevel@tonic-gate 		Perror0_exit("unplumb: SIOCGLIFMUXID");
38627c478bd9Sstevel@tonic-gate 	}
38637c478bd9Sstevel@tonic-gate 	arp_muxid = lifr.lifr_arp_muxid;
38647c478bd9Sstevel@tonic-gate 	ip_muxid = lifr.lifr_ip_muxid;
38657c478bd9Sstevel@tonic-gate 	/*
38667c478bd9Sstevel@tonic-gate 	 * We don't have a good way of knowing whether the arp stream is
38677c478bd9Sstevel@tonic-gate 	 * plumbed. We can't rely on IFF_NOARP because someone could
38687c478bd9Sstevel@tonic-gate 	 * have turned it off later using "ifconfig xxx -arp".
38697c478bd9Sstevel@tonic-gate 	 */
38707c478bd9Sstevel@tonic-gate 	if (arp_muxid != 0) {
38717c478bd9Sstevel@tonic-gate 		if (debug)
38727c478bd9Sstevel@tonic-gate 			(void) printf("arp_muxid %d\n", arp_muxid);
38737c478bd9Sstevel@tonic-gate 		if (ioctl(mux_fd, I_PUNLINK, arp_muxid) < 0) {
38747c478bd9Sstevel@tonic-gate 			if ((errno == EINVAL) &&
38757c478bd9Sstevel@tonic-gate 			    (flags & (IFF_NOARP | IFF_IPV6))) {
38767c478bd9Sstevel@tonic-gate 				/*
38777c478bd9Sstevel@tonic-gate 				 * Some plumbing utilities set the muxid to
38787c478bd9Sstevel@tonic-gate 				 * -1 or some invalid value to signify that
38797c478bd9Sstevel@tonic-gate 				 * there is no arp stream. Set the muxid to 0
38807c478bd9Sstevel@tonic-gate 				 * before trying to unplumb the IP stream.
38817c478bd9Sstevel@tonic-gate 				 * IP does not allow the IP stream to be
38827c478bd9Sstevel@tonic-gate 				 * unplumbed if it sees a non-null arp muxid,
38837c478bd9Sstevel@tonic-gate 				 * for consistency of IP-ARP streams.
38847c478bd9Sstevel@tonic-gate 				 */
38857c478bd9Sstevel@tonic-gate 				lifr.lifr_arp_muxid = 0;
38867c478bd9Sstevel@tonic-gate 				(void) ioctl(mux_fd, SIOCSLIFMUXID,
38877c478bd9Sstevel@tonic-gate 				    (caddr_t)&lifr);
38887c478bd9Sstevel@tonic-gate 				changed_arp_muxid = _B_TRUE;
38897c478bd9Sstevel@tonic-gate 			} else {
38907c478bd9Sstevel@tonic-gate 				Perror0("I_PUNLINK for arp");
38917c478bd9Sstevel@tonic-gate 			}
38927c478bd9Sstevel@tonic-gate 		}
38937c478bd9Sstevel@tonic-gate 	}
38947c478bd9Sstevel@tonic-gate 	if (debug)
38957c478bd9Sstevel@tonic-gate 		(void) printf("ip_muxid %d\n", ip_muxid);
38967c478bd9Sstevel@tonic-gate 
38977c478bd9Sstevel@tonic-gate 	if (ioctl(mux_fd, I_PUNLINK, ip_muxid) < 0) {
38987c478bd9Sstevel@tonic-gate 		if (changed_arp_muxid) {
38997c478bd9Sstevel@tonic-gate 			/*
39007c478bd9Sstevel@tonic-gate 			 * Some error occurred, and we need to restore
39017c478bd9Sstevel@tonic-gate 			 * everything back to what it was.
39027c478bd9Sstevel@tonic-gate 			 */
39037c478bd9Sstevel@tonic-gate 			save_errno = errno;
39047c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = arp_muxid;
39057c478bd9Sstevel@tonic-gate 			lifr.lifr_ip_muxid = ip_muxid;
39067c478bd9Sstevel@tonic-gate 			(void) ioctl(mux_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
39077c478bd9Sstevel@tonic-gate 			errno = save_errno;
39087c478bd9Sstevel@tonic-gate 		}
39097c478bd9Sstevel@tonic-gate 		Perror0_exit("I_PUNLINK for ip");
39107c478bd9Sstevel@tonic-gate 	}
39117c478bd9Sstevel@tonic-gate 	(void) close(mux_fd);
39127c478bd9Sstevel@tonic-gate 	return (0);
39137c478bd9Sstevel@tonic-gate }
39147c478bd9Sstevel@tonic-gate 
39157c478bd9Sstevel@tonic-gate /*
39167c478bd9Sstevel@tonic-gate  * If this is a physical interface then create it unless it is already
39177c478bd9Sstevel@tonic-gate  * present. If it is a logical interface name use SIOCLIFADDIF to
39187c478bd9Sstevel@tonic-gate  * create and (and fail it if already exists.)
39197c478bd9Sstevel@tonic-gate  * As a special case send SIOCLIFADDIF for the loopback interface. This
39207c478bd9Sstevel@tonic-gate  * is needed since there is no other notion of plumbing the loopback
39217c478bd9Sstevel@tonic-gate  * interface.
39227c478bd9Sstevel@tonic-gate  */
39237c478bd9Sstevel@tonic-gate /* ARGSUSED */
39247c478bd9Sstevel@tonic-gate static int
39257c478bd9Sstevel@tonic-gate inetplumb(char *arg, int64_t param)
39267c478bd9Sstevel@tonic-gate {
39277c478bd9Sstevel@tonic-gate 	char		*strptr;
39287c478bd9Sstevel@tonic-gate 	int		dev_fd;
39297c478bd9Sstevel@tonic-gate 	dlpi_if_attr_t	dia;
39307c478bd9Sstevel@tonic-gate 	boolean_t	islo;
39317c478bd9Sstevel@tonic-gate 
39327c478bd9Sstevel@tonic-gate 	strptr = strchr(name, ':');
39337c478bd9Sstevel@tonic-gate 	islo = (strcmp(name, LOOPBACK_IF) == 0);
39347c478bd9Sstevel@tonic-gate 
39357c478bd9Sstevel@tonic-gate 	if (strptr != NULL || islo) {
39367c478bd9Sstevel@tonic-gate 		(void) memset(&lifr, 0, sizeof (lifr));
39377c478bd9Sstevel@tonic-gate 		(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
39387c478bd9Sstevel@tonic-gate 		if (islo && ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0) {
39397c478bd9Sstevel@tonic-gate 			if (debug) {
39407c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
39417c478bd9Sstevel@tonic-gate 				    "ifconfig: %s already exists\n", name);
39427c478bd9Sstevel@tonic-gate 			}
39437c478bd9Sstevel@tonic-gate 			return (0);
39447c478bd9Sstevel@tonic-gate 		}
39457c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
39467c478bd9Sstevel@tonic-gate 			if (errno == EEXIST) {
39477c478bd9Sstevel@tonic-gate 				if (debug) {
39487c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
39497c478bd9Sstevel@tonic-gate 					    "ifconfig: %s already exists\n",
39507c478bd9Sstevel@tonic-gate 					    name);
39517c478bd9Sstevel@tonic-gate 				}
39527c478bd9Sstevel@tonic-gate 			} else {
39537c478bd9Sstevel@tonic-gate 				Perror2_exit("plumb: SIOCLIFADDIF", name);
39547c478bd9Sstevel@tonic-gate 			}
39557c478bd9Sstevel@tonic-gate 		}
39567c478bd9Sstevel@tonic-gate 		/*
39577c478bd9Sstevel@tonic-gate 		 * IP can create the new logical interface on a different
39587c478bd9Sstevel@tonic-gate 		 * physical interface in the same IPMP group. Take the new
39597c478bd9Sstevel@tonic-gate 		 * interface into account for further operations.
39607c478bd9Sstevel@tonic-gate 		 */
39617c478bd9Sstevel@tonic-gate 		(void) strncpy(name, lifr.lifr_name, sizeof (name));
39627c478bd9Sstevel@tonic-gate 		return (0);
39637c478bd9Sstevel@tonic-gate 	}
39647c478bd9Sstevel@tonic-gate 
39657c478bd9Sstevel@tonic-gate 	if (debug)
39667c478bd9Sstevel@tonic-gate 		(void) printf("inetplumb: %s af %d\n", name, afp->af_af);
39677c478bd9Sstevel@tonic-gate 
39687c478bd9Sstevel@tonic-gate 	if ((dev_fd = dlpi_if_open(name, &dia, _B_FALSE)) < 0) {
39697c478bd9Sstevel@tonic-gate 		Perror2_exit("plumb", name);
39707c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
39717c478bd9Sstevel@tonic-gate 	}
39727c478bd9Sstevel@tonic-gate 
39737c478bd9Sstevel@tonic-gate 	plumb_one_device(&dia, dev_fd, afp->af_af);
39747c478bd9Sstevel@tonic-gate 	(void) close(dev_fd);
39757c478bd9Sstevel@tonic-gate 	return (0);
39767c478bd9Sstevel@tonic-gate }
39777c478bd9Sstevel@tonic-gate 
39787c478bd9Sstevel@tonic-gate void
39797c478bd9Sstevel@tonic-gate Perror0(char *cmd)
39807c478bd9Sstevel@tonic-gate {
39817c478bd9Sstevel@tonic-gate 	int save_errno;
39827c478bd9Sstevel@tonic-gate 
39837c478bd9Sstevel@tonic-gate 	save_errno = errno;
39847c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "ifconfig: ");
39857c478bd9Sstevel@tonic-gate 	errno = save_errno;
39867c478bd9Sstevel@tonic-gate 	switch (errno) {
39877c478bd9Sstevel@tonic-gate 
39887c478bd9Sstevel@tonic-gate 	case ENXIO:
39897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: no such interface\n",
39907c478bd9Sstevel@tonic-gate 			cmd, lifr.lifr_name);
39917c478bd9Sstevel@tonic-gate 		break;
39927c478bd9Sstevel@tonic-gate 
39937c478bd9Sstevel@tonic-gate 	case EPERM:
39947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: permission denied\n",
39957c478bd9Sstevel@tonic-gate 			cmd, lifr.lifr_name);
39967c478bd9Sstevel@tonic-gate 		break;
39977c478bd9Sstevel@tonic-gate 
39987c478bd9Sstevel@tonic-gate 	case EEXIST:
39997c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: already exists\n",
40007c478bd9Sstevel@tonic-gate 			cmd, lifr.lifr_name);
40017c478bd9Sstevel@tonic-gate 		break;
40027c478bd9Sstevel@tonic-gate 
40037c478bd9Sstevel@tonic-gate 	default: {
40047c478bd9Sstevel@tonic-gate 		char buf[BUFSIZ];
40057c478bd9Sstevel@tonic-gate 
40067c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s: %s",
40077c478bd9Sstevel@tonic-gate 		    cmd, lifr.lifr_name);
40087c478bd9Sstevel@tonic-gate 		perror(buf);
40097c478bd9Sstevel@tonic-gate 	}
40107c478bd9Sstevel@tonic-gate 	}
40117c478bd9Sstevel@tonic-gate }
40127c478bd9Sstevel@tonic-gate 
40137c478bd9Sstevel@tonic-gate void
40147c478bd9Sstevel@tonic-gate Perror0_exit(char *cmd)
40157c478bd9Sstevel@tonic-gate {
40167c478bd9Sstevel@tonic-gate 	Perror0(cmd);
40177c478bd9Sstevel@tonic-gate 	exit(1);
40187c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
40197c478bd9Sstevel@tonic-gate }
40207c478bd9Sstevel@tonic-gate 
40217c478bd9Sstevel@tonic-gate void
40227c478bd9Sstevel@tonic-gate Perror2(char *cmd, char *str)
40237c478bd9Sstevel@tonic-gate {
40247c478bd9Sstevel@tonic-gate 	int save_errno;
40257c478bd9Sstevel@tonic-gate 
40267c478bd9Sstevel@tonic-gate 	save_errno = errno;
40277c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "ifconfig: ");
40287c478bd9Sstevel@tonic-gate 	errno = save_errno;
40297c478bd9Sstevel@tonic-gate 	switch (errno) {
40307c478bd9Sstevel@tonic-gate 
40317c478bd9Sstevel@tonic-gate 	case ENXIO:
40327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: no such interface\n",
40337c478bd9Sstevel@tonic-gate 			cmd, str);
40347c478bd9Sstevel@tonic-gate 		break;
40357c478bd9Sstevel@tonic-gate 
40367c478bd9Sstevel@tonic-gate 	case EPERM:
40377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: permission denied\n",
40387c478bd9Sstevel@tonic-gate 			cmd, str);
40397c478bd9Sstevel@tonic-gate 		break;
40407c478bd9Sstevel@tonic-gate 
40417c478bd9Sstevel@tonic-gate 	default: {
40427c478bd9Sstevel@tonic-gate 		char buf[BUFSIZ];
40437c478bd9Sstevel@tonic-gate 
40447c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s: %s", cmd, str);
40457c478bd9Sstevel@tonic-gate 		perror(buf);
40467c478bd9Sstevel@tonic-gate 	}
40477c478bd9Sstevel@tonic-gate 	}
40487c478bd9Sstevel@tonic-gate }
40497c478bd9Sstevel@tonic-gate 
40507c478bd9Sstevel@tonic-gate /*
40517c478bd9Sstevel@tonic-gate  * Print out error message (Perror2()) and exit
40527c478bd9Sstevel@tonic-gate  */
40537c478bd9Sstevel@tonic-gate void
40547c478bd9Sstevel@tonic-gate Perror2_exit(char *cmd, char *str)
40557c478bd9Sstevel@tonic-gate {
40567c478bd9Sstevel@tonic-gate 	Perror2(cmd, str);
40577c478bd9Sstevel@tonic-gate 	exit(1);
40587c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
40597c478bd9Sstevel@tonic-gate }
40607c478bd9Sstevel@tonic-gate 
40617c478bd9Sstevel@tonic-gate /*
40627c478bd9Sstevel@tonic-gate  * If the last argument is non-NULL allow a <addr>/<n> syntax and
40637c478bd9Sstevel@tonic-gate  * pass out <n> in *plenp.
40647c478bd9Sstevel@tonic-gate  * If <n> doesn't parse return BAD_ADDR as *plenp.
40657c478bd9Sstevel@tonic-gate  * If no /<n> is present return NO_PREFIX as *plenp.
40667c478bd9Sstevel@tonic-gate  */
40677c478bd9Sstevel@tonic-gate static void
40687c478bd9Sstevel@tonic-gate in_getaddr(char *s, struct sockaddr *saddr, int *plenp)
40697c478bd9Sstevel@tonic-gate {
4070*69bb4bb4Scarlsonj 	/* LINTED: alignment */
40717c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
40727c478bd9Sstevel@tonic-gate 	struct hostent *hp;
40737c478bd9Sstevel@tonic-gate 	struct netent *np;
40747c478bd9Sstevel@tonic-gate 	char str[BUFSIZ];
40757c478bd9Sstevel@tonic-gate 	int error_num;
40767c478bd9Sstevel@tonic-gate 
40777c478bd9Sstevel@tonic-gate 	(void) strncpy(str, s, sizeof (str));
40787c478bd9Sstevel@tonic-gate 
40797c478bd9Sstevel@tonic-gate 	/*
40807c478bd9Sstevel@tonic-gate 	 * Look for '/'<n> is plenp
40817c478bd9Sstevel@tonic-gate 	 */
40827c478bd9Sstevel@tonic-gate 	if (plenp != NULL) {
40837c478bd9Sstevel@tonic-gate 		char *cp;
40847c478bd9Sstevel@tonic-gate 
40857906a3e0Smeem 		*plenp = in_getprefixlen(str, _B_TRUE, IP_ABITS);
40867c478bd9Sstevel@tonic-gate 		if (*plenp == BAD_ADDR)
40877c478bd9Sstevel@tonic-gate 			return;
40887c478bd9Sstevel@tonic-gate 		cp = strchr(str, '/');
40897c478bd9Sstevel@tonic-gate 		if (cp != NULL)
40907c478bd9Sstevel@tonic-gate 			*cp = '\0';
40917c478bd9Sstevel@tonic-gate 	} else if (strchr(str, '/') != NULL) {
40927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
40937c478bd9Sstevel@tonic-gate 		exit(1);
40947c478bd9Sstevel@tonic-gate 	}
40957c478bd9Sstevel@tonic-gate 
40967c478bd9Sstevel@tonic-gate 	(void) memset(sin, 0, sizeof (*sin));
40977c478bd9Sstevel@tonic-gate 
40987c478bd9Sstevel@tonic-gate 	/*
40997c478bd9Sstevel@tonic-gate 	 *	Try to catch attempts to set the broadcast address to all 1's.
41007c478bd9Sstevel@tonic-gate 	 */
41017c478bd9Sstevel@tonic-gate 	if (strcmp(str, "255.255.255.255") == 0 ||
41027c478bd9Sstevel@tonic-gate 	    (strtoul(str, (char **)NULL, 0) == 0xffffffffUL)) {
41037c478bd9Sstevel@tonic-gate 		sin->sin_family = AF_INET;
41047c478bd9Sstevel@tonic-gate 		sin->sin_addr.s_addr = 0xffffffff;
41057c478bd9Sstevel@tonic-gate 		return;
41067c478bd9Sstevel@tonic-gate 	}
41077c478bd9Sstevel@tonic-gate 
41087c478bd9Sstevel@tonic-gate 	hp = getipnodebyname(str, AF_INET, 0, &error_num);
41097c478bd9Sstevel@tonic-gate 	if (hp) {
41107c478bd9Sstevel@tonic-gate 		sin->sin_family = hp->h_addrtype;
41117c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
41127c478bd9Sstevel@tonic-gate 		freehostent(hp);
41137c478bd9Sstevel@tonic-gate 		return;
41147c478bd9Sstevel@tonic-gate 	}
41157c478bd9Sstevel@tonic-gate 	np = getnetbyname(str);
41167c478bd9Sstevel@tonic-gate 	if (np) {
41177c478bd9Sstevel@tonic-gate 		sin->sin_family = np->n_addrtype;
41187c478bd9Sstevel@tonic-gate 		sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
41197c478bd9Sstevel@tonic-gate 		return;
41207c478bd9Sstevel@tonic-gate 	}
41217c478bd9Sstevel@tonic-gate 	if (error_num == TRY_AGAIN) {
41227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address "
41237c478bd9Sstevel@tonic-gate 		    "(try again later)\n", s);
41247c478bd9Sstevel@tonic-gate 	} else {
41257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
41267c478bd9Sstevel@tonic-gate 	}
41277c478bd9Sstevel@tonic-gate 	exit(1);
41287c478bd9Sstevel@tonic-gate }
41297c478bd9Sstevel@tonic-gate 
41307c478bd9Sstevel@tonic-gate /*
41317c478bd9Sstevel@tonic-gate  * If the last argument is non-NULL allow a <addr>/<n> syntax and
41327c478bd9Sstevel@tonic-gate  * pass out <n> in *plenp.
41337c478bd9Sstevel@tonic-gate  * If <n> doesn't parse return BAD_ADDR as *plenp.
41347c478bd9Sstevel@tonic-gate  * If no /<n> is present return NO_PREFIX as *plenp.
41357c478bd9Sstevel@tonic-gate  */
41367c478bd9Sstevel@tonic-gate static void
41377c478bd9Sstevel@tonic-gate in6_getaddr(char *s, struct sockaddr *saddr, int *plenp)
41387c478bd9Sstevel@tonic-gate {
4139*69bb4bb4Scarlsonj 	/* LINTED: alignment */
41407c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)saddr;
41417c478bd9Sstevel@tonic-gate 	struct hostent *hp;
41427c478bd9Sstevel@tonic-gate 	char str[BUFSIZ];
41437c478bd9Sstevel@tonic-gate 	int error_num;
41447c478bd9Sstevel@tonic-gate 
41457c478bd9Sstevel@tonic-gate 	(void) strncpy(str, s, sizeof (str));
41467c478bd9Sstevel@tonic-gate 
41477c478bd9Sstevel@tonic-gate 	/*
41487c478bd9Sstevel@tonic-gate 	 * Look for '/'<n> is plenp
41497c478bd9Sstevel@tonic-gate 	 */
41507c478bd9Sstevel@tonic-gate 	if (plenp != NULL) {
41517c478bd9Sstevel@tonic-gate 		char *cp;
41527c478bd9Sstevel@tonic-gate 
41537906a3e0Smeem 		*plenp = in_getprefixlen(str, _B_TRUE, IPV6_ABITS);
41547c478bd9Sstevel@tonic-gate 		if (*plenp == BAD_ADDR)
41557c478bd9Sstevel@tonic-gate 			return;
41567c478bd9Sstevel@tonic-gate 		cp = strchr(str, '/');
41577c478bd9Sstevel@tonic-gate 		if (cp != NULL)
41587c478bd9Sstevel@tonic-gate 			*cp = '\0';
41597c478bd9Sstevel@tonic-gate 	} else if (strchr(str, '/') != NULL) {
41607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
41617c478bd9Sstevel@tonic-gate 		exit(1);
41627c478bd9Sstevel@tonic-gate 	}
41637c478bd9Sstevel@tonic-gate 
41647c478bd9Sstevel@tonic-gate 	(void) memset(sin6, 0, sizeof (*sin6));
41657c478bd9Sstevel@tonic-gate 
41667c478bd9Sstevel@tonic-gate 	hp = getipnodebyname(str, AF_INET6, 0, &error_num);
41677c478bd9Sstevel@tonic-gate 	if (hp) {
41687c478bd9Sstevel@tonic-gate 		sin6->sin6_family = hp->h_addrtype;
41697c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin6->sin6_addr, hp->h_addr, hp->h_length);
41707c478bd9Sstevel@tonic-gate 		freehostent(hp);
41717c478bd9Sstevel@tonic-gate 		return;
41727c478bd9Sstevel@tonic-gate 	}
41737c478bd9Sstevel@tonic-gate 	if (error_num == TRY_AGAIN) {
41747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address "
41757c478bd9Sstevel@tonic-gate 		    "(try again later)\n", s);
41767c478bd9Sstevel@tonic-gate 	} else {
41777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
41787c478bd9Sstevel@tonic-gate 	}
41797c478bd9Sstevel@tonic-gate 	exit(1);
41807c478bd9Sstevel@tonic-gate }
41817c478bd9Sstevel@tonic-gate 
41827c478bd9Sstevel@tonic-gate /*
41837c478bd9Sstevel@tonic-gate  * If "slash" is zero this parses the whole string as
41847c478bd9Sstevel@tonic-gate  * an integer. With "slash" non zero it parses the tail part as an integer.
41857c478bd9Sstevel@tonic-gate  *
41867c478bd9Sstevel@tonic-gate  * If it is not a valid integer this returns BAD_ADDR.
41877c478bd9Sstevel@tonic-gate  * If there is /<n> present this returns NO_PREFIX.
41887c478bd9Sstevel@tonic-gate  */
41897c478bd9Sstevel@tonic-gate static int
41907c478bd9Sstevel@tonic-gate in_getprefixlen(char *addr, boolean_t slash, int max_plen)
41917c478bd9Sstevel@tonic-gate {
41927c478bd9Sstevel@tonic-gate 	int prefixlen;
41937c478bd9Sstevel@tonic-gate 	char *str, *end;
41947c478bd9Sstevel@tonic-gate 
41957c478bd9Sstevel@tonic-gate 	if (slash) {
41967c478bd9Sstevel@tonic-gate 		str = strchr(addr, '/');
41977c478bd9Sstevel@tonic-gate 		if (str == NULL)
41987c478bd9Sstevel@tonic-gate 			return (NO_PREFIX);
41997c478bd9Sstevel@tonic-gate 		str++;
42007c478bd9Sstevel@tonic-gate 	} else
42017c478bd9Sstevel@tonic-gate 		str = addr;
42027c478bd9Sstevel@tonic-gate 
42037c478bd9Sstevel@tonic-gate 	prefixlen = strtol(str, &end, 10);
42047c478bd9Sstevel@tonic-gate 	if (prefixlen < 0)
42057c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
42067c478bd9Sstevel@tonic-gate 	if (str == end)
42077c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
42087c478bd9Sstevel@tonic-gate 	if (max_plen != 0 && max_plen < prefixlen)
42097c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
42107c478bd9Sstevel@tonic-gate 	return (prefixlen);
42117c478bd9Sstevel@tonic-gate }
42127c478bd9Sstevel@tonic-gate 
42137c478bd9Sstevel@tonic-gate /*
42147c478bd9Sstevel@tonic-gate  * Convert a prefix length to a mask.
42157c478bd9Sstevel@tonic-gate  * Returns 1 if ok. 0 otherwise.
42167c478bd9Sstevel@tonic-gate  * Assumes the mask array is zero'ed by the caller.
42177c478bd9Sstevel@tonic-gate  */
42187c478bd9Sstevel@tonic-gate static boolean_t
42197c478bd9Sstevel@tonic-gate in_prefixlentomask(int prefixlen, int maxlen, uchar_t *mask)
42207c478bd9Sstevel@tonic-gate {
42217c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxlen)
42227c478bd9Sstevel@tonic-gate 		return (0);
42237c478bd9Sstevel@tonic-gate 
42247c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
42257c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
42267c478bd9Sstevel@tonic-gate 			*mask++ = 0xFF;
42277c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
42287c478bd9Sstevel@tonic-gate 			continue;
42297c478bd9Sstevel@tonic-gate 		}
42307c478bd9Sstevel@tonic-gate 		*mask |= 1 << (8 - prefixlen);
42317c478bd9Sstevel@tonic-gate 		prefixlen--;
42327c478bd9Sstevel@tonic-gate 	}
42337c478bd9Sstevel@tonic-gate 	return (1);
42347c478bd9Sstevel@tonic-gate }
42357c478bd9Sstevel@tonic-gate 
42367c478bd9Sstevel@tonic-gate static void
42377c478bd9Sstevel@tonic-gate print_flags(uint64_t flags)
42387c478bd9Sstevel@tonic-gate {
42397c478bd9Sstevel@tonic-gate 	boolean_t first = _B_TRUE;
42407c478bd9Sstevel@tonic-gate 	int cnt, i;
42417c478bd9Sstevel@tonic-gate 
42427c478bd9Sstevel@tonic-gate 	(void) printf("flags=%llx", flags);
42437c478bd9Sstevel@tonic-gate 	cnt = sizeof (if_flags_tbl) / sizeof (if_flags_t);
42447c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
42457c478bd9Sstevel@tonic-gate 		if (flags & if_flags_tbl[i].iff_value) {
42467c478bd9Sstevel@tonic-gate 			if (first) {
42477c478bd9Sstevel@tonic-gate 				(void) printf("<");
42487c478bd9Sstevel@tonic-gate 				first = _B_FALSE;
42497c478bd9Sstevel@tonic-gate 			} else {
42507c478bd9Sstevel@tonic-gate 				/*
42517c478bd9Sstevel@tonic-gate 				 * It has to be here and not with the
42527c478bd9Sstevel@tonic-gate 				 * printf below because for the last one,
42537c478bd9Sstevel@tonic-gate 				 * we don't want a comma before the ">".
42547c478bd9Sstevel@tonic-gate 				 */
42557c478bd9Sstevel@tonic-gate 				(void) printf(",");
42567c478bd9Sstevel@tonic-gate 			}
42577c478bd9Sstevel@tonic-gate 			(void) printf("%s", if_flags_tbl[i].iff_name);
42587c478bd9Sstevel@tonic-gate 		}
42597c478bd9Sstevel@tonic-gate 	}
42607c478bd9Sstevel@tonic-gate 	if (!first)
42617c478bd9Sstevel@tonic-gate 		(void) printf(">");
42627c478bd9Sstevel@tonic-gate }
42637c478bd9Sstevel@tonic-gate 
42647c478bd9Sstevel@tonic-gate static void
42657c478bd9Sstevel@tonic-gate print_config_flags(uint64_t flags)
42667c478bd9Sstevel@tonic-gate {
42677c478bd9Sstevel@tonic-gate 	int cnt, i;
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate 	cnt = sizeof (if_config_cmd_tbl) / sizeof (if_config_cmd_t);
42707c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
42717c478bd9Sstevel@tonic-gate 		if (flags & if_config_cmd_tbl[i].iff_flag) {
42727c478bd9Sstevel@tonic-gate 			(void) printf("%s ", if_config_cmd_tbl[i].iff_name);
42737c478bd9Sstevel@tonic-gate 		}
42747c478bd9Sstevel@tonic-gate 	}
42757c478bd9Sstevel@tonic-gate }
42767c478bd9Sstevel@tonic-gate 
42777c478bd9Sstevel@tonic-gate /*
4278dd7a6f5fSkcpoon  * Use the configured directory lookup mechanism (e.g. files/NIS/NIS+/...)
4279dd7a6f5fSkcpoon  * to find the network mask.  Returns true if we found one to set.
4280dd7a6f5fSkcpoon  *
4281dd7a6f5fSkcpoon  * The parameter addr_set controls whether we should get the address of
4282dd7a6f5fSkcpoon  * the working interface for the netmask query.  If addr_set is true,
4283dd7a6f5fSkcpoon  * we will use the address provided.  Otherwise, we will find the working
4284dd7a6f5fSkcpoon  * interface's address and use it instead.
42857c478bd9Sstevel@tonic-gate  */
42867c478bd9Sstevel@tonic-gate static boolean_t
4287dd7a6f5fSkcpoon in_getmask(struct sockaddr_in *saddr, boolean_t addr_set)
42887c478bd9Sstevel@tonic-gate {
42897c478bd9Sstevel@tonic-gate 	struct sockaddr_in ifaddr;
42907c478bd9Sstevel@tonic-gate 
42917c478bd9Sstevel@tonic-gate 	/*
4292dd7a6f5fSkcpoon 	 * Read the address from the interface if it is not passed in.
42937c478bd9Sstevel@tonic-gate 	 */
4294dd7a6f5fSkcpoon 	if (!addr_set) {
4295dd7a6f5fSkcpoon 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
4296dd7a6f5fSkcpoon 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
4297dd7a6f5fSkcpoon 			if (errno != EADDRNOTAVAIL) {
4298dd7a6f5fSkcpoon 				(void) fprintf(stderr, "Need net number for "
4299dd7a6f5fSkcpoon 				    "mask\n");
4300dd7a6f5fSkcpoon 			}
4301dd7a6f5fSkcpoon 			return (_B_FALSE);
4302dd7a6f5fSkcpoon 		}
4303dd7a6f5fSkcpoon 		ifaddr = *((struct sockaddr_in *)&lifr.lifr_addr);
4304dd7a6f5fSkcpoon 	} else {
4305dd7a6f5fSkcpoon 		ifaddr.sin_addr = saddr->sin_addr;
43067c478bd9Sstevel@tonic-gate 	}
43077c478bd9Sstevel@tonic-gate 	if (getnetmaskbyaddr(ifaddr.sin_addr, &saddr->sin_addr) == 0) {
43087c478bd9Sstevel@tonic-gate 		saddr->sin_family = AF_INET;
43097c478bd9Sstevel@tonic-gate 		return (_B_TRUE);
43107c478bd9Sstevel@tonic-gate 	}
43117c478bd9Sstevel@tonic-gate 	return (_B_FALSE);
43127c478bd9Sstevel@tonic-gate }
43137c478bd9Sstevel@tonic-gate 
43147c478bd9Sstevel@tonic-gate static int
43157c478bd9Sstevel@tonic-gate strioctl(int s, int cmd, char *buf, int buflen)
43167c478bd9Sstevel@tonic-gate {
43177c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
43187c478bd9Sstevel@tonic-gate 
43197c478bd9Sstevel@tonic-gate 	(void) memset(&ioc, 0, sizeof (ioc));
43207c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = cmd;
43217c478bd9Sstevel@tonic-gate 	ioc.ic_timout = 0;
43227c478bd9Sstevel@tonic-gate 	ioc.ic_len = buflen;
43237c478bd9Sstevel@tonic-gate 	ioc.ic_dp = buf;
43247c478bd9Sstevel@tonic-gate 	return (ioctl(s, I_STR, (char *)&ioc));
43257c478bd9Sstevel@tonic-gate }
43267c478bd9Sstevel@tonic-gate 
43277c478bd9Sstevel@tonic-gate static void
43287c478bd9Sstevel@tonic-gate add_ni(char *name)
43297c478bd9Sstevel@tonic-gate {
43307c478bd9Sstevel@tonic-gate 	ni_t **pp;
43317c478bd9Sstevel@tonic-gate 	ni_t *p;
43327c478bd9Sstevel@tonic-gate 
43337c478bd9Sstevel@tonic-gate 	for (pp = &ni_list; (p = *pp) != NULL; pp = &(p->ni_next)) {
43347c478bd9Sstevel@tonic-gate 		if (strcmp(p->ni_name, name) == 0) {
43357c478bd9Sstevel@tonic-gate 			if (debug > 2)
43367c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "'%s' is a duplicate\n",
43377c478bd9Sstevel@tonic-gate 				    name);
43387c478bd9Sstevel@tonic-gate 			return;
43397c478bd9Sstevel@tonic-gate 		}
43407c478bd9Sstevel@tonic-gate 	}
43417c478bd9Sstevel@tonic-gate 
43427c478bd9Sstevel@tonic-gate 	if (debug > 2)
43437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "adding '%s'\n",
43447c478bd9Sstevel@tonic-gate 		    name);
43457c478bd9Sstevel@tonic-gate 
43467c478bd9Sstevel@tonic-gate 	if ((p = malloc(sizeof (ni_t))) == NULL)
43477c478bd9Sstevel@tonic-gate 		return;
43487c478bd9Sstevel@tonic-gate 
43497c478bd9Sstevel@tonic-gate 	(void) strlcpy(p->ni_name, name, sizeof (p->ni_name));
43507c478bd9Sstevel@tonic-gate 	p->ni_next = NULL;
43517c478bd9Sstevel@tonic-gate 
43527c478bd9Sstevel@tonic-gate 	*pp = p;
43537c478bd9Sstevel@tonic-gate 	num_ni++;
43547c478bd9Sstevel@tonic-gate }
43557c478bd9Sstevel@tonic-gate 
43567c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
43577c478bd9Sstevel@tonic-gate static int
43587c478bd9Sstevel@tonic-gate devfs_entry(di_node_t node, di_minor_t minor, void *arg)
43597c478bd9Sstevel@tonic-gate {
43607c478bd9Sstevel@tonic-gate 	char *provider;
43617c478bd9Sstevel@tonic-gate 	int fd;
43627c478bd9Sstevel@tonic-gate 	uint_t ppa;
43637c478bd9Sstevel@tonic-gate 	dl_info_ack_t dlia;
43647c478bd9Sstevel@tonic-gate 	char ifname[LIFNAMSIZ];
43657c478bd9Sstevel@tonic-gate 
43667c478bd9Sstevel@tonic-gate 	provider = di_minor_name(minor);
43677c478bd9Sstevel@tonic-gate 
43687c478bd9Sstevel@tonic-gate 	if (strlen(provider) > LIFNAMSIZ)
43697c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
43707c478bd9Sstevel@tonic-gate 
43717c478bd9Sstevel@tonic-gate 	if (debug > 2)
43727c478bd9Sstevel@tonic-gate 		fprintf(stderr, "provider = %s\n", provider);
43737c478bd9Sstevel@tonic-gate 
43747c478bd9Sstevel@tonic-gate 	if ((fd = dlpi_open(provider)) < 0)
43757c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
43767c478bd9Sstevel@tonic-gate 
43777c478bd9Sstevel@tonic-gate 	if (debug > 2)
43787c478bd9Sstevel@tonic-gate 		fprintf(stderr, "getting DL_INFO_ACK\n");
43797c478bd9Sstevel@tonic-gate 
43807c478bd9Sstevel@tonic-gate 	if (dlpi_info(fd, -1, &dlia, NULL, NULL, NULL, NULL, NULL, NULL) < 0)
43817c478bd9Sstevel@tonic-gate 		goto done;
43827c478bd9Sstevel@tonic-gate 
43837c478bd9Sstevel@tonic-gate 	if (dlia.dl_provider_style == DL_STYLE1) {
43847c478bd9Sstevel@tonic-gate 		if (debug > 2)
43857c478bd9Sstevel@tonic-gate 			fprintf(stderr, "provider is DL_STYLE1\n");
43867c478bd9Sstevel@tonic-gate 		add_ni(provider);
43877c478bd9Sstevel@tonic-gate 		goto done;
43887c478bd9Sstevel@tonic-gate 	}
43897c478bd9Sstevel@tonic-gate 
43907c478bd9Sstevel@tonic-gate 	if (debug > 2)
43917c478bd9Sstevel@tonic-gate 		fprintf(stderr, "provider is DL_STYLE2\n");
43927c478bd9Sstevel@tonic-gate 
43937c478bd9Sstevel@tonic-gate 	if (di_minor_type(minor) != DDM_ALIAS) {
43947c478bd9Sstevel@tonic-gate 		if (debug > 2)
43957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
43967c478bd9Sstevel@tonic-gate 			    "non-alias node, ignoring\n");
43977c478bd9Sstevel@tonic-gate 		goto done;
43987c478bd9Sstevel@tonic-gate 	}
43997c478bd9Sstevel@tonic-gate 
44007c478bd9Sstevel@tonic-gate 	if (debug > 2)
44017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
44027c478bd9Sstevel@tonic-gate 		    "alias node, using instance\n");
44037c478bd9Sstevel@tonic-gate 	ppa = di_instance(node);
44047c478bd9Sstevel@tonic-gate 
44057c478bd9Sstevel@tonic-gate 	if (dlpi_attach(fd, -1, ppa) < 0) {
44067c478bd9Sstevel@tonic-gate 		if (debug > 2)
44077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
44087c478bd9Sstevel@tonic-gate 			    "non-existent PPA, ignoring\n");
44097c478bd9Sstevel@tonic-gate 		goto done;
44107c478bd9Sstevel@tonic-gate 	}
44117c478bd9Sstevel@tonic-gate 
44127c478bd9Sstevel@tonic-gate 	(void) snprintf(ifname, LIFNAMSIZ, "%s%d", provider, ppa);
44137c478bd9Sstevel@tonic-gate 	add_ni(ifname);
44147c478bd9Sstevel@tonic-gate 
44157c478bd9Sstevel@tonic-gate done:
44167c478bd9Sstevel@tonic-gate 	(void) dlpi_close(fd);
44177c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
44187c478bd9Sstevel@tonic-gate }
44197c478bd9Sstevel@tonic-gate 
44207c478bd9Sstevel@tonic-gate /*
44217c478bd9Sstevel@tonic-gate  * dhcp-related routines
44227c478bd9Sstevel@tonic-gate  */
44237c478bd9Sstevel@tonic-gate 
44247c478bd9Sstevel@tonic-gate static int
44257c478bd9Sstevel@tonic-gate setifdhcp(const char *caller, const char *ifname, int argc, char *argv[])
44267c478bd9Sstevel@tonic-gate {
44277c478bd9Sstevel@tonic-gate 	dhcp_ipc_request_t	*request;
44287c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t	*reply	= NULL;
44297c478bd9Sstevel@tonic-gate 	int			timeout = DHCP_IPC_WAIT_DEFAULT;
44307c478bd9Sstevel@tonic-gate 	dhcp_ipc_type_t		type	= DHCP_START;
44317c478bd9Sstevel@tonic-gate 	int			error;
44327c478bd9Sstevel@tonic-gate 	boolean_t		is_primary = _B_FALSE;
44337c478bd9Sstevel@tonic-gate 	boolean_t		started = _B_FALSE;
44347c478bd9Sstevel@tonic-gate 
44357c478bd9Sstevel@tonic-gate 	for (argv++; --argc > 0; argv++) {
44367c478bd9Sstevel@tonic-gate 
44377c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "primary") == 0) {
44387c478bd9Sstevel@tonic-gate 			is_primary = _B_TRUE;
44397c478bd9Sstevel@tonic-gate 			continue;
44407c478bd9Sstevel@tonic-gate 		}
44417c478bd9Sstevel@tonic-gate 
44427c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "wait") == 0) {
44437c478bd9Sstevel@tonic-gate 			if (--argc <= 0) {
44447c478bd9Sstevel@tonic-gate 				usage();
44457c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
44467c478bd9Sstevel@tonic-gate 			}
44477c478bd9Sstevel@tonic-gate 			argv++;
44487c478bd9Sstevel@tonic-gate 
44497c478bd9Sstevel@tonic-gate 			if (strcmp(*argv, "forever") == 0) {
44507c478bd9Sstevel@tonic-gate 				timeout = DHCP_IPC_WAIT_FOREVER;
44517c478bd9Sstevel@tonic-gate 				continue;
44527c478bd9Sstevel@tonic-gate 			}
44537c478bd9Sstevel@tonic-gate 
44547c478bd9Sstevel@tonic-gate 			if (sscanf(*argv, "%d", &timeout) != 1) {
44557c478bd9Sstevel@tonic-gate 				usage();
44567c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
44577c478bd9Sstevel@tonic-gate 			}
44587c478bd9Sstevel@tonic-gate 
44597c478bd9Sstevel@tonic-gate 			if (timeout < 0) {
44607c478bd9Sstevel@tonic-gate 				usage();
44617c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
44627c478bd9Sstevel@tonic-gate 			}
44637c478bd9Sstevel@tonic-gate 			continue;
44647c478bd9Sstevel@tonic-gate 		}
44657c478bd9Sstevel@tonic-gate 
44667c478bd9Sstevel@tonic-gate 		type = dhcp_string_to_request(*argv);
44677c478bd9Sstevel@tonic-gate 		if (type == -1) {
44687c478bd9Sstevel@tonic-gate 			usage();
44697c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_BADARGS);
44707c478bd9Sstevel@tonic-gate 		}
44717c478bd9Sstevel@tonic-gate 	}
44727c478bd9Sstevel@tonic-gate 
44737c478bd9Sstevel@tonic-gate 	/*
44747c478bd9Sstevel@tonic-gate 	 * Only try to start agent on start or inform; in all other cases it
44757c478bd9Sstevel@tonic-gate 	 * has to already be running for anything to make sense.
44767c478bd9Sstevel@tonic-gate 	 */
44777c478bd9Sstevel@tonic-gate 	if (type == DHCP_START || type == DHCP_INFORM) {
44787c478bd9Sstevel@tonic-gate 		if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1) {
44797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: unable to start %s\n",
44807c478bd9Sstevel@tonic-gate 			    caller, DHCP_AGENT_PATH);
44817c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_FAILURE);
44827c478bd9Sstevel@tonic-gate 		}
44837c478bd9Sstevel@tonic-gate 		started = _B_TRUE;
44847c478bd9Sstevel@tonic-gate 	}
44857c478bd9Sstevel@tonic-gate 
44867c478bd9Sstevel@tonic-gate 	if (is_primary)
44877c478bd9Sstevel@tonic-gate 		type = type | DHCP_PRIMARY;
44887c478bd9Sstevel@tonic-gate 
44897c478bd9Sstevel@tonic-gate 	request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
44907c478bd9Sstevel@tonic-gate 	if (request == NULL) {
44917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: out of memory\n", caller);
44927c478bd9Sstevel@tonic-gate 		return (DHCP_EXIT_SYSTEM);
44937c478bd9Sstevel@tonic-gate 	}
44947c478bd9Sstevel@tonic-gate 
44957c478bd9Sstevel@tonic-gate 	error = dhcp_ipc_make_request(request, &reply, timeout);
44967c478bd9Sstevel@tonic-gate 	if (error != 0) {
44977c478bd9Sstevel@tonic-gate 		free(request);
44987c478bd9Sstevel@tonic-gate 		/*
44997c478bd9Sstevel@tonic-gate 		 * Re-map connect error to not under control if we didn't try a
45007c478bd9Sstevel@tonic-gate 		 * start operation, as this has to be true and results in a
45017c478bd9Sstevel@tonic-gate 		 * clearer message, not to mention preserving compatibility
45027c478bd9Sstevel@tonic-gate 		 * with the days when we always started dhcpagent for every
45037c478bd9Sstevel@tonic-gate 		 * request.
45047c478bd9Sstevel@tonic-gate 		 */
45057c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_CONNECT && !started)
45067c478bd9Sstevel@tonic-gate 			error = DHCP_IPC_E_UNKIF;
45077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
45087c478bd9Sstevel@tonic-gate 		    dhcp_ipc_strerror(error));
45097c478bd9Sstevel@tonic-gate 		return (DHCP_EXIT_FAILURE);
45107c478bd9Sstevel@tonic-gate 	}
45117c478bd9Sstevel@tonic-gate 
45127c478bd9Sstevel@tonic-gate 	error = reply->return_code;
45137c478bd9Sstevel@tonic-gate 	if (error != 0) {
45147c478bd9Sstevel@tonic-gate 		free(request);
45157c478bd9Sstevel@tonic-gate 		free(reply);
45167c478bd9Sstevel@tonic-gate 
45177c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_TIMEOUT && timeout == 0)
45187c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_SUCCESS);
45197c478bd9Sstevel@tonic-gate 
45207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
45217c478bd9Sstevel@tonic-gate 		    dhcp_ipc_strerror(error));
45227c478bd9Sstevel@tonic-gate 
45237c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_TIMEOUT)
45247c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_TIMEOUT);
45257c478bd9Sstevel@tonic-gate 		else
45267c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_IF_FAILURE);
45277c478bd9Sstevel@tonic-gate 	}
45287c478bd9Sstevel@tonic-gate 
45297c478bd9Sstevel@tonic-gate 	if (DHCP_IPC_CMD(type) == DHCP_STATUS) {
45307c478bd9Sstevel@tonic-gate 		(void) printf("%s", dhcp_status_hdr_string());
45317c478bd9Sstevel@tonic-gate 		(void) printf("%s", dhcp_status_reply_to_string(reply));
45327c478bd9Sstevel@tonic-gate 	}
45337c478bd9Sstevel@tonic-gate 
45347c478bd9Sstevel@tonic-gate 	free(request);
45357c478bd9Sstevel@tonic-gate 	free(reply);
45367c478bd9Sstevel@tonic-gate 	return (DHCP_EXIT_SUCCESS);
45377c478bd9Sstevel@tonic-gate }
45387c478bd9Sstevel@tonic-gate 
45397c478bd9Sstevel@tonic-gate static void
45407c478bd9Sstevel@tonic-gate usage(void)
45417c478bd9Sstevel@tonic-gate {
45427c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
45437c478bd9Sstevel@tonic-gate 	    "usage: ifconfig <interface> | -a[ 4 | 6 | D ][ u | d ][ Z ]\n");
45447c478bd9Sstevel@tonic-gate 
45457c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s",
45467c478bd9Sstevel@tonic-gate 	    "\t[ <addr_family> ]\n"
45477c478bd9Sstevel@tonic-gate 	    "\t[ <address>[/<prefix_length>] [ <dest_address> ] ]\n"
45487c478bd9Sstevel@tonic-gate 	    "\t[ set [ <address>][/<prefix_length>] ]"
45497c478bd9Sstevel@tonic-gate 	    " [ <address>/<prefix_length>] ]\n"
45507c478bd9Sstevel@tonic-gate 	    "\t[ destination <dest_address> ]\n"
45517c478bd9Sstevel@tonic-gate 	    "\t[ addif <address>[/<prefix_length>]"
45527c478bd9Sstevel@tonic-gate 	    "  [ <dest_address> ] ]\n"
45537c478bd9Sstevel@tonic-gate 	    "\t[ removeif <address>[/<prefix_length>] ]\n"
45547c478bd9Sstevel@tonic-gate 	    "\t[ arp | -arp ]\n"
45557c478bd9Sstevel@tonic-gate 	    "\t[ auto-revarp ]\n"
45567c478bd9Sstevel@tonic-gate 	    "\t[ broadcast <broad_addr> ]\n"
45577c478bd9Sstevel@tonic-gate 	    "\t[ index <if_index> ]\n"
45587c478bd9Sstevel@tonic-gate 	    "\t[ metric <n> ] [ mtu <n> ]\n"
45597c478bd9Sstevel@tonic-gate 	    "\t[ netmask <mask> ]\n"
45607c478bd9Sstevel@tonic-gate 	    "\t[ plumb ] [ unplumb ]\n"
45617c478bd9Sstevel@tonic-gate 	    "\t[ preferred | -preferred ]\n"
45627c478bd9Sstevel@tonic-gate 	    "\t[ private | -private ]\n"
45637c478bd9Sstevel@tonic-gate 	    "\t[ local | -local ]\n"
45647c478bd9Sstevel@tonic-gate 	    "\t[ router | -router ]\n"
45657c478bd9Sstevel@tonic-gate 	    "\t[ subnet <subnet_address>]\n"
45667c478bd9Sstevel@tonic-gate 	    "\t[ trailers | -trailers ]\n"
45677c478bd9Sstevel@tonic-gate 	    "\t[ token <address>/<prefix_length> ]\n"
45687c478bd9Sstevel@tonic-gate 	    "\t[ tsrc <tunnel_src_address> ]\n"
45697c478bd9Sstevel@tonic-gate 	    "\t[ tdst <tunnel_dest_address> ]\n"
45707c478bd9Sstevel@tonic-gate 	    "\t[ auth_algs <tunnel_AH_authentication_algorithm> ]\n"
45717c478bd9Sstevel@tonic-gate 	    "\t[ encr_algs <tunnel_ESP_encryption_algorithm> ]\n"
45727c478bd9Sstevel@tonic-gate 	    "\t[ encr_auth_algs <tunnel_ESP_authentication_algorithm> ]\n"
45737c478bd9Sstevel@tonic-gate 	    "\t[ up ] [ down ]\n"
45747c478bd9Sstevel@tonic-gate 	    "\t[ xmit | -xmit ]\n"
45757c478bd9Sstevel@tonic-gate 	    "\t[ modlist ]\n"
45767c478bd9Sstevel@tonic-gate 	    "\t[ modinsert <module_name@position> ]\n"
45777c478bd9Sstevel@tonic-gate 	    "\t[ modremove <module_name@position> ]\n"
45787c478bd9Sstevel@tonic-gate 	    "\t[ group <groupname>] | [ group \"\"]\n"
45797c478bd9Sstevel@tonic-gate 	    "\t[ deprecated | -deprecated ]\n"
45807c478bd9Sstevel@tonic-gate 	    "\t[ standby | -standby ]\n"
45817c478bd9Sstevel@tonic-gate 	    "\t[ failover | -failover ]\n"
45827c478bd9Sstevel@tonic-gate 	    "\t[ zone <zonename> | -zone ]\n"
458345916cd2Sjpk 	    "\t[ usesrc <interface> ]\n"
458445916cd2Sjpk 	    "\t[ all-zones ]\n");
45857c478bd9Sstevel@tonic-gate 
45867c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "or\n");
45877c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
45887c478bd9Sstevel@tonic-gate 	    "\tifconfig <interface> |  -a[ 4 | 6 | D ] [ u | d ]\n");
45897c478bd9Sstevel@tonic-gate 
45907c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s", "\tauto-dhcp | dhcp\n"
45917c478bd9Sstevel@tonic-gate 	    "\t[ wait <time> | forever ]\n\t[ primary ]\n"
45927c478bd9Sstevel@tonic-gate 	    "\tstart | drop | ping | release | status | inform\n");
45937c478bd9Sstevel@tonic-gate }
4594