xref: /netbsd-src/sys/nfs/nfs_export.c (revision da9817918ec7e88db2912a2882967c7570a83f47)
1 /*	$NetBSD: nfs_export.c,v 1.47 2009/05/23 18:19:19 ad 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  * Lock order: vfs_busy -> mnt_updating -> netexport_lock.
77  */
78 
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: nfs_export.c,v 1.47 2009/05/23 18:19:19 ad Exp $");
81 
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/kernel.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 /* Publicly exported file system. */
130 struct nfs_public nfs_pub;
131 
132 /*
133  * Local prototypes.
134  */
135 static int init_exports(struct mount *, struct netexport **);
136 static int hang_addrlist(struct mount *, struct netexport *,
137     const struct export_args *);
138 static int sacheck(struct sockaddr *);
139 static int free_netcred(struct radix_node *, void *);
140 static int export(struct netexport *, const struct export_args *);
141 static int setpublicfs(struct mount *, struct netexport *,
142     const struct export_args *);
143 static struct netcred *netcred_lookup(struct netexport *, struct mbuf *);
144 static struct netexport *netexport_lookup(const struct mount *);
145 static struct netexport *netexport_lookup_byfsid(const fsid_t *);
146 static void netexport_clear(struct netexport *);
147 static void netexport_insert(struct netexport *);
148 static void netexport_remove(struct netexport *);
149 static void netexport_wrlock(void);
150 static void netexport_wrunlock(void);
151 static int nfs_export_update_30(struct mount *mp, const char *path, void *);
152 
153 static krwlock_t netexport_lock;
154 
155 /*
156  * PUBLIC INTERFACE
157  */
158 
159 /*
160  * Declare and initialize the file system export hooks.
161  */
162 static void netexport_unmount(struct mount *);
163 
164 struct vfs_hooks nfs_export_hooks = {
165 	{ NULL, NULL },
166 	.vh_unmount = netexport_unmount,
167 	.vh_reexport = nfs_export_update_30,
168 };
169 
170 /*
171  * VFS unmount hook for NFS exports.
172  *
173  * Releases NFS exports list resources if the given mount point has some.
174  * As allocation happens lazily, it may be that it doesn't has this
175  * information, although it theorically should.
176  */
177 static void
178 netexport_unmount(struct mount *mp)
179 {
180 	struct netexport *ne;
181 
182 	KASSERT(mp != NULL);
183 
184 	netexport_wrlock();
185 	ne = netexport_lookup(mp);
186 	if (ne == NULL) {
187 		netexport_wrunlock();
188 		return;
189 	}
190 	netexport_clear(ne);
191 	netexport_remove(ne);
192 	netexport_wrunlock();
193 	kmem_free(ne, sizeof(*ne));
194 }
195 
196 void
197 netexport_init(void)
198 {
199 
200 	rw_init(&netexport_lock);
201 }
202 
203 void
204 netexport_fini(void)
205 {
206 	struct netexport *ne;
207 	struct mount *mp;
208 	int error;
209 
210 	while (!CIRCLEQ_EMPTY(&netexport_list)) {
211 		netexport_wrlock();
212 		ne = CIRCLEQ_FIRST(&netexport_list);
213 		mp = ne->ne_mount;
214 		error = vfs_busy(mp, NULL);
215 		netexport_wrunlock();
216 		if (error != 0) {
217 			kpause("nfsfini", false, hz, NULL);
218 			continue;
219 		}
220 		mutex_enter(&mp->mnt_updating);	/* mnt_flag */
221 		netexport_unmount(mp);
222 		mutex_exit(&mp->mnt_updating);	/* mnt_flag */
223 		vfs_unbusy(mp, false, NULL);
224 	}
225 	rw_destroy(&netexport_lock);
226 }
227 
228 
229 /*
230  * Atomically set the NFS exports list of the given file system, replacing
231  * it with a new list of entries.
232  *
233  * Returns zero on success or an appropriate error code otherwise.
234  *
235  * Helper function for the nfssvc(2) system call (NFSSVC_SETEXPORTSLIST
236  * command).
237  */
238 int
239 mountd_set_exports_list(const struct mountd_exports_list *mel, struct lwp *l)
240 {
241 	int error;
242 #ifdef notyet
243 	/* XXX: See below to see the reason why this is disabled. */
244 	size_t i;
245 #endif
246 	struct mount *mp;
247 	struct netexport *ne;
248 	struct nameidata nd;
249 	struct vnode *vp;
250 	size_t fid_size;
251 
252 	if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_NFS,
253 	    KAUTH_REQ_NETWORK_NFS_EXPORT, NULL, NULL, NULL) != 0)
254 		return EPERM;
255 
256 	/* Lookup the file system path. */
257 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, mel->mel_path);
258 	error = namei(&nd);
259 	if (error != 0)
260 		return error;
261 	vp = nd.ni_vp;
262 	mp = vp->v_mount;
263 
264 	/*
265 	 * Make sure the file system can do vptofh.  If the file system
266 	 * knows the handle's size, just trust it's able to do the
267 	 * actual translation also (otherwise we should check fhtovp
268 	 * also, and that's getting a wee bit ridiculous).
269 	 */
270 	fid_size = 0;
271 	if ((error = VFS_VPTOFH(vp, NULL, &fid_size)) != E2BIG) {
272 		vput(vp);
273 		return EOPNOTSUPP;
274 	}
275 
276 	/* Mark the file system busy. */
277 	error = vfs_busy(mp, NULL);
278 	vput(vp);
279 	if (error != 0)
280 		return error;
281 	mutex_enter(&mp->mnt_updating);	/* mnt_flag */
282 	netexport_wrlock();
283 	ne = netexport_lookup(mp);
284 	if (ne == NULL) {
285 		error = init_exports(mp, &ne);
286 		if (error != 0) {
287 			goto out;
288 		}
289 	}
290 
291 	KASSERT(ne != NULL);
292 	KASSERT(ne->ne_mount == mp);
293 
294 	/*
295 	 * XXX: The part marked as 'notyet' works fine from the kernel's
296 	 * point of view, in the sense that it is able to atomically update
297 	 * the complete exports list for a file system.  However, supporting
298 	 * this in mountd(8) requires a lot of work; so, for now, keep the
299 	 * old behavior of updating a single entry per call.
300 	 *
301 	 * When mountd(8) is fixed, just remove the second branch of this
302 	 * preprocessor conditional and enable the first one.
303 	 */
304 #ifdef notyet
305 	netexport_clear(ne);
306 	for (i = 0; error == 0 && i < mel->mel_nexports; i++)
307 		error = export(ne, &mel->mel_exports[i]);
308 #else
309 	if (mel->mel_nexports == 0)
310 		netexport_clear(ne);
311 	else if (mel->mel_nexports == 1)
312 		error = export(ne, &mel->mel_exports[0]);
313 	else {
314 		printf("mountd_set_exports_list: Cannot set more than one "
315 		    "entry at once (unimplemented)\n");
316 		error = EOPNOTSUPP;
317 	}
318 #endif
319 
320 out:
321 	netexport_wrunlock();
322 	mutex_exit(&mp->mnt_updating);	/* mnt_flag */
323 	vfs_unbusy(mp, false, NULL);
324 	return error;
325 }
326 
327 static void
328 netexport_insert(struct netexport *ne)
329 {
330 
331 	CIRCLEQ_INSERT_HEAD(&netexport_list, ne, ne_list);
332 }
333 
334 static void
335 netexport_remove(struct netexport *ne)
336 {
337 
338 	CIRCLEQ_REMOVE(&netexport_list, ne, ne_list);
339 }
340 
341 static struct netexport *
342 netexport_lookup(const struct mount *mp)
343 {
344 	struct netexport *ne;
345 
346 	CIRCLEQ_FOREACH(ne, &netexport_list, ne_list) {
347 		if (ne->ne_mount == mp) {
348 			goto done;
349 		}
350 	}
351 	ne = NULL;
352 done:
353 	return ne;
354 }
355 
356 static struct netexport *
357 netexport_lookup_byfsid(const fsid_t *fsid)
358 {
359 	struct netexport *ne;
360 
361 	CIRCLEQ_FOREACH(ne, &netexport_list, ne_list) {
362 		const struct mount *mp = ne->ne_mount;
363 
364 		if (mp->mnt_stat.f_fsidx.__fsid_val[0] == fsid->__fsid_val[0] &&
365 		    mp->mnt_stat.f_fsidx.__fsid_val[1] == fsid->__fsid_val[1]) {
366 			goto done;
367 		}
368 	}
369 	ne = NULL;
370 done:
371 
372 	return ne;
373 }
374 
375 /*
376  * Check if the file system specified by the 'mp' mount structure is
377  * exported to a client with 'anon' anonymous credentials.  The 'mb'
378  * argument is an mbuf containing the network address of the client.
379  * The return parameters for the export flags for the client are returned
380  * in the address specified by 'wh'.
381  *
382  * This function is used exclusively by the NFS server.  It is generally
383  * invoked before VFS_FHTOVP to validate that client has access to the
384  * file system.
385  */
386 
387 int
388 netexport_check(const fsid_t *fsid, struct mbuf *mb, struct mount **mpp,
389     int *wh, kauth_cred_t *anon)
390 {
391 	struct netexport *ne;
392 	struct netcred *np;
393 
394 	ne = netexport_lookup_byfsid(fsid);
395 	if (ne == NULL) {
396 		return EACCES;
397 	}
398 	np = netcred_lookup(ne, mb);
399 	if (np == NULL) {
400 		return EACCES;
401 	}
402 
403 	*mpp = ne->ne_mount;
404 	*wh = np->netc_exflags;
405 	*anon = np->netc_anon;
406 
407 	return 0;
408 }
409 
410 /*
411  * Handles legacy export requests.  In this case, the export information
412  * is hardcoded in a specific place of the mount arguments structure (given
413  * in data); the request for an update is given through the fspec field
414  * (also in a known location), which must be a null pointer.
415  *
416  * Returns EJUSTRETURN if the given command was not a export request.
417  * Otherwise, returns 0 on success or an appropriate error code otherwise.
418  */
419 static int
420 nfs_export_update_30(struct mount *mp, const char *path, void *data)
421 {
422 	struct mountd_exports_list mel;
423 	struct mnt_export_args30 *args;
424 
425 	args = data;
426 	mel.mel_path = path;
427 
428 	if (args->fspec != NULL)
429 		return EJUSTRETURN;
430 
431 	if (args->eargs.ex_flags & 0x00020000) {
432 		/* Request to delete exports.  The mask above holds the
433 		 * value that used to be in MNT_DELEXPORT. */
434 		mel.mel_nexports = 0;
435 	} else {
436 		/* The following assumes export_args has not changed since
437 		 * export_args30 - typedef checks sizes. */
438 		typedef char x[sizeof args->eargs == sizeof *mel.mel_exports ? 1 : -1];
439 
440 		mel.mel_nexports = 1;
441 		mel.mel_exports = (void *)&args->eargs;
442 	}
443 
444 	return mountd_set_exports_list(&mel, curlwp);
445 }
446 
447 /*
448  * INTERNAL FUNCTIONS
449  */
450 
451 /*
452  * Initializes NFS exports for the mountpoint given in 'mp'.
453  * If successful, returns 0 and sets *nep to the address of the new
454  * netexport item; otherwise returns an appropriate error code
455  * and *nep remains unmodified.
456  */
457 static int
458 init_exports(struct mount *mp, struct netexport **nep)
459 {
460 	int error;
461 	struct export_args ea;
462 	struct netexport *ne;
463 
464 	KASSERT(mp != NULL);
465 
466 	/* Ensure that we do not already have this mount point. */
467 	KASSERT(netexport_lookup(mp) == NULL);
468 
469 	ne = kmem_zalloc(sizeof(*ne), KM_SLEEP);
470 	ne->ne_mount = mp;
471 
472 	/* Set the default export entry.  Handled internally by export upon
473 	 * first call. */
474 	memset(&ea, 0, sizeof(ea));
475 	ea.ex_root = -2;
476 	if (mp->mnt_flag & MNT_RDONLY)
477 		ea.ex_flags |= MNT_EXRDONLY;
478 	error = export(ne, &ea);
479 	if (error != 0) {
480 		kmem_free(ne, sizeof(*ne));
481 	} else {
482 		netexport_insert(ne);
483 		*nep = ne;
484 	}
485 
486 	return error;
487 }
488 
489 /*
490  * Build hash lists of net addresses and hang them off the mount point.
491  * Called by export() to set up a new entry in the lists of export
492  * addresses.
493  */
494 static int
495 hang_addrlist(struct mount *mp, struct netexport *nep,
496     const struct export_args *argp)
497 {
498 	int error, i;
499 	struct netcred *np, *enp;
500 	struct radix_node_head *rnh;
501 	struct sockaddr *saddr, *smask;
502 	struct domain *dom;
503 
504 	smask = NULL;
505 
506 	if (argp->ex_addrlen == 0) {
507 		if (mp->mnt_flag & MNT_DEFEXPORTED)
508 			return EPERM;
509 		np = &nep->ne_defexported;
510 		KASSERT(np->netc_anon == NULL);
511 		np->netc_anon = kauth_cred_alloc();
512 		np->netc_exflags = argp->ex_flags;
513 		kauth_uucred_to_cred(np->netc_anon, &argp->ex_anon);
514 		mp->mnt_flag |= MNT_DEFEXPORTED;
515 		return 0;
516 	}
517 
518 	if (argp->ex_addrlen > MLEN || argp->ex_masklen > MLEN)
519 		return EINVAL;
520 
521 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
522 	np = malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
523 	np->netc_anon = kauth_cred_alloc();
524 	saddr = (struct sockaddr *)(np + 1);
525 	error = copyin(argp->ex_addr, saddr, argp->ex_addrlen);
526 	if (error)
527 		goto out;
528 	if (saddr->sa_len > argp->ex_addrlen)
529 		saddr->sa_len = argp->ex_addrlen;
530 	if (sacheck(saddr) == -1)
531 		return EINVAL;
532 	if (argp->ex_masklen) {
533 		smask = (struct sockaddr *)((char *)saddr + argp->ex_addrlen);
534 		error = copyin(argp->ex_mask, smask, argp->ex_masklen);
535 		if (error)
536 			goto out;
537 		if (smask->sa_len > argp->ex_masklen)
538 			smask->sa_len = argp->ex_masklen;
539 		if (smask->sa_family != saddr->sa_family)
540 			return EINVAL;
541 		if (sacheck(smask) == -1)
542 			return EINVAL;
543 	}
544 	i = saddr->sa_family;
545 	if ((rnh = nep->ne_rtable[i]) == 0) {
546 		/*
547 		 * Seems silly to initialize every AF when most are not
548 		 * used, do so on demand here
549 		 */
550 		DOMAIN_FOREACH(dom) {
551 			if (dom->dom_family == i && dom->dom_rtattach) {
552 				dom->dom_rtattach((void **)&nep->ne_rtable[i],
553 					dom->dom_rtoffset);
554 				break;
555 			}
556 		}
557 		if ((rnh = nep->ne_rtable[i]) == 0) {
558 			error = ENOBUFS;
559 			goto out;
560 		}
561 	}
562 
563 	enp = (struct netcred *)(*rnh->rnh_addaddr)(saddr, smask, rnh,
564 	    np->netc_rnodes);
565 	if (enp != np) {
566 		if (enp == NULL) {
567 			enp = (struct netcred *)(*rnh->rnh_lookup)(saddr,
568 			    smask, rnh);
569 			if (enp == NULL) {
570 				error = EPERM;
571 				goto out;
572 			}
573 		} else
574 			enp->netc_refcnt++;
575 
576 		goto check;
577 	} else
578 		enp->netc_refcnt = 1;
579 
580 	np->netc_exflags = argp->ex_flags;
581 	kauth_uucred_to_cred(np->netc_anon, &argp->ex_anon);
582 	return 0;
583 check:
584 	if (enp->netc_exflags != argp->ex_flags ||
585 	    kauth_cred_uucmp(enp->netc_anon, &argp->ex_anon) != 0)
586 		error = EPERM;
587 	else
588 		error = 0;
589 out:
590 	KASSERT(np->netc_anon != NULL);
591 	kauth_cred_free(np->netc_anon);
592 	free(np, M_NETADDR);
593 	return error;
594 }
595 
596 /*
597  * Ensure that the address stored in 'sa' is valid.
598  * Returns zero on success, otherwise -1.
599  */
600 static int
601 sacheck(struct sockaddr *sa)
602 {
603 
604 	switch (sa->sa_family) {
605 	case AF_INET: {
606 		struct sockaddr_in *sin = (struct sockaddr_in *)sa;
607 		char *p = (char *)sin->sin_zero;
608 		size_t i;
609 
610 		if (sin->sin_len != sizeof(*sin))
611 			return -1;
612 		if (sin->sin_port != 0)
613 			return -1;
614 		for (i = 0; i < sizeof(sin->sin_zero); i++)
615 			if (*p++ != '\0')
616 				return -1;
617 		return 0;
618 	}
619 	case AF_INET6: {
620 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
621 
622 		if (sin6->sin6_len != sizeof(*sin6))
623 			return -1;
624 		if (sin6->sin6_port != 0)
625 			return -1;
626 		return 0;
627 	}
628 	default:
629 		return -1;
630 	}
631 }
632 
633 /*
634  * Free the netcred object pointed to by the 'rn' radix node.
635  * 'w' holds a pointer to the radix tree head.
636  */
637 static int
638 free_netcred(struct radix_node *rn, void *w)
639 {
640 	struct radix_node_head *rnh = (struct radix_node_head *)w;
641 	struct netcred *np = (struct netcred *)(void *)rn;
642 
643 	(*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
644 	if (--(np->netc_refcnt) <= 0) {
645 		KASSERT(np->netc_anon != NULL);
646 		kauth_cred_free(np->netc_anon);
647 		free(np, M_NETADDR);
648 	}
649 	return 0;
650 }
651 
652 /*
653  * Clears the exports list for a given file system.
654  */
655 static void
656 netexport_clear(struct netexport *ne)
657 {
658 	struct radix_node_head *rnh;
659 	struct mount *mp = ne->ne_mount;
660 	int i;
661 
662 	if (mp->mnt_flag & MNT_EXPUBLIC) {
663 		setpublicfs(NULL, NULL, NULL);
664 		mp->mnt_flag &= ~MNT_EXPUBLIC;
665 	}
666 
667 	for (i = 0; i <= AF_MAX; i++) {
668 		if ((rnh = ne->ne_rtable[i]) != NULL) {
669 			rn_walktree(rnh, free_netcred, rnh);
670 			free(rnh, M_RTABLE);
671 			ne->ne_rtable[i] = NULL;
672 		}
673 	}
674 
675 	if ((mp->mnt_flag & MNT_DEFEXPORTED) != 0) {
676 		struct netcred *np = &ne->ne_defexported;
677 
678 		KASSERT(np->netc_anon != NULL);
679 		kauth_cred_free(np->netc_anon);
680 		np->netc_anon = NULL;
681 	} else {
682 		KASSERT(ne->ne_defexported.netc_anon == NULL);
683 	}
684 
685 	mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
686 }
687 
688 /*
689  * Add a new export entry (described by an export_args structure) to the
690  * given file system.
691  */
692 static int
693 export(struct netexport *nep, const struct export_args *argp)
694 {
695 	struct mount *mp = nep->ne_mount;
696 	int error;
697 
698 	if (argp->ex_flags & MNT_EXPORTED) {
699 		if (argp->ex_flags & MNT_EXPUBLIC) {
700 			if ((error = setpublicfs(mp, nep, argp)) != 0)
701 				return error;
702 			mp->mnt_flag |= MNT_EXPUBLIC;
703 		}
704 		if ((error = hang_addrlist(mp, nep, argp)) != 0)
705 			return error;
706 		mp->mnt_flag |= MNT_EXPORTED;
707 	}
708 	return 0;
709 }
710 
711 /*
712  * Set the publicly exported filesystem (WebNFS).  Currently, only
713  * one public filesystem is possible in the spec (RFC 2054 and 2055)
714  */
715 static int
716 setpublicfs(struct mount *mp, struct netexport *nep,
717     const struct export_args *argp)
718 {
719 	char *cp;
720 	int error;
721 	struct vnode *rvp;
722 	size_t fhsize;
723 
724 	/*
725 	 * mp == NULL -> invalidate the current info, the FS is
726 	 * no longer exported. May be called from either export
727 	 * or unmount, so check if it hasn't already been done.
728 	 */
729 	if (mp == NULL) {
730 		if (nfs_pub.np_valid) {
731 			nfs_pub.np_valid = 0;
732 			if (nfs_pub.np_handle != NULL) {
733 				free(nfs_pub.np_handle, M_TEMP);
734 				nfs_pub.np_handle = NULL;
735 			}
736 			if (nfs_pub.np_index != NULL) {
737 				free(nfs_pub.np_index, M_TEMP);
738 				nfs_pub.np_index = NULL;
739 			}
740 		}
741 		return 0;
742 	}
743 
744 	/*
745 	 * Only one allowed at a time.
746 	 */
747 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
748 		return EBUSY;
749 
750 	/*
751 	 * Get real filehandle for root of exported FS.
752 	 */
753 	if ((error = VFS_ROOT(mp, &rvp)))
754 		return error;
755 
756 	fhsize = 0;
757 	error = vfs_composefh(rvp, NULL, &fhsize);
758 	if (error != E2BIG)
759 		return error;
760 	nfs_pub.np_handle = malloc(fhsize, M_TEMP, M_NOWAIT);
761 	if (nfs_pub.np_handle == NULL)
762 		error = ENOMEM;
763 	else
764 		error = vfs_composefh(rvp, nfs_pub.np_handle, &fhsize);
765 	if (error)
766 		return error;
767 
768 	vput(rvp);
769 
770 	/*
771 	 * If an indexfile was specified, pull it in.
772 	 */
773 	if (argp->ex_indexfile != NULL) {
774 		nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP, M_WAITOK);
775 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
776 		    MAXNAMLEN, (size_t *)0);
777 		if (!error) {
778 			/*
779 			 * Check for illegal filenames.
780 			 */
781 			for (cp = nfs_pub.np_index; *cp; cp++) {
782 				if (*cp == '/') {
783 					error = EINVAL;
784 					break;
785 				}
786 			}
787 		}
788 		if (error) {
789 			free(nfs_pub.np_index, M_TEMP);
790 			return error;
791 		}
792 	}
793 
794 	nfs_pub.np_mount = mp;
795 	nfs_pub.np_valid = 1;
796 	return 0;
797 }
798 
799 /*
800  * Lookup an export entry in the exports list that matches the address
801  * stored in 'nam'.  If no entry is found, the default one is used instead
802  * (if available).
803  */
804 static struct netcred *
805 netcred_lookup(struct netexport *ne, struct mbuf *nam)
806 {
807 	struct netcred *np;
808 	struct radix_node_head *rnh;
809 	struct sockaddr *saddr;
810 
811 	if ((ne->ne_mount->mnt_flag & MNT_EXPORTED) == 0) {
812 		return NULL;
813 	}
814 
815 	/*
816 	 * Lookup in the export list first.
817 	 */
818 	np = NULL;
819 	if (nam != NULL) {
820 		saddr = mtod(nam, struct sockaddr *);
821 		rnh = ne->ne_rtable[saddr->sa_family];
822 		if (rnh != NULL) {
823 			np = (struct netcred *)
824 				(*rnh->rnh_matchaddr)((void *)saddr,
825 						      rnh);
826 			if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
827 				np = NULL;
828 		}
829 	}
830 	/*
831 	 * If no address match, use the default if it exists.
832 	 */
833 	if (np == NULL && ne->ne_mount->mnt_flag & MNT_DEFEXPORTED)
834 		np = &ne->ne_defexported;
835 
836 	return np;
837 }
838 
839 void
840 netexport_rdlock(void)
841 {
842 
843 	rw_enter(&netexport_lock, RW_READER);
844 }
845 
846 void
847 netexport_rdunlock(void)
848 {
849 
850 	rw_exit(&netexport_lock);
851 }
852 
853 static void
854 netexport_wrlock(void)
855 {
856 
857 	rw_enter(&netexport_lock, RW_WRITER);
858 }
859 
860 static void
861 netexport_wrunlock(void)
862 {
863 
864 	rw_exit(&netexport_lock);
865 }
866