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