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