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