xref: /illumos-gate/usr/src/lib/libipadm/common/ipadm_addr.c (revision 7c238add7266ae1358181a73e97925aef02c3343)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2013 by Delphix. All rights reserved.
25  * Copyright (c) 2016-2017, Chris Fraire <cfraire@me.com>.
26  * Copyright 2021 Tintri by DDN, Inc. All rights reserved.
27  * Copyright 2023 Oxide Computer Company
28  */
29 
30 /*
31  * This file contains functions for address management such as creating
32  * an address, deleting an address, enabling an address, disabling an
33  * address, bringing an address down or up, setting/getting properties
34  * on an address object and listing address information
35  * for all addresses in active as well as persistent configuration.
36  */
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/param.h>
40 #include <netdb.h>
41 #include <inet/ip.h>
42 #include <string.h>
43 #include <strings.h>
44 #include <assert.h>
45 #include <sys/sockio.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include <stropts.h>
49 #include <zone.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <fcntl.h>
53 #include <ctype.h>
54 #include <dhcpagent_util.h>
55 #include <dhcpagent_ipc.h>
56 #include <dhcp_inittab.h>
57 #include <dhcp_symbol.h>
58 #include <ipadm_ndpd.h>
59 #include <libdladm.h>
60 #include <libdllink.h>
61 #include <libdliptun.h>
62 #include <ifaddrs.h>
63 #include "libipadm_impl.h"
64 
65 #define	SIN6(a)		((struct sockaddr_in6 *)a)
66 #define	SIN(a)		((struct sockaddr_in *)a)
67 
68 static ipadm_status_t	i_ipadm_create_addr(ipadm_handle_t, ipadm_addrobj_t,
69 			    uint32_t);
70 static ipadm_status_t	i_ipadm_create_dhcp(ipadm_handle_t, ipadm_addrobj_t,
71 			    uint32_t);
72 static ipadm_status_t	i_ipadm_delete_dhcp(ipadm_handle_t, ipadm_addrobj_t,
73 			    boolean_t);
74 static ipadm_status_t	i_ipadm_refresh_dhcp(ipadm_addrobj_t);
75 static ipadm_status_t	i_ipadm_get_db_addr(ipadm_handle_t, const char *,
76 			    const char *, nvlist_t **);
77 static ipadm_status_t	i_ipadm_op_dhcp(ipadm_addrobj_t, dhcp_ipc_type_t,
78 			    int *);
79 static ipadm_status_t	i_ipadm_dhcp_status(ipadm_addrobj_t addr,
80 			    dhcp_status_t *status, int *dhcperror);
81 static ipadm_status_t	i_ipadm_validate_create_addr(ipadm_handle_t,
82 			    ipadm_addrobj_t, uint32_t);
83 static ipadm_status_t	i_ipadm_addr_persist_nvl(ipadm_handle_t, nvlist_t *,
84 			    uint32_t);
85 static ipadm_status_t	i_ipadm_get_default_prefixlen(struct sockaddr_storage *,
86 			    uint32_t *);
87 static ipadm_status_t	i_ipadm_get_static_addr_db(ipadm_handle_t,
88 			    ipadm_addrobj_t);
89 static boolean_t	i_ipadm_is_user_aobjname_valid(const char *);
90 static ipadm_prop_desc_t	*i_ipadm_get_addrprop_desc(const char *pname);
91 
92 /*
93  * Callback functions to retrieve property values from the kernel. These
94  * functions, when required, translate the values from the kernel to a format
95  * suitable for printing. They also retrieve DEFAULT, PERM and POSSIBLE values
96  * for a given property.
97  */
98 static ipadm_pd_getf_t	i_ipadm_get_prefixlen, i_ipadm_get_addr_flag,
99 			i_ipadm_get_zone, i_ipadm_get_broadcast,
100 			i_ipadm_get_primary, i_ipadm_get_reqhost;
101 
102 /*
103  * Callback functions to set property values. These functions translate the
104  * values to a format suitable for kernel consumption, allocate the necessary
105  * ioctl buffers and then invoke ioctl(); or in the case of reqhost, get the
106  * collaborating agent to set the value.
107  */
108 static ipadm_pd_setf_t	i_ipadm_set_prefixlen, i_ipadm_set_addr_flag,
109 			i_ipadm_set_zone, i_ipadm_set_reqhost;
110 
111 static ipadm_status_t	i_ipadm_set_aobj_addrprop(ipadm_handle_t iph,
112     ipadm_addrobj_t ipaddr, uint_t flags, const char *propname);
113 
114 /* address properties description table */
115 ipadm_prop_desc_t ipadm_addrprop_table[] = {
116 	{ "broadcast", NULL, IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
117 	    NULL, NULL, i_ipadm_get_broadcast },
118 
119 	{ "deprecated", NULL, IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
120 	    i_ipadm_set_addr_flag, i_ipadm_get_onoff,
121 	    i_ipadm_get_addr_flag },
122 
123 	{ IPADM_NVP_PREFIXLEN, NULL, IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
124 	    i_ipadm_set_prefixlen, i_ipadm_get_prefixlen,
125 	    i_ipadm_get_prefixlen },
126 
127 	/*
128 	 * primary is read-only because there is no operation to un-set
129 	 * DHCP_IF_PRIMARY in dhcpagent except to delete-addr and then
130 	 * re-create-addr.
131 	 */
132 	{ "primary", NULL, IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
133 		NULL, NULL, i_ipadm_get_primary },
134 
135 	{ "private", NULL, IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
136 	    i_ipadm_set_addr_flag, i_ipadm_get_onoff, i_ipadm_get_addr_flag },
137 
138 	{ IPADM_NVP_REQHOST, NULL, IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
139 	    i_ipadm_set_reqhost, NULL, i_ipadm_get_reqhost },
140 
141 	{ "transmit", NULL, IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
142 	    i_ipadm_set_addr_flag, i_ipadm_get_onoff, i_ipadm_get_addr_flag },
143 
144 	{ "zone", NULL, IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
145 	    i_ipadm_set_zone, NULL, i_ipadm_get_zone },
146 
147 	{ NULL, NULL, 0, 0, 0, NULL, NULL, NULL }
148 };
149 
150 static ipadm_prop_desc_t up_addrprop = { "up", NULL, IPADMPROP_CLASS_ADDR,
151 					MOD_PROTO_NONE, 0, NULL, NULL, NULL };
152 
153 /*
154  * Helper function that initializes the `ipadm_ifname', `ipadm_aobjname', and
155  * `ipadm_atype' fields of the given `ipaddr'.
156  */
157 void
158 i_ipadm_init_addr(ipadm_addrobj_t ipaddr, const char *ifname,
159     const char *aobjname, ipadm_addr_type_t atype)
160 {
161 	bzero(ipaddr, sizeof (struct ipadm_addrobj_s));
162 	(void) strlcpy(ipaddr->ipadm_ifname, ifname,
163 	    sizeof (ipaddr->ipadm_ifname));
164 	(void) strlcpy(ipaddr->ipadm_aobjname, aobjname,
165 	    sizeof (ipaddr->ipadm_aobjname));
166 	ipaddr->ipadm_atype = atype;
167 }
168 
169 /*
170  * Determine the permission of the property depending on whether it has a
171  * set() and/or get() callback functions.
172  */
173 static ipadm_status_t
174 i_ipadm_pd2permstr(ipadm_prop_desc_t *pdp, char *buf, uint_t *bufsize)
175 {
176 	uint_t	perm;
177 	size_t	nbytes;
178 
179 	perm = 0;
180 	if (pdp->ipd_set != NULL)
181 		perm |= MOD_PROP_PERM_WRITE;
182 	if (pdp->ipd_get != NULL)
183 		perm |= MOD_PROP_PERM_READ;
184 
185 	nbytes = snprintf(buf, *bufsize, "%c%c",
186 	    ((perm & MOD_PROP_PERM_READ) != 0) ? 'r' : '-',
187 	    ((perm & MOD_PROP_PERM_WRITE) != 0) ? 'w' : '-');
188 
189 	if (nbytes >= *bufsize) {
190 		/* insufficient buffer space */
191 		*bufsize = nbytes + 1;
192 		return (IPADM_NO_BUFS);
193 	}
194 	return (IPADM_SUCCESS);
195 }
196 
197 /*
198  * Given an addrobj with `ipadm_aobjname' filled in, i_ipadm_get_addrobj()
199  * retrieves the information necessary for any operation on the object,
200  * such as delete-addr, enable-addr, disable-addr, up-addr, down-addr,
201  * refresh-addr, get-addrprop or set-addrprop. The information include
202  * the logical interface number, address type, address family,
203  * the interface id (if the address type is IPADM_ADDR_IPV6_ADDRCONF) and
204  * the ipadm_flags that indicate if the address is present in
205  * active configuration or persistent configuration or both. If the address
206  * is not found, IPADM_NOTSUP is returned.
207  */
208 ipadm_status_t
209 i_ipadm_get_addrobj(ipadm_handle_t iph, ipadm_addrobj_t ipaddr)
210 {
211 	ipmgmt_aobjop_arg_t	larg;
212 	ipmgmt_aobjop_rval_t	rval, *rvalp;
213 	int			err = 0;
214 
215 	/* populate the door_call argument structure */
216 	larg.ia_cmd = IPMGMT_CMD_AOBJNAME2ADDROBJ;
217 	(void) strlcpy(larg.ia_aobjname, ipaddr->ipadm_aobjname,
218 	    sizeof (larg.ia_aobjname));
219 
220 	rvalp = &rval;
221 	err = ipadm_door_call(iph, &larg, sizeof (larg), (void **)&rvalp,
222 	    sizeof (rval), B_FALSE);
223 	if (err != 0)
224 		return (ipadm_errno2status(err));
225 	(void) strlcpy(ipaddr->ipadm_ifname, rval.ir_ifname,
226 	    sizeof (ipaddr->ipadm_ifname));
227 	ipaddr->ipadm_lifnum = rval.ir_lnum;
228 	ipaddr->ipadm_atype = rval.ir_atype;
229 	ipaddr->ipadm_af = rval.ir_family;
230 	ipaddr->ipadm_flags = rval.ir_flags;
231 	switch (rval.ir_atype) {
232 	case IPADM_ADDR_IPV6_ADDRCONF:
233 		ipaddr->ipadm_intfid = rval.ipmgmt_ir_intfid;
234 		break;
235 	case IPADM_ADDR_DHCP:
236 		if (strlcpy(ipaddr->ipadm_reqhost, rval.ipmgmt_ir_reqhost,
237 		    sizeof (ipaddr->ipadm_reqhost)) >=
238 		    sizeof (ipaddr->ipadm_reqhost)) {
239 			/*
240 			 * shouldn't get here as the buffers are defined
241 			 * with same length, MAX_NAME_LEN
242 			 */
243 			return (IPADM_FAILURE);
244 		}
245 		break;
246 	default:
247 		break;
248 	}
249 
250 	return (IPADM_SUCCESS);
251 }
252 
253 /*
254  * Retrieves the static address (IPv4 or IPv6) for the given address object
255  * in `ipaddr' from persistent DB.
256  */
257 static ipadm_status_t
258 i_ipadm_get_static_addr_db(ipadm_handle_t iph, ipadm_addrobj_t ipaddr)
259 {
260 	ipadm_status_t		status;
261 	nvlist_t		*onvl;
262 	nvlist_t		*anvl = NULL;
263 	nvlist_t		*nvladdr;
264 	nvpair_t		*nvp;
265 	char			*name;
266 	char			*aobjname = ipaddr->ipadm_aobjname;
267 	char			*sname;
268 	sa_family_t		af = AF_UNSPEC;
269 
270 	/*
271 	 * Get the address line in the nvlist `onvl' from ipmgmtd daemon.
272 	 */
273 	status = i_ipadm_get_db_addr(iph, NULL, aobjname, &onvl);
274 	if (status != IPADM_SUCCESS)
275 		return (status);
276 	/*
277 	 * Walk through the nvlist `onvl' to extract the IPADM_NVP_IPV4ADDR
278 	 * or the IPADM_NVP_IPV6ADDR name-value pair.
279 	 */
280 	for (nvp = nvlist_next_nvpair(onvl, NULL); nvp != NULL;
281 	    nvp = nvlist_next_nvpair(onvl, NULL)) {
282 		if (nvpair_value_nvlist(nvp, &anvl) != 0)
283 			continue;
284 		if (nvlist_exists(anvl, IPADM_NVP_IPV4ADDR) ||
285 		    nvlist_exists(anvl, IPADM_NVP_IPV6ADDR))
286 			break;
287 	}
288 	nvlist_free(onvl);
289 
290 	if (nvp == NULL)
291 		return (IPADM_NOTFOUND);
292 
293 	for (nvp = nvlist_next_nvpair(anvl, NULL);
294 	    nvp != NULL; nvp = nvlist_next_nvpair(anvl, nvp)) {
295 		name = nvpair_name(nvp);
296 		if (strcmp(name, IPADM_NVP_IPV4ADDR) == 0) {
297 			af = AF_INET;
298 			break;
299 		} else if (strcmp(name, IPADM_NVP_IPV6ADDR) == 0) {
300 			af = AF_INET6;
301 			break;
302 		}
303 	}
304 	assert(af != AF_UNSPEC);
305 
306 	if (nvpair_value_nvlist(nvp, &nvladdr) != 0 ||
307 	    nvlist_lookup_string(nvladdr, IPADM_NVP_IPADDRHNAME, &sname) != 0 ||
308 	    ipadm_set_addr(ipaddr, sname, af) != IPADM_SUCCESS)
309 		return (IPADM_NOTFOUND);
310 
311 	return (IPADM_SUCCESS);
312 }
313 
314 /*
315  * For the given `addrobj->ipadm_lifnum' and `addrobj->ipadm_af', this function
316  * fills in the address objname, the address type and the ipadm_flags.
317  */
318 ipadm_status_t
319 i_ipadm_get_lif2addrobj(ipadm_handle_t iph, ipadm_addrobj_t addrobj)
320 {
321 	ipmgmt_aobjop_arg_t	larg;
322 	ipmgmt_aobjop_rval_t	rval, *rvalp;
323 	int			err;
324 
325 	larg.ia_cmd = IPMGMT_CMD_LIF2ADDROBJ;
326 	(void) strlcpy(larg.ia_ifname, addrobj->ipadm_ifname,
327 	    sizeof (larg.ia_ifname));
328 	larg.ia_lnum = addrobj->ipadm_lifnum;
329 	larg.ia_family = addrobj->ipadm_af;
330 
331 	rvalp = &rval;
332 	err = ipadm_door_call(iph, &larg, sizeof (larg), (void **)&rvalp,
333 	    sizeof (rval), B_FALSE);
334 	if (err != 0)
335 		return (ipadm_errno2status(err));
336 	(void) strlcpy(addrobj->ipadm_aobjname, rval.ir_aobjname,
337 	    sizeof (addrobj->ipadm_aobjname));
338 	addrobj->ipadm_atype = rval.ir_atype;
339 	addrobj->ipadm_flags = rval.ir_flags;
340 
341 	return (IPADM_SUCCESS);
342 }
343 
344 /*
345  * Adds an addrobj to ipmgmtd daemon's aobjmap (active configuration).
346  * with the given name and logical interface number.
347  * This API is called by in.ndpd to add addrobjs when new prefixes or
348  * dhcpv6 addresses are configured.
349  */
350 ipadm_status_t
351 ipadm_add_aobjname(ipadm_handle_t iph, const char *ifname, sa_family_t af,
352     const char *aobjname, ipadm_addr_type_t atype, int lnum)
353 {
354 	ipmgmt_aobjop_arg_t	larg;
355 	int			err;
356 
357 	larg.ia_cmd = IPMGMT_CMD_ADDROBJ_ADD;
358 	(void) strlcpy(larg.ia_ifname, ifname, sizeof (larg.ia_ifname));
359 	(void) strlcpy(larg.ia_aobjname, aobjname, sizeof (larg.ia_aobjname));
360 	larg.ia_atype = atype;
361 	larg.ia_lnum = lnum;
362 	larg.ia_family = af;
363 	err = ipadm_door_call(iph, &larg, sizeof (larg), NULL, 0, B_FALSE);
364 	return (ipadm_errno2status(err));
365 }
366 
367 /*
368  * Deletes an address object with given name and logical number from ipmgmtd
369  * daemon's aobjmap (active configuration). This API is called by in.ndpd to
370  * remove addrobjs when auto-configured prefixes or dhcpv6 addresses are
371  * removed.
372  */
373 ipadm_status_t
374 ipadm_delete_aobjname(ipadm_handle_t iph, const char *ifname, sa_family_t af,
375     const char *aobjname, ipadm_addr_type_t atype, int lnum)
376 {
377 	struct ipadm_addrobj_s	aobj;
378 
379 	i_ipadm_init_addr(&aobj, ifname, aobjname, atype);
380 	aobj.ipadm_af = af;
381 	aobj.ipadm_lifnum = lnum;
382 	return (i_ipadm_delete_addrobj(iph, &aobj, IPADM_OPT_ACTIVE));
383 }
384 
385 /*
386  * Gets all the addresses from active configuration and populates the
387  * address information in `addrinfo'.
388  */
389 ipadm_status_t
390 i_ipadm_active_addr_info(ipadm_handle_t iph, const char *ifname,
391     ipadm_addr_info_t **addrinfo, uint32_t ipadm_flags, int64_t lifc_flags)
392 {
393 	ipadm_status_t		status;
394 	struct ifaddrs		*ifap, *ifa;
395 	ipadm_addr_info_t	*curr, *prev = NULL;
396 	struct ifaddrs		*cifaddr;
397 	struct lifreq		lifr;
398 	int			sock;
399 	uint64_t		flags;
400 	char			cifname[LIFNAMSIZ];
401 	struct sockaddr_in6	*sin6;
402 	struct ipadm_addrobj_s	ipaddr;
403 	char			*sep;
404 	int			lnum;
405 
406 retry:
407 	*addrinfo = NULL;
408 
409 	/* Get all the configured addresses */
410 	if (getallifaddrs(AF_UNSPEC, &ifa, lifc_flags) < 0)
411 		return (ipadm_errno2status(errno));
412 	/* Return if there is nothing to process. */
413 	if (ifa == NULL)
414 		return (IPADM_SUCCESS);
415 	bzero(&lifr, sizeof (lifr));
416 	for (ifap = ifa; ifap != NULL; ifap = ifap->ifa_next) {
417 		struct sockaddr_storage data;
418 
419 		if (ifap->ifa_addr->sa_family == AF_LINK)
420 			continue;
421 
422 		(void) strlcpy(cifname, ifap->ifa_name, sizeof (cifname));
423 		lnum = 0;
424 		if ((sep = strrchr(cifname, ':')) != NULL) {
425 			*sep++ = '\0';
426 			lnum = atoi(sep);
427 		}
428 		if (ifname != NULL && strcmp(cifname, ifname) != 0)
429 			continue;
430 		if (!(ipadm_flags & IPADM_OPT_ZEROADDR) &&
431 		    sockaddrunspec(ifap->ifa_addr) &&
432 		    !(ifap->ifa_flags & IFF_DHCPRUNNING))
433 			continue;
434 
435 		/* Allocate and populate the current node in the list. */
436 		if ((curr = calloc(1, sizeof (ipadm_addr_info_t))) == NULL)
437 			goto fail;
438 
439 		/* Link to the list in `addrinfo'. */
440 		if (prev != NULL)
441 			prev->ia_ifa.ifa_next = &curr->ia_ifa;
442 		else
443 			*addrinfo = curr;
444 		prev = curr;
445 
446 		cifaddr = &curr->ia_ifa;
447 		if ((cifaddr->ifa_name = strdup(ifap->ifa_name)) == NULL)
448 			goto fail;
449 		cifaddr->ifa_flags = ifap->ifa_flags;
450 		cifaddr->ifa_addr = malloc(sizeof (struct sockaddr_storage));
451 		if (cifaddr->ifa_addr == NULL)
452 			goto fail;
453 		(void) memcpy(cifaddr->ifa_addr, ifap->ifa_addr,
454 		    sizeof (struct sockaddr_storage));
455 		cifaddr->ifa_netmask = malloc(sizeof (struct sockaddr_storage));
456 		if (cifaddr->ifa_netmask == NULL)
457 			goto fail;
458 		(void) memcpy(cifaddr->ifa_netmask, ifap->ifa_netmask,
459 		    sizeof (struct sockaddr_storage));
460 		if (ifap->ifa_flags & IFF_POINTOPOINT) {
461 			cifaddr->ifa_dstaddr = malloc(
462 			    sizeof (struct sockaddr_storage));
463 			if (cifaddr->ifa_dstaddr == NULL)
464 				goto fail;
465 			(void) memcpy(cifaddr->ifa_dstaddr, ifap->ifa_dstaddr,
466 			    sizeof (struct sockaddr_storage));
467 		} else if (ifap->ifa_flags & IFF_BROADCAST) {
468 			cifaddr->ifa_broadaddr = malloc(
469 			    sizeof (struct sockaddr_storage));
470 			if (cifaddr->ifa_broadaddr == NULL)
471 				goto fail;
472 			(void) memcpy(cifaddr->ifa_broadaddr,
473 			    ifap->ifa_broadaddr,
474 			    sizeof (struct sockaddr_storage));
475 		}
476 		/* Get the addrobj name stored for this logical interface. */
477 		ipaddr.ipadm_aobjname[0] = '\0';
478 		(void) strlcpy(ipaddr.ipadm_ifname, cifname,
479 		    sizeof (ipaddr.ipadm_ifname));
480 		ipaddr.ipadm_lifnum = lnum;
481 		ipaddr.ipadm_af = ifap->ifa_addr->sa_family;
482 		status = i_ipadm_get_lif2addrobj(iph, &ipaddr);
483 
484 		/*
485 		 * Find address type from ifa_flags, if we could not get it
486 		 * from daemon.
487 		 */
488 		(void) memcpy(&data, ifap->ifa_addr,
489 		    sizeof (struct sockaddr_in6));
490 		sin6 = SIN6(&data);
491 		flags = ifap->ifa_flags;
492 		if (status == IPADM_SUCCESS) {
493 			(void) strlcpy(curr->ia_aobjname, ipaddr.ipadm_aobjname,
494 			    sizeof (curr->ia_aobjname));
495 			curr->ia_atype = ipaddr.ipadm_atype;
496 		} else if ((flags & IFF_DHCPRUNNING) && (!(flags & IFF_IPV6) ||
497 		    !IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
498 			curr->ia_atype = IPADM_ADDR_DHCP;
499 		} else if (flags & IFF_ADDRCONF) {
500 			curr->ia_atype = IPADM_ADDR_IPV6_ADDRCONF;
501 		} else {
502 			curr->ia_atype = IPADM_ADDR_STATIC;
503 		}
504 		/*
505 		 * Populate the flags for the active configuration from the
506 		 * `ifa_flags'.
507 		 */
508 		if (!(flags & IFF_UP)) {
509 			if (flags & IFF_DUPLICATE)
510 				curr->ia_state = IFA_DUPLICATE;
511 			else
512 				curr->ia_state = IFA_DOWN;
513 		} else {
514 			curr->ia_cflags |= IA_UP;
515 			if (flags & IFF_RUNNING) {
516 				(void) strlcpy(lifr.lifr_name, ifap->ifa_name,
517 				    sizeof (lifr.lifr_name));
518 				sock = (ifap->ifa_addr->sa_family == AF_INET) ?
519 				    iph->iph_sock : iph->iph_sock6;
520 				if (ioctl(sock, SIOCGLIFDADSTATE,
521 				    (caddr_t)&lifr) < 0) {
522 					if (errno == ENXIO) {
523 						freeifaddrs(ifa);
524 						ipadm_free_addr_info(*addrinfo);
525 						goto retry;
526 					}
527 					goto fail;
528 				}
529 				if (lifr.lifr_dadstate == DAD_IN_PROGRESS)
530 					curr->ia_state = IFA_TENTATIVE;
531 				else
532 					curr->ia_state = IFA_OK;
533 			} else {
534 				curr->ia_state = IFA_INACCESSIBLE;
535 			}
536 		}
537 		if (flags & IFF_UNNUMBERED)
538 			curr->ia_cflags |= IA_UNNUMBERED;
539 		if (flags & IFF_PRIVATE)
540 			curr->ia_cflags |= IA_PRIVATE;
541 		if (flags & IFF_TEMPORARY)
542 			curr->ia_cflags |= IA_TEMPORARY;
543 		if (flags & IFF_DEPRECATED)
544 			curr->ia_cflags |= IA_DEPRECATED;
545 
546 	}
547 
548 	freeifaddrs(ifa);
549 	return (IPADM_SUCCESS);
550 
551 fail:
552 	/* On error, cleanup everything and return. */
553 	ipadm_free_addr_info(*addrinfo);
554 	*addrinfo = NULL;
555 	freeifaddrs(ifa);
556 	return (ipadm_errno2status(errno));
557 }
558 
559 /*
560  * From the given `name', i_ipadm_name2atype() deduces the address type
561  * and address family. If the `name' implies an address, it returns B_TRUE.
562  * Else, returns B_FALSE and leaves the output parameters unchanged.
563  */
564 boolean_t
565 i_ipadm_name2atype(const char *name, sa_family_t *af, ipadm_addr_type_t *type)
566 {
567 	boolean_t	is_addr = B_TRUE;
568 
569 	if (strcmp(name, IPADM_NVP_IPV4ADDR) == 0) {
570 		*af = AF_INET;
571 		*type = IPADM_ADDR_STATIC;
572 	} else if (strcmp(name, IPADM_NVP_IPV6ADDR) == 0) {
573 		*af = AF_INET6;
574 		*type = IPADM_ADDR_STATIC;
575 	} else if (strcmp(name, IPADM_NVP_DHCP) == 0) {
576 		*af = AF_INET;
577 		*type = IPADM_ADDR_DHCP;
578 	} else if (strcmp(name, IPADM_NVP_INTFID) == 0) {
579 		*af = AF_INET6;
580 		*type = IPADM_ADDR_IPV6_ADDRCONF;
581 	} else {
582 		is_addr = B_FALSE;
583 	}
584 
585 	return (is_addr);
586 }
587 
588 /*
589  * Parses the given nvlist `nvl' for an address or an address property.
590  * The input nvlist must contain either an address or an address property.
591  * `ainfo' is an input as well as output parameter. When an address or an
592  * address property is found, `ainfo' is updated with the information found.
593  * Some of the fields may be already filled in by the calling function.
594  *
595  * The fields that will be filled/updated by this function are `ia_pflags',
596  * `ia_sname' and `ia_dname'. Values for `ia_pflags' are obtained if the `nvl'
597  * contains an address property. `ia_sname', `ia_dname', and `ia_pflags' are
598  * obtained if `nvl' contains an address.
599  */
600 static ipadm_status_t
601 i_ipadm_nvl2ainfo_common(nvlist_t *nvl, ipadm_addr_info_t *ainfo)
602 {
603 	nvlist_t		*nvladdr;
604 	char			*name;
605 	char			*propstr = NULL;
606 	char			*sname, *dname;
607 	nvpair_t		*nvp;
608 	sa_family_t		af;
609 	ipadm_addr_type_t	atype;
610 	boolean_t		is_addr = B_FALSE;
611 	int			err;
612 
613 	for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
614 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
615 		name = nvpair_name(nvp);
616 		if (i_ipadm_name2atype(name, &af, &atype)) {
617 			err = nvpair_value_nvlist(nvp, &nvladdr);
618 			is_addr = B_TRUE;
619 		} else if (IPADM_PRIV_NVP(name)) {
620 			continue;
621 		} else {
622 			err = nvpair_value_string(nvp, &propstr);
623 		}
624 		if (err != 0)
625 			return (ipadm_errno2status(err));
626 	}
627 
628 	if (is_addr) {
629 		/*
630 		 * We got an address from the nvlist `nvl'.
631 		 * Parse `nvladdr' and populate relevant information
632 		 * in `ainfo'.
633 		 */
634 		switch (atype) {
635 		case IPADM_ADDR_STATIC:
636 			if (strcmp(name, "up") == 0 &&
637 			    strcmp(propstr, "yes") == 0) {
638 				ainfo->ia_pflags |= IA_UP;
639 			}
640 			/*
641 			 * For static addresses, we need to get the hostnames.
642 			 */
643 			err = nvlist_lookup_string(nvladdr,
644 			    IPADM_NVP_IPADDRHNAME, &sname);
645 			if (err != 0)
646 				return (ipadm_errno2status(err));
647 			(void) strlcpy(ainfo->ia_sname, sname,
648 			    sizeof (ainfo->ia_sname));
649 			err = nvlist_lookup_string(nvladdr,
650 			    IPADM_NVP_IPDADDRHNAME, &dname);
651 			if (err == 0) {
652 				(void) strlcpy(ainfo->ia_dname, dname,
653 				    sizeof (ainfo->ia_dname));
654 			}
655 			break;
656 		case IPADM_ADDR_DHCP:
657 		case IPADM_ADDR_IPV6_ADDRCONF:
658 			/*
659 			 * dhcp and addrconf address objects are always
660 			 * marked up when re-enabled.
661 			 */
662 			ainfo->ia_pflags |= IA_UP;
663 			break;
664 		default:
665 			return (IPADM_FAILURE);
666 		}
667 	} else {
668 		/*
669 		 * We got an address property from `nvl'. Parse the
670 		 * name and the property value. Update the `ainfo->ia_pflags'
671 		 * for the flags.
672 		 */
673 		if (strcmp(name, "deprecated") == 0) {
674 			if (strcmp(propstr, IPADM_ONSTR) == 0)
675 				ainfo->ia_pflags |= IA_DEPRECATED;
676 		} else if (strcmp(name, "private") == 0) {
677 			if (strcmp(propstr, IPADM_ONSTR) == 0)
678 				ainfo->ia_pflags |= IA_PRIVATE;
679 		}
680 	}
681 
682 	return (IPADM_SUCCESS);
683 }
684 
685 /*
686  * Parses the given nvlist `nvl' for an address or an address property.
687  * The input nvlist must contain either an address or an address property.
688  * `ainfo' is an input as well as output parameter. When an address or an
689  * address property is found, `ainfo' is updated with the information found.
690  * Some of the fields may be already filled in by the calling function,
691  * because of previous calls to i_ipadm_nvl2ainfo_active().
692  *
693  * Since the address object in `nvl' is also in the active configuration, the
694  * fields that will be filled/updated by this function are `ia_pflags',
695  * `ia_sname' and `ia_dname'.
696  *
697  * If this function returns an error, the calling function will take
698  * care of freeing the fields in `ainfo'.
699  */
700 static ipadm_status_t
701 i_ipadm_nvl2ainfo_active(nvlist_t *nvl, ipadm_addr_info_t *ainfo)
702 {
703 	return (i_ipadm_nvl2ainfo_common(nvl, ainfo));
704 }
705 
706 /*
707  * Parses the given nvlist `nvl' for an address or an address property.
708  * The input nvlist must contain either an address or an address property.
709  * `ainfo' is an input as well as output parameter. When an address or an
710  * address property is found, `ainfo' is updated with the information found.
711  * Some of the fields may be already filled in by the calling function,
712  * because of previous calls to i_ipadm_nvl2ainfo_persist().
713  *
714  * All the relevant fields in `ainfo' will be filled by this function based
715  * on what we find in `nvl'.
716  *
717  * If this function returns an error, the calling function will take
718  * care of freeing the fields in `ainfo'.
719  */
720 static ipadm_status_t
721 i_ipadm_nvl2ainfo_persist(nvlist_t *nvl, ipadm_addr_info_t *ainfo)
722 {
723 	nvlist_t		*nvladdr;
724 	struct ifaddrs		*ifa;
725 	char			*name;
726 	char			*ifname = NULL;
727 	char			*aobjname = NULL;
728 	char			*propstr = NULL;
729 	nvpair_t		*nvp;
730 	sa_family_t		af;
731 	ipadm_addr_type_t	atype;
732 	boolean_t		is_addr = B_FALSE;
733 	size_t			size = sizeof (struct sockaddr_storage);
734 	uint32_t		plen = 0;
735 	int			err;
736 	ipadm_status_t		status;
737 
738 	status = i_ipadm_nvl2ainfo_common(nvl, ainfo);
739 	if (status != IPADM_SUCCESS)
740 		return (status);
741 
742 	for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
743 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
744 		name = nvpair_name(nvp);
745 		if (strcmp(name, IPADM_NVP_IFNAME) == 0) {
746 			err = nvpair_value_string(nvp, &ifname);
747 		} else if (strcmp(name, IPADM_NVP_AOBJNAME) == 0) {
748 			err = nvpair_value_string(nvp, &aobjname);
749 		} else if (i_ipadm_name2atype(name, &af, &atype)) {
750 			err = nvpair_value_nvlist(nvp, &nvladdr);
751 			is_addr = B_TRUE;
752 		} else {
753 			err = nvpair_value_string(nvp, &propstr);
754 		}
755 		if (err != 0)
756 			return (ipadm_errno2status(err));
757 	}
758 
759 	ifa = &ainfo->ia_ifa;
760 	(void) strlcpy(ainfo->ia_aobjname, aobjname,
761 	    sizeof (ainfo->ia_aobjname));
762 	if (ifa->ifa_name == NULL && (ifa->ifa_name = strdup(ifname)) == NULL)
763 		return (IPADM_NO_MEMORY);
764 	if (is_addr) {
765 		struct sockaddr_in6 data;
766 
767 		/*
768 		 * We got an address from the nvlist `nvl'.
769 		 * Parse `nvladdr' and populate `ifa->ifa_addr'.
770 		 */
771 		ainfo->ia_atype = atype;
772 		if ((ifa->ifa_addr = calloc(1, size)) == NULL)
773 			return (IPADM_NO_MEMORY);
774 		switch (atype) {
775 		case IPADM_ADDR_STATIC:
776 			ifa->ifa_addr->sa_family = af;
777 			break;
778 		case IPADM_ADDR_DHCP:
779 			ifa->ifa_addr->sa_family = AF_INET;
780 			break;
781 		case IPADM_ADDR_IPV6_ADDRCONF:
782 			data.sin6_family = AF_INET6;
783 			if (i_ipadm_nvl2in6_addr(nvladdr, IPADM_NVP_IPNUMADDR,
784 			    &data.sin6_addr) != IPADM_SUCCESS)
785 				return (IPADM_NO_MEMORY);
786 			err = nvlist_lookup_uint32(nvladdr, IPADM_NVP_PREFIXLEN,
787 			    &plen);
788 			if (err != 0)
789 				return (ipadm_errno2status(err));
790 			if ((ifa->ifa_netmask = malloc(size)) == NULL)
791 				return (IPADM_NO_MEMORY);
792 			if ((err = plen2mask(plen, af, ifa->ifa_netmask)) != 0)
793 				return (ipadm_errno2status(err));
794 			(void) memcpy(ifa->ifa_addr, &data, sizeof (data));
795 			break;
796 		default:
797 			return (IPADM_FAILURE);
798 		}
799 	} else {
800 		if (strcmp(name, "prefixlen") == 0) {
801 			/*
802 			 * If a prefixlen was found, update the
803 			 * `ainfo->ia_ifa.ifa_netmask'.
804 			 */
805 
806 			if ((ifa->ifa_netmask = malloc(size)) == NULL)
807 				return (IPADM_NO_MEMORY);
808 			/*
809 			 * Address property lines always follow the address
810 			 * line itself in the persistent db. We must have
811 			 * found a valid `ainfo->ia_ifa.ifa_addr' by now.
812 			 */
813 			assert(ifa->ifa_addr != NULL);
814 			err = plen2mask(atoi(propstr), ifa->ifa_addr->sa_family,
815 			    ifa->ifa_netmask);
816 			if (err != 0)
817 				return (ipadm_errno2status(err));
818 		}
819 	}
820 
821 	return (IPADM_SUCCESS);
822 }
823 
824 /*
825  * Retrieves all addresses from active config and appends to it the
826  * addresses that are found only in persistent config. In addition,
827  * it updates the persistent fields for each address from information
828  * found in persistent config. The output parameter `addrinfo' contains
829  * complete information regarding all addresses in active as well as
830  * persistent config.
831  */
832 static ipadm_status_t
833 i_ipadm_get_all_addr_info(ipadm_handle_t iph, const char *ifname,
834     ipadm_addr_info_t **addrinfo, uint32_t ipadm_flags, int64_t lifc_flags)
835 {
836 	nvlist_t		*nvladdr = NULL;
837 	nvlist_t		*onvl = NULL;
838 	nvpair_t		*nvp;
839 	ipadm_status_t		status;
840 	ipadm_addr_info_t	*ainfo = NULL;
841 	ipadm_addr_info_t	*curr;
842 	ipadm_addr_info_t	*last = NULL;
843 	char			*aobjname;
844 
845 	/* Get all addresses from active config. */
846 	status = i_ipadm_active_addr_info(iph, ifname, &ainfo, ipadm_flags,
847 	    lifc_flags);
848 	if (status != IPADM_SUCCESS)
849 		goto fail;
850 
851 	/* Get all addresses from persistent config. */
852 	status = i_ipadm_get_db_addr(iph, ifname, NULL, &onvl);
853 	/*
854 	 * If no address was found in persistent config, just
855 	 * return what we found in active config.
856 	 */
857 	if (status == IPADM_NOTFOUND) {
858 		/*
859 		 * If nothing was found neither active nor persistent
860 		 * config, this means that the interface does not exist,
861 		 * if one was provided in `ifname'.
862 		 */
863 		if (ainfo == NULL && ifname != NULL)
864 			return (IPADM_ENXIO);
865 		*addrinfo = ainfo;
866 		return (IPADM_SUCCESS);
867 	}
868 	/* In case of any other error, cleanup and return. */
869 	if (status != IPADM_SUCCESS)
870 		goto fail;
871 	/* we append to make sure, loopback addresses are first */
872 	if (ainfo != NULL) {
873 		for (curr = ainfo; IA_NEXT(curr) != NULL; curr = IA_NEXT(curr))
874 			;
875 		last = curr;
876 	}
877 
878 	/*
879 	 * `onvl' will contain all the address lines from the db. Each line
880 	 * could contain the address itself or an address property. Addresses
881 	 * and address properties are found in separate lines.
882 	 *
883 	 * If an address A was found in active, we will already have `ainfo',
884 	 * and it is present in persistent configuration as well, we need to
885 	 * update `ainfo' with persistent information (`ia_pflags).
886 	 * For each address B found only in persistent configuration,
887 	 * append the address to the list with the address info for B from
888 	 * `onvl'.
889 	 */
890 	for (nvp = nvlist_next_nvpair(onvl, NULL); nvp != NULL;
891 	    nvp = nvlist_next_nvpair(onvl, nvp)) {
892 		if (nvpair_value_nvlist(nvp, &nvladdr) != 0)
893 			continue;
894 		if (nvlist_lookup_string(nvladdr, IPADM_NVP_AOBJNAME,
895 		    &aobjname) != 0)
896 			continue;
897 		for (curr = ainfo; curr != NULL; curr = IA_NEXT(curr)) {
898 			if (strcmp(curr->ia_aobjname, aobjname) == 0)
899 				break;
900 		}
901 		if (curr == NULL) {
902 			/*
903 			 * We did not find this address object in `ainfo'.
904 			 * This means that the address object exists only
905 			 * in the persistent configuration. Get its
906 			 * details and append to `ainfo'.
907 			 */
908 			curr = calloc(1, sizeof (ipadm_addr_info_t));
909 			if (curr == NULL)
910 				goto fail;
911 			curr->ia_state = IFA_DISABLED;
912 			if (last != NULL)
913 				last->ia_ifa.ifa_next = &curr->ia_ifa;
914 			else
915 				ainfo = curr;
916 			last = curr;
917 		}
918 		/*
919 		 * Fill relevant fields of `curr' from the persistent info
920 		 * in `nvladdr'. Call the appropriate function based on the
921 		 * `ia_state' value.
922 		 */
923 		if (curr->ia_state == IFA_DISABLED)
924 			status = i_ipadm_nvl2ainfo_persist(nvladdr, curr);
925 		else
926 			status = i_ipadm_nvl2ainfo_active(nvladdr, curr);
927 		if (status != IPADM_SUCCESS)
928 			goto fail;
929 	}
930 	*addrinfo = ainfo;
931 	nvlist_free(onvl);
932 	return (status);
933 fail:
934 	/* On error, cleanup and return. */
935 	nvlist_free(onvl);
936 	ipadm_free_addr_info(ainfo);
937 	*addrinfo = NULL;
938 	return (status);
939 }
940 
941 /*
942  * Callback function that sets the property `prefixlen' on the address
943  * object in `arg' to the value in `pval'.
944  */
945 /* ARGSUSED */
946 static ipadm_status_t
947 i_ipadm_set_prefixlen(ipadm_handle_t iph, const void *arg,
948     ipadm_prop_desc_t *pdp, const void *pval, uint_t af, uint_t flags)
949 {
950 	struct sockaddr_storage	netmask;
951 	struct lifreq		lifr;
952 	int			err, s;
953 	unsigned long		prefixlen, abits;
954 	char			*end;
955 	ipadm_addrobj_t		ipaddr = (ipadm_addrobj_t)arg;
956 
957 	if (ipaddr->ipadm_atype == IPADM_ADDR_DHCP)
958 		return (IPADM_NOTSUP);
959 
960 	errno = 0;
961 	prefixlen = strtoul(pval, &end, 10);
962 	if (errno != 0 || *end != '\0')
963 		return (IPADM_INVALID_ARG);
964 
965 	abits = (af == AF_INET ? IP_ABITS : IPV6_ABITS);
966 	if (prefixlen == 0 || prefixlen == (abits - 1))
967 		return (IPADM_INVALID_ARG);
968 
969 	if ((err = plen2mask(prefixlen, af, (struct sockaddr *)&netmask)) != 0)
970 		return (ipadm_errno2status(err));
971 
972 	s = (af == AF_INET ? iph->iph_sock : iph->iph_sock6);
973 
974 	bzero(&lifr, sizeof (lifr));
975 	i_ipadm_addrobj2lifname(ipaddr, lifr.lifr_name,
976 	    sizeof (lifr.lifr_name));
977 	(void) memcpy(&lifr.lifr_addr, &netmask, sizeof (netmask));
978 	if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
979 		return (ipadm_errno2status(errno));
980 
981 	/* now, change the broadcast address to reflect the prefixlen */
982 	if (af == AF_INET) {
983 		/*
984 		 * get the interface address and set it, this should reset
985 		 * the broadcast address.
986 		 */
987 		(void) ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr);
988 		(void) ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr);
989 	}
990 
991 	return (IPADM_SUCCESS);
992 }
993 
994 
995 /*
996  * Callback function that sets the given value `pval' to one of the
997  * properties among `deprecated', `private', and `transmit' as defined in
998  * `pdp', on the address object in `arg'.
999  */
1000 /* ARGSUSED */
1001 static ipadm_status_t
1002 i_ipadm_set_addr_flag(ipadm_handle_t iph, const void *arg,
1003     ipadm_prop_desc_t *pdp, const void *pval, uint_t af, uint_t flags)
1004 {
1005 	char		lifname[LIFNAMSIZ];
1006 	uint64_t	on_flags = 0, off_flags = 0;
1007 	boolean_t	on;
1008 	ipadm_addrobj_t	ipaddr = (ipadm_addrobj_t)arg;
1009 
1010 	if (ipaddr->ipadm_atype == IPADM_ADDR_DHCP &&
1011 	    strcmp(pdp->ipd_name, "deprecated") == 0)
1012 		return (IPADM_NOTSUP);
1013 
1014 	if (strcmp(pval, IPADM_ONSTR) == 0)
1015 		on = B_TRUE;
1016 	else if (strcmp(pval, IPADM_OFFSTR) == 0)
1017 		on = B_FALSE;
1018 	else
1019 		return (IPADM_INVALID_ARG);
1020 
1021 	if (strcmp(pdp->ipd_name, "private") == 0) {
1022 		if (on)
1023 			on_flags = IFF_PRIVATE;
1024 		else
1025 			off_flags = IFF_PRIVATE;
1026 	} else if (strcmp(pdp->ipd_name, "transmit") == 0) {
1027 		if (on)
1028 			off_flags = IFF_NOXMIT;
1029 		else
1030 			on_flags = IFF_NOXMIT;
1031 	} else if (strcmp(pdp->ipd_name, "deprecated") == 0) {
1032 		if (on)
1033 			on_flags = IFF_DEPRECATED;
1034 		else
1035 			off_flags = IFF_DEPRECATED;
1036 	} else {
1037 		return (IPADM_PROP_UNKNOWN);
1038 	}
1039 
1040 	i_ipadm_addrobj2lifname(ipaddr, lifname, sizeof (lifname));
1041 	return (i_ipadm_set_flags(iph, lifname, af, on_flags, off_flags));
1042 }
1043 
1044 /*
1045  * Callback function that sets the property `zone' on the address
1046  * object in `arg' to the value in `pval'.
1047  */
1048 /* ARGSUSED */
1049 static ipadm_status_t
1050 i_ipadm_set_zone(ipadm_handle_t iph, const void *arg,
1051     ipadm_prop_desc_t *pdp, const void *pval, uint_t af, uint_t flags)
1052 {
1053 	struct lifreq	lifr;
1054 	zoneid_t	zoneid;
1055 	int		s;
1056 
1057 	/*
1058 	 * To modify the zone assignment such that it persists across
1059 	 * reboots, zonecfg(8) must be used.
1060 	 */
1061 	if (flags & IPADM_OPT_PERSIST) {
1062 		return (IPADM_NOTSUP);
1063 	} else if (flags & IPADM_OPT_ACTIVE) {
1064 		/* put logical interface into all zones */
1065 		if (strcmp(pval, "all-zones") == 0) {
1066 			zoneid = ALL_ZONES;
1067 		} else {
1068 			/* zone must be ready or running */
1069 			if ((zoneid = getzoneidbyname(pval)) == -1)
1070 				return (ipadm_errno2status(errno));
1071 		}
1072 	} else {
1073 		return (IPADM_INVALID_ARG);
1074 	}
1075 
1076 	s = (af == AF_INET ? iph->iph_sock : iph->iph_sock6);
1077 	bzero(&lifr, sizeof (lifr));
1078 	i_ipadm_addrobj2lifname((ipadm_addrobj_t)arg, lifr.lifr_name,
1079 	    sizeof (lifr.lifr_name));
1080 	lifr.lifr_zoneid = zoneid;
1081 	if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0)
1082 		return (ipadm_errno2status(errno));
1083 
1084 	return (IPADM_SUCCESS);
1085 }
1086 
1087 /*
1088  * Callback function that sets the property `reqhost' on the address
1089  * object in `arg' to the value in `pval'.
1090  */
1091 /* ARGSUSED */
1092 static ipadm_status_t
1093 i_ipadm_set_reqhost(ipadm_handle_t iph, const void *arg,
1094     ipadm_prop_desc_t *pdp, const void *pval, uint_t af, uint_t flags)
1095 {
1096 	ipadm_status_t		status;
1097 	ipadm_addrobj_t		ipaddr = (ipadm_addrobj_t)arg;
1098 
1099 	if (ipaddr->ipadm_atype != IPADM_ADDR_DHCP)
1100 		return (IPADM_NOTSUP);
1101 
1102 	/*
1103 	 * If requested to set reqhost just from active config but the
1104 	 * address is not in active config, return error.
1105 	 */
1106 	if (!(ipaddr->ipadm_flags & IPMGMT_ACTIVE) &&
1107 	    (flags & IPADM_OPT_ACTIVE) && !(flags & IPADM_OPT_PERSIST)) {
1108 		return (IPADM_NOTFOUND);
1109 	}
1110 
1111 	status = ipadm_set_reqhost(ipaddr, pval);
1112 	if (status != IPADM_SUCCESS)
1113 		return (status);
1114 
1115 	if (ipaddr->ipadm_flags & IPMGMT_ACTIVE) {
1116 		status = i_ipadm_refresh_dhcp(ipaddr);
1117 
1118 		/*
1119 		 * We do not report a problem for IPADM_DHCP_IPC_TIMEOUT since
1120 		 * it is only a soft error to indicate the caller that the
1121 		 * lease might be renewed after the function returns.
1122 		 */
1123 		if (status != IPADM_SUCCESS && status != IPADM_DHCP_IPC_TIMEOUT)
1124 			return (status);
1125 	}
1126 
1127 	status = i_ipadm_set_aobj_addrprop(iph, ipaddr, flags,
1128 	    IPADM_NVP_REQHOST);
1129 	return (status);
1130 }
1131 
1132 /*
1133  * Used by address object property callback functions that need to do a
1134  * two-stage update because the addrprop is cached on the address object.
1135  */
1136 static ipadm_status_t
1137 i_ipadm_set_aobj_addrprop(ipadm_handle_t iph, ipadm_addrobj_t ipaddr,
1138     uint_t flags, const char *propname)
1139 {
1140 	ipadm_status_t	status;
1141 	uint32_t	two_stage_flags;
1142 
1143 	/*
1144 	 * Send the updated address object information to ipmgmtd, since the
1145 	 * cached version of an addrprop resides on an aobjmap, but do
1146 	 * not change the ACTIVE/PERSIST state of the aobjmap. Instead, request
1147 	 * a two-stage, SET_PROPS update with ACTIVE/PERSIST as the first stage
1148 	 * per the existing aobjmap flags and a second stage encoded in
1149 	 * IPADM_OPT_PERSIST_PROPS.
1150 	 */
1151 	two_stage_flags = (flags | IPADM_OPT_SET_PROPS)
1152 	    & ~(IPADM_OPT_ACTIVE | IPADM_OPT_PERSIST);
1153 	if (ipaddr->ipadm_flags & IPMGMT_ACTIVE)
1154 		two_stage_flags |= IPADM_OPT_ACTIVE;
1155 	if (ipaddr->ipadm_flags & IPMGMT_PERSIST)
1156 		two_stage_flags |= IPADM_OPT_PERSIST;
1157 	if (flags & IPADM_OPT_PERSIST)
1158 		two_stage_flags |= IPADM_OPT_PERSIST_PROPS;
1159 
1160 	status = i_ipadm_addr_persist(iph, ipaddr, B_FALSE, two_stage_flags,
1161 	    propname);
1162 	return (status);
1163 }
1164 
1165 /*
1166  * Callback function that gets the property `broadcast' for the address
1167  * object in `arg'.
1168  */
1169 /* ARGSUSED */
1170 static ipadm_status_t
1171 i_ipadm_get_broadcast(ipadm_handle_t iph, const void *arg,
1172     ipadm_prop_desc_t *pdp, char *buf, uint_t *bufsize, uint_t af,
1173     uint_t valtype)
1174 {
1175 	struct sockaddr_in	*sin;
1176 	struct lifreq		lifr;
1177 	char			lifname[LIFNAMSIZ];
1178 	ipadm_addrobj_t		ipaddr = (ipadm_addrobj_t)arg;
1179 	ipadm_status_t		status;
1180 	size_t			nbytes = 0;
1181 	uint64_t		ifflags = 0;
1182 
1183 	i_ipadm_addrobj2lifname(ipaddr, lifname, sizeof (lifname));
1184 	if (ipaddr->ipadm_flags & IPMGMT_ACTIVE) {
1185 		status = i_ipadm_get_flags(iph, lifname, af, &ifflags);
1186 		if (status != IPADM_SUCCESS)
1187 			return (status);
1188 		if (!(ifflags & IFF_BROADCAST)) {
1189 			buf[0] = '\0';
1190 			return (IPADM_SUCCESS);
1191 		}
1192 	}
1193 
1194 	switch (valtype) {
1195 	case MOD_PROP_DEFAULT: {
1196 		struct sockaddr_storage	mask;
1197 		struct in_addr		broadaddr;
1198 		uint_t			plen;
1199 		in_addr_t		addr, maddr;
1200 		char			val[MAXPROPVALLEN];
1201 		uint_t			valsz = MAXPROPVALLEN;
1202 		ipadm_status_t		status;
1203 		int			err;
1204 		struct sockaddr_in	*sin;
1205 
1206 		if (!(ipaddr->ipadm_flags & IPMGMT_ACTIVE)) {
1207 			/*
1208 			 * Since the address is unknown we cannot
1209 			 * obtain default prefixlen
1210 			 */
1211 			if (ipaddr->ipadm_atype == IPADM_ADDR_DHCP ||
1212 			    ipaddr->ipadm_af == AF_INET6) {
1213 				buf[0] = '\0';
1214 				return (IPADM_SUCCESS);
1215 			}
1216 			/*
1217 			 * For the static address, we get the address from the
1218 			 * persistent db.
1219 			 */
1220 			status = i_ipadm_get_static_addr_db(iph, ipaddr);
1221 			if (status != IPADM_SUCCESS)
1222 				return (status);
1223 			sin = SIN(&ipaddr->ipadm_static_addr);
1224 			addr = sin->sin_addr.s_addr;
1225 		} else {
1226 			/*
1227 			 * If the address object is active, we retrieve the
1228 			 * address from kernel.
1229 			 */
1230 			bzero(&lifr, sizeof (lifr));
1231 			(void) strlcpy(lifr.lifr_name, lifname,
1232 			    sizeof (lifr.lifr_name));
1233 			if (ioctl(iph->iph_sock, SIOCGLIFADDR,
1234 			    (caddr_t)&lifr) < 0)
1235 				return (ipadm_errno2status(errno));
1236 
1237 			addr = (SIN(&lifr.lifr_addr))->sin_addr.s_addr;
1238 		}
1239 		/*
1240 		 * For default broadcast address, get the address and the
1241 		 * default prefixlen for that address and then compute the
1242 		 * broadcast address.
1243 		 */
1244 		status = i_ipadm_get_prefixlen(iph, arg, NULL, val, &valsz, af,
1245 		    MOD_PROP_DEFAULT);
1246 		if (status != IPADM_SUCCESS)
1247 			return (status);
1248 
1249 		plen = atoi(val);
1250 		if ((err = plen2mask(plen, AF_INET,
1251 		    (struct sockaddr *)&mask)) != 0)
1252 			return (ipadm_errno2status(err));
1253 		maddr = (SIN(&mask))->sin_addr.s_addr;
1254 		broadaddr.s_addr = (addr & maddr) | ~maddr;
1255 		nbytes = snprintf(buf, *bufsize, "%s", inet_ntoa(broadaddr));
1256 		break;
1257 	}
1258 	case MOD_PROP_ACTIVE:
1259 		bzero(&lifr, sizeof (lifr));
1260 		(void) strlcpy(lifr.lifr_name, lifname,
1261 		    sizeof (lifr.lifr_name));
1262 		if (ioctl(iph->iph_sock, SIOCGLIFBRDADDR,
1263 		    (caddr_t)&lifr) < 0) {
1264 			return (ipadm_errno2status(errno));
1265 		} else {
1266 			sin = SIN(&lifr.lifr_addr);
1267 			nbytes = snprintf(buf, *bufsize, "%s",
1268 			    inet_ntoa(sin->sin_addr));
1269 		}
1270 		break;
1271 	default:
1272 		return (IPADM_INVALID_ARG);
1273 	}
1274 	if (nbytes >= *bufsize) {
1275 		/* insufficient buffer space */
1276 		*bufsize = nbytes + 1;
1277 		return (IPADM_NO_BUFS);
1278 	}
1279 	return (IPADM_SUCCESS);
1280 }
1281 
1282 /*
1283  * Callback function that retrieves the value of the property `prefixlen'
1284  * for the address object in `arg'.
1285  */
1286 /* ARGSUSED */
1287 static ipadm_status_t
1288 i_ipadm_get_prefixlen(ipadm_handle_t iph, const void *arg,
1289     ipadm_prop_desc_t *pdp, char *buf, uint_t *bufsize, uint_t af,
1290     uint_t valtype)
1291 {
1292 	struct lifreq	lifr;
1293 	ipadm_addrobj_t	ipaddr = (ipadm_addrobj_t)arg;
1294 	char		lifname[LIFNAMSIZ];
1295 	int		s;
1296 	uint32_t	prefixlen;
1297 	size_t		nbytes;
1298 	ipadm_status_t	status;
1299 	uint64_t	lifflags;
1300 
1301 	i_ipadm_addrobj2lifname(ipaddr, lifname, sizeof (lifname));
1302 	if (ipaddr->ipadm_flags & IPMGMT_ACTIVE) {
1303 		status = i_ipadm_get_flags(iph, lifname, af, &lifflags);
1304 		if (status != IPADM_SUCCESS) {
1305 			return (status);
1306 		} else if (lifflags & IFF_POINTOPOINT) {
1307 			buf[0] = '\0';
1308 			return (status);
1309 		}
1310 	}
1311 
1312 	s = (af == AF_INET ? iph->iph_sock : iph->iph_sock6);
1313 	bzero(&lifr, sizeof (lifr));
1314 	(void) strlcpy(lifr.lifr_name, lifname, sizeof (lifr.lifr_name));
1315 	switch (valtype) {
1316 	case MOD_PROP_POSSIBLE:
1317 		if (af == AF_INET)
1318 			nbytes = snprintf(buf, *bufsize, "1-30,32");
1319 		else
1320 			nbytes = snprintf(buf, *bufsize, "1-126,128");
1321 		break;
1322 	case MOD_PROP_DEFAULT:
1323 		if (ipaddr->ipadm_flags & IPMGMT_ACTIVE) {
1324 			/*
1325 			 * For static addresses, we retrieve the address
1326 			 * from kernel if it is active.
1327 			 */
1328 			if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0)
1329 				return (ipadm_errno2status(errno));
1330 			status = i_ipadm_get_default_prefixlen(
1331 			    &lifr.lifr_addr, &prefixlen);
1332 			if (status != IPADM_SUCCESS)
1333 				return (status);
1334 		} else if ((ipaddr->ipadm_flags & IPMGMT_PERSIST) &&
1335 		    ipaddr->ipadm_atype == IPADM_ADDR_DHCP) {
1336 			/*
1337 			 * Since the address is unknown we cannot
1338 			 * obtain default prefixlen
1339 			 */
1340 			buf[0] = '\0';
1341 			return (IPADM_SUCCESS);
1342 		} else {
1343 			/*
1344 			 * If not in active config, we use the address
1345 			 * from persistent store.
1346 			 */
1347 			status = i_ipadm_get_static_addr_db(iph, ipaddr);
1348 			if (status != IPADM_SUCCESS)
1349 				return (status);
1350 			status = i_ipadm_get_default_prefixlen(
1351 			    &ipaddr->ipadm_static_addr, &prefixlen);
1352 			if (status != IPADM_SUCCESS)
1353 				return (status);
1354 		}
1355 		nbytes = snprintf(buf, *bufsize, "%u", prefixlen);
1356 		break;
1357 	case MOD_PROP_ACTIVE:
1358 		if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0)
1359 			return (ipadm_errno2status(errno));
1360 		prefixlen = lifr.lifr_addrlen;
1361 		nbytes = snprintf(buf, *bufsize, "%u", prefixlen);
1362 		break;
1363 	default:
1364 		return (IPADM_INVALID_ARG);
1365 	}
1366 	if (nbytes >= *bufsize) {
1367 		/* insufficient buffer space */
1368 		*bufsize = nbytes + 1;
1369 		return (IPADM_NO_BUFS);
1370 	}
1371 	return (IPADM_SUCCESS);
1372 }
1373 
1374 /*
1375  * Callback function that retrieves the value of one of the properties
1376  * among `deprecated', `private', and `transmit' for the address object
1377  * in `arg'.
1378  */
1379 /* ARGSUSED */
1380 static ipadm_status_t
1381 i_ipadm_get_addr_flag(ipadm_handle_t iph, const void *arg,
1382     ipadm_prop_desc_t *pdp, char *buf, uint_t *bufsize, uint_t af,
1383     uint_t valtype)
1384 {
1385 	boolean_t	on = B_FALSE;
1386 	char		lifname[LIFNAMSIZ];
1387 	ipadm_status_t	status = IPADM_SUCCESS;
1388 	uint64_t	ifflags;
1389 	size_t		nbytes;
1390 	ipadm_addrobj_t	ipaddr = (ipadm_addrobj_t)arg;
1391 
1392 	switch (valtype) {
1393 	case MOD_PROP_DEFAULT:
1394 		if (strcmp(pdp->ipd_name, "private") == 0 ||
1395 		    strcmp(pdp->ipd_name, "deprecated") == 0) {
1396 			on = B_FALSE;
1397 		} else if (strcmp(pdp->ipd_name, "transmit") == 0) {
1398 			on = B_TRUE;
1399 		} else {
1400 			return (IPADM_PROP_UNKNOWN);
1401 		}
1402 		break;
1403 	case MOD_PROP_ACTIVE:
1404 		/*
1405 		 * If the address is present in active configuration, we
1406 		 * retrieve it from kernel to get the property value.
1407 		 * Else, there is no value to return.
1408 		 */
1409 		i_ipadm_addrobj2lifname(ipaddr, lifname, sizeof (lifname));
1410 		status = i_ipadm_get_flags(iph, lifname, af, &ifflags);
1411 		if (status != IPADM_SUCCESS)
1412 			return (status);
1413 		if (strcmp(pdp->ipd_name, "private") == 0)
1414 			on = (ifflags & IFF_PRIVATE);
1415 		else if (strcmp(pdp->ipd_name, "transmit") == 0)
1416 			on = !(ifflags & IFF_NOXMIT);
1417 		else if (strcmp(pdp->ipd_name, "deprecated") == 0)
1418 			on = (ifflags & IFF_DEPRECATED);
1419 		break;
1420 	default:
1421 		return (IPADM_INVALID_ARG);
1422 	}
1423 	nbytes = snprintf(buf, *bufsize, "%s",
1424 	    (on ? IPADM_ONSTR : IPADM_OFFSTR));
1425 	if (nbytes >= *bufsize) {
1426 		/* insufficient buffer space */
1427 		*bufsize = nbytes + 1;
1428 		status = IPADM_NO_BUFS;
1429 	}
1430 
1431 	return (status);
1432 }
1433 
1434 /*
1435  * Callback function that retrieves the value of the property `zone'
1436  * for the address object in `arg'.
1437  */
1438 /* ARGSUSED */
1439 static ipadm_status_t
1440 i_ipadm_get_zone(ipadm_handle_t iph, const void *arg,
1441     ipadm_prop_desc_t *pdp, char *buf, uint_t *bufsize, uint_t af,
1442     uint_t valtype)
1443 {
1444 	struct lifreq	lifr;
1445 	char		zone_name[ZONENAME_MAX];
1446 	int		s;
1447 	size_t		nbytes = 0;
1448 
1449 	if (iph->iph_zoneid != GLOBAL_ZONEID) {
1450 		buf[0] = '\0';
1451 		return (IPADM_SUCCESS);
1452 	}
1453 
1454 	/*
1455 	 * we are in global zone. See if the lifname is assigned to shared-ip
1456 	 * zone or global zone.
1457 	 */
1458 	switch (valtype) {
1459 	case MOD_PROP_DEFAULT:
1460 		if (getzonenamebyid(GLOBAL_ZONEID, zone_name,
1461 		    sizeof (zone_name)) > 0)
1462 			nbytes = snprintf(buf, *bufsize, "%s", zone_name);
1463 		else
1464 			return (ipadm_errno2status(errno));
1465 		break;
1466 	case MOD_PROP_ACTIVE:
1467 		bzero(&lifr, sizeof (lifr));
1468 		i_ipadm_addrobj2lifname((ipadm_addrobj_t)arg, lifr.lifr_name,
1469 		    sizeof (lifr.lifr_name));
1470 		s = (af == AF_INET ? iph->iph_sock : iph->iph_sock6);
1471 
1472 		if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifr) == -1)
1473 			return (ipadm_errno2status(errno));
1474 
1475 		if (lifr.lifr_zoneid == ALL_ZONES) {
1476 			nbytes = snprintf(buf, *bufsize, "%s", "all-zones");
1477 		} else if (getzonenamebyid(lifr.lifr_zoneid, zone_name,
1478 		    sizeof (zone_name)) < 0) {
1479 			return (ipadm_errno2status(errno));
1480 		} else {
1481 			nbytes = snprintf(buf, *bufsize, "%s", zone_name);
1482 		}
1483 		break;
1484 	default:
1485 		return (IPADM_INVALID_ARG);
1486 	}
1487 	if (nbytes >= *bufsize) {
1488 		/* insufficient buffer space */
1489 		*bufsize = nbytes + 1;
1490 		return (IPADM_NO_BUFS);
1491 	}
1492 
1493 	return (IPADM_SUCCESS);
1494 }
1495 
1496 /*
1497  * Callback function that retrieves the value of the property `primary'
1498  * for the address object in `arg'.
1499  */
1500 /* ARGSUSED */
1501 static ipadm_status_t
1502 i_ipadm_get_primary(ipadm_handle_t iph, const void *arg,
1503     ipadm_prop_desc_t *pdp, char *buf, uint_t *bufsize, uint_t af,
1504     uint_t valtype)
1505 {
1506 	ipadm_addrobj_t	ipaddr = (ipadm_addrobj_t)arg;
1507 	const char		*onoff = "";
1508 	size_t			nbytes;
1509 
1510 	switch (valtype) {
1511 	case MOD_PROP_DEFAULT:
1512 		if (ipaddr->ipadm_atype == IPADM_ADDR_DHCP)
1513 			onoff = IPADM_OFFSTR;
1514 		break;
1515 	case MOD_PROP_ACTIVE:
1516 		if (ipaddr->ipadm_atype == IPADM_ADDR_DHCP) {
1517 			dhcp_status_t	dhcp_status;
1518 			ipadm_status_t	ipc_status;
1519 			int			error;
1520 
1521 			ipc_status = i_ipadm_dhcp_status(ipaddr, &dhcp_status,
1522 			    &error);
1523 			if (ipc_status != IPADM_SUCCESS &&
1524 			    ipc_status != IPADM_NOTFOUND)
1525 				return (ipc_status);
1526 
1527 			onoff = dhcp_status.if_dflags & DHCP_IF_PRIMARY ?
1528 			    IPADM_ONSTR : IPADM_OFFSTR;
1529 		}
1530 		break;
1531 	default:
1532 		return (IPADM_INVALID_ARG);
1533 	}
1534 
1535 	nbytes = strlcpy(buf, onoff, *bufsize);
1536 	if (nbytes >= *bufsize) {
1537 		/* insufficient buffer space */
1538 		*bufsize = nbytes + 1;
1539 		return (IPADM_NO_BUFS);
1540 	}
1541 
1542 	return (IPADM_SUCCESS);
1543 }
1544 
1545 /*
1546  * Callback function that retrieves the value of the property `reqhost'
1547  * for the address object in `arg'.
1548  */
1549 /* ARGSUSED */
1550 static ipadm_status_t
1551 i_ipadm_get_reqhost(ipadm_handle_t iph, const void *arg,
1552     ipadm_prop_desc_t *pdp, char *buf, uint_t *bufsize, uint_t af,
1553     uint_t valtype)
1554 {
1555 	ipadm_addrobj_t	ipaddr = (ipadm_addrobj_t)arg;
1556 	const char	*reqhost = "";
1557 	size_t		nbytes;
1558 
1559 	switch (valtype) {
1560 	case MOD_PROP_DEFAULT:
1561 		break;
1562 	case MOD_PROP_ACTIVE:
1563 		if (ipaddr->ipadm_atype == IPADM_ADDR_DHCP)
1564 			reqhost = ipaddr->ipadm_reqhost;
1565 		break;
1566 	default:
1567 		return (IPADM_INVALID_ARG);
1568 	}
1569 
1570 	nbytes = strlcpy(buf, reqhost, *bufsize);
1571 	if (nbytes >= *bufsize) {
1572 		/* insufficient buffer space */
1573 		*bufsize = nbytes + 1;
1574 		return (IPADM_NO_BUFS);
1575 	}
1576 
1577 	return (IPADM_SUCCESS);
1578 }
1579 
1580 static ipadm_prop_desc_t *
1581 i_ipadm_get_addrprop_desc(const char *pname)
1582 {
1583 	int i;
1584 
1585 	for (i = 0; ipadm_addrprop_table[i].ipd_name != NULL; i++) {
1586 		if (strcmp(pname, ipadm_addrprop_table[i].ipd_name) == 0 ||
1587 		    (ipadm_addrprop_table[i].ipd_old_name != NULL &&
1588 		    strcmp(pname, ipadm_addrprop_table[i].ipd_old_name) == 0))
1589 			return (&ipadm_addrprop_table[i]);
1590 	}
1591 	return (NULL);
1592 }
1593 
1594 /*
1595  * Gets the value of the given address property `pname' for the address
1596  * object with name `aobjname'.
1597  */
1598 ipadm_status_t
1599 ipadm_get_addrprop(ipadm_handle_t iph, const char *pname, char *buf,
1600     uint_t *bufsize, const char *aobjname, uint_t valtype)
1601 {
1602 	struct ipadm_addrobj_s	ipaddr;
1603 	ipadm_status_t		status = IPADM_SUCCESS;
1604 	sa_family_t		af;
1605 	ipadm_prop_desc_t	*pdp = NULL;
1606 
1607 	if (iph == NULL || pname == NULL || buf == NULL ||
1608 	    bufsize == NULL || *bufsize == 0 || aobjname == NULL) {
1609 		return (IPADM_INVALID_ARG);
1610 	}
1611 
1612 	/* find the property in the property description table */
1613 	if ((pdp = i_ipadm_get_addrprop_desc(pname)) == NULL)
1614 		return (IPADM_PROP_UNKNOWN);
1615 
1616 	/*
1617 	 * For the given aobjname, get the addrobj it represents and
1618 	 * retrieve the property value for that object.
1619 	 */
1620 	i_ipadm_init_addr(&ipaddr, "", aobjname, IPADM_ADDR_NONE);
1621 	if ((status = i_ipadm_get_addrobj(iph, &ipaddr)) != IPADM_SUCCESS)
1622 		return (status);
1623 
1624 	if (ipaddr.ipadm_atype == IPADM_ADDR_IPV6_ADDRCONF)
1625 		return (IPADM_NOTSUP);
1626 	af = ipaddr.ipadm_af;
1627 
1628 	/*
1629 	 * Call the appropriate callback function to based on the field
1630 	 * that was asked for.
1631 	 */
1632 	switch (valtype) {
1633 	case IPADM_OPT_PERM:
1634 		status = i_ipadm_pd2permstr(pdp, buf, bufsize);
1635 		break;
1636 	case IPADM_OPT_ACTIVE:
1637 		if (!(ipaddr.ipadm_flags & IPMGMT_ACTIVE)) {
1638 			buf[0] = '\0';
1639 		} else {
1640 			status = pdp->ipd_get(iph, &ipaddr, pdp, buf, bufsize,
1641 			    af, MOD_PROP_ACTIVE);
1642 		}
1643 		break;
1644 	case IPADM_OPT_DEFAULT:
1645 		status = pdp->ipd_get(iph, &ipaddr, pdp, buf, bufsize,
1646 		    af, MOD_PROP_DEFAULT);
1647 		break;
1648 	case IPADM_OPT_POSSIBLE:
1649 		if (pdp->ipd_get_range != NULL) {
1650 			status = pdp->ipd_get_range(iph, &ipaddr, pdp, buf,
1651 			    bufsize, af, MOD_PROP_POSSIBLE);
1652 			break;
1653 		}
1654 		buf[0] = '\0';
1655 		break;
1656 	case IPADM_OPT_PERSIST:
1657 		status = i_ipadm_get_persist_propval(iph, pdp, buf, bufsize,
1658 		    &ipaddr);
1659 		break;
1660 	default:
1661 		status = IPADM_INVALID_ARG;
1662 		break;
1663 	}
1664 
1665 	return (status);
1666 }
1667 
1668 /*
1669  * Sets the value of the given address property `pname' to `pval' for the
1670  * address object with name `aobjname'.
1671  */
1672 ipadm_status_t
1673 ipadm_set_addrprop(ipadm_handle_t iph, const char *pname,
1674     const char *pval, const char *aobjname, uint_t pflags)
1675 {
1676 	struct ipadm_addrobj_s	ipaddr;
1677 	sa_family_t		af;
1678 	ipadm_prop_desc_t	*pdp = NULL;
1679 	char			defbuf[MAXPROPVALLEN];
1680 	uint_t			defbufsize = MAXPROPVALLEN;
1681 	boolean_t		reset = (pflags & IPADM_OPT_DEFAULT);
1682 	ipadm_status_t		status = IPADM_SUCCESS;
1683 
1684 	/* Check for solaris.network.interface.config authorization */
1685 	if (!ipadm_check_auth())
1686 		return (IPADM_EAUTH);
1687 
1688 	if (iph == NULL || pname == NULL || aobjname == NULL || pflags == 0 ||
1689 	    pflags == IPADM_OPT_PERSIST ||
1690 	    (pflags & ~(IPADM_COMMON_OPT_MASK|IPADM_OPT_DEFAULT)) ||
1691 	    (!reset && pval == NULL)) {
1692 		return (IPADM_INVALID_ARG);
1693 	}
1694 
1695 	/* find the property in the property description table */
1696 	if ((pdp = i_ipadm_get_addrprop_desc(pname)) == NULL)
1697 		return (IPADM_PROP_UNKNOWN);
1698 
1699 	if (pdp->ipd_set == NULL || (reset && pdp->ipd_get == NULL))
1700 		return (IPADM_NOTSUP);
1701 
1702 	if (!(pdp->ipd_flags & IPADMPROP_MULVAL) &&
1703 	    (pflags & (IPADM_OPT_APPEND|IPADM_OPT_REMOVE))) {
1704 		return (IPADM_INVALID_ARG);
1705 	}
1706 
1707 	/*
1708 	 * For the given aobjname, get the addrobj it represents and
1709 	 * set the property value for that object.
1710 	 */
1711 	i_ipadm_init_addr(&ipaddr, "", aobjname, IPADM_ADDR_NONE);
1712 	if ((status = i_ipadm_get_addrobj(iph, &ipaddr)) != IPADM_SUCCESS)
1713 		return (status);
1714 
1715 	if (!(ipaddr.ipadm_flags & IPMGMT_ACTIVE))
1716 		return (IPADM_OP_DISABLE_OBJ);
1717 
1718 	/* Persistent operation not allowed on a temporary object. */
1719 	if ((pflags & IPADM_OPT_PERSIST) &&
1720 	    !(ipaddr.ipadm_flags & IPMGMT_PERSIST))
1721 		return (IPADM_TEMPORARY_OBJ);
1722 	/*
1723 	 * Currently, setting an address property on an address object of type
1724 	 * IPADM_ADDR_IPV6_ADDRCONF is not supported. Supporting it involves
1725 	 * in.ndpd retrieving the address properties from ipmgmtd for given
1726 	 * address object and then setting them on auto-configured addresses,
1727 	 * whenever in.ndpd gets a new prefix. This will be supported in
1728 	 * future releases.
1729 	 */
1730 	if (ipaddr.ipadm_atype == IPADM_ADDR_IPV6_ADDRCONF)
1731 		return (IPADM_NOTSUP);
1732 
1733 	/*
1734 	 * Setting an address property on an address object that is
1735 	 * not present in active configuration is not supported.
1736 	 */
1737 	if (!(ipaddr.ipadm_flags & IPMGMT_ACTIVE))
1738 		return (IPADM_NOTSUP);
1739 
1740 	af = ipaddr.ipadm_af;
1741 	if (reset) {
1742 		/*
1743 		 * If we were asked to reset the value, we need to fetch
1744 		 * the default value and set the default value.
1745 		 */
1746 		status = pdp->ipd_get(iph, &ipaddr, pdp, defbuf, &defbufsize,
1747 		    af, MOD_PROP_DEFAULT);
1748 		if (status != IPADM_SUCCESS)
1749 			return (status);
1750 		pval = defbuf;
1751 	}
1752 	/* set the user provided or default property value */
1753 	status = pdp->ipd_set(iph, &ipaddr, pdp, pval, af, pflags);
1754 	if (status != IPADM_SUCCESS)
1755 		return (status);
1756 
1757 	/*
1758 	 * If IPADM_OPT_PERSIST was set in `flags', we need to store
1759 	 * property and its value in persistent DB.
1760 	 */
1761 	if (pflags & IPADM_OPT_PERSIST) {
1762 		status = i_ipadm_persist_propval(iph, pdp, pval, &ipaddr,
1763 		    pflags);
1764 	}
1765 
1766 	return (status);
1767 }
1768 
1769 /*
1770  * Remove the address specified by the address object in `addr'
1771  * from kernel. If the address is on a non-zero logical interface, we do a
1772  * SIOCLIFREMOVEIF, otherwise we set the address to INADDR_ANY for IPv4 or
1773  * :: for IPv6.
1774  */
1775 ipadm_status_t
1776 i_ipadm_delete_addr(ipadm_handle_t iph, ipadm_addrobj_t addr)
1777 {
1778 	struct lifreq	lifr;
1779 	int		sock;
1780 	ipadm_status_t	status;
1781 
1782 	bzero(&lifr, sizeof (lifr));
1783 	i_ipadm_addrobj2lifname(addr, lifr.lifr_name, sizeof (lifr.lifr_name));
1784 	sock = (addr->ipadm_af == AF_INET ? iph->iph_sock : iph->iph_sock6);
1785 	if (addr->ipadm_lifnum == 0) {
1786 		/*
1787 		 * Fake the deletion of the 0'th address by
1788 		 * clearing IFF_UP and setting it to as 0.0.0.0 or ::.
1789 		 */
1790 		status = i_ipadm_set_flags(iph, addr->ipadm_ifname,
1791 		    addr->ipadm_af, 0, IFF_UP);
1792 		if (status != IPADM_SUCCESS)
1793 			return (status);
1794 		bzero(&lifr.lifr_addr, sizeof (lifr.lifr_addr));
1795 		lifr.lifr_addr.ss_family = addr->ipadm_af;
1796 		if (ioctl(sock, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1797 			return (ipadm_errno2status(errno));
1798 		if (ioctl(sock, SIOCSLIFDSTADDR, (caddr_t)&lifr) < 0)
1799 			return (ipadm_errno2status(errno));
1800 	} else if (ioctl(sock, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) {
1801 		return (ipadm_errno2status(errno));
1802 	}
1803 
1804 	return (IPADM_SUCCESS);
1805 }
1806 
1807 /*
1808  * Extracts the IPv6 address from the nvlist in `nvl'.
1809  */
1810 ipadm_status_t
1811 i_ipadm_nvl2in6_addr(nvlist_t *nvl, char *addr_type, in6_addr_t *in6_addr)
1812 {
1813 	uint8_t	*addr6;
1814 	uint_t	n;
1815 
1816 	if (nvlist_lookup_uint8_array(nvl, addr_type, &addr6, &n) != 0)
1817 		return (IPADM_NOTFOUND);
1818 	assert(n == 16);
1819 	bcopy(addr6, in6_addr->s6_addr, n);
1820 	return (IPADM_SUCCESS);
1821 }
1822 
1823 /*
1824  * Used to validate the given addrobj name string. Length of `aobjname'
1825  * cannot exceed IPADM_AOBJ_USTRSIZ. `aobjname' should start with an
1826  * alphabetic character and it can only contain alphanumeric characters.
1827  */
1828 static boolean_t
1829 i_ipadm_is_user_aobjname_valid(const char *aobjname)
1830 {
1831 	const char	*cp;
1832 
1833 	if (aobjname == NULL || strlen(aobjname) >= IPADM_AOBJ_USTRSIZ ||
1834 	    !isalpha(*aobjname)) {
1835 		return (B_FALSE);
1836 	}
1837 	for (cp = aobjname + 1; *cp && isalnum(*cp); cp++)
1838 		;
1839 	return (*cp == '\0');
1840 }
1841 
1842 /*
1843  * Computes the prefixlen for the given `addr' based on the netmask found using
1844  * the order specified in /etc/nsswitch.conf. If not found, then the
1845  * prefixlen is computed using the Classful subnetting semantics defined
1846  * in RFC 791 for IPv4 and RFC 4291 for IPv6.
1847  */
1848 static ipadm_status_t
1849 i_ipadm_get_default_prefixlen(struct sockaddr_storage *addr, uint32_t *plen)
1850 {
1851 	sa_family_t af = addr->ss_family;
1852 	struct sockaddr_storage mask;
1853 	struct sockaddr_in *m = (struct sockaddr_in *)&mask;
1854 	struct sockaddr_in6 *sin6;
1855 	struct sockaddr_in *sin;
1856 	struct in_addr ia;
1857 	uint32_t prefixlen = 0;
1858 
1859 	switch (af) {
1860 	case AF_INET:
1861 		sin = SIN(addr);
1862 		ia.s_addr = ntohl(sin->sin_addr.s_addr);
1863 		get_netmask4(&ia, &m->sin_addr);
1864 		m->sin_addr.s_addr = htonl(m->sin_addr.s_addr);
1865 		m->sin_family = AF_INET;
1866 		prefixlen = mask2plen((struct sockaddr *)&mask);
1867 		break;
1868 	case AF_INET6:
1869 		sin6 = SIN6(addr);
1870 		if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
1871 			prefixlen = 10;
1872 		else
1873 			prefixlen = 64;
1874 		break;
1875 	default:
1876 		return (IPADM_INVALID_ARG);
1877 	}
1878 	*plen = prefixlen;
1879 	return (IPADM_SUCCESS);
1880 }
1881 
1882 ipadm_status_t
1883 i_ipadm_resolve_addr(const char *name, sa_family_t af,
1884     struct sockaddr_storage *ss)
1885 {
1886 	struct addrinfo hints, *ai;
1887 	int rc;
1888 	struct sockaddr_in6 *sin6;
1889 	struct sockaddr_in *sin;
1890 	boolean_t is_mapped;
1891 
1892 	(void) memset(&hints, 0, sizeof (hints));
1893 	hints.ai_family = af;
1894 	hints.ai_flags = (AI_ALL | AI_V4MAPPED);
1895 	rc = getaddrinfo(name, NULL, &hints, &ai);
1896 	if (rc != 0) {
1897 		if (rc == EAI_NONAME)
1898 			return (IPADM_BAD_ADDR);
1899 		else
1900 			return (IPADM_FAILURE);
1901 	}
1902 	if (ai->ai_next != NULL) {
1903 		/* maps to more than one hostname */
1904 		freeaddrinfo(ai);
1905 		return (IPADM_BAD_HOSTNAME);
1906 	}
1907 	/* LINTED E_BAD_PTR_CAST_ALIGN */
1908 	is_mapped = IN6_IS_ADDR_V4MAPPED(&(SIN6(ai->ai_addr))->sin6_addr);
1909 	if (is_mapped) {
1910 		sin = SIN(ss);
1911 		sin->sin_family = AF_INET;
1912 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1913 		IN6_V4MAPPED_TO_INADDR(&(SIN6(ai->ai_addr))->sin6_addr,
1914 		    &sin->sin_addr);
1915 	} else {
1916 		sin6 = SIN6(ss);
1917 		sin6->sin6_family = AF_INET6;
1918 		bcopy(ai->ai_addr, sin6, sizeof (*sin6));
1919 	}
1920 	freeaddrinfo(ai);
1921 	return (IPADM_SUCCESS);
1922 }
1923 
1924 /*
1925  * This takes a static address string <addr>[/<mask>] or a hostname
1926  * and maps it to a single numeric IP address, consulting DNS if
1927  * hostname was provided. If a specific address family was requested,
1928  * an error is returned if the given hostname does not map to an address
1929  * of the given family. Note that this function returns failure
1930  * if the name maps to more than one IP address.
1931  */
1932 ipadm_status_t
1933 ipadm_set_addr(ipadm_addrobj_t ipaddr, const char *astr, sa_family_t af)
1934 {
1935 	char		*prefixlenstr;
1936 	uint32_t	prefixlen = 0;
1937 	char		*endp;
1938 	/*
1939 	 * We use (NI_MAXHOST + 5) because the longest possible
1940 	 * astr will have (NI_MAXHOST + '/' + {a maximum of 32 for IPv4
1941 	 * or a maximum of 128 for IPv6 + '\0') chars
1942 	 */
1943 	char		addrstr[NI_MAXHOST + 5];
1944 	ipadm_status_t	status;
1945 
1946 	(void) snprintf(addrstr, sizeof (addrstr), "%s", astr);
1947 	if ((prefixlenstr = strchr(addrstr, '/')) != NULL) {
1948 		*prefixlenstr++ = '\0';
1949 		errno = 0;
1950 		prefixlen = strtoul(prefixlenstr, &endp, 10);
1951 		if (errno != 0 || *endp != '\0')
1952 			return (IPADM_INVALID_ARG);
1953 		if ((af == AF_INET && prefixlen > IP_ABITS) ||
1954 		    (af == AF_INET6 && prefixlen > IPV6_ABITS))
1955 			return (IPADM_INVALID_ARG);
1956 	}
1957 
1958 	status = i_ipadm_resolve_addr(addrstr, af, &ipaddr->ipadm_static_addr);
1959 	if (status == IPADM_SUCCESS) {
1960 		(void) strlcpy(ipaddr->ipadm_static_aname, addrstr,
1961 		    sizeof (ipaddr->ipadm_static_aname));
1962 		ipaddr->ipadm_af = ipaddr->ipadm_static_addr.ss_family;
1963 		ipaddr->ipadm_static_prefixlen = prefixlen;
1964 	}
1965 	return (status);
1966 }
1967 
1968 /*
1969  * Gets the static source address from the address object in `ipaddr'.
1970  * Memory for `addr' should be already allocated by the caller.
1971  */
1972 ipadm_status_t
1973 ipadm_get_addr(const ipadm_addrobj_t ipaddr, struct sockaddr_storage *addr)
1974 {
1975 	if (ipaddr == NULL || ipaddr->ipadm_atype != IPADM_ADDR_STATIC ||
1976 	    addr == NULL) {
1977 		return (IPADM_INVALID_ARG);
1978 	}
1979 	*addr = ipaddr->ipadm_static_addr;
1980 
1981 	return (IPADM_SUCCESS);
1982 }
1983 
1984 /*
1985  * Set up tunnel destination address in ipaddr by contacting DNS.
1986  * The function works similar to ipadm_set_addr().
1987  * The dst_addr must resolve to exactly one address. IPADM_BAD_ADDR is returned
1988  * if dst_addr resolves to more than one address. The caller has to verify
1989  * that ipadm_static_addr and ipadm_static_dst_addr have the same ss_family
1990  */
1991 ipadm_status_t
1992 ipadm_set_dst_addr(ipadm_addrobj_t ipaddr, const char *daddrstr, sa_family_t af)
1993 {
1994 	ipadm_status_t	status;
1995 
1996 	/* mask lengths are not meaningful for point-to-point interfaces. */
1997 	if (strchr(daddrstr, '/') != NULL)
1998 		return (IPADM_BAD_ADDR);
1999 
2000 	status = i_ipadm_resolve_addr(daddrstr, af,
2001 	    &ipaddr->ipadm_static_dst_addr);
2002 	if (status == IPADM_SUCCESS) {
2003 		(void) strlcpy(ipaddr->ipadm_static_dname, daddrstr,
2004 		    sizeof (ipaddr->ipadm_static_dname));
2005 	}
2006 	return (status);
2007 }
2008 
2009 /*
2010  * Sets the interface ID in the address object `ipaddr' with the address
2011  * in the string `interface_id'. This interface ID will be used when
2012  * ipadm_create_addr() is called with `ipaddr' with address type
2013  * set to IPADM_ADDR_IPV6_ADDRCONF.
2014  */
2015 ipadm_status_t
2016 ipadm_set_interface_id(ipadm_addrobj_t ipaddr, const char *interface_id)
2017 {
2018 	struct sockaddr_in6	*sin6;
2019 	char			*end;
2020 	char			*cp;
2021 	uint32_t		prefixlen;
2022 	char			addrstr[INET6_ADDRSTRLEN + 1];
2023 
2024 	if (ipaddr == NULL || interface_id == NULL ||
2025 	    ipaddr->ipadm_atype != IPADM_ADDR_IPV6_ADDRCONF)
2026 		return (IPADM_INVALID_ARG);
2027 
2028 	(void) strlcpy(addrstr, interface_id, sizeof (addrstr));
2029 	if ((cp = strchr(addrstr, '/')) == NULL)
2030 		return (IPADM_INVALID_ARG);
2031 	*cp++ = '\0';
2032 	sin6 = &ipaddr->ipadm_intfid;
2033 	if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) == 1) {
2034 		errno = 0;
2035 		prefixlen = strtoul(cp, &end, 10);
2036 		if (errno != 0 || *end != '\0' || prefixlen > IPV6_ABITS)
2037 			return (IPADM_INVALID_ARG);
2038 		sin6->sin6_family = AF_INET6;
2039 		ipaddr->ipadm_intfidlen = prefixlen;
2040 		return (IPADM_SUCCESS);
2041 	}
2042 	return (IPADM_INVALID_ARG);
2043 }
2044 
2045 /*
2046  * Sets the value for the field `ipadm_stateless' in address object `ipaddr'.
2047  */
2048 ipadm_status_t
2049 ipadm_set_stateless(ipadm_addrobj_t ipaddr, boolean_t stateless)
2050 {
2051 	if (ipaddr == NULL ||
2052 	    ipaddr->ipadm_atype != IPADM_ADDR_IPV6_ADDRCONF)
2053 		return (IPADM_INVALID_ARG);
2054 	ipaddr->ipadm_stateless = stateless;
2055 
2056 	return (IPADM_SUCCESS);
2057 }
2058 
2059 /*
2060  * Sets the value for the field `ipadm_stateful' in address object `ipaddr'.
2061  */
2062 ipadm_status_t
2063 ipadm_set_stateful(ipadm_addrobj_t ipaddr, boolean_t stateful)
2064 {
2065 	if (ipaddr == NULL ||
2066 	    ipaddr->ipadm_atype != IPADM_ADDR_IPV6_ADDRCONF)
2067 		return (IPADM_INVALID_ARG);
2068 	ipaddr->ipadm_stateful = stateful;
2069 
2070 	return (IPADM_SUCCESS);
2071 }
2072 
2073 /*
2074  * Sets the dhcp parameter `ipadm_primary' in the address object `ipaddr'.
2075  * The field is used during the address creation with address
2076  * type IPADM_ADDR_DHCP. It specifies if the interface should be set
2077  * as a primary interface for getting dhcp global options from the DHCP server.
2078  */
2079 ipadm_status_t
2080 ipadm_set_primary(ipadm_addrobj_t ipaddr, boolean_t primary)
2081 {
2082 	if (ipaddr == NULL || ipaddr->ipadm_atype != IPADM_ADDR_DHCP)
2083 		return (IPADM_INVALID_ARG);
2084 	ipaddr->ipadm_primary = primary;
2085 
2086 	return (IPADM_SUCCESS);
2087 }
2088 
2089 /*
2090  * Sets the dhcp parameter `ipadm_wait' in the address object `ipaddr'.
2091  * This field is used during the address creation with address type
2092  * IPADM_ADDR_DHCP. It specifies how long the API ipadm_create_addr()
2093  * should wait before returning while the dhcp address is being acquired
2094  * by the dhcpagent.
2095  * Possible values:
2096  * - IPADM_DHCP_WAIT_FOREVER : Do not return until dhcpagent returns.
2097  * - IPADM_DHCP_WAIT_DEFAULT : Wait a default amount of time before returning.
2098  * - <integer>	   : Wait the specified number of seconds before returning.
2099  */
2100 ipadm_status_t
2101 ipadm_set_wait_time(ipadm_addrobj_t ipaddr, int32_t wait)
2102 {
2103 	if (ipaddr == NULL || ipaddr->ipadm_atype != IPADM_ADDR_DHCP)
2104 		return (IPADM_INVALID_ARG);
2105 	ipaddr->ipadm_wait = wait;
2106 	return (IPADM_SUCCESS);
2107 }
2108 
2109 /*
2110  * Sets the dhcp parameter `ipadm_reqhost' in the address object `ipaddr',
2111  * but validate any non-nil value using ipadm_is_valid_hostname() and also
2112  * check length.
2113  */
2114 ipadm_status_t
2115 ipadm_set_reqhost(ipadm_addrobj_t ipaddr, const char *reqhost)
2116 {
2117 	const size_t HNLEN = sizeof (ipaddr->ipadm_reqhost);
2118 
2119 	if (ipaddr == NULL || ipaddr->ipadm_atype != IPADM_ADDR_DHCP)
2120 		return (IPADM_INVALID_ARG);
2121 
2122 	if (ipadm_is_nil_hostname(reqhost))
2123 		*ipaddr->ipadm_reqhost = '\0';
2124 	else if (!ipadm_is_valid_hostname(reqhost))
2125 		return (IPADM_INVALID_ARG);
2126 	else if (strlcpy(ipaddr->ipadm_reqhost, reqhost, HNLEN) >= HNLEN)
2127 		return (IPADM_INVALID_ARG);
2128 	return (IPADM_SUCCESS);
2129 }
2130 
2131 /*
2132  * Creates a placeholder for the `ipadm_aobjname' in the ipmgmtd `aobjmap'.
2133  * If the `aobjname' already exists in the daemon's `aobjmap' then
2134  * IPADM_ADDROBJ_EXISTS will be returned.
2135  *
2136  * If the libipadm consumer set `ipaddr.ipadm_aobjname[0]' to `\0', then the
2137  * daemon will generate an `aobjname' for the given `ipaddr'.
2138  */
2139 ipadm_status_t
2140 i_ipadm_lookupadd_addrobj(ipadm_handle_t iph, ipadm_addrobj_t ipaddr)
2141 {
2142 	ipmgmt_aobjop_arg_t	larg;
2143 	ipmgmt_aobjop_rval_t	rval, *rvalp;
2144 	int			err;
2145 
2146 	bzero(&larg, sizeof (larg));
2147 	larg.ia_cmd = IPMGMT_CMD_ADDROBJ_LOOKUPADD;
2148 	(void) strlcpy(larg.ia_aobjname, ipaddr->ipadm_aobjname,
2149 	    sizeof (larg.ia_aobjname));
2150 	(void) strlcpy(larg.ia_ifname, ipaddr->ipadm_ifname,
2151 	    sizeof (larg.ia_ifname));
2152 	larg.ia_family = ipaddr->ipadm_af;
2153 	larg.ia_atype = ipaddr->ipadm_atype;
2154 
2155 	rvalp = &rval;
2156 	err = ipadm_door_call(iph, &larg, sizeof (larg), (void **)&rvalp,
2157 	    sizeof (rval), B_FALSE);
2158 	if (err == 0 && ipaddr->ipadm_aobjname[0] == '\0') {
2159 		/* copy the daemon generated `aobjname' into `ipadddr' */
2160 		(void) strlcpy(ipaddr->ipadm_aobjname, rval.ir_aobjname,
2161 		    sizeof (ipaddr->ipadm_aobjname));
2162 	}
2163 	if (err == EEXIST)
2164 		return (IPADM_ADDROBJ_EXISTS);
2165 	return (ipadm_errno2status(err));
2166 }
2167 
2168 /*
2169  * Sets the logical interface number in the ipmgmtd's memory map for the
2170  * address object `ipaddr'. If another address object has the same
2171  * logical interface number, IPADM_ADDROBJ_EXISTS is returned.
2172  */
2173 ipadm_status_t
2174 i_ipadm_setlifnum_addrobj(ipadm_handle_t iph, ipadm_addrobj_t ipaddr)
2175 {
2176 	ipmgmt_aobjop_arg_t	larg;
2177 	ipmgmt_retval_t		rval, *rvalp;
2178 	int			err;
2179 
2180 	if (iph->iph_flags & IPH_IPMGMTD)
2181 		return (IPADM_SUCCESS);
2182 
2183 	bzero(&larg, sizeof (larg));
2184 	larg.ia_cmd = IPMGMT_CMD_ADDROBJ_SETLIFNUM;
2185 	(void) strlcpy(larg.ia_aobjname, ipaddr->ipadm_aobjname,
2186 	    sizeof (larg.ia_aobjname));
2187 	larg.ia_lnum = ipaddr->ipadm_lifnum;
2188 	(void) strlcpy(larg.ia_ifname, ipaddr->ipadm_ifname,
2189 	    sizeof (larg.ia_ifname));
2190 	larg.ia_family = ipaddr->ipadm_af;
2191 
2192 	rvalp = &rval;
2193 	err = ipadm_door_call(iph, &larg, sizeof (larg), (void **)&rvalp,
2194 	    sizeof (rval), B_FALSE);
2195 	if (err == EEXIST)
2196 		return (IPADM_ADDROBJ_EXISTS);
2197 	return (ipadm_errno2status(err));
2198 }
2199 
2200 /*
2201  * Creates the IPv4 or IPv6 address in the nvlist `nvl' on the interface
2202  * `ifname'. If a hostname is present, it is resolved before the address
2203  * is created.
2204  */
2205 ipadm_status_t
2206 i_ipadm_enable_static(ipadm_handle_t iph, const char *ifname, nvlist_t *nvl,
2207     sa_family_t af)
2208 {
2209 	char			*prefixlenstr = NULL;
2210 	char			*upstr = NULL;
2211 	char			*sname = NULL, *dname = NULL;
2212 	struct ipadm_addrobj_s	ipaddr;
2213 	char			*aobjname = NULL;
2214 	nvlist_t		*nvaddr = NULL;
2215 	nvpair_t		*nvp;
2216 	char			*cidraddr;
2217 	char			*name;
2218 	ipadm_status_t		status;
2219 	int			err = 0;
2220 	uint32_t		flags = IPADM_OPT_ACTIVE;
2221 
2222 	/* retrieve the address information */
2223 	for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
2224 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
2225 		name = nvpair_name(nvp);
2226 		if (strcmp(name, IPADM_NVP_IPV4ADDR) == 0 ||
2227 		    strcmp(name, IPADM_NVP_IPV6ADDR) == 0) {
2228 			err = nvpair_value_nvlist(nvp, &nvaddr);
2229 		} else if (strcmp(name, IPADM_NVP_AOBJNAME) == 0) {
2230 			err = nvpair_value_string(nvp, &aobjname);
2231 		} else if (strcmp(name, IPADM_NVP_PREFIXLEN) == 0) {
2232 			err = nvpair_value_string(nvp, &prefixlenstr);
2233 		} else if (strcmp(name, "up") == 0) {
2234 			err = nvpair_value_string(nvp, &upstr);
2235 		}
2236 		if (err != 0)
2237 			return (ipadm_errno2status(err));
2238 	}
2239 	for (nvp = nvlist_next_nvpair(nvaddr, NULL); nvp != NULL;
2240 	    nvp = nvlist_next_nvpair(nvaddr, nvp)) {
2241 		name = nvpair_name(nvp);
2242 		if (strcmp(name, IPADM_NVP_IPADDRHNAME) == 0)
2243 			err = nvpair_value_string(nvp, &sname);
2244 		else if (strcmp(name, IPADM_NVP_IPDADDRHNAME) == 0)
2245 			err = nvpair_value_string(nvp, &dname);
2246 		if (err != 0)
2247 			return (ipadm_errno2status(err));
2248 	}
2249 
2250 	if (strcmp(upstr, "yes") == 0)
2251 		flags |= IPADM_OPT_UP;
2252 
2253 	/* build the address object from the above information */
2254 	i_ipadm_init_addr(&ipaddr, ifname, aobjname, IPADM_ADDR_STATIC);
2255 	if (prefixlenstr != NULL && atoi(prefixlenstr) > 0) {
2256 		if (asprintf(&cidraddr, "%s/%s", sname, prefixlenstr) == -1)
2257 			return (IPADM_NO_MEMORY);
2258 		status = ipadm_set_addr(&ipaddr, cidraddr, af);
2259 		free(cidraddr);
2260 	} else {
2261 		status = ipadm_set_addr(&ipaddr, sname, af);
2262 	}
2263 	if (status != IPADM_SUCCESS)
2264 		return (status);
2265 
2266 	if (dname != NULL) {
2267 		status = ipadm_set_dst_addr(&ipaddr, dname, af);
2268 		if (status != IPADM_SUCCESS)
2269 			return (status);
2270 	}
2271 	return (i_ipadm_create_addr(iph, &ipaddr, flags));
2272 }
2273 
2274 /*
2275  * Creates a dhcp address on the interface `ifname' based on the
2276  * IPADM_ADDR_DHCP address object parameters from the nvlist `nvl'.
2277  */
2278 ipadm_status_t
2279 i_ipadm_enable_dhcp(ipadm_handle_t iph, const char *ifname, nvlist_t *nvl)
2280 {
2281 	int32_t			wait = IPADM_DHCP_WAIT_DEFAULT;
2282 	boolean_t		primary = B_FALSE;
2283 	nvlist_t		*nvdhcp = NULL;
2284 	nvpair_t		*nvp;
2285 	char			*name;
2286 	struct ipadm_addrobj_s	ipaddr;
2287 	char			*aobjname = NULL, *reqhost = NULL;
2288 	int			err = 0;
2289 	ipadm_status_t		ipadm_err = IPADM_SUCCESS;
2290 
2291 	/* Extract the dhcp parameters */
2292 	for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
2293 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
2294 		name = nvpair_name(nvp);
2295 		if (strcmp(name, IPADM_NVP_DHCP) == 0)
2296 			err = nvpair_value_nvlist(nvp, &nvdhcp);
2297 		else if (strcmp(name, IPADM_NVP_AOBJNAME) == 0)
2298 			err = nvpair_value_string(nvp, &aobjname);
2299 		else if (strcmp(name, IPADM_NVP_REQHOST) == 0)
2300 			err = nvpair_value_string(nvp, &reqhost);
2301 		if (err != 0)
2302 			return (ipadm_errno2status(err));
2303 	}
2304 	for (nvp = nvlist_next_nvpair(nvdhcp, NULL); nvp != NULL;
2305 	    nvp = nvlist_next_nvpair(nvdhcp, nvp)) {
2306 		name = nvpair_name(nvp);
2307 		if (strcmp(name, IPADM_NVP_WAIT) == 0)
2308 			err = nvpair_value_int32(nvp, &wait);
2309 		else if (strcmp(name, IPADM_NVP_PRIMARY) == 0)
2310 			err = nvpair_value_boolean_value(nvp, &primary);
2311 		if (err != 0)
2312 			return (ipadm_errno2status(err));
2313 	}
2314 
2315 	/* Build the address object */
2316 	i_ipadm_init_addr(&ipaddr, ifname, aobjname, IPADM_ADDR_DHCP);
2317 	ipaddr.ipadm_primary = primary;
2318 	if (iph->iph_flags & IPH_INIT)
2319 		ipaddr.ipadm_wait = 0;
2320 	else
2321 		ipaddr.ipadm_wait = wait;
2322 	ipadm_err = ipadm_set_reqhost(&ipaddr, reqhost);
2323 	if (ipadm_err != IPADM_SUCCESS)
2324 		return (ipadm_err);
2325 	ipaddr.ipadm_af = AF_INET;
2326 	return (i_ipadm_create_dhcp(iph, &ipaddr, IPADM_OPT_ACTIVE));
2327 }
2328 
2329 /*
2330  * Creates auto-configured addresses on the interface `ifname' based on
2331  * the IPADM_ADDR_IPV6_ADDRCONF address object parameters from the nvlist `nvl'.
2332  */
2333 ipadm_status_t
2334 i_ipadm_enable_addrconf(ipadm_handle_t iph, const char *ifname, nvlist_t *nvl)
2335 {
2336 	struct ipadm_addrobj_s	ipaddr;
2337 	char		*stateful = NULL, *stateless = NULL;
2338 	uint_t		n;
2339 	uint8_t		*addr6 = NULL;
2340 	uint32_t	intfidlen = 0;
2341 	char		*aobjname;
2342 	nvlist_t	*nvaddr;
2343 	nvpair_t	*nvp;
2344 	char		*name;
2345 	int		err = 0;
2346 
2347 	/* Extract the parameters */
2348 	for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
2349 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
2350 		name = nvpair_name(nvp);
2351 		if (strcmp(name, IPADM_NVP_INTFID) == 0)
2352 			err = nvpair_value_nvlist(nvp, &nvaddr);
2353 		else if (strcmp(name, IPADM_NVP_AOBJNAME) == 0)
2354 			err = nvpair_value_string(nvp, &aobjname);
2355 		if (err != 0)
2356 			return (ipadm_errno2status(err));
2357 	}
2358 	for (nvp = nvlist_next_nvpair(nvaddr, NULL); nvp != NULL;
2359 	    nvp = nvlist_next_nvpair(nvaddr, nvp)) {
2360 		name = nvpair_name(nvp);
2361 		if (strcmp(name, IPADM_NVP_IPNUMADDR) == 0)
2362 			err = nvpair_value_uint8_array(nvp, &addr6, &n);
2363 		if (strcmp(name, IPADM_NVP_PREFIXLEN) == 0)
2364 			err = nvpair_value_uint32(nvp, &intfidlen);
2365 		else if (strcmp(name, IPADM_NVP_STATELESS) == 0)
2366 			err = nvpair_value_string(nvp, &stateless);
2367 		else if (strcmp(name, IPADM_NVP_STATEFUL) == 0)
2368 			err = nvpair_value_string(nvp, &stateful);
2369 		if (err != 0)
2370 			return (ipadm_errno2status(err));
2371 	}
2372 	/* Build the address object. */
2373 	i_ipadm_init_addr(&ipaddr, ifname, aobjname, IPADM_ADDR_IPV6_ADDRCONF);
2374 	if (intfidlen > 0) {
2375 		ipaddr.ipadm_intfidlen = intfidlen;
2376 		bcopy(addr6, &ipaddr.ipadm_intfid.sin6_addr.s6_addr, n);
2377 	}
2378 	ipaddr.ipadm_stateless = (strcmp(stateless, "yes") == 0);
2379 	ipaddr.ipadm_stateful = (strcmp(stateful, "yes") == 0);
2380 	return (i_ipadm_create_ipv6addrs(iph, &ipaddr, IPADM_OPT_ACTIVE));
2381 }
2382 
2383 /*
2384  * Allocates `ipadm_addrobj_t' and populates the relevant member fields based on
2385  * the provided `type'. `aobjname' represents the address object name, which
2386  * is of the form `<ifname>/<addressname>'.
2387  *
2388  * The caller has to minimally provide <ifname>. If <addressname> is not
2389  * provided, then a default one will be generated by the API.
2390  */
2391 ipadm_status_t
2392 ipadm_create_addrobj(ipadm_addr_type_t type, const char *aobjname,
2393     ipadm_addrobj_t *ipaddr)
2394 {
2395 	ipadm_addrobj_t	newaddr;
2396 	ipadm_status_t	status;
2397 	char		*aname, *cp;
2398 	char		ifname[IPADM_AOBJSIZ];
2399 	ifspec_t	ifsp;
2400 
2401 	if (ipaddr == NULL)
2402 		return (IPADM_INVALID_ARG);
2403 	*ipaddr = NULL;
2404 
2405 	if (aobjname == NULL || aobjname[0] == '\0')
2406 		return (IPADM_INVALID_ARG);
2407 
2408 	if (strlcpy(ifname, aobjname, IPADM_AOBJSIZ) >= IPADM_AOBJSIZ)
2409 		return (IPADM_INVALID_ARG);
2410 
2411 	if ((aname = strchr(ifname, '/')) != NULL)
2412 		*aname++ = '\0';
2413 
2414 	/* Check if the interface name is valid. */
2415 	if (!ifparse_ifspec(ifname, &ifsp))
2416 		return (IPADM_INVALID_ARG);
2417 	/* Check if the given addrobj name is valid. */
2418 	if (aname != NULL && !i_ipadm_is_user_aobjname_valid(aname))
2419 		return (IPADM_INVALID_ARG);
2420 	if ((newaddr = calloc(1, sizeof (struct ipadm_addrobj_s))) == NULL)
2421 		return (IPADM_NO_MEMORY);
2422 
2423 	/*
2424 	 * If the ifname has logical interface number, extract it and assign
2425 	 * it to `ipadm_lifnum'. Only applications with IPH_LEGACY set will do
2426 	 * this today. We will check for the validity later in
2427 	 * i_ipadm_validate_create_addr().
2428 	 */
2429 	if (ifsp.ifsp_lunvalid) {
2430 		newaddr->ipadm_lifnum = ifsp.ifsp_lun;
2431 		cp = strchr(ifname, IPADM_LOGICAL_SEP);
2432 		*cp = '\0';
2433 	}
2434 	(void) strlcpy(newaddr->ipadm_ifname, ifname,
2435 	    sizeof (newaddr->ipadm_ifname));
2436 
2437 	if (aname != NULL) {
2438 		(void) snprintf(newaddr->ipadm_aobjname,
2439 		    sizeof (newaddr->ipadm_aobjname), "%s/%s", ifname, aname);
2440 	}
2441 
2442 	switch (type) {
2443 	case IPADM_ADDR_IPV6_ADDRCONF:
2444 		newaddr->ipadm_intfidlen = 0;
2445 		newaddr->ipadm_stateful = B_TRUE;
2446 		newaddr->ipadm_stateless = B_TRUE;
2447 		newaddr->ipadm_af = AF_INET6;
2448 		break;
2449 
2450 	case IPADM_ADDR_DHCP:
2451 		newaddr->ipadm_primary = B_FALSE;
2452 		newaddr->ipadm_wait = IPADM_DHCP_WAIT_DEFAULT;
2453 		newaddr->ipadm_af = AF_INET;
2454 		break;
2455 
2456 	case IPADM_ADDR_STATIC:
2457 		newaddr->ipadm_af = AF_UNSPEC;
2458 		newaddr->ipadm_static_prefixlen = 0;
2459 		break;
2460 
2461 	default:
2462 		status = IPADM_INVALID_ARG;
2463 		goto fail;
2464 	}
2465 	newaddr->ipadm_atype = type;
2466 	*ipaddr = newaddr;
2467 	return (IPADM_SUCCESS);
2468 fail:
2469 	free(newaddr);
2470 	return (status);
2471 }
2472 
2473 /*
2474  * Returns `aobjname' from the address object in `ipaddr'.
2475  */
2476 ipadm_status_t
2477 ipadm_get_aobjname(const ipadm_addrobj_t ipaddr, char *aobjname, size_t len)
2478 {
2479 	if (ipaddr == NULL || aobjname == NULL)
2480 		return (IPADM_INVALID_ARG);
2481 	if (strlcpy(aobjname, ipaddr->ipadm_aobjname, len) >= len)
2482 		return (IPADM_INVALID_ARG);
2483 
2484 	return (IPADM_SUCCESS);
2485 }
2486 
2487 /*
2488  * Frees the address object in `ipaddr'.
2489  */
2490 void
2491 ipadm_destroy_addrobj(ipadm_addrobj_t ipaddr)
2492 {
2493 	free(ipaddr);
2494 }
2495 
2496 /*
2497  * Retrieves the logical interface name from `ipaddr' and stores the
2498  * string in `lifname'.
2499  */
2500 void
2501 i_ipadm_addrobj2lifname(ipadm_addrobj_t ipaddr, char *lifname, int lifnamesize)
2502 {
2503 	if (ipaddr->ipadm_lifnum != 0) {
2504 		(void) snprintf(lifname, lifnamesize, "%s:%d",
2505 		    ipaddr->ipadm_ifname, ipaddr->ipadm_lifnum);
2506 	} else {
2507 		(void) snprintf(lifname, lifnamesize, "%s",
2508 		    ipaddr->ipadm_ifname);
2509 	}
2510 }
2511 
2512 /*
2513  * Checks if a non-zero static address is present on the 0th logical interface
2514  * of the given IPv4 or IPv6 physical interface. For an IPv4 interface, it
2515  * also checks if the interface is under DHCP control. If the condition is true,
2516  * the output argument `exists' will be set to B_TRUE. Otherwise, `exists'
2517  * is set to B_FALSE.
2518  *
2519  * Note that *exists will not be initialized if an error is encountered.
2520  */
2521 static ipadm_status_t
2522 i_ipadm_addr_exists_on_if(ipadm_handle_t iph, const char *ifname,
2523     sa_family_t af, boolean_t *exists)
2524 {
2525 	struct lifreq	lifr;
2526 	int		sock;
2527 
2528 	/* For IPH_LEGACY, a new logical interface will never be added. */
2529 	if (iph->iph_flags & IPH_LEGACY) {
2530 		*exists = B_FALSE;
2531 		return (IPADM_SUCCESS);
2532 	}
2533 	bzero(&lifr, sizeof (lifr));
2534 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2535 	if (af == AF_INET) {
2536 		sock = iph->iph_sock;
2537 		if (ioctl(sock, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0)
2538 			return (ipadm_errno2status(errno));
2539 		if (lifr.lifr_flags & IFF_DHCPRUNNING) {
2540 			*exists = B_TRUE;
2541 			return (IPADM_SUCCESS);
2542 		}
2543 	} else {
2544 		sock = iph->iph_sock6;
2545 	}
2546 	if (ioctl(sock, SIOCGLIFADDR, (caddr_t)&lifr) < 0)
2547 		return (ipadm_errno2status(errno));
2548 	*exists = !sockaddrunspec((struct sockaddr *)&lifr.lifr_addr);
2549 
2550 	return (IPADM_SUCCESS);
2551 }
2552 
2553 /*
2554  * Adds a new logical interface in the kernel for interface
2555  * `addr->ipadm_ifname', if there is a non-zero address on the 0th
2556  * logical interface or if the 0th logical interface is under DHCP
2557  * control. On success, it sets the lifnum in the address object `addr'.
2558  */
2559 ipadm_status_t
2560 i_ipadm_do_addif(ipadm_handle_t iph, ipadm_addrobj_t addr, boolean_t *added)
2561 {
2562 	ipadm_status_t	status;
2563 	boolean_t	addif;
2564 	struct lifreq	lifr;
2565 	int		sock;
2566 
2567 	status = i_ipadm_addr_exists_on_if(iph, addr->ipadm_ifname,
2568 	    addr->ipadm_af, &addif);
2569 	if (status != IPADM_SUCCESS)
2570 		return (status);
2571 	if (addif) {
2572 		/*
2573 		 * If there is an address on 0th logical interface,
2574 		 * add a new logical interface.
2575 		 */
2576 		bzero(&lifr, sizeof (lifr));
2577 		(void) strlcpy(lifr.lifr_name, addr->ipadm_ifname,
2578 		    sizeof (lifr.lifr_name));
2579 		sock = (addr->ipadm_af == AF_INET ? iph->iph_sock :
2580 		    iph->iph_sock6);
2581 		if (ioctl(sock, SIOCLIFADDIF, (caddr_t)&lifr) < 0)
2582 			return (ipadm_errno2status(errno));
2583 		addr->ipadm_lifnum = i_ipadm_get_lnum(lifr.lifr_name);
2584 		if (added != NULL)
2585 			*added = B_TRUE;
2586 	} else {
2587 		/*
2588 		 * The first logical interface (0) has a zero address, and is
2589 		 * not under DHCP control, use it.
2590 		 */
2591 		addr->ipadm_lifnum = 0;
2592 	}
2593 	return (IPADM_SUCCESS);
2594 }
2595 
2596 /*
2597  * Reads all the address lines from the persistent DB into the nvlist `onvl',
2598  * when both `ifname' and `aobjname' are NULL. If an `ifname' is provided,
2599  * it returns all the addresses for the given interface `ifname'.
2600  * If an `aobjname' is specified, then the address line corresponding to
2601  * that name will be returned.
2602  */
2603 static ipadm_status_t
2604 i_ipadm_get_db_addr(ipadm_handle_t iph, const char *ifname,
2605     const char *aobjname, nvlist_t **onvl)
2606 {
2607 	ipmgmt_getaddr_arg_t	garg;
2608 
2609 	/* Populate the door_call argument structure */
2610 	bzero(&garg, sizeof (garg));
2611 	garg.ia_cmd = IPMGMT_CMD_GETADDR;
2612 	if (aobjname != NULL)
2613 		(void) strlcpy(garg.ia_aobjname, aobjname,
2614 		    sizeof (garg.ia_aobjname));
2615 	if (ifname != NULL)
2616 		(void) strlcpy(garg.ia_ifname, ifname, sizeof (garg.ia_ifname));
2617 
2618 	return (i_ipadm_call_ipmgmtd(iph, (void *) &garg, sizeof (garg), onvl));
2619 }
2620 
2621 /*
2622  * Adds the IP address contained in the 'ipaddr' argument to the physical
2623  * interface represented by 'ifname' after doing the required validation.
2624  * If the interface does not exist, it is created before the address is
2625  * added.
2626  *
2627  * If IPH_LEGACY is set in iph_flags, flags has to be IPADM_OPT_ACTIVE
2628  * and a default addrobj name will be generated. Input `addr->ipadm_aobjname',
2629  * if provided, will be ignored and replaced with the newly generated name.
2630  * The interface name provided has to be a logical interface name that
2631  * already exists. No new logical interface will be added in this function.
2632  *
2633  * If IPADM_OPT_V46 is passed in the flags, then both IPv4 and IPv6 interfaces
2634  * are plumbed (if they haven't been already).  Otherwise, just the interface
2635  * specified in `addr' is plumbed.
2636  */
2637 ipadm_status_t
2638 ipadm_create_addr(ipadm_handle_t iph, ipadm_addrobj_t addr, uint32_t flags)
2639 {
2640 	ipadm_status_t		status;
2641 	sa_family_t		af;
2642 	sa_family_t		daf;
2643 	sa_family_t		other_af;
2644 	boolean_t		created_af = B_FALSE;
2645 	boolean_t		created_other_af = B_FALSE;
2646 	ipadm_addr_type_t	type;
2647 	char			*ifname = addr->ipadm_ifname;
2648 	boolean_t		legacy = (iph->iph_flags & IPH_LEGACY);
2649 	boolean_t		aobjfound = B_FALSE;
2650 	boolean_t		is_6to4;
2651 	struct lifreq		lifr;
2652 	uint64_t		ifflags;
2653 	boolean_t		is_boot = (iph->iph_flags & IPH_IPMGMTD);
2654 	boolean_t		is_ipmp;
2655 	char			gifname[LIFGRNAMSIZ];
2656 
2657 	/* check for solaris.network.interface.config authorization */
2658 	if (!ipadm_check_auth())
2659 		return (IPADM_EAUTH);
2660 
2661 	/* Validate the addrobj. This also fills in addr->ipadm_ifname. */
2662 	status = i_ipadm_validate_create_addr(iph, addr, flags);
2663 	if (status != IPADM_SUCCESS)
2664 		return (status);
2665 	/*
2666 	 * For Legacy case, check if an addrobj already exists for the
2667 	 * given logical interface name. If one does not exist,
2668 	 * a default name will be generated and added to the daemon's
2669 	 * aobjmap.
2670 	 */
2671 	if (legacy) {
2672 		struct ipadm_addrobj_s	ipaddr;
2673 
2674 		ipaddr = *addr;
2675 		status = i_ipadm_get_lif2addrobj(iph, &ipaddr);
2676 		if (status == IPADM_SUCCESS) {
2677 			aobjfound = B_TRUE;
2678 			/*
2679 			 * With IPH_LEGACY, modifying an address that is not
2680 			 * a static address will return with an error.
2681 			 */
2682 			if (ipaddr.ipadm_atype != IPADM_ADDR_STATIC)
2683 				return (IPADM_NOTSUP);
2684 			/*
2685 			 * we found the addrobj in daemon, copy over the
2686 			 * aobjname to `addr'.
2687 			 */
2688 			(void) strlcpy(addr->ipadm_aobjname,
2689 			    ipaddr.ipadm_aobjname, IPADM_AOBJSIZ);
2690 		} else if (status == IPADM_NOTFOUND) {
2691 			aobjfound = B_FALSE;
2692 		} else {
2693 			return (status);
2694 		}
2695 	}
2696 
2697 	af = addr->ipadm_af;
2698 	/*
2699 	 * Create a placeholder for this address object in the daemon.
2700 	 * Skip this step if we are booting a zone (and therefore being called
2701 	 * from ipmgmtd itself), and, for IPH_LEGACY case if the
2702 	 * addrobj already exists.
2703 	 *
2704 	 * Note that the placeholder is not needed in the NGZ boot case,
2705 	 * when zoneadmd has itself applied the "allowed-ips" property to clamp
2706 	 * down any interface configuration, so the namespace for the interface
2707 	 * is fully controlled by the GZ.
2708 	 */
2709 	if (!is_boot && (!legacy || !aobjfound)) {
2710 		status = i_ipadm_lookupadd_addrobj(iph, addr);
2711 		if (status != IPADM_SUCCESS)
2712 			return (status);
2713 	}
2714 
2715 	is_6to4 = i_ipadm_is_6to4(iph, ifname);
2716 	/* Plumb the IP interfaces if necessary */
2717 	status = i_ipadm_create_if(iph, ifname, af, flags);
2718 	if (status != IPADM_SUCCESS && status != IPADM_IF_EXISTS) {
2719 		(void) i_ipadm_delete_addrobj(iph, addr, IPADM_OPT_ACTIVE);
2720 		return (status);
2721 	}
2722 	if (status == IPADM_SUCCESS)
2723 		created_af = B_TRUE;
2724 	if (!is_6to4 && !legacy && (flags & IPADM_OPT_V46)) {
2725 		other_af = (af == AF_INET ? AF_INET6 : AF_INET);
2726 		status = i_ipadm_create_if(iph, ifname, other_af, flags);
2727 		if (status != IPADM_SUCCESS && status != IPADM_IF_EXISTS) {
2728 			(void) i_ipadm_delete_if(iph, ifname, af, flags);
2729 			return (status);
2730 		}
2731 		if (status == IPADM_SUCCESS)
2732 			created_other_af = B_TRUE;
2733 	}
2734 
2735 	/*
2736 	 * Some input validation based on the interface flags:
2737 	 * 1. in non-global zones, make sure that we are not persistently
2738 	 *    creating addresses on interfaces that are acquiring
2739 	 *    address from the global zone.
2740 	 * 2. Validate static addresses for IFF_POINTOPOINT interfaces.
2741 	 */
2742 	if (addr->ipadm_atype == IPADM_ADDR_STATIC) {
2743 		status = i_ipadm_get_flags(iph, ifname, af, &ifflags);
2744 		if (status != IPADM_SUCCESS)
2745 			goto fail;
2746 
2747 		if (iph->iph_zoneid != GLOBAL_ZONEID &&
2748 		    (ifflags & IFF_L3PROTECT) && (flags & IPADM_OPT_PERSIST)) {
2749 			status = IPADM_GZ_PERM;
2750 			goto fail;
2751 		}
2752 		daf = addr->ipadm_static_dst_addr.ss_family;
2753 		if (ifflags & IFF_POINTOPOINT) {
2754 			if (is_6to4) {
2755 				if (af != AF_INET6 || daf != AF_UNSPEC) {
2756 					status = IPADM_INVALID_ARG;
2757 					goto fail;
2758 				}
2759 			} else {
2760 				if (daf != af) {
2761 					status = IPADM_INVALID_ARG;
2762 					goto fail;
2763 				}
2764 				/* Check for a valid dst address. */
2765 				if (!legacy && sockaddrunspec(
2766 				    (struct sockaddr *)
2767 				    &addr->ipadm_static_dst_addr)) {
2768 					status = IPADM_BAD_ADDR;
2769 					goto fail;
2770 				}
2771 			}
2772 		} else {
2773 			/*
2774 			 * Disallow setting of dstaddr when the link is not
2775 			 * a point-to-point link.
2776 			 */
2777 			if (daf != AF_UNSPEC)
2778 				return (IPADM_INVALID_ARG);
2779 		}
2780 	}
2781 
2782 	/*
2783 	 * For 6to4 interfaces, kernel configures a default link-local
2784 	 * address. We need to replace it, if the caller has provided
2785 	 * an address that is different from the default link-local.
2786 	 */
2787 	if (status == IPADM_SUCCESS && is_6to4) {
2788 		bzero(&lifr, sizeof (lifr));
2789 		(void) strlcpy(lifr.lifr_name, addr->ipadm_ifname,
2790 		    sizeof (lifr.lifr_name));
2791 		if (ioctl(iph->iph_sock6, SIOCGLIFADDR, &lifr) < 0) {
2792 			status = ipadm_errno2status(errno);
2793 			goto fail;
2794 		}
2795 		if (sockaddrcmp(&lifr.lifr_addr, &addr->ipadm_static_addr))
2796 			return (IPADM_SUCCESS);
2797 	}
2798 
2799 	/*
2800 	 * If interface is an IPMP group member, move it out of the group before
2801 	 * performing any operations on it.
2802 	 */
2803 	if ((is_ipmp = i_ipadm_is_under_ipmp(iph, addr->ipadm_ifname))) {
2804 		(void) i_ipadm_get_groupname_active(iph, addr->ipadm_ifname,
2805 		    gifname, sizeof (gifname));
2806 		(void) i_ipadm_set_groupname_active(iph, addr->ipadm_ifname,
2807 		    "");
2808 	}
2809 
2810 	/* Create the address. */
2811 	type = addr->ipadm_atype;
2812 	switch (type) {
2813 	case IPADM_ADDR_STATIC:
2814 		status = i_ipadm_create_addr(iph, addr, flags);
2815 		break;
2816 	case IPADM_ADDR_DHCP:
2817 		status = i_ipadm_create_dhcp(iph, addr, flags);
2818 		break;
2819 	case IPADM_ADDR_IPV6_ADDRCONF:
2820 		status = i_ipadm_create_ipv6addrs(iph, addr, flags);
2821 		break;
2822 	default:
2823 		status = IPADM_INVALID_ARG;
2824 		break;
2825 	}
2826 
2827 	/* Move the underlying IPMP interface back to the group */
2828 	if (is_ipmp) {
2829 		(void) i_ipadm_set_groupname_active(iph, addr->ipadm_ifname,
2830 		    gifname);
2831 	}
2832 
2833 	/*
2834 	 * If address was not created successfully, unplumb the interface
2835 	 * if it was plumbed implicitly in this function and remove the
2836 	 * addrobj created by the ipmgmtd daemon as a placeholder.
2837 	 * If IPH_LEGACY is set, then remove the addrobj only if it was
2838 	 * created in this function.
2839 	 */
2840 fail:
2841 	if (status != IPADM_DHCP_IPC_TIMEOUT &&
2842 	    status != IPADM_SUCCESS) {
2843 		if (!legacy) {
2844 			if (created_af || created_other_af) {
2845 				if (created_af) {
2846 					(void) i_ipadm_delete_if(iph, ifname,
2847 					    af, flags);
2848 				}
2849 				if (created_other_af) {
2850 					(void) i_ipadm_delete_if(iph, ifname,
2851 					    other_af, flags);
2852 				}
2853 			} else {
2854 				(void) i_ipadm_delete_addrobj(iph, addr, flags);
2855 			}
2856 		} else if (!aobjfound) {
2857 			(void) i_ipadm_delete_addrobj(iph, addr, flags);
2858 		}
2859 	}
2860 
2861 	return (status);
2862 }
2863 
2864 /*
2865  * Creates the static address in `ipaddr' in kernel. After successfully
2866  * creating it, it updates the ipmgmtd daemon's aobjmap with the logical
2867  * interface information.
2868  */
2869 static ipadm_status_t
2870 i_ipadm_create_addr(ipadm_handle_t iph, ipadm_addrobj_t ipaddr, uint32_t flags)
2871 {
2872 	struct lifreq			lifr;
2873 	ipadm_status_t			status = IPADM_SUCCESS;
2874 	int				sock;
2875 	struct sockaddr_storage		m, *mask = &m;
2876 	const struct sockaddr_storage	*addr = &ipaddr->ipadm_static_addr;
2877 	const struct sockaddr_storage	*daddr = &ipaddr->ipadm_static_dst_addr;
2878 	sa_family_t			af;
2879 	boolean_t			legacy = (iph->iph_flags & IPH_LEGACY);
2880 	struct ipadm_addrobj_s		legacy_addr;
2881 	boolean_t			default_prefixlen = B_FALSE;
2882 	boolean_t			is_boot;
2883 
2884 	is_boot = ((iph->iph_flags & IPH_IPMGMTD) != 0);
2885 	af = ipaddr->ipadm_af;
2886 	sock = (af == AF_INET ? iph->iph_sock : iph->iph_sock6);
2887 
2888 	/* If prefixlen was not provided, get default prefixlen */
2889 	if (ipaddr->ipadm_static_prefixlen == 0) {
2890 		/* prefixlen was not provided, get default prefixlen */
2891 		status = i_ipadm_get_default_prefixlen(
2892 		    &ipaddr->ipadm_static_addr,
2893 		    &ipaddr->ipadm_static_prefixlen);
2894 		if (status != IPADM_SUCCESS)
2895 			return (status);
2896 		default_prefixlen = B_TRUE;
2897 	}
2898 	(void) plen2mask(ipaddr->ipadm_static_prefixlen, af,
2899 	    (struct sockaddr *)mask);
2900 
2901 	/*
2902 	 * Create a new logical interface if needed; otherwise, just
2903 	 * use the 0th logical interface.
2904 	 */
2905 	if (!(iph->iph_flags & IPH_LEGACY)) {
2906 		status = i_ipadm_do_addif(iph, ipaddr, NULL);
2907 		if (status != IPADM_SUCCESS)
2908 			return (status);
2909 	}
2910 	i_ipadm_addrobj2lifname(ipaddr, lifr.lifr_name,
2911 	    sizeof (lifr.lifr_name));
2912 	lifr.lifr_addr = *mask;
2913 	if (ioctl(sock, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
2914 		status = ipadm_errno2status(errno);
2915 		goto ret;
2916 	}
2917 	lifr.lifr_addr = *addr;
2918 	if (ioctl(sock, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
2919 		status = ipadm_errno2status(errno);
2920 		goto ret;
2921 	}
2922 	/* Set the destination address, if one is given. */
2923 	if (daddr->ss_family != AF_UNSPEC) {
2924 		lifr.lifr_addr = *daddr;
2925 		if (ioctl(sock, SIOCSLIFDSTADDR, (caddr_t)&lifr) < 0) {
2926 			status = ipadm_errno2status(errno);
2927 			goto ret;
2928 		}
2929 	}
2930 
2931 	if (flags & IPADM_OPT_UP) {
2932 		uint32_t	iff_flags = IFF_UP;
2933 
2934 		/*
2935 		 * Set the NOFAILOVER flag only on underlying IPMP interface
2936 		 * and not the IPMP group interface itself.
2937 		 */
2938 		if (i_ipadm_is_under_ipmp(iph, lifr.lifr_name) &&
2939 		    !i_ipadm_is_ipmp(iph, lifr.lifr_name))
2940 			iff_flags |= IFF_NOFAILOVER;
2941 
2942 		status = i_ipadm_set_flags(iph, lifr.lifr_name,
2943 		    af, iff_flags, 0);
2944 
2945 		/*
2946 		 * IPADM_DAD_FOUND is a soft-error for create-addr.
2947 		 * No need to tear down the address.
2948 		 */
2949 		if (status == IPADM_DAD_FOUND)
2950 			status = IPADM_SUCCESS;
2951 	}
2952 
2953 	if (status == IPADM_SUCCESS && !is_boot) {
2954 		/*
2955 		 * For IPH_LEGACY, we might be modifying the address on
2956 		 * an address object that already exists e.g. by doing
2957 		 * "ifconfig bge0:1 <addr>; ifconfig bge0:1 <newaddr>"
2958 		 * So, we need to store the object only if it does not
2959 		 * already exist in ipmgmtd.
2960 		 */
2961 		if (legacy) {
2962 			bzero(&legacy_addr, sizeof (legacy_addr));
2963 			(void) strlcpy(legacy_addr.ipadm_aobjname,
2964 			    ipaddr->ipadm_aobjname,
2965 			    sizeof (legacy_addr.ipadm_aobjname));
2966 			status = i_ipadm_get_addrobj(iph, &legacy_addr);
2967 			if (status == IPADM_SUCCESS &&
2968 			    legacy_addr.ipadm_lifnum >= 0) {
2969 				return (status);
2970 			}
2971 		}
2972 		status = i_ipadm_addr_persist(iph, ipaddr, default_prefixlen,
2973 		    flags, NULL);
2974 	}
2975 ret:
2976 	if (status != IPADM_SUCCESS && !legacy)
2977 		(void) i_ipadm_delete_addr(iph, ipaddr);
2978 
2979 	return (status);
2980 }
2981 
2982 /*
2983  * Removes the address object identified by `aobjname' from both active and
2984  * persistent configuration. The address object will be removed from only
2985  * active configuration if IPH_LEGACY is set in `iph->iph_flags'.
2986  *
2987  * If the address type is IPADM_ADDR_STATIC or IPADM_ADDR_DHCP, the address
2988  * in the address object will be removed from the physical interface.
2989  * If the address type is IPADM_ADDR_DHCP, the flag IPADM_OPT_RELEASE specifies
2990  * whether the lease should be released. If IPADM_OPT_RELEASE is not
2991  * specified, the lease will be dropped. This option is not supported
2992  * for other address types.
2993  *
2994  * If the address type is IPADM_ADDR_IPV6_ADDRCONF, the link-local address and
2995  * all the autoconfigured addresses will be removed.
2996  * Finally, the address object is also removed from ipmgmtd's aobjmap and from
2997  * the persistent DB.
2998  */
2999 ipadm_status_t
3000 ipadm_delete_addr(ipadm_handle_t iph, const char *aobjname, uint32_t flags)
3001 {
3002 	ipadm_status_t		status;
3003 	struct ipadm_addrobj_s	ipaddr;
3004 	boolean_t		release = ((flags & IPADM_OPT_RELEASE) != 0);
3005 	boolean_t		is_ipmp = B_FALSE;
3006 	char			gifname[LIFGRNAMSIZ];
3007 
3008 	/* check for solaris.network.interface.config authorization */
3009 	if (!ipadm_check_auth())
3010 		return (IPADM_EAUTH);
3011 
3012 	/* validate input */
3013 	if (flags == 0 || ((flags & IPADM_OPT_PERSIST) &&
3014 	    !(flags & IPADM_OPT_ACTIVE)) ||
3015 	    (flags & ~(IPADM_COMMON_OPT_MASK|IPADM_OPT_RELEASE))) {
3016 		return (IPADM_INVALID_ARG);
3017 	}
3018 	bzero(&ipaddr, sizeof (ipaddr));
3019 	if (aobjname == NULL || strlcpy(ipaddr.ipadm_aobjname, aobjname,
3020 	    IPADM_AOBJSIZ) >= IPADM_AOBJSIZ) {
3021 		return (IPADM_INVALID_ARG);
3022 	}
3023 
3024 	/* Retrieve the address object information from ipmgmtd. */
3025 	status = i_ipadm_get_addrobj(iph, &ipaddr);
3026 	if (status != IPADM_SUCCESS)
3027 		return (status);
3028 
3029 	if (release && ipaddr.ipadm_atype != IPADM_ADDR_DHCP)
3030 		return (IPADM_NOTSUP);
3031 	/*
3032 	 * If requested to delete just from active config but the address
3033 	 * is not in active config, return error.
3034 	 */
3035 	if (!(ipaddr.ipadm_flags & IPMGMT_ACTIVE) &&
3036 	    (flags & IPADM_OPT_ACTIVE) && !(flags & IPADM_OPT_PERSIST)) {
3037 		return (IPADM_NOTFOUND);
3038 	}
3039 
3040 	/*
3041 	 * If address is present in active config, remove it from
3042 	 * kernel.
3043 	 */
3044 	if (ipaddr.ipadm_flags & IPMGMT_ACTIVE) {
3045 
3046 		/*
3047 		 * If interface is an IPMP group member, move it out of the
3048 		 * group before performing any operations on it.
3049 		 */
3050 		if ((is_ipmp = i_ipadm_is_under_ipmp(iph,
3051 		    ipaddr.ipadm_ifname))) {
3052 			(void) i_ipadm_get_groupname_active(iph,
3053 			    ipaddr.ipadm_ifname, gifname, sizeof (gifname));
3054 			(void) i_ipadm_set_groupname_active(iph,
3055 			    ipaddr.ipadm_ifname, "");
3056 		}
3057 
3058 		switch (ipaddr.ipadm_atype) {
3059 		case IPADM_ADDR_STATIC:
3060 			status = i_ipadm_delete_addr(iph, &ipaddr);
3061 			break;
3062 		case IPADM_ADDR_DHCP:
3063 			status = i_ipadm_delete_dhcp(iph, &ipaddr, release);
3064 			break;
3065 		case IPADM_ADDR_IPV6_ADDRCONF:
3066 			status = i_ipadm_delete_ipv6addrs(iph, &ipaddr);
3067 			break;
3068 		default:
3069 			/*
3070 			 * This is the case of address object name residing in
3071 			 * daemon's aobjmap (added by ADDROBJ_LOOKUPADD). Fall
3072 			 * through and delete that address object.
3073 			 */
3074 			break;
3075 		}
3076 
3077 		/*
3078 		 * If the address was previously deleted from the active
3079 		 * config, we will get a IPADM_ENXIO from kernel.
3080 		 * We will still proceed and purge the address information
3081 		 * in the DB.
3082 		 */
3083 		if (status == IPADM_ENXIO)
3084 			status = IPADM_SUCCESS;
3085 		else if (status != IPADM_SUCCESS)
3086 			goto out;
3087 	}
3088 
3089 	if (!(ipaddr.ipadm_flags & IPMGMT_PERSIST) &&
3090 	    (flags & IPADM_OPT_PERSIST)) {
3091 		flags &= ~IPADM_OPT_PERSIST;
3092 	}
3093 	status = i_ipadm_delete_addrobj(iph, &ipaddr, flags);
3094 
3095 	if (status != IPADM_NOTFOUND)
3096 		status = IPADM_SUCCESS;
3097 
3098 out:
3099 	/*
3100 	 * Move the underlying IPMP interface back to the group.
3101 	 * This cannot be done until the persistent configuration has been
3102 	 * deleted as it will otherwise cause the active configuration to be
3103 	 * restored.
3104 	 */
3105 	if (is_ipmp) {
3106 		(void) i_ipadm_set_groupname_active(iph,
3107 		    ipaddr.ipadm_ifname, gifname);
3108 	}
3109 	return (status);
3110 }
3111 
3112 /*
3113  * Starts the dhcpagent and sends it the message DHCP_START to start
3114  * configuring a dhcp address on the given interface in `addr'.
3115  * After making the dhcpagent request, it also updates the
3116  * address object information in ipmgmtd's aobjmap and creates an
3117  * entry in persistent DB if IPADM_OPT_PERSIST is set in `flags'.
3118  */
3119 static ipadm_status_t
3120 i_ipadm_create_dhcp(ipadm_handle_t iph, ipadm_addrobj_t addr, uint32_t flags)
3121 {
3122 	ipadm_status_t	status;
3123 	ipadm_status_t	dh_status;
3124 
3125 	if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1)
3126 		return (IPADM_DHCP_START_ERROR);
3127 	/*
3128 	 * Create a new logical interface if needed; otherwise, just
3129 	 * use the 0th logical interface.
3130 	 */
3131 retry:
3132 	status = i_ipadm_do_addif(iph, addr, NULL);
3133 	if (status != IPADM_SUCCESS)
3134 		return (status);
3135 	/*
3136 	 * We don't have to set the lifnum for IPH_INIT case, because
3137 	 * there is no placeholder created for the address object in this
3138 	 * case.
3139 	 */
3140 	if (!(iph->iph_flags & IPH_INIT)) {
3141 		status = i_ipadm_setlifnum_addrobj(iph, addr);
3142 		if (status == IPADM_ADDROBJ_EXISTS)
3143 			goto retry;
3144 		if (status != IPADM_SUCCESS)
3145 			return (status);
3146 	}
3147 	/* Send DHCP_START to the dhcpagent. */
3148 	status = i_ipadm_op_dhcp(addr, DHCP_START, NULL);
3149 	/*
3150 	 * We do not undo the create-addr operation for IPADM_DHCP_IPC_TIMEOUT
3151 	 * since it is only a soft error to indicate the caller that the lease
3152 	 * might be required after the function returns.
3153 	 */
3154 	if (status != IPADM_SUCCESS && status != IPADM_DHCP_IPC_TIMEOUT)
3155 		goto fail;
3156 	dh_status = status;
3157 
3158 	/* Persist the address object information in ipmgmtd. */
3159 	status = i_ipadm_addr_persist(iph, addr, B_FALSE, flags, NULL);
3160 	if (status != IPADM_SUCCESS)
3161 		goto fail;
3162 
3163 	return (dh_status);
3164 fail:
3165 	/* In case of error, delete the dhcp address */
3166 	(void) i_ipadm_delete_dhcp(iph, addr, B_TRUE);
3167 	return (status);
3168 }
3169 
3170 /*
3171  * Releases/drops the dhcp lease on the logical interface in the address
3172  * object `addr'. If `release' is set to B_FALSE, the lease will be dropped.
3173  */
3174 static ipadm_status_t
3175 i_ipadm_delete_dhcp(ipadm_handle_t iph, ipadm_addrobj_t addr, boolean_t release)
3176 {
3177 	ipadm_status_t	status;
3178 	int		dherr;
3179 
3180 	/* Send DHCP_RELEASE or DHCP_DROP to the dhcpagent */
3181 	if (release) {
3182 		status = i_ipadm_op_dhcp(addr, DHCP_RELEASE, &dherr);
3183 		/*
3184 		 * If no lease was obtained on the object, we should
3185 		 * drop the dhcp control on the interface.
3186 		 */
3187 		if (status != IPADM_SUCCESS && dherr == DHCP_IPC_E_OUTSTATE)
3188 			status = i_ipadm_op_dhcp(addr, DHCP_DROP, NULL);
3189 	} else {
3190 		status = i_ipadm_op_dhcp(addr, DHCP_DROP, NULL);
3191 	}
3192 	if (status != IPADM_SUCCESS)
3193 		return (status);
3194 
3195 	/* Delete the logical interface */
3196 	if (addr->ipadm_lifnum != 0) {
3197 		struct lifreq lifr;
3198 
3199 		bzero(&lifr, sizeof (lifr));
3200 		i_ipadm_addrobj2lifname(addr, lifr.lifr_name,
3201 		    sizeof (lifr.lifr_name));
3202 		if (ioctl(iph->iph_sock, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0)
3203 			return (ipadm_errno2status(errno));
3204 	}
3205 
3206 	return (IPADM_SUCCESS);
3207 }
3208 
3209 /*
3210  * Communicates with the dhcpagent to send a dhcp message of type `type'.
3211  * It returns the dhcp error in `dhcperror' if a non-null pointer is provided
3212  * in `dhcperror'.
3213  */
3214 static ipadm_status_t
3215 i_ipadm_op_dhcp(ipadm_addrobj_t addr, dhcp_ipc_type_t type, int *dhcperror)
3216 {
3217 	dhcp_ipc_request_t	*request;
3218 	dhcp_ipc_reply_t	*reply	= NULL;
3219 	dhcp_symbol_t		*entry = NULL;
3220 	dhcp_data_type_t	dtype = DHCP_TYPE_NONE;
3221 	void			*d4o = NULL;
3222 	uint16_t		d4olen = 0;
3223 	char			ifname[LIFNAMSIZ];
3224 	int			error;
3225 	int			dhcp_timeout;
3226 
3227 	/* Construct a message to the dhcpagent. */
3228 	bzero(&ifname, sizeof (ifname));
3229 	i_ipadm_addrobj2lifname(addr, ifname, sizeof (ifname));
3230 	if (addr->ipadm_primary)
3231 		type |= DHCP_PRIMARY;
3232 
3233 	/* Set up a CD_HOSTNAME option, if applicable, to send through IPC */
3234 	switch (DHCP_IPC_CMD(type)) {
3235 	case DHCP_START:
3236 	case DHCP_EXTEND:
3237 		if (addr->ipadm_af == AF_INET && *addr->ipadm_reqhost != '\0') {
3238 			entry = inittab_getbycode(ITAB_CAT_STANDARD,
3239 			    ITAB_CONS_INFO, CD_HOSTNAME);
3240 			if (entry == NULL) {
3241 				return (IPADM_FAILURE);
3242 			} else {
3243 				d4o = inittab_encode(entry, addr->ipadm_reqhost,
3244 				    &d4olen, B_FALSE);
3245 				free(entry);
3246 				entry = NULL;
3247 				if (d4o == NULL)
3248 					return (IPADM_FAILURE);
3249 				dtype = DHCP_TYPE_OPTION;
3250 			}
3251 		}
3252 		break;
3253 	default:
3254 		break;
3255 	}
3256 
3257 	request = dhcp_ipc_alloc_request(type, ifname, d4o, d4olen, dtype);
3258 	if (request == NULL) {
3259 		free(d4o);
3260 		return (IPADM_NO_MEMORY);
3261 	}
3262 
3263 	if (addr->ipadm_wait == IPADM_DHCP_WAIT_FOREVER)
3264 		dhcp_timeout = DHCP_IPC_WAIT_FOREVER;
3265 	else if (addr->ipadm_wait == IPADM_DHCP_WAIT_DEFAULT)
3266 		dhcp_timeout = DHCP_IPC_WAIT_DEFAULT;
3267 	else
3268 		dhcp_timeout = addr->ipadm_wait;
3269 	/* Send the message to dhcpagent. */
3270 	error = dhcp_ipc_make_request(request, &reply, dhcp_timeout);
3271 	free(request);
3272 	free(d4o);
3273 	if (error == 0) {
3274 		error = reply->return_code;
3275 		free(reply);
3276 	}
3277 	if (error != 0) {
3278 		if (dhcperror != NULL)
3279 			*dhcperror = error;
3280 		if (error != DHCP_IPC_E_TIMEOUT)
3281 			return (IPADM_DHCP_IPC_ERROR);
3282 		else if (dhcp_timeout != 0)
3283 			return (IPADM_DHCP_IPC_TIMEOUT);
3284 	}
3285 
3286 	return (IPADM_SUCCESS);
3287 }
3288 
3289 /*
3290  * Communicates with the dhcpagent to send a dhcp message of type
3291  * DHCP_STATUS, and copy on success into the `status' instance owned by the
3292  * caller. It returns any dhcp error in `dhcperror' if a non-null pointer
3293  * is provided.
3294  */
3295 static ipadm_status_t
3296 i_ipadm_dhcp_status(ipadm_addrobj_t addr, dhcp_status_t *status,
3297     int *dhcperror)
3298 {
3299 	dhcp_ipc_type_t		type = DHCP_STATUS;
3300 	dhcp_ipc_request_t	*request;
3301 	dhcp_ipc_reply_t	*reply;
3302 	dhcp_status_t		*private_status;
3303 	size_t			reply_size;
3304 	int			error;
3305 
3306 	if (addr->ipadm_af == AF_INET6)
3307 		type |= DHCP_V6;
3308 
3309 	request = dhcp_ipc_alloc_request(type, addr->ipadm_ifname, NULL, 0,
3310 	    DHCP_TYPE_NONE);
3311 	if (request == NULL)
3312 		return (IPADM_NO_MEMORY);
3313 
3314 	error = dhcp_ipc_make_request(request, &reply, DHCP_IPC_WAIT_DEFAULT);
3315 	free(request);
3316 	if (error != 0) {
3317 		if (dhcperror != NULL)
3318 			*dhcperror = error;
3319 		return (error != DHCP_IPC_E_TIMEOUT ? IPADM_DHCP_IPC_ERROR
3320 		    : IPADM_DHCP_IPC_TIMEOUT);
3321 	}
3322 
3323 	error = reply->return_code;
3324 	if (error == DHCP_IPC_E_UNKIF) {
3325 		free(reply);
3326 		bzero(status, sizeof (dhcp_status_t));
3327 		return (IPADM_NOTFOUND);
3328 	}
3329 
3330 	private_status = dhcp_ipc_get_data(reply, &reply_size, NULL);
3331 	if (reply_size < DHCP_STATUS_VER1_SIZE) {
3332 		free(reply);
3333 		return (IPADM_DHCP_IPC_ERROR);
3334 	}
3335 
3336 	/*
3337 	 * Copy the status out of the memory allocated by this function into
3338 	 * memory owned by the caller.
3339 	 */
3340 	*status = *private_status;
3341 	free(reply);
3342 	return (IPADM_SUCCESS);
3343 }
3344 
3345 /*
3346  * Returns the IP addresses of the specified interface in both the
3347  * active and the persistent configuration. If no
3348  * interface is specified, it returns all non-zero IP addresses
3349  * configured on all interfaces in active and persistent
3350  * configurations.
3351  * `addrinfo' will contain addresses that are
3352  * (1) in both active and persistent configuration (created persistently)
3353  * (2) only in active configuration (created temporarily)
3354  * (3) only in persistent configuration (disabled addresses)
3355  *
3356  * Address list that is returned by this function must be freed
3357  * using the ipadm_freeaddr_info() function.
3358  */
3359 ipadm_status_t
3360 ipadm_addr_info(ipadm_handle_t iph, const char *ifname,
3361     ipadm_addr_info_t **addrinfo, uint32_t flags, int64_t lifc_flags)
3362 {
3363 	ifspec_t	ifsp;
3364 
3365 	if (addrinfo == NULL || iph == NULL)
3366 		return (IPADM_INVALID_ARG);
3367 	if (ifname != NULL &&
3368 	    (!ifparse_ifspec(ifname, &ifsp) || ifsp.ifsp_lunvalid)) {
3369 		return (IPADM_INVALID_ARG);
3370 	}
3371 	return (i_ipadm_get_all_addr_info(iph, ifname, addrinfo,
3372 	    flags, lifc_flags));
3373 }
3374 
3375 /*
3376  * Frees the structure allocated by ipadm_addr_info().
3377  */
3378 void
3379 ipadm_free_addr_info(ipadm_addr_info_t *ainfo)
3380 {
3381 	freeifaddrs((struct ifaddrs *)ainfo);
3382 }
3383 
3384 /*
3385  * Makes a door call to ipmgmtd to update its `aobjmap' with the address
3386  * object in `ipaddr'. This door call also can update the persistent DB to
3387  * remember address object to be recreated on next reboot or on an
3388  * ipadm_enable_addr()/ipadm_enable_if() call.
3389  */
3390 ipadm_status_t
3391 i_ipadm_addr_persist(ipadm_handle_t iph, const ipadm_addrobj_t ipaddr,
3392     boolean_t default_prefixlen, uint32_t flags, const char *propname)
3393 {
3394 	char			*aname = ipaddr->ipadm_aobjname;
3395 	nvlist_t		*nvl;
3396 	int			err = 0;
3397 	ipadm_status_t		status;
3398 	uint_t			pflags = 0;
3399 
3400 	/*
3401 	 * Construct the nvl to send to the door.
3402 	 */
3403 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
3404 		return (IPADM_NO_MEMORY);
3405 	if ((err = nvlist_add_string(nvl, IPADM_NVP_IFNAME,
3406 	    ipaddr->ipadm_ifname)) != 0 ||
3407 	    (err = nvlist_add_string(nvl, IPADM_NVP_AOBJNAME, aname)) != 0 ||
3408 	    (err = nvlist_add_int32(nvl, IPADM_NVP_LIFNUM,
3409 	    ipaddr->ipadm_lifnum)) != 0) {
3410 		status = ipadm_errno2status(err);
3411 		goto ret;
3412 	}
3413 	switch (ipaddr->ipadm_atype) {
3414 	case IPADM_ADDR_STATIC:
3415 		status = i_ipadm_add_ipaddr2nvl(nvl, ipaddr);
3416 		if (status != IPADM_SUCCESS)
3417 			goto ret;
3418 		if (flags & IPADM_OPT_UP)
3419 			err = nvlist_add_string(nvl, "up", "yes");
3420 		else
3421 			err = nvlist_add_string(nvl, "up", "no");
3422 		status = ipadm_errno2status(err);
3423 		break;
3424 	case IPADM_ADDR_DHCP:
3425 		status = i_ipadm_add_dhcp2nvl(nvl, ipaddr->ipadm_primary,
3426 		    ipaddr->ipadm_wait);
3427 		if (status != IPADM_SUCCESS)
3428 			goto ret;
3429 
3430 		/*
3431 		 * For purposes of updating the ipmgmtd cached representation of
3432 		 * reqhost (ipmgmt_am_reqhost), include a value here in `nvl',
3433 		 * but the value is actually fully persisted as a separate
3434 		 * i_ipadm_persist_propval below.
3435 		 */
3436 		err = nvlist_add_string(nvl, IPADM_NVP_REQHOST,
3437 		    ipaddr->ipadm_reqhost);
3438 		status = ipadm_errno2status(err);
3439 		break;
3440 	case IPADM_ADDR_IPV6_ADDRCONF:
3441 		status = i_ipadm_add_intfid2nvl(nvl, ipaddr);
3442 		break;
3443 	}
3444 	if (status != IPADM_SUCCESS)
3445 		goto ret;
3446 
3447 	if (iph->iph_flags & IPH_INIT) {
3448 		/*
3449 		 * IPMGMT_INIT tells the ipmgmtd to set both IPMGMT_ACTIVE and
3450 		 * IPMGMT_PERSIST on the address object in its `aobjmap'.
3451 		 * For the callers ipadm_enable_if() and ipadm_enable_addr(),
3452 		 * IPADM_OPT_PERSIST is not set in their flags. They send
3453 		 * IPH_INIT in iph_flags, so that the address object will be
3454 		 * set as both IPMGMT_ACTIVE and IPMGMT_PERSIST.
3455 		 */
3456 		pflags |= IPMGMT_INIT;
3457 	} else {
3458 		if (flags & IPADM_OPT_ACTIVE)
3459 			pflags |= IPMGMT_ACTIVE;
3460 		if (flags & IPADM_OPT_PERSIST)
3461 			pflags |= IPMGMT_PERSIST;
3462 		if (flags & IPADM_OPT_SET_PROPS)
3463 			pflags |= IPMGMT_PROPS_ONLY;
3464 	}
3465 	status = i_ipadm_addr_persist_nvl(iph, nvl, pflags);
3466 
3467 	if (flags & IPADM_OPT_SET_PROPS) {
3468 		/*
3469 		 * Set PERSIST per IPADM_OPT_PROPS_PERSIST, and then un-set the
3470 		 * SET_PROPS bits.
3471 		 */
3472 		flags |= IPADM_OPT_ACTIVE;
3473 		if (flags & IPADM_OPT_PERSIST_PROPS)
3474 			flags |= IPADM_OPT_PERSIST;
3475 		else
3476 			flags &= ~IPADM_OPT_PERSIST;
3477 		flags &= ~(IPADM_OPT_SET_PROPS | IPADM_OPT_PERSIST_PROPS);
3478 	}
3479 
3480 	if (status == IPADM_SUCCESS && (flags & IPADM_OPT_PERSIST)) {
3481 		char		pbuf[MAXPROPVALLEN], *pval = NULL;
3482 		ipadm_prop_desc_t	*pdp = NULL;
3483 
3484 		/*
3485 		 * addprop properties are stored on separate lines in the DB and
3486 		 * not along with the address itself. Call the function that
3487 		 * persists address properties.
3488 		 */
3489 
3490 		switch (ipaddr->ipadm_atype) {
3491 		case IPADM_ADDR_STATIC:
3492 			if (!default_prefixlen && (propname == NULL ||
3493 			    strcmp(propname, IPADM_NVP_PREFIXLEN) == 0)) {
3494 				pdp = i_ipadm_get_addrprop_desc(
3495 				    IPADM_NVP_PREFIXLEN);
3496 				(void) snprintf(pbuf, sizeof (pbuf), "%u",
3497 				    ipaddr->ipadm_static_prefixlen);
3498 				pval = pbuf;
3499 			}
3500 			break;
3501 		case IPADM_ADDR_DHCP:
3502 			if (propname == NULL ||
3503 			    strcmp(propname, IPADM_NVP_REQHOST) == 0) {
3504 				pdp = i_ipadm_get_addrprop_desc(
3505 				    IPADM_NVP_REQHOST);
3506 				pval = ipaddr->ipadm_reqhost;
3507 			}
3508 			break;
3509 		default:
3510 			break;
3511 		}
3512 
3513 		if (pval != NULL) {
3514 			assert(pdp != NULL);
3515 			status = i_ipadm_persist_propval(iph, pdp, pval,
3516 			    ipaddr, flags);
3517 		}
3518 	}
3519 
3520 ret:
3521 	nvlist_free(nvl);
3522 	return (status);
3523 }
3524 
3525 /*
3526  * Makes the door call to ipmgmtd to store the address object in the
3527  * nvlist `nvl'.
3528  */
3529 static ipadm_status_t
3530 i_ipadm_addr_persist_nvl(ipadm_handle_t iph, nvlist_t *nvl, uint32_t flags)
3531 {
3532 	char			*buf = NULL, *nvlbuf = NULL;
3533 	size_t			nvlsize, bufsize;
3534 	ipmgmt_setaddr_arg_t	*sargp;
3535 	int			err;
3536 
3537 	err = nvlist_pack(nvl, &nvlbuf, &nvlsize, NV_ENCODE_NATIVE, 0);
3538 	if (err != 0)
3539 		return (ipadm_errno2status(err));
3540 	bufsize = sizeof (*sargp) + nvlsize;
3541 	buf = calloc(1, bufsize);
3542 	sargp = (void *)buf;
3543 	sargp->ia_cmd = IPMGMT_CMD_SETADDR;
3544 	sargp->ia_flags = flags;
3545 	sargp->ia_nvlsize = nvlsize;
3546 	(void) bcopy(nvlbuf, buf + sizeof (*sargp), nvlsize);
3547 	err = ipadm_door_call(iph, buf, bufsize, NULL, 0, B_FALSE);
3548 	free(buf);
3549 	free(nvlbuf);
3550 	return (ipadm_errno2status(err));
3551 }
3552 
3553 /*
3554  * Makes a door call to ipmgmtd to remove the address object in `ipaddr'
3555  * from its `aobjmap'. This door call also removes the address object and all
3556  * its properties from the persistent DB if IPADM_OPT_PERSIST is set in
3557  * `flags', so that the object will not be recreated on next reboot or on an
3558  * ipadm_enable_addr()/ipadm_enable_if() call.
3559  */
3560 ipadm_status_t
3561 i_ipadm_delete_addrobj(ipadm_handle_t iph, const ipadm_addrobj_t ipaddr,
3562     uint32_t flags)
3563 {
3564 	ipmgmt_addr_arg_t	arg;
3565 	int			err;
3566 
3567 	arg.ia_cmd = IPMGMT_CMD_RESETADDR;
3568 	arg.ia_flags = 0;
3569 	if (flags & IPADM_OPT_ACTIVE)
3570 		arg.ia_flags |= IPMGMT_ACTIVE;
3571 	if (flags & IPADM_OPT_PERSIST)
3572 		arg.ia_flags |= IPMGMT_PERSIST;
3573 	(void) strlcpy(arg.ia_aobjname, ipaddr->ipadm_aobjname,
3574 	    sizeof (arg.ia_aobjname));
3575 	arg.ia_lnum = ipaddr->ipadm_lifnum;
3576 	err = ipadm_door_call(iph, &arg, sizeof (arg), NULL, 0, B_FALSE);
3577 	return (ipadm_errno2status(err));
3578 }
3579 
3580 /*
3581  * Checks if the caller is authorized for the up/down operation.
3582  * Retrieves the address object corresponding to `aobjname' from ipmgmtd
3583  * and retrieves the address flags for that object from kernel.
3584  * The arguments `ipaddr' and `ifflags' must be allocated by the caller.
3585  */
3586 static ipadm_status_t
3587 i_ipadm_updown_common(ipadm_handle_t iph, const char *aobjname,
3588     ipadm_addrobj_t ipaddr, uint32_t ipadm_flags, uint64_t *ifflags)
3589 {
3590 	ipadm_status_t	status;
3591 	char		lifname[LIFNAMSIZ];
3592 
3593 	/* check for solaris.network.interface.config authorization */
3594 	if (!ipadm_check_auth())
3595 		return (IPADM_EAUTH);
3596 
3597 	/* validate input */
3598 	if (aobjname == NULL || strlcpy(ipaddr->ipadm_aobjname, aobjname,
3599 	    IPADM_AOBJSIZ) >= IPADM_AOBJSIZ) {
3600 		return (IPADM_INVALID_ARG);
3601 	}
3602 
3603 	/* Retrieve the address object information. */
3604 	status = i_ipadm_get_addrobj(iph, ipaddr);
3605 	if (status != IPADM_SUCCESS)
3606 		return (status);
3607 
3608 	if (!(ipaddr->ipadm_flags & IPMGMT_ACTIVE))
3609 		return (IPADM_OP_DISABLE_OBJ);
3610 
3611 	if ((ipadm_flags & IPADM_OPT_PERSIST) &&
3612 	    !(ipaddr->ipadm_flags & IPMGMT_PERSIST))
3613 		return (IPADM_TEMPORARY_OBJ);
3614 
3615 	if (ipaddr->ipadm_atype == IPADM_ADDR_IPV6_ADDRCONF ||
3616 	    (ipaddr->ipadm_atype == IPADM_ADDR_DHCP &&
3617 	    (ipadm_flags & IPADM_OPT_PERSIST)))
3618 		return (IPADM_NOTSUP);
3619 
3620 	i_ipadm_addrobj2lifname(ipaddr, lifname, sizeof (lifname));
3621 
3622 	return (i_ipadm_get_flags(iph, lifname, ipaddr->ipadm_af, ifflags));
3623 }
3624 
3625 /*
3626  * Marks the address in the address object `aobjname' up. This operation is
3627  * not supported for an address object of type IPADM_ADDR_IPV6_ADDRCONF.
3628  * For an address object of type IPADM_ADDR_DHCP, this operation can
3629  * only be temporary and no updates will be made to the persistent DB.
3630  */
3631 ipadm_status_t
3632 ipadm_up_addr(ipadm_handle_t iph, const char *aobjname, uint32_t ipadm_flags)
3633 {
3634 	struct ipadm_addrobj_s ipaddr;
3635 	ipadm_status_t	status;
3636 	uint64_t	flags;
3637 	char		lifname[LIFNAMSIZ];
3638 
3639 	status = i_ipadm_updown_common(iph, aobjname, &ipaddr, ipadm_flags,
3640 	    &flags);
3641 	if (status != IPADM_SUCCESS)
3642 		return (status);
3643 	if (flags & IFF_UP)
3644 		goto persist;
3645 	/*
3646 	 * If the address is already a duplicate, then refresh-addr
3647 	 * should be used to mark it up.
3648 	 */
3649 	if (flags & IFF_DUPLICATE)
3650 		return (IPADM_DAD_FOUND);
3651 
3652 	i_ipadm_addrobj2lifname(&ipaddr, lifname, sizeof (lifname));
3653 	status = i_ipadm_set_flags(iph, lifname, ipaddr.ipadm_af, IFF_UP, 0);
3654 	if (status != IPADM_SUCCESS)
3655 		return (status);
3656 
3657 persist:
3658 	/* Update persistent DB. */
3659 	if (ipadm_flags & IPADM_OPT_PERSIST) {
3660 		status = i_ipadm_persist_propval(iph, &up_addrprop,
3661 		    "yes", &ipaddr, 0);
3662 	}
3663 
3664 	return (status);
3665 }
3666 
3667 /*
3668  * Marks the address in the address object `aobjname' down. This operation is
3669  * not supported for an address object of type IPADM_ADDR_IPV6_ADDRCONF.
3670  * For an address object of type IPADM_ADDR_DHCP, this operation can
3671  * only be temporary and no updates will be made to the persistent DB.
3672  */
3673 ipadm_status_t
3674 ipadm_down_addr(ipadm_handle_t iph, const char *aobjname, uint32_t ipadm_flags)
3675 {
3676 	struct ipadm_addrobj_s ipaddr;
3677 	ipadm_status_t	status;
3678 	struct lifreq	lifr;
3679 	uint64_t	flags;
3680 
3681 	status = i_ipadm_updown_common(iph, aobjname, &ipaddr, ipadm_flags,
3682 	    &flags);
3683 	if (status != IPADM_SUCCESS)
3684 		return (status);
3685 	i_ipadm_addrobj2lifname(&ipaddr, lifr.lifr_name,
3686 	    sizeof (lifr.lifr_name));
3687 	if (flags & IFF_UP) {
3688 		status = i_ipadm_set_flags(iph, lifr.lifr_name,
3689 		    ipaddr.ipadm_af, 0, IFF_UP);
3690 		if (status != IPADM_SUCCESS)
3691 			return (status);
3692 	} else if (flags & IFF_DUPLICATE) {
3693 		/*
3694 		 * Clear the IFF_DUPLICATE flag.
3695 		 */
3696 		if (ioctl(iph->iph_sock, SIOCGLIFADDR, &lifr) < 0)
3697 			return (ipadm_errno2status(errno));
3698 		if (ioctl(iph->iph_sock, SIOCSLIFADDR, &lifr) < 0)
3699 			return (ipadm_errno2status(errno));
3700 	}
3701 
3702 	/* Update persistent DB */
3703 	if (ipadm_flags & IPADM_OPT_PERSIST) {
3704 		status = i_ipadm_persist_propval(iph, &up_addrprop,
3705 		    "no", &ipaddr, 0);
3706 	}
3707 
3708 	return (status);
3709 }
3710 
3711 /*
3712  * Refreshes the address in the address object `aobjname'. If the address object
3713  * is of type IPADM_ADDR_STATIC, DAD is re-initiated on the address. If
3714  * `ipadm_flags' has IPADM_OPT_INFORM set, a DHCP_INFORM message is sent to the
3715  * dhcpagent for this static address. If the address object is of type
3716  * IPADM_ADDR_DHCP, a DHCP_EXTEND message is sent to the dhcpagent.
3717  * If a dhcp address has not yet been acquired, a DHCP_START is sent to the
3718  * dhcpagent. This operation is not supported for an address object of
3719  * type IPADM_ADDR_IPV6_ADDRCONF.
3720  */
3721 ipadm_status_t
3722 ipadm_refresh_addr(ipadm_handle_t iph, const char *aobjname,
3723     uint32_t ipadm_flags)
3724 {
3725 	ipadm_status_t		status = IPADM_SUCCESS;
3726 	uint64_t		flags;
3727 	struct ipadm_addrobj_s	ipaddr;
3728 	sa_family_t		af;
3729 	char			lifname[LIFNAMSIZ];
3730 	boolean_t		inform =
3731 	    ((ipadm_flags & IPADM_OPT_INFORM) != 0);
3732 
3733 	/* check for solaris.network.interface.config authorization */
3734 	if (!ipadm_check_auth())
3735 		return (IPADM_EAUTH);
3736 
3737 	bzero(&ipaddr, sizeof (ipaddr));
3738 	/* validate input */
3739 	if (aobjname == NULL || strlcpy(ipaddr.ipadm_aobjname, aobjname,
3740 	    IPADM_AOBJSIZ) >= IPADM_AOBJSIZ) {
3741 		return (IPADM_INVALID_ARG);
3742 	}
3743 
3744 	/* Retrieve the address object information. */
3745 	status = i_ipadm_get_addrobj(iph, &ipaddr);
3746 	if (status != IPADM_SUCCESS)
3747 		return (status);
3748 
3749 	if (!(ipaddr.ipadm_flags & IPMGMT_ACTIVE))
3750 		return (IPADM_OP_DISABLE_OBJ);
3751 
3752 	if (i_ipadm_is_vni(ipaddr.ipadm_ifname))
3753 		return (IPADM_NOTSUP);
3754 	if (inform && ipaddr.ipadm_atype != IPADM_ADDR_STATIC)
3755 		return (IPADM_INVALID_ARG);
3756 	af = ipaddr.ipadm_af;
3757 	if (ipaddr.ipadm_atype == IPADM_ADDR_STATIC) {
3758 		i_ipadm_addrobj2lifname(&ipaddr, lifname, sizeof (lifname));
3759 		status = i_ipadm_get_flags(iph, lifname, af, &flags);
3760 		if (status != IPADM_SUCCESS)
3761 			return (status);
3762 		if (inform) {
3763 			if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1)
3764 				return (IPADM_DHCP_START_ERROR);
3765 
3766 			ipaddr.ipadm_wait = IPADM_DHCP_WAIT_DEFAULT;
3767 			return (i_ipadm_op_dhcp(&ipaddr, DHCP_INFORM, NULL));
3768 		}
3769 		if (!(flags & IFF_DUPLICATE))
3770 			return (IPADM_SUCCESS);
3771 		status = i_ipadm_set_flags(iph, lifname, af, IFF_UP, 0);
3772 	} else if (ipaddr.ipadm_atype == IPADM_ADDR_DHCP) {
3773 		status = i_ipadm_refresh_dhcp(&ipaddr);
3774 	} else {
3775 		status = IPADM_NOTSUP;
3776 	}
3777 	return (status);
3778 }
3779 
3780 /*
3781  * This is called from ipadm_refresh_addr() and i_ipadm_set_reqhost() to
3782  * send a DHCP_EXTEND message and possibly a DHCP_START message
3783  * to the dhcpagent.
3784  */
3785 static ipadm_status_t
3786 i_ipadm_refresh_dhcp(ipadm_addrobj_t ipaddr)
3787 {
3788 	ipadm_status_t		status;
3789 	int			dherr;
3790 
3791 	status = i_ipadm_op_dhcp(ipaddr, DHCP_EXTEND, &dherr);
3792 	/*
3793 	 * Restart the dhcp address negotiation with server if no
3794 	 * address has been acquired yet.
3795 	 */
3796 	if (status != IPADM_SUCCESS && dherr == DHCP_IPC_E_OUTSTATE) {
3797 		ipaddr->ipadm_wait = IPADM_DHCP_WAIT_DEFAULT;
3798 		status = i_ipadm_op_dhcp(ipaddr, DHCP_START, NULL);
3799 	}
3800 
3801 	return (status);
3802 }
3803 
3804 /*
3805  * This is called from ipadm_create_addr() to validate the address parameters.
3806  * It does the following steps:
3807  * 1. Validates the interface name.
3808  * 2. Verifies that the interface is not an IPMP meta-interface or an
3809  *	underlying interface.
3810  * 3. In case of a persistent operation, verifies that the interface
3811  *	is persistent. Returns error if interface is not enabled but
3812  *	is in persistent config.
3813  * 4. Verifies that the destination address is not set or the address type is
3814  *	not DHCP or ADDRCONF when the interface is a loopback interface.
3815  * 5. Verifies that the address type is not DHCP or ADDRCONF when the interface
3816  *	has IFF_VRRP interface flag set.
3817  */
3818 static ipadm_status_t
3819 i_ipadm_validate_create_addr(ipadm_handle_t iph, ipadm_addrobj_t ipaddr,
3820     uint32_t flags)
3821 {
3822 	sa_family_t		af;
3823 	sa_family_t		other_af;
3824 	char			*ifname;
3825 	ipadm_status_t		status;
3826 	boolean_t		legacy = (iph->iph_flags & IPH_LEGACY);
3827 	boolean_t		islo, isvni;
3828 	uint64_t		ifflags = 0;
3829 	boolean_t		p_exists;
3830 	boolean_t		af_exists, other_af_exists, a_exists;
3831 
3832 	if (ipaddr == NULL || flags == 0 || flags == IPADM_OPT_PERSIST ||
3833 	    (flags & ~(IPADM_COMMON_OPT_MASK|IPADM_OPT_UP|IPADM_OPT_V46))) {
3834 		return (IPADM_INVALID_ARG);
3835 	}
3836 
3837 	if (ipaddr->ipadm_af == AF_UNSPEC)
3838 		return (IPADM_BAD_ADDR);
3839 
3840 	if (!legacy && ipaddr->ipadm_lifnum != 0)
3841 		return (IPADM_INVALID_ARG);
3842 
3843 	if (legacy && ipaddr->ipadm_atype != IPADM_ADDR_STATIC)
3844 		return (IPADM_NOTSUP);
3845 
3846 	ifname = ipaddr->ipadm_ifname;
3847 
3848 	/*
3849 	 * Do not go further when we are under ipmp.
3850 	 * The interface is plumbed up and we are going to add
3851 	 * NOFAILOVER address to make in.mpathd happy.
3852 	 */
3853 	if (i_ipadm_is_under_ipmp(iph, ifname))
3854 		return (IPADM_SUCCESS);
3855 
3856 	af = ipaddr->ipadm_af;
3857 	af_exists = ipadm_if_enabled(iph, ifname, af);
3858 	/*
3859 	 * For legacy case, interfaces are not implicitly plumbed. We need to
3860 	 * check if the interface exists in the active configuration.
3861 	 */
3862 	if (legacy && !af_exists)
3863 		return (IPADM_ENXIO);
3864 
3865 	other_af = (af == AF_INET ? AF_INET6 : AF_INET);
3866 	other_af_exists = ipadm_if_enabled(iph, ifname, other_af);
3867 	/*
3868 	 * Check if one of the v4 or the v6 interfaces exists in the
3869 	 * active configuration. An interface is considered disabled only
3870 	 * if both v4 and v6 are not active.
3871 	 */
3872 	a_exists = (af_exists || other_af_exists);
3873 
3874 	/* Check if interface exists in the persistent configuration. */
3875 	status = i_ipadm_if_pexists(iph, ifname, af, &p_exists);
3876 	if (status != IPADM_SUCCESS)
3877 		return (status);
3878 
3879 	if (!a_exists && p_exists)
3880 		return (IPADM_OP_DISABLE_OBJ);
3881 
3882 	if (af_exists) {
3883 		status = i_ipadm_get_flags(iph, ifname, af, &ifflags);
3884 		if (status != IPADM_SUCCESS)
3885 			return (status);
3886 	}
3887 
3888 	/* Perform validation steps (4) and (5) */
3889 	islo = i_ipadm_is_loopback(ifname);
3890 	isvni = i_ipadm_is_vni(ifname);
3891 	switch (ipaddr->ipadm_atype) {
3892 	case IPADM_ADDR_STATIC:
3893 		if ((islo || isvni) && ipaddr->ipadm_static_dname[0] != '\0')
3894 			return (IPADM_INVALID_ARG);
3895 		/* Check for a valid src address */
3896 		if (!legacy && sockaddrunspec(
3897 		    (struct sockaddr *)&ipaddr->ipadm_static_addr))
3898 			return (IPADM_BAD_ADDR);
3899 		break;
3900 	case IPADM_ADDR_DHCP:
3901 		if (islo || (ifflags & IFF_VRRP))
3902 			return (IPADM_NOTSUP);
3903 		break;
3904 	case IPADM_ADDR_IPV6_ADDRCONF:
3905 		if (islo || (ifflags & IFF_VRRP) ||
3906 		    i_ipadm_is_6to4(iph, ifname)) {
3907 			return (IPADM_NOTSUP);
3908 		}
3909 		break;
3910 	default:
3911 		return (IPADM_INVALID_ARG);
3912 	}
3913 
3914 	return (IPADM_SUCCESS);
3915 }
3916 
3917 ipadm_status_t
3918 i_ipadm_merge_addrprops_from_nvl(nvlist_t *invl, nvlist_t *onvl,
3919     const char *aobjname)
3920 {
3921 	const char * const	ADDRPROPS[] =
3922 	    { IPADM_NVP_PREFIXLEN, IPADM_NVP_REQHOST };
3923 	const size_t		ADDRPROPSLEN =
3924 	    sizeof (ADDRPROPS) / sizeof (*ADDRPROPS);
3925 	nvpair_t	*nvp, *propnvp;
3926 	nvlist_t	*tnvl;
3927 	char		*aname;
3928 	const char	*propname;
3929 	size_t		i;
3930 	int		err;
3931 
3932 	for (i = 0; i < ADDRPROPSLEN; ++i) {
3933 		propname = ADDRPROPS[i];
3934 
3935 		for (nvp = nvlist_next_nvpair(invl, NULL); nvp != NULL;
3936 		    nvp = nvlist_next_nvpair(invl, nvp)) {
3937 			if (nvpair_value_nvlist(nvp, &tnvl) == 0 &&
3938 			    nvlist_exists(tnvl, propname) &&
3939 			    nvlist_lookup_string(tnvl, IPADM_NVP_AOBJNAME,
3940 			    &aname) == 0 && strcmp(aname, aobjname) == 0) {
3941 
3942 				/*
3943 				 * property named `propname' exists for given
3944 				 * aobj
3945 				 */
3946 				(void) nvlist_lookup_nvpair(tnvl, propname,
3947 				    &propnvp);
3948 				err = nvlist_add_nvpair(onvl, propnvp);
3949 				if (err == 0) {
3950 					err = nvlist_remove(invl,
3951 					    nvpair_name(nvp), nvpair_type(nvp));
3952 				}
3953 				if (err != 0)
3954 					return (ipadm_errno2status(err));
3955 				break;
3956 			}
3957 		}
3958 	}
3959 	return (IPADM_SUCCESS);
3960 }
3961 
3962 /*
3963  * Re-enables the address object `aobjname' based on the saved
3964  * configuration for `aobjname'.
3965  */
3966 ipadm_status_t
3967 ipadm_enable_addr(ipadm_handle_t iph, const char *aobjname, uint32_t flags)
3968 {
3969 	nvlist_t	*addrnvl, *nvl;
3970 	nvpair_t	*nvp;
3971 	ipadm_status_t	status;
3972 	struct ipadm_addrobj_s ipaddr;
3973 
3974 	/* check for solaris.network.interface.config authorization */
3975 	if (!ipadm_check_auth())
3976 		return (IPADM_EAUTH);
3977 
3978 	/* validate input */
3979 	if (flags & IPADM_OPT_PERSIST)
3980 		return (IPADM_NOTSUP);
3981 	if (aobjname == NULL || strlcpy(ipaddr.ipadm_aobjname, aobjname,
3982 	    IPADM_AOBJSIZ) >= IPADM_AOBJSIZ) {
3983 		return (IPADM_INVALID_ARG);
3984 	}
3985 
3986 	/* Retrieve the address object information. */
3987 	status = i_ipadm_get_addrobj(iph, &ipaddr);
3988 	if (status != IPADM_SUCCESS)
3989 		return (status);
3990 	if (ipaddr.ipadm_flags & IPMGMT_ACTIVE)
3991 		return (IPADM_ADDROBJ_EXISTS);
3992 
3993 	status = i_ipadm_get_db_addr(iph, NULL, aobjname, &addrnvl);
3994 	if (status != IPADM_SUCCESS)
3995 		return (status);
3996 
3997 	assert(addrnvl != NULL);
3998 
3999 	for (nvp = nvlist_next_nvpair(addrnvl, NULL); nvp != NULL;
4000 	    nvp = nvlist_next_nvpair(addrnvl, nvp)) {
4001 		boolean_t set_init = B_FALSE;
4002 
4003 		if (nvpair_value_nvlist(nvp, &nvl) != 0)
4004 			continue;
4005 
4006 		if (nvlist_exists(nvl, IPADM_NVP_IPV4ADDR) ||
4007 		    nvlist_exists(nvl, IPADM_NVP_IPV6ADDR) ||
4008 		    nvlist_exists(nvl, IPADM_NVP_DHCP)) {
4009 			status = i_ipadm_merge_addrprops_from_nvl(addrnvl, nvl,
4010 			    aobjname);
4011 			if (status != IPADM_SUCCESS)
4012 				continue;
4013 		}
4014 
4015 		/*
4016 		 * ipadm_enable_addr() is never a persistent operation. We need
4017 		 * to set IPH_INIT because ipmgmtd daemon does not have to write
4018 		 * the address to the persistent db. The address is already
4019 		 * available in the persistent db and we are here to re-enable
4020 		 * the persistent configuration.
4021 		 *
4022 		 * But we need to make sure we're not accidentally clearing an
4023 		 * IPH_INIT flag that was already set when we were called.
4024 		 */
4025 		if ((iph->iph_flags & IPH_INIT) == 0) {
4026 			iph->iph_flags |= IPH_INIT;
4027 			set_init = B_TRUE;
4028 		}
4029 
4030 		status = i_ipadm_init_addrobj(iph, nvl);
4031 
4032 		if (set_init)
4033 			iph->iph_flags &= ~IPH_INIT;
4034 
4035 		if (status != IPADM_SUCCESS)
4036 			break;
4037 	}
4038 
4039 	nvlist_free(addrnvl);
4040 	return (status);
4041 }
4042 
4043 /*
4044  * Disables the address object in `aobjname' from the active configuration.
4045  * Error code return values follow the model in ipadm_delete_addr().
4046  */
4047 ipadm_status_t
4048 ipadm_disable_addr(ipadm_handle_t iph, const char *aobjname, uint32_t flags)
4049 {
4050 	/* validate input */
4051 	if (flags & IPADM_OPT_PERSIST)
4052 		return (IPADM_NOTSUP);
4053 
4054 	return (ipadm_delete_addr(iph, aobjname, IPADM_OPT_ACTIVE));
4055 }
4056