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