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