The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_export.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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: releng/9.0/sys/kern/vfs_export.c 197581 2009-09-28 18:07:16Z jamie $");
   39 
   40 #include <sys/param.h>
   41 #include <sys/dirent.h>
   42 #include <sys/domain.h>
   43 #include <sys/jail.h>
   44 #include <sys/kernel.h>
   45 #include <sys/lock.h>
   46 #include <sys/malloc.h>
   47 #include <sys/mbuf.h>
   48 #include <sys/mount.h>
   49 #include <sys/mutex.h>
   50 #include <sys/rwlock.h>
   51 #include <sys/refcount.h>
   52 #include <sys/socket.h>
   53 #include <sys/systm.h>
   54 #include <sys/vnode.h>
   55 
   56 #include <net/radix.h>
   57 
   58 static MALLOC_DEFINE(M_NETADDR, "export_host", "Export host address structure");
   59 
   60 static void     vfs_free_addrlist(struct netexport *nep);
   61 static int      vfs_free_netcred(struct radix_node *rn, void *w);
   62 static int      vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
   63                     struct export_args *argp);
   64 static struct netcred *vfs_export_lookup(struct mount *, struct sockaddr *);
   65 
   66 /*
   67  * Network address lookup element
   68  */
   69 struct netcred {
   70         struct  radix_node netc_rnodes[2];
   71         int     netc_exflags;
   72         struct  ucred *netc_anon;
   73         int     netc_numsecflavors;
   74         int     netc_secflavors[MAXSECFLAVORS];
   75 };
   76 
   77 /*
   78  * Network export information
   79  */
   80 struct netexport {
   81         struct  netcred ne_defexported;               /* Default export */
   82         struct  radix_node_head *ne_rtable[AF_MAX+1]; /* Individual exports */
   83 };
   84 
   85 /*
   86  * Build hash lists of net addresses and hang them off the mount point.
   87  * Called by vfs_export() to set up the lists of export addresses.
   88  */
   89 static int
   90 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
   91     struct export_args *argp)
   92 {
   93         register struct netcred *np;
   94         register struct radix_node_head *rnh;
   95         register int i;
   96         struct radix_node *rn;
   97         struct sockaddr *saddr, *smask = 0;
   98         struct domain *dom;
   99         int error;
  100 
  101         /*
  102          * XXX: This routine converts from a `struct xucred'
  103          * (argp->ex_anon) to a `struct ucred' (np->netc_anon).  This
  104          * operation is questionable; for example, what should be done
  105          * with fields like cr_uidinfo and cr_prison?  Currently, this
  106          * routine does not touch them (leaves them as NULL).
  107          */
  108         if (argp->ex_anon.cr_version != XUCRED_VERSION) {
  109                 vfs_mount_error(mp, "ex_anon.cr_version: %d != %d",
  110                     argp->ex_anon.cr_version, XUCRED_VERSION);
  111                 return (EINVAL);
  112         }
  113 
  114         if (argp->ex_addrlen == 0) {
  115                 if (mp->mnt_flag & MNT_DEFEXPORTED) {
  116                         vfs_mount_error(mp,
  117                             "MNT_DEFEXPORTED already set for mount %p", mp);
  118                         return (EPERM);
  119                 }
  120                 np = &nep->ne_defexported;
  121                 np->netc_exflags = argp->ex_flags;
  122                 np->netc_anon = crget();
  123                 np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
  124                 crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups,
  125                     argp->ex_anon.cr_groups);
  126                 np->netc_anon->cr_prison = &prison0;
  127                 prison_hold(np->netc_anon->cr_prison);
  128                 np->netc_numsecflavors = argp->ex_numsecflavors;
  129                 bcopy(argp->ex_secflavors, np->netc_secflavors,
  130                     sizeof(np->netc_secflavors));
  131                 MNT_ILOCK(mp);
  132                 mp->mnt_flag |= MNT_DEFEXPORTED;
  133                 MNT_IUNLOCK(mp);
  134                 return (0);
  135         }
  136 
  137 #if MSIZE <= 256
  138         if (argp->ex_addrlen > MLEN) {
  139                 vfs_mount_error(mp, "ex_addrlen %d is greater than %d",
  140                     argp->ex_addrlen, MLEN);
  141                 return (EINVAL);
  142         }
  143 #endif
  144 
  145         i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
  146         np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK | M_ZERO);
  147         saddr = (struct sockaddr *) (np + 1);
  148         if ((error = copyin(argp->ex_addr, saddr, argp->ex_addrlen)))
  149                 goto out;
  150         if (saddr->sa_family == AF_UNSPEC || saddr->sa_family > AF_MAX) {
  151                 error = EINVAL;
  152                 vfs_mount_error(mp, "Invalid saddr->sa_family: %d");
  153                 goto out;
  154         }
  155         if (saddr->sa_len > argp->ex_addrlen)
  156                 saddr->sa_len = argp->ex_addrlen;
  157         if (argp->ex_masklen) {
  158                 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
  159                 error = copyin(argp->ex_mask, smask, argp->ex_masklen);
  160                 if (error)
  161                         goto out;
  162                 if (smask->sa_len > argp->ex_masklen)
  163                         smask->sa_len = argp->ex_masklen;
  164         }
  165         i = saddr->sa_family;
  166         if ((rnh = nep->ne_rtable[i]) == NULL) {
  167                 /*
  168                  * Seems silly to initialize every AF when most are not used,
  169                  * do so on demand here
  170                  */
  171                 for (dom = domains; dom; dom = dom->dom_next) {
  172                         KASSERT(((i == AF_INET) || (i == AF_INET6)), 
  173                             ("unexpected protocol in vfs_hang_addrlist"));
  174                         if (dom->dom_family == i && dom->dom_rtattach) {
  175                                 /*
  176                                  * XXX MRT 
  177                                  * The INET and INET6 domains know the
  178                                  * offset already. We don't need to send it
  179                                  * So we just use it as a flag to say that
  180                                  * we are or are not setting up a real routing
  181                                  * table. Only IP and IPV6 need have this
  182                                  * be 0 so all other protocols can stay the 
  183                                  * same (ABI compatible).
  184                                  */ 
  185                                 dom->dom_rtattach(
  186                                     (void **) &nep->ne_rtable[i], 0);
  187                                 break;
  188                         }
  189                 }
  190                 if ((rnh = nep->ne_rtable[i]) == NULL) {
  191                         error = ENOBUFS;
  192                         vfs_mount_error(mp, "%s %s %d",
  193                             "Unable to initialize radix node head ",
  194                             "for address family", i);
  195                         goto out;
  196                 }
  197         }
  198         RADIX_NODE_HEAD_LOCK(rnh);
  199         rn = (*rnh->rnh_addaddr)(saddr, smask, rnh, np->netc_rnodes);
  200         RADIX_NODE_HEAD_UNLOCK(rnh);
  201         if (rn == NULL || np != (struct netcred *)rn) { /* already exists */
  202                 error = EPERM;
  203                 vfs_mount_error(mp, "Invalid radix node head, rn: %p %p",
  204                     rn, np);
  205                 goto out;
  206         }
  207         np->netc_exflags = argp->ex_flags;
  208         np->netc_anon = crget();
  209         np->netc_anon->cr_uid = argp->ex_anon.cr_uid;
  210         crsetgroups(np->netc_anon, argp->ex_anon.cr_ngroups,
  211             np->netc_anon->cr_groups);
  212         np->netc_anon->cr_prison = &prison0;
  213         prison_hold(np->netc_anon->cr_prison);
  214         np->netc_numsecflavors = argp->ex_numsecflavors;
  215         bcopy(argp->ex_secflavors, np->netc_secflavors,
  216             sizeof(np->netc_secflavors));
  217         return (0);
  218 out:
  219         free(np, M_NETADDR);
  220         return (error);
  221 }
  222 
  223 /* Helper for vfs_free_addrlist. */
  224 /* ARGSUSED */
  225 static int
  226 vfs_free_netcred(struct radix_node *rn, void *w)
  227 {
  228         struct radix_node_head *rnh = (struct radix_node_head *) w;
  229         struct ucred *cred;
  230 
  231         (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
  232         cred = ((struct netcred *)rn)->netc_anon;
  233         if (cred != NULL)
  234                 crfree(cred);
  235         free(rn, M_NETADDR);
  236         return (0);
  237 }
  238 
  239 /*
  240  * Free the net address hash lists that are hanging off the mount points.
  241  */
  242 static void
  243 vfs_free_addrlist(struct netexport *nep)
  244 {
  245         int i;
  246         struct radix_node_head *rnh;
  247         struct ucred *cred;
  248 
  249         for (i = 0; i <= AF_MAX; i++) {
  250                 if ((rnh = nep->ne_rtable[i])) {
  251                         RADIX_NODE_HEAD_LOCK(rnh);
  252                         (*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
  253                         RADIX_NODE_HEAD_UNLOCK(rnh);
  254                         RADIX_NODE_HEAD_DESTROY(rnh);
  255                         free(rnh, M_RTABLE);
  256                         nep->ne_rtable[i] = NULL;       /* not SMP safe XXX */
  257                 }
  258         }
  259         cred = nep->ne_defexported.netc_anon;
  260         if (cred != NULL)
  261                 crfree(cred);
  262 
  263 }
  264 
  265 /*
  266  * High level function to manipulate export options on a mount point
  267  * and the passed in netexport.
  268  * Struct export_args *argp is the variable used to twiddle options,
  269  * the structure is described in sys/mount.h
  270  */
  271 int
  272 vfs_export(struct mount *mp, struct export_args *argp)
  273 {
  274         struct netexport *nep;
  275         int error;
  276 
  277         if (argp->ex_numsecflavors < 0
  278             || argp->ex_numsecflavors >= MAXSECFLAVORS)
  279                 return (EINVAL);
  280 
  281         error = 0;
  282         lockmgr(&mp->mnt_explock, LK_EXCLUSIVE, NULL);
  283         nep = mp->mnt_export;
  284         if (argp->ex_flags & MNT_DELEXPORT) {
  285                 if (nep == NULL) {
  286                         error = ENOENT;
  287                         goto out;
  288                 }
  289                 if (mp->mnt_flag & MNT_EXPUBLIC) {
  290                         vfs_setpublicfs(NULL, NULL, NULL);
  291                         MNT_ILOCK(mp);
  292                         mp->mnt_flag &= ~MNT_EXPUBLIC;
  293                         MNT_IUNLOCK(mp);
  294                 }
  295                 vfs_free_addrlist(nep);
  296                 mp->mnt_export = NULL;
  297                 free(nep, M_MOUNT);
  298                 nep = NULL;
  299                 MNT_ILOCK(mp);
  300                 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
  301                 MNT_IUNLOCK(mp);
  302         }
  303         if (argp->ex_flags & MNT_EXPORTED) {
  304                 if (nep == NULL) {
  305                         nep = malloc(sizeof(struct netexport), M_MOUNT, M_WAITOK | M_ZERO);
  306                         mp->mnt_export = nep;
  307                 }
  308                 if (argp->ex_flags & MNT_EXPUBLIC) {
  309                         if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
  310                                 goto out;
  311                         MNT_ILOCK(mp);
  312                         mp->mnt_flag |= MNT_EXPUBLIC;
  313                         MNT_IUNLOCK(mp);
  314                 }
  315                 if ((error = vfs_hang_addrlist(mp, nep, argp)))
  316                         goto out;
  317                 MNT_ILOCK(mp);
  318                 mp->mnt_flag |= MNT_EXPORTED;
  319                 MNT_IUNLOCK(mp);
  320         }
  321 
  322 out:
  323         lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
  324         /*
  325          * Once we have executed the vfs_export() command, we do
  326          * not want to keep the "export" option around in the
  327          * options list, since that will cause subsequent MNT_UPDATE
  328          * calls to fail.  The export information is saved in
  329          * mp->mnt_export, so we can safely delete the "export" mount option
  330          * here.
  331          */
  332         vfs_deleteopt(mp->mnt_optnew, "export");
  333         vfs_deleteopt(mp->mnt_opt, "export");
  334         return (error);
  335 }
  336 
  337 /*
  338  * Set the publicly exported filesystem (WebNFS). Currently, only
  339  * one public filesystem is possible in the spec (RFC 2054 and 2055)
  340  */
  341 int
  342 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
  343     struct export_args *argp)
  344 {
  345         int error;
  346         struct vnode *rvp;
  347         char *cp;
  348 
  349         /*
  350          * mp == NULL -> invalidate the current info, the FS is
  351          * no longer exported. May be called from either vfs_export
  352          * or unmount, so check if it hasn't already been done.
  353          */
  354         if (mp == NULL) {
  355                 if (nfs_pub.np_valid) {
  356                         nfs_pub.np_valid = 0;
  357                         if (nfs_pub.np_index != NULL) {
  358                                 free(nfs_pub.np_index, M_TEMP);
  359                                 nfs_pub.np_index = NULL;
  360                         }
  361                 }
  362                 return (0);
  363         }
  364 
  365         /*
  366          * Only one allowed at a time.
  367          */
  368         if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
  369                 return (EBUSY);
  370 
  371         /*
  372          * Get real filehandle for root of exported FS.
  373          */
  374         bzero(&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
  375         nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
  376 
  377         if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rvp)))
  378                 return (error);
  379 
  380         if ((error = VOP_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
  381                 return (error);
  382 
  383         vput(rvp);
  384 
  385         /*
  386          * If an indexfile was specified, pull it in.
  387          */
  388         if (argp->ex_indexfile != NULL) {
  389                 if (nfs_pub.np_index != NULL)
  390                         nfs_pub.np_index = malloc(MAXNAMLEN + 1, M_TEMP,
  391                             M_WAITOK);
  392                 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
  393                     MAXNAMLEN, (size_t *)0);
  394                 if (!error) {
  395                         /*
  396                          * Check for illegal filenames.
  397                          */
  398                         for (cp = nfs_pub.np_index; *cp; cp++) {
  399                                 if (*cp == '/') {
  400                                         error = EINVAL;
  401                                         break;
  402                                 }
  403                         }
  404                 }
  405                 if (error) {
  406                         free(nfs_pub.np_index, M_TEMP);
  407                         nfs_pub.np_index = NULL;
  408                         return (error);
  409                 }
  410         }
  411 
  412         nfs_pub.np_mount = mp;
  413         nfs_pub.np_valid = 1;
  414         return (0);
  415 }
  416 
  417 /*
  418  * Used by the filesystems to determine if a given network address
  419  * (passed in 'nam') is present in their exports list, returns a pointer
  420  * to struct netcred so that the filesystem can examine it for
  421  * access rights (read/write/etc).
  422  */
  423 static struct netcred *
  424 vfs_export_lookup(struct mount *mp, struct sockaddr *nam)
  425 {
  426         struct netexport *nep;
  427         register struct netcred *np;
  428         register struct radix_node_head *rnh;
  429         struct sockaddr *saddr;
  430 
  431         nep = mp->mnt_export;
  432         if (nep == NULL)
  433                 return (NULL);
  434         np = NULL;
  435         if (mp->mnt_flag & MNT_EXPORTED) {
  436                 /*
  437                  * Lookup in the export list first.
  438                  */
  439                 if (nam != NULL) {
  440                         saddr = nam;
  441                         rnh = nep->ne_rtable[saddr->sa_family];
  442                         if (rnh != NULL) {
  443                                 RADIX_NODE_HEAD_RLOCK(rnh);
  444                                 np = (struct netcred *)
  445                                     (*rnh->rnh_matchaddr)(saddr, rnh);
  446                                 RADIX_NODE_HEAD_RUNLOCK(rnh);
  447                                 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
  448                                         np = NULL;
  449                         }
  450                 }
  451                 /*
  452                  * If no address match, use the default if it exists.
  453                  */
  454                 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
  455                         np = &nep->ne_defexported;
  456         }
  457         return (np);
  458 }
  459 
  460 /*
  461  * XXX: This comment comes from the deprecated ufs_check_export()
  462  * XXX: and may not entirely apply, but lacking something better:
  463  * This is the generic part of fhtovp called after the underlying
  464  * filesystem has validated the file handle.
  465  *
  466  * Verify that a host should have access to a filesystem.
  467  */
  468 
  469 int 
  470 vfs_stdcheckexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
  471     struct ucred **credanonp, int *numsecflavors, int **secflavors)
  472 {
  473         struct netcred *np;
  474 
  475         lockmgr(&mp->mnt_explock, LK_SHARED, NULL);
  476         np = vfs_export_lookup(mp, nam);
  477         if (np == NULL) {
  478                 lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
  479                 *credanonp = NULL;
  480                 return (EACCES);
  481         }
  482         *extflagsp = np->netc_exflags;
  483         if ((*credanonp = np->netc_anon) != NULL)
  484                 crhold(*credanonp);
  485         if (numsecflavors)
  486                 *numsecflavors = np->netc_numsecflavors;
  487         if (secflavors)
  488                 *secflavors = np->netc_secflavors;
  489         lockmgr(&mp->mnt_explock, LK_RELEASE, NULL);
  490         return (0);
  491 }
  492 

Cache object: 48855e0dad51692ff3880b9a8dbfa662


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.