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