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