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