xref: /freebsd/sys/kern/vfs_export.c (revision 721351876cd4d3a8a700f62d2061331fa951a488)
1 /*-
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)vfs_subr.c	8.31 (Berkeley) 5/26/95
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/dirent.h>
42 #include <sys/domain.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/refcount.h>
50 #include <sys/socket.h>
51 #include <sys/systm.h>
52 #include <sys/vnode.h>
53 
54 #include <net/radix.h>
55 
56 static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure");
57 
58 static void	vfs_free_addrlist(struct netexport *nep);
59 static int	vfs_free_netcred(struct radix_node *rn, void *w);
60 static int	vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
61 		    struct export_args *argp);
62 static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *);
63 
64 /*
65  * Network address lookup element
66  */
67 struct netcred {
68 	struct	radix_node netc_rnodes[2];
69 	int	netc_exflags;
70 	struct	ucred netc_anon;
71 };
72 
73 /*
74  * Network export information
75  */
76 struct netexport {
77 	struct	netcred ne_defexported;		      /* Default export */
78 	struct	radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
79 };
80 
81 /*
82  * Build hash lists of net addresses and hang them off the mount point.
83  * Called by ufs_mount() to set up the lists of export addresses.
84  */
85 static int
86 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
87     struct export_args *argp)
88 {
89 	register struct netcred *np;
90 	register struct radix_node_head *rnh;
91 	register int i;
92 	struct radix_node *rn;
93 	struct sockaddr *saddr, *smask = 0;
94 	struct domain *dom;
95 	int error;
96 
97 	/*
98 	 * XXX: This routine converts from a `struct xucred'
99 	 * (argp->ex_anon) to a `struct ucred' (np->netc_anon).  This
100 	 * operation is questionable; for example, what should be done
101 	 * with fields like cr_uidinfo and cr_prison?  Currently, this
102 	 * routine does not touch them (leaves them as NULL).
103 	 */
104 	if (argp->ex_anon.cr_version != XUCRED_VERSION) {
105 		vfs_mount_error(mp, "ex_anon.cr_version: %d != %d",
106 		    argp->ex_anon.cr_version, XUCRED_VERSION);
107 		return (EINVAL);
108 	}
109 
110 	if (argp->ex_addrlen == 0) {
111 		if (mp->mnt_flag & MNT_DEFEXPORTED) {
112 			vfs_mount_error(mp,
113 			    "MNT_DEFEXPORTED already set for mount %p", mp);
114 			return (EPERM);
115 		}
116 		np = &nep->ne_defexported;
117 		np->netc_exflags = argp->ex_flags;
118 		bzero(&np->netc_anon, sizeof(np->netc_anon));
119 		np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
120 		np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
121 		bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
122 		    sizeof(np->netc_anon.cr_groups));
123 		refcount_init(&np->netc_anon.cr_ref, 1);
124 		MNT_ILOCK(mp);
125 		mp->mnt_flag |= MNT_DEFEXPORTED;
126 		MNT_IUNLOCK(mp);
127 		return (0);
128 	}
129 
130 #if MSIZE <= 256
131 	if (argp->ex_addrlen > MLEN) {
132 		vfs_mount_error(mp, "ex_addrlen %d is greater than %d",
133 		    argp->ex_addrlen, MLEN);
134 		return (EINVAL);
135 	}
136 #endif
137 
138 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
139 	np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
140 	saddr = (struct sockaddr *) (np + 1);
141 	if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
142 		goto out;
143 	if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) {
144 		error = EINVAL;
145 		vfs_mount_error(mp, "Invalid saddr->sa_family: %d");
146 		goto out;
147 	}
148 	if (saddr->sa_len > argp->ex_addrlen)
149 		saddr->sa_len = argp->ex_addrlen;
150 	if (argp->ex_masklen) {
151 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
152 		error = copyin(argp->ex_mask, smask, argp->ex_masklen);
153 		if (error)
154 			goto out;
155 		if (smask->sa_len > argp->ex_masklen)
156 			smask->sa_len = argp->ex_masklen;
157 	}
158 	i = saddr->sa_family;
159 	if ((rnh = nep->ne_rtable[i]) == NULL) {
160 		/*
161 		 * Seems silly to initialize every AF when most are not used,
162 		 * do so on demand here
163 		 */
164 		for (dom = domains; dom; dom = dom->dom_next) {
165 			KASSERT(((i == AF_INET) || (i == AF_INET6)),
166 			    ("unexpected protocol in vfs_hang_addrlist"));
167 			if (dom->dom_family == i && dom->dom_rtattach) {
168 				/*
169 				 * XXX MRT
170 				 * The INET and INET6 domains know the
171 				 * offset already. We don't need to send it
172 				 * So we just use it as a flag to say that
173 				 * we are or are not setting up a real routing
174 				 * table. Only IP and IPV6 need have this
175 				 * be 0 so all other protocols can stay the
176 				 * same (ABI compatible).
177 				 */
178 				dom->dom_rtattach(
179 				    (void **) &nep->ne_rtable[i], 0);
180 				break;
181 			}
182 		}
183 		if ((rnh = nep->ne_rtable[i]) == NULL) {
184 			error = ENOBUFS;
185 			vfs_mount_error(mp, "%s %s %d",
186 			    "Unable to initialize radix node head ",
187 			    "for address family", i);
188 			goto out;
189 		}
190 	}
191 	RADIX_NODE_HEAD_LOCK(rnh);
192 	rn = (*rnh->rnh_addaddr)(saddr, smask, rnh, np->netc_rnodes);
193 	RADIX_NODE_HEAD_UNLOCK(rnh);
194 	if (rn == NULL || np != (struct netcred *)rn) {	/* already exists */
195 		error = EPERM;
196 		vfs_mount_error(mp, "Invalid radix node head, rn: %p %p",
197 		    rn, np);
198 		goto out;
199 	}
200 	np->netc_exflags = argp->ex_flags;
201 	bzero(&np->netc_anon, sizeof(np->netc_anon));
202 	np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
203 	np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
204 	bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
205 	    sizeof(np->netc_anon.cr_groups));
206 	refcount_init(&np->netc_anon.cr_ref, 1);
207 	return (0);
208 out:
209 	free(np, M_NETADDR);
210 	return (error);
211 }
212 
213 /* Helper for vfs_free_addrlist. */
214 /* ARGSUSED */
215 static int
216 vfs_free_netcred(struct radix_node *rn, void *w)
217 {
218 	register struct radix_node_head *rnh = (struct radix_node_head *) w;
219 
220 	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
221 	free(rn, M_NETADDR);
222 	return (0);
223 }
224 
225 /*
226  * Free the net address hash lists that are hanging off the mount points.
227  */
228 static void
229 vfs_free_addrlist(struct netexport *nep)
230 {
231 	register int i;
232 	register struct radix_node_head *rnh;
233 
234 	for (i = 0; i <= AF_MAX; i++)
235 		if ((rnh = nep->ne_rtable[i])) {
236 			RADIX_NODE_HEAD_LOCK(rnh);
237 			(*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
238 			RADIX_NODE_HEAD_DESTROY(rnh);
239 			free(rnh, M_RTABLE);
240 			nep->ne_rtable[i] = NULL;	/* not SMP safe XXX */
241 		}
242 }
243 
244 /*
245  * High level function to manipulate export options on a mount point
246  * and the passed in netexport.
247  * Struct export_args *argp is the variable used to twiddle options,
248  * the structure is described in sys/mount.h
249  */
250 int
251 vfs_export(struct mount *mp, struct export_args *argp)
252 {
253 	struct netexport *nep;
254 	int error;
255 
256 	nep = mp->mnt_export;
257 	error = 0;
258 	lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL);
259 	if (argp->ex_flags & MNT_DELEXPORT) {
260 		if (nep == NULL) {
261 			error = ENOENT;
262 			goto out;
263 		}
264 		if (mp->mnt_flag & MNT_EXPUBLIC) {
265 			vfs_setpublicfs(NULL, NULL, NULL);
266 			MNT_ILOCK(mp);
267 			mp->mnt_flag &= ~MNT_EXPUBLIC;
268 			MNT_IUNLOCK(mp);
269 		}
270 		vfs_free_addrlist(nep);
271 		mp->mnt_export = NULL;
272 		free(nep, M_MOUNT);
273 		nep = NULL;
274 		MNT_ILOCK(mp);
275 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
276 		MNT_IUNLOCK(mp);
277 	}
278 	if (argp->ex_flags & MNT_EXPORTED) {
279 		if (nep == NULL) {
280 			nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
281 			mp->mnt_export = nep;
282 		}
283 		if (argp->ex_flags & MNT_EXPUBLIC) {
284 			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
285 				goto out;
286 			MNT_ILOCK(mp);
287 			mp->mnt_flag |= MNT_EXPUBLIC;
288 			MNT_IUNLOCK(mp);
289 		}
290 		if ((error = vfs_hang_addrlist(mp, nep, argp)))
291 			goto out;
292 		MNT_ILOCK(mp);
293 		mp->mnt_flag |= MNT_EXPORTED;
294 		MNT_IUNLOCK(mp);
295 	}
296 
297 out:
298 	lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
299 	/*
300 	 * Once we have executed the vfs_export() command, we do
301 	 * not want to keep the "export" option around in the
302 	 * options list, since that will cause subsequent MNT_UPDATE
303 	 * calls to fail.  The export information is saved in
304 	 * mp->mnt_export, so we can safely delete the "export" mount option
305 	 * here.
306 	 */
307 	vfs_deleteopt(mp->mnt_optnew, "export");
308 	vfs_deleteopt(mp->mnt_opt, "export");
309 	return (error);
310 }
311 
312 /*
313  * Set the publicly exported filesystem (WebNFS). Currently, only
314  * one public filesystem is possible in the spec (RFC 2054 and 2055)
315  */
316 int
317 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
318     struct export_args *argp)
319 {
320 	int error;
321 	struct vnode *rvp;
322 	char *cp;
323 
324 	/*
325 	 * mp == NULL -> invalidate the current info, the FS is
326 	 * no longer exported. May be called from either vfs_export
327 	 * or unmount, so check if it hasn't already been done.
328 	 */
329 	if (mp == NULL) {
330 		if (nfs_pub.np_valid) {
331 			nfs_pub.np_valid = 0;
332 			if (nfs_pub.np_index != NULL) {
333 				FREE(nfs_pub.np_index, M_TEMP);
334 				nfs_pub.np_index = NULL;
335 			}
336 		}
337 		return (0);
338 	}
339 
340 	/*
341 	 * Only one allowed at a time.
342 	 */
343 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
344 		return (EBUSY);
345 
346 	/*
347 	 * Get real filehandle for root of exported FS.
348 	 */
349 	bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
350 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
351 
352 	if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp, curthread /* XXX */)))
353 		return (error);
354 
355 	if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
356 		return (error);
357 
358 	vput(rvp);
359 
360 	/*
361 	 * If an indexfile was specified, pull it in.
362 	 */
363 	if (argp->ex_indexfile != NULL) {
364 		MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
365 		    M_WAITOK);
366 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
367 		    MAXNAMLEN, (size_t *)0);
368 		if (!error) {
369 			/*
370 			 * Check for illegal filenames.
371 			 */
372 			for (cp = nfs_pub.np_index; *cp; cp++) {
373 				if (*cp == '/') {
374 					error = EINVAL;
375 					break;
376 				}
377 			}
378 		}
379 		if (error) {
380 			FREE(nfs_pub.np_index, M_TEMP);
381 			return (error);
382 		}
383 	}
384 
385 	nfs_pub.np_mount = mp;
386 	nfs_pub.np_valid = 1;
387 	return (0);
388 }
389 
390 /*
391  * Used by the filesystems to determine if a given network address
392  * (passed in 'nam') is present in thier exports list, returns a pointer
393  * to struct netcred so that the filesystem can examine it for
394  * access rights (read/write/etc).
395  */
396 static struct netcred *
397 vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
398 {
399 	struct netexport *nep;
400 	register struct netcred *np;
401 	register struct radix_node_head *rnh;
402 	struct sockaddr *saddr;
403 
404 	nep = mp->mnt_export;
405 	if (nep == NULL)
406 		return (NULL);
407 	np = NULL;
408 	if (mp->mnt_flag & MNT_EXPORTED) {
409 		/*
410 		 * Lookup in the export list first.
411 		 */
412 		if (nam != NULL) {
413 			saddr = nam;
414 			rnh = nep->ne_rtable[saddr->sa_family];
415 			if (rnh != NULL) {
416 				RADIX_NODE_HEAD_LOCK(rnh);
417 				np = (struct netcred *)
418 				    (*rnh->rnh_matchaddr)(saddr, rnh);
419 				RADIX_NODE_HEAD_UNLOCK(rnh);
420 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
421 					np = NULL;
422 			}
423 		}
424 		/*
425 		 * If no address match, use the default if it exists.
426 		 */
427 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
428 			np = &nep->ne_defexported;
429 	}
430 	return (np);
431 }
432 
433 /*
434  * XXX: This comment comes from the deprecated ufs_check_export()
435  * XXX: and may not entirely apply, but lacking something better:
436  * This is the generic part of fhtovp called after the underlying
437  * filesystem has validated the file handle.
438  *
439  * Verify that a host should have access to a filesystem.
440  */
441 
442 int
443 vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
444     struct ucred **credanonp)
445 {
446 	struct netcred *np;
447 
448 	lockmgr(&mp->mnt_explock, LK_SHARED, NULL);
449 	np = vfs_export_lookup(mp, nam);
450 	lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
451 	if (np == NULL)
452 		return (EACCES);
453 	*extflagsp = np->netc_exflags;
454 	*credanonp = &np->netc_anon;
455 	return (0);
456 }
457 
458