xref: /freebsd/sys/kern/vfs_export.c (revision ebbfc2f82d330ab6e2b510077dc9e35b2000a504)
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>
43986f4ce7SBruce Evans #include <sys/kernel.h>
44057e2795SBruce Evans #include <sys/lock.h>
45a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
46bf61e266SKris Kennaway #include <sys/mbuf.h>
47df8bae1dSRodney W. Grimes #include <sys/mount.h>
48057e2795SBruce Evans #include <sys/mutex.h>
49057e2795SBruce Evans #include <sys/socket.h>
50057e2795SBruce Evans #include <sys/systm.h>
515e950839SLuoqi Chen #include <sys/vnode.h>
52df8bae1dSRodney W. Grimes 
53057e2795SBruce Evans #include <net/radix.h>
54057e2795SBruce Evans 
55a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
5655166637SPoul-Henning Kamp 
574d77a549SAlfred Perlstein static void	vfs_free_addrlist(struct netexport *nep);
584d77a549SAlfred Perlstein static int	vfs_free_netcred(struct radix_node *rn, void *w);
594d77a549SAlfred Perlstein static int	vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
604d77a549SAlfred Perlstein 		    struct export_args *argp);
61ebbfc2f8SPoul-Henning Kamp static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *);
6298d93822SBruce Evans 
63df8bae1dSRodney W. Grimes /*
64a13234bbSPoul-Henning Kamp  * Network address lookup element
65a13234bbSPoul-Henning Kamp  */
66a13234bbSPoul-Henning Kamp struct netcred {
67a13234bbSPoul-Henning Kamp 	struct	radix_node netc_rnodes[2];
68a13234bbSPoul-Henning Kamp 	int	netc_exflags;
69a13234bbSPoul-Henning Kamp 	struct	ucred netc_anon;
70a13234bbSPoul-Henning Kamp };
71a13234bbSPoul-Henning Kamp 
72a13234bbSPoul-Henning Kamp /*
73a13234bbSPoul-Henning Kamp  * Network export information
74a13234bbSPoul-Henning Kamp  */
75a13234bbSPoul-Henning Kamp struct netexport {
76a13234bbSPoul-Henning Kamp 	struct	netcred ne_defexported;		      /* Default export */
77a13234bbSPoul-Henning Kamp 	struct	radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
78a13234bbSPoul-Henning Kamp };
79a13234bbSPoul-Henning Kamp 
80a13234bbSPoul-Henning Kamp /*
81df8bae1dSRodney W. Grimes  * Build hash lists of net addresses and hang them off the mount point.
82df8bae1dSRodney W. Grimes  * Called by ufs_mount() to set up the lists of export addresses.
83df8bae1dSRodney W. Grimes  */
84df8bae1dSRodney W. Grimes static int
85514ede09SBruce Evans vfs_hang_addrlist(mp, nep, argp)
86514ede09SBruce Evans 	struct mount *mp;
87514ede09SBruce Evans 	struct netexport *nep;
88514ede09SBruce Evans 	struct export_args *argp;
89df8bae1dSRodney W. Grimes {
90df8bae1dSRodney W. Grimes 	register struct netcred *np;
91df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
92df8bae1dSRodney W. Grimes 	register int i;
93df8bae1dSRodney W. Grimes 	struct radix_node *rn;
94df8bae1dSRodney W. Grimes 	struct sockaddr *saddr, *smask = 0;
95df8bae1dSRodney W. Grimes 	struct domain *dom;
96df8bae1dSRodney W. Grimes 	int error;
97df8bae1dSRodney W. Grimes 
98e74d4831SDima Dorfman 	/*
99e74d4831SDima Dorfman 	 * XXX: This routine converts from a `struct xucred'
100e74d4831SDima Dorfman 	 * (argp->ex_anon) to a `struct ucred' (np->netc_anon).  This
101e74d4831SDima Dorfman 	 * operation is questionable; for example, what should be done
102e74d4831SDima Dorfman 	 * with fields like cr_uidinfo and cr_prison?  Currently, this
103e74d4831SDima Dorfman 	 * routine does not touch them (leaves them as NULL).
104e74d4831SDima Dorfman 	 */
105e74d4831SDima Dorfman 	if (argp->ex_anon.cr_version != XUCRED_VERSION)
106e74d4831SDima Dorfman 		return (EINVAL);
107e74d4831SDima Dorfman 
108df8bae1dSRodney W. Grimes 	if (argp->ex_addrlen == 0) {
109df8bae1dSRodney W. Grimes 		if (mp->mnt_flag & MNT_DEFEXPORTED)
110df8bae1dSRodney W. Grimes 			return (EPERM);
111df8bae1dSRodney W. Grimes 		np = &nep->ne_defexported;
112df8bae1dSRodney W. Grimes 		np->netc_exflags = argp->ex_flags;
113c0511d3bSBrian Feldman 		bzero(&np->netc_anon, sizeof(np->netc_anon));
114c0511d3bSBrian Feldman 		np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
115c0511d3bSBrian Feldman 		np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
116c0511d3bSBrian Feldman 		bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
117c0511d3bSBrian Feldman 		    sizeof(np->netc_anon.cr_groups));
118df8bae1dSRodney W. Grimes 		np->netc_anon.cr_ref = 1;
119df8bae1dSRodney W. Grimes 		mp->mnt_flag |= MNT_DEFEXPORTED;
120df8bae1dSRodney W. Grimes 		return (0);
121df8bae1dSRodney W. Grimes 	}
122bf61e266SKris Kennaway 
123c43cad1aSScott Long #if MSIZE <= 256
124bf61e266SKris Kennaway 	if (argp->ex_addrlen > MLEN)
125bf61e266SKris Kennaway 		return (EINVAL);
126c43cad1aSScott Long #endif
127bf61e266SKris Kennaway 
128df8bae1dSRodney W. Grimes 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
129a163d034SWarner Losh 	np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
130df8bae1dSRodney W. Grimes 	saddr = (struct sockaddr *) (np + 1);
131210a5a71SAlfred Perlstein 	if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
132df8bae1dSRodney W. Grimes 		goto out;
13340ab7ed9SColin Percival 	if (saddr->sa_family > AF_MAX) {
134b96e102aSColin Percival 		error = EINVAL;
135b96e102aSColin Percival 		goto out;
136b96e102aSColin Percival 	}
137df8bae1dSRodney W. Grimes 	if (saddr->sa_len > argp->ex_addrlen)
138df8bae1dSRodney W. Grimes 		saddr->sa_len = argp->ex_addrlen;
139df8bae1dSRodney W. Grimes 	if (argp->ex_masklen) {
140c5e3ef7eSAlfred Perlstein 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
141210a5a71SAlfred Perlstein 		error = copyin(argp->ex_mask, smask, argp->ex_masklen);
142df8bae1dSRodney W. Grimes 		if (error)
143df8bae1dSRodney W. Grimes 			goto out;
144df8bae1dSRodney W. Grimes 		if (smask->sa_len > argp->ex_masklen)
145df8bae1dSRodney W. Grimes 			smask->sa_len = argp->ex_masklen;
146df8bae1dSRodney W. Grimes 	}
147df8bae1dSRodney W. Grimes 	i = saddr->sa_family;
148956b0b65SJeffrey Hsu 	if ((rnh = nep->ne_rtable[i]) == NULL) {
149df8bae1dSRodney W. Grimes 		/*
1500d94caffSDavid Greenman 		 * Seems silly to initialize every AF when most are not used,
1510d94caffSDavid Greenman 		 * do so on demand here
152df8bae1dSRodney W. Grimes 		 */
153df8bae1dSRodney W. Grimes 		for (dom = domains; dom; dom = dom->dom_next)
154df8bae1dSRodney W. Grimes 			if (dom->dom_family == i && dom->dom_rtattach) {
155df8bae1dSRodney W. Grimes 				dom->dom_rtattach((void **) &nep->ne_rtable[i],
156df8bae1dSRodney W. Grimes 				    dom->dom_rtoffset);
157df8bae1dSRodney W. Grimes 				break;
158df8bae1dSRodney W. Grimes 			}
159956b0b65SJeffrey Hsu 		if ((rnh = nep->ne_rtable[i]) == NULL) {
160df8bae1dSRodney W. Grimes 			error = ENOBUFS;
161df8bae1dSRodney W. Grimes 			goto out;
162df8bae1dSRodney W. Grimes 		}
163df8bae1dSRodney W. Grimes 	}
164956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_LOCK(rnh);
16597bb78acSAlfred Perlstein 	rn = (*rnh->rnh_addaddr)(saddr, smask, rnh, np->netc_rnodes);
166956b0b65SJeffrey Hsu 	RADIX_NODE_HEAD_UNLOCK(rnh);
167956b0b65SJeffrey Hsu 	if (rn == NULL || np != (struct netcred *)rn) {	/* already exists */
16837a6b453SAlfred Perlstein 		error = EPERM;
16937a6b453SAlfred Perlstein 		goto out;
170df8bae1dSRodney W. Grimes 	}
171df8bae1dSRodney W. Grimes 	np->netc_exflags = argp->ex_flags;
172c0511d3bSBrian Feldman 	bzero(&np->netc_anon, sizeof(np->netc_anon));
173c0511d3bSBrian Feldman 	np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
174c0511d3bSBrian Feldman 	np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
175c0511d3bSBrian Feldman 	bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
176c0511d3bSBrian Feldman 	    sizeof(np->netc_anon.cr_groups));
177df8bae1dSRodney W. Grimes 	np->netc_anon.cr_ref = 1;
178df8bae1dSRodney W. Grimes 	return (0);
179df8bae1dSRodney W. Grimes out:
180df8bae1dSRodney W. Grimes 	free(np, M_NETADDR);
181df8bae1dSRodney W. Grimes 	return (error);
182df8bae1dSRodney W. Grimes }
183df8bae1dSRodney W. Grimes 
184a863c0fbSEivind Eklund /* Helper for vfs_free_addrlist. */
185df8bae1dSRodney W. Grimes /* ARGSUSED */
186df8bae1dSRodney W. Grimes static int
187514ede09SBruce Evans vfs_free_netcred(rn, w)
188514ede09SBruce Evans 	struct radix_node *rn;
189514ede09SBruce Evans 	void *w;
190df8bae1dSRodney W. Grimes {
191df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh = (struct radix_node_head *) w;
192df8bae1dSRodney W. Grimes 
193df8bae1dSRodney W. Grimes 	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
194210a5a71SAlfred Perlstein 	free(rn, M_NETADDR);
195df8bae1dSRodney W. Grimes 	return (0);
196df8bae1dSRodney W. Grimes }
197df8bae1dSRodney W. Grimes 
198df8bae1dSRodney W. Grimes /*
199df8bae1dSRodney W. Grimes  * Free the net address hash lists that are hanging off the mount points.
200df8bae1dSRodney W. Grimes  */
201df8bae1dSRodney W. Grimes static void
202514ede09SBruce Evans vfs_free_addrlist(nep)
203514ede09SBruce Evans 	struct netexport *nep;
204df8bae1dSRodney W. Grimes {
205df8bae1dSRodney W. Grimes 	register int i;
206df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
207df8bae1dSRodney W. Grimes 
208df8bae1dSRodney W. Grimes 	for (i = 0; i <= AF_MAX; i++)
209bb56ec4aSPoul-Henning Kamp 		if ((rnh = nep->ne_rtable[i])) {
210956b0b65SJeffrey Hsu 			RADIX_NODE_HEAD_LOCK(rnh);
211210a5a71SAlfred Perlstein 			(*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
212956b0b65SJeffrey Hsu 			RADIX_NODE_HEAD_DESTROY(rnh);
213210a5a71SAlfred Perlstein 			free(rnh, M_RTABLE);
214956b0b65SJeffrey Hsu 			nep->ne_rtable[i] = NULL;	/* not SMP safe XXX */
215df8bae1dSRodney W. Grimes 		}
216df8bae1dSRodney W. Grimes }
217df8bae1dSRodney W. Grimes 
21821a90397SAlfred Perlstein /*
21921a90397SAlfred Perlstein  * High level function to manipulate export options on a mount point
22021a90397SAlfred Perlstein  * and the passed in netexport.
22121a90397SAlfred Perlstein  * Struct export_args *argp is the variable used to twiddle options,
22221a90397SAlfred Perlstein  * the structure is described in sys/mount.h
22321a90397SAlfred Perlstein  */
224df8bae1dSRodney W. Grimes int
225a13234bbSPoul-Henning Kamp vfs_export(mp, argp)
226df8bae1dSRodney W. Grimes 	struct mount *mp;
227df8bae1dSRodney W. Grimes 	struct export_args *argp;
228df8bae1dSRodney W. Grimes {
229a13234bbSPoul-Henning Kamp 	struct netexport *nep;
230df8bae1dSRodney W. Grimes 	int error;
231df8bae1dSRodney W. Grimes 
232a13234bbSPoul-Henning Kamp 	nep = mp->mnt_export;
233df8bae1dSRodney W. Grimes 	if (argp->ex_flags & MNT_DELEXPORT) {
234a13234bbSPoul-Henning Kamp 		if (nep == NULL)
2355f558fa4SIan Dowse 			return (ENOENT);
236f6b4c285SDoug Rabson 		if (mp->mnt_flag & MNT_EXPUBLIC) {
237f6b4c285SDoug Rabson 			vfs_setpublicfs(NULL, NULL, NULL);
238f6b4c285SDoug Rabson 			mp->mnt_flag &= ~MNT_EXPUBLIC;
239f6b4c285SDoug Rabson 		}
240df8bae1dSRodney W. Grimes 		vfs_free_addrlist(nep);
241a13234bbSPoul-Henning Kamp 		mp->mnt_export = NULL;
242a13234bbSPoul-Henning Kamp 		free(nep, M_MOUNT);
243df8bae1dSRodney W. Grimes 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
244df8bae1dSRodney W. Grimes 	}
245df8bae1dSRodney W. Grimes 	if (argp->ex_flags & MNT_EXPORTED) {
246a13234bbSPoul-Henning Kamp 		if (nep == NULL) {
247a163d034SWarner Losh 			nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
248a13234bbSPoul-Henning Kamp 			mp->mnt_export = nep;
249a13234bbSPoul-Henning Kamp 		}
250f6b4c285SDoug Rabson 		if (argp->ex_flags & MNT_EXPUBLIC) {
251f6b4c285SDoug Rabson 			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
252f6b4c285SDoug Rabson 				return (error);
253f6b4c285SDoug Rabson 			mp->mnt_flag |= MNT_EXPUBLIC;
254f6b4c285SDoug Rabson 		}
255bb56ec4aSPoul-Henning Kamp 		if ((error = vfs_hang_addrlist(mp, nep, argp)))
256df8bae1dSRodney W. Grimes 			return (error);
257df8bae1dSRodney W. Grimes 		mp->mnt_flag |= MNT_EXPORTED;
258df8bae1dSRodney W. Grimes 	}
259df8bae1dSRodney W. Grimes 	return (0);
260df8bae1dSRodney W. Grimes }
261df8bae1dSRodney W. Grimes 
262f6b4c285SDoug Rabson /*
263f6b4c285SDoug Rabson  * Set the publicly exported filesystem (WebNFS). Currently, only
264f6b4c285SDoug Rabson  * one public filesystem is possible in the spec (RFC 2054 and 2055)
265f6b4c285SDoug Rabson  */
266f6b4c285SDoug Rabson int
267f6b4c285SDoug Rabson vfs_setpublicfs(mp, nep, argp)
268f6b4c285SDoug Rabson 	struct mount *mp;
269f6b4c285SDoug Rabson 	struct netexport *nep;
270f6b4c285SDoug Rabson 	struct export_args *argp;
271f6b4c285SDoug Rabson {
272f6b4c285SDoug Rabson 	int error;
273f6b4c285SDoug Rabson 	struct vnode *rvp;
274f6b4c285SDoug Rabson 	char *cp;
275f6b4c285SDoug Rabson 
276f6b4c285SDoug Rabson 	/*
277f6b4c285SDoug Rabson 	 * mp == NULL -> invalidate the current info, the FS is
278f6b4c285SDoug Rabson 	 * no longer exported. May be called from either vfs_export
279f6b4c285SDoug Rabson 	 * or unmount, so check if it hasn't already been done.
280f6b4c285SDoug Rabson 	 */
281f6b4c285SDoug Rabson 	if (mp == NULL) {
282f6b4c285SDoug Rabson 		if (nfs_pub.np_valid) {
283f6b4c285SDoug Rabson 			nfs_pub.np_valid = 0;
284f6b4c285SDoug Rabson 			if (nfs_pub.np_index != NULL) {
285f6b4c285SDoug Rabson 				FREE(nfs_pub.np_index, M_TEMP);
286f6b4c285SDoug Rabson 				nfs_pub.np_index = NULL;
287f6b4c285SDoug Rabson 			}
288f6b4c285SDoug Rabson 		}
289f6b4c285SDoug Rabson 		return (0);
290f6b4c285SDoug Rabson 	}
291f6b4c285SDoug Rabson 
292f6b4c285SDoug Rabson 	/*
293f6b4c285SDoug Rabson 	 * Only one allowed at a time.
294f6b4c285SDoug Rabson 	 */
295f6b4c285SDoug Rabson 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
296f6b4c285SDoug Rabson 		return (EBUSY);
297f6b4c285SDoug Rabson 
298f6b4c285SDoug Rabson 	/*
299f6b4c285SDoug Rabson 	 * Get real filehandle for root of exported FS.
300f6b4c285SDoug Rabson 	 */
301210a5a71SAlfred Perlstein 	bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
302f6b4c285SDoug Rabson 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
303f6b4c285SDoug Rabson 
304f257b7a5SAlfred Perlstein 	if ((error = VFS_ROOT(mp, &rvp, curthread /* XXX */)))
305f6b4c285SDoug Rabson 		return (error);
306f6b4c285SDoug Rabson 
307f6b4c285SDoug Rabson 	if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
308f6b4c285SDoug Rabson 		return (error);
309f6b4c285SDoug Rabson 
310f6b4c285SDoug Rabson 	vput(rvp);
311f6b4c285SDoug Rabson 
312f6b4c285SDoug Rabson 	/*
313f6b4c285SDoug Rabson 	 * If an indexfile was specified, pull it in.
314f6b4c285SDoug Rabson 	 */
315f6b4c285SDoug Rabson 	if (argp->ex_indexfile != NULL) {
316f6b4c285SDoug Rabson 		MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
317a163d034SWarner Losh 		    M_WAITOK);
318f6b4c285SDoug Rabson 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
319f6b4c285SDoug Rabson 		    MAXNAMLEN, (size_t *)0);
320f6b4c285SDoug Rabson 		if (!error) {
321f6b4c285SDoug Rabson 			/*
322f6b4c285SDoug Rabson 			 * Check for illegal filenames.
323f6b4c285SDoug Rabson 			 */
324f6b4c285SDoug Rabson 			for (cp = nfs_pub.np_index; *cp; cp++) {
325f6b4c285SDoug Rabson 				if (*cp == '/') {
326f6b4c285SDoug Rabson 					error = EINVAL;
327f6b4c285SDoug Rabson 					break;
328f6b4c285SDoug Rabson 				}
329f6b4c285SDoug Rabson 			}
330f6b4c285SDoug Rabson 		}
331f6b4c285SDoug Rabson 		if (error) {
332f6b4c285SDoug Rabson 			FREE(nfs_pub.np_index, M_TEMP);
333f6b4c285SDoug Rabson 			return (error);
334f6b4c285SDoug Rabson 		}
335f6b4c285SDoug Rabson 	}
336f6b4c285SDoug Rabson 
337f6b4c285SDoug Rabson 	nfs_pub.np_mount = mp;
338f6b4c285SDoug Rabson 	nfs_pub.np_valid = 1;
339f6b4c285SDoug Rabson 	return (0);
340f6b4c285SDoug Rabson }
341f6b4c285SDoug Rabson 
34221a90397SAlfred Perlstein /*
34321a90397SAlfred Perlstein  * Used by the filesystems to determine if a given network address
34421a90397SAlfred Perlstein  * (passed in 'nam') is present in thier exports list, returns a pointer
34521a90397SAlfred Perlstein  * to struct netcred so that the filesystem can examine it for
34621a90397SAlfred Perlstein  * access rights (read/write/etc).
34721a90397SAlfred Perlstein  */
348ebbfc2f8SPoul-Henning Kamp static struct netcred *
349ebbfc2f8SPoul-Henning Kamp vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
350df8bae1dSRodney W. Grimes {
351a13234bbSPoul-Henning Kamp 	struct netexport *nep;
352df8bae1dSRodney W. Grimes 	register struct netcred *np;
353df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
354df8bae1dSRodney W. Grimes 	struct sockaddr *saddr;
355df8bae1dSRodney W. Grimes 
356a13234bbSPoul-Henning Kamp 	nep = mp->mnt_export;
357a13234bbSPoul-Henning Kamp 	if (nep == NULL)
358a13234bbSPoul-Henning Kamp 		return (NULL);
359df8bae1dSRodney W. Grimes 	np = NULL;
360df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_EXPORTED) {
361df8bae1dSRodney W. Grimes 		/*
362df8bae1dSRodney W. Grimes 		 * Lookup in the export list first.
363df8bae1dSRodney W. Grimes 		 */
364df8bae1dSRodney W. Grimes 		if (nam != NULL) {
36557bf258eSGarrett Wollman 			saddr = nam;
366df8bae1dSRodney W. Grimes 			rnh = nep->ne_rtable[saddr->sa_family];
367df8bae1dSRodney W. Grimes 			if (rnh != NULL) {
368956b0b65SJeffrey Hsu 				RADIX_NODE_HEAD_LOCK(rnh);
369df8bae1dSRodney W. Grimes 				np = (struct netcred *)
37097bb78acSAlfred Perlstein 				    (*rnh->rnh_matchaddr)(saddr, rnh);
371956b0b65SJeffrey Hsu 				RADIX_NODE_HEAD_UNLOCK(rnh);
372df8bae1dSRodney W. Grimes 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
373df8bae1dSRodney W. Grimes 					np = NULL;
374df8bae1dSRodney W. Grimes 			}
375df8bae1dSRodney W. Grimes 		}
376df8bae1dSRodney W. Grimes 		/*
377df8bae1dSRodney W. Grimes 		 * If no address match, use the default if it exists.
378df8bae1dSRodney W. Grimes 		 */
379df8bae1dSRodney W. Grimes 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
380df8bae1dSRodney W. Grimes 			np = &nep->ne_defexported;
381df8bae1dSRodney W. Grimes 	}
382df8bae1dSRodney W. Grimes 	return (np);
383df8bae1dSRodney W. Grimes }
38461f5d510SDavid Greenman 
38561f5d510SDavid Greenman /*
386a13234bbSPoul-Henning Kamp  * XXX: This comment comes from the deprecated ufs_check_export()
387a13234bbSPoul-Henning Kamp  * XXX: and may not entirely apply, but lacking something better:
388a13234bbSPoul-Henning Kamp  * This is the generic part of fhtovp called after the underlying
389a13234bbSPoul-Henning Kamp  * filesystem has validated the file handle.
390a13234bbSPoul-Henning Kamp  *
391a13234bbSPoul-Henning Kamp  * Verify that a host should have access to a filesystem.
392a13234bbSPoul-Henning Kamp  */
393a13234bbSPoul-Henning Kamp 
394a13234bbSPoul-Henning Kamp int
395a13234bbSPoul-Henning Kamp vfs_stdcheckexp(mp, nam, extflagsp, credanonp)
396a13234bbSPoul-Henning Kamp 	struct mount *mp;
397a13234bbSPoul-Henning Kamp 	struct sockaddr *nam;
398a13234bbSPoul-Henning Kamp 	int *extflagsp;
399a13234bbSPoul-Henning Kamp 	struct ucred **credanonp;
400a13234bbSPoul-Henning Kamp {
401a13234bbSPoul-Henning Kamp 	struct netcred *np;
402a13234bbSPoul-Henning Kamp 
403a13234bbSPoul-Henning Kamp 	np = vfs_export_lookup(mp, nam);
404a13234bbSPoul-Henning Kamp 	if (np == NULL)
405a13234bbSPoul-Henning Kamp 		return (EACCES);
406a13234bbSPoul-Henning Kamp 	*extflagsp = np->netc_exflags;
407a13234bbSPoul-Henning Kamp 	*credanonp = &np->netc_anon;
408a13234bbSPoul-Henning Kamp 	return (0);
409a13234bbSPoul-Henning Kamp }
410a13234bbSPoul-Henning Kamp 
411