19ec7b004SRick Macklem /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 49ec7b004SRick Macklem * Copyright (c) 1989, 1993 59ec7b004SRick Macklem * The Regents of the University of California. All rights reserved. 69ec7b004SRick Macklem * 79ec7b004SRick Macklem * This code is derived from software contributed to Berkeley by 89ec7b004SRick Macklem * Rick Macklem at The University of Guelph. 99ec7b004SRick Macklem * 109ec7b004SRick Macklem * Redistribution and use in source and binary forms, with or without 119ec7b004SRick Macklem * modification, are permitted provided that the following conditions 129ec7b004SRick Macklem * are met: 139ec7b004SRick Macklem * 1. Redistributions of source code must retain the above copyright 149ec7b004SRick Macklem * notice, this list of conditions and the following disclaimer. 159ec7b004SRick Macklem * 2. Redistributions in binary form must reproduce the above copyright 169ec7b004SRick Macklem * notice, this list of conditions and the following disclaimer in the 179ec7b004SRick Macklem * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 199ec7b004SRick Macklem * may be used to endorse or promote products derived from this software 209ec7b004SRick Macklem * without specific prior written permission. 219ec7b004SRick Macklem * 229ec7b004SRick Macklem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 239ec7b004SRick Macklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 249ec7b004SRick Macklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 259ec7b004SRick Macklem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 269ec7b004SRick Macklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 279ec7b004SRick Macklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 289ec7b004SRick Macklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 299ec7b004SRick Macklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 309ec7b004SRick Macklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 319ec7b004SRick Macklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 329ec7b004SRick Macklem * SUCH DAMAGE. 339ec7b004SRick Macklem * 349ec7b004SRick Macklem */ 359ec7b004SRick Macklem 369ec7b004SRick Macklem #include <sys/cdefs.h> 379ec7b004SRick Macklem __FBSDID("$FreeBSD$"); 389ec7b004SRick Macklem 399ec7b004SRick Macklem #include "opt_inet6.h" 409ec7b004SRick Macklem #include "opt_kgssapi.h" 419ec7b004SRick Macklem 429ec7b004SRick Macklem #include <fs/nfs/nfsport.h> 439ec7b004SRick Macklem 449ec7b004SRick Macklem #include <rpc/rpc.h> 459ec7b004SRick Macklem #include <rpc/rpcsec_gss.h> 469ec7b004SRick Macklem 47adb97406SKenneth D. Merry #include <nfs/nfs_fha.h> 48d96b98a3SKenneth D. Merry #include <fs/nfsserver/nfs_fha_new.h> 49d96b98a3SKenneth D. Merry 50bcf11e8dSRobert Watson #include <security/mac/mac_framework.h> 51bcf11e8dSRobert Watson 529ec7b004SRick Macklem NFSDLOCKMUTEX; 536001db29SRick Macklem NFSV4ROOTLOCKMUTEX; 546001db29SRick Macklem struct nfsv4lock nfsd_suspend_lock; 55acc6e58dSRick Macklem char *nfsrv_zeropnfsdat = NULL; 569ec7b004SRick Macklem 579ec7b004SRick Macklem /* 589ec7b004SRick Macklem * Mapping of old NFS Version 2 RPC numbers to generic numbers. 599ec7b004SRick Macklem */ 60d96b98a3SKenneth D. Merry int newnfs_nfsv3_procid[NFS_V3NPROCS] = { 619ec7b004SRick Macklem NFSPROC_NULL, 629ec7b004SRick Macklem NFSPROC_GETATTR, 639ec7b004SRick Macklem NFSPROC_SETATTR, 649ec7b004SRick Macklem NFSPROC_NOOP, 659ec7b004SRick Macklem NFSPROC_LOOKUP, 669ec7b004SRick Macklem NFSPROC_READLINK, 679ec7b004SRick Macklem NFSPROC_READ, 689ec7b004SRick Macklem NFSPROC_NOOP, 699ec7b004SRick Macklem NFSPROC_WRITE, 709ec7b004SRick Macklem NFSPROC_CREATE, 719ec7b004SRick Macklem NFSPROC_REMOVE, 729ec7b004SRick Macklem NFSPROC_RENAME, 739ec7b004SRick Macklem NFSPROC_LINK, 749ec7b004SRick Macklem NFSPROC_SYMLINK, 759ec7b004SRick Macklem NFSPROC_MKDIR, 769ec7b004SRick Macklem NFSPROC_RMDIR, 779ec7b004SRick Macklem NFSPROC_READDIR, 789ec7b004SRick Macklem NFSPROC_FSSTAT, 799ec7b004SRick Macklem NFSPROC_NOOP, 809ec7b004SRick Macklem NFSPROC_NOOP, 819ec7b004SRick Macklem NFSPROC_NOOP, 829ec7b004SRick Macklem NFSPROC_NOOP, 839ec7b004SRick Macklem }; 849ec7b004SRick Macklem 859ec7b004SRick Macklem 86a0c2c369SRick Macklem SYSCTL_DECL(_vfs_nfsd); 879ec7b004SRick Macklem 889ec7b004SRick Macklem SVCPOOL *nfsrvd_pool; 899ec7b004SRick Macklem 909ec7b004SRick Macklem static int nfs_privport = 0; 9142ecb595SKonstantin Belousov SYSCTL_INT(_vfs_nfsd, OID_AUTO, nfs_privport, CTLFLAG_RWTUN, 929ec7b004SRick Macklem &nfs_privport, 0, 9323114c6cSRick Macklem "Only allow clients using a privileged port for NFSv2, 3 and 4"); 949ec7b004SRick Macklem 9510a5a16eSRick Macklem static int nfs_minvers = NFS_VER2; 9642ecb595SKonstantin Belousov SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_min_nfsvers, CTLFLAG_RWTUN, 9710a5a16eSRick Macklem &nfs_minvers, 0, "The lowest version of NFS handled by the server"); 9810a5a16eSRick Macklem 9910a5a16eSRick Macklem static int nfs_maxvers = NFS_VER4; 10042ecb595SKonstantin Belousov SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_max_nfsvers, CTLFLAG_RWTUN, 10110a5a16eSRick Macklem &nfs_maxvers, 0, "The highest version of NFS handled by the server"); 10210a5a16eSRick Macklem 103d473bac7SAlexander Motin static int nfs_proc(struct nfsrv_descript *, u_int32_t, SVCXPRT *xprt, 104d473bac7SAlexander Motin struct nfsrvcache **); 1059ec7b004SRick Macklem 1069ec7b004SRick Macklem extern u_long sb_max_adj; 1079ec7b004SRick Macklem extern int newnfs_numnfsd; 1085f73287aSRick Macklem extern struct proc *nfsd_master_proc; 10990d2dfabSRick Macklem extern time_t nfsdev_time; 1103e5ba2e1SRick Macklem extern int nfsrv_writerpc[NFS_NPROCS]; 1113e5ba2e1SRick Macklem extern volatile int nfsrv_devidcnt; 1123e5ba2e1SRick Macklem extern struct nfsv4_opflag nfsv4_opflag[NFSV41_NOPS]; 1139ec7b004SRick Macklem 1149ec7b004SRick Macklem /* 1159ec7b004SRick Macklem * NFS server system calls 1169ec7b004SRick Macklem */ 1179ec7b004SRick Macklem 1189ec7b004SRick Macklem static void 1199ec7b004SRick Macklem nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt) 1209ec7b004SRick Macklem { 1219ec7b004SRick Macklem struct nfsrv_descript nd; 1229ec7b004SRick Macklem struct nfsrvcache *rp = NULL; 12398ad4453SRick Macklem int cacherep, credflavor; 1249ec7b004SRick Macklem 1259ec7b004SRick Macklem memset(&nd, 0, sizeof(nd)); 1269ec7b004SRick Macklem if (rqst->rq_vers == NFS_VER2) { 1272fbb9d56SRick Macklem if (rqst->rq_proc > NFSV2PROC_STATFS || 1282fbb9d56SRick Macklem newnfs_nfsv3_procid[rqst->rq_proc] == NFSPROC_NOOP) { 1299ec7b004SRick Macklem svcerr_noproc(rqst); 1309ec7b004SRick Macklem svc_freereq(rqst); 131a9285ae5SZack Kirsch goto out; 1329ec7b004SRick Macklem } 1339ec7b004SRick Macklem nd.nd_procnum = newnfs_nfsv3_procid[rqst->rq_proc]; 1349ec7b004SRick Macklem nd.nd_flag = ND_NFSV2; 1359ec7b004SRick Macklem } else if (rqst->rq_vers == NFS_VER3) { 1369ec7b004SRick Macklem if (rqst->rq_proc >= NFS_V3NPROCS) { 1379ec7b004SRick Macklem svcerr_noproc(rqst); 1389ec7b004SRick Macklem svc_freereq(rqst); 139a9285ae5SZack Kirsch goto out; 1409ec7b004SRick Macklem } 1419ec7b004SRick Macklem nd.nd_procnum = rqst->rq_proc; 1429ec7b004SRick Macklem nd.nd_flag = ND_NFSV3; 1439ec7b004SRick Macklem } else { 1449ec7b004SRick Macklem if (rqst->rq_proc != NFSPROC_NULL && 1459ec7b004SRick Macklem rqst->rq_proc != NFSV4PROC_COMPOUND) { 1469ec7b004SRick Macklem svcerr_noproc(rqst); 1479ec7b004SRick Macklem svc_freereq(rqst); 148a9285ae5SZack Kirsch goto out; 1499ec7b004SRick Macklem } 1509ec7b004SRick Macklem nd.nd_procnum = rqst->rq_proc; 1519ec7b004SRick Macklem nd.nd_flag = ND_NFSV4; 1529ec7b004SRick Macklem } 1539ec7b004SRick Macklem 1549ec7b004SRick Macklem /* 1559ec7b004SRick Macklem * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 - 1569ec7b004SRick Macklem * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP 1579ec7b004SRick Macklem * mounts. 1589ec7b004SRick Macklem */ 1599ec7b004SRick Macklem nd.nd_mrep = rqst->rq_args; 1609ec7b004SRick Macklem rqst->rq_args = NULL; 161d96b98a3SKenneth D. Merry newnfs_realign(&nd.nd_mrep, M_WAITOK); 1629ec7b004SRick Macklem nd.nd_md = nd.nd_mrep; 1639ec7b004SRick Macklem nd.nd_dpos = mtod(nd.nd_md, caddr_t); 1649ec7b004SRick Macklem nd.nd_nam = svc_getrpccaller(rqst); 1659ec7b004SRick Macklem nd.nd_nam2 = rqst->rq_addr; 1669ec7b004SRick Macklem nd.nd_mreq = NULL; 1679ec7b004SRick Macklem nd.nd_cred = NULL; 1689ec7b004SRick Macklem 16923114c6cSRick Macklem if (nfs_privport != 0) { 1709ec7b004SRick Macklem /* Check if source port is privileged */ 1719ec7b004SRick Macklem u_short port; 1729ec7b004SRick Macklem struct sockaddr *nam = nd.nd_nam; 1739ec7b004SRick Macklem struct sockaddr_in *sin; 1749ec7b004SRick Macklem 1759ec7b004SRick Macklem sin = (struct sockaddr_in *)nam; 1769ec7b004SRick Macklem /* 1779ec7b004SRick Macklem * INET/INET6 - same code: 1789ec7b004SRick Macklem * sin_port and sin6_port are at same offset 1799ec7b004SRick Macklem */ 1809ec7b004SRick Macklem port = ntohs(sin->sin_port); 1819ec7b004SRick Macklem if (port >= IPPORT_RESERVED && 1829ec7b004SRick Macklem nd.nd_procnum != NFSPROC_NULL) { 1839ec7b004SRick Macklem #ifdef INET6 1848144690aSEric van Gyzen char buf[INET6_ADDRSTRLEN]; 1858144690aSEric van Gyzen #else 1868144690aSEric van Gyzen char buf[INET_ADDRSTRLEN]; 1878144690aSEric van Gyzen #endif 1888144690aSEric van Gyzen #ifdef INET6 1899ec7b004SRick Macklem #if defined(KLD_MODULE) 1909ec7b004SRick Macklem /* Do not use ip6_sprintf: the nfs module should work without INET6. */ 1919ec7b004SRick Macklem #define ip6_sprintf(buf, a) \ 1929ec7b004SRick Macklem (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x", \ 1939ec7b004SRick Macklem (a)->s6_addr16[0], (a)->s6_addr16[1], \ 1949ec7b004SRick Macklem (a)->s6_addr16[2], (a)->s6_addr16[3], \ 1959ec7b004SRick Macklem (a)->s6_addr16[4], (a)->s6_addr16[5], \ 1969ec7b004SRick Macklem (a)->s6_addr16[6], (a)->s6_addr16[7]), \ 1979ec7b004SRick Macklem (buf)) 1989ec7b004SRick Macklem #endif 1999ec7b004SRick Macklem #endif 2009ec7b004SRick Macklem printf("NFS request from unprivileged port (%s:%d)\n", 2019ec7b004SRick Macklem #ifdef INET6 2029ec7b004SRick Macklem sin->sin_family == AF_INET6 ? 2038144690aSEric van Gyzen ip6_sprintf(buf, &satosin6(sin)->sin6_addr) : 2049ec7b004SRick Macklem #if defined(KLD_MODULE) 2059ec7b004SRick Macklem #undef ip6_sprintf 2069ec7b004SRick Macklem #endif 2079ec7b004SRick Macklem #endif 2088144690aSEric van Gyzen inet_ntoa_r(sin->sin_addr, buf), port); 2099ec7b004SRick Macklem svcerr_weakauth(rqst); 2109ec7b004SRick Macklem svc_freereq(rqst); 2119ec7b004SRick Macklem m_freem(nd.nd_mrep); 212a9285ae5SZack Kirsch goto out; 2139ec7b004SRick Macklem } 2149ec7b004SRick Macklem } 2159ec7b004SRick Macklem 2169ec7b004SRick Macklem if (nd.nd_procnum != NFSPROC_NULL) { 21798ad4453SRick Macklem if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) { 2189ec7b004SRick Macklem svcerr_weakauth(rqst); 2199ec7b004SRick Macklem svc_freereq(rqst); 2209ec7b004SRick Macklem m_freem(nd.nd_mrep); 221a9285ae5SZack Kirsch goto out; 2229ec7b004SRick Macklem } 22398ad4453SRick Macklem 22498ad4453SRick Macklem /* Set the flag based on credflavor */ 22598ad4453SRick Macklem if (credflavor == RPCSEC_GSS_KRB5) { 22698ad4453SRick Macklem nd.nd_flag |= ND_GSS; 22798ad4453SRick Macklem } else if (credflavor == RPCSEC_GSS_KRB5I) { 22898ad4453SRick Macklem nd.nd_flag |= (ND_GSS | ND_GSSINTEGRITY); 22998ad4453SRick Macklem } else if (credflavor == RPCSEC_GSS_KRB5P) { 23098ad4453SRick Macklem nd.nd_flag |= (ND_GSS | ND_GSSPRIVACY); 23198ad4453SRick Macklem } else if (credflavor != AUTH_SYS) { 23298ad4453SRick Macklem svcerr_weakauth(rqst); 23398ad4453SRick Macklem svc_freereq(rqst); 23498ad4453SRick Macklem m_freem(nd.nd_mrep); 235a9285ae5SZack Kirsch goto out; 23698ad4453SRick Macklem } 23798ad4453SRick Macklem 2389ec7b004SRick Macklem #ifdef MAC 2399ec7b004SRick Macklem mac_cred_associate_nfsd(nd.nd_cred); 2409ec7b004SRick Macklem #endif 2416001db29SRick Macklem /* 2426001db29SRick Macklem * Get a refcnt (shared lock) on nfsd_suspend_lock. 2436001db29SRick Macklem * NFSSVC_SUSPENDNFSD will take an exclusive lock on 2446001db29SRick Macklem * nfsd_suspend_lock to suspend these threads. 245ee58b564SPedro F. Giffuni * The call to nfsv4_lock() that precedes nfsv4_getref() 2468eabbbe2SRick Macklem * ensures that the acquisition of the exclusive lock 2478eabbbe2SRick Macklem * takes priority over acquisition of the shared lock by 2488eabbbe2SRick Macklem * waiting for any exclusive lock request to complete. 2496001db29SRick Macklem * This must be done here, before the check of 2506001db29SRick Macklem * nfsv4root exports by nfsvno_v4rootexport(). 2516001db29SRick Macklem */ 2526001db29SRick Macklem NFSLOCKV4ROOTMUTEX(); 2538eabbbe2SRick Macklem nfsv4_lock(&nfsd_suspend_lock, 0, NULL, NFSV4ROOTLOCKMUTEXPTR, 2548eabbbe2SRick Macklem NULL); 2556001db29SRick Macklem nfsv4_getref(&nfsd_suspend_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, 2566001db29SRick Macklem NULL); 2576001db29SRick Macklem NFSUNLOCKV4ROOTMUTEX(); 2586001db29SRick Macklem 259415670e4SRick Macklem if ((nd.nd_flag & ND_NFSV4) != 0) { 2609ec7b004SRick Macklem nd.nd_repstat = nfsvno_v4rootexport(&nd); 261415670e4SRick Macklem if (nd.nd_repstat != 0) { 2626001db29SRick Macklem NFSLOCKV4ROOTMUTEX(); 2636001db29SRick Macklem nfsv4_relref(&nfsd_suspend_lock); 2646001db29SRick Macklem NFSUNLOCKV4ROOTMUTEX(); 265415670e4SRick Macklem svcerr_weakauth(rqst); 266415670e4SRick Macklem svc_freereq(rqst); 267415670e4SRick Macklem m_freem(nd.nd_mrep); 268a9285ae5SZack Kirsch goto out; 269415670e4SRick Macklem } 270415670e4SRick Macklem } 2719ec7b004SRick Macklem 272d473bac7SAlexander Motin cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp); 2736001db29SRick Macklem NFSLOCKV4ROOTMUTEX(); 2746001db29SRick Macklem nfsv4_relref(&nfsd_suspend_lock); 2756001db29SRick Macklem NFSUNLOCKV4ROOTMUTEX(); 2769ec7b004SRick Macklem } else { 2779ec7b004SRick Macklem NFSMGET(nd.nd_mreq); 2789ec7b004SRick Macklem nd.nd_mreq->m_len = 0; 2799ec7b004SRick Macklem cacherep = RC_REPLY; 2809ec7b004SRick Macklem } 2819ec7b004SRick Macklem if (nd.nd_mrep != NULL) 2829ec7b004SRick Macklem m_freem(nd.nd_mrep); 2839ec7b004SRick Macklem 2849ec7b004SRick Macklem if (nd.nd_cred != NULL) 2859ec7b004SRick Macklem crfree(nd.nd_cred); 2869ec7b004SRick Macklem 2879ec7b004SRick Macklem if (cacherep == RC_DROPIT) { 2889ec7b004SRick Macklem if (nd.nd_mreq != NULL) 2899ec7b004SRick Macklem m_freem(nd.nd_mreq); 2909ec7b004SRick Macklem svc_freereq(rqst); 291a9285ae5SZack Kirsch goto out; 2929ec7b004SRick Macklem } 2939ec7b004SRick Macklem 2949ec7b004SRick Macklem if (nd.nd_mreq == NULL) { 2959ec7b004SRick Macklem svcerr_decode(rqst); 2969ec7b004SRick Macklem svc_freereq(rqst); 297a9285ae5SZack Kirsch goto out; 2989ec7b004SRick Macklem } 2999ec7b004SRick Macklem 3009ec7b004SRick Macklem if (nd.nd_repstat & NFSERR_AUTHERR) { 3019ec7b004SRick Macklem svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR); 3029ec7b004SRick Macklem if (nd.nd_mreq != NULL) 3039ec7b004SRick Macklem m_freem(nd.nd_mreq); 3049ec7b004SRick Macklem } else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) { 3059ec7b004SRick Macklem svcerr_systemerr(rqst); 3069ec7b004SRick Macklem } 307d473bac7SAlexander Motin if (rp != NULL) { 3086103bae6SAlexander Motin nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 || 3096103bae6SAlexander Motin SVC_ACK(xprt, NULL)), rqst->rq_reply_seq); 310d473bac7SAlexander Motin } 3119ec7b004SRick Macklem svc_freereq(rqst); 312a9285ae5SZack Kirsch 313a9285ae5SZack Kirsch out: 314aca4bb91SKonstantin Belousov td_softdep_cleanup(curthread); 315a9285ae5SZack Kirsch NFSEXITCODE(0); 3169ec7b004SRick Macklem } 3179ec7b004SRick Macklem 3189ec7b004SRick Macklem /* 3199ec7b004SRick Macklem * Check the cache and, optionally, do the RPC. 3209ec7b004SRick Macklem * Return the appropriate cache response. 3219ec7b004SRick Macklem */ 3229ec7b004SRick Macklem static int 323d473bac7SAlexander Motin nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt, 324d473bac7SAlexander Motin struct nfsrvcache **rpp) 3259ec7b004SRick Macklem { 326c59e4cc3SRick Macklem int cacherep = RC_DOIT, isdgram, taglen = -1; 327c59e4cc3SRick Macklem struct mbuf *m; 328c59e4cc3SRick Macklem u_char tag[NFSV4_SMALLSTR + 1], *tagstr = NULL; 329c59e4cc3SRick Macklem u_int32_t minorvers = 0; 330d473bac7SAlexander Motin uint32_t ack; 3319ec7b004SRick Macklem 3329ec7b004SRick Macklem *rpp = NULL; 3339ec7b004SRick Macklem if (nd->nd_nam2 == NULL) { 3349ec7b004SRick Macklem nd->nd_flag |= ND_STREAMSOCK; 3359ec7b004SRick Macklem isdgram = 0; 3369ec7b004SRick Macklem } else { 3379ec7b004SRick Macklem isdgram = 1; 3389ec7b004SRick Macklem } 3399ec7b004SRick Macklem 3409ec7b004SRick Macklem /* 341415670e4SRick Macklem * Two cases: 3429ec7b004SRick Macklem * 1 - For NFSv2 over UDP, if we are near our malloc/mget 3439ec7b004SRick Macklem * limit, just drop the request. There is no 3449ec7b004SRick Macklem * NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the 3459ec7b004SRick Macklem * client will timeout/retry over UDP in a little while. 346415670e4SRick Macklem * 2 - nd_repstat == 0 && nd_mreq == NULL, which 3479ec7b004SRick Macklem * means a normal nfs rpc, so check the cache 3489ec7b004SRick Macklem */ 3499ec7b004SRick Macklem if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL && 3509ec7b004SRick Macklem nfsrv_mallocmget_limit()) { 3519ec7b004SRick Macklem cacherep = RC_DROPIT; 3529ec7b004SRick Macklem } else { 3539ec7b004SRick Macklem /* 3549ec7b004SRick Macklem * For NFSv3, play it safe and assume that the client is 3559ec7b004SRick Macklem * doing retries on the same TCP connection. 3569ec7b004SRick Macklem */ 3579ec7b004SRick Macklem if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) == 3589ec7b004SRick Macklem ND_STREAMSOCK) 3599ec7b004SRick Macklem nd->nd_flag |= ND_SAMETCPCONN; 3609ec7b004SRick Macklem nd->nd_retxid = xid; 3619ec7b004SRick Macklem nd->nd_tcpconntime = NFSD_MONOSEC; 362d473bac7SAlexander Motin nd->nd_sockref = xprt->xp_sockref; 363c59e4cc3SRick Macklem if ((nd->nd_flag & ND_NFSV4) != 0) 364c59e4cc3SRick Macklem nfsd_getminorvers(nd, tag, &tagstr, &taglen, 365c59e4cc3SRick Macklem &minorvers); 366c59e4cc3SRick Macklem if ((nd->nd_flag & ND_NFSV41) != 0) 367c59e4cc3SRick Macklem /* NFSv4.1 caches replies in the session slots. */ 368c59e4cc3SRick Macklem cacherep = RC_DOIT; 369c59e4cc3SRick Macklem else { 370d473bac7SAlexander Motin cacherep = nfsrvd_getcache(nd); 371d473bac7SAlexander Motin ack = 0; 372d473bac7SAlexander Motin SVC_ACK(xprt, &ack); 373d473bac7SAlexander Motin nfsrc_trimcache(xprt->xp_sockref, ack, 0); 3749ec7b004SRick Macklem } 375c59e4cc3SRick Macklem } 3769ec7b004SRick Macklem 3779ec7b004SRick Macklem /* 3789ec7b004SRick Macklem * Handle the request. There are three cases. 3799ec7b004SRick Macklem * RC_DOIT - do the RPC 3809ec7b004SRick Macklem * RC_REPLY - return the reply already created 3819ec7b004SRick Macklem * RC_DROPIT - just throw the request away 3829ec7b004SRick Macklem */ 3839ec7b004SRick Macklem if (cacherep == RC_DOIT) { 384c59e4cc3SRick Macklem if ((nd->nd_flag & ND_NFSV41) != 0) 385c59e4cc3SRick Macklem nd->nd_xprt = xprt; 386*113aa933SEdward Tomasz Napierala nfsrvd_dorpc(nd, isdgram, tagstr, taglen, minorvers); 387c59e4cc3SRick Macklem if ((nd->nd_flag & ND_NFSV41) != 0) { 388c59e4cc3SRick Macklem if (nd->nd_repstat != NFSERR_REPLYFROMCACHE && 389c59e4cc3SRick Macklem (nd->nd_flag & ND_SAVEREPLY) != 0) { 390c59e4cc3SRick Macklem /* Cache a copy of the reply. */ 391c59e4cc3SRick Macklem m = m_copym(nd->nd_mreq, 0, M_COPYALL, 392c59e4cc3SRick Macklem M_WAITOK); 393c59e4cc3SRick Macklem } else 394c59e4cc3SRick Macklem m = NULL; 395c59e4cc3SRick Macklem if ((nd->nd_flag & ND_HASSEQUENCE) != 0) 396c59e4cc3SRick Macklem nfsrv_cache_session(nd->nd_sessionid, 397c59e4cc3SRick Macklem nd->nd_slotid, nd->nd_repstat, &m); 398c59e4cc3SRick Macklem if (nd->nd_repstat == NFSERR_REPLYFROMCACHE) 399c59e4cc3SRick Macklem nd->nd_repstat = 0; 400c59e4cc3SRick Macklem cacherep = RC_REPLY; 401c59e4cc3SRick Macklem } else { 4029ec7b004SRick Macklem if (nd->nd_repstat == NFSERR_DONTREPLY) 4039ec7b004SRick Macklem cacherep = RC_DROPIT; 4049ec7b004SRick Macklem else 4059ec7b004SRick Macklem cacherep = RC_REPLY; 406d473bac7SAlexander Motin *rpp = nfsrvd_updatecache(nd); 4079ec7b004SRick Macklem } 408c59e4cc3SRick Macklem } 409c59e4cc3SRick Macklem if (tagstr != NULL && taglen > NFSV4_SMALLSTR) 410c59e4cc3SRick Macklem free(tagstr, M_TEMP); 411a9285ae5SZack Kirsch 412a9285ae5SZack Kirsch NFSEXITCODE2(0, nd); 4139ec7b004SRick Macklem return (cacherep); 4149ec7b004SRick Macklem } 4159ec7b004SRick Macklem 416d473bac7SAlexander Motin static void 417d473bac7SAlexander Motin nfssvc_loss(SVCXPRT *xprt) 418d473bac7SAlexander Motin { 419d473bac7SAlexander Motin uint32_t ack; 420d473bac7SAlexander Motin 421d473bac7SAlexander Motin ack = 0; 422d473bac7SAlexander Motin SVC_ACK(xprt, &ack); 423d473bac7SAlexander Motin nfsrc_trimcache(xprt->xp_sockref, ack, 1); 424d473bac7SAlexander Motin } 425d473bac7SAlexander Motin 4269ec7b004SRick Macklem /* 4279ec7b004SRick Macklem * Adds a socket to the list for servicing by nfsds. 4289ec7b004SRick Macklem */ 4299ec7b004SRick Macklem int 4309ec7b004SRick Macklem nfsrvd_addsock(struct file *fp) 4319ec7b004SRick Macklem { 4329ec7b004SRick Macklem int siz; 4339ec7b004SRick Macklem struct socket *so; 434a9285ae5SZack Kirsch int error = 0; 4359ec7b004SRick Macklem SVCXPRT *xprt; 4369ec7b004SRick Macklem static u_int64_t sockref = 0; 4379ec7b004SRick Macklem 4389ec7b004SRick Macklem so = fp->f_data; 4399ec7b004SRick Macklem 4409ec7b004SRick Macklem siz = sb_max_adj; 4419ec7b004SRick Macklem error = soreserve(so, siz, siz); 442a9285ae5SZack Kirsch if (error) 443a9285ae5SZack Kirsch goto out; 4449ec7b004SRick Macklem 4459ec7b004SRick Macklem /* 4469ec7b004SRick Macklem * Steal the socket from userland so that it doesn't close 4479ec7b004SRick Macklem * unexpectedly. 4489ec7b004SRick Macklem */ 4499ec7b004SRick Macklem if (so->so_type == SOCK_DGRAM) 4509ec7b004SRick Macklem xprt = svc_dg_create(nfsrvd_pool, so, 0, 0); 4519ec7b004SRick Macklem else 4529ec7b004SRick Macklem xprt = svc_vc_create(nfsrvd_pool, so, 0, 0); 4539ec7b004SRick Macklem if (xprt) { 4549ec7b004SRick Macklem fp->f_ops = &badfileops; 4559ec7b004SRick Macklem fp->f_data = NULL; 4569ec7b004SRick Macklem xprt->xp_sockref = ++sockref; 45710a5a16eSRick Macklem if (nfs_minvers == NFS_VER2) 45810a5a16eSRick Macklem svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program, 45910a5a16eSRick Macklem NULL); 46010a5a16eSRick Macklem if (nfs_minvers <= NFS_VER3 && nfs_maxvers >= NFS_VER3) 46110a5a16eSRick Macklem svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program, 46210a5a16eSRick Macklem NULL); 46310a5a16eSRick Macklem if (nfs_maxvers >= NFS_VER4) 46410a5a16eSRick Macklem svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program, 46510a5a16eSRick Macklem NULL); 466d473bac7SAlexander Motin if (so->so_type == SOCK_STREAM) 467d473bac7SAlexander Motin svc_loss_reg(xprt, nfssvc_loss); 46876b30a0cSRick Macklem SVC_RELEASE(xprt); 4699ec7b004SRick Macklem } 4709ec7b004SRick Macklem 471a9285ae5SZack Kirsch out: 472a9285ae5SZack Kirsch NFSEXITCODE(error); 473a9285ae5SZack Kirsch return (error); 4749ec7b004SRick Macklem } 4759ec7b004SRick Macklem 4769ec7b004SRick Macklem /* 4779ec7b004SRick Macklem * Called by nfssvc() for nfsds. Just loops around servicing rpc requests 4789ec7b004SRick Macklem * until it is killed by a signal. 4799ec7b004SRick Macklem */ 4809ec7b004SRick Macklem int 4819ec7b004SRick Macklem nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args) 4829ec7b004SRick Macklem { 4837e745519SRick Macklem char principal[MAXHOSTNAMELEN + 5]; 4841bc93bb7SKonstantin Belousov struct proc *p; 485a9285ae5SZack Kirsch int error = 0; 4869ec7b004SRick Macklem bool_t ret2, ret3, ret4; 4879ec7b004SRick Macklem 4887e745519SRick Macklem error = copyinstr(args->principal, principal, sizeof (principal), 4897e745519SRick Macklem NULL); 4909ec7b004SRick Macklem if (error) 491a9285ae5SZack Kirsch goto out; 4929ec7b004SRick Macklem 4939ec7b004SRick Macklem /* 4949ec7b004SRick Macklem * Only the first nfsd actually does any work. The RPC code 4959ec7b004SRick Macklem * adds threads to it as needed. Any extra processes offered 4969ec7b004SRick Macklem * by nfsd just exit. If nfsd is new enough, it will call us 4979ec7b004SRick Macklem * once with a structure that specifies how many threads to 4989ec7b004SRick Macklem * use. 4999ec7b004SRick Macklem */ 5009ec7b004SRick Macklem NFSD_LOCK(); 5019ec7b004SRick Macklem if (newnfs_numnfsd == 0) { 50290d2dfabSRick Macklem nfsdev_time = time_second; 5031bc93bb7SKonstantin Belousov p = td->td_proc; 5041bc93bb7SKonstantin Belousov PROC_LOCK(p); 5051bc93bb7SKonstantin Belousov p->p_flag2 |= P2_AST_SU; 5061bc93bb7SKonstantin Belousov PROC_UNLOCK(p); 5079ec7b004SRick Macklem newnfs_numnfsd++; 5089ec7b004SRick Macklem 5099ec7b004SRick Macklem NFSD_UNLOCK(); 51090d2dfabSRick Macklem error = nfsrv_createdevids(args, td); 51190d2dfabSRick Macklem if (error == 0) { 5127e745519SRick Macklem /* An empty string implies AUTH_SYS only. */ 5137e745519SRick Macklem if (principal[0] != '\0') { 5147e7fd7d1SRick Macklem ret2 = rpc_gss_set_svc_name_call(principal, 51590d2dfabSRick Macklem "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, 51690d2dfabSRick Macklem NFS_VER2); 5177e7fd7d1SRick Macklem ret3 = rpc_gss_set_svc_name_call(principal, 51890d2dfabSRick Macklem "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, 51990d2dfabSRick Macklem NFS_VER3); 5207e7fd7d1SRick Macklem ret4 = rpc_gss_set_svc_name_call(principal, 52190d2dfabSRick Macklem "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, 52290d2dfabSRick Macklem NFS_VER4); 5239ec7b004SRick Macklem 5247e7fd7d1SRick Macklem if (!ret2 || !ret3 || !ret4) 52590d2dfabSRick Macklem printf( 52690d2dfabSRick Macklem "nfsd: can't register svc name\n"); 5279ec7b004SRick Macklem } 5289ec7b004SRick Macklem 5299ec7b004SRick Macklem nfsrvd_pool->sp_minthreads = args->minthreads; 5309ec7b004SRick Macklem nfsrvd_pool->sp_maxthreads = args->maxthreads; 5319ec7b004SRick Macklem 5323e5ba2e1SRick Macklem /* 5333e5ba2e1SRick Macklem * If this is a pNFS service, make Getattr do a 5343e5ba2e1SRick Macklem * vn_start_write(), so it can do a vn_set_extattr(). 5353e5ba2e1SRick Macklem */ 5363e5ba2e1SRick Macklem if (nfsrv_devidcnt > 0) { 5373e5ba2e1SRick Macklem nfsrv_writerpc[NFSPROC_GETATTR] = 1; 5383e5ba2e1SRick Macklem nfsv4_opflag[NFSV4OP_GETATTR].modifyfs = 1; 5393e5ba2e1SRick Macklem } 5403e5ba2e1SRick Macklem 5419ec7b004SRick Macklem svc_run(nfsrvd_pool); 5429ec7b004SRick Macklem 5433e5ba2e1SRick Macklem /* Reset Getattr to not do a vn_start_write(). */ 5443e5ba2e1SRick Macklem nfsrv_writerpc[NFSPROC_GETATTR] = 0; 5453e5ba2e1SRick Macklem nfsv4_opflag[NFSV4OP_GETATTR].modifyfs = 0; 5463e5ba2e1SRick Macklem 5477e745519SRick Macklem if (principal[0] != '\0') { 5487e7fd7d1SRick Macklem rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2); 5497e7fd7d1SRick Macklem rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3); 5507e7fd7d1SRick Macklem rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4); 5517e745519SRick Macklem } 55290d2dfabSRick Macklem } 5539ec7b004SRick Macklem NFSD_LOCK(); 5549ec7b004SRick Macklem newnfs_numnfsd--; 5559ec7b004SRick Macklem nfsrvd_init(1); 5561bc93bb7SKonstantin Belousov PROC_LOCK(p); 5571bc93bb7SKonstantin Belousov p->p_flag2 &= ~P2_AST_SU; 5581bc93bb7SKonstantin Belousov PROC_UNLOCK(p); 5599ec7b004SRick Macklem } 5609ec7b004SRick Macklem NFSD_UNLOCK(); 5619ec7b004SRick Macklem 562a9285ae5SZack Kirsch out: 563a9285ae5SZack Kirsch NFSEXITCODE(error); 564a9285ae5SZack Kirsch return (error); 5659ec7b004SRick Macklem } 5669ec7b004SRick Macklem 5679ec7b004SRick Macklem /* 5689ec7b004SRick Macklem * Initialize the data structures for the server. 5699ec7b004SRick Macklem * Handshake with any new nfsds starting up to avoid any chance of 5709ec7b004SRick Macklem * corruption. 5719ec7b004SRick Macklem */ 5729ec7b004SRick Macklem void 5739ec7b004SRick Macklem nfsrvd_init(int terminating) 5749ec7b004SRick Macklem { 5759ec7b004SRick Macklem 5769ec7b004SRick Macklem NFSD_LOCK_ASSERT(); 5779ec7b004SRick Macklem 5789ec7b004SRick Macklem if (terminating) { 5795f73287aSRick Macklem nfsd_master_proc = NULL; 5809ec7b004SRick Macklem NFSD_UNLOCK(); 58190d2dfabSRick Macklem nfsrv_freealllayoutsanddevids(); 582a0962bf8SRick Macklem nfsrv_freeallbackchannel_xprts(); 58390f90687SAndriy Gapon svcpool_close(nfsrvd_pool); 584acc6e58dSRick Macklem free(nfsrv_zeropnfsdat, M_TEMP); 585acc6e58dSRick Macklem nfsrv_zeropnfsdat = NULL; 5869ec7b004SRick Macklem NFSD_LOCK(); 58790f90687SAndriy Gapon } else { 5889ec7b004SRick Macklem NFSD_UNLOCK(); 58990f90687SAndriy Gapon nfsrvd_pool = svcpool_create("nfsd", 59090f90687SAndriy Gapon SYSCTL_STATIC_CHILDREN(_vfs_nfsd)); 5919ec7b004SRick Macklem nfsrvd_pool->sp_rcache = NULL; 592d96b98a3SKenneth D. Merry nfsrvd_pool->sp_assign = fhanew_assign; 593d96b98a3SKenneth D. Merry nfsrvd_pool->sp_done = fha_nd_complete; 5949ec7b004SRick Macklem NFSD_LOCK(); 5959ec7b004SRick Macklem } 59690f90687SAndriy Gapon } 5979ec7b004SRick Macklem 598