nfs_nfsdkrpc.c revision 1.2.10.3 1 1.2.10.2 tls /* $NetBSD: nfs_nfsdkrpc.c,v 1.2.10.3 2017/12/03 11:38:42 jdolecek Exp $ */
2 1.2.10.2 tls /*-
3 1.2.10.2 tls * Copyright (c) 1989, 1993
4 1.2.10.2 tls * The Regents of the University of California. All rights reserved.
5 1.2.10.2 tls *
6 1.2.10.2 tls * This code is derived from software contributed to Berkeley by
7 1.2.10.2 tls * Rick Macklem at The University of Guelph.
8 1.2.10.2 tls *
9 1.2.10.2 tls * Redistribution and use in source and binary forms, with or without
10 1.2.10.2 tls * modification, are permitted provided that the following conditions
11 1.2.10.2 tls * are met:
12 1.2.10.2 tls * 1. Redistributions of source code must retain the above copyright
13 1.2.10.2 tls * notice, this list of conditions and the following disclaimer.
14 1.2.10.2 tls * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.10.2 tls * notice, this list of conditions and the following disclaimer in the
16 1.2.10.2 tls * documentation and/or other materials provided with the distribution.
17 1.2.10.2 tls * 4. Neither the name of the University nor the names of its contributors
18 1.2.10.2 tls * may be used to endorse or promote products derived from this software
19 1.2.10.2 tls * without specific prior written permission.
20 1.2.10.2 tls *
21 1.2.10.2 tls * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.2.10.2 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.2.10.2 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.2.10.2 tls * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.2.10.2 tls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.2.10.2 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.2.10.2 tls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.2.10.2 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.2.10.2 tls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.2.10.2 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.2.10.2 tls * SUCH DAMAGE.
32 1.2.10.2 tls *
33 1.2.10.2 tls */
34 1.2.10.2 tls
35 1.2.10.2 tls #include <sys/cdefs.h>
36 1.2.10.3 jdolecek /* __FBSDID("FreeBSD: head/sys/fs/nfsserver/nfs_nfsdkrpc.c 299203 2016-05-06 23:40:37Z pfg "); */
37 1.2.10.2 tls __RCSID("$NetBSD: nfs_nfsdkrpc.c,v 1.2.10.3 2017/12/03 11:38:42 jdolecek Exp $");
38 1.2.10.2 tls
39 1.2.10.3 jdolecek #ifdef _KERNEL_OPT
40 1.2.10.2 tls #include "opt_inet6.h"
41 1.2.10.3 jdolecek #if 0
42 1.2.10.2 tls #include "opt_kgssapi.h"
43 1.2.10.3 jdolecek #endif
44 1.2.10.3 jdolecek #endif
45 1.2.10.2 tls
46 1.2.10.3 jdolecek #include <fs/nfs/common/nfsport.h>
47 1.2.10.2 tls
48 1.2.10.2 tls #include <rpc/rpc.h>
49 1.2.10.2 tls #include <rpc/rpcsec_gss.h>
50 1.2.10.2 tls
51 1.2.10.3 jdolecek #include <fs/nfs/common/nfs_fha.h>
52 1.2.10.3 jdolecek #include <fs/nfs/server/nfs_fha_new.h>
53 1.2.10.2 tls
54 1.2.10.3 jdolecek #if 0
55 1.2.10.2 tls #include <security/mac/mac_framework.h>
56 1.2.10.3 jdolecek #endif
57 1.2.10.2 tls
58 1.2.10.2 tls NFSDLOCKMUTEX;
59 1.2.10.2 tls NFSV4ROOTLOCKMUTEX;
60 1.2.10.2 tls struct nfsv4lock nfsd_suspend_lock;
61 1.2.10.2 tls
62 1.2.10.2 tls /*
63 1.2.10.2 tls * Mapping of old NFS Version 2 RPC numbers to generic numbers.
64 1.2.10.2 tls */
65 1.2.10.2 tls int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
66 1.2.10.2 tls NFSPROC_NULL,
67 1.2.10.2 tls NFSPROC_GETATTR,
68 1.2.10.2 tls NFSPROC_SETATTR,
69 1.2.10.2 tls NFSPROC_NOOP,
70 1.2.10.2 tls NFSPROC_LOOKUP,
71 1.2.10.2 tls NFSPROC_READLINK,
72 1.2.10.2 tls NFSPROC_READ,
73 1.2.10.2 tls NFSPROC_NOOP,
74 1.2.10.2 tls NFSPROC_WRITE,
75 1.2.10.2 tls NFSPROC_CREATE,
76 1.2.10.2 tls NFSPROC_REMOVE,
77 1.2.10.2 tls NFSPROC_RENAME,
78 1.2.10.2 tls NFSPROC_LINK,
79 1.2.10.2 tls NFSPROC_SYMLINK,
80 1.2.10.2 tls NFSPROC_MKDIR,
81 1.2.10.2 tls NFSPROC_RMDIR,
82 1.2.10.2 tls NFSPROC_READDIR,
83 1.2.10.2 tls NFSPROC_FSSTAT,
84 1.2.10.2 tls NFSPROC_NOOP,
85 1.2.10.2 tls NFSPROC_NOOP,
86 1.2.10.2 tls NFSPROC_NOOP,
87 1.2.10.2 tls NFSPROC_NOOP,
88 1.2.10.2 tls };
89 1.2.10.2 tls
90 1.2.10.2 tls
91 1.2.10.2 tls SYSCTL_DECL(_vfs_nfsd);
92 1.2.10.2 tls
93 1.2.10.2 tls SVCPOOL *nfsrvd_pool;
94 1.2.10.2 tls
95 1.2.10.2 tls static int nfs_privport = 0;
96 1.2.10.3 jdolecek SYSCTL_INT(_vfs_nfsd, OID_AUTO, nfs_privport, CTLFLAG_RWTUN,
97 1.2.10.2 tls &nfs_privport, 0,
98 1.2.10.2 tls "Only allow clients using a privileged port for NFSv2 and 3");
99 1.2.10.2 tls
100 1.2.10.2 tls static int nfs_minvers = NFS_VER2;
101 1.2.10.3 jdolecek SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_min_nfsvers, CTLFLAG_RWTUN,
102 1.2.10.2 tls &nfs_minvers, 0, "The lowest version of NFS handled by the server");
103 1.2.10.2 tls
104 1.2.10.2 tls static int nfs_maxvers = NFS_VER4;
105 1.2.10.3 jdolecek SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_max_nfsvers, CTLFLAG_RWTUN,
106 1.2.10.2 tls &nfs_maxvers, 0, "The highest version of NFS handled by the server");
107 1.2.10.2 tls
108 1.2.10.3 jdolecek static int nfs_proc(struct nfsrv_descript *, u_int32_t, SVCXPRT *xprt,
109 1.2.10.3 jdolecek struct nfsrvcache **);
110 1.2.10.2 tls
111 1.2.10.2 tls extern u_long sb_max_adj;
112 1.2.10.2 tls extern int newnfs_numnfsd;
113 1.2.10.2 tls extern struct proc *nfsd_master_proc;
114 1.2.10.2 tls
115 1.2.10.2 tls /*
116 1.2.10.2 tls * NFS server system calls
117 1.2.10.2 tls */
118 1.2.10.2 tls
119 1.2.10.2 tls static void
120 1.2.10.2 tls nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
121 1.2.10.2 tls {
122 1.2.10.2 tls struct nfsrv_descript nd;
123 1.2.10.2 tls struct nfsrvcache *rp = NULL;
124 1.2.10.2 tls int cacherep, credflavor;
125 1.2.10.2 tls
126 1.2.10.2 tls memset(&nd, 0, sizeof(nd));
127 1.2.10.2 tls if (rqst->rq_vers == NFS_VER2) {
128 1.2.10.3 jdolecek if (rqst->rq_proc > NFSV2PROC_STATFS ||
129 1.2.10.3 jdolecek newnfs_nfsv3_procid[rqst->rq_proc] == NFSPROC_NOOP) {
130 1.2.10.2 tls svcerr_noproc(rqst);
131 1.2.10.2 tls svc_freereq(rqst);
132 1.2.10.2 tls goto out;
133 1.2.10.2 tls }
134 1.2.10.2 tls nd.nd_procnum = newnfs_nfsv3_procid[rqst->rq_proc];
135 1.2.10.2 tls nd.nd_flag = ND_NFSV2;
136 1.2.10.2 tls } else if (rqst->rq_vers == NFS_VER3) {
137 1.2.10.2 tls if (rqst->rq_proc >= NFS_V3NPROCS) {
138 1.2.10.2 tls svcerr_noproc(rqst);
139 1.2.10.2 tls svc_freereq(rqst);
140 1.2.10.2 tls goto out;
141 1.2.10.2 tls }
142 1.2.10.2 tls nd.nd_procnum = rqst->rq_proc;
143 1.2.10.2 tls nd.nd_flag = ND_NFSV3;
144 1.2.10.2 tls } else {
145 1.2.10.2 tls if (rqst->rq_proc != NFSPROC_NULL &&
146 1.2.10.2 tls rqst->rq_proc != NFSV4PROC_COMPOUND) {
147 1.2.10.2 tls svcerr_noproc(rqst);
148 1.2.10.2 tls svc_freereq(rqst);
149 1.2.10.2 tls goto out;
150 1.2.10.2 tls }
151 1.2.10.2 tls nd.nd_procnum = rqst->rq_proc;
152 1.2.10.2 tls nd.nd_flag = ND_NFSV4;
153 1.2.10.2 tls }
154 1.2.10.2 tls
155 1.2.10.2 tls /*
156 1.2.10.2 tls * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
157 1.2.10.2 tls * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
158 1.2.10.2 tls * mounts.
159 1.2.10.2 tls */
160 1.2.10.2 tls nd.nd_mrep = rqst->rq_args;
161 1.2.10.2 tls rqst->rq_args = NULL;
162 1.2.10.2 tls newnfs_realign(&nd.nd_mrep, M_WAITOK);
163 1.2.10.2 tls nd.nd_md = nd.nd_mrep;
164 1.2.10.2 tls nd.nd_dpos = mtod(nd.nd_md, caddr_t);
165 1.2.10.2 tls nd.nd_nam = svc_getrpccaller(rqst);
166 1.2.10.2 tls nd.nd_nam2 = rqst->rq_addr;
167 1.2.10.2 tls nd.nd_mreq = NULL;
168 1.2.10.2 tls nd.nd_cred = NULL;
169 1.2.10.2 tls
170 1.2.10.2 tls if (nfs_privport && (nd.nd_flag & ND_NFSV4) == 0) {
171 1.2.10.2 tls /* Check if source port is privileged */
172 1.2.10.2 tls u_short port;
173 1.2.10.2 tls struct sockaddr *nam = nd.nd_nam;
174 1.2.10.2 tls struct sockaddr_in *sin;
175 1.2.10.2 tls
176 1.2.10.2 tls sin = (struct sockaddr_in *)nam;
177 1.2.10.2 tls /*
178 1.2.10.2 tls * INET/INET6 - same code:
179 1.2.10.2 tls * sin_port and sin6_port are at same offset
180 1.2.10.2 tls */
181 1.2.10.2 tls port = ntohs(sin->sin_port);
182 1.2.10.2 tls if (port >= IPPORT_RESERVED &&
183 1.2.10.2 tls nd.nd_procnum != NFSPROC_NULL) {
184 1.2.10.2 tls #ifdef INET6
185 1.2.10.2 tls char b6[INET6_ADDRSTRLEN];
186 1.2.10.3 jdolecek #if defined(_MODULE)
187 1.2.10.2 tls /* Do not use ip6_sprintf: the nfs module should work without INET6. */
188 1.2.10.2 tls #define ip6_sprintf(buf, a) \
189 1.2.10.2 tls (snprintf((buf), sizeof(buf), "%x:%x:%x:%x:%x:%x:%x:%x", \
190 1.2.10.2 tls (a)->s6_addr16[0], (a)->s6_addr16[1], \
191 1.2.10.2 tls (a)->s6_addr16[2], (a)->s6_addr16[3], \
192 1.2.10.2 tls (a)->s6_addr16[4], (a)->s6_addr16[5], \
193 1.2.10.2 tls (a)->s6_addr16[6], (a)->s6_addr16[7]), \
194 1.2.10.2 tls (buf))
195 1.2.10.2 tls #endif
196 1.2.10.2 tls #endif
197 1.2.10.2 tls printf("NFS request from unprivileged port (%s:%d)\n",
198 1.2.10.2 tls #ifdef INET6
199 1.2.10.2 tls sin->sin_family == AF_INET6 ?
200 1.2.10.2 tls ip6_sprintf(b6, &satosin6(sin)->sin6_addr) :
201 1.2.10.3 jdolecek #if defined(_MODULE)
202 1.2.10.2 tls #undef ip6_sprintf
203 1.2.10.2 tls #endif
204 1.2.10.2 tls #endif
205 1.2.10.2 tls inet_ntoa(sin->sin_addr), port);
206 1.2.10.2 tls svcerr_weakauth(rqst);
207 1.2.10.2 tls svc_freereq(rqst);
208 1.2.10.2 tls m_freem(nd.nd_mrep);
209 1.2.10.2 tls goto out;
210 1.2.10.2 tls }
211 1.2.10.2 tls }
212 1.2.10.2 tls
213 1.2.10.2 tls if (nd.nd_procnum != NFSPROC_NULL) {
214 1.2.10.2 tls if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {
215 1.2.10.2 tls svcerr_weakauth(rqst);
216 1.2.10.2 tls svc_freereq(rqst);
217 1.2.10.2 tls m_freem(nd.nd_mrep);
218 1.2.10.2 tls goto out;
219 1.2.10.2 tls }
220 1.2.10.2 tls
221 1.2.10.2 tls /* Set the flag based on credflavor */
222 1.2.10.2 tls if (credflavor == RPCSEC_GSS_KRB5) {
223 1.2.10.2 tls nd.nd_flag |= ND_GSS;
224 1.2.10.2 tls } else if (credflavor == RPCSEC_GSS_KRB5I) {
225 1.2.10.2 tls nd.nd_flag |= (ND_GSS | ND_GSSINTEGRITY);
226 1.2.10.2 tls } else if (credflavor == RPCSEC_GSS_KRB5P) {
227 1.2.10.2 tls nd.nd_flag |= (ND_GSS | ND_GSSPRIVACY);
228 1.2.10.2 tls } else if (credflavor != AUTH_SYS) {
229 1.2.10.2 tls svcerr_weakauth(rqst);
230 1.2.10.2 tls svc_freereq(rqst);
231 1.2.10.2 tls m_freem(nd.nd_mrep);
232 1.2.10.2 tls goto out;
233 1.2.10.2 tls }
234 1.2.10.2 tls
235 1.2.10.2 tls #ifdef MAC
236 1.2.10.2 tls mac_cred_associate_nfsd(nd.nd_cred);
237 1.2.10.2 tls #endif
238 1.2.10.2 tls /*
239 1.2.10.2 tls * Get a refcnt (shared lock) on nfsd_suspend_lock.
240 1.2.10.2 tls * NFSSVC_SUSPENDNFSD will take an exclusive lock on
241 1.2.10.2 tls * nfsd_suspend_lock to suspend these threads.
242 1.2.10.3 jdolecek * The call to nfsv4_lock() that precedes nfsv4_getref()
243 1.2.10.3 jdolecek * ensures that the acquisition of the exclusive lock
244 1.2.10.3 jdolecek * takes priority over acquisition of the shared lock by
245 1.2.10.3 jdolecek * waiting for any exclusive lock request to complete.
246 1.2.10.2 tls * This must be done here, before the check of
247 1.2.10.2 tls * nfsv4root exports by nfsvno_v4rootexport().
248 1.2.10.2 tls */
249 1.2.10.2 tls NFSLOCKV4ROOTMUTEX();
250 1.2.10.3 jdolecek nfsv4_lock(&nfsd_suspend_lock, 0, NULL, NFSV4ROOTLOCKMUTEXPTR,
251 1.2.10.3 jdolecek NULL);
252 1.2.10.2 tls nfsv4_getref(&nfsd_suspend_lock, NULL, NFSV4ROOTLOCKMUTEXPTR,
253 1.2.10.2 tls NULL);
254 1.2.10.2 tls NFSUNLOCKV4ROOTMUTEX();
255 1.2.10.2 tls
256 1.2.10.2 tls if ((nd.nd_flag & ND_NFSV4) != 0) {
257 1.2.10.2 tls nd.nd_repstat = nfsvno_v4rootexport(&nd);
258 1.2.10.2 tls if (nd.nd_repstat != 0) {
259 1.2.10.2 tls NFSLOCKV4ROOTMUTEX();
260 1.2.10.2 tls nfsv4_relref(&nfsd_suspend_lock);
261 1.2.10.2 tls NFSUNLOCKV4ROOTMUTEX();
262 1.2.10.2 tls svcerr_weakauth(rqst);
263 1.2.10.2 tls svc_freereq(rqst);
264 1.2.10.2 tls m_freem(nd.nd_mrep);
265 1.2.10.2 tls goto out;
266 1.2.10.2 tls }
267 1.2.10.2 tls }
268 1.2.10.2 tls
269 1.2.10.3 jdolecek cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp);
270 1.2.10.2 tls NFSLOCKV4ROOTMUTEX();
271 1.2.10.2 tls nfsv4_relref(&nfsd_suspend_lock);
272 1.2.10.2 tls NFSUNLOCKV4ROOTMUTEX();
273 1.2.10.2 tls } else {
274 1.2.10.2 tls NFSMGET(nd.nd_mreq);
275 1.2.10.2 tls nd.nd_mreq->m_len = 0;
276 1.2.10.2 tls cacherep = RC_REPLY;
277 1.2.10.2 tls }
278 1.2.10.2 tls if (nd.nd_mrep != NULL)
279 1.2.10.2 tls m_freem(nd.nd_mrep);
280 1.2.10.2 tls
281 1.2.10.2 tls if (nd.nd_cred != NULL)
282 1.2.10.2 tls crfree(nd.nd_cred);
283 1.2.10.2 tls
284 1.2.10.2 tls if (cacherep == RC_DROPIT) {
285 1.2.10.2 tls if (nd.nd_mreq != NULL)
286 1.2.10.2 tls m_freem(nd.nd_mreq);
287 1.2.10.2 tls svc_freereq(rqst);
288 1.2.10.2 tls goto out;
289 1.2.10.2 tls }
290 1.2.10.2 tls
291 1.2.10.2 tls if (nd.nd_mreq == NULL) {
292 1.2.10.2 tls svcerr_decode(rqst);
293 1.2.10.2 tls svc_freereq(rqst);
294 1.2.10.2 tls goto out;
295 1.2.10.2 tls }
296 1.2.10.2 tls
297 1.2.10.2 tls if (nd.nd_repstat & NFSERR_AUTHERR) {
298 1.2.10.2 tls svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
299 1.2.10.2 tls if (nd.nd_mreq != NULL)
300 1.2.10.2 tls m_freem(nd.nd_mreq);
301 1.2.10.2 tls } else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) {
302 1.2.10.2 tls svcerr_systemerr(rqst);
303 1.2.10.2 tls }
304 1.2.10.3 jdolecek if (rp != NULL) {
305 1.2.10.3 jdolecek nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 ||
306 1.2.10.3 jdolecek SVC_ACK(xprt, NULL)), rqst->rq_reply_seq);
307 1.2.10.3 jdolecek }
308 1.2.10.2 tls svc_freereq(rqst);
309 1.2.10.2 tls
310 1.2.10.2 tls out:
311 1.2.10.3 jdolecek if (softdep_ast_cleanup != NULL)
312 1.2.10.3 jdolecek softdep_ast_cleanup();
313 1.2.10.2 tls NFSEXITCODE(0);
314 1.2.10.2 tls }
315 1.2.10.2 tls
316 1.2.10.2 tls /*
317 1.2.10.2 tls * Check the cache and, optionally, do the RPC.
318 1.2.10.2 tls * Return the appropriate cache response.
319 1.2.10.2 tls */
320 1.2.10.2 tls static int
321 1.2.10.3 jdolecek nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt,
322 1.2.10.3 jdolecek struct nfsrvcache **rpp)
323 1.2.10.2 tls {
324 1.2.10.2 tls struct thread *td = curthread;
325 1.2.10.3 jdolecek int cacherep = RC_DOIT, isdgram, taglen = -1;
326 1.2.10.3 jdolecek struct mbuf *m;
327 1.2.10.3 jdolecek u_char tag[NFSV4_SMALLSTR + 1], *tagstr = NULL;
328 1.2.10.3 jdolecek u_int32_t minorvers = 0;
329 1.2.10.3 jdolecek uint32_t ack;
330 1.2.10.2 tls
331 1.2.10.2 tls *rpp = NULL;
332 1.2.10.2 tls if (nd->nd_nam2 == NULL) {
333 1.2.10.2 tls nd->nd_flag |= ND_STREAMSOCK;
334 1.2.10.2 tls isdgram = 0;
335 1.2.10.2 tls } else {
336 1.2.10.2 tls isdgram = 1;
337 1.2.10.2 tls }
338 1.2.10.2 tls
339 1.2.10.2 tls /*
340 1.2.10.2 tls * Two cases:
341 1.2.10.2 tls * 1 - For NFSv2 over UDP, if we are near our malloc/mget
342 1.2.10.2 tls * limit, just drop the request. There is no
343 1.2.10.2 tls * NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the
344 1.2.10.2 tls * client will timeout/retry over UDP in a little while.
345 1.2.10.2 tls * 2 - nd_repstat == 0 && nd_mreq == NULL, which
346 1.2.10.2 tls * means a normal nfs rpc, so check the cache
347 1.2.10.2 tls */
348 1.2.10.2 tls if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL &&
349 1.2.10.2 tls nfsrv_mallocmget_limit()) {
350 1.2.10.2 tls cacherep = RC_DROPIT;
351 1.2.10.2 tls } else {
352 1.2.10.2 tls /*
353 1.2.10.2 tls * For NFSv3, play it safe and assume that the client is
354 1.2.10.2 tls * doing retries on the same TCP connection.
355 1.2.10.2 tls */
356 1.2.10.2 tls if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) ==
357 1.2.10.2 tls ND_STREAMSOCK)
358 1.2.10.2 tls nd->nd_flag |= ND_SAMETCPCONN;
359 1.2.10.2 tls nd->nd_retxid = xid;
360 1.2.10.2 tls nd->nd_tcpconntime = NFSD_MONOSEC;
361 1.2.10.3 jdolecek nd->nd_sockref = xprt->xp_sockref;
362 1.2.10.3 jdolecek if ((nd->nd_flag & ND_NFSV4) != 0)
363 1.2.10.3 jdolecek nfsd_getminorvers(nd, tag, &tagstr, &taglen,
364 1.2.10.3 jdolecek &minorvers);
365 1.2.10.3 jdolecek if ((nd->nd_flag & ND_NFSV41) != 0)
366 1.2.10.3 jdolecek /* NFSv4.1 caches replies in the session slots. */
367 1.2.10.3 jdolecek cacherep = RC_DOIT;
368 1.2.10.3 jdolecek else {
369 1.2.10.3 jdolecek cacherep = nfsrvd_getcache(nd);
370 1.2.10.3 jdolecek ack = 0;
371 1.2.10.3 jdolecek SVC_ACK(xprt, &ack);
372 1.2.10.3 jdolecek nfsrc_trimcache(xprt->xp_sockref, ack, 0);
373 1.2.10.3 jdolecek }
374 1.2.10.2 tls }
375 1.2.10.2 tls
376 1.2.10.2 tls /*
377 1.2.10.2 tls * Handle the request. There are three cases.
378 1.2.10.2 tls * RC_DOIT - do the RPC
379 1.2.10.2 tls * RC_REPLY - return the reply already created
380 1.2.10.2 tls * RC_DROPIT - just throw the request away
381 1.2.10.2 tls */
382 1.2.10.2 tls if (cacherep == RC_DOIT) {
383 1.2.10.3 jdolecek if ((nd->nd_flag & ND_NFSV41) != 0)
384 1.2.10.3 jdolecek nd->nd_xprt = xprt;
385 1.2.10.3 jdolecek nfsrvd_dorpc(nd, isdgram, tagstr, taglen, minorvers, td);
386 1.2.10.3 jdolecek if ((nd->nd_flag & ND_NFSV41) != 0) {
387 1.2.10.3 jdolecek if (nd->nd_repstat != NFSERR_REPLYFROMCACHE &&
388 1.2.10.3 jdolecek (nd->nd_flag & ND_SAVEREPLY) != 0) {
389 1.2.10.3 jdolecek /* Cache a copy of the reply. */
390 1.2.10.3 jdolecek m = m_copym(nd->nd_mreq, 0, M_COPYALL,
391 1.2.10.3 jdolecek M_WAITOK);
392 1.2.10.3 jdolecek } else
393 1.2.10.3 jdolecek m = NULL;
394 1.2.10.3 jdolecek if ((nd->nd_flag & ND_HASSEQUENCE) != 0)
395 1.2.10.3 jdolecek nfsrv_cache_session(nd->nd_sessionid,
396 1.2.10.3 jdolecek nd->nd_slotid, nd->nd_repstat, &m);
397 1.2.10.3 jdolecek if (nd->nd_repstat == NFSERR_REPLYFROMCACHE)
398 1.2.10.3 jdolecek nd->nd_repstat = 0;
399 1.2.10.2 tls cacherep = RC_REPLY;
400 1.2.10.3 jdolecek } else {
401 1.2.10.3 jdolecek if (nd->nd_repstat == NFSERR_DONTREPLY)
402 1.2.10.3 jdolecek cacherep = RC_DROPIT;
403 1.2.10.3 jdolecek else
404 1.2.10.3 jdolecek cacherep = RC_REPLY;
405 1.2.10.3 jdolecek *rpp = nfsrvd_updatecache(nd);
406 1.2.10.3 jdolecek }
407 1.2.10.2 tls }
408 1.2.10.3 jdolecek if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
409 1.2.10.3 jdolecek free(tagstr, M_TEMP);
410 1.2.10.2 tls
411 1.2.10.2 tls NFSEXITCODE2(0, nd);
412 1.2.10.2 tls return (cacherep);
413 1.2.10.2 tls }
414 1.2.10.2 tls
415 1.2.10.3 jdolecek static void
416 1.2.10.3 jdolecek nfssvc_loss(SVCXPRT *xprt)
417 1.2.10.3 jdolecek {
418 1.2.10.3 jdolecek uint32_t ack;
419 1.2.10.3 jdolecek
420 1.2.10.3 jdolecek ack = 0;
421 1.2.10.3 jdolecek SVC_ACK(xprt, &ack);
422 1.2.10.3 jdolecek nfsrc_trimcache(xprt->xp_sockref, ack, 1);
423 1.2.10.3 jdolecek }
424 1.2.10.3 jdolecek
425 1.2.10.2 tls /*
426 1.2.10.2 tls * Adds a socket to the list for servicing by nfsds.
427 1.2.10.2 tls */
428 1.2.10.2 tls int
429 1.2.10.2 tls nfsrvd_addsock(struct file *fp)
430 1.2.10.2 tls {
431 1.2.10.2 tls int siz;
432 1.2.10.2 tls struct socket *so;
433 1.2.10.2 tls int error = 0;
434 1.2.10.2 tls SVCXPRT *xprt;
435 1.2.10.2 tls static u_int64_t sockref = 0;
436 1.2.10.2 tls
437 1.2.10.2 tls so = fp->f_data;
438 1.2.10.2 tls
439 1.2.10.2 tls siz = sb_max_adj;
440 1.2.10.2 tls error = soreserve(so, siz, siz);
441 1.2.10.2 tls if (error)
442 1.2.10.2 tls goto out;
443 1.2.10.2 tls
444 1.2.10.2 tls /*
445 1.2.10.2 tls * Steal the socket from userland so that it doesn't close
446 1.2.10.2 tls * unexpectedly.
447 1.2.10.2 tls */
448 1.2.10.2 tls if (so->so_type == SOCK_DGRAM)
449 1.2.10.2 tls xprt = svc_dg_create(nfsrvd_pool, so, 0, 0);
450 1.2.10.2 tls else
451 1.2.10.2 tls xprt = svc_vc_create(nfsrvd_pool, so, 0, 0);
452 1.2.10.2 tls if (xprt) {
453 1.2.10.2 tls fp->f_ops = &badfileops;
454 1.2.10.2 tls fp->f_data = NULL;
455 1.2.10.2 tls xprt->xp_sockref = ++sockref;
456 1.2.10.2 tls if (nfs_minvers == NFS_VER2)
457 1.2.10.2 tls svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program,
458 1.2.10.2 tls NULL);
459 1.2.10.2 tls if (nfs_minvers <= NFS_VER3 && nfs_maxvers >= NFS_VER3)
460 1.2.10.2 tls svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program,
461 1.2.10.2 tls NULL);
462 1.2.10.2 tls if (nfs_maxvers >= NFS_VER4)
463 1.2.10.2 tls svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program,
464 1.2.10.2 tls NULL);
465 1.2.10.3 jdolecek if (so->so_type == SOCK_STREAM)
466 1.2.10.3 jdolecek svc_loss_reg(xprt, nfssvc_loss);
467 1.2.10.2 tls SVC_RELEASE(xprt);
468 1.2.10.2 tls }
469 1.2.10.2 tls
470 1.2.10.2 tls out:
471 1.2.10.2 tls NFSEXITCODE(error);
472 1.2.10.2 tls return (error);
473 1.2.10.2 tls }
474 1.2.10.2 tls
475 1.2.10.2 tls /*
476 1.2.10.2 tls * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
477 1.2.10.2 tls * until it is killed by a signal.
478 1.2.10.2 tls */
479 1.2.10.2 tls int
480 1.2.10.2 tls nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
481 1.2.10.2 tls {
482 1.2.10.2 tls char principal[MAXHOSTNAMELEN + 5];
483 1.2.10.3 jdolecek struct proc *p;
484 1.2.10.2 tls int error = 0;
485 1.2.10.2 tls bool_t ret2, ret3, ret4;
486 1.2.10.2 tls
487 1.2.10.2 tls error = copyinstr(args->principal, principal, sizeof (principal),
488 1.2.10.2 tls NULL);
489 1.2.10.2 tls if (error)
490 1.2.10.2 tls goto out;
491 1.2.10.2 tls
492 1.2.10.2 tls /*
493 1.2.10.2 tls * Only the first nfsd actually does any work. The RPC code
494 1.2.10.2 tls * adds threads to it as needed. Any extra processes offered
495 1.2.10.2 tls * by nfsd just exit. If nfsd is new enough, it will call us
496 1.2.10.2 tls * once with a structure that specifies how many threads to
497 1.2.10.2 tls * use.
498 1.2.10.2 tls */
499 1.2.10.2 tls NFSD_LOCK();
500 1.2.10.2 tls if (newnfs_numnfsd == 0) {
501 1.2.10.3 jdolecek p = td->td_proc;
502 1.2.10.3 jdolecek PROC_LOCK(p);
503 1.2.10.3 jdolecek p->p_flag2 |= P2_AST_SU;
504 1.2.10.3 jdolecek PROC_UNLOCK(p);
505 1.2.10.2 tls newnfs_numnfsd++;
506 1.2.10.2 tls
507 1.2.10.2 tls NFSD_UNLOCK();
508 1.2.10.2 tls
509 1.2.10.2 tls /* An empty string implies AUTH_SYS only. */
510 1.2.10.2 tls if (principal[0] != '\0') {
511 1.2.10.2 tls ret2 = rpc_gss_set_svc_name_call(principal,
512 1.2.10.2 tls "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER2);
513 1.2.10.2 tls ret3 = rpc_gss_set_svc_name_call(principal,
514 1.2.10.2 tls "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER3);
515 1.2.10.2 tls ret4 = rpc_gss_set_svc_name_call(principal,
516 1.2.10.2 tls "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER4);
517 1.2.10.2 tls
518 1.2.10.2 tls if (!ret2 || !ret3 || !ret4)
519 1.2.10.2 tls printf("nfsd: can't register svc name\n");
520 1.2.10.2 tls }
521 1.2.10.2 tls
522 1.2.10.2 tls nfsrvd_pool->sp_minthreads = args->minthreads;
523 1.2.10.2 tls nfsrvd_pool->sp_maxthreads = args->maxthreads;
524 1.2.10.2 tls
525 1.2.10.2 tls svc_run(nfsrvd_pool);
526 1.2.10.2 tls
527 1.2.10.2 tls if (principal[0] != '\0') {
528 1.2.10.2 tls rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
529 1.2.10.2 tls rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
530 1.2.10.2 tls rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4);
531 1.2.10.2 tls }
532 1.2.10.2 tls
533 1.2.10.2 tls NFSD_LOCK();
534 1.2.10.2 tls newnfs_numnfsd--;
535 1.2.10.2 tls nfsrvd_init(1);
536 1.2.10.3 jdolecek PROC_LOCK(p);
537 1.2.10.3 jdolecek p->p_flag2 &= ~P2_AST_SU;
538 1.2.10.3 jdolecek PROC_UNLOCK(p);
539 1.2.10.2 tls }
540 1.2.10.2 tls NFSD_UNLOCK();
541 1.2.10.2 tls
542 1.2.10.2 tls out:
543 1.2.10.2 tls NFSEXITCODE(error);
544 1.2.10.2 tls return (error);
545 1.2.10.2 tls }
546 1.2.10.2 tls
547 1.2.10.2 tls /*
548 1.2.10.2 tls * Initialize the data structures for the server.
549 1.2.10.2 tls * Handshake with any new nfsds starting up to avoid any chance of
550 1.2.10.2 tls * corruption.
551 1.2.10.2 tls */
552 1.2.10.2 tls void
553 1.2.10.2 tls nfsrvd_init(int terminating)
554 1.2.10.2 tls {
555 1.2.10.2 tls
556 1.2.10.2 tls NFSD_LOCK_ASSERT();
557 1.2.10.2 tls
558 1.2.10.2 tls if (terminating) {
559 1.2.10.2 tls nfsd_master_proc = NULL;
560 1.2.10.2 tls NFSD_UNLOCK();
561 1.2.10.3 jdolecek nfsrv_freeallbackchannel_xprts();
562 1.2.10.2 tls svcpool_destroy(nfsrvd_pool);
563 1.2.10.2 tls nfsrvd_pool = NULL;
564 1.2.10.2 tls NFSD_LOCK();
565 1.2.10.2 tls }
566 1.2.10.2 tls
567 1.2.10.2 tls NFSD_UNLOCK();
568 1.2.10.2 tls
569 1.2.10.2 tls nfsrvd_pool = svcpool_create("nfsd", SYSCTL_STATIC_CHILDREN(_vfs_nfsd));
570 1.2.10.2 tls nfsrvd_pool->sp_rcache = NULL;
571 1.2.10.2 tls nfsrvd_pool->sp_assign = fhanew_assign;
572 1.2.10.2 tls nfsrvd_pool->sp_done = fha_nd_complete;
573 1.2.10.2 tls
574 1.2.10.2 tls NFSD_LOCK();
575 1.2.10.2 tls }
576 1.2.10.2 tls
577