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. 234*ee58b564SPedro F. Giffuni * The call to nfsv4_lock() that precedes nfsv4_getref() 2358eabbbe2SRick Macklem * ensures that the acquisition of the exclusive lock 2368eabbbe2SRick Macklem * takes priority over acquisition of the shared lock by 2378eabbbe2SRick Macklem * waiting for any exclusive lock request to complete. 2386001db29SRick Macklem * This must be done here, before the check of 2396001db29SRick Macklem * nfsv4root exports by nfsvno_v4rootexport(). 2406001db29SRick Macklem */ 2416001db29SRick Macklem NFSLOCKV4ROOTMUTEX(); 2428eabbbe2SRick Macklem nfsv4_lock(&nfsd_suspend_lock, 0, NULL, NFSV4ROOTLOCKMUTEXPTR, 2438eabbbe2SRick Macklem NULL); 2446001db29SRick Macklem nfsv4_getref(&nfsd_suspend_lock, NULL, NFSV4ROOTLOCKMUTEXPTR, 2456001db29SRick Macklem NULL); 2466001db29SRick Macklem NFSUNLOCKV4ROOTMUTEX(); 2476001db29SRick Macklem 248415670e4SRick Macklem if ((nd.nd_flag & ND_NFSV4) != 0) { 2499ec7b004SRick Macklem nd.nd_repstat = nfsvno_v4rootexport(&nd); 250415670e4SRick Macklem if (nd.nd_repstat != 0) { 2516001db29SRick Macklem NFSLOCKV4ROOTMUTEX(); 2526001db29SRick Macklem nfsv4_relref(&nfsd_suspend_lock); 2536001db29SRick Macklem NFSUNLOCKV4ROOTMUTEX(); 254415670e4SRick Macklem svcerr_weakauth(rqst); 255415670e4SRick Macklem svc_freereq(rqst); 256415670e4SRick Macklem m_freem(nd.nd_mrep); 257a9285ae5SZack Kirsch goto out; 258415670e4SRick Macklem } 259415670e4SRick Macklem } 2609ec7b004SRick Macklem 261d473bac7SAlexander Motin cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp); 2626001db29SRick Macklem NFSLOCKV4ROOTMUTEX(); 2636001db29SRick Macklem nfsv4_relref(&nfsd_suspend_lock); 2646001db29SRick Macklem NFSUNLOCKV4ROOTMUTEX(); 2659ec7b004SRick Macklem } else { 2669ec7b004SRick Macklem NFSMGET(nd.nd_mreq); 2679ec7b004SRick Macklem nd.nd_mreq->m_len = 0; 2689ec7b004SRick Macklem cacherep = RC_REPLY; 2699ec7b004SRick Macklem } 2709ec7b004SRick Macklem if (nd.nd_mrep != NULL) 2719ec7b004SRick Macklem m_freem(nd.nd_mrep); 2729ec7b004SRick Macklem 2739ec7b004SRick Macklem if (nd.nd_cred != NULL) 2749ec7b004SRick Macklem crfree(nd.nd_cred); 2759ec7b004SRick Macklem 2769ec7b004SRick Macklem if (cacherep == RC_DROPIT) { 2779ec7b004SRick Macklem if (nd.nd_mreq != NULL) 2789ec7b004SRick Macklem m_freem(nd.nd_mreq); 2799ec7b004SRick Macklem svc_freereq(rqst); 280a9285ae5SZack Kirsch goto out; 2819ec7b004SRick Macklem } 2829ec7b004SRick Macklem 2839ec7b004SRick Macklem if (nd.nd_mreq == NULL) { 2849ec7b004SRick Macklem svcerr_decode(rqst); 2859ec7b004SRick Macklem svc_freereq(rqst); 286a9285ae5SZack Kirsch goto out; 2879ec7b004SRick Macklem } 2889ec7b004SRick Macklem 2899ec7b004SRick Macklem if (nd.nd_repstat & NFSERR_AUTHERR) { 2909ec7b004SRick Macklem svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR); 2919ec7b004SRick Macklem if (nd.nd_mreq != NULL) 2929ec7b004SRick Macklem m_freem(nd.nd_mreq); 2939ec7b004SRick Macklem } else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) { 2949ec7b004SRick Macklem svcerr_systemerr(rqst); 2959ec7b004SRick Macklem } 296d473bac7SAlexander Motin if (rp != NULL) { 2976103bae6SAlexander Motin nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 || 2986103bae6SAlexander Motin SVC_ACK(xprt, NULL)), rqst->rq_reply_seq); 299d473bac7SAlexander Motin } 3009ec7b004SRick Macklem svc_freereq(rqst); 301a9285ae5SZack Kirsch 302a9285ae5SZack Kirsch out: 3031bc93bb7SKonstantin Belousov if (softdep_ast_cleanup != NULL) 3041bc93bb7SKonstantin Belousov softdep_ast_cleanup(); 305a9285ae5SZack Kirsch NFSEXITCODE(0); 3069ec7b004SRick Macklem } 3079ec7b004SRick Macklem 3089ec7b004SRick Macklem /* 3099ec7b004SRick Macklem * Check the cache and, optionally, do the RPC. 3109ec7b004SRick Macklem * Return the appropriate cache response. 3119ec7b004SRick Macklem */ 3129ec7b004SRick Macklem static int 313d473bac7SAlexander Motin nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt, 314d473bac7SAlexander Motin struct nfsrvcache **rpp) 3159ec7b004SRick Macklem { 3169ec7b004SRick Macklem struct thread *td = curthread; 317c59e4cc3SRick Macklem int cacherep = RC_DOIT, isdgram, taglen = -1; 318c59e4cc3SRick Macklem struct mbuf *m; 319c59e4cc3SRick Macklem u_char tag[NFSV4_SMALLSTR + 1], *tagstr = NULL; 320c59e4cc3SRick Macklem u_int32_t minorvers = 0; 321d473bac7SAlexander Motin uint32_t ack; 3229ec7b004SRick Macklem 3239ec7b004SRick Macklem *rpp = NULL; 3249ec7b004SRick Macklem if (nd->nd_nam2 == NULL) { 3259ec7b004SRick Macklem nd->nd_flag |= ND_STREAMSOCK; 3269ec7b004SRick Macklem isdgram = 0; 3279ec7b004SRick Macklem } else { 3289ec7b004SRick Macklem isdgram = 1; 3299ec7b004SRick Macklem } 3309ec7b004SRick Macklem 3319ec7b004SRick Macklem /* 332415670e4SRick Macklem * Two cases: 3339ec7b004SRick Macklem * 1 - For NFSv2 over UDP, if we are near our malloc/mget 3349ec7b004SRick Macklem * limit, just drop the request. There is no 3359ec7b004SRick Macklem * NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the 3369ec7b004SRick Macklem * client will timeout/retry over UDP in a little while. 337415670e4SRick Macklem * 2 - nd_repstat == 0 && nd_mreq == NULL, which 3389ec7b004SRick Macklem * means a normal nfs rpc, so check the cache 3399ec7b004SRick Macklem */ 3409ec7b004SRick Macklem if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL && 3419ec7b004SRick Macklem nfsrv_mallocmget_limit()) { 3429ec7b004SRick Macklem cacherep = RC_DROPIT; 3439ec7b004SRick Macklem } else { 3449ec7b004SRick Macklem /* 3459ec7b004SRick Macklem * For NFSv3, play it safe and assume that the client is 3469ec7b004SRick Macklem * doing retries on the same TCP connection. 3479ec7b004SRick Macklem */ 3489ec7b004SRick Macklem if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) == 3499ec7b004SRick Macklem ND_STREAMSOCK) 3509ec7b004SRick Macklem nd->nd_flag |= ND_SAMETCPCONN; 3519ec7b004SRick Macklem nd->nd_retxid = xid; 3529ec7b004SRick Macklem nd->nd_tcpconntime = NFSD_MONOSEC; 353d473bac7SAlexander Motin nd->nd_sockref = xprt->xp_sockref; 354c59e4cc3SRick Macklem if ((nd->nd_flag & ND_NFSV4) != 0) 355c59e4cc3SRick Macklem nfsd_getminorvers(nd, tag, &tagstr, &taglen, 356c59e4cc3SRick Macklem &minorvers); 357c59e4cc3SRick Macklem if ((nd->nd_flag & ND_NFSV41) != 0) 358c59e4cc3SRick Macklem /* NFSv4.1 caches replies in the session slots. */ 359c59e4cc3SRick Macklem cacherep = RC_DOIT; 360c59e4cc3SRick Macklem else { 361d473bac7SAlexander Motin cacherep = nfsrvd_getcache(nd); 362d473bac7SAlexander Motin ack = 0; 363d473bac7SAlexander Motin SVC_ACK(xprt, &ack); 364d473bac7SAlexander Motin nfsrc_trimcache(xprt->xp_sockref, ack, 0); 3659ec7b004SRick Macklem } 366c59e4cc3SRick Macklem } 3679ec7b004SRick Macklem 3689ec7b004SRick Macklem /* 3699ec7b004SRick Macklem * Handle the request. There are three cases. 3709ec7b004SRick Macklem * RC_DOIT - do the RPC 3719ec7b004SRick Macklem * RC_REPLY - return the reply already created 3729ec7b004SRick Macklem * RC_DROPIT - just throw the request away 3739ec7b004SRick Macklem */ 3749ec7b004SRick Macklem if (cacherep == RC_DOIT) { 375c59e4cc3SRick Macklem if ((nd->nd_flag & ND_NFSV41) != 0) 376c59e4cc3SRick Macklem nd->nd_xprt = xprt; 377c59e4cc3SRick Macklem nfsrvd_dorpc(nd, isdgram, tagstr, taglen, minorvers, td); 378c59e4cc3SRick Macklem if ((nd->nd_flag & ND_NFSV41) != 0) { 379c59e4cc3SRick Macklem if (nd->nd_repstat != NFSERR_REPLYFROMCACHE && 380c59e4cc3SRick Macklem (nd->nd_flag & ND_SAVEREPLY) != 0) { 381c59e4cc3SRick Macklem /* Cache a copy of the reply. */ 382c59e4cc3SRick Macklem m = m_copym(nd->nd_mreq, 0, M_COPYALL, 383c59e4cc3SRick Macklem M_WAITOK); 384c59e4cc3SRick Macklem } else 385c59e4cc3SRick Macklem m = NULL; 386c59e4cc3SRick Macklem if ((nd->nd_flag & ND_HASSEQUENCE) != 0) 387c59e4cc3SRick Macklem nfsrv_cache_session(nd->nd_sessionid, 388c59e4cc3SRick Macklem nd->nd_slotid, nd->nd_repstat, &m); 389c59e4cc3SRick Macklem if (nd->nd_repstat == NFSERR_REPLYFROMCACHE) 390c59e4cc3SRick Macklem nd->nd_repstat = 0; 391c59e4cc3SRick Macklem cacherep = RC_REPLY; 392c59e4cc3SRick Macklem } else { 3939ec7b004SRick Macklem if (nd->nd_repstat == NFSERR_DONTREPLY) 3949ec7b004SRick Macklem cacherep = RC_DROPIT; 3959ec7b004SRick Macklem else 3969ec7b004SRick Macklem cacherep = RC_REPLY; 397d473bac7SAlexander Motin *rpp = nfsrvd_updatecache(nd); 3989ec7b004SRick Macklem } 399c59e4cc3SRick Macklem } 400c59e4cc3SRick Macklem if (tagstr != NULL && taglen > NFSV4_SMALLSTR) 401c59e4cc3SRick Macklem free(tagstr, M_TEMP); 402a9285ae5SZack Kirsch 403a9285ae5SZack Kirsch NFSEXITCODE2(0, nd); 4049ec7b004SRick Macklem return (cacherep); 4059ec7b004SRick Macklem } 4069ec7b004SRick Macklem 407d473bac7SAlexander Motin static void 408d473bac7SAlexander Motin nfssvc_loss(SVCXPRT *xprt) 409d473bac7SAlexander Motin { 410d473bac7SAlexander Motin uint32_t ack; 411d473bac7SAlexander Motin 412d473bac7SAlexander Motin ack = 0; 413d473bac7SAlexander Motin SVC_ACK(xprt, &ack); 414d473bac7SAlexander Motin nfsrc_trimcache(xprt->xp_sockref, ack, 1); 415d473bac7SAlexander Motin } 416d473bac7SAlexander Motin 4179ec7b004SRick Macklem /* 4189ec7b004SRick Macklem * Adds a socket to the list for servicing by nfsds. 4199ec7b004SRick Macklem */ 4209ec7b004SRick Macklem int 4219ec7b004SRick Macklem nfsrvd_addsock(struct file *fp) 4229ec7b004SRick Macklem { 4239ec7b004SRick Macklem int siz; 4249ec7b004SRick Macklem struct socket *so; 425a9285ae5SZack Kirsch int error = 0; 4269ec7b004SRick Macklem SVCXPRT *xprt; 4279ec7b004SRick Macklem static u_int64_t sockref = 0; 4289ec7b004SRick Macklem 4299ec7b004SRick Macklem so = fp->f_data; 4309ec7b004SRick Macklem 4319ec7b004SRick Macklem siz = sb_max_adj; 4329ec7b004SRick Macklem error = soreserve(so, siz, siz); 433a9285ae5SZack Kirsch if (error) 434a9285ae5SZack Kirsch goto out; 4359ec7b004SRick Macklem 4369ec7b004SRick Macklem /* 4379ec7b004SRick Macklem * Steal the socket from userland so that it doesn't close 4389ec7b004SRick Macklem * unexpectedly. 4399ec7b004SRick Macklem */ 4409ec7b004SRick Macklem if (so->so_type == SOCK_DGRAM) 4419ec7b004SRick Macklem xprt = svc_dg_create(nfsrvd_pool, so, 0, 0); 4429ec7b004SRick Macklem else 4439ec7b004SRick Macklem xprt = svc_vc_create(nfsrvd_pool, so, 0, 0); 4449ec7b004SRick Macklem if (xprt) { 4459ec7b004SRick Macklem fp->f_ops = &badfileops; 4469ec7b004SRick Macklem fp->f_data = NULL; 4479ec7b004SRick Macklem xprt->xp_sockref = ++sockref; 44810a5a16eSRick Macklem if (nfs_minvers == NFS_VER2) 44910a5a16eSRick Macklem svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program, 45010a5a16eSRick Macklem NULL); 45110a5a16eSRick Macklem if (nfs_minvers <= NFS_VER3 && nfs_maxvers >= NFS_VER3) 45210a5a16eSRick Macklem svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program, 45310a5a16eSRick Macklem NULL); 45410a5a16eSRick Macklem if (nfs_maxvers >= NFS_VER4) 45510a5a16eSRick Macklem svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program, 45610a5a16eSRick Macklem NULL); 457d473bac7SAlexander Motin if (so->so_type == SOCK_STREAM) 458d473bac7SAlexander Motin svc_loss_reg(xprt, nfssvc_loss); 45976b30a0cSRick Macklem SVC_RELEASE(xprt); 4609ec7b004SRick Macklem } 4619ec7b004SRick Macklem 462a9285ae5SZack Kirsch out: 463a9285ae5SZack Kirsch NFSEXITCODE(error); 464a9285ae5SZack Kirsch return (error); 4659ec7b004SRick Macklem } 4669ec7b004SRick Macklem 4679ec7b004SRick Macklem /* 4689ec7b004SRick Macklem * Called by nfssvc() for nfsds. Just loops around servicing rpc requests 4699ec7b004SRick Macklem * until it is killed by a signal. 4709ec7b004SRick Macklem */ 4719ec7b004SRick Macklem int 4729ec7b004SRick Macklem nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args) 4739ec7b004SRick Macklem { 4747e745519SRick Macklem char principal[MAXHOSTNAMELEN + 5]; 4751bc93bb7SKonstantin Belousov struct proc *p; 476a9285ae5SZack Kirsch int error = 0; 4779ec7b004SRick Macklem bool_t ret2, ret3, ret4; 4789ec7b004SRick Macklem 4797e745519SRick Macklem error = copyinstr(args->principal, principal, sizeof (principal), 4807e745519SRick Macklem NULL); 4819ec7b004SRick Macklem if (error) 482a9285ae5SZack Kirsch goto out; 4839ec7b004SRick Macklem 4849ec7b004SRick Macklem /* 4859ec7b004SRick Macklem * Only the first nfsd actually does any work. The RPC code 4869ec7b004SRick Macklem * adds threads to it as needed. Any extra processes offered 4879ec7b004SRick Macklem * by nfsd just exit. If nfsd is new enough, it will call us 4889ec7b004SRick Macklem * once with a structure that specifies how many threads to 4899ec7b004SRick Macklem * use. 4909ec7b004SRick Macklem */ 4919ec7b004SRick Macklem NFSD_LOCK(); 4929ec7b004SRick Macklem if (newnfs_numnfsd == 0) { 4931bc93bb7SKonstantin Belousov p = td->td_proc; 4941bc93bb7SKonstantin Belousov PROC_LOCK(p); 4951bc93bb7SKonstantin Belousov p->p_flag2 |= P2_AST_SU; 4961bc93bb7SKonstantin Belousov PROC_UNLOCK(p); 4979ec7b004SRick Macklem newnfs_numnfsd++; 4989ec7b004SRick Macklem 4999ec7b004SRick Macklem NFSD_UNLOCK(); 5009ec7b004SRick Macklem 5017e745519SRick Macklem /* An empty string implies AUTH_SYS only. */ 5027e745519SRick Macklem if (principal[0] != '\0') { 5037e7fd7d1SRick Macklem ret2 = rpc_gss_set_svc_name_call(principal, 5047e7fd7d1SRick Macklem "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER2); 5057e7fd7d1SRick Macklem ret3 = rpc_gss_set_svc_name_call(principal, 5067e7fd7d1SRick Macklem "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER3); 5077e7fd7d1SRick Macklem ret4 = rpc_gss_set_svc_name_call(principal, 5087e7fd7d1SRick Macklem "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER4); 5099ec7b004SRick Macklem 5107e7fd7d1SRick Macklem if (!ret2 || !ret3 || !ret4) 5117e7fd7d1SRick Macklem printf("nfsd: can't register svc name\n"); 5129ec7b004SRick Macklem } 5139ec7b004SRick Macklem 5149ec7b004SRick Macklem nfsrvd_pool->sp_minthreads = args->minthreads; 5159ec7b004SRick Macklem nfsrvd_pool->sp_maxthreads = args->maxthreads; 5169ec7b004SRick Macklem 5179ec7b004SRick Macklem svc_run(nfsrvd_pool); 5189ec7b004SRick Macklem 5197e745519SRick Macklem if (principal[0] != '\0') { 5207e7fd7d1SRick Macklem rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2); 5217e7fd7d1SRick Macklem rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3); 5227e7fd7d1SRick Macklem rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4); 5237e745519SRick Macklem } 5249ec7b004SRick Macklem 5259ec7b004SRick Macklem NFSD_LOCK(); 5269ec7b004SRick Macklem newnfs_numnfsd--; 5279ec7b004SRick Macklem nfsrvd_init(1); 5281bc93bb7SKonstantin Belousov PROC_LOCK(p); 5291bc93bb7SKonstantin Belousov p->p_flag2 &= ~P2_AST_SU; 5301bc93bb7SKonstantin Belousov PROC_UNLOCK(p); 5319ec7b004SRick Macklem } 5329ec7b004SRick Macklem NFSD_UNLOCK(); 5339ec7b004SRick Macklem 534a9285ae5SZack Kirsch out: 535a9285ae5SZack Kirsch NFSEXITCODE(error); 536a9285ae5SZack Kirsch return (error); 5379ec7b004SRick Macklem } 5389ec7b004SRick Macklem 5399ec7b004SRick Macklem /* 5409ec7b004SRick Macklem * Initialize the data structures for the server. 5419ec7b004SRick Macklem * Handshake with any new nfsds starting up to avoid any chance of 5429ec7b004SRick Macklem * corruption. 5439ec7b004SRick Macklem */ 5449ec7b004SRick Macklem void 5459ec7b004SRick Macklem nfsrvd_init(int terminating) 5469ec7b004SRick Macklem { 5479ec7b004SRick Macklem 5489ec7b004SRick Macklem NFSD_LOCK_ASSERT(); 5499ec7b004SRick Macklem 5509ec7b004SRick Macklem if (terminating) { 5515f73287aSRick Macklem nfsd_master_proc = NULL; 5529ec7b004SRick Macklem NFSD_UNLOCK(); 553a0962bf8SRick Macklem nfsrv_freeallbackchannel_xprts(); 5549ec7b004SRick Macklem svcpool_destroy(nfsrvd_pool); 5559ec7b004SRick Macklem nfsrvd_pool = NULL; 5569ec7b004SRick Macklem NFSD_LOCK(); 5579ec7b004SRick Macklem } 5589ec7b004SRick Macklem 5599ec7b004SRick Macklem NFSD_UNLOCK(); 5609ec7b004SRick Macklem 561a0c2c369SRick Macklem nfsrvd_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsd)); 5629ec7b004SRick Macklem nfsrvd_pool->sp_rcache = NULL; 563d96b98a3SKenneth D. Merry nfsrvd_pool->sp_assign = fhanew_assign; 564d96b98a3SKenneth D. Merry nfsrvd_pool->sp_done = fha_nd_complete; 5659ec7b004SRick Macklem 5669ec7b004SRick Macklem NFSD_LOCK(); 5679ec7b004SRick Macklem } 5689ec7b004SRick Macklem 569