1*481d3881Srin /* $NetBSD: nfs_nfsdkrpc.c,v 1.5 2024/07/05 04:31:52 rin Exp $ */
26ca35587Sdholland /*-
36ca35587Sdholland * Copyright (c) 1989, 1993
46ca35587Sdholland * The Regents of the University of California. All rights reserved.
56ca35587Sdholland *
66ca35587Sdholland * This code is derived from software contributed to Berkeley by
76ca35587Sdholland * Rick Macklem at The University of Guelph.
86ca35587Sdholland *
96ca35587Sdholland * Redistribution and use in source and binary forms, with or without
106ca35587Sdholland * modification, are permitted provided that the following conditions
116ca35587Sdholland * are met:
126ca35587Sdholland * 1. Redistributions of source code must retain the above copyright
136ca35587Sdholland * notice, this list of conditions and the following disclaimer.
146ca35587Sdholland * 2. Redistributions in binary form must reproduce the above copyright
156ca35587Sdholland * notice, this list of conditions and the following disclaimer in the
166ca35587Sdholland * documentation and/or other materials provided with the distribution.
176ca35587Sdholland * 4. Neither the name of the University nor the names of its contributors
186ca35587Sdholland * may be used to endorse or promote products derived from this software
196ca35587Sdholland * without specific prior written permission.
206ca35587Sdholland *
216ca35587Sdholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
226ca35587Sdholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
236ca35587Sdholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
246ca35587Sdholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
256ca35587Sdholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
266ca35587Sdholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
276ca35587Sdholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
286ca35587Sdholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
296ca35587Sdholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
306ca35587Sdholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
316ca35587Sdholland * SUCH DAMAGE.
326ca35587Sdholland *
336ca35587Sdholland */
346ca35587Sdholland
356ca35587Sdholland #include <sys/cdefs.h>
362d39560cSpgoyette /* __FBSDID("FreeBSD: head/sys/fs/nfsserver/nfs_nfsdkrpc.c 299203 2016-05-06 23:40:37Z pfg "); */
37*481d3881Srin __RCSID("$NetBSD: nfs_nfsdkrpc.c,v 1.5 2024/07/05 04:31:52 rin Exp $");
386ca35587Sdholland
39c10f8b50Spgoyette #ifdef _KERNEL_OPT
406ca35587Sdholland #include "opt_inet6.h"
41c10f8b50Spgoyette #if 0
426ca35587Sdholland #include "opt_kgssapi.h"
43c10f8b50Spgoyette #endif
44c10f8b50Spgoyette #endif
456ca35587Sdholland
46c10f8b50Spgoyette #include <fs/nfs/common/nfsport.h>
476ca35587Sdholland
486ca35587Sdholland #include <rpc/rpc.h>
496ca35587Sdholland #include <rpc/rpcsec_gss.h>
506ca35587Sdholland
51c10f8b50Spgoyette #include <fs/nfs/common/nfs_fha.h>
52c10f8b50Spgoyette #include <fs/nfs/server/nfs_fha_new.h>
536ca35587Sdholland
54c10f8b50Spgoyette #if 0
556ca35587Sdholland #include <security/mac/mac_framework.h>
56c10f8b50Spgoyette #endif
576ca35587Sdholland
586ca35587Sdholland NFSDLOCKMUTEX;
596ca35587Sdholland NFSV4ROOTLOCKMUTEX;
606ca35587Sdholland struct nfsv4lock nfsd_suspend_lock;
616ca35587Sdholland
626ca35587Sdholland /*
636ca35587Sdholland * Mapping of old NFS Version 2 RPC numbers to generic numbers.
646ca35587Sdholland */
656ca35587Sdholland int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
666ca35587Sdholland NFSPROC_NULL,
676ca35587Sdholland NFSPROC_GETATTR,
686ca35587Sdholland NFSPROC_SETATTR,
696ca35587Sdholland NFSPROC_NOOP,
706ca35587Sdholland NFSPROC_LOOKUP,
716ca35587Sdholland NFSPROC_READLINK,
726ca35587Sdholland NFSPROC_READ,
736ca35587Sdholland NFSPROC_NOOP,
746ca35587Sdholland NFSPROC_WRITE,
756ca35587Sdholland NFSPROC_CREATE,
766ca35587Sdholland NFSPROC_REMOVE,
776ca35587Sdholland NFSPROC_RENAME,
786ca35587Sdholland NFSPROC_LINK,
796ca35587Sdholland NFSPROC_SYMLINK,
806ca35587Sdholland NFSPROC_MKDIR,
816ca35587Sdholland NFSPROC_RMDIR,
826ca35587Sdholland NFSPROC_READDIR,
836ca35587Sdholland NFSPROC_FSSTAT,
846ca35587Sdholland NFSPROC_NOOP,
856ca35587Sdholland NFSPROC_NOOP,
866ca35587Sdholland NFSPROC_NOOP,
876ca35587Sdholland NFSPROC_NOOP,
886ca35587Sdholland };
896ca35587Sdholland
906ca35587Sdholland
916ca35587Sdholland SYSCTL_DECL(_vfs_nfsd);
926ca35587Sdholland
936ca35587Sdholland SVCPOOL *nfsrvd_pool;
946ca35587Sdholland
956ca35587Sdholland static int nfs_privport = 0;
962d39560cSpgoyette SYSCTL_INT(_vfs_nfsd, OID_AUTO, nfs_privport, CTLFLAG_RWTUN,
976ca35587Sdholland &nfs_privport, 0,
986ca35587Sdholland "Only allow clients using a privileged port for NFSv2 and 3");
996ca35587Sdholland
1006ca35587Sdholland static int nfs_minvers = NFS_VER2;
1012d39560cSpgoyette SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_min_nfsvers, CTLFLAG_RWTUN,
1026ca35587Sdholland &nfs_minvers, 0, "The lowest version of NFS handled by the server");
1036ca35587Sdholland
1046ca35587Sdholland static int nfs_maxvers = NFS_VER4;
1052d39560cSpgoyette SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_max_nfsvers, CTLFLAG_RWTUN,
1066ca35587Sdholland &nfs_maxvers, 0, "The highest version of NFS handled by the server");
1076ca35587Sdholland
1082d39560cSpgoyette static int nfs_proc(struct nfsrv_descript *, u_int32_t, SVCXPRT *xprt,
1092d39560cSpgoyette struct nfsrvcache **);
1106ca35587Sdholland
1116ca35587Sdholland extern u_long sb_max_adj;
1126ca35587Sdholland extern int newnfs_numnfsd;
1136ca35587Sdholland extern struct proc *nfsd_master_proc;
1146ca35587Sdholland
1156ca35587Sdholland /*
1166ca35587Sdholland * NFS server system calls
1176ca35587Sdholland */
1186ca35587Sdholland
1196ca35587Sdholland static void
nfssvc_program(struct svc_req * rqst,SVCXPRT * xprt)1206ca35587Sdholland nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
1216ca35587Sdholland {
1226ca35587Sdholland struct nfsrv_descript nd;
1236ca35587Sdholland struct nfsrvcache *rp = NULL;
1246ca35587Sdholland int cacherep, credflavor;
1256ca35587Sdholland
1266ca35587Sdholland memset(&nd, 0, sizeof(nd));
1276ca35587Sdholland if (rqst->rq_vers == NFS_VER2) {
1282d39560cSpgoyette if (rqst->rq_proc > NFSV2PROC_STATFS ||
1292d39560cSpgoyette newnfs_nfsv3_procid[rqst->rq_proc] == NFSPROC_NOOP) {
1306ca35587Sdholland svcerr_noproc(rqst);
1316ca35587Sdholland svc_freereq(rqst);
1326ca35587Sdholland goto out;
1336ca35587Sdholland }
1346ca35587Sdholland nd.nd_procnum = newnfs_nfsv3_procid[rqst->rq_proc];
1356ca35587Sdholland nd.nd_flag = ND_NFSV2;
1366ca35587Sdholland } else if (rqst->rq_vers == NFS_VER3) {
1376ca35587Sdholland if (rqst->rq_proc >= NFS_V3NPROCS) {
1386ca35587Sdholland svcerr_noproc(rqst);
1396ca35587Sdholland svc_freereq(rqst);
1406ca35587Sdholland goto out;
1416ca35587Sdholland }
1426ca35587Sdholland nd.nd_procnum = rqst->rq_proc;
1436ca35587Sdholland nd.nd_flag = ND_NFSV3;
1446ca35587Sdholland } else {
1456ca35587Sdholland if (rqst->rq_proc != NFSPROC_NULL &&
1466ca35587Sdholland rqst->rq_proc != NFSV4PROC_COMPOUND) {
1476ca35587Sdholland svcerr_noproc(rqst);
1486ca35587Sdholland svc_freereq(rqst);
1496ca35587Sdholland goto out;
1506ca35587Sdholland }
1516ca35587Sdholland nd.nd_procnum = rqst->rq_proc;
1526ca35587Sdholland nd.nd_flag = ND_NFSV4;
1536ca35587Sdholland }
1546ca35587Sdholland
1556ca35587Sdholland /*
1566ca35587Sdholland * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
1576ca35587Sdholland * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
1586ca35587Sdholland * mounts.
1596ca35587Sdholland */
1606ca35587Sdholland nd.nd_mrep = rqst->rq_args;
1616ca35587Sdholland rqst->rq_args = NULL;
1626ca35587Sdholland newnfs_realign(&nd.nd_mrep, M_WAITOK);
1636ca35587Sdholland nd.nd_md = nd.nd_mrep;
1646ca35587Sdholland nd.nd_dpos = mtod(nd.nd_md, caddr_t);
1656ca35587Sdholland nd.nd_nam = svc_getrpccaller(rqst);
1666ca35587Sdholland nd.nd_nam2 = rqst->rq_addr;
1676ca35587Sdholland nd.nd_mreq = NULL;
1686ca35587Sdholland nd.nd_cred = NULL;
1696ca35587Sdholland
1706ca35587Sdholland if (nfs_privport && (nd.nd_flag & ND_NFSV4) == 0) {
1716ca35587Sdholland /* Check if source port is privileged */
1726ca35587Sdholland u_short port;
1736ca35587Sdholland struct sockaddr *nam = nd.nd_nam;
1746ca35587Sdholland struct sockaddr_in *sin;
1756ca35587Sdholland
1766ca35587Sdholland sin = (struct sockaddr_in *)nam;
1776ca35587Sdholland /*
1786ca35587Sdholland * INET/INET6 - same code:
1796ca35587Sdholland * sin_port and sin6_port are at same offset
1806ca35587Sdholland */
1816ca35587Sdholland port = ntohs(sin->sin_port);
1826ca35587Sdholland if (port >= IPPORT_RESERVED &&
1836ca35587Sdholland nd.nd_procnum != NFSPROC_NULL) {
1846ca35587Sdholland #ifdef INET6
1856ca35587Sdholland char b6[INET6_ADDRSTRLEN];
186c10f8b50Spgoyette #if defined(_MODULE)
1876ca35587Sdholland /* Do not use ip6_sprintf: the nfs module should work without INET6. */
1886ca35587Sdholland #define ip6_sprintf(buf, a) \
18966873a20Schristos (snprintf((buf), sizeof(buf), "%x:%x:%x:%x:%x:%x:%x:%x", \
1906ca35587Sdholland (a)->s6_addr16[0], (a)->s6_addr16[1], \
1916ca35587Sdholland (a)->s6_addr16[2], (a)->s6_addr16[3], \
1926ca35587Sdholland (a)->s6_addr16[4], (a)->s6_addr16[5], \
1936ca35587Sdholland (a)->s6_addr16[6], (a)->s6_addr16[7]), \
1946ca35587Sdholland (buf))
1956ca35587Sdholland #endif
1966ca35587Sdholland #endif
1976ca35587Sdholland printf("NFS request from unprivileged port (%s:%d)\n",
1986ca35587Sdholland #ifdef INET6
1996ca35587Sdholland sin->sin_family == AF_INET6 ?
2006ca35587Sdholland ip6_sprintf(b6, &satosin6(sin)->sin6_addr) :
201c10f8b50Spgoyette #if defined(_MODULE)
2026ca35587Sdholland #undef ip6_sprintf
2036ca35587Sdholland #endif
2046ca35587Sdholland #endif
2056ca35587Sdholland inet_ntoa(sin->sin_addr), port);
2066ca35587Sdholland svcerr_weakauth(rqst);
2076ca35587Sdholland svc_freereq(rqst);
2086ca35587Sdholland m_freem(nd.nd_mrep);
2096ca35587Sdholland goto out;
2106ca35587Sdholland }
2116ca35587Sdholland }
2126ca35587Sdholland
2136ca35587Sdholland if (nd.nd_procnum != NFSPROC_NULL) {
2146ca35587Sdholland if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {
2156ca35587Sdholland svcerr_weakauth(rqst);
2166ca35587Sdholland svc_freereq(rqst);
2176ca35587Sdholland m_freem(nd.nd_mrep);
2186ca35587Sdholland goto out;
2196ca35587Sdholland }
2206ca35587Sdholland
2216ca35587Sdholland /* Set the flag based on credflavor */
2226ca35587Sdholland if (credflavor == RPCSEC_GSS_KRB5) {
2236ca35587Sdholland nd.nd_flag |= ND_GSS;
2246ca35587Sdholland } else if (credflavor == RPCSEC_GSS_KRB5I) {
2256ca35587Sdholland nd.nd_flag |= (ND_GSS | ND_GSSINTEGRITY);
2266ca35587Sdholland } else if (credflavor == RPCSEC_GSS_KRB5P) {
2276ca35587Sdholland nd.nd_flag |= (ND_GSS | ND_GSSPRIVACY);
2286ca35587Sdholland } else if (credflavor != AUTH_SYS) {
2296ca35587Sdholland svcerr_weakauth(rqst);
2306ca35587Sdholland svc_freereq(rqst);
2316ca35587Sdholland m_freem(nd.nd_mrep);
2326ca35587Sdholland goto out;
2336ca35587Sdholland }
2346ca35587Sdholland
2356ca35587Sdholland #ifdef MAC
2366ca35587Sdholland mac_cred_associate_nfsd(nd.nd_cred);
2376ca35587Sdholland #endif
2386ca35587Sdholland /*
2396ca35587Sdholland * Get a refcnt (shared lock) on nfsd_suspend_lock.
2406ca35587Sdholland * NFSSVC_SUSPENDNFSD will take an exclusive lock on
2416ca35587Sdholland * nfsd_suspend_lock to suspend these threads.
2422d39560cSpgoyette * The call to nfsv4_lock() that precedes nfsv4_getref()
2432d39560cSpgoyette * ensures that the acquisition of the exclusive lock
2442d39560cSpgoyette * takes priority over acquisition of the shared lock by
2452d39560cSpgoyette * waiting for any exclusive lock request to complete.
2466ca35587Sdholland * This must be done here, before the check of
2476ca35587Sdholland * nfsv4root exports by nfsvno_v4rootexport().
2486ca35587Sdholland */
2496ca35587Sdholland NFSLOCKV4ROOTMUTEX();
2502d39560cSpgoyette nfsv4_lock(&nfsd_suspend_lock, 0, NULL, NFSV4ROOTLOCKMUTEXPTR,
2512d39560cSpgoyette NULL);
2526ca35587Sdholland nfsv4_getref(&nfsd_suspend_lock, NULL, NFSV4ROOTLOCKMUTEXPTR,
2536ca35587Sdholland NULL);
2546ca35587Sdholland NFSUNLOCKV4ROOTMUTEX();
2556ca35587Sdholland
2566ca35587Sdholland if ((nd.nd_flag & ND_NFSV4) != 0) {
2576ca35587Sdholland nd.nd_repstat = nfsvno_v4rootexport(&nd);
2586ca35587Sdholland if (nd.nd_repstat != 0) {
2596ca35587Sdholland NFSLOCKV4ROOTMUTEX();
2606ca35587Sdholland nfsv4_relref(&nfsd_suspend_lock);
2616ca35587Sdholland NFSUNLOCKV4ROOTMUTEX();
2626ca35587Sdholland svcerr_weakauth(rqst);
2636ca35587Sdholland svc_freereq(rqst);
2646ca35587Sdholland m_freem(nd.nd_mrep);
2656ca35587Sdholland goto out;
2666ca35587Sdholland }
2676ca35587Sdholland }
2686ca35587Sdholland
2692d39560cSpgoyette cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp);
2706ca35587Sdholland NFSLOCKV4ROOTMUTEX();
2716ca35587Sdholland nfsv4_relref(&nfsd_suspend_lock);
2726ca35587Sdholland NFSUNLOCKV4ROOTMUTEX();
2736ca35587Sdholland } else {
2746ca35587Sdholland NFSMGET(nd.nd_mreq);
2756ca35587Sdholland nd.nd_mreq->m_len = 0;
2766ca35587Sdholland cacherep = RC_REPLY;
2776ca35587Sdholland }
2786ca35587Sdholland m_freem(nd.nd_mrep);
2796ca35587Sdholland
2806ca35587Sdholland if (nd.nd_cred != NULL)
2816ca35587Sdholland crfree(nd.nd_cred);
2826ca35587Sdholland
2836ca35587Sdholland if (cacherep == RC_DROPIT) {
2846ca35587Sdholland m_freem(nd.nd_mreq);
2856ca35587Sdholland svc_freereq(rqst);
2866ca35587Sdholland goto out;
2876ca35587Sdholland }
2886ca35587Sdholland
2896ca35587Sdholland if (nd.nd_mreq == NULL) {
2906ca35587Sdholland svcerr_decode(rqst);
2916ca35587Sdholland svc_freereq(rqst);
2926ca35587Sdholland goto out;
2936ca35587Sdholland }
2946ca35587Sdholland
2956ca35587Sdholland if (nd.nd_repstat & NFSERR_AUTHERR) {
2966ca35587Sdholland svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
2976ca35587Sdholland m_freem(nd.nd_mreq);
2986ca35587Sdholland } else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) {
2996ca35587Sdholland svcerr_systemerr(rqst);
3006ca35587Sdholland }
3012d39560cSpgoyette if (rp != NULL) {
3022d39560cSpgoyette nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 ||
3032d39560cSpgoyette SVC_ACK(xprt, NULL)), rqst->rq_reply_seq);
3042d39560cSpgoyette }
3056ca35587Sdholland svc_freereq(rqst);
3066ca35587Sdholland
3076ca35587Sdholland out:
3082d39560cSpgoyette if (softdep_ast_cleanup != NULL)
3092d39560cSpgoyette softdep_ast_cleanup();
3106ca35587Sdholland NFSEXITCODE(0);
3116ca35587Sdholland }
3126ca35587Sdholland
3136ca35587Sdholland /*
3146ca35587Sdholland * Check the cache and, optionally, do the RPC.
3156ca35587Sdholland * Return the appropriate cache response.
3166ca35587Sdholland */
3176ca35587Sdholland static int
nfs_proc(struct nfsrv_descript * nd,u_int32_t xid,SVCXPRT * xprt,struct nfsrvcache ** rpp)3182d39560cSpgoyette nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt,
3192d39560cSpgoyette struct nfsrvcache **rpp)
3206ca35587Sdholland {
3216ca35587Sdholland struct thread *td = curthread;
3222d39560cSpgoyette int cacherep = RC_DOIT, isdgram, taglen = -1;
3232d39560cSpgoyette struct mbuf *m;
3242d39560cSpgoyette u_char tag[NFSV4_SMALLSTR + 1], *tagstr = NULL;
3252d39560cSpgoyette u_int32_t minorvers = 0;
3262d39560cSpgoyette uint32_t ack;
3276ca35587Sdholland
3286ca35587Sdholland *rpp = NULL;
3296ca35587Sdholland if (nd->nd_nam2 == NULL) {
3306ca35587Sdholland nd->nd_flag |= ND_STREAMSOCK;
3316ca35587Sdholland isdgram = 0;
3326ca35587Sdholland } else {
3336ca35587Sdholland isdgram = 1;
3346ca35587Sdholland }
3356ca35587Sdholland
3366ca35587Sdholland /*
3376ca35587Sdholland * Two cases:
3386ca35587Sdholland * 1 - For NFSv2 over UDP, if we are near our malloc/mget
3396ca35587Sdholland * limit, just drop the request. There is no
3406ca35587Sdholland * NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the
3416ca35587Sdholland * client will timeout/retry over UDP in a little while.
3426ca35587Sdholland * 2 - nd_repstat == 0 && nd_mreq == NULL, which
3436ca35587Sdholland * means a normal nfs rpc, so check the cache
3446ca35587Sdholland */
3456ca35587Sdholland if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL &&
3466ca35587Sdholland nfsrv_mallocmget_limit()) {
3476ca35587Sdholland cacherep = RC_DROPIT;
3486ca35587Sdholland } else {
3496ca35587Sdholland /*
3506ca35587Sdholland * For NFSv3, play it safe and assume that the client is
3516ca35587Sdholland * doing retries on the same TCP connection.
3526ca35587Sdholland */
3536ca35587Sdholland if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) ==
3546ca35587Sdholland ND_STREAMSOCK)
3556ca35587Sdholland nd->nd_flag |= ND_SAMETCPCONN;
3566ca35587Sdholland nd->nd_retxid = xid;
3576ca35587Sdholland nd->nd_tcpconntime = NFSD_MONOSEC;
3582d39560cSpgoyette nd->nd_sockref = xprt->xp_sockref;
3592d39560cSpgoyette if ((nd->nd_flag & ND_NFSV4) != 0)
3602d39560cSpgoyette nfsd_getminorvers(nd, tag, &tagstr, &taglen,
3612d39560cSpgoyette &minorvers);
3622d39560cSpgoyette if ((nd->nd_flag & ND_NFSV41) != 0)
3632d39560cSpgoyette /* NFSv4.1 caches replies in the session slots. */
3642d39560cSpgoyette cacherep = RC_DOIT;
3652d39560cSpgoyette else {
3662d39560cSpgoyette cacherep = nfsrvd_getcache(nd);
3672d39560cSpgoyette ack = 0;
3682d39560cSpgoyette SVC_ACK(xprt, &ack);
3692d39560cSpgoyette nfsrc_trimcache(xprt->xp_sockref, ack, 0);
3702d39560cSpgoyette }
3716ca35587Sdholland }
3726ca35587Sdholland
3736ca35587Sdholland /*
3746ca35587Sdholland * Handle the request. There are three cases.
3756ca35587Sdholland * RC_DOIT - do the RPC
3766ca35587Sdholland * RC_REPLY - return the reply already created
3776ca35587Sdholland * RC_DROPIT - just throw the request away
3786ca35587Sdholland */
3796ca35587Sdholland if (cacherep == RC_DOIT) {
3802d39560cSpgoyette if ((nd->nd_flag & ND_NFSV41) != 0)
3812d39560cSpgoyette nd->nd_xprt = xprt;
3822d39560cSpgoyette nfsrvd_dorpc(nd, isdgram, tagstr, taglen, minorvers, td);
3832d39560cSpgoyette if ((nd->nd_flag & ND_NFSV41) != 0) {
3842d39560cSpgoyette if (nd->nd_repstat != NFSERR_REPLYFROMCACHE &&
3852d39560cSpgoyette (nd->nd_flag & ND_SAVEREPLY) != 0) {
3862d39560cSpgoyette /* Cache a copy of the reply. */
3872d39560cSpgoyette m = m_copym(nd->nd_mreq, 0, M_COPYALL,
3882d39560cSpgoyette M_WAITOK);
3892d39560cSpgoyette } else
3902d39560cSpgoyette m = NULL;
3912d39560cSpgoyette if ((nd->nd_flag & ND_HASSEQUENCE) != 0)
3922d39560cSpgoyette nfsrv_cache_session(nd->nd_sessionid,
3932d39560cSpgoyette nd->nd_slotid, nd->nd_repstat, &m);
3942d39560cSpgoyette if (nd->nd_repstat == NFSERR_REPLYFROMCACHE)
3952d39560cSpgoyette nd->nd_repstat = 0;
3962d39560cSpgoyette cacherep = RC_REPLY;
3972d39560cSpgoyette } else {
3986ca35587Sdholland if (nd->nd_repstat == NFSERR_DONTREPLY)
3996ca35587Sdholland cacherep = RC_DROPIT;
4006ca35587Sdholland else
4016ca35587Sdholland cacherep = RC_REPLY;
4022d39560cSpgoyette *rpp = nfsrvd_updatecache(nd);
4036ca35587Sdholland }
4042d39560cSpgoyette }
4052d39560cSpgoyette if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
4062d39560cSpgoyette free(tagstr, M_TEMP);
4076ca35587Sdholland
4086ca35587Sdholland NFSEXITCODE2(0, nd);
4096ca35587Sdholland return (cacherep);
4106ca35587Sdholland }
4116ca35587Sdholland
4122d39560cSpgoyette static void
nfssvc_loss(SVCXPRT * xprt)4132d39560cSpgoyette nfssvc_loss(SVCXPRT *xprt)
4142d39560cSpgoyette {
4152d39560cSpgoyette uint32_t ack;
4162d39560cSpgoyette
4172d39560cSpgoyette ack = 0;
4182d39560cSpgoyette SVC_ACK(xprt, &ack);
4192d39560cSpgoyette nfsrc_trimcache(xprt->xp_sockref, ack, 1);
4202d39560cSpgoyette }
4212d39560cSpgoyette
4226ca35587Sdholland /*
4236ca35587Sdholland * Adds a socket to the list for servicing by nfsds.
4246ca35587Sdholland */
4256ca35587Sdholland int
nfsrvd_addsock(struct file * fp)4266ca35587Sdholland nfsrvd_addsock(struct file *fp)
4276ca35587Sdholland {
4286ca35587Sdholland int siz;
4296ca35587Sdholland struct socket *so;
4306ca35587Sdholland int error = 0;
4316ca35587Sdholland SVCXPRT *xprt;
4326ca35587Sdholland static u_int64_t sockref = 0;
4336ca35587Sdholland
4346ca35587Sdholland so = fp->f_data;
4356ca35587Sdholland
4366ca35587Sdholland siz = sb_max_adj;
4376ca35587Sdholland error = soreserve(so, siz, siz);
4386ca35587Sdholland if (error)
4396ca35587Sdholland goto out;
4406ca35587Sdholland
4416ca35587Sdholland /*
4426ca35587Sdholland * Steal the socket from userland so that it doesn't close
4436ca35587Sdholland * unexpectedly.
4446ca35587Sdholland */
4456ca35587Sdholland if (so->so_type == SOCK_DGRAM)
4466ca35587Sdholland xprt = svc_dg_create(nfsrvd_pool, so, 0, 0);
4476ca35587Sdholland else
4486ca35587Sdholland xprt = svc_vc_create(nfsrvd_pool, so, 0, 0);
4496ca35587Sdholland if (xprt) {
4506ca35587Sdholland fp->f_ops = &badfileops;
4516ca35587Sdholland fp->f_data = NULL;
4526ca35587Sdholland xprt->xp_sockref = ++sockref;
4536ca35587Sdholland if (nfs_minvers == NFS_VER2)
4546ca35587Sdholland svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program,
4556ca35587Sdholland NULL);
4566ca35587Sdholland if (nfs_minvers <= NFS_VER3 && nfs_maxvers >= NFS_VER3)
4576ca35587Sdholland svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program,
4586ca35587Sdholland NULL);
4596ca35587Sdholland if (nfs_maxvers >= NFS_VER4)
4606ca35587Sdholland svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program,
4616ca35587Sdholland NULL);
4622d39560cSpgoyette if (so->so_type == SOCK_STREAM)
4632d39560cSpgoyette svc_loss_reg(xprt, nfssvc_loss);
4646ca35587Sdholland SVC_RELEASE(xprt);
4656ca35587Sdholland }
4666ca35587Sdholland
4676ca35587Sdholland out:
4686ca35587Sdholland NFSEXITCODE(error);
4696ca35587Sdholland return (error);
4706ca35587Sdholland }
4716ca35587Sdholland
4726ca35587Sdholland /*
4736ca35587Sdholland * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
4746ca35587Sdholland * until it is killed by a signal.
4756ca35587Sdholland */
4766ca35587Sdholland int
nfsrvd_nfsd(struct thread * td,struct nfsd_nfsd_args * args)4776ca35587Sdholland nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
4786ca35587Sdholland {
4796ca35587Sdholland char principal[MAXHOSTNAMELEN + 5];
4802d39560cSpgoyette struct proc *p;
4816ca35587Sdholland int error = 0;
4826ca35587Sdholland bool_t ret2, ret3, ret4;
4836ca35587Sdholland
4846ca35587Sdholland error = copyinstr(args->principal, principal, sizeof (principal),
4856ca35587Sdholland NULL);
4866ca35587Sdholland if (error)
4876ca35587Sdholland goto out;
4886ca35587Sdholland
4896ca35587Sdholland /*
4906ca35587Sdholland * Only the first nfsd actually does any work. The RPC code
4916ca35587Sdholland * adds threads to it as needed. Any extra processes offered
4926ca35587Sdholland * by nfsd just exit. If nfsd is new enough, it will call us
4936ca35587Sdholland * once with a structure that specifies how many threads to
4946ca35587Sdholland * use.
4956ca35587Sdholland */
4966ca35587Sdholland NFSD_LOCK();
4976ca35587Sdholland if (newnfs_numnfsd == 0) {
4982d39560cSpgoyette p = td->td_proc;
4992d39560cSpgoyette PROC_LOCK(p);
5002d39560cSpgoyette p->p_flag2 |= P2_AST_SU;
5012d39560cSpgoyette PROC_UNLOCK(p);
5026ca35587Sdholland newnfs_numnfsd++;
5036ca35587Sdholland
5046ca35587Sdholland NFSD_UNLOCK();
5056ca35587Sdholland
5066ca35587Sdholland /* An empty string implies AUTH_SYS only. */
5076ca35587Sdholland if (principal[0] != '\0') {
5086ca35587Sdholland ret2 = rpc_gss_set_svc_name_call(principal,
5096ca35587Sdholland "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER2);
5106ca35587Sdholland ret3 = rpc_gss_set_svc_name_call(principal,
5116ca35587Sdholland "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER3);
5126ca35587Sdholland ret4 = rpc_gss_set_svc_name_call(principal,
5136ca35587Sdholland "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER4);
5146ca35587Sdholland
5156ca35587Sdholland if (!ret2 || !ret3 || !ret4)
5166ca35587Sdholland printf("nfsd: can't register svc name\n");
5176ca35587Sdholland }
5186ca35587Sdholland
5196ca35587Sdholland nfsrvd_pool->sp_minthreads = args->minthreads;
5206ca35587Sdholland nfsrvd_pool->sp_maxthreads = args->maxthreads;
5216ca35587Sdholland
5226ca35587Sdholland svc_run(nfsrvd_pool);
5236ca35587Sdholland
5246ca35587Sdholland if (principal[0] != '\0') {
5256ca35587Sdholland rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
5266ca35587Sdholland rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
5276ca35587Sdholland rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4);
5286ca35587Sdholland }
5296ca35587Sdholland
5306ca35587Sdholland NFSD_LOCK();
5316ca35587Sdholland newnfs_numnfsd--;
5326ca35587Sdholland nfsrvd_init(1);
5332d39560cSpgoyette PROC_LOCK(p);
5342d39560cSpgoyette p->p_flag2 &= ~P2_AST_SU;
5352d39560cSpgoyette PROC_UNLOCK(p);
5366ca35587Sdholland }
5376ca35587Sdholland NFSD_UNLOCK();
5386ca35587Sdholland
5396ca35587Sdholland out:
5406ca35587Sdholland NFSEXITCODE(error);
5416ca35587Sdholland return (error);
5426ca35587Sdholland }
5436ca35587Sdholland
5446ca35587Sdholland /*
5456ca35587Sdholland * Initialize the data structures for the server.
5466ca35587Sdholland * Handshake with any new nfsds starting up to avoid any chance of
5476ca35587Sdholland * corruption.
5486ca35587Sdholland */
5496ca35587Sdholland void
nfsrvd_init(int terminating)5506ca35587Sdholland nfsrvd_init(int terminating)
5516ca35587Sdholland {
5526ca35587Sdholland
5536ca35587Sdholland NFSD_LOCK_ASSERT();
5546ca35587Sdholland
5556ca35587Sdholland if (terminating) {
5566ca35587Sdholland nfsd_master_proc = NULL;
5576ca35587Sdholland NFSD_UNLOCK();
5582d39560cSpgoyette nfsrv_freeallbackchannel_xprts();
5596ca35587Sdholland svcpool_destroy(nfsrvd_pool);
5606ca35587Sdholland nfsrvd_pool = NULL;
5616ca35587Sdholland NFSD_LOCK();
5626ca35587Sdholland }
5636ca35587Sdholland
5646ca35587Sdholland NFSD_UNLOCK();
5656ca35587Sdholland
5666ca35587Sdholland nfsrvd_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsd));
5676ca35587Sdholland nfsrvd_pool->sp_rcache = NULL;
5686ca35587Sdholland nfsrvd_pool->sp_assign = fhanew_assign;
5696ca35587Sdholland nfsrvd_pool->sp_done = fha_nd_complete;
5706ca35587Sdholland
5716ca35587Sdholland NFSD_LOCK();
5726ca35587Sdholland }
5736ca35587Sdholland
574