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"
26*7906a3e0Smeem #define	TUN_NAME	"tun"
27*7906a3e0Smeem #define	ATUN_NAME	"atun"
28*7906a3e0Smeem #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" },
737c478bd9Sstevel@tonic-gate 	{ IFF_VIRTUAL,		"VIRTUAL"}
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static struct	lifreq lifr;
77*7906a3e0Smeem /* current interface name a particular function is accessing */
787c478bd9Sstevel@tonic-gate static char	name[LIFNAMSIZ];
797c478bd9Sstevel@tonic-gate /* foreach interface saved name */
807c478bd9Sstevel@tonic-gate static char	origname[LIFNAMSIZ];
817c478bd9Sstevel@tonic-gate static char savedname[LIFNAMSIZ];	/* For addif */
827c478bd9Sstevel@tonic-gate static int	setaddr;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * Make sure the algorithm variables hold more than the sizeof an algorithm
867c478bd9Sstevel@tonic-gate  * in PF_KEY.  (For now, more than a uint8_t.)  The NO_***_?ALG indicates that
877c478bd9Sstevel@tonic-gate  * there was no algorithm requested, and in the ipsec_req that service should
887c478bd9Sstevel@tonic-gate  * be disabled.  (E.g. if ah_aalg remains NO_AH_AALG, then AH will be
897c478bd9Sstevel@tonic-gate  * disabled on that tunnel.)
907c478bd9Sstevel@tonic-gate  */
917c478bd9Sstevel@tonic-gate #define	NO_AH_AALG 256
927c478bd9Sstevel@tonic-gate #define	NO_ESP_AALG 256
937c478bd9Sstevel@tonic-gate #define	NO_ESP_EALG 256
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * iface_t
977c478bd9Sstevel@tonic-gate  * used by setifether to create a list of interfaces to mark
987c478bd9Sstevel@tonic-gate  * down-up when changing the ethernet address of an interface
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate typedef struct iface {
1017c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
1027c478bd9Sstevel@tonic-gate 	struct iface *next;	/* pointer to the next list element */
1037c478bd9Sstevel@tonic-gate } iface_t;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static	iface_t	*logifs = NULL; /* list of logical interfaces */
1067c478bd9Sstevel@tonic-gate static 	iface_t	*phyif	= NULL;	/* physical interface */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate int	s;
1097c478bd9Sstevel@tonic-gate int	af = AF_INET;	/* default address family */
1107c478bd9Sstevel@tonic-gate int	debug = 0;
1117c478bd9Sstevel@tonic-gate int	all = 0;	/* setifdhcp() needs to know this */
1127c478bd9Sstevel@tonic-gate int	verbose = 0;
1137c478bd9Sstevel@tonic-gate int	v4compat = 0;	/* Compatible printing format */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * Function prototypes for command functions.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate static int	addif(char *arg, int64_t param);
1197c478bd9Sstevel@tonic-gate static int	inetplumb(char *arg, int64_t param);
1207c478bd9Sstevel@tonic-gate static int	inetunplumb(char *arg, int64_t param);
1217c478bd9Sstevel@tonic-gate static int	removeif(char *arg, int64_t param);
1227c478bd9Sstevel@tonic-gate static int	setdebugflag(char *arg, int64_t param);
1237c478bd9Sstevel@tonic-gate static int	setifaddr(char *arg, int64_t param);
1247c478bd9Sstevel@tonic-gate static int	setifbroadaddr(char *arg, int64_t param);
1257c478bd9Sstevel@tonic-gate static int	setifdstaddr(char *arg, int64_t param);
1267c478bd9Sstevel@tonic-gate static int	setifether(char *arg, int64_t param);
1277c478bd9Sstevel@tonic-gate static int	setifflags(char *arg, int64_t param);
1287c478bd9Sstevel@tonic-gate static int	setifindex(char *arg, int64_t param);
1297c478bd9Sstevel@tonic-gate static int	setifmetric(char *arg, int64_t param);
1307c478bd9Sstevel@tonic-gate static int	setifmtu(char *arg, int64_t param);
1317c478bd9Sstevel@tonic-gate static int	setifnetmask(char *arg, int64_t param);
1327c478bd9Sstevel@tonic-gate static int	setifprefixlen(char *arg, int64_t param);
1337c478bd9Sstevel@tonic-gate static int	setifrevarp(char *arg, int64_t param);
1347c478bd9Sstevel@tonic-gate static int	setifsubnet(char *arg, int64_t param);
1357c478bd9Sstevel@tonic-gate static int	setiftdst(char *arg, int64_t param);
1367c478bd9Sstevel@tonic-gate static int	setiftoken(char *arg, int64_t param);
1377c478bd9Sstevel@tonic-gate static int	setiftsrc(char *arg, int64_t param);
1387c478bd9Sstevel@tonic-gate static int	setverboseflag(char *arg, int64_t param);
1397c478bd9Sstevel@tonic-gate static int	set_tun_ah_alg(char *arg, int64_t param);
1407c478bd9Sstevel@tonic-gate static int	set_tun_esp_auth_alg(char *arg, int64_t param);
1417c478bd9Sstevel@tonic-gate static int	set_tun_esp_encr_alg(char *arg, int64_t param);
1427c478bd9Sstevel@tonic-gate static int	modlist(char *arg, int64_t param);
1437c478bd9Sstevel@tonic-gate static int	modinsert(char *arg, int64_t param);
1447c478bd9Sstevel@tonic-gate static int	modremove(char *arg, int64_t param);
1457c478bd9Sstevel@tonic-gate static int	setifgroupname(char *arg, int64_t param);
1467c478bd9Sstevel@tonic-gate static int	configinfo(char *arg, int64_t param);
1477c478bd9Sstevel@tonic-gate static void	print_config_flags(uint64_t flags);
1487c478bd9Sstevel@tonic-gate static void	print_flags(uint64_t flags);
1497c478bd9Sstevel@tonic-gate static void	print_ifether(char *ifname);
1507c478bd9Sstevel@tonic-gate static int	set_tun_encap_limit(char *arg, int64_t param);
1517c478bd9Sstevel@tonic-gate static int	clr_tun_encap_limit(char *arg, int64_t param);
1527c478bd9Sstevel@tonic-gate static int	set_tun_hop_limit(char *arg, int64_t param);
1537c478bd9Sstevel@tonic-gate static int	setzone(char *arg, int64_t param);
15445916cd2Sjpk static int	setallzones(char *arg, int64_t param);
1557c478bd9Sstevel@tonic-gate static int	setifsrc(char *arg, int64_t param);
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * Address family specific function prototypes.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate static void	in_getaddr(char *s, struct sockaddr *saddr, int *plenp);
1617c478bd9Sstevel@tonic-gate static void	in_status(int force, uint64_t flags);
1627c478bd9Sstevel@tonic-gate static void	in_configinfo(int force, uint64_t flags);
1637c478bd9Sstevel@tonic-gate static void	in6_getaddr(char *s, struct sockaddr *saddr, int *plenp);
1647c478bd9Sstevel@tonic-gate static void	in6_status(int force, uint64_t flags);
1657c478bd9Sstevel@tonic-gate static void	in6_configinfo(int force, uint64_t flags);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * Misc support functions
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate static int	devfs_entry(di_node_t node, di_minor_t minor, void *arg);
1717c478bd9Sstevel@tonic-gate static void	foreachinterface(void (*func)(), int argc, char *argv[],
1727c478bd9Sstevel@tonic-gate 		    int af, int64_t onflags, int64_t offflags,
1737c478bd9Sstevel@tonic-gate 		    int64_t lifc_flags);
1747c478bd9Sstevel@tonic-gate static void	ifconfig(int argc, char *argv[], int af, struct lifreq *lifrp);
1757c478bd9Sstevel@tonic-gate static int	ifdad(char *ifname, struct sockaddr_in6 *laddr);
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 },
278*7906a3e0Smeem 	{ "encaplimit",	NEXTARG,	set_tun_encap_limit,	0, AF_ANY },
279*7906a3e0Smeem 	{ "-encaplimit", 0,		clr_tun_encap_limit,	0, AF_ANY },
280*7906a3e0Smeem 	{ "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;
976*7906a3e0Smeem 			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;
985*7906a3e0Smeem 			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 	 * Catch set of address for AF_INET6 to perform
10107c478bd9Sstevel@tonic-gate 	 * duplicate address detection. Check that the interface is
10117c478bd9Sstevel@tonic-gate 	 * up.
10127c478bd9Sstevel@tonic-gate 	 */
10137c478bd9Sstevel@tonic-gate 	if (afp->af_af == AF_INET6) {
10147c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
10157c478bd9Sstevel@tonic-gate 			Perror0_exit("ifsetaddr: SIOCGLIFFLAGS");
10167c478bd9Sstevel@tonic-gate 		}
10177c478bd9Sstevel@tonic-gate 		if (lifr.lifr_flags & IFF_UP) {
10187c478bd9Sstevel@tonic-gate 			if (debug)
10197c478bd9Sstevel@tonic-gate 				(void) printf(
10207c478bd9Sstevel@tonic-gate 				    "setifaddr: Calling ifdad flags %llx\n",
10217c478bd9Sstevel@tonic-gate 				    lifr.lifr_flags);
10227c478bd9Sstevel@tonic-gate 			if (ifdad(name, (struct sockaddr_in6 *)&laddr) == -1)
10237c478bd9Sstevel@tonic-gate 				exit(3);
10247c478bd9Sstevel@tonic-gate 		}
10257c478bd9Sstevel@tonic-gate 	}
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	/*
10287c478bd9Sstevel@tonic-gate 	 * If setting the address and not the mask, clear any existing mask
10297c478bd9Sstevel@tonic-gate 	 * and the kernel will then assign the default (netmask has been set
10307c478bd9Sstevel@tonic-gate 	 * to 0 in this case).  If setting both (either by using a prefix or
10317c478bd9Sstevel@tonic-gate 	 * using the netmask command), set the mask first, so the address will
10327c478bd9Sstevel@tonic-gate 	 * be interpreted correctly.
10337c478bd9Sstevel@tonic-gate 	 */
1034dd7a6f5fSkcpoon 	set_mask_lifreq(&lifr, &laddr, &netmask);
10357c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
10367c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	if (debug) {
10397c478bd9Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
10407c478bd9Sstevel@tonic-gate 		void *addr = (afp->af_af == AF_INET) ?
10417c478bd9Sstevel@tonic-gate 		    (void *)&((struct sockaddr_in *)&laddr)->sin_addr :
10427c478bd9Sstevel@tonic-gate 		    (void *)&((struct sockaddr_in6 *)&laddr)->sin6_addr;
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 		(void) printf("Setting %s af %d addr %s\n",
10457c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, afp->af_af,
10467c478bd9Sstevel@tonic-gate 		    inet_ntop(afp->af_af, addr, abuf, sizeof (abuf)));
10477c478bd9Sstevel@tonic-gate 	}
10487c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = laddr;
10497c478bd9Sstevel@tonic-gate 	lifr.lifr_addr.ss_family = afp->af_af;
10507c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
10517c478bd9Sstevel@tonic-gate 		/*
10527c478bd9Sstevel@tonic-gate 		 * Restore the netmask
10537c478bd9Sstevel@tonic-gate 		 */
10547c478bd9Sstevel@tonic-gate 		int saverr = errno;
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
10577c478bd9Sstevel@tonic-gate 		lifr.lifr_addr = sav_netmask;
10587c478bd9Sstevel@tonic-gate 		(void) ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr);
10597c478bd9Sstevel@tonic-gate 		errno = saverr;
10607c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFADDR");
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	return (0);
10647c478bd9Sstevel@tonic-gate }
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate /*
10677c478bd9Sstevel@tonic-gate  * The following functions are stolen from the ipseckey(1m) program.
10687c478bd9Sstevel@tonic-gate  * Perhaps they should be somewhere common, but for now, we just maintain
10697c478bd9Sstevel@tonic-gate  * two versions.  We do this because of the different semantics for which
10707c478bd9Sstevel@tonic-gate  * algorithms we select ("requested" for ifconfig vs. "actual" for key).
10717c478bd9Sstevel@tonic-gate  */
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate static ulong_t
10747c478bd9Sstevel@tonic-gate parsenum(char *num)
10757c478bd9Sstevel@tonic-gate {
10767c478bd9Sstevel@tonic-gate 	ulong_t rc;
10777c478bd9Sstevel@tonic-gate 	char *end = NULL;
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	errno = 0;
10807c478bd9Sstevel@tonic-gate 	rc = strtoul(num, &end, 0);
10817c478bd9Sstevel@tonic-gate 	if (errno != 0 || end == num || *end != '\0') {
10827c478bd9Sstevel@tonic-gate 		rc = (ulong_t)-1;
10837c478bd9Sstevel@tonic-gate 	}
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	return (rc);
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate /*
10897c478bd9Sstevel@tonic-gate  * Parse and reverse parse possible algorithm values, include numbers.
10907c478bd9Sstevel@tonic-gate  * Mostly stolen from ipseckey.c. See the comments above parsenum() for why
10917c478bd9Sstevel@tonic-gate  * this isn't common to ipseckey.c.
10927c478bd9Sstevel@tonic-gate  *
10937c478bd9Sstevel@tonic-gate  * NOTE: Static buffer in this function for the return value.  Since ifconfig
10947c478bd9Sstevel@tonic-gate  *	 isn't multithreaded, this isn't a huge problem.
10957c478bd9Sstevel@tonic-gate  */
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate #define	NBUF_SIZE 20	/* Enough to print a large integer. */
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate static char *
11007c478bd9Sstevel@tonic-gate rparsealg(uint8_t alg_value, int proto_num)
11017c478bd9Sstevel@tonic-gate {
11027c478bd9Sstevel@tonic-gate 	struct ipsecalgent *alg;
11037c478bd9Sstevel@tonic-gate 	static char numprint[128];	/* Enough to hold an algorithm name. */
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	/* Special-case 0 to return "<any-none>" */
11067c478bd9Sstevel@tonic-gate 	if (alg_value == 0)
11077c478bd9Sstevel@tonic-gate 		return ("<any-none>");
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	alg = getipsecalgbynum(alg_value, proto_num, NULL);
11107c478bd9Sstevel@tonic-gate 	if (alg != NULL) {
11117c478bd9Sstevel@tonic-gate 		(void) strlcpy(numprint, alg->a_names[0], sizeof (numprint));
11127c478bd9Sstevel@tonic-gate 		freeipsecalgent(alg);
11137c478bd9Sstevel@tonic-gate 	} else {
11147c478bd9Sstevel@tonic-gate 		(void) snprintf(numprint, sizeof (numprint), "%d", alg_value);
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	return (numprint);
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate static uint_t
11217c478bd9Sstevel@tonic-gate parsealg(char *algname, int proto_num)
11227c478bd9Sstevel@tonic-gate {
11237c478bd9Sstevel@tonic-gate 	struct ipsecalgent *alg;
11247c478bd9Sstevel@tonic-gate 	ulong_t invalue;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	if (algname == NULL) {
11277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: Unexpected end of command "
11287c478bd9Sstevel@tonic-gate 		    "line.\n");
11297c478bd9Sstevel@tonic-gate 		exit(1);
11307c478bd9Sstevel@tonic-gate 	}
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	/*
11337c478bd9Sstevel@tonic-gate 	 * Special-case "none". Use strcasecmp because its length is
11347c478bd9Sstevel@tonic-gate 	 * bound.
11357c478bd9Sstevel@tonic-gate 	 */
11367c478bd9Sstevel@tonic-gate 	if (strcasecmp("none", algname) == 0) {
11377c478bd9Sstevel@tonic-gate 		return ((proto_num == IPSEC_PROTO_ESP) ?
11387c478bd9Sstevel@tonic-gate 		    NO_ESP_EALG : NO_ESP_AALG);
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	alg = getipsecalgbyname(algname, proto_num, NULL);
11427c478bd9Sstevel@tonic-gate 	if (alg != NULL) {
11437c478bd9Sstevel@tonic-gate 		invalue = alg->a_alg_num;
11447c478bd9Sstevel@tonic-gate 		freeipsecalgent(alg);
11457c478bd9Sstevel@tonic-gate 		return ((uint_t)invalue);
11467c478bd9Sstevel@tonic-gate 	}
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	/*
11497c478bd9Sstevel@tonic-gate 	 * Since algorithms can be loaded during kernel run-time, check for
11507c478bd9Sstevel@tonic-gate 	 * numeric algorithm values too.
11517c478bd9Sstevel@tonic-gate 	 */
11527c478bd9Sstevel@tonic-gate 	invalue = parsenum(algname);
11537c478bd9Sstevel@tonic-gate 	if ((invalue & (ulong_t)0xff) == invalue)
11547c478bd9Sstevel@tonic-gate 		return ((uint_t)invalue);
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "ifconfig: %s algorithm type %s unknown.\n",
11577c478bd9Sstevel@tonic-gate 	    (proto_num == IPSEC_PROTO_ESP) ?
11587c478bd9Sstevel@tonic-gate 	    "Encryption" : "Authentication", algname);
11597c478bd9Sstevel@tonic-gate 	exit(1);
11607c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate /*
11647c478bd9Sstevel@tonic-gate  * Actual ifconfig functions to set tunnel security properties.
11657c478bd9Sstevel@tonic-gate  */
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate enum ipsec_alg_type { ESP_ENCR_ALG = 1, ESP_AUTH_ALG, AH_AUTH_ALG };
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate boolean_t first_set_tun = _B_TRUE;
11707c478bd9Sstevel@tonic-gate boolean_t encr_alg_set = _B_FALSE;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate static int
11737c478bd9Sstevel@tonic-gate set_tun_algs(int which_alg, int alg)
11747c478bd9Sstevel@tonic-gate {
11757c478bd9Sstevel@tonic-gate 	struct iftun_req treq;
11767c478bd9Sstevel@tonic-gate 	ipsec_req_t *ipsr;
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	(void) strncpy(treq.ifta_lifr_name, name, sizeof (treq.ifta_lifr_name));
11797c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
11807c478bd9Sstevel@tonic-gate 		errno = EPERM;
11817c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
11827c478bd9Sstevel@tonic-gate 	}
11837c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGTUNPARAM, (caddr_t)&treq) < 0) {
11847c478bd9Sstevel@tonic-gate 		if (errno == EOPNOTSUPP || errno == EINVAL)
11857c478bd9Sstevel@tonic-gate 			Perror0_exit("Not a tunnel");
11867c478bd9Sstevel@tonic-gate 		else Perror0_exit("SIOCGTUNPARAM");
11877c478bd9Sstevel@tonic-gate 	}
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	ipsr = (ipsec_req_t *)&treq.ifta_secinfo;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	if (treq.ifta_vers != IFTUN_VERSION) {
11927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11937c478bd9Sstevel@tonic-gate 		    "Kernel tunnel secinfo version mismatch.\n");
11947c478bd9Sstevel@tonic-gate 		exit(1);
11957c478bd9Sstevel@tonic-gate 	}
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	/*
11987c478bd9Sstevel@tonic-gate 	 * If I'm just starting off this ifconfig, I want a clean slate,
11997c478bd9Sstevel@tonic-gate 	 * otherwise, I've captured the current tunnel security settings.
12007c478bd9Sstevel@tonic-gate 	 * In the case of continuation, I merely add to the settings.
12017c478bd9Sstevel@tonic-gate 	 */
12027c478bd9Sstevel@tonic-gate 	if (first_set_tun) {
12037c478bd9Sstevel@tonic-gate 		first_set_tun = _B_FALSE;
12047c478bd9Sstevel@tonic-gate 		(void) memset(ipsr, 0, sizeof (*ipsr));
12057c478bd9Sstevel@tonic-gate 	}
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	treq.ifta_flags = IFTUN_SECURITY;
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 	switch (which_alg) {
12107c478bd9Sstevel@tonic-gate 	case ESP_ENCR_ALG:
12117c478bd9Sstevel@tonic-gate 		if (alg == NO_ESP_EALG) {
12127c478bd9Sstevel@tonic-gate 			if (ipsr->ipsr_esp_auth_alg == SADB_AALG_NONE)
12137c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_req = 0;
12147c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_alg = SADB_EALG_NONE;
12157c478bd9Sstevel@tonic-gate 		} else {
12167c478bd9Sstevel@tonic-gate 			encr_alg_set = _B_TRUE;
12177c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_req =
12187c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12197c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_alg = alg;
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 		break;
12227c478bd9Sstevel@tonic-gate 	case ESP_AUTH_ALG:
12237c478bd9Sstevel@tonic-gate 		if (alg == NO_ESP_AALG) {
12247c478bd9Sstevel@tonic-gate 			if (ipsr->ipsr_esp_alg == SADB_EALG_NONE ||
12257c478bd9Sstevel@tonic-gate 			    ipsr->ipsr_esp_alg == SADB_EALG_NULL)
12267c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_req = 0;
12277c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_auth_alg = SADB_AALG_NONE;
12287c478bd9Sstevel@tonic-gate 		} else {
12297c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_req =
12307c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12317c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_auth_alg = alg;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 			/* Let the user specify NULL encryption implicitly. */
12347c478bd9Sstevel@tonic-gate 			if (ipsr->ipsr_esp_alg == SADB_EALG_NONE &&
12357c478bd9Sstevel@tonic-gate 			    !encr_alg_set)
12367c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_alg = SADB_EALG_NULL;
12377c478bd9Sstevel@tonic-gate 		}
12387c478bd9Sstevel@tonic-gate 		break;
12397c478bd9Sstevel@tonic-gate 	case AH_AUTH_ALG:
12407c478bd9Sstevel@tonic-gate 		if (alg == NO_AH_AALG) {
12417c478bd9Sstevel@tonic-gate 			ipsr->ipsr_ah_req = 0;
12427c478bd9Sstevel@tonic-gate 			ipsr->ipsr_auth_alg = SADB_AALG_NONE;
12437c478bd9Sstevel@tonic-gate 		} else {
12447c478bd9Sstevel@tonic-gate 			ipsr->ipsr_ah_req =
12457c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12467c478bd9Sstevel@tonic-gate 			ipsr->ipsr_auth_alg = alg;
12477c478bd9Sstevel@tonic-gate 		}
12487c478bd9Sstevel@tonic-gate 		break;
12497c478bd9Sstevel@tonic-gate 		/* Will never hit DEFAULT */
12507c478bd9Sstevel@tonic-gate 	}
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSTUNPARAM, (caddr_t)&treq) < 0) {
12537c478bd9Sstevel@tonic-gate 		Perror2_exit("set tunnel security properties",
12547c478bd9Sstevel@tonic-gate 		    treq.ifta_lifr_name);
12557c478bd9Sstevel@tonic-gate 	}
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	return (0);
12587c478bd9Sstevel@tonic-gate }
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate /* ARGSUSED */
12617c478bd9Sstevel@tonic-gate static int
12627c478bd9Sstevel@tonic-gate set_tun_esp_encr_alg(char *addr, int64_t param)
12637c478bd9Sstevel@tonic-gate {
12647c478bd9Sstevel@tonic-gate 	return (set_tun_algs(ESP_ENCR_ALG,
12657c478bd9Sstevel@tonic-gate 		    parsealg(addr, IPSEC_PROTO_ESP)));
12667c478bd9Sstevel@tonic-gate }
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate /* ARGSUSED */
12697c478bd9Sstevel@tonic-gate static int
12707c478bd9Sstevel@tonic-gate set_tun_esp_auth_alg(char *addr, int64_t param)
12717c478bd9Sstevel@tonic-gate {
12727c478bd9Sstevel@tonic-gate 	return (set_tun_algs(ESP_AUTH_ALG,
12737c478bd9Sstevel@tonic-gate 		    parsealg(addr, IPSEC_PROTO_AH)));
12747c478bd9Sstevel@tonic-gate }
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate /* ARGSUSED */
12777c478bd9Sstevel@tonic-gate static int
12787c478bd9Sstevel@tonic-gate set_tun_ah_alg(char *addr, int64_t param)
12797c478bd9Sstevel@tonic-gate {
12807c478bd9Sstevel@tonic-gate 	return (set_tun_algs(AH_AUTH_ALG,
12817c478bd9Sstevel@tonic-gate 		    parsealg(addr, IPSEC_PROTO_AH)));
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate /* ARGSUSED */
12857c478bd9Sstevel@tonic-gate static int
12867c478bd9Sstevel@tonic-gate setifrevarp(char *arg, int64_t param)
12877c478bd9Sstevel@tonic-gate {
12887c478bd9Sstevel@tonic-gate 	struct sockaddr_in	laddr;
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	if (afp->af_af == AF_INET6) {
12917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12927c478bd9Sstevel@tonic-gate 		    "ifconfig: revarp not possible on IPv6 interface %s\n",
12937c478bd9Sstevel@tonic-gate 		    name);
12947c478bd9Sstevel@tonic-gate 		exit(1);
12957c478bd9Sstevel@tonic-gate 	}
12967c478bd9Sstevel@tonic-gate 	if (doifrevarp(name, &laddr)) {
12977c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
12987c478bd9Sstevel@tonic-gate 		laddr.sin_family = AF_INET;
12997c478bd9Sstevel@tonic-gate 		(void) memcpy(&lifr.lifr_addr, &laddr, sizeof (laddr));
13007c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
13017c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCSLIFADDR");
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate 	return (0);
13047c478bd9Sstevel@tonic-gate }
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate /* ARGSUSED */
13077c478bd9Sstevel@tonic-gate static int
13087c478bd9Sstevel@tonic-gate setifsubnet(char *addr, int64_t param)
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
13117c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage subnet;
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, &subnet, &prefixlen);
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
13167c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
13177c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13187c478bd9Sstevel@tonic-gate 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
13197c478bd9Sstevel@tonic-gate 		exit(1);
13207c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
13217c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
13227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13237c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
13247c478bd9Sstevel@tonic-gate 		exit(1);
13257c478bd9Sstevel@tonic-gate 	default:
13267c478bd9Sstevel@tonic-gate 		break;
13277c478bd9Sstevel@tonic-gate 	}
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = subnet;
13307c478bd9Sstevel@tonic-gate 	lifr.lifr_addrlen = prefixlen;
13317c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13327c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFSUBNET, (caddr_t)&lifr) < 0)
13337c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFSUBNET");
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	return (0);
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate /* ARGSUSED */
13397c478bd9Sstevel@tonic-gate static int
13407c478bd9Sstevel@tonic-gate setifnetmask(char *addr, int64_t param)
13417c478bd9Sstevel@tonic-gate {
13427c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask;
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	assert(afp->af_af != AF_INET6);
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	if (strcmp(addr, "+") == 0) {
1347dd7a6f5fSkcpoon 		if (!in_getmask(&netmask, _B_FALSE))
13487c478bd9Sstevel@tonic-gate 			return (0);
1349dd7a6f5fSkcpoon 		(void) printf("Setting netmask of %s to %s\n", name,
1350dd7a6f5fSkcpoon 		    inet_ntoa(netmask.sin_addr));
13517c478bd9Sstevel@tonic-gate 	} else {
13527c478bd9Sstevel@tonic-gate 		in_getaddr(addr, (struct sockaddr *)&netmask, NULL);
13537c478bd9Sstevel@tonic-gate 	}
13547c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13557c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &netmask, sizeof (netmask));
13567c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
13577c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
13587c478bd9Sstevel@tonic-gate 	return (0);
13597c478bd9Sstevel@tonic-gate }
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate /*
13627c478bd9Sstevel@tonic-gate  * Parse '/<n>' as a netmask.
13637c478bd9Sstevel@tonic-gate  */
13647c478bd9Sstevel@tonic-gate /* ARGSUSED */
13657c478bd9Sstevel@tonic-gate static int
13667c478bd9Sstevel@tonic-gate setifprefixlen(char *addr, int64_t param)
13677c478bd9Sstevel@tonic-gate {
13687c478bd9Sstevel@tonic-gate 	int prefixlen;
13697c478bd9Sstevel@tonic-gate 	int af = afp->af_af;
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	prefixlen = in_getprefixlen(addr, _B_TRUE,
1372*7906a3e0Smeem 	    (af == AF_INET) ? IP_ABITS : IPV6_ABITS);
13737c478bd9Sstevel@tonic-gate 	if (prefixlen < 0) {
13747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13757c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
13767c478bd9Sstevel@tonic-gate 		exit(1);
13777c478bd9Sstevel@tonic-gate 	}
13787c478bd9Sstevel@tonic-gate 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
13797c478bd9Sstevel@tonic-gate 	lifr.lifr_addr.ss_family = af;
13807c478bd9Sstevel@tonic-gate 	if (af == AF_INET6) {
13817c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
1384*7906a3e0Smeem 		if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
13857c478bd9Sstevel@tonic-gate 		    (uchar_t *)&sin6->sin6_addr)) {
13867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: "
13877c478bd9Sstevel@tonic-gate 			    "Bad prefix length: %d\n",
13887c478bd9Sstevel@tonic-gate 			    prefixlen);
13897c478bd9Sstevel@tonic-gate 			exit(1);
13907c478bd9Sstevel@tonic-gate 		}
13917c478bd9Sstevel@tonic-gate 	} else if (af == AF_INET) {
13927c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin;
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
1395*7906a3e0Smeem 		if (!in_prefixlentomask(prefixlen, IP_ABITS,
13967c478bd9Sstevel@tonic-gate 		    (uchar_t *)&sin->sin_addr)) {
13977c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: "
13987c478bd9Sstevel@tonic-gate 			    "Bad prefix length: %d\n",
13997c478bd9Sstevel@tonic-gate 			    prefixlen);
14007c478bd9Sstevel@tonic-gate 			exit(1);
14017c478bd9Sstevel@tonic-gate 		}
14027c478bd9Sstevel@tonic-gate 	} else {
14037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: setting prefix only supported"
14047c478bd9Sstevel@tonic-gate 		    " for address family inet or inet6\n");
14057c478bd9Sstevel@tonic-gate 		exit(1);
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14087c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
14097c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
14107c478bd9Sstevel@tonic-gate 	return (0);
14117c478bd9Sstevel@tonic-gate }
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate /* ARGSUSED */
14147c478bd9Sstevel@tonic-gate static int
14157c478bd9Sstevel@tonic-gate setifbroadaddr(char *addr, int64_t param)
14167c478bd9Sstevel@tonic-gate {
14177c478bd9Sstevel@tonic-gate 	struct	sockaddr_in broadaddr;
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	assert(afp->af_af != AF_INET6);
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	if (strcmp(addr, "+") == 0) {
14227c478bd9Sstevel@tonic-gate 		/*
14237c478bd9Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
14247c478bd9Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
14257c478bd9Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
14267c478bd9Sstevel@tonic-gate 		 */
14277c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name,
14287c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
14297c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
14307c478bd9Sstevel@tonic-gate 			if (errno != EADDRNOTAVAIL)
14317c478bd9Sstevel@tonic-gate 				Perror0_exit("SIOCGLIFADDR");
14327c478bd9Sstevel@tonic-gate 			return (0);
14337c478bd9Sstevel@tonic-gate 		}
14347c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
14357c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCGLIFADDR");
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 		return (0);
14387c478bd9Sstevel@tonic-gate 	}
14397c478bd9Sstevel@tonic-gate 	in_getaddr(addr, (struct sockaddr *)&broadaddr, NULL);
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &broadaddr, sizeof (broadaddr));
14427c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14437c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFBRDADDR, (caddr_t)&lifr) < 0)
14447c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFBRDADDR");
14457c478bd9Sstevel@tonic-gate 	return (0);
14467c478bd9Sstevel@tonic-gate }
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate /*
14497c478bd9Sstevel@tonic-gate  * set interface destination address
14507c478bd9Sstevel@tonic-gate  */
14517c478bd9Sstevel@tonic-gate /* ARGSUSED */
14527c478bd9Sstevel@tonic-gate static int
14537c478bd9Sstevel@tonic-gate setifdstaddr(char *addr, int64_t param)
14547c478bd9Sstevel@tonic-gate {
14557c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, (struct sockaddr *)&lifr.lifr_addr, NULL);
14567c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14577c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFDSTADDR, (caddr_t)&lifr) < 0)
14587c478bd9Sstevel@tonic-gate 		Perror0_exit("setifdstaddr: SIOCSLIFDSTADDR");
14597c478bd9Sstevel@tonic-gate 	return (0);
14607c478bd9Sstevel@tonic-gate }
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate /* ARGSUSED */
14637c478bd9Sstevel@tonic-gate static int
14647c478bd9Sstevel@tonic-gate setifflags(char *val, int64_t value)
14657c478bd9Sstevel@tonic-gate {
14667c478bd9Sstevel@tonic-gate 	int phyintlen, origphyintlen;
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14697c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0)
14707c478bd9Sstevel@tonic-gate 		Perror0_exit("setifflags: SIOCGLIFFLAGS");
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	if (value == IFF_NOFAILOVER) {
14737c478bd9Sstevel@tonic-gate 		/*
14747c478bd9Sstevel@tonic-gate 		 * Fail if '-failover' is set after a prior addif created the
14757c478bd9Sstevel@tonic-gate 		 * alias on a different interface. This can happen when the
14767c478bd9Sstevel@tonic-gate 		 * interface is part of an IPMP group.
14777c478bd9Sstevel@tonic-gate 		 */
14787c478bd9Sstevel@tonic-gate 		phyintlen = strcspn(name, ":");
14797c478bd9Sstevel@tonic-gate 		origphyintlen = strcspn(origname, ":");
14807c478bd9Sstevel@tonic-gate 		if (phyintlen != origphyintlen ||
14817c478bd9Sstevel@tonic-gate 		    strncmp(name, origname, phyintlen) != 0) {
14827c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: can't set -failover "
14837c478bd9Sstevel@tonic-gate 			    "on failed/standby/offlined interface %s\n",
14847c478bd9Sstevel@tonic-gate 			    origname);
14857c478bd9Sstevel@tonic-gate 			exit(1);
14867c478bd9Sstevel@tonic-gate 		}
14877c478bd9Sstevel@tonic-gate 	}
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	/*
14907c478bd9Sstevel@tonic-gate 	 * Catch "up" transition for AF_INET6 to perform duplicate address
14917c478bd9Sstevel@tonic-gate 	 * detection. ifdad checks if an address has been set.
14927c478bd9Sstevel@tonic-gate 	 */
14937c478bd9Sstevel@tonic-gate 	if (afp->af_af == AF_INET6 && !(lifr.lifr_flags & IFF_UP) &&
14947c478bd9Sstevel@tonic-gate 	    value == IFF_UP) {
14957c478bd9Sstevel@tonic-gate 		if (debug)
14967c478bd9Sstevel@tonic-gate 			(void) printf(
14977c478bd9Sstevel@tonic-gate 			    "setifaddr:Calling ifdad flags %llx value 0x%llx\n",
14987c478bd9Sstevel@tonic-gate 			    lifr.lifr_flags, value);
14997c478bd9Sstevel@tonic-gate 		if (ifdad(name, NULL) == -1)
15007c478bd9Sstevel@tonic-gate 			exit(1);
15017c478bd9Sstevel@tonic-gate 	}
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	if (value < 0) {
15047c478bd9Sstevel@tonic-gate 		value = -value;
15057c478bd9Sstevel@tonic-gate 		lifr.lifr_flags &= ~value;
15067c478bd9Sstevel@tonic-gate 	} else
15077c478bd9Sstevel@tonic-gate 		lifr.lifr_flags |= value;
15087c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15097c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
15107c478bd9Sstevel@tonic-gate 		Perror0_exit("setifflags: SIOCSLIFFLAGS");
15117c478bd9Sstevel@tonic-gate 	}
15127c478bd9Sstevel@tonic-gate 	return (0);
15137c478bd9Sstevel@tonic-gate }
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate /* ARGSUSED */
15167c478bd9Sstevel@tonic-gate static int
15177c478bd9Sstevel@tonic-gate setifmetric(char *val, int64_t param)
15187c478bd9Sstevel@tonic-gate {
15197c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15207c478bd9Sstevel@tonic-gate 	lifr.lifr_metric = atoi(val);
15217c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFMETRIC, (caddr_t)&lifr) < 0)
15227c478bd9Sstevel@tonic-gate 		Perror0_exit("setifmetric: SIOCSLIFMETRIC");
15237c478bd9Sstevel@tonic-gate 	return (0);
15247c478bd9Sstevel@tonic-gate }
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate /* ARGSUSED */
15277c478bd9Sstevel@tonic-gate static int
15287c478bd9Sstevel@tonic-gate setifmtu(char *val, int64_t param)
15297c478bd9Sstevel@tonic-gate {
15307c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15317c478bd9Sstevel@tonic-gate 	lifr.lifr_mtu = atoi(val);
15327c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFMTU, (caddr_t)&lifr) < 0)
15337c478bd9Sstevel@tonic-gate 		Perror0_exit("setifmtu: SIOCSLIFMTU");
15347c478bd9Sstevel@tonic-gate 	return (0);
15357c478bd9Sstevel@tonic-gate }
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate /* ARGSUSED */
15387c478bd9Sstevel@tonic-gate static int
15397c478bd9Sstevel@tonic-gate setifindex(char *val, int64_t param)
15407c478bd9Sstevel@tonic-gate {
15417c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15427c478bd9Sstevel@tonic-gate 	lifr.lifr_index = atoi(val);
15437c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFINDEX, (caddr_t)&lifr) < 0)
15447c478bd9Sstevel@tonic-gate 		Perror0_exit("setifindex: SIOCSLIFINDEX");
15457c478bd9Sstevel@tonic-gate 	return (0);
15467c478bd9Sstevel@tonic-gate }
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate /* ARGSUSED */
15497c478bd9Sstevel@tonic-gate static int
15507c478bd9Sstevel@tonic-gate setifether(char *addr, int64_t param)
15517c478bd9Sstevel@tonic-gate {
15527c478bd9Sstevel@tonic-gate 	uchar_t	*ea;
15537c478bd9Sstevel@tonic-gate 	iface_t	*current;
15547c478bd9Sstevel@tonic-gate 	int	maclen;
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
15577c478bd9Sstevel@tonic-gate 		ifstatus(name);
15587c478bd9Sstevel@tonic-gate 		print_ifether(name);
15597c478bd9Sstevel@tonic-gate 		return (0);
15607c478bd9Sstevel@tonic-gate 	}
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	phyif = NULL;
15637c478bd9Sstevel@tonic-gate 	logifs = NULL;
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 	/*
15667c478bd9Sstevel@tonic-gate 	 * if the IP interface in the arguments is a logical
15677c478bd9Sstevel@tonic-gate 	 * interface, exit with an error now.
15687c478bd9Sstevel@tonic-gate 	 */
15697c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
15707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: cannot change"
15717c478bd9Sstevel@tonic-gate 		    " ethernet address of a logical interface\n");
15727c478bd9Sstevel@tonic-gate 		exit(1);
15737c478bd9Sstevel@tonic-gate 	}
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	ea = _link_aton(addr, &maclen);
15767c478bd9Sstevel@tonic-gate 	if (ea == NULL) {
15777c478bd9Sstevel@tonic-gate 		if (maclen == -1)
15787c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
15797c478bd9Sstevel@tonic-gate 			    "ifconfig: %s: bad address\n", addr);
15807c478bd9Sstevel@tonic-gate 		else
15817c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: malloc() failed\n");
15827c478bd9Sstevel@tonic-gate 		exit(1);
15837c478bd9Sstevel@tonic-gate 	}
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 	(void) strncpy(savedname, name, sizeof (savedname));
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	/*
15887c478bd9Sstevel@tonic-gate 	 * Call selectifs only for the IP interfaces that are ipv4.
15897c478bd9Sstevel@tonic-gate 	 * offflags == IFF_IPV6 because you should not change the
15907c478bd9Sstevel@tonic-gate 	 *		Ethernet address of an ipv6 interface
15917c478bd9Sstevel@tonic-gate 	 */
15927c478bd9Sstevel@tonic-gate 	foreachinterface(selectifs, 0, (char **)NULL, 0, 0, IFF_IPV6, 0);
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	/* If physical interface not found, exit now */
15957c478bd9Sstevel@tonic-gate 	if (phyif == NULL) {
15967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
15977c478bd9Sstevel@tonic-gate 		    "ifconfig: interface %s not found\n", savedname);
15987c478bd9Sstevel@tonic-gate 		exit(1);
15997c478bd9Sstevel@tonic-gate 	}
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 	/* Restore */
16027c478bd9Sstevel@tonic-gate 	(void) strncpy(name, savedname, sizeof (name));
16037c478bd9Sstevel@tonic-gate 	(void) strncpy(origname, savedname, sizeof (origname));
16047c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	/*
16077c478bd9Sstevel@tonic-gate 	 * close and reopen the socket
16087c478bd9Sstevel@tonic-gate 	 * we don't know which type of socket we have now
16097c478bd9Sstevel@tonic-gate 	 */
16107c478bd9Sstevel@tonic-gate 	(void) close(s);
16117c478bd9Sstevel@tonic-gate 	s = socket(SOCKET_AF(AF_UNSPEC), SOCK_DGRAM, 0);
16127c478bd9Sstevel@tonic-gate 	if (s < 0) {
16137c478bd9Sstevel@tonic-gate 		Perror0_exit("socket");
16147c478bd9Sstevel@tonic-gate 	}
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	/*
16177c478bd9Sstevel@tonic-gate 	 * mark down the logical interfaces first,
16187c478bd9Sstevel@tonic-gate 	 * and then the physical interface
16197c478bd9Sstevel@tonic-gate 	 */
16207c478bd9Sstevel@tonic-gate 	if (updownifs(logifs, 0) < 0 || updownifs(phyif, 0) < 0) {
16217c478bd9Sstevel@tonic-gate 		Perror0_exit("mark down interface failed");
16227c478bd9Sstevel@tonic-gate 	}
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	/*
16257c478bd9Sstevel@tonic-gate 	 * Change the physical address
16267c478bd9Sstevel@tonic-gate 	 */
16277c478bd9Sstevel@tonic-gate 	if (dlpi_set_address(savedname, ea, maclen) == -1) {
16287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
16297c478bd9Sstevel@tonic-gate 		    "ifconfig: failed setting mac address on %s\n",
16307c478bd9Sstevel@tonic-gate 		    savedname);
16317c478bd9Sstevel@tonic-gate 	}
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	/*
16347c478bd9Sstevel@tonic-gate 	 * if any interfaces were marked down before changing the
16357c478bd9Sstevel@tonic-gate 	 * ethernet address, put them up again.
16367c478bd9Sstevel@tonic-gate 	 * First the physical interface, then the logical ones.
16377c478bd9Sstevel@tonic-gate 	 */
16387c478bd9Sstevel@tonic-gate 	if (updownifs(phyif, 1) < 0 || updownifs(logifs, 1) < 0) {
16397c478bd9Sstevel@tonic-gate 		Perror0_exit("mark down interface failed");
16407c478bd9Sstevel@tonic-gate 	}
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	/* Free the memory allocated by selectifs */
16437c478bd9Sstevel@tonic-gate 	free(phyif);
16447c478bd9Sstevel@tonic-gate 	for (current = logifs; current != NULL; current = logifs) {
16457c478bd9Sstevel@tonic-gate 		logifs = logifs->next;
16467c478bd9Sstevel@tonic-gate 		free(current);
16477c478bd9Sstevel@tonic-gate 	}
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	return (0);
16507c478bd9Sstevel@tonic-gate }
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate /*
16537c478bd9Sstevel@tonic-gate  * Print an interface's Ethernet address, if it has one.
16547c478bd9Sstevel@tonic-gate  */
16557c478bd9Sstevel@tonic-gate static void
16567c478bd9Sstevel@tonic-gate print_ifether(char *ifname)
16577c478bd9Sstevel@tonic-gate {
16587c478bd9Sstevel@tonic-gate 	int		protocol;
16597c478bd9Sstevel@tonic-gate 	icfg_if_t	interface;
16607c478bd9Sstevel@tonic-gate 	icfg_handle_t	handle;
16617c478bd9Sstevel@tonic-gate 	int		fd;
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 	fd = socket(AF_INET, SOCK_DGRAM, 0);
16667c478bd9Sstevel@tonic-gate 	if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
16677c478bd9Sstevel@tonic-gate 		/*
16687c478bd9Sstevel@tonic-gate 		 * It's possible the interface is only configured for
16697c478bd9Sstevel@tonic-gate 		 * IPv6; check again with AF_INET6.
16707c478bd9Sstevel@tonic-gate 		 */
16717c478bd9Sstevel@tonic-gate 		(void) close(fd);
16727c478bd9Sstevel@tonic-gate 		fd = socket(AF_INET6, SOCK_DGRAM, 0);
16737c478bd9Sstevel@tonic-gate 		if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
16747c478bd9Sstevel@tonic-gate 			(void) close(fd);
16757c478bd9Sstevel@tonic-gate 			return;
16767c478bd9Sstevel@tonic-gate 		}
16777c478bd9Sstevel@tonic-gate 	}
16787c478bd9Sstevel@tonic-gate 	(void) close(fd);
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	/* Virtual interfaces don't have MAC addresses */
16817c478bd9Sstevel@tonic-gate 	if (lifr.lifr_flags & IFF_VIRTUAL)
16827c478bd9Sstevel@tonic-gate 		return;
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	/*
16857c478bd9Sstevel@tonic-gate 	 * We must be careful to set if_protocol based on the current
16867c478bd9Sstevel@tonic-gate 	 * properties of the interface.  For instance, if "ip.tun0" is
16877c478bd9Sstevel@tonic-gate 	 * configured only as an IPv6 tunnel, then if_protocol must be
16887c478bd9Sstevel@tonic-gate 	 * set to AF_INET6 or icfg_get_tunnel_lower() will fail and
16897c478bd9Sstevel@tonic-gate 	 * we will falsely conclude that it's not a tunnel.
16907c478bd9Sstevel@tonic-gate 	 */
16917c478bd9Sstevel@tonic-gate 	interface.if_protocol = AF_INET;
16927c478bd9Sstevel@tonic-gate 	if (lifr.lifr_flags & IFF_IPV6)
16937c478bd9Sstevel@tonic-gate 		interface.if_protocol = AF_INET6;
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, ifname, sizeof (interface.if_name));
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	if (icfg_open(&handle, &interface) == ICFG_SUCCESS) {
16987c478bd9Sstevel@tonic-gate 		if (icfg_get_tunnel_lower(handle, &protocol) == ICFG_SUCCESS) {
16997c478bd9Sstevel@tonic-gate 			/* Tunnel op succeeded -- it's a tunnel so skip */
17007c478bd9Sstevel@tonic-gate 			icfg_close(handle);
17017c478bd9Sstevel@tonic-gate 			return;
17027c478bd9Sstevel@tonic-gate 		}
17037c478bd9Sstevel@tonic-gate 		icfg_close(handle);
17047c478bd9Sstevel@tonic-gate 	}
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	dlpi_print_address(ifname);
17077c478bd9Sstevel@tonic-gate }
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate /*
17107c478bd9Sstevel@tonic-gate  * static void selectifs(int argc, char *argv[], int af, struct lifreq *rp)
17117c478bd9Sstevel@tonic-gate  *
17127c478bd9Sstevel@tonic-gate  * Called inside setifether() to create a list of interfaces to
17137c478bd9Sstevel@tonic-gate  * mark down/up when changing the Ethernet address.
17147c478bd9Sstevel@tonic-gate  * If the current interface is the physical interface passed
17157c478bd9Sstevel@tonic-gate  * as an argument to ifconfig, update phyif.
17167c478bd9Sstevel@tonic-gate  * If the current interface is a logical interface associated
17177c478bd9Sstevel@tonic-gate  * to the physical interface, add it to the logifs list.
17187c478bd9Sstevel@tonic-gate  */
17197c478bd9Sstevel@tonic-gate /* ARGSUSED */
17207c478bd9Sstevel@tonic-gate static void
17217c478bd9Sstevel@tonic-gate selectifs(int argc, char *argv[], int af, struct lifreq *rp)
17227c478bd9Sstevel@tonic-gate {
17237c478bd9Sstevel@tonic-gate 	char		*colonp;
17247c478bd9Sstevel@tonic-gate 	int		length;
17257c478bd9Sstevel@tonic-gate 	iface_t		*current;
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	/*
17287c478bd9Sstevel@tonic-gate 	 *  savedname=	name of the IP interface to which you want to
17297c478bd9Sstevel@tonic-gate 	 *		change ethernet address
17307c478bd9Sstevel@tonic-gate 	 *  name=	name of the current IP interface
17317c478bd9Sstevel@tonic-gate 	 */
17327c478bd9Sstevel@tonic-gate 	colonp = strchr(name, ':');
17337c478bd9Sstevel@tonic-gate 	if (colonp == NULL)
17347c478bd9Sstevel@tonic-gate 		length = max(strlen(savedname), strlen(name));
17357c478bd9Sstevel@tonic-gate 	else
17367c478bd9Sstevel@tonic-gate 		length = max(strlen(savedname), colonp - name);
17377c478bd9Sstevel@tonic-gate 	if (strncmp(savedname, name, length) == 0) {
17387c478bd9Sstevel@tonic-gate 		(void) strcpy(lifr.lifr_name, name);
17397c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFFLAGS, &lifr) < 0) {
17407c478bd9Sstevel@tonic-gate 			Perror0("selectifs: SIOCGLIFFLAGS");
17417c478bd9Sstevel@tonic-gate 			return;
17427c478bd9Sstevel@tonic-gate 		}
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 		if ((current = malloc(sizeof (iface_t))) == NULL) {
17457c478bd9Sstevel@tonic-gate 			Perror0_exit("selectifs: malloc failed\n");
17467c478bd9Sstevel@tonic-gate 		}
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 		if (colonp == NULL) {
17497c478bd9Sstevel@tonic-gate 			/* this is the physical interface */
17507c478bd9Sstevel@tonic-gate 			phyif = current;
17517c478bd9Sstevel@tonic-gate 			bcopy(&lifr, &phyif->lifr, sizeof (struct lifreq));
17527c478bd9Sstevel@tonic-gate 			phyif->next = NULL;
17537c478bd9Sstevel@tonic-gate 		} else {
17547c478bd9Sstevel@tonic-gate 			/* this is a logical interface */
17557c478bd9Sstevel@tonic-gate 			bcopy(&lifr, &current->lifr, sizeof (struct lifreq));
17567c478bd9Sstevel@tonic-gate 			current->next = logifs;
17577c478bd9Sstevel@tonic-gate 			logifs = current;
17587c478bd9Sstevel@tonic-gate 		}
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate }
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate /*
17637c478bd9Sstevel@tonic-gate  * static int updownifs(iface_t *ifs, int up)
17647c478bd9Sstevel@tonic-gate  *
17657c478bd9Sstevel@tonic-gate  * It takes in input a list of IP interfaces (ifs)
17667c478bd9Sstevel@tonic-gate  * and a flag (up).
17677c478bd9Sstevel@tonic-gate  * It marks each interface in the list down (up = 0)
17687c478bd9Sstevel@tonic-gate  * or up (up > 0). This is done ONLY if the IP
17697c478bd9Sstevel@tonic-gate  * interface was originally up.
17707c478bd9Sstevel@tonic-gate  *
17717c478bd9Sstevel@tonic-gate  * Return values:
17727c478bd9Sstevel@tonic-gate  *  0 = everything OK
17737c478bd9Sstevel@tonic-gate  * -1 = problem
17747c478bd9Sstevel@tonic-gate  */
17757c478bd9Sstevel@tonic-gate static int
17767c478bd9Sstevel@tonic-gate updownifs(iface_t *ifs, int up)
17777c478bd9Sstevel@tonic-gate {
17787c478bd9Sstevel@tonic-gate 	iface_t *current;
17797c478bd9Sstevel@tonic-gate 	int	ret = 0;
17807c478bd9Sstevel@tonic-gate 	int 	save_errno;
17817c478bd9Sstevel@tonic-gate 	char	savename[LIFNAMSIZ];
17827c478bd9Sstevel@tonic-gate 	uint64_t orig_flags;
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	for (current = ifs; current != NULL; current = current->next) {
17857c478bd9Sstevel@tonic-gate 		if (current->lifr.lifr_flags & IFF_UP) {
17867c478bd9Sstevel@tonic-gate 			orig_flags = current->lifr.lifr_flags;
17877c478bd9Sstevel@tonic-gate 			if (!up)
17887c478bd9Sstevel@tonic-gate 				current->lifr.lifr_flags &= ~IFF_UP;
17897c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCSLIFFLAGS, &current->lifr) < 0) {
17907c478bd9Sstevel@tonic-gate 				save_errno = errno;
17917c478bd9Sstevel@tonic-gate 				(void) strcpy(savename,
17927c478bd9Sstevel@tonic-gate 				    current->lifr.lifr_name);
17937c478bd9Sstevel@tonic-gate 				ret = -1;
17947c478bd9Sstevel@tonic-gate 			}
17957c478bd9Sstevel@tonic-gate 			if (!up) /* restore the original flags */
17967c478bd9Sstevel@tonic-gate 				current->lifr.lifr_flags = orig_flags;
17977c478bd9Sstevel@tonic-gate 		}
17987c478bd9Sstevel@tonic-gate 	}
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	if (ret == -1) {
18017c478bd9Sstevel@tonic-gate 		(void) strcpy(lifr.lifr_name, savename);
18027c478bd9Sstevel@tonic-gate 		errno = save_errno;
18037c478bd9Sstevel@tonic-gate 	}
18047c478bd9Sstevel@tonic-gate 	return (ret);
18057c478bd9Sstevel@tonic-gate }
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate /*
18087c478bd9Sstevel@tonic-gate  * Create the next unused logical interface using the original name
18097c478bd9Sstevel@tonic-gate  * and assign the address (and mask if '/<n>' is part of the address).
18107c478bd9Sstevel@tonic-gate  * Use the new logical interface for subsequent subcommands by updating
18117c478bd9Sstevel@tonic-gate  * the name variable.
18127c478bd9Sstevel@tonic-gate  *
18137c478bd9Sstevel@tonic-gate  * This allows syntax like:
18147c478bd9Sstevel@tonic-gate  *	ifconfig le0 addif 109.106.86.130 netmask + up \
18157c478bd9Sstevel@tonic-gate  *	addif 109.106.86.131 netmask + up
18167c478bd9Sstevel@tonic-gate  */
18177c478bd9Sstevel@tonic-gate /* ARGSUSED */
18187c478bd9Sstevel@tonic-gate static int
18197c478bd9Sstevel@tonic-gate addif(char *str, int64_t param)
18207c478bd9Sstevel@tonic-gate {
18217c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
18227c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
18237c478bd9Sstevel@tonic-gate 	struct sockaddr_storage mask;
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	(void) strncpy(name, origname, sizeof (name));
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
18287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18297c478bd9Sstevel@tonic-gate 		    "ifconfig: addif: bad physical interface name %s\n",
18307c478bd9Sstevel@tonic-gate 		    name);
18317c478bd9Sstevel@tonic-gate 		exit(1);
18327c478bd9Sstevel@tonic-gate 	}
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	/*
18357c478bd9Sstevel@tonic-gate 	 * clear so parser will interpret next address as source followed
18367c478bd9Sstevel@tonic-gate 	 * by possible dest
18377c478bd9Sstevel@tonic-gate 	 */
18387c478bd9Sstevel@tonic-gate 	setaddr = 0;
18397c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(str, (struct sockaddr *)&laddr, &prefixlen);
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
18427c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
18437c478bd9Sstevel@tonic-gate 		/* Nothing there - ok */
18447c478bd9Sstevel@tonic-gate 		break;
18457c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
18467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18477c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", str);
18487c478bd9Sstevel@tonic-gate 		exit(1);
18497c478bd9Sstevel@tonic-gate 	default:
18507c478bd9Sstevel@tonic-gate 		(void) memset(&mask, 0, sizeof (mask));
18517c478bd9Sstevel@tonic-gate 		mask.ss_family = afp->af_af;
18527c478bd9Sstevel@tonic-gate 		if (afp->af_af == AF_INET6) {
18537c478bd9Sstevel@tonic-gate 			struct sockaddr_in6 *sin6;
18547c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&mask;
1855*7906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
18567c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin6->sin6_addr)) {
18577c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
18587c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
18597c478bd9Sstevel@tonic-gate 				    prefixlen);
18607c478bd9Sstevel@tonic-gate 				exit(1);
18617c478bd9Sstevel@tonic-gate 			}
18627c478bd9Sstevel@tonic-gate 		} else {
18637c478bd9Sstevel@tonic-gate 			struct sockaddr_in *sin;
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 			sin = (struct sockaddr_in *)&mask;
1866*7906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IP_ABITS,
18677c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin->sin_addr)) {
18687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
18697c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
18707c478bd9Sstevel@tonic-gate 				    prefixlen);
18717c478bd9Sstevel@tonic-gate 				exit(1);
18727c478bd9Sstevel@tonic-gate 			}
18737c478bd9Sstevel@tonic-gate 		}
1874dd7a6f5fSkcpoon 		g_netmask_set = G_NETMASK_NIL;
18757c478bd9Sstevel@tonic-gate 		break;
18767c478bd9Sstevel@tonic-gate 	}
18777c478bd9Sstevel@tonic-gate 
1878dd7a6f5fSkcpoon 	/*
1879dd7a6f5fSkcpoon 	 * This is a "hack" to get around the problem of SIOCLIFADDIF.  The
1880dd7a6f5fSkcpoon 	 * problem is that this ioctl does not include the netmask when
1881dd7a6f5fSkcpoon 	 * adding a logical interface.  This is the same problem described
1882dd7a6f5fSkcpoon 	 * in the ifconfig() comments.  To get around this problem, we first
1883dd7a6f5fSkcpoon 	 * add the logical interface with a 0 address.  After that, we set
1884dd7a6f5fSkcpoon 	 * the netmask if provided.  Finally we set the interface address.
1885dd7a6f5fSkcpoon 	 */
18867c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1887dd7a6f5fSkcpoon 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	/* Note: no need to do DAD here since the interface isn't up yet. */
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0)
18927c478bd9Sstevel@tonic-gate 		Perror0_exit("addif: SIOCLIFADDIF");
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	(void) printf("Created new logical interface %s\n",
18957c478bd9Sstevel@tonic-gate 	    lifr.lifr_name);
18967c478bd9Sstevel@tonic-gate 	(void) strncpy(name, lifr.lifr_name, sizeof (name));
18977c478bd9Sstevel@tonic-gate 
1898dd7a6f5fSkcpoon 	/*
1899dd7a6f5fSkcpoon 	 * Check and see if any "netmask" command is used and perform the
1900dd7a6f5fSkcpoon 	 * necessary operation.
1901dd7a6f5fSkcpoon 	 */
1902dd7a6f5fSkcpoon 	set_mask_lifreq(&lifr, &laddr, &mask);
1903dd7a6f5fSkcpoon 	/*
1904dd7a6f5fSkcpoon 	 * Only set the netmask if "netmask" command is used or a prefix is
1905dd7a6f5fSkcpoon 	 * provided.
1906dd7a6f5fSkcpoon 	 */
1907dd7a6f5fSkcpoon 	if (g_netmask_set == G_NETMASK_SET || prefixlen >= 0) {
19087c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
19097c478bd9Sstevel@tonic-gate 			Perror0_exit("addif: SIOCSLIFNETMASK");
19107c478bd9Sstevel@tonic-gate 	}
1911dd7a6f5fSkcpoon 
1912dd7a6f5fSkcpoon 	/* Finally, we set the interface address. */
1913dd7a6f5fSkcpoon 	lifr.lifr_addr = laddr;
1914dd7a6f5fSkcpoon 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1915dd7a6f5fSkcpoon 		Perror0_exit("SIOCSLIFADDR");
1916dd7a6f5fSkcpoon 
19177c478bd9Sstevel@tonic-gate 	/*
19187c478bd9Sstevel@tonic-gate 	 * let parser know we got a source.
19197c478bd9Sstevel@tonic-gate 	 * Next address, if given, should be dest
19207c478bd9Sstevel@tonic-gate 	 */
19217c478bd9Sstevel@tonic-gate 	setaddr++;
19227c478bd9Sstevel@tonic-gate 	return (0);
19237c478bd9Sstevel@tonic-gate }
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate /*
19267c478bd9Sstevel@tonic-gate  * Remove a logical interface based on its IP address. Unlike addif
19277c478bd9Sstevel@tonic-gate  * there is no '/<n>' here.
19287c478bd9Sstevel@tonic-gate  * Verifies that the interface is down before it is removed.
19297c478bd9Sstevel@tonic-gate  */
19307c478bd9Sstevel@tonic-gate /* ARGSUSED */
19317c478bd9Sstevel@tonic-gate static int
19327c478bd9Sstevel@tonic-gate removeif(char *str, int64_t param)
19337c478bd9Sstevel@tonic-gate {
19347c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
19377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
19387c478bd9Sstevel@tonic-gate 		    "ifconfig: removeif: bad physical interface name %s\n",
19397c478bd9Sstevel@tonic-gate 		    name);
19407c478bd9Sstevel@tonic-gate 		exit(1);
19417c478bd9Sstevel@tonic-gate 	}
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(str, &laddr, NULL);
19447c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = laddr;
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
19477c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) {
19487c478bd9Sstevel@tonic-gate 		if (errno == EBUSY) {
19497c478bd9Sstevel@tonic-gate 			/* This can only happen if ipif_id = 0 */
19507c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
19517c478bd9Sstevel@tonic-gate 			    "ifconfig: removeif: can't remove interface: %s\n",
19527c478bd9Sstevel@tonic-gate 			    name);
19537c478bd9Sstevel@tonic-gate 			exit(1);
19547c478bd9Sstevel@tonic-gate 		}
19557c478bd9Sstevel@tonic-gate 		Perror0_exit("removeif: SIOCLIFREMOVEIF");
19567c478bd9Sstevel@tonic-gate 	}
19577c478bd9Sstevel@tonic-gate 	return (0);
19587c478bd9Sstevel@tonic-gate }
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate /*
19617c478bd9Sstevel@tonic-gate  * If laddr is non-NULL it is used - otherwise we use the address on
19627c478bd9Sstevel@tonic-gate  * the interface.
19637c478bd9Sstevel@tonic-gate  */
19647c478bd9Sstevel@tonic-gate /* ARGSUSED */
19657c478bd9Sstevel@tonic-gate static int
19667c478bd9Sstevel@tonic-gate ifdad(char *ifname, struct sockaddr_in6 *laddr)
19677c478bd9Sstevel@tonic-gate {
19687c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 testaddr;
19697c478bd9Sstevel@tonic-gate 	struct lifreq lifr2;	/* Avoid overriting lifr */
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	if (debug)
19727c478bd9Sstevel@tonic-gate 		(void) printf("ifdad(%s)\n", ifname);
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	assert(afp->af_af == AF_INET6);
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 	/*
19777c478bd9Sstevel@tonic-gate 	 * Check the address assigned to the interface.
19787c478bd9Sstevel@tonic-gate 	 * Skip the check if IFF_NOLOCAL, IFF_NONUD, IFF_ANYCAST, or
19797c478bd9Sstevel@tonic-gate 	 * IFF_LOOPBACK.
19807c478bd9Sstevel@tonic-gate 	 * Note that IFF_NONUD turns of both NUD and DAD.
19817c478bd9Sstevel@tonic-gate 	 */
19827c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr2.lifr_name, ifname,
19837c478bd9Sstevel@tonic-gate 	    sizeof (lifr2.lifr_name));
19847c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr2) < 0) {
19857c478bd9Sstevel@tonic-gate 		Perror0_exit("ifdad: SIOCGLIFFLAGS");
19867c478bd9Sstevel@tonic-gate 	}
19877c478bd9Sstevel@tonic-gate 	if (lifr2.lifr_flags & (IFF_NOLOCAL|IFF_LOOPBACK|IFF_NONUD|IFF_ANYCAST))
19887c478bd9Sstevel@tonic-gate 		return (0);
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	if (laddr != NULL) {
19917c478bd9Sstevel@tonic-gate 		testaddr = *laddr;
19927c478bd9Sstevel@tonic-gate 	} else {
19937c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr2) < 0) {
19947c478bd9Sstevel@tonic-gate 			Perror0_exit("ifdad: SIOCGLIFADDR");
19957c478bd9Sstevel@tonic-gate 		}
19967c478bd9Sstevel@tonic-gate 		testaddr = *(struct sockaddr_in6 *)&lifr2.lifr_addr;
19977c478bd9Sstevel@tonic-gate 	}
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(&testaddr.sin6_addr))
20007c478bd9Sstevel@tonic-gate 		return (0);
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 	if (do_dad(name, &testaddr) != 0)
20037c478bd9Sstevel@tonic-gate 		return (-1);
20047c478bd9Sstevel@tonic-gate 	else
20057c478bd9Sstevel@tonic-gate 		return (0);
20067c478bd9Sstevel@tonic-gate }
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate /*
20097c478bd9Sstevel@tonic-gate  * Set the address token for IPv6.
20107c478bd9Sstevel@tonic-gate  */
20117c478bd9Sstevel@tonic-gate /* ARGSUSED */
20127c478bd9Sstevel@tonic-gate static int
20137c478bd9Sstevel@tonic-gate setiftoken(char *addr, int64_t param)
20147c478bd9Sstevel@tonic-gate {
20157c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
20167c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 token;
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	in6_getaddr(addr, (struct sockaddr *)&token, &prefixlen);
20197c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
20207c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
20217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
20227c478bd9Sstevel@tonic-gate 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
20237c478bd9Sstevel@tonic-gate 		exit(1);
20247c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
20257c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
20267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
20277c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
20287c478bd9Sstevel@tonic-gate 		exit(1);
20297c478bd9Sstevel@tonic-gate 	default:
20307c478bd9Sstevel@tonic-gate 		break;
20317c478bd9Sstevel@tonic-gate 	}
20327c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &token, sizeof (token));
20337c478bd9Sstevel@tonic-gate 	lifr.lifr_addrlen = prefixlen;
20347c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
20357c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFTOKEN, (caddr_t)&lifr) < 0)  {
20367c478bd9Sstevel@tonic-gate 		Perror0_exit("setiftoken: SIOCSLIFTOKEN");
20377c478bd9Sstevel@tonic-gate 	}
20387c478bd9Sstevel@tonic-gate 	return (0);
20397c478bd9Sstevel@tonic-gate }
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate /*
20427c478bd9Sstevel@tonic-gate  * Return value: 0 on success, -1 on failure.
20437c478bd9Sstevel@tonic-gate  */
20447c478bd9Sstevel@tonic-gate static int
20457c478bd9Sstevel@tonic-gate connect_to_mpathd(int family)
20467c478bd9Sstevel@tonic-gate {
20477c478bd9Sstevel@tonic-gate 	int s;
20487c478bd9Sstevel@tonic-gate 	struct sockaddr_storage ss;
20497c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
20507c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
20517c478bd9Sstevel@tonic-gate 	struct in6_addr loopback_addr = IN6ADDR_LOOPBACK_INIT;
20527c478bd9Sstevel@tonic-gate 	int addrlen;
20537c478bd9Sstevel@tonic-gate 	int ret;
20547c478bd9Sstevel@tonic-gate 	int on;
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate 	s = socket(family, SOCK_STREAM, 0);
20577c478bd9Sstevel@tonic-gate 	if (s < 0) {
20587c478bd9Sstevel@tonic-gate 		Perror0_exit("connect_to_mpathd: socket");
20597c478bd9Sstevel@tonic-gate 	}
20607c478bd9Sstevel@tonic-gate 	(void) bzero((char *)&ss, sizeof (ss));
20617c478bd9Sstevel@tonic-gate 	ss.ss_family = family;
20627c478bd9Sstevel@tonic-gate 	/*
20637c478bd9Sstevel@tonic-gate 	 * Need to bind to a privileged port. For non-root, this
20647c478bd9Sstevel@tonic-gate 	 * will fail. in.mpathd verifies that only commands coming
20657c478bd9Sstevel@tonic-gate 	 * from privileged ports succeed so that ordinary users
20667c478bd9Sstevel@tonic-gate 	 * can't connect and start talking to in.mpathd
20677c478bd9Sstevel@tonic-gate 	 */
20687c478bd9Sstevel@tonic-gate 	on = 1;
20697c478bd9Sstevel@tonic-gate 	if (setsockopt(s, IPPROTO_TCP, TCP_ANONPRIVBIND, &on,
20707c478bd9Sstevel@tonic-gate 	    sizeof (on)) < 0) {
20717c478bd9Sstevel@tonic-gate 		Perror0_exit("connect_to_mpathd: setsockopt");
20727c478bd9Sstevel@tonic-gate 	}
20737c478bd9Sstevel@tonic-gate 	switch (family) {
20747c478bd9Sstevel@tonic-gate 	case AF_INET:
20757c478bd9Sstevel@tonic-gate 		sin->sin_port = 0;
20767c478bd9Sstevel@tonic-gate 		sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
20777c478bd9Sstevel@tonic-gate 		addrlen = sizeof (struct sockaddr_in);
20787c478bd9Sstevel@tonic-gate 		break;
20797c478bd9Sstevel@tonic-gate 	case AF_INET6:
20807c478bd9Sstevel@tonic-gate 		sin6->sin6_port = 0;
20817c478bd9Sstevel@tonic-gate 		sin6->sin6_addr = loopback_addr;
20827c478bd9Sstevel@tonic-gate 		addrlen = sizeof (struct sockaddr_in6);
20837c478bd9Sstevel@tonic-gate 		break;
20847c478bd9Sstevel@tonic-gate 	}
20857c478bd9Sstevel@tonic-gate 	ret = bind(s, (struct sockaddr *)&ss, addrlen);
20867c478bd9Sstevel@tonic-gate 	if (ret != 0) {
20877c478bd9Sstevel@tonic-gate 		(void) close(s);
20887c478bd9Sstevel@tonic-gate 		return (-1);
20897c478bd9Sstevel@tonic-gate 	}
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	switch (family) {
20927c478bd9Sstevel@tonic-gate 	case AF_INET:
20937c478bd9Sstevel@tonic-gate 		sin->sin_port = htons(MPATHD_PORT);
20947c478bd9Sstevel@tonic-gate 		break;
20957c478bd9Sstevel@tonic-gate 	case AF_INET6:
20967c478bd9Sstevel@tonic-gate 		sin6->sin6_port = htons(MPATHD_PORT);
20977c478bd9Sstevel@tonic-gate 		break;
20987c478bd9Sstevel@tonic-gate 	}
20997c478bd9Sstevel@tonic-gate 	ret = connect(s, (struct sockaddr *)&ss, addrlen);
21007c478bd9Sstevel@tonic-gate 	(void) close(s);
21017c478bd9Sstevel@tonic-gate 	return (ret);
21027c478bd9Sstevel@tonic-gate }
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate /* ARGSUSED */
21057c478bd9Sstevel@tonic-gate static int
21067c478bd9Sstevel@tonic-gate setifgroupname(char *grpname, int64_t param)
21077c478bd9Sstevel@tonic-gate {
21087c478bd9Sstevel@tonic-gate 	if (debug) {
21097c478bd9Sstevel@tonic-gate 		(void) printf("Setting groupname %s on interface %s\n",
21107c478bd9Sstevel@tonic-gate 		    grpname, name);
21117c478bd9Sstevel@tonic-gate 	}
21127c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
21137c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_groupname, grpname,
21147c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_groupname));
21157c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFGROUPNAME, (caddr_t)&lifr) < 0) {
21167c478bd9Sstevel@tonic-gate 		Perror0_exit("setifgroupname: SIOCSLIFGROUPNAME");
21177c478bd9Sstevel@tonic-gate 	}
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 	/*
21207c478bd9Sstevel@tonic-gate 	 * If the SUNW_NO_MPATHD environment variable is set then don't
21217c478bd9Sstevel@tonic-gate 	 * bother starting up in.mpathd.  See PSARC/2002/249 for the
21227c478bd9Sstevel@tonic-gate 	 * depressing details on this bit of stupidity.
21237c478bd9Sstevel@tonic-gate 	 */
21247c478bd9Sstevel@tonic-gate 	if (getenv("SUNW_NO_MPATHD") != NULL) {
21257c478bd9Sstevel@tonic-gate 		return (0);
21267c478bd9Sstevel@tonic-gate 	}
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	/*
21297c478bd9Sstevel@tonic-gate 	 * Try to connect to in.mpathd using IPv4. If we succeed,
21307c478bd9Sstevel@tonic-gate 	 * we conclude that in.mpathd is running, and quit.
21317c478bd9Sstevel@tonic-gate 	 */
21327c478bd9Sstevel@tonic-gate 	if (connect_to_mpathd(AF_INET) == 0) {
21337c478bd9Sstevel@tonic-gate 		/* connect succeeded, mpathd is already running */
21347c478bd9Sstevel@tonic-gate 		return (0);
21357c478bd9Sstevel@tonic-gate 	}
21367c478bd9Sstevel@tonic-gate 	/*
21377c478bd9Sstevel@tonic-gate 	 * Try to connect to in.mpathd using IPv6. If we succeed,
21387c478bd9Sstevel@tonic-gate 	 * we conclude that in.mpathd is running, and quit.
21397c478bd9Sstevel@tonic-gate 	 */
21407c478bd9Sstevel@tonic-gate 	if (connect_to_mpathd(AF_INET6) == 0) {
21417c478bd9Sstevel@tonic-gate 		/* connect succeeded, mpathd is already running */
21427c478bd9Sstevel@tonic-gate 		return (0);
21437c478bd9Sstevel@tonic-gate 	}
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 	/*
21467c478bd9Sstevel@tonic-gate 	 * in.mpathd may not be running. Start it now. If it is already
21477c478bd9Sstevel@tonic-gate 	 * running, in.mpathd will take care of handling multiple incarnations
21487c478bd9Sstevel@tonic-gate 	 * of itself. ifconfig only tries to optimize performance by not
21497c478bd9Sstevel@tonic-gate 	 * starting another incarnation of in.mpathd.
21507c478bd9Sstevel@tonic-gate 	 */
21517c478bd9Sstevel@tonic-gate 	switch (fork()) {
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 	case -1:
21547c478bd9Sstevel@tonic-gate 		Perror0_exit("setifgroupname: fork");
21557c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
21567c478bd9Sstevel@tonic-gate 	case 0:
21577c478bd9Sstevel@tonic-gate 		(void) execl(MPATHD_PATH, MPATHD_PATH, NULL);
21587c478bd9Sstevel@tonic-gate 		_exit(1);
21597c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
21607c478bd9Sstevel@tonic-gate 	default:
21617c478bd9Sstevel@tonic-gate 		return (0);
21627c478bd9Sstevel@tonic-gate 	}
21637c478bd9Sstevel@tonic-gate }
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate /*
21677c478bd9Sstevel@tonic-gate  * To list all the modules above a given network interface.
21687c478bd9Sstevel@tonic-gate  */
21697c478bd9Sstevel@tonic-gate /* ARGSUSED */
21707c478bd9Sstevel@tonic-gate static int
21717c478bd9Sstevel@tonic-gate modlist(char *null, int64_t param)
21727c478bd9Sstevel@tonic-gate {
21737c478bd9Sstevel@tonic-gate 	int muxfd;
21747c478bd9Sstevel@tonic-gate 	int ipfd_lowstr;
21757c478bd9Sstevel@tonic-gate 	int arpfd_lowstr;
21767c478bd9Sstevel@tonic-gate 	int num_mods;
21777c478bd9Sstevel@tonic-gate 	int i;
21787c478bd9Sstevel@tonic-gate 	struct str_list strlist;
21797c478bd9Sstevel@tonic-gate 	int orig_arpid;
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
21827c478bd9Sstevel@tonic-gate 	if (ip_domux2fd(&muxfd, &ipfd_lowstr, &arpfd_lowstr,
21837c478bd9Sstevel@tonic-gate 	    &orig_arpid) < 0) {
21847c478bd9Sstevel@tonic-gate 		return (-1);
21857c478bd9Sstevel@tonic-gate 	}
21867c478bd9Sstevel@tonic-gate 	if ((num_mods = ioctl(ipfd_lowstr, I_LIST, NULL)) < 0) {
21877c478bd9Sstevel@tonic-gate 		Perror0("cannot I_LIST to get the number of modules");
21887c478bd9Sstevel@tonic-gate 	} else {
21897c478bd9Sstevel@tonic-gate 		if (debug > 0) {
21907c478bd9Sstevel@tonic-gate 			(void) printf("Listing (%d) modules above %s\n",
21917c478bd9Sstevel@tonic-gate 			    num_mods, name);
21927c478bd9Sstevel@tonic-gate 		}
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 		strlist.sl_nmods = num_mods;
21957c478bd9Sstevel@tonic-gate 		strlist.sl_modlist = malloc(sizeof (struct str_mlist) *
21967c478bd9Sstevel@tonic-gate 		    num_mods);
21977c478bd9Sstevel@tonic-gate 		if (strlist.sl_modlist == NULL) {
21987c478bd9Sstevel@tonic-gate 			Perror0("cannot malloc");
21997c478bd9Sstevel@tonic-gate 		} else {
22007c478bd9Sstevel@tonic-gate 			if (ioctl(ipfd_lowstr, I_LIST, (caddr_t)&strlist) < 0) {
22017c478bd9Sstevel@tonic-gate 				Perror0("cannot I_LIST for module names");
22027c478bd9Sstevel@tonic-gate 			} else {
22037c478bd9Sstevel@tonic-gate 				for (i = 0; i < strlist.sl_nmods; i++) {
22047c478bd9Sstevel@tonic-gate 				    (void) printf("%d %s\n", i,
22057c478bd9Sstevel@tonic-gate 				    strlist.sl_modlist[i].l_name);
22067c478bd9Sstevel@tonic-gate 				}
22077c478bd9Sstevel@tonic-gate 			}
22087c478bd9Sstevel@tonic-gate 			free(strlist.sl_modlist);
22097c478bd9Sstevel@tonic-gate 		}
22107c478bd9Sstevel@tonic-gate 	}
22117c478bd9Sstevel@tonic-gate 	return (ip_plink(muxfd, ipfd_lowstr, arpfd_lowstr, orig_arpid));
22127c478bd9Sstevel@tonic-gate }
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate #define	MODINSERT_OP	'i'
22157c478bd9Sstevel@tonic-gate #define	MODREMOVE_OP	'r'
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate /*
22187c478bd9Sstevel@tonic-gate  * To insert a module to the stream of the interface.  It is just a
22197c478bd9Sstevel@tonic-gate  * wrapper.  The real function is modop().
22207c478bd9Sstevel@tonic-gate  */
22217c478bd9Sstevel@tonic-gate /* ARGSUSED */
22227c478bd9Sstevel@tonic-gate static int
22237c478bd9Sstevel@tonic-gate modinsert(char *arg, int64_t param)
22247c478bd9Sstevel@tonic-gate {
22257c478bd9Sstevel@tonic-gate 	return (modop(arg, MODINSERT_OP));
22267c478bd9Sstevel@tonic-gate }
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate /*
22297c478bd9Sstevel@tonic-gate  * To remove a module from the stream of the interface.  It is just a
22307c478bd9Sstevel@tonic-gate  * wrapper.  The real function is modop().
22317c478bd9Sstevel@tonic-gate  */
22327c478bd9Sstevel@tonic-gate /* ARGSUSED */
22337c478bd9Sstevel@tonic-gate static int
22347c478bd9Sstevel@tonic-gate modremove(char *arg, int64_t param)
22357c478bd9Sstevel@tonic-gate {
22367c478bd9Sstevel@tonic-gate 	return (modop(arg, MODREMOVE_OP));
22377c478bd9Sstevel@tonic-gate }
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate /*
22407c478bd9Sstevel@tonic-gate  * Open a stream on /dev/udp, pop off all undesired modules (note that
22417c478bd9Sstevel@tonic-gate  * the user may have configured autopush to add modules above or below
22427c478bd9Sstevel@tonic-gate  * udp), and push the arp module onto raw IP.
22437c478bd9Sstevel@tonic-gate  */
22447c478bd9Sstevel@tonic-gate static int
22457c478bd9Sstevel@tonic-gate open_arp_on_udp(char *udp_dev_name)
22467c478bd9Sstevel@tonic-gate {
22477c478bd9Sstevel@tonic-gate 	int fd;
22487c478bd9Sstevel@tonic-gate 	boolean_t popped;
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 	if ((fd = open(udp_dev_name, O_RDWR)) == -1) {
22517c478bd9Sstevel@tonic-gate 		Perror2("open", udp_dev_name);
22527c478bd9Sstevel@tonic-gate 		return (-1);
22537c478bd9Sstevel@tonic-gate 	}
22547c478bd9Sstevel@tonic-gate 	errno = 0;
22557c478bd9Sstevel@tonic-gate 	popped = _B_FALSE;
22567c478bd9Sstevel@tonic-gate 	while (ioctl(fd, I_POP, 0) != -1)
22577c478bd9Sstevel@tonic-gate 		popped = _B_TRUE;
22587c478bd9Sstevel@tonic-gate 	if (!popped) {
22597c478bd9Sstevel@tonic-gate 		Perror2("cannot pop", udp_dev_name);
22607c478bd9Sstevel@tonic-gate 	} else if (errno != EINVAL) {
22617c478bd9Sstevel@tonic-gate 		Perror2("pop", udp_dev_name);
22627c478bd9Sstevel@tonic-gate 	} else if (ioctl(fd, I_PUSH, ARP_MOD_NAME) == -1) {
22637c478bd9Sstevel@tonic-gate 		Perror2("arp PUSH", udp_dev_name);
22647c478bd9Sstevel@tonic-gate 	} else {
22657c478bd9Sstevel@tonic-gate 		return (fd);
22667c478bd9Sstevel@tonic-gate 	}
22677c478bd9Sstevel@tonic-gate 	(void) close(fd);
22687c478bd9Sstevel@tonic-gate 	return (-1);
22697c478bd9Sstevel@tonic-gate }
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate /*
22727c478bd9Sstevel@tonic-gate  * Helper function for mod*() functions.  It gets a fd to the lower IP
22737c478bd9Sstevel@tonic-gate  * stream and I_PUNLINK's the lower stream.  It also initializes the
22747c478bd9Sstevel@tonic-gate  * global variable lifr.
22757c478bd9Sstevel@tonic-gate  *
22767c478bd9Sstevel@tonic-gate  * Param:
22777c478bd9Sstevel@tonic-gate  *	int *udp_fd: (referenced) fd to /dev/udp (upper IP stream).
22787c478bd9Sstevel@tonic-gate  *	int *fd: (referenced) fd to the lower IP stream.
22797c478bd9Sstevel@tonic-gate  *
22807c478bd9Sstevel@tonic-gate  * Return:
22817c478bd9Sstevel@tonic-gate  *	-1 if operation fails, 0 otherwise.
22827c478bd9Sstevel@tonic-gate  *
22837c478bd9Sstevel@tonic-gate  * Please see the big block comment above plumb_one_device()
22847c478bd9Sstevel@tonic-gate  * for the logic of the PLINK/PUNLINK
22857c478bd9Sstevel@tonic-gate  */
22867c478bd9Sstevel@tonic-gate static int
22877c478bd9Sstevel@tonic-gate ip_domux2fd(int *muxfd, int *ipfd_lowstr, int *arpfd_lowstr, int *orig_arpid)
22887c478bd9Sstevel@tonic-gate {
22897c478bd9Sstevel@tonic-gate 	int		ip_fd;
22907c478bd9Sstevel@tonic-gate 	uint64_t	flags;
22917c478bd9Sstevel@tonic-gate 	char		*udp_dev_name;
22927c478bd9Sstevel@tonic-gate 	char		*ip_dev_name;
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	*orig_arpid = 0;
22957c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
22967c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
22977c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
22987c478bd9Sstevel@tonic-gate 	}
22997c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
23007c478bd9Sstevel@tonic-gate 	if (flags & IFF_IPV4) {
23017c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP_DEV_NAME;
23027c478bd9Sstevel@tonic-gate 		ip_dev_name  = IP_DEV_NAME;
23037c478bd9Sstevel@tonic-gate 	} else if (flags & IFF_IPV6) {
23047c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP6_DEV_NAME;
23057c478bd9Sstevel@tonic-gate 		ip_dev_name  = IP6_DEV_NAME;
23067c478bd9Sstevel@tonic-gate 	} else {
23077c478bd9Sstevel@tonic-gate 		return (-1);
23087c478bd9Sstevel@tonic-gate 	}
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 	if ((ip_fd = open(ip_dev_name, O_RDWR)) < 0) {
23117c478bd9Sstevel@tonic-gate 		Perror2("open", ip_dev_name);
23127c478bd9Sstevel@tonic-gate 		return (-1);
23137c478bd9Sstevel@tonic-gate 	}
23147c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
23157c478bd9Sstevel@tonic-gate 		Perror2("SIOCGLIFMUXID", ip_dev_name);
23167c478bd9Sstevel@tonic-gate 		return (-1);
23177c478bd9Sstevel@tonic-gate 	}
23187c478bd9Sstevel@tonic-gate 	if (debug > 0) {
23197c478bd9Sstevel@tonic-gate 		(void) printf("ARP_muxid %d IP_muxid %d\n",
23207c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid, lifr.lifr_ip_muxid);
23217c478bd9Sstevel@tonic-gate 	}
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate 	if ((*muxfd = open_arp_on_udp(udp_dev_name)) == -1)
23247c478bd9Sstevel@tonic-gate 		return (-1);
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate 	if (lifr.lifr_arp_muxid != 0) {
23277c478bd9Sstevel@tonic-gate 		if ((*arpfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
23287c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid)) < 0) {
23297c478bd9Sstevel@tonic-gate 			if ((errno == EINVAL) &&
23307c478bd9Sstevel@tonic-gate 			    (flags & (IFF_NOARP | IFF_IPV6))) {
23317c478bd9Sstevel@tonic-gate 				/*
23327c478bd9Sstevel@tonic-gate 				 * Some plumbing utilities set the muxid to
23337c478bd9Sstevel@tonic-gate 				 * -1 or some invalid value to signify that
23347c478bd9Sstevel@tonic-gate 				 * there is no arp stream. Set the muxid to 0
23357c478bd9Sstevel@tonic-gate 				 * before trying to unplumb the IP stream.
23367c478bd9Sstevel@tonic-gate 				 * IP does not allow the IP stream to be
23377c478bd9Sstevel@tonic-gate 				 * unplumbed if it sees a non-null arp muxid,
23387c478bd9Sstevel@tonic-gate 				 * for consistency of IP-ARP streams.
23397c478bd9Sstevel@tonic-gate 				 */
23407c478bd9Sstevel@tonic-gate 				*orig_arpid = lifr.lifr_arp_muxid;
23417c478bd9Sstevel@tonic-gate 				lifr.lifr_arp_muxid = 0;
23427c478bd9Sstevel@tonic-gate 				(void) ioctl(*muxfd, SIOCSLIFMUXID,
23437c478bd9Sstevel@tonic-gate 				    (caddr_t)&lifr);
23447c478bd9Sstevel@tonic-gate 				*arpfd_lowstr = -1;
23457c478bd9Sstevel@tonic-gate 			} else {
23467c478bd9Sstevel@tonic-gate 				Perror0("_I_MUXID2FD");
23477c478bd9Sstevel@tonic-gate 				return (-1);
23487c478bd9Sstevel@tonic-gate 			}
23497c478bd9Sstevel@tonic-gate 		} else if (ioctl(*muxfd, I_PUNLINK,
23507c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid) < 0) {
23517c478bd9Sstevel@tonic-gate 			Perror2("I_PUNLINK", udp_dev_name);
23527c478bd9Sstevel@tonic-gate 			return (-1);
23537c478bd9Sstevel@tonic-gate 		}
23547c478bd9Sstevel@tonic-gate 	} else {
23557c478bd9Sstevel@tonic-gate 		*arpfd_lowstr = -1;
23567c478bd9Sstevel@tonic-gate 	}
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	if ((*ipfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
23597c478bd9Sstevel@tonic-gate 	    lifr.lifr_ip_muxid)) < 0) {
23607c478bd9Sstevel@tonic-gate 		Perror0("_I_MUXID2FD");
23617c478bd9Sstevel@tonic-gate 		/* Undo any changes we made */
23627c478bd9Sstevel@tonic-gate 		if (*orig_arpid != 0) {
23637c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = *orig_arpid;
23647c478bd9Sstevel@tonic-gate 			(void) ioctl(*muxfd, SIOCSLIFMUXID, (caddr_t)&lifr);
23657c478bd9Sstevel@tonic-gate 		}
23667c478bd9Sstevel@tonic-gate 		return (-1);
23677c478bd9Sstevel@tonic-gate 	}
23687c478bd9Sstevel@tonic-gate 	if (ioctl(*muxfd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) {
23697c478bd9Sstevel@tonic-gate 		Perror2("I_PUNLINK", udp_dev_name);
23707c478bd9Sstevel@tonic-gate 		/* Undo any changes we made */
23717c478bd9Sstevel@tonic-gate 		if (*orig_arpid != 0) {
23727c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = *orig_arpid;
23737c478bd9Sstevel@tonic-gate 			(void) ioctl(*muxfd, SIOCSLIFMUXID, (caddr_t)&lifr);
23747c478bd9Sstevel@tonic-gate 		}
23757c478bd9Sstevel@tonic-gate 		return (-1);
23767c478bd9Sstevel@tonic-gate 	}
23777c478bd9Sstevel@tonic-gate 	return (0);
23787c478bd9Sstevel@tonic-gate }
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate /*
23817c478bd9Sstevel@tonic-gate  * Helper function for mod*() functions.  It I_PLINK's back the upper and
23827c478bd9Sstevel@tonic-gate  * lower IP streams.  Note that this function must be called after
23837c478bd9Sstevel@tonic-gate  * ip_domux2fd().  In ip_domux2fd(), the global variable lifr is initialized
23847c478bd9Sstevel@tonic-gate  * and ip_plink() needs information in lifr.  So ip_domux2fd() and ip_plink()
23857c478bd9Sstevel@tonic-gate  * must be called in pairs.
23867c478bd9Sstevel@tonic-gate  *
23877c478bd9Sstevel@tonic-gate  * Param:
23887c478bd9Sstevel@tonic-gate  *	int udp_fd: fd to /dev/udp (upper IP stream).
23897c478bd9Sstevel@tonic-gate  *	int fd: fd to the lower IP stream.
23907c478bd9Sstevel@tonic-gate  *
23917c478bd9Sstevel@tonic-gate  * Return:
23927c478bd9Sstevel@tonic-gate  *	-1 if operation fails, 0 otherwise.
23937c478bd9Sstevel@tonic-gate  *
23947c478bd9Sstevel@tonic-gate  * Please see the big block comment above plumb_one_device()
23957c478bd9Sstevel@tonic-gate  * for the logic of the PLINK/PUNLINK
23967c478bd9Sstevel@tonic-gate  */
23977c478bd9Sstevel@tonic-gate static int
23987c478bd9Sstevel@tonic-gate ip_plink(int muxfd, int ipfd_lowstr, int arpfd_lowstr, int orig_arpid)
23997c478bd9Sstevel@tonic-gate {
24007c478bd9Sstevel@tonic-gate 	int ip_muxid;
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 	ip_muxid = ioctl(muxfd, I_PLINK, ipfd_lowstr);
24037c478bd9Sstevel@tonic-gate 	if (ip_muxid < 0) {
24047c478bd9Sstevel@tonic-gate 		Perror2("I_PLINK", UDP_DEV_NAME);
24057c478bd9Sstevel@tonic-gate 		return (-1);
24067c478bd9Sstevel@tonic-gate 	}
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 	/*
24097c478bd9Sstevel@tonic-gate 	 * If there is an arp stream, plink it. If there is no
24107c478bd9Sstevel@tonic-gate 	 * arp stream, then it is possible that the plumbing
24117c478bd9Sstevel@tonic-gate 	 * utility could have stored any value in the arp_muxid.
24127c478bd9Sstevel@tonic-gate 	 * If so, restore it from orig_arpid.
24137c478bd9Sstevel@tonic-gate 	 */
24147c478bd9Sstevel@tonic-gate 	if (arpfd_lowstr != -1) {
24157c478bd9Sstevel@tonic-gate 		if (ioctl(muxfd, I_PLINK, arpfd_lowstr) < 0) {
24167c478bd9Sstevel@tonic-gate 			Perror2("I_PLINK", UDP_DEV_NAME);
24177c478bd9Sstevel@tonic-gate 			return (-1);
24187c478bd9Sstevel@tonic-gate 		}
24197c478bd9Sstevel@tonic-gate 	} else if (orig_arpid != 0) {
24207c478bd9Sstevel@tonic-gate 		/* Undo the changes we did in ip_domux2fd */
24217c478bd9Sstevel@tonic-gate 		lifr.lifr_arp_muxid = orig_arpid;
24227c478bd9Sstevel@tonic-gate 		lifr.lifr_ip_muxid = ip_muxid;
24237c478bd9Sstevel@tonic-gate 		(void) ioctl(muxfd, SIOCSLIFMUXID, (caddr_t)&lifr);
24247c478bd9Sstevel@tonic-gate 	}
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 	return (0);
24277c478bd9Sstevel@tonic-gate }
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate /*
24307c478bd9Sstevel@tonic-gate  * The real function to perform module insertion/removal.
24317c478bd9Sstevel@tonic-gate  *
24327c478bd9Sstevel@tonic-gate  * Param:
24337c478bd9Sstevel@tonic-gate  *	char *arg: the argument string module_name@position
24347c478bd9Sstevel@tonic-gate  *	char op: operation, either MODINSERT_OP or MODREMOVE_OP.
24357c478bd9Sstevel@tonic-gate  *
24367c478bd9Sstevel@tonic-gate  * Return:
24377c478bd9Sstevel@tonic-gate  *	Before doing ip_domux2fd(), this function calls exit(1) in case of
24387c478bd9Sstevel@tonic-gate  *	error.  After ip_domux2fd() is done, it returns -1 for error, 0
24397c478bd9Sstevel@tonic-gate  *	otherwise.
24407c478bd9Sstevel@tonic-gate  */
24417c478bd9Sstevel@tonic-gate static int
24427c478bd9Sstevel@tonic-gate modop(char *arg, char op)
24437c478bd9Sstevel@tonic-gate {
24447c478bd9Sstevel@tonic-gate 	char *pos_p;
24457c478bd9Sstevel@tonic-gate 	int muxfd;
24467c478bd9Sstevel@tonic-gate 	int ipfd_lowstr;  /* IP stream (lower stream of mux) to be plinked */
24477c478bd9Sstevel@tonic-gate 	int arpfd_lowstr; /* ARP stream (lower stream of mux) to be plinked */
24487c478bd9Sstevel@tonic-gate 	struct strmodconf mod;
24497c478bd9Sstevel@tonic-gate 	char *at_char = "@";
24507c478bd9Sstevel@tonic-gate 	char *arg_str;
24517c478bd9Sstevel@tonic-gate 	int orig_arpid;
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate 	/* Need to save the original string for -a option. */
24567c478bd9Sstevel@tonic-gate 	if ((arg_str = malloc(strlen(arg) + 1)) == NULL) {
24577c478bd9Sstevel@tonic-gate 		Perror0("cannot malloc");
24587c478bd9Sstevel@tonic-gate 		return (-1);
24597c478bd9Sstevel@tonic-gate 	}
24607c478bd9Sstevel@tonic-gate 	(void) strcpy(arg_str, arg);
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	if (*arg_str == *at_char) {
24637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
24647c478bd9Sstevel@tonic-gate 		    "ifconfig: must supply a module name\n");
24657c478bd9Sstevel@tonic-gate 		exit(1);
24667c478bd9Sstevel@tonic-gate 	}
24677c478bd9Sstevel@tonic-gate 	mod.mod_name = strtok(arg_str, at_char);
24687c478bd9Sstevel@tonic-gate 	if (strlen(mod.mod_name) > FMNAMESZ) {
24697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: module name too long: %s\n",
24707c478bd9Sstevel@tonic-gate 		    mod.mod_name);
24717c478bd9Sstevel@tonic-gate 		exit(1);
24727c478bd9Sstevel@tonic-gate 	}
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate 	/*
24757c478bd9Sstevel@tonic-gate 	 * Need to make sure that the core TCP/IP stack modules are not
24767c478bd9Sstevel@tonic-gate 	 * removed.  Otherwise, "bad" things can happen.  If a module
24777c478bd9Sstevel@tonic-gate 	 * is removed and inserted back, it loses its old state.  But
24787c478bd9Sstevel@tonic-gate 	 * the modules above it still have the old state.  E.g. IP assumes
24797c478bd9Sstevel@tonic-gate 	 * fast data path while tunnel after re-inserted assumes that it can
24807c478bd9Sstevel@tonic-gate 	 * receive M_DATA only in fast data path for which it does not have
24817c478bd9Sstevel@tonic-gate 	 * any state.  This is a general caveat of _I_REMOVE/_I_INSERT.
24827c478bd9Sstevel@tonic-gate 	 */
24837c478bd9Sstevel@tonic-gate 	if (op == MODREMOVE_OP &&
24847c478bd9Sstevel@tonic-gate 	    (strcmp(mod.mod_name, ARP_MOD_NAME) == 0 ||
24857c478bd9Sstevel@tonic-gate 	    strcmp(mod.mod_name, IP_MOD_NAME) == 0 ||
24867c478bd9Sstevel@tonic-gate 	    strcmp(mod.mod_name, TUN_NAME) == 0 ||
24877c478bd9Sstevel@tonic-gate 	    strcmp(mod.mod_name, ATUN_NAME) == 0 ||
24887c478bd9Sstevel@tonic-gate 	    strcmp(mod.mod_name, TUN6TO4_NAME) == 0)) {
24897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: cannot remove %s\n",
24907c478bd9Sstevel@tonic-gate 		    mod.mod_name);
24917c478bd9Sstevel@tonic-gate 		exit(1);
24927c478bd9Sstevel@tonic-gate 	}
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 	if ((pos_p = strtok(NULL, at_char)) == NULL) {
24957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: must supply a position\n");
24967c478bd9Sstevel@tonic-gate 		exit(1);
24977c478bd9Sstevel@tonic-gate 	}
24987c478bd9Sstevel@tonic-gate 	mod.pos = atoi(pos_p);
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate 	if (ip_domux2fd(&muxfd, &ipfd_lowstr, &arpfd_lowstr,
25017c478bd9Sstevel@tonic-gate 	    &orig_arpid) < 0) {
25027c478bd9Sstevel@tonic-gate 		free(arg_str);
25037c478bd9Sstevel@tonic-gate 		return (-1);
25047c478bd9Sstevel@tonic-gate 	}
25057c478bd9Sstevel@tonic-gate 	switch (op) {
25067c478bd9Sstevel@tonic-gate 	case MODINSERT_OP:
25077c478bd9Sstevel@tonic-gate 		if (debug > 0) {
25087c478bd9Sstevel@tonic-gate 			(void) printf("Inserting module %s at %d\n",
25097c478bd9Sstevel@tonic-gate 			    mod.mod_name, mod.pos);
25107c478bd9Sstevel@tonic-gate 		}
25117c478bd9Sstevel@tonic-gate 		if (ioctl(ipfd_lowstr, _I_INSERT, (caddr_t)&mod) < 0) {
25127c478bd9Sstevel@tonic-gate 			Perror2("fail to insert module", mod.mod_name);
25137c478bd9Sstevel@tonic-gate 		}
25147c478bd9Sstevel@tonic-gate 		break;
25157c478bd9Sstevel@tonic-gate 	case MODREMOVE_OP:
25167c478bd9Sstevel@tonic-gate 		if (debug > 0) {
25177c478bd9Sstevel@tonic-gate 			(void) printf("Removing module %s at %d\n",
25187c478bd9Sstevel@tonic-gate 			    mod.mod_name, mod.pos);
25197c478bd9Sstevel@tonic-gate 		}
25207c478bd9Sstevel@tonic-gate 		if (ioctl(ipfd_lowstr, _I_REMOVE, (caddr_t)&mod) < 0) {
25217c478bd9Sstevel@tonic-gate 			Perror2("fail to remove module", mod.mod_name);
25227c478bd9Sstevel@tonic-gate 		}
25237c478bd9Sstevel@tonic-gate 		break;
25247c478bd9Sstevel@tonic-gate 	default:
25257c478bd9Sstevel@tonic-gate 		/* Should never get to here. */
25267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Unknown operation\n");
25277c478bd9Sstevel@tonic-gate 		break;
25287c478bd9Sstevel@tonic-gate 	}
25297c478bd9Sstevel@tonic-gate 	free(arg_str);
25307c478bd9Sstevel@tonic-gate 	return (ip_plink(muxfd, ipfd_lowstr, arpfd_lowstr, orig_arpid));
25317c478bd9Sstevel@tonic-gate }
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate /*
25347c478bd9Sstevel@tonic-gate  * Set tunnel source address
25357c478bd9Sstevel@tonic-gate  */
25367c478bd9Sstevel@tonic-gate /* ARGSUSED */
25377c478bd9Sstevel@tonic-gate static int
25387c478bd9Sstevel@tonic-gate setiftsrc(char *addr, int64_t param)
25397c478bd9Sstevel@tonic-gate {
25407c478bd9Sstevel@tonic-gate 	return (settaddr(addr, icfg_set_tunnel_src));
25417c478bd9Sstevel@tonic-gate }
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate /*
25447c478bd9Sstevel@tonic-gate  * Set tunnel destination address
25457c478bd9Sstevel@tonic-gate  */
25467c478bd9Sstevel@tonic-gate /* ARGSUSED */
25477c478bd9Sstevel@tonic-gate static int
25487c478bd9Sstevel@tonic-gate setiftdst(char *addr, int64_t param)
25497c478bd9Sstevel@tonic-gate {
25507c478bd9Sstevel@tonic-gate 	return (settaddr(addr, icfg_set_tunnel_dest));
25517c478bd9Sstevel@tonic-gate }
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate /*
25547c478bd9Sstevel@tonic-gate  * sets tunnels src|dst address.  settaddr() expects the following:
25557c478bd9Sstevel@tonic-gate  * addr: Points to a printable string containing the address to be
25567c478bd9Sstevel@tonic-gate  *       set, e.g. 129.153.128.110.
25577c478bd9Sstevel@tonic-gate  * fn:   Pointer to a libinetcfg routine that will do the actual work.
25587c478bd9Sstevel@tonic-gate  *       The only valid functions are icfg_set_tunnel_src and
25597c478bd9Sstevel@tonic-gate  *       icfg_set_tunnel_dest.
25607c478bd9Sstevel@tonic-gate  */
25617c478bd9Sstevel@tonic-gate static int
25627c478bd9Sstevel@tonic-gate settaddr(char *addr,
25637c478bd9Sstevel@tonic-gate     int (*fn)(icfg_handle_t, const struct sockaddr *, socklen_t))
25647c478bd9Sstevel@tonic-gate {
25657c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
25667c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
25677c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
25687c478bd9Sstevel@tonic-gate 	int lower;
25697c478bd9Sstevel@tonic-gate 	int rc;
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
25727c478bd9Sstevel@tonic-gate 		errno = EPERM;
25737c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
25747c478bd9Sstevel@tonic-gate 	}
25757c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
25767c478bd9Sstevel@tonic-gate 	interface.if_protocol = SOCKET_AF(af);
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 	/* Open interface. */
25797c478bd9Sstevel@tonic-gate 	if ((rc = icfg_open(&handle, &interface)) != ICFG_SUCCESS)
25807c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	rc = icfg_get_tunnel_lower(handle, &lower);
25837c478bd9Sstevel@tonic-gate 	if (rc != ICFG_SUCCESS)
25847c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
25857c478bd9Sstevel@tonic-gate 
25867c478bd9Sstevel@tonic-gate 	if (lower == AF_INET) {
25877c478bd9Sstevel@tonic-gate 		in_getaddr(addr, (struct sockaddr *)&laddr, NULL);
25887c478bd9Sstevel@tonic-gate 	} else {
25897c478bd9Sstevel@tonic-gate 		in6_getaddr(addr, (struct sockaddr *)&laddr, NULL);
25907c478bd9Sstevel@tonic-gate 	}
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	/* Call fn to do the real work, and close the interface. */
25937c478bd9Sstevel@tonic-gate 	rc = (*fn)(handle, (struct sockaddr *)&laddr,
25947c478bd9Sstevel@tonic-gate 	    sizeof (struct sockaddr_storage));
25957c478bd9Sstevel@tonic-gate 	icfg_close(handle);
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate 	if (rc != ICFG_SUCCESS)
25987c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate 	return (0);
26017c478bd9Sstevel@tonic-gate }
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate /* Set tunnel encapsulation limit. */
26047c478bd9Sstevel@tonic-gate /* ARGSUSED */
26057c478bd9Sstevel@tonic-gate static int
26067c478bd9Sstevel@tonic-gate set_tun_encap_limit(char *arg, int64_t param)
26077c478bd9Sstevel@tonic-gate {
26087c478bd9Sstevel@tonic-gate 	short limit;
26097c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
26107c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
26117c478bd9Sstevel@tonic-gate 	int rc;
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
26147c478bd9Sstevel@tonic-gate 		errno = EPERM;
26157c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
26167c478bd9Sstevel@tonic-gate 	}
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	if ((sscanf(arg, "%hd", &limit) != 1) || (limit < 0) ||
26197c478bd9Sstevel@tonic-gate 	    (limit > 255)) {
26207c478bd9Sstevel@tonic-gate 		errno = EINVAL;
26217c478bd9Sstevel@tonic-gate 		Perror0_exit("Invalid encapsulation 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_encaplimit(handle, (int)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 encapsulation limit");
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 	return (0);
26377c478bd9Sstevel@tonic-gate }
26387c478bd9Sstevel@tonic-gate 
26397c478bd9Sstevel@tonic-gate /* Disable encapsulation limit. */
26407c478bd9Sstevel@tonic-gate /* ARGSUSED */
26417c478bd9Sstevel@tonic-gate static int
26427c478bd9Sstevel@tonic-gate clr_tun_encap_limit(char *arg, int64_t param)
26437c478bd9Sstevel@tonic-gate {
26447c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
26457c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
26467c478bd9Sstevel@tonic-gate 	int rc;
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
26497c478bd9Sstevel@tonic-gate 		errno = EPERM;
26507c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
26517c478bd9Sstevel@tonic-gate 	}
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 	/* Open interface for configuration. */
26547c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
26557c478bd9Sstevel@tonic-gate 	interface.if_protocol = SOCKET_AF(af);
26567c478bd9Sstevel@tonic-gate 	if (icfg_open(&handle, &interface) != ICFG_SUCCESS)
26577c478bd9Sstevel@tonic-gate 		Perror0_exit("couldn't open interface");
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 	rc = icfg_set_tunnel_encaplimit(handle, -1);
26607c478bd9Sstevel@tonic-gate 	icfg_close(handle);
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate 	if (rc != ICFG_SUCCESS)
26637c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate 	return (0);
26667c478bd9Sstevel@tonic-gate }
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate /* Set tunnel hop limit. */
26697c478bd9Sstevel@tonic-gate /* ARGSUSED */
26707c478bd9Sstevel@tonic-gate static int
26717c478bd9Sstevel@tonic-gate set_tun_hop_limit(char *arg, int64_t param)
26727c478bd9Sstevel@tonic-gate {
26737c478bd9Sstevel@tonic-gate 	unsigned short limit;
26747c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
26757c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
26767c478bd9Sstevel@tonic-gate 	int rc;
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
26797c478bd9Sstevel@tonic-gate 		errno = EPERM;
26807c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
26817c478bd9Sstevel@tonic-gate 	}
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate 	/*
26847c478bd9Sstevel@tonic-gate 	 * Check limit here since it's really only an 8-bit unsigned quantity.
26857c478bd9Sstevel@tonic-gate 	 */
26867c478bd9Sstevel@tonic-gate 	if ((sscanf(arg, "%hu", &limit) != 1) || (limit > 255)) {
26877c478bd9Sstevel@tonic-gate 		errno = EINVAL;
26887c478bd9Sstevel@tonic-gate 		Perror0_exit("Invalid hop limit");
26897c478bd9Sstevel@tonic-gate 	}
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate 	/* Open interface for configuration. */
26927c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
26937c478bd9Sstevel@tonic-gate 	interface.if_protocol = SOCKET_AF(af);
26947c478bd9Sstevel@tonic-gate 	if (icfg_open(&handle, &interface) != ICFG_SUCCESS)
26957c478bd9Sstevel@tonic-gate 		Perror0_exit("couldn't open interface");
26967c478bd9Sstevel@tonic-gate 
26977c478bd9Sstevel@tonic-gate 	rc = icfg_set_tunnel_hoplimit(handle, (uint8_t)limit);
26987c478bd9Sstevel@tonic-gate 	icfg_close(handle);
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate 	if (rc != ICFG_SUCCESS)
27017c478bd9Sstevel@tonic-gate 		Perror0_exit("Could not configure tunnel hop limit");
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate 	return (0);
27047c478bd9Sstevel@tonic-gate }
27057c478bd9Sstevel@tonic-gate 
27067c478bd9Sstevel@tonic-gate /* Set zone ID */
27077c478bd9Sstevel@tonic-gate static int
27087c478bd9Sstevel@tonic-gate setzone(char *arg, int64_t param)
27097c478bd9Sstevel@tonic-gate {
27107c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = GLOBAL_ZONEID;
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate 	if (param == NEXTARG) {
27137c478bd9Sstevel@tonic-gate 		/* zone must be active */
27147c478bd9Sstevel@tonic-gate 		if ((zoneid = getzoneidbyname(arg)) == -1) {
27157c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
27167c478bd9Sstevel@tonic-gate 			    "ifconfig: unknown zone '%s'\n", arg);
27177c478bd9Sstevel@tonic-gate 			exit(1);
27187c478bd9Sstevel@tonic-gate 		}
27197c478bd9Sstevel@tonic-gate 	}
27207c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
27217c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zoneid;
27227c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
27237c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFZONE");
27247c478bd9Sstevel@tonic-gate 	return (0);
27257c478bd9Sstevel@tonic-gate }
27267c478bd9Sstevel@tonic-gate 
272745916cd2Sjpk /* Put interface into all zones */
272845916cd2Sjpk /* ARGSUSED */
272945916cd2Sjpk static int
273045916cd2Sjpk setallzones(char *arg, int64_t param)
273145916cd2Sjpk {
273245916cd2Sjpk 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
273345916cd2Sjpk 	lifr.lifr_zoneid = ALL_ZONES;
273445916cd2Sjpk 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
273545916cd2Sjpk 		Perror0_exit("SIOCSLIFZONE");
273645916cd2Sjpk 	return (0);
273745916cd2Sjpk }
273845916cd2Sjpk 
27397c478bd9Sstevel@tonic-gate /* Set source address to use */
27407c478bd9Sstevel@tonic-gate /* ARGSUSED */
27417c478bd9Sstevel@tonic-gate static int
27427c478bd9Sstevel@tonic-gate setifsrc(char *arg, int64_t param)
27437c478bd9Sstevel@tonic-gate {
27447c478bd9Sstevel@tonic-gate 	uint_t ifindex = 0;
27457c478bd9Sstevel@tonic-gate 	int rval;
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
27487c478bd9Sstevel@tonic-gate 
27497c478bd9Sstevel@tonic-gate 	/*
27507c478bd9Sstevel@tonic-gate 	 * Argument can be either an interface name or "none". The latter means
27517c478bd9Sstevel@tonic-gate 	 * that any previous selection is cleared.
27527c478bd9Sstevel@tonic-gate 	 */
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	rval = strcmp(arg, name);
27557c478bd9Sstevel@tonic-gate 	if (rval == 0) {
27567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
27577c478bd9Sstevel@tonic-gate 		    "ifconfig: Cannot specify same interface for usesrc"
27587c478bd9Sstevel@tonic-gate 		    " group\n");
27597c478bd9Sstevel@tonic-gate 		exit(1);
27607c478bd9Sstevel@tonic-gate 	}
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate 	rval = strcmp(arg, NONE_STR);
27637c478bd9Sstevel@tonic-gate 	if (rval != 0) {
27647c478bd9Sstevel@tonic-gate 		if ((ifindex = if_nametoindex(arg)) == 0) {
27657c478bd9Sstevel@tonic-gate 			(void) strncpy(lifr.lifr_name, arg, LIFNAMSIZ);
27667c478bd9Sstevel@tonic-gate 			Perror0_exit("Could not get interface index");
27677c478bd9Sstevel@tonic-gate 		}
27687c478bd9Sstevel@tonic-gate 		lifr.lifr_index = ifindex;
27697c478bd9Sstevel@tonic-gate 	} else {
27707c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) != 0)
27717c478bd9Sstevel@tonic-gate 			Perror0_exit("Not a valid usesrc consumer");
27727c478bd9Sstevel@tonic-gate 		lifr.lifr_index = 0;
27737c478bd9Sstevel@tonic-gate 	}
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 	if (debug)
27767c478bd9Sstevel@tonic-gate 		(void) printf("setifsrc: lifr_name %s, lifr_index %d\n",
27777c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, lifr.lifr_index);
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFUSESRC, (caddr_t)&lifr) == -1) {
27807c478bd9Sstevel@tonic-gate 		if (rval == 0)
27817c478bd9Sstevel@tonic-gate 			Perror0_exit("Cannot reset usesrc group");
27827c478bd9Sstevel@tonic-gate 		else
27837c478bd9Sstevel@tonic-gate 			Perror0_exit("Could not set source interface");
27847c478bd9Sstevel@tonic-gate 	}
27857c478bd9Sstevel@tonic-gate 
27867c478bd9Sstevel@tonic-gate 	return (0);
27877c478bd9Sstevel@tonic-gate }
27887c478bd9Sstevel@tonic-gate 
27897c478bd9Sstevel@tonic-gate /*
27907c478bd9Sstevel@tonic-gate  * Print the interface status line associated with `ifname'
27917c478bd9Sstevel@tonic-gate  */
27927c478bd9Sstevel@tonic-gate static void
27937c478bd9Sstevel@tonic-gate ifstatus(const char *ifname)
27947c478bd9Sstevel@tonic-gate {
27957c478bd9Sstevel@tonic-gate 	uint64_t flags;
27967c478bd9Sstevel@tonic-gate 	char if_usesrc_name[LIFNAMSIZ];
27977c478bd9Sstevel@tonic-gate 	char *newbuf;
27987c478bd9Sstevel@tonic-gate 	int n, numifs, rval = 0;
27997c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp;
28007c478bd9Sstevel@tonic-gate 	struct lifsrcof lifs;
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
28037c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
28047c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
28057c478bd9Sstevel@tonic-gate 	}
28067c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	/*
28097c478bd9Sstevel@tonic-gate 	 * In V4 compatibility mode, we don't print the IFF_IPV4 flag or
28107c478bd9Sstevel@tonic-gate 	 * interfaces with IFF_IPV6 set.
28117c478bd9Sstevel@tonic-gate 	 */
28127c478bd9Sstevel@tonic-gate 	if (v4compat) {
28137c478bd9Sstevel@tonic-gate 		flags &= ~IFF_IPV4;
28147c478bd9Sstevel@tonic-gate 		if (flags & IFF_IPV6)
28157c478bd9Sstevel@tonic-gate 			return;
28167c478bd9Sstevel@tonic-gate 	}
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate 	(void) printf("%s: ", ifname);
28197c478bd9Sstevel@tonic-gate 	print_flags(flags);
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
28227c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
28237c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFMETRIC");
28247c478bd9Sstevel@tonic-gate 	} else {
28257c478bd9Sstevel@tonic-gate 		if (lifr.lifr_metric)
28267c478bd9Sstevel@tonic-gate 			(void) printf(" metric %d", lifr.lifr_metric);
28277c478bd9Sstevel@tonic-gate 	}
28287c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
28297c478bd9Sstevel@tonic-gate 		(void) printf(" mtu %d", lifr.lifr_metric);
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 	/* don't print index or zone when in compatibility mode */
28327c478bd9Sstevel@tonic-gate 	if (!v4compat) {
28337c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
28347c478bd9Sstevel@tonic-gate 			(void) printf(" index %d", lifr.lifr_index);
28357c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifr) >= 0 &&
28367c478bd9Sstevel@tonic-gate 		    lifr.lifr_zoneid != getzoneid()) {
28377c478bd9Sstevel@tonic-gate 			char zone_name[ZONENAME_MAX];
28387c478bd9Sstevel@tonic-gate 
283945916cd2Sjpk 			if (lifr.lifr_zoneid == ALL_ZONES) {
284045916cd2Sjpk 				(void) printf("\n\tall-zones");
284145916cd2Sjpk 			} else if (getzonenamebyid(lifr.lifr_zoneid, zone_name,
28427c478bd9Sstevel@tonic-gate 			    sizeof (zone_name)) < 0) {
28437c478bd9Sstevel@tonic-gate 				(void) printf("\n\tzone %d", lifr.lifr_zoneid);
28447c478bd9Sstevel@tonic-gate 			} else {
28457c478bd9Sstevel@tonic-gate 				(void) printf("\n\tzone %s", zone_name);
28467c478bd9Sstevel@tonic-gate 			}
28477c478bd9Sstevel@tonic-gate 		}
28487c478bd9Sstevel@tonic-gate 	}
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) {
28517c478bd9Sstevel@tonic-gate 		lifs.lifs_ifindex = lifr.lifr_index;
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate 		/*
28547c478bd9Sstevel@tonic-gate 		 * Find the number of interfaces that use this interfaces'
28557c478bd9Sstevel@tonic-gate 		 * address as a source address
28567c478bd9Sstevel@tonic-gate 		 */
28577c478bd9Sstevel@tonic-gate 		lifs.lifs_buf = NULL;
28587c478bd9Sstevel@tonic-gate 		lifs.lifs_maxlen = 0;
28597c478bd9Sstevel@tonic-gate 		for (;;) {
28607c478bd9Sstevel@tonic-gate 			/* The first pass will give the bufsize we need */
28617c478bd9Sstevel@tonic-gate 			rval = ioctl(s, SIOCGLIFSRCOF, (char *)&lifs);
28627c478bd9Sstevel@tonic-gate 			if (rval < 0) {
28637c478bd9Sstevel@tonic-gate 				if (lifs.lifs_buf != NULL) {
28647c478bd9Sstevel@tonic-gate 					free(lifs.lifs_buf);
28657c478bd9Sstevel@tonic-gate 					lifs.lifs_buf = NULL;
28667c478bd9Sstevel@tonic-gate 				}
28677c478bd9Sstevel@tonic-gate 				lifs.lifs_len = 0;
28687c478bd9Sstevel@tonic-gate 				break;
28697c478bd9Sstevel@tonic-gate 			}
28707c478bd9Sstevel@tonic-gate 			if (lifs.lifs_len <= lifs.lifs_maxlen)
28717c478bd9Sstevel@tonic-gate 				break;
28727c478bd9Sstevel@tonic-gate 			/* Use kernel's size + a small margin to avoid loops */
28737c478bd9Sstevel@tonic-gate 			lifs.lifs_maxlen = lifs.lifs_len +
28747c478bd9Sstevel@tonic-gate 			    5 * sizeof (struct lifreq);
28757c478bd9Sstevel@tonic-gate 			/* For the first pass, realloc acts like malloc */
28767c478bd9Sstevel@tonic-gate 			newbuf = realloc(lifs.lifs_buf, lifs.lifs_maxlen);
28777c478bd9Sstevel@tonic-gate 			if (newbuf == NULL) {
28787c478bd9Sstevel@tonic-gate 				if (lifs.lifs_buf != NULL) {
28797c478bd9Sstevel@tonic-gate 					free(lifs.lifs_buf);
28807c478bd9Sstevel@tonic-gate 					lifs.lifs_buf = NULL;
28817c478bd9Sstevel@tonic-gate 				}
28827c478bd9Sstevel@tonic-gate 				lifs.lifs_len = 0;
28837c478bd9Sstevel@tonic-gate 				break;
28847c478bd9Sstevel@tonic-gate 			}
28857c478bd9Sstevel@tonic-gate 			lifs.lifs_buf = newbuf;
28867c478bd9Sstevel@tonic-gate 		}
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 		numifs = lifs.lifs_len / sizeof (struct lifreq);
28907c478bd9Sstevel@tonic-gate 		if (numifs > 0) {
28917c478bd9Sstevel@tonic-gate 			lifrp = lifs.lifs_req;
28927c478bd9Sstevel@tonic-gate 			(void) printf("\n\tsrcof");
28937c478bd9Sstevel@tonic-gate 			for (n = numifs; n > 0; n--, lifrp++) {
28947c478bd9Sstevel@tonic-gate 				(void) printf(" %s", lifrp->lifr_name);
28957c478bd9Sstevel@tonic-gate 			}
28967c478bd9Sstevel@tonic-gate 		}
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 		if (lifs.lifs_buf != NULL)
28997c478bd9Sstevel@tonic-gate 			free(lifs.lifs_buf);
29007c478bd9Sstevel@tonic-gate 	}
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 	/* Find the interface whose source address this interface uses */
29037c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
29047c478bd9Sstevel@tonic-gate 		if (lifr.lifr_index != 0) {
29057c478bd9Sstevel@tonic-gate 			if (if_indextoname(lifr.lifr_index,
29067c478bd9Sstevel@tonic-gate 			    if_usesrc_name) == NULL) {
29077c478bd9Sstevel@tonic-gate 				(void) printf("\n\tusesrc ifIndex %d",
29087c478bd9Sstevel@tonic-gate 				    lifr.lifr_index);
29097c478bd9Sstevel@tonic-gate 			} else {
29107c478bd9Sstevel@tonic-gate 				(void) printf("\n\tusesrc %s", if_usesrc_name);
29117c478bd9Sstevel@tonic-gate 			}
29127c478bd9Sstevel@tonic-gate 		}
29137c478bd9Sstevel@tonic-gate 	}
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
29167c478bd9Sstevel@tonic-gate }
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 
29197c478bd9Sstevel@tonic-gate /*
29207c478bd9Sstevel@tonic-gate  * Print the status of the interface.  If an address family was
29217c478bd9Sstevel@tonic-gate  * specified, show it and it only; otherwise, show them all.
29227c478bd9Sstevel@tonic-gate  */
29237c478bd9Sstevel@tonic-gate static void
29247c478bd9Sstevel@tonic-gate status(void)
29257c478bd9Sstevel@tonic-gate {
29267c478bd9Sstevel@tonic-gate 	struct afswtch *p = afp;
29277c478bd9Sstevel@tonic-gate 	uint64_t flags;
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
29307c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
29317c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
29327c478bd9Sstevel@tonic-gate 	}
29337c478bd9Sstevel@tonic-gate 
29347c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
29357c478bd9Sstevel@tonic-gate 
29367c478bd9Sstevel@tonic-gate 	/*
29377c478bd9Sstevel@tonic-gate 	 * Only print the interface status if the address family matches
29387c478bd9Sstevel@tonic-gate 	 * the interface family flag.
29397c478bd9Sstevel@tonic-gate 	 */
29407c478bd9Sstevel@tonic-gate 	if (p != NULL) {
29417c478bd9Sstevel@tonic-gate 		if (((p->af_af == AF_INET6) && (flags & IFF_IPV4)) ||
29427c478bd9Sstevel@tonic-gate 		    ((p->af_af == AF_INET) && (flags & IFF_IPV6)))
29437c478bd9Sstevel@tonic-gate 			return;
29447c478bd9Sstevel@tonic-gate 	}
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	/*
29477c478bd9Sstevel@tonic-gate 	 * In V4 compatibility mode, don't print IFF_IPV6 interfaces.
29487c478bd9Sstevel@tonic-gate 	 */
29497c478bd9Sstevel@tonic-gate 	if (v4compat && (flags & IFF_IPV6))
29507c478bd9Sstevel@tonic-gate 		return;
29517c478bd9Sstevel@tonic-gate 
29527c478bd9Sstevel@tonic-gate 	ifstatus(name);
29537c478bd9Sstevel@tonic-gate 
29547c478bd9Sstevel@tonic-gate 	if (p != NULL) {
29557c478bd9Sstevel@tonic-gate 		(*p->af_status)(1, flags);
29567c478bd9Sstevel@tonic-gate 	} else {
29577c478bd9Sstevel@tonic-gate 		for (p = afs; p->af_name; p++) {
29587c478bd9Sstevel@tonic-gate 			(void) close(s);
29597c478bd9Sstevel@tonic-gate 			s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0);
29607c478bd9Sstevel@tonic-gate 			/* set global af for use in p->af_status */
29617c478bd9Sstevel@tonic-gate 			af = p->af_af;
29627c478bd9Sstevel@tonic-gate 			if (s == -1) {
29637c478bd9Sstevel@tonic-gate 				Perror0_exit("socket");
29647c478bd9Sstevel@tonic-gate 			}
29657c478bd9Sstevel@tonic-gate 			(*p->af_status)(0, flags);
29667c478bd9Sstevel@tonic-gate 		}
29677c478bd9Sstevel@tonic-gate 
29687c478bd9Sstevel@tonic-gate 		/*
29697c478bd9Sstevel@tonic-gate 		 * Historically, 'ether' has been an address family,
29707c478bd9Sstevel@tonic-gate 		 * so print it here.
29717c478bd9Sstevel@tonic-gate 		 */
29727c478bd9Sstevel@tonic-gate 		print_ifether(name);
29737c478bd9Sstevel@tonic-gate 	}
29747c478bd9Sstevel@tonic-gate }
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate /*
29777c478bd9Sstevel@tonic-gate  * Print the status of the interface in a format that can be used to
29787c478bd9Sstevel@tonic-gate  * reconfigure the interface later. Code stolen from status() above.
29797c478bd9Sstevel@tonic-gate  */
29807c478bd9Sstevel@tonic-gate /* ARGSUSED */
29817c478bd9Sstevel@tonic-gate static int
29827c478bd9Sstevel@tonic-gate configinfo(char *null, int64_t param)
29837c478bd9Sstevel@tonic-gate {
29847c478bd9Sstevel@tonic-gate 	struct afswtch *p = afp;
29857c478bd9Sstevel@tonic-gate 	uint64_t flags;
29867c478bd9Sstevel@tonic-gate 	char phydevname[LIFNAMSIZ];
29877c478bd9Sstevel@tonic-gate 	char if_usesrc_name[LIFNAMSIZ];
29887c478bd9Sstevel@tonic-gate 	char *cp;
29897c478bd9Sstevel@tonic-gate 
29907c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
29917c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
29927c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
29937c478bd9Sstevel@tonic-gate 	}
29947c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	if (debug) {
29977c478bd9Sstevel@tonic-gate 		(void) printf("configinfo: name %s flags  0x%llx af_af %d\n",
29987c478bd9Sstevel@tonic-gate 		    name, flags, p != NULL ? p->af_af : -1);
29997c478bd9Sstevel@tonic-gate 	}
30007c478bd9Sstevel@tonic-gate 
30017c478bd9Sstevel@tonic-gate 	/* remove LIF component */
30027c478bd9Sstevel@tonic-gate 	(void) strncpy(phydevname, name, sizeof (phydevname));
30037c478bd9Sstevel@tonic-gate 	cp = strchr(phydevname, ':');
30047c478bd9Sstevel@tonic-gate 	if (cp) {
30057c478bd9Sstevel@tonic-gate 		*cp = 0;
30067c478bd9Sstevel@tonic-gate 	}
30077c478bd9Sstevel@tonic-gate 	phydevname[sizeof (phydevname) - 1] = '\0';
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate 	/*
30107c478bd9Sstevel@tonic-gate 	 * if the interface is IPv4
30117c478bd9Sstevel@tonic-gate 	 *	if we have a IPv6 address family restriction return
30127c478bd9Sstevel@tonic-gate 	 *		so it won't print
30137c478bd9Sstevel@tonic-gate 	 *	if we are in IPv4 compatibility mode, clear out IFF_IPV4
30147c478bd9Sstevel@tonic-gate 	 *		so we don't print it.
30157c478bd9Sstevel@tonic-gate 	 */
30167c478bd9Sstevel@tonic-gate 	if (flags & IFF_IPV4) {
30177c478bd9Sstevel@tonic-gate 		if (p && p->af_af == AF_INET6)
30187c478bd9Sstevel@tonic-gate 			return (-1);
30197c478bd9Sstevel@tonic-gate 		if (v4compat)
30207c478bd9Sstevel@tonic-gate 			flags &= ~IFF_IPV4;
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate 		(void) printf("%s inet plumb", phydevname);
30237c478bd9Sstevel@tonic-gate 	} else if (flags & IFF_IPV6) {
30247c478bd9Sstevel@tonic-gate 		/*
30257c478bd9Sstevel@tonic-gate 		 * else if the interface is IPv6
30267c478bd9Sstevel@tonic-gate 		 *	if we have a IPv4 address family restriction return
30277c478bd9Sstevel@tonic-gate 		 *	or we are in IPv4 compatibiltiy mode, return.
30287c478bd9Sstevel@tonic-gate 		 */
30297c478bd9Sstevel@tonic-gate 		if (p && p->af_af == AF_INET)
30307c478bd9Sstevel@tonic-gate 			return (-1);
30317c478bd9Sstevel@tonic-gate 		if (v4compat)
30327c478bd9Sstevel@tonic-gate 			return (-1);
30337c478bd9Sstevel@tonic-gate 
30347c478bd9Sstevel@tonic-gate 		(void) printf("%s inet6 plumb", phydevname);
30357c478bd9Sstevel@tonic-gate 	}
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
30387c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
30397c478bd9Sstevel@tonic-gate 		Perror0_exit("configinfo: SIOCGLIFMETRIC");
30407c478bd9Sstevel@tonic-gate 	} else {
30417c478bd9Sstevel@tonic-gate 		if (lifr.lifr_metric)
30427c478bd9Sstevel@tonic-gate 			(void) printf(" metric %d ", lifr.lifr_metric);
30437c478bd9Sstevel@tonic-gate 	}
30447c478bd9Sstevel@tonic-gate 	if (((flags & (IFF_VIRTUAL|IFF_LOOPBACK)) != IFF_VIRTUAL) &&
30457c478bd9Sstevel@tonic-gate 		ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
30467c478bd9Sstevel@tonic-gate 		(void) printf(" mtu %d", lifr.lifr_metric);
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 	/* don't print index when in compatibility mode */
30497c478bd9Sstevel@tonic-gate 	if (!v4compat) {
30507c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
30517c478bd9Sstevel@tonic-gate 			(void) printf(" index %d", lifr.lifr_index);
30527c478bd9Sstevel@tonic-gate 	}
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
30557c478bd9Sstevel@tonic-gate 		if (lifr.lifr_index != 0) {
30567c478bd9Sstevel@tonic-gate 			if (if_indextoname(lifr.lifr_index,
30577c478bd9Sstevel@tonic-gate 			    if_usesrc_name) != NULL) {
30587c478bd9Sstevel@tonic-gate 				(void) printf(" usesrc %s", if_usesrc_name);
30597c478bd9Sstevel@tonic-gate 			}
30607c478bd9Sstevel@tonic-gate 		}
30617c478bd9Sstevel@tonic-gate 	}
30627c478bd9Sstevel@tonic-gate 
30637c478bd9Sstevel@tonic-gate 	if (p != NULL) {
30647c478bd9Sstevel@tonic-gate 		(*p->af_configinfo)(1, flags);
30657c478bd9Sstevel@tonic-gate 	} else {
30667c478bd9Sstevel@tonic-gate 		for (p = afs; p->af_name; p++) {
30677c478bd9Sstevel@tonic-gate 			(void) close(s);
30687c478bd9Sstevel@tonic-gate 			s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0);
30697c478bd9Sstevel@tonic-gate 			/* set global af for use in p->af_configinfo */
30707c478bd9Sstevel@tonic-gate 			af = p->af_af;
30717c478bd9Sstevel@tonic-gate 			if (s == -1) {
30727c478bd9Sstevel@tonic-gate 				Perror0_exit("socket");
30737c478bd9Sstevel@tonic-gate 			}
30747c478bd9Sstevel@tonic-gate 			(*p->af_configinfo)(0, flags);
30757c478bd9Sstevel@tonic-gate 		}
30767c478bd9Sstevel@tonic-gate 	}
30777c478bd9Sstevel@tonic-gate 
30787c478bd9Sstevel@tonic-gate 	(void) printf("\n");
30797c478bd9Sstevel@tonic-gate 
30807c478bd9Sstevel@tonic-gate 	return (0);
30817c478bd9Sstevel@tonic-gate }
30827c478bd9Sstevel@tonic-gate 
30837c478bd9Sstevel@tonic-gate static void
30847c478bd9Sstevel@tonic-gate print_tsec(struct iftun_req *tparams)
30857c478bd9Sstevel@tonic-gate {
30867c478bd9Sstevel@tonic-gate 	ipsec_req_t *ipsr;
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate 	(void) printf("\ttunnel security settings  ");
30897c478bd9Sstevel@tonic-gate 	/*
30907c478bd9Sstevel@tonic-gate 	 * Deal with versioning, for now just point
30917c478bd9Sstevel@tonic-gate 	 * an ipsec_req_t at ifta_secinfo.  If versions
30927c478bd9Sstevel@tonic-gate 	 * change, something else will overlay ifta_secinfo.
30937c478bd9Sstevel@tonic-gate 	 */
30947c478bd9Sstevel@tonic-gate 	assert(tparams->ifta_vers == IFTUN_VERSION);
30957c478bd9Sstevel@tonic-gate 
30967c478bd9Sstevel@tonic-gate 	ipsr = (ipsec_req_t *)(&tparams->ifta_secinfo);
30977c478bd9Sstevel@tonic-gate 	if (ipsr->ipsr_ah_req & IPSEC_PREF_REQUIRED) {
30987c478bd9Sstevel@tonic-gate 		(void) printf("ah (%s)  ",
30997c478bd9Sstevel@tonic-gate 		    rparsealg(ipsr->ipsr_auth_alg, IPSEC_PROTO_AH));
31007c478bd9Sstevel@tonic-gate 	}
31017c478bd9Sstevel@tonic-gate 	if (ipsr->ipsr_esp_req & IPSEC_PREF_REQUIRED) {
31027c478bd9Sstevel@tonic-gate 		(void) printf("esp (%s",
31037c478bd9Sstevel@tonic-gate 		    rparsealg(ipsr->ipsr_esp_alg, IPSEC_PROTO_ESP));
31047c478bd9Sstevel@tonic-gate 		(void) printf("/%s)",
31057c478bd9Sstevel@tonic-gate 		    rparsealg(ipsr->ipsr_esp_auth_alg, IPSEC_PROTO_AH));
31067c478bd9Sstevel@tonic-gate 	}
31077c478bd9Sstevel@tonic-gate 	(void) printf("\n");
31087c478bd9Sstevel@tonic-gate }
31097c478bd9Sstevel@tonic-gate 
31107c478bd9Sstevel@tonic-gate static void
31117c478bd9Sstevel@tonic-gate tun_status(void)
31127c478bd9Sstevel@tonic-gate {
31137c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
31147c478bd9Sstevel@tonic-gate 	int rc;
31157c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
31167c478bd9Sstevel@tonic-gate 	int protocol;
31177c478bd9Sstevel@tonic-gate 	char srcbuf[INET6_ADDRSTRLEN];
31187c478bd9Sstevel@tonic-gate 	char dstbuf[INET6_ADDRSTRLEN];
31197c478bd9Sstevel@tonic-gate 	boolean_t tabbed;
31207c478bd9Sstevel@tonic-gate 	uint8_t hoplimit;
31217c478bd9Sstevel@tonic-gate 	int16_t encaplimit;
31227c478bd9Sstevel@tonic-gate 	struct sockaddr_storage taddr;
31237c478bd9Sstevel@tonic-gate 	socklen_t socklen = sizeof (taddr);
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
31267c478bd9Sstevel@tonic-gate 	interface.if_protocol = SOCKET_AF(af);
31277c478bd9Sstevel@tonic-gate 	if ((rc = icfg_open(&handle, &interface)) != ICFG_SUCCESS)
31287c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
31297c478bd9Sstevel@tonic-gate 
31307c478bd9Sstevel@tonic-gate 	/*
31317c478bd9Sstevel@tonic-gate 	 * only print tunnel info for lun 0.  If ioctl fails, assume
31327c478bd9Sstevel@tonic-gate 	 * we are not a tunnel
31337c478bd9Sstevel@tonic-gate 	 */
31347c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL ||
31357c478bd9Sstevel@tonic-gate 	    icfg_get_tunnel_lower(handle, &protocol) != ICFG_SUCCESS) {
31367c478bd9Sstevel@tonic-gate 		icfg_close(handle);
31377c478bd9Sstevel@tonic-gate 		return;
31387c478bd9Sstevel@tonic-gate 	}
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 	switch (protocol) {
31417c478bd9Sstevel@tonic-gate 	case AF_INET:
31427c478bd9Sstevel@tonic-gate 		(void) printf("\tinet");
31437c478bd9Sstevel@tonic-gate 		break;
31447c478bd9Sstevel@tonic-gate 	case AF_INET6:
31457c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6");
31467c478bd9Sstevel@tonic-gate 		break;
31477c478bd9Sstevel@tonic-gate 	default:
31487c478bd9Sstevel@tonic-gate 		Perror0_exit("\ttunnel: Illegal lower stream\n\t");
31497c478bd9Sstevel@tonic-gate 		break;
31507c478bd9Sstevel@tonic-gate 	}
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate 	rc = icfg_get_tunnel_src(handle, (struct sockaddr *)&taddr, &socklen);
31537c478bd9Sstevel@tonic-gate 	if (rc == ICFG_NOT_SET) {
31547c478bd9Sstevel@tonic-gate 		(void) strlcpy(srcbuf, (protocol == AF_INET) ? "0.0.0.0" :
31557c478bd9Sstevel@tonic-gate 		    "::", sizeof (srcbuf));
31567c478bd9Sstevel@tonic-gate 	} else if (rc != ICFG_SUCCESS) {
31577c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
31587c478bd9Sstevel@tonic-gate 	} else {
31597c478bd9Sstevel@tonic-gate 		rc = icfg_sockaddr_to_str(protocol, (struct sockaddr *)&taddr,
31607c478bd9Sstevel@tonic-gate 		    srcbuf, sizeof (srcbuf));
31617c478bd9Sstevel@tonic-gate 		if (rc != ICFG_SUCCESS) {
31627c478bd9Sstevel@tonic-gate 			Perror0_exit((char *)icfg_errmsg(rc));
31637c478bd9Sstevel@tonic-gate 		}
31647c478bd9Sstevel@tonic-gate 	}
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate 	(void) printf(" tunnel src %s ", srcbuf);
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate 	rc = icfg_get_tunnel_dest(handle, (struct sockaddr *)&taddr, &socklen);
31697c478bd9Sstevel@tonic-gate 	if (rc == ICFG_NOT_SET) {
31707c478bd9Sstevel@tonic-gate 		(void) printf("\n");
31717c478bd9Sstevel@tonic-gate 	} else {
31727c478bd9Sstevel@tonic-gate 		rc = icfg_sockaddr_to_str(protocol, (struct sockaddr *)&taddr,
31737c478bd9Sstevel@tonic-gate 		    dstbuf, sizeof (dstbuf));
31747c478bd9Sstevel@tonic-gate 		if (rc != ICFG_SUCCESS) {
31757c478bd9Sstevel@tonic-gate 			Perror0_exit((char *)icfg_errmsg(rc));
31767c478bd9Sstevel@tonic-gate 		}
31777c478bd9Sstevel@tonic-gate 		(void) printf("tunnel dst %s\n", dstbuf);
31787c478bd9Sstevel@tonic-gate 	}
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate 	if (handle->ifh_tunnel_params != NULL &&
31817c478bd9Sstevel@tonic-gate 	    (handle->ifh_tunnel_params->ifta_flags & IFTUN_SECURITY))
31827c478bd9Sstevel@tonic-gate 		print_tsec(handle->ifh_tunnel_params);
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate 	/*
31857c478bd9Sstevel@tonic-gate 	 * tabbed indicates tabbed and printed.  Use it tell us whether
31867c478bd9Sstevel@tonic-gate 	 * to tab and that we've printed something here, so we need a
31877c478bd9Sstevel@tonic-gate 	 * newline
31887c478bd9Sstevel@tonic-gate 	 */
31897c478bd9Sstevel@tonic-gate 	tabbed = _B_FALSE;
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate 	if (icfg_get_tunnel_hoplimit(handle, &hoplimit) == ICFG_SUCCESS) {
31927c478bd9Sstevel@tonic-gate 		(void) printf("\ttunnel hop limit %d ", hoplimit);
31937c478bd9Sstevel@tonic-gate 		tabbed = _B_TRUE;
31947c478bd9Sstevel@tonic-gate 	}
31957c478bd9Sstevel@tonic-gate 
31967c478bd9Sstevel@tonic-gate 	if ((protocol == AF_INET6) &&
31977c478bd9Sstevel@tonic-gate 	    (icfg_get_tunnel_encaplimit(handle, &encaplimit) ==
31987c478bd9Sstevel@tonic-gate 		ICFG_SUCCESS)) {
31997c478bd9Sstevel@tonic-gate 		if (!tabbed) {
32007c478bd9Sstevel@tonic-gate 			(void) printf("\t");
32017c478bd9Sstevel@tonic-gate 			tabbed = _B_TRUE;
32027c478bd9Sstevel@tonic-gate 		}
32037c478bd9Sstevel@tonic-gate 		if (encaplimit >= 0) {
32047c478bd9Sstevel@tonic-gate 			(void) printf("tunnel encapsulation limit %d",
32057c478bd9Sstevel@tonic-gate 			    encaplimit);
32067c478bd9Sstevel@tonic-gate 		} else {
32077c478bd9Sstevel@tonic-gate 			(void) printf("tunnel encapsulation limit disabled");
32087c478bd9Sstevel@tonic-gate 		}
32097c478bd9Sstevel@tonic-gate 	}
32107c478bd9Sstevel@tonic-gate 
32117c478bd9Sstevel@tonic-gate 	if (tabbed)
32127c478bd9Sstevel@tonic-gate 		(void) printf("\n");
32137c478bd9Sstevel@tonic-gate 
32147c478bd9Sstevel@tonic-gate 	icfg_close(handle);
32157c478bd9Sstevel@tonic-gate }
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate static void
32187c478bd9Sstevel@tonic-gate in_status(int force, uint64_t flags)
32197c478bd9Sstevel@tonic-gate {
32207c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin, *laddr;
32217c478bd9Sstevel@tonic-gate 	struct	sockaddr_in netmask = { AF_INET };
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate 	if (debug)
32247c478bd9Sstevel@tonic-gate 		(void) printf("in_status(%s) flags 0x%llx\n", name, flags);
32257c478bd9Sstevel@tonic-gate 
32267c478bd9Sstevel@tonic-gate 	/* only print status for IPv4 interfaces */
32277c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV4))
32287c478bd9Sstevel@tonic-gate 		return;
32297c478bd9Sstevel@tonic-gate 
32307c478bd9Sstevel@tonic-gate 	/* if the interface is a tunnel, print the tunnel status */
32317c478bd9Sstevel@tonic-gate 	tun_status();
32327c478bd9Sstevel@tonic-gate 
32337c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
32347c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32357c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
32367c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
32377c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
32387c478bd9Sstevel@tonic-gate 				if (!force)
32397c478bd9Sstevel@tonic-gate 					return;
32407c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
32417c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
32427c478bd9Sstevel@tonic-gate 			} else
32437c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status: SIOCGLIFADDR");
32447c478bd9Sstevel@tonic-gate 		}
32457c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
32467c478bd9Sstevel@tonic-gate 		(void) printf("\tinet %s ", inet_ntoa(sin->sin_addr));
32477c478bd9Sstevel@tonic-gate 		laddr = sin;
32487c478bd9Sstevel@tonic-gate 	} else {
32497c478bd9Sstevel@tonic-gate 		(void) printf("\tinet ");
32507c478bd9Sstevel@tonic-gate 	}
32517c478bd9Sstevel@tonic-gate 
32527c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32537c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
32547c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
32557c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
32567c478bd9Sstevel@tonic-gate 			if (!force)
32577c478bd9Sstevel@tonic-gate 				return;
32587c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
32597c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
32607c478bd9Sstevel@tonic-gate 		} else {
32617c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status: SIOCGLIFSUBNET");
32627c478bd9Sstevel@tonic-gate 		}
32637c478bd9Sstevel@tonic-gate 	}
32647c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
32657c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
32667c478bd9Sstevel@tonic-gate 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
32677c478bd9Sstevel@tonic-gate 		(void) printf("subnet %s/%d ", inet_ntoa(sin->sin_addr),
32687c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
32697c478bd9Sstevel@tonic-gate 	}
32707c478bd9Sstevel@tonic-gate 	if (sin->sin_family != AF_INET) {
32717c478bd9Sstevel@tonic-gate 		(void) printf("Wrong family: %d\n", sin->sin_family);
32727c478bd9Sstevel@tonic-gate 	}
32737c478bd9Sstevel@tonic-gate 
32747c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32757c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
32767c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL)
32777c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status: SIOCGLIFNETMASK");
32787c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
32797c478bd9Sstevel@tonic-gate 	} else
32807c478bd9Sstevel@tonic-gate 		netmask.sin_addr =
32817c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
32827c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
32837c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32847c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
32857c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
32867c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
32877c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
32887c478bd9Sstevel@tonic-gate 			else
32897c478bd9Sstevel@tonic-gate 			    Perror0_exit("in_status: SIOCGLIFDSTADDR");
32907c478bd9Sstevel@tonic-gate 		}
32917c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
32927c478bd9Sstevel@tonic-gate 		(void) printf("--> %s ", inet_ntoa(sin->sin_addr));
32937c478bd9Sstevel@tonic-gate 	}
32947c478bd9Sstevel@tonic-gate 	(void) printf("netmask %x ", ntohl(netmask.sin_addr.s_addr));
32957c478bd9Sstevel@tonic-gate 	if (flags & IFF_BROADCAST) {
32967c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32977c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
32987c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
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_status: SIOCGLIFBRDADDR");
33037c478bd9Sstevel@tonic-gate 		}
33047c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
33057c478bd9Sstevel@tonic-gate 		if (sin->sin_addr.s_addr != 0) {
33067c478bd9Sstevel@tonic-gate 			(void) printf("broadcast %s",
33077c478bd9Sstevel@tonic-gate 			    inet_ntoa(sin->sin_addr));
33087c478bd9Sstevel@tonic-gate 		}
33097c478bd9Sstevel@tonic-gate 	}
33107c478bd9Sstevel@tonic-gate 	/* If there is a groupname, print it for lun 0 alone */
33117c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') == NULL) {
33127c478bd9Sstevel@tonic-gate 		(void) memset(lifr.lifr_groupname, 0,
33137c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_groupname));
33147c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr) >= 0) {
33157c478bd9Sstevel@tonic-gate 			if (strlen(lifr.lifr_groupname) > 0) {
33167c478bd9Sstevel@tonic-gate 				(void) printf("\n\tgroupname %s",
33177c478bd9Sstevel@tonic-gate 				    lifr.lifr_groupname);
33187c478bd9Sstevel@tonic-gate 			}
33197c478bd9Sstevel@tonic-gate 		}
33207c478bd9Sstevel@tonic-gate 	}
33217c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
33227c478bd9Sstevel@tonic-gate }
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate static void
33257c478bd9Sstevel@tonic-gate in6_status(int force, uint64_t flags)
33267c478bd9Sstevel@tonic-gate {
33277c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
33287c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6, *laddr6;
33297c478bd9Sstevel@tonic-gate 
33307c478bd9Sstevel@tonic-gate 	if (debug)
33317c478bd9Sstevel@tonic-gate 		(void) printf("in6_status(%s) flags 0x%llx\n", name, flags);
33327c478bd9Sstevel@tonic-gate 
33337c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV6))
33347c478bd9Sstevel@tonic-gate 		return;
33357c478bd9Sstevel@tonic-gate 
33367c478bd9Sstevel@tonic-gate 	/* if the interface is a tunnel, print the tunnel status */
33377c478bd9Sstevel@tonic-gate 	tun_status();
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
33407c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33417c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
33427c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
33437c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
33447c478bd9Sstevel@tonic-gate 				if (!force)
33457c478bd9Sstevel@tonic-gate 					return;
33467c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33477c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33487c478bd9Sstevel@tonic-gate 			} else
33497c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status6: SIOCGLIFADDR");
33507c478bd9Sstevel@tonic-gate 		}
33517c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
33527c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6 %s/%d ",
33537c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33547c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
33557c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
33567c478bd9Sstevel@tonic-gate 		laddr6 = sin6;
33577c478bd9Sstevel@tonic-gate 	} else {
33587c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6 ");
33597c478bd9Sstevel@tonic-gate 	}
33607c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33617c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
33627c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
33637c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
33647c478bd9Sstevel@tonic-gate 			if (!force)
33657c478bd9Sstevel@tonic-gate 				return;
33667c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
33677c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
33687c478bd9Sstevel@tonic-gate 		} else
33697c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status6: SIOCGLIFSUBNET");
33707c478bd9Sstevel@tonic-gate 	}
33717c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
33727c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
33737c478bd9Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
33747c478bd9Sstevel@tonic-gate 		(void) printf("subnet %s/%d ",
33757c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33767c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
33777c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
33787c478bd9Sstevel@tonic-gate 	}
33797c478bd9Sstevel@tonic-gate 	if (sin6->sin6_family != AF_INET6) {
33807c478bd9Sstevel@tonic-gate 		(void) printf("Wrong family: %d\n", sin6->sin6_family);
33817c478bd9Sstevel@tonic-gate 	}
33827c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
33837c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33847c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
33857c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
33867c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33877c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33887c478bd9Sstevel@tonic-gate 			else
33897c478bd9Sstevel@tonic-gate 			    Perror0_exit("in_status6: SIOCGLIFDSTADDR");
33907c478bd9Sstevel@tonic-gate 		}
33917c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
33927c478bd9Sstevel@tonic-gate 		(void) printf("--> %s ",
33937c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33947c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)));
33957c478bd9Sstevel@tonic-gate 	}
33967c478bd9Sstevel@tonic-gate 	if (verbose) {
33977c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
33987c478bd9Sstevel@tonic-gate 		(void) putchar('\t');
33997c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34007c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
34017c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EINVAL)
34027c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34037c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34047c478bd9Sstevel@tonic-gate 			else
34057c478bd9Sstevel@tonic-gate 			    Perror0_exit("in_status6: SIOCGLIFTOKEN");
34067c478bd9Sstevel@tonic-gate 		} else {
34077c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
34087c478bd9Sstevel@tonic-gate 			(void) printf("token %s/%d ",
34097c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
34107c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
34117c478bd9Sstevel@tonic-gate 			    lifr.lifr_addrlen);
34127c478bd9Sstevel@tonic-gate 		}
34137c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFLNKINFO, (caddr_t)&lifr) < 0) {
34147c478bd9Sstevel@tonic-gate 			if (errno != EINVAL) {
34157c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status6: SIOCGLIFLNKINFO");
34167c478bd9Sstevel@tonic-gate 			}
34177c478bd9Sstevel@tonic-gate 		} else {
34187c478bd9Sstevel@tonic-gate 			(void) printf("maxhops %u, reachtime %u ms, "
34197c478bd9Sstevel@tonic-gate 			    "reachretrans %u ms, maxmtu %u ",
34207c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_maxhops,
34217c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_reachtime,
34227c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_reachretrans,
34237c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_maxmtu);
34247c478bd9Sstevel@tonic-gate 		}
34257c478bd9Sstevel@tonic-gate 	}
34267c478bd9Sstevel@tonic-gate 	/* If there is a groupname, print it for lun 0 alone */
34277c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') == NULL) {
34287c478bd9Sstevel@tonic-gate 		(void) memset(lifr.lifr_groupname, 0,
34297c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_groupname));
34307c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr) >= 0) {
34317c478bd9Sstevel@tonic-gate 			if (strlen(lifr.lifr_groupname) > 0) {
34327c478bd9Sstevel@tonic-gate 				(void) printf("\n\tgroupname %s",
34337c478bd9Sstevel@tonic-gate 				    lifr.lifr_groupname);
34347c478bd9Sstevel@tonic-gate 			}
34357c478bd9Sstevel@tonic-gate 		}
34367c478bd9Sstevel@tonic-gate 	}
34377c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
34387c478bd9Sstevel@tonic-gate }
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate static void
34417c478bd9Sstevel@tonic-gate in_configinfo(int force, uint64_t flags)
34427c478bd9Sstevel@tonic-gate {
34437c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin, *laddr;
34447c478bd9Sstevel@tonic-gate 	struct	sockaddr_in netmask = { AF_INET };
34457c478bd9Sstevel@tonic-gate 
34467c478bd9Sstevel@tonic-gate 	if (debug)
34477c478bd9Sstevel@tonic-gate 		(void) printf("in_configinfo(%s) flags 0x%llx\n", name, flags);
34487c478bd9Sstevel@tonic-gate 
34497c478bd9Sstevel@tonic-gate 	/* only configinfo info for IPv4 interfaces */
34507c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV4))
34517c478bd9Sstevel@tonic-gate 		return;
34527c478bd9Sstevel@tonic-gate 
34537c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
34547c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34557c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
34567c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
34577c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
34587c478bd9Sstevel@tonic-gate 				if (!force)
34597c478bd9Sstevel@tonic-gate 					return;
34607c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34617c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34627c478bd9Sstevel@tonic-gate 			} else
34637c478bd9Sstevel@tonic-gate 				Perror0_exit("in_configinfo: SIOCGLIFADDR");
34647c478bd9Sstevel@tonic-gate 		}
34657c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
34667c478bd9Sstevel@tonic-gate 		if (get_lun(name) != 0) {
34677c478bd9Sstevel@tonic-gate 			(void) printf(" addif %s ", inet_ntoa(sin->sin_addr));
34687c478bd9Sstevel@tonic-gate 		} else {
34697c478bd9Sstevel@tonic-gate 			(void) printf(" set %s ", inet_ntoa(sin->sin_addr));
34707c478bd9Sstevel@tonic-gate 		}
34717c478bd9Sstevel@tonic-gate 		laddr = sin;
34727c478bd9Sstevel@tonic-gate 	}
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34757c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
34767c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
34777c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
34787c478bd9Sstevel@tonic-gate 			if (!force)
34797c478bd9Sstevel@tonic-gate 				return;
34807c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
34817c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
34827c478bd9Sstevel@tonic-gate 		} else {
34837c478bd9Sstevel@tonic-gate 			Perror0_exit("in_configinfo: SIOCGLIFSUBNET");
34847c478bd9Sstevel@tonic-gate 		}
34857c478bd9Sstevel@tonic-gate 	}
34867c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
34877c478bd9Sstevel@tonic-gate 
34887c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
34897c478bd9Sstevel@tonic-gate 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
34907c478bd9Sstevel@tonic-gate 		(void) printf(" subnet %s/%d ", inet_ntoa(sin->sin_addr),
34917c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
34927c478bd9Sstevel@tonic-gate 	}
34937c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34947c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
34957c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL)
34967c478bd9Sstevel@tonic-gate 			Perror0_exit("in_configinfo: SIOCGLIFNETMASK");
34977c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
34987c478bd9Sstevel@tonic-gate 	} else
34997c478bd9Sstevel@tonic-gate 		netmask.sin_addr =
35007c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
35017c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
35027c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35037c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
35047c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
35057c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
35067c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
35077c478bd9Sstevel@tonic-gate 			else
35087c478bd9Sstevel@tonic-gate 			    Perror0_exit("in_configinfo: SIOCGLIFDSTADDR");
35097c478bd9Sstevel@tonic-gate 		}
35107c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
35117c478bd9Sstevel@tonic-gate 		(void) printf(" destination %s ", inet_ntoa(sin->sin_addr));
35127c478bd9Sstevel@tonic-gate 	}
35137c478bd9Sstevel@tonic-gate 	(void) printf(" netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
35147c478bd9Sstevel@tonic-gate 	if (flags & IFF_BROADCAST) {
35157c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35167c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
35177c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
35187c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
35197c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
35207c478bd9Sstevel@tonic-gate 			else
35217c478bd9Sstevel@tonic-gate 			    Perror0_exit("in_configinfo: SIOCGLIFBRDADDR");
35227c478bd9Sstevel@tonic-gate 		}
35237c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
35247c478bd9Sstevel@tonic-gate 		if (sin->sin_addr.s_addr != 0) {
35257c478bd9Sstevel@tonic-gate 			(void) printf(" broadcast %s ",
35267c478bd9Sstevel@tonic-gate 			    inet_ntoa(sin->sin_addr));
35277c478bd9Sstevel@tonic-gate 		}
35287c478bd9Sstevel@tonic-gate 	}
35297c478bd9Sstevel@tonic-gate 
35307c478bd9Sstevel@tonic-gate 	/* If there is a groupname, print it for lun 0 alone */
35317c478bd9Sstevel@tonic-gate 	if (get_lun(name) == 0) {
35327c478bd9Sstevel@tonic-gate 		(void) memset(lifr.lifr_groupname, 0,
35337c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_groupname));
35347c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr) >= 0) {
35357c478bd9Sstevel@tonic-gate 			if (strlen(lifr.lifr_groupname) > 0) {
35367c478bd9Sstevel@tonic-gate 				(void) printf(" group %s ",
35377c478bd9Sstevel@tonic-gate 				    lifr.lifr_groupname);
35387c478bd9Sstevel@tonic-gate 			}
35397c478bd9Sstevel@tonic-gate 		}
35407c478bd9Sstevel@tonic-gate 	}
35417c478bd9Sstevel@tonic-gate 
35427c478bd9Sstevel@tonic-gate 	/* Print flags to configure */
35437c478bd9Sstevel@tonic-gate 	print_config_flags(flags);
35447c478bd9Sstevel@tonic-gate 
35457c478bd9Sstevel@tonic-gate 	/* IFF_NOARP applies to AF_INET only */
35467c478bd9Sstevel@tonic-gate 	if (flags & IFF_NOARP) {
35477c478bd9Sstevel@tonic-gate 		(void) printf("-arp ");
35487c478bd9Sstevel@tonic-gate 	}
35497c478bd9Sstevel@tonic-gate }
35507c478bd9Sstevel@tonic-gate 
35517c478bd9Sstevel@tonic-gate static void
35527c478bd9Sstevel@tonic-gate in6_configinfo(int force, uint64_t flags)
35537c478bd9Sstevel@tonic-gate {
35547c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
35557c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6, *laddr6;
35567c478bd9Sstevel@tonic-gate 
35577c478bd9Sstevel@tonic-gate 	if (debug)
35587c478bd9Sstevel@tonic-gate 		(void) printf("in6_configinfo(%s) flags 0x%llx\n", name,
35597c478bd9Sstevel@tonic-gate 		    flags);
35607c478bd9Sstevel@tonic-gate 
35617c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV6))
35627c478bd9Sstevel@tonic-gate 		return;
35637c478bd9Sstevel@tonic-gate 
35647c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
35657c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35667c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
35677c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
35687c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
35697c478bd9Sstevel@tonic-gate 				if (!force)
35707c478bd9Sstevel@tonic-gate 					return;
35717c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
35727c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
35737c478bd9Sstevel@tonic-gate 			} else
35747c478bd9Sstevel@tonic-gate 				Perror0_exit("in6_configinfo: SIOCGLIFADDR");
35757c478bd9Sstevel@tonic-gate 		}
35767c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
35777c478bd9Sstevel@tonic-gate 		if (get_lun(name) != 0) {
35787c478bd9Sstevel@tonic-gate 			(void) printf(" addif %s/%d ",
35797c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35807c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
35817c478bd9Sstevel@tonic-gate 			    lifr.lifr_addrlen);
35827c478bd9Sstevel@tonic-gate 		} else {
35837c478bd9Sstevel@tonic-gate 			(void) printf(" set %s/%d ",
35847c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35857c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
35867c478bd9Sstevel@tonic-gate 			    lifr.lifr_addrlen);
35877c478bd9Sstevel@tonic-gate 		}
35887c478bd9Sstevel@tonic-gate 		laddr6 = sin6;
35897c478bd9Sstevel@tonic-gate 	}
35907c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35917c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
35927c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
35937c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
35947c478bd9Sstevel@tonic-gate 			if (!force)
35957c478bd9Sstevel@tonic-gate 				return;
35967c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
35977c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
35987c478bd9Sstevel@tonic-gate 		} else
35997c478bd9Sstevel@tonic-gate 			Perror0_exit("in6_configinfo: SIOCGLIFSUBNET");
36007c478bd9Sstevel@tonic-gate 	}
36017c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
36027c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
36037c478bd9Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
36047c478bd9Sstevel@tonic-gate 		(void) printf(" subnet %s/%d ",
36057c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
36067c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
36077c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
36087c478bd9Sstevel@tonic-gate 	}
36097c478bd9Sstevel@tonic-gate 
36107c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
36117c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
36127c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
36137c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
36147c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
36157c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
36167c478bd9Sstevel@tonic-gate 			else
36177c478bd9Sstevel@tonic-gate 			    Perror0_exit("in6_configinfo: SIOCGLIFDSTADDR");
36187c478bd9Sstevel@tonic-gate 		}
36197c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
36207c478bd9Sstevel@tonic-gate 		(void) printf(" destination %s ",
36217c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
36227c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)));
36237c478bd9Sstevel@tonic-gate 	}
36247c478bd9Sstevel@tonic-gate 
36257c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
36267c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
36277c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EINVAL)
36287c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
36297c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
36307c478bd9Sstevel@tonic-gate 		else
36317c478bd9Sstevel@tonic-gate 		    Perror0_exit("in6_configinfo: SIOCGLIFTOKEN");
36327c478bd9Sstevel@tonic-gate 	} else {
36337c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
36347c478bd9Sstevel@tonic-gate 		(void) printf(" token %s/%d ",
36357c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
36367c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
36377c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
36387c478bd9Sstevel@tonic-gate 	}
36397c478bd9Sstevel@tonic-gate 
36407c478bd9Sstevel@tonic-gate 	/* If there is a groupname, print it for lun 0 alone */
36417c478bd9Sstevel@tonic-gate 	if (get_lun(name) == 0) {
36427c478bd9Sstevel@tonic-gate 		(void) memset(lifr.lifr_groupname, 0,
36437c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_groupname));
36447c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr) >= 0) {
36457c478bd9Sstevel@tonic-gate 			if (strlen(lifr.lifr_groupname) > 0) {
36467c478bd9Sstevel@tonic-gate 				(void) printf(" group %s ",
36477c478bd9Sstevel@tonic-gate 				    lifr.lifr_groupname);
36487c478bd9Sstevel@tonic-gate 			}
36497c478bd9Sstevel@tonic-gate 		}
36507c478bd9Sstevel@tonic-gate 	}
36517c478bd9Sstevel@tonic-gate 
36527c478bd9Sstevel@tonic-gate 	/* Print flags to configure */
36537c478bd9Sstevel@tonic-gate 	print_config_flags(flags);
36547c478bd9Sstevel@tonic-gate 
36557c478bd9Sstevel@tonic-gate 	/* IFF_NONUD applies to AF_INET6 only */
36567c478bd9Sstevel@tonic-gate 	if (flags & IFF_NONUD) {
36577c478bd9Sstevel@tonic-gate 		(void) printf("-nud ");
36587c478bd9Sstevel@tonic-gate 	}
36597c478bd9Sstevel@tonic-gate }
36607c478bd9Sstevel@tonic-gate 
36617c478bd9Sstevel@tonic-gate /* ARGSUSED */
36627c478bd9Sstevel@tonic-gate static int
36637c478bd9Sstevel@tonic-gate get_lun(char *rsrc)
36647c478bd9Sstevel@tonic-gate {
36657c478bd9Sstevel@tonic-gate 	char resource[LIFNAMSIZ];
36667c478bd9Sstevel@tonic-gate 	char *cp;
36677c478bd9Sstevel@tonic-gate 
36687c478bd9Sstevel@tonic-gate 	(void) strcpy(resource, rsrc);
36697c478bd9Sstevel@tonic-gate 
36707c478bd9Sstevel@tonic-gate 	/* remove LIF component */
36717c478bd9Sstevel@tonic-gate 	cp = strchr(resource, ':');
36727c478bd9Sstevel@tonic-gate 	if (cp) {
36737c478bd9Sstevel@tonic-gate 		cp++;
36747c478bd9Sstevel@tonic-gate 		return (atoi(cp));
36757c478bd9Sstevel@tonic-gate 	}
36767c478bd9Sstevel@tonic-gate 
36777c478bd9Sstevel@tonic-gate 	return (0);
36787c478bd9Sstevel@tonic-gate }
36797c478bd9Sstevel@tonic-gate 
36807c478bd9Sstevel@tonic-gate /*
36817c478bd9Sstevel@tonic-gate  * We need to plink both the arp-device stream and the arp-ip-device stream.
36827c478bd9Sstevel@tonic-gate  * However the muxid is stored only in IP. Plumbing 2 streams individually
36837c478bd9Sstevel@tonic-gate  * is not atomic, and if ifconfig is killed, the resulting plumbing can
36847c478bd9Sstevel@tonic-gate  * be inconsistent. For eg. if only the arp stream is plumbed, we have lost
36857c478bd9Sstevel@tonic-gate  * the muxid, and the half-baked plumbing can neither be unplumbed nor
36867c478bd9Sstevel@tonic-gate  * replumbed, thus requiring a reboot. To avoid the above the following
36877c478bd9Sstevel@tonic-gate  * scheme is used.
36887c478bd9Sstevel@tonic-gate  *
36897c478bd9Sstevel@tonic-gate  * Ifconfig asks IP to enforce atomicity of plumbing the arp and IP streams.
36907c478bd9Sstevel@tonic-gate  * This is done by pushing arp on to the mux (/dev/udp). ARP adds some
36917c478bd9Sstevel@tonic-gate  * extra information in the I_PLINK and I_PUNLINK ioctls to let IP know
36927c478bd9Sstevel@tonic-gate  * that the plumbing/unplumbing has to be done atomically. Ifconfig plumbs
36937c478bd9Sstevel@tonic-gate  * the IP stream first, and unplumbs it last. The kernel (IP) does not
36947c478bd9Sstevel@tonic-gate  * allow IP stream to be unplumbed without unplumbing arp stream. Similarly
36957c478bd9Sstevel@tonic-gate  * it does not allow arp stream to be plumbed before IP stream is plumbed.
36967c478bd9Sstevel@tonic-gate  * There is no need to use SIOCSLIFMUXID, since the whole operation is atomic,
36977c478bd9Sstevel@tonic-gate  * and IP uses the info in the I_PLINK message to get the muxid.
36987c478bd9Sstevel@tonic-gate  *
36997c478bd9Sstevel@tonic-gate  * a. STREAMS does not allow us to use /dev/ip itself as the mux. So we use
37007c478bd9Sstevel@tonic-gate  *    /dev/udp[6].
37017c478bd9Sstevel@tonic-gate  * b. SIOCGLIFMUXID returns the muxid corresponding to the V4 or V6 stream
37027c478bd9Sstevel@tonic-gate  *    depending on the open i.e. V4 vs V6 open. So we need to use /dev/udp
37037c478bd9Sstevel@tonic-gate  *    or /dev/udp6.
37047c478bd9Sstevel@tonic-gate  * c. We need to push ARP in order to get the required kernel support for
37057c478bd9Sstevel@tonic-gate  *    atomic plumbings. The actual work done by ARP is explained in arp.c
37067c478bd9Sstevel@tonic-gate  *    Without pushing ARP, we will still be able to plumb/unplumb. But
37077c478bd9Sstevel@tonic-gate  *    it is not atomic, and is supported by the kernel for backward
37087c478bd9Sstevel@tonic-gate  *    compatibility for other utilities like atmifconfig etc. In this case
37097c478bd9Sstevel@tonic-gate  *    the utility must use SIOCSLIFMUXID.
37107c478bd9Sstevel@tonic-gate  */
37117c478bd9Sstevel@tonic-gate static void
37127c478bd9Sstevel@tonic-gate plumb_one_device(dlpi_if_attr_t *diap, int ip_fd, int af)
37137c478bd9Sstevel@tonic-gate {
37147c478bd9Sstevel@tonic-gate 	int	arp_fd = -1;
37157c478bd9Sstevel@tonic-gate 	int	arp_muxid = -1, ip_muxid;
37167c478bd9Sstevel@tonic-gate 	int	mux_fd;
37177c478bd9Sstevel@tonic-gate 	char	*udp_dev_name;
37187c478bd9Sstevel@tonic-gate 	dlpi_if_attr_t	dia;
37197c478bd9Sstevel@tonic-gate 
37207c478bd9Sstevel@tonic-gate 	if (debug)
37217c478bd9Sstevel@tonic-gate 		(void) printf("plumb_one_device: ifname %s, ppa %d\n",
37227c478bd9Sstevel@tonic-gate 		    diap->ifname, diap->ppa);
37237c478bd9Sstevel@tonic-gate 
37247c478bd9Sstevel@tonic-gate 	if (diap->style == DL_STYLE2 && dlpi_detach(ip_fd, -1) < 0)
37257c478bd9Sstevel@tonic-gate 		Perror0_exit("dlpi_detach");
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, I_PUSH, IP_MOD_NAME) == -1)
37287c478bd9Sstevel@tonic-gate 		Perror2_exit("I_PUSH", IP_MOD_NAME);
37297c478bd9Sstevel@tonic-gate 
37307c478bd9Sstevel@tonic-gate 	/*
37317c478bd9Sstevel@tonic-gate 	 * Push the ARP module onto the interface stream. IP uses
37327c478bd9Sstevel@tonic-gate 	 * this to send resolution requests up to ARP. We need to
37337c478bd9Sstevel@tonic-gate 	 * do this before the SLIFNAME ioctl is sent down because
37347c478bd9Sstevel@tonic-gate 	 * the interface becomes publicly known as soon as the SLIFNAME
37357c478bd9Sstevel@tonic-gate 	 * ioctl completes. Thus some other process trying to bring up
37367c478bd9Sstevel@tonic-gate 	 * the interface after SLIFNAME but before we have pushed ARP
37377c478bd9Sstevel@tonic-gate 	 * could hang. We pop the module again later if it is not needed.
37387c478bd9Sstevel@tonic-gate 	 */
37397c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, I_PUSH, ARP_MOD_NAME) == -1)
37407c478bd9Sstevel@tonic-gate 		Perror2_exit("I_PUSH", ARP_MOD_NAME);
37417c478bd9Sstevel@tonic-gate 
37427c478bd9Sstevel@tonic-gate 	/*
37437c478bd9Sstevel@tonic-gate 	 * Set IFF_IPV4/IFF_IPV6 flags.
37447c478bd9Sstevel@tonic-gate 	 * At this point in time the kernel also allows an
37457c478bd9Sstevel@tonic-gate 	 * override of the CANTCHANGE flags.
37467c478bd9Sstevel@tonic-gate 	 */
37477c478bd9Sstevel@tonic-gate 
37487c478bd9Sstevel@tonic-gate 	lifr.lifr_name[0] = '\0';
37497c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, SIOCGLIFFLAGS, (char *)&lifr) == -1)
37507c478bd9Sstevel@tonic-gate 		Perror0_exit("plumb_one_device: SIOCGLIFFLAGS");
37517c478bd9Sstevel@tonic-gate 
37527c478bd9Sstevel@tonic-gate 	/* Set the name string and the IFF_IPV* flag */
37537c478bd9Sstevel@tonic-gate 	if (af == AF_INET6) {
37547c478bd9Sstevel@tonic-gate 		lifr.lifr_flags |= IFF_IPV6;
37557c478bd9Sstevel@tonic-gate 		lifr.lifr_flags &= ~(IFF_BROADCAST | IFF_IPV4);
37567c478bd9Sstevel@tonic-gate 	} else {
37577c478bd9Sstevel@tonic-gate 		lifr.lifr_flags |= IFF_IPV4;
37587c478bd9Sstevel@tonic-gate 		lifr.lifr_flags &= ~IFF_IPV6;
37597c478bd9Sstevel@tonic-gate 	}
37607c478bd9Sstevel@tonic-gate 
37617c478bd9Sstevel@tonic-gate 	/* record the device and module names as interface name */
37627c478bd9Sstevel@tonic-gate 	lifr.lifr_ppa = diap->ppa;
37637c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
37647c478bd9Sstevel@tonic-gate 
37657c478bd9Sstevel@tonic-gate 	/* set the interface name */
37667c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, SIOCSLIFNAME, (char *)&lifr) == -1) {
37677c478bd9Sstevel@tonic-gate 		if (errno != EEXIST)
37687c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCSLIFNAME for ip");
37697c478bd9Sstevel@tonic-gate 		/*
37707c478bd9Sstevel@tonic-gate 		 * This difference between the way we behave for EEXIST
37717c478bd9Sstevel@tonic-gate 		 * and that with other errors exists to preserve legacy
37727c478bd9Sstevel@tonic-gate 		 * behaviour. Earlier when foreachinterface() and matcif()
37737c478bd9Sstevel@tonic-gate 		 * were doing the duplicate interface name checks, for
37747c478bd9Sstevel@tonic-gate 		 * already existing interfaces, inetplumb() returned "0".
37757c478bd9Sstevel@tonic-gate 		 * To preserve this behaviour, Perror0() and return are
37767c478bd9Sstevel@tonic-gate 		 * called for EEXIST.
37777c478bd9Sstevel@tonic-gate 		 */
37787c478bd9Sstevel@tonic-gate 		Perror0("SIOCSLIFNAME for ip");
37797c478bd9Sstevel@tonic-gate 		return;
37807c478bd9Sstevel@tonic-gate 	}
37817c478bd9Sstevel@tonic-gate 
37827c478bd9Sstevel@tonic-gate 	/* Get the full set of existing flags for this stream */
37837c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, SIOCGLIFFLAGS, (char *)&lifr) == -1)
37847c478bd9Sstevel@tonic-gate 		Perror0_exit("plumb_one_device: SIOCFLIFFLAGS");
37857c478bd9Sstevel@tonic-gate 
37867c478bd9Sstevel@tonic-gate 	if (debug) {
37877c478bd9Sstevel@tonic-gate 		(void) printf("plumb_one_device: %s got flags: \n",
37887c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
37897c478bd9Sstevel@tonic-gate 		print_flags(lifr.lifr_flags);
37907c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
37917c478bd9Sstevel@tonic-gate 	}
37927c478bd9Sstevel@tonic-gate 
37937c478bd9Sstevel@tonic-gate 	/* Check if arp is not actually needed */
37947c478bd9Sstevel@tonic-gate 	if (lifr.lifr_flags & (IFF_NOARP|IFF_IPV6)) {
37957c478bd9Sstevel@tonic-gate 		if (ioctl(ip_fd, I_POP, 0) == -1)
37967c478bd9Sstevel@tonic-gate 			Perror2_exit("I_POP", ARP_MOD_NAME);
37977c478bd9Sstevel@tonic-gate 	}
37987c478bd9Sstevel@tonic-gate 
37997c478bd9Sstevel@tonic-gate 	/*
38007c478bd9Sstevel@tonic-gate 	 * Open "/dev/udp" for use as a multiplexor to PLINK the
38017c478bd9Sstevel@tonic-gate 	 * interface stream under. We use "/dev/udp" instead of "/dev/ip"
38027c478bd9Sstevel@tonic-gate 	 * since STREAMS will not let you PLINK a driver under itself,
38037c478bd9Sstevel@tonic-gate 	 * and "/dev/ip" is typically the driver at the bottom of
38047c478bd9Sstevel@tonic-gate 	 * the stream for tunneling interfaces.
38057c478bd9Sstevel@tonic-gate 	 */
38067c478bd9Sstevel@tonic-gate 	if (af == AF_INET6)
38077c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP6_DEV_NAME;
38087c478bd9Sstevel@tonic-gate 	else
38097c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP_DEV_NAME;
38107c478bd9Sstevel@tonic-gate 	if ((mux_fd = open_arp_on_udp(udp_dev_name)) == -1)
38117c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
38127c478bd9Sstevel@tonic-gate 
38137c478bd9Sstevel@tonic-gate 	/* Check if arp is not needed */
38147c478bd9Sstevel@tonic-gate 	if (lifr.lifr_flags & (IFF_NOARP|IFF_IPV6)) {
38157c478bd9Sstevel@tonic-gate 		/*
38167c478bd9Sstevel@tonic-gate 		 * PLINK the interface stream so that ifconfig can exit
38177c478bd9Sstevel@tonic-gate 		 * without tearing down the stream.
38187c478bd9Sstevel@tonic-gate 		 */
38197c478bd9Sstevel@tonic-gate 		if ((ip_muxid = ioctl(mux_fd, I_PLINK, ip_fd)) == -1) {
38207c478bd9Sstevel@tonic-gate 			Perror0_exit("I_PLINK for ip");
38217c478bd9Sstevel@tonic-gate 		}
38227c478bd9Sstevel@tonic-gate 		(void) close(mux_fd);
38237c478bd9Sstevel@tonic-gate 		return;
38247c478bd9Sstevel@tonic-gate 	}
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate 	/*
38277c478bd9Sstevel@tonic-gate 	 * This interface does use ARP, so set up a separate stream
38287c478bd9Sstevel@tonic-gate 	 * from the interface to ARP.
38297c478bd9Sstevel@tonic-gate 	 *
38307c478bd9Sstevel@tonic-gate 	 * Note: modules specified by the user are pushed
38317c478bd9Sstevel@tonic-gate 	 * only on the interface stream, not on the ARP stream.
38327c478bd9Sstevel@tonic-gate 	 */
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate 	if (debug)
38357c478bd9Sstevel@tonic-gate 		(void) printf("plumb_one_device: ifname: %s\n",
38367c478bd9Sstevel@tonic-gate 		    diap->ifname);
38377c478bd9Sstevel@tonic-gate 	arp_fd = dlpi_if_open(diap->ifname, &dia, _B_FALSE);
38387c478bd9Sstevel@tonic-gate 
38397c478bd9Sstevel@tonic-gate 	if (dia.style == DL_STYLE2 && dlpi_detach(arp_fd, -1) < 0)
38407c478bd9Sstevel@tonic-gate 		Perror0_exit("dlpi_detach");
38417c478bd9Sstevel@tonic-gate 
38427c478bd9Sstevel@tonic-gate 	if (ioctl(arp_fd, I_PUSH, ARP_MOD_NAME) == -1)
38437c478bd9Sstevel@tonic-gate 		Perror2_exit("I_PUSH", ARP_MOD_NAME);
38447c478bd9Sstevel@tonic-gate 
38457c478bd9Sstevel@tonic-gate 	/*
38467c478bd9Sstevel@tonic-gate 	 * Tell ARP the name and unit number for this interface.
38477c478bd9Sstevel@tonic-gate 	 * Note that arp has no support for transparent ioctls.
38487c478bd9Sstevel@tonic-gate 	 */
38497c478bd9Sstevel@tonic-gate 	if (strioctl(arp_fd, SIOCSLIFNAME, (char *)&lifr,
38507c478bd9Sstevel@tonic-gate 	    sizeof (lifr)) == -1) {
38517c478bd9Sstevel@tonic-gate 		if (errno != EEXIST)
38527c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCSLIFNAME for arp");
38537c478bd9Sstevel@tonic-gate 		Perror0("SIOCSLIFNAME for arp");
38547c478bd9Sstevel@tonic-gate 		(void) close(arp_fd);
38557c478bd9Sstevel@tonic-gate 		(void) close(mux_fd);
38567c478bd9Sstevel@tonic-gate 		return;
38577c478bd9Sstevel@tonic-gate 	}
38587c478bd9Sstevel@tonic-gate 	/*
38597c478bd9Sstevel@tonic-gate 	 * PLINK the IP and ARP streams so that ifconfig can exit
38607c478bd9Sstevel@tonic-gate 	 * without tearing down the stream.
38617c478bd9Sstevel@tonic-gate 	 */
38627c478bd9Sstevel@tonic-gate 	if ((ip_muxid = ioctl(mux_fd, I_PLINK, ip_fd)) == -1) {
38637c478bd9Sstevel@tonic-gate 		Perror0_exit("I_PLINK for ip");
38647c478bd9Sstevel@tonic-gate 	}
38657c478bd9Sstevel@tonic-gate 	if ((arp_muxid = ioctl(mux_fd, I_PLINK, arp_fd)) == -1) {
38667c478bd9Sstevel@tonic-gate 		(void) ioctl(mux_fd, I_PUNLINK, ip_muxid);
38677c478bd9Sstevel@tonic-gate 		Perror0_exit("I_PLINK for arp");
38687c478bd9Sstevel@tonic-gate 	}
38697c478bd9Sstevel@tonic-gate 
38707c478bd9Sstevel@tonic-gate 	if (debug)
38717c478bd9Sstevel@tonic-gate 		(void) printf("arp muxid = %d\n", arp_muxid);
38727c478bd9Sstevel@tonic-gate 	(void) close(arp_fd);
38737c478bd9Sstevel@tonic-gate 	(void) close(mux_fd);
38747c478bd9Sstevel@tonic-gate }
38757c478bd9Sstevel@tonic-gate 
38767c478bd9Sstevel@tonic-gate 
38777c478bd9Sstevel@tonic-gate /*
38787c478bd9Sstevel@tonic-gate  * If this is a physical interface then remove it.
38797c478bd9Sstevel@tonic-gate  * If it is a logical interface name use SIOCLIFREMOVEIF to
38807c478bd9Sstevel@tonic-gate  * remove it. In both cases fail if it doesn't exist.
38817c478bd9Sstevel@tonic-gate  */
38827c478bd9Sstevel@tonic-gate /* ARGSUSED */
38837c478bd9Sstevel@tonic-gate static int
38847c478bd9Sstevel@tonic-gate inetunplumb(char *arg, int64_t param)
38857c478bd9Sstevel@tonic-gate {
38867c478bd9Sstevel@tonic-gate 	int ip_muxid, arp_muxid;
38877c478bd9Sstevel@tonic-gate 	int mux_fd;
38887c478bd9Sstevel@tonic-gate 	char *udp_dev_name;
38897c478bd9Sstevel@tonic-gate 	char *strptr;
38907c478bd9Sstevel@tonic-gate 	uint64_t flags;
38917c478bd9Sstevel@tonic-gate 	boolean_t changed_arp_muxid = _B_FALSE;
38927c478bd9Sstevel@tonic-gate 	int save_errno;
38937c478bd9Sstevel@tonic-gate 
38947c478bd9Sstevel@tonic-gate 	strptr = strchr(name, ':');
38957c478bd9Sstevel@tonic-gate 	if (strptr != NULL || strcmp(name, LOOPBACK_IF) == 0) {
38967c478bd9Sstevel@tonic-gate 		/* Can't unplumb logical interface zero */
38977c478bd9Sstevel@tonic-gate 		if (strptr != NULL && strcmp(strptr, ":0") == 0) {
38987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: unplumb:"
38997c478bd9Sstevel@tonic-gate 			    " Cannot unplumb %s: Invalid interface\n", name);
39007c478bd9Sstevel@tonic-gate 			exit(1);
39017c478bd9Sstevel@tonic-gate 		}
39027c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
39037c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
39047c478bd9Sstevel@tonic-gate 
39057c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0)
39067c478bd9Sstevel@tonic-gate 			Perror0_exit("unplumb: SIOCLIFREMOVEIF");
39077c478bd9Sstevel@tonic-gate 		return (0);
39087c478bd9Sstevel@tonic-gate 	}
39097c478bd9Sstevel@tonic-gate 
39107c478bd9Sstevel@tonic-gate 	/*
39117c478bd9Sstevel@tonic-gate 	 * We used /dev/udp or udp6 to set up the mux. So we have to use
39127c478bd9Sstevel@tonic-gate 	 * the same now for PUNLINK also.
39137c478bd9Sstevel@tonic-gate 	 */
39147c478bd9Sstevel@tonic-gate 	if (afp->af_af == AF_INET6)
39157c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP6_DEV_NAME;
39167c478bd9Sstevel@tonic-gate 	else
39177c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP_DEV_NAME;
39187c478bd9Sstevel@tonic-gate 
39197c478bd9Sstevel@tonic-gate 	if ((mux_fd = open_arp_on_udp(udp_dev_name)) == -1)
39207c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
39217c478bd9Sstevel@tonic-gate 
39227c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
39237c478bd9Sstevel@tonic-gate 	if (ioctl(mux_fd, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
39247c478bd9Sstevel@tonic-gate 		Perror0_exit("unplumb: SIOCGLIFFLAGS");
39257c478bd9Sstevel@tonic-gate 	}
39267c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
39277c478bd9Sstevel@tonic-gate 	if (ioctl(mux_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
39287c478bd9Sstevel@tonic-gate 		Perror0_exit("unplumb: SIOCGLIFMUXID");
39297c478bd9Sstevel@tonic-gate 	}
39307c478bd9Sstevel@tonic-gate 	arp_muxid = lifr.lifr_arp_muxid;
39317c478bd9Sstevel@tonic-gate 	ip_muxid = lifr.lifr_ip_muxid;
39327c478bd9Sstevel@tonic-gate 	/*
39337c478bd9Sstevel@tonic-gate 	 * We don't have a good way of knowing whether the arp stream is
39347c478bd9Sstevel@tonic-gate 	 * plumbed. We can't rely on IFF_NOARP because someone could
39357c478bd9Sstevel@tonic-gate 	 * have turned it off later using "ifconfig xxx -arp".
39367c478bd9Sstevel@tonic-gate 	 */
39377c478bd9Sstevel@tonic-gate 	if (arp_muxid != 0) {
39387c478bd9Sstevel@tonic-gate 		if (debug)
39397c478bd9Sstevel@tonic-gate 			(void) printf("arp_muxid %d\n", arp_muxid);
39407c478bd9Sstevel@tonic-gate 		if (ioctl(mux_fd, I_PUNLINK, arp_muxid) < 0) {
39417c478bd9Sstevel@tonic-gate 			if ((errno == EINVAL) &&
39427c478bd9Sstevel@tonic-gate 			    (flags & (IFF_NOARP | IFF_IPV6))) {
39437c478bd9Sstevel@tonic-gate 				/*
39447c478bd9Sstevel@tonic-gate 				 * Some plumbing utilities set the muxid to
39457c478bd9Sstevel@tonic-gate 				 * -1 or some invalid value to signify that
39467c478bd9Sstevel@tonic-gate 				 * there is no arp stream. Set the muxid to 0
39477c478bd9Sstevel@tonic-gate 				 * before trying to unplumb the IP stream.
39487c478bd9Sstevel@tonic-gate 				 * IP does not allow the IP stream to be
39497c478bd9Sstevel@tonic-gate 				 * unplumbed if it sees a non-null arp muxid,
39507c478bd9Sstevel@tonic-gate 				 * for consistency of IP-ARP streams.
39517c478bd9Sstevel@tonic-gate 				 */
39527c478bd9Sstevel@tonic-gate 				lifr.lifr_arp_muxid = 0;
39537c478bd9Sstevel@tonic-gate 				(void) ioctl(mux_fd, SIOCSLIFMUXID,
39547c478bd9Sstevel@tonic-gate 				    (caddr_t)&lifr);
39557c478bd9Sstevel@tonic-gate 				changed_arp_muxid = _B_TRUE;
39567c478bd9Sstevel@tonic-gate 			} else {
39577c478bd9Sstevel@tonic-gate 				Perror0("I_PUNLINK for arp");
39587c478bd9Sstevel@tonic-gate 			}
39597c478bd9Sstevel@tonic-gate 		}
39607c478bd9Sstevel@tonic-gate 	}
39617c478bd9Sstevel@tonic-gate 	if (debug)
39627c478bd9Sstevel@tonic-gate 		(void) printf("ip_muxid %d\n", ip_muxid);
39637c478bd9Sstevel@tonic-gate 
39647c478bd9Sstevel@tonic-gate 	if (ioctl(mux_fd, I_PUNLINK, ip_muxid) < 0) {
39657c478bd9Sstevel@tonic-gate 		if (changed_arp_muxid) {
39667c478bd9Sstevel@tonic-gate 			/*
39677c478bd9Sstevel@tonic-gate 			 * Some error occurred, and we need to restore
39687c478bd9Sstevel@tonic-gate 			 * everything back to what it was.
39697c478bd9Sstevel@tonic-gate 			 */
39707c478bd9Sstevel@tonic-gate 			save_errno = errno;
39717c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = arp_muxid;
39727c478bd9Sstevel@tonic-gate 			lifr.lifr_ip_muxid = ip_muxid;
39737c478bd9Sstevel@tonic-gate 			(void) ioctl(mux_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
39747c478bd9Sstevel@tonic-gate 			errno = save_errno;
39757c478bd9Sstevel@tonic-gate 		}
39767c478bd9Sstevel@tonic-gate 		Perror0_exit("I_PUNLINK for ip");
39777c478bd9Sstevel@tonic-gate 	}
39787c478bd9Sstevel@tonic-gate 	(void) close(mux_fd);
39797c478bd9Sstevel@tonic-gate 	return (0);
39807c478bd9Sstevel@tonic-gate }
39817c478bd9Sstevel@tonic-gate 
39827c478bd9Sstevel@tonic-gate /*
39837c478bd9Sstevel@tonic-gate  * If this is a physical interface then create it unless it is already
39847c478bd9Sstevel@tonic-gate  * present. If it is a logical interface name use SIOCLIFADDIF to
39857c478bd9Sstevel@tonic-gate  * create and (and fail it if already exists.)
39867c478bd9Sstevel@tonic-gate  * As a special case send SIOCLIFADDIF for the loopback interface. This
39877c478bd9Sstevel@tonic-gate  * is needed since there is no other notion of plumbing the loopback
39887c478bd9Sstevel@tonic-gate  * interface.
39897c478bd9Sstevel@tonic-gate  */
39907c478bd9Sstevel@tonic-gate /* ARGSUSED */
39917c478bd9Sstevel@tonic-gate static int
39927c478bd9Sstevel@tonic-gate inetplumb(char *arg, int64_t param)
39937c478bd9Sstevel@tonic-gate {
39947c478bd9Sstevel@tonic-gate 	char		*strptr;
39957c478bd9Sstevel@tonic-gate 	int		dev_fd;
39967c478bd9Sstevel@tonic-gate 	dlpi_if_attr_t	dia;
39977c478bd9Sstevel@tonic-gate 	boolean_t	islo;
39987c478bd9Sstevel@tonic-gate 
39997c478bd9Sstevel@tonic-gate 	strptr = strchr(name, ':');
40007c478bd9Sstevel@tonic-gate 	islo = (strcmp(name, LOOPBACK_IF) == 0);
40017c478bd9Sstevel@tonic-gate 
40027c478bd9Sstevel@tonic-gate 	if (strptr != NULL || islo) {
40037c478bd9Sstevel@tonic-gate 		(void) memset(&lifr, 0, sizeof (lifr));
40047c478bd9Sstevel@tonic-gate 		(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
40057c478bd9Sstevel@tonic-gate 		if (islo && ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0) {
40067c478bd9Sstevel@tonic-gate 			if (debug) {
40077c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
40087c478bd9Sstevel@tonic-gate 				    "ifconfig: %s already exists\n", name);
40097c478bd9Sstevel@tonic-gate 			}
40107c478bd9Sstevel@tonic-gate 			return (0);
40117c478bd9Sstevel@tonic-gate 		}
40127c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
40137c478bd9Sstevel@tonic-gate 			if (errno == EEXIST) {
40147c478bd9Sstevel@tonic-gate 				if (debug) {
40157c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
40167c478bd9Sstevel@tonic-gate 					    "ifconfig: %s already exists\n",
40177c478bd9Sstevel@tonic-gate 					    name);
40187c478bd9Sstevel@tonic-gate 				}
40197c478bd9Sstevel@tonic-gate 			} else {
40207c478bd9Sstevel@tonic-gate 				Perror2_exit("plumb: SIOCLIFADDIF", name);
40217c478bd9Sstevel@tonic-gate 			}
40227c478bd9Sstevel@tonic-gate 		}
40237c478bd9Sstevel@tonic-gate 		/*
40247c478bd9Sstevel@tonic-gate 		 * IP can create the new logical interface on a different
40257c478bd9Sstevel@tonic-gate 		 * physical interface in the same IPMP group. Take the new
40267c478bd9Sstevel@tonic-gate 		 * interface into account for further operations.
40277c478bd9Sstevel@tonic-gate 		 */
40287c478bd9Sstevel@tonic-gate 		(void) strncpy(name, lifr.lifr_name, sizeof (name));
40297c478bd9Sstevel@tonic-gate 		return (0);
40307c478bd9Sstevel@tonic-gate 	}
40317c478bd9Sstevel@tonic-gate 
40327c478bd9Sstevel@tonic-gate 	if (debug)
40337c478bd9Sstevel@tonic-gate 		(void) printf("inetplumb: %s af %d\n", name, afp->af_af);
40347c478bd9Sstevel@tonic-gate 
40357c478bd9Sstevel@tonic-gate 	if ((dev_fd = dlpi_if_open(name, &dia, _B_FALSE)) < 0) {
40367c478bd9Sstevel@tonic-gate 		Perror2_exit("plumb", name);
40377c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
40387c478bd9Sstevel@tonic-gate 	}
40397c478bd9Sstevel@tonic-gate 
40407c478bd9Sstevel@tonic-gate 	plumb_one_device(&dia, dev_fd, afp->af_af);
40417c478bd9Sstevel@tonic-gate 	(void) close(dev_fd);
40427c478bd9Sstevel@tonic-gate 	return (0);
40437c478bd9Sstevel@tonic-gate }
40447c478bd9Sstevel@tonic-gate 
40457c478bd9Sstevel@tonic-gate void
40467c478bd9Sstevel@tonic-gate Perror0(char *cmd)
40477c478bd9Sstevel@tonic-gate {
40487c478bd9Sstevel@tonic-gate 	int save_errno;
40497c478bd9Sstevel@tonic-gate 
40507c478bd9Sstevel@tonic-gate 	save_errno = errno;
40517c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "ifconfig: ");
40527c478bd9Sstevel@tonic-gate 	errno = save_errno;
40537c478bd9Sstevel@tonic-gate 	switch (errno) {
40547c478bd9Sstevel@tonic-gate 
40557c478bd9Sstevel@tonic-gate 	case ENXIO:
40567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: no such interface\n",
40577c478bd9Sstevel@tonic-gate 			cmd, lifr.lifr_name);
40587c478bd9Sstevel@tonic-gate 		break;
40597c478bd9Sstevel@tonic-gate 
40607c478bd9Sstevel@tonic-gate 	case EPERM:
40617c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: permission denied\n",
40627c478bd9Sstevel@tonic-gate 			cmd, lifr.lifr_name);
40637c478bd9Sstevel@tonic-gate 		break;
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate 	case EEXIST:
40667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: already exists\n",
40677c478bd9Sstevel@tonic-gate 			cmd, lifr.lifr_name);
40687c478bd9Sstevel@tonic-gate 		break;
40697c478bd9Sstevel@tonic-gate 
40707c478bd9Sstevel@tonic-gate 	default: {
40717c478bd9Sstevel@tonic-gate 		char buf[BUFSIZ];
40727c478bd9Sstevel@tonic-gate 
40737c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s: %s",
40747c478bd9Sstevel@tonic-gate 		    cmd, lifr.lifr_name);
40757c478bd9Sstevel@tonic-gate 		perror(buf);
40767c478bd9Sstevel@tonic-gate 	}
40777c478bd9Sstevel@tonic-gate 	}
40787c478bd9Sstevel@tonic-gate }
40797c478bd9Sstevel@tonic-gate 
40807c478bd9Sstevel@tonic-gate void
40817c478bd9Sstevel@tonic-gate Perror0_exit(char *cmd)
40827c478bd9Sstevel@tonic-gate {
40837c478bd9Sstevel@tonic-gate 	Perror0(cmd);
40847c478bd9Sstevel@tonic-gate 	exit(1);
40857c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
40867c478bd9Sstevel@tonic-gate }
40877c478bd9Sstevel@tonic-gate 
40887c478bd9Sstevel@tonic-gate void
40897c478bd9Sstevel@tonic-gate Perror2(char *cmd, char *str)
40907c478bd9Sstevel@tonic-gate {
40917c478bd9Sstevel@tonic-gate 	int save_errno;
40927c478bd9Sstevel@tonic-gate 
40937c478bd9Sstevel@tonic-gate 	save_errno = errno;
40947c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "ifconfig: ");
40957c478bd9Sstevel@tonic-gate 	errno = save_errno;
40967c478bd9Sstevel@tonic-gate 	switch (errno) {
40977c478bd9Sstevel@tonic-gate 
40987c478bd9Sstevel@tonic-gate 	case ENXIO:
40997c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: no such interface\n",
41007c478bd9Sstevel@tonic-gate 			cmd, str);
41017c478bd9Sstevel@tonic-gate 		break;
41027c478bd9Sstevel@tonic-gate 
41037c478bd9Sstevel@tonic-gate 	case EPERM:
41047c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: permission denied\n",
41057c478bd9Sstevel@tonic-gate 			cmd, str);
41067c478bd9Sstevel@tonic-gate 		break;
41077c478bd9Sstevel@tonic-gate 
41087c478bd9Sstevel@tonic-gate 	default: {
41097c478bd9Sstevel@tonic-gate 		char buf[BUFSIZ];
41107c478bd9Sstevel@tonic-gate 
41117c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s: %s", cmd, str);
41127c478bd9Sstevel@tonic-gate 		perror(buf);
41137c478bd9Sstevel@tonic-gate 	}
41147c478bd9Sstevel@tonic-gate 	}
41157c478bd9Sstevel@tonic-gate }
41167c478bd9Sstevel@tonic-gate 
41177c478bd9Sstevel@tonic-gate /*
41187c478bd9Sstevel@tonic-gate  * Print out error message (Perror2()) and exit
41197c478bd9Sstevel@tonic-gate  */
41207c478bd9Sstevel@tonic-gate void
41217c478bd9Sstevel@tonic-gate Perror2_exit(char *cmd, char *str)
41227c478bd9Sstevel@tonic-gate {
41237c478bd9Sstevel@tonic-gate 	Perror2(cmd, str);
41247c478bd9Sstevel@tonic-gate 	exit(1);
41257c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
41267c478bd9Sstevel@tonic-gate }
41277c478bd9Sstevel@tonic-gate 
41287c478bd9Sstevel@tonic-gate /*
41297c478bd9Sstevel@tonic-gate  * If the last argument is non-NULL allow a <addr>/<n> syntax and
41307c478bd9Sstevel@tonic-gate  * pass out <n> in *plenp.
41317c478bd9Sstevel@tonic-gate  * If <n> doesn't parse return BAD_ADDR as *plenp.
41327c478bd9Sstevel@tonic-gate  * If no /<n> is present return NO_PREFIX as *plenp.
41337c478bd9Sstevel@tonic-gate  */
41347c478bd9Sstevel@tonic-gate static void
41357c478bd9Sstevel@tonic-gate in_getaddr(char *s, struct sockaddr *saddr, int *plenp)
41367c478bd9Sstevel@tonic-gate {
41377c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
41387c478bd9Sstevel@tonic-gate 	struct hostent *hp;
41397c478bd9Sstevel@tonic-gate 	struct netent *np;
41407c478bd9Sstevel@tonic-gate 	char str[BUFSIZ];
41417c478bd9Sstevel@tonic-gate 	int error_num;
41427c478bd9Sstevel@tonic-gate 
41437c478bd9Sstevel@tonic-gate 	(void) strncpy(str, s, sizeof (str));
41447c478bd9Sstevel@tonic-gate 
41457c478bd9Sstevel@tonic-gate 	/*
41467c478bd9Sstevel@tonic-gate 	 * Look for '/'<n> is plenp
41477c478bd9Sstevel@tonic-gate 	 */
41487c478bd9Sstevel@tonic-gate 	if (plenp != NULL) {
41497c478bd9Sstevel@tonic-gate 		char *cp;
41507c478bd9Sstevel@tonic-gate 
4151*7906a3e0Smeem 		*plenp = in_getprefixlen(str, _B_TRUE, IP_ABITS);
41527c478bd9Sstevel@tonic-gate 		if (*plenp == BAD_ADDR)
41537c478bd9Sstevel@tonic-gate 			return;
41547c478bd9Sstevel@tonic-gate 		cp = strchr(str, '/');
41557c478bd9Sstevel@tonic-gate 		if (cp != NULL)
41567c478bd9Sstevel@tonic-gate 			*cp = '\0';
41577c478bd9Sstevel@tonic-gate 	} else if (strchr(str, '/') != NULL) {
41587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
41597c478bd9Sstevel@tonic-gate 		exit(1);
41607c478bd9Sstevel@tonic-gate 	}
41617c478bd9Sstevel@tonic-gate 
41627c478bd9Sstevel@tonic-gate 	(void) memset(sin, 0, sizeof (*sin));
41637c478bd9Sstevel@tonic-gate 
41647c478bd9Sstevel@tonic-gate 	/*
41657c478bd9Sstevel@tonic-gate 	 *	Try to catch attempts to set the broadcast address to all 1's.
41667c478bd9Sstevel@tonic-gate 	 */
41677c478bd9Sstevel@tonic-gate 	if (strcmp(str, "255.255.255.255") == 0 ||
41687c478bd9Sstevel@tonic-gate 	    (strtoul(str, (char **)NULL, 0) == 0xffffffffUL)) {
41697c478bd9Sstevel@tonic-gate 		sin->sin_family = AF_INET;
41707c478bd9Sstevel@tonic-gate 		sin->sin_addr.s_addr = 0xffffffff;
41717c478bd9Sstevel@tonic-gate 		return;
41727c478bd9Sstevel@tonic-gate 	}
41737c478bd9Sstevel@tonic-gate 
41747c478bd9Sstevel@tonic-gate 	hp = getipnodebyname(str, AF_INET, 0, &error_num);
41757c478bd9Sstevel@tonic-gate 	if (hp) {
41767c478bd9Sstevel@tonic-gate 		sin->sin_family = hp->h_addrtype;
41777c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
41787c478bd9Sstevel@tonic-gate 		freehostent(hp);
41797c478bd9Sstevel@tonic-gate 		return;
41807c478bd9Sstevel@tonic-gate 	}
41817c478bd9Sstevel@tonic-gate 	np = getnetbyname(str);
41827c478bd9Sstevel@tonic-gate 	if (np) {
41837c478bd9Sstevel@tonic-gate 		sin->sin_family = np->n_addrtype;
41847c478bd9Sstevel@tonic-gate 		sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
41857c478bd9Sstevel@tonic-gate 		return;
41867c478bd9Sstevel@tonic-gate 	}
41877c478bd9Sstevel@tonic-gate 	if (error_num == TRY_AGAIN) {
41887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address "
41897c478bd9Sstevel@tonic-gate 		    "(try again later)\n", s);
41907c478bd9Sstevel@tonic-gate 	} else {
41917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
41927c478bd9Sstevel@tonic-gate 	}
41937c478bd9Sstevel@tonic-gate 	exit(1);
41947c478bd9Sstevel@tonic-gate }
41957c478bd9Sstevel@tonic-gate 
41967c478bd9Sstevel@tonic-gate /*
41977c478bd9Sstevel@tonic-gate  * If the last argument is non-NULL allow a <addr>/<n> syntax and
41987c478bd9Sstevel@tonic-gate  * pass out <n> in *plenp.
41997c478bd9Sstevel@tonic-gate  * If <n> doesn't parse return BAD_ADDR as *plenp.
42007c478bd9Sstevel@tonic-gate  * If no /<n> is present return NO_PREFIX as *plenp.
42017c478bd9Sstevel@tonic-gate  */
42027c478bd9Sstevel@tonic-gate static void
42037c478bd9Sstevel@tonic-gate in6_getaddr(char *s, struct sockaddr *saddr, int *plenp)
42047c478bd9Sstevel@tonic-gate {
42057c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)saddr;
42067c478bd9Sstevel@tonic-gate 	struct hostent *hp;
42077c478bd9Sstevel@tonic-gate 	char str[BUFSIZ];
42087c478bd9Sstevel@tonic-gate 	int error_num;
42097c478bd9Sstevel@tonic-gate 
42107c478bd9Sstevel@tonic-gate 	(void) strncpy(str, s, sizeof (str));
42117c478bd9Sstevel@tonic-gate 
42127c478bd9Sstevel@tonic-gate 	/*
42137c478bd9Sstevel@tonic-gate 	 * Look for '/'<n> is plenp
42147c478bd9Sstevel@tonic-gate 	 */
42157c478bd9Sstevel@tonic-gate 	if (plenp != NULL) {
42167c478bd9Sstevel@tonic-gate 		char *cp;
42177c478bd9Sstevel@tonic-gate 
4218*7906a3e0Smeem 		*plenp = in_getprefixlen(str, _B_TRUE, IPV6_ABITS);
42197c478bd9Sstevel@tonic-gate 		if (*plenp == BAD_ADDR)
42207c478bd9Sstevel@tonic-gate 			return;
42217c478bd9Sstevel@tonic-gate 		cp = strchr(str, '/');
42227c478bd9Sstevel@tonic-gate 		if (cp != NULL)
42237c478bd9Sstevel@tonic-gate 			*cp = '\0';
42247c478bd9Sstevel@tonic-gate 	} else if (strchr(str, '/') != NULL) {
42257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
42267c478bd9Sstevel@tonic-gate 		exit(1);
42277c478bd9Sstevel@tonic-gate 	}
42287c478bd9Sstevel@tonic-gate 
42297c478bd9Sstevel@tonic-gate 	(void) memset(sin6, 0, sizeof (*sin6));
42307c478bd9Sstevel@tonic-gate 
42317c478bd9Sstevel@tonic-gate 	hp = getipnodebyname(str, AF_INET6, 0, &error_num);
42327c478bd9Sstevel@tonic-gate 	if (hp) {
42337c478bd9Sstevel@tonic-gate 		sin6->sin6_family = hp->h_addrtype;
42347c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin6->sin6_addr, hp->h_addr, hp->h_length);
42357c478bd9Sstevel@tonic-gate 		freehostent(hp);
42367c478bd9Sstevel@tonic-gate 		return;
42377c478bd9Sstevel@tonic-gate 	}
42387c478bd9Sstevel@tonic-gate 	if (error_num == TRY_AGAIN) {
42397c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address "
42407c478bd9Sstevel@tonic-gate 		    "(try again later)\n", s);
42417c478bd9Sstevel@tonic-gate 	} else {
42427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
42437c478bd9Sstevel@tonic-gate 	}
42447c478bd9Sstevel@tonic-gate 	exit(1);
42457c478bd9Sstevel@tonic-gate }
42467c478bd9Sstevel@tonic-gate 
42477c478bd9Sstevel@tonic-gate /*
42487c478bd9Sstevel@tonic-gate  * If "slash" is zero this parses the whole string as
42497c478bd9Sstevel@tonic-gate  * an integer. With "slash" non zero it parses the tail part as an integer.
42507c478bd9Sstevel@tonic-gate  *
42517c478bd9Sstevel@tonic-gate  * If it is not a valid integer this returns BAD_ADDR.
42527c478bd9Sstevel@tonic-gate  * If there is /<n> present this returns NO_PREFIX.
42537c478bd9Sstevel@tonic-gate  */
42547c478bd9Sstevel@tonic-gate static int
42557c478bd9Sstevel@tonic-gate in_getprefixlen(char *addr, boolean_t slash, int max_plen)
42567c478bd9Sstevel@tonic-gate {
42577c478bd9Sstevel@tonic-gate 	int prefixlen;
42587c478bd9Sstevel@tonic-gate 	char *str, *end;
42597c478bd9Sstevel@tonic-gate 
42607c478bd9Sstevel@tonic-gate 	if (slash) {
42617c478bd9Sstevel@tonic-gate 		str = strchr(addr, '/');
42627c478bd9Sstevel@tonic-gate 		if (str == NULL)
42637c478bd9Sstevel@tonic-gate 			return (NO_PREFIX);
42647c478bd9Sstevel@tonic-gate 		str++;
42657c478bd9Sstevel@tonic-gate 	} else
42667c478bd9Sstevel@tonic-gate 		str = addr;
42677c478bd9Sstevel@tonic-gate 
42687c478bd9Sstevel@tonic-gate 	prefixlen = strtol(str, &end, 10);
42697c478bd9Sstevel@tonic-gate 	if (prefixlen < 0)
42707c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
42717c478bd9Sstevel@tonic-gate 	if (str == end)
42727c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
42737c478bd9Sstevel@tonic-gate 	if (max_plen != 0 && max_plen < prefixlen)
42747c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
42757c478bd9Sstevel@tonic-gate 	return (prefixlen);
42767c478bd9Sstevel@tonic-gate }
42777c478bd9Sstevel@tonic-gate 
42787c478bd9Sstevel@tonic-gate /*
42797c478bd9Sstevel@tonic-gate  * Convert a prefix length to a mask.
42807c478bd9Sstevel@tonic-gate  * Returns 1 if ok. 0 otherwise.
42817c478bd9Sstevel@tonic-gate  * Assumes the mask array is zero'ed by the caller.
42827c478bd9Sstevel@tonic-gate  */
42837c478bd9Sstevel@tonic-gate static boolean_t
42847c478bd9Sstevel@tonic-gate in_prefixlentomask(int prefixlen, int maxlen, uchar_t *mask)
42857c478bd9Sstevel@tonic-gate {
42867c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxlen)
42877c478bd9Sstevel@tonic-gate 		return (0);
42887c478bd9Sstevel@tonic-gate 
42897c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
42907c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
42917c478bd9Sstevel@tonic-gate 			*mask++ = 0xFF;
42927c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
42937c478bd9Sstevel@tonic-gate 			continue;
42947c478bd9Sstevel@tonic-gate 		}
42957c478bd9Sstevel@tonic-gate 		*mask |= 1 << (8 - prefixlen);
42967c478bd9Sstevel@tonic-gate 		prefixlen--;
42977c478bd9Sstevel@tonic-gate 	}
42987c478bd9Sstevel@tonic-gate 	return (1);
42997c478bd9Sstevel@tonic-gate }
43007c478bd9Sstevel@tonic-gate 
43017c478bd9Sstevel@tonic-gate static void
43027c478bd9Sstevel@tonic-gate print_flags(uint64_t flags)
43037c478bd9Sstevel@tonic-gate {
43047c478bd9Sstevel@tonic-gate 	boolean_t first = _B_TRUE;
43057c478bd9Sstevel@tonic-gate 	int cnt, i;
43067c478bd9Sstevel@tonic-gate 
43077c478bd9Sstevel@tonic-gate 	(void) printf("flags=%llx", flags);
43087c478bd9Sstevel@tonic-gate 	cnt = sizeof (if_flags_tbl) / sizeof (if_flags_t);
43097c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
43107c478bd9Sstevel@tonic-gate 		if (flags & if_flags_tbl[i].iff_value) {
43117c478bd9Sstevel@tonic-gate 			if (first) {
43127c478bd9Sstevel@tonic-gate 				(void) printf("<");
43137c478bd9Sstevel@tonic-gate 				first = _B_FALSE;
43147c478bd9Sstevel@tonic-gate 			} else {
43157c478bd9Sstevel@tonic-gate 				/*
43167c478bd9Sstevel@tonic-gate 				 * It has to be here and not with the
43177c478bd9Sstevel@tonic-gate 				 * printf below because for the last one,
43187c478bd9Sstevel@tonic-gate 				 * we don't want a comma before the ">".
43197c478bd9Sstevel@tonic-gate 				 */
43207c478bd9Sstevel@tonic-gate 				(void) printf(",");
43217c478bd9Sstevel@tonic-gate 			}
43227c478bd9Sstevel@tonic-gate 			(void) printf("%s", if_flags_tbl[i].iff_name);
43237c478bd9Sstevel@tonic-gate 		}
43247c478bd9Sstevel@tonic-gate 	}
43257c478bd9Sstevel@tonic-gate 	if (!first)
43267c478bd9Sstevel@tonic-gate 		(void) printf(">");
43277c478bd9Sstevel@tonic-gate }
43287c478bd9Sstevel@tonic-gate 
43297c478bd9Sstevel@tonic-gate static void
43307c478bd9Sstevel@tonic-gate print_config_flags(uint64_t flags)
43317c478bd9Sstevel@tonic-gate {
43327c478bd9Sstevel@tonic-gate 	int cnt, i;
43337c478bd9Sstevel@tonic-gate 
43347c478bd9Sstevel@tonic-gate 	cnt = sizeof (if_config_cmd_tbl) / sizeof (if_config_cmd_t);
43357c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
43367c478bd9Sstevel@tonic-gate 		if (flags & if_config_cmd_tbl[i].iff_flag) {
43377c478bd9Sstevel@tonic-gate 			(void) printf("%s ", if_config_cmd_tbl[i].iff_name);
43387c478bd9Sstevel@tonic-gate 		}
43397c478bd9Sstevel@tonic-gate 	}
43407c478bd9Sstevel@tonic-gate }
43417c478bd9Sstevel@tonic-gate 
43427c478bd9Sstevel@tonic-gate /*
4343dd7a6f5fSkcpoon  * Use the configured directory lookup mechanism (e.g. files/NIS/NIS+/...)
4344dd7a6f5fSkcpoon  * to find the network mask.  Returns true if we found one to set.
4345dd7a6f5fSkcpoon  *
4346dd7a6f5fSkcpoon  * The parameter addr_set controls whether we should get the address of
4347dd7a6f5fSkcpoon  * the working interface for the netmask query.  If addr_set is true,
4348dd7a6f5fSkcpoon  * we will use the address provided.  Otherwise, we will find the working
4349dd7a6f5fSkcpoon  * interface's address and use it instead.
43507c478bd9Sstevel@tonic-gate  */
43517c478bd9Sstevel@tonic-gate static boolean_t
4352dd7a6f5fSkcpoon in_getmask(struct sockaddr_in *saddr, boolean_t addr_set)
43537c478bd9Sstevel@tonic-gate {
43547c478bd9Sstevel@tonic-gate 	struct sockaddr_in ifaddr;
43557c478bd9Sstevel@tonic-gate 
43567c478bd9Sstevel@tonic-gate 	/*
4357dd7a6f5fSkcpoon 	 * Read the address from the interface if it is not passed in.
43587c478bd9Sstevel@tonic-gate 	 */
4359dd7a6f5fSkcpoon 	if (!addr_set) {
4360dd7a6f5fSkcpoon 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
4361dd7a6f5fSkcpoon 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
4362dd7a6f5fSkcpoon 			if (errno != EADDRNOTAVAIL) {
4363dd7a6f5fSkcpoon 				(void) fprintf(stderr, "Need net number for "
4364dd7a6f5fSkcpoon 				    "mask\n");
4365dd7a6f5fSkcpoon 			}
4366dd7a6f5fSkcpoon 			return (_B_FALSE);
4367dd7a6f5fSkcpoon 		}
4368dd7a6f5fSkcpoon 		ifaddr = *((struct sockaddr_in *)&lifr.lifr_addr);
4369dd7a6f5fSkcpoon 	} else {
4370dd7a6f5fSkcpoon 		ifaddr.sin_addr = saddr->sin_addr;
43717c478bd9Sstevel@tonic-gate 	}
43727c478bd9Sstevel@tonic-gate 	if (getnetmaskbyaddr(ifaddr.sin_addr, &saddr->sin_addr) == 0) {
43737c478bd9Sstevel@tonic-gate 		saddr->sin_family = AF_INET;
43747c478bd9Sstevel@tonic-gate 		return (_B_TRUE);
43757c478bd9Sstevel@tonic-gate 	}
43767c478bd9Sstevel@tonic-gate 	return (_B_FALSE);
43777c478bd9Sstevel@tonic-gate }
43787c478bd9Sstevel@tonic-gate 
43797c478bd9Sstevel@tonic-gate static int
43807c478bd9Sstevel@tonic-gate strioctl(int s, int cmd, char *buf, int buflen)
43817c478bd9Sstevel@tonic-gate {
43827c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
43837c478bd9Sstevel@tonic-gate 
43847c478bd9Sstevel@tonic-gate 	(void) memset(&ioc, 0, sizeof (ioc));
43857c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = cmd;
43867c478bd9Sstevel@tonic-gate 	ioc.ic_timout = 0;
43877c478bd9Sstevel@tonic-gate 	ioc.ic_len = buflen;
43887c478bd9Sstevel@tonic-gate 	ioc.ic_dp = buf;
43897c478bd9Sstevel@tonic-gate 	return (ioctl(s, I_STR, (char *)&ioc));
43907c478bd9Sstevel@tonic-gate }
43917c478bd9Sstevel@tonic-gate 
43927c478bd9Sstevel@tonic-gate static void
43937c478bd9Sstevel@tonic-gate add_ni(char *name)
43947c478bd9Sstevel@tonic-gate {
43957c478bd9Sstevel@tonic-gate 	ni_t **pp;
43967c478bd9Sstevel@tonic-gate 	ni_t *p;
43977c478bd9Sstevel@tonic-gate 
43987c478bd9Sstevel@tonic-gate 	for (pp = &ni_list; (p = *pp) != NULL; pp = &(p->ni_next)) {
43997c478bd9Sstevel@tonic-gate 		if (strcmp(p->ni_name, name) == 0) {
44007c478bd9Sstevel@tonic-gate 			if (debug > 2)
44017c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "'%s' is a duplicate\n",
44027c478bd9Sstevel@tonic-gate 				    name);
44037c478bd9Sstevel@tonic-gate 			return;
44047c478bd9Sstevel@tonic-gate 		}
44057c478bd9Sstevel@tonic-gate 	}
44067c478bd9Sstevel@tonic-gate 
44077c478bd9Sstevel@tonic-gate 	if (debug > 2)
44087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "adding '%s'\n",
44097c478bd9Sstevel@tonic-gate 		    name);
44107c478bd9Sstevel@tonic-gate 
44117c478bd9Sstevel@tonic-gate 	if ((p = malloc(sizeof (ni_t))) == NULL)
44127c478bd9Sstevel@tonic-gate 		return;
44137c478bd9Sstevel@tonic-gate 
44147c478bd9Sstevel@tonic-gate 	(void) strlcpy(p->ni_name, name, sizeof (p->ni_name));
44157c478bd9Sstevel@tonic-gate 	p->ni_next = NULL;
44167c478bd9Sstevel@tonic-gate 
44177c478bd9Sstevel@tonic-gate 	*pp = p;
44187c478bd9Sstevel@tonic-gate 	num_ni++;
44197c478bd9Sstevel@tonic-gate }
44207c478bd9Sstevel@tonic-gate 
44217c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
44227c478bd9Sstevel@tonic-gate static int
44237c478bd9Sstevel@tonic-gate devfs_entry(di_node_t node, di_minor_t minor, void *arg)
44247c478bd9Sstevel@tonic-gate {
44257c478bd9Sstevel@tonic-gate 	char *provider;
44267c478bd9Sstevel@tonic-gate 	int fd;
44277c478bd9Sstevel@tonic-gate 	uint_t ppa;
44287c478bd9Sstevel@tonic-gate 	dl_info_ack_t dlia;
44297c478bd9Sstevel@tonic-gate 	char ifname[LIFNAMSIZ];
44307c478bd9Sstevel@tonic-gate 
44317c478bd9Sstevel@tonic-gate 	provider = di_minor_name(minor);
44327c478bd9Sstevel@tonic-gate 
44337c478bd9Sstevel@tonic-gate 	if (strlen(provider) > LIFNAMSIZ)
44347c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
44357c478bd9Sstevel@tonic-gate 
44367c478bd9Sstevel@tonic-gate 	if (debug > 2)
44377c478bd9Sstevel@tonic-gate 		fprintf(stderr, "provider = %s\n", provider);
44387c478bd9Sstevel@tonic-gate 
44397c478bd9Sstevel@tonic-gate 	if ((fd = dlpi_open(provider)) < 0)
44407c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
44417c478bd9Sstevel@tonic-gate 
44427c478bd9Sstevel@tonic-gate 	if (debug > 2)
44437c478bd9Sstevel@tonic-gate 		fprintf(stderr, "getting DL_INFO_ACK\n");
44447c478bd9Sstevel@tonic-gate 
44457c478bd9Sstevel@tonic-gate 	if (dlpi_info(fd, -1, &dlia, NULL, NULL, NULL, NULL, NULL, NULL) < 0)
44467c478bd9Sstevel@tonic-gate 		goto done;
44477c478bd9Sstevel@tonic-gate 
44487c478bd9Sstevel@tonic-gate 	if (dlia.dl_provider_style == DL_STYLE1) {
44497c478bd9Sstevel@tonic-gate 		if (debug > 2)
44507c478bd9Sstevel@tonic-gate 			fprintf(stderr, "provider is DL_STYLE1\n");
44517c478bd9Sstevel@tonic-gate 		add_ni(provider);
44527c478bd9Sstevel@tonic-gate 		goto done;
44537c478bd9Sstevel@tonic-gate 	}
44547c478bd9Sstevel@tonic-gate 
44557c478bd9Sstevel@tonic-gate 	if (debug > 2)
44567c478bd9Sstevel@tonic-gate 		fprintf(stderr, "provider is DL_STYLE2\n");
44577c478bd9Sstevel@tonic-gate 
44587c478bd9Sstevel@tonic-gate 	if (di_minor_type(minor) != DDM_ALIAS) {
44597c478bd9Sstevel@tonic-gate 		if (debug > 2)
44607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
44617c478bd9Sstevel@tonic-gate 			    "non-alias node, ignoring\n");
44627c478bd9Sstevel@tonic-gate 		goto done;
44637c478bd9Sstevel@tonic-gate 	}
44647c478bd9Sstevel@tonic-gate 
44657c478bd9Sstevel@tonic-gate 	if (debug > 2)
44667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
44677c478bd9Sstevel@tonic-gate 		    "alias node, using instance\n");
44687c478bd9Sstevel@tonic-gate 	ppa = di_instance(node);
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate 	if (dlpi_attach(fd, -1, ppa) < 0) {
44717c478bd9Sstevel@tonic-gate 		if (debug > 2)
44727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
44737c478bd9Sstevel@tonic-gate 			    "non-existent PPA, ignoring\n");
44747c478bd9Sstevel@tonic-gate 		goto done;
44757c478bd9Sstevel@tonic-gate 	}
44767c478bd9Sstevel@tonic-gate 
44777c478bd9Sstevel@tonic-gate 	(void) snprintf(ifname, LIFNAMSIZ, "%s%d", provider, ppa);
44787c478bd9Sstevel@tonic-gate 	add_ni(ifname);
44797c478bd9Sstevel@tonic-gate 
44807c478bd9Sstevel@tonic-gate done:
44817c478bd9Sstevel@tonic-gate 	(void) dlpi_close(fd);
44827c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
44837c478bd9Sstevel@tonic-gate }
44847c478bd9Sstevel@tonic-gate 
44857c478bd9Sstevel@tonic-gate /*
44867c478bd9Sstevel@tonic-gate  * dhcp-related routines
44877c478bd9Sstevel@tonic-gate  */
44887c478bd9Sstevel@tonic-gate 
44897c478bd9Sstevel@tonic-gate static int
44907c478bd9Sstevel@tonic-gate setifdhcp(const char *caller, const char *ifname, int argc, char *argv[])
44917c478bd9Sstevel@tonic-gate {
44927c478bd9Sstevel@tonic-gate 	dhcp_ipc_request_t	*request;
44937c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t	*reply	= NULL;
44947c478bd9Sstevel@tonic-gate 	int			timeout = DHCP_IPC_WAIT_DEFAULT;
44957c478bd9Sstevel@tonic-gate 	dhcp_ipc_type_t		type	= DHCP_START;
44967c478bd9Sstevel@tonic-gate 	int			error;
44977c478bd9Sstevel@tonic-gate 	boolean_t		is_primary = _B_FALSE;
44987c478bd9Sstevel@tonic-gate 	boolean_t		started = _B_FALSE;
44997c478bd9Sstevel@tonic-gate 
45007c478bd9Sstevel@tonic-gate 	for (argv++; --argc > 0; argv++) {
45017c478bd9Sstevel@tonic-gate 
45027c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "primary") == 0) {
45037c478bd9Sstevel@tonic-gate 			is_primary = _B_TRUE;
45047c478bd9Sstevel@tonic-gate 			continue;
45057c478bd9Sstevel@tonic-gate 		}
45067c478bd9Sstevel@tonic-gate 
45077c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "wait") == 0) {
45087c478bd9Sstevel@tonic-gate 			if (--argc <= 0) {
45097c478bd9Sstevel@tonic-gate 				usage();
45107c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
45117c478bd9Sstevel@tonic-gate 			}
45127c478bd9Sstevel@tonic-gate 			argv++;
45137c478bd9Sstevel@tonic-gate 
45147c478bd9Sstevel@tonic-gate 			if (strcmp(*argv, "forever") == 0) {
45157c478bd9Sstevel@tonic-gate 				timeout = DHCP_IPC_WAIT_FOREVER;
45167c478bd9Sstevel@tonic-gate 				continue;
45177c478bd9Sstevel@tonic-gate 			}
45187c478bd9Sstevel@tonic-gate 
45197c478bd9Sstevel@tonic-gate 			if (sscanf(*argv, "%d", &timeout) != 1) {
45207c478bd9Sstevel@tonic-gate 				usage();
45217c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
45227c478bd9Sstevel@tonic-gate 			}
45237c478bd9Sstevel@tonic-gate 
45247c478bd9Sstevel@tonic-gate 			if (timeout < 0) {
45257c478bd9Sstevel@tonic-gate 				usage();
45267c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
45277c478bd9Sstevel@tonic-gate 			}
45287c478bd9Sstevel@tonic-gate 			continue;
45297c478bd9Sstevel@tonic-gate 		}
45307c478bd9Sstevel@tonic-gate 
45317c478bd9Sstevel@tonic-gate 		type = dhcp_string_to_request(*argv);
45327c478bd9Sstevel@tonic-gate 		if (type == -1) {
45337c478bd9Sstevel@tonic-gate 			usage();
45347c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_BADARGS);
45357c478bd9Sstevel@tonic-gate 		}
45367c478bd9Sstevel@tonic-gate 	}
45377c478bd9Sstevel@tonic-gate 
45387c478bd9Sstevel@tonic-gate 	/*
45397c478bd9Sstevel@tonic-gate 	 * Only try to start agent on start or inform; in all other cases it
45407c478bd9Sstevel@tonic-gate 	 * has to already be running for anything to make sense.
45417c478bd9Sstevel@tonic-gate 	 */
45427c478bd9Sstevel@tonic-gate 	if (type == DHCP_START || type == DHCP_INFORM) {
45437c478bd9Sstevel@tonic-gate 		if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1) {
45447c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: unable to start %s\n",
45457c478bd9Sstevel@tonic-gate 			    caller, DHCP_AGENT_PATH);
45467c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_FAILURE);
45477c478bd9Sstevel@tonic-gate 		}
45487c478bd9Sstevel@tonic-gate 		started = _B_TRUE;
45497c478bd9Sstevel@tonic-gate 	}
45507c478bd9Sstevel@tonic-gate 
45517c478bd9Sstevel@tonic-gate 	if (is_primary)
45527c478bd9Sstevel@tonic-gate 		type = type | DHCP_PRIMARY;
45537c478bd9Sstevel@tonic-gate 
45547c478bd9Sstevel@tonic-gate 	request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
45557c478bd9Sstevel@tonic-gate 	if (request == NULL) {
45567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: out of memory\n", caller);
45577c478bd9Sstevel@tonic-gate 		return (DHCP_EXIT_SYSTEM);
45587c478bd9Sstevel@tonic-gate 	}
45597c478bd9Sstevel@tonic-gate 
45607c478bd9Sstevel@tonic-gate 	error = dhcp_ipc_make_request(request, &reply, timeout);
45617c478bd9Sstevel@tonic-gate 	if (error != 0) {
45627c478bd9Sstevel@tonic-gate 		free(request);
45637c478bd9Sstevel@tonic-gate 		/*
45647c478bd9Sstevel@tonic-gate 		 * Re-map connect error to not under control if we didn't try a
45657c478bd9Sstevel@tonic-gate 		 * start operation, as this has to be true and results in a
45667c478bd9Sstevel@tonic-gate 		 * clearer message, not to mention preserving compatibility
45677c478bd9Sstevel@tonic-gate 		 * with the days when we always started dhcpagent for every
45687c478bd9Sstevel@tonic-gate 		 * request.
45697c478bd9Sstevel@tonic-gate 		 */
45707c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_CONNECT && !started)
45717c478bd9Sstevel@tonic-gate 			error = DHCP_IPC_E_UNKIF;
45727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
45737c478bd9Sstevel@tonic-gate 		    dhcp_ipc_strerror(error));
45747c478bd9Sstevel@tonic-gate 		return (DHCP_EXIT_FAILURE);
45757c478bd9Sstevel@tonic-gate 	}
45767c478bd9Sstevel@tonic-gate 
45777c478bd9Sstevel@tonic-gate 	error = reply->return_code;
45787c478bd9Sstevel@tonic-gate 	if (error != 0) {
45797c478bd9Sstevel@tonic-gate 		free(request);
45807c478bd9Sstevel@tonic-gate 		free(reply);
45817c478bd9Sstevel@tonic-gate 
45827c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_TIMEOUT && timeout == 0)
45837c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_SUCCESS);
45847c478bd9Sstevel@tonic-gate 
45857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
45867c478bd9Sstevel@tonic-gate 		    dhcp_ipc_strerror(error));
45877c478bd9Sstevel@tonic-gate 
45887c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_TIMEOUT)
45897c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_TIMEOUT);
45907c478bd9Sstevel@tonic-gate 		else
45917c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_IF_FAILURE);
45927c478bd9Sstevel@tonic-gate 	}
45937c478bd9Sstevel@tonic-gate 
45947c478bd9Sstevel@tonic-gate 	if (DHCP_IPC_CMD(type) == DHCP_STATUS) {
45957c478bd9Sstevel@tonic-gate 		(void) printf("%s", dhcp_status_hdr_string());
45967c478bd9Sstevel@tonic-gate 		(void) printf("%s", dhcp_status_reply_to_string(reply));
45977c478bd9Sstevel@tonic-gate 	}
45987c478bd9Sstevel@tonic-gate 
45997c478bd9Sstevel@tonic-gate 	free(request);
46007c478bd9Sstevel@tonic-gate 	free(reply);
46017c478bd9Sstevel@tonic-gate 	return (DHCP_EXIT_SUCCESS);
46027c478bd9Sstevel@tonic-gate }
46037c478bd9Sstevel@tonic-gate 
46047c478bd9Sstevel@tonic-gate static void
46057c478bd9Sstevel@tonic-gate usage(void)
46067c478bd9Sstevel@tonic-gate {
46077c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
46087c478bd9Sstevel@tonic-gate 	    "usage: ifconfig <interface> | -a[ 4 | 6 | D ][ u | d ][ Z ]\n");
46097c478bd9Sstevel@tonic-gate 
46107c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s",
46117c478bd9Sstevel@tonic-gate 	    "\t[ <addr_family> ]\n"
46127c478bd9Sstevel@tonic-gate 	    "\t[ <address>[/<prefix_length>] [ <dest_address> ] ]\n"
46137c478bd9Sstevel@tonic-gate 	    "\t[ set [ <address>][/<prefix_length>] ]"
46147c478bd9Sstevel@tonic-gate 	    " [ <address>/<prefix_length>] ]\n"
46157c478bd9Sstevel@tonic-gate 	    "\t[ destination <dest_address> ]\n"
46167c478bd9Sstevel@tonic-gate 	    "\t[ addif <address>[/<prefix_length>]"
46177c478bd9Sstevel@tonic-gate 	    "  [ <dest_address> ] ]\n"
46187c478bd9Sstevel@tonic-gate 	    "\t[ removeif <address>[/<prefix_length>] ]\n"
46197c478bd9Sstevel@tonic-gate 	    "\t[ arp | -arp ]\n"
46207c478bd9Sstevel@tonic-gate 	    "\t[ auto-revarp ]\n"
46217c478bd9Sstevel@tonic-gate 	    "\t[ broadcast <broad_addr> ]\n"
46227c478bd9Sstevel@tonic-gate 	    "\t[ index <if_index> ]\n"
46237c478bd9Sstevel@tonic-gate 	    "\t[ metric <n> ] [ mtu <n> ]\n"
46247c478bd9Sstevel@tonic-gate 	    "\t[ netmask <mask> ]\n"
46257c478bd9Sstevel@tonic-gate 	    "\t[ plumb ] [ unplumb ]\n"
46267c478bd9Sstevel@tonic-gate 	    "\t[ preferred | -preferred ]\n"
46277c478bd9Sstevel@tonic-gate 	    "\t[ private | -private ]\n"
46287c478bd9Sstevel@tonic-gate 	    "\t[ local | -local ]\n"
46297c478bd9Sstevel@tonic-gate 	    "\t[ router | -router ]\n"
46307c478bd9Sstevel@tonic-gate 	    "\t[ subnet <subnet_address>]\n"
46317c478bd9Sstevel@tonic-gate 	    "\t[ trailers | -trailers ]\n"
46327c478bd9Sstevel@tonic-gate 	    "\t[ token <address>/<prefix_length> ]\n"
46337c478bd9Sstevel@tonic-gate 	    "\t[ tsrc <tunnel_src_address> ]\n"
46347c478bd9Sstevel@tonic-gate 	    "\t[ tdst <tunnel_dest_address> ]\n"
46357c478bd9Sstevel@tonic-gate 	    "\t[ auth_algs <tunnel_AH_authentication_algorithm> ]\n"
46367c478bd9Sstevel@tonic-gate 	    "\t[ encr_algs <tunnel_ESP_encryption_algorithm> ]\n"
46377c478bd9Sstevel@tonic-gate 	    "\t[ encr_auth_algs <tunnel_ESP_authentication_algorithm> ]\n"
46387c478bd9Sstevel@tonic-gate 	    "\t[ up ] [ down ]\n"
46397c478bd9Sstevel@tonic-gate 	    "\t[ xmit | -xmit ]\n"
46407c478bd9Sstevel@tonic-gate 	    "\t[ modlist ]\n"
46417c478bd9Sstevel@tonic-gate 	    "\t[ modinsert <module_name@position> ]\n"
46427c478bd9Sstevel@tonic-gate 	    "\t[ modremove <module_name@position> ]\n"
46437c478bd9Sstevel@tonic-gate 	    "\t[ group <groupname>] | [ group \"\"]\n"
46447c478bd9Sstevel@tonic-gate 	    "\t[ deprecated | -deprecated ]\n"
46457c478bd9Sstevel@tonic-gate 	    "\t[ standby | -standby ]\n"
46467c478bd9Sstevel@tonic-gate 	    "\t[ failover | -failover ]\n"
46477c478bd9Sstevel@tonic-gate 	    "\t[ zone <zonename> | -zone ]\n"
464845916cd2Sjpk 	    "\t[ usesrc <interface> ]\n"
464945916cd2Sjpk 	    "\t[ all-zones ]\n");
46507c478bd9Sstevel@tonic-gate 
46517c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "or\n");
46527c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
46537c478bd9Sstevel@tonic-gate 	    "\tifconfig <interface> |  -a[ 4 | 6 | D ] [ u | d ]\n");
46547c478bd9Sstevel@tonic-gate 
46557c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s", "\tauto-dhcp | dhcp\n"
46567c478bd9Sstevel@tonic-gate 	    "\t[ wait <time> | forever ]\n\t[ primary ]\n"
46577c478bd9Sstevel@tonic-gate 	    "\tstart | drop | ping | release | status | inform\n");
46587c478bd9Sstevel@tonic-gate }
4659