17c478bd9Sstevel@tonic-gate /*
2*6e91bba0SGirish Moodalbail  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate /*
67c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
77c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
87c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
97c478bd9Sstevel@tonic-gate  */
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate #include "defs.h"
127c478bd9Sstevel@tonic-gate #include "strings.h"
137c478bd9Sstevel@tonic-gate #include "ifconfig.h"
147c478bd9Sstevel@tonic-gate #include <compat.h>
157c478bd9Sstevel@tonic-gate #include <libdlpi.h>
16da14cebeSEric Cheng #include <libdllink.h>
172b24ab6bSSebastien Roy #include <libdliptun.h>
182b24ab6bSSebastien Roy #include <libdllink.h>
19ff550d0eSmasputra #include <inet/ip.h>
20d2f8a3dfSpwernau #include <inet/ipsec_impl.h>
21*6e91bba0SGirish Moodalbail #include <libipadm.h>
22*6e91bba0SGirish Moodalbail #include <ifaddrs.h>
23*6e91bba0SGirish Moodalbail #include <libsocket_priv.h>
24ff550d0eSmasputra 
257c478bd9Sstevel@tonic-gate #define	LOOPBACK_IF	"lo0"
267c478bd9Sstevel@tonic-gate #define	NONE_STR	"none"
277c478bd9Sstevel@tonic-gate #define	ARP_MOD_NAME	"arp"
28*6e91bba0SGirish Moodalbail #define	LIFC_DEFAULT	(LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES |\
29*6e91bba0SGirish Moodalbail 			LIFC_UNDER_IPMP)
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate typedef struct if_flags {
327c478bd9Sstevel@tonic-gate 	uint64_t iff_value;
337c478bd9Sstevel@tonic-gate 	char	*iff_name;
347c478bd9Sstevel@tonic-gate } if_flags_t;
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static if_flags_t	if_flags_tbl[] = {
377c478bd9Sstevel@tonic-gate 	{ IFF_UP,		"UP" },
387c478bd9Sstevel@tonic-gate 	{ IFF_BROADCAST,	"BROADCAST" },
397c478bd9Sstevel@tonic-gate 	{ IFF_DEBUG,		"DEBUG" },
407c478bd9Sstevel@tonic-gate 	{ IFF_LOOPBACK,		"LOOPBACK" },
417c478bd9Sstevel@tonic-gate 	{ IFF_POINTOPOINT,	"POINTOPOINT" },
427c478bd9Sstevel@tonic-gate 	{ IFF_NOTRAILERS,	"NOTRAILERS" },
437c478bd9Sstevel@tonic-gate 	{ IFF_RUNNING,		"RUNNING" },
447c478bd9Sstevel@tonic-gate 	{ IFF_NOARP,		"NOARP" },
457c478bd9Sstevel@tonic-gate 	{ IFF_PROMISC,		"PROMISC" },
467c478bd9Sstevel@tonic-gate 	{ IFF_ALLMULTI,		"ALLMULTI" },
477c478bd9Sstevel@tonic-gate 	{ IFF_INTELLIGENT,	"INTELLIGENT" },
487c478bd9Sstevel@tonic-gate 	{ IFF_MULTICAST,	"MULTICAST" },
497c478bd9Sstevel@tonic-gate 	{ IFF_MULTI_BCAST,	"MULTI_BCAST" },
507c478bd9Sstevel@tonic-gate 	{ IFF_UNNUMBERED,	"UNNUMBERED" },
517c478bd9Sstevel@tonic-gate 	{ IFF_DHCPRUNNING,	"DHCP" },
527c478bd9Sstevel@tonic-gate 	{ IFF_PRIVATE,		"PRIVATE" },
537c478bd9Sstevel@tonic-gate 	{ IFF_NOXMIT,		"NOXMIT" },
547c478bd9Sstevel@tonic-gate 	{ IFF_NOLOCAL,		"NOLOCAL" },
557c478bd9Sstevel@tonic-gate 	{ IFF_DEPRECATED,	"DEPRECATED" },
567c478bd9Sstevel@tonic-gate 	{ IFF_ADDRCONF,		"ADDRCONF" },
577c478bd9Sstevel@tonic-gate 	{ IFF_ROUTER,		"ROUTER" },
587c478bd9Sstevel@tonic-gate 	{ IFF_NONUD,		"NONUD" },
597c478bd9Sstevel@tonic-gate 	{ IFF_ANYCAST,		"ANYCAST" },
607c478bd9Sstevel@tonic-gate 	{ IFF_NORTEXCH,		"NORTEXCH" },
617c478bd9Sstevel@tonic-gate 	{ IFF_IPV4,		"IPv4" },
627c478bd9Sstevel@tonic-gate 	{ IFF_IPV6,		"IPv6" },
637c478bd9Sstevel@tonic-gate 	{ IFF_NOFAILOVER,	"NOFAILOVER" },
647c478bd9Sstevel@tonic-gate 	{ IFF_FAILED,		"FAILED" },
657c478bd9Sstevel@tonic-gate 	{ IFF_STANDBY,		"STANDBY" },
667c478bd9Sstevel@tonic-gate 	{ IFF_INACTIVE,		"INACTIVE" },
677c478bd9Sstevel@tonic-gate 	{ IFF_OFFLINE,		"OFFLINE" },
687c478bd9Sstevel@tonic-gate 	{ IFF_XRESOLV,		"XRESOLV" },
697c478bd9Sstevel@tonic-gate 	{ IFF_COS_ENABLED,	"CoS" },
707c478bd9Sstevel@tonic-gate 	{ IFF_PREFERRED,	"PREFERRED" },
717c478bd9Sstevel@tonic-gate 	{ IFF_TEMPORARY,	"TEMPORARY" },
727c478bd9Sstevel@tonic-gate 	{ IFF_FIXEDMTU,		"FIXEDMTU" },
7369bb4bb4Scarlsonj 	{ IFF_VIRTUAL,		"VIRTUAL" },
74e11c3f44Smeem 	{ IFF_DUPLICATE,	"DUPLICATE" },
751cb875aeSCathy Zhou 	{ IFF_IPMP,		"IPMP"},
761cb875aeSCathy Zhou 	{ IFF_VRRP,		"VRRP"},
771cb875aeSCathy Zhou 	{ IFF_NOACCEPT,		"NOACCEPT"}
78e11c3f44Smeem };
79e11c3f44Smeem 
80e11c3f44Smeem typedef struct {
81e11c3f44Smeem 	const char		*ia_app;
82e11c3f44Smeem 	uint64_t		ia_flag;
83e11c3f44Smeem 	uint_t			ia_tries;
84e11c3f44Smeem } if_appflags_t;
85e11c3f44Smeem 
86e11c3f44Smeem static const if_appflags_t if_appflags_tbl[] = {
87e11c3f44Smeem 	{ "dhcpagent(1M)",	IFF_DHCPRUNNING,	1 },
88e11c3f44Smeem 	{ "in.ndpd(1M)",	IFF_ADDRCONF,		3 },
89e11c3f44Smeem 	{  NULL,		0,			0 }
907c478bd9Sstevel@tonic-gate };
917c478bd9Sstevel@tonic-gate 
922b24ab6bSSebastien Roy static dladm_handle_t	dlh;
932b24ab6bSSebastien Roy boolean_t		dlh_opened;
942b24ab6bSSebastien Roy static struct		lifreq lifr;
957906a3e0Smeem /* current interface name a particular function is accessing */
962b24ab6bSSebastien Roy static char		name[LIFNAMSIZ];
977c478bd9Sstevel@tonic-gate /* foreach interface saved name */
982b24ab6bSSebastien Roy static char		origname[LIFNAMSIZ];
992b24ab6bSSebastien Roy static int		setaddr;
100*6e91bba0SGirish Moodalbail static boolean_t	setaddr_done = _B_FALSE;
1012b24ab6bSSebastien Roy static boolean_t	ipsec_policy_set;
1022b24ab6bSSebastien Roy static boolean_t	ipsec_auth_covered;
103*6e91bba0SGirish Moodalbail static ipadm_handle_t	iph;
104*6e91bba0SGirish Moodalbail static ipadm_addrobj_t	ipaddr;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * Make sure the algorithm variables hold more than the sizeof an algorithm
1087c478bd9Sstevel@tonic-gate  * in PF_KEY.  (For now, more than a uint8_t.)  The NO_***_?ALG indicates that
1097c478bd9Sstevel@tonic-gate  * there was no algorithm requested, and in the ipsec_req that service should
1107c478bd9Sstevel@tonic-gate  * be disabled.  (E.g. if ah_aalg remains NO_AH_AALG, then AH will be
1117c478bd9Sstevel@tonic-gate  * disabled on that tunnel.)
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate #define	NO_AH_AALG 256
1147c478bd9Sstevel@tonic-gate #define	NO_ESP_AALG 256
1157c478bd9Sstevel@tonic-gate #define	NO_ESP_EALG 256
1167c478bd9Sstevel@tonic-gate 
117e11c3f44Smeem int	s, s4, s6;
1187c478bd9Sstevel@tonic-gate int	af = AF_INET;	/* default address family */
1197c478bd9Sstevel@tonic-gate int	debug = 0;
1207c478bd9Sstevel@tonic-gate int	all = 0;	/* setifdhcp() needs to know this */
1217c478bd9Sstevel@tonic-gate int	verbose = 0;
1227c478bd9Sstevel@tonic-gate int	v4compat = 0;	/* Compatible printing format */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * Function prototypes for command functions.
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate static int	addif(char *arg, int64_t param);
128e11c3f44Smeem static int	inetipmp(char *arg, int64_t param);
1297c478bd9Sstevel@tonic-gate static int	inetplumb(char *arg, int64_t param);
1307c478bd9Sstevel@tonic-gate static int	inetunplumb(char *arg, int64_t param);
1317c478bd9Sstevel@tonic-gate static int	removeif(char *arg, int64_t param);
1327c478bd9Sstevel@tonic-gate static int	setdebugflag(char *arg, int64_t param);
1337c478bd9Sstevel@tonic-gate static int	setifaddr(char *arg, int64_t param);
1347c478bd9Sstevel@tonic-gate static int	setifbroadaddr(char *arg, int64_t param);
1357c478bd9Sstevel@tonic-gate static int	setifdstaddr(char *arg, int64_t param);
1367c478bd9Sstevel@tonic-gate static int	setifether(char *arg, int64_t param);
1377c478bd9Sstevel@tonic-gate static int	setifflags(char *arg, int64_t param);
1387c478bd9Sstevel@tonic-gate static int	setifindex(char *arg, int64_t param);
1397c478bd9Sstevel@tonic-gate static int	setifmetric(char *arg, int64_t param);
1407c478bd9Sstevel@tonic-gate static int	setifmtu(char *arg, int64_t param);
1417c478bd9Sstevel@tonic-gate static int	setifnetmask(char *arg, int64_t param);
1427c478bd9Sstevel@tonic-gate static int	setifprefixlen(char *arg, int64_t param);
1437c478bd9Sstevel@tonic-gate static int	setifrevarp(char *arg, int64_t param);
1447c478bd9Sstevel@tonic-gate static int	setifsubnet(char *arg, int64_t param);
1457c478bd9Sstevel@tonic-gate static int	setiftdst(char *arg, int64_t param);
1467c478bd9Sstevel@tonic-gate static int	setiftoken(char *arg, int64_t param);
1477c478bd9Sstevel@tonic-gate static int	setiftsrc(char *arg, int64_t param);
1487c478bd9Sstevel@tonic-gate static int	setverboseflag(char *arg, int64_t param);
1497c478bd9Sstevel@tonic-gate static int	set_tun_ah_alg(char *arg, int64_t param);
1507c478bd9Sstevel@tonic-gate static int	set_tun_esp_auth_alg(char *arg, int64_t param);
1517c478bd9Sstevel@tonic-gate static int	set_tun_esp_encr_alg(char *arg, int64_t param);
1527c478bd9Sstevel@tonic-gate static int	modlist(char *arg, int64_t param);
1537c478bd9Sstevel@tonic-gate static int	modinsert(char *arg, int64_t param);
1547c478bd9Sstevel@tonic-gate static int	modremove(char *arg, int64_t param);
1557c478bd9Sstevel@tonic-gate static int	setifgroupname(char *arg, int64_t param);
1567c478bd9Sstevel@tonic-gate static int	configinfo(char *arg, int64_t param);
157e11c3f44Smeem static void	print_config_flags(int af, uint64_t flags);
1587c478bd9Sstevel@tonic-gate static void	print_flags(uint64_t flags);
159*6e91bba0SGirish Moodalbail static void	print_ifether(const char *ifname);
1607c478bd9Sstevel@tonic-gate static int	set_tun_encap_limit(char *arg, int64_t param);
1617c478bd9Sstevel@tonic-gate static int	clr_tun_encap_limit(char *arg, int64_t param);
1627c478bd9Sstevel@tonic-gate static int	set_tun_hop_limit(char *arg, int64_t param);
1637c478bd9Sstevel@tonic-gate static int	setzone(char *arg, int64_t param);
16445916cd2Sjpk static int	setallzones(char *arg, int64_t param);
1657c478bd9Sstevel@tonic-gate static int	setifsrc(char *arg, int64_t param);
166e11c3f44Smeem static int	lifnum(const char *ifname);
167*6e91bba0SGirish Moodalbail static void	plumball(int, char **, int64_t, int64_t, int64_t);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * Address family specific function prototypes.
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate static void	in_getaddr(char *s, struct sockaddr *saddr, int *plenp);
1737c478bd9Sstevel@tonic-gate static void	in_status(int force, uint64_t flags);
1747c478bd9Sstevel@tonic-gate static void	in_configinfo(int force, uint64_t flags);
1757c478bd9Sstevel@tonic-gate static void	in6_getaddr(char *s, struct sockaddr *saddr, int *plenp);
1767c478bd9Sstevel@tonic-gate static void	in6_status(int force, uint64_t flags);
1777c478bd9Sstevel@tonic-gate static void	in6_configinfo(int force, uint64_t flags);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * Misc support functions
1817c478bd9Sstevel@tonic-gate  */
182d62bc4baSyz static boolean_t	ni_entry(const char *, void *);
183*6e91bba0SGirish Moodalbail static void		foreachinterface(int argc, char *argv[],
184*6e91bba0SGirish Moodalbail 			    int af, int64_t onflags, int64_t offflags,
185*6e91bba0SGirish Moodalbail 			    int64_t lifc_flags);
186*6e91bba0SGirish Moodalbail static void		ifconfig(int argc, char *argv[], int af,
187*6e91bba0SGirish Moodalbail 			    struct ifaddrs *ifa);
188dd7a6f5fSkcpoon static boolean_t	in_getmask(struct sockaddr_in *saddr,
189dd7a6f5fSkcpoon 			    boolean_t addr_set);
190*6e91bba0SGirish Moodalbail static int		in_getprefixlen(char *addr, boolean_t slash, int plen);
1917c478bd9Sstevel@tonic-gate static boolean_t	in_prefixlentomask(int prefixlen, int maxlen,
1927c478bd9Sstevel@tonic-gate 			    uchar_t *mask);
193*6e91bba0SGirish Moodalbail static void		status(void);
194*6e91bba0SGirish Moodalbail static void		ifstatus(const char *ifname);
195*6e91bba0SGirish Moodalbail static void		tun_status(datalink_id_t);
196*6e91bba0SGirish Moodalbail static void		usage(void);
197*6e91bba0SGirish Moodalbail static int		setifdhcp(const char *caller, const char *ifname,
198*6e91bba0SGirish Moodalbail 			    int argc, char *argv[]);
199*6e91bba0SGirish Moodalbail static int		ip_domux2fd(int *, int *, int *, int *, int *);
200*6e91bba0SGirish Moodalbail static int		ip_plink(int, int, int, int, int);
201*6e91bba0SGirish Moodalbail static int		modop(char *arg, char op);
202*6e91bba0SGirish Moodalbail static int		find_all_interfaces(struct lifconf *lifcp, char **buf,
203*6e91bba0SGirish Moodalbail 			    int64_t lifc_flags);
204*6e91bba0SGirish Moodalbail static int		create_ipmp(const char *grname, int af,
205*6e91bba0SGirish Moodalbail 			    const char *ifname, boolean_t implicit);
206*6e91bba0SGirish Moodalbail static void		start_ipmp_daemon(void);
207*6e91bba0SGirish Moodalbail static boolean_t 	ifaddr_up(ifaddrlistx_t *ifaddrp);
208*6e91bba0SGirish Moodalbail static boolean_t 	ifaddr_down(ifaddrlistx_t *ifaddrp);
2092b24ab6bSSebastien Roy static dladm_status_t	ifconfig_dladm_open(const char *, datalink_class_t,
210*6e91bba0SGirish Moodalbail 			    datalink_id_t *);
211*6e91bba0SGirish Moodalbail static void		dladmerr_exit(dladm_status_t status, const char *str);
212*6e91bba0SGirish Moodalbail static void		ipadmerr_exit(ipadm_status_t status, const char *str);
213*6e91bba0SGirish Moodalbail static boolean_t	ifconfig_use_libipadm(int, const char *);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate #define	max(a, b)	((a) < (b) ? (b) : (a))
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  * DHCP_EXIT_IF_FAILURE indicates that the operation failed, but if there
2197c478bd9Sstevel@tonic-gate  * are more interfaces to act on (i.e., ifconfig was invoked with -a), keep
2207c478bd9Sstevel@tonic-gate  * on going rather than exit with an error.
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate #define	DHCP_EXIT_IF_FAILURE	-1
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate #define	NEXTARG		0xffffff	/* command takes an argument */
2267c478bd9Sstevel@tonic-gate #define	OPTARG		0xfffffe 	/* command takes an optional argument */
2277c478bd9Sstevel@tonic-gate #define	AF_ANY		(-1)
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /* Refer to the comments in ifconfig() on the netmask "hack" */
2307c478bd9Sstevel@tonic-gate #define	NETMASK_CMD	"netmask"
2317c478bd9Sstevel@tonic-gate struct sockaddr_storage	g_netmask;
232dd7a6f5fSkcpoon enum { G_NETMASK_NIL, G_NETMASK_PENDING, G_NETMASK_SET }
233dd7a6f5fSkcpoon     g_netmask_set = G_NETMASK_NIL;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate struct	cmd {
2367c478bd9Sstevel@tonic-gate 	char		*c_name;
2377c478bd9Sstevel@tonic-gate 	int64_t		c_parameter;	/* NEXTARG means next argv */
2387c478bd9Sstevel@tonic-gate 	int		(*c_func)(char *, int64_t);
2397c478bd9Sstevel@tonic-gate 	int		c_abortonfail;	/* don't continue parsing args */
2407c478bd9Sstevel@tonic-gate 					/* for the current interface */
2417c478bd9Sstevel@tonic-gate 	int	c_af;			/* address family restrictions */
2427c478bd9Sstevel@tonic-gate } cmds[] = {
2437c478bd9Sstevel@tonic-gate 	{ "up",		IFF_UP,		setifflags,	0,	AF_ANY },
2447c478bd9Sstevel@tonic-gate 	{ "down",	-IFF_UP,	setifflags,	0,	AF_ANY },
2457c478bd9Sstevel@tonic-gate 	{ "trailers",	-IFF_NOTRAILERS, setifflags,	0,	AF_ANY },
2467c478bd9Sstevel@tonic-gate 	{ "-trailers",	IFF_NOTRAILERS,	setifflags,	0,	AF_ANY },
2477c478bd9Sstevel@tonic-gate 	{ "arp",	-IFF_NOARP,	setifflags,	0,	AF_INET },
2487c478bd9Sstevel@tonic-gate 	{ "-arp",	IFF_NOARP,	setifflags,	0,	AF_INET },
2497c478bd9Sstevel@tonic-gate 	{ "router",	IFF_ROUTER,	setifflags,	0,	AF_ANY },
2507c478bd9Sstevel@tonic-gate 	{ "-router",	-IFF_ROUTER,	setifflags,	0,	AF_ANY },
2517c478bd9Sstevel@tonic-gate 	{ "private",	IFF_PRIVATE,	setifflags,	0,	AF_ANY },
2527c478bd9Sstevel@tonic-gate 	{ "-private",	-IFF_PRIVATE,	setifflags,	0,	AF_ANY },
2537c478bd9Sstevel@tonic-gate 	{ "xmit",	-IFF_NOXMIT,	setifflags,	0,	AF_ANY },
2547c478bd9Sstevel@tonic-gate 	{ "-xmit",	IFF_NOXMIT,	setifflags,	0,	AF_ANY },
2557c478bd9Sstevel@tonic-gate 	{ "-nud",	IFF_NONUD,	setifflags,	0,	AF_INET6 },
2567c478bd9Sstevel@tonic-gate 	{ "nud",	-IFF_NONUD,	setifflags,	0,	AF_INET6 },
2577c478bd9Sstevel@tonic-gate 	{ "anycast",	IFF_ANYCAST,	setifflags,	0,	AF_ANY },
2587c478bd9Sstevel@tonic-gate 	{ "-anycast",	-IFF_ANYCAST,	setifflags,	0,	AF_ANY },
2597c478bd9Sstevel@tonic-gate 	{ "local",	-IFF_NOLOCAL,	setifflags,	0,	AF_ANY },
2607c478bd9Sstevel@tonic-gate 	{ "-local",	IFF_NOLOCAL,	setifflags,	0,	AF_ANY },
2617c478bd9Sstevel@tonic-gate 	{ "deprecated",	IFF_DEPRECATED,	setifflags,	0,	AF_ANY },
2627c478bd9Sstevel@tonic-gate 	{ "-deprecated", -IFF_DEPRECATED, setifflags,	0,	AF_ANY },
2637c478bd9Sstevel@tonic-gate 	{ "preferred",	IFF_PREFERRED,	setifflags,	0,	AF_INET6 },
2647c478bd9Sstevel@tonic-gate 	{ "-preferred",	-IFF_PREFERRED,	setifflags,	0,	AF_INET6 },
2657c478bd9Sstevel@tonic-gate 	{ "debug",	0,		setdebugflag,	0,	AF_ANY },
2667c478bd9Sstevel@tonic-gate 	{ "verbose",	0,		setverboseflag,	0,	AF_ANY },
2677c478bd9Sstevel@tonic-gate 	{ NETMASK_CMD,	NEXTARG,	setifnetmask,	0,	AF_INET },
2687c478bd9Sstevel@tonic-gate 	{ "metric",	NEXTARG,	setifmetric,	0,	AF_ANY },
2697c478bd9Sstevel@tonic-gate 	{ "mtu",	NEXTARG,	setifmtu,	0,	AF_ANY },
2707c478bd9Sstevel@tonic-gate 	{ "index",	NEXTARG,	setifindex,	0,	AF_ANY },
2717c478bd9Sstevel@tonic-gate 	{ "broadcast",	NEXTARG,	setifbroadaddr,	0,	AF_INET },
2727c478bd9Sstevel@tonic-gate 	{ "auto-revarp", 0,		setifrevarp,	1,	AF_INET },
273e11c3f44Smeem 	{ "ipmp",	0,		inetipmp,	1,	AF_ANY },
2747c478bd9Sstevel@tonic-gate 	{ "plumb",	0,		inetplumb,	1,	AF_ANY },
2757c478bd9Sstevel@tonic-gate 	{ "unplumb",	0,		inetunplumb,	0,	AF_ANY },
2767c478bd9Sstevel@tonic-gate 	{ "subnet",	NEXTARG,	setifsubnet,	0,	AF_ANY },
2777c478bd9Sstevel@tonic-gate 	{ "token",	NEXTARG,	setiftoken,	0,	AF_INET6 },
2787c478bd9Sstevel@tonic-gate 	{ "tsrc",	NEXTARG,	setiftsrc,	0,	AF_ANY },
2797c478bd9Sstevel@tonic-gate 	{ "tdst",	NEXTARG,	setiftdst,	0,	AF_ANY },
2807c478bd9Sstevel@tonic-gate 	{ "encr_auth_algs", NEXTARG,	set_tun_esp_auth_alg, 0, AF_ANY },
2817c478bd9Sstevel@tonic-gate 	{ "encr_algs",	NEXTARG,	set_tun_esp_encr_alg, 0, AF_ANY },
2827c478bd9Sstevel@tonic-gate 	{ "auth_algs",	NEXTARG,	set_tun_ah_alg,	0,	AF_ANY },
2837c478bd9Sstevel@tonic-gate 	{ "addif",	NEXTARG,	addif,		1,	AF_ANY },
2847c478bd9Sstevel@tonic-gate 	{ "removeif",	NEXTARG,	removeif,	1,	AF_ANY },
2857c478bd9Sstevel@tonic-gate 	{ "modlist",	0,		modlist,	1,	AF_ANY },
2867c478bd9Sstevel@tonic-gate 	{ "modinsert",	NEXTARG,	modinsert,	1,	AF_ANY },
2877c478bd9Sstevel@tonic-gate 	{ "modremove",	NEXTARG,	modremove,	1,	AF_ANY },
2887c478bd9Sstevel@tonic-gate 	{ "failover",	-IFF_NOFAILOVER, setifflags,	1,	AF_ANY },
2897c478bd9Sstevel@tonic-gate 	{ "-failover",	IFF_NOFAILOVER, setifflags,	1,	AF_ANY },
2907c478bd9Sstevel@tonic-gate 	{ "standby",	IFF_STANDBY,	setifflags,	1,	AF_ANY },
2917c478bd9Sstevel@tonic-gate 	{ "-standby",	-IFF_STANDBY,	setifflags,	1,	AF_ANY },
2927c478bd9Sstevel@tonic-gate 	{ "failed",	IFF_FAILED,	setifflags,	1,	AF_ANY },
2937c478bd9Sstevel@tonic-gate 	{ "-failed",	-IFF_FAILED,	setifflags,	1,	AF_ANY },
2947c478bd9Sstevel@tonic-gate 	{ "group",	NEXTARG,	setifgroupname,	1,	AF_ANY },
2957c478bd9Sstevel@tonic-gate 	{ "configinfo",	0,		configinfo,	1,	AF_ANY },
2967906a3e0Smeem 	{ "encaplimit",	NEXTARG,	set_tun_encap_limit,	0, AF_ANY },
2977906a3e0Smeem 	{ "-encaplimit", 0,		clr_tun_encap_limit,	0, AF_ANY },
2987906a3e0Smeem 	{ "thoplimit",	NEXTARG,	set_tun_hop_limit,	0, AF_ANY },
2997c478bd9Sstevel@tonic-gate 	{ "set",	NEXTARG,	setifaddr,	0,	AF_ANY },
3007c478bd9Sstevel@tonic-gate 	{ "destination", NEXTARG,	setifdstaddr,	0,	AF_ANY },
3017c478bd9Sstevel@tonic-gate 	{ "zone",	NEXTARG,	setzone,	0,	AF_ANY },
3027c478bd9Sstevel@tonic-gate 	{ "-zone",	0,		setzone,	0,	AF_ANY },
30345916cd2Sjpk 	{ "all-zones",	0,		setallzones,	0,	AF_ANY },
3047c478bd9Sstevel@tonic-gate 	{ "ether",	OPTARG,		setifether,	0,	AF_ANY },
3057c478bd9Sstevel@tonic-gate 	{ "usesrc",	NEXTARG,	setifsrc,	0,	AF_ANY },
306f7d61273Smeem 
307f7d61273Smeem 	/*
308f7d61273Smeem 	 * NOTE: any additions to this table must also be applied to ifparse
309f7d61273Smeem 	 *	(usr/src/cmd/cmd-inet/sbin/ifparse/ifparse.c)
310f7d61273Smeem 	 */
311f7d61273Smeem 
3127c478bd9Sstevel@tonic-gate 	{ 0,		0,		setifaddr,	0,	AF_ANY },
3137c478bd9Sstevel@tonic-gate 	{ 0,		0,		setifdstaddr,	0,	AF_ANY },
3147c478bd9Sstevel@tonic-gate 	{ 0,		0,		0,		0,	0 },
3157c478bd9Sstevel@tonic-gate };
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate typedef struct if_config_cmd {
3197c478bd9Sstevel@tonic-gate 	uint64_t	iff_flag;
320e11c3f44Smeem 	int		iff_af;
3217c478bd9Sstevel@tonic-gate 	char		*iff_name;
3227c478bd9Sstevel@tonic-gate } if_config_cmd_t;
3237c478bd9Sstevel@tonic-gate 
324e11c3f44Smeem /*
325e11c3f44Smeem  * NOTE: print_config_flags() processes this table in order, so we put "up"
326e11c3f44Smeem  * last so that we can be sure "-failover" will take effect first.  Otherwise,
327e11c3f44Smeem  * IPMP test addresses will erroneously migrate to the IPMP interface.
328e11c3f44Smeem  */
3297c478bd9Sstevel@tonic-gate static if_config_cmd_t	if_config_cmd_tbl[] = {
330e11c3f44Smeem 	{ IFF_NOTRAILERS,	AF_UNSPEC,	"-trailers"	},
331e11c3f44Smeem 	{ IFF_PRIVATE,		AF_UNSPEC,	"private"	},
332e11c3f44Smeem 	{ IFF_NOXMIT,		AF_UNSPEC,	"-xmit"		},
333e11c3f44Smeem 	{ IFF_ANYCAST,		AF_INET6,	"anycast"	},
334e11c3f44Smeem 	{ IFF_NOLOCAL,		AF_UNSPEC,	"-local"	},
335e11c3f44Smeem 	{ IFF_DEPRECATED,	AF_UNSPEC,	"deprecated"	},
336e11c3f44Smeem 	{ IFF_NOFAILOVER,	AF_UNSPEC,	"-failover"	},
337e11c3f44Smeem 	{ IFF_STANDBY,		AF_UNSPEC,	"standby"	},
338e11c3f44Smeem 	{ IFF_FAILED,		AF_UNSPEC,	"failed"	},
339e11c3f44Smeem 	{ IFF_PREFERRED,	AF_UNSPEC,	"preferred"	},
340e11c3f44Smeem 	{ IFF_NONUD,		AF_INET6,	"-nud"		},
341e11c3f44Smeem 	{ IFF_NOARP,		AF_INET,	"-arp"		},
342e11c3f44Smeem 	{ IFF_UP,		AF_UNSPEC, 	"up" 		},
343e11c3f44Smeem 	{ 0,			0,		NULL		},
3447c478bd9Sstevel@tonic-gate };
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate typedef struct ni {
3477c478bd9Sstevel@tonic-gate 	char		ni_name[LIFNAMSIZ];
3487c478bd9Sstevel@tonic-gate 	struct ni	*ni_next;
3497c478bd9Sstevel@tonic-gate } ni_t;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate static ni_t	*ni_list = NULL;
3527c478bd9Sstevel@tonic-gate static int	num_ni = 0;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate /* End defines and structure definitions for ifconfig -a plumb */
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate /* Known address families */
3577c478bd9Sstevel@tonic-gate struct afswtch {
3587c478bd9Sstevel@tonic-gate 	char *af_name;
3597c478bd9Sstevel@tonic-gate 	short af_af;
3607c478bd9Sstevel@tonic-gate 	void (*af_status)();
3617c478bd9Sstevel@tonic-gate 	void (*af_getaddr)();
3627c478bd9Sstevel@tonic-gate 	void (*af_configinfo)();
3637c478bd9Sstevel@tonic-gate } afs[] = {
3647c478bd9Sstevel@tonic-gate 	{ "inet", AF_INET, in_status, in_getaddr, in_configinfo },
3657c478bd9Sstevel@tonic-gate 	{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_configinfo },
3667c478bd9Sstevel@tonic-gate 	{ 0, 0,	0, 0, 0 }
3677c478bd9Sstevel@tonic-gate };
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate struct afswtch *afp;	/* the address family being set or asked about */
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate int
3747c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3757c478bd9Sstevel@tonic-gate {
3762b24ab6bSSebastien Roy 	int64_t		lifc_flags;
3772b24ab6bSSebastien Roy 	char		*default_ip_str;
378*6e91bba0SGirish Moodalbail 	ipadm_status_t	istatus;
3797c478bd9Sstevel@tonic-gate 
380*6e91bba0SGirish Moodalbail 	lifc_flags = LIFC_DEFAULT;
381e11c3f44Smeem 
3827c478bd9Sstevel@tonic-gate 	if (argc < 2) {
3837c478bd9Sstevel@tonic-gate 		usage();
3847c478bd9Sstevel@tonic-gate 		exit(1);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 	argc--, argv++;
3877c478bd9Sstevel@tonic-gate 	if (strlen(*argv) > sizeof (name) - 1) {
3887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: interface name too long\n", *argv);
3897c478bd9Sstevel@tonic-gate 		exit(1);
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 	(void) strncpy(name, *argv, sizeof (name));
3927c478bd9Sstevel@tonic-gate 	name[sizeof (name) - 1] = '\0';
3937c478bd9Sstevel@tonic-gate 	(void) strncpy(origname, name, sizeof (origname));	/* For addif */
3947c478bd9Sstevel@tonic-gate 	default_ip_str = NULL;
3957c478bd9Sstevel@tonic-gate 	v4compat = get_compat_flag(&default_ip_str);
3967c478bd9Sstevel@tonic-gate 	if (v4compat == DEFAULT_PROT_BAD_VALUE) {
3977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3987c478bd9Sstevel@tonic-gate 		    "ifconfig: %s: Bad value for %s in %s\n", default_ip_str,
3997c478bd9Sstevel@tonic-gate 		    DEFAULT_IP, INET_DEFAULT_FILE);
4007c478bd9Sstevel@tonic-gate 		free(default_ip_str);
4017c478bd9Sstevel@tonic-gate 		exit(2);
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 	free(default_ip_str);
4047c478bd9Sstevel@tonic-gate 	argc--, argv++;
4057c478bd9Sstevel@tonic-gate 	if (argc > 0) {
4067c478bd9Sstevel@tonic-gate 		struct afswtch *myafp;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		for (myafp = afp = afs; myafp->af_name; myafp++) {
4097c478bd9Sstevel@tonic-gate 			if (strcmp(myafp->af_name, *argv) == 0) {
4107c478bd9Sstevel@tonic-gate 				afp = myafp; argc--; argv++;
4117c478bd9Sstevel@tonic-gate 				break;
4127c478bd9Sstevel@tonic-gate 			}
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 		af = lifr.lifr_addr.ss_family = afp->af_af;
4157c478bd9Sstevel@tonic-gate 		if (af == AF_INET6) {
4167c478bd9Sstevel@tonic-gate 			v4compat = 0;
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	s = socket(SOCKET_AF(af), SOCK_DGRAM, 0);
421e11c3f44Smeem 	s4 = socket(AF_INET, SOCK_DGRAM, 0);
422e11c3f44Smeem 	s6 = socket(AF_INET6, SOCK_DGRAM, 0);
423e11c3f44Smeem 	if (s == -1 || s4 == -1 || s6 == -1)
4247c478bd9Sstevel@tonic-gate 		Perror0_exit("socket");
425*6e91bba0SGirish Moodalbail 	/*
426*6e91bba0SGirish Moodalbail 	 * Open the global libipadm handle. The flag IPH_LEGACY has to
427*6e91bba0SGirish Moodalbail 	 * be specified to indicate that logical interface names will
428*6e91bba0SGirish Moodalbail 	 * be used during interface creation and address creation.
429*6e91bba0SGirish Moodalbail 	 */
430*6e91bba0SGirish Moodalbail 	if ((istatus = ipadm_open(&iph, IPH_LEGACY)) != IPADM_SUCCESS)
431*6e91bba0SGirish Moodalbail 		ipadmerr_exit(istatus, "unable to open handle to libipadm");
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	/*
4347c478bd9Sstevel@tonic-gate 	 * Special interface names is any combination of these flags.
4357c478bd9Sstevel@tonic-gate 	 * Note that due to the ifconfig syntax they have to be combined
4367c478bd9Sstevel@tonic-gate 	 * as a single '-' option.
4377c478bd9Sstevel@tonic-gate 	 *	-a	All interfaces
4387c478bd9Sstevel@tonic-gate 	 *	-u	"up" interfaces
4397c478bd9Sstevel@tonic-gate 	 *	-d	"down" interfaces
4407c478bd9Sstevel@tonic-gate 	 *	-D	Interfaces not controlled by DHCP
4417c478bd9Sstevel@tonic-gate 	 *	-4	IPv4 interfaces
4427c478bd9Sstevel@tonic-gate 	 *	-6	IPv6 interfaces
4437c478bd9Sstevel@tonic-gate 	 *	-X	Turn on debug (not documented)
4447c478bd9Sstevel@tonic-gate 	 *	-v	Turn on verbose
4457c478bd9Sstevel@tonic-gate 	 *	-Z	Only interfaces in caller's zone
4467c478bd9Sstevel@tonic-gate 	 */
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	if (name[0] == '-') {
4497c478bd9Sstevel@tonic-gate 		/* One or more options */
4507c478bd9Sstevel@tonic-gate 		int64_t onflags = 0;
4517c478bd9Sstevel@tonic-gate 		int64_t offflags = 0;
4527c478bd9Sstevel@tonic-gate 		int c;
4537c478bd9Sstevel@tonic-gate 		char *av[2] = { "ifconfig", name };
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		while ((c = getopt(2, av, "audDXZ46v")) != -1) {
4567c478bd9Sstevel@tonic-gate 			switch ((char)c) {
4577c478bd9Sstevel@tonic-gate 			case 'a':
4587c478bd9Sstevel@tonic-gate 				all = 1;
4597c478bd9Sstevel@tonic-gate 				break;
4607c478bd9Sstevel@tonic-gate 			case 'u':
4617c478bd9Sstevel@tonic-gate 				onflags |= IFF_UP;
4627c478bd9Sstevel@tonic-gate 				break;
4637c478bd9Sstevel@tonic-gate 			case 'd':
4647c478bd9Sstevel@tonic-gate 				offflags |= IFF_UP;
4657c478bd9Sstevel@tonic-gate 				break;
4667c478bd9Sstevel@tonic-gate 			case 'D':
4677c478bd9Sstevel@tonic-gate 				offflags |= IFF_DHCPRUNNING;
4687c478bd9Sstevel@tonic-gate 				break;
4697c478bd9Sstevel@tonic-gate 			case 'X':
4707c478bd9Sstevel@tonic-gate 				debug += 3;
4717c478bd9Sstevel@tonic-gate 				break;
4727c478bd9Sstevel@tonic-gate 			case 'Z':
4737c478bd9Sstevel@tonic-gate 				lifc_flags &= ~LIFC_ALLZONES;
4747c478bd9Sstevel@tonic-gate 				break;
4757c478bd9Sstevel@tonic-gate 			case '4':
4767c478bd9Sstevel@tonic-gate 				/*
4777c478bd9Sstevel@tonic-gate 				 * -4 is not a compatable flag, therefore
4787c478bd9Sstevel@tonic-gate 				 * we assume they want v4compat turned off
4797c478bd9Sstevel@tonic-gate 				 */
4807c478bd9Sstevel@tonic-gate 				v4compat = 0;
4817c478bd9Sstevel@tonic-gate 				onflags |= IFF_IPV4;
4827c478bd9Sstevel@tonic-gate 				break;
4837c478bd9Sstevel@tonic-gate 			case '6':
4847c478bd9Sstevel@tonic-gate 				/*
4857c478bd9Sstevel@tonic-gate 				 * If they want IPv6, well then we'll assume
4867c478bd9Sstevel@tonic-gate 				 * they don't want IPv4 compat
4877c478bd9Sstevel@tonic-gate 				 */
4887c478bd9Sstevel@tonic-gate 				v4compat = 0;
4897c478bd9Sstevel@tonic-gate 				onflags |= IFF_IPV6;
4907c478bd9Sstevel@tonic-gate 				break;
4917c478bd9Sstevel@tonic-gate 			case 'v':
4927c478bd9Sstevel@tonic-gate 				verbose = 1;
4937c478bd9Sstevel@tonic-gate 				break;
4947c478bd9Sstevel@tonic-gate 			case '?':
4957c478bd9Sstevel@tonic-gate 				usage();
4967c478bd9Sstevel@tonic-gate 				exit(1);
4977c478bd9Sstevel@tonic-gate 			}
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		if (!all) {
5007c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
501c7e4935fSss 			    "ifconfig: %s: no such interface\n", name);
5027c478bd9Sstevel@tonic-gate 			exit(1);
5037c478bd9Sstevel@tonic-gate 		}
504*6e91bba0SGirish Moodalbail 		foreachinterface(argc, argv, af, onflags, offflags,
5057c478bd9Sstevel@tonic-gate 		    lifc_flags);
5067c478bd9Sstevel@tonic-gate 	} else {
507*6e91bba0SGirish Moodalbail 		ifconfig(argc, argv, af, NULL);
5087c478bd9Sstevel@tonic-gate 	}
509*6e91bba0SGirish Moodalbail 	ipadm_close(iph);
5107c478bd9Sstevel@tonic-gate 	return (0);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate /*
514*6e91bba0SGirish Moodalbail  * For each interface, call ifconfig(argc, argv, af, ifa).
5157c478bd9Sstevel@tonic-gate  * Only call function if onflags and offflags are set or clear, respectively,
5167c478bd9Sstevel@tonic-gate  * in the interfaces flags field.
5177c478bd9Sstevel@tonic-gate  */
5187c478bd9Sstevel@tonic-gate static void
519*6e91bba0SGirish Moodalbail foreachinterface(int argc, char *argv[], int af,
5207c478bd9Sstevel@tonic-gate     int64_t onflags, int64_t offflags, int64_t lifc_flags)
5217c478bd9Sstevel@tonic-gate {
522*6e91bba0SGirish Moodalbail 	ipadm_addr_info_t *ainfo, *ainfop;
523*6e91bba0SGirish Moodalbail 	struct ifaddrs *ifa;
524*6e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	/*
5277c478bd9Sstevel@tonic-gate 	 * Special case:
5282b24ab6bSSebastien Roy 	 * ifconfig -a plumb should find all network interfaces in the current
5292b24ab6bSSebastien Roy 	 * zone.
5307c478bd9Sstevel@tonic-gate 	 */
5317c478bd9Sstevel@tonic-gate 	if (argc > 0 && (strcmp(*argv, "plumb") == 0)) {
532*6e91bba0SGirish Moodalbail 		plumball(argc, argv, onflags, offflags, lifc_flags);
533*6e91bba0SGirish Moodalbail 		return;
5347c478bd9Sstevel@tonic-gate 	}
535*6e91bba0SGirish Moodalbail 	/* Get all addresses in kernel including addresses that are zero. */
536*6e91bba0SGirish Moodalbail 	istatus = ipadm_addr_info(iph, NULL, &ainfo, IPADM_OPT_ZEROADDR,
537*6e91bba0SGirish Moodalbail 	    lifc_flags);
538*6e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS)
539*6e91bba0SGirish Moodalbail 		ipadmerr_exit(istatus, "could not get addresses from kernel");
5407c478bd9Sstevel@tonic-gate 
541*6e91bba0SGirish Moodalbail 	/*
542*6e91bba0SGirish Moodalbail 	 * For each logical interface, call ifconfig() with the
543*6e91bba0SGirish Moodalbail 	 * given arguments.
544*6e91bba0SGirish Moodalbail 	 */
545*6e91bba0SGirish Moodalbail 	for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop)) {
546*6e91bba0SGirish Moodalbail 		if (ainfop->ia_state == IFA_DISABLED)
547*6e91bba0SGirish Moodalbail 			continue;
548*6e91bba0SGirish Moodalbail 		ifa = &ainfop->ia_ifa;
5497c478bd9Sstevel@tonic-gate 		if (onflags || offflags) {
550*6e91bba0SGirish Moodalbail 			if ((ifa->ifa_flags & onflags) != onflags)
5517c478bd9Sstevel@tonic-gate 				continue;
552*6e91bba0SGirish Moodalbail 			if ((~ifa->ifa_flags & offflags) != offflags)
5537c478bd9Sstevel@tonic-gate 				continue;
5547c478bd9Sstevel@tonic-gate 		}
555*6e91bba0SGirish Moodalbail 		s = (ifa->ifa_addr->ss_family == AF_INET ? s4 : s6);
556*6e91bba0SGirish Moodalbail 		(void) strncpy(name, ifa->ifa_name, sizeof (name));
557*6e91bba0SGirish Moodalbail 		(void) strncpy(origname, name, sizeof (origname));
558*6e91bba0SGirish Moodalbail 		ifconfig(argc, argv, af, ifa);
559*6e91bba0SGirish Moodalbail 	}
560*6e91bba0SGirish Moodalbail 	ipadm_free_addr_info(ainfo);
561*6e91bba0SGirish Moodalbail }
5627c478bd9Sstevel@tonic-gate 
563*6e91bba0SGirish Moodalbail /*
564*6e91bba0SGirish Moodalbail  * Used for `ifconfig -a plumb'. Finds all datalinks and plumbs the interface.
565*6e91bba0SGirish Moodalbail  */
566*6e91bba0SGirish Moodalbail static void
567*6e91bba0SGirish Moodalbail plumball(int argc, char *argv[], int64_t onflags, int64_t offflags,
568*6e91bba0SGirish Moodalbail     int64_t lifc_flags)
569*6e91bba0SGirish Moodalbail {
570*6e91bba0SGirish Moodalbail 	int n;
571*6e91bba0SGirish Moodalbail 	struct lifreq *lifrp;
572*6e91bba0SGirish Moodalbail 	struct lifconf lifc;
573*6e91bba0SGirish Moodalbail 	char *buf;
5747c478bd9Sstevel@tonic-gate 
575*6e91bba0SGirish Moodalbail 	if (onflags != 0 || offflags != 0) {
576*6e91bba0SGirish Moodalbail 		(void) fprintf(stderr, "ifconfig: invalid syntax used to "
577*6e91bba0SGirish Moodalbail 		    "plumb all interfaces.\n");
578*6e91bba0SGirish Moodalbail 		exit(1);
579*6e91bba0SGirish Moodalbail 	}
580*6e91bba0SGirish Moodalbail 
581*6e91bba0SGirish Moodalbail 	if (find_all_interfaces(&lifc, &buf, lifc_flags) != 0 ||
582*6e91bba0SGirish Moodalbail 	    lifc.lifc_len == 0)
583*6e91bba0SGirish Moodalbail 		return;
5847c478bd9Sstevel@tonic-gate 
585*6e91bba0SGirish Moodalbail 	lifrp = lifc.lifc_req;
586*6e91bba0SGirish Moodalbail 	for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) {
5877c478bd9Sstevel@tonic-gate 		/*
5887c478bd9Sstevel@tonic-gate 		 * Reset global state
5897c478bd9Sstevel@tonic-gate 		 * setaddr: Used by parser to tear apart source and dest
5907c478bd9Sstevel@tonic-gate 		 * name and origname contain the name of the 'current'
5917c478bd9Sstevel@tonic-gate 		 * interface.
5927c478bd9Sstevel@tonic-gate 		 */
5937c478bd9Sstevel@tonic-gate 		setaddr = 0;
5947c478bd9Sstevel@tonic-gate 		(void) strncpy(name, lifrp->lifr_name, sizeof (name));
5957c478bd9Sstevel@tonic-gate 		(void) strncpy(origname, name, sizeof (origname));
596*6e91bba0SGirish Moodalbail 		ifconfig(argc, argv, af, NULL);
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate /*
601*6e91bba0SGirish Moodalbail  * Parses the interface name and the command in argv[]. Calls the
602*6e91bba0SGirish Moodalbail  * appropriate callback function for the given command from `cmds[]'
603*6e91bba0SGirish Moodalbail  * table.
604*6e91bba0SGirish Moodalbail  * If there is no command specified, it prints all addresses.
6057c478bd9Sstevel@tonic-gate  */
6067c478bd9Sstevel@tonic-gate static void
607*6e91bba0SGirish Moodalbail ifconfig(int argc, char *argv[], int af, struct ifaddrs *ifa)
6087c478bd9Sstevel@tonic-gate {
6097c478bd9Sstevel@tonic-gate 	static boolean_t scan_netmask = _B_FALSE;
6107c478bd9Sstevel@tonic-gate 	int ret;
611*6e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
612*6e91bba0SGirish Moodalbail 	struct lifreq lifr;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if (argc == 0) {
6157c478bd9Sstevel@tonic-gate 		status();
6167c478bd9Sstevel@tonic-gate 		return;
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	if (strcmp(*argv, "auto-dhcp") == 0 || strcmp(*argv, "dhcp") == 0) {
620d04ccbb3Scarlsonj 		/*
621d04ccbb3Scarlsonj 		 * Some errors are ignored in the case where more than one
622d04ccbb3Scarlsonj 		 * interface is being operated on.
623d04ccbb3Scarlsonj 		 */
624d04ccbb3Scarlsonj 		ret = setifdhcp("ifconfig", name, argc, argv);
625d04ccbb3Scarlsonj 		if (ret == DHCP_EXIT_IF_FAILURE) {
626d04ccbb3Scarlsonj 			if (!all)
627d04ccbb3Scarlsonj 				exit(DHCP_EXIT_FAILURE);
628d04ccbb3Scarlsonj 		} else if (ret != DHCP_EXIT_SUCCESS) {
629d04ccbb3Scarlsonj 			exit(ret);
630d04ccbb3Scarlsonj 		}
6317c478bd9Sstevel@tonic-gate 		return;
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	/*
6357c478bd9Sstevel@tonic-gate 	 * The following is a "hack" to get around the existing interface
6367c478bd9Sstevel@tonic-gate 	 * setting mechanism.  Currently, each interface attribute,
6377c478bd9Sstevel@tonic-gate 	 * such as address, netmask, broadcast, ... is set separately.  But
6387c478bd9Sstevel@tonic-gate 	 * sometimes two or more attributes must be set together.  For
6397c478bd9Sstevel@tonic-gate 	 * example, setting an address without a netmask does not make sense.
6407c478bd9Sstevel@tonic-gate 	 * Yet they can be set separately for IPv4 address using the current
6417c478bd9Sstevel@tonic-gate 	 * ifconfig(1M) syntax.  The kernel then "infers" the correct netmask
6427c478bd9Sstevel@tonic-gate 	 * using the deprecated "IP address classes."  This is simply not
6437c478bd9Sstevel@tonic-gate 	 * correct.
6447c478bd9Sstevel@tonic-gate 	 *
6457c478bd9Sstevel@tonic-gate 	 * The "hack" below is to go thru the whole command list looking for
6467c478bd9Sstevel@tonic-gate 	 * the netmask command first.  Then use this netmask to set the
6477c478bd9Sstevel@tonic-gate 	 * address.  This does not provide an extensible way to accommodate
6487c478bd9Sstevel@tonic-gate 	 * future need for setting more than one attributes together.
6497c478bd9Sstevel@tonic-gate 	 *
650dd7a6f5fSkcpoon 	 * Note that if the "netmask" command argument is a "+", we need
651dd7a6f5fSkcpoon 	 * to save this info and do the query after we know the address to
652dd7a6f5fSkcpoon 	 * be set.  The reason is that if "addif" is used, the working
653dd7a6f5fSkcpoon 	 * interface name will be changed later when the logical interface
654dd7a6f5fSkcpoon 	 * is created.  In in_getmask(), if an address is not provided,
655dd7a6f5fSkcpoon 	 * it will use the working interface's address to do the query.
656dd7a6f5fSkcpoon 	 * It will be wrong now as we don't know the logical interface's name.
657dd7a6f5fSkcpoon 	 *
6587c478bd9Sstevel@tonic-gate 	 * ifconfig(1M) is too overloaded and the code is so convoluted
6597c478bd9Sstevel@tonic-gate 	 * that it is "safer" not to re-architect the code to fix the above
6607c478bd9Sstevel@tonic-gate 	 * issue, hence this "hack."  We may be better off to have a new
6617c478bd9Sstevel@tonic-gate 	 * command with better syntax for configuring network interface
6627c478bd9Sstevel@tonic-gate 	 * parameters...
6637c478bd9Sstevel@tonic-gate 	 */
6647c478bd9Sstevel@tonic-gate 	if (!scan_netmask && afp->af_af == AF_INET) {
6657c478bd9Sstevel@tonic-gate 		int	largc;
6667c478bd9Sstevel@tonic-gate 		char	**largv;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 		/* Only go thru the command list once to find the netmask. */
6697c478bd9Sstevel@tonic-gate 		scan_netmask = _B_TRUE;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 		/*
6727c478bd9Sstevel@tonic-gate 		 * Currently, if multiple netmask commands are specified, the
6737c478bd9Sstevel@tonic-gate 		 * last one will be used as the final netmask.  So we need
6747c478bd9Sstevel@tonic-gate 		 * to scan the whole list to preserve this behavior.
6757c478bd9Sstevel@tonic-gate 		 */
6767c478bd9Sstevel@tonic-gate 		for (largc = argc, largv = argv; largc > 0; largc--, largv++) {
6777c478bd9Sstevel@tonic-gate 			if (strcmp(*largv, NETMASK_CMD) == 0) {
6787c478bd9Sstevel@tonic-gate 				if (--largc == 0)
6797c478bd9Sstevel@tonic-gate 					break;
6807c478bd9Sstevel@tonic-gate 				largv++;
6817c478bd9Sstevel@tonic-gate 				if (strcmp(*largv, "+") == 0) {
682dd7a6f5fSkcpoon 					g_netmask_set = G_NETMASK_PENDING;
6837c478bd9Sstevel@tonic-gate 				} else {
6847c478bd9Sstevel@tonic-gate 					in_getaddr(*largv, (struct sockaddr *)
6857c478bd9Sstevel@tonic-gate 					    &g_netmask, NULL);
686dd7a6f5fSkcpoon 					g_netmask_set = G_NETMASK_SET;
6877c478bd9Sstevel@tonic-gate 				}
6887c478bd9Sstevel@tonic-gate 				/* Continue the scan. */
6897c478bd9Sstevel@tonic-gate 			}
6907c478bd9Sstevel@tonic-gate 		}
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	while (argc > 0) {
6947c478bd9Sstevel@tonic-gate 		struct cmd *p;
6957c478bd9Sstevel@tonic-gate 		boolean_t found_cmd;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 		if (debug)
6987c478bd9Sstevel@tonic-gate 			(void) printf("ifconfig: argv %s\n", *argv);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		found_cmd = _B_FALSE;
7017c478bd9Sstevel@tonic-gate 		for (p = cmds; p->c_func; p++) {
7027c478bd9Sstevel@tonic-gate 			if (p->c_name) {
7037c478bd9Sstevel@tonic-gate 				if (strcmp(*argv, p->c_name) == 0) {
7047c478bd9Sstevel@tonic-gate 					/*
7057c478bd9Sstevel@tonic-gate 					 * indicate that the command was
7067c478bd9Sstevel@tonic-gate 					 * found and check to see if
7077c478bd9Sstevel@tonic-gate 					 * the address family is valid
7087c478bd9Sstevel@tonic-gate 					 */
7097c478bd9Sstevel@tonic-gate 					found_cmd = _B_TRUE;
7107c478bd9Sstevel@tonic-gate 					if (p->c_af == AF_ANY ||
7117c478bd9Sstevel@tonic-gate 					    af == p->c_af)
7127c478bd9Sstevel@tonic-gate 						break;
7137c478bd9Sstevel@tonic-gate 				}
7147c478bd9Sstevel@tonic-gate 			} else {
7157c478bd9Sstevel@tonic-gate 				if (p->c_af == AF_ANY ||
7167c478bd9Sstevel@tonic-gate 				    af == p->c_af)
7177c478bd9Sstevel@tonic-gate 					break;
7187c478bd9Sstevel@tonic-gate 			}
7197c478bd9Sstevel@tonic-gate 		}
7207c478bd9Sstevel@tonic-gate 		/*
7217c478bd9Sstevel@tonic-gate 		 * If we found the keyword, but the address family
7227c478bd9Sstevel@tonic-gate 		 * did not match spit out an error
7237c478bd9Sstevel@tonic-gate 		 */
7247c478bd9Sstevel@tonic-gate 		if (found_cmd && p->c_name == 0) {
7257c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: Operation %s not"
7267c478bd9Sstevel@tonic-gate 			    " supported for %s\n", *argv, afp->af_name);
7277c478bd9Sstevel@tonic-gate 			exit(1);
7287c478bd9Sstevel@tonic-gate 		}
7297c478bd9Sstevel@tonic-gate 		/*
7307c478bd9Sstevel@tonic-gate 		 * else (no keyword found), we assume it's an address
7317c478bd9Sstevel@tonic-gate 		 * of some sort
7327c478bd9Sstevel@tonic-gate 		 */
733*6e91bba0SGirish Moodalbail 		if (setaddr && ipaddr != NULL) {
734*6e91bba0SGirish Moodalbail 			/*
735*6e91bba0SGirish Moodalbail 			 * We must have already filled in a source address in
736*6e91bba0SGirish Moodalbail 			 * `ipaddr' and we now got a destination address.
737*6e91bba0SGirish Moodalbail 			 * Fill it in `ipaddr' and call libipadm to create
738*6e91bba0SGirish Moodalbail 			 * the static address.
739*6e91bba0SGirish Moodalbail 			 */
740*6e91bba0SGirish Moodalbail 			if (p->c_name == 0) {
741*6e91bba0SGirish Moodalbail 				istatus = ipadm_set_dst_addr(ipaddr, *argv,
742*6e91bba0SGirish Moodalbail 				    (p->c_af == AF_ANY ? AF_UNSPEC : af));
743*6e91bba0SGirish Moodalbail 				if (istatus != IPADM_SUCCESS) {
744*6e91bba0SGirish Moodalbail 					ipadmerr_exit(istatus, "could not "
745*6e91bba0SGirish Moodalbail 					    "set destination address");
746*6e91bba0SGirish Moodalbail 				}
747*6e91bba0SGirish Moodalbail 				/*
748*6e91bba0SGirish Moodalbail 				 * finished processing dstaddr, so reset setaddr
749*6e91bba0SGirish Moodalbail 				 */
750*6e91bba0SGirish Moodalbail 				setaddr = 0;
751*6e91bba0SGirish Moodalbail 			}
752*6e91bba0SGirish Moodalbail 			/*
753*6e91bba0SGirish Moodalbail 			 * Both source and destination address are in `ipaddr'.
754*6e91bba0SGirish Moodalbail 			 * Add the address by calling libipadm.
755*6e91bba0SGirish Moodalbail 			 */
756*6e91bba0SGirish Moodalbail 			istatus = ipadm_create_addr(iph, ipaddr,
757*6e91bba0SGirish Moodalbail 			    IPADM_OPT_ACTIVE);
758*6e91bba0SGirish Moodalbail 			if (istatus != IPADM_SUCCESS)
759*6e91bba0SGirish Moodalbail 				goto createfailed;
760*6e91bba0SGirish Moodalbail 			ipadm_destroy_addrobj(ipaddr);
761*6e91bba0SGirish Moodalbail 			ipaddr = NULL;
762*6e91bba0SGirish Moodalbail 			setaddr_done = _B_TRUE;
763*6e91bba0SGirish Moodalbail 			if (p->c_name == 0) {
764*6e91bba0SGirish Moodalbail 				/* move parser along */
765*6e91bba0SGirish Moodalbail 				argc--, argv++;
766*6e91bba0SGirish Moodalbail 				continue;
767*6e91bba0SGirish Moodalbail 			}
768*6e91bba0SGirish Moodalbail 		}
769*6e91bba0SGirish Moodalbail 		if (p->c_name == 0 && setaddr_done) {
770*6e91bba0SGirish Moodalbail 			/*
771*6e91bba0SGirish Moodalbail 			 * catch odd commands like
772*6e91bba0SGirish Moodalbail 			 * "ifconfig <intf> addr1 addr2 addr3 addr4 up"
773*6e91bba0SGirish Moodalbail 			 */
774*6e91bba0SGirish Moodalbail 			(void) fprintf(stderr, "%s",
775*6e91bba0SGirish Moodalbail 			    "ifconfig: cannot configure more than two "
776*6e91bba0SGirish Moodalbail 			    "addresses in one command\n");
777*6e91bba0SGirish Moodalbail 			exit(1);
778*6e91bba0SGirish Moodalbail 		}
7797c478bd9Sstevel@tonic-gate 		if (p->c_func) {
7807c478bd9Sstevel@tonic-gate 			if (p->c_af == AF_INET6) {
7817c478bd9Sstevel@tonic-gate 				v4compat = 0;
7827c478bd9Sstevel@tonic-gate 			}
7837c478bd9Sstevel@tonic-gate 			if (p->c_parameter == NEXTARG ||
7847c478bd9Sstevel@tonic-gate 			    p->c_parameter == OPTARG) {
7857c478bd9Sstevel@tonic-gate 				argc--, argv++;
7867c478bd9Sstevel@tonic-gate 				if (argc == 0 && p->c_parameter == NEXTARG) {
7877c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7887c478bd9Sstevel@tonic-gate 					    "ifconfig: no argument for %s\n",
7897c478bd9Sstevel@tonic-gate 					    p->c_name);
7907c478bd9Sstevel@tonic-gate 					exit(1);
7917c478bd9Sstevel@tonic-gate 				}
7927c478bd9Sstevel@tonic-gate 			}
7937c478bd9Sstevel@tonic-gate 			/*
7947c478bd9Sstevel@tonic-gate 			 *	Call the function if:
7957c478bd9Sstevel@tonic-gate 			 *
7967c478bd9Sstevel@tonic-gate 			 *		there's no address family
7977c478bd9Sstevel@tonic-gate 			 *		restriction
7987c478bd9Sstevel@tonic-gate 			 *	OR
7997c478bd9Sstevel@tonic-gate 			 *		we don't know the address yet
8007c478bd9Sstevel@tonic-gate 			 *		(because we were called from
8017c478bd9Sstevel@tonic-gate 			 *		main)
8027c478bd9Sstevel@tonic-gate 			 *	OR
8037c478bd9Sstevel@tonic-gate 			 *		there is a restriction AND
8047c478bd9Sstevel@tonic-gate 			 *		the address families match
8057c478bd9Sstevel@tonic-gate 			 */
8067c478bd9Sstevel@tonic-gate 			if ((p->c_af == AF_ANY)	||
807*6e91bba0SGirish Moodalbail 			    (ifa == NULL) ||
808*6e91bba0SGirish Moodalbail 			    (ifa->ifa_addr->ss_family == p->c_af)) {
8097c478bd9Sstevel@tonic-gate 				ret = (*p->c_func)(*argv, p->c_parameter);
8107c478bd9Sstevel@tonic-gate 				/*
8117c478bd9Sstevel@tonic-gate 				 *	If c_func failed and we should
8127c478bd9Sstevel@tonic-gate 				 *	abort processing for this
8137c478bd9Sstevel@tonic-gate 				 *	interface on failure, return
8147c478bd9Sstevel@tonic-gate 				 *	now rather than going on to
8157c478bd9Sstevel@tonic-gate 				 *	process other commands for
8167c478bd9Sstevel@tonic-gate 				 *	the same interface.
8177c478bd9Sstevel@tonic-gate 				 */
8187c478bd9Sstevel@tonic-gate 				if (ret != 0 && p->c_abortonfail)
8197c478bd9Sstevel@tonic-gate 					return;
8207c478bd9Sstevel@tonic-gate 			}
8217c478bd9Sstevel@tonic-gate 		}
8227c478bd9Sstevel@tonic-gate 		argc--, argv++;
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
825*6e91bba0SGirish Moodalbail 	if (setaddr && ipaddr != NULL) {
826*6e91bba0SGirish Moodalbail 		/*
827*6e91bba0SGirish Moodalbail 		 * Only the source address was provided, which was already
828*6e91bba0SGirish Moodalbail 		 * set in `ipaddr'. Add the address by calling libipadm.
829*6e91bba0SGirish Moodalbail 		 */
830*6e91bba0SGirish Moodalbail 		istatus = ipadm_create_addr(iph, ipaddr, IPADM_OPT_ACTIVE);
831*6e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
832*6e91bba0SGirish Moodalbail 			goto createfailed;
833*6e91bba0SGirish Moodalbail 		ipadm_destroy_addrobj(ipaddr);
834*6e91bba0SGirish Moodalbail 		ipaddr = NULL;
835*6e91bba0SGirish Moodalbail 		setaddr_done = _B_TRUE;
836*6e91bba0SGirish Moodalbail 	}
837*6e91bba0SGirish Moodalbail 
8387c478bd9Sstevel@tonic-gate 	/* Check to see if there's a security hole in the tunnel setup. */
8392b24ab6bSSebastien Roy 	if (ipsec_policy_set && !ipsec_auth_covered) {
8402b24ab6bSSebastien Roy 		(void) fprintf(stderr, "ifconfig: WARNING: tunnel with only "
8412b24ab6bSSebastien Roy 		    "ESP and no authentication.\n");
8422b24ab6bSSebastien Roy 	}
843*6e91bba0SGirish Moodalbail 	return;
844*6e91bba0SGirish Moodalbail 
845*6e91bba0SGirish Moodalbail createfailed:
846*6e91bba0SGirish Moodalbail 	(void) fprintf(stderr, "ifconfig: could not create address:% s\n",
847*6e91bba0SGirish Moodalbail 	    ipadm_status2str(istatus));
848*6e91bba0SGirish Moodalbail 	/* Remove the newly created logical interface. */
849*6e91bba0SGirish Moodalbail 	if (strcmp(name, origname) != 0) {
850*6e91bba0SGirish Moodalbail 		assert(strchr(name, ':') != NULL);
851*6e91bba0SGirish Moodalbail 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
852*6e91bba0SGirish Moodalbail 		(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
853*6e91bba0SGirish Moodalbail 	}
854*6e91bba0SGirish Moodalbail 	exit(1);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate /* ARGSUSED */
8587c478bd9Sstevel@tonic-gate static int
8597c478bd9Sstevel@tonic-gate setdebugflag(char *val, int64_t arg)
8607c478bd9Sstevel@tonic-gate {
8617c478bd9Sstevel@tonic-gate 	debug++;
8627c478bd9Sstevel@tonic-gate 	return (0);
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate /* ARGSUSED */
8667c478bd9Sstevel@tonic-gate static int
8677c478bd9Sstevel@tonic-gate setverboseflag(char *val, int64_t arg)
8687c478bd9Sstevel@tonic-gate {
8697c478bd9Sstevel@tonic-gate 	verbose++;
8707c478bd9Sstevel@tonic-gate 	return (0);
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate 
873dd7a6f5fSkcpoon /*
874dd7a6f5fSkcpoon  * This function fills in the given lifreq's lifr_addr field based on
875dd7a6f5fSkcpoon  * g_netmask_set.
876dd7a6f5fSkcpoon  */
877dd7a6f5fSkcpoon static void
878dd7a6f5fSkcpoon set_mask_lifreq(struct lifreq *lifr, struct sockaddr_storage *addr,
879dd7a6f5fSkcpoon     struct sockaddr_storage *mask)
880dd7a6f5fSkcpoon {
881dd7a6f5fSkcpoon 	assert(addr != NULL);
882dd7a6f5fSkcpoon 	assert(mask != NULL);
883dd7a6f5fSkcpoon 
884dd7a6f5fSkcpoon 	switch (g_netmask_set) {
885dd7a6f5fSkcpoon 	case G_NETMASK_SET:
886dd7a6f5fSkcpoon 		lifr->lifr_addr = g_netmask;
887dd7a6f5fSkcpoon 		break;
888dd7a6f5fSkcpoon 
889dd7a6f5fSkcpoon 	case G_NETMASK_PENDING:
890dd7a6f5fSkcpoon 		/*
891dd7a6f5fSkcpoon 		 * "+" is used as the argument to "netmask" command.  Query
892dd7a6f5fSkcpoon 		 * the database on the correct netmask based on the address to
893dd7a6f5fSkcpoon 		 * be set.
894dd7a6f5fSkcpoon 		 */
895dd7a6f5fSkcpoon 		assert(afp->af_af == AF_INET);
896dd7a6f5fSkcpoon 		g_netmask = *addr;
897dd7a6f5fSkcpoon 		if (!in_getmask((struct sockaddr_in *)&g_netmask, _B_TRUE)) {
898dd7a6f5fSkcpoon 			lifr->lifr_addr = *mask;
899dd7a6f5fSkcpoon 			g_netmask_set = G_NETMASK_NIL;
900dd7a6f5fSkcpoon 		} else {
901dd7a6f5fSkcpoon 			lifr->lifr_addr = g_netmask;
902dd7a6f5fSkcpoon 			g_netmask_set = G_NETMASK_SET;
903dd7a6f5fSkcpoon 		}
904dd7a6f5fSkcpoon 		break;
905dd7a6f5fSkcpoon 
906dd7a6f5fSkcpoon 	case G_NETMASK_NIL:
907dd7a6f5fSkcpoon 	default:
908dd7a6f5fSkcpoon 		lifr->lifr_addr = *mask;
909dd7a6f5fSkcpoon 		break;
910dd7a6f5fSkcpoon 	}
911dd7a6f5fSkcpoon }
912dd7a6f5fSkcpoon 
9137c478bd9Sstevel@tonic-gate /*
9147c478bd9Sstevel@tonic-gate  * Set the interface address. Handles <addr>, <addr>/<n> as well as /<n>
9157c478bd9Sstevel@tonic-gate  * syntax for setting the address, the address plus netmask, and just
9167c478bd9Sstevel@tonic-gate  * the netmask respectively.
9177c478bd9Sstevel@tonic-gate  */
9187c478bd9Sstevel@tonic-gate /* ARGSUSED */
9197c478bd9Sstevel@tonic-gate static int
9207c478bd9Sstevel@tonic-gate setifaddr(char *addr, int64_t param)
9217c478bd9Sstevel@tonic-gate {
922*6e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
9237c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
924*6e91bba0SGirish Moodalbail 	struct  lifreq lifr1;
9257c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage laddr;
9267c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage netmask;
9277c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 *sin6;
9287c478bd9Sstevel@tonic-gate 	struct	sockaddr_in *sin;
9297c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage sav_netmask;
930*6e91bba0SGirish Moodalbail 	char cidraddr[BUFSIZ];
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	if (addr[0] == '/')
9337c478bd9Sstevel@tonic-gate 		return (setifprefixlen(addr, 0));
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, (struct sockaddr *)&laddr, &prefixlen);
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	(void) memset(&netmask, 0, sizeof (netmask));
9387c478bd9Sstevel@tonic-gate 	netmask.ss_family = afp->af_af;
9397c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
9407c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
9417c478bd9Sstevel@tonic-gate 		/* Nothing there - ok */
9427c478bd9Sstevel@tonic-gate 		break;
9437c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
9447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: Bad prefix length in %s\n",
9457c478bd9Sstevel@tonic-gate 		    addr);
9467c478bd9Sstevel@tonic-gate 		exit(1);
9477c478bd9Sstevel@tonic-gate 	default:
9487c478bd9Sstevel@tonic-gate 		if (afp->af_af == AF_INET6) {
9497c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&netmask;
9507906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
9517c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin6->sin6_addr)) {
9527c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
9537c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
9547c478bd9Sstevel@tonic-gate 				    prefixlen);
9557c478bd9Sstevel@tonic-gate 				exit(1);
9567c478bd9Sstevel@tonic-gate 			}
9577c478bd9Sstevel@tonic-gate 		} else {
9587c478bd9Sstevel@tonic-gate 			sin = (struct sockaddr_in *)&netmask;
9597906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IP_ABITS,
9607c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin->sin_addr)) {
9617c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
9627c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
9637c478bd9Sstevel@tonic-gate 				    prefixlen);
9647c478bd9Sstevel@tonic-gate 				exit(1);
9657c478bd9Sstevel@tonic-gate 			}
9667c478bd9Sstevel@tonic-gate 		}
9677c478bd9Sstevel@tonic-gate 		/*
9687c478bd9Sstevel@tonic-gate 		 * Just in case of funny setting of both prefix and netmask,
9697c478bd9Sstevel@tonic-gate 		 * prefix should override the netmask command.
9707c478bd9Sstevel@tonic-gate 		 */
971dd7a6f5fSkcpoon 		g_netmask_set = G_NETMASK_NIL;
9727c478bd9Sstevel@tonic-gate 		break;
9737c478bd9Sstevel@tonic-gate 	}
974*6e91bba0SGirish Moodalbail 
975*6e91bba0SGirish Moodalbail 	/*
976*6e91bba0SGirish Moodalbail 	 * Check and see if any "netmask" command is used and perform the
977*6e91bba0SGirish Moodalbail 	 * necessary operation.
978*6e91bba0SGirish Moodalbail 	 */
979*6e91bba0SGirish Moodalbail 	set_mask_lifreq(&lifr, &laddr, &netmask);
980*6e91bba0SGirish Moodalbail 
981*6e91bba0SGirish Moodalbail 	/* This check is temporary until libipadm supports IPMP interfaces. */
982*6e91bba0SGirish Moodalbail 	if (ifconfig_use_libipadm(s, name)) {
983*6e91bba0SGirish Moodalbail 		istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name,
984*6e91bba0SGirish Moodalbail 		    &ipaddr);
985*6e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
986*6e91bba0SGirish Moodalbail 			ipadmerr_exit(istatus, "setifaddr");
987*6e91bba0SGirish Moodalbail 
988*6e91bba0SGirish Moodalbail 		if (strchr(addr, '/') == NULL) {
989*6e91bba0SGirish Moodalbail 			/*
990*6e91bba0SGirish Moodalbail 			 * lifr.lifr_addr, which is updated by set_mask_lifreq()
991*6e91bba0SGirish Moodalbail 			 * will contain the right mask to use.
992*6e91bba0SGirish Moodalbail 			 */
993*6e91bba0SGirish Moodalbail 			prefixlen = mask2plen(&lifr.lifr_addr);
994*6e91bba0SGirish Moodalbail 			(void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d",
995*6e91bba0SGirish Moodalbail 			    addr, prefixlen);
996*6e91bba0SGirish Moodalbail 			addr = cidraddr;
997*6e91bba0SGirish Moodalbail 		}
998*6e91bba0SGirish Moodalbail 		istatus = ipadm_set_addr(ipaddr, addr, af);
999*6e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
1000*6e91bba0SGirish Moodalbail 			ipadmerr_exit(istatus, "could not set address");
1001*6e91bba0SGirish Moodalbail 		/*
1002*6e91bba0SGirish Moodalbail 		 * let parser know we got a source.
1003*6e91bba0SGirish Moodalbail 		 * Next address, if given, should be dest
1004*6e91bba0SGirish Moodalbail 		 */
1005*6e91bba0SGirish Moodalbail 		setaddr++;
1006*6e91bba0SGirish Moodalbail 
1007*6e91bba0SGirish Moodalbail 		/*
1008*6e91bba0SGirish Moodalbail 		 * address will be set by the parser after nextarg has
1009*6e91bba0SGirish Moodalbail 		 * been scanned
1010*6e91bba0SGirish Moodalbail 		 */
1011*6e91bba0SGirish Moodalbail 		return (0);
1012*6e91bba0SGirish Moodalbail 	}
1013*6e91bba0SGirish Moodalbail 
10147c478bd9Sstevel@tonic-gate 	/* Tell parser that an address was set */
10157c478bd9Sstevel@tonic-gate 	setaddr++;
10167c478bd9Sstevel@tonic-gate 	/* save copy of netmask to restore in case of error */
1017*6e91bba0SGirish Moodalbail 	(void) strncpy(lifr1.lifr_name, name, sizeof (lifr1.lifr_name));
1018*6e91bba0SGirish Moodalbail 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr1) < 0)
10197c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCGLIFNETMASK");
1020*6e91bba0SGirish Moodalbail 	sav_netmask = lifr1.lifr_addr;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	/*
10237c478bd9Sstevel@tonic-gate 	 * If setting the address and not the mask, clear any existing mask
10247c478bd9Sstevel@tonic-gate 	 * and the kernel will then assign the default (netmask has been set
10257c478bd9Sstevel@tonic-gate 	 * to 0 in this case).  If setting both (either by using a prefix or
10267c478bd9Sstevel@tonic-gate 	 * using the netmask command), set the mask first, so the address will
10277c478bd9Sstevel@tonic-gate 	 * be interpreted correctly.
10287c478bd9Sstevel@tonic-gate 	 */
1029*6e91bba0SGirish Moodalbail 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1030*6e91bba0SGirish Moodalbail 	/* lifr.lifr_addr already contains netmask from set_mask_lifreq() */
10317c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
10327c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	if (debug) {
10357c478bd9Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
10367c478bd9Sstevel@tonic-gate 		void *addr = (afp->af_af == AF_INET) ?
10377c478bd9Sstevel@tonic-gate 		    (void *)&((struct sockaddr_in *)&laddr)->sin_addr :
10387c478bd9Sstevel@tonic-gate 		    (void *)&((struct sockaddr_in6 *)&laddr)->sin6_addr;
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 		(void) printf("Setting %s af %d addr %s\n",
10417c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, afp->af_af,
10427c478bd9Sstevel@tonic-gate 		    inet_ntop(afp->af_af, addr, abuf, sizeof (abuf)));
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = laddr;
10457c478bd9Sstevel@tonic-gate 	lifr.lifr_addr.ss_family = afp->af_af;
10467c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
10477c478bd9Sstevel@tonic-gate 		/*
10487c478bd9Sstevel@tonic-gate 		 * Restore the netmask
10497c478bd9Sstevel@tonic-gate 		 */
10507c478bd9Sstevel@tonic-gate 		int saverr = errno;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
10537c478bd9Sstevel@tonic-gate 		lifr.lifr_addr = sav_netmask;
10547c478bd9Sstevel@tonic-gate 		(void) ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr);
10557c478bd9Sstevel@tonic-gate 		errno = saverr;
10567c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFADDR");
10577c478bd9Sstevel@tonic-gate 	}
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	return (0);
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate /*
10637c478bd9Sstevel@tonic-gate  * The following functions are stolen from the ipseckey(1m) program.
10647c478bd9Sstevel@tonic-gate  * Perhaps they should be somewhere common, but for now, we just maintain
10657c478bd9Sstevel@tonic-gate  * two versions.  We do this because of the different semantics for which
10667c478bd9Sstevel@tonic-gate  * algorithms we select ("requested" for ifconfig vs. "actual" for key).
10677c478bd9Sstevel@tonic-gate  */
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate static ulong_t
10707c478bd9Sstevel@tonic-gate parsenum(char *num)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	ulong_t rc;
10737c478bd9Sstevel@tonic-gate 	char *end = NULL;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	errno = 0;
10767c478bd9Sstevel@tonic-gate 	rc = strtoul(num, &end, 0);
10777c478bd9Sstevel@tonic-gate 	if (errno != 0 || end == num || *end != '\0') {
10787c478bd9Sstevel@tonic-gate 		rc = (ulong_t)-1;
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	return (rc);
10827c478bd9Sstevel@tonic-gate }
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate /*
10857c478bd9Sstevel@tonic-gate  * Parse and reverse parse possible algorithm values, include numbers.
10867c478bd9Sstevel@tonic-gate  * Mostly stolen from ipseckey.c. See the comments above parsenum() for why
10877c478bd9Sstevel@tonic-gate  * this isn't common to ipseckey.c.
10887c478bd9Sstevel@tonic-gate  *
10897c478bd9Sstevel@tonic-gate  * NOTE: Static buffer in this function for the return value.  Since ifconfig
10902b24ab6bSSebastien Roy  *       isn't multithreaded, this isn't a huge problem.
10917c478bd9Sstevel@tonic-gate  */
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate #define	NBUF_SIZE 20	/* Enough to print a large integer. */
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate static char *
10967c478bd9Sstevel@tonic-gate rparsealg(uint8_t alg_value, int proto_num)
10977c478bd9Sstevel@tonic-gate {
10987c478bd9Sstevel@tonic-gate 	struct ipsecalgent *alg;
10997c478bd9Sstevel@tonic-gate 	static char numprint[128];	/* Enough to hold an algorithm name. */
11007c478bd9Sstevel@tonic-gate 
1101d2f8a3dfSpwernau 	/*
1102d2f8a3dfSpwernau 	 * Special cases for "any" and "none"
1103d2f8a3dfSpwernau 	 * The kernel needs to be able to distinguish between "any"
1104d2f8a3dfSpwernau 	 * and "none" and the APIs are underdefined in this area for auth.
1105d2f8a3dfSpwernau 	 */
1106d2f8a3dfSpwernau 	if (proto_num == IPSEC_PROTO_AH) {
1107d2f8a3dfSpwernau 		if (alg_value == SADB_AALG_NONE)
1108d2f8a3dfSpwernau 			return ("none");
1109d2f8a3dfSpwernau 		if (alg_value == SADB_AALG_ANY)
1110d2f8a3dfSpwernau 			return ("any");
1111d2f8a3dfSpwernau 	}
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	alg = getipsecalgbynum(alg_value, proto_num, NULL);
11147c478bd9Sstevel@tonic-gate 	if (alg != NULL) {
11157c478bd9Sstevel@tonic-gate 		(void) strlcpy(numprint, alg->a_names[0], sizeof (numprint));
11167c478bd9Sstevel@tonic-gate 		freeipsecalgent(alg);
11177c478bd9Sstevel@tonic-gate 	} else {
11187c478bd9Sstevel@tonic-gate 		(void) snprintf(numprint, sizeof (numprint), "%d", alg_value);
11197c478bd9Sstevel@tonic-gate 	}
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	return (numprint);
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate static uint_t
11257c478bd9Sstevel@tonic-gate parsealg(char *algname, int proto_num)
11267c478bd9Sstevel@tonic-gate {
11277c478bd9Sstevel@tonic-gate 	struct ipsecalgent *alg;
11287c478bd9Sstevel@tonic-gate 	ulong_t invalue;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	if (algname == NULL) {
11317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: Unexpected end of command "
11327c478bd9Sstevel@tonic-gate 		    "line.\n");
11337c478bd9Sstevel@tonic-gate 		exit(1);
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	/*
1137d2f8a3dfSpwernau 	 * Special-case "none" and "any".
1138d2f8a3dfSpwernau 	 * Use strcasecmp because its length is bounded.
11397c478bd9Sstevel@tonic-gate 	 */
11407c478bd9Sstevel@tonic-gate 	if (strcasecmp("none", algname) == 0) {
11417c478bd9Sstevel@tonic-gate 		return ((proto_num == IPSEC_PROTO_ESP) ?
11427c478bd9Sstevel@tonic-gate 		    NO_ESP_EALG : NO_ESP_AALG);
11437c478bd9Sstevel@tonic-gate 	}
1144d2f8a3dfSpwernau 	if ((strcasecmp("any", algname) == 0) && (proto_num == IPSEC_PROTO_AH))
1145d2f8a3dfSpwernau 		return (SADB_AALG_ANY);
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	alg = getipsecalgbyname(algname, proto_num, NULL);
11487c478bd9Sstevel@tonic-gate 	if (alg != NULL) {
11497c478bd9Sstevel@tonic-gate 		invalue = alg->a_alg_num;
11507c478bd9Sstevel@tonic-gate 		freeipsecalgent(alg);
11517c478bd9Sstevel@tonic-gate 		return ((uint_t)invalue);
11527c478bd9Sstevel@tonic-gate 	}
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	/*
11557c478bd9Sstevel@tonic-gate 	 * Since algorithms can be loaded during kernel run-time, check for
11567c478bd9Sstevel@tonic-gate 	 * numeric algorithm values too.
11577c478bd9Sstevel@tonic-gate 	 */
11587c478bd9Sstevel@tonic-gate 	invalue = parsenum(algname);
11597c478bd9Sstevel@tonic-gate 	if ((invalue & (ulong_t)0xff) == invalue)
11607c478bd9Sstevel@tonic-gate 		return ((uint_t)invalue);
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "ifconfig: %s algorithm type %s unknown.\n",
11637c478bd9Sstevel@tonic-gate 	    (proto_num == IPSEC_PROTO_ESP) ?
11647c478bd9Sstevel@tonic-gate 	    "Encryption" : "Authentication", algname);
11657c478bd9Sstevel@tonic-gate 	exit(1);
11667c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
11677c478bd9Sstevel@tonic-gate }
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate /*
11707c478bd9Sstevel@tonic-gate  * Actual ifconfig functions to set tunnel security properties.
11717c478bd9Sstevel@tonic-gate  */
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate enum ipsec_alg_type { ESP_ENCR_ALG = 1, ESP_AUTH_ALG, AH_AUTH_ALG };
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate static int
11767c478bd9Sstevel@tonic-gate set_tun_algs(int which_alg, int alg)
11777c478bd9Sstevel@tonic-gate {
11782b24ab6bSSebastien Roy 	boolean_t	encr_alg_set = _B_FALSE;
11792b24ab6bSSebastien Roy 	iptun_params_t	params;
11802b24ab6bSSebastien Roy 	dladm_status_t	status;
11812b24ab6bSSebastien Roy 	ipsec_req_t	*ipsr;
11827c478bd9Sstevel@tonic-gate 
11832b24ab6bSSebastien Roy 	if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN,
11842b24ab6bSSebastien Roy 	    &params.iptun_param_linkid)) != DLADM_STATUS_OK)
11852b24ab6bSSebastien Roy 		goto done;
11867c478bd9Sstevel@tonic-gate 
11872b24ab6bSSebastien Roy 	status = dladm_iptun_getparams(dlh, &params, DLADM_OPT_ACTIVE);
11882b24ab6bSSebastien Roy 	if (status != DLADM_STATUS_OK)
11892b24ab6bSSebastien Roy 		goto done;
11907c478bd9Sstevel@tonic-gate 
11912b24ab6bSSebastien Roy 	ipsr = &params.iptun_param_secinfo;
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	/*
11947c478bd9Sstevel@tonic-gate 	 * If I'm just starting off this ifconfig, I want a clean slate,
11957c478bd9Sstevel@tonic-gate 	 * otherwise, I've captured the current tunnel security settings.
11967c478bd9Sstevel@tonic-gate 	 * In the case of continuation, I merely add to the settings.
11977c478bd9Sstevel@tonic-gate 	 */
11982b24ab6bSSebastien Roy 	if (!(params.iptun_param_flags & IPTUN_PARAM_SECINFO))
11997c478bd9Sstevel@tonic-gate 		(void) memset(ipsr, 0, sizeof (*ipsr));
12007c478bd9Sstevel@tonic-gate 
12012b24ab6bSSebastien Roy 	/* We're only modifying the IPsec information */
12022b24ab6bSSebastien Roy 	params.iptun_param_flags = IPTUN_PARAM_SECINFO;
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	switch (which_alg) {
12057c478bd9Sstevel@tonic-gate 	case ESP_ENCR_ALG:
12067c478bd9Sstevel@tonic-gate 		if (alg == NO_ESP_EALG) {
12077c478bd9Sstevel@tonic-gate 			if (ipsr->ipsr_esp_auth_alg == SADB_AALG_NONE)
12087c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_req = 0;
12097c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_alg = SADB_EALG_NONE;
1210d2f8a3dfSpwernau 
1211d2f8a3dfSpwernau 			/* Let the user specify NULL encryption implicitly. */
1212d2f8a3dfSpwernau 			if (ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) {
1213d2f8a3dfSpwernau 				encr_alg_set = _B_TRUE;
1214d2f8a3dfSpwernau 				ipsr->ipsr_esp_alg = SADB_EALG_NULL;
1215d2f8a3dfSpwernau 			}
12167c478bd9Sstevel@tonic-gate 		} else {
12177c478bd9Sstevel@tonic-gate 			encr_alg_set = _B_TRUE;
12187c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_req =
12197c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12207c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_alg = alg;
12217c478bd9Sstevel@tonic-gate 		}
12227c478bd9Sstevel@tonic-gate 		break;
12237c478bd9Sstevel@tonic-gate 	case ESP_AUTH_ALG:
12247c478bd9Sstevel@tonic-gate 		if (alg == NO_ESP_AALG) {
1225d2f8a3dfSpwernau 			if ((ipsr->ipsr_esp_alg == SADB_EALG_NONE ||
1226d2f8a3dfSpwernau 			    ipsr->ipsr_esp_alg == SADB_EALG_NULL) &&
1227d2f8a3dfSpwernau 			    !encr_alg_set)
12287c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_req = 0;
12297c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_auth_alg = SADB_AALG_NONE;
12307c478bd9Sstevel@tonic-gate 		} else {
12317c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_req =
12327c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12337c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_auth_alg = alg;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 			/* Let the user specify NULL encryption implicitly. */
12367c478bd9Sstevel@tonic-gate 			if (ipsr->ipsr_esp_alg == SADB_EALG_NONE &&
12377c478bd9Sstevel@tonic-gate 			    !encr_alg_set)
12387c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_alg = SADB_EALG_NULL;
12397c478bd9Sstevel@tonic-gate 		}
12407c478bd9Sstevel@tonic-gate 		break;
12417c478bd9Sstevel@tonic-gate 	case AH_AUTH_ALG:
12427c478bd9Sstevel@tonic-gate 		if (alg == NO_AH_AALG) {
12437c478bd9Sstevel@tonic-gate 			ipsr->ipsr_ah_req = 0;
12447c478bd9Sstevel@tonic-gate 			ipsr->ipsr_auth_alg = SADB_AALG_NONE;
12457c478bd9Sstevel@tonic-gate 		} else {
12467c478bd9Sstevel@tonic-gate 			ipsr->ipsr_ah_req =
12477c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12487c478bd9Sstevel@tonic-gate 			ipsr->ipsr_auth_alg = alg;
12497c478bd9Sstevel@tonic-gate 		}
12507c478bd9Sstevel@tonic-gate 		break;
12517c478bd9Sstevel@tonic-gate 		/* Will never hit DEFAULT */
12527c478bd9Sstevel@tonic-gate 	}
12537c478bd9Sstevel@tonic-gate 
12542b24ab6bSSebastien Roy 	status = dladm_iptun_modify(dlh, &params, DLADM_OPT_ACTIVE);
12557c478bd9Sstevel@tonic-gate 
12562b24ab6bSSebastien Roy done:
12572b24ab6bSSebastien Roy 	if (status != DLADM_STATUS_OK)
12582b24ab6bSSebastien Roy 		dladmerr_exit(status, name);
12592b24ab6bSSebastien Roy 	else {
12602b24ab6bSSebastien Roy 		ipsec_policy_set = _B_TRUE;
12612b24ab6bSSebastien Roy 		if ((ipsr->ipsr_esp_req != 0 &&
12622b24ab6bSSebastien Roy 		    ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) ||
12632b24ab6bSSebastien Roy 		    (ipsr->ipsr_ah_req != 0 &&
12642b24ab6bSSebastien Roy 		    ipsr->ipsr_auth_alg != SADB_AALG_NONE))
12652b24ab6bSSebastien Roy 			ipsec_auth_covered = _B_TRUE;
12662b24ab6bSSebastien Roy 	}
12677c478bd9Sstevel@tonic-gate 	return (0);
12687c478bd9Sstevel@tonic-gate }
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate /* ARGSUSED */
12717c478bd9Sstevel@tonic-gate static int
12727c478bd9Sstevel@tonic-gate set_tun_esp_encr_alg(char *addr, int64_t param)
12737c478bd9Sstevel@tonic-gate {
12747c478bd9Sstevel@tonic-gate 	return (set_tun_algs(ESP_ENCR_ALG,
1275fc80c0dfSnordmark 	    parsealg(addr, IPSEC_PROTO_ESP)));
12767c478bd9Sstevel@tonic-gate }
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate /* ARGSUSED */
12797c478bd9Sstevel@tonic-gate static int
12807c478bd9Sstevel@tonic-gate set_tun_esp_auth_alg(char *addr, int64_t param)
12817c478bd9Sstevel@tonic-gate {
12827c478bd9Sstevel@tonic-gate 	return (set_tun_algs(ESP_AUTH_ALG,
1283fc80c0dfSnordmark 	    parsealg(addr, IPSEC_PROTO_AH)));
12847c478bd9Sstevel@tonic-gate }
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate /* ARGSUSED */
12877c478bd9Sstevel@tonic-gate static int
12887c478bd9Sstevel@tonic-gate set_tun_ah_alg(char *addr, int64_t param)
12897c478bd9Sstevel@tonic-gate {
12907c478bd9Sstevel@tonic-gate 	return (set_tun_algs(AH_AUTH_ALG,
1291fc80c0dfSnordmark 	    parsealg(addr, IPSEC_PROTO_AH)));
12927c478bd9Sstevel@tonic-gate }
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate /* ARGSUSED */
12957c478bd9Sstevel@tonic-gate static int
12967c478bd9Sstevel@tonic-gate setifrevarp(char *arg, int64_t param)
12977c478bd9Sstevel@tonic-gate {
12987c478bd9Sstevel@tonic-gate 	struct sockaddr_in	laddr;
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	if (afp->af_af == AF_INET6) {
13017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13027c478bd9Sstevel@tonic-gate 		    "ifconfig: revarp not possible on IPv6 interface %s\n",
13037c478bd9Sstevel@tonic-gate 		    name);
13047c478bd9Sstevel@tonic-gate 		exit(1);
13057c478bd9Sstevel@tonic-gate 	}
13067c478bd9Sstevel@tonic-gate 	if (doifrevarp(name, &laddr)) {
13077c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13087c478bd9Sstevel@tonic-gate 		laddr.sin_family = AF_INET;
13097c478bd9Sstevel@tonic-gate 		(void) memcpy(&lifr.lifr_addr, &laddr, sizeof (laddr));
13107c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
13117c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCSLIFADDR");
13127c478bd9Sstevel@tonic-gate 	}
13137c478bd9Sstevel@tonic-gate 	return (0);
13147c478bd9Sstevel@tonic-gate }
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate /* ARGSUSED */
13177c478bd9Sstevel@tonic-gate static int
13187c478bd9Sstevel@tonic-gate setifsubnet(char *addr, int64_t param)
13197c478bd9Sstevel@tonic-gate {
13207c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
13217c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage subnet;
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, &subnet, &prefixlen);
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
13267c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
13277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13287c478bd9Sstevel@tonic-gate 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
13297c478bd9Sstevel@tonic-gate 		exit(1);
13307c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
13317c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
13327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13337c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
13347c478bd9Sstevel@tonic-gate 		exit(1);
13357c478bd9Sstevel@tonic-gate 	default:
13367c478bd9Sstevel@tonic-gate 		break;
13377c478bd9Sstevel@tonic-gate 	}
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = subnet;
13407c478bd9Sstevel@tonic-gate 	lifr.lifr_addrlen = prefixlen;
13417c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13427c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFSUBNET, (caddr_t)&lifr) < 0)
13437c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFSUBNET");
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	return (0);
13467c478bd9Sstevel@tonic-gate }
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate /* ARGSUSED */
13497c478bd9Sstevel@tonic-gate static int
13507c478bd9Sstevel@tonic-gate setifnetmask(char *addr, int64_t param)
13517c478bd9Sstevel@tonic-gate {
13527c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask;
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 	assert(afp->af_af != AF_INET6);
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	if (strcmp(addr, "+") == 0) {
1357dd7a6f5fSkcpoon 		if (!in_getmask(&netmask, _B_FALSE))
13587c478bd9Sstevel@tonic-gate 			return (0);
1359dd7a6f5fSkcpoon 		(void) printf("Setting netmask of %s to %s\n", name,
1360dd7a6f5fSkcpoon 		    inet_ntoa(netmask.sin_addr));
13617c478bd9Sstevel@tonic-gate 	} else {
13627c478bd9Sstevel@tonic-gate 		in_getaddr(addr, (struct sockaddr *)&netmask, NULL);
13637c478bd9Sstevel@tonic-gate 	}
13647c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13657c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &netmask, sizeof (netmask));
13667c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
13677c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
13687c478bd9Sstevel@tonic-gate 	return (0);
13697c478bd9Sstevel@tonic-gate }
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate /*
13727c478bd9Sstevel@tonic-gate  * Parse '/<n>' as a netmask.
13737c478bd9Sstevel@tonic-gate  */
13747c478bd9Sstevel@tonic-gate /* ARGSUSED */
13757c478bd9Sstevel@tonic-gate static int
13767c478bd9Sstevel@tonic-gate setifprefixlen(char *addr, int64_t param)
13777c478bd9Sstevel@tonic-gate {
13787c478bd9Sstevel@tonic-gate 	int prefixlen;
13797c478bd9Sstevel@tonic-gate 	int af = afp->af_af;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	prefixlen = in_getprefixlen(addr, _B_TRUE,
13827906a3e0Smeem 	    (af == AF_INET) ? IP_ABITS : IPV6_ABITS);
13837c478bd9Sstevel@tonic-gate 	if (prefixlen < 0) {
13847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13857c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
13867c478bd9Sstevel@tonic-gate 		exit(1);
13877c478bd9Sstevel@tonic-gate 	}
13887c478bd9Sstevel@tonic-gate 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
13897c478bd9Sstevel@tonic-gate 	lifr.lifr_addr.ss_family = af;
13907c478bd9Sstevel@tonic-gate 	if (af == AF_INET6) {
13917c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
13947906a3e0Smeem 		if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
13957c478bd9Sstevel@tonic-gate 		    (uchar_t *)&sin6->sin6_addr)) {
13967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: "
13977c478bd9Sstevel@tonic-gate 			    "Bad prefix length: %d\n",
13987c478bd9Sstevel@tonic-gate 			    prefixlen);
13997c478bd9Sstevel@tonic-gate 			exit(1);
14007c478bd9Sstevel@tonic-gate 		}
14017c478bd9Sstevel@tonic-gate 	} else if (af == AF_INET) {
14027c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin;
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
14057906a3e0Smeem 		if (!in_prefixlentomask(prefixlen, IP_ABITS,
14067c478bd9Sstevel@tonic-gate 		    (uchar_t *)&sin->sin_addr)) {
14077c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: "
14087c478bd9Sstevel@tonic-gate 			    "Bad prefix length: %d\n",
14097c478bd9Sstevel@tonic-gate 			    prefixlen);
14107c478bd9Sstevel@tonic-gate 			exit(1);
14117c478bd9Sstevel@tonic-gate 		}
14127c478bd9Sstevel@tonic-gate 	} else {
14137c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: setting prefix only supported"
14147c478bd9Sstevel@tonic-gate 		    " for address family inet or inet6\n");
14157c478bd9Sstevel@tonic-gate 		exit(1);
14167c478bd9Sstevel@tonic-gate 	}
14177c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14187c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
14197c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
14207c478bd9Sstevel@tonic-gate 	return (0);
14217c478bd9Sstevel@tonic-gate }
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate /* ARGSUSED */
14247c478bd9Sstevel@tonic-gate static int
14257c478bd9Sstevel@tonic-gate setifbroadaddr(char *addr, int64_t param)
14267c478bd9Sstevel@tonic-gate {
14277c478bd9Sstevel@tonic-gate 	struct	sockaddr_in broadaddr;
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	assert(afp->af_af != AF_INET6);
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	if (strcmp(addr, "+") == 0) {
14327c478bd9Sstevel@tonic-gate 		/*
14337c478bd9Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
14347c478bd9Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
14357c478bd9Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
14367c478bd9Sstevel@tonic-gate 		 */
14377c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name,
14387c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
14397c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
14407c478bd9Sstevel@tonic-gate 			if (errno != EADDRNOTAVAIL)
14417c478bd9Sstevel@tonic-gate 				Perror0_exit("SIOCGLIFADDR");
14427c478bd9Sstevel@tonic-gate 			return (0);
14437c478bd9Sstevel@tonic-gate 		}
14447c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
14457c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCGLIFADDR");
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 		return (0);
14487c478bd9Sstevel@tonic-gate 	}
14497c478bd9Sstevel@tonic-gate 	in_getaddr(addr, (struct sockaddr *)&broadaddr, NULL);
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &broadaddr, sizeof (broadaddr));
14527c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14537c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFBRDADDR, (caddr_t)&lifr) < 0)
14547c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFBRDADDR");
14557c478bd9Sstevel@tonic-gate 	return (0);
14567c478bd9Sstevel@tonic-gate }
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate /*
14597c478bd9Sstevel@tonic-gate  * set interface destination address
14607c478bd9Sstevel@tonic-gate  */
14617c478bd9Sstevel@tonic-gate /* ARGSUSED */
14627c478bd9Sstevel@tonic-gate static int
14637c478bd9Sstevel@tonic-gate setifdstaddr(char *addr, int64_t param)
14647c478bd9Sstevel@tonic-gate {
14657c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, (struct sockaddr *)&lifr.lifr_addr, NULL);
14667c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14677c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFDSTADDR, (caddr_t)&lifr) < 0)
14687c478bd9Sstevel@tonic-gate 		Perror0_exit("setifdstaddr: SIOCSLIFDSTADDR");
14697c478bd9Sstevel@tonic-gate 	return (0);
14707c478bd9Sstevel@tonic-gate }
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate /* ARGSUSED */
14737c478bd9Sstevel@tonic-gate static int
14747c478bd9Sstevel@tonic-gate setifflags(char *val, int64_t value)
14757c478bd9Sstevel@tonic-gate {
1476e11c3f44Smeem 	struct lifreq lifrl;	/* local lifreq struct */
1477e11c3f44Smeem 	boolean_t bringup = _B_FALSE;
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14807c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0)
14817c478bd9Sstevel@tonic-gate 		Perror0_exit("setifflags: SIOCGLIFFLAGS");
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	if (value < 0) {
14847c478bd9Sstevel@tonic-gate 		value = -value;
1485e11c3f44Smeem 
1486e11c3f44Smeem 		if ((value & IFF_NOFAILOVER) && (lifr.lifr_flags & IFF_UP)) {
1487e11c3f44Smeem 			/*
1488e11c3f44Smeem 			 * The kernel does not allow administratively up test
1489e11c3f44Smeem 			 * addresses to be converted to data addresses.  Bring
1490e11c3f44Smeem 			 * the address down first, then bring it up after it's
1491e11c3f44Smeem 			 * been converted to a data address.
1492e11c3f44Smeem 			 */
1493e11c3f44Smeem 			lifr.lifr_flags &= ~IFF_UP;
1494e11c3f44Smeem 			(void) ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr);
1495e11c3f44Smeem 			bringup = _B_TRUE;
1496e11c3f44Smeem 		}
1497e11c3f44Smeem 
14987c478bd9Sstevel@tonic-gate 		lifr.lifr_flags &= ~value;
1499e11c3f44Smeem 		if ((value & (IFF_UP | IFF_NOFAILOVER)) &&
1500e11c3f44Smeem 		    (lifr.lifr_flags & IFF_DUPLICATE)) {
150169bb4bb4Scarlsonj 			/*
150269bb4bb4Scarlsonj 			 * If the user is trying to mark an interface with a
1503e11c3f44Smeem 			 * duplicate address as "down," or convert a duplicate
1504e11c3f44Smeem 			 * test address to a data address, then fetch the
1505e11c3f44Smeem 			 * address and set it.  This will cause IP to clear
1506e11c3f44Smeem 			 * the IFF_DUPLICATE flag and stop the automatic
1507e11c3f44Smeem 			 * recovery timer.
150869bb4bb4Scarlsonj 			 */
150969bb4bb4Scarlsonj 			value = lifr.lifr_flags;
151069bb4bb4Scarlsonj 			if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0)
151169bb4bb4Scarlsonj 				(void) ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr);
151269bb4bb4Scarlsonj 			lifr.lifr_flags = value;
151369bb4bb4Scarlsonj 		}
151469bb4bb4Scarlsonj 	} else {
15157c478bd9Sstevel@tonic-gate 		lifr.lifr_flags |= value;
151669bb4bb4Scarlsonj 	}
1517e11c3f44Smeem 
1518e11c3f44Smeem 	/*
1519e11c3f44Smeem 	 * If we're about to bring up an underlying physical IPv6 interface in
1520e11c3f44Smeem 	 * an IPMP group, ensure the IPv6 IPMP interface is also up.  This is
1521e11c3f44Smeem 	 * for backward compatibility with legacy configurations in which
1522e11c3f44Smeem 	 * there are no explicit hostname files for IPMP interfaces.  (For
1523e11c3f44Smeem 	 * IPv4, this is automatically handled by the kernel when migrating
1524e11c3f44Smeem 	 * the underlying interface's data address to the IPMP interface.)
1525e11c3f44Smeem 	 */
1526e11c3f44Smeem 	(void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ);
1527e11c3f44Smeem 
1528e11c3f44Smeem 	if (lifnum(lifr.lifr_name) == 0 &&
1529e11c3f44Smeem 	    (lifr.lifr_flags & (IFF_UP|IFF_IPV6)) == (IFF_UP|IFF_IPV6) &&
1530e11c3f44Smeem 	    ioctl(s, SIOCGLIFGROUPNAME, &lifrl) == 0 &&
1531e11c3f44Smeem 	    lifrl.lifr_groupname[0] != '\0') {
1532e11c3f44Smeem 		lifgroupinfo_t lifgr;
1533e11c3f44Smeem 
1534e11c3f44Smeem 		(void) strlcpy(lifgr.gi_grname, lifrl.lifr_groupname,
1535e11c3f44Smeem 		    LIFGRNAMSIZ);
1536e11c3f44Smeem 		if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1)
1537e11c3f44Smeem 			Perror0_exit("setifflags: SIOCGLIFGROUPINFO");
1538e11c3f44Smeem 
1539e11c3f44Smeem 		(void) strlcpy(lifrl.lifr_name, lifgr.gi_grifname, LIFNAMSIZ);
1540e11c3f44Smeem 		if (ioctl(s, SIOCGLIFFLAGS, &lifrl) == -1)
1541e11c3f44Smeem 			Perror0_exit("setifflags: SIOCGLIFFLAGS");
1542e11c3f44Smeem 		if (!(lifrl.lifr_flags & IFF_UP)) {
1543e11c3f44Smeem 			lifrl.lifr_flags |= IFF_UP;
1544e11c3f44Smeem 			if (ioctl(s, SIOCSLIFFLAGS, &lifrl) == -1)
1545e11c3f44Smeem 				Perror0_exit("setifflags: SIOCSLIFFLAGS");
1546e11c3f44Smeem 		}
1547e11c3f44Smeem 	}
1548e11c3f44Smeem 
15497c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1550e11c3f44Smeem 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0)
15517c478bd9Sstevel@tonic-gate 		Perror0_exit("setifflags: SIOCSLIFFLAGS");
1552e11c3f44Smeem 
1553e11c3f44Smeem 	if (bringup) {
1554e11c3f44Smeem 		lifr.lifr_flags |= IFF_UP;
1555e11c3f44Smeem 		if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0)
1556e11c3f44Smeem 			Perror0_exit("setifflags: SIOCSLIFFLAGS IFF_UP");
15577c478bd9Sstevel@tonic-gate 	}
1558e11c3f44Smeem 
15597c478bd9Sstevel@tonic-gate 	return (0);
15607c478bd9Sstevel@tonic-gate }
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate /* ARGSUSED */
15637c478bd9Sstevel@tonic-gate static int
15647c478bd9Sstevel@tonic-gate setifmetric(char *val, int64_t param)
15657c478bd9Sstevel@tonic-gate {
15667c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15677c478bd9Sstevel@tonic-gate 	lifr.lifr_metric = atoi(val);
15687c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFMETRIC, (caddr_t)&lifr) < 0)
15697c478bd9Sstevel@tonic-gate 		Perror0_exit("setifmetric: SIOCSLIFMETRIC");
15707c478bd9Sstevel@tonic-gate 	return (0);
15717c478bd9Sstevel@tonic-gate }
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate /* ARGSUSED */
15747c478bd9Sstevel@tonic-gate static int
15757c478bd9Sstevel@tonic-gate setifmtu(char *val, int64_t param)
15767c478bd9Sstevel@tonic-gate {
15777c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15787c478bd9Sstevel@tonic-gate 	lifr.lifr_mtu = atoi(val);
15797c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFMTU, (caddr_t)&lifr) < 0)
15807c478bd9Sstevel@tonic-gate 		Perror0_exit("setifmtu: SIOCSLIFMTU");
15817c478bd9Sstevel@tonic-gate 	return (0);
15827c478bd9Sstevel@tonic-gate }
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate /* ARGSUSED */
15857c478bd9Sstevel@tonic-gate static int
15867c478bd9Sstevel@tonic-gate setifindex(char *val, int64_t param)
15877c478bd9Sstevel@tonic-gate {
15887c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15897c478bd9Sstevel@tonic-gate 	lifr.lifr_index = atoi(val);
15907c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFINDEX, (caddr_t)&lifr) < 0)
15917c478bd9Sstevel@tonic-gate 		Perror0_exit("setifindex: SIOCSLIFINDEX");
15927c478bd9Sstevel@tonic-gate 	return (0);
15937c478bd9Sstevel@tonic-gate }
15947c478bd9Sstevel@tonic-gate 
1595e11c3f44Smeem /* ARGSUSED */
1596e11c3f44Smeem static void
1597e11c3f44Smeem notifycb(dlpi_handle_t dh, dlpi_notifyinfo_t *dnip, void *arg)
1598e11c3f44Smeem {
1599e11c3f44Smeem }
1600e11c3f44Smeem 
16017c478bd9Sstevel@tonic-gate /* ARGSUSED */
16027c478bd9Sstevel@tonic-gate static int
16037c478bd9Sstevel@tonic-gate setifether(char *addr, int64_t param)
16047c478bd9Sstevel@tonic-gate {
1605e11c3f44Smeem 	uchar_t		*hwaddr;
1606e11c3f44Smeem 	int		hwaddrlen;
1607e11c3f44Smeem 	int		retval;
1608e11c3f44Smeem 	ifaddrlistx_t	*ifaddrp, *ifaddrs = NULL;
1609e11c3f44Smeem 	dlpi_handle_t	dh;
1610e11c3f44Smeem 	dlpi_notifyid_t id;
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
16137c478bd9Sstevel@tonic-gate 		ifstatus(name);
16147c478bd9Sstevel@tonic-gate 		print_ifether(name);
16157c478bd9Sstevel@tonic-gate 		return (0);
16167c478bd9Sstevel@tonic-gate 	}
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	/*
16197c478bd9Sstevel@tonic-gate 	 * if the IP interface in the arguments is a logical
16207c478bd9Sstevel@tonic-gate 	 * interface, exit with an error now.
16217c478bd9Sstevel@tonic-gate 	 */
16227c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
16237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: cannot change"
16247c478bd9Sstevel@tonic-gate 		    " ethernet address of a logical interface\n");
16257c478bd9Sstevel@tonic-gate 		exit(1);
16267c478bd9Sstevel@tonic-gate 	}
16277c478bd9Sstevel@tonic-gate 
1628e11c3f44Smeem 	if ((hwaddr = _link_aton(addr, &hwaddrlen)) == NULL) {
1629e11c3f44Smeem 		if (hwaddrlen == -1)
16307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1631*6e91bba0SGirish Moodalbail 			    "ifconfig: bad ethernet address\n");
16327c478bd9Sstevel@tonic-gate 		else
16337c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: malloc() failed\n");
16347c478bd9Sstevel@tonic-gate 		exit(1);
16357c478bd9Sstevel@tonic-gate 	}
16367c478bd9Sstevel@tonic-gate 
1637e11c3f44Smeem 	if ((retval = dlpi_open(name, &dh, 0)) != DLPI_SUCCESS)
1638e11c3f44Smeem 		Perrdlpi_exit("cannot dlpi_open() link", name, retval);
16397c478bd9Sstevel@tonic-gate 
1640e11c3f44Smeem 	retval = dlpi_enabnotify(dh, DL_NOTE_PHYS_ADDR, notifycb, NULL, &id);
1641e11c3f44Smeem 	if (retval == DLPI_SUCCESS) {
1642e11c3f44Smeem 		(void) dlpi_disabnotify(dh, id, NULL);
1643e11c3f44Smeem 	} else {
1644e11c3f44Smeem 		/*
1645e11c3f44Smeem 		 * This link does not support DL_NOTE_PHYS_ADDR: bring down
1646e11c3f44Smeem 		 * all of the addresses to flush the old hardware address
1647e11c3f44Smeem 		 * information out of IP.
1648e11c3f44Smeem 		 *
1649e11c3f44Smeem 		 * NOTE: Skipping this when DL_NOTE_PHYS_ADDR is supported is
1650e11c3f44Smeem 		 * more than an optimization: in.mpathd will set IFF_OFFLINE
1651e11c3f44Smeem 		 * if it's notified and the new address is a duplicate of
1652e11c3f44Smeem 		 * another in the group -- but the flags manipulation in
1653e11c3f44Smeem 		 * ifaddr_{down,up}() cannot be atomic and thus might clobber
1654e11c3f44Smeem 		 * IFF_OFFLINE, confusing in.mpathd.
1655e11c3f44Smeem 		 */
1656e11c3f44Smeem 		if (ifaddrlistx(name, IFF_UP, 0, &ifaddrs) == -1)
1657e11c3f44Smeem 			Perror2_exit(name, "cannot get address list");
1658e11c3f44Smeem 
1659e11c3f44Smeem 		ifaddrp = ifaddrs;
1660e11c3f44Smeem 		for (; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
1661e11c3f44Smeem 			if (!ifaddr_down(ifaddrp)) {
1662e11c3f44Smeem 				Perror2_exit(ifaddrp->ia_name,
1663e11c3f44Smeem 				    "cannot bring down");
1664e11c3f44Smeem 			}
1665e11c3f44Smeem 		}
16667c478bd9Sstevel@tonic-gate 	}
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	/*
1669e11c3f44Smeem 	 * Change the hardware address.
16707c478bd9Sstevel@tonic-gate 	 */
1671e11c3f44Smeem 	retval = dlpi_set_physaddr(dh, DL_CURR_PHYS_ADDR, hwaddr, hwaddrlen);
1672e11c3f44Smeem 	if (retval != DLPI_SUCCESS) {
16737c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1674e11c3f44Smeem 		    "ifconfig: failed setting mac address on %s\n", name);
16757c478bd9Sstevel@tonic-gate 	}
1676e11c3f44Smeem 	dlpi_close(dh);
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 	/*
1679e11c3f44Smeem 	 * If any addresses were brought down before changing the hardware
1680e11c3f44Smeem 	 * address, bring them up again.
16817c478bd9Sstevel@tonic-gate 	 */
1682e11c3f44Smeem 	for (ifaddrp = ifaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
1683e11c3f44Smeem 		if (!ifaddr_up(ifaddrp))
1684e11c3f44Smeem 			Perror2_exit(ifaddrp->ia_name, "cannot bring up");
16857c478bd9Sstevel@tonic-gate 	}
1686e11c3f44Smeem 	ifaddrlistx_free(ifaddrs);
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 	return (0);
16897c478bd9Sstevel@tonic-gate }
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate /*
16927c478bd9Sstevel@tonic-gate  * Print an interface's Ethernet address, if it has one.
16937c478bd9Sstevel@tonic-gate  */
16947c478bd9Sstevel@tonic-gate static void
1695*6e91bba0SGirish Moodalbail print_ifether(const char *ifname)
16967c478bd9Sstevel@tonic-gate {
16972b24ab6bSSebastien Roy 	int fd;
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	fd = socket(AF_INET, SOCK_DGRAM, 0);
17027c478bd9Sstevel@tonic-gate 	if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
17037c478bd9Sstevel@tonic-gate 		/*
17047c478bd9Sstevel@tonic-gate 		 * It's possible the interface is only configured for
17057c478bd9Sstevel@tonic-gate 		 * IPv6; check again with AF_INET6.
17067c478bd9Sstevel@tonic-gate 		 */
17077c478bd9Sstevel@tonic-gate 		(void) close(fd);
17087c478bd9Sstevel@tonic-gate 		fd = socket(AF_INET6, SOCK_DGRAM, 0);
17097c478bd9Sstevel@tonic-gate 		if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
17107c478bd9Sstevel@tonic-gate 			(void) close(fd);
17117c478bd9Sstevel@tonic-gate 			return;
17127c478bd9Sstevel@tonic-gate 		}
17137c478bd9Sstevel@tonic-gate 	}
17147c478bd9Sstevel@tonic-gate 	(void) close(fd);
17157c478bd9Sstevel@tonic-gate 
1716e11c3f44Smeem 	/* VNI and IPMP interfaces don't have MAC addresses */
1717e11c3f44Smeem 	if (lifr.lifr_flags & (IFF_VIRTUAL|IFF_IPMP))
17187c478bd9Sstevel@tonic-gate 		return;
17197c478bd9Sstevel@tonic-gate 
17202b24ab6bSSebastien Roy 	/* IP tunnels also don't have Ethernet-like MAC addresses */
17212b24ab6bSSebastien Roy 	if (ifconfig_dladm_open(ifname, DATALINK_CLASS_IPTUN, NULL) ==
17222b24ab6bSSebastien Roy 	    DLADM_STATUS_OK)
17232b24ab6bSSebastien Roy 		return;
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	dlpi_print_address(ifname);
17267c478bd9Sstevel@tonic-gate }
17277c478bd9Sstevel@tonic-gate 
1728f4b3ec61Sdh /*
17292b24ab6bSSebastien Roy  * static int find_all_interfaces(struct lifconf *lifcp, char **buf,
1730f4b3ec61Sdh  *     int64_t lifc_flags)
1731f4b3ec61Sdh  *
17322b24ab6bSSebastien Roy  * It finds all active data links.
1733f4b3ec61Sdh  *
1734f4b3ec61Sdh  * It takes in input a pointer to struct lifconf to receive interfaces
1735f4b3ec61Sdh  * informations, a **char to hold allocated buffer, and a lifc_flags.
1736f4b3ec61Sdh  *
1737f4b3ec61Sdh  * Return values:
1738f4b3ec61Sdh  *  0 = everything OK
1739f4b3ec61Sdh  * -1 = problem
1740f4b3ec61Sdh  */
1741f4b3ec61Sdh static int
17422b24ab6bSSebastien Roy find_all_interfaces(struct lifconf *lifcp, char **buf, int64_t lifc_flags)
1743f4b3ec61Sdh {
1744f4b3ec61Sdh 	unsigned bufsize;
1745f4b3ec61Sdh 	int n;
1746f4b3ec61Sdh 	ni_t *nip;
1747f4b3ec61Sdh 	struct lifreq *lifrp;
1748d4d1f7bfSVasumathi Sundaram - Sun Microsystems 	dladm_status_t status;
1749f4b3ec61Sdh 
17502b24ab6bSSebastien Roy 	if (!dlh_opened) {
17512b24ab6bSSebastien Roy 		status = ifconfig_dladm_open(NULL, 0, NULL);
17522b24ab6bSSebastien Roy 		if (status != DLADM_STATUS_OK)
17532b24ab6bSSebastien Roy 			dladmerr_exit(status, "unable to open dladm handle");
1754d4d1f7bfSVasumathi Sundaram - Sun Microsystems 	}
1755d4d1f7bfSVasumathi Sundaram - Sun Microsystems 
17562b24ab6bSSebastien Roy 	(void) dlpi_walk(ni_entry, dlh, 0);
1757f4b3ec61Sdh 
17582b24ab6bSSebastien Roy 	/* Now, translate the linked list into a struct lifreq buffer */
1759f4b3ec61Sdh 	if (num_ni == 0) {
1760f4b3ec61Sdh 		lifcp->lifc_family = AF_UNSPEC;
1761f4b3ec61Sdh 		lifcp->lifc_flags = lifc_flags;
1762f4b3ec61Sdh 		lifcp->lifc_len = 0;
1763f4b3ec61Sdh 		lifcp->lifc_buf = NULL;
1764f4b3ec61Sdh 		return (0);
1765f4b3ec61Sdh 	}
1766f4b3ec61Sdh 
1767f4b3ec61Sdh 	bufsize = num_ni * sizeof (struct lifreq);
1768f4b3ec61Sdh 	if ((*buf = malloc(bufsize)) == NULL)
1769f4b3ec61Sdh 		Perror0_exit("find_all_interfaces: malloc failed");
1770f4b3ec61Sdh 
1771f4b3ec61Sdh 	lifcp->lifc_family = AF_UNSPEC;
1772f4b3ec61Sdh 	lifcp->lifc_flags = lifc_flags;
1773f4b3ec61Sdh 	lifcp->lifc_len = bufsize;
1774f4b3ec61Sdh 	lifcp->lifc_buf = *buf;
1775f4b3ec61Sdh 
1776f4b3ec61Sdh 	for (n = 0, lifrp = lifcp->lifc_req; n < num_ni; n++, lifrp++) {
1777f4b3ec61Sdh 		nip = ni_list;
1778f4b3ec61Sdh 		(void) strncpy(lifrp->lifr_name, nip->ni_name,
1779f4b3ec61Sdh 		    sizeof (lifr.lifr_name));
1780f4b3ec61Sdh 		ni_list = nip->ni_next;
1781f4b3ec61Sdh 		free(nip);
1782f4b3ec61Sdh 	}
1783f4b3ec61Sdh 	return (0);
1784f4b3ec61Sdh }
1785f4b3ec61Sdh 
17867c478bd9Sstevel@tonic-gate /*
17877c478bd9Sstevel@tonic-gate  * Create the next unused logical interface using the original name
17887c478bd9Sstevel@tonic-gate  * and assign the address (and mask if '/<n>' is part of the address).
17897c478bd9Sstevel@tonic-gate  * Use the new logical interface for subsequent subcommands by updating
17907c478bd9Sstevel@tonic-gate  * the name variable.
17917c478bd9Sstevel@tonic-gate  *
17927c478bd9Sstevel@tonic-gate  * This allows syntax like:
17937c478bd9Sstevel@tonic-gate  *	ifconfig le0 addif 109.106.86.130 netmask + up \
17947c478bd9Sstevel@tonic-gate  *	addif 109.106.86.131 netmask + up
17957c478bd9Sstevel@tonic-gate  */
17967c478bd9Sstevel@tonic-gate /* ARGSUSED */
17977c478bd9Sstevel@tonic-gate static int
17987c478bd9Sstevel@tonic-gate addif(char *str, int64_t param)
17997c478bd9Sstevel@tonic-gate {
18007c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
18017c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
18027c478bd9Sstevel@tonic-gate 	struct sockaddr_storage mask;
1803*6e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
1804*6e91bba0SGirish Moodalbail 	char cidraddr[BUFSIZ];
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	(void) strncpy(name, origname, sizeof (name));
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
18097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18107c478bd9Sstevel@tonic-gate 		    "ifconfig: addif: bad physical interface name %s\n",
18117c478bd9Sstevel@tonic-gate 		    name);
18127c478bd9Sstevel@tonic-gate 		exit(1);
18137c478bd9Sstevel@tonic-gate 	}
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	/*
18167c478bd9Sstevel@tonic-gate 	 * clear so parser will interpret next address as source followed
18177c478bd9Sstevel@tonic-gate 	 * by possible dest
18187c478bd9Sstevel@tonic-gate 	 */
18197c478bd9Sstevel@tonic-gate 	setaddr = 0;
18207c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(str, (struct sockaddr *)&laddr, &prefixlen);
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
18237c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
18247c478bd9Sstevel@tonic-gate 		/* Nothing there - ok */
18257c478bd9Sstevel@tonic-gate 		break;
18267c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
18277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18287c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", str);
18297c478bd9Sstevel@tonic-gate 		exit(1);
18307c478bd9Sstevel@tonic-gate 	default:
18317c478bd9Sstevel@tonic-gate 		(void) memset(&mask, 0, sizeof (mask));
18327c478bd9Sstevel@tonic-gate 		mask.ss_family = afp->af_af;
18337c478bd9Sstevel@tonic-gate 		if (afp->af_af == AF_INET6) {
18347c478bd9Sstevel@tonic-gate 			struct sockaddr_in6 *sin6;
18357c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&mask;
18367906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
18377c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin6->sin6_addr)) {
18387c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
18397c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
18407c478bd9Sstevel@tonic-gate 				    prefixlen);
18417c478bd9Sstevel@tonic-gate 				exit(1);
18427c478bd9Sstevel@tonic-gate 			}
18437c478bd9Sstevel@tonic-gate 		} else {
18447c478bd9Sstevel@tonic-gate 			struct sockaddr_in *sin;
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 			sin = (struct sockaddr_in *)&mask;
18477906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IP_ABITS,
18487c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin->sin_addr)) {
18497c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
18507c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
18517c478bd9Sstevel@tonic-gate 				    prefixlen);
18527c478bd9Sstevel@tonic-gate 				exit(1);
18537c478bd9Sstevel@tonic-gate 			}
18547c478bd9Sstevel@tonic-gate 		}
1855dd7a6f5fSkcpoon 		g_netmask_set = G_NETMASK_NIL;
18567c478bd9Sstevel@tonic-gate 		break;
18577c478bd9Sstevel@tonic-gate 	}
18587c478bd9Sstevel@tonic-gate 
1859dd7a6f5fSkcpoon 	/*
1860dd7a6f5fSkcpoon 	 * This is a "hack" to get around the problem of SIOCLIFADDIF.  The
1861dd7a6f5fSkcpoon 	 * problem is that this ioctl does not include the netmask when
1862dd7a6f5fSkcpoon 	 * adding a logical interface.  This is the same problem described
1863dd7a6f5fSkcpoon 	 * in the ifconfig() comments.  To get around this problem, we first
1864dd7a6f5fSkcpoon 	 * add the logical interface with a 0 address.  After that, we set
1865dd7a6f5fSkcpoon 	 * the netmask if provided.  Finally we set the interface address.
1866dd7a6f5fSkcpoon 	 */
18677c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1868dd7a6f5fSkcpoon 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	/* Note: no need to do DAD here since the interface isn't up yet. */
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0)
18737c478bd9Sstevel@tonic-gate 		Perror0_exit("addif: SIOCLIFADDIF");
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 	(void) printf("Created new logical interface %s\n",
18767c478bd9Sstevel@tonic-gate 	    lifr.lifr_name);
18777c478bd9Sstevel@tonic-gate 	(void) strncpy(name, lifr.lifr_name, sizeof (name));
18787c478bd9Sstevel@tonic-gate 
1879dd7a6f5fSkcpoon 	/*
1880dd7a6f5fSkcpoon 	 * Check and see if any "netmask" command is used and perform the
1881dd7a6f5fSkcpoon 	 * necessary operation.
1882dd7a6f5fSkcpoon 	 */
1883dd7a6f5fSkcpoon 	set_mask_lifreq(&lifr, &laddr, &mask);
1884*6e91bba0SGirish Moodalbail 
1885*6e91bba0SGirish Moodalbail 	/* This check is temporary until libipadm supports IPMP interfaces. */
1886*6e91bba0SGirish Moodalbail 	if (ifconfig_use_libipadm(s, name)) {
1887*6e91bba0SGirish Moodalbail 		/*
1888*6e91bba0SGirish Moodalbail 		 * We added the logical interface above before calling
1889*6e91bba0SGirish Moodalbail 		 * ipadm_create_addr(), because, with IPH_LEGACY, we need
1890*6e91bba0SGirish Moodalbail 		 * to do an addif for `ifconfig ce0 addif <addr>' but not for
1891*6e91bba0SGirish Moodalbail 		 * `ifconfig ce0 <addr>'. libipadm does not have a flag to
1892*6e91bba0SGirish Moodalbail 		 * to differentiate between these two cases. To keep it simple,
1893*6e91bba0SGirish Moodalbail 		 * we always create the logical interface and pass it to
1894*6e91bba0SGirish Moodalbail 		 * libipadm instead of requiring libipadm to addif for some
1895*6e91bba0SGirish Moodalbail 		 * cases and not do addif for other cases.
1896*6e91bba0SGirish Moodalbail 		 */
1897*6e91bba0SGirish Moodalbail 		istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name,
1898*6e91bba0SGirish Moodalbail 		    &ipaddr);
1899*6e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
1900*6e91bba0SGirish Moodalbail 			ipadmerr_exit(istatus, "addif");
1901*6e91bba0SGirish Moodalbail 
1902*6e91bba0SGirish Moodalbail 		if (strchr(str, '/') == NULL) {
1903*6e91bba0SGirish Moodalbail 			/*
1904*6e91bba0SGirish Moodalbail 			 * lifr.lifr_addr, which is updated by set_mask_lifreq()
1905*6e91bba0SGirish Moodalbail 			 * will contain the right mask to use.
1906*6e91bba0SGirish Moodalbail 			 */
1907*6e91bba0SGirish Moodalbail 			prefixlen = mask2plen(&lifr.lifr_addr);
1908*6e91bba0SGirish Moodalbail 			(void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d",
1909*6e91bba0SGirish Moodalbail 			    str, prefixlen);
1910*6e91bba0SGirish Moodalbail 			str = cidraddr;
1911*6e91bba0SGirish Moodalbail 		}
1912*6e91bba0SGirish Moodalbail 		istatus = ipadm_set_addr(ipaddr, str, af);
1913*6e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
1914*6e91bba0SGirish Moodalbail 			ipadmerr_exit(istatus, "could not set address");
1915*6e91bba0SGirish Moodalbail 		setaddr++;
1916*6e91bba0SGirish Moodalbail 		/*
1917*6e91bba0SGirish Moodalbail 		 * address will be set by the parser after nextarg
1918*6e91bba0SGirish Moodalbail 		 * has been scanned
1919*6e91bba0SGirish Moodalbail 		 */
1920*6e91bba0SGirish Moodalbail 		return (0);
1921*6e91bba0SGirish Moodalbail 	}
1922*6e91bba0SGirish Moodalbail 
1923dd7a6f5fSkcpoon 	/*
1924dd7a6f5fSkcpoon 	 * Only set the netmask if "netmask" command is used or a prefix is
1925dd7a6f5fSkcpoon 	 * provided.
1926dd7a6f5fSkcpoon 	 */
1927dd7a6f5fSkcpoon 	if (g_netmask_set == G_NETMASK_SET || prefixlen >= 0) {
1928*6e91bba0SGirish Moodalbail 		/*
1929*6e91bba0SGirish Moodalbail 		 * lifr.lifr_addr already contains netmask from
1930*6e91bba0SGirish Moodalbail 		 * set_mask_lifreq().
1931*6e91bba0SGirish Moodalbail 		 */
19327c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
19337c478bd9Sstevel@tonic-gate 			Perror0_exit("addif: SIOCSLIFNETMASK");
19347c478bd9Sstevel@tonic-gate 	}
1935dd7a6f5fSkcpoon 
1936dd7a6f5fSkcpoon 	/* Finally, we set the interface address. */
1937dd7a6f5fSkcpoon 	lifr.lifr_addr = laddr;
1938dd7a6f5fSkcpoon 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1939dd7a6f5fSkcpoon 		Perror0_exit("SIOCSLIFADDR");
1940dd7a6f5fSkcpoon 
19417c478bd9Sstevel@tonic-gate 	/*
19427c478bd9Sstevel@tonic-gate 	 * let parser know we got a source.
19437c478bd9Sstevel@tonic-gate 	 * Next address, if given, should be dest
19447c478bd9Sstevel@tonic-gate 	 */
19457c478bd9Sstevel@tonic-gate 	setaddr++;
19467c478bd9Sstevel@tonic-gate 	return (0);
19477c478bd9Sstevel@tonic-gate }
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate /*
19507c478bd9Sstevel@tonic-gate  * Remove a logical interface based on its IP address. Unlike addif
19517c478bd9Sstevel@tonic-gate  * there is no '/<n>' here.
19527c478bd9Sstevel@tonic-gate  * Verifies that the interface is down before it is removed.
19537c478bd9Sstevel@tonic-gate  */
19547c478bd9Sstevel@tonic-gate /* ARGSUSED */
19557c478bd9Sstevel@tonic-gate static int
19567c478bd9Sstevel@tonic-gate removeif(char *str, int64_t param)
19577c478bd9Sstevel@tonic-gate {
19587c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
1959*6e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
1960*6e91bba0SGirish Moodalbail 	ipadm_addr_info_t *ainfo, *ainfop;
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
19637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
19647c478bd9Sstevel@tonic-gate 		    "ifconfig: removeif: bad physical interface name %s\n",
19657c478bd9Sstevel@tonic-gate 		    name);
19667c478bd9Sstevel@tonic-gate 		exit(1);
19677c478bd9Sstevel@tonic-gate 	}
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(str, &laddr, NULL);
19707c478bd9Sstevel@tonic-gate 
1971*6e91bba0SGirish Moodalbail 	/*
1972*6e91bba0SGirish Moodalbail 	 * Following check is temporary until libipadm supports
1973*6e91bba0SGirish Moodalbail 	 * IPMP interfaces.
1974*6e91bba0SGirish Moodalbail 	 */
1975*6e91bba0SGirish Moodalbail 	if (!ifconfig_use_libipadm(s, name))
1976*6e91bba0SGirish Moodalbail 		goto delete;
1977*6e91bba0SGirish Moodalbail 
1978*6e91bba0SGirish Moodalbail 	/*
1979*6e91bba0SGirish Moodalbail 	 * Get all addresses and search this address among the active
1980*6e91bba0SGirish Moodalbail 	 * addresses. If an address object was found, delete using
1981*6e91bba0SGirish Moodalbail 	 * ipadm_delete_addr().
1982*6e91bba0SGirish Moodalbail 	 */
1983*6e91bba0SGirish Moodalbail 	istatus = ipadm_addr_info(iph, name, &ainfo, 0, LIFC_DEFAULT);
1984*6e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS)
1985*6e91bba0SGirish Moodalbail 		ipadmerr_exit(istatus, "removeif");
1986*6e91bba0SGirish Moodalbail 
1987*6e91bba0SGirish Moodalbail 	for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop))
1988*6e91bba0SGirish Moodalbail 		if (sockaddrcmp(ainfop->ia_ifa.ifa_addr, &laddr))
1989*6e91bba0SGirish Moodalbail 			break;
1990*6e91bba0SGirish Moodalbail 
1991*6e91bba0SGirish Moodalbail 	if (ainfop != NULL && ainfop->ia_aobjname[0] != '\0') {
1992*6e91bba0SGirish Moodalbail 		istatus = ipadm_delete_addr(iph, ainfop->ia_aobjname,
1993*6e91bba0SGirish Moodalbail 		    IPADM_OPT_ACTIVE);
1994*6e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
1995*6e91bba0SGirish Moodalbail 			ipadmerr_exit(istatus, "could not delete address");
1996*6e91bba0SGirish Moodalbail 		ipadm_free_addr_info(ainfo);
1997*6e91bba0SGirish Moodalbail 		return (0);
1998*6e91bba0SGirish Moodalbail 	}
1999*6e91bba0SGirish Moodalbail 	ipadm_free_addr_info(ainfo);
2000*6e91bba0SGirish Moodalbail 
2001*6e91bba0SGirish Moodalbail delete:
2002*6e91bba0SGirish Moodalbail 	/*
2003*6e91bba0SGirish Moodalbail 	 * An address object for this address was not found in ipadm.
2004*6e91bba0SGirish Moodalbail 	 * Delete with SIOCLIFREMOVEIF.
2005*6e91bba0SGirish Moodalbail 	 */
2006*6e91bba0SGirish Moodalbail 	lifr.lifr_addr = laddr;
20077c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
20087c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) {
20097c478bd9Sstevel@tonic-gate 		if (errno == EBUSY) {
20107c478bd9Sstevel@tonic-gate 			/* This can only happen if ipif_id = 0 */
20117c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
20127c478bd9Sstevel@tonic-gate 			    "ifconfig: removeif: can't remove interface: %s\n",
20137c478bd9Sstevel@tonic-gate 			    name);
20147c478bd9Sstevel@tonic-gate 			exit(1);
20157c478bd9Sstevel@tonic-gate 		}
20167c478bd9Sstevel@tonic-gate 		Perror0_exit("removeif: SIOCLIFREMOVEIF");
20177c478bd9Sstevel@tonic-gate 	}
20187c478bd9Sstevel@tonic-gate 	return (0);
20197c478bd9Sstevel@tonic-gate }
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate /*
20227c478bd9Sstevel@tonic-gate  * Set the address token for IPv6.
20237c478bd9Sstevel@tonic-gate  */
20247c478bd9Sstevel@tonic-gate /* ARGSUSED */
20257c478bd9Sstevel@tonic-gate static int
20267c478bd9Sstevel@tonic-gate setiftoken(char *addr, int64_t param)
20277c478bd9Sstevel@tonic-gate {
20287c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
20297c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 token;
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	in6_getaddr(addr, (struct sockaddr *)&token, &prefixlen);
20327c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
20337c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
20347c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
20357c478bd9Sstevel@tonic-gate 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
20367c478bd9Sstevel@tonic-gate 		exit(1);
20377c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
20387c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
20397c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
20407c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
20417c478bd9Sstevel@tonic-gate 		exit(1);
20427c478bd9Sstevel@tonic-gate 	default:
20437c478bd9Sstevel@tonic-gate 		break;
20447c478bd9Sstevel@tonic-gate 	}
20457c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &token, sizeof (token));
20467c478bd9Sstevel@tonic-gate 	lifr.lifr_addrlen = prefixlen;
20477c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
20487c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFTOKEN, (caddr_t)&lifr) < 0)  {
20497c478bd9Sstevel@tonic-gate 		Perror0_exit("setiftoken: SIOCSLIFTOKEN");
20507c478bd9Sstevel@tonic-gate 	}
20517c478bd9Sstevel@tonic-gate 	return (0);
20527c478bd9Sstevel@tonic-gate }
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate /* ARGSUSED */
20557c478bd9Sstevel@tonic-gate static int
2056e11c3f44Smeem setifgroupname(char *grname, int64_t param)
20577c478bd9Sstevel@tonic-gate {
2058e11c3f44Smeem 	lifgroupinfo_t		lifgr;
2059e11c3f44Smeem 	struct lifreq		lifrl;
2060e11c3f44Smeem 	ifaddrlistx_t		*ifaddrp, *nextifaddrp;
2061e11c3f44Smeem 	ifaddrlistx_t		*ifaddrs = NULL, *downaddrs = NULL;
2062e11c3f44Smeem 	int			af;
2063e11c3f44Smeem 
20647c478bd9Sstevel@tonic-gate 	if (debug) {
20657c478bd9Sstevel@tonic-gate 		(void) printf("Setting groupname %s on interface %s\n",
2066e11c3f44Smeem 		    grname, name);
20677c478bd9Sstevel@tonic-gate 	}
20687c478bd9Sstevel@tonic-gate 
2069e11c3f44Smeem 	(void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ);
2070e11c3f44Smeem 	(void) strlcpy(lifrl.lifr_groupname, grname, LIFGRNAMSIZ);
2071e11c3f44Smeem 
2072e11c3f44Smeem 	while (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) {
2073e11c3f44Smeem 		switch (errno) {
2074e11c3f44Smeem 		case ENOENT:
2075e11c3f44Smeem 			/*
2076e11c3f44Smeem 			 * The group doesn't yet exist; create it and repeat.
2077e11c3f44Smeem 			 */
2078e11c3f44Smeem 			af = afp->af_af;
2079e11c3f44Smeem 			if (create_ipmp(grname, af, NULL, _B_TRUE) == -1) {
2080e11c3f44Smeem 				if (errno == EEXIST)
2081e11c3f44Smeem 					continue;
2082e11c3f44Smeem 
2083e11c3f44Smeem 				Perror2(grname, "cannot create IPMP group");
2084e11c3f44Smeem 				goto fail;
2085e11c3f44Smeem 			}
2086e11c3f44Smeem 			continue;
2087e11c3f44Smeem 
2088e11c3f44Smeem 		case EALREADY:
2089e11c3f44Smeem 			/*
2090e11c3f44Smeem 			 * The interface is already in another group; must
2091e11c3f44Smeem 			 * remove existing membership first.
2092e11c3f44Smeem 			 */
2093e11c3f44Smeem 			lifrl.lifr_groupname[0] = '\0';
2094e11c3f44Smeem 			if (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) {
2095e11c3f44Smeem 				Perror2(name, "cannot remove existing "
2096e11c3f44Smeem 				    "IPMP group membership");
2097e11c3f44Smeem 				goto fail;
2098e11c3f44Smeem 			}
2099e11c3f44Smeem 			(void) strlcpy(lifrl.lifr_groupname, grname,
2100e11c3f44Smeem 			    LIFGRNAMSIZ);
2101e11c3f44Smeem 			continue;
2102e11c3f44Smeem 
2103e11c3f44Smeem 		case EAFNOSUPPORT:
2104e11c3f44Smeem 			/*
2105e11c3f44Smeem 			 * The group exists, but it's not configured with the
2106e11c3f44Smeem 			 * address families the interface needs.  Since only
2107e11c3f44Smeem 			 * two address families are currently supported, just
2108e11c3f44Smeem 			 * configure the "other" address family.  Note that we
2109e11c3f44Smeem 			 * may race with group deletion or creation by another
2110e11c3f44Smeem 			 * process (ENOENT or EEXIST); in such cases we repeat
2111e11c3f44Smeem 			 * our original SIOCSLIFGROUPNAME.
2112e11c3f44Smeem 			 */
2113e11c3f44Smeem 			(void) strlcpy(lifgr.gi_grname, grname, LIFGRNAMSIZ);
2114e11c3f44Smeem 			if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1) {
2115e11c3f44Smeem 				if (errno == ENOENT)
2116e11c3f44Smeem 					continue;
2117e11c3f44Smeem 
2118e11c3f44Smeem 				Perror2(grname, "SIOCGLIFGROUPINFO");
2119e11c3f44Smeem 				goto fail;
2120e11c3f44Smeem 			}
2121e11c3f44Smeem 
2122e11c3f44Smeem 			af = lifgr.gi_v4 ? AF_INET6 : AF_INET;
2123e11c3f44Smeem 			if (create_ipmp(grname, af, lifgr.gi_grifname,
2124e11c3f44Smeem 			    _B_TRUE) == -1) {
2125e11c3f44Smeem 				if (errno == EEXIST)
2126e11c3f44Smeem 					continue;
2127e11c3f44Smeem 
2128e11c3f44Smeem 				Perror2(grname, "cannot configure IPMP group");
2129e11c3f44Smeem 				goto fail;
2130e11c3f44Smeem 			}
2131e11c3f44Smeem 			continue;
2132e11c3f44Smeem 
2133e11c3f44Smeem 		case EADDRINUSE:
2134e11c3f44Smeem 			/*
2135e11c3f44Smeem 			 * Some addresses are in-use (or under control of DAD).
2136e11c3f44Smeem 			 * Bring them down and retry the group join operation.
2137e11c3f44Smeem 			 * We will bring them back up after the interface has
2138e11c3f44Smeem 			 * been placed in the group.
2139e11c3f44Smeem 			 */
2140e11c3f44Smeem 			if (ifaddrlistx(lifrl.lifr_name, IFF_UP|IFF_DUPLICATE,
2141e11c3f44Smeem 			    0, &ifaddrs) == -1) {
2142e11c3f44Smeem 				Perror2(grname, "cannot get address list");
2143e11c3f44Smeem 				goto fail;
2144e11c3f44Smeem 			}
2145e11c3f44Smeem 
2146e11c3f44Smeem 			ifaddrp = ifaddrs;
2147e11c3f44Smeem 			for (; ifaddrp != NULL; ifaddrp = nextifaddrp) {
2148e11c3f44Smeem 				if (!ifaddr_down(ifaddrp)) {
2149e11c3f44Smeem 					ifaddrs = ifaddrp;
2150e11c3f44Smeem 					goto fail;
2151e11c3f44Smeem 				}
2152e11c3f44Smeem 				nextifaddrp = ifaddrp->ia_next;
2153e11c3f44Smeem 				ifaddrp->ia_next = downaddrs;
2154e11c3f44Smeem 				downaddrs = ifaddrp;
2155e11c3f44Smeem 			}
2156e11c3f44Smeem 			ifaddrs = NULL;
2157e11c3f44Smeem 			continue;
2158e11c3f44Smeem 
2159e11c3f44Smeem 		case EADDRNOTAVAIL: {
2160e11c3f44Smeem 			/*
2161e11c3f44Smeem 			 * Some data addresses are under application control.
2162e11c3f44Smeem 			 * For some of these (e.g., ADDRCONF), the application
2163e11c3f44Smeem 			 * should remove the address, in which case we retry a
2164e11c3f44Smeem 			 * few times (since the application's action is not
2165e11c3f44Smeem 			 * atomic with respect to us) before bailing out and
2166e11c3f44Smeem 			 * informing the user.
2167e11c3f44Smeem 			 */
2168e11c3f44Smeem 			int ntries, nappaddr = 0;
2169e11c3f44Smeem 			const if_appflags_t *iap = if_appflags_tbl;
2170e11c3f44Smeem 
2171e11c3f44Smeem 			for (; iap->ia_app != NULL; iap++) {
2172e11c3f44Smeem 				ntries = 0;
2173e11c3f44Smeem again:
2174e11c3f44Smeem 				if (ifaddrlistx(lifrl.lifr_name, iap->ia_flag,
2175e11c3f44Smeem 				    IFF_NOFAILOVER, &ifaddrs) == -1) {
2176e11c3f44Smeem 					(void) fprintf(stderr, "ifconfig: %s: "
2177e11c3f44Smeem 					    "cannot get data addresses managed "
2178e11c3f44Smeem 					    "by %s\n", lifrl.lifr_name,
2179e11c3f44Smeem 					    iap->ia_app);
2180e11c3f44Smeem 					goto fail;
2181e11c3f44Smeem 				}
2182e11c3f44Smeem 
2183e11c3f44Smeem 				if (ifaddrs == NULL)
2184e11c3f44Smeem 					continue;
2185e11c3f44Smeem 
2186e11c3f44Smeem 				ifaddrlistx_free(ifaddrs);
2187e11c3f44Smeem 				ifaddrs = NULL;
2188e11c3f44Smeem 
2189e11c3f44Smeem 				if (++ntries < iap->ia_tries) {
2190e11c3f44Smeem 					(void) poll(NULL, 0, 100);
2191e11c3f44Smeem 					goto again;
2192e11c3f44Smeem 				}
2193e11c3f44Smeem 
2194e11c3f44Smeem 				(void) fprintf(stderr, "ifconfig: cannot join "
2195e11c3f44Smeem 				    "IPMP group: %s has data addresses managed "
2196e11c3f44Smeem 				    "by %s\n", lifrl.lifr_name, iap->ia_app);
2197e11c3f44Smeem 				nappaddr++;
2198e11c3f44Smeem 			}
2199e11c3f44Smeem 			if (nappaddr > 0)
2200e11c3f44Smeem 				goto fail;
2201e11c3f44Smeem 			continue;
2202e11c3f44Smeem 		}
2203e11c3f44Smeem 		default:
2204e11c3f44Smeem 			Perror2(name, "SIOCSLIFGROUPNAME");
2205e11c3f44Smeem 			goto fail;
2206e11c3f44Smeem 		}
22077c478bd9Sstevel@tonic-gate 	}
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 	/*
2210e11c3f44Smeem 	 * If there were addresses that we had to bring down, it's time to
2211e11c3f44Smeem 	 * bring them up again.  As part of bringing them up, the kernel will
2212e11c3f44Smeem 	 * automatically move them to the new IPMP interface.
22137c478bd9Sstevel@tonic-gate 	 */
2214e11c3f44Smeem 	for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
2215e11c3f44Smeem 		if (!ifaddr_up(ifaddrp) && errno != ENXIO) {
2216e11c3f44Smeem 			(void) fprintf(stderr, "ifconfig: cannot bring back up "
2217e11c3f44Smeem 			    "%s: %s\n", ifaddrp->ia_name, strerror(errno));
2218e11c3f44Smeem 		}
22197c478bd9Sstevel@tonic-gate 	}
2220e11c3f44Smeem 	ifaddrlistx_free(downaddrs);
2221e11c3f44Smeem 	return (0);
2222e11c3f44Smeem fail:
22237c478bd9Sstevel@tonic-gate 	/*
2224e11c3f44Smeem 	 * Attempt to bring back up any interfaces that we downed.
22257c478bd9Sstevel@tonic-gate 	 */
2226e11c3f44Smeem 	for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
2227e11c3f44Smeem 		if (!ifaddr_up(ifaddrp) && errno != ENXIO) {
2228e11c3f44Smeem 			(void) fprintf(stderr, "ifconfig: cannot bring back up "
2229e11c3f44Smeem 			    "%s: %s\n", ifaddrp->ia_name, strerror(errno));
2230e11c3f44Smeem 		}
22317c478bd9Sstevel@tonic-gate 	}
2232e11c3f44Smeem 	ifaddrlistx_free(downaddrs);
2233e11c3f44Smeem 	ifaddrlistx_free(ifaddrs);
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 	/*
2236e11c3f44Smeem 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2237e11c3f44Smeem 	 * into the exit status, so we're forced to explicitly exit().
22387c478bd9Sstevel@tonic-gate 	 */
2239e11c3f44Smeem 	exit(1);
2240e11c3f44Smeem 	/* NOTREACHED */
2241e11c3f44Smeem }
22427c478bd9Sstevel@tonic-gate 
2243e11c3f44Smeem static boolean_t
2244e11c3f44Smeem modcheck(const char *ifname)
2245e11c3f44Smeem {
2246e11c3f44Smeem 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2247e11c3f44Smeem 
2248e11c3f44Smeem 	if (ioctl(s, SIOCGLIFFLAGS, &lifr) < 0) {
2249e11c3f44Smeem 		Perror0("SIOCGLIFFLAGS");
2250e11c3f44Smeem 		return (_B_FALSE);
22517c478bd9Sstevel@tonic-gate 	}
22527c478bd9Sstevel@tonic-gate 
2253e11c3f44Smeem 	if (lifr.lifr_flags & IFF_IPMP) {
2254e11c3f44Smeem 		(void) fprintf(stderr, "ifconfig: %s: module operations not"
2255e11c3f44Smeem 		    " supported on IPMP interfaces\n", ifname);
2256e11c3f44Smeem 		return (_B_FALSE);
2257e11c3f44Smeem 	}
2258e11c3f44Smeem 	if (lifr.lifr_flags & IFF_VIRTUAL) {
2259e11c3f44Smeem 		(void) fprintf(stderr, "ifconfig: %s: module operations not"
2260e11c3f44Smeem 		    " supported on virtual IP interfaces\n", ifname);
2261e11c3f44Smeem 		return (_B_FALSE);
2262e11c3f44Smeem 	}
2263e11c3f44Smeem 	return (_B_TRUE);
2264e11c3f44Smeem }
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate /*
22677c478bd9Sstevel@tonic-gate  * To list all the modules above a given network interface.
22687c478bd9Sstevel@tonic-gate  */
22697c478bd9Sstevel@tonic-gate /* ARGSUSED */
22707c478bd9Sstevel@tonic-gate static int
22717c478bd9Sstevel@tonic-gate modlist(char *null, int64_t param)
22727c478bd9Sstevel@tonic-gate {
2273fc80c0dfSnordmark 	int muxid_fd;
22747c478bd9Sstevel@tonic-gate 	int muxfd;
22757c478bd9Sstevel@tonic-gate 	int ipfd_lowstr;
22767c478bd9Sstevel@tonic-gate 	int arpfd_lowstr;
22777c478bd9Sstevel@tonic-gate 	int num_mods;
22787c478bd9Sstevel@tonic-gate 	int i;
22797c478bd9Sstevel@tonic-gate 	struct str_list strlist;
22807c478bd9Sstevel@tonic-gate 	int orig_arpid;
22817c478bd9Sstevel@tonic-gate 
2282e11c3f44Smeem 	/*
2283e11c3f44Smeem 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2284e11c3f44Smeem 	 * into the exit status, so we're forced to explicitly exit().
2285e11c3f44Smeem 	 */
2286e11c3f44Smeem 	if (!modcheck(name))
2287e11c3f44Smeem 		exit(1);
2288e11c3f44Smeem 
2289fc80c0dfSnordmark 	if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
22907c478bd9Sstevel@tonic-gate 	    &orig_arpid) < 0) {
22917c478bd9Sstevel@tonic-gate 		return (-1);
22927c478bd9Sstevel@tonic-gate 	}
22937c478bd9Sstevel@tonic-gate 	if ((num_mods = ioctl(ipfd_lowstr, I_LIST, NULL)) < 0) {
22947c478bd9Sstevel@tonic-gate 		Perror0("cannot I_LIST to get the number of modules");
22957c478bd9Sstevel@tonic-gate 	} else {
22967c478bd9Sstevel@tonic-gate 		if (debug > 0) {
22977c478bd9Sstevel@tonic-gate 			(void) printf("Listing (%d) modules above %s\n",
22987c478bd9Sstevel@tonic-gate 			    num_mods, name);
22997c478bd9Sstevel@tonic-gate 		}
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 		strlist.sl_nmods = num_mods;
23027c478bd9Sstevel@tonic-gate 		strlist.sl_modlist = malloc(sizeof (struct str_mlist) *
23037c478bd9Sstevel@tonic-gate 		    num_mods);
23047c478bd9Sstevel@tonic-gate 		if (strlist.sl_modlist == NULL) {
23057c478bd9Sstevel@tonic-gate 			Perror0("cannot malloc");
23067c478bd9Sstevel@tonic-gate 		} else {
23077c478bd9Sstevel@tonic-gate 			if (ioctl(ipfd_lowstr, I_LIST, (caddr_t)&strlist) < 0) {
23087c478bd9Sstevel@tonic-gate 				Perror0("cannot I_LIST for module names");
23097c478bd9Sstevel@tonic-gate 			} else {
23107c478bd9Sstevel@tonic-gate 				for (i = 0; i < strlist.sl_nmods; i++) {
2311fc80c0dfSnordmark 					(void) printf("%d %s\n", i,
2312fc80c0dfSnordmark 					    strlist.sl_modlist[i].l_name);
23137c478bd9Sstevel@tonic-gate 				}
23147c478bd9Sstevel@tonic-gate 			}
23157c478bd9Sstevel@tonic-gate 			free(strlist.sl_modlist);
23167c478bd9Sstevel@tonic-gate 		}
23177c478bd9Sstevel@tonic-gate 	}
2318fc80c0dfSnordmark 	return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2319fc80c0dfSnordmark 	    orig_arpid));
23207c478bd9Sstevel@tonic-gate }
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate #define	MODINSERT_OP	'i'
23237c478bd9Sstevel@tonic-gate #define	MODREMOVE_OP	'r'
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate /*
23267c478bd9Sstevel@tonic-gate  * To insert a module to the stream of the interface.  It is just a
23277c478bd9Sstevel@tonic-gate  * wrapper.  The real function is modop().
23287c478bd9Sstevel@tonic-gate  */
23297c478bd9Sstevel@tonic-gate /* ARGSUSED */
23307c478bd9Sstevel@tonic-gate static int
23317c478bd9Sstevel@tonic-gate modinsert(char *arg, int64_t param)
23327c478bd9Sstevel@tonic-gate {
23337c478bd9Sstevel@tonic-gate 	return (modop(arg, MODINSERT_OP));
23347c478bd9Sstevel@tonic-gate }
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate /*
23377c478bd9Sstevel@tonic-gate  * To remove a module from the stream of the interface.  It is just a
23387c478bd9Sstevel@tonic-gate  * wrapper.  The real function is modop().
23397c478bd9Sstevel@tonic-gate  */
23407c478bd9Sstevel@tonic-gate /* ARGSUSED */
23417c478bd9Sstevel@tonic-gate static int
23427c478bd9Sstevel@tonic-gate modremove(char *arg, int64_t param)
23437c478bd9Sstevel@tonic-gate {
23447c478bd9Sstevel@tonic-gate 	return (modop(arg, MODREMOVE_OP));
23457c478bd9Sstevel@tonic-gate }
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate /*
23487c478bd9Sstevel@tonic-gate  * Helper function for mod*() functions.  It gets a fd to the lower IP
23497c478bd9Sstevel@tonic-gate  * stream and I_PUNLINK's the lower stream.  It also initializes the
23507c478bd9Sstevel@tonic-gate  * global variable lifr.
23517c478bd9Sstevel@tonic-gate  *
23527c478bd9Sstevel@tonic-gate  * Param:
2353fc80c0dfSnordmark  *	int *muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2354fc80c0dfSnordmark  *	int *muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2355fc80c0dfSnordmark  *	int *ipfd_lowstr: fd to the lower IP stream.
2356fc80c0dfSnordmark  *	int *arpfd_lowstr: fd to the lower ARP stream.
23577c478bd9Sstevel@tonic-gate  *
23587c478bd9Sstevel@tonic-gate  * Return:
23597c478bd9Sstevel@tonic-gate  *	-1 if operation fails, 0 otherwise.
23607c478bd9Sstevel@tonic-gate  *
2361e11c3f44Smeem  * Please see the big block comment above ifplumb() for the logic of the
2362e11c3f44Smeem  * PLINK/PUNLINK
23637c478bd9Sstevel@tonic-gate  */
23647c478bd9Sstevel@tonic-gate static int
2365fc80c0dfSnordmark ip_domux2fd(int *muxfd, int *muxid_fd, int *ipfd_lowstr, int *arpfd_lowstr,
2366fc80c0dfSnordmark     int *orig_arpid)
23677c478bd9Sstevel@tonic-gate {
23687c478bd9Sstevel@tonic-gate 	uint64_t	flags;
23697c478bd9Sstevel@tonic-gate 	char		*udp_dev_name;
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	*orig_arpid = 0;
23727c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
23737c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
23747c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
23757c478bd9Sstevel@tonic-gate 	}
23767c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
23777c478bd9Sstevel@tonic-gate 	if (flags & IFF_IPV4) {
23787c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP_DEV_NAME;
23797c478bd9Sstevel@tonic-gate 	} else if (flags & IFF_IPV6) {
23807c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP6_DEV_NAME;
23817c478bd9Sstevel@tonic-gate 	} else {
23827c478bd9Sstevel@tonic-gate 		return (-1);
23837c478bd9Sstevel@tonic-gate 	}
23847c478bd9Sstevel@tonic-gate 
2385fc80c0dfSnordmark 	if ((*muxid_fd = open(udp_dev_name, O_RDWR)) < 0) {
2386fc80c0dfSnordmark 		Perror2("open", udp_dev_name);
23877c478bd9Sstevel@tonic-gate 		return (-1);
23887c478bd9Sstevel@tonic-gate 	}
2389fc80c0dfSnordmark 	if (ioctl(*muxid_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
2390fc80c0dfSnordmark 		Perror2("SIOCGLIFMUXID", udp_dev_name);
23917c478bd9Sstevel@tonic-gate 		return (-1);
23927c478bd9Sstevel@tonic-gate 	}
23937c478bd9Sstevel@tonic-gate 	if (debug > 0) {
23947c478bd9Sstevel@tonic-gate 		(void) printf("ARP_muxid %d IP_muxid %d\n",
23957c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid, lifr.lifr_ip_muxid);
23967c478bd9Sstevel@tonic-gate 	}
23977c478bd9Sstevel@tonic-gate 
2398fc80c0dfSnordmark 	/*
2399fc80c0dfSnordmark 	 * Use /dev/udp{,6} as the mux to avoid linkcycles.
2400fc80c0dfSnordmark 	 */
2401*6e91bba0SGirish Moodalbail 	if (ipadm_open_arp_on_udp(udp_dev_name, muxfd) != IPADM_SUCCESS)
24027c478bd9Sstevel@tonic-gate 		return (-1);
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 	if (lifr.lifr_arp_muxid != 0) {
24057c478bd9Sstevel@tonic-gate 		if ((*arpfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
24067c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid)) < 0) {
24077c478bd9Sstevel@tonic-gate 			if ((errno == EINVAL) &&
24087c478bd9Sstevel@tonic-gate 			    (flags & (IFF_NOARP | IFF_IPV6))) {
24097c478bd9Sstevel@tonic-gate 				/*
24107c478bd9Sstevel@tonic-gate 				 * Some plumbing utilities set the muxid to
24117c478bd9Sstevel@tonic-gate 				 * -1 or some invalid value to signify that
24127c478bd9Sstevel@tonic-gate 				 * there is no arp stream. Set the muxid to 0
24137c478bd9Sstevel@tonic-gate 				 * before trying to unplumb the IP stream.
24147c478bd9Sstevel@tonic-gate 				 * IP does not allow the IP stream to be
24157c478bd9Sstevel@tonic-gate 				 * unplumbed if it sees a non-null arp muxid,
24167c478bd9Sstevel@tonic-gate 				 * for consistency of IP-ARP streams.
24177c478bd9Sstevel@tonic-gate 				 */
24187c478bd9Sstevel@tonic-gate 				*orig_arpid = lifr.lifr_arp_muxid;
24197c478bd9Sstevel@tonic-gate 				lifr.lifr_arp_muxid = 0;
2420fc80c0dfSnordmark 				(void) ioctl(*muxid_fd, SIOCSLIFMUXID,
24217c478bd9Sstevel@tonic-gate 				    (caddr_t)&lifr);
24227c478bd9Sstevel@tonic-gate 				*arpfd_lowstr = -1;
24237c478bd9Sstevel@tonic-gate 			} else {
24247c478bd9Sstevel@tonic-gate 				Perror0("_I_MUXID2FD");
24257c478bd9Sstevel@tonic-gate 				return (-1);
24267c478bd9Sstevel@tonic-gate 			}
24277c478bd9Sstevel@tonic-gate 		} else if (ioctl(*muxfd, I_PUNLINK,
24287c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid) < 0) {
24297c478bd9Sstevel@tonic-gate 			Perror2("I_PUNLINK", udp_dev_name);
24307c478bd9Sstevel@tonic-gate 			return (-1);
24317c478bd9Sstevel@tonic-gate 		}
24327c478bd9Sstevel@tonic-gate 	} else {
24337c478bd9Sstevel@tonic-gate 		*arpfd_lowstr = -1;
24347c478bd9Sstevel@tonic-gate 	}
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 	if ((*ipfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
24377c478bd9Sstevel@tonic-gate 	    lifr.lifr_ip_muxid)) < 0) {
24387c478bd9Sstevel@tonic-gate 		Perror0("_I_MUXID2FD");
24397c478bd9Sstevel@tonic-gate 		/* Undo any changes we made */
24407c478bd9Sstevel@tonic-gate 		if (*orig_arpid != 0) {
24417c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = *orig_arpid;
2442fc80c0dfSnordmark 			(void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
24437c478bd9Sstevel@tonic-gate 		}
24447c478bd9Sstevel@tonic-gate 		return (-1);
24457c478bd9Sstevel@tonic-gate 	}
24467c478bd9Sstevel@tonic-gate 	if (ioctl(*muxfd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) {
24477c478bd9Sstevel@tonic-gate 		Perror2("I_PUNLINK", udp_dev_name);
24487c478bd9Sstevel@tonic-gate 		/* Undo any changes we made */
24497c478bd9Sstevel@tonic-gate 		if (*orig_arpid != 0) {
24507c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = *orig_arpid;
2451fc80c0dfSnordmark 			(void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
24527c478bd9Sstevel@tonic-gate 		}
24537c478bd9Sstevel@tonic-gate 		return (-1);
24547c478bd9Sstevel@tonic-gate 	}
24557c478bd9Sstevel@tonic-gate 	return (0);
24567c478bd9Sstevel@tonic-gate }
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate /*
24597c478bd9Sstevel@tonic-gate  * Helper function for mod*() functions.  It I_PLINK's back the upper and
24607c478bd9Sstevel@tonic-gate  * lower IP streams.  Note that this function must be called after
24617c478bd9Sstevel@tonic-gate  * ip_domux2fd().  In ip_domux2fd(), the global variable lifr is initialized
24627c478bd9Sstevel@tonic-gate  * and ip_plink() needs information in lifr.  So ip_domux2fd() and ip_plink()
24637c478bd9Sstevel@tonic-gate  * must be called in pairs.
24647c478bd9Sstevel@tonic-gate  *
24657c478bd9Sstevel@tonic-gate  * Param:
2466fc80c0dfSnordmark  *	int muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2467fc80c0dfSnordmark  *	int muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2468fc80c0dfSnordmark  *	int ipfd_lowstr: fd to the lower IP stream.
2469fc80c0dfSnordmark  *	int arpfd_lowstr: fd to the lower ARP stream.
24707c478bd9Sstevel@tonic-gate  *
24717c478bd9Sstevel@tonic-gate  * Return:
24727c478bd9Sstevel@tonic-gate  *	-1 if operation fails, 0 otherwise.
24737c478bd9Sstevel@tonic-gate  *
2474e11c3f44Smeem  * Please see the big block comment above ifplumb() for the logic of the
2475e11c3f44Smeem  * PLINK/PUNLINK
24767c478bd9Sstevel@tonic-gate  */
24777c478bd9Sstevel@tonic-gate static int
2478fc80c0dfSnordmark ip_plink(int muxfd, int muxid_fd, int ipfd_lowstr, int arpfd_lowstr,
2479fc80c0dfSnordmark     int orig_arpid)
24807c478bd9Sstevel@tonic-gate {
24817c478bd9Sstevel@tonic-gate 	int ip_muxid;
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate 	ip_muxid = ioctl(muxfd, I_PLINK, ipfd_lowstr);
24847c478bd9Sstevel@tonic-gate 	if (ip_muxid < 0) {
24857c478bd9Sstevel@tonic-gate 		Perror2("I_PLINK", UDP_DEV_NAME);
24867c478bd9Sstevel@tonic-gate 		return (-1);
24877c478bd9Sstevel@tonic-gate 	}
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 	/*
24907c478bd9Sstevel@tonic-gate 	 * If there is an arp stream, plink it. If there is no
24917c478bd9Sstevel@tonic-gate 	 * arp stream, then it is possible that the plumbing
24927c478bd9Sstevel@tonic-gate 	 * utility could have stored any value in the arp_muxid.
24937c478bd9Sstevel@tonic-gate 	 * If so, restore it from orig_arpid.
24947c478bd9Sstevel@tonic-gate 	 */
24957c478bd9Sstevel@tonic-gate 	if (arpfd_lowstr != -1) {
24967c478bd9Sstevel@tonic-gate 		if (ioctl(muxfd, I_PLINK, arpfd_lowstr) < 0) {
24977c478bd9Sstevel@tonic-gate 			Perror2("I_PLINK", UDP_DEV_NAME);
24987c478bd9Sstevel@tonic-gate 			return (-1);
24997c478bd9Sstevel@tonic-gate 		}
25007c478bd9Sstevel@tonic-gate 	} else if (orig_arpid != 0) {
25017c478bd9Sstevel@tonic-gate 		/* Undo the changes we did in ip_domux2fd */
25027c478bd9Sstevel@tonic-gate 		lifr.lifr_arp_muxid = orig_arpid;
25037c478bd9Sstevel@tonic-gate 		lifr.lifr_ip_muxid = ip_muxid;
2504fc80c0dfSnordmark 		(void) ioctl(muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
25057c478bd9Sstevel@tonic-gate 	}
25067c478bd9Sstevel@tonic-gate 
2507fc80c0dfSnordmark 	(void) close(muxfd);
2508fc80c0dfSnordmark 	(void) close(muxid_fd);
25097c478bd9Sstevel@tonic-gate 	return (0);
25107c478bd9Sstevel@tonic-gate }
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate /*
25137c478bd9Sstevel@tonic-gate  * The real function to perform module insertion/removal.
25147c478bd9Sstevel@tonic-gate  *
25157c478bd9Sstevel@tonic-gate  * Param:
25167c478bd9Sstevel@tonic-gate  *	char *arg: the argument string module_name@position
25177c478bd9Sstevel@tonic-gate  *	char op: operation, either MODINSERT_OP or MODREMOVE_OP.
25187c478bd9Sstevel@tonic-gate  *
25197c478bd9Sstevel@tonic-gate  * Return:
25207c478bd9Sstevel@tonic-gate  *	Before doing ip_domux2fd(), this function calls exit(1) in case of
25217c478bd9Sstevel@tonic-gate  *	error.  After ip_domux2fd() is done, it returns -1 for error, 0
25227c478bd9Sstevel@tonic-gate  *	otherwise.
25237c478bd9Sstevel@tonic-gate  */
25247c478bd9Sstevel@tonic-gate static int
25257c478bd9Sstevel@tonic-gate modop(char *arg, char op)
25267c478bd9Sstevel@tonic-gate {
25277c478bd9Sstevel@tonic-gate 	char *pos_p;
25287c478bd9Sstevel@tonic-gate 	int muxfd;
2529fc80c0dfSnordmark 	int muxid_fd;
25307c478bd9Sstevel@tonic-gate 	int ipfd_lowstr;  /* IP stream (lower stream of mux) to be plinked */
25317c478bd9Sstevel@tonic-gate 	int arpfd_lowstr; /* ARP stream (lower stream of mux) to be plinked */
25327c478bd9Sstevel@tonic-gate 	struct strmodconf mod;
25337c478bd9Sstevel@tonic-gate 	char *at_char = "@";
25347c478bd9Sstevel@tonic-gate 	char *arg_str;
25357c478bd9Sstevel@tonic-gate 	int orig_arpid;
25367c478bd9Sstevel@tonic-gate 
2537e11c3f44Smeem 	/*
2538e11c3f44Smeem 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2539e11c3f44Smeem 	 * into the exit status, so we're forced to explicitly exit().
2540e11c3f44Smeem 	 */
2541e11c3f44Smeem 	if (!modcheck(name))
2542e11c3f44Smeem 		exit(1);
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 	/* Need to save the original string for -a option. */
25457c478bd9Sstevel@tonic-gate 	if ((arg_str = malloc(strlen(arg) + 1)) == NULL) {
25467c478bd9Sstevel@tonic-gate 		Perror0("cannot malloc");
25477c478bd9Sstevel@tonic-gate 		return (-1);
25487c478bd9Sstevel@tonic-gate 	}
25497c478bd9Sstevel@tonic-gate 	(void) strcpy(arg_str, arg);
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	if (*arg_str == *at_char) {
25527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
25537c478bd9Sstevel@tonic-gate 		    "ifconfig: must supply a module name\n");
25547c478bd9Sstevel@tonic-gate 		exit(1);
25557c478bd9Sstevel@tonic-gate 	}
25567c478bd9Sstevel@tonic-gate 	mod.mod_name = strtok(arg_str, at_char);
25577c478bd9Sstevel@tonic-gate 	if (strlen(mod.mod_name) > FMNAMESZ) {
25587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: module name too long: %s\n",
25597c478bd9Sstevel@tonic-gate 		    mod.mod_name);
25607c478bd9Sstevel@tonic-gate 		exit(1);
25617c478bd9Sstevel@tonic-gate 	}
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	/*
25647c478bd9Sstevel@tonic-gate 	 * Need to make sure that the core TCP/IP stack modules are not
25657c478bd9Sstevel@tonic-gate 	 * removed.  Otherwise, "bad" things can happen.  If a module
25667c478bd9Sstevel@tonic-gate 	 * is removed and inserted back, it loses its old state.  But
25677c478bd9Sstevel@tonic-gate 	 * the modules above it still have the old state.  E.g. IP assumes
25687c478bd9Sstevel@tonic-gate 	 * fast data path while tunnel after re-inserted assumes that it can
25697c478bd9Sstevel@tonic-gate 	 * receive M_DATA only in fast data path for which it does not have
25707c478bd9Sstevel@tonic-gate 	 * any state.  This is a general caveat of _I_REMOVE/_I_INSERT.
25717c478bd9Sstevel@tonic-gate 	 */
25727c478bd9Sstevel@tonic-gate 	if (op == MODREMOVE_OP &&
25737c478bd9Sstevel@tonic-gate 	    (strcmp(mod.mod_name, ARP_MOD_NAME) == 0 ||
25742b24ab6bSSebastien Roy 	    strcmp(mod.mod_name, IP_MOD_NAME) == 0)) {
25757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: cannot remove %s\n",
25767c478bd9Sstevel@tonic-gate 		    mod.mod_name);
25777c478bd9Sstevel@tonic-gate 		exit(1);
25787c478bd9Sstevel@tonic-gate 	}
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate 	if ((pos_p = strtok(NULL, at_char)) == NULL) {
25817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: must supply a position\n");
25827c478bd9Sstevel@tonic-gate 		exit(1);
25837c478bd9Sstevel@tonic-gate 	}
25847c478bd9Sstevel@tonic-gate 	mod.pos = atoi(pos_p);
25857c478bd9Sstevel@tonic-gate 
2586fc80c0dfSnordmark 	if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
25877c478bd9Sstevel@tonic-gate 	    &orig_arpid) < 0) {
25887c478bd9Sstevel@tonic-gate 		free(arg_str);
25897c478bd9Sstevel@tonic-gate 		return (-1);
25907c478bd9Sstevel@tonic-gate 	}
25917c478bd9Sstevel@tonic-gate 	switch (op) {
25927c478bd9Sstevel@tonic-gate 	case MODINSERT_OP:
25937c478bd9Sstevel@tonic-gate 		if (debug > 0) {
25947c478bd9Sstevel@tonic-gate 			(void) printf("Inserting module %s at %d\n",
25957c478bd9Sstevel@tonic-gate 			    mod.mod_name, mod.pos);
25967c478bd9Sstevel@tonic-gate 		}
25977c478bd9Sstevel@tonic-gate 		if (ioctl(ipfd_lowstr, _I_INSERT, (caddr_t)&mod) < 0) {
25987c478bd9Sstevel@tonic-gate 			Perror2("fail to insert module", mod.mod_name);
25997c478bd9Sstevel@tonic-gate 		}
26007c478bd9Sstevel@tonic-gate 		break;
26017c478bd9Sstevel@tonic-gate 	case MODREMOVE_OP:
26027c478bd9Sstevel@tonic-gate 		if (debug > 0) {
26037c478bd9Sstevel@tonic-gate 			(void) printf("Removing module %s at %d\n",
26047c478bd9Sstevel@tonic-gate 			    mod.mod_name, mod.pos);
26057c478bd9Sstevel@tonic-gate 		}
26067c478bd9Sstevel@tonic-gate 		if (ioctl(ipfd_lowstr, _I_REMOVE, (caddr_t)&mod) < 0) {
26077c478bd9Sstevel@tonic-gate 			Perror2("fail to remove module", mod.mod_name);
26087c478bd9Sstevel@tonic-gate 		}
26097c478bd9Sstevel@tonic-gate 		break;
26107c478bd9Sstevel@tonic-gate 	default:
26117c478bd9Sstevel@tonic-gate 		/* Should never get to here. */
26127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Unknown operation\n");
26137c478bd9Sstevel@tonic-gate 		break;
26147c478bd9Sstevel@tonic-gate 	}
26157c478bd9Sstevel@tonic-gate 	free(arg_str);
2616fc80c0dfSnordmark 	return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2617fc80c0dfSnordmark 	    orig_arpid));
26187c478bd9Sstevel@tonic-gate }
26197c478bd9Sstevel@tonic-gate 
26202b24ab6bSSebastien Roy static int
26212b24ab6bSSebastien Roy modify_tun(iptun_params_t *params)
26222b24ab6bSSebastien Roy {
26232b24ab6bSSebastien Roy 	dladm_status_t status;
26242b24ab6bSSebastien Roy 
26252b24ab6bSSebastien Roy 	if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN,
26262b24ab6bSSebastien Roy 	    &params->iptun_param_linkid)) == DLADM_STATUS_OK)
26272b24ab6bSSebastien Roy 		status = dladm_iptun_modify(dlh, params, DLADM_OPT_ACTIVE);
26282b24ab6bSSebastien Roy 	if (status != DLADM_STATUS_OK)
26292b24ab6bSSebastien Roy 		dladmerr_exit(status, name);
26302b24ab6bSSebastien Roy 	return (0);
26312b24ab6bSSebastien Roy }
26322b24ab6bSSebastien Roy 
26337c478bd9Sstevel@tonic-gate /*
26347c478bd9Sstevel@tonic-gate  * Set tunnel source address
26357c478bd9Sstevel@tonic-gate  */
26367c478bd9Sstevel@tonic-gate /* ARGSUSED */
26377c478bd9Sstevel@tonic-gate static int
26387c478bd9Sstevel@tonic-gate setiftsrc(char *addr, int64_t param)
26397c478bd9Sstevel@tonic-gate {
26402b24ab6bSSebastien Roy 	iptun_params_t params;
26412b24ab6bSSebastien Roy 
26422b24ab6bSSebastien Roy 	params.iptun_param_flags = IPTUN_PARAM_LADDR;
26432b24ab6bSSebastien Roy 	(void) strlcpy(params.iptun_param_laddr, addr,
26442b24ab6bSSebastien Roy 	    sizeof (params.iptun_param_laddr));
26452b24ab6bSSebastien Roy 	return (modify_tun(&params));
26467c478bd9Sstevel@tonic-gate }
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate /*
26497c478bd9Sstevel@tonic-gate  * Set tunnel destination address
26507c478bd9Sstevel@tonic-gate  */
26517c478bd9Sstevel@tonic-gate /* ARGSUSED */
26527c478bd9Sstevel@tonic-gate static int
26537c478bd9Sstevel@tonic-gate setiftdst(char *addr, int64_t param)
26547c478bd9Sstevel@tonic-gate {
26552b24ab6bSSebastien Roy 	iptun_params_t params;
26562b24ab6bSSebastien Roy 
26572b24ab6bSSebastien Roy 	params.iptun_param_flags = IPTUN_PARAM_RADDR;
26582b24ab6bSSebastien Roy 	(void) strlcpy(params.iptun_param_raddr, addr,
26592b24ab6bSSebastien Roy 	    sizeof (params.iptun_param_raddr));
26602b24ab6bSSebastien Roy 	return (modify_tun(&params));
26617c478bd9Sstevel@tonic-gate }
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate static int
26642b24ab6bSSebastien Roy set_tun_prop(const char *propname, char *value)
26657c478bd9Sstevel@tonic-gate {
26662b24ab6bSSebastien Roy 	dladm_status_t	status;
26672b24ab6bSSebastien Roy 	datalink_id_t	linkid;
26687c478bd9Sstevel@tonic-gate 
26692b24ab6bSSebastien Roy 	status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid);
26702b24ab6bSSebastien Roy 	if (status == DLADM_STATUS_OK) {
26712b24ab6bSSebastien Roy 		status = dladm_set_linkprop(dlh, linkid, propname, &value, 1,
26722b24ab6bSSebastien Roy 		    DLADM_OPT_ACTIVE);
26737c478bd9Sstevel@tonic-gate 	}
26742b24ab6bSSebastien Roy 	if (status != DLADM_STATUS_OK)
26752b24ab6bSSebastien Roy 		dladmerr_exit(status, name);
26767c478bd9Sstevel@tonic-gate 	return (0);
26777c478bd9Sstevel@tonic-gate }
26787c478bd9Sstevel@tonic-gate 
26797c478bd9Sstevel@tonic-gate /* Set tunnel encapsulation limit. */
26807c478bd9Sstevel@tonic-gate /* ARGSUSED */
26817c478bd9Sstevel@tonic-gate static int
26827c478bd9Sstevel@tonic-gate set_tun_encap_limit(char *arg, int64_t param)
26837c478bd9Sstevel@tonic-gate {
26842b24ab6bSSebastien Roy 	return (set_tun_prop("encaplimit", arg));
26857c478bd9Sstevel@tonic-gate }
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate /* Disable encapsulation limit. */
26887c478bd9Sstevel@tonic-gate /* ARGSUSED */
26897c478bd9Sstevel@tonic-gate static int
26907c478bd9Sstevel@tonic-gate clr_tun_encap_limit(char *arg, int64_t param)
26917c478bd9Sstevel@tonic-gate {
26922b24ab6bSSebastien Roy 	return (set_tun_encap_limit("-1", 0));
26937c478bd9Sstevel@tonic-gate }
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate /* Set tunnel hop limit. */
26967c478bd9Sstevel@tonic-gate /* ARGSUSED */
26977c478bd9Sstevel@tonic-gate static int
26987c478bd9Sstevel@tonic-gate set_tun_hop_limit(char *arg, int64_t param)
26997c478bd9Sstevel@tonic-gate {
27002b24ab6bSSebastien Roy 	return (set_tun_prop("hoplimit", arg));
27017c478bd9Sstevel@tonic-gate }
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate /* Set zone ID */
27047c478bd9Sstevel@tonic-gate static int
27057c478bd9Sstevel@tonic-gate setzone(char *arg, int64_t param)
27067c478bd9Sstevel@tonic-gate {
27077c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = GLOBAL_ZONEID;
27087c478bd9Sstevel@tonic-gate 
27097c478bd9Sstevel@tonic-gate 	if (param == NEXTARG) {
27107c478bd9Sstevel@tonic-gate 		/* zone must be active */
27117c478bd9Sstevel@tonic-gate 		if ((zoneid = getzoneidbyname(arg)) == -1) {
27127c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
27137c478bd9Sstevel@tonic-gate 			    "ifconfig: unknown zone '%s'\n", arg);
27147c478bd9Sstevel@tonic-gate 			exit(1);
27157c478bd9Sstevel@tonic-gate 		}
27167c478bd9Sstevel@tonic-gate 	}
27177c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
27187c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zoneid;
27197c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
27207c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFZONE");
27217c478bd9Sstevel@tonic-gate 	return (0);
27227c478bd9Sstevel@tonic-gate }
27237c478bd9Sstevel@tonic-gate 
272445916cd2Sjpk /* Put interface into all zones */
272545916cd2Sjpk /* ARGSUSED */
272645916cd2Sjpk static int
272745916cd2Sjpk setallzones(char *arg, int64_t param)
272845916cd2Sjpk {
272945916cd2Sjpk 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
273045916cd2Sjpk 	lifr.lifr_zoneid = ALL_ZONES;
273145916cd2Sjpk 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
273245916cd2Sjpk 		Perror0_exit("SIOCSLIFZONE");
273345916cd2Sjpk 	return (0);
273445916cd2Sjpk }
273545916cd2Sjpk 
27367c478bd9Sstevel@tonic-gate /* Set source address to use */
27377c478bd9Sstevel@tonic-gate /* ARGSUSED */
27387c478bd9Sstevel@tonic-gate static int
27397c478bd9Sstevel@tonic-gate setifsrc(char *arg, int64_t param)
27407c478bd9Sstevel@tonic-gate {
27417c478bd9Sstevel@tonic-gate 	uint_t ifindex = 0;
27427c478bd9Sstevel@tonic-gate 	int rval;
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
27457c478bd9Sstevel@tonic-gate 
27467c478bd9Sstevel@tonic-gate 	/*
27477c478bd9Sstevel@tonic-gate 	 * Argument can be either an interface name or "none". The latter means
27487c478bd9Sstevel@tonic-gate 	 * that any previous selection is cleared.
27497c478bd9Sstevel@tonic-gate 	 */
27507c478bd9Sstevel@tonic-gate 
2751*6e91bba0SGirish Moodalbail 	if (strchr(arg, ':') != NULL) {
27527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2753*6e91bba0SGirish Moodalbail 		    "ifconfig: Cannot specify logical interface for usesrc \n");
27547c478bd9Sstevel@tonic-gate 		exit(1);
27557c478bd9Sstevel@tonic-gate 	}
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate 	rval = strcmp(arg, NONE_STR);
27587c478bd9Sstevel@tonic-gate 	if (rval != 0) {
27597c478bd9Sstevel@tonic-gate 		if ((ifindex = if_nametoindex(arg)) == 0) {
27607c478bd9Sstevel@tonic-gate 			(void) strncpy(lifr.lifr_name, arg, LIFNAMSIZ);
27617c478bd9Sstevel@tonic-gate 			Perror0_exit("Could not get interface index");
27627c478bd9Sstevel@tonic-gate 		}
27637c478bd9Sstevel@tonic-gate 		lifr.lifr_index = ifindex;
27647c478bd9Sstevel@tonic-gate 	} else {
27657c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) != 0)
27667c478bd9Sstevel@tonic-gate 			Perror0_exit("Not a valid usesrc consumer");
27677c478bd9Sstevel@tonic-gate 		lifr.lifr_index = 0;
27687c478bd9Sstevel@tonic-gate 	}
27697c478bd9Sstevel@tonic-gate 
27707c478bd9Sstevel@tonic-gate 	if (debug)
27717c478bd9Sstevel@tonic-gate 		(void) printf("setifsrc: lifr_name %s, lifr_index %d\n",
27727c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, lifr.lifr_index);
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFUSESRC, (caddr_t)&lifr) == -1) {
27757c478bd9Sstevel@tonic-gate 		if (rval == 0)
27767c478bd9Sstevel@tonic-gate 			Perror0_exit("Cannot reset usesrc group");
27777c478bd9Sstevel@tonic-gate 		else
27787c478bd9Sstevel@tonic-gate 			Perror0_exit("Could not set source interface");
27797c478bd9Sstevel@tonic-gate 	}
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate 	return (0);
27827c478bd9Sstevel@tonic-gate }
27837c478bd9Sstevel@tonic-gate 
27847c478bd9Sstevel@tonic-gate /*
27857c478bd9Sstevel@tonic-gate  * Print the interface status line associated with `ifname'
27867c478bd9Sstevel@tonic-gate  */
27877c478bd9Sstevel@tonic-gate static void
27887c478bd9Sstevel@tonic-gate ifstatus(const char *ifname)
27897c478bd9Sstevel@tonic-gate {
27907c478bd9Sstevel@tonic-gate 	uint64_t flags;
27917c478bd9Sstevel@tonic-gate 	char if_usesrc_name[LIFNAMSIZ];
27927c478bd9Sstevel@tonic-gate 	char *newbuf;
27937c478bd9Sstevel@tonic-gate 	int n, numifs, rval = 0;
27947c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp;
27957c478bd9Sstevel@tonic-gate 	struct lifsrcof lifs;
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
27987c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
27997c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
28007c478bd9Sstevel@tonic-gate 	}
28017c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 	/*
28047c478bd9Sstevel@tonic-gate 	 * In V4 compatibility mode, we don't print the IFF_IPV4 flag or
28057c478bd9Sstevel@tonic-gate 	 * interfaces with IFF_IPV6 set.
28067c478bd9Sstevel@tonic-gate 	 */
28077c478bd9Sstevel@tonic-gate 	if (v4compat) {
28087c478bd9Sstevel@tonic-gate 		flags &= ~IFF_IPV4;
28097c478bd9Sstevel@tonic-gate 		if (flags & IFF_IPV6)
28107c478bd9Sstevel@tonic-gate 			return;
28117c478bd9Sstevel@tonic-gate 	}
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 	(void) printf("%s: ", ifname);
28147c478bd9Sstevel@tonic-gate 	print_flags(flags);
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
28177c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
28187c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFMETRIC");
28197c478bd9Sstevel@tonic-gate 	} else {
28207c478bd9Sstevel@tonic-gate 		if (lifr.lifr_metric)
28217c478bd9Sstevel@tonic-gate 			(void) printf(" metric %d", lifr.lifr_metric);
28227c478bd9Sstevel@tonic-gate 	}
28237c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
2824c08e5e1aSdr 		(void) printf(" mtu %u", lifr.lifr_mtu);
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 	/* don't print index or zone when in compatibility mode */
28277c478bd9Sstevel@tonic-gate 	if (!v4compat) {
28287c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
28297c478bd9Sstevel@tonic-gate 			(void) printf(" index %d", lifr.lifr_index);
2830f4b3ec61Sdh 		/*
2831f4b3ec61Sdh 		 * Stack instances use GLOBAL_ZONEID for IP data structures
2832f4b3ec61Sdh 		 * even in the non-global zone.
2833f4b3ec61Sdh 		 */
28347c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifr) >= 0 &&
2835f4b3ec61Sdh 		    lifr.lifr_zoneid != getzoneid() &&
2836f4b3ec61Sdh 		    lifr.lifr_zoneid != GLOBAL_ZONEID) {
28377c478bd9Sstevel@tonic-gate 			char zone_name[ZONENAME_MAX];
28387c478bd9Sstevel@tonic-gate 
283945916cd2Sjpk 			if (lifr.lifr_zoneid == ALL_ZONES) {
284045916cd2Sjpk 				(void) printf("\n\tall-zones");
284145916cd2Sjpk 			} else if (getzonenamebyid(lifr.lifr_zoneid, zone_name,
28427c478bd9Sstevel@tonic-gate 			    sizeof (zone_name)) < 0) {
28437c478bd9Sstevel@tonic-gate 				(void) printf("\n\tzone %d", lifr.lifr_zoneid);
28447c478bd9Sstevel@tonic-gate 			} else {
28457c478bd9Sstevel@tonic-gate 				(void) printf("\n\tzone %s", zone_name);
28467c478bd9Sstevel@tonic-gate 			}
28477c478bd9Sstevel@tonic-gate 		}
28487c478bd9Sstevel@tonic-gate 	}
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) {
28517c478bd9Sstevel@tonic-gate 		lifs.lifs_ifindex = lifr.lifr_index;
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate 		/*
28547c478bd9Sstevel@tonic-gate 		 * Find the number of interfaces that use this interfaces'
28557c478bd9Sstevel@tonic-gate 		 * address as a source address
28567c478bd9Sstevel@tonic-gate 		 */
28577c478bd9Sstevel@tonic-gate 		lifs.lifs_buf = NULL;
28587c478bd9Sstevel@tonic-gate 		lifs.lifs_maxlen = 0;
28597c478bd9Sstevel@tonic-gate 		for (;;) {
28607c478bd9Sstevel@tonic-gate 			/* The first pass will give the bufsize we need */
28617c478bd9Sstevel@tonic-gate 			rval = ioctl(s, SIOCGLIFSRCOF, (char *)&lifs);
28627c478bd9Sstevel@tonic-gate 			if (rval < 0) {
28637c478bd9Sstevel@tonic-gate 				if (lifs.lifs_buf != NULL) {
28647c478bd9Sstevel@tonic-gate 					free(lifs.lifs_buf);
28657c478bd9Sstevel@tonic-gate 					lifs.lifs_buf = NULL;
28667c478bd9Sstevel@tonic-gate 				}
28677c478bd9Sstevel@tonic-gate 				lifs.lifs_len = 0;
28687c478bd9Sstevel@tonic-gate 				break;
28697c478bd9Sstevel@tonic-gate 			}
28707c478bd9Sstevel@tonic-gate 			if (lifs.lifs_len <= lifs.lifs_maxlen)
28717c478bd9Sstevel@tonic-gate 				break;
28727c478bd9Sstevel@tonic-gate 			/* Use kernel's size + a small margin to avoid loops */
28737c478bd9Sstevel@tonic-gate 			lifs.lifs_maxlen = lifs.lifs_len +
28747c478bd9Sstevel@tonic-gate 			    5 * sizeof (struct lifreq);
28757c478bd9Sstevel@tonic-gate 			/* For the first pass, realloc acts like malloc */
28767c478bd9Sstevel@tonic-gate 			newbuf = realloc(lifs.lifs_buf, lifs.lifs_maxlen);
28777c478bd9Sstevel@tonic-gate 			if (newbuf == NULL) {
28787c478bd9Sstevel@tonic-gate 				if (lifs.lifs_buf != NULL) {
28797c478bd9Sstevel@tonic-gate 					free(lifs.lifs_buf);
28807c478bd9Sstevel@tonic-gate 					lifs.lifs_buf = NULL;
28817c478bd9Sstevel@tonic-gate 				}
28827c478bd9Sstevel@tonic-gate 				lifs.lifs_len = 0;
28837c478bd9Sstevel@tonic-gate 				break;
28847c478bd9Sstevel@tonic-gate 			}
28857c478bd9Sstevel@tonic-gate 			lifs.lifs_buf = newbuf;
28867c478bd9Sstevel@tonic-gate 		}
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 		numifs = lifs.lifs_len / sizeof (struct lifreq);
28907c478bd9Sstevel@tonic-gate 		if (numifs > 0) {
28917c478bd9Sstevel@tonic-gate 			lifrp = lifs.lifs_req;
28927c478bd9Sstevel@tonic-gate 			(void) printf("\n\tsrcof");
28937c478bd9Sstevel@tonic-gate 			for (n = numifs; n > 0; n--, lifrp++) {
28947c478bd9Sstevel@tonic-gate 				(void) printf(" %s", lifrp->lifr_name);
28957c478bd9Sstevel@tonic-gate 			}
28967c478bd9Sstevel@tonic-gate 		}
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 		if (lifs.lifs_buf != NULL)
28997c478bd9Sstevel@tonic-gate 			free(lifs.lifs_buf);
29007c478bd9Sstevel@tonic-gate 	}
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 	/* Find the interface whose source address this interface uses */
29037c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
29047c478bd9Sstevel@tonic-gate 		if (lifr.lifr_index != 0) {
29057c478bd9Sstevel@tonic-gate 			if (if_indextoname(lifr.lifr_index,
29067c478bd9Sstevel@tonic-gate 			    if_usesrc_name) == NULL) {
29077c478bd9Sstevel@tonic-gate 				(void) printf("\n\tusesrc ifIndex %d",
29087c478bd9Sstevel@tonic-gate 				    lifr.lifr_index);
29097c478bd9Sstevel@tonic-gate 			} else {
29107c478bd9Sstevel@tonic-gate 				(void) printf("\n\tusesrc %s", if_usesrc_name);
29117c478bd9Sstevel@tonic-gate 			}
29127c478bd9Sstevel@tonic-gate 		}
29137c478bd9Sstevel@tonic-gate 	}
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
29167c478bd9Sstevel@tonic-gate }
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate /*
29197c478bd9Sstevel@tonic-gate  * Print the status of the interface.  If an address family was
29207c478bd9Sstevel@tonic-gate  * specified, show it and it only; otherwise, show them all.
29217c478bd9Sstevel@tonic-gate  */
29227c478bd9Sstevel@tonic-gate static void
29237c478bd9Sstevel@tonic-gate status(void)
29247c478bd9Sstevel@tonic-gate {
29252b24ab6bSSebastien Roy 	struct afswtch	*p = afp;
29262b24ab6bSSebastien Roy 	uint64_t	flags;
29272b24ab6bSSebastien Roy 	datalink_id_t	linkid;
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
29307c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
29317c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
29327c478bd9Sstevel@tonic-gate 	}
29337c478bd9Sstevel@tonic-gate 
29347c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
29357c478bd9Sstevel@tonic-gate 
29367c478bd9Sstevel@tonic-gate 	/*
29377c478bd9Sstevel@tonic-gate 	 * Only print the interface status if the address family matches
29387c478bd9Sstevel@tonic-gate 	 * the interface family flag.
29397c478bd9Sstevel@tonic-gate 	 */
29407c478bd9Sstevel@tonic-gate 	if (p != NULL) {
29417c478bd9Sstevel@tonic-gate 		if (((p->af_af == AF_INET6) && (flags & IFF_IPV4)) ||
29427c478bd9Sstevel@tonic-gate 		    ((p->af_af == AF_INET) && (flags & IFF_IPV6)))
29437c478bd9Sstevel@tonic-gate 			return;
29447c478bd9Sstevel@tonic-gate 	}
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	/*
29477c478bd9Sstevel@tonic-gate 	 * In V4 compatibility mode, don't print IFF_IPV6 interfaces.
29487c478bd9Sstevel@tonic-gate 	 */
29497c478bd9Sstevel@tonic-gate 	if (v4compat && (flags & IFF_IPV6))
29507c478bd9Sstevel@tonic-gate 		return;
29517c478bd9Sstevel@tonic-gate 
29527c478bd9Sstevel@tonic-gate 	ifstatus(name);
29537c478bd9Sstevel@tonic-gate 
29542b24ab6bSSebastien Roy 	if (ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid) ==
29552b24ab6bSSebastien Roy 	    DLADM_STATUS_OK)
29562b24ab6bSSebastien Roy 		tun_status(linkid);
29572b24ab6bSSebastien Roy 
29587c478bd9Sstevel@tonic-gate 	if (p != NULL) {
29597c478bd9Sstevel@tonic-gate 		(*p->af_status)(1, flags);
29607c478bd9Sstevel@tonic-gate 	} else {
29617c478bd9Sstevel@tonic-gate 		for (p = afs; p->af_name; p++) {
29627c478bd9Sstevel@tonic-gate 			/* set global af for use in p->af_status */
29637c478bd9Sstevel@tonic-gate 			af = p->af_af;
29647c478bd9Sstevel@tonic-gate 			(*p->af_status)(0, flags);
29657c478bd9Sstevel@tonic-gate 		}
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate 		/*
29687c478bd9Sstevel@tonic-gate 		 * Historically, 'ether' has been an address family,
29697c478bd9Sstevel@tonic-gate 		 * so print it here.
29707c478bd9Sstevel@tonic-gate 		 */
29717c478bd9Sstevel@tonic-gate 		print_ifether(name);
29727c478bd9Sstevel@tonic-gate 	}
29737c478bd9Sstevel@tonic-gate }
29747c478bd9Sstevel@tonic-gate 
29757c478bd9Sstevel@tonic-gate /*
29767c478bd9Sstevel@tonic-gate  * Print the status of the interface in a format that can be used to
29777c478bd9Sstevel@tonic-gate  * reconfigure the interface later. Code stolen from status() above.
29787c478bd9Sstevel@tonic-gate  */
29797c478bd9Sstevel@tonic-gate /* ARGSUSED */
29807c478bd9Sstevel@tonic-gate static int
29817c478bd9Sstevel@tonic-gate configinfo(char *null, int64_t param)
29827c478bd9Sstevel@tonic-gate {
2983e11c3f44Smeem 	char *cp;
29847c478bd9Sstevel@tonic-gate 	struct afswtch *p = afp;
29857c478bd9Sstevel@tonic-gate 	uint64_t flags;
2986e11c3f44Smeem 	char lifname[LIFNAMSIZ];
29877c478bd9Sstevel@tonic-gate 	char if_usesrc_name[LIFNAMSIZ];
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2990e11c3f44Smeem 
29917c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
29927c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
29937c478bd9Sstevel@tonic-gate 	}
29947c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	if (debug) {
29977c478bd9Sstevel@tonic-gate 		(void) printf("configinfo: name %s flags  0x%llx af_af %d\n",
29987c478bd9Sstevel@tonic-gate 		    name, flags, p != NULL ? p->af_af : -1);
29997c478bd9Sstevel@tonic-gate 	}
30007c478bd9Sstevel@tonic-gate 
3001e11c3f44Smeem 	/*
3002e11c3f44Smeem 	 * Build the interface name to print (we can't directly use `name'
3003e11c3f44Smeem 	 * because one cannot "plumb" ":0" interfaces).
3004e11c3f44Smeem 	 */
3005e11c3f44Smeem 	(void) strlcpy(lifname, name, LIFNAMSIZ);
3006e11c3f44Smeem 	if ((cp = strchr(lifname, ':')) != NULL && atoi(cp + 1) == 0)
3007e11c3f44Smeem 		*cp = '\0';
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate 	/*
30107c478bd9Sstevel@tonic-gate 	 * if the interface is IPv4
30117c478bd9Sstevel@tonic-gate 	 *	if we have a IPv6 address family restriction return
30127c478bd9Sstevel@tonic-gate 	 *		so it won't print
30137c478bd9Sstevel@tonic-gate 	 *	if we are in IPv4 compatibility mode, clear out IFF_IPV4
30147c478bd9Sstevel@tonic-gate 	 *		so we don't print it.
30157c478bd9Sstevel@tonic-gate 	 */
30167c478bd9Sstevel@tonic-gate 	if (flags & IFF_IPV4) {
30177c478bd9Sstevel@tonic-gate 		if (p && p->af_af == AF_INET6)
30187c478bd9Sstevel@tonic-gate 			return (-1);
30197c478bd9Sstevel@tonic-gate 		if (v4compat)
30207c478bd9Sstevel@tonic-gate 			flags &= ~IFF_IPV4;
30217c478bd9Sstevel@tonic-gate 
3022e11c3f44Smeem 		(void) printf("%s inet plumb", lifname);
30237c478bd9Sstevel@tonic-gate 	} else if (flags & IFF_IPV6) {
30247c478bd9Sstevel@tonic-gate 		/*
30257c478bd9Sstevel@tonic-gate 		 * else if the interface is IPv6
30267c478bd9Sstevel@tonic-gate 		 *	if we have a IPv4 address family restriction return
30277c478bd9Sstevel@tonic-gate 		 *	or we are in IPv4 compatibiltiy mode, return.
30287c478bd9Sstevel@tonic-gate 		 */
30297c478bd9Sstevel@tonic-gate 		if (p && p->af_af == AF_INET)
30307c478bd9Sstevel@tonic-gate 			return (-1);
30317c478bd9Sstevel@tonic-gate 		if (v4compat)
30327c478bd9Sstevel@tonic-gate 			return (-1);
30337c478bd9Sstevel@tonic-gate 
3034e11c3f44Smeem 		(void) printf("%s inet6 plumb", lifname);
30357c478bd9Sstevel@tonic-gate 	}
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
30387c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
30397c478bd9Sstevel@tonic-gate 		Perror0_exit("configinfo: SIOCGLIFMETRIC");
30407c478bd9Sstevel@tonic-gate 	} else {
30417c478bd9Sstevel@tonic-gate 		if (lifr.lifr_metric)
30427c478bd9Sstevel@tonic-gate 			(void) printf(" metric %d ", lifr.lifr_metric);
30437c478bd9Sstevel@tonic-gate 	}
30447c478bd9Sstevel@tonic-gate 	if (((flags & (IFF_VIRTUAL|IFF_LOOPBACK)) != IFF_VIRTUAL) &&
3045fc80c0dfSnordmark 	    ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
30467c478bd9Sstevel@tonic-gate 		(void) printf(" mtu %d", lifr.lifr_metric);
30477c478bd9Sstevel@tonic-gate 
3048e11c3f44Smeem 	/* Index only applies to the zeroth interface */
3049e11c3f44Smeem 	if (lifnum(name) == 0) {
30507c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
30517c478bd9Sstevel@tonic-gate 			(void) printf(" index %d", lifr.lifr_index);
30527c478bd9Sstevel@tonic-gate 	}
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
30557c478bd9Sstevel@tonic-gate 		if (lifr.lifr_index != 0) {
30567c478bd9Sstevel@tonic-gate 			if (if_indextoname(lifr.lifr_index,
30577c478bd9Sstevel@tonic-gate 			    if_usesrc_name) != NULL) {
30587c478bd9Sstevel@tonic-gate 				(void) printf(" usesrc %s", if_usesrc_name);
30597c478bd9Sstevel@tonic-gate 			}
30607c478bd9Sstevel@tonic-gate 		}
30617c478bd9Sstevel@tonic-gate 	}
30627c478bd9Sstevel@tonic-gate 
30637c478bd9Sstevel@tonic-gate 	if (p != NULL) {
30647c478bd9Sstevel@tonic-gate 		(*p->af_configinfo)(1, flags);
30657c478bd9Sstevel@tonic-gate 	} else {
30667c478bd9Sstevel@tonic-gate 		for (p = afs; p->af_name; p++) {
30677c478bd9Sstevel@tonic-gate 			(void) close(s);
30687c478bd9Sstevel@tonic-gate 			s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0);
30697c478bd9Sstevel@tonic-gate 			/* set global af for use in p->af_configinfo */
30707c478bd9Sstevel@tonic-gate 			af = p->af_af;
30717c478bd9Sstevel@tonic-gate 			if (s == -1) {
30727c478bd9Sstevel@tonic-gate 				Perror0_exit("socket");
30737c478bd9Sstevel@tonic-gate 			}
30747c478bd9Sstevel@tonic-gate 			(*p->af_configinfo)(0, flags);
30757c478bd9Sstevel@tonic-gate 		}
30767c478bd9Sstevel@tonic-gate 	}
30777c478bd9Sstevel@tonic-gate 
30782b24ab6bSSebastien Roy 	(void) putchar('\n');
30797c478bd9Sstevel@tonic-gate 	return (0);
30807c478bd9Sstevel@tonic-gate }
30817c478bd9Sstevel@tonic-gate 
30827c478bd9Sstevel@tonic-gate static void
30832b24ab6bSSebastien Roy print_tsec(iptun_params_t *params)
30847c478bd9Sstevel@tonic-gate {
30857c478bd9Sstevel@tonic-gate 	ipsec_req_t *ipsr;
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate 	(void) printf("\ttunnel security settings  ");
30882b24ab6bSSebastien Roy 	if (!(params->iptun_param_flags & IPTUN_PARAM_SECINFO)) {
30892b24ab6bSSebastien Roy 		(void) printf("-->  use 'ipsecconf -ln -i %s'", name);
30908810c16bSdanmcd 	} else {
30912b24ab6bSSebastien Roy 		ipsr = &params->iptun_param_secinfo;
30928810c16bSdanmcd 		if (ipsr->ipsr_ah_req & IPSEC_PREF_REQUIRED) {
30938810c16bSdanmcd 			(void) printf("ah (%s)  ",
30948810c16bSdanmcd 			    rparsealg(ipsr->ipsr_auth_alg, IPSEC_PROTO_AH));
30958810c16bSdanmcd 		}
30968810c16bSdanmcd 		if (ipsr->ipsr_esp_req & IPSEC_PREF_REQUIRED) {
30978810c16bSdanmcd 			(void) printf("esp (%s",
30988810c16bSdanmcd 			    rparsealg(ipsr->ipsr_esp_alg, IPSEC_PROTO_ESP));
30998810c16bSdanmcd 			(void) printf("/%s)",
31008810c16bSdanmcd 			    rparsealg(ipsr->ipsr_esp_auth_alg, IPSEC_PROTO_AH));
31018810c16bSdanmcd 		}
31027c478bd9Sstevel@tonic-gate 	}
31037c478bd9Sstevel@tonic-gate 	(void) printf("\n");
31047c478bd9Sstevel@tonic-gate }
31057c478bd9Sstevel@tonic-gate 
31067c478bd9Sstevel@tonic-gate static void
31072b24ab6bSSebastien Roy tun_status(datalink_id_t linkid)
31087c478bd9Sstevel@tonic-gate {
31092b24ab6bSSebastien Roy 	iptun_params_t	params;
31102b24ab6bSSebastien Roy 	char		propval[DLADM_PROP_VAL_MAX];
31112b24ab6bSSebastien Roy 	char		*valptr[1];
31122b24ab6bSSebastien Roy 	uint_t		valcnt = 1;
31132b24ab6bSSebastien Roy 	boolean_t	tabbed = _B_FALSE;
31142b24ab6bSSebastien Roy 
31152b24ab6bSSebastien Roy 	params.iptun_param_linkid = linkid;
31162b24ab6bSSebastien Roy 
31172b24ab6bSSebastien Roy 	/* If dladm_iptun_getparams() fails, assume we are not a tunnel. */
31182b24ab6bSSebastien Roy 	assert(dlh_opened);
31192b24ab6bSSebastien Roy 	if (dladm_iptun_getparams(dlh, &params, DLADM_OPT_ACTIVE) !=
31202b24ab6bSSebastien Roy 	    DLADM_STATUS_OK)
31217c478bd9Sstevel@tonic-gate 		return;
31227c478bd9Sstevel@tonic-gate 
31232b24ab6bSSebastien Roy 	switch (params.iptun_param_type) {
31242b24ab6bSSebastien Roy 	case IPTUN_TYPE_IPV4:
31252b24ab6bSSebastien Roy 	case IPTUN_TYPE_6TO4:
31267c478bd9Sstevel@tonic-gate 		(void) printf("\tinet");
31277c478bd9Sstevel@tonic-gate 		break;
31282b24ab6bSSebastien Roy 	case IPTUN_TYPE_IPV6:
31297c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6");
31307c478bd9Sstevel@tonic-gate 		break;
31317c478bd9Sstevel@tonic-gate 	default:
31322b24ab6bSSebastien Roy 		dladmerr_exit(DLADM_STATUS_IPTUNTYPE, name);
31337c478bd9Sstevel@tonic-gate 		break;
31347c478bd9Sstevel@tonic-gate 	}
31357c478bd9Sstevel@tonic-gate 
31367c478bd9Sstevel@tonic-gate 	/*
31372b24ab6bSSebastien Roy 	 * There is always a source address.  If it hasn't been explicitly
31382b24ab6bSSebastien Roy 	 * set, the API will pass back a buffer containing the unspecified
31392b24ab6bSSebastien Roy 	 * address.
31407c478bd9Sstevel@tonic-gate 	 */
31412b24ab6bSSebastien Roy 	(void) printf(" tunnel src %s ", params.iptun_param_laddr);
31422b24ab6bSSebastien Roy 
31432b24ab6bSSebastien Roy 	if (params.iptun_param_flags & IPTUN_PARAM_RADDR)
31442b24ab6bSSebastien Roy 		(void) printf("tunnel dst %s\n", params.iptun_param_raddr);
31452b24ab6bSSebastien Roy 	else
31462b24ab6bSSebastien Roy 		(void) putchar('\n');
31472b24ab6bSSebastien Roy 
31482b24ab6bSSebastien Roy 	if (params.iptun_param_flags & IPTUN_PARAM_IPSECPOL)
31492b24ab6bSSebastien Roy 		print_tsec(&params);
31507c478bd9Sstevel@tonic-gate 
31512b24ab6bSSebastien Roy 	valptr[0] = propval;
31522b24ab6bSSebastien Roy 	if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT, "hoplimit",
31532b24ab6bSSebastien Roy 	    (char **)valptr, &valcnt) == DLADM_STATUS_OK) {
31542b24ab6bSSebastien Roy 		(void) printf("\ttunnel hop limit %s ", propval);
31557c478bd9Sstevel@tonic-gate 		tabbed = _B_TRUE;
31567c478bd9Sstevel@tonic-gate 	}
31577c478bd9Sstevel@tonic-gate 
31582b24ab6bSSebastien Roy 	if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT,
31592b24ab6bSSebastien Roy 	    "encaplimit", (char **)valptr, &valcnt) == DLADM_STATUS_OK) {
31602b24ab6bSSebastien Roy 		uint32_t elim;
31612b24ab6bSSebastien Roy 
31627c478bd9Sstevel@tonic-gate 		if (!tabbed) {
31632b24ab6bSSebastien Roy 			(void) putchar('\t');
31647c478bd9Sstevel@tonic-gate 			tabbed = _B_TRUE;
31657c478bd9Sstevel@tonic-gate 		}
31662b24ab6bSSebastien Roy 		elim = strtol(propval, NULL, 10);
31672b24ab6bSSebastien Roy 		if (elim > 0)
31682b24ab6bSSebastien Roy 			(void) printf("tunnel encapsulation limit %s", propval);
31692b24ab6bSSebastien Roy 		else
31707c478bd9Sstevel@tonic-gate 			(void) printf("tunnel encapsulation limit disabled");
31717c478bd9Sstevel@tonic-gate 	}
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 	if (tabbed)
31742b24ab6bSSebastien Roy 		(void) putchar('\n');
31757c478bd9Sstevel@tonic-gate }
31767c478bd9Sstevel@tonic-gate 
31777c478bd9Sstevel@tonic-gate static void
31787c478bd9Sstevel@tonic-gate in_status(int force, uint64_t flags)
31797c478bd9Sstevel@tonic-gate {
31802b24ab6bSSebastien Roy 	struct sockaddr_in	*sin, *laddr;
31812b24ab6bSSebastien Roy 	struct sockaddr_in	netmask = { AF_INET };
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate 	if (debug)
31847c478bd9Sstevel@tonic-gate 		(void) printf("in_status(%s) flags 0x%llx\n", name, flags);
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate 	/* only print status for IPv4 interfaces */
31877c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV4))
31887c478bd9Sstevel@tonic-gate 		return;
31897c478bd9Sstevel@tonic-gate 
31907c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
31917c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
31927c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
31937c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
31947c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
31957c478bd9Sstevel@tonic-gate 				if (!force)
31967c478bd9Sstevel@tonic-gate 					return;
31977c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
31987c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
31997c478bd9Sstevel@tonic-gate 			} else
32007c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status: SIOCGLIFADDR");
32017c478bd9Sstevel@tonic-gate 		}
32027c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
32037c478bd9Sstevel@tonic-gate 		(void) printf("\tinet %s ", inet_ntoa(sin->sin_addr));
32047c478bd9Sstevel@tonic-gate 		laddr = sin;
32057c478bd9Sstevel@tonic-gate 	} else {
32067c478bd9Sstevel@tonic-gate 		(void) printf("\tinet ");
32077c478bd9Sstevel@tonic-gate 	}
32087c478bd9Sstevel@tonic-gate 
32097c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32107c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
32117c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
32127c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
32137c478bd9Sstevel@tonic-gate 			if (!force)
32147c478bd9Sstevel@tonic-gate 				return;
32157c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
32167c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
32177c478bd9Sstevel@tonic-gate 		} else {
32187c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status: SIOCGLIFSUBNET");
32197c478bd9Sstevel@tonic-gate 		}
32207c478bd9Sstevel@tonic-gate 	}
32217c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
32227c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
32237c478bd9Sstevel@tonic-gate 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
32247c478bd9Sstevel@tonic-gate 		(void) printf("subnet %s/%d ", inet_ntoa(sin->sin_addr),
32257c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
32267c478bd9Sstevel@tonic-gate 	}
32277c478bd9Sstevel@tonic-gate 	if (sin->sin_family != AF_INET) {
32287c478bd9Sstevel@tonic-gate 		(void) printf("Wrong family: %d\n", sin->sin_family);
32297c478bd9Sstevel@tonic-gate 	}
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32327c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
32337c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL)
32347c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status: SIOCGLIFNETMASK");
32357c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
32367c478bd9Sstevel@tonic-gate 	} else
32377c478bd9Sstevel@tonic-gate 		netmask.sin_addr =
32387c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
32397c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
32407c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32417c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
32427c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
32437c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
32447c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
32457c478bd9Sstevel@tonic-gate 			else
3246fc80c0dfSnordmark 				Perror0_exit("in_status: SIOCGLIFDSTADDR");
32477c478bd9Sstevel@tonic-gate 		}
32487c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
32497c478bd9Sstevel@tonic-gate 		(void) printf("--> %s ", inet_ntoa(sin->sin_addr));
32507c478bd9Sstevel@tonic-gate 	}
32517c478bd9Sstevel@tonic-gate 	(void) printf("netmask %x ", ntohl(netmask.sin_addr.s_addr));
32527c478bd9Sstevel@tonic-gate 	if (flags & IFF_BROADCAST) {
32537c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32547c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
32557c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
32567c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
32577c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
32587c478bd9Sstevel@tonic-gate 			else
3259fc80c0dfSnordmark 				Perror0_exit("in_status: SIOCGLIFBRDADDR");
32607c478bd9Sstevel@tonic-gate 		}
32617c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
32627c478bd9Sstevel@tonic-gate 		if (sin->sin_addr.s_addr != 0) {
32637c478bd9Sstevel@tonic-gate 			(void) printf("broadcast %s",
32647c478bd9Sstevel@tonic-gate 			    inet_ntoa(sin->sin_addr));
32657c478bd9Sstevel@tonic-gate 		}
32667c478bd9Sstevel@tonic-gate 	}
3267e11c3f44Smeem 	/* If there is a groupname, print it for only the physical interface */
32687c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') == NULL) {
3269e11c3f44Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3270e11c3f44Smeem 		    lifr.lifr_groupname[0] != '\0') {
3271e11c3f44Smeem 			(void) printf("\n\tgroupname %s", lifr.lifr_groupname);
32727c478bd9Sstevel@tonic-gate 		}
32737c478bd9Sstevel@tonic-gate 	}
32747c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
32757c478bd9Sstevel@tonic-gate }
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate static void
32787c478bd9Sstevel@tonic-gate in6_status(int force, uint64_t flags)
32797c478bd9Sstevel@tonic-gate {
32802b24ab6bSSebastien Roy 	char			abuf[INET6_ADDRSTRLEN];
32812b24ab6bSSebastien Roy 	struct sockaddr_in6	*sin6, *laddr6;
32827c478bd9Sstevel@tonic-gate 
32837c478bd9Sstevel@tonic-gate 	if (debug)
32847c478bd9Sstevel@tonic-gate 		(void) printf("in6_status(%s) flags 0x%llx\n", name, flags);
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV6))
32877c478bd9Sstevel@tonic-gate 		return;
32887c478bd9Sstevel@tonic-gate 
32897c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
32907c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32917c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
32927c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
32937c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
32947c478bd9Sstevel@tonic-gate 				if (!force)
32957c478bd9Sstevel@tonic-gate 					return;
32967c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
32977c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
32987c478bd9Sstevel@tonic-gate 			} else
32997c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status6: SIOCGLIFADDR");
33007c478bd9Sstevel@tonic-gate 		}
33017c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
33027c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6 %s/%d ",
33037c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33047c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
33057c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
33067c478bd9Sstevel@tonic-gate 		laddr6 = sin6;
33077c478bd9Sstevel@tonic-gate 	} else {
33087c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6 ");
33097c478bd9Sstevel@tonic-gate 	}
33107c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33117c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
33127c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
33137c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
33147c478bd9Sstevel@tonic-gate 			if (!force)
33157c478bd9Sstevel@tonic-gate 				return;
33167c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
33177c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
33187c478bd9Sstevel@tonic-gate 		} else
33197c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status6: SIOCGLIFSUBNET");
33207c478bd9Sstevel@tonic-gate 	}
33217c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
33227c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
33237c478bd9Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
33247c478bd9Sstevel@tonic-gate 		(void) printf("subnet %s/%d ",
33257c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33267c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
33277c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
33287c478bd9Sstevel@tonic-gate 	}
33297c478bd9Sstevel@tonic-gate 	if (sin6->sin6_family != AF_INET6) {
33307c478bd9Sstevel@tonic-gate 		(void) printf("Wrong family: %d\n", sin6->sin6_family);
33317c478bd9Sstevel@tonic-gate 	}
33327c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
33337c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33347c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
33357c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
33367c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33377c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33387c478bd9Sstevel@tonic-gate 			else
3339fc80c0dfSnordmark 				Perror0_exit("in_status6: SIOCGLIFDSTADDR");
33407c478bd9Sstevel@tonic-gate 		}
33417c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
33427c478bd9Sstevel@tonic-gate 		(void) printf("--> %s ",
33437c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33447c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)));
33457c478bd9Sstevel@tonic-gate 	}
33467c478bd9Sstevel@tonic-gate 	if (verbose) {
33477c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
33487c478bd9Sstevel@tonic-gate 		(void) putchar('\t');
33497c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33507c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
33517c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EINVAL)
33527c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33537c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33547c478bd9Sstevel@tonic-gate 			else
3355fc80c0dfSnordmark 				Perror0_exit("in_status6: SIOCGLIFTOKEN");
33567c478bd9Sstevel@tonic-gate 		} else {
33577c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
33587c478bd9Sstevel@tonic-gate 			(void) printf("token %s/%d ",
33597c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33607c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
33617c478bd9Sstevel@tonic-gate 			    lifr.lifr_addrlen);
33627c478bd9Sstevel@tonic-gate 		}
33637c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFLNKINFO, (caddr_t)&lifr) < 0) {
33647c478bd9Sstevel@tonic-gate 			if (errno != EINVAL) {
33657c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status6: SIOCGLIFLNKINFO");
33667c478bd9Sstevel@tonic-gate 			}
33677c478bd9Sstevel@tonic-gate 		} else {
33687c478bd9Sstevel@tonic-gate 			(void) printf("maxhops %u, reachtime %u ms, "
33697c478bd9Sstevel@tonic-gate 			    "reachretrans %u ms, maxmtu %u ",
33707c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_maxhops,
33717c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_reachtime,
33727c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_reachretrans,
33737c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_maxmtu);
33747c478bd9Sstevel@tonic-gate 		}
33757c478bd9Sstevel@tonic-gate 	}
3376f7d61273Smeem 	/* If there is a groupname, print it for only the physical interface */
33777c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') == NULL) {
3378f7d61273Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3379f7d61273Smeem 		    lifr.lifr_groupname[0] != '\0') {
3380f7d61273Smeem 			(void) printf("\n\tgroupname %s", lifr.lifr_groupname);
33817c478bd9Sstevel@tonic-gate 		}
33827c478bd9Sstevel@tonic-gate 	}
33837c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
33847c478bd9Sstevel@tonic-gate }
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate static void
33877c478bd9Sstevel@tonic-gate in_configinfo(int force, uint64_t flags)
33887c478bd9Sstevel@tonic-gate {
33897c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin, *laddr;
33907c478bd9Sstevel@tonic-gate 	struct	sockaddr_in netmask = { AF_INET };
33917c478bd9Sstevel@tonic-gate 
33927c478bd9Sstevel@tonic-gate 	if (debug)
33937c478bd9Sstevel@tonic-gate 		(void) printf("in_configinfo(%s) flags 0x%llx\n", name, flags);
33947c478bd9Sstevel@tonic-gate 
33957c478bd9Sstevel@tonic-gate 	/* only configinfo info for IPv4 interfaces */
33967c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV4))
33977c478bd9Sstevel@tonic-gate 		return;
33987c478bd9Sstevel@tonic-gate 
33997c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
34007c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34017c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
34027c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
34037c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
34047c478bd9Sstevel@tonic-gate 				if (!force)
34057c478bd9Sstevel@tonic-gate 					return;
34067c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34077c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34087c478bd9Sstevel@tonic-gate 			} else
34097c478bd9Sstevel@tonic-gate 				Perror0_exit("in_configinfo: SIOCGLIFADDR");
34107c478bd9Sstevel@tonic-gate 		}
34117c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3412e11c3f44Smeem 		(void) printf(" set %s ", inet_ntoa(sin->sin_addr));
34137c478bd9Sstevel@tonic-gate 		laddr = sin;
34147c478bd9Sstevel@tonic-gate 	}
34157c478bd9Sstevel@tonic-gate 
34167c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34177c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
34187c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
34197c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
34207c478bd9Sstevel@tonic-gate 			if (!force)
34217c478bd9Sstevel@tonic-gate 				return;
34227c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
34237c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
34247c478bd9Sstevel@tonic-gate 		} else {
34257c478bd9Sstevel@tonic-gate 			Perror0_exit("in_configinfo: SIOCGLIFSUBNET");
34267c478bd9Sstevel@tonic-gate 		}
34277c478bd9Sstevel@tonic-gate 	}
34287c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
34297c478bd9Sstevel@tonic-gate 
34307c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
34317c478bd9Sstevel@tonic-gate 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
34327c478bd9Sstevel@tonic-gate 		(void) printf(" subnet %s/%d ", inet_ntoa(sin->sin_addr),
34337c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
34347c478bd9Sstevel@tonic-gate 	}
34357c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34367c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
34377c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL)
34387c478bd9Sstevel@tonic-gate 			Perror0_exit("in_configinfo: SIOCGLIFNETMASK");
34397c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
34407c478bd9Sstevel@tonic-gate 	} else
34417c478bd9Sstevel@tonic-gate 		netmask.sin_addr =
34427c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
34437c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
34447c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34457c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
34467c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
34477c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34487c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34497c478bd9Sstevel@tonic-gate 			else
3450fc80c0dfSnordmark 				Perror0_exit("in_configinfo: SIOCGLIFDSTADDR");
34517c478bd9Sstevel@tonic-gate 		}
34527c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
34537c478bd9Sstevel@tonic-gate 		(void) printf(" destination %s ", inet_ntoa(sin->sin_addr));
34547c478bd9Sstevel@tonic-gate 	}
34557c478bd9Sstevel@tonic-gate 	(void) printf(" netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
34567c478bd9Sstevel@tonic-gate 	if (flags & IFF_BROADCAST) {
34577c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34587c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
34597c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
34607c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34617c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34627c478bd9Sstevel@tonic-gate 			else
3463fc80c0dfSnordmark 				Perror0_exit("in_configinfo: SIOCGLIFBRDADDR");
34647c478bd9Sstevel@tonic-gate 		}
34657c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
34667c478bd9Sstevel@tonic-gate 		if (sin->sin_addr.s_addr != 0) {
34677c478bd9Sstevel@tonic-gate 			(void) printf(" broadcast %s ",
34687c478bd9Sstevel@tonic-gate 			    inet_ntoa(sin->sin_addr));
34697c478bd9Sstevel@tonic-gate 		}
34707c478bd9Sstevel@tonic-gate 	}
34717c478bd9Sstevel@tonic-gate 
3472e11c3f44Smeem 	/* If there is a groupname, print it for only the zeroth interface */
3473e11c3f44Smeem 	if (lifnum(name) == 0) {
3474f7d61273Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3475f7d61273Smeem 		    lifr.lifr_groupname[0] != '\0') {
3476f7d61273Smeem 			(void) printf(" group %s ", lifr.lifr_groupname);
34777c478bd9Sstevel@tonic-gate 		}
34787c478bd9Sstevel@tonic-gate 	}
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate 	/* Print flags to configure */
3481e11c3f44Smeem 	print_config_flags(AF_INET, flags);
34827c478bd9Sstevel@tonic-gate }
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate static void
34857c478bd9Sstevel@tonic-gate in6_configinfo(int force, uint64_t flags)
34867c478bd9Sstevel@tonic-gate {
34877c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
34887c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6, *laddr6;
34897c478bd9Sstevel@tonic-gate 
34907c478bd9Sstevel@tonic-gate 	if (debug)
34917c478bd9Sstevel@tonic-gate 		(void) printf("in6_configinfo(%s) flags 0x%llx\n", name,
34927c478bd9Sstevel@tonic-gate 		    flags);
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV6))
34957c478bd9Sstevel@tonic-gate 		return;
34967c478bd9Sstevel@tonic-gate 
34977c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
34987c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34997c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
35007c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
35017c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
35027c478bd9Sstevel@tonic-gate 				if (!force)
35037c478bd9Sstevel@tonic-gate 					return;
35047c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
35057c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
35067c478bd9Sstevel@tonic-gate 			} else
35077c478bd9Sstevel@tonic-gate 				Perror0_exit("in6_configinfo: SIOCGLIFADDR");
35087c478bd9Sstevel@tonic-gate 		}
35097c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3510e11c3f44Smeem 		(void) printf(" set %s/%d ",
3511e11c3f44Smeem 		    inet_ntop(AF_INET6, &sin6->sin6_addr, abuf, sizeof (abuf)),
3512e11c3f44Smeem 		    lifr.lifr_addrlen);
35137c478bd9Sstevel@tonic-gate 		laddr6 = sin6;
35147c478bd9Sstevel@tonic-gate 	}
35157c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35167c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
35177c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
35187c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
35197c478bd9Sstevel@tonic-gate 			if (!force)
35207c478bd9Sstevel@tonic-gate 				return;
35217c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
35227c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
35237c478bd9Sstevel@tonic-gate 		} else
35247c478bd9Sstevel@tonic-gate 			Perror0_exit("in6_configinfo: SIOCGLIFSUBNET");
35257c478bd9Sstevel@tonic-gate 	}
35267c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
35277c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
35287c478bd9Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
35297c478bd9Sstevel@tonic-gate 		(void) printf(" subnet %s/%d ",
35307c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35317c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
35327c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
35337c478bd9Sstevel@tonic-gate 	}
35347c478bd9Sstevel@tonic-gate 
35357c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
35367c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35377c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
35387c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
35397c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
35407c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
35417c478bd9Sstevel@tonic-gate 			else
3542fc80c0dfSnordmark 				Perror0_exit("in6_configinfo: SIOCGLIFDSTADDR");
35437c478bd9Sstevel@tonic-gate 		}
35447c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
35457c478bd9Sstevel@tonic-gate 		(void) printf(" destination %s ",
35467c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35477c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)));
35487c478bd9Sstevel@tonic-gate 	}
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35517c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
35527c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EINVAL)
35537c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
35547c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
35557c478bd9Sstevel@tonic-gate 		else
3556fc80c0dfSnordmark 			Perror0_exit("in6_configinfo: SIOCGLIFTOKEN");
35577c478bd9Sstevel@tonic-gate 	} else {
35587c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
35597c478bd9Sstevel@tonic-gate 		(void) printf(" token %s/%d ",
35607c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35617c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
35627c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
35637c478bd9Sstevel@tonic-gate 	}
35647c478bd9Sstevel@tonic-gate 
3565e11c3f44Smeem 	/* If there is a groupname, print it for only the zeroth interface */
3566e11c3f44Smeem 	if (lifnum(name) == 0) {
3567f7d61273Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3568f7d61273Smeem 		    lifr.lifr_groupname[0] != '\0') {
3569f7d61273Smeem 			(void) printf(" group %s ", lifr.lifr_groupname);
35707c478bd9Sstevel@tonic-gate 		}
35717c478bd9Sstevel@tonic-gate 	}
35727c478bd9Sstevel@tonic-gate 
35737c478bd9Sstevel@tonic-gate 	/* Print flags to configure */
3574e11c3f44Smeem 	print_config_flags(AF_INET6, flags);
35757c478bd9Sstevel@tonic-gate }
35767c478bd9Sstevel@tonic-gate 
35777c478bd9Sstevel@tonic-gate /*
35787c478bd9Sstevel@tonic-gate  * If this is a physical interface then remove it.
35797c478bd9Sstevel@tonic-gate  * If it is a logical interface name use SIOCLIFREMOVEIF to
35807c478bd9Sstevel@tonic-gate  * remove it. In both cases fail if it doesn't exist.
35817c478bd9Sstevel@tonic-gate  */
35827c478bd9Sstevel@tonic-gate /* ARGSUSED */
35837c478bd9Sstevel@tonic-gate static int
35847c478bd9Sstevel@tonic-gate inetunplumb(char *arg, int64_t param)
35857c478bd9Sstevel@tonic-gate {
3586*6e91bba0SGirish Moodalbail 	ipadm_status_t	istatus;
3587e11c3f44Smeem 
3588*6e91bba0SGirish Moodalbail 	istatus = ipadm_delete_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE);
3589*6e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS) {
3590*6e91bba0SGirish Moodalbail 		(void) fprintf(stderr, "ifconfig: cannot unplumb %s: %s\n",
3591*6e91bba0SGirish Moodalbail 		    name, ipadm_status2str(istatus));
3592*6e91bba0SGirish Moodalbail 		exit(1);
35937c478bd9Sstevel@tonic-gate 	}
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate 	return (0);
35967c478bd9Sstevel@tonic-gate }
35977c478bd9Sstevel@tonic-gate 
35987c478bd9Sstevel@tonic-gate /*
3599*6e91bba0SGirish Moodalbail  * Create the interface in `name', using ipadm_create_if(). If `name' is a
3600*6e91bba0SGirish Moodalbail  * logical interface or loopback interface, ipadm_create_if() uses
3601*6e91bba0SGirish Moodalbail  * SIOCLIFADDIF to create it.
36027c478bd9Sstevel@tonic-gate  */
36037c478bd9Sstevel@tonic-gate /* ARGSUSED */
36047c478bd9Sstevel@tonic-gate static int
36057c478bd9Sstevel@tonic-gate inetplumb(char *arg, int64_t param)
36067c478bd9Sstevel@tonic-gate {
3607*6e91bba0SGirish Moodalbail 	ipadm_status_t	istatus;
36087c478bd9Sstevel@tonic-gate 
3609*6e91bba0SGirish Moodalbail 	istatus = ipadm_create_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE);
3610*6e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS) {
3611*6e91bba0SGirish Moodalbail 		(void) fprintf(stderr, "ifconfig: cannot plumb %s: %s\n",
3612*6e91bba0SGirish Moodalbail 		    name, ipadm_status2str(istatus));
3613*6e91bba0SGirish Moodalbail 		if (istatus != IPADM_IF_EXISTS)
3614*6e91bba0SGirish Moodalbail 			exit(1);
3615f4b3ec61Sdh 	}
3616e11c3f44Smeem 	return (0);
3617e11c3f44Smeem }
3618e11c3f44Smeem 
3619e11c3f44Smeem /* ARGSUSED */
3620e11c3f44Smeem static int
3621e11c3f44Smeem inetipmp(char *arg, int64_t param)
3622e11c3f44Smeem {
3623e11c3f44Smeem 	int retval;
3624e11c3f44Smeem 
3625e11c3f44Smeem 	/*
3626e11c3f44Smeem 	 * Treat e.g. "ifconfig ipmp0:2 ipmp" as "ifconfig ipmp0:2 plumb".
3627e11c3f44Smeem 	 * Otherwise, try to create the requested IPMP interface.
3628e11c3f44Smeem 	 */
3629e11c3f44Smeem 	if (strchr(name, ':') != NULL)
3630e11c3f44Smeem 		retval = inetplumb(arg, param);
3631e11c3f44Smeem 	else
3632e11c3f44Smeem 		retval = create_ipmp(name, afp->af_af, name, _B_FALSE);
3633e11c3f44Smeem 
3634e11c3f44Smeem 	/*
3635e11c3f44Smeem 	 * We'd return -1, but foreachinterface() doesn't propagate the error
3636e11c3f44Smeem 	 * into the exit status, so we're forced to explicitly exit().
3637e11c3f44Smeem 	 */
3638e11c3f44Smeem 	if (retval == -1)
3639e11c3f44Smeem 		exit(1);
36407c478bd9Sstevel@tonic-gate 	return (0);
36417c478bd9Sstevel@tonic-gate }
36427c478bd9Sstevel@tonic-gate 
3643e11c3f44Smeem /*
3644e11c3f44Smeem  * Create an IPMP group `grname' with address family `af'.  If `ifname' is
3645e11c3f44Smeem  * non-NULL, it specifies the interface name to use.  Otherwise, use the name
3646e11c3f44Smeem  * ipmpN, where N corresponds to the lowest available integer.  If `implicit'
3647e11c3f44Smeem  * is set, then the group is being created as a side-effect of placing an
3648e11c3f44Smeem  * underlying interface in a group.  Also start in.mpathd if necessary.
3649e11c3f44Smeem  */
3650e11c3f44Smeem static int
3651e11c3f44Smeem create_ipmp(const char *grname, int af, const char *ifname, boolean_t implicit)
3652e11c3f44Smeem {
3653e11c3f44Smeem 	static int ipmp_daemon_started;
3654*6e91bba0SGirish Moodalbail 	uint32_t flags = IPADM_OPT_IPMP|IPADM_OPT_ACTIVE;
3655*6e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
3656e11c3f44Smeem 
3657e11c3f44Smeem 	if (debug) {
3658e11c3f44Smeem 		(void) printf("create_ipmp: ifname %s grname %s af %d\n",
3659e11c3f44Smeem 		    ifname != NULL ? ifname : "NULL", grname, af);
3660e11c3f44Smeem 	}
3661e11c3f44Smeem 
3662*6e91bba0SGirish Moodalbail 	/*
3663*6e91bba0SGirish Moodalbail 	 * ipadm_create_if() creates the IPMP interface and fills in the
3664*6e91bba0SGirish Moodalbail 	 * ppa in lifr.lifr_name, if `ifname'="ipmp".
3665*6e91bba0SGirish Moodalbail 	 */
3666*6e91bba0SGirish Moodalbail 	(void) strlcpy(lifr.lifr_name, (ifname ? ifname : "ipmp"),
3667*6e91bba0SGirish Moodalbail 	    sizeof (lifr.lifr_name));
3668*6e91bba0SGirish Moodalbail 	if (ifname == NULL)
3669*6e91bba0SGirish Moodalbail 		flags |= IPADM_OPT_GENPPA;
3670*6e91bba0SGirish Moodalbail 	istatus = ipadm_create_if(iph, lifr.lifr_name, af, flags);
3671*6e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS) {
3672*6e91bba0SGirish Moodalbail 		(void) fprintf(stderr, "ifconfig: cannot create IPMP interface "
3673*6e91bba0SGirish Moodalbail 		    "%s: %s\n", grname, ipadm_status2str(istatus));
3674e11c3f44Smeem 		return (-1);
3675e11c3f44Smeem 	}
3676e11c3f44Smeem 
3677e11c3f44Smeem 	/*
3678e11c3f44Smeem 	 * To preserve backward-compatibility, always bring up the link-local
3679e11c3f44Smeem 	 * address for implicitly-created IPv6 IPMP interfaces.
3680e11c3f44Smeem 	 */
3681e11c3f44Smeem 	if (implicit && af == AF_INET6) {
3682e11c3f44Smeem 		if (ioctl(s6, SIOCGLIFFLAGS, &lifr) == 0) {
3683e11c3f44Smeem 			lifr.lifr_flags |= IFF_UP;
3684e11c3f44Smeem 			(void) ioctl(s6, SIOCSLIFFLAGS, &lifr);
3685e11c3f44Smeem 		}
3686e11c3f44Smeem 	}
3687e11c3f44Smeem 
3688e11c3f44Smeem 	/*
3689e11c3f44Smeem 	 * If the caller requested a different group name, issue a
3690e11c3f44Smeem 	 * SIOCSLIFGROUPNAME on the new IPMP interface.
3691e11c3f44Smeem 	 */
3692e11c3f44Smeem 	if (strcmp(lifr.lifr_name, grname) != 0) {
3693e11c3f44Smeem 		(void) strlcpy(lifr.lifr_groupname, grname, LIFGRNAMSIZ);
3694e11c3f44Smeem 		if (ioctl(s, SIOCSLIFGROUPNAME, &lifr) == -1) {
3695e11c3f44Smeem 			Perror0("SIOCSLIFGROUPNAME");
3696e11c3f44Smeem 			return (-1);
3697e11c3f44Smeem 		}
3698e11c3f44Smeem 	}
3699e11c3f44Smeem 
3700e11c3f44Smeem 	/*
3701e11c3f44Smeem 	 * If we haven't done so yet, ensure in.mpathd is started.
3702e11c3f44Smeem 	 */
3703e11c3f44Smeem 	if (ipmp_daemon_started++ == 0)
3704e11c3f44Smeem 		start_ipmp_daemon();
3705e11c3f44Smeem 
3706e11c3f44Smeem 	return (0);
3707e11c3f44Smeem }
3708e11c3f44Smeem 
3709e11c3f44Smeem /*
3710e11c3f44Smeem  * Start in.mpathd if it's not already running.
3711e11c3f44Smeem  */
3712e11c3f44Smeem static void
3713e11c3f44Smeem start_ipmp_daemon(void)
3714e11c3f44Smeem {
3715e11c3f44Smeem 	int retval;
3716e11c3f44Smeem 	ipmp_handle_t ipmp_handle;
3717e11c3f44Smeem 
3718e11c3f44Smeem 	/*
3719e11c3f44Smeem 	 * Ping in.mpathd to see if it's running already.
3720e11c3f44Smeem 	 */
3721e11c3f44Smeem 	if ((retval = ipmp_open(&ipmp_handle)) != IPMP_SUCCESS) {
3722e11c3f44Smeem 		(void) fprintf(stderr, "ifconfig: cannot create IPMP handle: "
3723e11c3f44Smeem 		    "%s\n", ipmp_errmsg(retval));
3724e11c3f44Smeem 		return;
3725e11c3f44Smeem 	}
3726e11c3f44Smeem 
3727e11c3f44Smeem 	retval = ipmp_ping_daemon(ipmp_handle);
3728e11c3f44Smeem 	ipmp_close(ipmp_handle);
3729e11c3f44Smeem 
3730e11c3f44Smeem 	switch (retval) {
3731e11c3f44Smeem 	case IPMP_ENOMPATHD:
3732e11c3f44Smeem 		break;
3733e11c3f44Smeem 	case IPMP_SUCCESS:
3734e11c3f44Smeem 		return;
3735e11c3f44Smeem 	default:
3736e11c3f44Smeem 		(void) fprintf(stderr, "ifconfig: cannot ping in.mpathd: %s\n",
3737e11c3f44Smeem 		    ipmp_errmsg(retval));
3738e11c3f44Smeem 		break;
3739e11c3f44Smeem 	}
3740e11c3f44Smeem 
3741e11c3f44Smeem 	/*
3742e11c3f44Smeem 	 * Start in.mpathd.  Note that in.mpathd will handle multiple
3743e11c3f44Smeem 	 * incarnations (ipmp_ping_daemon() is just an optimization) so we
3744e11c3f44Smeem 	 * don't need to worry about racing with another ifconfig process.
3745e11c3f44Smeem 	 */
3746e11c3f44Smeem 	switch (fork()) {
3747e11c3f44Smeem 	case -1:
3748e11c3f44Smeem 		Perror0_exit("start_ipmp_daemon: fork");
3749e11c3f44Smeem 		/* NOTREACHED */
3750e11c3f44Smeem 	case 0:
3751e11c3f44Smeem 		(void) execl(MPATHD_PATH, MPATHD_PATH, NULL);
3752e11c3f44Smeem 		_exit(1);
3753e11c3f44Smeem 		/* NOTREACHED */
3754e11c3f44Smeem 	default:
3755e11c3f44Smeem 		break;
3756e11c3f44Smeem 	}
3757e11c3f44Smeem }
3758e11c3f44Smeem 
3759e11c3f44Smeem /*
3760e11c3f44Smeem  * Bring the address named by `ifaddrp' up or down.  Doesn't trust any mutable
3761e11c3f44Smeem  * values in ia_flags since they may be stale.
3762e11c3f44Smeem  */
3763e11c3f44Smeem static boolean_t
3764e11c3f44Smeem ifaddr_op(ifaddrlistx_t *ifaddrp, boolean_t up)
3765e11c3f44Smeem {
3766e11c3f44Smeem 	struct lifreq	lifrl;	/* Local lifreq struct */
3767e11c3f44Smeem 	int		fd = (ifaddrp->ia_flags & IFF_IPV4) ? s4 : s6;
3768e11c3f44Smeem 
3769e11c3f44Smeem 	(void) memset(&lifrl, 0, sizeof (lifrl));
3770e11c3f44Smeem 	(void) strlcpy(lifrl.lifr_name, ifaddrp->ia_name, LIFNAMSIZ);
3771e11c3f44Smeem 	if (ioctl(fd, SIOCGLIFFLAGS, &lifrl) == -1)
3772e11c3f44Smeem 		return (_B_FALSE);
3773e11c3f44Smeem 
3774614f1612Smeem 	if (up)
3775e11c3f44Smeem 		lifrl.lifr_flags |= IFF_UP;
3776614f1612Smeem 	else
3777e11c3f44Smeem 		lifrl.lifr_flags &= ~IFF_UP;
3778614f1612Smeem 
3779614f1612Smeem 	if (ioctl(fd, SIOCSLIFFLAGS, &lifrl) == -1)
3780614f1612Smeem 		return (_B_FALSE);
3781614f1612Smeem 
3782614f1612Smeem 	/*
3783614f1612Smeem 	 * If we're trying to bring the address down, ensure that DAD activity
3784614f1612Smeem 	 * (observable by IFF_DUPLICATE) has also been stopped.
3785614f1612Smeem 	 */
3786614f1612Smeem 	if (!up && ioctl(fd, SIOCGLIFFLAGS, &lifrl) != -1 &&
3787614f1612Smeem 	    lifrl.lifr_flags & IFF_DUPLICATE) {
3788614f1612Smeem 		if (ioctl(fd, SIOCGLIFADDR, &lifrl) == -1 ||
3789614f1612Smeem 		    ioctl(fd, SIOCSLIFADDR, &lifrl) == -1) {
3790614f1612Smeem 			return (_B_FALSE);
3791614f1612Smeem 		}
3792e11c3f44Smeem 	}
3793614f1612Smeem 	return (_B_TRUE);
3794e11c3f44Smeem }
3795e11c3f44Smeem 
3796e11c3f44Smeem static boolean_t
3797e11c3f44Smeem ifaddr_up(ifaddrlistx_t *ifaddrp)
3798e11c3f44Smeem {
3799e11c3f44Smeem 	return (ifaddr_op(ifaddrp, _B_TRUE));
3800e11c3f44Smeem }
3801e11c3f44Smeem 
3802e11c3f44Smeem static boolean_t
3803e11c3f44Smeem ifaddr_down(ifaddrlistx_t *ifaddrp)
3804e11c3f44Smeem {
3805e11c3f44Smeem 	return (ifaddr_op(ifaddrp, _B_FALSE));
3806e11c3f44Smeem }
3807e11c3f44Smeem 
38082b24ab6bSSebastien Roy /*
38092b24ab6bSSebastien Roy  * Open the global libdladm handle "dlh" if it isn't already opened.  The
38102b24ab6bSSebastien Roy  * caller may optionally supply a link name to obtain its linkid.  If a link
38112b24ab6bSSebastien Roy  * of a specific class or classes is required, reqclass specifies the class
38122b24ab6bSSebastien Roy  * mask.
38132b24ab6bSSebastien Roy  */
38142b24ab6bSSebastien Roy static dladm_status_t
38152b24ab6bSSebastien Roy ifconfig_dladm_open(const char *name, datalink_class_t reqclass,
38162b24ab6bSSebastien Roy     datalink_id_t *linkid)
38172b24ab6bSSebastien Roy {
38182b24ab6bSSebastien Roy 	dladm_status_t status = DLADM_STATUS_OK;
38192b24ab6bSSebastien Roy 	datalink_class_t class;
38202b24ab6bSSebastien Roy 
38212b24ab6bSSebastien Roy 	if (!dlh_opened) {
38222b24ab6bSSebastien Roy 		if ((status = dladm_open(&dlh)) != DLADM_STATUS_OK)
38232b24ab6bSSebastien Roy 			return (status);
38242b24ab6bSSebastien Roy 		dlh_opened = _B_TRUE;
38252b24ab6bSSebastien Roy 	}
38262b24ab6bSSebastien Roy 	if (name != NULL) {
38272b24ab6bSSebastien Roy 		status = dladm_name2info(dlh, name, linkid, NULL, &class, NULL);
38282b24ab6bSSebastien Roy 		if (status == DLADM_STATUS_OK) {
38292b24ab6bSSebastien Roy 			if (!(class & reqclass))
38302b24ab6bSSebastien Roy 				status = DLADM_STATUS_LINKINVAL;
38312b24ab6bSSebastien Roy 		}
38322b24ab6bSSebastien Roy 	}
38332b24ab6bSSebastien Roy 	return (status);
38342b24ab6bSSebastien Roy }
38352b24ab6bSSebastien Roy 
3836*6e91bba0SGirish Moodalbail /*
3837*6e91bba0SGirish Moodalbail  * This function checks if we can use libipadm API's. We will only
3838*6e91bba0SGirish Moodalbail  * call libipadm functions for non-IPMP interfaces. This check is
3839*6e91bba0SGirish Moodalbail  * temporary until libipadm supports IPMP interfaces.
3840*6e91bba0SGirish Moodalbail  */
3841*6e91bba0SGirish Moodalbail static boolean_t
3842*6e91bba0SGirish Moodalbail ifconfig_use_libipadm(int s, const char *lifname)
3843*6e91bba0SGirish Moodalbail {
3844*6e91bba0SGirish Moodalbail 	struct lifreq lifr1;
3845*6e91bba0SGirish Moodalbail 
3846*6e91bba0SGirish Moodalbail 	(void) strlcpy(lifr1.lifr_name, lifname, sizeof (lifr1.lifr_name));
3847*6e91bba0SGirish Moodalbail 	if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr1) < 0) {
3848*6e91bba0SGirish Moodalbail 		(void) strncpy(lifr.lifr_name, lifname,
3849*6e91bba0SGirish Moodalbail 		    sizeof (lifr.lifr_name));
3850*6e91bba0SGirish Moodalbail 		Perror0_exit("error");
3851*6e91bba0SGirish Moodalbail 	}
3852*6e91bba0SGirish Moodalbail 
3853*6e91bba0SGirish Moodalbail 	return (lifr1.lifr_groupname[0] == '\0');
3854*6e91bba0SGirish Moodalbail }
3855*6e91bba0SGirish Moodalbail 
3856*6e91bba0SGirish Moodalbail static void
3857*6e91bba0SGirish Moodalbail ipadmerr_exit(ipadm_status_t status, const char *str)
3858*6e91bba0SGirish Moodalbail {
3859*6e91bba0SGirish Moodalbail 	(void) fprintf(stderr, "ifconfig: %s: %s\n", str,
3860*6e91bba0SGirish Moodalbail 	    ipadm_status2str(status));
3861*6e91bba0SGirish Moodalbail 	exit(1);
3862*6e91bba0SGirish Moodalbail }
3863*6e91bba0SGirish Moodalbail 
3864*6e91bba0SGirish Moodalbail static void
38652b24ab6bSSebastien Roy dladmerr_exit(dladm_status_t status, const char *str)
38662b24ab6bSSebastien Roy {
38672b24ab6bSSebastien Roy 	char errstr[DLADM_STRSIZE];
38682b24ab6bSSebastien Roy 
38692b24ab6bSSebastien Roy 	(void) fprintf(stderr, "%s: %s\n", str,
38702b24ab6bSSebastien Roy 	    dladm_status2str(status, errstr));
38712b24ab6bSSebastien Roy 	exit(1);
38722b24ab6bSSebastien Roy }
38732b24ab6bSSebastien Roy 
38747c478bd9Sstevel@tonic-gate void
3875f7d61273Smeem Perror0(const char *cmd)
38767c478bd9Sstevel@tonic-gate {
3877f7d61273Smeem 	Perror2(cmd, lifr.lifr_name);
38787c478bd9Sstevel@tonic-gate }
38797c478bd9Sstevel@tonic-gate 
38807c478bd9Sstevel@tonic-gate void
3881f7d61273Smeem Perror0_exit(const char *cmd)
38827c478bd9Sstevel@tonic-gate {
38837c478bd9Sstevel@tonic-gate 	Perror0(cmd);
38847c478bd9Sstevel@tonic-gate 	exit(1);
38857c478bd9Sstevel@tonic-gate }
38867c478bd9Sstevel@tonic-gate 
38877c478bd9Sstevel@tonic-gate void
3888f7d61273Smeem Perror2(const char *cmd, const char *str)
38897c478bd9Sstevel@tonic-gate {
3890f7d61273Smeem 	int error = errno;
38917c478bd9Sstevel@tonic-gate 
3892f7d61273Smeem 	(void) fprintf(stderr, "ifconfig: %s: ", cmd);
38937c478bd9Sstevel@tonic-gate 
3894f7d61273Smeem 	switch (error) {
38957c478bd9Sstevel@tonic-gate 	case ENXIO:
3896f7d61273Smeem 		(void) fprintf(stderr, "%s: no such interface\n", str);
38977c478bd9Sstevel@tonic-gate 		break;
38987c478bd9Sstevel@tonic-gate 	case EPERM:
3899f7d61273Smeem 		(void) fprintf(stderr, "%s: permission denied\n", str);
39007c478bd9Sstevel@tonic-gate 		break;
3901f7d61273Smeem 	case EEXIST:
3902f7d61273Smeem 		(void) fprintf(stderr, "%s: already exists\n", str);
3903f7d61273Smeem 		break;
3904e899e593SGirish Moodalbail 	case ENAMETOOLONG:
3905e899e593SGirish Moodalbail 		(void) fprintf(stderr, "%s: interface name too long\n", str);
3906e899e593SGirish Moodalbail 		break;
3907e899e593SGirish Moodalbail 	case ERANGE:
3908e899e593SGirish Moodalbail 		(void) fprintf(stderr, "%s: logical interface id is outside "
3909e899e593SGirish Moodalbail 		    "allowed range\n", str);
3910e899e593SGirish Moodalbail 		break;
3911f7d61273Smeem 	default:
3912f7d61273Smeem 		errno = error;
3913f7d61273Smeem 		perror(str);
39147c478bd9Sstevel@tonic-gate 	}
39157c478bd9Sstevel@tonic-gate }
39167c478bd9Sstevel@tonic-gate 
39177c478bd9Sstevel@tonic-gate /*
39187c478bd9Sstevel@tonic-gate  * Print out error message (Perror2()) and exit
39197c478bd9Sstevel@tonic-gate  */
39207c478bd9Sstevel@tonic-gate void
3921f7d61273Smeem Perror2_exit(const char *cmd, const char *str)
39227c478bd9Sstevel@tonic-gate {
39237c478bd9Sstevel@tonic-gate 	Perror2(cmd, str);
39247c478bd9Sstevel@tonic-gate 	exit(1);
39257c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
39267c478bd9Sstevel@tonic-gate }
39277c478bd9Sstevel@tonic-gate 
3928c7e4935fSss void
3929c7e4935fSss Perrdlpi(const char *cmd, const char *linkname, int err)
3930c7e4935fSss {
3931c7e4935fSss 	(void) fprintf(stderr, "ifconfig: %s \"%s\": %s\n", cmd,
3932c7e4935fSss 	    linkname, dlpi_strerror(err));
3933c7e4935fSss }
3934c7e4935fSss 
3935c7e4935fSss /*
3936c7e4935fSss  * Print out error message (Perrdlpi()) and exit
3937c7e4935fSss  */
3938c7e4935fSss void
3939c7e4935fSss Perrdlpi_exit(const char *cmd, const char *linkname, int err)
3940c7e4935fSss {
3941c7e4935fSss 	Perrdlpi(cmd, linkname, err);
3942c7e4935fSss 	exit(1);
3943c7e4935fSss }
3944c7e4935fSss 
39457c478bd9Sstevel@tonic-gate /*
39467c478bd9Sstevel@tonic-gate  * If the last argument is non-NULL allow a <addr>/<n> syntax and
39477c478bd9Sstevel@tonic-gate  * pass out <n> in *plenp.
39487c478bd9Sstevel@tonic-gate  * If <n> doesn't parse return BAD_ADDR as *plenp.
39497c478bd9Sstevel@tonic-gate  * If no /<n> is present return NO_PREFIX as *plenp.
39507c478bd9Sstevel@tonic-gate  */
39517c478bd9Sstevel@tonic-gate static void
39527c478bd9Sstevel@tonic-gate in_getaddr(char *s, struct sockaddr *saddr, int *plenp)
39537c478bd9Sstevel@tonic-gate {
395469bb4bb4Scarlsonj 	/* LINTED: alignment */
39557c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
39567c478bd9Sstevel@tonic-gate 	struct hostent *hp;
39577c478bd9Sstevel@tonic-gate 	struct netent *np;
39587c478bd9Sstevel@tonic-gate 	char str[BUFSIZ];
39597c478bd9Sstevel@tonic-gate 	int error_num;
39607c478bd9Sstevel@tonic-gate 
39617c478bd9Sstevel@tonic-gate 	(void) strncpy(str, s, sizeof (str));
39627c478bd9Sstevel@tonic-gate 
39637c478bd9Sstevel@tonic-gate 	/*
39647c478bd9Sstevel@tonic-gate 	 * Look for '/'<n> is plenp
39657c478bd9Sstevel@tonic-gate 	 */
39667c478bd9Sstevel@tonic-gate 	if (plenp != NULL) {
39677c478bd9Sstevel@tonic-gate 		char *cp;
39687c478bd9Sstevel@tonic-gate 
39697906a3e0Smeem 		*plenp = in_getprefixlen(str, _B_TRUE, IP_ABITS);
39707c478bd9Sstevel@tonic-gate 		if (*plenp == BAD_ADDR)
39717c478bd9Sstevel@tonic-gate 			return;
39727c478bd9Sstevel@tonic-gate 		cp = strchr(str, '/');
39737c478bd9Sstevel@tonic-gate 		if (cp != NULL)
39747c478bd9Sstevel@tonic-gate 			*cp = '\0';
39757c478bd9Sstevel@tonic-gate 	} else if (strchr(str, '/') != NULL) {
39767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
39777c478bd9Sstevel@tonic-gate 		exit(1);
39787c478bd9Sstevel@tonic-gate 	}
39797c478bd9Sstevel@tonic-gate 
39807c478bd9Sstevel@tonic-gate 	(void) memset(sin, 0, sizeof (*sin));
39817c478bd9Sstevel@tonic-gate 
39827c478bd9Sstevel@tonic-gate 	/*
39837c478bd9Sstevel@tonic-gate 	 *	Try to catch attempts to set the broadcast address to all 1's.
39847c478bd9Sstevel@tonic-gate 	 */
39857c478bd9Sstevel@tonic-gate 	if (strcmp(str, "255.255.255.255") == 0 ||
39867c478bd9Sstevel@tonic-gate 	    (strtoul(str, (char **)NULL, 0) == 0xffffffffUL)) {
39877c478bd9Sstevel@tonic-gate 		sin->sin_family = AF_INET;
39887c478bd9Sstevel@tonic-gate 		sin->sin_addr.s_addr = 0xffffffff;
39897c478bd9Sstevel@tonic-gate 		return;
39907c478bd9Sstevel@tonic-gate 	}
39917c478bd9Sstevel@tonic-gate 
39927c478bd9Sstevel@tonic-gate 	hp = getipnodebyname(str, AF_INET, 0, &error_num);
39937c478bd9Sstevel@tonic-gate 	if (hp) {
39947c478bd9Sstevel@tonic-gate 		sin->sin_family = hp->h_addrtype;
39957c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
39967c478bd9Sstevel@tonic-gate 		freehostent(hp);
39977c478bd9Sstevel@tonic-gate 		return;
39987c478bd9Sstevel@tonic-gate 	}
39997c478bd9Sstevel@tonic-gate 	np = getnetbyname(str);
40007c478bd9Sstevel@tonic-gate 	if (np) {
40017c478bd9Sstevel@tonic-gate 		sin->sin_family = np->n_addrtype;
40027c478bd9Sstevel@tonic-gate 		sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
40037c478bd9Sstevel@tonic-gate 		return;
40047c478bd9Sstevel@tonic-gate 	}
40057c478bd9Sstevel@tonic-gate 	if (error_num == TRY_AGAIN) {
40067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address "
40077c478bd9Sstevel@tonic-gate 		    "(try again later)\n", s);
40087c478bd9Sstevel@tonic-gate 	} else {
40097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
40107c478bd9Sstevel@tonic-gate 	}
40117c478bd9Sstevel@tonic-gate 	exit(1);
40127c478bd9Sstevel@tonic-gate }
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate /*
40157c478bd9Sstevel@tonic-gate  * If the last argument is non-NULL allow a <addr>/<n> syntax and
40167c478bd9Sstevel@tonic-gate  * pass out <n> in *plenp.
40177c478bd9Sstevel@tonic-gate  * If <n> doesn't parse return BAD_ADDR as *plenp.
40187c478bd9Sstevel@tonic-gate  * If no /<n> is present return NO_PREFIX as *plenp.
40197c478bd9Sstevel@tonic-gate  */
40207c478bd9Sstevel@tonic-gate static void
40217c478bd9Sstevel@tonic-gate in6_getaddr(char *s, struct sockaddr *saddr, int *plenp)
40227c478bd9Sstevel@tonic-gate {
402369bb4bb4Scarlsonj 	/* LINTED: alignment */
40247c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)saddr;
40257c478bd9Sstevel@tonic-gate 	struct hostent *hp;
40267c478bd9Sstevel@tonic-gate 	char str[BUFSIZ];
40277c478bd9Sstevel@tonic-gate 	int error_num;
40287c478bd9Sstevel@tonic-gate 
40297c478bd9Sstevel@tonic-gate 	(void) strncpy(str, s, sizeof (str));
40307c478bd9Sstevel@tonic-gate 
40317c478bd9Sstevel@tonic-gate 	/*
40327c478bd9Sstevel@tonic-gate 	 * Look for '/'<n> is plenp
40337c478bd9Sstevel@tonic-gate 	 */
40347c478bd9Sstevel@tonic-gate 	if (plenp != NULL) {
40357c478bd9Sstevel@tonic-gate 		char *cp;
40367c478bd9Sstevel@tonic-gate 
40377906a3e0Smeem 		*plenp = in_getprefixlen(str, _B_TRUE, IPV6_ABITS);
40387c478bd9Sstevel@tonic-gate 		if (*plenp == BAD_ADDR)
40397c478bd9Sstevel@tonic-gate 			return;
40407c478bd9Sstevel@tonic-gate 		cp = strchr(str, '/');
40417c478bd9Sstevel@tonic-gate 		if (cp != NULL)
40427c478bd9Sstevel@tonic-gate 			*cp = '\0';
40437c478bd9Sstevel@tonic-gate 	} else if (strchr(str, '/') != NULL) {
40447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
40457c478bd9Sstevel@tonic-gate 		exit(1);
40467c478bd9Sstevel@tonic-gate 	}
40477c478bd9Sstevel@tonic-gate 
40487c478bd9Sstevel@tonic-gate 	(void) memset(sin6, 0, sizeof (*sin6));
40497c478bd9Sstevel@tonic-gate 
40507c478bd9Sstevel@tonic-gate 	hp = getipnodebyname(str, AF_INET6, 0, &error_num);
40517c478bd9Sstevel@tonic-gate 	if (hp) {
40527c478bd9Sstevel@tonic-gate 		sin6->sin6_family = hp->h_addrtype;
40537c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin6->sin6_addr, hp->h_addr, hp->h_length);
40547c478bd9Sstevel@tonic-gate 		freehostent(hp);
40557c478bd9Sstevel@tonic-gate 		return;
40567c478bd9Sstevel@tonic-gate 	}
40577c478bd9Sstevel@tonic-gate 	if (error_num == TRY_AGAIN) {
40587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address "
40597c478bd9Sstevel@tonic-gate 		    "(try again later)\n", s);
40607c478bd9Sstevel@tonic-gate 	} else {
40617c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
40627c478bd9Sstevel@tonic-gate 	}
40637c478bd9Sstevel@tonic-gate 	exit(1);
40647c478bd9Sstevel@tonic-gate }
40657c478bd9Sstevel@tonic-gate 
40667c478bd9Sstevel@tonic-gate /*
40677c478bd9Sstevel@tonic-gate  * If "slash" is zero this parses the whole string as
40687c478bd9Sstevel@tonic-gate  * an integer. With "slash" non zero it parses the tail part as an integer.
40697c478bd9Sstevel@tonic-gate  *
40707c478bd9Sstevel@tonic-gate  * If it is not a valid integer this returns BAD_ADDR.
40717c478bd9Sstevel@tonic-gate  * If there is /<n> present this returns NO_PREFIX.
40727c478bd9Sstevel@tonic-gate  */
40737c478bd9Sstevel@tonic-gate static int
40747c478bd9Sstevel@tonic-gate in_getprefixlen(char *addr, boolean_t slash, int max_plen)
40757c478bd9Sstevel@tonic-gate {
40767c478bd9Sstevel@tonic-gate 	int prefixlen;
40777c478bd9Sstevel@tonic-gate 	char *str, *end;
40787c478bd9Sstevel@tonic-gate 
40797c478bd9Sstevel@tonic-gate 	if (slash) {
40807c478bd9Sstevel@tonic-gate 		str = strchr(addr, '/');
40817c478bd9Sstevel@tonic-gate 		if (str == NULL)
40827c478bd9Sstevel@tonic-gate 			return (NO_PREFIX);
40837c478bd9Sstevel@tonic-gate 		str++;
40847c478bd9Sstevel@tonic-gate 	} else
40857c478bd9Sstevel@tonic-gate 		str = addr;
40867c478bd9Sstevel@tonic-gate 
40877c478bd9Sstevel@tonic-gate 	prefixlen = strtol(str, &end, 10);
40887c478bd9Sstevel@tonic-gate 	if (prefixlen < 0)
40897c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
40907c478bd9Sstevel@tonic-gate 	if (str == end)
40917c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
40927c478bd9Sstevel@tonic-gate 	if (max_plen != 0 && max_plen < prefixlen)
40937c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
40947c478bd9Sstevel@tonic-gate 	return (prefixlen);
40957c478bd9Sstevel@tonic-gate }
40967c478bd9Sstevel@tonic-gate 
40977c478bd9Sstevel@tonic-gate /*
40987c478bd9Sstevel@tonic-gate  * Convert a prefix length to a mask.
40997c478bd9Sstevel@tonic-gate  * Returns 1 if ok. 0 otherwise.
41007c478bd9Sstevel@tonic-gate  * Assumes the mask array is zero'ed by the caller.
41017c478bd9Sstevel@tonic-gate  */
41027c478bd9Sstevel@tonic-gate static boolean_t
41037c478bd9Sstevel@tonic-gate in_prefixlentomask(int prefixlen, int maxlen, uchar_t *mask)
41047c478bd9Sstevel@tonic-gate {
41057c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxlen)
41067c478bd9Sstevel@tonic-gate 		return (0);
41077c478bd9Sstevel@tonic-gate 
41087c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
41097c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
41107c478bd9Sstevel@tonic-gate 			*mask++ = 0xFF;
41117c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
41127c478bd9Sstevel@tonic-gate 			continue;
41137c478bd9Sstevel@tonic-gate 		}
41147c478bd9Sstevel@tonic-gate 		*mask |= 1 << (8 - prefixlen);
41157c478bd9Sstevel@tonic-gate 		prefixlen--;
41167c478bd9Sstevel@tonic-gate 	}
41177c478bd9Sstevel@tonic-gate 	return (1);
41187c478bd9Sstevel@tonic-gate }
41197c478bd9Sstevel@tonic-gate 
41207c478bd9Sstevel@tonic-gate static void
41217c478bd9Sstevel@tonic-gate print_flags(uint64_t flags)
41227c478bd9Sstevel@tonic-gate {
41237c478bd9Sstevel@tonic-gate 	boolean_t first = _B_TRUE;
41247c478bd9Sstevel@tonic-gate 	int cnt, i;
41257c478bd9Sstevel@tonic-gate 
41267c478bd9Sstevel@tonic-gate 	(void) printf("flags=%llx", flags);
41277c478bd9Sstevel@tonic-gate 	cnt = sizeof (if_flags_tbl) / sizeof (if_flags_t);
41287c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
41297c478bd9Sstevel@tonic-gate 		if (flags & if_flags_tbl[i].iff_value) {
41307c478bd9Sstevel@tonic-gate 			if (first) {
41317c478bd9Sstevel@tonic-gate 				(void) printf("<");
41327c478bd9Sstevel@tonic-gate 				first = _B_FALSE;
41337c478bd9Sstevel@tonic-gate 			} else {
41347c478bd9Sstevel@tonic-gate 				/*
41357c478bd9Sstevel@tonic-gate 				 * It has to be here and not with the
41367c478bd9Sstevel@tonic-gate 				 * printf below because for the last one,
41377c478bd9Sstevel@tonic-gate 				 * we don't want a comma before the ">".
41387c478bd9Sstevel@tonic-gate 				 */
41397c478bd9Sstevel@tonic-gate 				(void) printf(",");
41407c478bd9Sstevel@tonic-gate 			}
41417c478bd9Sstevel@tonic-gate 			(void) printf("%s", if_flags_tbl[i].iff_name);
41427c478bd9Sstevel@tonic-gate 		}
41437c478bd9Sstevel@tonic-gate 	}
41447c478bd9Sstevel@tonic-gate 	if (!first)
41457c478bd9Sstevel@tonic-gate 		(void) printf(">");
41467c478bd9Sstevel@tonic-gate }
41477c478bd9Sstevel@tonic-gate 
41487c478bd9Sstevel@tonic-gate static void
4149e11c3f44Smeem print_config_flags(int af, uint64_t flags)
41507c478bd9Sstevel@tonic-gate {
4151e11c3f44Smeem 	if_config_cmd_t *cmdp;
41527c478bd9Sstevel@tonic-gate 
4153e11c3f44Smeem 	for (cmdp = if_config_cmd_tbl; cmdp->iff_flag != 0; cmdp++) {
4154e11c3f44Smeem 		if ((flags & cmdp->iff_flag) &&
4155e11c3f44Smeem 		    (cmdp->iff_af == AF_UNSPEC || cmdp->iff_af == af)) {
4156e11c3f44Smeem 			(void) printf("%s ", cmdp->iff_name);
41577c478bd9Sstevel@tonic-gate 		}
41587c478bd9Sstevel@tonic-gate 	}
41597c478bd9Sstevel@tonic-gate }
41607c478bd9Sstevel@tonic-gate 
41617c478bd9Sstevel@tonic-gate /*
416236e852a1SRaja Andra  * Use the configured directory lookup mechanism (e.g. files/NIS/...)
4163dd7a6f5fSkcpoon  * to find the network mask.  Returns true if we found one to set.
4164dd7a6f5fSkcpoon  *
4165dd7a6f5fSkcpoon  * The parameter addr_set controls whether we should get the address of
4166dd7a6f5fSkcpoon  * the working interface for the netmask query.  If addr_set is true,
4167dd7a6f5fSkcpoon  * we will use the address provided.  Otherwise, we will find the working
4168dd7a6f5fSkcpoon  * interface's address and use it instead.
41697c478bd9Sstevel@tonic-gate  */
41707c478bd9Sstevel@tonic-gate static boolean_t
4171dd7a6f5fSkcpoon in_getmask(struct sockaddr_in *saddr, boolean_t addr_set)
41727c478bd9Sstevel@tonic-gate {
41737c478bd9Sstevel@tonic-gate 	struct sockaddr_in ifaddr;
41747c478bd9Sstevel@tonic-gate 
41757c478bd9Sstevel@tonic-gate 	/*
4176dd7a6f5fSkcpoon 	 * Read the address from the interface if it is not passed in.
41777c478bd9Sstevel@tonic-gate 	 */
4178dd7a6f5fSkcpoon 	if (!addr_set) {
4179dd7a6f5fSkcpoon 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
4180dd7a6f5fSkcpoon 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
4181dd7a6f5fSkcpoon 			if (errno != EADDRNOTAVAIL) {
4182dd7a6f5fSkcpoon 				(void) fprintf(stderr, "Need net number for "
4183dd7a6f5fSkcpoon 				    "mask\n");
4184dd7a6f5fSkcpoon 			}
4185dd7a6f5fSkcpoon 			return (_B_FALSE);
4186dd7a6f5fSkcpoon 		}
4187dd7a6f5fSkcpoon 		ifaddr = *((struct sockaddr_in *)&lifr.lifr_addr);
4188dd7a6f5fSkcpoon 	} else {
4189dd7a6f5fSkcpoon 		ifaddr.sin_addr = saddr->sin_addr;
41907c478bd9Sstevel@tonic-gate 	}
41917c478bd9Sstevel@tonic-gate 	if (getnetmaskbyaddr(ifaddr.sin_addr, &saddr->sin_addr) == 0) {
41927c478bd9Sstevel@tonic-gate 		saddr->sin_family = AF_INET;
41937c478bd9Sstevel@tonic-gate 		return (_B_TRUE);
41947c478bd9Sstevel@tonic-gate 	}
41957c478bd9Sstevel@tonic-gate 	return (_B_FALSE);
41967c478bd9Sstevel@tonic-gate }
41977c478bd9Sstevel@tonic-gate 
41987c478bd9Sstevel@tonic-gate static int
4199e11c3f44Smeem lifnum(const char *ifname)
4200e11c3f44Smeem {
4201e11c3f44Smeem 	const char *cp;
4202e11c3f44Smeem 
4203e11c3f44Smeem 	if ((cp = strchr(ifname, ':')) == NULL)
4204e11c3f44Smeem 		return (0);
4205e11c3f44Smeem 	else
4206e11c3f44Smeem 		return (atoi(cp + 1));
4207e11c3f44Smeem }
4208e11c3f44Smeem 
42097c478bd9Sstevel@tonic-gate static void
4210d62bc4baSyz add_ni(const char *name)
42117c478bd9Sstevel@tonic-gate {
42127c478bd9Sstevel@tonic-gate 	ni_t **pp;
42137c478bd9Sstevel@tonic-gate 	ni_t *p;
42147c478bd9Sstevel@tonic-gate 
42157c478bd9Sstevel@tonic-gate 	for (pp = &ni_list; (p = *pp) != NULL; pp = &(p->ni_next)) {
42167c478bd9Sstevel@tonic-gate 		if (strcmp(p->ni_name, name) == 0) {
42177c478bd9Sstevel@tonic-gate 			if (debug > 2)
42187c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "'%s' is a duplicate\n",
42197c478bd9Sstevel@tonic-gate 				    name);
42207c478bd9Sstevel@tonic-gate 			return;
42217c478bd9Sstevel@tonic-gate 		}
42227c478bd9Sstevel@tonic-gate 	}
42237c478bd9Sstevel@tonic-gate 
42247c478bd9Sstevel@tonic-gate 	if (debug > 2)
42257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "adding '%s'\n",
42267c478bd9Sstevel@tonic-gate 		    name);
42277c478bd9Sstevel@tonic-gate 
42287c478bd9Sstevel@tonic-gate 	if ((p = malloc(sizeof (ni_t))) == NULL)
42297c478bd9Sstevel@tonic-gate 		return;
42307c478bd9Sstevel@tonic-gate 
42317c478bd9Sstevel@tonic-gate 	(void) strlcpy(p->ni_name, name, sizeof (p->ni_name));
42327c478bd9Sstevel@tonic-gate 	p->ni_next = NULL;
42337c478bd9Sstevel@tonic-gate 
42347c478bd9Sstevel@tonic-gate 	*pp = p;
42357c478bd9Sstevel@tonic-gate 	num_ni++;
42367c478bd9Sstevel@tonic-gate }
42377c478bd9Sstevel@tonic-gate 
4238d62bc4baSyz static boolean_t
4239d62bc4baSyz ni_entry(const char *linkname, void *arg)
42407c478bd9Sstevel@tonic-gate {
4241c7e4935fSss 	dlpi_handle_t	dh;
4242da14cebeSEric Cheng 	datalink_class_t class;
42437c478bd9Sstevel@tonic-gate 
4244d4d1f7bfSVasumathi Sundaram - Sun Microsystems 	(void) dladm_name2info(arg, linkname, NULL, NULL, &class, NULL);
42454ac67f02SAnurag S. Maskey 
4246da14cebeSEric Cheng 	if (class == DATALINK_CLASS_ETHERSTUB)
4247da14cebeSEric Cheng 		return (_B_FALSE);
4248c7e4935fSss 	if (dlpi_open(linkname, &dh, 0) != DLPI_SUCCESS)
4249d62bc4baSyz 		return (_B_FALSE);
42507c478bd9Sstevel@tonic-gate 
4251d62bc4baSyz 	add_ni(linkname);
42527c478bd9Sstevel@tonic-gate 
4253c7e4935fSss 	dlpi_close(dh);
4254d62bc4baSyz 	return (_B_FALSE);
42557c478bd9Sstevel@tonic-gate }
42567c478bd9Sstevel@tonic-gate 
42577c478bd9Sstevel@tonic-gate /*
42587c478bd9Sstevel@tonic-gate  * dhcp-related routines
42597c478bd9Sstevel@tonic-gate  */
42607c478bd9Sstevel@tonic-gate 
42617c478bd9Sstevel@tonic-gate static int
42627c478bd9Sstevel@tonic-gate setifdhcp(const char *caller, const char *ifname, int argc, char *argv[])
42637c478bd9Sstevel@tonic-gate {
42647c478bd9Sstevel@tonic-gate 	dhcp_ipc_request_t	*request;
42657c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t	*reply	= NULL;
42667c478bd9Sstevel@tonic-gate 	int			timeout = DHCP_IPC_WAIT_DEFAULT;
42677c478bd9Sstevel@tonic-gate 	dhcp_ipc_type_t		type	= DHCP_START;
42687c478bd9Sstevel@tonic-gate 	int			error;
42697c478bd9Sstevel@tonic-gate 	boolean_t		is_primary = _B_FALSE;
42707c478bd9Sstevel@tonic-gate 	boolean_t		started = _B_FALSE;
42717c478bd9Sstevel@tonic-gate 
42727c478bd9Sstevel@tonic-gate 	for (argv++; --argc > 0; argv++) {
42737c478bd9Sstevel@tonic-gate 
42747c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "primary") == 0) {
42757c478bd9Sstevel@tonic-gate 			is_primary = _B_TRUE;
42767c478bd9Sstevel@tonic-gate 			continue;
42777c478bd9Sstevel@tonic-gate 		}
42787c478bd9Sstevel@tonic-gate 
42797c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "wait") == 0) {
42807c478bd9Sstevel@tonic-gate 			if (--argc <= 0) {
42817c478bd9Sstevel@tonic-gate 				usage();
42827c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
42837c478bd9Sstevel@tonic-gate 			}
42847c478bd9Sstevel@tonic-gate 			argv++;
42857c478bd9Sstevel@tonic-gate 
42867c478bd9Sstevel@tonic-gate 			if (strcmp(*argv, "forever") == 0) {
42877c478bd9Sstevel@tonic-gate 				timeout = DHCP_IPC_WAIT_FOREVER;
42887c478bd9Sstevel@tonic-gate 				continue;
42897c478bd9Sstevel@tonic-gate 			}
42907c478bd9Sstevel@tonic-gate 
42917c478bd9Sstevel@tonic-gate 			if (sscanf(*argv, "%d", &timeout) != 1) {
42927c478bd9Sstevel@tonic-gate 				usage();
42937c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
42947c478bd9Sstevel@tonic-gate 			}
42957c478bd9Sstevel@tonic-gate 
42967c478bd9Sstevel@tonic-gate 			if (timeout < 0) {
42977c478bd9Sstevel@tonic-gate 				usage();
42987c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
42997c478bd9Sstevel@tonic-gate 			}
43007c478bd9Sstevel@tonic-gate 			continue;
43017c478bd9Sstevel@tonic-gate 		}
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate 		type = dhcp_string_to_request(*argv);
43047c478bd9Sstevel@tonic-gate 		if (type == -1) {
43057c478bd9Sstevel@tonic-gate 			usage();
43067c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_BADARGS);
43077c478bd9Sstevel@tonic-gate 		}
43087c478bd9Sstevel@tonic-gate 	}
43097c478bd9Sstevel@tonic-gate 
43107c478bd9Sstevel@tonic-gate 	/*
43117c478bd9Sstevel@tonic-gate 	 * Only try to start agent on start or inform; in all other cases it
43127c478bd9Sstevel@tonic-gate 	 * has to already be running for anything to make sense.
43137c478bd9Sstevel@tonic-gate 	 */
43147c478bd9Sstevel@tonic-gate 	if (type == DHCP_START || type == DHCP_INFORM) {
43157c478bd9Sstevel@tonic-gate 		if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1) {
43167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: unable to start %s\n",
43177c478bd9Sstevel@tonic-gate 			    caller, DHCP_AGENT_PATH);
43187c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_FAILURE);
43197c478bd9Sstevel@tonic-gate 		}
43207c478bd9Sstevel@tonic-gate 		started = _B_TRUE;
43217c478bd9Sstevel@tonic-gate 	}
43227c478bd9Sstevel@tonic-gate 
43237c478bd9Sstevel@tonic-gate 	if (is_primary)
4324d04ccbb3Scarlsonj 		type |= DHCP_PRIMARY;
4325d04ccbb3Scarlsonj 
4326d04ccbb3Scarlsonj 	if (af != AF_INET)
4327d04ccbb3Scarlsonj 		type |= DHCP_V6;
43287c478bd9Sstevel@tonic-gate 
43297c478bd9Sstevel@tonic-gate 	request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
43307c478bd9Sstevel@tonic-gate 	if (request == NULL) {
43317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: out of memory\n", caller);
43327c478bd9Sstevel@tonic-gate 		return (DHCP_EXIT_SYSTEM);
43337c478bd9Sstevel@tonic-gate 	}
43347c478bd9Sstevel@tonic-gate 
43357c478bd9Sstevel@tonic-gate 	error = dhcp_ipc_make_request(request, &reply, timeout);
43367c478bd9Sstevel@tonic-gate 	if (error != 0) {
43377c478bd9Sstevel@tonic-gate 		free(request);
43387c478bd9Sstevel@tonic-gate 		/*
43397c478bd9Sstevel@tonic-gate 		 * Re-map connect error to not under control if we didn't try a
43407c478bd9Sstevel@tonic-gate 		 * start operation, as this has to be true and results in a
43417c478bd9Sstevel@tonic-gate 		 * clearer message, not to mention preserving compatibility
43427c478bd9Sstevel@tonic-gate 		 * with the days when we always started dhcpagent for every
43437c478bd9Sstevel@tonic-gate 		 * request.
43447c478bd9Sstevel@tonic-gate 		 */
43457c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_CONNECT && !started)
43467c478bd9Sstevel@tonic-gate 			error = DHCP_IPC_E_UNKIF;
43477c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
43487c478bd9Sstevel@tonic-gate 		    dhcp_ipc_strerror(error));
43497c478bd9Sstevel@tonic-gate 		return (DHCP_EXIT_FAILURE);
43507c478bd9Sstevel@tonic-gate 	}
43517c478bd9Sstevel@tonic-gate 
43527c478bd9Sstevel@tonic-gate 	error = reply->return_code;
43537c478bd9Sstevel@tonic-gate 	if (error != 0) {
43547c478bd9Sstevel@tonic-gate 		free(request);
43557c478bd9Sstevel@tonic-gate 		free(reply);
43567c478bd9Sstevel@tonic-gate 
43577c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_TIMEOUT && timeout == 0)
43587c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_SUCCESS);
43597c478bd9Sstevel@tonic-gate 
43607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
43617c478bd9Sstevel@tonic-gate 		    dhcp_ipc_strerror(error));
43627c478bd9Sstevel@tonic-gate 
43637c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_TIMEOUT)
43647c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_TIMEOUT);
43657c478bd9Sstevel@tonic-gate 		else
43667c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_IF_FAILURE);
43677c478bd9Sstevel@tonic-gate 	}
43687c478bd9Sstevel@tonic-gate 
43697c478bd9Sstevel@tonic-gate 	if (DHCP_IPC_CMD(type) == DHCP_STATUS) {
43707c478bd9Sstevel@tonic-gate 		(void) printf("%s", dhcp_status_hdr_string());
43717c478bd9Sstevel@tonic-gate 		(void) printf("%s", dhcp_status_reply_to_string(reply));
43727c478bd9Sstevel@tonic-gate 	}
43737c478bd9Sstevel@tonic-gate 
43747c478bd9Sstevel@tonic-gate 	free(request);
43757c478bd9Sstevel@tonic-gate 	free(reply);
43767c478bd9Sstevel@tonic-gate 	return (DHCP_EXIT_SUCCESS);
43777c478bd9Sstevel@tonic-gate }
43787c478bd9Sstevel@tonic-gate 
43797c478bd9Sstevel@tonic-gate static void
43807c478bd9Sstevel@tonic-gate usage(void)
43817c478bd9Sstevel@tonic-gate {
43827c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
43837c478bd9Sstevel@tonic-gate 	    "usage: ifconfig <interface> | -a[ 4 | 6 | D ][ u | d ][ Z ]\n");
43847c478bd9Sstevel@tonic-gate 
43857c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s",
43867c478bd9Sstevel@tonic-gate 	    "\t[ <addr_family> ]\n"
43877c478bd9Sstevel@tonic-gate 	    "\t[ <address>[/<prefix_length>] [ <dest_address> ] ]\n"
43887c478bd9Sstevel@tonic-gate 	    "\t[ set [ <address>][/<prefix_length>] ]"
43897c478bd9Sstevel@tonic-gate 	    " [ <address>/<prefix_length>] ]\n"
43907c478bd9Sstevel@tonic-gate 	    "\t[ destination <dest_address> ]\n"
43917c478bd9Sstevel@tonic-gate 	    "\t[ addif <address>[/<prefix_length>]"
43927c478bd9Sstevel@tonic-gate 	    "  [ <dest_address> ] ]\n"
43937c478bd9Sstevel@tonic-gate 	    "\t[ removeif <address>[/<prefix_length>] ]\n"
43947c478bd9Sstevel@tonic-gate 	    "\t[ arp | -arp ]\n"
43957c478bd9Sstevel@tonic-gate 	    "\t[ auto-revarp ]\n"
43967c478bd9Sstevel@tonic-gate 	    "\t[ broadcast <broad_addr> ]\n"
43977c478bd9Sstevel@tonic-gate 	    "\t[ index <if_index> ]\n"
43987c478bd9Sstevel@tonic-gate 	    "\t[ metric <n> ] [ mtu <n> ]\n"
43997c478bd9Sstevel@tonic-gate 	    "\t[ netmask <mask> ]\n"
44007c478bd9Sstevel@tonic-gate 	    "\t[ plumb ] [ unplumb ]\n"
44017c478bd9Sstevel@tonic-gate 	    "\t[ preferred | -preferred ]\n"
44027c478bd9Sstevel@tonic-gate 	    "\t[ private | -private ]\n"
44037c478bd9Sstevel@tonic-gate 	    "\t[ local | -local ]\n"
44047c478bd9Sstevel@tonic-gate 	    "\t[ router | -router ]\n"
44057c478bd9Sstevel@tonic-gate 	    "\t[ subnet <subnet_address>]\n"
44067c478bd9Sstevel@tonic-gate 	    "\t[ trailers | -trailers ]\n"
44077c478bd9Sstevel@tonic-gate 	    "\t[ token <address>/<prefix_length> ]\n"
44087c478bd9Sstevel@tonic-gate 	    "\t[ tsrc <tunnel_src_address> ]\n"
44097c478bd9Sstevel@tonic-gate 	    "\t[ tdst <tunnel_dest_address> ]\n"
44107c478bd9Sstevel@tonic-gate 	    "\t[ auth_algs <tunnel_AH_authentication_algorithm> ]\n"
44117c478bd9Sstevel@tonic-gate 	    "\t[ encr_algs <tunnel_ESP_encryption_algorithm> ]\n"
44127c478bd9Sstevel@tonic-gate 	    "\t[ encr_auth_algs <tunnel_ESP_authentication_algorithm> ]\n"
44137c478bd9Sstevel@tonic-gate 	    "\t[ up ] [ down ]\n"
44147c478bd9Sstevel@tonic-gate 	    "\t[ xmit | -xmit ]\n"
44157c478bd9Sstevel@tonic-gate 	    "\t[ modlist ]\n"
44167c478bd9Sstevel@tonic-gate 	    "\t[ modinsert <module_name@position> ]\n"
44177c478bd9Sstevel@tonic-gate 	    "\t[ modremove <module_name@position> ]\n"
4418e11c3f44Smeem 	    "\t[ ipmp ]\n"
44197c478bd9Sstevel@tonic-gate 	    "\t[ group <groupname>] | [ group \"\"]\n"
44207c478bd9Sstevel@tonic-gate 	    "\t[ deprecated | -deprecated ]\n"
44217c478bd9Sstevel@tonic-gate 	    "\t[ standby | -standby ]\n"
44227c478bd9Sstevel@tonic-gate 	    "\t[ failover | -failover ]\n"
44237c478bd9Sstevel@tonic-gate 	    "\t[ zone <zonename> | -zone ]\n"
442445916cd2Sjpk 	    "\t[ usesrc <interface> ]\n"
442545916cd2Sjpk 	    "\t[ all-zones ]\n");
44267c478bd9Sstevel@tonic-gate 
44277c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "or\n");
44287c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
44297c478bd9Sstevel@tonic-gate 	    "\tifconfig <interface> |  -a[ 4 | 6 | D ] [ u | d ]\n");
44307c478bd9Sstevel@tonic-gate 
44317c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s", "\tauto-dhcp | dhcp\n"
44327c478bd9Sstevel@tonic-gate 	    "\t[ wait <time> | forever ]\n\t[ primary ]\n"
44337c478bd9Sstevel@tonic-gate 	    "\tstart | drop | ping | release | status | inform\n");
44347c478bd9Sstevel@tonic-gate }
4435