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