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

Cache object: 99074ce52db8bb4a9a143c4601d23d2b


[ 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.