xref: /freebsd/sys/kern/vfs_export.c (revision 593efaf9f7ff8b4a961e61f55c333e63ee36495c)
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 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41057e2795SBruce Evans #include <sys/dirent.h>
42057e2795SBruce Evans #include <sys/domain.h>
43d4468577SJamie Gritton #include <sys/jail.h>
44986f4ce7SBruce Evans #include <sys/kernel.h>
45057e2795SBruce Evans #include <sys/lock.h>
46a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
47bf61e266SKris Kennaway #include <sys/mbuf.h>
48df8bae1dSRodney W. Grimes #include <sys/mount.h>
49057e2795SBruce Evans #include <sys/mutex.h>
503120b9d4SKip Macy #include <sys/rwlock.h>
517e9e371fSJohn Baldwin #include <sys/refcount.h>
52*593efaf9SJohn Baldwin #include <sys/signalvar.h>
53057e2795SBruce Evans #include <sys/socket.h>
54057e2795SBruce Evans #include <sys/systm.h>
555e950839SLuoqi Chen #include <sys/vnode.h>
56df8bae1dSRodney W. Grimes 
57057e2795SBruce Evans #include <net/radix.h>
58057e2795SBruce Evans 
595bb84bc8SRobert Watson static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure");
6055166637SPoul-Henning Kamp 
614d77a549SAlfred Perlstein static void	vfs_free_addrlist(struct netexport *nep);
624d77a549SAlfred Perlstein static int	vfs_free_netcred(struct radix_node *rn, void *w);
634d77a549SAlfred Perlstein static int	vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
644d77a549SAlfred Perlstein 		    struct export_args *argp);
65ebbfc2f8SPoul-Henning Kamp static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *);
6698d93822SBruce Evans 
67df8bae1dSRodney W. Grimes /*
68a13234bbSPoul-Henning Kamp  * Network address lookup element
69a13234bbSPoul-Henning Kamp  */
70a13234bbSPoul-Henning Kamp struct netcred {
71a13234bbSPoul-Henning Kamp 	struct	radix_node netc_rnodes[2];
72a13234bbSPoul-Henning Kamp 	int	netc_exflags;
735679fe19SAlexander Kabaev 	struct	ucred *netc_anon;
74a9148abdSDoug Rabson 	int	netc_numsecflavors;
75a9148abdSDoug Rabson 	int	netc_secflavors[MAXSECFLAVORS];
76a13234bbSPoul-Henning Kamp };
77a13234bbSPoul-Henning Kamp 
78a13234bbSPoul-Henning Kamp /*
79a13234bbSPoul-Henning Kamp  * Network export information
80a13234bbSPoul-Henning Kamp  */
81a13234bbSPoul-Henning Kamp struct netexport {
82a13234bbSPoul-Henning Kamp 	struct	netcred ne_defexported;		      /* Default export */
83a13234bbSPoul-Henning Kamp 	struct	radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
84a13234bbSPoul-Henning Kamp };
85a13234bbSPoul-Henning Kamp 
86a13234bbSPoul-Henning Kamp /*
87df8bae1dSRodney W. Grimes  * Build hash lists of net addresses and hang them off the mount point.
885679fe19SAlexander Kabaev  * Called by vfs_export() to set up the lists of export addresses.
89df8bae1dSRodney W. Grimes  */
90df8bae1dSRodney W. Grimes static int
912830e09dSCraig Rodrigues vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
922830e09dSCraig Rodrigues     struct export_args *argp)
93df8bae1dSRodney W. Grimes {
94df8bae1dSRodney W. Grimes 	register struct netcred *np;
95df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
96df8bae1dSRodney W. Grimes 	register int i;
97df8bae1dSRodney W. Grimes 	struct radix_node *rn;
98df8bae1dSRodney W. Grimes 	struct sockaddr *saddr, *smask = 0;
99df8bae1dSRodney W. Grimes 	struct domain *dom;
100df8bae1dSRodney W. Grimes 	int error;
101df8bae1dSRodney W. Grimes 
102e74d4831SDima Dorfman 	/*
103e74d4831SDima Dorfman 	 * XXX: This routine converts from a `struct xucred'
104e74d4831SDima Dorfman 	 * (argp->ex_anon) to a `struct ucred' (np->netc_anon).  This
105e74d4831SDima Dorfman 	 * operation is questionable; for example, what should be done
106e74d4831SDima Dorfman 	 * with fields like cr_uidinfo and cr_prison?  Currently, this
107e74d4831SDima Dorfman 	 * routine does not touch them (leaves them as NULL).
108e74d4831SDima Dorfman 	 */
10961e323a2SCraig Rodrigues 	if (argp->ex_anon.cr_version != XUCRED_VERSION) {
11061e323a2SCraig Rodrigues 		vfs_mount_error(mp, "ex_anon.cr_version: %d != %d",
11161e323a2SCraig Rodrigues 		    argp->ex_anon.cr_version, XUCRED_VERSION);
112e74d4831SDima Dorfman 		return (EINVAL);
11361e323a2SCraig Rodrigues 	}
114e74d4831SDima Dorfman 
115df8bae1dSRodney W. Grimes 	if (argp->ex_addrlen == 0) {
11661e323a2SCraig Rodrigues 		if (mp->mnt_flag & MNT_DEFEXPORTED) {
11761e323a2SCraig Rodrigues 			vfs_mount_error(mp,
11861e323a2SCraig Rodrigues 			    "MNT_DEFEXPORTED already set for mount %p", mp);
119df8bae1dSRodney W. Grimes 			return (EPERM);
12061e323a2SCraig Rodrigues 		}
121df8bae1dSRodney W. Grimes 		np = &nep->ne_defexported;
122df8bae1dSRodney W. Grimes 		np->netc_exflags = argp->ex_flags;
1235679fe19SAlexander Kabaev 		np->netc_anon = crget();
1245679fe19SAlexander Kabaev 		np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
125838d9858SBrooks Davis 		crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups,
126838d9858SBrooks Davis 		    argp->ex_anon.cr_groups);
127d4468577SJamie Gritton 		np->netc_anon->cr_prison = &prison0;
128d4468577SJamie Gritton 		prison_hold(np->netc_anon->cr_prison);
129a9148abdSDoug Rabson 		np->netc_numsecflavors = argp->ex_numsecflavors;
130a9148abdSDoug Rabson 		bcopy(argp->ex_secflavors, np->netc_secflavors,
131a9148abdSDoug Rabson 		    sizeof(np->netc_secflavors));
1325da56ddbSTor Egge 		MNT_ILOCK(mp);
133df8bae1dSRodney W. Grimes 		mp->mnt_flag |= MNT_DEFEXPORTED;
1345da56ddbSTor Egge 		MNT_IUNLOCK(mp);
135df8bae1dSRodney W. Grimes 		return (0);
136df8bae1dSRodney W. Grimes 	}
137bf61e266SKris Kennaway 
138c43cad1aSScott Long #if MSIZE <= 256
1393a13c9ccSCraig Rodrigues 	if (argp->ex_addrlen > MLEN) {
1403a13c9ccSCraig Rodrigues 		vfs_mount_error(mp, "ex_addrlen %d is greater than %d",
1413a13c9ccSCraig Rodrigues 		    argp->ex_addrlen, MLEN);
142bf61e266SKris Kennaway 		return (EINVAL);
1433a13c9ccSCraig Rodrigues 	}
144c43cad1aSScott Long #endif
145bf61e266SKris Kennaway 
146df8bae1dSRodney W. Grimes 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
147a163d034SWarner Losh 	np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
148df8bae1dSRodney W. Grimes 	saddr = (struct sockaddr *) (np + 1);
149210a5a71SAlfred Perlstein 	if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
150df8bae1dSRodney W. Grimes 		goto out;
15161e323a2SCraig Rodrigues 	if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) {
152b96e102aSColin Percival 		error = EINVAL;
15361e323a2SCraig Rodrigues 		vfs_mount_error(mp, "Invalid saddr->sa_family: %d");
154b96e102aSColin Percival 		goto out;
155b96e102aSColin Percival 	}
156df8bae1dSRodney W. Grimes 	if (saddr->sa_len > argp->ex_addrlen)
157df8bae1dSRodney W. Grimes 		saddr->sa_len = argp->ex_addrlen;
158df8bae1dSRodney W. Grimes 	if (argp->ex_masklen) {
159c5e3ef7eSAlfred Perlstein 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
160210a5a71SAlfred Perlstein 		error = copyin(argp->ex_mask, smask, argp->ex_masklen);
161df8bae1dSRodney W. Grimes 		if (error)
162df8bae1dSRodney W. Grimes 			goto out;
163df8bae1dSRodney W. Grimes 		if (smask->sa_len > argp->ex_masklen)
164df8bae1dSRodney W. Grimes 			smask->sa_len = argp->ex_masklen;
165df8bae1dSRodney W. Grimes 	}
166df8bae1dSRodney W. Grimes 	i = saddr->sa_family;
167956b0b65SJeffrey Hsu 	if ((rnh = nep->ne_rtable[i]) == NULL) {
168df8bae1dSRodney W. Grimes 		/*
1690d94caffSDavid Greenman 		 * Seems silly to initialize every AF when most are not used,
1700d94caffSDavid Greenman 		 * do so on demand here
171df8bae1dSRodney W. Grimes 		 */
1728b07e49aSJulian Elischer 		for (dom = domains; dom; dom = dom->dom_next) {
1738b07e49aSJulian Elischer 			KASSERT(((i == AF_INET) || (i == AF_INET6)),
1748b07e49aSJulian Elischer 			    ("unexpected protocol in vfs_hang_addrlist"));
175df8bae1dSRodney W. Grimes 			if (dom->dom_family == i && dom->dom_rtattach) {
1768b07e49aSJulian Elischer 				/*
1778b07e49aSJulian Elischer 				 * XXX MRT
1788b07e49aSJulian Elischer 				 * The INET and INET6 domains know the
1798b07e49aSJulian Elischer 				 * offset already. We don't need to send it
1808b07e49aSJulian Elischer 				 * So we just use it as a flag to say that
1818b07e49aSJulian Elischer 				 * we are or are not setting up a real routing
1828b07e49aSJulian Elischer 				 * table. Only IP and IPV6 need have this
1838b07e49aSJulian Elischer 				 * be 0 so all other protocols can stay the
1848b07e49aSJulian Elischer 				 * same (ABI compatible).
1858b07e49aSJulian Elischer 				 */
1868b07e49aSJulian Elischer 				dom->dom_rtattach(
1878b07e49aSJulian Elischer 				    (void **) &nep->ne_rtable[i], 0);
188df8bae1dSRodney W. Grimes 				break;
189df8bae1dSRodney W. Grimes 			}
1908b07e49aSJulian Elischer 		}
191956b0b65SJeffrey Hsu 		if ((rnh = nep->ne_rtable[i]) == NULL) {
192df8bae1dSRodney W. Grimes 			error = ENOBUFS;
19361e323a2SCraig Rodrigues 			vfs_mount_error(mp, "%s %s %d",
19461e323a2SCraig Rodrigues 			    "Unable to initialize radix node head ",
19561e323a2SCraig Rodrigues 			    "for address family", i);
196df8bae1dSRodney W. Grimes 			goto out;
197df8bae1dSRodney W. Grimes 		}
198df8bae1dSRodney W. Grimes 	}
199956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
20097bb78acSAlfred Perlstein 	rn = (*rnh->rnh_addaddr)(saddr, smask, rnh, np->netc_rnodes);
201956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
202956b0b65SJeffrey Hsu 	if (rn == NULL || np != (struct netcred *)rn) {	/* already exists */
20337a6b453SAlfred Perlstein 		error = EPERM;
2043a13c9ccSCraig Rodrigues 		vfs_mount_error(mp, "Invalid radix node head, rn: %p %p",
2053a13c9ccSCraig Rodrigues 		    rn, np);
20637a6b453SAlfred Perlstein 		goto out;
207df8bae1dSRodney W. Grimes 	}
208df8bae1dSRodney W. Grimes 	np->netc_exflags = argp->ex_flags;
2095679fe19SAlexander Kabaev 	np->netc_anon = crget();
2105679fe19SAlexander Kabaev 	np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
211838d9858SBrooks Davis 	crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups,
212f1c4014cSRick Macklem 	    argp->ex_anon.cr_groups);
213d4468577SJamie Gritton 	np->netc_anon->cr_prison = &prison0;
214d4468577SJamie Gritton 	prison_hold(np->netc_anon->cr_prison);
215a9148abdSDoug Rabson 	np->netc_numsecflavors = argp->ex_numsecflavors;
216a9148abdSDoug Rabson 	bcopy(argp->ex_secflavors, np->netc_secflavors,
217a9148abdSDoug Rabson 	    sizeof(np->netc_secflavors));
218df8bae1dSRodney W. Grimes 	return (0);
219df8bae1dSRodney W. Grimes out:
220df8bae1dSRodney W. Grimes 	free(np, M_NETADDR);
221df8bae1dSRodney W. Grimes 	return (error);
222df8bae1dSRodney W. Grimes }
223df8bae1dSRodney W. Grimes 
224a863c0fbSEivind Eklund /* Helper for vfs_free_addrlist. */
225df8bae1dSRodney W. Grimes /* ARGSUSED */
226df8bae1dSRodney W. Grimes static int
2272830e09dSCraig Rodrigues vfs_free_netcred(struct radix_node *rn, void *w)
228df8bae1dSRodney W. Grimes {
229a73034efSKonstantin Belousov 	struct radix_node_head *rnh = (struct radix_node_head *) w;
230a73034efSKonstantin Belousov 	struct ucred *cred;
231df8bae1dSRodney W. Grimes 
232df8bae1dSRodney W. Grimes 	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
233a73034efSKonstantin Belousov 	cred = ((struct netcred *)rn)->netc_anon;
234a73034efSKonstantin Belousov 	if (cred != NULL)
235a73034efSKonstantin Belousov 		crfree(cred);
236210a5a71SAlfred Perlstein 	free(rn, M_NETADDR);
237df8bae1dSRodney W. Grimes 	return (0);
238df8bae1dSRodney W. Grimes }
239df8bae1dSRodney W. Grimes 
240df8bae1dSRodney W. Grimes /*
241df8bae1dSRodney W. Grimes  * Free the net address hash lists that are hanging off the mount points.
242df8bae1dSRodney W. Grimes  */
243df8bae1dSRodney W. Grimes static void
2442830e09dSCraig Rodrigues vfs_free_addrlist(struct netexport *nep)
245df8bae1dSRodney W. Grimes {
246a73034efSKonstantin Belousov 	int i;
247a73034efSKonstantin Belousov 	struct radix_node_head *rnh;
248a73034efSKonstantin Belousov 	struct ucred *cred;
249df8bae1dSRodney W. Grimes 
250a73034efSKonstantin Belousov 	for (i = 0; i <= AF_MAX; i++) {
251bb56ec4aSPoul-Henning Kamp 		if ((rnh = nep->ne_rtable[i])) {
252956b0b65SJeffrey Hsu 			RADIX_NODE_HEAD_LOCK(rnh);
253210a5a71SAlfred Perlstein 			(*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
25408a2459eSKip Macy 			RADIX_NODE_HEAD_UNLOCK(rnh);
255956b0b65SJeffrey Hsu 			RADIX_NODE_HEAD_DESTROY(rnh);
256210a5a71SAlfred Perlstein 			free(rnh, M_RTABLE);
257956b0b65SJeffrey Hsu 			nep->ne_rtable[i] = NULL;	/* not SMP safe XXX */
258df8bae1dSRodney W. Grimes 		}
259df8bae1dSRodney W. Grimes 	}
260a73034efSKonstantin Belousov 	cred = nep->ne_defexported.netc_anon;
261a73034efSKonstantin Belousov 	if (cred != NULL)
262a73034efSKonstantin Belousov 		crfree(cred);
263a73034efSKonstantin Belousov 
264a73034efSKonstantin Belousov }
265df8bae1dSRodney W. Grimes 
26621a90397SAlfred Perlstein /*
26721a90397SAlfred Perlstein  * High level function to manipulate export options on a mount point
26821a90397SAlfred Perlstein  * and the passed in netexport.
26921a90397SAlfred Perlstein  * Struct export_args *argp is the variable used to twiddle options,
27021a90397SAlfred Perlstein  * the structure is described in sys/mount.h
27121a90397SAlfred Perlstein  */
272df8bae1dSRodney W. Grimes int
2732830e09dSCraig Rodrigues vfs_export(struct mount *mp, struct export_args *argp)
274df8bae1dSRodney W. Grimes {
275a13234bbSPoul-Henning Kamp 	struct netexport *nep;
276df8bae1dSRodney W. Grimes 	int error;
277df8bae1dSRodney W. Grimes 
278a9148abdSDoug Rabson 	if (argp->ex_numsecflavors < 0
279a9148abdSDoug Rabson 	    || argp->ex_numsecflavors >= MAXSECFLAVORS)
280a9148abdSDoug Rabson 		return (EINVAL);
281a9148abdSDoug Rabson 
28261e323a2SCraig Rodrigues 	error = 0;
283a7053783SKonstantin Belousov 	lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL);
2845679fe19SAlexander Kabaev 	nep = mp->mnt_export;
285df8bae1dSRodney W. Grimes 	if (argp->ex_flags & MNT_DELEXPORT) {
28603eff583SCraig Rodrigues 		if (nep == NULL) {
28761e323a2SCraig Rodrigues 			error = ENOENT;
28861e323a2SCraig Rodrigues 			goto out;
28903eff583SCraig Rodrigues 		}
290f6b4c285SDoug Rabson 		if (mp->mnt_flag & MNT_EXPUBLIC) {
291f6b4c285SDoug Rabson 			vfs_setpublicfs(NULL, NULL, NULL);
2925da56ddbSTor Egge 			MNT_ILOCK(mp);
293f6b4c285SDoug Rabson 			mp->mnt_flag &= ~MNT_EXPUBLIC;
2945da56ddbSTor Egge 			MNT_IUNLOCK(mp);
295f6b4c285SDoug Rabson 		}
296df8bae1dSRodney W. Grimes 		vfs_free_addrlist(nep);
297a13234bbSPoul-Henning Kamp 		mp->mnt_export = NULL;
298a13234bbSPoul-Henning Kamp 		free(nep, M_MOUNT);
2990ca9ed86SAlexander Kabaev 		nep = NULL;
3005da56ddbSTor Egge 		MNT_ILOCK(mp);
301df8bae1dSRodney W. Grimes 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
3025da56ddbSTor Egge 		MNT_IUNLOCK(mp);
303df8bae1dSRodney W. Grimes 	}
304df8bae1dSRodney W. Grimes 	if (argp->ex_flags & MNT_EXPORTED) {
305a13234bbSPoul-Henning Kamp 		if (nep == NULL) {
306a163d034SWarner Losh 			nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
307a13234bbSPoul-Henning Kamp 			mp->mnt_export = nep;
308a13234bbSPoul-Henning Kamp 		}
309f6b4c285SDoug Rabson 		if (argp->ex_flags & MNT_EXPUBLIC) {
310f6b4c285SDoug Rabson 			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
31161e323a2SCraig Rodrigues 				goto out;
3125da56ddbSTor Egge 			MNT_ILOCK(mp);
313f6b4c285SDoug Rabson 			mp->mnt_flag |= MNT_EXPUBLIC;
3145da56ddbSTor Egge 			MNT_IUNLOCK(mp);
315f6b4c285SDoug Rabson 		}
316bb56ec4aSPoul-Henning Kamp 		if ((error = vfs_hang_addrlist(mp, nep, argp)))
31761e323a2SCraig Rodrigues 			goto out;
3185da56ddbSTor Egge 		MNT_ILOCK(mp);
319df8bae1dSRodney W. Grimes 		mp->mnt_flag |= MNT_EXPORTED;
3205da56ddbSTor Egge 		MNT_IUNLOCK(mp);
321df8bae1dSRodney W. Grimes 	}
32261e323a2SCraig Rodrigues 
32361e323a2SCraig Rodrigues out:
324a7053783SKonstantin Belousov 	lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
32561e323a2SCraig Rodrigues 	/*
32661e323a2SCraig Rodrigues 	 * Once we have executed the vfs_export() command, we do
32761e323a2SCraig Rodrigues 	 * not want to keep the "export" option around in the
32861e323a2SCraig Rodrigues 	 * options list, since that will cause subsequent MNT_UPDATE
32961e323a2SCraig Rodrigues 	 * calls to fail.  The export information is saved in
33061e323a2SCraig Rodrigues 	 * mp->mnt_export, so we can safely delete the "export" mount option
33161e323a2SCraig Rodrigues 	 * here.
33261e323a2SCraig Rodrigues 	 */
33361e323a2SCraig Rodrigues 	vfs_deleteopt(mp->mnt_optnew, "export");
33461e323a2SCraig Rodrigues 	vfs_deleteopt(mp->mnt_opt, "export");
33561e323a2SCraig Rodrigues 	return (error);
336df8bae1dSRodney W. Grimes }
337df8bae1dSRodney W. Grimes 
338f6b4c285SDoug Rabson /*
339f6b4c285SDoug Rabson  * Set the publicly exported filesystem (WebNFS). Currently, only
340f6b4c285SDoug Rabson  * one public filesystem is possible in the spec (RFC 2054 and 2055)
341f6b4c285SDoug Rabson  */
342f6b4c285SDoug Rabson int
3432830e09dSCraig Rodrigues vfs_setpublicfs(struct mount *mp, struct netexport *nep,
3442830e09dSCraig Rodrigues     struct export_args *argp)
345f6b4c285SDoug Rabson {
346f6b4c285SDoug Rabson 	int error;
347f6b4c285SDoug Rabson 	struct vnode *rvp;
348f6b4c285SDoug Rabson 	char *cp;
349f6b4c285SDoug Rabson 
350f6b4c285SDoug Rabson 	/*
351f6b4c285SDoug Rabson 	 * mp == NULL -> invalidate the current info, the FS is
352f6b4c285SDoug Rabson 	 * no longer exported. May be called from either vfs_export
353f6b4c285SDoug Rabson 	 * or unmount, so check if it hasn't already been done.
354f6b4c285SDoug Rabson 	 */
355f6b4c285SDoug Rabson 	if (mp == NULL) {
356f6b4c285SDoug Rabson 		if (nfs_pub.np_valid) {
357f6b4c285SDoug Rabson 			nfs_pub.np_valid = 0;
358f6b4c285SDoug Rabson 			if (nfs_pub.np_index != NULL) {
3591ede983cSDag-Erling Smørgrav 				free(nfs_pub.np_index, M_TEMP);
360f6b4c285SDoug Rabson 				nfs_pub.np_index = NULL;
361f6b4c285SDoug Rabson 			}
362f6b4c285SDoug Rabson 		}
363f6b4c285SDoug Rabson 		return (0);
364f6b4c285SDoug Rabson 	}
365f6b4c285SDoug Rabson 
366f6b4c285SDoug Rabson 	/*
367f6b4c285SDoug Rabson 	 * Only one allowed at a time.
368f6b4c285SDoug Rabson 	 */
369f6b4c285SDoug Rabson 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
370f6b4c285SDoug Rabson 		return (EBUSY);
371f6b4c285SDoug Rabson 
372f6b4c285SDoug Rabson 	/*
373f6b4c285SDoug Rabson 	 * Get real filehandle for root of exported FS.
374f6b4c285SDoug Rabson 	 */
375210a5a71SAlfred Perlstein 	bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
376f6b4c285SDoug Rabson 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
377f6b4c285SDoug Rabson 
378dfd233edSAttilio Rao 	if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp)))
379f6b4c285SDoug Rabson 		return (error);
380f6b4c285SDoug Rabson 
38110bcafe9SPawel Jakub Dawidek 	if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
382f6b4c285SDoug Rabson 		return (error);
383f6b4c285SDoug Rabson 
384f6b4c285SDoug Rabson 	vput(rvp);
385f6b4c285SDoug Rabson 
386f6b4c285SDoug Rabson 	/*
387f6b4c285SDoug Rabson 	 * If an indexfile was specified, pull it in.
388f6b4c285SDoug Rabson 	 */
389f6b4c285SDoug Rabson 	if (argp->ex_indexfile != NULL) {
3905679fe19SAlexander Kabaev 		if (nfs_pub.np_index != NULL)
3911ede983cSDag-Erling Smørgrav 			nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP,
392a163d034SWarner Losh 			    M_WAITOK);
393f6b4c285SDoug Rabson 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
394f6b4c285SDoug Rabson 		    MAXNAMLEN, (size_t *)0);
395f6b4c285SDoug Rabson 		if (!error) {
396f6b4c285SDoug Rabson 			/*
397f6b4c285SDoug Rabson 			 * Check for illegal filenames.
398f6b4c285SDoug Rabson 			 */
399f6b4c285SDoug Rabson 			for (cp = nfs_pub.np_index; *cp; cp++) {
400f6b4c285SDoug Rabson 				if (*cp == '/') {
401f6b4c285SDoug Rabson 					error = EINVAL;
402f6b4c285SDoug Rabson 					break;
403f6b4c285SDoug Rabson 				}
404f6b4c285SDoug Rabson 			}
405f6b4c285SDoug Rabson 		}
406f6b4c285SDoug Rabson 		if (error) {
4071ede983cSDag-Erling Smørgrav 			free(nfs_pub.np_index, M_TEMP);
4085679fe19SAlexander Kabaev 			nfs_pub.np_index = NULL;
409f6b4c285SDoug Rabson 			return (error);
410f6b4c285SDoug Rabson 		}
411f6b4c285SDoug Rabson 	}
412f6b4c285SDoug Rabson 
413f6b4c285SDoug Rabson 	nfs_pub.np_mount = mp;
414f6b4c285SDoug Rabson 	nfs_pub.np_valid = 1;
415f6b4c285SDoug Rabson 	return (0);
416f6b4c285SDoug Rabson }
417f6b4c285SDoug Rabson 
41821a90397SAlfred Perlstein /*
41921a90397SAlfred Perlstein  * Used by the filesystems to determine if a given network address
4206ffb78d1SEd Maste  * (passed in 'nam') is present in their exports list, returns a pointer
42121a90397SAlfred Perlstein  * to struct netcred so that the filesystem can examine it for
42221a90397SAlfred Perlstein  * access rights (read/write/etc).
42321a90397SAlfred Perlstein  */
424ebbfc2f8SPoul-Henning Kamp static struct netcred *
425ebbfc2f8SPoul-Henning Kamp vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
426df8bae1dSRodney W. Grimes {
427a13234bbSPoul-Henning Kamp 	struct netexport *nep;
428df8bae1dSRodney W. Grimes 	register struct netcred *np;
429df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
430df8bae1dSRodney W. Grimes 	struct sockaddr *saddr;
431df8bae1dSRodney W. Grimes 
432a13234bbSPoul-Henning Kamp 	nep = mp->mnt_export;
433a13234bbSPoul-Henning Kamp 	if (nep == NULL)
434a13234bbSPoul-Henning Kamp 		return (NULL);
435df8bae1dSRodney W. Grimes 	np = NULL;
436df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_EXPORTED) {
437df8bae1dSRodney W. Grimes 		/*
438df8bae1dSRodney W. Grimes 		 * Lookup in the export list first.
439df8bae1dSRodney W. Grimes 		 */
440df8bae1dSRodney W. Grimes 		if (nam != NULL) {
44157bf258eSGarrett Wollman 			saddr = nam;
442df8bae1dSRodney W. Grimes 			rnh = nep->ne_rtable[saddr->sa_family];
443df8bae1dSRodney W. Grimes 			if (rnh != NULL) {
4443120b9d4SKip Macy 				RADIX_NODE_HEAD_RLOCK(rnh);
445df8bae1dSRodney W. Grimes 				np = (struct netcred *)
44697bb78acSAlfred Perlstein 				    (*rnh->rnh_matchaddr)(saddr, rnh);
4473120b9d4SKip Macy 				RADIX_NODE_HEAD_RUNLOCK(rnh);
448df8bae1dSRodney W. Grimes 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
449df8bae1dSRodney W. Grimes 					np = NULL;
450df8bae1dSRodney W. Grimes 			}
451df8bae1dSRodney W. Grimes 		}
452df8bae1dSRodney W. Grimes 		/*
453df8bae1dSRodney W. Grimes 		 * If no address match, use the default if it exists.
454df8bae1dSRodney W. Grimes 		 */
455df8bae1dSRodney W. Grimes 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
456df8bae1dSRodney W. Grimes 			np = &nep->ne_defexported;
457df8bae1dSRodney W. Grimes 	}
458df8bae1dSRodney W. Grimes 	return (np);
459df8bae1dSRodney W. Grimes }
46061f5d510SDavid Greenman 
46161f5d510SDavid Greenman /*
462a13234bbSPoul-Henning Kamp  * XXX: This comment comes from the deprecated ufs_check_export()
463a13234bbSPoul-Henning Kamp  * XXX: and may not entirely apply, but lacking something better:
464a13234bbSPoul-Henning Kamp  * This is the generic part of fhtovp called after the underlying
465a13234bbSPoul-Henning Kamp  * filesystem has validated the file handle.
466a13234bbSPoul-Henning Kamp  *
467a13234bbSPoul-Henning Kamp  * Verify that a host should have access to a filesystem.
468a13234bbSPoul-Henning Kamp  */
469a13234bbSPoul-Henning Kamp 
470a13234bbSPoul-Henning Kamp int
4712830e09dSCraig Rodrigues vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
472a9148abdSDoug Rabson     struct ucred **credanonp, int *numsecflavors, int **secflavors)
473a13234bbSPoul-Henning Kamp {
474a13234bbSPoul-Henning Kamp 	struct netcred *np;
475a13234bbSPoul-Henning Kamp 
476a7053783SKonstantin Belousov 	lockmgr(&mp->mnt_explock, LK_SHARED, NULL);
477a13234bbSPoul-Henning Kamp 	np = vfs_export_lookup(mp, nam);
4785679fe19SAlexander Kabaev 	if (np == NULL) {
479a7053783SKonstantin Belousov 		lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
4805679fe19SAlexander Kabaev 		*credanonp = NULL;
481a13234bbSPoul-Henning Kamp 		return (EACCES);
4825679fe19SAlexander Kabaev 	}
483a13234bbSPoul-Henning Kamp 	*extflagsp = np->netc_exflags;
4845679fe19SAlexander Kabaev 	if ((*credanonp = np->netc_anon) != NULL)
4855679fe19SAlexander Kabaev 		crhold(*credanonp);
486a9148abdSDoug Rabson 	if (numsecflavors)
487a9148abdSDoug Rabson 		*numsecflavors = np->netc_numsecflavors;
488a9148abdSDoug Rabson 	if (secflavors)
489a9148abdSDoug Rabson 		*secflavors = np->netc_secflavors;
4905679fe19SAlexander Kabaev 	lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
491a13234bbSPoul-Henning Kamp 	return (0);
492a13234bbSPoul-Henning Kamp }
493a13234bbSPoul-Henning Kamp 
494