xref: /illumos-gate/usr/src/cmd/cmd-inet/lib/ipmgmtd/ipmgmt_door.c (revision c0dd49bdd68c0d758a67d56f07826f3b45cfc664)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Main door handler functions used by ipmgmtd to process the different door
29  * call requests, issued by the library libipadm.so.
30  */
31 
32 #include <alloca.h>
33 #include <pwd.h>
34 #include <auth_attr.h>
35 #include <secdb.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <strings.h>
40 #include <errno.h>
41 #include <assert.h>
42 #include <libnvpair.h>
43 #include "ipmgmt_impl.h"
44 
45 /* Handler declaration for each door command */
46 typedef void ipmgmt_door_handler_t(void *argp);
47 
48 static ipmgmt_door_handler_t	ipmgmt_getaddr_handler,
49 				ipmgmt_getprop_handler,
50 				ipmgmt_getif_handler,
51 				ipmgmt_initif_handler,
52 				ipmgmt_aobjop_handler,
53 				ipmgmt_resetaddr_handler,
54 				ipmgmt_setif_handler,
55 				ipmgmt_resetif_handler,
56 				ipmgmt_resetprop_handler,
57 				ipmgmt_setaddr_handler,
58 				ipmgmt_setprop_handler;
59 
60 typedef struct ipmgmt_door_info_s {
61 	uint_t			idi_cmd;
62 	boolean_t		idi_set;
63 	ipmgmt_door_handler_t	*idi_handler;
64 } ipmgmt_door_info_t;
65 
66 /* maps door commands to door handler functions */
67 static ipmgmt_door_info_t i_ipmgmt_door_info_tbl[] = {
68 	{ IPMGMT_CMD_SETPROP,		B_TRUE,  ipmgmt_setprop_handler },
69 	{ IPMGMT_CMD_SETIF,		B_TRUE,  ipmgmt_setif_handler },
70 	{ IPMGMT_CMD_SETADDR,		B_TRUE,  ipmgmt_setaddr_handler },
71 	{ IPMGMT_CMD_GETPROP,		B_FALSE, ipmgmt_getprop_handler },
72 	{ IPMGMT_CMD_GETIF,		B_FALSE, ipmgmt_getif_handler },
73 	{ IPMGMT_CMD_GETADDR,		B_FALSE, ipmgmt_getaddr_handler },
74 	{ IPMGMT_CMD_RESETIF,		B_TRUE,  ipmgmt_resetif_handler },
75 	{ IPMGMT_CMD_RESETADDR,		B_TRUE,  ipmgmt_resetaddr_handler },
76 	{ IPMGMT_CMD_RESETPROP,		B_TRUE,  ipmgmt_resetprop_handler },
77 	{ IPMGMT_CMD_INITIF,		B_TRUE,  ipmgmt_initif_handler },
78 	{ IPMGMT_CMD_ADDROBJ_LOOKUPADD,	B_TRUE,  ipmgmt_aobjop_handler },
79 	{ IPMGMT_CMD_ADDROBJ_ADD,	B_TRUE,  ipmgmt_aobjop_handler },
80 	{ IPMGMT_CMD_AOBJNAME2ADDROBJ,	B_FALSE, ipmgmt_aobjop_handler },
81 	{ IPMGMT_CMD_LIF2ADDROBJ,	B_FALSE, ipmgmt_aobjop_handler },
82 	{ 0, 0, NULL },
83 };
84 
85 /*
86  * The main server procedure function that gets invoked for any of the incoming
87  * door commands. Inside this function we identify the incoming command and
88  * invoke the right door handler function.
89  */
90 /* ARGSUSED */
91 void
92 ipmgmt_handler(void *cookie, char *argp, size_t argsz, door_desc_t *dp,
93     uint_t n_desc)
94 {
95 	ipmgmt_door_info_t	*infop = NULL;
96 	ipmgmt_retval_t		retval;
97 	int			i;
98 	uint_t			err;
99 	ucred_t			*cred = NULL;
100 
101 	for (i = 0; i_ipmgmt_door_info_tbl[i].idi_cmd != 0; i++) {
102 		if (i_ipmgmt_door_info_tbl[i].idi_cmd ==
103 		    ((ipmgmt_arg_t *)(void *)argp)->ia_cmd) {
104 			infop = &i_ipmgmt_door_info_tbl[i];
105 			break;
106 		}
107 	}
108 
109 	if (infop == NULL) {
110 		ipmgmt_log(LOG_ERR, "Invalid door command specified");
111 		err = EINVAL;
112 		goto fail;
113 	}
114 
115 	/* check for solaris.network.interface.config authorization */
116 	if (infop->idi_set) {
117 		uid_t		uid;
118 		struct passwd	pwd;
119 		char		buf[1024];
120 
121 		if (door_ucred(&cred) != 0) {
122 			err = errno;
123 			ipmgmt_log(LOG_ERR, "Could not get user credentials.");
124 			goto fail;
125 		}
126 		uid = ucred_getruid(cred);
127 		if ((int)uid < 0) {
128 			err = errno;
129 			ipmgmt_log(LOG_ERR, "Could not get user id.");
130 			goto fail;
131 		}
132 		if (getpwuid_r(uid, &pwd, buf, sizeof (buf)) ==
133 		    NULL) {
134 			err = errno;
135 			ipmgmt_log(LOG_ERR, "Could not get password entry.");
136 			goto fail;
137 		}
138 		if (chkauthattr(NETWORK_INTERFACE_CONFIG_AUTH,
139 		    pwd.pw_name) != 1) {
140 			err = EPERM;
141 			ipmgmt_log(LOG_ERR, "Not authorized for operation.");
142 			goto fail;
143 		}
144 		ucred_free(cred);
145 	}
146 
147 	/* individual handlers take care of calling door_return */
148 	infop->idi_handler((void *)argp);
149 	return;
150 fail:
151 	ucred_free(cred);
152 	retval.ir_err = err;
153 	(void) door_return((char *)&retval, sizeof (retval), NULL, 0);
154 }
155 
156 /*
157  * Handles the door command IPMGMT_CMD_GETPROP. It retrieves the persisted
158  * property value for the given property.
159  */
160 static void
161 ipmgmt_getprop_handler(void *argp)
162 {
163 	ipmgmt_prop_arg_t	*pargp = argp;
164 	ipmgmt_getprop_rval_t	rval, *rvalp = &rval;
165 
166 	assert(pargp->ia_cmd == IPMGMT_CMD_GETPROP);
167 
168 	rvalp->ir_err = ipmgmt_db_walk(ipmgmt_db_getprop, pargp, IPADM_DB_READ);
169 	if (rvalp->ir_err == 0)
170 		(void) strlcpy(rvalp->ir_pval, pargp->ia_pval,
171 		    sizeof (rvalp->ir_pval));
172 	(void) door_return((char *)rvalp, sizeof (*rvalp), NULL, 0);
173 }
174 
175 /*
176  * Handles the door command IPMGMT_CMD_SETPROP. It persists the property value
177  * for the given property in the DB.
178  */
179 static void
180 ipmgmt_setprop_handler(void *argp)
181 {
182 	ipmgmt_prop_arg_t	*pargp = argp;
183 	ipmgmt_retval_t		rval;
184 	ipadm_dbwrite_cbarg_t	cb;
185 	nvlist_t		*nvl = NULL;
186 	int			err;
187 
188 	assert(pargp->ia_cmd == IPMGMT_CMD_SETPROP);
189 
190 	if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0)) != 0)
191 		goto fail;
192 	if (pargp->ia_module[0] != '\0' &&
193 	    (err = nvlist_add_string(nvl, IPADM_NVP_PROTONAME,
194 	    pargp->ia_module)) != 0) {
195 		goto fail;
196 	}
197 	if (pargp->ia_ifname[0] != '\0' &&
198 	    (err = nvlist_add_string(nvl, IPADM_NVP_IFNAME,
199 	    pargp->ia_ifname)) != 0)
200 		goto fail;
201 	if (pargp->ia_aobjname[0] != '\0' &&
202 	    (err = nvlist_add_string(nvl, IPADM_NVP_AOBJNAME,
203 	    pargp->ia_aobjname)) != 0)
204 		goto fail;
205 	if ((err = nvlist_add_string(nvl, pargp->ia_pname,
206 	    pargp->ia_pval)) != 0)
207 		goto fail;
208 
209 	cb.dbw_nvl = nvl;
210 	cb.dbw_flags = pargp->ia_flags;
211 	err = ipmgmt_db_walk(ipmgmt_db_update, &cb, IPADM_DB_WRITE);
212 fail:
213 	nvlist_free(nvl);
214 	rval.ir_err = err;
215 	(void) door_return((char *)&rval, sizeof (rval), NULL, 0);
216 }
217 
218 /*
219  * Helper function for ipmgmt_setaddr_handler().
220  * It converts the nvlist_t, `nvl', to aobjmap node `nodep'.
221  */
222 static int
223 i_ipmgmt_nvl2aobjnode(nvlist_t *nvl, ipmgmt_aobjmap_t *nodep)
224 {
225 	char			*aobjname = NULL, *ifname = NULL;
226 	int32_t			lnum;
227 	nvlist_t		*nvladdr;
228 	struct sockaddr_storage	addr;
229 	uint_t			n;
230 	sa_family_t		af = AF_UNSPEC;
231 	ipadm_addr_type_t	addrtype = IPADM_ADDR_NONE;
232 	int			err = 0;
233 
234 	/*
235 	 * Retrieve all the information needed to build '*nodep' from
236 	 * nvlist_t nvl.
237 	 */
238 	if ((err = nvlist_lookup_string(nvl, IPADM_NVP_AOBJNAME,
239 	    &aobjname)) != 0 ||
240 	    (err = nvlist_lookup_string(nvl, IPADM_NVP_IFNAME, &ifname)) != 0 ||
241 	    (err = nvlist_lookup_int32(nvl, IPADM_NVP_LIFNUM, &lnum)) != 0) {
242 		return (err);
243 	}
244 	if (nvlist_exists(nvl, IPADM_NVP_IPV4ADDR)) {
245 		af = AF_INET;
246 		addrtype = IPADM_ADDR_STATIC;
247 	} else if (nvlist_exists(nvl, IPADM_NVP_DHCP)) {
248 		af = AF_INET;
249 		addrtype = IPADM_ADDR_DHCP;
250 	} else if (nvlist_exists(nvl, IPADM_NVP_IPV6ADDR)) {
251 		af = AF_INET6;
252 		addrtype = IPADM_ADDR_STATIC;
253 	} else if (nvlist_lookup_nvlist(nvl, IPADM_NVP_INTFID, &nvladdr) == 0) {
254 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
255 		uint8_t	*addr6;
256 		uint32_t plen;
257 
258 		af = AF_INET6;
259 		addrtype = IPADM_ADDR_IPV6_ADDRCONF;
260 		if (nvlist_lookup_uint32(nvladdr, IPADM_NVP_PREFIXLEN,
261 		    &plen) != 0)
262 			return (EINVAL);
263 		if (plen != 0) {
264 			if (nvlist_lookup_uint8_array(nvladdr,
265 			    IPADM_NVP_IPNUMADDR, &addr6, &n) != 0)
266 				return (EINVAL);
267 			bcopy(addr6, &sin6->sin6_addr, n);
268 		} else {
269 			bzero(&sin6->sin6_addr, sizeof (sin6->sin6_addr));
270 		}
271 	}
272 
273 	/*
274 	 * populate the `*nodep' with retrieved values.
275 	 */
276 	(void) strlcpy(nodep->am_ifname, ifname, sizeof (nodep->am_ifname));
277 	(void) strlcpy(nodep->am_aobjname, aobjname,
278 	    sizeof (nodep->am_aobjname));
279 	nodep->am_lnum = lnum;
280 	nodep->am_family = af;
281 	nodep->am_atype = addrtype;
282 	if (addrtype == IPADM_ADDR_IPV6_ADDRCONF) {
283 		nodep->am_linklocal = B_TRUE;
284 		nodep->am_ifid = addr;
285 	}
286 	nodep->am_next = NULL;
287 
288 	/*
289 	 * Do not store logical interface number in persistent store as it
290 	 * takes different value on reboot. So remove it from `nvl'.
291 	 */
292 	if (nvlist_exists(nvl, IPADM_NVP_LIFNUM))
293 		(void) nvlist_remove(nvl, IPADM_NVP_LIFNUM, DATA_TYPE_INT32);
294 
295 	return (0);
296 }
297 
298 /*
299  * Handles the door command IPMGMT_CMD_SETADDR. It adds a new address object
300  * node to the list `aobjmap' and then persists the address information in the
301  * DB.
302  */
303 static void
304 ipmgmt_setaddr_handler(void *argp)
305 {
306 	ipmgmt_setaddr_arg_t	*sargp = argp;
307 	ipmgmt_retval_t		rval;
308 	ipmgmt_aobjmap_t	node;
309 	nvlist_t		*nvl = NULL;
310 	char			*nvlbuf;
311 	size_t			nvlsize = sargp->ia_nvlsize;
312 	uint32_t		flags = sargp->ia_flags;
313 	int			err = 0;
314 
315 	nvlbuf = (char *)argp + sizeof (ipmgmt_setaddr_arg_t);
316 	if ((err = nvlist_unpack(nvlbuf, nvlsize, &nvl, NV_ENCODE_NATIVE)) != 0)
317 		goto ret;
318 	if (flags & (IPMGMT_ACTIVE|IPMGMT_INIT)) {
319 		if ((err = i_ipmgmt_nvl2aobjnode(nvl, &node)) != 0)
320 			goto ret;
321 		if (flags & IPMGMT_INIT)
322 			node.am_flags = (IPMGMT_ACTIVE|IPMGMT_PERSIST);
323 		else
324 			node.am_flags = flags;
325 		if ((err = ipmgmt_aobjmap_op(&node, ADDROBJ_ADD)) != 0)
326 			goto ret;
327 	}
328 	if (flags & IPMGMT_PERSIST) {
329 		ipadm_dbwrite_cbarg_t	cb;
330 
331 		cb.dbw_nvl = nvl;
332 		cb.dbw_flags = 0;
333 		err = ipmgmt_db_walk(ipmgmt_db_add, &cb, IPADM_DB_WRITE);
334 	}
335 ret:
336 	nvlist_free(nvl);
337 	rval.ir_err = err;
338 	(void) door_return((char *)&rval, sizeof (rval), NULL, 0);
339 }
340 
341 /*
342  * Handles the door commands that modify the `aobjmap' structure.
343  *
344  * IPMGMT_CMD_ADDROBJ_LOOKUPADD - places a stub address object in `aobjmap'
345  *	after ensuring that the namespace is not taken. If required, also
346  *	generates an `aobjname' for address object for the library to use.
347  * IPMGMT_CMD_ADDROBJ_ADD - add/update address object in `aobjmap'
348  * IPMGMT_CMD_LIF2ADDROBJ - given a logical interface, return address object
349  *	associated with that logical interface.
350  * IPMGMT_CMD_AOBJNAME2ADDROBJ - given an address object name return logical
351  *	interface associated with that address object.
352  */
353 static void
354 ipmgmt_aobjop_handler(void *argp)
355 {
356 	ipmgmt_aobjop_arg_t	*largp = argp;
357 	ipmgmt_retval_t		rval;
358 	ipmgmt_aobjop_rval_t	aobjrval;
359 	void			*rvalp;
360 	size_t			rsize;
361 	ipmgmt_aobjmap_t	node;
362 	int			err = 0;
363 	char			*ifname = largp->ia_ifname;
364 	char			*aobjname = largp->ia_aobjname;
365 	int32_t			lnum = largp->ia_lnum;
366 	sa_family_t		af = largp->ia_family;
367 	ipadm_addr_type_t	atype = largp->ia_atype;
368 	ipmgmt_aobjmap_t	*head;
369 
370 	switch (largp->ia_cmd) {
371 	case IPMGMT_CMD_ADDROBJ_LOOKUPADD:
372 		rsize = sizeof (ipmgmt_aobjop_rval_t);
373 		rvalp = &aobjrval;
374 		bzero(&node, sizeof (node));
375 		(void) strlcpy(node.am_aobjname, aobjname,
376 		    sizeof (node.am_aobjname));
377 		(void) strlcpy(node.am_ifname, ifname,
378 		    sizeof (node.am_ifname));
379 		node.am_family = af;
380 		/* no logical number is associated with this addrobj yet */
381 		node.am_lnum = -1;
382 		/* The address object is not persisted yet. */
383 		node.am_flags = IPMGMT_ACTIVE;
384 		err = ipmgmt_aobjmap_op(&node, ADDROBJ_LOOKUPADD);
385 		if (err == 0) {
386 			(void) strlcpy(aobjrval.ir_aobjname, node.am_aobjname,
387 			    sizeof (aobjrval.ir_aobjname));
388 		}
389 		break;
390 	case IPMGMT_CMD_ADDROBJ_ADD:
391 		rsize = sizeof (ipmgmt_retval_t);
392 		rvalp = &rval;
393 		if (aobjname[0] == '\0' || ifname[0] == '\0' || lnum == -1 ||
394 		    af == AF_UNSPEC) {
395 			err = EINVAL;
396 			break;
397 		}
398 		bzero(&node, sizeof (node));
399 		(void) strlcpy(node.am_aobjname, aobjname,
400 		    sizeof (node.am_aobjname));
401 		(void) strlcpy(node.am_ifname, ifname,
402 		    sizeof (node.am_ifname));
403 		node.am_atype = atype;
404 		node.am_lnum = lnum;
405 		node.am_family = af;
406 		/* The address object is not persisted. */
407 		node.am_flags = IPMGMT_ACTIVE;
408 		err = ipmgmt_aobjmap_op(&node, ADDROBJ_ADD);
409 		break;
410 	case IPMGMT_CMD_AOBJNAME2ADDROBJ:
411 		rsize = sizeof (ipmgmt_aobjop_rval_t);
412 		rvalp = &aobjrval;
413 		bzero(&aobjrval, sizeof (aobjrval));
414 		if (aobjname[0] == '\0') {
415 			err = EINVAL;
416 			break;
417 		}
418 		(void) pthread_rwlock_rdlock(&aobjmap.aobjmap_rwlock);
419 		head = aobjmap.aobjmap_head;
420 		for (; head; head = head->am_next) {
421 			if (strcmp(head->am_aobjname, aobjname) != 0)
422 				continue;
423 			/*
424 			 * For an auto-configured interface, return
425 			 * the lifnum that has the link-local on it.
426 			 * Other logical interfaces were created for
427 			 * prefixes and dhcpv6 addresses and do not
428 			 * have am_ifid set.
429 			 */
430 			if (head->am_atype != IPADM_ADDR_IPV6_ADDRCONF ||
431 			    head->am_linklocal) {
432 				break;
433 			}
434 		}
435 		if (head == NULL) {
436 			err = ENOENT;
437 			(void) pthread_rwlock_unlock(&aobjmap.aobjmap_rwlock);
438 			break;
439 		}
440 		(void) strlcpy(aobjrval.ir_ifname, head->am_ifname,
441 		    sizeof (aobjrval.ir_ifname));
442 		aobjrval.ir_lnum = head->am_lnum;
443 		aobjrval.ir_family = head->am_family;
444 		aobjrval.ir_flags = head->am_flags;
445 		aobjrval.ir_atype = head->am_atype;
446 		if (head->am_atype == IPADM_ADDR_IPV6_ADDRCONF &&
447 		    head->am_linklocal)
448 			aobjrval.ir_ifid = head->am_ifid;
449 		(void) pthread_rwlock_unlock(&aobjmap.aobjmap_rwlock);
450 		break;
451 	case IPMGMT_CMD_LIF2ADDROBJ:
452 		rsize = sizeof (ipmgmt_aobjop_rval_t);
453 		rvalp = &aobjrval;
454 		bzero(&aobjrval, sizeof (aobjrval));
455 		if (ifname[0] == '\0') {
456 			err = EINVAL;
457 			break;
458 		}
459 		(void) pthread_rwlock_rdlock(&aobjmap.aobjmap_rwlock);
460 		head = aobjmap.aobjmap_head;
461 		for (; head; head = head->am_next) {
462 			if (strcmp(head->am_ifname, ifname) == 0 &&
463 			    head->am_lnum == lnum &&
464 			    head->am_family == af) {
465 				break;
466 			}
467 		}
468 		if (head == NULL) {
469 			err = ENOENT;
470 			(void) pthread_rwlock_unlock(&aobjmap.aobjmap_rwlock);
471 			break;
472 		}
473 		(void) strlcpy(aobjrval.ir_aobjname, head->am_aobjname,
474 		    sizeof (aobjrval.ir_aobjname));
475 		aobjrval.ir_atype = head->am_atype;
476 		aobjrval.ir_flags = head->am_flags;
477 		(void) pthread_rwlock_unlock(&aobjmap.aobjmap_rwlock);
478 		break;
479 	default:
480 		rsize = sizeof (ipmgmt_retval_t);
481 		rvalp = &rval;
482 		err = EINVAL;
483 	}
484 	((ipmgmt_retval_t *)rvalp)->ir_err = err;
485 	(void) door_return((char *)rvalp, rsize, NULL, 0);
486 }
487 
488 /*
489  * Given an interface name and family, deletes all the address objects
490  * associated with it.
491  */
492 void
493 i_ipmgmt_delif_aobjs(char *ifname, sa_family_t af, uint32_t flags)
494 {
495 	ipmgmt_aobjmap_t	*head, *next, *prev;
496 	ipadm_db_op_t		db_op;
497 
498 	prev = NULL;
499 
500 	(void) pthread_rwlock_wrlock(&aobjmap.aobjmap_rwlock);
501 	head = aobjmap.aobjmap_head;
502 	for (; head; head = next) {
503 		next = head->am_next;
504 		if (strcmp(head->am_ifname, ifname) != 0 ||
505 		    head->am_family != af) {
506 			prev = head;
507 			continue;
508 		}
509 
510 		if (head->am_flags == (IPMGMT_ACTIVE|IPMGMT_PERSIST) &&
511 		    flags == IPMGMT_ACTIVE) {
512 			/*
513 			 * If the addres is present in both active and
514 			 * persistent store, and if we are performing
515 			 * a temporary delete, we update the node to
516 			 * indicate that the address is only present in
517 			 * persistent store and we proceed. Otherwise
518 			 * we always delete the node from aobjmap.
519 			 */
520 			head->am_flags &= ~IPMGMT_ACTIVE;
521 			head->am_lnum = -1;
522 			db_op = IPADM_DB_WRITE;
523 		} else {
524 			db_op = IPADM_DB_DELETE;
525 			if (prev == NULL)
526 				aobjmap.aobjmap_head = next;
527 			else
528 				prev->am_next = next;
529 		}
530 		(void) ipmgmt_persist_aobjmap(head, db_op);
531 		if (db_op == IPADM_DB_DELETE)
532 			free(head);
533 	}
534 	(void) pthread_rwlock_unlock(&aobjmap.aobjmap_rwlock);
535 }
536 
537 /*
538  * Handles the door command IPMGMT_CMD_SETIF. It persists the interface
539  * information in the DB.
540  */
541 static void
542 ipmgmt_setif_handler(void *argp)
543 {
544 	ipmgmt_if_arg_t		*sargp = argp;
545 	ipmgmt_retval_t		rval;
546 	ipadm_dbwrite_cbarg_t	cb;
547 	uint32_t		flags = sargp->ia_flags;
548 	nvlist_t		*nvl = NULL;
549 	int			err = 0;
550 	char			strval[IPMGMT_STRSIZE];
551 
552 	if (!(flags & IPMGMT_PERSIST) || sargp->ia_family == AF_UNSPEC ||
553 	    sargp->ia_ifname[0] == '\0') {
554 		err = EINVAL;
555 		goto ret;
556 	}
557 	if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0)) != 0)
558 		goto ret;
559 	if ((err = nvlist_add_string(nvl, IPADM_NVP_IFNAME,
560 	    sargp->ia_ifname)) != 0)
561 		goto ret;
562 	(void) snprintf(strval, IPMGMT_STRSIZE, "%d", sargp->ia_family);
563 	if ((err = nvlist_add_string(nvl, IPADM_NVP_FAMILY, strval)) != 0)
564 		goto ret;
565 	cb.dbw_nvl = nvl;
566 	cb.dbw_flags = 0;
567 	err = ipmgmt_db_walk(ipmgmt_db_add, &cb, IPADM_DB_WRITE);
568 ret:
569 	rval.ir_err = err;
570 	nvlist_free(nvl);
571 	(void) door_return((char *)&rval, sizeof (rval), NULL, 0);
572 }
573 
574 /*
575  * Handles the door command IPMGMT_CMD_RESETIF. For the given interface,
576  * deletes all the persisted interface configuration. It also deletes, from
577  * `aobjmap', all the address objects configured on the given interface.
578  */
579 static void
580 ipmgmt_resetif_handler(void *argp)
581 {
582 	ipmgmt_if_arg_t		*rargp = argp;
583 	ipmgmt_retval_t		rval;
584 	ipmgmt_if_cbarg_t	cbarg;
585 	uint32_t		flags = rargp->ia_flags;
586 	int			err = 0;
587 
588 	cbarg.cb_family = rargp->ia_family;
589 	cbarg.cb_ifname = rargp->ia_ifname;
590 	if (flags & IPMGMT_PERSIST)
591 		err = ipmgmt_db_walk(ipmgmt_db_resetif, &cbarg,
592 		    IPADM_DB_DELETE);
593 
594 	if (flags & IPMGMT_ACTIVE)
595 		i_ipmgmt_delif_aobjs(rargp->ia_ifname, rargp->ia_family,
596 		    flags);
597 
598 	rval.ir_err = err;
599 	(void) door_return((char *)&rval, sizeof (rval), NULL, 0);
600 }
601 
602 /*
603  * Handles the door command IPMGMT_CMD_RESETADDR. For the given addrobj
604  * deletes all the persisted addrobj configuration. It also deletes the
605  * corresponding node, from `aobjmap'.
606  */
607 static void
608 ipmgmt_resetaddr_handler(void *argp)
609 {
610 	ipmgmt_addr_arg_t	*rargp = argp;
611 	ipmgmt_retval_t		rval;
612 	ipmgmt_aobjmap_t	node;
613 	uint32_t		flags = rargp->ia_flags;
614 	int			err = 0;
615 	ipmgmt_resetaddr_cbarg_t cbarg;
616 
617 	cbarg.cb_aobjname = rargp->ia_aobjname;
618 
619 	if (flags & IPMGMT_PERSIST)
620 		err = ipmgmt_db_walk(ipmgmt_db_resetaddr, &cbarg,
621 		    IPADM_DB_DELETE);
622 
623 	if (flags & IPMGMT_ACTIVE) {
624 		bzero(&node, sizeof (node));
625 		(void) strlcpy(node.am_aobjname, rargp->ia_aobjname,
626 		    sizeof (node.am_aobjname));
627 
628 		/*
629 		 * am_lnum is used only for IPv6 autoconf case, since there
630 		 * can be multiple nodes with the same aobjname.
631 		 */
632 		node.am_lnum = rargp->ia_lnum;
633 		node.am_flags = flags;
634 		(void) ipmgmt_aobjmap_op(&node, ADDROBJ_DELETE);
635 	}
636 
637 	rval.ir_err = err;
638 	(void) door_return((char *)&rval, sizeof (rval), NULL, 0);
639 }
640 
641 /*
642  * Handles the door command IPMGMT_CMD_GETADDR. It retrieves the persisted
643  * address for a given `gargp->ia_aobjname'. If it is not defined then it
644  * retrieves all the addresses configured on `gargp->ia_ifname'. The
645  * "ipadm show-addr addrobj" or "ipadm show-addr <ifname>/\*" will call this
646  * handler through library.
647  */
648 static void
649 ipmgmt_getaddr_handler(void *argp)
650 {
651 	size_t			buflen, onvlsize;
652 	char			*buf, *onvlbuf;
653 	ipmgmt_getaddr_arg_t	*gargp = argp;
654 	ipmgmt_getaddr_cbarg_t	cbarg;
655 	ipmgmt_get_rval_t 	rval, *rvalp = &rval;
656 	int			err = 0;
657 
658 	cbarg.cb_ifname = gargp->ia_ifname;
659 	cbarg.cb_aobjname = gargp->ia_aobjname;
660 	cbarg.cb_ocnt = 0;
661 	if (nvlist_alloc(&cbarg.cb_onvl, NV_UNIQUE_NAME, 0) != 0)
662 		goto fail;
663 	err = ipmgmt_db_walk(ipmgmt_db_getaddr, &cbarg, IPADM_DB_READ);
664 	if (err == ENOENT && cbarg.cb_ocnt > 0) {
665 		/*
666 		 * If there is atleast one entry in the nvlist,
667 		 * do not return error.
668 		 */
669 		err = 0;
670 	}
671 	if (err != 0)
672 		goto fail;
673 
674 	if ((err = nvlist_size(cbarg.cb_onvl, &onvlsize,
675 	    NV_ENCODE_NATIVE)) != 0) {
676 		goto fail;
677 	}
678 	buflen = onvlsize + sizeof (ipmgmt_get_rval_t);
679 	/*
680 	 * We cannot use malloc() here because door_return never returns, and
681 	 * memory allocated by malloc() would get leaked. Use alloca() instead.
682 	 */
683 	buf = alloca(buflen);
684 	onvlbuf = buf + sizeof (ipmgmt_get_rval_t);
685 	if ((err = nvlist_pack(cbarg.cb_onvl, &onvlbuf, &onvlsize,
686 	    NV_ENCODE_NATIVE, 0)) != 0) {
687 		goto fail;
688 	}
689 	nvlist_free(cbarg.cb_onvl);
690 	rvalp = (ipmgmt_get_rval_t *)(void *)buf;
691 	rvalp->ir_err = 0;
692 	rvalp->ir_nvlsize = onvlsize;
693 
694 	(void) door_return(buf, buflen, NULL, 0);
695 	return;
696 fail:
697 	nvlist_free(cbarg.cb_onvl);
698 	rvalp->ir_err = err;
699 	(void) door_return((char *)rvalp, sizeof (*rvalp), NULL, 0);
700 }
701 
702 /*
703  * Handles the door command IPMGMT_CMD_RESETPROP. It deletes the property line
704  * from the DB.
705  */
706 static void
707 ipmgmt_resetprop_handler(void *argp)
708 {
709 	ipmgmt_prop_arg_t	*pargp = argp;
710 	ipmgmt_retval_t		rval;
711 
712 	assert(pargp->ia_cmd == IPMGMT_CMD_RESETPROP);
713 
714 	rval.ir_err = ipmgmt_db_walk(ipmgmt_db_resetprop, pargp,
715 	    IPADM_DB_DELETE);
716 	(void) door_return((char *)&rval, sizeof (rval), NULL, 0);
717 }
718 
719 /*
720  * Handles the door command IPMGMT_CMD_GETIF. It retrieves the name of all the
721  * persisted interfaces and the IP protocols (IPv4 or IPv6) they support.
722  */
723 static void
724 ipmgmt_getif_handler(void *argp)
725 {
726 	ipmgmt_getif_arg_t	*getif = argp;
727 	ipmgmt_getif_rval_t	*rvalp;
728 	ipmgmt_retval_t		rval;
729 	ipmgmt_getif_cbarg_t	cbarg;
730 	ipadm_if_info_t		*ifp, *rifp, *curifp;
731 	int			i, err = 0, count = 0;
732 	size_t			rbufsize;
733 
734 	assert(getif->ia_cmd == IPMGMT_CMD_GETIF);
735 
736 	bzero(&cbarg, sizeof (cbarg));
737 	cbarg.cb_ifname = getif->ia_ifname;
738 	err = ipmgmt_db_walk(ipmgmt_db_getif, &cbarg, IPADM_DB_READ);
739 	if (err == ENOENT && cbarg.cb_ifinfo) {
740 		/*
741 		 * If there is atleast one entry in the nvlist,
742 		 * do not return error.
743 		 */
744 		err = 0;
745 	}
746 	if (err != 0) {
747 		rval.ir_err = err;
748 		(void) door_return((char *)&rval, sizeof (rval), NULL, 0);
749 		return;
750 	}
751 
752 	/* allocate sufficient buffer to return the interface info */
753 	for (ifp = cbarg.cb_ifinfo; ifp != NULL; ifp = ifp->ifi_next)
754 		++count;
755 	rbufsize = sizeof (*rvalp) + count * sizeof (*ifp);
756 	rvalp = alloca(rbufsize);
757 	bzero(rvalp, rbufsize);
758 
759 	rvalp->ir_ifcnt = count;
760 	rifp = rvalp->ir_ifinfo;
761 	ifp = cbarg.cb_ifinfo;
762 
763 	/*
764 	 * copy the interface info to buffer allocated on stack. The reason
765 	 * we do this is to avoid memory leak, as door_return() would never
766 	 * return
767 	 */
768 	for (i = 0; i < count; i++) {
769 		rifp = rvalp->ir_ifinfo + i;
770 		(void) bcopy(ifp, rifp, sizeof (*rifp));
771 		rifp->ifi_next = NULL;
772 		curifp = ifp->ifi_next;
773 		free(ifp);
774 		ifp = curifp;
775 	}
776 	rvalp->ir_err = err;
777 	(void) door_return((char *)rvalp, rbufsize, NULL, 0);
778 }
779 
780 /*
781  * Handles the door command IPMGMT_CMD_INITIF. It retrieves all the persisted
782  * interface configuration (interface properties and addresses), for all those
783  * interfaces that need to be initialized.
784  */
785 static void
786 ipmgmt_initif_handler(void *argp)
787 {
788 	ipmgmt_initif_arg_t	*initif = argp;
789 	size_t			buflen, nvlsize;
790 	char			*buf = NULL, *onvlbuf, *invlbuf;
791 	ipmgmt_get_rval_t	rval, *rvalp = &rval;
792 	ipmgmt_initif_cbarg_t	cbarg;
793 	int			err;
794 
795 	assert(initif->ia_cmd == IPMGMT_CMD_INITIF);
796 
797 	bzero(&cbarg, sizeof (cbarg));
798 	invlbuf = (char *)argp + sizeof (ipmgmt_initif_arg_t);
799 	nvlsize = initif->ia_nvlsize;
800 	err = nvlist_unpack(invlbuf, nvlsize, &cbarg.cb_invl, NV_ENCODE_NATIVE);
801 	if (err != 0)
802 		goto fail;
803 
804 	cbarg.cb_family = initif->ia_family;
805 	if (nvlist_alloc(&cbarg.cb_onvl, NV_UNIQUE_NAME, 0) != 0)
806 		goto fail;
807 
808 	err = ipmgmt_db_walk(ipmgmt_db_initif, &cbarg, IPADM_DB_READ);
809 	if (err == ENOENT && cbarg.cb_ocnt > 0) {
810 		/*
811 		 * If there is atleast one entry in the nvlist,
812 		 * do not return error.
813 		 */
814 		err = 0;
815 	}
816 	if (err != 0)
817 		goto fail;
818 
819 	if ((err = nvlist_size(cbarg.cb_onvl, &nvlsize, NV_ENCODE_NATIVE)) != 0)
820 		goto fail;
821 	buflen = nvlsize + sizeof (ipmgmt_get_rval_t);
822 	/*
823 	 * We cannot use malloc() here because door_return never returns, and
824 	 * memory allocated by malloc() would get leaked. Use alloca() instead.
825 	 */
826 	buf = alloca(buflen);
827 	onvlbuf = buf + sizeof (ipmgmt_get_rval_t);
828 	if ((err = nvlist_pack(cbarg.cb_onvl, &onvlbuf, &nvlsize,
829 	    NV_ENCODE_NATIVE, 0)) != 0) {
830 		goto fail;
831 	}
832 	nvlist_free(cbarg.cb_invl);
833 	nvlist_free(cbarg.cb_onvl);
834 	rvalp = (ipmgmt_get_rval_t *)(void *)buf;
835 	rvalp->ir_err = 0;
836 	rvalp->ir_nvlsize = nvlsize;
837 
838 	(void) door_return(buf, buflen, NULL, 0);
839 	return;
840 fail:
841 	nvlist_free(cbarg.cb_invl);
842 	nvlist_free(cbarg.cb_onvl);
843 	rvalp->ir_err = err;
844 	(void) door_return((char *)rvalp, sizeof (*rvalp), NULL, 0);
845 }
846