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