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;
57 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
59 int newnfs_nfsv3_procid[NFS_V3NPROCS] = {
85 SYSCTL_DECL(_vfs_nfsd);
89 static int nfs_privport = 0;
90 SYSCTL_INT(_vfs_nfsd, OID_AUTO, nfs_privport, CTLFLAG_RWTUN,
92 "Only allow clients using a privileged port for NFSv2 and 3");
94 static int nfs_minvers = NFS_VER2;
95 SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_min_nfsvers, CTLFLAG_RWTUN,
96 &nfs_minvers, 0, "The lowest version of NFS handled by the server");
98 static int nfs_maxvers = NFS_VER4;
99 SYSCTL_INT(_vfs_nfsd, OID_AUTO, server_max_nfsvers, CTLFLAG_RWTUN,
100 &nfs_maxvers, 0, "The highest version of NFS handled by the server");
102 static int nfs_proc(struct nfsrv_descript *, u_int32_t, SVCXPRT *xprt,
103 struct nfsrvcache **);
105 extern u_long sb_max_adj;
106 extern int newnfs_numnfsd;
107 extern struct proc *nfsd_master_proc;
110 * NFS server system calls
114 nfssvc_program(struct svc_req *rqst, SVCXPRT *xprt)
116 struct nfsrv_descript nd;
117 struct nfsrvcache *rp = NULL;
118 int cacherep, credflavor;
120 memset(&nd, 0, sizeof(nd));
121 if (rqst->rq_vers == NFS_VER2) {
122 if (rqst->rq_proc > NFSV2PROC_STATFS ||
123 newnfs_nfsv3_procid[rqst->rq_proc] == NFSPROC_NOOP) {
128 nd.nd_procnum = newnfs_nfsv3_procid[rqst->rq_proc];
129 nd.nd_flag = ND_NFSV2;
130 } else if (rqst->rq_vers == NFS_VER3) {
131 if (rqst->rq_proc >= NFS_V3NPROCS) {
136 nd.nd_procnum = rqst->rq_proc;
137 nd.nd_flag = ND_NFSV3;
139 if (rqst->rq_proc != NFSPROC_NULL &&
140 rqst->rq_proc != NFSV4PROC_COMPOUND) {
145 nd.nd_procnum = rqst->rq_proc;
146 nd.nd_flag = ND_NFSV4;
150 * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
151 * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
154 nd.nd_mrep = rqst->rq_args;
155 rqst->rq_args = NULL;
156 newnfs_realign(&nd.nd_mrep, M_WAITOK);
157 nd.nd_md = nd.nd_mrep;
158 nd.nd_dpos = mtod(nd.nd_md, caddr_t);
159 nd.nd_nam = svc_getrpccaller(rqst);
160 nd.nd_nam2 = rqst->rq_addr;
164 if (nfs_privport && (nd.nd_flag & ND_NFSV4) == 0) {
165 /* Check if source port is privileged */
167 struct sockaddr *nam = nd.nd_nam;
168 struct sockaddr_in *sin;
170 sin = (struct sockaddr_in *)nam;
172 * INET/INET6 - same code:
173 * sin_port and sin6_port are at same offset
175 port = ntohs(sin->sin_port);
176 if (port >= IPPORT_RESERVED &&
177 nd.nd_procnum != NFSPROC_NULL) {
179 char buf[INET6_ADDRSTRLEN];
181 char buf[INET_ADDRSTRLEN];
184 #if defined(KLD_MODULE)
185 /* Do not use ip6_sprintf: the nfs module should work without INET6. */
186 #define ip6_sprintf(buf, a) \
187 (sprintf((buf), "%x:%x:%x:%x:%x:%x:%x:%x", \
188 (a)->s6_addr16[0], (a)->s6_addr16[1], \
189 (a)->s6_addr16[2], (a)->s6_addr16[3], \
190 (a)->s6_addr16[4], (a)->s6_addr16[5], \
191 (a)->s6_addr16[6], (a)->s6_addr16[7]), \
195 printf("NFS request from unprivileged port (%s:%d)\n",
197 sin->sin_family == AF_INET6 ?
198 ip6_sprintf(buf, &satosin6(sin)->sin6_addr) :
199 #if defined(KLD_MODULE)
203 inet_ntoa_r(sin->sin_addr, buf), port);
204 svcerr_weakauth(rqst);
211 if (nd.nd_procnum != NFSPROC_NULL) {
212 if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {
213 svcerr_weakauth(rqst);
219 /* Set the flag based on credflavor */
220 if (credflavor == RPCSEC_GSS_KRB5) {
221 nd.nd_flag |= ND_GSS;
222 } else if (credflavor == RPCSEC_GSS_KRB5I) {
223 nd.nd_flag |= (ND_GSS | ND_GSSINTEGRITY);
224 } else if (credflavor == RPCSEC_GSS_KRB5P) {
225 nd.nd_flag |= (ND_GSS | ND_GSSPRIVACY);
226 } else if (credflavor != AUTH_SYS) {
227 svcerr_weakauth(rqst);
234 mac_cred_associate_nfsd(nd.nd_cred);
237 * Get a refcnt (shared lock) on nfsd_suspend_lock.
238 * NFSSVC_SUSPENDNFSD will take an exclusive lock on
239 * nfsd_suspend_lock to suspend these threads.
240 * The call to nfsv4_lock() that precedes nfsv4_getref()
241 * ensures that the acquisition of the exclusive lock
242 * takes priority over acquisition of the shared lock by
243 * waiting for any exclusive lock request to complete.
244 * This must be done here, before the check of
245 * nfsv4root exports by nfsvno_v4rootexport().
247 NFSLOCKV4ROOTMUTEX();
248 nfsv4_lock(&nfsd_suspend_lock, 0, NULL, NFSV4ROOTLOCKMUTEXPTR,
250 nfsv4_getref(&nfsd_suspend_lock, NULL, NFSV4ROOTLOCKMUTEXPTR,
252 NFSUNLOCKV4ROOTMUTEX();
254 if ((nd.nd_flag & ND_NFSV4) != 0) {
255 nd.nd_repstat = nfsvno_v4rootexport(&nd);
256 if (nd.nd_repstat != 0) {
257 NFSLOCKV4ROOTMUTEX();
258 nfsv4_relref(&nfsd_suspend_lock);
259 NFSUNLOCKV4ROOTMUTEX();
260 svcerr_weakauth(rqst);
267 cacherep = nfs_proc(&nd, rqst->rq_xid, xprt, &rp);
268 NFSLOCKV4ROOTMUTEX();
269 nfsv4_relref(&nfsd_suspend_lock);
270 NFSUNLOCKV4ROOTMUTEX();
273 nd.nd_mreq->m_len = 0;
276 if (nd.nd_mrep != NULL)
279 if (nd.nd_cred != NULL)
282 if (cacherep == RC_DROPIT) {
283 if (nd.nd_mreq != NULL)
289 if (nd.nd_mreq == NULL) {
295 if (nd.nd_repstat & NFSERR_AUTHERR) {
296 svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
297 if (nd.nd_mreq != NULL)
299 } else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq)) {
300 svcerr_systemerr(rqst);
303 nfsrvd_sentcache(rp, (rqst->rq_reply_seq != 0 ||
304 SVC_ACK(xprt, NULL)), rqst->rq_reply_seq);
309 td_softdep_cleanup(curthread);
314 * Check the cache and, optionally, do the RPC.
315 * Return the appropriate cache response.
318 nfs_proc(struct nfsrv_descript *nd, u_int32_t xid, SVCXPRT *xprt,
319 struct nfsrvcache **rpp)
321 struct thread *td = curthread;
322 int cacherep = RC_DOIT, isdgram, taglen = -1;
324 u_char tag[NFSV4_SMALLSTR + 1], *tagstr = NULL;
325 u_int32_t minorvers = 0;
329 if (nd->nd_nam2 == NULL) {
330 nd->nd_flag |= ND_STREAMSOCK;
338 * 1 - For NFSv2 over UDP, if we are near our malloc/mget
339 * limit, just drop the request. There is no
340 * NFSERR_RESOURCE or NFSERR_DELAY for NFSv2 and the
341 * client will timeout/retry over UDP in a little while.
342 * 2 - nd_repstat == 0 && nd_mreq == NULL, which
343 * means a normal nfs rpc, so check the cache
345 if ((nd->nd_flag & ND_NFSV2) && nd->nd_nam2 != NULL &&
346 nfsrv_mallocmget_limit()) {
347 cacherep = RC_DROPIT;
350 * For NFSv3, play it safe and assume that the client is
351 * doing retries on the same TCP connection.
353 if ((nd->nd_flag & (ND_NFSV4 | ND_STREAMSOCK)) ==
355 nd->nd_flag |= ND_SAMETCPCONN;
357 nd->nd_tcpconntime = NFSD_MONOSEC;
358 nd->nd_sockref = xprt->xp_sockref;
359 if ((nd->nd_flag & ND_NFSV4) != 0)
360 nfsd_getminorvers(nd, tag, &tagstr, &taglen,
362 if ((nd->nd_flag & ND_NFSV41) != 0)
363 /* NFSv4.1 caches replies in the session slots. */
366 cacherep = nfsrvd_getcache(nd);
369 nfsrc_trimcache(xprt->xp_sockref, ack, 0);
374 * Handle the request. There are three cases.
375 * RC_DOIT - do the RPC
376 * RC_REPLY - return the reply already created
377 * RC_DROPIT - just throw the request away
379 if (cacherep == RC_DOIT) {
380 if ((nd->nd_flag & ND_NFSV41) != 0)
382 nfsrvd_dorpc(nd, isdgram, tagstr, taglen, minorvers, td);
383 if ((nd->nd_flag & ND_NFSV41) != 0) {
384 if (nd->nd_repstat != NFSERR_REPLYFROMCACHE &&
385 (nd->nd_flag & ND_SAVEREPLY) != 0) {
386 /* Cache a copy of the reply. */
387 m = m_copym(nd->nd_mreq, 0, M_COPYALL,
391 if ((nd->nd_flag & ND_HASSEQUENCE) != 0)
392 nfsrv_cache_session(nd->nd_sessionid,
393 nd->nd_slotid, nd->nd_repstat, &m);
394 if (nd->nd_repstat == NFSERR_REPLYFROMCACHE)
398 if (nd->nd_repstat == NFSERR_DONTREPLY)
399 cacherep = RC_DROPIT;
402 *rpp = nfsrvd_updatecache(nd);
405 if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
406 free(tagstr, M_TEMP);
413 nfssvc_loss(SVCXPRT *xprt)
419 nfsrc_trimcache(xprt->xp_sockref, ack, 1);
423 * Adds a socket to the list for servicing by nfsds.
426 nfsrvd_addsock(struct file *fp)
432 static u_int64_t sockref = 0;
437 error = soreserve(so, siz, siz);
442 * Steal the socket from userland so that it doesn't close
445 if (so->so_type == SOCK_DGRAM)
446 xprt = svc_dg_create(nfsrvd_pool, so, 0, 0);
448 xprt = svc_vc_create(nfsrvd_pool, so, 0, 0);
450 fp->f_ops = &badfileops;
452 xprt->xp_sockref = ++sockref;
453 if (nfs_minvers == NFS_VER2)
454 svc_reg(xprt, NFS_PROG, NFS_VER2, nfssvc_program,
456 if (nfs_minvers <= NFS_VER3 && nfs_maxvers >= NFS_VER3)
457 svc_reg(xprt, NFS_PROG, NFS_VER3, nfssvc_program,
459 if (nfs_maxvers >= NFS_VER4)
460 svc_reg(xprt, NFS_PROG, NFS_VER4, nfssvc_program,
462 if (so->so_type == SOCK_STREAM)
463 svc_loss_reg(xprt, nfssvc_loss);
473 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
474 * until it is killed by a signal.
477 nfsrvd_nfsd(struct thread *td, struct nfsd_nfsd_args *args)
479 char principal[MAXHOSTNAMELEN + 5];
482 bool_t ret2, ret3, ret4;
484 error = copyinstr(args->principal, principal, sizeof (principal),
490 * Only the first nfsd actually does any work. The RPC code
491 * adds threads to it as needed. Any extra processes offered
492 * by nfsd just exit. If nfsd is new enough, it will call us
493 * once with a structure that specifies how many threads to
497 if (newnfs_numnfsd == 0) {
500 p->p_flag2 |= P2_AST_SU;
506 /* An empty string implies AUTH_SYS only. */
507 if (principal[0] != '\0') {
508 ret2 = rpc_gss_set_svc_name_call(principal,
509 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER2);
510 ret3 = rpc_gss_set_svc_name_call(principal,
511 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER3);
512 ret4 = rpc_gss_set_svc_name_call(principal,
513 "kerberosv5", GSS_C_INDEFINITE, NFS_PROG, NFS_VER4);
515 if (!ret2 || !ret3 || !ret4)
516 printf("nfsd: can't register svc name\n");
519 nfsrvd_pool->sp_minthreads = args->minthreads;
520 nfsrvd_pool->sp_maxthreads = args->maxthreads;
522 svc_run(nfsrvd_pool);
524 if (principal[0] != '\0') {
525 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER2);
526 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER3);
527 rpc_gss_clear_svc_name_call(NFS_PROG, NFS_VER4);
534 p->p_flag2 &= ~P2_AST_SU;
545 * Initialize the data structures for the server.
546 * Handshake with any new nfsds starting up to avoid any chance of
550 nfsrvd_init(int terminating)
556 nfsd_master_proc = NULL;
558 nfsrv_freeallbackchannel_xprts();
559 svcpool_close(nfsrvd_pool);
563 nfsrvd_pool = svcpool_create("nfsd",
564 SYSCTL_STATIC_CHILDREN(_vfs_nfsd));
565 nfsrvd_pool->sp_rcache = NULL;
566 nfsrvd_pool->sp_assign = fhanew_assign;
567 nfsrvd_pool->sp_done = fha_nd_complete;