Home | History | Annotate | Line # | Download | only in client
nfs_clkrpc.c revision 1.1.1.1.6.2
      1  1.1.1.1.6.2  yamt /*	$NetBSD: nfs_clkrpc.c,v 1.1.1.1.6.2 2014/05/22 11:41:00 yamt Exp $	*/
      2  1.1.1.1.6.2  yamt /*-
      3  1.1.1.1.6.2  yamt  * Copyright (c) 1989, 1993
      4  1.1.1.1.6.2  yamt  *	The Regents of the University of California.  All rights reserved.
      5  1.1.1.1.6.2  yamt  *
      6  1.1.1.1.6.2  yamt  * This code is derived from software contributed to Berkeley by
      7  1.1.1.1.6.2  yamt  * Rick Macklem at The University of Guelph.
      8  1.1.1.1.6.2  yamt  *
      9  1.1.1.1.6.2  yamt  * Redistribution and use in source and binary forms, with or without
     10  1.1.1.1.6.2  yamt  * modification, are permitted provided that the following conditions
     11  1.1.1.1.6.2  yamt  * are met:
     12  1.1.1.1.6.2  yamt  * 1. Redistributions of source code must retain the above copyright
     13  1.1.1.1.6.2  yamt  *    notice, this list of conditions and the following disclaimer.
     14  1.1.1.1.6.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1.1.1.6.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     16  1.1.1.1.6.2  yamt  *    documentation and/or other materials provided with the distribution.
     17  1.1.1.1.6.2  yamt  * 4. Neither the name of the University nor the names of its contributors
     18  1.1.1.1.6.2  yamt  *    may be used to endorse or promote products derived from this software
     19  1.1.1.1.6.2  yamt  *    without specific prior written permission.
     20  1.1.1.1.6.2  yamt  *
     21  1.1.1.1.6.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1.1.1.6.2  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1.1.1.6.2  yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1.1.1.6.2  yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1.1.1.6.2  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1.1.1.6.2  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1.1.1.6.2  yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1.1.1.6.2  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1.1.1.6.2  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1.1.1.6.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1.1.1.6.2  yamt  * SUCH DAMAGE.
     32  1.1.1.1.6.2  yamt  *
     33  1.1.1.1.6.2  yamt  */
     34  1.1.1.1.6.2  yamt 
     35  1.1.1.1.6.2  yamt #include <sys/cdefs.h>
     36  1.1.1.1.6.2  yamt /* __FBSDID("FreeBSD: head/sys/fs/nfsclient/nfs_clkrpc.c 255216 2013-09-04 22:47:56Z rmacklem "); */
     37  1.1.1.1.6.2  yamt __RCSID("$NetBSD: nfs_clkrpc.c,v 1.1.1.1.6.2 2014/05/22 11:41:00 yamt Exp $");
     38  1.1.1.1.6.2  yamt 
     39  1.1.1.1.6.2  yamt #include "opt_kgssapi.h"
     40  1.1.1.1.6.2  yamt 
     41  1.1.1.1.6.2  yamt #include <fs/nfs/nfsport.h>
     42  1.1.1.1.6.2  yamt 
     43  1.1.1.1.6.2  yamt #include <rpc/rpc.h>
     44  1.1.1.1.6.2  yamt #include <rpc/rpcsec_gss.h>
     45  1.1.1.1.6.2  yamt #include <rpc/replay.h>
     46  1.1.1.1.6.2  yamt 
     47  1.1.1.1.6.2  yamt 
     48  1.1.1.1.6.2  yamt NFSDLOCKMUTEX;
     49  1.1.1.1.6.2  yamt 
     50  1.1.1.1.6.2  yamt extern SVCPOOL	*nfscbd_pool;
     51  1.1.1.1.6.2  yamt 
     52  1.1.1.1.6.2  yamt static int nfs_cbproc(struct nfsrv_descript *, u_int32_t);
     53  1.1.1.1.6.2  yamt 
     54  1.1.1.1.6.2  yamt extern u_long sb_max_adj;
     55  1.1.1.1.6.2  yamt extern int nfs_numnfscbd;
     56  1.1.1.1.6.2  yamt extern int nfscl_debuglevel;
     57  1.1.1.1.6.2  yamt 
     58  1.1.1.1.6.2  yamt /*
     59  1.1.1.1.6.2  yamt  * NFS client system calls for handling callbacks.
     60  1.1.1.1.6.2  yamt  */
     61  1.1.1.1.6.2  yamt 
     62  1.1.1.1.6.2  yamt /*
     63  1.1.1.1.6.2  yamt  * Handles server to client callbacks.
     64  1.1.1.1.6.2  yamt  */
     65  1.1.1.1.6.2  yamt static void
     66  1.1.1.1.6.2  yamt nfscb_program(struct svc_req *rqst, SVCXPRT *xprt)
     67  1.1.1.1.6.2  yamt {
     68  1.1.1.1.6.2  yamt 	struct nfsrv_descript nd;
     69  1.1.1.1.6.2  yamt 	int cacherep, credflavor;
     70  1.1.1.1.6.2  yamt 
     71  1.1.1.1.6.2  yamt 	memset(&nd, 0, sizeof(nd));
     72  1.1.1.1.6.2  yamt 	if (rqst->rq_proc != NFSPROC_NULL &&
     73  1.1.1.1.6.2  yamt 	    rqst->rq_proc != NFSV4PROC_CBCOMPOUND) {
     74  1.1.1.1.6.2  yamt 		svcerr_noproc(rqst);
     75  1.1.1.1.6.2  yamt 		svc_freereq(rqst);
     76  1.1.1.1.6.2  yamt 		return;
     77  1.1.1.1.6.2  yamt 	}
     78  1.1.1.1.6.2  yamt 	nd.nd_procnum = rqst->rq_proc;
     79  1.1.1.1.6.2  yamt 	nd.nd_flag = (ND_NFSCB | ND_NFSV4);
     80  1.1.1.1.6.2  yamt 
     81  1.1.1.1.6.2  yamt 	/*
     82  1.1.1.1.6.2  yamt 	 * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
     83  1.1.1.1.6.2  yamt 	 * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
     84  1.1.1.1.6.2  yamt 	 * mounts.
     85  1.1.1.1.6.2  yamt 	 */
     86  1.1.1.1.6.2  yamt 	nd.nd_mrep = rqst->rq_args;
     87  1.1.1.1.6.2  yamt 	rqst->rq_args = NULL;
     88  1.1.1.1.6.2  yamt 	newnfs_realign(&nd.nd_mrep, M_WAITOK);
     89  1.1.1.1.6.2  yamt 	nd.nd_md = nd.nd_mrep;
     90  1.1.1.1.6.2  yamt 	nd.nd_dpos = mtod(nd.nd_md, caddr_t);
     91  1.1.1.1.6.2  yamt 	nd.nd_nam = svc_getrpccaller(rqst);
     92  1.1.1.1.6.2  yamt 	nd.nd_nam2 = rqst->rq_addr;
     93  1.1.1.1.6.2  yamt 	nd.nd_mreq = NULL;
     94  1.1.1.1.6.2  yamt 	nd.nd_cred = NULL;
     95  1.1.1.1.6.2  yamt 
     96  1.1.1.1.6.2  yamt 	NFSCL_DEBUG(1, "cbproc=%d\n",nd.nd_procnum);
     97  1.1.1.1.6.2  yamt 	if (nd.nd_procnum != NFSPROC_NULL) {
     98  1.1.1.1.6.2  yamt 		if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {
     99  1.1.1.1.6.2  yamt 			svcerr_weakauth(rqst);
    100  1.1.1.1.6.2  yamt 			svc_freereq(rqst);
    101  1.1.1.1.6.2  yamt 			m_freem(nd.nd_mrep);
    102  1.1.1.1.6.2  yamt 			return;
    103  1.1.1.1.6.2  yamt 		}
    104  1.1.1.1.6.2  yamt 
    105  1.1.1.1.6.2  yamt 		/* For now, I don't care what credential flavor was used. */
    106  1.1.1.1.6.2  yamt #ifdef notyet
    107  1.1.1.1.6.2  yamt #ifdef MAC
    108  1.1.1.1.6.2  yamt 		mac_cred_associate_nfsd(nd.nd_cred);
    109  1.1.1.1.6.2  yamt #endif
    110  1.1.1.1.6.2  yamt #endif
    111  1.1.1.1.6.2  yamt 		cacherep = nfs_cbproc(&nd, rqst->rq_xid);
    112  1.1.1.1.6.2  yamt 	} else {
    113  1.1.1.1.6.2  yamt 		NFSMGET(nd.nd_mreq);
    114  1.1.1.1.6.2  yamt 		nd.nd_mreq->m_len = 0;
    115  1.1.1.1.6.2  yamt 		cacherep = RC_REPLY;
    116  1.1.1.1.6.2  yamt 	}
    117  1.1.1.1.6.2  yamt 	if (nd.nd_mrep != NULL)
    118  1.1.1.1.6.2  yamt 		m_freem(nd.nd_mrep);
    119  1.1.1.1.6.2  yamt 
    120  1.1.1.1.6.2  yamt 	if (nd.nd_cred != NULL)
    121  1.1.1.1.6.2  yamt 		crfree(nd.nd_cred);
    122  1.1.1.1.6.2  yamt 
    123  1.1.1.1.6.2  yamt 	if (cacherep == RC_DROPIT) {
    124  1.1.1.1.6.2  yamt 		if (nd.nd_mreq != NULL)
    125  1.1.1.1.6.2  yamt 			m_freem(nd.nd_mreq);
    126  1.1.1.1.6.2  yamt 		svc_freereq(rqst);
    127  1.1.1.1.6.2  yamt 		return;
    128  1.1.1.1.6.2  yamt 	}
    129  1.1.1.1.6.2  yamt 
    130  1.1.1.1.6.2  yamt 	if (nd.nd_mreq == NULL) {
    131  1.1.1.1.6.2  yamt 		svcerr_decode(rqst);
    132  1.1.1.1.6.2  yamt 		svc_freereq(rqst);
    133  1.1.1.1.6.2  yamt 		return;
    134  1.1.1.1.6.2  yamt 	}
    135  1.1.1.1.6.2  yamt 
    136  1.1.1.1.6.2  yamt 	if (nd.nd_repstat & NFSERR_AUTHERR) {
    137  1.1.1.1.6.2  yamt 		svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
    138  1.1.1.1.6.2  yamt 		if (nd.nd_mreq != NULL)
    139  1.1.1.1.6.2  yamt 			m_freem(nd.nd_mreq);
    140  1.1.1.1.6.2  yamt 	} else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq))
    141  1.1.1.1.6.2  yamt 		svcerr_systemerr(rqst);
    142  1.1.1.1.6.2  yamt 	else
    143  1.1.1.1.6.2  yamt 		NFSCL_DEBUG(1, "cbrep sent\n");
    144  1.1.1.1.6.2  yamt 	svc_freereq(rqst);
    145  1.1.1.1.6.2  yamt }
    146  1.1.1.1.6.2  yamt 
    147  1.1.1.1.6.2  yamt /*
    148  1.1.1.1.6.2  yamt  * Check the cache and, optionally, do the RPC.
    149  1.1.1.1.6.2  yamt  * Return the appropriate cache response.
    150  1.1.1.1.6.2  yamt  */
    151  1.1.1.1.6.2  yamt static int
    152  1.1.1.1.6.2  yamt nfs_cbproc(struct nfsrv_descript *nd, u_int32_t xid)
    153  1.1.1.1.6.2  yamt {
    154  1.1.1.1.6.2  yamt 	struct thread *td = curthread;
    155  1.1.1.1.6.2  yamt 	int cacherep;
    156  1.1.1.1.6.2  yamt 
    157  1.1.1.1.6.2  yamt 	if (nd->nd_nam2 == NULL)
    158  1.1.1.1.6.2  yamt 		nd->nd_flag |= ND_STREAMSOCK;
    159  1.1.1.1.6.2  yamt 
    160  1.1.1.1.6.2  yamt 	nfscl_docb(nd, td);
    161  1.1.1.1.6.2  yamt 	if (nd->nd_repstat == NFSERR_DONTREPLY)
    162  1.1.1.1.6.2  yamt 		cacherep = RC_DROPIT;
    163  1.1.1.1.6.2  yamt 	else
    164  1.1.1.1.6.2  yamt 		cacherep = RC_REPLY;
    165  1.1.1.1.6.2  yamt 	return (cacherep);
    166  1.1.1.1.6.2  yamt }
    167  1.1.1.1.6.2  yamt 
    168  1.1.1.1.6.2  yamt /*
    169  1.1.1.1.6.2  yamt  * Adds a socket to the list for servicing by nfscbds.
    170  1.1.1.1.6.2  yamt  */
    171  1.1.1.1.6.2  yamt int
    172  1.1.1.1.6.2  yamt nfscbd_addsock(struct file *fp)
    173  1.1.1.1.6.2  yamt {
    174  1.1.1.1.6.2  yamt 	int siz;
    175  1.1.1.1.6.2  yamt 	struct socket *so;
    176  1.1.1.1.6.2  yamt 	int error;
    177  1.1.1.1.6.2  yamt 	SVCXPRT *xprt;
    178  1.1.1.1.6.2  yamt 
    179  1.1.1.1.6.2  yamt 	so = fp->f_data;
    180  1.1.1.1.6.2  yamt 
    181  1.1.1.1.6.2  yamt 	siz = sb_max_adj;
    182  1.1.1.1.6.2  yamt 	error = soreserve(so, siz, siz);
    183  1.1.1.1.6.2  yamt 	if (error)
    184  1.1.1.1.6.2  yamt 		return (error);
    185  1.1.1.1.6.2  yamt 
    186  1.1.1.1.6.2  yamt 	/*
    187  1.1.1.1.6.2  yamt 	 * Steal the socket from userland so that it doesn't close
    188  1.1.1.1.6.2  yamt 	 * unexpectedly.
    189  1.1.1.1.6.2  yamt 	 */
    190  1.1.1.1.6.2  yamt 	if (so->so_type == SOCK_DGRAM)
    191  1.1.1.1.6.2  yamt 		xprt = svc_dg_create(nfscbd_pool, so, 0, 0);
    192  1.1.1.1.6.2  yamt 	else
    193  1.1.1.1.6.2  yamt 		xprt = svc_vc_create(nfscbd_pool, so, 0, 0);
    194  1.1.1.1.6.2  yamt 	if (xprt) {
    195  1.1.1.1.6.2  yamt 		fp->f_ops = &badfileops;
    196  1.1.1.1.6.2  yamt 		fp->f_data = NULL;
    197  1.1.1.1.6.2  yamt 		svc_reg(xprt, NFS_CALLBCKPROG, NFSV4_CBVERS, nfscb_program,
    198  1.1.1.1.6.2  yamt 		    NULL);
    199  1.1.1.1.6.2  yamt 		SVC_RELEASE(xprt);
    200  1.1.1.1.6.2  yamt 	}
    201  1.1.1.1.6.2  yamt 
    202  1.1.1.1.6.2  yamt 	return (0);
    203  1.1.1.1.6.2  yamt }
    204  1.1.1.1.6.2  yamt 
    205  1.1.1.1.6.2  yamt /*
    206  1.1.1.1.6.2  yamt  * Called by nfssvc() for nfscbds. Just loops around servicing rpc requests
    207  1.1.1.1.6.2  yamt  * until it is killed by a signal.
    208  1.1.1.1.6.2  yamt  *
    209  1.1.1.1.6.2  yamt  * For now, only support callbacks via RPCSEC_GSS if there is a KerberosV
    210  1.1.1.1.6.2  yamt  * keytab entry with a host based entry in it on the client. (I'm not even
    211  1.1.1.1.6.2  yamt  * sure that getting Acceptor credentials for a user principal with a
    212  1.1.1.1.6.2  yamt  * credentials cache is possible, but even if it is, major changes to the
    213  1.1.1.1.6.2  yamt  * kgssapi would be required.)
    214  1.1.1.1.6.2  yamt  * I don't believe that this is a serious limitation since, as of 2009, most
    215  1.1.1.1.6.2  yamt  * NFSv4 servers supporting callbacks are using AUTH_SYS for callbacks even
    216  1.1.1.1.6.2  yamt  * when the client is using RPCSEC_GSS. (This BSD server uses AUTH_SYS
    217  1.1.1.1.6.2  yamt  * for callbacks unless nfsrv_gsscallbackson is set non-zero.)
    218  1.1.1.1.6.2  yamt  */
    219  1.1.1.1.6.2  yamt int
    220  1.1.1.1.6.2  yamt nfscbd_nfsd(struct thread *td, struct nfsd_nfscbd_args *args)
    221  1.1.1.1.6.2  yamt {
    222  1.1.1.1.6.2  yamt 	char principal[128];
    223  1.1.1.1.6.2  yamt 	int error;
    224  1.1.1.1.6.2  yamt 
    225  1.1.1.1.6.2  yamt 	if (args != NULL) {
    226  1.1.1.1.6.2  yamt 		error = copyinstr(args->principal, principal,
    227  1.1.1.1.6.2  yamt 		    sizeof(principal), NULL);
    228  1.1.1.1.6.2  yamt 		if (error)
    229  1.1.1.1.6.2  yamt 			return (error);
    230  1.1.1.1.6.2  yamt 	} else {
    231  1.1.1.1.6.2  yamt 		principal[0] = '\0';
    232  1.1.1.1.6.2  yamt 	}
    233  1.1.1.1.6.2  yamt 
    234  1.1.1.1.6.2  yamt 	/*
    235  1.1.1.1.6.2  yamt 	 * Only the first nfsd actually does any work. The RPC code
    236  1.1.1.1.6.2  yamt 	 * adds threads to it as needed. Any extra processes offered
    237  1.1.1.1.6.2  yamt 	 * by nfsd just exit. If nfsd is new enough, it will call us
    238  1.1.1.1.6.2  yamt 	 * once with a structure that specifies how many threads to
    239  1.1.1.1.6.2  yamt 	 * use.
    240  1.1.1.1.6.2  yamt 	 */
    241  1.1.1.1.6.2  yamt 	NFSD_LOCK();
    242  1.1.1.1.6.2  yamt 	if (nfs_numnfscbd == 0) {
    243  1.1.1.1.6.2  yamt 		nfs_numnfscbd++;
    244  1.1.1.1.6.2  yamt 
    245  1.1.1.1.6.2  yamt 		NFSD_UNLOCK();
    246  1.1.1.1.6.2  yamt 
    247  1.1.1.1.6.2  yamt 		if (principal[0] != '\0')
    248  1.1.1.1.6.2  yamt 			rpc_gss_set_svc_name_call(principal, "kerberosv5",
    249  1.1.1.1.6.2  yamt 			    GSS_C_INDEFINITE, NFS_CALLBCKPROG, NFSV4_CBVERS);
    250  1.1.1.1.6.2  yamt 
    251  1.1.1.1.6.2  yamt 		nfscbd_pool->sp_minthreads = 4;
    252  1.1.1.1.6.2  yamt 		nfscbd_pool->sp_maxthreads = 4;
    253  1.1.1.1.6.2  yamt 
    254  1.1.1.1.6.2  yamt 		svc_run(nfscbd_pool);
    255  1.1.1.1.6.2  yamt 
    256  1.1.1.1.6.2  yamt 		rpc_gss_clear_svc_name_call(NFS_CALLBCKPROG, NFSV4_CBVERS);
    257  1.1.1.1.6.2  yamt 
    258  1.1.1.1.6.2  yamt 		NFSD_LOCK();
    259  1.1.1.1.6.2  yamt 		nfs_numnfscbd--;
    260  1.1.1.1.6.2  yamt 		nfsrvd_cbinit(1);
    261  1.1.1.1.6.2  yamt 	}
    262  1.1.1.1.6.2  yamt 	NFSD_UNLOCK();
    263  1.1.1.1.6.2  yamt 
    264  1.1.1.1.6.2  yamt 	return (0);
    265  1.1.1.1.6.2  yamt }
    266  1.1.1.1.6.2  yamt 
    267  1.1.1.1.6.2  yamt /*
    268  1.1.1.1.6.2  yamt  * Initialize the data structures for the server.
    269  1.1.1.1.6.2  yamt  * Handshake with any new nfsds starting up to avoid any chance of
    270  1.1.1.1.6.2  yamt  * corruption.
    271  1.1.1.1.6.2  yamt  */
    272  1.1.1.1.6.2  yamt void
    273  1.1.1.1.6.2  yamt nfsrvd_cbinit(int terminating)
    274  1.1.1.1.6.2  yamt {
    275  1.1.1.1.6.2  yamt 
    276  1.1.1.1.6.2  yamt 	NFSD_LOCK_ASSERT();
    277  1.1.1.1.6.2  yamt 
    278  1.1.1.1.6.2  yamt 	if (terminating) {
    279  1.1.1.1.6.2  yamt 		/* Wait for any xprt registrations to complete. */
    280  1.1.1.1.6.2  yamt 		while (nfs_numnfscbd > 0)
    281  1.1.1.1.6.2  yamt 			msleep(&nfs_numnfscbd, NFSDLOCKMUTEXPTR, PZERO,
    282  1.1.1.1.6.2  yamt 			    "nfscbdt", 0);
    283  1.1.1.1.6.2  yamt 	}
    284  1.1.1.1.6.2  yamt 
    285  1.1.1.1.6.2  yamt 	if (nfscbd_pool == NULL) {
    286  1.1.1.1.6.2  yamt 		NFSD_UNLOCK();
    287  1.1.1.1.6.2  yamt 		nfscbd_pool = svcpool_create("nfscbd", NULL);
    288  1.1.1.1.6.2  yamt 		nfscbd_pool->sp_rcache = NULL;
    289  1.1.1.1.6.2  yamt 		nfscbd_pool->sp_assign = NULL;
    290  1.1.1.1.6.2  yamt 		nfscbd_pool->sp_done = NULL;
    291  1.1.1.1.6.2  yamt 		NFSD_LOCK();
    292  1.1.1.1.6.2  yamt 	}
    293  1.1.1.1.6.2  yamt }
    294  1.1.1.1.6.2  yamt 
    295