xref: /netbsd-src/sys/nfs/nfs_export.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /*	$NetBSD: nfs_export.c,v 1.38 2008/05/10 02:26:10 rumble Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998, 2004, 2005, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Charles M. Hannum.
12  * This code is derived from software contributed to The NetBSD Foundation
13  * by Julio M. Merino Vidal.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 /*
38  * Copyright (c) 1989, 1993
39  *	The Regents of the University of California.  All rights reserved.
40  * (c) UNIX System Laboratories, Inc.
41  * All or some portions of this file are derived from material licensed
42  * to the University of California by American Telephone and Telegraph
43  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
44  * the permission of UNIX System Laboratories, Inc.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. Neither the name of the University nor the names of its contributors
55  *    may be used to endorse or promote products derived from this software
56  *    without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  *
70  *	@(#)vfs_subr.c	8.13 (Berkeley) 4/18/94
71  */
72 
73 /*
74  * VFS exports list management.
75  */
76 
77 #include <sys/cdefs.h>
78 __KERNEL_RCSID(0, "$NetBSD: nfs_export.c,v 1.38 2008/05/10 02:26:10 rumble Exp $");
79 
80 #include "opt_compat_netbsd.h"
81 #include "opt_inet.h"
82 
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/queue.h>
86 #include <sys/proc.h>
87 #include <sys/mount.h>
88 #include <sys/vnode.h>
89 #include <sys/namei.h>
90 #include <sys/errno.h>
91 #include <sys/malloc.h>
92 #include <sys/domain.h>
93 #include <sys/mbuf.h>
94 #include <sys/dirent.h>
95 #include <sys/socket.h>		/* XXX for AF_MAX */
96 #include <sys/kauth.h>
97 
98 #include <net/radix.h>
99 
100 #include <netinet/in.h>
101 
102 #include <nfs/rpcv2.h>
103 #include <nfs/nfsproto.h>
104 #include <nfs/nfs.h>
105 #include <nfs/nfs_var.h>
106 
107 /*
108  * Network address lookup element.
109  */
110 struct netcred {
111 	struct	radix_node netc_rnodes[2];
112 	int	netc_refcnt;
113 	int	netc_exflags;
114 	kauth_cred_t netc_anon;
115 };
116 
117 /*
118  * Network export information.
119  */
120 struct netexport {
121 	CIRCLEQ_ENTRY(netexport) ne_list;
122 	struct mount *ne_mount;
123 	struct netcred ne_defexported;		      /* Default export */
124 	struct radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
125 };
126 CIRCLEQ_HEAD(, netexport) netexport_list =
127     CIRCLEQ_HEAD_INITIALIZER(netexport_list);
128 
129 /* Malloc type used by the mount<->netexport map. */
130 MALLOC_DEFINE(M_NFS_EXPORT, "nfs_export", "NFS export data");
131 
132 /* Publicly exported file system. */
133 struct nfs_public nfs_pub;
134 
135 /*
136  * Local prototypes.
137  */
138 static int init_exports(struct mount *, struct netexport **);
139 static int hang_addrlist(struct mount *, struct netexport *,
140     const struct export_args *);
141 static int sacheck(struct sockaddr *);
142 static int free_netcred(struct radix_node *, void *);
143 static int export(struct netexport *, const struct export_args *);
144 static int setpublicfs(struct mount *, struct netexport *,
145     const struct export_args *);
146 static struct netcred *netcred_lookup(struct netexport *, struct mbuf *);
147 static struct netexport *netexport_lookup(const struct mount *);
148 static struct netexport *netexport_lookup_byfsid(const fsid_t *);
149 static void netexport_clear(struct netexport *);
150 static void netexport_insert(struct netexport *);
151 static void netexport_remove(struct netexport *);
152 static void netexport_wrlock(void);
153 static void netexport_wrunlock(void);
154 
155 /*
156  * PUBLIC INTERFACE
157  */
158 
159 /*
160  * Declare and initialize the file system export hooks.
161  */
162 static void nfs_export_unmount(struct mount *);
163 
164 struct vfs_hooks nfs_export_hooks = {
165 	nfs_export_unmount,
166 	{ NULL, NULL }
167 };
168 
169 /*
170  * VFS unmount hook for NFS exports.
171  *
172  * Releases NFS exports list resources if the given mount point has some.
173  * As allocation happens lazily, it may be that it doesn't has this
174  * information, although it theorically should.
175  */
176 static void
177 nfs_export_unmount(struct mount *mp)
178 {
179 	struct netexport *ne;
180 
181 	KASSERT(mp != NULL);
182 
183 	netexport_wrlock();
184 	ne = netexport_lookup(mp);
185 	if (ne == NULL) {
186 		netexport_wrunlock();
187 		return;
188 	}
189 	netexport_clear(ne);
190 	netexport_remove(ne);
191 	netexport_wrunlock();
192 	free(ne, M_NFS_EXPORT);
193 }
194 
195 /*
196  * Atomically set the NFS exports list of the given file system, replacing
197  * it with a new list of entries.
198  *
199  * Returns zero on success or an appropriate error code otherwise.
200  *
201  * Helper function for the nfssvc(2) system call (NFSSVC_SETEXPORTSLIST
202  * command).
203  */
204 int
205 mountd_set_exports_list(const struct mountd_exports_list *mel, struct lwp *l)
206 {
207 	int error;
208 #ifdef notyet
209 	/* XXX: See below to see the reason why this is disabled. */
210 	size_t i;
211 #endif
212 	struct mount *mp;
213 	struct netexport *ne;
214 	struct nameidata nd;
215 	struct vnode *vp;
216 	struct fid *fid;
217 	size_t fid_size;
218 
219 	if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_NFS,
220 	    KAUTH_REQ_NETWORK_NFS_EXPORT, NULL, NULL, NULL) != 0)
221 		return EPERM;
222 
223 	/* Lookup the file system path. */
224 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, mel->mel_path);
225 	error = namei(&nd);
226 	if (error != 0)
227 		return error;
228 	vp = nd.ni_vp;
229 	mp = vp->v_mount;
230 
231 	fid_size = 0;
232 	if ((error = VFS_VPTOFH(vp, NULL, &fid_size)) == E2BIG) {
233 		fid = malloc(fid_size, M_TEMP, M_NOWAIT);
234 		if (fid != NULL) {
235 			error = VFS_VPTOFH(vp, fid, &fid_size);
236 			free(fid, M_TEMP);
237 		}
238 	}
239 	if (error != 0) {
240 		vput(vp);
241 		return EOPNOTSUPP;
242 	}
243 
244 	/* Mark the file system busy. */
245 	error = vfs_busy(mp, NULL);
246 	vput(vp);
247 	if (error != 0)
248 		return error;
249 
250 	netexport_wrlock();
251 	ne = netexport_lookup(mp);
252 	if (ne == NULL) {
253 		error = init_exports(mp, &ne);
254 		if (error != 0) {
255 			goto out;
256 		}
257 	}
258 
259 	KASSERT(ne != NULL);
260 	KASSERT(ne->ne_mount == mp);
261 
262 	/*
263 	 * XXX: The part marked as 'notyet' works fine from the kernel's
264 	 * point of view, in the sense that it is able to atomically update
265 	 * the complete exports list for a file system.  However, supporting
266 	 * this in mountd(8) requires a lot of work; so, for now, keep the
267 	 * old behavior of updating a single entry per call.
268 	 *
269 	 * When mountd(8) is fixed, just remove the second branch of this
270 	 * preprocessor conditional and enable the first one.
271 	 */
272 #ifdef notyet
273 	netexport_clear(ne);
274 	for (i = 0; error == 0 && i < mel->mel_nexports; i++)
275 		error = export(ne, &mel->mel_exports[i]);
276 #else
277 	if (mel->mel_nexports == 0)
278 		netexport_clear(ne);
279 	else if (mel->mel_nexports == 1)
280 		error = export(ne, &mel->mel_exports[0]);
281 	else {
282 		printf("mountd_set_exports_list: Cannot set more than one "
283 		    "entry at once (unimplemented)\n");
284 		error = EOPNOTSUPP;
285 	}
286 #endif
287 
288 out:
289 	netexport_wrunlock();
290 	vfs_unbusy(mp, false, NULL);
291 	return error;
292 }
293 
294 static void
295 netexport_insert(struct netexport *ne)
296 {
297 
298 	CIRCLEQ_INSERT_HEAD(&netexport_list, ne, ne_list);
299 }
300 
301 static void
302 netexport_remove(struct netexport *ne)
303 {
304 
305 	CIRCLEQ_REMOVE(&netexport_list, ne, ne_list);
306 }
307 
308 static struct netexport *
309 netexport_lookup(const struct mount *mp)
310 {
311 	struct netexport *ne;
312 
313 	CIRCLEQ_FOREACH(ne, &netexport_list, ne_list) {
314 		if (ne->ne_mount == mp) {
315 			goto done;
316 		}
317 	}
318 	ne = NULL;
319 done:
320 	return ne;
321 }
322 
323 static struct netexport *
324 netexport_lookup_byfsid(const fsid_t *fsid)
325 {
326 	struct netexport *ne;
327 
328 	CIRCLEQ_FOREACH(ne, &netexport_list, ne_list) {
329 		const struct mount *mp = ne->ne_mount;
330 
331 		if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] &&
332 		    mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) {
333 			goto done;
334 		}
335 	}
336 	ne = NULL;
337 done:
338 
339 	return ne;
340 }
341 
342 /*
343  * Check if the file system specified by the 'mp' mount structure is
344  * exported to a client with 'anon' anonymous credentials.  The 'mb'
345  * argument is an mbuf containing the network address of the client.
346  * The return parameters for the export flags for the client are returned
347  * in the address specified by 'wh'.
348  *
349  * This function is used exclusively by the NFS server.  It is generally
350  * invoked before VFS_FHTOVP to validate that client has access to the
351  * file system.
352  */
353 
354 int
355 netexport_check(const fsid_t *fsid, struct mbuf *mb, struct mount **mpp,
356     int *wh, kauth_cred_t *anon)
357 {
358 	struct netexport *ne;
359 	struct netcred *np;
360 
361 	ne = netexport_lookup_byfsid(fsid);
362 	if (ne == NULL) {
363 		return EACCES;
364 	}
365 	np = netcred_lookup(ne, mb);
366 	if (np == NULL) {
367 		return EACCES;
368 	}
369 
370 	*mpp = ne->ne_mount;
371 	*wh = np->netc_exflags;
372 	*anon = np->netc_anon;
373 
374 	return 0;
375 }
376 
377 #ifdef COMPAT_30
378 /*
379  * Handles legacy export requests.  In this case, the export information
380  * is hardcoded in a specific place of the mount arguments structure (given
381  * in data); the request for an update is given through the fspec field
382  * (also in a known location), which must be a null pointer.
383  *
384  * Returns EJUSTRETURN if the given command was not a export request.
385  * Otherwise, returns 0 on success or an appropriate error code otherwise.
386  */
387 int
388 nfs_update_exports_30(struct mount *mp, const char *path,
389     struct mnt_export_args30 *args, struct lwp *l)
390 {
391 	struct mountd_exports_list mel;
392 
393 	mel.mel_path = path;
394 
395 	if (args->fspec != NULL)
396 		return EJUSTRETURN;
397 
398 	if (args->eargs.ex_flags & 0x00020000) {
399 		/* Request to delete exports.  The mask above holds the
400 		 * value that used to be in MNT_DELEXPORT. */
401 		mel.mel_nexports = 0;
402 	} else {
403 		/* The following assumes export_args has not changed since
404 		 * export_args30 - typedef checks sizes. */
405 		typedef char x[sizeof args->eargs == sizeof *mel.mel_exports ? 1 : -1];
406 
407 		mel.mel_nexports = 1;
408 		mel.mel_exports = (void *)&args->eargs;
409 	}
410 
411 	return mountd_set_exports_list(&mel, l);
412 }
413 #endif
414 
415 /*
416  * INTERNAL FUNCTIONS
417  */
418 
419 /*
420  * Initializes NFS exports for the file system given in 'mp' if it supports
421  * file handles; this is determined by checking whether mp's vfs_vptofh and
422  * vfs_fhtovp operations are NULL or not.
423  *
424  * If successful, returns 0 and sets *mnpp to the address of the new
425  * mount_netexport_pair item; otherwise returns an appropriate error code
426  * and *mnpp remains unmodified.
427  */
428 static int
429 init_exports(struct mount *mp, struct netexport **nep)
430 {
431 	int error;
432 	struct export_args ea;
433 	struct netexport *ne;
434 
435 	KASSERT(mp != NULL);
436 
437 	/* Ensure that we do not already have this mount point. */
438 	KASSERT(netexport_lookup(mp) == NULL);
439 
440 	ne = malloc(sizeof(*ne), M_NFS_EXPORT, M_WAITOK | M_ZERO);
441 	ne->ne_mount = mp;
442 
443 	/* Set the default export entry.  Handled internally by export upon
444 	 * first call. */
445 	memset(&ea, 0, sizeof(ea));
446 	ea.ex_root = -2;
447 	if (mp->mnt_flag & MNT_RDONLY)
448 		ea.ex_flags |= MNT_EXRDONLY;
449 	error = export(ne, &ea);
450 	if (error != 0) {
451 		free(ne, M_NFS_EXPORT);
452 	} else {
453 		netexport_insert(ne);
454 		*nep = ne;
455 	}
456 
457 	return error;
458 }
459 
460 /*
461  * Build hash lists of net addresses and hang them off the mount point.
462  * Called by export() to set up a new entry in the lists of export
463  * addresses.
464  */
465 static int
466 hang_addrlist(struct mount *mp, struct netexport *nep,
467     const struct export_args *argp)
468 {
469 	int error, i;
470 	struct netcred *np, *enp;
471 	struct radix_node_head *rnh;
472 	struct sockaddr *saddr, *smask;
473 	struct domain *dom;
474 
475 	smask = NULL;
476 
477 	if (argp->ex_addrlen == 0) {
478 		if (mp->mnt_flag & MNT_DEFEXPORTED)
479 			return EPERM;
480 		np = &nep->ne_defexported;
481 		KASSERT(np->netc_anon == NULL);
482 		np->netc_anon = kauth_cred_alloc();
483 		np->netc_exflags = argp->ex_flags;
484 		kauth_uucred_to_cred(np->netc_anon, &argp->ex_anon);
485 		mp->mnt_flag |= MNT_DEFEXPORTED;
486 		return 0;
487 	}
488 
489 	if (argp->ex_addrlen > MLEN || argp->ex_masklen > MLEN)
490 		return EINVAL;
491 
492 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
493 	np = malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
494 	np->netc_anon = kauth_cred_alloc();
495 	saddr = (struct sockaddr *)(np + 1);
496 	error = copyin(argp->ex_addr, saddr, argp->ex_addrlen);
497 	if (error)
498 		goto out;
499 	if (saddr->sa_len > argp->ex_addrlen)
500 		saddr->sa_len = argp->ex_addrlen;
501 	if (sacheck(saddr) == -1)
502 		return EINVAL;
503 	if (argp->ex_masklen) {
504 		smask = (struct sockaddr *)((char *)saddr + argp->ex_addrlen);
505 		error = copyin(argp->ex_mask, smask, argp->ex_masklen);
506 		if (error)
507 			goto out;
508 		if (smask->sa_len > argp->ex_masklen)
509 			smask->sa_len = argp->ex_masklen;
510 		if (smask->sa_family != saddr->sa_family)
511 			return EINVAL;
512 		if (sacheck(smask) == -1)
513 			return EINVAL;
514 	}
515 	i = saddr->sa_family;
516 	if ((rnh = nep->ne_rtable[i]) == 0) {
517 		/*
518 		 * Seems silly to initialize every AF when most are not
519 		 * used, do so on demand here
520 		 */
521 		DOMAIN_FOREACH(dom) {
522 			if (dom->dom_family == i && dom->dom_rtattach) {
523 				dom->dom_rtattach((void **)&nep->ne_rtable[i],
524 					dom->dom_rtoffset);
525 				break;
526 			}
527 		}
528 		if ((rnh = nep->ne_rtable[i]) == 0) {
529 			error = ENOBUFS;
530 			goto out;
531 		}
532 	}
533 
534 	enp = (struct netcred *)(*rnh->rnh_addaddr)(saddr, smask, rnh,
535 	    np->netc_rnodes);
536 	if (enp != np) {
537 		if (enp == NULL) {
538 			enp = (struct netcred *)(*rnh->rnh_lookup)(saddr,
539 			    smask, rnh);
540 			if (enp == NULL) {
541 				error = EPERM;
542 				goto out;
543 			}
544 		} else
545 			enp->netc_refcnt++;
546 
547 		goto check;
548 	} else
549 		enp->netc_refcnt = 1;
550 
551 	np->netc_exflags = argp->ex_flags;
552 	kauth_uucred_to_cred(np->netc_anon, &argp->ex_anon);
553 	return 0;
554 check:
555 	if (enp->netc_exflags != argp->ex_flags ||
556 	    kauth_cred_uucmp(enp->netc_anon, &argp->ex_anon) != 0)
557 		error = EPERM;
558 	else
559 		error = 0;
560 out:
561 	KASSERT(np->netc_anon != NULL);
562 	kauth_cred_free(np->netc_anon);
563 	free(np, M_NETADDR);
564 	return error;
565 }
566 
567 /*
568  * Ensure that the address stored in 'sa' is valid.
569  * Returns zero on success, otherwise -1.
570  */
571 static int
572 sacheck(struct sockaddr *sa)
573 {
574 
575 	switch (sa->sa_family) {
576 #ifdef INET
577 	case AF_INET: {
578 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
579 		char *p = (char *)sin->sin_zero;
580 		size_t i;
581 
582 		if (sin->sin_len != sizeof(*sin))
583 			return -1;
584 		if (sin->sin_port != 0)
585 			return -1;
586 		for (i = 0; i < sizeof(sin->sin_zero); i++)
587 			if (*p++ != '\0')
588 				return -1;
589 		return 0;
590 	}
591 #endif
592 #ifdef INET6
593 	case AF_INET6: {
594 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
595 
596 		if (sin6->sin6_len != sizeof(*sin6))
597 			return -1;
598 		if (sin6->sin6_port != 0)
599 			return -1;
600 		return 0;
601 	}
602 #endif
603 	default:
604 		return -1;
605 	}
606 }
607 
608 /*
609  * Free the netcred object pointed to by the 'rn' radix node.
610  * 'w' holds a pointer to the radix tree head.
611  */
612 static int
613 free_netcred(struct radix_node *rn, void *w)
614 {
615 	struct radix_node_head *rnh = (struct radix_node_head *)w;
616 	struct netcred *np = (struct netcred *)(void *)rn;
617 
618 	(*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
619 	if (--(np->netc_refcnt) <= 0) {
620 		KASSERT(np->netc_anon != NULL);
621 		kauth_cred_free(np->netc_anon);
622 		free(np, M_NETADDR);
623 	}
624 	return 0;
625 }
626 
627 /*
628  * Clears the exports list for a given file system.
629  */
630 static void
631 netexport_clear(struct netexport *ne)
632 {
633 	struct radix_node_head *rnh;
634 	struct mount *mp = ne->ne_mount;
635 	int i;
636 
637 	if (mp->mnt_flag & MNT_EXPUBLIC) {
638 		setpublicfs(NULL, NULL, NULL);
639 		mp->mnt_flag &= ~MNT_EXPUBLIC;
640 	}
641 
642 	for (i = 0; i <= AF_MAX; i++) {
643 		if ((rnh = ne->ne_rtable[i]) != NULL) {
644 			rn_walktree(rnh, free_netcred, rnh);
645 			free(rnh, M_RTABLE);
646 			ne->ne_rtable[i] = NULL;
647 		}
648 	}
649 
650 	if ((mp->mnt_flag & MNT_DEFEXPORTED) != 0) {
651 		struct netcred *np = &ne->ne_defexported;
652 
653 		KASSERT(np->netc_anon != NULL);
654 		kauth_cred_free(np->netc_anon);
655 		np->netc_anon = NULL;
656 	} else {
657 		KASSERT(ne->ne_defexported.netc_anon == NULL);
658 	}
659 
660 	mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
661 }
662 
663 /*
664  * Add a new export entry (described by an export_args structure) to the
665  * given file system.
666  */
667 static int
668 export(struct netexport *nep, const struct export_args *argp)
669 {
670 	struct mount *mp = nep->ne_mount;
671 	int error;
672 
673 	if (argp->ex_flags & MNT_EXPORTED) {
674 		if (argp->ex_flags & MNT_EXPUBLIC) {
675 			if ((error = setpublicfs(mp, nep, argp)) != 0)
676 				return error;
677 			mp->mnt_flag |= MNT_EXPUBLIC;
678 		}
679 		if ((error = hang_addrlist(mp, nep, argp)) != 0)
680 			return error;
681 		mp->mnt_flag |= MNT_EXPORTED;
682 	}
683 	return 0;
684 }
685 
686 /*
687  * Set the publicly exported filesystem (WebNFS).  Currently, only
688  * one public filesystem is possible in the spec (RFC 2054 and 2055)
689  */
690 static int
691 setpublicfs(struct mount *mp, struct netexport *nep,
692     const struct export_args *argp)
693 {
694 	char *cp;
695 	int error;
696 	struct vnode *rvp;
697 	size_t fhsize;
698 
699 	/*
700 	 * mp == NULL -> invalidate the current info, the FS is
701 	 * no longer exported. May be called from either export
702 	 * or unmount, so check if it hasn't already been done.
703 	 */
704 	if (mp == NULL) {
705 		if (nfs_pub.np_valid) {
706 			nfs_pub.np_valid = 0;
707 			if (nfs_pub.np_handle != NULL) {
708 				free(nfs_pub.np_handle, M_TEMP);
709 				nfs_pub.np_handle = NULL;
710 			}
711 			if (nfs_pub.np_index != NULL) {
712 				FREE(nfs_pub.np_index, M_TEMP);
713 				nfs_pub.np_index = NULL;
714 			}
715 		}
716 		return 0;
717 	}
718 
719 	/*
720 	 * Only one allowed at a time.
721 	 */
722 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
723 		return EBUSY;
724 
725 	/*
726 	 * Get real filehandle for root of exported FS.
727 	 */
728 	if ((error = VFS_ROOT(mp, &rvp)))
729 		return error;
730 
731 	fhsize = 0;
732 	error = vfs_composefh(rvp, NULL, &fhsize);
733 	if (error != E2BIG)
734 		return error;
735 	nfs_pub.np_handle = malloc(fhsize, M_TEMP, M_NOWAIT);
736 	if (nfs_pub.np_handle == NULL)
737 		error = ENOMEM;
738 	else
739 		error = vfs_composefh(rvp, nfs_pub.np_handle, &fhsize);
740 	if (error)
741 		return error;
742 
743 	vput(rvp);
744 
745 	/*
746 	 * If an indexfile was specified, pull it in.
747 	 */
748 	if (argp->ex_indexfile != NULL) {
749 		MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
750 		    M_WAITOK);
751 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
752 		    MAXNAMLEN, (size_t *)0);
753 		if (!error) {
754 			/*
755 			 * Check for illegal filenames.
756 			 */
757 			for (cp = nfs_pub.np_index; *cp; cp++) {
758 				if (*cp == '/') {
759 					error = EINVAL;
760 					break;
761 				}
762 			}
763 		}
764 		if (error) {
765 			FREE(nfs_pub.np_index, M_TEMP);
766 			return error;
767 		}
768 	}
769 
770 	nfs_pub.np_mount = mp;
771 	nfs_pub.np_valid = 1;
772 	return 0;
773 }
774 
775 /*
776  * Lookup an export entry in the exports list that matches the address
777  * stored in 'nam'.  If no entry is found, the default one is used instead
778  * (if available).
779  */
780 static struct netcred *
781 netcred_lookup(struct netexport *ne, struct mbuf *nam)
782 {
783 	struct netcred *np;
784 	struct radix_node_head *rnh;
785 	struct sockaddr *saddr;
786 
787 	if ((ne->ne_mount->mnt_flag & MNT_EXPORTED) == 0) {
788 		return NULL;
789 	}
790 
791 	/*
792 	 * Lookup in the export list first.
793 	 */
794 	np = NULL;
795 	if (nam != NULL) {
796 		saddr = mtod(nam, struct sockaddr *);
797 		rnh = ne->ne_rtable[saddr->sa_family];
798 		if (rnh != NULL) {
799 			np = (struct netcred *)
800 				(*rnh->rnh_matchaddr)((void *)saddr,
801 						      rnh);
802 			if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
803 				np = NULL;
804 		}
805 	}
806 	/*
807 	 * If no address match, use the default if it exists.
808 	 */
809 	if (np == NULL && ne->ne_mount->mnt_flag & MNT_DEFEXPORTED)
810 		np = &ne->ne_defexported;
811 
812 	return np;
813 }
814 
815 krwlock_t netexport_lock;
816 
817 void
818 netexport_rdlock(void)
819 {
820 
821 	rw_enter(&netexport_lock, RW_READER);
822 }
823 
824 void
825 netexport_rdunlock(void)
826 {
827 
828 	rw_exit(&netexport_lock);
829 }
830 
831 static void
832 netexport_wrlock(void)
833 {
834 
835 	rw_enter(&netexport_lock, RW_WRITER);
836 }
837 
838 static void
839 netexport_wrunlock(void)
840 {
841 
842 	rw_exit(&netexport_lock);
843 }
844