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