/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1989, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Rick Macklem at The University of Guelph.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*33*/3435#include <sys/cdefs.h>36#include "opt_kgssapi.h"37#include "opt_kern_tls.h"3839#include <fs/nfs/nfsport.h>4041#include <rpc/rpc.h>42#include <rpc/replay.h>43#include <rpc/rpcsec_gss.h>44#include <rpc/rpcsec_tls.h>4546NFSDLOCKMUTEX;4748extern SVCPOOL *nfscbd_pool;4950static int nfs_cbproc(struct nfsrv_descript *, u_int32_t);5152extern u_long sb_max_adj;53extern int nfs_numnfscbd;54extern int nfscl_debuglevel;5556/*57* NFS client system calls for handling callbacks.58*/5960/*61* Handles server to client callbacks.62*/63static void64nfscb_program(struct svc_req *rqst, SVCXPRT *xprt)65{66struct nfsrv_descript nd;67int cacherep, credflavor;68#ifdef KERN_TLS69u_int maxlen;70#endif7172memset(&nd, 0, sizeof(nd));73if (rqst->rq_proc != NFSPROC_NULL &&74rqst->rq_proc != NFSV4PROC_CBCOMPOUND) {75svcerr_noproc(rqst);76svc_freereq(rqst);77return;78}79nd.nd_procnum = rqst->rq_proc;80nd.nd_flag = (ND_NFSCB | ND_NFSV4);8182/*83* Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -84* NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP85* mounts.86*/87nd.nd_mrep = rqst->rq_args;88rqst->rq_args = NULL;89newnfs_realign(&nd.nd_mrep, M_WAITOK);90nd.nd_md = nd.nd_mrep;91nd.nd_dpos = mtod(nd.nd_md, caddr_t);92nd.nd_nam = svc_getrpccaller(rqst);93nd.nd_nam2 = rqst->rq_addr;94nd.nd_mreq = NULL;95nd.nd_cred = NULL;9697NFSCL_DEBUG(1, "cbproc=%d\n",nd.nd_procnum);98if (nd.nd_procnum != NFSPROC_NULL) {99if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {100svcerr_weakauth(rqst);101svc_freereq(rqst);102m_freem(nd.nd_mrep);103return;104}105106/* For now, I don't care what credential flavor was used. */107#ifdef notyet108#ifdef MAC109mac_cred_associate_nfsd(nd.nd_cred);110#endif111#endif112#ifdef KERN_TLS113if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0 &&114rpctls_getinfo(&maxlen, false, false)) {115nd.nd_flag |= ND_EXTPG;116nd.nd_maxextsiz = maxlen;117}118#endif119cacherep = nfs_cbproc(&nd, rqst->rq_xid);120} else {121NFSMGET(nd.nd_mreq);122nd.nd_mreq->m_len = 0;123cacherep = RC_REPLY;124}125if (nd.nd_mrep != NULL)126m_freem(nd.nd_mrep);127128if (nd.nd_cred != NULL)129crfree(nd.nd_cred);130131if (cacherep == RC_DROPIT) {132if (nd.nd_mreq != NULL)133m_freem(nd.nd_mreq);134svc_freereq(rqst);135return;136}137138if (nd.nd_mreq == NULL) {139svcerr_decode(rqst);140svc_freereq(rqst);141return;142}143144if (nd.nd_repstat & NFSERR_AUTHERR) {145svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);146if (nd.nd_mreq != NULL)147m_freem(nd.nd_mreq);148} else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq))149svcerr_systemerr(rqst);150else151NFSCL_DEBUG(1, "cbrep sent\n");152svc_freereq(rqst);153}154155/*156* Check the cache and, optionally, do the RPC.157* Return the appropriate cache response.158*/159static int160nfs_cbproc(struct nfsrv_descript *nd, u_int32_t xid)161{162struct thread *td = curthread;163int cacherep;164165if (nd->nd_nam2 == NULL)166nd->nd_flag |= ND_STREAMSOCK;167168nfscl_docb(nd, td);169if (nd->nd_repstat == NFSERR_DONTREPLY)170cacherep = RC_DROPIT;171else172cacherep = RC_REPLY;173return (cacherep);174}175176/*177* Adds a socket to the list for servicing by nfscbds.178*/179int180nfscbd_addsock(struct file *fp)181{182int siz;183struct socket *so;184int error;185SVCXPRT *xprt;186187so = fp->f_data;188189siz = sb_max_adj;190error = soreserve(so, siz, siz);191if (error)192return (error);193194/*195* Steal the socket from userland so that it doesn't close196* unexpectedly.197*/198if (so->so_type == SOCK_DGRAM)199xprt = svc_dg_create(nfscbd_pool, so, 0, 0);200else201xprt = svc_vc_create(nfscbd_pool, so, 0, 0);202if (xprt) {203fp->f_ops = &badfileops;204fp->f_data = NULL;205svc_reg(xprt, NFS_CALLBCKPROG, NFSV4_CBVERS, nfscb_program,206NULL);207SVC_RELEASE(xprt);208}209210return (0);211}212213/*214* Called by nfssvc() for nfscbds. Just loops around servicing rpc requests215* until it is killed by a signal.216*217* For now, only support callbacks via RPCSEC_GSS if there is a KerberosV218* keytab entry with a host based entry in it on the client. (I'm not even219* sure that getting Acceptor credentials for a user principal with a220* credentials cache is possible, but even if it is, major changes to the221* kgssapi would be required.)222* I don't believe that this is a serious limitation since, as of 2009, most223* NFSv4 servers supporting callbacks are using AUTH_SYS for callbacks even224* when the client is using RPCSEC_GSS. (This BSD server uses AUTH_SYS225* for callbacks unless nfsrv_gsscallbackson is set non-zero.)226*/227int228nfscbd_nfsd(struct thread *td, struct nfsd_nfscbd_args *args)229{230char principal[128];231int error;232233if (args != NULL) {234error = copyinstr(args->principal, principal,235sizeof(principal), NULL);236if (error)237return (error);238} else {239principal[0] = '\0';240}241242/*243* Only the first nfsd actually does any work. The RPC code244* adds threads to it as needed. Any extra processes offered245* by nfsd just exit. If nfsd is new enough, it will call us246* once with a structure that specifies how many threads to247* use.248*/249NFSD_LOCK();250if (nfs_numnfscbd == 0) {251nfs_numnfscbd++;252253NFSD_UNLOCK();254255if (principal[0] != '\0')256rpc_gss_set_svc_name_call(principal, "kerberosv5",257GSS_C_INDEFINITE, NFS_CALLBCKPROG, NFSV4_CBVERS);258259nfscbd_pool->sp_minthreads = 4;260nfscbd_pool->sp_maxthreads = 4;261262svc_run(nfscbd_pool);263264rpc_gss_clear_svc_name_call(NFS_CALLBCKPROG, NFSV4_CBVERS);265266NFSD_LOCK();267nfs_numnfscbd--;268nfsrvd_cbinit(1);269}270NFSD_UNLOCK();271272return (0);273}274275/*276* Initialize the data structures for the server.277* Handshake with any new nfsds starting up to avoid any chance of278* corruption.279*/280void281nfsrvd_cbinit(int terminating)282{283284NFSD_LOCK_ASSERT();285286if (terminating) {287/* Wait for any xprt registrations to complete. */288while (nfs_numnfscbd > 0)289msleep(&nfs_numnfscbd, NFSDLOCKMUTEXPTR, PZERO,290"nfscbdt", 0);291if (nfscbd_pool != NULL) {292NFSD_UNLOCK();293svcpool_close(nfscbd_pool);294NFSD_LOCK();295}296}297298if (nfscbd_pool == NULL) {299NFSD_UNLOCK();300nfscbd_pool = svcpool_create("nfscbd", NULL);301nfscbd_pool->sp_rcache = NULL;302nfscbd_pool->sp_assign = NULL;303nfscbd_pool->sp_done = NULL;304NFSD_LOCK();305}306}307308309