xref: /illumos-gate/usr/src/lib/libipadm/common/libipadm.c (revision b1d7ec75953cd517f5b7c3d9cb427ff8ec5d7d07)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <stropts.h>
33 #include <sys/sockio.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/socket.h>
37 #include <net/route.h>
38 #include <netinet/in.h>
39 #include <inet/ip.h>
40 #include <arpa/inet.h>
41 #include <libintl.h>
42 #include <libdlpi.h>
43 #include <libinetutil.h>
44 #include <libdladm.h>
45 #include <libdllink.h>
46 #include <libdliptun.h>
47 #include <strings.h>
48 #include <zone.h>
49 #include <ctype.h>
50 #include <limits.h>
51 #include <assert.h>
52 #include <netdb.h>
53 #include <pwd.h>
54 #include <auth_attr.h>
55 #include <secdb.h>
56 #include <nss_dbdefs.h>
57 #include "libipadm_impl.h"
58 
59 /* error codes and text description */
60 static struct ipadm_error_info {
61 	ipadm_status_t	error_code;
62 	const char	*error_desc;
63 } ipadm_errors[] = {
64 	{ IPADM_SUCCESS,	"Operation succeeded" },
65 	{ IPADM_FAILURE,	"Operation failed" },
66 	{ IPADM_EAUTH,		"Insufficient user authorizations" },
67 	{ IPADM_EPERM,		"Permission denied" },
68 	{ IPADM_NO_BUFS,	"No buffer space available" },
69 	{ IPADM_NO_MEMORY,	"Insufficient memory" },
70 	{ IPADM_BAD_ADDR,	"Invalid address" },
71 	{ IPADM_BAD_PROTOCOL,	"Incorrect protocol family for operation" },
72 	{ IPADM_DAD_FOUND,	"Duplicate address detected" },
73 	{ IPADM_EXISTS,		"Already exists" },
74 	{ IPADM_IF_EXISTS,	"Interface already exists" },
75 	{ IPADM_ADDROBJ_EXISTS, "Address object already exists" },
76 	{ IPADM_ADDRCONF_EXISTS, "Addrconf already in progress" },
77 	{ IPADM_ENXIO,		"Interface does not exist" },
78 	{ IPADM_GRP_NOTEMPTY,	"IPMP group is not empty" },
79 	{ IPADM_INVALID_ARG,	"Invalid argument provided" },
80 	{ IPADM_INVALID_NAME,	"Invalid name" },
81 	{ IPADM_DLPI_FAILURE,	"Could not open DLPI link" },
82 	{ IPADM_DLADM_FAILURE,	"Datalink does not exist" },
83 	{ IPADM_PROP_UNKNOWN,   "Unknown property" },
84 	{ IPADM_ERANGE,		"Value is outside the allowed range" },
85 	{ IPADM_ESRCH,		"Value does not exist" },
86 	{ IPADM_EOVERFLOW,	"Number of values exceeds the allowed limit" },
87 	{ IPADM_NOTFOUND,	"Object not found" },
88 	{ IPADM_IF_INUSE,	"Interface already in use" },
89 	{ IPADM_ADDR_INUSE,	"Address already in use" },
90 	{ IPADM_BAD_HOSTNAME,	"Hostname maps to multiple IP addresses" },
91 	{ IPADM_ADDR_NOTAVAIL,	"Can't assign requested address" },
92 	{ IPADM_ALL_ADDRS_NOT_ENABLED, "All addresses could not be enabled" },
93 	{ IPADM_NDPD_NOT_RUNNING, "IPv6 autoconf daemon in.ndpd not running" },
94 	{ IPADM_DHCP_START_ERROR, "Could not start dhcpagent" },
95 	{ IPADM_DHCP_IPC_ERROR,	"Could not communicate with dhcpagent" },
96 	{ IPADM_DHCP_IPC_TIMEOUT, "Communication with dhcpagent timed out" },
97 	{ IPADM_TEMPORARY_OBJ,	"Persistent operation on temporary object" },
98 	{ IPADM_IPC_ERROR,	"Could not communicate with ipmgmtd" },
99 	{ IPADM_NOTSUP,		"Operation not supported" },
100 	{ IPADM_OP_DISABLE_OBJ, "Operation not supported on disabled object" },
101 	{ IPADM_EBADE,		"Invalid data exchange with daemon" }
102 };
103 
104 #define	IPADM_NUM_ERRORS	(sizeof (ipadm_errors) / sizeof (*ipadm_errors))
105 
106 ipadm_status_t
107 ipadm_errno2status(int error)
108 {
109 	switch (error) {
110 	case 0:
111 		return (IPADM_SUCCESS);
112 	case ENXIO:
113 		return (IPADM_ENXIO);
114 	case ENOMEM:
115 		return (IPADM_NO_MEMORY);
116 	case ENOBUFS:
117 		return (IPADM_NO_BUFS);
118 	case EINVAL:
119 		return (IPADM_INVALID_ARG);
120 	case EBUSY:
121 		return (IPADM_IF_INUSE);
122 	case EEXIST:
123 		return (IPADM_EXISTS);
124 	case EADDRNOTAVAIL:
125 		return (IPADM_ADDR_NOTAVAIL);
126 	case EADDRINUSE:
127 		return (IPADM_ADDR_INUSE);
128 	case ENOENT:
129 		return (IPADM_NOTFOUND);
130 	case ERANGE:
131 		return (IPADM_ERANGE);
132 	case EPERM:
133 		return (IPADM_EPERM);
134 	case ENOTSUP:
135 	case EOPNOTSUPP:
136 		return (IPADM_NOTSUP);
137 	case EBADF:
138 		return (IPADM_IPC_ERROR);
139 	case EBADE:
140 		return (IPADM_EBADE);
141 	case ESRCH:
142 		return (IPADM_ESRCH);
143 	case EOVERFLOW:
144 		return (IPADM_EOVERFLOW);
145 	default:
146 		return (IPADM_FAILURE);
147 	}
148 }
149 
150 /*
151  * Returns a message string for the given libipadm error status.
152  */
153 const char *
154 ipadm_status2str(ipadm_status_t status)
155 {
156 	int	i;
157 
158 	for (i = 0; i < IPADM_NUM_ERRORS; i++) {
159 		if (status == ipadm_errors[i].error_code)
160 			return (dgettext(TEXT_DOMAIN,
161 			    ipadm_errors[i].error_desc));
162 	}
163 
164 	return (dgettext(TEXT_DOMAIN, "<unknown error>"));
165 }
166 
167 /*
168  * Opens a handle to libipadm.
169  * Possible values for flags:
170  *  IPH_VRRP:	Used by VRRP daemon to set the socket option SO_VRRP.
171  *  IPH_LEGACY:	This is used whenever an application needs to provide a
172  *		logical interface name while creating or deleting
173  *		interfaces and static addresses.
174  *  IPH_INIT:   Used by ipadm_init_prop(), to initialize protocol properties
175  *		on reboot.
176  */
177 ipadm_status_t
178 ipadm_open(ipadm_handle_t *handle, uint32_t flags)
179 {
180 	ipadm_handle_t	iph;
181 	ipadm_status_t	status = IPADM_SUCCESS;
182 	zoneid_t	zoneid;
183 	ushort_t	zflags;
184 	int		on = B_TRUE;
185 
186 	if (handle == NULL)
187 		return (IPADM_INVALID_ARG);
188 	*handle = NULL;
189 
190 	if (flags & ~(IPH_VRRP|IPH_LEGACY|IPH_INIT))
191 		return (IPADM_INVALID_ARG);
192 
193 	if ((iph = calloc(1, sizeof (struct ipadm_handle))) == NULL)
194 		return (IPADM_NO_MEMORY);
195 	iph->iph_sock = -1;
196 	iph->iph_sock6 = -1;
197 	iph->iph_door_fd = -1;
198 	iph->iph_flags = flags;
199 	(void) pthread_mutex_init(&iph->iph_lock, NULL);
200 
201 	if ((iph->iph_sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ||
202 	    (iph->iph_sock6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
203 		goto errnofail;
204 	}
205 
206 	/*
207 	 * We open a handle to libdladm here, to facilitate some daemons (like
208 	 * nwamd) which opens handle to libipadm before devfsadmd installs the
209 	 * right device permissions into the kernel and requires "all"
210 	 * privileges to open DLD_CONTROL_DEV.
211 	 *
212 	 * In a non-global shared-ip zone there will be no DLD_CONTROL_DEV node
213 	 * and dladm_open() will fail. So, we avoid this by not calling
214 	 * dladm_open() for such zones.
215 	 */
216 	zoneid = getzoneid();
217 	if (zoneid != GLOBAL_ZONEID) {
218 		if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &zflags,
219 		    sizeof (zflags)) < 0) {
220 			goto errnofail;
221 		}
222 	}
223 	if ((zoneid == GLOBAL_ZONEID) || (zflags & ZF_NET_EXCL)) {
224 		if (dladm_open(&iph->iph_dlh) != DLADM_STATUS_OK) {
225 			ipadm_close(iph);
226 			return (IPADM_DLADM_FAILURE);
227 		}
228 	} else {
229 		assert(zoneid != GLOBAL_ZONEID);
230 		iph->iph_dlh = NULL;
231 	}
232 	if (flags & IPH_VRRP) {
233 		if (setsockopt(iph->iph_sock6, SOL_SOCKET, SO_VRRP, &on,
234 		    sizeof (on)) < 0 || setsockopt(iph->iph_sock, SOL_SOCKET,
235 		    SO_VRRP, &on, sizeof (on)) < 0) {
236 			goto errnofail;
237 		}
238 	}
239 	*handle = iph;
240 	return (status);
241 
242 errnofail:
243 	status = ipadm_errno2status(errno);
244 	ipadm_close(iph);
245 	return (status);
246 }
247 
248 /*
249  * Closes and frees the libipadm handle.
250  */
251 void
252 ipadm_close(ipadm_handle_t iph)
253 {
254 	if (iph == NULL)
255 		return;
256 	if (iph->iph_sock != -1)
257 		(void) close(iph->iph_sock);
258 	if (iph->iph_sock6 != -1)
259 		(void) close(iph->iph_sock6);
260 	if (iph->iph_door_fd != -1)
261 		(void) close(iph->iph_door_fd);
262 	dladm_close(iph->iph_dlh);
263 	(void) pthread_mutex_destroy(&iph->iph_lock);
264 	free(iph);
265 }
266 
267 /*
268  * Checks if the caller has the authorization to configure network
269  * interfaces.
270  */
271 boolean_t
272 ipadm_check_auth(void)
273 {
274 	struct passwd	pwd;
275 	char		buf[NSS_BUFLEN_PASSWD];
276 
277 	/* get the password entry for the given user ID */
278 	if (getpwuid_r(getuid(), &pwd, buf, sizeof (buf)) == NULL)
279 		return (B_FALSE);
280 
281 	/* check for presence of given authorization */
282 	return (chkauthattr(NETWORK_INTERFACE_CONFIG_AUTH, pwd.pw_name) != 0);
283 }
284 
285 /*
286  * Stores the index value of the interface in `ifname' for the address
287  * family `af' into the buffer pointed to by `index'.
288  */
289 static ipadm_status_t
290 i_ipadm_get_index(ipadm_handle_t iph, const char *ifname, sa_family_t af,
291     int *index)
292 {
293 	struct lifreq	lifr;
294 	int		sock;
295 
296 	bzero(&lifr, sizeof (lifr));
297 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
298 	if (af == AF_INET)
299 		sock = iph->iph_sock;
300 	else
301 		sock = iph->iph_sock6;
302 
303 	if (ioctl(sock, SIOCGLIFINDEX, (caddr_t)&lifr) < 0)
304 		return (ipadm_errno2status(errno));
305 	*index = lifr.lifr_index;
306 
307 	return (IPADM_SUCCESS);
308 }
309 
310 /*
311  * Maximum amount of time (in milliseconds) to wait for Duplicate Address
312  * Detection to complete in the kernel.
313  */
314 #define	DAD_WAIT_TIME		1000
315 
316 /*
317  * Any time that flags are changed on an interface where either the new or the
318  * existing flags have IFF_UP set, we'll get a RTM_NEWADDR message to
319  * announce the new address added and its flag status.
320  * We wait here for that message and look for IFF_UP.
321  * If something's amiss with the kernel, though, we don't wait forever.
322  * (Note that IFF_DUPLICATE is a high-order bit, and we cannot see
323  * it in the routing socket messages.)
324  */
325 static ipadm_status_t
326 i_ipadm_dad_wait(ipadm_handle_t handle, const char *lifname, sa_family_t af,
327     int rtsock)
328 {
329 	struct pollfd	fds[1];
330 	union {
331 		struct if_msghdr ifm;
332 		char buf[1024];
333 	} msg;
334 	int		index;
335 	ipadm_status_t	retv;
336 	uint64_t	flags;
337 	hrtime_t	starttime, now;
338 
339 	fds[0].fd = rtsock;
340 	fds[0].events = POLLIN;
341 	fds[0].revents = 0;
342 
343 	retv = i_ipadm_get_index(handle, lifname, af, &index);
344 	if (retv != IPADM_SUCCESS)
345 		return (retv);
346 
347 	starttime = gethrtime();
348 	for (;;) {
349 		now = gethrtime();
350 		now = (now - starttime) / 1000000;
351 		if (now >= DAD_WAIT_TIME)
352 			break;
353 		if (poll(fds, 1, DAD_WAIT_TIME - (int)now) <= 0)
354 			break;
355 		if (read(rtsock, &msg, sizeof (msg)) <= 0)
356 			break;
357 		if (msg.ifm.ifm_type != RTM_NEWADDR)
358 			continue;
359 		/* Note that ifm_index is just 16 bits */
360 		if (index == msg.ifm.ifm_index && (msg.ifm.ifm_flags & IFF_UP))
361 			return (IPADM_SUCCESS);
362 	}
363 
364 	retv = i_ipadm_get_flags(handle, lifname, af, &flags);
365 	if (retv != IPADM_SUCCESS)
366 		return (retv);
367 	if (flags & IFF_DUPLICATE)
368 		return (IPADM_DAD_FOUND);
369 
370 	return (IPADM_SUCCESS);
371 }
372 
373 /*
374  * Sets the flags `on_flags' and resets the flags `off_flags' for the logical
375  * interface in `lifname'.
376  *
377  * If the new flags value will transition the interface from "down" to "up"
378  * then duplicate address detection is performed by the kernel.  This routine
379  * waits to get the outcome of that test.
380  */
381 ipadm_status_t
382 i_ipadm_set_flags(ipadm_handle_t iph, const char *lifname, sa_family_t af,
383     uint64_t on_flags, uint64_t off_flags)
384 {
385 	struct lifreq	lifr;
386 	uint64_t	oflags;
387 	ipadm_status_t	ret;
388 	int		rtsock = -1;
389 	int		sock, err;
390 
391 	ret = i_ipadm_get_flags(iph, lifname, af, &oflags);
392 	if (ret != IPADM_SUCCESS)
393 		return (ret);
394 
395 	sock = (af == AF_INET ? iph->iph_sock : iph->iph_sock6);
396 
397 	/*
398 	 * Any time flags are changed on an interface that has IFF_UP set,
399 	 * we get a routing socket message.  We care about the status,
400 	 * though, only when the new flags are marked "up."
401 	 */
402 	if (!(oflags & IFF_UP) && (on_flags & IFF_UP))
403 		rtsock = socket(PF_ROUTE, SOCK_RAW, af);
404 
405 	oflags |= on_flags;
406 	oflags &= ~off_flags;
407 	bzero(&lifr, sizeof (lifr));
408 	(void) strlcpy(lifr.lifr_name, lifname, sizeof (lifr.lifr_name));
409 	lifr.lifr_flags = oflags;
410 	if (ioctl(sock, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
411 		err = errno;
412 		if (rtsock != -1)
413 			(void) close(rtsock);
414 		return (ipadm_errno2status(err));
415 	}
416 	if (rtsock == -1) {
417 		return (IPADM_SUCCESS);
418 	} else {
419 		/* Wait for DAD to complete. */
420 		ret = i_ipadm_dad_wait(iph, lifname, af, rtsock);
421 		(void) close(rtsock);
422 		return (ret);
423 	}
424 }
425 
426 /*
427  * Returns the flags value for the logical interface in `lifname'
428  * in the buffer pointed to by `flags'.
429  */
430 ipadm_status_t
431 i_ipadm_get_flags(ipadm_handle_t iph, const char *lifname, sa_family_t af,
432     uint64_t *flags)
433 {
434 	struct lifreq	lifr;
435 	int		sock;
436 
437 	bzero(&lifr, sizeof (lifr));
438 	(void) strlcpy(lifr.lifr_name, lifname, sizeof (lifr.lifr_name));
439 	if (af == AF_INET)
440 		sock = iph->iph_sock;
441 	else
442 		sock = iph->iph_sock6;
443 
444 	if (ioctl(sock, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
445 		return (ipadm_errno2status(errno));
446 	}
447 	*flags = lifr.lifr_flags;
448 
449 	return (IPADM_SUCCESS);
450 }
451 
452 /*
453  * Determines whether or not an interface name represents a loopback
454  * interface, before the interface has been plumbed.
455  * It is assumed that the interface name in `ifname' is of correct format
456  * as verified by ifparse_ifspec().
457  *
458  * Returns: B_TRUE if loopback, B_FALSE if not.
459  */
460 boolean_t
461 i_ipadm_is_loopback(const char *ifname)
462 {
463 	int len = strlen(LOOPBACK_IF);
464 
465 	return (strncmp(ifname, LOOPBACK_IF, len) == 0 &&
466 	    (ifname[len] == '\0' || ifname[len] == IPADM_LOGICAL_SEP));
467 }
468 
469 /*
470  * Determines whether or not an interface name represents a vni
471  * interface, before the interface has been plumbed.
472  * It is assumed that the interface name in `ifname' is of correct format
473  * as verified by ifparse_ifspec().
474  *
475  * Returns: B_TRUE if vni, B_FALSE if not.
476  */
477 boolean_t
478 i_ipadm_is_vni(const char *ifname)
479 {
480 	ifspec_t	ifsp;
481 
482 	return (ifparse_ifspec(ifname, &ifsp) &&
483 	    strcmp(ifsp.ifsp_devnm, "vni") == 0);
484 }
485 
486 /*
487  * Returns B_TRUE if `ifname' is an IP interface on a 6to4 tunnel.
488  */
489 boolean_t
490 i_ipadm_is_6to4(ipadm_handle_t iph, char *ifname)
491 {
492 	dladm_status_t		dlstatus;
493 	datalink_class_t	class;
494 	iptun_params_t		params;
495 	datalink_id_t		linkid;
496 
497 	if (iph->iph_dlh == NULL) {
498 		assert(getzoneid() != GLOBAL_ZONEID);
499 		return (B_FALSE);
500 	}
501 	dlstatus = dladm_name2info(iph->iph_dlh, ifname, &linkid, NULL,
502 	    &class, NULL);
503 	if (dlstatus == DLADM_STATUS_OK && class == DATALINK_CLASS_IPTUN) {
504 		params.iptun_param_linkid = linkid;
505 		dlstatus = dladm_iptun_getparams(iph->iph_dlh, &params,
506 		    DLADM_OPT_ACTIVE);
507 		if (dlstatus == DLADM_STATUS_OK &&
508 		    params.iptun_param_type == IPTUN_TYPE_6TO4) {
509 			return (B_TRUE);
510 		}
511 	}
512 	return (B_FALSE);
513 }
514 
515 /*
516  * Returns B_TRUE if `ifname' represents an IPMP underlying interface.
517  */
518 boolean_t
519 i_ipadm_is_under_ipmp(ipadm_handle_t iph, const char *ifname)
520 {
521 	struct lifreq	lifr;
522 
523 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
524 	if (ioctl(iph->iph_sock, SIOCGLIFGROUPNAME, (caddr_t)&lifr) < 0) {
525 		if (ioctl(iph->iph_sock6, SIOCGLIFGROUPNAME,
526 		    (caddr_t)&lifr) < 0) {
527 			return (B_FALSE);
528 		}
529 	}
530 	return (lifr.lifr_groupname[0] != '\0');
531 }
532 
533 /*
534  * Returns B_TRUE if `ifname' represents an IPMP meta-interface.
535  */
536 boolean_t
537 i_ipadm_is_ipmp(ipadm_handle_t iph, const char *ifname)
538 {
539 	uint64_t flags;
540 
541 	if (i_ipadm_get_flags(iph, ifname, AF_INET, &flags) != IPADM_SUCCESS &&
542 	    i_ipadm_get_flags(iph, ifname, AF_INET6, &flags) != IPADM_SUCCESS)
543 		return (B_FALSE);
544 
545 	return ((flags & IFF_IPMP) != 0);
546 }
547 
548 /*
549  * For a given interface name, ipadm_if_enabled() checks if v4
550  * or v6 or both IP interfaces exist in the active configuration.
551  */
552 boolean_t
553 ipadm_if_enabled(ipadm_handle_t iph, const char *ifname, sa_family_t af)
554 {
555 	struct lifreq	lifr;
556 	int		s4 = iph->iph_sock;
557 	int		s6 = iph->iph_sock6;
558 
559 	bzero(&lifr, sizeof (lifr));
560 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
561 	switch (af) {
562 	case AF_INET:
563 		if (ioctl(s4, SIOCGLIFFLAGS, (caddr_t)&lifr) == 0)
564 			return (B_TRUE);
565 		break;
566 	case AF_INET6:
567 		if (ioctl(s6, SIOCGLIFFLAGS, (caddr_t)&lifr) == 0)
568 			return (B_TRUE);
569 		break;
570 	case AF_UNSPEC:
571 		if (ioctl(s4, SIOCGLIFFLAGS, (caddr_t)&lifr) == 0 ||
572 		    ioctl(s6, SIOCGLIFFLAGS, (caddr_t)&lifr) == 0) {
573 			return (B_TRUE);
574 		}
575 	}
576 	return (B_FALSE);
577 }
578 
579 /*
580  * Apply the interface property by retrieving information from nvl.
581  */
582 static ipadm_status_t
583 i_ipadm_init_ifprop(ipadm_handle_t iph, nvlist_t *nvl)
584 {
585 	nvpair_t	*nvp;
586 	char		*name, *pname = NULL;
587 	char		*protostr = NULL, *ifname = NULL, *pval = NULL;
588 	uint_t		proto;
589 	int		err = 0;
590 
591 	for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
592 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
593 		name = nvpair_name(nvp);
594 		if (strcmp(name, IPADM_NVP_IFNAME) == 0) {
595 			if ((err = nvpair_value_string(nvp, &ifname)) != 0)
596 				break;
597 		} else if (strcmp(name, IPADM_NVP_PROTONAME) == 0) {
598 			if ((err = nvpair_value_string(nvp, &protostr)) != 0)
599 				break;
600 		} else {
601 			assert(!IPADM_PRIV_NVP(name));
602 			pname = name;
603 			if ((err = nvpair_value_string(nvp, &pval)) != 0)
604 				break;
605 		}
606 	}
607 	if (err != 0)
608 		return (ipadm_errno2status(err));
609 	proto = ipadm_str2proto(protostr);
610 	return (ipadm_set_ifprop(iph, ifname, pname, pval, proto,
611 	    IPADM_OPT_ACTIVE));
612 }
613 
614 /*
615  * Instantiate the address object or set the address object property by
616  * retrieving the configuration from the nvlist `nvl'.
617  */
618 ipadm_status_t
619 i_ipadm_init_addrobj(ipadm_handle_t iph, nvlist_t *nvl)
620 {
621 	nvpair_t	*nvp;
622 	char		*name;
623 	char		*aobjname = NULL, *pval = NULL, *ifname = NULL;
624 	sa_family_t	af = AF_UNSPEC;
625 	ipadm_addr_type_t atype = IPADM_ADDR_NONE;
626 	int		err = 0;
627 	ipadm_status_t	status = IPADM_SUCCESS;
628 
629 	for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
630 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
631 		name = nvpair_name(nvp);
632 		if (strcmp(name, IPADM_NVP_IFNAME) == 0) {
633 			if ((err = nvpair_value_string(nvp, &ifname)) != 0)
634 				break;
635 		} else if (strcmp(name, IPADM_NVP_AOBJNAME) == 0) {
636 			if ((err = nvpair_value_string(nvp, &aobjname)) != 0)
637 				break;
638 		} else if (i_ipadm_name2atype(name, &af, &atype)) {
639 			break;
640 		} else {
641 			assert(!IPADM_PRIV_NVP(name));
642 			err = nvpair_value_string(nvp, &pval);
643 			break;
644 		}
645 	}
646 	if (err != 0)
647 		return (ipadm_errno2status(err));
648 
649 	switch (atype) {
650 	case IPADM_ADDR_STATIC:
651 		status = i_ipadm_enable_static(iph, ifname, nvl, af);
652 		break;
653 	case IPADM_ADDR_DHCP:
654 		status = i_ipadm_enable_dhcp(iph, ifname, nvl);
655 		if (status == IPADM_DHCP_IPC_TIMEOUT)
656 			status = IPADM_SUCCESS;
657 		break;
658 	case IPADM_ADDR_IPV6_ADDRCONF:
659 		status = i_ipadm_enable_addrconf(iph, ifname, nvl);
660 		break;
661 	case IPADM_ADDR_NONE:
662 		status = ipadm_set_addrprop(iph, name, pval, aobjname,
663 		    IPADM_OPT_ACTIVE);
664 		break;
665 	}
666 
667 	return (status);
668 }
669 
670 /*
671  * Instantiate the interface object by retrieving the configuration from
672  * `ifnvl'. The nvlist `ifnvl' contains all the persistent configuration
673  * (interface properties and address objects on that interface) for the
674  * given `ifname'.
675  */
676 ipadm_status_t
677 i_ipadm_init_ifobj(ipadm_handle_t iph, const char *ifname, nvlist_t *ifnvl)
678 {
679 	nvlist_t	*nvl = NULL;
680 	nvpair_t	*nvp;
681 	char		*afstr;
682 	ipadm_status_t	status;
683 	ipadm_status_t	ret_status = IPADM_SUCCESS;
684 	char		newifname[LIFNAMSIZ];
685 	char		*aobjstr;
686 
687 	(void) strlcpy(newifname, ifname, sizeof (newifname));
688 	/*
689 	 * First plumb the given interface and then apply all the persistent
690 	 * interface properties and then instantiate any persistent addresses
691 	 * objects on that interface.
692 	 */
693 	for (nvp = nvlist_next_nvpair(ifnvl, NULL); nvp != NULL;
694 	    nvp = nvlist_next_nvpair(ifnvl, nvp)) {
695 		if (nvpair_value_nvlist(nvp, &nvl) != 0)
696 			continue;
697 
698 		if (nvlist_lookup_string(nvl, IPADM_NVP_FAMILY, &afstr) == 0) {
699 			status = i_ipadm_plumb_if(iph, newifname, atoi(afstr),
700 			    IPADM_OPT_ACTIVE);
701 			/*
702 			 * If the interface is already plumbed, we should
703 			 * ignore this error because there might be address
704 			 * address objects on that interface that needs to
705 			 * be enabled again.
706 			 */
707 			if (status == IPADM_IF_EXISTS)
708 				status = IPADM_SUCCESS;
709 		} else if (nvlist_lookup_string(nvl, IPADM_NVP_AOBJNAME,
710 		    &aobjstr) == 0) {
711 			/*
712 			 * For a static address, we need to search for
713 			 * the prefixlen in the nvlist `ifnvl'.
714 			 */
715 			if (nvlist_exists(nvl, IPADM_NVP_IPV4ADDR) ||
716 			    nvlist_exists(nvl, IPADM_NVP_IPV6ADDR)) {
717 				status = i_ipadm_merge_prefixlen_from_nvl(ifnvl,
718 				    nvl, aobjstr);
719 				if (status != IPADM_SUCCESS)
720 					continue;
721 			}
722 			status = i_ipadm_init_addrobj(iph, nvl);
723 			/*
724 			 * If this address is in use on some other interface,
725 			 * we want to record an error to be returned as
726 			 * a soft error and continue processing the rest of
727 			 * the addresses.
728 			 */
729 			if (status == IPADM_ADDR_NOTAVAIL) {
730 				ret_status = IPADM_ALL_ADDRS_NOT_ENABLED;
731 				status = IPADM_SUCCESS;
732 			}
733 		} else {
734 			assert(nvlist_exists(nvl, IPADM_NVP_PROTONAME));
735 			status = i_ipadm_init_ifprop(iph, nvl);
736 		}
737 		if (status != IPADM_SUCCESS)
738 			return (status);
739 	}
740 	return (ret_status);
741 }
742 
743 /*
744  * Retrieves the persistent configuration for the given interface(s) in `ifs'
745  * by contacting the daemon and dumps the information in `allifs'.
746  */
747 ipadm_status_t
748 i_ipadm_init_ifs(ipadm_handle_t iph, const char *ifs, nvlist_t **allifs)
749 {
750 	nvlist_t		*nvl = NULL;
751 	size_t			nvlsize, bufsize;
752 	ipmgmt_initif_arg_t	*iargp;
753 	char			*buf = NULL, *nvlbuf = NULL;
754 	ipmgmt_get_rval_t	*rvalp = NULL;
755 	int			err;
756 	ipadm_status_t		status = IPADM_SUCCESS;
757 
758 	if ((err = ipadm_str2nvlist(ifs, &nvl, IPADM_NORVAL)) != 0)
759 		return (ipadm_errno2status(err));
760 
761 	err = nvlist_pack(nvl, &nvlbuf, &nvlsize, NV_ENCODE_NATIVE, 0);
762 	if (err != 0) {
763 		status = ipadm_errno2status(err);
764 		goto done;
765 	}
766 	bufsize = sizeof (*iargp) + nvlsize;
767 	if ((buf = malloc(bufsize)) == NULL) {
768 		status = ipadm_errno2status(errno);
769 		goto done;
770 	}
771 
772 	/* populate the door_call argument structure */
773 	iargp = (void *)buf;
774 	iargp->ia_cmd = IPMGMT_CMD_INITIF;
775 	iargp->ia_flags = 0;
776 	iargp->ia_family = AF_UNSPEC;
777 	iargp->ia_nvlsize = nvlsize;
778 	(void) bcopy(nvlbuf, buf + sizeof (*iargp), nvlsize);
779 
780 	if ((rvalp = malloc(sizeof (ipmgmt_get_rval_t))) == NULL) {
781 		status = ipadm_errno2status(errno);
782 		goto done;
783 	}
784 	if ((err = ipadm_door_call(iph, iargp, bufsize, (void **)&rvalp,
785 	    sizeof (*rvalp), B_TRUE)) != 0) {
786 		status = ipadm_errno2status(err);
787 		goto done;
788 	}
789 	nvlsize = rvalp->ir_nvlsize;
790 	nvlbuf = (char *)rvalp + sizeof (ipmgmt_get_rval_t);
791 
792 	/*
793 	 * nvlbuf contains a list of nvlists, each of which represents
794 	 * configuration information for the given interface(s)
795 	 */
796 	err = nvlist_unpack(nvlbuf, nvlsize, allifs, NV_ENCODE_NATIVE);
797 	if (err != 0)
798 		status = ipadm_errno2status(err);
799 done:
800 	nvlist_free(nvl);
801 	free(buf);
802 	free(nvlbuf);
803 	free(rvalp);
804 	return (status);
805 }
806 
807 /*
808  * Returns B_FALSE if
809  * (1) `ifname' is NULL or has no string or has a string of invalid length
810  * (2) ifname is a logical interface and IPH_LEGACY is not set, or
811  */
812 boolean_t
813 i_ipadm_validate_ifname(ipadm_handle_t iph, const char *ifname)
814 {
815 	ifspec_t ifsp;
816 
817 	if (ifname == NULL || ifname[0] == '\0' ||
818 	    !ifparse_ifspec(ifname, &ifsp))
819 		return (B_FALSE);
820 	if (ifsp.ifsp_lunvalid)
821 		return (ifsp.ifsp_lun > 0 && (iph->iph_flags & IPH_LEGACY));
822 	return (B_TRUE);
823 }
824 
825 /*
826  * Wrapper for sending a non-transparent I_STR ioctl().
827  * Returns: Result from ioctl().
828  */
829 int
830 i_ipadm_strioctl(int s, int cmd, char *buf, int buflen)
831 {
832 	struct strioctl ioc;
833 
834 	(void) memset(&ioc, 0, sizeof (ioc));
835 	ioc.ic_cmd = cmd;
836 	ioc.ic_timout = 0;
837 	ioc.ic_len = buflen;
838 	ioc.ic_dp = buf;
839 
840 	return (ioctl(s, I_STR, (char *)&ioc));
841 }
842 
843 /*
844  * Make a door call to the server and checks if the door call succeeded or not.
845  * `is_varsize' specifies that the data returned by ipmgmtd daemon is of
846  * variable size and door will allocate buffer using mmap(). In such cases
847  * we re-allocate the required memory,n assign it to `rbufp', copy the data to
848  * `rbufp' and then call munmap() (see below).
849  *
850  * It also checks to see if the server side procedure ran successfully by
851  * checking for ir_err. Therefore, for some callers who just care about the
852  * return status can set `rbufp' to NULL and set `rsize' to 0.
853  */
854 int
855 ipadm_door_call(ipadm_handle_t iph, void *arg, size_t asize, void **rbufp,
856     size_t rsize, boolean_t is_varsize)
857 {
858 	door_arg_t	darg;
859 	int		err;
860 	ipmgmt_retval_t	rval, *rvalp;
861 
862 	if (rbufp == NULL) {
863 		rvalp = &rval;
864 		rbufp = (void **)&rvalp;
865 		rsize = sizeof (rval);
866 	}
867 
868 	darg.data_ptr = arg;
869 	darg.data_size = asize;
870 	darg.desc_ptr = NULL;
871 	darg.desc_num = 0;
872 	darg.rbuf = *rbufp;
873 	darg.rsize = rsize;
874 
875 	(void) pthread_mutex_lock(&iph->iph_lock);
876 	/* The door descriptor is opened if it isn't already */
877 	if (iph->iph_door_fd == -1) {
878 		if ((iph->iph_door_fd = open(IPMGMT_DOOR, O_RDONLY)) < 0) {
879 			err = errno;
880 			(void) pthread_mutex_unlock(&iph->iph_lock);
881 			return (err);
882 		}
883 	}
884 	(void) pthread_mutex_unlock(&iph->iph_lock);
885 
886 	if (door_call(iph->iph_door_fd, &darg) == -1)
887 		return (errno);
888 	err = ((ipmgmt_retval_t *)(void *)(darg.rbuf))->ir_err;
889 	if (darg.rbuf != *rbufp) {
890 		/*
891 		 * if the caller is expecting the result to fit in specified
892 		 * buffer then return failure.
893 		 */
894 		if (!is_varsize)
895 			err = EBADE;
896 		/*
897 		 * The size of the buffer `*rbufp' was not big enough
898 		 * and the door itself allocated buffer, for us. We will
899 		 * hit this, on several occasion as for some cases
900 		 * we cannot predict the size of the return structure.
901 		 * Reallocate the buffer `*rbufp' and memcpy() the contents
902 		 * to new buffer.
903 		 */
904 		if (err == 0) {
905 			void *newp;
906 
907 			/* allocated memory will be freed by the caller */
908 			if ((newp = realloc(*rbufp, darg.rsize)) == NULL) {
909 				err = ENOMEM;
910 			} else {
911 				*rbufp = newp;
912 				(void) memcpy(*rbufp, darg.rbuf, darg.rsize);
913 			}
914 		}
915 		/* munmap() the door buffer */
916 		(void) munmap(darg.rbuf, darg.rsize);
917 	} else {
918 		if (darg.rsize != rsize)
919 			err = EBADE;
920 	}
921 	return (err);
922 }
923