17c478bd9Sstevel@tonic-gate /*
2d62bc4baSyz  * Copyright 2008 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 #include <libdlpi.h>
18ff550d0eSmasputra #include <inet/ip.h>
19*d2f8a3dfSpwernau #include <inet/ipsec_impl.h>
20ff550d0eSmasputra 
217c478bd9Sstevel@tonic-gate #define	LOOPBACK_IF	"lo0"
227c478bd9Sstevel@tonic-gate #define	NONE_STR	"none"
237c478bd9Sstevel@tonic-gate #define	ARP_MOD_NAME	"arp"
247906a3e0Smeem #define	TUN_NAME	"tun"
257906a3e0Smeem #define	ATUN_NAME	"atun"
267906a3e0Smeem #define	TUN6TO4_NAME	"6to4tun"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate typedef struct if_flags {
297c478bd9Sstevel@tonic-gate 	uint64_t iff_value;
307c478bd9Sstevel@tonic-gate 	char	*iff_name;
317c478bd9Sstevel@tonic-gate } if_flags_t;
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate static if_flags_t	if_flags_tbl[] = {
347c478bd9Sstevel@tonic-gate 	{ IFF_UP,		"UP" },
357c478bd9Sstevel@tonic-gate 	{ IFF_BROADCAST,	"BROADCAST" },
367c478bd9Sstevel@tonic-gate 	{ IFF_DEBUG,		"DEBUG" },
377c478bd9Sstevel@tonic-gate 	{ IFF_LOOPBACK,		"LOOPBACK" },
387c478bd9Sstevel@tonic-gate 	{ IFF_POINTOPOINT,	"POINTOPOINT" },
397c478bd9Sstevel@tonic-gate 	{ IFF_NOTRAILERS,	"NOTRAILERS" },
407c478bd9Sstevel@tonic-gate 	{ IFF_RUNNING,		"RUNNING" },
417c478bd9Sstevel@tonic-gate 	{ IFF_NOARP,		"NOARP" },
427c478bd9Sstevel@tonic-gate 	{ IFF_PROMISC,		"PROMISC" },
437c478bd9Sstevel@tonic-gate 	{ IFF_ALLMULTI,		"ALLMULTI" },
447c478bd9Sstevel@tonic-gate 	{ IFF_INTELLIGENT,	"INTELLIGENT" },
457c478bd9Sstevel@tonic-gate 	{ IFF_MULTICAST,	"MULTICAST" },
467c478bd9Sstevel@tonic-gate 	{ IFF_MULTI_BCAST,	"MULTI_BCAST" },
477c478bd9Sstevel@tonic-gate 	{ IFF_UNNUMBERED,	"UNNUMBERED" },
487c478bd9Sstevel@tonic-gate 	{ IFF_DHCPRUNNING,	"DHCP" },
497c478bd9Sstevel@tonic-gate 	{ IFF_PRIVATE,		"PRIVATE" },
507c478bd9Sstevel@tonic-gate 	{ IFF_NOXMIT,		"NOXMIT" },
517c478bd9Sstevel@tonic-gate 	{ IFF_NOLOCAL,		"NOLOCAL" },
527c478bd9Sstevel@tonic-gate 	{ IFF_DEPRECATED,	"DEPRECATED" },
537c478bd9Sstevel@tonic-gate 	{ IFF_ADDRCONF,		"ADDRCONF" },
547c478bd9Sstevel@tonic-gate 	{ IFF_ROUTER,		"ROUTER" },
557c478bd9Sstevel@tonic-gate 	{ IFF_NONUD,		"NONUD" },
567c478bd9Sstevel@tonic-gate 	{ IFF_ANYCAST,		"ANYCAST" },
577c478bd9Sstevel@tonic-gate 	{ IFF_NORTEXCH,		"NORTEXCH" },
587c478bd9Sstevel@tonic-gate 	{ IFF_IPV4,		"IPv4" },
597c478bd9Sstevel@tonic-gate 	{ IFF_IPV6,		"IPv6" },
607c478bd9Sstevel@tonic-gate 	{ IFF_NOFAILOVER,	"NOFAILOVER" },
617c478bd9Sstevel@tonic-gate 	{ IFF_FAILED,		"FAILED" },
627c478bd9Sstevel@tonic-gate 	{ IFF_STANDBY,		"STANDBY" },
637c478bd9Sstevel@tonic-gate 	{ IFF_INACTIVE,		"INACTIVE" },
647c478bd9Sstevel@tonic-gate 	{ IFF_OFFLINE,		"OFFLINE" },
657c478bd9Sstevel@tonic-gate 	{ IFF_XRESOLV,		"XRESOLV" },
667c478bd9Sstevel@tonic-gate 	{ IFF_COS_ENABLED,	"CoS" },
677c478bd9Sstevel@tonic-gate 	{ IFF_PREFERRED,	"PREFERRED" },
687c478bd9Sstevel@tonic-gate 	{ IFF_TEMPORARY,	"TEMPORARY" },
697c478bd9Sstevel@tonic-gate 	{ IFF_FIXEDMTU,		"FIXEDMTU" },
7069bb4bb4Scarlsonj 	{ IFF_VIRTUAL,		"VIRTUAL" },
7169bb4bb4Scarlsonj 	{ IFF_DUPLICATE,	"DUPLICATE" }
727c478bd9Sstevel@tonic-gate };
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static struct	lifreq lifr;
757906a3e0Smeem /* current interface name a particular function is accessing */
767c478bd9Sstevel@tonic-gate static char	name[LIFNAMSIZ];
777c478bd9Sstevel@tonic-gate /* foreach interface saved name */
787c478bd9Sstevel@tonic-gate static char	origname[LIFNAMSIZ];
797c478bd9Sstevel@tonic-gate static char savedname[LIFNAMSIZ];	/* For addif */
807c478bd9Sstevel@tonic-gate static int	setaddr;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Make sure the algorithm variables hold more than the sizeof an algorithm
847c478bd9Sstevel@tonic-gate  * in PF_KEY.  (For now, more than a uint8_t.)  The NO_***_?ALG indicates that
857c478bd9Sstevel@tonic-gate  * there was no algorithm requested, and in the ipsec_req that service should
867c478bd9Sstevel@tonic-gate  * be disabled.  (E.g. if ah_aalg remains NO_AH_AALG, then AH will be
877c478bd9Sstevel@tonic-gate  * disabled on that tunnel.)
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate #define	NO_AH_AALG 256
907c478bd9Sstevel@tonic-gate #define	NO_ESP_AALG 256
917c478bd9Sstevel@tonic-gate #define	NO_ESP_EALG 256
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * iface_t
957c478bd9Sstevel@tonic-gate  * used by setifether to create a list of interfaces to mark
967c478bd9Sstevel@tonic-gate  * down-up when changing the ethernet address of an interface
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate typedef struct iface {
997c478bd9Sstevel@tonic-gate 	struct lifreq lifr;
1007c478bd9Sstevel@tonic-gate 	struct iface *next;	/* pointer to the next list element */
1017c478bd9Sstevel@tonic-gate } iface_t;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static	iface_t	*logifs = NULL; /* list of logical interfaces */
1047c478bd9Sstevel@tonic-gate static 	iface_t	*phyif	= NULL;	/* physical interface */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate int	s;
1077c478bd9Sstevel@tonic-gate int	af = AF_INET;	/* default address family */
1087c478bd9Sstevel@tonic-gate int	debug = 0;
1097c478bd9Sstevel@tonic-gate int	all = 0;	/* setifdhcp() needs to know this */
1107c478bd9Sstevel@tonic-gate int	verbose = 0;
1117c478bd9Sstevel@tonic-gate int	v4compat = 0;	/* Compatible printing format */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * Function prototypes for command functions.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate static int	addif(char *arg, int64_t param);
1177c478bd9Sstevel@tonic-gate static int	inetplumb(char *arg, int64_t param);
1187c478bd9Sstevel@tonic-gate static int	inetunplumb(char *arg, int64_t param);
1197c478bd9Sstevel@tonic-gate static int	removeif(char *arg, int64_t param);
1207c478bd9Sstevel@tonic-gate static int	setdebugflag(char *arg, int64_t param);
1217c478bd9Sstevel@tonic-gate static int	setifaddr(char *arg, int64_t param);
1227c478bd9Sstevel@tonic-gate static int	setifbroadaddr(char *arg, int64_t param);
1237c478bd9Sstevel@tonic-gate static int	setifdstaddr(char *arg, int64_t param);
1247c478bd9Sstevel@tonic-gate static int	setifether(char *arg, int64_t param);
1257c478bd9Sstevel@tonic-gate static int	setifflags(char *arg, int64_t param);
1267c478bd9Sstevel@tonic-gate static int	setifindex(char *arg, int64_t param);
1277c478bd9Sstevel@tonic-gate static int	setifmetric(char *arg, int64_t param);
1287c478bd9Sstevel@tonic-gate static int	setifmtu(char *arg, int64_t param);
1297c478bd9Sstevel@tonic-gate static int	setifnetmask(char *arg, int64_t param);
1307c478bd9Sstevel@tonic-gate static int	setifprefixlen(char *arg, int64_t param);
1317c478bd9Sstevel@tonic-gate static int	setifrevarp(char *arg, int64_t param);
1327c478bd9Sstevel@tonic-gate static int	setifsubnet(char *arg, int64_t param);
1337c478bd9Sstevel@tonic-gate static int	setiftdst(char *arg, int64_t param);
1347c478bd9Sstevel@tonic-gate static int	setiftoken(char *arg, int64_t param);
1357c478bd9Sstevel@tonic-gate static int	setiftsrc(char *arg, int64_t param);
1367c478bd9Sstevel@tonic-gate static int	setverboseflag(char *arg, int64_t param);
1377c478bd9Sstevel@tonic-gate static int	set_tun_ah_alg(char *arg, int64_t param);
1387c478bd9Sstevel@tonic-gate static int	set_tun_esp_auth_alg(char *arg, int64_t param);
1397c478bd9Sstevel@tonic-gate static int	set_tun_esp_encr_alg(char *arg, int64_t param);
1407c478bd9Sstevel@tonic-gate static int	modlist(char *arg, int64_t param);
1417c478bd9Sstevel@tonic-gate static int	modinsert(char *arg, int64_t param);
1427c478bd9Sstevel@tonic-gate static int	modremove(char *arg, int64_t param);
1437c478bd9Sstevel@tonic-gate static int	setifgroupname(char *arg, int64_t param);
1447c478bd9Sstevel@tonic-gate static int	configinfo(char *arg, int64_t param);
1457c478bd9Sstevel@tonic-gate static void	print_config_flags(uint64_t flags);
1467c478bd9Sstevel@tonic-gate static void	print_flags(uint64_t flags);
1477c478bd9Sstevel@tonic-gate static void	print_ifether(char *ifname);
1487c478bd9Sstevel@tonic-gate static int	set_tun_encap_limit(char *arg, int64_t param);
1497c478bd9Sstevel@tonic-gate static int	clr_tun_encap_limit(char *arg, int64_t param);
1507c478bd9Sstevel@tonic-gate static int	set_tun_hop_limit(char *arg, int64_t param);
1517c478bd9Sstevel@tonic-gate static int	setzone(char *arg, int64_t param);
15245916cd2Sjpk static int	setallzones(char *arg, int64_t param);
1537c478bd9Sstevel@tonic-gate static int	setifsrc(char *arg, int64_t param);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Address family specific function prototypes.
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate static void	in_getaddr(char *s, struct sockaddr *saddr, int *plenp);
1597c478bd9Sstevel@tonic-gate static void	in_status(int force, uint64_t flags);
1607c478bd9Sstevel@tonic-gate static void	in_configinfo(int force, uint64_t flags);
1617c478bd9Sstevel@tonic-gate static void	in6_getaddr(char *s, struct sockaddr *saddr, int *plenp);
1627c478bd9Sstevel@tonic-gate static void	in6_status(int force, uint64_t flags);
1637c478bd9Sstevel@tonic-gate static void	in6_configinfo(int force, uint64_t flags);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * Misc support functions
1677c478bd9Sstevel@tonic-gate  */
168d62bc4baSyz static boolean_t	ni_entry(const char *, void *);
1697c478bd9Sstevel@tonic-gate static void	foreachinterface(void (*func)(), int argc, char *argv[],
1707c478bd9Sstevel@tonic-gate 		    int af, int64_t onflags, int64_t offflags,
1717c478bd9Sstevel@tonic-gate 		    int64_t lifc_flags);
1727c478bd9Sstevel@tonic-gate static void	ifconfig(int argc, char *argv[], int af, struct lifreq *lifrp);
173dd7a6f5fSkcpoon static boolean_t	in_getmask(struct sockaddr_in *saddr,
174dd7a6f5fSkcpoon 			    boolean_t addr_set);
1757c478bd9Sstevel@tonic-gate static int	in_getprefixlen(char *addr, boolean_t slash, int plen);
1767c478bd9Sstevel@tonic-gate static boolean_t	in_prefixlentomask(int prefixlen, int maxlen,
1777c478bd9Sstevel@tonic-gate 			    uchar_t *mask);
1787c478bd9Sstevel@tonic-gate static int	settaddr(char *, int (*)(icfg_handle_t,
1797c478bd9Sstevel@tonic-gate 			    const struct sockaddr *, socklen_t));
1807c478bd9Sstevel@tonic-gate static void	status(void);
1817c478bd9Sstevel@tonic-gate static void	ifstatus(const char *);
1827c478bd9Sstevel@tonic-gate static void	usage(void);
1837c478bd9Sstevel@tonic-gate static int	strioctl(int s, int cmd, char *buf, int buflen);
1847c478bd9Sstevel@tonic-gate static int	setifdhcp(const char *caller, const char *ifname,
1857c478bd9Sstevel@tonic-gate 		    int argc, char *argv[]);
186fc80c0dfSnordmark static int	ip_domux2fd(int *, int *, int *, int *, int *);
187fc80c0dfSnordmark static int	ip_plink(int, int, int, int, int);
1887c478bd9Sstevel@tonic-gate static int	modop(char *arg, char op);
1897c478bd9Sstevel@tonic-gate static void	selectifs(int argc, char *argv[], int af,
1907c478bd9Sstevel@tonic-gate 			struct lifreq *lifrp);
1917c478bd9Sstevel@tonic-gate static int	updownifs(iface_t *ifs, int up);
192f4b3ec61Sdh static int	find_all_global_interfaces(struct lifconf *lifcp, char **buf,
193f4b3ec61Sdh 		    int64_t lifc_flags);
194f4b3ec61Sdh static int	find_all_zone_interfaces(struct lifconf *lifcp, char **buf,
195f4b3ec61Sdh 		    int64_t lifc_flags);
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 
2077c478bd9Sstevel@tonic-gate #define	NEXTARG		0xffffff	/* command takes an argument */
2087c478bd9Sstevel@tonic-gate #define	OPTARG		0xfffffe 	/* command takes an optional argument */
2097c478bd9Sstevel@tonic-gate #define	AF_ANY		(-1)
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /* Refer to the comments in ifconfig() on the netmask "hack" */
2127c478bd9Sstevel@tonic-gate #define	NETMASK_CMD	"netmask"
2137c478bd9Sstevel@tonic-gate struct sockaddr_storage	g_netmask;
214dd7a6f5fSkcpoon enum { G_NETMASK_NIL, G_NETMASK_PENDING, G_NETMASK_SET }
215dd7a6f5fSkcpoon     g_netmask_set = G_NETMASK_NIL;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate struct	cmd {
2187c478bd9Sstevel@tonic-gate 	char		*c_name;
2197c478bd9Sstevel@tonic-gate 	int64_t		c_parameter;	/* NEXTARG means next argv */
2207c478bd9Sstevel@tonic-gate 	int		(*c_func)(char *, int64_t);
2217c478bd9Sstevel@tonic-gate 	int		c_abortonfail;	/* don't continue parsing args */
2227c478bd9Sstevel@tonic-gate 					/* for the current interface */
2237c478bd9Sstevel@tonic-gate 	int	c_af;			/* address family restrictions */
2247c478bd9Sstevel@tonic-gate } cmds[] = {
2257c478bd9Sstevel@tonic-gate 	{ "up",		IFF_UP,		setifflags,	0,	AF_ANY },
2267c478bd9Sstevel@tonic-gate 	{ "down",	-IFF_UP,	setifflags,	0,	AF_ANY },
2277c478bd9Sstevel@tonic-gate 	{ "trailers",	-IFF_NOTRAILERS, setifflags,	0,	AF_ANY },
2287c478bd9Sstevel@tonic-gate 	{ "-trailers",	IFF_NOTRAILERS,	setifflags,	0,	AF_ANY },
2297c478bd9Sstevel@tonic-gate 	{ "arp",	-IFF_NOARP,	setifflags,	0,	AF_INET },
2307c478bd9Sstevel@tonic-gate 	{ "-arp",	IFF_NOARP,	setifflags,	0,	AF_INET },
2317c478bd9Sstevel@tonic-gate 	{ "router",	IFF_ROUTER,	setifflags,	0,	AF_ANY },
2327c478bd9Sstevel@tonic-gate 	{ "-router",	-IFF_ROUTER,	setifflags,	0,	AF_ANY },
2337c478bd9Sstevel@tonic-gate 	{ "private",	IFF_PRIVATE,	setifflags,	0,	AF_ANY },
2347c478bd9Sstevel@tonic-gate 	{ "-private",	-IFF_PRIVATE,	setifflags,	0,	AF_ANY },
2357c478bd9Sstevel@tonic-gate 	{ "xmit",	-IFF_NOXMIT,	setifflags,	0,	AF_ANY },
2367c478bd9Sstevel@tonic-gate 	{ "-xmit",	IFF_NOXMIT,	setifflags,	0,	AF_ANY },
2377c478bd9Sstevel@tonic-gate 	{ "-nud",	IFF_NONUD,	setifflags,	0,	AF_INET6 },
2387c478bd9Sstevel@tonic-gate 	{ "nud",	-IFF_NONUD,	setifflags,	0,	AF_INET6 },
2397c478bd9Sstevel@tonic-gate 	{ "anycast",	IFF_ANYCAST,	setifflags,	0,	AF_ANY },
2407c478bd9Sstevel@tonic-gate 	{ "-anycast",	-IFF_ANYCAST,	setifflags,	0,	AF_ANY },
2417c478bd9Sstevel@tonic-gate 	{ "local",	-IFF_NOLOCAL,	setifflags,	0,	AF_ANY },
2427c478bd9Sstevel@tonic-gate 	{ "-local",	IFF_NOLOCAL,	setifflags,	0,	AF_ANY },
2437c478bd9Sstevel@tonic-gate 	{ "deprecated",	IFF_DEPRECATED,	setifflags,	0,	AF_ANY },
2447c478bd9Sstevel@tonic-gate 	{ "-deprecated", -IFF_DEPRECATED, setifflags,	0,	AF_ANY },
2457c478bd9Sstevel@tonic-gate 	{ "preferred",	IFF_PREFERRED,	setifflags,	0,	AF_INET6 },
2467c478bd9Sstevel@tonic-gate 	{ "-preferred",	-IFF_PREFERRED,	setifflags,	0,	AF_INET6 },
2477c478bd9Sstevel@tonic-gate 	{ "debug",	0,		setdebugflag,	0,	AF_ANY },
2487c478bd9Sstevel@tonic-gate 	{ "verbose",	0,		setverboseflag,	0,	AF_ANY },
2497c478bd9Sstevel@tonic-gate 	{ NETMASK_CMD,	NEXTARG,	setifnetmask,	0,	AF_INET },
2507c478bd9Sstevel@tonic-gate 	{ "metric",	NEXTARG,	setifmetric,	0,	AF_ANY },
2517c478bd9Sstevel@tonic-gate 	{ "mtu",	NEXTARG,	setifmtu,	0,	AF_ANY },
2527c478bd9Sstevel@tonic-gate 	{ "index",	NEXTARG,	setifindex,	0,	AF_ANY },
2537c478bd9Sstevel@tonic-gate 	{ "broadcast",	NEXTARG,	setifbroadaddr,	0,	AF_INET },
2547c478bd9Sstevel@tonic-gate 	{ "auto-revarp", 0,		setifrevarp,	1,	AF_INET },
2557c478bd9Sstevel@tonic-gate 	{ "plumb",	0,		inetplumb,	1,	AF_ANY },
2567c478bd9Sstevel@tonic-gate 	{ "unplumb",	0,		inetunplumb,	0,	AF_ANY },
2577c478bd9Sstevel@tonic-gate 	{ "subnet",	NEXTARG,	setifsubnet,	0,	AF_ANY },
2587c478bd9Sstevel@tonic-gate 	{ "token",	NEXTARG,	setiftoken,	0,	AF_INET6 },
2597c478bd9Sstevel@tonic-gate 	{ "tsrc",	NEXTARG,	setiftsrc,	0,	AF_ANY },
2607c478bd9Sstevel@tonic-gate 	{ "tdst",	NEXTARG,	setiftdst,	0,	AF_ANY },
2617c478bd9Sstevel@tonic-gate 	{ "encr_auth_algs", NEXTARG,	set_tun_esp_auth_alg, 0, AF_ANY },
2627c478bd9Sstevel@tonic-gate 	{ "encr_algs",	NEXTARG,	set_tun_esp_encr_alg, 0, AF_ANY },
2637c478bd9Sstevel@tonic-gate 	{ "auth_algs",	NEXTARG,	set_tun_ah_alg,	0,	AF_ANY },
2647c478bd9Sstevel@tonic-gate 	{ "addif",	NEXTARG,	addif,		1,	AF_ANY },
2657c478bd9Sstevel@tonic-gate 	{ "removeif",	NEXTARG,	removeif,	1,	AF_ANY },
2667c478bd9Sstevel@tonic-gate 	{ "modlist",	0,		modlist,	1,	AF_ANY },
2677c478bd9Sstevel@tonic-gate 	{ "modinsert",	NEXTARG,	modinsert,	1,	AF_ANY },
2687c478bd9Sstevel@tonic-gate 	{ "modremove",	NEXTARG,	modremove,	1,	AF_ANY },
2697c478bd9Sstevel@tonic-gate 	{ "failover",	-IFF_NOFAILOVER, setifflags,	1,	AF_ANY },
2707c478bd9Sstevel@tonic-gate 	{ "-failover",	IFF_NOFAILOVER, setifflags,	1,	AF_ANY },
2717c478bd9Sstevel@tonic-gate 	{ "standby",	IFF_STANDBY,	setifflags,	1,	AF_ANY },
2727c478bd9Sstevel@tonic-gate 	{ "-standby",	-IFF_STANDBY,	setifflags,	1,	AF_ANY },
2737c478bd9Sstevel@tonic-gate 	{ "failed",	IFF_FAILED,	setifflags,	1,	AF_ANY },
2747c478bd9Sstevel@tonic-gate 	{ "-failed",	-IFF_FAILED,	setifflags,	1,	AF_ANY },
2757c478bd9Sstevel@tonic-gate 	{ "group",	NEXTARG,	setifgroupname,	1,	AF_ANY },
2767c478bd9Sstevel@tonic-gate 	{ "configinfo",	0,		configinfo,	1,	AF_ANY },
2777906a3e0Smeem 	{ "encaplimit",	NEXTARG,	set_tun_encap_limit,	0, AF_ANY },
2787906a3e0Smeem 	{ "-encaplimit", 0,		clr_tun_encap_limit,	0, AF_ANY },
2797906a3e0Smeem 	{ "thoplimit",	NEXTARG,	set_tun_hop_limit,	0, AF_ANY },
2807c478bd9Sstevel@tonic-gate 	{ "set",	NEXTARG,	setifaddr,	0,	AF_ANY },
2817c478bd9Sstevel@tonic-gate 	{ "destination", NEXTARG,	setifdstaddr,	0,	AF_ANY },
2827c478bd9Sstevel@tonic-gate 	{ "zone",	NEXTARG,	setzone,	0,	AF_ANY },
2837c478bd9Sstevel@tonic-gate 	{ "-zone",	0,		setzone,	0,	AF_ANY },
28445916cd2Sjpk 	{ "all-zones",	0,		setallzones,	0,	AF_ANY },
2857c478bd9Sstevel@tonic-gate 	{ "ether",	OPTARG,		setifether,	0,	AF_ANY },
2867c478bd9Sstevel@tonic-gate 	{ "usesrc",	NEXTARG,	setifsrc,	0,	AF_ANY },
287f7d61273Smeem 
288f7d61273Smeem 	/*
289f7d61273Smeem 	 * NOTE: any additions to this table must also be applied to ifparse
290f7d61273Smeem 	 *	(usr/src/cmd/cmd-inet/sbin/ifparse/ifparse.c)
291f7d61273Smeem 	 */
292f7d61273Smeem 
2937c478bd9Sstevel@tonic-gate 	{ 0,		0,		setifaddr,	0,	AF_ANY },
2947c478bd9Sstevel@tonic-gate 	{ 0,		0,		setifdstaddr,	0,	AF_ANY },
2957c478bd9Sstevel@tonic-gate 	{ 0,		0,		0,		0,	0 },
2967c478bd9Sstevel@tonic-gate };
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate typedef struct if_config_cmd {
3007c478bd9Sstevel@tonic-gate 	uint64_t	iff_flag;
3017c478bd9Sstevel@tonic-gate 	char		*iff_name;
3027c478bd9Sstevel@tonic-gate } if_config_cmd_t;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate static if_config_cmd_t	if_config_cmd_tbl[] = {
3057c478bd9Sstevel@tonic-gate 	{ IFF_UP,		"up" },
3067c478bd9Sstevel@tonic-gate 	{ IFF_NOTRAILERS,	"-trailers" },
3077c478bd9Sstevel@tonic-gate 	{ IFF_PRIVATE,		"private" },
3087c478bd9Sstevel@tonic-gate 	{ IFF_NOXMIT,		"-xmit" },
3097c478bd9Sstevel@tonic-gate 	{ IFF_ANYCAST,		"anycast" },
3107c478bd9Sstevel@tonic-gate 	{ IFF_NOLOCAL,		"-local" },
3117c478bd9Sstevel@tonic-gate 	{ IFF_DEPRECATED,	"deprecated" },
3127c478bd9Sstevel@tonic-gate 	{ IFF_NOFAILOVER,	"-failover" },
3137c478bd9Sstevel@tonic-gate 	{ IFF_STANDBY,		"standby" },
3147c478bd9Sstevel@tonic-gate 	{ IFF_FAILED,		"failed" },
3157c478bd9Sstevel@tonic-gate 	{ IFF_PREFERRED,	"preferred" },
3167c478bd9Sstevel@tonic-gate 	{ 0,			0 },
3177c478bd9Sstevel@tonic-gate };
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate typedef struct ni {
3207c478bd9Sstevel@tonic-gate 	char		ni_name[LIFNAMSIZ];
3217c478bd9Sstevel@tonic-gate 	struct ni	*ni_next;
3227c478bd9Sstevel@tonic-gate } ni_t;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate static ni_t	*ni_list = NULL;
3257c478bd9Sstevel@tonic-gate static int	num_ni = 0;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /* End defines and structure definitions for ifconfig -a plumb */
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate /* Known address families */
3307c478bd9Sstevel@tonic-gate struct afswtch {
3317c478bd9Sstevel@tonic-gate 	char *af_name;
3327c478bd9Sstevel@tonic-gate 	short af_af;
3337c478bd9Sstevel@tonic-gate 	void (*af_status)();
3347c478bd9Sstevel@tonic-gate 	void (*af_getaddr)();
3357c478bd9Sstevel@tonic-gate 	void (*af_configinfo)();
3367c478bd9Sstevel@tonic-gate } afs[] = {
3377c478bd9Sstevel@tonic-gate 	{ "inet", AF_INET, in_status, in_getaddr, in_configinfo },
3387c478bd9Sstevel@tonic-gate 	{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_configinfo },
3397c478bd9Sstevel@tonic-gate 	{ 0, 0,	0, 0, 0 }
3407c478bd9Sstevel@tonic-gate };
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate struct afswtch *afp;	/* the address family being set or asked about */
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate int
3477c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	/* Include IFF_NOXMIT, IFF_TEMPORARY and all zone interfaces */
3507c478bd9Sstevel@tonic-gate 	int64_t lifc_flags = LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES;
3517c478bd9Sstevel@tonic-gate 	char *default_ip_str;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (argc < 2) {
3547c478bd9Sstevel@tonic-gate 		usage();
3557c478bd9Sstevel@tonic-gate 		exit(1);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	argc--, argv++;
3587c478bd9Sstevel@tonic-gate 	if (strlen(*argv) > sizeof (name) - 1) {
3597c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: interface name too long\n", *argv);
3607c478bd9Sstevel@tonic-gate 		exit(1);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 	(void) strncpy(name, *argv, sizeof (name));
3637c478bd9Sstevel@tonic-gate 	name[sizeof (name) - 1] = '\0';
3647c478bd9Sstevel@tonic-gate 	(void) strncpy(origname, name, sizeof (origname));	/* For addif */
3657c478bd9Sstevel@tonic-gate 	default_ip_str = NULL;
3667c478bd9Sstevel@tonic-gate 	v4compat = get_compat_flag(&default_ip_str);
3677c478bd9Sstevel@tonic-gate 	if (v4compat == DEFAULT_PROT_BAD_VALUE) {
3687c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3697c478bd9Sstevel@tonic-gate 		    "ifconfig: %s: Bad value for %s in %s\n", default_ip_str,
3707c478bd9Sstevel@tonic-gate 		    DEFAULT_IP, INET_DEFAULT_FILE);
3717c478bd9Sstevel@tonic-gate 		free(default_ip_str);
3727c478bd9Sstevel@tonic-gate 		exit(2);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 	free(default_ip_str);
3757c478bd9Sstevel@tonic-gate 	argc--, argv++;
3767c478bd9Sstevel@tonic-gate 	if (argc > 0) {
3777c478bd9Sstevel@tonic-gate 		struct afswtch *myafp;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 		for (myafp = afp = afs; myafp->af_name; myafp++) {
3807c478bd9Sstevel@tonic-gate 			if (strcmp(myafp->af_name, *argv) == 0) {
3817c478bd9Sstevel@tonic-gate 				afp = myafp; argc--; argv++;
3827c478bd9Sstevel@tonic-gate 				break;
3837c478bd9Sstevel@tonic-gate 			}
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 		af = lifr.lifr_addr.ss_family = afp->af_af;
3867c478bd9Sstevel@tonic-gate 		if (af == AF_INET6) {
3877c478bd9Sstevel@tonic-gate 			v4compat = 0;
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	s = socket(SOCKET_AF(af), SOCK_DGRAM, 0);
3927c478bd9Sstevel@tonic-gate 	if (s < 0) {
3937c478bd9Sstevel@tonic-gate 		Perror0_exit("socket");
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * Special interface names is any combination of these flags.
3987c478bd9Sstevel@tonic-gate 	 * Note that due to the ifconfig syntax they have to be combined
3997c478bd9Sstevel@tonic-gate 	 * as a single '-' option.
4007c478bd9Sstevel@tonic-gate 	 *	-a	All interfaces
4017c478bd9Sstevel@tonic-gate 	 *	-u	"up" interfaces
4027c478bd9Sstevel@tonic-gate 	 *	-d	"down" interfaces
4037c478bd9Sstevel@tonic-gate 	 *	-D	Interfaces not controlled by DHCP
4047c478bd9Sstevel@tonic-gate 	 *	-4	IPv4 interfaces
4057c478bd9Sstevel@tonic-gate 	 *	-6	IPv6 interfaces
4067c478bd9Sstevel@tonic-gate 	 *	-X	Turn on debug (not documented)
4077c478bd9Sstevel@tonic-gate 	 *	-v	Turn on verbose
4087c478bd9Sstevel@tonic-gate 	 *	-Z	Only interfaces in caller's zone
4097c478bd9Sstevel@tonic-gate 	 */
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if (name[0] == '-') {
4127c478bd9Sstevel@tonic-gate 		/* One or more options */
4137c478bd9Sstevel@tonic-gate 		int64_t onflags = 0;
4147c478bd9Sstevel@tonic-gate 		int64_t offflags = 0;
4157c478bd9Sstevel@tonic-gate 		int c;
4167c478bd9Sstevel@tonic-gate 		char *av[2] = { "ifconfig", name };
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		while ((c = getopt(2, av, "audDXZ46v")) != -1) {
4197c478bd9Sstevel@tonic-gate 			switch ((char)c) {
4207c478bd9Sstevel@tonic-gate 			case 'a':
4217c478bd9Sstevel@tonic-gate 				all = 1;
4227c478bd9Sstevel@tonic-gate 				break;
4237c478bd9Sstevel@tonic-gate 			case 'u':
4247c478bd9Sstevel@tonic-gate 				onflags |= IFF_UP;
4257c478bd9Sstevel@tonic-gate 				break;
4267c478bd9Sstevel@tonic-gate 			case 'd':
4277c478bd9Sstevel@tonic-gate 				offflags |= IFF_UP;
4287c478bd9Sstevel@tonic-gate 				break;
4297c478bd9Sstevel@tonic-gate 			case 'D':
4307c478bd9Sstevel@tonic-gate 				offflags |= IFF_DHCPRUNNING;
4317c478bd9Sstevel@tonic-gate 				break;
4327c478bd9Sstevel@tonic-gate 			case 'X':
4337c478bd9Sstevel@tonic-gate 				debug += 3;
4347c478bd9Sstevel@tonic-gate 				break;
4357c478bd9Sstevel@tonic-gate 			case 'Z':
4367c478bd9Sstevel@tonic-gate 				lifc_flags &= ~LIFC_ALLZONES;
4377c478bd9Sstevel@tonic-gate 				break;
4387c478bd9Sstevel@tonic-gate 			case '4':
4397c478bd9Sstevel@tonic-gate 				/*
4407c478bd9Sstevel@tonic-gate 				 * -4 is not a compatable flag, therefore
4417c478bd9Sstevel@tonic-gate 				 * we assume they want v4compat turned off
4427c478bd9Sstevel@tonic-gate 				 */
4437c478bd9Sstevel@tonic-gate 				v4compat = 0;
4447c478bd9Sstevel@tonic-gate 				onflags |= IFF_IPV4;
4457c478bd9Sstevel@tonic-gate 				break;
4467c478bd9Sstevel@tonic-gate 			case '6':
4477c478bd9Sstevel@tonic-gate 				/*
4487c478bd9Sstevel@tonic-gate 				 * If they want IPv6, well then we'll assume
4497c478bd9Sstevel@tonic-gate 				 * they don't want IPv4 compat
4507c478bd9Sstevel@tonic-gate 				 */
4517c478bd9Sstevel@tonic-gate 				v4compat = 0;
4527c478bd9Sstevel@tonic-gate 				onflags |= IFF_IPV6;
4537c478bd9Sstevel@tonic-gate 				break;
4547c478bd9Sstevel@tonic-gate 			case 'v':
4557c478bd9Sstevel@tonic-gate 				verbose = 1;
4567c478bd9Sstevel@tonic-gate 				break;
4577c478bd9Sstevel@tonic-gate 			case '?':
4587c478bd9Sstevel@tonic-gate 				usage();
4597c478bd9Sstevel@tonic-gate 				exit(1);
4607c478bd9Sstevel@tonic-gate 			}
4617c478bd9Sstevel@tonic-gate 		}
4627c478bd9Sstevel@tonic-gate 		if (!all) {
4637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
464c7e4935fSss 			    "ifconfig: %s: no such interface\n", name);
4657c478bd9Sstevel@tonic-gate 			exit(1);
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 		foreachinterface(ifconfig, argc, argv, af, onflags, offflags,
4687c478bd9Sstevel@tonic-gate 		    lifc_flags);
4697c478bd9Sstevel@tonic-gate 	} else {
4707c478bd9Sstevel@tonic-gate 		ifconfig(argc, argv, af, (struct lifreq *)NULL);
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 	return (0);
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate /*
4767c478bd9Sstevel@tonic-gate  * For each interface, call (*func)(argc, argv, af, lifrp).
4777c478bd9Sstevel@tonic-gate  * Only call function if onflags and offflags are set or clear, respectively,
4787c478bd9Sstevel@tonic-gate  * in the interfaces flags field.
4797c478bd9Sstevel@tonic-gate  */
4807c478bd9Sstevel@tonic-gate static void
4817c478bd9Sstevel@tonic-gate foreachinterface(void (*func)(), int argc, char *argv[], int af,
4827c478bd9Sstevel@tonic-gate     int64_t onflags, int64_t offflags, int64_t lifc_flags)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	int n;
4857c478bd9Sstevel@tonic-gate 	char *buf;
4867c478bd9Sstevel@tonic-gate 	struct lifnum lifn;
4877c478bd9Sstevel@tonic-gate 	struct lifconf lifc;
4887c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp;
4897c478bd9Sstevel@tonic-gate 	struct lifreq lifrl;	/* Local lifreq struct */
4907c478bd9Sstevel@tonic-gate 	int numifs;
4917c478bd9Sstevel@tonic-gate 	unsigned bufsize;
4927c478bd9Sstevel@tonic-gate 	int plumball = 0;
4937c478bd9Sstevel@tonic-gate 	int save_af = af;
4947c478bd9Sstevel@tonic-gate 
495f4b3ec61Sdh 	buf = NULL;
4967c478bd9Sstevel@tonic-gate 	/*
4977c478bd9Sstevel@tonic-gate 	 * Special case:
4987c478bd9Sstevel@tonic-gate 	 * ifconfig -a plumb should find all network interfaces
499d62bc4baSyz 	 * in the machine for the global zone.
500f4b3ec61Sdh 	 * For non-global zones, only find the assigned interfaces.
5017c478bd9Sstevel@tonic-gate 	 * Also, there is no need to  SIOCGLIF* ioctls, since
5027c478bd9Sstevel@tonic-gate 	 * those interfaces have already been plumbed
5037c478bd9Sstevel@tonic-gate 	 */
5047c478bd9Sstevel@tonic-gate 	if (argc > 0 && (strcmp(*argv, "plumb") == 0)) {
505f4b3ec61Sdh 		if (getzoneid() == GLOBAL_ZONEID) {
506f4b3ec61Sdh 			if (find_all_global_interfaces(&lifc, &buf,
507f4b3ec61Sdh 			    lifc_flags) != 0)
508f4b3ec61Sdh 				return;
509f4b3ec61Sdh 		} else {
510f4b3ec61Sdh 			if (find_all_zone_interfaces(&lifc, &buf,
511f4b3ec61Sdh 			    lifc_flags) != 0)
512f4b3ec61Sdh 				return;
5137c478bd9Sstevel@tonic-gate 		}
514f4b3ec61Sdh 		if (lifc.lifc_len == 0)
515f4b3ec61Sdh 			return;
5167c478bd9Sstevel@tonic-gate 		plumball = 1;
5177c478bd9Sstevel@tonic-gate 	} else {
5187c478bd9Sstevel@tonic-gate 		lifn.lifn_family = AF_UNSPEC;
5197c478bd9Sstevel@tonic-gate 		lifn.lifn_flags = lifc_flags;
5207c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
5217c478bd9Sstevel@tonic-gate 			Perror0_exit("Could not determine number"
5227c478bd9Sstevel@tonic-gate 			    " of interfaces");
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 		numifs = lifn.lifn_count;
5257c478bd9Sstevel@tonic-gate 		if (debug)
5267c478bd9Sstevel@tonic-gate 			(void) printf("ifconfig: %d interfaces\n",  numifs);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 		bufsize = numifs * sizeof (struct lifreq);
5297c478bd9Sstevel@tonic-gate 		if ((buf = malloc(bufsize)) == NULL) {
5307c478bd9Sstevel@tonic-gate 			Perror0("out of memory\n");
5317c478bd9Sstevel@tonic-gate 			(void) close(s);
5327c478bd9Sstevel@tonic-gate 			return;
5337c478bd9Sstevel@tonic-gate 		}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 		lifc.lifc_family = AF_UNSPEC;
5367c478bd9Sstevel@tonic-gate 		lifc.lifc_flags = lifc_flags;
5377c478bd9Sstevel@tonic-gate 		lifc.lifc_len = bufsize;
5387c478bd9Sstevel@tonic-gate 		lifc.lifc_buf = buf;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
5417c478bd9Sstevel@tonic-gate 			Perror0("SIOCGLIFCONF");
5427c478bd9Sstevel@tonic-gate 			(void) close(s);
5437c478bd9Sstevel@tonic-gate 			free(buf);
5447c478bd9Sstevel@tonic-gate 			return;
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	lifrp = lifc.lifc_req;
5497c478bd9Sstevel@tonic-gate 	for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) {
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 		if (!plumball) {
5527c478bd9Sstevel@tonic-gate 			/*
5537c478bd9Sstevel@tonic-gate 			 * We must close and recreate the socket each time
5547c478bd9Sstevel@tonic-gate 			 * since we don't know what type of socket it is now
5557c478bd9Sstevel@tonic-gate 			 * (each status function may change it).
5567c478bd9Sstevel@tonic-gate 			 */
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 			(void) close(s);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 			af = lifrp->lifr_addr.ss_family;
5617c478bd9Sstevel@tonic-gate 			s = socket(SOCKET_AF(af), SOCK_DGRAM, 0);
5627c478bd9Sstevel@tonic-gate 			if (s == -1) {
5637c478bd9Sstevel@tonic-gate 				/*
5647c478bd9Sstevel@tonic-gate 				 * Perror0() assumes the name to be in the
5657c478bd9Sstevel@tonic-gate 				 * globally defined lifreq structure.
5667c478bd9Sstevel@tonic-gate 				 */
5677c478bd9Sstevel@tonic-gate 				(void) strncpy(lifr.lifr_name,
5687c478bd9Sstevel@tonic-gate 				    lifrp->lifr_name, sizeof (lifr.lifr_name));
5697c478bd9Sstevel@tonic-gate 				Perror0_exit("socket");
5707c478bd9Sstevel@tonic-gate 			}
5717c478bd9Sstevel@tonic-gate 		}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 		/*
5747c478bd9Sstevel@tonic-gate 		 * Only service interfaces that match the on and off
5757c478bd9Sstevel@tonic-gate 		 * flags masks.
5767c478bd9Sstevel@tonic-gate 		 */
5777c478bd9Sstevel@tonic-gate 		if (onflags || offflags) {
5787c478bd9Sstevel@tonic-gate 			(void) memset(&lifrl, 0, sizeof (lifrl));
5797c478bd9Sstevel@tonic-gate 			(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
580fc80c0dfSnordmark 			    sizeof (lifrl.lifr_name));
5817c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifrl) < 0) {
5827c478bd9Sstevel@tonic-gate 				/*
5837c478bd9Sstevel@tonic-gate 				 * Perror0() assumes the name to be in the
5847c478bd9Sstevel@tonic-gate 				 * globally defined lifreq structure.
5857c478bd9Sstevel@tonic-gate 				 */
5867c478bd9Sstevel@tonic-gate 				(void) strncpy(lifr.lifr_name,
5877c478bd9Sstevel@tonic-gate 				    lifrp->lifr_name, sizeof (lifr.lifr_name));
5887c478bd9Sstevel@tonic-gate 				Perror0_exit("foreachinterface: SIOCGLIFFLAGS");
5897c478bd9Sstevel@tonic-gate 			}
5907c478bd9Sstevel@tonic-gate 			if ((lifrl.lifr_flags & onflags) != onflags)
5917c478bd9Sstevel@tonic-gate 				continue;
5927c478bd9Sstevel@tonic-gate 			if ((~lifrl.lifr_flags & offflags) != offflags)
5937c478bd9Sstevel@tonic-gate 				continue;
5947c478bd9Sstevel@tonic-gate 		}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		if (!plumball) {
5977c478bd9Sstevel@tonic-gate 			(void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
5987c478bd9Sstevel@tonic-gate 			    sizeof (lifrl.lifr_name));
5997c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifrl) < 0) {
6007c478bd9Sstevel@tonic-gate 				/*
6017c478bd9Sstevel@tonic-gate 				 * Perror0() assumes the name to be in the
6027c478bd9Sstevel@tonic-gate 				 * globally defined lifreq structure.
6037c478bd9Sstevel@tonic-gate 				 */
6047c478bd9Sstevel@tonic-gate 				(void) strncpy(lifr.lifr_name,
6057c478bd9Sstevel@tonic-gate 				    lifrp->lifr_name, sizeof (lifr.lifr_name));
6067c478bd9Sstevel@tonic-gate 				Perror0("foreachinterface: SIOCGLIFADDR");
6077c478bd9Sstevel@tonic-gate 				continue;
6087c478bd9Sstevel@tonic-gate 			}
6097c478bd9Sstevel@tonic-gate 			if (lifrl.lifr_addr.ss_family != af) {
6107c478bd9Sstevel@tonic-gate 				/* Switch address family */
6117c478bd9Sstevel@tonic-gate 				af = lifrl.lifr_addr.ss_family;
6127c478bd9Sstevel@tonic-gate 				(void) close(s);
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 				s = socket(SOCKET_AF(af), SOCK_DGRAM, 0);
6157c478bd9Sstevel@tonic-gate 				if (s == -1) {
6167c478bd9Sstevel@tonic-gate 					/*
6177c478bd9Sstevel@tonic-gate 					 * Perror0() assumes the name to be in
6187c478bd9Sstevel@tonic-gate 					 * the globally defined lifreq
6197c478bd9Sstevel@tonic-gate 					 * structure.
6207c478bd9Sstevel@tonic-gate 					 */
6217c478bd9Sstevel@tonic-gate 					(void) strncpy(lifr.lifr_name,
6227c478bd9Sstevel@tonic-gate 					    lifrp->lifr_name,
6237c478bd9Sstevel@tonic-gate 					    sizeof (lifr.lifr_name));
6247c478bd9Sstevel@tonic-gate 					Perror0_exit("socket");
6257c478bd9Sstevel@tonic-gate 				}
6267c478bd9Sstevel@tonic-gate 			}
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 		/*
6307c478bd9Sstevel@tonic-gate 		 * Reset global state
6317c478bd9Sstevel@tonic-gate 		 * setaddr: Used by parser to tear apart source and dest
6327c478bd9Sstevel@tonic-gate 		 * name and origname contain the name of the 'current'
6337c478bd9Sstevel@tonic-gate 		 * interface.
6347c478bd9Sstevel@tonic-gate 		 */
6357c478bd9Sstevel@tonic-gate 		setaddr = 0;
6367c478bd9Sstevel@tonic-gate 		(void) strncpy(name, lifrp->lifr_name, sizeof (name));
6377c478bd9Sstevel@tonic-gate 		(void) strncpy(origname, name, sizeof (origname));
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		(*func)(argc, argv, save_af, lifrp);
6407c478bd9Sstevel@tonic-gate 		/* the func could have overwritten origname, so restore */
6417c478bd9Sstevel@tonic-gate 		(void) strncpy(name, origname, sizeof (name));
6427c478bd9Sstevel@tonic-gate 	}
643f4b3ec61Sdh 	if (buf != NULL)
644f4b3ec61Sdh 		free(buf);
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate static void
6487c478bd9Sstevel@tonic-gate tun_reality_check(void)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	struct iftun_req treq;
6517c478bd9Sstevel@tonic-gate 	ipsec_req_t *ipsr;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	(void) strncpy(treq.ifta_lifr_name, name, sizeof (treq.ifta_lifr_name));
6547c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
6557c478bd9Sstevel@tonic-gate 		/* Return, we don't need to check. */
6567c478bd9Sstevel@tonic-gate 		return;
6577c478bd9Sstevel@tonic-gate 	}
6587c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGTUNPARAM, (caddr_t)&treq) < 0 ||
6597c478bd9Sstevel@tonic-gate 	    (treq.ifta_flags & IFTUN_SECURITY) == 0) {
6607c478bd9Sstevel@tonic-gate 		/*
6617c478bd9Sstevel@tonic-gate 		 * Either not a tunnel (the SIOCGTUNPARAM fails on
6627c478bd9Sstevel@tonic-gate 		 * non-tunnels), or the security flag is not set.  Either
6637c478bd9Sstevel@tonic-gate 		 * way, return.
6647c478bd9Sstevel@tonic-gate 		 */
6657c478bd9Sstevel@tonic-gate 		return;
6667c478bd9Sstevel@tonic-gate 	}
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	ipsr = (ipsec_req_t *)&treq.ifta_secinfo;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	if (ipsr->ipsr_esp_req != 0 &&
6717c478bd9Sstevel@tonic-gate 	    ipsr->ipsr_esp_auth_alg == SADB_AALG_NONE &&
6727c478bd9Sstevel@tonic-gate 	    ipsr->ipsr_ah_req == 0)
6737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: WARNING - tunnel with "
674*d2f8a3dfSpwernau 		    "only ESP and no authentication.\n");
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate /*
6787c478bd9Sstevel@tonic-gate  * for the specified interface call (*func)(argc, argv, af, lifrp).
6797c478bd9Sstevel@tonic-gate  */
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate static void
6827c478bd9Sstevel@tonic-gate ifconfig(int argc, char *argv[], int af, struct lifreq *lifrp)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	static boolean_t scan_netmask = _B_FALSE;
6857c478bd9Sstevel@tonic-gate 	int ret;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	if (argc == 0) {
6887c478bd9Sstevel@tonic-gate 		status();
6897c478bd9Sstevel@tonic-gate 		return;
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if (strcmp(*argv, "auto-dhcp") == 0 || strcmp(*argv, "dhcp") == 0) {
693d04ccbb3Scarlsonj 		/*
694d04ccbb3Scarlsonj 		 * Some errors are ignored in the case where more than one
695d04ccbb3Scarlsonj 		 * interface is being operated on.
696d04ccbb3Scarlsonj 		 */
697d04ccbb3Scarlsonj 		ret = setifdhcp("ifconfig", name, argc, argv);
698d04ccbb3Scarlsonj 		if (ret == DHCP_EXIT_IF_FAILURE) {
699d04ccbb3Scarlsonj 			if (!all)
700d04ccbb3Scarlsonj 				exit(DHCP_EXIT_FAILURE);
701d04ccbb3Scarlsonj 		} else if (ret != DHCP_EXIT_SUCCESS) {
702d04ccbb3Scarlsonj 			exit(ret);
703d04ccbb3Scarlsonj 		}
7047c478bd9Sstevel@tonic-gate 		return;
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	/*
7087c478bd9Sstevel@tonic-gate 	 * The following is a "hack" to get around the existing interface
7097c478bd9Sstevel@tonic-gate 	 * setting mechanism.  Currently, each interface attribute,
7107c478bd9Sstevel@tonic-gate 	 * such as address, netmask, broadcast, ... is set separately.  But
7117c478bd9Sstevel@tonic-gate 	 * sometimes two or more attributes must be set together.  For
7127c478bd9Sstevel@tonic-gate 	 * example, setting an address without a netmask does not make sense.
7137c478bd9Sstevel@tonic-gate 	 * Yet they can be set separately for IPv4 address using the current
7147c478bd9Sstevel@tonic-gate 	 * ifconfig(1M) syntax.  The kernel then "infers" the correct netmask
7157c478bd9Sstevel@tonic-gate 	 * using the deprecated "IP address classes."  This is simply not
7167c478bd9Sstevel@tonic-gate 	 * correct.
7177c478bd9Sstevel@tonic-gate 	 *
7187c478bd9Sstevel@tonic-gate 	 * The "hack" below is to go thru the whole command list looking for
7197c478bd9Sstevel@tonic-gate 	 * the netmask command first.  Then use this netmask to set the
7207c478bd9Sstevel@tonic-gate 	 * address.  This does not provide an extensible way to accommodate
7217c478bd9Sstevel@tonic-gate 	 * future need for setting more than one attributes together.
7227c478bd9Sstevel@tonic-gate 	 *
723dd7a6f5fSkcpoon 	 * Note that if the "netmask" command argument is a "+", we need
724dd7a6f5fSkcpoon 	 * to save this info and do the query after we know the address to
725dd7a6f5fSkcpoon 	 * be set.  The reason is that if "addif" is used, the working
726dd7a6f5fSkcpoon 	 * interface name will be changed later when the logical interface
727dd7a6f5fSkcpoon 	 * is created.  In in_getmask(), if an address is not provided,
728dd7a6f5fSkcpoon 	 * it will use the working interface's address to do the query.
729dd7a6f5fSkcpoon 	 * It will be wrong now as we don't know the logical interface's name.
730dd7a6f5fSkcpoon 	 *
7317c478bd9Sstevel@tonic-gate 	 * ifconfig(1M) is too overloaded and the code is so convoluted
7327c478bd9Sstevel@tonic-gate 	 * that it is "safer" not to re-architect the code to fix the above
7337c478bd9Sstevel@tonic-gate 	 * issue, hence this "hack."  We may be better off to have a new
7347c478bd9Sstevel@tonic-gate 	 * command with better syntax for configuring network interface
7357c478bd9Sstevel@tonic-gate 	 * parameters...
7367c478bd9Sstevel@tonic-gate 	 */
7377c478bd9Sstevel@tonic-gate 	if (!scan_netmask && afp->af_af == AF_INET) {
7387c478bd9Sstevel@tonic-gate 		int	largc;
7397c478bd9Sstevel@tonic-gate 		char	**largv;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		/* Only go thru the command list once to find the netmask. */
7427c478bd9Sstevel@tonic-gate 		scan_netmask = _B_TRUE;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		/*
7457c478bd9Sstevel@tonic-gate 		 * Currently, if multiple netmask commands are specified, the
7467c478bd9Sstevel@tonic-gate 		 * last one will be used as the final netmask.  So we need
7477c478bd9Sstevel@tonic-gate 		 * to scan the whole list to preserve this behavior.
7487c478bd9Sstevel@tonic-gate 		 */
7497c478bd9Sstevel@tonic-gate 		for (largc = argc, largv = argv; largc > 0; largc--, largv++) {
7507c478bd9Sstevel@tonic-gate 			if (strcmp(*largv, NETMASK_CMD) == 0) {
7517c478bd9Sstevel@tonic-gate 				if (--largc == 0)
7527c478bd9Sstevel@tonic-gate 					break;
7537c478bd9Sstevel@tonic-gate 				largv++;
7547c478bd9Sstevel@tonic-gate 				if (strcmp(*largv, "+") == 0) {
755dd7a6f5fSkcpoon 					g_netmask_set = G_NETMASK_PENDING;
7567c478bd9Sstevel@tonic-gate 				} else {
7577c478bd9Sstevel@tonic-gate 					in_getaddr(*largv, (struct sockaddr *)
7587c478bd9Sstevel@tonic-gate 					    &g_netmask, NULL);
759dd7a6f5fSkcpoon 					g_netmask_set = G_NETMASK_SET;
7607c478bd9Sstevel@tonic-gate 				}
7617c478bd9Sstevel@tonic-gate 				/* Continue the scan. */
7627c478bd9Sstevel@tonic-gate 			}
7637c478bd9Sstevel@tonic-gate 		}
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	while (argc > 0) {
7677c478bd9Sstevel@tonic-gate 		struct cmd *p;
7687c478bd9Sstevel@tonic-gate 		boolean_t found_cmd;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 		if (debug)
7717c478bd9Sstevel@tonic-gate 			(void) printf("ifconfig: argv %s\n", *argv);
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 		found_cmd = _B_FALSE;
7747c478bd9Sstevel@tonic-gate 		for (p = cmds; p->c_func; p++) {
7757c478bd9Sstevel@tonic-gate 			if (p->c_name) {
7767c478bd9Sstevel@tonic-gate 				if (strcmp(*argv, p->c_name) == 0) {
7777c478bd9Sstevel@tonic-gate 					/*
7787c478bd9Sstevel@tonic-gate 					 * indicate that the command was
7797c478bd9Sstevel@tonic-gate 					 * found and check to see if
7807c478bd9Sstevel@tonic-gate 					 * the address family is valid
7817c478bd9Sstevel@tonic-gate 					 */
7827c478bd9Sstevel@tonic-gate 					found_cmd = _B_TRUE;
7837c478bd9Sstevel@tonic-gate 					if (p->c_af == AF_ANY ||
7847c478bd9Sstevel@tonic-gate 					    af == p->c_af)
7857c478bd9Sstevel@tonic-gate 						break;
7867c478bd9Sstevel@tonic-gate 				}
7877c478bd9Sstevel@tonic-gate 			} else {
7887c478bd9Sstevel@tonic-gate 				if (p->c_af == AF_ANY ||
7897c478bd9Sstevel@tonic-gate 				    af == p->c_af)
7907c478bd9Sstevel@tonic-gate 					break;
7917c478bd9Sstevel@tonic-gate 			}
7927c478bd9Sstevel@tonic-gate 		}
7937c478bd9Sstevel@tonic-gate 		/*
7947c478bd9Sstevel@tonic-gate 		 * If we found the keyword, but the address family
7957c478bd9Sstevel@tonic-gate 		 * did not match spit out an error
7967c478bd9Sstevel@tonic-gate 		 */
7977c478bd9Sstevel@tonic-gate 		if (found_cmd && p->c_name == 0) {
7987c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: Operation %s not"
7997c478bd9Sstevel@tonic-gate 			    " supported for %s\n", *argv, afp->af_name);
8007c478bd9Sstevel@tonic-gate 			exit(1);
8017c478bd9Sstevel@tonic-gate 		}
8027c478bd9Sstevel@tonic-gate 		/*
8037c478bd9Sstevel@tonic-gate 		 * else (no keyword found), we assume it's an address
8047c478bd9Sstevel@tonic-gate 		 * of some sort
8057c478bd9Sstevel@tonic-gate 		 */
8067c478bd9Sstevel@tonic-gate 		if (p->c_name == 0 && setaddr)
8077c478bd9Sstevel@tonic-gate 			p++;	/* got src, do dst */
8087c478bd9Sstevel@tonic-gate 		if (p->c_func) {
8097c478bd9Sstevel@tonic-gate 			if (p->c_af == AF_INET6) {
8107c478bd9Sstevel@tonic-gate 				v4compat = 0;
8117c478bd9Sstevel@tonic-gate 			}
8127c478bd9Sstevel@tonic-gate 			if (p->c_parameter == NEXTARG ||
8137c478bd9Sstevel@tonic-gate 			    p->c_parameter == OPTARG) {
8147c478bd9Sstevel@tonic-gate 				argc--, argv++;
8157c478bd9Sstevel@tonic-gate 				if (argc == 0 && p->c_parameter == NEXTARG) {
8167c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
8177c478bd9Sstevel@tonic-gate 					    "ifconfig: no argument for %s\n",
8187c478bd9Sstevel@tonic-gate 					    p->c_name);
8197c478bd9Sstevel@tonic-gate 					exit(1);
8207c478bd9Sstevel@tonic-gate 				}
8217c478bd9Sstevel@tonic-gate 			}
8227c478bd9Sstevel@tonic-gate 			/*
8237c478bd9Sstevel@tonic-gate 			 *	Call the function if:
8247c478bd9Sstevel@tonic-gate 			 *
8257c478bd9Sstevel@tonic-gate 			 *		there's no address family
8267c478bd9Sstevel@tonic-gate 			 *		restriction
8277c478bd9Sstevel@tonic-gate 			 *	OR
8287c478bd9Sstevel@tonic-gate 			 *		we don't know the address yet
8297c478bd9Sstevel@tonic-gate 			 *		(because we were called from
8307c478bd9Sstevel@tonic-gate 			 *		main)
8317c478bd9Sstevel@tonic-gate 			 *	OR
8327c478bd9Sstevel@tonic-gate 			 *		there is a restriction AND
8337c478bd9Sstevel@tonic-gate 			 *		the address families match
8347c478bd9Sstevel@tonic-gate 			 */
8357c478bd9Sstevel@tonic-gate 			if ((p->c_af == AF_ANY)	||
8367c478bd9Sstevel@tonic-gate 			    (lifrp == (struct lifreq *)NULL) ||
8377c478bd9Sstevel@tonic-gate 			    (lifrp->lifr_addr.ss_family == p->c_af)) {
8387c478bd9Sstevel@tonic-gate 				ret = (*p->c_func)(*argv, p->c_parameter);
8397c478bd9Sstevel@tonic-gate 				/*
8407c478bd9Sstevel@tonic-gate 				 *	If c_func failed and we should
8417c478bd9Sstevel@tonic-gate 				 *	abort processing for this
8427c478bd9Sstevel@tonic-gate 				 *	interface on failure, return
8437c478bd9Sstevel@tonic-gate 				 *	now rather than going on to
8447c478bd9Sstevel@tonic-gate 				 *	process other commands for
8457c478bd9Sstevel@tonic-gate 				 *	the same interface.
8467c478bd9Sstevel@tonic-gate 				 */
8477c478bd9Sstevel@tonic-gate 				if (ret != 0 && p->c_abortonfail)
8487c478bd9Sstevel@tonic-gate 					return;
8497c478bd9Sstevel@tonic-gate 			}
8507c478bd9Sstevel@tonic-gate 		}
8517c478bd9Sstevel@tonic-gate 		argc--, argv++;
8527c478bd9Sstevel@tonic-gate 	}
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	/* Check to see if there's a security hole in the tunnel setup. */
8557c478bd9Sstevel@tonic-gate 	tun_reality_check();
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate /* ARGSUSED */
8597c478bd9Sstevel@tonic-gate static int
8607c478bd9Sstevel@tonic-gate setdebugflag(char *val, int64_t arg)
8617c478bd9Sstevel@tonic-gate {
8627c478bd9Sstevel@tonic-gate 	debug++;
8637c478bd9Sstevel@tonic-gate 	return (0);
8647c478bd9Sstevel@tonic-gate }
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate /* ARGSUSED */
8677c478bd9Sstevel@tonic-gate static int
8687c478bd9Sstevel@tonic-gate setverboseflag(char *val, int64_t arg)
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate 	verbose++;
8717c478bd9Sstevel@tonic-gate 	return (0);
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate 
874dd7a6f5fSkcpoon /*
875dd7a6f5fSkcpoon  * This function fills in the given lifreq's lifr_addr field based on
876dd7a6f5fSkcpoon  * g_netmask_set.
877dd7a6f5fSkcpoon  */
878dd7a6f5fSkcpoon static void
879dd7a6f5fSkcpoon set_mask_lifreq(struct lifreq *lifr, struct sockaddr_storage *addr,
880dd7a6f5fSkcpoon     struct sockaddr_storage *mask)
881dd7a6f5fSkcpoon {
882dd7a6f5fSkcpoon 	assert(addr != NULL);
883dd7a6f5fSkcpoon 	assert(mask != NULL);
884dd7a6f5fSkcpoon 
885dd7a6f5fSkcpoon 	switch (g_netmask_set) {
886dd7a6f5fSkcpoon 	case G_NETMASK_SET:
887dd7a6f5fSkcpoon 		lifr->lifr_addr = g_netmask;
888dd7a6f5fSkcpoon 		break;
889dd7a6f5fSkcpoon 
890dd7a6f5fSkcpoon 	case G_NETMASK_PENDING:
891dd7a6f5fSkcpoon 		/*
892dd7a6f5fSkcpoon 		 * "+" is used as the argument to "netmask" command.  Query
893dd7a6f5fSkcpoon 		 * the database on the correct netmask based on the address to
894dd7a6f5fSkcpoon 		 * be set.
895dd7a6f5fSkcpoon 		 */
896dd7a6f5fSkcpoon 		assert(afp->af_af == AF_INET);
897dd7a6f5fSkcpoon 		g_netmask = *addr;
898dd7a6f5fSkcpoon 		if (!in_getmask((struct sockaddr_in *)&g_netmask, _B_TRUE)) {
899dd7a6f5fSkcpoon 			lifr->lifr_addr = *mask;
900dd7a6f5fSkcpoon 			g_netmask_set = G_NETMASK_NIL;
901dd7a6f5fSkcpoon 		} else {
902dd7a6f5fSkcpoon 			lifr->lifr_addr = g_netmask;
903dd7a6f5fSkcpoon 			g_netmask_set = G_NETMASK_SET;
904dd7a6f5fSkcpoon 		}
905dd7a6f5fSkcpoon 		break;
906dd7a6f5fSkcpoon 
907dd7a6f5fSkcpoon 	case G_NETMASK_NIL:
908dd7a6f5fSkcpoon 	default:
909dd7a6f5fSkcpoon 		lifr->lifr_addr = *mask;
910dd7a6f5fSkcpoon 		break;
911dd7a6f5fSkcpoon 	}
912dd7a6f5fSkcpoon }
913dd7a6f5fSkcpoon 
9147c478bd9Sstevel@tonic-gate /*
9157c478bd9Sstevel@tonic-gate  * Set the interface address. Handles <addr>, <addr>/<n> as well as /<n>
9167c478bd9Sstevel@tonic-gate  * syntax for setting the address, the address plus netmask, and just
9177c478bd9Sstevel@tonic-gate  * the netmask respectively.
9187c478bd9Sstevel@tonic-gate  */
9197c478bd9Sstevel@tonic-gate /* ARGSUSED */
9207c478bd9Sstevel@tonic-gate static int
9217c478bd9Sstevel@tonic-gate setifaddr(char *addr, int64_t param)
9227c478bd9Sstevel@tonic-gate {
9237c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
9247c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage laddr;
9257c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage netmask;
9267c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 *sin6;
9277c478bd9Sstevel@tonic-gate 	struct	sockaddr_in *sin;
9287c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage sav_netmask;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	if (addr[0] == '/')
9317c478bd9Sstevel@tonic-gate 		return (setifprefixlen(addr, 0));
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, (struct sockaddr *)&laddr, &prefixlen);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	(void) memset(&netmask, 0, sizeof (netmask));
9367c478bd9Sstevel@tonic-gate 	netmask.ss_family = afp->af_af;
9377c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
9387c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
9397c478bd9Sstevel@tonic-gate 		/* Nothing there - ok */
9407c478bd9Sstevel@tonic-gate 		break;
9417c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
9427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: Bad prefix length in %s\n",
9437c478bd9Sstevel@tonic-gate 		    addr);
9447c478bd9Sstevel@tonic-gate 		exit(1);
9457c478bd9Sstevel@tonic-gate 	default:
9467c478bd9Sstevel@tonic-gate 		if (afp->af_af == AF_INET6) {
9477c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&netmask;
9487906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
9497c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin6->sin6_addr)) {
9507c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
9517c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
9527c478bd9Sstevel@tonic-gate 				    prefixlen);
9537c478bd9Sstevel@tonic-gate 				exit(1);
9547c478bd9Sstevel@tonic-gate 			}
9557c478bd9Sstevel@tonic-gate 		} else {
9567c478bd9Sstevel@tonic-gate 			sin = (struct sockaddr_in *)&netmask;
9577906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IP_ABITS,
9587c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin->sin_addr)) {
9597c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
9607c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
9617c478bd9Sstevel@tonic-gate 				    prefixlen);
9627c478bd9Sstevel@tonic-gate 				exit(1);
9637c478bd9Sstevel@tonic-gate 			}
9647c478bd9Sstevel@tonic-gate 		}
9657c478bd9Sstevel@tonic-gate 		/*
9667c478bd9Sstevel@tonic-gate 		 * Just in case of funny setting of both prefix and netmask,
9677c478bd9Sstevel@tonic-gate 		 * prefix should override the netmask command.
9687c478bd9Sstevel@tonic-gate 		 */
969dd7a6f5fSkcpoon 		g_netmask_set = G_NETMASK_NIL;
9707c478bd9Sstevel@tonic-gate 		break;
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 	/* Tell parser that an address was set */
9737c478bd9Sstevel@tonic-gate 	setaddr++;
9747c478bd9Sstevel@tonic-gate 	/* save copy of netmask to restore in case of error */
9757c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
9767c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0)
9777c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCGLIFNETMASK");
9787c478bd9Sstevel@tonic-gate 	sav_netmask = lifr.lifr_addr;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	/*
9817c478bd9Sstevel@tonic-gate 	 * If setting the address and not the mask, clear any existing mask
9827c478bd9Sstevel@tonic-gate 	 * and the kernel will then assign the default (netmask has been set
9837c478bd9Sstevel@tonic-gate 	 * to 0 in this case).  If setting both (either by using a prefix or
9847c478bd9Sstevel@tonic-gate 	 * using the netmask command), set the mask first, so the address will
9857c478bd9Sstevel@tonic-gate 	 * be interpreted correctly.
9867c478bd9Sstevel@tonic-gate 	 */
987dd7a6f5fSkcpoon 	set_mask_lifreq(&lifr, &laddr, &netmask);
9887c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
9897c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	if (debug) {
9927c478bd9Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
9937c478bd9Sstevel@tonic-gate 		void *addr = (afp->af_af == AF_INET) ?
9947c478bd9Sstevel@tonic-gate 		    (void *)&((struct sockaddr_in *)&laddr)->sin_addr :
9957c478bd9Sstevel@tonic-gate 		    (void *)&((struct sockaddr_in6 *)&laddr)->sin6_addr;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 		(void) printf("Setting %s af %d addr %s\n",
9987c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, afp->af_af,
9997c478bd9Sstevel@tonic-gate 		    inet_ntop(afp->af_af, addr, abuf, sizeof (abuf)));
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = laddr;
10027c478bd9Sstevel@tonic-gate 	lifr.lifr_addr.ss_family = afp->af_af;
10037c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
10047c478bd9Sstevel@tonic-gate 		/*
10057c478bd9Sstevel@tonic-gate 		 * Restore the netmask
10067c478bd9Sstevel@tonic-gate 		 */
10077c478bd9Sstevel@tonic-gate 		int saverr = errno;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
10107c478bd9Sstevel@tonic-gate 		lifr.lifr_addr = sav_netmask;
10117c478bd9Sstevel@tonic-gate 		(void) ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr);
10127c478bd9Sstevel@tonic-gate 		errno = saverr;
10137c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFADDR");
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	return (0);
10177c478bd9Sstevel@tonic-gate }
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate /*
10207c478bd9Sstevel@tonic-gate  * The following functions are stolen from the ipseckey(1m) program.
10217c478bd9Sstevel@tonic-gate  * Perhaps they should be somewhere common, but for now, we just maintain
10227c478bd9Sstevel@tonic-gate  * two versions.  We do this because of the different semantics for which
10237c478bd9Sstevel@tonic-gate  * algorithms we select ("requested" for ifconfig vs. "actual" for key).
10247c478bd9Sstevel@tonic-gate  */
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate static ulong_t
10277c478bd9Sstevel@tonic-gate parsenum(char *num)
10287c478bd9Sstevel@tonic-gate {
10297c478bd9Sstevel@tonic-gate 	ulong_t rc;
10307c478bd9Sstevel@tonic-gate 	char *end = NULL;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	errno = 0;
10337c478bd9Sstevel@tonic-gate 	rc = strtoul(num, &end, 0);
10347c478bd9Sstevel@tonic-gate 	if (errno != 0 || end == num || *end != '\0') {
10357c478bd9Sstevel@tonic-gate 		rc = (ulong_t)-1;
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	return (rc);
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate /*
10427c478bd9Sstevel@tonic-gate  * Parse and reverse parse possible algorithm values, include numbers.
10437c478bd9Sstevel@tonic-gate  * Mostly stolen from ipseckey.c. See the comments above parsenum() for why
10447c478bd9Sstevel@tonic-gate  * this isn't common to ipseckey.c.
10457c478bd9Sstevel@tonic-gate  *
10467c478bd9Sstevel@tonic-gate  * NOTE: Static buffer in this function for the return value.  Since ifconfig
10477c478bd9Sstevel@tonic-gate  *	 isn't multithreaded, this isn't a huge problem.
10487c478bd9Sstevel@tonic-gate  */
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate #define	NBUF_SIZE 20	/* Enough to print a large integer. */
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate static char *
10537c478bd9Sstevel@tonic-gate rparsealg(uint8_t alg_value, int proto_num)
10547c478bd9Sstevel@tonic-gate {
10557c478bd9Sstevel@tonic-gate 	struct ipsecalgent *alg;
10567c478bd9Sstevel@tonic-gate 	static char numprint[128];	/* Enough to hold an algorithm name. */
10577c478bd9Sstevel@tonic-gate 
1058*d2f8a3dfSpwernau 	/*
1059*d2f8a3dfSpwernau 	 * Special cases for "any" and "none"
1060*d2f8a3dfSpwernau 	 * The kernel needs to be able to distinguish between "any"
1061*d2f8a3dfSpwernau 	 * and "none" and the APIs are underdefined in this area for auth.
1062*d2f8a3dfSpwernau 	 */
1063*d2f8a3dfSpwernau 	if (proto_num == IPSEC_PROTO_AH) {
1064*d2f8a3dfSpwernau 		if (alg_value == SADB_AALG_NONE)
1065*d2f8a3dfSpwernau 			return ("none");
1066*d2f8a3dfSpwernau 		if (alg_value == SADB_AALG_ANY)
1067*d2f8a3dfSpwernau 			return ("any");
1068*d2f8a3dfSpwernau 	}
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	alg = getipsecalgbynum(alg_value, proto_num, NULL);
10717c478bd9Sstevel@tonic-gate 	if (alg != NULL) {
10727c478bd9Sstevel@tonic-gate 		(void) strlcpy(numprint, alg->a_names[0], sizeof (numprint));
10737c478bd9Sstevel@tonic-gate 		freeipsecalgent(alg);
10747c478bd9Sstevel@tonic-gate 	} else {
10757c478bd9Sstevel@tonic-gate 		(void) snprintf(numprint, sizeof (numprint), "%d", alg_value);
10767c478bd9Sstevel@tonic-gate 	}
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	return (numprint);
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate static uint_t
10827c478bd9Sstevel@tonic-gate parsealg(char *algname, int proto_num)
10837c478bd9Sstevel@tonic-gate {
10847c478bd9Sstevel@tonic-gate 	struct ipsecalgent *alg;
10857c478bd9Sstevel@tonic-gate 	ulong_t invalue;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	if (algname == NULL) {
10887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: Unexpected end of command "
10897c478bd9Sstevel@tonic-gate 		    "line.\n");
10907c478bd9Sstevel@tonic-gate 		exit(1);
10917c478bd9Sstevel@tonic-gate 	}
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	/*
1094*d2f8a3dfSpwernau 	 * Special-case "none" and "any".
1095*d2f8a3dfSpwernau 	 * Use strcasecmp because its length is bounded.
10967c478bd9Sstevel@tonic-gate 	 */
10977c478bd9Sstevel@tonic-gate 	if (strcasecmp("none", algname) == 0) {
10987c478bd9Sstevel@tonic-gate 		return ((proto_num == IPSEC_PROTO_ESP) ?
10997c478bd9Sstevel@tonic-gate 		    NO_ESP_EALG : NO_ESP_AALG);
11007c478bd9Sstevel@tonic-gate 	}
1101*d2f8a3dfSpwernau 	if ((strcasecmp("any", algname) == 0) && (proto_num == IPSEC_PROTO_AH))
1102*d2f8a3dfSpwernau 		return (SADB_AALG_ANY);
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	alg = getipsecalgbyname(algname, proto_num, NULL);
11057c478bd9Sstevel@tonic-gate 	if (alg != NULL) {
11067c478bd9Sstevel@tonic-gate 		invalue = alg->a_alg_num;
11077c478bd9Sstevel@tonic-gate 		freeipsecalgent(alg);
11087c478bd9Sstevel@tonic-gate 		return ((uint_t)invalue);
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	/*
11127c478bd9Sstevel@tonic-gate 	 * Since algorithms can be loaded during kernel run-time, check for
11137c478bd9Sstevel@tonic-gate 	 * numeric algorithm values too.
11147c478bd9Sstevel@tonic-gate 	 */
11157c478bd9Sstevel@tonic-gate 	invalue = parsenum(algname);
11167c478bd9Sstevel@tonic-gate 	if ((invalue & (ulong_t)0xff) == invalue)
11177c478bd9Sstevel@tonic-gate 		return ((uint_t)invalue);
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "ifconfig: %s algorithm type %s unknown.\n",
11207c478bd9Sstevel@tonic-gate 	    (proto_num == IPSEC_PROTO_ESP) ?
11217c478bd9Sstevel@tonic-gate 	    "Encryption" : "Authentication", algname);
11227c478bd9Sstevel@tonic-gate 	exit(1);
11237c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate /*
11277c478bd9Sstevel@tonic-gate  * Actual ifconfig functions to set tunnel security properties.
11287c478bd9Sstevel@tonic-gate  */
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate enum ipsec_alg_type { ESP_ENCR_ALG = 1, ESP_AUTH_ALG, AH_AUTH_ALG };
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate boolean_t first_set_tun = _B_TRUE;
11337c478bd9Sstevel@tonic-gate boolean_t encr_alg_set = _B_FALSE;
11347c478bd9Sstevel@tonic-gate 
1135*d2f8a3dfSpwernau /*
1136*d2f8a3dfSpwernau  * Need global for multiple calls to set_tun_algs
1137*d2f8a3dfSpwernau  * because we accumulate algorithm selections over
1138*d2f8a3dfSpwernau  * the lifetime of this ifconfig(1M) invocation.
1139*d2f8a3dfSpwernau  */
1140*d2f8a3dfSpwernau static struct iftun_req treq_tun;
1141*d2f8a3dfSpwernau 
11427c478bd9Sstevel@tonic-gate static int
11437c478bd9Sstevel@tonic-gate set_tun_algs(int which_alg, int alg)
11447c478bd9Sstevel@tonic-gate {
11457c478bd9Sstevel@tonic-gate 	ipsec_req_t *ipsr;
11467c478bd9Sstevel@tonic-gate 
1147*d2f8a3dfSpwernau 	(void) strncpy(treq_tun.ifta_lifr_name, name,
1148*d2f8a3dfSpwernau 	    sizeof (treq_tun.ifta_lifr_name));
11497c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
11507c478bd9Sstevel@tonic-gate 		errno = EPERM;
11517c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
11527c478bd9Sstevel@tonic-gate 	}
1153*d2f8a3dfSpwernau 	if (ioctl(s, SIOCGTUNPARAM, (caddr_t)&treq_tun) < 0) {
11547c478bd9Sstevel@tonic-gate 		if (errno == EOPNOTSUPP || errno == EINVAL)
11557c478bd9Sstevel@tonic-gate 			Perror0_exit("Not a tunnel");
11567c478bd9Sstevel@tonic-gate 		else Perror0_exit("SIOCGTUNPARAM");
11577c478bd9Sstevel@tonic-gate 	}
11587c478bd9Sstevel@tonic-gate 
1159*d2f8a3dfSpwernau 	ipsr = (ipsec_req_t *)&treq_tun.ifta_secinfo;
11607c478bd9Sstevel@tonic-gate 
1161*d2f8a3dfSpwernau 	if (treq_tun.ifta_vers != IFTUN_VERSION) {
11627c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
11637c478bd9Sstevel@tonic-gate 		    "Kernel tunnel secinfo version mismatch.\n");
11647c478bd9Sstevel@tonic-gate 		exit(1);
11657c478bd9Sstevel@tonic-gate 	}
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	/*
11687c478bd9Sstevel@tonic-gate 	 * If I'm just starting off this ifconfig, I want a clean slate,
11697c478bd9Sstevel@tonic-gate 	 * otherwise, I've captured the current tunnel security settings.
11707c478bd9Sstevel@tonic-gate 	 * In the case of continuation, I merely add to the settings.
11717c478bd9Sstevel@tonic-gate 	 */
11727c478bd9Sstevel@tonic-gate 	if (first_set_tun) {
11737c478bd9Sstevel@tonic-gate 		first_set_tun = _B_FALSE;
11747c478bd9Sstevel@tonic-gate 		(void) memset(ipsr, 0, sizeof (*ipsr));
11757c478bd9Sstevel@tonic-gate 	}
11767c478bd9Sstevel@tonic-gate 
1177*d2f8a3dfSpwernau 	treq_tun.ifta_flags = IFTUN_SECURITY;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	switch (which_alg) {
11807c478bd9Sstevel@tonic-gate 	case ESP_ENCR_ALG:
11817c478bd9Sstevel@tonic-gate 		if (alg == NO_ESP_EALG) {
11827c478bd9Sstevel@tonic-gate 			if (ipsr->ipsr_esp_auth_alg == SADB_AALG_NONE)
11837c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_req = 0;
11847c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_alg = SADB_EALG_NONE;
1185*d2f8a3dfSpwernau 
1186*d2f8a3dfSpwernau 			/* Let the user specify NULL encryption implicitly. */
1187*d2f8a3dfSpwernau 			if (ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) {
1188*d2f8a3dfSpwernau 				encr_alg_set = _B_TRUE;
1189*d2f8a3dfSpwernau 				ipsr->ipsr_esp_alg = SADB_EALG_NULL;
1190*d2f8a3dfSpwernau 			}
11917c478bd9Sstevel@tonic-gate 		} else {
11927c478bd9Sstevel@tonic-gate 			encr_alg_set = _B_TRUE;
11937c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_req =
11947c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
11957c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_alg = alg;
11967c478bd9Sstevel@tonic-gate 		}
11977c478bd9Sstevel@tonic-gate 		break;
11987c478bd9Sstevel@tonic-gate 	case ESP_AUTH_ALG:
11997c478bd9Sstevel@tonic-gate 		if (alg == NO_ESP_AALG) {
1200*d2f8a3dfSpwernau 			if ((ipsr->ipsr_esp_alg == SADB_EALG_NONE ||
1201*d2f8a3dfSpwernau 			    ipsr->ipsr_esp_alg == SADB_EALG_NULL) &&
1202*d2f8a3dfSpwernau 			    !encr_alg_set)
12037c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_req = 0;
12047c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_auth_alg = SADB_AALG_NONE;
12057c478bd9Sstevel@tonic-gate 		} else {
12067c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_req =
12077c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12087c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_auth_alg = alg;
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 			/* Let the user specify NULL encryption implicitly. */
12117c478bd9Sstevel@tonic-gate 			if (ipsr->ipsr_esp_alg == SADB_EALG_NONE &&
12127c478bd9Sstevel@tonic-gate 			    !encr_alg_set)
12137c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_alg = SADB_EALG_NULL;
12147c478bd9Sstevel@tonic-gate 		}
12157c478bd9Sstevel@tonic-gate 		break;
12167c478bd9Sstevel@tonic-gate 	case AH_AUTH_ALG:
12177c478bd9Sstevel@tonic-gate 		if (alg == NO_AH_AALG) {
12187c478bd9Sstevel@tonic-gate 			ipsr->ipsr_ah_req = 0;
12197c478bd9Sstevel@tonic-gate 			ipsr->ipsr_auth_alg = SADB_AALG_NONE;
12207c478bd9Sstevel@tonic-gate 		} else {
12217c478bd9Sstevel@tonic-gate 			ipsr->ipsr_ah_req =
12227c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12237c478bd9Sstevel@tonic-gate 			ipsr->ipsr_auth_alg = alg;
12247c478bd9Sstevel@tonic-gate 		}
12257c478bd9Sstevel@tonic-gate 		break;
12267c478bd9Sstevel@tonic-gate 		/* Will never hit DEFAULT */
12277c478bd9Sstevel@tonic-gate 	}
12287c478bd9Sstevel@tonic-gate 
1229*d2f8a3dfSpwernau 	if (ioctl(s, SIOCSTUNPARAM, (caddr_t)&treq_tun) < 0) {
12307c478bd9Sstevel@tonic-gate 		Perror2_exit("set tunnel security properties",
1231*d2f8a3dfSpwernau 		    treq_tun.ifta_lifr_name);
12327c478bd9Sstevel@tonic-gate 	}
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	return (0);
12357c478bd9Sstevel@tonic-gate }
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate /* ARGSUSED */
12387c478bd9Sstevel@tonic-gate static int
12397c478bd9Sstevel@tonic-gate set_tun_esp_encr_alg(char *addr, int64_t param)
12407c478bd9Sstevel@tonic-gate {
12417c478bd9Sstevel@tonic-gate 	return (set_tun_algs(ESP_ENCR_ALG,
1242fc80c0dfSnordmark 	    parsealg(addr, IPSEC_PROTO_ESP)));
12437c478bd9Sstevel@tonic-gate }
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate /* ARGSUSED */
12467c478bd9Sstevel@tonic-gate static int
12477c478bd9Sstevel@tonic-gate set_tun_esp_auth_alg(char *addr, int64_t param)
12487c478bd9Sstevel@tonic-gate {
12497c478bd9Sstevel@tonic-gate 	return (set_tun_algs(ESP_AUTH_ALG,
1250fc80c0dfSnordmark 	    parsealg(addr, IPSEC_PROTO_AH)));
12517c478bd9Sstevel@tonic-gate }
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate /* ARGSUSED */
12547c478bd9Sstevel@tonic-gate static int
12557c478bd9Sstevel@tonic-gate set_tun_ah_alg(char *addr, int64_t param)
12567c478bd9Sstevel@tonic-gate {
12577c478bd9Sstevel@tonic-gate 	return (set_tun_algs(AH_AUTH_ALG,
1258fc80c0dfSnordmark 	    parsealg(addr, IPSEC_PROTO_AH)));
12597c478bd9Sstevel@tonic-gate }
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate /* ARGSUSED */
12627c478bd9Sstevel@tonic-gate static int
12637c478bd9Sstevel@tonic-gate setifrevarp(char *arg, int64_t param)
12647c478bd9Sstevel@tonic-gate {
12657c478bd9Sstevel@tonic-gate 	struct sockaddr_in	laddr;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	if (afp->af_af == AF_INET6) {
12687c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12697c478bd9Sstevel@tonic-gate 		    "ifconfig: revarp not possible on IPv6 interface %s\n",
12707c478bd9Sstevel@tonic-gate 		    name);
12717c478bd9Sstevel@tonic-gate 		exit(1);
12727c478bd9Sstevel@tonic-gate 	}
12737c478bd9Sstevel@tonic-gate 	if (doifrevarp(name, &laddr)) {
12747c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
12757c478bd9Sstevel@tonic-gate 		laddr.sin_family = AF_INET;
12767c478bd9Sstevel@tonic-gate 		(void) memcpy(&lifr.lifr_addr, &laddr, sizeof (laddr));
12777c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
12787c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCSLIFADDR");
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate 	return (0);
12817c478bd9Sstevel@tonic-gate }
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate /* ARGSUSED */
12847c478bd9Sstevel@tonic-gate static int
12857c478bd9Sstevel@tonic-gate setifsubnet(char *addr, int64_t param)
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
12887c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage subnet;
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, &subnet, &prefixlen);
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
12937c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
12947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
12957c478bd9Sstevel@tonic-gate 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
12967c478bd9Sstevel@tonic-gate 		exit(1);
12977c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
12987c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
12997c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13007c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
13017c478bd9Sstevel@tonic-gate 		exit(1);
13027c478bd9Sstevel@tonic-gate 	default:
13037c478bd9Sstevel@tonic-gate 		break;
13047c478bd9Sstevel@tonic-gate 	}
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = subnet;
13077c478bd9Sstevel@tonic-gate 	lifr.lifr_addrlen = prefixlen;
13087c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13097c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFSUBNET, (caddr_t)&lifr) < 0)
13107c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFSUBNET");
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	return (0);
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate /* ARGSUSED */
13167c478bd9Sstevel@tonic-gate static int
13177c478bd9Sstevel@tonic-gate setifnetmask(char *addr, int64_t param)
13187c478bd9Sstevel@tonic-gate {
13197c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask;
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	assert(afp->af_af != AF_INET6);
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	if (strcmp(addr, "+") == 0) {
1324dd7a6f5fSkcpoon 		if (!in_getmask(&netmask, _B_FALSE))
13257c478bd9Sstevel@tonic-gate 			return (0);
1326dd7a6f5fSkcpoon 		(void) printf("Setting netmask of %s to %s\n", name,
1327dd7a6f5fSkcpoon 		    inet_ntoa(netmask.sin_addr));
13287c478bd9Sstevel@tonic-gate 	} else {
13297c478bd9Sstevel@tonic-gate 		in_getaddr(addr, (struct sockaddr *)&netmask, NULL);
13307c478bd9Sstevel@tonic-gate 	}
13317c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13327c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &netmask, sizeof (netmask));
13337c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
13347c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
13357c478bd9Sstevel@tonic-gate 	return (0);
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate /*
13397c478bd9Sstevel@tonic-gate  * Parse '/<n>' as a netmask.
13407c478bd9Sstevel@tonic-gate  */
13417c478bd9Sstevel@tonic-gate /* ARGSUSED */
13427c478bd9Sstevel@tonic-gate static int
13437c478bd9Sstevel@tonic-gate setifprefixlen(char *addr, int64_t param)
13447c478bd9Sstevel@tonic-gate {
13457c478bd9Sstevel@tonic-gate 	int prefixlen;
13467c478bd9Sstevel@tonic-gate 	int af = afp->af_af;
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	prefixlen = in_getprefixlen(addr, _B_TRUE,
13497906a3e0Smeem 	    (af == AF_INET) ? IP_ABITS : IPV6_ABITS);
13507c478bd9Sstevel@tonic-gate 	if (prefixlen < 0) {
13517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13527c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
13537c478bd9Sstevel@tonic-gate 		exit(1);
13547c478bd9Sstevel@tonic-gate 	}
13557c478bd9Sstevel@tonic-gate 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
13567c478bd9Sstevel@tonic-gate 	lifr.lifr_addr.ss_family = af;
13577c478bd9Sstevel@tonic-gate 	if (af == AF_INET6) {
13587c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
13617906a3e0Smeem 		if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
13627c478bd9Sstevel@tonic-gate 		    (uchar_t *)&sin6->sin6_addr)) {
13637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: "
13647c478bd9Sstevel@tonic-gate 			    "Bad prefix length: %d\n",
13657c478bd9Sstevel@tonic-gate 			    prefixlen);
13667c478bd9Sstevel@tonic-gate 			exit(1);
13677c478bd9Sstevel@tonic-gate 		}
13687c478bd9Sstevel@tonic-gate 	} else if (af == AF_INET) {
13697c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin;
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
13727906a3e0Smeem 		if (!in_prefixlentomask(prefixlen, IP_ABITS,
13737c478bd9Sstevel@tonic-gate 		    (uchar_t *)&sin->sin_addr)) {
13747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: "
13757c478bd9Sstevel@tonic-gate 			    "Bad prefix length: %d\n",
13767c478bd9Sstevel@tonic-gate 			    prefixlen);
13777c478bd9Sstevel@tonic-gate 			exit(1);
13787c478bd9Sstevel@tonic-gate 		}
13797c478bd9Sstevel@tonic-gate 	} else {
13807c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: setting prefix only supported"
13817c478bd9Sstevel@tonic-gate 		    " for address family inet or inet6\n");
13827c478bd9Sstevel@tonic-gate 		exit(1);
13837c478bd9Sstevel@tonic-gate 	}
13847c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13857c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
13867c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
13877c478bd9Sstevel@tonic-gate 	return (0);
13887c478bd9Sstevel@tonic-gate }
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate /* ARGSUSED */
13917c478bd9Sstevel@tonic-gate static int
13927c478bd9Sstevel@tonic-gate setifbroadaddr(char *addr, int64_t param)
13937c478bd9Sstevel@tonic-gate {
13947c478bd9Sstevel@tonic-gate 	struct	sockaddr_in broadaddr;
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	assert(afp->af_af != AF_INET6);
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	if (strcmp(addr, "+") == 0) {
13997c478bd9Sstevel@tonic-gate 		/*
14007c478bd9Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
14017c478bd9Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
14027c478bd9Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
14037c478bd9Sstevel@tonic-gate 		 */
14047c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name,
14057c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
14067c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
14077c478bd9Sstevel@tonic-gate 			if (errno != EADDRNOTAVAIL)
14087c478bd9Sstevel@tonic-gate 				Perror0_exit("SIOCGLIFADDR");
14097c478bd9Sstevel@tonic-gate 			return (0);
14107c478bd9Sstevel@tonic-gate 		}
14117c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
14127c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCGLIFADDR");
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 		return (0);
14157c478bd9Sstevel@tonic-gate 	}
14167c478bd9Sstevel@tonic-gate 	in_getaddr(addr, (struct sockaddr *)&broadaddr, NULL);
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &broadaddr, sizeof (broadaddr));
14197c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14207c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFBRDADDR, (caddr_t)&lifr) < 0)
14217c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFBRDADDR");
14227c478bd9Sstevel@tonic-gate 	return (0);
14237c478bd9Sstevel@tonic-gate }
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate /*
14267c478bd9Sstevel@tonic-gate  * set interface destination address
14277c478bd9Sstevel@tonic-gate  */
14287c478bd9Sstevel@tonic-gate /* ARGSUSED */
14297c478bd9Sstevel@tonic-gate static int
14307c478bd9Sstevel@tonic-gate setifdstaddr(char *addr, int64_t param)
14317c478bd9Sstevel@tonic-gate {
14327c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, (struct sockaddr *)&lifr.lifr_addr, NULL);
14337c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14347c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFDSTADDR, (caddr_t)&lifr) < 0)
14357c478bd9Sstevel@tonic-gate 		Perror0_exit("setifdstaddr: SIOCSLIFDSTADDR");
14367c478bd9Sstevel@tonic-gate 	return (0);
14377c478bd9Sstevel@tonic-gate }
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate /* ARGSUSED */
14407c478bd9Sstevel@tonic-gate static int
14417c478bd9Sstevel@tonic-gate setifflags(char *val, int64_t value)
14427c478bd9Sstevel@tonic-gate {
14437c478bd9Sstevel@tonic-gate 	int phyintlen, origphyintlen;
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14467c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0)
14477c478bd9Sstevel@tonic-gate 		Perror0_exit("setifflags: SIOCGLIFFLAGS");
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	if (value == IFF_NOFAILOVER) {
14507c478bd9Sstevel@tonic-gate 		/*
14517c478bd9Sstevel@tonic-gate 		 * Fail if '-failover' is set after a prior addif created the
14527c478bd9Sstevel@tonic-gate 		 * alias on a different interface. This can happen when the
14537c478bd9Sstevel@tonic-gate 		 * interface is part of an IPMP group.
14547c478bd9Sstevel@tonic-gate 		 */
14557c478bd9Sstevel@tonic-gate 		phyintlen = strcspn(name, ":");
14567c478bd9Sstevel@tonic-gate 		origphyintlen = strcspn(origname, ":");
14577c478bd9Sstevel@tonic-gate 		if (phyintlen != origphyintlen ||
14587c478bd9Sstevel@tonic-gate 		    strncmp(name, origname, phyintlen) != 0) {
14597c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: can't set -failover "
14607c478bd9Sstevel@tonic-gate 			    "on failed/standby/offlined interface %s\n",
14617c478bd9Sstevel@tonic-gate 			    origname);
14627c478bd9Sstevel@tonic-gate 			exit(1);
14637c478bd9Sstevel@tonic-gate 		}
14647c478bd9Sstevel@tonic-gate 	}
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	if (value < 0) {
14677c478bd9Sstevel@tonic-gate 		value = -value;
14687c478bd9Sstevel@tonic-gate 		lifr.lifr_flags &= ~value;
146969bb4bb4Scarlsonj 		if ((value & IFF_UP) && (lifr.lifr_flags & IFF_DUPLICATE)) {
147069bb4bb4Scarlsonj 			/*
147169bb4bb4Scarlsonj 			 * If the user is trying to mark an interface with a
147269bb4bb4Scarlsonj 			 * duplicate address as "down," then fetch the address
147369bb4bb4Scarlsonj 			 * and set it.  This will cause IP to clear the
147469bb4bb4Scarlsonj 			 * IFF_DUPLICATE flag and stop the automatic recovery
147569bb4bb4Scarlsonj 			 * timer.
147669bb4bb4Scarlsonj 			 */
147769bb4bb4Scarlsonj 			value = lifr.lifr_flags;
147869bb4bb4Scarlsonj 			if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0)
147969bb4bb4Scarlsonj 				(void) ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr);
148069bb4bb4Scarlsonj 			lifr.lifr_flags = value;
148169bb4bb4Scarlsonj 		}
148269bb4bb4Scarlsonj 	} else {
14837c478bd9Sstevel@tonic-gate 		lifr.lifr_flags |= value;
148469bb4bb4Scarlsonj 	}
14857c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14867c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
14877c478bd9Sstevel@tonic-gate 		Perror0_exit("setifflags: SIOCSLIFFLAGS");
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate 	return (0);
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate /* ARGSUSED */
14937c478bd9Sstevel@tonic-gate static int
14947c478bd9Sstevel@tonic-gate setifmetric(char *val, int64_t param)
14957c478bd9Sstevel@tonic-gate {
14967c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14977c478bd9Sstevel@tonic-gate 	lifr.lifr_metric = atoi(val);
14987c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFMETRIC, (caddr_t)&lifr) < 0)
14997c478bd9Sstevel@tonic-gate 		Perror0_exit("setifmetric: SIOCSLIFMETRIC");
15007c478bd9Sstevel@tonic-gate 	return (0);
15017c478bd9Sstevel@tonic-gate }
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate /* ARGSUSED */
15047c478bd9Sstevel@tonic-gate static int
15057c478bd9Sstevel@tonic-gate setifmtu(char *val, int64_t param)
15067c478bd9Sstevel@tonic-gate {
15077c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15087c478bd9Sstevel@tonic-gate 	lifr.lifr_mtu = atoi(val);
15097c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFMTU, (caddr_t)&lifr) < 0)
15107c478bd9Sstevel@tonic-gate 		Perror0_exit("setifmtu: SIOCSLIFMTU");
15117c478bd9Sstevel@tonic-gate 	return (0);
15127c478bd9Sstevel@tonic-gate }
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate /* ARGSUSED */
15157c478bd9Sstevel@tonic-gate static int
15167c478bd9Sstevel@tonic-gate setifindex(char *val, int64_t param)
15177c478bd9Sstevel@tonic-gate {
15187c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15197c478bd9Sstevel@tonic-gate 	lifr.lifr_index = atoi(val);
15207c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFINDEX, (caddr_t)&lifr) < 0)
15217c478bd9Sstevel@tonic-gate 		Perror0_exit("setifindex: SIOCSLIFINDEX");
15227c478bd9Sstevel@tonic-gate 	return (0);
15237c478bd9Sstevel@tonic-gate }
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate /* ARGSUSED */
15267c478bd9Sstevel@tonic-gate static int
15277c478bd9Sstevel@tonic-gate setifether(char *addr, int64_t param)
15287c478bd9Sstevel@tonic-gate {
15297c478bd9Sstevel@tonic-gate 	uchar_t	*ea;
15307c478bd9Sstevel@tonic-gate 	iface_t	*current;
15317c478bd9Sstevel@tonic-gate 	int	maclen;
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
15347c478bd9Sstevel@tonic-gate 		ifstatus(name);
15357c478bd9Sstevel@tonic-gate 		print_ifether(name);
15367c478bd9Sstevel@tonic-gate 		return (0);
15377c478bd9Sstevel@tonic-gate 	}
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	phyif = NULL;
15407c478bd9Sstevel@tonic-gate 	logifs = NULL;
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	/*
15437c478bd9Sstevel@tonic-gate 	 * if the IP interface in the arguments is a logical
15447c478bd9Sstevel@tonic-gate 	 * interface, exit with an error now.
15457c478bd9Sstevel@tonic-gate 	 */
15467c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
15477c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: cannot change"
15487c478bd9Sstevel@tonic-gate 		    " ethernet address of a logical interface\n");
15497c478bd9Sstevel@tonic-gate 		exit(1);
15507c478bd9Sstevel@tonic-gate 	}
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	ea = _link_aton(addr, &maclen);
15537c478bd9Sstevel@tonic-gate 	if (ea == NULL) {
15547c478bd9Sstevel@tonic-gate 		if (maclen == -1)
15557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
15567c478bd9Sstevel@tonic-gate 			    "ifconfig: %s: bad address\n", addr);
15577c478bd9Sstevel@tonic-gate 		else
15587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: malloc() failed\n");
15597c478bd9Sstevel@tonic-gate 		exit(1);
15607c478bd9Sstevel@tonic-gate 	}
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	(void) strncpy(savedname, name, sizeof (savedname));
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	/*
15657c478bd9Sstevel@tonic-gate 	 * Call selectifs only for the IP interfaces that are ipv4.
15667c478bd9Sstevel@tonic-gate 	 * offflags == IFF_IPV6 because you should not change the
15677c478bd9Sstevel@tonic-gate 	 *		Ethernet address of an ipv6 interface
15687c478bd9Sstevel@tonic-gate 	 */
15697c478bd9Sstevel@tonic-gate 	foreachinterface(selectifs, 0, (char **)NULL, 0, 0, IFF_IPV6, 0);
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 	/* If physical interface not found, exit now */
15727c478bd9Sstevel@tonic-gate 	if (phyif == NULL) {
15737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
15747c478bd9Sstevel@tonic-gate 		    "ifconfig: interface %s not found\n", savedname);
15757c478bd9Sstevel@tonic-gate 		exit(1);
15767c478bd9Sstevel@tonic-gate 	}
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	/* Restore */
15797c478bd9Sstevel@tonic-gate 	(void) strncpy(name, savedname, sizeof (name));
15807c478bd9Sstevel@tonic-gate 	(void) strncpy(origname, savedname, sizeof (origname));
15817c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	/*
15847c478bd9Sstevel@tonic-gate 	 * close and reopen the socket
15857c478bd9Sstevel@tonic-gate 	 * we don't know which type of socket we have now
15867c478bd9Sstevel@tonic-gate 	 */
15877c478bd9Sstevel@tonic-gate 	(void) close(s);
15887c478bd9Sstevel@tonic-gate 	s = socket(SOCKET_AF(AF_UNSPEC), SOCK_DGRAM, 0);
15897c478bd9Sstevel@tonic-gate 	if (s < 0) {
15907c478bd9Sstevel@tonic-gate 		Perror0_exit("socket");
15917c478bd9Sstevel@tonic-gate 	}
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	/*
15947c478bd9Sstevel@tonic-gate 	 * mark down the logical interfaces first,
15957c478bd9Sstevel@tonic-gate 	 * and then the physical interface
15967c478bd9Sstevel@tonic-gate 	 */
15977c478bd9Sstevel@tonic-gate 	if (updownifs(logifs, 0) < 0 || updownifs(phyif, 0) < 0) {
15987c478bd9Sstevel@tonic-gate 		Perror0_exit("mark down interface failed");
15997c478bd9Sstevel@tonic-gate 	}
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 	/*
16027c478bd9Sstevel@tonic-gate 	 * Change the physical address
16037c478bd9Sstevel@tonic-gate 	 */
16047c478bd9Sstevel@tonic-gate 	if (dlpi_set_address(savedname, ea, maclen) == -1) {
16057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
16067c478bd9Sstevel@tonic-gate 		    "ifconfig: failed setting mac address on %s\n",
16077c478bd9Sstevel@tonic-gate 		    savedname);
16087c478bd9Sstevel@tonic-gate 	}
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	/*
16117c478bd9Sstevel@tonic-gate 	 * if any interfaces were marked down before changing the
16127c478bd9Sstevel@tonic-gate 	 * ethernet address, put them up again.
16137c478bd9Sstevel@tonic-gate 	 * First the physical interface, then the logical ones.
16147c478bd9Sstevel@tonic-gate 	 */
16157c478bd9Sstevel@tonic-gate 	if (updownifs(phyif, 1) < 0 || updownifs(logifs, 1) < 0) {
16167c478bd9Sstevel@tonic-gate 		Perror0_exit("mark down interface failed");
16177c478bd9Sstevel@tonic-gate 	}
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	/* Free the memory allocated by selectifs */
16207c478bd9Sstevel@tonic-gate 	free(phyif);
16217c478bd9Sstevel@tonic-gate 	for (current = logifs; current != NULL; current = logifs) {
16227c478bd9Sstevel@tonic-gate 		logifs = logifs->next;
16237c478bd9Sstevel@tonic-gate 		free(current);
16247c478bd9Sstevel@tonic-gate 	}
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	return (0);
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate /*
16307c478bd9Sstevel@tonic-gate  * Print an interface's Ethernet address, if it has one.
16317c478bd9Sstevel@tonic-gate  */
16327c478bd9Sstevel@tonic-gate static void
16337c478bd9Sstevel@tonic-gate print_ifether(char *ifname)
16347c478bd9Sstevel@tonic-gate {
16357c478bd9Sstevel@tonic-gate 	int		protocol;
16367c478bd9Sstevel@tonic-gate 	icfg_if_t	interface;
16377c478bd9Sstevel@tonic-gate 	icfg_handle_t	handle;
16387c478bd9Sstevel@tonic-gate 	int		fd;
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	fd = socket(AF_INET, SOCK_DGRAM, 0);
16437c478bd9Sstevel@tonic-gate 	if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
16447c478bd9Sstevel@tonic-gate 		/*
16457c478bd9Sstevel@tonic-gate 		 * It's possible the interface is only configured for
16467c478bd9Sstevel@tonic-gate 		 * IPv6; check again with AF_INET6.
16477c478bd9Sstevel@tonic-gate 		 */
16487c478bd9Sstevel@tonic-gate 		(void) close(fd);
16497c478bd9Sstevel@tonic-gate 		fd = socket(AF_INET6, SOCK_DGRAM, 0);
16507c478bd9Sstevel@tonic-gate 		if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
16517c478bd9Sstevel@tonic-gate 			(void) close(fd);
16527c478bd9Sstevel@tonic-gate 			return;
16537c478bd9Sstevel@tonic-gate 		}
16547c478bd9Sstevel@tonic-gate 	}
16557c478bd9Sstevel@tonic-gate 	(void) close(fd);
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate 	/* Virtual interfaces don't have MAC addresses */
16587c478bd9Sstevel@tonic-gate 	if (lifr.lifr_flags & IFF_VIRTUAL)
16597c478bd9Sstevel@tonic-gate 		return;
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	/*
16627c478bd9Sstevel@tonic-gate 	 * We must be careful to set if_protocol based on the current
16637c478bd9Sstevel@tonic-gate 	 * properties of the interface.  For instance, if "ip.tun0" is
16647c478bd9Sstevel@tonic-gate 	 * configured only as an IPv6 tunnel, then if_protocol must be
16657c478bd9Sstevel@tonic-gate 	 * set to AF_INET6 or icfg_get_tunnel_lower() will fail and
16667c478bd9Sstevel@tonic-gate 	 * we will falsely conclude that it's not a tunnel.
16677c478bd9Sstevel@tonic-gate 	 */
16687c478bd9Sstevel@tonic-gate 	interface.if_protocol = AF_INET;
16697c478bd9Sstevel@tonic-gate 	if (lifr.lifr_flags & IFF_IPV6)
16707c478bd9Sstevel@tonic-gate 		interface.if_protocol = AF_INET6;
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, ifname, sizeof (interface.if_name));
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	if (icfg_open(&handle, &interface) == ICFG_SUCCESS) {
16757c478bd9Sstevel@tonic-gate 		if (icfg_get_tunnel_lower(handle, &protocol) == ICFG_SUCCESS) {
16767c478bd9Sstevel@tonic-gate 			/* Tunnel op succeeded -- it's a tunnel so skip */
16777c478bd9Sstevel@tonic-gate 			icfg_close(handle);
16787c478bd9Sstevel@tonic-gate 			return;
16797c478bd9Sstevel@tonic-gate 		}
16807c478bd9Sstevel@tonic-gate 		icfg_close(handle);
16817c478bd9Sstevel@tonic-gate 	}
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	dlpi_print_address(ifname);
16847c478bd9Sstevel@tonic-gate }
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate /*
16877c478bd9Sstevel@tonic-gate  * static void selectifs(int argc, char *argv[], int af, struct lifreq *rp)
16887c478bd9Sstevel@tonic-gate  *
16897c478bd9Sstevel@tonic-gate  * Called inside setifether() to create a list of interfaces to
16907c478bd9Sstevel@tonic-gate  * mark down/up when changing the Ethernet address.
16917c478bd9Sstevel@tonic-gate  * If the current interface is the physical interface passed
16927c478bd9Sstevel@tonic-gate  * as an argument to ifconfig, update phyif.
16937c478bd9Sstevel@tonic-gate  * If the current interface is a logical interface associated
16947c478bd9Sstevel@tonic-gate  * to the physical interface, add it to the logifs list.
16957c478bd9Sstevel@tonic-gate  */
16967c478bd9Sstevel@tonic-gate /* ARGSUSED */
16977c478bd9Sstevel@tonic-gate static void
16987c478bd9Sstevel@tonic-gate selectifs(int argc, char *argv[], int af, struct lifreq *rp)
16997c478bd9Sstevel@tonic-gate {
17007c478bd9Sstevel@tonic-gate 	char		*colonp;
17017c478bd9Sstevel@tonic-gate 	int		length;
17027c478bd9Sstevel@tonic-gate 	iface_t		*current;
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	/*
17057c478bd9Sstevel@tonic-gate 	 *  savedname=	name of the IP interface to which you want to
17067c478bd9Sstevel@tonic-gate 	 *		change ethernet address
17077c478bd9Sstevel@tonic-gate 	 *  name=	name of the current IP interface
17087c478bd9Sstevel@tonic-gate 	 */
17097c478bd9Sstevel@tonic-gate 	colonp = strchr(name, ':');
17107c478bd9Sstevel@tonic-gate 	if (colonp == NULL)
17117c478bd9Sstevel@tonic-gate 		length = max(strlen(savedname), strlen(name));
17127c478bd9Sstevel@tonic-gate 	else
17137c478bd9Sstevel@tonic-gate 		length = max(strlen(savedname), colonp - name);
17147c478bd9Sstevel@tonic-gate 	if (strncmp(savedname, name, length) == 0) {
17157c478bd9Sstevel@tonic-gate 		(void) strcpy(lifr.lifr_name, name);
17167c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFFLAGS, &lifr) < 0) {
17177c478bd9Sstevel@tonic-gate 			Perror0("selectifs: SIOCGLIFFLAGS");
17187c478bd9Sstevel@tonic-gate 			return;
17197c478bd9Sstevel@tonic-gate 		}
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 		if ((current = malloc(sizeof (iface_t))) == NULL) {
17227c478bd9Sstevel@tonic-gate 			Perror0_exit("selectifs: malloc failed\n");
17237c478bd9Sstevel@tonic-gate 		}
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 		if (colonp == NULL) {
17267c478bd9Sstevel@tonic-gate 			/* this is the physical interface */
17277c478bd9Sstevel@tonic-gate 			phyif = current;
17287c478bd9Sstevel@tonic-gate 			bcopy(&lifr, &phyif->lifr, sizeof (struct lifreq));
17297c478bd9Sstevel@tonic-gate 			phyif->next = NULL;
17307c478bd9Sstevel@tonic-gate 		} else {
17317c478bd9Sstevel@tonic-gate 			/* this is a logical interface */
17327c478bd9Sstevel@tonic-gate 			bcopy(&lifr, &current->lifr, sizeof (struct lifreq));
17337c478bd9Sstevel@tonic-gate 			current->next = logifs;
17347c478bd9Sstevel@tonic-gate 			logifs = current;
17357c478bd9Sstevel@tonic-gate 		}
17367c478bd9Sstevel@tonic-gate 	}
17377c478bd9Sstevel@tonic-gate }
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate /*
17407c478bd9Sstevel@tonic-gate  * static int updownifs(iface_t *ifs, int up)
17417c478bd9Sstevel@tonic-gate  *
17427c478bd9Sstevel@tonic-gate  * It takes in input a list of IP interfaces (ifs)
17437c478bd9Sstevel@tonic-gate  * and a flag (up).
17447c478bd9Sstevel@tonic-gate  * It marks each interface in the list down (up = 0)
17457c478bd9Sstevel@tonic-gate  * or up (up > 0). This is done ONLY if the IP
17467c478bd9Sstevel@tonic-gate  * interface was originally up.
17477c478bd9Sstevel@tonic-gate  *
17487c478bd9Sstevel@tonic-gate  * Return values:
17497c478bd9Sstevel@tonic-gate  *  0 = everything OK
17507c478bd9Sstevel@tonic-gate  * -1 = problem
17517c478bd9Sstevel@tonic-gate  */
17527c478bd9Sstevel@tonic-gate static int
17537c478bd9Sstevel@tonic-gate updownifs(iface_t *ifs, int up)
17547c478bd9Sstevel@tonic-gate {
17557c478bd9Sstevel@tonic-gate 	iface_t *current;
17567c478bd9Sstevel@tonic-gate 	int	ret = 0;
17577c478bd9Sstevel@tonic-gate 	int 	save_errno;
17587c478bd9Sstevel@tonic-gate 	char	savename[LIFNAMSIZ];
17597c478bd9Sstevel@tonic-gate 	uint64_t orig_flags;
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	for (current = ifs; current != NULL; current = current->next) {
17627c478bd9Sstevel@tonic-gate 		if (current->lifr.lifr_flags & IFF_UP) {
17637c478bd9Sstevel@tonic-gate 			orig_flags = current->lifr.lifr_flags;
17647c478bd9Sstevel@tonic-gate 			if (!up)
17657c478bd9Sstevel@tonic-gate 				current->lifr.lifr_flags &= ~IFF_UP;
17667c478bd9Sstevel@tonic-gate 			if (ioctl(s, SIOCSLIFFLAGS, &current->lifr) < 0) {
17677c478bd9Sstevel@tonic-gate 				save_errno = errno;
17687c478bd9Sstevel@tonic-gate 				(void) strcpy(savename,
17697c478bd9Sstevel@tonic-gate 				    current->lifr.lifr_name);
17707c478bd9Sstevel@tonic-gate 				ret = -1;
17717c478bd9Sstevel@tonic-gate 			}
17727c478bd9Sstevel@tonic-gate 			if (!up) /* restore the original flags */
17737c478bd9Sstevel@tonic-gate 				current->lifr.lifr_flags = orig_flags;
17747c478bd9Sstevel@tonic-gate 		}
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	if (ret == -1) {
17787c478bd9Sstevel@tonic-gate 		(void) strcpy(lifr.lifr_name, savename);
17797c478bd9Sstevel@tonic-gate 		errno = save_errno;
17807c478bd9Sstevel@tonic-gate 	}
17817c478bd9Sstevel@tonic-gate 	return (ret);
17827c478bd9Sstevel@tonic-gate }
17837c478bd9Sstevel@tonic-gate 
1784f4b3ec61Sdh /*
1785f4b3ec61Sdh  * static int find_all_global_interfaces(struct lifconf *lifcp, char **buf,
1786f4b3ec61Sdh  *     int64_t lifc_flags)
1787f4b3ec61Sdh  *
1788d62bc4baSyz  * It finds all data links for the global zone.
1789f4b3ec61Sdh  *
1790f4b3ec61Sdh  * It takes in input a pointer to struct lifconf to receive interfaces
1791f4b3ec61Sdh  * informations, a **char to hold allocated buffer, and a lifc_flags.
1792f4b3ec61Sdh  *
1793f4b3ec61Sdh  * Return values:
1794f4b3ec61Sdh  *  0 = everything OK
1795f4b3ec61Sdh  * -1 = problem
1796f4b3ec61Sdh  */
1797f4b3ec61Sdh static int
1798f4b3ec61Sdh find_all_global_interfaces(struct lifconf *lifcp, char **buf,
1799f4b3ec61Sdh     int64_t lifc_flags)
1800f4b3ec61Sdh {
1801f4b3ec61Sdh 	unsigned bufsize;
1802f4b3ec61Sdh 	int n;
1803f4b3ec61Sdh 	ni_t *nip;
1804f4b3ec61Sdh 	struct lifreq *lifrp;
1805f4b3ec61Sdh 
1806d62bc4baSyz 	(void) dlpi_walk(ni_entry, NULL, 0);
1807f4b3ec61Sdh 
1808f4b3ec61Sdh 	/*
1809f4b3ec61Sdh 	 * Now, translate the linked list into
1810f4b3ec61Sdh 	 * a struct lifreq buffer
1811f4b3ec61Sdh 	 */
1812f4b3ec61Sdh 	if (num_ni == 0) {
1813f4b3ec61Sdh 		lifcp->lifc_family = AF_UNSPEC;
1814f4b3ec61Sdh 		lifcp->lifc_flags = lifc_flags;
1815f4b3ec61Sdh 		lifcp->lifc_len = 0;
1816f4b3ec61Sdh 		lifcp->lifc_buf = NULL;
1817f4b3ec61Sdh 		return (0);
1818f4b3ec61Sdh 	}
1819f4b3ec61Sdh 
1820f4b3ec61Sdh 	bufsize = num_ni * sizeof (struct lifreq);
1821f4b3ec61Sdh 	if ((*buf = malloc(bufsize)) == NULL)
1822f4b3ec61Sdh 		Perror0_exit("find_all_interfaces: malloc failed");
1823f4b3ec61Sdh 
1824f4b3ec61Sdh 	lifcp->lifc_family = AF_UNSPEC;
1825f4b3ec61Sdh 	lifcp->lifc_flags = lifc_flags;
1826f4b3ec61Sdh 	lifcp->lifc_len = bufsize;
1827f4b3ec61Sdh 	lifcp->lifc_buf = *buf;
1828f4b3ec61Sdh 
1829f4b3ec61Sdh 	for (n = 0, lifrp = lifcp->lifc_req; n < num_ni; n++, lifrp++) {
1830f4b3ec61Sdh 		nip = ni_list;
1831f4b3ec61Sdh 		(void) strncpy(lifrp->lifr_name, nip->ni_name,
1832f4b3ec61Sdh 		    sizeof (lifr.lifr_name));
1833f4b3ec61Sdh 		ni_list = nip->ni_next;
1834f4b3ec61Sdh 		free(nip);
1835f4b3ec61Sdh 	}
1836f4b3ec61Sdh 	return (0);
1837f4b3ec61Sdh }
1838f4b3ec61Sdh 
1839f4b3ec61Sdh /*
1840f4b3ec61Sdh  * static int find_all_zone_interfaces(struct lifconf *lifcp, char **buf,
1841f4b3ec61Sdh  *     int64_t lifc_flags)
1842f4b3ec61Sdh  *
1843f4b3ec61Sdh  * It finds all interfaces for an exclusive-IP zone, that is all the interfaces
1844f4b3ec61Sdh  * assigned to it.
1845f4b3ec61Sdh  *
1846f4b3ec61Sdh  * It takes in input a pointer to struct lifconf to receive interfaces
1847f4b3ec61Sdh  * informations, a **char to hold allocated buffer, and a lifc_flags.
1848f4b3ec61Sdh  *
1849f4b3ec61Sdh  * Return values:
1850f4b3ec61Sdh  *  0 = everything OK
1851f4b3ec61Sdh  * -1 = problem
1852f4b3ec61Sdh  */
1853f4b3ec61Sdh static int
1854f4b3ec61Sdh find_all_zone_interfaces(struct lifconf *lifcp, char **buf, int64_t lifc_flags)
1855f4b3ec61Sdh {
1856f4b3ec61Sdh 	zoneid_t zoneid;
1857f4b3ec61Sdh 	unsigned bufsize;
1858f4b3ec61Sdh 	char *dlnames, *ptr;
1859f4b3ec61Sdh 	struct lifreq *lifrp;
1860f4b3ec61Sdh 	int num_ni_saved, i;
1861f4b3ec61Sdh 
1862f4b3ec61Sdh 	zoneid = getzoneid();
1863f4b3ec61Sdh 
1864f4b3ec61Sdh 	num_ni = 0;
1865f4b3ec61Sdh 	if (zone_list_datalink(zoneid, &num_ni, NULL) != 0)
1866f4b3ec61Sdh 		Perror0_exit("find_all_interfaces: list interfaces failed");
1867f4b3ec61Sdh again:
1868f4b3ec61Sdh 	/* this zone doesn't have any data-links */
1869f4b3ec61Sdh 	if (num_ni == 0) {
1870f4b3ec61Sdh 		lifcp->lifc_family = AF_UNSPEC;
1871f4b3ec61Sdh 		lifcp->lifc_flags = lifc_flags;
1872f4b3ec61Sdh 		lifcp->lifc_len = 0;
1873f4b3ec61Sdh 		lifcp->lifc_buf = NULL;
1874f4b3ec61Sdh 		return (0);
1875f4b3ec61Sdh 	}
1876f4b3ec61Sdh 
1877f4b3ec61Sdh 	dlnames = malloc(num_ni * LIFNAMSIZ);
1878f4b3ec61Sdh 	if (dlnames == NULL)
1879f4b3ec61Sdh 		Perror0_exit("find_all_interfaces: out of memory");
1880f4b3ec61Sdh 	num_ni_saved = num_ni;
1881f4b3ec61Sdh 
1882f4b3ec61Sdh 	if (zone_list_datalink(zoneid, &num_ni, dlnames) != 0)
1883f4b3ec61Sdh 		Perror0_exit("find_all_interfaces: list interfaces failed");
1884f4b3ec61Sdh 
1885f4b3ec61Sdh 	if (num_ni_saved < num_ni) {
1886f4b3ec61Sdh 		/* list increased, try again */
1887f4b3ec61Sdh 		free(dlnames);
1888f4b3ec61Sdh 		goto again;
1889f4b3ec61Sdh 	}
1890f4b3ec61Sdh 
1891f4b3ec61Sdh 	/* this zone doesn't have any data-links now */
1892f4b3ec61Sdh 	if (num_ni == 0) {
1893f4b3ec61Sdh 		free(dlnames);
1894f4b3ec61Sdh 		lifcp->lifc_family = AF_UNSPEC;
1895f4b3ec61Sdh 		lifcp->lifc_flags = lifc_flags;
1896f4b3ec61Sdh 		lifcp->lifc_len = 0;
1897f4b3ec61Sdh 		lifcp->lifc_buf = NULL;
1898f4b3ec61Sdh 		return (0);
1899f4b3ec61Sdh 	}
1900f4b3ec61Sdh 
1901f4b3ec61Sdh 	bufsize = num_ni * sizeof (struct lifreq);
1902f4b3ec61Sdh 	if ((*buf = malloc(bufsize)) == NULL) {
1903f4b3ec61Sdh 		free(dlnames);
1904f4b3ec61Sdh 		Perror0_exit("find_all_interfaces: malloc failed");
1905f4b3ec61Sdh 	}
1906f4b3ec61Sdh 
1907f4b3ec61Sdh 	lifrp = (struct lifreq *)*buf;
1908f4b3ec61Sdh 	ptr = dlnames;
1909f4b3ec61Sdh 	for (i = 0; i < num_ni; i++) {
1910f4b3ec61Sdh 		if (strlcpy(lifrp->lifr_name, ptr, LIFNAMSIZ) >=
1911f4b3ec61Sdh 		    LIFNAMSIZ)
1912f4b3ec61Sdh 			Perror0_exit("find_all_interfaces: overflow");
1913f4b3ec61Sdh 		ptr += LIFNAMSIZ;
1914f4b3ec61Sdh 		lifrp++;
1915f4b3ec61Sdh 	}
1916f4b3ec61Sdh 
1917f4b3ec61Sdh 	free(dlnames);
1918f4b3ec61Sdh 	lifcp->lifc_family = AF_UNSPEC;
1919f4b3ec61Sdh 	lifcp->lifc_flags = lifc_flags;
1920f4b3ec61Sdh 	lifcp->lifc_len = bufsize;
1921f4b3ec61Sdh 	lifcp->lifc_buf = *buf;
1922f4b3ec61Sdh 	return (0);
1923f4b3ec61Sdh }
1924f4b3ec61Sdh 
19257c478bd9Sstevel@tonic-gate /*
19267c478bd9Sstevel@tonic-gate  * Create the next unused logical interface using the original name
19277c478bd9Sstevel@tonic-gate  * and assign the address (and mask if '/<n>' is part of the address).
19287c478bd9Sstevel@tonic-gate  * Use the new logical interface for subsequent subcommands by updating
19297c478bd9Sstevel@tonic-gate  * the name variable.
19307c478bd9Sstevel@tonic-gate  *
19317c478bd9Sstevel@tonic-gate  * This allows syntax like:
19327c478bd9Sstevel@tonic-gate  *	ifconfig le0 addif 109.106.86.130 netmask + up \
19337c478bd9Sstevel@tonic-gate  *	addif 109.106.86.131 netmask + up
19347c478bd9Sstevel@tonic-gate  */
19357c478bd9Sstevel@tonic-gate /* ARGSUSED */
19367c478bd9Sstevel@tonic-gate static int
19377c478bd9Sstevel@tonic-gate addif(char *str, int64_t param)
19387c478bd9Sstevel@tonic-gate {
19397c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
19407c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
19417c478bd9Sstevel@tonic-gate 	struct sockaddr_storage mask;
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	(void) strncpy(name, origname, sizeof (name));
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
19467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
19477c478bd9Sstevel@tonic-gate 		    "ifconfig: addif: bad physical interface name %s\n",
19487c478bd9Sstevel@tonic-gate 		    name);
19497c478bd9Sstevel@tonic-gate 		exit(1);
19507c478bd9Sstevel@tonic-gate 	}
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 	/*
19537c478bd9Sstevel@tonic-gate 	 * clear so parser will interpret next address as source followed
19547c478bd9Sstevel@tonic-gate 	 * by possible dest
19557c478bd9Sstevel@tonic-gate 	 */
19567c478bd9Sstevel@tonic-gate 	setaddr = 0;
19577c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(str, (struct sockaddr *)&laddr, &prefixlen);
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
19607c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
19617c478bd9Sstevel@tonic-gate 		/* Nothing there - ok */
19627c478bd9Sstevel@tonic-gate 		break;
19637c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
19647c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
19657c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", str);
19667c478bd9Sstevel@tonic-gate 		exit(1);
19677c478bd9Sstevel@tonic-gate 	default:
19687c478bd9Sstevel@tonic-gate 		(void) memset(&mask, 0, sizeof (mask));
19697c478bd9Sstevel@tonic-gate 		mask.ss_family = afp->af_af;
19707c478bd9Sstevel@tonic-gate 		if (afp->af_af == AF_INET6) {
19717c478bd9Sstevel@tonic-gate 			struct sockaddr_in6 *sin6;
19727c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&mask;
19737906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
19747c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin6->sin6_addr)) {
19757c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
19767c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
19777c478bd9Sstevel@tonic-gate 				    prefixlen);
19787c478bd9Sstevel@tonic-gate 				exit(1);
19797c478bd9Sstevel@tonic-gate 			}
19807c478bd9Sstevel@tonic-gate 		} else {
19817c478bd9Sstevel@tonic-gate 			struct sockaddr_in *sin;
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 			sin = (struct sockaddr_in *)&mask;
19847906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IP_ABITS,
19857c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin->sin_addr)) {
19867c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
19877c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
19887c478bd9Sstevel@tonic-gate 				    prefixlen);
19897c478bd9Sstevel@tonic-gate 				exit(1);
19907c478bd9Sstevel@tonic-gate 			}
19917c478bd9Sstevel@tonic-gate 		}
1992dd7a6f5fSkcpoon 		g_netmask_set = G_NETMASK_NIL;
19937c478bd9Sstevel@tonic-gate 		break;
19947c478bd9Sstevel@tonic-gate 	}
19957c478bd9Sstevel@tonic-gate 
1996dd7a6f5fSkcpoon 	/*
1997dd7a6f5fSkcpoon 	 * This is a "hack" to get around the problem of SIOCLIFADDIF.  The
1998dd7a6f5fSkcpoon 	 * problem is that this ioctl does not include the netmask when
1999dd7a6f5fSkcpoon 	 * adding a logical interface.  This is the same problem described
2000dd7a6f5fSkcpoon 	 * in the ifconfig() comments.  To get around this problem, we first
2001dd7a6f5fSkcpoon 	 * add the logical interface with a 0 address.  After that, we set
2002dd7a6f5fSkcpoon 	 * the netmask if provided.  Finally we set the interface address.
2003dd7a6f5fSkcpoon 	 */
20047c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2005dd7a6f5fSkcpoon 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	/* Note: no need to do DAD here since the interface isn't up yet. */
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0)
20107c478bd9Sstevel@tonic-gate 		Perror0_exit("addif: SIOCLIFADDIF");
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	(void) printf("Created new logical interface %s\n",
20137c478bd9Sstevel@tonic-gate 	    lifr.lifr_name);
20147c478bd9Sstevel@tonic-gate 	(void) strncpy(name, lifr.lifr_name, sizeof (name));
20157c478bd9Sstevel@tonic-gate 
2016dd7a6f5fSkcpoon 	/*
2017dd7a6f5fSkcpoon 	 * Check and see if any "netmask" command is used and perform the
2018dd7a6f5fSkcpoon 	 * necessary operation.
2019dd7a6f5fSkcpoon 	 */
2020dd7a6f5fSkcpoon 	set_mask_lifreq(&lifr, &laddr, &mask);
2021dd7a6f5fSkcpoon 	/*
2022dd7a6f5fSkcpoon 	 * Only set the netmask if "netmask" command is used or a prefix is
2023dd7a6f5fSkcpoon 	 * provided.
2024dd7a6f5fSkcpoon 	 */
2025dd7a6f5fSkcpoon 	if (g_netmask_set == G_NETMASK_SET || prefixlen >= 0) {
20267c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
20277c478bd9Sstevel@tonic-gate 			Perror0_exit("addif: SIOCSLIFNETMASK");
20287c478bd9Sstevel@tonic-gate 	}
2029dd7a6f5fSkcpoon 
2030dd7a6f5fSkcpoon 	/* Finally, we set the interface address. */
2031dd7a6f5fSkcpoon 	lifr.lifr_addr = laddr;
2032dd7a6f5fSkcpoon 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
2033dd7a6f5fSkcpoon 		Perror0_exit("SIOCSLIFADDR");
2034dd7a6f5fSkcpoon 
20357c478bd9Sstevel@tonic-gate 	/*
20367c478bd9Sstevel@tonic-gate 	 * let parser know we got a source.
20377c478bd9Sstevel@tonic-gate 	 * Next address, if given, should be dest
20387c478bd9Sstevel@tonic-gate 	 */
20397c478bd9Sstevel@tonic-gate 	setaddr++;
20407c478bd9Sstevel@tonic-gate 	return (0);
20417c478bd9Sstevel@tonic-gate }
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate /*
20447c478bd9Sstevel@tonic-gate  * Remove a logical interface based on its IP address. Unlike addif
20457c478bd9Sstevel@tonic-gate  * there is no '/<n>' here.
20467c478bd9Sstevel@tonic-gate  * Verifies that the interface is down before it is removed.
20477c478bd9Sstevel@tonic-gate  */
20487c478bd9Sstevel@tonic-gate /* ARGSUSED */
20497c478bd9Sstevel@tonic-gate static int
20507c478bd9Sstevel@tonic-gate removeif(char *str, int64_t param)
20517c478bd9Sstevel@tonic-gate {
20527c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
20557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
20567c478bd9Sstevel@tonic-gate 		    "ifconfig: removeif: bad physical interface name %s\n",
20577c478bd9Sstevel@tonic-gate 		    name);
20587c478bd9Sstevel@tonic-gate 		exit(1);
20597c478bd9Sstevel@tonic-gate 	}
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(str, &laddr, NULL);
20627c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = laddr;
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
20657c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) {
20667c478bd9Sstevel@tonic-gate 		if (errno == EBUSY) {
20677c478bd9Sstevel@tonic-gate 			/* This can only happen if ipif_id = 0 */
20687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
20697c478bd9Sstevel@tonic-gate 			    "ifconfig: removeif: can't remove interface: %s\n",
20707c478bd9Sstevel@tonic-gate 			    name);
20717c478bd9Sstevel@tonic-gate 			exit(1);
20727c478bd9Sstevel@tonic-gate 		}
20737c478bd9Sstevel@tonic-gate 		Perror0_exit("removeif: SIOCLIFREMOVEIF");
20747c478bd9Sstevel@tonic-gate 	}
20757c478bd9Sstevel@tonic-gate 	return (0);
20767c478bd9Sstevel@tonic-gate }
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate /*
20797c478bd9Sstevel@tonic-gate  * Set the address token for IPv6.
20807c478bd9Sstevel@tonic-gate  */
20817c478bd9Sstevel@tonic-gate /* ARGSUSED */
20827c478bd9Sstevel@tonic-gate static int
20837c478bd9Sstevel@tonic-gate setiftoken(char *addr, int64_t param)
20847c478bd9Sstevel@tonic-gate {
20857c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
20867c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 token;
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	in6_getaddr(addr, (struct sockaddr *)&token, &prefixlen);
20897c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
20907c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
20917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
20927c478bd9Sstevel@tonic-gate 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
20937c478bd9Sstevel@tonic-gate 		exit(1);
20947c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
20957c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
20967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
20977c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
20987c478bd9Sstevel@tonic-gate 		exit(1);
20997c478bd9Sstevel@tonic-gate 	default:
21007c478bd9Sstevel@tonic-gate 		break;
21017c478bd9Sstevel@tonic-gate 	}
21027c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &token, sizeof (token));
21037c478bd9Sstevel@tonic-gate 	lifr.lifr_addrlen = prefixlen;
21047c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
21057c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFTOKEN, (caddr_t)&lifr) < 0)  {
21067c478bd9Sstevel@tonic-gate 		Perror0_exit("setiftoken: SIOCSLIFTOKEN");
21077c478bd9Sstevel@tonic-gate 	}
21087c478bd9Sstevel@tonic-gate 	return (0);
21097c478bd9Sstevel@tonic-gate }
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate /*
21127c478bd9Sstevel@tonic-gate  * Return value: 0 on success, -1 on failure.
21137c478bd9Sstevel@tonic-gate  */
21147c478bd9Sstevel@tonic-gate static int
21157c478bd9Sstevel@tonic-gate connect_to_mpathd(int family)
21167c478bd9Sstevel@tonic-gate {
21177c478bd9Sstevel@tonic-gate 	int s;
21187c478bd9Sstevel@tonic-gate 	struct sockaddr_storage ss;
21197c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
21207c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
21217c478bd9Sstevel@tonic-gate 	struct in6_addr loopback_addr = IN6ADDR_LOOPBACK_INIT;
21227c478bd9Sstevel@tonic-gate 	int addrlen;
21237c478bd9Sstevel@tonic-gate 	int ret;
21247c478bd9Sstevel@tonic-gate 	int on;
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	s = socket(family, SOCK_STREAM, 0);
21277c478bd9Sstevel@tonic-gate 	if (s < 0) {
21287c478bd9Sstevel@tonic-gate 		Perror0_exit("connect_to_mpathd: socket");
21297c478bd9Sstevel@tonic-gate 	}
21307c478bd9Sstevel@tonic-gate 	(void) bzero((char *)&ss, sizeof (ss));
21317c478bd9Sstevel@tonic-gate 	ss.ss_family = family;
21327c478bd9Sstevel@tonic-gate 	/*
21337c478bd9Sstevel@tonic-gate 	 * Need to bind to a privileged port. For non-root, this
21347c478bd9Sstevel@tonic-gate 	 * will fail. in.mpathd verifies that only commands coming
21357c478bd9Sstevel@tonic-gate 	 * from privileged ports succeed so that ordinary users
21367c478bd9Sstevel@tonic-gate 	 * can't connect and start talking to in.mpathd
21377c478bd9Sstevel@tonic-gate 	 */
21387c478bd9Sstevel@tonic-gate 	on = 1;
21397c478bd9Sstevel@tonic-gate 	if (setsockopt(s, IPPROTO_TCP, TCP_ANONPRIVBIND, &on,
21407c478bd9Sstevel@tonic-gate 	    sizeof (on)) < 0) {
21417c478bd9Sstevel@tonic-gate 		Perror0_exit("connect_to_mpathd: setsockopt");
21427c478bd9Sstevel@tonic-gate 	}
21437c478bd9Sstevel@tonic-gate 	switch (family) {
21447c478bd9Sstevel@tonic-gate 	case AF_INET:
21457c478bd9Sstevel@tonic-gate 		sin->sin_port = 0;
21467c478bd9Sstevel@tonic-gate 		sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
21477c478bd9Sstevel@tonic-gate 		addrlen = sizeof (struct sockaddr_in);
21487c478bd9Sstevel@tonic-gate 		break;
21497c478bd9Sstevel@tonic-gate 	case AF_INET6:
21507c478bd9Sstevel@tonic-gate 		sin6->sin6_port = 0;
21517c478bd9Sstevel@tonic-gate 		sin6->sin6_addr = loopback_addr;
21527c478bd9Sstevel@tonic-gate 		addrlen = sizeof (struct sockaddr_in6);
21537c478bd9Sstevel@tonic-gate 		break;
21547c478bd9Sstevel@tonic-gate 	}
21557c478bd9Sstevel@tonic-gate 	ret = bind(s, (struct sockaddr *)&ss, addrlen);
21567c478bd9Sstevel@tonic-gate 	if (ret != 0) {
21577c478bd9Sstevel@tonic-gate 		(void) close(s);
21587c478bd9Sstevel@tonic-gate 		return (-1);
21597c478bd9Sstevel@tonic-gate 	}
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	switch (family) {
21627c478bd9Sstevel@tonic-gate 	case AF_INET:
21637c478bd9Sstevel@tonic-gate 		sin->sin_port = htons(MPATHD_PORT);
21647c478bd9Sstevel@tonic-gate 		break;
21657c478bd9Sstevel@tonic-gate 	case AF_INET6:
21667c478bd9Sstevel@tonic-gate 		sin6->sin6_port = htons(MPATHD_PORT);
21677c478bd9Sstevel@tonic-gate 		break;
21687c478bd9Sstevel@tonic-gate 	}
21697c478bd9Sstevel@tonic-gate 	ret = connect(s, (struct sockaddr *)&ss, addrlen);
21707c478bd9Sstevel@tonic-gate 	(void) close(s);
21717c478bd9Sstevel@tonic-gate 	return (ret);
21727c478bd9Sstevel@tonic-gate }
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate /* ARGSUSED */
21757c478bd9Sstevel@tonic-gate static int
21767c478bd9Sstevel@tonic-gate setifgroupname(char *grpname, int64_t param)
21777c478bd9Sstevel@tonic-gate {
21787c478bd9Sstevel@tonic-gate 	if (debug) {
21797c478bd9Sstevel@tonic-gate 		(void) printf("Setting groupname %s on interface %s\n",
21807c478bd9Sstevel@tonic-gate 		    grpname, name);
21817c478bd9Sstevel@tonic-gate 	}
21827c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
21837c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_groupname, grpname,
21847c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_groupname));
21857c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFGROUPNAME, (caddr_t)&lifr) < 0) {
21867c478bd9Sstevel@tonic-gate 		Perror0_exit("setifgroupname: SIOCSLIFGROUPNAME");
21877c478bd9Sstevel@tonic-gate 	}
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate 	/*
21907c478bd9Sstevel@tonic-gate 	 * If the SUNW_NO_MPATHD environment variable is set then don't
21917c478bd9Sstevel@tonic-gate 	 * bother starting up in.mpathd.  See PSARC/2002/249 for the
21927c478bd9Sstevel@tonic-gate 	 * depressing details on this bit of stupidity.
21937c478bd9Sstevel@tonic-gate 	 */
21947c478bd9Sstevel@tonic-gate 	if (getenv("SUNW_NO_MPATHD") != NULL) {
21957c478bd9Sstevel@tonic-gate 		return (0);
21967c478bd9Sstevel@tonic-gate 	}
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	/*
21997c478bd9Sstevel@tonic-gate 	 * Try to connect to in.mpathd using IPv4. If we succeed,
22007c478bd9Sstevel@tonic-gate 	 * we conclude that in.mpathd is running, and quit.
22017c478bd9Sstevel@tonic-gate 	 */
22027c478bd9Sstevel@tonic-gate 	if (connect_to_mpathd(AF_INET) == 0) {
22037c478bd9Sstevel@tonic-gate 		/* connect succeeded, mpathd is already running */
22047c478bd9Sstevel@tonic-gate 		return (0);
22057c478bd9Sstevel@tonic-gate 	}
22067c478bd9Sstevel@tonic-gate 	/*
22077c478bd9Sstevel@tonic-gate 	 * Try to connect to in.mpathd using IPv6. If we succeed,
22087c478bd9Sstevel@tonic-gate 	 * we conclude that in.mpathd is running, and quit.
22097c478bd9Sstevel@tonic-gate 	 */
22107c478bd9Sstevel@tonic-gate 	if (connect_to_mpathd(AF_INET6) == 0) {
22117c478bd9Sstevel@tonic-gate 		/* connect succeeded, mpathd is already running */
22127c478bd9Sstevel@tonic-gate 		return (0);
22137c478bd9Sstevel@tonic-gate 	}
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 	/*
22167c478bd9Sstevel@tonic-gate 	 * in.mpathd may not be running. Start it now. If it is already
22177c478bd9Sstevel@tonic-gate 	 * running, in.mpathd will take care of handling multiple incarnations
22187c478bd9Sstevel@tonic-gate 	 * of itself. ifconfig only tries to optimize performance by not
22197c478bd9Sstevel@tonic-gate 	 * starting another incarnation of in.mpathd.
22207c478bd9Sstevel@tonic-gate 	 */
22217c478bd9Sstevel@tonic-gate 	switch (fork()) {
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate 	case -1:
22247c478bd9Sstevel@tonic-gate 		Perror0_exit("setifgroupname: fork");
22257c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
22267c478bd9Sstevel@tonic-gate 	case 0:
22277c478bd9Sstevel@tonic-gate 		(void) execl(MPATHD_PATH, MPATHD_PATH, NULL);
22287c478bd9Sstevel@tonic-gate 		_exit(1);
22297c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
22307c478bd9Sstevel@tonic-gate 	default:
22317c478bd9Sstevel@tonic-gate 		return (0);
22327c478bd9Sstevel@tonic-gate 	}
22337c478bd9Sstevel@tonic-gate }
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate /*
22377c478bd9Sstevel@tonic-gate  * To list all the modules above a given network interface.
22387c478bd9Sstevel@tonic-gate  */
22397c478bd9Sstevel@tonic-gate /* ARGSUSED */
22407c478bd9Sstevel@tonic-gate static int
22417c478bd9Sstevel@tonic-gate modlist(char *null, int64_t param)
22427c478bd9Sstevel@tonic-gate {
2243fc80c0dfSnordmark 	int muxid_fd;
22447c478bd9Sstevel@tonic-gate 	int muxfd;
22457c478bd9Sstevel@tonic-gate 	int ipfd_lowstr;
22467c478bd9Sstevel@tonic-gate 	int arpfd_lowstr;
22477c478bd9Sstevel@tonic-gate 	int num_mods;
22487c478bd9Sstevel@tonic-gate 	int i;
22497c478bd9Sstevel@tonic-gate 	struct str_list strlist;
22507c478bd9Sstevel@tonic-gate 	int orig_arpid;
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2253fc80c0dfSnordmark 	if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
22547c478bd9Sstevel@tonic-gate 	    &orig_arpid) < 0) {
22557c478bd9Sstevel@tonic-gate 		return (-1);
22567c478bd9Sstevel@tonic-gate 	}
22577c478bd9Sstevel@tonic-gate 	if ((num_mods = ioctl(ipfd_lowstr, I_LIST, NULL)) < 0) {
22587c478bd9Sstevel@tonic-gate 		Perror0("cannot I_LIST to get the number of modules");
22597c478bd9Sstevel@tonic-gate 	} else {
22607c478bd9Sstevel@tonic-gate 		if (debug > 0) {
22617c478bd9Sstevel@tonic-gate 			(void) printf("Listing (%d) modules above %s\n",
22627c478bd9Sstevel@tonic-gate 			    num_mods, name);
22637c478bd9Sstevel@tonic-gate 		}
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 		strlist.sl_nmods = num_mods;
22667c478bd9Sstevel@tonic-gate 		strlist.sl_modlist = malloc(sizeof (struct str_mlist) *
22677c478bd9Sstevel@tonic-gate 		    num_mods);
22687c478bd9Sstevel@tonic-gate 		if (strlist.sl_modlist == NULL) {
22697c478bd9Sstevel@tonic-gate 			Perror0("cannot malloc");
22707c478bd9Sstevel@tonic-gate 		} else {
22717c478bd9Sstevel@tonic-gate 			if (ioctl(ipfd_lowstr, I_LIST, (caddr_t)&strlist) < 0) {
22727c478bd9Sstevel@tonic-gate 				Perror0("cannot I_LIST for module names");
22737c478bd9Sstevel@tonic-gate 			} else {
22747c478bd9Sstevel@tonic-gate 				for (i = 0; i < strlist.sl_nmods; i++) {
2275fc80c0dfSnordmark 					(void) printf("%d %s\n", i,
2276fc80c0dfSnordmark 					    strlist.sl_modlist[i].l_name);
22777c478bd9Sstevel@tonic-gate 				}
22787c478bd9Sstevel@tonic-gate 			}
22797c478bd9Sstevel@tonic-gate 			free(strlist.sl_modlist);
22807c478bd9Sstevel@tonic-gate 		}
22817c478bd9Sstevel@tonic-gate 	}
2282fc80c0dfSnordmark 	return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2283fc80c0dfSnordmark 	    orig_arpid));
22847c478bd9Sstevel@tonic-gate }
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate #define	MODINSERT_OP	'i'
22877c478bd9Sstevel@tonic-gate #define	MODREMOVE_OP	'r'
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate /*
22907c478bd9Sstevel@tonic-gate  * To insert a module to the stream of the interface.  It is just a
22917c478bd9Sstevel@tonic-gate  * wrapper.  The real function is modop().
22927c478bd9Sstevel@tonic-gate  */
22937c478bd9Sstevel@tonic-gate /* ARGSUSED */
22947c478bd9Sstevel@tonic-gate static int
22957c478bd9Sstevel@tonic-gate modinsert(char *arg, int64_t param)
22967c478bd9Sstevel@tonic-gate {
22977c478bd9Sstevel@tonic-gate 	return (modop(arg, MODINSERT_OP));
22987c478bd9Sstevel@tonic-gate }
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate /*
23017c478bd9Sstevel@tonic-gate  * To remove a module from the stream of the interface.  It is just a
23027c478bd9Sstevel@tonic-gate  * wrapper.  The real function is modop().
23037c478bd9Sstevel@tonic-gate  */
23047c478bd9Sstevel@tonic-gate /* ARGSUSED */
23057c478bd9Sstevel@tonic-gate static int
23067c478bd9Sstevel@tonic-gate modremove(char *arg, int64_t param)
23077c478bd9Sstevel@tonic-gate {
23087c478bd9Sstevel@tonic-gate 	return (modop(arg, MODREMOVE_OP));
23097c478bd9Sstevel@tonic-gate }
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate /*
2312fc80c0dfSnordmark  * Open a stream on /dev/udp{,6}, pop off all undesired modules (note that
2313fc80c0dfSnordmark  * the user may have configured autopush to add modules above
2314fc80c0dfSnordmark  * udp), and push the arp module onto the resulting stream.
2315fc80c0dfSnordmark  * This is used to make IP+ARP be able to atomically track the muxid
2316fc80c0dfSnordmark  * for the I_PLINKed STREAMS, thus it isn't related to ARP running the ARP
2317fc80c0dfSnordmark  * protocol.
23187c478bd9Sstevel@tonic-gate  */
23197c478bd9Sstevel@tonic-gate static int
23207c478bd9Sstevel@tonic-gate open_arp_on_udp(char *udp_dev_name)
23217c478bd9Sstevel@tonic-gate {
23227c478bd9Sstevel@tonic-gate 	int fd;
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	if ((fd = open(udp_dev_name, O_RDWR)) == -1) {
23257c478bd9Sstevel@tonic-gate 		Perror2("open", udp_dev_name);
23267c478bd9Sstevel@tonic-gate 		return (-1);
23277c478bd9Sstevel@tonic-gate 	}
23287c478bd9Sstevel@tonic-gate 	errno = 0;
23297c478bd9Sstevel@tonic-gate 	while (ioctl(fd, I_POP, 0) != -1)
2330fc80c0dfSnordmark 		;
2331fc80c0dfSnordmark 	if (errno != EINVAL) {
23327c478bd9Sstevel@tonic-gate 		Perror2("pop", udp_dev_name);
23337c478bd9Sstevel@tonic-gate 	} else if (ioctl(fd, I_PUSH, ARP_MOD_NAME) == -1) {
23347c478bd9Sstevel@tonic-gate 		Perror2("arp PUSH", udp_dev_name);
23357c478bd9Sstevel@tonic-gate 	} else {
23367c478bd9Sstevel@tonic-gate 		return (fd);
23377c478bd9Sstevel@tonic-gate 	}
23387c478bd9Sstevel@tonic-gate 	(void) close(fd);
23397c478bd9Sstevel@tonic-gate 	return (-1);
23407c478bd9Sstevel@tonic-gate }
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate /*
23437c478bd9Sstevel@tonic-gate  * Helper function for mod*() functions.  It gets a fd to the lower IP
23447c478bd9Sstevel@tonic-gate  * stream and I_PUNLINK's the lower stream.  It also initializes the
23457c478bd9Sstevel@tonic-gate  * global variable lifr.
23467c478bd9Sstevel@tonic-gate  *
23477c478bd9Sstevel@tonic-gate  * Param:
2348fc80c0dfSnordmark  *	int *muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2349fc80c0dfSnordmark  *	int *muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2350fc80c0dfSnordmark  *	int *ipfd_lowstr: fd to the lower IP stream.
2351fc80c0dfSnordmark  *	int *arpfd_lowstr: fd to the lower ARP stream.
23527c478bd9Sstevel@tonic-gate  *
23537c478bd9Sstevel@tonic-gate  * Return:
23547c478bd9Sstevel@tonic-gate  *	-1 if operation fails, 0 otherwise.
23557c478bd9Sstevel@tonic-gate  *
23567c478bd9Sstevel@tonic-gate  * Please see the big block comment above plumb_one_device()
23577c478bd9Sstevel@tonic-gate  * for the logic of the PLINK/PUNLINK
23587c478bd9Sstevel@tonic-gate  */
23597c478bd9Sstevel@tonic-gate static int
2360fc80c0dfSnordmark ip_domux2fd(int *muxfd, int *muxid_fd, int *ipfd_lowstr, int *arpfd_lowstr,
2361fc80c0dfSnordmark     int *orig_arpid)
23627c478bd9Sstevel@tonic-gate {
23637c478bd9Sstevel@tonic-gate 	uint64_t	flags;
23647c478bd9Sstevel@tonic-gate 	char		*udp_dev_name;
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate 	*orig_arpid = 0;
23677c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
23687c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
23697c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
23707c478bd9Sstevel@tonic-gate 	}
23717c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
23727c478bd9Sstevel@tonic-gate 	if (flags & IFF_IPV4) {
23737c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP_DEV_NAME;
23747c478bd9Sstevel@tonic-gate 	} else if (flags & IFF_IPV6) {
23757c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP6_DEV_NAME;
23767c478bd9Sstevel@tonic-gate 	} else {
23777c478bd9Sstevel@tonic-gate 		return (-1);
23787c478bd9Sstevel@tonic-gate 	}
23797c478bd9Sstevel@tonic-gate 
2380fc80c0dfSnordmark 	if ((*muxid_fd = open(udp_dev_name, O_RDWR)) < 0) {
2381fc80c0dfSnordmark 		Perror2("open", udp_dev_name);
23827c478bd9Sstevel@tonic-gate 		return (-1);
23837c478bd9Sstevel@tonic-gate 	}
2384fc80c0dfSnordmark 	if (ioctl(*muxid_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
2385fc80c0dfSnordmark 		Perror2("SIOCGLIFMUXID", udp_dev_name);
23867c478bd9Sstevel@tonic-gate 		return (-1);
23877c478bd9Sstevel@tonic-gate 	}
23887c478bd9Sstevel@tonic-gate 	if (debug > 0) {
23897c478bd9Sstevel@tonic-gate 		(void) printf("ARP_muxid %d IP_muxid %d\n",
23907c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid, lifr.lifr_ip_muxid);
23917c478bd9Sstevel@tonic-gate 	}
23927c478bd9Sstevel@tonic-gate 
2393fc80c0dfSnordmark 	/*
2394fc80c0dfSnordmark 	 * Use /dev/udp{,6} as the mux to avoid linkcycles.
2395fc80c0dfSnordmark 	 */
23967c478bd9Sstevel@tonic-gate 	if ((*muxfd = open_arp_on_udp(udp_dev_name)) == -1)
23977c478bd9Sstevel@tonic-gate 		return (-1);
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 	if (lifr.lifr_arp_muxid != 0) {
24007c478bd9Sstevel@tonic-gate 		if ((*arpfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
24017c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid)) < 0) {
24027c478bd9Sstevel@tonic-gate 			if ((errno == EINVAL) &&
24037c478bd9Sstevel@tonic-gate 			    (flags & (IFF_NOARP | IFF_IPV6))) {
24047c478bd9Sstevel@tonic-gate 				/*
24057c478bd9Sstevel@tonic-gate 				 * Some plumbing utilities set the muxid to
24067c478bd9Sstevel@tonic-gate 				 * -1 or some invalid value to signify that
24077c478bd9Sstevel@tonic-gate 				 * there is no arp stream. Set the muxid to 0
24087c478bd9Sstevel@tonic-gate 				 * before trying to unplumb the IP stream.
24097c478bd9Sstevel@tonic-gate 				 * IP does not allow the IP stream to be
24107c478bd9Sstevel@tonic-gate 				 * unplumbed if it sees a non-null arp muxid,
24117c478bd9Sstevel@tonic-gate 				 * for consistency of IP-ARP streams.
24127c478bd9Sstevel@tonic-gate 				 */
24137c478bd9Sstevel@tonic-gate 				*orig_arpid = lifr.lifr_arp_muxid;
24147c478bd9Sstevel@tonic-gate 				lifr.lifr_arp_muxid = 0;
2415fc80c0dfSnordmark 				(void) ioctl(*muxid_fd, SIOCSLIFMUXID,
24167c478bd9Sstevel@tonic-gate 				    (caddr_t)&lifr);
24177c478bd9Sstevel@tonic-gate 				*arpfd_lowstr = -1;
24187c478bd9Sstevel@tonic-gate 			} else {
24197c478bd9Sstevel@tonic-gate 				Perror0("_I_MUXID2FD");
24207c478bd9Sstevel@tonic-gate 				return (-1);
24217c478bd9Sstevel@tonic-gate 			}
24227c478bd9Sstevel@tonic-gate 		} else if (ioctl(*muxfd, I_PUNLINK,
24237c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid) < 0) {
24247c478bd9Sstevel@tonic-gate 			Perror2("I_PUNLINK", udp_dev_name);
24257c478bd9Sstevel@tonic-gate 			return (-1);
24267c478bd9Sstevel@tonic-gate 		}
24277c478bd9Sstevel@tonic-gate 	} else {
24287c478bd9Sstevel@tonic-gate 		*arpfd_lowstr = -1;
24297c478bd9Sstevel@tonic-gate 	}
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	if ((*ipfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
24327c478bd9Sstevel@tonic-gate 	    lifr.lifr_ip_muxid)) < 0) {
24337c478bd9Sstevel@tonic-gate 		Perror0("_I_MUXID2FD");
24347c478bd9Sstevel@tonic-gate 		/* Undo any changes we made */
24357c478bd9Sstevel@tonic-gate 		if (*orig_arpid != 0) {
24367c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = *orig_arpid;
2437fc80c0dfSnordmark 			(void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
24387c478bd9Sstevel@tonic-gate 		}
24397c478bd9Sstevel@tonic-gate 		return (-1);
24407c478bd9Sstevel@tonic-gate 	}
24417c478bd9Sstevel@tonic-gate 	if (ioctl(*muxfd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) {
24427c478bd9Sstevel@tonic-gate 		Perror2("I_PUNLINK", udp_dev_name);
24437c478bd9Sstevel@tonic-gate 		/* Undo any changes we made */
24447c478bd9Sstevel@tonic-gate 		if (*orig_arpid != 0) {
24457c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = *orig_arpid;
2446fc80c0dfSnordmark 			(void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
24477c478bd9Sstevel@tonic-gate 		}
24487c478bd9Sstevel@tonic-gate 		return (-1);
24497c478bd9Sstevel@tonic-gate 	}
24507c478bd9Sstevel@tonic-gate 	return (0);
24517c478bd9Sstevel@tonic-gate }
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate /*
24547c478bd9Sstevel@tonic-gate  * Helper function for mod*() functions.  It I_PLINK's back the upper and
24557c478bd9Sstevel@tonic-gate  * lower IP streams.  Note that this function must be called after
24567c478bd9Sstevel@tonic-gate  * ip_domux2fd().  In ip_domux2fd(), the global variable lifr is initialized
24577c478bd9Sstevel@tonic-gate  * and ip_plink() needs information in lifr.  So ip_domux2fd() and ip_plink()
24587c478bd9Sstevel@tonic-gate  * must be called in pairs.
24597c478bd9Sstevel@tonic-gate  *
24607c478bd9Sstevel@tonic-gate  * Param:
2461fc80c0dfSnordmark  *	int muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2462fc80c0dfSnordmark  *	int muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2463fc80c0dfSnordmark  *	int ipfd_lowstr: fd to the lower IP stream.
2464fc80c0dfSnordmark  *	int arpfd_lowstr: fd to the lower ARP stream.
24657c478bd9Sstevel@tonic-gate  *
24667c478bd9Sstevel@tonic-gate  * Return:
24677c478bd9Sstevel@tonic-gate  *	-1 if operation fails, 0 otherwise.
24687c478bd9Sstevel@tonic-gate  *
24697c478bd9Sstevel@tonic-gate  * Please see the big block comment above plumb_one_device()
24707c478bd9Sstevel@tonic-gate  * for the logic of the PLINK/PUNLINK
24717c478bd9Sstevel@tonic-gate  */
24727c478bd9Sstevel@tonic-gate static int
2473fc80c0dfSnordmark ip_plink(int muxfd, int muxid_fd, int ipfd_lowstr, int arpfd_lowstr,
2474fc80c0dfSnordmark     int orig_arpid)
24757c478bd9Sstevel@tonic-gate {
24767c478bd9Sstevel@tonic-gate 	int ip_muxid;
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate 	ip_muxid = ioctl(muxfd, I_PLINK, ipfd_lowstr);
24797c478bd9Sstevel@tonic-gate 	if (ip_muxid < 0) {
24807c478bd9Sstevel@tonic-gate 		Perror2("I_PLINK", UDP_DEV_NAME);
24817c478bd9Sstevel@tonic-gate 		return (-1);
24827c478bd9Sstevel@tonic-gate 	}
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 	/*
24857c478bd9Sstevel@tonic-gate 	 * If there is an arp stream, plink it. If there is no
24867c478bd9Sstevel@tonic-gate 	 * arp stream, then it is possible that the plumbing
24877c478bd9Sstevel@tonic-gate 	 * utility could have stored any value in the arp_muxid.
24887c478bd9Sstevel@tonic-gate 	 * If so, restore it from orig_arpid.
24897c478bd9Sstevel@tonic-gate 	 */
24907c478bd9Sstevel@tonic-gate 	if (arpfd_lowstr != -1) {
24917c478bd9Sstevel@tonic-gate 		if (ioctl(muxfd, I_PLINK, arpfd_lowstr) < 0) {
24927c478bd9Sstevel@tonic-gate 			Perror2("I_PLINK", UDP_DEV_NAME);
24937c478bd9Sstevel@tonic-gate 			return (-1);
24947c478bd9Sstevel@tonic-gate 		}
24957c478bd9Sstevel@tonic-gate 	} else if (orig_arpid != 0) {
24967c478bd9Sstevel@tonic-gate 		/* Undo the changes we did in ip_domux2fd */
24977c478bd9Sstevel@tonic-gate 		lifr.lifr_arp_muxid = orig_arpid;
24987c478bd9Sstevel@tonic-gate 		lifr.lifr_ip_muxid = ip_muxid;
2499fc80c0dfSnordmark 		(void) ioctl(muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
25007c478bd9Sstevel@tonic-gate 	}
25017c478bd9Sstevel@tonic-gate 
2502fc80c0dfSnordmark 	(void) close(muxfd);
2503fc80c0dfSnordmark 	(void) close(muxid_fd);
25047c478bd9Sstevel@tonic-gate 	return (0);
25057c478bd9Sstevel@tonic-gate }
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate /*
25087c478bd9Sstevel@tonic-gate  * The real function to perform module insertion/removal.
25097c478bd9Sstevel@tonic-gate  *
25107c478bd9Sstevel@tonic-gate  * Param:
25117c478bd9Sstevel@tonic-gate  *	char *arg: the argument string module_name@position
25127c478bd9Sstevel@tonic-gate  *	char op: operation, either MODINSERT_OP or MODREMOVE_OP.
25137c478bd9Sstevel@tonic-gate  *
25147c478bd9Sstevel@tonic-gate  * Return:
25157c478bd9Sstevel@tonic-gate  *	Before doing ip_domux2fd(), this function calls exit(1) in case of
25167c478bd9Sstevel@tonic-gate  *	error.  After ip_domux2fd() is done, it returns -1 for error, 0
25177c478bd9Sstevel@tonic-gate  *	otherwise.
25187c478bd9Sstevel@tonic-gate  */
25197c478bd9Sstevel@tonic-gate static int
25207c478bd9Sstevel@tonic-gate modop(char *arg, char op)
25217c478bd9Sstevel@tonic-gate {
25227c478bd9Sstevel@tonic-gate 	char *pos_p;
25237c478bd9Sstevel@tonic-gate 	int muxfd;
2524fc80c0dfSnordmark 	int muxid_fd;
25257c478bd9Sstevel@tonic-gate 	int ipfd_lowstr;  /* IP stream (lower stream of mux) to be plinked */
25267c478bd9Sstevel@tonic-gate 	int arpfd_lowstr; /* ARP stream (lower stream of mux) to be plinked */
25277c478bd9Sstevel@tonic-gate 	struct strmodconf mod;
25287c478bd9Sstevel@tonic-gate 	char *at_char = "@";
25297c478bd9Sstevel@tonic-gate 	char *arg_str;
25307c478bd9Sstevel@tonic-gate 	int orig_arpid;
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate 	/* Need to save the original string for -a option. */
25357c478bd9Sstevel@tonic-gate 	if ((arg_str = malloc(strlen(arg) + 1)) == NULL) {
25367c478bd9Sstevel@tonic-gate 		Perror0("cannot malloc");
25377c478bd9Sstevel@tonic-gate 		return (-1);
25387c478bd9Sstevel@tonic-gate 	}
25397c478bd9Sstevel@tonic-gate 	(void) strcpy(arg_str, arg);
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 	if (*arg_str == *at_char) {
25427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
25437c478bd9Sstevel@tonic-gate 		    "ifconfig: must supply a module name\n");
25447c478bd9Sstevel@tonic-gate 		exit(1);
25457c478bd9Sstevel@tonic-gate 	}
25467c478bd9Sstevel@tonic-gate 	mod.mod_name = strtok(arg_str, at_char);
25477c478bd9Sstevel@tonic-gate 	if (strlen(mod.mod_name) > FMNAMESZ) {
25487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: module name too long: %s\n",
25497c478bd9Sstevel@tonic-gate 		    mod.mod_name);
25507c478bd9Sstevel@tonic-gate 		exit(1);
25517c478bd9Sstevel@tonic-gate 	}
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	/*
25547c478bd9Sstevel@tonic-gate 	 * Need to make sure that the core TCP/IP stack modules are not
25557c478bd9Sstevel@tonic-gate 	 * removed.  Otherwise, "bad" things can happen.  If a module
25567c478bd9Sstevel@tonic-gate 	 * is removed and inserted back, it loses its old state.  But
25577c478bd9Sstevel@tonic-gate 	 * the modules above it still have the old state.  E.g. IP assumes
25587c478bd9Sstevel@tonic-gate 	 * fast data path while tunnel after re-inserted assumes that it can
25597c478bd9Sstevel@tonic-gate 	 * receive M_DATA only in fast data path for which it does not have
25607c478bd9Sstevel@tonic-gate 	 * any state.  This is a general caveat of _I_REMOVE/_I_INSERT.
25617c478bd9Sstevel@tonic-gate 	 */
25627c478bd9Sstevel@tonic-gate 	if (op == MODREMOVE_OP &&
25637c478bd9Sstevel@tonic-gate 	    (strcmp(mod.mod_name, ARP_MOD_NAME) == 0 ||
25647c478bd9Sstevel@tonic-gate 	    strcmp(mod.mod_name, IP_MOD_NAME) == 0 ||
25657c478bd9Sstevel@tonic-gate 	    strcmp(mod.mod_name, TUN_NAME) == 0 ||
25667c478bd9Sstevel@tonic-gate 	    strcmp(mod.mod_name, ATUN_NAME) == 0 ||
25677c478bd9Sstevel@tonic-gate 	    strcmp(mod.mod_name, TUN6TO4_NAME) == 0)) {
25687c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: cannot remove %s\n",
25697c478bd9Sstevel@tonic-gate 		    mod.mod_name);
25707c478bd9Sstevel@tonic-gate 		exit(1);
25717c478bd9Sstevel@tonic-gate 	}
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	if ((pos_p = strtok(NULL, at_char)) == NULL) {
25747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: must supply a position\n");
25757c478bd9Sstevel@tonic-gate 		exit(1);
25767c478bd9Sstevel@tonic-gate 	}
25777c478bd9Sstevel@tonic-gate 	mod.pos = atoi(pos_p);
25787c478bd9Sstevel@tonic-gate 
2579fc80c0dfSnordmark 	if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
25807c478bd9Sstevel@tonic-gate 	    &orig_arpid) < 0) {
25817c478bd9Sstevel@tonic-gate 		free(arg_str);
25827c478bd9Sstevel@tonic-gate 		return (-1);
25837c478bd9Sstevel@tonic-gate 	}
25847c478bd9Sstevel@tonic-gate 	switch (op) {
25857c478bd9Sstevel@tonic-gate 	case MODINSERT_OP:
25867c478bd9Sstevel@tonic-gate 		if (debug > 0) {
25877c478bd9Sstevel@tonic-gate 			(void) printf("Inserting module %s at %d\n",
25887c478bd9Sstevel@tonic-gate 			    mod.mod_name, mod.pos);
25897c478bd9Sstevel@tonic-gate 		}
25907c478bd9Sstevel@tonic-gate 		if (ioctl(ipfd_lowstr, _I_INSERT, (caddr_t)&mod) < 0) {
25917c478bd9Sstevel@tonic-gate 			Perror2("fail to insert module", mod.mod_name);
25927c478bd9Sstevel@tonic-gate 		}
25937c478bd9Sstevel@tonic-gate 		break;
25947c478bd9Sstevel@tonic-gate 	case MODREMOVE_OP:
25957c478bd9Sstevel@tonic-gate 		if (debug > 0) {
25967c478bd9Sstevel@tonic-gate 			(void) printf("Removing module %s at %d\n",
25977c478bd9Sstevel@tonic-gate 			    mod.mod_name, mod.pos);
25987c478bd9Sstevel@tonic-gate 		}
25997c478bd9Sstevel@tonic-gate 		if (ioctl(ipfd_lowstr, _I_REMOVE, (caddr_t)&mod) < 0) {
26007c478bd9Sstevel@tonic-gate 			Perror2("fail to remove module", mod.mod_name);
26017c478bd9Sstevel@tonic-gate 		}
26027c478bd9Sstevel@tonic-gate 		break;
26037c478bd9Sstevel@tonic-gate 	default:
26047c478bd9Sstevel@tonic-gate 		/* Should never get to here. */
26057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Unknown operation\n");
26067c478bd9Sstevel@tonic-gate 		break;
26077c478bd9Sstevel@tonic-gate 	}
26087c478bd9Sstevel@tonic-gate 	free(arg_str);
2609fc80c0dfSnordmark 	return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2610fc80c0dfSnordmark 	    orig_arpid));
26117c478bd9Sstevel@tonic-gate }
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate /*
26147c478bd9Sstevel@tonic-gate  * Set tunnel source address
26157c478bd9Sstevel@tonic-gate  */
26167c478bd9Sstevel@tonic-gate /* ARGSUSED */
26177c478bd9Sstevel@tonic-gate static int
26187c478bd9Sstevel@tonic-gate setiftsrc(char *addr, int64_t param)
26197c478bd9Sstevel@tonic-gate {
26207c478bd9Sstevel@tonic-gate 	return (settaddr(addr, icfg_set_tunnel_src));
26217c478bd9Sstevel@tonic-gate }
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate /*
26247c478bd9Sstevel@tonic-gate  * Set tunnel destination address
26257c478bd9Sstevel@tonic-gate  */
26267c478bd9Sstevel@tonic-gate /* ARGSUSED */
26277c478bd9Sstevel@tonic-gate static int
26287c478bd9Sstevel@tonic-gate setiftdst(char *addr, int64_t param)
26297c478bd9Sstevel@tonic-gate {
26307c478bd9Sstevel@tonic-gate 	return (settaddr(addr, icfg_set_tunnel_dest));
26317c478bd9Sstevel@tonic-gate }
26327c478bd9Sstevel@tonic-gate 
26337c478bd9Sstevel@tonic-gate /*
26347c478bd9Sstevel@tonic-gate  * sets tunnels src|dst address.  settaddr() expects the following:
26357c478bd9Sstevel@tonic-gate  * addr: Points to a printable string containing the address to be
26367c478bd9Sstevel@tonic-gate  *       set, e.g. 129.153.128.110.
26377c478bd9Sstevel@tonic-gate  * fn:   Pointer to a libinetcfg routine that will do the actual work.
26387c478bd9Sstevel@tonic-gate  *       The only valid functions are icfg_set_tunnel_src and
26397c478bd9Sstevel@tonic-gate  *       icfg_set_tunnel_dest.
26407c478bd9Sstevel@tonic-gate  */
26417c478bd9Sstevel@tonic-gate static int
26427c478bd9Sstevel@tonic-gate settaddr(char *addr,
26437c478bd9Sstevel@tonic-gate     int (*fn)(icfg_handle_t, const struct sockaddr *, socklen_t))
26447c478bd9Sstevel@tonic-gate {
26457c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
26467c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
26477c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
26487c478bd9Sstevel@tonic-gate 	int lower;
26497c478bd9Sstevel@tonic-gate 	int rc;
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
26527c478bd9Sstevel@tonic-gate 		errno = EPERM;
26537c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
26547c478bd9Sstevel@tonic-gate 	}
26557c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
26567c478bd9Sstevel@tonic-gate 	interface.if_protocol = SOCKET_AF(af);
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 	/* Open interface. */
26597c478bd9Sstevel@tonic-gate 	if ((rc = icfg_open(&handle, &interface)) != ICFG_SUCCESS)
26607c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate 	rc = icfg_get_tunnel_lower(handle, &lower);
26637c478bd9Sstevel@tonic-gate 	if (rc != ICFG_SUCCESS)
26647c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
26657c478bd9Sstevel@tonic-gate 
26667c478bd9Sstevel@tonic-gate 	if (lower == AF_INET) {
26677c478bd9Sstevel@tonic-gate 		in_getaddr(addr, (struct sockaddr *)&laddr, NULL);
26687c478bd9Sstevel@tonic-gate 	} else {
26697c478bd9Sstevel@tonic-gate 		in6_getaddr(addr, (struct sockaddr *)&laddr, NULL);
26707c478bd9Sstevel@tonic-gate 	}
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 	/* Call fn to do the real work, and close the interface. */
26737c478bd9Sstevel@tonic-gate 	rc = (*fn)(handle, (struct sockaddr *)&laddr,
26747c478bd9Sstevel@tonic-gate 	    sizeof (struct sockaddr_storage));
26757c478bd9Sstevel@tonic-gate 	icfg_close(handle);
26767c478bd9Sstevel@tonic-gate 
26777c478bd9Sstevel@tonic-gate 	if (rc != ICFG_SUCCESS)
26787c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
26797c478bd9Sstevel@tonic-gate 
26807c478bd9Sstevel@tonic-gate 	return (0);
26817c478bd9Sstevel@tonic-gate }
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate /* Set tunnel encapsulation limit. */
26847c478bd9Sstevel@tonic-gate /* ARGSUSED */
26857c478bd9Sstevel@tonic-gate static int
26867c478bd9Sstevel@tonic-gate set_tun_encap_limit(char *arg, int64_t param)
26877c478bd9Sstevel@tonic-gate {
26887c478bd9Sstevel@tonic-gate 	short limit;
26897c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
26907c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
26917c478bd9Sstevel@tonic-gate 	int rc;
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
26947c478bd9Sstevel@tonic-gate 		errno = EPERM;
26957c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
26967c478bd9Sstevel@tonic-gate 	}
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 	if ((sscanf(arg, "%hd", &limit) != 1) || (limit < 0) ||
26997c478bd9Sstevel@tonic-gate 	    (limit > 255)) {
27007c478bd9Sstevel@tonic-gate 		errno = EINVAL;
27017c478bd9Sstevel@tonic-gate 		Perror0_exit("Invalid encapsulation limit");
27027c478bd9Sstevel@tonic-gate 	}
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 	/* Open interface for configuration. */
27057c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
27067c478bd9Sstevel@tonic-gate 	interface.if_protocol = SOCKET_AF(af);
27077c478bd9Sstevel@tonic-gate 	if (icfg_open(&handle, &interface) != ICFG_SUCCESS)
27087c478bd9Sstevel@tonic-gate 		Perror0_exit("couldn't open interface");
27097c478bd9Sstevel@tonic-gate 
27107c478bd9Sstevel@tonic-gate 	rc = icfg_set_tunnel_encaplimit(handle, (int)limit);
27117c478bd9Sstevel@tonic-gate 	icfg_close(handle);
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate 	if (rc != ICFG_SUCCESS)
27147c478bd9Sstevel@tonic-gate 		Perror0_exit("Could not configure tunnel encapsulation limit");
27157c478bd9Sstevel@tonic-gate 
27167c478bd9Sstevel@tonic-gate 	return (0);
27177c478bd9Sstevel@tonic-gate }
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate /* Disable encapsulation limit. */
27207c478bd9Sstevel@tonic-gate /* ARGSUSED */
27217c478bd9Sstevel@tonic-gate static int
27227c478bd9Sstevel@tonic-gate clr_tun_encap_limit(char *arg, int64_t param)
27237c478bd9Sstevel@tonic-gate {
27247c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
27257c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
27267c478bd9Sstevel@tonic-gate 	int rc;
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
27297c478bd9Sstevel@tonic-gate 		errno = EPERM;
27307c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
27317c478bd9Sstevel@tonic-gate 	}
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate 	/* Open interface for configuration. */
27347c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
27357c478bd9Sstevel@tonic-gate 	interface.if_protocol = SOCKET_AF(af);
27367c478bd9Sstevel@tonic-gate 	if (icfg_open(&handle, &interface) != ICFG_SUCCESS)
27377c478bd9Sstevel@tonic-gate 		Perror0_exit("couldn't open interface");
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate 	rc = icfg_set_tunnel_encaplimit(handle, -1);
27407c478bd9Sstevel@tonic-gate 	icfg_close(handle);
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate 	if (rc != ICFG_SUCCESS)
27437c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate 	return (0);
27467c478bd9Sstevel@tonic-gate }
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate /* Set tunnel hop limit. */
27497c478bd9Sstevel@tonic-gate /* ARGSUSED */
27507c478bd9Sstevel@tonic-gate static int
27517c478bd9Sstevel@tonic-gate set_tun_hop_limit(char *arg, int64_t param)
27527c478bd9Sstevel@tonic-gate {
27537c478bd9Sstevel@tonic-gate 	unsigned short limit;
27547c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
27557c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
27567c478bd9Sstevel@tonic-gate 	int rc;
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
27597c478bd9Sstevel@tonic-gate 		errno = EPERM;
27607c478bd9Sstevel@tonic-gate 		Perror0_exit("Tunnel params on logical interfaces");
27617c478bd9Sstevel@tonic-gate 	}
27627c478bd9Sstevel@tonic-gate 
27637c478bd9Sstevel@tonic-gate 	/*
27647c478bd9Sstevel@tonic-gate 	 * Check limit here since it's really only an 8-bit unsigned quantity.
27657c478bd9Sstevel@tonic-gate 	 */
27667c478bd9Sstevel@tonic-gate 	if ((sscanf(arg, "%hu", &limit) != 1) || (limit > 255)) {
27677c478bd9Sstevel@tonic-gate 		errno = EINVAL;
27687c478bd9Sstevel@tonic-gate 		Perror0_exit("Invalid hop limit");
27697c478bd9Sstevel@tonic-gate 	}
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 	/* Open interface for configuration. */
27727c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
27737c478bd9Sstevel@tonic-gate 	interface.if_protocol = SOCKET_AF(af);
27747c478bd9Sstevel@tonic-gate 	if (icfg_open(&handle, &interface) != ICFG_SUCCESS)
27757c478bd9Sstevel@tonic-gate 		Perror0_exit("couldn't open interface");
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 	rc = icfg_set_tunnel_hoplimit(handle, (uint8_t)limit);
27787c478bd9Sstevel@tonic-gate 	icfg_close(handle);
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	if (rc != ICFG_SUCCESS)
27817c478bd9Sstevel@tonic-gate 		Perror0_exit("Could not configure tunnel hop limit");
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate 	return (0);
27847c478bd9Sstevel@tonic-gate }
27857c478bd9Sstevel@tonic-gate 
27867c478bd9Sstevel@tonic-gate /* Set zone ID */
27877c478bd9Sstevel@tonic-gate static int
27887c478bd9Sstevel@tonic-gate setzone(char *arg, int64_t param)
27897c478bd9Sstevel@tonic-gate {
27907c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = GLOBAL_ZONEID;
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 	if (param == NEXTARG) {
27937c478bd9Sstevel@tonic-gate 		/* zone must be active */
27947c478bd9Sstevel@tonic-gate 		if ((zoneid = getzoneidbyname(arg)) == -1) {
27957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
27967c478bd9Sstevel@tonic-gate 			    "ifconfig: unknown zone '%s'\n", arg);
27977c478bd9Sstevel@tonic-gate 			exit(1);
27987c478bd9Sstevel@tonic-gate 		}
27997c478bd9Sstevel@tonic-gate 	}
28007c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
28017c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zoneid;
28027c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
28037c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFZONE");
28047c478bd9Sstevel@tonic-gate 	return (0);
28057c478bd9Sstevel@tonic-gate }
28067c478bd9Sstevel@tonic-gate 
280745916cd2Sjpk /* Put interface into all zones */
280845916cd2Sjpk /* ARGSUSED */
280945916cd2Sjpk static int
281045916cd2Sjpk setallzones(char *arg, int64_t param)
281145916cd2Sjpk {
281245916cd2Sjpk 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
281345916cd2Sjpk 	lifr.lifr_zoneid = ALL_ZONES;
281445916cd2Sjpk 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
281545916cd2Sjpk 		Perror0_exit("SIOCSLIFZONE");
281645916cd2Sjpk 	return (0);
281745916cd2Sjpk }
281845916cd2Sjpk 
28197c478bd9Sstevel@tonic-gate /* Set source address to use */
28207c478bd9Sstevel@tonic-gate /* ARGSUSED */
28217c478bd9Sstevel@tonic-gate static int
28227c478bd9Sstevel@tonic-gate setifsrc(char *arg, int64_t param)
28237c478bd9Sstevel@tonic-gate {
28247c478bd9Sstevel@tonic-gate 	uint_t ifindex = 0;
28257c478bd9Sstevel@tonic-gate 	int rval;
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate 	/*
28307c478bd9Sstevel@tonic-gate 	 * Argument can be either an interface name or "none". The latter means
28317c478bd9Sstevel@tonic-gate 	 * that any previous selection is cleared.
28327c478bd9Sstevel@tonic-gate 	 */
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 	rval = strcmp(arg, name);
28357c478bd9Sstevel@tonic-gate 	if (rval == 0) {
28367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
28377c478bd9Sstevel@tonic-gate 		    "ifconfig: Cannot specify same interface for usesrc"
28387c478bd9Sstevel@tonic-gate 		    " group\n");
28397c478bd9Sstevel@tonic-gate 		exit(1);
28407c478bd9Sstevel@tonic-gate 	}
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 	rval = strcmp(arg, NONE_STR);
28437c478bd9Sstevel@tonic-gate 	if (rval != 0) {
28447c478bd9Sstevel@tonic-gate 		if ((ifindex = if_nametoindex(arg)) == 0) {
28457c478bd9Sstevel@tonic-gate 			(void) strncpy(lifr.lifr_name, arg, LIFNAMSIZ);
28467c478bd9Sstevel@tonic-gate 			Perror0_exit("Could not get interface index");
28477c478bd9Sstevel@tonic-gate 		}
28487c478bd9Sstevel@tonic-gate 		lifr.lifr_index = ifindex;
28497c478bd9Sstevel@tonic-gate 	} else {
28507c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) != 0)
28517c478bd9Sstevel@tonic-gate 			Perror0_exit("Not a valid usesrc consumer");
28527c478bd9Sstevel@tonic-gate 		lifr.lifr_index = 0;
28537c478bd9Sstevel@tonic-gate 	}
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 	if (debug)
28567c478bd9Sstevel@tonic-gate 		(void) printf("setifsrc: lifr_name %s, lifr_index %d\n",
28577c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, lifr.lifr_index);
28587c478bd9Sstevel@tonic-gate 
28597c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFUSESRC, (caddr_t)&lifr) == -1) {
28607c478bd9Sstevel@tonic-gate 		if (rval == 0)
28617c478bd9Sstevel@tonic-gate 			Perror0_exit("Cannot reset usesrc group");
28627c478bd9Sstevel@tonic-gate 		else
28637c478bd9Sstevel@tonic-gate 			Perror0_exit("Could not set source interface");
28647c478bd9Sstevel@tonic-gate 	}
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 	return (0);
28677c478bd9Sstevel@tonic-gate }
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate /*
28707c478bd9Sstevel@tonic-gate  * Print the interface status line associated with `ifname'
28717c478bd9Sstevel@tonic-gate  */
28727c478bd9Sstevel@tonic-gate static void
28737c478bd9Sstevel@tonic-gate ifstatus(const char *ifname)
28747c478bd9Sstevel@tonic-gate {
28757c478bd9Sstevel@tonic-gate 	uint64_t flags;
28767c478bd9Sstevel@tonic-gate 	char if_usesrc_name[LIFNAMSIZ];
28777c478bd9Sstevel@tonic-gate 	char *newbuf;
28787c478bd9Sstevel@tonic-gate 	int n, numifs, rval = 0;
28797c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp;
28807c478bd9Sstevel@tonic-gate 	struct lifsrcof lifs;
28817c478bd9Sstevel@tonic-gate 
28827c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
28837c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
28847c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
28857c478bd9Sstevel@tonic-gate 	}
28867c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate 	/*
28897c478bd9Sstevel@tonic-gate 	 * In V4 compatibility mode, we don't print the IFF_IPV4 flag or
28907c478bd9Sstevel@tonic-gate 	 * interfaces with IFF_IPV6 set.
28917c478bd9Sstevel@tonic-gate 	 */
28927c478bd9Sstevel@tonic-gate 	if (v4compat) {
28937c478bd9Sstevel@tonic-gate 		flags &= ~IFF_IPV4;
28947c478bd9Sstevel@tonic-gate 		if (flags & IFF_IPV6)
28957c478bd9Sstevel@tonic-gate 			return;
28967c478bd9Sstevel@tonic-gate 	}
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 	(void) printf("%s: ", ifname);
28997c478bd9Sstevel@tonic-gate 	print_flags(flags);
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
29027c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
29037c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFMETRIC");
29047c478bd9Sstevel@tonic-gate 	} else {
29057c478bd9Sstevel@tonic-gate 		if (lifr.lifr_metric)
29067c478bd9Sstevel@tonic-gate 			(void) printf(" metric %d", lifr.lifr_metric);
29077c478bd9Sstevel@tonic-gate 	}
29087c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
2909c08e5e1aSdr 		(void) printf(" mtu %u", lifr.lifr_mtu);
29107c478bd9Sstevel@tonic-gate 
29117c478bd9Sstevel@tonic-gate 	/* don't print index or zone when in compatibility mode */
29127c478bd9Sstevel@tonic-gate 	if (!v4compat) {
29137c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
29147c478bd9Sstevel@tonic-gate 			(void) printf(" index %d", lifr.lifr_index);
2915f4b3ec61Sdh 		/*
2916f4b3ec61Sdh 		 * Stack instances use GLOBAL_ZONEID for IP data structures
2917f4b3ec61Sdh 		 * even in the non-global zone.
2918f4b3ec61Sdh 		 */
29197c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifr) >= 0 &&
2920f4b3ec61Sdh 		    lifr.lifr_zoneid != getzoneid() &&
2921f4b3ec61Sdh 		    lifr.lifr_zoneid != GLOBAL_ZONEID) {
29227c478bd9Sstevel@tonic-gate 			char zone_name[ZONENAME_MAX];
29237c478bd9Sstevel@tonic-gate 
292445916cd2Sjpk 			if (lifr.lifr_zoneid == ALL_ZONES) {
292545916cd2Sjpk 				(void) printf("\n\tall-zones");
292645916cd2Sjpk 			} else if (getzonenamebyid(lifr.lifr_zoneid, zone_name,
29277c478bd9Sstevel@tonic-gate 			    sizeof (zone_name)) < 0) {
29287c478bd9Sstevel@tonic-gate 				(void) printf("\n\tzone %d", lifr.lifr_zoneid);
29297c478bd9Sstevel@tonic-gate 			} else {
29307c478bd9Sstevel@tonic-gate 				(void) printf("\n\tzone %s", zone_name);
29317c478bd9Sstevel@tonic-gate 			}
29327c478bd9Sstevel@tonic-gate 		}
29337c478bd9Sstevel@tonic-gate 	}
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) {
29367c478bd9Sstevel@tonic-gate 		lifs.lifs_ifindex = lifr.lifr_index;
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 		/*
29397c478bd9Sstevel@tonic-gate 		 * Find the number of interfaces that use this interfaces'
29407c478bd9Sstevel@tonic-gate 		 * address as a source address
29417c478bd9Sstevel@tonic-gate 		 */
29427c478bd9Sstevel@tonic-gate 		lifs.lifs_buf = NULL;
29437c478bd9Sstevel@tonic-gate 		lifs.lifs_maxlen = 0;
29447c478bd9Sstevel@tonic-gate 		for (;;) {
29457c478bd9Sstevel@tonic-gate 			/* The first pass will give the bufsize we need */
29467c478bd9Sstevel@tonic-gate 			rval = ioctl(s, SIOCGLIFSRCOF, (char *)&lifs);
29477c478bd9Sstevel@tonic-gate 			if (rval < 0) {
29487c478bd9Sstevel@tonic-gate 				if (lifs.lifs_buf != NULL) {
29497c478bd9Sstevel@tonic-gate 					free(lifs.lifs_buf);
29507c478bd9Sstevel@tonic-gate 					lifs.lifs_buf = NULL;
29517c478bd9Sstevel@tonic-gate 				}
29527c478bd9Sstevel@tonic-gate 				lifs.lifs_len = 0;
29537c478bd9Sstevel@tonic-gate 				break;
29547c478bd9Sstevel@tonic-gate 			}
29557c478bd9Sstevel@tonic-gate 			if (lifs.lifs_len <= lifs.lifs_maxlen)
29567c478bd9Sstevel@tonic-gate 				break;
29577c478bd9Sstevel@tonic-gate 			/* Use kernel's size + a small margin to avoid loops */
29587c478bd9Sstevel@tonic-gate 			lifs.lifs_maxlen = lifs.lifs_len +
29597c478bd9Sstevel@tonic-gate 			    5 * sizeof (struct lifreq);
29607c478bd9Sstevel@tonic-gate 			/* For the first pass, realloc acts like malloc */
29617c478bd9Sstevel@tonic-gate 			newbuf = realloc(lifs.lifs_buf, lifs.lifs_maxlen);
29627c478bd9Sstevel@tonic-gate 			if (newbuf == NULL) {
29637c478bd9Sstevel@tonic-gate 				if (lifs.lifs_buf != NULL) {
29647c478bd9Sstevel@tonic-gate 					free(lifs.lifs_buf);
29657c478bd9Sstevel@tonic-gate 					lifs.lifs_buf = NULL;
29667c478bd9Sstevel@tonic-gate 				}
29677c478bd9Sstevel@tonic-gate 				lifs.lifs_len = 0;
29687c478bd9Sstevel@tonic-gate 				break;
29697c478bd9Sstevel@tonic-gate 			}
29707c478bd9Sstevel@tonic-gate 			lifs.lifs_buf = newbuf;
29717c478bd9Sstevel@tonic-gate 		}
29727c478bd9Sstevel@tonic-gate 
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 		numifs = lifs.lifs_len / sizeof (struct lifreq);
29757c478bd9Sstevel@tonic-gate 		if (numifs > 0) {
29767c478bd9Sstevel@tonic-gate 			lifrp = lifs.lifs_req;
29777c478bd9Sstevel@tonic-gate 			(void) printf("\n\tsrcof");
29787c478bd9Sstevel@tonic-gate 			for (n = numifs; n > 0; n--, lifrp++) {
29797c478bd9Sstevel@tonic-gate 				(void) printf(" %s", lifrp->lifr_name);
29807c478bd9Sstevel@tonic-gate 			}
29817c478bd9Sstevel@tonic-gate 		}
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate 		if (lifs.lifs_buf != NULL)
29847c478bd9Sstevel@tonic-gate 			free(lifs.lifs_buf);
29857c478bd9Sstevel@tonic-gate 	}
29867c478bd9Sstevel@tonic-gate 
29877c478bd9Sstevel@tonic-gate 	/* Find the interface whose source address this interface uses */
29887c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
29897c478bd9Sstevel@tonic-gate 		if (lifr.lifr_index != 0) {
29907c478bd9Sstevel@tonic-gate 			if (if_indextoname(lifr.lifr_index,
29917c478bd9Sstevel@tonic-gate 			    if_usesrc_name) == NULL) {
29927c478bd9Sstevel@tonic-gate 				(void) printf("\n\tusesrc ifIndex %d",
29937c478bd9Sstevel@tonic-gate 				    lifr.lifr_index);
29947c478bd9Sstevel@tonic-gate 			} else {
29957c478bd9Sstevel@tonic-gate 				(void) printf("\n\tusesrc %s", if_usesrc_name);
29967c478bd9Sstevel@tonic-gate 			}
29977c478bd9Sstevel@tonic-gate 		}
29987c478bd9Sstevel@tonic-gate 	}
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
30017c478bd9Sstevel@tonic-gate }
30027c478bd9Sstevel@tonic-gate 
30037c478bd9Sstevel@tonic-gate 
30047c478bd9Sstevel@tonic-gate /*
30057c478bd9Sstevel@tonic-gate  * Print the status of the interface.  If an address family was
30067c478bd9Sstevel@tonic-gate  * specified, show it and it only; otherwise, show them all.
30077c478bd9Sstevel@tonic-gate  */
30087c478bd9Sstevel@tonic-gate static void
30097c478bd9Sstevel@tonic-gate status(void)
30107c478bd9Sstevel@tonic-gate {
30117c478bd9Sstevel@tonic-gate 	struct afswtch *p = afp;
30127c478bd9Sstevel@tonic-gate 	uint64_t flags;
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
30157c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
30167c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
30177c478bd9Sstevel@tonic-gate 	}
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate 	/*
30227c478bd9Sstevel@tonic-gate 	 * Only print the interface status if the address family matches
30237c478bd9Sstevel@tonic-gate 	 * the interface family flag.
30247c478bd9Sstevel@tonic-gate 	 */
30257c478bd9Sstevel@tonic-gate 	if (p != NULL) {
30267c478bd9Sstevel@tonic-gate 		if (((p->af_af == AF_INET6) && (flags & IFF_IPV4)) ||
30277c478bd9Sstevel@tonic-gate 		    ((p->af_af == AF_INET) && (flags & IFF_IPV6)))
30287c478bd9Sstevel@tonic-gate 			return;
30297c478bd9Sstevel@tonic-gate 	}
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate 	/*
30327c478bd9Sstevel@tonic-gate 	 * In V4 compatibility mode, don't print IFF_IPV6 interfaces.
30337c478bd9Sstevel@tonic-gate 	 */
30347c478bd9Sstevel@tonic-gate 	if (v4compat && (flags & IFF_IPV6))
30357c478bd9Sstevel@tonic-gate 		return;
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 	ifstatus(name);
30387c478bd9Sstevel@tonic-gate 
30397c478bd9Sstevel@tonic-gate 	if (p != NULL) {
30407c478bd9Sstevel@tonic-gate 		(*p->af_status)(1, flags);
30417c478bd9Sstevel@tonic-gate 	} else {
30427c478bd9Sstevel@tonic-gate 		for (p = afs; p->af_name; p++) {
30437c478bd9Sstevel@tonic-gate 			(void) close(s);
30447c478bd9Sstevel@tonic-gate 			s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0);
30457c478bd9Sstevel@tonic-gate 			/* set global af for use in p->af_status */
30467c478bd9Sstevel@tonic-gate 			af = p->af_af;
30477c478bd9Sstevel@tonic-gate 			if (s == -1) {
30487c478bd9Sstevel@tonic-gate 				Perror0_exit("socket");
30497c478bd9Sstevel@tonic-gate 			}
30507c478bd9Sstevel@tonic-gate 			(*p->af_status)(0, flags);
30517c478bd9Sstevel@tonic-gate 		}
30527c478bd9Sstevel@tonic-gate 
30537c478bd9Sstevel@tonic-gate 		/*
30547c478bd9Sstevel@tonic-gate 		 * Historically, 'ether' has been an address family,
30557c478bd9Sstevel@tonic-gate 		 * so print it here.
30567c478bd9Sstevel@tonic-gate 		 */
30577c478bd9Sstevel@tonic-gate 		print_ifether(name);
30587c478bd9Sstevel@tonic-gate 	}
30597c478bd9Sstevel@tonic-gate }
30607c478bd9Sstevel@tonic-gate 
30617c478bd9Sstevel@tonic-gate /*
30627c478bd9Sstevel@tonic-gate  * Print the status of the interface in a format that can be used to
30637c478bd9Sstevel@tonic-gate  * reconfigure the interface later. Code stolen from status() above.
30647c478bd9Sstevel@tonic-gate  */
30657c478bd9Sstevel@tonic-gate /* ARGSUSED */
30667c478bd9Sstevel@tonic-gate static int
30677c478bd9Sstevel@tonic-gate configinfo(char *null, int64_t param)
30687c478bd9Sstevel@tonic-gate {
30697c478bd9Sstevel@tonic-gate 	struct afswtch *p = afp;
30707c478bd9Sstevel@tonic-gate 	uint64_t flags;
30717c478bd9Sstevel@tonic-gate 	char phydevname[LIFNAMSIZ];
30727c478bd9Sstevel@tonic-gate 	char if_usesrc_name[LIFNAMSIZ];
30737c478bd9Sstevel@tonic-gate 	char *cp;
30747c478bd9Sstevel@tonic-gate 
30757c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
30767c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
30777c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
30787c478bd9Sstevel@tonic-gate 	}
30797c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 	if (debug) {
30827c478bd9Sstevel@tonic-gate 		(void) printf("configinfo: name %s flags  0x%llx af_af %d\n",
30837c478bd9Sstevel@tonic-gate 		    name, flags, p != NULL ? p->af_af : -1);
30847c478bd9Sstevel@tonic-gate 	}
30857c478bd9Sstevel@tonic-gate 
30867c478bd9Sstevel@tonic-gate 	/* remove LIF component */
30877c478bd9Sstevel@tonic-gate 	(void) strncpy(phydevname, name, sizeof (phydevname));
30887c478bd9Sstevel@tonic-gate 	cp = strchr(phydevname, ':');
30897c478bd9Sstevel@tonic-gate 	if (cp) {
30907c478bd9Sstevel@tonic-gate 		*cp = 0;
30917c478bd9Sstevel@tonic-gate 	}
30927c478bd9Sstevel@tonic-gate 	phydevname[sizeof (phydevname) - 1] = '\0';
30937c478bd9Sstevel@tonic-gate 
30947c478bd9Sstevel@tonic-gate 	/*
30957c478bd9Sstevel@tonic-gate 	 * if the interface is IPv4
30967c478bd9Sstevel@tonic-gate 	 *	if we have a IPv6 address family restriction return
30977c478bd9Sstevel@tonic-gate 	 *		so it won't print
30987c478bd9Sstevel@tonic-gate 	 *	if we are in IPv4 compatibility mode, clear out IFF_IPV4
30997c478bd9Sstevel@tonic-gate 	 *		so we don't print it.
31007c478bd9Sstevel@tonic-gate 	 */
31017c478bd9Sstevel@tonic-gate 	if (flags & IFF_IPV4) {
31027c478bd9Sstevel@tonic-gate 		if (p && p->af_af == AF_INET6)
31037c478bd9Sstevel@tonic-gate 			return (-1);
31047c478bd9Sstevel@tonic-gate 		if (v4compat)
31057c478bd9Sstevel@tonic-gate 			flags &= ~IFF_IPV4;
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate 		(void) printf("%s inet plumb", phydevname);
31087c478bd9Sstevel@tonic-gate 	} else if (flags & IFF_IPV6) {
31097c478bd9Sstevel@tonic-gate 		/*
31107c478bd9Sstevel@tonic-gate 		 * else if the interface is IPv6
31117c478bd9Sstevel@tonic-gate 		 *	if we have a IPv4 address family restriction return
31127c478bd9Sstevel@tonic-gate 		 *	or we are in IPv4 compatibiltiy mode, return.
31137c478bd9Sstevel@tonic-gate 		 */
31147c478bd9Sstevel@tonic-gate 		if (p && p->af_af == AF_INET)
31157c478bd9Sstevel@tonic-gate 			return (-1);
31167c478bd9Sstevel@tonic-gate 		if (v4compat)
31177c478bd9Sstevel@tonic-gate 			return (-1);
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate 		(void) printf("%s inet6 plumb", phydevname);
31207c478bd9Sstevel@tonic-gate 	}
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
31237c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
31247c478bd9Sstevel@tonic-gate 		Perror0_exit("configinfo: SIOCGLIFMETRIC");
31257c478bd9Sstevel@tonic-gate 	} else {
31267c478bd9Sstevel@tonic-gate 		if (lifr.lifr_metric)
31277c478bd9Sstevel@tonic-gate 			(void) printf(" metric %d ", lifr.lifr_metric);
31287c478bd9Sstevel@tonic-gate 	}
31297c478bd9Sstevel@tonic-gate 	if (((flags & (IFF_VIRTUAL|IFF_LOOPBACK)) != IFF_VIRTUAL) &&
3130fc80c0dfSnordmark 	    ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
31317c478bd9Sstevel@tonic-gate 		(void) printf(" mtu %d", lifr.lifr_metric);
31327c478bd9Sstevel@tonic-gate 
31337c478bd9Sstevel@tonic-gate 	/* don't print index when in compatibility mode */
31347c478bd9Sstevel@tonic-gate 	if (!v4compat) {
31357c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
31367c478bd9Sstevel@tonic-gate 			(void) printf(" index %d", lifr.lifr_index);
31377c478bd9Sstevel@tonic-gate 	}
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
31407c478bd9Sstevel@tonic-gate 		if (lifr.lifr_index != 0) {
31417c478bd9Sstevel@tonic-gate 			if (if_indextoname(lifr.lifr_index,
31427c478bd9Sstevel@tonic-gate 			    if_usesrc_name) != NULL) {
31437c478bd9Sstevel@tonic-gate 				(void) printf(" usesrc %s", if_usesrc_name);
31447c478bd9Sstevel@tonic-gate 			}
31457c478bd9Sstevel@tonic-gate 		}
31467c478bd9Sstevel@tonic-gate 	}
31477c478bd9Sstevel@tonic-gate 
31487c478bd9Sstevel@tonic-gate 	if (p != NULL) {
31497c478bd9Sstevel@tonic-gate 		(*p->af_configinfo)(1, flags);
31507c478bd9Sstevel@tonic-gate 	} else {
31517c478bd9Sstevel@tonic-gate 		for (p = afs; p->af_name; p++) {
31527c478bd9Sstevel@tonic-gate 			(void) close(s);
31537c478bd9Sstevel@tonic-gate 			s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0);
31547c478bd9Sstevel@tonic-gate 			/* set global af for use in p->af_configinfo */
31557c478bd9Sstevel@tonic-gate 			af = p->af_af;
31567c478bd9Sstevel@tonic-gate 			if (s == -1) {
31577c478bd9Sstevel@tonic-gate 				Perror0_exit("socket");
31587c478bd9Sstevel@tonic-gate 			}
31597c478bd9Sstevel@tonic-gate 			(*p->af_configinfo)(0, flags);
31607c478bd9Sstevel@tonic-gate 		}
31617c478bd9Sstevel@tonic-gate 	}
31627c478bd9Sstevel@tonic-gate 
31637c478bd9Sstevel@tonic-gate 	(void) printf("\n");
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate 	return (0);
31667c478bd9Sstevel@tonic-gate }
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate static void
31697c478bd9Sstevel@tonic-gate print_tsec(struct iftun_req *tparams)
31707c478bd9Sstevel@tonic-gate {
31717c478bd9Sstevel@tonic-gate 	ipsec_req_t *ipsr;
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 	(void) printf("\ttunnel security settings  ");
31747c478bd9Sstevel@tonic-gate 	/*
31757c478bd9Sstevel@tonic-gate 	 * Deal with versioning, for now just point
31767c478bd9Sstevel@tonic-gate 	 * an ipsec_req_t at ifta_secinfo.  If versions
31777c478bd9Sstevel@tonic-gate 	 * change, something else will overlay ifta_secinfo.
31787c478bd9Sstevel@tonic-gate 	 */
31797c478bd9Sstevel@tonic-gate 	assert(tparams->ifta_vers == IFTUN_VERSION);
31807c478bd9Sstevel@tonic-gate 
31818810c16bSdanmcd 	if (tparams->ifta_flags & IFTUN_COMPLEX_SECURITY) {
31828810c16bSdanmcd 		(void) printf("-->  use 'ipsecconf -ln -i %s'",
31838810c16bSdanmcd 		    tparams->ifta_lifr_name);
31848810c16bSdanmcd 	} else {
31858810c16bSdanmcd 		ipsr = (ipsec_req_t *)(&tparams->ifta_secinfo);
31868810c16bSdanmcd 		if (ipsr->ipsr_ah_req & IPSEC_PREF_REQUIRED) {
31878810c16bSdanmcd 			(void) printf("ah (%s)  ",
31888810c16bSdanmcd 			    rparsealg(ipsr->ipsr_auth_alg, IPSEC_PROTO_AH));
31898810c16bSdanmcd 		}
31908810c16bSdanmcd 		if (ipsr->ipsr_esp_req & IPSEC_PREF_REQUIRED) {
31918810c16bSdanmcd 			(void) printf("esp (%s",
31928810c16bSdanmcd 			    rparsealg(ipsr->ipsr_esp_alg, IPSEC_PROTO_ESP));
31938810c16bSdanmcd 			(void) printf("/%s)",
31948810c16bSdanmcd 			    rparsealg(ipsr->ipsr_esp_auth_alg, IPSEC_PROTO_AH));
31958810c16bSdanmcd 		}
31967c478bd9Sstevel@tonic-gate 	}
31977c478bd9Sstevel@tonic-gate 	(void) printf("\n");
31987c478bd9Sstevel@tonic-gate }
31997c478bd9Sstevel@tonic-gate 
32007c478bd9Sstevel@tonic-gate static void
32017c478bd9Sstevel@tonic-gate tun_status(void)
32027c478bd9Sstevel@tonic-gate {
32037c478bd9Sstevel@tonic-gate 	icfg_if_t interface;
32047c478bd9Sstevel@tonic-gate 	int rc;
32057c478bd9Sstevel@tonic-gate 	icfg_handle_t handle;
32067c478bd9Sstevel@tonic-gate 	int protocol;
32077c478bd9Sstevel@tonic-gate 	char srcbuf[INET6_ADDRSTRLEN];
32087c478bd9Sstevel@tonic-gate 	char dstbuf[INET6_ADDRSTRLEN];
32097c478bd9Sstevel@tonic-gate 	boolean_t tabbed;
32107c478bd9Sstevel@tonic-gate 	uint8_t hoplimit;
32117c478bd9Sstevel@tonic-gate 	int16_t encaplimit;
32127c478bd9Sstevel@tonic-gate 	struct sockaddr_storage taddr;
32137c478bd9Sstevel@tonic-gate 	socklen_t socklen = sizeof (taddr);
32147c478bd9Sstevel@tonic-gate 
32157c478bd9Sstevel@tonic-gate 	(void) strncpy(interface.if_name, name, sizeof (interface.if_name));
32167c478bd9Sstevel@tonic-gate 	interface.if_protocol = SOCKET_AF(af);
32177c478bd9Sstevel@tonic-gate 	if ((rc = icfg_open(&handle, &interface)) != ICFG_SUCCESS)
32187c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate 	/*
32217c478bd9Sstevel@tonic-gate 	 * only print tunnel info for lun 0.  If ioctl fails, assume
32227c478bd9Sstevel@tonic-gate 	 * we are not a tunnel
32237c478bd9Sstevel@tonic-gate 	 */
32247c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL ||
32257c478bd9Sstevel@tonic-gate 	    icfg_get_tunnel_lower(handle, &protocol) != ICFG_SUCCESS) {
32267c478bd9Sstevel@tonic-gate 		icfg_close(handle);
32277c478bd9Sstevel@tonic-gate 		return;
32287c478bd9Sstevel@tonic-gate 	}
32297c478bd9Sstevel@tonic-gate 
32307c478bd9Sstevel@tonic-gate 	switch (protocol) {
32317c478bd9Sstevel@tonic-gate 	case AF_INET:
32327c478bd9Sstevel@tonic-gate 		(void) printf("\tinet");
32337c478bd9Sstevel@tonic-gate 		break;
32347c478bd9Sstevel@tonic-gate 	case AF_INET6:
32357c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6");
32367c478bd9Sstevel@tonic-gate 		break;
32377c478bd9Sstevel@tonic-gate 	default:
32387c478bd9Sstevel@tonic-gate 		Perror0_exit("\ttunnel: Illegal lower stream\n\t");
32397c478bd9Sstevel@tonic-gate 		break;
32407c478bd9Sstevel@tonic-gate 	}
32417c478bd9Sstevel@tonic-gate 
32427c478bd9Sstevel@tonic-gate 	rc = icfg_get_tunnel_src(handle, (struct sockaddr *)&taddr, &socklen);
32437c478bd9Sstevel@tonic-gate 	if (rc == ICFG_NOT_SET) {
32447c478bd9Sstevel@tonic-gate 		(void) strlcpy(srcbuf, (protocol == AF_INET) ? "0.0.0.0" :
32457c478bd9Sstevel@tonic-gate 		    "::", sizeof (srcbuf));
32467c478bd9Sstevel@tonic-gate 	} else if (rc != ICFG_SUCCESS) {
32477c478bd9Sstevel@tonic-gate 		Perror0_exit((char *)icfg_errmsg(rc));
32487c478bd9Sstevel@tonic-gate 	} else {
32497c478bd9Sstevel@tonic-gate 		rc = icfg_sockaddr_to_str(protocol, (struct sockaddr *)&taddr,
32507c478bd9Sstevel@tonic-gate 		    srcbuf, sizeof (srcbuf));
32517c478bd9Sstevel@tonic-gate 		if (rc != ICFG_SUCCESS) {
32527c478bd9Sstevel@tonic-gate 			Perror0_exit((char *)icfg_errmsg(rc));
32537c478bd9Sstevel@tonic-gate 		}
32547c478bd9Sstevel@tonic-gate 	}
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate 	(void) printf(" tunnel src %s ", srcbuf);
32577c478bd9Sstevel@tonic-gate 
32587c478bd9Sstevel@tonic-gate 	rc = icfg_get_tunnel_dest(handle, (struct sockaddr *)&taddr, &socklen);
32597c478bd9Sstevel@tonic-gate 	if (rc == ICFG_NOT_SET) {
32607c478bd9Sstevel@tonic-gate 		(void) printf("\n");
32617c478bd9Sstevel@tonic-gate 	} else {
32627c478bd9Sstevel@tonic-gate 		rc = icfg_sockaddr_to_str(protocol, (struct sockaddr *)&taddr,
32637c478bd9Sstevel@tonic-gate 		    dstbuf, sizeof (dstbuf));
32647c478bd9Sstevel@tonic-gate 		if (rc != ICFG_SUCCESS) {
32657c478bd9Sstevel@tonic-gate 			Perror0_exit((char *)icfg_errmsg(rc));
32667c478bd9Sstevel@tonic-gate 		}
32677c478bd9Sstevel@tonic-gate 		(void) printf("tunnel dst %s\n", dstbuf);
32687c478bd9Sstevel@tonic-gate 	}
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate 	if (handle->ifh_tunnel_params != NULL &&
32717c478bd9Sstevel@tonic-gate 	    (handle->ifh_tunnel_params->ifta_flags & IFTUN_SECURITY))
32727c478bd9Sstevel@tonic-gate 		print_tsec(handle->ifh_tunnel_params);
32737c478bd9Sstevel@tonic-gate 
32747c478bd9Sstevel@tonic-gate 	/*
32757c478bd9Sstevel@tonic-gate 	 * tabbed indicates tabbed and printed.  Use it tell us whether
32767c478bd9Sstevel@tonic-gate 	 * to tab and that we've printed something here, so we need a
32777c478bd9Sstevel@tonic-gate 	 * newline
32787c478bd9Sstevel@tonic-gate 	 */
32797c478bd9Sstevel@tonic-gate 	tabbed = _B_FALSE;
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate 	if (icfg_get_tunnel_hoplimit(handle, &hoplimit) == ICFG_SUCCESS) {
32827c478bd9Sstevel@tonic-gate 		(void) printf("\ttunnel hop limit %d ", hoplimit);
32837c478bd9Sstevel@tonic-gate 		tabbed = _B_TRUE;
32847c478bd9Sstevel@tonic-gate 	}
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate 	if ((protocol == AF_INET6) &&
32877c478bd9Sstevel@tonic-gate 	    (icfg_get_tunnel_encaplimit(handle, &encaplimit) ==
3288fc80c0dfSnordmark 	    ICFG_SUCCESS)) {
32897c478bd9Sstevel@tonic-gate 		if (!tabbed) {
32907c478bd9Sstevel@tonic-gate 			(void) printf("\t");
32917c478bd9Sstevel@tonic-gate 			tabbed = _B_TRUE;
32927c478bd9Sstevel@tonic-gate 		}
32937c478bd9Sstevel@tonic-gate 		if (encaplimit >= 0) {
32947c478bd9Sstevel@tonic-gate 			(void) printf("tunnel encapsulation limit %d",
32957c478bd9Sstevel@tonic-gate 			    encaplimit);
32967c478bd9Sstevel@tonic-gate 		} else {
32977c478bd9Sstevel@tonic-gate 			(void) printf("tunnel encapsulation limit disabled");
32987c478bd9Sstevel@tonic-gate 		}
32997c478bd9Sstevel@tonic-gate 	}
33007c478bd9Sstevel@tonic-gate 
33017c478bd9Sstevel@tonic-gate 	if (tabbed)
33027c478bd9Sstevel@tonic-gate 		(void) printf("\n");
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate 	icfg_close(handle);
33057c478bd9Sstevel@tonic-gate }
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate static void
33087c478bd9Sstevel@tonic-gate in_status(int force, uint64_t flags)
33097c478bd9Sstevel@tonic-gate {
33107c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin, *laddr;
33117c478bd9Sstevel@tonic-gate 	struct	sockaddr_in netmask = { AF_INET };
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate 	if (debug)
33147c478bd9Sstevel@tonic-gate 		(void) printf("in_status(%s) flags 0x%llx\n", name, flags);
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate 	/* only print status for IPv4 interfaces */
33177c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV4))
33187c478bd9Sstevel@tonic-gate 		return;
33197c478bd9Sstevel@tonic-gate 
33207c478bd9Sstevel@tonic-gate 	/* if the interface is a tunnel, print the tunnel status */
33217c478bd9Sstevel@tonic-gate 	tun_status();
33227c478bd9Sstevel@tonic-gate 
33237c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
33247c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33257c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
33267c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
33277c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
33287c478bd9Sstevel@tonic-gate 				if (!force)
33297c478bd9Sstevel@tonic-gate 					return;
33307c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33317c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33327c478bd9Sstevel@tonic-gate 			} else
33337c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status: SIOCGLIFADDR");
33347c478bd9Sstevel@tonic-gate 		}
33357c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
33367c478bd9Sstevel@tonic-gate 		(void) printf("\tinet %s ", inet_ntoa(sin->sin_addr));
33377c478bd9Sstevel@tonic-gate 		laddr = sin;
33387c478bd9Sstevel@tonic-gate 	} else {
33397c478bd9Sstevel@tonic-gate 		(void) printf("\tinet ");
33407c478bd9Sstevel@tonic-gate 	}
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33437c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
33447c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
33457c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
33467c478bd9Sstevel@tonic-gate 			if (!force)
33477c478bd9Sstevel@tonic-gate 				return;
33487c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
33497c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
33507c478bd9Sstevel@tonic-gate 		} else {
33517c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status: SIOCGLIFSUBNET");
33527c478bd9Sstevel@tonic-gate 		}
33537c478bd9Sstevel@tonic-gate 	}
33547c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
33557c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
33567c478bd9Sstevel@tonic-gate 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
33577c478bd9Sstevel@tonic-gate 		(void) printf("subnet %s/%d ", inet_ntoa(sin->sin_addr),
33587c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
33597c478bd9Sstevel@tonic-gate 	}
33607c478bd9Sstevel@tonic-gate 	if (sin->sin_family != AF_INET) {
33617c478bd9Sstevel@tonic-gate 		(void) printf("Wrong family: %d\n", sin->sin_family);
33627c478bd9Sstevel@tonic-gate 	}
33637c478bd9Sstevel@tonic-gate 
33647c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33657c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
33667c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL)
33677c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status: SIOCGLIFNETMASK");
33687c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
33697c478bd9Sstevel@tonic-gate 	} else
33707c478bd9Sstevel@tonic-gate 		netmask.sin_addr =
33717c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
33727c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
33737c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33747c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
33757c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
33767c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33777c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33787c478bd9Sstevel@tonic-gate 			else
3379fc80c0dfSnordmark 				Perror0_exit("in_status: SIOCGLIFDSTADDR");
33807c478bd9Sstevel@tonic-gate 		}
33817c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
33827c478bd9Sstevel@tonic-gate 		(void) printf("--> %s ", inet_ntoa(sin->sin_addr));
33837c478bd9Sstevel@tonic-gate 	}
33847c478bd9Sstevel@tonic-gate 	(void) printf("netmask %x ", ntohl(netmask.sin_addr.s_addr));
33857c478bd9Sstevel@tonic-gate 	if (flags & IFF_BROADCAST) {
33867c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33877c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
33887c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
33897c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33907c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33917c478bd9Sstevel@tonic-gate 			else
3392fc80c0dfSnordmark 				Perror0_exit("in_status: SIOCGLIFBRDADDR");
33937c478bd9Sstevel@tonic-gate 		}
33947c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
33957c478bd9Sstevel@tonic-gate 		if (sin->sin_addr.s_addr != 0) {
33967c478bd9Sstevel@tonic-gate 			(void) printf("broadcast %s",
33977c478bd9Sstevel@tonic-gate 			    inet_ntoa(sin->sin_addr));
33987c478bd9Sstevel@tonic-gate 		}
33997c478bd9Sstevel@tonic-gate 	}
34007c478bd9Sstevel@tonic-gate 	/* If there is a groupname, print it for lun 0 alone */
34017c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') == NULL) {
34027c478bd9Sstevel@tonic-gate 		(void) memset(lifr.lifr_groupname, 0,
34037c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_groupname));
34047c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr) >= 0) {
34057c478bd9Sstevel@tonic-gate 			if (strlen(lifr.lifr_groupname) > 0) {
34067c478bd9Sstevel@tonic-gate 				(void) printf("\n\tgroupname %s",
34077c478bd9Sstevel@tonic-gate 				    lifr.lifr_groupname);
34087c478bd9Sstevel@tonic-gate 			}
34097c478bd9Sstevel@tonic-gate 		}
34107c478bd9Sstevel@tonic-gate 	}
34117c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
34127c478bd9Sstevel@tonic-gate }
34137c478bd9Sstevel@tonic-gate 
34147c478bd9Sstevel@tonic-gate static void
34157c478bd9Sstevel@tonic-gate in6_status(int force, uint64_t flags)
34167c478bd9Sstevel@tonic-gate {
34177c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
34187c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6, *laddr6;
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate 	if (debug)
34217c478bd9Sstevel@tonic-gate 		(void) printf("in6_status(%s) flags 0x%llx\n", name, flags);
34227c478bd9Sstevel@tonic-gate 
34237c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV6))
34247c478bd9Sstevel@tonic-gate 		return;
34257c478bd9Sstevel@tonic-gate 
34267c478bd9Sstevel@tonic-gate 	/* if the interface is a tunnel, print the tunnel status */
34277c478bd9Sstevel@tonic-gate 	tun_status();
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
34307c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34317c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
34327c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
34337c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
34347c478bd9Sstevel@tonic-gate 				if (!force)
34357c478bd9Sstevel@tonic-gate 					return;
34367c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34377c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34387c478bd9Sstevel@tonic-gate 			} else
34397c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status6: SIOCGLIFADDR");
34407c478bd9Sstevel@tonic-gate 		}
34417c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
34427c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6 %s/%d ",
34437c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
34447c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
34457c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
34467c478bd9Sstevel@tonic-gate 		laddr6 = sin6;
34477c478bd9Sstevel@tonic-gate 	} else {
34487c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6 ");
34497c478bd9Sstevel@tonic-gate 	}
34507c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34517c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
34527c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
34537c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
34547c478bd9Sstevel@tonic-gate 			if (!force)
34557c478bd9Sstevel@tonic-gate 				return;
34567c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
34577c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
34587c478bd9Sstevel@tonic-gate 		} else
34597c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status6: SIOCGLIFSUBNET");
34607c478bd9Sstevel@tonic-gate 	}
34617c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
34627c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
34637c478bd9Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
34647c478bd9Sstevel@tonic-gate 		(void) printf("subnet %s/%d ",
34657c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
34667c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
34677c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
34687c478bd9Sstevel@tonic-gate 	}
34697c478bd9Sstevel@tonic-gate 	if (sin6->sin6_family != AF_INET6) {
34707c478bd9Sstevel@tonic-gate 		(void) printf("Wrong family: %d\n", sin6->sin6_family);
34717c478bd9Sstevel@tonic-gate 	}
34727c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
34737c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34747c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
34757c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
34767c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34777c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34787c478bd9Sstevel@tonic-gate 			else
3479fc80c0dfSnordmark 				Perror0_exit("in_status6: SIOCGLIFDSTADDR");
34807c478bd9Sstevel@tonic-gate 		}
34817c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
34827c478bd9Sstevel@tonic-gate 		(void) printf("--> %s ",
34837c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
34847c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)));
34857c478bd9Sstevel@tonic-gate 	}
34867c478bd9Sstevel@tonic-gate 	if (verbose) {
34877c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
34887c478bd9Sstevel@tonic-gate 		(void) putchar('\t');
34897c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34907c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
34917c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EINVAL)
34927c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34937c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34947c478bd9Sstevel@tonic-gate 			else
3495fc80c0dfSnordmark 				Perror0_exit("in_status6: SIOCGLIFTOKEN");
34967c478bd9Sstevel@tonic-gate 		} else {
34977c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
34987c478bd9Sstevel@tonic-gate 			(void) printf("token %s/%d ",
34997c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35007c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
35017c478bd9Sstevel@tonic-gate 			    lifr.lifr_addrlen);
35027c478bd9Sstevel@tonic-gate 		}
35037c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFLNKINFO, (caddr_t)&lifr) < 0) {
35047c478bd9Sstevel@tonic-gate 			if (errno != EINVAL) {
35057c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status6: SIOCGLIFLNKINFO");
35067c478bd9Sstevel@tonic-gate 			}
35077c478bd9Sstevel@tonic-gate 		} else {
35087c478bd9Sstevel@tonic-gate 			(void) printf("maxhops %u, reachtime %u ms, "
35097c478bd9Sstevel@tonic-gate 			    "reachretrans %u ms, maxmtu %u ",
35107c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_maxhops,
35117c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_reachtime,
35127c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_reachretrans,
35137c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_maxmtu);
35147c478bd9Sstevel@tonic-gate 		}
35157c478bd9Sstevel@tonic-gate 	}
3516f7d61273Smeem 	/* If there is a groupname, print it for only the physical interface */
35177c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') == NULL) {
3518f7d61273Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3519f7d61273Smeem 		    lifr.lifr_groupname[0] != '\0') {
3520f7d61273Smeem 			(void) printf("\n\tgroupname %s", lifr.lifr_groupname);
35217c478bd9Sstevel@tonic-gate 		}
35227c478bd9Sstevel@tonic-gate 	}
35237c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
35247c478bd9Sstevel@tonic-gate }
35257c478bd9Sstevel@tonic-gate 
35267c478bd9Sstevel@tonic-gate static void
35277c478bd9Sstevel@tonic-gate in_configinfo(int force, uint64_t flags)
35287c478bd9Sstevel@tonic-gate {
35297c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin, *laddr;
35307c478bd9Sstevel@tonic-gate 	struct	sockaddr_in netmask = { AF_INET };
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate 	if (debug)
35337c478bd9Sstevel@tonic-gate 		(void) printf("in_configinfo(%s) flags 0x%llx\n", name, flags);
35347c478bd9Sstevel@tonic-gate 
35357c478bd9Sstevel@tonic-gate 	/* only configinfo info for IPv4 interfaces */
35367c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV4))
35377c478bd9Sstevel@tonic-gate 		return;
35387c478bd9Sstevel@tonic-gate 
35397c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
35407c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35417c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
35427c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
35437c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
35447c478bd9Sstevel@tonic-gate 				if (!force)
35457c478bd9Sstevel@tonic-gate 					return;
35467c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
35477c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
35487c478bd9Sstevel@tonic-gate 			} else
35497c478bd9Sstevel@tonic-gate 				Perror0_exit("in_configinfo: SIOCGLIFADDR");
35507c478bd9Sstevel@tonic-gate 		}
35517c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3552f7d61273Smeem 		if (strchr(name, ':') != NULL) {
35537c478bd9Sstevel@tonic-gate 			(void) printf(" addif %s ", inet_ntoa(sin->sin_addr));
35547c478bd9Sstevel@tonic-gate 		} else {
35557c478bd9Sstevel@tonic-gate 			(void) printf(" set %s ", inet_ntoa(sin->sin_addr));
35567c478bd9Sstevel@tonic-gate 		}
35577c478bd9Sstevel@tonic-gate 		laddr = sin;
35587c478bd9Sstevel@tonic-gate 	}
35597c478bd9Sstevel@tonic-gate 
35607c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35617c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
35627c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
35637c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
35647c478bd9Sstevel@tonic-gate 			if (!force)
35657c478bd9Sstevel@tonic-gate 				return;
35667c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
35677c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
35687c478bd9Sstevel@tonic-gate 		} else {
35697c478bd9Sstevel@tonic-gate 			Perror0_exit("in_configinfo: SIOCGLIFSUBNET");
35707c478bd9Sstevel@tonic-gate 		}
35717c478bd9Sstevel@tonic-gate 	}
35727c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
35737c478bd9Sstevel@tonic-gate 
35747c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
35757c478bd9Sstevel@tonic-gate 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
35767c478bd9Sstevel@tonic-gate 		(void) printf(" subnet %s/%d ", inet_ntoa(sin->sin_addr),
35777c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
35787c478bd9Sstevel@tonic-gate 	}
35797c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35807c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
35817c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL)
35827c478bd9Sstevel@tonic-gate 			Perror0_exit("in_configinfo: SIOCGLIFNETMASK");
35837c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
35847c478bd9Sstevel@tonic-gate 	} else
35857c478bd9Sstevel@tonic-gate 		netmask.sin_addr =
35867c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
35877c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
35887c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35897c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
35907c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
35917c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
35927c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
35937c478bd9Sstevel@tonic-gate 			else
3594fc80c0dfSnordmark 				Perror0_exit("in_configinfo: SIOCGLIFDSTADDR");
35957c478bd9Sstevel@tonic-gate 		}
35967c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
35977c478bd9Sstevel@tonic-gate 		(void) printf(" destination %s ", inet_ntoa(sin->sin_addr));
35987c478bd9Sstevel@tonic-gate 	}
35997c478bd9Sstevel@tonic-gate 	(void) printf(" netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
36007c478bd9Sstevel@tonic-gate 	if (flags & IFF_BROADCAST) {
36017c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
36027c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
36037c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
36047c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
36057c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
36067c478bd9Sstevel@tonic-gate 			else
3607fc80c0dfSnordmark 				Perror0_exit("in_configinfo: SIOCGLIFBRDADDR");
36087c478bd9Sstevel@tonic-gate 		}
36097c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
36107c478bd9Sstevel@tonic-gate 		if (sin->sin_addr.s_addr != 0) {
36117c478bd9Sstevel@tonic-gate 			(void) printf(" broadcast %s ",
36127c478bd9Sstevel@tonic-gate 			    inet_ntoa(sin->sin_addr));
36137c478bd9Sstevel@tonic-gate 		}
36147c478bd9Sstevel@tonic-gate 	}
36157c478bd9Sstevel@tonic-gate 
3616f7d61273Smeem 	/* If there is a groupname, print it for only the physical interface */
3617f7d61273Smeem 	if (strchr(name, ':') == NULL) {
3618f7d61273Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3619f7d61273Smeem 		    lifr.lifr_groupname[0] != '\0') {
3620f7d61273Smeem 			(void) printf(" group %s ", lifr.lifr_groupname);
36217c478bd9Sstevel@tonic-gate 		}
36227c478bd9Sstevel@tonic-gate 	}
36237c478bd9Sstevel@tonic-gate 
36247c478bd9Sstevel@tonic-gate 	/* Print flags to configure */
36257c478bd9Sstevel@tonic-gate 	print_config_flags(flags);
36267c478bd9Sstevel@tonic-gate 
36277c478bd9Sstevel@tonic-gate 	/* IFF_NOARP applies to AF_INET only */
36287c478bd9Sstevel@tonic-gate 	if (flags & IFF_NOARP) {
36297c478bd9Sstevel@tonic-gate 		(void) printf("-arp ");
36307c478bd9Sstevel@tonic-gate 	}
36317c478bd9Sstevel@tonic-gate }
36327c478bd9Sstevel@tonic-gate 
36337c478bd9Sstevel@tonic-gate static void
36347c478bd9Sstevel@tonic-gate in6_configinfo(int force, uint64_t flags)
36357c478bd9Sstevel@tonic-gate {
36367c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
36377c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6, *laddr6;
36387c478bd9Sstevel@tonic-gate 
36397c478bd9Sstevel@tonic-gate 	if (debug)
36407c478bd9Sstevel@tonic-gate 		(void) printf("in6_configinfo(%s) flags 0x%llx\n", name,
36417c478bd9Sstevel@tonic-gate 		    flags);
36427c478bd9Sstevel@tonic-gate 
36437c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV6))
36447c478bd9Sstevel@tonic-gate 		return;
36457c478bd9Sstevel@tonic-gate 
36467c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
36477c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
36487c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
36497c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
36507c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
36517c478bd9Sstevel@tonic-gate 				if (!force)
36527c478bd9Sstevel@tonic-gate 					return;
36537c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
36547c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
36557c478bd9Sstevel@tonic-gate 			} else
36567c478bd9Sstevel@tonic-gate 				Perror0_exit("in6_configinfo: SIOCGLIFADDR");
36577c478bd9Sstevel@tonic-gate 		}
36587c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3659f7d61273Smeem 		if (strchr(name, ':') != NULL) {
36607c478bd9Sstevel@tonic-gate 			(void) printf(" addif %s/%d ",
36617c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
36627c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
36637c478bd9Sstevel@tonic-gate 			    lifr.lifr_addrlen);
36647c478bd9Sstevel@tonic-gate 		} else {
36657c478bd9Sstevel@tonic-gate 			(void) printf(" set %s/%d ",
36667c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
36677c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
36687c478bd9Sstevel@tonic-gate 			    lifr.lifr_addrlen);
36697c478bd9Sstevel@tonic-gate 		}
36707c478bd9Sstevel@tonic-gate 		laddr6 = sin6;
36717c478bd9Sstevel@tonic-gate 	}
36727c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
36737c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
36747c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
36757c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
36767c478bd9Sstevel@tonic-gate 			if (!force)
36777c478bd9Sstevel@tonic-gate 				return;
36787c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
36797c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
36807c478bd9Sstevel@tonic-gate 		} else
36817c478bd9Sstevel@tonic-gate 			Perror0_exit("in6_configinfo: SIOCGLIFSUBNET");
36827c478bd9Sstevel@tonic-gate 	}
36837c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
36847c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
36857c478bd9Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
36867c478bd9Sstevel@tonic-gate 		(void) printf(" subnet %s/%d ",
36877c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
36887c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
36897c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
36907c478bd9Sstevel@tonic-gate 	}
36917c478bd9Sstevel@tonic-gate 
36927c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
36937c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
36947c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
36957c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
36967c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
36977c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
36987c478bd9Sstevel@tonic-gate 			else
3699fc80c0dfSnordmark 				Perror0_exit("in6_configinfo: SIOCGLIFDSTADDR");
37007c478bd9Sstevel@tonic-gate 		}
37017c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
37027c478bd9Sstevel@tonic-gate 		(void) printf(" destination %s ",
37037c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
37047c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)));
37057c478bd9Sstevel@tonic-gate 	}
37067c478bd9Sstevel@tonic-gate 
37077c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
37087c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
37097c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EINVAL)
37107c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
37117c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
37127c478bd9Sstevel@tonic-gate 		else
3713fc80c0dfSnordmark 			Perror0_exit("in6_configinfo: SIOCGLIFTOKEN");
37147c478bd9Sstevel@tonic-gate 	} else {
37157c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
37167c478bd9Sstevel@tonic-gate 		(void) printf(" token %s/%d ",
37177c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
37187c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
37197c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
37207c478bd9Sstevel@tonic-gate 	}
37217c478bd9Sstevel@tonic-gate 
3722f7d61273Smeem 	/* If there is a groupname, print it for only the physical interface */
3723f7d61273Smeem 	if (strchr(name, ':') == NULL) {
3724f7d61273Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3725f7d61273Smeem 		    lifr.lifr_groupname[0] != '\0') {
3726f7d61273Smeem 			(void) printf(" group %s ", lifr.lifr_groupname);
37277c478bd9Sstevel@tonic-gate 		}
37287c478bd9Sstevel@tonic-gate 	}
37297c478bd9Sstevel@tonic-gate 
37307c478bd9Sstevel@tonic-gate 	/* Print flags to configure */
37317c478bd9Sstevel@tonic-gate 	print_config_flags(flags);
37327c478bd9Sstevel@tonic-gate 
37337c478bd9Sstevel@tonic-gate 	/* IFF_NONUD applies to AF_INET6 only */
37347c478bd9Sstevel@tonic-gate 	if (flags & IFF_NONUD) {
37357c478bd9Sstevel@tonic-gate 		(void) printf("-nud ");
37367c478bd9Sstevel@tonic-gate 	}
37377c478bd9Sstevel@tonic-gate }
37387c478bd9Sstevel@tonic-gate 
37397c478bd9Sstevel@tonic-gate /*
37407c478bd9Sstevel@tonic-gate  * We need to plink both the arp-device stream and the arp-ip-device stream.
37417c478bd9Sstevel@tonic-gate  * However the muxid is stored only in IP. Plumbing 2 streams individually
37427c478bd9Sstevel@tonic-gate  * is not atomic, and if ifconfig is killed, the resulting plumbing can
37437c478bd9Sstevel@tonic-gate  * be inconsistent. For eg. if only the arp stream is plumbed, we have lost
37447c478bd9Sstevel@tonic-gate  * the muxid, and the half-baked plumbing can neither be unplumbed nor
37457c478bd9Sstevel@tonic-gate  * replumbed, thus requiring a reboot. To avoid the above the following
37467c478bd9Sstevel@tonic-gate  * scheme is used.
37477c478bd9Sstevel@tonic-gate  *
37487c478bd9Sstevel@tonic-gate  * Ifconfig asks IP to enforce atomicity of plumbing the arp and IP streams.
37497c478bd9Sstevel@tonic-gate  * This is done by pushing arp on to the mux (/dev/udp). ARP adds some
37507c478bd9Sstevel@tonic-gate  * extra information in the I_PLINK and I_PUNLINK ioctls to let IP know
37517c478bd9Sstevel@tonic-gate  * that the plumbing/unplumbing has to be done atomically. Ifconfig plumbs
37527c478bd9Sstevel@tonic-gate  * the IP stream first, and unplumbs it last. The kernel (IP) does not
37537c478bd9Sstevel@tonic-gate  * allow IP stream to be unplumbed without unplumbing arp stream. Similarly
37547c478bd9Sstevel@tonic-gate  * it does not allow arp stream to be plumbed before IP stream is plumbed.
37557c478bd9Sstevel@tonic-gate  * There is no need to use SIOCSLIFMUXID, since the whole operation is atomic,
37567c478bd9Sstevel@tonic-gate  * and IP uses the info in the I_PLINK message to get the muxid.
37577c478bd9Sstevel@tonic-gate  *
37587c478bd9Sstevel@tonic-gate  * a. STREAMS does not allow us to use /dev/ip itself as the mux. So we use
3759fc80c0dfSnordmark  *    /dev/udp{,6}.
37607c478bd9Sstevel@tonic-gate  * b. SIOCGLIFMUXID returns the muxid corresponding to the V4 or V6 stream
37617c478bd9Sstevel@tonic-gate  *    depending on the open i.e. V4 vs V6 open. So we need to use /dev/udp
3762fc80c0dfSnordmark  *    or /dev/udp6 for SIOCGLIFMUXID and SIOCSLIFMUXID.
37637c478bd9Sstevel@tonic-gate  * c. We need to push ARP in order to get the required kernel support for
37647c478bd9Sstevel@tonic-gate  *    atomic plumbings. The actual work done by ARP is explained in arp.c
37657c478bd9Sstevel@tonic-gate  *    Without pushing ARP, we will still be able to plumb/unplumb. But
37667c478bd9Sstevel@tonic-gate  *    it is not atomic, and is supported by the kernel for backward
37677c478bd9Sstevel@tonic-gate  *    compatibility for other utilities like atmifconfig etc. In this case
37687c478bd9Sstevel@tonic-gate  *    the utility must use SIOCSLIFMUXID.
37697c478bd9Sstevel@tonic-gate  */
37707c478bd9Sstevel@tonic-gate static void
3771c7e4935fSss plumb_one_device(int af)
37727c478bd9Sstevel@tonic-gate {
37737c478bd9Sstevel@tonic-gate 	int	arp_muxid = -1, ip_muxid;
3774c7e4935fSss 	int	mux_fd, ip_fd, arp_fd;
3775c7e4935fSss 	int 	retval;
3776c7e4935fSss 	uint_t	ppa;
37777c478bd9Sstevel@tonic-gate 	char	*udp_dev_name;
3778c7e4935fSss 	char    provider[DLPI_LINKNAME_MAX];
3779c7e4935fSss 	dlpi_handle_t	dh_arp, dh_ip;
37807c478bd9Sstevel@tonic-gate 
3781c7e4935fSss 	/*
3782c7e4935fSss 	 * We use DLPI_NOATTACH because the ip module will do the attach
3783c7e4935fSss 	 * itself for DLPI style-2 devices.
3784c7e4935fSss 	 */
3785c7e4935fSss 	retval = dlpi_open(name, &dh_ip, DLPI_NOATTACH);
3786c7e4935fSss 	if (retval != DLPI_SUCCESS)
3787c7e4935fSss 		Perrdlpi_exit("cannot open link", name, retval);
37887c478bd9Sstevel@tonic-gate 
3789c7e4935fSss 	if ((retval = dlpi_parselink(name, provider, &ppa)) != DLPI_SUCCESS)
3790c7e4935fSss 		Perrdlpi_exit("dlpi_parselink", name, retval);
37917c478bd9Sstevel@tonic-gate 
3792c7e4935fSss 	if (debug) {
3793c7e4935fSss 		(void) printf("ifconfig: plumb_one_device: provider %s,"
3794c7e4935fSss 		    " ppa %u\n", provider, ppa);
3795c7e4935fSss 	}
3796c7e4935fSss 
3797c7e4935fSss 	ip_fd = dlpi_fd(dh_ip);
37987c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, I_PUSH, IP_MOD_NAME) == -1)
37997c478bd9Sstevel@tonic-gate 		Perror2_exit("I_PUSH", IP_MOD_NAME);
38007c478bd9Sstevel@tonic-gate 
38017c478bd9Sstevel@tonic-gate 	/*
38027c478bd9Sstevel@tonic-gate 	 * Push the ARP module onto the interface stream. IP uses
38037c478bd9Sstevel@tonic-gate 	 * this to send resolution requests up to ARP. We need to
38047c478bd9Sstevel@tonic-gate 	 * do this before the SLIFNAME ioctl is sent down because
38057c478bd9Sstevel@tonic-gate 	 * the interface becomes publicly known as soon as the SLIFNAME
38067c478bd9Sstevel@tonic-gate 	 * ioctl completes. Thus some other process trying to bring up
38077c478bd9Sstevel@tonic-gate 	 * the interface after SLIFNAME but before we have pushed ARP
38087c478bd9Sstevel@tonic-gate 	 * could hang. We pop the module again later if it is not needed.
38097c478bd9Sstevel@tonic-gate 	 */
38107c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, I_PUSH, ARP_MOD_NAME) == -1)
38117c478bd9Sstevel@tonic-gate 		Perror2_exit("I_PUSH", ARP_MOD_NAME);
38127c478bd9Sstevel@tonic-gate 
38137c478bd9Sstevel@tonic-gate 	/*
38147c478bd9Sstevel@tonic-gate 	 * Set IFF_IPV4/IFF_IPV6 flags.
38157c478bd9Sstevel@tonic-gate 	 * At this point in time the kernel also allows an
38167c478bd9Sstevel@tonic-gate 	 * override of the CANTCHANGE flags.
38177c478bd9Sstevel@tonic-gate 	 */
38187c478bd9Sstevel@tonic-gate 	lifr.lifr_name[0] = '\0';
38197c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, SIOCGLIFFLAGS, (char *)&lifr) == -1)
38207c478bd9Sstevel@tonic-gate 		Perror0_exit("plumb_one_device: SIOCGLIFFLAGS");
38217c478bd9Sstevel@tonic-gate 
38227c478bd9Sstevel@tonic-gate 	/* Set the name string and the IFF_IPV* flag */
38237c478bd9Sstevel@tonic-gate 	if (af == AF_INET6) {
38247c478bd9Sstevel@tonic-gate 		lifr.lifr_flags |= IFF_IPV6;
38257c478bd9Sstevel@tonic-gate 		lifr.lifr_flags &= ~(IFF_BROADCAST | IFF_IPV4);
38267c478bd9Sstevel@tonic-gate 	} else {
38277c478bd9Sstevel@tonic-gate 		lifr.lifr_flags |= IFF_IPV4;
38287c478bd9Sstevel@tonic-gate 		lifr.lifr_flags &= ~IFF_IPV6;
38297c478bd9Sstevel@tonic-gate 	}
38307c478bd9Sstevel@tonic-gate 
38317c478bd9Sstevel@tonic-gate 	/* record the device and module names as interface name */
3832c7e4935fSss 	lifr.lifr_ppa = ppa;
38337c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
38347c478bd9Sstevel@tonic-gate 
38357c478bd9Sstevel@tonic-gate 	/* set the interface name */
38367c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, SIOCSLIFNAME, (char *)&lifr) == -1) {
38377c478bd9Sstevel@tonic-gate 		if (errno != EEXIST)
38387c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCSLIFNAME for ip");
38397c478bd9Sstevel@tonic-gate 		/*
38407c478bd9Sstevel@tonic-gate 		 * This difference between the way we behave for EEXIST
38417c478bd9Sstevel@tonic-gate 		 * and that with other errors exists to preserve legacy
3842f7d61273Smeem 		 * behaviour. Earlier when foreachinterface() and matchif()
38437c478bd9Sstevel@tonic-gate 		 * were doing the duplicate interface name checks, for
38447c478bd9Sstevel@tonic-gate 		 * already existing interfaces, inetplumb() returned "0".
38457c478bd9Sstevel@tonic-gate 		 * To preserve this behaviour, Perror0() and return are
38467c478bd9Sstevel@tonic-gate 		 * called for EEXIST.
38477c478bd9Sstevel@tonic-gate 		 */
38487c478bd9Sstevel@tonic-gate 		Perror0("SIOCSLIFNAME for ip");
38497c478bd9Sstevel@tonic-gate 		return;
38507c478bd9Sstevel@tonic-gate 	}
38517c478bd9Sstevel@tonic-gate 
38527c478bd9Sstevel@tonic-gate 	/* Get the full set of existing flags for this stream */
38537c478bd9Sstevel@tonic-gate 	if (ioctl(ip_fd, SIOCGLIFFLAGS, (char *)&lifr) == -1)
38547c478bd9Sstevel@tonic-gate 		Perror0_exit("plumb_one_device: SIOCFLIFFLAGS");
38557c478bd9Sstevel@tonic-gate 
38567c478bd9Sstevel@tonic-gate 	if (debug) {
3857c7e4935fSss 		(void) printf("ifconfig: plumb_one_device: %s got flags:\n",
38587c478bd9Sstevel@tonic-gate 		    lifr.lifr_name);
38597c478bd9Sstevel@tonic-gate 		print_flags(lifr.lifr_flags);
38607c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
38617c478bd9Sstevel@tonic-gate 	}
38627c478bd9Sstevel@tonic-gate 
38637c478bd9Sstevel@tonic-gate 	/* Check if arp is not actually needed */
38647c478bd9Sstevel@tonic-gate 	if (lifr.lifr_flags & (IFF_NOARP|IFF_IPV6)) {
38657c478bd9Sstevel@tonic-gate 		if (ioctl(ip_fd, I_POP, 0) == -1)
38667c478bd9Sstevel@tonic-gate 			Perror2_exit("I_POP", ARP_MOD_NAME);
38677c478bd9Sstevel@tonic-gate 	}
38687c478bd9Sstevel@tonic-gate 
38697c478bd9Sstevel@tonic-gate 	/*
38707c478bd9Sstevel@tonic-gate 	 * Open "/dev/udp" for use as a multiplexor to PLINK the
38717c478bd9Sstevel@tonic-gate 	 * interface stream under. We use "/dev/udp" instead of "/dev/ip"
38727c478bd9Sstevel@tonic-gate 	 * since STREAMS will not let you PLINK a driver under itself,
38737c478bd9Sstevel@tonic-gate 	 * and "/dev/ip" is typically the driver at the bottom of
38747c478bd9Sstevel@tonic-gate 	 * the stream for tunneling interfaces.
38757c478bd9Sstevel@tonic-gate 	 */
38767c478bd9Sstevel@tonic-gate 	if (af == AF_INET6)
38777c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP6_DEV_NAME;
38787c478bd9Sstevel@tonic-gate 	else
38797c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP_DEV_NAME;
38807c478bd9Sstevel@tonic-gate 	if ((mux_fd = open_arp_on_udp(udp_dev_name)) == -1)
38817c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
38827c478bd9Sstevel@tonic-gate 
38837c478bd9Sstevel@tonic-gate 	/* Check if arp is not needed */
38847c478bd9Sstevel@tonic-gate 	if (lifr.lifr_flags & (IFF_NOARP|IFF_IPV6)) {
38857c478bd9Sstevel@tonic-gate 		/*
38867c478bd9Sstevel@tonic-gate 		 * PLINK the interface stream so that ifconfig can exit
38877c478bd9Sstevel@tonic-gate 		 * without tearing down the stream.
38887c478bd9Sstevel@tonic-gate 		 */
3889c7e4935fSss 		if ((ip_muxid = ioctl(mux_fd, I_PLINK, ip_fd)) == -1)
38907c478bd9Sstevel@tonic-gate 			Perror0_exit("I_PLINK for ip");
38917c478bd9Sstevel@tonic-gate 		(void) close(mux_fd);
38927c478bd9Sstevel@tonic-gate 		return;
38937c478bd9Sstevel@tonic-gate 	}
38947c478bd9Sstevel@tonic-gate 
38957c478bd9Sstevel@tonic-gate 	/*
38967c478bd9Sstevel@tonic-gate 	 * This interface does use ARP, so set up a separate stream
38977c478bd9Sstevel@tonic-gate 	 * from the interface to ARP.
38987c478bd9Sstevel@tonic-gate 	 *
38997c478bd9Sstevel@tonic-gate 	 * Note: modules specified by the user are pushed
39007c478bd9Sstevel@tonic-gate 	 * only on the interface stream, not on the ARP stream.
39017c478bd9Sstevel@tonic-gate 	 */
39027c478bd9Sstevel@tonic-gate 	if (debug)
3903c7e4935fSss 		(void) printf("ifconfig: plumb_one_device: ifname: %s\n", name);
39047c478bd9Sstevel@tonic-gate 
3905c7e4935fSss 	/*
3906c7e4935fSss 	 * We use DLPI_NOATTACH because the arp module will do the attach
3907c7e4935fSss 	 * itself for DLPI style-2 devices.
3908c7e4935fSss 	 */
3909c7e4935fSss 	retval = dlpi_open(name, &dh_arp, DLPI_NOATTACH);
3910c7e4935fSss 	if (retval != DLPI_SUCCESS)
3911c7e4935fSss 		Perrdlpi_exit("cannot open link", name, retval);
39127c478bd9Sstevel@tonic-gate 
3913c7e4935fSss 	arp_fd = dlpi_fd(dh_arp);
39147c478bd9Sstevel@tonic-gate 	if (ioctl(arp_fd, I_PUSH, ARP_MOD_NAME) == -1)
39157c478bd9Sstevel@tonic-gate 		Perror2_exit("I_PUSH", ARP_MOD_NAME);
39167c478bd9Sstevel@tonic-gate 
39177c478bd9Sstevel@tonic-gate 	/*
39187c478bd9Sstevel@tonic-gate 	 * Tell ARP the name and unit number for this interface.
39197c478bd9Sstevel@tonic-gate 	 * Note that arp has no support for transparent ioctls.
39207c478bd9Sstevel@tonic-gate 	 */
39217c478bd9Sstevel@tonic-gate 	if (strioctl(arp_fd, SIOCSLIFNAME, (char *)&lifr,
39227c478bd9Sstevel@tonic-gate 	    sizeof (lifr)) == -1) {
39237c478bd9Sstevel@tonic-gate 		if (errno != EEXIST)
39247c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCSLIFNAME for arp");
39257c478bd9Sstevel@tonic-gate 		Perror0("SIOCSLIFNAME for arp");
3926c7e4935fSss 		dlpi_close(dh_arp);
3927c7e4935fSss 		dlpi_close(dh_ip);
39287c478bd9Sstevel@tonic-gate 		(void) close(mux_fd);
39297c478bd9Sstevel@tonic-gate 		return;
39307c478bd9Sstevel@tonic-gate 	}
39317c478bd9Sstevel@tonic-gate 	/*
39327c478bd9Sstevel@tonic-gate 	 * PLINK the IP and ARP streams so that ifconfig can exit
39337c478bd9Sstevel@tonic-gate 	 * without tearing down the stream.
39347c478bd9Sstevel@tonic-gate 	 */
3935c7e4935fSss 	if ((ip_muxid = ioctl(mux_fd, I_PLINK, ip_fd)) == -1)
39367c478bd9Sstevel@tonic-gate 		Perror0_exit("I_PLINK for ip");
39377c478bd9Sstevel@tonic-gate 	if ((arp_muxid = ioctl(mux_fd, I_PLINK, arp_fd)) == -1) {
39387c478bd9Sstevel@tonic-gate 		(void) ioctl(mux_fd, I_PUNLINK, ip_muxid);
39397c478bd9Sstevel@tonic-gate 		Perror0_exit("I_PLINK for arp");
39407c478bd9Sstevel@tonic-gate 	}
39417c478bd9Sstevel@tonic-gate 
39427c478bd9Sstevel@tonic-gate 	if (debug)
39437c478bd9Sstevel@tonic-gate 		(void) printf("arp muxid = %d\n", arp_muxid);
3944c7e4935fSss 	dlpi_close(dh_ip);
3945c7e4935fSss 	dlpi_close(dh_arp);
39467c478bd9Sstevel@tonic-gate 	(void) close(mux_fd);
39477c478bd9Sstevel@tonic-gate }
39487c478bd9Sstevel@tonic-gate 
39497c478bd9Sstevel@tonic-gate 
39507c478bd9Sstevel@tonic-gate /*
39517c478bd9Sstevel@tonic-gate  * If this is a physical interface then remove it.
39527c478bd9Sstevel@tonic-gate  * If it is a logical interface name use SIOCLIFREMOVEIF to
39537c478bd9Sstevel@tonic-gate  * remove it. In both cases fail if it doesn't exist.
39547c478bd9Sstevel@tonic-gate  */
39557c478bd9Sstevel@tonic-gate /* ARGSUSED */
39567c478bd9Sstevel@tonic-gate static int
39577c478bd9Sstevel@tonic-gate inetunplumb(char *arg, int64_t param)
39587c478bd9Sstevel@tonic-gate {
39597c478bd9Sstevel@tonic-gate 	int ip_muxid, arp_muxid;
39607c478bd9Sstevel@tonic-gate 	int mux_fd;
3961fc80c0dfSnordmark 	int muxid_fd;
39627c478bd9Sstevel@tonic-gate 	char *udp_dev_name;
39637c478bd9Sstevel@tonic-gate 	char *strptr;
39647c478bd9Sstevel@tonic-gate 	uint64_t flags;
39657c478bd9Sstevel@tonic-gate 	boolean_t changed_arp_muxid = _B_FALSE;
39667c478bd9Sstevel@tonic-gate 	int save_errno;
39677c478bd9Sstevel@tonic-gate 
39687c478bd9Sstevel@tonic-gate 	strptr = strchr(name, ':');
39697c478bd9Sstevel@tonic-gate 	if (strptr != NULL || strcmp(name, LOOPBACK_IF) == 0) {
39707c478bd9Sstevel@tonic-gate 		/* Can't unplumb logical interface zero */
39717c478bd9Sstevel@tonic-gate 		if (strptr != NULL && strcmp(strptr, ":0") == 0) {
39727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: unplumb:"
39737c478bd9Sstevel@tonic-gate 			    " Cannot unplumb %s: Invalid interface\n", name);
39747c478bd9Sstevel@tonic-gate 			exit(1);
39757c478bd9Sstevel@tonic-gate 		}
39767c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
39777c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
39787c478bd9Sstevel@tonic-gate 
39797c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0)
39807c478bd9Sstevel@tonic-gate 			Perror0_exit("unplumb: SIOCLIFREMOVEIF");
39817c478bd9Sstevel@tonic-gate 		return (0);
39827c478bd9Sstevel@tonic-gate 	}
39837c478bd9Sstevel@tonic-gate 
39847c478bd9Sstevel@tonic-gate 	/*
39857c478bd9Sstevel@tonic-gate 	 * We used /dev/udp or udp6 to set up the mux. So we have to use
39867c478bd9Sstevel@tonic-gate 	 * the same now for PUNLINK also.
39877c478bd9Sstevel@tonic-gate 	 */
39887c478bd9Sstevel@tonic-gate 	if (afp->af_af == AF_INET6)
39897c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP6_DEV_NAME;
39907c478bd9Sstevel@tonic-gate 	else
39917c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP_DEV_NAME;
39927c478bd9Sstevel@tonic-gate 
3993fc80c0dfSnordmark 	if ((muxid_fd = open(udp_dev_name, O_RDWR)) == -1)
3994fc80c0dfSnordmark 		exit(EXIT_FAILURE);
3995fc80c0dfSnordmark 
39967c478bd9Sstevel@tonic-gate 	if ((mux_fd = open_arp_on_udp(udp_dev_name)) == -1)
39977c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
39987c478bd9Sstevel@tonic-gate 
39997c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
4000fc80c0dfSnordmark 	if (ioctl(muxid_fd, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
40017c478bd9Sstevel@tonic-gate 		Perror0_exit("unplumb: SIOCGLIFFLAGS");
40027c478bd9Sstevel@tonic-gate 	}
40037c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
4004fc80c0dfSnordmark 	if (ioctl(muxid_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
40057c478bd9Sstevel@tonic-gate 		Perror0_exit("unplumb: SIOCGLIFMUXID");
40067c478bd9Sstevel@tonic-gate 	}
40077c478bd9Sstevel@tonic-gate 	arp_muxid = lifr.lifr_arp_muxid;
40087c478bd9Sstevel@tonic-gate 	ip_muxid = lifr.lifr_ip_muxid;
40097c478bd9Sstevel@tonic-gate 	/*
40107c478bd9Sstevel@tonic-gate 	 * We don't have a good way of knowing whether the arp stream is
40117c478bd9Sstevel@tonic-gate 	 * plumbed. We can't rely on IFF_NOARP because someone could
40127c478bd9Sstevel@tonic-gate 	 * have turned it off later using "ifconfig xxx -arp".
40137c478bd9Sstevel@tonic-gate 	 */
40147c478bd9Sstevel@tonic-gate 	if (arp_muxid != 0) {
40157c478bd9Sstevel@tonic-gate 		if (debug)
40167c478bd9Sstevel@tonic-gate 			(void) printf("arp_muxid %d\n", arp_muxid);
40177c478bd9Sstevel@tonic-gate 		if (ioctl(mux_fd, I_PUNLINK, arp_muxid) < 0) {
40187c478bd9Sstevel@tonic-gate 			if ((errno == EINVAL) &&
40197c478bd9Sstevel@tonic-gate 			    (flags & (IFF_NOARP | IFF_IPV6))) {
40207c478bd9Sstevel@tonic-gate 				/*
40217c478bd9Sstevel@tonic-gate 				 * Some plumbing utilities set the muxid to
40227c478bd9Sstevel@tonic-gate 				 * -1 or some invalid value to signify that
40237c478bd9Sstevel@tonic-gate 				 * there is no arp stream. Set the muxid to 0
40247c478bd9Sstevel@tonic-gate 				 * before trying to unplumb the IP stream.
40257c478bd9Sstevel@tonic-gate 				 * IP does not allow the IP stream to be
40267c478bd9Sstevel@tonic-gate 				 * unplumbed if it sees a non-null arp muxid,
40277c478bd9Sstevel@tonic-gate 				 * for consistency of IP-ARP streams.
40287c478bd9Sstevel@tonic-gate 				 */
40297c478bd9Sstevel@tonic-gate 				lifr.lifr_arp_muxid = 0;
4030fc80c0dfSnordmark 				(void) ioctl(muxid_fd, SIOCSLIFMUXID,
40317c478bd9Sstevel@tonic-gate 				    (caddr_t)&lifr);
40327c478bd9Sstevel@tonic-gate 				changed_arp_muxid = _B_TRUE;
40337c478bd9Sstevel@tonic-gate 			} else {
40347c478bd9Sstevel@tonic-gate 				Perror0("I_PUNLINK for arp");
40357c478bd9Sstevel@tonic-gate 			}
40367c478bd9Sstevel@tonic-gate 		}
40377c478bd9Sstevel@tonic-gate 	}
40387c478bd9Sstevel@tonic-gate 	if (debug)
40397c478bd9Sstevel@tonic-gate 		(void) printf("ip_muxid %d\n", ip_muxid);
40407c478bd9Sstevel@tonic-gate 
40417c478bd9Sstevel@tonic-gate 	if (ioctl(mux_fd, I_PUNLINK, ip_muxid) < 0) {
40427c478bd9Sstevel@tonic-gate 		if (changed_arp_muxid) {
40437c478bd9Sstevel@tonic-gate 			/*
40447c478bd9Sstevel@tonic-gate 			 * Some error occurred, and we need to restore
40457c478bd9Sstevel@tonic-gate 			 * everything back to what it was.
40467c478bd9Sstevel@tonic-gate 			 */
40477c478bd9Sstevel@tonic-gate 			save_errno = errno;
40487c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = arp_muxid;
40497c478bd9Sstevel@tonic-gate 			lifr.lifr_ip_muxid = ip_muxid;
4050fc80c0dfSnordmark 			(void) ioctl(muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
40517c478bd9Sstevel@tonic-gate 			errno = save_errno;
40527c478bd9Sstevel@tonic-gate 		}
40537c478bd9Sstevel@tonic-gate 		Perror0_exit("I_PUNLINK for ip");
40547c478bd9Sstevel@tonic-gate 	}
40557c478bd9Sstevel@tonic-gate 	(void) close(mux_fd);
4056fc80c0dfSnordmark 	(void) close(muxid_fd);
40577c478bd9Sstevel@tonic-gate 	return (0);
40587c478bd9Sstevel@tonic-gate }
40597c478bd9Sstevel@tonic-gate 
40607c478bd9Sstevel@tonic-gate /*
40617c478bd9Sstevel@tonic-gate  * If this is a physical interface then create it unless it is already
40627c478bd9Sstevel@tonic-gate  * present. If it is a logical interface name use SIOCLIFADDIF to
40637c478bd9Sstevel@tonic-gate  * create and (and fail it if already exists.)
40647c478bd9Sstevel@tonic-gate  * As a special case send SIOCLIFADDIF for the loopback interface. This
40657c478bd9Sstevel@tonic-gate  * is needed since there is no other notion of plumbing the loopback
40667c478bd9Sstevel@tonic-gate  * interface.
40677c478bd9Sstevel@tonic-gate  */
40687c478bd9Sstevel@tonic-gate /* ARGSUSED */
40697c478bd9Sstevel@tonic-gate static int
40707c478bd9Sstevel@tonic-gate inetplumb(char *arg, int64_t param)
40717c478bd9Sstevel@tonic-gate {
40727c478bd9Sstevel@tonic-gate 	char		*strptr;
40737c478bd9Sstevel@tonic-gate 	boolean_t	islo;
4074f4b3ec61Sdh 	zoneid_t	zoneid;
40757c478bd9Sstevel@tonic-gate 
40767c478bd9Sstevel@tonic-gate 	strptr = strchr(name, ':');
40777c478bd9Sstevel@tonic-gate 	islo = (strcmp(name, LOOPBACK_IF) == 0);
40787c478bd9Sstevel@tonic-gate 
40797c478bd9Sstevel@tonic-gate 	if (strptr != NULL || islo) {
40807c478bd9Sstevel@tonic-gate 		(void) memset(&lifr, 0, sizeof (lifr));
40817c478bd9Sstevel@tonic-gate 		(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
40827c478bd9Sstevel@tonic-gate 		if (islo && ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0) {
40837c478bd9Sstevel@tonic-gate 			if (debug) {
40847c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
40857c478bd9Sstevel@tonic-gate 				    "ifconfig: %s already exists\n", name);
40867c478bd9Sstevel@tonic-gate 			}
40877c478bd9Sstevel@tonic-gate 			return (0);
40887c478bd9Sstevel@tonic-gate 		}
40897c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
40907c478bd9Sstevel@tonic-gate 			if (errno == EEXIST) {
40917c478bd9Sstevel@tonic-gate 				if (debug) {
40927c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
40937c478bd9Sstevel@tonic-gate 					    "ifconfig: %s already exists\n",
40947c478bd9Sstevel@tonic-gate 					    name);
40957c478bd9Sstevel@tonic-gate 				}
40967c478bd9Sstevel@tonic-gate 			} else {
40977c478bd9Sstevel@tonic-gate 				Perror2_exit("plumb: SIOCLIFADDIF", name);
40987c478bd9Sstevel@tonic-gate 			}
40997c478bd9Sstevel@tonic-gate 		}
41007c478bd9Sstevel@tonic-gate 		/*
41017c478bd9Sstevel@tonic-gate 		 * IP can create the new logical interface on a different
41027c478bd9Sstevel@tonic-gate 		 * physical interface in the same IPMP group. Take the new
41037c478bd9Sstevel@tonic-gate 		 * interface into account for further operations.
41047c478bd9Sstevel@tonic-gate 		 */
41057c478bd9Sstevel@tonic-gate 		(void) strncpy(name, lifr.lifr_name, sizeof (name));
41067c478bd9Sstevel@tonic-gate 		return (0);
41077c478bd9Sstevel@tonic-gate 	}
41087c478bd9Sstevel@tonic-gate 
4109f4b3ec61Sdh 	/*
4110f4b3ec61Sdh 	 * For global zone, check if the interface is used by a non-global
4111f4b3ec61Sdh 	 * zone, note that the non-global zones doesn't need this check,
4112f4b3ec61Sdh 	 * because zoneadm has taken care of this when the zone boots.
4113f4b3ec61Sdh 	 */
4114f4b3ec61Sdh 	zoneid = getzoneid();
4115f4b3ec61Sdh 	if (zoneid == GLOBAL_ZONEID) {
4116f4b3ec61Sdh 		int ret;
4117f4b3ec61Sdh 
4118f4b3ec61Sdh 		zoneid = ALL_ZONES;
4119f4b3ec61Sdh 		ret = zone_check_datalink(&zoneid, name);
4120f4b3ec61Sdh 		if (ret == 0) {
4121f4b3ec61Sdh 			char zonename[ZONENAME_MAX];
4122f4b3ec61Sdh 
4123f4b3ec61Sdh 			(void) getzonenamebyid(zoneid, zonename, ZONENAME_MAX);
4124f4b3ec61Sdh 			(void) fprintf(stderr, "%s is used by non-global"
4125f4b3ec61Sdh 			    "zone: %s\n", name, zonename);
4126f4b3ec61Sdh 			return (1);
4127f4b3ec61Sdh 		}
4128f4b3ec61Sdh 	}
4129f4b3ec61Sdh 
41307c478bd9Sstevel@tonic-gate 	if (debug)
41317c478bd9Sstevel@tonic-gate 		(void) printf("inetplumb: %s af %d\n", name, afp->af_af);
41327c478bd9Sstevel@tonic-gate 
4133c7e4935fSss 	plumb_one_device(afp->af_af);
41347c478bd9Sstevel@tonic-gate 	return (0);
41357c478bd9Sstevel@tonic-gate }
41367c478bd9Sstevel@tonic-gate 
41377c478bd9Sstevel@tonic-gate void
4138f7d61273Smeem Perror0(const char *cmd)
41397c478bd9Sstevel@tonic-gate {
4140f7d61273Smeem 	Perror2(cmd, lifr.lifr_name);
41417c478bd9Sstevel@tonic-gate }
41427c478bd9Sstevel@tonic-gate 
41437c478bd9Sstevel@tonic-gate void
4144f7d61273Smeem Perror0_exit(const char *cmd)
41457c478bd9Sstevel@tonic-gate {
41467c478bd9Sstevel@tonic-gate 	Perror0(cmd);
41477c478bd9Sstevel@tonic-gate 	exit(1);
41487c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
41497c478bd9Sstevel@tonic-gate }
41507c478bd9Sstevel@tonic-gate 
41517c478bd9Sstevel@tonic-gate void
4152f7d61273Smeem Perror2(const char *cmd, const char *str)
41537c478bd9Sstevel@tonic-gate {
4154f7d61273Smeem 	int error = errno;
41557c478bd9Sstevel@tonic-gate 
4156f7d61273Smeem 	(void) fprintf(stderr, "ifconfig: %s: ", cmd);
41577c478bd9Sstevel@tonic-gate 
4158f7d61273Smeem 	switch (error) {
41597c478bd9Sstevel@tonic-gate 	case ENXIO:
4160f7d61273Smeem 		(void) fprintf(stderr, "%s: no such interface\n", str);
41617c478bd9Sstevel@tonic-gate 		break;
41627c478bd9Sstevel@tonic-gate 	case EPERM:
4163f7d61273Smeem 		(void) fprintf(stderr, "%s: permission denied\n", str);
41647c478bd9Sstevel@tonic-gate 		break;
4165f7d61273Smeem 	case EEXIST:
4166f7d61273Smeem 		(void) fprintf(stderr, "%s: already exists\n", str);
4167f7d61273Smeem 		break;
4168f7d61273Smeem 	default:
4169f7d61273Smeem 		errno = error;
4170f7d61273Smeem 		perror(str);
41717c478bd9Sstevel@tonic-gate 	}
41727c478bd9Sstevel@tonic-gate }
41737c478bd9Sstevel@tonic-gate 
41747c478bd9Sstevel@tonic-gate /*
41757c478bd9Sstevel@tonic-gate  * Print out error message (Perror2()) and exit
41767c478bd9Sstevel@tonic-gate  */
41777c478bd9Sstevel@tonic-gate void
4178f7d61273Smeem Perror2_exit(const char *cmd, const char *str)
41797c478bd9Sstevel@tonic-gate {
41807c478bd9Sstevel@tonic-gate 	Perror2(cmd, str);
41817c478bd9Sstevel@tonic-gate 	exit(1);
41827c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
41837c478bd9Sstevel@tonic-gate }
41847c478bd9Sstevel@tonic-gate 
4185c7e4935fSss void
4186c7e4935fSss Perrdlpi(const char *cmd, const char *linkname, int err)
4187c7e4935fSss {
4188c7e4935fSss 	(void) fprintf(stderr, "ifconfig: %s \"%s\": %s\n", cmd,
4189c7e4935fSss 	    linkname, dlpi_strerror(err));
4190c7e4935fSss }
4191c7e4935fSss 
4192c7e4935fSss /*
4193c7e4935fSss  * Print out error message (Perrdlpi()) and exit
4194c7e4935fSss  */
4195c7e4935fSss void
4196c7e4935fSss Perrdlpi_exit(const char *cmd, const char *linkname, int err)
4197c7e4935fSss {
4198c7e4935fSss 	Perrdlpi(cmd, linkname, err);
4199c7e4935fSss 	exit(1);
4200c7e4935fSss }
4201c7e4935fSss 
42027c478bd9Sstevel@tonic-gate /*
42037c478bd9Sstevel@tonic-gate  * If the last argument is non-NULL allow a <addr>/<n> syntax and
42047c478bd9Sstevel@tonic-gate  * pass out <n> in *plenp.
42057c478bd9Sstevel@tonic-gate  * If <n> doesn't parse return BAD_ADDR as *plenp.
42067c478bd9Sstevel@tonic-gate  * If no /<n> is present return NO_PREFIX as *plenp.
42077c478bd9Sstevel@tonic-gate  */
42087c478bd9Sstevel@tonic-gate static void
42097c478bd9Sstevel@tonic-gate in_getaddr(char *s, struct sockaddr *saddr, int *plenp)
42107c478bd9Sstevel@tonic-gate {
421169bb4bb4Scarlsonj 	/* LINTED: alignment */
42127c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
42137c478bd9Sstevel@tonic-gate 	struct hostent *hp;
42147c478bd9Sstevel@tonic-gate 	struct netent *np;
42157c478bd9Sstevel@tonic-gate 	char str[BUFSIZ];
42167c478bd9Sstevel@tonic-gate 	int error_num;
42177c478bd9Sstevel@tonic-gate 
42187c478bd9Sstevel@tonic-gate 	(void) strncpy(str, s, sizeof (str));
42197c478bd9Sstevel@tonic-gate 
42207c478bd9Sstevel@tonic-gate 	/*
42217c478bd9Sstevel@tonic-gate 	 * Look for '/'<n> is plenp
42227c478bd9Sstevel@tonic-gate 	 */
42237c478bd9Sstevel@tonic-gate 	if (plenp != NULL) {
42247c478bd9Sstevel@tonic-gate 		char *cp;
42257c478bd9Sstevel@tonic-gate 
42267906a3e0Smeem 		*plenp = in_getprefixlen(str, _B_TRUE, IP_ABITS);
42277c478bd9Sstevel@tonic-gate 		if (*plenp == BAD_ADDR)
42287c478bd9Sstevel@tonic-gate 			return;
42297c478bd9Sstevel@tonic-gate 		cp = strchr(str, '/');
42307c478bd9Sstevel@tonic-gate 		if (cp != NULL)
42317c478bd9Sstevel@tonic-gate 			*cp = '\0';
42327c478bd9Sstevel@tonic-gate 	} else if (strchr(str, '/') != NULL) {
42337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
42347c478bd9Sstevel@tonic-gate 		exit(1);
42357c478bd9Sstevel@tonic-gate 	}
42367c478bd9Sstevel@tonic-gate 
42377c478bd9Sstevel@tonic-gate 	(void) memset(sin, 0, sizeof (*sin));
42387c478bd9Sstevel@tonic-gate 
42397c478bd9Sstevel@tonic-gate 	/*
42407c478bd9Sstevel@tonic-gate 	 *	Try to catch attempts to set the broadcast address to all 1's.
42417c478bd9Sstevel@tonic-gate 	 */
42427c478bd9Sstevel@tonic-gate 	if (strcmp(str, "255.255.255.255") == 0 ||
42437c478bd9Sstevel@tonic-gate 	    (strtoul(str, (char **)NULL, 0) == 0xffffffffUL)) {
42447c478bd9Sstevel@tonic-gate 		sin->sin_family = AF_INET;
42457c478bd9Sstevel@tonic-gate 		sin->sin_addr.s_addr = 0xffffffff;
42467c478bd9Sstevel@tonic-gate 		return;
42477c478bd9Sstevel@tonic-gate 	}
42487c478bd9Sstevel@tonic-gate 
42497c478bd9Sstevel@tonic-gate 	hp = getipnodebyname(str, AF_INET, 0, &error_num);
42507c478bd9Sstevel@tonic-gate 	if (hp) {
42517c478bd9Sstevel@tonic-gate 		sin->sin_family = hp->h_addrtype;
42527c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
42537c478bd9Sstevel@tonic-gate 		freehostent(hp);
42547c478bd9Sstevel@tonic-gate 		return;
42557c478bd9Sstevel@tonic-gate 	}
42567c478bd9Sstevel@tonic-gate 	np = getnetbyname(str);
42577c478bd9Sstevel@tonic-gate 	if (np) {
42587c478bd9Sstevel@tonic-gate 		sin->sin_family = np->n_addrtype;
42597c478bd9Sstevel@tonic-gate 		sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
42607c478bd9Sstevel@tonic-gate 		return;
42617c478bd9Sstevel@tonic-gate 	}
42627c478bd9Sstevel@tonic-gate 	if (error_num == TRY_AGAIN) {
42637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address "
42647c478bd9Sstevel@tonic-gate 		    "(try again later)\n", s);
42657c478bd9Sstevel@tonic-gate 	} else {
42667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
42677c478bd9Sstevel@tonic-gate 	}
42687c478bd9Sstevel@tonic-gate 	exit(1);
42697c478bd9Sstevel@tonic-gate }
42707c478bd9Sstevel@tonic-gate 
42717c478bd9Sstevel@tonic-gate /*
42727c478bd9Sstevel@tonic-gate  * If the last argument is non-NULL allow a <addr>/<n> syntax and
42737c478bd9Sstevel@tonic-gate  * pass out <n> in *plenp.
42747c478bd9Sstevel@tonic-gate  * If <n> doesn't parse return BAD_ADDR as *plenp.
42757c478bd9Sstevel@tonic-gate  * If no /<n> is present return NO_PREFIX as *plenp.
42767c478bd9Sstevel@tonic-gate  */
42777c478bd9Sstevel@tonic-gate static void
42787c478bd9Sstevel@tonic-gate in6_getaddr(char *s, struct sockaddr *saddr, int *plenp)
42797c478bd9Sstevel@tonic-gate {
428069bb4bb4Scarlsonj 	/* LINTED: alignment */
42817c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)saddr;
42827c478bd9Sstevel@tonic-gate 	struct hostent *hp;
42837c478bd9Sstevel@tonic-gate 	char str[BUFSIZ];
42847c478bd9Sstevel@tonic-gate 	int error_num;
42857c478bd9Sstevel@tonic-gate 
42867c478bd9Sstevel@tonic-gate 	(void) strncpy(str, s, sizeof (str));
42877c478bd9Sstevel@tonic-gate 
42887c478bd9Sstevel@tonic-gate 	/*
42897c478bd9Sstevel@tonic-gate 	 * Look for '/'<n> is plenp
42907c478bd9Sstevel@tonic-gate 	 */
42917c478bd9Sstevel@tonic-gate 	if (plenp != NULL) {
42927c478bd9Sstevel@tonic-gate 		char *cp;
42937c478bd9Sstevel@tonic-gate 
42947906a3e0Smeem 		*plenp = in_getprefixlen(str, _B_TRUE, IPV6_ABITS);
42957c478bd9Sstevel@tonic-gate 		if (*plenp == BAD_ADDR)
42967c478bd9Sstevel@tonic-gate 			return;
42977c478bd9Sstevel@tonic-gate 		cp = strchr(str, '/');
42987c478bd9Sstevel@tonic-gate 		if (cp != NULL)
42997c478bd9Sstevel@tonic-gate 			*cp = '\0';
43007c478bd9Sstevel@tonic-gate 	} else if (strchr(str, '/') != NULL) {
43017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
43027c478bd9Sstevel@tonic-gate 		exit(1);
43037c478bd9Sstevel@tonic-gate 	}
43047c478bd9Sstevel@tonic-gate 
43057c478bd9Sstevel@tonic-gate 	(void) memset(sin6, 0, sizeof (*sin6));
43067c478bd9Sstevel@tonic-gate 
43077c478bd9Sstevel@tonic-gate 	hp = getipnodebyname(str, AF_INET6, 0, &error_num);
43087c478bd9Sstevel@tonic-gate 	if (hp) {
43097c478bd9Sstevel@tonic-gate 		sin6->sin6_family = hp->h_addrtype;
43107c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin6->sin6_addr, hp->h_addr, hp->h_length);
43117c478bd9Sstevel@tonic-gate 		freehostent(hp);
43127c478bd9Sstevel@tonic-gate 		return;
43137c478bd9Sstevel@tonic-gate 	}
43147c478bd9Sstevel@tonic-gate 	if (error_num == TRY_AGAIN) {
43157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address "
43167c478bd9Sstevel@tonic-gate 		    "(try again later)\n", s);
43177c478bd9Sstevel@tonic-gate 	} else {
43187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
43197c478bd9Sstevel@tonic-gate 	}
43207c478bd9Sstevel@tonic-gate 	exit(1);
43217c478bd9Sstevel@tonic-gate }
43227c478bd9Sstevel@tonic-gate 
43237c478bd9Sstevel@tonic-gate /*
43247c478bd9Sstevel@tonic-gate  * If "slash" is zero this parses the whole string as
43257c478bd9Sstevel@tonic-gate  * an integer. With "slash" non zero it parses the tail part as an integer.
43267c478bd9Sstevel@tonic-gate  *
43277c478bd9Sstevel@tonic-gate  * If it is not a valid integer this returns BAD_ADDR.
43287c478bd9Sstevel@tonic-gate  * If there is /<n> present this returns NO_PREFIX.
43297c478bd9Sstevel@tonic-gate  */
43307c478bd9Sstevel@tonic-gate static int
43317c478bd9Sstevel@tonic-gate in_getprefixlen(char *addr, boolean_t slash, int max_plen)
43327c478bd9Sstevel@tonic-gate {
43337c478bd9Sstevel@tonic-gate 	int prefixlen;
43347c478bd9Sstevel@tonic-gate 	char *str, *end;
43357c478bd9Sstevel@tonic-gate 
43367c478bd9Sstevel@tonic-gate 	if (slash) {
43377c478bd9Sstevel@tonic-gate 		str = strchr(addr, '/');
43387c478bd9Sstevel@tonic-gate 		if (str == NULL)
43397c478bd9Sstevel@tonic-gate 			return (NO_PREFIX);
43407c478bd9Sstevel@tonic-gate 		str++;
43417c478bd9Sstevel@tonic-gate 	} else
43427c478bd9Sstevel@tonic-gate 		str = addr;
43437c478bd9Sstevel@tonic-gate 
43447c478bd9Sstevel@tonic-gate 	prefixlen = strtol(str, &end, 10);
43457c478bd9Sstevel@tonic-gate 	if (prefixlen < 0)
43467c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
43477c478bd9Sstevel@tonic-gate 	if (str == end)
43487c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
43497c478bd9Sstevel@tonic-gate 	if (max_plen != 0 && max_plen < prefixlen)
43507c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
43517c478bd9Sstevel@tonic-gate 	return (prefixlen);
43527c478bd9Sstevel@tonic-gate }
43537c478bd9Sstevel@tonic-gate 
43547c478bd9Sstevel@tonic-gate /*
43557c478bd9Sstevel@tonic-gate  * Convert a prefix length to a mask.
43567c478bd9Sstevel@tonic-gate  * Returns 1 if ok. 0 otherwise.
43577c478bd9Sstevel@tonic-gate  * Assumes the mask array is zero'ed by the caller.
43587c478bd9Sstevel@tonic-gate  */
43597c478bd9Sstevel@tonic-gate static boolean_t
43607c478bd9Sstevel@tonic-gate in_prefixlentomask(int prefixlen, int maxlen, uchar_t *mask)
43617c478bd9Sstevel@tonic-gate {
43627c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxlen)
43637c478bd9Sstevel@tonic-gate 		return (0);
43647c478bd9Sstevel@tonic-gate 
43657c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
43667c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
43677c478bd9Sstevel@tonic-gate 			*mask++ = 0xFF;
43687c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
43697c478bd9Sstevel@tonic-gate 			continue;
43707c478bd9Sstevel@tonic-gate 		}
43717c478bd9Sstevel@tonic-gate 		*mask |= 1 << (8 - prefixlen);
43727c478bd9Sstevel@tonic-gate 		prefixlen--;
43737c478bd9Sstevel@tonic-gate 	}
43747c478bd9Sstevel@tonic-gate 	return (1);
43757c478bd9Sstevel@tonic-gate }
43767c478bd9Sstevel@tonic-gate 
43777c478bd9Sstevel@tonic-gate static void
43787c478bd9Sstevel@tonic-gate print_flags(uint64_t flags)
43797c478bd9Sstevel@tonic-gate {
43807c478bd9Sstevel@tonic-gate 	boolean_t first = _B_TRUE;
43817c478bd9Sstevel@tonic-gate 	int cnt, i;
43827c478bd9Sstevel@tonic-gate 
43837c478bd9Sstevel@tonic-gate 	(void) printf("flags=%llx", flags);
43847c478bd9Sstevel@tonic-gate 	cnt = sizeof (if_flags_tbl) / sizeof (if_flags_t);
43857c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
43867c478bd9Sstevel@tonic-gate 		if (flags & if_flags_tbl[i].iff_value) {
43877c478bd9Sstevel@tonic-gate 			if (first) {
43887c478bd9Sstevel@tonic-gate 				(void) printf("<");
43897c478bd9Sstevel@tonic-gate 				first = _B_FALSE;
43907c478bd9Sstevel@tonic-gate 			} else {
43917c478bd9Sstevel@tonic-gate 				/*
43927c478bd9Sstevel@tonic-gate 				 * It has to be here and not with the
43937c478bd9Sstevel@tonic-gate 				 * printf below because for the last one,
43947c478bd9Sstevel@tonic-gate 				 * we don't want a comma before the ">".
43957c478bd9Sstevel@tonic-gate 				 */
43967c478bd9Sstevel@tonic-gate 				(void) printf(",");
43977c478bd9Sstevel@tonic-gate 			}
43987c478bd9Sstevel@tonic-gate 			(void) printf("%s", if_flags_tbl[i].iff_name);
43997c478bd9Sstevel@tonic-gate 		}
44007c478bd9Sstevel@tonic-gate 	}
44017c478bd9Sstevel@tonic-gate 	if (!first)
44027c478bd9Sstevel@tonic-gate 		(void) printf(">");
44037c478bd9Sstevel@tonic-gate }
44047c478bd9Sstevel@tonic-gate 
44057c478bd9Sstevel@tonic-gate static void
44067c478bd9Sstevel@tonic-gate print_config_flags(uint64_t flags)
44077c478bd9Sstevel@tonic-gate {
44087c478bd9Sstevel@tonic-gate 	int cnt, i;
44097c478bd9Sstevel@tonic-gate 
44107c478bd9Sstevel@tonic-gate 	cnt = sizeof (if_config_cmd_tbl) / sizeof (if_config_cmd_t);
44117c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
44127c478bd9Sstevel@tonic-gate 		if (flags & if_config_cmd_tbl[i].iff_flag) {
44137c478bd9Sstevel@tonic-gate 			(void) printf("%s ", if_config_cmd_tbl[i].iff_name);
44147c478bd9Sstevel@tonic-gate 		}
44157c478bd9Sstevel@tonic-gate 	}
44167c478bd9Sstevel@tonic-gate }
44177c478bd9Sstevel@tonic-gate 
44187c478bd9Sstevel@tonic-gate /*
4419dd7a6f5fSkcpoon  * Use the configured directory lookup mechanism (e.g. files/NIS/NIS+/...)
4420dd7a6f5fSkcpoon  * to find the network mask.  Returns true if we found one to set.
4421dd7a6f5fSkcpoon  *
4422dd7a6f5fSkcpoon  * The parameter addr_set controls whether we should get the address of
4423dd7a6f5fSkcpoon  * the working interface for the netmask query.  If addr_set is true,
4424dd7a6f5fSkcpoon  * we will use the address provided.  Otherwise, we will find the working
4425dd7a6f5fSkcpoon  * interface's address and use it instead.
44267c478bd9Sstevel@tonic-gate  */
44277c478bd9Sstevel@tonic-gate static boolean_t
4428dd7a6f5fSkcpoon in_getmask(struct sockaddr_in *saddr, boolean_t addr_set)
44297c478bd9Sstevel@tonic-gate {
44307c478bd9Sstevel@tonic-gate 	struct sockaddr_in ifaddr;
44317c478bd9Sstevel@tonic-gate 
44327c478bd9Sstevel@tonic-gate 	/*
4433dd7a6f5fSkcpoon 	 * Read the address from the interface if it is not passed in.
44347c478bd9Sstevel@tonic-gate 	 */
4435dd7a6f5fSkcpoon 	if (!addr_set) {
4436dd7a6f5fSkcpoon 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
4437dd7a6f5fSkcpoon 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
4438dd7a6f5fSkcpoon 			if (errno != EADDRNOTAVAIL) {
4439dd7a6f5fSkcpoon 				(void) fprintf(stderr, "Need net number for "
4440dd7a6f5fSkcpoon 				    "mask\n");
4441dd7a6f5fSkcpoon 			}
4442dd7a6f5fSkcpoon 			return (_B_FALSE);
4443dd7a6f5fSkcpoon 		}
4444dd7a6f5fSkcpoon 		ifaddr = *((struct sockaddr_in *)&lifr.lifr_addr);
4445dd7a6f5fSkcpoon 	} else {
4446dd7a6f5fSkcpoon 		ifaddr.sin_addr = saddr->sin_addr;
44477c478bd9Sstevel@tonic-gate 	}
44487c478bd9Sstevel@tonic-gate 	if (getnetmaskbyaddr(ifaddr.sin_addr, &saddr->sin_addr) == 0) {
44497c478bd9Sstevel@tonic-gate 		saddr->sin_family = AF_INET;
44507c478bd9Sstevel@tonic-gate 		return (_B_TRUE);
44517c478bd9Sstevel@tonic-gate 	}
44527c478bd9Sstevel@tonic-gate 	return (_B_FALSE);
44537c478bd9Sstevel@tonic-gate }
44547c478bd9Sstevel@tonic-gate 
44557c478bd9Sstevel@tonic-gate static int
44567c478bd9Sstevel@tonic-gate strioctl(int s, int cmd, char *buf, int buflen)
44577c478bd9Sstevel@tonic-gate {
44587c478bd9Sstevel@tonic-gate 	struct strioctl ioc;
44597c478bd9Sstevel@tonic-gate 
44607c478bd9Sstevel@tonic-gate 	(void) memset(&ioc, 0, sizeof (ioc));
44617c478bd9Sstevel@tonic-gate 	ioc.ic_cmd = cmd;
44627c478bd9Sstevel@tonic-gate 	ioc.ic_timout = 0;
44637c478bd9Sstevel@tonic-gate 	ioc.ic_len = buflen;
44647c478bd9Sstevel@tonic-gate 	ioc.ic_dp = buf;
44657c478bd9Sstevel@tonic-gate 	return (ioctl(s, I_STR, (char *)&ioc));
44667c478bd9Sstevel@tonic-gate }
44677c478bd9Sstevel@tonic-gate 
44687c478bd9Sstevel@tonic-gate static void
4469d62bc4baSyz add_ni(const char *name)
44707c478bd9Sstevel@tonic-gate {
44717c478bd9Sstevel@tonic-gate 	ni_t **pp;
44727c478bd9Sstevel@tonic-gate 	ni_t *p;
44737c478bd9Sstevel@tonic-gate 
44747c478bd9Sstevel@tonic-gate 	for (pp = &ni_list; (p = *pp) != NULL; pp = &(p->ni_next)) {
44757c478bd9Sstevel@tonic-gate 		if (strcmp(p->ni_name, name) == 0) {
44767c478bd9Sstevel@tonic-gate 			if (debug > 2)
44777c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "'%s' is a duplicate\n",
44787c478bd9Sstevel@tonic-gate 				    name);
44797c478bd9Sstevel@tonic-gate 			return;
44807c478bd9Sstevel@tonic-gate 		}
44817c478bd9Sstevel@tonic-gate 	}
44827c478bd9Sstevel@tonic-gate 
44837c478bd9Sstevel@tonic-gate 	if (debug > 2)
44847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "adding '%s'\n",
44857c478bd9Sstevel@tonic-gate 		    name);
44867c478bd9Sstevel@tonic-gate 
44877c478bd9Sstevel@tonic-gate 	if ((p = malloc(sizeof (ni_t))) == NULL)
44887c478bd9Sstevel@tonic-gate 		return;
44897c478bd9Sstevel@tonic-gate 
44907c478bd9Sstevel@tonic-gate 	(void) strlcpy(p->ni_name, name, sizeof (p->ni_name));
44917c478bd9Sstevel@tonic-gate 	p->ni_next = NULL;
44927c478bd9Sstevel@tonic-gate 
44937c478bd9Sstevel@tonic-gate 	*pp = p;
44947c478bd9Sstevel@tonic-gate 	num_ni++;
44957c478bd9Sstevel@tonic-gate }
44967c478bd9Sstevel@tonic-gate 
44977c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
4498d62bc4baSyz static boolean_t
4499d62bc4baSyz ni_entry(const char *linkname, void *arg)
45007c478bd9Sstevel@tonic-gate {
4501c7e4935fSss 	dlpi_handle_t	dh;
45027c478bd9Sstevel@tonic-gate 
4503c7e4935fSss 	if (dlpi_open(linkname, &dh, 0) != DLPI_SUCCESS)
4504d62bc4baSyz 		return (_B_FALSE);
45057c478bd9Sstevel@tonic-gate 
4506d62bc4baSyz 	add_ni(linkname);
45077c478bd9Sstevel@tonic-gate 
4508c7e4935fSss 	dlpi_close(dh);
4509d62bc4baSyz 	return (_B_FALSE);
45107c478bd9Sstevel@tonic-gate }
45117c478bd9Sstevel@tonic-gate 
45127c478bd9Sstevel@tonic-gate /*
45137c478bd9Sstevel@tonic-gate  * dhcp-related routines
45147c478bd9Sstevel@tonic-gate  */
45157c478bd9Sstevel@tonic-gate 
45167c478bd9Sstevel@tonic-gate static int
45177c478bd9Sstevel@tonic-gate setifdhcp(const char *caller, const char *ifname, int argc, char *argv[])
45187c478bd9Sstevel@tonic-gate {
45197c478bd9Sstevel@tonic-gate 	dhcp_ipc_request_t	*request;
45207c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t	*reply	= NULL;
45217c478bd9Sstevel@tonic-gate 	int			timeout = DHCP_IPC_WAIT_DEFAULT;
45227c478bd9Sstevel@tonic-gate 	dhcp_ipc_type_t		type	= DHCP_START;
45237c478bd9Sstevel@tonic-gate 	int			error;
45247c478bd9Sstevel@tonic-gate 	boolean_t		is_primary = _B_FALSE;
45257c478bd9Sstevel@tonic-gate 	boolean_t		started = _B_FALSE;
45267c478bd9Sstevel@tonic-gate 
45277c478bd9Sstevel@tonic-gate 	for (argv++; --argc > 0; argv++) {
45287c478bd9Sstevel@tonic-gate 
45297c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "primary") == 0) {
45307c478bd9Sstevel@tonic-gate 			is_primary = _B_TRUE;
45317c478bd9Sstevel@tonic-gate 			continue;
45327c478bd9Sstevel@tonic-gate 		}
45337c478bd9Sstevel@tonic-gate 
45347c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "wait") == 0) {
45357c478bd9Sstevel@tonic-gate 			if (--argc <= 0) {
45367c478bd9Sstevel@tonic-gate 				usage();
45377c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
45387c478bd9Sstevel@tonic-gate 			}
45397c478bd9Sstevel@tonic-gate 			argv++;
45407c478bd9Sstevel@tonic-gate 
45417c478bd9Sstevel@tonic-gate 			if (strcmp(*argv, "forever") == 0) {
45427c478bd9Sstevel@tonic-gate 				timeout = DHCP_IPC_WAIT_FOREVER;
45437c478bd9Sstevel@tonic-gate 				continue;
45447c478bd9Sstevel@tonic-gate 			}
45457c478bd9Sstevel@tonic-gate 
45467c478bd9Sstevel@tonic-gate 			if (sscanf(*argv, "%d", &timeout) != 1) {
45477c478bd9Sstevel@tonic-gate 				usage();
45487c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
45497c478bd9Sstevel@tonic-gate 			}
45507c478bd9Sstevel@tonic-gate 
45517c478bd9Sstevel@tonic-gate 			if (timeout < 0) {
45527c478bd9Sstevel@tonic-gate 				usage();
45537c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
45547c478bd9Sstevel@tonic-gate 			}
45557c478bd9Sstevel@tonic-gate 			continue;
45567c478bd9Sstevel@tonic-gate 		}
45577c478bd9Sstevel@tonic-gate 
45587c478bd9Sstevel@tonic-gate 		type = dhcp_string_to_request(*argv);
45597c478bd9Sstevel@tonic-gate 		if (type == -1) {
45607c478bd9Sstevel@tonic-gate 			usage();
45617c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_BADARGS);
45627c478bd9Sstevel@tonic-gate 		}
45637c478bd9Sstevel@tonic-gate 	}
45647c478bd9Sstevel@tonic-gate 
45657c478bd9Sstevel@tonic-gate 	/*
45667c478bd9Sstevel@tonic-gate 	 * Only try to start agent on start or inform; in all other cases it
45677c478bd9Sstevel@tonic-gate 	 * has to already be running for anything to make sense.
45687c478bd9Sstevel@tonic-gate 	 */
45697c478bd9Sstevel@tonic-gate 	if (type == DHCP_START || type == DHCP_INFORM) {
45707c478bd9Sstevel@tonic-gate 		if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1) {
45717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: unable to start %s\n",
45727c478bd9Sstevel@tonic-gate 			    caller, DHCP_AGENT_PATH);
45737c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_FAILURE);
45747c478bd9Sstevel@tonic-gate 		}
45757c478bd9Sstevel@tonic-gate 		started = _B_TRUE;
45767c478bd9Sstevel@tonic-gate 	}
45777c478bd9Sstevel@tonic-gate 
45787c478bd9Sstevel@tonic-gate 	if (is_primary)
4579d04ccbb3Scarlsonj 		type |= DHCP_PRIMARY;
4580d04ccbb3Scarlsonj 
4581d04ccbb3Scarlsonj 	if (af != AF_INET)
4582d04ccbb3Scarlsonj 		type |= DHCP_V6;
45837c478bd9Sstevel@tonic-gate 
45847c478bd9Sstevel@tonic-gate 	request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
45857c478bd9Sstevel@tonic-gate 	if (request == NULL) {
45867c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: out of memory\n", caller);
45877c478bd9Sstevel@tonic-gate 		return (DHCP_EXIT_SYSTEM);
45887c478bd9Sstevel@tonic-gate 	}
45897c478bd9Sstevel@tonic-gate 
45907c478bd9Sstevel@tonic-gate 	error = dhcp_ipc_make_request(request, &reply, timeout);
45917c478bd9Sstevel@tonic-gate 	if (error != 0) {
45927c478bd9Sstevel@tonic-gate 		free(request);
45937c478bd9Sstevel@tonic-gate 		/*
45947c478bd9Sstevel@tonic-gate 		 * Re-map connect error to not under control if we didn't try a
45957c478bd9Sstevel@tonic-gate 		 * start operation, as this has to be true and results in a
45967c478bd9Sstevel@tonic-gate 		 * clearer message, not to mention preserving compatibility
45977c478bd9Sstevel@tonic-gate 		 * with the days when we always started dhcpagent for every
45987c478bd9Sstevel@tonic-gate 		 * request.
45997c478bd9Sstevel@tonic-gate 		 */
46007c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_CONNECT && !started)
46017c478bd9Sstevel@tonic-gate 			error = DHCP_IPC_E_UNKIF;
46027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
46037c478bd9Sstevel@tonic-gate 		    dhcp_ipc_strerror(error));
46047c478bd9Sstevel@tonic-gate 		return (DHCP_EXIT_FAILURE);
46057c478bd9Sstevel@tonic-gate 	}
46067c478bd9Sstevel@tonic-gate 
46077c478bd9Sstevel@tonic-gate 	error = reply->return_code;
46087c478bd9Sstevel@tonic-gate 	if (error != 0) {
46097c478bd9Sstevel@tonic-gate 		free(request);
46107c478bd9Sstevel@tonic-gate 		free(reply);
46117c478bd9Sstevel@tonic-gate 
46127c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_TIMEOUT && timeout == 0)
46137c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_SUCCESS);
46147c478bd9Sstevel@tonic-gate 
46157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
46167c478bd9Sstevel@tonic-gate 		    dhcp_ipc_strerror(error));
46177c478bd9Sstevel@tonic-gate 
46187c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_TIMEOUT)
46197c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_TIMEOUT);
46207c478bd9Sstevel@tonic-gate 		else
46217c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_IF_FAILURE);
46227c478bd9Sstevel@tonic-gate 	}
46237c478bd9Sstevel@tonic-gate 
46247c478bd9Sstevel@tonic-gate 	if (DHCP_IPC_CMD(type) == DHCP_STATUS) {
46257c478bd9Sstevel@tonic-gate 		(void) printf("%s", dhcp_status_hdr_string());
46267c478bd9Sstevel@tonic-gate 		(void) printf("%s", dhcp_status_reply_to_string(reply));
46277c478bd9Sstevel@tonic-gate 	}
46287c478bd9Sstevel@tonic-gate 
46297c478bd9Sstevel@tonic-gate 	free(request);
46307c478bd9Sstevel@tonic-gate 	free(reply);
46317c478bd9Sstevel@tonic-gate 	return (DHCP_EXIT_SUCCESS);
46327c478bd9Sstevel@tonic-gate }
46337c478bd9Sstevel@tonic-gate 
46347c478bd9Sstevel@tonic-gate static void
46357c478bd9Sstevel@tonic-gate usage(void)
46367c478bd9Sstevel@tonic-gate {
46377c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
46387c478bd9Sstevel@tonic-gate 	    "usage: ifconfig <interface> | -a[ 4 | 6 | D ][ u | d ][ Z ]\n");
46397c478bd9Sstevel@tonic-gate 
46407c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s",
46417c478bd9Sstevel@tonic-gate 	    "\t[ <addr_family> ]\n"
46427c478bd9Sstevel@tonic-gate 	    "\t[ <address>[/<prefix_length>] [ <dest_address> ] ]\n"
46437c478bd9Sstevel@tonic-gate 	    "\t[ set [ <address>][/<prefix_length>] ]"
46447c478bd9Sstevel@tonic-gate 	    " [ <address>/<prefix_length>] ]\n"
46457c478bd9Sstevel@tonic-gate 	    "\t[ destination <dest_address> ]\n"
46467c478bd9Sstevel@tonic-gate 	    "\t[ addif <address>[/<prefix_length>]"
46477c478bd9Sstevel@tonic-gate 	    "  [ <dest_address> ] ]\n"
46487c478bd9Sstevel@tonic-gate 	    "\t[ removeif <address>[/<prefix_length>] ]\n"
46497c478bd9Sstevel@tonic-gate 	    "\t[ arp | -arp ]\n"
46507c478bd9Sstevel@tonic-gate 	    "\t[ auto-revarp ]\n"
46517c478bd9Sstevel@tonic-gate 	    "\t[ broadcast <broad_addr> ]\n"
46527c478bd9Sstevel@tonic-gate 	    "\t[ index <if_index> ]\n"
46537c478bd9Sstevel@tonic-gate 	    "\t[ metric <n> ] [ mtu <n> ]\n"
46547c478bd9Sstevel@tonic-gate 	    "\t[ netmask <mask> ]\n"
46557c478bd9Sstevel@tonic-gate 	    "\t[ plumb ] [ unplumb ]\n"
46567c478bd9Sstevel@tonic-gate 	    "\t[ preferred | -preferred ]\n"
46577c478bd9Sstevel@tonic-gate 	    "\t[ private | -private ]\n"
46587c478bd9Sstevel@tonic-gate 	    "\t[ local | -local ]\n"
46597c478bd9Sstevel@tonic-gate 	    "\t[ router | -router ]\n"
46607c478bd9Sstevel@tonic-gate 	    "\t[ subnet <subnet_address>]\n"
46617c478bd9Sstevel@tonic-gate 	    "\t[ trailers | -trailers ]\n"
46627c478bd9Sstevel@tonic-gate 	    "\t[ token <address>/<prefix_length> ]\n"
46637c478bd9Sstevel@tonic-gate 	    "\t[ tsrc <tunnel_src_address> ]\n"
46647c478bd9Sstevel@tonic-gate 	    "\t[ tdst <tunnel_dest_address> ]\n"
46657c478bd9Sstevel@tonic-gate 	    "\t[ auth_algs <tunnel_AH_authentication_algorithm> ]\n"
46667c478bd9Sstevel@tonic-gate 	    "\t[ encr_algs <tunnel_ESP_encryption_algorithm> ]\n"
46677c478bd9Sstevel@tonic-gate 	    "\t[ encr_auth_algs <tunnel_ESP_authentication_algorithm> ]\n"
46687c478bd9Sstevel@tonic-gate 	    "\t[ up ] [ down ]\n"
46697c478bd9Sstevel@tonic-gate 	    "\t[ xmit | -xmit ]\n"
46707c478bd9Sstevel@tonic-gate 	    "\t[ modlist ]\n"
46717c478bd9Sstevel@tonic-gate 	    "\t[ modinsert <module_name@position> ]\n"
46727c478bd9Sstevel@tonic-gate 	    "\t[ modremove <module_name@position> ]\n"
46737c478bd9Sstevel@tonic-gate 	    "\t[ group <groupname>] | [ group \"\"]\n"
46747c478bd9Sstevel@tonic-gate 	    "\t[ deprecated | -deprecated ]\n"
46757c478bd9Sstevel@tonic-gate 	    "\t[ standby | -standby ]\n"
46767c478bd9Sstevel@tonic-gate 	    "\t[ failover | -failover ]\n"
46777c478bd9Sstevel@tonic-gate 	    "\t[ zone <zonename> | -zone ]\n"
467845916cd2Sjpk 	    "\t[ usesrc <interface> ]\n"
467945916cd2Sjpk 	    "\t[ all-zones ]\n");
46807c478bd9Sstevel@tonic-gate 
46817c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "or\n");
46827c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
46837c478bd9Sstevel@tonic-gate 	    "\tifconfig <interface> |  -a[ 4 | 6 | D ] [ u | d ]\n");
46847c478bd9Sstevel@tonic-gate 
46857c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s", "\tauto-dhcp | dhcp\n"
46867c478bd9Sstevel@tonic-gate 	    "\t[ wait <time> | forever ]\n\t[ primary ]\n"
46877c478bd9Sstevel@tonic-gate 	    "\tstart | drop | ping | release | status | inform\n");
46887c478bd9Sstevel@tonic-gate }
4689