2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/dirent.h>
49 #include <sys/kernel.h>
51 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/rmlock.h>
56 #include <sys/refcount.h>
57 #include <sys/signalvar.h>
58 #include <sys/socket.h>
59 #include <sys/vnode.h>
61 #include <netinet/in.h>
62 #include <net/radix.h>
64 static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure");
66 #if defined(INET) || defined(INET6)
67 static struct radix_node_head *vfs_create_addrlist_af(
68 struct radix_node_head **prnh, int off);
70 static void vfs_free_addrlist(struct netexport *nep);
71 static int vfs_free_netcred(struct radix_node *rn, void *w);
72 static void vfs_free_addrlist_af(struct radix_node_head **prnh);
73 static int vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
74 struct export_args *argp);
75 static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *);
78 * Network address lookup element
81 struct radix_node netc_rnodes[2];
83 struct ucred *netc_anon;
84 int netc_numsecflavors;
85 int netc_secflavors[MAXSECFLAVORS];
89 * Network export information
92 struct netcred ne_defexported; /* Default export */
93 struct radix_node_head *ne4;
94 struct radix_node_head *ne6;
98 * Build hash lists of net addresses and hang them off the mount point.
99 * Called by vfs_export() to set up the lists of export addresses.
102 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
103 struct export_args *argp)
106 struct radix_node_head *rnh;
108 struct radix_node *rn;
109 struct sockaddr *saddr, *smask = NULL;
110 #if defined(INET6) || defined(INET)
116 * XXX: This routine converts from a `struct xucred'
117 * (argp->ex_anon) to a `struct ucred' (np->netc_anon). This
118 * operation is questionable; for example, what should be done
119 * with fields like cr_uidinfo and cr_prison? Currently, this
120 * routine does not touch them (leaves them as NULL).
122 if (argp->ex_anon.cr_version != XUCRED_VERSION) {
123 vfs_mount_error(mp, "ex_anon.cr_version: %d != %d",
124 argp->ex_anon.cr_version, XUCRED_VERSION);
128 if (argp->ex_addrlen == 0) {
129 if (mp->mnt_flag & MNT_DEFEXPORTED) {
131 "MNT_DEFEXPORTED already set for mount %p", mp);
134 np = &nep->ne_defexported;
135 np->netc_exflags = argp->ex_flags;
136 np->netc_anon = crget();
137 np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
138 crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups,
139 argp->ex_anon.cr_groups);
140 np->netc_anon->cr_prison = &prison0;
141 prison_hold(np->netc_anon->cr_prison);
142 np->netc_numsecflavors = argp->ex_numsecflavors;
143 bcopy(argp->ex_secflavors, np->netc_secflavors,
144 sizeof(np->netc_secflavors));
146 mp->mnt_flag |= MNT_DEFEXPORTED;
152 if (argp->ex_addrlen > MLEN) {
153 vfs_mount_error(mp, "ex_addrlen %d is greater than %d",
154 argp->ex_addrlen, MLEN);
159 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
160 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
161 saddr = (struct sockaddr *) (np + 1);
162 if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
164 if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) {
166 vfs_mount_error(mp, "Invalid saddr->sa_family: %d");
169 if (saddr->sa_len > argp->ex_addrlen)
170 saddr->sa_len = argp->ex_addrlen;
171 if (argp->ex_masklen) {
172 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
173 error = copyin(argp->ex_mask, smask, argp->ex_masklen);
176 if (smask->sa_len > argp->ex_masklen)
177 smask->sa_len = argp->ex_masklen;
180 switch (saddr->sa_family) {
183 if ((rnh = nep->ne4) == NULL) {
184 off = offsetof(struct sockaddr_in, sin_addr) << 3;
185 rnh = vfs_create_addrlist_af(&nep->ne4, off);
191 if ((rnh = nep->ne6) == NULL) {
192 off = offsetof(struct sockaddr_in6, sin6_addr) << 3;
193 rnh = vfs_create_addrlist_af(&nep->ne6, off);
200 vfs_mount_error(mp, "%s %s %d",
201 "Unable to initialize radix node head ",
202 "for address family", saddr->sa_family);
205 RADIX_NODE_HEAD_LOCK(rnh);
206 rn = (*rnh->rnh_addaddr)(saddr, smask, &rnh->rh, np->netc_rnodes);
207 RADIX_NODE_HEAD_UNLOCK(rnh);
208 if (rn == NULL || np != (struct netcred *)rn) { /* already exists */
211 "netcred already exists for given addr/mask");
214 np->netc_exflags = argp->ex_flags;
215 np->netc_anon = crget();
216 np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
217 crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups,
218 argp->ex_anon.cr_groups);
219 np->netc_anon->cr_prison = &prison0;
220 prison_hold(np->netc_anon->cr_prison);
221 np->netc_numsecflavors = argp->ex_numsecflavors;
222 bcopy(argp->ex_secflavors, np->netc_secflavors,
223 sizeof(np->netc_secflavors));
230 /* Helper for vfs_free_addrlist. */
233 vfs_free_netcred(struct radix_node *rn, void *w)
235 struct radix_node_head *rnh = (struct radix_node_head *) w;
238 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, &rnh->rh);
239 cred = ((struct netcred *)rn)->netc_anon;
246 #if defined(INET) || defined(INET6)
247 static struct radix_node_head *
248 vfs_create_addrlist_af(struct radix_node_head **prnh, int off)
251 if (rn_inithead((void **)prnh, off) == 0)
253 RADIX_NODE_HEAD_LOCK_INIT(*prnh);
259 vfs_free_addrlist_af(struct radix_node_head **prnh)
261 struct radix_node_head *rnh;
264 RADIX_NODE_HEAD_LOCK(rnh);
265 (*rnh->rnh_walktree)(&rnh->rh, vfs_free_netcred, rnh);
266 RADIX_NODE_HEAD_UNLOCK(rnh);
267 RADIX_NODE_HEAD_DESTROY(rnh);
268 rn_detachhead((void **)prnh);
273 * Free the net address hash lists that are hanging off the mount points.
276 vfs_free_addrlist(struct netexport *nep)
280 if (nep->ne4 != NULL)
281 vfs_free_addrlist_af(&nep->ne4);
282 if (nep->ne6 != NULL)
283 vfs_free_addrlist_af(&nep->ne6);
285 cred = nep->ne_defexported.netc_anon;
292 * High level function to manipulate export options on a mount point
293 * and the passed in netexport.
294 * Struct export_args *argp is the variable used to twiddle options,
295 * the structure is described in sys/mount.h
298 vfs_export(struct mount *mp, struct export_args *argp)
300 struct netexport *nep;
303 if (argp->ex_numsecflavors < 0
304 || argp->ex_numsecflavors >= MAXSECFLAVORS)
308 lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL);
309 nep = mp->mnt_export;
310 if (argp->ex_flags & MNT_DELEXPORT) {
315 if (mp->mnt_flag & MNT_EXPUBLIC) {
316 vfs_setpublicfs(NULL, NULL, NULL);
318 mp->mnt_flag &= ~MNT_EXPUBLIC;
321 vfs_free_addrlist(nep);
322 mp->mnt_export = NULL;
326 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
329 if (argp->ex_flags & MNT_EXPORTED) {
331 nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
332 mp->mnt_export = nep;
334 if (argp->ex_flags & MNT_EXPUBLIC) {
335 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
338 mp->mnt_flag |= MNT_EXPUBLIC;
341 if ((error = vfs_hang_addrlist(mp, nep, argp)))
344 mp->mnt_flag |= MNT_EXPORTED;
349 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
351 * Once we have executed the vfs_export() command, we do
352 * not want to keep the "export" option around in the
353 * options list, since that will cause subsequent MNT_UPDATE
354 * calls to fail. The export information is saved in
355 * mp->mnt_export, so we can safely delete the "export" mount option
358 vfs_deleteopt(mp->mnt_optnew, "export");
359 vfs_deleteopt(mp->mnt_opt, "export");
364 * Set the publicly exported filesystem (WebNFS). Currently, only
365 * one public filesystem is possible in the spec (RFC 2054 and 2055)
368 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
369 struct export_args *argp)
376 * mp == NULL -> invalidate the current info, the FS is
377 * no longer exported. May be called from either vfs_export
378 * or unmount, so check if it hasn't already been done.
381 if (nfs_pub.np_valid) {
382 nfs_pub.np_valid = 0;
383 if (nfs_pub.np_index != NULL) {
384 free(nfs_pub.np_index, M_TEMP);
385 nfs_pub.np_index = NULL;
392 * Only one allowed at a time.
394 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
398 * Get real filehandle for root of exported FS.
400 bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
401 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
403 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp)))
406 if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
412 * If an indexfile was specified, pull it in.
414 if (argp->ex_indexfile != NULL) {
415 if (nfs_pub.np_index == NULL)
416 nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP,
418 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
419 MAXNAMLEN, (size_t *)0);
422 * Check for illegal filenames.
424 for (cp = nfs_pub.np_index; *cp; cp++) {
432 free(nfs_pub.np_index, M_TEMP);
433 nfs_pub.np_index = NULL;
438 nfs_pub.np_mount = mp;
439 nfs_pub.np_valid = 1;
444 * Used by the filesystems to determine if a given network address
445 * (passed in 'nam') is present in their exports list, returns a pointer
446 * to struct netcred so that the filesystem can examine it for
447 * access rights (read/write/etc).
449 static struct netcred *
450 vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
452 RADIX_NODE_HEAD_RLOCK_TRACKER;
453 struct netexport *nep;
454 struct netcred *np = NULL;
455 struct radix_node_head *rnh;
456 struct sockaddr *saddr;
458 nep = mp->mnt_export;
461 if ((mp->mnt_flag & MNT_EXPORTED) == 0)
465 * Lookup in the export list
470 switch (saddr->sa_family) {
479 RADIX_NODE_HEAD_RLOCK(rnh);
480 np = (struct netcred *) (*rnh->rnh_matchaddr)(saddr, &rnh->rh);
481 RADIX_NODE_HEAD_RUNLOCK(rnh);
482 if (np != NULL && (np->netc_rnodes->rn_flags & RNF_ROOT) != 0)
488 * If no address match, use the default if it exists.
490 if (np == NULL && (mp->mnt_flag & MNT_DEFEXPORTED) != 0)
491 return (&nep->ne_defexported);
497 * XXX: This comment comes from the deprecated ufs_check_export()
498 * XXX: and may not entirely apply, but lacking something better:
499 * This is the generic part of fhtovp called after the underlying
500 * filesystem has validated the file handle.
502 * Verify that a host should have access to a filesystem.
506 vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
507 struct ucred **credanonp, int *numsecflavors, int **secflavors)
511 lockmgr(&mp->mnt_explock, LK_SHARED, NULL);
512 np = vfs_export_lookup(mp, nam);
514 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
518 *extflagsp = np->netc_exflags;
519 if ((*credanonp = np->netc_anon) != NULL)
522 *numsecflavors = np->netc_numsecflavors;
524 *secflavors = np->netc_secflavors;
525 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);