17c478bd9Sstevel@tonic-gate /*
236b41818SGirish Moodalbail  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
3*60b43c45SDan McDonald  * Copyright 2012, Daniil Lunev. All rights reserved.
4*60b43c45SDan McDonald  * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
57c478bd9Sstevel@tonic-gate  */
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
97c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
107c478bd9Sstevel@tonic-gate  */
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #include "defs.h"
137c478bd9Sstevel@tonic-gate #include "strings.h"
147c478bd9Sstevel@tonic-gate #include "ifconfig.h"
157c478bd9Sstevel@tonic-gate #include <compat.h>
167c478bd9Sstevel@tonic-gate #include <libdlpi.h>
17da14cebeSEric Cheng #include <libdllink.h>
182b24ab6bSSebastien Roy #include <libdliptun.h>
192b24ab6bSSebastien Roy #include <libdllink.h>
20ff550d0eSmasputra #include <inet/ip.h>
21d2f8a3dfSpwernau #include <inet/ipsec_impl.h>
226e91bba0SGirish Moodalbail #include <libipadm.h>
236e91bba0SGirish Moodalbail #include <ifaddrs.h>
246e91bba0SGirish Moodalbail #include <libsocket_priv.h>
25ff550d0eSmasputra 
267c478bd9Sstevel@tonic-gate #define	LOOPBACK_IF	"lo0"
277c478bd9Sstevel@tonic-gate #define	NONE_STR	"none"
287c478bd9Sstevel@tonic-gate #define	ARP_MOD_NAME	"arp"
296e91bba0SGirish Moodalbail #define	LIFC_DEFAULT	(LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES |\
306e91bba0SGirish Moodalbail 			LIFC_UNDER_IPMP)
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate typedef struct if_flags {
337c478bd9Sstevel@tonic-gate 	uint64_t iff_value;
347c478bd9Sstevel@tonic-gate 	char	*iff_name;
357c478bd9Sstevel@tonic-gate } if_flags_t;
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static if_flags_t	if_flags_tbl[] = {
387c478bd9Sstevel@tonic-gate 	{ IFF_UP,		"UP" },
397c478bd9Sstevel@tonic-gate 	{ IFF_BROADCAST,	"BROADCAST" },
407c478bd9Sstevel@tonic-gate 	{ IFF_DEBUG,		"DEBUG" },
417c478bd9Sstevel@tonic-gate 	{ IFF_LOOPBACK,		"LOOPBACK" },
427c478bd9Sstevel@tonic-gate 	{ IFF_POINTOPOINT,	"POINTOPOINT" },
437c478bd9Sstevel@tonic-gate 	{ IFF_NOTRAILERS,	"NOTRAILERS" },
447c478bd9Sstevel@tonic-gate 	{ IFF_RUNNING,		"RUNNING" },
457c478bd9Sstevel@tonic-gate 	{ IFF_NOARP,		"NOARP" },
467c478bd9Sstevel@tonic-gate 	{ IFF_PROMISC,		"PROMISC" },
477c478bd9Sstevel@tonic-gate 	{ IFF_ALLMULTI,		"ALLMULTI" },
487c478bd9Sstevel@tonic-gate 	{ IFF_INTELLIGENT,	"INTELLIGENT" },
497c478bd9Sstevel@tonic-gate 	{ IFF_MULTICAST,	"MULTICAST" },
507c478bd9Sstevel@tonic-gate 	{ IFF_MULTI_BCAST,	"MULTI_BCAST" },
517c478bd9Sstevel@tonic-gate 	{ IFF_UNNUMBERED,	"UNNUMBERED" },
527c478bd9Sstevel@tonic-gate 	{ IFF_DHCPRUNNING,	"DHCP" },
537c478bd9Sstevel@tonic-gate 	{ IFF_PRIVATE,		"PRIVATE" },
547c478bd9Sstevel@tonic-gate 	{ IFF_NOXMIT,		"NOXMIT" },
557c478bd9Sstevel@tonic-gate 	{ IFF_NOLOCAL,		"NOLOCAL" },
567c478bd9Sstevel@tonic-gate 	{ IFF_DEPRECATED,	"DEPRECATED" },
577c478bd9Sstevel@tonic-gate 	{ IFF_ADDRCONF,		"ADDRCONF" },
587c478bd9Sstevel@tonic-gate 	{ IFF_ROUTER,		"ROUTER" },
597c478bd9Sstevel@tonic-gate 	{ IFF_NONUD,		"NONUD" },
607c478bd9Sstevel@tonic-gate 	{ IFF_ANYCAST,		"ANYCAST" },
617c478bd9Sstevel@tonic-gate 	{ IFF_NORTEXCH,		"NORTEXCH" },
627c478bd9Sstevel@tonic-gate 	{ IFF_IPV4,		"IPv4" },
637c478bd9Sstevel@tonic-gate 	{ IFF_IPV6,		"IPv6" },
647c478bd9Sstevel@tonic-gate 	{ IFF_NOFAILOVER,	"NOFAILOVER" },
657c478bd9Sstevel@tonic-gate 	{ IFF_FAILED,		"FAILED" },
667c478bd9Sstevel@tonic-gate 	{ IFF_STANDBY,		"STANDBY" },
677c478bd9Sstevel@tonic-gate 	{ IFF_INACTIVE,		"INACTIVE" },
687c478bd9Sstevel@tonic-gate 	{ IFF_OFFLINE,		"OFFLINE" },
697c478bd9Sstevel@tonic-gate 	{ IFF_XRESOLV,		"XRESOLV" },
707c478bd9Sstevel@tonic-gate 	{ IFF_COS_ENABLED,	"CoS" },
717c478bd9Sstevel@tonic-gate 	{ IFF_PREFERRED,	"PREFERRED" },
727c478bd9Sstevel@tonic-gate 	{ IFF_TEMPORARY,	"TEMPORARY" },
737c478bd9Sstevel@tonic-gate 	{ IFF_FIXEDMTU,		"FIXEDMTU" },
7469bb4bb4Scarlsonj 	{ IFF_VIRTUAL,		"VIRTUAL" },
75e11c3f44Smeem 	{ IFF_DUPLICATE,	"DUPLICATE" },
761cb875aeSCathy Zhou 	{ IFF_IPMP,		"IPMP"},
771cb875aeSCathy Zhou 	{ IFF_VRRP,		"VRRP"},
78550b6e40SSowmini Varadhan 	{ IFF_NOACCEPT,		"NOACCEPT"},
79550b6e40SSowmini Varadhan 	{ IFF_L3PROTECT,	"L3PROTECT"}
80e11c3f44Smeem };
81e11c3f44Smeem 
82e11c3f44Smeem typedef struct {
83e11c3f44Smeem 	const char		*ia_app;
84e11c3f44Smeem 	uint64_t		ia_flag;
85e11c3f44Smeem 	uint_t			ia_tries;
86e11c3f44Smeem } if_appflags_t;
87e11c3f44Smeem 
88e11c3f44Smeem static const if_appflags_t if_appflags_tbl[] = {
89e11c3f44Smeem 	{ "dhcpagent(1M)",	IFF_DHCPRUNNING,	1 },
90e11c3f44Smeem 	{ "in.ndpd(1M)",	IFF_ADDRCONF,		3 },
91e11c3f44Smeem 	{  NULL,		0,			0 }
927c478bd9Sstevel@tonic-gate };
937c478bd9Sstevel@tonic-gate 
942b24ab6bSSebastien Roy static dladm_handle_t	dlh;
952b24ab6bSSebastien Roy boolean_t		dlh_opened;
962b24ab6bSSebastien Roy static struct		lifreq lifr;
977906a3e0Smeem /* current interface name a particular function is accessing */
982b24ab6bSSebastien Roy static char		name[LIFNAMSIZ];
997c478bd9Sstevel@tonic-gate /* foreach interface saved name */
1002b24ab6bSSebastien Roy static char		origname[LIFNAMSIZ];
1012b24ab6bSSebastien Roy static int		setaddr;
1026e91bba0SGirish Moodalbail static boolean_t	setaddr_done = _B_FALSE;
1032b24ab6bSSebastien Roy static boolean_t	ipsec_policy_set;
1042b24ab6bSSebastien Roy static boolean_t	ipsec_auth_covered;
1056e91bba0SGirish Moodalbail static ipadm_handle_t	iph;
1066e91bba0SGirish Moodalbail static ipadm_addrobj_t	ipaddr;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  * Make sure the algorithm variables hold more than the sizeof an algorithm
1107c478bd9Sstevel@tonic-gate  * in PF_KEY.  (For now, more than a uint8_t.)  The NO_***_?ALG indicates that
1117c478bd9Sstevel@tonic-gate  * there was no algorithm requested, and in the ipsec_req that service should
1127c478bd9Sstevel@tonic-gate  * be disabled.  (E.g. if ah_aalg remains NO_AH_AALG, then AH will be
1137c478bd9Sstevel@tonic-gate  * disabled on that tunnel.)
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate #define	NO_AH_AALG 256
1167c478bd9Sstevel@tonic-gate #define	NO_ESP_AALG 256
1177c478bd9Sstevel@tonic-gate #define	NO_ESP_EALG 256
1187c478bd9Sstevel@tonic-gate 
119e11c3f44Smeem int	s, s4, s6;
1207c478bd9Sstevel@tonic-gate int	af = AF_INET;	/* default address family */
1217c478bd9Sstevel@tonic-gate int	debug = 0;
1227c478bd9Sstevel@tonic-gate int	all = 0;	/* setifdhcp() needs to know this */
1237c478bd9Sstevel@tonic-gate int	verbose = 0;
1247c478bd9Sstevel@tonic-gate int	v4compat = 0;	/* Compatible printing format */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Function prototypes for command functions.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate static int	addif(char *arg, int64_t param);
130e11c3f44Smeem static int	inetipmp(char *arg, int64_t param);
1317c478bd9Sstevel@tonic-gate static int	inetplumb(char *arg, int64_t param);
1327c478bd9Sstevel@tonic-gate static int	inetunplumb(char *arg, int64_t param);
1337c478bd9Sstevel@tonic-gate static int	removeif(char *arg, int64_t param);
1347c478bd9Sstevel@tonic-gate static int	setdebugflag(char *arg, int64_t param);
1357c478bd9Sstevel@tonic-gate static int	setifaddr(char *arg, int64_t param);
1367c478bd9Sstevel@tonic-gate static int	setifbroadaddr(char *arg, int64_t param);
1377c478bd9Sstevel@tonic-gate static int	setifdstaddr(char *arg, int64_t param);
1387c478bd9Sstevel@tonic-gate static int	setifether(char *arg, int64_t param);
1397c478bd9Sstevel@tonic-gate static int	setifflags(char *arg, int64_t param);
1407c478bd9Sstevel@tonic-gate static int	setifindex(char *arg, int64_t param);
1417c478bd9Sstevel@tonic-gate static int	setifmetric(char *arg, int64_t param);
1427c478bd9Sstevel@tonic-gate static int	setifmtu(char *arg, int64_t param);
1437c478bd9Sstevel@tonic-gate static int	setifnetmask(char *arg, int64_t param);
1447c478bd9Sstevel@tonic-gate static int	setifprefixlen(char *arg, int64_t param);
1457c478bd9Sstevel@tonic-gate static int	setifrevarp(char *arg, int64_t param);
1467c478bd9Sstevel@tonic-gate static int	setifsubnet(char *arg, int64_t param);
1477c478bd9Sstevel@tonic-gate static int	setiftdst(char *arg, int64_t param);
1487c478bd9Sstevel@tonic-gate static int	setiftoken(char *arg, int64_t param);
1497c478bd9Sstevel@tonic-gate static int	setiftsrc(char *arg, int64_t param);
1507c478bd9Sstevel@tonic-gate static int	setverboseflag(char *arg, int64_t param);
1517c478bd9Sstevel@tonic-gate static int	set_tun_ah_alg(char *arg, int64_t param);
1527c478bd9Sstevel@tonic-gate static int	set_tun_esp_auth_alg(char *arg, int64_t param);
1537c478bd9Sstevel@tonic-gate static int	set_tun_esp_encr_alg(char *arg, int64_t param);
1547c478bd9Sstevel@tonic-gate static int	modlist(char *arg, int64_t param);
1557c478bd9Sstevel@tonic-gate static int	modinsert(char *arg, int64_t param);
1567c478bd9Sstevel@tonic-gate static int	modremove(char *arg, int64_t param);
1577c478bd9Sstevel@tonic-gate static int	setifgroupname(char *arg, int64_t param);
1587c478bd9Sstevel@tonic-gate static int	configinfo(char *arg, int64_t param);
159e11c3f44Smeem static void	print_config_flags(int af, uint64_t flags);
1607c478bd9Sstevel@tonic-gate static void	print_flags(uint64_t flags);
1616e91bba0SGirish Moodalbail static void	print_ifether(const char *ifname);
1627c478bd9Sstevel@tonic-gate static int	set_tun_encap_limit(char *arg, int64_t param);
1637c478bd9Sstevel@tonic-gate static int	clr_tun_encap_limit(char *arg, int64_t param);
1647c478bd9Sstevel@tonic-gate static int	set_tun_hop_limit(char *arg, int64_t param);
1657c478bd9Sstevel@tonic-gate static int	setzone(char *arg, int64_t param);
16645916cd2Sjpk static int	setallzones(char *arg, int64_t param);
1677c478bd9Sstevel@tonic-gate static int	setifsrc(char *arg, int64_t param);
168e11c3f44Smeem static int	lifnum(const char *ifname);
1696e91bba0SGirish Moodalbail static void	plumball(int, char **, int64_t, int64_t, int64_t);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * Address family specific function prototypes.
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate static void	in_getaddr(char *s, struct sockaddr *saddr, int *plenp);
1757c478bd9Sstevel@tonic-gate static void	in_status(int force, uint64_t flags);
1767c478bd9Sstevel@tonic-gate static void	in_configinfo(int force, uint64_t flags);
1777c478bd9Sstevel@tonic-gate static void	in6_getaddr(char *s, struct sockaddr *saddr, int *plenp);
1787c478bd9Sstevel@tonic-gate static void	in6_status(int force, uint64_t flags);
1797c478bd9Sstevel@tonic-gate static void	in6_configinfo(int force, uint64_t flags);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * Misc support functions
1837c478bd9Sstevel@tonic-gate  */
184d62bc4baSyz static boolean_t	ni_entry(const char *, void *);
1856e91bba0SGirish Moodalbail static void		foreachinterface(int argc, char *argv[],
1866e91bba0SGirish Moodalbail 			    int af, int64_t onflags, int64_t offflags,
1876e91bba0SGirish Moodalbail 			    int64_t lifc_flags);
1886e91bba0SGirish Moodalbail static void		ifconfig(int argc, char *argv[], int af,
1896e91bba0SGirish Moodalbail 			    struct ifaddrs *ifa);
190dd7a6f5fSkcpoon static boolean_t	in_getmask(struct sockaddr_in *saddr,
191dd7a6f5fSkcpoon 			    boolean_t addr_set);
1926e91bba0SGirish Moodalbail static int		in_getprefixlen(char *addr, boolean_t slash, int plen);
1937c478bd9Sstevel@tonic-gate static boolean_t	in_prefixlentomask(int prefixlen, int maxlen,
1947c478bd9Sstevel@tonic-gate 			    uchar_t *mask);
1956e91bba0SGirish Moodalbail static void		status(void);
1966e91bba0SGirish Moodalbail static void		ifstatus(const char *ifname);
1976e91bba0SGirish Moodalbail static void		tun_status(datalink_id_t);
1986e91bba0SGirish Moodalbail static void		usage(void);
1996e91bba0SGirish Moodalbail static int		setifdhcp(const char *caller, const char *ifname,
2006e91bba0SGirish Moodalbail 			    int argc, char *argv[]);
2016e91bba0SGirish Moodalbail static int		ip_domux2fd(int *, int *, int *, int *, int *);
2026e91bba0SGirish Moodalbail static int		ip_plink(int, int, int, int, int);
2036e91bba0SGirish Moodalbail static int		modop(char *arg, char op);
2046e91bba0SGirish Moodalbail static int		find_all_interfaces(struct lifconf *lifcp, char **buf,
2056e91bba0SGirish Moodalbail 			    int64_t lifc_flags);
2066e91bba0SGirish Moodalbail static int		create_ipmp(const char *grname, int af,
2076e91bba0SGirish Moodalbail 			    const char *ifname, boolean_t implicit);
2086e91bba0SGirish Moodalbail static void		start_ipmp_daemon(void);
2096e91bba0SGirish Moodalbail static boolean_t 	ifaddr_up(ifaddrlistx_t *ifaddrp);
2106e91bba0SGirish Moodalbail static boolean_t 	ifaddr_down(ifaddrlistx_t *ifaddrp);
2112b24ab6bSSebastien Roy static dladm_status_t	ifconfig_dladm_open(const char *, datalink_class_t,
2126e91bba0SGirish Moodalbail 			    datalink_id_t *);
2136e91bba0SGirish Moodalbail static void		dladmerr_exit(dladm_status_t status, const char *str);
2146e91bba0SGirish Moodalbail static void		ipadmerr_exit(ipadm_status_t status, const char *str);
2156e91bba0SGirish Moodalbail static boolean_t	ifconfig_use_libipadm(int, const char *);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate #define	max(a, b)	((a) < (b) ? (b) : (a))
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate  * DHCP_EXIT_IF_FAILURE indicates that the operation failed, but if there
2217c478bd9Sstevel@tonic-gate  * are more interfaces to act on (i.e., ifconfig was invoked with -a), keep
2227c478bd9Sstevel@tonic-gate  * on going rather than exit with an error.
2237c478bd9Sstevel@tonic-gate  */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate #define	DHCP_EXIT_IF_FAILURE	-1
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate #define	NEXTARG		0xffffff	/* command takes an argument */
2287c478bd9Sstevel@tonic-gate #define	OPTARG		0xfffffe 	/* command takes an optional argument */
2297c478bd9Sstevel@tonic-gate #define	AF_ANY		(-1)
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /* Refer to the comments in ifconfig() on the netmask "hack" */
2327c478bd9Sstevel@tonic-gate #define	NETMASK_CMD	"netmask"
2337c478bd9Sstevel@tonic-gate struct sockaddr_storage	g_netmask;
234dd7a6f5fSkcpoon enum { G_NETMASK_NIL, G_NETMASK_PENDING, G_NETMASK_SET }
235dd7a6f5fSkcpoon     g_netmask_set = G_NETMASK_NIL;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate struct	cmd {
2387c478bd9Sstevel@tonic-gate 	char		*c_name;
2397c478bd9Sstevel@tonic-gate 	int64_t		c_parameter;	/* NEXTARG means next argv */
2407c478bd9Sstevel@tonic-gate 	int		(*c_func)(char *, int64_t);
2417c478bd9Sstevel@tonic-gate 	int		c_abortonfail;	/* don't continue parsing args */
2427c478bd9Sstevel@tonic-gate 					/* for the current interface */
2437c478bd9Sstevel@tonic-gate 	int	c_af;			/* address family restrictions */
2447c478bd9Sstevel@tonic-gate } cmds[] = {
2457c478bd9Sstevel@tonic-gate 	{ "up",		IFF_UP,		setifflags,	0,	AF_ANY },
2467c478bd9Sstevel@tonic-gate 	{ "down",	-IFF_UP,	setifflags,	0,	AF_ANY },
2477c478bd9Sstevel@tonic-gate 	{ "trailers",	-IFF_NOTRAILERS, setifflags,	0,	AF_ANY },
2487c478bd9Sstevel@tonic-gate 	{ "-trailers",	IFF_NOTRAILERS,	setifflags,	0,	AF_ANY },
2497c478bd9Sstevel@tonic-gate 	{ "arp",	-IFF_NOARP,	setifflags,	0,	AF_INET },
2507c478bd9Sstevel@tonic-gate 	{ "-arp",	IFF_NOARP,	setifflags,	0,	AF_INET },
2517c478bd9Sstevel@tonic-gate 	{ "router",	IFF_ROUTER,	setifflags,	0,	AF_ANY },
2527c478bd9Sstevel@tonic-gate 	{ "-router",	-IFF_ROUTER,	setifflags,	0,	AF_ANY },
2537c478bd9Sstevel@tonic-gate 	{ "private",	IFF_PRIVATE,	setifflags,	0,	AF_ANY },
2547c478bd9Sstevel@tonic-gate 	{ "-private",	-IFF_PRIVATE,	setifflags,	0,	AF_ANY },
2557c478bd9Sstevel@tonic-gate 	{ "xmit",	-IFF_NOXMIT,	setifflags,	0,	AF_ANY },
2567c478bd9Sstevel@tonic-gate 	{ "-xmit",	IFF_NOXMIT,	setifflags,	0,	AF_ANY },
2577c478bd9Sstevel@tonic-gate 	{ "-nud",	IFF_NONUD,	setifflags,	0,	AF_INET6 },
2587c478bd9Sstevel@tonic-gate 	{ "nud",	-IFF_NONUD,	setifflags,	0,	AF_INET6 },
2597c478bd9Sstevel@tonic-gate 	{ "anycast",	IFF_ANYCAST,	setifflags,	0,	AF_ANY },
2607c478bd9Sstevel@tonic-gate 	{ "-anycast",	-IFF_ANYCAST,	setifflags,	0,	AF_ANY },
2617c478bd9Sstevel@tonic-gate 	{ "local",	-IFF_NOLOCAL,	setifflags,	0,	AF_ANY },
2627c478bd9Sstevel@tonic-gate 	{ "-local",	IFF_NOLOCAL,	setifflags,	0,	AF_ANY },
2637c478bd9Sstevel@tonic-gate 	{ "deprecated",	IFF_DEPRECATED,	setifflags,	0,	AF_ANY },
2647c478bd9Sstevel@tonic-gate 	{ "-deprecated", -IFF_DEPRECATED, setifflags,	0,	AF_ANY },
2657c478bd9Sstevel@tonic-gate 	{ "preferred",	IFF_PREFERRED,	setifflags,	0,	AF_INET6 },
2667c478bd9Sstevel@tonic-gate 	{ "-preferred",	-IFF_PREFERRED,	setifflags,	0,	AF_INET6 },
2677c478bd9Sstevel@tonic-gate 	{ "debug",	0,		setdebugflag,	0,	AF_ANY },
2687c478bd9Sstevel@tonic-gate 	{ "verbose",	0,		setverboseflag,	0,	AF_ANY },
2697c478bd9Sstevel@tonic-gate 	{ NETMASK_CMD,	NEXTARG,	setifnetmask,	0,	AF_INET },
2707c478bd9Sstevel@tonic-gate 	{ "metric",	NEXTARG,	setifmetric,	0,	AF_ANY },
2717c478bd9Sstevel@tonic-gate 	{ "mtu",	NEXTARG,	setifmtu,	0,	AF_ANY },
2727c478bd9Sstevel@tonic-gate 	{ "index",	NEXTARG,	setifindex,	0,	AF_ANY },
2737c478bd9Sstevel@tonic-gate 	{ "broadcast",	NEXTARG,	setifbroadaddr,	0,	AF_INET },
2747c478bd9Sstevel@tonic-gate 	{ "auto-revarp", 0,		setifrevarp,	1,	AF_INET },
275e11c3f44Smeem 	{ "ipmp",	0,		inetipmp,	1,	AF_ANY },
2767c478bd9Sstevel@tonic-gate 	{ "plumb",	0,		inetplumb,	1,	AF_ANY },
2777c478bd9Sstevel@tonic-gate 	{ "unplumb",	0,		inetunplumb,	0,	AF_ANY },
2787c478bd9Sstevel@tonic-gate 	{ "subnet",	NEXTARG,	setifsubnet,	0,	AF_ANY },
2797c478bd9Sstevel@tonic-gate 	{ "token",	NEXTARG,	setiftoken,	0,	AF_INET6 },
2807c478bd9Sstevel@tonic-gate 	{ "tsrc",	NEXTARG,	setiftsrc,	0,	AF_ANY },
2817c478bd9Sstevel@tonic-gate 	{ "tdst",	NEXTARG,	setiftdst,	0,	AF_ANY },
2827c478bd9Sstevel@tonic-gate 	{ "encr_auth_algs", NEXTARG,	set_tun_esp_auth_alg, 0, AF_ANY },
2837c478bd9Sstevel@tonic-gate 	{ "encr_algs",	NEXTARG,	set_tun_esp_encr_alg, 0, AF_ANY },
2847c478bd9Sstevel@tonic-gate 	{ "auth_algs",	NEXTARG,	set_tun_ah_alg,	0,	AF_ANY },
2857c478bd9Sstevel@tonic-gate 	{ "addif",	NEXTARG,	addif,		1,	AF_ANY },
2867c478bd9Sstevel@tonic-gate 	{ "removeif",	NEXTARG,	removeif,	1,	AF_ANY },
2877c478bd9Sstevel@tonic-gate 	{ "modlist",	0,		modlist,	1,	AF_ANY },
2887c478bd9Sstevel@tonic-gate 	{ "modinsert",	NEXTARG,	modinsert,	1,	AF_ANY },
2897c478bd9Sstevel@tonic-gate 	{ "modremove",	NEXTARG,	modremove,	1,	AF_ANY },
2907c478bd9Sstevel@tonic-gate 	{ "failover",	-IFF_NOFAILOVER, setifflags,	1,	AF_ANY },
2917c478bd9Sstevel@tonic-gate 	{ "-failover",	IFF_NOFAILOVER, setifflags,	1,	AF_ANY },
2927c478bd9Sstevel@tonic-gate 	{ "standby",	IFF_STANDBY,	setifflags,	1,	AF_ANY },
2937c478bd9Sstevel@tonic-gate 	{ "-standby",	-IFF_STANDBY,	setifflags,	1,	AF_ANY },
2947c478bd9Sstevel@tonic-gate 	{ "failed",	IFF_FAILED,	setifflags,	1,	AF_ANY },
2957c478bd9Sstevel@tonic-gate 	{ "-failed",	-IFF_FAILED,	setifflags,	1,	AF_ANY },
2967c478bd9Sstevel@tonic-gate 	{ "group",	NEXTARG,	setifgroupname,	1,	AF_ANY },
2977c478bd9Sstevel@tonic-gate 	{ "configinfo",	0,		configinfo,	1,	AF_ANY },
2987906a3e0Smeem 	{ "encaplimit",	NEXTARG,	set_tun_encap_limit,	0, AF_ANY },
2997906a3e0Smeem 	{ "-encaplimit", 0,		clr_tun_encap_limit,	0, AF_ANY },
3007906a3e0Smeem 	{ "thoplimit",	NEXTARG,	set_tun_hop_limit,	0, AF_ANY },
3017c478bd9Sstevel@tonic-gate 	{ "set",	NEXTARG,	setifaddr,	0,	AF_ANY },
3027c478bd9Sstevel@tonic-gate 	{ "destination", NEXTARG,	setifdstaddr,	0,	AF_ANY },
3037c478bd9Sstevel@tonic-gate 	{ "zone",	NEXTARG,	setzone,	0,	AF_ANY },
3047c478bd9Sstevel@tonic-gate 	{ "-zone",	0,		setzone,	0,	AF_ANY },
30545916cd2Sjpk 	{ "all-zones",	0,		setallzones,	0,	AF_ANY },
3067c478bd9Sstevel@tonic-gate 	{ "ether",	OPTARG,		setifether,	0,	AF_ANY },
3077c478bd9Sstevel@tonic-gate 	{ "usesrc",	NEXTARG,	setifsrc,	0,	AF_ANY },
308f7d61273Smeem 
309f7d61273Smeem 	/*
310f7d61273Smeem 	 * NOTE: any additions to this table must also be applied to ifparse
311f7d61273Smeem 	 *	(usr/src/cmd/cmd-inet/sbin/ifparse/ifparse.c)
312f7d61273Smeem 	 */
313f7d61273Smeem 
3147c478bd9Sstevel@tonic-gate 	{ 0,		0,		setifaddr,	0,	AF_ANY },
3157c478bd9Sstevel@tonic-gate 	{ 0,		0,		setifdstaddr,	0,	AF_ANY },
3167c478bd9Sstevel@tonic-gate 	{ 0,		0,		0,		0,	0 },
3177c478bd9Sstevel@tonic-gate };
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate typedef struct if_config_cmd {
3217c478bd9Sstevel@tonic-gate 	uint64_t	iff_flag;
322e11c3f44Smeem 	int		iff_af;
3237c478bd9Sstevel@tonic-gate 	char		*iff_name;
3247c478bd9Sstevel@tonic-gate } if_config_cmd_t;
3257c478bd9Sstevel@tonic-gate 
326e11c3f44Smeem /*
327e11c3f44Smeem  * NOTE: print_config_flags() processes this table in order, so we put "up"
328e11c3f44Smeem  * last so that we can be sure "-failover" will take effect first.  Otherwise,
329e11c3f44Smeem  * IPMP test addresses will erroneously migrate to the IPMP interface.
330e11c3f44Smeem  */
3317c478bd9Sstevel@tonic-gate static if_config_cmd_t	if_config_cmd_tbl[] = {
332e11c3f44Smeem 	{ IFF_NOTRAILERS,	AF_UNSPEC,	"-trailers"	},
333e11c3f44Smeem 	{ IFF_PRIVATE,		AF_UNSPEC,	"private"	},
334e11c3f44Smeem 	{ IFF_NOXMIT,		AF_UNSPEC,	"-xmit"		},
335e11c3f44Smeem 	{ IFF_ANYCAST,		AF_INET6,	"anycast"	},
336e11c3f44Smeem 	{ IFF_NOLOCAL,		AF_UNSPEC,	"-local"	},
337e11c3f44Smeem 	{ IFF_DEPRECATED,	AF_UNSPEC,	"deprecated"	},
338e11c3f44Smeem 	{ IFF_NOFAILOVER,	AF_UNSPEC,	"-failover"	},
339e11c3f44Smeem 	{ IFF_STANDBY,		AF_UNSPEC,	"standby"	},
340e11c3f44Smeem 	{ IFF_FAILED,		AF_UNSPEC,	"failed"	},
341e11c3f44Smeem 	{ IFF_PREFERRED,	AF_UNSPEC,	"preferred"	},
342e11c3f44Smeem 	{ IFF_NONUD,		AF_INET6,	"-nud"		},
343e11c3f44Smeem 	{ IFF_NOARP,		AF_INET,	"-arp"		},
344e11c3f44Smeem 	{ IFF_UP,		AF_UNSPEC, 	"up" 		},
345e11c3f44Smeem 	{ 0,			0,		NULL		},
3467c478bd9Sstevel@tonic-gate };
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate typedef struct ni {
3497c478bd9Sstevel@tonic-gate 	char		ni_name[LIFNAMSIZ];
3507c478bd9Sstevel@tonic-gate 	struct ni	*ni_next;
3517c478bd9Sstevel@tonic-gate } ni_t;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate static ni_t	*ni_list = NULL;
3547c478bd9Sstevel@tonic-gate static int	num_ni = 0;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate /* End defines and structure definitions for ifconfig -a plumb */
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /* Known address families */
3597c478bd9Sstevel@tonic-gate struct afswtch {
3607c478bd9Sstevel@tonic-gate 	char *af_name;
3617c478bd9Sstevel@tonic-gate 	short af_af;
3627c478bd9Sstevel@tonic-gate 	void (*af_status)();
3637c478bd9Sstevel@tonic-gate 	void (*af_getaddr)();
3647c478bd9Sstevel@tonic-gate 	void (*af_configinfo)();
3657c478bd9Sstevel@tonic-gate } afs[] = {
3667c478bd9Sstevel@tonic-gate 	{ "inet", AF_INET, in_status, in_getaddr, in_configinfo },
3677c478bd9Sstevel@tonic-gate 	{ "inet6", AF_INET6, in6_status, in6_getaddr, in6_configinfo },
3687c478bd9Sstevel@tonic-gate 	{ 0, 0,	0, 0, 0 }
3697c478bd9Sstevel@tonic-gate };
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate struct afswtch *afp;	/* the address family being set or asked about */
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate int
3767c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3777c478bd9Sstevel@tonic-gate {
3782b24ab6bSSebastien Roy 	int64_t		lifc_flags;
3792b24ab6bSSebastien Roy 	char		*default_ip_str;
3806e91bba0SGirish Moodalbail 	ipadm_status_t	istatus;
3817c478bd9Sstevel@tonic-gate 
3826e91bba0SGirish Moodalbail 	lifc_flags = LIFC_DEFAULT;
383e11c3f44Smeem 
3847c478bd9Sstevel@tonic-gate 	if (argc < 2) {
385f7c14501SDaniil Lunev 		(void) strncpy(name, "-a", sizeof (name));
386f7c14501SDaniil Lunev 	} else {
387f7c14501SDaniil Lunev 		argc--, argv++;
388f7c14501SDaniil Lunev 		if (strlen(*argv) > sizeof (name) - 1) {
389f7c14501SDaniil Lunev 			(void) fprintf(stderr, "%s: interface name too long\n",
390f7c14501SDaniil Lunev 			    *argv);
391f7c14501SDaniil Lunev 			exit(1);
392f7c14501SDaniil Lunev 		}
393f7c14501SDaniil Lunev 		(void) strncpy(name, *argv, sizeof (name));
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 	name[sizeof (name) - 1] = '\0';
3967c478bd9Sstevel@tonic-gate 	(void) strncpy(origname, name, sizeof (origname));	/* For addif */
3977c478bd9Sstevel@tonic-gate 	default_ip_str = NULL;
3987c478bd9Sstevel@tonic-gate 	v4compat = get_compat_flag(&default_ip_str);
3997c478bd9Sstevel@tonic-gate 	if (v4compat == DEFAULT_PROT_BAD_VALUE) {
4007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4017c478bd9Sstevel@tonic-gate 		    "ifconfig: %s: Bad value for %s in %s\n", default_ip_str,
4027c478bd9Sstevel@tonic-gate 		    DEFAULT_IP, INET_DEFAULT_FILE);
4037c478bd9Sstevel@tonic-gate 		free(default_ip_str);
4047c478bd9Sstevel@tonic-gate 		exit(2);
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 	free(default_ip_str);
4077c478bd9Sstevel@tonic-gate 	argc--, argv++;
4087c478bd9Sstevel@tonic-gate 	if (argc > 0) {
4097c478bd9Sstevel@tonic-gate 		struct afswtch *myafp;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 		for (myafp = afp = afs; myafp->af_name; myafp++) {
4127c478bd9Sstevel@tonic-gate 			if (strcmp(myafp->af_name, *argv) == 0) {
4137c478bd9Sstevel@tonic-gate 				afp = myafp; argc--; argv++;
4147c478bd9Sstevel@tonic-gate 				break;
4157c478bd9Sstevel@tonic-gate 			}
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate 		af = lifr.lifr_addr.ss_family = afp->af_af;
4187c478bd9Sstevel@tonic-gate 		if (af == AF_INET6) {
4197c478bd9Sstevel@tonic-gate 			v4compat = 0;
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	s = socket(SOCKET_AF(af), SOCK_DGRAM, 0);
424e11c3f44Smeem 	s4 = socket(AF_INET, SOCK_DGRAM, 0);
425e11c3f44Smeem 	s6 = socket(AF_INET6, SOCK_DGRAM, 0);
426e11c3f44Smeem 	if (s == -1 || s4 == -1 || s6 == -1)
4277c478bd9Sstevel@tonic-gate 		Perror0_exit("socket");
4286e91bba0SGirish Moodalbail 	/*
4296e91bba0SGirish Moodalbail 	 * Open the global libipadm handle. The flag IPH_LEGACY has to
4306e91bba0SGirish Moodalbail 	 * be specified to indicate that logical interface names will
4316e91bba0SGirish Moodalbail 	 * be used during interface creation and address creation.
4326e91bba0SGirish Moodalbail 	 */
4336e91bba0SGirish Moodalbail 	if ((istatus = ipadm_open(&iph, IPH_LEGACY)) != IPADM_SUCCESS)
4346e91bba0SGirish Moodalbail 		ipadmerr_exit(istatus, "unable to open handle to libipadm");
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	/*
4377c478bd9Sstevel@tonic-gate 	 * Special interface names is any combination of these flags.
4387c478bd9Sstevel@tonic-gate 	 * Note that due to the ifconfig syntax they have to be combined
4397c478bd9Sstevel@tonic-gate 	 * as a single '-' option.
4407c478bd9Sstevel@tonic-gate 	 *	-a	All interfaces
4417c478bd9Sstevel@tonic-gate 	 *	-u	"up" interfaces
4427c478bd9Sstevel@tonic-gate 	 *	-d	"down" interfaces
4437c478bd9Sstevel@tonic-gate 	 *	-D	Interfaces not controlled by DHCP
4447c478bd9Sstevel@tonic-gate 	 *	-4	IPv4 interfaces
4457c478bd9Sstevel@tonic-gate 	 *	-6	IPv6 interfaces
4467c478bd9Sstevel@tonic-gate 	 *	-X	Turn on debug (not documented)
4477c478bd9Sstevel@tonic-gate 	 *	-v	Turn on verbose
4487c478bd9Sstevel@tonic-gate 	 *	-Z	Only interfaces in caller's zone
4497c478bd9Sstevel@tonic-gate 	 */
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (name[0] == '-') {
4527c478bd9Sstevel@tonic-gate 		/* One or more options */
4537c478bd9Sstevel@tonic-gate 		int64_t onflags = 0;
4547c478bd9Sstevel@tonic-gate 		int64_t offflags = 0;
4557c478bd9Sstevel@tonic-gate 		int c;
4567c478bd9Sstevel@tonic-gate 		char *av[2] = { "ifconfig", name };
4577c478bd9Sstevel@tonic-gate 
458f7c14501SDaniil Lunev 		while ((c = getopt(2, av, "audhDXZ46v")) != -1) {
4597c478bd9Sstevel@tonic-gate 			switch ((char)c) {
4607c478bd9Sstevel@tonic-gate 			case 'a':
4617c478bd9Sstevel@tonic-gate 				all = 1;
4627c478bd9Sstevel@tonic-gate 				break;
4637c478bd9Sstevel@tonic-gate 			case 'u':
4647c478bd9Sstevel@tonic-gate 				onflags |= IFF_UP;
4657c478bd9Sstevel@tonic-gate 				break;
4667c478bd9Sstevel@tonic-gate 			case 'd':
4677c478bd9Sstevel@tonic-gate 				offflags |= IFF_UP;
4687c478bd9Sstevel@tonic-gate 				break;
4697c478bd9Sstevel@tonic-gate 			case 'D':
4707c478bd9Sstevel@tonic-gate 				offflags |= IFF_DHCPRUNNING;
4717c478bd9Sstevel@tonic-gate 				break;
4727c478bd9Sstevel@tonic-gate 			case 'X':
4737c478bd9Sstevel@tonic-gate 				debug += 3;
4747c478bd9Sstevel@tonic-gate 				break;
4757c478bd9Sstevel@tonic-gate 			case 'Z':
4767c478bd9Sstevel@tonic-gate 				lifc_flags &= ~LIFC_ALLZONES;
4777c478bd9Sstevel@tonic-gate 				break;
4787c478bd9Sstevel@tonic-gate 			case '4':
4797c478bd9Sstevel@tonic-gate 				/*
4807c478bd9Sstevel@tonic-gate 				 * -4 is not a compatable flag, therefore
4817c478bd9Sstevel@tonic-gate 				 * we assume they want v4compat turned off
4827c478bd9Sstevel@tonic-gate 				 */
4837c478bd9Sstevel@tonic-gate 				v4compat = 0;
4847c478bd9Sstevel@tonic-gate 				onflags |= IFF_IPV4;
4857c478bd9Sstevel@tonic-gate 				break;
4867c478bd9Sstevel@tonic-gate 			case '6':
4877c478bd9Sstevel@tonic-gate 				/*
4887c478bd9Sstevel@tonic-gate 				 * If they want IPv6, well then we'll assume
4897c478bd9Sstevel@tonic-gate 				 * they don't want IPv4 compat
4907c478bd9Sstevel@tonic-gate 				 */
4917c478bd9Sstevel@tonic-gate 				v4compat = 0;
4927c478bd9Sstevel@tonic-gate 				onflags |= IFF_IPV6;
4937c478bd9Sstevel@tonic-gate 				break;
4947c478bd9Sstevel@tonic-gate 			case 'v':
4957c478bd9Sstevel@tonic-gate 				verbose = 1;
4967c478bd9Sstevel@tonic-gate 				break;
497f7c14501SDaniil Lunev 			case 'h':
4987c478bd9Sstevel@tonic-gate 			case '?':
4997c478bd9Sstevel@tonic-gate 				usage();
5007c478bd9Sstevel@tonic-gate 				exit(1);
5017c478bd9Sstevel@tonic-gate 			}
5027c478bd9Sstevel@tonic-gate 		}
5037c478bd9Sstevel@tonic-gate 		if (!all) {
5047c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
505c7e4935fSss 			    "ifconfig: %s: no such interface\n", name);
5067c478bd9Sstevel@tonic-gate 			exit(1);
5077c478bd9Sstevel@tonic-gate 		}
5086e91bba0SGirish Moodalbail 		foreachinterface(argc, argv, af, onflags, offflags,
5097c478bd9Sstevel@tonic-gate 		    lifc_flags);
5107c478bd9Sstevel@tonic-gate 	} else {
5116e91bba0SGirish Moodalbail 		ifconfig(argc, argv, af, NULL);
5127c478bd9Sstevel@tonic-gate 	}
5136e91bba0SGirish Moodalbail 	ipadm_close(iph);
5147c478bd9Sstevel@tonic-gate 	return (0);
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate /*
5186e91bba0SGirish Moodalbail  * For each interface, call ifconfig(argc, argv, af, ifa).
5197c478bd9Sstevel@tonic-gate  * Only call function if onflags and offflags are set or clear, respectively,
5207c478bd9Sstevel@tonic-gate  * in the interfaces flags field.
5217c478bd9Sstevel@tonic-gate  */
5227c478bd9Sstevel@tonic-gate static void
5236e91bba0SGirish Moodalbail foreachinterface(int argc, char *argv[], int af,
5247c478bd9Sstevel@tonic-gate     int64_t onflags, int64_t offflags, int64_t lifc_flags)
5257c478bd9Sstevel@tonic-gate {
5266e91bba0SGirish Moodalbail 	ipadm_addr_info_t *ainfo, *ainfop;
5276e91bba0SGirish Moodalbail 	struct ifaddrs *ifa;
5286e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	/*
5317c478bd9Sstevel@tonic-gate 	 * Special case:
5322b24ab6bSSebastien Roy 	 * ifconfig -a plumb should find all network interfaces in the current
5332b24ab6bSSebastien Roy 	 * zone.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	if (argc > 0 && (strcmp(*argv, "plumb") == 0)) {
5366e91bba0SGirish Moodalbail 		plumball(argc, argv, onflags, offflags, lifc_flags);
5376e91bba0SGirish Moodalbail 		return;
5387c478bd9Sstevel@tonic-gate 	}
5396e91bba0SGirish Moodalbail 	/* Get all addresses in kernel including addresses that are zero. */
5406e91bba0SGirish Moodalbail 	istatus = ipadm_addr_info(iph, NULL, &ainfo, IPADM_OPT_ZEROADDR,
5416e91bba0SGirish Moodalbail 	    lifc_flags);
5426e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS)
5436e91bba0SGirish Moodalbail 		ipadmerr_exit(istatus, "could not get addresses from kernel");
5447c478bd9Sstevel@tonic-gate 
5456e91bba0SGirish Moodalbail 	/*
5466e91bba0SGirish Moodalbail 	 * For each logical interface, call ifconfig() with the
5476e91bba0SGirish Moodalbail 	 * given arguments.
5486e91bba0SGirish Moodalbail 	 */
5496e91bba0SGirish Moodalbail 	for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop)) {
5506e91bba0SGirish Moodalbail 		if (ainfop->ia_state == IFA_DISABLED)
5516e91bba0SGirish Moodalbail 			continue;
5526e91bba0SGirish Moodalbail 		ifa = &ainfop->ia_ifa;
5537c478bd9Sstevel@tonic-gate 		if (onflags || offflags) {
5546e91bba0SGirish Moodalbail 			if ((ifa->ifa_flags & onflags) != onflags)
5557c478bd9Sstevel@tonic-gate 				continue;
5566e91bba0SGirish Moodalbail 			if ((~ifa->ifa_flags & offflags) != offflags)
5577c478bd9Sstevel@tonic-gate 				continue;
5587c478bd9Sstevel@tonic-gate 		}
55964639aafSDarren Reed 		s = (ifa->ifa_addr->sa_family == AF_INET ? s4 : s6);
5606e91bba0SGirish Moodalbail 		(void) strncpy(name, ifa->ifa_name, sizeof (name));
5616e91bba0SGirish Moodalbail 		(void) strncpy(origname, name, sizeof (origname));
5626e91bba0SGirish Moodalbail 		ifconfig(argc, argv, af, ifa);
5636e91bba0SGirish Moodalbail 	}
5646e91bba0SGirish Moodalbail 	ipadm_free_addr_info(ainfo);
5656e91bba0SGirish Moodalbail }
5667c478bd9Sstevel@tonic-gate 
5676e91bba0SGirish Moodalbail /*
5686e91bba0SGirish Moodalbail  * Used for `ifconfig -a plumb'. Finds all datalinks and plumbs the interface.
5696e91bba0SGirish Moodalbail  */
5706e91bba0SGirish Moodalbail static void
5716e91bba0SGirish Moodalbail plumball(int argc, char *argv[], int64_t onflags, int64_t offflags,
5726e91bba0SGirish Moodalbail     int64_t lifc_flags)
5736e91bba0SGirish Moodalbail {
5746e91bba0SGirish Moodalbail 	int n;
5756e91bba0SGirish Moodalbail 	struct lifreq *lifrp;
5766e91bba0SGirish Moodalbail 	struct lifconf lifc;
5776e91bba0SGirish Moodalbail 	char *buf;
5787c478bd9Sstevel@tonic-gate 
5796e91bba0SGirish Moodalbail 	if (onflags != 0 || offflags != 0) {
5806e91bba0SGirish Moodalbail 		(void) fprintf(stderr, "ifconfig: invalid syntax used to "
5816e91bba0SGirish Moodalbail 		    "plumb all interfaces.\n");
5826e91bba0SGirish Moodalbail 		exit(1);
5836e91bba0SGirish Moodalbail 	}
5846e91bba0SGirish Moodalbail 
5856e91bba0SGirish Moodalbail 	if (find_all_interfaces(&lifc, &buf, lifc_flags) != 0 ||
5866e91bba0SGirish Moodalbail 	    lifc.lifc_len == 0)
5876e91bba0SGirish Moodalbail 		return;
5887c478bd9Sstevel@tonic-gate 
5896e91bba0SGirish Moodalbail 	lifrp = lifc.lifc_req;
5906e91bba0SGirish Moodalbail 	for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) {
5917c478bd9Sstevel@tonic-gate 		/*
5927c478bd9Sstevel@tonic-gate 		 * Reset global state
5937c478bd9Sstevel@tonic-gate 		 * setaddr: Used by parser to tear apart source and dest
5947c478bd9Sstevel@tonic-gate 		 * name and origname contain the name of the 'current'
5957c478bd9Sstevel@tonic-gate 		 * interface.
5967c478bd9Sstevel@tonic-gate 		 */
5977c478bd9Sstevel@tonic-gate 		setaddr = 0;
5987c478bd9Sstevel@tonic-gate 		(void) strncpy(name, lifrp->lifr_name, sizeof (name));
5997c478bd9Sstevel@tonic-gate 		(void) strncpy(origname, name, sizeof (origname));
6006e91bba0SGirish Moodalbail 		ifconfig(argc, argv, af, NULL);
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate /*
6056e91bba0SGirish Moodalbail  * Parses the interface name and the command in argv[]. Calls the
6066e91bba0SGirish Moodalbail  * appropriate callback function for the given command from `cmds[]'
6076e91bba0SGirish Moodalbail  * table.
6086e91bba0SGirish Moodalbail  * If there is no command specified, it prints all addresses.
6097c478bd9Sstevel@tonic-gate  */
6107c478bd9Sstevel@tonic-gate static void
6116e91bba0SGirish Moodalbail ifconfig(int argc, char *argv[], int af, struct ifaddrs *ifa)
6127c478bd9Sstevel@tonic-gate {
6137c478bd9Sstevel@tonic-gate 	static boolean_t scan_netmask = _B_FALSE;
6147c478bd9Sstevel@tonic-gate 	int ret;
6156e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
6166e91bba0SGirish Moodalbail 	struct lifreq lifr;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (argc == 0) {
6197c478bd9Sstevel@tonic-gate 		status();
6207c478bd9Sstevel@tonic-gate 		return;
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	if (strcmp(*argv, "auto-dhcp") == 0 || strcmp(*argv, "dhcp") == 0) {
624d04ccbb3Scarlsonj 		/*
625d04ccbb3Scarlsonj 		 * Some errors are ignored in the case where more than one
626d04ccbb3Scarlsonj 		 * interface is being operated on.
627d04ccbb3Scarlsonj 		 */
628d04ccbb3Scarlsonj 		ret = setifdhcp("ifconfig", name, argc, argv);
629d04ccbb3Scarlsonj 		if (ret == DHCP_EXIT_IF_FAILURE) {
630d04ccbb3Scarlsonj 			if (!all)
631d04ccbb3Scarlsonj 				exit(DHCP_EXIT_FAILURE);
632d04ccbb3Scarlsonj 		} else if (ret != DHCP_EXIT_SUCCESS) {
633d04ccbb3Scarlsonj 			exit(ret);
634d04ccbb3Scarlsonj 		}
6357c478bd9Sstevel@tonic-gate 		return;
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	/*
6397c478bd9Sstevel@tonic-gate 	 * The following is a "hack" to get around the existing interface
6407c478bd9Sstevel@tonic-gate 	 * setting mechanism.  Currently, each interface attribute,
6417c478bd9Sstevel@tonic-gate 	 * such as address, netmask, broadcast, ... is set separately.  But
6427c478bd9Sstevel@tonic-gate 	 * sometimes two or more attributes must be set together.  For
6437c478bd9Sstevel@tonic-gate 	 * example, setting an address without a netmask does not make sense.
6447c478bd9Sstevel@tonic-gate 	 * Yet they can be set separately for IPv4 address using the current
6457c478bd9Sstevel@tonic-gate 	 * ifconfig(1M) syntax.  The kernel then "infers" the correct netmask
6467c478bd9Sstevel@tonic-gate 	 * using the deprecated "IP address classes."  This is simply not
6477c478bd9Sstevel@tonic-gate 	 * correct.
6487c478bd9Sstevel@tonic-gate 	 *
6497c478bd9Sstevel@tonic-gate 	 * The "hack" below is to go thru the whole command list looking for
6507c478bd9Sstevel@tonic-gate 	 * the netmask command first.  Then use this netmask to set the
6517c478bd9Sstevel@tonic-gate 	 * address.  This does not provide an extensible way to accommodate
6527c478bd9Sstevel@tonic-gate 	 * future need for setting more than one attributes together.
6537c478bd9Sstevel@tonic-gate 	 *
654dd7a6f5fSkcpoon 	 * Note that if the "netmask" command argument is a "+", we need
655dd7a6f5fSkcpoon 	 * to save this info and do the query after we know the address to
656dd7a6f5fSkcpoon 	 * be set.  The reason is that if "addif" is used, the working
657dd7a6f5fSkcpoon 	 * interface name will be changed later when the logical interface
658dd7a6f5fSkcpoon 	 * is created.  In in_getmask(), if an address is not provided,
659dd7a6f5fSkcpoon 	 * it will use the working interface's address to do the query.
660dd7a6f5fSkcpoon 	 * It will be wrong now as we don't know the logical interface's name.
661dd7a6f5fSkcpoon 	 *
6627c478bd9Sstevel@tonic-gate 	 * ifconfig(1M) is too overloaded and the code is so convoluted
6637c478bd9Sstevel@tonic-gate 	 * that it is "safer" not to re-architect the code to fix the above
6647c478bd9Sstevel@tonic-gate 	 * issue, hence this "hack."  We may be better off to have a new
6657c478bd9Sstevel@tonic-gate 	 * command with better syntax for configuring network interface
6667c478bd9Sstevel@tonic-gate 	 * parameters...
6677c478bd9Sstevel@tonic-gate 	 */
6687c478bd9Sstevel@tonic-gate 	if (!scan_netmask && afp->af_af == AF_INET) {
6697c478bd9Sstevel@tonic-gate 		int	largc;
6707c478bd9Sstevel@tonic-gate 		char	**largv;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 		/* Only go thru the command list once to find the netmask. */
6737c478bd9Sstevel@tonic-gate 		scan_netmask = _B_TRUE;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		/*
6767c478bd9Sstevel@tonic-gate 		 * Currently, if multiple netmask commands are specified, the
6777c478bd9Sstevel@tonic-gate 		 * last one will be used as the final netmask.  So we need
6787c478bd9Sstevel@tonic-gate 		 * to scan the whole list to preserve this behavior.
6797c478bd9Sstevel@tonic-gate 		 */
6807c478bd9Sstevel@tonic-gate 		for (largc = argc, largv = argv; largc > 0; largc--, largv++) {
6817c478bd9Sstevel@tonic-gate 			if (strcmp(*largv, NETMASK_CMD) == 0) {
6827c478bd9Sstevel@tonic-gate 				if (--largc == 0)
6837c478bd9Sstevel@tonic-gate 					break;
6847c478bd9Sstevel@tonic-gate 				largv++;
6857c478bd9Sstevel@tonic-gate 				if (strcmp(*largv, "+") == 0) {
686dd7a6f5fSkcpoon 					g_netmask_set = G_NETMASK_PENDING;
6877c478bd9Sstevel@tonic-gate 				} else {
6887c478bd9Sstevel@tonic-gate 					in_getaddr(*largv, (struct sockaddr *)
6897c478bd9Sstevel@tonic-gate 					    &g_netmask, NULL);
690dd7a6f5fSkcpoon 					g_netmask_set = G_NETMASK_SET;
6917c478bd9Sstevel@tonic-gate 				}
6927c478bd9Sstevel@tonic-gate 				/* Continue the scan. */
6937c478bd9Sstevel@tonic-gate 			}
6947c478bd9Sstevel@tonic-gate 		}
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	while (argc > 0) {
6987c478bd9Sstevel@tonic-gate 		struct cmd *p;
6997c478bd9Sstevel@tonic-gate 		boolean_t found_cmd;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		if (debug)
7027c478bd9Sstevel@tonic-gate 			(void) printf("ifconfig: argv %s\n", *argv);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 		found_cmd = _B_FALSE;
7057c478bd9Sstevel@tonic-gate 		for (p = cmds; p->c_func; p++) {
7067c478bd9Sstevel@tonic-gate 			if (p->c_name) {
7077c478bd9Sstevel@tonic-gate 				if (strcmp(*argv, p->c_name) == 0) {
7087c478bd9Sstevel@tonic-gate 					/*
7097c478bd9Sstevel@tonic-gate 					 * indicate that the command was
7107c478bd9Sstevel@tonic-gate 					 * found and check to see if
7117c478bd9Sstevel@tonic-gate 					 * the address family is valid
7127c478bd9Sstevel@tonic-gate 					 */
7137c478bd9Sstevel@tonic-gate 					found_cmd = _B_TRUE;
7147c478bd9Sstevel@tonic-gate 					if (p->c_af == AF_ANY ||
7157c478bd9Sstevel@tonic-gate 					    af == p->c_af)
7167c478bd9Sstevel@tonic-gate 						break;
7177c478bd9Sstevel@tonic-gate 				}
7187c478bd9Sstevel@tonic-gate 			} else {
7197c478bd9Sstevel@tonic-gate 				if (p->c_af == AF_ANY ||
7207c478bd9Sstevel@tonic-gate 				    af == p->c_af)
7217c478bd9Sstevel@tonic-gate 					break;
7227c478bd9Sstevel@tonic-gate 			}
7237c478bd9Sstevel@tonic-gate 		}
7247c478bd9Sstevel@tonic-gate 		/*
7257c478bd9Sstevel@tonic-gate 		 * If we found the keyword, but the address family
7267c478bd9Sstevel@tonic-gate 		 * did not match spit out an error
7277c478bd9Sstevel@tonic-gate 		 */
7287c478bd9Sstevel@tonic-gate 		if (found_cmd && p->c_name == 0) {
7297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: Operation %s not"
7307c478bd9Sstevel@tonic-gate 			    " supported for %s\n", *argv, afp->af_name);
7317c478bd9Sstevel@tonic-gate 			exit(1);
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 		/*
7347c478bd9Sstevel@tonic-gate 		 * else (no keyword found), we assume it's an address
7357c478bd9Sstevel@tonic-gate 		 * of some sort
7367c478bd9Sstevel@tonic-gate 		 */
7376e91bba0SGirish Moodalbail 		if (setaddr && ipaddr != NULL) {
7386e91bba0SGirish Moodalbail 			/*
7396e91bba0SGirish Moodalbail 			 * We must have already filled in a source address in
7406e91bba0SGirish Moodalbail 			 * `ipaddr' and we now got a destination address.
7416e91bba0SGirish Moodalbail 			 * Fill it in `ipaddr' and call libipadm to create
7426e91bba0SGirish Moodalbail 			 * the static address.
7436e91bba0SGirish Moodalbail 			 */
7446e91bba0SGirish Moodalbail 			if (p->c_name == 0) {
7456e91bba0SGirish Moodalbail 				istatus = ipadm_set_dst_addr(ipaddr, *argv,
7466e91bba0SGirish Moodalbail 				    (p->c_af == AF_ANY ? AF_UNSPEC : af));
7476e91bba0SGirish Moodalbail 				if (istatus != IPADM_SUCCESS) {
7486e91bba0SGirish Moodalbail 					ipadmerr_exit(istatus, "could not "
7496e91bba0SGirish Moodalbail 					    "set destination address");
7506e91bba0SGirish Moodalbail 				}
7516e91bba0SGirish Moodalbail 				/*
7526e91bba0SGirish Moodalbail 				 * finished processing dstaddr, so reset setaddr
7536e91bba0SGirish Moodalbail 				 */
7546e91bba0SGirish Moodalbail 				setaddr = 0;
7556e91bba0SGirish Moodalbail 			}
7566e91bba0SGirish Moodalbail 			/*
7576e91bba0SGirish Moodalbail 			 * Both source and destination address are in `ipaddr'.
7586e91bba0SGirish Moodalbail 			 * Add the address by calling libipadm.
7596e91bba0SGirish Moodalbail 			 */
7606e91bba0SGirish Moodalbail 			istatus = ipadm_create_addr(iph, ipaddr,
7616e91bba0SGirish Moodalbail 			    IPADM_OPT_ACTIVE);
7626e91bba0SGirish Moodalbail 			if (istatus != IPADM_SUCCESS)
7636e91bba0SGirish Moodalbail 				goto createfailed;
7646e91bba0SGirish Moodalbail 			ipadm_destroy_addrobj(ipaddr);
7656e91bba0SGirish Moodalbail 			ipaddr = NULL;
7666e91bba0SGirish Moodalbail 			setaddr_done = _B_TRUE;
7676e91bba0SGirish Moodalbail 			if (p->c_name == 0) {
7686e91bba0SGirish Moodalbail 				/* move parser along */
7696e91bba0SGirish Moodalbail 				argc--, argv++;
7706e91bba0SGirish Moodalbail 				continue;
7716e91bba0SGirish Moodalbail 			}
7726e91bba0SGirish Moodalbail 		}
7736e91bba0SGirish Moodalbail 		if (p->c_name == 0 && setaddr_done) {
7746e91bba0SGirish Moodalbail 			/*
7756e91bba0SGirish Moodalbail 			 * catch odd commands like
7766e91bba0SGirish Moodalbail 			 * "ifconfig <intf> addr1 addr2 addr3 addr4 up"
7776e91bba0SGirish Moodalbail 			 */
7786e91bba0SGirish Moodalbail 			(void) fprintf(stderr, "%s",
7796e91bba0SGirish Moodalbail 			    "ifconfig: cannot configure more than two "
7806e91bba0SGirish Moodalbail 			    "addresses in one command\n");
7816e91bba0SGirish Moodalbail 			exit(1);
7826e91bba0SGirish Moodalbail 		}
7837c478bd9Sstevel@tonic-gate 		if (p->c_func) {
7847c478bd9Sstevel@tonic-gate 			if (p->c_af == AF_INET6) {
7857c478bd9Sstevel@tonic-gate 				v4compat = 0;
7867c478bd9Sstevel@tonic-gate 			}
7877c478bd9Sstevel@tonic-gate 			if (p->c_parameter == NEXTARG ||
7887c478bd9Sstevel@tonic-gate 			    p->c_parameter == OPTARG) {
7897c478bd9Sstevel@tonic-gate 				argc--, argv++;
7907c478bd9Sstevel@tonic-gate 				if (argc == 0 && p->c_parameter == NEXTARG) {
7917c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
7927c478bd9Sstevel@tonic-gate 					    "ifconfig: no argument for %s\n",
7937c478bd9Sstevel@tonic-gate 					    p->c_name);
7947c478bd9Sstevel@tonic-gate 					exit(1);
7957c478bd9Sstevel@tonic-gate 				}
7967c478bd9Sstevel@tonic-gate 			}
7977c478bd9Sstevel@tonic-gate 			/*
7987c478bd9Sstevel@tonic-gate 			 *	Call the function if:
7997c478bd9Sstevel@tonic-gate 			 *
8007c478bd9Sstevel@tonic-gate 			 *		there's no address family
8017c478bd9Sstevel@tonic-gate 			 *		restriction
8027c478bd9Sstevel@tonic-gate 			 *	OR
8037c478bd9Sstevel@tonic-gate 			 *		we don't know the address yet
8047c478bd9Sstevel@tonic-gate 			 *		(because we were called from
8057c478bd9Sstevel@tonic-gate 			 *		main)
8067c478bd9Sstevel@tonic-gate 			 *	OR
8077c478bd9Sstevel@tonic-gate 			 *		there is a restriction AND
8087c478bd9Sstevel@tonic-gate 			 *		the address families match
8097c478bd9Sstevel@tonic-gate 			 */
8107c478bd9Sstevel@tonic-gate 			if ((p->c_af == AF_ANY)	||
8116e91bba0SGirish Moodalbail 			    (ifa == NULL) ||
81264639aafSDarren Reed 			    (ifa->ifa_addr->sa_family == p->c_af)) {
8137c478bd9Sstevel@tonic-gate 				ret = (*p->c_func)(*argv, p->c_parameter);
8147c478bd9Sstevel@tonic-gate 				/*
8157c478bd9Sstevel@tonic-gate 				 *	If c_func failed and we should
8167c478bd9Sstevel@tonic-gate 				 *	abort processing for this
8177c478bd9Sstevel@tonic-gate 				 *	interface on failure, return
8187c478bd9Sstevel@tonic-gate 				 *	now rather than going on to
8197c478bd9Sstevel@tonic-gate 				 *	process other commands for
8207c478bd9Sstevel@tonic-gate 				 *	the same interface.
8217c478bd9Sstevel@tonic-gate 				 */
8227c478bd9Sstevel@tonic-gate 				if (ret != 0 && p->c_abortonfail)
8237c478bd9Sstevel@tonic-gate 					return;
8247c478bd9Sstevel@tonic-gate 			}
8257c478bd9Sstevel@tonic-gate 		}
8267c478bd9Sstevel@tonic-gate 		argc--, argv++;
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 
8296e91bba0SGirish Moodalbail 	if (setaddr && ipaddr != NULL) {
8306e91bba0SGirish Moodalbail 		/*
8316e91bba0SGirish Moodalbail 		 * Only the source address was provided, which was already
8326e91bba0SGirish Moodalbail 		 * set in `ipaddr'. Add the address by calling libipadm.
8336e91bba0SGirish Moodalbail 		 */
8346e91bba0SGirish Moodalbail 		istatus = ipadm_create_addr(iph, ipaddr, IPADM_OPT_ACTIVE);
8356e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
8366e91bba0SGirish Moodalbail 			goto createfailed;
8376e91bba0SGirish Moodalbail 		ipadm_destroy_addrobj(ipaddr);
8386e91bba0SGirish Moodalbail 		ipaddr = NULL;
8396e91bba0SGirish Moodalbail 		setaddr_done = _B_TRUE;
8406e91bba0SGirish Moodalbail 	}
8416e91bba0SGirish Moodalbail 
8427c478bd9Sstevel@tonic-gate 	/* Check to see if there's a security hole in the tunnel setup. */
8432b24ab6bSSebastien Roy 	if (ipsec_policy_set && !ipsec_auth_covered) {
8442b24ab6bSSebastien Roy 		(void) fprintf(stderr, "ifconfig: WARNING: tunnel with only "
8452b24ab6bSSebastien Roy 		    "ESP and no authentication.\n");
8462b24ab6bSSebastien Roy 	}
8476e91bba0SGirish Moodalbail 	return;
8486e91bba0SGirish Moodalbail 
8496e91bba0SGirish Moodalbail createfailed:
8506e91bba0SGirish Moodalbail 	(void) fprintf(stderr, "ifconfig: could not create address:% s\n",
8516e91bba0SGirish Moodalbail 	    ipadm_status2str(istatus));
8526e91bba0SGirish Moodalbail 	/* Remove the newly created logical interface. */
8536e91bba0SGirish Moodalbail 	if (strcmp(name, origname) != 0) {
8546e91bba0SGirish Moodalbail 		assert(strchr(name, ':') != NULL);
8556e91bba0SGirish Moodalbail 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
8566e91bba0SGirish Moodalbail 		(void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
8576e91bba0SGirish Moodalbail 	}
8586e91bba0SGirish Moodalbail 	exit(1);
8597c478bd9Sstevel@tonic-gate }
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate /* ARGSUSED */
8627c478bd9Sstevel@tonic-gate static int
8637c478bd9Sstevel@tonic-gate setdebugflag(char *val, int64_t arg)
8647c478bd9Sstevel@tonic-gate {
8657c478bd9Sstevel@tonic-gate 	debug++;
8667c478bd9Sstevel@tonic-gate 	return (0);
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate /* ARGSUSED */
8707c478bd9Sstevel@tonic-gate static int
8717c478bd9Sstevel@tonic-gate setverboseflag(char *val, int64_t arg)
8727c478bd9Sstevel@tonic-gate {
8737c478bd9Sstevel@tonic-gate 	verbose++;
8747c478bd9Sstevel@tonic-gate 	return (0);
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate 
877dd7a6f5fSkcpoon /*
878dd7a6f5fSkcpoon  * This function fills in the given lifreq's lifr_addr field based on
879dd7a6f5fSkcpoon  * g_netmask_set.
880dd7a6f5fSkcpoon  */
881dd7a6f5fSkcpoon static void
882dd7a6f5fSkcpoon set_mask_lifreq(struct lifreq *lifr, struct sockaddr_storage *addr,
883dd7a6f5fSkcpoon     struct sockaddr_storage *mask)
884dd7a6f5fSkcpoon {
885dd7a6f5fSkcpoon 	assert(addr != NULL);
886dd7a6f5fSkcpoon 	assert(mask != NULL);
887dd7a6f5fSkcpoon 
888dd7a6f5fSkcpoon 	switch (g_netmask_set) {
889dd7a6f5fSkcpoon 	case G_NETMASK_SET:
890dd7a6f5fSkcpoon 		lifr->lifr_addr = g_netmask;
891dd7a6f5fSkcpoon 		break;
892dd7a6f5fSkcpoon 
893dd7a6f5fSkcpoon 	case G_NETMASK_PENDING:
894dd7a6f5fSkcpoon 		/*
895dd7a6f5fSkcpoon 		 * "+" is used as the argument to "netmask" command.  Query
896dd7a6f5fSkcpoon 		 * the database on the correct netmask based on the address to
897dd7a6f5fSkcpoon 		 * be set.
898dd7a6f5fSkcpoon 		 */
899dd7a6f5fSkcpoon 		assert(afp->af_af == AF_INET);
900dd7a6f5fSkcpoon 		g_netmask = *addr;
901dd7a6f5fSkcpoon 		if (!in_getmask((struct sockaddr_in *)&g_netmask, _B_TRUE)) {
902dd7a6f5fSkcpoon 			lifr->lifr_addr = *mask;
903dd7a6f5fSkcpoon 			g_netmask_set = G_NETMASK_NIL;
904dd7a6f5fSkcpoon 		} else {
905dd7a6f5fSkcpoon 			lifr->lifr_addr = g_netmask;
906dd7a6f5fSkcpoon 			g_netmask_set = G_NETMASK_SET;
907dd7a6f5fSkcpoon 		}
908dd7a6f5fSkcpoon 		break;
909dd7a6f5fSkcpoon 
910dd7a6f5fSkcpoon 	case G_NETMASK_NIL:
911dd7a6f5fSkcpoon 	default:
912dd7a6f5fSkcpoon 		lifr->lifr_addr = *mask;
913dd7a6f5fSkcpoon 		break;
914dd7a6f5fSkcpoon 	}
915dd7a6f5fSkcpoon }
916dd7a6f5fSkcpoon 
9177c478bd9Sstevel@tonic-gate /*
9187c478bd9Sstevel@tonic-gate  * Set the interface address. Handles <addr>, <addr>/<n> as well as /<n>
9197c478bd9Sstevel@tonic-gate  * syntax for setting the address, the address plus netmask, and just
9207c478bd9Sstevel@tonic-gate  * the netmask respectively.
9217c478bd9Sstevel@tonic-gate  */
9227c478bd9Sstevel@tonic-gate /* ARGSUSED */
9237c478bd9Sstevel@tonic-gate static int
9247c478bd9Sstevel@tonic-gate setifaddr(char *addr, int64_t param)
9257c478bd9Sstevel@tonic-gate {
9266e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
9277c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
9286e91bba0SGirish Moodalbail 	struct  lifreq lifr1;
9297c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage laddr;
9307c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage netmask;
9317c478bd9Sstevel@tonic-gate 	struct	sockaddr_in6 *sin6;
9327c478bd9Sstevel@tonic-gate 	struct	sockaddr_in *sin;
9337c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage sav_netmask;
9346e91bba0SGirish Moodalbail 	char cidraddr[BUFSIZ];
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	if (addr[0] == '/')
9377c478bd9Sstevel@tonic-gate 		return (setifprefixlen(addr, 0));
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, (struct sockaddr *)&laddr, &prefixlen);
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	(void) memset(&netmask, 0, sizeof (netmask));
9427c478bd9Sstevel@tonic-gate 	netmask.ss_family = afp->af_af;
9437c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
9447c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
9457c478bd9Sstevel@tonic-gate 		/* Nothing there - ok */
9467c478bd9Sstevel@tonic-gate 		break;
9477c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
9487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: Bad prefix length in %s\n",
9497c478bd9Sstevel@tonic-gate 		    addr);
9507c478bd9Sstevel@tonic-gate 		exit(1);
9517c478bd9Sstevel@tonic-gate 	default:
9527c478bd9Sstevel@tonic-gate 		if (afp->af_af == AF_INET6) {
9537c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&netmask;
9547906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
9557c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin6->sin6_addr)) {
9567c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
9577c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
9587c478bd9Sstevel@tonic-gate 				    prefixlen);
9597c478bd9Sstevel@tonic-gate 				exit(1);
9607c478bd9Sstevel@tonic-gate 			}
9617c478bd9Sstevel@tonic-gate 		} else {
9627c478bd9Sstevel@tonic-gate 			sin = (struct sockaddr_in *)&netmask;
9637906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IP_ABITS,
9647c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin->sin_addr)) {
9657c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
9667c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
9677c478bd9Sstevel@tonic-gate 				    prefixlen);
9687c478bd9Sstevel@tonic-gate 				exit(1);
9697c478bd9Sstevel@tonic-gate 			}
9707c478bd9Sstevel@tonic-gate 		}
9717c478bd9Sstevel@tonic-gate 		/*
9727c478bd9Sstevel@tonic-gate 		 * Just in case of funny setting of both prefix and netmask,
9737c478bd9Sstevel@tonic-gate 		 * prefix should override the netmask command.
9747c478bd9Sstevel@tonic-gate 		 */
975dd7a6f5fSkcpoon 		g_netmask_set = G_NETMASK_NIL;
9767c478bd9Sstevel@tonic-gate 		break;
9777c478bd9Sstevel@tonic-gate 	}
9786e91bba0SGirish Moodalbail 
9796e91bba0SGirish Moodalbail 	/*
9806e91bba0SGirish Moodalbail 	 * Check and see if any "netmask" command is used and perform the
9816e91bba0SGirish Moodalbail 	 * necessary operation.
9826e91bba0SGirish Moodalbail 	 */
9836e91bba0SGirish Moodalbail 	set_mask_lifreq(&lifr, &laddr, &netmask);
9846e91bba0SGirish Moodalbail 
9856e91bba0SGirish Moodalbail 	/* This check is temporary until libipadm supports IPMP interfaces. */
9866e91bba0SGirish Moodalbail 	if (ifconfig_use_libipadm(s, name)) {
987e6889d40SVasumathi Sundaram 		char	addrstr[INET6_ADDRSTRLEN];
988e6889d40SVasumathi Sundaram 
989e6889d40SVasumathi Sundaram 		if (af == AF_INET) {
990e6889d40SVasumathi Sundaram 			sin = (struct sockaddr_in *)&laddr;
991e6889d40SVasumathi Sundaram 			(void) inet_ntop(AF_INET, &sin->sin_addr, addrstr,
992e6889d40SVasumathi Sundaram 			    sizeof (addrstr));
993e6889d40SVasumathi Sundaram 		} else {
994e6889d40SVasumathi Sundaram 			sin6 = (struct sockaddr_in6 *)&laddr;
995e6889d40SVasumathi Sundaram 			(void) inet_ntop(AF_INET6, &sin6->sin6_addr, addrstr,
996e6889d40SVasumathi Sundaram 			    sizeof (addrstr));
997e6889d40SVasumathi Sundaram 		}
9986e91bba0SGirish Moodalbail 		istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name,
9996e91bba0SGirish Moodalbail 		    &ipaddr);
10006e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
10016e91bba0SGirish Moodalbail 			ipadmerr_exit(istatus, "setifaddr");
10026e91bba0SGirish Moodalbail 
10031214df40SGirish Moodalbail 		/*
10041214df40SGirish Moodalbail 		 * lifr.lifr_addr, which is updated by set_mask_lifreq()
10051214df40SGirish Moodalbail 		 * will contain the right mask to use.
10061214df40SGirish Moodalbail 		 */
100764639aafSDarren Reed 		prefixlen = mask2plen((struct sockaddr *)&lifr.lifr_addr);
10081214df40SGirish Moodalbail 		(void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d",
10091214df40SGirish Moodalbail 		    addrstr, prefixlen);
10101214df40SGirish Moodalbail 
10111214df40SGirish Moodalbail 		istatus = ipadm_set_addr(ipaddr, cidraddr, af);
10126e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
10136e91bba0SGirish Moodalbail 			ipadmerr_exit(istatus, "could not set address");
10146e91bba0SGirish Moodalbail 		/*
10156e91bba0SGirish Moodalbail 		 * let parser know we got a source.
10166e91bba0SGirish Moodalbail 		 * Next address, if given, should be dest
10176e91bba0SGirish Moodalbail 		 */
10186e91bba0SGirish Moodalbail 		setaddr++;
10196e91bba0SGirish Moodalbail 
10206e91bba0SGirish Moodalbail 		/*
10216e91bba0SGirish Moodalbail 		 * address will be set by the parser after nextarg has
10226e91bba0SGirish Moodalbail 		 * been scanned
10236e91bba0SGirish Moodalbail 		 */
10246e91bba0SGirish Moodalbail 		return (0);
10256e91bba0SGirish Moodalbail 	}
10266e91bba0SGirish Moodalbail 
10277c478bd9Sstevel@tonic-gate 	/* Tell parser that an address was set */
10287c478bd9Sstevel@tonic-gate 	setaddr++;
10297c478bd9Sstevel@tonic-gate 	/* save copy of netmask to restore in case of error */
10306e91bba0SGirish Moodalbail 	(void) strncpy(lifr1.lifr_name, name, sizeof (lifr1.lifr_name));
10316e91bba0SGirish Moodalbail 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr1) < 0)
10327c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCGLIFNETMASK");
10336e91bba0SGirish Moodalbail 	sav_netmask = lifr1.lifr_addr;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	/*
10367c478bd9Sstevel@tonic-gate 	 * If setting the address and not the mask, clear any existing mask
10377c478bd9Sstevel@tonic-gate 	 * and the kernel will then assign the default (netmask has been set
10387c478bd9Sstevel@tonic-gate 	 * to 0 in this case).  If setting both (either by using a prefix or
10397c478bd9Sstevel@tonic-gate 	 * using the netmask command), set the mask first, so the address will
10407c478bd9Sstevel@tonic-gate 	 * be interpreted correctly.
10417c478bd9Sstevel@tonic-gate 	 */
10426e91bba0SGirish Moodalbail 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
10436e91bba0SGirish Moodalbail 	/* lifr.lifr_addr already contains netmask from set_mask_lifreq() */
10447c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
10457c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	if (debug) {
10487c478bd9Sstevel@tonic-gate 		char abuf[INET6_ADDRSTRLEN];
10497c478bd9Sstevel@tonic-gate 		void *addr = (afp->af_af == AF_INET) ?
10507c478bd9Sstevel@tonic-gate 		    (void *)&((struct sockaddr_in *)&laddr)->sin_addr :
10517c478bd9Sstevel@tonic-gate 		    (void *)&((struct sockaddr_in6 *)&laddr)->sin6_addr;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 		(void) printf("Setting %s af %d addr %s\n",
10547c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, afp->af_af,
10557c478bd9Sstevel@tonic-gate 		    inet_ntop(afp->af_af, addr, abuf, sizeof (abuf)));
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = laddr;
10587c478bd9Sstevel@tonic-gate 	lifr.lifr_addr.ss_family = afp->af_af;
10597c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
10607c478bd9Sstevel@tonic-gate 		/*
10617c478bd9Sstevel@tonic-gate 		 * Restore the netmask
10627c478bd9Sstevel@tonic-gate 		 */
10637c478bd9Sstevel@tonic-gate 		int saverr = errno;
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
10667c478bd9Sstevel@tonic-gate 		lifr.lifr_addr = sav_netmask;
10677c478bd9Sstevel@tonic-gate 		(void) ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr);
10687c478bd9Sstevel@tonic-gate 		errno = saverr;
10697c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFADDR");
10707c478bd9Sstevel@tonic-gate 	}
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	return (0);
10737c478bd9Sstevel@tonic-gate }
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate /*
10767c478bd9Sstevel@tonic-gate  * The following functions are stolen from the ipseckey(1m) program.
10777c478bd9Sstevel@tonic-gate  * Perhaps they should be somewhere common, but for now, we just maintain
10787c478bd9Sstevel@tonic-gate  * two versions.  We do this because of the different semantics for which
10797c478bd9Sstevel@tonic-gate  * algorithms we select ("requested" for ifconfig vs. "actual" for key).
10807c478bd9Sstevel@tonic-gate  */
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate static ulong_t
10837c478bd9Sstevel@tonic-gate parsenum(char *num)
10847c478bd9Sstevel@tonic-gate {
10857c478bd9Sstevel@tonic-gate 	ulong_t rc;
10867c478bd9Sstevel@tonic-gate 	char *end = NULL;
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	errno = 0;
10897c478bd9Sstevel@tonic-gate 	rc = strtoul(num, &end, 0);
10907c478bd9Sstevel@tonic-gate 	if (errno != 0 || end == num || *end != '\0') {
10917c478bd9Sstevel@tonic-gate 		rc = (ulong_t)-1;
10927c478bd9Sstevel@tonic-gate 	}
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	return (rc);
10957c478bd9Sstevel@tonic-gate }
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate /*
10987c478bd9Sstevel@tonic-gate  * Parse and reverse parse possible algorithm values, include numbers.
10997c478bd9Sstevel@tonic-gate  * Mostly stolen from ipseckey.c. See the comments above parsenum() for why
11007c478bd9Sstevel@tonic-gate  * this isn't common to ipseckey.c.
11017c478bd9Sstevel@tonic-gate  *
11027c478bd9Sstevel@tonic-gate  * NOTE: Static buffer in this function for the return value.  Since ifconfig
11032b24ab6bSSebastien Roy  *       isn't multithreaded, this isn't a huge problem.
11047c478bd9Sstevel@tonic-gate  */
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate #define	NBUF_SIZE 20	/* Enough to print a large integer. */
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate static char *
11097c478bd9Sstevel@tonic-gate rparsealg(uint8_t alg_value, int proto_num)
11107c478bd9Sstevel@tonic-gate {
11117c478bd9Sstevel@tonic-gate 	struct ipsecalgent *alg;
11127c478bd9Sstevel@tonic-gate 	static char numprint[128];	/* Enough to hold an algorithm name. */
11137c478bd9Sstevel@tonic-gate 
1114d2f8a3dfSpwernau 	/*
1115d2f8a3dfSpwernau 	 * Special cases for "any" and "none"
1116d2f8a3dfSpwernau 	 * The kernel needs to be able to distinguish between "any"
1117d2f8a3dfSpwernau 	 * and "none" and the APIs are underdefined in this area for auth.
1118d2f8a3dfSpwernau 	 */
1119d2f8a3dfSpwernau 	if (proto_num == IPSEC_PROTO_AH) {
1120d2f8a3dfSpwernau 		if (alg_value == SADB_AALG_NONE)
1121d2f8a3dfSpwernau 			return ("none");
1122d2f8a3dfSpwernau 		if (alg_value == SADB_AALG_ANY)
1123d2f8a3dfSpwernau 			return ("any");
1124d2f8a3dfSpwernau 	}
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	alg = getipsecalgbynum(alg_value, proto_num, NULL);
11277c478bd9Sstevel@tonic-gate 	if (alg != NULL) {
11287c478bd9Sstevel@tonic-gate 		(void) strlcpy(numprint, alg->a_names[0], sizeof (numprint));
11297c478bd9Sstevel@tonic-gate 		freeipsecalgent(alg);
11307c478bd9Sstevel@tonic-gate 	} else {
11317c478bd9Sstevel@tonic-gate 		(void) snprintf(numprint, sizeof (numprint), "%d", alg_value);
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	return (numprint);
11357c478bd9Sstevel@tonic-gate }
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate static uint_t
11387c478bd9Sstevel@tonic-gate parsealg(char *algname, int proto_num)
11397c478bd9Sstevel@tonic-gate {
11407c478bd9Sstevel@tonic-gate 	struct ipsecalgent *alg;
11417c478bd9Sstevel@tonic-gate 	ulong_t invalue;
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	if (algname == NULL) {
11447c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: Unexpected end of command "
11457c478bd9Sstevel@tonic-gate 		    "line.\n");
11467c478bd9Sstevel@tonic-gate 		exit(1);
11477c478bd9Sstevel@tonic-gate 	}
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	/*
1150d2f8a3dfSpwernau 	 * Special-case "none" and "any".
1151d2f8a3dfSpwernau 	 * Use strcasecmp because its length is bounded.
11527c478bd9Sstevel@tonic-gate 	 */
11537c478bd9Sstevel@tonic-gate 	if (strcasecmp("none", algname) == 0) {
11547c478bd9Sstevel@tonic-gate 		return ((proto_num == IPSEC_PROTO_ESP) ?
11557c478bd9Sstevel@tonic-gate 		    NO_ESP_EALG : NO_ESP_AALG);
11567c478bd9Sstevel@tonic-gate 	}
1157d2f8a3dfSpwernau 	if ((strcasecmp("any", algname) == 0) && (proto_num == IPSEC_PROTO_AH))
1158d2f8a3dfSpwernau 		return (SADB_AALG_ANY);
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	alg = getipsecalgbyname(algname, proto_num, NULL);
11617c478bd9Sstevel@tonic-gate 	if (alg != NULL) {
11627c478bd9Sstevel@tonic-gate 		invalue = alg->a_alg_num;
11637c478bd9Sstevel@tonic-gate 		freeipsecalgent(alg);
11647c478bd9Sstevel@tonic-gate 		return ((uint_t)invalue);
11657c478bd9Sstevel@tonic-gate 	}
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	/*
11687c478bd9Sstevel@tonic-gate 	 * Since algorithms can be loaded during kernel run-time, check for
11697c478bd9Sstevel@tonic-gate 	 * numeric algorithm values too.
11707c478bd9Sstevel@tonic-gate 	 */
11717c478bd9Sstevel@tonic-gate 	invalue = parsenum(algname);
11727c478bd9Sstevel@tonic-gate 	if ((invalue & (ulong_t)0xff) == invalue)
11737c478bd9Sstevel@tonic-gate 		return ((uint_t)invalue);
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "ifconfig: %s algorithm type %s unknown.\n",
11767c478bd9Sstevel@tonic-gate 	    (proto_num == IPSEC_PROTO_ESP) ?
11777c478bd9Sstevel@tonic-gate 	    "Encryption" : "Authentication", algname);
11787c478bd9Sstevel@tonic-gate 	exit(1);
11797c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
11807c478bd9Sstevel@tonic-gate }
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate /*
11837c478bd9Sstevel@tonic-gate  * Actual ifconfig functions to set tunnel security properties.
11847c478bd9Sstevel@tonic-gate  */
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate enum ipsec_alg_type { ESP_ENCR_ALG = 1, ESP_AUTH_ALG, AH_AUTH_ALG };
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate static int
11897c478bd9Sstevel@tonic-gate set_tun_algs(int which_alg, int alg)
11907c478bd9Sstevel@tonic-gate {
11912b24ab6bSSebastien Roy 	boolean_t	encr_alg_set = _B_FALSE;
11922b24ab6bSSebastien Roy 	iptun_params_t	params;
11932b24ab6bSSebastien Roy 	dladm_status_t	status;
11942b24ab6bSSebastien Roy 	ipsec_req_t	*ipsr;
11957c478bd9Sstevel@tonic-gate 
11962b24ab6bSSebastien Roy 	if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN,
11972b24ab6bSSebastien Roy 	    &params.iptun_param_linkid)) != DLADM_STATUS_OK)
11982b24ab6bSSebastien Roy 		goto done;
11997c478bd9Sstevel@tonic-gate 
12002b24ab6bSSebastien Roy 	status = dladm_iptun_getparams(dlh, &params, DLADM_OPT_ACTIVE);
12012b24ab6bSSebastien Roy 	if (status != DLADM_STATUS_OK)
12022b24ab6bSSebastien Roy 		goto done;
12037c478bd9Sstevel@tonic-gate 
12042b24ab6bSSebastien Roy 	ipsr = &params.iptun_param_secinfo;
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	/*
12077c478bd9Sstevel@tonic-gate 	 * If I'm just starting off this ifconfig, I want a clean slate,
12087c478bd9Sstevel@tonic-gate 	 * otherwise, I've captured the current tunnel security settings.
12097c478bd9Sstevel@tonic-gate 	 * In the case of continuation, I merely add to the settings.
12107c478bd9Sstevel@tonic-gate 	 */
12112b24ab6bSSebastien Roy 	if (!(params.iptun_param_flags & IPTUN_PARAM_SECINFO))
12127c478bd9Sstevel@tonic-gate 		(void) memset(ipsr, 0, sizeof (*ipsr));
12137c478bd9Sstevel@tonic-gate 
12142b24ab6bSSebastien Roy 	/* We're only modifying the IPsec information */
12152b24ab6bSSebastien Roy 	params.iptun_param_flags = IPTUN_PARAM_SECINFO;
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	switch (which_alg) {
12187c478bd9Sstevel@tonic-gate 	case ESP_ENCR_ALG:
12197c478bd9Sstevel@tonic-gate 		if (alg == NO_ESP_EALG) {
12207c478bd9Sstevel@tonic-gate 			if (ipsr->ipsr_esp_auth_alg == SADB_AALG_NONE)
12217c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_req = 0;
12227c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_alg = SADB_EALG_NONE;
1223d2f8a3dfSpwernau 
1224d2f8a3dfSpwernau 			/* Let the user specify NULL encryption implicitly. */
1225d2f8a3dfSpwernau 			if (ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) {
1226d2f8a3dfSpwernau 				encr_alg_set = _B_TRUE;
1227d2f8a3dfSpwernau 				ipsr->ipsr_esp_alg = SADB_EALG_NULL;
1228d2f8a3dfSpwernau 			}
12297c478bd9Sstevel@tonic-gate 		} else {
12307c478bd9Sstevel@tonic-gate 			encr_alg_set = _B_TRUE;
12317c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_req =
12327c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12337c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_alg = alg;
12347c478bd9Sstevel@tonic-gate 		}
12357c478bd9Sstevel@tonic-gate 		break;
12367c478bd9Sstevel@tonic-gate 	case ESP_AUTH_ALG:
12377c478bd9Sstevel@tonic-gate 		if (alg == NO_ESP_AALG) {
1238d2f8a3dfSpwernau 			if ((ipsr->ipsr_esp_alg == SADB_EALG_NONE ||
1239d2f8a3dfSpwernau 			    ipsr->ipsr_esp_alg == SADB_EALG_NULL) &&
1240d2f8a3dfSpwernau 			    !encr_alg_set)
12417c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_req = 0;
12427c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_auth_alg = SADB_AALG_NONE;
12437c478bd9Sstevel@tonic-gate 		} else {
12447c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_req =
12457c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12467c478bd9Sstevel@tonic-gate 			ipsr->ipsr_esp_auth_alg = alg;
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 			/* Let the user specify NULL encryption implicitly. */
12497c478bd9Sstevel@tonic-gate 			if (ipsr->ipsr_esp_alg == SADB_EALG_NONE &&
12507c478bd9Sstevel@tonic-gate 			    !encr_alg_set)
12517c478bd9Sstevel@tonic-gate 				ipsr->ipsr_esp_alg = SADB_EALG_NULL;
12527c478bd9Sstevel@tonic-gate 		}
12537c478bd9Sstevel@tonic-gate 		break;
12547c478bd9Sstevel@tonic-gate 	case AH_AUTH_ALG:
12557c478bd9Sstevel@tonic-gate 		if (alg == NO_AH_AALG) {
12567c478bd9Sstevel@tonic-gate 			ipsr->ipsr_ah_req = 0;
12577c478bd9Sstevel@tonic-gate 			ipsr->ipsr_auth_alg = SADB_AALG_NONE;
12587c478bd9Sstevel@tonic-gate 		} else {
12597c478bd9Sstevel@tonic-gate 			ipsr->ipsr_ah_req =
12607c478bd9Sstevel@tonic-gate 			    IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
12617c478bd9Sstevel@tonic-gate 			ipsr->ipsr_auth_alg = alg;
12627c478bd9Sstevel@tonic-gate 		}
12637c478bd9Sstevel@tonic-gate 		break;
12647c478bd9Sstevel@tonic-gate 		/* Will never hit DEFAULT */
12657c478bd9Sstevel@tonic-gate 	}
12667c478bd9Sstevel@tonic-gate 
12672b24ab6bSSebastien Roy 	status = dladm_iptun_modify(dlh, &params, DLADM_OPT_ACTIVE);
12687c478bd9Sstevel@tonic-gate 
12692b24ab6bSSebastien Roy done:
12702b24ab6bSSebastien Roy 	if (status != DLADM_STATUS_OK)
12712b24ab6bSSebastien Roy 		dladmerr_exit(status, name);
12722b24ab6bSSebastien Roy 	else {
12732b24ab6bSSebastien Roy 		ipsec_policy_set = _B_TRUE;
12742b24ab6bSSebastien Roy 		if ((ipsr->ipsr_esp_req != 0 &&
12752b24ab6bSSebastien Roy 		    ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) ||
12762b24ab6bSSebastien Roy 		    (ipsr->ipsr_ah_req != 0 &&
12772b24ab6bSSebastien Roy 		    ipsr->ipsr_auth_alg != SADB_AALG_NONE))
12782b24ab6bSSebastien Roy 			ipsec_auth_covered = _B_TRUE;
12792b24ab6bSSebastien Roy 	}
12807c478bd9Sstevel@tonic-gate 	return (0);
12817c478bd9Sstevel@tonic-gate }
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate /* ARGSUSED */
12847c478bd9Sstevel@tonic-gate static int
12857c478bd9Sstevel@tonic-gate set_tun_esp_encr_alg(char *addr, int64_t param)
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate 	return (set_tun_algs(ESP_ENCR_ALG,
1288fc80c0dfSnordmark 	    parsealg(addr, IPSEC_PROTO_ESP)));
12897c478bd9Sstevel@tonic-gate }
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate /* ARGSUSED */
12927c478bd9Sstevel@tonic-gate static int
12937c478bd9Sstevel@tonic-gate set_tun_esp_auth_alg(char *addr, int64_t param)
12947c478bd9Sstevel@tonic-gate {
12957c478bd9Sstevel@tonic-gate 	return (set_tun_algs(ESP_AUTH_ALG,
1296fc80c0dfSnordmark 	    parsealg(addr, IPSEC_PROTO_AH)));
12977c478bd9Sstevel@tonic-gate }
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate /* ARGSUSED */
13007c478bd9Sstevel@tonic-gate static int
13017c478bd9Sstevel@tonic-gate set_tun_ah_alg(char *addr, int64_t param)
13027c478bd9Sstevel@tonic-gate {
13037c478bd9Sstevel@tonic-gate 	return (set_tun_algs(AH_AUTH_ALG,
1304fc80c0dfSnordmark 	    parsealg(addr, IPSEC_PROTO_AH)));
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate /* ARGSUSED */
13087c478bd9Sstevel@tonic-gate static int
13097c478bd9Sstevel@tonic-gate setifrevarp(char *arg, int64_t param)
13107c478bd9Sstevel@tonic-gate {
13117c478bd9Sstevel@tonic-gate 	struct sockaddr_in	laddr;
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	if (afp->af_af == AF_INET6) {
13147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13157c478bd9Sstevel@tonic-gate 		    "ifconfig: revarp not possible on IPv6 interface %s\n",
13167c478bd9Sstevel@tonic-gate 		    name);
13177c478bd9Sstevel@tonic-gate 		exit(1);
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 	if (doifrevarp(name, &laddr)) {
13207c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13217c478bd9Sstevel@tonic-gate 		laddr.sin_family = AF_INET;
13227c478bd9Sstevel@tonic-gate 		(void) memcpy(&lifr.lifr_addr, &laddr, sizeof (laddr));
13237c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
13247c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCSLIFADDR");
13257c478bd9Sstevel@tonic-gate 	}
13267c478bd9Sstevel@tonic-gate 	return (0);
13277c478bd9Sstevel@tonic-gate }
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate /* ARGSUSED */
13307c478bd9Sstevel@tonic-gate static int
13317c478bd9Sstevel@tonic-gate setifsubnet(char *addr, int64_t param)
13327c478bd9Sstevel@tonic-gate {
13337c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
13347c478bd9Sstevel@tonic-gate 	struct	sockaddr_storage subnet;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, &subnet, &prefixlen);
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
13397c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
13407c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13417c478bd9Sstevel@tonic-gate 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
13427c478bd9Sstevel@tonic-gate 		exit(1);
13437c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
13447c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
13457c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13467c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
13477c478bd9Sstevel@tonic-gate 		exit(1);
13487c478bd9Sstevel@tonic-gate 	default:
13497c478bd9Sstevel@tonic-gate 		break;
13507c478bd9Sstevel@tonic-gate 	}
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	lifr.lifr_addr = subnet;
13537c478bd9Sstevel@tonic-gate 	lifr.lifr_addrlen = prefixlen;
13547c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13557c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFSUBNET, (caddr_t)&lifr) < 0)
13567c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFSUBNET");
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	return (0);
13597c478bd9Sstevel@tonic-gate }
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate /* ARGSUSED */
13627c478bd9Sstevel@tonic-gate static int
13637c478bd9Sstevel@tonic-gate setifnetmask(char *addr, int64_t param)
13647c478bd9Sstevel@tonic-gate {
13657c478bd9Sstevel@tonic-gate 	struct sockaddr_in netmask;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	assert(afp->af_af != AF_INET6);
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 	if (strcmp(addr, "+") == 0) {
1370dd7a6f5fSkcpoon 		if (!in_getmask(&netmask, _B_FALSE))
13717c478bd9Sstevel@tonic-gate 			return (0);
1372dd7a6f5fSkcpoon 		(void) printf("Setting netmask of %s to %s\n", name,
1373dd7a6f5fSkcpoon 		    inet_ntoa(netmask.sin_addr));
13747c478bd9Sstevel@tonic-gate 	} else {
13757c478bd9Sstevel@tonic-gate 		in_getaddr(addr, (struct sockaddr *)&netmask, NULL);
13767c478bd9Sstevel@tonic-gate 	}
13777c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
13787c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &netmask, sizeof (netmask));
13797c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
13807c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
13817c478bd9Sstevel@tonic-gate 	return (0);
13827c478bd9Sstevel@tonic-gate }
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate /*
13857c478bd9Sstevel@tonic-gate  * Parse '/<n>' as a netmask.
13867c478bd9Sstevel@tonic-gate  */
13877c478bd9Sstevel@tonic-gate /* ARGSUSED */
13887c478bd9Sstevel@tonic-gate static int
13897c478bd9Sstevel@tonic-gate setifprefixlen(char *addr, int64_t param)
13907c478bd9Sstevel@tonic-gate {
13917c478bd9Sstevel@tonic-gate 	int prefixlen;
13927c478bd9Sstevel@tonic-gate 	int af = afp->af_af;
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	prefixlen = in_getprefixlen(addr, _B_TRUE,
13957906a3e0Smeem 	    (af == AF_INET) ? IP_ABITS : IPV6_ABITS);
13967c478bd9Sstevel@tonic-gate 	if (prefixlen < 0) {
13977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
13987c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
13997c478bd9Sstevel@tonic-gate 		exit(1);
14007c478bd9Sstevel@tonic-gate 	}
14017c478bd9Sstevel@tonic-gate 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
14027c478bd9Sstevel@tonic-gate 	lifr.lifr_addr.ss_family = af;
14037c478bd9Sstevel@tonic-gate 	if (af == AF_INET6) {
14047c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
14077906a3e0Smeem 		if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
14087c478bd9Sstevel@tonic-gate 		    (uchar_t *)&sin6->sin6_addr)) {
14097c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: "
14107c478bd9Sstevel@tonic-gate 			    "Bad prefix length: %d\n",
14117c478bd9Sstevel@tonic-gate 			    prefixlen);
14127c478bd9Sstevel@tonic-gate 			exit(1);
14137c478bd9Sstevel@tonic-gate 		}
14147c478bd9Sstevel@tonic-gate 	} else if (af == AF_INET) {
14157c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin;
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
14187906a3e0Smeem 		if (!in_prefixlentomask(prefixlen, IP_ABITS,
14197c478bd9Sstevel@tonic-gate 		    (uchar_t *)&sin->sin_addr)) {
14207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: "
14217c478bd9Sstevel@tonic-gate 			    "Bad prefix length: %d\n",
14227c478bd9Sstevel@tonic-gate 			    prefixlen);
14237c478bd9Sstevel@tonic-gate 			exit(1);
14247c478bd9Sstevel@tonic-gate 		}
14257c478bd9Sstevel@tonic-gate 	} else {
14267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: setting prefix only supported"
14277c478bd9Sstevel@tonic-gate 		    " for address family inet or inet6\n");
14287c478bd9Sstevel@tonic-gate 		exit(1);
14297c478bd9Sstevel@tonic-gate 	}
14307c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14317c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
14327c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFNETMASK");
14337c478bd9Sstevel@tonic-gate 	return (0);
14347c478bd9Sstevel@tonic-gate }
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate /* ARGSUSED */
14377c478bd9Sstevel@tonic-gate static int
14387c478bd9Sstevel@tonic-gate setifbroadaddr(char *addr, int64_t param)
14397c478bd9Sstevel@tonic-gate {
14407c478bd9Sstevel@tonic-gate 	struct	sockaddr_in broadaddr;
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	assert(afp->af_af != AF_INET6);
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	if (strcmp(addr, "+") == 0) {
14457c478bd9Sstevel@tonic-gate 		/*
14467c478bd9Sstevel@tonic-gate 		 * This doesn't set the broadcast address at all. Rather, it
14477c478bd9Sstevel@tonic-gate 		 * gets, then sets the interface's address, relying on the fact
14487c478bd9Sstevel@tonic-gate 		 * that resetting the address will reset the broadcast address.
14497c478bd9Sstevel@tonic-gate 		 */
14507c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name,
14517c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
14527c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
14537c478bd9Sstevel@tonic-gate 			if (errno != EADDRNOTAVAIL)
14547c478bd9Sstevel@tonic-gate 				Perror0_exit("SIOCGLIFADDR");
14557c478bd9Sstevel@tonic-gate 			return (0);
14567c478bd9Sstevel@tonic-gate 		}
14577c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
14587c478bd9Sstevel@tonic-gate 			Perror0_exit("SIOCGLIFADDR");
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 		return (0);
14617c478bd9Sstevel@tonic-gate 	}
14627c478bd9Sstevel@tonic-gate 	in_getaddr(addr, (struct sockaddr *)&broadaddr, NULL);
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &broadaddr, sizeof (broadaddr));
14657c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14667c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFBRDADDR, (caddr_t)&lifr) < 0)
14677c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFBRDADDR");
14687c478bd9Sstevel@tonic-gate 	return (0);
14697c478bd9Sstevel@tonic-gate }
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate /*
14727c478bd9Sstevel@tonic-gate  * set interface destination address
14737c478bd9Sstevel@tonic-gate  */
14747c478bd9Sstevel@tonic-gate /* ARGSUSED */
14757c478bd9Sstevel@tonic-gate static int
14767c478bd9Sstevel@tonic-gate setifdstaddr(char *addr, int64_t param)
14777c478bd9Sstevel@tonic-gate {
14787c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(addr, (struct sockaddr *)&lifr.lifr_addr, NULL);
14797c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14807c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFDSTADDR, (caddr_t)&lifr) < 0)
14817c478bd9Sstevel@tonic-gate 		Perror0_exit("setifdstaddr: SIOCSLIFDSTADDR");
14827c478bd9Sstevel@tonic-gate 	return (0);
14837c478bd9Sstevel@tonic-gate }
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate /* ARGSUSED */
14867c478bd9Sstevel@tonic-gate static int
14877c478bd9Sstevel@tonic-gate setifflags(char *val, int64_t value)
14887c478bd9Sstevel@tonic-gate {
1489e11c3f44Smeem 	struct lifreq lifrl;	/* local lifreq struct */
1490e11c3f44Smeem 	boolean_t bringup = _B_FALSE;
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
14937c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0)
14947c478bd9Sstevel@tonic-gate 		Perror0_exit("setifflags: SIOCGLIFFLAGS");
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	if (value < 0) {
14977c478bd9Sstevel@tonic-gate 		value = -value;
1498e11c3f44Smeem 
1499e11c3f44Smeem 		if ((value & IFF_NOFAILOVER) && (lifr.lifr_flags & IFF_UP)) {
1500e11c3f44Smeem 			/*
1501e11c3f44Smeem 			 * The kernel does not allow administratively up test
1502e11c3f44Smeem 			 * addresses to be converted to data addresses.  Bring
1503e11c3f44Smeem 			 * the address down first, then bring it up after it's
1504e11c3f44Smeem 			 * been converted to a data address.
1505e11c3f44Smeem 			 */
1506e11c3f44Smeem 			lifr.lifr_flags &= ~IFF_UP;
1507e11c3f44Smeem 			(void) ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr);
1508e11c3f44Smeem 			bringup = _B_TRUE;
1509e11c3f44Smeem 		}
1510e11c3f44Smeem 
15117c478bd9Sstevel@tonic-gate 		lifr.lifr_flags &= ~value;
1512e11c3f44Smeem 		if ((value & (IFF_UP | IFF_NOFAILOVER)) &&
1513e11c3f44Smeem 		    (lifr.lifr_flags & IFF_DUPLICATE)) {
151469bb4bb4Scarlsonj 			/*
151569bb4bb4Scarlsonj 			 * If the user is trying to mark an interface with a
1516e11c3f44Smeem 			 * duplicate address as "down," or convert a duplicate
1517e11c3f44Smeem 			 * test address to a data address, then fetch the
1518e11c3f44Smeem 			 * address and set it.  This will cause IP to clear
1519e11c3f44Smeem 			 * the IFF_DUPLICATE flag and stop the automatic
1520e11c3f44Smeem 			 * recovery timer.
152169bb4bb4Scarlsonj 			 */
152269bb4bb4Scarlsonj 			value = lifr.lifr_flags;
152369bb4bb4Scarlsonj 			if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0)
152469bb4bb4Scarlsonj 				(void) ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr);
152569bb4bb4Scarlsonj 			lifr.lifr_flags = value;
152669bb4bb4Scarlsonj 		}
152769bb4bb4Scarlsonj 	} else {
15287c478bd9Sstevel@tonic-gate 		lifr.lifr_flags |= value;
152969bb4bb4Scarlsonj 	}
1530e11c3f44Smeem 
1531e11c3f44Smeem 	/*
1532e11c3f44Smeem 	 * If we're about to bring up an underlying physical IPv6 interface in
1533e11c3f44Smeem 	 * an IPMP group, ensure the IPv6 IPMP interface is also up.  This is
1534e11c3f44Smeem 	 * for backward compatibility with legacy configurations in which
1535e11c3f44Smeem 	 * there are no explicit hostname files for IPMP interfaces.  (For
1536e11c3f44Smeem 	 * IPv4, this is automatically handled by the kernel when migrating
1537e11c3f44Smeem 	 * the underlying interface's data address to the IPMP interface.)
1538e11c3f44Smeem 	 */
1539e11c3f44Smeem 	(void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ);
1540e11c3f44Smeem 
1541e11c3f44Smeem 	if (lifnum(lifr.lifr_name) == 0 &&
1542e11c3f44Smeem 	    (lifr.lifr_flags & (IFF_UP|IFF_IPV6)) == (IFF_UP|IFF_IPV6) &&
1543e11c3f44Smeem 	    ioctl(s, SIOCGLIFGROUPNAME, &lifrl) == 0 &&
1544e11c3f44Smeem 	    lifrl.lifr_groupname[0] != '\0') {
1545e11c3f44Smeem 		lifgroupinfo_t lifgr;
1546e11c3f44Smeem 
1547e11c3f44Smeem 		(void) strlcpy(lifgr.gi_grname, lifrl.lifr_groupname,
1548e11c3f44Smeem 		    LIFGRNAMSIZ);
1549e11c3f44Smeem 		if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1)
1550e11c3f44Smeem 			Perror0_exit("setifflags: SIOCGLIFGROUPINFO");
1551e11c3f44Smeem 
1552e11c3f44Smeem 		(void) strlcpy(lifrl.lifr_name, lifgr.gi_grifname, LIFNAMSIZ);
1553e11c3f44Smeem 		if (ioctl(s, SIOCGLIFFLAGS, &lifrl) == -1)
1554e11c3f44Smeem 			Perror0_exit("setifflags: SIOCGLIFFLAGS");
1555e11c3f44Smeem 		if (!(lifrl.lifr_flags & IFF_UP)) {
1556e11c3f44Smeem 			lifrl.lifr_flags |= IFF_UP;
1557e11c3f44Smeem 			if (ioctl(s, SIOCSLIFFLAGS, &lifrl) == -1)
1558e11c3f44Smeem 				Perror0_exit("setifflags: SIOCSLIFFLAGS");
1559e11c3f44Smeem 		}
1560e11c3f44Smeem 	}
1561e11c3f44Smeem 
15627c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1563e11c3f44Smeem 	if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0)
15647c478bd9Sstevel@tonic-gate 		Perror0_exit("setifflags: SIOCSLIFFLAGS");
1565e11c3f44Smeem 
1566e11c3f44Smeem 	if (bringup) {
1567e11c3f44Smeem 		lifr.lifr_flags |= IFF_UP;
1568e11c3f44Smeem 		if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0)
1569e11c3f44Smeem 			Perror0_exit("setifflags: SIOCSLIFFLAGS IFF_UP");
15707c478bd9Sstevel@tonic-gate 	}
1571e11c3f44Smeem 
15727c478bd9Sstevel@tonic-gate 	return (0);
15737c478bd9Sstevel@tonic-gate }
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate /* ARGSUSED */
15767c478bd9Sstevel@tonic-gate static int
15777c478bd9Sstevel@tonic-gate setifmetric(char *val, int64_t param)
15787c478bd9Sstevel@tonic-gate {
15797c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15807c478bd9Sstevel@tonic-gate 	lifr.lifr_metric = atoi(val);
15817c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFMETRIC, (caddr_t)&lifr) < 0)
15827c478bd9Sstevel@tonic-gate 		Perror0_exit("setifmetric: SIOCSLIFMETRIC");
15837c478bd9Sstevel@tonic-gate 	return (0);
15847c478bd9Sstevel@tonic-gate }
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate /* ARGSUSED */
15877c478bd9Sstevel@tonic-gate static int
15887c478bd9Sstevel@tonic-gate setifmtu(char *val, int64_t param)
15897c478bd9Sstevel@tonic-gate {
15907c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
15917c478bd9Sstevel@tonic-gate 	lifr.lifr_mtu = atoi(val);
15927c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFMTU, (caddr_t)&lifr) < 0)
15937c478bd9Sstevel@tonic-gate 		Perror0_exit("setifmtu: SIOCSLIFMTU");
15947c478bd9Sstevel@tonic-gate 	return (0);
15957c478bd9Sstevel@tonic-gate }
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate /* ARGSUSED */
15987c478bd9Sstevel@tonic-gate static int
15997c478bd9Sstevel@tonic-gate setifindex(char *val, int64_t param)
16007c478bd9Sstevel@tonic-gate {
16017c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
16027c478bd9Sstevel@tonic-gate 	lifr.lifr_index = atoi(val);
16037c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFINDEX, (caddr_t)&lifr) < 0)
16047c478bd9Sstevel@tonic-gate 		Perror0_exit("setifindex: SIOCSLIFINDEX");
16057c478bd9Sstevel@tonic-gate 	return (0);
16067c478bd9Sstevel@tonic-gate }
16077c478bd9Sstevel@tonic-gate 
1608e11c3f44Smeem /* ARGSUSED */
1609e11c3f44Smeem static void
1610e11c3f44Smeem notifycb(dlpi_handle_t dh, dlpi_notifyinfo_t *dnip, void *arg)
1611e11c3f44Smeem {
1612e11c3f44Smeem }
1613e11c3f44Smeem 
16147c478bd9Sstevel@tonic-gate /* ARGSUSED */
16157c478bd9Sstevel@tonic-gate static int
16167c478bd9Sstevel@tonic-gate setifether(char *addr, int64_t param)
16177c478bd9Sstevel@tonic-gate {
1618e11c3f44Smeem 	uchar_t		*hwaddr;
1619e11c3f44Smeem 	int		hwaddrlen;
1620e11c3f44Smeem 	int		retval;
1621e11c3f44Smeem 	ifaddrlistx_t	*ifaddrp, *ifaddrs = NULL;
1622e11c3f44Smeem 	dlpi_handle_t	dh;
1623e11c3f44Smeem 	dlpi_notifyid_t id;
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
16267c478bd9Sstevel@tonic-gate 		ifstatus(name);
16277c478bd9Sstevel@tonic-gate 		print_ifether(name);
16287c478bd9Sstevel@tonic-gate 		return (0);
16297c478bd9Sstevel@tonic-gate 	}
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	/*
16327c478bd9Sstevel@tonic-gate 	 * if the IP interface in the arguments is a logical
16337c478bd9Sstevel@tonic-gate 	 * interface, exit with an error now.
16347c478bd9Sstevel@tonic-gate 	 */
16357c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
16367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: cannot change"
16377c478bd9Sstevel@tonic-gate 		    " ethernet address of a logical interface\n");
16387c478bd9Sstevel@tonic-gate 		exit(1);
16397c478bd9Sstevel@tonic-gate 	}
16407c478bd9Sstevel@tonic-gate 
1641e11c3f44Smeem 	if ((hwaddr = _link_aton(addr, &hwaddrlen)) == NULL) {
1642e11c3f44Smeem 		if (hwaddrlen == -1)
16437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
16446e91bba0SGirish Moodalbail 			    "ifconfig: bad ethernet address\n");
16457c478bd9Sstevel@tonic-gate 		else
16467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "ifconfig: malloc() failed\n");
16477c478bd9Sstevel@tonic-gate 		exit(1);
16487c478bd9Sstevel@tonic-gate 	}
16497c478bd9Sstevel@tonic-gate 
1650e11c3f44Smeem 	if ((retval = dlpi_open(name, &dh, 0)) != DLPI_SUCCESS)
1651e11c3f44Smeem 		Perrdlpi_exit("cannot dlpi_open() link", name, retval);
16527c478bd9Sstevel@tonic-gate 
1653e11c3f44Smeem 	retval = dlpi_enabnotify(dh, DL_NOTE_PHYS_ADDR, notifycb, NULL, &id);
1654e11c3f44Smeem 	if (retval == DLPI_SUCCESS) {
1655e11c3f44Smeem 		(void) dlpi_disabnotify(dh, id, NULL);
1656e11c3f44Smeem 	} else {
1657e11c3f44Smeem 		/*
1658e11c3f44Smeem 		 * This link does not support DL_NOTE_PHYS_ADDR: bring down
1659e11c3f44Smeem 		 * all of the addresses to flush the old hardware address
1660e11c3f44Smeem 		 * information out of IP.
1661e11c3f44Smeem 		 *
1662e11c3f44Smeem 		 * NOTE: Skipping this when DL_NOTE_PHYS_ADDR is supported is
1663e11c3f44Smeem 		 * more than an optimization: in.mpathd will set IFF_OFFLINE
1664e11c3f44Smeem 		 * if it's notified and the new address is a duplicate of
1665e11c3f44Smeem 		 * another in the group -- but the flags manipulation in
1666e11c3f44Smeem 		 * ifaddr_{down,up}() cannot be atomic and thus might clobber
1667e11c3f44Smeem 		 * IFF_OFFLINE, confusing in.mpathd.
1668e11c3f44Smeem 		 */
1669e11c3f44Smeem 		if (ifaddrlistx(name, IFF_UP, 0, &ifaddrs) == -1)
1670e11c3f44Smeem 			Perror2_exit(name, "cannot get address list");
1671e11c3f44Smeem 
1672e11c3f44Smeem 		ifaddrp = ifaddrs;
1673e11c3f44Smeem 		for (; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
1674e11c3f44Smeem 			if (!ifaddr_down(ifaddrp)) {
1675e11c3f44Smeem 				Perror2_exit(ifaddrp->ia_name,
1676e11c3f44Smeem 				    "cannot bring down");
1677e11c3f44Smeem 			}
1678e11c3f44Smeem 		}
16797c478bd9Sstevel@tonic-gate 	}
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 	/*
1682e11c3f44Smeem 	 * Change the hardware address.
16837c478bd9Sstevel@tonic-gate 	 */
1684e11c3f44Smeem 	retval = dlpi_set_physaddr(dh, DL_CURR_PHYS_ADDR, hwaddr, hwaddrlen);
1685e11c3f44Smeem 	if (retval != DLPI_SUCCESS) {
16867c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1687e11c3f44Smeem 		    "ifconfig: failed setting mac address on %s\n", name);
16887c478bd9Sstevel@tonic-gate 	}
1689e11c3f44Smeem 	dlpi_close(dh);
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 	/*
1692e11c3f44Smeem 	 * If any addresses were brought down before changing the hardware
1693e11c3f44Smeem 	 * address, bring them up again.
16947c478bd9Sstevel@tonic-gate 	 */
1695e11c3f44Smeem 	for (ifaddrp = ifaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
1696e11c3f44Smeem 		if (!ifaddr_up(ifaddrp))
1697e11c3f44Smeem 			Perror2_exit(ifaddrp->ia_name, "cannot bring up");
16987c478bd9Sstevel@tonic-gate 	}
1699e11c3f44Smeem 	ifaddrlistx_free(ifaddrs);
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	return (0);
17027c478bd9Sstevel@tonic-gate }
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate /*
17057c478bd9Sstevel@tonic-gate  * Print an interface's Ethernet address, if it has one.
17067c478bd9Sstevel@tonic-gate  */
17077c478bd9Sstevel@tonic-gate static void
17086e91bba0SGirish Moodalbail print_ifether(const char *ifname)
17097c478bd9Sstevel@tonic-gate {
17102b24ab6bSSebastien Roy 	int fd;
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	fd = socket(AF_INET, SOCK_DGRAM, 0);
17157c478bd9Sstevel@tonic-gate 	if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
17167c478bd9Sstevel@tonic-gate 		/*
17177c478bd9Sstevel@tonic-gate 		 * It's possible the interface is only configured for
17187c478bd9Sstevel@tonic-gate 		 * IPv6; check again with AF_INET6.
17197c478bd9Sstevel@tonic-gate 		 */
17207c478bd9Sstevel@tonic-gate 		(void) close(fd);
17217c478bd9Sstevel@tonic-gate 		fd = socket(AF_INET6, SOCK_DGRAM, 0);
17227c478bd9Sstevel@tonic-gate 		if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
17237c478bd9Sstevel@tonic-gate 			(void) close(fd);
17247c478bd9Sstevel@tonic-gate 			return;
17257c478bd9Sstevel@tonic-gate 		}
17267c478bd9Sstevel@tonic-gate 	}
17277c478bd9Sstevel@tonic-gate 	(void) close(fd);
17287c478bd9Sstevel@tonic-gate 
1729e11c3f44Smeem 	/* VNI and IPMP interfaces don't have MAC addresses */
1730e11c3f44Smeem 	if (lifr.lifr_flags & (IFF_VIRTUAL|IFF_IPMP))
17317c478bd9Sstevel@tonic-gate 		return;
17327c478bd9Sstevel@tonic-gate 
17332b24ab6bSSebastien Roy 	/* IP tunnels also don't have Ethernet-like MAC addresses */
17342b24ab6bSSebastien Roy 	if (ifconfig_dladm_open(ifname, DATALINK_CLASS_IPTUN, NULL) ==
17352b24ab6bSSebastien Roy 	    DLADM_STATUS_OK)
17362b24ab6bSSebastien Roy 		return;
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	dlpi_print_address(ifname);
17397c478bd9Sstevel@tonic-gate }
17407c478bd9Sstevel@tonic-gate 
1741f4b3ec61Sdh /*
17422b24ab6bSSebastien Roy  * static int find_all_interfaces(struct lifconf *lifcp, char **buf,
1743f4b3ec61Sdh  *     int64_t lifc_flags)
1744f4b3ec61Sdh  *
17452b24ab6bSSebastien Roy  * It finds all active data links.
1746f4b3ec61Sdh  *
1747f4b3ec61Sdh  * It takes in input a pointer to struct lifconf to receive interfaces
1748f4b3ec61Sdh  * informations, a **char to hold allocated buffer, and a lifc_flags.
1749f4b3ec61Sdh  *
1750f4b3ec61Sdh  * Return values:
1751f4b3ec61Sdh  *  0 = everything OK
1752f4b3ec61Sdh  * -1 = problem
1753f4b3ec61Sdh  */
1754f4b3ec61Sdh static int
17552b24ab6bSSebastien Roy find_all_interfaces(struct lifconf *lifcp, char **buf, int64_t lifc_flags)
1756f4b3ec61Sdh {
1757f4b3ec61Sdh 	unsigned bufsize;
1758f4b3ec61Sdh 	int n;
1759f4b3ec61Sdh 	ni_t *nip;
1760f4b3ec61Sdh 	struct lifreq *lifrp;
1761d4d1f7bfSVasumathi Sundaram - Sun Microsystems 	dladm_status_t status;
1762f4b3ec61Sdh 
17632b24ab6bSSebastien Roy 	if (!dlh_opened) {
17642b24ab6bSSebastien Roy 		status = ifconfig_dladm_open(NULL, 0, NULL);
17652b24ab6bSSebastien Roy 		if (status != DLADM_STATUS_OK)
17662b24ab6bSSebastien Roy 			dladmerr_exit(status, "unable to open dladm handle");
1767d4d1f7bfSVasumathi Sundaram - Sun Microsystems 	}
1768d4d1f7bfSVasumathi Sundaram - Sun Microsystems 
17692b24ab6bSSebastien Roy 	(void) dlpi_walk(ni_entry, dlh, 0);
1770f4b3ec61Sdh 
17712b24ab6bSSebastien Roy 	/* Now, translate the linked list into a struct lifreq buffer */
1772f4b3ec61Sdh 	if (num_ni == 0) {
1773f4b3ec61Sdh 		lifcp->lifc_family = AF_UNSPEC;
1774f4b3ec61Sdh 		lifcp->lifc_flags = lifc_flags;
1775f4b3ec61Sdh 		lifcp->lifc_len = 0;
1776f4b3ec61Sdh 		lifcp->lifc_buf = NULL;
1777f4b3ec61Sdh 		return (0);
1778f4b3ec61Sdh 	}
1779f4b3ec61Sdh 
1780f4b3ec61Sdh 	bufsize = num_ni * sizeof (struct lifreq);
1781f4b3ec61Sdh 	if ((*buf = malloc(bufsize)) == NULL)
1782f4b3ec61Sdh 		Perror0_exit("find_all_interfaces: malloc failed");
1783f4b3ec61Sdh 
1784f4b3ec61Sdh 	lifcp->lifc_family = AF_UNSPEC;
1785f4b3ec61Sdh 	lifcp->lifc_flags = lifc_flags;
1786f4b3ec61Sdh 	lifcp->lifc_len = bufsize;
1787f4b3ec61Sdh 	lifcp->lifc_buf = *buf;
1788f4b3ec61Sdh 
1789f4b3ec61Sdh 	for (n = 0, lifrp = lifcp->lifc_req; n < num_ni; n++, lifrp++) {
1790f4b3ec61Sdh 		nip = ni_list;
1791f4b3ec61Sdh 		(void) strncpy(lifrp->lifr_name, nip->ni_name,
1792f4b3ec61Sdh 		    sizeof (lifr.lifr_name));
1793f4b3ec61Sdh 		ni_list = nip->ni_next;
1794f4b3ec61Sdh 		free(nip);
1795f4b3ec61Sdh 	}
1796f4b3ec61Sdh 	return (0);
1797f4b3ec61Sdh }
1798f4b3ec61Sdh 
17997c478bd9Sstevel@tonic-gate /*
18007c478bd9Sstevel@tonic-gate  * Create the next unused logical interface using the original name
18017c478bd9Sstevel@tonic-gate  * and assign the address (and mask if '/<n>' is part of the address).
18027c478bd9Sstevel@tonic-gate  * Use the new logical interface for subsequent subcommands by updating
18037c478bd9Sstevel@tonic-gate  * the name variable.
18047c478bd9Sstevel@tonic-gate  *
18057c478bd9Sstevel@tonic-gate  * This allows syntax like:
18067c478bd9Sstevel@tonic-gate  *	ifconfig le0 addif 109.106.86.130 netmask + up \
18077c478bd9Sstevel@tonic-gate  *	addif 109.106.86.131 netmask + up
18087c478bd9Sstevel@tonic-gate  */
18097c478bd9Sstevel@tonic-gate /* ARGSUSED */
18107c478bd9Sstevel@tonic-gate static int
18117c478bd9Sstevel@tonic-gate addif(char *str, int64_t param)
18127c478bd9Sstevel@tonic-gate {
18137c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
18147c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
18157c478bd9Sstevel@tonic-gate 	struct sockaddr_storage mask;
18163166459dSVasumathi Sundaram 	struct sockaddr_in6 *sin6;
18173166459dSVasumathi Sundaram 	struct sockaddr_in *sin;
18186e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
18196e91bba0SGirish Moodalbail 	char cidraddr[BUFSIZ];
18203166459dSVasumathi Sundaram 	char addrstr[INET6_ADDRSTRLEN];
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	(void) strncpy(name, origname, sizeof (name));
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
18257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18267c478bd9Sstevel@tonic-gate 		    "ifconfig: addif: bad physical interface name %s\n",
18277c478bd9Sstevel@tonic-gate 		    name);
18287c478bd9Sstevel@tonic-gate 		exit(1);
18297c478bd9Sstevel@tonic-gate 	}
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	/*
18327c478bd9Sstevel@tonic-gate 	 * clear so parser will interpret next address as source followed
18337c478bd9Sstevel@tonic-gate 	 * by possible dest
18347c478bd9Sstevel@tonic-gate 	 */
18357c478bd9Sstevel@tonic-gate 	setaddr = 0;
18367c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(str, (struct sockaddr *)&laddr, &prefixlen);
18377c478bd9Sstevel@tonic-gate 
1838*60b43c45SDan McDonald 	(void) memset(&mask, 0, sizeof (mask));
1839*60b43c45SDan McDonald 	mask.ss_family = afp->af_af;
1840*60b43c45SDan McDonald 
18417c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
18427c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
18437c478bd9Sstevel@tonic-gate 		/* Nothing there - ok */
18447c478bd9Sstevel@tonic-gate 		break;
18457c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
18467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
18477c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", str);
18487c478bd9Sstevel@tonic-gate 		exit(1);
18497c478bd9Sstevel@tonic-gate 	default:
18507c478bd9Sstevel@tonic-gate 		(void) memset(&mask, 0, sizeof (mask));
18517c478bd9Sstevel@tonic-gate 		mask.ss_family = afp->af_af;
18527c478bd9Sstevel@tonic-gate 		if (afp->af_af == AF_INET6) {
18537c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&mask;
18547906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
18557c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin6->sin6_addr)) {
18567c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
18577c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
18587c478bd9Sstevel@tonic-gate 				    prefixlen);
18597c478bd9Sstevel@tonic-gate 				exit(1);
18607c478bd9Sstevel@tonic-gate 			}
18617c478bd9Sstevel@tonic-gate 		} else {
18627c478bd9Sstevel@tonic-gate 			sin = (struct sockaddr_in *)&mask;
18637906a3e0Smeem 			if (!in_prefixlentomask(prefixlen, IP_ABITS,
18647c478bd9Sstevel@tonic-gate 			    (uchar_t *)&sin->sin_addr)) {
18657c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "ifconfig: "
18667c478bd9Sstevel@tonic-gate 				    "Bad prefix length: %d\n",
18677c478bd9Sstevel@tonic-gate 				    prefixlen);
18687c478bd9Sstevel@tonic-gate 				exit(1);
18697c478bd9Sstevel@tonic-gate 			}
18707c478bd9Sstevel@tonic-gate 		}
1871dd7a6f5fSkcpoon 		g_netmask_set = G_NETMASK_NIL;
18727c478bd9Sstevel@tonic-gate 		break;
18737c478bd9Sstevel@tonic-gate 	}
18747c478bd9Sstevel@tonic-gate 
1875dd7a6f5fSkcpoon 	/*
1876dd7a6f5fSkcpoon 	 * This is a "hack" to get around the problem of SIOCLIFADDIF.  The
1877dd7a6f5fSkcpoon 	 * problem is that this ioctl does not include the netmask when
1878dd7a6f5fSkcpoon 	 * adding a logical interface.  This is the same problem described
1879dd7a6f5fSkcpoon 	 * in the ifconfig() comments.  To get around this problem, we first
1880dd7a6f5fSkcpoon 	 * add the logical interface with a 0 address.  After that, we set
1881dd7a6f5fSkcpoon 	 * the netmask if provided.  Finally we set the interface address.
1882dd7a6f5fSkcpoon 	 */
18837c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1884dd7a6f5fSkcpoon 	(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	/* Note: no need to do DAD here since the interface isn't up yet. */
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0)
18897c478bd9Sstevel@tonic-gate 		Perror0_exit("addif: SIOCLIFADDIF");
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	(void) printf("Created new logical interface %s\n",
18927c478bd9Sstevel@tonic-gate 	    lifr.lifr_name);
18937c478bd9Sstevel@tonic-gate 	(void) strncpy(name, lifr.lifr_name, sizeof (name));
18947c478bd9Sstevel@tonic-gate 
1895dd7a6f5fSkcpoon 	/*
1896dd7a6f5fSkcpoon 	 * Check and see if any "netmask" command is used and perform the
1897dd7a6f5fSkcpoon 	 * necessary operation.
1898dd7a6f5fSkcpoon 	 */
1899dd7a6f5fSkcpoon 	set_mask_lifreq(&lifr, &laddr, &mask);
19006e91bba0SGirish Moodalbail 
19016e91bba0SGirish Moodalbail 	/* This check is temporary until libipadm supports IPMP interfaces. */
19026e91bba0SGirish Moodalbail 	if (ifconfig_use_libipadm(s, name)) {
19036e91bba0SGirish Moodalbail 		/*
19046e91bba0SGirish Moodalbail 		 * We added the logical interface above before calling
19056e91bba0SGirish Moodalbail 		 * ipadm_create_addr(), because, with IPH_LEGACY, we need
19066e91bba0SGirish Moodalbail 		 * to do an addif for `ifconfig ce0 addif <addr>' but not for
19076e91bba0SGirish Moodalbail 		 * `ifconfig ce0 <addr>'. libipadm does not have a flag to
19086e91bba0SGirish Moodalbail 		 * to differentiate between these two cases. To keep it simple,
19096e91bba0SGirish Moodalbail 		 * we always create the logical interface and pass it to
19106e91bba0SGirish Moodalbail 		 * libipadm instead of requiring libipadm to addif for some
19116e91bba0SGirish Moodalbail 		 * cases and not do addif for other cases.
19126e91bba0SGirish Moodalbail 		 */
19136e91bba0SGirish Moodalbail 		istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name,
19146e91bba0SGirish Moodalbail 		    &ipaddr);
19156e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
19166e91bba0SGirish Moodalbail 			ipadmerr_exit(istatus, "addif");
19176e91bba0SGirish Moodalbail 
19183166459dSVasumathi Sundaram 		if (af == AF_INET) {
19193166459dSVasumathi Sundaram 			sin = (struct sockaddr_in *)&laddr;
19203166459dSVasumathi Sundaram 			(void) inet_ntop(AF_INET, &sin->sin_addr, addrstr,
19213166459dSVasumathi Sundaram 			    sizeof (addrstr));
19223166459dSVasumathi Sundaram 		} else {
19233166459dSVasumathi Sundaram 			sin6 = (struct sockaddr_in6 *)&laddr;
19243166459dSVasumathi Sundaram 			(void) inet_ntop(AF_INET6, &sin6->sin6_addr, addrstr,
19253166459dSVasumathi Sundaram 			    sizeof (addrstr));
19266e91bba0SGirish Moodalbail 		}
19273166459dSVasumathi Sundaram 		/*
19283166459dSVasumathi Sundaram 		 * lifr.lifr_addr, which is updated by set_mask_lifreq()
19293166459dSVasumathi Sundaram 		 * will contain the right mask to use.
19303166459dSVasumathi Sundaram 		 */
193164639aafSDarren Reed 		prefixlen = mask2plen((struct sockaddr *)&lifr.lifr_addr);
193264639aafSDarren Reed 
19333166459dSVasumathi Sundaram 		(void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d",
19343166459dSVasumathi Sundaram 		    addrstr, prefixlen);
19353166459dSVasumathi Sundaram 
19363166459dSVasumathi Sundaram 		istatus = ipadm_set_addr(ipaddr, cidraddr, af);
19373166459dSVasumathi Sundaram 
19386e91bba0SGirish Moodalbail 		if (istatus != IPADM_SUCCESS)
19396e91bba0SGirish Moodalbail 			ipadmerr_exit(istatus, "could not set address");
19406e91bba0SGirish Moodalbail 		setaddr++;
19416e91bba0SGirish Moodalbail 		/*
19426e91bba0SGirish Moodalbail 		 * address will be set by the parser after nextarg
19436e91bba0SGirish Moodalbail 		 * has been scanned
19446e91bba0SGirish Moodalbail 		 */
19456e91bba0SGirish Moodalbail 		return (0);
19466e91bba0SGirish Moodalbail 	}
19476e91bba0SGirish Moodalbail 
1948dd7a6f5fSkcpoon 	/*
1949dd7a6f5fSkcpoon 	 * Only set the netmask if "netmask" command is used or a prefix is
1950dd7a6f5fSkcpoon 	 * provided.
1951dd7a6f5fSkcpoon 	 */
1952dd7a6f5fSkcpoon 	if (g_netmask_set == G_NETMASK_SET || prefixlen >= 0) {
19536e91bba0SGirish Moodalbail 		/*
19546e91bba0SGirish Moodalbail 		 * lifr.lifr_addr already contains netmask from
19556e91bba0SGirish Moodalbail 		 * set_mask_lifreq().
19566e91bba0SGirish Moodalbail 		 */
19577c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
19587c478bd9Sstevel@tonic-gate 			Perror0_exit("addif: SIOCSLIFNETMASK");
19597c478bd9Sstevel@tonic-gate 	}
1960dd7a6f5fSkcpoon 
1961dd7a6f5fSkcpoon 	/* Finally, we set the interface address. */
1962dd7a6f5fSkcpoon 	lifr.lifr_addr = laddr;
1963dd7a6f5fSkcpoon 	if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1964dd7a6f5fSkcpoon 		Perror0_exit("SIOCSLIFADDR");
1965dd7a6f5fSkcpoon 
19667c478bd9Sstevel@tonic-gate 	/*
19677c478bd9Sstevel@tonic-gate 	 * let parser know we got a source.
19687c478bd9Sstevel@tonic-gate 	 * Next address, if given, should be dest
19697c478bd9Sstevel@tonic-gate 	 */
19707c478bd9Sstevel@tonic-gate 	setaddr++;
19717c478bd9Sstevel@tonic-gate 	return (0);
19727c478bd9Sstevel@tonic-gate }
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate /*
19757c478bd9Sstevel@tonic-gate  * Remove a logical interface based on its IP address. Unlike addif
19767c478bd9Sstevel@tonic-gate  * there is no '/<n>' here.
19777c478bd9Sstevel@tonic-gate  * Verifies that the interface is down before it is removed.
19787c478bd9Sstevel@tonic-gate  */
19797c478bd9Sstevel@tonic-gate /* ARGSUSED */
19807c478bd9Sstevel@tonic-gate static int
19817c478bd9Sstevel@tonic-gate removeif(char *str, int64_t param)
19827c478bd9Sstevel@tonic-gate {
19837c478bd9Sstevel@tonic-gate 	struct sockaddr_storage laddr;
19846e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
19856e91bba0SGirish Moodalbail 	ipadm_addr_info_t *ainfo, *ainfop;
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') != NULL) {
19887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
19897c478bd9Sstevel@tonic-gate 		    "ifconfig: removeif: bad physical interface name %s\n",
19907c478bd9Sstevel@tonic-gate 		    name);
19917c478bd9Sstevel@tonic-gate 		exit(1);
19927c478bd9Sstevel@tonic-gate 	}
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 	(*afp->af_getaddr)(str, &laddr, NULL);
19957c478bd9Sstevel@tonic-gate 
19966e91bba0SGirish Moodalbail 	/*
19976e91bba0SGirish Moodalbail 	 * Following check is temporary until libipadm supports
19986e91bba0SGirish Moodalbail 	 * IPMP interfaces.
19996e91bba0SGirish Moodalbail 	 */
20006e91bba0SGirish Moodalbail 	if (!ifconfig_use_libipadm(s, name))
20016e91bba0SGirish Moodalbail 		goto delete;
20026e91bba0SGirish Moodalbail 
20036e91bba0SGirish Moodalbail 	/*
20046e91bba0SGirish Moodalbail 	 * Get all addresses and search this address among the active
20056e91bba0SGirish Moodalbail 	 * addresses. If an address object was found, delete using
20066e91bba0SGirish Moodalbail 	 * ipadm_delete_addr().
20076e91bba0SGirish Moodalbail 	 */
20086e91bba0SGirish Moodalbail 	istatus = ipadm_addr_info(iph, name, &ainfo, 0, LIFC_DEFAULT);
20096e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS)
20106e91bba0SGirish Moodalbail 		ipadmerr_exit(istatus, "removeif");
20116e91bba0SGirish Moodalbail 
20126e91bba0SGirish Moodalbail 	for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop))
201364639aafSDarren Reed 		if (sockaddrcmp(
201464639aafSDarren Reed 		    (struct sockaddr_storage *)ainfop->ia_ifa.ifa_addr, &laddr))
20156e91bba0SGirish Moodalbail 			break;
20166e91bba0SGirish Moodalbail 
201767ce1608SVasumathi Sundaram 	if (ainfop != NULL) {
201867ce1608SVasumathi Sundaram 		if (strchr(ainfop->ia_ifa.ifa_name, ':') == NULL) {
201967ce1608SVasumathi Sundaram 			(void) fprintf(stderr,
202067ce1608SVasumathi Sundaram 			    "ifconfig: removeif: cannot remove interface: %s\n",
202167ce1608SVasumathi Sundaram 			    name);
202267ce1608SVasumathi Sundaram 			exit(1);
202367ce1608SVasumathi Sundaram 		}
202467ce1608SVasumathi Sundaram 		if (ainfop->ia_aobjname[0] != '\0') {
202567ce1608SVasumathi Sundaram 			istatus = ipadm_delete_addr(iph, ainfop->ia_aobjname,
202667ce1608SVasumathi Sundaram 			    IPADM_OPT_ACTIVE);
202767ce1608SVasumathi Sundaram 			if (istatus != IPADM_SUCCESS) {
202867ce1608SVasumathi Sundaram 				ipadmerr_exit(istatus,
202967ce1608SVasumathi Sundaram 				    "could not delete address");
203067ce1608SVasumathi Sundaram 			}
203167ce1608SVasumathi Sundaram 			ipadm_free_addr_info(ainfo);
203267ce1608SVasumathi Sundaram 			return (0);
203367ce1608SVasumathi Sundaram 		}
20346e91bba0SGirish Moodalbail 	}
20356e91bba0SGirish Moodalbail 	ipadm_free_addr_info(ainfo);
20366e91bba0SGirish Moodalbail 
20376e91bba0SGirish Moodalbail delete:
20386e91bba0SGirish Moodalbail 	/*
20396e91bba0SGirish Moodalbail 	 * An address object for this address was not found in ipadm.
20406e91bba0SGirish Moodalbail 	 * Delete with SIOCLIFREMOVEIF.
20416e91bba0SGirish Moodalbail 	 */
20426e91bba0SGirish Moodalbail 	lifr.lifr_addr = laddr;
20437c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
20447c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) {
20457c478bd9Sstevel@tonic-gate 		if (errno == EBUSY) {
20467c478bd9Sstevel@tonic-gate 			/* This can only happen if ipif_id = 0 */
20477c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
204867ce1608SVasumathi Sundaram 			    "ifconfig: removeif: cannot remove interface: %s\n",
20497c478bd9Sstevel@tonic-gate 			    name);
20507c478bd9Sstevel@tonic-gate 			exit(1);
20517c478bd9Sstevel@tonic-gate 		}
20527c478bd9Sstevel@tonic-gate 		Perror0_exit("removeif: SIOCLIFREMOVEIF");
20537c478bd9Sstevel@tonic-gate 	}
20547c478bd9Sstevel@tonic-gate 	return (0);
20557c478bd9Sstevel@tonic-gate }
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate /*
20587c478bd9Sstevel@tonic-gate  * Set the address token for IPv6.
20597c478bd9Sstevel@tonic-gate  */
20607c478bd9Sstevel@tonic-gate /* ARGSUSED */
20617c478bd9Sstevel@tonic-gate static int
20627c478bd9Sstevel@tonic-gate setiftoken(char *addr, int64_t param)
20637c478bd9Sstevel@tonic-gate {
20647c478bd9Sstevel@tonic-gate 	int prefixlen = 0;
20657c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 token;
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	in6_getaddr(addr, (struct sockaddr *)&token, &prefixlen);
20687c478bd9Sstevel@tonic-gate 	switch (prefixlen) {
20697c478bd9Sstevel@tonic-gate 	case NO_PREFIX:
20707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
20717c478bd9Sstevel@tonic-gate 		    "ifconfig: Missing prefix length in subnet %s\n", addr);
20727c478bd9Sstevel@tonic-gate 		exit(1);
20737c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
20747c478bd9Sstevel@tonic-gate 	case BAD_ADDR:
20757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
20767c478bd9Sstevel@tonic-gate 		    "ifconfig: Bad prefix length in %s\n", addr);
20777c478bd9Sstevel@tonic-gate 		exit(1);
20787c478bd9Sstevel@tonic-gate 	default:
20797c478bd9Sstevel@tonic-gate 		break;
20807c478bd9Sstevel@tonic-gate 	}
20817c478bd9Sstevel@tonic-gate 	(void) memcpy(&lifr.lifr_addr, &token, sizeof (token));
20827c478bd9Sstevel@tonic-gate 	lifr.lifr_addrlen = prefixlen;
20837c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
20847c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFTOKEN, (caddr_t)&lifr) < 0)  {
20857c478bd9Sstevel@tonic-gate 		Perror0_exit("setiftoken: SIOCSLIFTOKEN");
20867c478bd9Sstevel@tonic-gate 	}
20877c478bd9Sstevel@tonic-gate 	return (0);
20887c478bd9Sstevel@tonic-gate }
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate /* ARGSUSED */
20917c478bd9Sstevel@tonic-gate static int
2092e11c3f44Smeem setifgroupname(char *grname, int64_t param)
20937c478bd9Sstevel@tonic-gate {
2094e11c3f44Smeem 	lifgroupinfo_t		lifgr;
2095e11c3f44Smeem 	struct lifreq		lifrl;
2096e11c3f44Smeem 	ifaddrlistx_t		*ifaddrp, *nextifaddrp;
2097e11c3f44Smeem 	ifaddrlistx_t		*ifaddrs = NULL, *downaddrs = NULL;
2098e11c3f44Smeem 	int			af;
2099e11c3f44Smeem 
21007c478bd9Sstevel@tonic-gate 	if (debug) {
21017c478bd9Sstevel@tonic-gate 		(void) printf("Setting groupname %s on interface %s\n",
2102e11c3f44Smeem 		    grname, name);
21037c478bd9Sstevel@tonic-gate 	}
21047c478bd9Sstevel@tonic-gate 
2105e11c3f44Smeem 	(void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ);
2106e11c3f44Smeem 	(void) strlcpy(lifrl.lifr_groupname, grname, LIFGRNAMSIZ);
2107e11c3f44Smeem 
2108e11c3f44Smeem 	while (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) {
2109e11c3f44Smeem 		switch (errno) {
2110e11c3f44Smeem 		case ENOENT:
2111e11c3f44Smeem 			/*
2112e11c3f44Smeem 			 * The group doesn't yet exist; create it and repeat.
2113e11c3f44Smeem 			 */
2114e11c3f44Smeem 			af = afp->af_af;
2115e11c3f44Smeem 			if (create_ipmp(grname, af, NULL, _B_TRUE) == -1) {
2116e11c3f44Smeem 				if (errno == EEXIST)
2117e11c3f44Smeem 					continue;
2118e11c3f44Smeem 
2119e11c3f44Smeem 				Perror2(grname, "cannot create IPMP group");
2120e11c3f44Smeem 				goto fail;
2121e11c3f44Smeem 			}
2122e11c3f44Smeem 			continue;
2123e11c3f44Smeem 
2124e11c3f44Smeem 		case EALREADY:
2125e11c3f44Smeem 			/*
2126e11c3f44Smeem 			 * The interface is already in another group; must
2127e11c3f44Smeem 			 * remove existing membership first.
2128e11c3f44Smeem 			 */
2129e11c3f44Smeem 			lifrl.lifr_groupname[0] = '\0';
2130e11c3f44Smeem 			if (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) {
2131e11c3f44Smeem 				Perror2(name, "cannot remove existing "
2132e11c3f44Smeem 				    "IPMP group membership");
2133e11c3f44Smeem 				goto fail;
2134e11c3f44Smeem 			}
2135e11c3f44Smeem 			(void) strlcpy(lifrl.lifr_groupname, grname,
2136e11c3f44Smeem 			    LIFGRNAMSIZ);
2137e11c3f44Smeem 			continue;
2138e11c3f44Smeem 
2139e11c3f44Smeem 		case EAFNOSUPPORT:
2140e11c3f44Smeem 			/*
2141e11c3f44Smeem 			 * The group exists, but it's not configured with the
2142e11c3f44Smeem 			 * address families the interface needs.  Since only
2143e11c3f44Smeem 			 * two address families are currently supported, just
2144e11c3f44Smeem 			 * configure the "other" address family.  Note that we
2145e11c3f44Smeem 			 * may race with group deletion or creation by another
2146e11c3f44Smeem 			 * process (ENOENT or EEXIST); in such cases we repeat
2147e11c3f44Smeem 			 * our original SIOCSLIFGROUPNAME.
2148e11c3f44Smeem 			 */
2149e11c3f44Smeem 			(void) strlcpy(lifgr.gi_grname, grname, LIFGRNAMSIZ);
2150e11c3f44Smeem 			if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1) {
2151e11c3f44Smeem 				if (errno == ENOENT)
2152e11c3f44Smeem 					continue;
2153e11c3f44Smeem 
2154e11c3f44Smeem 				Perror2(grname, "SIOCGLIFGROUPINFO");
2155e11c3f44Smeem 				goto fail;
2156e11c3f44Smeem 			}
2157e11c3f44Smeem 
2158e11c3f44Smeem 			af = lifgr.gi_v4 ? AF_INET6 : AF_INET;
2159e11c3f44Smeem 			if (create_ipmp(grname, af, lifgr.gi_grifname,
2160e11c3f44Smeem 			    _B_TRUE) == -1) {
2161e11c3f44Smeem 				if (errno == EEXIST)
2162e11c3f44Smeem 					continue;
2163e11c3f44Smeem 
2164e11c3f44Smeem 				Perror2(grname, "cannot configure IPMP group");
2165e11c3f44Smeem 				goto fail;
2166e11c3f44Smeem 			}
2167e11c3f44Smeem 			continue;
2168e11c3f44Smeem 
2169e11c3f44Smeem 		case EADDRINUSE:
2170e11c3f44Smeem 			/*
2171e11c3f44Smeem 			 * Some addresses are in-use (or under control of DAD).
2172e11c3f44Smeem 			 * Bring them down and retry the group join operation.
2173e11c3f44Smeem 			 * We will bring them back up after the interface has
2174e11c3f44Smeem 			 * been placed in the group.
2175e11c3f44Smeem 			 */
2176e11c3f44Smeem 			if (ifaddrlistx(lifrl.lifr_name, IFF_UP|IFF_DUPLICATE,
2177e11c3f44Smeem 			    0, &ifaddrs) == -1) {
2178e11c3f44Smeem 				Perror2(grname, "cannot get address list");
2179e11c3f44Smeem 				goto fail;
2180e11c3f44Smeem 			}
2181e11c3f44Smeem 
2182e11c3f44Smeem 			ifaddrp = ifaddrs;
2183e11c3f44Smeem 			for (; ifaddrp != NULL; ifaddrp = nextifaddrp) {
2184e11c3f44Smeem 				if (!ifaddr_down(ifaddrp)) {
2185e11c3f44Smeem 					ifaddrs = ifaddrp;
2186e11c3f44Smeem 					goto fail;
2187e11c3f44Smeem 				}
2188e11c3f44Smeem 				nextifaddrp = ifaddrp->ia_next;
2189e11c3f44Smeem 				ifaddrp->ia_next = downaddrs;
2190e11c3f44Smeem 				downaddrs = ifaddrp;
2191e11c3f44Smeem 			}
2192e11c3f44Smeem 			ifaddrs = NULL;
2193e11c3f44Smeem 			continue;
2194e11c3f44Smeem 
2195e11c3f44Smeem 		case EADDRNOTAVAIL: {
2196e11c3f44Smeem 			/*
2197e11c3f44Smeem 			 * Some data addresses are under application control.
2198e11c3f44Smeem 			 * For some of these (e.g., ADDRCONF), the application
2199e11c3f44Smeem 			 * should remove the address, in which case we retry a
2200e11c3f44Smeem 			 * few times (since the application's action is not
2201e11c3f44Smeem 			 * atomic with respect to us) before bailing out and
2202e11c3f44Smeem 			 * informing the user.
2203e11c3f44Smeem 			 */
2204e11c3f44Smeem 			int ntries, nappaddr = 0;
2205e11c3f44Smeem 			const if_appflags_t *iap = if_appflags_tbl;
2206e11c3f44Smeem 
2207e11c3f44Smeem 			for (; iap->ia_app != NULL; iap++) {
2208e11c3f44Smeem 				ntries = 0;
2209e11c3f44Smeem again:
2210e11c3f44Smeem 				if (ifaddrlistx(lifrl.lifr_name, iap->ia_flag,
2211e11c3f44Smeem 				    IFF_NOFAILOVER, &ifaddrs) == -1) {
2212e11c3f44Smeem 					(void) fprintf(stderr, "ifconfig: %s: "
2213e11c3f44Smeem 					    "cannot get data addresses managed "
2214e11c3f44Smeem 					    "by %s\n", lifrl.lifr_name,
2215e11c3f44Smeem 					    iap->ia_app);
2216e11c3f44Smeem 					goto fail;
2217e11c3f44Smeem 				}
2218e11c3f44Smeem 
2219e11c3f44Smeem 				if (ifaddrs == NULL)
2220e11c3f44Smeem 					continue;
2221e11c3f44Smeem 
2222e11c3f44Smeem 				ifaddrlistx_free(ifaddrs);
2223e11c3f44Smeem 				ifaddrs = NULL;
2224e11c3f44Smeem 
2225e11c3f44Smeem 				if (++ntries < iap->ia_tries) {
2226e11c3f44Smeem 					(void) poll(NULL, 0, 100);
2227e11c3f44Smeem 					goto again;
2228e11c3f44Smeem 				}
2229e11c3f44Smeem 
2230e11c3f44Smeem 				(void) fprintf(stderr, "ifconfig: cannot join "
2231e11c3f44Smeem 				    "IPMP group: %s has data addresses managed "
2232e11c3f44Smeem 				    "by %s\n", lifrl.lifr_name, iap->ia_app);
2233e11c3f44Smeem 				nappaddr++;
2234e11c3f44Smeem 			}
2235e11c3f44Smeem 			if (nappaddr > 0)
2236e11c3f44Smeem 				goto fail;
2237e11c3f44Smeem 			continue;
2238e11c3f44Smeem 		}
2239e11c3f44Smeem 		default:
2240e11c3f44Smeem 			Perror2(name, "SIOCSLIFGROUPNAME");
2241e11c3f44Smeem 			goto fail;
2242e11c3f44Smeem 		}
22437c478bd9Sstevel@tonic-gate 	}
22447c478bd9Sstevel@tonic-gate 
224536b41818SGirish Moodalbail 	/*
224636b41818SGirish Moodalbail 	 * If the interface being moved is under the control of `ipmgmtd(1M)'
224736b41818SGirish Moodalbail 	 * dameon then we should inform the daemon about this move, so that
224836b41818SGirish Moodalbail 	 * the daemon can delete the state associated with this interface.
224936b41818SGirish Moodalbail 	 *
225036b41818SGirish Moodalbail 	 * This workaround is needed until the IPMP support in ipadm(1M).
225136b41818SGirish Moodalbail 	 */
225236b41818SGirish Moodalbail 	ipadm_if_move(iph, name);
225336b41818SGirish Moodalbail 
22547c478bd9Sstevel@tonic-gate 	/*
2255e11c3f44Smeem 	 * If there were addresses that we had to bring down, it's time to
2256e11c3f44Smeem 	 * bring them up again.  As part of bringing them up, the kernel will
2257e11c3f44Smeem 	 * automatically move them to the new IPMP interface.
22587c478bd9Sstevel@tonic-gate 	 */
2259e11c3f44Smeem 	for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
2260e11c3f44Smeem 		if (!ifaddr_up(ifaddrp) && errno != ENXIO) {
2261e11c3f44Smeem 			(void) fprintf(stderr, "ifconfig: cannot bring back up "
2262e11c3f44Smeem 			    "%s: %s\n", ifaddrp->ia_name, strerror(errno));
2263e11c3f44Smeem 		}
22647c478bd9Sstevel@tonic-gate 	}
2265e11c3f44Smeem 	ifaddrlistx_free(downaddrs);
2266e11c3f44Smeem 	return (0);
2267e11c3f44Smeem fail:
22687c478bd9Sstevel@tonic-gate 	/*
2269e11c3f44Smeem 	 * Attempt to bring back up any interfaces that we downed.
22707c478bd9Sstevel@tonic-gate 	 */
2271e11c3f44Smeem 	for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
2272e11c3f44Smeem 		if (!ifaddr_up(ifaddrp) && errno != ENXIO) {
2273e11c3f44Smeem 			(void) fprintf(stderr, "ifconfig: cannot bring back up "
2274e11c3f44Smeem 			    "%s: %s\n", ifaddrp->ia_name, strerror(errno));
2275e11c3f44Smeem 		}
22767c478bd9Sstevel@tonic-gate 	}
2277e11c3f44Smeem 	ifaddrlistx_free(downaddrs);
2278e11c3f44Smeem 	ifaddrlistx_free(ifaddrs);
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	/*
2281e11c3f44Smeem 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2282e11c3f44Smeem 	 * into the exit status, so we're forced to explicitly exit().
22837c478bd9Sstevel@tonic-gate 	 */
2284e11c3f44Smeem 	exit(1);
2285e11c3f44Smeem 	/* NOTREACHED */
2286e11c3f44Smeem }
22877c478bd9Sstevel@tonic-gate 
2288e11c3f44Smeem static boolean_t
2289e11c3f44Smeem modcheck(const char *ifname)
2290e11c3f44Smeem {
2291e11c3f44Smeem 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2292e11c3f44Smeem 
2293e11c3f44Smeem 	if (ioctl(s, SIOCGLIFFLAGS, &lifr) < 0) {
2294e11c3f44Smeem 		Perror0("SIOCGLIFFLAGS");
2295e11c3f44Smeem 		return (_B_FALSE);
22967c478bd9Sstevel@tonic-gate 	}
22977c478bd9Sstevel@tonic-gate 
2298e11c3f44Smeem 	if (lifr.lifr_flags & IFF_IPMP) {
2299e11c3f44Smeem 		(void) fprintf(stderr, "ifconfig: %s: module operations not"
2300e11c3f44Smeem 		    " supported on IPMP interfaces\n", ifname);
2301e11c3f44Smeem 		return (_B_FALSE);
2302e11c3f44Smeem 	}
2303e11c3f44Smeem 	if (lifr.lifr_flags & IFF_VIRTUAL) {
2304e11c3f44Smeem 		(void) fprintf(stderr, "ifconfig: %s: module operations not"
2305e11c3f44Smeem 		    " supported on virtual IP interfaces\n", ifname);
2306e11c3f44Smeem 		return (_B_FALSE);
2307e11c3f44Smeem 	}
2308e11c3f44Smeem 	return (_B_TRUE);
2309e11c3f44Smeem }
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate /*
23127c478bd9Sstevel@tonic-gate  * To list all the modules above a given network interface.
23137c478bd9Sstevel@tonic-gate  */
23147c478bd9Sstevel@tonic-gate /* ARGSUSED */
23157c478bd9Sstevel@tonic-gate static int
23167c478bd9Sstevel@tonic-gate modlist(char *null, int64_t param)
23177c478bd9Sstevel@tonic-gate {
2318fc80c0dfSnordmark 	int muxid_fd;
23197c478bd9Sstevel@tonic-gate 	int muxfd;
23207c478bd9Sstevel@tonic-gate 	int ipfd_lowstr;
23217c478bd9Sstevel@tonic-gate 	int arpfd_lowstr;
23227c478bd9Sstevel@tonic-gate 	int num_mods;
23237c478bd9Sstevel@tonic-gate 	int i;
23247c478bd9Sstevel@tonic-gate 	struct str_list strlist;
23257c478bd9Sstevel@tonic-gate 	int orig_arpid;
23267c478bd9Sstevel@tonic-gate 
2327e11c3f44Smeem 	/*
2328e11c3f44Smeem 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2329e11c3f44Smeem 	 * into the exit status, so we're forced to explicitly exit().
2330e11c3f44Smeem 	 */
2331e11c3f44Smeem 	if (!modcheck(name))
2332e11c3f44Smeem 		exit(1);
2333e11c3f44Smeem 
2334fc80c0dfSnordmark 	if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
23357c478bd9Sstevel@tonic-gate 	    &orig_arpid) < 0) {
23367c478bd9Sstevel@tonic-gate 		return (-1);
23377c478bd9Sstevel@tonic-gate 	}
23387c478bd9Sstevel@tonic-gate 	if ((num_mods = ioctl(ipfd_lowstr, I_LIST, NULL)) < 0) {
23397c478bd9Sstevel@tonic-gate 		Perror0("cannot I_LIST to get the number of modules");
23407c478bd9Sstevel@tonic-gate 	} else {
23417c478bd9Sstevel@tonic-gate 		if (debug > 0) {
23427c478bd9Sstevel@tonic-gate 			(void) printf("Listing (%d) modules above %s\n",
23437c478bd9Sstevel@tonic-gate 			    num_mods, name);
23447c478bd9Sstevel@tonic-gate 		}
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 		strlist.sl_nmods = num_mods;
23477c478bd9Sstevel@tonic-gate 		strlist.sl_modlist = malloc(sizeof (struct str_mlist) *
23487c478bd9Sstevel@tonic-gate 		    num_mods);
23497c478bd9Sstevel@tonic-gate 		if (strlist.sl_modlist == NULL) {
23507c478bd9Sstevel@tonic-gate 			Perror0("cannot malloc");
23517c478bd9Sstevel@tonic-gate 		} else {
23527c478bd9Sstevel@tonic-gate 			if (ioctl(ipfd_lowstr, I_LIST, (caddr_t)&strlist) < 0) {
23537c478bd9Sstevel@tonic-gate 				Perror0("cannot I_LIST for module names");
23547c478bd9Sstevel@tonic-gate 			} else {
23557c478bd9Sstevel@tonic-gate 				for (i = 0; i < strlist.sl_nmods; i++) {
2356fc80c0dfSnordmark 					(void) printf("%d %s\n", i,
2357fc80c0dfSnordmark 					    strlist.sl_modlist[i].l_name);
23587c478bd9Sstevel@tonic-gate 				}
23597c478bd9Sstevel@tonic-gate 			}
23607c478bd9Sstevel@tonic-gate 			free(strlist.sl_modlist);
23617c478bd9Sstevel@tonic-gate 		}
23627c478bd9Sstevel@tonic-gate 	}
2363fc80c0dfSnordmark 	return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2364fc80c0dfSnordmark 	    orig_arpid));
23657c478bd9Sstevel@tonic-gate }
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate #define	MODINSERT_OP	'i'
23687c478bd9Sstevel@tonic-gate #define	MODREMOVE_OP	'r'
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate /*
23717c478bd9Sstevel@tonic-gate  * To insert a module to the stream of the interface.  It is just a
23727c478bd9Sstevel@tonic-gate  * wrapper.  The real function is modop().
23737c478bd9Sstevel@tonic-gate  */
23747c478bd9Sstevel@tonic-gate /* ARGSUSED */
23757c478bd9Sstevel@tonic-gate static int
23767c478bd9Sstevel@tonic-gate modinsert(char *arg, int64_t param)
23777c478bd9Sstevel@tonic-gate {
23787c478bd9Sstevel@tonic-gate 	return (modop(arg, MODINSERT_OP));
23797c478bd9Sstevel@tonic-gate }
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate /*
23827c478bd9Sstevel@tonic-gate  * To remove a module from the stream of the interface.  It is just a
23837c478bd9Sstevel@tonic-gate  * wrapper.  The real function is modop().
23847c478bd9Sstevel@tonic-gate  */
23857c478bd9Sstevel@tonic-gate /* ARGSUSED */
23867c478bd9Sstevel@tonic-gate static int
23877c478bd9Sstevel@tonic-gate modremove(char *arg, int64_t param)
23887c478bd9Sstevel@tonic-gate {
23897c478bd9Sstevel@tonic-gate 	return (modop(arg, MODREMOVE_OP));
23907c478bd9Sstevel@tonic-gate }
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate /*
23937c478bd9Sstevel@tonic-gate  * Helper function for mod*() functions.  It gets a fd to the lower IP
23947c478bd9Sstevel@tonic-gate  * stream and I_PUNLINK's the lower stream.  It also initializes the
23957c478bd9Sstevel@tonic-gate  * global variable lifr.
23967c478bd9Sstevel@tonic-gate  *
23977c478bd9Sstevel@tonic-gate  * Param:
2398fc80c0dfSnordmark  *	int *muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2399fc80c0dfSnordmark  *	int *muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2400fc80c0dfSnordmark  *	int *ipfd_lowstr: fd to the lower IP stream.
2401fc80c0dfSnordmark  *	int *arpfd_lowstr: fd to the lower ARP stream.
24027c478bd9Sstevel@tonic-gate  *
24037c478bd9Sstevel@tonic-gate  * Return:
24047c478bd9Sstevel@tonic-gate  *	-1 if operation fails, 0 otherwise.
24057c478bd9Sstevel@tonic-gate  *
2406e11c3f44Smeem  * Please see the big block comment above ifplumb() for the logic of the
2407e11c3f44Smeem  * PLINK/PUNLINK
24087c478bd9Sstevel@tonic-gate  */
24097c478bd9Sstevel@tonic-gate static int
2410fc80c0dfSnordmark ip_domux2fd(int *muxfd, int *muxid_fd, int *ipfd_lowstr, int *arpfd_lowstr,
2411fc80c0dfSnordmark     int *orig_arpid)
24127c478bd9Sstevel@tonic-gate {
24137c478bd9Sstevel@tonic-gate 	uint64_t	flags;
24147c478bd9Sstevel@tonic-gate 	char		*udp_dev_name;
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 	*orig_arpid = 0;
24177c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
24187c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
24197c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
24207c478bd9Sstevel@tonic-gate 	}
24217c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
24227c478bd9Sstevel@tonic-gate 	if (flags & IFF_IPV4) {
24237c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP_DEV_NAME;
24247c478bd9Sstevel@tonic-gate 	} else if (flags & IFF_IPV6) {
24257c478bd9Sstevel@tonic-gate 		udp_dev_name = UDP6_DEV_NAME;
24267c478bd9Sstevel@tonic-gate 	} else {
24277c478bd9Sstevel@tonic-gate 		return (-1);
24287c478bd9Sstevel@tonic-gate 	}
24297c478bd9Sstevel@tonic-gate 
2430fc80c0dfSnordmark 	if ((*muxid_fd = open(udp_dev_name, O_RDWR)) < 0) {
2431fc80c0dfSnordmark 		Perror2("open", udp_dev_name);
24327c478bd9Sstevel@tonic-gate 		return (-1);
24337c478bd9Sstevel@tonic-gate 	}
2434fc80c0dfSnordmark 	if (ioctl(*muxid_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
2435fc80c0dfSnordmark 		Perror2("SIOCGLIFMUXID", udp_dev_name);
24367c478bd9Sstevel@tonic-gate 		return (-1);
24377c478bd9Sstevel@tonic-gate 	}
24387c478bd9Sstevel@tonic-gate 	if (debug > 0) {
24397c478bd9Sstevel@tonic-gate 		(void) printf("ARP_muxid %d IP_muxid %d\n",
24407c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid, lifr.lifr_ip_muxid);
24417c478bd9Sstevel@tonic-gate 	}
24427c478bd9Sstevel@tonic-gate 
2443fc80c0dfSnordmark 	/*
2444fc80c0dfSnordmark 	 * Use /dev/udp{,6} as the mux to avoid linkcycles.
2445fc80c0dfSnordmark 	 */
24466e91bba0SGirish Moodalbail 	if (ipadm_open_arp_on_udp(udp_dev_name, muxfd) != IPADM_SUCCESS)
24477c478bd9Sstevel@tonic-gate 		return (-1);
24487c478bd9Sstevel@tonic-gate 
24497c478bd9Sstevel@tonic-gate 	if (lifr.lifr_arp_muxid != 0) {
24507c478bd9Sstevel@tonic-gate 		if ((*arpfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
24517c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid)) < 0) {
24527c478bd9Sstevel@tonic-gate 			if ((errno == EINVAL) &&
24537c478bd9Sstevel@tonic-gate 			    (flags & (IFF_NOARP | IFF_IPV6))) {
24547c478bd9Sstevel@tonic-gate 				/*
24557c478bd9Sstevel@tonic-gate 				 * Some plumbing utilities set the muxid to
24567c478bd9Sstevel@tonic-gate 				 * -1 or some invalid value to signify that
24577c478bd9Sstevel@tonic-gate 				 * there is no arp stream. Set the muxid to 0
24587c478bd9Sstevel@tonic-gate 				 * before trying to unplumb the IP stream.
24597c478bd9Sstevel@tonic-gate 				 * IP does not allow the IP stream to be
24607c478bd9Sstevel@tonic-gate 				 * unplumbed if it sees a non-null arp muxid,
24617c478bd9Sstevel@tonic-gate 				 * for consistency of IP-ARP streams.
24627c478bd9Sstevel@tonic-gate 				 */
24637c478bd9Sstevel@tonic-gate 				*orig_arpid = lifr.lifr_arp_muxid;
24647c478bd9Sstevel@tonic-gate 				lifr.lifr_arp_muxid = 0;
2465fc80c0dfSnordmark 				(void) ioctl(*muxid_fd, SIOCSLIFMUXID,
24667c478bd9Sstevel@tonic-gate 				    (caddr_t)&lifr);
24677c478bd9Sstevel@tonic-gate 				*arpfd_lowstr = -1;
24687c478bd9Sstevel@tonic-gate 			} else {
24697c478bd9Sstevel@tonic-gate 				Perror0("_I_MUXID2FD");
24707c478bd9Sstevel@tonic-gate 				return (-1);
24717c478bd9Sstevel@tonic-gate 			}
24727c478bd9Sstevel@tonic-gate 		} else if (ioctl(*muxfd, I_PUNLINK,
24737c478bd9Sstevel@tonic-gate 		    lifr.lifr_arp_muxid) < 0) {
24747c478bd9Sstevel@tonic-gate 			Perror2("I_PUNLINK", udp_dev_name);
24757c478bd9Sstevel@tonic-gate 			return (-1);
24767c478bd9Sstevel@tonic-gate 		}
24777c478bd9Sstevel@tonic-gate 	} else {
24787c478bd9Sstevel@tonic-gate 		*arpfd_lowstr = -1;
24797c478bd9Sstevel@tonic-gate 	}
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	if ((*ipfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
24827c478bd9Sstevel@tonic-gate 	    lifr.lifr_ip_muxid)) < 0) {
24837c478bd9Sstevel@tonic-gate 		Perror0("_I_MUXID2FD");
24847c478bd9Sstevel@tonic-gate 		/* Undo any changes we made */
24857c478bd9Sstevel@tonic-gate 		if (*orig_arpid != 0) {
24867c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = *orig_arpid;
2487fc80c0dfSnordmark 			(void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
24887c478bd9Sstevel@tonic-gate 		}
24897c478bd9Sstevel@tonic-gate 		return (-1);
24907c478bd9Sstevel@tonic-gate 	}
24917c478bd9Sstevel@tonic-gate 	if (ioctl(*muxfd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) {
24927c478bd9Sstevel@tonic-gate 		Perror2("I_PUNLINK", udp_dev_name);
24937c478bd9Sstevel@tonic-gate 		/* Undo any changes we made */
24947c478bd9Sstevel@tonic-gate 		if (*orig_arpid != 0) {
24957c478bd9Sstevel@tonic-gate 			lifr.lifr_arp_muxid = *orig_arpid;
2496fc80c0dfSnordmark 			(void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
24977c478bd9Sstevel@tonic-gate 		}
24987c478bd9Sstevel@tonic-gate 		return (-1);
24997c478bd9Sstevel@tonic-gate 	}
25007c478bd9Sstevel@tonic-gate 	return (0);
25017c478bd9Sstevel@tonic-gate }
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate /*
25047c478bd9Sstevel@tonic-gate  * Helper function for mod*() functions.  It I_PLINK's back the upper and
25057c478bd9Sstevel@tonic-gate  * lower IP streams.  Note that this function must be called after
25067c478bd9Sstevel@tonic-gate  * ip_domux2fd().  In ip_domux2fd(), the global variable lifr is initialized
25077c478bd9Sstevel@tonic-gate  * and ip_plink() needs information in lifr.  So ip_domux2fd() and ip_plink()
25087c478bd9Sstevel@tonic-gate  * must be called in pairs.
25097c478bd9Sstevel@tonic-gate  *
25107c478bd9Sstevel@tonic-gate  * Param:
2511fc80c0dfSnordmark  *	int muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2512fc80c0dfSnordmark  *	int muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2513fc80c0dfSnordmark  *	int ipfd_lowstr: fd to the lower IP stream.
2514fc80c0dfSnordmark  *	int arpfd_lowstr: fd to the lower ARP stream.
25157c478bd9Sstevel@tonic-gate  *
25167c478bd9Sstevel@tonic-gate  * Return:
25177c478bd9Sstevel@tonic-gate  *	-1 if operation fails, 0 otherwise.
25187c478bd9Sstevel@tonic-gate  *
2519e11c3f44Smeem  * Please see the big block comment above ifplumb() for the logic of the
2520e11c3f44Smeem  * PLINK/PUNLINK
25217c478bd9Sstevel@tonic-gate  */
25227c478bd9Sstevel@tonic-gate static int
2523fc80c0dfSnordmark ip_plink(int muxfd, int muxid_fd, int ipfd_lowstr, int arpfd_lowstr,
2524fc80c0dfSnordmark     int orig_arpid)
25257c478bd9Sstevel@tonic-gate {
25267c478bd9Sstevel@tonic-gate 	int ip_muxid;
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	ip_muxid = ioctl(muxfd, I_PLINK, ipfd_lowstr);
25297c478bd9Sstevel@tonic-gate 	if (ip_muxid < 0) {
25307c478bd9Sstevel@tonic-gate 		Perror2("I_PLINK", UDP_DEV_NAME);
25317c478bd9Sstevel@tonic-gate 		return (-1);
25327c478bd9Sstevel@tonic-gate 	}
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate 	/*
25357c478bd9Sstevel@tonic-gate 	 * If there is an arp stream, plink it. If there is no
25367c478bd9Sstevel@tonic-gate 	 * arp stream, then it is possible that the plumbing
25377c478bd9Sstevel@tonic-gate 	 * utility could have stored any value in the arp_muxid.
25387c478bd9Sstevel@tonic-gate 	 * If so, restore it from orig_arpid.
25397c478bd9Sstevel@tonic-gate 	 */
25407c478bd9Sstevel@tonic-gate 	if (arpfd_lowstr != -1) {
25417c478bd9Sstevel@tonic-gate 		if (ioctl(muxfd, I_PLINK, arpfd_lowstr) < 0) {
25427c478bd9Sstevel@tonic-gate 			Perror2("I_PLINK", UDP_DEV_NAME);
25437c478bd9Sstevel@tonic-gate 			return (-1);
25447c478bd9Sstevel@tonic-gate 		}
25457c478bd9Sstevel@tonic-gate 	} else if (orig_arpid != 0) {
25467c478bd9Sstevel@tonic-gate 		/* Undo the changes we did in ip_domux2fd */
25477c478bd9Sstevel@tonic-gate 		lifr.lifr_arp_muxid = orig_arpid;
25487c478bd9Sstevel@tonic-gate 		lifr.lifr_ip_muxid = ip_muxid;
2549fc80c0dfSnordmark 		(void) ioctl(muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
25507c478bd9Sstevel@tonic-gate 	}
25517c478bd9Sstevel@tonic-gate 
2552fc80c0dfSnordmark 	(void) close(muxfd);
2553fc80c0dfSnordmark 	(void) close(muxid_fd);
25547c478bd9Sstevel@tonic-gate 	return (0);
25557c478bd9Sstevel@tonic-gate }
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate /*
25587c478bd9Sstevel@tonic-gate  * The real function to perform module insertion/removal.
25597c478bd9Sstevel@tonic-gate  *
25607c478bd9Sstevel@tonic-gate  * Param:
25617c478bd9Sstevel@tonic-gate  *	char *arg: the argument string module_name@position
25627c478bd9Sstevel@tonic-gate  *	char op: operation, either MODINSERT_OP or MODREMOVE_OP.
25637c478bd9Sstevel@tonic-gate  *
25647c478bd9Sstevel@tonic-gate  * Return:
25657c478bd9Sstevel@tonic-gate  *	Before doing ip_domux2fd(), this function calls exit(1) in case of
25667c478bd9Sstevel@tonic-gate  *	error.  After ip_domux2fd() is done, it returns -1 for error, 0
25677c478bd9Sstevel@tonic-gate  *	otherwise.
25687c478bd9Sstevel@tonic-gate  */
25697c478bd9Sstevel@tonic-gate static int
25707c478bd9Sstevel@tonic-gate modop(char *arg, char op)
25717c478bd9Sstevel@tonic-gate {
25727c478bd9Sstevel@tonic-gate 	char *pos_p;
25737c478bd9Sstevel@tonic-gate 	int muxfd;
2574fc80c0dfSnordmark 	int muxid_fd;
25757c478bd9Sstevel@tonic-gate 	int ipfd_lowstr;  /* IP stream (lower stream of mux) to be plinked */
25767c478bd9Sstevel@tonic-gate 	int arpfd_lowstr; /* ARP stream (lower stream of mux) to be plinked */
25777c478bd9Sstevel@tonic-gate 	struct strmodconf mod;
25787c478bd9Sstevel@tonic-gate 	char *at_char = "@";
25797c478bd9Sstevel@tonic-gate 	char *arg_str;
25807c478bd9Sstevel@tonic-gate 	int orig_arpid;
25817c478bd9Sstevel@tonic-gate 
2582e11c3f44Smeem 	/*
2583e11c3f44Smeem 	 * We'd return -1, but foreachinterface() doesn't propagate the error
2584e11c3f44Smeem 	 * into the exit status, so we're forced to explicitly exit().
2585e11c3f44Smeem 	 */
2586e11c3f44Smeem 	if (!modcheck(name))
2587e11c3f44Smeem 		exit(1);
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate 	/* Need to save the original string for -a option. */
25907c478bd9Sstevel@tonic-gate 	if ((arg_str = malloc(strlen(arg) + 1)) == NULL) {
25917c478bd9Sstevel@tonic-gate 		Perror0("cannot malloc");
25927c478bd9Sstevel@tonic-gate 		return (-1);
25937c478bd9Sstevel@tonic-gate 	}
25947c478bd9Sstevel@tonic-gate 	(void) strcpy(arg_str, arg);
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate 	if (*arg_str == *at_char) {
25977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
25987c478bd9Sstevel@tonic-gate 		    "ifconfig: must supply a module name\n");
25997c478bd9Sstevel@tonic-gate 		exit(1);
26007c478bd9Sstevel@tonic-gate 	}
26017c478bd9Sstevel@tonic-gate 	mod.mod_name = strtok(arg_str, at_char);
26027c478bd9Sstevel@tonic-gate 	if (strlen(mod.mod_name) > FMNAMESZ) {
26037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: module name too long: %s\n",
26047c478bd9Sstevel@tonic-gate 		    mod.mod_name);
26057c478bd9Sstevel@tonic-gate 		exit(1);
26067c478bd9Sstevel@tonic-gate 	}
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate 	/*
26097c478bd9Sstevel@tonic-gate 	 * Need to make sure that the core TCP/IP stack modules are not
26107c478bd9Sstevel@tonic-gate 	 * removed.  Otherwise, "bad" things can happen.  If a module
26117c478bd9Sstevel@tonic-gate 	 * is removed and inserted back, it loses its old state.  But
26127c478bd9Sstevel@tonic-gate 	 * the modules above it still have the old state.  E.g. IP assumes
26137c478bd9Sstevel@tonic-gate 	 * fast data path while tunnel after re-inserted assumes that it can
26147c478bd9Sstevel@tonic-gate 	 * receive M_DATA only in fast data path for which it does not have
26157c478bd9Sstevel@tonic-gate 	 * any state.  This is a general caveat of _I_REMOVE/_I_INSERT.
26167c478bd9Sstevel@tonic-gate 	 */
26177c478bd9Sstevel@tonic-gate 	if (op == MODREMOVE_OP &&
26187c478bd9Sstevel@tonic-gate 	    (strcmp(mod.mod_name, ARP_MOD_NAME) == 0 ||
26192b24ab6bSSebastien Roy 	    strcmp(mod.mod_name, IP_MOD_NAME) == 0)) {
26207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: cannot remove %s\n",
26217c478bd9Sstevel@tonic-gate 		    mod.mod_name);
26227c478bd9Sstevel@tonic-gate 		exit(1);
26237c478bd9Sstevel@tonic-gate 	}
26247c478bd9Sstevel@tonic-gate 
26257c478bd9Sstevel@tonic-gate 	if ((pos_p = strtok(NULL, at_char)) == NULL) {
26267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: must supply a position\n");
26277c478bd9Sstevel@tonic-gate 		exit(1);
26287c478bd9Sstevel@tonic-gate 	}
26297c478bd9Sstevel@tonic-gate 	mod.pos = atoi(pos_p);
26307c478bd9Sstevel@tonic-gate 
2631fc80c0dfSnordmark 	if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
26327c478bd9Sstevel@tonic-gate 	    &orig_arpid) < 0) {
26337c478bd9Sstevel@tonic-gate 		free(arg_str);
26347c478bd9Sstevel@tonic-gate 		return (-1);
26357c478bd9Sstevel@tonic-gate 	}
26367c478bd9Sstevel@tonic-gate 	switch (op) {
26377c478bd9Sstevel@tonic-gate 	case MODINSERT_OP:
26387c478bd9Sstevel@tonic-gate 		if (debug > 0) {
26397c478bd9Sstevel@tonic-gate 			(void) printf("Inserting module %s at %d\n",
26407c478bd9Sstevel@tonic-gate 			    mod.mod_name, mod.pos);
26417c478bd9Sstevel@tonic-gate 		}
26427c478bd9Sstevel@tonic-gate 		if (ioctl(ipfd_lowstr, _I_INSERT, (caddr_t)&mod) < 0) {
26437c478bd9Sstevel@tonic-gate 			Perror2("fail to insert module", mod.mod_name);
26447c478bd9Sstevel@tonic-gate 		}
26457c478bd9Sstevel@tonic-gate 		break;
26467c478bd9Sstevel@tonic-gate 	case MODREMOVE_OP:
26477c478bd9Sstevel@tonic-gate 		if (debug > 0) {
26487c478bd9Sstevel@tonic-gate 			(void) printf("Removing module %s at %d\n",
26497c478bd9Sstevel@tonic-gate 			    mod.mod_name, mod.pos);
26507c478bd9Sstevel@tonic-gate 		}
26517c478bd9Sstevel@tonic-gate 		if (ioctl(ipfd_lowstr, _I_REMOVE, (caddr_t)&mod) < 0) {
26527c478bd9Sstevel@tonic-gate 			Perror2("fail to remove module", mod.mod_name);
26537c478bd9Sstevel@tonic-gate 		}
26547c478bd9Sstevel@tonic-gate 		break;
26557c478bd9Sstevel@tonic-gate 	default:
26567c478bd9Sstevel@tonic-gate 		/* Should never get to here. */
26577c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Unknown operation\n");
26587c478bd9Sstevel@tonic-gate 		break;
26597c478bd9Sstevel@tonic-gate 	}
26607c478bd9Sstevel@tonic-gate 	free(arg_str);
2661fc80c0dfSnordmark 	return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2662fc80c0dfSnordmark 	    orig_arpid));
26637c478bd9Sstevel@tonic-gate }
26647c478bd9Sstevel@tonic-gate 
26652b24ab6bSSebastien Roy static int
26662b24ab6bSSebastien Roy modify_tun(iptun_params_t *params)
26672b24ab6bSSebastien Roy {
26682b24ab6bSSebastien Roy 	dladm_status_t status;
26692b24ab6bSSebastien Roy 
26702b24ab6bSSebastien Roy 	if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN,
26712b24ab6bSSebastien Roy 	    &params->iptun_param_linkid)) == DLADM_STATUS_OK)
26722b24ab6bSSebastien Roy 		status = dladm_iptun_modify(dlh, params, DLADM_OPT_ACTIVE);
26732b24ab6bSSebastien Roy 	if (status != DLADM_STATUS_OK)
26742b24ab6bSSebastien Roy 		dladmerr_exit(status, name);
26752b24ab6bSSebastien Roy 	return (0);
26762b24ab6bSSebastien Roy }
26772b24ab6bSSebastien Roy 
26787c478bd9Sstevel@tonic-gate /*
26797c478bd9Sstevel@tonic-gate  * Set tunnel source address
26807c478bd9Sstevel@tonic-gate  */
26817c478bd9Sstevel@tonic-gate /* ARGSUSED */
26827c478bd9Sstevel@tonic-gate static int
26837c478bd9Sstevel@tonic-gate setiftsrc(char *addr, int64_t param)
26847c478bd9Sstevel@tonic-gate {
26852b24ab6bSSebastien Roy 	iptun_params_t params;
26862b24ab6bSSebastien Roy 
26872b24ab6bSSebastien Roy 	params.iptun_param_flags = IPTUN_PARAM_LADDR;
26882b24ab6bSSebastien Roy 	(void) strlcpy(params.iptun_param_laddr, addr,
26892b24ab6bSSebastien Roy 	    sizeof (params.iptun_param_laddr));
26902b24ab6bSSebastien Roy 	return (modify_tun(&params));
26917c478bd9Sstevel@tonic-gate }
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate /*
26947c478bd9Sstevel@tonic-gate  * Set tunnel destination address
26957c478bd9Sstevel@tonic-gate  */
26967c478bd9Sstevel@tonic-gate /* ARGSUSED */
26977c478bd9Sstevel@tonic-gate static int
26987c478bd9Sstevel@tonic-gate setiftdst(char *addr, int64_t param)
26997c478bd9Sstevel@tonic-gate {
27002b24ab6bSSebastien Roy 	iptun_params_t params;
27012b24ab6bSSebastien Roy 
27022b24ab6bSSebastien Roy 	params.iptun_param_flags = IPTUN_PARAM_RADDR;
27032b24ab6bSSebastien Roy 	(void) strlcpy(params.iptun_param_raddr, addr,
27042b24ab6bSSebastien Roy 	    sizeof (params.iptun_param_raddr));
27052b24ab6bSSebastien Roy 	return (modify_tun(&params));
27067c478bd9Sstevel@tonic-gate }
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate static int
27092b24ab6bSSebastien Roy set_tun_prop(const char *propname, char *value)
27107c478bd9Sstevel@tonic-gate {
27112b24ab6bSSebastien Roy 	dladm_status_t	status;
27122b24ab6bSSebastien Roy 	datalink_id_t	linkid;
27137c478bd9Sstevel@tonic-gate 
27142b24ab6bSSebastien Roy 	status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid);
27152b24ab6bSSebastien Roy 	if (status == DLADM_STATUS_OK) {
27162b24ab6bSSebastien Roy 		status = dladm_set_linkprop(dlh, linkid, propname, &value, 1,
27172b24ab6bSSebastien Roy 		    DLADM_OPT_ACTIVE);
27187c478bd9Sstevel@tonic-gate 	}
27192b24ab6bSSebastien Roy 	if (status != DLADM_STATUS_OK)
27202b24ab6bSSebastien Roy 		dladmerr_exit(status, name);
27217c478bd9Sstevel@tonic-gate 	return (0);
27227c478bd9Sstevel@tonic-gate }
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate /* Set tunnel encapsulation limit. */
27257c478bd9Sstevel@tonic-gate /* ARGSUSED */
27267c478bd9Sstevel@tonic-gate static int
27277c478bd9Sstevel@tonic-gate set_tun_encap_limit(char *arg, int64_t param)
27287c478bd9Sstevel@tonic-gate {
27292b24ab6bSSebastien Roy 	return (set_tun_prop("encaplimit", arg));
27307c478bd9Sstevel@tonic-gate }
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate /* Disable encapsulation limit. */
27337c478bd9Sstevel@tonic-gate /* ARGSUSED */
27347c478bd9Sstevel@tonic-gate static int
27357c478bd9Sstevel@tonic-gate clr_tun_encap_limit(char *arg, int64_t param)
27367c478bd9Sstevel@tonic-gate {
27372b24ab6bSSebastien Roy 	return (set_tun_encap_limit("-1", 0));
27387c478bd9Sstevel@tonic-gate }
27397c478bd9Sstevel@tonic-gate 
27407c478bd9Sstevel@tonic-gate /* Set tunnel hop limit. */
27417c478bd9Sstevel@tonic-gate /* ARGSUSED */
27427c478bd9Sstevel@tonic-gate static int
27437c478bd9Sstevel@tonic-gate set_tun_hop_limit(char *arg, int64_t param)
27447c478bd9Sstevel@tonic-gate {
27452b24ab6bSSebastien Roy 	return (set_tun_prop("hoplimit", arg));
27467c478bd9Sstevel@tonic-gate }
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate /* Set zone ID */
27497c478bd9Sstevel@tonic-gate static int
27507c478bd9Sstevel@tonic-gate setzone(char *arg, int64_t param)
27517c478bd9Sstevel@tonic-gate {
27527c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = GLOBAL_ZONEID;
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	if (param == NEXTARG) {
27557c478bd9Sstevel@tonic-gate 		/* zone must be active */
27567c478bd9Sstevel@tonic-gate 		if ((zoneid = getzoneidbyname(arg)) == -1) {
27577c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
27587c478bd9Sstevel@tonic-gate 			    "ifconfig: unknown zone '%s'\n", arg);
27597c478bd9Sstevel@tonic-gate 			exit(1);
27607c478bd9Sstevel@tonic-gate 		}
27617c478bd9Sstevel@tonic-gate 	}
27627c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
27637c478bd9Sstevel@tonic-gate 	lifr.lifr_zoneid = zoneid;
27647c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
27657c478bd9Sstevel@tonic-gate 		Perror0_exit("SIOCSLIFZONE");
27667c478bd9Sstevel@tonic-gate 	return (0);
27677c478bd9Sstevel@tonic-gate }
27687c478bd9Sstevel@tonic-gate 
276945916cd2Sjpk /* Put interface into all zones */
277045916cd2Sjpk /* ARGSUSED */
277145916cd2Sjpk static int
277245916cd2Sjpk setallzones(char *arg, int64_t param)
277345916cd2Sjpk {
277445916cd2Sjpk 	(void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
277545916cd2Sjpk 	lifr.lifr_zoneid = ALL_ZONES;
277645916cd2Sjpk 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
277745916cd2Sjpk 		Perror0_exit("SIOCSLIFZONE");
277845916cd2Sjpk 	return (0);
277945916cd2Sjpk }
278045916cd2Sjpk 
27817c478bd9Sstevel@tonic-gate /* Set source address to use */
27827c478bd9Sstevel@tonic-gate /* ARGSUSED */
27837c478bd9Sstevel@tonic-gate static int
27847c478bd9Sstevel@tonic-gate setifsrc(char *arg, int64_t param)
27857c478bd9Sstevel@tonic-gate {
27867c478bd9Sstevel@tonic-gate 	uint_t ifindex = 0;
27877c478bd9Sstevel@tonic-gate 	int rval;
27887c478bd9Sstevel@tonic-gate 
27897c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	/*
27927c478bd9Sstevel@tonic-gate 	 * Argument can be either an interface name or "none". The latter means
27937c478bd9Sstevel@tonic-gate 	 * that any previous selection is cleared.
27947c478bd9Sstevel@tonic-gate 	 */
27957c478bd9Sstevel@tonic-gate 
27966e91bba0SGirish Moodalbail 	if (strchr(arg, ':') != NULL) {
27977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
27986e91bba0SGirish Moodalbail 		    "ifconfig: Cannot specify logical interface for usesrc \n");
27997c478bd9Sstevel@tonic-gate 		exit(1);
28007c478bd9Sstevel@tonic-gate 	}
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 	rval = strcmp(arg, NONE_STR);
28037c478bd9Sstevel@tonic-gate 	if (rval != 0) {
28047c478bd9Sstevel@tonic-gate 		if ((ifindex = if_nametoindex(arg)) == 0) {
28057c478bd9Sstevel@tonic-gate 			(void) strncpy(lifr.lifr_name, arg, LIFNAMSIZ);
28067c478bd9Sstevel@tonic-gate 			Perror0_exit("Could not get interface index");
28077c478bd9Sstevel@tonic-gate 		}
28087c478bd9Sstevel@tonic-gate 		lifr.lifr_index = ifindex;
28097c478bd9Sstevel@tonic-gate 	} else {
28107c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) != 0)
28117c478bd9Sstevel@tonic-gate 			Perror0_exit("Not a valid usesrc consumer");
28127c478bd9Sstevel@tonic-gate 		lifr.lifr_index = 0;
28137c478bd9Sstevel@tonic-gate 	}
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	if (debug)
28167c478bd9Sstevel@tonic-gate 		(void) printf("setifsrc: lifr_name %s, lifr_index %d\n",
28177c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, lifr.lifr_index);
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCSLIFUSESRC, (caddr_t)&lifr) == -1) {
28207c478bd9Sstevel@tonic-gate 		if (rval == 0)
28217c478bd9Sstevel@tonic-gate 			Perror0_exit("Cannot reset usesrc group");
28227c478bd9Sstevel@tonic-gate 		else
28237c478bd9Sstevel@tonic-gate 			Perror0_exit("Could not set source interface");
28247c478bd9Sstevel@tonic-gate 	}
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 	return (0);
28277c478bd9Sstevel@tonic-gate }
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate /*
28307c478bd9Sstevel@tonic-gate  * Print the interface status line associated with `ifname'
28317c478bd9Sstevel@tonic-gate  */
28327c478bd9Sstevel@tonic-gate static void
28337c478bd9Sstevel@tonic-gate ifstatus(const char *ifname)
28347c478bd9Sstevel@tonic-gate {
28357c478bd9Sstevel@tonic-gate 	uint64_t flags;
28367c478bd9Sstevel@tonic-gate 	char if_usesrc_name[LIFNAMSIZ];
28377c478bd9Sstevel@tonic-gate 	char *newbuf;
28387c478bd9Sstevel@tonic-gate 	int n, numifs, rval = 0;
28397c478bd9Sstevel@tonic-gate 	struct lifreq *lifrp;
28407c478bd9Sstevel@tonic-gate 	struct lifsrcof lifs;
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
28437c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
28447c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
28457c478bd9Sstevel@tonic-gate 	}
28467c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 	/*
28497c478bd9Sstevel@tonic-gate 	 * In V4 compatibility mode, we don't print the IFF_IPV4 flag or
28507c478bd9Sstevel@tonic-gate 	 * interfaces with IFF_IPV6 set.
28517c478bd9Sstevel@tonic-gate 	 */
28527c478bd9Sstevel@tonic-gate 	if (v4compat) {
28537c478bd9Sstevel@tonic-gate 		flags &= ~IFF_IPV4;
28547c478bd9Sstevel@tonic-gate 		if (flags & IFF_IPV6)
28557c478bd9Sstevel@tonic-gate 			return;
28567c478bd9Sstevel@tonic-gate 	}
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 	(void) printf("%s: ", ifname);
28597c478bd9Sstevel@tonic-gate 	print_flags(flags);
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
28627c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
28637c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFMETRIC");
28647c478bd9Sstevel@tonic-gate 	} else {
28657c478bd9Sstevel@tonic-gate 		if (lifr.lifr_metric)
28667c478bd9Sstevel@tonic-gate 			(void) printf(" metric %d", lifr.lifr_metric);
28677c478bd9Sstevel@tonic-gate 	}
28687c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
2869c08e5e1aSdr 		(void) printf(" mtu %u", lifr.lifr_mtu);
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 	/* don't print index or zone when in compatibility mode */
28727c478bd9Sstevel@tonic-gate 	if (!v4compat) {
28737c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
28747c478bd9Sstevel@tonic-gate 			(void) printf(" index %d", lifr.lifr_index);
2875f4b3ec61Sdh 		/*
2876f4b3ec61Sdh 		 * Stack instances use GLOBAL_ZONEID for IP data structures
2877f4b3ec61Sdh 		 * even in the non-global zone.
2878f4b3ec61Sdh 		 */
28797c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifr) >= 0 &&
2880f4b3ec61Sdh 		    lifr.lifr_zoneid != getzoneid() &&
2881f4b3ec61Sdh 		    lifr.lifr_zoneid != GLOBAL_ZONEID) {
28827c478bd9Sstevel@tonic-gate 			char zone_name[ZONENAME_MAX];
28837c478bd9Sstevel@tonic-gate 
288445916cd2Sjpk 			if (lifr.lifr_zoneid == ALL_ZONES) {
288545916cd2Sjpk 				(void) printf("\n\tall-zones");
288645916cd2Sjpk 			} else if (getzonenamebyid(lifr.lifr_zoneid, zone_name,
28877c478bd9Sstevel@tonic-gate 			    sizeof (zone_name)) < 0) {
28887c478bd9Sstevel@tonic-gate 				(void) printf("\n\tzone %d", lifr.lifr_zoneid);
28897c478bd9Sstevel@tonic-gate 			} else {
28907c478bd9Sstevel@tonic-gate 				(void) printf("\n\tzone %s", zone_name);
28917c478bd9Sstevel@tonic-gate 			}
28927c478bd9Sstevel@tonic-gate 		}
28937c478bd9Sstevel@tonic-gate 	}
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) {
28967c478bd9Sstevel@tonic-gate 		lifs.lifs_ifindex = lifr.lifr_index;
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 		/*
28997c478bd9Sstevel@tonic-gate 		 * Find the number of interfaces that use this interfaces'
29007c478bd9Sstevel@tonic-gate 		 * address as a source address
29017c478bd9Sstevel@tonic-gate 		 */
29027c478bd9Sstevel@tonic-gate 		lifs.lifs_buf = NULL;
29037c478bd9Sstevel@tonic-gate 		lifs.lifs_maxlen = 0;
29047c478bd9Sstevel@tonic-gate 		for (;;) {
29057c478bd9Sstevel@tonic-gate 			/* The first pass will give the bufsize we need */
29067c478bd9Sstevel@tonic-gate 			rval = ioctl(s, SIOCGLIFSRCOF, (char *)&lifs);
29077c478bd9Sstevel@tonic-gate 			if (rval < 0) {
29087c478bd9Sstevel@tonic-gate 				if (lifs.lifs_buf != NULL) {
29097c478bd9Sstevel@tonic-gate 					free(lifs.lifs_buf);
29107c478bd9Sstevel@tonic-gate 					lifs.lifs_buf = NULL;
29117c478bd9Sstevel@tonic-gate 				}
29127c478bd9Sstevel@tonic-gate 				lifs.lifs_len = 0;
29137c478bd9Sstevel@tonic-gate 				break;
29147c478bd9Sstevel@tonic-gate 			}
29157c478bd9Sstevel@tonic-gate 			if (lifs.lifs_len <= lifs.lifs_maxlen)
29167c478bd9Sstevel@tonic-gate 				break;
29177c478bd9Sstevel@tonic-gate 			/* Use kernel's size + a small margin to avoid loops */
29187c478bd9Sstevel@tonic-gate 			lifs.lifs_maxlen = lifs.lifs_len +
29197c478bd9Sstevel@tonic-gate 			    5 * sizeof (struct lifreq);
29207c478bd9Sstevel@tonic-gate 			/* For the first pass, realloc acts like malloc */
29217c478bd9Sstevel@tonic-gate 			newbuf = realloc(lifs.lifs_buf, lifs.lifs_maxlen);
29227c478bd9Sstevel@tonic-gate 			if (newbuf == NULL) {
29237c478bd9Sstevel@tonic-gate 				if (lifs.lifs_buf != NULL) {
29247c478bd9Sstevel@tonic-gate 					free(lifs.lifs_buf);
29257c478bd9Sstevel@tonic-gate 					lifs.lifs_buf = NULL;
29267c478bd9Sstevel@tonic-gate 				}
29277c478bd9Sstevel@tonic-gate 				lifs.lifs_len = 0;
29287c478bd9Sstevel@tonic-gate 				break;
29297c478bd9Sstevel@tonic-gate 			}
29307c478bd9Sstevel@tonic-gate 			lifs.lifs_buf = newbuf;
29317c478bd9Sstevel@tonic-gate 		}
29327c478bd9Sstevel@tonic-gate 
29337c478bd9Sstevel@tonic-gate 
29347c478bd9Sstevel@tonic-gate 		numifs = lifs.lifs_len / sizeof (struct lifreq);
29357c478bd9Sstevel@tonic-gate 		if (numifs > 0) {
29367c478bd9Sstevel@tonic-gate 			lifrp = lifs.lifs_req;
29377c478bd9Sstevel@tonic-gate 			(void) printf("\n\tsrcof");
29387c478bd9Sstevel@tonic-gate 			for (n = numifs; n > 0; n--, lifrp++) {
29397c478bd9Sstevel@tonic-gate 				(void) printf(" %s", lifrp->lifr_name);
29407c478bd9Sstevel@tonic-gate 			}
29417c478bd9Sstevel@tonic-gate 		}
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate 		if (lifs.lifs_buf != NULL)
29447c478bd9Sstevel@tonic-gate 			free(lifs.lifs_buf);
29457c478bd9Sstevel@tonic-gate 	}
29467c478bd9Sstevel@tonic-gate 
29477c478bd9Sstevel@tonic-gate 	/* Find the interface whose source address this interface uses */
29487c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
29497c478bd9Sstevel@tonic-gate 		if (lifr.lifr_index != 0) {
29507c478bd9Sstevel@tonic-gate 			if (if_indextoname(lifr.lifr_index,
29517c478bd9Sstevel@tonic-gate 			    if_usesrc_name) == NULL) {
29527c478bd9Sstevel@tonic-gate 				(void) printf("\n\tusesrc ifIndex %d",
29537c478bd9Sstevel@tonic-gate 				    lifr.lifr_index);
29547c478bd9Sstevel@tonic-gate 			} else {
29557c478bd9Sstevel@tonic-gate 				(void) printf("\n\tusesrc %s", if_usesrc_name);
29567c478bd9Sstevel@tonic-gate 			}
29577c478bd9Sstevel@tonic-gate 		}
29587c478bd9Sstevel@tonic-gate 	}
29597c478bd9Sstevel@tonic-gate 
29607c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
29617c478bd9Sstevel@tonic-gate }
29627c478bd9Sstevel@tonic-gate 
29637c478bd9Sstevel@tonic-gate /*
29647c478bd9Sstevel@tonic-gate  * Print the status of the interface.  If an address family was
29657c478bd9Sstevel@tonic-gate  * specified, show it and it only; otherwise, show them all.
29667c478bd9Sstevel@tonic-gate  */
29677c478bd9Sstevel@tonic-gate static void
29687c478bd9Sstevel@tonic-gate status(void)
29697c478bd9Sstevel@tonic-gate {
29702b24ab6bSSebastien Roy 	struct afswtch	*p = afp;
29712b24ab6bSSebastien Roy 	uint64_t	flags;
29722b24ab6bSSebastien Roy 	datalink_id_t	linkid;
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
29757c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
29767c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
29777c478bd9Sstevel@tonic-gate 	}
29787c478bd9Sstevel@tonic-gate 
29797c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 	/*
29827c478bd9Sstevel@tonic-gate 	 * Only print the interface status if the address family matches
29837c478bd9Sstevel@tonic-gate 	 * the interface family flag.
29847c478bd9Sstevel@tonic-gate 	 */
29857c478bd9Sstevel@tonic-gate 	if (p != NULL) {
29867c478bd9Sstevel@tonic-gate 		if (((p->af_af == AF_INET6) && (flags & IFF_IPV4)) ||
29877c478bd9Sstevel@tonic-gate 		    ((p->af_af == AF_INET) && (flags & IFF_IPV6)))
29887c478bd9Sstevel@tonic-gate 			return;
29897c478bd9Sstevel@tonic-gate 	}
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate 	/*
29927c478bd9Sstevel@tonic-gate 	 * In V4 compatibility mode, don't print IFF_IPV6 interfaces.
29937c478bd9Sstevel@tonic-gate 	 */
29947c478bd9Sstevel@tonic-gate 	if (v4compat && (flags & IFF_IPV6))
29957c478bd9Sstevel@tonic-gate 		return;
29967c478bd9Sstevel@tonic-gate 
29977c478bd9Sstevel@tonic-gate 	ifstatus(name);
29987c478bd9Sstevel@tonic-gate 
29992b24ab6bSSebastien Roy 	if (ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid) ==
30002b24ab6bSSebastien Roy 	    DLADM_STATUS_OK)
30012b24ab6bSSebastien Roy 		tun_status(linkid);
30022b24ab6bSSebastien Roy 
30037c478bd9Sstevel@tonic-gate 	if (p != NULL) {
30047c478bd9Sstevel@tonic-gate 		(*p->af_status)(1, flags);
30057c478bd9Sstevel@tonic-gate 	} else {
30067c478bd9Sstevel@tonic-gate 		for (p = afs; p->af_name; p++) {
30077c478bd9Sstevel@tonic-gate 			/* set global af for use in p->af_status */
30087c478bd9Sstevel@tonic-gate 			af = p->af_af;
30097c478bd9Sstevel@tonic-gate 			(*p->af_status)(0, flags);
30107c478bd9Sstevel@tonic-gate 		}
30117c478bd9Sstevel@tonic-gate 
30127c478bd9Sstevel@tonic-gate 		/*
30137c478bd9Sstevel@tonic-gate 		 * Historically, 'ether' has been an address family,
30147c478bd9Sstevel@tonic-gate 		 * so print it here.
30157c478bd9Sstevel@tonic-gate 		 */
30167c478bd9Sstevel@tonic-gate 		print_ifether(name);
30177c478bd9Sstevel@tonic-gate 	}
30187c478bd9Sstevel@tonic-gate }
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate /*
30217c478bd9Sstevel@tonic-gate  * Print the status of the interface in a format that can be used to
30227c478bd9Sstevel@tonic-gate  * reconfigure the interface later. Code stolen from status() above.
30237c478bd9Sstevel@tonic-gate  */
30247c478bd9Sstevel@tonic-gate /* ARGSUSED */
30257c478bd9Sstevel@tonic-gate static int
30267c478bd9Sstevel@tonic-gate configinfo(char *null, int64_t param)
30277c478bd9Sstevel@tonic-gate {
3028e11c3f44Smeem 	char *cp;
30297c478bd9Sstevel@tonic-gate 	struct afswtch *p = afp;
30307c478bd9Sstevel@tonic-gate 	uint64_t flags;
3031e11c3f44Smeem 	char lifname[LIFNAMSIZ];
30327c478bd9Sstevel@tonic-gate 	char if_usesrc_name[LIFNAMSIZ];
30337c478bd9Sstevel@tonic-gate 
30347c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3035e11c3f44Smeem 
30367c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
30377c478bd9Sstevel@tonic-gate 		Perror0_exit("status: SIOCGLIFFLAGS");
30387c478bd9Sstevel@tonic-gate 	}
30397c478bd9Sstevel@tonic-gate 	flags = lifr.lifr_flags;
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate 	if (debug) {
30427c478bd9Sstevel@tonic-gate 		(void) printf("configinfo: name %s flags  0x%llx af_af %d\n",
30437c478bd9Sstevel@tonic-gate 		    name, flags, p != NULL ? p->af_af : -1);
30447c478bd9Sstevel@tonic-gate 	}
30457c478bd9Sstevel@tonic-gate 
3046e11c3f44Smeem 	/*
304767ce1608SVasumathi Sundaram 	 * Build the interface name to print (we cannot directly use `name'
3048e11c3f44Smeem 	 * because one cannot "plumb" ":0" interfaces).
3049e11c3f44Smeem 	 */
3050e11c3f44Smeem 	(void) strlcpy(lifname, name, LIFNAMSIZ);
3051e11c3f44Smeem 	if ((cp = strchr(lifname, ':')) != NULL && atoi(cp + 1) == 0)
3052e11c3f44Smeem 		*cp = '\0';
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate 	/*
30557c478bd9Sstevel@tonic-gate 	 * if the interface is IPv4
30567c478bd9Sstevel@tonic-gate 	 *	if we have a IPv6 address family restriction return
30577c478bd9Sstevel@tonic-gate 	 *		so it won't print
30587c478bd9Sstevel@tonic-gate 	 *	if we are in IPv4 compatibility mode, clear out IFF_IPV4
30597c478bd9Sstevel@tonic-gate 	 *		so we don't print it.
30607c478bd9Sstevel@tonic-gate 	 */
30617c478bd9Sstevel@tonic-gate 	if (flags & IFF_IPV4) {
30627c478bd9Sstevel@tonic-gate 		if (p && p->af_af == AF_INET6)
30637c478bd9Sstevel@tonic-gate 			return (-1);
30647c478bd9Sstevel@tonic-gate 		if (v4compat)
30657c478bd9Sstevel@tonic-gate 			flags &= ~IFF_IPV4;
30667c478bd9Sstevel@tonic-gate 
3067e11c3f44Smeem 		(void) printf("%s inet plumb", lifname);
30687c478bd9Sstevel@tonic-gate 	} else if (flags & IFF_IPV6) {
30697c478bd9Sstevel@tonic-gate 		/*
30707c478bd9Sstevel@tonic-gate 		 * else if the interface is IPv6
30717c478bd9Sstevel@tonic-gate 		 *	if we have a IPv4 address family restriction return
30727c478bd9Sstevel@tonic-gate 		 *	or we are in IPv4 compatibiltiy mode, return.
30737c478bd9Sstevel@tonic-gate 		 */
30747c478bd9Sstevel@tonic-gate 		if (p && p->af_af == AF_INET)
30757c478bd9Sstevel@tonic-gate 			return (-1);
30767c478bd9Sstevel@tonic-gate 		if (v4compat)
30777c478bd9Sstevel@tonic-gate 			return (-1);
30787c478bd9Sstevel@tonic-gate 
3079e11c3f44Smeem 		(void) printf("%s inet6 plumb", lifname);
30807c478bd9Sstevel@tonic-gate 	}
30817c478bd9Sstevel@tonic-gate 
30827c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
30837c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
30847c478bd9Sstevel@tonic-gate 		Perror0_exit("configinfo: SIOCGLIFMETRIC");
30857c478bd9Sstevel@tonic-gate 	} else {
30867c478bd9Sstevel@tonic-gate 		if (lifr.lifr_metric)
30877c478bd9Sstevel@tonic-gate 			(void) printf(" metric %d ", lifr.lifr_metric);
30887c478bd9Sstevel@tonic-gate 	}
30897c478bd9Sstevel@tonic-gate 	if (((flags & (IFF_VIRTUAL|IFF_LOOPBACK)) != IFF_VIRTUAL) &&
3090fc80c0dfSnordmark 	    ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
30917c478bd9Sstevel@tonic-gate 		(void) printf(" mtu %d", lifr.lifr_metric);
30927c478bd9Sstevel@tonic-gate 
3093e11c3f44Smeem 	/* Index only applies to the zeroth interface */
3094e11c3f44Smeem 	if (lifnum(name) == 0) {
30957c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
30967c478bd9Sstevel@tonic-gate 			(void) printf(" index %d", lifr.lifr_index);
30977c478bd9Sstevel@tonic-gate 	}
30987c478bd9Sstevel@tonic-gate 
30997c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
31007c478bd9Sstevel@tonic-gate 		if (lifr.lifr_index != 0) {
31017c478bd9Sstevel@tonic-gate 			if (if_indextoname(lifr.lifr_index,
31027c478bd9Sstevel@tonic-gate 			    if_usesrc_name) != NULL) {
31037c478bd9Sstevel@tonic-gate 				(void) printf(" usesrc %s", if_usesrc_name);
31047c478bd9Sstevel@tonic-gate 			}
31057c478bd9Sstevel@tonic-gate 		}
31067c478bd9Sstevel@tonic-gate 	}
31077c478bd9Sstevel@tonic-gate 
31087c478bd9Sstevel@tonic-gate 	if (p != NULL) {
31097c478bd9Sstevel@tonic-gate 		(*p->af_configinfo)(1, flags);
31107c478bd9Sstevel@tonic-gate 	} else {
31117c478bd9Sstevel@tonic-gate 		for (p = afs; p->af_name; p++) {
31127c478bd9Sstevel@tonic-gate 			(void) close(s);
31137c478bd9Sstevel@tonic-gate 			s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0);
31147c478bd9Sstevel@tonic-gate 			/* set global af for use in p->af_configinfo */
31157c478bd9Sstevel@tonic-gate 			af = p->af_af;
31167c478bd9Sstevel@tonic-gate 			if (s == -1) {
31177c478bd9Sstevel@tonic-gate 				Perror0_exit("socket");
31187c478bd9Sstevel@tonic-gate 			}
31197c478bd9Sstevel@tonic-gate 			(*p->af_configinfo)(0, flags);
31207c478bd9Sstevel@tonic-gate 		}
31217c478bd9Sstevel@tonic-gate 	}
31227c478bd9Sstevel@tonic-gate 
31232b24ab6bSSebastien Roy 	(void) putchar('\n');
31247c478bd9Sstevel@tonic-gate 	return (0);
31257c478bd9Sstevel@tonic-gate }
31267c478bd9Sstevel@tonic-gate 
31277c478bd9Sstevel@tonic-gate static void
31282b24ab6bSSebastien Roy print_tsec(iptun_params_t *params)
31297c478bd9Sstevel@tonic-gate {
31307c478bd9Sstevel@tonic-gate 	ipsec_req_t *ipsr;
31317c478bd9Sstevel@tonic-gate 
31327c478bd9Sstevel@tonic-gate 	(void) printf("\ttunnel security settings  ");
31332b24ab6bSSebastien Roy 	if (!(params->iptun_param_flags & IPTUN_PARAM_SECINFO)) {
31342b24ab6bSSebastien Roy 		(void) printf("-->  use 'ipsecconf -ln -i %s'", name);
31358810c16bSdanmcd 	} else {
31362b24ab6bSSebastien Roy 		ipsr = &params->iptun_param_secinfo;
31378810c16bSdanmcd 		if (ipsr->ipsr_ah_req & IPSEC_PREF_REQUIRED) {
31388810c16bSdanmcd 			(void) printf("ah (%s)  ",
31398810c16bSdanmcd 			    rparsealg(ipsr->ipsr_auth_alg, IPSEC_PROTO_AH));
31408810c16bSdanmcd 		}
31418810c16bSdanmcd 		if (ipsr->ipsr_esp_req & IPSEC_PREF_REQUIRED) {
31428810c16bSdanmcd 			(void) printf("esp (%s",
31438810c16bSdanmcd 			    rparsealg(ipsr->ipsr_esp_alg, IPSEC_PROTO_ESP));
31448810c16bSdanmcd 			(void) printf("/%s)",
31458810c16bSdanmcd 			    rparsealg(ipsr->ipsr_esp_auth_alg, IPSEC_PROTO_AH));
31468810c16bSdanmcd 		}
31477c478bd9Sstevel@tonic-gate 	}
31487c478bd9Sstevel@tonic-gate 	(void) printf("\n");
31497c478bd9Sstevel@tonic-gate }
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate static void
31522b24ab6bSSebastien Roy tun_status(datalink_id_t linkid)
31537c478bd9Sstevel@tonic-gate {
31542b24ab6bSSebastien Roy 	iptun_params_t	params;
31552b24ab6bSSebastien Roy 	char		propval[DLADM_PROP_VAL_MAX];
31562b24ab6bSSebastien Roy 	char		*valptr[1];
31572b24ab6bSSebastien Roy 	uint_t		valcnt = 1;
31582b24ab6bSSebastien Roy 	boolean_t	tabbed = _B_FALSE;
31592b24ab6bSSebastien Roy 
31602b24ab6bSSebastien Roy 	params.iptun_param_linkid = linkid;
31612b24ab6bSSebastien Roy 
31622b24ab6bSSebastien Roy 	/* If dladm_iptun_getparams() fails, assume we are not a tunnel. */
31632b24ab6bSSebastien Roy 	assert(dlh_opened);
31642b24ab6bSSebastien Roy 	if (dladm_iptun_getparams(dlh, &params, DLADM_OPT_ACTIVE) !=
31652b24ab6bSSebastien Roy 	    DLADM_STATUS_OK)
31667c478bd9Sstevel@tonic-gate 		return;
31677c478bd9Sstevel@tonic-gate 
31682b24ab6bSSebastien Roy 	switch (params.iptun_param_type) {
31692b24ab6bSSebastien Roy 	case IPTUN_TYPE_IPV4:
31702b24ab6bSSebastien Roy 	case IPTUN_TYPE_6TO4:
31717c478bd9Sstevel@tonic-gate 		(void) printf("\tinet");
31727c478bd9Sstevel@tonic-gate 		break;
31732b24ab6bSSebastien Roy 	case IPTUN_TYPE_IPV6:
31747c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6");
31757c478bd9Sstevel@tonic-gate 		break;
31767c478bd9Sstevel@tonic-gate 	default:
31772b24ab6bSSebastien Roy 		dladmerr_exit(DLADM_STATUS_IPTUNTYPE, name);
31787c478bd9Sstevel@tonic-gate 		break;
31797c478bd9Sstevel@tonic-gate 	}
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 	/*
31822b24ab6bSSebastien Roy 	 * There is always a source address.  If it hasn't been explicitly
31832b24ab6bSSebastien Roy 	 * set, the API will pass back a buffer containing the unspecified
31842b24ab6bSSebastien Roy 	 * address.
31857c478bd9Sstevel@tonic-gate 	 */
31862b24ab6bSSebastien Roy 	(void) printf(" tunnel src %s ", params.iptun_param_laddr);
31872b24ab6bSSebastien Roy 
31882b24ab6bSSebastien Roy 	if (params.iptun_param_flags & IPTUN_PARAM_RADDR)
31892b24ab6bSSebastien Roy 		(void) printf("tunnel dst %s\n", params.iptun_param_raddr);
31902b24ab6bSSebastien Roy 	else
31912b24ab6bSSebastien Roy 		(void) putchar('\n');
31922b24ab6bSSebastien Roy 
31932b24ab6bSSebastien Roy 	if (params.iptun_param_flags & IPTUN_PARAM_IPSECPOL)
31942b24ab6bSSebastien Roy 		print_tsec(&params);
31957c478bd9Sstevel@tonic-gate 
31962b24ab6bSSebastien Roy 	valptr[0] = propval;
31972b24ab6bSSebastien Roy 	if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT, "hoplimit",
31982b24ab6bSSebastien Roy 	    (char **)valptr, &valcnt) == DLADM_STATUS_OK) {
31992b24ab6bSSebastien Roy 		(void) printf("\ttunnel hop limit %s ", propval);
32007c478bd9Sstevel@tonic-gate 		tabbed = _B_TRUE;
32017c478bd9Sstevel@tonic-gate 	}
32027c478bd9Sstevel@tonic-gate 
32032b24ab6bSSebastien Roy 	if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT,
32042b24ab6bSSebastien Roy 	    "encaplimit", (char **)valptr, &valcnt) == DLADM_STATUS_OK) {
32052b24ab6bSSebastien Roy 		uint32_t elim;
32062b24ab6bSSebastien Roy 
32077c478bd9Sstevel@tonic-gate 		if (!tabbed) {
32082b24ab6bSSebastien Roy 			(void) putchar('\t');
32097c478bd9Sstevel@tonic-gate 			tabbed = _B_TRUE;
32107c478bd9Sstevel@tonic-gate 		}
32112b24ab6bSSebastien Roy 		elim = strtol(propval, NULL, 10);
32122b24ab6bSSebastien Roy 		if (elim > 0)
32132b24ab6bSSebastien Roy 			(void) printf("tunnel encapsulation limit %s", propval);
32142b24ab6bSSebastien Roy 		else
32157c478bd9Sstevel@tonic-gate 			(void) printf("tunnel encapsulation limit disabled");
32167c478bd9Sstevel@tonic-gate 	}
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate 	if (tabbed)
32192b24ab6bSSebastien Roy 		(void) putchar('\n');
32207c478bd9Sstevel@tonic-gate }
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate static void
32237c478bd9Sstevel@tonic-gate in_status(int force, uint64_t flags)
32247c478bd9Sstevel@tonic-gate {
32252b24ab6bSSebastien Roy 	struct sockaddr_in	*sin, *laddr;
32262b24ab6bSSebastien Roy 	struct sockaddr_in	netmask = { AF_INET };
32277c478bd9Sstevel@tonic-gate 
32287c478bd9Sstevel@tonic-gate 	if (debug)
32297c478bd9Sstevel@tonic-gate 		(void) printf("in_status(%s) flags 0x%llx\n", name, flags);
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 	/* only print status for IPv4 interfaces */
32327c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV4))
32337c478bd9Sstevel@tonic-gate 		return;
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
32367c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32377c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
32387c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
32397c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
32407c478bd9Sstevel@tonic-gate 				if (!force)
32417c478bd9Sstevel@tonic-gate 					return;
32427c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
32437c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
32447c478bd9Sstevel@tonic-gate 			} else
32457c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status: SIOCGLIFADDR");
32467c478bd9Sstevel@tonic-gate 		}
32477c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
32487c478bd9Sstevel@tonic-gate 		(void) printf("\tinet %s ", inet_ntoa(sin->sin_addr));
32497c478bd9Sstevel@tonic-gate 		laddr = sin;
32507c478bd9Sstevel@tonic-gate 	} else {
32517c478bd9Sstevel@tonic-gate 		(void) printf("\tinet ");
32527c478bd9Sstevel@tonic-gate 	}
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32557c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
32567c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
32577c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
32587c478bd9Sstevel@tonic-gate 			if (!force)
32597c478bd9Sstevel@tonic-gate 				return;
32607c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
32617c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
32627c478bd9Sstevel@tonic-gate 		} else {
32637c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status: SIOCGLIFSUBNET");
32647c478bd9Sstevel@tonic-gate 		}
32657c478bd9Sstevel@tonic-gate 	}
32667c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
32677c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
32687c478bd9Sstevel@tonic-gate 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
32697c478bd9Sstevel@tonic-gate 		(void) printf("subnet %s/%d ", inet_ntoa(sin->sin_addr),
32707c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
32717c478bd9Sstevel@tonic-gate 	}
32727c478bd9Sstevel@tonic-gate 	if (sin->sin_family != AF_INET) {
32737c478bd9Sstevel@tonic-gate 		(void) printf("Wrong family: %d\n", sin->sin_family);
32747c478bd9Sstevel@tonic-gate 	}
32757c478bd9Sstevel@tonic-gate 
32767c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32777c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
32787c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL)
32797c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status: SIOCGLIFNETMASK");
32807c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
32817c478bd9Sstevel@tonic-gate 	} else
32827c478bd9Sstevel@tonic-gate 		netmask.sin_addr =
32837c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
32847c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
32857c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32867c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
32877c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
32887c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
32897c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
32907c478bd9Sstevel@tonic-gate 			else
3291fc80c0dfSnordmark 				Perror0_exit("in_status: SIOCGLIFDSTADDR");
32927c478bd9Sstevel@tonic-gate 		}
32937c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
32947c478bd9Sstevel@tonic-gate 		(void) printf("--> %s ", inet_ntoa(sin->sin_addr));
32957c478bd9Sstevel@tonic-gate 	}
32967c478bd9Sstevel@tonic-gate 	(void) printf("netmask %x ", ntohl(netmask.sin_addr.s_addr));
32977c478bd9Sstevel@tonic-gate 	if (flags & IFF_BROADCAST) {
32987c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
32997c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
33007c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
33017c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33027c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33037c478bd9Sstevel@tonic-gate 			else
3304fc80c0dfSnordmark 				Perror0_exit("in_status: SIOCGLIFBRDADDR");
33057c478bd9Sstevel@tonic-gate 		}
33067c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
33077c478bd9Sstevel@tonic-gate 		if (sin->sin_addr.s_addr != 0) {
33087c478bd9Sstevel@tonic-gate 			(void) printf("broadcast %s",
33097c478bd9Sstevel@tonic-gate 			    inet_ntoa(sin->sin_addr));
33107c478bd9Sstevel@tonic-gate 		}
33117c478bd9Sstevel@tonic-gate 	}
3312e11c3f44Smeem 	/* If there is a groupname, print it for only the physical interface */
33137c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') == NULL) {
3314e11c3f44Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3315e11c3f44Smeem 		    lifr.lifr_groupname[0] != '\0') {
3316e11c3f44Smeem 			(void) printf("\n\tgroupname %s", lifr.lifr_groupname);
33177c478bd9Sstevel@tonic-gate 		}
33187c478bd9Sstevel@tonic-gate 	}
33197c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
33207c478bd9Sstevel@tonic-gate }
33217c478bd9Sstevel@tonic-gate 
33227c478bd9Sstevel@tonic-gate static void
33237c478bd9Sstevel@tonic-gate in6_status(int force, uint64_t flags)
33247c478bd9Sstevel@tonic-gate {
33252b24ab6bSSebastien Roy 	char			abuf[INET6_ADDRSTRLEN];
33262b24ab6bSSebastien Roy 	struct sockaddr_in6	*sin6, *laddr6;
33277c478bd9Sstevel@tonic-gate 
33287c478bd9Sstevel@tonic-gate 	if (debug)
33297c478bd9Sstevel@tonic-gate 		(void) printf("in6_status(%s) flags 0x%llx\n", name, flags);
33307c478bd9Sstevel@tonic-gate 
33317c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV6))
33327c478bd9Sstevel@tonic-gate 		return;
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
33357c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33367c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
33377c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
33387c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
33397c478bd9Sstevel@tonic-gate 				if (!force)
33407c478bd9Sstevel@tonic-gate 					return;
33417c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33427c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33437c478bd9Sstevel@tonic-gate 			} else
33447c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status6: SIOCGLIFADDR");
33457c478bd9Sstevel@tonic-gate 		}
33467c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
33477c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6 %s/%d ",
33487c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33497c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
33507c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
33517c478bd9Sstevel@tonic-gate 		laddr6 = sin6;
33527c478bd9Sstevel@tonic-gate 	} else {
33537c478bd9Sstevel@tonic-gate 		(void) printf("\tinet6 ");
33547c478bd9Sstevel@tonic-gate 	}
33557c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33567c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
33577c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
33587c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
33597c478bd9Sstevel@tonic-gate 			if (!force)
33607c478bd9Sstevel@tonic-gate 				return;
33617c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
33627c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
33637c478bd9Sstevel@tonic-gate 		} else
33647c478bd9Sstevel@tonic-gate 			Perror0_exit("in_status6: SIOCGLIFSUBNET");
33657c478bd9Sstevel@tonic-gate 	}
33667c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
33677c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
33687c478bd9Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
33697c478bd9Sstevel@tonic-gate 		(void) printf("subnet %s/%d ",
33707c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33717c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
33727c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
33737c478bd9Sstevel@tonic-gate 	}
33747c478bd9Sstevel@tonic-gate 	if (sin6->sin6_family != AF_INET6) {
33757c478bd9Sstevel@tonic-gate 		(void) printf("Wrong family: %d\n", sin6->sin6_family);
33767c478bd9Sstevel@tonic-gate 	}
33777c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
33787c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33797c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
33807c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
33817c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33827c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33837c478bd9Sstevel@tonic-gate 			else
3384fc80c0dfSnordmark 				Perror0_exit("in_status6: SIOCGLIFDSTADDR");
33857c478bd9Sstevel@tonic-gate 		}
33867c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
33877c478bd9Sstevel@tonic-gate 		(void) printf("--> %s ",
33887c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
33897c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)));
33907c478bd9Sstevel@tonic-gate 	}
33917c478bd9Sstevel@tonic-gate 	if (verbose) {
33927c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
33937c478bd9Sstevel@tonic-gate 		(void) putchar('\t');
33947c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
33957c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
33967c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EINVAL)
33977c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
33987c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
33997c478bd9Sstevel@tonic-gate 			else
3400fc80c0dfSnordmark 				Perror0_exit("in_status6: SIOCGLIFTOKEN");
34017c478bd9Sstevel@tonic-gate 		} else {
34027c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
34037c478bd9Sstevel@tonic-gate 			(void) printf("token %s/%d ",
34047c478bd9Sstevel@tonic-gate 			    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
34057c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
34067c478bd9Sstevel@tonic-gate 			    lifr.lifr_addrlen);
34077c478bd9Sstevel@tonic-gate 		}
34087c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFLNKINFO, (caddr_t)&lifr) < 0) {
34097c478bd9Sstevel@tonic-gate 			if (errno != EINVAL) {
34107c478bd9Sstevel@tonic-gate 				Perror0_exit("in_status6: SIOCGLIFLNKINFO");
34117c478bd9Sstevel@tonic-gate 			}
34127c478bd9Sstevel@tonic-gate 		} else {
34137c478bd9Sstevel@tonic-gate 			(void) printf("maxhops %u, reachtime %u ms, "
34147c478bd9Sstevel@tonic-gate 			    "reachretrans %u ms, maxmtu %u ",
34157c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_maxhops,
34167c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_reachtime,
34177c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_reachretrans,
34187c478bd9Sstevel@tonic-gate 			    lifr.lifr_ifinfo.lir_maxmtu);
34197c478bd9Sstevel@tonic-gate 		}
34207c478bd9Sstevel@tonic-gate 	}
3421f7d61273Smeem 	/* If there is a groupname, print it for only the physical interface */
34227c478bd9Sstevel@tonic-gate 	if (strchr(name, ':') == NULL) {
3423f7d61273Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3424f7d61273Smeem 		    lifr.lifr_groupname[0] != '\0') {
3425f7d61273Smeem 			(void) printf("\n\tgroupname %s", lifr.lifr_groupname);
34267c478bd9Sstevel@tonic-gate 		}
34277c478bd9Sstevel@tonic-gate 	}
34287c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
34297c478bd9Sstevel@tonic-gate }
34307c478bd9Sstevel@tonic-gate 
34317c478bd9Sstevel@tonic-gate static void
34327c478bd9Sstevel@tonic-gate in_configinfo(int force, uint64_t flags)
34337c478bd9Sstevel@tonic-gate {
34347c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin, *laddr;
34357c478bd9Sstevel@tonic-gate 	struct	sockaddr_in netmask = { AF_INET };
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate 	if (debug)
34387c478bd9Sstevel@tonic-gate 		(void) printf("in_configinfo(%s) flags 0x%llx\n", name, flags);
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate 	/* only configinfo info for IPv4 interfaces */
34417c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV4))
34427c478bd9Sstevel@tonic-gate 		return;
34437c478bd9Sstevel@tonic-gate 
34447c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
34457c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34467c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
34477c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
34487c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
34497c478bd9Sstevel@tonic-gate 				if (!force)
34507c478bd9Sstevel@tonic-gate 					return;
34517c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34527c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34537c478bd9Sstevel@tonic-gate 			} else
34547c478bd9Sstevel@tonic-gate 				Perror0_exit("in_configinfo: SIOCGLIFADDR");
34557c478bd9Sstevel@tonic-gate 		}
34567c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
3457e11c3f44Smeem 		(void) printf(" set %s ", inet_ntoa(sin->sin_addr));
34587c478bd9Sstevel@tonic-gate 		laddr = sin;
34597c478bd9Sstevel@tonic-gate 	}
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34627c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
34637c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
34647c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
34657c478bd9Sstevel@tonic-gate 			if (!force)
34667c478bd9Sstevel@tonic-gate 				return;
34677c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
34687c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
34697c478bd9Sstevel@tonic-gate 		} else {
34707c478bd9Sstevel@tonic-gate 			Perror0_exit("in_configinfo: SIOCGLIFSUBNET");
34717c478bd9Sstevel@tonic-gate 		}
34727c478bd9Sstevel@tonic-gate 	}
34737c478bd9Sstevel@tonic-gate 	sin = (struct sockaddr_in *)&lifr.lifr_addr;
34747c478bd9Sstevel@tonic-gate 
34757c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
34767c478bd9Sstevel@tonic-gate 	    sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
34777c478bd9Sstevel@tonic-gate 		(void) printf(" subnet %s/%d ", inet_ntoa(sin->sin_addr),
34787c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
34797c478bd9Sstevel@tonic-gate 	}
34807c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34817c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
34827c478bd9Sstevel@tonic-gate 		if (errno != EADDRNOTAVAIL)
34837c478bd9Sstevel@tonic-gate 			Perror0_exit("in_configinfo: SIOCGLIFNETMASK");
34847c478bd9Sstevel@tonic-gate 		(void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
34857c478bd9Sstevel@tonic-gate 	} else
34867c478bd9Sstevel@tonic-gate 		netmask.sin_addr =
34877c478bd9Sstevel@tonic-gate 		    ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
34887c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
34897c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
34907c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
34917c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
34927c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
34937c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
34947c478bd9Sstevel@tonic-gate 			else
3495fc80c0dfSnordmark 				Perror0_exit("in_configinfo: SIOCGLIFDSTADDR");
34967c478bd9Sstevel@tonic-gate 		}
34977c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
34987c478bd9Sstevel@tonic-gate 		(void) printf(" destination %s ", inet_ntoa(sin->sin_addr));
34997c478bd9Sstevel@tonic-gate 	}
35007c478bd9Sstevel@tonic-gate 	(void) printf(" netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
35017c478bd9Sstevel@tonic-gate 	if (flags & IFF_BROADCAST) {
35027c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35037c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
35047c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
35057c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
35067c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
35077c478bd9Sstevel@tonic-gate 			else
3508fc80c0dfSnordmark 				Perror0_exit("in_configinfo: SIOCGLIFBRDADDR");
35097c478bd9Sstevel@tonic-gate 		}
35107c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&lifr.lifr_addr;
35117c478bd9Sstevel@tonic-gate 		if (sin->sin_addr.s_addr != 0) {
35127c478bd9Sstevel@tonic-gate 			(void) printf(" broadcast %s ",
35137c478bd9Sstevel@tonic-gate 			    inet_ntoa(sin->sin_addr));
35147c478bd9Sstevel@tonic-gate 		}
35157c478bd9Sstevel@tonic-gate 	}
35167c478bd9Sstevel@tonic-gate 
3517e11c3f44Smeem 	/* If there is a groupname, print it for only the zeroth interface */
3518e11c3f44Smeem 	if (lifnum(name) == 0) {
3519f7d61273Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3520f7d61273Smeem 		    lifr.lifr_groupname[0] != '\0') {
3521f7d61273Smeem 			(void) printf(" group %s ", lifr.lifr_groupname);
35227c478bd9Sstevel@tonic-gate 		}
35237c478bd9Sstevel@tonic-gate 	}
35247c478bd9Sstevel@tonic-gate 
35257c478bd9Sstevel@tonic-gate 	/* Print flags to configure */
3526e11c3f44Smeem 	print_config_flags(AF_INET, flags);
35277c478bd9Sstevel@tonic-gate }
35287c478bd9Sstevel@tonic-gate 
35297c478bd9Sstevel@tonic-gate static void
35307c478bd9Sstevel@tonic-gate in6_configinfo(int force, uint64_t flags)
35317c478bd9Sstevel@tonic-gate {
35327c478bd9Sstevel@tonic-gate 	char abuf[INET6_ADDRSTRLEN];
35337c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6, *laddr6;
35347c478bd9Sstevel@tonic-gate 
35357c478bd9Sstevel@tonic-gate 	if (debug)
35367c478bd9Sstevel@tonic-gate 		(void) printf("in6_configinfo(%s) flags 0x%llx\n", name,
35377c478bd9Sstevel@tonic-gate 		    flags);
35387c478bd9Sstevel@tonic-gate 
35397c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_IPV6))
35407c478bd9Sstevel@tonic-gate 		return;
35417c478bd9Sstevel@tonic-gate 
35427c478bd9Sstevel@tonic-gate 	if (!(flags & IFF_NOLOCAL)) {
35437c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35447c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
35457c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
35467c478bd9Sstevel@tonic-gate 			    errno == ENXIO) {
35477c478bd9Sstevel@tonic-gate 				if (!force)
35487c478bd9Sstevel@tonic-gate 					return;
35497c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
35507c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
35517c478bd9Sstevel@tonic-gate 			} else
35527c478bd9Sstevel@tonic-gate 				Perror0_exit("in6_configinfo: SIOCGLIFADDR");
35537c478bd9Sstevel@tonic-gate 		}
35547c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3555e11c3f44Smeem 		(void) printf(" set %s/%d ",
3556e11c3f44Smeem 		    inet_ntop(AF_INET6, &sin6->sin6_addr, abuf, sizeof (abuf)),
3557e11c3f44Smeem 		    lifr.lifr_addrlen);
35587c478bd9Sstevel@tonic-gate 		laddr6 = sin6;
35597c478bd9Sstevel@tonic-gate 	}
35607c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35617c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
35627c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
35637c478bd9Sstevel@tonic-gate 		    errno == ENXIO) {
35647c478bd9Sstevel@tonic-gate 			if (!force)
35657c478bd9Sstevel@tonic-gate 				return;
35667c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
35677c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
35687c478bd9Sstevel@tonic-gate 		} else
35697c478bd9Sstevel@tonic-gate 			Perror0_exit("in6_configinfo: SIOCGLIFSUBNET");
35707c478bd9Sstevel@tonic-gate 	}
35717c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
35727c478bd9Sstevel@tonic-gate 	if ((flags & IFF_NOLOCAL) ||
35737c478bd9Sstevel@tonic-gate 	    !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
35747c478bd9Sstevel@tonic-gate 		(void) printf(" subnet %s/%d ",
35757c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35767c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
35777c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
35787c478bd9Sstevel@tonic-gate 	}
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate 	if (flags & IFF_POINTOPOINT) {
35817c478bd9Sstevel@tonic-gate 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35827c478bd9Sstevel@tonic-gate 		if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
35837c478bd9Sstevel@tonic-gate 			if (errno == EADDRNOTAVAIL)
35847c478bd9Sstevel@tonic-gate 				(void) memset(&lifr.lifr_addr, 0,
35857c478bd9Sstevel@tonic-gate 				    sizeof (lifr.lifr_addr));
35867c478bd9Sstevel@tonic-gate 			else
3587fc80c0dfSnordmark 				Perror0_exit("in6_configinfo: SIOCGLIFDSTADDR");
35887c478bd9Sstevel@tonic-gate 		}
35897c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
35907c478bd9Sstevel@tonic-gate 		(void) printf(" destination %s ",
35917c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
35927c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)));
35937c478bd9Sstevel@tonic-gate 	}
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate 	(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
35967c478bd9Sstevel@tonic-gate 	if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
35977c478bd9Sstevel@tonic-gate 		if (errno == EADDRNOTAVAIL || errno == EINVAL)
35987c478bd9Sstevel@tonic-gate 			(void) memset(&lifr.lifr_addr, 0,
35997c478bd9Sstevel@tonic-gate 			    sizeof (lifr.lifr_addr));
36007c478bd9Sstevel@tonic-gate 		else
3601fc80c0dfSnordmark 			Perror0_exit("in6_configinfo: SIOCGLIFTOKEN");
36027c478bd9Sstevel@tonic-gate 	} else {
36037c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
36047c478bd9Sstevel@tonic-gate 		(void) printf(" token %s/%d ",
36057c478bd9Sstevel@tonic-gate 		    inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
36067c478bd9Sstevel@tonic-gate 		    abuf, sizeof (abuf)),
36077c478bd9Sstevel@tonic-gate 		    lifr.lifr_addrlen);
36087c478bd9Sstevel@tonic-gate 	}
36097c478bd9Sstevel@tonic-gate 
3610e11c3f44Smeem 	/* If there is a groupname, print it for only the zeroth interface */
3611e11c3f44Smeem 	if (lifnum(name) == 0) {
3612f7d61273Smeem 		if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3613f7d61273Smeem 		    lifr.lifr_groupname[0] != '\0') {
3614f7d61273Smeem 			(void) printf(" group %s ", lifr.lifr_groupname);
36157c478bd9Sstevel@tonic-gate 		}
36167c478bd9Sstevel@tonic-gate 	}
36177c478bd9Sstevel@tonic-gate 
36187c478bd9Sstevel@tonic-gate 	/* Print flags to configure */
3619e11c3f44Smeem 	print_config_flags(AF_INET6, flags);
36207c478bd9Sstevel@tonic-gate }
36217c478bd9Sstevel@tonic-gate 
36227c478bd9Sstevel@tonic-gate /*
36237c478bd9Sstevel@tonic-gate  * If this is a physical interface then remove it.
36247c478bd9Sstevel@tonic-gate  * If it is a logical interface name use SIOCLIFREMOVEIF to
36257c478bd9Sstevel@tonic-gate  * remove it. In both cases fail if it doesn't exist.
36267c478bd9Sstevel@tonic-gate  */
36277c478bd9Sstevel@tonic-gate /* ARGSUSED */
36287c478bd9Sstevel@tonic-gate static int
36297c478bd9Sstevel@tonic-gate inetunplumb(char *arg, int64_t param)
36307c478bd9Sstevel@tonic-gate {
36316e91bba0SGirish Moodalbail 	ipadm_status_t	istatus;
3632e11c3f44Smeem 
36336e91bba0SGirish Moodalbail 	istatus = ipadm_delete_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE);
36346e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS) {
36356e91bba0SGirish Moodalbail 		(void) fprintf(stderr, "ifconfig: cannot unplumb %s: %s\n",
36366e91bba0SGirish Moodalbail 		    name, ipadm_status2str(istatus));
36376e91bba0SGirish Moodalbail 		exit(1);
36387c478bd9Sstevel@tonic-gate 	}
36397c478bd9Sstevel@tonic-gate 
36407c478bd9Sstevel@tonic-gate 	return (0);
36417c478bd9Sstevel@tonic-gate }
36427c478bd9Sstevel@tonic-gate 
36437c478bd9Sstevel@tonic-gate /*
36446e91bba0SGirish Moodalbail  * Create the interface in `name', using ipadm_create_if(). If `name' is a
36456e91bba0SGirish Moodalbail  * logical interface or loopback interface, ipadm_create_if() uses
36466e91bba0SGirish Moodalbail  * SIOCLIFADDIF to create it.
36477c478bd9Sstevel@tonic-gate  */
36487c478bd9Sstevel@tonic-gate /* ARGSUSED */
36497c478bd9Sstevel@tonic-gate static int
36507c478bd9Sstevel@tonic-gate inetplumb(char *arg, int64_t param)
36517c478bd9Sstevel@tonic-gate {
36526e91bba0SGirish Moodalbail 	ipadm_status_t	istatus;
36537c478bd9Sstevel@tonic-gate 
36546e91bba0SGirish Moodalbail 	istatus = ipadm_create_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE);
36556e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS) {
36566e91bba0SGirish Moodalbail 		(void) fprintf(stderr, "ifconfig: cannot plumb %s: %s\n",
36576e91bba0SGirish Moodalbail 		    name, ipadm_status2str(istatus));
36586e91bba0SGirish Moodalbail 		if (istatus != IPADM_IF_EXISTS)
36596e91bba0SGirish Moodalbail 			exit(1);
3660f4b3ec61Sdh 	}
3661e11c3f44Smeem 	return (0);
3662e11c3f44Smeem }
3663e11c3f44Smeem 
3664e11c3f44Smeem /* ARGSUSED */
3665e11c3f44Smeem static int
3666e11c3f44Smeem inetipmp(char *arg, int64_t param)
3667e11c3f44Smeem {
3668e11c3f44Smeem 	int retval;
3669e11c3f44Smeem 
3670e11c3f44Smeem 	/*
3671e11c3f44Smeem 	 * Treat e.g. "ifconfig ipmp0:2 ipmp" as "ifconfig ipmp0:2 plumb".
3672e11c3f44Smeem 	 * Otherwise, try to create the requested IPMP interface.
3673e11c3f44Smeem 	 */
3674e11c3f44Smeem 	if (strchr(name, ':') != NULL)
3675e11c3f44Smeem 		retval = inetplumb(arg, param);
3676e11c3f44Smeem 	else
3677e11c3f44Smeem 		retval = create_ipmp(name, afp->af_af, name, _B_FALSE);
3678e11c3f44Smeem 
3679e11c3f44Smeem 	/*
3680e11c3f44Smeem 	 * We'd return -1, but foreachinterface() doesn't propagate the error
3681e11c3f44Smeem 	 * into the exit status, so we're forced to explicitly exit().
3682e11c3f44Smeem 	 */
3683e11c3f44Smeem 	if (retval == -1)
3684e11c3f44Smeem 		exit(1);
36857c478bd9Sstevel@tonic-gate 	return (0);
36867c478bd9Sstevel@tonic-gate }
36877c478bd9Sstevel@tonic-gate 
3688e11c3f44Smeem /*
3689e11c3f44Smeem  * Create an IPMP group `grname' with address family `af'.  If `ifname' is
3690e11c3f44Smeem  * non-NULL, it specifies the interface name to use.  Otherwise, use the name
3691e11c3f44Smeem  * ipmpN, where N corresponds to the lowest available integer.  If `implicit'
3692e11c3f44Smeem  * is set, then the group is being created as a side-effect of placing an
3693e11c3f44Smeem  * underlying interface in a group.  Also start in.mpathd if necessary.
3694e11c3f44Smeem  */
3695e11c3f44Smeem static int
3696e11c3f44Smeem create_ipmp(const char *grname, int af, const char *ifname, boolean_t implicit)
3697e11c3f44Smeem {
3698e11c3f44Smeem 	static int ipmp_daemon_started;
36996e91bba0SGirish Moodalbail 	uint32_t flags = IPADM_OPT_IPMP|IPADM_OPT_ACTIVE;
37006e91bba0SGirish Moodalbail 	ipadm_status_t istatus;
3701e11c3f44Smeem 
3702e11c3f44Smeem 	if (debug) {
3703e11c3f44Smeem 		(void) printf("create_ipmp: ifname %s grname %s af %d\n",
3704e11c3f44Smeem 		    ifname != NULL ? ifname : "NULL", grname, af);
3705e11c3f44Smeem 	}
3706e11c3f44Smeem 
37076e91bba0SGirish Moodalbail 	/*
37086e91bba0SGirish Moodalbail 	 * ipadm_create_if() creates the IPMP interface and fills in the
37096e91bba0SGirish Moodalbail 	 * ppa in lifr.lifr_name, if `ifname'="ipmp".
37106e91bba0SGirish Moodalbail 	 */
37116e91bba0SGirish Moodalbail 	(void) strlcpy(lifr.lifr_name, (ifname ? ifname : "ipmp"),
37126e91bba0SGirish Moodalbail 	    sizeof (lifr.lifr_name));
37136e91bba0SGirish Moodalbail 	if (ifname == NULL)
37146e91bba0SGirish Moodalbail 		flags |= IPADM_OPT_GENPPA;
37156e91bba0SGirish Moodalbail 	istatus = ipadm_create_if(iph, lifr.lifr_name, af, flags);
37166e91bba0SGirish Moodalbail 	if (istatus != IPADM_SUCCESS) {
37176e91bba0SGirish Moodalbail 		(void) fprintf(stderr, "ifconfig: cannot create IPMP interface "
37186e91bba0SGirish Moodalbail 		    "%s: %s\n", grname, ipadm_status2str(istatus));
3719e11c3f44Smeem 		return (-1);
3720e11c3f44Smeem 	}
3721e11c3f44Smeem 
3722e11c3f44Smeem 	/*
3723e11c3f44Smeem 	 * To preserve backward-compatibility, always bring up the link-local
3724e11c3f44Smeem 	 * address for implicitly-created IPv6 IPMP interfaces.
3725e11c3f44Smeem 	 */
3726e11c3f44Smeem 	if (implicit && af == AF_INET6) {
3727e11c3f44Smeem 		if (ioctl(s6, SIOCGLIFFLAGS, &lifr) == 0) {
3728e11c3f44Smeem 			lifr.lifr_flags |= IFF_UP;
3729e11c3f44Smeem 			(void) ioctl(s6, SIOCSLIFFLAGS, &lifr);
3730e11c3f44Smeem 		}
3731e11c3f44Smeem 	}
3732e11c3f44Smeem 
3733e11c3f44Smeem 	/*
3734e11c3f44Smeem 	 * If the caller requested a different group name, issue a
3735e11c3f44Smeem 	 * SIOCSLIFGROUPNAME on the new IPMP interface.
3736e11c3f44Smeem 	 */
3737e11c3f44Smeem 	if (strcmp(lifr.lifr_name, grname) != 0) {
3738e11c3f44Smeem 		(void) strlcpy(lifr.lifr_groupname, grname, LIFGRNAMSIZ);
3739e11c3f44Smeem 		if (ioctl(s, SIOCSLIFGROUPNAME, &lifr) == -1) {
3740e11c3f44Smeem 			Perror0("SIOCSLIFGROUPNAME");
3741e11c3f44Smeem 			return (-1);
3742e11c3f44Smeem 		}
3743e11c3f44Smeem 	}
3744e11c3f44Smeem 
3745e11c3f44Smeem 	/*
3746e11c3f44Smeem 	 * If we haven't done so yet, ensure in.mpathd is started.
3747e11c3f44Smeem 	 */
3748e11c3f44Smeem 	if (ipmp_daemon_started++ == 0)
3749e11c3f44Smeem 		start_ipmp_daemon();
3750e11c3f44Smeem 
3751e11c3f44Smeem 	return (0);
3752e11c3f44Smeem }
3753e11c3f44Smeem 
3754e11c3f44Smeem /*
3755e11c3f44Smeem  * Start in.mpathd if it's not already running.
3756e11c3f44Smeem  */
3757e11c3f44Smeem static void
3758e11c3f44Smeem start_ipmp_daemon(void)
3759e11c3f44Smeem {
3760e11c3f44Smeem 	int retval;
3761e11c3f44Smeem 	ipmp_handle_t ipmp_handle;
3762e11c3f44Smeem 
3763e11c3f44Smeem 	/*
3764e11c3f44Smeem 	 * Ping in.mpathd to see if it's running already.
3765e11c3f44Smeem 	 */
3766e11c3f44Smeem 	if ((retval = ipmp_open(&ipmp_handle)) != IPMP_SUCCESS) {
3767e11c3f44Smeem 		(void) fprintf(stderr, "ifconfig: cannot create IPMP handle: "
3768e11c3f44Smeem 		    "%s\n", ipmp_errmsg(retval));
3769e11c3f44Smeem 		return;
3770e11c3f44Smeem 	}
3771e11c3f44Smeem 
3772e11c3f44Smeem 	retval = ipmp_ping_daemon(ipmp_handle);
3773e11c3f44Smeem 	ipmp_close(ipmp_handle);
3774e11c3f44Smeem 
3775e11c3f44Smeem 	switch (retval) {
3776e11c3f44Smeem 	case IPMP_ENOMPATHD:
3777e11c3f44Smeem 		break;
3778e11c3f44Smeem 	case IPMP_SUCCESS:
3779e11c3f44Smeem 		return;
3780e11c3f44Smeem 	default:
3781e11c3f44Smeem 		(void) fprintf(stderr, "ifconfig: cannot ping in.mpathd: %s\n",
3782e11c3f44Smeem 		    ipmp_errmsg(retval));
3783e11c3f44Smeem 		break;
3784e11c3f44Smeem 	}
3785e11c3f44Smeem 
3786e11c3f44Smeem 	/*
3787e11c3f44Smeem 	 * Start in.mpathd.  Note that in.mpathd will handle multiple
3788e11c3f44Smeem 	 * incarnations (ipmp_ping_daemon() is just an optimization) so we
3789e11c3f44Smeem 	 * don't need to worry about racing with another ifconfig process.
3790e11c3f44Smeem 	 */
3791e11c3f44Smeem 	switch (fork()) {
3792e11c3f44Smeem 	case -1:
3793e11c3f44Smeem 		Perror0_exit("start_ipmp_daemon: fork");
3794e11c3f44Smeem 		/* NOTREACHED */
3795e11c3f44Smeem 	case 0:
3796e11c3f44Smeem 		(void) execl(MPATHD_PATH, MPATHD_PATH, NULL);
3797e11c3f44Smeem 		_exit(1);
3798e11c3f44Smeem 		/* NOTREACHED */
3799e11c3f44Smeem 	default:
3800e11c3f44Smeem 		break;
3801e11c3f44Smeem 	}
3802e11c3f44Smeem }
3803e11c3f44Smeem 
3804e11c3f44Smeem /*
3805e11c3f44Smeem  * Bring the address named by `ifaddrp' up or down.  Doesn't trust any mutable
3806e11c3f44Smeem  * values in ia_flags since they may be stale.
3807e11c3f44Smeem  */
3808e11c3f44Smeem static boolean_t
3809e11c3f44Smeem ifaddr_op(ifaddrlistx_t *ifaddrp, boolean_t up)
3810e11c3f44Smeem {
3811e11c3f44Smeem 	struct lifreq	lifrl;	/* Local lifreq struct */
3812e11c3f44Smeem 	int		fd = (ifaddrp->ia_flags & IFF_IPV4) ? s4 : s6;
3813e11c3f44Smeem 
3814e11c3f44Smeem 	(void) memset(&lifrl, 0, sizeof (lifrl));
3815e11c3f44Smeem 	(void) strlcpy(lifrl.lifr_name, ifaddrp->ia_name, LIFNAMSIZ);
3816e11c3f44Smeem 	if (ioctl(fd, SIOCGLIFFLAGS, &lifrl) == -1)
3817e11c3f44Smeem 		return (_B_FALSE);
3818e11c3f44Smeem 
3819614f1612Smeem 	if (up)
3820e11c3f44Smeem 		lifrl.lifr_flags |= IFF_UP;
3821614f1612Smeem 	else
3822e11c3f44Smeem 		lifrl.lifr_flags &= ~IFF_UP;
3823614f1612Smeem 
3824614f1612Smeem 	if (ioctl(fd, SIOCSLIFFLAGS, &lifrl) == -1)
3825614f1612Smeem 		return (_B_FALSE);
3826614f1612Smeem 
3827614f1612Smeem 	/*
3828614f1612Smeem 	 * If we're trying to bring the address down, ensure that DAD activity
3829614f1612Smeem 	 * (observable by IFF_DUPLICATE) has also been stopped.
3830614f1612Smeem 	 */
3831614f1612Smeem 	if (!up && ioctl(fd, SIOCGLIFFLAGS, &lifrl) != -1 &&
3832614f1612Smeem 	    lifrl.lifr_flags & IFF_DUPLICATE) {
3833614f1612Smeem 		if (ioctl(fd, SIOCGLIFADDR, &lifrl) == -1 ||
3834614f1612Smeem 		    ioctl(fd, SIOCSLIFADDR, &lifrl) == -1) {
3835614f1612Smeem 			return (_B_FALSE);
3836614f1612Smeem 		}
3837e11c3f44Smeem 	}
3838614f1612Smeem 	return (_B_TRUE);
3839e11c3f44Smeem }
3840e11c3f44Smeem 
3841e11c3f44Smeem static boolean_t
3842e11c3f44Smeem ifaddr_up(ifaddrlistx_t *ifaddrp)
3843e11c3f44Smeem {
3844e11c3f44Smeem 	return (ifaddr_op(ifaddrp, _B_TRUE));
3845e11c3f44Smeem }
3846e11c3f44Smeem 
3847e11c3f44Smeem static boolean_t
3848e11c3f44Smeem ifaddr_down(ifaddrlistx_t *ifaddrp)
3849e11c3f44Smeem {
3850e11c3f44Smeem 	return (ifaddr_op(ifaddrp, _B_FALSE));
3851e11c3f44Smeem }
3852e11c3f44Smeem 
38532b24ab6bSSebastien Roy /*
38542b24ab6bSSebastien Roy  * Open the global libdladm handle "dlh" if it isn't already opened.  The
38552b24ab6bSSebastien Roy  * caller may optionally supply a link name to obtain its linkid.  If a link
38562b24ab6bSSebastien Roy  * of a specific class or classes is required, reqclass specifies the class
38572b24ab6bSSebastien Roy  * mask.
38582b24ab6bSSebastien Roy  */
38592b24ab6bSSebastien Roy static dladm_status_t
38602b24ab6bSSebastien Roy ifconfig_dladm_open(const char *name, datalink_class_t reqclass,
38612b24ab6bSSebastien Roy     datalink_id_t *linkid)
38622b24ab6bSSebastien Roy {
38632b24ab6bSSebastien Roy 	dladm_status_t status = DLADM_STATUS_OK;
38642b24ab6bSSebastien Roy 	datalink_class_t class;
38652b24ab6bSSebastien Roy 
38662b24ab6bSSebastien Roy 	if (!dlh_opened) {
38672b24ab6bSSebastien Roy 		if ((status = dladm_open(&dlh)) != DLADM_STATUS_OK)
38682b24ab6bSSebastien Roy 			return (status);
38692b24ab6bSSebastien Roy 		dlh_opened = _B_TRUE;
38702b24ab6bSSebastien Roy 	}
38712b24ab6bSSebastien Roy 	if (name != NULL) {
38722b24ab6bSSebastien Roy 		status = dladm_name2info(dlh, name, linkid, NULL, &class, NULL);
38732b24ab6bSSebastien Roy 		if (status == DLADM_STATUS_OK) {
38742b24ab6bSSebastien Roy 			if (!(class & reqclass))
38752b24ab6bSSebastien Roy 				status = DLADM_STATUS_LINKINVAL;
38762b24ab6bSSebastien Roy 		}
38772b24ab6bSSebastien Roy 	}
38782b24ab6bSSebastien Roy 	return (status);
38792b24ab6bSSebastien Roy }
38802b24ab6bSSebastien Roy 
38816e91bba0SGirish Moodalbail /*
38826e91bba0SGirish Moodalbail  * This function checks if we can use libipadm API's. We will only
38836e91bba0SGirish Moodalbail  * call libipadm functions for non-IPMP interfaces. This check is
38846e91bba0SGirish Moodalbail  * temporary until libipadm supports IPMP interfaces.
38856e91bba0SGirish Moodalbail  */
38866e91bba0SGirish Moodalbail static boolean_t
38876e91bba0SGirish Moodalbail ifconfig_use_libipadm(int s, const char *lifname)
38886e91bba0SGirish Moodalbail {
38896e91bba0SGirish Moodalbail 	struct lifreq lifr1;
38906e91bba0SGirish Moodalbail 
38916e91bba0SGirish Moodalbail 	(void) strlcpy(lifr1.lifr_name, lifname, sizeof (lifr1.lifr_name));
38926e91bba0SGirish Moodalbail 	if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr1) < 0) {
38936e91bba0SGirish Moodalbail 		(void) strncpy(lifr.lifr_name, lifname,
38946e91bba0SGirish Moodalbail 		    sizeof (lifr.lifr_name));
38956e91bba0SGirish Moodalbail 		Perror0_exit("error");
38966e91bba0SGirish Moodalbail 	}
38976e91bba0SGirish Moodalbail 
38986e91bba0SGirish Moodalbail 	return (lifr1.lifr_groupname[0] == '\0');
38996e91bba0SGirish Moodalbail }
39006e91bba0SGirish Moodalbail 
39016e91bba0SGirish Moodalbail static void
39026e91bba0SGirish Moodalbail ipadmerr_exit(ipadm_status_t status, const char *str)
39036e91bba0SGirish Moodalbail {
39046e91bba0SGirish Moodalbail 	(void) fprintf(stderr, "ifconfig: %s: %s\n", str,
39056e91bba0SGirish Moodalbail 	    ipadm_status2str(status));
39066e91bba0SGirish Moodalbail 	exit(1);
39076e91bba0SGirish Moodalbail }
39086e91bba0SGirish Moodalbail 
39096e91bba0SGirish Moodalbail static void
39102b24ab6bSSebastien Roy dladmerr_exit(dladm_status_t status, const char *str)
39112b24ab6bSSebastien Roy {
39122b24ab6bSSebastien Roy 	char errstr[DLADM_STRSIZE];
39132b24ab6bSSebastien Roy 
39142b24ab6bSSebastien Roy 	(void) fprintf(stderr, "%s: %s\n", str,
39152b24ab6bSSebastien Roy 	    dladm_status2str(status, errstr));
39162b24ab6bSSebastien Roy 	exit(1);
39172b24ab6bSSebastien Roy }
39182b24ab6bSSebastien Roy 
39197c478bd9Sstevel@tonic-gate void
3920f7d61273Smeem Perror0(const char *cmd)
39217c478bd9Sstevel@tonic-gate {
3922f7d61273Smeem 	Perror2(cmd, lifr.lifr_name);
39237c478bd9Sstevel@tonic-gate }
39247c478bd9Sstevel@tonic-gate 
39257c478bd9Sstevel@tonic-gate void
3926f7d61273Smeem Perror0_exit(const char *cmd)
39277c478bd9Sstevel@tonic-gate {
39287c478bd9Sstevel@tonic-gate 	Perror0(cmd);
39297c478bd9Sstevel@tonic-gate 	exit(1);
39307c478bd9Sstevel@tonic-gate }
39317c478bd9Sstevel@tonic-gate 
39327c478bd9Sstevel@tonic-gate void
3933f7d61273Smeem Perror2(const char *cmd, const char *str)
39347c478bd9Sstevel@tonic-gate {
3935f7d61273Smeem 	int error = errno;
39367c478bd9Sstevel@tonic-gate 
3937f7d61273Smeem 	(void) fprintf(stderr, "ifconfig: %s: ", cmd);
39387c478bd9Sstevel@tonic-gate 
3939f7d61273Smeem 	switch (error) {
39407c478bd9Sstevel@tonic-gate 	case ENXIO:
3941f7d61273Smeem 		(void) fprintf(stderr, "%s: no such interface\n", str);
39427c478bd9Sstevel@tonic-gate 		break;
39437c478bd9Sstevel@tonic-gate 	case EPERM:
3944f7d61273Smeem 		(void) fprintf(stderr, "%s: permission denied\n", str);
39457c478bd9Sstevel@tonic-gate 		break;
3946f7d61273Smeem 	case EEXIST:
3947f7d61273Smeem 		(void) fprintf(stderr, "%s: already exists\n", str);
3948f7d61273Smeem 		break;
3949e899e593SGirish Moodalbail 	case ENAMETOOLONG:
3950e899e593SGirish Moodalbail 		(void) fprintf(stderr, "%s: interface name too long\n", str);
3951e899e593SGirish Moodalbail 		break;
3952e899e593SGirish Moodalbail 	case ERANGE:
3953e899e593SGirish Moodalbail 		(void) fprintf(stderr, "%s: logical interface id is outside "
3954e899e593SGirish Moodalbail 		    "allowed range\n", str);
3955e899e593SGirish Moodalbail 		break;
3956f7d61273Smeem 	default:
3957f7d61273Smeem 		errno = error;
3958f7d61273Smeem 		perror(str);
39597c478bd9Sstevel@tonic-gate 	}
39607c478bd9Sstevel@tonic-gate }
39617c478bd9Sstevel@tonic-gate 
39627c478bd9Sstevel@tonic-gate /*
39637c478bd9Sstevel@tonic-gate  * Print out error message (Perror2()) and exit
39647c478bd9Sstevel@tonic-gate  */
39657c478bd9Sstevel@tonic-gate void
3966f7d61273Smeem Perror2_exit(const char *cmd, const char *str)
39677c478bd9Sstevel@tonic-gate {
39687c478bd9Sstevel@tonic-gate 	Perror2(cmd, str);
39697c478bd9Sstevel@tonic-gate 	exit(1);
39707c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
39717c478bd9Sstevel@tonic-gate }
39727c478bd9Sstevel@tonic-gate 
3973c7e4935fSss void
3974c7e4935fSss Perrdlpi(const char *cmd, const char *linkname, int err)
3975c7e4935fSss {
3976c7e4935fSss 	(void) fprintf(stderr, "ifconfig: %s \"%s\": %s\n", cmd,
3977c7e4935fSss 	    linkname, dlpi_strerror(err));
3978c7e4935fSss }
3979c7e4935fSss 
3980c7e4935fSss /*
3981c7e4935fSss  * Print out error message (Perrdlpi()) and exit
3982c7e4935fSss  */
3983c7e4935fSss void
3984c7e4935fSss Perrdlpi_exit(const char *cmd, const char *linkname, int err)
3985c7e4935fSss {
3986c7e4935fSss 	Perrdlpi(cmd, linkname, err);
3987c7e4935fSss 	exit(1);
3988c7e4935fSss }
3989c7e4935fSss 
39907c478bd9Sstevel@tonic-gate /*
39917c478bd9Sstevel@tonic-gate  * If the last argument is non-NULL allow a <addr>/<n> syntax and
39927c478bd9Sstevel@tonic-gate  * pass out <n> in *plenp.
39937c478bd9Sstevel@tonic-gate  * If <n> doesn't parse return BAD_ADDR as *plenp.
39947c478bd9Sstevel@tonic-gate  * If no /<n> is present return NO_PREFIX as *plenp.
39957c478bd9Sstevel@tonic-gate  */
39967c478bd9Sstevel@tonic-gate static void
39977c478bd9Sstevel@tonic-gate in_getaddr(char *s, struct sockaddr *saddr, int *plenp)
39987c478bd9Sstevel@tonic-gate {
399969bb4bb4Scarlsonj 	/* LINTED: alignment */
40007c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
40017c478bd9Sstevel@tonic-gate 	struct hostent *hp;
40027c478bd9Sstevel@tonic-gate 	struct netent *np;
40037c478bd9Sstevel@tonic-gate 	char str[BUFSIZ];
40047c478bd9Sstevel@tonic-gate 	int error_num;
40057c478bd9Sstevel@tonic-gate 
40067c478bd9Sstevel@tonic-gate 	(void) strncpy(str, s, sizeof (str));
40077c478bd9Sstevel@tonic-gate 
40087c478bd9Sstevel@tonic-gate 	/*
40097c478bd9Sstevel@tonic-gate 	 * Look for '/'<n> is plenp
40107c478bd9Sstevel@tonic-gate 	 */
40117c478bd9Sstevel@tonic-gate 	if (plenp != NULL) {
40127c478bd9Sstevel@tonic-gate 		char *cp;
40137c478bd9Sstevel@tonic-gate 
40147906a3e0Smeem 		*plenp = in_getprefixlen(str, _B_TRUE, IP_ABITS);
40157c478bd9Sstevel@tonic-gate 		if (*plenp == BAD_ADDR)
40167c478bd9Sstevel@tonic-gate 			return;
40177c478bd9Sstevel@tonic-gate 		cp = strchr(str, '/');
40187c478bd9Sstevel@tonic-gate 		if (cp != NULL)
40197c478bd9Sstevel@tonic-gate 			*cp = '\0';
40207c478bd9Sstevel@tonic-gate 	} else if (strchr(str, '/') != NULL) {
40217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
40227c478bd9Sstevel@tonic-gate 		exit(1);
40237c478bd9Sstevel@tonic-gate 	}
40247c478bd9Sstevel@tonic-gate 
40257c478bd9Sstevel@tonic-gate 	(void) memset(sin, 0, sizeof (*sin));
40267c478bd9Sstevel@tonic-gate 
40277c478bd9Sstevel@tonic-gate 	/*
40287c478bd9Sstevel@tonic-gate 	 *	Try to catch attempts to set the broadcast address to all 1's.
40297c478bd9Sstevel@tonic-gate 	 */
40307c478bd9Sstevel@tonic-gate 	if (strcmp(str, "255.255.255.255") == 0 ||
40317c478bd9Sstevel@tonic-gate 	    (strtoul(str, (char **)NULL, 0) == 0xffffffffUL)) {
40327c478bd9Sstevel@tonic-gate 		sin->sin_family = AF_INET;
40337c478bd9Sstevel@tonic-gate 		sin->sin_addr.s_addr = 0xffffffff;
40347c478bd9Sstevel@tonic-gate 		return;
40357c478bd9Sstevel@tonic-gate 	}
40367c478bd9Sstevel@tonic-gate 
40377c478bd9Sstevel@tonic-gate 	hp = getipnodebyname(str, AF_INET, 0, &error_num);
40387c478bd9Sstevel@tonic-gate 	if (hp) {
40397c478bd9Sstevel@tonic-gate 		sin->sin_family = hp->h_addrtype;
40407c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
40417c478bd9Sstevel@tonic-gate 		freehostent(hp);
40427c478bd9Sstevel@tonic-gate 		return;
40437c478bd9Sstevel@tonic-gate 	}
40447c478bd9Sstevel@tonic-gate 	np = getnetbyname(str);
40457c478bd9Sstevel@tonic-gate 	if (np) {
40467c478bd9Sstevel@tonic-gate 		sin->sin_family = np->n_addrtype;
40477c478bd9Sstevel@tonic-gate 		sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
40487c478bd9Sstevel@tonic-gate 		return;
40497c478bd9Sstevel@tonic-gate 	}
40507c478bd9Sstevel@tonic-gate 	if (error_num == TRY_AGAIN) {
40517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address "
40527c478bd9Sstevel@tonic-gate 		    "(try again later)\n", s);
40537c478bd9Sstevel@tonic-gate 	} else {
40547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
40557c478bd9Sstevel@tonic-gate 	}
40567c478bd9Sstevel@tonic-gate 	exit(1);
40577c478bd9Sstevel@tonic-gate }
40587c478bd9Sstevel@tonic-gate 
40597c478bd9Sstevel@tonic-gate /*
40607c478bd9Sstevel@tonic-gate  * If the last argument is non-NULL allow a <addr>/<n> syntax and
40617c478bd9Sstevel@tonic-gate  * pass out <n> in *plenp.
40627c478bd9Sstevel@tonic-gate  * If <n> doesn't parse return BAD_ADDR as *plenp.
40637c478bd9Sstevel@tonic-gate  * If no /<n> is present return NO_PREFIX as *plenp.
40647c478bd9Sstevel@tonic-gate  */
40657c478bd9Sstevel@tonic-gate static void
40667c478bd9Sstevel@tonic-gate in6_getaddr(char *s, struct sockaddr *saddr, int *plenp)
40677c478bd9Sstevel@tonic-gate {
406869bb4bb4Scarlsonj 	/* LINTED: alignment */
40697c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)saddr;
40707c478bd9Sstevel@tonic-gate 	struct hostent *hp;
40717c478bd9Sstevel@tonic-gate 	char str[BUFSIZ];
40727c478bd9Sstevel@tonic-gate 	int error_num;
40737c478bd9Sstevel@tonic-gate 
40747c478bd9Sstevel@tonic-gate 	(void) strncpy(str, s, sizeof (str));
40757c478bd9Sstevel@tonic-gate 
40767c478bd9Sstevel@tonic-gate 	/*
40777c478bd9Sstevel@tonic-gate 	 * Look for '/'<n> is plenp
40787c478bd9Sstevel@tonic-gate 	 */
40797c478bd9Sstevel@tonic-gate 	if (plenp != NULL) {
40807c478bd9Sstevel@tonic-gate 		char *cp;
40817c478bd9Sstevel@tonic-gate 
40827906a3e0Smeem 		*plenp = in_getprefixlen(str, _B_TRUE, IPV6_ABITS);
40837c478bd9Sstevel@tonic-gate 		if (*plenp == BAD_ADDR)
40847c478bd9Sstevel@tonic-gate 			return;
40857c478bd9Sstevel@tonic-gate 		cp = strchr(str, '/');
40867c478bd9Sstevel@tonic-gate 		if (cp != NULL)
40877c478bd9Sstevel@tonic-gate 			*cp = '\0';
40887c478bd9Sstevel@tonic-gate 	} else if (strchr(str, '/') != NULL) {
40897c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
40907c478bd9Sstevel@tonic-gate 		exit(1);
40917c478bd9Sstevel@tonic-gate 	}
40927c478bd9Sstevel@tonic-gate 
40937c478bd9Sstevel@tonic-gate 	(void) memset(sin6, 0, sizeof (*sin6));
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate 	hp = getipnodebyname(str, AF_INET6, 0, &error_num);
40967c478bd9Sstevel@tonic-gate 	if (hp) {
40977c478bd9Sstevel@tonic-gate 		sin6->sin6_family = hp->h_addrtype;
40987c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin6->sin6_addr, hp->h_addr, hp->h_length);
40997c478bd9Sstevel@tonic-gate 		freehostent(hp);
41007c478bd9Sstevel@tonic-gate 		return;
41017c478bd9Sstevel@tonic-gate 	}
41027c478bd9Sstevel@tonic-gate 	if (error_num == TRY_AGAIN) {
41037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address "
41047c478bd9Sstevel@tonic-gate 		    "(try again later)\n", s);
41057c478bd9Sstevel@tonic-gate 	} else {
41067c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
41077c478bd9Sstevel@tonic-gate 	}
41087c478bd9Sstevel@tonic-gate 	exit(1);
41097c478bd9Sstevel@tonic-gate }
41107c478bd9Sstevel@tonic-gate 
41117c478bd9Sstevel@tonic-gate /*
41127c478bd9Sstevel@tonic-gate  * If "slash" is zero this parses the whole string as
41137c478bd9Sstevel@tonic-gate  * an integer. With "slash" non zero it parses the tail part as an integer.
41147c478bd9Sstevel@tonic-gate  *
41157c478bd9Sstevel@tonic-gate  * If it is not a valid integer this returns BAD_ADDR.
41167c478bd9Sstevel@tonic-gate  * If there is /<n> present this returns NO_PREFIX.
41177c478bd9Sstevel@tonic-gate  */
41187c478bd9Sstevel@tonic-gate static int
41197c478bd9Sstevel@tonic-gate in_getprefixlen(char *addr, boolean_t slash, int max_plen)
41207c478bd9Sstevel@tonic-gate {
41217c478bd9Sstevel@tonic-gate 	int prefixlen;
41227c478bd9Sstevel@tonic-gate 	char *str, *end;
41237c478bd9Sstevel@tonic-gate 
41247c478bd9Sstevel@tonic-gate 	if (slash) {
41257c478bd9Sstevel@tonic-gate 		str = strchr(addr, '/');
41267c478bd9Sstevel@tonic-gate 		if (str == NULL)
41277c478bd9Sstevel@tonic-gate 			return (NO_PREFIX);
41287c478bd9Sstevel@tonic-gate 		str++;
41297c478bd9Sstevel@tonic-gate 	} else
41307c478bd9Sstevel@tonic-gate 		str = addr;
41317c478bd9Sstevel@tonic-gate 
41327c478bd9Sstevel@tonic-gate 	prefixlen = strtol(str, &end, 10);
41337c478bd9Sstevel@tonic-gate 	if (prefixlen < 0)
41347c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
41357c478bd9Sstevel@tonic-gate 	if (str == end)
41367c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
41377c478bd9Sstevel@tonic-gate 	if (max_plen != 0 && max_plen < prefixlen)
41387c478bd9Sstevel@tonic-gate 		return (BAD_ADDR);
41397c478bd9Sstevel@tonic-gate 	return (prefixlen);
41407c478bd9Sstevel@tonic-gate }
41417c478bd9Sstevel@tonic-gate 
41427c478bd9Sstevel@tonic-gate /*
41437c478bd9Sstevel@tonic-gate  * Convert a prefix length to a mask.
41447c478bd9Sstevel@tonic-gate  * Returns 1 if ok. 0 otherwise.
41457c478bd9Sstevel@tonic-gate  * Assumes the mask array is zero'ed by the caller.
41467c478bd9Sstevel@tonic-gate  */
41477c478bd9Sstevel@tonic-gate static boolean_t
41487c478bd9Sstevel@tonic-gate in_prefixlentomask(int prefixlen, int maxlen, uchar_t *mask)
41497c478bd9Sstevel@tonic-gate {
41507c478bd9Sstevel@tonic-gate 	if (prefixlen < 0 || prefixlen > maxlen)
41517c478bd9Sstevel@tonic-gate 		return (0);
41527c478bd9Sstevel@tonic-gate 
41537c478bd9Sstevel@tonic-gate 	while (prefixlen > 0) {
41547c478bd9Sstevel@tonic-gate 		if (prefixlen >= 8) {
41557c478bd9Sstevel@tonic-gate 			*mask++ = 0xFF;
41567c478bd9Sstevel@tonic-gate 			prefixlen -= 8;
41577c478bd9Sstevel@tonic-gate 			continue;
41587c478bd9Sstevel@tonic-gate 		}
41597c478bd9Sstevel@tonic-gate 		*mask |= 1 << (8 - prefixlen);
41607c478bd9Sstevel@tonic-gate 		prefixlen--;
41617c478bd9Sstevel@tonic-gate 	}
41627c478bd9Sstevel@tonic-gate 	return (1);
41637c478bd9Sstevel@tonic-gate }
41647c478bd9Sstevel@tonic-gate 
41657c478bd9Sstevel@tonic-gate static void
41667c478bd9Sstevel@tonic-gate print_flags(uint64_t flags)
41677c478bd9Sstevel@tonic-gate {
41687c478bd9Sstevel@tonic-gate 	boolean_t first = _B_TRUE;
41697c478bd9Sstevel@tonic-gate 	int cnt, i;
41707c478bd9Sstevel@tonic-gate 
41717c478bd9Sstevel@tonic-gate 	(void) printf("flags=%llx", flags);
41727c478bd9Sstevel@tonic-gate 	cnt = sizeof (if_flags_tbl) / sizeof (if_flags_t);
41737c478bd9Sstevel@tonic-gate 	for (i = 0; i < cnt; i++) {
41747c478bd9Sstevel@tonic-gate 		if (flags & if_flags_tbl[i].iff_value) {
41757c478bd9Sstevel@tonic-gate 			if (first) {
41767c478bd9Sstevel@tonic-gate 				(void) printf("<");
41777c478bd9Sstevel@tonic-gate 				first = _B_FALSE;
41787c478bd9Sstevel@tonic-gate 			} else {
41797c478bd9Sstevel@tonic-gate 				/*
41807c478bd9Sstevel@tonic-gate 				 * It has to be here and not with the
41817c478bd9Sstevel@tonic-gate 				 * printf below because for the last one,
41827c478bd9Sstevel@tonic-gate 				 * we don't want a comma before the ">".
41837c478bd9Sstevel@tonic-gate 				 */
41847c478bd9Sstevel@tonic-gate 				(void) printf(",");
41857c478bd9Sstevel@tonic-gate 			}
41867c478bd9Sstevel@tonic-gate 			(void) printf("%s", if_flags_tbl[i].iff_name);
41877c478bd9Sstevel@tonic-gate 		}
41887c478bd9Sstevel@tonic-gate 	}
41897c478bd9Sstevel@tonic-gate 	if (!first)
41907c478bd9Sstevel@tonic-gate 		(void) printf(">");
41917c478bd9Sstevel@tonic-gate }
41927c478bd9Sstevel@tonic-gate 
41937c478bd9Sstevel@tonic-gate static void
4194e11c3f44Smeem print_config_flags(int af, uint64_t flags)
41957c478bd9Sstevel@tonic-gate {
4196e11c3f44Smeem 	if_config_cmd_t *cmdp;
41977c478bd9Sstevel@tonic-gate 
4198e11c3f44Smeem 	for (cmdp = if_config_cmd_tbl; cmdp->iff_flag != 0; cmdp++) {
4199e11c3f44Smeem 		if ((flags & cmdp->iff_flag) &&
4200e11c3f44Smeem 		    (cmdp->iff_af == AF_UNSPEC || cmdp->iff_af == af)) {
4201e11c3f44Smeem 			(void) printf("%s ", cmdp->iff_name);
42027c478bd9Sstevel@tonic-gate 		}
42037c478bd9Sstevel@tonic-gate 	}
42047c478bd9Sstevel@tonic-gate }
42057c478bd9Sstevel@tonic-gate 
42067c478bd9Sstevel@tonic-gate /*
420736e852a1SRaja Andra  * Use the configured directory lookup mechanism (e.g. files/NIS/...)
4208dd7a6f5fSkcpoon  * to find the network mask.  Returns true if we found one to set.
4209dd7a6f5fSkcpoon  *
4210dd7a6f5fSkcpoon  * The parameter addr_set controls whether we should get the address of
4211dd7a6f5fSkcpoon  * the working interface for the netmask query.  If addr_set is true,
4212dd7a6f5fSkcpoon  * we will use the address provided.  Otherwise, we will find the working
4213dd7a6f5fSkcpoon  * interface's address and use it instead.
42147c478bd9Sstevel@tonic-gate  */
42157c478bd9Sstevel@tonic-gate static boolean_t
4216dd7a6f5fSkcpoon in_getmask(struct sockaddr_in *saddr, boolean_t addr_set)
42177c478bd9Sstevel@tonic-gate {
42187c478bd9Sstevel@tonic-gate 	struct sockaddr_in ifaddr;
42197c478bd9Sstevel@tonic-gate 
42207c478bd9Sstevel@tonic-gate 	/*
4221dd7a6f5fSkcpoon 	 * Read the address from the interface if it is not passed in.
42227c478bd9Sstevel@tonic-gate 	 */
4223dd7a6f5fSkcpoon 	if (!addr_set) {
4224dd7a6f5fSkcpoon 		(void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
4225dd7a6f5fSkcpoon 		if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
4226dd7a6f5fSkcpoon 			if (errno != EADDRNOTAVAIL) {
4227dd7a6f5fSkcpoon 				(void) fprintf(stderr, "Need net number for "
4228dd7a6f5fSkcpoon 				    "mask\n");
4229dd7a6f5fSkcpoon 			}
4230dd7a6f5fSkcpoon 			return (_B_FALSE);
4231dd7a6f5fSkcpoon 		}
4232dd7a6f5fSkcpoon 		ifaddr = *((struct sockaddr_in *)&lifr.lifr_addr);
4233dd7a6f5fSkcpoon 	} else {
4234dd7a6f5fSkcpoon 		ifaddr.sin_addr = saddr->sin_addr;
42357c478bd9Sstevel@tonic-gate 	}
42367c478bd9Sstevel@tonic-gate 	if (getnetmaskbyaddr(ifaddr.sin_addr, &saddr->sin_addr) == 0) {
42377c478bd9Sstevel@tonic-gate 		saddr->sin_family = AF_INET;
42387c478bd9Sstevel@tonic-gate 		return (_B_TRUE);
42397c478bd9Sstevel@tonic-gate 	}
42407c478bd9Sstevel@tonic-gate 	return (_B_FALSE);
42417c478bd9Sstevel@tonic-gate }
42427c478bd9Sstevel@tonic-gate 
42437c478bd9Sstevel@tonic-gate static int
4244e11c3f44Smeem lifnum(const char *ifname)
4245e11c3f44Smeem {
4246e11c3f44Smeem 	const char *cp;
4247e11c3f44Smeem 
4248e11c3f44Smeem 	if ((cp = strchr(ifname, ':')) == NULL)
4249e11c3f44Smeem 		return (0);
4250e11c3f44Smeem 	else
4251e11c3f44Smeem 		return (atoi(cp + 1));
4252e11c3f44Smeem }
4253e11c3f44Smeem 
42547c478bd9Sstevel@tonic-gate static void
4255d62bc4baSyz add_ni(const char *name)
42567c478bd9Sstevel@tonic-gate {
42577c478bd9Sstevel@tonic-gate 	ni_t **pp;
42587c478bd9Sstevel@tonic-gate 	ni_t *p;
42597c478bd9Sstevel@tonic-gate 
42607c478bd9Sstevel@tonic-gate 	for (pp = &ni_list; (p = *pp) != NULL; pp = &(p->ni_next)) {
42617c478bd9Sstevel@tonic-gate 		if (strcmp(p->ni_name, name) == 0) {
42627c478bd9Sstevel@tonic-gate 			if (debug > 2)
42637c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "'%s' is a duplicate\n",
42647c478bd9Sstevel@tonic-gate 				    name);
42657c478bd9Sstevel@tonic-gate 			return;
42667c478bd9Sstevel@tonic-gate 		}
42677c478bd9Sstevel@tonic-gate 	}
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate 	if (debug > 2)
42707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "adding '%s'\n",
42717c478bd9Sstevel@tonic-gate 		    name);
42727c478bd9Sstevel@tonic-gate 
42737c478bd9Sstevel@tonic-gate 	if ((p = malloc(sizeof (ni_t))) == NULL)
42747c478bd9Sstevel@tonic-gate 		return;
42757c478bd9Sstevel@tonic-gate 
42767c478bd9Sstevel@tonic-gate 	(void) strlcpy(p->ni_name, name, sizeof (p->ni_name));
42777c478bd9Sstevel@tonic-gate 	p->ni_next = NULL;
42787c478bd9Sstevel@tonic-gate 
42797c478bd9Sstevel@tonic-gate 	*pp = p;
42807c478bd9Sstevel@tonic-gate 	num_ni++;
42817c478bd9Sstevel@tonic-gate }
42827c478bd9Sstevel@tonic-gate 
4283d62bc4baSyz static boolean_t
4284d62bc4baSyz ni_entry(const char *linkname, void *arg)
42857c478bd9Sstevel@tonic-gate {
4286c7e4935fSss 	dlpi_handle_t	dh;
4287da14cebeSEric Cheng 	datalink_class_t class;
42887c478bd9Sstevel@tonic-gate 
4289d4d1f7bfSVasumathi Sundaram - Sun Microsystems 	(void) dladm_name2info(arg, linkname, NULL, NULL, &class, NULL);
42904ac67f02SAnurag S. Maskey 
4291da14cebeSEric Cheng 	if (class == DATALINK_CLASS_ETHERSTUB)
4292da14cebeSEric Cheng 		return (_B_FALSE);
4293c7e4935fSss 	if (dlpi_open(linkname, &dh, 0) != DLPI_SUCCESS)
4294d62bc4baSyz 		return (_B_FALSE);
42957c478bd9Sstevel@tonic-gate 
4296d62bc4baSyz 	add_ni(linkname);
42977c478bd9Sstevel@tonic-gate 
4298c7e4935fSss 	dlpi_close(dh);
4299d62bc4baSyz 	return (_B_FALSE);
43007c478bd9Sstevel@tonic-gate }
43017c478bd9Sstevel@tonic-gate 
43027c478bd9Sstevel@tonic-gate /*
43037c478bd9Sstevel@tonic-gate  * dhcp-related routines
43047c478bd9Sstevel@tonic-gate  */
43057c478bd9Sstevel@tonic-gate 
43067c478bd9Sstevel@tonic-gate static int
43077c478bd9Sstevel@tonic-gate setifdhcp(const char *caller, const char *ifname, int argc, char *argv[])
43087c478bd9Sstevel@tonic-gate {
43097c478bd9Sstevel@tonic-gate 	dhcp_ipc_request_t	*request;
43107c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t	*reply	= NULL;
43117c478bd9Sstevel@tonic-gate 	int			timeout = DHCP_IPC_WAIT_DEFAULT;
43127c478bd9Sstevel@tonic-gate 	dhcp_ipc_type_t		type	= DHCP_START;
43137c478bd9Sstevel@tonic-gate 	int			error;
43147c478bd9Sstevel@tonic-gate 	boolean_t		is_primary = _B_FALSE;
43157c478bd9Sstevel@tonic-gate 	boolean_t		started = _B_FALSE;
43167c478bd9Sstevel@tonic-gate 
43177c478bd9Sstevel@tonic-gate 	for (argv++; --argc > 0; argv++) {
43187c478bd9Sstevel@tonic-gate 
43197c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "primary") == 0) {
43207c478bd9Sstevel@tonic-gate 			is_primary = _B_TRUE;
43217c478bd9Sstevel@tonic-gate 			continue;
43227c478bd9Sstevel@tonic-gate 		}
43237c478bd9Sstevel@tonic-gate 
43247c478bd9Sstevel@tonic-gate 		if (strcmp(*argv, "wait") == 0) {
43257c478bd9Sstevel@tonic-gate 			if (--argc <= 0) {
43267c478bd9Sstevel@tonic-gate 				usage();
43277c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
43287c478bd9Sstevel@tonic-gate 			}
43297c478bd9Sstevel@tonic-gate 			argv++;
43307c478bd9Sstevel@tonic-gate 
43317c478bd9Sstevel@tonic-gate 			if (strcmp(*argv, "forever") == 0) {
43327c478bd9Sstevel@tonic-gate 				timeout = DHCP_IPC_WAIT_FOREVER;
43337c478bd9Sstevel@tonic-gate 				continue;
43347c478bd9Sstevel@tonic-gate 			}
43357c478bd9Sstevel@tonic-gate 
43367c478bd9Sstevel@tonic-gate 			if (sscanf(*argv, "%d", &timeout) != 1) {
43377c478bd9Sstevel@tonic-gate 				usage();
43387c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
43397c478bd9Sstevel@tonic-gate 			}
43407c478bd9Sstevel@tonic-gate 
43417c478bd9Sstevel@tonic-gate 			if (timeout < 0) {
43427c478bd9Sstevel@tonic-gate 				usage();
43437c478bd9Sstevel@tonic-gate 				return (DHCP_EXIT_BADARGS);
43447c478bd9Sstevel@tonic-gate 			}
43457c478bd9Sstevel@tonic-gate 			continue;
43467c478bd9Sstevel@tonic-gate 		}
43477c478bd9Sstevel@tonic-gate 
43487c478bd9Sstevel@tonic-gate 		type = dhcp_string_to_request(*argv);
43497c478bd9Sstevel@tonic-gate 		if (type == -1) {
43507c478bd9Sstevel@tonic-gate 			usage();
43517c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_BADARGS);
43527c478bd9Sstevel@tonic-gate 		}
43537c478bd9Sstevel@tonic-gate 	}
43547c478bd9Sstevel@tonic-gate 
43557c478bd9Sstevel@tonic-gate 	/*
43567c478bd9Sstevel@tonic-gate 	 * Only try to start agent on start or inform; in all other cases it
43577c478bd9Sstevel@tonic-gate 	 * has to already be running for anything to make sense.
43587c478bd9Sstevel@tonic-gate 	 */
43597c478bd9Sstevel@tonic-gate 	if (type == DHCP_START || type == DHCP_INFORM) {
43607c478bd9Sstevel@tonic-gate 		if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1) {
43617c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: unable to start %s\n",
43627c478bd9Sstevel@tonic-gate 			    caller, DHCP_AGENT_PATH);
43637c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_FAILURE);
43647c478bd9Sstevel@tonic-gate 		}
43657c478bd9Sstevel@tonic-gate 		started = _B_TRUE;
43667c478bd9Sstevel@tonic-gate 	}
43677c478bd9Sstevel@tonic-gate 
43687c478bd9Sstevel@tonic-gate 	if (is_primary)
4369d04ccbb3Scarlsonj 		type |= DHCP_PRIMARY;
4370d04ccbb3Scarlsonj 
4371d04ccbb3Scarlsonj 	if (af != AF_INET)
4372d04ccbb3Scarlsonj 		type |= DHCP_V6;
43737c478bd9Sstevel@tonic-gate 
43747c478bd9Sstevel@tonic-gate 	request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
43757c478bd9Sstevel@tonic-gate 	if (request == NULL) {
43767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: out of memory\n", caller);
43777c478bd9Sstevel@tonic-gate 		return (DHCP_EXIT_SYSTEM);
43787c478bd9Sstevel@tonic-gate 	}
43797c478bd9Sstevel@tonic-gate 
43807c478bd9Sstevel@tonic-gate 	error = dhcp_ipc_make_request(request, &reply, timeout);
43817c478bd9Sstevel@tonic-gate 	if (error != 0) {
43827c478bd9Sstevel@tonic-gate 		free(request);
43837c478bd9Sstevel@tonic-gate 		/*
43847c478bd9Sstevel@tonic-gate 		 * Re-map connect error to not under control if we didn't try a
43857c478bd9Sstevel@tonic-gate 		 * start operation, as this has to be true and results in a
43867c478bd9Sstevel@tonic-gate 		 * clearer message, not to mention preserving compatibility
43877c478bd9Sstevel@tonic-gate 		 * with the days when we always started dhcpagent for every
43887c478bd9Sstevel@tonic-gate 		 * request.
43897c478bd9Sstevel@tonic-gate 		 */
43907c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_CONNECT && !started)
43917c478bd9Sstevel@tonic-gate 			error = DHCP_IPC_E_UNKIF;
43927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
43937c478bd9Sstevel@tonic-gate 		    dhcp_ipc_strerror(error));
43947c478bd9Sstevel@tonic-gate 		return (DHCP_EXIT_FAILURE);
43957c478bd9Sstevel@tonic-gate 	}
43967c478bd9Sstevel@tonic-gate 
43977c478bd9Sstevel@tonic-gate 	error = reply->return_code;
43987c478bd9Sstevel@tonic-gate 	if (error != 0) {
43997c478bd9Sstevel@tonic-gate 		free(request);
44007c478bd9Sstevel@tonic-gate 		free(reply);
44017c478bd9Sstevel@tonic-gate 
44027c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_TIMEOUT && timeout == 0)
44037c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_SUCCESS);
44047c478bd9Sstevel@tonic-gate 
44057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
44067c478bd9Sstevel@tonic-gate 		    dhcp_ipc_strerror(error));
44077c478bd9Sstevel@tonic-gate 
44087c478bd9Sstevel@tonic-gate 		if (error == DHCP_IPC_E_TIMEOUT)
44097c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_TIMEOUT);
44107c478bd9Sstevel@tonic-gate 		else
44117c478bd9Sstevel@tonic-gate 			return (DHCP_EXIT_IF_FAILURE);
44127c478bd9Sstevel@tonic-gate 	}
44137c478bd9Sstevel@tonic-gate 
44147c478bd9Sstevel@tonic-gate 	if (DHCP_IPC_CMD(type) == DHCP_STATUS) {
44157c478bd9Sstevel@tonic-gate 		(void) printf("%s", dhcp_status_hdr_string());
44167c478bd9Sstevel@tonic-gate 		(void) printf("%s", dhcp_status_reply_to_string(reply));
44177c478bd9Sstevel@tonic-gate 	}
44187c478bd9Sstevel@tonic-gate 
44197c478bd9Sstevel@tonic-gate 	free(request);
44207c478bd9Sstevel@tonic-gate 	free(reply);
44217c478bd9Sstevel@tonic-gate 	return (DHCP_EXIT_SUCCESS);
44227c478bd9Sstevel@tonic-gate }
44237c478bd9Sstevel@tonic-gate 
44247c478bd9Sstevel@tonic-gate static void
44257c478bd9Sstevel@tonic-gate usage(void)
44267c478bd9Sstevel@tonic-gate {
44277c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
44287c478bd9Sstevel@tonic-gate 	    "usage: ifconfig <interface> | -a[ 4 | 6 | D ][ u | d ][ Z ]\n");
44297c478bd9Sstevel@tonic-gate 
44307c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s",
44317c478bd9Sstevel@tonic-gate 	    "\t[ <addr_family> ]\n"
44327c478bd9Sstevel@tonic-gate 	    "\t[ <address>[/<prefix_length>] [ <dest_address> ] ]\n"
44337c478bd9Sstevel@tonic-gate 	    "\t[ set [ <address>][/<prefix_length>] ]"
44347c478bd9Sstevel@tonic-gate 	    " [ <address>/<prefix_length>] ]\n"
44357c478bd9Sstevel@tonic-gate 	    "\t[ destination <dest_address> ]\n"
44367c478bd9Sstevel@tonic-gate 	    "\t[ addif <address>[/<prefix_length>]"
44377c478bd9Sstevel@tonic-gate 	    "  [ <dest_address> ] ]\n"
44387c478bd9Sstevel@tonic-gate 	    "\t[ removeif <address>[/<prefix_length>] ]\n"
44397c478bd9Sstevel@tonic-gate 	    "\t[ arp | -arp ]\n"
44407c478bd9Sstevel@tonic-gate 	    "\t[ auto-revarp ]\n"
44417c478bd9Sstevel@tonic-gate 	    "\t[ broadcast <broad_addr> ]\n"
44427c478bd9Sstevel@tonic-gate 	    "\t[ index <if_index> ]\n"
44437c478bd9Sstevel@tonic-gate 	    "\t[ metric <n> ] [ mtu <n> ]\n"
44447c478bd9Sstevel@tonic-gate 	    "\t[ netmask <mask> ]\n"
44457c478bd9Sstevel@tonic-gate 	    "\t[ plumb ] [ unplumb ]\n"
44467c478bd9Sstevel@tonic-gate 	    "\t[ preferred | -preferred ]\n"
44477c478bd9Sstevel@tonic-gate 	    "\t[ private | -private ]\n"
44487c478bd9Sstevel@tonic-gate 	    "\t[ local | -local ]\n"
44497c478bd9Sstevel@tonic-gate 	    "\t[ router | -router ]\n"
44507c478bd9Sstevel@tonic-gate 	    "\t[ subnet <subnet_address>]\n"
44517c478bd9Sstevel@tonic-gate 	    "\t[ trailers | -trailers ]\n"
44527c478bd9Sstevel@tonic-gate 	    "\t[ token <address>/<prefix_length> ]\n"
44537c478bd9Sstevel@tonic-gate 	    "\t[ tsrc <tunnel_src_address> ]\n"
44547c478bd9Sstevel@tonic-gate 	    "\t[ tdst <tunnel_dest_address> ]\n"
44557c478bd9Sstevel@tonic-gate 	    "\t[ auth_algs <tunnel_AH_authentication_algorithm> ]\n"
44567c478bd9Sstevel@tonic-gate 	    "\t[ encr_algs <tunnel_ESP_encryption_algorithm> ]\n"
44577c478bd9Sstevel@tonic-gate 	    "\t[ encr_auth_algs <tunnel_ESP_authentication_algorithm> ]\n"
44587c478bd9Sstevel@tonic-gate 	    "\t[ up ] [ down ]\n"
44597c478bd9Sstevel@tonic-gate 	    "\t[ xmit | -xmit ]\n"
44607c478bd9Sstevel@tonic-gate 	    "\t[ modlist ]\n"
44617c478bd9Sstevel@tonic-gate 	    "\t[ modinsert <module_name@position> ]\n"
44627c478bd9Sstevel@tonic-gate 	    "\t[ modremove <module_name@position> ]\n"
4463e11c3f44Smeem 	    "\t[ ipmp ]\n"
44647c478bd9Sstevel@tonic-gate 	    "\t[ group <groupname>] | [ group \"\"]\n"
44657c478bd9Sstevel@tonic-gate 	    "\t[ deprecated | -deprecated ]\n"
44667c478bd9Sstevel@tonic-gate 	    "\t[ standby | -standby ]\n"
44677c478bd9Sstevel@tonic-gate 	    "\t[ failover | -failover ]\n"
44687c478bd9Sstevel@tonic-gate 	    "\t[ zone <zonename> | -zone ]\n"
446945916cd2Sjpk 	    "\t[ usesrc <interface> ]\n"
447045916cd2Sjpk 	    "\t[ all-zones ]\n");
44717c478bd9Sstevel@tonic-gate 
44727c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "or\n");
44737c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
44747c478bd9Sstevel@tonic-gate 	    "\tifconfig <interface> |  -a[ 4 | 6 | D ] [ u | d ]\n");
44757c478bd9Sstevel@tonic-gate 
44767c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s", "\tauto-dhcp | dhcp\n"
44777c478bd9Sstevel@tonic-gate 	    "\t[ wait <time> | forever ]\n\t[ primary ]\n"
44787c478bd9Sstevel@tonic-gate 	    "\tstart | drop | ping | release | status | inform\n");
44797c478bd9Sstevel@tonic-gate }
4480