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