xref: /freebsd/sys/kern/vfs_export.c (revision 6f1af0d7d2af54b339b5212434cd6d4fda628d80)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)vfs_subr.c	8.31 (Berkeley) 5/26/95
37  */
38 
39 #include <sys/cdefs.h>
40 #include "opt_inet.h"
41 #include "opt_inet6.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/dirent.h>
46 #include <sys/jail.h>
47 #include <sys/kernel.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
53 #include <sys/proc.h>
54 #include <sys/rmlock.h>
55 #include <sys/refcount.h>
56 #include <sys/signalvar.h>
57 #include <sys/socket.h>
58 #include <sys/vnode.h>
59 
60 #include <netinet/in.h>
61 #include <net/radix.h>
62 
63 #include <rpc/types.h>
64 #include <rpc/auth.h>
65 
66 static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure");
67 
68 #if defined(INET) || defined(INET6)
69 static struct radix_node_head *vfs_create_addrlist_af(
70 		    struct radix_node_head **prnh, int off);
71 #endif
72 static int	vfs_free_netcred(struct radix_node *rn, void *w);
73 static void	vfs_free_addrlist_af(struct radix_node_head **prnh);
74 static int	vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
75 		    struct export_args *argp);
76 static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *);
77 
78 /*
79  * Network address lookup element
80  */
81 struct netcred {
82 	struct	radix_node netc_rnodes[2];
83 	uint64_t netc_exflags;
84 	struct	ucred *netc_anon;
85 	int	netc_numsecflavors;
86 	int	netc_secflavors[MAXSECFLAVORS];
87 };
88 
89 /*
90  * Network export information
91  */
92 struct netexport {
93 	struct	netcred ne_defexported;		      /* Default export */
94 	struct 	radix_node_head	*ne4;
95 	struct 	radix_node_head	*ne6;
96 };
97 
98 /*
99  * Build hash lists of net addresses and hang them off the mount point.
100  * Called by vfs_export() to set up the lists of export addresses.
101  */
102 static int
103 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
104     struct export_args *argp)
105 {
106 	struct netcred *np;
107 	struct radix_node_head *rnh;
108 	int i;
109 	struct radix_node *rn;
110 	struct sockaddr *saddr, *smask = NULL;
111 #if defined(INET6) || defined(INET)
112 	int off;
113 #endif
114 	int error;
115 
116 	KASSERT(argp->ex_numsecflavors > 0,
117 	    ("%s: numsecflavors <= 0", __func__));
118 	KASSERT(argp->ex_numsecflavors < MAXSECFLAVORS,
119 	    ("%s: numsecflavors >= MAXSECFLAVORS", __func__));
120 
121 	/*
122 	 * XXX: This routine converts from a uid plus gid list
123 	 * to a `struct ucred' (np->netc_anon).  This
124 	 * operation is questionable; for example, what should be done
125 	 * with fields like cr_uidinfo and cr_prison?  Currently, this
126 	 * routine does not touch them (leaves them as NULL).
127 	 */
128 	if (argp->ex_addrlen == 0) {
129 		if (mp->mnt_flag & MNT_DEFEXPORTED) {
130 			vfs_mount_error(mp,
131 			    "MNT_DEFEXPORTED already set for mount %p", mp);
132 			return (EPERM);
133 		}
134 		np = &nep->ne_defexported;
135 		np->netc_exflags = argp->ex_flags;
136 		np->netc_anon = crget();
137 		np->netc_anon->cr_uid = argp->ex_uid;
138 		crsetgroups(np->netc_anon, argp->ex_ngroups,
139 		    argp->ex_groups);
140 		np->netc_anon->cr_prison = &prison0;
141 		prison_hold(np->netc_anon->cr_prison);
142 		np->netc_numsecflavors = argp->ex_numsecflavors;
143 		bcopy(argp->ex_secflavors, np->netc_secflavors,
144 		    sizeof(np->netc_secflavors));
145 		MNT_ILOCK(mp);
146 		mp->mnt_flag |= MNT_DEFEXPORTED;
147 		MNT_IUNLOCK(mp);
148 		return (0);
149 	}
150 
151 #if MSIZE <= 256
152 	if (argp->ex_addrlen > MLEN) {
153 		vfs_mount_error(mp, "ex_addrlen %d is greater than %d",
154 		    argp->ex_addrlen, MLEN);
155 		return (EINVAL);
156 	}
157 #endif
158 
159 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
160 	np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
161 	saddr = (struct sockaddr *) (np + 1);
162 	if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
163 		goto out;
164 	if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) {
165 		error = EINVAL;
166 		vfs_mount_error(mp, "Invalid saddr->sa_family: %d");
167 		goto out;
168 	}
169 	if (saddr->sa_len > argp->ex_addrlen)
170 		saddr->sa_len = argp->ex_addrlen;
171 	if (argp->ex_masklen) {
172 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
173 		error = copyin(argp->ex_mask, smask, argp->ex_masklen);
174 		if (error)
175 			goto out;
176 		if (smask->sa_len > argp->ex_masklen)
177 			smask->sa_len = argp->ex_masklen;
178 	}
179 	rnh = NULL;
180 	switch (saddr->sa_family) {
181 #ifdef INET
182 	case AF_INET:
183 		if ((rnh = nep->ne4) == NULL) {
184 			off = offsetof(struct sockaddr_in, sin_addr) << 3;
185 			rnh = vfs_create_addrlist_af(&nep->ne4, off);
186 		}
187 		break;
188 #endif
189 #ifdef INET6
190 	case AF_INET6:
191 		if ((rnh = nep->ne6) == NULL) {
192 			off = offsetof(struct sockaddr_in6, sin6_addr) << 3;
193 			rnh = vfs_create_addrlist_af(&nep->ne6, off);
194 		}
195 		break;
196 #endif
197 	}
198 	if (rnh == NULL) {
199 		error = ENOBUFS;
200 		vfs_mount_error(mp, "%s %s %d",
201 		    "Unable to initialize radix node head ",
202 		    "for address family", saddr->sa_family);
203 		goto out;
204 	}
205 	RADIX_NODE_HEAD_LOCK(rnh);
206 	rn = (*rnh->rnh_addaddr)(saddr, smask, &rnh->rh, np->netc_rnodes);
207 	RADIX_NODE_HEAD_UNLOCK(rnh);
208 	if (rn == NULL || np != (struct netcred *)rn) {	/* already exists */
209 		error = EPERM;
210 		vfs_mount_error(mp,
211 		    "netcred already exists for given addr/mask");
212 		goto out;
213 	}
214 	np->netc_exflags = argp->ex_flags;
215 	np->netc_anon = crget();
216 	np->netc_anon->cr_uid = argp->ex_uid;
217 	crsetgroups(np->netc_anon, argp->ex_ngroups,
218 	    argp->ex_groups);
219 	np->netc_anon->cr_prison = &prison0;
220 	prison_hold(np->netc_anon->cr_prison);
221 	np->netc_numsecflavors = argp->ex_numsecflavors;
222 	bcopy(argp->ex_secflavors, np->netc_secflavors,
223 	    sizeof(np->netc_secflavors));
224 	return (0);
225 out:
226 	free(np, M_NETADDR);
227 	return (error);
228 }
229 
230 /* Helper for vfs_free_addrlist. */
231 /* ARGSUSED */
232 static int
233 vfs_free_netcred(struct radix_node *rn, void *w)
234 {
235 	struct radix_node_head *rnh = (struct radix_node_head *) w;
236 	struct ucred *cred;
237 
238 	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, &rnh->rh);
239 	cred = ((struct netcred *)rn)->netc_anon;
240 	if (cred != NULL)
241 		crfree(cred);
242 	free(rn, M_NETADDR);
243 	return (0);
244 }
245 
246 #if defined(INET) || defined(INET6)
247 static struct radix_node_head *
248 vfs_create_addrlist_af(struct radix_node_head **prnh, int off)
249 {
250 
251 	if (rn_inithead((void **)prnh, off) == 0)
252 		return (NULL);
253 	RADIX_NODE_HEAD_LOCK_INIT(*prnh);
254 	return (*prnh);
255 }
256 #endif
257 
258 static void
259 vfs_free_addrlist_af(struct radix_node_head **prnh)
260 {
261 	struct radix_node_head *rnh;
262 
263 	rnh = *prnh;
264 	RADIX_NODE_HEAD_LOCK(rnh);
265 	(*rnh->rnh_walktree)(&rnh->rh, vfs_free_netcred, rnh);
266 	RADIX_NODE_HEAD_UNLOCK(rnh);
267 	RADIX_NODE_HEAD_DESTROY(rnh);
268 	rn_detachhead((void **)prnh);
269 	prnh = NULL;
270 }
271 
272 /*
273  * Free the net address hash lists that are hanging off the mount points.
274  */
275 void
276 vfs_free_addrlist(struct netexport *nep)
277 {
278 	struct ucred *cred;
279 
280 	if (nep->ne4 != NULL)
281 		vfs_free_addrlist_af(&nep->ne4);
282 	if (nep->ne6 != NULL)
283 		vfs_free_addrlist_af(&nep->ne6);
284 
285 	cred = nep->ne_defexported.netc_anon;
286 	if (cred != NULL) {
287 		crfree(cred);
288 		nep->ne_defexported.netc_anon = NULL;
289 	}
290 
291 }
292 
293 /*
294  * High level function to manipulate export options on a mount point
295  * and the passed in netexport.
296  * Struct export_args *argp is the variable used to twiddle options,
297  * the structure is described in sys/mount.h
298  * The do_exjail argument should be true if *mp is in the mountlist
299  * and false if not.  It is not in the mountlist for the NFSv4 rootfs
300  * fake mount point just used for exports.
301  */
302 int
303 vfs_export(struct mount *mp, struct export_args *argp, bool do_exjail)
304 {
305 	struct netexport *nep;
306 	struct ucred *cr;
307 	struct prison *pr;
308 	int error;
309 	bool new_nep;
310 
311 	if ((argp->ex_flags & (MNT_DELEXPORT | MNT_EXPORTED)) == 0)
312 		return (EINVAL);
313 
314 	if ((argp->ex_flags & MNT_EXPORTED) != 0 &&
315 	    (argp->ex_numsecflavors < 0
316 	    || argp->ex_numsecflavors >= MAXSECFLAVORS))
317 		return (EINVAL);
318 
319 	error = 0;
320 	pr = curthread->td_ucred->cr_prison;
321 	lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL);
322 	nep = mp->mnt_export;
323 	if (argp->ex_flags & MNT_DELEXPORT) {
324 		if (nep == NULL) {
325 			error = ENOENT;
326 			goto out;
327 		}
328 		MNT_ILOCK(mp);
329 		if (mp->mnt_exjail != NULL && mp->mnt_exjail->cr_prison != pr &&
330 		    pr == &prison0) {
331 			MNT_IUNLOCK(mp);
332 			/* EXDEV will not get logged by mountd(8). */
333 			error = EXDEV;
334 			goto out;
335 		} else if (mp->mnt_exjail != NULL &&
336 		    mp->mnt_exjail->cr_prison != pr) {
337 			MNT_IUNLOCK(mp);
338 			/* EPERM will get logged by mountd(8). */
339 			error = EPERM;
340 			goto out;
341 		}
342 		MNT_IUNLOCK(mp);
343 		if (mp->mnt_flag & MNT_EXPUBLIC) {
344 			vfs_setpublicfs(NULL, NULL, NULL);
345 			MNT_ILOCK(mp);
346 			mp->mnt_flag &= ~MNT_EXPUBLIC;
347 			MNT_IUNLOCK(mp);
348 		}
349 		vfs_free_addrlist(nep);
350 		mp->mnt_export = NULL;
351 		free(nep, M_MOUNT);
352 		nep = NULL;
353 		MNT_ILOCK(mp);
354 		cr = mp->mnt_exjail;
355 		mp->mnt_exjail = NULL;
356 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
357 		MNT_IUNLOCK(mp);
358 		if (cr != NULL) {
359 			atomic_subtract_int(&pr->pr_exportcnt, 1);
360 			crfree(cr);
361 		}
362 	}
363 	if (argp->ex_flags & MNT_EXPORTED) {
364 		new_nep = false;
365 		MNT_ILOCK(mp);
366 		if (mp->mnt_exjail == NULL) {
367 			MNT_IUNLOCK(mp);
368 			if (do_exjail && nep != NULL) {
369 				vfs_free_addrlist(nep);
370 				memset(nep, 0, sizeof(*nep));
371 				new_nep = true;
372 			}
373 		} else if (mp->mnt_exjail->cr_prison != pr) {
374 			MNT_IUNLOCK(mp);
375 			error = EPERM;
376 			goto out;
377 		} else
378 			MNT_IUNLOCK(mp);
379 		if (nep == NULL) {
380 			nep = malloc(sizeof(struct netexport), M_MOUNT,
381 			    M_WAITOK | M_ZERO);
382 			mp->mnt_export = nep;
383 			new_nep = true;
384 		}
385 		if (argp->ex_flags & MNT_EXPUBLIC) {
386 			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0) {
387 				if (new_nep) {
388 					mp->mnt_export = NULL;
389 					free(nep, M_MOUNT);
390 				}
391 				goto out;
392 			}
393 			new_nep = false;
394 			MNT_ILOCK(mp);
395 			if (do_exjail && mp->mnt_exjail == NULL) {
396 				mp->mnt_exjail = crhold(curthread->td_ucred);
397 				atomic_add_int(&pr->pr_exportcnt, 1);
398 			}
399 			mp->mnt_flag |= MNT_EXPUBLIC;
400 			MNT_IUNLOCK(mp);
401 		}
402 		if (argp->ex_numsecflavors == 0) {
403 			argp->ex_numsecflavors = 1;
404 			argp->ex_secflavors[0] = AUTH_SYS;
405 		}
406 		if ((error = vfs_hang_addrlist(mp, nep, argp))) {
407 			if (new_nep) {
408 				mp->mnt_export = NULL;
409 				free(nep, M_MOUNT);
410 			}
411 			goto out;
412 		}
413 		MNT_ILOCK(mp);
414 		if (do_exjail && mp->mnt_exjail == NULL) {
415 			mp->mnt_exjail = crhold(curthread->td_ucred);
416 			atomic_add_int(&pr->pr_exportcnt, 1);
417 		}
418 		mp->mnt_flag |= MNT_EXPORTED;
419 		MNT_IUNLOCK(mp);
420 	}
421 
422 out:
423 	lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
424 	/*
425 	 * Once we have executed the vfs_export() command, we do
426 	 * not want to keep the "export" option around in the
427 	 * options list, since that will cause subsequent MNT_UPDATE
428 	 * calls to fail.  The export information is saved in
429 	 * mp->mnt_export, so we can safely delete the "export" mount option
430 	 * here.
431 	 */
432 	vfs_deleteopt(mp->mnt_optnew, "export");
433 	vfs_deleteopt(mp->mnt_opt, "export");
434 	return (error);
435 }
436 
437 /*
438  * Get rid of credential references for this prison.
439  */
440 void
441 vfs_exjail_delete(struct prison *pr)
442 {
443 	struct mount *mp;
444 	struct ucred *cr;
445 	int error, i;
446 
447 	/*
448 	 * Since this function is called from prison_cleanup() after
449 	 * all processes in the prison have exited, the value of
450 	 * pr_exportcnt can no longer increase.  It is possible for
451 	 * a dismount of a file system exported within this prison
452 	 * to be in progress.  In this case, the file system is no
453 	 * longer in the mountlist and the mnt_exjail will be free'd
454 	 * by vfs_mount_destroy() at some time.  As such, pr_exportcnt
455 	 * and, therefore "i", is the upper bound on the number of
456 	 * mnt_exjail entries to be found by this function.
457 	 */
458 	i = atomic_load_int(&pr->pr_exportcnt);
459 	KASSERT(i >= 0, ("vfs_exjail_delete: pr_exportcnt negative"));
460 	if (i == 0)
461 		return;
462 	mtx_lock(&mountlist_mtx);
463 tryagain:
464 	TAILQ_FOREACH(mp, &mountlist, mnt_list) {
465 		MNT_ILOCK(mp);
466 		if (mp->mnt_exjail != NULL &&
467 		    mp->mnt_exjail->cr_prison == pr) {
468 			MNT_IUNLOCK(mp);
469 			error = vfs_busy(mp, MBF_MNTLSTLOCK | MBF_NOWAIT);
470 			if (error != 0) {
471 				/*
472 				 * If the vfs_busy() fails, we still want to
473 				 * get rid of mnt_exjail for two reasons:
474 				 * - a credential reference will result in
475 				 *   a prison not being removed
476 				 * - setting mnt_exjail NULL indicates that
477 				 *   the exports are no longer valid
478 				 * The now invalid exports will be deleted
479 				 * when the file system is dismounted or
480 				 * the file system is re-exported by mountd.
481 				 */
482 				cr = NULL;
483 				MNT_ILOCK(mp);
484 				if (mp->mnt_exjail != NULL &&
485 				    mp->mnt_exjail->cr_prison == pr) {
486 					cr = mp->mnt_exjail;
487 					mp->mnt_exjail = NULL;
488 				}
489 				MNT_IUNLOCK(mp);
490 				if (cr != NULL) {
491 					crfree(cr);
492 					i--;
493 				}
494 				if (i == 0)
495 					break;
496 				continue;
497 			}
498 			cr = NULL;
499 			lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL);
500 			MNT_ILOCK(mp);
501 			if (mp->mnt_exjail != NULL &&
502 			    mp->mnt_exjail->cr_prison == pr) {
503 				cr = mp->mnt_exjail;
504 				mp->mnt_exjail = NULL;
505 				mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
506 				MNT_IUNLOCK(mp);
507 				vfs_free_addrlist(mp->mnt_export);
508 				free(mp->mnt_export, M_MOUNT);
509 				mp->mnt_export = NULL;
510 			} else
511 				MNT_IUNLOCK(mp);
512 			lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
513 			if (cr != NULL) {
514 				crfree(cr);
515 				i--;
516 			}
517 			mtx_lock(&mountlist_mtx);
518 			vfs_unbusy(mp);
519 			if (i == 0)
520 				break;
521 			goto tryagain;
522 		}
523 		MNT_IUNLOCK(mp);
524 	}
525 	mtx_unlock(&mountlist_mtx);
526 }
527 
528 /*
529  * Set the publicly exported filesystem (WebNFS). Currently, only
530  * one public filesystem is possible in the spec (RFC 2054 and 2055)
531  */
532 int
533 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
534     struct export_args *argp)
535 {
536 	int error;
537 	struct vnode *rvp;
538 	char *cp;
539 
540 	/*
541 	 * mp == NULL -> invalidate the current info, the FS is
542 	 * no longer exported. May be called from either vfs_export
543 	 * or unmount, so check if it hasn't already been done.
544 	 */
545 	if (mp == NULL) {
546 		if (nfs_pub.np_valid) {
547 			nfs_pub.np_valid = 0;
548 			if (nfs_pub.np_index != NULL) {
549 				free(nfs_pub.np_index, M_TEMP);
550 				nfs_pub.np_index = NULL;
551 			}
552 		}
553 		return (0);
554 	}
555 
556 	/*
557 	 * Only one allowed at a time.
558 	 */
559 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
560 		return (EBUSY);
561 
562 	/*
563 	 * Get real filehandle for root of exported FS.
564 	 */
565 	bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
566 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
567 
568 	if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp)))
569 		return (error);
570 
571 	if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
572 		return (error);
573 
574 	vput(rvp);
575 
576 	/*
577 	 * If an indexfile was specified, pull it in.
578 	 */
579 	if (argp->ex_indexfile != NULL) {
580 		if (nfs_pub.np_index == NULL)
581 			nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP,
582 			    M_WAITOK);
583 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
584 		    MAXNAMLEN, (size_t *)0);
585 		if (!error) {
586 			/*
587 			 * Check for illegal filenames.
588 			 */
589 			for (cp = nfs_pub.np_index; *cp; cp++) {
590 				if (*cp == '/') {
591 					error = EINVAL;
592 					break;
593 				}
594 			}
595 		}
596 		if (error) {
597 			free(nfs_pub.np_index, M_TEMP);
598 			nfs_pub.np_index = NULL;
599 			return (error);
600 		}
601 	}
602 
603 	nfs_pub.np_mount = mp;
604 	nfs_pub.np_valid = 1;
605 	return (0);
606 }
607 
608 /*
609  * Used by the filesystems to determine if a given network address
610  * (passed in 'nam') is present in their exports list, returns a pointer
611  * to struct netcred so that the filesystem can examine it for
612  * access rights (read/write/etc).
613  */
614 static struct netcred *
615 vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
616 {
617 	RADIX_NODE_HEAD_RLOCK_TRACKER;
618 	struct netexport *nep;
619 	struct netcred *np = NULL;
620 	struct radix_node_head *rnh;
621 	struct sockaddr *saddr;
622 
623 	nep = mp->mnt_export;
624 	if (nep == NULL)
625 		return (NULL);
626 	if ((mp->mnt_flag & MNT_EXPORTED) == 0)
627 		return (NULL);
628 
629 	/*
630 	 * Lookup in the export list
631 	 */
632 	if (nam != NULL) {
633 		saddr = nam;
634 		rnh = NULL;
635 		switch (saddr->sa_family) {
636 		case AF_INET:
637 			rnh = nep->ne4;
638 			break;
639 		case AF_INET6:
640 			rnh = nep->ne6;
641 			break;
642 		}
643 		if (rnh != NULL) {
644 			RADIX_NODE_HEAD_RLOCK(rnh);
645 			np = (struct netcred *) (*rnh->rnh_matchaddr)(saddr, &rnh->rh);
646 			RADIX_NODE_HEAD_RUNLOCK(rnh);
647 			if (np != NULL && (np->netc_rnodes->rn_flags & RNF_ROOT) != 0)
648 				return (NULL);
649 		}
650 	}
651 
652 	/*
653 	 * If no address match, use the default if it exists.
654 	 */
655 	if (np == NULL && (mp->mnt_flag & MNT_DEFEXPORTED) != 0)
656 		return (&nep->ne_defexported);
657 
658 	return (np);
659 }
660 
661 /*
662  * XXX: This comment comes from the deprecated ufs_check_export()
663  * XXX: and may not entirely apply, but lacking something better:
664  * This is the generic part of fhtovp called after the underlying
665  * filesystem has validated the file handle.
666  *
667  * Verify that a host should have access to a filesystem.
668  */
669 
670 int
671 vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, uint64_t *extflagsp,
672     struct ucred **credanonp, int *numsecflavors, int *secflavors)
673 {
674 	struct netcred *np;
675 
676 	lockmgr(&mp->mnt_explock, LK_SHARED, NULL);
677 	np = vfs_export_lookup(mp, nam);
678 	if (np == NULL) {
679 		lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
680 		*credanonp = NULL;
681 		return (EACCES);
682 	}
683 	*extflagsp = np->netc_exflags;
684 	if ((*credanonp = np->netc_anon) != NULL)
685 		crhold(*credanonp);
686 	if (numsecflavors) {
687 		*numsecflavors = np->netc_numsecflavors;
688 		KASSERT(*numsecflavors > 0,
689 		    ("%s: numsecflavors <= 0", __func__));
690 		KASSERT(*numsecflavors < MAXSECFLAVORS,
691 		    ("%s: numsecflavors >= MAXSECFLAVORS", __func__));
692 	}
693 	if (secflavors && np->netc_numsecflavors > 0)
694 		memcpy(secflavors, np->netc_secflavors, np->netc_numsecflavors *
695 		    sizeof(int));
696 	lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
697 	return (0);
698 }
699