19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1989, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34996c772fSJohn Dyson * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37677b542eSDavid E. O'Brien #include <sys/cdefs.h> 38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39677b542eSDavid E. O'Brien 409f25cbe4SAlexander V. Chernikov #include "opt_inet.h" 419f25cbe4SAlexander V. Chernikov #include "opt_inet6.h" 429f25cbe4SAlexander V. Chernikov 43df8bae1dSRodney W. Grimes #include <sys/param.h> 44057e2795SBruce Evans #include <sys/dirent.h> 45d4468577SJamie Gritton #include <sys/jail.h> 46986f4ce7SBruce Evans #include <sys/kernel.h> 47057e2795SBruce Evans #include <sys/lock.h> 48a1c995b6SPoul-Henning Kamp #include <sys/malloc.h> 49bf61e266SKris Kennaway #include <sys/mbuf.h> 50df8bae1dSRodney W. Grimes #include <sys/mount.h> 51057e2795SBruce Evans #include <sys/mutex.h> 523120b9d4SKip Macy #include <sys/rwlock.h> 537e9e371fSJohn Baldwin #include <sys/refcount.h> 54593efaf9SJohn Baldwin #include <sys/signalvar.h> 55057e2795SBruce Evans #include <sys/socket.h> 56057e2795SBruce Evans #include <sys/systm.h> 575e950839SLuoqi Chen #include <sys/vnode.h> 58df8bae1dSRodney W. Grimes 599f25cbe4SAlexander V. Chernikov #include <netinet/in.h> 60057e2795SBruce Evans #include <net/radix.h> 61057e2795SBruce Evans 625bb84bc8SRobert Watson static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure"); 6355166637SPoul-Henning Kamp 649f25cbe4SAlexander V. Chernikov static struct radix_node_head *vfs_create_addrlist_af( 659f25cbe4SAlexander V. Chernikov struct radix_node_head **prnh, int off); 664d77a549SAlfred Perlstein static void vfs_free_addrlist(struct netexport *nep); 674d77a549SAlfred Perlstein static int vfs_free_netcred(struct radix_node *rn, void *w); 689f25cbe4SAlexander V. Chernikov static void vfs_free_addrlist_af(struct radix_node_head **prnh); 694d77a549SAlfred Perlstein static int vfs_hang_addrlist(struct mount *mp, struct netexport *nep, 704d77a549SAlfred Perlstein struct export_args *argp); 71ebbfc2f8SPoul-Henning Kamp static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *); 7298d93822SBruce Evans 73df8bae1dSRodney W. Grimes /* 74a13234bbSPoul-Henning Kamp * Network address lookup element 75a13234bbSPoul-Henning Kamp */ 76a13234bbSPoul-Henning Kamp struct netcred { 77a13234bbSPoul-Henning Kamp struct radix_node netc_rnodes[2]; 78a13234bbSPoul-Henning Kamp int netc_exflags; 795679fe19SAlexander Kabaev struct ucred *netc_anon; 80a9148abdSDoug Rabson int netc_numsecflavors; 81a9148abdSDoug Rabson int netc_secflavors[MAXSECFLAVORS]; 82a13234bbSPoul-Henning Kamp }; 83a13234bbSPoul-Henning Kamp 84a13234bbSPoul-Henning Kamp /* 85a13234bbSPoul-Henning Kamp * Network export information 86a13234bbSPoul-Henning Kamp */ 87a13234bbSPoul-Henning Kamp struct netexport { 88a13234bbSPoul-Henning Kamp struct netcred ne_defexported; /* Default export */ 899f25cbe4SAlexander V. Chernikov struct radix_node_head *ne4; 909f25cbe4SAlexander V. Chernikov struct radix_node_head *ne6; 91a13234bbSPoul-Henning Kamp }; 92a13234bbSPoul-Henning Kamp 93a13234bbSPoul-Henning Kamp /* 94df8bae1dSRodney W. Grimes * Build hash lists of net addresses and hang them off the mount point. 955679fe19SAlexander Kabaev * Called by vfs_export() to set up the lists of export addresses. 96df8bae1dSRodney W. Grimes */ 97df8bae1dSRodney W. Grimes static int 982830e09dSCraig Rodrigues vfs_hang_addrlist(struct mount *mp, struct netexport *nep, 992830e09dSCraig Rodrigues struct export_args *argp) 100df8bae1dSRodney W. Grimes { 101df8bae1dSRodney W. Grimes register struct netcred *np; 102df8bae1dSRodney W. Grimes register struct radix_node_head *rnh; 103df8bae1dSRodney W. Grimes register int i; 104df8bae1dSRodney W. Grimes struct radix_node *rn; 105df8bae1dSRodney W. Grimes struct sockaddr *saddr, *smask = 0; 106763f2e78SBjoern A. Zeeb #if defined(INET6) || defined(INET) 1079f25cbe4SAlexander V. Chernikov int off; 108763f2e78SBjoern A. Zeeb #endif 109df8bae1dSRodney W. Grimes int error; 110df8bae1dSRodney W. Grimes 111e74d4831SDima Dorfman /* 112e74d4831SDima Dorfman * XXX: This routine converts from a `struct xucred' 113e74d4831SDima Dorfman * (argp->ex_anon) to a `struct ucred' (np->netc_anon). This 114e74d4831SDima Dorfman * operation is questionable; for example, what should be done 115e74d4831SDima Dorfman * with fields like cr_uidinfo and cr_prison? Currently, this 116e74d4831SDima Dorfman * routine does not touch them (leaves them as NULL). 117e74d4831SDima Dorfman */ 11861e323a2SCraig Rodrigues if (argp->ex_anon.cr_version != XUCRED_VERSION) { 11961e323a2SCraig Rodrigues vfs_mount_error(mp, "ex_anon.cr_version: %d != %d", 12061e323a2SCraig Rodrigues argp->ex_anon.cr_version, XUCRED_VERSION); 121e74d4831SDima Dorfman return (EINVAL); 12261e323a2SCraig Rodrigues } 123e74d4831SDima Dorfman 124df8bae1dSRodney W. Grimes if (argp->ex_addrlen == 0) { 12561e323a2SCraig Rodrigues if (mp->mnt_flag & MNT_DEFEXPORTED) { 12661e323a2SCraig Rodrigues vfs_mount_error(mp, 12761e323a2SCraig Rodrigues "MNT_DEFEXPORTED already set for mount %p", mp); 128df8bae1dSRodney W. Grimes return (EPERM); 12961e323a2SCraig Rodrigues } 130df8bae1dSRodney W. Grimes np = &nep->ne_defexported; 131df8bae1dSRodney W. Grimes np->netc_exflags = argp->ex_flags; 1325679fe19SAlexander Kabaev np->netc_anon = crget(); 1335679fe19SAlexander Kabaev np->netc_anon->cr_uid = argp->ex_anon.cr_uid; 134838d9858SBrooks Davis crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups, 135838d9858SBrooks Davis argp->ex_anon.cr_groups); 136d4468577SJamie Gritton np->netc_anon->cr_prison = &prison0; 137d4468577SJamie Gritton prison_hold(np->netc_anon->cr_prison); 138a9148abdSDoug Rabson np->netc_numsecflavors = argp->ex_numsecflavors; 139a9148abdSDoug Rabson bcopy(argp->ex_secflavors, np->netc_secflavors, 140a9148abdSDoug Rabson sizeof(np->netc_secflavors)); 1415da56ddbSTor Egge MNT_ILOCK(mp); 142df8bae1dSRodney W. Grimes mp->mnt_flag |= MNT_DEFEXPORTED; 1435da56ddbSTor Egge MNT_IUNLOCK(mp); 144df8bae1dSRodney W. Grimes return (0); 145df8bae1dSRodney W. Grimes } 146bf61e266SKris Kennaway 147c43cad1aSScott Long #if MSIZE <= 256 1483a13c9ccSCraig Rodrigues if (argp->ex_addrlen > MLEN) { 1493a13c9ccSCraig Rodrigues vfs_mount_error(mp, "ex_addrlen %d is greater than %d", 1503a13c9ccSCraig Rodrigues argp->ex_addrlen, MLEN); 151bf61e266SKris Kennaway return (EINVAL); 1523a13c9ccSCraig Rodrigues } 153c43cad1aSScott Long #endif 154bf61e266SKris Kennaway 155df8bae1dSRodney W. Grimes i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; 156a163d034SWarner Losh np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO); 157df8bae1dSRodney W. Grimes saddr = (struct sockaddr *) (np + 1); 158210a5a71SAlfred Perlstein if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen))) 159df8bae1dSRodney W. Grimes goto out; 16061e323a2SCraig Rodrigues if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) { 161b96e102aSColin Percival error = EINVAL; 16261e323a2SCraig Rodrigues vfs_mount_error(mp, "Invalid saddr->sa_family: %d"); 163b96e102aSColin Percival goto out; 164b96e102aSColin Percival } 165df8bae1dSRodney W. Grimes if (saddr->sa_len > argp->ex_addrlen) 166df8bae1dSRodney W. Grimes saddr->sa_len = argp->ex_addrlen; 167df8bae1dSRodney W. Grimes if (argp->ex_masklen) { 168c5e3ef7eSAlfred Perlstein smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen); 169210a5a71SAlfred Perlstein error = copyin(argp->ex_mask, smask, argp->ex_masklen); 170df8bae1dSRodney W. Grimes if (error) 171df8bae1dSRodney W. Grimes goto out; 172df8bae1dSRodney W. Grimes if (smask->sa_len > argp->ex_masklen) 173df8bae1dSRodney W. Grimes smask->sa_len = argp->ex_masklen; 174df8bae1dSRodney W. Grimes } 1759f25cbe4SAlexander V. Chernikov rnh = NULL; 1769f25cbe4SAlexander V. Chernikov switch (saddr->sa_family) { 1779f25cbe4SAlexander V. Chernikov #ifdef INET 1789f25cbe4SAlexander V. Chernikov case AF_INET: 1799f25cbe4SAlexander V. Chernikov if ((rnh = nep->ne4) == NULL) { 1809f25cbe4SAlexander V. Chernikov off = offsetof(struct sockaddr_in, sin_addr) << 3; 1819f25cbe4SAlexander V. Chernikov rnh = vfs_create_addrlist_af(&nep->ne4, off); 1829f25cbe4SAlexander V. Chernikov } 183df8bae1dSRodney W. Grimes break; 1849f25cbe4SAlexander V. Chernikov #endif 1859f25cbe4SAlexander V. Chernikov #ifdef INET6 1869f25cbe4SAlexander V. Chernikov case AF_INET6: 1879f25cbe4SAlexander V. Chernikov if ((rnh = nep->ne6) == NULL) { 1889f25cbe4SAlexander V. Chernikov off = offsetof(struct sockaddr_in6, sin6_addr) << 3; 1899f25cbe4SAlexander V. Chernikov rnh = vfs_create_addrlist_af(&nep->ne6, off); 190df8bae1dSRodney W. Grimes } 1919f25cbe4SAlexander V. Chernikov break; 1929f25cbe4SAlexander V. Chernikov #endif 1938b07e49aSJulian Elischer } 1949f25cbe4SAlexander V. Chernikov if (rnh == NULL) { 195df8bae1dSRodney W. Grimes error = ENOBUFS; 19661e323a2SCraig Rodrigues vfs_mount_error(mp, "%s %s %d", 19761e323a2SCraig Rodrigues "Unable to initialize radix node head ", 1989f25cbe4SAlexander V. Chernikov "for address family", saddr->sa_family); 199df8bae1dSRodney W. Grimes goto out; 200df8bae1dSRodney W. Grimes } 201956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 202*61eee0e2SAlexander V. Chernikov rn = (*rnh->rnh_addaddr)(saddr, smask, &rnh->rh, np->netc_rnodes); 203956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 204956b0b65SJeffrey Hsu if (rn == NULL || np != (struct netcred *)rn) { /* already exists */ 20537a6b453SAlfred Perlstein error = EPERM; 2069f25cbe4SAlexander V. Chernikov vfs_mount_error(mp, 2079f25cbe4SAlexander V. Chernikov "netcred already exists for given addr/mask"); 20837a6b453SAlfred Perlstein goto out; 209df8bae1dSRodney W. Grimes } 210df8bae1dSRodney W. Grimes np->netc_exflags = argp->ex_flags; 2115679fe19SAlexander Kabaev np->netc_anon = crget(); 2125679fe19SAlexander Kabaev np->netc_anon->cr_uid = argp->ex_anon.cr_uid; 213838d9858SBrooks Davis crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups, 214f1c4014cSRick Macklem argp->ex_anon.cr_groups); 215d4468577SJamie Gritton np->netc_anon->cr_prison = &prison0; 216d4468577SJamie Gritton prison_hold(np->netc_anon->cr_prison); 217a9148abdSDoug Rabson np->netc_numsecflavors = argp->ex_numsecflavors; 218a9148abdSDoug Rabson bcopy(argp->ex_secflavors, np->netc_secflavors, 219a9148abdSDoug Rabson sizeof(np->netc_secflavors)); 220df8bae1dSRodney W. Grimes return (0); 221df8bae1dSRodney W. Grimes out: 222df8bae1dSRodney W. Grimes free(np, M_NETADDR); 223df8bae1dSRodney W. Grimes return (error); 224df8bae1dSRodney W. Grimes } 225df8bae1dSRodney W. Grimes 226a863c0fbSEivind Eklund /* Helper for vfs_free_addrlist. */ 227df8bae1dSRodney W. Grimes /* ARGSUSED */ 228df8bae1dSRodney W. Grimes static int 2292830e09dSCraig Rodrigues vfs_free_netcred(struct radix_node *rn, void *w) 230df8bae1dSRodney W. Grimes { 231a73034efSKonstantin Belousov struct radix_node_head *rnh = (struct radix_node_head *) w; 232a73034efSKonstantin Belousov struct ucred *cred; 233df8bae1dSRodney W. Grimes 234*61eee0e2SAlexander V. Chernikov (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, &rnh->rh); 235a73034efSKonstantin Belousov cred = ((struct netcred *)rn)->netc_anon; 236a73034efSKonstantin Belousov if (cred != NULL) 237a73034efSKonstantin Belousov crfree(cred); 238210a5a71SAlfred Perlstein free(rn, M_NETADDR); 239df8bae1dSRodney W. Grimes return (0); 240df8bae1dSRodney W. Grimes } 241df8bae1dSRodney W. Grimes 2429f25cbe4SAlexander V. Chernikov static struct radix_node_head * 2439f25cbe4SAlexander V. Chernikov vfs_create_addrlist_af(struct radix_node_head **prnh, int off) 2449f25cbe4SAlexander V. Chernikov { 2459f25cbe4SAlexander V. Chernikov 2469f25cbe4SAlexander V. Chernikov if (rn_inithead((void **)prnh, off) == 0) 2479f25cbe4SAlexander V. Chernikov return (NULL); 2489f25cbe4SAlexander V. Chernikov RADIX_NODE_HEAD_LOCK_INIT(*prnh); 2499f25cbe4SAlexander V. Chernikov return (*prnh); 2509f25cbe4SAlexander V. Chernikov } 2519f25cbe4SAlexander V. Chernikov 2529f25cbe4SAlexander V. Chernikov static void 2539f25cbe4SAlexander V. Chernikov vfs_free_addrlist_af(struct radix_node_head **prnh) 2549f25cbe4SAlexander V. Chernikov { 2559f25cbe4SAlexander V. Chernikov struct radix_node_head *rnh; 2569f25cbe4SAlexander V. Chernikov 2579f25cbe4SAlexander V. Chernikov rnh = *prnh; 2589f25cbe4SAlexander V. Chernikov RADIX_NODE_HEAD_LOCK(rnh); 259*61eee0e2SAlexander V. Chernikov (*rnh->rnh_walktree)(&rnh->rh, vfs_free_netcred, &rnh->rh); 2609f25cbe4SAlexander V. Chernikov RADIX_NODE_HEAD_UNLOCK(rnh); 2619f25cbe4SAlexander V. Chernikov RADIX_NODE_HEAD_DESTROY(rnh); 2629f25cbe4SAlexander V. Chernikov free(rnh, M_RTABLE); 2639f25cbe4SAlexander V. Chernikov prnh = NULL; 2649f25cbe4SAlexander V. Chernikov } 2659f25cbe4SAlexander V. Chernikov 266df8bae1dSRodney W. Grimes /* 267df8bae1dSRodney W. Grimes * Free the net address hash lists that are hanging off the mount points. 268df8bae1dSRodney W. Grimes */ 269df8bae1dSRodney W. Grimes static void 2702830e09dSCraig Rodrigues vfs_free_addrlist(struct netexport *nep) 271df8bae1dSRodney W. Grimes { 272a73034efSKonstantin Belousov struct ucred *cred; 273df8bae1dSRodney W. Grimes 2749f25cbe4SAlexander V. Chernikov if (nep->ne4 != NULL) 2759f25cbe4SAlexander V. Chernikov vfs_free_addrlist_af(&nep->ne4); 2769f25cbe4SAlexander V. Chernikov if (nep->ne6 != NULL) 2779f25cbe4SAlexander V. Chernikov vfs_free_addrlist_af(&nep->ne6); 2789f25cbe4SAlexander V. Chernikov 279a73034efSKonstantin Belousov cred = nep->ne_defexported.netc_anon; 280a73034efSKonstantin Belousov if (cred != NULL) 281a73034efSKonstantin Belousov crfree(cred); 282a73034efSKonstantin Belousov 283a73034efSKonstantin Belousov } 284df8bae1dSRodney W. Grimes 28521a90397SAlfred Perlstein /* 28621a90397SAlfred Perlstein * High level function to manipulate export options on a mount point 28721a90397SAlfred Perlstein * and the passed in netexport. 28821a90397SAlfred Perlstein * Struct export_args *argp is the variable used to twiddle options, 28921a90397SAlfred Perlstein * the structure is described in sys/mount.h 29021a90397SAlfred Perlstein */ 291df8bae1dSRodney W. Grimes int 2922830e09dSCraig Rodrigues vfs_export(struct mount *mp, struct export_args *argp) 293df8bae1dSRodney W. Grimes { 294a13234bbSPoul-Henning Kamp struct netexport *nep; 295df8bae1dSRodney W. Grimes int error; 296df8bae1dSRodney W. Grimes 297a9148abdSDoug Rabson if (argp->ex_numsecflavors < 0 298a9148abdSDoug Rabson || argp->ex_numsecflavors >= MAXSECFLAVORS) 299a9148abdSDoug Rabson return (EINVAL); 300a9148abdSDoug Rabson 30161e323a2SCraig Rodrigues error = 0; 302a7053783SKonstantin Belousov lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL); 3035679fe19SAlexander Kabaev nep = mp->mnt_export; 304df8bae1dSRodney W. Grimes if (argp->ex_flags & MNT_DELEXPORT) { 30503eff583SCraig Rodrigues if (nep == NULL) { 30661e323a2SCraig Rodrigues error = ENOENT; 30761e323a2SCraig Rodrigues goto out; 30803eff583SCraig Rodrigues } 309f6b4c285SDoug Rabson if (mp->mnt_flag & MNT_EXPUBLIC) { 310f6b4c285SDoug Rabson vfs_setpublicfs(NULL, NULL, NULL); 3115da56ddbSTor Egge MNT_ILOCK(mp); 312f6b4c285SDoug Rabson mp->mnt_flag &= ~MNT_EXPUBLIC; 3135da56ddbSTor Egge MNT_IUNLOCK(mp); 314f6b4c285SDoug Rabson } 315df8bae1dSRodney W. Grimes vfs_free_addrlist(nep); 316a13234bbSPoul-Henning Kamp mp->mnt_export = NULL; 317a13234bbSPoul-Henning Kamp free(nep, M_MOUNT); 3180ca9ed86SAlexander Kabaev nep = NULL; 3195da56ddbSTor Egge MNT_ILOCK(mp); 320df8bae1dSRodney W. Grimes mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 3215da56ddbSTor Egge MNT_IUNLOCK(mp); 322df8bae1dSRodney W. Grimes } 323df8bae1dSRodney W. Grimes if (argp->ex_flags & MNT_EXPORTED) { 324a13234bbSPoul-Henning Kamp if (nep == NULL) { 325a163d034SWarner Losh nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO); 326a13234bbSPoul-Henning Kamp mp->mnt_export = nep; 327a13234bbSPoul-Henning Kamp } 328f6b4c285SDoug Rabson if (argp->ex_flags & MNT_EXPUBLIC) { 329f6b4c285SDoug Rabson if ((error = vfs_setpublicfs(mp, nep, argp)) != 0) 33061e323a2SCraig Rodrigues goto out; 3315da56ddbSTor Egge MNT_ILOCK(mp); 332f6b4c285SDoug Rabson mp->mnt_flag |= MNT_EXPUBLIC; 3335da56ddbSTor Egge MNT_IUNLOCK(mp); 334f6b4c285SDoug Rabson } 335bb56ec4aSPoul-Henning Kamp if ((error = vfs_hang_addrlist(mp, nep, argp))) 33661e323a2SCraig Rodrigues goto out; 3375da56ddbSTor Egge MNT_ILOCK(mp); 338df8bae1dSRodney W. Grimes mp->mnt_flag |= MNT_EXPORTED; 3395da56ddbSTor Egge MNT_IUNLOCK(mp); 340df8bae1dSRodney W. Grimes } 34161e323a2SCraig Rodrigues 34261e323a2SCraig Rodrigues out: 343a7053783SKonstantin Belousov lockmgr(&mp->mnt_explock, LK_RELEASE, NULL); 34461e323a2SCraig Rodrigues /* 34561e323a2SCraig Rodrigues * Once we have executed the vfs_export() command, we do 34661e323a2SCraig Rodrigues * not want to keep the "export" option around in the 34761e323a2SCraig Rodrigues * options list, since that will cause subsequent MNT_UPDATE 34861e323a2SCraig Rodrigues * calls to fail. The export information is saved in 34961e323a2SCraig Rodrigues * mp->mnt_export, so we can safely delete the "export" mount option 35061e323a2SCraig Rodrigues * here. 35161e323a2SCraig Rodrigues */ 35261e323a2SCraig Rodrigues vfs_deleteopt(mp->mnt_optnew, "export"); 35361e323a2SCraig Rodrigues vfs_deleteopt(mp->mnt_opt, "export"); 35461e323a2SCraig Rodrigues return (error); 355df8bae1dSRodney W. Grimes } 356df8bae1dSRodney W. Grimes 357f6b4c285SDoug Rabson /* 358f6b4c285SDoug Rabson * Set the publicly exported filesystem (WebNFS). Currently, only 359f6b4c285SDoug Rabson * one public filesystem is possible in the spec (RFC 2054 and 2055) 360f6b4c285SDoug Rabson */ 361f6b4c285SDoug Rabson int 3622830e09dSCraig Rodrigues vfs_setpublicfs(struct mount *mp, struct netexport *nep, 3632830e09dSCraig Rodrigues struct export_args *argp) 364f6b4c285SDoug Rabson { 365f6b4c285SDoug Rabson int error; 366f6b4c285SDoug Rabson struct vnode *rvp; 367f6b4c285SDoug Rabson char *cp; 368f6b4c285SDoug Rabson 369f6b4c285SDoug Rabson /* 370f6b4c285SDoug Rabson * mp == NULL -> invalidate the current info, the FS is 371f6b4c285SDoug Rabson * no longer exported. May be called from either vfs_export 372f6b4c285SDoug Rabson * or unmount, so check if it hasn't already been done. 373f6b4c285SDoug Rabson */ 374f6b4c285SDoug Rabson if (mp == NULL) { 375f6b4c285SDoug Rabson if (nfs_pub.np_valid) { 376f6b4c285SDoug Rabson nfs_pub.np_valid = 0; 377f6b4c285SDoug Rabson if (nfs_pub.np_index != NULL) { 3781ede983cSDag-Erling Smørgrav free(nfs_pub.np_index, M_TEMP); 379f6b4c285SDoug Rabson nfs_pub.np_index = NULL; 380f6b4c285SDoug Rabson } 381f6b4c285SDoug Rabson } 382f6b4c285SDoug Rabson return (0); 383f6b4c285SDoug Rabson } 384f6b4c285SDoug Rabson 385f6b4c285SDoug Rabson /* 386f6b4c285SDoug Rabson * Only one allowed at a time. 387f6b4c285SDoug Rabson */ 388f6b4c285SDoug Rabson if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount) 389f6b4c285SDoug Rabson return (EBUSY); 390f6b4c285SDoug Rabson 391f6b4c285SDoug Rabson /* 392f6b4c285SDoug Rabson * Get real filehandle for root of exported FS. 393f6b4c285SDoug Rabson */ 394210a5a71SAlfred Perlstein bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle)); 395f6b4c285SDoug Rabson nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid; 396f6b4c285SDoug Rabson 397dfd233edSAttilio Rao if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp))) 398f6b4c285SDoug Rabson return (error); 399f6b4c285SDoug Rabson 40010bcafe9SPawel Jakub Dawidek if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid))) 401f6b4c285SDoug Rabson return (error); 402f6b4c285SDoug Rabson 403f6b4c285SDoug Rabson vput(rvp); 404f6b4c285SDoug Rabson 405f6b4c285SDoug Rabson /* 406f6b4c285SDoug Rabson * If an indexfile was specified, pull it in. 407f6b4c285SDoug Rabson */ 408f6b4c285SDoug Rabson if (argp->ex_indexfile != NULL) { 4095679fe19SAlexander Kabaev if (nfs_pub.np_index != NULL) 4101ede983cSDag-Erling Smørgrav nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP, 411a163d034SWarner Losh M_WAITOK); 412f6b4c285SDoug Rabson error = copyinstr(argp->ex_indexfile, nfs_pub.np_index, 413f6b4c285SDoug Rabson MAXNAMLEN, (size_t *)0); 414f6b4c285SDoug Rabson if (!error) { 415f6b4c285SDoug Rabson /* 416f6b4c285SDoug Rabson * Check for illegal filenames. 417f6b4c285SDoug Rabson */ 418f6b4c285SDoug Rabson for (cp = nfs_pub.np_index; *cp; cp++) { 419f6b4c285SDoug Rabson if (*cp == '/') { 420f6b4c285SDoug Rabson error = EINVAL; 421f6b4c285SDoug Rabson break; 422f6b4c285SDoug Rabson } 423f6b4c285SDoug Rabson } 424f6b4c285SDoug Rabson } 425f6b4c285SDoug Rabson if (error) { 4261ede983cSDag-Erling Smørgrav free(nfs_pub.np_index, M_TEMP); 4275679fe19SAlexander Kabaev nfs_pub.np_index = NULL; 428f6b4c285SDoug Rabson return (error); 429f6b4c285SDoug Rabson } 430f6b4c285SDoug Rabson } 431f6b4c285SDoug Rabson 432f6b4c285SDoug Rabson nfs_pub.np_mount = mp; 433f6b4c285SDoug Rabson nfs_pub.np_valid = 1; 434f6b4c285SDoug Rabson return (0); 435f6b4c285SDoug Rabson } 436f6b4c285SDoug Rabson 43721a90397SAlfred Perlstein /* 43821a90397SAlfred Perlstein * Used by the filesystems to determine if a given network address 4396ffb78d1SEd Maste * (passed in 'nam') is present in their exports list, returns a pointer 44021a90397SAlfred Perlstein * to struct netcred so that the filesystem can examine it for 44121a90397SAlfred Perlstein * access rights (read/write/etc). 44221a90397SAlfred Perlstein */ 443ebbfc2f8SPoul-Henning Kamp static struct netcred * 444ebbfc2f8SPoul-Henning Kamp vfs_export_lookup(struct mount *mp, struct sockaddr *nam) 445df8bae1dSRodney W. Grimes { 446a13234bbSPoul-Henning Kamp struct netexport *nep; 447df8bae1dSRodney W. Grimes register struct netcred *np; 448df8bae1dSRodney W. Grimes register struct radix_node_head *rnh; 449df8bae1dSRodney W. Grimes struct sockaddr *saddr; 450df8bae1dSRodney W. Grimes 451a13234bbSPoul-Henning Kamp nep = mp->mnt_export; 452a13234bbSPoul-Henning Kamp if (nep == NULL) 453a13234bbSPoul-Henning Kamp return (NULL); 454df8bae1dSRodney W. Grimes np = NULL; 455df8bae1dSRodney W. Grimes if (mp->mnt_flag & MNT_EXPORTED) { 456df8bae1dSRodney W. Grimes /* 457df8bae1dSRodney W. Grimes * Lookup in the export list first. 458df8bae1dSRodney W. Grimes */ 459df8bae1dSRodney W. Grimes if (nam != NULL) { 46057bf258eSGarrett Wollman saddr = nam; 4619f25cbe4SAlexander V. Chernikov rnh = NULL; 4629f25cbe4SAlexander V. Chernikov switch (saddr->sa_family) { 4639f25cbe4SAlexander V. Chernikov case AF_INET: 4649f25cbe4SAlexander V. Chernikov rnh = nep->ne4; 4659f25cbe4SAlexander V. Chernikov break; 4669f25cbe4SAlexander V. Chernikov case AF_INET6: 4679f25cbe4SAlexander V. Chernikov rnh = nep->ne6; 4689f25cbe4SAlexander V. Chernikov break; 4699f25cbe4SAlexander V. Chernikov } 470df8bae1dSRodney W. Grimes if (rnh != NULL) { 4713120b9d4SKip Macy RADIX_NODE_HEAD_RLOCK(rnh); 472df8bae1dSRodney W. Grimes np = (struct netcred *) 473*61eee0e2SAlexander V. Chernikov (*rnh->rnh_matchaddr)(saddr, &rnh->rh); 4743120b9d4SKip Macy RADIX_NODE_HEAD_RUNLOCK(rnh); 475df8bae1dSRodney W. Grimes if (np && np->netc_rnodes->rn_flags & RNF_ROOT) 476df8bae1dSRodney W. Grimes np = NULL; 477df8bae1dSRodney W. Grimes } 478df8bae1dSRodney W. Grimes } 479df8bae1dSRodney W. Grimes /* 480df8bae1dSRodney W. Grimes * If no address match, use the default if it exists. 481df8bae1dSRodney W. Grimes */ 482df8bae1dSRodney W. Grimes if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) 483df8bae1dSRodney W. Grimes np = &nep->ne_defexported; 484df8bae1dSRodney W. Grimes } 485df8bae1dSRodney W. Grimes return (np); 486df8bae1dSRodney W. Grimes } 48761f5d510SDavid Greenman 48861f5d510SDavid Greenman /* 489a13234bbSPoul-Henning Kamp * XXX: This comment comes from the deprecated ufs_check_export() 490a13234bbSPoul-Henning Kamp * XXX: and may not entirely apply, but lacking something better: 491a13234bbSPoul-Henning Kamp * This is the generic part of fhtovp called after the underlying 492a13234bbSPoul-Henning Kamp * filesystem has validated the file handle. 493a13234bbSPoul-Henning Kamp * 494a13234bbSPoul-Henning Kamp * Verify that a host should have access to a filesystem. 495a13234bbSPoul-Henning Kamp */ 496a13234bbSPoul-Henning Kamp 497a13234bbSPoul-Henning Kamp int 4982830e09dSCraig Rodrigues vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp, 499a9148abdSDoug Rabson struct ucred **credanonp, int *numsecflavors, int **secflavors) 500a13234bbSPoul-Henning Kamp { 501a13234bbSPoul-Henning Kamp struct netcred *np; 502a13234bbSPoul-Henning Kamp 503a7053783SKonstantin Belousov lockmgr(&mp->mnt_explock, LK_SHARED, NULL); 504a13234bbSPoul-Henning Kamp np = vfs_export_lookup(mp, nam); 5055679fe19SAlexander Kabaev if (np == NULL) { 506a7053783SKonstantin Belousov lockmgr(&mp->mnt_explock, LK_RELEASE, NULL); 5075679fe19SAlexander Kabaev *credanonp = NULL; 508a13234bbSPoul-Henning Kamp return (EACCES); 5095679fe19SAlexander Kabaev } 510a13234bbSPoul-Henning Kamp *extflagsp = np->netc_exflags; 5115679fe19SAlexander Kabaev if ((*credanonp = np->netc_anon) != NULL) 5125679fe19SAlexander Kabaev crhold(*credanonp); 513a9148abdSDoug Rabson if (numsecflavors) 514a9148abdSDoug Rabson *numsecflavors = np->netc_numsecflavors; 515a9148abdSDoug Rabson if (secflavors) 516a9148abdSDoug Rabson *secflavors = np->netc_secflavors; 5175679fe19SAlexander Kabaev lockmgr(&mp->mnt_explock, LK_RELEASE, NULL); 518a13234bbSPoul-Henning Kamp return (0); 519a13234bbSPoul-Henning Kamp } 520a13234bbSPoul-Henning Kamp 521