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