19454b2d8SWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1989, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 7df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 8df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 9df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 11df8bae1dSRodney W. Grimes * 12df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 13df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 14df8bae1dSRodney W. Grimes * are met: 15df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 17df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 19df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 2069a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors 21df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 22df8bae1dSRodney W. Grimes * without specific prior written permission. 23df8bae1dSRodney W. Grimes * 24df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34df8bae1dSRodney W. Grimes * SUCH DAMAGE. 35df8bae1dSRodney W. Grimes * 36996c772fSJohn Dyson * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 37df8bae1dSRodney W. Grimes */ 38df8bae1dSRodney W. Grimes 39677b542eSDavid E. O'Brien #include <sys/cdefs.h> 40677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 41677b542eSDavid E. O'Brien 429f25cbe4SAlexander V. Chernikov #include "opt_inet.h" 439f25cbe4SAlexander V. Chernikov #include "opt_inet6.h" 449f25cbe4SAlexander V. Chernikov 45df8bae1dSRodney W. Grimes #include <sys/param.h> 46*6d2e2df7SMark Johnston #include <sys/systm.h> 47057e2795SBruce Evans #include <sys/dirent.h> 48d4468577SJamie Gritton #include <sys/jail.h> 49986f4ce7SBruce Evans #include <sys/kernel.h> 50057e2795SBruce Evans #include <sys/lock.h> 51a1c995b6SPoul-Henning Kamp #include <sys/malloc.h> 52bf61e266SKris Kennaway #include <sys/mbuf.h> 53df8bae1dSRodney W. Grimes #include <sys/mount.h> 54057e2795SBruce Evans #include <sys/mutex.h> 5520efcfc6SAndrey V. Elsukov #include <sys/rmlock.h> 567e9e371fSJohn Baldwin #include <sys/refcount.h> 57593efaf9SJohn Baldwin #include <sys/signalvar.h> 58057e2795SBruce Evans #include <sys/socket.h> 595e950839SLuoqi Chen #include <sys/vnode.h> 60df8bae1dSRodney W. Grimes 619f25cbe4SAlexander V. Chernikov #include <netinet/in.h> 62057e2795SBruce Evans #include <net/radix.h> 63057e2795SBruce Evans 645bb84bc8SRobert Watson static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure"); 6555166637SPoul-Henning Kamp 666e18247aSRuslan Bukin #if defined(INET) || defined(INET6) 679f25cbe4SAlexander V. Chernikov static struct radix_node_head *vfs_create_addrlist_af( 689f25cbe4SAlexander V. Chernikov struct radix_node_head **prnh, int off); 696e18247aSRuslan Bukin #endif 704d77a549SAlfred Perlstein static void vfs_free_addrlist(struct netexport *nep); 714d77a549SAlfred Perlstein static int vfs_free_netcred(struct radix_node *rn, void *w); 729f25cbe4SAlexander V. Chernikov static void vfs_free_addrlist_af(struct radix_node_head **prnh); 734d77a549SAlfred Perlstein static int vfs_hang_addrlist(struct mount *mp, struct netexport *nep, 744d77a549SAlfred Perlstein struct export_args *argp); 75ebbfc2f8SPoul-Henning Kamp static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *); 7698d93822SBruce Evans 77df8bae1dSRodney W. Grimes /* 78a13234bbSPoul-Henning Kamp * Network address lookup element 79a13234bbSPoul-Henning Kamp */ 80a13234bbSPoul-Henning Kamp struct netcred { 81a13234bbSPoul-Henning Kamp struct radix_node netc_rnodes[2]; 82a13234bbSPoul-Henning Kamp int netc_exflags; 835679fe19SAlexander Kabaev struct ucred *netc_anon; 84a9148abdSDoug Rabson int netc_numsecflavors; 85a9148abdSDoug Rabson int netc_secflavors[MAXSECFLAVORS]; 86a13234bbSPoul-Henning Kamp }; 87a13234bbSPoul-Henning Kamp 88a13234bbSPoul-Henning Kamp /* 89a13234bbSPoul-Henning Kamp * Network export information 90a13234bbSPoul-Henning Kamp */ 91a13234bbSPoul-Henning Kamp struct netexport { 92a13234bbSPoul-Henning Kamp struct netcred ne_defexported; /* Default export */ 939f25cbe4SAlexander V. Chernikov struct radix_node_head *ne4; 949f25cbe4SAlexander V. Chernikov struct radix_node_head *ne6; 95a13234bbSPoul-Henning Kamp }; 96a13234bbSPoul-Henning Kamp 97a13234bbSPoul-Henning Kamp /* 98df8bae1dSRodney W. Grimes * Build hash lists of net addresses and hang them off the mount point. 995679fe19SAlexander Kabaev * Called by vfs_export() to set up the lists of export addresses. 100df8bae1dSRodney W. Grimes */ 101df8bae1dSRodney W. Grimes static int 1022830e09dSCraig Rodrigues vfs_hang_addrlist(struct mount *mp, struct netexport *nep, 1032830e09dSCraig Rodrigues struct export_args *argp) 104df8bae1dSRodney W. Grimes { 1053e85b721SEd Maste struct netcred *np; 1063e85b721SEd Maste struct radix_node_head *rnh; 1073e85b721SEd Maste int i; 108df8bae1dSRodney W. Grimes struct radix_node *rn; 109b85f65afSPedro F. Giffuni struct sockaddr *saddr, *smask = NULL; 110763f2e78SBjoern A. Zeeb #if defined(INET6) || defined(INET) 1119f25cbe4SAlexander V. Chernikov int off; 112763f2e78SBjoern A. Zeeb #endif 113df8bae1dSRodney W. Grimes int error; 114df8bae1dSRodney W. Grimes 115e74d4831SDima Dorfman /* 116e74d4831SDima Dorfman * XXX: This routine converts from a `struct xucred' 117e74d4831SDima Dorfman * (argp->ex_anon) to a `struct ucred' (np->netc_anon). This 118e74d4831SDima Dorfman * operation is questionable; for example, what should be done 119e74d4831SDima Dorfman * with fields like cr_uidinfo and cr_prison? Currently, this 120e74d4831SDima Dorfman * routine does not touch them (leaves them as NULL). 121e74d4831SDima Dorfman */ 12261e323a2SCraig Rodrigues if (argp->ex_anon.cr_version != XUCRED_VERSION) { 12361e323a2SCraig Rodrigues vfs_mount_error(mp, "ex_anon.cr_version: %d != %d", 12461e323a2SCraig Rodrigues argp->ex_anon.cr_version, XUCRED_VERSION); 125e74d4831SDima Dorfman return (EINVAL); 12661e323a2SCraig Rodrigues } 127e74d4831SDima Dorfman 128df8bae1dSRodney W. Grimes if (argp->ex_addrlen == 0) { 12961e323a2SCraig Rodrigues if (mp->mnt_flag & MNT_DEFEXPORTED) { 13061e323a2SCraig Rodrigues vfs_mount_error(mp, 13161e323a2SCraig Rodrigues "MNT_DEFEXPORTED already set for mount %p", mp); 132df8bae1dSRodney W. Grimes return (EPERM); 13361e323a2SCraig Rodrigues } 134df8bae1dSRodney W. Grimes np = &nep->ne_defexported; 135df8bae1dSRodney W. Grimes np->netc_exflags = argp->ex_flags; 1365679fe19SAlexander Kabaev np->netc_anon = crget(); 1375679fe19SAlexander Kabaev np->netc_anon->cr_uid = argp->ex_anon.cr_uid; 138838d9858SBrooks Davis crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups, 139838d9858SBrooks Davis argp->ex_anon.cr_groups); 140d4468577SJamie Gritton np->netc_anon->cr_prison = &prison0; 141d4468577SJamie Gritton prison_hold(np->netc_anon->cr_prison); 142a9148abdSDoug Rabson np->netc_numsecflavors = argp->ex_numsecflavors; 143a9148abdSDoug Rabson bcopy(argp->ex_secflavors, np->netc_secflavors, 144a9148abdSDoug Rabson sizeof(np->netc_secflavors)); 1455da56ddbSTor Egge MNT_ILOCK(mp); 146df8bae1dSRodney W. Grimes mp->mnt_flag |= MNT_DEFEXPORTED; 1475da56ddbSTor Egge MNT_IUNLOCK(mp); 148df8bae1dSRodney W. Grimes return (0); 149df8bae1dSRodney W. Grimes } 150bf61e266SKris Kennaway 151c43cad1aSScott Long #if MSIZE <= 256 1523a13c9ccSCraig Rodrigues if (argp->ex_addrlen > MLEN) { 1533a13c9ccSCraig Rodrigues vfs_mount_error(mp, "ex_addrlen %d is greater than %d", 1543a13c9ccSCraig Rodrigues argp->ex_addrlen, MLEN); 155bf61e266SKris Kennaway return (EINVAL); 1563a13c9ccSCraig Rodrigues } 157c43cad1aSScott Long #endif 158bf61e266SKris Kennaway 159df8bae1dSRodney W. Grimes i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; 160a163d034SWarner Losh np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO); 161df8bae1dSRodney W. Grimes saddr = (struct sockaddr *) (np + 1); 162210a5a71SAlfred Perlstein if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen))) 163df8bae1dSRodney W. Grimes goto out; 16461e323a2SCraig Rodrigues if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) { 165b96e102aSColin Percival error = EINVAL; 16661e323a2SCraig Rodrigues vfs_mount_error(mp, "Invalid saddr->sa_family: %d"); 167b96e102aSColin Percival goto out; 168b96e102aSColin Percival } 169df8bae1dSRodney W. Grimes if (saddr->sa_len > argp->ex_addrlen) 170df8bae1dSRodney W. Grimes saddr->sa_len = argp->ex_addrlen; 171df8bae1dSRodney W. Grimes if (argp->ex_masklen) { 172c5e3ef7eSAlfred Perlstein smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen); 173210a5a71SAlfred Perlstein error = copyin(argp->ex_mask, smask, argp->ex_masklen); 174df8bae1dSRodney W. Grimes if (error) 175df8bae1dSRodney W. Grimes goto out; 176df8bae1dSRodney W. Grimes if (smask->sa_len > argp->ex_masklen) 177df8bae1dSRodney W. Grimes smask->sa_len = argp->ex_masklen; 178df8bae1dSRodney W. Grimes } 1799f25cbe4SAlexander V. Chernikov rnh = NULL; 1809f25cbe4SAlexander V. Chernikov switch (saddr->sa_family) { 1819f25cbe4SAlexander V. Chernikov #ifdef INET 1829f25cbe4SAlexander V. Chernikov case AF_INET: 1839f25cbe4SAlexander V. Chernikov if ((rnh = nep->ne4) == NULL) { 1849f25cbe4SAlexander V. Chernikov off = offsetof(struct sockaddr_in, sin_addr) << 3; 1859f25cbe4SAlexander V. Chernikov rnh = vfs_create_addrlist_af(&nep->ne4, off); 1869f25cbe4SAlexander V. Chernikov } 187df8bae1dSRodney W. Grimes break; 1889f25cbe4SAlexander V. Chernikov #endif 1899f25cbe4SAlexander V. Chernikov #ifdef INET6 1909f25cbe4SAlexander V. Chernikov case AF_INET6: 1919f25cbe4SAlexander V. Chernikov if ((rnh = nep->ne6) == NULL) { 1929f25cbe4SAlexander V. Chernikov off = offsetof(struct sockaddr_in6, sin6_addr) << 3; 1939f25cbe4SAlexander V. Chernikov rnh = vfs_create_addrlist_af(&nep->ne6, off); 194df8bae1dSRodney W. Grimes } 1959f25cbe4SAlexander V. Chernikov break; 1969f25cbe4SAlexander V. Chernikov #endif 1978b07e49aSJulian Elischer } 1989f25cbe4SAlexander V. Chernikov if (rnh == NULL) { 199df8bae1dSRodney W. Grimes error = ENOBUFS; 20061e323a2SCraig Rodrigues vfs_mount_error(mp, "%s %s %d", 20161e323a2SCraig Rodrigues "Unable to initialize radix node head ", 2029f25cbe4SAlexander V. Chernikov "for address family", saddr->sa_family); 203df8bae1dSRodney W. Grimes goto out; 204df8bae1dSRodney W. Grimes } 205956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 20661eee0e2SAlexander V. Chernikov rn = (*rnh->rnh_addaddr)(saddr, smask, &rnh->rh, np->netc_rnodes); 207956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 208956b0b65SJeffrey Hsu if (rn == NULL || np != (struct netcred *)rn) { /* already exists */ 20937a6b453SAlfred Perlstein error = EPERM; 2109f25cbe4SAlexander V. Chernikov vfs_mount_error(mp, 2119f25cbe4SAlexander V. Chernikov "netcred already exists for given addr/mask"); 21237a6b453SAlfred Perlstein goto out; 213df8bae1dSRodney W. Grimes } 214df8bae1dSRodney W. Grimes np->netc_exflags = argp->ex_flags; 2155679fe19SAlexander Kabaev np->netc_anon = crget(); 2165679fe19SAlexander Kabaev np->netc_anon->cr_uid = argp->ex_anon.cr_uid; 217838d9858SBrooks Davis crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups, 218f1c4014cSRick Macklem argp->ex_anon.cr_groups); 219d4468577SJamie Gritton np->netc_anon->cr_prison = &prison0; 220d4468577SJamie Gritton prison_hold(np->netc_anon->cr_prison); 221a9148abdSDoug Rabson np->netc_numsecflavors = argp->ex_numsecflavors; 222a9148abdSDoug Rabson bcopy(argp->ex_secflavors, np->netc_secflavors, 223a9148abdSDoug Rabson sizeof(np->netc_secflavors)); 224df8bae1dSRodney W. Grimes return (0); 225df8bae1dSRodney W. Grimes out: 226df8bae1dSRodney W. Grimes free(np, M_NETADDR); 227df8bae1dSRodney W. Grimes return (error); 228df8bae1dSRodney W. Grimes } 229df8bae1dSRodney W. Grimes 230a863c0fbSEivind Eklund /* Helper for vfs_free_addrlist. */ 231df8bae1dSRodney W. Grimes /* ARGSUSED */ 232df8bae1dSRodney W. Grimes static int 2332830e09dSCraig Rodrigues vfs_free_netcred(struct radix_node *rn, void *w) 234df8bae1dSRodney W. Grimes { 235a73034efSKonstantin Belousov struct radix_node_head *rnh = (struct radix_node_head *) w; 236a73034efSKonstantin Belousov struct ucred *cred; 237df8bae1dSRodney W. Grimes 23861eee0e2SAlexander V. Chernikov (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, &rnh->rh); 239a73034efSKonstantin Belousov cred = ((struct netcred *)rn)->netc_anon; 240a73034efSKonstantin Belousov if (cred != NULL) 241a73034efSKonstantin Belousov crfree(cred); 242210a5a71SAlfred Perlstein free(rn, M_NETADDR); 243df8bae1dSRodney W. Grimes return (0); 244df8bae1dSRodney W. Grimes } 245df8bae1dSRodney W. Grimes 2466e18247aSRuslan Bukin #if defined(INET) || defined(INET6) 2479f25cbe4SAlexander V. Chernikov static struct radix_node_head * 2489f25cbe4SAlexander V. Chernikov vfs_create_addrlist_af(struct radix_node_head **prnh, int off) 2499f25cbe4SAlexander V. Chernikov { 2509f25cbe4SAlexander V. Chernikov 2519f25cbe4SAlexander V. Chernikov if (rn_inithead((void **)prnh, off) == 0) 2529f25cbe4SAlexander V. Chernikov return (NULL); 2539f25cbe4SAlexander V. Chernikov RADIX_NODE_HEAD_LOCK_INIT(*prnh); 2549f25cbe4SAlexander V. Chernikov return (*prnh); 2559f25cbe4SAlexander V. Chernikov } 2566e18247aSRuslan Bukin #endif 2579f25cbe4SAlexander V. Chernikov 2589f25cbe4SAlexander V. Chernikov static void 2599f25cbe4SAlexander V. Chernikov vfs_free_addrlist_af(struct radix_node_head **prnh) 2609f25cbe4SAlexander V. Chernikov { 2619f25cbe4SAlexander V. Chernikov struct radix_node_head *rnh; 2629f25cbe4SAlexander V. Chernikov 2639f25cbe4SAlexander V. Chernikov rnh = *prnh; 2649f25cbe4SAlexander V. Chernikov RADIX_NODE_HEAD_LOCK(rnh); 2651c9b29f9SBryan Drewery (*rnh->rnh_walktree)(&rnh->rh, vfs_free_netcred, rnh); 2669f25cbe4SAlexander V. Chernikov RADIX_NODE_HEAD_UNLOCK(rnh); 2679f25cbe4SAlexander V. Chernikov RADIX_NODE_HEAD_DESTROY(rnh); 26857a8e341SBryan Drewery rn_detachhead((void **)prnh); 2699f25cbe4SAlexander V. Chernikov prnh = NULL; 2709f25cbe4SAlexander V. Chernikov } 2719f25cbe4SAlexander V. Chernikov 272df8bae1dSRodney W. Grimes /* 273df8bae1dSRodney W. Grimes * Free the net address hash lists that are hanging off the mount points. 274df8bae1dSRodney W. Grimes */ 275df8bae1dSRodney W. Grimes static void 2762830e09dSCraig Rodrigues vfs_free_addrlist(struct netexport *nep) 277df8bae1dSRodney W. Grimes { 278a73034efSKonstantin Belousov struct ucred *cred; 279df8bae1dSRodney W. Grimes 2809f25cbe4SAlexander V. Chernikov if (nep->ne4 != NULL) 2819f25cbe4SAlexander V. Chernikov vfs_free_addrlist_af(&nep->ne4); 2829f25cbe4SAlexander V. Chernikov if (nep->ne6 != NULL) 2839f25cbe4SAlexander V. Chernikov vfs_free_addrlist_af(&nep->ne6); 2849f25cbe4SAlexander V. Chernikov 285a73034efSKonstantin Belousov cred = nep->ne_defexported.netc_anon; 286a73034efSKonstantin Belousov if (cred != NULL) 287a73034efSKonstantin Belousov crfree(cred); 288a73034efSKonstantin Belousov 289a73034efSKonstantin Belousov } 290df8bae1dSRodney W. Grimes 29121a90397SAlfred Perlstein /* 29221a90397SAlfred Perlstein * High level function to manipulate export options on a mount point 29321a90397SAlfred Perlstein * and the passed in netexport. 29421a90397SAlfred Perlstein * Struct export_args *argp is the variable used to twiddle options, 29521a90397SAlfred Perlstein * the structure is described in sys/mount.h 29621a90397SAlfred Perlstein */ 297df8bae1dSRodney W. Grimes int 2982830e09dSCraig Rodrigues vfs_export(struct mount *mp, struct export_args *argp) 299df8bae1dSRodney W. Grimes { 300a13234bbSPoul-Henning Kamp struct netexport *nep; 301df8bae1dSRodney W. Grimes int error; 302df8bae1dSRodney W. Grimes 303a9148abdSDoug Rabson if (argp->ex_numsecflavors < 0 304a9148abdSDoug Rabson || argp->ex_numsecflavors >= MAXSECFLAVORS) 305a9148abdSDoug Rabson return (EINVAL); 306a9148abdSDoug Rabson 30761e323a2SCraig Rodrigues error = 0; 308a7053783SKonstantin Belousov lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL); 3095679fe19SAlexander Kabaev nep = mp->mnt_export; 310df8bae1dSRodney W. Grimes if (argp->ex_flags & MNT_DELEXPORT) { 31103eff583SCraig Rodrigues if (nep == NULL) { 31261e323a2SCraig Rodrigues error = ENOENT; 31361e323a2SCraig Rodrigues goto out; 31403eff583SCraig Rodrigues } 315f6b4c285SDoug Rabson if (mp->mnt_flag & MNT_EXPUBLIC) { 316f6b4c285SDoug Rabson vfs_setpublicfs(NULL, NULL, NULL); 3175da56ddbSTor Egge MNT_ILOCK(mp); 318f6b4c285SDoug Rabson mp->mnt_flag &= ~MNT_EXPUBLIC; 3195da56ddbSTor Egge MNT_IUNLOCK(mp); 320f6b4c285SDoug Rabson } 321df8bae1dSRodney W. Grimes vfs_free_addrlist(nep); 322a13234bbSPoul-Henning Kamp mp->mnt_export = NULL; 323a13234bbSPoul-Henning Kamp free(nep, M_MOUNT); 3240ca9ed86SAlexander Kabaev nep = NULL; 3255da56ddbSTor Egge MNT_ILOCK(mp); 326df8bae1dSRodney W. Grimes mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 3275da56ddbSTor Egge MNT_IUNLOCK(mp); 328df8bae1dSRodney W. Grimes } 329df8bae1dSRodney W. Grimes if (argp->ex_flags & MNT_EXPORTED) { 330a13234bbSPoul-Henning Kamp if (nep == NULL) { 331a163d034SWarner Losh nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO); 332a13234bbSPoul-Henning Kamp mp->mnt_export = nep; 333a13234bbSPoul-Henning Kamp } 334f6b4c285SDoug Rabson if (argp->ex_flags & MNT_EXPUBLIC) { 335f6b4c285SDoug Rabson if ((error = vfs_setpublicfs(mp, nep, argp)) != 0) 33661e323a2SCraig Rodrigues goto out; 3375da56ddbSTor Egge MNT_ILOCK(mp); 338f6b4c285SDoug Rabson mp->mnt_flag |= MNT_EXPUBLIC; 3395da56ddbSTor Egge MNT_IUNLOCK(mp); 340f6b4c285SDoug Rabson } 341bb56ec4aSPoul-Henning Kamp if ((error = vfs_hang_addrlist(mp, nep, argp))) 34261e323a2SCraig Rodrigues goto out; 3435da56ddbSTor Egge MNT_ILOCK(mp); 344df8bae1dSRodney W. Grimes mp->mnt_flag |= MNT_EXPORTED; 3455da56ddbSTor Egge MNT_IUNLOCK(mp); 346df8bae1dSRodney W. Grimes } 34761e323a2SCraig Rodrigues 34861e323a2SCraig Rodrigues out: 349a7053783SKonstantin Belousov lockmgr(&mp->mnt_explock, LK_RELEASE, NULL); 35061e323a2SCraig Rodrigues /* 35161e323a2SCraig Rodrigues * Once we have executed the vfs_export() command, we do 35261e323a2SCraig Rodrigues * not want to keep the "export" option around in the 35361e323a2SCraig Rodrigues * options list, since that will cause subsequent MNT_UPDATE 35461e323a2SCraig Rodrigues * calls to fail. The export information is saved in 35561e323a2SCraig Rodrigues * mp->mnt_export, so we can safely delete the "export" mount option 35661e323a2SCraig Rodrigues * here. 35761e323a2SCraig Rodrigues */ 35861e323a2SCraig Rodrigues vfs_deleteopt(mp->mnt_optnew, "export"); 35961e323a2SCraig Rodrigues vfs_deleteopt(mp->mnt_opt, "export"); 36061e323a2SCraig Rodrigues return (error); 361df8bae1dSRodney W. Grimes } 362df8bae1dSRodney W. Grimes 363f6b4c285SDoug Rabson /* 364f6b4c285SDoug Rabson * Set the publicly exported filesystem (WebNFS). Currently, only 365f6b4c285SDoug Rabson * one public filesystem is possible in the spec (RFC 2054 and 2055) 366f6b4c285SDoug Rabson */ 367f6b4c285SDoug Rabson int 3682830e09dSCraig Rodrigues vfs_setpublicfs(struct mount *mp, struct netexport *nep, 3692830e09dSCraig Rodrigues struct export_args *argp) 370f6b4c285SDoug Rabson { 371f6b4c285SDoug Rabson int error; 372f6b4c285SDoug Rabson struct vnode *rvp; 373f6b4c285SDoug Rabson char *cp; 374f6b4c285SDoug Rabson 375f6b4c285SDoug Rabson /* 376f6b4c285SDoug Rabson * mp == NULL -> invalidate the current info, the FS is 377f6b4c285SDoug Rabson * no longer exported. May be called from either vfs_export 378f6b4c285SDoug Rabson * or unmount, so check if it hasn't already been done. 379f6b4c285SDoug Rabson */ 380f6b4c285SDoug Rabson if (mp == NULL) { 381f6b4c285SDoug Rabson if (nfs_pub.np_valid) { 382f6b4c285SDoug Rabson nfs_pub.np_valid = 0; 383f6b4c285SDoug Rabson if (nfs_pub.np_index != NULL) { 3841ede983cSDag-Erling Smørgrav free(nfs_pub.np_index, M_TEMP); 385f6b4c285SDoug Rabson nfs_pub.np_index = NULL; 386f6b4c285SDoug Rabson } 387f6b4c285SDoug Rabson } 388f6b4c285SDoug Rabson return (0); 389f6b4c285SDoug Rabson } 390f6b4c285SDoug Rabson 391f6b4c285SDoug Rabson /* 392f6b4c285SDoug Rabson * Only one allowed at a time. 393f6b4c285SDoug Rabson */ 394f6b4c285SDoug Rabson if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount) 395f6b4c285SDoug Rabson return (EBUSY); 396f6b4c285SDoug Rabson 397f6b4c285SDoug Rabson /* 398f6b4c285SDoug Rabson * Get real filehandle for root of exported FS. 399f6b4c285SDoug Rabson */ 400210a5a71SAlfred Perlstein bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle)); 401f6b4c285SDoug Rabson nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid; 402f6b4c285SDoug Rabson 403dfd233edSAttilio Rao if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp))) 404f6b4c285SDoug Rabson return (error); 405f6b4c285SDoug Rabson 40610bcafe9SPawel Jakub Dawidek if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid))) 407f6b4c285SDoug Rabson return (error); 408f6b4c285SDoug Rabson 409f6b4c285SDoug Rabson vput(rvp); 410f6b4c285SDoug Rabson 411f6b4c285SDoug Rabson /* 412f6b4c285SDoug Rabson * If an indexfile was specified, pull it in. 413f6b4c285SDoug Rabson */ 414f6b4c285SDoug Rabson if (argp->ex_indexfile != NULL) { 4156d41588bSAlexander Kabaev if (nfs_pub.np_index == NULL) 4161ede983cSDag-Erling Smørgrav nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP, 417a163d034SWarner Losh M_WAITOK); 418f6b4c285SDoug Rabson error = copyinstr(argp->ex_indexfile, nfs_pub.np_index, 419f6b4c285SDoug Rabson MAXNAMLEN, (size_t *)0); 420f6b4c285SDoug Rabson if (!error) { 421f6b4c285SDoug Rabson /* 422f6b4c285SDoug Rabson * Check for illegal filenames. 423f6b4c285SDoug Rabson */ 424f6b4c285SDoug Rabson for (cp = nfs_pub.np_index; *cp; cp++) { 425f6b4c285SDoug Rabson if (*cp == '/') { 426f6b4c285SDoug Rabson error = EINVAL; 427f6b4c285SDoug Rabson break; 428f6b4c285SDoug Rabson } 429f6b4c285SDoug Rabson } 430f6b4c285SDoug Rabson } 431f6b4c285SDoug Rabson if (error) { 4321ede983cSDag-Erling Smørgrav free(nfs_pub.np_index, M_TEMP); 4335679fe19SAlexander Kabaev nfs_pub.np_index = NULL; 434f6b4c285SDoug Rabson return (error); 435f6b4c285SDoug Rabson } 436f6b4c285SDoug Rabson } 437f6b4c285SDoug Rabson 438f6b4c285SDoug Rabson nfs_pub.np_mount = mp; 439f6b4c285SDoug Rabson nfs_pub.np_valid = 1; 440f6b4c285SDoug Rabson return (0); 441f6b4c285SDoug Rabson } 442f6b4c285SDoug Rabson 44321a90397SAlfred Perlstein /* 44421a90397SAlfred Perlstein * Used by the filesystems to determine if a given network address 4456ffb78d1SEd Maste * (passed in 'nam') is present in their exports list, returns a pointer 44621a90397SAlfred Perlstein * to struct netcred so that the filesystem can examine it for 44721a90397SAlfred Perlstein * access rights (read/write/etc). 44821a90397SAlfred Perlstein */ 449ebbfc2f8SPoul-Henning Kamp static struct netcred * 450ebbfc2f8SPoul-Henning Kamp vfs_export_lookup(struct mount *mp, struct sockaddr *nam) 451df8bae1dSRodney W. Grimes { 45220efcfc6SAndrey V. Elsukov RADIX_NODE_HEAD_RLOCK_TRACKER; 453a13234bbSPoul-Henning Kamp struct netexport *nep; 4545e254379SEmmanuel Vadot struct netcred *np = NULL; 4553e85b721SEd Maste struct radix_node_head *rnh; 456df8bae1dSRodney W. Grimes struct sockaddr *saddr; 457df8bae1dSRodney W. Grimes 458a13234bbSPoul-Henning Kamp nep = mp->mnt_export; 459a13234bbSPoul-Henning Kamp if (nep == NULL) 460a13234bbSPoul-Henning Kamp return (NULL); 4615e254379SEmmanuel Vadot if ((mp->mnt_flag & MNT_EXPORTED) == 0) 4625e254379SEmmanuel Vadot return (NULL); 4635e254379SEmmanuel Vadot 464df8bae1dSRodney W. Grimes /* 4655e254379SEmmanuel Vadot * Lookup in the export list 4665e254379SEmmanuel Vadot */ 467b1d51685SEmmanuel Vadot if (nam != NULL) { 46857bf258eSGarrett Wollman saddr = nam; 4699f25cbe4SAlexander V. Chernikov rnh = NULL; 4709f25cbe4SAlexander V. Chernikov switch (saddr->sa_family) { 4719f25cbe4SAlexander V. Chernikov case AF_INET: 4729f25cbe4SAlexander V. Chernikov rnh = nep->ne4; 4739f25cbe4SAlexander V. Chernikov break; 4749f25cbe4SAlexander V. Chernikov case AF_INET6: 4759f25cbe4SAlexander V. Chernikov rnh = nep->ne6; 4769f25cbe4SAlexander V. Chernikov break; 4779f25cbe4SAlexander V. Chernikov } 478df8bae1dSRodney W. Grimes if (rnh != NULL) { 4793120b9d4SKip Macy RADIX_NODE_HEAD_RLOCK(rnh); 4805e254379SEmmanuel Vadot np = (struct netcred *) (*rnh->rnh_matchaddr)(saddr, &rnh->rh); 4813120b9d4SKip Macy RADIX_NODE_HEAD_RUNLOCK(rnh); 4825e254379SEmmanuel Vadot if (np != NULL && (np->netc_rnodes->rn_flags & RNF_ROOT) != 0) 4835e254379SEmmanuel Vadot return (NULL); 484df8bae1dSRodney W. Grimes } 485b1d51685SEmmanuel Vadot } 486b1d51685SEmmanuel Vadot 487b1d51685SEmmanuel Vadot /* 488b1d51685SEmmanuel Vadot * If no address match, use the default if it exists. 489b1d51685SEmmanuel Vadot */ 490b1d51685SEmmanuel Vadot if (np == NULL && (mp->mnt_flag & MNT_DEFEXPORTED) != 0) 491b1d51685SEmmanuel Vadot return (&nep->ne_defexported); 4925e254379SEmmanuel Vadot 493df8bae1dSRodney W. Grimes return (np); 494df8bae1dSRodney W. Grimes } 49561f5d510SDavid Greenman 49661f5d510SDavid Greenman /* 497a13234bbSPoul-Henning Kamp * XXX: This comment comes from the deprecated ufs_check_export() 498a13234bbSPoul-Henning Kamp * XXX: and may not entirely apply, but lacking something better: 499a13234bbSPoul-Henning Kamp * This is the generic part of fhtovp called after the underlying 500a13234bbSPoul-Henning Kamp * filesystem has validated the file handle. 501a13234bbSPoul-Henning Kamp * 502a13234bbSPoul-Henning Kamp * Verify that a host should have access to a filesystem. 503a13234bbSPoul-Henning Kamp */ 504a13234bbSPoul-Henning Kamp 505a13234bbSPoul-Henning Kamp int 5062830e09dSCraig Rodrigues vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp, 507a9148abdSDoug Rabson struct ucred **credanonp, int *numsecflavors, int **secflavors) 508a13234bbSPoul-Henning Kamp { 509a13234bbSPoul-Henning Kamp struct netcred *np; 510a13234bbSPoul-Henning Kamp 511a7053783SKonstantin Belousov lockmgr(&mp->mnt_explock, LK_SHARED, NULL); 512a13234bbSPoul-Henning Kamp np = vfs_export_lookup(mp, nam); 5135679fe19SAlexander Kabaev if (np == NULL) { 514a7053783SKonstantin Belousov lockmgr(&mp->mnt_explock, LK_RELEASE, NULL); 5155679fe19SAlexander Kabaev *credanonp = NULL; 516a13234bbSPoul-Henning Kamp return (EACCES); 5175679fe19SAlexander Kabaev } 518a13234bbSPoul-Henning Kamp *extflagsp = np->netc_exflags; 5195679fe19SAlexander Kabaev if ((*credanonp = np->netc_anon) != NULL) 5205679fe19SAlexander Kabaev crhold(*credanonp); 521a9148abdSDoug Rabson if (numsecflavors) 522a9148abdSDoug Rabson *numsecflavors = np->netc_numsecflavors; 523a9148abdSDoug Rabson if (secflavors) 524a9148abdSDoug Rabson *secflavors = np->netc_secflavors; 5255679fe19SAlexander Kabaev lockmgr(&mp->mnt_explock, LK_RELEASE, NULL); 526a13234bbSPoul-Henning Kamp return (0); 527a13234bbSPoul-Henning Kamp } 528a13234bbSPoul-Henning Kamp 529