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