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