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/fs/nfsserver/nfs_nfsdkrpc.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  *
    5  * This code is derived from software contributed to Berkeley by
    6  * Rick Macklem at The University of Guelph.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 4. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __FBSDID("$FreeBSD: releng/8.4/sys/fs/nfsserver/nfs_nfsdkrpc.c 247503 2013-02-28 21:58:07Z jhb $");
   36 
   37 #include "opt_inet6.h"
   38 #include "opt_kgssapi.h"
   39 
   40 #include <fs/nfs/nfsport.h>
   41 
   42 #include <rpc/rpc.h>
   43 #include <rpc/rpcsec_gss.h>
   44 
   45 #include <security/mac/mac_framework.h>
   46 
   47 NFSDLOCKMUTEX;
   48 
   49 /*
   50  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
   51  */
   52 static int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
   53         NFSPROC_NULL,
   54         NFSPROC_GETATTR,
   55         NFSPROC_SETATTR,
   56         NFSPROC_NOOP,
   57         NFSPROC_LOOKUP,
   58         NFSPROC_READLINK,
   59         NFSPROC_READ,
   60         NFSPROC_NOOP,
   61         NFSPROC_WRITE,
   62         NFSPROC_CREATE,
   63         NFSPROC_REMOVE,
   64         NFSPROC_RENAME,
   65         NFSPROC_LINK,
   66         NFSPROC_SYMLINK,
   67         NFSPROC_MKDIR,
   68         NFSPROC_RMDIR,
   69         NFSPROC_READDIR,
   70         NFSPROC_FSSTAT,
   71         NFSPROC_NOOP,
   72         NFSPROC_NOOP,
   73         NFSPROC_NOOP,
   74         NFSPROC_NOOP,
   75 };
   76 
   77 
   78 SYSCTL_DECL(_vfs_newnfs);
   79 
   80 SVCPOOL         *nfsrvd_pool;
   81 
   82 static int      nfs_privport = 0;
   83 SYSCTL_INT(_vfs_newnfs, OID_AUTO, nfs_privport, CTLFLAG_RW,
   84     &nfs_privport, 0,
   85     "Only allow clients using a privileged port for NFSv2 and 3");
   86 
   87 static int      nfs_minvers = NFS_VER2;
   88 SYSCTL_INT(_vfs_newnfs, OID_AUTO, server_min_nfsvers, CTLFLAG_RW,
   89     &nfs_minvers, 0, "The lowest version of NFS handled by the server");
   90 
   91 static int      nfs_maxvers = NFS_VER4;
   92 SYSCTL_INT(_vfs_newnfs, OID_AUTO, server_max_nfsvers, CTLFLAG_RW,
   93     &nfs_maxvers, 0, "The highest version of NFS handled by the server");
   94 
   95 static int nfs_proc(struct nfsrv_descript *, u_int32_t, struct socket *,
   96     u_int64_t, struct nfsrvcache **);
   97 
   98 extern u_long sb_max_adj;
   99 extern int newnfs_numnfsd;
  100 extern struct proc *nfsd_master_proc;
  101 
  102 /*
  103  * NFS server system calls
  104  */
  105 
  106 static void
  107 nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
  108 {
  109         struct nfsrv_descript nd;
  110         struct nfsrvcache *rp = NULL;
  111         int cacherep, credflavor;
  112 
  113         memset(&nd, 0, sizeof(nd));
  114         if (rqst->rq_vers == NFS_VER2) {
  115                 if (rqst->rq_proc > NFSV2PROC_STATFS) {
  116                         svcerr_noproc(rqst);
  117                         svc_freereq(rqst);
  118                         goto out;
  119                 }
  120                 nd.nd_procnum = newnfs_nfsv3_procid[rqst->rq_proc];
  121                 nd.nd_flag = ND_NFSV2;
  122         } else if (rqst->rq_vers == NFS_VER3) {
  123                 if (rqst->rq_proc >= NFS_V3NPROCS) {
  124                         svcerr_noproc(rqst);
  125                         svc_freereq(rqst);
  126                         goto out;
  127                 }
  128                 nd.nd_procnum = rqst->rq_proc;
  129                 nd.nd_flag = ND_NFSV3;
  130         } else {
  131                 if (rqst->rq_proc != NFSPROC_NULL &&
  132                     rqst->rq_proc != NFSV4PROC_COMPOUND) {
  133                         svcerr_noproc(rqst);
  134                         svc_freereq(rqst);
  135                         goto out;
  136                 }
  137                 nd.nd_procnum = rqst->rq_proc;
  138                 nd.nd_flag = ND_NFSV4;
  139         }
  140 
  141         /*
  142          * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
  143          * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
  144          * mounts.
  145          */
  146         nd.nd_mrep = rqst->rq_args;
  147         rqst->rq_args = NULL;
  148         newnfs_realign(&nd.nd_mrep);
  149         nd.nd_md = nd.nd_mrep;
  150         nd.nd_dpos = mtod(nd.nd_md, caddr_t);
  151         nd.nd_nam = svc_getrpccaller(rqst);
  152         nd.nd_nam2 = rqst->rq_addr;
  153         nd.nd_mreq = NULL;
  154         nd.nd_cred = NULL;
  155 
  156         if (nfs_privport && (nd.nd_flag & ND_NFSV4) == 0) {
  157                 /* Check if source port is privileged */
  158                 u_short port;
  159                 struct sockaddr *nam = nd.nd_nam;
  160                 struct sockaddr_in *sin;
  161 
  162                 sin = (struct sockaddr_in *)nam;
  163                 /*
  164                  * INET/INET6 - same code:
  165                  *    sin_port and sin6_port are at same offset
  166                  */
  167                 port = ntohs(sin->sin_port);
  168                 if (port >= IPPORT_RESERVED &&
  169                     nd.nd_procnum != NFSPROC_NULL) {
  170 #ifdef INET6
  171                         char b6[INET6_ADDRSTRLEN];
  172 #if defined(KLD_MODULE)
  173                         /* Do not use ip6_sprintf: the nfs module should work without INET6. */
  174 #define ip6_sprintf(buf, a)                                             \
  175                         (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x",      \
  176                             (a)->s6_addr16[0], (a)->s6_addr16[1],       \
  177                             (a)->s6_addr16[2], (a)->s6_addr16[3],       \
  178                             (a)->s6_addr16[4], (a)->s6_addr16[5],       \
  179                             (a)->s6_addr16[6], (a)->s6_addr16[7]),      \
  180                             (buf))
  181 #endif
  182 #endif
  183                         printf("NFS request from unprivileged port (%s:%d)\n",
  184 #ifdef INET6
  185                             sin->sin_family == AF_INET6 ?
  186                             ip6_sprintf(b6, &satosin6(sin)->sin6_addr) :
  187 #if defined(KLD_MODULE)
  188 #undef ip6_sprintf
  189 #endif
  190 #endif
  191                             inet_ntoa(sin->sin_addr), port);
  192                         svcerr_weakauth(rqst);
  193                         svc_freereq(rqst);
  194                         m_freem(nd.nd_mrep);
  195                         goto out;
  196                 }
  197         }
  198 
  199         if (nd.nd_procnum != NFSPROC_NULL) {
  200                 if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {
  201                         svcerr_weakauth(rqst);
  202                         svc_freereq(rqst);
  203                         m_freem(nd.nd_mrep);
  204                         goto out;
  205                 }
  206 
  207                 /* Set the flag based on credflavor */
  208                 if (credflavor == RPCSEC_GSS_KRB5) {
  209                         nd.nd_flag |= ND_GSS;
  210                 } else if (credflavor == RPCSEC_GSS_KRB5I) {
  211                         nd.nd_flag |= (ND_GSS | ND_GSSINTEGRITY);
  212                 } else if (credflavor == RPCSEC_GSS_KRB5P) {
  213                         nd.nd_flag |= (ND_GSS | ND_GSSPRIVACY);
  214                 } else if (credflavor != AUTH_SYS) {
  215                         svcerr_weakauth(rqst);
  216                         svc_freereq(rqst);
  217                         m_freem(nd.nd_mrep);
  218                         goto out;
  219                 }
  220 
  221 #ifdef MAC
  222                 mac_cred_associate_nfsd(nd.nd_cred);
  223 #endif
  224                 if ((nd.nd_flag & ND_NFSV4) != 0) {
  225                         nd.nd_repstat = nfsvno_v4rootexport(&nd);
  226                         if (nd.nd_repstat != 0) {
  227                                 svcerr_weakauth(rqst);
  228                                 svc_freereq(rqst);
  229                                 m_freem(nd.nd_mrep);
  230                                 goto out;
  231                         }
  232                 }
  233 
  234                 cacherep = nfs_proc(&nd, rqst->rq_xid, xprt->xp_socket,
  235                     xprt->xp_sockref, &rp);
  236         } else {
  237                 NFSMGET(nd.nd_mreq);
  238                 nd.nd_mreq->m_len = 0;
  239                 cacherep = RC_REPLY;
  240         }
  241         if (nd.nd_mrep != NULL)
  242                 m_freem(nd.nd_mrep);
  243 
  244         if (nd.nd_cred != NULL)
  245                 crfree(nd.nd_cred);
  246 
  247         if (cacherep == RC_DROPIT) {
  248                 if (nd.nd_mreq != NULL)
  249                         m_freem(nd.nd_mreq);
  250                 svc_freereq(rqst);
  251                 goto out;
  252         }
  253 
  254         if (nd.nd_mreq == NULL) {
  255                 svcerr_decode(rqst);
  256                 svc_freereq(rqst);
  257                 goto out;
  258         }
  259 
  260         if (nd.nd_repstat & NFSERR_AUTHERR) {
  261                 svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
  262                 if (nd.nd_mreq != NULL)
  263                         m_freem(nd.nd_mreq);
  264         } else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) {
  265                 svcerr_systemerr(rqst);
  266         }
  267         if (rp != NULL)
  268                 nfsrvd_sentcache(rp, xprt->xp_socket, 0);
  269         svc_freereq(rqst);
  270 
  271 out:
  272         NFSEXITCODE(0);
  273 }
  274 
  275 /*
  276  * Check the cache and, optionally, do the RPC.
  277  * Return the appropriate cache response.
  278  */
  279 static int
  280 nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, struct socket *so,
  281     u_int64_t sockref, struct nfsrvcache **rpp)
  282 {
  283         struct thread *td = curthread;
  284         int cacherep = RC_DOIT, isdgram;
  285 
  286         *rpp = NULL;
  287         if (nd->nd_nam2 == NULL) {
  288                 nd->nd_flag |= ND_STREAMSOCK;
  289                 isdgram = 0;
  290         } else {
  291                 isdgram = 1;
  292         }
  293 
  294         /*
  295          * Two cases:
  296          * 1 - For NFSv2 over UDP, if we are near our malloc/mget
  297          *     limit, just drop the request. There is no
  298          *     NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the
  299          *     client will timeout/retry over UDP in a little while.
  300          * 2 - nd_repstat == 0 && nd_mreq == NULL, which
  301          *     means a normal nfs rpc, so check the cache
  302          */
  303         if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL &&
  304             nfsrv_mallocmget_limit()) {
  305                 cacherep = RC_DROPIT;
  306         } else {
  307                 /*
  308                  * For NFSv3, play it safe and assume that the client is
  309                  * doing retries on the same TCP connection.
  310                  */
  311                 if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) ==
  312                     ND_STREAMSOCK)
  313                         nd->nd_flag |= ND_SAMETCPCONN;
  314                 nd->nd_retxid = xid;
  315                 nd->nd_tcpconntime = NFSD_MONOSEC;
  316                 nd->nd_sockref = sockref;
  317                 cacherep = nfsrvd_getcache(nd, so);
  318         }
  319 
  320         /*
  321          * Handle the request. There are three cases.
  322          * RC_DOIT - do the RPC
  323          * RC_REPLY - return the reply already created
  324          * RC_DROPIT - just throw the request away
  325          */
  326         if (cacherep == RC_DOIT) {
  327                 nfsrvd_dorpc(nd, isdgram, td);
  328                 if (nd->nd_repstat == NFSERR_DONTREPLY)
  329                         cacherep = RC_DROPIT;
  330                 else
  331                         cacherep = RC_REPLY;
  332                 *rpp = nfsrvd_updatecache(nd, so);
  333         }
  334 
  335         NFSEXITCODE2(0, nd);
  336         return (cacherep);
  337 }
  338 
  339 /*
  340  * Adds a socket to the list for servicing by nfsds.
  341  */
  342 int
  343 nfsrvd_addsock(struct file *fp)
  344 {
  345         int siz;
  346         struct socket *so;
  347         int error = 0;
  348         SVCXPRT *xprt;
  349         static u_int64_t sockref = 0;
  350 
  351         so = fp->f_data;
  352 
  353         siz = sb_max_adj;
  354         error = soreserve(so, siz, siz);
  355         if (error)
  356                 goto out;
  357 
  358         /*
  359          * Steal the socket from userland so that it doesn't close
  360          * unexpectedly.
  361          */
  362         if (so->so_type == SOCK_DGRAM)
  363                 xprt = svc_dg_create(nfsrvd_pool, so, 0, 0);
  364         else
  365                 xprt = svc_vc_create(nfsrvd_pool, so, 0, 0);
  366         if (xprt) {
  367                 fp->f_ops = &badfileops;
  368                 fp->f_data = NULL;
  369                 xprt->xp_sockref = ++sockref;
  370                 if (nfs_minvers == NFS_VER2)
  371                         svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program,
  372                             NULL);
  373                 if (nfs_minvers <= NFS_VER3 && nfs_maxvers >= NFS_VER3)
  374                         svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program,
  375                             NULL);
  376                 if (nfs_maxvers >= NFS_VER4)
  377                         svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program,
  378                             NULL);
  379                 SVC_RELEASE(xprt);
  380         }
  381 
  382 out:
  383         NFSEXITCODE(error);
  384         return (error);
  385 }
  386 
  387 /*
  388  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
  389  * until it is killed by a signal.
  390  */
  391 int
  392 nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
  393 {
  394         char principal[MAXHOSTNAMELEN + 5];
  395         int error = 0;
  396         bool_t ret2, ret3, ret4;
  397 
  398         error = copyinstr(args->principal, principal, sizeof (principal),
  399             NULL);
  400         if (error)
  401                 goto out;
  402 
  403         /*
  404          * Only the first nfsd actually does any work. The RPC code
  405          * adds threads to it as needed. Any extra processes offered
  406          * by nfsd just exit. If nfsd is new enough, it will call us
  407          * once with a structure that specifies how many threads to
  408          * use.
  409          */
  410         NFSD_LOCK();
  411         if (newnfs_numnfsd == 0) {
  412                 newnfs_numnfsd++;
  413 
  414                 NFSD_UNLOCK();
  415 
  416                 /* An empty string implies AUTH_SYS only. */
  417                 if (principal[0] != '\0') {
  418                         ret2 = rpc_gss_set_svc_name_call(principal,
  419                             "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER2);
  420                         ret3 = rpc_gss_set_svc_name_call(principal,
  421                             "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER3);
  422                         ret4 = rpc_gss_set_svc_name_call(principal,
  423                             "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER4);
  424 
  425                         if (!ret2 || !ret3 || !ret4)
  426                                 printf("nfsd: can't register svc name\n");
  427                 }
  428 
  429                 nfsrvd_pool->sp_minthreads = args->minthreads;
  430                 nfsrvd_pool->sp_maxthreads = args->maxthreads;
  431                         
  432                 svc_run(nfsrvd_pool);
  433 
  434                 if (principal[0] != '\0') {
  435                         rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
  436                         rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
  437                         rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4);
  438                 }
  439 
  440                 NFSD_LOCK();
  441                 newnfs_numnfsd--;
  442                 nfsrvd_init(1);
  443         }
  444         NFSD_UNLOCK();
  445 
  446 out:
  447         NFSEXITCODE(error);
  448         return (error);
  449 }
  450 
  451 /*
  452  * Initialize the data structures for the server.
  453  * Handshake with any new nfsds starting up to avoid any chance of
  454  * corruption.
  455  */
  456 void
  457 nfsrvd_init(int terminating)
  458 {
  459 
  460         NFSD_LOCK_ASSERT();
  461 
  462         if (terminating) {
  463                 nfsd_master_proc = NULL;
  464                 NFSD_UNLOCK();
  465                 svcpool_destroy(nfsrvd_pool);
  466                 nfsrvd_pool = NULL;
  467                 NFSD_LOCK();
  468         }
  469 
  470         NFSD_UNLOCK();
  471 
  472         nfsrvd_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_newnfs));
  473         nfsrvd_pool->sp_rcache = NULL;
  474         nfsrvd_pool->sp_assign = NULL;
  475         nfsrvd_pool->sp_done = NULL;
  476 
  477         NFSD_LOCK();
  478 }
  479 

Cache object: 299e519bcd4da8f3e0d1dd5a01f6f3c6


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