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