1 /* $NetBSD: nfs_socket.c,v 1.100 2003/12/07 21:15:46 fvdl 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_socket.c,v 1.100 2003/12/07 21:15:46 fvdl Exp $"); 43 44 #include "fs_nfs.h" 45 #include "opt_nfs.h" 46 #include "opt_nfsserver.h" 47 #include "opt_mbuftrace.h" 48 #include "opt_inet.h" 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/callout.h> 53 #include <sys/proc.h> 54 #include <sys/mount.h> 55 #include <sys/kernel.h> 56 #include <sys/mbuf.h> 57 #include <sys/vnode.h> 58 #include <sys/domain.h> 59 #include <sys/protosw.h> 60 #include <sys/socket.h> 61 #include <sys/socketvar.h> 62 #include <sys/syslog.h> 63 #include <sys/tprintf.h> 64 #include <sys/namei.h> 65 #include <sys/signal.h> 66 #include <sys/signalvar.h> 67 68 #include <netinet/in.h> 69 #include <netinet/tcp.h> 70 71 #include <nfs/rpcv2.h> 72 #include <nfs/nfsproto.h> 73 #include <nfs/nfs.h> 74 #include <nfs/xdr_subs.h> 75 #include <nfs/nfsm_subs.h> 76 #include <nfs/nfsmount.h> 77 #include <nfs/nfsnode.h> 78 #include <nfs/nfsrtt.h> 79 #include <nfs/nqnfs.h> 80 #include <nfs/nfs_var.h> 81 82 MALLOC_DEFINE(M_NFSREQ, "NFS req", "NFS request header"); 83 #ifdef MBUFTRACE 84 struct mowner nfs_mowner = { "nfs" }; 85 #endif 86 87 /* 88 * Estimate rto for an nfs rpc sent via. an unreliable datagram. 89 * Use the mean and mean deviation of rtt for the appropriate type of rpc 90 * for the frequent rpcs and a default for the others. 91 * The justification for doing "other" this way is that these rpcs 92 * happen so infrequently that timer est. would probably be stale. 93 * Also, since many of these rpcs are 94 * non-idempotent, a conservative timeout is desired. 95 * getattr, lookup - A+2D 96 * read, write - A+4D 97 * other - nm_timeo 98 */ 99 #define NFS_RTO(n, t) \ 100 ((t) == 0 ? (n)->nm_timeo : \ 101 ((t) < 3 ? \ 102 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \ 103 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1))) 104 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1] 105 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1] 106 /* 107 * External data, mostly RPC constants in XDR form 108 */ 109 extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers, 110 rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr, 111 rpc_auth_kerb; 112 extern u_int32_t nfs_prog, nqnfs_prog; 113 extern time_t nqnfsstarttime; 114 extern const int nfsv3_procid[NFS_NPROCS]; 115 extern int nfs_ticks; 116 117 /* 118 * Defines which timer to use for the procnum. 119 * 0 - default 120 * 1 - getattr 121 * 2 - lookup 122 * 3 - read 123 * 4 - write 124 */ 125 static const int proct[NFS_NPROCS] = { 126 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 127 0, 0, 0, 128 }; 129 130 /* 131 * There is a congestion window for outstanding rpcs maintained per mount 132 * point. The cwnd size is adjusted in roughly the way that: 133 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of 134 * SIGCOMM '88". ACM, August 1988. 135 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout 136 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd 137 * of rpcs is in progress. 138 * (The sent count and cwnd are scaled for integer arith.) 139 * Variants of "slow start" were tried and were found to be too much of a 140 * performance hit (ave. rtt 3 times larger), 141 * I suspect due to the large rtt that nfs rpcs have. 142 */ 143 #define NFS_CWNDSCALE 256 144 #define NFS_MAXCWND (NFS_CWNDSCALE * 32) 145 static const int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, }; 146 int nfsrtton = 0; 147 struct nfsrtt nfsrtt; 148 struct nfsreqhead nfs_reqq; 149 150 struct callout nfs_timer_ch = CALLOUT_INITIALIZER_SETFUNC(nfs_timer, NULL); 151 152 /* 153 * Initialize sockets and congestion for a new NFS connection. 154 * We do not free the sockaddr if error. 155 */ 156 int 157 nfs_connect(nmp, rep) 158 struct nfsmount *nmp; 159 struct nfsreq *rep; 160 { 161 struct socket *so; 162 int s, error, rcvreserve, sndreserve; 163 struct sockaddr *saddr; 164 struct sockaddr_in *sin; 165 #ifdef INET6 166 struct sockaddr_in6 *sin6; 167 #endif 168 struct mbuf *m; 169 170 nmp->nm_so = (struct socket *)0; 171 saddr = mtod(nmp->nm_nam, struct sockaddr *); 172 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype, 173 nmp->nm_soproto); 174 if (error) 175 goto bad; 176 so = nmp->nm_so; 177 #ifdef MBUFTRACE 178 so->so_mowner = &nfs_mowner; 179 so->so_rcv.sb_mowner = &nfs_mowner; 180 so->so_snd.sb_mowner = &nfs_mowner; 181 #endif 182 nmp->nm_soflags = so->so_proto->pr_flags; 183 184 /* 185 * Some servers require that the client port be a reserved port number. 186 */ 187 if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) { 188 m = m_get(M_WAIT, MT_SOOPTS); 189 MCLAIM(m, so->so_mowner); 190 *mtod(m, int32_t *) = IP_PORTRANGE_LOW; 191 m->m_len = sizeof(int32_t); 192 if ((error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, m))) 193 goto bad; 194 m = m_get(M_WAIT, MT_SONAME); 195 MCLAIM(m, so->so_mowner); 196 sin = mtod(m, struct sockaddr_in *); 197 sin->sin_len = m->m_len = sizeof (struct sockaddr_in); 198 sin->sin_family = AF_INET; 199 sin->sin_addr.s_addr = INADDR_ANY; 200 sin->sin_port = 0; 201 error = sobind(so, m, &proc0); 202 m_freem(m); 203 if (error) 204 goto bad; 205 } 206 #ifdef INET6 207 if (saddr->sa_family == AF_INET6 && (nmp->nm_flag & NFSMNT_RESVPORT)) { 208 m = m_get(M_WAIT, MT_SOOPTS); 209 MCLAIM(m, so->so_mowner); 210 *mtod(m, int32_t *) = IPV6_PORTRANGE_LOW; 211 m->m_len = sizeof(int32_t); 212 if ((error = sosetopt(so, IPPROTO_IPV6, IPV6_PORTRANGE, m))) 213 goto bad; 214 m = m_get(M_WAIT, MT_SONAME); 215 MCLAIM(m, so->so_mowner); 216 sin6 = mtod(m, struct sockaddr_in6 *); 217 sin6->sin6_len = m->m_len = sizeof (struct sockaddr_in6); 218 sin6->sin6_family = AF_INET6; 219 sin6->sin6_addr = in6addr_any; 220 sin6->sin6_port = 0; 221 error = sobind(so, m, &proc0); 222 m_freem(m); 223 if (error) 224 goto bad; 225 } 226 #endif 227 228 /* 229 * Protocols that do not require connections may be optionally left 230 * unconnected for servers that reply from a port other than NFS_PORT. 231 */ 232 if (nmp->nm_flag & NFSMNT_NOCONN) { 233 if (nmp->nm_soflags & PR_CONNREQUIRED) { 234 error = ENOTCONN; 235 goto bad; 236 } 237 } else { 238 error = soconnect(so, nmp->nm_nam); 239 if (error) 240 goto bad; 241 242 /* 243 * Wait for the connection to complete. Cribbed from the 244 * connect system call but with the wait timing out so 245 * that interruptible mounts don't hang here for a long time. 246 */ 247 s = splsoftnet(); 248 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) { 249 (void) tsleep((caddr_t)&so->so_timeo, PSOCK, 250 "nfscn1", 2 * hz); 251 if ((so->so_state & SS_ISCONNECTING) && 252 so->so_error == 0 && rep && 253 (error = nfs_sigintr(nmp, rep, rep->r_procp)) != 0){ 254 so->so_state &= ~SS_ISCONNECTING; 255 splx(s); 256 goto bad; 257 } 258 } 259 if (so->so_error) { 260 error = so->so_error; 261 so->so_error = 0; 262 splx(s); 263 goto bad; 264 } 265 splx(s); 266 } 267 if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT)) { 268 so->so_rcv.sb_timeo = (5 * hz); 269 so->so_snd.sb_timeo = (5 * hz); 270 } else { 271 so->so_rcv.sb_timeo = 0; 272 so->so_snd.sb_timeo = 0; 273 } 274 if (nmp->nm_sotype == SOCK_DGRAM) { 275 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2; 276 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) + 277 NFS_MAXPKTHDR) * 2; 278 } else if (nmp->nm_sotype == SOCK_SEQPACKET) { 279 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2; 280 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) + 281 NFS_MAXPKTHDR) * 2; 282 } else { 283 if (nmp->nm_sotype != SOCK_STREAM) 284 panic("nfscon sotype"); 285 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 286 m = m_get(M_WAIT, MT_SOOPTS); 287 MCLAIM(m, so->so_mowner); 288 *mtod(m, int32_t *) = 1; 289 m->m_len = sizeof(int32_t); 290 sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m); 291 } 292 if (so->so_proto->pr_protocol == IPPROTO_TCP) { 293 m = m_get(M_WAIT, MT_SOOPTS); 294 MCLAIM(m, so->so_mowner); 295 *mtod(m, int32_t *) = 1; 296 m->m_len = sizeof(int32_t); 297 sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m); 298 } 299 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR + 300 sizeof (u_int32_t)) * 2; 301 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR + 302 sizeof (u_int32_t)) * 2; 303 } 304 error = soreserve(so, sndreserve, rcvreserve); 305 if (error) 306 goto bad; 307 so->so_rcv.sb_flags |= SB_NOINTR; 308 so->so_snd.sb_flags |= SB_NOINTR; 309 310 /* Initialize other non-zero congestion variables */ 311 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] = nmp->nm_srtt[3] = 312 NFS_TIMEO << 3; 313 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] = 314 nmp->nm_sdrtt[3] = 0; 315 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */ 316 nmp->nm_sent = 0; 317 nmp->nm_timeouts = 0; 318 return (0); 319 320 bad: 321 nfs_disconnect(nmp); 322 return (error); 323 } 324 325 /* 326 * Reconnect routine: 327 * Called when a connection is broken on a reliable protocol. 328 * - clean up the old socket 329 * - nfs_connect() again 330 * - set R_MUSTRESEND for all outstanding requests on mount point 331 * If this fails the mount point is DEAD! 332 * nb: Must be called with the nfs_sndlock() set on the mount point. 333 */ 334 int 335 nfs_reconnect(rep) 336 struct nfsreq *rep; 337 { 338 struct nfsreq *rp; 339 struct nfsmount *nmp = rep->r_nmp; 340 int error; 341 342 nfs_disconnect(nmp); 343 while ((error = nfs_connect(nmp, rep)) != 0) { 344 if (error == EINTR || error == ERESTART) 345 return (EINTR); 346 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfscn2", 0); 347 } 348 349 /* 350 * Loop through outstanding request list and fix up all requests 351 * on old socket. 352 */ 353 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) { 354 if (rp->r_nmp == nmp) 355 rp->r_flags |= R_MUSTRESEND; 356 } 357 return (0); 358 } 359 360 /* 361 * NFS disconnect. Clean up and unlink. 362 */ 363 void 364 nfs_disconnect(nmp) 365 struct nfsmount *nmp; 366 { 367 struct socket *so; 368 int drain = 0; 369 370 if (nmp->nm_so) { 371 so = nmp->nm_so; 372 nmp->nm_so = (struct socket *)0; 373 soshutdown(so, 2); 374 drain = (nmp->nm_iflag & NFSMNT_DISMNT) != 0; 375 if (drain) { 376 /* 377 * soshutdown() above should wake up the current 378 * listener. 379 * Now wake up those waiting for the receive lock, and 380 * wait for them to go away unhappy, to prevent *nmp 381 * from evaporating while they're sleeping. 382 */ 383 while (nmp->nm_waiters > 0) { 384 wakeup (&nmp->nm_iflag); 385 (void) tsleep(&nmp->nm_waiters, PVFS, 386 "nfsdis", 0); 387 } 388 } 389 soclose(so); 390 } 391 #ifdef DIAGNOSTIC 392 if (drain && (nmp->nm_waiters > 0)) 393 panic("nfs_disconnect: waiters left after drain?"); 394 #endif 395 } 396 397 void 398 nfs_safedisconnect(nmp) 399 struct nfsmount *nmp; 400 { 401 struct nfsreq dummyreq; 402 403 memset(&dummyreq, 0, sizeof(dummyreq)); 404 dummyreq.r_nmp = nmp; 405 nfs_rcvlock(&dummyreq); /* XXX ignored error return */ 406 nfs_disconnect(nmp); 407 nfs_rcvunlock(nmp); 408 } 409 410 /* 411 * This is the nfs send routine. For connection based socket types, it 412 * must be called with an nfs_sndlock() on the socket. 413 * "rep == NULL" indicates that it has been called from a server. 414 * For the client side: 415 * - return EINTR if the RPC is terminated, 0 otherwise 416 * - set R_MUSTRESEND if the send fails for any reason 417 * - do any cleanup required by recoverable socket errors (? ? ?) 418 * For the server side: 419 * - return EINTR or ERESTART if interrupted by a signal 420 * - return EPIPE if a connection is lost for connection based sockets (TCP...) 421 * - do any cleanup required by recoverable socket errors (? ? ?) 422 */ 423 int 424 nfs_send(so, nam, top, rep) 425 struct socket *so; 426 struct mbuf *nam; 427 struct mbuf *top; 428 struct nfsreq *rep; 429 { 430 struct mbuf *sendnam; 431 int error, soflags, flags; 432 433 if (rep) { 434 if (rep->r_flags & R_SOFTTERM) { 435 m_freem(top); 436 return (EINTR); 437 } 438 if ((so = rep->r_nmp->nm_so) == NULL) { 439 rep->r_flags |= R_MUSTRESEND; 440 m_freem(top); 441 return (0); 442 } 443 rep->r_flags &= ~R_MUSTRESEND; 444 soflags = rep->r_nmp->nm_soflags; 445 } else 446 soflags = so->so_proto->pr_flags; 447 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED)) 448 sendnam = (struct mbuf *)0; 449 else 450 sendnam = nam; 451 if (so->so_type == SOCK_SEQPACKET) 452 flags = MSG_EOR; 453 else 454 flags = 0; 455 456 error = (*so->so_send)(so, sendnam, (struct uio *)0, top, 457 (struct mbuf *)0, flags); 458 if (error) { 459 if (rep) { 460 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) { 461 /* 462 * We're too fast for the network/driver, 463 * and UDP isn't flowcontrolled. 464 * We need to resend. This is not fatal, 465 * just try again. 466 * 467 * Could be smarter here by doing some sort 468 * of a backoff, but this is rare. 469 */ 470 rep->r_flags |= R_MUSTRESEND; 471 } else { 472 log(LOG_INFO, "nfs send error %d for %s\n", 473 error, 474 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 475 /* 476 * Deal with errors for the client side. 477 */ 478 if (rep->r_flags & R_SOFTTERM) 479 error = EINTR; 480 else 481 rep->r_flags |= R_MUSTRESEND; 482 } 483 } else { 484 /* 485 * See above. This error can happen under normal 486 * circumstances and the log is too noisy. 487 * The error will still show up in nfsstat. 488 */ 489 if (error != ENOBUFS || so->so_type != SOCK_DGRAM) 490 log(LOG_INFO, "nfsd send error %d\n", error); 491 } 492 493 /* 494 * Handle any recoverable (soft) socket errors here. (? ? ?) 495 */ 496 if (error != EINTR && error != ERESTART && 497 error != EWOULDBLOCK && error != EPIPE) 498 error = 0; 499 } 500 return (error); 501 } 502 503 #ifdef NFS 504 /* 505 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all 506 * done by soreceive(), but for SOCK_STREAM we must deal with the Record 507 * Mark and consolidate the data into a new mbuf list. 508 * nb: Sometimes TCP passes the data up to soreceive() in long lists of 509 * small mbufs. 510 * For SOCK_STREAM we must be very careful to read an entire record once 511 * we have read any of it, even if the system call has been interrupted. 512 */ 513 int 514 nfs_receive(rep, aname, mp) 515 struct nfsreq *rep; 516 struct mbuf **aname; 517 struct mbuf **mp; 518 { 519 struct socket *so; 520 struct uio auio; 521 struct iovec aio; 522 struct mbuf *m; 523 struct mbuf *control; 524 u_int32_t len; 525 struct mbuf **getnam; 526 int error, sotype, rcvflg; 527 struct proc *p = curproc; /* XXX */ 528 529 /* 530 * Set up arguments for soreceive() 531 */ 532 *mp = (struct mbuf *)0; 533 *aname = (struct mbuf *)0; 534 sotype = rep->r_nmp->nm_sotype; 535 536 /* 537 * For reliable protocols, lock against other senders/receivers 538 * in case a reconnect is necessary. 539 * For SOCK_STREAM, first get the Record Mark to find out how much 540 * more there is to get. 541 * We must lock the socket against other receivers 542 * until we have an entire rpc request/reply. 543 */ 544 if (sotype != SOCK_DGRAM) { 545 error = nfs_sndlock(&rep->r_nmp->nm_iflag, rep); 546 if (error) 547 return (error); 548 tryagain: 549 /* 550 * Check for fatal errors and resending request. 551 */ 552 /* 553 * Ugh: If a reconnect attempt just happened, nm_so 554 * would have changed. NULL indicates a failed 555 * attempt that has essentially shut down this 556 * mount point. 557 */ 558 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) { 559 nfs_sndunlock(&rep->r_nmp->nm_iflag); 560 return (EINTR); 561 } 562 so = rep->r_nmp->nm_so; 563 if (!so) { 564 error = nfs_reconnect(rep); 565 if (error) { 566 nfs_sndunlock(&rep->r_nmp->nm_iflag); 567 return (error); 568 } 569 goto tryagain; 570 } 571 while (rep->r_flags & R_MUSTRESEND) { 572 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT); 573 nfsstats.rpcretries++; 574 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep); 575 if (error) { 576 if (error == EINTR || error == ERESTART || 577 (error = nfs_reconnect(rep)) != 0) { 578 nfs_sndunlock(&rep->r_nmp->nm_iflag); 579 return (error); 580 } 581 goto tryagain; 582 } 583 } 584 nfs_sndunlock(&rep->r_nmp->nm_iflag); 585 if (sotype == SOCK_STREAM) { 586 aio.iov_base = (caddr_t) &len; 587 aio.iov_len = sizeof(u_int32_t); 588 auio.uio_iov = &aio; 589 auio.uio_iovcnt = 1; 590 auio.uio_segflg = UIO_SYSSPACE; 591 auio.uio_rw = UIO_READ; 592 auio.uio_offset = 0; 593 auio.uio_resid = sizeof(u_int32_t); 594 auio.uio_procp = p; 595 do { 596 rcvflg = MSG_WAITALL; 597 error = (*so->so_receive)(so, (struct mbuf **)0, &auio, 598 (struct mbuf **)0, (struct mbuf **)0, &rcvflg); 599 if (error == EWOULDBLOCK && rep) { 600 if (rep->r_flags & R_SOFTTERM) 601 return (EINTR); 602 } 603 } while (error == EWOULDBLOCK); 604 if (!error && auio.uio_resid > 0) { 605 /* 606 * Don't log a 0 byte receive; it means 607 * that the socket has been closed, and 608 * can happen during normal operation 609 * (forcible unmount or Solaris server). 610 */ 611 if (auio.uio_resid != sizeof (u_int32_t)) 612 log(LOG_INFO, 613 "short receive (%lu/%lu) from nfs server %s\n", 614 (u_long)sizeof(u_int32_t) - auio.uio_resid, 615 (u_long)sizeof(u_int32_t), 616 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 617 error = EPIPE; 618 } 619 if (error) 620 goto errout; 621 len = ntohl(len) & ~0x80000000; 622 /* 623 * This is SERIOUS! We are out of sync with the sender 624 * and forcing a disconnect/reconnect is all I can do. 625 */ 626 if (len > NFS_MAXPACKET) { 627 log(LOG_ERR, "%s (%d) from nfs server %s\n", 628 "impossible packet length", 629 len, 630 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 631 error = EFBIG; 632 goto errout; 633 } 634 auio.uio_resid = len; 635 do { 636 rcvflg = MSG_WAITALL; 637 error = (*so->so_receive)(so, (struct mbuf **)0, 638 &auio, mp, (struct mbuf **)0, &rcvflg); 639 } while (error == EWOULDBLOCK || error == EINTR || 640 error == ERESTART); 641 if (!error && auio.uio_resid > 0) { 642 if (len != auio.uio_resid) 643 log(LOG_INFO, 644 "short receive (%lu/%d) from nfs server %s\n", 645 (u_long)len - auio.uio_resid, len, 646 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 647 error = EPIPE; 648 } 649 } else { 650 /* 651 * NB: Since uio_resid is big, MSG_WAITALL is ignored 652 * and soreceive() will return when it has either a 653 * control msg or a data msg. 654 * We have no use for control msg., but must grab them 655 * and then throw them away so we know what is going 656 * on. 657 */ 658 auio.uio_resid = len = 100000000; /* Anything Big */ 659 auio.uio_procp = p; 660 do { 661 rcvflg = 0; 662 error = (*so->so_receive)(so, (struct mbuf **)0, 663 &auio, mp, &control, &rcvflg); 664 if (control) 665 m_freem(control); 666 if (error == EWOULDBLOCK && rep) { 667 if (rep->r_flags & R_SOFTTERM) 668 return (EINTR); 669 } 670 } while (error == EWOULDBLOCK || 671 (!error && *mp == NULL && control)); 672 if ((rcvflg & MSG_EOR) == 0) 673 printf("Egad!!\n"); 674 if (!error && *mp == NULL) 675 error = EPIPE; 676 len -= auio.uio_resid; 677 } 678 errout: 679 if (error && error != EINTR && error != ERESTART) { 680 m_freem(*mp); 681 *mp = (struct mbuf *)0; 682 if (error != EPIPE) 683 log(LOG_INFO, 684 "receive error %d from nfs server %s\n", 685 error, 686 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 687 error = nfs_sndlock(&rep->r_nmp->nm_iflag, rep); 688 if (!error) 689 error = nfs_reconnect(rep); 690 if (!error) 691 goto tryagain; 692 else 693 nfs_sndunlock(&rep->r_nmp->nm_iflag); 694 } 695 } else { 696 if ((so = rep->r_nmp->nm_so) == NULL) 697 return (EACCES); 698 if (so->so_state & SS_ISCONNECTED) 699 getnam = (struct mbuf **)0; 700 else 701 getnam = aname; 702 auio.uio_resid = len = 1000000; 703 auio.uio_procp = p; 704 do { 705 rcvflg = 0; 706 error = (*so->so_receive)(so, getnam, &auio, mp, 707 (struct mbuf **)0, &rcvflg); 708 if (error == EWOULDBLOCK && 709 (rep->r_flags & R_SOFTTERM)) 710 return (EINTR); 711 } while (error == EWOULDBLOCK); 712 len -= auio.uio_resid; 713 if (!error && *mp == NULL) 714 error = EPIPE; 715 } 716 if (error) { 717 m_freem(*mp); 718 *mp = (struct mbuf *)0; 719 } 720 return (error); 721 } 722 723 /* 724 * Implement receipt of reply on a socket. 725 * We must search through the list of received datagrams matching them 726 * with outstanding requests using the xid, until ours is found. 727 */ 728 /* ARGSUSED */ 729 int 730 nfs_reply(myrep) 731 struct nfsreq *myrep; 732 { 733 struct nfsreq *rep; 734 struct nfsmount *nmp = myrep->r_nmp; 735 int32_t t1; 736 struct mbuf *mrep, *nam, *md; 737 u_int32_t rxid, *tl; 738 caddr_t dpos, cp2; 739 int error; 740 741 /* 742 * Loop around until we get our own reply 743 */ 744 for (;;) { 745 /* 746 * Lock against other receivers so that I don't get stuck in 747 * sbwait() after someone else has received my reply for me. 748 * Also necessary for connection based protocols to avoid 749 * race conditions during a reconnect. 750 */ 751 error = nfs_rcvlock(myrep); 752 if (error == EALREADY) 753 return (0); 754 if (error) 755 return (error); 756 /* 757 * Get the next Rpc reply off the socket 758 */ 759 nmp->nm_waiters++; 760 error = nfs_receive(myrep, &nam, &mrep); 761 nfs_rcvunlock(nmp); 762 if (error) { 763 764 if (nmp->nm_iflag & NFSMNT_DISMNT) { 765 /* 766 * Oops, we're going away now.. 767 */ 768 nmp->nm_waiters--; 769 wakeup (&nmp->nm_waiters); 770 return error; 771 } 772 nmp->nm_waiters--; 773 /* 774 * Ignore routing errors on connectionless protocols? ? 775 */ 776 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) { 777 nmp->nm_so->so_error = 0; 778 #ifdef DEBUG 779 printf("nfs_reply: ignoring error %d\n", error); 780 #endif 781 if (myrep->r_flags & R_GETONEREP) 782 return (0); 783 continue; 784 } 785 return (error); 786 } 787 nmp->nm_waiters--; 788 if (nam) 789 m_freem(nam); 790 791 /* 792 * Get the xid and check that it is an rpc reply 793 */ 794 md = mrep; 795 dpos = mtod(md, caddr_t); 796 nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED); 797 rxid = *tl++; 798 if (*tl != rpc_reply) { 799 #ifndef NFS_V2_ONLY 800 if (nmp->nm_flag & NFSMNT_NQNFS) { 801 if (nqnfs_callback(nmp, mrep, md, dpos)) 802 nfsstats.rpcinvalid++; 803 } else 804 #endif 805 { 806 nfsstats.rpcinvalid++; 807 m_freem(mrep); 808 } 809 nfsmout: 810 if (myrep->r_flags & R_GETONEREP) 811 return (0); 812 continue; 813 } 814 815 /* 816 * Loop through the request list to match up the reply 817 * Iff no match, just drop the datagram 818 */ 819 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) { 820 if (rep->r_mrep == NULL && rxid == rep->r_xid) { 821 /* Found it.. */ 822 rep->r_mrep = mrep; 823 rep->r_md = md; 824 rep->r_dpos = dpos; 825 if (nfsrtton) { 826 struct rttl *rt; 827 828 rt = &nfsrtt.rttl[nfsrtt.pos]; 829 rt->proc = rep->r_procnum; 830 rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]); 831 rt->sent = nmp->nm_sent; 832 rt->cwnd = nmp->nm_cwnd; 833 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1]; 834 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1]; 835 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid; 836 rt->tstamp = time; 837 if (rep->r_flags & R_TIMING) 838 rt->rtt = rep->r_rtt; 839 else 840 rt->rtt = 1000000; 841 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ; 842 } 843 /* 844 * Update congestion window. 845 * Do the additive increase of 846 * one rpc/rtt. 847 */ 848 if (nmp->nm_cwnd <= nmp->nm_sent) { 849 nmp->nm_cwnd += 850 (NFS_CWNDSCALE * NFS_CWNDSCALE + 851 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd; 852 if (nmp->nm_cwnd > NFS_MAXCWND) 853 nmp->nm_cwnd = NFS_MAXCWND; 854 } 855 rep->r_flags &= ~R_SENT; 856 nmp->nm_sent -= NFS_CWNDSCALE; 857 /* 858 * Update rtt using a gain of 0.125 on the mean 859 * and a gain of 0.25 on the deviation. 860 */ 861 if (rep->r_flags & R_TIMING) { 862 /* 863 * Since the timer resolution of 864 * NFS_HZ is so course, it can often 865 * result in r_rtt == 0. Since 866 * r_rtt == N means that the actual 867 * rtt is between N+dt and N+2-dt ticks, 868 * add 1. 869 */ 870 t1 = rep->r_rtt + 1; 871 t1 -= (NFS_SRTT(rep) >> 3); 872 NFS_SRTT(rep) += t1; 873 if (t1 < 0) 874 t1 = -t1; 875 t1 -= (NFS_SDRTT(rep) >> 2); 876 NFS_SDRTT(rep) += t1; 877 } 878 nmp->nm_timeouts = 0; 879 break; 880 } 881 } 882 /* 883 * If not matched to a request, drop it. 884 * If it's mine, get out. 885 */ 886 if (rep == 0) { 887 nfsstats.rpcunexpected++; 888 m_freem(mrep); 889 } else if (rep == myrep) { 890 if (rep->r_mrep == NULL) 891 panic("nfsreply nil"); 892 return (0); 893 } 894 if (myrep->r_flags & R_GETONEREP) 895 return (0); 896 } 897 } 898 899 /* 900 * nfs_request - goes something like this 901 * - fill in request struct 902 * - links it into list 903 * - calls nfs_send() for first transmit 904 * - calls nfs_receive() to get reply 905 * - break down rpc header and return with nfs reply pointed to 906 * by mrep or error 907 * nb: always frees up mreq mbuf list 908 */ 909 int 910 nfs_request(np, mrest, procnum, procp, cred, mrp, mdp, dposp) 911 struct nfsnode *np; 912 struct mbuf *mrest; 913 int procnum; 914 struct proc *procp; 915 struct ucred *cred; 916 struct mbuf **mrp; 917 struct mbuf **mdp; 918 caddr_t *dposp; 919 { 920 struct mbuf *m, *mrep; 921 struct nfsreq *rep; 922 u_int32_t *tl; 923 int i; 924 struct nfsmount *nmp; 925 struct mbuf *md, *mheadend; 926 char nickv[RPCX_NICKVERF]; 927 time_t reqtime, waituntil; 928 caddr_t dpos, cp2; 929 int t1, s, error = 0, mrest_len, auth_len, auth_type; 930 int trylater_delay = NFS_TRYLATERDEL, failed_auth = 0; 931 int verf_len, verf_type; 932 u_int32_t xid; 933 char *auth_str, *verf_str; 934 NFSKERBKEY_T key; /* save session key */ 935 struct ucred acred; 936 #ifndef NFS_V2_ONLY 937 int nqlflag, cachable; 938 u_quad_t frev; 939 #endif 940 941 KASSERT(cred != NULL); 942 nmp = VFSTONFS(np->n_vnode->v_mount); 943 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK); 944 rep->r_nmp = nmp; 945 rep->r_procp = procp; 946 rep->r_procnum = procnum; 947 i = 0; 948 m = mrest; 949 while (m) { 950 i += m->m_len; 951 m = m->m_next; 952 } 953 mrest_len = i; 954 955 /* 956 * Get the RPC header with authorization. 957 */ 958 kerbauth: 959 verf_str = auth_str = (char *)0; 960 if (nmp->nm_flag & NFSMNT_KERB) { 961 verf_str = nickv; 962 verf_len = sizeof (nickv); 963 auth_type = RPCAUTH_KERB4; 964 memset((caddr_t)key, 0, sizeof (key)); 965 if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str, 966 &auth_len, verf_str, verf_len)) { 967 error = nfs_getauth(nmp, rep, cred, &auth_str, 968 &auth_len, verf_str, &verf_len, key); 969 if (error) { 970 free((caddr_t)rep, M_NFSREQ); 971 m_freem(mrest); 972 return (error); 973 } 974 } 975 } else { 976 switch (procnum) { 977 case NFSPROC_READ: 978 case NFSPROC_WRITE: 979 case NFSPROC_COMMIT: 980 acred.cr_uid = np->n_vattr->va_uid; 981 acred.cr_gid = np->n_vattr->va_gid; 982 acred.cr_ngroups = 0; 983 acred.cr_ref = 2; /* Just to be safe.. */ 984 cred = &acred; 985 break; 986 } 987 auth_type = RPCAUTH_UNIX; 988 auth_len = (((cred->cr_ngroups > nmp->nm_numgrps) ? 989 nmp->nm_numgrps : cred->cr_ngroups) << 2) + 990 5 * NFSX_UNSIGNED; 991 } 992 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len, 993 auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid); 994 if (auth_str) 995 free(auth_str, M_TEMP); 996 997 /* 998 * For stream protocols, insert a Sun RPC Record Mark. 999 */ 1000 if (nmp->nm_sotype == SOCK_STREAM) { 1001 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT); 1002 *mtod(m, u_int32_t *) = htonl(0x80000000 | 1003 (m->m_pkthdr.len - NFSX_UNSIGNED)); 1004 } 1005 rep->r_mreq = m; 1006 rep->r_xid = xid; 1007 tryagain: 1008 if (nmp->nm_flag & NFSMNT_SOFT) 1009 rep->r_retry = nmp->nm_retry; 1010 else 1011 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */ 1012 rep->r_rtt = rep->r_rexmit = 0; 1013 if (proct[procnum] > 0) 1014 rep->r_flags = R_TIMING; 1015 else 1016 rep->r_flags = 0; 1017 rep->r_mrep = NULL; 1018 1019 /* 1020 * Do the client side RPC. 1021 */ 1022 nfsstats.rpcrequests++; 1023 /* 1024 * Chain request into list of outstanding requests. Be sure 1025 * to put it LAST so timer finds oldest requests first. 1026 */ 1027 s = splsoftnet(); 1028 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain); 1029 1030 /* Get send time for nqnfs */ 1031 reqtime = time.tv_sec; 1032 1033 /* 1034 * If backing off another request or avoiding congestion, don't 1035 * send this one now but let timer do it. If not timing a request, 1036 * do it now. 1037 */ 1038 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM || 1039 (nmp->nm_flag & NFSMNT_DUMBTIMR) || 1040 nmp->nm_sent < nmp->nm_cwnd)) { 1041 splx(s); 1042 if (nmp->nm_soflags & PR_CONNREQUIRED) 1043 error = nfs_sndlock(&nmp->nm_iflag, rep); 1044 if (!error) { 1045 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT); 1046 error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep); 1047 if (nmp->nm_soflags & PR_CONNREQUIRED) 1048 nfs_sndunlock(&nmp->nm_iflag); 1049 } 1050 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) { 1051 nmp->nm_sent += NFS_CWNDSCALE; 1052 rep->r_flags |= R_SENT; 1053 } 1054 } else { 1055 splx(s); 1056 rep->r_rtt = -1; 1057 } 1058 1059 /* 1060 * Wait for the reply from our send or the timer's. 1061 */ 1062 if (!error || error == EPIPE) 1063 error = nfs_reply(rep); 1064 1065 /* 1066 * RPC done, unlink the request. 1067 */ 1068 s = splsoftnet(); 1069 TAILQ_REMOVE(&nfs_reqq, rep, r_chain); 1070 splx(s); 1071 1072 /* 1073 * Decrement the outstanding request count. 1074 */ 1075 if (rep->r_flags & R_SENT) { 1076 rep->r_flags &= ~R_SENT; /* paranoia */ 1077 nmp->nm_sent -= NFS_CWNDSCALE; 1078 } 1079 1080 /* 1081 * If there was a successful reply and a tprintf msg. 1082 * tprintf a response. 1083 */ 1084 if (!error && (rep->r_flags & R_TPRINTFMSG)) 1085 nfs_msg(rep->r_procp, nmp->nm_mountp->mnt_stat.f_mntfromname, 1086 "is alive again"); 1087 mrep = rep->r_mrep; 1088 md = rep->r_md; 1089 dpos = rep->r_dpos; 1090 if (error) { 1091 m_freem(rep->r_mreq); 1092 free((caddr_t)rep, M_NFSREQ); 1093 return (error); 1094 } 1095 1096 /* 1097 * break down the rpc header and check if ok 1098 */ 1099 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1100 if (*tl++ == rpc_msgdenied) { 1101 if (*tl == rpc_mismatch) 1102 error = EOPNOTSUPP; 1103 else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) { 1104 if (!failed_auth) { 1105 failed_auth++; 1106 mheadend->m_next = (struct mbuf *)0; 1107 m_freem(mrep); 1108 m_freem(rep->r_mreq); 1109 goto kerbauth; 1110 } else 1111 error = EAUTH; 1112 } else 1113 error = EACCES; 1114 m_freem(mrep); 1115 m_freem(rep->r_mreq); 1116 free((caddr_t)rep, M_NFSREQ); 1117 return (error); 1118 } 1119 1120 /* 1121 * Grab any Kerberos verifier, otherwise just throw it away. 1122 */ 1123 verf_type = fxdr_unsigned(int, *tl++); 1124 i = fxdr_unsigned(int32_t, *tl); 1125 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) { 1126 error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep); 1127 if (error) 1128 goto nfsmout; 1129 } else if (i > 0) 1130 nfsm_adv(nfsm_rndup(i)); 1131 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1132 /* 0 == ok */ 1133 if (*tl == 0) { 1134 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1135 if (*tl != 0) { 1136 error = fxdr_unsigned(int, *tl); 1137 if ((nmp->nm_flag & NFSMNT_NFSV3) && 1138 error == NFSERR_TRYLATER) { 1139 m_freem(mrep); 1140 error = 0; 1141 waituntil = time.tv_sec + trylater_delay; 1142 while (time.tv_sec < waituntil) 1143 (void) tsleep((caddr_t)&lbolt, 1144 PSOCK, "nqnfstry", 0); 1145 trylater_delay *= NFS_TRYLATERDELMUL; 1146 if (trylater_delay > NFS_TRYLATERDELMAX) 1147 trylater_delay = NFS_TRYLATERDELMAX; 1148 /* 1149 * RFC1813: 1150 * The client should wait and then try 1151 * the request with a new RPC transaction ID. 1152 */ 1153 nfs_renewxid(rep); 1154 goto tryagain; 1155 } 1156 1157 /* 1158 * If the File Handle was stale, invalidate the 1159 * lookup cache, just in case. 1160 */ 1161 if (error == ESTALE) 1162 cache_purge(NFSTOV(np)); 1163 if (nmp->nm_flag & NFSMNT_NFSV3) { 1164 *mrp = mrep; 1165 *mdp = md; 1166 *dposp = dpos; 1167 error |= NFSERR_RETERR; 1168 } else 1169 m_freem(mrep); 1170 m_freem(rep->r_mreq); 1171 free((caddr_t)rep, M_NFSREQ); 1172 return (error); 1173 } 1174 1175 #ifndef NFS_V2_ONLY 1176 /* 1177 * For nqnfs, get any lease in reply 1178 */ 1179 if (nmp->nm_flag & NFSMNT_NQNFS) { 1180 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1181 if (*tl) { 1182 nqlflag = fxdr_unsigned(int, *tl); 1183 nfsm_dissect(tl, u_int32_t *, 4*NFSX_UNSIGNED); 1184 cachable = fxdr_unsigned(int, *tl++); 1185 reqtime += fxdr_unsigned(int, *tl++); 1186 if (reqtime > time.tv_sec) { 1187 frev = fxdr_hyper(tl); 1188 nqnfs_clientlease(nmp, np, nqlflag, 1189 cachable, reqtime, frev); 1190 } 1191 } 1192 } 1193 #endif 1194 *mrp = mrep; 1195 *mdp = md; 1196 *dposp = dpos; 1197 m_freem(rep->r_mreq); 1198 FREE((caddr_t)rep, M_NFSREQ); 1199 return (0); 1200 } 1201 m_freem(mrep); 1202 error = EPROTONOSUPPORT; 1203 nfsmout: 1204 m_freem(rep->r_mreq); 1205 free((caddr_t)rep, M_NFSREQ); 1206 return (error); 1207 } 1208 #endif /* NFS */ 1209 1210 /* 1211 * Generate the rpc reply header 1212 * siz arg. is used to decide if adding a cluster is worthwhile 1213 */ 1214 int 1215 nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp) 1216 int siz; 1217 struct nfsrv_descript *nd; 1218 struct nfssvc_sock *slp; 1219 int err; 1220 int cache; 1221 u_quad_t *frev; 1222 struct mbuf **mrq; 1223 struct mbuf **mbp; 1224 caddr_t *bposp; 1225 { 1226 u_int32_t *tl; 1227 struct mbuf *mreq; 1228 caddr_t bpos; 1229 struct mbuf *mb; 1230 1231 mreq = m_gethdr(M_WAIT, MT_DATA); 1232 MCLAIM(mreq, &nfs_mowner); 1233 mb = mreq; 1234 /* 1235 * If this is a big reply, use a cluster else 1236 * try and leave leading space for the lower level headers. 1237 */ 1238 siz += RPC_REPLYSIZ; 1239 if (siz >= max_datalen) { 1240 m_clget(mreq, M_WAIT); 1241 } else 1242 mreq->m_data += max_hdr; 1243 tl = mtod(mreq, u_int32_t *); 1244 mreq->m_len = 6 * NFSX_UNSIGNED; 1245 bpos = ((caddr_t)tl) + mreq->m_len; 1246 *tl++ = txdr_unsigned(nd->nd_retxid); 1247 *tl++ = rpc_reply; 1248 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) { 1249 *tl++ = rpc_msgdenied; 1250 if (err & NFSERR_AUTHERR) { 1251 *tl++ = rpc_autherr; 1252 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR); 1253 mreq->m_len -= NFSX_UNSIGNED; 1254 bpos -= NFSX_UNSIGNED; 1255 } else { 1256 *tl++ = rpc_mismatch; 1257 *tl++ = txdr_unsigned(RPC_VER2); 1258 *tl = txdr_unsigned(RPC_VER2); 1259 } 1260 } else { 1261 *tl++ = rpc_msgaccepted; 1262 1263 /* 1264 * For Kerberos authentication, we must send the nickname 1265 * verifier back, otherwise just RPCAUTH_NULL. 1266 */ 1267 if (nd->nd_flag & ND_KERBFULL) { 1268 struct nfsuid *nuidp; 1269 struct timeval ktvin, ktvout; 1270 1271 LIST_FOREACH(nuidp, NUIDHASH(slp, nd->nd_cr.cr_uid), 1272 nu_hash) { 1273 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid && 1274 (!nd->nd_nam2 || netaddr_match( 1275 NU_NETFAM(nuidp), &nuidp->nu_haddr, 1276 nd->nd_nam2))) 1277 break; 1278 } 1279 if (nuidp) { 1280 ktvin.tv_sec = 1281 txdr_unsigned(nuidp->nu_timestamp.tv_sec 1282 - 1); 1283 ktvin.tv_usec = 1284 txdr_unsigned(nuidp->nu_timestamp.tv_usec); 1285 1286 /* 1287 * Encrypt the timestamp in ecb mode using the 1288 * session key. 1289 */ 1290 #ifdef NFSKERB 1291 XXX 1292 #endif 1293 1294 *tl++ = rpc_auth_kerb; 1295 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED); 1296 *tl = ktvout.tv_sec; 1297 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1298 *tl++ = ktvout.tv_usec; 1299 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid); 1300 } else { 1301 *tl++ = 0; 1302 *tl++ = 0; 1303 } 1304 } else { 1305 *tl++ = 0; 1306 *tl++ = 0; 1307 } 1308 switch (err) { 1309 case EPROGUNAVAIL: 1310 *tl = txdr_unsigned(RPC_PROGUNAVAIL); 1311 break; 1312 case EPROGMISMATCH: 1313 *tl = txdr_unsigned(RPC_PROGMISMATCH); 1314 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1315 if (nd->nd_flag & ND_NQNFS) { 1316 *tl++ = txdr_unsigned(3); 1317 *tl = txdr_unsigned(3); 1318 } else { 1319 *tl++ = txdr_unsigned(2); 1320 *tl = txdr_unsigned(3); 1321 } 1322 break; 1323 case EPROCUNAVAIL: 1324 *tl = txdr_unsigned(RPC_PROCUNAVAIL); 1325 break; 1326 case EBADRPC: 1327 *tl = txdr_unsigned(RPC_GARBAGE); 1328 break; 1329 default: 1330 *tl = 0; 1331 if (err != NFSERR_RETVOID) { 1332 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1333 if (err) 1334 *tl = txdr_unsigned(nfsrv_errmap(nd, err)); 1335 else 1336 *tl = 0; 1337 } 1338 break; 1339 }; 1340 } 1341 1342 /* 1343 * For nqnfs, piggyback lease as requested. 1344 */ 1345 if ((nd->nd_flag & ND_NQNFS) && err == 0) { 1346 if (nd->nd_flag & ND_LEASE) { 1347 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1348 *tl++ = txdr_unsigned(nd->nd_flag & ND_LEASE); 1349 *tl++ = txdr_unsigned(cache); 1350 *tl++ = txdr_unsigned(nd->nd_duration); 1351 txdr_hyper(*frev, tl); 1352 } else { 1353 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1354 *tl = 0; 1355 } 1356 } 1357 if (mrq != NULL) 1358 *mrq = mreq; 1359 *mbp = mb; 1360 *bposp = bpos; 1361 if (err != 0 && err != NFSERR_RETVOID) 1362 nfsstats.srvrpc_errs++; 1363 return (0); 1364 } 1365 1366 /* 1367 * Nfs timer routine 1368 * Scan the nfsreq list and retranmit any requests that have timed out 1369 * To avoid retransmission attempts on STREAM sockets (in the future) make 1370 * sure to set the r_retry field to 0 (implies nm_retry == 0). 1371 */ 1372 void 1373 nfs_timer(arg) 1374 void *arg; /* never used */ 1375 { 1376 struct nfsreq *rep; 1377 struct mbuf *m; 1378 struct socket *so; 1379 struct nfsmount *nmp; 1380 int timeo; 1381 int s, error; 1382 #ifdef NFSSERVER 1383 struct nfssvc_sock *slp; 1384 static long lasttime = 0; 1385 u_quad_t cur_usec; 1386 #endif 1387 1388 s = splsoftnet(); 1389 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) { 1390 nmp = rep->r_nmp; 1391 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) 1392 continue; 1393 if (nfs_sigintr(nmp, rep, rep->r_procp)) { 1394 rep->r_flags |= R_SOFTTERM; 1395 continue; 1396 } 1397 if (rep->r_rtt >= 0) { 1398 rep->r_rtt++; 1399 if (nmp->nm_flag & NFSMNT_DUMBTIMR) 1400 timeo = nmp->nm_timeo; 1401 else 1402 timeo = NFS_RTO(nmp, proct[rep->r_procnum]); 1403 if (nmp->nm_timeouts > 0) 1404 timeo *= nfs_backoff[nmp->nm_timeouts - 1]; 1405 if (rep->r_rtt <= timeo) 1406 continue; 1407 if (nmp->nm_timeouts < 1408 (sizeof(nfs_backoff) / sizeof(nfs_backoff[0]))) 1409 nmp->nm_timeouts++; 1410 } 1411 /* 1412 * Check for server not responding 1413 */ 1414 if ((rep->r_flags & R_TPRINTFMSG) == 0 && 1415 rep->r_rexmit > nmp->nm_deadthresh) { 1416 nfs_msg(rep->r_procp, 1417 nmp->nm_mountp->mnt_stat.f_mntfromname, 1418 "not responding"); 1419 rep->r_flags |= R_TPRINTFMSG; 1420 } 1421 if (rep->r_rexmit >= rep->r_retry) { /* too many */ 1422 nfsstats.rpctimeouts++; 1423 rep->r_flags |= R_SOFTTERM; 1424 continue; 1425 } 1426 if (nmp->nm_sotype != SOCK_DGRAM) { 1427 if (++rep->r_rexmit > NFS_MAXREXMIT) 1428 rep->r_rexmit = NFS_MAXREXMIT; 1429 continue; 1430 } 1431 if ((so = nmp->nm_so) == NULL) 1432 continue; 1433 1434 /* 1435 * If there is enough space and the window allows.. 1436 * Resend it 1437 * Set r_rtt to -1 in case we fail to send it now. 1438 */ 1439 rep->r_rtt = -1; 1440 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len && 1441 ((nmp->nm_flag & NFSMNT_DUMBTIMR) || 1442 (rep->r_flags & R_SENT) || 1443 nmp->nm_sent < nmp->nm_cwnd) && 1444 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){ 1445 if (so->so_state & SS_ISCONNECTED) 1446 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m, 1447 (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0); 1448 else 1449 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m, 1450 nmp->nm_nam, (struct mbuf *)0, (struct proc *)0); 1451 if (error) { 1452 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) { 1453 #ifdef DEBUG 1454 printf("nfs_timer: ignoring error %d\n", 1455 error); 1456 #endif 1457 so->so_error = 0; 1458 } 1459 } else { 1460 /* 1461 * Iff first send, start timing 1462 * else turn timing off, backoff timer 1463 * and divide congestion window by 2. 1464 */ 1465 if (rep->r_flags & R_SENT) { 1466 rep->r_flags &= ~R_TIMING; 1467 if (++rep->r_rexmit > NFS_MAXREXMIT) 1468 rep->r_rexmit = NFS_MAXREXMIT; 1469 nmp->nm_cwnd >>= 1; 1470 if (nmp->nm_cwnd < NFS_CWNDSCALE) 1471 nmp->nm_cwnd = NFS_CWNDSCALE; 1472 nfsstats.rpcretries++; 1473 } else { 1474 rep->r_flags |= R_SENT; 1475 nmp->nm_sent += NFS_CWNDSCALE; 1476 } 1477 rep->r_rtt = 0; 1478 } 1479 } 1480 } 1481 1482 #ifdef NFSSERVER 1483 /* 1484 * Call the nqnfs server timer once a second to handle leases. 1485 */ 1486 if (lasttime != time.tv_sec) { 1487 lasttime = time.tv_sec; 1488 nqnfs_serverd(); 1489 } 1490 1491 /* 1492 * Scan the write gathering queues for writes that need to be 1493 * completed now. 1494 */ 1495 cur_usec = (u_quad_t)time.tv_sec * 1000000 + (u_quad_t)time.tv_usec; 1496 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) { 1497 if (LIST_FIRST(&slp->ns_tq) && 1498 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) 1499 nfsrv_wakenfsd(slp); 1500 } 1501 #endif /* NFSSERVER */ 1502 splx(s); 1503 callout_schedule(&nfs_timer_ch, nfs_ticks); 1504 } 1505 1506 /*ARGSUSED*/ 1507 void 1508 nfs_exit(p, v) 1509 struct proc *p; 1510 void *v; 1511 { 1512 struct nfsreq *rp; 1513 int s = splsoftnet(); 1514 1515 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) { 1516 if (rp->r_procp == p) 1517 TAILQ_REMOVE(&nfs_reqq, rp, r_chain); 1518 } 1519 splx(s); 1520 } 1521 1522 /* 1523 * Test for a termination condition pending on the process. 1524 * This is used for NFSMNT_INT mounts. 1525 */ 1526 int 1527 nfs_sigintr(nmp, rep, p) 1528 struct nfsmount *nmp; 1529 struct nfsreq *rep; 1530 struct proc *p; 1531 { 1532 sigset_t ss; 1533 1534 if (rep && (rep->r_flags & R_SOFTTERM)) 1535 return (EINTR); 1536 if (!(nmp->nm_flag & NFSMNT_INT)) 1537 return (0); 1538 if (p) { 1539 sigpending1(p, &ss); 1540 #if 0 1541 sigminusset(&p->p_sigctx.ps_sigignore, &ss); 1542 #endif 1543 if (sigismember(&ss, SIGINT) || sigismember(&ss, SIGTERM) || 1544 sigismember(&ss, SIGKILL) || sigismember(&ss, SIGHUP) || 1545 sigismember(&ss, SIGQUIT)) 1546 return (EINTR); 1547 } 1548 return (0); 1549 } 1550 1551 /* 1552 * Lock a socket against others. 1553 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply 1554 * and also to avoid race conditions between the processes with nfs requests 1555 * in progress when a reconnect is necessary. 1556 */ 1557 int 1558 nfs_sndlock(flagp, rep) 1559 int *flagp; 1560 struct nfsreq *rep; 1561 { 1562 struct proc *p; 1563 int slpflag = 0, slptimeo = 0; 1564 1565 if (rep) { 1566 p = rep->r_procp; 1567 if (rep->r_nmp->nm_flag & NFSMNT_INT) 1568 slpflag = PCATCH; 1569 } else 1570 p = (struct proc *)0; 1571 while (*flagp & NFSMNT_SNDLOCK) { 1572 if (nfs_sigintr(rep->r_nmp, rep, p)) 1573 return (EINTR); 1574 *flagp |= NFSMNT_WANTSND; 1575 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck", 1576 slptimeo); 1577 if (slpflag == PCATCH) { 1578 slpflag = 0; 1579 slptimeo = 2 * hz; 1580 } 1581 } 1582 *flagp |= NFSMNT_SNDLOCK; 1583 return (0); 1584 } 1585 1586 /* 1587 * Unlock the stream socket for others. 1588 */ 1589 void 1590 nfs_sndunlock(flagp) 1591 int *flagp; 1592 { 1593 1594 if ((*flagp & NFSMNT_SNDLOCK) == 0) 1595 panic("nfs sndunlock"); 1596 *flagp &= ~NFSMNT_SNDLOCK; 1597 if (*flagp & NFSMNT_WANTSND) { 1598 *flagp &= ~NFSMNT_WANTSND; 1599 wakeup((caddr_t)flagp); 1600 } 1601 } 1602 1603 int 1604 nfs_rcvlock(rep) 1605 struct nfsreq *rep; 1606 { 1607 struct nfsmount *nmp = rep->r_nmp; 1608 int *flagp = &nmp->nm_iflag; 1609 int slpflag, slptimeo = 0; 1610 int error = 0; 1611 1612 if (*flagp & NFSMNT_DISMNT) 1613 return EIO; 1614 1615 if (*flagp & NFSMNT_INT) 1616 slpflag = PCATCH; 1617 else 1618 slpflag = 0; 1619 simple_lock(&nmp->nm_slock); 1620 while (*flagp & NFSMNT_RCVLOCK) { 1621 if (nfs_sigintr(rep->r_nmp, rep, rep->r_procp)) { 1622 error = EINTR; 1623 goto quit; 1624 } 1625 *flagp |= NFSMNT_WANTRCV; 1626 nmp->nm_waiters++; 1627 (void) ltsleep(flagp, slpflag | (PZERO - 1), "nfsrcvlk", 1628 slptimeo, &nmp->nm_slock); 1629 nmp->nm_waiters--; 1630 if (*flagp & NFSMNT_DISMNT) { 1631 wakeup(&nmp->nm_waiters); 1632 error = EIO; 1633 goto quit; 1634 } 1635 /* If our reply was received while we were sleeping, 1636 * then just return without taking the lock to avoid a 1637 * situation where a single iod could 'capture' the 1638 * receive lock. 1639 */ 1640 if (rep->r_mrep != NULL) { 1641 error = EALREADY; 1642 goto quit; 1643 } 1644 if (slpflag == PCATCH) { 1645 slpflag = 0; 1646 slptimeo = 2 * hz; 1647 } 1648 } 1649 *flagp |= NFSMNT_RCVLOCK; 1650 quit: 1651 simple_unlock(&nmp->nm_slock); 1652 return error; 1653 } 1654 1655 /* 1656 * Unlock the stream socket for others. 1657 */ 1658 void 1659 nfs_rcvunlock(nmp) 1660 struct nfsmount *nmp; 1661 { 1662 int *flagp = &nmp->nm_iflag; 1663 1664 simple_lock(&nmp->nm_slock); 1665 if ((*flagp & NFSMNT_RCVLOCK) == 0) 1666 panic("nfs rcvunlock"); 1667 *flagp &= ~NFSMNT_RCVLOCK; 1668 if (*flagp & NFSMNT_WANTRCV) { 1669 *flagp &= ~NFSMNT_WANTRCV; 1670 wakeup((caddr_t)flagp); 1671 } 1672 simple_unlock(&nmp->nm_slock); 1673 } 1674 1675 /* 1676 * Parse an RPC request 1677 * - verify it 1678 * - fill in the cred struct. 1679 */ 1680 int 1681 nfs_getreq(nd, nfsd, has_header) 1682 struct nfsrv_descript *nd; 1683 struct nfsd *nfsd; 1684 int has_header; 1685 { 1686 int len, i; 1687 u_int32_t *tl; 1688 int32_t t1; 1689 struct uio uio; 1690 struct iovec iov; 1691 caddr_t dpos, cp2, cp; 1692 u_int32_t nfsvers, auth_type; 1693 uid_t nickuid; 1694 int error = 0, nqnfs = 0, ticklen; 1695 struct mbuf *mrep, *md; 1696 struct nfsuid *nuidp; 1697 struct timeval tvin, tvout; 1698 1699 mrep = nd->nd_mrep; 1700 md = nd->nd_md; 1701 dpos = nd->nd_dpos; 1702 if (has_header) { 1703 nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED); 1704 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++); 1705 if (*tl++ != rpc_call) { 1706 m_freem(mrep); 1707 return (EBADRPC); 1708 } 1709 } else 1710 nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 1711 nd->nd_repstat = 0; 1712 nd->nd_flag = 0; 1713 if (*tl++ != rpc_vers) { 1714 nd->nd_repstat = ERPCMISMATCH; 1715 nd->nd_procnum = NFSPROC_NOOP; 1716 return (0); 1717 } 1718 if (*tl != nfs_prog) { 1719 if (*tl == nqnfs_prog) 1720 nqnfs++; 1721 else { 1722 nd->nd_repstat = EPROGUNAVAIL; 1723 nd->nd_procnum = NFSPROC_NOOP; 1724 return (0); 1725 } 1726 } 1727 tl++; 1728 nfsvers = fxdr_unsigned(u_int32_t, *tl++); 1729 if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) || 1730 (nfsvers != NQNFS_VER3 && nqnfs)) { 1731 nd->nd_repstat = EPROGMISMATCH; 1732 nd->nd_procnum = NFSPROC_NOOP; 1733 return (0); 1734 } 1735 if (nqnfs) 1736 nd->nd_flag = (ND_NFSV3 | ND_NQNFS); 1737 else if (nfsvers == NFS_VER3) 1738 nd->nd_flag = ND_NFSV3; 1739 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++); 1740 if (nd->nd_procnum == NFSPROC_NULL) 1741 return (0); 1742 if (nd->nd_procnum >= NFS_NPROCS || 1743 (!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) || 1744 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) { 1745 nd->nd_repstat = EPROCUNAVAIL; 1746 nd->nd_procnum = NFSPROC_NOOP; 1747 return (0); 1748 } 1749 if ((nd->nd_flag & ND_NFSV3) == 0) 1750 nd->nd_procnum = nfsv3_procid[nd->nd_procnum]; 1751 auth_type = *tl++; 1752 len = fxdr_unsigned(int, *tl++); 1753 if (len < 0 || len > RPCAUTH_MAXSIZ) { 1754 m_freem(mrep); 1755 return (EBADRPC); 1756 } 1757 1758 nd->nd_flag &= ~ND_KERBAUTH; 1759 /* 1760 * Handle auth_unix or auth_kerb. 1761 */ 1762 if (auth_type == rpc_auth_unix) { 1763 len = fxdr_unsigned(int, *++tl); 1764 if (len < 0 || len > NFS_MAXNAMLEN) { 1765 m_freem(mrep); 1766 return (EBADRPC); 1767 } 1768 nfsm_adv(nfsm_rndup(len)); 1769 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1770 memset((caddr_t)&nd->nd_cr, 0, sizeof (struct ucred)); 1771 nd->nd_cr.cr_ref = 1; 1772 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++); 1773 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++); 1774 len = fxdr_unsigned(int, *tl); 1775 if (len < 0 || len > RPCAUTH_UNIXGIDS) { 1776 m_freem(mrep); 1777 return (EBADRPC); 1778 } 1779 nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED); 1780 for (i = 0; i < len; i++) 1781 if (i < NGROUPS) 1782 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++); 1783 else 1784 tl++; 1785 nd->nd_cr.cr_ngroups = (len > NGROUPS) ? NGROUPS : len; 1786 if (nd->nd_cr.cr_ngroups > 1) 1787 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups); 1788 len = fxdr_unsigned(int, *++tl); 1789 if (len < 0 || len > RPCAUTH_MAXSIZ) { 1790 m_freem(mrep); 1791 return (EBADRPC); 1792 } 1793 if (len > 0) 1794 nfsm_adv(nfsm_rndup(len)); 1795 } else if (auth_type == rpc_auth_kerb) { 1796 switch (fxdr_unsigned(int, *tl++)) { 1797 case RPCAKN_FULLNAME: 1798 ticklen = fxdr_unsigned(int, *tl); 1799 *((u_int32_t *)nfsd->nfsd_authstr) = *tl; 1800 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED; 1801 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED; 1802 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) { 1803 m_freem(mrep); 1804 return (EBADRPC); 1805 } 1806 uio.uio_offset = 0; 1807 uio.uio_iov = &iov; 1808 uio.uio_iovcnt = 1; 1809 uio.uio_segflg = UIO_SYSSPACE; 1810 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4]; 1811 iov.iov_len = RPCAUTH_MAXSIZ - 4; 1812 nfsm_mtouio(&uio, uio.uio_resid); 1813 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1814 if (*tl++ != rpc_auth_kerb || 1815 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) { 1816 printf("Bad kerb verifier\n"); 1817 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 1818 nd->nd_procnum = NFSPROC_NOOP; 1819 return (0); 1820 } 1821 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED); 1822 tl = (u_int32_t *)cp; 1823 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) { 1824 printf("Not fullname kerb verifier\n"); 1825 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 1826 nd->nd_procnum = NFSPROC_NOOP; 1827 return (0); 1828 } 1829 cp += NFSX_UNSIGNED; 1830 memcpy(nfsd->nfsd_verfstr, cp, 3 * NFSX_UNSIGNED); 1831 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED; 1832 nd->nd_flag |= ND_KERBFULL; 1833 nfsd->nfsd_flag |= NFSD_NEEDAUTH; 1834 break; 1835 case RPCAKN_NICKNAME: 1836 if (len != 2 * NFSX_UNSIGNED) { 1837 printf("Kerb nickname short\n"); 1838 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED); 1839 nd->nd_procnum = NFSPROC_NOOP; 1840 return (0); 1841 } 1842 nickuid = fxdr_unsigned(uid_t, *tl); 1843 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1844 if (*tl++ != rpc_auth_kerb || 1845 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) { 1846 printf("Kerb nick verifier bad\n"); 1847 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 1848 nd->nd_procnum = NFSPROC_NOOP; 1849 return (0); 1850 } 1851 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1852 tvin.tv_sec = *tl++; 1853 tvin.tv_usec = *tl; 1854 1855 LIST_FOREACH(nuidp, NUIDHASH(nfsd->nfsd_slp, nickuid), 1856 nu_hash) { 1857 if (nuidp->nu_cr.cr_uid == nickuid && 1858 (!nd->nd_nam2 || 1859 netaddr_match(NU_NETFAM(nuidp), 1860 &nuidp->nu_haddr, nd->nd_nam2))) 1861 break; 1862 } 1863 if (!nuidp) { 1864 nd->nd_repstat = 1865 (NFSERR_AUTHERR|AUTH_REJECTCRED); 1866 nd->nd_procnum = NFSPROC_NOOP; 1867 return (0); 1868 } 1869 1870 /* 1871 * Now, decrypt the timestamp using the session key 1872 * and validate it. 1873 */ 1874 #ifdef NFSKERB 1875 XXX 1876 #endif 1877 1878 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec); 1879 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec); 1880 if (nuidp->nu_expire < time.tv_sec || 1881 nuidp->nu_timestamp.tv_sec > tvout.tv_sec || 1882 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec && 1883 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) { 1884 nuidp->nu_expire = 0; 1885 nd->nd_repstat = 1886 (NFSERR_AUTHERR|AUTH_REJECTVERF); 1887 nd->nd_procnum = NFSPROC_NOOP; 1888 return (0); 1889 } 1890 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr); 1891 nd->nd_flag |= ND_KERBNICK; 1892 }; 1893 } else { 1894 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED); 1895 nd->nd_procnum = NFSPROC_NOOP; 1896 return (0); 1897 } 1898 1899 /* 1900 * For nqnfs, get piggybacked lease request. 1901 */ 1902 if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) { 1903 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1904 nd->nd_flag |= fxdr_unsigned(int, *tl); 1905 if (nd->nd_flag & ND_LEASE) { 1906 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1907 nd->nd_duration = fxdr_unsigned(u_int32_t, *tl); 1908 } else 1909 nd->nd_duration = NQ_MINLEASE; 1910 } else 1911 nd->nd_duration = NQ_MINLEASE; 1912 nd->nd_md = md; 1913 nd->nd_dpos = dpos; 1914 return (0); 1915 nfsmout: 1916 return (error); 1917 } 1918 1919 int 1920 nfs_msg(p, server, msg) 1921 struct proc *p; 1922 char *server, *msg; 1923 { 1924 tpr_t tpr; 1925 1926 if (p) 1927 tpr = tprintf_open(p); 1928 else 1929 tpr = NULL; 1930 tprintf(tpr, "nfs server %s: %s\n", server, msg); 1931 tprintf_close(tpr); 1932 return (0); 1933 } 1934 1935 #ifdef NFSSERVER 1936 int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *, 1937 struct nfssvc_sock *, struct proc *, 1938 struct mbuf **)) = { 1939 nfsrv_null, 1940 nfsrv_getattr, 1941 nfsrv_setattr, 1942 nfsrv_lookup, 1943 nfsrv3_access, 1944 nfsrv_readlink, 1945 nfsrv_read, 1946 nfsrv_write, 1947 nfsrv_create, 1948 nfsrv_mkdir, 1949 nfsrv_symlink, 1950 nfsrv_mknod, 1951 nfsrv_remove, 1952 nfsrv_rmdir, 1953 nfsrv_rename, 1954 nfsrv_link, 1955 nfsrv_readdir, 1956 nfsrv_readdirplus, 1957 nfsrv_statfs, 1958 nfsrv_fsinfo, 1959 nfsrv_pathconf, 1960 nfsrv_commit, 1961 nqnfsrv_getlease, 1962 nqnfsrv_vacated, 1963 nfsrv_noop, 1964 nfsrv_noop 1965 }; 1966 1967 /* 1968 * Socket upcall routine for the nfsd sockets. 1969 * The caddr_t arg is a pointer to the "struct nfssvc_sock". 1970 * Essentially do as much as possible non-blocking, else punt and it will 1971 * be called with M_WAIT from an nfsd. 1972 */ 1973 void 1974 nfsrv_rcv(so, arg, waitflag) 1975 struct socket *so; 1976 caddr_t arg; 1977 int waitflag; 1978 { 1979 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg; 1980 struct mbuf *m; 1981 struct mbuf *mp, *nam; 1982 struct uio auio; 1983 int flags, error; 1984 1985 if ((slp->ns_flag & SLP_VALID) == 0) 1986 return; 1987 #ifdef notdef 1988 /* 1989 * Define this to test for nfsds handling this under heavy load. 1990 */ 1991 if (waitflag == M_DONTWAIT) { 1992 slp->ns_flag |= SLP_NEEDQ; goto dorecs; 1993 } 1994 #endif 1995 auio.uio_procp = NULL; 1996 if (so->so_type == SOCK_STREAM) { 1997 /* 1998 * If there are already records on the queue, defer soreceive() 1999 * to an nfsd so that there is feedback to the TCP layer that 2000 * the nfs servers are heavily loaded. 2001 */ 2002 if (slp->ns_rec && waitflag == M_DONTWAIT) { 2003 slp->ns_flag |= SLP_NEEDQ; 2004 goto dorecs; 2005 } 2006 2007 /* 2008 * Do soreceive(). 2009 */ 2010 auio.uio_resid = 1000000000; 2011 flags = MSG_DONTWAIT; 2012 error = (*so->so_receive)(so, &nam, &auio, &mp, (struct mbuf **)0, &flags); 2013 if (error || mp == (struct mbuf *)0) { 2014 if (error == EWOULDBLOCK) 2015 slp->ns_flag |= SLP_NEEDQ; 2016 else 2017 slp->ns_flag |= SLP_DISCONN; 2018 goto dorecs; 2019 } 2020 m = mp; 2021 if (slp->ns_rawend) { 2022 slp->ns_rawend->m_next = m; 2023 slp->ns_cc += 1000000000 - auio.uio_resid; 2024 } else { 2025 slp->ns_raw = m; 2026 slp->ns_cc = 1000000000 - auio.uio_resid; 2027 } 2028 while (m->m_next) 2029 m = m->m_next; 2030 slp->ns_rawend = m; 2031 2032 /* 2033 * Now try and parse record(s) out of the raw stream data. 2034 */ 2035 error = nfsrv_getstream(slp, waitflag); 2036 if (error) { 2037 if (error == EPERM) 2038 slp->ns_flag |= SLP_DISCONN; 2039 else 2040 slp->ns_flag |= SLP_NEEDQ; 2041 } 2042 } else { 2043 do { 2044 auio.uio_resid = 1000000000; 2045 flags = MSG_DONTWAIT; 2046 error = (*so->so_receive)(so, &nam, &auio, &mp, 2047 (struct mbuf **)0, &flags); 2048 if (mp) { 2049 if (nam) { 2050 m = nam; 2051 m->m_next = mp; 2052 } else 2053 m = mp; 2054 if (slp->ns_recend) 2055 slp->ns_recend->m_nextpkt = m; 2056 else 2057 slp->ns_rec = m; 2058 slp->ns_recend = m; 2059 m->m_nextpkt = (struct mbuf *)0; 2060 } 2061 if (error) { 2062 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) 2063 && error != EWOULDBLOCK) { 2064 slp->ns_flag |= SLP_DISCONN; 2065 goto dorecs; 2066 } 2067 } 2068 } while (mp); 2069 } 2070 2071 /* 2072 * Now try and process the request records, non-blocking. 2073 */ 2074 dorecs: 2075 if (waitflag == M_DONTWAIT && 2076 (slp->ns_rec || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN)))) 2077 nfsrv_wakenfsd(slp); 2078 } 2079 2080 /* 2081 * Try and extract an RPC request from the mbuf data list received on a 2082 * stream socket. The "waitflag" argument indicates whether or not it 2083 * can sleep. 2084 */ 2085 int 2086 nfsrv_getstream(slp, waitflag) 2087 struct nfssvc_sock *slp; 2088 int waitflag; 2089 { 2090 struct mbuf *m, **mpp; 2091 struct mbuf *recm; 2092 u_int32_t recmark; 2093 2094 if (slp->ns_flag & SLP_GETSTREAM) 2095 panic("nfs getstream"); 2096 slp->ns_flag |= SLP_GETSTREAM; 2097 for (;;) { 2098 if (slp->ns_reclen == 0) { 2099 if (slp->ns_cc < NFSX_UNSIGNED) { 2100 slp->ns_flag &= ~SLP_GETSTREAM; 2101 return (0); 2102 } 2103 m = slp->ns_raw; 2104 m_copydata(m, 0, NFSX_UNSIGNED, (caddr_t)&recmark); 2105 m_adj(m, NFSX_UNSIGNED); 2106 slp->ns_cc -= NFSX_UNSIGNED; 2107 recmark = ntohl(recmark); 2108 slp->ns_reclen = recmark & ~0x80000000; 2109 if (recmark & 0x80000000) 2110 slp->ns_flag |= SLP_LASTFRAG; 2111 else 2112 slp->ns_flag &= ~SLP_LASTFRAG; 2113 if (slp->ns_reclen > NFS_MAXPACKET) { 2114 slp->ns_flag &= ~SLP_GETSTREAM; 2115 return (EPERM); 2116 } 2117 } 2118 2119 /* 2120 * Now get the record part. 2121 * 2122 * Note that slp->ns_reclen may be 0. Linux sometimes 2123 * generates 0-length records. 2124 */ 2125 if (slp->ns_cc == slp->ns_reclen) { 2126 recm = slp->ns_raw; 2127 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0; 2128 slp->ns_cc = slp->ns_reclen = 0; 2129 } else if (slp->ns_cc > slp->ns_reclen) { 2130 recm = slp->ns_raw; 2131 m = m_split(recm, slp->ns_reclen, waitflag); 2132 if (m == NULL) { 2133 slp->ns_flag &= ~SLP_GETSTREAM; 2134 return (EWOULDBLOCK); 2135 } 2136 m_claim(recm, &nfs_mowner); 2137 slp->ns_raw = m; 2138 if (m->m_next == NULL) 2139 slp->ns_rawend = m; 2140 slp->ns_cc -= slp->ns_reclen; 2141 slp->ns_reclen = 0; 2142 } else { 2143 slp->ns_flag &= ~SLP_GETSTREAM; 2144 return (0); 2145 } 2146 2147 /* 2148 * Accumulate the fragments into a record. 2149 */ 2150 mpp = &slp->ns_frag; 2151 while (*mpp) 2152 mpp = &((*mpp)->m_next); 2153 *mpp = recm; 2154 if (slp->ns_flag & SLP_LASTFRAG) { 2155 if (slp->ns_recend) 2156 slp->ns_recend->m_nextpkt = slp->ns_frag; 2157 else 2158 slp->ns_rec = slp->ns_frag; 2159 slp->ns_recend = slp->ns_frag; 2160 slp->ns_frag = (struct mbuf *)0; 2161 } 2162 } 2163 } 2164 2165 /* 2166 * Parse an RPC header. 2167 */ 2168 int 2169 nfsrv_dorec(slp, nfsd, ndp) 2170 struct nfssvc_sock *slp; 2171 struct nfsd *nfsd; 2172 struct nfsrv_descript **ndp; 2173 { 2174 struct mbuf *m, *nam; 2175 struct nfsrv_descript *nd; 2176 int error; 2177 2178 *ndp = NULL; 2179 if ((slp->ns_flag & SLP_VALID) == 0 || 2180 (m = slp->ns_rec) == (struct mbuf *)0) 2181 return (ENOBUFS); 2182 slp->ns_rec = m->m_nextpkt; 2183 if (slp->ns_rec) 2184 m->m_nextpkt = (struct mbuf *)0; 2185 else 2186 slp->ns_recend = (struct mbuf *)0; 2187 if (m->m_type == MT_SONAME) { 2188 nam = m; 2189 m = m->m_next; 2190 nam->m_next = NULL; 2191 } else 2192 nam = NULL; 2193 nd = pool_get(&nfs_srvdesc_pool, PR_WAITOK); 2194 nd->nd_md = nd->nd_mrep = m; 2195 nd->nd_nam2 = nam; 2196 nd->nd_dpos = mtod(m, caddr_t); 2197 error = nfs_getreq(nd, nfsd, TRUE); 2198 if (error) { 2199 m_freem(nam); 2200 pool_put(&nfs_srvdesc_pool, nd); 2201 return (error); 2202 } 2203 *ndp = nd; 2204 nfsd->nfsd_nd = nd; 2205 return (0); 2206 } 2207 2208 2209 /* 2210 * Search for a sleeping nfsd and wake it up. 2211 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the 2212 * running nfsds will go look for the work in the nfssvc_sock list. 2213 */ 2214 void 2215 nfsrv_wakenfsd(slp) 2216 struct nfssvc_sock *slp; 2217 { 2218 struct nfsd *nd; 2219 2220 if ((slp->ns_flag & SLP_VALID) == 0) 2221 return; 2222 simple_lock(&nfsd_slock); 2223 if (slp->ns_flag & SLP_DOREC) { 2224 simple_unlock(&nfsd_slock); 2225 return; 2226 } 2227 nd = SLIST_FIRST(&nfsd_idle_head); 2228 if (nd) { 2229 SLIST_REMOVE_HEAD(&nfsd_idle_head, nfsd_idle); 2230 simple_unlock(&nfsd_slock); 2231 2232 KASSERT(nd->nfsd_flag & NFSD_WAITING); 2233 nd->nfsd_flag &= ~NFSD_WAITING; 2234 if (nd->nfsd_slp) 2235 panic("nfsd wakeup"); 2236 slp->ns_sref++; 2237 nd->nfsd_slp = slp; 2238 wakeup(nd); 2239 return; 2240 } 2241 slp->ns_flag |= SLP_DOREC; 2242 nfsd_head_flag |= NFSD_CHECKSLP; 2243 TAILQ_INSERT_TAIL(&nfssvc_sockpending, slp, ns_pending); 2244 simple_unlock(&nfsd_slock); 2245 } 2246 #endif /* NFSSERVER */ 2247