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