8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / dlmgmtd / dlmgmt_door.c
blob11e432966901cc6eceab2d0100b1e02f520097a1
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
27 * Main door handler functions used by dlmgmtd to process the different door
28 * call requests. Door call requests can come from the user-land applications,
29 * or from the kernel.
31 * Note on zones handling:
33 * There are two zoneid's associated with a link. One is the zoneid of the
34 * zone in which the link was created (ll_zoneid in the dlmgmt_link_t), and
35 * the other is the zoneid of the zone where the link is currently assigned
36 * (the "zone" link property). The two can be different if a datalink is
37 * created in the global zone and subsequently assigned to a non-global zone
38 * via zonecfg or via explicitly setting the "zone" link property.
40 * Door clients can see links that were created in their zone, and links that
41 * are currently assigned to their zone. Door clients in a zone can only
42 * modify links that were created in their zone.
44 * The datalink ID space is global, while each zone has its own datalink name
45 * space. This allows each zone to have complete freedom over the names that
46 * they assign to links created within the zone.
49 #include <assert.h>
50 #include <alloca.h>
51 #include <errno.h>
52 #include <priv_utils.h>
53 #include <stdlib.h>
54 #include <strings.h>
55 #include <syslog.h>
56 #include <sys/sysevent/eventdefs.h>
57 #include <zone.h>
58 #include <libsysevent.h>
59 #include <libdlmgmt.h>
60 #include <librcm.h>
61 #include "dlmgmt_impl.h"
63 typedef void dlmgmt_door_handler_t(void *, void *, size_t *, zoneid_t,
64 ucred_t *);
66 typedef struct dlmgmt_door_info_s {
67 uint_t di_cmd;
68 size_t di_reqsz;
69 size_t di_acksz;
70 dlmgmt_door_handler_t *di_handler;
71 } dlmgmt_door_info_t;
74 * Check if the caller has the required privileges to operate on a link of the
75 * given class.
77 static int
78 dlmgmt_checkprivs(datalink_class_t class, ucred_t *cred)
80 const priv_set_t *eset;
82 eset = ucred_getprivset(cred, PRIV_EFFECTIVE);
83 if (eset != NULL && ((class == DATALINK_CLASS_IPTUN &&
84 priv_ismember(eset, PRIV_SYS_IPTUN_CONFIG)) ||
85 priv_ismember(eset, PRIV_SYS_DL_CONFIG) ||
86 priv_ismember(eset, PRIV_SYS_NET_CONFIG)))
87 return (0);
88 return (EACCES);
91 static dlmgmt_link_t *
92 dlmgmt_getlink_by_dev(char *devname, zoneid_t zoneid)
94 dlmgmt_link_t *linkp = avl_first(&dlmgmt_id_avl);
96 for (; linkp != NULL; linkp = AVL_NEXT(&dlmgmt_id_avl, linkp)) {
97 if (link_is_visible(linkp, zoneid) &&
98 (linkp->ll_class == DATALINK_CLASS_PHYS) &&
99 linkattr_equal(&(linkp->ll_head), FDEVNAME, devname,
100 strlen(devname) + 1)) {
101 return (linkp);
104 return (NULL);
108 * Post the EC_DATALINK sysevent for the given linkid. This sysevent will
109 * be consumed by the datalink sysevent module.
111 static void
112 dlmgmt_post_sysevent(const char *subclass, datalink_id_t linkid,
113 boolean_t reconfigured)
115 nvlist_t *nvl = NULL;
116 sysevent_id_t eid;
117 int err;
119 if (((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0)) != 0) ||
120 ((err = nvlist_add_uint64(nvl, RCM_NV_LINKID, linkid)) != 0) ||
121 ((err = nvlist_add_boolean_value(nvl, RCM_NV_RECONFIGURED,
122 reconfigured)) != 0)) {
123 goto done;
126 if (sysevent_post_event(EC_DATALINK, (char *)subclass, SUNW_VENDOR,
127 (char *)progname, nvl, &eid) == -1) {
128 err = errno;
131 done:
132 if (err != 0) {
133 dlmgmt_log(LOG_WARNING, "dlmgmt_post_sysevent(%d) failed: %s",
134 linkid, strerror(err));
136 nvlist_free(nvl);
139 /* ARGSUSED */
140 static void
141 dlmgmt_upcall_create(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
142 ucred_t *cred)
144 dlmgmt_upcall_arg_create_t *create = argp;
145 dlmgmt_create_retval_t *retvalp = retp;
146 datalink_class_t class;
147 uint32_t media;
148 dlmgmt_link_t *linkp;
149 char link[MAXLINKNAMELEN];
150 uint32_t flags;
151 int err = 0;
152 boolean_t created = B_FALSE;
153 boolean_t reconfigured = B_FALSE;
156 * Determine whether this link is persistent. Note that this request
157 * is coming from kernel so this link must be active.
159 flags = DLMGMT_ACTIVE | (create->ld_persist ? DLMGMT_PERSIST : 0);
161 class = create->ld_class;
162 media = create->ld_media;
165 * Hold the writer lock to update the link table.
167 dlmgmt_table_lock(B_TRUE);
169 if ((err = dlmgmt_checkprivs(class, cred)) != 0)
170 goto done;
173 * Check to see whether this is the reattachment of an existing
174 * physical link. If so, return its linkid.
176 if ((class == DATALINK_CLASS_PHYS) && (linkp =
177 dlmgmt_getlink_by_dev(create->ld_devname, zoneid)) != NULL) {
178 if (linkattr_equal(&(linkp->ll_head), FPHYMAJ,
179 &create->ld_phymaj, sizeof (uint64_t)) &&
180 linkattr_equal(&(linkp->ll_head), FPHYINST,
181 &create->ld_phyinst, sizeof (uint64_t)) &&
182 (linkp->ll_flags & flags) == flags) {
184 * If nothing has been changed, directly return.
186 goto noupdate;
189 err = linkattr_set(&(linkp->ll_head), FPHYMAJ,
190 &create->ld_phymaj, sizeof (uint64_t), DLADM_TYPE_UINT64);
191 if (err != 0)
192 goto done;
194 err = linkattr_set(&(linkp->ll_head), FPHYINST,
195 &create->ld_phyinst, sizeof (uint64_t), DLADM_TYPE_UINT64);
196 if (err != 0)
197 goto done;
200 * This is a device that is dynamic reconfigured.
202 if ((linkp->ll_flags & DLMGMT_ACTIVE) == 0)
203 reconfigured = B_TRUE;
205 if ((err = link_activate(linkp)) != 0)
206 goto done;
207 linkp->ll_flags |= flags;
208 linkp->ll_gen++;
210 goto done;
213 if ((err = dlmgmt_create_common(create->ld_devname, class, media,
214 zoneid, flags, &linkp)) == EEXIST) {
216 * The link name already exists. Return error if this is a
217 * non-physical link (in that case, the link name must be
218 * the same as the given name).
220 if (class != DATALINK_CLASS_PHYS)
221 goto done;
224 * The physical link's name already exists, request
225 * a suggested link name: net<nextppa>
227 err = dlmgmt_generate_name("net", link, MAXLINKNAMELEN, zoneid);
228 if (err != 0)
229 goto done;
231 err = dlmgmt_create_common(link, class, media, zoneid, flags,
232 &linkp);
235 if (err != 0)
236 goto done;
238 created = B_TRUE;
241 * This is a new link. Only need to persist link attributes for
242 * physical links.
244 if (class == DATALINK_CLASS_PHYS &&
245 (((err = linkattr_set(&linkp->ll_head, FDEVNAME, create->ld_devname,
246 strlen(create->ld_devname) + 1, DLADM_TYPE_STR)) != 0) ||
247 ((err = linkattr_set(&linkp->ll_head, FPHYMAJ, &create->ld_phymaj,
248 sizeof (uint64_t), DLADM_TYPE_UINT64)) != 0) ||
249 ((err = linkattr_set(&linkp->ll_head, FPHYINST, &create->ld_phyinst,
250 sizeof (uint64_t), DLADM_TYPE_UINT64)) != 0))) {
251 (void) dlmgmt_destroy_common(linkp, flags);
254 done:
255 if ((err == 0) && ((err = dlmgmt_write_db_entry(linkp->ll_link, linkp,
256 linkp->ll_flags)) != 0) && created) {
257 (void) dlmgmt_destroy_common(linkp, flags);
260 noupdate:
261 if (err == 0)
262 retvalp->lr_linkid = linkp->ll_linkid;
264 dlmgmt_table_unlock();
266 if ((err == 0) && (class == DATALINK_CLASS_PHYS)) {
268 * Post the ESC_DATALINK_PHYS_ADD sysevent. This sysevent
269 * is consumed by the datalink sysevent module which in
270 * turn generates the RCM_RESOURCE_LINK_NEW RCM event.
272 dlmgmt_post_sysevent(ESC_DATALINK_PHYS_ADD,
273 retvalp->lr_linkid, reconfigured);
276 retvalp->lr_err = err;
279 /* ARGSUSED */
280 static void
281 dlmgmt_upcall_update(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
282 ucred_t *cred)
284 dlmgmt_upcall_arg_update_t *update = argp;
285 dlmgmt_update_retval_t *retvalp = retp;
286 uint32_t media = update->ld_media;
287 dlmgmt_link_t *linkp;
288 int err = 0;
291 * Hold the writer lock to update the link table.
293 dlmgmt_table_lock(B_TRUE);
296 * Check to see whether this is the reattachment of an existing
297 * physical link. If so, return its linkid.
299 if ((linkp = dlmgmt_getlink_by_dev(update->ld_devname, zoneid)) ==
300 NULL) {
301 err = ENOENT;
302 goto done;
305 if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
306 goto done;
308 retvalp->lr_linkid = linkp->ll_linkid;
309 retvalp->lr_media = media;
310 if (linkp->ll_media != media && linkp->ll_media != DL_OTHER) {
312 * Assume a DL_ETHER link ce0, a DL_WIFI link ath0
313 * 1. # dladm rename-link ce0 net0
314 * 2. DR out ce0. net0 is down.
315 * 3. use rename-link to have the ath0 device inherit
316 * the configuration from net0
317 * # dladm rename-link ath0 net0
318 * 4. DR in ath0.
319 * As ath0 and ce0 do not have the same media type, ath0
320 * cannot inherit the configuration of net0.
322 err = EEXIST;
325 * Return the media type of the existing link to indicate the
326 * reason for the name conflict.
328 retvalp->lr_media = linkp->ll_media;
329 goto done;
332 if (update->ld_novanity &&
333 (strcmp(update->ld_devname, linkp->ll_link) != 0)) {
335 * Return an error if this is a physical link that does not
336 * support vanity naming, but the link name is not the same
337 * as the given device name.
339 err = EEXIST;
340 goto done;
343 if (linkp->ll_media != media) {
344 linkp->ll_media = media;
345 linkp->ll_gen++;
346 (void) dlmgmt_write_db_entry(linkp->ll_link, linkp,
347 linkp->ll_flags);
350 done:
351 dlmgmt_table_unlock();
352 retvalp->lr_err = err;
355 /* ARGSUSED */
356 static void
357 dlmgmt_upcall_destroy(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
358 ucred_t *cred)
360 dlmgmt_upcall_arg_destroy_t *destroy = argp;
361 dlmgmt_destroy_retval_t *retvalp = retp;
362 datalink_id_t linkid = destroy->ld_linkid;
363 dlmgmt_link_t *linkp = NULL;
364 uint32_t flags, dflags = 0;
365 int err = 0;
367 flags = DLMGMT_ACTIVE | (destroy->ld_persist ? DLMGMT_PERSIST : 0);
370 * Hold the writer lock to update the link table.
372 dlmgmt_table_lock(B_TRUE);
374 if ((linkp = link_by_id(linkid, zoneid)) == NULL) {
375 err = ENOENT;
376 goto done;
379 if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
380 goto done;
382 if (((linkp->ll_flags & flags) & DLMGMT_ACTIVE) != 0) {
383 if ((err = dlmgmt_delete_db_entry(linkp, DLMGMT_ACTIVE)) != 0)
384 goto done;
385 dflags |= DLMGMT_ACTIVE;
388 if (((linkp->ll_flags & flags) & DLMGMT_PERSIST) != 0) {
389 if ((err = dlmgmt_delete_db_entry(linkp, DLMGMT_PERSIST)) != 0)
390 goto done;
391 dflags |= DLMGMT_PERSIST;
394 err = dlmgmt_destroy_common(linkp, flags);
395 done:
396 if (err != 0 && dflags != 0)
397 (void) dlmgmt_write_db_entry(linkp->ll_link, linkp, dflags);
399 dlmgmt_table_unlock();
400 retvalp->lr_err = err;
403 /* ARGSUSED */
404 static void
405 dlmgmt_getname(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
406 ucred_t *cred)
408 dlmgmt_door_getname_t *getname = argp;
409 dlmgmt_getname_retval_t *retvalp = retp;
410 dlmgmt_link_t *linkp;
411 int err = 0;
414 * Hold the reader lock to access the link
416 dlmgmt_table_lock(B_FALSE);
417 if ((linkp = link_by_id(getname->ld_linkid, zoneid)) == NULL) {
418 err = ENOENT;
419 } else if (strlcpy(retvalp->lr_link, linkp->ll_link, MAXLINKNAMELEN) >=
420 MAXLINKNAMELEN) {
421 err = ENOSPC;
422 } else {
423 retvalp->lr_flags = linkp->ll_flags;
424 retvalp->lr_class = linkp->ll_class;
425 retvalp->lr_media = linkp->ll_media;
428 dlmgmt_table_unlock();
429 retvalp->lr_err = err;
432 /* ARGSUSED */
433 static void
434 dlmgmt_getlinkid(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
435 ucred_t *cred)
437 dlmgmt_door_getlinkid_t *getlinkid = argp;
438 dlmgmt_getlinkid_retval_t *retvalp = retp;
439 dlmgmt_link_t *linkp;
440 int err = 0;
443 * Hold the reader lock to access the link
445 dlmgmt_table_lock(B_FALSE);
447 if ((linkp = link_by_name(getlinkid->ld_link, zoneid)) == NULL) {
449 * The link does not exist in this zone.
451 err = ENOENT;
452 goto done;
455 retvalp->lr_linkid = linkp->ll_linkid;
456 retvalp->lr_flags = linkp->ll_flags;
457 retvalp->lr_class = linkp->ll_class;
458 retvalp->lr_media = linkp->ll_media;
460 done:
461 dlmgmt_table_unlock();
462 retvalp->lr_err = err;
465 /* ARGSUSED */
466 static void
467 dlmgmt_getnext(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
468 ucred_t *cred)
470 dlmgmt_door_getnext_t *getnext = argp;
471 dlmgmt_getnext_retval_t *retvalp = retp;
472 dlmgmt_link_t link, *linkp;
473 avl_index_t where;
474 int err = 0;
477 * Hold the reader lock to access the link
479 dlmgmt_table_lock(B_FALSE);
481 link.ll_linkid = (getnext->ld_linkid + 1);
482 if ((linkp = avl_find(&dlmgmt_id_avl, &link, &where)) == NULL)
483 linkp = avl_nearest(&dlmgmt_id_avl, where, AVL_AFTER);
485 for (; linkp != NULL; linkp = AVL_NEXT(&dlmgmt_id_avl, linkp)) {
486 if (!link_is_visible(linkp, zoneid))
487 continue;
488 if ((linkp->ll_class & getnext->ld_class) &&
489 (linkp->ll_flags & getnext->ld_flags) &&
490 DATALINK_MEDIA_ACCEPTED(getnext->ld_dmedia,
491 linkp->ll_media))
492 break;
495 if (linkp == NULL) {
496 err = ENOENT;
497 } else {
498 retvalp->lr_linkid = linkp->ll_linkid;
499 retvalp->lr_class = linkp->ll_class;
500 retvalp->lr_media = linkp->ll_media;
501 retvalp->lr_flags = linkp->ll_flags;
504 dlmgmt_table_unlock();
505 retvalp->lr_err = err;
508 /* ARGSUSED */
509 static void
510 dlmgmt_upcall_getattr(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
511 ucred_t *cred)
513 dlmgmt_upcall_arg_getattr_t *getattr = argp;
514 dlmgmt_getattr_retval_t *retvalp = retp;
515 dlmgmt_link_t *linkp;
518 * Hold the reader lock to access the link
520 dlmgmt_table_lock(B_FALSE);
521 if ((linkp = link_by_id(getattr->ld_linkid, zoneid)) == NULL) {
522 retvalp->lr_err = ENOENT;
523 } else {
524 retvalp->lr_err = dlmgmt_getattr_common(&linkp->ll_head,
525 getattr->ld_attr, retvalp);
527 dlmgmt_table_unlock();
530 /* ARGSUSED */
531 static void
532 dlmgmt_createid(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
533 ucred_t *cred)
535 dlmgmt_door_createid_t *createid = argp;
536 dlmgmt_createid_retval_t *retvalp = retp;
537 dlmgmt_link_t *linkp;
538 datalink_id_t linkid = DATALINK_INVALID_LINKID;
539 char link[MAXLINKNAMELEN];
540 int err;
543 * Hold the writer lock to update the dlconf table.
545 dlmgmt_table_lock(B_TRUE);
547 if ((err = dlmgmt_checkprivs(createid->ld_class, cred)) != 0)
548 goto done;
550 if (createid->ld_prefix) {
551 err = dlmgmt_generate_name(createid->ld_link, link,
552 MAXLINKNAMELEN, zoneid);
553 if (err != 0)
554 goto done;
556 err = dlmgmt_create_common(link, createid->ld_class,
557 createid->ld_media, zoneid, createid->ld_flags, &linkp);
558 } else {
559 err = dlmgmt_create_common(createid->ld_link,
560 createid->ld_class, createid->ld_media, zoneid,
561 createid->ld_flags, &linkp);
564 if (err == 0) {
566 * Keep the active mapping.
568 linkid = linkp->ll_linkid;
569 if (createid->ld_flags & DLMGMT_ACTIVE) {
570 (void) dlmgmt_write_db_entry(linkp->ll_link, linkp,
571 DLMGMT_ACTIVE);
575 done:
576 dlmgmt_table_unlock();
577 retvalp->lr_linkid = linkid;
578 retvalp->lr_err = err;
581 /* ARGSUSED */
582 static void
583 dlmgmt_destroyid(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
584 ucred_t *cred)
586 dlmgmt_door_destroyid_t *destroyid = argp;
587 dlmgmt_destroyid_retval_t *retvalp = retp;
588 datalink_id_t linkid = destroyid->ld_linkid;
589 uint32_t flags = destroyid->ld_flags;
590 dlmgmt_link_t *linkp = NULL;
591 int err = 0;
594 * Hold the writer lock to update the link table.
596 dlmgmt_table_lock(B_TRUE);
597 if ((linkp = link_by_id(linkid, zoneid)) == NULL) {
598 err = ENOENT;
599 goto done;
602 if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
603 goto done;
606 * Delete the active mapping.
608 if (flags & DLMGMT_ACTIVE)
609 err = dlmgmt_delete_db_entry(linkp, DLMGMT_ACTIVE);
610 if (err == 0)
611 err = dlmgmt_destroy_common(linkp, flags);
612 done:
613 dlmgmt_table_unlock();
614 retvalp->lr_err = err;
618 * Remap a linkid to a given link name, i.e., rename an existing link1
619 * (ld_linkid) to a non-existent link2 (ld_link): rename link1's name to
620 * the given link name.
622 /* ARGSUSED */
623 static void
624 dlmgmt_remapid(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
625 ucred_t *cred)
627 dlmgmt_door_remapid_t *remapid = argp;
628 dlmgmt_remapid_retval_t *retvalp = retp;
629 dlmgmt_link_t *linkp;
630 char oldname[MAXLINKNAMELEN];
631 boolean_t renamed = B_FALSE;
632 int err = 0;
634 if (!dladm_valid_linkname(remapid->ld_link)) {
635 retvalp->lr_err = EINVAL;
636 return;
640 * Hold the writer lock to update the link table.
642 dlmgmt_table_lock(B_TRUE);
643 if ((linkp = link_by_id(remapid->ld_linkid, zoneid)) == NULL) {
644 err = ENOENT;
645 goto done;
648 if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
649 goto done;
651 if (link_by_name(remapid->ld_link, linkp->ll_zoneid) != NULL) {
652 err = EEXIST;
653 goto done;
656 (void) strlcpy(oldname, linkp->ll_link, MAXLINKNAMELEN);
657 avl_remove(&dlmgmt_name_avl, linkp);
658 (void) strlcpy(linkp->ll_link, remapid->ld_link, MAXLINKNAMELEN);
659 avl_add(&dlmgmt_name_avl, linkp);
660 renamed = B_TRUE;
662 if (linkp->ll_flags & DLMGMT_ACTIVE) {
663 err = dlmgmt_write_db_entry(oldname, linkp, DLMGMT_ACTIVE);
664 if (err != 0)
665 goto done;
667 if (linkp->ll_flags & DLMGMT_PERSIST) {
668 err = dlmgmt_write_db_entry(oldname, linkp, DLMGMT_PERSIST);
669 if (err != 0) {
670 if (linkp->ll_flags & DLMGMT_ACTIVE) {
671 (void) dlmgmt_write_db_entry(remapid->ld_link,
672 linkp, DLMGMT_ACTIVE);
674 goto done;
678 dlmgmt_advance(linkp);
679 linkp->ll_gen++;
680 done:
681 if (err != 0 && renamed) {
682 avl_remove(&dlmgmt_name_avl, linkp);
683 (void) strlcpy(linkp->ll_link, oldname, MAXLINKNAMELEN);
684 avl_add(&dlmgmt_name_avl, linkp);
686 dlmgmt_table_unlock();
687 retvalp->lr_err = err;
690 /* ARGSUSED */
691 static void
692 dlmgmt_upid(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
693 ucred_t *cred)
695 dlmgmt_door_upid_t *upid = argp;
696 dlmgmt_upid_retval_t *retvalp = retp;
697 dlmgmt_link_t *linkp;
698 int err = 0;
701 * Hold the writer lock to update the link table.
703 dlmgmt_table_lock(B_TRUE);
704 if ((linkp = link_by_id(upid->ld_linkid, zoneid)) == NULL) {
705 err = ENOENT;
706 goto done;
709 if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
710 goto done;
712 if (linkp->ll_flags & DLMGMT_ACTIVE) {
713 err = EINVAL;
714 goto done;
717 if ((err = link_activate(linkp)) == 0) {
718 (void) dlmgmt_write_db_entry(linkp->ll_link, linkp,
719 DLMGMT_ACTIVE);
721 done:
722 dlmgmt_table_unlock();
723 retvalp->lr_err = err;
726 /* ARGSUSED */
727 static void
728 dlmgmt_createconf(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
729 ucred_t *cred)
731 dlmgmt_door_createconf_t *createconf = argp;
732 dlmgmt_createconf_retval_t *retvalp = retp;
733 dlmgmt_dlconf_t *dlconfp;
734 int err;
737 * Hold the writer lock to update the dlconf table.
739 dlmgmt_dlconf_table_lock(B_TRUE);
741 if ((err = dlmgmt_checkprivs(createconf->ld_class, cred)) != 0)
742 goto done;
744 err = dlconf_create(createconf->ld_link, createconf->ld_linkid,
745 createconf->ld_class, createconf->ld_media, zoneid, &dlconfp);
746 if (err == 0) {
747 avl_add(&dlmgmt_dlconf_avl, dlconfp);
748 dlmgmt_advance_dlconfid(dlconfp);
749 retvalp->lr_confid = dlconfp->ld_id;
751 done:
752 dlmgmt_dlconf_table_unlock();
753 retvalp->lr_err = err;
756 /* ARGSUSED */
757 static void
758 dlmgmt_setattr(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
759 ucred_t *cred)
761 dlmgmt_door_setattr_t *setattr = argp;
762 dlmgmt_setattr_retval_t *retvalp = retp;
763 dlmgmt_dlconf_t dlconf, *dlconfp;
764 int err = 0;
767 * Hold the writer lock to update the dlconf table.
769 dlmgmt_dlconf_table_lock(B_TRUE);
771 dlconf.ld_id = setattr->ld_confid;
772 dlconfp = avl_find(&dlmgmt_dlconf_avl, &dlconf, NULL);
773 if (dlconfp == NULL || zoneid != dlconfp->ld_zoneid) {
774 err = ENOENT;
775 goto done;
778 if ((err = dlmgmt_checkprivs(dlconfp->ld_class, cred)) != 0)
779 goto done;
781 err = linkattr_set(&(dlconfp->ld_head), setattr->ld_attr,
782 &setattr->ld_attrval, setattr->ld_attrsz, setattr->ld_type);
784 done:
785 dlmgmt_dlconf_table_unlock();
786 retvalp->lr_err = err;
789 /* ARGSUSED */
790 static void
791 dlmgmt_unsetconfattr(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
792 ucred_t *cred)
794 dlmgmt_door_unsetattr_t *unsetattr = argp;
795 dlmgmt_unsetattr_retval_t *retvalp = retp;
796 dlmgmt_dlconf_t dlconf, *dlconfp;
797 int err = 0;
800 * Hold the writer lock to update the dlconf table.
802 dlmgmt_dlconf_table_lock(B_TRUE);
804 dlconf.ld_id = unsetattr->ld_confid;
805 dlconfp = avl_find(&dlmgmt_dlconf_avl, &dlconf, NULL);
806 if (dlconfp == NULL || zoneid != dlconfp->ld_zoneid) {
807 err = ENOENT;
808 goto done;
811 if ((err = dlmgmt_checkprivs(dlconfp->ld_class, cred)) != 0)
812 goto done;
814 linkattr_unset(&(dlconfp->ld_head), unsetattr->ld_attr);
816 done:
817 dlmgmt_dlconf_table_unlock();
818 retvalp->lr_err = err;
822 * Note that dlmgmt_openconf() returns a conf ID of a conf AVL tree entry,
823 * which is managed by dlmgmtd. The ID is used to find the conf entry when
824 * dlmgmt_write_conf() is called. The conf entry contains an ld_gen value
825 * (which is the generation number - ll_gen) of the dlmgmt_link_t at the time
826 * of dlmgmt_openconf(), and ll_gen changes every time the dlmgmt_link_t
827 * changes its attributes. Therefore, dlmgmt_write_conf() can compare ld_gen
828 * in the conf entry against the latest dlmgmt_link_t ll_gen value to see if
829 * anything has changed between the dlmgmt_openconf() and dlmgmt_writeconf()
830 * calls. If so, EAGAIN is returned. This mechanism can ensures atomicity
831 * across the pair of dladm_read_conf() and dladm_write_conf() calls.
833 /* ARGSUSED */
834 static void
835 dlmgmt_writeconf(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
836 ucred_t *cred)
838 dlmgmt_door_writeconf_t *writeconf = argp;
839 dlmgmt_writeconf_retval_t *retvalp = retp;
840 dlmgmt_dlconf_t dlconf, *dlconfp;
841 dlmgmt_link_t *linkp;
842 dlmgmt_linkattr_t *attrp, *next;
843 int err = 0;
846 * Hold the lock to access the dlconf table.
848 dlmgmt_dlconf_table_lock(B_TRUE);
850 dlconf.ld_id = writeconf->ld_confid;
851 dlconfp = avl_find(&dlmgmt_dlconf_avl, &dlconf, NULL);
852 if (dlconfp == NULL || zoneid != dlconfp->ld_zoneid) {
853 err = ENOENT;
854 goto done;
857 if ((err = dlmgmt_checkprivs(dlconfp->ld_class, cred)) != 0)
858 goto done;
861 * Hold the writer lock to update the link table.
863 dlmgmt_table_lock(B_TRUE);
864 linkp = link_by_id(dlconfp->ld_linkid, zoneid);
865 if ((linkp == NULL) || (linkp->ll_class != dlconfp->ld_class) ||
866 (linkp->ll_media != dlconfp->ld_media) ||
867 (strcmp(linkp->ll_link, dlconfp->ld_link) != 0)) {
869 * The link does not exist.
871 dlmgmt_table_unlock();
872 err = ENOENT;
873 goto done;
876 if (linkp->ll_gen != dlconfp->ld_gen) {
878 * Something has changed the link configuration; try again.
880 dlmgmt_table_unlock();
881 err = EAGAIN;
882 goto done;
886 * Delete the old attribute list.
888 for (attrp = linkp->ll_head; attrp != NULL; attrp = next) {
889 next = attrp->lp_next;
890 free(attrp->lp_val);
891 free(attrp);
893 linkp->ll_head = NULL;
896 * Set the new attribute.
898 for (attrp = dlconfp->ld_head; attrp != NULL; attrp = attrp->lp_next) {
899 if ((err = linkattr_set(&(linkp->ll_head), attrp->lp_name,
900 attrp->lp_val, attrp->lp_sz, attrp->lp_type)) != 0) {
901 dlmgmt_table_unlock();
902 goto done;
906 linkp->ll_gen++;
907 err = dlmgmt_write_db_entry(linkp->ll_link, linkp, DLMGMT_PERSIST);
908 dlmgmt_table_unlock();
909 done:
910 dlmgmt_dlconf_table_unlock();
911 retvalp->lr_err = err;
914 /* ARGSUSED */
915 static void
916 dlmgmt_removeconf(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
917 ucred_t *cred)
919 dlmgmt_door_removeconf_t *removeconf = argp;
920 dlmgmt_removeconf_retval_t *retvalp = retp;
921 dlmgmt_link_t *linkp;
922 int err;
924 dlmgmt_table_lock(B_TRUE);
925 if ((linkp = link_by_id(removeconf->ld_linkid, zoneid)) == NULL) {
926 err = ENOENT;
927 goto done;
929 if (zoneid != GLOBAL_ZONEID && linkp->ll_onloan) {
931 * A non-global zone cannot remove the persistent
932 * configuration of a link that is on loan from the global
933 * zone.
935 err = EACCES;
936 goto done;
938 if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
939 goto done;
941 err = dlmgmt_delete_db_entry(linkp, DLMGMT_PERSIST);
942 done:
943 dlmgmt_table_unlock();
944 retvalp->lr_err = err;
947 /* ARGSUSED */
948 static void
949 dlmgmt_destroyconf(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
950 ucred_t *cred)
952 dlmgmt_door_destroyconf_t *destroyconf = argp;
953 dlmgmt_destroyconf_retval_t *retvalp = retp;
954 dlmgmt_dlconf_t dlconf, *dlconfp;
955 int err = 0;
958 * Hold the writer lock to update the dlconf table.
960 dlmgmt_dlconf_table_lock(B_TRUE);
962 dlconf.ld_id = destroyconf->ld_confid;
963 dlconfp = avl_find(&dlmgmt_dlconf_avl, &dlconf, NULL);
964 if (dlconfp == NULL || zoneid != dlconfp->ld_zoneid) {
965 err = ENOENT;
966 goto done;
969 if ((err = dlmgmt_checkprivs(dlconfp->ld_class, cred)) != 0)
970 goto done;
972 avl_remove(&dlmgmt_dlconf_avl, dlconfp);
973 dlconf_destroy(dlconfp);
975 done:
976 dlmgmt_dlconf_table_unlock();
977 retvalp->lr_err = err;
981 * dlmgmt_openconf() returns a handle of the current configuration, which
982 * is then used to update the configuration by dlmgmt_writeconf(). Therefore,
983 * it requires privileges.
985 * Further, please see the comments above dladm_write_conf() to see how
986 * ld_gen is used to ensure atomicity across the {dlmgmt_openconf(),
987 * dlmgmt_writeconf()} pair.
989 /* ARGSUSED */
990 static void
991 dlmgmt_openconf(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
992 ucred_t *cred)
994 dlmgmt_door_openconf_t *openconf = argp;
995 dlmgmt_openconf_retval_t *retvalp = retp;
996 dlmgmt_link_t *linkp;
997 datalink_id_t linkid = openconf->ld_linkid;
998 dlmgmt_dlconf_t *dlconfp;
999 dlmgmt_linkattr_t *attrp;
1000 int err = 0;
1003 * Hold the writer lock to update the dlconf table.
1005 dlmgmt_dlconf_table_lock(B_TRUE);
1008 * Hold the reader lock to access the link
1010 dlmgmt_table_lock(B_FALSE);
1011 linkp = link_by_id(linkid, zoneid);
1012 if ((linkp == NULL) || !(linkp->ll_flags & DLMGMT_PERSIST)) {
1013 /* The persistent link configuration does not exist. */
1014 err = ENOENT;
1015 goto done;
1017 if (linkp->ll_onloan && zoneid != GLOBAL_ZONEID) {
1019 * The caller is in a non-global zone and the persistent
1020 * configuration belongs to the global zone.
1022 err = EACCES;
1023 goto done;
1026 if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
1027 goto done;
1029 if ((err = dlconf_create(linkp->ll_link, linkp->ll_linkid,
1030 linkp->ll_class, linkp->ll_media, zoneid, &dlconfp)) != 0)
1031 goto done;
1033 for (attrp = linkp->ll_head; attrp != NULL; attrp = attrp->lp_next) {
1034 if ((err = linkattr_set(&(dlconfp->ld_head), attrp->lp_name,
1035 attrp->lp_val, attrp->lp_sz, attrp->lp_type)) != 0) {
1036 dlconf_destroy(dlconfp);
1037 goto done;
1040 dlconfp->ld_gen = linkp->ll_gen;
1041 avl_add(&dlmgmt_dlconf_avl, dlconfp);
1042 dlmgmt_advance_dlconfid(dlconfp);
1044 retvalp->lr_confid = dlconfp->ld_id;
1045 done:
1046 dlmgmt_table_unlock();
1047 dlmgmt_dlconf_table_unlock();
1048 retvalp->lr_err = err;
1052 * dlmgmt_getconfsnapshot() returns a read-only snapshot of all the
1053 * configuration, and requires no privileges.
1055 * If the given size cannot hold all the configuration, set the size
1056 * that is needed, and return ENOSPC.
1058 /* ARGSUSED */
1059 static void
1060 dlmgmt_getconfsnapshot(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
1061 ucred_t *cred)
1063 dlmgmt_door_getconfsnapshot_t *snapshot = argp;
1064 dlmgmt_getconfsnapshot_retval_t *retvalp = retp;
1065 dlmgmt_link_t *linkp;
1066 datalink_id_t linkid = snapshot->ld_linkid;
1067 dlmgmt_linkattr_t *attrp;
1068 char *buf;
1069 size_t nvlsz;
1070 nvlist_t *nvl = NULL;
1071 int err = 0;
1073 assert(*sz >= sizeof (dlmgmt_getconfsnapshot_retval_t));
1076 * Hold the reader lock to access the link
1078 dlmgmt_table_lock(B_FALSE);
1079 linkp = link_by_id(linkid, zoneid);
1080 if ((linkp == NULL) || !(linkp->ll_flags & DLMGMT_PERSIST)) {
1081 /* The persistent link configuration does not exist. */
1082 err = ENOENT;
1083 goto done;
1085 if (linkp->ll_onloan && zoneid != GLOBAL_ZONEID) {
1087 * The caller is in a non-global zone and the persistent
1088 * configuration belongs to the global zone.
1090 err = EACCES;
1091 goto done;
1094 err = nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0);
1095 if (err != 0)
1096 goto done;
1098 for (attrp = linkp->ll_head; attrp != NULL; attrp = attrp->lp_next) {
1099 if ((err = nvlist_add_byte_array(nvl, attrp->lp_name,
1100 attrp->lp_val, attrp->lp_sz)) != 0) {
1101 goto done;
1105 if ((err = nvlist_size(nvl, &nvlsz, NV_ENCODE_NATIVE)) != 0)
1106 goto done;
1108 if (nvlsz + sizeof (dlmgmt_getconfsnapshot_retval_t) > *sz) {
1109 *sz = nvlsz + sizeof (dlmgmt_getconfsnapshot_retval_t);
1110 err = ENOSPC;
1111 goto done;
1115 * pack the the nvlist into the return value.
1117 *sz = nvlsz + sizeof (dlmgmt_getconfsnapshot_retval_t);
1118 retvalp->lr_nvlsz = nvlsz;
1119 buf = (char *)retvalp + sizeof (dlmgmt_getconfsnapshot_retval_t);
1120 err = nvlist_pack(nvl, &buf, &nvlsz, NV_ENCODE_NATIVE, 0);
1122 done:
1123 dlmgmt_table_unlock();
1124 nvlist_free(nvl);
1125 retvalp->lr_err = err;
1128 /* ARGSUSED */
1129 static void
1130 dlmgmt_getattr(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
1131 ucred_t *cred)
1133 dlmgmt_door_getattr_t *getattr = argp;
1134 dlmgmt_getattr_retval_t *retvalp = retp;
1135 dlmgmt_dlconf_t dlconf, *dlconfp;
1136 int err;
1139 * Hold the read lock to access the dlconf table.
1141 dlmgmt_dlconf_table_lock(B_FALSE);
1143 dlconf.ld_id = getattr->ld_confid;
1144 if ((dlconfp = avl_find(&dlmgmt_dlconf_avl, &dlconf, NULL)) == NULL ||
1145 zoneid != dlconfp->ld_zoneid) {
1146 retvalp->lr_err = ENOENT;
1147 } else {
1148 if ((err = dlmgmt_checkprivs(dlconfp->ld_class, cred)) != 0) {
1149 retvalp->lr_err = err;
1150 } else {
1151 retvalp->lr_err = dlmgmt_getattr_common(
1152 &dlconfp->ld_head, getattr->ld_attr, retvalp);
1156 dlmgmt_dlconf_table_unlock();
1159 /* ARGSUSED */
1160 static void
1161 dlmgmt_upcall_linkprop_init(void *argp, void *retp, size_t *sz,
1162 zoneid_t zoneid, ucred_t *cred)
1164 dlmgmt_door_linkprop_init_t *lip = argp;
1165 dlmgmt_linkprop_init_retval_t *retvalp = retp;
1166 dlmgmt_link_t *linkp;
1167 int err;
1169 dlmgmt_table_lock(B_FALSE);
1170 if ((linkp = link_by_id(lip->ld_linkid, zoneid)) == NULL)
1171 err = ENOENT;
1172 else
1173 err = dlmgmt_checkprivs(linkp->ll_class, cred);
1174 dlmgmt_table_unlock();
1176 if (err == 0) {
1177 dladm_status_t s;
1178 char buf[DLADM_STRSIZE];
1180 s = dladm_init_linkprop(dld_handle, lip->ld_linkid, B_TRUE);
1181 if (s != DLADM_STATUS_OK) {
1182 dlmgmt_log(LOG_WARNING,
1183 "linkprop initialization failed on link %d: %s",
1184 lip->ld_linkid, dladm_status2str(s, buf));
1185 err = EINVAL;
1188 retvalp->lr_err = err;
1191 /* ARGSUSED */
1192 static void
1193 dlmgmt_setzoneid(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
1194 ucred_t *cred)
1196 dlmgmt_door_setzoneid_t *setzoneid = argp;
1197 dlmgmt_setzoneid_retval_t *retvalp = retp;
1198 dlmgmt_link_t *linkp;
1199 datalink_id_t linkid = setzoneid->ld_linkid;
1200 zoneid_t oldzoneid, newzoneid;
1201 int err = 0;
1203 dlmgmt_table_lock(B_TRUE);
1205 /* We currently only allow changing zoneid's from the global zone. */
1206 if (zoneid != GLOBAL_ZONEID) {
1207 err = EACCES;
1208 goto done;
1211 if ((linkp = link_by_id(linkid, zoneid)) == NULL) {
1212 err = ENOENT;
1213 goto done;
1216 if ((err = dlmgmt_checkprivs(linkp->ll_class, cred)) != 0)
1217 goto done;
1219 /* We can only assign an active link to a zone. */
1220 if (!(linkp->ll_flags & DLMGMT_ACTIVE)) {
1221 err = EINVAL;
1222 goto done;
1225 oldzoneid = linkp->ll_zoneid;
1226 newzoneid = setzoneid->ld_zoneid;
1228 if (oldzoneid == newzoneid)
1229 goto done;
1232 * Before we remove the link from its current zone, make sure that
1233 * there isn't a link with the same name in the destination zone.
1235 if (zoneid != GLOBAL_ZONEID &&
1236 link_by_name(linkp->ll_link, newzoneid) != NULL) {
1237 err = EEXIST;
1238 goto done;
1241 if (oldzoneid != GLOBAL_ZONEID) {
1242 if (zone_remove_datalink(oldzoneid, linkid) != 0) {
1243 err = errno;
1244 dlmgmt_log(LOG_WARNING, "unable to remove link %d from "
1245 "zone %d: %s", linkid, oldzoneid, strerror(err));
1246 goto done;
1248 avl_remove(&dlmgmt_loan_avl, linkp);
1249 linkp->ll_onloan = B_FALSE;
1251 if (newzoneid != GLOBAL_ZONEID) {
1252 if (zone_add_datalink(newzoneid, linkid) != 0) {
1253 err = errno;
1254 dlmgmt_log(LOG_WARNING, "unable to add link %d to zone "
1255 "%d: %s", linkid, newzoneid, strerror(err));
1256 (void) zone_add_datalink(oldzoneid, linkid);
1257 goto done;
1259 avl_add(&dlmgmt_loan_avl, linkp);
1260 linkp->ll_onloan = B_TRUE;
1263 avl_remove(&dlmgmt_name_avl, linkp);
1264 linkp->ll_zoneid = newzoneid;
1265 avl_add(&dlmgmt_name_avl, linkp);
1267 done:
1268 dlmgmt_table_unlock();
1269 retvalp->lr_err = err;
1272 /* ARGSUSED */
1273 static void
1274 dlmgmt_zoneboot(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
1275 ucred_t *cred)
1277 int err;
1278 dlmgmt_door_zoneboot_t *zoneboot = argp;
1279 dlmgmt_zoneboot_retval_t *retvalp = retp;
1281 dlmgmt_table_lock(B_TRUE);
1283 if ((err = dlmgmt_checkprivs(0, cred)) != 0)
1284 goto done;
1286 if (zoneid != GLOBAL_ZONEID) {
1287 err = EACCES;
1288 goto done;
1290 if (zoneboot->ld_zoneid == GLOBAL_ZONEID) {
1291 err = EINVAL;
1292 goto done;
1295 if ((err = dlmgmt_elevate_privileges()) == 0) {
1296 err = dlmgmt_zone_init(zoneboot->ld_zoneid);
1297 (void) dlmgmt_drop_privileges();
1299 done:
1300 dlmgmt_table_unlock();
1301 retvalp->lr_err = err;
1304 /* ARGSUSED */
1305 static void
1306 dlmgmt_zonehalt(void *argp, void *retp, size_t *sz, zoneid_t zoneid,
1307 ucred_t *cred)
1309 int err = 0;
1310 dlmgmt_door_zonehalt_t *zonehalt = argp;
1311 dlmgmt_zonehalt_retval_t *retvalp = retp;
1313 if ((err = dlmgmt_checkprivs(0, cred)) == 0) {
1314 if (zoneid != GLOBAL_ZONEID) {
1315 err = EACCES;
1316 } else if (zonehalt->ld_zoneid == GLOBAL_ZONEID) {
1317 err = EINVAL;
1318 } else {
1319 dlmgmt_table_lock(B_TRUE);
1320 dlmgmt_db_fini(zonehalt->ld_zoneid);
1321 dlmgmt_table_unlock();
1324 retvalp->lr_err = err;
1327 static dlmgmt_door_info_t i_dlmgmt_door_info_tbl[] = {
1328 { DLMGMT_CMD_DLS_CREATE, sizeof (dlmgmt_upcall_arg_create_t),
1329 sizeof (dlmgmt_create_retval_t), dlmgmt_upcall_create },
1330 { DLMGMT_CMD_DLS_GETATTR, sizeof (dlmgmt_upcall_arg_getattr_t),
1331 sizeof (dlmgmt_getattr_retval_t), dlmgmt_upcall_getattr },
1332 { DLMGMT_CMD_DLS_DESTROY, sizeof (dlmgmt_upcall_arg_destroy_t),
1333 sizeof (dlmgmt_destroy_retval_t), dlmgmt_upcall_destroy },
1334 { DLMGMT_CMD_GETNAME, sizeof (dlmgmt_door_getname_t),
1335 sizeof (dlmgmt_getname_retval_t), dlmgmt_getname },
1336 { DLMGMT_CMD_GETLINKID, sizeof (dlmgmt_door_getlinkid_t),
1337 sizeof (dlmgmt_getlinkid_retval_t), dlmgmt_getlinkid },
1338 { DLMGMT_CMD_GETNEXT, sizeof (dlmgmt_door_getnext_t),
1339 sizeof (dlmgmt_getnext_retval_t), dlmgmt_getnext },
1340 { DLMGMT_CMD_DLS_UPDATE, sizeof (dlmgmt_upcall_arg_update_t),
1341 sizeof (dlmgmt_update_retval_t), dlmgmt_upcall_update },
1342 { DLMGMT_CMD_CREATE_LINKID, sizeof (dlmgmt_door_createid_t),
1343 sizeof (dlmgmt_createid_retval_t), dlmgmt_createid },
1344 { DLMGMT_CMD_DESTROY_LINKID, sizeof (dlmgmt_door_destroyid_t),
1345 sizeof (dlmgmt_destroyid_retval_t), dlmgmt_destroyid },
1346 { DLMGMT_CMD_REMAP_LINKID, sizeof (dlmgmt_door_remapid_t),
1347 sizeof (dlmgmt_remapid_retval_t), dlmgmt_remapid },
1348 { DLMGMT_CMD_CREATECONF, sizeof (dlmgmt_door_createconf_t),
1349 sizeof (dlmgmt_createconf_retval_t), dlmgmt_createconf },
1350 { DLMGMT_CMD_OPENCONF, sizeof (dlmgmt_door_openconf_t),
1351 sizeof (dlmgmt_openconf_retval_t), dlmgmt_openconf },
1352 { DLMGMT_CMD_WRITECONF, sizeof (dlmgmt_door_writeconf_t),
1353 sizeof (dlmgmt_writeconf_retval_t), dlmgmt_writeconf },
1354 { DLMGMT_CMD_UP_LINKID, sizeof (dlmgmt_door_upid_t),
1355 sizeof (dlmgmt_upid_retval_t), dlmgmt_upid },
1356 { DLMGMT_CMD_SETATTR, sizeof (dlmgmt_door_setattr_t),
1357 sizeof (dlmgmt_setattr_retval_t), dlmgmt_setattr },
1358 { DLMGMT_CMD_UNSETATTR, sizeof (dlmgmt_door_unsetattr_t),
1359 sizeof (dlmgmt_unsetattr_retval_t), dlmgmt_unsetconfattr },
1360 { DLMGMT_CMD_REMOVECONF, sizeof (dlmgmt_door_removeconf_t),
1361 sizeof (dlmgmt_removeconf_retval_t), dlmgmt_removeconf },
1362 { DLMGMT_CMD_DESTROYCONF, sizeof (dlmgmt_door_destroyconf_t),
1363 sizeof (dlmgmt_destroyconf_retval_t), dlmgmt_destroyconf },
1364 { DLMGMT_CMD_GETATTR, sizeof (dlmgmt_door_getattr_t),
1365 sizeof (dlmgmt_getattr_retval_t), dlmgmt_getattr },
1366 { DLMGMT_CMD_GETCONFSNAPSHOT, sizeof (dlmgmt_door_getconfsnapshot_t),
1367 sizeof (dlmgmt_getconfsnapshot_retval_t), dlmgmt_getconfsnapshot },
1368 { DLMGMT_CMD_LINKPROP_INIT, sizeof (dlmgmt_door_linkprop_init_t),
1369 sizeof (dlmgmt_linkprop_init_retval_t),
1370 dlmgmt_upcall_linkprop_init },
1371 { DLMGMT_CMD_SETZONEID, sizeof (dlmgmt_door_setzoneid_t),
1372 sizeof (dlmgmt_setzoneid_retval_t), dlmgmt_setzoneid },
1373 { DLMGMT_CMD_ZONEBOOT, sizeof (dlmgmt_door_zoneboot_t),
1374 sizeof (dlmgmt_zoneboot_retval_t), dlmgmt_zoneboot },
1375 { DLMGMT_CMD_ZONEHALT, sizeof (dlmgmt_door_zonehalt_t),
1376 sizeof (dlmgmt_zonehalt_retval_t), dlmgmt_zonehalt },
1377 { 0, 0, 0, NULL }
1380 static dlmgmt_door_info_t *
1381 dlmgmt_getcmdinfo(int cmd)
1383 dlmgmt_door_info_t *infop = i_dlmgmt_door_info_tbl;
1385 while (infop->di_handler != NULL) {
1386 if (infop->di_cmd == cmd)
1387 break;
1388 infop++;
1390 return (infop);
1393 /* ARGSUSED */
1394 void
1395 dlmgmt_handler(void *cookie, char *argp, size_t argsz, door_desc_t *dp,
1396 uint_t n_desc)
1398 dlmgmt_door_arg_t *door_arg = (dlmgmt_door_arg_t *)(void *)argp;
1399 dlmgmt_door_info_t *infop = NULL;
1400 dlmgmt_retval_t retval;
1401 ucred_t *cred = NULL;
1402 zoneid_t zoneid;
1403 void *retvalp = NULL;
1404 size_t sz, acksz;
1405 int err = 0;
1407 infop = dlmgmt_getcmdinfo(door_arg->ld_cmd);
1408 if (infop == NULL || argsz != infop->di_reqsz) {
1409 err = EINVAL;
1410 goto done;
1413 if (door_ucred(&cred) != 0 || (zoneid = ucred_getzoneid(cred)) == -1) {
1414 err = errno;
1415 goto done;
1419 * Note that malloc() cannot be used here because door_return
1420 * never returns, and memory allocated by malloc() would get leaked.
1421 * Use alloca() instead.
1423 acksz = infop->di_acksz;
1425 again:
1426 retvalp = alloca(acksz);
1427 sz = acksz;
1428 infop->di_handler(argp, retvalp, &acksz, zoneid, cred);
1429 if (acksz > sz) {
1431 * If the specified buffer size is not big enough to hold the
1432 * return value, reallocate the buffer and try to get the
1433 * result one more time.
1435 assert(((dlmgmt_retval_t *)retvalp)->lr_err == ENOSPC);
1436 goto again;
1439 done:
1440 if (cred != NULL)
1441 ucred_free(cred);
1442 if (err == 0) {
1443 (void) door_return(retvalp, acksz, NULL, 0);
1444 } else {
1445 retval.lr_err = err;
1446 (void) door_return((char *)&retval, sizeof (retval), NULL, 0);