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