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