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