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$");
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 
   40 #include "opt_inet6.h"
   41 #include "opt_kgssapi.h"
   42 
   43 #include <fs/nfs/nfsport.h>
   44 
   45 #include <rpc/rpc.h>
   46 #include <rpc/rpcsec_gss.h>
   47 
   48 #include <nfs/nfs_fha.h>
   49 #include <fs/nfsserver/nfs_fha_new.h>
   50 
   51 #include <security/mac/mac_framework.h>
   52 
   53 NFSDLOCKMUTEX;
   54 NFSV4ROOTLOCKMUTEX;
   55 struct nfsv4lock nfsd_suspend_lock;
   56 
   57 /*
   58  * Mapping of old NFS Version 2 RPC numbers to generic numbers.
   59  */
   60 int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
   61         NFSPROC_NULL,
   62         NFSPROC_GETATTR,
   63         NFSPROC_SETATTR,
   64         NFSPROC_NOOP,
   65         NFSPROC_LOOKUP,
   66         NFSPROC_READLINK,
   67         NFSPROC_READ,
   68         NFSPROC_NOOP,
   69         NFSPROC_WRITE,
   70         NFSPROC_CREATE,
   71         NFSPROC_REMOVE,
   72         NFSPROC_RENAME,
   73         NFSPROC_LINK,
   74         NFSPROC_SYMLINK,
   75         NFSPROC_MKDIR,
   76         NFSPROC_RMDIR,
   77         NFSPROC_READDIR,
   78         NFSPROC_FSSTAT,
   79         NFSPROC_NOOP,
   80         NFSPROC_NOOP,
   81         NFSPROC_NOOP,
   82         NFSPROC_NOOP,
   83 };
   84 
   85 
   86 SYSCTL_DECL(_vfs_nfsd);
   87 
   88 SVCPOOL         *nfsrvd_pool;
   89 
   90 static int      nfs_privport = 0;
   91 SYSCTL_INT(_vfs_nfsd, OID_AUTO, nfs_privport, CTLFLAG_RW,
   92     &nfs_privport, 0,
   93     "Only allow clients using a privileged port for NFSv2 and 3");
   94 
   95 static int      nfs_minvers = NFS_VER2;
   96 SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_min_nfsvers, CTLFLAG_RW,
   97     &nfs_minvers, 0, "The lowest version of NFS handled by the server");
   98 
   99 static int      nfs_maxvers = NFS_VER4;
  100 SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_max_nfsvers, CTLFLAG_RW,
  101     &nfs_maxvers, 0, "The highest version of NFS handled by the server");
  102 
  103 static int nfs_proc(struct nfsrv_descript *, u_int32_t, SVCXPRT *xprt,
  104     struct nfsrvcache **);
  105 
  106 extern u_long sb_max_adj;
  107 extern int newnfs_numnfsd;
  108 extern struct proc *nfsd_master_proc;
  109 
  110 /*
  111  * NFS server system calls
  112  */
  113 
  114 static void
  115 nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
  116 {
  117         struct nfsrv_descript nd;
  118         struct nfsrvcache *rp = NULL;
  119         int cacherep, credflavor;
  120 
  121         memset(&nd, 0, sizeof(nd));
  122         if (rqst->rq_vers == NFS_VER2) {
  123                 if (rqst->rq_proc > NFSV2PROC_STATFS ||
  124                     newnfs_nfsv3_procid[rqst->rq_proc] == NFSPROC_NOOP) {
  125                         svcerr_noproc(rqst);
  126                         svc_freereq(rqst);
  127                         goto out;
  128                 }
  129                 nd.nd_procnum = newnfs_nfsv3_procid[rqst->rq_proc];
  130                 nd.nd_flag = ND_NFSV2;
  131         } else if (rqst->rq_vers == NFS_VER3) {
  132                 if (rqst->rq_proc >= NFS_V3NPROCS) {
  133                         svcerr_noproc(rqst);
  134                         svc_freereq(rqst);
  135                         goto out;
  136                 }
  137                 nd.nd_procnum = rqst->rq_proc;
  138                 nd.nd_flag = ND_NFSV3;
  139         } else {
  140                 if (rqst->rq_proc != NFSPROC_NULL &&
  141                     rqst->rq_proc != NFSV4PROC_COMPOUND) {
  142                         svcerr_noproc(rqst);
  143                         svc_freereq(rqst);
  144                         goto out;
  145                 }
  146                 nd.nd_procnum = rqst->rq_proc;
  147                 nd.nd_flag = ND_NFSV4;
  148         }
  149 
  150         /*
  151          * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
  152          * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
  153          * mounts.
  154          */
  155         nd.nd_mrep = rqst->rq_args;
  156         rqst->rq_args = NULL;
  157         newnfs_realign(&nd.nd_mrep, M_WAITOK);
  158         nd.nd_md = nd.nd_mrep;
  159         nd.nd_dpos = mtod(nd.nd_md, caddr_t);
  160         nd.nd_nam = svc_getrpccaller(rqst);
  161         nd.nd_nam2 = rqst->rq_addr;
  162         nd.nd_mreq = NULL;
  163         nd.nd_cred = NULL;
  164 
  165         if (nfs_privport && (nd.nd_flag & ND_NFSV4) == 0) {
  166                 /* Check if source port is privileged */
  167                 u_short port;
  168                 struct sockaddr *nam = nd.nd_nam;
  169                 struct sockaddr_in *sin;
  170 
  171                 sin = (struct sockaddr_in *)nam;
  172                 /*
  173                  * INET/INET6 - same code:
  174                  *    sin_port and sin6_port are at same offset
  175                  */
  176                 port = ntohs(sin->sin_port);
  177                 if (port >= IPPORT_RESERVED &&
  178                     nd.nd_procnum != NFSPROC_NULL) {
  179 #ifdef INET6
  180                         char b6[INET6_ADDRSTRLEN];
  181 #if defined(KLD_MODULE)
  182                         /* Do not use ip6_sprintf: the nfs module should work without INET6. */
  183 #define ip6_sprintf(buf, a)                                             \
  184                         (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x",      \
  185                             (a)->s6_addr16[0], (a)->s6_addr16[1],       \
  186                             (a)->s6_addr16[2], (a)->s6_addr16[3],       \
  187                             (a)->s6_addr16[4], (a)->s6_addr16[5],       \
  188                             (a)->s6_addr16[6], (a)->s6_addr16[7]),      \
  189                             (buf))
  190 #endif
  191 #endif
  192                         printf("NFS request from unprivileged port (%s:%d)\n",
  193 #ifdef INET6
  194                             sin->sin_family == AF_INET6 ?
  195                             ip6_sprintf(b6, &satosin6(sin)->sin6_addr) :
  196 #if defined(KLD_MODULE)
  197 #undef ip6_sprintf
  198 #endif
  199 #endif
  200                             inet_ntoa(sin->sin_addr), port);
  201                         svcerr_weakauth(rqst);
  202                         svc_freereq(rqst);
  203                         m_freem(nd.nd_mrep);
  204                         goto out;
  205                 }
  206         }
  207 
  208         if (nd.nd_procnum != NFSPROC_NULL) {
  209                 if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {
  210                         svcerr_weakauth(rqst);
  211                         svc_freereq(rqst);
  212                         m_freem(nd.nd_mrep);
  213                         goto out;
  214                 }
  215 
  216                 /* Set the flag based on credflavor */
  217                 if (credflavor == RPCSEC_GSS_KRB5) {
  218                         nd.nd_flag |= ND_GSS;
  219                 } else if (credflavor == RPCSEC_GSS_KRB5I) {
  220                         nd.nd_flag |= (ND_GSS | ND_GSSINTEGRITY);
  221                 } else if (credflavor == RPCSEC_GSS_KRB5P) {
  222                         nd.nd_flag |= (ND_GSS | ND_GSSPRIVACY);
  223                 } else if (credflavor != AUTH_SYS) {
  224                         svcerr_weakauth(rqst);
  225                         svc_freereq(rqst);
  226                         m_freem(nd.nd_mrep);
  227                         goto out;
  228                 }
  229 
  230 #ifdef MAC
  231                 mac_cred_associate_nfsd(nd.nd_cred);
  232 #endif
  233                 /*
  234                  * Get a refcnt (shared lock) on nfsd_suspend_lock.
  235                  * NFSSVC_SUSPENDNFSD will take an exclusive lock on
  236                  * nfsd_suspend_lock to suspend these threads.
  237                  * The call to nfsv4_lock() that preceeds nfsv4_getref()
  238                  * ensures that the acquisition of the exclusive lock
  239                  * takes priority over acquisition of the shared lock by
  240                  * waiting for any exclusive lock request to complete.
  241                  * This must be done here, before the check of
  242                  * nfsv4root exports by nfsvno_v4rootexport().
  243                  */
  244                 NFSLOCKV4ROOTMUTEX();
  245                 nfsv4_lock(&nfsd_suspend_lock, 0, NULL, NFSV4ROOTLOCKMUTEXPTR,
  246                     NULL);
  247                 nfsv4_getref(&nfsd_suspend_lock, NULL, NFSV4ROOTLOCKMUTEXPTR,
  248                     NULL);
  249                 NFSUNLOCKV4ROOTMUTEX();
  250 
  251                 if ((nd.nd_flag & ND_NFSV4) != 0) {
  252                         nd.nd_repstat = nfsvno_v4rootexport(&nd);
  253                         if (nd.nd_repstat != 0) {
  254                                 NFSLOCKV4ROOTMUTEX();
  255                                 nfsv4_relref(&nfsd_suspend_lock);
  256                                 NFSUNLOCKV4ROOTMUTEX();
  257                                 svcerr_weakauth(rqst);
  258                                 svc_freereq(rqst);
  259                                 m_freem(nd.nd_mrep);
  260                                 goto out;
  261                         }
  262                 }
  263 
  264                 cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp);
  265                 NFSLOCKV4ROOTMUTEX();
  266                 nfsv4_relref(&nfsd_suspend_lock);
  267                 NFSUNLOCKV4ROOTMUTEX();
  268         } else {
  269                 NFSMGET(nd.nd_mreq);
  270                 nd.nd_mreq->m_len = 0;
  271                 cacherep = RC_REPLY;
  272         }
  273         if (nd.nd_mrep != NULL)
  274                 m_freem(nd.nd_mrep);
  275 
  276         if (nd.nd_cred != NULL)
  277                 crfree(nd.nd_cred);
  278 
  279         if (cacherep == RC_DROPIT) {
  280                 if (nd.nd_mreq != NULL)
  281                         m_freem(nd.nd_mreq);
  282                 svc_freereq(rqst);
  283                 goto out;
  284         }
  285 
  286         if (nd.nd_mreq == NULL) {
  287                 svcerr_decode(rqst);
  288                 svc_freereq(rqst);
  289                 goto out;
  290         }
  291 
  292         if (nd.nd_repstat & NFSERR_AUTHERR) {
  293                 svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
  294                 if (nd.nd_mreq != NULL)
  295                         m_freem(nd.nd_mreq);
  296         } else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) {
  297                 svcerr_systemerr(rqst);
  298         }
  299         if (rp != NULL) {
  300                 nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 ||
  301                     SVC_ACK(xprt, NULL)), rqst->rq_reply_seq);
  302         }
  303         svc_freereq(rqst);
  304 
  305 out:
  306         NFSEXITCODE(0);
  307 }
  308 
  309 /*
  310  * Check the cache and, optionally, do the RPC.
  311  * Return the appropriate cache response.
  312  */
  313 static int
  314 nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt,
  315     struct nfsrvcache **rpp)
  316 {
  317         struct thread *td = curthread;
  318         int cacherep = RC_DOIT, isdgram;
  319         uint32_t ack;
  320 
  321         *rpp = NULL;
  322         if (nd->nd_nam2 == NULL) {
  323                 nd->nd_flag |= ND_STREAMSOCK;
  324                 isdgram = 0;
  325         } else {
  326                 isdgram = 1;
  327         }
  328 
  329         /*
  330          * Two cases:
  331          * 1 - For NFSv2 over UDP, if we are near our malloc/mget
  332          *     limit, just drop the request. There is no
  333          *     NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the
  334          *     client will timeout/retry over UDP in a little while.
  335          * 2 - nd_repstat == 0 && nd_mreq == NULL, which
  336          *     means a normal nfs rpc, so check the cache
  337          */
  338         if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL &&
  339             nfsrv_mallocmget_limit()) {
  340                 cacherep = RC_DROPIT;
  341         } else {
  342                 /*
  343                  * For NFSv3, play it safe and assume that the client is
  344                  * doing retries on the same TCP connection.
  345                  */
  346                 if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) ==
  347                     ND_STREAMSOCK)
  348                         nd->nd_flag |= ND_SAMETCPCONN;
  349                 nd->nd_retxid = xid;
  350                 nd->nd_tcpconntime = NFSD_MONOSEC;
  351                 nd->nd_sockref = xprt->xp_sockref;
  352                 cacherep = nfsrvd_getcache(nd);
  353                 ack = 0;
  354                 SVC_ACK(xprt, &ack);
  355                 nfsrc_trimcache(xprt->xp_sockref, ack, 0);
  356         }
  357 
  358         /*
  359          * Handle the request. There are three cases.
  360          * RC_DOIT - do the RPC
  361          * RC_REPLY - return the reply already created
  362          * RC_DROPIT - just throw the request away
  363          */
  364         if (cacherep == RC_DOIT) {
  365                 nfsrvd_dorpc(nd, isdgram, td);
  366                 if (nd->nd_repstat == NFSERR_DONTREPLY)
  367                         cacherep = RC_DROPIT;
  368                 else
  369                         cacherep = RC_REPLY;
  370                 *rpp = nfsrvd_updatecache(nd);
  371         }
  372 
  373         NFSEXITCODE2(0, nd);
  374         return (cacherep);
  375 }
  376 
  377 static void
  378 nfssvc_loss(SVCXPRT *xprt)
  379 {
  380         uint32_t ack;
  381 
  382         ack = 0;
  383         SVC_ACK(xprt, &ack);
  384         nfsrc_trimcache(xprt->xp_sockref, ack, 1);
  385 }
  386 
  387 /*
  388  * Adds a socket to the list for servicing by nfsds.
  389  */
  390 int
  391 nfsrvd_addsock(struct file *fp)
  392 {
  393         int siz;
  394         struct socket *so;
  395         int error = 0;
  396         SVCXPRT *xprt;
  397         static u_int64_t sockref = 0;
  398 
  399         so = fp->f_data;
  400 
  401         siz = sb_max_adj;
  402         error = soreserve(so, siz, siz);
  403         if (error)
  404                 goto out;
  405 
  406         /*
  407          * Steal the socket from userland so that it doesn't close
  408          * unexpectedly.
  409          */
  410         if (so->so_type == SOCK_DGRAM)
  411                 xprt = svc_dg_create(nfsrvd_pool, so, 0, 0);
  412         else
  413                 xprt = svc_vc_create(nfsrvd_pool, so, 0, 0);
  414         if (xprt) {
  415                 fp->f_ops = &badfileops;
  416                 fp->f_data = NULL;
  417                 xprt->xp_sockref = ++sockref;
  418                 if (nfs_minvers == NFS_VER2)
  419                         svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program,
  420                             NULL);
  421                 if (nfs_minvers <= NFS_VER3 && nfs_maxvers >= NFS_VER3)
  422                         svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program,
  423                             NULL);
  424                 if (nfs_maxvers >= NFS_VER4)
  425                         svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program,
  426                             NULL);
  427                 if (so->so_type == SOCK_STREAM)
  428                         svc_loss_reg(xprt, nfssvc_loss);
  429                 SVC_RELEASE(xprt);
  430         }
  431 
  432 out:
  433         NFSEXITCODE(error);
  434         return (error);
  435 }
  436 
  437 /*
  438  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
  439  * until it is killed by a signal.
  440  */
  441 int
  442 nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
  443 {
  444         char principal[MAXHOSTNAMELEN + 5];
  445         int error = 0;
  446         bool_t ret2, ret3, ret4;
  447 
  448         error = copyinstr(args->principal, principal, sizeof (principal),
  449             NULL);
  450         if (error)
  451                 goto out;
  452 
  453         /*
  454          * Only the first nfsd actually does any work. The RPC code
  455          * adds threads to it as needed. Any extra processes offered
  456          * by nfsd just exit. If nfsd is new enough, it will call us
  457          * once with a structure that specifies how many threads to
  458          * use.
  459          */
  460         NFSD_LOCK();
  461         if (newnfs_numnfsd == 0) {
  462                 newnfs_numnfsd++;
  463 
  464                 NFSD_UNLOCK();
  465 
  466                 /* An empty string implies AUTH_SYS only. */
  467                 if (principal[0] != '\0') {
  468                         ret2 = rpc_gss_set_svc_name_call(principal,
  469                             "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER2);
  470                         ret3 = rpc_gss_set_svc_name_call(principal,
  471                             "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER3);
  472                         ret4 = rpc_gss_set_svc_name_call(principal,
  473                             "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER4);
  474 
  475                         if (!ret2 || !ret3 || !ret4)
  476                                 printf("nfsd: can't register svc name\n");
  477                 }
  478 
  479                 nfsrvd_pool->sp_minthreads = args->minthreads;
  480                 nfsrvd_pool->sp_maxthreads = args->maxthreads;
  481                         
  482                 svc_run(nfsrvd_pool);
  483 
  484                 if (principal[0] != '\0') {
  485                         rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
  486                         rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
  487                         rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4);
  488                 }
  489 
  490                 NFSD_LOCK();
  491                 newnfs_numnfsd--;
  492                 nfsrvd_init(1);
  493         }
  494         NFSD_UNLOCK();
  495 
  496 out:
  497         NFSEXITCODE(error);
  498         return (error);
  499 }
  500 
  501 /*
  502  * Initialize the data structures for the server.
  503  * Handshake with any new nfsds starting up to avoid any chance of
  504  * corruption.
  505  */
  506 void
  507 nfsrvd_init(int terminating)
  508 {
  509 
  510         NFSD_LOCK_ASSERT();
  511 
  512         if (terminating) {
  513                 nfsd_master_proc = NULL;
  514                 NFSD_UNLOCK();
  515                 svcpool_destroy(nfsrvd_pool);
  516                 nfsrvd_pool = NULL;
  517                 NFSD_LOCK();
  518         }
  519 
  520         NFSD_UNLOCK();
  521 
  522         nfsrvd_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsd));
  523         nfsrvd_pool->sp_rcache = NULL;
  524         nfsrvd_pool->sp_assign = fhanew_assign;
  525         nfsrvd_pool->sp_done = fha_nd_complete;
  526 
  527         NFSD_LOCK();
  528 }
  529 

Cache object: 22a8cfbd0c24e1ff1873a39c63165ed4


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