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