2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 #include "opt_inet6.h"
43 #include <sys/param.h>
44 #include <sys/dirent.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/mutex.h>
52 #include <sys/rwlock.h>
53 #include <sys/refcount.h>
54 #include <sys/signalvar.h>
55 #include <sys/socket.h>
56 #include <sys/systm.h>
57 #include <sys/vnode.h>
59 #include <netinet/in.h>
60 #include <net/radix.h>
62 static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure");
64 #if defined(INET) || defined(INET6)
65 static struct radix_node_head *vfs_create_addrlist_af(
66 struct radix_node_head **prnh, int off);
68 static void vfs_free_addrlist(struct netexport *nep);
69 static int vfs_free_netcred(struct radix_node *rn, void *w);
70 static void vfs_free_addrlist_af(struct radix_node_head **prnh);
71 static int vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
72 struct export_args *argp);
73 static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *);
76 * Network address lookup element
79 struct radix_node netc_rnodes[2];
81 struct ucred *netc_anon;
82 int netc_numsecflavors;
83 int netc_secflavors[MAXSECFLAVORS];
87 * Network export information
90 struct netcred ne_defexported; /* Default export */
91 struct radix_node_head *ne4;
92 struct radix_node_head *ne6;
96 * Build hash lists of net addresses and hang them off the mount point.
97 * Called by vfs_export() to set up the lists of export addresses.
100 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
101 struct export_args *argp)
103 register struct netcred *np;
104 register struct radix_node_head *rnh;
106 struct radix_node *rn;
107 struct sockaddr *saddr, *smask = NULL;
108 #if defined(INET6) || defined(INET)
114 * XXX: This routine converts from a `struct xucred'
115 * (argp->ex_anon) to a `struct ucred' (np->netc_anon). This
116 * operation is questionable; for example, what should be done
117 * with fields like cr_uidinfo and cr_prison? Currently, this
118 * routine does not touch them (leaves them as NULL).
120 if (argp->ex_anon.cr_version != XUCRED_VERSION) {
121 vfs_mount_error(mp, "ex_anon.cr_version: %d != %d",
122 argp->ex_anon.cr_version, XUCRED_VERSION);
126 if (argp->ex_addrlen == 0) {
127 if (mp->mnt_flag & MNT_DEFEXPORTED) {
129 "MNT_DEFEXPORTED already set for mount %p", mp);
132 np = &nep->ne_defexported;
133 np->netc_exflags = argp->ex_flags;
134 np->netc_anon = crget();
135 np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
136 crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups,
137 argp->ex_anon.cr_groups);
138 np->netc_anon->cr_prison = &prison0;
139 prison_hold(np->netc_anon->cr_prison);
140 np->netc_numsecflavors = argp->ex_numsecflavors;
141 bcopy(argp->ex_secflavors, np->netc_secflavors,
142 sizeof(np->netc_secflavors));
144 mp->mnt_flag |= MNT_DEFEXPORTED;
150 if (argp->ex_addrlen > MLEN) {
151 vfs_mount_error(mp, "ex_addrlen %d is greater than %d",
152 argp->ex_addrlen, MLEN);
157 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
158 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
159 saddr = (struct sockaddr *) (np + 1);
160 if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
162 if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) {
164 vfs_mount_error(mp, "Invalid saddr->sa_family: %d");
167 if (saddr->sa_len > argp->ex_addrlen)
168 saddr->sa_len = argp->ex_addrlen;
169 if (argp->ex_masklen) {
170 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
171 error = copyin(argp->ex_mask, smask, argp->ex_masklen);
174 if (smask->sa_len > argp->ex_masklen)
175 smask->sa_len = argp->ex_masklen;
178 switch (saddr->sa_family) {
181 if ((rnh = nep->ne4) == NULL) {
182 off = offsetof(struct sockaddr_in, sin_addr) << 3;
183 rnh = vfs_create_addrlist_af(&nep->ne4, off);
189 if ((rnh = nep->ne6) == NULL) {
190 off = offsetof(struct sockaddr_in6, sin6_addr) << 3;
191 rnh = vfs_create_addrlist_af(&nep->ne6, off);
198 vfs_mount_error(mp, "%s %s %d",
199 "Unable to initialize radix node head ",
200 "for address family", saddr->sa_family);
203 RADIX_NODE_HEAD_LOCK(rnh);
204 rn = (*rnh->rnh_addaddr)(saddr, smask, &rnh->rh, np->netc_rnodes);
205 RADIX_NODE_HEAD_UNLOCK(rnh);
206 if (rn == NULL || np != (struct netcred *)rn) { /* already exists */
209 "netcred already exists for given addr/mask");
212 np->netc_exflags = argp->ex_flags;
213 np->netc_anon = crget();
214 np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
215 crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups,
216 argp->ex_anon.cr_groups);
217 np->netc_anon->cr_prison = &prison0;
218 prison_hold(np->netc_anon->cr_prison);
219 np->netc_numsecflavors = argp->ex_numsecflavors;
220 bcopy(argp->ex_secflavors, np->netc_secflavors,
221 sizeof(np->netc_secflavors));
228 /* Helper for vfs_free_addrlist. */
231 vfs_free_netcred(struct radix_node *rn, void *w)
233 struct radix_node_head *rnh = (struct radix_node_head *) w;
236 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, &rnh->rh);
237 cred = ((struct netcred *)rn)->netc_anon;
244 #if defined(INET) || defined(INET6)
245 static struct radix_node_head *
246 vfs_create_addrlist_af(struct radix_node_head **prnh, int off)
249 if (rn_inithead((void **)prnh, off) == 0)
251 RADIX_NODE_HEAD_LOCK_INIT(*prnh);
257 vfs_free_addrlist_af(struct radix_node_head **prnh)
259 struct radix_node_head *rnh;
262 RADIX_NODE_HEAD_LOCK(rnh);
263 (*rnh->rnh_walktree)(&rnh->rh, vfs_free_netcred, rnh);
264 RADIX_NODE_HEAD_UNLOCK(rnh);
265 RADIX_NODE_HEAD_DESTROY(rnh);
266 rn_detachhead((void **)prnh);
271 * Free the net address hash lists that are hanging off the mount points.
274 vfs_free_addrlist(struct netexport *nep)
278 if (nep->ne4 != NULL)
279 vfs_free_addrlist_af(&nep->ne4);
280 if (nep->ne6 != NULL)
281 vfs_free_addrlist_af(&nep->ne6);
283 cred = nep->ne_defexported.netc_anon;
290 * High level function to manipulate export options on a mount point
291 * and the passed in netexport.
292 * Struct export_args *argp is the variable used to twiddle options,
293 * the structure is described in sys/mount.h
296 vfs_export(struct mount *mp, struct export_args *argp)
298 struct netexport *nep;
301 if (argp->ex_numsecflavors < 0
302 || argp->ex_numsecflavors >= MAXSECFLAVORS)
306 lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL);
307 nep = mp->mnt_export;
308 if (argp->ex_flags & MNT_DELEXPORT) {
313 if (mp->mnt_flag & MNT_EXPUBLIC) {
314 vfs_setpublicfs(NULL, NULL, NULL);
316 mp->mnt_flag &= ~MNT_EXPUBLIC;
319 vfs_free_addrlist(nep);
320 mp->mnt_export = NULL;
324 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
327 if (argp->ex_flags & MNT_EXPORTED) {
329 nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
330 mp->mnt_export = nep;
332 if (argp->ex_flags & MNT_EXPUBLIC) {
333 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
336 mp->mnt_flag |= MNT_EXPUBLIC;
339 if ((error = vfs_hang_addrlist(mp, nep, argp)))
342 mp->mnt_flag |= MNT_EXPORTED;
347 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
349 * Once we have executed the vfs_export() command, we do
350 * not want to keep the "export" option around in the
351 * options list, since that will cause subsequent MNT_UPDATE
352 * calls to fail. The export information is saved in
353 * mp->mnt_export, so we can safely delete the "export" mount option
356 vfs_deleteopt(mp->mnt_optnew, "export");
357 vfs_deleteopt(mp->mnt_opt, "export");
362 * Set the publicly exported filesystem (WebNFS). Currently, only
363 * one public filesystem is possible in the spec (RFC 2054 and 2055)
366 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
367 struct export_args *argp)
374 * mp == NULL -> invalidate the current info, the FS is
375 * no longer exported. May be called from either vfs_export
376 * or unmount, so check if it hasn't already been done.
379 if (nfs_pub.np_valid) {
380 nfs_pub.np_valid = 0;
381 if (nfs_pub.np_index != NULL) {
382 free(nfs_pub.np_index, M_TEMP);
383 nfs_pub.np_index = NULL;
390 * Only one allowed at a time.
392 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
396 * Get real filehandle for root of exported FS.
398 bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
399 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
401 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp)))
404 if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
410 * If an indexfile was specified, pull it in.
412 if (argp->ex_indexfile != NULL) {
413 if (nfs_pub.np_index != NULL)
414 nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP,
416 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
417 MAXNAMLEN, (size_t *)0);
420 * Check for illegal filenames.
422 for (cp = nfs_pub.np_index; *cp; cp++) {
430 free(nfs_pub.np_index, M_TEMP);
431 nfs_pub.np_index = NULL;
436 nfs_pub.np_mount = mp;
437 nfs_pub.np_valid = 1;
442 * Used by the filesystems to determine if a given network address
443 * (passed in 'nam') is present in their exports list, returns a pointer
444 * to struct netcred so that the filesystem can examine it for
445 * access rights (read/write/etc).
447 static struct netcred *
448 vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
450 struct netexport *nep;
451 register struct netcred *np;
452 register struct radix_node_head *rnh;
453 struct sockaddr *saddr;
455 nep = mp->mnt_export;
459 if (mp->mnt_flag & MNT_EXPORTED) {
461 * Lookup in the export list first.
466 switch (saddr->sa_family) {
475 RADIX_NODE_HEAD_RLOCK(rnh);
476 np = (struct netcred *)
477 (*rnh->rnh_matchaddr)(saddr, &rnh->rh);
478 RADIX_NODE_HEAD_RUNLOCK(rnh);
479 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
484 * If no address match, use the default if it exists.
486 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
487 np = &nep->ne_defexported;
493 * XXX: This comment comes from the deprecated ufs_check_export()
494 * XXX: and may not entirely apply, but lacking something better:
495 * This is the generic part of fhtovp called after the underlying
496 * filesystem has validated the file handle.
498 * Verify that a host should have access to a filesystem.
502 vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
503 struct ucred **credanonp, int *numsecflavors, int **secflavors)
507 lockmgr(&mp->mnt_explock, LK_SHARED, NULL);
508 np = vfs_export_lookup(mp, nam);
510 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
514 *extflagsp = np->netc_exflags;
515 if ((*credanonp = np->netc_anon) != NULL)
518 *numsecflavors = np->netc_numsecflavors;
520 *secflavors = np->netc_secflavors;
521 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);