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"
42 #include <fs/nfs/nfsport.h>
45 #include <rpc/rpcsec_gss.h>
47 #include <nfs/nfs_fha.h>
48 #include <fs/nfsserver/nfs_fha_new.h>
50 #include <security/mac/mac_framework.h>
54 struct nfsv4lock nfsd_suspend_lock;
55 char *nfsrv_zeropnfsdat = NULL;
58 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
60 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[NFSV41_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;
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) {
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) {
141 nd.nd_procnum = rqst->rq_proc;
142 nd.nd_flag = ND_NFSV3;
144 if (rqst->rq_proc != NFSPROC_NULL &&
145 rqst->rq_proc != NFSV4PROC_COMPOUND) {
150 nd.nd_procnum = rqst->rq_proc;
151 nd.nd_flag = ND_NFSV4;
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
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;
169 if (nfs_privport != 0) {
170 /* Check if source port is privileged */
172 struct sockaddr *nam = nd.nd_nam;
173 struct sockaddr_in *sin;
175 sin = (struct sockaddr_in *)nam;
177 * INET/INET6 - same code:
178 * sin_port and sin6_port are at same offset
180 port = ntohs(sin->sin_port);
181 if (port >= IPPORT_RESERVED &&
182 nd.nd_procnum != NFSPROC_NULL) {
184 char buf[INET6_ADDRSTRLEN];
186 char buf[INET_ADDRSTRLEN];
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]), \
200 printf("NFS request from unprivileged port (%s:%d)\n",
202 sin->sin_family == AF_INET6 ?
203 ip6_sprintf(buf, &satosin6(sin)->sin6_addr) :
204 #if defined(KLD_MODULE)
208 inet_ntoa_r(sin->sin_addr, buf), port);
209 svcerr_weakauth(rqst);
216 if (nd.nd_procnum != NFSPROC_NULL) {
217 if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {
218 svcerr_weakauth(rqst);
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);
239 mac_cred_associate_nfsd(nd.nd_cred);
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().
252 NFSLOCKV4ROOTMUTEX();
253 nfsv4_lock(&nfsd_suspend_lock, 0, NULL, NFSV4ROOTLOCKMUTEXPTR,
255 nfsv4_getref(&nfsd_suspend_lock, NULL, NFSV4ROOTLOCKMUTEXPTR,
257 NFSUNLOCKV4ROOTMUTEX();
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);
272 cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp);
273 NFSLOCKV4ROOTMUTEX();
274 nfsv4_relref(&nfsd_suspend_lock);
275 NFSUNLOCKV4ROOTMUTEX();
278 nd.nd_mreq->m_len = 0;
281 if (nd.nd_mrep != NULL)
284 if (nd.nd_cred != NULL)
287 if (cacherep == RC_DROPIT) {
288 if (nd.nd_mreq != NULL)
294 if (nd.nd_mreq == NULL) {
300 if (nd.nd_repstat & NFSERR_AUTHERR) {
301 svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
302 if (nd.nd_mreq != NULL)
304 } else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) {
305 svcerr_systemerr(rqst);
308 nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 ||
309 SVC_ACK(xprt, NULL)), rqst->rq_reply_seq);
314 td_softdep_cleanup(curthread);
319 * Check the cache and, optionally, do the RPC.
320 * Return the appropriate cache response.
323 nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt,
324 struct nfsrvcache **rpp)
326 int cacherep = RC_DOIT, isdgram, taglen = -1;
328 u_char tag[NFSV4_SMALLSTR + 1], *tagstr = NULL;
329 u_int32_t minorvers = 0;
333 if (nd->nd_nam2 == NULL) {
334 nd->nd_flag |= ND_STREAMSOCK;
342 * 1 - For NFSv2 over UDP, if we are near our malloc/mget
343 * limit, just drop the request. There is no
344 * NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the
345 * client will timeout/retry over UDP in a little while.
346 * 2 - nd_repstat == 0 && nd_mreq == NULL, which
347 * means a normal nfs rpc, so check the cache
349 if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL &&
350 nfsrv_mallocmget_limit()) {
351 cacherep = RC_DROPIT;
354 * For NFSv3, play it safe and assume that the client is
355 * doing retries on the same TCP connection.
357 if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) ==
359 nd->nd_flag |= ND_SAMETCPCONN;
361 nd->nd_tcpconntime = NFSD_MONOSEC;
362 nd->nd_sockref = xprt->xp_sockref;
363 if ((nd->nd_flag & ND_NFSV4) != 0)
364 nfsd_getminorvers(nd, tag, &tagstr, &taglen,
366 if ((nd->nd_flag & ND_NFSV41) != 0)
367 /* NFSv4.1 caches replies in the session slots. */
370 cacherep = nfsrvd_getcache(nd);
373 nfsrc_trimcache(xprt->xp_sockref, ack, 0);
378 * Handle the request. There are three cases.
379 * RC_DOIT - do the RPC
380 * RC_REPLY - return the reply already created
381 * RC_DROPIT - just throw the request away
383 if (cacherep == RC_DOIT) {
384 if ((nd->nd_flag & ND_NFSV41) != 0)
386 nfsrvd_dorpc(nd, isdgram, tagstr, taglen, minorvers);
387 if ((nd->nd_flag & ND_NFSV41) != 0) {
388 if (nd->nd_repstat != NFSERR_REPLYFROMCACHE &&
389 (nd->nd_flag & ND_SAVEREPLY) != 0) {
390 /* Cache a copy of the reply. */
391 m = m_copym(nd->nd_mreq, 0, M_COPYALL,
395 if ((nd->nd_flag & ND_HASSEQUENCE) != 0)
396 nfsrv_cache_session(nd->nd_sessionid,
397 nd->nd_slotid, nd->nd_repstat, &m);
398 if (nd->nd_repstat == NFSERR_REPLYFROMCACHE)
402 if (nd->nd_repstat == NFSERR_DONTREPLY)
403 cacherep = RC_DROPIT;
406 *rpp = nfsrvd_updatecache(nd);
409 if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
410 free(tagstr, M_TEMP);
417 nfssvc_loss(SVCXPRT *xprt)
423 nfsrc_trimcache(xprt->xp_sockref, ack, 1);
427 * Adds a socket to the list for servicing by nfsds.
430 nfsrvd_addsock(struct file *fp)
436 static u_int64_t sockref = 0;
441 error = soreserve(so, siz, siz);
446 * Steal the socket from userland so that it doesn't close
449 if (so->so_type == SOCK_DGRAM)
450 xprt = svc_dg_create(nfsrvd_pool, so, 0, 0);
452 xprt = svc_vc_create(nfsrvd_pool, so, 0, 0);
454 fp->f_ops = &badfileops;
456 xprt->xp_sockref = ++sockref;
457 if (nfs_minvers == NFS_VER2)
458 svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program,
460 if (nfs_minvers <= NFS_VER3 && nfs_maxvers >= NFS_VER3)
461 svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program,
463 if (nfs_maxvers >= NFS_VER4)
464 svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program,
466 if (so->so_type == SOCK_STREAM)
467 svc_loss_reg(xprt, nfssvc_loss);
477 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
478 * until it is killed by a signal.
481 nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
483 char principal[MAXHOSTNAMELEN + 5];
486 bool_t ret2, ret3, ret4;
488 error = copyinstr(args->principal, principal, sizeof (principal),
494 * Only the first nfsd actually does any work. The RPC code
495 * adds threads to it as needed. Any extra processes offered
496 * by nfsd just exit. If nfsd is new enough, it will call us
497 * once with a structure that specifies how many threads to
501 if (newnfs_numnfsd == 0) {
502 nfsdev_time = time_second;
505 p->p_flag2 |= P2_AST_SU;
510 error = nfsrv_createdevids(args, td);
512 /* An empty string implies AUTH_SYS only. */
513 if (principal[0] != '\0') {
514 ret2 = rpc_gss_set_svc_name_call(principal,
515 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
517 ret3 = rpc_gss_set_svc_name_call(principal,
518 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
520 ret4 = rpc_gss_set_svc_name_call(principal,
521 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG,
524 if (!ret2 || !ret3 || !ret4)
526 "nfsd: can't register svc name\n");
529 nfsrvd_pool->sp_minthreads = args->minthreads;
530 nfsrvd_pool->sp_maxthreads = args->maxthreads;
533 * If this is a pNFS service, make Getattr do a
534 * vn_start_write(), so it can do a vn_set_extattr().
536 if (nfsrv_devidcnt > 0) {
537 nfsrv_writerpc[NFSPROC_GETATTR] = 1;
538 nfsv4_opflag[NFSV4OP_GETATTR].modifyfs = 1;
541 svc_run(nfsrvd_pool);
543 /* Reset Getattr to not do a vn_start_write(). */
544 nfsrv_writerpc[NFSPROC_GETATTR] = 0;
545 nfsv4_opflag[NFSV4OP_GETATTR].modifyfs = 0;
547 if (principal[0] != '\0') {
548 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
549 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
550 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4);
557 p->p_flag2 &= ~P2_AST_SU;
568 * Initialize the data structures for the server.
569 * Handshake with any new nfsds starting up to avoid any chance of
573 nfsrvd_init(int terminating)
579 nfsd_master_proc = NULL;
581 nfsrv_freealllayoutsanddevids();
582 nfsrv_freeallbackchannel_xprts();
583 svcpool_close(nfsrvd_pool);
584 free(nfsrv_zeropnfsdat, M_TEMP);
585 nfsrv_zeropnfsdat = NULL;
589 nfsrvd_pool = svcpool_create("nfsd",
590 SYSCTL_STATIC_CHILDREN(_vfs_nfsd));
591 nfsrvd_pool->sp_rcache = NULL;
592 nfsrvd_pool->sp_assign = fhanew_assign;
593 nfsrvd_pool->sp_done = fha_nd_complete;