nfs_clkrpc.c revision 192121
1191783Srmacklem/*- 2191783Srmacklem * Copyright (c) 1989, 1993 3191783Srmacklem * The Regents of the University of California. All rights reserved. 4191783Srmacklem * 5191783Srmacklem * This code is derived from software contributed to Berkeley by 6191783Srmacklem * Rick Macklem at The University of Guelph. 7191783Srmacklem * 8191783Srmacklem * Redistribution and use in source and binary forms, with or without 9191783Srmacklem * modification, are permitted provided that the following conditions 10191783Srmacklem * are met: 11191783Srmacklem * 1. Redistributions of source code must retain the above copyright 12191783Srmacklem * notice, this list of conditions and the following disclaimer. 13191783Srmacklem * 2. Redistributions in binary form must reproduce the above copyright 14191783Srmacklem * notice, this list of conditions and the following disclaimer in the 15191783Srmacklem * documentation and/or other materials provided with the distribution. 16191783Srmacklem * 4. Neither the name of the University nor the names of its contributors 17191783Srmacklem * may be used to endorse or promote products derived from this software 18191783Srmacklem * without specific prior written permission. 19191783Srmacklem * 20191783Srmacklem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21191783Srmacklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22191783Srmacklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23191783Srmacklem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24191783Srmacklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25191783Srmacklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26191783Srmacklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27191783Srmacklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28191783Srmacklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29191783Srmacklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30191783Srmacklem * SUCH DAMAGE. 31191783Srmacklem * 32191783Srmacklem */ 33191783Srmacklem 34191783Srmacklem#include <sys/cdefs.h> 35191783Srmacklem__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clkrpc.c 192121 2009-05-14 21:39:08Z rmacklem $"); 36191783Srmacklem 37191783Srmacklem#include "opt_inet6.h" 38191783Srmacklem#include "opt_kgssapi.h" 39191783Srmacklem 40191783Srmacklem#include <fs/nfs/nfsport.h> 41191783Srmacklem 42191783Srmacklem#include <rpc/rpc.h> 43191783Srmacklem#include <rpc/rpcsec_gss.h> 44191783Srmacklem#include <rpc/replay.h> 45191783Srmacklem 46191783Srmacklem 47191783SrmacklemNFSDLOCKMUTEX; 48191783Srmacklem 49191783SrmacklemSYSCTL_DECL(_vfs_newnfs); 50191783Srmacklem 51191783SrmacklemSVCPOOL *nfscbd_pool; 52191783Srmacklem 53191783Srmacklemstatic int nfs_cbproc(struct nfsrv_descript *, u_int32_t); 54191783Srmacklem 55191783Srmacklemextern u_long sb_max_adj; 56191783Srmacklemextern int nfs_numnfscbd; 57191783Srmacklem 58191783Srmacklem/* 59191783Srmacklem * NFS client system calls for handling callbacks. 60191783Srmacklem */ 61191783Srmacklem 62191783Srmacklem/* 63191783Srmacklem * Handles server to client callbacks. 64191783Srmacklem */ 65191783Srmacklemstatic void 66191783Srmacklemnfscb_program(struct svc_req *rqst, SVCXPRT *xprt) 67191783Srmacklem{ 68191783Srmacklem struct nfsrv_descript nd; 69192121Srmacklem int cacherep, credflavor; 70191783Srmacklem 71191783Srmacklem memset(&nd, 0, sizeof(nd)); 72191783Srmacklem if (rqst->rq_proc != NFSPROC_NULL && 73191783Srmacklem rqst->rq_proc != NFSV4PROC_CBCOMPOUND) { 74191783Srmacklem svcerr_noproc(rqst); 75191783Srmacklem svc_freereq(rqst); 76191783Srmacklem return; 77191783Srmacklem } 78191783Srmacklem nd.nd_procnum = rqst->rq_proc; 79191783Srmacklem nd.nd_flag = (ND_NFSCB | ND_NFSV4); 80191783Srmacklem 81191783Srmacklem /* 82191783Srmacklem * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 - 83191783Srmacklem * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP 84191783Srmacklem * mounts. 85191783Srmacklem */ 86191783Srmacklem nd.nd_mrep = rqst->rq_args; 87191783Srmacklem rqst->rq_args = NULL; 88191783Srmacklem newnfs_realign(&nd.nd_mrep); 89191783Srmacklem nd.nd_md = nd.nd_mrep; 90191783Srmacklem nd.nd_dpos = mtod(nd.nd_md, caddr_t); 91191783Srmacklem nd.nd_nam = svc_getrpccaller(rqst); 92191783Srmacklem nd.nd_nam2 = rqst->rq_addr; 93191783Srmacklem nd.nd_mreq = NULL; 94191783Srmacklem nd.nd_cred = NULL; 95191783Srmacklem 96191783Srmacklem if (nd.nd_procnum != NFSPROC_NULL) { 97192121Srmacklem if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) { 98191783Srmacklem svcerr_weakauth(rqst); 99191783Srmacklem svc_freereq(rqst); 100191783Srmacklem m_freem(nd.nd_mrep); 101191783Srmacklem return; 102191783Srmacklem } 103192121Srmacklem 104192121Srmacklem /* For now, I don't care what credential flavor was used. */ 105191783Srmacklem#ifdef notyet 106191783Srmacklem#ifdef MAC 107191783Srmacklem mac_cred_associate_nfsd(nd.nd_cred); 108191783Srmacklem#endif 109191783Srmacklem#endif 110191783Srmacklem cacherep = nfs_cbproc(&nd, rqst->rq_xid); 111191783Srmacklem } else { 112191783Srmacklem NFSMGET(nd.nd_mreq); 113191783Srmacklem nd.nd_mreq->m_len = 0; 114191783Srmacklem cacherep = RC_REPLY; 115191783Srmacklem } 116191783Srmacklem if (nd.nd_mrep != NULL) 117191783Srmacklem m_freem(nd.nd_mrep); 118191783Srmacklem 119191783Srmacklem if (nd.nd_cred != NULL) 120191783Srmacklem crfree(nd.nd_cred); 121191783Srmacklem 122191783Srmacklem if (cacherep == RC_DROPIT) { 123191783Srmacklem if (nd.nd_mreq != NULL) 124191783Srmacklem m_freem(nd.nd_mreq); 125191783Srmacklem svc_freereq(rqst); 126191783Srmacklem return; 127191783Srmacklem } 128191783Srmacklem 129191783Srmacklem if (nd.nd_mreq == NULL) { 130191783Srmacklem svcerr_decode(rqst); 131191783Srmacklem svc_freereq(rqst); 132191783Srmacklem return; 133191783Srmacklem } 134191783Srmacklem 135191783Srmacklem if (nd.nd_repstat & NFSERR_AUTHERR) { 136191783Srmacklem svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR); 137191783Srmacklem if (nd.nd_mreq != NULL) 138191783Srmacklem m_freem(nd.nd_mreq); 139191783Srmacklem } else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) { 140191783Srmacklem svcerr_systemerr(rqst); 141191783Srmacklem } 142191783Srmacklem svc_freereq(rqst); 143191783Srmacklem} 144191783Srmacklem 145191783Srmacklem/* 146191783Srmacklem * Check the cache and, optionally, do the RPC. 147191783Srmacklem * Return the appropriate cache response. 148191783Srmacklem */ 149191783Srmacklemstatic int 150191783Srmacklemnfs_cbproc(struct nfsrv_descript *nd, u_int32_t xid) 151191783Srmacklem{ 152191783Srmacklem struct thread *td = curthread; 153191783Srmacklem int cacherep; 154191783Srmacklem 155191783Srmacklem if (nd->nd_nam2 == NULL) 156191783Srmacklem nd->nd_flag |= ND_STREAMSOCK; 157191783Srmacklem 158191783Srmacklem nfscl_docb(nd, td); 159191783Srmacklem if (nd->nd_repstat == NFSERR_DONTREPLY) 160191783Srmacklem cacherep = RC_DROPIT; 161191783Srmacklem else 162191783Srmacklem cacherep = RC_REPLY; 163191783Srmacklem return (cacherep); 164191783Srmacklem} 165191783Srmacklem 166191783Srmacklem/* 167191783Srmacklem * Adds a socket to the list for servicing by nfscbds. 168191783Srmacklem */ 169191783Srmacklemint 170191783Srmacklemnfscbd_addsock(struct file *fp) 171191783Srmacklem{ 172191783Srmacklem int siz; 173191783Srmacklem struct socket *so; 174191783Srmacklem int error; 175191783Srmacklem SVCXPRT *xprt; 176191783Srmacklem 177191783Srmacklem so = fp->f_data; 178191783Srmacklem 179191783Srmacklem siz = sb_max_adj; 180191783Srmacklem error = soreserve(so, siz, siz); 181191783Srmacklem if (error) 182191783Srmacklem return (error); 183191783Srmacklem 184191783Srmacklem /* 185191783Srmacklem * Steal the socket from userland so that it doesn't close 186191783Srmacklem * unexpectedly. 187191783Srmacklem */ 188191783Srmacklem if (so->so_type == SOCK_DGRAM) 189191783Srmacklem xprt = svc_dg_create(nfscbd_pool, so, 0, 0); 190191783Srmacklem else 191191783Srmacklem xprt = svc_vc_create(nfscbd_pool, so, 0, 0); 192191783Srmacklem if (xprt) { 193191783Srmacklem fp->f_ops = &badfileops; 194191783Srmacklem fp->f_data = NULL; 195191783Srmacklem svc_reg(xprt, NFS_CALLBCKPROG, NFSV4_CBVERS, nfscb_program, 196191783Srmacklem NULL); 197191783Srmacklem } 198191783Srmacklem 199191783Srmacklem return (0); 200191783Srmacklem} 201191783Srmacklem 202191783Srmacklem/* 203191783Srmacklem * Called by nfssvc() for nfscbds. Just loops around servicing rpc requests 204191783Srmacklem * until it is killed by a signal. 205191783Srmacklem * 206191783Srmacklem * For now, only support callbacks via RPCSEC_GSS if there is a KerberosV 207191783Srmacklem * keytab entry with a host based entry in it on the client. (I'm not even 208191783Srmacklem * sure that getting Acceptor credentials for a user principal with a 209191783Srmacklem * credentials cache is possible, but even if it is, major changes to the 210191783Srmacklem * kgssapi would be required.) 211191783Srmacklem * I don't believe that this is a serious limitation since, as of 2009, most 212191783Srmacklem * NFSv4 servers supporting callbacks are using AUTH_SYS for callbacks even 213191783Srmacklem * when the client is using RPCSEC_GSS. (This BSD server uses AUTH_SYS 214191783Srmacklem * for callbacks unless nfsrv_gsscallbackson is set non-zero.) 215191783Srmacklem */ 216191783Srmacklemint 217191783Srmacklemnfscbd_nfsd(struct thread *td, struct nfsd_nfscbd_args *args) 218191783Srmacklem{ 219191783Srmacklem#ifdef KGSSAPI 220191783Srmacklem char principal[128]; 221191783Srmacklem int error; 222191783Srmacklem#endif 223191783Srmacklem 224191783Srmacklem#ifdef KGSSAPI 225191783Srmacklem if (args != NULL) { 226191783Srmacklem error = copyinstr(args->principal, principal, 227191783Srmacklem sizeof(principal), NULL); 228191783Srmacklem if (error) 229191783Srmacklem return (error); 230191783Srmacklem } else { 231191783Srmacklem principal[0] = '\0'; 232191783Srmacklem } 233191783Srmacklem#endif 234191783Srmacklem 235191783Srmacklem /* 236191783Srmacklem * Only the first nfsd actually does any work. The RPC code 237191783Srmacklem * adds threads to it as needed. Any extra processes offered 238191783Srmacklem * by nfsd just exit. If nfsd is new enough, it will call us 239191783Srmacklem * once with a structure that specifies how many threads to 240191783Srmacklem * use. 241191783Srmacklem */ 242191783Srmacklem NFSD_LOCK(); 243191783Srmacklem if (nfs_numnfscbd == 0) { 244191783Srmacklem nfs_numnfscbd++; 245191783Srmacklem 246191783Srmacklem NFSD_UNLOCK(); 247191783Srmacklem 248191783Srmacklem#ifdef KGSSAPI 249191783Srmacklem if (principal[0] != '\0') 250191783Srmacklem rpc_gss_set_svc_name(principal, "kerberosv5", 251191783Srmacklem GSS_C_INDEFINITE, NFS_CALLBCKPROG, NFSV4_CBVERS); 252191783Srmacklem#endif 253191783Srmacklem 254191783Srmacklem nfscbd_pool->sp_minthreads = 4; 255191783Srmacklem nfscbd_pool->sp_maxthreads = 4; 256191783Srmacklem 257191783Srmacklem svc_run(nfscbd_pool); 258191783Srmacklem 259191783Srmacklem#ifdef KGSSAPI 260191783Srmacklem rpc_gss_clear_svc_name(NFS_CALLBCKPROG, NFSV4_CBVERS); 261191783Srmacklem#endif 262191783Srmacklem 263191783Srmacklem NFSD_LOCK(); 264191783Srmacklem nfs_numnfscbd--; 265191783Srmacklem nfsrvd_cbinit(1); 266191783Srmacklem } 267191783Srmacklem NFSD_UNLOCK(); 268191783Srmacklem 269191783Srmacklem return (0); 270191783Srmacklem} 271191783Srmacklem 272191783Srmacklem/* 273191783Srmacklem * Initialize the data structures for the server. 274191783Srmacklem * Handshake with any new nfsds starting up to avoid any chance of 275191783Srmacklem * corruption. 276191783Srmacklem */ 277191783Srmacklemvoid 278191783Srmacklemnfsrvd_cbinit(int terminating) 279191783Srmacklem{ 280191783Srmacklem 281191783Srmacklem NFSD_LOCK_ASSERT(); 282191783Srmacklem 283191783Srmacklem if (terminating) { 284191783Srmacklem NFSD_UNLOCK(); 285191783Srmacklem svcpool_destroy(nfscbd_pool); 286191783Srmacklem nfscbd_pool = NULL; 287191783Srmacklem NFSD_LOCK(); 288191783Srmacklem } 289191783Srmacklem 290191783Srmacklem NFSD_UNLOCK(); 291191783Srmacklem 292191783Srmacklem nfscbd_pool = svcpool_create("nfscbd", SYSCTL_STATIC_CHILDREN(_vfs_newnfs)); 293191783Srmacklem nfscbd_pool->sp_rcache = NULL; 294191783Srmacklem nfscbd_pool->sp_assign = NULL; 295191783Srmacklem nfscbd_pool->sp_done = NULL; 296191783Srmacklem 297191783Srmacklem NFSD_LOCK(); 298191783Srmacklem} 299191783Srmacklem 300