1 /*- 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. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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 * 4. 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. 21 * 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 32 * SUCH DAMAGE. 33 * 34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include <sys/param.h> 41 #include <sys/dirent.h> 42 #include <sys/domain.h> 43 #include <sys/kernel.h> 44 #include <sys/lock.h> 45 #include <sys/malloc.h> 46 #include <sys/mbuf.h> 47 #include <sys/mount.h> 48 #include <sys/mutex.h> 49 #include <sys/refcount.h> 50 #include <sys/socket.h> 51 #include <sys/systm.h> 52 #include <sys/vnode.h> 53 54 #include <net/radix.h> 55 56 static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure"); 57 58 static void vfs_free_addrlist(struct netexport *nep); 59 static int vfs_free_netcred(struct radix_node *rn, void *w); 60 static int vfs_hang_addrlist(struct mount *mp, struct netexport *nep, 61 struct export_args *argp); 62 static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *); 63 64 /* 65 * Network address lookup element 66 */ 67 struct netcred { 68 struct radix_node netc_rnodes[2]; 69 int netc_exflags; 70 struct ucred netc_anon; 71 }; 72 73 /* 74 * Network export information 75 */ 76 struct netexport { 77 struct netcred ne_defexported; /* Default export */ 78 struct radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */ 79 }; 80 81 /* 82 * Build hash lists of net addresses and hang them off the mount point. 83 * Called by ufs_mount() to set up the lists of export addresses. 84 */ 85 static int 86 vfs_hang_addrlist(struct mount *mp, struct netexport *nep, 87 struct export_args *argp) 88 { 89 register struct netcred *np; 90 register struct radix_node_head *rnh; 91 register int i; 92 struct radix_node *rn; 93 struct sockaddr *saddr, *smask = 0; 94 struct domain *dom; 95 int error; 96 97 /* 98 * XXX: This routine converts from a `struct xucred' 99 * (argp->ex_anon) to a `struct ucred' (np->netc_anon). This 100 * operation is questionable; for example, what should be done 101 * with fields like cr_uidinfo and cr_prison? Currently, this 102 * routine does not touch them (leaves them as NULL). 103 */ 104 if (argp->ex_anon.cr_version != XUCRED_VERSION) 105 return (EINVAL); 106 107 if (argp->ex_addrlen == 0) { 108 if (mp->mnt_flag & MNT_DEFEXPORTED) 109 return (EPERM); 110 np = &nep->ne_defexported; 111 np->netc_exflags = argp->ex_flags; 112 bzero(&np->netc_anon, sizeof(np->netc_anon)); 113 np->netc_anon.cr_uid = argp->ex_anon.cr_uid; 114 np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups; 115 bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups, 116 sizeof(np->netc_anon.cr_groups)); 117 refcount_init(&np->netc_anon.cr_ref, 1); 118 MNT_ILOCK(mp); 119 mp->mnt_flag |= MNT_DEFEXPORTED; 120 MNT_IUNLOCK(mp); 121 return (0); 122 } 123 124 #if MSIZE <= 256 125 if (argp->ex_addrlen > MLEN) { 126 vfs_mount_error(mp, "ex_addrlen %d is greater than %d", 127 argp->ex_addrlen, MLEN); 128 return (EINVAL); 129 } 130 #endif 131 132 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; 133 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO); 134 saddr = (struct sockaddr *) (np + 1); 135 if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen))) 136 goto out; 137 if (saddr->sa_family > AF_MAX) { 138 error = EINVAL; 139 goto out; 140 } 141 if (saddr->sa_len > argp->ex_addrlen) 142 saddr->sa_len = argp->ex_addrlen; 143 if (argp->ex_masklen) { 144 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen); 145 error = copyin(argp->ex_mask, smask, argp->ex_masklen); 146 if (error) 147 goto out; 148 if (smask->sa_len > argp->ex_masklen) 149 smask->sa_len = argp->ex_masklen; 150 } 151 i = saddr->sa_family; 152 if ((rnh = nep->ne_rtable[i]) == NULL) { 153 /* 154 * Seems silly to initialize every AF when most are not used, 155 * do so on demand here 156 */ 157 for (dom = domains; dom; dom = dom->dom_next) 158 if (dom->dom_family == i && dom->dom_rtattach) { 159 dom->dom_rtattach((void **) &nep->ne_rtable[i], 160 dom->dom_rtoffset); 161 break; 162 } 163 if ((rnh = nep->ne_rtable[i]) == NULL) { 164 error = ENOBUFS; 165 vfs_mount_error(mp, 166 "Unable to initialize radix node head"); 167 goto out; 168 } 169 } 170 RADIX_NODE_HEAD_LOCK(rnh); 171 rn = (*rnh->rnh_addaddr)(saddr, smask, rnh, np->netc_rnodes); 172 RADIX_NODE_HEAD_UNLOCK(rnh); 173 if (rn == NULL || np != (struct netcred *)rn) { /* already exists */ 174 error = EPERM; 175 vfs_mount_error(mp, "Invalid radix node head, rn: %p %p", 176 rn, np); 177 goto out; 178 } 179 np->netc_exflags = argp->ex_flags; 180 bzero(&np->netc_anon, sizeof(np->netc_anon)); 181 np->netc_anon.cr_uid = argp->ex_anon.cr_uid; 182 np->netc_anon.cr_ngroups = argp->ex_anon.cr_ngroups; 183 bcopy(argp->ex_anon.cr_groups, np->netc_anon.cr_groups, 184 sizeof(np->netc_anon.cr_groups)); 185 refcount_init(&np->netc_anon.cr_ref, 1); 186 return (0); 187 out: 188 free(np, M_NETADDR); 189 return (error); 190 } 191 192 /* Helper for vfs_free_addrlist. */ 193 /* ARGSUSED */ 194 static int 195 vfs_free_netcred(struct radix_node *rn, void *w) 196 { 197 register struct radix_node_head *rnh = (struct radix_node_head *) w; 198 199 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh); 200 free(rn, M_NETADDR); 201 return (0); 202 } 203 204 /* 205 * Free the net address hash lists that are hanging off the mount points. 206 */ 207 static void 208 vfs_free_addrlist(struct netexport *nep) 209 { 210 register int i; 211 register struct radix_node_head *rnh; 212 213 for (i = 0; i <= AF_MAX; i++) 214 if ((rnh = nep->ne_rtable[i])) { 215 RADIX_NODE_HEAD_LOCK(rnh); 216 (*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh); 217 RADIX_NODE_HEAD_DESTROY(rnh); 218 free(rnh, M_RTABLE); 219 nep->ne_rtable[i] = NULL; /* not SMP safe XXX */ 220 } 221 } 222 223 /* 224 * High level function to manipulate export options on a mount point 225 * and the passed in netexport. 226 * Struct export_args *argp is the variable used to twiddle options, 227 * the structure is described in sys/mount.h 228 */ 229 int 230 vfs_export(struct mount *mp, struct export_args *argp) 231 { 232 struct netexport *nep; 233 int error; 234 235 nep = mp->mnt_export; 236 if (argp->ex_flags & MNT_DELEXPORT) { 237 if (nep == NULL) { 238 vfs_deleteopt(mp->mnt_optnew, "export"); 239 return (ENOENT); 240 } 241 if (mp->mnt_flag & MNT_EXPUBLIC) { 242 vfs_setpublicfs(NULL, NULL, NULL); 243 MNT_ILOCK(mp); 244 mp->mnt_flag &= ~MNT_EXPUBLIC; 245 MNT_IUNLOCK(mp); 246 } 247 vfs_free_addrlist(nep); 248 mp->mnt_export = NULL; 249 free(nep, M_MOUNT); 250 nep = NULL; 251 MNT_ILOCK(mp); 252 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 253 MNT_IUNLOCK(mp); 254 vfs_deleteopt(mp->mnt_optnew, "export"); 255 } 256 if (argp->ex_flags & MNT_EXPORTED) { 257 if (nep == NULL) { 258 nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO); 259 mp->mnt_export = nep; 260 } 261 if (argp->ex_flags & MNT_EXPUBLIC) { 262 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0) 263 return (error); 264 MNT_ILOCK(mp); 265 mp->mnt_flag |= MNT_EXPUBLIC; 266 MNT_IUNLOCK(mp); 267 } 268 if ((error = vfs_hang_addrlist(mp, nep, argp))) 269 return (error); 270 MNT_ILOCK(mp); 271 mp->mnt_flag |= MNT_EXPORTED; 272 MNT_IUNLOCK(mp); 273 } 274 return (0); 275 } 276 277 /* 278 * Set the publicly exported filesystem (WebNFS). Currently, only 279 * one public filesystem is possible in the spec (RFC 2054 and 2055) 280 */ 281 int 282 vfs_setpublicfs(struct mount *mp, struct netexport *nep, 283 struct export_args *argp) 284 { 285 int error; 286 struct vnode *rvp; 287 char *cp; 288 289 /* 290 * mp == NULL -> invalidate the current info, the FS is 291 * no longer exported. May be called from either vfs_export 292 * or unmount, so check if it hasn't already been done. 293 */ 294 if (mp == NULL) { 295 if (nfs_pub.np_valid) { 296 nfs_pub.np_valid = 0; 297 if (nfs_pub.np_index != NULL) { 298 FREE(nfs_pub.np_index, M_TEMP); 299 nfs_pub.np_index = NULL; 300 } 301 } 302 return (0); 303 } 304 305 /* 306 * Only one allowed at a time. 307 */ 308 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount) 309 return (EBUSY); 310 311 /* 312 * Get real filehandle for root of exported FS. 313 */ 314 bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle)); 315 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid; 316 317 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp, curthread /* XXX */))) 318 return (error); 319 320 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid))) 321 return (error); 322 323 vput(rvp); 324 325 /* 326 * If an indexfile was specified, pull it in. 327 */ 328 if (argp->ex_indexfile != NULL) { 329 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP, 330 M_WAITOK); 331 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index, 332 MAXNAMLEN, (size_t *)0); 333 if (!error) { 334 /* 335 * Check for illegal filenames. 336 */ 337 for (cp = nfs_pub.np_index; *cp; cp++) { 338 if (*cp == '/') { 339 error = EINVAL; 340 break; 341 } 342 } 343 } 344 if (error) { 345 FREE(nfs_pub.np_index, M_TEMP); 346 return (error); 347 } 348 } 349 350 nfs_pub.np_mount = mp; 351 nfs_pub.np_valid = 1; 352 return (0); 353 } 354 355 /* 356 * Used by the filesystems to determine if a given network address 357 * (passed in 'nam') is present in thier exports list, returns a pointer 358 * to struct netcred so that the filesystem can examine it for 359 * access rights (read/write/etc). 360 */ 361 static struct netcred * 362 vfs_export_lookup(struct mount *mp, struct sockaddr *nam) 363 { 364 struct netexport *nep; 365 register struct netcred *np; 366 register struct radix_node_head *rnh; 367 struct sockaddr *saddr; 368 369 nep = mp->mnt_export; 370 if (nep == NULL) 371 return (NULL); 372 np = NULL; 373 if (mp->mnt_flag & MNT_EXPORTED) { 374 /* 375 * Lookup in the export list first. 376 */ 377 if (nam != NULL) { 378 saddr = nam; 379 rnh = nep->ne_rtable[saddr->sa_family]; 380 if (rnh != NULL) { 381 RADIX_NODE_HEAD_LOCK(rnh); 382 np = (struct netcred *) 383 (*rnh->rnh_matchaddr)(saddr, rnh); 384 RADIX_NODE_HEAD_UNLOCK(rnh); 385 if (np && np->netc_rnodes->rn_flags & RNF_ROOT) 386 np = NULL; 387 } 388 } 389 /* 390 * If no address match, use the default if it exists. 391 */ 392 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) 393 np = &nep->ne_defexported; 394 } 395 return (np); 396 } 397 398 /* 399 * XXX: This comment comes from the deprecated ufs_check_export() 400 * XXX: and may not entirely apply, but lacking something better: 401 * This is the generic part of fhtovp called after the underlying 402 * filesystem has validated the file handle. 403 * 404 * Verify that a host should have access to a filesystem. 405 */ 406 407 int 408 vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp, 409 struct ucred **credanonp) 410 { 411 struct netcred *np; 412 413 np = vfs_export_lookup(mp, nam); 414 if (np == NULL) 415 return (EACCES); 416 *extflagsp = np->netc_exflags; 417 *credanonp = &np->netc_anon; 418 return (0); 419 } 420 421