/*-
* Copyright (c) 1997, 1998, 2004, 2005, 2008, 2019 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
* This code is derived from software contributed to The NetBSD Foundation
* by Charles M. Hannum.
* This code is derived from software contributed to The NetBSD Foundation
* by Julio M. Merino Vidal.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)vfs_subr.c 8.13 (Berkeley) 4/18/94
*/
/*
* VFS unmount hook for NFS exports.
*
* Releases NFS exports list resources if the given mount point has some.
* As allocation happens lazily, it may be that it doesn't have this
* information, although it theoretically should.
*/
static void
netexport_unmount(struct mount *mp)
{
struct netexport *ne;
KASSERT(mp != NULL);
netexport_wrlock();
ne = netexport_lookup(mp);
if (ne == NULL) {
netexport_wrunlock();
return;
}
netexport_clear(ne);
netexport_remove(ne);
netexport_wrunlock();
kmem_free(ne, sizeof(*ne));
}
void
netexport_init(void)
{
rw_init(&netexport_lock);
}
void
netexport_fini(void)
{
struct netexport *ne;
struct mount *mp;
int error;
/*
* Atomically set the NFS exports list of the given file system, replacing
* it with a new list of entries.
*
* Returns zero on success or an appropriate error code otherwise.
*
* Helper function for the nfssvc(2) system call (NFSSVC_SETEXPORTSLIST
* and NFSSVC_REPLACEEXPORTSLIST command).
*/
int
mountd_set_exports_list(const struct mountd_exports_list *mel, struct lwp *l,
struct mount *nmp, int cmd)
{
int error;
size_t i;
struct mount *mp;
struct netexport *ne;
struct pathbuf *pb;
struct nameidata nd;
struct vnode *vp;
size_t fid_size;
/* Look up the file system path. */
error = pathbuf_copyin(mel->mel_path, &pb);
if (error) {
return error;
}
NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, pb);
error = namei(&nd);
if (error != 0) {
pathbuf_destroy(pb);
return error;
}
vp = nd.ni_vp;
mp = vp->v_mount;
KASSERT(nmp == NULL || nmp == mp);
pathbuf_destroy(pb);
/*
* Make sure the file system can do vptofh. If the file system
* knows the handle's size, just trust it's able to do the
* actual translation also (otherwise we should check fhtovp
* also, and that's getting a wee bit ridiculous).
*/
fid_size = 0;
if ((error = VFS_VPTOFH(vp, NULL, &fid_size)) != E2BIG) {
vput(vp);
return EOPNOTSUPP;
}
/* Mark the file system busy. */
error = vfs_busy(mp);
vput(vp);
if (error != 0)
return error;
if (nmp == NULL)
mutex_enter(mp->mnt_updating); /* mnt_flag */
netexport_wrlock();
ne = netexport_lookup(mp);
if (ne == NULL) {
error = init_exports(mp, &ne);
if (error != 0) {
goto out;
}
}
KASSERT(ne != NULL);
KASSERT(ne->ne_mount == mp);
if (cmd == NFSSVC_SETEXPORTSLIST) {
if (mel->mel_nexports == 0)
netexport_clear(ne);
else if (mel->mel_nexports == 1)
error = export(ne, &mel->mel_exports[0]);
else {
printf("%s: Cannot set more than one "
"entry at once (unimplemented)\n", __func__);
error = EOPNOTSUPP;
}
} else if (cmd == NFSSVC_REPLACEEXPORTSLIST) {
netexport_clear(ne);
for (i = 0; error == 0 && i < mel->mel_nexports; i++)
error = export(ne, &mel->mel_exports[i]);
} else {
printf("%s: Command %#x not implemented\n", __func__, cmd);
error = EOPNOTSUPP;
}
TAILQ_FOREACH(ne, &netexport_list, ne_list) {
const struct mount *mp = ne->ne_mount;
if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] &&
mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) {
goto done;
}
}
ne = NULL;
done:
return ne;
}
/*
* Check if the file system specified by the 'mp' mount structure is
* exported to a client with 'anon' anonymous credentials. The 'mb'
* argument is an mbuf containing the network address of the client.
* The return parameters for the export flags for the client are returned
* in the address specified by 'wh'.
*
* This function is used exclusively by the NFS server. It is generally
* invoked before VFS_FHTOVP to validate that a client has access to the
* file system.
*/
int
netexport_check(const fsid_t *fsid, struct mbuf *mb, struct mount **mpp,
int *wh, kauth_cred_t *anon)
{
struct netexport *ne;
struct netcred *np;
ne = netexport_lookup_byfsid(fsid);
if (ne == NULL) {
return EACCES;
}
np = netcred_lookup(ne, mb);
if (np == NULL) {
return EACCES;
}
/*
* Handles legacy export requests. In this case, the export information
* is hardcoded in a specific place of the mount arguments structure (given
* in data); the request for an update is given through the fspec field
* (also in a known location), which must be a null pointer.
*
* Returns EJUSTRETURN if the given command was not a export request.
* Otherwise, returns 0 on success or an appropriate error code otherwise.
*/
static int
nfs_export_update_30(struct mount *mp, const char *path, void *data)
{
struct mountd_exports_list mel;
struct mnt_export_args30 *args;
args = data;
mel.mel_path = path;
if (args->fspec != NULL)
return EJUSTRETURN;
if (args->eargs.ex_flags & 0x00020000) {
/* Request to delete exports. The mask above holds the
* value that used to be in MNT_DELEXPORT. */
mel.mel_nexports = 0;
} else {
/*
* The following code assumes export_args has not
* changed since export_args30, so check that.
*/
__CTASSERT(sizeof(args->eargs) == sizeof(*mel.mel_exports));
/*
* Initializes NFS exports for the mountpoint given in 'mp'.
* If successful, returns 0 and sets *nep to the address of the new
* netexport item; otherwise returns an appropriate error code
* and *nep remains unmodified.
*/
static int
init_exports(struct mount *mp, struct netexport **nep)
{
int error;
struct export_args ea;
struct netexport *ne;
KASSERT(mp != NULL);
/* Ensure that we do not already have this mount point. */
KASSERT(netexport_lookup(mp) == NULL);
ne = kmem_zalloc(sizeof(*ne), KM_SLEEP);
ne->ne_mount = mp;
/* Set the default export entry. Handled internally by export upon
* first call. */
memset(&ea, 0, sizeof(ea));
ea.ex_root = -2;
if (mp->mnt_flag & MNT_RDONLY)
ea.ex_flags |= MNT_EXRDONLY;
error = export(ne, &ea);
if (error != 0) {
kmem_free(ne, sizeof(*ne));
} else {
netexport_insert(ne);
*nep = ne;
}
return error;
}
/*
* Build hash lists of net addresses and hang them off the mount point.
* Called by export() to set up a new entry in the lists of export
* addresses.
*/
static int
hang_addrlist(struct mount *mp, struct netexport *nep,
const struct export_args *argp)
{
int error, i;
struct netcred *np, *enp;
struct radix_node_head *rnh;
struct sockaddr *saddr, *smask;
struct domain *dom;
/*
* Clears the exports list for a given file system.
*/
static void
netexport_clear(struct netexport *ne)
{
struct radix_node_head *rnh;
struct mount *mp = ne->ne_mount;
int i;
/*
* Add a new export entry (described by an export_args structure) to the
* given file system.
*/
static int
export(struct netexport *nep, const struct export_args *argp)
{
struct mount *mp = nep->ne_mount;
int error;
/*
* Set the publicly exported filesystem (WebNFS). Currently, only
* one public filesystem is possible in the spec (RFC 2054 and 2055)
*/
static int
setpublicfs(struct mount *mp, struct netexport *nep,
const struct export_args *argp)
{
char *cp;
int error;
struct vnode *rvp;
size_t fhsize;
/*
* mp == NULL --> invalidate the current info; the FS is
* no longer exported. May be called from either export
* or unmount, so check if it hasn't already been done.
*/
if (mp == NULL) {
if (nfs_pub.np_valid) {
nfs_pub.np_valid = 0;
if (nfs_pub.np_handle != NULL) {
free(nfs_pub.np_handle, M_TEMP);
nfs_pub.np_handle = NULL;
}
if (nfs_pub.np_index != NULL) {
free(nfs_pub.np_index, M_TEMP);
nfs_pub.np_index = NULL;
}
}
return 0;
}
/*
* Only one allowed at a time.
*/
if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
return EBUSY;
/*
* Get real filehandle for root of exported FS.
*/
if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp)))
return error;
/*
* Look up an export entry in the exports list that matches the address
* stored in 'nam'. If no entry is found, the default one is used instead
* (if available).
*/
static struct netcred *
netcred_lookup(struct netexport *ne, struct mbuf *nam)
{
struct netcred *np;
struct radix_node_head *rnh;
struct sockaddr *saddr;
if ((ne->ne_mount->mnt_flag & MNT_EXPORTED) == 0) {
return NULL;
}
/*
* Look in the export list first.
*/
np = NULL;
if (nam != NULL) {
saddr = mtod(nam, struct sockaddr *);
rnh = ne->ne_rtable[saddr->sa_family];
if (rnh != NULL) {
np = (struct netcred *)
(*rnh->rnh_matchaddr)((void *)saddr,
rnh);
if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
np = NULL;
}
}
/*
* If no address match, use the default if it exists.
*/
if (np == NULL && ne->ne_mount->mnt_flag & MNT_DEFEXPORTED)
np = &ne->ne_defexported;