xref: /freebsd/sys/kern/vfs_export.c (revision 7d0d268b8a67f28ccefdd0b8ce6fb38acac78d80)
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/rwlock.h>
50 #include <sys/refcount.h>
51 #include <sys/socket.h>
52 #include <sys/systm.h>
53 #include <sys/vnode.h>
54 
55 #include <net/radix.h>
56 
57 static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure");
58 
59 static void	vfs_free_addrlist(struct netexport *nep);
60 static int	vfs_free_netcred(struct radix_node *rn, void *w);
61 static int	vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
62 		    struct export_args *argp);
63 static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *);
64 
65 /*
66  * Network address lookup element
67  */
68 struct netcred {
69 	struct	radix_node netc_rnodes[2];
70 	int	netc_exflags;
71 	struct	ucred netc_anon;
72 	int	netc_numsecflavors;
73 	int	netc_secflavors[MAXSECFLAVORS];
74 };
75 
76 /*
77  * Network export information
78  */
79 struct netexport {
80 	struct	netcred ne_defexported;		      /* Default export */
81 	struct	radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
82 };
83 
84 /*
85  * Build hash lists of net addresses and hang them off the mount point.
86  * Called by ufs_mount() to set up the lists of export addresses.
87  */
88 static int
89 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
90     struct export_args *argp)
91 {
92 	register struct netcred *np;
93 	register struct radix_node_head *rnh;
94 	register int i;
95 	struct radix_node *rn;
96 	struct sockaddr *saddr, *smask = 0;
97 	struct domain *dom;
98 	int error;
99 
100 	/*
101 	 * XXX: This routine converts from a `struct xucred'
102 	 * (argp->ex_anon) to a `struct ucred' (np->netc_anon).  This
103 	 * operation is questionable; for example, what should be done
104 	 * with fields like cr_uidinfo and cr_prison?  Currently, this
105 	 * routine does not touch them (leaves them as NULL).
106 	 */
107 	if (argp->ex_anon.cr_version != XUCRED_VERSION) {
108 		vfs_mount_error(mp, "ex_anon.cr_version: %d != %d",
109 		    argp->ex_anon.cr_version, XUCRED_VERSION);
110 		return (EINVAL);
111 	}
112 
113 	if (argp->ex_addrlen == 0) {
114 		if (mp->mnt_flag & MNT_DEFEXPORTED) {
115 			vfs_mount_error(mp,
116 			    "MNT_DEFEXPORTED already set for mount %p", mp);
117 			return (EPERM);
118 		}
119 		np = &nep->ne_defexported;
120 		np->netc_exflags = argp->ex_flags;
121 		bzero(&np->netc_anon, sizeof(np->netc_anon));
122 		np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
123 		np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
124 		bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
125 		    sizeof(np->netc_anon.cr_groups));
126 		np->netc_numsecflavors = argp->ex_numsecflavors;
127 		bcopy(argp->ex_secflavors, np->netc_secflavors,
128 		    sizeof(np->netc_secflavors));
129 		refcount_init(&np->netc_anon.cr_ref, 1);
130 		MNT_ILOCK(mp);
131 		mp->mnt_flag |= MNT_DEFEXPORTED;
132 		MNT_IUNLOCK(mp);
133 		return (0);
134 	}
135 
136 #if MSIZE <= 256
137 	if (argp->ex_addrlen > MLEN) {
138 		vfs_mount_error(mp, "ex_addrlen %d is greater than %d",
139 		    argp->ex_addrlen, MLEN);
140 		return (EINVAL);
141 	}
142 #endif
143 
144 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
145 	np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
146 	saddr = (struct sockaddr *) (np + 1);
147 	if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
148 		goto out;
149 	if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) {
150 		error = EINVAL;
151 		vfs_mount_error(mp, "Invalid saddr->sa_family: %d");
152 		goto out;
153 	}
154 	if (saddr->sa_len > argp->ex_addrlen)
155 		saddr->sa_len = argp->ex_addrlen;
156 	if (argp->ex_masklen) {
157 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
158 		error = copyin(argp->ex_mask, smask, argp->ex_masklen);
159 		if (error)
160 			goto out;
161 		if (smask->sa_len > argp->ex_masklen)
162 			smask->sa_len = argp->ex_masklen;
163 	}
164 	i = saddr->sa_family;
165 	if ((rnh = nep->ne_rtable[i]) == NULL) {
166 		/*
167 		 * Seems silly to initialize every AF when most are not used,
168 		 * do so on demand here
169 		 */
170 		for (dom = domains; dom; dom = dom->dom_next) {
171 			KASSERT(((i == AF_INET) || (i == AF_INET6)),
172 			    ("unexpected protocol in vfs_hang_addrlist"));
173 			if (dom->dom_family == i && dom->dom_rtattach) {
174 				/*
175 				 * XXX MRT
176 				 * The INET and INET6 domains know the
177 				 * offset already. We don't need to send it
178 				 * So we just use it as a flag to say that
179 				 * we are or are not setting up a real routing
180 				 * table. Only IP and IPV6 need have this
181 				 * be 0 so all other protocols can stay the
182 				 * same (ABI compatible).
183 				 */
184 				dom->dom_rtattach(
185 				    (void **) &nep->ne_rtable[i], 0);
186 				break;
187 			}
188 		}
189 		if ((rnh = nep->ne_rtable[i]) == NULL) {
190 			error = ENOBUFS;
191 			vfs_mount_error(mp, "%s %s %d",
192 			    "Unable to initialize radix node head ",
193 			    "for address family", i);
194 			goto out;
195 		}
196 	}
197 	RADIX_NODE_HEAD_LOCK(rnh);
198 	rn = (*rnh->rnh_addaddr)(saddr, smask, rnh, np->netc_rnodes);
199 	RADIX_NODE_HEAD_UNLOCK(rnh);
200 	if (rn == NULL || np != (struct netcred *)rn) {	/* already exists */
201 		error = EPERM;
202 		vfs_mount_error(mp, "Invalid radix node head, rn: %p %p",
203 		    rn, np);
204 		goto out;
205 	}
206 	np->netc_exflags = argp->ex_flags;
207 	bzero(&np->netc_anon, sizeof(np->netc_anon));
208 	np->netc_anon.cr_uid = argp->ex_anon.cr_uid;
209 	np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups;
210 	bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups,
211 	    sizeof(np->netc_anon.cr_groups));
212 	np->netc_numsecflavors = argp->ex_numsecflavors;
213 	bcopy(argp->ex_secflavors, np->netc_secflavors,
214 	    sizeof(np->netc_secflavors));
215 	refcount_init(&np->netc_anon.cr_ref, 1);
216 	return (0);
217 out:
218 	free(np, M_NETADDR);
219 	return (error);
220 }
221 
222 /* Helper for vfs_free_addrlist. */
223 /* ARGSUSED */
224 static int
225 vfs_free_netcred(struct radix_node *rn, void *w)
226 {
227 	register struct radix_node_head *rnh = (struct radix_node_head *) w;
228 
229 	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
230 	free(rn, M_NETADDR);
231 	return (0);
232 }
233 
234 /*
235  * Free the net address hash lists that are hanging off the mount points.
236  */
237 static void
238 vfs_free_addrlist(struct netexport *nep)
239 {
240 	register int i;
241 	register struct radix_node_head *rnh;
242 
243 	for (i = 0; i <= AF_MAX; i++)
244 		if ((rnh = nep->ne_rtable[i])) {
245 			RADIX_NODE_HEAD_LOCK(rnh);
246 			(*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
247 			RADIX_NODE_HEAD_UNLOCK(rnh);
248 			RADIX_NODE_HEAD_DESTROY(rnh);
249 			free(rnh, M_RTABLE);
250 			nep->ne_rtable[i] = NULL;	/* not SMP safe XXX */
251 		}
252 }
253 
254 /*
255  * High level function to manipulate export options on a mount point
256  * and the passed in netexport.
257  * Struct export_args *argp is the variable used to twiddle options,
258  * the structure is described in sys/mount.h
259  */
260 int
261 vfs_export(struct mount *mp, struct export_args *argp)
262 {
263 	struct netexport *nep;
264 	int error;
265 
266 	if (argp->ex_numsecflavors < 0
267 	    || argp->ex_numsecflavors >= MAXSECFLAVORS)
268 		return (EINVAL);
269 
270 	nep = mp->mnt_export;
271 	error = 0;
272 	lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL);
273 	if (argp->ex_flags & MNT_DELEXPORT) {
274 		if (nep == NULL) {
275 			error = ENOENT;
276 			goto out;
277 		}
278 		if (mp->mnt_flag & MNT_EXPUBLIC) {
279 			vfs_setpublicfs(NULL, NULL, NULL);
280 			MNT_ILOCK(mp);
281 			mp->mnt_flag &= ~MNT_EXPUBLIC;
282 			MNT_IUNLOCK(mp);
283 		}
284 		vfs_free_addrlist(nep);
285 		mp->mnt_export = NULL;
286 		free(nep, M_MOUNT);
287 		nep = NULL;
288 		MNT_ILOCK(mp);
289 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
290 		MNT_IUNLOCK(mp);
291 	}
292 	if (argp->ex_flags & MNT_EXPORTED) {
293 		if (nep == NULL) {
294 			nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
295 			mp->mnt_export = nep;
296 		}
297 		if (argp->ex_flags & MNT_EXPUBLIC) {
298 			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
299 				goto out;
300 			MNT_ILOCK(mp);
301 			mp->mnt_flag |= MNT_EXPUBLIC;
302 			MNT_IUNLOCK(mp);
303 		}
304 		if ((error = vfs_hang_addrlist(mp, nep, argp)))
305 			goto out;
306 		MNT_ILOCK(mp);
307 		mp->mnt_flag |= MNT_EXPORTED;
308 		MNT_IUNLOCK(mp);
309 	}
310 
311 out:
312 	lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
313 	/*
314 	 * Once we have executed the vfs_export() command, we do
315 	 * not want to keep the "export" option around in the
316 	 * options list, since that will cause subsequent MNT_UPDATE
317 	 * calls to fail.  The export information is saved in
318 	 * mp->mnt_export, so we can safely delete the "export" mount option
319 	 * here.
320 	 */
321 	vfs_deleteopt(mp->mnt_optnew, "export");
322 	vfs_deleteopt(mp->mnt_opt, "export");
323 	return (error);
324 }
325 
326 /*
327  * Set the publicly exported filesystem (WebNFS). Currently, only
328  * one public filesystem is possible in the spec (RFC 2054 and 2055)
329  */
330 int
331 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
332     struct export_args *argp)
333 {
334 	int error;
335 	struct vnode *rvp;
336 	char *cp;
337 
338 	/*
339 	 * mp == NULL -> invalidate the current info, the FS is
340 	 * no longer exported. May be called from either vfs_export
341 	 * or unmount, so check if it hasn't already been done.
342 	 */
343 	if (mp == NULL) {
344 		if (nfs_pub.np_valid) {
345 			nfs_pub.np_valid = 0;
346 			if (nfs_pub.np_index != NULL) {
347 				free(nfs_pub.np_index, M_TEMP);
348 				nfs_pub.np_index = NULL;
349 			}
350 		}
351 		return (0);
352 	}
353 
354 	/*
355 	 * Only one allowed at a time.
356 	 */
357 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
358 		return (EBUSY);
359 
360 	/*
361 	 * Get real filehandle for root of exported FS.
362 	 */
363 	bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
364 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
365 
366 	if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp, curthread /* XXX */)))
367 		return (error);
368 
369 	if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
370 		return (error);
371 
372 	vput(rvp);
373 
374 	/*
375 	 * If an indexfile was specified, pull it in.
376 	 */
377 	if (argp->ex_indexfile != NULL) {
378 		nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP,
379 		    M_WAITOK);
380 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
381 		    MAXNAMLEN, (size_t *)0);
382 		if (!error) {
383 			/*
384 			 * Check for illegal filenames.
385 			 */
386 			for (cp = nfs_pub.np_index; *cp; cp++) {
387 				if (*cp == '/') {
388 					error = EINVAL;
389 					break;
390 				}
391 			}
392 		}
393 		if (error) {
394 			free(nfs_pub.np_index, M_TEMP);
395 			return (error);
396 		}
397 	}
398 
399 	nfs_pub.np_mount = mp;
400 	nfs_pub.np_valid = 1;
401 	return (0);
402 }
403 
404 /*
405  * Used by the filesystems to determine if a given network address
406  * (passed in 'nam') is present in their exports list, returns a pointer
407  * to struct netcred so that the filesystem can examine it for
408  * access rights (read/write/etc).
409  */
410 static struct netcred *
411 vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
412 {
413 	struct netexport *nep;
414 	register struct netcred *np;
415 	register struct radix_node_head *rnh;
416 	struct sockaddr *saddr;
417 
418 	nep = mp->mnt_export;
419 	if (nep == NULL)
420 		return (NULL);
421 	np = NULL;
422 	if (mp->mnt_flag & MNT_EXPORTED) {
423 		/*
424 		 * Lookup in the export list first.
425 		 */
426 		if (nam != NULL) {
427 			saddr = nam;
428 			rnh = nep->ne_rtable[saddr->sa_family];
429 			if (rnh != NULL) {
430 				RADIX_NODE_HEAD_RLOCK(rnh);
431 				np = (struct netcred *)
432 				    (*rnh->rnh_matchaddr)(saddr, rnh);
433 				RADIX_NODE_HEAD_RUNLOCK(rnh);
434 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
435 					np = NULL;
436 			}
437 		}
438 		/*
439 		 * If no address match, use the default if it exists.
440 		 */
441 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
442 			np = &nep->ne_defexported;
443 	}
444 	return (np);
445 }
446 
447 /*
448  * XXX: This comment comes from the deprecated ufs_check_export()
449  * XXX: and may not entirely apply, but lacking something better:
450  * This is the generic part of fhtovp called after the underlying
451  * filesystem has validated the file handle.
452  *
453  * Verify that a host should have access to a filesystem.
454  */
455 
456 int
457 vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
458     struct ucred **credanonp, int *numsecflavors, int **secflavors)
459 {
460 	struct netcred *np;
461 
462 	lockmgr(&mp->mnt_explock, LK_SHARED, NULL);
463 	np = vfs_export_lookup(mp, nam);
464 	lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
465 	if (np == NULL)
466 		return (EACCES);
467 	*extflagsp = np->netc_exflags;
468 	*credanonp = &np->netc_anon;
469 	if (numsecflavors)
470 		*numsecflavors = np->netc_numsecflavors;
471 	if (secflavors)
472 		*secflavors = np->netc_secflavors;
473 	return (0);
474 }
475 
476