19ec7b004SRick Macklem /*- 29ec7b004SRick Macklem * Copyright (c) 1989, 1993 39ec7b004SRick Macklem * The Regents of the University of California. All rights reserved. 49ec7b004SRick Macklem * 59ec7b004SRick Macklem * This code is derived from software contributed to Berkeley by 69ec7b004SRick Macklem * Rick Macklem at The University of Guelph. 79ec7b004SRick Macklem * 89ec7b004SRick Macklem * Redistribution and use in source and binary forms, with or without 99ec7b004SRick Macklem * modification, are permitted provided that the following conditions 109ec7b004SRick Macklem * are met: 119ec7b004SRick Macklem * 1. Redistributions of source code must retain the above copyright 129ec7b004SRick Macklem * notice, this list of conditions and the following disclaimer. 139ec7b004SRick Macklem * 2. Redistributions in binary form must reproduce the above copyright 149ec7b004SRick Macklem * notice, this list of conditions and the following disclaimer in the 159ec7b004SRick Macklem * documentation and/or other materials provided with the distribution. 169ec7b004SRick Macklem * 4. Neither the name of the University nor the names of its contributors 179ec7b004SRick Macklem * may be used to endorse or promote products derived from this software 189ec7b004SRick Macklem * without specific prior written permission. 199ec7b004SRick Macklem * 209ec7b004SRick Macklem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 219ec7b004SRick Macklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 229ec7b004SRick Macklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 239ec7b004SRick Macklem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 249ec7b004SRick Macklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 259ec7b004SRick Macklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 269ec7b004SRick Macklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 279ec7b004SRick Macklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 289ec7b004SRick Macklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 299ec7b004SRick Macklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 309ec7b004SRick Macklem * SUCH DAMAGE. 319ec7b004SRick Macklem * 329ec7b004SRick Macklem */ 339ec7b004SRick Macklem 349ec7b004SRick Macklem #include <sys/cdefs.h> 359ec7b004SRick Macklem __FBSDID("$FreeBSD$"); 369ec7b004SRick Macklem 379ec7b004SRick Macklem #include "opt_inet6.h" 389ec7b004SRick Macklem #include "opt_kgssapi.h" 399ec7b004SRick Macklem 409ec7b004SRick Macklem #include <fs/nfs/nfsport.h> 419ec7b004SRick Macklem 429ec7b004SRick Macklem #include <rpc/rpc.h> 439ec7b004SRick Macklem #include <rpc/rpcsec_gss.h> 449ec7b004SRick Macklem 45adb97406SKenneth D. Merry #include <nfs/nfs_fha.h> 46d96b98a3SKenneth D. Merry #include <fs/nfsserver/nfs_fha_new.h> 47d96b98a3SKenneth D. Merry 48bcf11e8dSRobert Watson #include <security/mac/mac_framework.h> 49bcf11e8dSRobert Watson 509ec7b004SRick Macklem NFSDLOCKMUTEX; 516001db29SRick Macklem NFSV4ROOTLOCKMUTEX; 526001db29SRick Macklem struct nfsv4lock nfsd_suspend_lock; 539ec7b004SRick Macklem 549ec7b004SRick Macklem /* 559ec7b004SRick Macklem * Mapping of old NFS Version 2 RPC numbers to generic numbers. 569ec7b004SRick Macklem */ 57d96b98a3SKenneth D. Merry int newnfs_nfsv3_procid[NFS_V3NPROCS] = { 589ec7b004SRick Macklem NFSPROC_NULL, 599ec7b004SRick Macklem NFSPROC_GETATTR, 609ec7b004SRick Macklem NFSPROC_SETATTR, 619ec7b004SRick Macklem NFSPROC_NOOP, 629ec7b004SRick Macklem NFSPROC_LOOKUP, 639ec7b004SRick Macklem NFSPROC_READLINK, 649ec7b004SRick Macklem NFSPROC_READ, 659ec7b004SRick Macklem NFSPROC_NOOP, 669ec7b004SRick Macklem NFSPROC_WRITE, 679ec7b004SRick Macklem NFSPROC_CREATE, 689ec7b004SRick Macklem NFSPROC_REMOVE, 699ec7b004SRick Macklem NFSPROC_RENAME, 709ec7b004SRick Macklem NFSPROC_LINK, 719ec7b004SRick Macklem NFSPROC_SYMLINK, 729ec7b004SRick Macklem NFSPROC_MKDIR, 739ec7b004SRick Macklem NFSPROC_RMDIR, 749ec7b004SRick Macklem NFSPROC_READDIR, 759ec7b004SRick Macklem NFSPROC_FSSTAT, 769ec7b004SRick Macklem NFSPROC_NOOP, 779ec7b004SRick Macklem NFSPROC_NOOP, 789ec7b004SRick Macklem NFSPROC_NOOP, 799ec7b004SRick Macklem NFSPROC_NOOP, 809ec7b004SRick Macklem }; 819ec7b004SRick Macklem 829ec7b004SRick Macklem 83a0c2c369SRick Macklem SYSCTL_DECL(_vfs_nfsd); 849ec7b004SRick Macklem 859ec7b004SRick Macklem SVCPOOL *nfsrvd_pool; 869ec7b004SRick Macklem 879ec7b004SRick Macklem static int nfs_privport = 0; 8842ecb595SKonstantin Belousov SYSCTL_INT(_vfs_nfsd, OID_AUTO, nfs_privport, CTLFLAG_RWTUN, 899ec7b004SRick Macklem &nfs_privport, 0, 909ec7b004SRick Macklem "Only allow clients using a privileged port for NFSv2 and 3"); 919ec7b004SRick Macklem 9210a5a16eSRick Macklem static int nfs_minvers = NFS_VER2; 9342ecb595SKonstantin Belousov SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_min_nfsvers, CTLFLAG_RWTUN, 9410a5a16eSRick Macklem &nfs_minvers, 0, "The lowest version of NFS handled by the server"); 9510a5a16eSRick Macklem 9610a5a16eSRick Macklem static int nfs_maxvers = NFS_VER4; 9742ecb595SKonstantin Belousov SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_max_nfsvers, CTLFLAG_RWTUN, 9810a5a16eSRick Macklem &nfs_maxvers, 0, "The highest version of NFS handled by the server"); 9910a5a16eSRick Macklem 100d473bac7SAlexander Motin static int nfs_proc(struct nfsrv_descript *, u_int32_t, SVCXPRT *xprt, 101d473bac7SAlexander Motin struct nfsrvcache **); 1029ec7b004SRick Macklem 1039ec7b004SRick Macklem extern u_long sb_max_adj; 1049ec7b004SRick Macklem extern int newnfs_numnfsd; 1055f73287aSRick Macklem extern struct proc *nfsd_master_proc; 1069ec7b004SRick Macklem 1079ec7b004SRick Macklem /* 1089ec7b004SRick Macklem * NFS server system calls 1099ec7b004SRick Macklem */ 1109ec7b004SRick Macklem 1119ec7b004SRick Macklem static void 1129ec7b004SRick Macklem nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt) 1139ec7b004SRick Macklem { 1149ec7b004SRick Macklem struct nfsrv_descript nd; 1159ec7b004SRick Macklem struct nfsrvcache *rp = NULL; 11698ad4453SRick Macklem int cacherep, credflavor; 1179ec7b004SRick Macklem 1189ec7b004SRick Macklem memset(&nd, 0, sizeof(nd)); 1199ec7b004SRick Macklem if (rqst->rq_vers == NFS_VER2) { 1202fbb9d56SRick Macklem if (rqst->rq_proc > NFSV2PROC_STATFS || 1212fbb9d56SRick Macklem newnfs_nfsv3_procid[rqst->rq_proc] == NFSPROC_NOOP) { 1229ec7b004SRick Macklem svcerr_noproc(rqst); 1239ec7b004SRick Macklem svc_freereq(rqst); 124a9285ae5SZack Kirsch goto out; 1259ec7b004SRick Macklem } 1269ec7b004SRick Macklem nd.nd_procnum = newnfs_nfsv3_procid[rqst->rq_proc]; 1279ec7b004SRick Macklem nd.nd_flag = ND_NFSV2; 1289ec7b004SRick Macklem } else if (rqst->rq_vers == NFS_VER3) { 1299ec7b004SRick Macklem if (rqst->rq_proc >= NFS_V3NPROCS) { 1309ec7b004SRick Macklem svcerr_noproc(rqst); 1319ec7b004SRick Macklem svc_freereq(rqst); 132a9285ae5SZack Kirsch goto out; 1339ec7b004SRick Macklem } 1349ec7b004SRick Macklem nd.nd_procnum = rqst->rq_proc; 1359ec7b004SRick Macklem nd.nd_flag = ND_NFSV3; 1369ec7b004SRick Macklem } else { 1379ec7b004SRick Macklem if (rqst->rq_proc != NFSPROC_NULL && 1389ec7b004SRick Macklem rqst->rq_proc != NFSV4PROC_COMPOUND) { 1399ec7b004SRick Macklem svcerr_noproc(rqst); 1409ec7b004SRick Macklem svc_freereq(rqst); 141a9285ae5SZack Kirsch goto out; 1429ec7b004SRick Macklem } 1439ec7b004SRick Macklem nd.nd_procnum = rqst->rq_proc; 1449ec7b004SRick Macklem nd.nd_flag = ND_NFSV4; 1459ec7b004SRick Macklem } 1469ec7b004SRick Macklem 1479ec7b004SRick Macklem /* 1489ec7b004SRick Macklem * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 - 1499ec7b004SRick Macklem * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP 1509ec7b004SRick Macklem * mounts. 1519ec7b004SRick Macklem */ 1529ec7b004SRick Macklem nd.nd_mrep = rqst->rq_args; 1539ec7b004SRick Macklem rqst->rq_args = NULL; 154d96b98a3SKenneth D. Merry newnfs_realign(&nd.nd_mrep, M_WAITOK); 1559ec7b004SRick Macklem nd.nd_md = nd.nd_mrep; 1569ec7b004SRick Macklem nd.nd_dpos = mtod(nd.nd_md, caddr_t); 1579ec7b004SRick Macklem nd.nd_nam = svc_getrpccaller(rqst); 1589ec7b004SRick Macklem nd.nd_nam2 = rqst->rq_addr; 1599ec7b004SRick Macklem nd.nd_mreq = NULL; 1609ec7b004SRick Macklem nd.nd_cred = NULL; 1619ec7b004SRick Macklem 1629ec7b004SRick Macklem if (nfs_privport && (nd.nd_flag & ND_NFSV4) == 0) { 1639ec7b004SRick Macklem /* Check if source port is privileged */ 1649ec7b004SRick Macklem u_short port; 1659ec7b004SRick Macklem struct sockaddr *nam = nd.nd_nam; 1669ec7b004SRick Macklem struct sockaddr_in *sin; 1679ec7b004SRick Macklem 1689ec7b004SRick Macklem sin = (struct sockaddr_in *)nam; 1699ec7b004SRick Macklem /* 1709ec7b004SRick Macklem * INET/INET6 - same code: 1719ec7b004SRick Macklem * sin_port and sin6_port are at same offset 1729ec7b004SRick Macklem */ 1739ec7b004SRick Macklem port = ntohs(sin->sin_port); 1749ec7b004SRick Macklem if (port >= IPPORT_RESERVED && 1759ec7b004SRick Macklem nd.nd_procnum != NFSPROC_NULL) { 1769ec7b004SRick Macklem #ifdef INET6 1779ec7b004SRick Macklem char b6[INET6_ADDRSTRLEN]; 1789ec7b004SRick Macklem #if defined(KLD_MODULE) 1799ec7b004SRick Macklem /* Do not use ip6_sprintf: the nfs module should work without INET6. */ 1809ec7b004SRick Macklem #define ip6_sprintf(buf, a) \ 1819ec7b004SRick Macklem (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x", \ 1829ec7b004SRick Macklem (a)->s6_addr16[0], (a)->s6_addr16[1], \ 1839ec7b004SRick Macklem (a)->s6_addr16[2], (a)->s6_addr16[3], \ 1849ec7b004SRick Macklem (a)->s6_addr16[4], (a)->s6_addr16[5], \ 1859ec7b004SRick Macklem (a)->s6_addr16[6], (a)->s6_addr16[7]), \ 1869ec7b004SRick Macklem (buf)) 1879ec7b004SRick Macklem #endif 1889ec7b004SRick Macklem #endif 1899ec7b004SRick Macklem printf("NFS request from unprivileged port (%s:%d)\n", 1909ec7b004SRick Macklem #ifdef INET6 1919ec7b004SRick Macklem sin->sin_family == AF_INET6 ? 1929ec7b004SRick Macklem ip6_sprintf(b6, &satosin6(sin)->sin6_addr) : 1939ec7b004SRick Macklem #if defined(KLD_MODULE) 1949ec7b004SRick Macklem #undef ip6_sprintf 1959ec7b004SRick Macklem #endif 1969ec7b004SRick Macklem #endif 1979ec7b004SRick Macklem inet_ntoa(sin->sin_addr), port); 1989ec7b004SRick Macklem svcerr_weakauth(rqst); 1999ec7b004SRick Macklem svc_freereq(rqst); 2009ec7b004SRick Macklem m_freem(nd.nd_mrep); 201a9285ae5SZack Kirsch goto out; 2029ec7b004SRick Macklem } 2039ec7b004SRick Macklem } 2049ec7b004SRick Macklem 2059ec7b004SRick Macklem if (nd.nd_procnum != NFSPROC_NULL) { 20698ad4453SRick Macklem if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) { 2079ec7b004SRick Macklem svcerr_weakauth(rqst); 2089ec7b004SRick Macklem svc_freereq(rqst); 2099ec7b004SRick Macklem m_freem(nd.nd_mrep); 210a9285ae5SZack Kirsch goto out; 2119ec7b004SRick Macklem } 21298ad4453SRick Macklem 21398ad4453SRick Macklem /* Set the flag based on credflavor */ 21498ad4453SRick Macklem if (credflavor == RPCSEC_GSS_KRB5) { 21598ad4453SRick Macklem nd.nd_flag |= ND_GSS; 21698ad4453SRick Macklem } else if (credflavor == RPCSEC_GSS_KRB5I) { 21798ad4453SRick Macklem nd.nd_flag |= (ND_GSS | ND_GSSINTEGRITY); 21898ad4453SRick Macklem } else if (credflavor == RPCSEC_GSS_KRB5P) { 21998ad4453SRick Macklem nd.nd_flag |= (ND_GSS | ND_GSSPRIVACY); 22098ad4453SRick Macklem } else if (credflavor != AUTH_SYS) { 22198ad4453SRick Macklem svcerr_weakauth(rqst); 22298ad4453SRick Macklem svc_freereq(rqst); 22398ad4453SRick Macklem m_freem(nd.nd_mrep); 224a9285ae5SZack Kirsch goto out; 22598ad4453SRick Macklem } 22698ad4453SRick Macklem 2279ec7b004SRick Macklem #ifdef MAC 2289ec7b004SRick Macklem mac_cred_associate_nfsd(nd.nd_cred); 2299ec7b004SRick Macklem #endif 2306001db29SRick Macklem /* 2316001db29SRick Macklem * Get a refcnt (shared lock) on nfsd_suspend_lock. 2326001db29SRick Macklem * NFSSVC_SUSPENDNFSD will take an exclusive lock on 2336001db29SRick Macklem * nfsd_suspend_lock to suspend these threads. 2346001db29SRick Macklem * This must be done here, before the check of 2356001db29SRick Macklem * nfsv4root exports by nfsvno_v4rootexport(). 2366001db29SRick Macklem */ 2376001db29SRick Macklem NFSLOCKV4ROOTMUTEX(); 2386001db29SRick Macklem nfsv4_getref(&nfsd_suspend_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, 2396001db29SRick Macklem NULL); 2406001db29SRick Macklem NFSUNLOCKV4ROOTMUTEX(); 2416001db29SRick Macklem 242415670e4SRick Macklem if ((nd.nd_flag & ND_NFSV4) != 0) { 2439ec7b004SRick Macklem nd.nd_repstat = nfsvno_v4rootexport(&nd); 244415670e4SRick Macklem if (nd.nd_repstat != 0) { 2456001db29SRick Macklem NFSLOCKV4ROOTMUTEX(); 2466001db29SRick Macklem nfsv4_relref(&nfsd_suspend_lock); 2476001db29SRick Macklem NFSUNLOCKV4ROOTMUTEX(); 248415670e4SRick Macklem svcerr_weakauth(rqst); 249415670e4SRick Macklem svc_freereq(rqst); 250415670e4SRick Macklem m_freem(nd.nd_mrep); 251a9285ae5SZack Kirsch goto out; 252415670e4SRick Macklem } 253415670e4SRick Macklem } 2549ec7b004SRick Macklem 255d473bac7SAlexander Motin cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp); 2566001db29SRick Macklem NFSLOCKV4ROOTMUTEX(); 2576001db29SRick Macklem nfsv4_relref(&nfsd_suspend_lock); 2586001db29SRick Macklem NFSUNLOCKV4ROOTMUTEX(); 2599ec7b004SRick Macklem } else { 2609ec7b004SRick Macklem NFSMGET(nd.nd_mreq); 2619ec7b004SRick Macklem nd.nd_mreq->m_len = 0; 2629ec7b004SRick Macklem cacherep = RC_REPLY; 2639ec7b004SRick Macklem } 2649ec7b004SRick Macklem if (nd.nd_mrep != NULL) 2659ec7b004SRick Macklem m_freem(nd.nd_mrep); 2669ec7b004SRick Macklem 2679ec7b004SRick Macklem if (nd.nd_cred != NULL) 2689ec7b004SRick Macklem crfree(nd.nd_cred); 2699ec7b004SRick Macklem 2709ec7b004SRick Macklem if (cacherep == RC_DROPIT) { 2719ec7b004SRick Macklem if (nd.nd_mreq != NULL) 2729ec7b004SRick Macklem m_freem(nd.nd_mreq); 2739ec7b004SRick Macklem svc_freereq(rqst); 274a9285ae5SZack Kirsch goto out; 2759ec7b004SRick Macklem } 2769ec7b004SRick Macklem 2779ec7b004SRick Macklem if (nd.nd_mreq == NULL) { 2789ec7b004SRick Macklem svcerr_decode(rqst); 2799ec7b004SRick Macklem svc_freereq(rqst); 280a9285ae5SZack Kirsch goto out; 2819ec7b004SRick Macklem } 2829ec7b004SRick Macklem 2839ec7b004SRick Macklem if (nd.nd_repstat & NFSERR_AUTHERR) { 2849ec7b004SRick Macklem svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR); 2859ec7b004SRick Macklem if (nd.nd_mreq != NULL) 2869ec7b004SRick Macklem m_freem(nd.nd_mreq); 2879ec7b004SRick Macklem } else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) { 2889ec7b004SRick Macklem svcerr_systemerr(rqst); 2899ec7b004SRick Macklem } 290d473bac7SAlexander Motin if (rp != NULL) { 2916103bae6SAlexander Motin nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 || 2926103bae6SAlexander Motin SVC_ACK(xprt, NULL)), rqst->rq_reply_seq); 293d473bac7SAlexander Motin } 2949ec7b004SRick Macklem svc_freereq(rqst); 295a9285ae5SZack Kirsch 296a9285ae5SZack Kirsch out: 2971bc93bb7SKonstantin Belousov if (softdep_ast_cleanup != NULL) 2981bc93bb7SKonstantin Belousov softdep_ast_cleanup(); 299a9285ae5SZack Kirsch NFSEXITCODE(0); 3009ec7b004SRick Macklem } 3019ec7b004SRick Macklem 3029ec7b004SRick Macklem /* 3039ec7b004SRick Macklem * Check the cache and, optionally, do the RPC. 3049ec7b004SRick Macklem * Return the appropriate cache response. 3059ec7b004SRick Macklem */ 3069ec7b004SRick Macklem static int 307d473bac7SAlexander Motin nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt, 308d473bac7SAlexander Motin struct nfsrvcache **rpp) 3099ec7b004SRick Macklem { 3109ec7b004SRick Macklem struct thread *td = curthread; 311c59e4cc3SRick Macklem int cacherep = RC_DOIT, isdgram, taglen = -1; 312c59e4cc3SRick Macklem struct mbuf *m; 313c59e4cc3SRick Macklem u_char tag[NFSV4_SMALLSTR + 1], *tagstr = NULL; 314c59e4cc3SRick Macklem u_int32_t minorvers = 0; 315d473bac7SAlexander Motin uint32_t ack; 3169ec7b004SRick Macklem 3179ec7b004SRick Macklem *rpp = NULL; 3189ec7b004SRick Macklem if (nd->nd_nam2 == NULL) { 3199ec7b004SRick Macklem nd->nd_flag |= ND_STREAMSOCK; 3209ec7b004SRick Macklem isdgram = 0; 3219ec7b004SRick Macklem } else { 3229ec7b004SRick Macklem isdgram = 1; 3239ec7b004SRick Macklem } 3249ec7b004SRick Macklem 3259ec7b004SRick Macklem /* 326415670e4SRick Macklem * Two cases: 3279ec7b004SRick Macklem * 1 - For NFSv2 over UDP, if we are near our malloc/mget 3289ec7b004SRick Macklem * limit, just drop the request. There is no 3299ec7b004SRick Macklem * NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the 3309ec7b004SRick Macklem * client will timeout/retry over UDP in a little while. 331415670e4SRick Macklem * 2 - nd_repstat == 0 && nd_mreq == NULL, which 3329ec7b004SRick Macklem * means a normal nfs rpc, so check the cache 3339ec7b004SRick Macklem */ 3349ec7b004SRick Macklem if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL && 3359ec7b004SRick Macklem nfsrv_mallocmget_limit()) { 3369ec7b004SRick Macklem cacherep = RC_DROPIT; 3379ec7b004SRick Macklem } else { 3389ec7b004SRick Macklem /* 3399ec7b004SRick Macklem * For NFSv3, play it safe and assume that the client is 3409ec7b004SRick Macklem * doing retries on the same TCP connection. 3419ec7b004SRick Macklem */ 3429ec7b004SRick Macklem if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) == 3439ec7b004SRick Macklem ND_STREAMSOCK) 3449ec7b004SRick Macklem nd->nd_flag |= ND_SAMETCPCONN; 3459ec7b004SRick Macklem nd->nd_retxid = xid; 3469ec7b004SRick Macklem nd->nd_tcpconntime = NFSD_MONOSEC; 347d473bac7SAlexander Motin nd->nd_sockref = xprt->xp_sockref; 348c59e4cc3SRick Macklem if ((nd->nd_flag & ND_NFSV4) != 0) 349c59e4cc3SRick Macklem nfsd_getminorvers(nd, tag, &tagstr, &taglen, 350c59e4cc3SRick Macklem &minorvers); 351c59e4cc3SRick Macklem if ((nd->nd_flag & ND_NFSV41) != 0) 352c59e4cc3SRick Macklem /* NFSv4.1 caches replies in the session slots. */ 353c59e4cc3SRick Macklem cacherep = RC_DOIT; 354c59e4cc3SRick Macklem else { 355d473bac7SAlexander Motin cacherep = nfsrvd_getcache(nd); 356d473bac7SAlexander Motin ack = 0; 357d473bac7SAlexander Motin SVC_ACK(xprt, &ack); 358d473bac7SAlexander Motin nfsrc_trimcache(xprt->xp_sockref, ack, 0); 3599ec7b004SRick Macklem } 360c59e4cc3SRick Macklem } 3619ec7b004SRick Macklem 3629ec7b004SRick Macklem /* 3639ec7b004SRick Macklem * Handle the request. There are three cases. 3649ec7b004SRick Macklem * RC_DOIT - do the RPC 3659ec7b004SRick Macklem * RC_REPLY - return the reply already created 3669ec7b004SRick Macklem * RC_DROPIT - just throw the request away 3679ec7b004SRick Macklem */ 3689ec7b004SRick Macklem if (cacherep == RC_DOIT) { 369c59e4cc3SRick Macklem if ((nd->nd_flag & ND_NFSV41) != 0) 370c59e4cc3SRick Macklem nd->nd_xprt = xprt; 371c59e4cc3SRick Macklem nfsrvd_dorpc(nd, isdgram, tagstr, taglen, minorvers, td); 372c59e4cc3SRick Macklem if ((nd->nd_flag & ND_NFSV41) != 0) { 373c59e4cc3SRick Macklem if (nd->nd_repstat != NFSERR_REPLYFROMCACHE && 374c59e4cc3SRick Macklem (nd->nd_flag & ND_SAVEREPLY) != 0) { 375c59e4cc3SRick Macklem /* Cache a copy of the reply. */ 376c59e4cc3SRick Macklem m = m_copym(nd->nd_mreq, 0, M_COPYALL, 377c59e4cc3SRick Macklem M_WAITOK); 378c59e4cc3SRick Macklem } else 379c59e4cc3SRick Macklem m = NULL; 380c59e4cc3SRick Macklem if ((nd->nd_flag & ND_HASSEQUENCE) != 0) 381c59e4cc3SRick Macklem nfsrv_cache_session(nd->nd_sessionid, 382c59e4cc3SRick Macklem nd->nd_slotid, nd->nd_repstat, &m); 383c59e4cc3SRick Macklem if (nd->nd_repstat == NFSERR_REPLYFROMCACHE) 384c59e4cc3SRick Macklem nd->nd_repstat = 0; 385c59e4cc3SRick Macklem cacherep = RC_REPLY; 386c59e4cc3SRick Macklem } else { 3879ec7b004SRick Macklem if (nd->nd_repstat == NFSERR_DONTREPLY) 3889ec7b004SRick Macklem cacherep = RC_DROPIT; 3899ec7b004SRick Macklem else 3909ec7b004SRick Macklem cacherep = RC_REPLY; 391d473bac7SAlexander Motin *rpp = nfsrvd_updatecache(nd); 3929ec7b004SRick Macklem } 393c59e4cc3SRick Macklem } 394c59e4cc3SRick Macklem if (tagstr != NULL && taglen > NFSV4_SMALLSTR) 395c59e4cc3SRick Macklem free(tagstr, M_TEMP); 396a9285ae5SZack Kirsch 397a9285ae5SZack Kirsch NFSEXITCODE2(0, nd); 3989ec7b004SRick Macklem return (cacherep); 3999ec7b004SRick Macklem } 4009ec7b004SRick Macklem 401d473bac7SAlexander Motin static void 402d473bac7SAlexander Motin nfssvc_loss(SVCXPRT *xprt) 403d473bac7SAlexander Motin { 404d473bac7SAlexander Motin uint32_t ack; 405d473bac7SAlexander Motin 406d473bac7SAlexander Motin ack = 0; 407d473bac7SAlexander Motin SVC_ACK(xprt, &ack); 408d473bac7SAlexander Motin nfsrc_trimcache(xprt->xp_sockref, ack, 1); 409d473bac7SAlexander Motin } 410d473bac7SAlexander Motin 4119ec7b004SRick Macklem /* 4129ec7b004SRick Macklem * Adds a socket to the list for servicing by nfsds. 4139ec7b004SRick Macklem */ 4149ec7b004SRick Macklem int 4159ec7b004SRick Macklem nfsrvd_addsock(struct file *fp) 4169ec7b004SRick Macklem { 4179ec7b004SRick Macklem int siz; 4189ec7b004SRick Macklem struct socket *so; 419a9285ae5SZack Kirsch int error = 0; 4209ec7b004SRick Macklem SVCXPRT *xprt; 4219ec7b004SRick Macklem static u_int64_t sockref = 0; 4229ec7b004SRick Macklem 4239ec7b004SRick Macklem so = fp->f_data; 4249ec7b004SRick Macklem 4259ec7b004SRick Macklem siz = sb_max_adj; 4269ec7b004SRick Macklem error = soreserve(so, siz, siz); 427a9285ae5SZack Kirsch if (error) 428a9285ae5SZack Kirsch goto out; 4299ec7b004SRick Macklem 4309ec7b004SRick Macklem /* 4319ec7b004SRick Macklem * Steal the socket from userland so that it doesn't close 4329ec7b004SRick Macklem * unexpectedly. 4339ec7b004SRick Macklem */ 4349ec7b004SRick Macklem if (so->so_type == SOCK_DGRAM) 4359ec7b004SRick Macklem xprt = svc_dg_create(nfsrvd_pool, so, 0, 0); 4369ec7b004SRick Macklem else 4379ec7b004SRick Macklem xprt = svc_vc_create(nfsrvd_pool, so, 0, 0); 4389ec7b004SRick Macklem if (xprt) { 4399ec7b004SRick Macklem fp->f_ops = &badfileops; 4409ec7b004SRick Macklem fp->f_data = NULL; 4419ec7b004SRick Macklem xprt->xp_sockref = ++sockref; 44210a5a16eSRick Macklem if (nfs_minvers == NFS_VER2) 44310a5a16eSRick Macklem svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program, 44410a5a16eSRick Macklem NULL); 44510a5a16eSRick Macklem if (nfs_minvers <= NFS_VER3 && nfs_maxvers >= NFS_VER3) 44610a5a16eSRick Macklem svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program, 44710a5a16eSRick Macklem NULL); 44810a5a16eSRick Macklem if (nfs_maxvers >= NFS_VER4) 44910a5a16eSRick Macklem svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program, 45010a5a16eSRick Macklem NULL); 451d473bac7SAlexander Motin if (so->so_type == SOCK_STREAM) 452d473bac7SAlexander Motin svc_loss_reg(xprt, nfssvc_loss); 45376b30a0cSRick Macklem SVC_RELEASE(xprt); 4549ec7b004SRick Macklem } 4559ec7b004SRick Macklem 456a9285ae5SZack Kirsch out: 457a9285ae5SZack Kirsch NFSEXITCODE(error); 458a9285ae5SZack Kirsch return (error); 4599ec7b004SRick Macklem } 4609ec7b004SRick Macklem 4619ec7b004SRick Macklem /* 4629ec7b004SRick Macklem * Called by nfssvc() for nfsds. Just loops around servicing rpc requests 4639ec7b004SRick Macklem * until it is killed by a signal. 4649ec7b004SRick Macklem */ 4659ec7b004SRick Macklem int 4669ec7b004SRick Macklem nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args) 4679ec7b004SRick Macklem { 4687e745519SRick Macklem char principal[MAXHOSTNAMELEN + 5]; 4691bc93bb7SKonstantin Belousov struct proc *p; 470a9285ae5SZack Kirsch int error = 0; 4719ec7b004SRick Macklem bool_t ret2, ret3, ret4; 4729ec7b004SRick Macklem 4737e745519SRick Macklem error = copyinstr(args->principal, principal, sizeof (principal), 4747e745519SRick Macklem NULL); 4759ec7b004SRick Macklem if (error) 476a9285ae5SZack Kirsch goto out; 4779ec7b004SRick Macklem 4789ec7b004SRick Macklem /* 4799ec7b004SRick Macklem * Only the first nfsd actually does any work. The RPC code 4809ec7b004SRick Macklem * adds threads to it as needed. Any extra processes offered 4819ec7b004SRick Macklem * by nfsd just exit. If nfsd is new enough, it will call us 4829ec7b004SRick Macklem * once with a structure that specifies how many threads to 4839ec7b004SRick Macklem * use. 4849ec7b004SRick Macklem */ 4859ec7b004SRick Macklem NFSD_LOCK(); 4869ec7b004SRick Macklem if (newnfs_numnfsd == 0) { 4871bc93bb7SKonstantin Belousov p = td->td_proc; 4881bc93bb7SKonstantin Belousov PROC_LOCK(p); 4891bc93bb7SKonstantin Belousov p->p_flag2 |= P2_AST_SU; 4901bc93bb7SKonstantin Belousov PROC_UNLOCK(p); 4919ec7b004SRick Macklem newnfs_numnfsd++; 4929ec7b004SRick Macklem 4939ec7b004SRick Macklem NFSD_UNLOCK(); 4949ec7b004SRick Macklem 4957e745519SRick Macklem /* An empty string implies AUTH_SYS only. */ 4967e745519SRick Macklem if (principal[0] != '\0') { 4977e7fd7d1SRick Macklem ret2 = rpc_gss_set_svc_name_call(principal, 4987e7fd7d1SRick Macklem "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER2); 4997e7fd7d1SRick Macklem ret3 = rpc_gss_set_svc_name_call(principal, 5007e7fd7d1SRick Macklem "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER3); 5017e7fd7d1SRick Macklem ret4 = rpc_gss_set_svc_name_call(principal, 5027e7fd7d1SRick Macklem "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER4); 5039ec7b004SRick Macklem 5047e7fd7d1SRick Macklem if (!ret2 || !ret3 || !ret4) 5057e7fd7d1SRick Macklem printf("nfsd: can't register svc name\n"); 5069ec7b004SRick Macklem } 5079ec7b004SRick Macklem 5089ec7b004SRick Macklem nfsrvd_pool->sp_minthreads = args->minthreads; 5099ec7b004SRick Macklem nfsrvd_pool->sp_maxthreads = args->maxthreads; 5109ec7b004SRick Macklem 5119ec7b004SRick Macklem svc_run(nfsrvd_pool); 5129ec7b004SRick Macklem 5137e745519SRick Macklem if (principal[0] != '\0') { 5147e7fd7d1SRick Macklem rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2); 5157e7fd7d1SRick Macklem rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3); 5167e7fd7d1SRick Macklem rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4); 5177e745519SRick Macklem } 5189ec7b004SRick Macklem 5199ec7b004SRick Macklem NFSD_LOCK(); 5209ec7b004SRick Macklem newnfs_numnfsd--; 5219ec7b004SRick Macklem nfsrvd_init(1); 5221bc93bb7SKonstantin Belousov PROC_LOCK(p); 5231bc93bb7SKonstantin Belousov p->p_flag2 &= ~P2_AST_SU; 5241bc93bb7SKonstantin Belousov PROC_UNLOCK(p); 5259ec7b004SRick Macklem } 5269ec7b004SRick Macklem NFSD_UNLOCK(); 5279ec7b004SRick Macklem 528a9285ae5SZack Kirsch out: 529a9285ae5SZack Kirsch NFSEXITCODE(error); 530a9285ae5SZack Kirsch return (error); 5319ec7b004SRick Macklem } 5329ec7b004SRick Macklem 5339ec7b004SRick Macklem /* 5349ec7b004SRick Macklem * Initialize the data structures for the server. 5359ec7b004SRick Macklem * Handshake with any new nfsds starting up to avoid any chance of 5369ec7b004SRick Macklem * corruption. 5379ec7b004SRick Macklem */ 5389ec7b004SRick Macklem void 5399ec7b004SRick Macklem nfsrvd_init(int terminating) 5409ec7b004SRick Macklem { 5419ec7b004SRick Macklem 5429ec7b004SRick Macklem NFSD_LOCK_ASSERT(); 5439ec7b004SRick Macklem 5449ec7b004SRick Macklem if (terminating) { 5455f73287aSRick Macklem nfsd_master_proc = NULL; 5469ec7b004SRick Macklem NFSD_UNLOCK(); 547*a0962bf8SRick Macklem nfsrv_freeallbackchannel_xprts(); 5489ec7b004SRick Macklem svcpool_destroy(nfsrvd_pool); 5499ec7b004SRick Macklem nfsrvd_pool = NULL; 5509ec7b004SRick Macklem NFSD_LOCK(); 5519ec7b004SRick Macklem } 5529ec7b004SRick Macklem 5539ec7b004SRick Macklem NFSD_UNLOCK(); 5549ec7b004SRick Macklem 555a0c2c369SRick Macklem nfsrvd_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsd)); 5569ec7b004SRick Macklem nfsrvd_pool->sp_rcache = NULL; 557d96b98a3SKenneth D. Merry nfsrvd_pool->sp_assign = fhanew_assign; 558d96b98a3SKenneth D. Merry nfsrvd_pool->sp_done = fha_nd_complete; 5599ec7b004SRick Macklem 5609ec7b004SRick Macklem NFSD_LOCK(); 5619ec7b004SRick Macklem } 5629ec7b004SRick Macklem 563