17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21ffbafc53Scomay
227c478bd9Sstevel@tonic-gate /*
230fbb751dSJohn Levon * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
248cd81a20SJerry Jelinek * Copyright (c) 2013, Joyent Inc. All rights reserved.
25*9a686fbcSPaul Dagnelie * Copyright (c) 2015 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
29a3002e0aSGarrett D'Amore * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
30a3002e0aSGarrett D'Amore */
31a3002e0aSGarrett D'Amore
32a3002e0aSGarrett D'Amore /*
337c478bd9Sstevel@tonic-gate * This module contains functions used to bring up and tear down the
347c478bd9Sstevel@tonic-gate * Virtual Platform: [un]mounting file-systems, [un]plumbing network
357c478bd9Sstevel@tonic-gate * interfaces, [un]configuring devices, establishing resource controls,
367c478bd9Sstevel@tonic-gate * and creating/destroying the zone in the kernel. These actions, on
377c478bd9Sstevel@tonic-gate * the way up, ready the zone; on the way down, they halt the zone.
387c478bd9Sstevel@tonic-gate * See the much longer block comment at the beginning of zoneadmd.c
397c478bd9Sstevel@tonic-gate * for a bigger picture of how the whole program functions.
40108322fbScarlsonj *
41108322fbScarlsonj * This module also has primary responsibility for the layout of "scratch
42108322fbScarlsonj * zones." These are mounted, but inactive, zones that are used during
43108322fbScarlsonj * operating system upgrade and potentially other administrative action. The
44108322fbScarlsonj * scratch zone environment is similar to the miniroot environment. The zone's
45108322fbScarlsonj * actual root is mounted read-write on /a, and the standard paths (/usr,
46108322fbScarlsonj * /sbin, /lib) all lead to read-only copies of the running system's binaries.
47108322fbScarlsonj * This allows the administrative tools to manipulate the zone using "-R /a"
48108322fbScarlsonj * without relying on any binaries in the zone itself.
49108322fbScarlsonj *
50108322fbScarlsonj * If the scratch zone is on an alternate root (Live Upgrade [LU] boot
51108322fbScarlsonj * environment), then we must resolve the lofs mounts used there to uncover
52108322fbScarlsonj * writable (unshared) resources. Shared resources, though, are always
53108322fbScarlsonj * read-only. In addition, if the "same" zone with a different root path is
54108322fbScarlsonj * currently running, then "/b" inside the zone points to the running zone's
55108322fbScarlsonj * root. This allows LU to synchronize configuration files during the upgrade
56108322fbScarlsonj * process.
57108322fbScarlsonj *
58108322fbScarlsonj * To construct this environment, this module creates a tmpfs mount on
59108322fbScarlsonj * $ZONEPATH/lu. Inside this scratch area, the miniroot-like environment as
60108322fbScarlsonj * described above is constructed on the fly. The zone is then created using
61108322fbScarlsonj * $ZONEPATH/lu as the root.
62108322fbScarlsonj *
63108322fbScarlsonj * Note that scratch zones are inactive. The zone's bits are not running and
64108322fbScarlsonj * likely cannot be run correctly until upgrade is done. Init is not running
65108322fbScarlsonj * there, nor is SMF. Because of this, the "mounted" state of a scratch zone
66108322fbScarlsonj * is not a part of the usual halt/ready/boot state machine.
677c478bd9Sstevel@tonic-gate */
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate #include <sys/param.h>
707c478bd9Sstevel@tonic-gate #include <sys/mount.h>
717c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
727c478bd9Sstevel@tonic-gate #include <sys/socket.h>
737c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
747c478bd9Sstevel@tonic-gate #include <sys/types.h>
757c478bd9Sstevel@tonic-gate #include <sys/stat.h>
767c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
777c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
787c478bd9Sstevel@tonic-gate #include <sys/conf.h>
795679c89fSjv227347 #include <sys/systeminfo.h>
807c478bd9Sstevel@tonic-gate
81f4b3ec61Sdh155122 #include <libdlpi.h>
82f595a68aSyz147064 #include <libdllink.h>
83d62bc4baSyz147064 #include <libdlvlan.h>
84f4b3ec61Sdh155122
857c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
867c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
877c478bd9Sstevel@tonic-gate #include <netinet/in.h>
887c478bd9Sstevel@tonic-gate #include <net/route.h>
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate #include <stdio.h>
917c478bd9Sstevel@tonic-gate #include <errno.h>
927c478bd9Sstevel@tonic-gate #include <fcntl.h>
937c478bd9Sstevel@tonic-gate #include <unistd.h>
947c478bd9Sstevel@tonic-gate #include <rctl.h>
957c478bd9Sstevel@tonic-gate #include <stdlib.h>
967c478bd9Sstevel@tonic-gate #include <string.h>
977c478bd9Sstevel@tonic-gate #include <strings.h>
987c478bd9Sstevel@tonic-gate #include <wait.h>
997c478bd9Sstevel@tonic-gate #include <limits.h>
1007c478bd9Sstevel@tonic-gate #include <libgen.h>
101fa9e4066Sahrens #include <libzfs.h>
102facf4a8dSllai1 #include <libdevinfo.h>
1037c478bd9Sstevel@tonic-gate #include <zone.h>
1047c478bd9Sstevel@tonic-gate #include <assert.h>
105555afedfScarlsonj #include <libcontract.h>
106555afedfScarlsonj #include <libcontract_priv.h>
107555afedfScarlsonj #include <uuid/uuid.h>
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
1107c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
1117c478bd9Sstevel@tonic-gate #include <sys/fs/autofs.h> /* for _autofssys() */
1127c478bd9Sstevel@tonic-gate #include <sys/fs/lofs_info.h>
113fa9e4066Sahrens #include <sys/fs/zfs.h>
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate #include <pool.h>
1167c478bd9Sstevel@tonic-gate #include <sys/pool.h>
1170209230bSgjelinek #include <sys/priocntl.h>
1187c478bd9Sstevel@tonic-gate
1199acbbeafSnn35248 #include <libbrand.h>
1209acbbeafSnn35248 #include <sys/brand.h>
1217c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
12239d3e169Sevanl #include <synch.h>
12322321485Svp157776
1247c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
12545916cd2Sjpk #include <tsol/label.h>
12645916cd2Sjpk #include <libtsnet.h>
12745916cd2Sjpk #include <sys/priv.h>
128550b6e40SSowmini Varadhan #include <libinetutil.h>
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate #define V4_ADDR_LEN 32
1317c478bd9Sstevel@tonic-gate #define V6_ADDR_LEN 128
1327c478bd9Sstevel@tonic-gate
1336e1ae2a3SGary Pennington #define RESOURCE_DEFAULT_OPTS \
1347c478bd9Sstevel@tonic-gate MNTOPT_RO "," MNTOPT_LOFS_NOSUB "," MNTOPT_NODEVICES
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate #define DFSTYPES "/etc/dfs/fstypes"
13745916cd2Sjpk #define MAXTNZLEN 2048
1387c478bd9Sstevel@tonic-gate
1396cfd72c6Sgjelinek #define ALT_MOUNT(mount_cmd) ((mount_cmd) != Z_MNT_BOOT)
1406cfd72c6Sgjelinek
141ff19e029SMenno Lageman /* a reasonable estimate for the number of lwps per process */
142ff19e029SMenno Lageman #define LWPS_PER_PROCESS 10
143ff19e029SMenno Lageman
1447c478bd9Sstevel@tonic-gate /* for routing socket */
1457c478bd9Sstevel@tonic-gate static int rts_seqno = 0;
1467c478bd9Sstevel@tonic-gate
147108322fbScarlsonj /* mangled zone name when mounting in an alternate root environment */
148108322fbScarlsonj static char kernzone[ZONENAME_MAX];
149108322fbScarlsonj
150108322fbScarlsonj /* array of cached mount entries for resolve_lofs */
151108322fbScarlsonj static struct mnttab *resolve_lofs_mnts, *resolve_lofs_mnt_max;
152108322fbScarlsonj
15345916cd2Sjpk /* for Trusted Extensions */
15445916cd2Sjpk static tsol_zcent_t *get_zone_label(zlog_t *, priv_set_t *);
15545916cd2Sjpk static int tsol_mounts(zlog_t *, char *, char *);
15645916cd2Sjpk static void tsol_unmounts(zlog_t *, char *);
1579d48116cSdh155122
15845916cd2Sjpk static m_label_t *zlabel = NULL;
15945916cd2Sjpk static m_label_t *zid_label = NULL;
16045916cd2Sjpk static priv_set_t *zprivs = NULL;
16145916cd2Sjpk
1628cd81a20SJerry Jelinek static const char *DFLT_FS_ALLOWED = "hsfs,smbfs,nfs,nfs3,nfs4,nfsdyn";
1638cd81a20SJerry Jelinek
1647c478bd9Sstevel@tonic-gate /* from libsocket, not in any header file */
1657c478bd9Sstevel@tonic-gate extern int getnetmaskbyaddr(struct in_addr, struct in_addr *);
1667c478bd9Sstevel@tonic-gate
167c5cd6260S /* from zoneadmd */
168c5cd6260S extern char query_hook[];
169c5cd6260S
1707c478bd9Sstevel@tonic-gate /*
171550b6e40SSowmini Varadhan * For each "net" resource configured in zonecfg, we track a zone_addr_list_t
172550b6e40SSowmini Varadhan * node in a linked list that is sorted by linkid. The list is constructed as
173550b6e40SSowmini Varadhan * the xml configuration file is parsed, and the information
174550b6e40SSowmini Varadhan * contained in each node is added to the kernel before the zone is
175550b6e40SSowmini Varadhan * booted, to be retrieved and applied from within the exclusive-IP NGZ
176550b6e40SSowmini Varadhan * on boot.
177550b6e40SSowmini Varadhan */
178550b6e40SSowmini Varadhan typedef struct zone_addr_list {
179550b6e40SSowmini Varadhan struct zone_addr_list *za_next;
180550b6e40SSowmini Varadhan datalink_id_t za_linkid; /* datalink_id_t of interface */
181550b6e40SSowmini Varadhan struct zone_nwiftab za_nwiftab; /* address, defrouter properties */
182550b6e40SSowmini Varadhan } zone_addr_list_t;
183550b6e40SSowmini Varadhan
184550b6e40SSowmini Varadhan /*
185108322fbScarlsonj * An optimization for build_mnttable: reallocate (and potentially copy the
186108322fbScarlsonj * data) only once every N times through the loop.
187108322fbScarlsonj */
188108322fbScarlsonj #define MNTTAB_HUNK 32
189108322fbScarlsonj
190550b6e40SSowmini Varadhan /* some handy macros */
191550b6e40SSowmini Varadhan #define SIN(s) ((struct sockaddr_in *)s)
192550b6e40SSowmini Varadhan #define SIN6(s) ((struct sockaddr_in6 *)s)
193550b6e40SSowmini Varadhan
194108322fbScarlsonj /*
1957c478bd9Sstevel@tonic-gate * Private autofs system call
1967c478bd9Sstevel@tonic-gate */
1977c478bd9Sstevel@tonic-gate extern int _autofssys(int, void *);
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate static int
autofs_cleanup(zoneid_t zoneid)2007c478bd9Sstevel@tonic-gate autofs_cleanup(zoneid_t zoneid)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate * Ask autofs to unmount all trigger nodes in the given zone.
2047c478bd9Sstevel@tonic-gate */
2057c478bd9Sstevel@tonic-gate return (_autofssys(AUTOFS_UNMOUNTALL, (void *)zoneid));
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate
208108322fbScarlsonj static void
free_mnttable(struct mnttab * mnt_array,uint_t nelem)209108322fbScarlsonj free_mnttable(struct mnttab *mnt_array, uint_t nelem)
210108322fbScarlsonj {
211108322fbScarlsonj uint_t i;
212108322fbScarlsonj
213108322fbScarlsonj if (mnt_array == NULL)
214108322fbScarlsonj return;
215108322fbScarlsonj for (i = 0; i < nelem; i++) {
216108322fbScarlsonj free(mnt_array[i].mnt_mountp);
217108322fbScarlsonj free(mnt_array[i].mnt_fstype);
218108322fbScarlsonj free(mnt_array[i].mnt_special);
219108322fbScarlsonj free(mnt_array[i].mnt_mntopts);
220108322fbScarlsonj assert(mnt_array[i].mnt_time == NULL);
221108322fbScarlsonj }
222108322fbScarlsonj free(mnt_array);
223108322fbScarlsonj }
224108322fbScarlsonj
225108322fbScarlsonj /*
226108322fbScarlsonj * Build the mount table for the zone rooted at "zroot", storing the resulting
227108322fbScarlsonj * array of struct mnttabs in "mnt_arrayp" and the number of elements in the
228108322fbScarlsonj * array in "nelemp".
229108322fbScarlsonj */
230108322fbScarlsonj static int
build_mnttable(zlog_t * zlogp,const char * zroot,size_t zrootlen,FILE * mnttab,struct mnttab ** mnt_arrayp,uint_t * nelemp)231108322fbScarlsonj build_mnttable(zlog_t *zlogp, const char *zroot, size_t zrootlen, FILE *mnttab,
232108322fbScarlsonj struct mnttab **mnt_arrayp, uint_t *nelemp)
233108322fbScarlsonj {
234108322fbScarlsonj struct mnttab mnt;
235108322fbScarlsonj struct mnttab *mnts;
236108322fbScarlsonj struct mnttab *mnp;
237108322fbScarlsonj uint_t nmnt;
238108322fbScarlsonj
239108322fbScarlsonj rewind(mnttab);
240108322fbScarlsonj resetmnttab(mnttab);
241108322fbScarlsonj nmnt = 0;
242108322fbScarlsonj mnts = NULL;
243108322fbScarlsonj while (getmntent(mnttab, &mnt) == 0) {
244108322fbScarlsonj struct mnttab *tmp_array;
245108322fbScarlsonj
246108322fbScarlsonj if (strncmp(mnt.mnt_mountp, zroot, zrootlen) != 0)
247108322fbScarlsonj continue;
248108322fbScarlsonj if (nmnt % MNTTAB_HUNK == 0) {
249108322fbScarlsonj tmp_array = realloc(mnts,
250108322fbScarlsonj (nmnt + MNTTAB_HUNK) * sizeof (*mnts));
251108322fbScarlsonj if (tmp_array == NULL) {
252108322fbScarlsonj free_mnttable(mnts, nmnt);
253108322fbScarlsonj return (-1);
254108322fbScarlsonj }
255108322fbScarlsonj mnts = tmp_array;
256108322fbScarlsonj }
257108322fbScarlsonj mnp = &mnts[nmnt++];
258108322fbScarlsonj
259108322fbScarlsonj /*
260108322fbScarlsonj * Zero out any fields we're not using.
261108322fbScarlsonj */
262108322fbScarlsonj (void) memset(mnp, 0, sizeof (*mnp));
263108322fbScarlsonj
264108322fbScarlsonj if (mnt.mnt_special != NULL)
265108322fbScarlsonj mnp->mnt_special = strdup(mnt.mnt_special);
266108322fbScarlsonj if (mnt.mnt_mntopts != NULL)
267108322fbScarlsonj mnp->mnt_mntopts = strdup(mnt.mnt_mntopts);
268108322fbScarlsonj mnp->mnt_mountp = strdup(mnt.mnt_mountp);
269108322fbScarlsonj mnp->mnt_fstype = strdup(mnt.mnt_fstype);
270108322fbScarlsonj if ((mnt.mnt_special != NULL && mnp->mnt_special == NULL) ||
271108322fbScarlsonj (mnt.mnt_mntopts != NULL && mnp->mnt_mntopts == NULL) ||
272108322fbScarlsonj mnp->mnt_mountp == NULL || mnp->mnt_fstype == NULL) {
273108322fbScarlsonj zerror(zlogp, B_TRUE, "memory allocation failed");
274108322fbScarlsonj free_mnttable(mnts, nmnt);
275108322fbScarlsonj return (-1);
276108322fbScarlsonj }
277108322fbScarlsonj }
278108322fbScarlsonj *mnt_arrayp = mnts;
279108322fbScarlsonj *nelemp = nmnt;
280108322fbScarlsonj return (0);
281108322fbScarlsonj }
282108322fbScarlsonj
283108322fbScarlsonj /*
284108322fbScarlsonj * This is an optimization. The resolve_lofs function is used quite frequently
285108322fbScarlsonj * to manipulate file paths, and on a machine with a large number of zones,
286108322fbScarlsonj * there will be a huge number of mounted file systems. Thus, we trigger a
287108322fbScarlsonj * reread of the list of mount points
288108322fbScarlsonj */
289108322fbScarlsonj static void
lofs_discard_mnttab(void)290108322fbScarlsonj lofs_discard_mnttab(void)
291108322fbScarlsonj {
292108322fbScarlsonj free_mnttable(resolve_lofs_mnts,
293108322fbScarlsonj resolve_lofs_mnt_max - resolve_lofs_mnts);
294108322fbScarlsonj resolve_lofs_mnts = resolve_lofs_mnt_max = NULL;
295108322fbScarlsonj }
296108322fbScarlsonj
297108322fbScarlsonj static int
lofs_read_mnttab(zlog_t * zlogp)298108322fbScarlsonj lofs_read_mnttab(zlog_t *zlogp)
299108322fbScarlsonj {
300108322fbScarlsonj FILE *mnttab;
301108322fbScarlsonj uint_t nmnts;
302108322fbScarlsonj
303108322fbScarlsonj if ((mnttab = fopen(MNTTAB, "r")) == NULL)
304108322fbScarlsonj return (-1);
305108322fbScarlsonj if (build_mnttable(zlogp, "", 0, mnttab, &resolve_lofs_mnts,
306108322fbScarlsonj &nmnts) == -1) {
307108322fbScarlsonj (void) fclose(mnttab);
308108322fbScarlsonj return (-1);
309108322fbScarlsonj }
310108322fbScarlsonj (void) fclose(mnttab);
311108322fbScarlsonj resolve_lofs_mnt_max = resolve_lofs_mnts + nmnts;
312108322fbScarlsonj return (0);
313108322fbScarlsonj }
314108322fbScarlsonj
315108322fbScarlsonj /*
316108322fbScarlsonj * This function loops over potential loopback mounts and symlinks in a given
317108322fbScarlsonj * path and resolves them all down to an absolute path.
318108322fbScarlsonj */
319910f48daSedp void
resolve_lofs(zlog_t * zlogp,char * path,size_t pathlen)320108322fbScarlsonj resolve_lofs(zlog_t *zlogp, char *path, size_t pathlen)
321108322fbScarlsonj {
322108322fbScarlsonj int len, arlen;
323108322fbScarlsonj const char *altroot;
324108322fbScarlsonj char tmppath[MAXPATHLEN];
325108322fbScarlsonj boolean_t outside_altroot;
326108322fbScarlsonj
327108322fbScarlsonj if ((len = resolvepath(path, tmppath, sizeof (tmppath))) == -1)
328108322fbScarlsonj return;
329108322fbScarlsonj tmppath[len] = '\0';
330108322fbScarlsonj (void) strlcpy(path, tmppath, sizeof (tmppath));
331108322fbScarlsonj
332108322fbScarlsonj /* This happens once per zoneadmd operation. */
333108322fbScarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
334108322fbScarlsonj return;
335108322fbScarlsonj
336108322fbScarlsonj altroot = zonecfg_get_root();
337108322fbScarlsonj arlen = strlen(altroot);
338108322fbScarlsonj outside_altroot = B_FALSE;
339108322fbScarlsonj for (;;) {
340108322fbScarlsonj struct mnttab *mnp;
341108322fbScarlsonj
3421e9d8f9fSdminer /* Search in reverse order to find longest match */
3431e9d8f9fSdminer for (mnp = resolve_lofs_mnt_max - 1; mnp >= resolve_lofs_mnts;
3441e9d8f9fSdminer mnp--) {
345108322fbScarlsonj if (mnp->mnt_fstype == NULL ||
346108322fbScarlsonj mnp->mnt_mountp == NULL ||
3471e9d8f9fSdminer mnp->mnt_special == NULL)
348108322fbScarlsonj continue;
349108322fbScarlsonj len = strlen(mnp->mnt_mountp);
350108322fbScarlsonj if (strncmp(mnp->mnt_mountp, path, len) == 0 &&
351108322fbScarlsonj (path[len] == '/' || path[len] == '\0'))
352108322fbScarlsonj break;
353108322fbScarlsonj }
3541e9d8f9fSdminer if (mnp < resolve_lofs_mnts)
3551e9d8f9fSdminer break;
3561e9d8f9fSdminer /* If it's not a lofs then we're done */
3571e9d8f9fSdminer if (strcmp(mnp->mnt_fstype, MNTTYPE_LOFS) != 0)
358108322fbScarlsonj break;
359108322fbScarlsonj if (outside_altroot) {
360108322fbScarlsonj char *cp;
361108322fbScarlsonj int olen = sizeof (MNTOPT_RO) - 1;
362108322fbScarlsonj
363108322fbScarlsonj /*
364108322fbScarlsonj * If we run into a read-only mount outside of the
365108322fbScarlsonj * alternate root environment, then the user doesn't
366108322fbScarlsonj * want this path to be made read-write.
367108322fbScarlsonj */
368108322fbScarlsonj if (mnp->mnt_mntopts != NULL &&
369108322fbScarlsonj (cp = strstr(mnp->mnt_mntopts, MNTOPT_RO)) !=
370108322fbScarlsonj NULL &&
371108322fbScarlsonj (cp == mnp->mnt_mntopts || cp[-1] == ',') &&
372108322fbScarlsonj (cp[olen] == '\0' || cp[olen] == ',')) {
373108322fbScarlsonj break;
374108322fbScarlsonj }
375108322fbScarlsonj } else if (arlen > 0 &&
376108322fbScarlsonj (strncmp(mnp->mnt_special, altroot, arlen) != 0 ||
377108322fbScarlsonj (mnp->mnt_special[arlen] != '\0' &&
378108322fbScarlsonj mnp->mnt_special[arlen] != '/'))) {
379108322fbScarlsonj outside_altroot = B_TRUE;
380108322fbScarlsonj }
381108322fbScarlsonj /* use temporary buffer because new path might be longer */
382108322fbScarlsonj (void) snprintf(tmppath, sizeof (tmppath), "%s%s",
383108322fbScarlsonj mnp->mnt_special, path + len);
384108322fbScarlsonj if ((len = resolvepath(tmppath, path, pathlen)) == -1)
385108322fbScarlsonj break;
386108322fbScarlsonj path[len] = '\0';
387108322fbScarlsonj }
388108322fbScarlsonj }
389108322fbScarlsonj
390108322fbScarlsonj /*
391108322fbScarlsonj * For a regular mount, check if a replacement lofs mount is needed because the
392108322fbScarlsonj * referenced device is already mounted somewhere.
393108322fbScarlsonj */
394108322fbScarlsonj static int
check_lofs_needed(zlog_t * zlogp,struct zone_fstab * fsptr)395108322fbScarlsonj check_lofs_needed(zlog_t *zlogp, struct zone_fstab *fsptr)
396108322fbScarlsonj {
397108322fbScarlsonj struct mnttab *mnp;
398108322fbScarlsonj zone_fsopt_t *optptr, *onext;
399108322fbScarlsonj
400108322fbScarlsonj /* This happens once per zoneadmd operation. */
401108322fbScarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
402108322fbScarlsonj return (-1);
403108322fbScarlsonj
404108322fbScarlsonj /*
405108322fbScarlsonj * If this special node isn't already in use, then it's ours alone;
406108322fbScarlsonj * no need to worry about conflicting mounts.
407108322fbScarlsonj */
408108322fbScarlsonj for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max;
409108322fbScarlsonj mnp++) {
410108322fbScarlsonj if (strcmp(mnp->mnt_special, fsptr->zone_fs_special) == 0)
411108322fbScarlsonj break;
412108322fbScarlsonj }
413108322fbScarlsonj if (mnp >= resolve_lofs_mnt_max)
414108322fbScarlsonj return (0);
415108322fbScarlsonj
416108322fbScarlsonj /*
417108322fbScarlsonj * Convert this duplicate mount into a lofs mount.
418108322fbScarlsonj */
419108322fbScarlsonj (void) strlcpy(fsptr->zone_fs_special, mnp->mnt_mountp,
420108322fbScarlsonj sizeof (fsptr->zone_fs_special));
421108322fbScarlsonj (void) strlcpy(fsptr->zone_fs_type, MNTTYPE_LOFS,
422108322fbScarlsonj sizeof (fsptr->zone_fs_type));
423108322fbScarlsonj fsptr->zone_fs_raw[0] = '\0';
424108322fbScarlsonj
425108322fbScarlsonj /*
4266e1ae2a3SGary Pennington * Discard all but one of the original options and set that to our
4276e1ae2a3SGary Pennington * default set of options used for resources.
428108322fbScarlsonj */
429108322fbScarlsonj optptr = fsptr->zone_fs_options;
430108322fbScarlsonj if (optptr == NULL) {
431108322fbScarlsonj optptr = malloc(sizeof (*optptr));
432108322fbScarlsonj if (optptr == NULL) {
433108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot mount %s",
434108322fbScarlsonj fsptr->zone_fs_dir);
435108322fbScarlsonj return (-1);
436108322fbScarlsonj }
437108322fbScarlsonj } else {
438108322fbScarlsonj while ((onext = optptr->zone_fsopt_next) != NULL) {
439108322fbScarlsonj optptr->zone_fsopt_next = onext->zone_fsopt_next;
440108322fbScarlsonj free(onext);
441108322fbScarlsonj }
442108322fbScarlsonj }
4436e1ae2a3SGary Pennington (void) strcpy(optptr->zone_fsopt_opt, RESOURCE_DEFAULT_OPTS);
444108322fbScarlsonj optptr->zone_fsopt_next = NULL;
445108322fbScarlsonj fsptr->zone_fs_options = optptr;
446108322fbScarlsonj return (0);
447108322fbScarlsonj }
448108322fbScarlsonj
449d314f035Sedp int
make_one_dir(zlog_t * zlogp,const char * prefix,const char * subdir,mode_t mode,uid_t userid,gid_t groupid)45027e6fb21Sdp make_one_dir(zlog_t *zlogp, const char *prefix, const char *subdir, mode_t mode,
45127e6fb21Sdp uid_t userid, gid_t groupid)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate char path[MAXPATHLEN];
4547c478bd9Sstevel@tonic-gate struct stat st;
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate if (snprintf(path, sizeof (path), "%s%s", prefix, subdir) >
4577c478bd9Sstevel@tonic-gate sizeof (path)) {
4587c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "pathname %s%s is too long", prefix,
4597c478bd9Sstevel@tonic-gate subdir);
4607c478bd9Sstevel@tonic-gate return (-1);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate if (lstat(path, &st) == 0) {
4647c478bd9Sstevel@tonic-gate /*
4657c478bd9Sstevel@tonic-gate * We don't check the file mode since presumably the zone
4667c478bd9Sstevel@tonic-gate * administrator may have had good reason to change the mode,
4677c478bd9Sstevel@tonic-gate * and we don't need to second guess him.
4687c478bd9Sstevel@tonic-gate */
4697c478bd9Sstevel@tonic-gate if (!S_ISDIR(st.st_mode)) {
470756cf395SRic Aleshire if (S_ISREG(st.st_mode)) {
47145916cd2Sjpk /*
472756cf395SRic Aleshire * Allow readonly mounts of /etc/ files; this
473756cf395SRic Aleshire * is needed most by Trusted Extensions.
47445916cd2Sjpk */
47545916cd2Sjpk if (strncmp(subdir, "/etc/",
47645916cd2Sjpk strlen("/etc/")) != 0) {
47745916cd2Sjpk zerror(zlogp, B_FALSE,
47845916cd2Sjpk "%s is not in /etc", path);
4797c478bd9Sstevel@tonic-gate return (-1);
4807c478bd9Sstevel@tonic-gate }
48145916cd2Sjpk } else {
48245916cd2Sjpk zerror(zlogp, B_FALSE,
48345916cd2Sjpk "%s is not a directory", path);
48445916cd2Sjpk return (-1);
48545916cd2Sjpk }
48645916cd2Sjpk }
48727e6fb21Sdp return (0);
48827e6fb21Sdp }
48927e6fb21Sdp
49027e6fb21Sdp if (mkdirp(path, mode) != 0) {
4917c478bd9Sstevel@tonic-gate if (errno == EROFS)
4927c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "Could not mkdir %s.\nIt is on "
4937c478bd9Sstevel@tonic-gate "a read-only file system in this local zone.\nMake "
4947c478bd9Sstevel@tonic-gate "sure %s exists in the global zone.", path, subdir);
4957c478bd9Sstevel@tonic-gate else
4967c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "mkdirp of %s failed", path);
4977c478bd9Sstevel@tonic-gate return (-1);
4987c478bd9Sstevel@tonic-gate }
49927e6fb21Sdp
50027e6fb21Sdp (void) chown(path, userid, groupid);
5017c478bd9Sstevel@tonic-gate return (0);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate
5047c478bd9Sstevel@tonic-gate static void
free_remote_fstypes(char ** types)5057c478bd9Sstevel@tonic-gate free_remote_fstypes(char **types)
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate uint_t i;
5087c478bd9Sstevel@tonic-gate
5097c478bd9Sstevel@tonic-gate if (types == NULL)
5107c478bd9Sstevel@tonic-gate return;
5117c478bd9Sstevel@tonic-gate for (i = 0; types[i] != NULL; i++)
5127c478bd9Sstevel@tonic-gate free(types[i]);
5137c478bd9Sstevel@tonic-gate free(types);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate
5167c478bd9Sstevel@tonic-gate static char **
get_remote_fstypes(zlog_t * zlogp)5177c478bd9Sstevel@tonic-gate get_remote_fstypes(zlog_t *zlogp)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate char **types = NULL;
5207c478bd9Sstevel@tonic-gate FILE *fp;
5217c478bd9Sstevel@tonic-gate char buf[MAXPATHLEN];
5227c478bd9Sstevel@tonic-gate char fstype[MAXPATHLEN];
5237c478bd9Sstevel@tonic-gate uint_t lines = 0;
5247c478bd9Sstevel@tonic-gate uint_t i;
5257c478bd9Sstevel@tonic-gate
5267c478bd9Sstevel@tonic-gate if ((fp = fopen(DFSTYPES, "r")) == NULL) {
5277c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", DFSTYPES);
5287c478bd9Sstevel@tonic-gate return (NULL);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate /*
5317c478bd9Sstevel@tonic-gate * Count the number of lines
5327c478bd9Sstevel@tonic-gate */
5337c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL)
5347c478bd9Sstevel@tonic-gate lines++;
5357c478bd9Sstevel@tonic-gate if (lines == 0) /* didn't read anything; empty file */
5367c478bd9Sstevel@tonic-gate goto out;
5377c478bd9Sstevel@tonic-gate rewind(fp);
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate * Allocate enough space for a NULL-terminated array.
5407c478bd9Sstevel@tonic-gate */
5417c478bd9Sstevel@tonic-gate types = calloc(lines + 1, sizeof (char *));
5427c478bd9Sstevel@tonic-gate if (types == NULL) {
5437c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed");
5447c478bd9Sstevel@tonic-gate goto out;
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate i = 0;
5477c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL) {
5487c478bd9Sstevel@tonic-gate /* LINTED - fstype is big enough to hold buf */
5497c478bd9Sstevel@tonic-gate if (sscanf(buf, "%s", fstype) == 0) {
5507c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to parse %s", DFSTYPES);
5517c478bd9Sstevel@tonic-gate free_remote_fstypes(types);
5527c478bd9Sstevel@tonic-gate types = NULL;
5537c478bd9Sstevel@tonic-gate goto out;
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate types[i] = strdup(fstype);
5567c478bd9Sstevel@tonic-gate if (types[i] == NULL) {
5577c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed");
5587c478bd9Sstevel@tonic-gate free_remote_fstypes(types);
5597c478bd9Sstevel@tonic-gate types = NULL;
5607c478bd9Sstevel@tonic-gate goto out;
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate i++;
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate out:
5657c478bd9Sstevel@tonic-gate (void) fclose(fp);
5667c478bd9Sstevel@tonic-gate return (types);
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate
5697c478bd9Sstevel@tonic-gate static boolean_t
is_remote_fstype(const char * fstype,char * const * remote_fstypes)5707c478bd9Sstevel@tonic-gate is_remote_fstype(const char *fstype, char *const *remote_fstypes)
5717c478bd9Sstevel@tonic-gate {
5727c478bd9Sstevel@tonic-gate uint_t i;
5737c478bd9Sstevel@tonic-gate
5747c478bd9Sstevel@tonic-gate if (remote_fstypes == NULL)
5757c478bd9Sstevel@tonic-gate return (B_FALSE);
5767c478bd9Sstevel@tonic-gate for (i = 0; remote_fstypes[i] != NULL; i++) {
5777c478bd9Sstevel@tonic-gate if (strcmp(remote_fstypes[i], fstype) == 0)
5787c478bd9Sstevel@tonic-gate return (B_TRUE);
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate return (B_FALSE);
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate
583108322fbScarlsonj /*
584108322fbScarlsonj * This converts a zone root path (normally of the form .../root) to a Live
585108322fbScarlsonj * Upgrade scratch zone root (of the form .../lu).
586108322fbScarlsonj */
5877c478bd9Sstevel@tonic-gate static void
root_to_lu(zlog_t * zlogp,char * zroot,size_t zrootlen,boolean_t isresolved)588108322fbScarlsonj root_to_lu(zlog_t *zlogp, char *zroot, size_t zrootlen, boolean_t isresolved)
5897c478bd9Sstevel@tonic-gate {
590108322fbScarlsonj if (!isresolved && zonecfg_in_alt_root())
591108322fbScarlsonj resolve_lofs(zlogp, zroot, zrootlen);
592108322fbScarlsonj (void) strcpy(strrchr(zroot, '/') + 1, "lu");
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate
5957c478bd9Sstevel@tonic-gate /*
5967c478bd9Sstevel@tonic-gate * The general strategy for unmounting filesystems is as follows:
5977c478bd9Sstevel@tonic-gate *
5987c478bd9Sstevel@tonic-gate * - Remote filesystems may be dead, and attempting to contact them as
5997c478bd9Sstevel@tonic-gate * part of a regular unmount may hang forever; we want to always try to
6007c478bd9Sstevel@tonic-gate * forcibly unmount such filesystems and only fall back to regular
6017c478bd9Sstevel@tonic-gate * unmounts if the filesystem doesn't support forced unmounts.
6027c478bd9Sstevel@tonic-gate *
6037c478bd9Sstevel@tonic-gate * - We don't want to unnecessarily corrupt metadata on local
6047c478bd9Sstevel@tonic-gate * filesystems (ie UFS), so we want to start off with graceful unmounts,
6057c478bd9Sstevel@tonic-gate * and only escalate to doing forced unmounts if we get stuck.
6067c478bd9Sstevel@tonic-gate *
6077c478bd9Sstevel@tonic-gate * We start off walking backwards through the mount table. This doesn't
6087c478bd9Sstevel@tonic-gate * give us strict ordering but ensures that we try to unmount submounts
6097c478bd9Sstevel@tonic-gate * first. We thus limit the number of failed umount2(2) calls.
6107c478bd9Sstevel@tonic-gate *
6117c478bd9Sstevel@tonic-gate * The mechanism for determining if we're stuck is to count the number
6127c478bd9Sstevel@tonic-gate * of failed unmounts each iteration through the mount table. This
6137c478bd9Sstevel@tonic-gate * gives us an upper bound on the number of filesystems which remain
6147c478bd9Sstevel@tonic-gate * mounted (autofs trigger nodes are dealt with separately). If at the
6157c478bd9Sstevel@tonic-gate * end of one unmount+autofs_cleanup cycle we still have the same number
6167c478bd9Sstevel@tonic-gate * of mounts that we started out with, we're stuck and try a forced
6177c478bd9Sstevel@tonic-gate * unmount. If that fails (filesystem doesn't support forced unmounts)
6187c478bd9Sstevel@tonic-gate * then we bail and are unable to teardown the zone. If it succeeds,
6197c478bd9Sstevel@tonic-gate * we're no longer stuck so we continue with our policy of trying
6207c478bd9Sstevel@tonic-gate * graceful mounts first.
6217c478bd9Sstevel@tonic-gate *
6227c478bd9Sstevel@tonic-gate * Zone must be down (ie, no processes or threads active).
6237c478bd9Sstevel@tonic-gate */
6247c478bd9Sstevel@tonic-gate static int
unmount_filesystems(zlog_t * zlogp,zoneid_t zoneid,boolean_t unmount_cmd)625108322fbScarlsonj unmount_filesystems(zlog_t *zlogp, zoneid_t zoneid, boolean_t unmount_cmd)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate int error = 0;
6287c478bd9Sstevel@tonic-gate FILE *mnttab;
6297c478bd9Sstevel@tonic-gate struct mnttab *mnts;
6307c478bd9Sstevel@tonic-gate uint_t nmnt;
6317c478bd9Sstevel@tonic-gate char zroot[MAXPATHLEN + 1];
6327c478bd9Sstevel@tonic-gate size_t zrootlen;
6337c478bd9Sstevel@tonic-gate uint_t oldcount = UINT_MAX;
6347c478bd9Sstevel@tonic-gate boolean_t stuck = B_FALSE;
6357c478bd9Sstevel@tonic-gate char **remote_fstypes = NULL;
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
6387c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "unable to determine zone root");
6397c478bd9Sstevel@tonic-gate return (-1);
6407c478bd9Sstevel@tonic-gate }
641108322fbScarlsonj if (unmount_cmd)
642108322fbScarlsonj root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate (void) strcat(zroot, "/");
6457c478bd9Sstevel@tonic-gate zrootlen = strlen(zroot);
6467c478bd9Sstevel@tonic-gate
64745916cd2Sjpk /*
64845916cd2Sjpk * For Trusted Extensions unmount each higher level zone's mount
64945916cd2Sjpk * of our zone's /export/home
65045916cd2Sjpk */
65148451833Scarlsonj if (!unmount_cmd)
65245916cd2Sjpk tsol_unmounts(zlogp, zone_name);
65345916cd2Sjpk
6547c478bd9Sstevel@tonic-gate if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
6557c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "failed to open %s", MNTTAB);
6567c478bd9Sstevel@tonic-gate return (-1);
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate /*
6597c478bd9Sstevel@tonic-gate * Use our hacky mntfs ioctl so we see everything, even mounts with
6607c478bd9Sstevel@tonic-gate * MS_NOMNTTAB.
6617c478bd9Sstevel@tonic-gate */
6627c478bd9Sstevel@tonic-gate if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0) {
6637c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to configure %s", MNTTAB);
6647c478bd9Sstevel@tonic-gate error++;
6657c478bd9Sstevel@tonic-gate goto out;
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate
6687c478bd9Sstevel@tonic-gate /*
6697c478bd9Sstevel@tonic-gate * Build the list of remote fstypes so we know which ones we
6707c478bd9Sstevel@tonic-gate * should forcibly unmount.
6717c478bd9Sstevel@tonic-gate */
6727c478bd9Sstevel@tonic-gate remote_fstypes = get_remote_fstypes(zlogp);
6737c478bd9Sstevel@tonic-gate for (; /* ever */; ) {
6747c478bd9Sstevel@tonic-gate uint_t newcount = 0;
6757c478bd9Sstevel@tonic-gate boolean_t unmounted;
6767c478bd9Sstevel@tonic-gate struct mnttab *mnp;
6777c478bd9Sstevel@tonic-gate char *path;
6787c478bd9Sstevel@tonic-gate uint_t i;
6797c478bd9Sstevel@tonic-gate
6807c478bd9Sstevel@tonic-gate mnts = NULL;
6817c478bd9Sstevel@tonic-gate nmnt = 0;
6827c478bd9Sstevel@tonic-gate /*
6837c478bd9Sstevel@tonic-gate * MNTTAB gives us a way to walk through mounted
6847c478bd9Sstevel@tonic-gate * filesystems; we need to be able to walk them in
6857c478bd9Sstevel@tonic-gate * reverse order, so we build a list of all mounted
6867c478bd9Sstevel@tonic-gate * filesystems.
6877c478bd9Sstevel@tonic-gate */
6887c478bd9Sstevel@tonic-gate if (build_mnttable(zlogp, zroot, zrootlen, mnttab, &mnts,
6897c478bd9Sstevel@tonic-gate &nmnt) != 0) {
6907c478bd9Sstevel@tonic-gate error++;
6917c478bd9Sstevel@tonic-gate goto out;
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate for (i = 0; i < nmnt; i++) {
6947c478bd9Sstevel@tonic-gate mnp = &mnts[nmnt - i - 1]; /* access in reverse order */
6957c478bd9Sstevel@tonic-gate path = mnp->mnt_mountp;
6967c478bd9Sstevel@tonic-gate unmounted = B_FALSE;
6977c478bd9Sstevel@tonic-gate /*
6987c478bd9Sstevel@tonic-gate * Try forced unmount first for remote filesystems.
6997c478bd9Sstevel@tonic-gate *
7007c478bd9Sstevel@tonic-gate * Not all remote filesystems support forced unmounts,
7017c478bd9Sstevel@tonic-gate * so if this fails (ENOTSUP) we'll continue on
7027c478bd9Sstevel@tonic-gate * and try a regular unmount.
7037c478bd9Sstevel@tonic-gate */
7047c478bd9Sstevel@tonic-gate if (is_remote_fstype(mnp->mnt_fstype, remote_fstypes)) {
7057c478bd9Sstevel@tonic-gate if (umount2(path, MS_FORCE) == 0)
7067c478bd9Sstevel@tonic-gate unmounted = B_TRUE;
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate /*
7097c478bd9Sstevel@tonic-gate * Try forced unmount if we're stuck.
7107c478bd9Sstevel@tonic-gate */
7117c478bd9Sstevel@tonic-gate if (stuck) {
7127c478bd9Sstevel@tonic-gate if (umount2(path, MS_FORCE) == 0) {
7137c478bd9Sstevel@tonic-gate unmounted = B_TRUE;
7147c478bd9Sstevel@tonic-gate stuck = B_FALSE;
7157c478bd9Sstevel@tonic-gate } else {
7167c478bd9Sstevel@tonic-gate /*
7177c478bd9Sstevel@tonic-gate * The first failure indicates a
7187c478bd9Sstevel@tonic-gate * mount we won't be able to get
7197c478bd9Sstevel@tonic-gate * rid of automatically, so we
7207c478bd9Sstevel@tonic-gate * bail.
7217c478bd9Sstevel@tonic-gate */
7227c478bd9Sstevel@tonic-gate error++;
7237c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE,
7247c478bd9Sstevel@tonic-gate "unable to unmount '%s'", path);
7257c478bd9Sstevel@tonic-gate free_mnttable(mnts, nmnt);
7267c478bd9Sstevel@tonic-gate goto out;
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate /*
7307c478bd9Sstevel@tonic-gate * Try regular unmounts for everything else.
7317c478bd9Sstevel@tonic-gate */
7327c478bd9Sstevel@tonic-gate if (!unmounted && umount2(path, 0) != 0)
7337c478bd9Sstevel@tonic-gate newcount++;
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate free_mnttable(mnts, nmnt);
7367c478bd9Sstevel@tonic-gate
7377c478bd9Sstevel@tonic-gate if (newcount == 0)
7387c478bd9Sstevel@tonic-gate break;
7397c478bd9Sstevel@tonic-gate if (newcount >= oldcount) {
7407c478bd9Sstevel@tonic-gate /*
7417c478bd9Sstevel@tonic-gate * Last round didn't unmount anything; we're stuck and
7427c478bd9Sstevel@tonic-gate * should start trying forced unmounts.
7437c478bd9Sstevel@tonic-gate */
7447c478bd9Sstevel@tonic-gate stuck = B_TRUE;
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate oldcount = newcount;
7477c478bd9Sstevel@tonic-gate
7487c478bd9Sstevel@tonic-gate /*
7497c478bd9Sstevel@tonic-gate * Autofs doesn't let you unmount its trigger nodes from
7507c478bd9Sstevel@tonic-gate * userland so we have to tell the kernel to cleanup for us.
7517c478bd9Sstevel@tonic-gate */
7527c478bd9Sstevel@tonic-gate if (autofs_cleanup(zoneid) != 0) {
7537c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to remove autofs nodes");
7547c478bd9Sstevel@tonic-gate error++;
7557c478bd9Sstevel@tonic-gate goto out;
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate }
7587c478bd9Sstevel@tonic-gate
7597c478bd9Sstevel@tonic-gate out:
7607c478bd9Sstevel@tonic-gate free_remote_fstypes(remote_fstypes);
7617c478bd9Sstevel@tonic-gate (void) fclose(mnttab);
7627c478bd9Sstevel@tonic-gate return (error ? -1 : 0);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate
7657c478bd9Sstevel@tonic-gate static int
fs_compare(const void * m1,const void * m2)7667c478bd9Sstevel@tonic-gate fs_compare(const void *m1, const void *m2)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate struct zone_fstab *i = (struct zone_fstab *)m1;
7697c478bd9Sstevel@tonic-gate struct zone_fstab *j = (struct zone_fstab *)m2;
7707c478bd9Sstevel@tonic-gate
7717c478bd9Sstevel@tonic-gate return (strcmp(i->zone_fs_dir, j->zone_fs_dir));
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate
7747c478bd9Sstevel@tonic-gate /*
7757c478bd9Sstevel@tonic-gate * Fork and exec (and wait for) the mentioned binary with the provided
7767c478bd9Sstevel@tonic-gate * arguments. Returns (-1) if something went wrong with fork(2) or exec(2),
7777c478bd9Sstevel@tonic-gate * returns the exit status otherwise.
7787c478bd9Sstevel@tonic-gate *
7797c478bd9Sstevel@tonic-gate * If we were unable to exec the provided pathname (for whatever
7807c478bd9Sstevel@tonic-gate * reason), we return the special token ZEXIT_EXEC. The current value
7817c478bd9Sstevel@tonic-gate * of ZEXIT_EXEC doesn't conflict with legitimate exit codes of the
7827c478bd9Sstevel@tonic-gate * consumers of this function; any future consumers must make sure this
7837c478bd9Sstevel@tonic-gate * remains the case.
7847c478bd9Sstevel@tonic-gate */
7857c478bd9Sstevel@tonic-gate static int
forkexec(zlog_t * zlogp,const char * path,char * const argv[])7867c478bd9Sstevel@tonic-gate forkexec(zlog_t *zlogp, const char *path, char *const argv[])
7877c478bd9Sstevel@tonic-gate {
7887c478bd9Sstevel@tonic-gate pid_t child_pid;
7897c478bd9Sstevel@tonic-gate int child_status = 0;
7907c478bd9Sstevel@tonic-gate
7917c478bd9Sstevel@tonic-gate /*
7927c478bd9Sstevel@tonic-gate * Do not let another thread localize a message while we are forking.
7937c478bd9Sstevel@tonic-gate */
7947c478bd9Sstevel@tonic-gate (void) mutex_lock(&msglock);
7957c478bd9Sstevel@tonic-gate child_pid = fork();
7967c478bd9Sstevel@tonic-gate (void) mutex_unlock(&msglock);
7977c478bd9Sstevel@tonic-gate if (child_pid == -1) {
7987c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not fork for %s", argv[0]);
7997c478bd9Sstevel@tonic-gate return (-1);
8007c478bd9Sstevel@tonic-gate } else if (child_pid == 0) {
8017c478bd9Sstevel@tonic-gate closefrom(0);
8021390a385Sgjelinek /* redirect stdin, stdout & stderr to /dev/null */
8031390a385Sgjelinek (void) open("/dev/null", O_RDONLY); /* stdin */
8041390a385Sgjelinek (void) open("/dev/null", O_WRONLY); /* stdout */
8051390a385Sgjelinek (void) open("/dev/null", O_WRONLY); /* stderr */
8067c478bd9Sstevel@tonic-gate (void) execv(path, argv);
8077c478bd9Sstevel@tonic-gate /*
8087c478bd9Sstevel@tonic-gate * Since we are in the child, there is no point calling zerror()
8097c478bd9Sstevel@tonic-gate * since there is nobody waiting to consume it. So exit with a
8107c478bd9Sstevel@tonic-gate * special code that the parent will recognize and call zerror()
8117c478bd9Sstevel@tonic-gate * accordingly.
8127c478bd9Sstevel@tonic-gate */
8137c478bd9Sstevel@tonic-gate
8147c478bd9Sstevel@tonic-gate _exit(ZEXIT_EXEC);
8157c478bd9Sstevel@tonic-gate } else {
8167c478bd9Sstevel@tonic-gate (void) waitpid(child_pid, &child_status, 0);
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate
8197c478bd9Sstevel@tonic-gate if (WIFSIGNALED(child_status)) {
8207c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
8217c478bd9Sstevel@tonic-gate "signal %d", path, WTERMSIG(child_status));
8227c478bd9Sstevel@tonic-gate return (-1);
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate assert(WIFEXITED(child_status));
8257c478bd9Sstevel@tonic-gate if (WEXITSTATUS(child_status) == ZEXIT_EXEC) {
8267c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "failed to exec %s", path);
8277c478bd9Sstevel@tonic-gate return (-1);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate return (WEXITSTATUS(child_status));
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate
8327c478bd9Sstevel@tonic-gate static int
isregfile(const char * path)83393239addSjohnlev isregfile(const char *path)
83493239addSjohnlev {
83593239addSjohnlev struct stat64 st;
83693239addSjohnlev
83793239addSjohnlev if (stat64(path, &st) == -1)
83893239addSjohnlev return (-1);
83993239addSjohnlev
84093239addSjohnlev return (S_ISREG(st.st_mode));
84193239addSjohnlev }
84293239addSjohnlev
84393239addSjohnlev static int
dofsck(zlog_t * zlogp,const char * fstype,const char * rawdev)8447c478bd9Sstevel@tonic-gate dofsck(zlog_t *zlogp, const char *fstype, const char *rawdev)
8457c478bd9Sstevel@tonic-gate {
8467c478bd9Sstevel@tonic-gate char cmdbuf[MAXPATHLEN];
84757a250fbSbatschul char *argv[5];
8487c478bd9Sstevel@tonic-gate int status;
8497c478bd9Sstevel@tonic-gate
8507c478bd9Sstevel@tonic-gate /*
8517c478bd9Sstevel@tonic-gate * We could alternatively have called /usr/sbin/fsck -F <fstype>, but
8527c478bd9Sstevel@tonic-gate * that would cost us an extra fork/exec without buying us anything.
8537c478bd9Sstevel@tonic-gate */
8547c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck", fstype)
8559acbbeafSnn35248 >= sizeof (cmdbuf)) {
8567c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8577c478bd9Sstevel@tonic-gate return (-1);
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate
86093239addSjohnlev /*
86193239addSjohnlev * If it doesn't exist, that's OK: we verified this previously
86293239addSjohnlev * in zoneadm.
86393239addSjohnlev */
86493239addSjohnlev if (isregfile(cmdbuf) == -1)
86593239addSjohnlev return (0);
86693239addSjohnlev
8677c478bd9Sstevel@tonic-gate argv[0] = "fsck";
86857a250fbSbatschul argv[1] = "-o";
86957a250fbSbatschul argv[2] = "p";
87057a250fbSbatschul argv[3] = (char *)rawdev;
87157a250fbSbatschul argv[4] = NULL;
8727c478bd9Sstevel@tonic-gate
8737c478bd9Sstevel@tonic-gate status = forkexec(zlogp, cmdbuf, argv);
8747c478bd9Sstevel@tonic-gate if (status == 0 || status == -1)
8757c478bd9Sstevel@tonic-gate return (status);
8767c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "fsck of '%s' failed with exit status %d; "
8777c478bd9Sstevel@tonic-gate "run fsck manually", rawdev, status);
8787c478bd9Sstevel@tonic-gate return (-1);
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate
8817c478bd9Sstevel@tonic-gate static int
domount(zlog_t * zlogp,const char * fstype,const char * opts,const char * special,const char * directory)8827c478bd9Sstevel@tonic-gate domount(zlog_t *zlogp, const char *fstype, const char *opts,
8837c478bd9Sstevel@tonic-gate const char *special, const char *directory)
8847c478bd9Sstevel@tonic-gate {
8857c478bd9Sstevel@tonic-gate char cmdbuf[MAXPATHLEN];
8867c478bd9Sstevel@tonic-gate char *argv[6];
8877c478bd9Sstevel@tonic-gate int status;
8887c478bd9Sstevel@tonic-gate
8897c478bd9Sstevel@tonic-gate /*
8907c478bd9Sstevel@tonic-gate * We could alternatively have called /usr/sbin/mount -F <fstype>, but
8917c478bd9Sstevel@tonic-gate * that would cost us an extra fork/exec without buying us anything.
8927c478bd9Sstevel@tonic-gate */
8937c478bd9Sstevel@tonic-gate if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount", fstype)
8949acbbeafSnn35248 >= sizeof (cmdbuf)) {
8957c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "file-system type %s too long", fstype);
8967c478bd9Sstevel@tonic-gate return (-1);
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate argv[0] = "mount";
8997c478bd9Sstevel@tonic-gate if (opts[0] == '\0') {
9007c478bd9Sstevel@tonic-gate argv[1] = (char *)special;
9017c478bd9Sstevel@tonic-gate argv[2] = (char *)directory;
9027c478bd9Sstevel@tonic-gate argv[3] = NULL;
9037c478bd9Sstevel@tonic-gate } else {
9047c478bd9Sstevel@tonic-gate argv[1] = "-o";
9057c478bd9Sstevel@tonic-gate argv[2] = (char *)opts;
9067c478bd9Sstevel@tonic-gate argv[3] = (char *)special;
9077c478bd9Sstevel@tonic-gate argv[4] = (char *)directory;
9087c478bd9Sstevel@tonic-gate argv[5] = NULL;
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate
9117c478bd9Sstevel@tonic-gate status = forkexec(zlogp, cmdbuf, argv);
9127c478bd9Sstevel@tonic-gate if (status == 0 || status == -1)
9137c478bd9Sstevel@tonic-gate return (status);
9147c478bd9Sstevel@tonic-gate if (opts[0] == '\0')
9157c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "\"%s %s %s\" "
9167c478bd9Sstevel@tonic-gate "failed with exit code %d",
9177c478bd9Sstevel@tonic-gate cmdbuf, special, directory, status);
9187c478bd9Sstevel@tonic-gate else
9197c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "\"%s -o %s %s %s\" "
9207c478bd9Sstevel@tonic-gate "failed with exit code %d",
9217c478bd9Sstevel@tonic-gate cmdbuf, opts, special, directory, status);
9227c478bd9Sstevel@tonic-gate return (-1);
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate
9257c478bd9Sstevel@tonic-gate /*
926d314f035Sedp * Check if a given mount point path exists.
927d314f035Sedp * If it does, make sure it doesn't contain any symlinks.
928d314f035Sedp * Note that if "leaf" is false we're checking an intermediate
929d314f035Sedp * component of the mount point path, so it must be a directory.
930d314f035Sedp * If "leaf" is true, then we're checking the entire mount point
931d314f035Sedp * path, so the mount point itself can be anything aside from a
932d314f035Sedp * symbolic link.
933d314f035Sedp *
934d314f035Sedp * If the path is invalid then a negative value is returned. If the
935d314f035Sedp * path exists and is a valid mount point path then 0 is returned.
936d314f035Sedp * If the path doesn't exist return a positive value.
9377c478bd9Sstevel@tonic-gate */
9387c478bd9Sstevel@tonic-gate static int
valid_mount_point(zlog_t * zlogp,const char * path,const boolean_t leaf)939d314f035Sedp valid_mount_point(zlog_t *zlogp, const char *path, const boolean_t leaf)
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate struct stat statbuf;
9427c478bd9Sstevel@tonic-gate char respath[MAXPATHLEN];
9437c478bd9Sstevel@tonic-gate int res;
9447c478bd9Sstevel@tonic-gate
9457c478bd9Sstevel@tonic-gate if (lstat(path, &statbuf) != 0) {
9467c478bd9Sstevel@tonic-gate if (errno == ENOENT)
947d314f035Sedp return (1);
9487c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "can't stat %s", path);
9497c478bd9Sstevel@tonic-gate return (-1);
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate if (S_ISLNK(statbuf.st_mode)) {
9527c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is a symlink", path);
9537c478bd9Sstevel@tonic-gate return (-1);
9547c478bd9Sstevel@tonic-gate }
955d314f035Sedp if (!leaf && !S_ISDIR(statbuf.st_mode)) {
9567c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not a directory", path);
9577c478bd9Sstevel@tonic-gate return (-1);
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate if ((res = resolvepath(path, respath, sizeof (respath))) == -1) {
9607c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to resolve path %s", path);
9617c478bd9Sstevel@tonic-gate return (-1);
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate respath[res] = '\0';
9647c478bd9Sstevel@tonic-gate if (strcmp(path, respath) != 0) {
9657c478bd9Sstevel@tonic-gate /*
966d314f035Sedp * We don't like ".."s, "."s, or "//"s throwing us off
9677c478bd9Sstevel@tonic-gate */
9687c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s is not a canonical path", path);
9697c478bd9Sstevel@tonic-gate return (-1);
9707c478bd9Sstevel@tonic-gate }
9717c478bd9Sstevel@tonic-gate return (0);
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate
9747c478bd9Sstevel@tonic-gate /*
975d314f035Sedp * Validate a mount point path. A valid mount point path is an
976d314f035Sedp * absolute path that either doesn't exist, or, if it does exists it
977d314f035Sedp * must be an absolute canonical path that doesn't have any symbolic
978d314f035Sedp * links in it. The target of a mount point path can be any filesystem
979d314f035Sedp * object. (Different filesystems can support different mount points,
980d314f035Sedp * for example "lofs" and "mntfs" both support files and directories
981d314f035Sedp * while "ufs" just supports directories.)
9827c478bd9Sstevel@tonic-gate *
983d314f035Sedp * If the path is invalid then a negative value is returned. If the
984d314f035Sedp * path exists and is a valid mount point path then 0 is returned.
985d314f035Sedp * If the path doesn't exist return a positive value.
9867c478bd9Sstevel@tonic-gate */
987d314f035Sedp int
valid_mount_path(zlog_t * zlogp,const char * rootpath,const char * spec,const char * dir,const char * fstype)988d314f035Sedp valid_mount_path(zlog_t *zlogp, const char *rootpath, const char *spec,
989d314f035Sedp const char *dir, const char *fstype)
9907c478bd9Sstevel@tonic-gate {
991d314f035Sedp char abspath[MAXPATHLEN], *slashp, *slashp_next;
992d314f035Sedp int rv;
9937c478bd9Sstevel@tonic-gate
9947c478bd9Sstevel@tonic-gate /*
995d314f035Sedp * Sanity check the target mount point path.
996d314f035Sedp * It must be a non-null string that starts with a '/'.
9977c478bd9Sstevel@tonic-gate */
998d314f035Sedp if (dir[0] != '/') {
999d314f035Sedp /* Something went wrong. */
1000d314f035Sedp zerror(zlogp, B_FALSE, "invalid mount directory, "
1001d314f035Sedp "type: \"%s\", special: \"%s\", dir: \"%s\"",
1002d314f035Sedp fstype, spec, dir);
1003d314f035Sedp return (-1);
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate
1006d314f035Sedp /*
1007d314f035Sedp * Join rootpath and dir. Make sure abspath ends with '/', this
1008d314f035Sedp * is added to all paths (even non-directory paths) to allow us
1009d314f035Sedp * to detect the end of paths below. If the path already ends
1010d314f035Sedp * in a '/', then that's ok too (although we'll fail the
1011d314f035Sedp * cannonical path check in valid_mount_point()).
1012d314f035Sedp */
1013d314f035Sedp if (snprintf(abspath, sizeof (abspath),
1014d314f035Sedp "%s%s/", rootpath, dir) >= sizeof (abspath)) {
1015d314f035Sedp zerror(zlogp, B_FALSE, "pathname %s%s is too long",
1016d314f035Sedp rootpath, dir);
1017d314f035Sedp return (-1);
1018d314f035Sedp }
1019d314f035Sedp
1020d314f035Sedp /*
1021d314f035Sedp * Starting with rootpath, verify the mount path one component
1022d314f035Sedp * at a time. Continue until we've evaluated all of abspath.
1023d314f035Sedp */
10247c478bd9Sstevel@tonic-gate slashp = &abspath[strlen(rootpath)];
10257c478bd9Sstevel@tonic-gate assert(*slashp == '/');
10267c478bd9Sstevel@tonic-gate do {
1027d314f035Sedp slashp_next = strchr(slashp + 1, '/');
10287c478bd9Sstevel@tonic-gate *slashp = '\0';
1029d314f035Sedp if (slashp_next != NULL) {
1030d314f035Sedp /* This is an intermediary mount path component. */
1031d314f035Sedp rv = valid_mount_point(zlogp, abspath, B_FALSE);
1032d314f035Sedp } else {
1033d314f035Sedp /* This is the last component of the mount path. */
1034d314f035Sedp rv = valid_mount_point(zlogp, abspath, B_TRUE);
1035d314f035Sedp }
1036d314f035Sedp if (rv < 0)
1037d314f035Sedp return (rv);
10387c478bd9Sstevel@tonic-gate *slashp = '/';
1039d314f035Sedp } while ((slashp = slashp_next) != NULL);
1040d314f035Sedp return (rv);
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate
10437c478bd9Sstevel@tonic-gate static int
mount_one_dev_device_cb(void * arg,const char * match,const char * name)10449acbbeafSnn35248 mount_one_dev_device_cb(void *arg, const char *match, const char *name)
10459acbbeafSnn35248 {
10469acbbeafSnn35248 di_prof_t prof = arg;
10479acbbeafSnn35248
10489acbbeafSnn35248 if (name == NULL)
10499acbbeafSnn35248 return (di_prof_add_dev(prof, match));
10509acbbeafSnn35248 return (di_prof_add_map(prof, match, name));
10519acbbeafSnn35248 }
10529acbbeafSnn35248
10539acbbeafSnn35248 static int
mount_one_dev_symlink_cb(void * arg,const char * source,const char * target)10549acbbeafSnn35248 mount_one_dev_symlink_cb(void *arg, const char *source, const char *target)
10559acbbeafSnn35248 {
10569acbbeafSnn35248 di_prof_t prof = arg;
10579acbbeafSnn35248
10589acbbeafSnn35248 return (di_prof_add_symlink(prof, source, target));
10599acbbeafSnn35248 }
10609acbbeafSnn35248
10612b24ab6bSSebastien Roy int
vplat_get_iptype(zlog_t * zlogp,zone_iptype_t * iptypep)10622b24ab6bSSebastien Roy vplat_get_iptype(zlog_t *zlogp, zone_iptype_t *iptypep)
1063f4b3ec61Sdh155122 {
1064f4b3ec61Sdh155122 zone_dochandle_t handle;
1065f4b3ec61Sdh155122
1066f4b3ec61Sdh155122 if ((handle = zonecfg_init_handle()) == NULL) {
1067f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "getting zone configuration handle");
1068f4b3ec61Sdh155122 return (-1);
1069f4b3ec61Sdh155122 }
1070f4b3ec61Sdh155122 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
1071f4b3ec61Sdh155122 zerror(zlogp, B_FALSE, "invalid configuration");
1072f4b3ec61Sdh155122 zonecfg_fini_handle(handle);
1073f4b3ec61Sdh155122 return (-1);
1074f4b3ec61Sdh155122 }
1075f4b3ec61Sdh155122 if (zonecfg_get_iptype(handle, iptypep) != Z_OK) {
1076f4b3ec61Sdh155122 zerror(zlogp, B_FALSE, "invalid ip-type configuration");
1077f4b3ec61Sdh155122 zonecfg_fini_handle(handle);
1078f4b3ec61Sdh155122 return (-1);
1079f4b3ec61Sdh155122 }
1080f4b3ec61Sdh155122 zonecfg_fini_handle(handle);
1081f4b3ec61Sdh155122 return (0);
1082f4b3ec61Sdh155122 }
1083f4b3ec61Sdh155122
10849acbbeafSnn35248 /*
10859acbbeafSnn35248 * Apply the standard lists of devices/symlinks/mappings and the user-specified
10869acbbeafSnn35248 * list of devices (via zonecfg) to the /dev filesystem. The filesystem will
10879acbbeafSnn35248 * use these as a profile/filter to determine what exists in /dev.
10889acbbeafSnn35248 */
10899acbbeafSnn35248 static int
mount_one_dev(zlog_t * zlogp,char * devpath,zone_mnt_t mount_cmd)10900679f794S mount_one_dev(zlog_t *zlogp, char *devpath, zone_mnt_t mount_cmd)
10919acbbeafSnn35248 {
10929acbbeafSnn35248 char brand[MAXNAMELEN];
10939acbbeafSnn35248 zone_dochandle_t handle = NULL;
1094123807fbSedp brand_handle_t bh = NULL;
10959acbbeafSnn35248 struct zone_devtab ztab;
10969acbbeafSnn35248 di_prof_t prof = NULL;
10979acbbeafSnn35248 int err;
10989acbbeafSnn35248 int retval = -1;
1099f4b3ec61Sdh155122 zone_iptype_t iptype;
1100f4b3ec61Sdh155122 const char *curr_iptype;
11019acbbeafSnn35248
11029acbbeafSnn35248 if (di_prof_init(devpath, &prof)) {
11039acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to initialize profile");
11049acbbeafSnn35248 goto cleanup;
11059acbbeafSnn35248 }
11069acbbeafSnn35248
11070679f794S /*
11080679f794S * Get a handle to the brand info for this zone.
1109e5816e35SEdward Pilatowicz * If we are mounting the zone, then we must always use the default
11100679f794S * brand device mounts.
11110679f794S */
11120679f794S if (ALT_MOUNT(mount_cmd)) {
1113e5816e35SEdward Pilatowicz (void) strlcpy(brand, default_brand, sizeof (brand));
11140679f794S } else {
111562868012SSteve Lawrence (void) strlcpy(brand, brand_name, sizeof (brand));
11160679f794S }
11170679f794S
11180679f794S if ((bh = brand_open(brand)) == NULL) {
11199acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand");
11209acbbeafSnn35248 goto cleanup;
11219acbbeafSnn35248 }
11229acbbeafSnn35248
11232b24ab6bSSebastien Roy if (vplat_get_iptype(zlogp, &iptype) < 0) {
1124f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "unable to determine ip-type");
1125f4b3ec61Sdh155122 goto cleanup;
1126f4b3ec61Sdh155122 }
1127f4b3ec61Sdh155122 switch (iptype) {
1128f4b3ec61Sdh155122 case ZS_SHARED:
1129f4b3ec61Sdh155122 curr_iptype = "shared";
1130f4b3ec61Sdh155122 break;
1131f4b3ec61Sdh155122 case ZS_EXCLUSIVE:
1132f4b3ec61Sdh155122 curr_iptype = "exclusive";
1133f4b3ec61Sdh155122 break;
1134f4b3ec61Sdh155122 }
1135f4b3ec61Sdh155122
1136123807fbSedp if (brand_platform_iter_devices(bh, zone_name,
1137f4b3ec61Sdh155122 mount_one_dev_device_cb, prof, curr_iptype) != 0) {
11389acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to add standard device");
11399acbbeafSnn35248 goto cleanup;
11409acbbeafSnn35248 }
11419acbbeafSnn35248
1142123807fbSedp if (brand_platform_iter_link(bh,
11439acbbeafSnn35248 mount_one_dev_symlink_cb, prof) != 0) {
11449acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to add standard symlink");
11459acbbeafSnn35248 goto cleanup;
11469acbbeafSnn35248 }
11479acbbeafSnn35248
11489acbbeafSnn35248 /* Add user-specified devices and directories */
11499acbbeafSnn35248 if ((handle = zonecfg_init_handle()) == NULL) {
11509acbbeafSnn35248 zerror(zlogp, B_FALSE, "can't initialize zone handle");
11519acbbeafSnn35248 goto cleanup;
11529acbbeafSnn35248 }
11539acbbeafSnn35248 if (err = zonecfg_get_handle(zone_name, handle)) {
11549acbbeafSnn35248 zerror(zlogp, B_FALSE, "can't get handle for zone "
11559acbbeafSnn35248 "%s: %s", zone_name, zonecfg_strerror(err));
11569acbbeafSnn35248 goto cleanup;
11579acbbeafSnn35248 }
11589acbbeafSnn35248 if (err = zonecfg_setdevent(handle)) {
11599acbbeafSnn35248 zerror(zlogp, B_FALSE, "%s: %s", zone_name,
11609acbbeafSnn35248 zonecfg_strerror(err));
11619acbbeafSnn35248 goto cleanup;
11629acbbeafSnn35248 }
11639acbbeafSnn35248 while (zonecfg_getdevent(handle, &ztab) == Z_OK) {
11649acbbeafSnn35248 if (di_prof_add_dev(prof, ztab.zone_dev_match)) {
11659acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to add "
11669acbbeafSnn35248 "user-specified device");
11679acbbeafSnn35248 goto cleanup;
11689acbbeafSnn35248 }
11699acbbeafSnn35248 }
11709acbbeafSnn35248 (void) zonecfg_enddevent(handle);
11719acbbeafSnn35248
11729acbbeafSnn35248 /* Send profile to kernel */
11739acbbeafSnn35248 if (di_prof_commit(prof)) {
11749acbbeafSnn35248 zerror(zlogp, B_TRUE, "failed to commit profile");
11759acbbeafSnn35248 goto cleanup;
11769acbbeafSnn35248 }
11779acbbeafSnn35248
11789acbbeafSnn35248 retval = 0;
11799acbbeafSnn35248
11809acbbeafSnn35248 cleanup:
1181123807fbSedp if (bh != NULL)
1182123807fbSedp brand_close(bh);
1183e767a340Sgjelinek if (handle != NULL)
11849acbbeafSnn35248 zonecfg_fini_handle(handle);
11859acbbeafSnn35248 if (prof)
11869acbbeafSnn35248 di_prof_fini(prof);
11879acbbeafSnn35248 return (retval);
11889acbbeafSnn35248 }
11899acbbeafSnn35248
11909acbbeafSnn35248 static int
mount_one(zlog_t * zlogp,struct zone_fstab * fsptr,const char * rootpath,zone_mnt_t mount_cmd)11910679f794S mount_one(zlog_t *zlogp, struct zone_fstab *fsptr, const char *rootpath,
11920679f794S zone_mnt_t mount_cmd)
11937c478bd9Sstevel@tonic-gate {
11947c478bd9Sstevel@tonic-gate char path[MAXPATHLEN];
11957c478bd9Sstevel@tonic-gate char optstr[MAX_MNTOPT_STR];
11967c478bd9Sstevel@tonic-gate zone_fsopt_t *optptr;
11979acbbeafSnn35248 int rv;
11987c478bd9Sstevel@tonic-gate
1199d314f035Sedp if ((rv = valid_mount_path(zlogp, rootpath, fsptr->zone_fs_special,
1200d314f035Sedp fsptr->zone_fs_dir, fsptr->zone_fs_type)) < 0) {
12017c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
12027c478bd9Sstevel@tonic-gate rootpath, fsptr->zone_fs_dir);
12037c478bd9Sstevel@tonic-gate return (-1);
1204d314f035Sedp } else if (rv > 0) {
1205d314f035Sedp /* The mount point path doesn't exist, create it now. */
1206d314f035Sedp if (make_one_dir(zlogp, rootpath, fsptr->zone_fs_dir,
1207d314f035Sedp DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
1208d314f035Sedp DEFAULT_DIR_GROUP) != 0) {
1209d314f035Sedp zerror(zlogp, B_FALSE, "failed to create mount point");
1210d314f035Sedp return (-1);
12117c478bd9Sstevel@tonic-gate }
12127c478bd9Sstevel@tonic-gate
1213d314f035Sedp /*
1214d314f035Sedp * Now this might seem weird, but we need to invoke
1215d314f035Sedp * valid_mount_path() again. Why? Because it checks
1216d314f035Sedp * to make sure that the mount point path is canonical,
1217d314f035Sedp * which it can only do if the path exists, so now that
1218d314f035Sedp * we've created the path we have to verify it again.
1219d314f035Sedp */
1220d314f035Sedp if ((rv = valid_mount_path(zlogp, rootpath,
1221d314f035Sedp fsptr->zone_fs_special, fsptr->zone_fs_dir,
1222d314f035Sedp fsptr->zone_fs_type)) < 0) {
1223d314f035Sedp zerror(zlogp, B_FALSE,
1224d314f035Sedp "%s%s is not a valid mount point",
1225d314f035Sedp rootpath, fsptr->zone_fs_dir);
12267c478bd9Sstevel@tonic-gate return (-1);
1227d314f035Sedp }
1228d314f035Sedp }
12297c478bd9Sstevel@tonic-gate
12307c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s", rootpath,
12317c478bd9Sstevel@tonic-gate fsptr->zone_fs_dir);
12327c478bd9Sstevel@tonic-gate
12337c478bd9Sstevel@tonic-gate /*
12347c478bd9Sstevel@tonic-gate * In general the strategy here is to do just as much verification as
12357c478bd9Sstevel@tonic-gate * necessary to avoid crashing or otherwise doing something bad; if the
12367c478bd9Sstevel@tonic-gate * administrator initiated the operation via zoneadm(1m), he'll get
12377c478bd9Sstevel@tonic-gate * auto-verification which will let him know what's wrong. If he
12387c478bd9Sstevel@tonic-gate * modifies the zone configuration of a running zone and doesn't attempt
12397c478bd9Sstevel@tonic-gate * to verify that it's OK we won't crash but won't bother trying to be
12407c478bd9Sstevel@tonic-gate * too helpful either. zoneadm verify is only a couple keystrokes away.
12417c478bd9Sstevel@tonic-gate */
12427c478bd9Sstevel@tonic-gate if (!zonecfg_valid_fs_type(fsptr->zone_fs_type)) {
12437c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "cannot mount %s on %s: "
12447c478bd9Sstevel@tonic-gate "invalid file-system type %s", fsptr->zone_fs_special,
12457c478bd9Sstevel@tonic-gate fsptr->zone_fs_dir, fsptr->zone_fs_type);
12467c478bd9Sstevel@tonic-gate return (-1);
12477c478bd9Sstevel@tonic-gate }
12487c478bd9Sstevel@tonic-gate
12497c478bd9Sstevel@tonic-gate /*
1250108322fbScarlsonj * If we're looking at an alternate root environment, then construct
1251fa3d7ae1Sedp * read-only loopback mounts as necessary. Note that any special
1252fa3d7ae1Sedp * paths for lofs zone mounts in an alternate root must have
1253fa3d7ae1Sedp * already been pre-pended with any alternate root path by the
1254fa3d7ae1Sedp * time we get here.
1255108322fbScarlsonj */
1256108322fbScarlsonj if (zonecfg_in_alt_root()) {
1257108322fbScarlsonj struct stat64 st;
1258108322fbScarlsonj
1259108322fbScarlsonj if (stat64(fsptr->zone_fs_special, &st) != -1 &&
126068eeb376Scarlsonj S_ISBLK(st.st_mode)) {
1261fa3d7ae1Sedp /*
1262fa3d7ae1Sedp * If we're going to mount a block device we need
1263fa3d7ae1Sedp * to check if that device is already mounted
1264fa3d7ae1Sedp * somewhere else, and if so, do a lofs mount
1265fa3d7ae1Sedp * of the device instead of a direct mount
1266fa3d7ae1Sedp */
126768eeb376Scarlsonj if (check_lofs_needed(zlogp, fsptr) == -1)
1268108322fbScarlsonj return (-1);
126968eeb376Scarlsonj } else if (strcmp(fsptr->zone_fs_type, MNTTYPE_LOFS) == 0) {
1270fa3d7ae1Sedp /*
1271fa3d7ae1Sedp * For lofs mounts, the special node is inside the
1272fa3d7ae1Sedp * alternate root. We need lofs resolution for
1273fa3d7ae1Sedp * this case in order to get at the underlying
1274fa3d7ae1Sedp * read-write path.
1275fa3d7ae1Sedp */
1276fa3d7ae1Sedp resolve_lofs(zlogp, fsptr->zone_fs_special,
1277108322fbScarlsonj sizeof (fsptr->zone_fs_special));
1278108322fbScarlsonj }
1279108322fbScarlsonj }
1280108322fbScarlsonj
1281108322fbScarlsonj /*
12827c478bd9Sstevel@tonic-gate * Run 'fsck -m' if there's a device to fsck.
12837c478bd9Sstevel@tonic-gate */
12847c478bd9Sstevel@tonic-gate if (fsptr->zone_fs_raw[0] != '\0' &&
128593239addSjohnlev dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_raw) != 0) {
12867c478bd9Sstevel@tonic-gate return (-1);
128793239addSjohnlev } else if (isregfile(fsptr->zone_fs_special) == 1 &&
128893239addSjohnlev dofsck(zlogp, fsptr->zone_fs_type, fsptr->zone_fs_special) != 0) {
128993239addSjohnlev return (-1);
129093239addSjohnlev }
12917c478bd9Sstevel@tonic-gate
12927c478bd9Sstevel@tonic-gate /*
12937c478bd9Sstevel@tonic-gate * Build up mount option string.
12947c478bd9Sstevel@tonic-gate */
12957c478bd9Sstevel@tonic-gate optstr[0] = '\0';
12967c478bd9Sstevel@tonic-gate if (fsptr->zone_fs_options != NULL) {
12977c478bd9Sstevel@tonic-gate (void) strlcpy(optstr, fsptr->zone_fs_options->zone_fsopt_opt,
12987c478bd9Sstevel@tonic-gate sizeof (optstr));
12997c478bd9Sstevel@tonic-gate for (optptr = fsptr->zone_fs_options->zone_fsopt_next;
13007c478bd9Sstevel@tonic-gate optptr != NULL; optptr = optptr->zone_fsopt_next) {
13017c478bd9Sstevel@tonic-gate (void) strlcat(optstr, ",", sizeof (optstr));
13027c478bd9Sstevel@tonic-gate (void) strlcat(optstr, optptr->zone_fsopt_opt,
13037c478bd9Sstevel@tonic-gate sizeof (optstr));
13047c478bd9Sstevel@tonic-gate }
13057c478bd9Sstevel@tonic-gate }
13069acbbeafSnn35248
13079acbbeafSnn35248 if ((rv = domount(zlogp, fsptr->zone_fs_type, optstr,
13089acbbeafSnn35248 fsptr->zone_fs_special, path)) != 0)
13099acbbeafSnn35248 return (rv);
13109acbbeafSnn35248
13119acbbeafSnn35248 /*
13129acbbeafSnn35248 * The mount succeeded. If this was not a mount of /dev then
13139acbbeafSnn35248 * we're done.
13149acbbeafSnn35248 */
13159acbbeafSnn35248 if (strcmp(fsptr->zone_fs_type, MNTTYPE_DEV) != 0)
13169acbbeafSnn35248 return (0);
13179acbbeafSnn35248
13189acbbeafSnn35248 /*
13199acbbeafSnn35248 * We just mounted an instance of a /dev filesystem, so now we
13209acbbeafSnn35248 * need to configure it.
13219acbbeafSnn35248 */
13220679f794S return (mount_one_dev(zlogp, path, mount_cmd));
13237c478bd9Sstevel@tonic-gate }
13247c478bd9Sstevel@tonic-gate
13257c478bd9Sstevel@tonic-gate static void
free_fs_data(struct zone_fstab * fsarray,uint_t nelem)13267c478bd9Sstevel@tonic-gate free_fs_data(struct zone_fstab *fsarray, uint_t nelem)
13277c478bd9Sstevel@tonic-gate {
13287c478bd9Sstevel@tonic-gate uint_t i;
13297c478bd9Sstevel@tonic-gate
13307c478bd9Sstevel@tonic-gate if (fsarray == NULL)
13317c478bd9Sstevel@tonic-gate return;
13327c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++)
13337c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(fsarray[i].zone_fs_options);
13347c478bd9Sstevel@tonic-gate free(fsarray);
13357c478bd9Sstevel@tonic-gate }
13367c478bd9Sstevel@tonic-gate
1337108322fbScarlsonj /*
1338f4368d3dSvp157776 * This function initiates the creation of a small Solaris Environment for
1339f4368d3dSvp157776 * scratch zone. The Environment creation process is split up into two
1340f4368d3dSvp157776 * functions(build_mounted_pre_var() and build_mounted_post_var()). It
1341f4368d3dSvp157776 * is done this way because:
1342f4368d3dSvp157776 * We need to have both /etc and /var in the root of the scratchzone.
1343f4368d3dSvp157776 * We loopback mount zone's own /etc and /var into the root of the
1344f4368d3dSvp157776 * scratch zone. Unlike /etc, /var can be a seperate filesystem. So we
1345f4368d3dSvp157776 * need to delay the mount of /var till the zone's root gets populated.
1346f4368d3dSvp157776 * So mounting of localdirs[](/etc and /var) have been moved to the
1347f4368d3dSvp157776 * build_mounted_post_var() which gets called only after the zone
1348f4368d3dSvp157776 * specific filesystems are mounted.
13496cfd72c6Sgjelinek *
13506cfd72c6Sgjelinek * Note that the scratch zone we set up for updating the zone (Z_MNT_UPDATE)
13516cfd72c6Sgjelinek * does not loopback mount the zone's own /etc and /var into the root of the
13526cfd72c6Sgjelinek * scratch zone.
1353108322fbScarlsonj */
1354108322fbScarlsonj static boolean_t
build_mounted_pre_var(zlog_t * zlogp,char * rootpath,size_t rootlen,const char * zonepath,char * luroot,size_t lurootlen)1355f4368d3dSvp157776 build_mounted_pre_var(zlog_t *zlogp, char *rootpath,
135616bca938Svp157776 size_t rootlen, const char *zonepath, char *luroot, size_t lurootlen)
1357108322fbScarlsonj {
1358108322fbScarlsonj char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1359108322fbScarlsonj const char **cpp;
1360108322fbScarlsonj static const char *mkdirs[] = {
13613f604e0fSdp "/system", "/system/contract", "/system/object", "/proc",
13623f604e0fSdp "/dev", "/tmp", "/a", NULL
1363108322fbScarlsonj };
1364108322fbScarlsonj char *altstr;
1365f4368d3dSvp157776 FILE *fp;
1366108322fbScarlsonj uuid_t uuid;
1367108322fbScarlsonj
1368108322fbScarlsonj resolve_lofs(zlogp, rootpath, rootlen);
136916bca938Svp157776 (void) snprintf(luroot, lurootlen, "%s/lu", zonepath);
137016bca938Svp157776 resolve_lofs(zlogp, luroot, lurootlen);
1371108322fbScarlsonj (void) snprintf(tmp, sizeof (tmp), "%s/bin", luroot);
1372108322fbScarlsonj (void) symlink("./usr/bin", tmp);
1373108322fbScarlsonj
1374108322fbScarlsonj /*
1375108322fbScarlsonj * These are mostly special mount points; not handled here. (See
1376108322fbScarlsonj * zone_mount_early.)
1377108322fbScarlsonj */
1378108322fbScarlsonj for (cpp = mkdirs; *cpp != NULL; cpp++) {
1379108322fbScarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1380108322fbScarlsonj if (mkdir(tmp, 0755) != 0) {
1381108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1382108322fbScarlsonj return (B_FALSE);
1383108322fbScarlsonj }
1384108322fbScarlsonj }
1385f4368d3dSvp157776 /*
1386f4368d3dSvp157776 * This is here to support lucopy. If there's an instance of this same
1387f4368d3dSvp157776 * zone on the current running system, then we mount its root up as
1388f4368d3dSvp157776 * read-only inside the scratch zone.
1389f4368d3dSvp157776 */
1390f4368d3dSvp157776 (void) zonecfg_get_uuid(zone_name, uuid);
1391f4368d3dSvp157776 altstr = strdup(zonecfg_get_root());
1392f4368d3dSvp157776 if (altstr == NULL) {
1393f4368d3dSvp157776 zerror(zlogp, B_TRUE, "memory allocation failed");
1394f4368d3dSvp157776 return (B_FALSE);
1395f4368d3dSvp157776 }
1396f4368d3dSvp157776 zonecfg_set_root("");
1397f4368d3dSvp157776 (void) strlcpy(tmp, zone_name, sizeof (tmp));
1398f4368d3dSvp157776 (void) zonecfg_get_name_by_uuid(uuid, tmp, sizeof (tmp));
1399f4368d3dSvp157776 if (zone_get_rootpath(tmp, fromdir, sizeof (fromdir)) == Z_OK &&
1400f4368d3dSvp157776 strcmp(fromdir, rootpath) != 0) {
1401f4368d3dSvp157776 (void) snprintf(tmp, sizeof (tmp), "%s/b", luroot);
1402f4368d3dSvp157776 if (mkdir(tmp, 0755) != 0) {
1403f4368d3dSvp157776 zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1404f4368d3dSvp157776 return (B_FALSE);
1405f4368d3dSvp157776 }
14066e1ae2a3SGary Pennington if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, fromdir,
1407f4368d3dSvp157776 tmp) != 0) {
1408f4368d3dSvp157776 zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1409f4368d3dSvp157776 fromdir);
1410f4368d3dSvp157776 return (B_FALSE);
1411f4368d3dSvp157776 }
1412f4368d3dSvp157776 }
1413f4368d3dSvp157776 zonecfg_set_root(altstr);
1414f4368d3dSvp157776 free(altstr);
1415f4368d3dSvp157776
1416f4368d3dSvp157776 if ((fp = zonecfg_open_scratch(luroot, B_TRUE)) == NULL) {
1417f4368d3dSvp157776 zerror(zlogp, B_TRUE, "cannot open zone mapfile");
1418f4368d3dSvp157776 return (B_FALSE);
1419f4368d3dSvp157776 }
1420f4368d3dSvp157776 (void) ftruncate(fileno(fp), 0);
1421f4368d3dSvp157776 if (zonecfg_add_scratch(fp, zone_name, kernzone, "/") == -1) {
1422f4368d3dSvp157776 zerror(zlogp, B_TRUE, "cannot add zone mapfile entry");
1423f4368d3dSvp157776 }
1424f4368d3dSvp157776 zonecfg_close_scratch(fp);
1425f4368d3dSvp157776 (void) snprintf(tmp, sizeof (tmp), "%s/a", luroot);
1426f4368d3dSvp157776 if (domount(zlogp, MNTTYPE_LOFS, "", rootpath, tmp) != 0)
1427f4368d3dSvp157776 return (B_FALSE);
1428f4368d3dSvp157776 (void) strlcpy(rootpath, tmp, rootlen);
1429f4368d3dSvp157776 return (B_TRUE);
1430f4368d3dSvp157776 }
1431f4368d3dSvp157776
1432f4368d3dSvp157776
1433f4368d3dSvp157776 static boolean_t
build_mounted_post_var(zlog_t * zlogp,zone_mnt_t mount_cmd,char * rootpath,const char * luroot)14346cfd72c6Sgjelinek build_mounted_post_var(zlog_t *zlogp, zone_mnt_t mount_cmd, char *rootpath,
14356cfd72c6Sgjelinek const char *luroot)
1436f4368d3dSvp157776 {
1437f4368d3dSvp157776 char tmp[MAXPATHLEN], fromdir[MAXPATHLEN];
1438f4368d3dSvp157776 const char **cpp;
14396cfd72c6Sgjelinek const char **loopdirs;
14406cfd72c6Sgjelinek const char **tmpdirs;
1441f4368d3dSvp157776 static const char *localdirs[] = {
1442f4368d3dSvp157776 "/etc", "/var", NULL
1443f4368d3dSvp157776 };
14446cfd72c6Sgjelinek static const char *scr_loopdirs[] = {
1445f4368d3dSvp157776 "/etc/lib", "/etc/fs", "/lib", "/sbin", "/platform",
1446f4368d3dSvp157776 "/usr", NULL
1447f4368d3dSvp157776 };
14486cfd72c6Sgjelinek static const char *upd_loopdirs[] = {
14496cfd72c6Sgjelinek "/etc", "/kernel", "/lib", "/opt", "/platform", "/sbin",
14506cfd72c6Sgjelinek "/usr", "/var", NULL
14516cfd72c6Sgjelinek };
14526cfd72c6Sgjelinek static const char *scr_tmpdirs[] = {
1453f4368d3dSvp157776 "/tmp", "/var/run", NULL
1454f4368d3dSvp157776 };
14556cfd72c6Sgjelinek static const char *upd_tmpdirs[] = {
14566cfd72c6Sgjelinek "/tmp", "/var/run", "/var/tmp", NULL
14576cfd72c6Sgjelinek };
1458f4368d3dSvp157776 struct stat st;
1459f4368d3dSvp157776
14606cfd72c6Sgjelinek if (mount_cmd == Z_MNT_SCRATCH) {
1461108322fbScarlsonj /*
14626cfd72c6Sgjelinek * These are mounted read-write from the zone undergoing
14636cfd72c6Sgjelinek * upgrade. We must be careful not to 'leak' things from the
14646cfd72c6Sgjelinek * main system into the zone, and this accomplishes that goal.
1465108322fbScarlsonj */
1466108322fbScarlsonj for (cpp = localdirs; *cpp != NULL; cpp++) {
14676cfd72c6Sgjelinek (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot,
1468108322fbScarlsonj *cpp);
14696cfd72c6Sgjelinek (void) snprintf(fromdir, sizeof (fromdir), "%s%s",
14706cfd72c6Sgjelinek rootpath, *cpp);
1471108322fbScarlsonj if (mkdir(tmp, 0755) != 0) {
1472108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1473108322fbScarlsonj return (B_FALSE);
1474108322fbScarlsonj }
14756cfd72c6Sgjelinek if (domount(zlogp, MNTTYPE_LOFS, "", fromdir, tmp)
14766cfd72c6Sgjelinek != 0) {
14776cfd72c6Sgjelinek zerror(zlogp, B_TRUE, "cannot mount %s on %s",
14786cfd72c6Sgjelinek tmp, *cpp);
1479108322fbScarlsonj return (B_FALSE);
1480108322fbScarlsonj }
1481108322fbScarlsonj }
14826cfd72c6Sgjelinek }
14836cfd72c6Sgjelinek
14846cfd72c6Sgjelinek if (mount_cmd == Z_MNT_UPDATE)
14856cfd72c6Sgjelinek loopdirs = upd_loopdirs;
14866cfd72c6Sgjelinek else
14876cfd72c6Sgjelinek loopdirs = scr_loopdirs;
1488108322fbScarlsonj
1489108322fbScarlsonj /*
1490108322fbScarlsonj * These are things mounted read-only from the running system because
1491108322fbScarlsonj * they contain binaries that must match system.
1492108322fbScarlsonj */
1493108322fbScarlsonj for (cpp = loopdirs; *cpp != NULL; cpp++) {
1494108322fbScarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
1495108322fbScarlsonj if (mkdir(tmp, 0755) != 0) {
1496108322fbScarlsonj if (errno != EEXIST) {
1497108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1498108322fbScarlsonj return (B_FALSE);
1499108322fbScarlsonj }
1500108322fbScarlsonj if (lstat(tmp, &st) != 0) {
1501108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot stat %s", tmp);
1502108322fbScarlsonj return (B_FALSE);
1503108322fbScarlsonj }
1504108322fbScarlsonj /*
1505108322fbScarlsonj * Ignore any non-directories encountered. These are
1506108322fbScarlsonj * things that have been converted into symlinks
1507108322fbScarlsonj * (/etc/fs and /etc/lib) and no longer need a lofs
1508108322fbScarlsonj * fixup.
1509108322fbScarlsonj */
1510108322fbScarlsonj if (!S_ISDIR(st.st_mode))
1511108322fbScarlsonj continue;
1512108322fbScarlsonj }
15136e1ae2a3SGary Pennington if (domount(zlogp, MNTTYPE_LOFS, RESOURCE_DEFAULT_OPTS, *cpp,
1514108322fbScarlsonj tmp) != 0) {
1515108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot mount %s on %s", tmp,
1516108322fbScarlsonj *cpp);
1517108322fbScarlsonj return (B_FALSE);
1518108322fbScarlsonj }
1519108322fbScarlsonj }
1520108322fbScarlsonj
15216cfd72c6Sgjelinek if (mount_cmd == Z_MNT_UPDATE)
15226cfd72c6Sgjelinek tmpdirs = upd_tmpdirs;
15236cfd72c6Sgjelinek else
15246cfd72c6Sgjelinek tmpdirs = scr_tmpdirs;
15256cfd72c6Sgjelinek
1526108322fbScarlsonj /*
1527108322fbScarlsonj * These are things with tmpfs mounted inside.
1528108322fbScarlsonj */
1529108322fbScarlsonj for (cpp = tmpdirs; *cpp != NULL; cpp++) {
1530108322fbScarlsonj (void) snprintf(tmp, sizeof (tmp), "%s%s", luroot, *cpp);
15316cfd72c6Sgjelinek if (mount_cmd == Z_MNT_SCRATCH && mkdir(tmp, 0755) != 0 &&
15326cfd72c6Sgjelinek errno != EEXIST) {
1533108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", tmp);
1534108322fbScarlsonj return (B_FALSE);
1535108322fbScarlsonj }
1536d30e996aSgjelinek
1537d30e996aSgjelinek /*
1538d30e996aSgjelinek * We could set the mode for /tmp when we do the mkdir but
1539d30e996aSgjelinek * since that can be modified by the umask we will just set
1540d30e996aSgjelinek * the correct mode for /tmp now.
1541d30e996aSgjelinek */
1542d30e996aSgjelinek if (strcmp(*cpp, "/tmp") == 0 && chmod(tmp, 01777) != 0) {
1543d30e996aSgjelinek zerror(zlogp, B_TRUE, "cannot chmod %s", tmp);
1544d30e996aSgjelinek return (B_FALSE);
1545d30e996aSgjelinek }
1546d30e996aSgjelinek
1547108322fbScarlsonj if (domount(zlogp, MNTTYPE_TMPFS, "", "swap", tmp) != 0) {
1548108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot mount swap on %s", *cpp);
1549108322fbScarlsonj return (B_FALSE);
1550108322fbScarlsonj }
1551108322fbScarlsonj }
1552108322fbScarlsonj return (B_TRUE);
1553108322fbScarlsonj }
1554108322fbScarlsonj
15559acbbeafSnn35248 typedef struct plat_gmount_cb_data {
15569acbbeafSnn35248 zlog_t *pgcd_zlogp;
15579acbbeafSnn35248 struct zone_fstab **pgcd_fs_tab;
15589acbbeafSnn35248 int *pgcd_num_fs;
15599acbbeafSnn35248 } plat_gmount_cb_data_t;
15609acbbeafSnn35248
15619acbbeafSnn35248 /*
15629acbbeafSnn35248 * plat_gmount_cb() is a callback function invoked by libbrand to iterate
15639acbbeafSnn35248 * through all global brand platform mounts.
15649acbbeafSnn35248 */
15659acbbeafSnn35248 int
plat_gmount_cb(void * data,const char * spec,const char * dir,const char * fstype,const char * opt)15669acbbeafSnn35248 plat_gmount_cb(void *data, const char *spec, const char *dir,
15679acbbeafSnn35248 const char *fstype, const char *opt)
15689acbbeafSnn35248 {
15699acbbeafSnn35248 plat_gmount_cb_data_t *cp = data;
15709acbbeafSnn35248 zlog_t *zlogp = cp->pgcd_zlogp;
15719acbbeafSnn35248 struct zone_fstab *fs_ptr = *cp->pgcd_fs_tab;
15729acbbeafSnn35248 int num_fs = *cp->pgcd_num_fs;
15739acbbeafSnn35248 struct zone_fstab *fsp, *tmp_ptr;
15749acbbeafSnn35248
15759acbbeafSnn35248 num_fs++;
15769acbbeafSnn35248 if ((tmp_ptr = realloc(fs_ptr, num_fs * sizeof (*tmp_ptr))) == NULL) {
15779acbbeafSnn35248 zerror(zlogp, B_TRUE, "memory allocation failed");
15789acbbeafSnn35248 return (-1);
15799acbbeafSnn35248 }
15809acbbeafSnn35248
15819acbbeafSnn35248 fs_ptr = tmp_ptr;
15829acbbeafSnn35248 fsp = &fs_ptr[num_fs - 1];
15839acbbeafSnn35248
15849acbbeafSnn35248 /* update the callback struct passed in */
15859acbbeafSnn35248 *cp->pgcd_fs_tab = fs_ptr;
15869acbbeafSnn35248 *cp->pgcd_num_fs = num_fs;
15879acbbeafSnn35248
15889acbbeafSnn35248 fsp->zone_fs_raw[0] = '\0';
15899acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_special, spec,
15909acbbeafSnn35248 sizeof (fsp->zone_fs_special));
15919acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_dir, dir, sizeof (fsp->zone_fs_dir));
15929acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_type, fstype, sizeof (fsp->zone_fs_type));
15939acbbeafSnn35248 fsp->zone_fs_options = NULL;
1594fa3d7ae1Sedp if ((opt != NULL) &&
1595fa3d7ae1Sedp (zonecfg_add_fs_option(fsp, (char *)opt) != Z_OK)) {
15969acbbeafSnn35248 zerror(zlogp, B_FALSE, "error adding property");
15979acbbeafSnn35248 return (-1);
15989acbbeafSnn35248 }
15999acbbeafSnn35248
16009acbbeafSnn35248 return (0);
16019acbbeafSnn35248 }
16029acbbeafSnn35248
16039acbbeafSnn35248 static int
mount_filesystems_fsent(zone_dochandle_t handle,zlog_t * zlogp,struct zone_fstab ** fs_tabp,int * num_fsp,zone_mnt_t mount_cmd)16049acbbeafSnn35248 mount_filesystems_fsent(zone_dochandle_t handle, zlog_t *zlogp,
16056cfd72c6Sgjelinek struct zone_fstab **fs_tabp, int *num_fsp, zone_mnt_t mount_cmd)
16069acbbeafSnn35248 {
16079acbbeafSnn35248 struct zone_fstab *tmp_ptr, *fs_ptr, *fsp, fstab;
16089acbbeafSnn35248 int num_fs;
16099acbbeafSnn35248
16109acbbeafSnn35248 num_fs = *num_fsp;
16119acbbeafSnn35248 fs_ptr = *fs_tabp;
16129acbbeafSnn35248
16139acbbeafSnn35248 if (zonecfg_setfsent(handle) != Z_OK) {
16149acbbeafSnn35248 zerror(zlogp, B_FALSE, "invalid configuration");
16159acbbeafSnn35248 return (-1);
16169acbbeafSnn35248 }
16179acbbeafSnn35248 while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
16189acbbeafSnn35248 /*
16199acbbeafSnn35248 * ZFS filesystems will not be accessible under an alternate
16209acbbeafSnn35248 * root, since the pool will not be known. Ignore them in this
16219acbbeafSnn35248 * case.
16229acbbeafSnn35248 */
16236cfd72c6Sgjelinek if (ALT_MOUNT(mount_cmd) &&
16246cfd72c6Sgjelinek strcmp(fstab.zone_fs_type, MNTTYPE_ZFS) == 0)
16259acbbeafSnn35248 continue;
16269acbbeafSnn35248
16279acbbeafSnn35248 num_fs++;
16289acbbeafSnn35248 if ((tmp_ptr = realloc(fs_ptr,
16299acbbeafSnn35248 num_fs * sizeof (*tmp_ptr))) == NULL) {
16309acbbeafSnn35248 zerror(zlogp, B_TRUE, "memory allocation failed");
16319acbbeafSnn35248 (void) zonecfg_endfsent(handle);
16329acbbeafSnn35248 return (-1);
16339acbbeafSnn35248 }
16349acbbeafSnn35248 /* update the pointers passed in */
16359acbbeafSnn35248 *fs_tabp = tmp_ptr;
16369acbbeafSnn35248 *num_fsp = num_fs;
16379acbbeafSnn35248
16389acbbeafSnn35248 fs_ptr = tmp_ptr;
16399acbbeafSnn35248 fsp = &fs_ptr[num_fs - 1];
16409acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_dir,
16419acbbeafSnn35248 fstab.zone_fs_dir, sizeof (fsp->zone_fs_dir));
16429acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_raw, fstab.zone_fs_raw,
16439acbbeafSnn35248 sizeof (fsp->zone_fs_raw));
16449acbbeafSnn35248 (void) strlcpy(fsp->zone_fs_type, fstab.zone_fs_type,
16459acbbeafSnn35248 sizeof (fsp->zone_fs_type));
16469acbbeafSnn35248 fsp->zone_fs_options = fstab.zone_fs_options;
1647fa3d7ae1Sedp
1648fa3d7ae1Sedp /*
1649fa3d7ae1Sedp * For all lofs mounts, make sure that the 'special'
1650fa3d7ae1Sedp * entry points inside the alternate root. The
1651fa3d7ae1Sedp * source path for a lofs mount in a given zone needs
1652fa3d7ae1Sedp * to be relative to the root of the boot environment
1653fa3d7ae1Sedp * that contains the zone. Note that we don't do this
1654fa3d7ae1Sedp * for non-lofs mounts since they will have a device
1655fa3d7ae1Sedp * as a backing store and device paths must always be
1656fa3d7ae1Sedp * specified relative to the current boot environment.
1657fa3d7ae1Sedp */
1658fa3d7ae1Sedp fsp->zone_fs_special[0] = '\0';
1659fa3d7ae1Sedp if (strcmp(fsp->zone_fs_type, MNTTYPE_LOFS) == 0) {
1660fa3d7ae1Sedp (void) strlcat(fsp->zone_fs_special, zonecfg_get_root(),
1661fa3d7ae1Sedp sizeof (fsp->zone_fs_special));
1662fa3d7ae1Sedp }
1663fa3d7ae1Sedp (void) strlcat(fsp->zone_fs_special, fstab.zone_fs_special,
1664fa3d7ae1Sedp sizeof (fsp->zone_fs_special));
16659acbbeafSnn35248 }
16669acbbeafSnn35248 (void) zonecfg_endfsent(handle);
16679acbbeafSnn35248 return (0);
16689acbbeafSnn35248 }
16699acbbeafSnn35248
16707c478bd9Sstevel@tonic-gate static int
mount_filesystems(zlog_t * zlogp,zone_mnt_t mount_cmd)16716cfd72c6Sgjelinek mount_filesystems(zlog_t *zlogp, zone_mnt_t mount_cmd)
16727c478bd9Sstevel@tonic-gate {
16737c478bd9Sstevel@tonic-gate char rootpath[MAXPATHLEN];
16747c478bd9Sstevel@tonic-gate char zonepath[MAXPATHLEN];
16759acbbeafSnn35248 char brand[MAXNAMELEN];
167616bca938Svp157776 char luroot[MAXPATHLEN];
16779acbbeafSnn35248 int i, num_fs = 0;
16789acbbeafSnn35248 struct zone_fstab *fs_ptr = NULL;
16797c478bd9Sstevel@tonic-gate zone_dochandle_t handle = NULL;
16807c478bd9Sstevel@tonic-gate zone_state_t zstate;
1681123807fbSedp brand_handle_t bh;
16829acbbeafSnn35248 plat_gmount_cb_data_t cb;
16837c478bd9Sstevel@tonic-gate
16847c478bd9Sstevel@tonic-gate if (zone_get_state(zone_name, &zstate) != Z_OK ||
1685108322fbScarlsonj (zstate != ZONE_STATE_READY && zstate != ZONE_STATE_MOUNTED)) {
16867c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE,
1687108322fbScarlsonj "zone must be in '%s' or '%s' state to mount file-systems",
1688108322fbScarlsonj zone_state_str(ZONE_STATE_READY),
1689108322fbScarlsonj zone_state_str(ZONE_STATE_MOUNTED));
16907c478bd9Sstevel@tonic-gate goto bad;
16917c478bd9Sstevel@tonic-gate }
16927c478bd9Sstevel@tonic-gate
16937c478bd9Sstevel@tonic-gate if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
16947c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone path");
16957c478bd9Sstevel@tonic-gate goto bad;
16967c478bd9Sstevel@tonic-gate }
16977c478bd9Sstevel@tonic-gate
16987c478bd9Sstevel@tonic-gate if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
16997c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone root");
17007c478bd9Sstevel@tonic-gate goto bad;
17017c478bd9Sstevel@tonic-gate }
17027c478bd9Sstevel@tonic-gate
17037c478bd9Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) {
1704ffbafc53Scomay zerror(zlogp, B_TRUE, "getting zone configuration handle");
17057c478bd9Sstevel@tonic-gate goto bad;
17067c478bd9Sstevel@tonic-gate }
17077c478bd9Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK ||
17087c478bd9Sstevel@tonic-gate zonecfg_setfsent(handle) != Z_OK) {
17097c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration");
17107c478bd9Sstevel@tonic-gate goto bad;
17117c478bd9Sstevel@tonic-gate }
17127c478bd9Sstevel@tonic-gate
17130679f794S /*
1714e5816e35SEdward Pilatowicz * If we are mounting the zone, then we must always use the default
17150679f794S * brand global mounts.
17160679f794S */
17170679f794S if (ALT_MOUNT(mount_cmd)) {
1718e5816e35SEdward Pilatowicz (void) strlcpy(brand, default_brand, sizeof (brand));
17190679f794S } else {
172062868012SSteve Lawrence (void) strlcpy(brand, brand_name, sizeof (brand));
17210679f794S }
17220679f794S
17239acbbeafSnn35248 /* Get a handle to the brand info for this zone */
17240679f794S if ((bh = brand_open(brand)) == NULL) {
17259acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand");
1726e767a340Sgjelinek zonecfg_fini_handle(handle);
17279acbbeafSnn35248 return (-1);
17289acbbeafSnn35248 }
17299acbbeafSnn35248
17309acbbeafSnn35248 /*
17319acbbeafSnn35248 * Get the list of global filesystems to mount from the brand
17329acbbeafSnn35248 * configuration.
17339acbbeafSnn35248 */
17349acbbeafSnn35248 cb.pgcd_zlogp = zlogp;
17359acbbeafSnn35248 cb.pgcd_fs_tab = &fs_ptr;
17369acbbeafSnn35248 cb.pgcd_num_fs = &num_fs;
1737123807fbSedp if (brand_platform_iter_gmounts(bh, zonepath,
17389acbbeafSnn35248 plat_gmount_cb, &cb) != 0) {
17399acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to mount filesystems");
1740123807fbSedp brand_close(bh);
1741e767a340Sgjelinek zonecfg_fini_handle(handle);
17429acbbeafSnn35248 return (-1);
17439acbbeafSnn35248 }
1744123807fbSedp brand_close(bh);
17459acbbeafSnn35248
17467c478bd9Sstevel@tonic-gate /*
17476e1ae2a3SGary Pennington * Iterate through the rest of the filesystems. Sort them all,
17486e1ae2a3SGary Pennington * then mount them in sorted order. This is to make sure the
17496e1ae2a3SGary Pennington * higher level directories (e.g., /usr) get mounted before
17506e1ae2a3SGary Pennington * any beneath them (e.g., /usr/local).
17517c478bd9Sstevel@tonic-gate */
17529acbbeafSnn35248 if (mount_filesystems_fsent(handle, zlogp, &fs_ptr, &num_fs,
17539acbbeafSnn35248 mount_cmd) != 0)
17547c478bd9Sstevel@tonic-gate goto bad;
1755fa9e4066Sahrens
17567c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle);
17577c478bd9Sstevel@tonic-gate handle = NULL;
17587c478bd9Sstevel@tonic-gate
1759108322fbScarlsonj /*
17609acbbeafSnn35248 * Normally when we mount a zone all the zone filesystems
17619acbbeafSnn35248 * get mounted relative to rootpath, which is usually
17629acbbeafSnn35248 * <zonepath>/root. But when mounting a zone for administration
17639acbbeafSnn35248 * purposes via the zone "mount" state, build_mounted_pre_var()
17649acbbeafSnn35248 * updates rootpath to be <zonepath>/lu/a so we'll mount all
17659acbbeafSnn35248 * the zones filesystems there instead.
17669acbbeafSnn35248 *
17679acbbeafSnn35248 * build_mounted_pre_var() and build_mounted_post_var() will
17689acbbeafSnn35248 * also do some extra work to create directories and lofs mount
17699acbbeafSnn35248 * a bunch of global zone file system paths into <zonepath>/lu.
17709acbbeafSnn35248 *
17719acbbeafSnn35248 * This allows us to be able to enter the zone (now rooted at
17729acbbeafSnn35248 * <zonepath>/lu) and run the upgrade/patch tools that are in the
17739acbbeafSnn35248 * global zone and have them upgrade the to-be-modified zone's
17749acbbeafSnn35248 * files mounted on /a. (Which mirrors the existing standard
17759acbbeafSnn35248 * upgrade environment.)
17769acbbeafSnn35248 *
17779acbbeafSnn35248 * There is of course one catch. When doing the upgrade
17789acbbeafSnn35248 * we need <zoneroot>/lu/dev to be the /dev filesystem
17799acbbeafSnn35248 * for the zone and we don't want to have any /dev filesystem
17809acbbeafSnn35248 * mounted at <zoneroot>/lu/a/dev. Since /dev is specified
17819acbbeafSnn35248 * as a normal zone filesystem by default we'll try to mount
17829acbbeafSnn35248 * it at <zoneroot>/lu/a/dev, so we have to detect this
17839acbbeafSnn35248 * case and instead mount it at <zoneroot>/lu/dev.
17849acbbeafSnn35248 *
17859acbbeafSnn35248 * All this work is done in three phases:
1786f4368d3dSvp157776 * 1) Create and populate lu directory (build_mounted_pre_var()).
1787f4368d3dSvp157776 * 2) Mount the required filesystems as per the zone configuration.
1788f4368d3dSvp157776 * 3) Set up the rest of the scratch zone environment
1789f4368d3dSvp157776 * (build_mounted_post_var()).
1790108322fbScarlsonj */
17916cfd72c6Sgjelinek if (ALT_MOUNT(mount_cmd) && !build_mounted_pre_var(zlogp,
179216bca938Svp157776 rootpath, sizeof (rootpath), zonepath, luroot, sizeof (luroot)))
1793108322fbScarlsonj goto bad;
1794108322fbScarlsonj
17957c478bd9Sstevel@tonic-gate qsort(fs_ptr, num_fs, sizeof (*fs_ptr), fs_compare);
17969acbbeafSnn35248
17977c478bd9Sstevel@tonic-gate for (i = 0; i < num_fs; i++) {
17986cfd72c6Sgjelinek if (ALT_MOUNT(mount_cmd) &&
17999acbbeafSnn35248 strcmp(fs_ptr[i].zone_fs_dir, "/dev") == 0) {
18009acbbeafSnn35248 size_t slen = strlen(rootpath) - 2;
18019acbbeafSnn35248
18029acbbeafSnn35248 /*
18039acbbeafSnn35248 * By default we'll try to mount /dev as /a/dev
18049acbbeafSnn35248 * but /dev is special and always goes at the top
18059acbbeafSnn35248 * so strip the trailing '/a' from the rootpath.
18069acbbeafSnn35248 */
18079acbbeafSnn35248 assert(strcmp(&rootpath[slen], "/a") == 0);
18089acbbeafSnn35248 rootpath[slen] = '\0';
18090679f794S if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd)
18100679f794S != 0)
18119acbbeafSnn35248 goto bad;
18129acbbeafSnn35248 rootpath[slen] = '/';
18139acbbeafSnn35248 continue;
18149acbbeafSnn35248 }
18150679f794S if (mount_one(zlogp, &fs_ptr[i], rootpath, mount_cmd) != 0)
18167c478bd9Sstevel@tonic-gate goto bad;
18177c478bd9Sstevel@tonic-gate }
18186cfd72c6Sgjelinek if (ALT_MOUNT(mount_cmd) &&
18196cfd72c6Sgjelinek !build_mounted_post_var(zlogp, mount_cmd, rootpath, luroot))
1820f4368d3dSvp157776 goto bad;
182145916cd2Sjpk
182245916cd2Sjpk /*
182345916cd2Sjpk * For Trusted Extensions cross-mount each lower level /export/home
182445916cd2Sjpk */
18256cfd72c6Sgjelinek if (mount_cmd == Z_MNT_BOOT &&
18266cfd72c6Sgjelinek tsol_mounts(zlogp, zone_name, rootpath) != 0)
182745916cd2Sjpk goto bad;
182845916cd2Sjpk
18297c478bd9Sstevel@tonic-gate free_fs_data(fs_ptr, num_fs);
18307c478bd9Sstevel@tonic-gate
18317c478bd9Sstevel@tonic-gate /*
18327c478bd9Sstevel@tonic-gate * Everything looks fine.
18337c478bd9Sstevel@tonic-gate */
18347c478bd9Sstevel@tonic-gate return (0);
18357c478bd9Sstevel@tonic-gate
18367c478bd9Sstevel@tonic-gate bad:
18377c478bd9Sstevel@tonic-gate if (handle != NULL)
18387c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle);
18397c478bd9Sstevel@tonic-gate free_fs_data(fs_ptr, num_fs);
18407c478bd9Sstevel@tonic-gate return (-1);
18417c478bd9Sstevel@tonic-gate }
18427c478bd9Sstevel@tonic-gate
18437c478bd9Sstevel@tonic-gate /* caller makes sure neither parameter is NULL */
18447c478bd9Sstevel@tonic-gate static int
addr2netmask(char * prefixstr,int maxprefixlen,uchar_t * maskstr)18457c478bd9Sstevel@tonic-gate addr2netmask(char *prefixstr, int maxprefixlen, uchar_t *maskstr)
18467c478bd9Sstevel@tonic-gate {
18477c478bd9Sstevel@tonic-gate int prefixlen;
18487c478bd9Sstevel@tonic-gate
18497c478bd9Sstevel@tonic-gate prefixlen = atoi(prefixstr);
18507c478bd9Sstevel@tonic-gate if (prefixlen < 0 || prefixlen > maxprefixlen)
18517c478bd9Sstevel@tonic-gate return (1);
18527c478bd9Sstevel@tonic-gate while (prefixlen > 0) {
18537c478bd9Sstevel@tonic-gate if (prefixlen >= 8) {
18547c478bd9Sstevel@tonic-gate *maskstr++ = 0xFF;
18557c478bd9Sstevel@tonic-gate prefixlen -= 8;
18567c478bd9Sstevel@tonic-gate continue;
18577c478bd9Sstevel@tonic-gate }
18587c478bd9Sstevel@tonic-gate *maskstr |= 1 << (8 - prefixlen);
18597c478bd9Sstevel@tonic-gate prefixlen--;
18607c478bd9Sstevel@tonic-gate }
18617c478bd9Sstevel@tonic-gate return (0);
18627c478bd9Sstevel@tonic-gate }
18637c478bd9Sstevel@tonic-gate
18647c478bd9Sstevel@tonic-gate /*
18657c478bd9Sstevel@tonic-gate * Tear down all interfaces belonging to the given zone. This should
18667c478bd9Sstevel@tonic-gate * be called with the zone in a state other than "running", so that
18677c478bd9Sstevel@tonic-gate * interfaces can't be assigned to the zone after this returns.
18687c478bd9Sstevel@tonic-gate *
18697c478bd9Sstevel@tonic-gate * If anything goes wrong, log an error message and return an error.
18707c478bd9Sstevel@tonic-gate */
18717c478bd9Sstevel@tonic-gate static int
unconfigure_shared_network_interfaces(zlog_t * zlogp,zoneid_t zone_id)1872f4b3ec61Sdh155122 unconfigure_shared_network_interfaces(zlog_t *zlogp, zoneid_t zone_id)
18737c478bd9Sstevel@tonic-gate {
18747c478bd9Sstevel@tonic-gate struct lifnum lifn;
18757c478bd9Sstevel@tonic-gate struct lifconf lifc;
18767c478bd9Sstevel@tonic-gate struct lifreq *lifrp, lifrl;
18777c478bd9Sstevel@tonic-gate int64_t lifc_flags = LIFC_NOXMIT | LIFC_ALLZONES;
18787c478bd9Sstevel@tonic-gate int num_ifs, s, i, ret_code = 0;
18797c478bd9Sstevel@tonic-gate uint_t bufsize;
18807c478bd9Sstevel@tonic-gate char *buf = NULL;
18817c478bd9Sstevel@tonic-gate
18827c478bd9Sstevel@tonic-gate if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
18837c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get socket");
18847c478bd9Sstevel@tonic-gate ret_code = -1;
18857c478bd9Sstevel@tonic-gate goto bad;
18867c478bd9Sstevel@tonic-gate }
18877c478bd9Sstevel@tonic-gate lifn.lifn_family = AF_UNSPEC;
18887c478bd9Sstevel@tonic-gate lifn.lifn_flags = (int)lifc_flags;
18897c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) < 0) {
18907c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE,
1891f4b3ec61Sdh155122 "could not determine number of network interfaces");
18927c478bd9Sstevel@tonic-gate ret_code = -1;
18937c478bd9Sstevel@tonic-gate goto bad;
18947c478bd9Sstevel@tonic-gate }
18957c478bd9Sstevel@tonic-gate num_ifs = lifn.lifn_count;
18967c478bd9Sstevel@tonic-gate bufsize = num_ifs * sizeof (struct lifreq);
18977c478bd9Sstevel@tonic-gate if ((buf = malloc(bufsize)) == NULL) {
18987c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed");
18997c478bd9Sstevel@tonic-gate ret_code = -1;
19007c478bd9Sstevel@tonic-gate goto bad;
19017c478bd9Sstevel@tonic-gate }
19027c478bd9Sstevel@tonic-gate lifc.lifc_family = AF_UNSPEC;
19037c478bd9Sstevel@tonic-gate lifc.lifc_flags = (int)lifc_flags;
19047c478bd9Sstevel@tonic-gate lifc.lifc_len = bufsize;
19057c478bd9Sstevel@tonic-gate lifc.lifc_buf = buf;
19067c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) < 0) {
1907f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "could not get configured network "
1908f4b3ec61Sdh155122 "interfaces");
19097c478bd9Sstevel@tonic-gate ret_code = -1;
19107c478bd9Sstevel@tonic-gate goto bad;
19117c478bd9Sstevel@tonic-gate }
19127c478bd9Sstevel@tonic-gate lifrp = lifc.lifc_req;
19137c478bd9Sstevel@tonic-gate for (i = lifc.lifc_len / sizeof (struct lifreq); i > 0; i--, lifrp++) {
19147c478bd9Sstevel@tonic-gate (void) close(s);
19157c478bd9Sstevel@tonic-gate if ((s = socket(lifrp->lifr_addr.ss_family, SOCK_DGRAM, 0)) <
19167c478bd9Sstevel@tonic-gate 0) {
19177c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get socket",
19187c478bd9Sstevel@tonic-gate lifrl.lifr_name);
19197c478bd9Sstevel@tonic-gate ret_code = -1;
19207c478bd9Sstevel@tonic-gate continue;
19217c478bd9Sstevel@tonic-gate }
19227c478bd9Sstevel@tonic-gate (void) memset(&lifrl, 0, sizeof (lifrl));
19237c478bd9Sstevel@tonic-gate (void) strncpy(lifrl.lifr_name, lifrp->lifr_name,
19247c478bd9Sstevel@tonic-gate sizeof (lifrl.lifr_name));
19257c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifrl) < 0) {
1926c1c0ebd5Ssl108498 if (errno == ENXIO)
1927c1c0ebd5Ssl108498 /*
1928c1c0ebd5Ssl108498 * Interface may have been removed by admin or
1929c1c0ebd5Ssl108498 * another zone halting.
1930c1c0ebd5Ssl108498 */
1931c1c0ebd5Ssl108498 continue;
19327c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE,
1933c1c0ebd5Ssl108498 "%s: could not determine the zone to which this "
1934f4b3ec61Sdh155122 "network interface is bound", lifrl.lifr_name);
19357c478bd9Sstevel@tonic-gate ret_code = -1;
19367c478bd9Sstevel@tonic-gate continue;
19377c478bd9Sstevel@tonic-gate }
19387c478bd9Sstevel@tonic-gate if (lifrl.lifr_zoneid == zone_id) {
19397c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifrl) < 0) {
19407c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE,
1941f4b3ec61Sdh155122 "%s: could not remove network interface",
19427c478bd9Sstevel@tonic-gate lifrl.lifr_name);
19437c478bd9Sstevel@tonic-gate ret_code = -1;
19447c478bd9Sstevel@tonic-gate continue;
19457c478bd9Sstevel@tonic-gate }
19467c478bd9Sstevel@tonic-gate }
19477c478bd9Sstevel@tonic-gate }
19487c478bd9Sstevel@tonic-gate bad:
19497c478bd9Sstevel@tonic-gate if (s > 0)
19507c478bd9Sstevel@tonic-gate (void) close(s);
19517c478bd9Sstevel@tonic-gate if (buf)
19527c478bd9Sstevel@tonic-gate free(buf);
19537c478bd9Sstevel@tonic-gate return (ret_code);
19547c478bd9Sstevel@tonic-gate }
19557c478bd9Sstevel@tonic-gate
19567c478bd9Sstevel@tonic-gate static union sockunion {
19577c478bd9Sstevel@tonic-gate struct sockaddr sa;
19587c478bd9Sstevel@tonic-gate struct sockaddr_in sin;
19597c478bd9Sstevel@tonic-gate struct sockaddr_dl sdl;
19607c478bd9Sstevel@tonic-gate struct sockaddr_in6 sin6;
19617c478bd9Sstevel@tonic-gate } so_dst, so_ifp;
19627c478bd9Sstevel@tonic-gate
19637c478bd9Sstevel@tonic-gate static struct {
19647c478bd9Sstevel@tonic-gate struct rt_msghdr hdr;
19657c478bd9Sstevel@tonic-gate char space[512];
19667c478bd9Sstevel@tonic-gate } rtmsg;
19677c478bd9Sstevel@tonic-gate
19687c478bd9Sstevel@tonic-gate static int
salen(struct sockaddr * sa)19697c478bd9Sstevel@tonic-gate salen(struct sockaddr *sa)
19707c478bd9Sstevel@tonic-gate {
19717c478bd9Sstevel@tonic-gate switch (sa->sa_family) {
19727c478bd9Sstevel@tonic-gate case AF_INET:
19737c478bd9Sstevel@tonic-gate return (sizeof (struct sockaddr_in));
19747c478bd9Sstevel@tonic-gate case AF_LINK:
19757c478bd9Sstevel@tonic-gate return (sizeof (struct sockaddr_dl));
19767c478bd9Sstevel@tonic-gate case AF_INET6:
19777c478bd9Sstevel@tonic-gate return (sizeof (struct sockaddr_in6));
19787c478bd9Sstevel@tonic-gate default:
19797c478bd9Sstevel@tonic-gate return (sizeof (struct sockaddr));
19807c478bd9Sstevel@tonic-gate }
19817c478bd9Sstevel@tonic-gate }
19827c478bd9Sstevel@tonic-gate
19837c478bd9Sstevel@tonic-gate #define ROUNDUP_LONG(a) \
19847c478bd9Sstevel@tonic-gate ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
19857c478bd9Sstevel@tonic-gate
19867c478bd9Sstevel@tonic-gate /*
19877c478bd9Sstevel@tonic-gate * Look up which zone is using a given IP address. The address in question
19887c478bd9Sstevel@tonic-gate * is expected to have been stuffed into the structure to which lifr points
19897c478bd9Sstevel@tonic-gate * via a previous SIOCGLIFADDR ioctl().
19907c478bd9Sstevel@tonic-gate *
19917c478bd9Sstevel@tonic-gate * This is done using black router socket magic.
19927c478bd9Sstevel@tonic-gate *
19937c478bd9Sstevel@tonic-gate * Return the name of the zone on success or NULL on failure.
19947c478bd9Sstevel@tonic-gate *
19957c478bd9Sstevel@tonic-gate * This is a lot of code for a simple task; a new ioctl request to take care
19967c478bd9Sstevel@tonic-gate * of this might be a useful RFE.
19977c478bd9Sstevel@tonic-gate */
19987c478bd9Sstevel@tonic-gate
19997c478bd9Sstevel@tonic-gate static char *
who_is_using(zlog_t * zlogp,struct lifreq * lifr)20007c478bd9Sstevel@tonic-gate who_is_using(zlog_t *zlogp, struct lifreq *lifr)
20017c478bd9Sstevel@tonic-gate {
20027c478bd9Sstevel@tonic-gate static char answer[ZONENAME_MAX];
20037c478bd9Sstevel@tonic-gate pid_t pid;
20047c478bd9Sstevel@tonic-gate int s, rlen, l, i;
20057c478bd9Sstevel@tonic-gate char *cp = rtmsg.space;
20067c478bd9Sstevel@tonic-gate struct sockaddr_dl *ifp = NULL;
20077c478bd9Sstevel@tonic-gate struct sockaddr *sa;
20087c478bd9Sstevel@tonic-gate char save_if_name[LIFNAMSIZ];
20097c478bd9Sstevel@tonic-gate
20107c478bd9Sstevel@tonic-gate answer[0] = '\0';
20117c478bd9Sstevel@tonic-gate
20127c478bd9Sstevel@tonic-gate pid = getpid();
20137c478bd9Sstevel@tonic-gate if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
20147c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get routing socket");
20157c478bd9Sstevel@tonic-gate return (NULL);
20167c478bd9Sstevel@tonic-gate }
20177c478bd9Sstevel@tonic-gate
20187c478bd9Sstevel@tonic-gate if (lifr->lifr_addr.ss_family == AF_INET) {
20197c478bd9Sstevel@tonic-gate struct sockaddr_in *sin4;
20207c478bd9Sstevel@tonic-gate
20217c478bd9Sstevel@tonic-gate so_dst.sa.sa_family = AF_INET;
20227c478bd9Sstevel@tonic-gate sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20237c478bd9Sstevel@tonic-gate so_dst.sin.sin_addr = sin4->sin_addr;
20247c478bd9Sstevel@tonic-gate } else {
20257c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6;
20267c478bd9Sstevel@tonic-gate
20277c478bd9Sstevel@tonic-gate so_dst.sa.sa_family = AF_INET6;
20287c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20297c478bd9Sstevel@tonic-gate so_dst.sin6.sin6_addr = sin6->sin6_addr;
20307c478bd9Sstevel@tonic-gate }
20317c478bd9Sstevel@tonic-gate
20327c478bd9Sstevel@tonic-gate so_ifp.sa.sa_family = AF_LINK;
20337c478bd9Sstevel@tonic-gate
20347c478bd9Sstevel@tonic-gate (void) memset(&rtmsg, 0, sizeof (rtmsg));
20357c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_type = RTM_GET;
20367c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_flags = RTF_UP | RTF_HOST;
20377c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_version = RTM_VERSION;
20387c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_seq = ++rts_seqno;
20397c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_addrs = RTA_IFP | RTA_DST;
20407c478bd9Sstevel@tonic-gate
20417c478bd9Sstevel@tonic-gate l = ROUNDUP_LONG(salen(&so_dst.sa));
20427c478bd9Sstevel@tonic-gate (void) memmove(cp, &(so_dst), l);
20437c478bd9Sstevel@tonic-gate cp += l;
20447c478bd9Sstevel@tonic-gate l = ROUNDUP_LONG(salen(&so_ifp.sa));
20457c478bd9Sstevel@tonic-gate (void) memmove(cp, &(so_ifp), l);
20467c478bd9Sstevel@tonic-gate cp += l;
20477c478bd9Sstevel@tonic-gate
20487c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_msglen = l = cp - (char *)&rtmsg;
20497c478bd9Sstevel@tonic-gate
20507c478bd9Sstevel@tonic-gate if ((rlen = write(s, &rtmsg, l)) < 0) {
20517c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "writing to routing socket");
20527c478bd9Sstevel@tonic-gate return (NULL);
20537c478bd9Sstevel@tonic-gate } else if (rlen < (int)rtmsg.hdr.rtm_msglen) {
20547c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE,
20557c478bd9Sstevel@tonic-gate "write to routing socket got only %d for len\n", rlen);
20567c478bd9Sstevel@tonic-gate return (NULL);
20577c478bd9Sstevel@tonic-gate }
20587c478bd9Sstevel@tonic-gate do {
20597c478bd9Sstevel@tonic-gate l = read(s, &rtmsg, sizeof (rtmsg));
20607c478bd9Sstevel@tonic-gate } while (l > 0 && (rtmsg.hdr.rtm_seq != rts_seqno ||
20617c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_pid != pid));
20627c478bd9Sstevel@tonic-gate if (l < 0) {
20637c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "reading from routing socket");
20647c478bd9Sstevel@tonic-gate return (NULL);
20657c478bd9Sstevel@tonic-gate }
20667c478bd9Sstevel@tonic-gate
20677c478bd9Sstevel@tonic-gate if (rtmsg.hdr.rtm_version != RTM_VERSION) {
20687c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE,
20697c478bd9Sstevel@tonic-gate "routing message version %d not understood",
20707c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_version);
20717c478bd9Sstevel@tonic-gate return (NULL);
20727c478bd9Sstevel@tonic-gate }
20737c478bd9Sstevel@tonic-gate if (rtmsg.hdr.rtm_msglen != (ushort_t)l) {
20747c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "message length mismatch, "
20757c478bd9Sstevel@tonic-gate "expected %d bytes, returned %d bytes",
20767c478bd9Sstevel@tonic-gate rtmsg.hdr.rtm_msglen, l);
20777c478bd9Sstevel@tonic-gate return (NULL);
20787c478bd9Sstevel@tonic-gate }
20797c478bd9Sstevel@tonic-gate if (rtmsg.hdr.rtm_errno != 0) {
20807c478bd9Sstevel@tonic-gate errno = rtmsg.hdr.rtm_errno;
20817c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "RTM_GET routing socket message");
20827c478bd9Sstevel@tonic-gate return (NULL);
20837c478bd9Sstevel@tonic-gate }
20847c478bd9Sstevel@tonic-gate if ((rtmsg.hdr.rtm_addrs & RTA_IFP) == 0) {
2085f4b3ec61Sdh155122 zerror(zlogp, B_FALSE, "network interface not found");
20867c478bd9Sstevel@tonic-gate return (NULL);
20877c478bd9Sstevel@tonic-gate }
20887c478bd9Sstevel@tonic-gate cp = ((char *)(&rtmsg.hdr + 1));
20897c478bd9Sstevel@tonic-gate for (i = 1; i != 0; i <<= 1) {
20907c478bd9Sstevel@tonic-gate /* LINTED E_BAD_PTR_CAST_ALIGN */
20917c478bd9Sstevel@tonic-gate sa = (struct sockaddr *)cp;
20927c478bd9Sstevel@tonic-gate if (i != RTA_IFP) {
20937c478bd9Sstevel@tonic-gate if ((i & rtmsg.hdr.rtm_addrs) != 0)
20947c478bd9Sstevel@tonic-gate cp += ROUNDUP_LONG(salen(sa));
20957c478bd9Sstevel@tonic-gate continue;
20967c478bd9Sstevel@tonic-gate }
20977c478bd9Sstevel@tonic-gate if (sa->sa_family == AF_LINK &&
20987c478bd9Sstevel@tonic-gate ((struct sockaddr_dl *)sa)->sdl_nlen != 0)
20997c478bd9Sstevel@tonic-gate ifp = (struct sockaddr_dl *)sa;
21007c478bd9Sstevel@tonic-gate break;
21017c478bd9Sstevel@tonic-gate }
21027c478bd9Sstevel@tonic-gate if (ifp == NULL) {
2103f4b3ec61Sdh155122 zerror(zlogp, B_FALSE, "network interface could not be "
2104f4b3ec61Sdh155122 "determined");
21057c478bd9Sstevel@tonic-gate return (NULL);
21067c478bd9Sstevel@tonic-gate }
21077c478bd9Sstevel@tonic-gate
21087c478bd9Sstevel@tonic-gate /*
21097c478bd9Sstevel@tonic-gate * We need to set the I/F name to what we got above, then do the
21107c478bd9Sstevel@tonic-gate * appropriate ioctl to get its zone name. But lifr->lifr_name is
21117c478bd9Sstevel@tonic-gate * used by the calling function to do a REMOVEIF, so if we leave the
21127c478bd9Sstevel@tonic-gate * "good" zone's I/F name in place, *that* I/F will be removed instead
21137c478bd9Sstevel@tonic-gate * of the bad one. So we save the old (bad) I/F name before over-
21147c478bd9Sstevel@tonic-gate * writing it and doing the ioctl, then restore it after the ioctl.
21157c478bd9Sstevel@tonic-gate */
21167c478bd9Sstevel@tonic-gate (void) strlcpy(save_if_name, lifr->lifr_name, sizeof (save_if_name));
21177c478bd9Sstevel@tonic-gate (void) strncpy(lifr->lifr_name, ifp->sdl_data, ifp->sdl_nlen);
21187c478bd9Sstevel@tonic-gate lifr->lifr_name[ifp->sdl_nlen] = '\0';
21197c478bd9Sstevel@tonic-gate i = ioctl(s, SIOCGLIFZONE, lifr);
21207c478bd9Sstevel@tonic-gate (void) strlcpy(lifr->lifr_name, save_if_name, sizeof (save_if_name));
21217c478bd9Sstevel@tonic-gate if (i < 0) {
21227c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE,
2123f4b3ec61Sdh155122 "%s: could not determine the zone network interface "
2124f4b3ec61Sdh155122 "belongs to", lifr->lifr_name);
21257c478bd9Sstevel@tonic-gate return (NULL);
21267c478bd9Sstevel@tonic-gate }
21277c478bd9Sstevel@tonic-gate if (getzonenamebyid(lifr->lifr_zoneid, answer, sizeof (answer)) < 0)
21287c478bd9Sstevel@tonic-gate (void) snprintf(answer, sizeof (answer), "%d",
21297c478bd9Sstevel@tonic-gate lifr->lifr_zoneid);
21307c478bd9Sstevel@tonic-gate
21317c478bd9Sstevel@tonic-gate if (strlen(answer) > 0)
21327c478bd9Sstevel@tonic-gate return (answer);
21337c478bd9Sstevel@tonic-gate return (NULL);
21347c478bd9Sstevel@tonic-gate }
21357c478bd9Sstevel@tonic-gate
21367c478bd9Sstevel@tonic-gate /*
21377c478bd9Sstevel@tonic-gate * Configures a single interface: a new virtual interface is added, based on
21387c478bd9Sstevel@tonic-gate * the physical interface nwiftabptr->zone_nwif_physical, with the address
21397c478bd9Sstevel@tonic-gate * specified in nwiftabptr->zone_nwif_address, for zone zone_id. Note that
21407c478bd9Sstevel@tonic-gate * the "address" can be an IPv6 address (with a /prefixlength required), an
21417c478bd9Sstevel@tonic-gate * IPv4 address (with a /prefixlength optional), or a name; for the latter,
21427c478bd9Sstevel@tonic-gate * an IPv4 name-to-address resolution will be attempted.
21437c478bd9Sstevel@tonic-gate *
21447c478bd9Sstevel@tonic-gate * If anything goes wrong, we log an detailed error message, attempt to tear
21457c478bd9Sstevel@tonic-gate * down whatever we set up and return an error.
21467c478bd9Sstevel@tonic-gate */
21477c478bd9Sstevel@tonic-gate static int
configure_one_interface(zlog_t * zlogp,zoneid_t zone_id,struct zone_nwiftab * nwiftabptr)21487c478bd9Sstevel@tonic-gate configure_one_interface(zlog_t *zlogp, zoneid_t zone_id,
2149f14f3ae7Sjv227347 struct zone_nwiftab *nwiftabptr)
21507c478bd9Sstevel@tonic-gate {
21517c478bd9Sstevel@tonic-gate struct lifreq lifr;
21527c478bd9Sstevel@tonic-gate struct sockaddr_in netmask4;
21537c478bd9Sstevel@tonic-gate struct sockaddr_in6 netmask6;
21542e481403SVamsi Nagineni struct sockaddr_storage laddr;
21557c478bd9Sstevel@tonic-gate struct in_addr in4;
21567c478bd9Sstevel@tonic-gate sa_family_t af;
21577c478bd9Sstevel@tonic-gate char *slashp = strchr(nwiftabptr->zone_nwif_address, '/');
21587c478bd9Sstevel@tonic-gate int s;
21597c478bd9Sstevel@tonic-gate boolean_t got_netmask = B_FALSE;
2160f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India boolean_t is_loopback = B_FALSE;
21617c478bd9Sstevel@tonic-gate char addrstr4[INET_ADDRSTRLEN];
21627c478bd9Sstevel@tonic-gate int res;
21637c478bd9Sstevel@tonic-gate
21647c478bd9Sstevel@tonic-gate res = zonecfg_valid_net_address(nwiftabptr->zone_nwif_address, &lifr);
21657c478bd9Sstevel@tonic-gate if (res != Z_OK) {
21667c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s: %s", zonecfg_strerror(res),
21677c478bd9Sstevel@tonic-gate nwiftabptr->zone_nwif_address);
21687c478bd9Sstevel@tonic-gate return (-1);
21697c478bd9Sstevel@tonic-gate }
21707c478bd9Sstevel@tonic-gate af = lifr.lifr_addr.ss_family;
21717c478bd9Sstevel@tonic-gate if (af == AF_INET)
21727c478bd9Sstevel@tonic-gate in4 = ((struct sockaddr_in *)(&lifr.lifr_addr))->sin_addr;
21737c478bd9Sstevel@tonic-gate if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
21747c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "could not get socket");
21757c478bd9Sstevel@tonic-gate return (-1);
21767c478bd9Sstevel@tonic-gate }
21777c478bd9Sstevel@tonic-gate
21782e481403SVamsi Nagineni /*
21792e481403SVamsi Nagineni * This is a similar kind of "hack" like in addif() to get around
21802e481403SVamsi Nagineni * the problem of SIOCLIFADDIF. The problem is that this ioctl
21812e481403SVamsi Nagineni * does not include the netmask when adding a logical interface.
21822e481403SVamsi Nagineni * To get around this problem, we first add the logical interface
21832e481403SVamsi Nagineni * with a 0 address. After that, we set the netmask if provided.
21842e481403SVamsi Nagineni * Finally we set the interface address.
21852e481403SVamsi Nagineni */
21862e481403SVamsi Nagineni laddr = lifr.lifr_addr;
21877c478bd9Sstevel@tonic-gate (void) strlcpy(lifr.lifr_name, nwiftabptr->zone_nwif_physical,
21887c478bd9Sstevel@tonic-gate sizeof (lifr.lifr_name));
21892e481403SVamsi Nagineni (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
21902e481403SVamsi Nagineni
21917c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0) {
219222321485Svp157776 /*
219322321485Svp157776 * Here, we know that the interface can't be brought up.
219422321485Svp157776 * A similar warning message was already printed out to
219522321485Svp157776 * the console by zoneadm(1M) so instead we log the
219622321485Svp157776 * message to syslog and continue.
219722321485Svp157776 */
2198f4b3ec61Sdh155122 zerror(&logsys, B_TRUE, "WARNING: skipping network interface "
219922321485Svp157776 "'%s' which may not be present/plumbed in the "
220022321485Svp157776 "global zone.", lifr.lifr_name);
22017c478bd9Sstevel@tonic-gate (void) close(s);
220222321485Svp157776 return (Z_OK);
22037c478bd9Sstevel@tonic-gate }
22047c478bd9Sstevel@tonic-gate
22057c478bd9Sstevel@tonic-gate /* Preserve literal IPv4 address for later potential printing. */
22067c478bd9Sstevel@tonic-gate if (af == AF_INET)
22077c478bd9Sstevel@tonic-gate (void) inet_ntop(AF_INET, &in4, addrstr4, INET_ADDRSTRLEN);
22087c478bd9Sstevel@tonic-gate
22097c478bd9Sstevel@tonic-gate lifr.lifr_zoneid = zone_id;
22107c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) < 0) {
2211f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "%s: could not place network interface "
2212f4b3ec61Sdh155122 "into zone", lifr.lifr_name);
22137c478bd9Sstevel@tonic-gate goto bad;
22147c478bd9Sstevel@tonic-gate }
22157c478bd9Sstevel@tonic-gate
2216f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India /*
2217f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India * Loopback interface will use the default netmask assigned, if no
2218f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India * netmask is found.
2219f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India */
22207c478bd9Sstevel@tonic-gate if (strcmp(nwiftabptr->zone_nwif_physical, "lo0") == 0) {
2221f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India is_loopback = B_TRUE;
2222f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India }
22237c478bd9Sstevel@tonic-gate if (af == AF_INET) {
22247c478bd9Sstevel@tonic-gate /*
22257c478bd9Sstevel@tonic-gate * The IPv4 netmask can be determined either
22267c478bd9Sstevel@tonic-gate * directly if a prefix length was supplied with
22277c478bd9Sstevel@tonic-gate * the address or via the netmasks database. Not
22287c478bd9Sstevel@tonic-gate * being able to determine it is a common failure,
22297c478bd9Sstevel@tonic-gate * but it often is not fatal to operation of the
22307c478bd9Sstevel@tonic-gate * interface. In that case, a warning will be
22317c478bd9Sstevel@tonic-gate * printed after the rest of the interface's
22327c478bd9Sstevel@tonic-gate * parameters have been configured.
22337c478bd9Sstevel@tonic-gate */
22347c478bd9Sstevel@tonic-gate (void) memset(&netmask4, 0, sizeof (netmask4));
22357c478bd9Sstevel@tonic-gate if (slashp != NULL) {
22367c478bd9Sstevel@tonic-gate if (addr2netmask(slashp + 1, V4_ADDR_LEN,
22377c478bd9Sstevel@tonic-gate (uchar_t *)&netmask4.sin_addr) != 0) {
22387c478bd9Sstevel@tonic-gate *slashp = '/';
22397c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE,
22407c478bd9Sstevel@tonic-gate "%s: invalid prefix length in %s",
22417c478bd9Sstevel@tonic-gate lifr.lifr_name,
22427c478bd9Sstevel@tonic-gate nwiftabptr->zone_nwif_address);
22437c478bd9Sstevel@tonic-gate goto bad;
22447c478bd9Sstevel@tonic-gate }
22457c478bd9Sstevel@tonic-gate got_netmask = B_TRUE;
22467c478bd9Sstevel@tonic-gate } else if (getnetmaskbyaddr(in4,
22477c478bd9Sstevel@tonic-gate &netmask4.sin_addr) == 0) {
22487c478bd9Sstevel@tonic-gate got_netmask = B_TRUE;
22497c478bd9Sstevel@tonic-gate }
22507c478bd9Sstevel@tonic-gate if (got_netmask) {
22517c478bd9Sstevel@tonic-gate netmask4.sin_family = af;
22527c478bd9Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &netmask4,
22537c478bd9Sstevel@tonic-gate sizeof (netmask4));
22547c478bd9Sstevel@tonic-gate }
22557c478bd9Sstevel@tonic-gate } else {
22567c478bd9Sstevel@tonic-gate (void) memset(&netmask6, 0, sizeof (netmask6));
22577c478bd9Sstevel@tonic-gate if (addr2netmask(slashp + 1, V6_ADDR_LEN,
22587c478bd9Sstevel@tonic-gate (uchar_t *)&netmask6.sin6_addr) != 0) {
22597c478bd9Sstevel@tonic-gate *slashp = '/';
22607c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE,
22617c478bd9Sstevel@tonic-gate "%s: invalid prefix length in %s",
22627c478bd9Sstevel@tonic-gate lifr.lifr_name,
22637c478bd9Sstevel@tonic-gate nwiftabptr->zone_nwif_address);
22647c478bd9Sstevel@tonic-gate goto bad;
22657c478bd9Sstevel@tonic-gate }
22667c478bd9Sstevel@tonic-gate got_netmask = B_TRUE;
22677c478bd9Sstevel@tonic-gate netmask6.sin6_family = af;
22687c478bd9Sstevel@tonic-gate (void) memcpy(&lifr.lifr_addr, &netmask6,
22697c478bd9Sstevel@tonic-gate sizeof (netmask6));
22707c478bd9Sstevel@tonic-gate }
22717c478bd9Sstevel@tonic-gate if (got_netmask &&
22727c478bd9Sstevel@tonic-gate ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0) {
22737c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not set netmask",
22747c478bd9Sstevel@tonic-gate lifr.lifr_name);
22757c478bd9Sstevel@tonic-gate goto bad;
22767c478bd9Sstevel@tonic-gate }
22777c478bd9Sstevel@tonic-gate
22782e481403SVamsi Nagineni /* Set the interface address */
22792e481403SVamsi Nagineni lifr.lifr_addr = laddr;
22807c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
22817c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE,
22822e481403SVamsi Nagineni "%s: could not set IP address to %s",
22832e481403SVamsi Nagineni lifr.lifr_name, nwiftabptr->zone_nwif_address);
22847c478bd9Sstevel@tonic-gate goto bad;
22857c478bd9Sstevel@tonic-gate }
22867c478bd9Sstevel@tonic-gate
22877c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
22887c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get flags",
22897c478bd9Sstevel@tonic-gate lifr.lifr_name);
22907c478bd9Sstevel@tonic-gate goto bad;
22917c478bd9Sstevel@tonic-gate }
22927c478bd9Sstevel@tonic-gate lifr.lifr_flags |= IFF_UP;
22937c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0) {
22947c478bd9Sstevel@tonic-gate int save_errno = errno;
22957c478bd9Sstevel@tonic-gate char *zone_using;
22967c478bd9Sstevel@tonic-gate
22977c478bd9Sstevel@tonic-gate /*
22987c478bd9Sstevel@tonic-gate * If we failed with something other than EADDRNOTAVAIL,
22997c478bd9Sstevel@tonic-gate * then skip to the end. Otherwise, look up our address,
23007c478bd9Sstevel@tonic-gate * then call a function to determine which zone is already
23017c478bd9Sstevel@tonic-gate * using that address.
23027c478bd9Sstevel@tonic-gate */
23037c478bd9Sstevel@tonic-gate if (errno != EADDRNOTAVAIL) {
23047c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE,
2305f4b3ec61Sdh155122 "%s: could not bring network interface up",
2306f4b3ec61Sdh155122 lifr.lifr_name);
23077c478bd9Sstevel@tonic-gate goto bad;
23087c478bd9Sstevel@tonic-gate }
23097c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
23107c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s: could not get address",
23117c478bd9Sstevel@tonic-gate lifr.lifr_name);
23127c478bd9Sstevel@tonic-gate goto bad;
23137c478bd9Sstevel@tonic-gate }
23147c478bd9Sstevel@tonic-gate zone_using = who_is_using(zlogp, &lifr);
23157c478bd9Sstevel@tonic-gate errno = save_errno;
23167c478bd9Sstevel@tonic-gate if (zone_using == NULL)
23177c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE,
2318f4b3ec61Sdh155122 "%s: could not bring network interface up",
2319f4b3ec61Sdh155122 lifr.lifr_name);
23207c478bd9Sstevel@tonic-gate else
2321f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "%s: could not bring network "
2322f4b3ec61Sdh155122 "interface up: address in use by zone '%s'",
2323f4b3ec61Sdh155122 lifr.lifr_name, zone_using);
23247c478bd9Sstevel@tonic-gate goto bad;
23257c478bd9Sstevel@tonic-gate }
23267c478bd9Sstevel@tonic-gate
2327f9329705Ssaurabh vyas - Sun Microsystems - Bangalore India if (!got_netmask && !is_loopback) {
23287c478bd9Sstevel@tonic-gate /*
23297c478bd9Sstevel@tonic-gate * A common, but often non-fatal problem, is that the system
23307c478bd9Sstevel@tonic-gate * cannot find the netmask for an interface address. This is
23317c478bd9Sstevel@tonic-gate * often caused by it being only in /etc/inet/netmasks, but
23327c478bd9Sstevel@tonic-gate * /etc/nsswitch.conf says to use NIS or NIS+ and it's not
23337c478bd9Sstevel@tonic-gate * in that. This doesn't show up at boot because the netmask
23347c478bd9Sstevel@tonic-gate * is obtained from /etc/inet/netmasks when no network
23357c478bd9Sstevel@tonic-gate * interfaces are up, but isn't consulted when NIS/NIS+ is
23367c478bd9Sstevel@tonic-gate * available. We warn the user here that something like this
23377c478bd9Sstevel@tonic-gate * has happened and we're just running with a default and
23387c478bd9Sstevel@tonic-gate * possible incorrect netmask.
23397c478bd9Sstevel@tonic-gate */
23407c478bd9Sstevel@tonic-gate char buffer[INET6_ADDRSTRLEN];
23417c478bd9Sstevel@tonic-gate void *addr;
2342e11c3f44Smeem const char *nomatch = "no matching subnet found in netmasks(4)";
23437c478bd9Sstevel@tonic-gate
23447c478bd9Sstevel@tonic-gate if (af == AF_INET)
23457c478bd9Sstevel@tonic-gate addr = &((struct sockaddr_in *)
23467c478bd9Sstevel@tonic-gate (&lifr.lifr_addr))->sin_addr;
23477c478bd9Sstevel@tonic-gate else
23487c478bd9Sstevel@tonic-gate addr = &((struct sockaddr_in6 *)
23497c478bd9Sstevel@tonic-gate (&lifr.lifr_addr))->sin6_addr;
23507c478bd9Sstevel@tonic-gate
2351e11c3f44Smeem /*
2352e11c3f44Smeem * Find out what netmask the interface is going to be using.
2353e11c3f44Smeem * If we just brought up an IPMP data address on an underlying
2354e11c3f44Smeem * interface above, the address will have already migrated, so
2355e11c3f44Smeem * the SIOCGLIFNETMASK won't be able to find it (but we need
2356e11c3f44Smeem * to bring the address up to get the actual netmask). Just
2357e11c3f44Smeem * omit printing the actual netmask in this corner-case.
2358e11c3f44Smeem */
23597c478bd9Sstevel@tonic-gate if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0 ||
2360e11c3f44Smeem inet_ntop(af, addr, buffer, sizeof (buffer)) == NULL) {
2361e11c3f44Smeem zerror(zlogp, B_FALSE, "WARNING: %s; using default.",
2362e11c3f44Smeem nomatch);
2363e11c3f44Smeem } else {
23647c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE,
2365e11c3f44Smeem "WARNING: %s: %s: %s; using default of %s.",
2366e11c3f44Smeem lifr.lifr_name, nomatch, addrstr4, buffer);
2367e11c3f44Smeem }
23687c478bd9Sstevel@tonic-gate }
23697c478bd9Sstevel@tonic-gate
2370de860bd9Sgfaden /*
2371de860bd9Sgfaden * If a default router was specified for this interface
2372de860bd9Sgfaden * set the route now. Ignore if already set.
2373de860bd9Sgfaden */
2374de860bd9Sgfaden if (strlen(nwiftabptr->zone_nwif_defrouter) > 0) {
2375de860bd9Sgfaden int status;
2376de860bd9Sgfaden char *argv[7];
2377de860bd9Sgfaden
2378de860bd9Sgfaden argv[0] = "route";
2379de860bd9Sgfaden argv[1] = "add";
2380de860bd9Sgfaden argv[2] = "-ifp";
2381de860bd9Sgfaden argv[3] = nwiftabptr->zone_nwif_physical;
2382de860bd9Sgfaden argv[4] = "default";
2383de860bd9Sgfaden argv[5] = nwiftabptr->zone_nwif_defrouter;
2384de860bd9Sgfaden argv[6] = NULL;
2385de860bd9Sgfaden
2386de860bd9Sgfaden status = forkexec(zlogp, "/usr/sbin/route", argv);
2387de860bd9Sgfaden if (status != 0 && status != EEXIST)
2388de860bd9Sgfaden zerror(zlogp, B_FALSE, "Unable to set route for "
2389de860bd9Sgfaden "interface %s to %s\n",
2390de860bd9Sgfaden nwiftabptr->zone_nwif_physical,
2391de860bd9Sgfaden nwiftabptr->zone_nwif_defrouter);
2392de860bd9Sgfaden }
2393de860bd9Sgfaden
23947c478bd9Sstevel@tonic-gate (void) close(s);
23957c478bd9Sstevel@tonic-gate return (Z_OK);
23967c478bd9Sstevel@tonic-gate bad:
23977c478bd9Sstevel@tonic-gate (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
23987c478bd9Sstevel@tonic-gate (void) close(s);
23997c478bd9Sstevel@tonic-gate return (-1);
24007c478bd9Sstevel@tonic-gate }
24017c478bd9Sstevel@tonic-gate
24027c478bd9Sstevel@tonic-gate /*
24037c478bd9Sstevel@tonic-gate * Sets up network interfaces based on information from the zone configuration.
2404f14f3ae7Sjv227347 * IPv4 and IPv6 loopback interfaces are set up "for free", modeling the global
2405f14f3ae7Sjv227347 * system.
24067c478bd9Sstevel@tonic-gate *
24077c478bd9Sstevel@tonic-gate * If anything goes wrong, we log a general error message, attempt to tear down
24087c478bd9Sstevel@tonic-gate * whatever we set up, and return an error.
24097c478bd9Sstevel@tonic-gate */
24107c478bd9Sstevel@tonic-gate static int
configure_shared_network_interfaces(zlog_t * zlogp)2411f4b3ec61Sdh155122 configure_shared_network_interfaces(zlog_t *zlogp)
24127c478bd9Sstevel@tonic-gate {
24137c478bd9Sstevel@tonic-gate zone_dochandle_t handle;
24147c478bd9Sstevel@tonic-gate struct zone_nwiftab nwiftab, loopback_iftab;
24157c478bd9Sstevel@tonic-gate zoneid_t zoneid;
24167c478bd9Sstevel@tonic-gate
24177c478bd9Sstevel@tonic-gate if ((zoneid = getzoneidbyname(zone_name)) == ZONE_ID_UNDEFINED) {
24187c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid");
24197c478bd9Sstevel@tonic-gate return (-1);
24207c478bd9Sstevel@tonic-gate }
24217c478bd9Sstevel@tonic-gate
24227c478bd9Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) {
24237c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "getting zone configuration handle");
24247c478bd9Sstevel@tonic-gate return (-1);
24257c478bd9Sstevel@tonic-gate }
24267c478bd9Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
24277c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration");
24287c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle);
24297c478bd9Sstevel@tonic-gate return (-1);
24307c478bd9Sstevel@tonic-gate }
24317c478bd9Sstevel@tonic-gate if (zonecfg_setnwifent(handle) == Z_OK) {
24327c478bd9Sstevel@tonic-gate for (;;) {
24337c478bd9Sstevel@tonic-gate if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
24347c478bd9Sstevel@tonic-gate break;
2435f14f3ae7Sjv227347 if (configure_one_interface(zlogp, zoneid, &nwiftab) !=
24367c478bd9Sstevel@tonic-gate Z_OK) {
24377c478bd9Sstevel@tonic-gate (void) zonecfg_endnwifent(handle);
24387c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle);
24397c478bd9Sstevel@tonic-gate return (-1);
24407c478bd9Sstevel@tonic-gate }
24417c478bd9Sstevel@tonic-gate }
24427c478bd9Sstevel@tonic-gate (void) zonecfg_endnwifent(handle);
24437c478bd9Sstevel@tonic-gate }
24447c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle);
2445fdb7141fSgfaden if (is_system_labeled()) {
2446fdb7141fSgfaden /*
2447fdb7141fSgfaden * Labeled zones share the loopback interface
2448fdb7141fSgfaden * so it is not plumbed for shared stack instances.
2449fdb7141fSgfaden */
2450fdb7141fSgfaden return (0);
2451fdb7141fSgfaden }
24527c478bd9Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_physical, "lo0",
24537c478bd9Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_physical));
24547c478bd9Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_address, "127.0.0.1",
24557c478bd9Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_address));
2456442d3e9eSgfaden loopback_iftab.zone_nwif_defrouter[0] = '\0';
2457f14f3ae7Sjv227347 if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
24587c478bd9Sstevel@tonic-gate return (-1);
2459f14f3ae7Sjv227347
2460f14f3ae7Sjv227347 /* Always plumb up the IPv6 loopback interface. */
24617c478bd9Sstevel@tonic-gate (void) strlcpy(loopback_iftab.zone_nwif_address, "::1/128",
24627c478bd9Sstevel@tonic-gate sizeof (loopback_iftab.zone_nwif_address));
2463f14f3ae7Sjv227347 if (configure_one_interface(zlogp, zoneid, &loopback_iftab) != Z_OK)
24647c478bd9Sstevel@tonic-gate return (-1);
24657c478bd9Sstevel@tonic-gate return (0);
24667c478bd9Sstevel@tonic-gate }
24677c478bd9Sstevel@tonic-gate
246889b1c373Smeem static void
zdlerror(zlog_t * zlogp,dladm_status_t err,const char * dlname,const char * str)246989b1c373Smeem zdlerror(zlog_t *zlogp, dladm_status_t err, const char *dlname, const char *str)
247089b1c373Smeem {
247189b1c373Smeem char errmsg[DLADM_STRSIZE];
247289b1c373Smeem
247389b1c373Smeem (void) dladm_status2str(err, errmsg);
247489b1c373Smeem zerror(zlogp, B_FALSE, "%s '%s': %s", str, dlname, errmsg);
247589b1c373Smeem }
247689b1c373Smeem
2477f4b3ec61Sdh155122 static int
add_datalink(zlog_t * zlogp,char * zone_name,datalink_id_t linkid,char * dlname)24782b24ab6bSSebastien Roy add_datalink(zlog_t *zlogp, char *zone_name, datalink_id_t linkid, char *dlname)
2479f4b3ec61Sdh155122 {
248089b1c373Smeem dladm_status_t err;
24810dc2366fSVenugopal Iyer boolean_t cpuset, poolset;
2482efd4c9b6SSteve Lawrence char *poolp;
248389b1c373Smeem
2484f4b3ec61Sdh155122 /* First check if it's in use by global zone. */
2485f4b3ec61Sdh155122 if (zonecfg_ifname_exists(AF_INET, dlname) ||
2486f4b3ec61Sdh155122 zonecfg_ifname_exists(AF_INET6, dlname)) {
248789b1c373Smeem zerror(zlogp, B_FALSE, "WARNING: skipping network interface "
248889b1c373Smeem "'%s' which is used in the global zone", dlname);
2489f4b3ec61Sdh155122 return (-1);
2490f4b3ec61Sdh155122 }
2491f4b3ec61Sdh155122
2492d62bc4baSyz147064 /* Set zoneid of this link. */
24932b24ab6bSSebastien Roy err = dladm_set_linkprop(dld_handle, linkid, "zone", &zone_name, 1,
24942b24ab6bSSebastien Roy DLADM_OPT_ACTIVE);
249589b1c373Smeem if (err != DLADM_STATUS_OK) {
249689b1c373Smeem zdlerror(zlogp, err, dlname,
249789b1c373Smeem "WARNING: unable to add network interface");
2498f4b3ec61Sdh155122 return (-1);
2499f4b3ec61Sdh155122 }
25000dc2366fSVenugopal Iyer
25010dc2366fSVenugopal Iyer /*
25020dc2366fSVenugopal Iyer * Set the pool of this link if the zone has a pool and
25030dc2366fSVenugopal Iyer * neither the cpus nor the pool datalink property is
25040dc2366fSVenugopal Iyer * already set.
25050dc2366fSVenugopal Iyer */
25060dc2366fSVenugopal Iyer err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
25070dc2366fSVenugopal Iyer "cpus", &cpuset);
25080dc2366fSVenugopal Iyer if (err != DLADM_STATUS_OK) {
25090dc2366fSVenugopal Iyer zdlerror(zlogp, err, dlname,
25100dc2366fSVenugopal Iyer "WARNING: unable to check if cpus link property is set");
25110dc2366fSVenugopal Iyer }
25120dc2366fSVenugopal Iyer err = dladm_linkprop_is_set(dld_handle, linkid, DLADM_PROP_VAL_CURRENT,
25130dc2366fSVenugopal Iyer "pool", &poolset);
25140dc2366fSVenugopal Iyer if (err != DLADM_STATUS_OK) {
25150dc2366fSVenugopal Iyer zdlerror(zlogp, err, dlname,
25160dc2366fSVenugopal Iyer "WARNING: unable to check if pool link property is set");
25170dc2366fSVenugopal Iyer }
25180dc2366fSVenugopal Iyer
25190dc2366fSVenugopal Iyer if ((strlen(pool_name) != 0) && !cpuset && !poolset) {
2520efd4c9b6SSteve Lawrence poolp = pool_name;
25210dc2366fSVenugopal Iyer err = dladm_set_linkprop(dld_handle, linkid, "pool",
2522efd4c9b6SSteve Lawrence &poolp, 1, DLADM_OPT_ACTIVE);
25230dc2366fSVenugopal Iyer if (err != DLADM_STATUS_OK) {
25240dc2366fSVenugopal Iyer zerror(zlogp, B_FALSE, "WARNING: unable to set "
25250dc2366fSVenugopal Iyer "pool %s to datalink %s", pool_name, dlname);
2526a3002e0aSGarrett D'Amore bzero(pool_name, sizeof (pool_name));
25270dc2366fSVenugopal Iyer }
25280dc2366fSVenugopal Iyer } else {
2529a3002e0aSGarrett D'Amore bzero(pool_name, sizeof (pool_name));
25300dc2366fSVenugopal Iyer }
2531f4b3ec61Sdh155122 return (0);
2532f4b3ec61Sdh155122 }
2533f4b3ec61Sdh155122
2534550b6e40SSowmini Varadhan static boolean_t
sockaddr_to_str(sa_family_t af,const struct sockaddr * sockaddr,char * straddr,size_t len)2535550b6e40SSowmini Varadhan sockaddr_to_str(sa_family_t af, const struct sockaddr *sockaddr,
2536550b6e40SSowmini Varadhan char *straddr, size_t len)
2537550b6e40SSowmini Varadhan {
2538550b6e40SSowmini Varadhan struct sockaddr_in *sin;
2539550b6e40SSowmini Varadhan struct sockaddr_in6 *sin6;
2540550b6e40SSowmini Varadhan const char *str = NULL;
2541550b6e40SSowmini Varadhan
2542550b6e40SSowmini Varadhan if (af == AF_INET) {
2543550b6e40SSowmini Varadhan /* LINTED E_BAD_PTR_CAST_ALIGN */
2544550b6e40SSowmini Varadhan sin = SIN(sockaddr);
2545550b6e40SSowmini Varadhan str = inet_ntop(AF_INET, (void *)&sin->sin_addr, straddr, len);
2546550b6e40SSowmini Varadhan } else if (af == AF_INET6) {
2547550b6e40SSowmini Varadhan /* LINTED E_BAD_PTR_CAST_ALIGN */
2548550b6e40SSowmini Varadhan sin6 = SIN6(sockaddr);
2549550b6e40SSowmini Varadhan str = inet_ntop(AF_INET6, (void *)&sin6->sin6_addr, straddr,
2550550b6e40SSowmini Varadhan len);
2551550b6e40SSowmini Varadhan }
2552550b6e40SSowmini Varadhan
2553550b6e40SSowmini Varadhan return (str != NULL);
2554550b6e40SSowmini Varadhan }
2555550b6e40SSowmini Varadhan
2556550b6e40SSowmini Varadhan static int
ipv4_prefixlen(struct sockaddr_in * sin)2557550b6e40SSowmini Varadhan ipv4_prefixlen(struct sockaddr_in *sin)
2558550b6e40SSowmini Varadhan {
2559550b6e40SSowmini Varadhan struct sockaddr_in *m;
2560550b6e40SSowmini Varadhan struct sockaddr_storage mask;
2561550b6e40SSowmini Varadhan
2562550b6e40SSowmini Varadhan m = SIN(&mask);
2563550b6e40SSowmini Varadhan m->sin_family = AF_INET;
2564550b6e40SSowmini Varadhan if (getnetmaskbyaddr(sin->sin_addr, &m->sin_addr) == 0) {
256564639aafSDarren Reed return (mask2plen((struct sockaddr *)&mask));
2566550b6e40SSowmini Varadhan } else if (IN_CLASSA(htonl(sin->sin_addr.s_addr))) {
2567550b6e40SSowmini Varadhan return (8);
2568550b6e40SSowmini Varadhan } else if (IN_CLASSB(ntohl(sin->sin_addr.s_addr))) {
2569550b6e40SSowmini Varadhan return (16);
2570550b6e40SSowmini Varadhan } else if (IN_CLASSC(ntohl(sin->sin_addr.s_addr))) {
2571550b6e40SSowmini Varadhan return (24);
2572550b6e40SSowmini Varadhan }
2573550b6e40SSowmini Varadhan return (0);
2574550b6e40SSowmini Varadhan }
2575550b6e40SSowmini Varadhan
2576550b6e40SSowmini Varadhan static int
zone_setattr_network(int type,zoneid_t zoneid,datalink_id_t linkid,void * buf,size_t bufsize)2577550b6e40SSowmini Varadhan zone_setattr_network(int type, zoneid_t zoneid, datalink_id_t linkid,
2578550b6e40SSowmini Varadhan void *buf, size_t bufsize)
2579550b6e40SSowmini Varadhan {
2580550b6e40SSowmini Varadhan zone_net_data_t *zndata;
2581550b6e40SSowmini Varadhan size_t znsize;
2582550b6e40SSowmini Varadhan int err;
2583550b6e40SSowmini Varadhan
2584550b6e40SSowmini Varadhan znsize = sizeof (*zndata) + bufsize;
2585550b6e40SSowmini Varadhan zndata = calloc(1, znsize);
2586550b6e40SSowmini Varadhan if (zndata == NULL)
2587550b6e40SSowmini Varadhan return (ENOMEM);
2588550b6e40SSowmini Varadhan zndata->zn_type = type;
2589550b6e40SSowmini Varadhan zndata->zn_len = bufsize;
2590550b6e40SSowmini Varadhan zndata->zn_linkid = linkid;
2591550b6e40SSowmini Varadhan bcopy(buf, zndata->zn_val, zndata->zn_len);
2592550b6e40SSowmini Varadhan err = zone_setattr(zoneid, ZONE_ATTR_NETWORK, zndata, znsize);
2593550b6e40SSowmini Varadhan free(zndata);
2594550b6e40SSowmini Varadhan return (err);
2595550b6e40SSowmini Varadhan }
2596550b6e40SSowmini Varadhan
2597550b6e40SSowmini Varadhan static int
add_net_for_linkid(zlog_t * zlogp,zoneid_t zoneid,zone_addr_list_t * start)2598550b6e40SSowmini Varadhan add_net_for_linkid(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *start)
2599550b6e40SSowmini Varadhan {
2600550b6e40SSowmini Varadhan struct lifreq lifr;
2601550b6e40SSowmini Varadhan char **astr, *address;
2602550b6e40SSowmini Varadhan dladm_status_t dlstatus;
2603550b6e40SSowmini Varadhan char *ip_nospoof = "ip-nospoof";
2604550b6e40SSowmini Varadhan int nnet, naddr, err = 0, j;
2605550b6e40SSowmini Varadhan size_t zlen, cpleft;
2606550b6e40SSowmini Varadhan zone_addr_list_t *ptr, *end;
2607550b6e40SSowmini Varadhan char tmp[INET6_ADDRSTRLEN], *maskstr;
2608550b6e40SSowmini Varadhan char *zaddr, *cp;
2609550b6e40SSowmini Varadhan struct in6_addr *routes = NULL;
2610550b6e40SSowmini Varadhan boolean_t is_set;
2611550b6e40SSowmini Varadhan datalink_id_t linkid;
2612550b6e40SSowmini Varadhan
2613550b6e40SSowmini Varadhan assert(start != NULL);
2614550b6e40SSowmini Varadhan naddr = 0; /* number of addresses */
2615550b6e40SSowmini Varadhan nnet = 0; /* number of net resources */
2616550b6e40SSowmini Varadhan linkid = start->za_linkid;
2617550b6e40SSowmini Varadhan for (ptr = start; ptr != NULL && ptr->za_linkid == linkid;
2618550b6e40SSowmini Varadhan ptr = ptr->za_next) {
2619550b6e40SSowmini Varadhan nnet++;
2620550b6e40SSowmini Varadhan }
2621550b6e40SSowmini Varadhan end = ptr;
2622550b6e40SSowmini Varadhan zlen = nnet * (INET6_ADDRSTRLEN + 1);
2623550b6e40SSowmini Varadhan astr = calloc(1, nnet * sizeof (uintptr_t));
2624550b6e40SSowmini Varadhan zaddr = calloc(1, zlen);
2625550b6e40SSowmini Varadhan if (astr == NULL || zaddr == NULL) {
2626550b6e40SSowmini Varadhan err = ENOMEM;
2627550b6e40SSowmini Varadhan goto done;
2628550b6e40SSowmini Varadhan }
2629550b6e40SSowmini Varadhan cp = zaddr;
2630550b6e40SSowmini Varadhan cpleft = zlen;
2631550b6e40SSowmini Varadhan j = 0;
2632550b6e40SSowmini Varadhan for (ptr = start; ptr != end; ptr = ptr->za_next) {
2633550b6e40SSowmini Varadhan address = ptr->za_nwiftab.zone_nwif_allowed_address;
2634550b6e40SSowmini Varadhan if (address[0] == '\0')
2635550b6e40SSowmini Varadhan continue;
2636550b6e40SSowmini Varadhan (void) snprintf(tmp, sizeof (tmp), "%s", address);
2637550b6e40SSowmini Varadhan /*
2638550b6e40SSowmini Varadhan * Validate the data. zonecfg_valid_net_address() clobbers
2639550b6e40SSowmini Varadhan * the /<mask> in the address string.
2640550b6e40SSowmini Varadhan */
2641550b6e40SSowmini Varadhan if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2642550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "invalid address [%s]\n",
2643550b6e40SSowmini Varadhan address);
2644550b6e40SSowmini Varadhan err = EINVAL;
2645550b6e40SSowmini Varadhan goto done;
2646550b6e40SSowmini Varadhan }
2647550b6e40SSowmini Varadhan /*
2648550b6e40SSowmini Varadhan * convert any hostnames to numeric address strings.
2649550b6e40SSowmini Varadhan */
2650550b6e40SSowmini Varadhan if (!sockaddr_to_str(lifr.lifr_addr.ss_family,
2651550b6e40SSowmini Varadhan (const struct sockaddr *)&lifr.lifr_addr, cp, cpleft)) {
2652550b6e40SSowmini Varadhan err = EINVAL;
2653550b6e40SSowmini Varadhan goto done;
2654550b6e40SSowmini Varadhan }
2655550b6e40SSowmini Varadhan /*
2656550b6e40SSowmini Varadhan * make a copy of the numeric string for the data needed
2657550b6e40SSowmini Varadhan * by the "allowed-ips" datalink property.
2658550b6e40SSowmini Varadhan */
2659550b6e40SSowmini Varadhan astr[j] = strdup(cp);
2660550b6e40SSowmini Varadhan if (astr[j] == NULL) {
2661550b6e40SSowmini Varadhan err = ENOMEM;
2662550b6e40SSowmini Varadhan goto done;
2663550b6e40SSowmini Varadhan }
2664550b6e40SSowmini Varadhan j++;
2665550b6e40SSowmini Varadhan /*
2666550b6e40SSowmini Varadhan * compute the default netmask from the address, if necessary
2667550b6e40SSowmini Varadhan */
2668550b6e40SSowmini Varadhan if ((maskstr = strchr(tmp, '/')) == NULL) {
2669550b6e40SSowmini Varadhan int prefixlen;
2670550b6e40SSowmini Varadhan
2671550b6e40SSowmini Varadhan if (lifr.lifr_addr.ss_family == AF_INET) {
2672550b6e40SSowmini Varadhan prefixlen = ipv4_prefixlen(
2673550b6e40SSowmini Varadhan SIN(&lifr.lifr_addr));
2674550b6e40SSowmini Varadhan } else {
2675550b6e40SSowmini Varadhan struct sockaddr_in6 *sin6;
2676550b6e40SSowmini Varadhan
2677550b6e40SSowmini Varadhan sin6 = SIN6(&lifr.lifr_addr);
2678550b6e40SSowmini Varadhan if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
2679550b6e40SSowmini Varadhan prefixlen = 10;
2680550b6e40SSowmini Varadhan else
2681550b6e40SSowmini Varadhan prefixlen = 64;
2682550b6e40SSowmini Varadhan }
2683550b6e40SSowmini Varadhan (void) snprintf(tmp, sizeof (tmp), "%d", prefixlen);
2684550b6e40SSowmini Varadhan maskstr = tmp;
2685550b6e40SSowmini Varadhan } else {
2686550b6e40SSowmini Varadhan maskstr++;
2687550b6e40SSowmini Varadhan }
2688550b6e40SSowmini Varadhan /* append the "/<netmask>" */
2689550b6e40SSowmini Varadhan (void) strlcat(cp, "/", cpleft);
2690550b6e40SSowmini Varadhan (void) strlcat(cp, maskstr, cpleft);
2691550b6e40SSowmini Varadhan (void) strlcat(cp, ",", cpleft);
2692550b6e40SSowmini Varadhan cp += strnlen(cp, zlen);
2693550b6e40SSowmini Varadhan cpleft = &zaddr[INET6_ADDRSTRLEN] - cp;
2694550b6e40SSowmini Varadhan }
2695550b6e40SSowmini Varadhan naddr = j; /* the actual number of addresses in the net resource */
2696550b6e40SSowmini Varadhan assert(naddr <= nnet);
2697550b6e40SSowmini Varadhan
2698550b6e40SSowmini Varadhan /*
2699550b6e40SSowmini Varadhan * zonecfg has already verified that the defrouter property can only
2700550b6e40SSowmini Varadhan * be set if there is at least one address defined for the net resource.
2701550b6e40SSowmini Varadhan * If j is 0, there are no addresses defined, and therefore no routers
2702550b6e40SSowmini Varadhan * to configure, and we are done at that point.
2703550b6e40SSowmini Varadhan */
2704550b6e40SSowmini Varadhan if (j == 0)
2705550b6e40SSowmini Varadhan goto done;
2706550b6e40SSowmini Varadhan
2707550b6e40SSowmini Varadhan /* over-write last ',' with '\0' */
2708550b6e40SSowmini Varadhan zaddr[strnlen(zaddr, zlen) + 1] = '\0';
2709550b6e40SSowmini Varadhan
2710550b6e40SSowmini Varadhan /*
2711550b6e40SSowmini Varadhan * First make sure L3 protection is not already set on the link.
2712550b6e40SSowmini Varadhan */
2713550b6e40SSowmini Varadhan dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2714550b6e40SSowmini Varadhan "protection", &is_set);
2715550b6e40SSowmini Varadhan if (dlstatus != DLADM_STATUS_OK) {
2716550b6e40SSowmini Varadhan err = EINVAL;
2717550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "unable to check if protection is set");
2718550b6e40SSowmini Varadhan goto done;
2719550b6e40SSowmini Varadhan }
2720550b6e40SSowmini Varadhan if (is_set) {
2721550b6e40SSowmini Varadhan err = EINVAL;
2722550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "Protection is already set");
2723550b6e40SSowmini Varadhan goto done;
2724550b6e40SSowmini Varadhan }
2725550b6e40SSowmini Varadhan dlstatus = dladm_linkprop_is_set(dld_handle, linkid, DLADM_OPT_ACTIVE,
2726550b6e40SSowmini Varadhan "allowed-ips", &is_set);
2727550b6e40SSowmini Varadhan if (dlstatus != DLADM_STATUS_OK) {
2728550b6e40SSowmini Varadhan err = EINVAL;
2729550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "unable to check if allowed-ips is set");
2730550b6e40SSowmini Varadhan goto done;
2731550b6e40SSowmini Varadhan }
2732550b6e40SSowmini Varadhan if (is_set) {
2733550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "allowed-ips is already set");
2734550b6e40SSowmini Varadhan err = EINVAL;
2735550b6e40SSowmini Varadhan goto done;
2736550b6e40SSowmini Varadhan }
2737550b6e40SSowmini Varadhan
2738550b6e40SSowmini Varadhan /*
2739550b6e40SSowmini Varadhan * Enable ip-nospoof for the link, and add address to the allowed-ips
2740550b6e40SSowmini Varadhan * list.
2741550b6e40SSowmini Varadhan */
2742550b6e40SSowmini Varadhan dlstatus = dladm_set_linkprop(dld_handle, linkid, "protection",
2743550b6e40SSowmini Varadhan &ip_nospoof, 1, DLADM_OPT_ACTIVE);
2744550b6e40SSowmini Varadhan if (dlstatus != DLADM_STATUS_OK) {
2745550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "could not set protection\n");
2746550b6e40SSowmini Varadhan err = EINVAL;
2747550b6e40SSowmini Varadhan goto done;
2748550b6e40SSowmini Varadhan }
2749550b6e40SSowmini Varadhan dlstatus = dladm_set_linkprop(dld_handle, linkid, "allowed-ips",
2750550b6e40SSowmini Varadhan astr, naddr, DLADM_OPT_ACTIVE);
2751550b6e40SSowmini Varadhan if (dlstatus != DLADM_STATUS_OK) {
2752550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "could not set allowed-ips\n");
2753550b6e40SSowmini Varadhan err = EINVAL;
2754550b6e40SSowmini Varadhan goto done;
2755550b6e40SSowmini Varadhan }
2756550b6e40SSowmini Varadhan
2757550b6e40SSowmini Varadhan /* now set the address in the data-store */
2758550b6e40SSowmini Varadhan err = zone_setattr_network(ZONE_NETWORK_ADDRESS, zoneid, linkid,
2759550b6e40SSowmini Varadhan zaddr, strnlen(zaddr, zlen) + 1);
2760550b6e40SSowmini Varadhan if (err != 0)
2761550b6e40SSowmini Varadhan goto done;
2762550b6e40SSowmini Varadhan
2763550b6e40SSowmini Varadhan /*
2764550b6e40SSowmini Varadhan * add the defaultrouters
2765550b6e40SSowmini Varadhan */
2766550b6e40SSowmini Varadhan routes = calloc(1, nnet * sizeof (*routes));
2767550b6e40SSowmini Varadhan j = 0;
2768550b6e40SSowmini Varadhan for (ptr = start; ptr != end; ptr = ptr->za_next) {
2769550b6e40SSowmini Varadhan address = ptr->za_nwiftab.zone_nwif_defrouter;
2770550b6e40SSowmini Varadhan if (address[0] == '\0')
2771550b6e40SSowmini Varadhan continue;
2772550b6e40SSowmini Varadhan if (strchr(address, '/') == NULL && strchr(address, ':') != 0) {
2773550b6e40SSowmini Varadhan /*
2774550b6e40SSowmini Varadhan * zonecfg_valid_net_address() expects numeric IPv6
2775550b6e40SSowmini Varadhan * addresses to have a CIDR format netmask.
2776550b6e40SSowmini Varadhan */
2777550b6e40SSowmini Varadhan (void) snprintf(tmp, sizeof (tmp), "/%d", V6_ADDR_LEN);
2778550b6e40SSowmini Varadhan (void) strlcat(address, tmp, INET6_ADDRSTRLEN);
2779550b6e40SSowmini Varadhan }
2780550b6e40SSowmini Varadhan if (zonecfg_valid_net_address(address, &lifr) != Z_OK) {
2781550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE,
2782550b6e40SSowmini Varadhan "invalid router [%s]\n", address);
2783550b6e40SSowmini Varadhan err = EINVAL;
2784550b6e40SSowmini Varadhan goto done;
2785550b6e40SSowmini Varadhan }
2786550b6e40SSowmini Varadhan if (lifr.lifr_addr.ss_family == AF_INET6) {
2787550b6e40SSowmini Varadhan routes[j] = SIN6(&lifr.lifr_addr)->sin6_addr;
2788550b6e40SSowmini Varadhan } else {
2789550b6e40SSowmini Varadhan IN6_INADDR_TO_V4MAPPED(&SIN(&lifr.lifr_addr)->sin_addr,
2790550b6e40SSowmini Varadhan &routes[j]);
2791550b6e40SSowmini Varadhan }
2792550b6e40SSowmini Varadhan j++;
2793550b6e40SSowmini Varadhan }
2794550b6e40SSowmini Varadhan assert(j <= nnet);
2795550b6e40SSowmini Varadhan if (j > 0) {
2796550b6e40SSowmini Varadhan err = zone_setattr_network(ZONE_NETWORK_DEFROUTER, zoneid,
2797550b6e40SSowmini Varadhan linkid, routes, j * sizeof (*routes));
2798550b6e40SSowmini Varadhan }
2799550b6e40SSowmini Varadhan done:
2800550b6e40SSowmini Varadhan free(routes);
2801550b6e40SSowmini Varadhan for (j = 0; j < naddr; j++)
2802550b6e40SSowmini Varadhan free(astr[j]);
2803550b6e40SSowmini Varadhan free(astr);
2804550b6e40SSowmini Varadhan free(zaddr);
2805550b6e40SSowmini Varadhan return (err);
2806550b6e40SSowmini Varadhan
2807550b6e40SSowmini Varadhan }
2808550b6e40SSowmini Varadhan
2809550b6e40SSowmini Varadhan static int
add_net(zlog_t * zlogp,zoneid_t zoneid,zone_addr_list_t * zalist)2810550b6e40SSowmini Varadhan add_net(zlog_t *zlogp, zoneid_t zoneid, zone_addr_list_t *zalist)
2811550b6e40SSowmini Varadhan {
2812550b6e40SSowmini Varadhan zone_addr_list_t *ptr;
2813550b6e40SSowmini Varadhan datalink_id_t linkid;
2814550b6e40SSowmini Varadhan int err;
2815550b6e40SSowmini Varadhan
2816550b6e40SSowmini Varadhan if (zalist == NULL)
2817550b6e40SSowmini Varadhan return (0);
2818550b6e40SSowmini Varadhan
2819550b6e40SSowmini Varadhan linkid = zalist->za_linkid;
2820550b6e40SSowmini Varadhan
2821550b6e40SSowmini Varadhan err = add_net_for_linkid(zlogp, zoneid, zalist);
2822550b6e40SSowmini Varadhan if (err != 0)
2823550b6e40SSowmini Varadhan return (err);
2824550b6e40SSowmini Varadhan
2825550b6e40SSowmini Varadhan for (ptr = zalist; ptr != NULL; ptr = ptr->za_next) {
2826550b6e40SSowmini Varadhan if (ptr->za_linkid == linkid)
2827550b6e40SSowmini Varadhan continue;
2828550b6e40SSowmini Varadhan linkid = ptr->za_linkid;
2829550b6e40SSowmini Varadhan err = add_net_for_linkid(zlogp, zoneid, ptr);
2830550b6e40SSowmini Varadhan if (err != 0)
2831550b6e40SSowmini Varadhan return (err);
2832550b6e40SSowmini Varadhan }
2833550b6e40SSowmini Varadhan return (0);
2834550b6e40SSowmini Varadhan }
2835550b6e40SSowmini Varadhan
2836550b6e40SSowmini Varadhan /*
2837550b6e40SSowmini Varadhan * Add "new" to the list of network interfaces to be configured by
2838550b6e40SSowmini Varadhan * add_net on zone boot in "old". The list of interfaces in "old" is
2839550b6e40SSowmini Varadhan * sorted by datalink_id_t, with interfaces sorted FIFO for a given
2840550b6e40SSowmini Varadhan * datalink_id_t.
2841550b6e40SSowmini Varadhan *
2842550b6e40SSowmini Varadhan * Returns the merged list of IP interfaces containing "old" and "new"
2843550b6e40SSowmini Varadhan */
2844550b6e40SSowmini Varadhan static zone_addr_list_t *
add_ip_interface(zone_addr_list_t * old,zone_addr_list_t * new)2845550b6e40SSowmini Varadhan add_ip_interface(zone_addr_list_t *old, zone_addr_list_t *new)
2846550b6e40SSowmini Varadhan {
2847550b6e40SSowmini Varadhan zone_addr_list_t *ptr, *next;
2848550b6e40SSowmini Varadhan datalink_id_t linkid = new->za_linkid;
2849550b6e40SSowmini Varadhan
2850550b6e40SSowmini Varadhan assert(old != new);
2851550b6e40SSowmini Varadhan
2852550b6e40SSowmini Varadhan if (old == NULL)
2853550b6e40SSowmini Varadhan return (new);
2854550b6e40SSowmini Varadhan for (ptr = old; ptr != NULL; ptr = ptr->za_next) {
2855550b6e40SSowmini Varadhan if (ptr->za_linkid == linkid)
2856550b6e40SSowmini Varadhan break;
2857550b6e40SSowmini Varadhan }
2858550b6e40SSowmini Varadhan if (ptr == NULL) {
2859550b6e40SSowmini Varadhan /* linkid does not already exist, add to the beginning */
2860550b6e40SSowmini Varadhan new->za_next = old;
2861550b6e40SSowmini Varadhan return (new);
2862550b6e40SSowmini Varadhan }
2863550b6e40SSowmini Varadhan /*
2864550b6e40SSowmini Varadhan * adding to the middle of the list; ptr points at the first
2865550b6e40SSowmini Varadhan * occurrence of linkid. Find the last occurrence.
2866550b6e40SSowmini Varadhan */
2867550b6e40SSowmini Varadhan while ((next = ptr->za_next) != NULL) {
2868550b6e40SSowmini Varadhan if (next->za_linkid != linkid)
2869550b6e40SSowmini Varadhan break;
2870550b6e40SSowmini Varadhan ptr = next;
2871550b6e40SSowmini Varadhan }
2872550b6e40SSowmini Varadhan /* insert new after ptr */
2873550b6e40SSowmini Varadhan new->za_next = next;
2874550b6e40SSowmini Varadhan ptr->za_next = new;
2875550b6e40SSowmini Varadhan return (old);
2876550b6e40SSowmini Varadhan }
2877550b6e40SSowmini Varadhan
2878550b6e40SSowmini Varadhan void
free_ip_interface(zone_addr_list_t * zalist)2879550b6e40SSowmini Varadhan free_ip_interface(zone_addr_list_t *zalist)
2880550b6e40SSowmini Varadhan {
2881550b6e40SSowmini Varadhan zone_addr_list_t *ptr, *new;
2882550b6e40SSowmini Varadhan
2883550b6e40SSowmini Varadhan for (ptr = zalist; ptr != NULL; ) {
2884550b6e40SSowmini Varadhan new = ptr;
2885550b6e40SSowmini Varadhan ptr = ptr->za_next;
2886550b6e40SSowmini Varadhan free(new);
2887550b6e40SSowmini Varadhan }
2888550b6e40SSowmini Varadhan }
2889550b6e40SSowmini Varadhan
2890f4b3ec61Sdh155122 /*
2891f4b3ec61Sdh155122 * Add the kernel access control information for the interface names.
2892f4b3ec61Sdh155122 * If anything goes wrong, we log a general error message, attempt to tear down
2893f4b3ec61Sdh155122 * whatever we set up, and return an error.
2894f4b3ec61Sdh155122 */
2895f4b3ec61Sdh155122 static int
configure_exclusive_network_interfaces(zlog_t * zlogp,zoneid_t zoneid)2896550b6e40SSowmini Varadhan configure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
2897f4b3ec61Sdh155122 {
2898f4b3ec61Sdh155122 zone_dochandle_t handle;
2899f4b3ec61Sdh155122 struct zone_nwiftab nwiftab;
2900f4b3ec61Sdh155122 char rootpath[MAXPATHLEN];
2901f4b3ec61Sdh155122 char path[MAXPATHLEN];
29022b24ab6bSSebastien Roy datalink_id_t linkid;
2903f4b3ec61Sdh155122 di_prof_t prof = NULL;
2904f4b3ec61Sdh155122 boolean_t added = B_FALSE;
2905550b6e40SSowmini Varadhan zone_addr_list_t *zalist = NULL, *new;
2906f4b3ec61Sdh155122
2907f4b3ec61Sdh155122 if ((handle = zonecfg_init_handle()) == NULL) {
2908f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "getting zone configuration handle");
2909f4b3ec61Sdh155122 return (-1);
2910f4b3ec61Sdh155122 }
2911f4b3ec61Sdh155122 if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
2912f4b3ec61Sdh155122 zerror(zlogp, B_FALSE, "invalid configuration");
2913f4b3ec61Sdh155122 zonecfg_fini_handle(handle);
2914f4b3ec61Sdh155122 return (-1);
2915f4b3ec61Sdh155122 }
2916f4b3ec61Sdh155122
2917f4b3ec61Sdh155122 if (zonecfg_setnwifent(handle) != Z_OK) {
2918f4b3ec61Sdh155122 zonecfg_fini_handle(handle);
2919f4b3ec61Sdh155122 return (0);
2920f4b3ec61Sdh155122 }
2921f4b3ec61Sdh155122
2922f4b3ec61Sdh155122 for (;;) {
2923f4b3ec61Sdh155122 if (zonecfg_getnwifent(handle, &nwiftab) != Z_OK)
2924f4b3ec61Sdh155122 break;
2925f4b3ec61Sdh155122
2926f4b3ec61Sdh155122 if (prof == NULL) {
2927f4b3ec61Sdh155122 if (zone_get_devroot(zone_name, rootpath,
2928f4b3ec61Sdh155122 sizeof (rootpath)) != Z_OK) {
2929f4b3ec61Sdh155122 (void) zonecfg_endnwifent(handle);
2930f4b3ec61Sdh155122 zonecfg_fini_handle(handle);
2931f4b3ec61Sdh155122 zerror(zlogp, B_TRUE,
2932f4b3ec61Sdh155122 "unable to determine dev root");
2933f4b3ec61Sdh155122 return (-1);
2934f4b3ec61Sdh155122 }
2935f4b3ec61Sdh155122 (void) snprintf(path, sizeof (path), "%s%s", rootpath,
2936f4b3ec61Sdh155122 "/dev");
2937f4b3ec61Sdh155122 if (di_prof_init(path, &prof) != 0) {
2938f4b3ec61Sdh155122 (void) zonecfg_endnwifent(handle);
2939f4b3ec61Sdh155122 zonecfg_fini_handle(handle);
2940f4b3ec61Sdh155122 zerror(zlogp, B_TRUE,
2941f4b3ec61Sdh155122 "failed to initialize profile");
2942f4b3ec61Sdh155122 return (-1);
2943f4b3ec61Sdh155122 }
2944f4b3ec61Sdh155122 }
2945f4b3ec61Sdh155122
2946f4b3ec61Sdh155122 /*
2947d62bc4baSyz147064 * Create the /dev entry for backward compatibility.
2948f4b3ec61Sdh155122 * Only create the /dev entry if it's not in use.
2949d62bc4baSyz147064 * Note that the zone still boots when the assigned
2950d62bc4baSyz147064 * interface is inaccessible, used by others, etc.
2951d62bc4baSyz147064 * Also, when vanity naming is used, some interface do
2952d62bc4baSyz147064 * do not have corresponding /dev node names (for example,
2953d62bc4baSyz147064 * vanity named aggregations). The /dev entry is not
2954d62bc4baSyz147064 * created in that case. The /dev/net entry is always
2955d62bc4baSyz147064 * accessible.
2956f4b3ec61Sdh155122 */
29572b24ab6bSSebastien Roy if (dladm_name2info(dld_handle, nwiftab.zone_nwif_physical,
29582b24ab6bSSebastien Roy &linkid, NULL, NULL, NULL) == DLADM_STATUS_OK &&
29592b24ab6bSSebastien Roy add_datalink(zlogp, zone_name, linkid,
29602b24ab6bSSebastien Roy nwiftab.zone_nwif_physical) == 0) {
29613bc21d0aSAruna Ramakrishna - Sun Microsystems added = B_TRUE;
29623bc21d0aSAruna Ramakrishna - Sun Microsystems } else {
2963f4b3ec61Sdh155122 (void) zonecfg_endnwifent(handle);
2964f4b3ec61Sdh155122 zonecfg_fini_handle(handle);
29653bc21d0aSAruna Ramakrishna - Sun Microsystems zerror(zlogp, B_TRUE, "failed to add network device");
2966f4b3ec61Sdh155122 return (-1);
2967f4b3ec61Sdh155122 }
2968550b6e40SSowmini Varadhan /* set up the new IP interface, and add them all later */
2969550b6e40SSowmini Varadhan new = malloc(sizeof (*new));
2970550b6e40SSowmini Varadhan if (new == NULL) {
2971550b6e40SSowmini Varadhan zerror(zlogp, B_TRUE, "no memory for %s",
2972550b6e40SSowmini Varadhan nwiftab.zone_nwif_physical);
2973550b6e40SSowmini Varadhan zonecfg_fini_handle(handle);
2974550b6e40SSowmini Varadhan free_ip_interface(zalist);
2975550b6e40SSowmini Varadhan }
2976550b6e40SSowmini Varadhan bzero(new, sizeof (*new));
2977550b6e40SSowmini Varadhan new->za_nwiftab = nwiftab;
2978550b6e40SSowmini Varadhan new->za_linkid = linkid;
2979550b6e40SSowmini Varadhan zalist = add_ip_interface(zalist, new);
2980550b6e40SSowmini Varadhan }
2981550b6e40SSowmini Varadhan if (zalist != NULL) {
2982550b6e40SSowmini Varadhan if ((errno = add_net(zlogp, zoneid, zalist)) != 0) {
2983550b6e40SSowmini Varadhan (void) zonecfg_endnwifent(handle);
2984550b6e40SSowmini Varadhan zonecfg_fini_handle(handle);
2985550b6e40SSowmini Varadhan zerror(zlogp, B_TRUE, "failed to add address");
2986550b6e40SSowmini Varadhan free_ip_interface(zalist);
2987550b6e40SSowmini Varadhan return (-1);
2988550b6e40SSowmini Varadhan }
2989550b6e40SSowmini Varadhan free_ip_interface(zalist);
2990d62bc4baSyz147064 }
2991f4b3ec61Sdh155122 (void) zonecfg_endnwifent(handle);
2992f4b3ec61Sdh155122 zonecfg_fini_handle(handle);
2993f4b3ec61Sdh155122
2994f4b3ec61Sdh155122 if (prof != NULL && added) {
2995f4b3ec61Sdh155122 if (di_prof_commit(prof) != 0) {
2996f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "failed to commit profile");
2997f4b3ec61Sdh155122 return (-1);
2998f4b3ec61Sdh155122 }
2999f4b3ec61Sdh155122 }
3000f4b3ec61Sdh155122 if (prof != NULL)
3001f4b3ec61Sdh155122 di_prof_fini(prof);
3002f4b3ec61Sdh155122
3003f4b3ec61Sdh155122 return (0);
3004f4b3ec61Sdh155122 }
3005f4b3ec61Sdh155122
3006f4b3ec61Sdh155122 static int
remove_datalink_pool(zlog_t * zlogp,zoneid_t zoneid)30070dc2366fSVenugopal Iyer remove_datalink_pool(zlog_t *zlogp, zoneid_t zoneid)
30080dc2366fSVenugopal Iyer {
30090dc2366fSVenugopal Iyer ushort_t flags;
30100dc2366fSVenugopal Iyer zone_iptype_t iptype;
30110dc2366fSVenugopal Iyer int i, dlnum = 0;
30120dc2366fSVenugopal Iyer datalink_id_t *dllink, *dllinks = NULL;
30130dc2366fSVenugopal Iyer dladm_status_t err;
30140dc2366fSVenugopal Iyer
30150dc2366fSVenugopal Iyer if (strlen(pool_name) == 0)
30160dc2366fSVenugopal Iyer return (0);
30170dc2366fSVenugopal Iyer
30180dc2366fSVenugopal Iyer if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
30190dc2366fSVenugopal Iyer sizeof (flags)) < 0) {
30200dc2366fSVenugopal Iyer if (vplat_get_iptype(zlogp, &iptype) < 0) {
3021550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "unable to determine ip-type");
30220dc2366fSVenugopal Iyer return (-1);
30230dc2366fSVenugopal Iyer }
30240dc2366fSVenugopal Iyer } else {
30250dc2366fSVenugopal Iyer if (flags & ZF_NET_EXCL)
30260dc2366fSVenugopal Iyer iptype = ZS_EXCLUSIVE;
30270dc2366fSVenugopal Iyer else
30280dc2366fSVenugopal Iyer iptype = ZS_SHARED;
30290dc2366fSVenugopal Iyer }
30300dc2366fSVenugopal Iyer
30310dc2366fSVenugopal Iyer if (iptype == ZS_EXCLUSIVE) {
30320dc2366fSVenugopal Iyer /*
30330dc2366fSVenugopal Iyer * Get the datalink count and for each datalink,
30340dc2366fSVenugopal Iyer * attempt to clear the pool property and clear
30350dc2366fSVenugopal Iyer * the pool_name.
30360dc2366fSVenugopal Iyer */
30370dc2366fSVenugopal Iyer if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
30380dc2366fSVenugopal Iyer zerror(zlogp, B_TRUE, "unable to count network "
30390dc2366fSVenugopal Iyer "interfaces");
30400dc2366fSVenugopal Iyer return (-1);
30410dc2366fSVenugopal Iyer }
30420dc2366fSVenugopal Iyer
30430dc2366fSVenugopal Iyer if (dlnum == 0)
30440dc2366fSVenugopal Iyer return (0);
30450dc2366fSVenugopal Iyer
30460dc2366fSVenugopal Iyer if ((dllinks = malloc(dlnum * sizeof (datalink_id_t)))
30470dc2366fSVenugopal Iyer == NULL) {
30480dc2366fSVenugopal Iyer zerror(zlogp, B_TRUE, "memory allocation failed");
30490dc2366fSVenugopal Iyer return (-1);
30500dc2366fSVenugopal Iyer }
30510dc2366fSVenugopal Iyer if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
30520dc2366fSVenugopal Iyer zerror(zlogp, B_TRUE, "unable to list network "
30530dc2366fSVenugopal Iyer "interfaces");
30540dc2366fSVenugopal Iyer return (-1);
30550dc2366fSVenugopal Iyer }
30560dc2366fSVenugopal Iyer
3057a3002e0aSGarrett D'Amore bzero(pool_name, sizeof (pool_name));
30580dc2366fSVenugopal Iyer for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
30590dc2366fSVenugopal Iyer err = dladm_set_linkprop(dld_handle, *dllink, "pool",
30600dc2366fSVenugopal Iyer NULL, 0, DLADM_OPT_ACTIVE);
30610dc2366fSVenugopal Iyer if (err != DLADM_STATUS_OK) {
30620dc2366fSVenugopal Iyer zerror(zlogp, B_TRUE,
30630dc2366fSVenugopal Iyer "WARNING: unable to clear pool");
30640dc2366fSVenugopal Iyer }
30650dc2366fSVenugopal Iyer }
30660dc2366fSVenugopal Iyer free(dllinks);
30670dc2366fSVenugopal Iyer }
30680dc2366fSVenugopal Iyer return (0);
30690dc2366fSVenugopal Iyer }
30700dc2366fSVenugopal Iyer
30710dc2366fSVenugopal Iyer static int
remove_datalink_protect(zlog_t * zlogp,zoneid_t zoneid)3072550b6e40SSowmini Varadhan remove_datalink_protect(zlog_t *zlogp, zoneid_t zoneid)
3073550b6e40SSowmini Varadhan {
3074550b6e40SSowmini Varadhan ushort_t flags;
3075550b6e40SSowmini Varadhan zone_iptype_t iptype;
3076550b6e40SSowmini Varadhan int i, dlnum = 0;
3077550b6e40SSowmini Varadhan dladm_status_t dlstatus;
3078550b6e40SSowmini Varadhan datalink_id_t *dllink, *dllinks = NULL;
3079550b6e40SSowmini Varadhan
3080550b6e40SSowmini Varadhan if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
3081550b6e40SSowmini Varadhan sizeof (flags)) < 0) {
3082550b6e40SSowmini Varadhan if (vplat_get_iptype(zlogp, &iptype) < 0) {
3083550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE, "unable to determine ip-type");
3084550b6e40SSowmini Varadhan return (-1);
3085550b6e40SSowmini Varadhan }
3086550b6e40SSowmini Varadhan } else {
3087550b6e40SSowmini Varadhan if (flags & ZF_NET_EXCL)
3088550b6e40SSowmini Varadhan iptype = ZS_EXCLUSIVE;
3089550b6e40SSowmini Varadhan else
3090550b6e40SSowmini Varadhan iptype = ZS_SHARED;
3091550b6e40SSowmini Varadhan }
3092550b6e40SSowmini Varadhan
3093550b6e40SSowmini Varadhan if (iptype != ZS_EXCLUSIVE)
3094550b6e40SSowmini Varadhan return (0);
3095550b6e40SSowmini Varadhan
3096550b6e40SSowmini Varadhan /*
3097550b6e40SSowmini Varadhan * Get the datalink count and for each datalink,
3098550b6e40SSowmini Varadhan * attempt to clear the pool property and clear
3099550b6e40SSowmini Varadhan * the pool_name.
3100550b6e40SSowmini Varadhan */
3101550b6e40SSowmini Varadhan if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3102550b6e40SSowmini Varadhan zerror(zlogp, B_TRUE, "unable to count network interfaces");
3103550b6e40SSowmini Varadhan return (-1);
3104550b6e40SSowmini Varadhan }
3105550b6e40SSowmini Varadhan
3106550b6e40SSowmini Varadhan if (dlnum == 0)
3107550b6e40SSowmini Varadhan return (0);
3108550b6e40SSowmini Varadhan
3109550b6e40SSowmini Varadhan if ((dllinks = malloc(dlnum * sizeof (datalink_id_t))) == NULL) {
3110550b6e40SSowmini Varadhan zerror(zlogp, B_TRUE, "memory allocation failed");
3111550b6e40SSowmini Varadhan return (-1);
3112550b6e40SSowmini Varadhan }
3113550b6e40SSowmini Varadhan if (zone_list_datalink(zoneid, &dlnum, dllinks) != 0) {
3114550b6e40SSowmini Varadhan zerror(zlogp, B_TRUE, "unable to list network interfaces");
3115550b6e40SSowmini Varadhan free(dllinks);
3116550b6e40SSowmini Varadhan return (-1);
3117550b6e40SSowmini Varadhan }
3118550b6e40SSowmini Varadhan
3119550b6e40SSowmini Varadhan for (i = 0, dllink = dllinks; i < dlnum; i++, dllink++) {
3120fff7ec1dSSowmini Varadhan char dlerr[DLADM_STRSIZE];
3121fff7ec1dSSowmini Varadhan
3122550b6e40SSowmini Varadhan dlstatus = dladm_set_linkprop(dld_handle, *dllink,
3123550b6e40SSowmini Varadhan "protection", NULL, 0, DLADM_OPT_ACTIVE);
3124fff7ec1dSSowmini Varadhan if (dlstatus == DLADM_STATUS_NOTFOUND) {
3125fff7ec1dSSowmini Varadhan /* datalink does not belong to the GZ */
3126fff7ec1dSSowmini Varadhan continue;
3127fff7ec1dSSowmini Varadhan }
3128550b6e40SSowmini Varadhan if (dlstatus != DLADM_STATUS_OK) {
3129fff7ec1dSSowmini Varadhan zerror(zlogp, B_FALSE,
3130fff7ec1dSSowmini Varadhan dladm_status2str(dlstatus, dlerr));
3131550b6e40SSowmini Varadhan free(dllinks);
3132550b6e40SSowmini Varadhan return (-1);
3133550b6e40SSowmini Varadhan }
3134550b6e40SSowmini Varadhan dlstatus = dladm_set_linkprop(dld_handle, *dllink,
3135550b6e40SSowmini Varadhan "allowed-ips", NULL, 0, DLADM_OPT_ACTIVE);
3136550b6e40SSowmini Varadhan if (dlstatus != DLADM_STATUS_OK) {
3137fff7ec1dSSowmini Varadhan zerror(zlogp, B_FALSE,
3138fff7ec1dSSowmini Varadhan dladm_status2str(dlstatus, dlerr));
3139550b6e40SSowmini Varadhan free(dllinks);
3140550b6e40SSowmini Varadhan return (-1);
3141550b6e40SSowmini Varadhan }
3142550b6e40SSowmini Varadhan }
3143550b6e40SSowmini Varadhan free(dllinks);
3144550b6e40SSowmini Varadhan return (0);
3145550b6e40SSowmini Varadhan }
3146550b6e40SSowmini Varadhan
3147550b6e40SSowmini Varadhan static int
unconfigure_exclusive_network_interfaces(zlog_t * zlogp,zoneid_t zoneid)31482b24ab6bSSebastien Roy unconfigure_exclusive_network_interfaces(zlog_t *zlogp, zoneid_t zoneid)
3149f4b3ec61Sdh155122 {
31502b24ab6bSSebastien Roy int dlnum = 0;
3151f4b3ec61Sdh155122
31522b24ab6bSSebastien Roy /*
31532b24ab6bSSebastien Roy * The kernel shutdown callback for the dls module should have removed
31542b24ab6bSSebastien Roy * all datalinks from this zone. If any remain, then there's a
31552b24ab6bSSebastien Roy * problem.
31562b24ab6bSSebastien Roy */
3157f4b3ec61Sdh155122 if (zone_list_datalink(zoneid, &dlnum, NULL) != 0) {
3158f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "unable to list network interfaces");
3159f4b3ec61Sdh155122 return (-1);
3160f4b3ec61Sdh155122 }
31612b24ab6bSSebastien Roy if (dlnum != 0) {
31622b24ab6bSSebastien Roy zerror(zlogp, B_FALSE,
31632b24ab6bSSebastien Roy "datalinks remain in zone after shutdown");
3164f4b3ec61Sdh155122 return (-1);
3165f4b3ec61Sdh155122 }
3166f4b3ec61Sdh155122 return (0);
3167f4b3ec61Sdh155122 }
3168f4b3ec61Sdh155122
31697c478bd9Sstevel@tonic-gate static int
tcp_abort_conn(zlog_t * zlogp,zoneid_t zoneid,const struct sockaddr_storage * local,const struct sockaddr_storage * remote)31707c478bd9Sstevel@tonic-gate tcp_abort_conn(zlog_t *zlogp, zoneid_t zoneid,
31717c478bd9Sstevel@tonic-gate const struct sockaddr_storage *local, const struct sockaddr_storage *remote)
31727c478bd9Sstevel@tonic-gate {
31737c478bd9Sstevel@tonic-gate int fd;
31747c478bd9Sstevel@tonic-gate struct strioctl ioc;
31757c478bd9Sstevel@tonic-gate tcp_ioc_abort_conn_t conn;
31767c478bd9Sstevel@tonic-gate int error;
31777c478bd9Sstevel@tonic-gate
31787c478bd9Sstevel@tonic-gate conn.ac_local = *local;
31797c478bd9Sstevel@tonic-gate conn.ac_remote = *remote;
31807c478bd9Sstevel@tonic-gate conn.ac_start = TCPS_SYN_SENT;
31817c478bd9Sstevel@tonic-gate conn.ac_end = TCPS_TIME_WAIT;
31827c478bd9Sstevel@tonic-gate conn.ac_zoneid = zoneid;
31837c478bd9Sstevel@tonic-gate
31847c478bd9Sstevel@tonic-gate ioc.ic_cmd = TCP_IOC_ABORT_CONN;
31857c478bd9Sstevel@tonic-gate ioc.ic_timout = -1; /* infinite timeout */
31867c478bd9Sstevel@tonic-gate ioc.ic_len = sizeof (conn);
31877c478bd9Sstevel@tonic-gate ioc.ic_dp = (char *)&conn;
31887c478bd9Sstevel@tonic-gate
31897c478bd9Sstevel@tonic-gate if ((fd = open("/dev/tcp", O_RDONLY)) < 0) {
31907c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to open %s", "/dev/tcp");
31917c478bd9Sstevel@tonic-gate return (-1);
31927c478bd9Sstevel@tonic-gate }
31937c478bd9Sstevel@tonic-gate
31947c478bd9Sstevel@tonic-gate error = ioctl(fd, I_STR, &ioc);
31957c478bd9Sstevel@tonic-gate (void) close(fd);
31967c478bd9Sstevel@tonic-gate if (error == 0 || errno == ENOENT) /* ENOENT is not an error */
31977c478bd9Sstevel@tonic-gate return (0);
31987c478bd9Sstevel@tonic-gate return (-1);
31997c478bd9Sstevel@tonic-gate }
32007c478bd9Sstevel@tonic-gate
32017c478bd9Sstevel@tonic-gate static int
tcp_abort_connections(zlog_t * zlogp,zoneid_t zoneid)32027c478bd9Sstevel@tonic-gate tcp_abort_connections(zlog_t *zlogp, zoneid_t zoneid)
32037c478bd9Sstevel@tonic-gate {
32047c478bd9Sstevel@tonic-gate struct sockaddr_storage l, r;
32057c478bd9Sstevel@tonic-gate struct sockaddr_in *local, *remote;
32067c478bd9Sstevel@tonic-gate struct sockaddr_in6 *local6, *remote6;
32077c478bd9Sstevel@tonic-gate int error;
32087c478bd9Sstevel@tonic-gate
32097c478bd9Sstevel@tonic-gate /*
32107c478bd9Sstevel@tonic-gate * Abort IPv4 connections.
32117c478bd9Sstevel@tonic-gate */
32127c478bd9Sstevel@tonic-gate bzero(&l, sizeof (*local));
32137c478bd9Sstevel@tonic-gate local = (struct sockaddr_in *)&l;
32147c478bd9Sstevel@tonic-gate local->sin_family = AF_INET;
32157c478bd9Sstevel@tonic-gate local->sin_addr.s_addr = INADDR_ANY;
32167c478bd9Sstevel@tonic-gate local->sin_port = 0;
32177c478bd9Sstevel@tonic-gate
32187c478bd9Sstevel@tonic-gate bzero(&r, sizeof (*remote));
32197c478bd9Sstevel@tonic-gate remote = (struct sockaddr_in *)&r;
32207c478bd9Sstevel@tonic-gate remote->sin_family = AF_INET;
32217c478bd9Sstevel@tonic-gate remote->sin_addr.s_addr = INADDR_ANY;
32227c478bd9Sstevel@tonic-gate remote->sin_port = 0;
32237c478bd9Sstevel@tonic-gate
32247c478bd9Sstevel@tonic-gate if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
32257c478bd9Sstevel@tonic-gate return (error);
32267c478bd9Sstevel@tonic-gate
32277c478bd9Sstevel@tonic-gate /*
32287c478bd9Sstevel@tonic-gate * Abort IPv6 connections.
32297c478bd9Sstevel@tonic-gate */
32307c478bd9Sstevel@tonic-gate bzero(&l, sizeof (*local6));
32317c478bd9Sstevel@tonic-gate local6 = (struct sockaddr_in6 *)&l;
32327c478bd9Sstevel@tonic-gate local6->sin6_family = AF_INET6;
32337c478bd9Sstevel@tonic-gate local6->sin6_port = 0;
32347c478bd9Sstevel@tonic-gate local6->sin6_addr = in6addr_any;
32357c478bd9Sstevel@tonic-gate
32367c478bd9Sstevel@tonic-gate bzero(&r, sizeof (*remote6));
32377c478bd9Sstevel@tonic-gate remote6 = (struct sockaddr_in6 *)&r;
32387c478bd9Sstevel@tonic-gate remote6->sin6_family = AF_INET6;
32397c478bd9Sstevel@tonic-gate remote6->sin6_port = 0;
32407c478bd9Sstevel@tonic-gate remote6->sin6_addr = in6addr_any;
32417c478bd9Sstevel@tonic-gate
32427c478bd9Sstevel@tonic-gate if ((error = tcp_abort_conn(zlogp, zoneid, &l, &r)) != 0)
32437c478bd9Sstevel@tonic-gate return (error);
32447c478bd9Sstevel@tonic-gate return (0);
32457c478bd9Sstevel@tonic-gate }
32467c478bd9Sstevel@tonic-gate
32477c478bd9Sstevel@tonic-gate static int
get_privset(zlog_t * zlogp,priv_set_t * privs,zone_mnt_t mount_cmd)32486cfd72c6Sgjelinek get_privset(zlog_t *zlogp, priv_set_t *privs, zone_mnt_t mount_cmd)
3249ffbafc53Scomay {
3250ffbafc53Scomay int error = -1;
3251ffbafc53Scomay zone_dochandle_t handle;
3252ffbafc53Scomay char *privname = NULL;
3253ffbafc53Scomay
3254ffbafc53Scomay if ((handle = zonecfg_init_handle()) == NULL) {
3255ffbafc53Scomay zerror(zlogp, B_TRUE, "getting zone configuration handle");
3256ffbafc53Scomay return (-1);
3257ffbafc53Scomay }
3258ffbafc53Scomay if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3259ffbafc53Scomay zerror(zlogp, B_FALSE, "invalid configuration");
3260ffbafc53Scomay zonecfg_fini_handle(handle);
3261ffbafc53Scomay return (-1);
3262ffbafc53Scomay }
3263ffbafc53Scomay
32646cfd72c6Sgjelinek if (ALT_MOUNT(mount_cmd)) {
3265bf1d7e28Sdh155122 zone_iptype_t iptype;
3266bf1d7e28Sdh155122 const char *curr_iptype;
3267bf1d7e28Sdh155122
3268bf1d7e28Sdh155122 if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
3269bf1d7e28Sdh155122 zerror(zlogp, B_TRUE, "unable to determine ip-type");
3270bf1d7e28Sdh155122 zonecfg_fini_handle(handle);
3271bf1d7e28Sdh155122 return (-1);
3272bf1d7e28Sdh155122 }
3273bf1d7e28Sdh155122
3274bf1d7e28Sdh155122 switch (iptype) {
3275bf1d7e28Sdh155122 case ZS_SHARED:
3276bf1d7e28Sdh155122 curr_iptype = "shared";
3277bf1d7e28Sdh155122 break;
3278bf1d7e28Sdh155122 case ZS_EXCLUSIVE:
3279bf1d7e28Sdh155122 curr_iptype = "exclusive";
3280bf1d7e28Sdh155122 break;
3281bf1d7e28Sdh155122 }
3282bf1d7e28Sdh155122
3283e767a340Sgjelinek if (zonecfg_default_privset(privs, curr_iptype) == Z_OK) {
3284e767a340Sgjelinek zonecfg_fini_handle(handle);
32859acbbeafSnn35248 return (0);
3286e767a340Sgjelinek }
32879acbbeafSnn35248 zerror(zlogp, B_FALSE,
32889acbbeafSnn35248 "failed to determine the zone's default privilege set");
32899acbbeafSnn35248 zonecfg_fini_handle(handle);
32909acbbeafSnn35248 return (-1);
32919acbbeafSnn35248 }
32929acbbeafSnn35248
3293ffbafc53Scomay switch (zonecfg_get_privset(handle, privs, &privname)) {
3294ffbafc53Scomay case Z_OK:
3295ffbafc53Scomay error = 0;
3296ffbafc53Scomay break;
3297ffbafc53Scomay case Z_PRIV_PROHIBITED:
3298ffbafc53Scomay zerror(zlogp, B_FALSE, "privilege \"%s\" is not permitted "
3299ffbafc53Scomay "within the zone's privilege set", privname);
3300ffbafc53Scomay break;
3301ffbafc53Scomay case Z_PRIV_REQUIRED:
3302ffbafc53Scomay zerror(zlogp, B_FALSE, "required privilege \"%s\" is missing "
3303ffbafc53Scomay "from the zone's privilege set", privname);
3304ffbafc53Scomay break;
3305ffbafc53Scomay case Z_PRIV_UNKNOWN:
3306ffbafc53Scomay zerror(zlogp, B_FALSE, "unknown privilege \"%s\" specified "
3307ffbafc53Scomay "in the zone's privilege set", privname);
3308ffbafc53Scomay break;
3309ffbafc53Scomay default:
3310ffbafc53Scomay zerror(zlogp, B_FALSE, "failed to determine the zone's "
3311ffbafc53Scomay "privilege set");
3312ffbafc53Scomay break;
3313ffbafc53Scomay }
3314ffbafc53Scomay
3315ffbafc53Scomay free(privname);
3316ffbafc53Scomay zonecfg_fini_handle(handle);
3317ffbafc53Scomay return (error);
3318ffbafc53Scomay }
3319ffbafc53Scomay
3320ffbafc53Scomay static int
get_rctls(zlog_t * zlogp,char ** bufp,size_t * bufsizep)33217c478bd9Sstevel@tonic-gate get_rctls(zlog_t *zlogp, char **bufp, size_t *bufsizep)
33227c478bd9Sstevel@tonic-gate {
33237c478bd9Sstevel@tonic-gate nvlist_t *nvl = NULL;
33247c478bd9Sstevel@tonic-gate char *nvl_packed = NULL;
33257c478bd9Sstevel@tonic-gate size_t nvl_size = 0;
33267c478bd9Sstevel@tonic-gate nvlist_t **nvlv = NULL;
33277c478bd9Sstevel@tonic-gate int rctlcount = 0;
33287c478bd9Sstevel@tonic-gate int error = -1;
33297c478bd9Sstevel@tonic-gate zone_dochandle_t handle;
33307c478bd9Sstevel@tonic-gate struct zone_rctltab rctltab;
33317c478bd9Sstevel@tonic-gate rctlblk_t *rctlblk = NULL;
3332ff19e029SMenno Lageman uint64_t maxlwps;
3333ff19e029SMenno Lageman uint64_t maxprocs;
33347c478bd9Sstevel@tonic-gate
33357c478bd9Sstevel@tonic-gate *bufp = NULL;
33367c478bd9Sstevel@tonic-gate *bufsizep = 0;
33377c478bd9Sstevel@tonic-gate
33387c478bd9Sstevel@tonic-gate if ((handle = zonecfg_init_handle()) == NULL) {
33397c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "getting zone configuration handle");
33407c478bd9Sstevel@tonic-gate return (-1);
33417c478bd9Sstevel@tonic-gate }
33427c478bd9Sstevel@tonic-gate if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
33437c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid configuration");
33447c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle);
33457c478bd9Sstevel@tonic-gate return (-1);
33467c478bd9Sstevel@tonic-gate }
33477c478bd9Sstevel@tonic-gate
33487c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL;
33497c478bd9Sstevel@tonic-gate if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
33507c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "nvlist_alloc");
33517c478bd9Sstevel@tonic-gate goto out;
33527c478bd9Sstevel@tonic-gate }
33537c478bd9Sstevel@tonic-gate
3354ff19e029SMenno Lageman /*
3355ff19e029SMenno Lageman * Allow the administrator to control both the maximum number of
3356ff19e029SMenno Lageman * process table slots and the maximum number of lwps with just the
3357ff19e029SMenno Lageman * max-processes property. If only the max-processes property is set,
3358ff19e029SMenno Lageman * we add a max-lwps property with a limit derived from max-processes.
3359ff19e029SMenno Lageman */
3360ff19e029SMenno Lageman if (zonecfg_get_aliased_rctl(handle, ALIAS_MAXPROCS, &maxprocs)
3361ff19e029SMenno Lageman == Z_OK &&
3362ff19e029SMenno Lageman zonecfg_get_aliased_rctl(handle, ALIAS_MAXLWPS, &maxlwps)
3363ff19e029SMenno Lageman == Z_NO_ENTRY) {
3364ff19e029SMenno Lageman if (zonecfg_set_aliased_rctl(handle, ALIAS_MAXLWPS,
3365ff19e029SMenno Lageman maxprocs * LWPS_PER_PROCESS) != Z_OK) {
3366ff19e029SMenno Lageman zerror(zlogp, B_FALSE, "unable to set max-lwps alias");
3367ff19e029SMenno Lageman goto out;
3368ff19e029SMenno Lageman }
3369ff19e029SMenno Lageman }
3370ff19e029SMenno Lageman
33717c478bd9Sstevel@tonic-gate if (zonecfg_setrctlent(handle) != Z_OK) {
33727c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setrctlent");
33737c478bd9Sstevel@tonic-gate goto out;
33747c478bd9Sstevel@tonic-gate }
33757c478bd9Sstevel@tonic-gate
33767c478bd9Sstevel@tonic-gate if ((rctlblk = malloc(rctlblk_size())) == NULL) {
33777c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "memory allocation failed");
33787c478bd9Sstevel@tonic-gate goto out;
33797c478bd9Sstevel@tonic-gate }
33807c478bd9Sstevel@tonic-gate while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
33817c478bd9Sstevel@tonic-gate struct zone_rctlvaltab *rctlval;
33827c478bd9Sstevel@tonic-gate uint_t i, count;
33837c478bd9Sstevel@tonic-gate const char *name = rctltab.zone_rctl_name;
33847c478bd9Sstevel@tonic-gate
33857c478bd9Sstevel@tonic-gate /* zoneadm should have already warned about unknown rctls. */
33867c478bd9Sstevel@tonic-gate if (!zonecfg_is_rctl(name)) {
33877c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
33887c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL;
33897c478bd9Sstevel@tonic-gate continue;
33907c478bd9Sstevel@tonic-gate }
33917c478bd9Sstevel@tonic-gate count = 0;
33927c478bd9Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
33937c478bd9Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next) {
33947c478bd9Sstevel@tonic-gate count++;
33957c478bd9Sstevel@tonic-gate }
33967c478bd9Sstevel@tonic-gate if (count == 0) { /* ignore */
33977c478bd9Sstevel@tonic-gate continue; /* Nothing to free */
33987c478bd9Sstevel@tonic-gate }
33997c478bd9Sstevel@tonic-gate if ((nvlv = malloc(sizeof (*nvlv) * count)) == NULL)
34007c478bd9Sstevel@tonic-gate goto out;
34017c478bd9Sstevel@tonic-gate i = 0;
34027c478bd9Sstevel@tonic-gate for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
34037c478bd9Sstevel@tonic-gate rctlval = rctlval->zone_rctlval_next, i++) {
34047c478bd9Sstevel@tonic-gate if (nvlist_alloc(&nvlv[i], NV_UNIQUE_NAME, 0) != 0) {
34057c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed",
34067c478bd9Sstevel@tonic-gate "nvlist_alloc");
34077c478bd9Sstevel@tonic-gate goto out;
34087c478bd9Sstevel@tonic-gate }
34097c478bd9Sstevel@tonic-gate if (zonecfg_construct_rctlblk(rctlval, rctlblk)
34107c478bd9Sstevel@tonic-gate != Z_OK) {
34117c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "invalid rctl value: "
34127c478bd9Sstevel@tonic-gate "(priv=%s,limit=%s,action=%s)",
34137c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv,
34147c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit,
34157c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action);
34167c478bd9Sstevel@tonic-gate goto out;
34177c478bd9Sstevel@tonic-gate }
34187c478bd9Sstevel@tonic-gate if (!zonecfg_valid_rctl(name, rctlblk)) {
34197c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE,
34207c478bd9Sstevel@tonic-gate "(priv=%s,limit=%s,action=%s) is not a "
34217c478bd9Sstevel@tonic-gate "valid value for rctl '%s'",
34227c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_priv,
34237c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_limit,
34247c478bd9Sstevel@tonic-gate rctlval->zone_rctlval_action,
34257c478bd9Sstevel@tonic-gate name);
34267c478bd9Sstevel@tonic-gate goto out;
34277c478bd9Sstevel@tonic-gate }
34287c478bd9Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "privilege",
34297c478bd9Sstevel@tonic-gate rctlblk_get_privilege(rctlblk)) != 0) {
34307c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed",
34317c478bd9Sstevel@tonic-gate "nvlist_add_uint64");
34327c478bd9Sstevel@tonic-gate goto out;
34337c478bd9Sstevel@tonic-gate }
34347c478bd9Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "limit",
34357c478bd9Sstevel@tonic-gate rctlblk_get_value(rctlblk)) != 0) {
34367c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed",
34377c478bd9Sstevel@tonic-gate "nvlist_add_uint64");
34387c478bd9Sstevel@tonic-gate goto out;
34397c478bd9Sstevel@tonic-gate }
34407c478bd9Sstevel@tonic-gate if (nvlist_add_uint64(nvlv[i], "action",
34417c478bd9Sstevel@tonic-gate (uint_t)rctlblk_get_local_action(rctlblk, NULL))
34427c478bd9Sstevel@tonic-gate != 0) {
34437c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed",
34447c478bd9Sstevel@tonic-gate "nvlist_add_uint64");
34457c478bd9Sstevel@tonic-gate goto out;
34467c478bd9Sstevel@tonic-gate }
34477c478bd9Sstevel@tonic-gate }
34487c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
34497c478bd9Sstevel@tonic-gate rctltab.zone_rctl_valptr = NULL;
34507c478bd9Sstevel@tonic-gate if (nvlist_add_nvlist_array(nvl, (char *)name, nvlv, count)
34517c478bd9Sstevel@tonic-gate != 0) {
34527c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed",
34537c478bd9Sstevel@tonic-gate "nvlist_add_nvlist_array");
34547c478bd9Sstevel@tonic-gate goto out;
34557c478bd9Sstevel@tonic-gate }
34567c478bd9Sstevel@tonic-gate for (i = 0; i < count; i++)
34577c478bd9Sstevel@tonic-gate nvlist_free(nvlv[i]);
34587c478bd9Sstevel@tonic-gate free(nvlv);
34597c478bd9Sstevel@tonic-gate nvlv = NULL;
34607c478bd9Sstevel@tonic-gate rctlcount++;
34617c478bd9Sstevel@tonic-gate }
34627c478bd9Sstevel@tonic-gate (void) zonecfg_endrctlent(handle);
34637c478bd9Sstevel@tonic-gate
34647c478bd9Sstevel@tonic-gate if (rctlcount == 0) {
34657c478bd9Sstevel@tonic-gate error = 0;
34667c478bd9Sstevel@tonic-gate goto out;
34677c478bd9Sstevel@tonic-gate }
34687c478bd9Sstevel@tonic-gate if (nvlist_pack(nvl, &nvl_packed, &nvl_size, NV_ENCODE_NATIVE, 0)
34697c478bd9Sstevel@tonic-gate != 0) {
34707c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s failed", "nvlist_pack");
34717c478bd9Sstevel@tonic-gate goto out;
34727c478bd9Sstevel@tonic-gate }
34737c478bd9Sstevel@tonic-gate
34747c478bd9Sstevel@tonic-gate error = 0;
34757c478bd9Sstevel@tonic-gate *bufp = nvl_packed;
34767c478bd9Sstevel@tonic-gate *bufsizep = nvl_size;
34777c478bd9Sstevel@tonic-gate
34787c478bd9Sstevel@tonic-gate out:
34797c478bd9Sstevel@tonic-gate free(rctlblk);
34807c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
34817c478bd9Sstevel@tonic-gate if (error && nvl_packed != NULL)
34827c478bd9Sstevel@tonic-gate free(nvl_packed);
34837c478bd9Sstevel@tonic-gate nvlist_free(nvl);
34847c478bd9Sstevel@tonic-gate if (nvlv != NULL)
34857c478bd9Sstevel@tonic-gate free(nvlv);
34867c478bd9Sstevel@tonic-gate if (handle != NULL)
34877c478bd9Sstevel@tonic-gate zonecfg_fini_handle(handle);
34887c478bd9Sstevel@tonic-gate return (error);
34897c478bd9Sstevel@tonic-gate }
34907c478bd9Sstevel@tonic-gate
34917c478bd9Sstevel@tonic-gate static int
get_implicit_datasets(zlog_t * zlogp,char ** retstr)3492c5cd6260S get_implicit_datasets(zlog_t *zlogp, char **retstr)
3493c5cd6260S {
3494c5cd6260S char cmdbuf[2 * MAXPATHLEN];
3495c5cd6260S
3496c5cd6260S if (query_hook[0] == '\0')
3497c5cd6260S return (0);
3498c5cd6260S
3499c5cd6260S if (snprintf(cmdbuf, sizeof (cmdbuf), "%s datasets", query_hook)
3500c5cd6260S > sizeof (cmdbuf))
3501c5cd6260S return (-1);
3502c5cd6260S
3503c5cd6260S if (do_subproc(zlogp, cmdbuf, retstr) != 0)
3504c5cd6260S return (-1);
3505c5cd6260S
3506c5cd6260S return (0);
3507c5cd6260S }
3508c5cd6260S
3509c5cd6260S static int
get_datasets(zlog_t * zlogp,char ** bufp,size_t * bufsizep)3510fa9e4066Sahrens get_datasets(zlog_t *zlogp, char **bufp, size_t *bufsizep)
3511fa9e4066Sahrens {
3512fa9e4066Sahrens zone_dochandle_t handle;
3513fa9e4066Sahrens struct zone_dstab dstab;
3514fa9e4066Sahrens size_t total, offset, len;
3515fa9e4066Sahrens int error = -1;
3516e5a17f6aSgjelinek char *str = NULL;
3517c5cd6260S char *implicit_datasets = NULL;
3518c5cd6260S int implicit_len = 0;
3519fa9e4066Sahrens
3520fa9e4066Sahrens *bufp = NULL;
3521fa9e4066Sahrens *bufsizep = 0;
3522fa9e4066Sahrens
3523fa9e4066Sahrens if ((handle = zonecfg_init_handle()) == NULL) {
3524fa9e4066Sahrens zerror(zlogp, B_TRUE, "getting zone configuration handle");
3525fa9e4066Sahrens return (-1);
3526fa9e4066Sahrens }
3527fa9e4066Sahrens if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3528fa9e4066Sahrens zerror(zlogp, B_FALSE, "invalid configuration");
3529fa9e4066Sahrens zonecfg_fini_handle(handle);
3530fa9e4066Sahrens return (-1);
3531fa9e4066Sahrens }
3532fa9e4066Sahrens
3533c5cd6260S if (get_implicit_datasets(zlogp, &implicit_datasets) != 0) {
3534c5cd6260S zerror(zlogp, B_FALSE, "getting implicit datasets failed");
3535c5cd6260S goto out;
3536c5cd6260S }
3537c5cd6260S
3538fa9e4066Sahrens if (zonecfg_setdsent(handle) != Z_OK) {
3539fa9e4066Sahrens zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3540fa9e4066Sahrens goto out;
3541fa9e4066Sahrens }
3542fa9e4066Sahrens
3543fa9e4066Sahrens total = 0;
3544fa9e4066Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK)
3545fa9e4066Sahrens total += strlen(dstab.zone_dataset_name) + 1;
3546fa9e4066Sahrens (void) zonecfg_enddsent(handle);
3547fa9e4066Sahrens
3548c5cd6260S if (implicit_datasets != NULL)
3549c5cd6260S implicit_len = strlen(implicit_datasets);
3550c5cd6260S if (implicit_len > 0)
3551c5cd6260S total += implicit_len + 1;
3552c5cd6260S
3553fa9e4066Sahrens if (total == 0) {
3554fa9e4066Sahrens error = 0;
3555fa9e4066Sahrens goto out;
3556fa9e4066Sahrens }
3557fa9e4066Sahrens
3558fa9e4066Sahrens if ((str = malloc(total)) == NULL) {
3559fa9e4066Sahrens zerror(zlogp, B_TRUE, "memory allocation failed");
3560fa9e4066Sahrens goto out;
3561fa9e4066Sahrens }
3562fa9e4066Sahrens
3563fa9e4066Sahrens if (zonecfg_setdsent(handle) != Z_OK) {
3564fa9e4066Sahrens zerror(zlogp, B_FALSE, "%s failed", "zonecfg_setdsent");
3565fa9e4066Sahrens goto out;
3566fa9e4066Sahrens }
3567fa9e4066Sahrens offset = 0;
3568fa9e4066Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3569fa9e4066Sahrens len = strlen(dstab.zone_dataset_name);
3570fa9e4066Sahrens (void) strlcpy(str + offset, dstab.zone_dataset_name,
3571e5a17f6aSgjelinek total - offset);
3572fa9e4066Sahrens offset += len;
3573e5a17f6aSgjelinek if (offset < total - 1)
3574fa9e4066Sahrens str[offset++] = ',';
3575fa9e4066Sahrens }
3576fa9e4066Sahrens (void) zonecfg_enddsent(handle);
3577fa9e4066Sahrens
3578c5cd6260S if (implicit_len > 0)
3579c5cd6260S (void) strlcpy(str + offset, implicit_datasets, total - offset);
3580c5cd6260S
3581fa9e4066Sahrens error = 0;
3582fa9e4066Sahrens *bufp = str;
3583fa9e4066Sahrens *bufsizep = total;
3584fa9e4066Sahrens
3585fa9e4066Sahrens out:
3586fa9e4066Sahrens if (error != 0 && str != NULL)
3587fa9e4066Sahrens free(str);
3588fa9e4066Sahrens if (handle != NULL)
3589fa9e4066Sahrens zonecfg_fini_handle(handle);
3590c5cd6260S if (implicit_datasets != NULL)
3591c5cd6260S free(implicit_datasets);
3592fa9e4066Sahrens
3593fa9e4066Sahrens return (error);
3594fa9e4066Sahrens }
3595fa9e4066Sahrens
3596fa9e4066Sahrens static int
validate_datasets(zlog_t * zlogp)3597fa9e4066Sahrens validate_datasets(zlog_t *zlogp)
3598fa9e4066Sahrens {
3599fa9e4066Sahrens zone_dochandle_t handle;
3600fa9e4066Sahrens struct zone_dstab dstab;
3601fa9e4066Sahrens zfs_handle_t *zhp;
360299653d4eSeschrock libzfs_handle_t *hdl;
3603fa9e4066Sahrens
3604fa9e4066Sahrens if ((handle = zonecfg_init_handle()) == NULL) {
3605fa9e4066Sahrens zerror(zlogp, B_TRUE, "getting zone configuration handle");
3606fa9e4066Sahrens return (-1);
3607fa9e4066Sahrens }
3608fa9e4066Sahrens if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
3609fa9e4066Sahrens zerror(zlogp, B_FALSE, "invalid configuration");
3610fa9e4066Sahrens zonecfg_fini_handle(handle);
3611fa9e4066Sahrens return (-1);
3612fa9e4066Sahrens }
3613fa9e4066Sahrens
3614fa9e4066Sahrens if (zonecfg_setdsent(handle) != Z_OK) {
3615fa9e4066Sahrens zerror(zlogp, B_FALSE, "invalid configuration");
3616fa9e4066Sahrens zonecfg_fini_handle(handle);
3617fa9e4066Sahrens return (-1);
3618fa9e4066Sahrens }
3619fa9e4066Sahrens
362099653d4eSeschrock if ((hdl = libzfs_init()) == NULL) {
362199653d4eSeschrock zerror(zlogp, B_FALSE, "opening ZFS library");
362299653d4eSeschrock zonecfg_fini_handle(handle);
362399653d4eSeschrock return (-1);
362499653d4eSeschrock }
3625fa9e4066Sahrens
3626fa9e4066Sahrens while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
3627fa9e4066Sahrens
362899653d4eSeschrock if ((zhp = zfs_open(hdl, dstab.zone_dataset_name,
3629fa9e4066Sahrens ZFS_TYPE_FILESYSTEM)) == NULL) {
3630fa9e4066Sahrens zerror(zlogp, B_FALSE, "cannot open ZFS dataset '%s'",
3631fa9e4066Sahrens dstab.zone_dataset_name);
3632fa9e4066Sahrens zonecfg_fini_handle(handle);
363399653d4eSeschrock libzfs_fini(hdl);
3634fa9e4066Sahrens return (-1);
3635fa9e4066Sahrens }
3636fa9e4066Sahrens
3637fa9e4066Sahrens /*
3638fa9e4066Sahrens * Automatically set the 'zoned' property. We check the value
3639fa9e4066Sahrens * first because we'll get EPERM if it is already set.
3640fa9e4066Sahrens */
3641fa9e4066Sahrens if (!zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
3642e9dbad6fSeschrock zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_ZONED),
3643e9dbad6fSeschrock "on") != 0) {
3644fa9e4066Sahrens zerror(zlogp, B_FALSE, "cannot set 'zoned' "
3645fa9e4066Sahrens "property for ZFS dataset '%s'\n",
3646fa9e4066Sahrens dstab.zone_dataset_name);
3647fa9e4066Sahrens zonecfg_fini_handle(handle);
3648fa9e4066Sahrens zfs_close(zhp);
364999653d4eSeschrock libzfs_fini(hdl);
3650fa9e4066Sahrens return (-1);
3651fa9e4066Sahrens }
3652fa9e4066Sahrens
3653fa9e4066Sahrens zfs_close(zhp);
3654fa9e4066Sahrens }
3655fa9e4066Sahrens (void) zonecfg_enddsent(handle);
3656fa9e4066Sahrens
3657fa9e4066Sahrens zonecfg_fini_handle(handle);
365899653d4eSeschrock libzfs_fini(hdl);
3659fa9e4066Sahrens
3660fa9e4066Sahrens return (0);
3661fa9e4066Sahrens }
3662fa9e4066Sahrens
366345916cd2Sjpk /*
36644201a95eSRic Aleshire * Return true if the path is its own zfs file system. We determine this
36654201a95eSRic Aleshire * by stat-ing the path to see if it is zfs and stat-ing the parent to see
36664201a95eSRic Aleshire * if it is a different fs.
36674201a95eSRic Aleshire */
36684201a95eSRic Aleshire boolean_t
is_zonepath_zfs(char * zonepath)36694201a95eSRic Aleshire is_zonepath_zfs(char *zonepath)
36704201a95eSRic Aleshire {
36714201a95eSRic Aleshire int res;
36724201a95eSRic Aleshire char *path;
36734201a95eSRic Aleshire char *parent;
36744201a95eSRic Aleshire struct statvfs64 buf1, buf2;
36754201a95eSRic Aleshire
36764201a95eSRic Aleshire if (statvfs64(zonepath, &buf1) != 0)
36774201a95eSRic Aleshire return (B_FALSE);
36784201a95eSRic Aleshire
36794201a95eSRic Aleshire if (strcmp(buf1.f_basetype, "zfs") != 0)
36804201a95eSRic Aleshire return (B_FALSE);
36814201a95eSRic Aleshire
36824201a95eSRic Aleshire if ((path = strdup(zonepath)) == NULL)
36834201a95eSRic Aleshire return (B_FALSE);
36844201a95eSRic Aleshire
36854201a95eSRic Aleshire parent = dirname(path);
36864201a95eSRic Aleshire res = statvfs64(parent, &buf2);
36874201a95eSRic Aleshire free(path);
36884201a95eSRic Aleshire
36894201a95eSRic Aleshire if (res != 0)
36904201a95eSRic Aleshire return (B_FALSE);
36914201a95eSRic Aleshire
36924201a95eSRic Aleshire if (buf1.f_fsid == buf2.f_fsid)
36934201a95eSRic Aleshire return (B_FALSE);
36944201a95eSRic Aleshire
36954201a95eSRic Aleshire return (B_TRUE);
36964201a95eSRic Aleshire }
36974201a95eSRic Aleshire
36984201a95eSRic Aleshire /*
36994201a95eSRic Aleshire * Verify the MAC label in the root dataset for the zone.
37004201a95eSRic Aleshire * If the label exists, it must match the label configured for the zone.
37014201a95eSRic Aleshire * Otherwise if there's no label on the dataset, create one here.
37024201a95eSRic Aleshire */
37034201a95eSRic Aleshire
37044201a95eSRic Aleshire static int
validate_rootds_label(zlog_t * zlogp,char * rootpath,m_label_t * zone_sl)37054201a95eSRic Aleshire validate_rootds_label(zlog_t *zlogp, char *rootpath, m_label_t *zone_sl)
37064201a95eSRic Aleshire {
37074201a95eSRic Aleshire int error = -1;
37084201a95eSRic Aleshire zfs_handle_t *zhp;
37094201a95eSRic Aleshire libzfs_handle_t *hdl;
37104201a95eSRic Aleshire m_label_t ds_sl;
37114201a95eSRic Aleshire char zonepath[MAXPATHLEN];
37124201a95eSRic Aleshire char ds_hexsl[MAXNAMELEN];
37134201a95eSRic Aleshire
37144201a95eSRic Aleshire if (!is_system_labeled())
37154201a95eSRic Aleshire return (0);
37164201a95eSRic Aleshire
37174201a95eSRic Aleshire if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
37184201a95eSRic Aleshire zerror(zlogp, B_TRUE, "unable to determine zone path");
37194201a95eSRic Aleshire return (-1);
37204201a95eSRic Aleshire }
37214201a95eSRic Aleshire
37224201a95eSRic Aleshire if (!is_zonepath_zfs(zonepath))
37234201a95eSRic Aleshire return (0);
37244201a95eSRic Aleshire
37254201a95eSRic Aleshire if ((hdl = libzfs_init()) == NULL) {
37264201a95eSRic Aleshire zerror(zlogp, B_FALSE, "opening ZFS library");
37274201a95eSRic Aleshire return (-1);
37284201a95eSRic Aleshire }
37294201a95eSRic Aleshire
37304201a95eSRic Aleshire if ((zhp = zfs_path_to_zhandle(hdl, rootpath,
37314201a95eSRic Aleshire ZFS_TYPE_FILESYSTEM)) == NULL) {
37324201a95eSRic Aleshire zerror(zlogp, B_FALSE, "cannot open ZFS dataset for path '%s'",
37334201a95eSRic Aleshire rootpath);
37344201a95eSRic Aleshire libzfs_fini(hdl);
37354201a95eSRic Aleshire return (-1);
37364201a95eSRic Aleshire }
37374201a95eSRic Aleshire
37384201a95eSRic Aleshire /* Get the mlslabel property if it exists. */
37394201a95eSRic Aleshire if ((zfs_prop_get(zhp, ZFS_PROP_MLSLABEL, ds_hexsl, MAXNAMELEN,
37404201a95eSRic Aleshire NULL, NULL, 0, B_TRUE) != 0) ||
37414201a95eSRic Aleshire (strcmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)) {
37424201a95eSRic Aleshire char *str2 = NULL;
37434201a95eSRic Aleshire
37444201a95eSRic Aleshire /*
37454201a95eSRic Aleshire * No label on the dataset (or default only); create one.
37464201a95eSRic Aleshire * (Only do this automatic labeling for the labeled brand.)
37474201a95eSRic Aleshire */
37484201a95eSRic Aleshire if (strcmp(brand_name, LABELED_BRAND_NAME) != 0) {
37494201a95eSRic Aleshire error = 0;
37504201a95eSRic Aleshire goto out;
37514201a95eSRic Aleshire }
37524201a95eSRic Aleshire
37534201a95eSRic Aleshire error = l_to_str_internal(zone_sl, &str2);
37544201a95eSRic Aleshire if (error)
37554201a95eSRic Aleshire goto out;
37564201a95eSRic Aleshire if (str2 == NULL) {
37574201a95eSRic Aleshire error = -1;
37584201a95eSRic Aleshire goto out;
37594201a95eSRic Aleshire }
37604201a95eSRic Aleshire if ((error = zfs_prop_set(zhp,
37614201a95eSRic Aleshire zfs_prop_to_name(ZFS_PROP_MLSLABEL), str2)) != 0) {
37624201a95eSRic Aleshire zerror(zlogp, B_FALSE, "cannot set 'mlslabel' "
37634201a95eSRic Aleshire "property for root dataset at '%s'\n", rootpath);
37644201a95eSRic Aleshire }
37654201a95eSRic Aleshire free(str2);
37664201a95eSRic Aleshire goto out;
37674201a95eSRic Aleshire }
37684201a95eSRic Aleshire
37694201a95eSRic Aleshire /* Convert the retrieved dataset label to binary form. */
37704201a95eSRic Aleshire error = hexstr_to_label(ds_hexsl, &ds_sl);
37714201a95eSRic Aleshire if (error) {
37724201a95eSRic Aleshire zerror(zlogp, B_FALSE, "invalid 'mlslabel' "
37734201a95eSRic Aleshire "property on root dataset at '%s'\n", rootpath);
37744201a95eSRic Aleshire goto out; /* exit with error */
37754201a95eSRic Aleshire }
37764201a95eSRic Aleshire
37774201a95eSRic Aleshire /*
37784201a95eSRic Aleshire * Perform a MAC check by comparing the zone label with the
37794201a95eSRic Aleshire * dataset label.
37804201a95eSRic Aleshire */
37814201a95eSRic Aleshire error = (!blequal(zone_sl, &ds_sl));
37824201a95eSRic Aleshire if (error)
37834201a95eSRic Aleshire zerror(zlogp, B_FALSE, "Rootpath dataset has mismatched label");
37844201a95eSRic Aleshire out:
37854201a95eSRic Aleshire zfs_close(zhp);
37864201a95eSRic Aleshire libzfs_fini(hdl);
37874201a95eSRic Aleshire
37884201a95eSRic Aleshire return (error);
37894201a95eSRic Aleshire }
37904201a95eSRic Aleshire
37914201a95eSRic Aleshire /*
379245916cd2Sjpk * Mount lower level home directories into/from current zone
379345916cd2Sjpk * Share exported directories specified in dfstab for zone
379445916cd2Sjpk */
379545916cd2Sjpk static int
tsol_mounts(zlog_t * zlogp,char * zone_name,char * rootpath)379645916cd2Sjpk tsol_mounts(zlog_t *zlogp, char *zone_name, char *rootpath)
379745916cd2Sjpk {
379845916cd2Sjpk zoneid_t *zids = NULL;
379945916cd2Sjpk priv_set_t *zid_privs;
380045916cd2Sjpk const priv_impl_info_t *ip = NULL;
380145916cd2Sjpk uint_t nzents_saved;
380245916cd2Sjpk uint_t nzents;
380345916cd2Sjpk int i;
380445916cd2Sjpk char readonly[] = "ro";
380545916cd2Sjpk struct zone_fstab lower_fstab;
380645916cd2Sjpk char *argv[4];
380745916cd2Sjpk
380845916cd2Sjpk if (!is_system_labeled())
380945916cd2Sjpk return (0);
381045916cd2Sjpk
381145916cd2Sjpk if (zid_label == NULL) {
381245916cd2Sjpk zid_label = m_label_alloc(MAC_LABEL);
381345916cd2Sjpk if (zid_label == NULL)
381445916cd2Sjpk return (-1);
381545916cd2Sjpk }
381645916cd2Sjpk
381745916cd2Sjpk /* Make sure our zone has an /export/home dir */
381845916cd2Sjpk (void) make_one_dir(zlogp, rootpath, "/export/home",
381927e6fb21Sdp DEFAULT_DIR_MODE, DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
382045916cd2Sjpk
382145916cd2Sjpk lower_fstab.zone_fs_raw[0] = '\0';
382245916cd2Sjpk (void) strlcpy(lower_fstab.zone_fs_type, MNTTYPE_LOFS,
382345916cd2Sjpk sizeof (lower_fstab.zone_fs_type));
382445916cd2Sjpk lower_fstab.zone_fs_options = NULL;
382545916cd2Sjpk (void) zonecfg_add_fs_option(&lower_fstab, readonly);
382645916cd2Sjpk
382745916cd2Sjpk /*
382845916cd2Sjpk * Get the list of zones from the kernel
382945916cd2Sjpk */
383045916cd2Sjpk if (zone_list(NULL, &nzents) != 0) {
383145916cd2Sjpk zerror(zlogp, B_TRUE, "unable to list zones");
383245916cd2Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
383345916cd2Sjpk return (-1);
383445916cd2Sjpk }
383545916cd2Sjpk again:
383645916cd2Sjpk if (nzents == 0) {
383745916cd2Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
383845916cd2Sjpk return (-1);
383945916cd2Sjpk }
384045916cd2Sjpk
384145916cd2Sjpk zids = malloc(nzents * sizeof (zoneid_t));
384245916cd2Sjpk if (zids == NULL) {
38433f2f09c1Sdp zerror(zlogp, B_TRUE, "memory allocation failed");
384445916cd2Sjpk return (-1);
384545916cd2Sjpk }
384645916cd2Sjpk nzents_saved = nzents;
384745916cd2Sjpk
384845916cd2Sjpk if (zone_list(zids, &nzents) != 0) {
384945916cd2Sjpk zerror(zlogp, B_TRUE, "unable to list zones");
385045916cd2Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
385145916cd2Sjpk free(zids);
385245916cd2Sjpk return (-1);
385345916cd2Sjpk }
385445916cd2Sjpk if (nzents != nzents_saved) {
385545916cd2Sjpk /* list changed, try again */
385645916cd2Sjpk free(zids);
385745916cd2Sjpk goto again;
385845916cd2Sjpk }
385945916cd2Sjpk
386045916cd2Sjpk ip = getprivimplinfo();
386145916cd2Sjpk if ((zid_privs = priv_allocset()) == NULL) {
386245916cd2Sjpk zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
386345916cd2Sjpk zonecfg_free_fs_option_list(
386445916cd2Sjpk lower_fstab.zone_fs_options);
386545916cd2Sjpk free(zids);
386645916cd2Sjpk return (-1);
386745916cd2Sjpk }
386845916cd2Sjpk
386945916cd2Sjpk for (i = 0; i < nzents; i++) {
387045916cd2Sjpk char zid_name[ZONENAME_MAX];
387145916cd2Sjpk zone_state_t zid_state;
387245916cd2Sjpk char zid_rpath[MAXPATHLEN];
387345916cd2Sjpk struct stat stat_buf;
387445916cd2Sjpk
387545916cd2Sjpk if (zids[i] == GLOBAL_ZONEID)
387645916cd2Sjpk continue;
387745916cd2Sjpk
387845916cd2Sjpk if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
387945916cd2Sjpk continue;
388045916cd2Sjpk
388145916cd2Sjpk /*
388245916cd2Sjpk * Do special setup for the zone we are booting
388345916cd2Sjpk */
388445916cd2Sjpk if (strcmp(zid_name, zone_name) == 0) {
388545916cd2Sjpk struct zone_fstab autofs_fstab;
388645916cd2Sjpk char map_path[MAXPATHLEN];
388745916cd2Sjpk int fd;
388845916cd2Sjpk
388945916cd2Sjpk /*
389045916cd2Sjpk * Create auto_home_<zone> map for this zone
38919acbbeafSnn35248 * in the global zone. The non-global zone entry
389245916cd2Sjpk * will be created by automount when the zone
389345916cd2Sjpk * is booted.
389445916cd2Sjpk */
389545916cd2Sjpk
389645916cd2Sjpk (void) snprintf(autofs_fstab.zone_fs_special,
389745916cd2Sjpk MAXPATHLEN, "auto_home_%s", zid_name);
389845916cd2Sjpk
389945916cd2Sjpk (void) snprintf(autofs_fstab.zone_fs_dir, MAXPATHLEN,
390045916cd2Sjpk "/zone/%s/home", zid_name);
390145916cd2Sjpk
390245916cd2Sjpk (void) snprintf(map_path, sizeof (map_path),
390345916cd2Sjpk "/etc/%s", autofs_fstab.zone_fs_special);
390445916cd2Sjpk /*
390545916cd2Sjpk * If the map file doesn't exist create a template
390645916cd2Sjpk */
390745916cd2Sjpk if ((fd = open(map_path, O_RDWR | O_CREAT | O_EXCL,
390845916cd2Sjpk S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH)) != -1) {
390945916cd2Sjpk int len;
391045916cd2Sjpk char map_rec[MAXPATHLEN];
391145916cd2Sjpk
391245916cd2Sjpk len = snprintf(map_rec, sizeof (map_rec),
391345916cd2Sjpk "+%s\n*\t-fstype=lofs\t:%s/export/home/&\n",
391445916cd2Sjpk autofs_fstab.zone_fs_special, rootpath);
391545916cd2Sjpk (void) write(fd, map_rec, len);
391645916cd2Sjpk (void) close(fd);
391745916cd2Sjpk }
391845916cd2Sjpk
391945916cd2Sjpk /*
392045916cd2Sjpk * Mount auto_home_<zone> in the global zone if absent.
392145916cd2Sjpk * If it's already of type autofs, then
392245916cd2Sjpk * don't mount it again.
392345916cd2Sjpk */
392445916cd2Sjpk if ((stat(autofs_fstab.zone_fs_dir, &stat_buf) == -1) ||
392545916cd2Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_AUTOFS) != 0) {
392645916cd2Sjpk char optstr[] = "indirect,ignore,nobrowse";
392745916cd2Sjpk
392845916cd2Sjpk (void) make_one_dir(zlogp, "",
392927e6fb21Sdp autofs_fstab.zone_fs_dir, DEFAULT_DIR_MODE,
393027e6fb21Sdp DEFAULT_DIR_USER, DEFAULT_DIR_GROUP);
393145916cd2Sjpk
393245916cd2Sjpk /*
393345916cd2Sjpk * Mount will fail if automounter has already
393445916cd2Sjpk * processed the auto_home_<zonename> map
393545916cd2Sjpk */
393645916cd2Sjpk (void) domount(zlogp, MNTTYPE_AUTOFS, optstr,
393745916cd2Sjpk autofs_fstab.zone_fs_special,
393845916cd2Sjpk autofs_fstab.zone_fs_dir);
393945916cd2Sjpk }
394045916cd2Sjpk continue;
394145916cd2Sjpk }
394245916cd2Sjpk
394345916cd2Sjpk
394445916cd2Sjpk if (zone_get_state(zid_name, &zid_state) != Z_OK ||
394548451833Scarlsonj (zid_state != ZONE_STATE_READY &&
394648451833Scarlsonj zid_state != ZONE_STATE_RUNNING))
394745916cd2Sjpk /* Skip over zones without mounted filesystems */
394845916cd2Sjpk continue;
394945916cd2Sjpk
395045916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
395145916cd2Sjpk sizeof (m_label_t)) < 0)
395245916cd2Sjpk /* Skip over zones with unspecified label */
395345916cd2Sjpk continue;
395445916cd2Sjpk
395545916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
395645916cd2Sjpk sizeof (zid_rpath)) == -1)
395745916cd2Sjpk /* Skip over zones with bad path */
395845916cd2Sjpk continue;
395945916cd2Sjpk
396045916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_PRIVSET, zid_privs,
396145916cd2Sjpk sizeof (priv_chunk_t) * ip->priv_setsize) == -1)
396245916cd2Sjpk /* Skip over zones with bad privs */
396345916cd2Sjpk continue;
396445916cd2Sjpk
396545916cd2Sjpk /*
396645916cd2Sjpk * Reading down is valid according to our label model
396745916cd2Sjpk * but some customers want to disable it because it
396845916cd2Sjpk * allows execute down and other possible attacks.
396945916cd2Sjpk * Therefore, we restrict this feature to zones that
397045916cd2Sjpk * have the NET_MAC_AWARE privilege which is required
397145916cd2Sjpk * for NFS read-down semantics.
397245916cd2Sjpk */
397345916cd2Sjpk if ((bldominates(zlabel, zid_label)) &&
397445916cd2Sjpk (priv_ismember(zprivs, PRIV_NET_MAC_AWARE))) {
397545916cd2Sjpk /*
397645916cd2Sjpk * Our zone dominates this one.
397745916cd2Sjpk * Create a lofs mount from lower zone's /export/home
397845916cd2Sjpk */
397945916cd2Sjpk (void) snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
398045916cd2Sjpk "%s/zone/%s/export/home", rootpath, zid_name);
398145916cd2Sjpk
398245916cd2Sjpk /*
398345916cd2Sjpk * If the target is already an LOFS mount
398445916cd2Sjpk * then don't do it again.
398545916cd2Sjpk */
398645916cd2Sjpk if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
398745916cd2Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
398845916cd2Sjpk
398945916cd2Sjpk if (snprintf(lower_fstab.zone_fs_special,
399045916cd2Sjpk MAXPATHLEN, "%s/export",
399145916cd2Sjpk zid_rpath) > MAXPATHLEN)
399245916cd2Sjpk continue;
399345916cd2Sjpk
399445916cd2Sjpk /*
399545916cd2Sjpk * Make sure the lower-level home exists
399645916cd2Sjpk */
399745916cd2Sjpk if (make_one_dir(zlogp,
399827e6fb21Sdp lower_fstab.zone_fs_special, "/home",
399927e6fb21Sdp DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
400027e6fb21Sdp DEFAULT_DIR_GROUP) != 0)
400145916cd2Sjpk continue;
400245916cd2Sjpk
400345916cd2Sjpk (void) strlcat(lower_fstab.zone_fs_special,
400445916cd2Sjpk "/home", MAXPATHLEN);
400545916cd2Sjpk
400645916cd2Sjpk /*
400745916cd2Sjpk * Mount can fail because the lower-level
400845916cd2Sjpk * zone may have already done a mount up.
400945916cd2Sjpk */
40100679f794S (void) mount_one(zlogp, &lower_fstab, "",
40110679f794S Z_MNT_BOOT);
401245916cd2Sjpk }
401345916cd2Sjpk } else if ((bldominates(zid_label, zlabel)) &&
401445916cd2Sjpk (priv_ismember(zid_privs, PRIV_NET_MAC_AWARE))) {
401545916cd2Sjpk /*
401645916cd2Sjpk * This zone dominates our zone.
401745916cd2Sjpk * Create a lofs mount from our zone's /export/home
401845916cd2Sjpk */
401945916cd2Sjpk if (snprintf(lower_fstab.zone_fs_dir, MAXPATHLEN,
402045916cd2Sjpk "%s/zone/%s/export/home", zid_rpath,
402145916cd2Sjpk zone_name) > MAXPATHLEN)
402245916cd2Sjpk continue;
402345916cd2Sjpk
402445916cd2Sjpk /*
402545916cd2Sjpk * If the target is already an LOFS mount
402645916cd2Sjpk * then don't do it again.
402745916cd2Sjpk */
402845916cd2Sjpk if ((stat(lower_fstab.zone_fs_dir, &stat_buf) == -1) ||
402945916cd2Sjpk strcmp(stat_buf.st_fstype, MNTTYPE_LOFS) != 0) {
403045916cd2Sjpk
403145916cd2Sjpk (void) snprintf(lower_fstab.zone_fs_special,
403245916cd2Sjpk MAXPATHLEN, "%s/export/home", rootpath);
403345916cd2Sjpk
403445916cd2Sjpk /*
403545916cd2Sjpk * Mount can fail because the higher-level
403645916cd2Sjpk * zone may have already done a mount down.
403745916cd2Sjpk */
40380679f794S (void) mount_one(zlogp, &lower_fstab, "",
40390679f794S Z_MNT_BOOT);
404045916cd2Sjpk }
404145916cd2Sjpk }
404245916cd2Sjpk }
404345916cd2Sjpk zonecfg_free_fs_option_list(lower_fstab.zone_fs_options);
404445916cd2Sjpk priv_freeset(zid_privs);
404545916cd2Sjpk free(zids);
404645916cd2Sjpk
404745916cd2Sjpk /*
404845916cd2Sjpk * Now share any exported directories from this zone.
404945916cd2Sjpk * Each zone can have its own dfstab.
405045916cd2Sjpk */
405145916cd2Sjpk
405245916cd2Sjpk argv[0] = "zoneshare";
405345916cd2Sjpk argv[1] = "-z";
405445916cd2Sjpk argv[2] = zone_name;
405545916cd2Sjpk argv[3] = NULL;
405645916cd2Sjpk
405745916cd2Sjpk (void) forkexec(zlogp, "/usr/lib/zones/zoneshare", argv);
405845916cd2Sjpk /* Don't check for errors since they don't affect the zone */
405945916cd2Sjpk
406045916cd2Sjpk return (0);
406145916cd2Sjpk }
406245916cd2Sjpk
406345916cd2Sjpk /*
406445916cd2Sjpk * Unmount lofs mounts from higher level zones
406545916cd2Sjpk * Unshare nfs exported directories
406645916cd2Sjpk */
406745916cd2Sjpk static void
tsol_unmounts(zlog_t * zlogp,char * zone_name)406845916cd2Sjpk tsol_unmounts(zlog_t *zlogp, char *zone_name)
406945916cd2Sjpk {
407045916cd2Sjpk zoneid_t *zids = NULL;
407145916cd2Sjpk uint_t nzents_saved;
407245916cd2Sjpk uint_t nzents;
407345916cd2Sjpk int i;
407445916cd2Sjpk char *argv[4];
407545916cd2Sjpk char path[MAXPATHLEN];
407645916cd2Sjpk
407745916cd2Sjpk if (!is_system_labeled())
407845916cd2Sjpk return;
407945916cd2Sjpk
408045916cd2Sjpk /*
408145916cd2Sjpk * Get the list of zones from the kernel
408245916cd2Sjpk */
408345916cd2Sjpk if (zone_list(NULL, &nzents) != 0) {
408445916cd2Sjpk return;
408545916cd2Sjpk }
408645916cd2Sjpk
408745916cd2Sjpk if (zid_label == NULL) {
408845916cd2Sjpk zid_label = m_label_alloc(MAC_LABEL);
408945916cd2Sjpk if (zid_label == NULL)
409045916cd2Sjpk return;
409145916cd2Sjpk }
409245916cd2Sjpk
409345916cd2Sjpk again:
409445916cd2Sjpk if (nzents == 0)
409545916cd2Sjpk return;
409645916cd2Sjpk
409745916cd2Sjpk zids = malloc(nzents * sizeof (zoneid_t));
409845916cd2Sjpk if (zids == NULL) {
40993f2f09c1Sdp zerror(zlogp, B_TRUE, "memory allocation failed");
410045916cd2Sjpk return;
410145916cd2Sjpk }
410245916cd2Sjpk nzents_saved = nzents;
410345916cd2Sjpk
410445916cd2Sjpk if (zone_list(zids, &nzents) != 0) {
410545916cd2Sjpk free(zids);
410645916cd2Sjpk return;
410745916cd2Sjpk }
410845916cd2Sjpk if (nzents != nzents_saved) {
410945916cd2Sjpk /* list changed, try again */
411045916cd2Sjpk free(zids);
411145916cd2Sjpk goto again;
411245916cd2Sjpk }
411345916cd2Sjpk
411445916cd2Sjpk for (i = 0; i < nzents; i++) {
411545916cd2Sjpk char zid_name[ZONENAME_MAX];
411645916cd2Sjpk zone_state_t zid_state;
411745916cd2Sjpk char zid_rpath[MAXPATHLEN];
411845916cd2Sjpk
411945916cd2Sjpk if (zids[i] == GLOBAL_ZONEID)
412045916cd2Sjpk continue;
412145916cd2Sjpk
412245916cd2Sjpk if (getzonenamebyid(zids[i], zid_name, ZONENAME_MAX) == -1)
412345916cd2Sjpk continue;
412445916cd2Sjpk
412545916cd2Sjpk /*
412645916cd2Sjpk * Skip the zone we are halting
412745916cd2Sjpk */
412845916cd2Sjpk if (strcmp(zid_name, zone_name) == 0)
412945916cd2Sjpk continue;
413045916cd2Sjpk
413145916cd2Sjpk if ((zone_getattr(zids[i], ZONE_ATTR_STATUS, &zid_state,
413245916cd2Sjpk sizeof (zid_state)) < 0) ||
413345916cd2Sjpk (zid_state < ZONE_IS_READY))
413445916cd2Sjpk /* Skip over zones without mounted filesystems */
413545916cd2Sjpk continue;
413645916cd2Sjpk
413745916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_SLBL, zid_label,
413845916cd2Sjpk sizeof (m_label_t)) < 0)
413945916cd2Sjpk /* Skip over zones with unspecified label */
414045916cd2Sjpk continue;
414145916cd2Sjpk
414245916cd2Sjpk if (zone_getattr(zids[i], ZONE_ATTR_ROOT, zid_rpath,
414345916cd2Sjpk sizeof (zid_rpath)) == -1)
414445916cd2Sjpk /* Skip over zones with bad path */
414545916cd2Sjpk continue;
414645916cd2Sjpk
414745916cd2Sjpk if (zlabel != NULL && bldominates(zid_label, zlabel)) {
414845916cd2Sjpk /*
414945916cd2Sjpk * This zone dominates our zone.
415045916cd2Sjpk * Unmount the lofs mount of our zone's /export/home
415145916cd2Sjpk */
415245916cd2Sjpk
415345916cd2Sjpk if (snprintf(path, MAXPATHLEN,
415445916cd2Sjpk "%s/zone/%s/export/home", zid_rpath,
415545916cd2Sjpk zone_name) > MAXPATHLEN)
415645916cd2Sjpk continue;
415745916cd2Sjpk
415845916cd2Sjpk /* Skip over mount failures */
415945916cd2Sjpk (void) umount(path);
416045916cd2Sjpk }
416145916cd2Sjpk }
416245916cd2Sjpk free(zids);
416345916cd2Sjpk
416445916cd2Sjpk /*
416545916cd2Sjpk * Unmount global zone autofs trigger for this zone
416645916cd2Sjpk */
416745916cd2Sjpk (void) snprintf(path, MAXPATHLEN, "/zone/%s/home", zone_name);
416845916cd2Sjpk /* Skip over mount failures */
416945916cd2Sjpk (void) umount(path);
417045916cd2Sjpk
417145916cd2Sjpk /*
417245916cd2Sjpk * Next unshare any exported directories from this zone.
417345916cd2Sjpk */
417445916cd2Sjpk
417545916cd2Sjpk argv[0] = "zoneunshare";
417645916cd2Sjpk argv[1] = "-z";
417745916cd2Sjpk argv[2] = zone_name;
417845916cd2Sjpk argv[3] = NULL;
417945916cd2Sjpk
418045916cd2Sjpk (void) forkexec(zlogp, "/usr/lib/zones/zoneunshare", argv);
418145916cd2Sjpk /* Don't check for errors since they don't affect the zone */
418245916cd2Sjpk
418345916cd2Sjpk /*
418445916cd2Sjpk * Finally, deallocate any devices in the zone.
418545916cd2Sjpk */
418645916cd2Sjpk
418745916cd2Sjpk argv[0] = "deallocate";
418845916cd2Sjpk argv[1] = "-Isz";
418945916cd2Sjpk argv[2] = zone_name;
419045916cd2Sjpk argv[3] = NULL;
419145916cd2Sjpk
419245916cd2Sjpk (void) forkexec(zlogp, "/usr/sbin/deallocate", argv);
419345916cd2Sjpk /* Don't check for errors since they don't affect the zone */
419445916cd2Sjpk }
419545916cd2Sjpk
419645916cd2Sjpk /*
419745916cd2Sjpk * Fetch the Trusted Extensions label and multi-level ports (MLPs) for
419845916cd2Sjpk * this zone.
419945916cd2Sjpk */
420045916cd2Sjpk static tsol_zcent_t *
get_zone_label(zlog_t * zlogp,priv_set_t * privs)420145916cd2Sjpk get_zone_label(zlog_t *zlogp, priv_set_t *privs)
420245916cd2Sjpk {
420345916cd2Sjpk FILE *fp;
420445916cd2Sjpk tsol_zcent_t *zcent = NULL;
420545916cd2Sjpk char line[MAXTNZLEN];
420645916cd2Sjpk
420745916cd2Sjpk if ((fp = fopen(TNZONECFG_PATH, "r")) == NULL) {
420845916cd2Sjpk zerror(zlogp, B_TRUE, "%s", TNZONECFG_PATH);
420945916cd2Sjpk return (NULL);
421045916cd2Sjpk }
421145916cd2Sjpk
421245916cd2Sjpk while (fgets(line, sizeof (line), fp) != NULL) {
421345916cd2Sjpk /*
421445916cd2Sjpk * Check for malformed database
421545916cd2Sjpk */
421645916cd2Sjpk if (strlen(line) == MAXTNZLEN - 1)
421745916cd2Sjpk break;
421845916cd2Sjpk if ((zcent = tsol_sgetzcent(line, NULL, NULL)) == NULL)
421945916cd2Sjpk continue;
422045916cd2Sjpk if (strcmp(zcent->zc_name, zone_name) == 0)
422145916cd2Sjpk break;
422245916cd2Sjpk tsol_freezcent(zcent);
422345916cd2Sjpk zcent = NULL;
422445916cd2Sjpk }
422545916cd2Sjpk (void) fclose(fp);
422645916cd2Sjpk
422745916cd2Sjpk if (zcent == NULL) {
422845916cd2Sjpk zerror(zlogp, B_FALSE, "zone requires a label assignment. "
422945916cd2Sjpk "See tnzonecfg(4)");
423045916cd2Sjpk } else {
423145916cd2Sjpk if (zlabel == NULL)
423245916cd2Sjpk zlabel = m_label_alloc(MAC_LABEL);
423345916cd2Sjpk /*
423445916cd2Sjpk * Save this zone's privileges for later read-down processing
423545916cd2Sjpk */
423645916cd2Sjpk if ((zprivs = priv_allocset()) == NULL) {
423745916cd2Sjpk zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
423845916cd2Sjpk return (NULL);
423945916cd2Sjpk } else {
424045916cd2Sjpk priv_copyset(privs, zprivs);
424145916cd2Sjpk }
424245916cd2Sjpk }
424345916cd2Sjpk return (zcent);
424445916cd2Sjpk }
424545916cd2Sjpk
424645916cd2Sjpk /*
424745916cd2Sjpk * Add the Trusted Extensions multi-level ports for this zone.
424845916cd2Sjpk */
424945916cd2Sjpk static void
set_mlps(zlog_t * zlogp,zoneid_t zoneid,tsol_zcent_t * zcent)425045916cd2Sjpk set_mlps(zlog_t *zlogp, zoneid_t zoneid, tsol_zcent_t *zcent)
425145916cd2Sjpk {
425245916cd2Sjpk tsol_mlp_t *mlp;
425345916cd2Sjpk tsol_mlpent_t tsme;
425445916cd2Sjpk
425545916cd2Sjpk if (!is_system_labeled())
425645916cd2Sjpk return;
425745916cd2Sjpk
425845916cd2Sjpk tsme.tsme_zoneid = zoneid;
425945916cd2Sjpk tsme.tsme_flags = 0;
426045916cd2Sjpk for (mlp = zcent->zc_private_mlp; !TSOL_MLP_END(mlp); mlp++) {
426145916cd2Sjpk tsme.tsme_mlp = *mlp;
426245916cd2Sjpk if (tnmlp(TNDB_LOAD, &tsme) != 0) {
426345916cd2Sjpk zerror(zlogp, B_TRUE, "cannot set zone-specific MLP "
426445916cd2Sjpk "on %d-%d/%d", mlp->mlp_port,
426545916cd2Sjpk mlp->mlp_port_upper, mlp->mlp_ipp);
426645916cd2Sjpk }
426745916cd2Sjpk }
426845916cd2Sjpk
426945916cd2Sjpk tsme.tsme_flags = TSOL_MEF_SHARED;
427045916cd2Sjpk for (mlp = zcent->zc_shared_mlp; !TSOL_MLP_END(mlp); mlp++) {
427145916cd2Sjpk tsme.tsme_mlp = *mlp;
427245916cd2Sjpk if (tnmlp(TNDB_LOAD, &tsme) != 0) {
427345916cd2Sjpk zerror(zlogp, B_TRUE, "cannot set shared MLP "
427445916cd2Sjpk "on %d-%d/%d", mlp->mlp_port,
427545916cd2Sjpk mlp->mlp_port_upper, mlp->mlp_ipp);
427645916cd2Sjpk }
427745916cd2Sjpk }
427845916cd2Sjpk }
427945916cd2Sjpk
428045916cd2Sjpk static void
remove_mlps(zlog_t * zlogp,zoneid_t zoneid)428145916cd2Sjpk remove_mlps(zlog_t *zlogp, zoneid_t zoneid)
428245916cd2Sjpk {
428345916cd2Sjpk tsol_mlpent_t tsme;
428445916cd2Sjpk
428545916cd2Sjpk if (!is_system_labeled())
428645916cd2Sjpk return;
428745916cd2Sjpk
428845916cd2Sjpk (void) memset(&tsme, 0, sizeof (tsme));
428945916cd2Sjpk tsme.tsme_zoneid = zoneid;
429045916cd2Sjpk if (tnmlp(TNDB_FLUSH, &tsme) != 0)
429145916cd2Sjpk zerror(zlogp, B_TRUE, "cannot flush MLPs");
429245916cd2Sjpk }
429345916cd2Sjpk
42947c478bd9Sstevel@tonic-gate int
prtmount(const struct mnttab * fs,void * x)4295*9a686fbcSPaul Dagnelie prtmount(const struct mnttab *fs, void *x)
4296*9a686fbcSPaul Dagnelie {
42970094b373Sjv227347 zerror((zlog_t *)x, B_FALSE, " %s", fs->mnt_mountp);
42987c478bd9Sstevel@tonic-gate return (0);
42997c478bd9Sstevel@tonic-gate }
43007c478bd9Sstevel@tonic-gate
4301108322fbScarlsonj /*
4302108322fbScarlsonj * Look for zones running on the main system that are using this root (or any
4303108322fbScarlsonj * subdirectory of it). Return B_TRUE and print an error if a conflicting zone
4304108322fbScarlsonj * is found or if we can't tell.
4305108322fbScarlsonj */
4306108322fbScarlsonj static boolean_t
duplicate_zone_root(zlog_t * zlogp,const char * rootpath)4307108322fbScarlsonj duplicate_zone_root(zlog_t *zlogp, const char *rootpath)
43087c478bd9Sstevel@tonic-gate {
4309108322fbScarlsonj zoneid_t *zids = NULL;
4310108322fbScarlsonj uint_t nzids = 0;
4311108322fbScarlsonj boolean_t retv;
4312108322fbScarlsonj int rlen, zlen;
4313108322fbScarlsonj char zroot[MAXPATHLEN];
4314108322fbScarlsonj char zonename[ZONENAME_MAX];
4315108322fbScarlsonj
4316108322fbScarlsonj for (;;) {
4317108322fbScarlsonj nzids += 10;
4318108322fbScarlsonj zids = malloc(nzids * sizeof (*zids));
4319108322fbScarlsonj if (zids == NULL) {
43203f2f09c1Sdp zerror(zlogp, B_TRUE, "memory allocation failed");
4321108322fbScarlsonj return (B_TRUE);
4322108322fbScarlsonj }
4323108322fbScarlsonj if (zone_list(zids, &nzids) == 0)
4324108322fbScarlsonj break;
4325108322fbScarlsonj free(zids);
4326108322fbScarlsonj }
4327108322fbScarlsonj retv = B_FALSE;
4328108322fbScarlsonj rlen = strlen(rootpath);
4329108322fbScarlsonj while (nzids > 0) {
4330108322fbScarlsonj /*
4331108322fbScarlsonj * Ignore errors; they just mean that the zone has disappeared
4332108322fbScarlsonj * while we were busy.
4333108322fbScarlsonj */
4334108322fbScarlsonj if (zone_getattr(zids[--nzids], ZONE_ATTR_ROOT, zroot,
4335108322fbScarlsonj sizeof (zroot)) == -1)
4336108322fbScarlsonj continue;
4337108322fbScarlsonj zlen = strlen(zroot);
4338108322fbScarlsonj if (zlen > rlen)
4339108322fbScarlsonj zlen = rlen;
4340108322fbScarlsonj if (strncmp(rootpath, zroot, zlen) == 0 &&
4341108322fbScarlsonj (zroot[zlen] == '\0' || zroot[zlen] == '/') &&
4342108322fbScarlsonj (rootpath[zlen] == '\0' || rootpath[zlen] == '/')) {
4343108322fbScarlsonj if (getzonenamebyid(zids[nzids], zonename,
4344108322fbScarlsonj sizeof (zonename)) == -1)
4345108322fbScarlsonj (void) snprintf(zonename, sizeof (zonename),
4346108322fbScarlsonj "id %d", (int)zids[nzids]);
4347108322fbScarlsonj zerror(zlogp, B_FALSE,
4348108322fbScarlsonj "zone root %s already in use by zone %s",
4349108322fbScarlsonj rootpath, zonename);
4350108322fbScarlsonj retv = B_TRUE;
4351108322fbScarlsonj break;
4352108322fbScarlsonj }
4353108322fbScarlsonj }
4354108322fbScarlsonj free(zids);
4355108322fbScarlsonj return (retv);
4356108322fbScarlsonj }
4357108322fbScarlsonj
4358108322fbScarlsonj /*
4359108322fbScarlsonj * Search for loopback mounts that use this same source node (same device and
4360108322fbScarlsonj * inode). Return B_TRUE if there is one or if we can't tell.
4361108322fbScarlsonj */
4362108322fbScarlsonj static boolean_t
duplicate_reachable_path(zlog_t * zlogp,const char * rootpath)4363108322fbScarlsonj duplicate_reachable_path(zlog_t *zlogp, const char *rootpath)
4364108322fbScarlsonj {
4365108322fbScarlsonj struct stat64 rst, zst;
4366108322fbScarlsonj struct mnttab *mnp;
4367108322fbScarlsonj
4368108322fbScarlsonj if (stat64(rootpath, &rst) == -1) {
4369108322fbScarlsonj zerror(zlogp, B_TRUE, "can't stat %s", rootpath);
4370108322fbScarlsonj return (B_TRUE);
4371108322fbScarlsonj }
4372108322fbScarlsonj if (resolve_lofs_mnts == NULL && lofs_read_mnttab(zlogp) == -1)
4373108322fbScarlsonj return (B_TRUE);
4374108322fbScarlsonj for (mnp = resolve_lofs_mnts; mnp < resolve_lofs_mnt_max; mnp++) {
4375108322fbScarlsonj if (mnp->mnt_fstype == NULL ||
4376108322fbScarlsonj strcmp(MNTTYPE_LOFS, mnp->mnt_fstype) != 0)
4377108322fbScarlsonj continue;
4378108322fbScarlsonj /* We're looking at a loopback mount. Stat it. */
4379108322fbScarlsonj if (mnp->mnt_special != NULL &&
4380108322fbScarlsonj stat64(mnp->mnt_special, &zst) != -1 &&
4381108322fbScarlsonj rst.st_dev == zst.st_dev && rst.st_ino == zst.st_ino) {
4382108322fbScarlsonj zerror(zlogp, B_FALSE,
4383108322fbScarlsonj "zone root %s is reachable through %s",
4384108322fbScarlsonj rootpath, mnp->mnt_mountp);
4385108322fbScarlsonj return (B_TRUE);
4386108322fbScarlsonj }
4387108322fbScarlsonj }
4388108322fbScarlsonj return (B_FALSE);
4389108322fbScarlsonj }
4390108322fbScarlsonj
43910209230bSgjelinek /*
43920209230bSgjelinek * Set memory cap and pool info for the zone's resource management
43930209230bSgjelinek * configuration.
43940209230bSgjelinek */
43950209230bSgjelinek static int
setup_zone_rm(zlog_t * zlogp,char * zone_name,zoneid_t zoneid)43960209230bSgjelinek setup_zone_rm(zlog_t *zlogp, char *zone_name, zoneid_t zoneid)
43970209230bSgjelinek {
43980209230bSgjelinek int res;
43990209230bSgjelinek uint64_t tmp;
44000209230bSgjelinek struct zone_mcaptab mcap;
44010209230bSgjelinek char sched[MAXNAMELEN];
44020209230bSgjelinek zone_dochandle_t handle = NULL;
44030209230bSgjelinek char pool_err[128];
44040209230bSgjelinek
44050209230bSgjelinek if ((handle = zonecfg_init_handle()) == NULL) {
44060209230bSgjelinek zerror(zlogp, B_TRUE, "getting zone configuration handle");
44070209230bSgjelinek return (Z_BAD_HANDLE);
44080209230bSgjelinek }
44090209230bSgjelinek
44100209230bSgjelinek if ((res = zonecfg_get_snapshot_handle(zone_name, handle)) != Z_OK) {
44110209230bSgjelinek zerror(zlogp, B_FALSE, "invalid configuration");
44120209230bSgjelinek zonecfg_fini_handle(handle);
44130209230bSgjelinek return (res);
44140209230bSgjelinek }
44150209230bSgjelinek
44160209230bSgjelinek /*
44170209230bSgjelinek * If a memory cap is configured, set the cap in the kernel using
44180209230bSgjelinek * zone_setattr() and make sure the rcapd SMF service is enabled.
44190209230bSgjelinek */
44200209230bSgjelinek if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
44210209230bSgjelinek uint64_t num;
44220209230bSgjelinek char smf_err[128];
44230209230bSgjelinek
44240209230bSgjelinek num = (uint64_t)strtoull(mcap.zone_physmem_cap, NULL, 10);
44250209230bSgjelinek if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
44260209230bSgjelinek zerror(zlogp, B_TRUE, "could not set zone memory cap");
44270209230bSgjelinek zonecfg_fini_handle(handle);
44280209230bSgjelinek return (Z_INVAL);
44290209230bSgjelinek }
44300209230bSgjelinek
44310209230bSgjelinek if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
44320209230bSgjelinek zerror(zlogp, B_FALSE, "enabling system/rcap service "
44330209230bSgjelinek "failed: %s", smf_err);
44340209230bSgjelinek zonecfg_fini_handle(handle);
44350209230bSgjelinek return (Z_INVAL);
44360209230bSgjelinek }
44370209230bSgjelinek }
44380209230bSgjelinek
44390209230bSgjelinek /* Get the scheduling class set in the zone configuration. */
44400209230bSgjelinek if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
44410209230bSgjelinek strlen(sched) > 0) {
44420209230bSgjelinek if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, sched,
44430209230bSgjelinek strlen(sched)) == -1)
44440209230bSgjelinek zerror(zlogp, B_TRUE, "WARNING: unable to set the "
44450209230bSgjelinek "default scheduling class");
44460209230bSgjelinek
44470209230bSgjelinek } else if (zonecfg_get_aliased_rctl(handle, ALIAS_SHARES, &tmp)
44480209230bSgjelinek == Z_OK) {
44490209230bSgjelinek /*
44500209230bSgjelinek * If the zone has the zone.cpu-shares rctl set then we want to
44510209230bSgjelinek * use the Fair Share Scheduler (FSS) for processes in the
44520209230bSgjelinek * zone. Check what scheduling class the zone would be running
44530209230bSgjelinek * in by default so we can print a warning and modify the class
44540209230bSgjelinek * if we wouldn't be using FSS.
44550209230bSgjelinek */
44560209230bSgjelinek char class_name[PC_CLNMSZ];
44570209230bSgjelinek
44580209230bSgjelinek if (zonecfg_get_dflt_sched_class(handle, class_name,
44590209230bSgjelinek sizeof (class_name)) != Z_OK) {
44600209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: unable to determine "
44610209230bSgjelinek "the zone's scheduling class");
44620209230bSgjelinek
44630209230bSgjelinek } else if (strcmp("FSS", class_name) != 0) {
44640209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: The zone.cpu-shares "
44650209230bSgjelinek "rctl is set but\nFSS is not the default "
44660209230bSgjelinek "scheduling class for\nthis zone. FSS will be "
44670209230bSgjelinek "used for processes\nin the zone but to get the "
44680209230bSgjelinek "full benefit of FSS,\nit should be the default "
44690209230bSgjelinek "scheduling class.\nSee dispadmin(1M) for more "
44700209230bSgjelinek "details.");
44710209230bSgjelinek
44720209230bSgjelinek if (zone_setattr(zoneid, ZONE_ATTR_SCHED_CLASS, "FSS",
44730209230bSgjelinek strlen("FSS")) == -1)
44740209230bSgjelinek zerror(zlogp, B_TRUE, "WARNING: unable to set "
44750209230bSgjelinek "zone scheduling class to FSS");
44760209230bSgjelinek }
44770209230bSgjelinek }
44780209230bSgjelinek
44790209230bSgjelinek /*
44800209230bSgjelinek * The next few blocks of code attempt to set up temporary pools as
44810209230bSgjelinek * well as persistent pools. In all cases we call the functions
44820209230bSgjelinek * unconditionally. Within each funtion the code will check if the
44830209230bSgjelinek * zone is actually configured for a temporary pool or persistent pool
44840209230bSgjelinek * and just return if there is nothing to do.
44850209230bSgjelinek *
44860209230bSgjelinek * If we are rebooting we want to attempt to reuse any temporary pool
44870209230bSgjelinek * that was previously set up. zonecfg_bind_tmp_pool() will do the
44880209230bSgjelinek * right thing in all cases (reuse or create) based on the current
44890209230bSgjelinek * zonecfg.
44900209230bSgjelinek */
44910209230bSgjelinek if ((res = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
44920209230bSgjelinek sizeof (pool_err))) != Z_OK) {
44930209230bSgjelinek if (res == Z_POOL || res == Z_POOL_CREATE || res == Z_POOL_BIND)
44940209230bSgjelinek zerror(zlogp, B_FALSE, "%s: %s\ndedicated-cpu setting "
44950209230bSgjelinek "cannot be instantiated", zonecfg_strerror(res),
44960209230bSgjelinek pool_err);
44970209230bSgjelinek else
44980209230bSgjelinek zerror(zlogp, B_FALSE, "could not bind zone to "
44990209230bSgjelinek "temporary pool: %s", zonecfg_strerror(res));
45000209230bSgjelinek zonecfg_fini_handle(handle);
45010209230bSgjelinek return (Z_POOL_BIND);
45020209230bSgjelinek }
45030209230bSgjelinek
45040209230bSgjelinek /*
45050209230bSgjelinek * Check if we need to warn about poold not being enabled.
45060209230bSgjelinek */
45070209230bSgjelinek if (zonecfg_warn_poold(handle)) {
45080209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: A range of dedicated-cpus has "
45090209230bSgjelinek "been specified\nbut the dynamic pool service is not "
45100209230bSgjelinek "enabled.\nThe system will not dynamically adjust the\n"
45110209230bSgjelinek "processor allocation within the specified range\n"
45120209230bSgjelinek "until svc:/system/pools/dynamic is enabled.\n"
45130209230bSgjelinek "See poold(1M).");
45140209230bSgjelinek }
45150209230bSgjelinek
45160209230bSgjelinek /* The following is a warning, not an error. */
45170209230bSgjelinek if ((res = zonecfg_bind_pool(handle, zoneid, pool_err,
45180209230bSgjelinek sizeof (pool_err))) != Z_OK) {
45190209230bSgjelinek if (res == Z_POOL_BIND)
45200209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: unable to bind to "
45210209230bSgjelinek "pool '%s'; using default pool.", pool_err);
45220209230bSgjelinek else if (res == Z_POOL)
45230209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: %s: %s",
45240209230bSgjelinek zonecfg_strerror(res), pool_err);
45250209230bSgjelinek else
45260209230bSgjelinek zerror(zlogp, B_FALSE, "WARNING: %s",
45270209230bSgjelinek zonecfg_strerror(res));
45280209230bSgjelinek }
4529efd4c9b6SSteve Lawrence
4530efd4c9b6SSteve Lawrence /* Update saved pool name in case it has changed */
4531a3002e0aSGarrett D'Amore (void) zonecfg_get_poolname(handle, zone_name, pool_name,
4532a3002e0aSGarrett D'Amore sizeof (pool_name));
45330209230bSgjelinek
45340209230bSgjelinek zonecfg_fini_handle(handle);
45350209230bSgjelinek return (Z_OK);
45360209230bSgjelinek }
45370209230bSgjelinek
45380fbb751dSJohn Levon static void
report_prop_err(zlog_t * zlogp,const char * name,const char * value,int res)45390fbb751dSJohn Levon report_prop_err(zlog_t *zlogp, const char *name, const char *value, int res)
45400fbb751dSJohn Levon {
45410fbb751dSJohn Levon switch (res) {
45420fbb751dSJohn Levon case Z_TOO_BIG:
45430fbb751dSJohn Levon zerror(zlogp, B_FALSE, "%s property value is too large.", name);
45440fbb751dSJohn Levon break;
45450fbb751dSJohn Levon
45460fbb751dSJohn Levon case Z_INVALID_PROPERTY:
45470fbb751dSJohn Levon zerror(zlogp, B_FALSE, "%s property value \"%s\" is not valid",
45480fbb751dSJohn Levon name, value);
45490fbb751dSJohn Levon break;
45500fbb751dSJohn Levon
45510fbb751dSJohn Levon default:
45520fbb751dSJohn Levon zerror(zlogp, B_TRUE, "fetching property %s: %d", name, res);
45530fbb751dSJohn Levon break;
45540fbb751dSJohn Levon }
45550fbb751dSJohn Levon }
45560fbb751dSJohn Levon
45575679c89fSjv227347 /*
45585679c89fSjv227347 * Sets the hostid of the new zone based on its configured value. The zone's
45595679c89fSjv227347 * zone_t structure must already exist in kernel memory. 'zlogp' refers to the
45605679c89fSjv227347 * log used to report errors and warnings and must be non-NULL. 'zone_namep'
45615679c89fSjv227347 * is the name of the new zone and must be non-NULL. 'zoneid' is the numeric
45625679c89fSjv227347 * ID of the new zone.
45635679c89fSjv227347 *
45645679c89fSjv227347 * This function returns zero on success and a nonzero error code on failure.
45655679c89fSjv227347 */
45665679c89fSjv227347 static int
setup_zone_hostid(zone_dochandle_t handle,zlog_t * zlogp,zoneid_t zoneid)45670fbb751dSJohn Levon setup_zone_hostid(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
45685679c89fSjv227347 {
45695679c89fSjv227347 int res;
45705679c89fSjv227347 char hostidp[HW_HOSTID_LEN];
45715679c89fSjv227347 unsigned int hostid;
45725679c89fSjv227347
45730fbb751dSJohn Levon res = zonecfg_get_hostid(handle, hostidp, sizeof (hostidp));
45740fbb751dSJohn Levon
45750fbb751dSJohn Levon if (res == Z_BAD_PROPERTY) {
45760fbb751dSJohn Levon return (Z_OK);
45770fbb751dSJohn Levon } else if (res != Z_OK) {
45780fbb751dSJohn Levon report_prop_err(zlogp, "hostid", hostidp, res);
45790fbb751dSJohn Levon return (res);
45800fbb751dSJohn Levon }
45810fbb751dSJohn Levon
45820fbb751dSJohn Levon hostid = (unsigned int)strtoul(hostidp, NULL, 16);
45830fbb751dSJohn Levon if ((res = zone_setattr(zoneid, ZONE_ATTR_HOSTID, &hostid,
45840fbb751dSJohn Levon sizeof (hostid))) != 0) {
45850fbb751dSJohn Levon zerror(zlogp, B_TRUE,
45860fbb751dSJohn Levon "zone hostid is not valid: %s: %d", hostidp, res);
45870fbb751dSJohn Levon return (Z_SYSTEM);
45880fbb751dSJohn Levon }
45890fbb751dSJohn Levon
45900fbb751dSJohn Levon return (res);
45910fbb751dSJohn Levon }
45920fbb751dSJohn Levon
45930fbb751dSJohn Levon static int
setup_zone_fs_allowed(zone_dochandle_t handle,zlog_t * zlogp,zoneid_t zoneid)45940fbb751dSJohn Levon setup_zone_fs_allowed(zone_dochandle_t handle, zlog_t *zlogp, zoneid_t zoneid)
45950fbb751dSJohn Levon {
45968cd81a20SJerry Jelinek char fsallowed[ZONE_FS_ALLOWED_MAX];
45978cd81a20SJerry Jelinek char *fsallowedp = fsallowed;
45988cd81a20SJerry Jelinek int len = sizeof (fsallowed);
45990fbb751dSJohn Levon int res;
46000fbb751dSJohn Levon
46018cd81a20SJerry Jelinek res = zonecfg_get_fs_allowed(handle, fsallowed, len);
46020fbb751dSJohn Levon
46030fbb751dSJohn Levon if (res == Z_BAD_PROPERTY) {
46048cd81a20SJerry Jelinek /* No value, set the defaults */
46058cd81a20SJerry Jelinek (void) strlcpy(fsallowed, DFLT_FS_ALLOWED, len);
46060fbb751dSJohn Levon } else if (res != Z_OK) {
46078cd81a20SJerry Jelinek report_prop_err(zlogp, "fs-allowed", fsallowed, res);
46080fbb751dSJohn Levon return (res);
46098cd81a20SJerry Jelinek } else if (fsallowed[0] == '-') {
46108cd81a20SJerry Jelinek /* dropping default privs - use remaining list */
46118cd81a20SJerry Jelinek if (fsallowed[1] != ',')
46128cd81a20SJerry Jelinek return (Z_OK);
46138cd81a20SJerry Jelinek fsallowedp += 2;
46148cd81a20SJerry Jelinek len -= 2;
46158cd81a20SJerry Jelinek } else {
46168cd81a20SJerry Jelinek /* Has a value, append the defaults */
46178cd81a20SJerry Jelinek if (strlcat(fsallowed, ",", len) >= len ||
46188cd81a20SJerry Jelinek strlcat(fsallowed, DFLT_FS_ALLOWED, len) >= len) {
46198cd81a20SJerry Jelinek report_prop_err(zlogp, "fs-allowed", fsallowed,
46208cd81a20SJerry Jelinek Z_TOO_BIG);
46218cd81a20SJerry Jelinek return (Z_TOO_BIG);
46228cd81a20SJerry Jelinek }
46230fbb751dSJohn Levon }
46240fbb751dSJohn Levon
46258cd81a20SJerry Jelinek if (zone_setattr(zoneid, ZONE_ATTR_FS_ALLOWED, fsallowedp, len) != 0) {
46260fbb751dSJohn Levon zerror(zlogp, B_TRUE,
46270fbb751dSJohn Levon "fs-allowed couldn't be set: %s: %d", fsallowedp, res);
46280fbb751dSJohn Levon return (Z_SYSTEM);
46290fbb751dSJohn Levon }
46300fbb751dSJohn Levon
46318cd81a20SJerry Jelinek return (Z_OK);
46320fbb751dSJohn Levon }
46330fbb751dSJohn Levon
46340fbb751dSJohn Levon static int
setup_zone_attrs(zlog_t * zlogp,char * zone_namep,zoneid_t zoneid)46350fbb751dSJohn Levon setup_zone_attrs(zlog_t *zlogp, char *zone_namep, zoneid_t zoneid)
46360fbb751dSJohn Levon {
46370fbb751dSJohn Levon zone_dochandle_t handle;
46380fbb751dSJohn Levon int res = Z_OK;
46390fbb751dSJohn Levon
46405679c89fSjv227347 if ((handle = zonecfg_init_handle()) == NULL) {
46415679c89fSjv227347 zerror(zlogp, B_TRUE, "getting zone configuration handle");
46425679c89fSjv227347 return (Z_BAD_HANDLE);
46435679c89fSjv227347 }
46445679c89fSjv227347 if ((res = zonecfg_get_snapshot_handle(zone_namep, handle)) != Z_OK) {
46455679c89fSjv227347 zerror(zlogp, B_FALSE, "invalid configuration");
46460fbb751dSJohn Levon goto out;
46475679c89fSjv227347 }
46485679c89fSjv227347
46490fbb751dSJohn Levon if ((res = setup_zone_hostid(handle, zlogp, zoneid)) != Z_OK)
46500fbb751dSJohn Levon goto out;
46510fbb751dSJohn Levon
46520fbb751dSJohn Levon if ((res = setup_zone_fs_allowed(handle, zlogp, zoneid)) != Z_OK)
46530fbb751dSJohn Levon goto out;
46540fbb751dSJohn Levon
46550fbb751dSJohn Levon out:
46565679c89fSjv227347 zonecfg_fini_handle(handle);
46575679c89fSjv227347 return (res);
46585679c89fSjv227347 }
46595679c89fSjv227347
4660108322fbScarlsonj zoneid_t
vplat_create(zlog_t * zlogp,zone_mnt_t mount_cmd)46616cfd72c6Sgjelinek vplat_create(zlog_t *zlogp, zone_mnt_t mount_cmd)
4662108322fbScarlsonj {
4663108322fbScarlsonj zoneid_t rval = -1;
46647c478bd9Sstevel@tonic-gate priv_set_t *privs;
46657c478bd9Sstevel@tonic-gate char rootpath[MAXPATHLEN];
46667c478bd9Sstevel@tonic-gate char *rctlbuf = NULL;
4667108322fbScarlsonj size_t rctlbufsz = 0;
4668fa9e4066Sahrens char *zfsbuf = NULL;
4669fa9e4066Sahrens size_t zfsbufsz = 0;
4670108322fbScarlsonj zoneid_t zoneid = -1;
46717c478bd9Sstevel@tonic-gate int xerr;
4672108322fbScarlsonj char *kzone;
4673108322fbScarlsonj FILE *fp = NULL;
467445916cd2Sjpk tsol_zcent_t *zcent = NULL;
467545916cd2Sjpk int match = 0;
467645916cd2Sjpk int doi = 0;
4677f4b3ec61Sdh155122 int flags;
4678f4b3ec61Sdh155122 zone_iptype_t iptype;
46797c478bd9Sstevel@tonic-gate
46807c478bd9Sstevel@tonic-gate if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
46817c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to determine zone root");
46827c478bd9Sstevel@tonic-gate return (-1);
46837c478bd9Sstevel@tonic-gate }
4684108322fbScarlsonj if (zonecfg_in_alt_root())
4685108322fbScarlsonj resolve_lofs(zlogp, rootpath, sizeof (rootpath));
46867c478bd9Sstevel@tonic-gate
46872b24ab6bSSebastien Roy if (vplat_get_iptype(zlogp, &iptype) < 0) {
4688f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "unable to determine ip-type");
4689f4b3ec61Sdh155122 return (-1);
4690f4b3ec61Sdh155122 }
4691f4b3ec61Sdh155122 switch (iptype) {
4692f4b3ec61Sdh155122 case ZS_SHARED:
4693f4b3ec61Sdh155122 flags = 0;
4694f4b3ec61Sdh155122 break;
4695f4b3ec61Sdh155122 case ZS_EXCLUSIVE:
4696f4b3ec61Sdh155122 flags = ZCF_NET_EXCL;
4697f4b3ec61Sdh155122 break;
4698f4b3ec61Sdh155122 }
4699f4b3ec61Sdh155122
47007c478bd9Sstevel@tonic-gate if ((privs = priv_allocset()) == NULL) {
47017c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
47027c478bd9Sstevel@tonic-gate return (-1);
47037c478bd9Sstevel@tonic-gate }
47047c478bd9Sstevel@tonic-gate priv_emptyset(privs);
4705ffbafc53Scomay if (get_privset(zlogp, privs, mount_cmd) != 0)
47067c478bd9Sstevel@tonic-gate goto error;
4707ffbafc53Scomay
47086cfd72c6Sgjelinek if (mount_cmd == Z_MNT_BOOT &&
47096cfd72c6Sgjelinek get_rctls(zlogp, &rctlbuf, &rctlbufsz) != 0) {
47107c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "Unable to get list of rctls");
47117c478bd9Sstevel@tonic-gate goto error;
47127c478bd9Sstevel@tonic-gate }
4713ffbafc53Scomay
4714fa9e4066Sahrens if (get_datasets(zlogp, &zfsbuf, &zfsbufsz) != 0) {
4715fa9e4066Sahrens zerror(zlogp, B_FALSE, "Unable to get list of ZFS datasets");
4716fa9e4066Sahrens goto error;
4717fa9e4066Sahrens }
47187c478bd9Sstevel@tonic-gate
47196cfd72c6Sgjelinek if (mount_cmd == Z_MNT_BOOT && is_system_labeled()) {
472045916cd2Sjpk zcent = get_zone_label(zlogp, privs);
472148451833Scarlsonj if (zcent != NULL) {
472245916cd2Sjpk match = zcent->zc_match;
472345916cd2Sjpk doi = zcent->zc_doi;
472445916cd2Sjpk *zlabel = zcent->zc_label;
472545916cd2Sjpk } else {
472645916cd2Sjpk goto error;
472745916cd2Sjpk }
47284201a95eSRic Aleshire if (validate_rootds_label(zlogp, rootpath, zlabel) != 0)
47294201a95eSRic Aleshire goto error;
473045916cd2Sjpk }
473145916cd2Sjpk
4732108322fbScarlsonj kzone = zone_name;
4733108322fbScarlsonj
4734108322fbScarlsonj /*
4735108322fbScarlsonj * We must do this scan twice. First, we look for zones running on the
4736108322fbScarlsonj * main system that are using this root (or any subdirectory of it).
4737108322fbScarlsonj * Next, we reduce to the shortest path and search for loopback mounts
4738108322fbScarlsonj * that use this same source node (same device and inode).
4739108322fbScarlsonj */
4740108322fbScarlsonj if (duplicate_zone_root(zlogp, rootpath))
4741108322fbScarlsonj goto error;
4742108322fbScarlsonj if (duplicate_reachable_path(zlogp, rootpath))
4743108322fbScarlsonj goto error;
4744108322fbScarlsonj
47456cfd72c6Sgjelinek if (ALT_MOUNT(mount_cmd)) {
4746108322fbScarlsonj root_to_lu(zlogp, rootpath, sizeof (rootpath), B_TRUE);
4747108322fbScarlsonj
4748108322fbScarlsonj /*
4749108322fbScarlsonj * Forge up a special root for this zone. When a zone is
4750108322fbScarlsonj * mounted, we can't let the zone have its own root because the
4751108322fbScarlsonj * tools that will be used in this "scratch zone" need access
4752108322fbScarlsonj * to both the zone's resources and the running machine's
4753108322fbScarlsonj * executables.
4754108322fbScarlsonj *
4755108322fbScarlsonj * Note that the mkdir here also catches read-only filesystems.
4756108322fbScarlsonj */
4757108322fbScarlsonj if (mkdir(rootpath, 0755) != 0 && errno != EEXIST) {
4758108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot create %s", rootpath);
4759108322fbScarlsonj goto error;
4760108322fbScarlsonj }
4761108322fbScarlsonj if (domount(zlogp, "tmpfs", "", "swap", rootpath) != 0)
4762108322fbScarlsonj goto error;
4763108322fbScarlsonj }
4764108322fbScarlsonj
4765108322fbScarlsonj if (zonecfg_in_alt_root()) {
4766108322fbScarlsonj /*
4767108322fbScarlsonj * If we are mounting up a zone in an alternate root partition,
4768108322fbScarlsonj * then we have some additional work to do before starting the
4769108322fbScarlsonj * zone. First, resolve the root path down so that we're not
4770108322fbScarlsonj * fooled by duplicates. Then forge up an internal name for
4771108322fbScarlsonj * the zone.
4772108322fbScarlsonj */
4773108322fbScarlsonj if ((fp = zonecfg_open_scratch("", B_TRUE)) == NULL) {
4774108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot open mapfile");
4775108322fbScarlsonj goto error;
4776108322fbScarlsonj }
4777108322fbScarlsonj if (zonecfg_lock_scratch(fp) != 0) {
4778108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot lock mapfile");
4779108322fbScarlsonj goto error;
4780108322fbScarlsonj }
4781108322fbScarlsonj if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
4782108322fbScarlsonj NULL, 0) == 0) {
4783108322fbScarlsonj zerror(zlogp, B_FALSE, "scratch zone already running");
4784108322fbScarlsonj goto error;
4785108322fbScarlsonj }
4786108322fbScarlsonj /* This is the preferred name */
4787108322fbScarlsonj (void) snprintf(kernzone, sizeof (kernzone), "SUNWlu-%s",
4788108322fbScarlsonj zone_name);
4789108322fbScarlsonj srandom(getpid());
4790108322fbScarlsonj while (zonecfg_reverse_scratch(fp, kernzone, NULL, 0, NULL,
4791108322fbScarlsonj 0) == 0) {
4792108322fbScarlsonj /* This is just an arbitrary name; note "." usage */
4793108322fbScarlsonj (void) snprintf(kernzone, sizeof (kernzone),
4794108322fbScarlsonj "SUNWlu.%08lX%08lX", random(), random());
4795108322fbScarlsonj }
4796108322fbScarlsonj kzone = kernzone;
4797108322fbScarlsonj }
4798108322fbScarlsonj
47997c478bd9Sstevel@tonic-gate xerr = 0;
4800108322fbScarlsonj if ((zoneid = zone_create(kzone, rootpath, privs, rctlbuf,
4801f4b3ec61Sdh155122 rctlbufsz, zfsbuf, zfsbufsz, &xerr, match, doi, zlabel,
4802f4b3ec61Sdh155122 flags)) == -1) {
48037c478bd9Sstevel@tonic-gate if (xerr == ZE_AREMOUNTS) {
48047c478bd9Sstevel@tonic-gate if (zonecfg_find_mounts(rootpath, NULL, NULL) < 1) {
48057c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE,
48067c478bd9Sstevel@tonic-gate "An unknown file-system is mounted on "
48077c478bd9Sstevel@tonic-gate "a subdirectory of %s", rootpath);
48087c478bd9Sstevel@tonic-gate } else {
48097c478bd9Sstevel@tonic-gate
48107c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE,
48117c478bd9Sstevel@tonic-gate "These file-systems are mounted on "
48127c478bd9Sstevel@tonic-gate "subdirectories of %s:", rootpath);
48137c478bd9Sstevel@tonic-gate (void) zonecfg_find_mounts(rootpath,
48147c478bd9Sstevel@tonic-gate prtmount, zlogp);
48157c478bd9Sstevel@tonic-gate }
48167c478bd9Sstevel@tonic-gate } else if (xerr == ZE_CHROOTED) {
48177c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE, "%s: "
48187c478bd9Sstevel@tonic-gate "cannot create a zone from a chrooted "
48197c478bd9Sstevel@tonic-gate "environment", "zone_create");
48208c55461bSton } else if (xerr == ZE_LABELINUSE) {
48218c55461bSton char zonename[ZONENAME_MAX];
48228c55461bSton (void) getzonenamebyid(getzoneidbylabel(zlabel),
48238c55461bSton zonename, ZONENAME_MAX);
48248c55461bSton zerror(zlogp, B_FALSE, "The zone label is already "
48258c55461bSton "used by the zone '%s'.", zonename);
48267c478bd9Sstevel@tonic-gate } else {
48277c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "%s failed", "zone_create");
48287c478bd9Sstevel@tonic-gate }
48297c478bd9Sstevel@tonic-gate goto error;
48307c478bd9Sstevel@tonic-gate }
4831108322fbScarlsonj
4832108322fbScarlsonj if (zonecfg_in_alt_root() &&
4833108322fbScarlsonj zonecfg_add_scratch(fp, zone_name, kernzone,
4834108322fbScarlsonj zonecfg_get_root()) == -1) {
4835108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot add mapfile entry");
4836108322fbScarlsonj goto error;
4837108322fbScarlsonj }
4838108322fbScarlsonj
48390679f794S /*
48400679f794S * The following actions are not performed when merely mounting a zone
48410679f794S * for administrative use.
48420679f794S */
48430679f794S if (mount_cmd == Z_MNT_BOOT) {
48440679f794S brand_handle_t bh;
48450679f794S struct brand_attr attr;
48460679f794S char modname[MAXPATHLEN];
48470679f794S
48480fbb751dSJohn Levon if (setup_zone_attrs(zlogp, zone_name, zoneid) != Z_OK)
48495679c89fSjv227347 goto error;
48505679c89fSjv227347
485162868012SSteve Lawrence if ((bh = brand_open(brand_name)) == NULL) {
48520679f794S zerror(zlogp, B_FALSE,
48530679f794S "unable to determine brand name");
48545679c89fSjv227347 goto error;
48559acbbeafSnn35248 }
48569acbbeafSnn35248
48579a5d73e0SRic Aleshire if (!is_system_labeled() &&
485862868012SSteve Lawrence (strcmp(brand_name, LABELED_BRAND_NAME) == 0)) {
48599a5d73e0SRic Aleshire brand_close(bh);
48609a5d73e0SRic Aleshire zerror(zlogp, B_FALSE,
48619a5d73e0SRic Aleshire "cannot boot labeled zone on unlabeled system");
48629a5d73e0SRic Aleshire goto error;
48639a5d73e0SRic Aleshire }
48649a5d73e0SRic Aleshire
48659acbbeafSnn35248 /*
48669acbbeafSnn35248 * If this brand requires any kernel support, now is the time to
48679acbbeafSnn35248 * get it loaded and initialized.
48689acbbeafSnn35248 */
4869123807fbSedp if (brand_get_modname(bh, modname, MAXPATHLEN) < 0) {
4870e767a340Sgjelinek brand_close(bh);
48710679f794S zerror(zlogp, B_FALSE,
48720679f794S "unable to determine brand kernel module");
48735679c89fSjv227347 goto error;
48749acbbeafSnn35248 }
4875e767a340Sgjelinek brand_close(bh);
48769acbbeafSnn35248
48779acbbeafSnn35248 if (strlen(modname) > 0) {
487862868012SSteve Lawrence (void) strlcpy(attr.ba_brandname, brand_name,
487962868012SSteve Lawrence sizeof (attr.ba_brandname));
488062868012SSteve Lawrence (void) strlcpy(attr.ba_modname, modname,
488162868012SSteve Lawrence sizeof (attr.ba_modname));
48829acbbeafSnn35248 if (zone_setattr(zoneid, ZONE_ATTR_BRAND, &attr,
48839acbbeafSnn35248 sizeof (attr) != 0)) {
48840679f794S zerror(zlogp, B_TRUE,
48850679f794S "could not set zone brand attribute.");
48869acbbeafSnn35248 goto error;
48879acbbeafSnn35248 }
48889acbbeafSnn35248 }
48899acbbeafSnn35248
48905679c89fSjv227347 if (setup_zone_rm(zlogp, zone_name, zoneid) != Z_OK)
48910209230bSgjelinek goto error;
48920209230bSgjelinek
489345916cd2Sjpk set_mlps(zlogp, zoneid, zcent);
48940209230bSgjelinek }
48950209230bSgjelinek
4896108322fbScarlsonj rval = zoneid;
4897108322fbScarlsonj zoneid = -1;
4898108322fbScarlsonj
48997c478bd9Sstevel@tonic-gate error:
49005679c89fSjv227347 if (zoneid != -1) {
49015679c89fSjv227347 (void) zone_shutdown(zoneid);
4902108322fbScarlsonj (void) zone_destroy(zoneid);
49035679c89fSjv227347 }
49047c478bd9Sstevel@tonic-gate if (rctlbuf != NULL)
49057c478bd9Sstevel@tonic-gate free(rctlbuf);
49067c478bd9Sstevel@tonic-gate priv_freeset(privs);
4907108322fbScarlsonj if (fp != NULL)
4908108322fbScarlsonj zonecfg_close_scratch(fp);
4909108322fbScarlsonj lofs_discard_mnttab();
491045916cd2Sjpk if (zcent != NULL)
491145916cd2Sjpk tsol_freezcent(zcent);
49127c478bd9Sstevel@tonic-gate return (rval);
49137c478bd9Sstevel@tonic-gate }
49147c478bd9Sstevel@tonic-gate
4915555afedfScarlsonj /*
4916555afedfScarlsonj * Enter the zone and write a /etc/zones/index file there. This allows
4917555afedfScarlsonj * libzonecfg (and thus zoneadm) to report the UUID and potentially other zone
4918555afedfScarlsonj * details from inside the zone.
4919555afedfScarlsonj */
4920555afedfScarlsonj static void
write_index_file(zoneid_t zoneid)4921555afedfScarlsonj write_index_file(zoneid_t zoneid)
4922555afedfScarlsonj {
4923555afedfScarlsonj FILE *zef;
4924555afedfScarlsonj FILE *zet;
4925555afedfScarlsonj struct zoneent *zep;
4926555afedfScarlsonj pid_t child;
4927555afedfScarlsonj int tmpl_fd;
4928555afedfScarlsonj ctid_t ct;
4929555afedfScarlsonj int fd;
4930555afedfScarlsonj char uuidstr[UUID_PRINTABLE_STRING_LENGTH];
4931555afedfScarlsonj
4932555afedfScarlsonj /* Locate the zone entry in the global zone's index file */
4933555afedfScarlsonj if ((zef = setzoneent()) == NULL)
4934555afedfScarlsonj return;
4935555afedfScarlsonj while ((zep = getzoneent_private(zef)) != NULL) {
4936555afedfScarlsonj if (strcmp(zep->zone_name, zone_name) == 0)
4937555afedfScarlsonj break;
4938555afedfScarlsonj free(zep);
4939555afedfScarlsonj }
4940555afedfScarlsonj endzoneent(zef);
4941555afedfScarlsonj if (zep == NULL)
4942555afedfScarlsonj return;
4943555afedfScarlsonj
4944555afedfScarlsonj if ((tmpl_fd = init_template()) == -1) {
4945555afedfScarlsonj free(zep);
4946555afedfScarlsonj return;
4947555afedfScarlsonj }
4948555afedfScarlsonj
4949555afedfScarlsonj if ((child = fork()) == -1) {
4950555afedfScarlsonj (void) ct_tmpl_clear(tmpl_fd);
4951555afedfScarlsonj (void) close(tmpl_fd);
4952555afedfScarlsonj free(zep);
4953555afedfScarlsonj return;
4954555afedfScarlsonj }
4955555afedfScarlsonj
4956555afedfScarlsonj /* parent waits for child to finish */
4957555afedfScarlsonj if (child != 0) {
4958555afedfScarlsonj free(zep);
4959555afedfScarlsonj if (contract_latest(&ct) == -1)
4960555afedfScarlsonj ct = -1;
4961555afedfScarlsonj (void) ct_tmpl_clear(tmpl_fd);
4962555afedfScarlsonj (void) close(tmpl_fd);
4963555afedfScarlsonj (void) waitpid(child, NULL, 0);
4964555afedfScarlsonj (void) contract_abandon_id(ct);
4965555afedfScarlsonj return;
4966555afedfScarlsonj }
4967555afedfScarlsonj
4968555afedfScarlsonj /* child enters zone and sets up index file */
4969555afedfScarlsonj (void) ct_tmpl_clear(tmpl_fd);
4970555afedfScarlsonj if (zone_enter(zoneid) != -1) {
4971555afedfScarlsonj (void) mkdir(ZONE_CONFIG_ROOT, ZONE_CONFIG_MODE);
4972555afedfScarlsonj (void) chown(ZONE_CONFIG_ROOT, ZONE_CONFIG_UID,
4973555afedfScarlsonj ZONE_CONFIG_GID);
4974555afedfScarlsonj fd = open(ZONE_INDEX_FILE, O_WRONLY|O_CREAT|O_TRUNC,
4975555afedfScarlsonj ZONE_INDEX_MODE);
4976555afedfScarlsonj if (fd != -1 && (zet = fdopen(fd, "w")) != NULL) {
4977555afedfScarlsonj (void) fchown(fd, ZONE_INDEX_UID, ZONE_INDEX_GID);
4978555afedfScarlsonj if (uuid_is_null(zep->zone_uuid))
4979555afedfScarlsonj uuidstr[0] = '\0';
4980555afedfScarlsonj else
4981555afedfScarlsonj uuid_unparse(zep->zone_uuid, uuidstr);
4982555afedfScarlsonj (void) fprintf(zet, "%s:%s:/:%s\n", zep->zone_name,
4983555afedfScarlsonj zone_state_str(zep->zone_state),
4984555afedfScarlsonj uuidstr);
4985555afedfScarlsonj (void) fclose(zet);
4986555afedfScarlsonj }
4987555afedfScarlsonj }
4988555afedfScarlsonj _exit(0);
4989555afedfScarlsonj }
4990555afedfScarlsonj
49917c478bd9Sstevel@tonic-gate int
vplat_bringup(zlog_t * zlogp,zone_mnt_t mount_cmd,zoneid_t zoneid)49926cfd72c6Sgjelinek vplat_bringup(zlog_t *zlogp, zone_mnt_t mount_cmd, zoneid_t zoneid)
49937c478bd9Sstevel@tonic-gate {
49949acbbeafSnn35248 char zonepath[MAXPATHLEN];
49955749802bSdp
49966cfd72c6Sgjelinek if (mount_cmd == Z_MNT_BOOT && validate_datasets(zlogp) != 0) {
4997fa9e4066Sahrens lofs_discard_mnttab();
4998fa9e4066Sahrens return (-1);
4999fa9e4066Sahrens }
5000fa9e4066Sahrens
50019acbbeafSnn35248 /*
50029acbbeafSnn35248 * Before we try to mount filesystems we need to create the
50039acbbeafSnn35248 * attribute backing store for /dev
50049acbbeafSnn35248 */
50059acbbeafSnn35248 if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
50069acbbeafSnn35248 lofs_discard_mnttab();
50079acbbeafSnn35248 return (-1);
50089acbbeafSnn35248 }
500916bca938Svp157776 resolve_lofs(zlogp, zonepath, sizeof (zonepath));
501027e6fb21Sdp
501127e6fb21Sdp /* Make /dev directory owned by root, grouped sys */
501227e6fb21Sdp if (make_one_dir(zlogp, zonepath, "/dev", DEFAULT_DIR_MODE,
501327e6fb21Sdp 0, 3) != 0) {
5014108322fbScarlsonj lofs_discard_mnttab();
50157c478bd9Sstevel@tonic-gate return (-1);
5016108322fbScarlsonj }
5017facf4a8dSllai1
50189acbbeafSnn35248 if (mount_filesystems(zlogp, mount_cmd) != 0) {
5019facf4a8dSllai1 lofs_discard_mnttab();
5020facf4a8dSllai1 return (-1);
5021facf4a8dSllai1 }
5022facf4a8dSllai1
50236cfd72c6Sgjelinek if (mount_cmd == Z_MNT_BOOT) {
5024f4b3ec61Sdh155122 zone_iptype_t iptype;
5025f4b3ec61Sdh155122
50262b24ab6bSSebastien Roy if (vplat_get_iptype(zlogp, &iptype) < 0) {
5027f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "unable to determine ip-type");
5028108322fbScarlsonj lofs_discard_mnttab();
50297c478bd9Sstevel@tonic-gate return (-1);
5030108322fbScarlsonj }
5031555afedfScarlsonj
5032f4b3ec61Sdh155122 switch (iptype) {
5033f4b3ec61Sdh155122 case ZS_SHARED:
5034f4b3ec61Sdh155122 /* Always do this to make lo0 get configured */
5035f4b3ec61Sdh155122 if (configure_shared_network_interfaces(zlogp) != 0) {
5036f4b3ec61Sdh155122 lofs_discard_mnttab();
5037f4b3ec61Sdh155122 return (-1);
5038f4b3ec61Sdh155122 }
5039f4b3ec61Sdh155122 break;
5040f4b3ec61Sdh155122 case ZS_EXCLUSIVE:
5041550b6e40SSowmini Varadhan if (configure_exclusive_network_interfaces(zlogp,
5042550b6e40SSowmini Varadhan zoneid) !=
5043f4b3ec61Sdh155122 0) {
5044f4b3ec61Sdh155122 lofs_discard_mnttab();
5045f4b3ec61Sdh155122 return (-1);
5046f4b3ec61Sdh155122 }
5047f4b3ec61Sdh155122 break;
5048f4b3ec61Sdh155122 }
5049f4b3ec61Sdh155122 }
5050f4b3ec61Sdh155122
5051555afedfScarlsonj write_index_file(zoneid);
5052555afedfScarlsonj
5053108322fbScarlsonj lofs_discard_mnttab();
50547c478bd9Sstevel@tonic-gate return (0);
50557c478bd9Sstevel@tonic-gate }
50567c478bd9Sstevel@tonic-gate
5057108322fbScarlsonj static int
lu_root_teardown(zlog_t * zlogp)5058108322fbScarlsonj lu_root_teardown(zlog_t *zlogp)
50597c478bd9Sstevel@tonic-gate {
5060108322fbScarlsonj char zroot[MAXPATHLEN];
5061108322fbScarlsonj
5062108322fbScarlsonj if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
5063108322fbScarlsonj zerror(zlogp, B_FALSE, "unable to determine zone root");
5064108322fbScarlsonj return (-1);
5065108322fbScarlsonj }
5066108322fbScarlsonj root_to_lu(zlogp, zroot, sizeof (zroot), B_FALSE);
5067108322fbScarlsonj
5068108322fbScarlsonj /*
5069108322fbScarlsonj * At this point, the processes are gone, the filesystems (save the
5070108322fbScarlsonj * root) are unmounted, and the zone is on death row. But there may
5071108322fbScarlsonj * still be creds floating about in the system that reference the
5072108322fbScarlsonj * zone_t, and which pin down zone_rootvp causing this call to fail
5073108322fbScarlsonj * with EBUSY. Thus, we try for a little while before just giving up.
5074108322fbScarlsonj * (How I wish this were not true, and umount2 just did the right
5075108322fbScarlsonj * thing, or tmpfs supported MS_FORCE This is a gross hack.)
5076108322fbScarlsonj */
5077108322fbScarlsonj if (umount2(zroot, MS_FORCE) != 0) {
5078108322fbScarlsonj if (errno == ENOTSUP && umount2(zroot, 0) == 0)
5079108322fbScarlsonj goto unmounted;
5080108322fbScarlsonj if (errno == EBUSY) {
5081108322fbScarlsonj int tries = 10;
5082108322fbScarlsonj
5083108322fbScarlsonj while (--tries >= 0) {
5084108322fbScarlsonj (void) sleep(1);
5085108322fbScarlsonj if (umount2(zroot, 0) == 0)
5086108322fbScarlsonj goto unmounted;
5087108322fbScarlsonj if (errno != EBUSY)
5088108322fbScarlsonj break;
5089108322fbScarlsonj }
5090108322fbScarlsonj }
5091108322fbScarlsonj zerror(zlogp, B_TRUE, "unable to unmount '%s'", zroot);
5092108322fbScarlsonj return (-1);
5093108322fbScarlsonj }
5094108322fbScarlsonj unmounted:
5095108322fbScarlsonj
5096108322fbScarlsonj /*
5097108322fbScarlsonj * Only zones in an alternate root environment have scratch zone
5098108322fbScarlsonj * entries.
5099108322fbScarlsonj */
5100108322fbScarlsonj if (zonecfg_in_alt_root()) {
5101108322fbScarlsonj FILE *fp;
5102108322fbScarlsonj int retv;
5103108322fbScarlsonj
5104108322fbScarlsonj if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5105108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot open mapfile");
5106108322fbScarlsonj return (-1);
5107108322fbScarlsonj }
5108108322fbScarlsonj retv = -1;
5109108322fbScarlsonj if (zonecfg_lock_scratch(fp) != 0)
5110108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot lock mapfile");
5111108322fbScarlsonj else if (zonecfg_delete_scratch(fp, kernzone) != 0)
5112108322fbScarlsonj zerror(zlogp, B_TRUE, "cannot delete map entry");
5113108322fbScarlsonj else
5114108322fbScarlsonj retv = 0;
5115108322fbScarlsonj zonecfg_close_scratch(fp);
5116108322fbScarlsonj return (retv);
5117108322fbScarlsonj } else {
5118108322fbScarlsonj return (0);
5119108322fbScarlsonj }
5120108322fbScarlsonj }
5121108322fbScarlsonj
5122108322fbScarlsonj int
vplat_teardown(zlog_t * zlogp,boolean_t unmount_cmd,boolean_t rebooting)51230209230bSgjelinek vplat_teardown(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
5124108322fbScarlsonj {
5125108322fbScarlsonj char *kzone;
51267c478bd9Sstevel@tonic-gate zoneid_t zoneid;
51270209230bSgjelinek int res;
51280209230bSgjelinek char pool_err[128];
5129ff17c8bfSgjelinek char zpath[MAXPATHLEN];
51309acbbeafSnn35248 char cmdbuf[MAXPATHLEN];
5131123807fbSedp brand_handle_t bh = NULL;
51322b24ab6bSSebastien Roy dladm_status_t status;
51332b24ab6bSSebastien Roy char errmsg[DLADM_STRSIZE];
5134f4b3ec61Sdh155122 ushort_t flags;
51357c478bd9Sstevel@tonic-gate
5136108322fbScarlsonj kzone = zone_name;
5137108322fbScarlsonj if (zonecfg_in_alt_root()) {
5138108322fbScarlsonj FILE *fp;
5139108322fbScarlsonj
5140108322fbScarlsonj if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
5141108322fbScarlsonj zerror(zlogp, B_TRUE, "unable to open map file");
5142108322fbScarlsonj goto error;
5143108322fbScarlsonj }
5144108322fbScarlsonj if (zonecfg_find_scratch(fp, zone_name, zonecfg_get_root(),
5145108322fbScarlsonj kernzone, sizeof (kernzone)) != 0) {
5146108322fbScarlsonj zerror(zlogp, B_FALSE, "unable to find scratch zone");
5147108322fbScarlsonj zonecfg_close_scratch(fp);
5148108322fbScarlsonj goto error;
5149108322fbScarlsonj }
5150108322fbScarlsonj zonecfg_close_scratch(fp);
5151108322fbScarlsonj kzone = kernzone;
5152108322fbScarlsonj }
5153108322fbScarlsonj
5154108322fbScarlsonj if ((zoneid = getzoneidbyname(kzone)) == ZONE_ID_UNDEFINED) {
51557c478bd9Sstevel@tonic-gate if (!bringup_failure_recovery)
51567c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to get zoneid");
5157108322fbScarlsonj if (unmount_cmd)
5158108322fbScarlsonj (void) lu_root_teardown(zlogp);
51597c478bd9Sstevel@tonic-gate goto error;
51607c478bd9Sstevel@tonic-gate }
51617c478bd9Sstevel@tonic-gate
51620dc2366fSVenugopal Iyer if (remove_datalink_pool(zlogp, zoneid) != 0) {
51630dc2366fSVenugopal Iyer zerror(zlogp, B_FALSE, "unable clear datalink pool property");
51640dc2366fSVenugopal Iyer goto error;
51650dc2366fSVenugopal Iyer }
51660dc2366fSVenugopal Iyer
5167550b6e40SSowmini Varadhan if (remove_datalink_protect(zlogp, zoneid) != 0) {
5168550b6e40SSowmini Varadhan zerror(zlogp, B_FALSE,
5169550b6e40SSowmini Varadhan "unable clear datalink protect property");
5170550b6e40SSowmini Varadhan goto error;
5171550b6e40SSowmini Varadhan }
5172550b6e40SSowmini Varadhan
5173550b6e40SSowmini Varadhan /*
5174550b6e40SSowmini Varadhan * The datalinks assigned to the zone will be removed from the NGZ as
5175550b6e40SSowmini Varadhan * part of zone_shutdown() so that we need to remove protect/pool etc.
5176550b6e40SSowmini Varadhan * before zone_shutdown(). Even if the shutdown itself fails, the zone
5177550b6e40SSowmini Varadhan * will not be able to violate any constraints applied because the
5178550b6e40SSowmini Varadhan * datalinks are no longer available to the zone.
5179550b6e40SSowmini Varadhan */
51807c478bd9Sstevel@tonic-gate if (zone_shutdown(zoneid) != 0) {
51817c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to shutdown zone");
51827c478bd9Sstevel@tonic-gate goto error;
51837c478bd9Sstevel@tonic-gate }
51847c478bd9Sstevel@tonic-gate
5185ff17c8bfSgjelinek /* Get the zonepath of this zone */
5186ff17c8bfSgjelinek if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
5187ff17c8bfSgjelinek zerror(zlogp, B_FALSE, "unable to determine zone path");
51889acbbeafSnn35248 goto error;
51899acbbeafSnn35248 }
51909acbbeafSnn35248
51919acbbeafSnn35248 /* Get a handle to the brand info for this zone */
519262868012SSteve Lawrence if ((bh = brand_open(brand_name)) == NULL) {
51939acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine zone brand");
51949acbbeafSnn35248 return (-1);
51959acbbeafSnn35248 }
51969acbbeafSnn35248 /*
51979acbbeafSnn35248 * If there is a brand 'halt' callback, execute it now to give the
51989acbbeafSnn35248 * brand a chance to cleanup any custom configuration.
51999acbbeafSnn35248 */
52009acbbeafSnn35248 (void) strcpy(cmdbuf, EXEC_PREFIX);
5201ff17c8bfSgjelinek if (brand_get_halt(bh, zone_name, zpath, cmdbuf + EXEC_LEN,
5202ff17c8bfSgjelinek sizeof (cmdbuf) - EXEC_LEN) < 0) {
5203123807fbSedp brand_close(bh);
52049acbbeafSnn35248 zerror(zlogp, B_FALSE, "unable to determine branded zone's "
52059acbbeafSnn35248 "halt callback.");
52069acbbeafSnn35248 goto error;
52079acbbeafSnn35248 }
5208123807fbSedp brand_close(bh);
52099acbbeafSnn35248
52109acbbeafSnn35248 if ((strlen(cmdbuf) > EXEC_LEN) &&
5211c5cd6260S (do_subproc(zlogp, cmdbuf, NULL) != Z_OK)) {
52129acbbeafSnn35248 zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
52139acbbeafSnn35248 goto error;
52149acbbeafSnn35248 }
52159acbbeafSnn35248
5216f4b3ec61Sdh155122 if (!unmount_cmd) {
5217f4b3ec61Sdh155122 zone_iptype_t iptype;
5218f4b3ec61Sdh155122
5219f4b3ec61Sdh155122 if (zone_getattr(zoneid, ZONE_ATTR_FLAGS, &flags,
5220f4b3ec61Sdh155122 sizeof (flags)) < 0) {
52212b24ab6bSSebastien Roy if (vplat_get_iptype(zlogp, &iptype) < 0) {
5222f4b3ec61Sdh155122 zerror(zlogp, B_TRUE, "unable to determine "
5223f4b3ec61Sdh155122 "ip-type");
52247c478bd9Sstevel@tonic-gate goto error;
52257c478bd9Sstevel@tonic-gate }
5226f4b3ec61Sdh155122 } else {
5227f4b3ec61Sdh155122 if (flags & ZF_NET_EXCL)
5228f4b3ec61Sdh155122 iptype = ZS_EXCLUSIVE;
5229f4b3ec61Sdh155122 else
5230f4b3ec61Sdh155122 iptype = ZS_SHARED;
5231f4b3ec61Sdh155122 }
5232f4b3ec61Sdh155122
5233f4b3ec61Sdh155122 switch (iptype) {
5234f4b3ec61Sdh155122 case ZS_SHARED:
5235f4b3ec61Sdh155122 if (unconfigure_shared_network_interfaces(zlogp,
5236f4b3ec61Sdh155122 zoneid) != 0) {
5237f4b3ec61Sdh155122 zerror(zlogp, B_FALSE, "unable to unconfigure "
5238f4b3ec61Sdh155122 "network interfaces in zone");
5239f4b3ec61Sdh155122 goto error;
5240f4b3ec61Sdh155122 }
5241f4b3ec61Sdh155122 break;
5242f4b3ec61Sdh155122 case ZS_EXCLUSIVE:
5243f4b3ec61Sdh155122 if (unconfigure_exclusive_network_interfaces(zlogp,
5244f4b3ec61Sdh155122 zoneid) != 0) {
5245f4b3ec61Sdh155122 zerror(zlogp, B_FALSE, "unable to unconfigure "
5246f4b3ec61Sdh155122 "network interfaces in zone");
5247f4b3ec61Sdh155122 goto error;
5248f4b3ec61Sdh155122 }
52492b24ab6bSSebastien Roy status = dladm_zone_halt(dld_handle, zoneid);
52502b24ab6bSSebastien Roy if (status != DLADM_STATUS_OK) {
52512b24ab6bSSebastien Roy zerror(zlogp, B_FALSE, "unable to notify "
52522b24ab6bSSebastien Roy "dlmgmtd of zone halt: %s",
52532b24ab6bSSebastien Roy dladm_status2str(status, errmsg));
52542b24ab6bSSebastien Roy }
5255f4b3ec61Sdh155122 break;
5256f4b3ec61Sdh155122 }
5257f4b3ec61Sdh155122 }
52587c478bd9Sstevel@tonic-gate
5259108322fbScarlsonj if (!unmount_cmd && tcp_abort_connections(zlogp, zoneid) != 0) {
52607c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to abort TCP connections");
52617c478bd9Sstevel@tonic-gate goto error;
52627c478bd9Sstevel@tonic-gate }
52637c478bd9Sstevel@tonic-gate
5264108322fbScarlsonj if (unmount_filesystems(zlogp, zoneid, unmount_cmd) != 0) {
52657c478bd9Sstevel@tonic-gate zerror(zlogp, B_FALSE,
52667c478bd9Sstevel@tonic-gate "unable to unmount file systems in zone");
52677c478bd9Sstevel@tonic-gate goto error;
52687c478bd9Sstevel@tonic-gate }
52697c478bd9Sstevel@tonic-gate
52700209230bSgjelinek /*
5271cf6b13d2Sgjelinek * If we are rebooting then we normally don't want to destroy an
5272cf6b13d2Sgjelinek * existing temporary pool at this point so that we can just reuse it
5273cf6b13d2Sgjelinek * when the zone boots back up. However, it is also possible we were
5274cf6b13d2Sgjelinek * running with a temporary pool and the zone configuration has been
5275cf6b13d2Sgjelinek * modified to no longer use a temporary pool. In that case we need
5276cf6b13d2Sgjelinek * to destroy the temporary pool now. This case looks like the case
5277cf6b13d2Sgjelinek * where we never had a temporary pool configured but
5278cf6b13d2Sgjelinek * zonecfg_destroy_tmp_pool will do the right thing either way.
52790209230bSgjelinek */
5280cf6b13d2Sgjelinek if (!unmount_cmd) {
5281cf6b13d2Sgjelinek boolean_t destroy_tmp_pool = B_TRUE;
5282cf6b13d2Sgjelinek
5283cf6b13d2Sgjelinek if (rebooting) {
5284cf6b13d2Sgjelinek struct zone_psettab pset_tab;
5285cf6b13d2Sgjelinek zone_dochandle_t handle;
5286cf6b13d2Sgjelinek
5287cf6b13d2Sgjelinek if ((handle = zonecfg_init_handle()) != NULL &&
5288cf6b13d2Sgjelinek zonecfg_get_handle(zone_name, handle) == Z_OK &&
5289cf6b13d2Sgjelinek zonecfg_lookup_pset(handle, &pset_tab) == Z_OK)
5290cf6b13d2Sgjelinek destroy_tmp_pool = B_FALSE;
5291e767a340Sgjelinek
5292e767a340Sgjelinek zonecfg_fini_handle(handle);
5293cf6b13d2Sgjelinek }
5294cf6b13d2Sgjelinek
5295cf6b13d2Sgjelinek if (destroy_tmp_pool) {
52960209230bSgjelinek if ((res = zonecfg_destroy_tmp_pool(zone_name, pool_err,
52970209230bSgjelinek sizeof (pool_err))) != Z_OK) {
52980209230bSgjelinek if (res == Z_POOL)
52990209230bSgjelinek zerror(zlogp, B_FALSE, pool_err);
53000209230bSgjelinek }
53010209230bSgjelinek }
5302cf6b13d2Sgjelinek }
53030209230bSgjelinek
530445916cd2Sjpk remove_mlps(zlogp, zoneid);
530545916cd2Sjpk
53067c478bd9Sstevel@tonic-gate if (zone_destroy(zoneid) != 0) {
53077c478bd9Sstevel@tonic-gate zerror(zlogp, B_TRUE, "unable to destroy zone");
53087c478bd9Sstevel@tonic-gate goto error;
53097c478bd9Sstevel@tonic-gate }
5310108322fbScarlsonj
5311108322fbScarlsonj /*
5312108322fbScarlsonj * Special teardown for alternate boot environments: remove the tmpfs
5313108322fbScarlsonj * root for the zone and then remove it from the map file.
5314108322fbScarlsonj */
5315108322fbScarlsonj if (unmount_cmd && lu_root_teardown(zlogp) != 0)
5316108322fbScarlsonj goto error;
5317108322fbScarlsonj
5318108322fbScarlsonj lofs_discard_mnttab();
53197c478bd9Sstevel@tonic-gate return (0);
53207c478bd9Sstevel@tonic-gate
53217c478bd9Sstevel@tonic-gate error:
5322108322fbScarlsonj lofs_discard_mnttab();
53237c478bd9Sstevel@tonic-gate return (-1);
53247c478bd9Sstevel@tonic-gate }
5325