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