1 /* $NetBSD: nfs_srvsocket.c,v 1.7 2026/06/18 19:59:41 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1991, 1993, 1995 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Rick Macklem at The University of Guelph. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 21 * 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 32 * SUCH DAMAGE. 33 * 34 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95 35 */ 36 37 /* 38 * Socket operations for use by nfs 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: nfs_srvsocket.c,v 1.7 2026/06/18 19:59:41 christos Exp $"); 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/evcnt.h> 47 #include <sys/callout.h> 48 #include <sys/proc.h> 49 #include <sys/mount.h> 50 #include <sys/kernel.h> 51 #include <sys/kmem.h> 52 #include <sys/mbuf.h> 53 #include <sys/vnode.h> 54 #include <sys/domain.h> 55 #include <sys/protosw.h> 56 #include <sys/socket.h> 57 #include <sys/socketvar.h> 58 #include <sys/syslog.h> 59 #include <sys/tprintf.h> 60 #include <sys/namei.h> 61 #include <sys/signal.h> 62 #include <sys/signalvar.h> 63 #include <sys/kauth.h> 64 65 #include <netinet/in.h> 66 #include <netinet/tcp.h> 67 68 #include <nfs/rpcv2.h> 69 #include <nfs/nfsproto.h> 70 #include <nfs/nfs.h> 71 #include <nfs/xdr_subs.h> 72 #include <nfs/nfsm_subs.h> 73 #include <nfs/nfsmount.h> 74 #include <nfs/nfsnode.h> 75 #include <nfs/nfsrtt.h> 76 #include <nfs/nfs_var.h> 77 78 static void nfsrv_wakenfsd_locked(struct nfssvc_sock *); 79 80 int (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *, 81 struct nfssvc_sock *, struct lwp *, 82 struct mbuf **) = { 83 nfsrv_null, 84 nfsrv_getattr, 85 nfsrv_setattr, 86 nfsrv_lookup, 87 nfsrv3_access, 88 nfsrv_readlink, 89 nfsrv_read, 90 nfsrv_write, 91 nfsrv_create, 92 nfsrv_mkdir, 93 nfsrv_symlink, 94 nfsrv_mknod, 95 nfsrv_remove, 96 nfsrv_rmdir, 97 nfsrv_rename, 98 nfsrv_link, 99 nfsrv_readdir, 100 nfsrv_readdirplus, 101 nfsrv_statfs, 102 nfsrv_fsinfo, 103 nfsrv_pathconf, 104 nfsrv_commit, 105 nfsrv_noop 106 }; 107 108 /* 109 * Socket upcall routine for the nfsd sockets. 110 * The void *arg is a pointer to the "struct nfssvc_sock". 111 */ 112 void 113 nfsrv_soupcall(struct socket *so, void *arg, int events, int waitflag) 114 { 115 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg; 116 117 nfsdsock_setbits(slp, SLP_A_NEEDQ); 118 nfsrv_wakenfsd(slp); 119 } 120 121 void 122 nfsrv_rcv(struct nfssvc_sock *slp) 123 { 124 struct socket *so; 125 struct mbuf *m; 126 struct mbuf *mp, *nam; 127 struct uio auio; 128 int flags; 129 int error; 130 int setflags = 0; 131 132 error = nfsdsock_lock(slp, true); 133 if (error) { 134 setflags |= SLP_A_NEEDQ; 135 goto dorecs_unlocked; 136 } 137 138 nfsdsock_clearbits(slp, SLP_A_NEEDQ); 139 140 so = slp->ns_so; 141 if (so->so_type == SOCK_STREAM) { 142 /* 143 * Do soreceive(). 144 */ 145 auio.uio_resid = 1000000000; 146 /* not need to setup uio_vmspace */ 147 flags = MSG_DONTWAIT; 148 error = (*so->so_receive)(so, &nam, &auio, &mp, NULL, &flags); 149 if (error || mp == NULL) { 150 if (error == EWOULDBLOCK) 151 setflags |= SLP_A_NEEDQ; 152 else 153 setflags |= SLP_A_DISCONN; 154 goto dorecs; 155 } 156 m = mp; 157 m_claimm(m, &nfs_mowner); 158 if (slp->ns_rawend) { 159 slp->ns_rawend->m_next = m; 160 slp->ns_cc += 1000000000 - auio.uio_resid; 161 } else { 162 slp->ns_raw = m; 163 slp->ns_cc = 1000000000 - auio.uio_resid; 164 } 165 while (m->m_next) 166 m = m->m_next; 167 slp->ns_rawend = m; 168 169 /* 170 * Now try and parse record(s) out of the raw stream data. 171 */ 172 error = nfsrv_getstream(slp, M_WAIT); 173 if (error) { 174 if (error == EPERM) 175 setflags |= SLP_A_DISCONN; 176 else 177 setflags |= SLP_A_NEEDQ; 178 } 179 } else { 180 do { 181 auio.uio_resid = 1000000000; 182 /* not need to setup uio_vmspace */ 183 flags = MSG_DONTWAIT; 184 error = (*so->so_receive)(so, &nam, &auio, &mp, NULL, 185 &flags); 186 if (mp) { 187 if (nam) { 188 m = nam; 189 m->m_next = mp; 190 } else 191 m = mp; 192 m_claimm(m, &nfs_mowner); 193 if (slp->ns_recend) 194 slp->ns_recend->m_nextpkt = m; 195 else 196 slp->ns_rec = m; 197 slp->ns_recend = m; 198 m->m_nextpkt = (struct mbuf *)0; 199 } 200 if (error) { 201 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) 202 && error != EWOULDBLOCK) { 203 setflags |= SLP_A_DISCONN; 204 goto dorecs; 205 } 206 } 207 } while (mp); 208 } 209 dorecs: 210 nfsdsock_unlock(slp); 211 212 dorecs_unlocked: 213 if (setflags) { 214 nfsdsock_setbits(slp, setflags); 215 } 216 } 217 218 int 219 nfsdsock_lock(struct nfssvc_sock *slp, bool waitok) 220 { 221 222 mutex_enter(&slp->ns_lock); 223 while ((~slp->ns_flags & (SLP_BUSY|SLP_VALID)) == 0) { 224 if (!waitok) { 225 mutex_exit(&slp->ns_lock); 226 return EWOULDBLOCK; 227 } 228 cv_wait(&slp->ns_cv, &slp->ns_lock); 229 } 230 if ((slp->ns_flags & SLP_VALID) == 0) { 231 mutex_exit(&slp->ns_lock); 232 return EINVAL; 233 } 234 KASSERT((slp->ns_flags & SLP_BUSY) == 0); 235 slp->ns_flags |= SLP_BUSY; 236 mutex_exit(&slp->ns_lock); 237 238 return 0; 239 } 240 241 void 242 nfsdsock_unlock(struct nfssvc_sock *slp) 243 { 244 245 mutex_enter(&slp->ns_lock); 246 KASSERT((slp->ns_flags & SLP_BUSY) != 0); 247 cv_broadcast(&slp->ns_cv); 248 slp->ns_flags &= ~SLP_BUSY; 249 mutex_exit(&slp->ns_lock); 250 } 251 252 int 253 nfsdsock_drain(struct nfssvc_sock *slp) 254 { 255 int error = 0; 256 257 mutex_enter(&slp->ns_lock); 258 if ((slp->ns_flags & SLP_VALID) == 0) { 259 error = EINVAL; 260 goto done; 261 } 262 slp->ns_flags &= ~SLP_VALID; 263 while ((slp->ns_flags & SLP_BUSY) != 0) { 264 cv_wait(&slp->ns_cv, &slp->ns_lock); 265 } 266 done: 267 mutex_exit(&slp->ns_lock); 268 269 return error; 270 } 271 272 #define NFS_MAX_FRAG 64 /* Maximum number of pending fragments */ 273 #define NFS_MAX_STREAM (1024 * 1014) /* Max streaming packet length */ 274 275 /* 276 * Try and extract an RPC request from the mbuf data list received on a 277 * stream socket. The "waitflag" argument indicates whether or not it 278 * can sleep. 279 */ 280 int 281 nfsrv_getstream(struct nfssvc_sock *slp, int waitflag) 282 { 283 struct mbuf *m, **mpp; 284 struct mbuf *recm; 285 u_int32_t recmark; 286 int error = 0; 287 288 KASSERT((slp->ns_flags & SLP_BUSY) != 0); 289 for (;;) { 290 if (slp->ns_reclen == 0) { 291 if (slp->ns_cc < NFSX_UNSIGNED) { 292 break; 293 } 294 m = slp->ns_raw; 295 m_copydata(m, 0, NFSX_UNSIGNED, (void *)&recmark); 296 m_adj(m, NFSX_UNSIGNED); 297 slp->ns_cc -= NFSX_UNSIGNED; 298 recmark = ntohl(recmark); 299 slp->ns_reclen = recmark & ~0x80000000; 300 if (recmark & 0x80000000) 301 slp->ns_sflags |= SLP_S_LASTFRAG; 302 else { 303 slp->ns_sflags &= ~SLP_S_LASTFRAG; 304 slp->ns_frag_count++; 305 if (slp->ns_frag_count > NFS_MAX_FRAG) { 306 printf("%s: frag count exceeded\n", 307 __func__); 308 error = EPERM; 309 break; 310 } 311 } 312 if (slp->ns_reclen > NFS_MAXPACKET) { 313 error = EPERM; 314 break; 315 } 316 slp->ns_streamlen += slp->ns_reclen; 317 if (slp->ns_streamlen > NFS_MAX_STREAM) { 318 printf("%s: stream length exceeded\n", 319 __func__); 320 error = EPERM; 321 break; 322 } 323 } 324 325 /* 326 * Now get the record part. 327 * 328 * Note that slp->ns_reclen may be 0. Linux sometimes 329 * generates 0-length records. 330 */ 331 if (slp->ns_cc == slp->ns_reclen) { 332 recm = slp->ns_raw; 333 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0; 334 slp->ns_cc = slp->ns_reclen = 0; 335 } else if (slp->ns_cc > slp->ns_reclen) { 336 recm = slp->ns_raw; 337 m = m_split(recm, slp->ns_reclen, waitflag); 338 if (m == NULL) { 339 error = EWOULDBLOCK; 340 break; 341 } 342 m_claimm(recm, &nfs_mowner); 343 slp->ns_raw = m; 344 while (m->m_next) 345 m = m->m_next; 346 slp->ns_rawend = m; 347 slp->ns_cc -= slp->ns_reclen; 348 slp->ns_reclen = 0; 349 } else { 350 break; 351 } 352 353 /* 354 * Accumulate the fragments into a record. 355 */ 356 mpp = &slp->ns_frag; 357 while (*mpp) 358 mpp = &((*mpp)->m_next); 359 *mpp = recm; 360 if (slp->ns_sflags & SLP_S_LASTFRAG) { 361 if (slp->ns_recend) 362 slp->ns_recend->m_nextpkt = slp->ns_frag; 363 else 364 slp->ns_rec = slp->ns_frag; 365 slp->ns_recend = slp->ns_frag; 366 slp->ns_frag = NULL; 367 slp->ns_frag_count = 0; 368 slp->ns_streamlen = 0; 369 } 370 } 371 372 return error; 373 } 374 375 /* 376 * Parse an RPC header. 377 */ 378 int 379 nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd, 380 struct nfsrv_descript **ndp, bool *more) 381 { 382 struct mbuf *m, *nam; 383 struct nfsrv_descript *nd; 384 int error; 385 386 *ndp = NULL; 387 *more = false; 388 389 if (nfsdsock_lock(slp, true)) { 390 return ENOBUFS; 391 } 392 m = slp->ns_rec; 393 if (m == NULL) { 394 nfsdsock_unlock(slp); 395 return ENOBUFS; 396 } 397 slp->ns_rec = m->m_nextpkt; 398 if (slp->ns_rec) { 399 m->m_nextpkt = NULL; 400 *more = true; 401 } else { 402 slp->ns_recend = NULL; 403 } 404 nfsdsock_unlock(slp); 405 406 if (m->m_type == MT_SONAME) { 407 nam = m; 408 m = m->m_next; 409 nam->m_next = NULL; 410 } else 411 nam = NULL; 412 nd = nfsdreq_alloc(); 413 nd->nd_md = nd->nd_mrep = m; 414 nd->nd_nam2 = nam; 415 nd->nd_dpos = mtod(m, void *); 416 error = nfs_getreq(nd, nfsd, true); 417 if (error) { 418 m_freem(nam); 419 nfsdreq_free(nd); 420 return (error); 421 } 422 *ndp = nd; 423 nfsd->nfsd_nd = nd; 424 return (0); 425 } 426 427 bool 428 nfsrv_timer(void) 429 { 430 struct timeval tv; 431 struct nfssvc_sock *slp; 432 u_quad_t cur_usec; 433 struct nfsrv_descript *nd; 434 bool more; 435 436 /* 437 * Scan the write gathering queues for writes that need to be 438 * completed now. 439 */ 440 getmicrotime(&tv); 441 cur_usec = (u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec; 442 more = false; 443 mutex_enter(&nfsd_lock); 444 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) { 445 nd = LIST_FIRST(&slp->ns_tq); 446 if (nd != NULL) { 447 if (nd->nd_time <= cur_usec) { 448 nfsrv_wakenfsd_locked(slp); 449 } 450 more = true; 451 } 452 } 453 mutex_exit(&nfsd_lock); 454 return more; 455 } 456 457 /* 458 * Search for a sleeping nfsd and wake it up. 459 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the 460 * running nfsds will go look for the work in the nfssvc_sock list. 461 */ 462 static void 463 nfsrv_wakenfsd_locked(struct nfssvc_sock *slp) 464 { 465 struct nfsd *nd; 466 467 KASSERT(mutex_owned(&nfsd_lock)); 468 469 if ((slp->ns_flags & SLP_VALID) == 0) 470 return; 471 if (slp->ns_gflags & SLP_G_DOREC) 472 return; 473 nd = SLIST_FIRST(&nfsd_idle_head); 474 if (nd) { 475 SLIST_REMOVE_HEAD(&nfsd_idle_head, nfsd_idle); 476 if (nd->nfsd_slp) 477 panic("nfsd wakeup"); 478 slp->ns_sref++; 479 KASSERT(slp->ns_sref > 0); 480 nd->nfsd_slp = slp; 481 cv_signal(&nd->nfsd_cv); 482 } else { 483 slp->ns_gflags |= SLP_G_DOREC; 484 nfsd_head_flag |= NFSD_CHECKSLP; 485 TAILQ_INSERT_TAIL(&nfssvc_sockpending, slp, ns_pending); 486 } 487 } 488 489 void 490 nfsrv_wakenfsd(struct nfssvc_sock *slp) 491 { 492 493 mutex_enter(&nfsd_lock); 494 nfsrv_wakenfsd_locked(slp); 495 mutex_exit(&nfsd_lock); 496 } 497 498 int 499 nfsdsock_sendreply(struct nfssvc_sock *slp, struct nfsrv_descript *nd) 500 { 501 int error; 502 503 m_freem(nd->nd_mrep); 504 nd->nd_mrep = NULL; 505 506 mutex_enter(&slp->ns_lock); 507 if ((slp->ns_flags & SLP_SENDING) != 0) { 508 SIMPLEQ_INSERT_TAIL(&slp->ns_sendq, nd, nd_sendq); 509 mutex_exit(&slp->ns_lock); 510 return 0; 511 } 512 KASSERT(SIMPLEQ_EMPTY(&slp->ns_sendq)); 513 slp->ns_flags |= SLP_SENDING; 514 mutex_exit(&slp->ns_lock); 515 516 again: 517 error = nfs_send(slp->ns_so, nd->nd_nam2, nd->nd_mreq, NULL, curlwp); 518 if (nd->nd_nam2) { 519 m_free(nd->nd_nam2); 520 } 521 nfsdreq_free(nd); 522 523 mutex_enter(&slp->ns_lock); 524 KASSERT((slp->ns_flags & SLP_SENDING) != 0); 525 nd = SIMPLEQ_FIRST(&slp->ns_sendq); 526 if (nd != NULL) { 527 SIMPLEQ_REMOVE_HEAD(&slp->ns_sendq, nd_sendq); 528 mutex_exit(&slp->ns_lock); 529 goto again; 530 } 531 slp->ns_flags &= ~SLP_SENDING; 532 mutex_exit(&slp->ns_lock); 533 534 return error; 535 } 536 537 void 538 nfsdsock_setbits(struct nfssvc_sock *slp, int bits) 539 { 540 541 mutex_enter(&slp->ns_alock); 542 slp->ns_aflags |= bits; 543 mutex_exit(&slp->ns_alock); 544 } 545 546 void 547 nfsdsock_clearbits(struct nfssvc_sock *slp, int bits) 548 { 549 550 mutex_enter(&slp->ns_alock); 551 slp->ns_aflags &= ~bits; 552 mutex_exit(&slp->ns_alock); 553 } 554 555 bool 556 nfsdsock_testbits(struct nfssvc_sock *slp, int bits) 557 { 558 559 return (slp->ns_aflags & bits); 560 } 561