2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include "opt_inet6.h"
40 #include "opt_kgssapi.h"
41 #include "opt_kern_tls.h"
43 #include <fs/nfs/nfsport.h>
46 #include <rpc/rpcsec_gss.h>
47 #include <rpc/rpcsec_tls.h>
49 #include <fs/nfsserver/nfs_fha_new.h>
51 #include <security/mac/mac_framework.h>
55 struct nfsv4lock nfsd_suspend_lock;
56 char *nfsrv_zeropnfsdat = NULL;
59 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
61 int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
86 SYSCTL_DECL(_vfs_nfsd);
90 static int nfs_privport = 0;
91 SYSCTL_INT(_vfs_nfsd, OID_AUTO, nfs_privport, CTLFLAG_RWTUN,
93 "Only allow clients using a privileged port for NFSv2, 3 and 4");
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");
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");
103 static int nfs_proc(struct nfsrv_descript *, u_int32_t, SVCXPRT *xprt,
104 struct nfsrvcache **);
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[NFSV42_NOPS];
115 * NFS server system calls
119 nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
121 struct nfsrv_descript nd;
122 struct nfsrvcache *rp = NULL;
123 int cacherep, credflavor;
128 memset(&nd, 0, sizeof(nd));
129 if (rqst->rq_vers == NFS_VER2) {
130 if (rqst->rq_proc > NFSV2PROC_STATFS ||
131 newnfs_nfsv3_procid[rqst->rq_proc] == NFSPROC_NOOP) {
136 nd.nd_procnum = newnfs_nfsv3_procid[rqst->rq_proc];
137 nd.nd_flag = ND_NFSV2;
138 } else if (rqst->rq_vers == NFS_VER3) {
139 if (rqst->rq_proc >= NFS_V3NPROCS) {
144 nd.nd_procnum = rqst->rq_proc;
145 nd.nd_flag = ND_NFSV3;
147 if (rqst->rq_proc != NFSPROC_NULL &&
148 rqst->rq_proc != NFSV4PROC_COMPOUND) {
153 nd.nd_procnum = rqst->rq_proc;
154 nd.nd_flag = ND_NFSV4;
158 * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
159 * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
162 nd.nd_mrep = rqst->rq_args;
163 rqst->rq_args = NULL;
164 newnfs_realign(&nd.nd_mrep, M_WAITOK);
165 nd.nd_md = nd.nd_mrep;
166 nd.nd_dpos = mtod(nd.nd_md, caddr_t);
167 nd.nd_nam = svc_getrpccaller(rqst);
168 nd.nd_nam2 = rqst->rq_addr;
172 if (nfs_privport != 0) {
173 /* Check if source port is privileged */
175 struct sockaddr *nam = nd.nd_nam;
176 struct sockaddr_in *sin;
178 sin = (struct sockaddr_in *)nam;
180 * INET/INET6 - same code:
181 * sin_port and sin6_port are at same offset
183 port = ntohs(sin->sin_port);
184 if (port >= IPPORT_RESERVED &&
185 nd.nd_procnum != NFSPROC_NULL) {
187 char buf[INET6_ADDRSTRLEN];
189 char buf[INET_ADDRSTRLEN];
192 #if defined(KLD_MODULE)
193 /* Do not use ip6_sprintf: the nfs module should work without INET6. */
194 #define ip6_sprintf(buf, a) \
195 (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x", \
196 (a)->s6_addr16[0], (a)->s6_addr16[1], \
197 (a)->s6_addr16[2], (a)->s6_addr16[3], \
198 (a)->s6_addr16[4], (a)->s6_addr16[5], \
199 (a)->s6_addr16[6], (a)->s6_addr16[7]), \
203 printf("NFS request from unprivileged port (%s:%d)\n",
205 sin->sin_family == AF_INET6 ?
206 ip6_sprintf(buf, &satosin6(sin)->sin6_addr) :
207 #if defined(KLD_MODULE)
211 inet_ntoa_r(sin->sin_addr, buf), port);
212 svcerr_weakauth(rqst);
219 if (nd.nd_procnum != NFSPROC_NULL) {
220 if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {
221 svcerr_weakauth(rqst);
227 /* Set the flag based on credflavor */
228 if (credflavor == RPCSEC_GSS_KRB5) {
229 nd.nd_flag |= ND_GSS;
230 } else if (credflavor == RPCSEC_GSS_KRB5I) {
231 nd.nd_flag |= (ND_GSS | ND_GSSINTEGRITY);
232 } else if (credflavor == RPCSEC_GSS_KRB5P) {
233 nd.nd_flag |= (ND_GSS | ND_GSSPRIVACY);
234 } else if (credflavor != AUTH_SYS) {
235 svcerr_weakauth(rqst);
241 if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0) {
242 nd.nd_flag |= ND_TLS;
243 if ((xprt->xp_tls & RPCTLS_FLAGS_VERIFIED) != 0)
244 nd.nd_flag |= ND_TLSCERT;
245 if ((xprt->xp_tls & RPCTLS_FLAGS_CERTUSER) != 0)
246 nd.nd_flag |= ND_TLSCERTUSER;
248 nd.nd_maxextsiz = 16384;
250 mac_cred_associate_nfsd(nd.nd_cred);
253 * Get a refcnt (shared lock) on nfsd_suspend_lock.
254 * NFSSVC_SUSPENDNFSD will take an exclusive lock on
255 * nfsd_suspend_lock to suspend these threads.
256 * The call to nfsv4_lock() that precedes nfsv4_getref()
257 * ensures that the acquisition of the exclusive lock
258 * takes priority over acquisition of the shared lock by
259 * waiting for any exclusive lock request to complete.
260 * This must be done here, before the check of
261 * nfsv4root exports by nfsvno_v4rootexport().
263 NFSLOCKV4ROOTMUTEX();
264 nfsv4_lock(&nfsd_suspend_lock, 0, NULL, NFSV4ROOTLOCKMUTEXPTR,
266 nfsv4_getref(&nfsd_suspend_lock, NULL, NFSV4ROOTLOCKMUTEXPTR,
268 NFSUNLOCKV4ROOTMUTEX();
270 if ((nd.nd_flag & ND_NFSV4) != 0) {
271 nd.nd_repstat = nfsvno_v4rootexport(&nd);
272 if (nd.nd_repstat != 0) {
273 NFSLOCKV4ROOTMUTEX();
274 nfsv4_relref(&nfsd_suspend_lock);
275 NFSUNLOCKV4ROOTMUTEX();
276 svcerr_weakauth(rqst);
284 if ((xprt->xp_tls & RPCTLS_FLAGS_HANDSHAKE) != 0 &&
285 rpctls_getinfo(&maxlen, false, false))
286 nd.nd_maxextsiz = maxlen;
288 cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp);
289 NFSLOCKV4ROOTMUTEX();
290 nfsv4_relref(&nfsd_suspend_lock);
291 NFSUNLOCKV4ROOTMUTEX();
294 nd.nd_mreq->m_len = 0;
297 if (nd.nd_mrep != NULL)
300 if (nd.nd_cred != NULL)
303 if (cacherep == RC_DROPIT) {
304 if (nd.nd_mreq != NULL)
310 if (nd.nd_mreq == NULL) {
316 if (nd.nd_repstat & NFSERR_AUTHERR) {
317 svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
318 if (nd.nd_mreq != NULL)
320 } else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) {
321 svcerr_systemerr(rqst);
324 nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 ||
325 SVC_ACK(xprt, NULL)), rqst->rq_reply_seq);
330 td_softdep_cleanup(curthread);
335 * Check the cache and, optionally, do the RPC.
336 * Return the appropriate cache response.
339 nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt,
340 struct nfsrvcache **rpp)
342 int cacherep = RC_DOIT, isdgram, taglen = -1;
344 u_char tag[NFSV4_SMALLSTR + 1], *tagstr = NULL;
345 u_int32_t minorvers = 0;
349 if (nd->nd_nam2 == NULL) {
350 nd->nd_flag |= ND_STREAMSOCK;
358 * 1 - For NFSv2 over UDP, if we are near our malloc/mget
359 * limit, just drop the request. There is no
360 * NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the
361 * client will timeout/retry over UDP in a little while.
362 * 2 - nd_repstat == 0 && nd_mreq == NULL, which
363 * means a normal nfs rpc, so check the cache
365 if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL &&
366 nfsrv_mallocmget_limit()) {
367 cacherep = RC_DROPIT;
370 * For NFSv3, play it safe and assume that the client is
371 * doing retries on the same TCP connection.
373 if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) ==
375 nd->nd_flag |= ND_SAMETCPCONN;
377 nd->nd_tcpconntime = NFSD_MONOSEC;
378 nd->nd_sockref = xprt->xp_sockref;
379 if ((nd->nd_flag & ND_NFSV4) != 0)
380 nfsd_getminorvers(nd, tag, &tagstr, &taglen,
382 if ((nd->nd_flag & ND_NFSV41) != 0)
383 /* NFSv4.1 caches replies in the session slots. */
386 cacherep = nfsrvd_getcache(nd);
389 nfsrc_trimcache(xprt->xp_sockref, ack, 0);
394 * Handle the request. There are three cases.
395 * RC_DOIT - do the RPC
396 * RC_REPLY - return the reply already created
397 * RC_DROPIT - just throw the request away
399 if (cacherep == RC_DOIT) {
400 if ((nd->nd_flag & ND_NFSV41) != 0)
402 nfsrvd_dorpc(nd, isdgram, tagstr, taglen, minorvers);
403 if ((nd->nd_flag & ND_NFSV41) != 0) {
404 if (nd->nd_repstat != NFSERR_REPLYFROMCACHE &&
405 (nd->nd_flag & ND_SAVEREPLY) != 0) {
406 /* Cache a copy of the reply. */
407 m = m_copym(nd->nd_mreq, 0, M_COPYALL,
411 if ((nd->nd_flag & ND_HASSEQUENCE) != 0)
412 nfsrv_cache_session(nd, &m);
413 if (nd->nd_repstat == NFSERR_REPLYFROMCACHE)
417 if (nd->nd_repstat == NFSERR_DONTREPLY)
418 cacherep = RC_DROPIT;
421 *rpp = nfsrvd_updatecache(nd);
424 if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
425 free(tagstr, M_TEMP);
432 nfssvc_loss(SVCXPRT *xprt)
438 nfsrc_trimcache(xprt->xp_sockref, ack, 1);
442 * Adds a socket to the list for servicing by nfsds.
445 nfsrvd_addsock(struct file *fp)
451 static u_int64_t sockref = 0;
456 error = soreserve(so, siz, siz);
461 * Steal the socket from userland so that it doesn't close
464 if (so->so_type == SOCK_DGRAM)
465 xprt = svc_dg_create(nfsrvd_pool, so, 0, 0);
467 xprt = svc_vc_create(nfsrvd_pool, so, 0, 0);
469 fp->f_ops = &badfileops;
471 xprt->xp_sockref = ++sockref;
472 if (nfs_minvers == NFS_VER2)
473 svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program,
475 if (nfs_minvers <= NFS_VER3 && nfs_maxvers >= NFS_VER3)
476 svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program,
478 if (nfs_maxvers >= NFS_VER4)
479 svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program,
481 if (so->so_type == SOCK_STREAM)
482 svc_loss_reg(xprt, nfssvc_loss);
492 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
493 * until it is killed by a signal.
496 nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
498 char principal[MAXHOSTNAMELEN + 5];
501 bool_t ret2, ret3, ret4;
503 error = copyinstr(args->principal, principal, sizeof (principal),
509 * Only the first nfsd actually does any work. The RPC code
510 * adds threads to it as needed. Any extra processes offered
511 * by nfsd just exit. If nfsd is new enough, it will call us
512 * once with a structure that specifies how many threads to
516 if (newnfs_numnfsd == 0) {
517 nfsdev_time = time_second;
520 p->p_flag2 |= P2_AST_SU;
525 error = nfsrv_createdevids(args, td);
527 /* An empty string implies AUTH_SYS only. */
528 if (principal[0] != '\0') {
529 ret2 = rpc_gss_set_svc_name_call(principal,
530 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
532 ret3 = rpc_gss_set_svc_name_call(principal,
533 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
535 ret4 = rpc_gss_set_svc_name_call(principal,
536 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
539 if (!ret2 || !ret3 || !ret4)
541 "nfsd: can't register svc name\n");
544 nfsrvd_pool->sp_minthreads = args->minthreads;
545 nfsrvd_pool->sp_maxthreads = args->maxthreads;
548 * If this is a pNFS service, make Getattr do a
549 * vn_start_write(), so it can do a vn_set_extattr().
551 if (nfsrv_devidcnt > 0) {
552 nfsrv_writerpc[NFSPROC_GETATTR] = 1;
553 nfsv4_opflag[NFSV4OP_GETATTR].modifyfs = 1;
556 svc_run(nfsrvd_pool);
558 /* Reset Getattr to not do a vn_start_write(). */
559 nfsrv_writerpc[NFSPROC_GETATTR] = 0;
560 nfsv4_opflag[NFSV4OP_GETATTR].modifyfs = 0;
562 if (principal[0] != '\0') {
563 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
564 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
565 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4);
572 p->p_flag2 &= ~P2_AST_SU;
583 * Initialize the data structures for the server.
584 * Handshake with any new nfsds starting up to avoid any chance of
588 nfsrvd_init(int terminating)
594 nfsd_master_proc = NULL;
596 nfsrv_freealllayoutsanddevids();
597 nfsrv_freeallbackchannel_xprts();
598 svcpool_close(nfsrvd_pool);
599 free(nfsrv_zeropnfsdat, M_TEMP);
600 nfsrv_zeropnfsdat = NULL;
604 nfsrvd_pool = svcpool_create("nfsd",
605 SYSCTL_STATIC_CHILDREN(_vfs_nfsd));
606 nfsrvd_pool->sp_rcache = NULL;
607 nfsrvd_pool->sp_assign = fhanew_assign;
608 nfsrvd_pool->sp_done = fhanew_nd_complete;