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 * 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. 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 "opt_inet.h" 41 #include "opt_inet6.h" 42 43 #include <sys/param.h> 44 #include <sys/dirent.h> 45 #include <sys/jail.h> 46 #include <sys/kernel.h> 47 #include <sys/lock.h> 48 #include <sys/malloc.h> 49 #include <sys/mbuf.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> 58 59 #include <netinet/in.h> 60 #include <net/radix.h> 61 62 static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure"); 63 64 #if defined(INET) || defined(INET6) 65 static struct radix_node_head *vfs_create_addrlist_af( 66 struct radix_node_head **prnh, int off); 67 #endif 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 *); 74 75 /* 76 * Network address lookup element 77 */ 78 struct netcred { 79 struct radix_node netc_rnodes[2]; 80 int netc_exflags; 81 struct ucred *netc_anon; 82 int netc_numsecflavors; 83 int netc_secflavors[MAXSECFLAVORS]; 84 }; 85 86 /* 87 * Network export information 88 */ 89 struct netexport { 90 struct netcred ne_defexported; /* Default export */ 91 struct radix_node_head *ne4; 92 struct radix_node_head *ne6; 93 }; 94 95 /* 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. 98 */ 99 static int 100 vfs_hang_addrlist(struct mount *mp, struct netexport *nep, 101 struct export_args *argp) 102 { 103 register struct netcred *np; 104 register struct radix_node_head *rnh; 105 register int i; 106 struct radix_node *rn; 107 struct sockaddr *saddr, *smask = NULL; 108 #if defined(INET6) || defined(INET) 109 int off; 110 #endif 111 int error; 112 113 /* 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). 119 */ 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); 123 return (EINVAL); 124 } 125 126 if (argp->ex_addrlen == 0) { 127 if (mp->mnt_flag & MNT_DEFEXPORTED) { 128 vfs_mount_error(mp, 129 "MNT_DEFEXPORTED already set for mount %p", mp); 130 return (EPERM); 131 } 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)); 143 MNT_ILOCK(mp); 144 mp->mnt_flag |= MNT_DEFEXPORTED; 145 MNT_IUNLOCK(mp); 146 return (0); 147 } 148 149 #if MSIZE <= 256 150 if (argp->ex_addrlen > MLEN) { 151 vfs_mount_error(mp, "ex_addrlen %d is greater than %d", 152 argp->ex_addrlen, MLEN); 153 return (EINVAL); 154 } 155 #endif 156 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))) 161 goto out; 162 if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) { 163 error = EINVAL; 164 vfs_mount_error(mp, "Invalid saddr->sa_family: %d"); 165 goto out; 166 } 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); 172 if (error) 173 goto out; 174 if (smask->sa_len > argp->ex_masklen) 175 smask->sa_len = argp->ex_masklen; 176 } 177 rnh = NULL; 178 switch (saddr->sa_family) { 179 #ifdef INET 180 case AF_INET: 181 if ((rnh = nep->ne4) == NULL) { 182 off = offsetof(struct sockaddr_in, sin_addr) << 3; 183 rnh = vfs_create_addrlist_af(&nep->ne4, off); 184 } 185 break; 186 #endif 187 #ifdef INET6 188 case AF_INET6: 189 if ((rnh = nep->ne6) == NULL) { 190 off = offsetof(struct sockaddr_in6, sin6_addr) << 3; 191 rnh = vfs_create_addrlist_af(&nep->ne6, off); 192 } 193 break; 194 #endif 195 } 196 if (rnh == NULL) { 197 error = ENOBUFS; 198 vfs_mount_error(mp, "%s %s %d", 199 "Unable to initialize radix node head ", 200 "for address family", saddr->sa_family); 201 goto out; 202 } 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 */ 207 error = EPERM; 208 vfs_mount_error(mp, 209 "netcred already exists for given addr/mask"); 210 goto out; 211 } 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)); 222 return (0); 223 out: 224 free(np, M_NETADDR); 225 return (error); 226 } 227 228 /* Helper for vfs_free_addrlist. */ 229 /* ARGSUSED */ 230 static int 231 vfs_free_netcred(struct radix_node *rn, void *w) 232 { 233 struct radix_node_head *rnh = (struct radix_node_head *) w; 234 struct ucred *cred; 235 236 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, &rnh->rh); 237 cred = ((struct netcred *)rn)->netc_anon; 238 if (cred != NULL) 239 crfree(cred); 240 free(rn, M_NETADDR); 241 return (0); 242 } 243 244 #if defined(INET) || defined(INET6) 245 static struct radix_node_head * 246 vfs_create_addrlist_af(struct radix_node_head **prnh, int off) 247 { 248 249 if (rn_inithead((void **)prnh, off) == 0) 250 return (NULL); 251 RADIX_NODE_HEAD_LOCK_INIT(*prnh); 252 return (*prnh); 253 } 254 #endif 255 256 static void 257 vfs_free_addrlist_af(struct radix_node_head **prnh) 258 { 259 struct radix_node_head *rnh; 260 261 rnh = *prnh; 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); 267 prnh = NULL; 268 } 269 270 /* 271 * Free the net address hash lists that are hanging off the mount points. 272 */ 273 static void 274 vfs_free_addrlist(struct netexport *nep) 275 { 276 struct ucred *cred; 277 278 if (nep->ne4 != NULL) 279 vfs_free_addrlist_af(&nep->ne4); 280 if (nep->ne6 != NULL) 281 vfs_free_addrlist_af(&nep->ne6); 282 283 cred = nep->ne_defexported.netc_anon; 284 if (cred != NULL) 285 crfree(cred); 286 287 } 288 289 /* 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 294 */ 295 int 296 vfs_export(struct mount *mp, struct export_args *argp) 297 { 298 struct netexport *nep; 299 int error; 300 301 if (argp->ex_numsecflavors < 0 302 || argp->ex_numsecflavors >= MAXSECFLAVORS) 303 return (EINVAL); 304 305 error = 0; 306 lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL); 307 nep = mp->mnt_export; 308 if (argp->ex_flags & MNT_DELEXPORT) { 309 if (nep == NULL) { 310 error = ENOENT; 311 goto out; 312 } 313 if (mp->mnt_flag & MNT_EXPUBLIC) { 314 vfs_setpublicfs(NULL, NULL, NULL); 315 MNT_ILOCK(mp); 316 mp->mnt_flag &= ~MNT_EXPUBLIC; 317 MNT_IUNLOCK(mp); 318 } 319 vfs_free_addrlist(nep); 320 mp->mnt_export = NULL; 321 free(nep, M_MOUNT); 322 nep = NULL; 323 MNT_ILOCK(mp); 324 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 325 MNT_IUNLOCK(mp); 326 } 327 if (argp->ex_flags & MNT_EXPORTED) { 328 if (nep == NULL) { 329 nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO); 330 mp->mnt_export = nep; 331 } 332 if (argp->ex_flags & MNT_EXPUBLIC) { 333 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0) 334 goto out; 335 MNT_ILOCK(mp); 336 mp->mnt_flag |= MNT_EXPUBLIC; 337 MNT_IUNLOCK(mp); 338 } 339 if ((error = vfs_hang_addrlist(mp, nep, argp))) 340 goto out; 341 MNT_ILOCK(mp); 342 mp->mnt_flag |= MNT_EXPORTED; 343 MNT_IUNLOCK(mp); 344 } 345 346 out: 347 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL); 348 /* 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 354 * here. 355 */ 356 vfs_deleteopt(mp->mnt_optnew, "export"); 357 vfs_deleteopt(mp->mnt_opt, "export"); 358 return (error); 359 } 360 361 /* 362 * Set the publicly exported filesystem (WebNFS). Currently, only 363 * one public filesystem is possible in the spec (RFC 2054 and 2055) 364 */ 365 int 366 vfs_setpublicfs(struct mount *mp, struct netexport *nep, 367 struct export_args *argp) 368 { 369 int error; 370 struct vnode *rvp; 371 char *cp; 372 373 /* 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. 377 */ 378 if (mp == NULL) { 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; 384 } 385 } 386 return (0); 387 } 388 389 /* 390 * Only one allowed at a time. 391 */ 392 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount) 393 return (EBUSY); 394 395 /* 396 * Get real filehandle for root of exported FS. 397 */ 398 bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle)); 399 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid; 400 401 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp))) 402 return (error); 403 404 if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid))) 405 return (error); 406 407 vput(rvp); 408 409 /* 410 * If an indexfile was specified, pull it in. 411 */ 412 if (argp->ex_indexfile != NULL) { 413 if (nfs_pub.np_index != NULL) 414 nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP, 415 M_WAITOK); 416 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index, 417 MAXNAMLEN, (size_t *)0); 418 if (!error) { 419 /* 420 * Check for illegal filenames. 421 */ 422 for (cp = nfs_pub.np_index; *cp; cp++) { 423 if (*cp == '/') { 424 error = EINVAL; 425 break; 426 } 427 } 428 } 429 if (error) { 430 free(nfs_pub.np_index, M_TEMP); 431 nfs_pub.np_index = NULL; 432 return (error); 433 } 434 } 435 436 nfs_pub.np_mount = mp; 437 nfs_pub.np_valid = 1; 438 return (0); 439 } 440 441 /* 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). 446 */ 447 static struct netcred * 448 vfs_export_lookup(struct mount *mp, struct sockaddr *nam) 449 { 450 struct netexport *nep; 451 register struct netcred *np; 452 register struct radix_node_head *rnh; 453 struct sockaddr *saddr; 454 455 nep = mp->mnt_export; 456 if (nep == NULL) 457 return (NULL); 458 np = NULL; 459 if (mp->mnt_flag & MNT_EXPORTED) { 460 /* 461 * Lookup in the export list first. 462 */ 463 if (nam != NULL) { 464 saddr = nam; 465 rnh = NULL; 466 switch (saddr->sa_family) { 467 case AF_INET: 468 rnh = nep->ne4; 469 break; 470 case AF_INET6: 471 rnh = nep->ne6; 472 break; 473 } 474 if (rnh != NULL) { 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) 480 np = NULL; 481 } 482 } 483 /* 484 * If no address match, use the default if it exists. 485 */ 486 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) 487 np = &nep->ne_defexported; 488 } 489 return (np); 490 } 491 492 /* 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. 497 * 498 * Verify that a host should have access to a filesystem. 499 */ 500 501 int 502 vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp, 503 struct ucred **credanonp, int *numsecflavors, int **secflavors) 504 { 505 struct netcred *np; 506 507 lockmgr(&mp->mnt_explock, LK_SHARED, NULL); 508 np = vfs_export_lookup(mp, nam); 509 if (np == NULL) { 510 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL); 511 *credanonp = NULL; 512 return (EACCES); 513 } 514 *extflagsp = np->netc_exflags; 515 if ((*credanonp = np->netc_anon) != NULL) 516 crhold(*credanonp); 517 if (numsecflavors) 518 *numsecflavors = np->netc_numsecflavors; 519 if (secflavors) 520 *secflavors = np->netc_secflavors; 521 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL); 522 return (0); 523 } 524 525