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