1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, Joyent Inc. All rights reserved.
25 */
26
27 /*
28 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
29 */
30
31 /*
32 * This module contains functions used to bring up and tear down the
33 * Virtual Platform: [un]mounting file-systems, [un]plumbing network
34 * interfaces, [un]configuring devices, establishing resource controls,
35 * and creating/destroying the zone in the kernel. These actions, on
36 * the way up, ready the zone; on the way down, they halt the zone.
37 * See the much longer block comment at the beginning of zoneadmd.c
38 * for a bigger picture of how the whole program functions.
39 *
40 * This module also has primary responsibility for the layout of "scratch
41 * zones." These are mounted, but inactive, zones that are used during
42 * operating system upgrade and potentially other administrative action. The
43 * scratch zone environment is similar to the miniroot environment. The zone's
44 * actual root is mounted read-write on /a, and the standard paths (/usr,
45 * /sbin, /lib) all lead to read-only copies of the running system's binaries.
46 * This allows the administrative tools to manipulate the zone using "-R /a"
47 * without relying on any binaries in the zone itself.
48 *
49 * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
50 * environment), then we must resolve the lofs mounts used there to uncover
51 * writable (unshared) resources. Shared resources, though, are always
52 * read-only. In addition, if the "same" zone with a different root path is
53 * currently running, then "/b" inside the zone points to the running zone's
54 * root. This allows LU to synchronize configuration files during the upgrade
55 * process.
56 *
57 * To construct this environment, this module creates a tmpfs mount on
58 * $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as
59 * described above is constructed on the fly. The zone is then created using
60 * $ZONEPATH/lu as the root.
61 *
62 * Note that scratch zones are inactive. The zone's bits are not running and
63 * likely cannot be run correctly until upgrade is done. Init is not running
64 * there, nor is SMF. Because of this, the "mounted" state of a scratch zone
65 * is not a part of the usual halt/ready/boot state machine.
66 */
67
68 #include <sys/param.h>
69 #include <sys/mount.h>
70 #include <sys/mntent.h>
71 #include <sys/socket.h>
72 #include <sys/utsname.h>
73 #include <sys/types.h>
74 #include <sys/stat.h>
75 #include <sys/sockio.h>
76 #include <sys/stropts.h>
77 #include <sys/conf.h>
78 #include <sys/systeminfo.h>
79
80 #include <libdlpi.h>
81 #include <libdllink.h>
82 #include <libdlvlan.h>
83
84 #include <inet/tcp.h>
85 #include <arpa/inet.h>
86 #include <netinet/in.h>
87 #include <net/route.h>
88
89 #include <stdio.h>
90 #include <errno.h>
91 #include <fcntl.h>
92 #include <unistd.h>
93 #include <rctl.h>
94 #include <stdlib.h>
95 #include <string.h>
96 #include <strings.h>
97 #include <wait.h>
98 #include <limits.h>
99 #include <libgen.h>
100 #include <libzfs.h>
101 #include <libdevinfo.h>
102 #include <zone.h>
103 #include <assert.h>
104 #include <libcontract.h>
105 #include <libcontract_priv.h>
106 #include <uuid/uuid.h>
107
108 #include <sys/mntio.h>
109 #include <sys/mnttab.h>
110 #include <sys/fs/autofs.h> /* for _autofssys() */
111 #include <sys/fs/lofs_info.h>
112 #include <sys/fs/zfs.h>
113
114 #include <pool.h>
115 #include <sys/pool.h>
116 #include <sys/priocntl.h>
117
118 #include <libbrand.h>
119 #include <sys/brand.h>
120 #include <libzonecfg.h>
121 #include <synch.h>
122
123 #include "zoneadmd.h"
124 #include <tsol/label.h>
125 #include <libtsnet.h>
126 #include <sys/priv.h>
127 #include <libinetutil.h>
128
129 #define V4_ADDR_LEN 32
130 #define V6_ADDR_LEN 128
131
132 #define RESOURCE_DEFAULT_OPTS \
133 MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
134
135 #define DFSTYPES "/etc/dfs/fstypes"
136 #define MAXTNZLEN 2048
137
138 #define ALT_MOUNT(mount_cmd) ((mount_cmd) != Z_MNT_BOOT)
139
140 /* a reasonable estimate for the number of lwps per process */
141 #define LWPS_PER_PROCESS 10
142
143 /* for routing socket */
144 static int rts_seqno = 0;
145
146 /* mangled zone name when mounting in an alternate root environment */
147 static char kernzone[ZONENAME_MAX];
148
149 /* array of cached mount entries for resolve_lofs */
150 static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
151
152 /* for Trusted Extensions */
153 static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
154 static int tsol_mounts(zlog_t *, char *, char *);
155 static void tsol_unmounts(zlog_t *, char *);
156
157 static m_label_t *zlabel = NULL;
158 static m_label_t *zid_label = NULL;
159 static priv_set_t *zprivs = NULL;
160
161 static const char *DFLT_FS_ALLOWED = "hsfs,smbfs,nfs,nfs3,nfs4,nfsdyn";
162
163 /* from libsocket, not in any header file */
164 extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
165
166 /* from zoneadmd */
167 extern char query_hook[];
168
169 /*
170 * For each "net" resource configured in zonecfg, we track a zone_addr_list_t
171 * node in a linked list that is sorted by linkid. The list is constructed as
172 * the xml configuration file is parsed, and the information
173 * contained in each node is added to the kernel before the zone is
174 * booted, to be retrieved and applied from within the exclusive-IP NGZ
175 * on boot.
176 */
177 typedef struct zone_addr_list {
178 struct zone_addr_list *za_next;
179 datalink_id_t za_linkid; /* datalink_id_t of interface */
180 struct zone_nwiftab za_nwiftab; /* address, defrouter properties */
181 } zone_addr_list_t;
182
183 /*
184 * An optimization for build_mnttable: reallocate (and potentially copy the
185 * data) only once every N times through the loop.
186 */
187 #define MNTTAB_HUNK 32
188
189 /* some handy macros */
190 #define SIN(s) ((struct sockaddr_in *)s)
191 #define SIN6(s) ((struct sockaddr_in6 *)s)
192
193 /*
194 * Private autofs system call
195 */
196 extern int _autofssys(int, void *);
197
198 static int
199 autofs_cleanup(zoneid_t zoneid)
200 {
201 /*
202 * Ask autofs to unmount all trigger nodes in the given zone.
203 */
204 return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
205 }
206
207 static void
208 free_mnttable(struct mnttab *mnt_array, uint_t nelem)
209 {
210 uint_t i;
211
212 if (mnt_array == NULL)
213 return;
214 for (i = 0; i < nelem; i++) {
215 free(mnt_array[i].mnt_mountp);
216 free(mnt_array[i].mnt_fstype);
217 free(mnt_array[i].mnt_special);
218 free(mnt_array[i].mnt_mntopts);
219 assert(mnt_array[i].mnt_time == NULL);
220 }
221 free(mnt_array);
222 }
223
224 /*
225 * Build the mount table for the zone rooted at "zroot", storing the resulting
226 * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
227 * array in "nelemp".
228 */
229 static int
230 build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
231 struct mnttab **mnt_arrayp, uint_t *nelemp)
232 {
233 struct mnttab mnt;
234 struct mnttab *mnts;
235 struct mnttab *mnp;
236 uint_t nmnt;
237
238 rewind(mnttab);
239 resetmnttab(mnttab);
240 nmnt = 0;
241 mnts = NULL;
242 while (getmntent(mnttab, &mnt) == 0) {
243 struct mnttab *tmp_array;
244
245 if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
246 continue;
247 if (nmnt % MNTTAB_HUNK == 0) {
248 tmp_array = realloc(mnts,
249 (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
250 if (tmp_array == NULL) {
251 free_mnttable(mnts, nmnt);
252 return (-1);
253 }
254 mnts = tmp_array;
255 }
256 mnp = &mnts[nmnt++];
257
258 /*
259 * Zero out any fields we're not using.
260 */
261 (void) memset(mnp, 0, sizeof (*mnp));
262
263 if (mnt.mnt_special != NULL)
264 mnp->mnt_special = strdup(mnt.mnt_special);
265 if (mnt.mnt_mntopts != NULL)
266 mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
267 mnp->mnt_mountp = strdup(mnt.mnt_mountp);
268 mnp->mnt_fstype = strdup(mnt.mnt_fstype);
269 if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
270 (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
271 mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
272 zerror(zlogp, B_TRUE, "memory allocation failed");
273 free_mnttable(mnts, nmnt);
274 return (-1);
275 }
276 }
277 *mnt_arrayp = mnts;
278 *nelemp = nmnt;
279 return (0);
280 }
281
282 /*
283 * This is an optimization. The resolve_lofs function is used quite frequently
284 * to manipulate file paths, and on a machine with a large number of zones,
285 * there will be a huge number of mounted file systems. Thus, we trigger a
286 * reread of the list of mount points
287 */
288 static void
289 lofs_discard_mnttab(void)
290 {
291 free_mnttable(resolve_lofs_mnts,
292 resolve_lofs_mnt_max - resolve_lofs_mnts);
293 resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
294 }
295
296 static int
297 lofs_read_mnttab(zlog_t *zlogp)
298 {
299 FILE *mnttab;
300 uint_t nmnts;
301
302 if ((mnttab = fopen(MNTTAB, "r")) == NULL)
303 return (-1);
304 if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
305 &nmnts) == -1) {
306 (void) fclose(mnttab);
307 return (-1);
308 }
309 (void) fclose(mnttab);
310 resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
311 return (0);
312 }
313
314 /*
315 * This function loops over potential loopback mounts and symlinks in a given
316 * path and resolves them all down to an absolute path.
317 */
318 void
319 resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
320 {
321 int len, arlen;
322 const char *altroot;
323 char tmppath[MAXPATHLEN];
324 boolean_t outside_altroot;
325
326 if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
327 return;
328 tmppath[len] = '\0';
329 (void) strlcpy(path, tmppath, sizeof (tmppath));
330
331 /* This happens once per zoneadmd operation. */
332 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
333 return;
334
335 altroot = zonecfg_get_root();
336 arlen = strlen(altroot);
337 outside_altroot = B_FALSE;
338 for (;;) {
339 struct mnttab *mnp;
340
341 /* Search in reverse order to find longest match */
342 for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
343 mnp--) {
344 if (mnp->mnt_fstype == NULL ||
345 mnp->mnt_mountp == NULL ||
346 mnp->mnt_special == NULL)
347 continue;
348 len = strlen(mnp->mnt_mountp);
349 if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
350 (path[len] == '/' || path[len] == '\0'))
351 break;
352 }
353 if (mnp < resolve_lofs_mnts)
354 break;
355 /* If it's not a lofs then we're done */
356 if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
357 break;
358 if (outside_altroot) {
359 char *cp;
360 int olen = sizeof (MNTOPT_RO) - 1;
361
362 /*
363 * If we run into a read-only mount outside of the
364 * alternate root environment, then the user doesn't
365 * want this path to be made read-write.
366 */
367 if (mnp->mnt_mntopts != NULL &&
368 (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
369 NULL &&
370 (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
371 (cp[olen] == '\0' || cp[olen] == ',')) {
372 break;
373 }
374 } else if (arlen > 0 &&
375 (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
376 (mnp->mnt_special[arlen] != '\0' &&
377 mnp->mnt_special[arlen] != '/'))) {
378 outside_altroot = B_TRUE;
379 }
380 /* use temporary buffer because new path might be longer */
381 (void) snprintf(tmppath, sizeof (tmppath), "%s%s",
382 mnp->mnt_special, path + len);
383 if ((len = resolvepath(tmppath, path, pathlen)) == -1)
384 break;
385 path[len] = '\0';
386 }
387 }
388
389 /*
390 * For a regular mount, check if a replacement lofs mount is needed because the
391 * referenced device is already mounted somewhere.
392 */
393 static int
394 check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
395 {
396 struct mnttab *mnp;
397 zone_fsopt_t *optptr, *onext;
398
399 /* This happens once per zoneadmd operation. */
400 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
401 return (-1);
402
403 /*
404 * If this special node isn't already in use, then it's ours alone;
405 * no need to worry about conflicting mounts.
406 */
407 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
408 mnp++) {
409 if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
410 break;
411 }
412 if (mnp >= resolve_lofs_mnt_max)
413 return (0);
414
415 /*
416 * Convert this duplicate mount into a lofs mount.
417 */
418 (void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
419 sizeof (fsptr->zone_fs_special));
420 (void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
421 sizeof (fsptr->zone_fs_type));
422 fsptr->zone_fs_raw[0] = '\0';
423
424 /*
425 * Discard all but one of the original options and set that to our
426 * default set of options used for resources.
427 */
428 optptr = fsptr->zone_fs_options;
429 if (optptr == NULL) {
430 optptr = malloc(sizeof (*optptr));
431 if (optptr == NULL) {
432 zerror(zlogp, B_TRUE, "cannot mount %s",
433 fsptr->zone_fs_dir);
434 return (-1);
435 }
436 } else {
437 while ((onext = optptr->zone_fsopt_next) != NULL) {
438 optptr->zone_fsopt_next = onext->zone_fsopt_next;
439 free(onext);
440 }
441 }
442 (void) strcpy(optptr->zone_fsopt_opt, RESOURCE_DEFAULT_OPTS);
443 optptr->zone_fsopt_next = NULL;
444 fsptr->zone_fs_options = optptr;
445 return (0);
446 }
447
448 int
449 make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
450 uid_t userid, gid_t groupid)
451 {
452 char path[MAXPATHLEN];
453 struct stat st;
454
455 if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
456 sizeof (path)) {
457 zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
458 subdir);
459 return (-1);
460 }
461
462 if (lstat(path, &st) == 0) {
463 /*
464 * We don't check the file mode since presumably the zone
465 * administrator may have had good reason to change the mode,
466 * and we don't need to second guess him.
467 */
468 if (!S_ISDIR(st.st_mode)) {
469 if (S_ISREG(st.st_mode)) {
470 /*
471 * Allow readonly mounts of /etc/ files; this
472 * is needed most by Trusted Extensions.
473 */
474 if (strncmp(subdir, "/etc/",
475 strlen("/etc/")) != 0) {
476 zerror(zlogp, B_FALSE,
477 "%s is not in /etc", path);
478 return (-1);
479 }
480 } else {
481 zerror(zlogp, B_FALSE,
482 "%s is not a directory", path);
483 return (-1);
484 }
485 }
486 return (0);
487 }
488
489 if (mkdirp(path, mode) != 0) {
490 if (errno == EROFS)
491 zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
492 "a read-only file system in this local zone.\nMake "
493 "sure %s exists in the global zone.", path, subdir);
494 else
495 zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
496 return (-1);
497 }
498
499 (void) chown(path, userid, groupid);
500 return (0);
501 }
502
503 static void
504 free_remote_fstypes(char **types)
505 {
506 uint_t i;
507
508 if (types == NULL)
509 return;
510 for (i = 0; types[i] != NULL; i++)
511 free(types[i]);
512 free(types);
513 }
514
515 static char **
516 get_remote_fstypes(zlog_t *zlogp)
517 {
518 char **types = NULL;
519 FILE *fp;
520 char buf[MAXPATHLEN];
521 char fstype[MAXPATHLEN];
522 uint_t lines = 0;
523 uint_t i;
524
525 if ((fp = fopen(DFSTYPES, "r")) == NULL) {
526 zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
527 return (NULL);
528 }
529 /*
530 * Count the number of lines
531 */
532 while (fgets(buf, sizeof (buf), fp) != NULL)
533 lines++;
534 if (lines == 0) /* didn't read anything; empty file */
535 goto out;
536 rewind(fp);
537 /*
538 * Allocate enough space for a NULL-terminated array.
539 */
540 types = calloc(lines + 1, sizeof (char *));
541 if (types == NULL) {
542 zerror(zlogp, B_TRUE, "memory allocation failed");
543 goto out;
544 }
545 i = 0;
546 while (fgets(buf, sizeof (buf), fp) != NULL) {
547 /* LINTED - fstype is big enough to hold buf */
548 if (sscanf(buf, "%s", fstype) == 0) {
549 zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
550 free_remote_fstypes(types);
551 types = NULL;
552 goto out;
553 }
554 types[i] = strdup(fstype);
555 if (types[i] == NULL) {
556 zerror(zlogp, B_TRUE, "memory allocation failed");
557 free_remote_fstypes(types);
558 types = NULL;
559 goto out;
560 }
561 i++;
562 }
563 out:
564 (void) fclose(fp);
565 return (types);
566 }
567
568 static boolean_t
569 is_remote_fstype(const char *fstype, char *const *remote_fstypes)
570 {
571 uint_t i;
572
573 if (remote_fstypes == NULL)
574 return (B_FALSE);
575 for (i = 0; remote_fstypes[i] != NULL; i++) {
576 if (strcmp(remote_fstypes[i], fstype) == 0)
577 return (B_TRUE);
578 }
579 return (B_FALSE);
580 }
581
582 /*
583 * This converts a zone root path (normally of the form .../root) to a Live
584 * Upgrade scratch zone root (of the form .../lu).
585 */
586 static void
587 root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
588 {
589 if (!isresolved && zonecfg_in_alt_root())
590 resolve_lofs(zlogp, zroot, zrootlen);
591 (void) strcpy(strrchr(zroot, '/') + 1, "lu");
592 }
593
594 /*
595 * The general strategy for unmounting filesystems is as follows:
596 *
597 * - Remote filesystems may be dead, and attempting to contact them as
598 * part of a regular unmount may hang forever; we want to always try to
599 * forcibly unmount such filesystems and only fall back to regular
600 * unmounts if the filesystem doesn't support forced unmounts.
601 *
602 * - We don't want to unnecessarily corrupt metadata on local
603 * filesystems (ie UFS), so we want to start off with graceful unmounts,
604 * and only escalate to doing forced unmounts if we get stuck.
605 *
606 * We start off walking backwards through the mount table. This doesn't
607 * give us strict ordering but ensures that we try to unmount submounts
608 * first. We thus limit the number of failed umount2(2) calls.
609 *
610 * The mechanism for determining if we're stuck is to count the number
611 * of failed unmounts each iteration through the mount table. This
612 * gives us an upper bound on the number of filesystems which remain
613 * mounted (autofs trigger nodes are dealt with separately). If at the
614 * end of one unmount+autofs_cleanup cycle we still have the same number
615 * of mounts that we started out with, we're stuck and try a forced
616 * unmount. If that fails (filesystem doesn't support forced unmounts)
617 * then we bail and are unable to teardown the zone. If it succeeds,
618 * we're no longer stuck so we continue with our policy of trying
619 * graceful mounts first.
620 *
621 * Zone must be down (ie, no processes or threads active).
622 */
623 static int
624 unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
625 {
626 int error = 0;
627 FILE *mnttab;
628 struct mnttab *mnts;
629 uint_t nmnt;
630 char zroot[MAXPATHLEN + 1];
631 size_t zrootlen;
632 uint_t oldcount = UINT_MAX;
633 boolean_t stuck = B_FALSE;
634 char **remote_fstypes = NULL;
635
636 if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
637 zerror(zlogp, B_FALSE, "unable to determine zone root");
638 return (-1);
639 }
640 if (unmount_cmd)
641 root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
642
643 (void) strcat(zroot, "/");
644 zrootlen = strlen(zroot);
645
646 /*
647 * For Trusted Extensions unmount each higher level zone's mount
648 * of our zone's /export/home
649 */
650 if (!unmount_cmd)
651 tsol_unmounts(zlogp, zone_name);
652
653 if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
654 zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
655 return (-1);
656 }
657 /*
658 * Use our hacky mntfs ioctl so we see everything, even mounts with
659 * MS_NOMNTTAB.
660 */
661 if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
662 zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
663 error++;
664 goto out;
665 }
666
667 /*
668 * Build the list of remote fstypes so we know which ones we
669 * should forcibly unmount.
670 */
671 remote_fstypes = get_remote_fstypes(zlogp);
672 for (; /* ever */; ) {
673 uint_t newcount = 0;
674 boolean_t unmounted;
675 struct mnttab *mnp;
676 char *path;
677 uint_t i;
678
679 mnts = NULL;
680 nmnt = 0;
681 /*
682 * MNTTAB gives us a way to walk through mounted
683 * filesystems; we need to be able to walk them in
684 * reverse order, so we build a list of all mounted
685 * filesystems.
686 */
687 if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
688 &nmnt) != 0) {
689 error++;
690 goto out;
691 }
692 for (i = 0; i < nmnt; i++) {
693 mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
694 path = mnp->mnt_mountp;
695 unmounted = B_FALSE;
696 /*
697 * Try forced unmount first for remote filesystems.
698 *
699 * Not all remote filesystems support forced unmounts,
700 * so if this fails (ENOTSUP) we'll continue on
701 * and try a regular unmount.
702 */
703 if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
704 if (umount2(path, MS_FORCE) == 0)
705 unmounted = B_TRUE;
706 }
707 /*
708 * Try forced unmount if we're stuck.
709 */
710 if (stuck) {
711 if (umount2(path, MS_FORCE) == 0) {
712 unmounted = B_TRUE;
713 stuck = B_FALSE;
714 } else {
715 /*
716 * The first failure indicates a
717 * mount we won't be able to get
718 * rid of automatically, so we
719 * bail.
720 */
721 error++;
722 zerror(zlogp, B_FALSE,
723 "unable to unmount '%s'", path);
724 free_mnttable(mnts, nmnt);
725 goto out;
726 }
727 }
728 /*
729 * Try regular unmounts for everything else.
730 */
731 if (!unmounted && umount2(path, 0) != 0)
732 newcount++;
733 }
734 free_mnttable(mnts, nmnt);
735
736 if (newcount == 0)
737 break;
738 if (newcount >= oldcount) {
739 /*
740 * Last round didn't unmount anything; we're stuck and
741 * should start trying forced unmounts.
742 */
743 stuck = B_TRUE;
744 }
745 oldcount = newcount;
746
747 /*
748 * Autofs doesn't let you unmount its trigger nodes from
749 * userland so we have to tell the kernel to cleanup for us.
750 */
751 if (autofs_cleanup(zoneid) != 0) {
752 zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
753 error++;
754 goto out;
755 }
756 }
757
758 out:
759 free_remote_fstypes(remote_fstypes);
760 (void) fclose(mnttab);
761 return (error ? -1 : 0);
762 }
763
764 static int
765 fs_compare(const void *m1, const void *m2)
766 {
767 struct zone_fstab *i = (struct zone_fstab *)m1;
768 struct zone_fstab *j = (struct zone_fstab *)m2;
769
770 return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
771 }
772
773 /*
774 * Fork and exec (and wait for) the mentioned binary with the provided
775 * arguments. Returns (-1) if something went wrong with fork(2) or exec(2),
776 * returns the exit status otherwise.
777 *
778 * If we were unable to exec the provided pathname (for whatever
779 * reason), we return the special token ZEXIT_EXEC. The current value
780 * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
781 * consumers of this function; any future consumers must make sure this
782 * remains the case.
783 */
784 static int
785 forkexec(zlog_t *zlogp, const char *path, char *const argv[])
786 {
787 pid_t child_pid;
788 int child_status = 0;
789
790 /*
791 * Do not let another thread localize a message while we are forking.
792 */
793 (void) mutex_lock(&msglock);
794 child_pid = fork();
795 (void) mutex_unlock(&msglock);
796 if (child_pid == -1) {
797 zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
798 return (-1);
799 } else if (child_pid == 0) {
800 closefrom(0);
801 /* redirect stdin, stdout & stderr to /dev/null */
802 (void) open("/dev/null", O_RDONLY); /* stdin */
803 (void) open("/dev/null", O_WRONLY); /* stdout */
804 (void) open("/dev/null", O_WRONLY); /* stderr */
805 (void) execv(path, argv);
806 /*
807 * Since we are in the child, there is no point calling zerror()
808 * since there is nobody waiting to consume it. So exit with a
809 * special code that the parent will recognize and call zerror()
810 * accordingly.
811 */
812
813 _exit(ZEXIT_EXEC);
814 } else {
815 (void) waitpid(child_pid, &child_status, 0);
816 }
817
818 if (WIFSIGNALED(child_status)) {
819 zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
820 "signal %d", path, WTERMSIG(child_status));
821 return (-1);
822 }
823 assert(WIFEXITED(child_status));
824 if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
825 zerror(zlogp, B_FALSE, "failed to exec %s", path);
826 return (-1);
827 }
828 return (WEXITSTATUS(child_status));
829 }
830
831 static int
832 isregfile(const char *path)
833 {
834 struct stat64 st;
835
836 if (stat64(path, &st) == -1)
837 return (-1);
838
839 return (S_ISREG(st.st_mode));
840 }
841
842 static int
843 dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
844 {
845 char cmdbuf[MAXPATHLEN];
846 char *argv[5];
847 int status;
848
849 /*
850 * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
851 * that would cost us an extra fork/exec without buying us anything.
852 */
853 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
854 >= sizeof (cmdbuf)) {
855 zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
856 return (-1);
857 }
858
859 /*
860 * If it doesn't exist, that's OK: we verified this previously
861 * in zoneadm.
862 */
863 if (isregfile(cmdbuf) == -1)
864 return (0);
865
866 argv[0] = "fsck";
867 argv[1] = "-o";
868 argv[2] = "p";
869 argv[3] = (char *)rawdev;
870 argv[4] = NULL;
871
872 status = forkexec(zlogp, cmdbuf, argv);
873 if (status == 0 || status == -1)
874 return (status);
875 zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
876 "run fsck manually", rawdev, status);
877 return (-1);
878 }
879
880 static int
881 domount(zlog_t *zlogp, const char *fstype, const char *opts,
882 const char *special, const char *directory)
883 {
884 char cmdbuf[MAXPATHLEN];
885 char *argv[6];
886 int status;
887
888 /*
889 * We could alternatively have called /usr/sbin/mount -F <fstype>, but
890 * that would cost us an extra fork/exec without buying us anything.
891 */
892 if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
893 >= sizeof (cmdbuf)) {
894 zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
895 return (-1);
896 }
897 argv[0] = "mount";
898 if (opts[0] == '\0') {
899 argv[1] = (char *)special;
900 argv[2] = (char *)directory;
901 argv[3] = NULL;
902 } else {
903 argv[1] = "-o";
904 argv[2] = (char *)opts;
905 argv[3] = (char *)special;
906 argv[4] = (char *)directory;
907 argv[5] = NULL;
908 }
909
910 status = forkexec(zlogp, cmdbuf, argv);
911 if (status == 0 || status == -1)
912 return (status);
913 if (opts[0] == '\0')
914 zerror(zlogp, B_FALSE, "\"%s %s %s\" "
915 "failed with exit code %d",
916 cmdbuf, special, directory, status);
917 else
918 zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
919 "failed with exit code %d",
920 cmdbuf, opts, special, directory, status);
921 return (-1);
922 }
923
924 /*
925 * Check if a given mount point path exists.
926 * If it does, make sure it doesn't contain any symlinks.
927 * Note that if "leaf" is false we're checking an intermediate
928 * component of the mount point path, so it must be a directory.
929 * If "leaf" is true, then we're checking the entire mount point
930 * path, so the mount point itself can be anything aside from a
931 * symbolic link.
932 *
933 * If the path is invalid then a negative value is returned. If the
934 * path exists and is a valid mount point path then 0 is returned.
935 * If the path doesn't exist return a positive value.
936 */
937 static int
938 valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
939 {
940 struct stat statbuf;
941 char respath[MAXPATHLEN];
942 int res;
943
944 if (lstat(path, &statbuf) != 0) {
945 if (errno == ENOENT)
946 return (1);
947 zerror(zlogp, B_TRUE, "can't stat %s", path);
948 return (-1);
949 }
950 if (S_ISLNK(statbuf.st_mode)) {
951 zerror(zlogp, B_FALSE, "%s is a symlink", path);
952 return (-1);
953 }
954 if (!leaf && !S_ISDIR(statbuf.st_mode)) {
955 zerror(zlogp, B_FALSE, "%s is not a directory", path);
956 return (-1);
957 }
958 if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
959 zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
960 return (-1);
961 }
962 respath[res] = '\0';
963 if (strcmp(path, respath) != 0) {
964 /*
965 * We don't like ".."s, "."s, or "//"s throwing us off
966 */
967 zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
968 return (-1);
969 }
970 return (0);
971 }
972
973 /*
974 * Validate a mount point path. A valid mount point path is an
975 * absolute path that either doesn't exist, or, if it does exists it
976 * must be an absolute canonical path that doesn't have any symbolic
977 * links in it. The target of a mount point path can be any filesystem
978 * object. (Different filesystems can support different mount points,
979 * for example "lofs" and "mntfs" both support files and directories
980 * while "ufs" just supports directories.)
981 *
982 * If the path is invalid then a negative value is returned. If the
983 * path exists and is a valid mount point path then 0 is returned.
984 * If the path doesn't exist return a positive value.
985 */
986 int
987 valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
988 const char *dir, const char *fstype)
989 {
990 char abspath[MAXPATHLEN], *slashp, *slashp_next;
991 int rv;
992
993 /*
994 * Sanity check the target mount point path.
995 * It must be a non-null string that starts with a '/'.
996 */
997 if (dir[0] != '/') {
998 /* Something went wrong. */
999 zerror(zlogp, B_FALSE, "invalid mount directory, "
1000 "type: \"%s\", special: \"%s\", dir: \"%s\"",
1001 fstype, spec, dir);
1002 return (-1);
1003 }
1004
1005 /*
1006 * Join rootpath and dir. Make sure abspath ends with '/', this
1007 * is added to all paths (even non-directory paths) to allow us
1008 * to detect the end of paths below. If the path already ends
1009 * in a '/', then that's ok too (although we'll fail the
1010 * cannonical path check in valid_mount_point()).
1011 */
1012 if (snprintf(abspath, sizeof (abspath),
1013 "%s%s/", rootpath, dir) >= sizeof (abspath)) {
1014 zerror(zlogp, B_FALSE, "pathname %s%s is too long",
1015 rootpath, dir);
1016 return (-1);
1017 }
1018
1019 /*
1020 * Starting with rootpath, verify the mount path one component
1021 * at a time. Continue until we've evaluated all of abspath.
1022 */
1023 slashp = &abspath[strlen(rootpath)];
1024 assert(*slashp == '/');
1025 do {
1026 slashp_next = strchr(slashp + 1, '/');
1027 *slashp = '\0';
1028 if (slashp_next != NULL) {
1029 /* This is an intermediary mount path component. */
1030 rv = valid_mount_point(zlogp, abspath, B_FALSE);
1031 } else {
1032 /* This is the last component of the mount path. */
1033 rv = valid_mount_point(zlogp, abspath, B_TRUE);
1034 }
1035 if (rv < 0)
1036 return (rv);
1037 *slashp = '/';
1038 } while ((slashp = slashp_next) != NULL);
1039 return (rv);
1040 }
1041
1042 static int
1043 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
1044 {
1045 di_prof_t prof = arg;
1046
1047 if (name == NULL)
1048 return (di_prof_add_dev(prof, match));
1049 return (di_prof_add_map(prof, match, name));
1050 }
1051
1052 static int
1053 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
1054 {
1055 di_prof_t prof = arg;
1056
1057 return (di_prof_add_symlink(prof, source, target));
1058 }
1059
1060 int
1061 vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1062 {
1063 zone_dochandle_t handle;
1064
1065 if ((handle = zonecfg_init_handle()) == NULL) {
1066 zerror(zlogp, B_TRUE, "getting zone configuration handle");
1067 return (-1);
1068 }
1069 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1070 zerror(zlogp, B_FALSE, "invalid configuration");
1071 zonecfg_fini_handle(handle);
1072 return (-1);
1073 }
1074 if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
1075 zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1076 zonecfg_fini_handle(handle);
1077 return (-1);
1078 }
1079 zonecfg_fini_handle(handle);
1080 return (0);
1081 }
1082
1083 /*
1084 * Apply the standard lists of devices/symlinks/mappings and the user-specified
1085 * list of devices (via zonecfg) to the /dev filesystem. The filesystem will
1086 * use these as a profile/filter to determine what exists in /dev.
1087 */
1088 static int
1089 mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
1090 {
1091 char brand[MAXNAMELEN];
1092 zone_dochandle_t handle = NULL;
1093 brand_handle_t bh = NULL;
1094 struct zone_devtab ztab;
1095 di_prof_t prof = NULL;
1096 int err;
1097 int retval = -1;
1098 zone_iptype_t iptype;
1099 const char *curr_iptype;
1100
1101 if (di_prof_init(devpath, &prof)) {
1102 zerror(zlogp, B_TRUE, "failed to initialize profile");
1103 goto cleanup;
1104 }
1105
1106 /*
1107 * Get a handle to the brand info for this zone.
1108 * If we are mounting the zone, then we must always use the default
1109 * brand device mounts.
1110 */
1111 if (ALT_MOUNT(mount_cmd)) {
1112 (void) strlcpy(brand, default_brand, sizeof (brand));
1113 } else {
1114 (void) strlcpy(brand, brand_name, sizeof (brand));
1115 }
1116
1117 if ((bh = brand_open(brand)) == NULL) {
1118 zerror(zlogp, B_FALSE, "unable to determine zone brand");
1119 goto cleanup;
1120 }
1121
1122 if (vplat_get_iptype(zlogp, &iptype) < 0) {
1123 zerror(zlogp, B_TRUE, "unable to determine ip-type");
1124 goto cleanup;
1125 }
1126 switch (iptype) {
1127 case ZS_SHARED:
1128 curr_iptype = "shared";
1129 break;
1130 case ZS_EXCLUSIVE:
1131 curr_iptype = "exclusive";
1132 break;
1133 }
1134
1135 if (brand_platform_iter_devices(bh, zone_name,
1136 mount_one_dev_device_cb, prof, curr_iptype) != 0) {
1137 zerror(zlogp, B_TRUE, "failed to add standard device");
1138 goto cleanup;
1139 }
1140
1141 if (brand_platform_iter_link(bh,
1142 mount_one_dev_symlink_cb, prof) != 0) {
1143 zerror(zlogp, B_TRUE, "failed to add standard symlink");
1144 goto cleanup;
1145 }
1146
1147 /* Add user-specified devices and directories */
1148 if ((handle = zonecfg_init_handle()) == NULL) {
1149 zerror(zlogp, B_FALSE, "can't initialize zone handle");
1150 goto cleanup;
1151 }
1152 if (err = zonecfg_get_handle(zone_name, handle)) {
1153 zerror(zlogp, B_FALSE, "can't get handle for zone "
1154 "%s: %s", zone_name, zonecfg_strerror(err));
1155 goto cleanup;
1156 }
1157 if (err = zonecfg_setdevent(handle)) {
1158 zerror(zlogp, B_FALSE, "%s: %s", zone_name,
1159 zonecfg_strerror(err));
1160 goto cleanup;
1161 }
1162 while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
1163 if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
1164 zerror(zlogp, B_TRUE, "failed to add "
1165 "user-specified device");
1166 goto cleanup;
1167 }
1168 }
1169 (void) zonecfg_enddevent(handle);
1170
1171 /* Send profile to kernel */
1172 if (di_prof_commit(prof)) {
1173 zerror(zlogp, B_TRUE, "failed to commit profile");
1174 goto cleanup;
1175 }
1176
1177 retval = 0;
1178
1179 cleanup:
1180 if (bh != NULL)
1181 brand_close(bh);
1182 if (handle != NULL)
1183 zonecfg_fini_handle(handle);
1184 if (prof)
1185 di_prof_fini(prof);
1186 return (retval);
1187 }
1188
1189 static int
1190 mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
1191 zone_mnt_t mount_cmd)
1192 {
1193 char path[MAXPATHLEN];
1194 char optstr[MAX_MNTOPT_STR];
1195 zone_fsopt_t *optptr;
1196 int rv;
1197
1198 if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1199 fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
1200 zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
1201 rootpath, fsptr->zone_fs_dir);
1202 return (-1);
1203 } else if (rv > 0) {
1204 /* The mount point path doesn't exist, create it now. */
1205 if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1206 DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1207 DEFAULT_DIR_GROUP) != 0) {
1208 zerror(zlogp, B_FALSE, "failed to create mount point");
1209 return (-1);
1210 }
1211
1212 /*
1213 * Now this might seem weird, but we need to invoke
1214 * valid_mount_path() again. Why? Because it checks
1215 * to make sure that the mount point path is canonical,
1216 * which it can only do if the path exists, so now that
1217 * we've created the path we have to verify it again.
1218 */
1219 if ((rv = valid_mount_path(zlogp, rootpath,
1220 fsptr->zone_fs_special, fsptr->zone_fs_dir,
1221 fsptr->zone_fs_type)) < 0) {
1222 zerror(zlogp, B_FALSE,
1223 "%s%s is not a valid mount point",
1224 rootpath, fsptr->zone_fs_dir);
1225 return (-1);
1226 }
1227 }
1228
1229 (void) snprintf(path, sizeof (path), "%s%s", rootpath,
1230 fsptr->zone_fs_dir);
1231
1232 /*
1233 * In general the strategy here is to do just as much verification as
1234 * necessary to avoid crashing or otherwise doing something bad; if the
1235 * administrator initiated the operation via zoneadm(1m), he'll get
1236 * auto-verification which will let him know what's wrong. If he
1237 * modifies the zone configuration of a running zone and doesn't attempt
1238 * to verify that it's OK we won't crash but won't bother trying to be
1239 * too helpful either. zoneadm verify is only a couple keystrokes away.
1240 */
1241 if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
1242 zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
1243 "invalid file-system type %s", fsptr->zone_fs_special,
1244 fsptr->zone_fs_dir, fsptr->zone_fs_type);
1245 return (-1);
1246 }
1247
1248 /*
1249 * If we're looking at an alternate root environment, then construct
1250 * read-only loopback mounts as necessary. Note that any special
1251 * paths for lofs zone mounts in an alternate root must have
1252 * already been pre-pended with any alternate root path by the
1253 * time we get here.
1254 */
1255 if (zonecfg_in_alt_root()) {
1256 struct stat64 st;
1257
1258 if (stat64(fsptr->zone_fs_special, &st) != -1 &&
1259 S_ISBLK(st.st_mode)) {
1260 /*
1261 * If we're going to mount a block device we need
1262 * to check if that device is already mounted
1263 * somewhere else, and if so, do a lofs mount
1264 * of the device instead of a direct mount
1265 */
1266 if (check_lofs_needed(zlogp, fsptr) == -1)
1267 return (-1);
1268 } else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1269 /*
1270 * For lofs mounts, the special node is inside the
1271 * alternate root. We need lofs resolution for
1272 * this case in order to get at the underlying
1273 * read-write path.
1274 */
1275 resolve_lofs(zlogp, fsptr->zone_fs_special,
1276 sizeof (fsptr->zone_fs_special));
1277 }
1278 }
1279
1280 /*
1281 * Run 'fsck -m' if there's a device to fsck.
1282 */
1283 if (fsptr->zone_fs_raw[0] != '\0' &&
1284 dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
1285 return (-1);
1286 } else if (isregfile(fsptr->zone_fs_special) == 1 &&
1287 dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
1288 return (-1);
1289 }
1290
1291 /*
1292 * Build up mount option string.
1293 */
1294 optstr[0] = '\0';
1295 if (fsptr->zone_fs_options != NULL) {
1296 (void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
1297 sizeof (optstr));
1298 for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
1299 optptr != NULL; optptr = optptr->zone_fsopt_next) {
1300 (void) strlcat(optstr, ",", sizeof (optstr));
1301 (void) strlcat(optstr, optptr->zone_fsopt_opt,
1302 sizeof (optstr));
1303 }
1304 }
1305
1306 if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
1307 fsptr->zone_fs_special, path)) != 0)
1308 return (rv);
1309
1310 /*
1311 * The mount succeeded. If this was not a mount of /dev then
1312 * we're done.
1313 */
1314 if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
1315 return (0);
1316
1317 /*
1318 * We just mounted an instance of a /dev filesystem, so now we
1319 * need to configure it.
1320 */
1321 return (mount_one_dev(zlogp, path, mount_cmd));
1322 }
1323
1324 static void
1325 free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
1326 {
1327 uint_t i;
1328
1329 if (fsarray == NULL)
1330 return;
1331 for (i = 0; i < nelem; i++)
1332 zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
1333 free(fsarray);
1334 }
1335
1336 /*
1337 * This function initiates the creation of a small Solaris Environment for
1338 * scratch zone. The Environment creation process is split up into two
1339 * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1340 * is done this way because:
1341 * We need to have both /etc and /var in the root of the scratchzone.
1342 * We loopback mount zone's own /etc and /var into the root of the
1343 * scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1344 * need to delay the mount of /var till the zone's root gets populated.
1345 * So mounting of localdirs[](/etc and /var) have been moved to the
1346 * build_mounted_post_var() which gets called only after the zone
1347 * specific filesystems are mounted.
1348 *
1349 * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
1350 * does not loopback mount the zone's own /etc and /var into the root of the
1351 * scratch zone.
1352 */
1353 static boolean_t
1354 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
1355 size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1356 {
1357 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1358 const char **cpp;
1359 static const char *mkdirs[] = {
1360 "/system", "/system/contract", "/system/object", "/proc",
1361 "/dev", "/tmp", "/a", NULL
1362 };
1363 char *altstr;
1364 FILE *fp;
1365 uuid_t uuid;
1366
1367 resolve_lofs(zlogp, rootpath, rootlen);
1368 (void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
1369 resolve_lofs(zlogp, luroot, lurootlen);
1370 (void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1371 (void) symlink("./usr/bin", tmp);
1372
1373 /*
1374 * These are mostly special mount points; not handled here. (See
1375 * zone_mount_early.)
1376 */
1377 for (cpp = mkdirs; *cpp != NULL; cpp++) {
1378 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1379 if (mkdir(tmp, 0755) != 0) {
1380 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1381 return (B_FALSE);
1382 }
1383 }
1384 /*
1385 * This is here to support lucopy. If there's an instance of this same
1386 * zone on the current running system, then we mount its root up as
1387 * read-only inside the scratch zone.
1388 */
1389 (void) zonecfg_get_uuid(zone_name, uuid);
1390 altstr = strdup(zonecfg_get_root());
1391 if (altstr == NULL) {
1392 zerror(zlogp, B_TRUE, "memory allocation failed");
1393 return (B_FALSE);
1394 }
1395 zonecfg_set_root("");
1396 (void) strlcpy(tmp, zone_name, sizeof (tmp));
1397 (void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1398 if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1399 strcmp(fromdir, rootpath) != 0) {
1400 (void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1401 if (mkdir(tmp, 0755) != 0) {
1402 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1403 return (B_FALSE);
1404 }
1405 if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, fromdir,
1406 tmp) != 0) {
1407 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1408 fromdir);
1409 return (B_FALSE);
1410 }
1411 }
1412 zonecfg_set_root(altstr);
1413 free(altstr);
1414
1415 if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1416 zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1417 return (B_FALSE);
1418 }
1419 (void) ftruncate(fileno(fp), 0);
1420 if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1421 zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1422 }
1423 zonecfg_close_scratch(fp);
1424 (void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1425 if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1426 return (B_FALSE);
1427 (void) strlcpy(rootpath, tmp, rootlen);
1428 return (B_TRUE);
1429 }
1430
1431
1432 static boolean_t
1433 build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
1434 const char *luroot)
1435 {
1436 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1437 const char **cpp;
1438 const char **loopdirs;
1439 const char **tmpdirs;
1440 static const char *localdirs[] = {
1441 "/etc", "/var", NULL
1442 };
1443 static const char *scr_loopdirs[] = {
1444 "/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1445 "/usr", NULL
1446 };
1447 static const char *upd_loopdirs[] = {
1448 "/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
1449 "/usr", "/var", NULL
1450 };
1451 static const char *scr_tmpdirs[] = {
1452 "/tmp", "/var/run", NULL
1453 };
1454 static const char *upd_tmpdirs[] = {
1455 "/tmp", "/var/run", "/var/tmp", NULL
1456 };
1457 struct stat st;
1458
1459 if (mount_cmd == Z_MNT_SCRATCH) {
1460 /*
1461 * These are mounted read-write from the zone undergoing
1462 * upgrade. We must be careful not to 'leak' things from the
1463 * main system into the zone, and this accomplishes that goal.
1464 */
1465 for (cpp = localdirs; *cpp != NULL; cpp++) {
1466 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
1467 *cpp);
1468 (void) snprintf(fromdir, sizeof (fromdir), "%s%s",
1469 rootpath, *cpp);
1470 if (mkdir(tmp, 0755) != 0) {
1471 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1472 return (B_FALSE);
1473 }
1474 if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
1475 != 0) {
1476 zerror(zlogp, B_TRUE, "cannot mount %s on %s",
1477 tmp, *cpp);
1478 return (B_FALSE);
1479 }
1480 }
1481 }
1482
1483 if (mount_cmd == Z_MNT_UPDATE)
1484 loopdirs = upd_loopdirs;
1485 else
1486 loopdirs = scr_loopdirs;
1487
1488 /*
1489 * These are things mounted read-only from the running system because
1490 * they contain binaries that must match system.
1491 */
1492 for (cpp = loopdirs; *cpp != NULL; cpp++) {
1493 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1494 if (mkdir(tmp, 0755) != 0) {
1495 if (errno != EEXIST) {
1496 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1497 return (B_FALSE);
1498 }
1499 if (lstat(tmp, &st) != 0) {
1500 zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1501 return (B_FALSE);
1502 }
1503 /*
1504 * Ignore any non-directories encountered. These are
1505 * things that have been converted into symlinks
1506 * (/etc/fs and /etc/lib) and no longer need a lofs
1507 * fixup.
1508 */
1509 if (!S_ISDIR(st.st_mode))
1510 continue;
1511 }
1512 if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, *cpp,
1513 tmp) != 0) {
1514 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1515 *cpp);
1516 return (B_FALSE);
1517 }
1518 }
1519
1520 if (mount_cmd == Z_MNT_UPDATE)
1521 tmpdirs = upd_tmpdirs;
1522 else
1523 tmpdirs = scr_tmpdirs;
1524
1525 /*
1526 * These are things with tmpfs mounted inside.
1527 */
1528 for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1529 (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1530 if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
1531 errno != EEXIST) {
1532 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1533 return (B_FALSE);
1534 }
1535
1536 /*
1537 * We could set the mode for /tmp when we do the mkdir but
1538 * since that can be modified by the umask we will just set
1539 * the correct mode for /tmp now.
1540 */
1541 if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1542 zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1543 return (B_FALSE);
1544 }
1545
1546 if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1547 zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1548 return (B_FALSE);
1549 }
1550 }
1551 return (B_TRUE);
1552 }
1553
1554 typedef struct plat_gmount_cb_data {
1555 zlog_t *pgcd_zlogp;
1556 struct zone_fstab **pgcd_fs_tab;
1557 int *pgcd_num_fs;
1558 } plat_gmount_cb_data_t;
1559
1560 /*
1561 * plat_gmount_cb() is a callback function invoked by libbrand to iterate
1562 * through all global brand platform mounts.
1563 */
1564 int
1565 plat_gmount_cb(void *data, const char *spec, const char *dir,
1566 const char *fstype, const char *opt)
1567 {
1568 plat_gmount_cb_data_t *cp = data;
1569 zlog_t *zlogp = cp->pgcd_zlogp;
1570 struct zone_fstab *fs_ptr = *cp->pgcd_fs_tab;
1571 int num_fs = *cp->pgcd_num_fs;
1572 struct zone_fstab *fsp, *tmp_ptr;
1573
1574 num_fs++;
1575 if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
1576 zerror(zlogp, B_TRUE, "memory allocation failed");
1577 return (-1);
1578 }
1579
1580 fs_ptr = tmp_ptr;
1581 fsp = &fs_ptr[num_fs - 1];
1582
1583 /* update the callback struct passed in */
1584 *cp->pgcd_fs_tab = fs_ptr;
1585 *cp->pgcd_num_fs = num_fs;
1586
1587 fsp->zone_fs_raw[0] = '\0';
1588 (void) strlcpy(fsp->zone_fs_special, spec,
1589 sizeof (fsp->zone_fs_special));
1590 (void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
1591 (void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
1592 fsp->zone_fs_options = NULL;
1593 if ((opt != NULL) &&
1594 (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
1595 zerror(zlogp, B_FALSE, "error adding property");
1596 return (-1);
1597 }
1598
1599 return (0);
1600 }
1601
1602 static int
1603 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
1604 struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
1605 {
1606 struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
1607 int num_fs;
1608
1609 num_fs = *num_fsp;
1610 fs_ptr = *fs_tabp;
1611
1612 if (zonecfg_setfsent(handle) != Z_OK) {
1613 zerror(zlogp, B_FALSE, "invalid configuration");
1614 return (-1);
1615 }
1616 while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
1617 /*
1618 * ZFS filesystems will not be accessible under an alternate
1619 * root, since the pool will not be known. Ignore them in this
1620 * case.
1621 */
1622 if (ALT_MOUNT(mount_cmd) &&
1623 strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
1624 continue;
1625
1626 num_fs++;
1627 if ((tmp_ptr = realloc(fs_ptr,
1628 num_fs * sizeof (*tmp_ptr))) == NULL) {
1629 zerror(zlogp, B_TRUE, "memory allocation failed");
1630 (void) zonecfg_endfsent(handle);
1631 return (-1);
1632 }
1633 /* update the pointers passed in */
1634 *fs_tabp = tmp_ptr;
1635 *num_fsp = num_fs;
1636
1637 fs_ptr = tmp_ptr;
1638 fsp = &fs_ptr[num_fs - 1];
1639 (void) strlcpy(fsp->zone_fs_dir,
1640 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
1641 (void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
1642 sizeof (fsp->zone_fs_raw));
1643 (void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
1644 sizeof (fsp->zone_fs_type));
1645 fsp->zone_fs_options = fstab.zone_fs_options;
1646
1647 /*
1648 * For all lofs mounts, make sure that the 'special'
1649 * entry points inside the alternate root. The
1650 * source path for a lofs mount in a given zone needs
1651 * to be relative to the root of the boot environment
1652 * that contains the zone. Note that we don't do this
1653 * for non-lofs mounts since they will have a device
1654 * as a backing store and device paths must always be
1655 * specified relative to the current boot environment.
1656 */
1657 fsp->zone_fs_special[0] = '\0';
1658 if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1659 (void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1660 sizeof (fsp->zone_fs_special));
1661 }
1662 (void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1663 sizeof (fsp->zone_fs_special));
1664 }
1665 (void) zonecfg_endfsent(handle);
1666 return (0);
1667 }
1668
1669 static int
1670 mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
1671 {
1672 char rootpath[MAXPATHLEN];
1673 char zonepath[MAXPATHLEN];
1674 char brand[MAXNAMELEN];
1675 char luroot[MAXPATHLEN];
1676 int i, num_fs = 0;
1677 struct zone_fstab *fs_ptr = NULL;
1678 zone_dochandle_t handle = NULL;
1679 zone_state_t zstate;
1680 brand_handle_t bh;
1681 plat_gmount_cb_data_t cb;
1682
1683 if (zone_get_state(zone_name, &zstate) != Z_OK ||
1684 (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
1685 zerror(zlogp, B_FALSE,
1686 "zone must be in '%s' or '%s' state to mount file-systems",
1687 zone_state_str(ZONE_STATE_READY),
1688 zone_state_str(ZONE_STATE_MOUNTED));
1689 goto bad;
1690 }
1691
1692 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
1693 zerror(zlogp, B_TRUE, "unable to determine zone path");
1694 goto bad;
1695 }
1696
1697 if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
1698 zerror(zlogp, B_TRUE, "unable to determine zone root");
1699 goto bad;
1700 }
1701
1702 if ((handle = zonecfg_init_handle()) == NULL) {
1703 zerror(zlogp, B_TRUE, "getting zone configuration handle");
1704 goto bad;
1705 }
1706 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
1707 zonecfg_setfsent(handle) != Z_OK) {
1708 zerror(zlogp, B_FALSE, "invalid configuration");
1709 goto bad;
1710 }
1711
1712 /*
1713 * If we are mounting the zone, then we must always use the default
1714 * brand global mounts.
1715 */
1716 if (ALT_MOUNT(mount_cmd)) {
1717 (void) strlcpy(brand, default_brand, sizeof (brand));
1718 } else {
1719 (void) strlcpy(brand, brand_name, sizeof (brand));
1720 }
1721
1722 /* Get a handle to the brand info for this zone */
1723 if ((bh = brand_open(brand)) == NULL) {
1724 zerror(zlogp, B_FALSE, "unable to determine zone brand");
1725 zonecfg_fini_handle(handle);
1726 return (-1);
1727 }
1728
1729 /*
1730 * Get the list of global filesystems to mount from the brand
1731 * configuration.
1732 */
1733 cb.pgcd_zlogp = zlogp;
1734 cb.pgcd_fs_tab = &fs_ptr;
1735 cb.pgcd_num_fs = &num_fs;
1736 if (brand_platform_iter_gmounts(bh, zonepath,
1737 plat_gmount_cb, &cb) != 0) {
1738 zerror(zlogp, B_FALSE, "unable to mount filesystems");
1739 brand_close(bh);
1740 zonecfg_fini_handle(handle);
1741 return (-1);
1742 }
1743 brand_close(bh);
1744
1745 /*
1746 * Iterate through the rest of the filesystems. Sort them all,
1747 * then mount them in sorted order. This is to make sure the
1748 * higher level directories (e.g., /usr) get mounted before
1749 * any beneath them (e.g., /usr/local).
1750 */
1751 if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
1752 mount_cmd) != 0)
1753 goto bad;
1754
1755 zonecfg_fini_handle(handle);
1756 handle = NULL;
1757
1758 /*
1759 * Normally when we mount a zone all the zone filesystems
1760 * get mounted relative to rootpath, which is usually
1761 * <zonepath>/root. But when mounting a zone for administration
1762 * purposes via the zone "mount" state, build_mounted_pre_var()
1763 * updates rootpath to be <zonepath>/lu/a so we'll mount all
1764 * the zones filesystems there instead.
1765 *
1766 * build_mounted_pre_var() and build_mounted_post_var() will
1767 * also do some extra work to create directories and lofs mount
1768 * a bunch of global zone file system paths into <zonepath>/lu.
1769 *
1770 * This allows us to be able to enter the zone (now rooted at
1771 * <zonepath>/lu) and run the upgrade/patch tools that are in the
1772 * global zone and have them upgrade the to-be-modified zone's
1773 * files mounted on /a. (Which mirrors the existing standard
1774 * upgrade environment.)
1775 *
1776 * There is of course one catch. When doing the upgrade
1777 * we need <zoneroot>/lu/dev to be the /dev filesystem
1778 * for the zone and we don't want to have any /dev filesystem
1779 * mounted at <zoneroot>/lu/a/dev. Since /dev is specified
1780 * as a normal zone filesystem by default we'll try to mount
1781 * it at <zoneroot>/lu/a/dev, so we have to detect this
1782 * case and instead mount it at <zoneroot>/lu/dev.
1783 *
1784 * All this work is done in three phases:
1785 * 1) Create and populate lu directory (build_mounted_pre_var()).
1786 * 2) Mount the required filesystems as per the zone configuration.
1787 * 3) Set up the rest of the scratch zone environment
1788 * (build_mounted_post_var()).
1789 */
1790 if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
1791 rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1792 goto bad;
1793
1794 qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
1795
1796 for (i = 0; i < num_fs; i++) {
1797 if (ALT_MOUNT(mount_cmd) &&
1798 strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
1799 size_t slen = strlen(rootpath) - 2;
1800
1801 /*
1802 * By default we'll try to mount /dev as /a/dev
1803 * but /dev is special and always goes at the top
1804 * so strip the trailing '/a' from the rootpath.
1805 */
1806 assert(strcmp(&rootpath[slen], "/a") == 0);
1807 rootpath[slen] = '\0';
1808 if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd)
1809 != 0)
1810 goto bad;
1811 rootpath[slen] = '/';
1812 continue;
1813 }
1814 if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
1815 goto bad;
1816 }
1817 if (ALT_MOUNT(mount_cmd) &&
1818 !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
1819 goto bad;
1820
1821 /*
1822 * For Trusted Extensions cross-mount each lower level /export/home
1823 */
1824 if (mount_cmd == Z_MNT_BOOT &&
1825 tsol_mounts(zlogp, zone_name, rootpath) != 0)
1826 goto bad;
1827
1828 free_fs_data(fs_ptr, num_fs);
1829
1830 /*
1831 * Everything looks fine.
1832 */
1833 return (0);
1834
1835 bad:
1836 if (handle != NULL)
1837 zonecfg_fini_handle(handle);
1838 free_fs_data(fs_ptr, num_fs);
1839 return (-1);
1840 }
1841
1842 /* caller makes sure neither parameter is NULL */
1843 static int
1844 addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
1845 {
1846 int prefixlen;
1847
1848 prefixlen = atoi(prefixstr);
1849 if (prefixlen < 0 || prefixlen > maxprefixlen)
1850 return (1);
1851 while (prefixlen > 0) {
1852 if (prefixlen >= 8) {
1853 *maskstr++ = 0xFF;
1854 prefixlen -= 8;
1855 continue;
1856 }
1857 *maskstr |= 1 << (8 - prefixlen);
1858 prefixlen--;
1859 }
1860 return (0);
1861 }
1862
1863 /*
1864 * Tear down all interfaces belonging to the given zone. This should
1865 * be called with the zone in a state other than "running", so that
1866 * interfaces can't be assigned to the zone after this returns.
1867 *
1868 * If anything goes wrong, log an error message and return an error.
1869 */
1870 static int
1871 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
1872 {
1873 struct lifnum lifn;
1874 struct lifconf lifc;
1875 struct lifreq *lifrp, lifrl;
1876 int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
1877 int num_ifs, s, i, ret_code = 0;
1878 uint_t bufsize;
1879 char *buf = NULL;
1880
1881 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1882 zerror(zlogp, B_TRUE, "could not get socket");
1883 ret_code = -1;
1884 goto bad;
1885 }
1886 lifn.lifn_family = AF_UNSPEC;
1887 lifn.lifn_flags = (int)lifc_flags;
1888 if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
1889 zerror(zlogp, B_TRUE,
1890 "could not determine number of network interfaces");
1891 ret_code = -1;
1892 goto bad;
1893 }
1894 num_ifs = lifn.lifn_count;
1895 bufsize = num_ifs * sizeof (struct lifreq);
1896 if ((buf = malloc(bufsize)) == NULL) {
1897 zerror(zlogp, B_TRUE, "memory allocation failed");
1898 ret_code = -1;
1899 goto bad;
1900 }
1901 lifc.lifc_family = AF_UNSPEC;
1902 lifc.lifc_flags = (int)lifc_flags;
1903 lifc.lifc_len = bufsize;
1904 lifc.lifc_buf = buf;
1905 if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1906 zerror(zlogp, B_TRUE, "could not get configured network "
1907 "interfaces");
1908 ret_code = -1;
1909 goto bad;
1910 }
1911 lifrp = lifc.lifc_req;
1912 for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
1913 (void) close(s);
1914 if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
1915 0) {
1916 zerror(zlogp, B_TRUE, "%s: could not get socket",
1917 lifrl.lifr_name);
1918 ret_code = -1;
1919 continue;
1920 }
1921 (void) memset(&lifrl, 0, sizeof (lifrl));
1922 (void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
1923 sizeof (lifrl.lifr_name));
1924 if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1925 if (errno == ENXIO)
1926 /*
1927 * Interface may have been removed by admin or
1928 * another zone halting.
1929 */
1930 continue;
1931 zerror(zlogp, B_TRUE,
1932 "%s: could not determine the zone to which this "
1933 "network interface is bound", lifrl.lifr_name);
1934 ret_code = -1;
1935 continue;
1936 }
1937 if (lifrl.lifr_zoneid == zone_id) {
1938 if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
1939 zerror(zlogp, B_TRUE,
1940 "%s: could not remove network interface",
1941 lifrl.lifr_name);
1942 ret_code = -1;
1943 continue;
1944 }
1945 }
1946 }
1947 bad:
1948 if (s > 0)
1949 (void) close(s);
1950 if (buf)
1951 free(buf);
1952 return (ret_code);
1953 }
1954
1955 static union sockunion {
1956 struct sockaddr sa;
1957 struct sockaddr_in sin;
1958 struct sockaddr_dl sdl;
1959 struct sockaddr_in6 sin6;
1960 } so_dst, so_ifp;
1961
1962 static struct {
1963 struct rt_msghdr hdr;
1964 char space[512];
1965 } rtmsg;
1966
1967 static int
1968 salen(struct sockaddr *sa)
1969 {
1970 switch (sa->sa_family) {
1971 case AF_INET:
1972 return (sizeof (struct sockaddr_in));
1973 case AF_LINK:
1974 return (sizeof (struct sockaddr_dl));
1975 case AF_INET6:
1976 return (sizeof (struct sockaddr_in6));
1977 default:
1978 return (sizeof (struct sockaddr));
1979 }
1980 }
1981
1982 #define ROUNDUP_LONG(a) \
1983 ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
1984
1985 /*
1986 * Look up which zone is using a given IP address. The address in question
1987 * is expected to have been stuffed into the structure to which lifr points
1988 * via a previous SIOCGLIFADDR ioctl().
1989 *
1990 * This is done using black router socket magic.
1991 *
1992 * Return the name of the zone on success or NULL on failure.
1993 *
1994 * This is a lot of code for a simple task; a new ioctl request to take care
1995 * of this might be a useful RFE.
1996 */
1997
1998 static char *
1999 who_is_using(zlog_t *zlogp, struct lifreq *lifr)
2000 {
2001 static char answer[ZONENAME_MAX];
2002 pid_t pid;
2003 int s, rlen, l, i;
2004 char *cp = rtmsg.space;
2005 struct sockaddr_dl *ifp = NULL;
2006 struct sockaddr *sa;
2007 char save_if_name[LIFNAMSIZ];
2008
2009 answer[0] = '\0';
2010
2011 pid = getpid();
2012 if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
2013 zerror(zlogp, B_TRUE, "could not get routing socket");
2014 return (NULL);
2015 }
2016
2017 if (lifr->lifr_addr.ss_family == AF_INET) {
2018 struct sockaddr_in *sin4;
2019
2020 so_dst.sa.sa_family = AF_INET;
2021 sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
2022 so_dst.sin.sin_addr = sin4->sin_addr;
2023 } else {
2024 struct sockaddr_in6 *sin6;
2025
2026 so_dst.sa.sa_family = AF_INET6;
2027 sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
2028 so_dst.sin6.sin6_addr = sin6->sin6_addr;
2029 }
2030
2031 so_ifp.sa.sa_family = AF_LINK;
2032
2033 (void) memset(&rtmsg, 0, sizeof (rtmsg));
2034 rtmsg.hdr.rtm_type = RTM_GET;
2035 rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
2036 rtmsg.hdr.rtm_version = RTM_VERSION;
2037 rtmsg.hdr.rtm_seq = ++rts_seqno;
2038 rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
2039
2040 l = ROUNDUP_LONG(salen(&so_dst.sa));
2041 (void) memmove(cp, &(so_dst), l);
2042 cp += l;
2043 l = ROUNDUP_LONG(salen(&so_ifp.sa));
2044 (void) memmove(cp, &(so_ifp), l);
2045 cp += l;
2046
2047 rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
2048
2049 if ((rlen = write(s, &rtmsg, l)) < 0) {
2050 zerror(zlogp, B_TRUE, "writing to routing socket");
2051 return (NULL);
2052 } else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
2053 zerror(zlogp, B_TRUE,
2054 "write to routing socket got only %d for len\n", rlen);
2055 return (NULL);
2056 }
2057 do {
2058 l = read(s, &rtmsg, sizeof (rtmsg));
2059 } while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
2060 rtmsg.hdr.rtm_pid != pid));
2061 if (l < 0) {
2062 zerror(zlogp, B_TRUE, "reading from routing socket");
2063 return (NULL);
2064 }
2065
2066 if (rtmsg.hdr.rtm_version != RTM_VERSION) {
2067 zerror(zlogp, B_FALSE,
2068 "routing message version %d not understood",
2069 rtmsg.hdr.rtm_version);
2070 return (NULL);
2071 }
2072 if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
2073 zerror(zlogp, B_FALSE, "message length mismatch, "
2074 "expected %d bytes, returned %d bytes",
2075 rtmsg.hdr.rtm_msglen, l);
2076 return (NULL);
2077 }
2078 if (rtmsg.hdr.rtm_errno != 0) {
2079 errno = rtmsg.hdr.rtm_errno;
2080 zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
2081 return (NULL);
2082 }
2083 if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2084 zerror(zlogp, B_FALSE, "network interface not found");
2085 return (NULL);
2086 }
2087 cp = ((char *)(&rtmsg.hdr + 1));
2088 for (i = 1; i != 0; i <<= 1) {
2089 /* LINTED E_BAD_PTR_CAST_ALIGN */
2090 sa = (struct sockaddr *)cp;
2091 if (i != RTA_IFP) {
2092 if ((i & rtmsg.hdr.rtm_addrs) != 0)
2093 cp += ROUNDUP_LONG(salen(sa));
2094 continue;
2095 }
2096 if (sa->sa_family == AF_LINK &&
2097 ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
2098 ifp = (struct sockaddr_dl *)sa;
2099 break;
2100 }
2101 if (ifp == NULL) {
2102 zerror(zlogp, B_FALSE, "network interface could not be "
2103 "determined");
2104 return (NULL);
2105 }
2106
2107 /*
2108 * We need to set the I/F name to what we got above, then do the
2109 * appropriate ioctl to get its zone name. But lifr->lifr_name is
2110 * used by the calling function to do a REMOVEIF, so if we leave the
2111 * "good" zone's I/F name in place, *that* I/F will be removed instead
2112 * of the bad one. So we save the old (bad) I/F name before over-
2113 * writing it and doing the ioctl, then restore it after the ioctl.
2114 */
2115 (void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
2116 (void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
2117 lifr->lifr_name[ifp->sdl_nlen] = '\0';
2118 i = ioctl(s, SIOCGLIFZONE, lifr);
2119 (void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
2120 if (i < 0) {
2121 zerror(zlogp, B_TRUE,
2122 "%s: could not determine the zone network interface "
2123 "belongs to", lifr->lifr_name);
2124 return (NULL);
2125 }
2126 if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
2127 (void) snprintf(answer, sizeof (answer), "%d",
2128 lifr->lifr_zoneid);
2129
2130 if (strlen(answer) > 0)
2131 return (answer);
2132 return (NULL);
2133 }
2134
2135 /*
2136 * Configures a single interface: a new virtual interface is added, based on
2137 * the physical interface nwiftabptr->zone_nwif_physical, with the address
2138 * specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that
2139 * the "address" can be an IPv6 address (with a /prefixlength required), an
2140 * IPv4 address (with a /prefixlength optional), or a name; for the latter,
2141 * an IPv4 name-to-address resolution will be attempted.
2142 *
2143 * If anything goes wrong, we log an detailed error message, attempt to tear
2144 * down whatever we set up and return an error.
2145 */
2146 static int
2147 configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
2148 struct zone_nwiftab *nwiftabptr)
2149 {
2150 struct lifreq lifr;
2151 struct sockaddr_in netmask4;
2152 struct sockaddr_in6 netmask6;
2153 struct sockaddr_storage laddr;
2154 struct in_addr in4;
2155 sa_family_t af;
2156 char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
2157 int s;
2158 boolean_t got_netmask = B_FALSE;
2159 boolean_t is_loopback = B_FALSE;
2160 char addrstr4[INET_ADDRSTRLEN];
2161 int res;
2162
2163 res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
2164 if (res != Z_OK) {
2165 zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
2166 nwiftabptr->zone_nwif_address);
2167 return (-1);
2168 }
2169 af = lifr.lifr_addr.ss_family;
2170 if (af == AF_INET)
2171 in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
2172 if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
2173 zerror(zlogp, B_TRUE, "could not get socket");
2174 return (-1);
2175 }
2176
2177 /*
2178 * This is a similar kind of "hack" like in addif() to get around
2179 * the problem of SIOCLIFADDIF. The problem is that this ioctl
2180 * does not include the netmask when adding a logical interface.
2181 * To get around this problem, we first add the logical interface
2182 * with a 0 address. After that, we set the netmask if provided.
2183 * Finally we set the interface address.
2184 */
2185 laddr = lifr.lifr_addr;
2186 (void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
2187 sizeof (lifr.lifr_name));
2188 (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
2189
2190 if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
2191 /*
2192 * Here, we know that the interface can't be brought up.
2193 * A similar warning message was already printed out to
2194 * the console by zoneadm(1M) so instead we log the
2195 * message to syslog and continue.
2196 */
2197 zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
2198 "'%s' which may not be present/plumbed in the "
2199 "global zone.", lifr.lifr_name);
2200 (void) close(s);
2201 return (Z_OK);
2202 }
2203
2204 /* Preserve literal IPv4 address for later potential printing. */
2205 if (af == AF_INET)
2206 (void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
2207
2208 lifr.lifr_zoneid = zone_id;
2209 if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2210 zerror(zlogp, B_TRUE, "%s: could not place network interface "
2211 "into zone", lifr.lifr_name);
2212 goto bad;
2213 }
2214
2215 /*
2216 * Loopback interface will use the default netmask assigned, if no
2217 * netmask is found.
2218 */
2219 if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
2220 is_loopback = B_TRUE;
2221 }
2222 if (af == AF_INET) {
2223 /*
2224 * The IPv4 netmask can be determined either
2225 * directly if a prefix length was supplied with
2226 * the address or via the netmasks database. Not
2227 * being able to determine it is a common failure,
2228 * but it often is not fatal to operation of the
2229 * interface. In that case, a warning will be
2230 * printed after the rest of the interface's
2231 * parameters have been configured.
2232 */
2233 (void) memset(&netmask4, 0, sizeof (netmask4));
2234 if (slashp != NULL) {
2235 if (addr2netmask(slashp + 1, V4_ADDR_LEN,
2236 (uchar_t *)&netmask4.sin_addr) != 0) {
2237 *slashp = '/';
2238 zerror(zlogp, B_FALSE,
2239 "%s: invalid prefix length in %s",
2240 lifr.lifr_name,
2241 nwiftabptr->zone_nwif_address);
2242 goto bad;
2243 }
2244 got_netmask = B_TRUE;
2245 } else if (getnetmaskbyaddr(in4,
2246 &netmask4.sin_addr) == 0) {
2247 got_netmask = B_TRUE;
2248 }
2249 if (got_netmask) {
2250 netmask4.sin_family = af;
2251 (void) memcpy(&lifr.lifr_addr, &netmask4,
2252 sizeof (netmask4));
2253 }
2254 } else {
2255 (void) memset(&netmask6, 0, sizeof (netmask6));
2256 if (addr2netmask(slashp + 1, V6_ADDR_LEN,
2257 (uchar_t *)&netmask6.sin6_addr) != 0) {
2258 *slashp = '/';
2259 zerror(zlogp, B_FALSE,
2260 "%s: invalid prefix length in %s",
2261 lifr.lifr_name,
2262 nwiftabptr->zone_nwif_address);
2263 goto bad;
2264 }
2265 got_netmask = B_TRUE;
2266 netmask6.sin6_family = af;
2267 (void) memcpy(&lifr.lifr_addr, &netmask6,
2268 sizeof (netmask6));
2269 }
2270 if (got_netmask &&
2271 ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
2272 zerror(zlogp, B_TRUE, "%s: could not set netmask",
2273 lifr.lifr_name);
2274 goto bad;
2275 }
2276
2277 /* Set the interface address */
2278 lifr.lifr_addr = laddr;
2279 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
2280 zerror(zlogp, B_TRUE,
2281 "%s: could not set IP address to %s",
2282 lifr.lifr_name, nwiftabptr->zone_nwif_address);
2283 goto bad;
2284 }
2285
2286 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2287 zerror(zlogp, B_TRUE, "%s: could not get flags",
2288 lifr.lifr_name);
2289 goto bad;
2290 }
2291 lifr.lifr_flags |= IFF_UP;
2292 if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
2293 int save_errno = errno;
2294 char *zone_using;
2295
2296 /*
2297 * If we failed with something other than EADDRNOTAVAIL,
2298 * then skip to the end. Otherwise, look up our address,
2299 * then call a function to determine which zone is already
2300 * using that address.
2301 */
2302 if (errno != EADDRNOTAVAIL) {
2303 zerror(zlogp, B_TRUE,
2304 "%s: could not bring network interface up",
2305 lifr.lifr_name);
2306 goto bad;
2307 }
2308 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
2309 zerror(zlogp, B_TRUE, "%s: could not get address",
2310 lifr.lifr_name);
2311 goto bad;
2312 }
2313 zone_using = who_is_using(zlogp, &lifr);
2314 errno = save_errno;
2315 if (zone_using == NULL)
2316 zerror(zlogp, B_TRUE,
2317 "%s: could not bring network interface up",
2318 lifr.lifr_name);
2319 else
2320 zerror(zlogp, B_TRUE, "%s: could not bring network "
2321 "interface up: address in use by zone '%s'",
2322 lifr.lifr_name, zone_using);
2323 goto bad;
2324 }
2325
2326 if (!got_netmask && !is_loopback) {
2327 /*
2328 * A common, but often non-fatal problem, is that the system
2329 * cannot find the netmask for an interface address. This is
2330 * often caused by it being only in /etc/inet/netmasks, but
2331 * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
2332 * in that. This doesn't show up at boot because the netmask
2333 * is obtained from /etc/inet/netmasks when no network
2334 * interfaces are up, but isn't consulted when NIS/NIS+ is
2335 * available. We warn the user here that something like this
2336 * has happened and we're just running with a default and
2337 * possible incorrect netmask.
2338 */
2339 char buffer[INET6_ADDRSTRLEN];
2340 void *addr;
2341 const char *nomatch = "no matching subnet found in netmasks(4)";
2342
2343 if (af == AF_INET)
2344 addr = &((struct sockaddr_in *)
2345 (&lifr.lifr_addr))->sin_addr;
2346 else
2347 addr = &((struct sockaddr_in6 *)
2348 (&lifr.lifr_addr))->sin6_addr;
2349
2350 /*
2351 * Find out what netmask the interface is going to be using.
2352 * If we just brought up an IPMP data address on an underlying
2353 * interface above, the address will have already migrated, so
2354 * the SIOCGLIFNETMASK won't be able to find it (but we need
2355 * to bring the address up to get the actual netmask). Just
2356 * omit printing the actual netmask in this corner-case.
2357 */
2358 if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
2359 inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) {
2360 zerror(zlogp, B_FALSE, "WARNING: %s; using default.",
2361 nomatch);
2362 } else {
2363 zerror(zlogp, B_FALSE,
2364 "WARNING: %s: %s: %s; using default of %s.",
2365 lifr.lifr_name, nomatch, addrstr4, buffer);
2366 }
2367 }
2368
2369 /*
2370 * If a default router was specified for this interface
2371 * set the route now. Ignore if already set.
2372 */
2373 if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
2374 int status;
2375 char *argv[7];
2376
2377 argv[0] = "route";
2378 argv[1] = "add";
2379 argv[2] = "-ifp";
2380 argv[3] = nwiftabptr->zone_nwif_physical;
2381 argv[4] = "default";
2382 argv[5] = nwiftabptr->zone_nwif_defrouter;
2383 argv[6] = NULL;
2384
2385 status = forkexec(zlogp, "/usr/sbin/route", argv);
2386 if (status != 0 && status != EEXIST)
2387 zerror(zlogp, B_FALSE, "Unable to set route for "
2388 "interface %s to %s\n",
2389 nwiftabptr->zone_nwif_physical,
2390 nwiftabptr->zone_nwif_defrouter);
2391 }
2392
2393 (void) close(s);
2394 return (Z_OK);
2395 bad:
2396 (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
2397 (void) close(s);
2398 return (-1);
2399 }
2400
2401 /*
2402 * Sets up network interfaces based on information from the zone configuration.
2403 * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
2404 * system.
2405 *
2406 * If anything goes wrong, we log a general error message, attempt to tear down
2407 * whatever we set up, and return an error.
2408 */
2409 static int
2410 configure_shared_network_interfaces(zlog_t *zlogp)
2411 {
2412 zone_dochandle_t handle;
2413 struct zone_nwiftab nwiftab, loopback_iftab;
2414 zoneid_t zoneid;
2415
2416 if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
2417 zerror(zlogp, B_TRUE, "unable to get zoneid");
2418 return (-1);
2419 }
2420
2421 if ((handle = zonecfg_init_handle()) == NULL) {
2422 zerror(zlogp, B_TRUE, "getting zone configuration handle");
2423 return (-1);
2424 }
2425 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2426 zerror(zlogp, B_FALSE, "invalid configuration");
2427 zonecfg_fini_handle(handle);
2428 return (-1);
2429 }
2430 if (zonecfg_setnwifent(handle) == Z_OK) {
2431 for (;;) {
2432 if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2433 break;
2434 if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
2435 Z_OK) {
2436 (void) zonecfg_endnwifent(handle);
2437 zonecfg_fini_handle(handle);
2438 return (-1);
2439 }
2440 }
2441 (void) zonecfg_endnwifent(handle);
2442 }
2443 zonecfg_fini_handle(handle);
2444 if (is_system_labeled()) {
2445 /*
2446 * Labeled zones share the loopback interface
2447 * so it is not plumbed for shared stack instances.
2448 */
2449 return (0);
2450 }
2451 (void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
2452 sizeof (loopback_iftab.zone_nwif_physical));
2453 (void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
2454 sizeof (loopback_iftab.zone_nwif_address));
2455 loopback_iftab.zone_nwif_defrouter[0] = '\0';
2456 if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
2457 return (-1);
2458
2459 /* Always plumb up the IPv6 loopback interface. */
2460 (void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
2461 sizeof (loopback_iftab.zone_nwif_address));
2462 if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
2463 return (-1);
2464 return (0);
2465 }
2466
2467 static void
2468 zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str)
2469 {
2470 char errmsg[DLADM_STRSIZE];
2471
2472 (void) dladm_status2str(err, errmsg);
2473 zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg);
2474 }
2475
2476 static int
2477 add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname)
2478 {
2479 dladm_status_t err;
2480 boolean_t cpuset, poolset;
2481 char *poolp;
2482
2483 /* First check if it's in use by global zone. */
2484 if (zonecfg_ifname_exists(AF_INET, dlname) ||
2485 zonecfg_ifname_exists(AF_INET6, dlname)) {
2486 zerror(zlogp, B_FALSE, "WARNING: skipping network interface "
2487 "'%s' which is used in the global zone", dlname);
2488 return (-1);
2489 }
2490
2491 /* Set zoneid of this link. */
2492 err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1,
2493 DLADM_OPT_ACTIVE);
2494 if (err != DLADM_STATUS_OK) {
2495 zdlerror(zlogp, err, dlname,
2496 "WARNING: unable to add network interface");
2497 return (-1);
2498 }
2499
2500 /*
2501 * Set the pool of this link if the zone has a pool and
2502 * neither the cpus nor the pool datalink property is
2503 * already set.
2504 */
2505 err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
2506 "cpus", &cpuset);
2507 if (err != DLADM_STATUS_OK) {
2508 zdlerror(zlogp, err, dlname,
2509 "WARNING: unable to check if cpus link property is set");
2510 }
2511 err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
2512 "pool", &poolset);
2513 if (err != DLADM_STATUS_OK) {
2514 zdlerror(zlogp, err, dlname,
2515 "WARNING: unable to check if pool link property is set");
2516 }
2517
2518 if ((strlen(pool_name) != 0) && !cpuset && !poolset) {
2519 poolp = pool_name;
2520 err = dladm_set_linkprop(dld_handle, linkid, "pool",
2521 &poolp, 1, DLADM_OPT_ACTIVE);
2522 if (err != DLADM_STATUS_OK) {
2523 zerror(zlogp, B_FALSE, "WARNING: unable to set "
2524 "pool %s to datalink %s", pool_name, dlname);
2525 bzero(pool_name, sizeof (pool_name));
2526 }
2527 } else {
2528 bzero(pool_name, sizeof (pool_name));
2529 }
2530 return (0);
2531 }
2532
2533 static boolean_t
2534 sockaddr_to_str(sa_family_t af, const struct sockaddr *sockaddr,
2535 char *straddr, size_t len)
2536 {
2537 struct sockaddr_in *sin;
2538 struct sockaddr_in6 *sin6;
2539 const char *str = NULL;
2540
2541 if (af == AF_INET) {
2542 /* LINTED E_BAD_PTR_CAST_ALIGN */
2543 sin = SIN(sockaddr);
2544 str = inet_ntop(AF_INET, (void *)&sin->sin_addr, straddr, len);
2545 } else if (af == AF_INET6) {
2546 /* LINTED E_BAD_PTR_CAST_ALIGN */
2547 sin6 = SIN6(sockaddr);
2548 str = inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, straddr,
2549 len);
2550 }
2551
2552 return (str != NULL);
2553 }
2554
2555 static int
2556 ipv4_prefixlen(struct sockaddr_in *sin)
2557 {
2558 struct sockaddr_in *m;
2559 struct sockaddr_storage mask;
2560
2561 m = SIN(&mask);
2562 m->sin_family = AF_INET;
2563 if (getnetmaskbyaddr(sin->sin_addr, &m->sin_addr) == 0) {
2564 return (mask2plen((struct sockaddr *)&mask));
2565 } else if (IN_CLASSA(htonl(sin->sin_addr.s_addr))) {
2566 return (8);
2567 } else if (IN_CLASSB(ntohl(sin->sin_addr.s_addr))) {
2568 return (16);
2569 } else if (IN_CLASSC(ntohl(sin->sin_addr.s_addr))) {
2570 return (24);
2571 }
2572 return (0);
2573 }
2574
2575 static int
2576 zone_setattr_network(int type, zoneid_t zoneid, datalink_id_t linkid,
2577 void *buf, size_t bufsize)
2578 {
2579 zone_net_data_t *zndata;
2580 size_t znsize;
2581 int err;
2582
2583 znsize = sizeof (*zndata) + bufsize;
2584 zndata = calloc(1, znsize);
2585 if (zndata == NULL)
2586 return (ENOMEM);
2587 zndata->zn_type = type;
2588 zndata->zn_len = bufsize;
2589 zndata->zn_linkid = linkid;
2590 bcopy(buf, zndata->zn_val, zndata->zn_len);
2591 err = zone_setattr(zoneid, ZONE_ATTR_NETWORK, zndata, znsize);
2592 free(zndata);
2593 return (err);
2594 }
2595
2596 static int
2597 add_net_for_linkid(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *start)
2598 {
2599 struct lifreq lifr;
2600 char **astr, *address;
2601 dladm_status_t dlstatus;
2602 char *ip_nospoof = "ip-nospoof";
2603 int nnet, naddr, err = 0, j;
2604 size_t zlen, cpleft;
2605 zone_addr_list_t *ptr, *end;
2606 char tmp[INET6_ADDRSTRLEN], *maskstr;
2607 char *zaddr, *cp;
2608 struct in6_addr *routes = NULL;
2609 boolean_t is_set;
2610 datalink_id_t linkid;
2611
2612 assert(start != NULL);
2613 naddr = 0; /* number of addresses */
2614 nnet = 0; /* number of net resources */
2615 linkid = start->za_linkid;
2616 for (ptr = start; ptr != NULL && ptr->za_linkid == linkid;
2617 ptr = ptr->za_next) {
2618 nnet++;
2619 }
2620 end = ptr;
2621 zlen = nnet * (INET6_ADDRSTRLEN + 1);
2622 astr = calloc(1, nnet * sizeof (uintptr_t));
2623 zaddr = calloc(1, zlen);
2624 if (astr == NULL || zaddr == NULL) {
2625 err = ENOMEM;
2626 goto done;
2627 }
2628 cp = zaddr;
2629 cpleft = zlen;
2630 j = 0;
2631 for (ptr = start; ptr != end; ptr = ptr->za_next) {
2632 address = ptr->za_nwiftab.zone_nwif_allowed_address;
2633 if (address[0] == '\0')
2634 continue;
2635 (void) snprintf(tmp, sizeof (tmp), "%s", address);
2636 /*
2637 * Validate the data. zonecfg_valid_net_address() clobbers
2638 * the /<mask> in the address string.
2639 */
2640 if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2641 zerror(zlogp, B_FALSE, "invalid address [%s]\n",
2642 address);
2643 err = EINVAL;
2644 goto done;
2645 }
2646 /*
2647 * convert any hostnames to numeric address strings.
2648 */
2649 if (!sockaddr_to_str(lifr.lifr_addr.ss_family,
2650 (const struct sockaddr *)&lifr.lifr_addr, cp, cpleft)) {
2651 err = EINVAL;
2652 goto done;
2653 }
2654 /*
2655 * make a copy of the numeric string for the data needed
2656 * by the "allowed-ips" datalink property.
2657 */
2658 astr[j] = strdup(cp);
2659 if (astr[j] == NULL) {
2660 err = ENOMEM;
2661 goto done;
2662 }
2663 j++;
2664 /*
2665 * compute the default netmask from the address, if necessary
2666 */
2667 if ((maskstr = strchr(tmp, '/')) == NULL) {
2668 int prefixlen;
2669
2670 if (lifr.lifr_addr.ss_family == AF_INET) {
2671 prefixlen = ipv4_prefixlen(
2672 SIN(&lifr.lifr_addr));
2673 } else {
2674 struct sockaddr_in6 *sin6;
2675
2676 sin6 = SIN6(&lifr.lifr_addr);
2677 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2678 prefixlen = 10;
2679 else
2680 prefixlen = 64;
2681 }
2682 (void) snprintf(tmp, sizeof (tmp), "%d", prefixlen);
2683 maskstr = tmp;
2684 } else {
2685 maskstr++;
2686 }
2687 /* append the "/<netmask>" */
2688 (void) strlcat(cp, "/", cpleft);
2689 (void) strlcat(cp, maskstr, cpleft);
2690 (void) strlcat(cp, ",", cpleft);
2691 cp += strnlen(cp, zlen);
2692 cpleft = &zaddr[INET6_ADDRSTRLEN] - cp;
2693 }
2694 naddr = j; /* the actual number of addresses in the net resource */
2695 assert(naddr <= nnet);
2696
2697 /*
2698 * zonecfg has already verified that the defrouter property can only
2699 * be set if there is at least one address defined for the net resource.
2700 * If j is 0, there are no addresses defined, and therefore no routers
2701 * to configure, and we are done at that point.
2702 */
2703 if (j == 0)
2704 goto done;
2705
2706 /* over-write last ',' with '\0' */
2707 zaddr[strnlen(zaddr, zlen) + 1] = '\0';
2708
2709 /*
2710 * First make sure L3 protection is not already set on the link.
2711 */
2712 dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2713 "protection", &is_set);
2714 if (dlstatus != DLADM_STATUS_OK) {
2715 err = EINVAL;
2716 zerror(zlogp, B_FALSE, "unable to check if protection is set");
2717 goto done;
2718 }
2719 if (is_set) {
2720 err = EINVAL;
2721 zerror(zlogp, B_FALSE, "Protection is already set");
2722 goto done;
2723 }
2724 dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2725 "allowed-ips", &is_set);
2726 if (dlstatus != DLADM_STATUS_OK) {
2727 err = EINVAL;
2728 zerror(zlogp, B_FALSE, "unable to check if allowed-ips is set");
2729 goto done;
2730 }
2731 if (is_set) {
2732 zerror(zlogp, B_FALSE, "allowed-ips is already set");
2733 err = EINVAL;
2734 goto done;
2735 }
2736
2737 /*
2738 * Enable ip-nospoof for the link, and add address to the allowed-ips
2739 * list.
2740 */
2741 dlstatus = dladm_set_linkprop(dld_handle, linkid, "protection",
2742 &ip_nospoof, 1, DLADM_OPT_ACTIVE);
2743 if (dlstatus != DLADM_STATUS_OK) {
2744 zerror(zlogp, B_FALSE, "could not set protection\n");
2745 err = EINVAL;
2746 goto done;
2747 }
2748 dlstatus = dladm_set_linkprop(dld_handle, linkid, "allowed-ips",
2749 astr, naddr, DLADM_OPT_ACTIVE);
2750 if (dlstatus != DLADM_STATUS_OK) {
2751 zerror(zlogp, B_FALSE, "could not set allowed-ips\n");
2752 err = EINVAL;
2753 goto done;
2754 }
2755
2756 /* now set the address in the data-store */
2757 err = zone_setattr_network(ZONE_NETWORK_ADDRESS, zoneid, linkid,
2758 zaddr, strnlen(zaddr, zlen) + 1);
2759 if (err != 0)
2760 goto done;
2761
2762 /*
2763 * add the defaultrouters
2764 */
2765 routes = calloc(1, nnet * sizeof (*routes));
2766 j = 0;
2767 for (ptr = start; ptr != end; ptr = ptr->za_next) {
2768 address = ptr->za_nwiftab.zone_nwif_defrouter;
2769 if (address[0] == '\0')
2770 continue;
2771 if (strchr(address, '/') == NULL && strchr(address, ':') != 0) {
2772 /*
2773 * zonecfg_valid_net_address() expects numeric IPv6
2774 * addresses to have a CIDR format netmask.
2775 */
2776 (void) snprintf(tmp, sizeof (tmp), "/%d", V6_ADDR_LEN);
2777 (void) strlcat(address, tmp, INET6_ADDRSTRLEN);
2778 }
2779 if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2780 zerror(zlogp, B_FALSE,
2781 "invalid router [%s]\n", address);
2782 err = EINVAL;
2783 goto done;
2784 }
2785 if (lifr.lifr_addr.ss_family == AF_INET6) {
2786 routes[j] = SIN6(&lifr.lifr_addr)->sin6_addr;
2787 } else {
2788 IN6_INADDR_TO_V4MAPPED(&SIN(&lifr.lifr_addr)->sin_addr,
2789 &routes[j]);
2790 }
2791 j++;
2792 }
2793 assert(j <= nnet);
2794 if (j > 0) {
2795 err = zone_setattr_network(ZONE_NETWORK_DEFROUTER, zoneid,
2796 linkid, routes, j * sizeof (*routes));
2797 }
2798 done:
2799 free(routes);
2800 for (j = 0; j < naddr; j++)
2801 free(astr[j]);
2802 free(astr);
2803 free(zaddr);
2804 return (err);
2805
2806 }
2807
2808 static int
2809 add_net(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *zalist)
2810 {
2811 zone_addr_list_t *ptr;
2812 datalink_id_t linkid;
2813 int err;
2814
2815 if (zalist == NULL)
2816 return (0);
2817
2818 linkid = zalist->za_linkid;
2819
2820 err = add_net_for_linkid(zlogp, zoneid, zalist);
2821 if (err != 0)
2822 return (err);
2823
2824 for (ptr = zalist; ptr != NULL; ptr = ptr->za_next) {
2825 if (ptr->za_linkid == linkid)
2826 continue;
2827 linkid = ptr->za_linkid;
2828 err = add_net_for_linkid(zlogp, zoneid, ptr);
2829 if (err != 0)
2830 return (err);
2831 }
2832 return (0);
2833 }
2834
2835 /*
2836 * Add "new" to the list of network interfaces to be configured by
2837 * add_net on zone boot in "old". The list of interfaces in "old" is
2838 * sorted by datalink_id_t, with interfaces sorted FIFO for a given
2839 * datalink_id_t.
2840 *
2841 * Returns the merged list of IP interfaces containing "old" and "new"
2842 */
2843 static zone_addr_list_t *
2844 add_ip_interface(zone_addr_list_t *old, zone_addr_list_t *new)
2845 {
2846 zone_addr_list_t *ptr, *next;
2847 datalink_id_t linkid = new->za_linkid;
2848
2849 assert(old != new);
2850
2851 if (old == NULL)
2852 return (new);
2853 for (ptr = old; ptr != NULL; ptr = ptr->za_next) {
2854 if (ptr->za_linkid == linkid)
2855 break;
2856 }
2857 if (ptr == NULL) {
2858 /* linkid does not already exist, add to the beginning */
2859 new->za_next = old;
2860 return (new);
2861 }
2862 /*
2863 * adding to the middle of the list; ptr points at the first
2864 * occurrence of linkid. Find the last occurrence.
2865 */
2866 while ((next = ptr->za_next) != NULL) {
2867 if (next->za_linkid != linkid)
2868 break;
2869 ptr = next;
2870 }
2871 /* insert new after ptr */
2872 new->za_next = next;
2873 ptr->za_next = new;
2874 return (old);
2875 }
2876
2877 void
2878 free_ip_interface(zone_addr_list_t *zalist)
2879 {
2880 zone_addr_list_t *ptr, *new;
2881
2882 for (ptr = zalist; ptr != NULL; ) {
2883 new = ptr;
2884 ptr = ptr->za_next;
2885 free(new);
2886 }
2887 }
2888
2889 /*
2890 * Add the kernel access control information for the interface names.
2891 * If anything goes wrong, we log a general error message, attempt to tear down
2892 * whatever we set up, and return an error.
2893 */
2894 static int
2895 configure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2896 {
2897 zone_dochandle_t handle;
2898 struct zone_nwiftab nwiftab;
2899 char rootpath[MAXPATHLEN];
2900 char path[MAXPATHLEN];
2901 datalink_id_t linkid;
2902 di_prof_t prof = NULL;
2903 boolean_t added = B_FALSE;
2904 zone_addr_list_t *zalist = NULL, *new;
2905
2906 if ((handle = zonecfg_init_handle()) == NULL) {
2907 zerror(zlogp, B_TRUE, "getting zone configuration handle");
2908 return (-1);
2909 }
2910 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2911 zerror(zlogp, B_FALSE, "invalid configuration");
2912 zonecfg_fini_handle(handle);
2913 return (-1);
2914 }
2915
2916 if (zonecfg_setnwifent(handle) != Z_OK) {
2917 zonecfg_fini_handle(handle);
2918 return (0);
2919 }
2920
2921 for (;;) {
2922 if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2923 break;
2924
2925 if (prof == NULL) {
2926 if (zone_get_devroot(zone_name, rootpath,
2927 sizeof (rootpath)) != Z_OK) {
2928 (void) zonecfg_endnwifent(handle);
2929 zonecfg_fini_handle(handle);
2930 zerror(zlogp, B_TRUE,
2931 "unable to determine dev root");
2932 return (-1);
2933 }
2934 (void) snprintf(path, sizeof (path), "%s%s", rootpath,
2935 "/dev");
2936 if (di_prof_init(path, &prof) != 0) {
2937 (void) zonecfg_endnwifent(handle);
2938 zonecfg_fini_handle(handle);
2939 zerror(zlogp, B_TRUE,
2940 "failed to initialize profile");
2941 return (-1);
2942 }
2943 }
2944
2945 /*
2946 * Create the /dev entry for backward compatibility.
2947 * Only create the /dev entry if it's not in use.
2948 * Note that the zone still boots when the assigned
2949 * interface is inaccessible, used by others, etc.
2950 * Also, when vanity naming is used, some interface do
2951 * do not have corresponding /dev node names (for example,
2952 * vanity named aggregations). The /dev entry is not
2953 * created in that case. The /dev/net entry is always
2954 * accessible.
2955 */
2956 if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical,
2957 &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
2958 add_datalink(zlogp, zone_name, linkid,
2959 nwiftab.zone_nwif_physical) == 0) {
2960 added = B_TRUE;
2961 } else {
2962 (void) zonecfg_endnwifent(handle);
2963 zonecfg_fini_handle(handle);
2964 zerror(zlogp, B_TRUE, "failed to add network device");
2965 return (-1);
2966 }
2967 /* set up the new IP interface, and add them all later */
2968 new = malloc(sizeof (*new));
2969 if (new == NULL) {
2970 zerror(zlogp, B_TRUE, "no memory for %s",
2971 nwiftab.zone_nwif_physical);
2972 zonecfg_fini_handle(handle);
2973 free_ip_interface(zalist);
2974 }
2975 bzero(new, sizeof (*new));
2976 new->za_nwiftab = nwiftab;
2977 new->za_linkid = linkid;
2978 zalist = add_ip_interface(zalist, new);
2979 }
2980 if (zalist != NULL) {
2981 if ((errno = add_net(zlogp, zoneid, zalist)) != 0) {
2982 (void) zonecfg_endnwifent(handle);
2983 zonecfg_fini_handle(handle);
2984 zerror(zlogp, B_TRUE, "failed to add address");
2985 free_ip_interface(zalist);
2986 return (-1);
2987 }
2988 free_ip_interface(zalist);
2989 }
2990 (void) zonecfg_endnwifent(handle);
2991 zonecfg_fini_handle(handle);
2992
2993 if (prof != NULL && added) {
2994 if (di_prof_commit(prof) != 0) {
2995 zerror(zlogp, B_TRUE, "failed to commit profile");
2996 return (-1);
2997 }
2998 }
2999 if (prof != NULL)
3000 di_prof_fini(prof);
3001
3002 return (0);
3003 }
3004
3005 static int
3006 remove_datalink_pool(zlog_t *zlogp, zoneid_t zoneid)
3007 {
3008 ushort_t flags;
3009 zone_iptype_t iptype;
3010 int i, dlnum = 0;
3011 datalink_id_t *dllink, *dllinks = NULL;
3012 dladm_status_t err;
3013
3014 if (strlen(pool_name) == 0)
3015 return (0);
3016
3017 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
3018 sizeof (flags)) < 0) {
3019 if (vplat_get_iptype(zlogp, &iptype) < 0) {
3020 zerror(zlogp, B_FALSE, "unable to determine ip-type");
3021 return (-1);
3022 }
3023 } else {
3024 if (flags & ZF_NET_EXCL)
3025 iptype = ZS_EXCLUSIVE;
3026 else
3027 iptype = ZS_SHARED;
3028 }
3029
3030 if (iptype == ZS_EXCLUSIVE) {
3031 /*
3032 * Get the datalink count and for each datalink,
3033 * attempt to clear the pool property and clear
3034 * the pool_name.
3035 */
3036 if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3037 zerror(zlogp, B_TRUE, "unable to count network "
3038 "interfaces");
3039 return (-1);
3040 }
3041
3042 if (dlnum == 0)
3043 return (0);
3044
3045 if ((dllinks = malloc(dlnum * sizeof (datalink_id_t)))
3046 == NULL) {
3047 zerror(zlogp, B_TRUE, "memory allocation failed");
3048 return (-1);
3049 }
3050 if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
3051 zerror(zlogp, B_TRUE, "unable to list network "
3052 "interfaces");
3053 return (-1);
3054 }
3055
3056 bzero(pool_name, sizeof (pool_name));
3057 for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
3058 err = dladm_set_linkprop(dld_handle, *dllink, "pool",
3059 NULL, 0, DLADM_OPT_ACTIVE);
3060 if (err != DLADM_STATUS_OK) {
3061 zerror(zlogp, B_TRUE,
3062 "WARNING: unable to clear pool");
3063 }
3064 }
3065 free(dllinks);
3066 }
3067 return (0);
3068 }
3069
3070 static int
3071 remove_datalink_protect(zlog_t *zlogp, zoneid_t zoneid)
3072 {
3073 ushort_t flags;
3074 zone_iptype_t iptype;
3075 int i, dlnum = 0;
3076 dladm_status_t dlstatus;
3077 datalink_id_t *dllink, *dllinks = NULL;
3078
3079 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
3080 sizeof (flags)) < 0) {
3081 if (vplat_get_iptype(zlogp, &iptype) < 0) {
3082 zerror(zlogp, B_FALSE, "unable to determine ip-type");
3083 return (-1);
3084 }
3085 } else {
3086 if (flags & ZF_NET_EXCL)
3087 iptype = ZS_EXCLUSIVE;
3088 else
3089 iptype = ZS_SHARED;
3090 }
3091
3092 if (iptype != ZS_EXCLUSIVE)
3093 return (0);
3094
3095 /*
3096 * Get the datalink count and for each datalink,
3097 * attempt to clear the pool property and clear
3098 * the pool_name.
3099 */
3100 if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3101 zerror(zlogp, B_TRUE, "unable to count network interfaces");
3102 return (-1);
3103 }
3104
3105 if (dlnum == 0)
3106 return (0);
3107
3108 if ((dllinks = malloc(dlnum * sizeof (datalink_id_t))) == NULL) {
3109 zerror(zlogp, B_TRUE, "memory allocation failed");
3110 return (-1);
3111 }
3112 if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
3113 zerror(zlogp, B_TRUE, "unable to list network interfaces");
3114 free(dllinks);
3115 return (-1);
3116 }
3117
3118 for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
3119 char dlerr[DLADM_STRSIZE];
3120
3121 dlstatus = dladm_set_linkprop(dld_handle, *dllink,
3122 "protection", NULL, 0, DLADM_OPT_ACTIVE);
3123 if (dlstatus == DLADM_STATUS_NOTFOUND) {
3124 /* datalink does not belong to the GZ */
3125 continue;
3126 }
3127 if (dlstatus != DLADM_STATUS_OK)
3128 zerror(zlogp, B_FALSE,
3129 "clear 'protection' link property: %s",
3130 dladm_status2str(dlstatus, dlerr));
3131
3132 dlstatus = dladm_set_linkprop(dld_handle, *dllink,
3133 "allowed-ips", NULL, 0, DLADM_OPT_ACTIVE);
3134 if (dlstatus != DLADM_STATUS_OK)
3135 zerror(zlogp, B_FALSE,
3136 "clear 'allowed-ips' link property: %s",
3137 dladm_status2str(dlstatus, dlerr));
3138 }
3139 free(dllinks);
3140 return (0);
3141 }
3142
3143 static int
3144 tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
3145 const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
3146 {
3147 int fd;
3148 struct strioctl ioc;
3149 tcp_ioc_abort_conn_t conn;
3150 int error;
3151
3152 conn.ac_local = *local;
3153 conn.ac_remote = *remote;
3154 conn.ac_start = TCPS_SYN_SENT;
3155 conn.ac_end = TCPS_TIME_WAIT;
3156 conn.ac_zoneid = zoneid;
3157
3158 ioc.ic_cmd = TCP_IOC_ABORT_CONN;
3159 ioc.ic_timout = -1; /* infinite timeout */
3160 ioc.ic_len = sizeof (conn);
3161 ioc.ic_dp = (char *)&conn;
3162
3163 if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
3164 zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
3165 return (-1);
3166 }
3167
3168 error = ioctl(fd, I_STR, &ioc);
3169 (void) close(fd);
3170 if (error == 0 || errno == ENOENT) /* ENOENT is not an error */
3171 return (0);
3172 return (-1);
3173 }
3174
3175 static int
3176 tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
3177 {
3178 struct sockaddr_storage l, r;
3179 struct sockaddr_in *local, *remote;
3180 struct sockaddr_in6 *local6, *remote6;
3181 int error;
3182
3183 /*
3184 * Abort IPv4 connections.
3185 */
3186 bzero(&l, sizeof (*local));
3187 local = (struct sockaddr_in *)&l;
3188 local->sin_family = AF_INET;
3189 local->sin_addr.s_addr = INADDR_ANY;
3190 local->sin_port = 0;
3191
3192 bzero(&r, sizeof (*remote));
3193 remote = (struct sockaddr_in *)&r;
3194 remote->sin_family = AF_INET;
3195 remote->sin_addr.s_addr = INADDR_ANY;
3196 remote->sin_port = 0;
3197
3198 if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
3199 return (error);
3200
3201 /*
3202 * Abort IPv6 connections.
3203 */
3204 bzero(&l, sizeof (*local6));
3205 local6 = (struct sockaddr_in6 *)&l;
3206 local6->sin6_family = AF_INET6;
3207 local6->sin6_port = 0;
3208 local6->sin6_addr = in6addr_any;
3209
3210 bzero(&r, sizeof (*remote6));
3211 remote6 = (struct sockaddr_in6 *)&r;
3212 remote6->sin6_family = AF_INET6;
3213 remote6->sin6_port = 0;
3214 remote6->sin6_addr = in6addr_any;
3215
3216 if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
3217 return (error);
3218 return (0);
3219 }
3220
3221 static int
3222 get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
3223 {
3224 int error = -1;
3225 zone_dochandle_t handle;
3226 char *privname = NULL;
3227
3228 if ((handle = zonecfg_init_handle()) == NULL) {
3229 zerror(zlogp, B_TRUE, "getting zone configuration handle");
3230 return (-1);
3231 }
3232 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3233 zerror(zlogp, B_FALSE, "invalid configuration");
3234 zonecfg_fini_handle(handle);
3235 return (-1);
3236 }
3237
3238 if (ALT_MOUNT(mount_cmd)) {
3239 zone_iptype_t iptype;
3240 const char *curr_iptype;
3241
3242 if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
3243 zerror(zlogp, B_TRUE, "unable to determine ip-type");
3244 zonecfg_fini_handle(handle);
3245 return (-1);
3246 }
3247
3248 switch (iptype) {
3249 case ZS_SHARED:
3250 curr_iptype = "shared";
3251 break;
3252 case ZS_EXCLUSIVE:
3253 curr_iptype = "exclusive";
3254 break;
3255 }
3256
3257 if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
3258 zonecfg_fini_handle(handle);
3259 return (0);
3260 }
3261 zerror(zlogp, B_FALSE,
3262 "failed to determine the zone's default privilege set");
3263 zonecfg_fini_handle(handle);
3264 return (-1);
3265 }
3266
3267 switch (zonecfg_get_privset(handle, privs, &privname)) {
3268 case Z_OK:
3269 error = 0;
3270 break;
3271 case Z_PRIV_PROHIBITED:
3272 zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
3273 "within the zone's privilege set", privname);
3274 break;
3275 case Z_PRIV_REQUIRED:
3276 zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
3277 "from the zone's privilege set", privname);
3278 break;
3279 case Z_PRIV_UNKNOWN:
3280 zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
3281 "in the zone's privilege set", privname);
3282 break;
3283 default:
3284 zerror(zlogp, B_FALSE, "failed to determine the zone's "
3285 "privilege set");
3286 break;
3287 }
3288
3289 free(privname);
3290 zonecfg_fini_handle(handle);
3291 return (error);
3292 }
3293
3294 static int
3295 get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3296 {
3297 nvlist_t *nvl = NULL;
3298 char *nvl_packed = NULL;
3299 size_t nvl_size = 0;
3300 nvlist_t **nvlv = NULL;
3301 int rctlcount = 0;
3302 int error = -1;
3303 zone_dochandle_t handle;
3304 struct zone_rctltab rctltab;
3305 rctlblk_t *rctlblk = NULL;
3306 uint64_t maxlwps;
3307 uint64_t maxprocs;
3308
3309 *bufp = NULL;
3310 *bufsizep = 0;
3311
3312 if ((handle = zonecfg_init_handle()) == NULL) {
3313 zerror(zlogp, B_TRUE, "getting zone configuration handle");
3314 return (-1);
3315 }
3316 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3317 zerror(zlogp, B_FALSE, "invalid configuration");
3318 zonecfg_fini_handle(handle);
3319 return (-1);
3320 }
3321
3322 rctltab.zone_rctl_valptr = NULL;
3323 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
3324 zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
3325 goto out;
3326 }
3327
3328 /*
3329 * Allow the administrator to control both the maximum number of
3330 * process table slots and the maximum number of lwps with just the
3331 * max-processes property. If only the max-processes property is set,
3332 * we add a max-lwps property with a limit derived from max-processes.
3333 */
3334 if (zonecfg_get_aliased_rctl(handle, ALIAS_MAXPROCS, &maxprocs)
3335 == Z_OK &&
3336 zonecfg_get_aliased_rctl(handle, ALIAS_MAXLWPS, &maxlwps)
3337 == Z_NO_ENTRY) {
3338 if (zonecfg_set_aliased_rctl(handle, ALIAS_MAXLWPS,
3339 maxprocs * LWPS_PER_PROCESS) != Z_OK) {
3340 zerror(zlogp, B_FALSE, "unable to set max-lwps alias");
3341 goto out;
3342 }
3343 }
3344
3345 if (zonecfg_setrctlent(handle) != Z_OK) {
3346 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
3347 goto out;
3348 }
3349
3350 if ((rctlblk = malloc(rctlblk_size())) == NULL) {
3351 zerror(zlogp, B_TRUE, "memory allocation failed");
3352 goto out;
3353 }
3354 while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
3355 struct zone_rctlvaltab *rctlval;
3356 uint_t i, count;
3357 const char *name = rctltab.zone_rctl_name;
3358
3359 /* zoneadm should have already warned about unknown rctls. */
3360 if (!zonecfg_is_rctl(name)) {
3361 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
3362 rctltab.zone_rctl_valptr = NULL;
3363 continue;
3364 }
3365 count = 0;
3366 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
3367 rctlval = rctlval->zone_rctlval_next) {
3368 count++;
3369 }
3370 if (count == 0) { /* ignore */
3371 continue; /* Nothing to free */
3372 }
3373 if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
3374 goto out;
3375 i = 0;
3376 for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
3377 rctlval = rctlval->zone_rctlval_next, i++) {
3378 if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
3379 zerror(zlogp, B_TRUE, "%s failed",
3380 "nvlist_alloc");
3381 goto out;
3382 }
3383 if (zonecfg_construct_rctlblk(rctlval, rctlblk)
3384 != Z_OK) {
3385 zerror(zlogp, B_FALSE, "invalid rctl value: "
3386 "(priv=%s,limit=%s,action=%s)",
3387 rctlval->zone_rctlval_priv,
3388 rctlval->zone_rctlval_limit,
3389 rctlval->zone_rctlval_action);
3390 goto out;
3391 }
3392 if (!zonecfg_valid_rctl(name, rctlblk)) {
3393 zerror(zlogp, B_FALSE,
3394 "(priv=%s,limit=%s,action=%s) is not a "
3395 "valid value for rctl '%s'",
3396 rctlval->zone_rctlval_priv,
3397 rctlval->zone_rctlval_limit,
3398 rctlval->zone_rctlval_action,
3399 name);
3400 goto out;
3401 }
3402 if (nvlist_add_uint64(nvlv[i], "privilege",
3403 rctlblk_get_privilege(rctlblk)) != 0) {
3404 zerror(zlogp, B_FALSE, "%s failed",
3405 "nvlist_add_uint64");
3406 goto out;
3407 }
3408 if (nvlist_add_uint64(nvlv[i], "limit",
3409 rctlblk_get_value(rctlblk)) != 0) {
3410 zerror(zlogp, B_FALSE, "%s failed",
3411 "nvlist_add_uint64");
3412 goto out;
3413 }
3414 if (nvlist_add_uint64(nvlv[i], "action",
3415 (uint_t)rctlblk_get_local_action(rctlblk, NULL))
3416 != 0) {
3417 zerror(zlogp, B_FALSE, "%s failed",
3418 "nvlist_add_uint64");
3419 goto out;
3420 }
3421 }
3422 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
3423 rctltab.zone_rctl_valptr = NULL;
3424 if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
3425 != 0) {
3426 zerror(zlogp, B_FALSE, "%s failed",
3427 "nvlist_add_nvlist_array");
3428 goto out;
3429 }
3430 for (i = 0; i < count; i++)
3431 nvlist_free(nvlv[i]);
3432 free(nvlv);
3433 nvlv = NULL;
3434 rctlcount++;
3435 }
3436 (void) zonecfg_endrctlent(handle);
3437
3438 if (rctlcount == 0) {
3439 error = 0;
3440 goto out;
3441 }
3442 if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
3443 != 0) {
3444 zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
3445 goto out;
3446 }
3447
3448 error = 0;
3449 *bufp = nvl_packed;
3450 *bufsizep = nvl_size;
3451
3452 out:
3453 free(rctlblk);
3454 zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
3455 if (error && nvl_packed != NULL)
3456 free(nvl_packed);
3457 if (nvl != NULL)
3458 nvlist_free(nvl);
3459 if (nvlv != NULL)
3460 free(nvlv);
3461 if (handle != NULL)
3462 zonecfg_fini_handle(handle);
3463 return (error);
3464 }
3465
3466 static int
3467 get_implicit_datasets(zlog_t *zlogp, char **retstr)
3468 {
3469 char cmdbuf[2 * MAXPATHLEN];
3470
3471 if (query_hook[0] == '\0')
3472 return (0);
3473
3474 if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
3475 > sizeof (cmdbuf))
3476 return (-1);
3477
3478 if (do_subproc(zlogp, cmdbuf, retstr) != 0)
3479 return (-1);
3480
3481 return (0);
3482 }
3483
3484 static int
3485 get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3486 {
3487 zone_dochandle_t handle;
3488 struct zone_dstab dstab;
3489 size_t total, offset, len;
3490 int error = -1;
3491 char *str = NULL;
3492 char *implicit_datasets = NULL;
3493 int implicit_len = 0;
3494
3495 *bufp = NULL;
3496 *bufsizep = 0;
3497
3498 if ((handle = zonecfg_init_handle()) == NULL) {
3499 zerror(zlogp, B_TRUE, "getting zone configuration handle");
3500 return (-1);
3501 }
3502 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3503 zerror(zlogp, B_FALSE, "invalid configuration");
3504 zonecfg_fini_handle(handle);
3505 return (-1);
3506 }
3507
3508 if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
3509 zerror(zlogp, B_FALSE, "getting implicit datasets failed");
3510 goto out;
3511 }
3512
3513 if (zonecfg_setdsent(handle) != Z_OK) {
3514 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3515 goto out;
3516 }
3517
3518 total = 0;
3519 while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3520 total += strlen(dstab.zone_dataset_name) + 1;
3521 (void) zonecfg_enddsent(handle);
3522
3523 if (implicit_datasets != NULL)
3524 implicit_len = strlen(implicit_datasets);
3525 if (implicit_len > 0)
3526 total += implicit_len + 1;
3527
3528 if (total == 0) {
3529 error = 0;
3530 goto out;
3531 }
3532
3533 if ((str = malloc(total)) == NULL) {
3534 zerror(zlogp, B_TRUE, "memory allocation failed");
3535 goto out;
3536 }
3537
3538 if (zonecfg_setdsent(handle) != Z_OK) {
3539 zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3540 goto out;
3541 }
3542 offset = 0;
3543 while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3544 len = strlen(dstab.zone_dataset_name);
3545 (void) strlcpy(str + offset, dstab.zone_dataset_name,
3546 total - offset);
3547 offset += len;
3548 if (offset < total - 1)
3549 str[offset++] = ',';
3550 }
3551 (void) zonecfg_enddsent(handle);
3552
3553 if (implicit_len > 0)
3554 (void) strlcpy(str + offset, implicit_datasets, total - offset);
3555
3556 error = 0;
3557 *bufp = str;
3558 *bufsizep = total;
3559
3560 out:
3561 if (error != 0 && str != NULL)
3562 free(str);
3563 if (handle != NULL)
3564 zonecfg_fini_handle(handle);
3565 if (implicit_datasets != NULL)
3566 free(implicit_datasets);
3567
3568 return (error);
3569 }
3570
3571 static int
3572 validate_datasets(zlog_t *zlogp)
3573 {
3574 zone_dochandle_t handle;
3575 struct zone_dstab dstab;
3576 zfs_handle_t *zhp;
3577 libzfs_handle_t *hdl;
3578
3579 if ((handle = zonecfg_init_handle()) == NULL) {
3580 zerror(zlogp, B_TRUE, "getting zone configuration handle");
3581 return (-1);
3582 }
3583 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3584 zerror(zlogp, B_FALSE, "invalid configuration");
3585 zonecfg_fini_handle(handle);
3586 return (-1);
3587 }
3588
3589 if (zonecfg_setdsent(handle) != Z_OK) {
3590 zerror(zlogp, B_FALSE, "invalid configuration");
3591 zonecfg_fini_handle(handle);
3592 return (-1);
3593 }
3594
3595 if ((hdl = libzfs_init()) == NULL) {
3596 zerror(zlogp, B_FALSE, "opening ZFS library");
3597 zonecfg_fini_handle(handle);
3598 return (-1);
3599 }
3600
3601 while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3602
3603 if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3604 ZFS_TYPE_FILESYSTEM)) == NULL) {
3605 zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3606 dstab.zone_dataset_name);
3607 zonecfg_fini_handle(handle);
3608 libzfs_fini(hdl);
3609 return (-1);
3610 }
3611
3612 /*
3613 * Automatically set the 'zoned' property. We check the value
3614 * first because we'll get EPERM if it is already set.
3615 */
3616 if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3617 zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3618 "on") != 0) {
3619 zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3620 "property for ZFS dataset '%s'\n",
3621 dstab.zone_dataset_name);
3622 zonecfg_fini_handle(handle);
3623 zfs_close(zhp);
3624 libzfs_fini(hdl);
3625 return (-1);
3626 }
3627
3628 zfs_close(zhp);
3629 }
3630 (void) zonecfg_enddsent(handle);
3631
3632 zonecfg_fini_handle(handle);
3633 libzfs_fini(hdl);
3634
3635 return (0);
3636 }
3637
3638 /*
3639 * Return true if the path is its own zfs file system. We determine this
3640 * by stat-ing the path to see if it is zfs and stat-ing the parent to see
3641 * if it is a different fs.
3642 */
3643 boolean_t
3644 is_zonepath_zfs(char *zonepath)
3645 {
3646 int res;
3647 char *path;
3648 char *parent;
3649 struct statvfs64 buf1, buf2;
3650
3651 if (statvfs64(zonepath, &buf1) != 0)
3652 return (B_FALSE);
3653
3654 if (strcmp(buf1.f_basetype, "zfs") != 0)
3655 return (B_FALSE);
3656
3657 if ((path = strdup(zonepath)) == NULL)
3658 return (B_FALSE);
3659
3660 parent = dirname(path);
3661 res = statvfs64(parent, &buf2);
3662 free(path);
3663
3664 if (res != 0)
3665 return (B_FALSE);
3666
3667 if (buf1.f_fsid == buf2.f_fsid)
3668 return (B_FALSE);
3669
3670 return (B_TRUE);
3671 }
3672
3673 /*
3674 * Verify the MAC label in the root dataset for the zone.
3675 * If the label exists, it must match the label configured for the zone.
3676 * Otherwise if there's no label on the dataset, create one here.
3677 */
3678
3679 static int
3680 validate_rootds_label(zlog_t *zlogp, char *rootpath, m_label_t *zone_sl)
3681 {
3682 int error = -1;
3683 zfs_handle_t *zhp;
3684 libzfs_handle_t *hdl;
3685 m_label_t ds_sl;
3686 char zonepath[MAXPATHLEN];
3687 char ds_hexsl[MAXNAMELEN];
3688
3689 if (!is_system_labeled())
3690 return (0);
3691
3692 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
3693 zerror(zlogp, B_TRUE, "unable to determine zone path");
3694 return (-1);
3695 }
3696
3697 if (!is_zonepath_zfs(zonepath))
3698 return (0);
3699
3700 if ((hdl = libzfs_init()) == NULL) {
3701 zerror(zlogp, B_FALSE, "opening ZFS library");
3702 return (-1);
3703 }
3704
3705 if ((zhp = zfs_path_to_zhandle(hdl, rootpath,
3706 ZFS_TYPE_FILESYSTEM)) == NULL) {
3707 zerror(zlogp, B_FALSE, "cannot open ZFS dataset for path '%s'",
3708 rootpath);
3709 libzfs_fini(hdl);
3710 return (-1);
3711 }
3712
3713 /* Get the mlslabel property if it exists. */
3714 if ((zfs_prop_get(zhp, ZFS_PROP_MLSLABEL, ds_hexsl, MAXNAMELEN,
3715 NULL, NULL, 0, B_TRUE) != 0) ||
3716 (strcmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)) {
3717 char *str2 = NULL;
3718
3719 /*
3720 * No label on the dataset (or default only); create one.
3721 * (Only do this automatic labeling for the labeled brand.)
3722 */
3723 if (strcmp(brand_name, LABELED_BRAND_NAME) != 0) {
3724 error = 0;
3725 goto out;
3726 }
3727
3728 error = l_to_str_internal(zone_sl, &str2);
3729 if (error)
3730 goto out;
3731 if (str2 == NULL) {
3732 error = -1;
3733 goto out;
3734 }
3735 if ((error = zfs_prop_set(zhp,
3736 zfs_prop_to_name(ZFS_PROP_MLSLABEL), str2)) != 0) {
3737 zerror(zlogp, B_FALSE, "cannot set 'mlslabel' "
3738 "property for root dataset at '%s'\n", rootpath);
3739 }
3740 free(str2);
3741 goto out;
3742 }
3743
3744 /* Convert the retrieved dataset label to binary form. */
3745 error = hexstr_to_label(ds_hexsl, &ds_sl);
3746 if (error) {
3747 zerror(zlogp, B_FALSE, "invalid 'mlslabel' "
3748 "property on root dataset at '%s'\n", rootpath);
3749 goto out; /* exit with error */
3750 }
3751
3752 /*
3753 * Perform a MAC check by comparing the zone label with the
3754 * dataset label.
3755 */
3756 error = (!blequal(zone_sl, &ds_sl));
3757 if (error)
3758 zerror(zlogp, B_FALSE, "Rootpath dataset has mismatched label");
3759 out:
3760 zfs_close(zhp);
3761 libzfs_fini(hdl);
3762
3763 return (error);
3764 }
3765
3766 /*
3767 * Mount lower level home directories into/from current zone
3768 * Share exported directories specified in dfstab for zone
3769 */
3770 static int
3771 tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
3772 {
3773 zoneid_t *zids = NULL;
3774 priv_set_t *zid_privs;
3775 const priv_impl_info_t *ip = NULL;
3776 uint_t nzents_saved;
3777 uint_t nzents;
3778 int i;
3779 char readonly[] = "ro";
3780 struct zone_fstab lower_fstab;
3781 char *argv[4];
3782
3783 if (!is_system_labeled())
3784 return (0);
3785
3786 if (zid_label == NULL) {
3787 zid_label = m_label_alloc(MAC_LABEL);
3788 if (zid_label == NULL)
3789 return (-1);
3790 }
3791
3792 /* Make sure our zone has an /export/home dir */
3793 (void) make_one_dir(zlogp, rootpath, "/export/home",
3794 DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
3795
3796 lower_fstab.zone_fs_raw[0] = '\0';
3797 (void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
3798 sizeof (lower_fstab.zone_fs_type));
3799 lower_fstab.zone_fs_options = NULL;
3800 (void) zonecfg_add_fs_option(&lower_fstab, readonly);
3801
3802 /*
3803 * Get the list of zones from the kernel
3804 */
3805 if (zone_list(NULL, &nzents) != 0) {
3806 zerror(zlogp, B_TRUE, "unable to list zones");
3807 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3808 return (-1);
3809 }
3810 again:
3811 if (nzents == 0) {
3812 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3813 return (-1);
3814 }
3815
3816 zids = malloc(nzents * sizeof (zoneid_t));
3817 if (zids == NULL) {
3818 zerror(zlogp, B_TRUE, "memory allocation failed");
3819 return (-1);
3820 }
3821 nzents_saved = nzents;
3822
3823 if (zone_list(zids, &nzents) != 0) {
3824 zerror(zlogp, B_TRUE, "unable to list zones");
3825 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
3826 free(zids);
3827 return (-1);
3828 }
3829 if (nzents != nzents_saved) {
3830 /* list changed, try again */
3831 free(zids);
3832 goto again;
3833 }
3834
3835 ip = getprivimplinfo();
3836 if ((zid_privs = priv_allocset()) == NULL) {
3837 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
3838 zonecfg_free_fs_option_list(
3839 lower_fstab.zone_fs_options);
3840 free(zids);
3841 return (-1);
3842 }
3843
3844 for (i = 0; i < nzents; i++) {
3845 char zid_name[ZONENAME_MAX];
3846 zone_state_t zid_state;
3847 char zid_rpath[MAXPATHLEN];
3848 struct stat stat_buf;
3849
3850 if (zids[i] == GLOBAL_ZONEID)
3851 continue;
3852
3853 if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
3854 continue;
3855
3856 /*
3857 * Do special setup for the zone we are booting
3858 */
3859 if (strcmp(zid_name, zone_name) == 0) {
3860 struct zone_fstab autofs_fstab;
3861 char map_path[MAXPATHLEN];
3862 int fd;
3863
3864 /*
3865 * Create auto_home_<zone> map for this zone
3866 * in the global zone. The non-global zone entry
3867 * will be created by automount when the zone
3868 * is booted.
3869 */
3870
3871 (void) snprintf(autofs_fstab.zone_fs_special,
3872 MAXPATHLEN, "auto_home_%s", zid_name);
3873
3874 (void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
3875 "/zone/%s/home", zid_name);
3876
3877 (void) snprintf(map_path, sizeof (map_path),
3878 "/etc/%s", autofs_fstab.zone_fs_special);
3879 /*
3880 * If the map file doesn't exist create a template
3881 */
3882 if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
3883 S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
3884 int len;
3885 char map_rec[MAXPATHLEN];
3886
3887 len = snprintf(map_rec, sizeof (map_rec),
3888 "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
3889 autofs_fstab.zone_fs_special, rootpath);
3890 (void) write(fd, map_rec, len);
3891 (void) close(fd);
3892 }
3893
3894 /*
3895 * Mount auto_home_<zone> in the global zone if absent.
3896 * If it's already of type autofs, then
3897 * don't mount it again.
3898 */
3899 if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
3900 strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
3901 char optstr[] = "indirect,ignore,nobrowse";
3902
3903 (void) make_one_dir(zlogp, "",
3904 autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
3905 DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
3906
3907 /*
3908 * Mount will fail if automounter has already
3909 * processed the auto_home_<zonename> map
3910 */
3911 (void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
3912 autofs_fstab.zone_fs_special,
3913 autofs_fstab.zone_fs_dir);
3914 }
3915 continue;
3916 }
3917
3918
3919 if (zone_get_state(zid_name, &zid_state) != Z_OK ||
3920 (zid_state != ZONE_STATE_READY &&
3921 zid_state != ZONE_STATE_RUNNING))
3922 /* Skip over zones without mounted filesystems */
3923 continue;
3924
3925 if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
3926 sizeof (m_label_t)) < 0)
3927 /* Skip over zones with unspecified label */
3928 continue;
3929
3930 if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
3931 sizeof (zid_rpath)) == -1)
3932 /* Skip over zones with bad path */
3933 continue;
3934
3935 if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
3936 sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
3937 /* Skip over zones with bad privs */
3938 continue;
3939
3940 /*
3941 * Reading down is valid according to our label model
3942 * but some customers want to disable it because it
3943 * allows execute down and other possible attacks.
3944 * Therefore, we restrict this feature to zones that
3945 * have the NET_MAC_AWARE privilege which is required
3946 * for NFS read-down semantics.
3947 */
3948 if ((bldominates(zlabel, zid_label)) &&
3949 (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
3950 /*
3951 * Our zone dominates this one.
3952 * Create a lofs mount from lower zone's /export/home
3953 */
3954 (void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
3955 "%s/zone/%s/export/home", rootpath, zid_name);
3956
3957 /*
3958 * If the target is already an LOFS mount
3959 * then don't do it again.
3960 */
3961 if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
3962 strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
3963
3964 if (snprintf(lower_fstab.zone_fs_special,
3965 MAXPATHLEN, "%s/export",
3966 zid_rpath) > MAXPATHLEN)
3967 continue;
3968
3969 /*
3970 * Make sure the lower-level home exists
3971 */
3972 if (make_one_dir(zlogp,
3973 lower_fstab.zone_fs_special, "/home",
3974 DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
3975 DEFAULT_DIR_GROUP) != 0)
3976 continue;
3977
3978 (void) strlcat(lower_fstab.zone_fs_special,
3979 "/home", MAXPATHLEN);
3980
3981 /*
3982 * Mount can fail because the lower-level
3983 * zone may have already done a mount up.
3984 */
3985 (void) mount_one(zlogp, &lower_fstab, "",
3986 Z_MNT_BOOT);
3987 }
3988 } else if ((bldominates(zid_label, zlabel)) &&
3989 (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
3990 /*
3991 * This zone dominates our zone.
3992 * Create a lofs mount from our zone's /export/home
3993 */
3994 if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
3995 "%s/zone/%s/export/home", zid_rpath,
3996 zone_name) > MAXPATHLEN)
3997 continue;
3998
3999 /*
4000 * If the target is already an LOFS mount
4001 * then don't do it again.
4002 */
4003 if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
4004 strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
4005
4006 (void) snprintf(lower_fstab.zone_fs_special,
4007 MAXPATHLEN, "%s/export/home", rootpath);
4008
4009 /*
4010 * Mount can fail because the higher-level
4011 * zone may have already done a mount down.
4012 */
4013 (void) mount_one(zlogp, &lower_fstab, "",
4014 Z_MNT_BOOT);
4015 }
4016 }
4017 }
4018 zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
4019 priv_freeset(zid_privs);
4020 free(zids);
4021
4022 /*
4023 * Now share any exported directories from this zone.
4024 * Each zone can have its own dfstab.
4025 */
4026
4027 argv[0] = "zoneshare";
4028 argv[1] = "-z";
4029 argv[2] = zone_name;
4030 argv[3] = NULL;
4031
4032 (void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
4033 /* Don't check for errors since they don't affect the zone */
4034
4035 return (0);
4036 }
4037
4038 /*
4039 * Unmount lofs mounts from higher level zones
4040 * Unshare nfs exported directories
4041 */
4042 static void
4043 tsol_unmounts(zlog_t *zlogp, char *zone_name)
4044 {
4045 zoneid_t *zids = NULL;
4046 uint_t nzents_saved;
4047 uint_t nzents;
4048 int i;
4049 char *argv[4];
4050 char path[MAXPATHLEN];
4051
4052 if (!is_system_labeled())
4053 return;
4054
4055 /*
4056 * Get the list of zones from the kernel
4057 */
4058 if (zone_list(NULL, &nzents) != 0) {
4059 return;
4060 }
4061
4062 if (zid_label == NULL) {
4063 zid_label = m_label_alloc(MAC_LABEL);
4064 if (zid_label == NULL)
4065 return;
4066 }
4067
4068 again:
4069 if (nzents == 0)
4070 return;
4071
4072 zids = malloc(nzents * sizeof (zoneid_t));
4073 if (zids == NULL) {
4074 zerror(zlogp, B_TRUE, "memory allocation failed");
4075 return;
4076 }
4077 nzents_saved = nzents;
4078
4079 if (zone_list(zids, &nzents) != 0) {
4080 free(zids);
4081 return;
4082 }
4083 if (nzents != nzents_saved) {
4084 /* list changed, try again */
4085 free(zids);
4086 goto again;
4087 }
4088
4089 for (i = 0; i < nzents; i++) {
4090 char zid_name[ZONENAME_MAX];
4091 zone_state_t zid_state;
4092 char zid_rpath[MAXPATHLEN];
4093
4094 if (zids[i] == GLOBAL_ZONEID)
4095 continue;
4096
4097 if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
4098 continue;
4099
4100 /*
4101 * Skip the zone we are halting
4102 */
4103 if (strcmp(zid_name, zone_name) == 0)
4104 continue;
4105
4106 if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
4107 sizeof (zid_state)) < 0) ||
4108 (zid_state < ZONE_IS_READY))
4109 /* Skip over zones without mounted filesystems */
4110 continue;
4111
4112 if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
4113 sizeof (m_label_t)) < 0)
4114 /* Skip over zones with unspecified label */
4115 continue;
4116
4117 if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
4118 sizeof (zid_rpath)) == -1)
4119 /* Skip over zones with bad path */
4120 continue;
4121
4122 if (zlabel != NULL && bldominates(zid_label, zlabel)) {
4123 /*
4124 * This zone dominates our zone.
4125 * Unmount the lofs mount of our zone's /export/home
4126 */
4127
4128 if (snprintf(path, MAXPATHLEN,
4129 "%s/zone/%s/export/home", zid_rpath,
4130 zone_name) > MAXPATHLEN)
4131 continue;
4132
4133 /* Skip over mount failures */
4134 (void) umount(path);
4135 }
4136 }
4137 free(zids);
4138
4139 /*
4140 * Unmount global zone autofs trigger for this zone
4141 */
4142 (void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
4143 /* Skip over mount failures */
4144 (void) umount(path);
4145
4146 /*
4147 * Next unshare any exported directories from this zone.
4148 */
4149
4150 argv[0] = "zoneunshare";
4151 argv[1] = "-z";
4152 argv[2] = zone_name;
4153 argv[3] = NULL;
4154
4155 (void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
4156 /* Don't check for errors since they don't affect the zone */
4157
4158 /*
4159 * Finally, deallocate any devices in the zone.
4160 */
4161
4162 argv[0] = "deallocate";
4163 argv[1] = "-Isz";
4164 argv[2] = zone_name;
4165 argv[3] = NULL;
4166
4167 (void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
4168 /* Don't check for errors since they don't affect the zone */
4169 }
4170
4171 /*
4172 * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
4173 * this zone.
4174 */
4175 static tsol_zcent_t *
4176 get_zone_label(zlog_t *zlogp, priv_set_t *privs)
4177 {
4178 FILE *fp;
4179 tsol_zcent_t *zcent = NULL;
4180 char line[MAXTNZLEN];
4181
4182 if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
4183 zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
4184 return (NULL);
4185 }
4186
4187 while (fgets(line, sizeof (line), fp) != NULL) {
4188 /*
4189 * Check for malformed database
4190 */
4191 if (strlen(line) == MAXTNZLEN - 1)
4192 break;
4193 if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
4194 continue;
4195 if (strcmp(zcent->zc_name, zone_name) == 0)
4196 break;
4197 tsol_freezcent(zcent);
4198 zcent = NULL;
4199 }
4200 (void) fclose(fp);
4201
4202 if (zcent == NULL) {
4203 zerror(zlogp, B_FALSE, "zone requires a label assignment. "
4204 "See tnzonecfg(4)");
4205 } else {
4206 if (zlabel == NULL)
4207 zlabel = m_label_alloc(MAC_LABEL);
4208 /*
4209 * Save this zone's privileges for later read-down processing
4210 */
4211 if ((zprivs = priv_allocset()) == NULL) {
4212 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
4213 return (NULL);
4214 } else {
4215 priv_copyset(privs, zprivs);
4216 }
4217 }
4218 return (zcent);
4219 }
4220
4221 /*
4222 * Add the Trusted Extensions multi-level ports for this zone.
4223 */
4224 static void
4225 set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
4226 {
4227 tsol_mlp_t *mlp;
4228 tsol_mlpent_t tsme;
4229
4230 if (!is_system_labeled())
4231 return;
4232
4233 tsme.tsme_zoneid = zoneid;
4234 tsme.tsme_flags = 0;
4235 for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
4236 tsme.tsme_mlp = *mlp;
4237 if (tnmlp(TNDB_LOAD, &tsme) != 0) {
4238 zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
4239 "on %d-%d/%d", mlp->mlp_port,
4240 mlp->mlp_port_upper, mlp->mlp_ipp);
4241 }
4242 }
4243
4244 tsme.tsme_flags = TSOL_MEF_SHARED;
4245 for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
4246 tsme.tsme_mlp = *mlp;
4247 if (tnmlp(TNDB_LOAD, &tsme) != 0) {
4248 zerror(zlogp, B_TRUE, "cannot set shared MLP "
4249 "on %d-%d/%d", mlp->mlp_port,
4250 mlp->mlp_port_upper, mlp->mlp_ipp);
4251 }
4252 }
4253 }
4254
4255 static void
4256 remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
4257 {
4258 tsol_mlpent_t tsme;
4259
4260 if (!is_system_labeled())
4261 return;
4262
4263 (void) memset(&tsme, 0, sizeof (tsme));
4264 tsme.tsme_zoneid = zoneid;
4265 if (tnmlp(TNDB_FLUSH, &tsme) != 0)
4266 zerror(zlogp, B_TRUE, "cannot flush MLPs");
4267 }
4268
4269 int
4270 prtmount(const struct mnttab *fs, void *x) {
4271 zerror((zlog_t *)x, B_FALSE, " %s", fs->mnt_mountp);
4272 return (0);
4273 }
4274
4275 /*
4276 * Look for zones running on the main system that are using this root (or any
4277 * subdirectory of it). Return B_TRUE and print an error if a conflicting zone
4278 * is found or if we can't tell.
4279 */
4280 static boolean_t
4281 duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
4282 {
4283 zoneid_t *zids = NULL;
4284 uint_t nzids = 0;
4285 boolean_t retv;
4286 int rlen, zlen;
4287 char zroot[MAXPATHLEN];
4288 char zonename[ZONENAME_MAX];
4289
4290 for (;;) {
4291 nzids += 10;
4292 zids = malloc(nzids * sizeof (*zids));
4293 if (zids == NULL) {
4294 zerror(zlogp, B_TRUE, "memory allocation failed");
4295 return (B_TRUE);
4296 }
4297 if (zone_list(zids, &nzids) == 0)
4298 break;
4299 free(zids);
4300 }
4301 retv = B_FALSE;
4302 rlen = strlen(rootpath);
4303 while (nzids > 0) {
4304 /*
4305 * Ignore errors; they just mean that the zone has disappeared
4306 * while we were busy.
4307 */
4308 if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
4309 sizeof (zroot)) == -1)
4310 continue;
4311 zlen = strlen(zroot);
4312 if (zlen > rlen)
4313 zlen = rlen;
4314 if (strncmp(rootpath, zroot, zlen) == 0 &&
4315 (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
4316 (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
4317 if (getzonenamebyid(zids[nzids], zonename,
4318 sizeof (zonename)) == -1)
4319 (void) snprintf(zonename, sizeof (zonename),
4320 "id %d", (int)zids[nzids]);
4321 zerror(zlogp, B_FALSE,
4322 "zone root %s already in use by zone %s",
4323 rootpath, zonename);
4324 retv = B_TRUE;
4325 break;
4326 }
4327 }
4328 free(zids);
4329 return (retv);
4330 }
4331
4332 /*
4333 * Search for loopback mounts that use this same source node (same device and
4334 * inode). Return B_TRUE if there is one or if we can't tell.
4335 */
4336 static boolean_t
4337 duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
4338 {
4339 struct stat64 rst, zst;
4340 struct mnttab *mnp;
4341
4342 if (stat64(rootpath, &rst) == -1) {
4343 zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
4344 return (B_TRUE);
4345 }
4346 if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
4347 return (B_TRUE);
4348 for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
4349 if (mnp->mnt_fstype == NULL ||
4350 strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
4351 continue;
4352 /* We're looking at a loopback mount. Stat it. */
4353 if (mnp->mnt_special != NULL &&
4354 stat64(mnp->mnt_special, &zst) != -1 &&
4355 rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
4356 zerror(zlogp, B_FALSE,
4357 "zone root %s is reachable through %s",
4358 rootpath, mnp->mnt_mountp);
4359 return (B_TRUE);
4360 }
4361 }
4362 return (B_FALSE);
4363 }
4364
4365 /*
4366 * Set memory cap and pool info for the zone's resource management
4367 * configuration.
4368 */
4369 static int
4370 setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
4371 {
4372 int res;
4373 uint64_t tmp;
4374 struct zone_mcaptab mcap;
4375 char sched[MAXNAMELEN];
4376 zone_dochandle_t handle = NULL;
4377 char pool_err[128];
4378
4379 if ((handle = zonecfg_init_handle()) == NULL) {
4380 zerror(zlogp, B_TRUE, "getting zone configuration handle");
4381 return (Z_BAD_HANDLE);
4382 }
4383
4384 if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
4385 zerror(zlogp, B_FALSE, "invalid configuration");
4386 zonecfg_fini_handle(handle);
4387 return (res);
4388 }
4389
4390 /*
4391 * If a memory cap is configured, set the cap in the kernel using
4392 * zone_setattr() and make sure the rcapd SMF service is enabled.
4393 */
4394 if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
4395 uint64_t num;
4396 char smf_err[128];
4397
4398 num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
4399 if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
4400 zerror(zlogp, B_TRUE, "could not set zone memory cap");
4401 zonecfg_fini_handle(handle);
4402 return (Z_INVAL);
4403 }
4404
4405 if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
4406 zerror(zlogp, B_FALSE, "enabling system/rcap service "
4407 "failed: %s", smf_err);
4408 zonecfg_fini_handle(handle);
4409 return (Z_INVAL);
4410 }
4411 }
4412
4413 /* Get the scheduling class set in the zone configuration. */
4414 if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
4415 strlen(sched) > 0) {
4416 if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
4417 strlen(sched)) == -1)
4418 zerror(zlogp, B_TRUE, "WARNING: unable to set the "
4419 "default scheduling class");
4420
4421 } else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
4422 == Z_OK) {
4423 /*
4424 * If the zone has the zone.cpu-shares rctl set then we want to
4425 * use the Fair Share Scheduler (FSS) for processes in the
4426 * zone. Check what scheduling class the zone would be running
4427 * in by default so we can print a warning and modify the class
4428 * if we wouldn't be using FSS.
4429 */
4430 char class_name[PC_CLNMSZ];
4431
4432 if (zonecfg_get_dflt_sched_class(handle, class_name,
4433 sizeof (class_name)) != Z_OK) {
4434 zerror(zlogp, B_FALSE, "WARNING: unable to determine "
4435 "the zone's scheduling class");
4436
4437 } else if (strcmp("FSS", class_name) != 0) {
4438 zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
4439 "rctl is set but\nFSS is not the default "
4440 "scheduling class for\nthis zone. FSS will be "
4441 "used for processes\nin the zone but to get the "
4442 "full benefit of FSS,\nit should be the default "
4443 "scheduling class.\nSee dispadmin(1M) for more "
4444 "details.");
4445
4446 if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
4447 strlen("FSS")) == -1)
4448 zerror(zlogp, B_TRUE, "WARNING: unable to set "
4449 "zone scheduling class to FSS");
4450 }
4451 }
4452
4453 /*
4454 * The next few blocks of code attempt to set up temporary pools as
4455 * well as persistent pools. In all cases we call the functions
4456 * unconditionally. Within each funtion the code will check if the
4457 * zone is actually configured for a temporary pool or persistent pool
4458 * and just return if there is nothing to do.
4459 *
4460 * If we are rebooting we want to attempt to reuse any temporary pool
4461 * that was previously set up. zonecfg_bind_tmp_pool() will do the
4462 * right thing in all cases (reuse or create) based on the current
4463 * zonecfg.
4464 */
4465 if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
4466 sizeof (pool_err))) != Z_OK) {
4467 if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
4468 zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
4469 "cannot be instantiated", zonecfg_strerror(res),
4470 pool_err);
4471 else
4472 zerror(zlogp, B_FALSE, "could not bind zone to "
4473 "temporary pool: %s", zonecfg_strerror(res));
4474 zonecfg_fini_handle(handle);
4475 return (Z_POOL_BIND);
4476 }
4477
4478 /*
4479 * Check if we need to warn about poold not being enabled.
4480 */
4481 if (zonecfg_warn_poold(handle)) {
4482 zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
4483 "been specified\nbut the dynamic pool service is not "
4484 "enabled.\nThe system will not dynamically adjust the\n"
4485 "processor allocation within the specified range\n"
4486 "until svc:/system/pools/dynamic is enabled.\n"
4487 "See poold(1M).");
4488 }
4489
4490 /* The following is a warning, not an error. */
4491 if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
4492 sizeof (pool_err))) != Z_OK) {
4493 if (res == Z_POOL_BIND)
4494 zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
4495 "pool '%s'; using default pool.", pool_err);
4496 else if (res == Z_POOL)
4497 zerror(zlogp, B_FALSE, "WARNING: %s: %s",
4498 zonecfg_strerror(res), pool_err);
4499 else
4500 zerror(zlogp, B_FALSE, "WARNING: %s",
4501 zonecfg_strerror(res));
4502 }
4503
4504 /* Update saved pool name in case it has changed */
4505 (void) zonecfg_get_poolname(handle, zone_name, pool_name,
4506 sizeof (pool_name));
4507
4508 zonecfg_fini_handle(handle);
4509 return (Z_OK);
4510 }
4511
4512 static void
4513 report_prop_err(zlog_t *zlogp, const char *name, const char *value, int res)
4514 {
4515 switch (res) {
4516 case Z_TOO_BIG:
4517 zerror(zlogp, B_FALSE, "%s property value is too large.", name);
4518 break;
4519
4520 case Z_INVALID_PROPERTY:
4521 zerror(zlogp, B_FALSE, "%s property value \"%s\" is not valid",
4522 name, value);
4523 break;
4524
4525 default:
4526 zerror(zlogp, B_TRUE, "fetching property %s: %d", name, res);
4527 break;
4528 }
4529 }
4530
4531 /*
4532 * Sets the hostid of the new zone based on its configured value. The zone's
4533 * zone_t structure must already exist in kernel memory. 'zlogp' refers to the
4534 * log used to report errors and warnings and must be non-NULL. 'zone_namep'
4535 * is the name of the new zone and must be non-NULL. 'zoneid' is the numeric
4536 * ID of the new zone.
4537 *
4538 * This function returns zero on success and a nonzero error code on failure.
4539 */
4540 static int
4541 setup_zone_hostid(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
4542 {
4543 int res;
4544 char hostidp[HW_HOSTID_LEN];
4545 unsigned int hostid;
4546
4547 res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp));
4548
4549 if (res == Z_BAD_PROPERTY) {
4550 return (Z_OK);
4551 } else if (res != Z_OK) {
4552 report_prop_err(zlogp, "hostid", hostidp, res);
4553 return (res);
4554 }
4555
4556 hostid = (unsigned int)strtoul(hostidp, NULL, 16);
4557 if ((res = zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid,
4558 sizeof (hostid))) != 0) {
4559 zerror(zlogp, B_TRUE,
4560 "zone hostid is not valid: %s: %d", hostidp, res);
4561 return (Z_SYSTEM);
4562 }
4563
4564 return (res);
4565 }
4566
4567 static int
4568 setup_zone_fs_allowed(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
4569 {
4570 char fsallowed[ZONE_FS_ALLOWED_MAX];
4571 char *fsallowedp = fsallowed;
4572 int len = sizeof (fsallowed);
4573 int res;
4574
4575 res = zonecfg_get_fs_allowed(handle, fsallowed, len);
4576
4577 if (res == Z_BAD_PROPERTY) {
4578 /* No value, set the defaults */
4579 (void) strlcpy(fsallowed, DFLT_FS_ALLOWED, len);
4580 } else if (res != Z_OK) {
4581 report_prop_err(zlogp, "fs-allowed", fsallowed, res);
4582 return (res);
4583 } else if (fsallowed[0] == '-') {
4584 /* dropping default privs - use remaining list */
4585 if (fsallowed[1] != ',')
4586 return (Z_OK);
4587 fsallowedp += 2;
4588 len -= 2;
4589 } else {
4590 /* Has a value, append the defaults */
4591 if (strlcat(fsallowed, ",", len) >= len ||
4592 strlcat(fsallowed, DFLT_FS_ALLOWED, len) >= len) {
4593 report_prop_err(zlogp, "fs-allowed", fsallowed,
4594 Z_TOO_BIG);
4595 return (Z_TOO_BIG);
4596 }
4597 }
4598
4599 if (zone_setattr(zoneid, ZONE_ATTR_FS_ALLOWED, fsallowedp, len) != 0) {
4600 zerror(zlogp, B_TRUE,
4601 "fs-allowed couldn't be set: %s: %d", fsallowedp, res);
4602 return (Z_SYSTEM);
4603 }
4604
4605 return (Z_OK);
4606 }
4607
4608 static int
4609 setup_zone_attrs(zlog_t *zlogp, char *zone_namep, zoneid_t zoneid)
4610 {
4611 zone_dochandle_t handle;
4612 int res = Z_OK;
4613
4614 if ((handle = zonecfg_init_handle()) == NULL) {
4615 zerror(zlogp, B_TRUE, "getting zone configuration handle");
4616 return (Z_BAD_HANDLE);
4617 }
4618 if ((res = zonecfg_get_snapshot_handle(zone_namep, handle)) != Z_OK) {
4619 zerror(zlogp, B_FALSE, "invalid configuration");
4620 goto out;
4621 }
4622
4623 if ((res = setup_zone_hostid(handle, zlogp, zoneid)) != Z_OK)
4624 goto out;
4625
4626 if ((res = setup_zone_fs_allowed(handle, zlogp, zoneid)) != Z_OK)
4627 goto out;
4628
4629 out:
4630 zonecfg_fini_handle(handle);
4631 return (res);
4632 }
4633
4634 zoneid_t
4635 vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
4636 {
4637 zoneid_t rval = -1;
4638 priv_set_t *privs;
4639 char rootpath[MAXPATHLEN];
4640 char *rctlbuf = NULL;
4641 size_t rctlbufsz = 0;
4642 char *zfsbuf = NULL;
4643 size_t zfsbufsz = 0;
4644 zoneid_t zoneid = -1;
4645 int xerr;
4646 char *kzone;
4647 FILE *fp = NULL;
4648 tsol_zcent_t *zcent = NULL;
4649 int match = 0;
4650 int doi = 0;
4651 int flags;
4652 zone_iptype_t iptype;
4653
4654 if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
4655 zerror(zlogp, B_TRUE, "unable to determine zone root");
4656 return (-1);
4657 }
4658 if (zonecfg_in_alt_root())
4659 resolve_lofs(zlogp, rootpath, sizeof (rootpath));
4660
4661 if (vplat_get_iptype(zlogp, &iptype) < 0) {
4662 zerror(zlogp, B_TRUE, "unable to determine ip-type");
4663 return (-1);
4664 }
4665 switch (iptype) {
4666 case ZS_SHARED:
4667 flags = 0;
4668 break;
4669 case ZS_EXCLUSIVE:
4670 flags = ZCF_NET_EXCL;
4671 break;
4672 }
4673
4674 if ((privs = priv_allocset()) == NULL) {
4675 zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
4676 return (-1);
4677 }
4678 priv_emptyset(privs);
4679 if (get_privset(zlogp, privs, mount_cmd) != 0)
4680 goto error;
4681
4682 if (mount_cmd == Z_MNT_BOOT &&
4683 get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
4684 zerror(zlogp, B_FALSE, "Unable to get list of rctls");
4685 goto error;
4686 }
4687
4688 if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4689 zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4690 goto error;
4691 }
4692
4693 if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
4694 zcent = get_zone_label(zlogp, privs);
4695 if (zcent != NULL) {
4696 match = zcent->zc_match;
4697 doi = zcent->zc_doi;
4698 *zlabel = zcent->zc_label;
4699 } else {
4700 goto error;
4701 }
4702 if (validate_rootds_label(zlogp, rootpath, zlabel) != 0)
4703 goto error;
4704 }
4705
4706 kzone = zone_name;
4707
4708 /*
4709 * We must do this scan twice. First, we look for zones running on the
4710 * main system that are using this root (or any subdirectory of it).
4711 * Next, we reduce to the shortest path and search for loopback mounts
4712 * that use this same source node (same device and inode).
4713 */
4714 if (duplicate_zone_root(zlogp, rootpath))
4715 goto error;
4716 if (duplicate_reachable_path(zlogp, rootpath))
4717 goto error;
4718
4719 if (ALT_MOUNT(mount_cmd)) {
4720 root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4721
4722 /*
4723 * Forge up a special root for this zone. When a zone is
4724 * mounted, we can't let the zone have its own root because the
4725 * tools that will be used in this "scratch zone" need access
4726 * to both the zone's resources and the running machine's
4727 * executables.
4728 *
4729 * Note that the mkdir here also catches read-only filesystems.
4730 */
4731 if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4732 zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4733 goto error;
4734 }
4735 if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4736 goto error;
4737 }
4738
4739 if (zonecfg_in_alt_root()) {
4740 /*
4741 * If we are mounting up a zone in an alternate root partition,
4742 * then we have some additional work to do before starting the
4743 * zone. First, resolve the root path down so that we're not
4744 * fooled by duplicates. Then forge up an internal name for
4745 * the zone.
4746 */
4747 if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4748 zerror(zlogp, B_TRUE, "cannot open mapfile");
4749 goto error;
4750 }
4751 if (zonecfg_lock_scratch(fp) != 0) {
4752 zerror(zlogp, B_TRUE, "cannot lock mapfile");
4753 goto error;
4754 }
4755 if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4756 NULL, 0) == 0) {
4757 zerror(zlogp, B_FALSE, "scratch zone already running");
4758 goto error;
4759 }
4760 /* This is the preferred name */
4761 (void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4762 zone_name);
4763 srandom(getpid());
4764 while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4765 0) == 0) {
4766 /* This is just an arbitrary name; note "." usage */
4767 (void) snprintf(kernzone, sizeof (kernzone),
4768 "SUNWlu.%08lX%08lX", random(), random());
4769 }
4770 kzone = kernzone;
4771 }
4772
4773 xerr = 0;
4774 if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4775 rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4776 flags)) == -1) {
4777 if (xerr == ZE_AREMOUNTS) {
4778 if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
4779 zerror(zlogp, B_FALSE,
4780 "An unknown file-system is mounted on "
4781 "a subdirectory of %s", rootpath);
4782 } else {
4783
4784 zerror(zlogp, B_FALSE,
4785 "These file-systems are mounted on "
4786 "subdirectories of %s:", rootpath);
4787 (void) zonecfg_find_mounts(rootpath,
4788 prtmount, zlogp);
4789 }
4790 } else if (xerr == ZE_CHROOTED) {
4791 zerror(zlogp, B_FALSE, "%s: "
4792 "cannot create a zone from a chrooted "
4793 "environment", "zone_create");
4794 } else if (xerr == ZE_LABELINUSE) {
4795 char zonename[ZONENAME_MAX];
4796 (void) getzonenamebyid(getzoneidbylabel(zlabel),
4797 zonename, ZONENAME_MAX);
4798 zerror(zlogp, B_FALSE, "The zone label is already "
4799 "used by the zone '%s'.", zonename);
4800 } else {
4801 zerror(zlogp, B_TRUE, "%s failed", "zone_create");
4802 }
4803 goto error;
4804 }
4805
4806 if (zonecfg_in_alt_root() &&
4807 zonecfg_add_scratch(fp, zone_name, kernzone,
4808 zonecfg_get_root()) == -1) {
4809 zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4810 goto error;
4811 }
4812
4813 /*
4814 * The following actions are not performed when merely mounting a zone
4815 * for administrative use.
4816 */
4817 if (mount_cmd == Z_MNT_BOOT) {
4818 brand_handle_t bh;
4819 struct brand_attr attr;
4820 char modname[MAXPATHLEN];
4821
4822 if (setup_zone_attrs(zlogp, zone_name, zoneid) != Z_OK)
4823 goto error;
4824
4825 if ((bh = brand_open(brand_name)) == NULL) {
4826 zerror(zlogp, B_FALSE,
4827 "unable to determine brand name");
4828 goto error;
4829 }
4830
4831 if (!is_system_labeled() &&
4832 (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) {
4833 brand_close(bh);
4834 zerror(zlogp, B_FALSE,
4835 "cannot boot labeled zone on unlabeled system");
4836 goto error;
4837 }
4838
4839 /*
4840 * If this brand requires any kernel support, now is the time to
4841 * get it loaded and initialized.
4842 */
4843 if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4844 brand_close(bh);
4845 zerror(zlogp, B_FALSE,
4846 "unable to determine brand kernel module");
4847 goto error;
4848 }
4849 brand_close(bh);
4850
4851 if (strlen(modname) > 0) {
4852 (void) strlcpy(attr.ba_brandname, brand_name,
4853 sizeof (attr.ba_brandname));
4854 (void) strlcpy(attr.ba_modname, modname,
4855 sizeof (attr.ba_modname));
4856 if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
4857 sizeof (attr) != 0)) {
4858 zerror(zlogp, B_TRUE,
4859 "could not set zone brand attribute.");
4860 goto error;
4861 }
4862 }
4863
4864 if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK)
4865 goto error;
4866
4867 set_mlps(zlogp, zoneid, zcent);
4868 }
4869
4870 rval = zoneid;
4871 zoneid = -1;
4872
4873 error:
4874 if (zoneid != -1) {
4875 (void) zone_shutdown(zoneid);
4876 (void) zone_destroy(zoneid);
4877 }
4878 if (rctlbuf != NULL)
4879 free(rctlbuf);
4880 priv_freeset(privs);
4881 if (fp != NULL)
4882 zonecfg_close_scratch(fp);
4883 lofs_discard_mnttab();
4884 if (zcent != NULL)
4885 tsol_freezcent(zcent);
4886 return (rval);
4887 }
4888
4889 /*
4890 * Enter the zone and write a /etc/zones/index file there. This allows
4891 * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4892 * details from inside the zone.
4893 */
4894 static void
4895 write_index_file(zoneid_t zoneid)
4896 {
4897 FILE *zef;
4898 FILE *zet;
4899 struct zoneent *zep;
4900 pid_t child;
4901 int tmpl_fd;
4902 ctid_t ct;
4903 int fd;
4904 char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4905
4906 /* Locate the zone entry in the global zone's index file */
4907 if ((zef = setzoneent()) == NULL)
4908 return;
4909 while ((zep = getzoneent_private(zef)) != NULL) {
4910 if (strcmp(zep->zone_name, zone_name) == 0)
4911 break;
4912 free(zep);
4913 }
4914 endzoneent(zef);
4915 if (zep == NULL)
4916 return;
4917
4918 if ((tmpl_fd = init_template()) == -1) {
4919 free(zep);
4920 return;
4921 }
4922
4923 if ((child = fork()) == -1) {
4924 (void) ct_tmpl_clear(tmpl_fd);
4925 (void) close(tmpl_fd);
4926 free(zep);
4927 return;
4928 }
4929
4930 /* parent waits for child to finish */
4931 if (child != 0) {
4932 free(zep);
4933 if (contract_latest(&ct) == -1)
4934 ct = -1;
4935 (void) ct_tmpl_clear(tmpl_fd);
4936 (void) close(tmpl_fd);
4937 (void) waitpid(child, NULL, 0);
4938 (void) contract_abandon_id(ct);
4939 return;
4940 }
4941
4942 /* child enters zone and sets up index file */
4943 (void) ct_tmpl_clear(tmpl_fd);
4944 if (zone_enter(zoneid) != -1) {
4945 (void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4946 (void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4947 ZONE_CONFIG_GID);
4948 fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4949 ZONE_INDEX_MODE);
4950 if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4951 (void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4952 if (uuid_is_null(zep->zone_uuid))
4953 uuidstr[0] = '\0';
4954 else
4955 uuid_unparse(zep->zone_uuid, uuidstr);
4956 (void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4957 zone_state_str(zep->zone_state),
4958 uuidstr);
4959 (void) fclose(zet);
4960 }
4961 }
4962 _exit(0);
4963 }
4964
4965 int
4966 vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
4967 {
4968 char zonepath[MAXPATHLEN];
4969
4970 if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4971 lofs_discard_mnttab();
4972 return (-1);
4973 }
4974
4975 /*
4976 * Before we try to mount filesystems we need to create the
4977 * attribute backing store for /dev
4978 */
4979 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
4980 lofs_discard_mnttab();
4981 return (-1);
4982 }
4983 resolve_lofs(zlogp, zonepath, sizeof (zonepath));
4984
4985 /* Make /dev directory owned by root, grouped sys */
4986 if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
4987 0, 3) != 0) {
4988 lofs_discard_mnttab();
4989 return (-1);
4990 }
4991
4992 if (mount_filesystems(zlogp, mount_cmd) != 0) {
4993 lofs_discard_mnttab();
4994 return (-1);
4995 }
4996
4997 if (mount_cmd == Z_MNT_BOOT) {
4998 zone_iptype_t iptype;
4999
5000 if (vplat_get_iptype(zlogp, &iptype) < 0) {
5001 zerror(zlogp, B_TRUE, "unable to determine ip-type");
5002 lofs_discard_mnttab();
5003 return (-1);
5004 }
5005
5006 switch (iptype) {
5007 case ZS_SHARED:
5008 /* Always do this to make lo0 get configured */
5009 if (configure_shared_network_interfaces(zlogp) != 0) {
5010 lofs_discard_mnttab();
5011 return (-1);
5012 }
5013 break;
5014 case ZS_EXCLUSIVE:
5015 if (configure_exclusive_network_interfaces(zlogp,
5016 zoneid) !=
5017 0) {
5018 lofs_discard_mnttab();
5019 return (-1);
5020 }
5021 break;
5022 }
5023 }
5024
5025 write_index_file(zoneid);
5026
5027 lofs_discard_mnttab();
5028 return (0);
5029 }
5030
5031 static int
5032 lu_root_teardown(zlog_t *zlogp)
5033 {
5034 char zroot[MAXPATHLEN];
5035
5036 if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
5037 zerror(zlogp, B_FALSE, "unable to determine zone root");
5038 return (-1);
5039 }
5040 root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
5041
5042 /*
5043 * At this point, the processes are gone, the filesystems (save the
5044 * root) are unmounted, and the zone is on death row. But there may
5045 * still be creds floating about in the system that reference the
5046 * zone_t, and which pin down zone_rootvp causing this call to fail
5047 * with EBUSY. Thus, we try for a little while before just giving up.
5048 * (How I wish this were not true, and umount2 just did the right
5049 * thing, or tmpfs supported MS_FORCE This is a gross hack.)
5050 */
5051 if (umount2(zroot, MS_FORCE) != 0) {
5052 if (errno == ENOTSUP && umount2(zroot, 0) == 0)
5053 goto unmounted;
5054 if (errno == EBUSY) {
5055 int tries = 10;
5056
5057 while (--tries >= 0) {
5058 (void) sleep(1);
5059 if (umount2(zroot, 0) == 0)
5060 goto unmounted;
5061 if (errno != EBUSY)
5062 break;
5063 }
5064 }
5065 zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
5066 return (-1);
5067 }
5068 unmounted:
5069
5070 /*
5071 * Only zones in an alternate root environment have scratch zone
5072 * entries.
5073 */
5074 if (zonecfg_in_alt_root()) {
5075 FILE *fp;
5076 int retv;
5077
5078 if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5079 zerror(zlogp, B_TRUE, "cannot open mapfile");
5080 return (-1);
5081 }
5082 retv = -1;
5083 if (zonecfg_lock_scratch(fp) != 0)
5084 zerror(zlogp, B_TRUE, "cannot lock mapfile");
5085 else if (zonecfg_delete_scratch(fp, kernzone) != 0)
5086 zerror(zlogp, B_TRUE, "cannot delete map entry");
5087 else
5088 retv = 0;
5089 zonecfg_close_scratch(fp);
5090 return (retv);
5091 } else {
5092 return (0);
5093 }
5094 }
5095
5096 int
5097 vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
5098 {
5099 char *kzone;
5100 zoneid_t zoneid;
5101 int res;
5102 char pool_err[128];
5103 char zpath[MAXPATHLEN];
5104 char cmdbuf[MAXPATHLEN];
5105 brand_handle_t bh = NULL;
5106 dladm_status_t status;
5107 char errmsg[DLADM_STRSIZE];
5108 ushort_t flags;
5109
5110 kzone = zone_name;
5111 if (zonecfg_in_alt_root()) {
5112 FILE *fp;
5113
5114 if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5115 zerror(zlogp, B_TRUE, "unable to open map file");
5116 goto error;
5117 }
5118 if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
5119 kernzone, sizeof (kernzone)) != 0) {
5120 zerror(zlogp, B_FALSE, "unable to find scratch zone");
5121 zonecfg_close_scratch(fp);
5122 goto error;
5123 }
5124 zonecfg_close_scratch(fp);
5125 kzone = kernzone;
5126 }
5127
5128 if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
5129 if (!bringup_failure_recovery)
5130 zerror(zlogp, B_TRUE, "unable to get zoneid");
5131 if (unmount_cmd)
5132 (void) lu_root_teardown(zlogp);
5133 goto error;
5134 }
5135
5136 if (remove_datalink_pool(zlogp, zoneid) != 0)
5137 zerror(zlogp, B_FALSE, "unable clear datalink pool property");
5138
5139 if (remove_datalink_protect(zlogp, zoneid) != 0)
5140 zerror(zlogp, B_FALSE,
5141 "unable clear datalink protect property");
5142
5143 /*
5144 * The datalinks assigned to the zone will be removed from the NGZ as
5145 * part of zone_shutdown() so that we need to remove protect/pool etc.
5146 * before zone_shutdown(). Even if the shutdown itself fails, the zone
5147 * will not be able to violate any constraints applied because the
5148 * datalinks are no longer available to the zone.
5149 */
5150 if (zone_shutdown(zoneid) != 0) {
5151 zerror(zlogp, B_TRUE, "unable to shutdown zone");
5152 goto error;
5153 }
5154
5155 /* Get the zonepath of this zone */
5156 if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
5157 zerror(zlogp, B_FALSE, "unable to determine zone path");
5158 goto error;
5159 }
5160
5161 /* Get a handle to the brand info for this zone */
5162 if ((bh = brand_open(brand_name)) == NULL) {
5163 zerror(zlogp, B_FALSE, "unable to determine zone brand");
5164 return (-1);
5165 }
5166 /*
5167 * If there is a brand 'halt' callback, execute it now to give the
5168 * brand a chance to cleanup any custom configuration.
5169 */
5170 (void) strcpy(cmdbuf, EXEC_PREFIX);
5171 if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
5172 sizeof (cmdbuf) - EXEC_LEN) < 0) {
5173 brand_close(bh);
5174 zerror(zlogp, B_FALSE, "unable to determine branded zone's "
5175 "halt callback.");
5176 goto error;
5177 }
5178 brand_close(bh);
5179
5180 if ((strlen(cmdbuf) > EXEC_LEN) &&
5181 (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
5182 zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
5183 goto error;
5184 }
5185
5186 if (!unmount_cmd) {
5187 zone_iptype_t iptype;
5188
5189 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
5190 sizeof (flags)) < 0) {
5191 if (vplat_get_iptype(zlogp, &iptype) < 0) {
5192 zerror(zlogp, B_TRUE, "unable to determine "
5193 "ip-type");
5194 goto error;
5195 }
5196 } else {
5197 if (flags & ZF_NET_EXCL)
5198 iptype = ZS_EXCLUSIVE;
5199 else
5200 iptype = ZS_SHARED;
5201 }
5202
5203 switch (iptype) {
5204 case ZS_SHARED:
5205 if (unconfigure_shared_network_interfaces(zlogp,
5206 zoneid) != 0) {
5207 zerror(zlogp, B_FALSE, "unable to unconfigure "
5208 "network interfaces in zone");
5209 goto error;
5210 }
5211 break;
5212 case ZS_EXCLUSIVE:
5213 status = dladm_zone_halt(dld_handle, zoneid);
5214 if (status != DLADM_STATUS_OK) {
5215 zerror(zlogp, B_FALSE, "unable to notify "
5216 "dlmgmtd of zone halt: %s",
5217 dladm_status2str(status, errmsg));
5218 }
5219 break;
5220 }
5221 }
5222
5223 if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
5224 zerror(zlogp, B_TRUE, "unable to abort TCP connections");
5225 goto error;
5226 }
5227
5228 if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
5229 zerror(zlogp, B_FALSE,
5230 "unable to unmount file systems in zone");
5231 goto error;
5232 }
5233
5234 /*
5235 * If we are rebooting then we normally don't want to destroy an
5236 * existing temporary pool at this point so that we can just reuse it
5237 * when the zone boots back up. However, it is also possible we were
5238 * running with a temporary pool and the zone configuration has been
5239 * modified to no longer use a temporary pool. In that case we need
5240 * to destroy the temporary pool now. This case looks like the case
5241 * where we never had a temporary pool configured but
5242 * zonecfg_destroy_tmp_pool will do the right thing either way.
5243 */
5244 if (!unmount_cmd) {
5245 boolean_t destroy_tmp_pool = B_TRUE;
5246
5247 if (rebooting) {
5248 struct zone_psettab pset_tab;
5249 zone_dochandle_t handle;
5250
5251 if ((handle = zonecfg_init_handle()) != NULL &&
5252 zonecfg_get_handle(zone_name, handle) == Z_OK &&
5253 zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
5254 destroy_tmp_pool = B_FALSE;
5255
5256 zonecfg_fini_handle(handle);
5257 }
5258
5259 if (destroy_tmp_pool) {
5260 if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
5261 sizeof (pool_err))) != Z_OK) {
5262 if (res == Z_POOL)
5263 zerror(zlogp, B_FALSE, pool_err);
5264 }
5265 }
5266 }
5267
5268 remove_mlps(zlogp, zoneid);
5269
5270 if (zone_destroy(zoneid) != 0) {
5271 zerror(zlogp, B_TRUE, "unable to destroy zone");
5272 goto error;
5273 }
5274
5275 /*
5276 * Special teardown for alternate boot environments: remove the tmpfs
5277 * root for the zone and then remove it from the map file.
5278 */
5279 if (unmount_cmd && lu_root_teardown(zlogp) != 0)
5280 goto error;
5281
5282 lofs_discard_mnttab();
5283 return (0);
5284
5285 error:
5286 lofs_discard_mnttab();
5287 return (-1);
5288 }