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/nfsserver/nfs_srvkrpc.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  *      @(#)nfs_syscalls.c      8.5 (Berkeley) 3/30/95
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD$");
   37 
   38 #include "opt_inet6.h"
   39 #include "opt_kgssapi.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/sysproto.h>
   44 #include <sys/kernel.h>
   45 #include <sys/sysctl.h>
   46 #include <sys/file.h>
   47 #include <sys/filedesc.h>
   48 #include <sys/jail.h>
   49 #include <sys/vnode.h>
   50 #include <sys/malloc.h>
   51 #include <sys/mount.h>
   52 #include <sys/priv.h>
   53 #include <sys/proc.h>
   54 #include <sys/bio.h>
   55 #include <sys/buf.h>
   56 #include <sys/mbuf.h>
   57 #include <sys/socket.h>
   58 #include <sys/socketvar.h>
   59 #include <sys/domain.h>
   60 #include <sys/protosw.h>
   61 #include <sys/namei.h>
   62 #include <sys/fcntl.h>
   63 #include <sys/lockf.h>
   64 #include <sys/eventhandler.h>
   65 
   66 #include <netinet/in.h>
   67 #include <netinet/tcp.h>
   68 #ifdef INET6
   69 #include <net/if.h>
   70 #include <netinet6/in6_var.h>
   71 #endif
   72 
   73 #include <rpc/rpc.h>
   74 #include <rpc/rpcsec_gss.h>
   75 #include <rpc/replay.h>
   76 
   77 #include <nfs/xdr_subs.h>
   78 #include <nfs/nfsproto.h>
   79 #include <nfsserver/nfs.h>
   80 #include <nfsserver/nfsm_subs.h>
   81 #include <nfsserver/nfsrvcache.h>
   82 #include <nfsserver/nfs_fha.h>
   83 
   84 #include <security/mac/mac_framework.h>
   85 
   86 static MALLOC_DEFINE(M_NFSSVC, "nfss_srvsock", "Nfs server structure");
   87 
   88 MALLOC_DEFINE(M_NFSRVDESC, "nfss_srvdesc", "NFS server socket descriptor");
   89 MALLOC_DEFINE(M_NFSD, "nfss_daemon", "Nfs server daemon structure");
   90 
   91 #define TRUE    1
   92 #define FALSE   0
   93 
   94 SYSCTL_DECL(_vfs_nfsrv);
   95 
   96 SVCPOOL         *nfsrv_pool;
   97 int             nfsd_waiting = 0;
   98 int             nfsrv_numnfsd = 0;
   99 struct callout  nfsrv_callout;
  100 static eventhandler_tag nfsrv_nmbclusters_tag;
  101 
  102 static int      nfs_privport = 0;
  103 SYSCTL_INT(_vfs_nfsrv, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW,
  104     &nfs_privport, 0,
  105     "Only allow clients using a privileged port");
  106 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay, CTLFLAG_RW,
  107     &nfsrvw_procrastinate, 0,
  108     "Delay value for write gathering");
  109 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, gatherdelay_v3, CTLFLAG_RW,
  110     &nfsrvw_procrastinate_v3, 0,
  111     "Delay in seconds for NFSv3 write gathering");
  112 
  113 static int      nfssvc_addsock(struct file *, struct thread *);
  114 static int      nfssvc_nfsd(struct thread *, struct nfsd_nfsd_args *);
  115 
  116 extern u_long sb_max_adj;
  117 
  118 int32_t (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *nd,
  119     struct nfssvc_sock *slp, struct mbuf **mreqp) = {
  120         nfsrv_null,
  121         nfsrv_getattr,
  122         nfsrv_setattr,
  123         nfsrv_lookup,
  124         nfsrv3_access,
  125         nfsrv_readlink,
  126         nfsrv_read,
  127         nfsrv_write,
  128         nfsrv_create,
  129         nfsrv_mkdir,
  130         nfsrv_symlink,
  131         nfsrv_mknod,
  132         nfsrv_remove,
  133         nfsrv_rmdir,
  134         nfsrv_rename,
  135         nfsrv_link,
  136         nfsrv_readdir,
  137         nfsrv_readdirplus,
  138         nfsrv_statfs,
  139         nfsrv_fsinfo,
  140         nfsrv_pathconf,
  141         nfsrv_commit,
  142         nfsrv_noop
  143 };
  144 
  145 /*
  146  * NFS server system calls
  147  */
  148 /*
  149  * This is now called from nfssvc() in nfs/nfs_nfssvc.c.
  150  */
  151 
  152 /*
  153  * Nfs server psuedo system call for the nfsd's
  154  * Based on the flag value it either:
  155  * - adds a socket to the selection list
  156  * - remains in the kernel as an nfsd
  157  * - remains in the kernel as an nfsiod
  158  * For INET6 we suppose that nfsd provides only IN6P_IPV6_V6ONLY sockets
  159  * and that mountd provides
  160  *  - sockaddr with no IPv4-mapped addresses
  161  *  - mask for both INET and INET6 families if there is IPv4-mapped overlap
  162  */
  163 int
  164 nfssvc_nfsserver(struct thread *td, struct nfssvc_args *uap)
  165 {
  166         struct file *fp;
  167         struct nfsd_addsock_args addsockarg;
  168         struct nfsd_nfsd_args nfsdarg;
  169         int error;
  170 
  171         if (uap->flag & NFSSVC_ADDSOCK) {
  172                 error = copyin(uap->argp, (caddr_t)&addsockarg,
  173                     sizeof(addsockarg));
  174                 if (error)
  175                         return (error);
  176                 if ((error = fget(td, addsockarg.sock, &fp)) != 0)
  177                         return (error);
  178                 if (fp->f_type != DTYPE_SOCKET) {
  179                         fdrop(fp, td);
  180                         return (error); /* XXXRW: Should be EINVAL? */
  181                 }
  182                 error = nfssvc_addsock(fp, td);
  183                 fdrop(fp, td);
  184         } else if (uap->flag & NFSSVC_OLDNFSD)
  185                 error = nfssvc_nfsd(td, NULL);
  186         else if (uap->flag & NFSSVC_NFSD) {
  187                 if (!uap->argp)
  188                         return (EINVAL);
  189                 error = copyin(uap->argp, (caddr_t)&nfsdarg,
  190                     sizeof(nfsdarg));
  191                 if (error)
  192                         return (error);
  193                 error = nfssvc_nfsd(td, &nfsdarg);
  194         } else
  195                 error = ENXIO;
  196         return (error);
  197 }
  198 
  199 /*
  200  * Generate the rpc reply header
  201  * siz arg. is used to decide if adding a cluster is worthwhile
  202  */
  203 struct mbuf *
  204 nfs_rephead(int siz, struct nfsrv_descript *nd, int err,
  205     struct mbuf **mbp, caddr_t *bposp)
  206 {
  207         u_int32_t *tl;
  208         struct mbuf *mreq;
  209         caddr_t bpos;
  210         struct mbuf *mb;
  211 
  212         if (err == EBADRPC)
  213                 return (NULL);
  214 
  215         nd->nd_repstat = err;
  216         if (err && (nd->nd_flag & ND_NFSV3) == 0)       /* XXX recheck */
  217                 siz = 0;
  218 
  219         MGET(mreq, M_WAIT, MT_DATA);
  220 
  221         /*
  222          * If this is a big reply, use a cluster
  223          */
  224         mreq->m_len = 0;
  225         if (siz >= MINCLSIZE) {
  226                 MCLGET(mreq, M_WAIT);
  227         }
  228         mb = mreq;
  229         bpos = mtod(mb, caddr_t);
  230 
  231         if (err != NFSERR_RETVOID) {
  232                 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
  233                 if (err)
  234                         *tl = txdr_unsigned(nfsrv_errmap(nd, err));
  235                 else
  236                         *tl = 0;
  237         }
  238 
  239         *mbp = mb;
  240         *bposp = bpos;
  241         if (err != 0 && err != NFSERR_RETVOID)
  242                 nfsrvstats.srvrpc_errs++;
  243 
  244         return (mreq);
  245 }
  246 
  247 static void
  248 nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
  249 {
  250         rpcproc_t procnum;
  251         int32_t (*proc)(struct nfsrv_descript *nd, struct nfssvc_sock *slp,
  252             struct mbuf **mreqp);
  253         int flag;
  254         struct nfsrv_descript nd;
  255         struct mbuf *mreq, *mrep;
  256         int error;
  257 
  258         if (rqst->rq_vers == NFS_VER2) {
  259                 if (rqst->rq_proc > NFSV2PROC_STATFS) {
  260                         svcerr_noproc(rqst);
  261                         svc_freereq(rqst);
  262                         return;
  263                 }
  264                 procnum = nfsrv_nfsv3_procid[rqst->rq_proc];
  265                 flag = 0;
  266         } else {
  267                 if (rqst->rq_proc >= NFS_NPROCS) {
  268                         svcerr_noproc(rqst);
  269                         svc_freereq(rqst);
  270                         return;
  271                 }
  272                 procnum = rqst->rq_proc;
  273                 flag = ND_NFSV3;
  274         }
  275         proc = nfsrv3_procs[procnum];
  276 
  277         mreq = mrep = NULL;
  278         mreq = rqst->rq_args;
  279         rqst->rq_args = NULL;
  280         (void)nfs_realign(&mreq, M_WAIT);
  281 
  282         /*
  283          * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
  284          * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
  285          * mounts.
  286          */
  287         memset(&nd, 0, sizeof(nd));
  288         nd.nd_md = nd.nd_mrep = mreq;
  289         nd.nd_dpos = mtod(mreq, caddr_t);
  290         nd.nd_nam = svc_getrpccaller(rqst);
  291         nd.nd_nam2 = rqst->rq_addr;
  292         nd.nd_procnum = procnum;
  293         nd.nd_cr = NULL;
  294         nd.nd_flag = flag;
  295 
  296         if (nfs_privport) {
  297                 /* Check if source port is privileged */
  298                 u_short port;
  299                 struct sockaddr *nam = nd.nd_nam;
  300                 struct sockaddr_in *sin;
  301 
  302                 sin = (struct sockaddr_in *)nam;
  303                 /*
  304                  * INET/INET6 - same code:
  305                  *    sin_port and sin6_port are at same offset
  306                  */
  307                 port = ntohs(sin->sin_port);
  308                 if (port >= IPPORT_RESERVED &&
  309                     nd.nd_procnum != NFSPROC_NULL) {
  310 #ifdef INET6
  311                         char b6[INET6_ADDRSTRLEN];
  312 #if defined(KLD_MODULE)
  313                         /* Do not use ip6_sprintf: the nfs module should work without INET6. */
  314 #define ip6_sprintf(buf, a)                                             \
  315                         (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x",      \
  316                             (a)->s6_addr16[0], (a)->s6_addr16[1],       \
  317                             (a)->s6_addr16[2], (a)->s6_addr16[3],       \
  318                             (a)->s6_addr16[4], (a)->s6_addr16[5],       \
  319                             (a)->s6_addr16[6], (a)->s6_addr16[7]),      \
  320                             (buf))
  321 #endif
  322 #endif
  323                         printf("NFS request from unprivileged port (%s:%d)\n",
  324 #ifdef INET6
  325                             sin->sin_family == AF_INET6 ?
  326                             ip6_sprintf(b6, &satosin6(sin)->sin6_addr) :
  327 #if defined(KLD_MODULE)
  328 #undef ip6_sprintf
  329 #endif
  330 #endif
  331                             inet_ntoa(sin->sin_addr), port);
  332                         m_freem(mreq);
  333                         svcerr_weakauth(rqst);
  334                         svc_freereq(rqst);
  335                         return;
  336                 }
  337         }
  338 
  339         if (proc != nfsrv_null) {
  340                 if (!svc_getcred(rqst, &nd.nd_cr, &nd.nd_credflavor)) {
  341                         m_freem(mreq);
  342                         svcerr_weakauth(rqst);
  343                         svc_freereq(rqst);
  344                         return;
  345                 }
  346 #ifdef MAC
  347                 mac_cred_associate_nfsd(nd.nd_cr);
  348 #endif
  349         }
  350         nfsrvstats.srvrpccnt[nd.nd_procnum]++;
  351 
  352         error = proc(&nd, NULL, &mrep);
  353 
  354         if (nd.nd_cr)
  355                 crfree(nd.nd_cr);
  356 
  357         if (mrep == NULL) {
  358                 svcerr_decode(rqst);
  359                 svc_freereq(rqst);
  360                 return;
  361         }
  362         if (error && error != NFSERR_RETVOID) {
  363                 svcerr_systemerr(rqst);
  364                 svc_freereq(rqst);
  365                 return;
  366         }
  367         if (nd.nd_repstat & NFSERR_AUTHERR) {
  368                 svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
  369                 m_freem(mrep);
  370         } else {
  371                 if (!svc_sendreply_mbuf(rqst, mrep))
  372                         svcerr_systemerr(rqst);
  373         }
  374         svc_freereq(rqst);
  375 }
  376 
  377 /*
  378  * Adds a socket to the list for servicing by nfsds.
  379  */
  380 static int
  381 nfssvc_addsock(struct file *fp, struct thread *td)
  382 {
  383         int siz;
  384         struct socket *so;
  385         int error;
  386         SVCXPRT *xprt;
  387 
  388         so = fp->f_data;
  389 
  390         siz = sb_max_adj;
  391         error = soreserve(so, siz, siz);
  392         if (error)
  393                 return (error);
  394 
  395         /*
  396          * Steal the socket from userland so that it doesn't close
  397          * unexpectedly.
  398          */
  399         if (so->so_type == SOCK_DGRAM)
  400                 xprt = svc_dg_create(nfsrv_pool, so, 0, 0);
  401         else
  402                 xprt = svc_vc_create(nfsrv_pool, so, 0, 0);
  403         if (xprt) {
  404                 fp->f_ops = &badfileops;
  405                 fp->f_data = NULL;
  406                 svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program, NULL);
  407                 svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program, NULL);
  408                 SVC_RELEASE(xprt);
  409         }
  410 
  411         return (0);
  412 }
  413 
  414 /*
  415  * Called by nfssvc() for nfsds.  Just loops around servicing rpc requests
  416  * until it is killed by a signal.
  417  */
  418 static int
  419 nfssvc_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
  420 {
  421         char principal[128];
  422         int error;
  423 
  424         if (args) {
  425                 error = copyinstr(args->principal, principal,
  426                     sizeof(principal), NULL);
  427                 if (error)
  428                         return (error);
  429         } else {
  430                 memcpy(principal, "nfs@", 4);
  431                 getcredhostname(td->td_ucred, principal + 4,
  432                     sizeof(principal) - 4);
  433         }
  434 
  435         /*
  436          * Only the first nfsd actually does any work.  The RPC code
  437          * adds threads to it as needed.  Any extra processes offered
  438          * by nfsd just exit.  If nfsd is new enough, it will call us
  439          * once with a structure that specifies how many threads to
  440          * use.
  441          */
  442         NFSD_LOCK();
  443         if (nfsrv_numnfsd == 0) {
  444                 nfsrv_numnfsd++;
  445 
  446                 NFSD_UNLOCK();
  447 
  448                 rpc_gss_set_svc_name_call(principal, "kerberosv5",
  449                     GSS_C_INDEFINITE, NFS_PROG, NFS_VER2);
  450                 rpc_gss_set_svc_name_call(principal, "kerberosv5",
  451                     GSS_C_INDEFINITE, NFS_PROG, NFS_VER3);
  452 
  453                 if (args) {
  454                         nfsrv_pool->sp_minthreads = args->minthreads;
  455                         nfsrv_pool->sp_maxthreads = args->maxthreads;
  456                 } else {
  457                         nfsrv_pool->sp_minthreads = 4;
  458                         nfsrv_pool->sp_maxthreads = 4;
  459                 }
  460 
  461                 svc_run(nfsrv_pool);
  462 
  463                 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
  464                 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
  465 
  466                 NFSD_LOCK();
  467                 nfsrv_numnfsd--;
  468                 nfsrv_init(TRUE);
  469         }
  470         NFSD_UNLOCK();
  471 
  472         return (0);
  473 }
  474 
  475 /*
  476  * Size the NFS server's duplicate request cache at 1/2 the
  477  * nmbclusters, floating within a (64, 2048) range.  This is to
  478  * prevent all mbuf clusters being tied up in the NFS dupreq
  479  * cache for small values of nmbclusters.
  480  */
  481 static size_t
  482 nfsrv_replay_size(void)
  483 {
  484         size_t replaysiz;
  485 
  486         replaysiz = nmbclusters / 2;
  487         if (replaysiz > NFSRVCACHE_MAX_SIZE)
  488                 replaysiz = NFSRVCACHE_MAX_SIZE;
  489         if (replaysiz < NFSRVCACHE_MIN_SIZE)
  490                 replaysiz = NFSRVCACHE_MIN_SIZE;
  491         replaysiz *= MCLBYTES;
  492 
  493         return (replaysiz);
  494 }
  495 
  496 /*
  497  * Called when nmbclusters changes - we resize the replay cache
  498  * accordingly.
  499  */
  500 static void
  501 nfsrv_nmbclusters_change(void *tag)
  502 {
  503 
  504         if (nfsrv_pool)
  505                 replay_setsize(nfsrv_pool->sp_rcache, nfsrv_replay_size());
  506 }
  507 
  508 /*
  509  * Initialize the data structures for the server.
  510  * Handshake with any new nfsds starting up to avoid any chance of
  511  * corruption.
  512  */
  513 void
  514 nfsrv_init(int terminating)
  515 {
  516 
  517         NFSD_LOCK_ASSERT();
  518 
  519         if (terminating) {
  520                 NFSD_UNLOCK();
  521                 EVENTHANDLER_DEREGISTER(nmbclusters_change,
  522                     nfsrv_nmbclusters_tag);
  523                 svcpool_destroy(nfsrv_pool);
  524                 nfsrv_pool = NULL;
  525                 NFSD_LOCK();
  526         } else
  527                 nfs_pub.np_valid = 0;
  528 
  529         NFSD_UNLOCK();
  530 
  531         nfsrv_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsrv));
  532         nfsrv_pool->sp_rcache = replay_newcache(nfsrv_replay_size());
  533         nfsrv_pool->sp_assign = fha_assign;
  534         nfsrv_pool->sp_done = fha_nd_complete;
  535         nfsrv_nmbclusters_tag = EVENTHANDLER_REGISTER(nmbclusters_change,
  536             nfsrv_nmbclusters_change, NULL, EVENTHANDLER_PRI_FIRST);
  537 
  538         NFSD_LOCK();
  539 }

Cache object: 8b19bdee441929124220e1cfd28e57e8


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