17c478bd9Sstevel@tonic-gate /*
2*36b41818SGirish Moodalbail  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
37c478bd9Sstevel@tonic-gate  */
47c478bd9Sstevel@tonic-gate /*
57c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
67c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
77c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
87c478bd9Sstevel@tonic-gate  */
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate #include "defs.h"
117c478bd9Sstevel@tonic-gate #include "strings.h"
127c478bd9Sstevel@tonic-gate #include "ifconfig.h"
137c478bd9Sstevel@tonic-gate #include <compat.h>
147c478bd9Sstevel@tonic-gate #include <libdlpi.h>
15da14cebeSEric Cheng #include <libdllink.h>
162b24ab6bSSebastien Roy #include <libdliptun.h>
172b24ab6bSSebastien Roy #include <libdllink.h>
18ff550d0eSmasputra #include <inet/ip.h>
19d2f8a3dfSpwernau #include <inet/ipsec_impl.h>
206e91bba0SGirish Moodalbail #include <libipadm.h>
216e91bba0SGirish Moodalbail #include <ifaddrs.h>
226e91bba0SGirish Moodalbail #include <libsocket_priv.h>
23ff550d0eSmasputra 
247c478bd9Sstevel@tonic-gate #define	LOOPBACK_IF	"lo0"
257c478bd9Sstevel@tonic-gate #define	NONE_STR	"none"
267c478bd9Sstevel@tonic-gate #define	ARP_MOD_NAME	"arp"
276e91bba0SGirish Moodalbail #define	LIFC_DEFAULT	(LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES |\
286e91bba0SGirish Moodalbail 			LIFC_UNDER_IPMP)
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate typedef struct if_flags {
317c478bd9Sstevel@tonic-gate 	uint64_t iff_value;
327c478bd9Sstevel@tonic-gate 	char	*iff_name;
337c478bd9Sstevel@tonic-gate } if_flags_t;
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate static if_flags_t	if_flags_tbl[] = {
367c478bd9Sstevel@tonic-gate 	{ IFF_UP,		"UP" },
377c478bd9Sstevel@tonic-gate 	{ IFF_BROADCAST,	"BROADCAST" },
387c478bd9Sstevel@tonic-gate 	{ IFF_DEBUG,		"DEBUG" },
397c478bd9Sstevel@tonic-gate 	{ IFF_LOOPBACK,		"LOOPBACK" },
407c478bd9Sstevel@tonic-gate 	{ IFF_POINTOPOINT,	"POINTOPOINT" },
417c478bd9Sstevel@tonic-gate 	{ IFF_NOTRAILERS,	"NOTRAILERS" },
427c478bd9Sstevel@tonic-gate 	{ IFF_RUNNING,		"RUNNING" },
437c478bd9Sstevel@tonic-gate 	{ IFF_NOARP,		"NOARP" },
447c478bd9Sstevel@tonic-gate 	{ IFF_PROMISC,		"PROMISC" },
457c478bd9Sstevel@tonic-gate 	{ IFF_ALLMULTI,		"ALLMULTI" },
467c478bd9Sstevel@tonic-gate 	{ IFF_INTELLIGENT,	"INTELLIGENT" },
477c478bd9Sstevel@tonic-gate 	{ IFF_MULTICAST,	"MULTICAST" },
487c478bd9Sstevel@tonic-gate 	{ IFF_MULTI_BCAST,	"MULTI_BCAST" },
497c478bd9Sstevel@tonic-gate 	{ IFF_UNNUMBERED,	"UNNUMBERED" },
507c478bd9Sstevel@tonic-gate 	{ IFF_DHCPRUNNING,	"DHCP" },
517c478bd9Sstevel@tonic-gate 	{ IFF_PRIVATE,		"PRIVATE" },
527c478bd9Sstevel@tonic-gate 	{ IFF_NOXMIT,		"NOXMIT" },
537c478bd9Sstevel@tonic-gate 	{ IFF_NOLOCAL,		"NOLOCAL" },
547c478bd9Sstevel@tonic-gate 	{ IFF_DEPRECATED,	"DEPRECATED" },
557c478bd9Sstevel@tonic-gate 	{ IFF_ADDRCONF,		"ADDRCONF" },
567c478bd9Sstevel@tonic-gate 	{ IFF_ROUTER,		"ROUTER" },
577c478bd9Sstevel@tonic-gate 	{ IFF_NONUD,		"NONUD" },
587c478bd9Sstevel@tonic-gate 	{ IFF_ANYCAST,		"ANYCAST" },
597c478bd9Sstevel@tonic-gate 	{ IFF_NORTEXCH,		"NORTEXCH" },
607c478bd9Sstevel@tonic-gate 	{ IFF_IPV4,		"IPv4" },
617c478bd9Sstevel@tonic-gate 	{ IFF_IPV6,		"IPv6" },
627c478bd9Sstevel@tonic-gate 	{ IFF_NOFAILOVER,	"NOFAILOVER" },
637c478bd9Sstevel@tonic-gate 	{ IFF_FAILED,		"FAILED" },
647c478bd9Sstevel@tonic-gate 	{ IFF_STANDBY,		"STANDBY" },
657c478bd9Sstevel@tonic-gate 	{ IFF_INACTIVE,		"INACTIVE" },
667c478bd9Sstevel@tonic-gate 	{ IFF_OFFLINE,		"OFFLINE" },
677c478bd9Sstevel@tonic-gate 	{ IFF_XRESOLV,		"XRESOLV" },
687c478bd9Sstevel@tonic-gate 	{ IFF_COS_ENABLED,	"CoS" },
697c478bd9Sstevel@tonic-gate 	{ IFF_PREFERRED,	"PREFERRED" },
707c478bd9Sstevel@tonic-gate 	{ IFF_TEMPORARY,	"TEMPORARY" },
717c478bd9Sstevel@tonic-gate 	{ IFF_FIXEDMTU,		"FIXEDMTU" },
7269bb4bb4Scarlsonj 	{ IFF_VIRTUAL,		"VIRTUAL" },
73e11c3f44Smeem 	{ IFF_DUPLICATE,	"DUPLICATE" },
741cb875aeSCathy Zhou 	{ IFF_IPMP,		"IPMP"},
751cb875aeSCathy Zhou 	{ IFF_VRRP,		"VRRP"},
761cb875aeSCathy Zhou 	{ IFF_NOACCEPT,		"NOACCEPT"}
77e11c3f44Smeem };
78e11c3f44Smeem 
79e11c3f44Smeem typedef struct {
80e11c3f44Smeem 	const char		*ia_app;
81e11c3f44Smeem 	uint64_t		ia_flag;
82e11c3f44Smeem 	uint_t			ia_tries;
83e11c3f44Smeem } if_appflags_t;
84e11c3f44Smeem 
85e11c3f44Smeem static const if_appflags_t if_appflags_tbl[] = {
86e11c3f44Smeem 	{ "dhcpagent(1M)",	IFF_DHCPRUNNING,	1 },
87e11c3f44Smeem 	{ "in.ndpd(1M)",	IFF_ADDRCONF,		3 },
88e11c3f44Smeem 	{  NULL,		0,			0 }
897c478bd9Sstevel@tonic-gate };
907c478bd9Sstevel@tonic-gate 
912b24ab6bSSebastien Roy static dladm_handle_t	dlh;
922b24ab6bSSebastien Roy boolean_t		dlh_opened;
932b24ab6bSSebastien Roy static struct		lifreq lifr;
947906a3e0Smeem /* current interface name a particular function is accessing */
952b24ab6bSSebastien Roy static char		name[LIFNAMSIZ];
967c478bd9Sstevel@tonic-gate /* foreach interface saved name */
972b24ab6bSSebastien Roy static char		origname[LIFNAMSIZ];
982b24ab6bSSebastien Roy static int		setaddr;
996e91bba0SGirish Moodalbail static boolean_t	setaddr_done = _B_FALSE;
1002b24ab6bSSebastien Roy static boolean_t	ipsec_policy_set;
1012b24ab6bSSebastien Roy static boolean_t	ipsec_auth_covered;
1026e91bba0SGirish Moodalbail static ipadm_handle_t	iph;
1036e91bba0SGirish Moodalbail static ipadm_addrobj_t	ipaddr;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Make sure the algorithm variables hold more than the sizeof an algorithm
1077c478bd9Sstevel@tonic-gate  * in PF_KEY.  (For now, more than a uint8_t.)  The NO_***_?ALG indicates that
1087c478bd9Sstevel@tonic-gate  * there was no algorithm requested, and in the ipsec_req that service should
1097c478bd9Sstevel@tonic-gate  * be disabled.  (E.g. if ah_aalg remains NO_AH_AALG, then AH will be
1107c478bd9Sstevel@tonic-gate  * disabled on that tunnel.)
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate #define	NO_AH_AALG 256
1137c478bd9Sstevel@tonic-gate #define	NO_ESP_AALG 256
1147c478bd9Sstevel@tonic-gate #define	NO_ESP_EALG 256
1157c478bd9Sstevel@tonic-gate 
116e11c3f44Smeem int	s, s4, s6;
1177c478bd9Sstevel@tonic-gate int	af = AF_INET;	/* default address family */
1187c478bd9Sstevel@tonic-gate int	debug = 0;
1197c478bd9Sstevel@tonic-gate int	all = 0;	/* setifdhcp() needs to know this */
1207c478bd9Sstevel@tonic-gate int	verbose = 0;
1217c478bd9Sstevel@tonic-gate int	v4compat = 0;	/* Compatible printing format */
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Function prototypes for command functions.
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate static int	addif(char *arg, int64_t param);
127e11c3f44Smeem static int	inetipmp(char *arg, int64_t param);
1287c478bd9Sstevel@tonic-gate static int	inetplumb(char *arg, int64_t param);
1297c478bd9Sstevel@tonic-gate static int	inetunplumb(char *arg, int64_t param);
1307c478bd9Sstevel@tonic-gate static int	removeif(char *arg, int64_t param);
1317c478bd9Sstevel@tonic-gate static int	setdebugflag(char *arg, int64_t param);
1327c478bd9Sstevel@tonic-gate static int	setifaddr(char *arg, int64_t param);
1337c478bd9Sstevel@tonic-gate static int	setifbroadaddr(char *arg, int64_t param);
1347c478bd9Sstevel@tonic-gate static int	setifdstaddr(char *arg, int64_t param);
1357c478bd9Sstevel@tonic-gate static int	setifether(char *arg, int64_t param);
1367c478bd9Sstevel@tonic-gate static int	setifflags(char *arg, int64_t param);
1377c478bd9Sstevel@tonic-gate static int	setifindex(char *arg, int64_t param);
1387c478bd9Sstevel@tonic-gate static int	setifmetric(char *arg, int64_t param);
1397c478bd9Sstevel@tonic-gate static int	setifmtu(char *arg, int64_t param);
1407c478bd9Sstevel@tonic-gate static int	setifnetmask(char *arg, int64_t param);
1417c478bd9Sstevel@tonic-gate static int	setifprefixlen(char *arg, int64_t param);
1427c478bd9Sstevel@tonic-gate static int	setifrevarp(char *arg, int64_t param);
1437c478bd9Sstevel@tonic-gate static int	setifsubnet(char *arg, int64_t param);
1447c478bd9Sstevel@tonic-gate static int	setiftdst(char *arg, int64_t param);
1457c478bd9Sstevel@tonic-gate static int	setiftoken(char *arg, int64_t param);
1467c478bd9Sstevel@tonic-gate static int	setiftsrc(char *arg, int64_t param);
1477c478bd9Sstevel@tonic-gate static int	setverboseflag(char *arg, int64_t param);
1487c478bd9Sstevel@tonic-gate static int	set_tun_ah_alg(char *arg, int64_t param);
1497c478bd9Sstevel@tonic-gate static int	set_tun_esp_auth_alg(char *arg, int64_t param);
1507c478bd9Sstevel@tonic-gate static int	set_tun_esp_encr_alg(char *arg, int64_t param);
1517c478bd9Sstevel@tonic-gate static int	modlist(char *arg, int64_t param);
1527c478bd9Sstevel@tonic-gate static int	modinsert(char *arg, int64_t param);
1537c478bd9Sstevel@tonic-gate static int	modremove(char *arg, int64_t param);
1547c478bd9Sstevel@tonic-gate static int	setifgroupname(char *arg, int64_t param);
1557c478bd9Sstevel@tonic-gate static int	configinfo(char *arg, int64_t param);
156e11c3f44Smeem static void	print_config_flags(int af, uint64_t flags);
1577c478bd9Sstevel@tonic-gate static void	print_flags(uint64_t flags);
1586e91bba0SGirish Moodalbail static void	print_ifether(const char *ifname);
1597c478bd9Sstevel@tonic-gate static int	set_tun_encap_limit(char *arg, int64_t param);
1607c478bd9Sstevel@tonic-gate static int	clr_tun_encap_limit(char *arg, int64_t param);
1617c478bd9Sstevel@tonic-gate static int	set_tun_hop_limit(char *arg, int64_t param);
1627c478bd9Sstevel@tonic-gate static int	setzone(char *arg, int64_t param);
16345916cd2Sjpk static int	setallzones(char *arg, int64_t param);
1647c478bd9Sstevel@tonic-gate static int	setifsrc(char *arg, int64_t param);
165e11c3f44Smeem static int	lifnum(const char *ifname);
1666e91bba0SGirish Moodalbail static void	plumball(int, char **, int64_t, int64_t, int64_t);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Address family specific function prototypes.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate static void	in_getaddr(char *s, struct sockaddr *saddr, int *plenp);
1727c478bd9Sstevel@tonic-gate static void	in_status(int force, uint64_t flags);
1737c478bd9Sstevel@tonic-gate static void	in_configinfo(int force, uint64_t flags);
1747c478bd9Sstevel@tonic-gate static void	in6_getaddr(char *s, struct sockaddr *saddr, int *plenp);
1757c478bd9Sstevel@tonic-gate static void	in6_status(int force, uint64_t flags);
1767c478bd9Sstevel@tonic-gate static void	in6_configinfo(int force, uint64_t flags);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate  * Misc support functions
1807c478bd9Sstevel@tonic-gate  */
181d62bc4baSyz static boolean_t	ni_entry(const char *, void *);
1826e91bba0SGirish Moodalbail static void		foreachinterface(int argc, char *argv[],
1836e91bba0SGirish Moodalbail 			    int af, int64_t onflags, int64_t offflags,
1846e91bba0SGirish Moodalbail 			    int64_t lifc_flags);
1856e91bba0SGirish Moodalbail static void		ifconfig(int argc, char *argv[], int af,
1866e91bba0SGirish Moodalbail 			    struct ifaddrs *ifa);
187dd7a6f5fSkcpoon static boolean_t	in_getmask(struct sockaddr_in *saddr,
188dd7a6f5fSkcpoon 			    boolean_t addr_set);
1896e91bba0SGirish Moodalbail static int		in_getprefixlen(char *addr, boolean_t slash, int plen);
1907c478bd9Sstevel@tonic-gate static boolean_t	in_prefixlentomask(int prefixlen, int maxlen,
1917c478bd9Sstevel@tonic-gate 			    uchar_t *mask);
1926e91bba0SGirish Moodalbail static void		status(void);
1936e91bba0SGirish Moodalbail static void		ifstatus(const char *ifname);
1946e91bba0SGirish Moodalbail static void		tun_status(datalink_id_t);
1956e91bba0SGirish Moodalbail static void		usage(void);
1966e91bba0SGirish Moodalbail static int		setifdhcp(const char *caller, const char *ifname,
1976e91bba0SGirish Moodalbail 			    int argc, char *argv[]);
1986e91bba0SGirish Moodalbail static int		ip_domux2fd(int *, int *, int *, int *, int *);
1996e91bba0SGirish Moodalbail static int		ip_plink(int, int, int, int, int);
2006e91bba0SGirish Moodalbail static int		modop(char *arg, char op);
2016e91bba0SGirish Moodalbail static int		find_all_interfaces(struct lifconf *lifcp, char **buf,
2026e91bba0SGirish Moodalbail 			    int64_t lifc_flags);
2036e91bba0SGirish Moodalbail static int		create_ipmp(const char *grname, int af,
2046e91bba0SGirish Moodalbail 			    const char *ifname, boolean_t implicit);
2056e91bba0SGirish Moodalbail static void		start_ipmp_daemon(void);
2066e91bba0SGirish Moodalbail static boolean_t 	ifaddr_up(ifaddrlistx_t *ifaddrp);
2076e91bba0SGirish Moodalbail static boolean_t 	ifaddr_down(ifaddrlistx_t *ifaddrp);
2082b24ab6bSSebastien Roy static dladm_status_t	ifconfig_dladm_open(const char *, datalink_class_t,
2096e91bba0SGirish Moodalbail 			    datalink_id_t *);
2106e91bba0SGirish Moodalbail static void		dladmerr_exit(dladm_status_t status, const char *str);
2116e91bba0SGirish Moodalbail static void		ipadmerr_exit(ipadm_status_t status, const char *str);
2126e91bba0SGirish Moodalbail static boolean_t	ifconfig_use_libipadm(int, const char *);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate #define	max(a, b)	((a) < (b) ? (b) : (a))
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * DHCP_EXIT_IF_FAILURE indicates that the operation failed, but if there
2187c478bd9Sstevel@tonic-gate  * are more interfaces to act on (i.e., ifconfig was invoked with -a), keep
2197c478bd9Sstevel@tonic-gate  * on going rather than exit with an error.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate #define	DHCP_EXIT_IF_FAILURE	-1
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate #define	NEXTARG		0xffffff	/* command takes an argument */
2257c478bd9Sstevel@tonic-gate #define	OPTARG		0xfffffe 	/* command takes an optional argument */
2267c478bd9Sstevel@tonic-gate #define	AF_ANY		(-1)
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /* Refer to the comments in ifconfig() on the netmask "hack" */
2297c478bd9Sstevel@tonic-gate #define	NETMASK_CMD	"netmask"
2307c478bd9Sstevel@tonic-gate struct sockaddr_storage	g_netmask;
231dd7a6f5fSkcpoon enum { G_NETMASK_NIL, G_NETMASK_PENDING, G_NETMASK_SET }
232dd7a6f5fSkcpoon     g_netmask_set = G_NETMASK_NIL;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate struct	cmd {
2357c478bd9Sstevel@tonic-gate 	char		*c_name;
2367c478bd9Sstevel@tonic-gate 	int64_t		c_parameter;	/* NEXTARG means next argv */
2377c478bd9Sstevel@tonic-gate 	int		(*c_func)(char *, int64_t);
2387c478bd9Sstevel@tonic-gate 	int		c_abortonfail;	/* don't continue parsing args */
2397c478bd9Sstevel@tonic-gate 					/* for the current interface */
2407c478bd9Sstevel@tonic-gate 	int	c_af;			/* address family restrictions */
2417c478bd9Sstevel@tonic-gate } cmds[] = {
2427c478bd9Sstevel@tonic-gate 	{ "up",		IFF_UP,		setifflags,	0,	AF_ANY },
2437c478bd9Sstevel@tonic-gate 	{ "down",	-IFF_UP,	setifflags,	0,	AF_ANY },
2447c478bd9Sstevel@tonic-gate 	{ "trailers",	-IFF_NOTRAILERS, setifflags,	0,	AF_ANY },
2457c478bd9Sstevel@tonic-gate 	{ "-trailers",	IFF_NOTRAILERS,	setifflags,	0,	AF_ANY },
2467c478bd9Sstevel@tonic-gate 	{ "arp",	-IFF_NOARP,	setifflags,	0,	AF_INET },
2477c478bd9Sstevel@tonic-gate 	{ "-arp",	IFF_NOARP,	setifflags,	0,	AF_INET },
2487c478bd9Sstevel@tonic-gate 	{ "router",	IFF_ROUTER,	setifflags,	0,	AF_ANY },
2497c478bd9Sstevel@tonic-gate 	{ "-router",	-IFF_ROUTER,	setifflags,	0,	AF_ANY },
2507c478bd9Sstevel@tonic-gate 	{ "private",	IFF_PRIVATE,	setifflags,	0,	AF_ANY },
2517c478bd9Sstevel@tonic-gate 	{ "-private",	-IFF_PRIVATE,	setifflags,	0,	AF_ANY },
2527c478bd9Sstevel@tonic-gate 	{ "xmit",	-IFF_NOXMIT,	setifflags,	0,	AF_ANY },
2537c478bd9Sstevel@tonic-gate 	{ "-xmit",	IFF_NOXMIT,	setifflags,	0,	AF_ANY },
2547c478bd9Sstevel@tonic-gate 	{ "-nud",	IFF_NONUD,	setifflags,	0,	AF_INET6 },
2557c478bd9Sstevel@tonic-gate 	{ "nud",	-IFF_NONUD,	setifflags,	0,	AF_INET6 },
2567c478bd9Sstevel@tonic-gate 	{ "anycast",	IFF_ANYCAST,	setifflags,	0,	AF_ANY },
2577c478bd9Sstevel@tonic-gate 	{ "-anycast",	-IFF_ANYCAST,	setifflags,	0,	AF_ANY },
2587c478bd9Sstevel@tonic-gate 	{ "local",	-IFF_NOLOCAL,	setifflags,	0,	AF_ANY },
2597c478bd9Sstevel@tonic-gate 	{ "-local",	IFF_NOLOCAL,	setifflags,	0,	AF_ANY },
2607c478bd9Sstevel@tonic-gate 	{ "deprecated",	IFF_DEPRECATED,	setifflags,	0,	AF_ANY },
2617c478bd9Sstevel@tonic-gate 	{ "-deprecated", -IFF_DEPRECATED, setifflags,	0,	AF_ANY },
2627c478bd9Sstevel@tonic-gate 	{ "preferred",	IFF_PREFERRED,	setifflags,	0,	AF_INET6 },
2637c478bd9Sstevel@tonic-gate 	{ "-preferred",	-IFF_PREFERRED,	setifflags,	0,	AF_INET6 },
2647c478bd9Sstevel@tonic-gate 	{ "debug",	0,		setdebugflag,	0,	AF_ANY },
2657c478bd9Sstevel@tonic-gate 	{ "verbose",	0,		setverboseflag,	0,	AF_ANY },
2667c478bd9Sstevel@tonic-gate 	{ NETMASK_CMD,	NEXTARG,	setifnetmask,	0,	AF_INET },
2677c478bd9Sstevel@tonic-gate 	{ "metric",	NEXTARG,	setifmetric,	0,	AF_ANY },
2687c478bd9Sstevel@tonic-gate 	{ "mtu",	NEXTARG,	setifmtu,	0,	AF_ANY },
2697c478bd9Sstevel@tonic-gate 	{ "index",	NEXTARG,	setifindex,	0,	AF_ANY },
2707c478bd9Sstevel@tonic-gate 	{ "broadcast",	NEXTARG,	setifbroadaddr,	0,	AF_INET },
2717c478bd9Sstevel@tonic-gate 	{ "auto-revarp", 0,		setifrevarp,	1,	AF_INET },
272e11c3f44Smeem 	{ "ipmp",	0,		inetipmp,	1,	AF_ANY },
2737c478bd9Sstevel@tonic-gate 	{ "plumb",	0,		inetplumb,	1,	AF_ANY },
2747c478bd9Sstevel@tonic-gate 	{ "unplumb",	0,		inetunplumb,	0,	AF_ANY },
2757c478bd9Sstevel@tonic-gate 	{ "subnet",	NEXTARG,	setifsubnet,	0,	AF_ANY },
2767c478bd9Sstevel@tonic-gate 	{ "token",	NEXTARG,	setiftoken,	0,	AF_INET6 },
2777c478bd9Sstevel@tonic-gate 	{ "tsrc",	NEXTARG,	setiftsrc,	0,	AF_ANY },
2787c478bd9Sstevel@tonic-gate 	{ "tdst",	NEXTARG,	setiftdst,	0,	AF_ANY },
2797c478bd9Sstevel@tonic-gate 	{ "encr_auth_algs", NEXTARG,	set_tun_esp_auth_alg, 0, AF_ANY },
2807c478bd9Sstevel@tonic-gate 	{ "encr_algs",	NEXTARG,	set_tun_esp_encr_alg, 0, AF_ANY },
2817c478bd9Sstevel@tonic-gate 	{ "auth_algs",	NEXTARG,	set_tun_ah_alg,	0,	AF_ANY },
2827c478bd9Sstevel@tonic-gate 	{ "addif",	NEXTARG,	addif,		1,	AF_ANY },
2837c478bd9Sstevel@tonic-gate 	{ "removeif",	NEXTARG,	removeif,	1,	AF_ANY },
2847c478bd9Sstevel@tonic-gate 	{ "modlist",	0,		modlist,	1,	AF_ANY },
2857c478bd9Sstevel@tonic-gate 	{ "modinsert",	NEXTARG,	modinsert,	1,	AF_ANY },
2867c478bd9Sstevel@tonic-gate 	{ "modremove",	NEXTARG,	modremove,	1,	AF_ANY },
2877c478bd9Sstevel@tonic-gate 	{ "failover",	-IFF_NOFAILOVER, setifflags,	1,	AF_ANY },
2887c478bd9Sstevel@tonic-gate 	{ "-failover",	IFF_NOFAILOVER, setifflags,	1,	AF_ANY },
2897c478bd9Sstevel@tonic-gate 	{ "standby",	IFF_STANDBY,	setifflags,	1,	AF_ANY },
2907c478bd9Sstevel@tonic-gate 	{ "-standby",	-IFF_STANDBY,	setifflags,	1,	AF_ANY },
2917c478bd9Sstevel@tonic-gate 	{ "failed",	IFF_FAILED,	setifflags,	1,	AF_ANY },
2927c478bd9Sstevel@tonic-gate 	{ "-failed",	-IFF_FAILED,	setifflags,	1,	AF_ANY },
2937c478bd9Sstevel@tonic-gate 	{ "group",	NEXTARG,	setifgroupname,	1,	AF_ANY },
2947c478bd9Sstevel@tonic-gate 	{ "configinfo",	0,		configinfo,	1,	AF_ANY },
2957906a3e0Smeem 	{ "encaplimit",	NEXTARG,	set_tun_encap_limit,	0, AF_ANY },
2967906a3e0Smeem 	{ "-encaplimit", 0,		clr_tun_encap_limit,	0, AF_ANY },
2977906a3e0Smeem 	{ "thoplimit",	NEXTARG,	set_tun_hop_limit,	0, AF_ANY },
2987c478bd9Sstevel@tonic-gate 	{ "set",	NEXTARG,	setifaddr,	0,	AF_ANY },
2997c478bd9Sstevel@tonic-gate 	{ "destination", NEXTARG,	setifdstaddr,	0,	AF_ANY },
3007c478bd9Sstevel@tonic-gate 	{ "zone",	NEXTARG,	setzone,	0,	AF_ANY },
3017c478bd9Sstevel@tonic-gate 	{ "-zone",	0,		setzone,	0,	AF_ANY },
30245916cd2Sjpk 	{ "all-zones",	0,		setallzones,	0,	AF_ANY },
3037c478bd9Sstevel@tonic-gate 	{ "ether",	OPTARG,		setifether,	0,	AF_ANY },
3047c478bd9Sstevel@tonic-gate 	{ "usesrc",	NEXTARG,	setifsrc,	0,	AF_ANY },
305f7d61273Smeem 
306f7d61273Smeem 	/*
307f7d61273Smeem 	 * NOTE: any additions to this table must also be applied to ifparse
308f7d61273Smeem 	 *	(usr/src/cmd/cmd-inet/sbin/ifparse/ifparse.c)
309f7d61273Smeem 	 */
310f7d61273Smeem 
3117c478bd9Sstevel@tonic-gate 	{ 0,		0,		setifaddr,	0,	AF_ANY },
3127c478bd9Sstevel@tonic-gate 	{ 0,		0,		setifdstaddr,	0,	AF_ANY },
3137c478bd9Sstevel@tonic-gate 	{ 0,		0,		0,		0,	0 },
3147c478bd9Sstevel@tonic-gate };
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate typedef struct if_config_cmd {
3187c478bd9Sstevel@tonic-gate 	uint64_t	iff_flag;
319e11c3f44Smeem 	int		iff_af;
3207c478bd9Sstevel@tonic-gate 	char		*iff_name;
3217c478bd9Sstevel@tonic-gate } if_config_cmd_t;
3227c478bd9Sstevel@tonic-gate 
323e11c3f44Smeem /*
324e11c3f44Smeem  * NOTE: print_config_flags() processes this table in order, so we put "up"
325e11c3f44Smeem  * last so that we can be sure "-failover" will take effect first.  Otherwise,
326e11c3f44Smeem  * IPMP test addresses will erroneously migrate to the IPMP interface.
327e11c3f44Smeem  */
3287c478bd9Sstevel@tonic-gate static if_config_cmd_t	if_config_cmd_tbl[] = {
329e11c3f44Smeem 	{ IFF_NOTRAILERS,	AF_UNSPEC,	"-trailers"	},
330e11c3f44Smeem 	{ IFF_PRIVATE,		AF_UNSPEC,	"private"	},
331e11c3f44Smeem 	{ IFF_NOXMIT,		AF_UNSPEC,	"-xmit"		},
332e11c3f44Smeem 	{ IFF_ANYCAST,		AF_INET6,	"anycast"	},
333e11c3f44Smeem 	{ IFF_NOLOCAL,		AF_UNSPEC,	"-local"	},
334e11c3f44Smeem 	{ IFF_DEPRECATED,	AF_UNSPEC,	"deprecated"	},
335e11c3f44Smeem 	{ IFF_NOFAILOVER,	AF_UNSPEC,	"-failover"	},
336e11c3f44Smeem 	{ IFF_STANDBY,		AF_UNSPEC,	"standby"	},
337e11c3f44Smeem 	{ IFF_FAILED,		AF_UNSPEC,	"failed"	},
338e11c3f44Smeem 	{ IFF_PREFERRED,	AF_UNSPEC,	"preferred"	},
339e11c3f44Smeem 	{ IFF_NONUD,		AF_INET6,	"-nud"		},
340e11c3f44Smeem 	{ IFF_NOARP,		AF_INET,	"-arp"		},
341e11c3f44Smeem 	{ IFF_UP,		AF_UNSPEC, 	"up" 		},
342e11c3f44Smeem 	{ 0,			0,		NULL		},
3437c478bd9Sstevel@tonic-gate };
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate typedef struct ni {
3467c478bd9Sstevel@tonic-gate 	char		ni_name[LIFNAMSIZ];
3477c478bd9Sstevel@tonic-gate 	struct ni	*ni_next;
3487c478bd9Sstevel@tonic-gate } ni_t;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate static ni_t	*ni_list = NULL;
3517c478bd9Sstevel@tonic-gate static int	num_ni = 0;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate /* End defines and structure definitions for ifconfig -a plumb */
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate /* Known address families */
3567c478bd9Sstevel@tonic-gate struct afswtch {
3577c478bd9Sstevel@tonic-gate 	char *af_name;
3587c478bd9Sstevel@tonic-gate 	short af_af;
3597c478bd9Sstevel@tonic-gate 	void (*af_status)();
3607c478bd9Sstevel@tonic-gate 	void (*af_getaddr)();
3617c478bd9Sstevel@tonic-gate 	void (*af_configinfo)();
3627c478bd9Sstevel@tonic-gate } afs[] = {
3637c478bd9Sstevel@tonic-gate 	{ "inet", AF_INET, in_status, in_getaddr, in_configinfo },
3647c478bd9Sstevel@tonic-gate 	{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_configinfo },
3657c478bd9Sstevel@tonic-gate 	{ 0, 0,	0, 0, 0 }
3667c478bd9Sstevel@tonic-gate };
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate struct afswtch *afp;	/* the address family being set or asked about */
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate int
3737c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3747c478bd9Sstevel@tonic-gate {
3752b24ab6bSSebastien Roy 	int64_t		lifc_flags;
3762b24ab6bSSebastien Roy 	char		*default_ip_str;
3776e91bba0SGirish Moodalbail 	ipadm_status_t	istatus;
3787c478bd9Sstevel@tonic-gate 
3796e91bba0SGirish Moodalbail 	lifc_flags = LIFC_DEFAULT;
380e11c3f44Smeem 
3817c478bd9Sstevel@tonic-gate 	if (argc < 2) {
3827c478bd9Sstevel@tonic-gate 		usage();
3837c478bd9Sstevel@tonic-gate 		exit(1);
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 	argc--, argv++;
3867c478bd9Sstevel@tonic-gate 	if (strlen(*argv) > sizeof (name) - 1) {
3877c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: interface name too long\n", *argv);
3887c478bd9Sstevel@tonic-gate 		exit(1);
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 	(void) strncpy(name, *argv, sizeof (name));
3917c478bd9Sstevel@tonic-gate 	name[sizeof (name) - 1] = '\0';
3927c478bd9Sstevel@tonic-gate 	(void) strncpy(origname, name, sizeof (origname));	/* For addif */
3937c478bd9Sstevel@tonic-gate 	default_ip_str = NULL;
3947c478bd9Sstevel@tonic-gate 	v4compat = get_compat_flag(&default_ip_str);
3957c478bd9Sstevel@tonic-gate 	if (v4compat == DEFAULT_PROT_BAD_VALUE) {
3967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3977c478bd9Sstevel@tonic-gate 		    "ifconfig: %s: Bad value for %s in %s\n", default_ip_str,
3987c478bd9Sstevel@tonic-gate 		    DEFAULT_IP, INET_DEFAULT_FILE);
3997c478bd9Sstevel@tonic-gate 		free(default_ip_str);
4007c478bd9Sstevel@tonic-gate 		exit(2);
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 	free(default_ip_str);
4037c478bd9Sstevel@tonic-gate 	argc--, argv++;
4047c478bd9Sstevel@tonic-gate 	if (argc > 0) {
4057c478bd9Sstevel@tonic-gate 		struct afswtch *myafp;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 		for (myafp = afp = afs; myafp->af_name; myafp++) {
4087c478bd9Sstevel@tonic-gate 			if (strcmp(myafp->af_name, *argv) == 0) {
4097c478bd9Sstevel@tonic-gate 				afp = myafp; argc--; argv++;
4107c478bd9Sstevel@tonic-gate 				break;
4117c478bd9Sstevel@tonic-gate 			}
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 		af = lifr.lifr_addr.ss_family = afp->af_af;
4147c478bd9Sstevel@tonic-gate 		if (af == AF_INET6) {
4157c478bd9Sstevel@tonic-gate 			v4compat = 0;
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	s = socket(SOCKET_AF(af), SOCK_DGRAM, 0);
420e11c3f44Smeem 	s4 = socket(AF_INET, SOCK_DGRAM, 0);
421e11c3f44Smeem 	s6 = socket(AF_INET6, SOCK_DGRAM, 0);
422e11c3f44Smeem 	if (s == -1 || s4 == -1 || s6 == -1)
4237c478bd9Sstevel@tonic-gate 		Perror0_exit("socket");
4246e91bba0SGirish Moodalbail 	/*
4256e91bba0SGirish Moodalbail 	 * Open the global libipadm handle. The flag IPH_LEGACY has to
4266e91bba0SGirish Moodalbail 	 * be specified to indicate that logical interface names will
4276e91bba0SGirish Moodalbail 	 * be used during interface creation and address creation.
4286e91bba0SGirish Moodalbail 	 */
4296e91bba0SGirish Moodalbail 	if ((istatus = ipadm_open(&iph, IPH_LEGACY)) != IPADM_SUCCESS)
4306e91bba0SGirish Moodalbail 		ipadmerr_exit(istatus, "unable to open handle to libipadm");
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	/*
4337c478bd9Sstevel@tonic-gate 	 * Special interface names is any combination of these flags.
4347c478bd9Sstevel@tonic-gate 	 * Note that due to the ifconfig syntax they have to be combined
4357c478bd9Sstevel@tonic-gate 	 * as a single '-' option.
4367c478bd9Sstevel@tonic-gate 	 *	-a	All interfaces
4377c478bd9Sstevel@tonic-gate 	 *	-u	"up" interfaces
4387c478bd9Sstevel@tonic-gate 	 *	-d	"down" interfaces
4397c478bd9Sstevel@tonic-gate 	 *	-D	Interfaces not controlled by DHCP
4407c478bd9Sstevel@tonic-gate 	 *	-4	IPv4 interfaces
4417c478bd9Sstevel@tonic-gate 	 *	-6	IPv6 interfaces
4427c478bd9Sstevel@tonic-gate 	 *	-X	Turn on debug (not documented)
4437c478bd9Sstevel@tonic-gate 	 *	-v	Turn on verbose
4447c478bd9Sstevel@tonic-gate 	 *	-Z	Only interfaces in caller's zone
4457c478bd9Sstevel@tonic-gate 	 */
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	if (name[0] == '-') {
4487c478bd9Sstevel@tonic-gate 		/* One or more options */
4497c478bd9Sstevel@tonic-gate 		int64_t onflags = 0;
4507c478bd9Sstevel@tonic-gate 		int64_t offflags = 0;
4517c478bd9Sstevel@tonic-gate 		int c;
4527c478bd9Sstevel@tonic-gate 		char *av[2] = { "ifconfig", name };
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 		while ((c = getopt(2, av, "audDXZ46v")) != -1) {
4557c478bd9Sstevel@tonic-gate 			switch ((char)c) {
4567c478bd9Sstevel@tonic-gate 			case 'a':
4577c478bd9Sstevel@tonic-gate 				all = 1;
4587c478bd9Sstevel@tonic-gate 				break;
4597c478bd9Sstevel@tonic-gate 			case 'u':
4607c478bd9Sstevel@tonic-gate 				onflags |= IFF_UP;
4617c478bd9Sstevel@tonic-gate 				break;
4627c478bd9Sstevel@tonic-gate 			case 'd':
4637c478bd9Sstevel@tonic-gate 				offflags |= IFF_UP;
4647c478bd9Sstevel@tonic-gate 				break;
4657c478bd9Sstevel@tonic-gate 			case 'D':
4667c478bd9Sstevel@tonic-gate 				offflags |= IFF_DHCPRUNNING;
4677c478bd9Sstevel@tonic-gate 				break;
4687c478bd9Sstevel@tonic-gate 			case 'X':
4697c478bd9Sstevel@tonic-gate 				debug += 3;
4707c478bd9Sstevel@tonic-gate 				break;
4717c478bd9Sstevel@tonic-gate 			case 'Z':
4727c478bd9Sstevel@tonic-gate 				lifc_flags &= ~LIFC_ALLZONES;
4737c478bd9Sstevel@tonic-gate 				break;
4747c478bd9Sstevel@tonic-gate 			case '4':
4757c478bd9Sstevel@tonic-gate 				/*
4767c478bd9Sstevel@tonic-gate 				 * -4 is not a compatable flag, therefore
4777c478bd9Sstevel@tonic-gate 				 * we assume they want v4compat turned off
4787c478bd9Sstevel@tonic-gate 				 */
4797c478bd9Sstevel@tonic-gate 				v4compat = 0;
4807c478bd9Sstevel@tonic-gate 				onflags |= IFF_IPV4;
4817c478bd9Sstevel@tonic-gate 				break;
4827c478bd9Sstevel@tonic-gate 			case '6':
4837c478bd9Sstevel@tonic-gate 				/*
4847c478bd9Sstevel@tonic-gate 				 * If they want IPv6, well then we'll assume
4857c478bd9Sstevel@tonic-gate 				 * they don't want IPv4 compat
4867c478bd9Sstevel@tonic-gate 				 */
4877c478bd9Sstevel@tonic-gate 				v4compat = 0;
4887c478bd9Sstevel@tonic-gate 				onflags |= IFF_IPV6;
4897c478bd9Sstevel@tonic-gate 				break;
4907c478bd9Sstevel@tonic-gate 			case 'v':
4917c478bd9Sstevel@tonic-gate 				verbose = 1;
4927c478bd9Sstevel@tonic-gate 				break;
4937c478bd9Sstevel@tonic-gate 			case '?':
4947c478bd9Sstevel@tonic-gate 				usage();
4957c478bd9Sstevel@tonic-gate 				exit(1);
4967c478bd9Sstevel@tonic-gate 			}
4977c478bd9Sstevel@tonic-gate 		}
4987c478bd9Sstevel@tonic-gate 		if (!all) {
4997c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
500c7e4935fSss 			    "ifconfig: %s: no such interface\n", name);
5017c478bd9Sstevel@tonic-gate 			exit(1);
5027c478bd9Sstevel@tonic-gate 		}
5036e91bba0SGirish Moodalbail 		foreachinterface(argc, argv, af, onflags, offflags,
5047c478bd9Sstevel@tonic-gate 		    lifc_flags);
5057c478bd9Sstevel@tonic-gate 	} else {
5066e91bba0SGirish Moodalbail 		ifconfig(argc, argv, af, NULL);
5077c478bd9Sstevel@tonic-gate 	}
5086e91bba0SGirish Moodalbail 	ipadm_close(iph);
5097c478bd9Sstevel@tonic-gate 	return (0);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate /*
5136e91bba0SGirish Moodalbail  * For each interface, call ifconfig(argc, argv, af, ifa).
5147c478bd9Sstevel@tonic-gate  * Only call function if onflags and offflags are set or clear, respectively,
5157c478bd9Sstevel@tonic-gate  * in the interfaces flags field.
5167c478bd9Sstevel@tonic-gate  */
5177c478bd9Sstevel@tonic-gate static void
5186e91bba0SGirish Moodalbail foreachinterface(int argc, char *argv[], int af,
5197c478bd9Sstevel@tonic-gate     int64_t onflags, int64_t offflags, int64_t lifc_flags)
5207c478bd9Sstevel@tonic-gate {
5216e91bba0SGirish Moodalbail 	ipadm_addr_info_t *ainfo, *ainfop;
5226e91bba0SGirish Moodalbail 	struct ifaddrs *ifa;
5236e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/*
5267c478bd9Sstevel@tonic-gate 	 * Special case:
5272b24ab6bSSebastien Roy 	 * ifconfig -a plumb should find all network interfaces in the current
5282b24ab6bSSebastien Roy 	 * zone.
5297c478bd9Sstevel@tonic-gate 	 */
5307c478bd9Sstevel@tonic-gate 	if (argc > 0 && (strcmp(*argv, "plumb") == 0)) {
5316e91bba0SGirish Moodalbail 		plumball(argc, argv, onflags, offflags, lifc_flags);
5326e91bba0SGirish Moodalbail 		return;
5337c478bd9Sstevel@tonic-gate 	}
5346e91bba0SGirish Moodalbail 	/* Get all addresses in kernel including addresses that are zero. */
5356e91bba0SGirish Moodalbail 	istatus = ipadm_addr_info(iph, NULL, &ainfo, IPADM_OPT_ZEROADDR,
5366e91bba0SGirish Moodalbail 	    lifc_flags);
5376e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS)
5386e91bba0SGirish Moodalbail 		ipadmerr_exit(istatus, "could not get addresses from kernel");
5397c478bd9Sstevel@tonic-gate 
5406e91bba0SGirish Moodalbail 	/*
5416e91bba0SGirish Moodalbail 	 * For each logical interface, call ifconfig() with the
5426e91bba0SGirish Moodalbail 	 * given arguments.
5436e91bba0SGirish Moodalbail 	 */
5446e91bba0SGirish Moodalbail 	for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop)) {
5456e91bba0SGirish Moodalbail 		if (ainfop->ia_state == IFA_DISABLED)
5466e91bba0SGirish Moodalbail 			continue;
5476e91bba0SGirish Moodalbail 		ifa = &ainfop->ia_ifa;
5487c478bd9Sstevel@tonic-gate 		if (onflags || offflags) {
5496e91bba0SGirish Moodalbail 			if ((ifa->ifa_flags & onflags) != onflags)
5507c478bd9Sstevel@tonic-gate 				continue;
5516e91bba0SGirish Moodalbail 			if ((~ifa->ifa_flags & offflags) != offflags)
5527c478bd9Sstevel@tonic-gate 				continue;
5537c478bd9Sstevel@tonic-gate 		}
5546e91bba0SGirish Moodalbail 		s = (ifa->ifa_addr->ss_family == AF_INET ? s4 : s6);
5556e91bba0SGirish Moodalbail 		(void) strncpy(name, ifa->ifa_name, sizeof (name));
5566e91bba0SGirish Moodalbail 		(void) strncpy(origname, name, sizeof (origname));
5576e91bba0SGirish Moodalbail 		ifconfig(argc, argv, af, ifa);
5586e91bba0SGirish Moodalbail 	}
5596e91bba0SGirish Moodalbail 	ipadm_free_addr_info(ainfo);
5606e91bba0SGirish Moodalbail }
5617c478bd9Sstevel@tonic-gate 
5626e91bba0SGirish Moodalbail /*
5636e91bba0SGirish Moodalbail  * Used for `ifconfig -a plumb'. Finds all datalinks and plumbs the interface.
5646e91bba0SGirish Moodalbail  */
5656e91bba0SGirish Moodalbail static void
5666e91bba0SGirish Moodalbail plumball(int argc, char *argv[], int64_t onflags, int64_t offflags,
5676e91bba0SGirish Moodalbail     int64_t lifc_flags)
5686e91bba0SGirish Moodalbail {
5696e91bba0SGirish Moodalbail 	int n;
5706e91bba0SGirish Moodalbail 	struct lifreq *lifrp;
5716e91bba0SGirish Moodalbail 	struct lifconf lifc;
5726e91bba0SGirish Moodalbail 	char *buf;
5737c478bd9Sstevel@tonic-gate 
5746e91bba0SGirish Moodalbail 	if (onflags != 0 || offflags != 0) {
5756e91bba0SGirish Moodalbail 		(void) fprintf(stderr, "ifconfig: invalid syntax used to "
5766e91bba0SGirish Moodalbail 		    "plumb all interfaces.\n");
5776e91bba0SGirish Moodalbail 		exit(1);
5786e91bba0SGirish Moodalbail 	}
5796e91bba0SGirish Moodalbail 
5806e91bba0SGirish Moodalbail 	if (find_all_interfaces(&lifc, &buf, lifc_flags) != 0 ||
5816e91bba0SGirish Moodalbail 	    lifc.lifc_len == 0)
5826e91bba0SGirish Moodalbail 		return;
5837c478bd9Sstevel@tonic-gate 
5846e91bba0SGirish Moodalbail 	lifrp = lifc.lifc_req;
5856e91bba0SGirish Moodalbail 	for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) {
5867c478bd9Sstevel@tonic-gate 		/*
5877c478bd9Sstevel@tonic-gate 		 * Reset global state
5887c478bd9Sstevel@tonic-gate 		 * setaddr: Used by parser to tear apart source and dest
5897c478bd9Sstevel@tonic-gate 		 * name and origname contain the name of the 'current'
5907c478bd9Sstevel@tonic-gate 		 * interface.
5917c478bd9Sstevel@tonic-gate 		 */
5927c478bd9Sstevel@tonic-gate 		setaddr = 0;
5937c478bd9Sstevel@tonic-gate 		(void) strncpy(name, lifrp->lifr_name, sizeof (name));
5947c478bd9Sstevel@tonic-gate 		(void) strncpy(origname, name, sizeof (origname));
5956e91bba0SGirish Moodalbail 		ifconfig(argc, argv, af, NULL);
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate /*
6006e91bba0SGirish Moodalbail  * Parses the interface name and the command in argv[]. Calls the
6016e91bba0SGirish Moodalbail  * appropriate callback function for the given command from `cmds[]'
6026e91bba0SGirish Moodalbail  * table.
6036e91bba0SGirish Moodalbail  * If there is no command specified, it prints all addresses.
6047c478bd9Sstevel@tonic-gate  */
6057c478bd9Sstevel@tonic-gate static void
6066e91bba0SGirish Moodalbail ifconfig(int argc, char *argv[], int af, struct ifaddrs *ifa)
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate 	static boolean_t scan_netmask = _B_FALSE;
6097c478bd9Sstevel@tonic-gate 	int ret;
6106e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
6116e91bba0SGirish Moodalbail 	struct lifreq lifr;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	if (argc == 0) {
6147c478bd9Sstevel@tonic-gate 		status();
6157c478bd9Sstevel@tonic-gate 		return;
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (strcmp(*argv, "auto-dhcp") == 0 || strcmp(*argv, "dhcp") == 0) {
619d04ccbb3Scarlsonj 		/*
620d04ccbb3Scarlsonj 		 * Some errors are ignored in the case where more than one
621d04ccbb3Scarlsonj 		 * interface is being operated on.
622d04ccbb3Scarlsonj 		 */
623d04ccbb3Scarlsonj 		ret = setifdhcp("ifconfig", name, argc, argv);
624d04ccbb3Scarlsonj 		if (ret == DHCP_EXIT_IF_FAILURE) {
625d04ccbb3Scarlsonj 			if (!all)
626d04ccbb3Scarlsonj 				exit(DHCP_EXIT_FAILURE);
627d04ccbb3Scarlsonj 		} else if (ret != DHCP_EXIT_SUCCESS) {
628d04ccbb3Scarlsonj 			exit(ret);
629d04ccbb3Scarlsonj 		}
6307c478bd9Sstevel@tonic-gate 		return;
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	/*
6347c478bd9Sstevel@tonic-gate 	 * The following is a "hack" to get around the existing interface
6357c478bd9Sstevel@tonic-gate 	 * setting mechanism.  Currently, each interface attribute,
6367c478bd9Sstevel@tonic-gate 	 * such as address, netmask, broadcast, ... is set separately.  But
6377c478bd9Sstevel@tonic-gate 	 * sometimes two or more attributes must be set together.  For
6387c478bd9Sstevel@tonic-gate 	 * example, setting an address without a netmask does not make sense.
6397c478bd9Sstevel@tonic-gate 	 * Yet they can be set separately for IPv4 address using the current
6407c478bd9Sstevel@tonic-gate 	 * ifconfig(1M) syntax.  The kernel then "infers" the correct netmask
6417c478bd9Sstevel@tonic-gate 	 * using the deprecated "IP address classes."  This is simply not
6427c478bd9Sstevel@tonic-gate 	 * correct.
6437c478bd9Sstevel@tonic-gate 	 *
6447c478bd9Sstevel@tonic-gate 	 * The "hack" below is to go thru the whole command list looking for
6457c478bd9Sstevel@tonic-gate 	 * the netmask command first.  Then use this netmask to set the
6467c478bd9Sstevel@tonic-gate 	 * address.  This does not provide an extensible way to accommodate
6477c478bd9Sstevel@tonic-gate 	 * future need for setting more than one attributes together.
6487c478bd9Sstevel@tonic-gate 	 *
649dd7a6f5fSkcpoon 	 * Note that if the "netmask" command argument is a "+", we need
650dd7a6f5fSkcpoon 	 * to save this info and do the query after we know the address to
651dd7a6f5fSkcpoon 	 * be set.  The reason is that if "addif" is used, the working
652dd7a6f5fSkcpoon 	 * interface name will be changed later when the logical interface
653dd7a6f5fSkcpoon 	 * is created.  In in_getmask(), if an address is not provided,
654dd7a6f5fSkcpoon 	 * it will use the working interface's address to do the query.
655dd7a6f5fSkcpoon 	 * It will be wrong now as we don't know the logical interface's name.
656dd7a6f5fSkcpoon 	 *
6577c478bd9Sstevel@tonic-gate 	 * ifconfig(1M) is too overloaded and the code is so convoluted
6587c478bd9Sstevel@tonic-gate 	 * that it is "safer" not to re-architect the code to fix the above
6597c478bd9Sstevel@tonic-gate 	 * issue, hence this "hack."  We may be better off to have a new
6607c478bd9Sstevel@tonic-gate 	 * command with better syntax for configuring network interface
6617c478bd9Sstevel@tonic-gate 	 * parameters...
6627c478bd9Sstevel@tonic-gate 	 */
6637c478bd9Sstevel@tonic-gate 	if (!scan_netmask && afp->af_af == AF_INET) {
6647c478bd9Sstevel@tonic-gate 		int	largc;
6657c478bd9Sstevel@tonic-gate 		char	**largv;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		/* Only go thru the command list once to find the netmask. */
6687c478bd9Sstevel@tonic-gate 		scan_netmask = _B_TRUE;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		/*
6717c478bd9Sstevel@tonic-gate 		 * Currently, if multiple netmask commands are specified, the
6727c478bd9Sstevel@tonic-gate 		 * last one will be used as the final netmask.  So we need
6737c478bd9Sstevel@tonic-gate 		 * to scan the whole list to preserve this behavior.
6747c478bd9Sstevel@tonic-gate 		 */
6757c478bd9Sstevel@tonic-gate 		for (largc = argc, largv = argv; largc > 0; largc--, largv++) {
6767c478bd9Sstevel@tonic-gate 			if (strcmp(*largv, NETMASK_CMD) == 0) {
6777c478bd9Sstevel@tonic-gate 				if (--largc == 0)
6787c478bd9Sstevel@tonic-gate 					break;
6797c478bd9Sstevel@tonic-gate 				largv++;
6807c478bd9Sstevel@tonic-gate 				if (strcmp(*largv, "+") == 0) {
681dd7a6f5fSkcpoon 					g_netmask_set = G_NETMASK_PENDING;
6827c478bd9Sstevel@tonic-gate 				} else {
6837c478bd9Sstevel@tonic-gate 					in_getaddr(*largv, (struct sockaddr *)
6847c478bd9Sstevel@tonic-gate 					    &g_netmask, NULL);
685dd7a6f5fSkcpoon 					g_netmask_set = G_NETMASK_SET;
6867c478bd9Sstevel@tonic-gate 				}
6877c478bd9Sstevel@tonic-gate 				/* Continue the scan. */
6887c478bd9Sstevel@tonic-gate 			}
6897c478bd9Sstevel@tonic-gate 		}
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	while (argc > 0) {
6937c478bd9Sstevel@tonic-gate 		struct cmd *p;
6947c478bd9Sstevel@tonic-gate 		boolean_t found_cmd;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 		if (debug)
6977c478bd9Sstevel@tonic-gate 			(void) printf("ifconfig: argv %s\n", *argv);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 		found_cmd = _B_FALSE;
7007c478bd9Sstevel@tonic-gate 		for (p = cmds; p->c_func; p++) {
7017c478bd9Sstevel@tonic-gate 			if (p->c_name) {
7027c478bd9Sstevel@tonic-gate 				if (strcmp(*argv, p->c_name) == 0) {
7037c478bd9Sstevel@tonic-gate 					/*
7047c478bd9Sstevel@tonic-gate 					 * indicate that the command was
7057c478bd9Sstevel@tonic-gate 					 * found and check to see if
7067c478bd9Sstevel@tonic-gate 					 * the address family is valid
7077c478bd9Sstevel@tonic-gate 					 */
7087c478bd9Sstevel@tonic-gate 					found_cmd = _B_TRUE;
7097c478bd9Sstevel@tonic-gate 					if (p->c_af == AF_ANY ||
7107c478bd9Sstevel@tonic-gate 					    af == p->c_af)
7117c478bd9Sstevel@tonic-gate 						break;
7127c478bd9Sstevel@tonic-gate 				}
7137c478bd9Sstevel@tonic-gate 			} else {
7147c478bd9Sstevel@tonic-gate 				if (p->c_af == AF_ANY ||
7157c478bd9Sstevel@tonic-gate 				    af == p->c_af)
7167c478bd9Sstevel@tonic-gate 					break;
7177c478bd9Sstevel@tonic-gate 			}
7187c478bd9Sstevel@tonic-gate 		}
7197c478bd9Sstevel@tonic-gate 		/*
7207c478bd9Sstevel@tonic-gate 		 * If we found the keyword, but the address family
7217c478bd9Sstevel@tonic-gate 		 * did not match spit out an error
7227c478bd9Sstevel@tonic-gate 		 */
7237c478bd9Sstevel@tonic-gate 		if (found_cmd && p->c_name == 0) {
7247c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: Operation %s not"
7257c478bd9Sstevel@tonic-gate 			    " supported for %s\n", *argv, afp->af_name);
7267c478bd9Sstevel@tonic-gate 			exit(1);
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 		/*
7297c478bd9Sstevel@tonic-gate 		 * else (no keyword found), we assume it's an address
7307c478bd9Sstevel@tonic-gate 		 * of some sort
7317c478bd9Sstevel@tonic-gate 		 */
7326e91bba0SGirish Moodalbail 		if (setaddr && ipaddr != NULL) {
7336e91bba0SGirish Moodalbail 			/*
7346e91bba0SGirish Moodalbail 			 * We must have already filled in a source address in
7356e91bba0SGirish Moodalbail 			 * `ipaddr' and we now got a destination address.
7366e91bba0SGirish Moodalbail 			 * Fill it in `ipaddr' and call libipadm to create
7376e91bba0SGirish Moodalbail 			 * the static address.
7386e91bba0SGirish Moodalbail 			 */
7396e91bba0SGirish Moodalbail 			if (p->c_name == 0) {
7406e91bba0SGirish Moodalbail 				istatus = ipadm_set_dst_addr(ipaddr, *argv,
7416e91bba0SGirish Moodalbail 				    (p->c_af == AF_ANY ? AF_UNSPEC : af));
7426e91bba0SGirish Moodalbail 				if (istatus != IPADM_SUCCESS) {
7436e91bba0SGirish Moodalbail 					ipadmerr_exit(istatus, "could not "
7446e91bba0SGirish Moodalbail 					    "set destination address");
7456e91bba0SGirish Moodalbail 				}
7466e91bba0SGirish Moodalbail 				/*
7476e91bba0SGirish Moodalbail 				 * finished processing dstaddr, so reset setaddr
7486e91bba0SGirish Moodalbail 				 */
7496e91bba0SGirish Moodalbail 				setaddr = 0;
7506e91bba0SGirish Moodalbail 			}
7516e91bba0SGirish Moodalbail 			/*
7526e91bba0SGirish Moodalbail 			 * Both source and destination address are in `ipaddr'.
7536e91bba0SGirish Moodalbail 			 * Add the address by calling libipadm.
7546e91bba0SGirish Moodalbail 			 */
7556e91bba0SGirish Moodalbail 			istatus = ipadm_create_addr(iph, ipaddr,
7566e91bba0SGirish Moodalbail 			    IPADM_OPT_ACTIVE);
7576e91bba0SGirish Moodalbail 			if (istatus != IPADM_SUCCESS)
7586e91bba0SGirish Moodalbail 				goto createfailed;
7596e91bba0SGirish Moodalbail 			ipadm_destroy_addrobj(ipaddr);
7606e91bba0SGirish Moodalbail 			ipaddr = NULL;
7616e91bba0SGirish Moodalbail 			setaddr_done = _B_TRUE;
7626e91bba0SGirish Moodalbail 			if (p->c_name == 0) {
7636e91bba0SGirish Moodalbail 				/* move parser along */
7646e91bba0SGirish Moodalbail 				argc--, argv++;
7656e91bba0SGirish Moodalbail 				continue;
7666e91bba0SGirish Moodalbail 			}
7676e91bba0SGirish Moodalbail 		}
7686e91bba0SGirish Moodalbail 		if (p->c_name == 0 && setaddr_done) {
7696e91bba0SGirish Moodalbail 			/*
7706e91bba0SGirish Moodalbail 			 * catch odd commands like
7716e91bba0SGirish Moodalbail 			 * "ifconfig <intf> addr1 addr2 addr3 addr4 up"
7726e91bba0SGirish Moodalbail 			 */
7736e91bba0SGirish Moodalbail 			(void) fprintf(stderr, "%s",
7746e91bba0SGirish Moodalbail 			    "ifconfig: cannot configure more than two "
7756e91bba0SGirish Moodalbail 			    "addresses in one command\n");
7766e91bba0SGirish Moodalbail 			exit(1);
7776e91bba0SGirish Moodalbail 		}
7787c478bd9Sstevel@tonic-gate 		if (p->c_func) {
7797c478bd9Sstevel@tonic-gate 			if (p->c_af == AF_INET6) {
7807c478bd9Sstevel@tonic-gate 				v4compat = 0;
7817c478bd9Sstevel@tonic-gate 			}
7827c478bd9Sstevel@tonic-gate 			if (p->c_parameter == NEXTARG ||
7837c478bd9Sstevel@tonic-gate 			    p->c_parameter == OPTARG) {
7847c478bd9Sstevel@tonic-gate 				argc--, argv++;
7857c478bd9Sstevel@tonic-gate 				if (argc == 0 && p->c_parameter == NEXTARG) {
7867c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7877c478bd9Sstevel@tonic-gate 					    "ifconfig: no argument for %s\n",
7887c478bd9Sstevel@tonic-gate 					    p->c_name);
7897c478bd9Sstevel@tonic-gate 					exit(1);
7907c478bd9Sstevel@tonic-gate 				}
7917c478bd9Sstevel@tonic-gate 			}
7927c478bd9Sstevel@tonic-gate 			/*
7937c478bd9Sstevel@tonic-gate 			 *	Call the function if:
7947c478bd9Sstevel@tonic-gate 			 *
7957c478bd9Sstevel@tonic-gate 			 *		there's no address family
7967c478bd9Sstevel@tonic-gate 			 *		restriction
7977c478bd9Sstevel@tonic-gate 			 *	OR
7987c478bd9Sstevel@tonic-gate 			 *		we don't know the address yet
7997c478bd9Sstevel@tonic-gate 			 *		(because we were called from
8007c478bd9Sstevel@tonic-gate 			 *		main)
8017c478bd9Sstevel@tonic-gate 			 *	OR
8027c478bd9Sstevel@tonic-gate 			 *		there is a restriction AND
8037c478bd9Sstevel@tonic-gate 			 *		the address families match
8047c478bd9Sstevel@tonic-gate 			 */
8057c478bd9Sstevel@tonic-gate 			if ((p->c_af == AF_ANY)	||
8066e91bba0SGirish Moodalbail 			    (ifa == NULL) ||
8076e91bba0SGirish Moodalbail 			    (ifa->ifa_addr->ss_family == p->c_af)) {
8087c478bd9Sstevel@tonic-gate 				ret = (*p->c_func)(*argv, p->c_parameter);
8097c478bd9Sstevel@tonic-gate 				/*
8107c478bd9Sstevel@tonic-gate 				 *	If c_func failed and we should
8117c478bd9Sstevel@tonic-gate 				 *	abort processing for this
8127c478bd9Sstevel@tonic-gate 				 *	interface on failure, return
8137c478bd9Sstevel@tonic-gate 				 *	now rather than going on to
8147c478bd9Sstevel@tonic-gate 				 *	process other commands for
8157c478bd9Sstevel@tonic-gate 				 *	the same interface.
8167c478bd9Sstevel@tonic-gate 				 */
8177c478bd9Sstevel@tonic-gate 				if (ret != 0 && p->c_abortonfail)
8187c478bd9Sstevel@tonic-gate 					return;
8197c478bd9Sstevel@tonic-gate 			}
8207c478bd9Sstevel@tonic-gate 		}
8217c478bd9Sstevel@tonic-gate 		argc--, argv++;
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 
8246e91bba0SGirish Moodalbail 	if (setaddr && ipaddr != NULL) {
8256e91bba0SGirish Moodalbail 		/*
8266e91bba0SGirish Moodalbail 		 * Only the source address was provided, which was already
8276e91bba0SGirish Moodalbail 		 * set in `ipaddr'. Add the address by calling libipadm.
8286e91bba0SGirish Moodalbail 		 */
8296e91bba0SGirish Moodalbail 		istatus = ipadm_create_addr(iph, ipaddr, IPADM_OPT_ACTIVE);
8306e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
8316e91bba0SGirish Moodalbail 			goto createfailed;
8326e91bba0SGirish Moodalbail 		ipadm_destroy_addrobj(ipaddr);
8336e91bba0SGirish Moodalbail 		ipaddr = NULL;
8346e91bba0SGirish Moodalbail 		setaddr_done = _B_TRUE;
8356e91bba0SGirish Moodalbail 	}
8366e91bba0SGirish Moodalbail 
8377c478bd9Sstevel@tonic-gate 	/* Check to see if there's a security hole in the tunnel setup. */
8382b24ab6bSSebastien Roy 	if (ipsec_policy_set && !ipsec_auth_covered) {
8392b24ab6bSSebastien Roy 		(void) fprintf(stderr, "ifconfig: WARNING: tunnel with only "
8402b24ab6bSSebastien Roy 		    "ESP and no authentication.\n");
8412b24ab6bSSebastien Roy 	}
8426e91bba0SGirish Moodalbail 	return;
8436e91bba0SGirish Moodalbail 
8446e91bba0SGirish Moodalbail createfailed:
8456e91bba0SGirish Moodalbail 	(void) fprintf(stderr, "ifconfig: could not create address:% s\n",
8466e91bba0SGirish Moodalbail 	    ipadm_status2str(istatus));
8476e91bba0SGirish Moodalbail 	/* Remove the newly created logical interface. */
8486e91bba0SGirish Moodalbail 	if (strcmp(name, origname) != 0) {
8496e91bba0SGirish Moodalbail 		assert(strchr(name, ':') != NULL);
8506e91bba0SGirish Moodalbail 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
8516e91bba0SGirish Moodalbail 		(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
8526e91bba0SGirish Moodalbail 	}
8536e91bba0SGirish Moodalbail 	exit(1);
8547c478bd9Sstevel@tonic-gate }
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate /* ARGSUSED */
8577c478bd9Sstevel@tonic-gate static int
8587c478bd9Sstevel@tonic-gate setdebugflag(char *val, int64_t arg)
8597c478bd9Sstevel@tonic-gate {
8607c478bd9Sstevel@tonic-gate 	debug++;
8617c478bd9Sstevel@tonic-gate 	return (0);
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate /* ARGSUSED */
8657c478bd9Sstevel@tonic-gate static int
8667c478bd9Sstevel@tonic-gate setverboseflag(char *val, int64_t arg)
8677c478bd9Sstevel@tonic-gate {
8687c478bd9Sstevel@tonic-gate 	verbose++;
8697c478bd9Sstevel@tonic-gate 	return (0);
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate 
872dd7a6f5fSkcpoon /*
873dd7a6f5fSkcpoon  * This function fills in the given lifreq's lifr_addr field based on
874dd7a6f5fSkcpoon  * g_netmask_set.
875dd7a6f5fSkcpoon  */
876dd7a6f5fSkcpoon static void
877dd7a6f5fSkcpoon set_mask_lifreq(struct lifreq *lifr, struct sockaddr_storage *addr,
878dd7a6f5fSkcpoon     struct sockaddr_storage *mask)
879dd7a6f5fSkcpoon {
880dd7a6f5fSkcpoon 	assert(addr != NULL);
881dd7a6f5fSkcpoon 	assert(mask != NULL);
882dd7a6f5fSkcpoon 
883dd7a6f5fSkcpoon 	switch (g_netmask_set) {
884dd7a6f5fSkcpoon 	case G_NETMASK_SET:
885dd7a6f5fSkcpoon 		lifr->lifr_addr = g_netmask;
886dd7a6f5fSkcpoon 		break;
887dd7a6f5fSkcpoon 
888dd7a6f5fSkcpoon 	case G_NETMASK_PENDING:
889dd7a6f5fSkcpoon 		/*
890dd7a6f5fSkcpoon 		 * "+" is used as the argument to "netmask" command.  Query
891dd7a6f5fSkcpoon 		 * the database on the correct netmask based on the address to
892dd7a6f5fSkcpoon 		 * be set.
893dd7a6f5fSkcpoon 		 */
894dd7a6f5fSkcpoon 		assert(afp->af_af == AF_INET);
895dd7a6f5fSkcpoon 		g_netmask = *addr;
896dd7a6f5fSkcpoon 		if (!in_getmask((struct sockaddr_in *)&g_netmask, _B_TRUE)) {
897dd7a6f5fSkcpoon 			lifr->lifr_addr = *mask;
898dd7a6f5fSkcpoon 			g_netmask_set = G_NETMASK_NIL;
899dd7a6f5fSkcpoon 		} else {
900dd7a6f5fSkcpoon 			lifr->lifr_addr = g_netmask;
901dd7a6f5fSkcpoon 			g_netmask_set = G_NETMASK_SET;
902dd7a6f5fSkcpoon 		}
903dd7a6f5fSkcpoon 		break;
904dd7a6f5fSkcpoon 
905dd7a6f5fSkcpoon 	case G_NETMASK_NIL:
906dd7a6f5fSkcpoon 	default:
907dd7a6f5fSkcpoon 		lifr->lifr_addr = *mask;
908dd7a6f5fSkcpoon 		break;
909dd7a6f5fSkcpoon 	}
910dd7a6f5fSkcpoon }
911dd7a6f5fSkcpoon 
9127c478bd9Sstevel@tonic-gate /*
9137c478bd9Sstevel@tonic-gate  * Set the interface address. Handles <addr>, <addr>/<n> as well as /<n>
9147c478bd9Sstevel@tonic-gate  * syntax for setting the address, the address plus netmask, and just
9157c478bd9Sstevel@tonic-gate  * the netmask respectively.
9167c478bd9Sstevel@tonic-gate  */
9177c478bd9Sstevel@tonic-gate /* ARGSUSED */
9187c478bd9Sstevel@tonic-gate static int
9197c478bd9Sstevel@tonic-gate setifaddr(char *addr, int64_t param)
9207c478bd9Sstevel@tonic-gate {
9216e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
9227c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
9236e91bba0SGirish Moodalbail 	struct  lifreq lifr1;
9247c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage laddr;
9257c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage netmask;
9267c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 *sin6;
9277c478bd9Sstevel@tonic-gate 	struct	sockaddr_in *sin;
9287c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage sav_netmask;
9296e91bba0SGirish Moodalbail 	char cidraddr[BUFSIZ];
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 	}
9736e91bba0SGirish Moodalbail 
9746e91bba0SGirish Moodalbail 	/*
9756e91bba0SGirish Moodalbail 	 * Check and see if any "netmask" command is used and perform the
9766e91bba0SGirish Moodalbail 	 * necessary operation.
9776e91bba0SGirish Moodalbail 	 */
9786e91bba0SGirish Moodalbail 	set_mask_lifreq(&lifr, &laddr, &netmask);
9796e91bba0SGirish Moodalbail 
9806e91bba0SGirish Moodalbail 	/* This check is temporary until libipadm supports IPMP interfaces. */
9816e91bba0SGirish Moodalbail 	if (ifconfig_use_libipadm(s, name)) {
9826e91bba0SGirish Moodalbail 		istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name,
9836e91bba0SGirish Moodalbail 		    &ipaddr);
9846e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
9856e91bba0SGirish Moodalbail 			ipadmerr_exit(istatus, "setifaddr");
9866e91bba0SGirish Moodalbail 
9876e91bba0SGirish Moodalbail 		if (strchr(addr, '/') == NULL) {
9886e91bba0SGirish Moodalbail 			/*
9896e91bba0SGirish Moodalbail 			 * lifr.lifr_addr, which is updated by set_mask_lifreq()
9906e91bba0SGirish Moodalbail 			 * will contain the right mask to use.
9916e91bba0SGirish Moodalbail 			 */
9926e91bba0SGirish Moodalbail 			prefixlen = mask2plen(&lifr.lifr_addr);
9936e91bba0SGirish Moodalbail 			(void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d",
9946e91bba0SGirish Moodalbail 			    addr, prefixlen);
9956e91bba0SGirish Moodalbail 			addr = cidraddr;
9966e91bba0SGirish Moodalbail 		}
9976e91bba0SGirish Moodalbail 		istatus = ipadm_set_addr(ipaddr, addr, af);
9986e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
9996e91bba0SGirish Moodalbail 			ipadmerr_exit(istatus, "could not set address");
10006e91bba0SGirish Moodalbail 		/*
10016e91bba0SGirish Moodalbail 		 * let parser know we got a source.
10026e91bba0SGirish Moodalbail 		 * Next address, if given, should be dest
10036e91bba0SGirish Moodalbail 		 */
10046e91bba0SGirish Moodalbail 		setaddr++;
10056e91bba0SGirish Moodalbail 
10066e91bba0SGirish Moodalbail 		/*
10076e91bba0SGirish Moodalbail 		 * address will be set by the parser after nextarg has
10086e91bba0SGirish Moodalbail 		 * been scanned
10096e91bba0SGirish Moodalbail 		 */
10106e91bba0SGirish Moodalbail 		return (0);
10116e91bba0SGirish Moodalbail 	}
10126e91bba0SGirish Moodalbail 
10137c478bd9Sstevel@tonic-gate 	/* Tell parser that an address was set */
10147c478bd9Sstevel@tonic-gate 	setaddr++;
10157c478bd9Sstevel@tonic-gate 	/* save copy of netmask to restore in case of error */
10166e91bba0SGirish Moodalbail 	(void) strncpy(lifr1.lifr_name, name, sizeof (lifr1.lifr_name));
10176e91bba0SGirish Moodalbail 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr1) < 0)
10187c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCGLIFNETMASK");
10196e91bba0SGirish Moodalbail 	sav_netmask = lifr1.lifr_addr;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	/*
10227c478bd9Sstevel@tonic-gate 	 * If setting the address and not the mask, clear any existing mask
10237c478bd9Sstevel@tonic-gate 	 * and the kernel will then assign the default (netmask has been set
10247c478bd9Sstevel@tonic-gate 	 * to 0 in this case).  If setting both (either by using a prefix or
10257c478bd9Sstevel@tonic-gate 	 * using the netmask command), set the mask first, so the address will
10267c478bd9Sstevel@tonic-gate 	 * be interpreted correctly.
10277c478bd9Sstevel@tonic-gate 	 */
10286e91bba0SGirish Moodalbail 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
10296e91bba0SGirish Moodalbail 	/* lifr.lifr_addr already contains netmask from set_mask_lifreq() */
10307c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
10317c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	if (debug) {
10347c478bd9Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
10357c478bd9Sstevel@tonic-gate 		void *addr = (afp->af_af == AF_INET) ?
10367c478bd9Sstevel@tonic-gate 		    (void *)&((struct sockaddr_in *)&laddr)->sin_addr :
10377c478bd9Sstevel@tonic-gate 		    (void *)&((struct sockaddr_in6 *)&laddr)->sin6_addr;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 		(void) printf("Setting %s af %d addr %s\n",
10407c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, afp->af_af,
10417c478bd9Sstevel@tonic-gate 		    inet_ntop(afp->af_af, addr, abuf, sizeof (abuf)));
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = laddr;
10447c478bd9Sstevel@tonic-gate 	lifr.lifr_addr.ss_family = afp->af_af;
10457c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
10467c478bd9Sstevel@tonic-gate 		/*
10477c478bd9Sstevel@tonic-gate 		 * Restore the netmask
10487c478bd9Sstevel@tonic-gate 		 */
10497c478bd9Sstevel@tonic-gate 		int saverr = errno;
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
10527c478bd9Sstevel@tonic-gate 		lifr.lifr_addr = sav_netmask;
10537c478bd9Sstevel@tonic-gate 		(void) ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr);
10547c478bd9Sstevel@tonic-gate 		errno = saverr;
10557c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFADDR");
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	return (0);
10597c478bd9Sstevel@tonic-gate }
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate /*
10627c478bd9Sstevel@tonic-gate  * The following functions are stolen from the ipseckey(1m) program.
10637c478bd9Sstevel@tonic-gate  * Perhaps they should be somewhere common, but for now, we just maintain
10647c478bd9Sstevel@tonic-gate  * two versions.  We do this because of the different semantics for which
10657c478bd9Sstevel@tonic-gate  * algorithms we select ("requested" for ifconfig vs. "actual" for key).
10667c478bd9Sstevel@tonic-gate  */
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate static ulong_t
10697c478bd9Sstevel@tonic-gate parsenum(char *num)
10707c478bd9Sstevel@tonic-gate {
10717c478bd9Sstevel@tonic-gate 	ulong_t rc;
10727c478bd9Sstevel@tonic-gate 	char *end = NULL;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	errno = 0;
10757c478bd9Sstevel@tonic-gate 	rc = strtoul(num, &end, 0);
10767c478bd9Sstevel@tonic-gate 	if (errno != 0 || end == num || *end != '\0') {
10777c478bd9Sstevel@tonic-gate 		rc = (ulong_t)-1;
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	return (rc);
10817c478bd9Sstevel@tonic-gate }
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate /*
10847c478bd9Sstevel@tonic-gate  * Parse and reverse parse possible algorithm values, include numbers.
10857c478bd9Sstevel@tonic-gate  * Mostly stolen from ipseckey.c. See the comments above parsenum() for why
10867c478bd9Sstevel@tonic-gate  * this isn't common to ipseckey.c.
10877c478bd9Sstevel@tonic-gate  *
10887c478bd9Sstevel@tonic-gate  * NOTE: Static buffer in this function for the return value.  Since ifconfig
10892b24ab6bSSebastien Roy  *       isn't multithreaded, this isn't a huge problem.
10907c478bd9Sstevel@tonic-gate  */
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate #define	NBUF_SIZE 20	/* Enough to print a large integer. */
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate static char *
10957c478bd9Sstevel@tonic-gate rparsealg(uint8_t alg_value, int proto_num)
10967c478bd9Sstevel@tonic-gate {
10977c478bd9Sstevel@tonic-gate 	struct ipsecalgent *alg;
10987c478bd9Sstevel@tonic-gate 	static char numprint[128];	/* Enough to hold an algorithm name. */
10997c478bd9Sstevel@tonic-gate 
1100d2f8a3dfSpwernau 	/*
1101d2f8a3dfSpwernau 	 * Special cases for "any" and "none"
1102d2f8a3dfSpwernau 	 * The kernel needs to be able to distinguish between "any"
1103d2f8a3dfSpwernau 	 * and "none" and the APIs are underdefined in this area for auth.
1104d2f8a3dfSpwernau 	 */
1105d2f8a3dfSpwernau 	if (proto_num == IPSEC_PROTO_AH) {
1106d2f8a3dfSpwernau 		if (alg_value == SADB_AALG_NONE)
1107d2f8a3dfSpwernau 			return ("none");
1108d2f8a3dfSpwernau 		if (alg_value == SADB_AALG_ANY)
1109d2f8a3dfSpwernau 			return ("any");
1110d2f8a3dfSpwernau 	}
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	alg = getipsecalgbynum(alg_value, proto_num, NULL);
11137c478bd9Sstevel@tonic-gate 	if (alg != NULL) {
11147c478bd9Sstevel@tonic-gate 		(void) strlcpy(numprint, alg->a_names[0], sizeof (numprint));
11157c478bd9Sstevel@tonic-gate 		freeipsecalgent(alg);
11167c478bd9Sstevel@tonic-gate 	} else {
11177c478bd9Sstevel@tonic-gate 		(void) snprintf(numprint, sizeof (numprint), "%d", alg_value);
11187c478bd9Sstevel@tonic-gate 	}
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	return (numprint);
11217c478bd9Sstevel@tonic-gate }
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate static uint_t
11247c478bd9Sstevel@tonic-gate parsealg(char *algname, int proto_num)
11257c478bd9Sstevel@tonic-gate {
11267c478bd9Sstevel@tonic-gate 	struct ipsecalgent *alg;
11277c478bd9Sstevel@tonic-gate 	ulong_t invalue;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	if (algname == NULL) {
11307c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: Unexpected end of command "
11317c478bd9Sstevel@tonic-gate 		    "line.\n");
11327c478bd9Sstevel@tonic-gate 		exit(1);
11337c478bd9Sstevel@tonic-gate 	}
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	/*
1136d2f8a3dfSpwernau 	 * Special-case "none" and "any".
1137d2f8a3dfSpwernau 	 * Use strcasecmp because its length is bounded.
11387c478bd9Sstevel@tonic-gate 	 */
11397c478bd9Sstevel@tonic-gate 	if (strcasecmp("none", algname) == 0) {
11407c478bd9Sstevel@tonic-gate 		return ((proto_num == IPSEC_PROTO_ESP) ?
11417c478bd9Sstevel@tonic-gate 		    NO_ESP_EALG : NO_ESP_AALG);
11427c478bd9Sstevel@tonic-gate 	}
1143d2f8a3dfSpwernau 	if ((strcasecmp("any", algname) == 0) && (proto_num == IPSEC_PROTO_AH))
1144d2f8a3dfSpwernau 		return (SADB_AALG_ANY);
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	alg = getipsecalgbyname(algname, proto_num, NULL);
11477c478bd9Sstevel@tonic-gate 	if (alg != NULL) {
11487c478bd9Sstevel@tonic-gate 		invalue = alg->a_alg_num;
11497c478bd9Sstevel@tonic-gate 		freeipsecalgent(alg);
11507c478bd9Sstevel@tonic-gate 		return ((uint_t)invalue);
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	/*
11547c478bd9Sstevel@tonic-gate 	 * Since algorithms can be loaded during kernel run-time, check for
11557c478bd9Sstevel@tonic-gate 	 * numeric algorithm values too.
11567c478bd9Sstevel@tonic-gate 	 */
11577c478bd9Sstevel@tonic-gate 	invalue = parsenum(algname);
11587c478bd9Sstevel@tonic-gate 	if ((invalue & (ulong_t)0xff) == invalue)
11597c478bd9Sstevel@tonic-gate 		return ((uint_t)invalue);
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "ifconfig: %s algorithm type %s unknown.\n",
11627c478bd9Sstevel@tonic-gate 	    (proto_num == IPSEC_PROTO_ESP) ?
11637c478bd9Sstevel@tonic-gate 	    "Encryption" : "Authentication", algname);
11647c478bd9Sstevel@tonic-gate 	exit(1);
11657c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
11667c478bd9Sstevel@tonic-gate }
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate /*
11697c478bd9Sstevel@tonic-gate  * Actual ifconfig functions to set tunnel security properties.
11707c478bd9Sstevel@tonic-gate  */
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate enum ipsec_alg_type { ESP_ENCR_ALG = 1, ESP_AUTH_ALG, AH_AUTH_ALG };
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate static int
11757c478bd9Sstevel@tonic-gate set_tun_algs(int which_alg, int alg)
11767c478bd9Sstevel@tonic-gate {
11772b24ab6bSSebastien Roy 	boolean_t	encr_alg_set = _B_FALSE;
11782b24ab6bSSebastien Roy 	iptun_params_t	params;
11792b24ab6bSSebastien Roy 	dladm_status_t	status;
11802b24ab6bSSebastien Roy 	ipsec_req_t	*ipsr;
11817c478bd9Sstevel@tonic-gate 
11822b24ab6bSSebastien Roy 	if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN,
11832b24ab6bSSebastien Roy 	    &params.iptun_param_linkid)) != DLADM_STATUS_OK)
11842b24ab6bSSebastien Roy 		goto done;
11857c478bd9Sstevel@tonic-gate 
11862b24ab6bSSebastien Roy 	status = dladm_iptun_getparams(dlh, &params, DLADM_OPT_ACTIVE);
11872b24ab6bSSebastien Roy 	if (status != DLADM_STATUS_OK)
11882b24ab6bSSebastien Roy 		goto done;
11897c478bd9Sstevel@tonic-gate 
11902b24ab6bSSebastien Roy 	ipsr = &params.iptun_param_secinfo;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	/*
11937c478bd9Sstevel@tonic-gate 	 * If I'm just starting off this ifconfig, I want a clean slate,
11947c478bd9Sstevel@tonic-gate 	 * otherwise, I've captured the current tunnel security settings.
11957c478bd9Sstevel@tonic-gate 	 * In the case of continuation, I merely add to the settings.
11967c478bd9Sstevel@tonic-gate 	 */
11972b24ab6bSSebastien Roy 	if (!(params.iptun_param_flags & IPTUN_PARAM_SECINFO))
11987c478bd9Sstevel@tonic-gate 		(void) memset(ipsr, 0, sizeof (*ipsr));
11997c478bd9Sstevel@tonic-gate 
12002b24ab6bSSebastien Roy 	/* We're only modifying the IPsec information */
12012b24ab6bSSebastien Roy 	params.iptun_param_flags = IPTUN_PARAM_SECINFO;
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	switch (which_alg) {
12047c478bd9Sstevel@tonic-gate 	case ESP_ENCR_ALG:
12057c478bd9Sstevel@tonic-gate 		if (alg == NO_ESP_EALG) {
12067c478bd9Sstevel@tonic-gate 			if (ipsr->ipsr_esp_auth_alg == SADB_AALG_NONE)
12077c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_req = 0;
12087c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_alg = SADB_EALG_NONE;
1209d2f8a3dfSpwernau 
1210d2f8a3dfSpwernau 			/* Let the user specify NULL encryption implicitly. */
1211d2f8a3dfSpwernau 			if (ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) {
1212d2f8a3dfSpwernau 				encr_alg_set = _B_TRUE;
1213d2f8a3dfSpwernau 				ipsr->ipsr_esp_alg = SADB_EALG_NULL;
1214d2f8a3dfSpwernau 			}
12157c478bd9Sstevel@tonic-gate 		} else {
12167c478bd9Sstevel@tonic-gate 			encr_alg_set = _B_TRUE;
12177c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_req =
12187c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12197c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_alg = alg;
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 		break;
12227c478bd9Sstevel@tonic-gate 	case ESP_AUTH_ALG:
12237c478bd9Sstevel@tonic-gate 		if (alg == NO_ESP_AALG) {
1224d2f8a3dfSpwernau 			if ((ipsr->ipsr_esp_alg == SADB_EALG_NONE ||
1225d2f8a3dfSpwernau 			    ipsr->ipsr_esp_alg == SADB_EALG_NULL) &&
1226d2f8a3dfSpwernau 			    !encr_alg_set)
12277c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_req = 0;
12287c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_auth_alg = SADB_AALG_NONE;
12297c478bd9Sstevel@tonic-gate 		} else {
12307c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_req =
12317c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12327c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_auth_alg = alg;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 			/* Let the user specify NULL encryption implicitly. */
12357c478bd9Sstevel@tonic-gate 			if (ipsr->ipsr_esp_alg == SADB_EALG_NONE &&
12367c478bd9Sstevel@tonic-gate 			    !encr_alg_set)
12377c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_alg = SADB_EALG_NULL;
12387c478bd9Sstevel@tonic-gate 		}
12397c478bd9Sstevel@tonic-gate 		break;
12407c478bd9Sstevel@tonic-gate 	case AH_AUTH_ALG:
12417c478bd9Sstevel@tonic-gate 		if (alg == NO_AH_AALG) {
12427c478bd9Sstevel@tonic-gate 			ipsr->ipsr_ah_req = 0;
12437c478bd9Sstevel@tonic-gate 			ipsr->ipsr_auth_alg = SADB_AALG_NONE;
12447c478bd9Sstevel@tonic-gate 		} else {
12457c478bd9Sstevel@tonic-gate 			ipsr->ipsr_ah_req =
12467c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12477c478bd9Sstevel@tonic-gate 			ipsr->ipsr_auth_alg = alg;
12487c478bd9Sstevel@tonic-gate 		}
12497c478bd9Sstevel@tonic-gate 		break;
12507c478bd9Sstevel@tonic-gate 		/* Will never hit DEFAULT */
12517c478bd9Sstevel@tonic-gate 	}
12527c478bd9Sstevel@tonic-gate 
12532b24ab6bSSebastien Roy 	status = dladm_iptun_modify(dlh, &params, DLADM_OPT_ACTIVE);
12547c478bd9Sstevel@tonic-gate 
12552b24ab6bSSebastien Roy done:
12562b24ab6bSSebastien Roy 	if (status != DLADM_STATUS_OK)
12572b24ab6bSSebastien Roy 		dladmerr_exit(status, name);
12582b24ab6bSSebastien Roy 	else {
12592b24ab6bSSebastien Roy 		ipsec_policy_set = _B_TRUE;
12602b24ab6bSSebastien Roy 		if ((ipsr->ipsr_esp_req != 0 &&
12612b24ab6bSSebastien Roy 		    ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) ||
12622b24ab6bSSebastien Roy 		    (ipsr->ipsr_ah_req != 0 &&
12632b24ab6bSSebastien Roy 		    ipsr->ipsr_auth_alg != SADB_AALG_NONE))
12642b24ab6bSSebastien Roy 			ipsec_auth_covered = _B_TRUE;
12652b24ab6bSSebastien Roy 	}
12667c478bd9Sstevel@tonic-gate 	return (0);
12677c478bd9Sstevel@tonic-gate }
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate /* ARGSUSED */
12707c478bd9Sstevel@tonic-gate static int
12717c478bd9Sstevel@tonic-gate set_tun_esp_encr_alg(char *addr, int64_t param)
12727c478bd9Sstevel@tonic-gate {
12737c478bd9Sstevel@tonic-gate 	return (set_tun_algs(ESP_ENCR_ALG,
1274fc80c0dfSnordmark 	    parsealg(addr, IPSEC_PROTO_ESP)));
12757c478bd9Sstevel@tonic-gate }
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate /* ARGSUSED */
12787c478bd9Sstevel@tonic-gate static int
12797c478bd9Sstevel@tonic-gate set_tun_esp_auth_alg(char *addr, int64_t param)
12807c478bd9Sstevel@tonic-gate {
12817c478bd9Sstevel@tonic-gate 	return (set_tun_algs(ESP_AUTH_ALG,
1282fc80c0dfSnordmark 	    parsealg(addr, IPSEC_PROTO_AH)));
12837c478bd9Sstevel@tonic-gate }
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate /* ARGSUSED */
12867c478bd9Sstevel@tonic-gate static int
12877c478bd9Sstevel@tonic-gate set_tun_ah_alg(char *addr, int64_t param)
12887c478bd9Sstevel@tonic-gate {
12897c478bd9Sstevel@tonic-gate 	return (set_tun_algs(AH_AUTH_ALG,
1290fc80c0dfSnordmark 	    parsealg(addr, IPSEC_PROTO_AH)));
12917c478bd9Sstevel@tonic-gate }
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate /* ARGSUSED */
12947c478bd9Sstevel@tonic-gate static int
12957c478bd9Sstevel@tonic-gate setifrevarp(char *arg, int64_t param)
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate 	struct sockaddr_in	laddr;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	if (afp->af_af == AF_INET6) {
13007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13017c478bd9Sstevel@tonic-gate 		    "ifconfig: revarp not possible on IPv6 interface %s\n",
13027c478bd9Sstevel@tonic-gate 		    name);
13037c478bd9Sstevel@tonic-gate 		exit(1);
13047c478bd9Sstevel@tonic-gate 	}
13057c478bd9Sstevel@tonic-gate 	if (doifrevarp(name, &laddr)) {
13067c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13077c478bd9Sstevel@tonic-gate 		laddr.sin_family = AF_INET;
13087c478bd9Sstevel@tonic-gate 		(void) memcpy(&lifr.lifr_addr, &laddr, sizeof (laddr));
13097c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
13107c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCSLIFADDR");
13117c478bd9Sstevel@tonic-gate 	}
13127c478bd9Sstevel@tonic-gate 	return (0);
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate /* ARGSUSED */
13167c478bd9Sstevel@tonic-gate static int
13177c478bd9Sstevel@tonic-gate setifsubnet(char *addr, int64_t param)
13187c478bd9Sstevel@tonic-gate {
13197c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
13207c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage subnet;
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, &subnet, &prefixlen);
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
13257c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
13267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13277c478bd9Sstevel@tonic-gate 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
13287c478bd9Sstevel@tonic-gate 		exit(1);
13297c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
13307c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
13317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13327c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
13337c478bd9Sstevel@tonic-gate 		exit(1);
13347c478bd9Sstevel@tonic-gate 	default:
13357c478bd9Sstevel@tonic-gate 		break;
13367c478bd9Sstevel@tonic-gate 	}
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = subnet;
13397c478bd9Sstevel@tonic-gate 	lifr.lifr_addrlen = prefixlen;
13407c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13417c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFSUBNET, (caddr_t)&lifr) < 0)
13427c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFSUBNET");
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	return (0);
13457c478bd9Sstevel@tonic-gate }
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate /* ARGSUSED */
13487c478bd9Sstevel@tonic-gate static int
13497c478bd9Sstevel@tonic-gate setifnetmask(char *addr, int64_t param)
13507c478bd9Sstevel@tonic-gate {
13517c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask;
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	assert(afp->af_af != AF_INET6);
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	if (strcmp(addr, "+") == 0) {
1356dd7a6f5fSkcpoon 		if (!in_getmask(&netmask, _B_FALSE))
13577c478bd9Sstevel@tonic-gate 			return (0);
1358dd7a6f5fSkcpoon 		(void) printf("Setting netmask of %s to %s\n", name,
1359dd7a6f5fSkcpoon 		    inet_ntoa(netmask.sin_addr));
13607c478bd9Sstevel@tonic-gate 	} else {
13617c478bd9Sstevel@tonic-gate 		in_getaddr(addr, (struct sockaddr *)&netmask, NULL);
13627c478bd9Sstevel@tonic-gate 	}
13637c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13647c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &netmask, sizeof (netmask));
13657c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
13667c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
13677c478bd9Sstevel@tonic-gate 	return (0);
13687c478bd9Sstevel@tonic-gate }
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate /*
13717c478bd9Sstevel@tonic-gate  * Parse '/<n>' as a netmask.
13727c478bd9Sstevel@tonic-gate  */
13737c478bd9Sstevel@tonic-gate /* ARGSUSED */
13747c478bd9Sstevel@tonic-gate static int
13757c478bd9Sstevel@tonic-gate setifprefixlen(char *addr, int64_t param)
13767c478bd9Sstevel@tonic-gate {
13777c478bd9Sstevel@tonic-gate 	int prefixlen;
13787c478bd9Sstevel@tonic-gate 	int af = afp->af_af;
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	prefixlen = in_getprefixlen(addr, _B_TRUE,
13817906a3e0Smeem 	    (af == AF_INET) ? IP_ABITS : IPV6_ABITS);
13827c478bd9Sstevel@tonic-gate 	if (prefixlen < 0) {
13837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13847c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
13857c478bd9Sstevel@tonic-gate 		exit(1);
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
13887c478bd9Sstevel@tonic-gate 	lifr.lifr_addr.ss_family = af;
13897c478bd9Sstevel@tonic-gate 	if (af == AF_INET6) {
13907c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
13937906a3e0Smeem 		if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
13947c478bd9Sstevel@tonic-gate 		    (uchar_t *)&sin6->sin6_addr)) {
13957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: "
13967c478bd9Sstevel@tonic-gate 			    "Bad prefix length: %d\n",
13977c478bd9Sstevel@tonic-gate 			    prefixlen);
13987c478bd9Sstevel@tonic-gate 			exit(1);
13997c478bd9Sstevel@tonic-gate 		}
14007c478bd9Sstevel@tonic-gate 	} else if (af == AF_INET) {
14017c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin;
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
14047906a3e0Smeem 		if (!in_prefixlentomask(prefixlen, IP_ABITS,
14057c478bd9Sstevel@tonic-gate 		    (uchar_t *)&sin->sin_addr)) {
14067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: "
14077c478bd9Sstevel@tonic-gate 			    "Bad prefix length: %d\n",
14087c478bd9Sstevel@tonic-gate 			    prefixlen);
14097c478bd9Sstevel@tonic-gate 			exit(1);
14107c478bd9Sstevel@tonic-gate 		}
14117c478bd9Sstevel@tonic-gate 	} else {
14127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: setting prefix only supported"
14137c478bd9Sstevel@tonic-gate 		    " for address family inet or inet6\n");
14147c478bd9Sstevel@tonic-gate 		exit(1);
14157c478bd9Sstevel@tonic-gate 	}
14167c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14177c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
14187c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
14197c478bd9Sstevel@tonic-gate 	return (0);
14207c478bd9Sstevel@tonic-gate }
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate /* ARGSUSED */
14237c478bd9Sstevel@tonic-gate static int
14247c478bd9Sstevel@tonic-gate setifbroadaddr(char *addr, int64_t param)
14257c478bd9Sstevel@tonic-gate {
14267c478bd9Sstevel@tonic-gate 	struct	sockaddr_in broadaddr;
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	assert(afp->af_af != AF_INET6);
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	if (strcmp(addr, "+") == 0) {
14317c478bd9Sstevel@tonic-gate 		/*
14327c478bd9Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
14337c478bd9Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
14347c478bd9Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
14357c478bd9Sstevel@tonic-gate 		 */
14367c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name,
14377c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
14387c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
14397c478bd9Sstevel@tonic-gate 			if (errno != EADDRNOTAVAIL)
14407c478bd9Sstevel@tonic-gate 				Perror0_exit("SIOCGLIFADDR");
14417c478bd9Sstevel@tonic-gate 			return (0);
14427c478bd9Sstevel@tonic-gate 		}
14437c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
14447c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCGLIFADDR");
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 		return (0);
14477c478bd9Sstevel@tonic-gate 	}
14487c478bd9Sstevel@tonic-gate 	in_getaddr(addr, (struct sockaddr *)&broadaddr, NULL);
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &broadaddr, sizeof (broadaddr));
14517c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14527c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFBRDADDR, (caddr_t)&lifr) < 0)
14537c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFBRDADDR");
14547c478bd9Sstevel@tonic-gate 	return (0);
14557c478bd9Sstevel@tonic-gate }
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate /*
14587c478bd9Sstevel@tonic-gate  * set interface destination address
14597c478bd9Sstevel@tonic-gate  */
14607c478bd9Sstevel@tonic-gate /* ARGSUSED */
14617c478bd9Sstevel@tonic-gate static int
14627c478bd9Sstevel@tonic-gate setifdstaddr(char *addr, int64_t param)
14637c478bd9Sstevel@tonic-gate {
14647c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, (struct sockaddr *)&lifr.lifr_addr, NULL);
14657c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14667c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFDSTADDR, (caddr_t)&lifr) < 0)
14677c478bd9Sstevel@tonic-gate 		Perror0_exit("setifdstaddr: SIOCSLIFDSTADDR");
14687c478bd9Sstevel@tonic-gate 	return (0);
14697c478bd9Sstevel@tonic-gate }
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate /* ARGSUSED */
14727c478bd9Sstevel@tonic-gate static int
14737c478bd9Sstevel@tonic-gate setifflags(char *val, int64_t value)
14747c478bd9Sstevel@tonic-gate {
1475e11c3f44Smeem 	struct lifreq lifrl;	/* local lifreq struct */
1476e11c3f44Smeem 	boolean_t bringup = _B_FALSE;
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14797c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0)
14807c478bd9Sstevel@tonic-gate 		Perror0_exit("setifflags: SIOCGLIFFLAGS");
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	if (value < 0) {
14837c478bd9Sstevel@tonic-gate 		value = -value;
1484e11c3f44Smeem 
1485e11c3f44Smeem 		if ((value & IFF_NOFAILOVER) && (lifr.lifr_flags & IFF_UP)) {
1486e11c3f44Smeem 			/*
1487e11c3f44Smeem 			 * The kernel does not allow administratively up test
1488e11c3f44Smeem 			 * addresses to be converted to data addresses.  Bring
1489e11c3f44Smeem 			 * the address down first, then bring it up after it's
1490e11c3f44Smeem 			 * been converted to a data address.
1491e11c3f44Smeem 			 */
1492e11c3f44Smeem 			lifr.lifr_flags &= ~IFF_UP;
1493e11c3f44Smeem 			(void) ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr);
1494e11c3f44Smeem 			bringup = _B_TRUE;
1495e11c3f44Smeem 		}
1496e11c3f44Smeem 
14977c478bd9Sstevel@tonic-gate 		lifr.lifr_flags &= ~value;
1498e11c3f44Smeem 		if ((value & (IFF_UP | IFF_NOFAILOVER)) &&
1499e11c3f44Smeem 		    (lifr.lifr_flags & IFF_DUPLICATE)) {
150069bb4bb4Scarlsonj 			/*
150169bb4bb4Scarlsonj 			 * If the user is trying to mark an interface with a
1502e11c3f44Smeem 			 * duplicate address as "down," or convert a duplicate
1503e11c3f44Smeem 			 * test address to a data address, then fetch the
1504e11c3f44Smeem 			 * address and set it.  This will cause IP to clear
1505e11c3f44Smeem 			 * the IFF_DUPLICATE flag and stop the automatic
1506e11c3f44Smeem 			 * recovery timer.
150769bb4bb4Scarlsonj 			 */
150869bb4bb4Scarlsonj 			value = lifr.lifr_flags;
150969bb4bb4Scarlsonj 			if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0)
151069bb4bb4Scarlsonj 				(void) ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr);
151169bb4bb4Scarlsonj 			lifr.lifr_flags = value;
151269bb4bb4Scarlsonj 		}
151369bb4bb4Scarlsonj 	} else {
15147c478bd9Sstevel@tonic-gate 		lifr.lifr_flags |= value;
151569bb4bb4Scarlsonj 	}
1516e11c3f44Smeem 
1517e11c3f44Smeem 	/*
1518e11c3f44Smeem 	 * If we're about to bring up an underlying physical IPv6 interface in
1519e11c3f44Smeem 	 * an IPMP group, ensure the IPv6 IPMP interface is also up.  This is
1520e11c3f44Smeem 	 * for backward compatibility with legacy configurations in which
1521e11c3f44Smeem 	 * there are no explicit hostname files for IPMP interfaces.  (For
1522e11c3f44Smeem 	 * IPv4, this is automatically handled by the kernel when migrating
1523e11c3f44Smeem 	 * the underlying interface's data address to the IPMP interface.)
1524e11c3f44Smeem 	 */
1525e11c3f44Smeem 	(void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ);
1526e11c3f44Smeem 
1527e11c3f44Smeem 	if (lifnum(lifr.lifr_name) == 0 &&
1528e11c3f44Smeem 	    (lifr.lifr_flags & (IFF_UP|IFF_IPV6)) == (IFF_UP|IFF_IPV6) &&
1529e11c3f44Smeem 	    ioctl(s, SIOCGLIFGROUPNAME, &lifrl) == 0 &&
1530e11c3f44Smeem 	    lifrl.lifr_groupname[0] != '\0') {
1531e11c3f44Smeem 		lifgroupinfo_t lifgr;
1532e11c3f44Smeem 
1533e11c3f44Smeem 		(void) strlcpy(lifgr.gi_grname, lifrl.lifr_groupname,
1534e11c3f44Smeem 		    LIFGRNAMSIZ);
1535e11c3f44Smeem 		if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1)
1536e11c3f44Smeem 			Perror0_exit("setifflags: SIOCGLIFGROUPINFO");
1537e11c3f44Smeem 
1538e11c3f44Smeem 		(void) strlcpy(lifrl.lifr_name, lifgr.gi_grifname, LIFNAMSIZ);
1539e11c3f44Smeem 		if (ioctl(s, SIOCGLIFFLAGS, &lifrl) == -1)
1540e11c3f44Smeem 			Perror0_exit("setifflags: SIOCGLIFFLAGS");
1541e11c3f44Smeem 		if (!(lifrl.lifr_flags & IFF_UP)) {
1542e11c3f44Smeem 			lifrl.lifr_flags |= IFF_UP;
1543e11c3f44Smeem 			if (ioctl(s, SIOCSLIFFLAGS, &lifrl) == -1)
1544e11c3f44Smeem 				Perror0_exit("setifflags: SIOCSLIFFLAGS");
1545e11c3f44Smeem 		}
1546e11c3f44Smeem 	}
1547e11c3f44Smeem 
15487c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1549e11c3f44Smeem 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0)
15507c478bd9Sstevel@tonic-gate 		Perror0_exit("setifflags: SIOCSLIFFLAGS");
1551e11c3f44Smeem 
1552e11c3f44Smeem 	if (bringup) {
1553e11c3f44Smeem 		lifr.lifr_flags |= IFF_UP;
1554e11c3f44Smeem 		if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0)
1555e11c3f44Smeem 			Perror0_exit("setifflags: SIOCSLIFFLAGS IFF_UP");
15567c478bd9Sstevel@tonic-gate 	}
1557e11c3f44Smeem 
15587c478bd9Sstevel@tonic-gate 	return (0);
15597c478bd9Sstevel@tonic-gate }
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate /* ARGSUSED */
15627c478bd9Sstevel@tonic-gate static int
15637c478bd9Sstevel@tonic-gate setifmetric(char *val, int64_t param)
15647c478bd9Sstevel@tonic-gate {
15657c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15667c478bd9Sstevel@tonic-gate 	lifr.lifr_metric = atoi(val);
15677c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFMETRIC, (caddr_t)&lifr) < 0)
15687c478bd9Sstevel@tonic-gate 		Perror0_exit("setifmetric: SIOCSLIFMETRIC");
15697c478bd9Sstevel@tonic-gate 	return (0);
15707c478bd9Sstevel@tonic-gate }
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate /* ARGSUSED */
15737c478bd9Sstevel@tonic-gate static int
15747c478bd9Sstevel@tonic-gate setifmtu(char *val, int64_t param)
15757c478bd9Sstevel@tonic-gate {
15767c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15777c478bd9Sstevel@tonic-gate 	lifr.lifr_mtu = atoi(val);
15787c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFMTU, (caddr_t)&lifr) < 0)
15797c478bd9Sstevel@tonic-gate 		Perror0_exit("setifmtu: SIOCSLIFMTU");
15807c478bd9Sstevel@tonic-gate 	return (0);
15817c478bd9Sstevel@tonic-gate }
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate /* ARGSUSED */
15847c478bd9Sstevel@tonic-gate static int
15857c478bd9Sstevel@tonic-gate setifindex(char *val, int64_t param)
15867c478bd9Sstevel@tonic-gate {
15877c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15887c478bd9Sstevel@tonic-gate 	lifr.lifr_index = atoi(val);
15897c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFINDEX, (caddr_t)&lifr) < 0)
15907c478bd9Sstevel@tonic-gate 		Perror0_exit("setifindex: SIOCSLIFINDEX");
15917c478bd9Sstevel@tonic-gate 	return (0);
15927c478bd9Sstevel@tonic-gate }
15937c478bd9Sstevel@tonic-gate 
1594e11c3f44Smeem /* ARGSUSED */
1595e11c3f44Smeem static void
1596e11c3f44Smeem notifycb(dlpi_handle_t dh, dlpi_notifyinfo_t *dnip, void *arg)
1597e11c3f44Smeem {
1598e11c3f44Smeem }
1599e11c3f44Smeem 
16007c478bd9Sstevel@tonic-gate /* ARGSUSED */
16017c478bd9Sstevel@tonic-gate static int
16027c478bd9Sstevel@tonic-gate setifether(char *addr, int64_t param)
16037c478bd9Sstevel@tonic-gate {
1604e11c3f44Smeem 	uchar_t		*hwaddr;
1605e11c3f44Smeem 	int		hwaddrlen;
1606e11c3f44Smeem 	int		retval;
1607e11c3f44Smeem 	ifaddrlistx_t	*ifaddrp, *ifaddrs = NULL;
1608e11c3f44Smeem 	dlpi_handle_t	dh;
1609e11c3f44Smeem 	dlpi_notifyid_t id;
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
16127c478bd9Sstevel@tonic-gate 		ifstatus(name);
16137c478bd9Sstevel@tonic-gate 		print_ifether(name);
16147c478bd9Sstevel@tonic-gate 		return (0);
16157c478bd9Sstevel@tonic-gate 	}
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	/*
16187c478bd9Sstevel@tonic-gate 	 * if the IP interface in the arguments is a logical
16197c478bd9Sstevel@tonic-gate 	 * interface, exit with an error now.
16207c478bd9Sstevel@tonic-gate 	 */
16217c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
16227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: cannot change"
16237c478bd9Sstevel@tonic-gate 		    " ethernet address of a logical interface\n");
16247c478bd9Sstevel@tonic-gate 		exit(1);
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate 
1627e11c3f44Smeem 	if ((hwaddr = _link_aton(addr, &hwaddrlen)) == NULL) {
1628e11c3f44Smeem 		if (hwaddrlen == -1)
16297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
16306e91bba0SGirish Moodalbail 			    "ifconfig: bad ethernet address\n");
16317c478bd9Sstevel@tonic-gate 		else
16327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: malloc() failed\n");
16337c478bd9Sstevel@tonic-gate 		exit(1);
16347c478bd9Sstevel@tonic-gate 	}
16357c478bd9Sstevel@tonic-gate 
1636e11c3f44Smeem 	if ((retval = dlpi_open(name, &dh, 0)) != DLPI_SUCCESS)
1637e11c3f44Smeem 		Perrdlpi_exit("cannot dlpi_open() link", name, retval);
16387c478bd9Sstevel@tonic-gate 
1639e11c3f44Smeem 	retval = dlpi_enabnotify(dh, DL_NOTE_PHYS_ADDR, notifycb, NULL, &id);
1640e11c3f44Smeem 	if (retval == DLPI_SUCCESS) {
1641e11c3f44Smeem 		(void) dlpi_disabnotify(dh, id, NULL);
1642e11c3f44Smeem 	} else {
1643e11c3f44Smeem 		/*
1644e11c3f44Smeem 		 * This link does not support DL_NOTE_PHYS_ADDR: bring down
1645e11c3f44Smeem 		 * all of the addresses to flush the old hardware address
1646e11c3f44Smeem 		 * information out of IP.
1647e11c3f44Smeem 		 *
1648e11c3f44Smeem 		 * NOTE: Skipping this when DL_NOTE_PHYS_ADDR is supported is
1649e11c3f44Smeem 		 * more than an optimization: in.mpathd will set IFF_OFFLINE
1650e11c3f44Smeem 		 * if it's notified and the new address is a duplicate of
1651e11c3f44Smeem 		 * another in the group -- but the flags manipulation in
1652e11c3f44Smeem 		 * ifaddr_{down,up}() cannot be atomic and thus might clobber
1653e11c3f44Smeem 		 * IFF_OFFLINE, confusing in.mpathd.
1654e11c3f44Smeem 		 */
1655e11c3f44Smeem 		if (ifaddrlistx(name, IFF_UP, 0, &ifaddrs) == -1)
1656e11c3f44Smeem 			Perror2_exit(name, "cannot get address list");
1657e11c3f44Smeem 
1658e11c3f44Smeem 		ifaddrp = ifaddrs;
1659e11c3f44Smeem 		for (; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
1660e11c3f44Smeem 			if (!ifaddr_down(ifaddrp)) {
1661e11c3f44Smeem 				Perror2_exit(ifaddrp->ia_name,
1662e11c3f44Smeem 				    "cannot bring down");
1663e11c3f44Smeem 			}
1664e11c3f44Smeem 		}
16657c478bd9Sstevel@tonic-gate 	}
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	/*
1668e11c3f44Smeem 	 * Change the hardware address.
16697c478bd9Sstevel@tonic-gate 	 */
1670e11c3f44Smeem 	retval = dlpi_set_physaddr(dh, DL_CURR_PHYS_ADDR, hwaddr, hwaddrlen);
1671e11c3f44Smeem 	if (retval != DLPI_SUCCESS) {
16727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1673e11c3f44Smeem 		    "ifconfig: failed setting mac address on %s\n", name);
16747c478bd9Sstevel@tonic-gate 	}
1675e11c3f44Smeem 	dlpi_close(dh);
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 	/*
1678e11c3f44Smeem 	 * If any addresses were brought down before changing the hardware
1679e11c3f44Smeem 	 * address, bring them up again.
16807c478bd9Sstevel@tonic-gate 	 */
1681e11c3f44Smeem 	for (ifaddrp = ifaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
1682e11c3f44Smeem 		if (!ifaddr_up(ifaddrp))
1683e11c3f44Smeem 			Perror2_exit(ifaddrp->ia_name, "cannot bring up");
16847c478bd9Sstevel@tonic-gate 	}
1685e11c3f44Smeem 	ifaddrlistx_free(ifaddrs);
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 	return (0);
16887c478bd9Sstevel@tonic-gate }
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate /*
16917c478bd9Sstevel@tonic-gate  * Print an interface's Ethernet address, if it has one.
16927c478bd9Sstevel@tonic-gate  */
16937c478bd9Sstevel@tonic-gate static void
16946e91bba0SGirish Moodalbail print_ifether(const char *ifname)
16957c478bd9Sstevel@tonic-gate {
16962b24ab6bSSebastien Roy 	int fd;
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	fd = socket(AF_INET, SOCK_DGRAM, 0);
17017c478bd9Sstevel@tonic-gate 	if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
17027c478bd9Sstevel@tonic-gate 		/*
17037c478bd9Sstevel@tonic-gate 		 * It's possible the interface is only configured for
17047c478bd9Sstevel@tonic-gate 		 * IPv6; check again with AF_INET6.
17057c478bd9Sstevel@tonic-gate 		 */
17067c478bd9Sstevel@tonic-gate 		(void) close(fd);
17077c478bd9Sstevel@tonic-gate 		fd = socket(AF_INET6, SOCK_DGRAM, 0);
17087c478bd9Sstevel@tonic-gate 		if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
17097c478bd9Sstevel@tonic-gate 			(void) close(fd);
17107c478bd9Sstevel@tonic-gate 			return;
17117c478bd9Sstevel@tonic-gate 		}
17127c478bd9Sstevel@tonic-gate 	}
17137c478bd9Sstevel@tonic-gate 	(void) close(fd);
17147c478bd9Sstevel@tonic-gate 
1715e11c3f44Smeem 	/* VNI and IPMP interfaces don't have MAC addresses */
1716e11c3f44Smeem 	if (lifr.lifr_flags & (IFF_VIRTUAL|IFF_IPMP))
17177c478bd9Sstevel@tonic-gate 		return;
17187c478bd9Sstevel@tonic-gate 
17192b24ab6bSSebastien Roy 	/* IP tunnels also don't have Ethernet-like MAC addresses */
17202b24ab6bSSebastien Roy 	if (ifconfig_dladm_open(ifname, DATALINK_CLASS_IPTUN, NULL) ==
17212b24ab6bSSebastien Roy 	    DLADM_STATUS_OK)
17222b24ab6bSSebastien Roy 		return;
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	dlpi_print_address(ifname);
17257c478bd9Sstevel@tonic-gate }
17267c478bd9Sstevel@tonic-gate 
1727f4b3ec61Sdh /*
17282b24ab6bSSebastien Roy  * static int find_all_interfaces(struct lifconf *lifcp, char **buf,
1729f4b3ec61Sdh  *     int64_t lifc_flags)
1730f4b3ec61Sdh  *
17312b24ab6bSSebastien Roy  * It finds all active data links.
1732f4b3ec61Sdh  *
1733f4b3ec61Sdh  * It takes in input a pointer to struct lifconf to receive interfaces
1734f4b3ec61Sdh  * informations, a **char to hold allocated buffer, and a lifc_flags.
1735f4b3ec61Sdh  *
1736f4b3ec61Sdh  * Return values:
1737f4b3ec61Sdh  *  0 = everything OK
1738f4b3ec61Sdh  * -1 = problem
1739f4b3ec61Sdh  */
1740f4b3ec61Sdh static int
17412b24ab6bSSebastien Roy find_all_interfaces(struct lifconf *lifcp, char **buf, int64_t lifc_flags)
1742f4b3ec61Sdh {
1743f4b3ec61Sdh 	unsigned bufsize;
1744f4b3ec61Sdh 	int n;
1745f4b3ec61Sdh 	ni_t *nip;
1746f4b3ec61Sdh 	struct lifreq *lifrp;
1747d4d1f7bfSVasumathi Sundaram - Sun Microsystems 	dladm_status_t status;
1748f4b3ec61Sdh 
17492b24ab6bSSebastien Roy 	if (!dlh_opened) {
17502b24ab6bSSebastien Roy 		status = ifconfig_dladm_open(NULL, 0, NULL);
17512b24ab6bSSebastien Roy 		if (status != DLADM_STATUS_OK)
17522b24ab6bSSebastien Roy 			dladmerr_exit(status, "unable to open dladm handle");
1753d4d1f7bfSVasumathi Sundaram - Sun Microsystems 	}
1754d4d1f7bfSVasumathi Sundaram - Sun Microsystems 
17552b24ab6bSSebastien Roy 	(void) dlpi_walk(ni_entry, dlh, 0);
1756f4b3ec61Sdh 
17572b24ab6bSSebastien Roy 	/* Now, translate the linked list into a struct lifreq buffer */
1758f4b3ec61Sdh 	if (num_ni == 0) {
1759f4b3ec61Sdh 		lifcp->lifc_family = AF_UNSPEC;
1760f4b3ec61Sdh 		lifcp->lifc_flags = lifc_flags;
1761f4b3ec61Sdh 		lifcp->lifc_len = 0;
1762f4b3ec61Sdh 		lifcp->lifc_buf = NULL;
1763f4b3ec61Sdh 		return (0);
1764f4b3ec61Sdh 	}
1765f4b3ec61Sdh 
1766f4b3ec61Sdh 	bufsize = num_ni * sizeof (struct lifreq);
1767f4b3ec61Sdh 	if ((*buf = malloc(bufsize)) == NULL)
1768f4b3ec61Sdh 		Perror0_exit("find_all_interfaces: malloc failed");
1769f4b3ec61Sdh 
1770f4b3ec61Sdh 	lifcp->lifc_family = AF_UNSPEC;
1771f4b3ec61Sdh 	lifcp->lifc_flags = lifc_flags;
1772f4b3ec61Sdh 	lifcp->lifc_len = bufsize;
1773f4b3ec61Sdh 	lifcp->lifc_buf = *buf;
1774f4b3ec61Sdh 
1775f4b3ec61Sdh 	for (n = 0, lifrp = lifcp->lifc_req; n < num_ni; n++, lifrp++) {
1776f4b3ec61Sdh 		nip = ni_list;
1777f4b3ec61Sdh 		(void) strncpy(lifrp->lifr_name, nip->ni_name,
1778f4b3ec61Sdh 		    sizeof (lifr.lifr_name));
1779f4b3ec61Sdh 		ni_list = nip->ni_next;
1780f4b3ec61Sdh 		free(nip);
1781f4b3ec61Sdh 	}
1782f4b3ec61Sdh 	return (0);
1783f4b3ec61Sdh }
1784f4b3ec61Sdh 
17857c478bd9Sstevel@tonic-gate /*
17867c478bd9Sstevel@tonic-gate  * Create the next unused logical interface using the original name
17877c478bd9Sstevel@tonic-gate  * and assign the address (and mask if '/<n>' is part of the address).
17887c478bd9Sstevel@tonic-gate  * Use the new logical interface for subsequent subcommands by updating
17897c478bd9Sstevel@tonic-gate  * the name variable.
17907c478bd9Sstevel@tonic-gate  *
17917c478bd9Sstevel@tonic-gate  * This allows syntax like:
17927c478bd9Sstevel@tonic-gate  *	ifconfig le0 addif 109.106.86.130 netmask + up \
17937c478bd9Sstevel@tonic-gate  *	addif 109.106.86.131 netmask + up
17947c478bd9Sstevel@tonic-gate  */
17957c478bd9Sstevel@tonic-gate /* ARGSUSED */
17967c478bd9Sstevel@tonic-gate static int
17977c478bd9Sstevel@tonic-gate addif(char *str, int64_t param)
17987c478bd9Sstevel@tonic-gate {
17997c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
18007c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
18017c478bd9Sstevel@tonic-gate 	struct sockaddr_storage mask;
18026e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
18036e91bba0SGirish Moodalbail 	char cidraddr[BUFSIZ];
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	(void) strncpy(name, origname, sizeof (name));
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
18087c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18097c478bd9Sstevel@tonic-gate 		    "ifconfig: addif: bad physical interface name %s\n",
18107c478bd9Sstevel@tonic-gate 		    name);
18117c478bd9Sstevel@tonic-gate 		exit(1);
18127c478bd9Sstevel@tonic-gate 	}
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	/*
18157c478bd9Sstevel@tonic-gate 	 * clear so parser will interpret next address as source followed
18167c478bd9Sstevel@tonic-gate 	 * by possible dest
18177c478bd9Sstevel@tonic-gate 	 */
18187c478bd9Sstevel@tonic-gate 	setaddr = 0;
18197c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(str, (struct sockaddr *)&laddr, &prefixlen);
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
18227c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
18237c478bd9Sstevel@tonic-gate 		/* Nothing there - ok */
18247c478bd9Sstevel@tonic-gate 		break;
18257c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
18267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18277c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", str);
18287c478bd9Sstevel@tonic-gate 		exit(1);
18297c478bd9Sstevel@tonic-gate 	default:
18307c478bd9Sstevel@tonic-gate 		(void) memset(&mask, 0, sizeof (mask));
18317c478bd9Sstevel@tonic-gate 		mask.ss_family = afp->af_af;
18327c478bd9Sstevel@tonic-gate 		if (afp->af_af == AF_INET6) {
18337c478bd9Sstevel@tonic-gate 			struct sockaddr_in6 *sin6;
18347c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&mask;
18357906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
18367c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin6->sin6_addr)) {
18377c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
18387c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
18397c478bd9Sstevel@tonic-gate 				    prefixlen);
18407c478bd9Sstevel@tonic-gate 				exit(1);
18417c478bd9Sstevel@tonic-gate 			}
18427c478bd9Sstevel@tonic-gate 		} else {
18437c478bd9Sstevel@tonic-gate 			struct sockaddr_in *sin;
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 			sin = (struct sockaddr_in *)&mask;
18467906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IP_ABITS,
18477c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin->sin_addr)) {
18487c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
18497c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
18507c478bd9Sstevel@tonic-gate 				    prefixlen);
18517c478bd9Sstevel@tonic-gate 				exit(1);
18527c478bd9Sstevel@tonic-gate 			}
18537c478bd9Sstevel@tonic-gate 		}
1854dd7a6f5fSkcpoon 		g_netmask_set = G_NETMASK_NIL;
18557c478bd9Sstevel@tonic-gate 		break;
18567c478bd9Sstevel@tonic-gate 	}
18577c478bd9Sstevel@tonic-gate 
1858dd7a6f5fSkcpoon 	/*
1859dd7a6f5fSkcpoon 	 * This is a "hack" to get around the problem of SIOCLIFADDIF.  The
1860dd7a6f5fSkcpoon 	 * problem is that this ioctl does not include the netmask when
1861dd7a6f5fSkcpoon 	 * adding a logical interface.  This is the same problem described
1862dd7a6f5fSkcpoon 	 * in the ifconfig() comments.  To get around this problem, we first
1863dd7a6f5fSkcpoon 	 * add the logical interface with a 0 address.  After that, we set
1864dd7a6f5fSkcpoon 	 * the netmask if provided.  Finally we set the interface address.
1865dd7a6f5fSkcpoon 	 */
18667c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1867dd7a6f5fSkcpoon 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	/* Note: no need to do DAD here since the interface isn't up yet. */
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0)
18727c478bd9Sstevel@tonic-gate 		Perror0_exit("addif: SIOCLIFADDIF");
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	(void) printf("Created new logical interface %s\n",
18757c478bd9Sstevel@tonic-gate 	    lifr.lifr_name);
18767c478bd9Sstevel@tonic-gate 	(void) strncpy(name, lifr.lifr_name, sizeof (name));
18777c478bd9Sstevel@tonic-gate 
1878dd7a6f5fSkcpoon 	/*
1879dd7a6f5fSkcpoon 	 * Check and see if any "netmask" command is used and perform the
1880dd7a6f5fSkcpoon 	 * necessary operation.
1881dd7a6f5fSkcpoon 	 */
1882dd7a6f5fSkcpoon 	set_mask_lifreq(&lifr, &laddr, &mask);
18836e91bba0SGirish Moodalbail 
18846e91bba0SGirish Moodalbail 	/* This check is temporary until libipadm supports IPMP interfaces. */
18856e91bba0SGirish Moodalbail 	if (ifconfig_use_libipadm(s, name)) {
18866e91bba0SGirish Moodalbail 		/*
18876e91bba0SGirish Moodalbail 		 * We added the logical interface above before calling
18886e91bba0SGirish Moodalbail 		 * ipadm_create_addr(), because, with IPH_LEGACY, we need
18896e91bba0SGirish Moodalbail 		 * to do an addif for `ifconfig ce0 addif <addr>' but not for
18906e91bba0SGirish Moodalbail 		 * `ifconfig ce0 <addr>'. libipadm does not have a flag to
18916e91bba0SGirish Moodalbail 		 * to differentiate between these two cases. To keep it simple,
18926e91bba0SGirish Moodalbail 		 * we always create the logical interface and pass it to
18936e91bba0SGirish Moodalbail 		 * libipadm instead of requiring libipadm to addif for some
18946e91bba0SGirish Moodalbail 		 * cases and not do addif for other cases.
18956e91bba0SGirish Moodalbail 		 */
18966e91bba0SGirish Moodalbail 		istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name,
18976e91bba0SGirish Moodalbail 		    &ipaddr);
18986e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
18996e91bba0SGirish Moodalbail 			ipadmerr_exit(istatus, "addif");
19006e91bba0SGirish Moodalbail 
19016e91bba0SGirish Moodalbail 		if (strchr(str, '/') == NULL) {
19026e91bba0SGirish Moodalbail 			/*
19036e91bba0SGirish Moodalbail 			 * lifr.lifr_addr, which is updated by set_mask_lifreq()
19046e91bba0SGirish Moodalbail 			 * will contain the right mask to use.
19056e91bba0SGirish Moodalbail 			 */
19066e91bba0SGirish Moodalbail 			prefixlen = mask2plen(&lifr.lifr_addr);
19076e91bba0SGirish Moodalbail 			(void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d",
19086e91bba0SGirish Moodalbail 			    str, prefixlen);
19096e91bba0SGirish Moodalbail 			str = cidraddr;
19106e91bba0SGirish Moodalbail 		}
19116e91bba0SGirish Moodalbail 		istatus = ipadm_set_addr(ipaddr, str, af);
19126e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
19136e91bba0SGirish Moodalbail 			ipadmerr_exit(istatus, "could not set address");
19146e91bba0SGirish Moodalbail 		setaddr++;
19156e91bba0SGirish Moodalbail 		/*
19166e91bba0SGirish Moodalbail 		 * address will be set by the parser after nextarg
19176e91bba0SGirish Moodalbail 		 * has been scanned
19186e91bba0SGirish Moodalbail 		 */
19196e91bba0SGirish Moodalbail 		return (0);
19206e91bba0SGirish Moodalbail 	}
19216e91bba0SGirish Moodalbail 
1922dd7a6f5fSkcpoon 	/*
1923dd7a6f5fSkcpoon 	 * Only set the netmask if "netmask" command is used or a prefix is
1924dd7a6f5fSkcpoon 	 * provided.
1925dd7a6f5fSkcpoon 	 */
1926dd7a6f5fSkcpoon 	if (g_netmask_set == G_NETMASK_SET || prefixlen >= 0) {
19276e91bba0SGirish Moodalbail 		/*
19286e91bba0SGirish Moodalbail 		 * lifr.lifr_addr already contains netmask from
19296e91bba0SGirish Moodalbail 		 * set_mask_lifreq().
19306e91bba0SGirish Moodalbail 		 */
19317c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
19327c478bd9Sstevel@tonic-gate 			Perror0_exit("addif: SIOCSLIFNETMASK");
19337c478bd9Sstevel@tonic-gate 	}
1934dd7a6f5fSkcpoon 
1935dd7a6f5fSkcpoon 	/* Finally, we set the interface address. */
1936dd7a6f5fSkcpoon 	lifr.lifr_addr = laddr;
1937dd7a6f5fSkcpoon 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1938dd7a6f5fSkcpoon 		Perror0_exit("SIOCSLIFADDR");
1939dd7a6f5fSkcpoon 
19407c478bd9Sstevel@tonic-gate 	/*
19417c478bd9Sstevel@tonic-gate 	 * let parser know we got a source.
19427c478bd9Sstevel@tonic-gate 	 * Next address, if given, should be dest
19437c478bd9Sstevel@tonic-gate 	 */
19447c478bd9Sstevel@tonic-gate 	setaddr++;
19457c478bd9Sstevel@tonic-gate 	return (0);
19467c478bd9Sstevel@tonic-gate }
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate /*
19497c478bd9Sstevel@tonic-gate  * Remove a logical interface based on its IP address. Unlike addif
19507c478bd9Sstevel@tonic-gate  * there is no '/<n>' here.
19517c478bd9Sstevel@tonic-gate  * Verifies that the interface is down before it is removed.
19527c478bd9Sstevel@tonic-gate  */
19537c478bd9Sstevel@tonic-gate /* ARGSUSED */
19547c478bd9Sstevel@tonic-gate static int
19557c478bd9Sstevel@tonic-gate removeif(char *str, int64_t param)
19567c478bd9Sstevel@tonic-gate {
19577c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
19586e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
19596e91bba0SGirish Moodalbail 	ipadm_addr_info_t *ainfo, *ainfop;
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
19627c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
19637c478bd9Sstevel@tonic-gate 		    "ifconfig: removeif: bad physical interface name %s\n",
19647c478bd9Sstevel@tonic-gate 		    name);
19657c478bd9Sstevel@tonic-gate 		exit(1);
19667c478bd9Sstevel@tonic-gate 	}
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(str, &laddr, NULL);
19697c478bd9Sstevel@tonic-gate 
19706e91bba0SGirish Moodalbail 	/*
19716e91bba0SGirish Moodalbail 	 * Following check is temporary until libipadm supports
19726e91bba0SGirish Moodalbail 	 * IPMP interfaces.
19736e91bba0SGirish Moodalbail 	 */
19746e91bba0SGirish Moodalbail 	if (!ifconfig_use_libipadm(s, name))
19756e91bba0SGirish Moodalbail 		goto delete;
19766e91bba0SGirish Moodalbail 
19776e91bba0SGirish Moodalbail 	/*
19786e91bba0SGirish Moodalbail 	 * Get all addresses and search this address among the active
19796e91bba0SGirish Moodalbail 	 * addresses. If an address object was found, delete using
19806e91bba0SGirish Moodalbail 	 * ipadm_delete_addr().
19816e91bba0SGirish Moodalbail 	 */
19826e91bba0SGirish Moodalbail 	istatus = ipadm_addr_info(iph, name, &ainfo, 0, LIFC_DEFAULT);
19836e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS)
19846e91bba0SGirish Moodalbail 		ipadmerr_exit(istatus, "removeif");
19856e91bba0SGirish Moodalbail 
19866e91bba0SGirish Moodalbail 	for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop))
19876e91bba0SGirish Moodalbail 		if (sockaddrcmp(ainfop->ia_ifa.ifa_addr, &laddr))
19886e91bba0SGirish Moodalbail 			break;
19896e91bba0SGirish Moodalbail 
19906e91bba0SGirish Moodalbail 	if (ainfop != NULL && ainfop->ia_aobjname[0] != '\0') {
19916e91bba0SGirish Moodalbail 		istatus = ipadm_delete_addr(iph, ainfop->ia_aobjname,
19926e91bba0SGirish Moodalbail 		    IPADM_OPT_ACTIVE);
19936e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
19946e91bba0SGirish Moodalbail 			ipadmerr_exit(istatus, "could not delete address");
19956e91bba0SGirish Moodalbail 		ipadm_free_addr_info(ainfo);
19966e91bba0SGirish Moodalbail 		return (0);
19976e91bba0SGirish Moodalbail 	}
19986e91bba0SGirish Moodalbail 	ipadm_free_addr_info(ainfo);
19996e91bba0SGirish Moodalbail 
20006e91bba0SGirish Moodalbail delete:
20016e91bba0SGirish Moodalbail 	/*
20026e91bba0SGirish Moodalbail 	 * An address object for this address was not found in ipadm.
20036e91bba0SGirish Moodalbail 	 * Delete with SIOCLIFREMOVEIF.
20046e91bba0SGirish Moodalbail 	 */
20056e91bba0SGirish Moodalbail 	lifr.lifr_addr = laddr;
20067c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
20077c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) {
20087c478bd9Sstevel@tonic-gate 		if (errno == EBUSY) {
20097c478bd9Sstevel@tonic-gate 			/* This can only happen if ipif_id = 0 */
20107c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
20117c478bd9Sstevel@tonic-gate 			    "ifconfig: removeif: can't remove interface: %s\n",
20127c478bd9Sstevel@tonic-gate 			    name);
20137c478bd9Sstevel@tonic-gate 			exit(1);
20147c478bd9Sstevel@tonic-gate 		}
20157c478bd9Sstevel@tonic-gate 		Perror0_exit("removeif: SIOCLIFREMOVEIF");
20167c478bd9Sstevel@tonic-gate 	}
20177c478bd9Sstevel@tonic-gate 	return (0);
20187c478bd9Sstevel@tonic-gate }
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate /*
20217c478bd9Sstevel@tonic-gate  * Set the address token for IPv6.
20227c478bd9Sstevel@tonic-gate  */
20237c478bd9Sstevel@tonic-gate /* ARGSUSED */
20247c478bd9Sstevel@tonic-gate static int
20257c478bd9Sstevel@tonic-gate setiftoken(char *addr, int64_t param)
20267c478bd9Sstevel@tonic-gate {
20277c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
20287c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 token;
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	in6_getaddr(addr, (struct sockaddr *)&token, &prefixlen);
20317c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
20327c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
20337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
20347c478bd9Sstevel@tonic-gate 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
20357c478bd9Sstevel@tonic-gate 		exit(1);
20367c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
20377c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
20387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
20397c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
20407c478bd9Sstevel@tonic-gate 		exit(1);
20417c478bd9Sstevel@tonic-gate 	default:
20427c478bd9Sstevel@tonic-gate 		break;
20437c478bd9Sstevel@tonic-gate 	}
20447c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &token, sizeof (token));
20457c478bd9Sstevel@tonic-gate 	lifr.lifr_addrlen = prefixlen;
20467c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
20477c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFTOKEN, (caddr_t)&lifr) < 0)  {
20487c478bd9Sstevel@tonic-gate 		Perror0_exit("setiftoken: SIOCSLIFTOKEN");
20497c478bd9Sstevel@tonic-gate 	}
20507c478bd9Sstevel@tonic-gate 	return (0);
20517c478bd9Sstevel@tonic-gate }
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate /* ARGSUSED */
20547c478bd9Sstevel@tonic-gate static int
2055e11c3f44Smeem setifgroupname(char *grname, int64_t param)
20567c478bd9Sstevel@tonic-gate {
2057e11c3f44Smeem 	lifgroupinfo_t		lifgr;
2058e11c3f44Smeem 	struct lifreq		lifrl;
2059e11c3f44Smeem 	ifaddrlistx_t		*ifaddrp, *nextifaddrp;
2060e11c3f44Smeem 	ifaddrlistx_t		*ifaddrs = NULL, *downaddrs = NULL;
2061e11c3f44Smeem 	int			af;
2062e11c3f44Smeem 
20637c478bd9Sstevel@tonic-gate 	if (debug) {
20647c478bd9Sstevel@tonic-gate 		(void) printf("Setting groupname %s on interface %s\n",
2065e11c3f44Smeem 		    grname, name);
20667c478bd9Sstevel@tonic-gate 	}
20677c478bd9Sstevel@tonic-gate 
2068e11c3f44Smeem 	(void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ);
2069e11c3f44Smeem 	(void) strlcpy(lifrl.lifr_groupname, grname, LIFGRNAMSIZ);
2070e11c3f44Smeem 
2071e11c3f44Smeem 	while (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) {
2072e11c3f44Smeem 		switch (errno) {
2073e11c3f44Smeem 		case ENOENT:
2074e11c3f44Smeem 			/*
2075e11c3f44Smeem 			 * The group doesn't yet exist; create it and repeat.
2076e11c3f44Smeem 			 */
2077e11c3f44Smeem 			af = afp->af_af;
2078e11c3f44Smeem 			if (create_ipmp(grname, af, NULL, _B_TRUE) == -1) {
2079e11c3f44Smeem 				if (errno == EEXIST)
2080e11c3f44Smeem 					continue;
2081e11c3f44Smeem 
2082e11c3f44Smeem 				Perror2(grname, "cannot create IPMP group");
2083e11c3f44Smeem 				goto fail;
2084e11c3f44Smeem 			}
2085e11c3f44Smeem 			continue;
2086e11c3f44Smeem 
2087e11c3f44Smeem 		case EALREADY:
2088e11c3f44Smeem 			/*
2089e11c3f44Smeem 			 * The interface is already in another group; must
2090e11c3f44Smeem 			 * remove existing membership first.
2091e11c3f44Smeem 			 */
2092e11c3f44Smeem 			lifrl.lifr_groupname[0] = '\0';
2093e11c3f44Smeem 			if (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) {
2094e11c3f44Smeem 				Perror2(name, "cannot remove existing "
2095e11c3f44Smeem 				    "IPMP group membership");
2096e11c3f44Smeem 				goto fail;
2097e11c3f44Smeem 			}
2098e11c3f44Smeem 			(void) strlcpy(lifrl.lifr_groupname, grname,
2099e11c3f44Smeem 			    LIFGRNAMSIZ);
2100e11c3f44Smeem 			continue;
2101e11c3f44Smeem 
2102e11c3f44Smeem 		case EAFNOSUPPORT:
2103e11c3f44Smeem 			/*
2104e11c3f44Smeem 			 * The group exists, but it's not configured with the
2105e11c3f44Smeem 			 * address families the interface needs.  Since only
2106e11c3f44Smeem 			 * two address families are currently supported, just
2107e11c3f44Smeem 			 * configure the "other" address family.  Note that we
2108e11c3f44Smeem 			 * may race with group deletion or creation by another
2109e11c3f44Smeem 			 * process (ENOENT or EEXIST); in such cases we repeat
2110e11c3f44Smeem 			 * our original SIOCSLIFGROUPNAME.
2111e11c3f44Smeem 			 */
2112e11c3f44Smeem 			(void) strlcpy(lifgr.gi_grname, grname, LIFGRNAMSIZ);
2113e11c3f44Smeem 			if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1) {
2114e11c3f44Smeem 				if (errno == ENOENT)
2115e11c3f44Smeem 					continue;
2116e11c3f44Smeem 
2117e11c3f44Smeem 				Perror2(grname, "SIOCGLIFGROUPINFO");
2118e11c3f44Smeem 				goto fail;
2119e11c3f44Smeem 			}
2120e11c3f44Smeem 
2121e11c3f44Smeem 			af = lifgr.gi_v4 ? AF_INET6 : AF_INET;
2122e11c3f44Smeem 			if (create_ipmp(grname, af, lifgr.gi_grifname,
2123e11c3f44Smeem 			    _B_TRUE) == -1) {
2124e11c3f44Smeem 				if (errno == EEXIST)
2125e11c3f44Smeem 					continue;
2126e11c3f44Smeem 
2127e11c3f44Smeem 				Perror2(grname, "cannot configure IPMP group");
2128e11c3f44Smeem 				goto fail;
2129e11c3f44Smeem 			}
2130e11c3f44Smeem 			continue;
2131e11c3f44Smeem 
2132e11c3f44Smeem 		case EADDRINUSE:
2133e11c3f44Smeem 			/*
2134e11c3f44Smeem 			 * Some addresses are in-use (or under control of DAD).
2135e11c3f44Smeem 			 * Bring them down and retry the group join operation.
2136e11c3f44Smeem 			 * We will bring them back up after the interface has
2137e11c3f44Smeem 			 * been placed in the group.
2138e11c3f44Smeem 			 */
2139e11c3f44Smeem 			if (ifaddrlistx(lifrl.lifr_name, IFF_UP|IFF_DUPLICATE,
2140e11c3f44Smeem 			    0, &ifaddrs) == -1) {
2141e11c3f44Smeem 				Perror2(grname, "cannot get address list");
2142e11c3f44Smeem 				goto fail;
2143e11c3f44Smeem 			}
2144e11c3f44Smeem 
2145e11c3f44Smeem 			ifaddrp = ifaddrs;
2146e11c3f44Smeem 			for (; ifaddrp != NULL; ifaddrp = nextifaddrp) {
2147e11c3f44Smeem 				if (!ifaddr_down(ifaddrp)) {
2148e11c3f44Smeem 					ifaddrs = ifaddrp;
2149e11c3f44Smeem 					goto fail;
2150e11c3f44Smeem 				}
2151e11c3f44Smeem 				nextifaddrp = ifaddrp->ia_next;
2152e11c3f44Smeem 				ifaddrp->ia_next = downaddrs;
2153e11c3f44Smeem 				downaddrs = ifaddrp;
2154e11c3f44Smeem 			}
2155e11c3f44Smeem 			ifaddrs = NULL;
2156e11c3f44Smeem 			continue;
2157e11c3f44Smeem 
2158e11c3f44Smeem 		case EADDRNOTAVAIL: {
2159e11c3f44Smeem 			/*
2160e11c3f44Smeem 			 * Some data addresses are under application control.
2161e11c3f44Smeem 			 * For some of these (e.g., ADDRCONF), the application
2162e11c3f44Smeem 			 * should remove the address, in which case we retry a
2163e11c3f44Smeem 			 * few times (since the application's action is not
2164e11c3f44Smeem 			 * atomic with respect to us) before bailing out and
2165e11c3f44Smeem 			 * informing the user.
2166e11c3f44Smeem 			 */
2167e11c3f44Smeem 			int ntries, nappaddr = 0;
2168e11c3f44Smeem 			const if_appflags_t *iap = if_appflags_tbl;
2169e11c3f44Smeem 
2170e11c3f44Smeem 			for (; iap->ia_app != NULL; iap++) {
2171e11c3f44Smeem 				ntries = 0;
2172e11c3f44Smeem again:
2173e11c3f44Smeem 				if (ifaddrlistx(lifrl.lifr_name, iap->ia_flag,
2174e11c3f44Smeem 				    IFF_NOFAILOVER, &ifaddrs) == -1) {
2175e11c3f44Smeem 					(void) fprintf(stderr, "ifconfig: %s: "
2176e11c3f44Smeem 					    "cannot get data addresses managed "
2177e11c3f44Smeem 					    "by %s\n", lifrl.lifr_name,
2178e11c3f44Smeem 					    iap->ia_app);
2179e11c3f44Smeem 					goto fail;
2180e11c3f44Smeem 				}
2181e11c3f44Smeem 
2182e11c3f44Smeem 				if (ifaddrs == NULL)
2183e11c3f44Smeem 					continue;
2184e11c3f44Smeem 
2185e11c3f44Smeem 				ifaddrlistx_free(ifaddrs);
2186e11c3f44Smeem 				ifaddrs = NULL;
2187e11c3f44Smeem 
2188e11c3f44Smeem 				if (++ntries < iap->ia_tries) {
2189e11c3f44Smeem 					(void) poll(NULL, 0, 100);
2190e11c3f44Smeem 					goto again;
2191e11c3f44Smeem 				}
2192e11c3f44Smeem 
2193e11c3f44Smeem 				(void) fprintf(stderr, "ifconfig: cannot join "
2194e11c3f44Smeem 				    "IPMP group: %s has data addresses managed "
2195e11c3f44Smeem 				    "by %s\n", lifrl.lifr_name, iap->ia_app);
2196e11c3f44Smeem 				nappaddr++;
2197e11c3f44Smeem 			}
2198e11c3f44Smeem 			if (nappaddr > 0)
2199e11c3f44Smeem 				goto fail;
2200e11c3f44Smeem 			continue;
2201e11c3f44Smeem 		}
2202e11c3f44Smeem 		default:
2203e11c3f44Smeem 			Perror2(name, "SIOCSLIFGROUPNAME");
2204e11c3f44Smeem 			goto fail;
2205e11c3f44Smeem 		}
22067c478bd9Sstevel@tonic-gate 	}
22077c478bd9Sstevel@tonic-gate 
2208*36b41818SGirish Moodalbail 	/*
2209*36b41818SGirish Moodalbail 	 * If the interface being moved is under the control of `ipmgmtd(1M)'
2210*36b41818SGirish Moodalbail 	 * dameon then we should inform the daemon about this move, so that
2211*36b41818SGirish Moodalbail 	 * the daemon can delete the state associated with this interface.
2212*36b41818SGirish Moodalbail 	 *
2213*36b41818SGirish Moodalbail 	 * This workaround is needed until the IPMP support in ipadm(1M).
2214*36b41818SGirish Moodalbail 	 */
2215*36b41818SGirish Moodalbail 	ipadm_if_move(iph, name);
2216*36b41818SGirish Moodalbail 
22177c478bd9Sstevel@tonic-gate 	/*
2218e11c3f44Smeem 	 * If there were addresses that we had to bring down, it's time to
2219e11c3f44Smeem 	 * bring them up again.  As part of bringing them up, the kernel will
2220e11c3f44Smeem 	 * automatically move them to the new IPMP interface.
22217c478bd9Sstevel@tonic-gate 	 */
2222e11c3f44Smeem 	for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
2223e11c3f44Smeem 		if (!ifaddr_up(ifaddrp) && errno != ENXIO) {
2224e11c3f44Smeem 			(void) fprintf(stderr, "ifconfig: cannot bring back up "
2225e11c3f44Smeem 			    "%s: %s\n", ifaddrp->ia_name, strerror(errno));
2226e11c3f44Smeem 		}
22277c478bd9Sstevel@tonic-gate 	}
2228e11c3f44Smeem 	ifaddrlistx_free(downaddrs);
2229e11c3f44Smeem 	return (0);
2230e11c3f44Smeem fail:
22317c478bd9Sstevel@tonic-gate 	/*
2232e11c3f44Smeem 	 * Attempt to bring back up any interfaces that we downed.
22337c478bd9Sstevel@tonic-gate 	 */
2234e11c3f44Smeem 	for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
2235e11c3f44Smeem 		if (!ifaddr_up(ifaddrp) && errno != ENXIO) {
2236e11c3f44Smeem 			(void) fprintf(stderr, "ifconfig: cannot bring back up "
2237e11c3f44Smeem 			    "%s: %s\n", ifaddrp->ia_name, strerror(errno));
2238e11c3f44Smeem 		}
22397c478bd9Sstevel@tonic-gate 	}
2240e11c3f44Smeem 	ifaddrlistx_free(downaddrs);
2241e11c3f44Smeem 	ifaddrlistx_free(ifaddrs);
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 	/*
2244e11c3f44Smeem 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2245e11c3f44Smeem 	 * into the exit status, so we're forced to explicitly exit().
22467c478bd9Sstevel@tonic-gate 	 */
2247e11c3f44Smeem 	exit(1);
2248e11c3f44Smeem 	/* NOTREACHED */
2249e11c3f44Smeem }
22507c478bd9Sstevel@tonic-gate 
2251e11c3f44Smeem static boolean_t
2252e11c3f44Smeem modcheck(const char *ifname)
2253e11c3f44Smeem {
2254e11c3f44Smeem 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2255e11c3f44Smeem 
2256e11c3f44Smeem 	if (ioctl(s, SIOCGLIFFLAGS, &lifr) < 0) {
2257e11c3f44Smeem 		Perror0("SIOCGLIFFLAGS");
2258e11c3f44Smeem 		return (_B_FALSE);
22597c478bd9Sstevel@tonic-gate 	}
22607c478bd9Sstevel@tonic-gate 
2261e11c3f44Smeem 	if (lifr.lifr_flags & IFF_IPMP) {
2262e11c3f44Smeem 		(void) fprintf(stderr, "ifconfig: %s: module operations not"
2263e11c3f44Smeem 		    " supported on IPMP interfaces\n", ifname);
2264e11c3f44Smeem 		return (_B_FALSE);
2265e11c3f44Smeem 	}
2266e11c3f44Smeem 	if (lifr.lifr_flags & IFF_VIRTUAL) {
2267e11c3f44Smeem 		(void) fprintf(stderr, "ifconfig: %s: module operations not"
2268e11c3f44Smeem 		    " supported on virtual IP interfaces\n", ifname);
2269e11c3f44Smeem 		return (_B_FALSE);
2270e11c3f44Smeem 	}
2271e11c3f44Smeem 	return (_B_TRUE);
2272e11c3f44Smeem }
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate /*
22757c478bd9Sstevel@tonic-gate  * To list all the modules above a given network interface.
22767c478bd9Sstevel@tonic-gate  */
22777c478bd9Sstevel@tonic-gate /* ARGSUSED */
22787c478bd9Sstevel@tonic-gate static int
22797c478bd9Sstevel@tonic-gate modlist(char *null, int64_t param)
22807c478bd9Sstevel@tonic-gate {
2281fc80c0dfSnordmark 	int muxid_fd;
22827c478bd9Sstevel@tonic-gate 	int muxfd;
22837c478bd9Sstevel@tonic-gate 	int ipfd_lowstr;
22847c478bd9Sstevel@tonic-gate 	int arpfd_lowstr;
22857c478bd9Sstevel@tonic-gate 	int num_mods;
22867c478bd9Sstevel@tonic-gate 	int i;
22877c478bd9Sstevel@tonic-gate 	struct str_list strlist;
22887c478bd9Sstevel@tonic-gate 	int orig_arpid;
22897c478bd9Sstevel@tonic-gate 
2290e11c3f44Smeem 	/*
2291e11c3f44Smeem 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2292e11c3f44Smeem 	 * into the exit status, so we're forced to explicitly exit().
2293e11c3f44Smeem 	 */
2294e11c3f44Smeem 	if (!modcheck(name))
2295e11c3f44Smeem 		exit(1);
2296e11c3f44Smeem 
2297fc80c0dfSnordmark 	if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
22987c478bd9Sstevel@tonic-gate 	    &orig_arpid) < 0) {
22997c478bd9Sstevel@tonic-gate 		return (-1);
23007c478bd9Sstevel@tonic-gate 	}
23017c478bd9Sstevel@tonic-gate 	if ((num_mods = ioctl(ipfd_lowstr, I_LIST, NULL)) < 0) {
23027c478bd9Sstevel@tonic-gate 		Perror0("cannot I_LIST to get the number of modules");
23037c478bd9Sstevel@tonic-gate 	} else {
23047c478bd9Sstevel@tonic-gate 		if (debug > 0) {
23057c478bd9Sstevel@tonic-gate 			(void) printf("Listing (%d) modules above %s\n",
23067c478bd9Sstevel@tonic-gate 			    num_mods, name);
23077c478bd9Sstevel@tonic-gate 		}
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 		strlist.sl_nmods = num_mods;
23107c478bd9Sstevel@tonic-gate 		strlist.sl_modlist = malloc(sizeof (struct str_mlist) *
23117c478bd9Sstevel@tonic-gate 		    num_mods);
23127c478bd9Sstevel@tonic-gate 		if (strlist.sl_modlist == NULL) {
23137c478bd9Sstevel@tonic-gate 			Perror0("cannot malloc");
23147c478bd9Sstevel@tonic-gate 		} else {
23157c478bd9Sstevel@tonic-gate 			if (ioctl(ipfd_lowstr, I_LIST, (caddr_t)&strlist) < 0) {
23167c478bd9Sstevel@tonic-gate 				Perror0("cannot I_LIST for module names");
23177c478bd9Sstevel@tonic-gate 			} else {
23187c478bd9Sstevel@tonic-gate 				for (i = 0; i < strlist.sl_nmods; i++) {
2319fc80c0dfSnordmark 					(void) printf("%d %s\n", i,
2320fc80c0dfSnordmark 					    strlist.sl_modlist[i].l_name);
23217c478bd9Sstevel@tonic-gate 				}
23227c478bd9Sstevel@tonic-gate 			}
23237c478bd9Sstevel@tonic-gate 			free(strlist.sl_modlist);
23247c478bd9Sstevel@tonic-gate 		}
23257c478bd9Sstevel@tonic-gate 	}
2326fc80c0dfSnordmark 	return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2327fc80c0dfSnordmark 	    orig_arpid));
23287c478bd9Sstevel@tonic-gate }
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate #define	MODINSERT_OP	'i'
23317c478bd9Sstevel@tonic-gate #define	MODREMOVE_OP	'r'
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate /*
23347c478bd9Sstevel@tonic-gate  * To insert a module to the stream of the interface.  It is just a
23357c478bd9Sstevel@tonic-gate  * wrapper.  The real function is modop().
23367c478bd9Sstevel@tonic-gate  */
23377c478bd9Sstevel@tonic-gate /* ARGSUSED */
23387c478bd9Sstevel@tonic-gate static int
23397c478bd9Sstevel@tonic-gate modinsert(char *arg, int64_t param)
23407c478bd9Sstevel@tonic-gate {
23417c478bd9Sstevel@tonic-gate 	return (modop(arg, MODINSERT_OP));
23427c478bd9Sstevel@tonic-gate }
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate /*
23457c478bd9Sstevel@tonic-gate  * To remove a module from the stream of the interface.  It is just a
23467c478bd9Sstevel@tonic-gate  * wrapper.  The real function is modop().
23477c478bd9Sstevel@tonic-gate  */
23487c478bd9Sstevel@tonic-gate /* ARGSUSED */
23497c478bd9Sstevel@tonic-gate static int
23507c478bd9Sstevel@tonic-gate modremove(char *arg, int64_t param)
23517c478bd9Sstevel@tonic-gate {
23527c478bd9Sstevel@tonic-gate 	return (modop(arg, MODREMOVE_OP));
23537c478bd9Sstevel@tonic-gate }
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate /*
23567c478bd9Sstevel@tonic-gate  * Helper function for mod*() functions.  It gets a fd to the lower IP
23577c478bd9Sstevel@tonic-gate  * stream and I_PUNLINK's the lower stream.  It also initializes the
23587c478bd9Sstevel@tonic-gate  * global variable lifr.
23597c478bd9Sstevel@tonic-gate  *
23607c478bd9Sstevel@tonic-gate  * Param:
2361fc80c0dfSnordmark  *	int *muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2362fc80c0dfSnordmark  *	int *muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2363fc80c0dfSnordmark  *	int *ipfd_lowstr: fd to the lower IP stream.
2364fc80c0dfSnordmark  *	int *arpfd_lowstr: fd to the lower ARP stream.
23657c478bd9Sstevel@tonic-gate  *
23667c478bd9Sstevel@tonic-gate  * Return:
23677c478bd9Sstevel@tonic-gate  *	-1 if operation fails, 0 otherwise.
23687c478bd9Sstevel@tonic-gate  *
2369e11c3f44Smeem  * Please see the big block comment above ifplumb() for the logic of the
2370e11c3f44Smeem  * PLINK/PUNLINK
23717c478bd9Sstevel@tonic-gate  */
23727c478bd9Sstevel@tonic-gate static int
2373fc80c0dfSnordmark ip_domux2fd(int *muxfd, int *muxid_fd, int *ipfd_lowstr, int *arpfd_lowstr,
2374fc80c0dfSnordmark     int *orig_arpid)
23757c478bd9Sstevel@tonic-gate {
23767c478bd9Sstevel@tonic-gate 	uint64_t	flags;
23777c478bd9Sstevel@tonic-gate 	char		*udp_dev_name;
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	*orig_arpid = 0;
23807c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
23817c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
23827c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
23837c478bd9Sstevel@tonic-gate 	}
23847c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
23857c478bd9Sstevel@tonic-gate 	if (flags & IFF_IPV4) {
23867c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP_DEV_NAME;
23877c478bd9Sstevel@tonic-gate 	} else if (flags & IFF_IPV6) {
23887c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP6_DEV_NAME;
23897c478bd9Sstevel@tonic-gate 	} else {
23907c478bd9Sstevel@tonic-gate 		return (-1);
23917c478bd9Sstevel@tonic-gate 	}
23927c478bd9Sstevel@tonic-gate 
2393fc80c0dfSnordmark 	if ((*muxid_fd = open(udp_dev_name, O_RDWR)) < 0) {
2394fc80c0dfSnordmark 		Perror2("open", udp_dev_name);
23957c478bd9Sstevel@tonic-gate 		return (-1);
23967c478bd9Sstevel@tonic-gate 	}
2397fc80c0dfSnordmark 	if (ioctl(*muxid_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
2398fc80c0dfSnordmark 		Perror2("SIOCGLIFMUXID", udp_dev_name);
23997c478bd9Sstevel@tonic-gate 		return (-1);
24007c478bd9Sstevel@tonic-gate 	}
24017c478bd9Sstevel@tonic-gate 	if (debug > 0) {
24027c478bd9Sstevel@tonic-gate 		(void) printf("ARP_muxid %d IP_muxid %d\n",
24037c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid, lifr.lifr_ip_muxid);
24047c478bd9Sstevel@tonic-gate 	}
24057c478bd9Sstevel@tonic-gate 
2406fc80c0dfSnordmark 	/*
2407fc80c0dfSnordmark 	 * Use /dev/udp{,6} as the mux to avoid linkcycles.
2408fc80c0dfSnordmark 	 */
24096e91bba0SGirish Moodalbail 	if (ipadm_open_arp_on_udp(udp_dev_name, muxfd) != IPADM_SUCCESS)
24107c478bd9Sstevel@tonic-gate 		return (-1);
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 	if (lifr.lifr_arp_muxid != 0) {
24137c478bd9Sstevel@tonic-gate 		if ((*arpfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
24147c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid)) < 0) {
24157c478bd9Sstevel@tonic-gate 			if ((errno == EINVAL) &&
24167c478bd9Sstevel@tonic-gate 			    (flags & (IFF_NOARP | IFF_IPV6))) {
24177c478bd9Sstevel@tonic-gate 				/*
24187c478bd9Sstevel@tonic-gate 				 * Some plumbing utilities set the muxid to
24197c478bd9Sstevel@tonic-gate 				 * -1 or some invalid value to signify that
24207c478bd9Sstevel@tonic-gate 				 * there is no arp stream. Set the muxid to 0
24217c478bd9Sstevel@tonic-gate 				 * before trying to unplumb the IP stream.
24227c478bd9Sstevel@tonic-gate 				 * IP does not allow the IP stream to be
24237c478bd9Sstevel@tonic-gate 				 * unplumbed if it sees a non-null arp muxid,
24247c478bd9Sstevel@tonic-gate 				 * for consistency of IP-ARP streams.
24257c478bd9Sstevel@tonic-gate 				 */
24267c478bd9Sstevel@tonic-gate 				*orig_arpid = lifr.lifr_arp_muxid;
24277c478bd9Sstevel@tonic-gate 				lifr.lifr_arp_muxid = 0;
2428fc80c0dfSnordmark 				(void) ioctl(*muxid_fd, SIOCSLIFMUXID,
24297c478bd9Sstevel@tonic-gate 				    (caddr_t)&lifr);
24307c478bd9Sstevel@tonic-gate 				*arpfd_lowstr = -1;
24317c478bd9Sstevel@tonic-gate 			} else {
24327c478bd9Sstevel@tonic-gate 				Perror0("_I_MUXID2FD");
24337c478bd9Sstevel@tonic-gate 				return (-1);
24347c478bd9Sstevel@tonic-gate 			}
24357c478bd9Sstevel@tonic-gate 		} else if (ioctl(*muxfd, I_PUNLINK,
24367c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid) < 0) {
24377c478bd9Sstevel@tonic-gate 			Perror2("I_PUNLINK", udp_dev_name);
24387c478bd9Sstevel@tonic-gate 			return (-1);
24397c478bd9Sstevel@tonic-gate 		}
24407c478bd9Sstevel@tonic-gate 	} else {
24417c478bd9Sstevel@tonic-gate 		*arpfd_lowstr = -1;
24427c478bd9Sstevel@tonic-gate 	}
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 	if ((*ipfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
24457c478bd9Sstevel@tonic-gate 	    lifr.lifr_ip_muxid)) < 0) {
24467c478bd9Sstevel@tonic-gate 		Perror0("_I_MUXID2FD");
24477c478bd9Sstevel@tonic-gate 		/* Undo any changes we made */
24487c478bd9Sstevel@tonic-gate 		if (*orig_arpid != 0) {
24497c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = *orig_arpid;
2450fc80c0dfSnordmark 			(void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
24517c478bd9Sstevel@tonic-gate 		}
24527c478bd9Sstevel@tonic-gate 		return (-1);
24537c478bd9Sstevel@tonic-gate 	}
24547c478bd9Sstevel@tonic-gate 	if (ioctl(*muxfd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) {
24557c478bd9Sstevel@tonic-gate 		Perror2("I_PUNLINK", udp_dev_name);
24567c478bd9Sstevel@tonic-gate 		/* Undo any changes we made */
24577c478bd9Sstevel@tonic-gate 		if (*orig_arpid != 0) {
24587c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = *orig_arpid;
2459fc80c0dfSnordmark 			(void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
24607c478bd9Sstevel@tonic-gate 		}
24617c478bd9Sstevel@tonic-gate 		return (-1);
24627c478bd9Sstevel@tonic-gate 	}
24637c478bd9Sstevel@tonic-gate 	return (0);
24647c478bd9Sstevel@tonic-gate }
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate /*
24677c478bd9Sstevel@tonic-gate  * Helper function for mod*() functions.  It I_PLINK's back the upper and
24687c478bd9Sstevel@tonic-gate  * lower IP streams.  Note that this function must be called after
24697c478bd9Sstevel@tonic-gate  * ip_domux2fd().  In ip_domux2fd(), the global variable lifr is initialized
24707c478bd9Sstevel@tonic-gate  * and ip_plink() needs information in lifr.  So ip_domux2fd() and ip_plink()
24717c478bd9Sstevel@tonic-gate  * must be called in pairs.
24727c478bd9Sstevel@tonic-gate  *
24737c478bd9Sstevel@tonic-gate  * Param:
2474fc80c0dfSnordmark  *	int muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2475fc80c0dfSnordmark  *	int muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2476fc80c0dfSnordmark  *	int ipfd_lowstr: fd to the lower IP stream.
2477fc80c0dfSnordmark  *	int arpfd_lowstr: fd to the lower ARP stream.
24787c478bd9Sstevel@tonic-gate  *
24797c478bd9Sstevel@tonic-gate  * Return:
24807c478bd9Sstevel@tonic-gate  *	-1 if operation fails, 0 otherwise.
24817c478bd9Sstevel@tonic-gate  *
2482e11c3f44Smeem  * Please see the big block comment above ifplumb() for the logic of the
2483e11c3f44Smeem  * PLINK/PUNLINK
24847c478bd9Sstevel@tonic-gate  */
24857c478bd9Sstevel@tonic-gate static int
2486fc80c0dfSnordmark ip_plink(int muxfd, int muxid_fd, int ipfd_lowstr, int arpfd_lowstr,
2487fc80c0dfSnordmark     int orig_arpid)
24887c478bd9Sstevel@tonic-gate {
24897c478bd9Sstevel@tonic-gate 	int ip_muxid;
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate 	ip_muxid = ioctl(muxfd, I_PLINK, ipfd_lowstr);
24927c478bd9Sstevel@tonic-gate 	if (ip_muxid < 0) {
24937c478bd9Sstevel@tonic-gate 		Perror2("I_PLINK", UDP_DEV_NAME);
24947c478bd9Sstevel@tonic-gate 		return (-1);
24957c478bd9Sstevel@tonic-gate 	}
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate 	/*
24987c478bd9Sstevel@tonic-gate 	 * If there is an arp stream, plink it. If there is no
24997c478bd9Sstevel@tonic-gate 	 * arp stream, then it is possible that the plumbing
25007c478bd9Sstevel@tonic-gate 	 * utility could have stored any value in the arp_muxid.
25017c478bd9Sstevel@tonic-gate 	 * If so, restore it from orig_arpid.
25027c478bd9Sstevel@tonic-gate 	 */
25037c478bd9Sstevel@tonic-gate 	if (arpfd_lowstr != -1) {
25047c478bd9Sstevel@tonic-gate 		if (ioctl(muxfd, I_PLINK, arpfd_lowstr) < 0) {
25057c478bd9Sstevel@tonic-gate 			Perror2("I_PLINK", UDP_DEV_NAME);
25067c478bd9Sstevel@tonic-gate 			return (-1);
25077c478bd9Sstevel@tonic-gate 		}
25087c478bd9Sstevel@tonic-gate 	} else if (orig_arpid != 0) {
25097c478bd9Sstevel@tonic-gate 		/* Undo the changes we did in ip_domux2fd */
25107c478bd9Sstevel@tonic-gate 		lifr.lifr_arp_muxid = orig_arpid;
25117c478bd9Sstevel@tonic-gate 		lifr.lifr_ip_muxid = ip_muxid;
2512fc80c0dfSnordmark 		(void) ioctl(muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
25137c478bd9Sstevel@tonic-gate 	}
25147c478bd9Sstevel@tonic-gate 
2515fc80c0dfSnordmark 	(void) close(muxfd);
2516fc80c0dfSnordmark 	(void) close(muxid_fd);
25177c478bd9Sstevel@tonic-gate 	return (0);
25187c478bd9Sstevel@tonic-gate }
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate /*
25217c478bd9Sstevel@tonic-gate  * The real function to perform module insertion/removal.
25227c478bd9Sstevel@tonic-gate  *
25237c478bd9Sstevel@tonic-gate  * Param:
25247c478bd9Sstevel@tonic-gate  *	char *arg: the argument string module_name@position
25257c478bd9Sstevel@tonic-gate  *	char op: operation, either MODINSERT_OP or MODREMOVE_OP.
25267c478bd9Sstevel@tonic-gate  *
25277c478bd9Sstevel@tonic-gate  * Return:
25287c478bd9Sstevel@tonic-gate  *	Before doing ip_domux2fd(), this function calls exit(1) in case of
25297c478bd9Sstevel@tonic-gate  *	error.  After ip_domux2fd() is done, it returns -1 for error, 0
25307c478bd9Sstevel@tonic-gate  *	otherwise.
25317c478bd9Sstevel@tonic-gate  */
25327c478bd9Sstevel@tonic-gate static int
25337c478bd9Sstevel@tonic-gate modop(char *arg, char op)
25347c478bd9Sstevel@tonic-gate {
25357c478bd9Sstevel@tonic-gate 	char *pos_p;
25367c478bd9Sstevel@tonic-gate 	int muxfd;
2537fc80c0dfSnordmark 	int muxid_fd;
25387c478bd9Sstevel@tonic-gate 	int ipfd_lowstr;  /* IP stream (lower stream of mux) to be plinked */
25397c478bd9Sstevel@tonic-gate 	int arpfd_lowstr; /* ARP stream (lower stream of mux) to be plinked */
25407c478bd9Sstevel@tonic-gate 	struct strmodconf mod;
25417c478bd9Sstevel@tonic-gate 	char *at_char = "@";
25427c478bd9Sstevel@tonic-gate 	char *arg_str;
25437c478bd9Sstevel@tonic-gate 	int orig_arpid;
25447c478bd9Sstevel@tonic-gate 
2545e11c3f44Smeem 	/*
2546e11c3f44Smeem 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2547e11c3f44Smeem 	 * into the exit status, so we're forced to explicitly exit().
2548e11c3f44Smeem 	 */
2549e11c3f44Smeem 	if (!modcheck(name))
2550e11c3f44Smeem 		exit(1);
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate 	/* Need to save the original string for -a option. */
25537c478bd9Sstevel@tonic-gate 	if ((arg_str = malloc(strlen(arg) + 1)) == NULL) {
25547c478bd9Sstevel@tonic-gate 		Perror0("cannot malloc");
25557c478bd9Sstevel@tonic-gate 		return (-1);
25567c478bd9Sstevel@tonic-gate 	}
25577c478bd9Sstevel@tonic-gate 	(void) strcpy(arg_str, arg);
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate 	if (*arg_str == *at_char) {
25607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
25617c478bd9Sstevel@tonic-gate 		    "ifconfig: must supply a module name\n");
25627c478bd9Sstevel@tonic-gate 		exit(1);
25637c478bd9Sstevel@tonic-gate 	}
25647c478bd9Sstevel@tonic-gate 	mod.mod_name = strtok(arg_str, at_char);
25657c478bd9Sstevel@tonic-gate 	if (strlen(mod.mod_name) > FMNAMESZ) {
25667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: module name too long: %s\n",
25677c478bd9Sstevel@tonic-gate 		    mod.mod_name);
25687c478bd9Sstevel@tonic-gate 		exit(1);
25697c478bd9Sstevel@tonic-gate 	}
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate 	/*
25727c478bd9Sstevel@tonic-gate 	 * Need to make sure that the core TCP/IP stack modules are not
25737c478bd9Sstevel@tonic-gate 	 * removed.  Otherwise, "bad" things can happen.  If a module
25747c478bd9Sstevel@tonic-gate 	 * is removed and inserted back, it loses its old state.  But
25757c478bd9Sstevel@tonic-gate 	 * the modules above it still have the old state.  E.g. IP assumes
25767c478bd9Sstevel@tonic-gate 	 * fast data path while tunnel after re-inserted assumes that it can
25777c478bd9Sstevel@tonic-gate 	 * receive M_DATA only in fast data path for which it does not have
25787c478bd9Sstevel@tonic-gate 	 * any state.  This is a general caveat of _I_REMOVE/_I_INSERT.
25797c478bd9Sstevel@tonic-gate 	 */
25807c478bd9Sstevel@tonic-gate 	if (op == MODREMOVE_OP &&
25817c478bd9Sstevel@tonic-gate 	    (strcmp(mod.mod_name, ARP_MOD_NAME) == 0 ||
25822b24ab6bSSebastien Roy 	    strcmp(mod.mod_name, IP_MOD_NAME) == 0)) {
25837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: cannot remove %s\n",
25847c478bd9Sstevel@tonic-gate 		    mod.mod_name);
25857c478bd9Sstevel@tonic-gate 		exit(1);
25867c478bd9Sstevel@tonic-gate 	}
25877c478bd9Sstevel@tonic-gate 
25887c478bd9Sstevel@tonic-gate 	if ((pos_p = strtok(NULL, at_char)) == NULL) {
25897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: must supply a position\n");
25907c478bd9Sstevel@tonic-gate 		exit(1);
25917c478bd9Sstevel@tonic-gate 	}
25927c478bd9Sstevel@tonic-gate 	mod.pos = atoi(pos_p);
25937c478bd9Sstevel@tonic-gate 
2594fc80c0dfSnordmark 	if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
25957c478bd9Sstevel@tonic-gate 	    &orig_arpid) < 0) {
25967c478bd9Sstevel@tonic-gate 		free(arg_str);
25977c478bd9Sstevel@tonic-gate 		return (-1);
25987c478bd9Sstevel@tonic-gate 	}
25997c478bd9Sstevel@tonic-gate 	switch (op) {
26007c478bd9Sstevel@tonic-gate 	case MODINSERT_OP:
26017c478bd9Sstevel@tonic-gate 		if (debug > 0) {
26027c478bd9Sstevel@tonic-gate 			(void) printf("Inserting module %s at %d\n",
26037c478bd9Sstevel@tonic-gate 			    mod.mod_name, mod.pos);
26047c478bd9Sstevel@tonic-gate 		}
26057c478bd9Sstevel@tonic-gate 		if (ioctl(ipfd_lowstr, _I_INSERT, (caddr_t)&mod) < 0) {
26067c478bd9Sstevel@tonic-gate 			Perror2("fail to insert module", mod.mod_name);
26077c478bd9Sstevel@tonic-gate 		}
26087c478bd9Sstevel@tonic-gate 		break;
26097c478bd9Sstevel@tonic-gate 	case MODREMOVE_OP:
26107c478bd9Sstevel@tonic-gate 		if (debug > 0) {
26117c478bd9Sstevel@tonic-gate 			(void) printf("Removing module %s at %d\n",
26127c478bd9Sstevel@tonic-gate 			    mod.mod_name, mod.pos);
26137c478bd9Sstevel@tonic-gate 		}
26147c478bd9Sstevel@tonic-gate 		if (ioctl(ipfd_lowstr, _I_REMOVE, (caddr_t)&mod) < 0) {
26157c478bd9Sstevel@tonic-gate 			Perror2("fail to remove module", mod.mod_name);
26167c478bd9Sstevel@tonic-gate 		}
26177c478bd9Sstevel@tonic-gate 		break;
26187c478bd9Sstevel@tonic-gate 	default:
26197c478bd9Sstevel@tonic-gate 		/* Should never get to here. */
26207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Unknown operation\n");
26217c478bd9Sstevel@tonic-gate 		break;
26227c478bd9Sstevel@tonic-gate 	}
26237c478bd9Sstevel@tonic-gate 	free(arg_str);
2624fc80c0dfSnordmark 	return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2625fc80c0dfSnordmark 	    orig_arpid));
26267c478bd9Sstevel@tonic-gate }
26277c478bd9Sstevel@tonic-gate 
26282b24ab6bSSebastien Roy static int
26292b24ab6bSSebastien Roy modify_tun(iptun_params_t *params)
26302b24ab6bSSebastien Roy {
26312b24ab6bSSebastien Roy 	dladm_status_t status;
26322b24ab6bSSebastien Roy 
26332b24ab6bSSebastien Roy 	if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN,
26342b24ab6bSSebastien Roy 	    &params->iptun_param_linkid)) == DLADM_STATUS_OK)
26352b24ab6bSSebastien Roy 		status = dladm_iptun_modify(dlh, params, DLADM_OPT_ACTIVE);
26362b24ab6bSSebastien Roy 	if (status != DLADM_STATUS_OK)
26372b24ab6bSSebastien Roy 		dladmerr_exit(status, name);
26382b24ab6bSSebastien Roy 	return (0);
26392b24ab6bSSebastien Roy }
26402b24ab6bSSebastien Roy 
26417c478bd9Sstevel@tonic-gate /*
26427c478bd9Sstevel@tonic-gate  * Set tunnel source address
26437c478bd9Sstevel@tonic-gate  */
26447c478bd9Sstevel@tonic-gate /* ARGSUSED */
26457c478bd9Sstevel@tonic-gate static int
26467c478bd9Sstevel@tonic-gate setiftsrc(char *addr, int64_t param)
26477c478bd9Sstevel@tonic-gate {
26482b24ab6bSSebastien Roy 	iptun_params_t params;
26492b24ab6bSSebastien Roy 
26502b24ab6bSSebastien Roy 	params.iptun_param_flags = IPTUN_PARAM_LADDR;
26512b24ab6bSSebastien Roy 	(void) strlcpy(params.iptun_param_laddr, addr,
26522b24ab6bSSebastien Roy 	    sizeof (params.iptun_param_laddr));
26532b24ab6bSSebastien Roy 	return (modify_tun(&params));
26547c478bd9Sstevel@tonic-gate }
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate /*
26577c478bd9Sstevel@tonic-gate  * Set tunnel destination address
26587c478bd9Sstevel@tonic-gate  */
26597c478bd9Sstevel@tonic-gate /* ARGSUSED */
26607c478bd9Sstevel@tonic-gate static int
26617c478bd9Sstevel@tonic-gate setiftdst(char *addr, int64_t param)
26627c478bd9Sstevel@tonic-gate {
26632b24ab6bSSebastien Roy 	iptun_params_t params;
26642b24ab6bSSebastien Roy 
26652b24ab6bSSebastien Roy 	params.iptun_param_flags = IPTUN_PARAM_RADDR;
26662b24ab6bSSebastien Roy 	(void) strlcpy(params.iptun_param_raddr, addr,
26672b24ab6bSSebastien Roy 	    sizeof (params.iptun_param_raddr));
26682b24ab6bSSebastien Roy 	return (modify_tun(&params));
26697c478bd9Sstevel@tonic-gate }
26707c478bd9Sstevel@tonic-gate 
26717c478bd9Sstevel@tonic-gate static int
26722b24ab6bSSebastien Roy set_tun_prop(const char *propname, char *value)
26737c478bd9Sstevel@tonic-gate {
26742b24ab6bSSebastien Roy 	dladm_status_t	status;
26752b24ab6bSSebastien Roy 	datalink_id_t	linkid;
26767c478bd9Sstevel@tonic-gate 
26772b24ab6bSSebastien Roy 	status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid);
26782b24ab6bSSebastien Roy 	if (status == DLADM_STATUS_OK) {
26792b24ab6bSSebastien Roy 		status = dladm_set_linkprop(dlh, linkid, propname, &value, 1,
26802b24ab6bSSebastien Roy 		    DLADM_OPT_ACTIVE);
26817c478bd9Sstevel@tonic-gate 	}
26822b24ab6bSSebastien Roy 	if (status != DLADM_STATUS_OK)
26832b24ab6bSSebastien Roy 		dladmerr_exit(status, name);
26847c478bd9Sstevel@tonic-gate 	return (0);
26857c478bd9Sstevel@tonic-gate }
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate /* Set tunnel encapsulation limit. */
26887c478bd9Sstevel@tonic-gate /* ARGSUSED */
26897c478bd9Sstevel@tonic-gate static int
26907c478bd9Sstevel@tonic-gate set_tun_encap_limit(char *arg, int64_t param)
26917c478bd9Sstevel@tonic-gate {
26922b24ab6bSSebastien Roy 	return (set_tun_prop("encaplimit", arg));
26937c478bd9Sstevel@tonic-gate }
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate /* Disable encapsulation limit. */
26967c478bd9Sstevel@tonic-gate /* ARGSUSED */
26977c478bd9Sstevel@tonic-gate static int
26987c478bd9Sstevel@tonic-gate clr_tun_encap_limit(char *arg, int64_t param)
26997c478bd9Sstevel@tonic-gate {
27002b24ab6bSSebastien Roy 	return (set_tun_encap_limit("-1", 0));
27017c478bd9Sstevel@tonic-gate }
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate /* Set tunnel hop limit. */
27047c478bd9Sstevel@tonic-gate /* ARGSUSED */
27057c478bd9Sstevel@tonic-gate static int
27067c478bd9Sstevel@tonic-gate set_tun_hop_limit(char *arg, int64_t param)
27077c478bd9Sstevel@tonic-gate {
27082b24ab6bSSebastien Roy 	return (set_tun_prop("hoplimit", arg));
27097c478bd9Sstevel@tonic-gate }
27107c478bd9Sstevel@tonic-gate 
27117c478bd9Sstevel@tonic-gate /* Set zone ID */
27127c478bd9Sstevel@tonic-gate static int
27137c478bd9Sstevel@tonic-gate setzone(char *arg, int64_t param)
27147c478bd9Sstevel@tonic-gate {
27157c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = GLOBAL_ZONEID;
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate 	if (param == NEXTARG) {
27187c478bd9Sstevel@tonic-gate 		/* zone must be active */
27197c478bd9Sstevel@tonic-gate 		if ((zoneid = getzoneidbyname(arg)) == -1) {
27207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
27217c478bd9Sstevel@tonic-gate 			    "ifconfig: unknown zone '%s'\n", arg);
27227c478bd9Sstevel@tonic-gate 			exit(1);
27237c478bd9Sstevel@tonic-gate 		}
27247c478bd9Sstevel@tonic-gate 	}
27257c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
27267c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zoneid;
27277c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
27287c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFZONE");
27297c478bd9Sstevel@tonic-gate 	return (0);
27307c478bd9Sstevel@tonic-gate }
27317c478bd9Sstevel@tonic-gate 
273245916cd2Sjpk /* Put interface into all zones */
273345916cd2Sjpk /* ARGSUSED */
273445916cd2Sjpk static int
273545916cd2Sjpk setallzones(char *arg, int64_t param)
273645916cd2Sjpk {
273745916cd2Sjpk 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
273845916cd2Sjpk 	lifr.lifr_zoneid = ALL_ZONES;
273945916cd2Sjpk 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
274045916cd2Sjpk 		Perror0_exit("SIOCSLIFZONE");
274145916cd2Sjpk 	return (0);
274245916cd2Sjpk }
274345916cd2Sjpk 
27447c478bd9Sstevel@tonic-gate /* Set source address to use */
27457c478bd9Sstevel@tonic-gate /* ARGSUSED */
27467c478bd9Sstevel@tonic-gate static int
27477c478bd9Sstevel@tonic-gate setifsrc(char *arg, int64_t param)
27487c478bd9Sstevel@tonic-gate {
27497c478bd9Sstevel@tonic-gate 	uint_t ifindex = 0;
27507c478bd9Sstevel@tonic-gate 	int rval;
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	/*
27557c478bd9Sstevel@tonic-gate 	 * Argument can be either an interface name or "none". The latter means
27567c478bd9Sstevel@tonic-gate 	 * that any previous selection is cleared.
27577c478bd9Sstevel@tonic-gate 	 */
27587c478bd9Sstevel@tonic-gate 
27596e91bba0SGirish Moodalbail 	if (strchr(arg, ':') != NULL) {
27607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
27616e91bba0SGirish Moodalbail 		    "ifconfig: Cannot specify logical interface for usesrc \n");
27627c478bd9Sstevel@tonic-gate 		exit(1);
27637c478bd9Sstevel@tonic-gate 	}
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate 	rval = strcmp(arg, NONE_STR);
27667c478bd9Sstevel@tonic-gate 	if (rval != 0) {
27677c478bd9Sstevel@tonic-gate 		if ((ifindex = if_nametoindex(arg)) == 0) {
27687c478bd9Sstevel@tonic-gate 			(void) strncpy(lifr.lifr_name, arg, LIFNAMSIZ);
27697c478bd9Sstevel@tonic-gate 			Perror0_exit("Could not get interface index");
27707c478bd9Sstevel@tonic-gate 		}
27717c478bd9Sstevel@tonic-gate 		lifr.lifr_index = ifindex;
27727c478bd9Sstevel@tonic-gate 	} else {
27737c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) != 0)
27747c478bd9Sstevel@tonic-gate 			Perror0_exit("Not a valid usesrc consumer");
27757c478bd9Sstevel@tonic-gate 		lifr.lifr_index = 0;
27767c478bd9Sstevel@tonic-gate 	}
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate 	if (debug)
27797c478bd9Sstevel@tonic-gate 		(void) printf("setifsrc: lifr_name %s, lifr_index %d\n",
27807c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, lifr.lifr_index);
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFUSESRC, (caddr_t)&lifr) == -1) {
27837c478bd9Sstevel@tonic-gate 		if (rval == 0)
27847c478bd9Sstevel@tonic-gate 			Perror0_exit("Cannot reset usesrc group");
27857c478bd9Sstevel@tonic-gate 		else
27867c478bd9Sstevel@tonic-gate 			Perror0_exit("Could not set source interface");
27877c478bd9Sstevel@tonic-gate 	}
27887c478bd9Sstevel@tonic-gate 
27897c478bd9Sstevel@tonic-gate 	return (0);
27907c478bd9Sstevel@tonic-gate }
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate /*
27937c478bd9Sstevel@tonic-gate  * Print the interface status line associated with `ifname'
27947c478bd9Sstevel@tonic-gate  */
27957c478bd9Sstevel@tonic-gate static void
27967c478bd9Sstevel@tonic-gate ifstatus(const char *ifname)
27977c478bd9Sstevel@tonic-gate {
27987c478bd9Sstevel@tonic-gate 	uint64_t flags;
27997c478bd9Sstevel@tonic-gate 	char if_usesrc_name[LIFNAMSIZ];
28007c478bd9Sstevel@tonic-gate 	char *newbuf;
28017c478bd9Sstevel@tonic-gate 	int n, numifs, rval = 0;
28027c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp;
28037c478bd9Sstevel@tonic-gate 	struct lifsrcof lifs;
28047c478bd9Sstevel@tonic-gate 
28057c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
28067c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
28077c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
28087c478bd9Sstevel@tonic-gate 	}
28097c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	/*
28127c478bd9Sstevel@tonic-gate 	 * In V4 compatibility mode, we don't print the IFF_IPV4 flag or
28137c478bd9Sstevel@tonic-gate 	 * interfaces with IFF_IPV6 set.
28147c478bd9Sstevel@tonic-gate 	 */
28157c478bd9Sstevel@tonic-gate 	if (v4compat) {
28167c478bd9Sstevel@tonic-gate 		flags &= ~IFF_IPV4;
28177c478bd9Sstevel@tonic-gate 		if (flags & IFF_IPV6)
28187c478bd9Sstevel@tonic-gate 			return;
28197c478bd9Sstevel@tonic-gate 	}
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 	(void) printf("%s: ", ifname);
28227c478bd9Sstevel@tonic-gate 	print_flags(flags);
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
28257c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
28267c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFMETRIC");
28277c478bd9Sstevel@tonic-gate 	} else {
28287c478bd9Sstevel@tonic-gate 		if (lifr.lifr_metric)
28297c478bd9Sstevel@tonic-gate 			(void) printf(" metric %d", lifr.lifr_metric);
28307c478bd9Sstevel@tonic-gate 	}
28317c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
2832c08e5e1aSdr 		(void) printf(" mtu %u", lifr.lifr_mtu);
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 	/* don't print index or zone when in compatibility mode */
28357c478bd9Sstevel@tonic-gate 	if (!v4compat) {
28367c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
28377c478bd9Sstevel@tonic-gate 			(void) printf(" index %d", lifr.lifr_index);
2838f4b3ec61Sdh 		/*
2839f4b3ec61Sdh 		 * Stack instances use GLOBAL_ZONEID for IP data structures
2840f4b3ec61Sdh 		 * even in the non-global zone.
2841f4b3ec61Sdh 		 */
28427c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifr) >= 0 &&
2843f4b3ec61Sdh 		    lifr.lifr_zoneid != getzoneid() &&
2844f4b3ec61Sdh 		    lifr.lifr_zoneid != GLOBAL_ZONEID) {
28457c478bd9Sstevel@tonic-gate 			char zone_name[ZONENAME_MAX];
28467c478bd9Sstevel@tonic-gate 
284745916cd2Sjpk 			if (lifr.lifr_zoneid == ALL_ZONES) {
284845916cd2Sjpk 				(void) printf("\n\tall-zones");
284945916cd2Sjpk 			} else if (getzonenamebyid(lifr.lifr_zoneid, zone_name,
28507c478bd9Sstevel@tonic-gate 			    sizeof (zone_name)) < 0) {
28517c478bd9Sstevel@tonic-gate 				(void) printf("\n\tzone %d", lifr.lifr_zoneid);
28527c478bd9Sstevel@tonic-gate 			} else {
28537c478bd9Sstevel@tonic-gate 				(void) printf("\n\tzone %s", zone_name);
28547c478bd9Sstevel@tonic-gate 			}
28557c478bd9Sstevel@tonic-gate 		}
28567c478bd9Sstevel@tonic-gate 	}
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) {
28597c478bd9Sstevel@tonic-gate 		lifs.lifs_ifindex = lifr.lifr_index;
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 		/*
28627c478bd9Sstevel@tonic-gate 		 * Find the number of interfaces that use this interfaces'
28637c478bd9Sstevel@tonic-gate 		 * address as a source address
28647c478bd9Sstevel@tonic-gate 		 */
28657c478bd9Sstevel@tonic-gate 		lifs.lifs_buf = NULL;
28667c478bd9Sstevel@tonic-gate 		lifs.lifs_maxlen = 0;
28677c478bd9Sstevel@tonic-gate 		for (;;) {
28687c478bd9Sstevel@tonic-gate 			/* The first pass will give the bufsize we need */
28697c478bd9Sstevel@tonic-gate 			rval = ioctl(s, SIOCGLIFSRCOF, (char *)&lifs);
28707c478bd9Sstevel@tonic-gate 			if (rval < 0) {
28717c478bd9Sstevel@tonic-gate 				if (lifs.lifs_buf != NULL) {
28727c478bd9Sstevel@tonic-gate 					free(lifs.lifs_buf);
28737c478bd9Sstevel@tonic-gate 					lifs.lifs_buf = NULL;
28747c478bd9Sstevel@tonic-gate 				}
28757c478bd9Sstevel@tonic-gate 				lifs.lifs_len = 0;
28767c478bd9Sstevel@tonic-gate 				break;
28777c478bd9Sstevel@tonic-gate 			}
28787c478bd9Sstevel@tonic-gate 			if (lifs.lifs_len <= lifs.lifs_maxlen)
28797c478bd9Sstevel@tonic-gate 				break;
28807c478bd9Sstevel@tonic-gate 			/* Use kernel's size + a small margin to avoid loops */
28817c478bd9Sstevel@tonic-gate 			lifs.lifs_maxlen = lifs.lifs_len +
28827c478bd9Sstevel@tonic-gate 			    5 * sizeof (struct lifreq);
28837c478bd9Sstevel@tonic-gate 			/* For the first pass, realloc acts like malloc */
28847c478bd9Sstevel@tonic-gate 			newbuf = realloc(lifs.lifs_buf, lifs.lifs_maxlen);
28857c478bd9Sstevel@tonic-gate 			if (newbuf == NULL) {
28867c478bd9Sstevel@tonic-gate 				if (lifs.lifs_buf != NULL) {
28877c478bd9Sstevel@tonic-gate 					free(lifs.lifs_buf);
28887c478bd9Sstevel@tonic-gate 					lifs.lifs_buf = NULL;
28897c478bd9Sstevel@tonic-gate 				}
28907c478bd9Sstevel@tonic-gate 				lifs.lifs_len = 0;
28917c478bd9Sstevel@tonic-gate 				break;
28927c478bd9Sstevel@tonic-gate 			}
28937c478bd9Sstevel@tonic-gate 			lifs.lifs_buf = newbuf;
28947c478bd9Sstevel@tonic-gate 		}
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate 		numifs = lifs.lifs_len / sizeof (struct lifreq);
28987c478bd9Sstevel@tonic-gate 		if (numifs > 0) {
28997c478bd9Sstevel@tonic-gate 			lifrp = lifs.lifs_req;
29007c478bd9Sstevel@tonic-gate 			(void) printf("\n\tsrcof");
29017c478bd9Sstevel@tonic-gate 			for (n = numifs; n > 0; n--, lifrp++) {
29027c478bd9Sstevel@tonic-gate 				(void) printf(" %s", lifrp->lifr_name);
29037c478bd9Sstevel@tonic-gate 			}
29047c478bd9Sstevel@tonic-gate 		}
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 		if (lifs.lifs_buf != NULL)
29077c478bd9Sstevel@tonic-gate 			free(lifs.lifs_buf);
29087c478bd9Sstevel@tonic-gate 	}
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate 	/* Find the interface whose source address this interface uses */
29117c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
29127c478bd9Sstevel@tonic-gate 		if (lifr.lifr_index != 0) {
29137c478bd9Sstevel@tonic-gate 			if (if_indextoname(lifr.lifr_index,
29147c478bd9Sstevel@tonic-gate 			    if_usesrc_name) == NULL) {
29157c478bd9Sstevel@tonic-gate 				(void) printf("\n\tusesrc ifIndex %d",
29167c478bd9Sstevel@tonic-gate 				    lifr.lifr_index);
29177c478bd9Sstevel@tonic-gate 			} else {
29187c478bd9Sstevel@tonic-gate 				(void) printf("\n\tusesrc %s", if_usesrc_name);
29197c478bd9Sstevel@tonic-gate 			}
29207c478bd9Sstevel@tonic-gate 		}
29217c478bd9Sstevel@tonic-gate 	}
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
29247c478bd9Sstevel@tonic-gate }
29257c478bd9Sstevel@tonic-gate 
29267c478bd9Sstevel@tonic-gate /*
29277c478bd9Sstevel@tonic-gate  * Print the status of the interface.  If an address family was
29287c478bd9Sstevel@tonic-gate  * specified, show it and it only; otherwise, show them all.
29297c478bd9Sstevel@tonic-gate  */
29307c478bd9Sstevel@tonic-gate static void
29317c478bd9Sstevel@tonic-gate status(void)
29327c478bd9Sstevel@tonic-gate {
29332b24ab6bSSebastien Roy 	struct afswtch	*p = afp;
29342b24ab6bSSebastien Roy 	uint64_t	flags;
29352b24ab6bSSebastien Roy 	datalink_id_t	linkid;
29367c478bd9Sstevel@tonic-gate 
29377c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
29387c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
29397c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
29407c478bd9Sstevel@tonic-gate 	}
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
29437c478bd9Sstevel@tonic-gate 
29447c478bd9Sstevel@tonic-gate 	/*
29457c478bd9Sstevel@tonic-gate 	 * Only print the interface status if the address family matches
29467c478bd9Sstevel@tonic-gate 	 * the interface family flag.
29477c478bd9Sstevel@tonic-gate 	 */
29487c478bd9Sstevel@tonic-gate 	if (p != NULL) {
29497c478bd9Sstevel@tonic-gate 		if (((p->af_af == AF_INET6) && (flags & IFF_IPV4)) ||
29507c478bd9Sstevel@tonic-gate 		    ((p->af_af == AF_INET) && (flags & IFF_IPV6)))
29517c478bd9Sstevel@tonic-gate 			return;
29527c478bd9Sstevel@tonic-gate 	}
29537c478bd9Sstevel@tonic-gate 
29547c478bd9Sstevel@tonic-gate 	/*
29557c478bd9Sstevel@tonic-gate 	 * In V4 compatibility mode, don't print IFF_IPV6 interfaces.
29567c478bd9Sstevel@tonic-gate 	 */
29577c478bd9Sstevel@tonic-gate 	if (v4compat && (flags & IFF_IPV6))
29587c478bd9Sstevel@tonic-gate 		return;
29597c478bd9Sstevel@tonic-gate 
29607c478bd9Sstevel@tonic-gate 	ifstatus(name);
29617c478bd9Sstevel@tonic-gate 
29622b24ab6bSSebastien Roy 	if (ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid) ==
29632b24ab6bSSebastien Roy 	    DLADM_STATUS_OK)
29642b24ab6bSSebastien Roy 		tun_status(linkid);
29652b24ab6bSSebastien Roy 
29667c478bd9Sstevel@tonic-gate 	if (p != NULL) {
29677c478bd9Sstevel@tonic-gate 		(*p->af_status)(1, flags);
29687c478bd9Sstevel@tonic-gate 	} else {
29697c478bd9Sstevel@tonic-gate 		for (p = afs; p->af_name; p++) {
29707c478bd9Sstevel@tonic-gate 			/* set global af for use in p->af_status */
29717c478bd9Sstevel@tonic-gate 			af = p->af_af;
29727c478bd9Sstevel@tonic-gate 			(*p->af_status)(0, flags);
29737c478bd9Sstevel@tonic-gate 		}
29747c478bd9Sstevel@tonic-gate 
29757c478bd9Sstevel@tonic-gate 		/*
29767c478bd9Sstevel@tonic-gate 		 * Historically, 'ether' has been an address family,
29777c478bd9Sstevel@tonic-gate 		 * so print it here.
29787c478bd9Sstevel@tonic-gate 		 */
29797c478bd9Sstevel@tonic-gate 		print_ifether(name);
29807c478bd9Sstevel@tonic-gate 	}
29817c478bd9Sstevel@tonic-gate }
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate /*
29847c478bd9Sstevel@tonic-gate  * Print the status of the interface in a format that can be used to
29857c478bd9Sstevel@tonic-gate  * reconfigure the interface later. Code stolen from status() above.
29867c478bd9Sstevel@tonic-gate  */
29877c478bd9Sstevel@tonic-gate /* ARGSUSED */
29887c478bd9Sstevel@tonic-gate static int
29897c478bd9Sstevel@tonic-gate configinfo(char *null, int64_t param)
29907c478bd9Sstevel@tonic-gate {
2991e11c3f44Smeem 	char *cp;
29927c478bd9Sstevel@tonic-gate 	struct afswtch *p = afp;
29937c478bd9Sstevel@tonic-gate 	uint64_t flags;
2994e11c3f44Smeem 	char lifname[LIFNAMSIZ];
29957c478bd9Sstevel@tonic-gate 	char if_usesrc_name[LIFNAMSIZ];
29967c478bd9Sstevel@tonic-gate 
29977c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2998e11c3f44Smeem 
29997c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
30007c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
30017c478bd9Sstevel@tonic-gate 	}
30027c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
30037c478bd9Sstevel@tonic-gate 
30047c478bd9Sstevel@tonic-gate 	if (debug) {
30057c478bd9Sstevel@tonic-gate 		(void) printf("configinfo: name %s flags  0x%llx af_af %d\n",
30067c478bd9Sstevel@tonic-gate 		    name, flags, p != NULL ? p->af_af : -1);
30077c478bd9Sstevel@tonic-gate 	}
30087c478bd9Sstevel@tonic-gate 
3009e11c3f44Smeem 	/*
3010e11c3f44Smeem 	 * Build the interface name to print (we can't directly use `name'
3011e11c3f44Smeem 	 * because one cannot "plumb" ":0" interfaces).
3012e11c3f44Smeem 	 */
3013e11c3f44Smeem 	(void) strlcpy(lifname, name, LIFNAMSIZ);
3014e11c3f44Smeem 	if ((cp = strchr(lifname, ':')) != NULL && atoi(cp + 1) == 0)
3015e11c3f44Smeem 		*cp = '\0';
30167c478bd9Sstevel@tonic-gate 
30177c478bd9Sstevel@tonic-gate 	/*
30187c478bd9Sstevel@tonic-gate 	 * if the interface is IPv4
30197c478bd9Sstevel@tonic-gate 	 *	if we have a IPv6 address family restriction return
30207c478bd9Sstevel@tonic-gate 	 *		so it won't print
30217c478bd9Sstevel@tonic-gate 	 *	if we are in IPv4 compatibility mode, clear out IFF_IPV4
30227c478bd9Sstevel@tonic-gate 	 *		so we don't print it.
30237c478bd9Sstevel@tonic-gate 	 */
30247c478bd9Sstevel@tonic-gate 	if (flags & IFF_IPV4) {
30257c478bd9Sstevel@tonic-gate 		if (p && p->af_af == AF_INET6)
30267c478bd9Sstevel@tonic-gate 			return (-1);
30277c478bd9Sstevel@tonic-gate 		if (v4compat)
30287c478bd9Sstevel@tonic-gate 			flags &= ~IFF_IPV4;
30297c478bd9Sstevel@tonic-gate 
3030e11c3f44Smeem 		(void) printf("%s inet plumb", lifname);
30317c478bd9Sstevel@tonic-gate 	} else if (flags & IFF_IPV6) {
30327c478bd9Sstevel@tonic-gate 		/*
30337c478bd9Sstevel@tonic-gate 		 * else if the interface is IPv6
30347c478bd9Sstevel@tonic-gate 		 *	if we have a IPv4 address family restriction return
30357c478bd9Sstevel@tonic-gate 		 *	or we are in IPv4 compatibiltiy mode, return.
30367c478bd9Sstevel@tonic-gate 		 */
30377c478bd9Sstevel@tonic-gate 		if (p && p->af_af == AF_INET)
30387c478bd9Sstevel@tonic-gate 			return (-1);
30397c478bd9Sstevel@tonic-gate 		if (v4compat)
30407c478bd9Sstevel@tonic-gate 			return (-1);
30417c478bd9Sstevel@tonic-gate 
3042e11c3f44Smeem 		(void) printf("%s inet6 plumb", lifname);
30437c478bd9Sstevel@tonic-gate 	}
30447c478bd9Sstevel@tonic-gate 
30457c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
30467c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
30477c478bd9Sstevel@tonic-gate 		Perror0_exit("configinfo: SIOCGLIFMETRIC");
30487c478bd9Sstevel@tonic-gate 	} else {
30497c478bd9Sstevel@tonic-gate 		if (lifr.lifr_metric)
30507c478bd9Sstevel@tonic-gate 			(void) printf(" metric %d ", lifr.lifr_metric);
30517c478bd9Sstevel@tonic-gate 	}
30527c478bd9Sstevel@tonic-gate 	if (((flags & (IFF_VIRTUAL|IFF_LOOPBACK)) != IFF_VIRTUAL) &&
3053fc80c0dfSnordmark 	    ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
30547c478bd9Sstevel@tonic-gate 		(void) printf(" mtu %d", lifr.lifr_metric);
30557c478bd9Sstevel@tonic-gate 
3056e11c3f44Smeem 	/* Index only applies to the zeroth interface */
3057e11c3f44Smeem 	if (lifnum(name) == 0) {
30587c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
30597c478bd9Sstevel@tonic-gate 			(void) printf(" index %d", lifr.lifr_index);
30607c478bd9Sstevel@tonic-gate 	}
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
30637c478bd9Sstevel@tonic-gate 		if (lifr.lifr_index != 0) {
30647c478bd9Sstevel@tonic-gate 			if (if_indextoname(lifr.lifr_index,
30657c478bd9Sstevel@tonic-gate 			    if_usesrc_name) != NULL) {
30667c478bd9Sstevel@tonic-gate 				(void) printf(" usesrc %s", if_usesrc_name);
30677c478bd9Sstevel@tonic-gate 			}
30687c478bd9Sstevel@tonic-gate 		}
30697c478bd9Sstevel@tonic-gate 	}
30707c478bd9Sstevel@tonic-gate 
30717c478bd9Sstevel@tonic-gate 	if (p != NULL) {
30727c478bd9Sstevel@tonic-gate 		(*p->af_configinfo)(1, flags);
30737c478bd9Sstevel@tonic-gate 	} else {
30747c478bd9Sstevel@tonic-gate 		for (p = afs; p->af_name; p++) {
30757c478bd9Sstevel@tonic-gate 			(void) close(s);
30767c478bd9Sstevel@tonic-gate 			s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0);
30777c478bd9Sstevel@tonic-gate 			/* set global af for use in p->af_configinfo */
30787c478bd9Sstevel@tonic-gate 			af = p->af_af;
30797c478bd9Sstevel@tonic-gate 			if (s == -1) {
30807c478bd9Sstevel@tonic-gate 				Perror0_exit("socket");
30817c478bd9Sstevel@tonic-gate 			}
30827c478bd9Sstevel@tonic-gate 			(*p->af_configinfo)(0, flags);
30837c478bd9Sstevel@tonic-gate 		}
30847c478bd9Sstevel@tonic-gate 	}
30857c478bd9Sstevel@tonic-gate 
30862b24ab6bSSebastien Roy 	(void) putchar('\n');
30877c478bd9Sstevel@tonic-gate 	return (0);
30887c478bd9Sstevel@tonic-gate }
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate static void
30912b24ab6bSSebastien Roy print_tsec(iptun_params_t *params)
30927c478bd9Sstevel@tonic-gate {
30937c478bd9Sstevel@tonic-gate 	ipsec_req_t *ipsr;
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 	(void) printf("\ttunnel security settings  ");
30962b24ab6bSSebastien Roy 	if (!(params->iptun_param_flags & IPTUN_PARAM_SECINFO)) {
30972b24ab6bSSebastien Roy 		(void) printf("-->  use 'ipsecconf -ln -i %s'", name);
30988810c16bSdanmcd 	} else {
30992b24ab6bSSebastien Roy 		ipsr = &params->iptun_param_secinfo;
31008810c16bSdanmcd 		if (ipsr->ipsr_ah_req & IPSEC_PREF_REQUIRED) {
31018810c16bSdanmcd 			(void) printf("ah (%s)  ",
31028810c16bSdanmcd 			    rparsealg(ipsr->ipsr_auth_alg, IPSEC_PROTO_AH));
31038810c16bSdanmcd 		}
31048810c16bSdanmcd 		if (ipsr->ipsr_esp_req & IPSEC_PREF_REQUIRED) {
31058810c16bSdanmcd 			(void) printf("esp (%s",
31068810c16bSdanmcd 			    rparsealg(ipsr->ipsr_esp_alg, IPSEC_PROTO_ESP));
31078810c16bSdanmcd 			(void) printf("/%s)",
31088810c16bSdanmcd 			    rparsealg(ipsr->ipsr_esp_auth_alg, IPSEC_PROTO_AH));
31098810c16bSdanmcd 		}
31107c478bd9Sstevel@tonic-gate 	}
31117c478bd9Sstevel@tonic-gate 	(void) printf("\n");
31127c478bd9Sstevel@tonic-gate }
31137c478bd9Sstevel@tonic-gate 
31147c478bd9Sstevel@tonic-gate static void
31152b24ab6bSSebastien Roy tun_status(datalink_id_t linkid)
31167c478bd9Sstevel@tonic-gate {
31172b24ab6bSSebastien Roy 	iptun_params_t	params;
31182b24ab6bSSebastien Roy 	char		propval[DLADM_PROP_VAL_MAX];
31192b24ab6bSSebastien Roy 	char		*valptr[1];
31202b24ab6bSSebastien Roy 	uint_t		valcnt = 1;
31212b24ab6bSSebastien Roy 	boolean_t	tabbed = _B_FALSE;
31222b24ab6bSSebastien Roy 
31232b24ab6bSSebastien Roy 	params.iptun_param_linkid = linkid;
31242b24ab6bSSebastien Roy 
31252b24ab6bSSebastien Roy 	/* If dladm_iptun_getparams() fails, assume we are not a tunnel. */
31262b24ab6bSSebastien Roy 	assert(dlh_opened);
31272b24ab6bSSebastien Roy 	if (dladm_iptun_getparams(dlh, &params, DLADM_OPT_ACTIVE) !=
31282b24ab6bSSebastien Roy 	    DLADM_STATUS_OK)
31297c478bd9Sstevel@tonic-gate 		return;
31307c478bd9Sstevel@tonic-gate 
31312b24ab6bSSebastien Roy 	switch (params.iptun_param_type) {
31322b24ab6bSSebastien Roy 	case IPTUN_TYPE_IPV4:
31332b24ab6bSSebastien Roy 	case IPTUN_TYPE_6TO4:
31347c478bd9Sstevel@tonic-gate 		(void) printf("\tinet");
31357c478bd9Sstevel@tonic-gate 		break;
31362b24ab6bSSebastien Roy 	case IPTUN_TYPE_IPV6:
31377c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6");
31387c478bd9Sstevel@tonic-gate 		break;
31397c478bd9Sstevel@tonic-gate 	default:
31402b24ab6bSSebastien Roy 		dladmerr_exit(DLADM_STATUS_IPTUNTYPE, name);
31417c478bd9Sstevel@tonic-gate 		break;
31427c478bd9Sstevel@tonic-gate 	}
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate 	/*
31452b24ab6bSSebastien Roy 	 * There is always a source address.  If it hasn't been explicitly
31462b24ab6bSSebastien Roy 	 * set, the API will pass back a buffer containing the unspecified
31472b24ab6bSSebastien Roy 	 * address.
31487c478bd9Sstevel@tonic-gate 	 */
31492b24ab6bSSebastien Roy 	(void) printf(" tunnel src %s ", params.iptun_param_laddr);
31502b24ab6bSSebastien Roy 
31512b24ab6bSSebastien Roy 	if (params.iptun_param_flags & IPTUN_PARAM_RADDR)
31522b24ab6bSSebastien Roy 		(void) printf("tunnel dst %s\n", params.iptun_param_raddr);
31532b24ab6bSSebastien Roy 	else
31542b24ab6bSSebastien Roy 		(void) putchar('\n');
31552b24ab6bSSebastien Roy 
31562b24ab6bSSebastien Roy 	if (params.iptun_param_flags & IPTUN_PARAM_IPSECPOL)
31572b24ab6bSSebastien Roy 		print_tsec(&params);
31587c478bd9Sstevel@tonic-gate 
31592b24ab6bSSebastien Roy 	valptr[0] = propval;
31602b24ab6bSSebastien Roy 	if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT, "hoplimit",
31612b24ab6bSSebastien Roy 	    (char **)valptr, &valcnt) == DLADM_STATUS_OK) {
31622b24ab6bSSebastien Roy 		(void) printf("\ttunnel hop limit %s ", propval);
31637c478bd9Sstevel@tonic-gate 		tabbed = _B_TRUE;
31647c478bd9Sstevel@tonic-gate 	}
31657c478bd9Sstevel@tonic-gate 
31662b24ab6bSSebastien Roy 	if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT,
31672b24ab6bSSebastien Roy 	    "encaplimit", (char **)valptr, &valcnt) == DLADM_STATUS_OK) {
31682b24ab6bSSebastien Roy 		uint32_t elim;
31692b24ab6bSSebastien Roy 
31707c478bd9Sstevel@tonic-gate 		if (!tabbed) {
31712b24ab6bSSebastien Roy 			(void) putchar('\t');
31727c478bd9Sstevel@tonic-gate 			tabbed = _B_TRUE;
31737c478bd9Sstevel@tonic-gate 		}
31742b24ab6bSSebastien Roy 		elim = strtol(propval, NULL, 10);
31752b24ab6bSSebastien Roy 		if (elim > 0)
31762b24ab6bSSebastien Roy 			(void) printf("tunnel encapsulation limit %s", propval);
31772b24ab6bSSebastien Roy 		else
31787c478bd9Sstevel@tonic-gate 			(void) printf("tunnel encapsulation limit disabled");
31797c478bd9Sstevel@tonic-gate 	}
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 	if (tabbed)
31822b24ab6bSSebastien Roy 		(void) putchar('\n');
31837c478bd9Sstevel@tonic-gate }
31847c478bd9Sstevel@tonic-gate 
31857c478bd9Sstevel@tonic-gate static void
31867c478bd9Sstevel@tonic-gate in_status(int force, uint64_t flags)
31877c478bd9Sstevel@tonic-gate {
31882b24ab6bSSebastien Roy 	struct sockaddr_in	*sin, *laddr;
31892b24ab6bSSebastien Roy 	struct sockaddr_in	netmask = { AF_INET };
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate 	if (debug)
31927c478bd9Sstevel@tonic-gate 		(void) printf("in_status(%s) flags 0x%llx\n", name, flags);
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate 	/* only print status for IPv4 interfaces */
31957c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV4))
31967c478bd9Sstevel@tonic-gate 		return;
31977c478bd9Sstevel@tonic-gate 
31987c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
31997c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32007c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
32017c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
32027c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
32037c478bd9Sstevel@tonic-gate 				if (!force)
32047c478bd9Sstevel@tonic-gate 					return;
32057c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
32067c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
32077c478bd9Sstevel@tonic-gate 			} else
32087c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status: SIOCGLIFADDR");
32097c478bd9Sstevel@tonic-gate 		}
32107c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
32117c478bd9Sstevel@tonic-gate 		(void) printf("\tinet %s ", inet_ntoa(sin->sin_addr));
32127c478bd9Sstevel@tonic-gate 		laddr = sin;
32137c478bd9Sstevel@tonic-gate 	} else {
32147c478bd9Sstevel@tonic-gate 		(void) printf("\tinet ");
32157c478bd9Sstevel@tonic-gate 	}
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32187c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
32197c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
32207c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
32217c478bd9Sstevel@tonic-gate 			if (!force)
32227c478bd9Sstevel@tonic-gate 				return;
32237c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
32247c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
32257c478bd9Sstevel@tonic-gate 		} else {
32267c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status: SIOCGLIFSUBNET");
32277c478bd9Sstevel@tonic-gate 		}
32287c478bd9Sstevel@tonic-gate 	}
32297c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
32307c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
32317c478bd9Sstevel@tonic-gate 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
32327c478bd9Sstevel@tonic-gate 		(void) printf("subnet %s/%d ", inet_ntoa(sin->sin_addr),
32337c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
32347c478bd9Sstevel@tonic-gate 	}
32357c478bd9Sstevel@tonic-gate 	if (sin->sin_family != AF_INET) {
32367c478bd9Sstevel@tonic-gate 		(void) printf("Wrong family: %d\n", sin->sin_family);
32377c478bd9Sstevel@tonic-gate 	}
32387c478bd9Sstevel@tonic-gate 
32397c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32407c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
32417c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL)
32427c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status: SIOCGLIFNETMASK");
32437c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
32447c478bd9Sstevel@tonic-gate 	} else
32457c478bd9Sstevel@tonic-gate 		netmask.sin_addr =
32467c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
32477c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
32487c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32497c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
32507c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
32517c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
32527c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
32537c478bd9Sstevel@tonic-gate 			else
3254fc80c0dfSnordmark 				Perror0_exit("in_status: SIOCGLIFDSTADDR");
32557c478bd9Sstevel@tonic-gate 		}
32567c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
32577c478bd9Sstevel@tonic-gate 		(void) printf("--> %s ", inet_ntoa(sin->sin_addr));
32587c478bd9Sstevel@tonic-gate 	}
32597c478bd9Sstevel@tonic-gate 	(void) printf("netmask %x ", ntohl(netmask.sin_addr.s_addr));
32607c478bd9Sstevel@tonic-gate 	if (flags & IFF_BROADCAST) {
32617c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32627c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
32637c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
32647c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
32657c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
32667c478bd9Sstevel@tonic-gate 			else
3267fc80c0dfSnordmark 				Perror0_exit("in_status: SIOCGLIFBRDADDR");
32687c478bd9Sstevel@tonic-gate 		}
32697c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
32707c478bd9Sstevel@tonic-gate 		if (sin->sin_addr.s_addr != 0) {
32717c478bd9Sstevel@tonic-gate 			(void) printf("broadcast %s",
32727c478bd9Sstevel@tonic-gate 			    inet_ntoa(sin->sin_addr));
32737c478bd9Sstevel@tonic-gate 		}
32747c478bd9Sstevel@tonic-gate 	}
3275e11c3f44Smeem 	/* If there is a groupname, print it for only the physical interface */
32767c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') == NULL) {
3277e11c3f44Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3278e11c3f44Smeem 		    lifr.lifr_groupname[0] != '\0') {
3279e11c3f44Smeem 			(void) printf("\n\tgroupname %s", lifr.lifr_groupname);
32807c478bd9Sstevel@tonic-gate 		}
32817c478bd9Sstevel@tonic-gate 	}
32827c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
32837c478bd9Sstevel@tonic-gate }
32847c478bd9Sstevel@tonic-gate 
32857c478bd9Sstevel@tonic-gate static void
32867c478bd9Sstevel@tonic-gate in6_status(int force, uint64_t flags)
32877c478bd9Sstevel@tonic-gate {
32882b24ab6bSSebastien Roy 	char			abuf[INET6_ADDRSTRLEN];
32892b24ab6bSSebastien Roy 	struct sockaddr_in6	*sin6, *laddr6;
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate 	if (debug)
32927c478bd9Sstevel@tonic-gate 		(void) printf("in6_status(%s) flags 0x%llx\n", name, flags);
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV6))
32957c478bd9Sstevel@tonic-gate 		return;
32967c478bd9Sstevel@tonic-gate 
32977c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
32987c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32997c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
33007c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
33017c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
33027c478bd9Sstevel@tonic-gate 				if (!force)
33037c478bd9Sstevel@tonic-gate 					return;
33047c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33057c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33067c478bd9Sstevel@tonic-gate 			} else
33077c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status6: SIOCGLIFADDR");
33087c478bd9Sstevel@tonic-gate 		}
33097c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
33107c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6 %s/%d ",
33117c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33127c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
33137c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
33147c478bd9Sstevel@tonic-gate 		laddr6 = sin6;
33157c478bd9Sstevel@tonic-gate 	} else {
33167c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6 ");
33177c478bd9Sstevel@tonic-gate 	}
33187c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33197c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
33207c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
33217c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
33227c478bd9Sstevel@tonic-gate 			if (!force)
33237c478bd9Sstevel@tonic-gate 				return;
33247c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
33257c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
33267c478bd9Sstevel@tonic-gate 		} else
33277c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status6: SIOCGLIFSUBNET");
33287c478bd9Sstevel@tonic-gate 	}
33297c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
33307c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
33317c478bd9Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
33327c478bd9Sstevel@tonic-gate 		(void) printf("subnet %s/%d ",
33337c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33347c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
33357c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
33367c478bd9Sstevel@tonic-gate 	}
33377c478bd9Sstevel@tonic-gate 	if (sin6->sin6_family != AF_INET6) {
33387c478bd9Sstevel@tonic-gate 		(void) printf("Wrong family: %d\n", sin6->sin6_family);
33397c478bd9Sstevel@tonic-gate 	}
33407c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
33417c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33427c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
33437c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
33447c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33457c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33467c478bd9Sstevel@tonic-gate 			else
3347fc80c0dfSnordmark 				Perror0_exit("in_status6: SIOCGLIFDSTADDR");
33487c478bd9Sstevel@tonic-gate 		}
33497c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
33507c478bd9Sstevel@tonic-gate 		(void) printf("--> %s ",
33517c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33527c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)));
33537c478bd9Sstevel@tonic-gate 	}
33547c478bd9Sstevel@tonic-gate 	if (verbose) {
33557c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
33567c478bd9Sstevel@tonic-gate 		(void) putchar('\t');
33577c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33587c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
33597c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EINVAL)
33607c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33617c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33627c478bd9Sstevel@tonic-gate 			else
3363fc80c0dfSnordmark 				Perror0_exit("in_status6: SIOCGLIFTOKEN");
33647c478bd9Sstevel@tonic-gate 		} else {
33657c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
33667c478bd9Sstevel@tonic-gate 			(void) printf("token %s/%d ",
33677c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33687c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
33697c478bd9Sstevel@tonic-gate 			    lifr.lifr_addrlen);
33707c478bd9Sstevel@tonic-gate 		}
33717c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFLNKINFO, (caddr_t)&lifr) < 0) {
33727c478bd9Sstevel@tonic-gate 			if (errno != EINVAL) {
33737c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status6: SIOCGLIFLNKINFO");
33747c478bd9Sstevel@tonic-gate 			}
33757c478bd9Sstevel@tonic-gate 		} else {
33767c478bd9Sstevel@tonic-gate 			(void) printf("maxhops %u, reachtime %u ms, "
33777c478bd9Sstevel@tonic-gate 			    "reachretrans %u ms, maxmtu %u ",
33787c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_maxhops,
33797c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_reachtime,
33807c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_reachretrans,
33817c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_maxmtu);
33827c478bd9Sstevel@tonic-gate 		}
33837c478bd9Sstevel@tonic-gate 	}
3384f7d61273Smeem 	/* If there is a groupname, print it for only the physical interface */
33857c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') == NULL) {
3386f7d61273Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3387f7d61273Smeem 		    lifr.lifr_groupname[0] != '\0') {
3388f7d61273Smeem 			(void) printf("\n\tgroupname %s", lifr.lifr_groupname);
33897c478bd9Sstevel@tonic-gate 		}
33907c478bd9Sstevel@tonic-gate 	}
33917c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
33927c478bd9Sstevel@tonic-gate }
33937c478bd9Sstevel@tonic-gate 
33947c478bd9Sstevel@tonic-gate static void
33957c478bd9Sstevel@tonic-gate in_configinfo(int force, uint64_t flags)
33967c478bd9Sstevel@tonic-gate {
33977c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin, *laddr;
33987c478bd9Sstevel@tonic-gate 	struct	sockaddr_in netmask = { AF_INET };
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate 	if (debug)
34017c478bd9Sstevel@tonic-gate 		(void) printf("in_configinfo(%s) flags 0x%llx\n", name, flags);
34027c478bd9Sstevel@tonic-gate 
34037c478bd9Sstevel@tonic-gate 	/* only configinfo info for IPv4 interfaces */
34047c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV4))
34057c478bd9Sstevel@tonic-gate 		return;
34067c478bd9Sstevel@tonic-gate 
34077c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
34087c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34097c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
34107c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
34117c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
34127c478bd9Sstevel@tonic-gate 				if (!force)
34137c478bd9Sstevel@tonic-gate 					return;
34147c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34157c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34167c478bd9Sstevel@tonic-gate 			} else
34177c478bd9Sstevel@tonic-gate 				Perror0_exit("in_configinfo: SIOCGLIFADDR");
34187c478bd9Sstevel@tonic-gate 		}
34197c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3420e11c3f44Smeem 		(void) printf(" set %s ", inet_ntoa(sin->sin_addr));
34217c478bd9Sstevel@tonic-gate 		laddr = sin;
34227c478bd9Sstevel@tonic-gate 	}
34237c478bd9Sstevel@tonic-gate 
34247c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34257c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
34267c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
34277c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
34287c478bd9Sstevel@tonic-gate 			if (!force)
34297c478bd9Sstevel@tonic-gate 				return;
34307c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
34317c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
34327c478bd9Sstevel@tonic-gate 		} else {
34337c478bd9Sstevel@tonic-gate 			Perror0_exit("in_configinfo: SIOCGLIFSUBNET");
34347c478bd9Sstevel@tonic-gate 		}
34357c478bd9Sstevel@tonic-gate 	}
34367c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
34397c478bd9Sstevel@tonic-gate 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
34407c478bd9Sstevel@tonic-gate 		(void) printf(" subnet %s/%d ", inet_ntoa(sin->sin_addr),
34417c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
34427c478bd9Sstevel@tonic-gate 	}
34437c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34447c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
34457c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL)
34467c478bd9Sstevel@tonic-gate 			Perror0_exit("in_configinfo: SIOCGLIFNETMASK");
34477c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
34487c478bd9Sstevel@tonic-gate 	} else
34497c478bd9Sstevel@tonic-gate 		netmask.sin_addr =
34507c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
34517c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
34527c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34537c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
34547c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
34557c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34567c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34577c478bd9Sstevel@tonic-gate 			else
3458fc80c0dfSnordmark 				Perror0_exit("in_configinfo: SIOCGLIFDSTADDR");
34597c478bd9Sstevel@tonic-gate 		}
34607c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
34617c478bd9Sstevel@tonic-gate 		(void) printf(" destination %s ", inet_ntoa(sin->sin_addr));
34627c478bd9Sstevel@tonic-gate 	}
34637c478bd9Sstevel@tonic-gate 	(void) printf(" netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
34647c478bd9Sstevel@tonic-gate 	if (flags & IFF_BROADCAST) {
34657c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34667c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
34677c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
34687c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34697c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34707c478bd9Sstevel@tonic-gate 			else
3471fc80c0dfSnordmark 				Perror0_exit("in_configinfo: SIOCGLIFBRDADDR");
34727c478bd9Sstevel@tonic-gate 		}
34737c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
34747c478bd9Sstevel@tonic-gate 		if (sin->sin_addr.s_addr != 0) {
34757c478bd9Sstevel@tonic-gate 			(void) printf(" broadcast %s ",
34767c478bd9Sstevel@tonic-gate 			    inet_ntoa(sin->sin_addr));
34777c478bd9Sstevel@tonic-gate 		}
34787c478bd9Sstevel@tonic-gate 	}
34797c478bd9Sstevel@tonic-gate 
3480e11c3f44Smeem 	/* If there is a groupname, print it for only the zeroth interface */
3481e11c3f44Smeem 	if (lifnum(name) == 0) {
3482f7d61273Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3483f7d61273Smeem 		    lifr.lifr_groupname[0] != '\0') {
3484f7d61273Smeem 			(void) printf(" group %s ", lifr.lifr_groupname);
34857c478bd9Sstevel@tonic-gate 		}
34867c478bd9Sstevel@tonic-gate 	}
34877c478bd9Sstevel@tonic-gate 
34887c478bd9Sstevel@tonic-gate 	/* Print flags to configure */
3489e11c3f44Smeem 	print_config_flags(AF_INET, flags);
34907c478bd9Sstevel@tonic-gate }
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate static void
34937c478bd9Sstevel@tonic-gate in6_configinfo(int force, uint64_t flags)
34947c478bd9Sstevel@tonic-gate {
34957c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
34967c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6, *laddr6;
34977c478bd9Sstevel@tonic-gate 
34987c478bd9Sstevel@tonic-gate 	if (debug)
34997c478bd9Sstevel@tonic-gate 		(void) printf("in6_configinfo(%s) flags 0x%llx\n", name,
35007c478bd9Sstevel@tonic-gate 		    flags);
35017c478bd9Sstevel@tonic-gate 
35027c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV6))
35037c478bd9Sstevel@tonic-gate 		return;
35047c478bd9Sstevel@tonic-gate 
35057c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
35067c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35077c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
35087c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
35097c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
35107c478bd9Sstevel@tonic-gate 				if (!force)
35117c478bd9Sstevel@tonic-gate 					return;
35127c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
35137c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
35147c478bd9Sstevel@tonic-gate 			} else
35157c478bd9Sstevel@tonic-gate 				Perror0_exit("in6_configinfo: SIOCGLIFADDR");
35167c478bd9Sstevel@tonic-gate 		}
35177c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3518e11c3f44Smeem 		(void) printf(" set %s/%d ",
3519e11c3f44Smeem 		    inet_ntop(AF_INET6, &sin6->sin6_addr, abuf, sizeof (abuf)),
3520e11c3f44Smeem 		    lifr.lifr_addrlen);
35217c478bd9Sstevel@tonic-gate 		laddr6 = sin6;
35227c478bd9Sstevel@tonic-gate 	}
35237c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35247c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
35257c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
35267c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
35277c478bd9Sstevel@tonic-gate 			if (!force)
35287c478bd9Sstevel@tonic-gate 				return;
35297c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
35307c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
35317c478bd9Sstevel@tonic-gate 		} else
35327c478bd9Sstevel@tonic-gate 			Perror0_exit("in6_configinfo: SIOCGLIFSUBNET");
35337c478bd9Sstevel@tonic-gate 	}
35347c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
35357c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
35367c478bd9Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
35377c478bd9Sstevel@tonic-gate 		(void) printf(" subnet %s/%d ",
35387c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35397c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
35407c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
35417c478bd9Sstevel@tonic-gate 	}
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
35447c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35457c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
35467c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
35477c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
35487c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
35497c478bd9Sstevel@tonic-gate 			else
3550fc80c0dfSnordmark 				Perror0_exit("in6_configinfo: SIOCGLIFDSTADDR");
35517c478bd9Sstevel@tonic-gate 		}
35527c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
35537c478bd9Sstevel@tonic-gate 		(void) printf(" destination %s ",
35547c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35557c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)));
35567c478bd9Sstevel@tonic-gate 	}
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35597c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
35607c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EINVAL)
35617c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
35627c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
35637c478bd9Sstevel@tonic-gate 		else
3564fc80c0dfSnordmark 			Perror0_exit("in6_configinfo: SIOCGLIFTOKEN");
35657c478bd9Sstevel@tonic-gate 	} else {
35667c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
35677c478bd9Sstevel@tonic-gate 		(void) printf(" token %s/%d ",
35687c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35697c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
35707c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
35717c478bd9Sstevel@tonic-gate 	}
35727c478bd9Sstevel@tonic-gate 
3573e11c3f44Smeem 	/* If there is a groupname, print it for only the zeroth interface */
3574e11c3f44Smeem 	if (lifnum(name) == 0) {
3575f7d61273Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3576f7d61273Smeem 		    lifr.lifr_groupname[0] != '\0') {
3577f7d61273Smeem 			(void) printf(" group %s ", lifr.lifr_groupname);
35787c478bd9Sstevel@tonic-gate 		}
35797c478bd9Sstevel@tonic-gate 	}
35807c478bd9Sstevel@tonic-gate 
35817c478bd9Sstevel@tonic-gate 	/* Print flags to configure */
3582e11c3f44Smeem 	print_config_flags(AF_INET6, flags);
35837c478bd9Sstevel@tonic-gate }
35847c478bd9Sstevel@tonic-gate 
35857c478bd9Sstevel@tonic-gate /*
35867c478bd9Sstevel@tonic-gate  * If this is a physical interface then remove it.
35877c478bd9Sstevel@tonic-gate  * If it is a logical interface name use SIOCLIFREMOVEIF to
35887c478bd9Sstevel@tonic-gate  * remove it. In both cases fail if it doesn't exist.
35897c478bd9Sstevel@tonic-gate  */
35907c478bd9Sstevel@tonic-gate /* ARGSUSED */
35917c478bd9Sstevel@tonic-gate static int
35927c478bd9Sstevel@tonic-gate inetunplumb(char *arg, int64_t param)
35937c478bd9Sstevel@tonic-gate {
35946e91bba0SGirish Moodalbail 	ipadm_status_t	istatus;
3595e11c3f44Smeem 
35966e91bba0SGirish Moodalbail 	istatus = ipadm_delete_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE);
35976e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS) {
35986e91bba0SGirish Moodalbail 		(void) fprintf(stderr, "ifconfig: cannot unplumb %s: %s\n",
35996e91bba0SGirish Moodalbail 		    name, ipadm_status2str(istatus));
36006e91bba0SGirish Moodalbail 		exit(1);
36017c478bd9Sstevel@tonic-gate 	}
36027c478bd9Sstevel@tonic-gate 
36037c478bd9Sstevel@tonic-gate 	return (0);
36047c478bd9Sstevel@tonic-gate }
36057c478bd9Sstevel@tonic-gate 
36067c478bd9Sstevel@tonic-gate /*
36076e91bba0SGirish Moodalbail  * Create the interface in `name', using ipadm_create_if(). If `name' is a
36086e91bba0SGirish Moodalbail  * logical interface or loopback interface, ipadm_create_if() uses
36096e91bba0SGirish Moodalbail  * SIOCLIFADDIF to create it.
36107c478bd9Sstevel@tonic-gate  */
36117c478bd9Sstevel@tonic-gate /* ARGSUSED */
36127c478bd9Sstevel@tonic-gate static int
36137c478bd9Sstevel@tonic-gate inetplumb(char *arg, int64_t param)
36147c478bd9Sstevel@tonic-gate {
36156e91bba0SGirish Moodalbail 	ipadm_status_t	istatus;
36167c478bd9Sstevel@tonic-gate 
36176e91bba0SGirish Moodalbail 	istatus = ipadm_create_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE);
36186e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS) {
36196e91bba0SGirish Moodalbail 		(void) fprintf(stderr, "ifconfig: cannot plumb %s: %s\n",
36206e91bba0SGirish Moodalbail 		    name, ipadm_status2str(istatus));
36216e91bba0SGirish Moodalbail 		if (istatus != IPADM_IF_EXISTS)
36226e91bba0SGirish Moodalbail 			exit(1);
3623f4b3ec61Sdh 	}
3624e11c3f44Smeem 	return (0);
3625e11c3f44Smeem }
3626e11c3f44Smeem 
3627e11c3f44Smeem /* ARGSUSED */
3628e11c3f44Smeem static int
3629e11c3f44Smeem inetipmp(char *arg, int64_t param)
3630e11c3f44Smeem {
3631e11c3f44Smeem 	int retval;
3632e11c3f44Smeem 
3633e11c3f44Smeem 	/*
3634e11c3f44Smeem 	 * Treat e.g. "ifconfig ipmp0:2 ipmp" as "ifconfig ipmp0:2 plumb".
3635e11c3f44Smeem 	 * Otherwise, try to create the requested IPMP interface.
3636e11c3f44Smeem 	 */
3637e11c3f44Smeem 	if (strchr(name, ':') != NULL)
3638e11c3f44Smeem 		retval = inetplumb(arg, param);
3639e11c3f44Smeem 	else
3640e11c3f44Smeem 		retval = create_ipmp(name, afp->af_af, name, _B_FALSE);
3641e11c3f44Smeem 
3642e11c3f44Smeem 	/*
3643e11c3f44Smeem 	 * We'd return -1, but foreachinterface() doesn't propagate the error
3644e11c3f44Smeem 	 * into the exit status, so we're forced to explicitly exit().
3645e11c3f44Smeem 	 */
3646e11c3f44Smeem 	if (retval == -1)
3647e11c3f44Smeem 		exit(1);
36487c478bd9Sstevel@tonic-gate 	return (0);
36497c478bd9Sstevel@tonic-gate }
36507c478bd9Sstevel@tonic-gate 
3651e11c3f44Smeem /*
3652e11c3f44Smeem  * Create an IPMP group `grname' with address family `af'.  If `ifname' is
3653e11c3f44Smeem  * non-NULL, it specifies the interface name to use.  Otherwise, use the name
3654e11c3f44Smeem  * ipmpN, where N corresponds to the lowest available integer.  If `implicit'
3655e11c3f44Smeem  * is set, then the group is being created as a side-effect of placing an
3656e11c3f44Smeem  * underlying interface in a group.  Also start in.mpathd if necessary.
3657e11c3f44Smeem  */
3658e11c3f44Smeem static int
3659e11c3f44Smeem create_ipmp(const char *grname, int af, const char *ifname, boolean_t implicit)
3660e11c3f44Smeem {
3661e11c3f44Smeem 	static int ipmp_daemon_started;
36626e91bba0SGirish Moodalbail 	uint32_t flags = IPADM_OPT_IPMP|IPADM_OPT_ACTIVE;
36636e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
3664e11c3f44Smeem 
3665e11c3f44Smeem 	if (debug) {
3666e11c3f44Smeem 		(void) printf("create_ipmp: ifname %s grname %s af %d\n",
3667e11c3f44Smeem 		    ifname != NULL ? ifname : "NULL", grname, af);
3668e11c3f44Smeem 	}
3669e11c3f44Smeem 
36706e91bba0SGirish Moodalbail 	/*
36716e91bba0SGirish Moodalbail 	 * ipadm_create_if() creates the IPMP interface and fills in the
36726e91bba0SGirish Moodalbail 	 * ppa in lifr.lifr_name, if `ifname'="ipmp".
36736e91bba0SGirish Moodalbail 	 */
36746e91bba0SGirish Moodalbail 	(void) strlcpy(lifr.lifr_name, (ifname ? ifname : "ipmp"),
36756e91bba0SGirish Moodalbail 	    sizeof (lifr.lifr_name));
36766e91bba0SGirish Moodalbail 	if (ifname == NULL)
36776e91bba0SGirish Moodalbail 		flags |= IPADM_OPT_GENPPA;
36786e91bba0SGirish Moodalbail 	istatus = ipadm_create_if(iph, lifr.lifr_name, af, flags);
36796e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS) {
36806e91bba0SGirish Moodalbail 		(void) fprintf(stderr, "ifconfig: cannot create IPMP interface "
36816e91bba0SGirish Moodalbail 		    "%s: %s\n", grname, ipadm_status2str(istatus));
3682e11c3f44Smeem 		return (-1);
3683e11c3f44Smeem 	}
3684e11c3f44Smeem 
3685e11c3f44Smeem 	/*
3686e11c3f44Smeem 	 * To preserve backward-compatibility, always bring up the link-local
3687e11c3f44Smeem 	 * address for implicitly-created IPv6 IPMP interfaces.
3688e11c3f44Smeem 	 */
3689e11c3f44Smeem 	if (implicit && af == AF_INET6) {
3690e11c3f44Smeem 		if (ioctl(s6, SIOCGLIFFLAGS, &lifr) == 0) {
3691e11c3f44Smeem 			lifr.lifr_flags |= IFF_UP;
3692e11c3f44Smeem 			(void) ioctl(s6, SIOCSLIFFLAGS, &lifr);
3693e11c3f44Smeem 		}
3694e11c3f44Smeem 	}
3695e11c3f44Smeem 
3696e11c3f44Smeem 	/*
3697e11c3f44Smeem 	 * If the caller requested a different group name, issue a
3698e11c3f44Smeem 	 * SIOCSLIFGROUPNAME on the new IPMP interface.
3699e11c3f44Smeem 	 */
3700e11c3f44Smeem 	if (strcmp(lifr.lifr_name, grname) != 0) {
3701e11c3f44Smeem 		(void) strlcpy(lifr.lifr_groupname, grname, LIFGRNAMSIZ);
3702e11c3f44Smeem 		if (ioctl(s, SIOCSLIFGROUPNAME, &lifr) == -1) {
3703e11c3f44Smeem 			Perror0("SIOCSLIFGROUPNAME");
3704e11c3f44Smeem 			return (-1);
3705e11c3f44Smeem 		}
3706e11c3f44Smeem 	}
3707e11c3f44Smeem 
3708e11c3f44Smeem 	/*
3709e11c3f44Smeem 	 * If we haven't done so yet, ensure in.mpathd is started.
3710e11c3f44Smeem 	 */
3711e11c3f44Smeem 	if (ipmp_daemon_started++ == 0)
3712e11c3f44Smeem 		start_ipmp_daemon();
3713e11c3f44Smeem 
3714e11c3f44Smeem 	return (0);
3715e11c3f44Smeem }
3716e11c3f44Smeem 
3717e11c3f44Smeem /*
3718e11c3f44Smeem  * Start in.mpathd if it's not already running.
3719e11c3f44Smeem  */
3720e11c3f44Smeem static void
3721e11c3f44Smeem start_ipmp_daemon(void)
3722e11c3f44Smeem {
3723e11c3f44Smeem 	int retval;
3724e11c3f44Smeem 	ipmp_handle_t ipmp_handle;
3725e11c3f44Smeem 
3726e11c3f44Smeem 	/*
3727e11c3f44Smeem 	 * Ping in.mpathd to see if it's running already.
3728e11c3f44Smeem 	 */
3729e11c3f44Smeem 	if ((retval = ipmp_open(&ipmp_handle)) != IPMP_SUCCESS) {
3730e11c3f44Smeem 		(void) fprintf(stderr, "ifconfig: cannot create IPMP handle: "
3731e11c3f44Smeem 		    "%s\n", ipmp_errmsg(retval));
3732e11c3f44Smeem 		return;
3733e11c3f44Smeem 	}
3734e11c3f44Smeem 
3735e11c3f44Smeem 	retval = ipmp_ping_daemon(ipmp_handle);
3736e11c3f44Smeem 	ipmp_close(ipmp_handle);
3737e11c3f44Smeem 
3738e11c3f44Smeem 	switch (retval) {
3739e11c3f44Smeem 	case IPMP_ENOMPATHD:
3740e11c3f44Smeem 		break;
3741e11c3f44Smeem 	case IPMP_SUCCESS:
3742e11c3f44Smeem 		return;
3743e11c3f44Smeem 	default:
3744e11c3f44Smeem 		(void) fprintf(stderr, "ifconfig: cannot ping in.mpathd: %s\n",
3745e11c3f44Smeem 		    ipmp_errmsg(retval));
3746e11c3f44Smeem 		break;
3747e11c3f44Smeem 	}
3748e11c3f44Smeem 
3749e11c3f44Smeem 	/*
3750e11c3f44Smeem 	 * Start in.mpathd.  Note that in.mpathd will handle multiple
3751e11c3f44Smeem 	 * incarnations (ipmp_ping_daemon() is just an optimization) so we
3752e11c3f44Smeem 	 * don't need to worry about racing with another ifconfig process.
3753e11c3f44Smeem 	 */
3754e11c3f44Smeem 	switch (fork()) {
3755e11c3f44Smeem 	case -1:
3756e11c3f44Smeem 		Perror0_exit("start_ipmp_daemon: fork");
3757e11c3f44Smeem 		/* NOTREACHED */
3758e11c3f44Smeem 	case 0:
3759e11c3f44Smeem 		(void) execl(MPATHD_PATH, MPATHD_PATH, NULL);
3760e11c3f44Smeem 		_exit(1);
3761e11c3f44Smeem 		/* NOTREACHED */
3762e11c3f44Smeem 	default:
3763e11c3f44Smeem 		break;
3764e11c3f44Smeem 	}
3765e11c3f44Smeem }
3766e11c3f44Smeem 
3767e11c3f44Smeem /*
3768e11c3f44Smeem  * Bring the address named by `ifaddrp' up or down.  Doesn't trust any mutable
3769e11c3f44Smeem  * values in ia_flags since they may be stale.
3770e11c3f44Smeem  */
3771e11c3f44Smeem static boolean_t
3772e11c3f44Smeem ifaddr_op(ifaddrlistx_t *ifaddrp, boolean_t up)
3773e11c3f44Smeem {
3774e11c3f44Smeem 	struct lifreq	lifrl;	/* Local lifreq struct */
3775e11c3f44Smeem 	int		fd = (ifaddrp->ia_flags & IFF_IPV4) ? s4 : s6;
3776e11c3f44Smeem 
3777e11c3f44Smeem 	(void) memset(&lifrl, 0, sizeof (lifrl));
3778e11c3f44Smeem 	(void) strlcpy(lifrl.lifr_name, ifaddrp->ia_name, LIFNAMSIZ);
3779e11c3f44Smeem 	if (ioctl(fd, SIOCGLIFFLAGS, &lifrl) == -1)
3780e11c3f44Smeem 		return (_B_FALSE);
3781e11c3f44Smeem 
3782614f1612Smeem 	if (up)
3783e11c3f44Smeem 		lifrl.lifr_flags |= IFF_UP;
3784614f1612Smeem 	else
3785e11c3f44Smeem 		lifrl.lifr_flags &= ~IFF_UP;
3786614f1612Smeem 
3787614f1612Smeem 	if (ioctl(fd, SIOCSLIFFLAGS, &lifrl) == -1)
3788614f1612Smeem 		return (_B_FALSE);
3789614f1612Smeem 
3790614f1612Smeem 	/*
3791614f1612Smeem 	 * If we're trying to bring the address down, ensure that DAD activity
3792614f1612Smeem 	 * (observable by IFF_DUPLICATE) has also been stopped.
3793614f1612Smeem 	 */
3794614f1612Smeem 	if (!up && ioctl(fd, SIOCGLIFFLAGS, &lifrl) != -1 &&
3795614f1612Smeem 	    lifrl.lifr_flags & IFF_DUPLICATE) {
3796614f1612Smeem 		if (ioctl(fd, SIOCGLIFADDR, &lifrl) == -1 ||
3797614f1612Smeem 		    ioctl(fd, SIOCSLIFADDR, &lifrl) == -1) {
3798614f1612Smeem 			return (_B_FALSE);
3799614f1612Smeem 		}
3800e11c3f44Smeem 	}
3801614f1612Smeem 	return (_B_TRUE);
3802e11c3f44Smeem }
3803e11c3f44Smeem 
3804e11c3f44Smeem static boolean_t
3805e11c3f44Smeem ifaddr_up(ifaddrlistx_t *ifaddrp)
3806e11c3f44Smeem {
3807e11c3f44Smeem 	return (ifaddr_op(ifaddrp, _B_TRUE));
3808e11c3f44Smeem }
3809e11c3f44Smeem 
3810e11c3f44Smeem static boolean_t
3811e11c3f44Smeem ifaddr_down(ifaddrlistx_t *ifaddrp)
3812e11c3f44Smeem {
3813e11c3f44Smeem 	return (ifaddr_op(ifaddrp, _B_FALSE));
3814e11c3f44Smeem }
3815e11c3f44Smeem 
38162b24ab6bSSebastien Roy /*
38172b24ab6bSSebastien Roy  * Open the global libdladm handle "dlh" if it isn't already opened.  The
38182b24ab6bSSebastien Roy  * caller may optionally supply a link name to obtain its linkid.  If a link
38192b24ab6bSSebastien Roy  * of a specific class or classes is required, reqclass specifies the class
38202b24ab6bSSebastien Roy  * mask.
38212b24ab6bSSebastien Roy  */
38222b24ab6bSSebastien Roy static dladm_status_t
38232b24ab6bSSebastien Roy ifconfig_dladm_open(const char *name, datalink_class_t reqclass,
38242b24ab6bSSebastien Roy     datalink_id_t *linkid)
38252b24ab6bSSebastien Roy {
38262b24ab6bSSebastien Roy 	dladm_status_t status = DLADM_STATUS_OK;
38272b24ab6bSSebastien Roy 	datalink_class_t class;
38282b24ab6bSSebastien Roy 
38292b24ab6bSSebastien Roy 	if (!dlh_opened) {
38302b24ab6bSSebastien Roy 		if ((status = dladm_open(&dlh)) != DLADM_STATUS_OK)
38312b24ab6bSSebastien Roy 			return (status);
38322b24ab6bSSebastien Roy 		dlh_opened = _B_TRUE;
38332b24ab6bSSebastien Roy 	}
38342b24ab6bSSebastien Roy 	if (name != NULL) {
38352b24ab6bSSebastien Roy 		status = dladm_name2info(dlh, name, linkid, NULL, &class, NULL);
38362b24ab6bSSebastien Roy 		if (status == DLADM_STATUS_OK) {
38372b24ab6bSSebastien Roy 			if (!(class & reqclass))
38382b24ab6bSSebastien Roy 				status = DLADM_STATUS_LINKINVAL;
38392b24ab6bSSebastien Roy 		}
38402b24ab6bSSebastien Roy 	}
38412b24ab6bSSebastien Roy 	return (status);
38422b24ab6bSSebastien Roy }
38432b24ab6bSSebastien Roy 
38446e91bba0SGirish Moodalbail /*
38456e91bba0SGirish Moodalbail  * This function checks if we can use libipadm API's. We will only
38466e91bba0SGirish Moodalbail  * call libipadm functions for non-IPMP interfaces. This check is
38476e91bba0SGirish Moodalbail  * temporary until libipadm supports IPMP interfaces.
38486e91bba0SGirish Moodalbail  */
38496e91bba0SGirish Moodalbail static boolean_t
38506e91bba0SGirish Moodalbail ifconfig_use_libipadm(int s, const char *lifname)
38516e91bba0SGirish Moodalbail {
38526e91bba0SGirish Moodalbail 	struct lifreq lifr1;
38536e91bba0SGirish Moodalbail 
38546e91bba0SGirish Moodalbail 	(void) strlcpy(lifr1.lifr_name, lifname, sizeof (lifr1.lifr_name));
38556e91bba0SGirish Moodalbail 	if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr1) < 0) {
38566e91bba0SGirish Moodalbail 		(void) strncpy(lifr.lifr_name, lifname,
38576e91bba0SGirish Moodalbail 		    sizeof (lifr.lifr_name));
38586e91bba0SGirish Moodalbail 		Perror0_exit("error");
38596e91bba0SGirish Moodalbail 	}
38606e91bba0SGirish Moodalbail 
38616e91bba0SGirish Moodalbail 	return (lifr1.lifr_groupname[0] == '\0');
38626e91bba0SGirish Moodalbail }
38636e91bba0SGirish Moodalbail 
38646e91bba0SGirish Moodalbail static void
38656e91bba0SGirish Moodalbail ipadmerr_exit(ipadm_status_t status, const char *str)
38666e91bba0SGirish Moodalbail {
38676e91bba0SGirish Moodalbail 	(void) fprintf(stderr, "ifconfig: %s: %s\n", str,
38686e91bba0SGirish Moodalbail 	    ipadm_status2str(status));
38696e91bba0SGirish Moodalbail 	exit(1);
38706e91bba0SGirish Moodalbail }
38716e91bba0SGirish Moodalbail 
38726e91bba0SGirish Moodalbail static void
38732b24ab6bSSebastien Roy dladmerr_exit(dladm_status_t status, const char *str)
38742b24ab6bSSebastien Roy {
38752b24ab6bSSebastien Roy 	char errstr[DLADM_STRSIZE];
38762b24ab6bSSebastien Roy 
38772b24ab6bSSebastien Roy 	(void) fprintf(stderr, "%s: %s\n", str,
38782b24ab6bSSebastien Roy 	    dladm_status2str(status, errstr));
38792b24ab6bSSebastien Roy 	exit(1);
38802b24ab6bSSebastien Roy }
38812b24ab6bSSebastien Roy 
38827c478bd9Sstevel@tonic-gate void
3883f7d61273Smeem Perror0(const char *cmd)
38847c478bd9Sstevel@tonic-gate {
3885f7d61273Smeem 	Perror2(cmd, lifr.lifr_name);
38867c478bd9Sstevel@tonic-gate }
38877c478bd9Sstevel@tonic-gate 
38887c478bd9Sstevel@tonic-gate void
3889f7d61273Smeem Perror0_exit(const char *cmd)
38907c478bd9Sstevel@tonic-gate {
38917c478bd9Sstevel@tonic-gate 	Perror0(cmd);
38927c478bd9Sstevel@tonic-gate 	exit(1);
38937c478bd9Sstevel@tonic-gate }
38947c478bd9Sstevel@tonic-gate 
38957c478bd9Sstevel@tonic-gate void
3896f7d61273Smeem Perror2(const char *cmd, const char *str)
38977c478bd9Sstevel@tonic-gate {
3898f7d61273Smeem 	int error = errno;
38997c478bd9Sstevel@tonic-gate 
3900f7d61273Smeem 	(void) fprintf(stderr, "ifconfig: %s: ", cmd);
39017c478bd9Sstevel@tonic-gate 
3902f7d61273Smeem 	switch (error) {
39037c478bd9Sstevel@tonic-gate 	case ENXIO:
3904f7d61273Smeem 		(void) fprintf(stderr, "%s: no such interface\n", str);
39057c478bd9Sstevel@tonic-gate 		break;
39067c478bd9Sstevel@tonic-gate 	case EPERM:
3907f7d61273Smeem 		(void) fprintf(stderr, "%s: permission denied\n", str);
39087c478bd9Sstevel@tonic-gate 		break;
3909f7d61273Smeem 	case EEXIST:
3910f7d61273Smeem 		(void) fprintf(stderr, "%s: already exists\n", str);
3911f7d61273Smeem 		break;
3912e899e593SGirish Moodalbail 	case ENAMETOOLONG:
3913e899e593SGirish Moodalbail 		(void) fprintf(stderr, "%s: interface name too long\n", str);
3914e899e593SGirish Moodalbail 		break;
3915e899e593SGirish Moodalbail 	case ERANGE:
3916e899e593SGirish Moodalbail 		(void) fprintf(stderr, "%s: logical interface id is outside "
3917e899e593SGirish Moodalbail 		    "allowed range\n", str);
3918e899e593SGirish Moodalbail 		break;
3919f7d61273Smeem 	default:
3920f7d61273Smeem 		errno = error;
3921f7d61273Smeem 		perror(str);
39227c478bd9Sstevel@tonic-gate 	}
39237c478bd9Sstevel@tonic-gate }
39247c478bd9Sstevel@tonic-gate 
39257c478bd9Sstevel@tonic-gate /*
39267c478bd9Sstevel@tonic-gate  * Print out error message (Perror2()) and exit
39277c478bd9Sstevel@tonic-gate  */
39287c478bd9Sstevel@tonic-gate void
3929f7d61273Smeem Perror2_exit(const char *cmd, const char *str)
39307c478bd9Sstevel@tonic-gate {
39317c478bd9Sstevel@tonic-gate 	Perror2(cmd, str);
39327c478bd9Sstevel@tonic-gate 	exit(1);
39337c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
39347c478bd9Sstevel@tonic-gate }
39357c478bd9Sstevel@tonic-gate 
3936c7e4935fSss void
3937c7e4935fSss Perrdlpi(const char *cmd, const char *linkname, int err)
3938c7e4935fSss {
3939c7e4935fSss 	(void) fprintf(stderr, "ifconfig: %s \"%s\": %s\n", cmd,
3940c7e4935fSss 	    linkname, dlpi_strerror(err));
3941c7e4935fSss }
3942c7e4935fSss 
3943c7e4935fSss /*
3944c7e4935fSss  * Print out error message (Perrdlpi()) and exit
3945c7e4935fSss  */
3946c7e4935fSss void
3947c7e4935fSss Perrdlpi_exit(const char *cmd, const char *linkname, int err)
3948c7e4935fSss {
3949c7e4935fSss 	Perrdlpi(cmd, linkname, err);
3950c7e4935fSss 	exit(1);
3951c7e4935fSss }
3952c7e4935fSss 
39537c478bd9Sstevel@tonic-gate /*
39547c478bd9Sstevel@tonic-gate  * If the last argument is non-NULL allow a <addr>/<n> syntax and
39557c478bd9Sstevel@tonic-gate  * pass out <n> in *plenp.
39567c478bd9Sstevel@tonic-gate  * If <n> doesn't parse return BAD_ADDR as *plenp.
39577c478bd9Sstevel@tonic-gate  * If no /<n> is present return NO_PREFIX as *plenp.
39587c478bd9Sstevel@tonic-gate  */
39597c478bd9Sstevel@tonic-gate static void
39607c478bd9Sstevel@tonic-gate in_getaddr(char *s, struct sockaddr *saddr, int *plenp)
39617c478bd9Sstevel@tonic-gate {
396269bb4bb4Scarlsonj 	/* LINTED: alignment */
39637c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
39647c478bd9Sstevel@tonic-gate 	struct hostent *hp;
39657c478bd9Sstevel@tonic-gate 	struct netent *np;
39667c478bd9Sstevel@tonic-gate 	char str[BUFSIZ];
39677c478bd9Sstevel@tonic-gate 	int error_num;
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate 	(void) strncpy(str, s, sizeof (str));
39707c478bd9Sstevel@tonic-gate 
39717c478bd9Sstevel@tonic-gate 	/*
39727c478bd9Sstevel@tonic-gate 	 * Look for '/'<n> is plenp
39737c478bd9Sstevel@tonic-gate 	 */
39747c478bd9Sstevel@tonic-gate 	if (plenp != NULL) {
39757c478bd9Sstevel@tonic-gate 		char *cp;
39767c478bd9Sstevel@tonic-gate 
39777906a3e0Smeem 		*plenp = in_getprefixlen(str, _B_TRUE, IP_ABITS);
39787c478bd9Sstevel@tonic-gate 		if (*plenp == BAD_ADDR)
39797c478bd9Sstevel@tonic-gate 			return;
39807c478bd9Sstevel@tonic-gate 		cp = strchr(str, '/');
39817c478bd9Sstevel@tonic-gate 		if (cp != NULL)
39827c478bd9Sstevel@tonic-gate 			*cp = '\0';
39837c478bd9Sstevel@tonic-gate 	} else if (strchr(str, '/') != NULL) {
39847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
39857c478bd9Sstevel@tonic-gate 		exit(1);
39867c478bd9Sstevel@tonic-gate 	}
39877c478bd9Sstevel@tonic-gate 
39887c478bd9Sstevel@tonic-gate 	(void) memset(sin, 0, sizeof (*sin));
39897c478bd9Sstevel@tonic-gate 
39907c478bd9Sstevel@tonic-gate 	/*
39917c478bd9Sstevel@tonic-gate 	 *	Try to catch attempts to set the broadcast address to all 1's.
39927c478bd9Sstevel@tonic-gate 	 */
39937c478bd9Sstevel@tonic-gate 	if (strcmp(str, "255.255.255.255") == 0 ||
39947c478bd9Sstevel@tonic-gate 	    (strtoul(str, (char **)NULL, 0) == 0xffffffffUL)) {
39957c478bd9Sstevel@tonic-gate 		sin->sin_family = AF_INET;
39967c478bd9Sstevel@tonic-gate 		sin->sin_addr.s_addr = 0xffffffff;
39977c478bd9Sstevel@tonic-gate 		return;
39987c478bd9Sstevel@tonic-gate 	}
39997c478bd9Sstevel@tonic-gate 
40007c478bd9Sstevel@tonic-gate 	hp = getipnodebyname(str, AF_INET, 0, &error_num);
40017c478bd9Sstevel@tonic-gate 	if (hp) {
40027c478bd9Sstevel@tonic-gate 		sin->sin_family = hp->h_addrtype;
40037c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
40047c478bd9Sstevel@tonic-gate 		freehostent(hp);
40057c478bd9Sstevel@tonic-gate 		return;
40067c478bd9Sstevel@tonic-gate 	}
40077c478bd9Sstevel@tonic-gate 	np = getnetbyname(str);
40087c478bd9Sstevel@tonic-gate 	if (np) {
40097c478bd9Sstevel@tonic-gate 		sin->sin_family = np->n_addrtype;
40107c478bd9Sstevel@tonic-gate 		sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
40117c478bd9Sstevel@tonic-gate 		return;
40127c478bd9Sstevel@tonic-gate 	}
40137c478bd9Sstevel@tonic-gate 	if (error_num == TRY_AGAIN) {
40147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address "
40157c478bd9Sstevel@tonic-gate 		    "(try again later)\n", s);
40167c478bd9Sstevel@tonic-gate 	} else {
40177c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
40187c478bd9Sstevel@tonic-gate 	}
40197c478bd9Sstevel@tonic-gate 	exit(1);
40207c478bd9Sstevel@tonic-gate }
40217c478bd9Sstevel@tonic-gate 
40227c478bd9Sstevel@tonic-gate /*
40237c478bd9Sstevel@tonic-gate  * If the last argument is non-NULL allow a <addr>/<n> syntax and
40247c478bd9Sstevel@tonic-gate  * pass out <n> in *plenp.
40257c478bd9Sstevel@tonic-gate  * If <n> doesn't parse return BAD_ADDR as *plenp.
40267c478bd9Sstevel@tonic-gate  * If no /<n> is present return NO_PREFIX as *plenp.
40277c478bd9Sstevel@tonic-gate  */
40287c478bd9Sstevel@tonic-gate static void
40297c478bd9Sstevel@tonic-gate in6_getaddr(char *s, struct sockaddr *saddr, int *plenp)
40307c478bd9Sstevel@tonic-gate {
403169bb4bb4Scarlsonj 	/* LINTED: alignment */
40327c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)saddr;
40337c478bd9Sstevel@tonic-gate 	struct hostent *hp;
40347c478bd9Sstevel@tonic-gate 	char str[BUFSIZ];
40357c478bd9Sstevel@tonic-gate 	int error_num;
40367c478bd9Sstevel@tonic-gate 
40377c478bd9Sstevel@tonic-gate 	(void) strncpy(str, s, sizeof (str));
40387c478bd9Sstevel@tonic-gate 
40397c478bd9Sstevel@tonic-gate 	/*
40407c478bd9Sstevel@tonic-gate 	 * Look for '/'<n> is plenp
40417c478bd9Sstevel@tonic-gate 	 */
40427c478bd9Sstevel@tonic-gate 	if (plenp != NULL) {
40437c478bd9Sstevel@tonic-gate 		char *cp;
40447c478bd9Sstevel@tonic-gate 
40457906a3e0Smeem 		*plenp = in_getprefixlen(str, _B_TRUE, IPV6_ABITS);
40467c478bd9Sstevel@tonic-gate 		if (*plenp == BAD_ADDR)
40477c478bd9Sstevel@tonic-gate 			return;
40487c478bd9Sstevel@tonic-gate 		cp = strchr(str, '/');
40497c478bd9Sstevel@tonic-gate 		if (cp != NULL)
40507c478bd9Sstevel@tonic-gate 			*cp = '\0';
40517c478bd9Sstevel@tonic-gate 	} else if (strchr(str, '/') != NULL) {
40527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
40537c478bd9Sstevel@tonic-gate 		exit(1);
40547c478bd9Sstevel@tonic-gate 	}
40557c478bd9Sstevel@tonic-gate 
40567c478bd9Sstevel@tonic-gate 	(void) memset(sin6, 0, sizeof (*sin6));
40577c478bd9Sstevel@tonic-gate 
40587c478bd9Sstevel@tonic-gate 	hp = getipnodebyname(str, AF_INET6, 0, &error_num);
40597c478bd9Sstevel@tonic-gate 	if (hp) {
40607c478bd9Sstevel@tonic-gate 		sin6->sin6_family = hp->h_addrtype;
40617c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin6->sin6_addr, hp->h_addr, hp->h_length);
40627c478bd9Sstevel@tonic-gate 		freehostent(hp);
40637c478bd9Sstevel@tonic-gate 		return;
40647c478bd9Sstevel@tonic-gate 	}
40657c478bd9Sstevel@tonic-gate 	if (error_num == TRY_AGAIN) {
40667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address "
40677c478bd9Sstevel@tonic-gate 		    "(try again later)\n", s);
40687c478bd9Sstevel@tonic-gate 	} else {
40697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
40707c478bd9Sstevel@tonic-gate 	}
40717c478bd9Sstevel@tonic-gate 	exit(1);
40727c478bd9Sstevel@tonic-gate }
40737c478bd9Sstevel@tonic-gate 
40747c478bd9Sstevel@tonic-gate /*
40757c478bd9Sstevel@tonic-gate  * If "slash" is zero this parses the whole string as
40767c478bd9Sstevel@tonic-gate  * an integer. With "slash" non zero it parses the tail part as an integer.
40777c478bd9Sstevel@tonic-gate  *
40787c478bd9Sstevel@tonic-gate  * If it is not a valid integer this returns BAD_ADDR.
40797c478bd9Sstevel@tonic-gate  * If there is /<n> present this returns NO_PREFIX.
40807c478bd9Sstevel@tonic-gate  */
40817c478bd9Sstevel@tonic-gate static int
40827c478bd9Sstevel@tonic-gate in_getprefixlen(char *addr, boolean_t slash, int max_plen)
40837c478bd9Sstevel@tonic-gate {
40847c478bd9Sstevel@tonic-gate 	int prefixlen;
40857c478bd9Sstevel@tonic-gate 	char *str, *end;
40867c478bd9Sstevel@tonic-gate 
40877c478bd9Sstevel@tonic-gate 	if (slash) {
40887c478bd9Sstevel@tonic-gate 		str = strchr(addr, '/');
40897c478bd9Sstevel@tonic-gate 		if (str == NULL)
40907c478bd9Sstevel@tonic-gate 			return (NO_PREFIX);
40917c478bd9Sstevel@tonic-gate 		str++;
40927c478bd9Sstevel@tonic-gate 	} else
40937c478bd9Sstevel@tonic-gate 		str = addr;
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate 	prefixlen = strtol(str, &end, 10);
40967c478bd9Sstevel@tonic-gate 	if (prefixlen < 0)
40977c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
40987c478bd9Sstevel@tonic-gate 	if (str == end)
40997c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
41007c478bd9Sstevel@tonic-gate 	if (max_plen != 0 && max_plen < prefixlen)
41017c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
41027c478bd9Sstevel@tonic-gate 	return (prefixlen);
41037c478bd9Sstevel@tonic-gate }
41047c478bd9Sstevel@tonic-gate 
41057c478bd9Sstevel@tonic-gate /*
41067c478bd9Sstevel@tonic-gate  * Convert a prefix length to a mask.
41077c478bd9Sstevel@tonic-gate  * Returns 1 if ok. 0 otherwise.
41087c478bd9Sstevel@tonic-gate  * Assumes the mask array is zero'ed by the caller.
41097c478bd9Sstevel@tonic-gate  */
41107c478bd9Sstevel@tonic-gate static boolean_t
41117c478bd9Sstevel@tonic-gate in_prefixlentomask(int prefixlen, int maxlen, uchar_t *mask)
41127c478bd9Sstevel@tonic-gate {
41137c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxlen)
41147c478bd9Sstevel@tonic-gate 		return (0);
41157c478bd9Sstevel@tonic-gate 
41167c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
41177c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
41187c478bd9Sstevel@tonic-gate 			*mask++ = 0xFF;
41197c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
41207c478bd9Sstevel@tonic-gate 			continue;
41217c478bd9Sstevel@tonic-gate 		}
41227c478bd9Sstevel@tonic-gate 		*mask |= 1 << (8 - prefixlen);
41237c478bd9Sstevel@tonic-gate 		prefixlen--;
41247c478bd9Sstevel@tonic-gate 	}
41257c478bd9Sstevel@tonic-gate 	return (1);
41267c478bd9Sstevel@tonic-gate }
41277c478bd9Sstevel@tonic-gate 
41287c478bd9Sstevel@tonic-gate static void
41297c478bd9Sstevel@tonic-gate print_flags(uint64_t flags)
41307c478bd9Sstevel@tonic-gate {
41317c478bd9Sstevel@tonic-gate 	boolean_t first = _B_TRUE;
41327c478bd9Sstevel@tonic-gate 	int cnt, i;
41337c478bd9Sstevel@tonic-gate 
41347c478bd9Sstevel@tonic-gate 	(void) printf("flags=%llx", flags);
41357c478bd9Sstevel@tonic-gate 	cnt = sizeof (if_flags_tbl) / sizeof (if_flags_t);
41367c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
41377c478bd9Sstevel@tonic-gate 		if (flags & if_flags_tbl[i].iff_value) {
41387c478bd9Sstevel@tonic-gate 			if (first) {
41397c478bd9Sstevel@tonic-gate 				(void) printf("<");
41407c478bd9Sstevel@tonic-gate 				first = _B_FALSE;
41417c478bd9Sstevel@tonic-gate 			} else {
41427c478bd9Sstevel@tonic-gate 				/*
41437c478bd9Sstevel@tonic-gate 				 * It has to be here and not with the
41447c478bd9Sstevel@tonic-gate 				 * printf below because for the last one,
41457c478bd9Sstevel@tonic-gate 				 * we don't want a comma before the ">".
41467c478bd9Sstevel@tonic-gate 				 */
41477c478bd9Sstevel@tonic-gate 				(void) printf(",");
41487c478bd9Sstevel@tonic-gate 			}
41497c478bd9Sstevel@tonic-gate 			(void) printf("%s", if_flags_tbl[i].iff_name);
41507c478bd9Sstevel@tonic-gate 		}
41517c478bd9Sstevel@tonic-gate 	}
41527c478bd9Sstevel@tonic-gate 	if (!first)
41537c478bd9Sstevel@tonic-gate 		(void) printf(">");
41547c478bd9Sstevel@tonic-gate }
41557c478bd9Sstevel@tonic-gate 
41567c478bd9Sstevel@tonic-gate static void
4157e11c3f44Smeem print_config_flags(int af, uint64_t flags)
41587c478bd9Sstevel@tonic-gate {
4159e11c3f44Smeem 	if_config_cmd_t *cmdp;
41607c478bd9Sstevel@tonic-gate 
4161e11c3f44Smeem 	for (cmdp = if_config_cmd_tbl; cmdp->iff_flag != 0; cmdp++) {
4162e11c3f44Smeem 		if ((flags & cmdp->iff_flag) &&
4163e11c3f44Smeem 		    (cmdp->iff_af == AF_UNSPEC || cmdp->iff_af == af)) {
4164e11c3f44Smeem 			(void) printf("%s ", cmdp->iff_name);
41657c478bd9Sstevel@tonic-gate 		}
41667c478bd9Sstevel@tonic-gate 	}
41677c478bd9Sstevel@tonic-gate }
41687c478bd9Sstevel@tonic-gate 
41697c478bd9Sstevel@tonic-gate /*
417036e852a1SRaja Andra  * Use the configured directory lookup mechanism (e.g. files/NIS/...)
4171dd7a6f5fSkcpoon  * to find the network mask.  Returns true if we found one to set.
4172dd7a6f5fSkcpoon  *
4173dd7a6f5fSkcpoon  * The parameter addr_set controls whether we should get the address of
4174dd7a6f5fSkcpoon  * the working interface for the netmask query.  If addr_set is true,
4175dd7a6f5fSkcpoon  * we will use the address provided.  Otherwise, we will find the working
4176dd7a6f5fSkcpoon  * interface's address and use it instead.
41777c478bd9Sstevel@tonic-gate  */
41787c478bd9Sstevel@tonic-gate static boolean_t
4179dd7a6f5fSkcpoon in_getmask(struct sockaddr_in *saddr, boolean_t addr_set)
41807c478bd9Sstevel@tonic-gate {
41817c478bd9Sstevel@tonic-gate 	struct sockaddr_in ifaddr;
41827c478bd9Sstevel@tonic-gate 
41837c478bd9Sstevel@tonic-gate 	/*
4184dd7a6f5fSkcpoon 	 * Read the address from the interface if it is not passed in.
41857c478bd9Sstevel@tonic-gate 	 */
4186dd7a6f5fSkcpoon 	if (!addr_set) {
4187dd7a6f5fSkcpoon 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
4188dd7a6f5fSkcpoon 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
4189dd7a6f5fSkcpoon 			if (errno != EADDRNOTAVAIL) {
4190dd7a6f5fSkcpoon 				(void) fprintf(stderr, "Need net number for "
4191dd7a6f5fSkcpoon 				    "mask\n");
4192dd7a6f5fSkcpoon 			}
4193dd7a6f5fSkcpoon 			return (_B_FALSE);
4194dd7a6f5fSkcpoon 		}
4195dd7a6f5fSkcpoon 		ifaddr = *((struct sockaddr_in *)&lifr.lifr_addr);
4196dd7a6f5fSkcpoon 	} else {
4197dd7a6f5fSkcpoon 		ifaddr.sin_addr = saddr->sin_addr;
41987c478bd9Sstevel@tonic-gate 	}
41997c478bd9Sstevel@tonic-gate 	if (getnetmaskbyaddr(ifaddr.sin_addr, &saddr->sin_addr) == 0) {
42007c478bd9Sstevel@tonic-gate 		saddr->sin_family = AF_INET;
42017c478bd9Sstevel@tonic-gate 		return (_B_TRUE);
42027c478bd9Sstevel@tonic-gate 	}
42037c478bd9Sstevel@tonic-gate 	return (_B_FALSE);
42047c478bd9Sstevel@tonic-gate }
42057c478bd9Sstevel@tonic-gate 
42067c478bd9Sstevel@tonic-gate static int
4207e11c3f44Smeem lifnum(const char *ifname)
4208e11c3f44Smeem {
4209e11c3f44Smeem 	const char *cp;
4210e11c3f44Smeem 
4211e11c3f44Smeem 	if ((cp = strchr(ifname, ':')) == NULL)
4212e11c3f44Smeem 		return (0);
4213e11c3f44Smeem 	else
4214e11c3f44Smeem 		return (atoi(cp + 1));
4215e11c3f44Smeem }
4216e11c3f44Smeem 
42177c478bd9Sstevel@tonic-gate static void
4218d62bc4baSyz add_ni(const char *name)
42197c478bd9Sstevel@tonic-gate {
42207c478bd9Sstevel@tonic-gate 	ni_t **pp;
42217c478bd9Sstevel@tonic-gate 	ni_t *p;
42227c478bd9Sstevel@tonic-gate 
42237c478bd9Sstevel@tonic-gate 	for (pp = &ni_list; (p = *pp) != NULL; pp = &(p->ni_next)) {
42247c478bd9Sstevel@tonic-gate 		if (strcmp(p->ni_name, name) == 0) {
42257c478bd9Sstevel@tonic-gate 			if (debug > 2)
42267c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "'%s' is a duplicate\n",
42277c478bd9Sstevel@tonic-gate 				    name);
42287c478bd9Sstevel@tonic-gate 			return;
42297c478bd9Sstevel@tonic-gate 		}
42307c478bd9Sstevel@tonic-gate 	}
42317c478bd9Sstevel@tonic-gate 
42327c478bd9Sstevel@tonic-gate 	if (debug > 2)
42337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "adding '%s'\n",
42347c478bd9Sstevel@tonic-gate 		    name);
42357c478bd9Sstevel@tonic-gate 
42367c478bd9Sstevel@tonic-gate 	if ((p = malloc(sizeof (ni_t))) == NULL)
42377c478bd9Sstevel@tonic-gate 		return;
42387c478bd9Sstevel@tonic-gate 
42397c478bd9Sstevel@tonic-gate 	(void) strlcpy(p->ni_name, name, sizeof (p->ni_name));
42407c478bd9Sstevel@tonic-gate 	p->ni_next = NULL;
42417c478bd9Sstevel@tonic-gate 
42427c478bd9Sstevel@tonic-gate 	*pp = p;
42437c478bd9Sstevel@tonic-gate 	num_ni++;
42447c478bd9Sstevel@tonic-gate }
42457c478bd9Sstevel@tonic-gate 
4246d62bc4baSyz static boolean_t
4247d62bc4baSyz ni_entry(const char *linkname, void *arg)
42487c478bd9Sstevel@tonic-gate {
4249c7e4935fSss 	dlpi_handle_t	dh;
4250da14cebeSEric Cheng 	datalink_class_t class;
42517c478bd9Sstevel@tonic-gate 
4252d4d1f7bfSVasumathi Sundaram - Sun Microsystems 	(void) dladm_name2info(arg, linkname, NULL, NULL, &class, NULL);
42534ac67f02SAnurag S. Maskey 
4254da14cebeSEric Cheng 	if (class == DATALINK_CLASS_ETHERSTUB)
4255da14cebeSEric Cheng 		return (_B_FALSE);
4256c7e4935fSss 	if (dlpi_open(linkname, &dh, 0) != DLPI_SUCCESS)
4257d62bc4baSyz 		return (_B_FALSE);
42587c478bd9Sstevel@tonic-gate 
4259d62bc4baSyz 	add_ni(linkname);
42607c478bd9Sstevel@tonic-gate 
4261c7e4935fSss 	dlpi_close(dh);
4262d62bc4baSyz 	return (_B_FALSE);
42637c478bd9Sstevel@tonic-gate }
42647c478bd9Sstevel@tonic-gate 
42657c478bd9Sstevel@tonic-gate /*
42667c478bd9Sstevel@tonic-gate  * dhcp-related routines
42677c478bd9Sstevel@tonic-gate  */
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate static int
42707c478bd9Sstevel@tonic-gate setifdhcp(const char *caller, const char *ifname, int argc, char *argv[])
42717c478bd9Sstevel@tonic-gate {
42727c478bd9Sstevel@tonic-gate 	dhcp_ipc_request_t	*request;
42737c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t	*reply	= NULL;
42747c478bd9Sstevel@tonic-gate 	int			timeout = DHCP_IPC_WAIT_DEFAULT;
42757c478bd9Sstevel@tonic-gate 	dhcp_ipc_type_t		type	= DHCP_START;
42767c478bd9Sstevel@tonic-gate 	int			error;
42777c478bd9Sstevel@tonic-gate 	boolean_t		is_primary = _B_FALSE;
42787c478bd9Sstevel@tonic-gate 	boolean_t		started = _B_FALSE;
42797c478bd9Sstevel@tonic-gate 
42807c478bd9Sstevel@tonic-gate 	for (argv++; --argc > 0; argv++) {
42817c478bd9Sstevel@tonic-gate 
42827c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "primary") == 0) {
42837c478bd9Sstevel@tonic-gate 			is_primary = _B_TRUE;
42847c478bd9Sstevel@tonic-gate 			continue;
42857c478bd9Sstevel@tonic-gate 		}
42867c478bd9Sstevel@tonic-gate 
42877c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "wait") == 0) {
42887c478bd9Sstevel@tonic-gate 			if (--argc <= 0) {
42897c478bd9Sstevel@tonic-gate 				usage();
42907c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
42917c478bd9Sstevel@tonic-gate 			}
42927c478bd9Sstevel@tonic-gate 			argv++;
42937c478bd9Sstevel@tonic-gate 
42947c478bd9Sstevel@tonic-gate 			if (strcmp(*argv, "forever") == 0) {
42957c478bd9Sstevel@tonic-gate 				timeout = DHCP_IPC_WAIT_FOREVER;
42967c478bd9Sstevel@tonic-gate 				continue;
42977c478bd9Sstevel@tonic-gate 			}
42987c478bd9Sstevel@tonic-gate 
42997c478bd9Sstevel@tonic-gate 			if (sscanf(*argv, "%d", &timeout) != 1) {
43007c478bd9Sstevel@tonic-gate 				usage();
43017c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
43027c478bd9Sstevel@tonic-gate 			}
43037c478bd9Sstevel@tonic-gate 
43047c478bd9Sstevel@tonic-gate 			if (timeout < 0) {
43057c478bd9Sstevel@tonic-gate 				usage();
43067c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
43077c478bd9Sstevel@tonic-gate 			}
43087c478bd9Sstevel@tonic-gate 			continue;
43097c478bd9Sstevel@tonic-gate 		}
43107c478bd9Sstevel@tonic-gate 
43117c478bd9Sstevel@tonic-gate 		type = dhcp_string_to_request(*argv);
43127c478bd9Sstevel@tonic-gate 		if (type == -1) {
43137c478bd9Sstevel@tonic-gate 			usage();
43147c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_BADARGS);
43157c478bd9Sstevel@tonic-gate 		}
43167c478bd9Sstevel@tonic-gate 	}
43177c478bd9Sstevel@tonic-gate 
43187c478bd9Sstevel@tonic-gate 	/*
43197c478bd9Sstevel@tonic-gate 	 * Only try to start agent on start or inform; in all other cases it
43207c478bd9Sstevel@tonic-gate 	 * has to already be running for anything to make sense.
43217c478bd9Sstevel@tonic-gate 	 */
43227c478bd9Sstevel@tonic-gate 	if (type == DHCP_START || type == DHCP_INFORM) {
43237c478bd9Sstevel@tonic-gate 		if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1) {
43247c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: unable to start %s\n",
43257c478bd9Sstevel@tonic-gate 			    caller, DHCP_AGENT_PATH);
43267c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_FAILURE);
43277c478bd9Sstevel@tonic-gate 		}
43287c478bd9Sstevel@tonic-gate 		started = _B_TRUE;
43297c478bd9Sstevel@tonic-gate 	}
43307c478bd9Sstevel@tonic-gate 
43317c478bd9Sstevel@tonic-gate 	if (is_primary)
4332d04ccbb3Scarlsonj 		type |= DHCP_PRIMARY;
4333d04ccbb3Scarlsonj 
4334d04ccbb3Scarlsonj 	if (af != AF_INET)
4335d04ccbb3Scarlsonj 		type |= DHCP_V6;
43367c478bd9Sstevel@tonic-gate 
43377c478bd9Sstevel@tonic-gate 	request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
43387c478bd9Sstevel@tonic-gate 	if (request == NULL) {
43397c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: out of memory\n", caller);
43407c478bd9Sstevel@tonic-gate 		return (DHCP_EXIT_SYSTEM);
43417c478bd9Sstevel@tonic-gate 	}
43427c478bd9Sstevel@tonic-gate 
43437c478bd9Sstevel@tonic-gate 	error = dhcp_ipc_make_request(request, &reply, timeout);
43447c478bd9Sstevel@tonic-gate 	if (error != 0) {
43457c478bd9Sstevel@tonic-gate 		free(request);
43467c478bd9Sstevel@tonic-gate 		/*
43477c478bd9Sstevel@tonic-gate 		 * Re-map connect error to not under control if we didn't try a
43487c478bd9Sstevel@tonic-gate 		 * start operation, as this has to be true and results in a
43497c478bd9Sstevel@tonic-gate 		 * clearer message, not to mention preserving compatibility
43507c478bd9Sstevel@tonic-gate 		 * with the days when we always started dhcpagent for every
43517c478bd9Sstevel@tonic-gate 		 * request.
43527c478bd9Sstevel@tonic-gate 		 */
43537c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_CONNECT && !started)
43547c478bd9Sstevel@tonic-gate 			error = DHCP_IPC_E_UNKIF;
43557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
43567c478bd9Sstevel@tonic-gate 		    dhcp_ipc_strerror(error));
43577c478bd9Sstevel@tonic-gate 		return (DHCP_EXIT_FAILURE);
43587c478bd9Sstevel@tonic-gate 	}
43597c478bd9Sstevel@tonic-gate 
43607c478bd9Sstevel@tonic-gate 	error = reply->return_code;
43617c478bd9Sstevel@tonic-gate 	if (error != 0) {
43627c478bd9Sstevel@tonic-gate 		free(request);
43637c478bd9Sstevel@tonic-gate 		free(reply);
43647c478bd9Sstevel@tonic-gate 
43657c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_TIMEOUT && timeout == 0)
43667c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_SUCCESS);
43677c478bd9Sstevel@tonic-gate 
43687c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
43697c478bd9Sstevel@tonic-gate 		    dhcp_ipc_strerror(error));
43707c478bd9Sstevel@tonic-gate 
43717c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_TIMEOUT)
43727c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_TIMEOUT);
43737c478bd9Sstevel@tonic-gate 		else
43747c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_IF_FAILURE);
43757c478bd9Sstevel@tonic-gate 	}
43767c478bd9Sstevel@tonic-gate 
43777c478bd9Sstevel@tonic-gate 	if (DHCP_IPC_CMD(type) == DHCP_STATUS) {
43787c478bd9Sstevel@tonic-gate 		(void) printf("%s", dhcp_status_hdr_string());
43797c478bd9Sstevel@tonic-gate 		(void) printf("%s", dhcp_status_reply_to_string(reply));
43807c478bd9Sstevel@tonic-gate 	}
43817c478bd9Sstevel@tonic-gate 
43827c478bd9Sstevel@tonic-gate 	free(request);
43837c478bd9Sstevel@tonic-gate 	free(reply);
43847c478bd9Sstevel@tonic-gate 	return (DHCP_EXIT_SUCCESS);
43857c478bd9Sstevel@tonic-gate }
43867c478bd9Sstevel@tonic-gate 
43877c478bd9Sstevel@tonic-gate static void
43887c478bd9Sstevel@tonic-gate usage(void)
43897c478bd9Sstevel@tonic-gate {
43907c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
43917c478bd9Sstevel@tonic-gate 	    "usage: ifconfig <interface> | -a[ 4 | 6 | D ][ u | d ][ Z ]\n");
43927c478bd9Sstevel@tonic-gate 
43937c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s",
43947c478bd9Sstevel@tonic-gate 	    "\t[ <addr_family> ]\n"
43957c478bd9Sstevel@tonic-gate 	    "\t[ <address>[/<prefix_length>] [ <dest_address> ] ]\n"
43967c478bd9Sstevel@tonic-gate 	    "\t[ set [ <address>][/<prefix_length>] ]"
43977c478bd9Sstevel@tonic-gate 	    " [ <address>/<prefix_length>] ]\n"
43987c478bd9Sstevel@tonic-gate 	    "\t[ destination <dest_address> ]\n"
43997c478bd9Sstevel@tonic-gate 	    "\t[ addif <address>[/<prefix_length>]"
44007c478bd9Sstevel@tonic-gate 	    "  [ <dest_address> ] ]\n"
44017c478bd9Sstevel@tonic-gate 	    "\t[ removeif <address>[/<prefix_length>] ]\n"
44027c478bd9Sstevel@tonic-gate 	    "\t[ arp | -arp ]\n"
44037c478bd9Sstevel@tonic-gate 	    "\t[ auto-revarp ]\n"
44047c478bd9Sstevel@tonic-gate 	    "\t[ broadcast <broad_addr> ]\n"
44057c478bd9Sstevel@tonic-gate 	    "\t[ index <if_index> ]\n"
44067c478bd9Sstevel@tonic-gate 	    "\t[ metric <n> ] [ mtu <n> ]\n"
44077c478bd9Sstevel@tonic-gate 	    "\t[ netmask <mask> ]\n"
44087c478bd9Sstevel@tonic-gate 	    "\t[ plumb ] [ unplumb ]\n"
44097c478bd9Sstevel@tonic-gate 	    "\t[ preferred | -preferred ]\n"
44107c478bd9Sstevel@tonic-gate 	    "\t[ private | -private ]\n"
44117c478bd9Sstevel@tonic-gate 	    "\t[ local | -local ]\n"
44127c478bd9Sstevel@tonic-gate 	    "\t[ router | -router ]\n"
44137c478bd9Sstevel@tonic-gate 	    "\t[ subnet <subnet_address>]\n"
44147c478bd9Sstevel@tonic-gate 	    "\t[ trailers | -trailers ]\n"
44157c478bd9Sstevel@tonic-gate 	    "\t[ token <address>/<prefix_length> ]\n"
44167c478bd9Sstevel@tonic-gate 	    "\t[ tsrc <tunnel_src_address> ]\n"
44177c478bd9Sstevel@tonic-gate 	    "\t[ tdst <tunnel_dest_address> ]\n"
44187c478bd9Sstevel@tonic-gate 	    "\t[ auth_algs <tunnel_AH_authentication_algorithm> ]\n"
44197c478bd9Sstevel@tonic-gate 	    "\t[ encr_algs <tunnel_ESP_encryption_algorithm> ]\n"
44207c478bd9Sstevel@tonic-gate 	    "\t[ encr_auth_algs <tunnel_ESP_authentication_algorithm> ]\n"
44217c478bd9Sstevel@tonic-gate 	    "\t[ up ] [ down ]\n"
44227c478bd9Sstevel@tonic-gate 	    "\t[ xmit | -xmit ]\n"
44237c478bd9Sstevel@tonic-gate 	    "\t[ modlist ]\n"
44247c478bd9Sstevel@tonic-gate 	    "\t[ modinsert <module_name@position> ]\n"
44257c478bd9Sstevel@tonic-gate 	    "\t[ modremove <module_name@position> ]\n"
4426e11c3f44Smeem 	    "\t[ ipmp ]\n"
44277c478bd9Sstevel@tonic-gate 	    "\t[ group <groupname>] | [ group \"\"]\n"
44287c478bd9Sstevel@tonic-gate 	    "\t[ deprecated | -deprecated ]\n"
44297c478bd9Sstevel@tonic-gate 	    "\t[ standby | -standby ]\n"
44307c478bd9Sstevel@tonic-gate 	    "\t[ failover | -failover ]\n"
44317c478bd9Sstevel@tonic-gate 	    "\t[ zone <zonename> | -zone ]\n"
443245916cd2Sjpk 	    "\t[ usesrc <interface> ]\n"
443345916cd2Sjpk 	    "\t[ all-zones ]\n");
44347c478bd9Sstevel@tonic-gate 
44357c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "or\n");
44367c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
44377c478bd9Sstevel@tonic-gate 	    "\tifconfig <interface> |  -a[ 4 | 6 | D ] [ u | d ]\n");
44387c478bd9Sstevel@tonic-gate 
44397c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s", "\tauto-dhcp | dhcp\n"
44407c478bd9Sstevel@tonic-gate 	    "\t[ wait <time> | forever ]\n\t[ primary ]\n"
44417c478bd9Sstevel@tonic-gate 	    "\tstart | drop | ping | release | status | inform\n");
44427c478bd9Sstevel@tonic-gate }
4443