xref: /freebsd/sys/fs/nfsclient/nfs_clkrpc.c (revision 830940567b49bb0c08dfaed40418999e76616909)
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 SYSCTL_DECL(_vfs_newnfs);
50 
51 SVCPOOL		*nfscbd_pool;
52 
53 static int nfs_cbproc(struct nfsrv_descript *, u_int32_t);
54 
55 extern u_long sb_max_adj;
56 extern int nfs_numnfscbd;
57 
58 /*
59  * NFS client system calls for handling callbacks.
60  */
61 
62 /*
63  * Handles server to client callbacks.
64  */
65 static void
66 nfscb_program(struct svc_req *rqst, SVCXPRT *xprt)
67 {
68 	struct nfsrv_descript nd;
69 	int cacherep, credflavor;
70 
71 	memset(&nd, 0, sizeof(nd));
72 	if (rqst->rq_proc != NFSPROC_NULL &&
73 	    rqst->rq_proc != NFSV4PROC_CBCOMPOUND) {
74 		svcerr_noproc(rqst);
75 		svc_freereq(rqst);
76 		return;
77 	}
78 	nd.nd_procnum = rqst->rq_proc;
79 	nd.nd_flag = (ND_NFSCB | ND_NFSV4);
80 
81 	/*
82 	 * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
83 	 * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
84 	 * mounts.
85 	 */
86 	nd.nd_mrep = rqst->rq_args;
87 	rqst->rq_args = NULL;
88 	newnfs_realign(&nd.nd_mrep);
89 	nd.nd_md = nd.nd_mrep;
90 	nd.nd_dpos = mtod(nd.nd_md, caddr_t);
91 	nd.nd_nam = svc_getrpccaller(rqst);
92 	nd.nd_nam2 = rqst->rq_addr;
93 	nd.nd_mreq = NULL;
94 	nd.nd_cred = NULL;
95 
96 	if (nd.nd_procnum != NFSPROC_NULL) {
97 		if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {
98 			svcerr_weakauth(rqst);
99 			svc_freereq(rqst);
100 			m_freem(nd.nd_mrep);
101 			return;
102 		}
103 
104 		/* For now, I don't care what credential flavor was used. */
105 #ifdef notyet
106 #ifdef MAC
107 		mac_cred_associate_nfsd(nd.nd_cred);
108 #endif
109 #endif
110 		cacherep = nfs_cbproc(&nd, rqst->rq_xid);
111 	} else {
112 		NFSMGET(nd.nd_mreq);
113 		nd.nd_mreq->m_len = 0;
114 		cacherep = RC_REPLY;
115 	}
116 	if (nd.nd_mrep != NULL)
117 		m_freem(nd.nd_mrep);
118 
119 	if (nd.nd_cred != NULL)
120 		crfree(nd.nd_cred);
121 
122 	if (cacherep == RC_DROPIT) {
123 		if (nd.nd_mreq != NULL)
124 			m_freem(nd.nd_mreq);
125 		svc_freereq(rqst);
126 		return;
127 	}
128 
129 	if (nd.nd_mreq == NULL) {
130 		svcerr_decode(rqst);
131 		svc_freereq(rqst);
132 		return;
133 	}
134 
135 	if (nd.nd_repstat & NFSERR_AUTHERR) {
136 		svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
137 		if (nd.nd_mreq != NULL)
138 			m_freem(nd.nd_mreq);
139 	} else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) {
140 		svcerr_systemerr(rqst);
141 	}
142 	svc_freereq(rqst);
143 }
144 
145 /*
146  * Check the cache and, optionally, do the RPC.
147  * Return the appropriate cache response.
148  */
149 static int
150 nfs_cbproc(struct nfsrv_descript *nd, u_int32_t xid)
151 {
152 	struct thread *td = curthread;
153 	int cacherep;
154 
155 	if (nd->nd_nam2 == NULL)
156 		nd->nd_flag |= ND_STREAMSOCK;
157 
158 	nfscl_docb(nd, td);
159 	if (nd->nd_repstat == NFSERR_DONTREPLY)
160 		cacherep = RC_DROPIT;
161 	else
162 		cacherep = RC_REPLY;
163 	return (cacherep);
164 }
165 
166 /*
167  * Adds a socket to the list for servicing by nfscbds.
168  */
169 int
170 nfscbd_addsock(struct file *fp)
171 {
172 	int siz;
173 	struct socket *so;
174 	int error;
175 	SVCXPRT *xprt;
176 
177 	so = fp->f_data;
178 
179 	siz = sb_max_adj;
180 	error = soreserve(so, siz, siz);
181 	if (error)
182 		return (error);
183 
184 	/*
185 	 * Steal the socket from userland so that it doesn't close
186 	 * unexpectedly.
187 	 */
188 	if (so->so_type == SOCK_DGRAM)
189 		xprt = svc_dg_create(nfscbd_pool, so, 0, 0);
190 	else
191 		xprt = svc_vc_create(nfscbd_pool, so, 0, 0);
192 	if (xprt) {
193 		fp->f_ops = &badfileops;
194 		fp->f_data = NULL;
195 		svc_reg(xprt, NFS_CALLBCKPROG, NFSV4_CBVERS, nfscb_program,
196 		    NULL);
197 		SVC_RELEASE(xprt);
198 	}
199 
200 	return (0);
201 }
202 
203 /*
204  * Called by nfssvc() for nfscbds. Just loops around servicing rpc requests
205  * until it is killed by a signal.
206  *
207  * For now, only support callbacks via RPCSEC_GSS if there is a KerberosV
208  * keytab entry with a host based entry in it on the client. (I'm not even
209  * sure that getting Acceptor credentials for a user principal with a
210  * credentials cache is possible, but even if it is, major changes to the
211  * kgssapi would be required.)
212  * I don't believe that this is a serious limitation since, as of 2009, most
213  * NFSv4 servers supporting callbacks are using AUTH_SYS for callbacks even
214  * when the client is using RPCSEC_GSS. (This BSD server uses AUTH_SYS
215  * for callbacks unless nfsrv_gsscallbackson is set non-zero.)
216  */
217 int
218 nfscbd_nfsd(struct thread *td, struct nfsd_nfscbd_args *args)
219 {
220 #ifdef KGSSAPI
221 	char principal[128];
222 	int error;
223 #endif
224 
225 #ifdef KGSSAPI
226 	if (args != NULL) {
227 		error = copyinstr(args->principal, principal,
228 		    sizeof(principal), NULL);
229 		if (error)
230 			return (error);
231 	} else {
232 		principal[0] = '\0';
233 	}
234 #endif
235 
236 	/*
237 	 * Only the first nfsd actually does any work. The RPC code
238 	 * adds threads to it as needed. Any extra processes offered
239 	 * by nfsd just exit. If nfsd is new enough, it will call us
240 	 * once with a structure that specifies how many threads to
241 	 * use.
242 	 */
243 	NFSD_LOCK();
244 	if (nfs_numnfscbd == 0) {
245 		nfs_numnfscbd++;
246 
247 		NFSD_UNLOCK();
248 
249 #ifdef KGSSAPI
250 		if (principal[0] != '\0')
251 			rpc_gss_set_svc_name(principal, "kerberosv5",
252 			    GSS_C_INDEFINITE, NFS_CALLBCKPROG, NFSV4_CBVERS);
253 #endif
254 
255 		nfscbd_pool->sp_minthreads = 4;
256 		nfscbd_pool->sp_maxthreads = 4;
257 
258 		svc_run(nfscbd_pool);
259 
260 #ifdef KGSSAPI
261 		rpc_gss_clear_svc_name(NFS_CALLBCKPROG, NFSV4_CBVERS);
262 #endif
263 
264 		NFSD_LOCK();
265 		nfs_numnfscbd--;
266 		nfsrvd_cbinit(1);
267 	}
268 	NFSD_UNLOCK();
269 
270 	return (0);
271 }
272 
273 /*
274  * Initialize the data structures for the server.
275  * Handshake with any new nfsds starting up to avoid any chance of
276  * corruption.
277  */
278 void
279 nfsrvd_cbinit(int terminating)
280 {
281 
282 	NFSD_LOCK_ASSERT();
283 
284 	if (terminating) {
285 		NFSD_UNLOCK();
286 		svcpool_destroy(nfscbd_pool);
287 		nfscbd_pool = NULL;
288 		NFSD_LOCK();
289 	}
290 
291 	NFSD_UNLOCK();
292 
293 	nfscbd_pool = svcpool_create("nfscbd", NULL);
294 	nfscbd_pool->sp_rcache = NULL;
295 	nfscbd_pool->sp_assign = NULL;
296 	nfscbd_pool->sp_done = NULL;
297 
298 	NFSD_LOCK();
299 }
300 
301