1 /* 2 * Copyright (c) 1989, 1991, 1993, 1995 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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95 37 * $FreeBSD: src/sys/nfs/nfs_socket.c,v 1.60.2.6 2003/03/26 01:44:46 alfred Exp $ 38 * $DragonFly: src/sys/vfs/nfs/nfs_socket.c,v 1.32 2006/03/01 15:09:35 drhodus Exp $ 39 */ 40 41 /* 42 * Socket operations for use by nfs 43 */ 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/proc.h> 48 #include <sys/malloc.h> 49 #include <sys/mount.h> 50 #include <sys/kernel.h> 51 #include <sys/mbuf.h> 52 #include <sys/vnode.h> 53 #include <sys/protosw.h> 54 #include <sys/resourcevar.h> 55 #include <sys/socket.h> 56 #include <sys/socketvar.h> 57 #include <sys/socketops.h> 58 #include <sys/syslog.h> 59 #include <sys/thread.h> 60 #include <sys/tprintf.h> 61 #include <sys/sysctl.h> 62 #include <sys/signalvar.h> 63 64 #include <netinet/in.h> 65 #include <netinet/tcp.h> 66 #include <sys/thread2.h> 67 68 #include "rpcv2.h" 69 #include "nfsproto.h" 70 #include "nfs.h" 71 #include "xdr_subs.h" 72 #include "nfsm_subs.h" 73 #include "nfsmount.h" 74 #include "nfsnode.h" 75 #include "nfsrtt.h" 76 #include "nqnfs.h" 77 78 #define TRUE 1 79 #define FALSE 0 80 81 /* 82 * Estimate rto for an nfs rpc sent via. an unreliable datagram. 83 * Use the mean and mean deviation of rtt for the appropriate type of rpc 84 * for the frequent rpcs and a default for the others. 85 * The justification for doing "other" this way is that these rpcs 86 * happen so infrequently that timer est. would probably be stale. 87 * Also, since many of these rpcs are 88 * non-idempotent, a conservative timeout is desired. 89 * getattr, lookup - A+2D 90 * read, write - A+4D 91 * other - nm_timeo 92 */ 93 #define NFS_RTO(n, t) \ 94 ((t) == 0 ? (n)->nm_timeo : \ 95 ((t) < 3 ? \ 96 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \ 97 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1))) 98 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1] 99 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1] 100 /* 101 * External data, mostly RPC constants in XDR form 102 */ 103 extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers, 104 rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr, 105 rpc_auth_kerb; 106 extern u_int32_t nfs_prog, nqnfs_prog; 107 extern time_t nqnfsstarttime; 108 extern struct nfsstats nfsstats; 109 extern int nfsv3_procid[NFS_NPROCS]; 110 extern int nfs_ticks; 111 112 /* 113 * Defines which timer to use for the procnum. 114 * 0 - default 115 * 1 - getattr 116 * 2 - lookup 117 * 3 - read 118 * 4 - write 119 */ 120 static int proct[NFS_NPROCS] = { 121 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 122 0, 0, 0, 123 }; 124 125 static int nfs_realign_test; 126 static int nfs_realign_count; 127 static int nfs_bufpackets = 4; 128 static int nfs_timer_raced; 129 130 SYSCTL_DECL(_vfs_nfs); 131 132 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, ""); 133 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, ""); 134 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, ""); 135 136 137 /* 138 * There is a congestion window for outstanding rpcs maintained per mount 139 * point. The cwnd size is adjusted in roughly the way that: 140 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of 141 * SIGCOMM '88". ACM, August 1988. 142 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout 143 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd 144 * of rpcs is in progress. 145 * (The sent count and cwnd are scaled for integer arith.) 146 * Variants of "slow start" were tried and were found to be too much of a 147 * performance hit (ave. rtt 3 times larger), 148 * I suspect due to the large rtt that nfs rpcs have. 149 */ 150 #define NFS_CWNDSCALE 256 151 #define NFS_MAXCWND (NFS_CWNDSCALE * 32) 152 static int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, }; 153 int nfsrtton = 0; 154 struct nfsrtt nfsrtt; 155 struct callout nfs_timer_handle; 156 157 static int nfs_msg (struct thread *,char *,char *); 158 static int nfs_rcvlock (struct nfsreq *); 159 static void nfs_rcvunlock (struct nfsreq *); 160 static void nfs_realign (struct mbuf **pm, int hsiz); 161 static int nfs_receive (struct nfsreq *rep, struct sockaddr **aname, 162 struct mbuf **mp); 163 static void nfs_softterm (struct nfsreq *rep); 164 static int nfs_reconnect (struct nfsreq *rep); 165 #ifndef NFS_NOSERVER 166 static int nfsrv_getstream (struct nfssvc_sock *, int, int *); 167 168 int (*nfsrv3_procs[NFS_NPROCS]) (struct nfsrv_descript *nd, 169 struct nfssvc_sock *slp, 170 struct thread *td, 171 struct mbuf **mreqp) = { 172 nfsrv_null, 173 nfsrv_getattr, 174 nfsrv_setattr, 175 nfsrv_lookup, 176 nfsrv3_access, 177 nfsrv_readlink, 178 nfsrv_read, 179 nfsrv_write, 180 nfsrv_create, 181 nfsrv_mkdir, 182 nfsrv_symlink, 183 nfsrv_mknod, 184 nfsrv_remove, 185 nfsrv_rmdir, 186 nfsrv_rename, 187 nfsrv_link, 188 nfsrv_readdir, 189 nfsrv_readdirplus, 190 nfsrv_statfs, 191 nfsrv_fsinfo, 192 nfsrv_pathconf, 193 nfsrv_commit, 194 nqnfsrv_getlease, 195 nqnfsrv_vacated, 196 nfsrv_noop, 197 nfsrv_noop 198 }; 199 #endif /* NFS_NOSERVER */ 200 201 /* 202 * Initialize sockets and congestion for a new NFS connection. 203 * We do not free the sockaddr if error. 204 */ 205 int 206 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep) 207 { 208 struct socket *so; 209 int error, rcvreserve, sndreserve; 210 int pktscale; 211 struct sockaddr *saddr; 212 struct sockaddr_in *sin; 213 struct thread *td = &thread0; /* only used for socreate and sobind */ 214 215 nmp->nm_so = (struct socket *)0; 216 saddr = nmp->nm_nam; 217 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype, 218 nmp->nm_soproto, td); 219 if (error) 220 goto bad; 221 so = nmp->nm_so; 222 nmp->nm_soflags = so->so_proto->pr_flags; 223 224 /* 225 * Some servers require that the client port be a reserved port number. 226 */ 227 if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) { 228 struct sockopt sopt; 229 int ip; 230 struct sockaddr_in ssin; 231 232 bzero(&sopt, sizeof sopt); 233 ip = IP_PORTRANGE_LOW; 234 sopt.sopt_level = IPPROTO_IP; 235 sopt.sopt_name = IP_PORTRANGE; 236 sopt.sopt_val = (void *)&ip; 237 sopt.sopt_valsize = sizeof(ip); 238 sopt.sopt_td = NULL; 239 error = sosetopt(so, &sopt); 240 if (error) 241 goto bad; 242 bzero(&ssin, sizeof ssin); 243 sin = &ssin; 244 sin->sin_len = sizeof (struct sockaddr_in); 245 sin->sin_family = AF_INET; 246 sin->sin_addr.s_addr = INADDR_ANY; 247 sin->sin_port = htons(0); 248 error = sobind(so, (struct sockaddr *)sin, td); 249 if (error) 250 goto bad; 251 bzero(&sopt, sizeof sopt); 252 ip = IP_PORTRANGE_DEFAULT; 253 sopt.sopt_level = IPPROTO_IP; 254 sopt.sopt_name = IP_PORTRANGE; 255 sopt.sopt_val = (void *)&ip; 256 sopt.sopt_valsize = sizeof(ip); 257 sopt.sopt_td = NULL; 258 error = sosetopt(so, &sopt); 259 if (error) 260 goto bad; 261 } 262 263 /* 264 * Protocols that do not require connections may be optionally left 265 * unconnected for servers that reply from a port other than NFS_PORT. 266 */ 267 if (nmp->nm_flag & NFSMNT_NOCONN) { 268 if (nmp->nm_soflags & PR_CONNREQUIRED) { 269 error = ENOTCONN; 270 goto bad; 271 } 272 } else { 273 error = soconnect(so, nmp->nm_nam, td); 274 if (error) 275 goto bad; 276 277 /* 278 * Wait for the connection to complete. Cribbed from the 279 * connect system call but with the wait timing out so 280 * that interruptible mounts don't hang here for a long time. 281 */ 282 crit_enter(); 283 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) { 284 (void) tsleep((caddr_t)&so->so_timeo, 0, 285 "nfscon", 2 * hz); 286 if ((so->so_state & SS_ISCONNECTING) && 287 so->so_error == 0 && rep && 288 (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0){ 289 so->so_state &= ~SS_ISCONNECTING; 290 crit_exit(); 291 goto bad; 292 } 293 } 294 if (so->so_error) { 295 error = so->so_error; 296 so->so_error = 0; 297 crit_exit(); 298 goto bad; 299 } 300 crit_exit(); 301 } 302 so->so_rcv.sb_timeo = (5 * hz); 303 so->so_snd.sb_timeo = (5 * hz); 304 305 /* 306 * Get buffer reservation size from sysctl, but impose reasonable 307 * limits. 308 */ 309 pktscale = nfs_bufpackets; 310 if (pktscale < 2) 311 pktscale = 2; 312 if (pktscale > 64) 313 pktscale = 64; 314 315 if (nmp->nm_sotype == SOCK_DGRAM) { 316 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale; 317 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) + 318 NFS_MAXPKTHDR) * pktscale; 319 } else if (nmp->nm_sotype == SOCK_SEQPACKET) { 320 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale; 321 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) + 322 NFS_MAXPKTHDR) * pktscale; 323 } else { 324 if (nmp->nm_sotype != SOCK_STREAM) 325 panic("nfscon sotype"); 326 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 327 struct sockopt sopt; 328 int val; 329 330 bzero(&sopt, sizeof sopt); 331 sopt.sopt_level = SOL_SOCKET; 332 sopt.sopt_name = SO_KEEPALIVE; 333 sopt.sopt_val = &val; 334 sopt.sopt_valsize = sizeof val; 335 val = 1; 336 sosetopt(so, &sopt); 337 } 338 if (so->so_proto->pr_protocol == IPPROTO_TCP) { 339 struct sockopt sopt; 340 int val; 341 342 bzero(&sopt, sizeof sopt); 343 sopt.sopt_level = IPPROTO_TCP; 344 sopt.sopt_name = TCP_NODELAY; 345 sopt.sopt_val = &val; 346 sopt.sopt_valsize = sizeof val; 347 val = 1; 348 sosetopt(so, &sopt); 349 } 350 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR + 351 sizeof (u_int32_t)) * pktscale; 352 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR + 353 sizeof (u_int32_t)) * pktscale; 354 } 355 error = soreserve(so, sndreserve, rcvreserve, 356 &td->td_proc->p_rlimit[RLIMIT_SBSIZE]); 357 if (error) 358 goto bad; 359 so->so_rcv.sb_flags |= SB_NOINTR; 360 so->so_snd.sb_flags |= SB_NOINTR; 361 362 /* Initialize other non-zero congestion variables */ 363 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] = 364 nmp->nm_srtt[3] = (NFS_TIMEO << 3); 365 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] = 366 nmp->nm_sdrtt[3] = 0; 367 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */ 368 nmp->nm_sent = 0; 369 nmp->nm_timeouts = 0; 370 return (0); 371 372 bad: 373 nfs_disconnect(nmp); 374 return (error); 375 } 376 377 /* 378 * Reconnect routine: 379 * Called when a connection is broken on a reliable protocol. 380 * - clean up the old socket 381 * - nfs_connect() again 382 * - set R_MUSTRESEND for all outstanding requests on mount point 383 * If this fails the mount point is DEAD! 384 * nb: Must be called with the nfs_sndlock() set on the mount point. 385 */ 386 static int 387 nfs_reconnect(struct nfsreq *rep) 388 { 389 struct nfsreq *rp; 390 struct nfsmount *nmp = rep->r_nmp; 391 int error; 392 393 nfs_disconnect(nmp); 394 while ((error = nfs_connect(nmp, rep)) != 0) { 395 if (error == EINTR || error == ERESTART) 396 return (EINTR); 397 (void) tsleep((caddr_t)&lbolt, 0, "nfscon", 0); 398 } 399 400 /* 401 * Loop through outstanding request list and fix up all requests 402 * on old socket. 403 */ 404 crit_enter(); 405 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) { 406 if (rp->r_nmp == nmp) 407 rp->r_flags |= R_MUSTRESEND; 408 } 409 crit_exit(); 410 return (0); 411 } 412 413 /* 414 * NFS disconnect. Clean up and unlink. 415 */ 416 void 417 nfs_disconnect(struct nfsmount *nmp) 418 { 419 struct socket *so; 420 421 if (nmp->nm_so) { 422 so = nmp->nm_so; 423 nmp->nm_so = (struct socket *)0; 424 soshutdown(so, 2); 425 soclose(so); 426 } 427 } 428 429 void 430 nfs_safedisconnect(struct nfsmount *nmp) 431 { 432 struct nfsreq dummyreq; 433 434 bzero(&dummyreq, sizeof(dummyreq)); 435 dummyreq.r_nmp = nmp; 436 dummyreq.r_td = NULL; 437 nfs_rcvlock(&dummyreq); 438 nfs_disconnect(nmp); 439 nfs_rcvunlock(&dummyreq); 440 } 441 442 /* 443 * This is the nfs send routine. For connection based socket types, it 444 * must be called with an nfs_sndlock() on the socket. 445 * "rep == NULL" indicates that it has been called from a server. 446 * For the client side: 447 * - return EINTR if the RPC is terminated, 0 otherwise 448 * - set R_MUSTRESEND if the send fails for any reason 449 * - do any cleanup required by recoverable socket errors (?) 450 * For the server side: 451 * - return EINTR or ERESTART if interrupted by a signal 452 * - return EPIPE if a connection is lost for connection based sockets (TCP...) 453 * - do any cleanup required by recoverable socket errors (?) 454 */ 455 int 456 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top, 457 struct nfsreq *rep) 458 { 459 struct sockaddr *sendnam; 460 int error, soflags, flags; 461 462 if (rep) { 463 if (rep->r_flags & R_SOFTTERM) { 464 m_freem(top); 465 return (EINTR); 466 } 467 if ((so = rep->r_nmp->nm_so) == NULL) { 468 rep->r_flags |= R_MUSTRESEND; 469 m_freem(top); 470 return (0); 471 } 472 rep->r_flags &= ~R_MUSTRESEND; 473 soflags = rep->r_nmp->nm_soflags; 474 } else 475 soflags = so->so_proto->pr_flags; 476 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED)) 477 sendnam = (struct sockaddr *)0; 478 else 479 sendnam = nam; 480 if (so->so_type == SOCK_SEQPACKET) 481 flags = MSG_EOR; 482 else 483 flags = 0; 484 485 error = so_pru_sosend(so, sendnam, NULL, top, NULL, flags, 486 curthread /*XXX*/); 487 /* 488 * ENOBUFS for dgram sockets is transient and non fatal. 489 * No need to log, and no need to break a soft mount. 490 */ 491 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) { 492 error = 0; 493 if (rep) /* do backoff retransmit on client */ 494 rep->r_flags |= R_MUSTRESEND; 495 } 496 497 if (error) { 498 if (rep) { 499 log(LOG_INFO, "nfs send error %d for server %s\n",error, 500 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 501 /* 502 * Deal with errors for the client side. 503 */ 504 if (rep->r_flags & R_SOFTTERM) 505 error = EINTR; 506 else 507 rep->r_flags |= R_MUSTRESEND; 508 } else 509 log(LOG_INFO, "nfsd send error %d\n", error); 510 511 /* 512 * Handle any recoverable (soft) socket errors here. (?) 513 */ 514 if (error != EINTR && error != ERESTART && 515 error != EWOULDBLOCK && error != EPIPE) 516 error = 0; 517 } 518 return (error); 519 } 520 521 /* 522 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all 523 * done by soreceive(), but for SOCK_STREAM we must deal with the Record 524 * Mark and consolidate the data into a new mbuf list. 525 * nb: Sometimes TCP passes the data up to soreceive() in long lists of 526 * small mbufs. 527 * For SOCK_STREAM we must be very careful to read an entire record once 528 * we have read any of it, even if the system call has been interrupted. 529 */ 530 static int 531 nfs_receive(struct nfsreq *rep, struct sockaddr **aname, struct mbuf **mp) 532 { 533 struct socket *so; 534 struct uio auio; 535 struct iovec aio; 536 struct mbuf *m; 537 struct mbuf *control; 538 u_int32_t len; 539 struct sockaddr **getnam; 540 int error, sotype, rcvflg; 541 struct thread *td = curthread; /* XXX */ 542 543 /* 544 * Set up arguments for soreceive() 545 */ 546 *mp = (struct mbuf *)0; 547 *aname = (struct sockaddr *)0; 548 sotype = rep->r_nmp->nm_sotype; 549 550 /* 551 * For reliable protocols, lock against other senders/receivers 552 * in case a reconnect is necessary. 553 * For SOCK_STREAM, first get the Record Mark to find out how much 554 * more there is to get. 555 * We must lock the socket against other receivers 556 * until we have an entire rpc request/reply. 557 */ 558 if (sotype != SOCK_DGRAM) { 559 error = nfs_sndlock(rep); 560 if (error) 561 return (error); 562 tryagain: 563 /* 564 * Check for fatal errors and resending request. 565 */ 566 /* 567 * Ugh: If a reconnect attempt just happened, nm_so 568 * would have changed. NULL indicates a failed 569 * attempt that has essentially shut down this 570 * mount point. 571 */ 572 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) { 573 nfs_sndunlock(rep); 574 return (EINTR); 575 } 576 so = rep->r_nmp->nm_so; 577 if (!so) { 578 error = nfs_reconnect(rep); 579 if (error) { 580 nfs_sndunlock(rep); 581 return (error); 582 } 583 goto tryagain; 584 } 585 while (rep->r_flags & R_MUSTRESEND) { 586 m = m_copym(rep->r_mreq, 0, M_COPYALL, MB_WAIT); 587 nfsstats.rpcretries++; 588 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep); 589 if (error) { 590 if (error == EINTR || error == ERESTART || 591 (error = nfs_reconnect(rep)) != 0) { 592 nfs_sndunlock(rep); 593 return (error); 594 } 595 goto tryagain; 596 } 597 } 598 nfs_sndunlock(rep); 599 if (sotype == SOCK_STREAM) { 600 aio.iov_base = (caddr_t) &len; 601 aio.iov_len = sizeof(u_int32_t); 602 auio.uio_iov = &aio; 603 auio.uio_iovcnt = 1; 604 auio.uio_segflg = UIO_SYSSPACE; 605 auio.uio_rw = UIO_READ; 606 auio.uio_offset = 0; 607 auio.uio_resid = sizeof(u_int32_t); 608 auio.uio_td = td; 609 do { 610 rcvflg = MSG_WAITALL; 611 error = so_pru_soreceive(so, NULL, &auio, NULL, 612 NULL, &rcvflg); 613 if (error == EWOULDBLOCK && rep) { 614 if (rep->r_flags & R_SOFTTERM) 615 return (EINTR); 616 } 617 } while (error == EWOULDBLOCK); 618 if (!error && auio.uio_resid > 0) { 619 /* 620 * Don't log a 0 byte receive; it means 621 * that the socket has been closed, and 622 * can happen during normal operation 623 * (forcible unmount or Solaris server). 624 */ 625 if (auio.uio_resid != sizeof (u_int32_t)) 626 log(LOG_INFO, 627 "short receive (%d/%d) from nfs server %s\n", 628 (int)(sizeof(u_int32_t) - auio.uio_resid), 629 (int)sizeof(u_int32_t), 630 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 631 error = EPIPE; 632 } 633 if (error) 634 goto errout; 635 len = ntohl(len) & ~0x80000000; 636 /* 637 * This is SERIOUS! We are out of sync with the sender 638 * and forcing a disconnect/reconnect is all I can do. 639 */ 640 if (len > NFS_MAXPACKET) { 641 log(LOG_ERR, "%s (%d) from nfs server %s\n", 642 "impossible packet length", 643 len, 644 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 645 error = EFBIG; 646 goto errout; 647 } 648 auio.uio_resid = len; 649 do { 650 rcvflg = MSG_WAITALL; 651 error = so_pru_soreceive(so, NULL, &auio, mp, 652 NULL, &rcvflg); 653 } while (error == EWOULDBLOCK || error == EINTR || 654 error == ERESTART); 655 if (!error && auio.uio_resid > 0) { 656 if (len != auio.uio_resid) 657 log(LOG_INFO, 658 "short receive (%d/%d) from nfs server %s\n", 659 len - auio.uio_resid, len, 660 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 661 error = EPIPE; 662 } 663 } else { 664 /* 665 * NB: Since uio_resid is big, MSG_WAITALL is ignored 666 * and soreceive() will return when it has either a 667 * control msg or a data msg. 668 * We have no use for control msg., but must grab them 669 * and then throw them away so we know what is going 670 * on. 671 */ 672 auio.uio_resid = len = 100000000; /* Anything Big */ 673 auio.uio_td = td; 674 do { 675 rcvflg = 0; 676 error = so_pru_soreceive(so, NULL, &auio, mp, 677 &control, &rcvflg); 678 if (control) 679 m_freem(control); 680 if (error == EWOULDBLOCK && rep) { 681 if (rep->r_flags & R_SOFTTERM) 682 return (EINTR); 683 } 684 } while (error == EWOULDBLOCK || 685 (!error && *mp == NULL && control)); 686 if ((rcvflg & MSG_EOR) == 0) 687 printf("Egad!!\n"); 688 if (!error && *mp == NULL) 689 error = EPIPE; 690 len -= auio.uio_resid; 691 } 692 errout: 693 if (error && error != EINTR && error != ERESTART) { 694 m_freem(*mp); 695 *mp = (struct mbuf *)0; 696 if (error != EPIPE) 697 log(LOG_INFO, 698 "receive error %d from nfs server %s\n", 699 error, 700 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname); 701 error = nfs_sndlock(rep); 702 if (!error) { 703 error = nfs_reconnect(rep); 704 if (!error) 705 goto tryagain; 706 else 707 nfs_sndunlock(rep); 708 } 709 } 710 } else { 711 if ((so = rep->r_nmp->nm_so) == NULL) 712 return (EACCES); 713 if (so->so_state & SS_ISCONNECTED) 714 getnam = (struct sockaddr **)0; 715 else 716 getnam = aname; 717 auio.uio_resid = len = 1000000; 718 auio.uio_td = td; 719 do { 720 rcvflg = 0; 721 error = so_pru_soreceive(so, getnam, &auio, mp, NULL, 722 &rcvflg); 723 if (error == EWOULDBLOCK && 724 (rep->r_flags & R_SOFTTERM)) 725 return (EINTR); 726 } while (error == EWOULDBLOCK); 727 len -= auio.uio_resid; 728 } 729 if (error) { 730 m_freem(*mp); 731 *mp = (struct mbuf *)0; 732 } 733 /* 734 * Search for any mbufs that are not a multiple of 4 bytes long 735 * or with m_data not longword aligned. 736 * These could cause pointer alignment problems, so copy them to 737 * well aligned mbufs. 738 */ 739 nfs_realign(mp, 5 * NFSX_UNSIGNED); 740 return (error); 741 } 742 743 /* 744 * Implement receipt of reply on a socket. 745 * We must search through the list of received datagrams matching them 746 * with outstanding requests using the xid, until ours is found. 747 */ 748 /* ARGSUSED */ 749 int 750 nfs_reply(struct nfsreq *myrep) 751 { 752 struct nfsreq *rep; 753 struct nfsmount *nmp = myrep->r_nmp; 754 int32_t t1; 755 struct mbuf *mrep, *md; 756 struct sockaddr *nam; 757 u_int32_t rxid, *tl; 758 caddr_t dpos, cp2; 759 int error; 760 761 /* 762 * Loop around until we get our own reply 763 */ 764 for (;;) { 765 /* 766 * Lock against other receivers so that I don't get stuck in 767 * sbwait() after someone else has received my reply for me. 768 * Also necessary for connection based protocols to avoid 769 * race conditions during a reconnect. 770 * If nfs_rcvlock() returns EALREADY, that means that 771 * the reply has already been recieved by another 772 * process and we can return immediately. In this 773 * case, the lock is not taken to avoid races with 774 * other processes. 775 */ 776 error = nfs_rcvlock(myrep); 777 if (error == EALREADY) 778 return (0); 779 if (error) 780 return (error); 781 /* 782 * Get the next Rpc reply off the socket 783 */ 784 error = nfs_receive(myrep, &nam, &mrep); 785 nfs_rcvunlock(myrep); 786 if (error) { 787 /* 788 * Ignore routing errors on connectionless protocols?? 789 */ 790 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) { 791 nmp->nm_so->so_error = 0; 792 if (myrep->r_flags & R_GETONEREP) 793 return (0); 794 continue; 795 } 796 return (error); 797 } 798 if (nam) 799 FREE(nam, M_SONAME); 800 801 /* 802 * Get the xid and check that it is an rpc reply 803 */ 804 md = mrep; 805 dpos = mtod(md, caddr_t); 806 nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED); 807 rxid = *tl++; 808 if (*tl != rpc_reply) { 809 #ifndef NFS_NOSERVER 810 if (nmp->nm_flag & NFSMNT_NQNFS) { 811 if (nqnfs_callback(nmp, mrep, md, dpos)) 812 nfsstats.rpcinvalid++; 813 } else { 814 nfsstats.rpcinvalid++; 815 m_freem(mrep); 816 } 817 #else 818 nfsstats.rpcinvalid++; 819 m_freem(mrep); 820 #endif 821 nfsmout: 822 if (myrep->r_flags & R_GETONEREP) 823 return (0); 824 continue; 825 } 826 827 /* 828 * Loop through the request list to match up the reply 829 * Iff no match, just drop the datagram. On match, set 830 * r_mrep atomically to prevent the timer from messing 831 * around with the request after we have exited the critical 832 * section. 833 */ 834 crit_enter(); 835 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) { 836 if (rep->r_mrep == NULL && rxid == rep->r_xid) { 837 rep->r_mrep = mrep; 838 break; 839 } 840 } 841 crit_exit(); 842 843 /* 844 * Fill in the rest of the reply if we found a match. 845 */ 846 if (rep) { 847 rep->r_md = md; 848 rep->r_dpos = dpos; 849 if (nfsrtton) { 850 struct rttl *rt; 851 852 rt = &nfsrtt.rttl[nfsrtt.pos]; 853 rt->proc = rep->r_procnum; 854 rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]); 855 rt->sent = nmp->nm_sent; 856 rt->cwnd = nmp->nm_cwnd; 857 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1]; 858 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1]; 859 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid; 860 getmicrotime(&rt->tstamp); 861 if (rep->r_flags & R_TIMING) 862 rt->rtt = rep->r_rtt; 863 else 864 rt->rtt = 1000000; 865 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ; 866 } 867 /* 868 * Update congestion window. 869 * Do the additive increase of 870 * one rpc/rtt. 871 */ 872 if (nmp->nm_cwnd <= nmp->nm_sent) { 873 nmp->nm_cwnd += 874 (NFS_CWNDSCALE * NFS_CWNDSCALE + 875 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd; 876 if (nmp->nm_cwnd > NFS_MAXCWND) 877 nmp->nm_cwnd = NFS_MAXCWND; 878 } 879 crit_enter(); /* nfs_timer interlock for nm_sent */ 880 if (rep->r_flags & R_SENT) { 881 rep->r_flags &= ~R_SENT; 882 nmp->nm_sent -= NFS_CWNDSCALE; 883 } 884 crit_exit(); 885 /* 886 * Update rtt using a gain of 0.125 on the mean 887 * and a gain of 0.25 on the deviation. 888 */ 889 if (rep->r_flags & R_TIMING) { 890 /* 891 * Since the timer resolution of 892 * NFS_HZ is so course, it can often 893 * result in r_rtt == 0. Since 894 * r_rtt == N means that the actual 895 * rtt is between N+dt and N+2-dt ticks, 896 * add 1. 897 */ 898 t1 = rep->r_rtt + 1; 899 t1 -= (NFS_SRTT(rep) >> 3); 900 NFS_SRTT(rep) += t1; 901 if (t1 < 0) 902 t1 = -t1; 903 t1 -= (NFS_SDRTT(rep) >> 2); 904 NFS_SDRTT(rep) += t1; 905 } 906 nmp->nm_timeouts = 0; 907 } 908 /* 909 * If not matched to a request, drop it. 910 * If it's mine, get out. 911 */ 912 if (rep == NULL) { 913 nfsstats.rpcunexpected++; 914 m_freem(mrep); 915 } else if (rep == myrep) { 916 if (rep->r_mrep == NULL) 917 panic("nfsreply nil"); 918 return (0); 919 } 920 if (myrep->r_flags & R_GETONEREP) 921 return (0); 922 } 923 } 924 925 /* 926 * nfs_request - goes something like this 927 * - fill in request struct 928 * - links it into list 929 * - calls nfs_send() for first transmit 930 * - calls nfs_receive() to get reply 931 * - break down rpc header and return with nfs reply pointed to 932 * by mrep or error 933 * nb: always frees up mreq mbuf list 934 */ 935 int 936 nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum, 937 struct thread *td, struct ucred *cred, struct mbuf **mrp, 938 struct mbuf **mdp, caddr_t *dposp) 939 { 940 struct mbuf *mrep, *m2; 941 struct nfsreq *rep; 942 u_int32_t *tl; 943 int i; 944 struct nfsmount *nmp; 945 struct mbuf *m, *md, *mheadend; 946 struct nfsnode *np; 947 char nickv[RPCX_NICKVERF]; 948 time_t reqtime, waituntil; 949 caddr_t dpos, cp2; 950 int t1, nqlflag, cachable, error = 0, mrest_len, auth_len, auth_type; 951 int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0, failed_auth = 0; 952 int verf_len, verf_type; 953 int retdummy; 954 u_int32_t xid; 955 u_quad_t frev; 956 char *auth_str, *verf_str; 957 NFSKERBKEY_T key; /* save session key */ 958 959 /* Reject requests while attempting a forced unmount. */ 960 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) { 961 m_freem(mrest); 962 return (ESTALE); 963 } 964 nmp = VFSTONFS(vp->v_mount); 965 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK); 966 rep->r_nmp = nmp; 967 rep->r_vp = vp; 968 rep->r_td = td; 969 rep->r_procnum = procnum; 970 rep->r_mreq = NULL; 971 i = 0; 972 m = mrest; 973 while (m) { 974 i += m->m_len; 975 m = m->m_next; 976 } 977 mrest_len = i; 978 979 /* 980 * Get the RPC header with authorization. 981 */ 982 kerbauth: 983 verf_str = auth_str = (char *)0; 984 if (nmp->nm_flag & NFSMNT_KERB) { 985 verf_str = nickv; 986 verf_len = sizeof (nickv); 987 auth_type = RPCAUTH_KERB4; 988 bzero((caddr_t)key, sizeof (key)); 989 if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str, 990 &auth_len, verf_str, verf_len)) { 991 error = nfs_getauth(nmp, rep, cred, &auth_str, 992 &auth_len, verf_str, &verf_len, key); 993 if (error) { 994 free((caddr_t)rep, M_NFSREQ); 995 m_freem(mrest); 996 return (error); 997 } 998 } 999 } else { 1000 auth_type = RPCAUTH_UNIX; 1001 if (cred->cr_ngroups < 1) 1002 panic("nfsreq nogrps"); 1003 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ? 1004 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) + 1005 5 * NFSX_UNSIGNED; 1006 } 1007 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len, 1008 auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid); 1009 if (auth_str) 1010 free(auth_str, M_TEMP); 1011 1012 /* 1013 * For stream protocols, insert a Sun RPC Record Mark. 1014 */ 1015 if (nmp->nm_sotype == SOCK_STREAM) { 1016 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT); 1017 if (m == NULL) { 1018 free(rep, M_NFSREQ); 1019 return (ENOBUFS); 1020 } 1021 *mtod(m, u_int32_t *) = htonl(0x80000000 | 1022 (m->m_pkthdr.len - NFSX_UNSIGNED)); 1023 } 1024 rep->r_mreq = m; 1025 rep->r_xid = xid; 1026 tryagain: 1027 if (nmp->nm_flag & NFSMNT_SOFT) 1028 rep->r_retry = nmp->nm_retry; 1029 else 1030 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */ 1031 rep->r_rtt = rep->r_rexmit = 0; 1032 if (proct[procnum] > 0) 1033 rep->r_flags = R_TIMING | R_MASKTIMER; 1034 else 1035 rep->r_flags = R_MASKTIMER; 1036 rep->r_mrep = NULL; 1037 1038 /* 1039 * Do the client side RPC. 1040 */ 1041 nfsstats.rpcrequests++; 1042 1043 /* 1044 * Chain request into list of outstanding requests. Be sure 1045 * to put it LAST so timer finds oldest requests first. Note 1046 * that R_MASKTIMER is set at the moment to prevent any timer 1047 * action on this request while we are still doing processing on 1048 * it below. splsoftclock() primarily protects nm_sent. Note 1049 * that we may block in this code so there is no atomicy guarentee. 1050 */ 1051 crit_enter(); 1052 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain); 1053 1054 /* Get send time for nqnfs */ 1055 reqtime = time_second; 1056 1057 /* 1058 * If backing off another request or avoiding congestion, don't 1059 * send this one now but let timer do it. If not timing a request, 1060 * do it now. 1061 * 1062 * Even though the timer will not mess with our request there is 1063 * still the possibility that we will race a reply (which clears 1064 * R_SENT), especially on localhost connections, so be very careful 1065 * when setting R_SENT. We could set R_SENT prior to calling 1066 * nfs_send() but why bother if the response occurs that quickly? 1067 */ 1068 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM || 1069 (nmp->nm_flag & NFSMNT_DUMBTIMR) || 1070 nmp->nm_sent < nmp->nm_cwnd)) { 1071 if (nmp->nm_soflags & PR_CONNREQUIRED) 1072 error = nfs_sndlock(rep); 1073 if (!error) { 1074 m2 = m_copym(m, 0, M_COPYALL, MB_WAIT); 1075 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep); 1076 if (nmp->nm_soflags & PR_CONNREQUIRED) 1077 nfs_sndunlock(rep); 1078 } 1079 if (!error && (rep->r_flags & R_MUSTRESEND) == 0 && 1080 rep->r_mrep == NULL) { 1081 KASSERT((rep->r_flags & R_SENT) == 0, 1082 ("R_SENT ASSERT %p", rep)); 1083 nmp->nm_sent += NFS_CWNDSCALE; 1084 rep->r_flags |= R_SENT; 1085 } 1086 } else { 1087 rep->r_rtt = -1; 1088 } 1089 1090 /* 1091 * Let the timer do what it will with the request, then 1092 * wait for the reply from our send or the timer's. 1093 */ 1094 if (!error || error == EPIPE) { 1095 rep->r_flags &= ~R_MASKTIMER; 1096 crit_exit(); 1097 error = nfs_reply(rep); 1098 crit_enter(); 1099 } 1100 1101 /* 1102 * RPC done, unlink the request, but don't rip it out from under 1103 * the callout timer. 1104 */ 1105 while (rep->r_flags & R_LOCKED) { 1106 nfs_timer_raced = 1; 1107 tsleep(&nfs_timer_raced, 0, "nfstrac", 0); 1108 } 1109 TAILQ_REMOVE(&nfs_reqq, rep, r_chain); 1110 1111 /* 1112 * Decrement the outstanding request count. 1113 */ 1114 if (rep->r_flags & R_SENT) { 1115 rep->r_flags &= ~R_SENT; 1116 nmp->nm_sent -= NFS_CWNDSCALE; 1117 } 1118 crit_exit(); 1119 1120 /* 1121 * If there was a successful reply and a tprintf msg. 1122 * tprintf a response. 1123 */ 1124 if (!error && (rep->r_flags & R_TPRINTFMSG)) 1125 nfs_msg(rep->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname, 1126 "is alive again"); 1127 mrep = rep->r_mrep; 1128 md = rep->r_md; 1129 dpos = rep->r_dpos; 1130 if (error) { 1131 m_freem(rep->r_mreq); 1132 free((caddr_t)rep, M_NFSREQ); 1133 return (error); 1134 } 1135 1136 /* 1137 * break down the rpc header and check if ok 1138 */ 1139 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1140 if (*tl++ == rpc_msgdenied) { 1141 if (*tl == rpc_mismatch) 1142 error = EOPNOTSUPP; 1143 else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) { 1144 if (!failed_auth) { 1145 failed_auth++; 1146 mheadend->m_next = (struct mbuf *)0; 1147 m_freem(mrep); 1148 m_freem(rep->r_mreq); 1149 goto kerbauth; 1150 } else 1151 error = EAUTH; 1152 } else 1153 error = EACCES; 1154 m_freem(mrep); 1155 m_freem(rep->r_mreq); 1156 free((caddr_t)rep, M_NFSREQ); 1157 return (error); 1158 } 1159 1160 /* 1161 * Grab any Kerberos verifier, otherwise just throw it away. 1162 */ 1163 verf_type = fxdr_unsigned(int, *tl++); 1164 i = fxdr_unsigned(int32_t, *tl); 1165 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) { 1166 error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep); 1167 if (error) 1168 goto nfsmout; 1169 } else if (i > 0) 1170 nfsm_adv(nfsm_rndup(i)); 1171 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1172 /* 0 == ok */ 1173 if (*tl == 0) { 1174 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1175 if (*tl != 0) { 1176 error = fxdr_unsigned(int, *tl); 1177 if ((nmp->nm_flag & NFSMNT_NFSV3) && 1178 error == NFSERR_TRYLATER) { 1179 m_freem(mrep); 1180 error = 0; 1181 waituntil = time_second + trylater_delay; 1182 while (time_second < waituntil) 1183 (void) tsleep((caddr_t)&lbolt, 1184 0, "nqnfstry", 0); 1185 trylater_delay *= nfs_backoff[trylater_cnt]; 1186 if (trylater_cnt < 7) 1187 trylater_cnt++; 1188 goto tryagain; 1189 } 1190 1191 /* 1192 * If the File Handle was stale, invalidate the 1193 * lookup cache, just in case. 1194 */ 1195 if (error == ESTALE) { 1196 retdummy = 0; 1197 cache_inval_vp(vp, CINV_CHILDREN, &retdummy); 1198 } 1199 if (nmp->nm_flag & NFSMNT_NFSV3) { 1200 *mrp = mrep; 1201 *mdp = md; 1202 *dposp = dpos; 1203 error |= NFSERR_RETERR; 1204 } else 1205 m_freem(mrep); 1206 m_freem(rep->r_mreq); 1207 free((caddr_t)rep, M_NFSREQ); 1208 return (error); 1209 } 1210 1211 /* 1212 * For nqnfs, get any lease in reply 1213 */ 1214 if (nmp->nm_flag & NFSMNT_NQNFS) { 1215 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1216 if (*tl) { 1217 np = VTONFS(vp); 1218 nqlflag = fxdr_unsigned(int, *tl); 1219 nfsm_dissect(tl, u_int32_t *, 4*NFSX_UNSIGNED); 1220 cachable = fxdr_unsigned(int, *tl++); 1221 reqtime += fxdr_unsigned(int, *tl++); 1222 if (reqtime > time_second) { 1223 frev = fxdr_hyper(tl); 1224 nqnfs_clientlease(nmp, np, nqlflag, 1225 cachable, reqtime, frev); 1226 } 1227 } 1228 } 1229 *mrp = mrep; 1230 *mdp = md; 1231 *dposp = dpos; 1232 m_freem(rep->r_mreq); 1233 FREE((caddr_t)rep, M_NFSREQ); 1234 return (0); 1235 } 1236 m_freem(mrep); 1237 error = EPROTONOSUPPORT; 1238 nfsmout: 1239 m_freem(rep->r_mreq); 1240 free((caddr_t)rep, M_NFSREQ); 1241 return (error); 1242 } 1243 1244 #ifndef NFS_NOSERVER 1245 /* 1246 * Generate the rpc reply header 1247 * siz arg. is used to decide if adding a cluster is worthwhile 1248 */ 1249 int 1250 nfs_rephead(int siz, struct nfsrv_descript *nd, struct nfssvc_sock *slp, 1251 int err, int cache, u_quad_t *frev, struct mbuf **mrq, 1252 struct mbuf **mbp, caddr_t *bposp) 1253 { 1254 u_int32_t *tl; 1255 struct mbuf *mreq; 1256 caddr_t bpos; 1257 struct mbuf *mb, *mb2; 1258 1259 siz += RPC_REPLYSIZ; 1260 mb = mreq = m_getl(max_hdr + siz, MB_WAIT, MT_DATA, M_PKTHDR, NULL); 1261 mreq->m_pkthdr.len = 0; 1262 /* 1263 * If this is not a cluster, try and leave leading space 1264 * for the lower level headers. 1265 */ 1266 if ((max_hdr + siz) < MINCLSIZE) 1267 mreq->m_data += max_hdr; 1268 tl = mtod(mreq, u_int32_t *); 1269 mreq->m_len = 6 * NFSX_UNSIGNED; 1270 bpos = ((caddr_t)tl) + mreq->m_len; 1271 *tl++ = txdr_unsigned(nd->nd_retxid); 1272 *tl++ = rpc_reply; 1273 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) { 1274 *tl++ = rpc_msgdenied; 1275 if (err & NFSERR_AUTHERR) { 1276 *tl++ = rpc_autherr; 1277 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR); 1278 mreq->m_len -= NFSX_UNSIGNED; 1279 bpos -= NFSX_UNSIGNED; 1280 } else { 1281 *tl++ = rpc_mismatch; 1282 *tl++ = txdr_unsigned(RPC_VER2); 1283 *tl = txdr_unsigned(RPC_VER2); 1284 } 1285 } else { 1286 *tl++ = rpc_msgaccepted; 1287 1288 /* 1289 * For Kerberos authentication, we must send the nickname 1290 * verifier back, otherwise just RPCAUTH_NULL. 1291 */ 1292 if (nd->nd_flag & ND_KERBFULL) { 1293 struct nfsuid *nuidp; 1294 struct timeval ktvin, ktvout; 1295 1296 for (nuidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first; 1297 nuidp != 0; nuidp = nuidp->nu_hash.le_next) { 1298 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid && 1299 (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp), 1300 &nuidp->nu_haddr, nd->nd_nam2))) 1301 break; 1302 } 1303 if (nuidp) { 1304 ktvin.tv_sec = 1305 txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1); 1306 ktvin.tv_usec = 1307 txdr_unsigned(nuidp->nu_timestamp.tv_usec); 1308 1309 /* 1310 * Encrypt the timestamp in ecb mode using the 1311 * session key. 1312 */ 1313 #ifdef NFSKERB 1314 XXX 1315 #endif 1316 1317 *tl++ = rpc_auth_kerb; 1318 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED); 1319 *tl = ktvout.tv_sec; 1320 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1321 *tl++ = ktvout.tv_usec; 1322 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid); 1323 } else { 1324 *tl++ = 0; 1325 *tl++ = 0; 1326 } 1327 } else { 1328 *tl++ = 0; 1329 *tl++ = 0; 1330 } 1331 switch (err) { 1332 case EPROGUNAVAIL: 1333 *tl = txdr_unsigned(RPC_PROGUNAVAIL); 1334 break; 1335 case EPROGMISMATCH: 1336 *tl = txdr_unsigned(RPC_PROGMISMATCH); 1337 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1338 if (nd->nd_flag & ND_NQNFS) { 1339 *tl++ = txdr_unsigned(3); 1340 *tl = txdr_unsigned(3); 1341 } else { 1342 *tl++ = txdr_unsigned(2); 1343 *tl = txdr_unsigned(3); 1344 } 1345 break; 1346 case EPROCUNAVAIL: 1347 *tl = txdr_unsigned(RPC_PROCUNAVAIL); 1348 break; 1349 case EBADRPC: 1350 *tl = txdr_unsigned(RPC_GARBAGE); 1351 break; 1352 default: 1353 *tl = 0; 1354 if (err != NFSERR_RETVOID) { 1355 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1356 if (err) 1357 *tl = txdr_unsigned(nfsrv_errmap(nd, err)); 1358 else 1359 *tl = 0; 1360 } 1361 break; 1362 }; 1363 } 1364 1365 /* 1366 * For nqnfs, piggyback lease as requested. 1367 */ 1368 if ((nd->nd_flag & ND_NQNFS) && err == 0) { 1369 if (nd->nd_flag & ND_LEASE) { 1370 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1371 *tl++ = txdr_unsigned(nd->nd_flag & ND_LEASE); 1372 *tl++ = txdr_unsigned(cache); 1373 *tl++ = txdr_unsigned(nd->nd_duration); 1374 txdr_hyper(*frev, tl); 1375 } else { 1376 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 1377 *tl = 0; 1378 } 1379 } 1380 if (mrq != NULL) 1381 *mrq = mreq; 1382 *mbp = mb; 1383 *bposp = bpos; 1384 if (err != 0 && err != NFSERR_RETVOID) 1385 nfsstats.srvrpc_errs++; 1386 return (0); 1387 } 1388 1389 1390 #endif /* NFS_NOSERVER */ 1391 /* 1392 * Nfs timer routine 1393 * Scan the nfsreq list and retranmit any requests that have timed out 1394 * To avoid retransmission attempts on STREAM sockets (in the future) make 1395 * sure to set the r_retry field to 0 (implies nm_retry == 0). 1396 */ 1397 void 1398 nfs_timer(void *arg /* never used */) 1399 { 1400 struct nfsreq *rep; 1401 struct mbuf *m; 1402 struct socket *so; 1403 struct nfsmount *nmp; 1404 int timeo; 1405 int error; 1406 #ifndef NFS_NOSERVER 1407 static long lasttime = 0; 1408 struct nfssvc_sock *slp; 1409 u_quad_t cur_usec; 1410 #endif /* NFS_NOSERVER */ 1411 struct thread *td = &thread0; /* XXX for credentials, will break if sleep */ 1412 1413 crit_enter(); 1414 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) { 1415 nmp = rep->r_nmp; 1416 if (rep->r_mrep || (rep->r_flags & (R_SOFTTERM|R_MASKTIMER))) 1417 continue; 1418 rep->r_flags |= R_LOCKED; 1419 if (nfs_sigintr(nmp, rep, rep->r_td)) { 1420 nfs_softterm(rep); 1421 goto skip; 1422 } 1423 if (rep->r_rtt >= 0) { 1424 rep->r_rtt++; 1425 if (nmp->nm_flag & NFSMNT_DUMBTIMR) 1426 timeo = nmp->nm_timeo; 1427 else 1428 timeo = NFS_RTO(nmp, proct[rep->r_procnum]); 1429 if (nmp->nm_timeouts > 0) 1430 timeo *= nfs_backoff[nmp->nm_timeouts - 1]; 1431 if (rep->r_rtt <= timeo) 1432 goto skip; 1433 if (nmp->nm_timeouts < 8) 1434 nmp->nm_timeouts++; 1435 } 1436 /* 1437 * Check for server not responding 1438 */ 1439 if ((rep->r_flags & R_TPRINTFMSG) == 0 && 1440 rep->r_rexmit > nmp->nm_deadthresh) { 1441 nfs_msg(rep->r_td, 1442 nmp->nm_mountp->mnt_stat.f_mntfromname, 1443 "not responding"); 1444 rep->r_flags |= R_TPRINTFMSG; 1445 } 1446 if (rep->r_rexmit >= rep->r_retry) { /* too many */ 1447 nfsstats.rpctimeouts++; 1448 nfs_softterm(rep); 1449 goto skip; 1450 } 1451 if (nmp->nm_sotype != SOCK_DGRAM) { 1452 if (++rep->r_rexmit > NFS_MAXREXMIT) 1453 rep->r_rexmit = NFS_MAXREXMIT; 1454 goto skip; 1455 } 1456 if ((so = nmp->nm_so) == NULL) 1457 goto skip; 1458 1459 /* 1460 * If there is enough space and the window allows.. 1461 * Resend it 1462 * Set r_rtt to -1 in case we fail to send it now. 1463 */ 1464 rep->r_rtt = -1; 1465 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len && 1466 ((nmp->nm_flag & NFSMNT_DUMBTIMR) || 1467 (rep->r_flags & R_SENT) || 1468 nmp->nm_sent < nmp->nm_cwnd) && 1469 (m = m_copym(rep->r_mreq, 0, M_COPYALL, MB_DONTWAIT))){ 1470 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0) 1471 error = so_pru_send(so, 0, m, (struct sockaddr *)0, 1472 (struct mbuf *)0, td); 1473 else 1474 error = so_pru_send(so, 0, m, nmp->nm_nam, 1475 (struct mbuf *)0, td); 1476 if (error) { 1477 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) 1478 so->so_error = 0; 1479 } else if (rep->r_mrep == NULL) { 1480 /* 1481 * Iff first send, start timing 1482 * else turn timing off, backoff timer 1483 * and divide congestion window by 2. 1484 * 1485 * It is possible for the so_pru_send() to 1486 * block and for us to race a reply so we 1487 * only do this if the reply field has not 1488 * been filled in. R_LOCKED will prevent 1489 * the request from being ripped out from under 1490 * us entirely. 1491 */ 1492 if (rep->r_flags & R_SENT) { 1493 rep->r_flags &= ~R_TIMING; 1494 if (++rep->r_rexmit > NFS_MAXREXMIT) 1495 rep->r_rexmit = NFS_MAXREXMIT; 1496 nmp->nm_cwnd >>= 1; 1497 if (nmp->nm_cwnd < NFS_CWNDSCALE) 1498 nmp->nm_cwnd = NFS_CWNDSCALE; 1499 nfsstats.rpcretries++; 1500 } else { 1501 rep->r_flags |= R_SENT; 1502 nmp->nm_sent += NFS_CWNDSCALE; 1503 } 1504 rep->r_rtt = 0; 1505 } 1506 } 1507 skip: 1508 rep->r_flags &= ~R_LOCKED; 1509 } 1510 #ifndef NFS_NOSERVER 1511 /* 1512 * Call the nqnfs server timer once a second to handle leases. 1513 */ 1514 if (lasttime != time_second) { 1515 lasttime = time_second; 1516 nqnfs_serverd(); 1517 } 1518 1519 /* 1520 * Scan the write gathering queues for writes that need to be 1521 * completed now. 1522 */ 1523 cur_usec = nfs_curusec(); 1524 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) { 1525 if (slp->ns_tq.lh_first && slp->ns_tq.lh_first->nd_time<=cur_usec) 1526 nfsrv_wakenfsd(slp, 1); 1527 } 1528 #endif /* NFS_NOSERVER */ 1529 1530 /* 1531 * Due to possible blocking, a client operation may be waiting for 1532 * us to finish processing this request so it can remove it. 1533 */ 1534 if (nfs_timer_raced) { 1535 nfs_timer_raced = 0; 1536 wakeup(&nfs_timer_raced); 1537 } 1538 crit_exit(); 1539 callout_reset(&nfs_timer_handle, nfs_ticks, nfs_timer, NULL); 1540 } 1541 1542 /* 1543 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and 1544 * wait for all requests to complete. This is used by forced unmounts 1545 * to terminate any outstanding RPCs. 1546 */ 1547 int 1548 nfs_nmcancelreqs(struct nfsmount *nmp) 1549 { 1550 struct nfsreq *req; 1551 int i; 1552 1553 crit_enter(); 1554 TAILQ_FOREACH(req, &nfs_reqq, r_chain) { 1555 if (nmp != req->r_nmp || req->r_mrep != NULL || 1556 (req->r_flags & R_SOFTTERM)) { 1557 continue; 1558 } 1559 nfs_softterm(req); 1560 } 1561 crit_exit(); 1562 1563 for (i = 0; i < 30; i++) { 1564 crit_enter(); 1565 TAILQ_FOREACH(req, &nfs_reqq, r_chain) { 1566 if (nmp == req->r_nmp) 1567 break; 1568 } 1569 crit_exit(); 1570 if (req == NULL) 1571 return (0); 1572 tsleep(&lbolt, 0, "nfscancel", 0); 1573 } 1574 return (EBUSY); 1575 } 1576 1577 /* 1578 * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT). 1579 * The nm_send count is decremented now to avoid deadlocks when the process in 1580 * soreceive() hasn't yet managed to send its own request. 1581 * 1582 * This routine must be called at splsoftclock() to protect r_flags and 1583 * nm_sent. 1584 */ 1585 1586 static void 1587 nfs_softterm(struct nfsreq *rep) 1588 { 1589 rep->r_flags |= R_SOFTTERM; 1590 1591 if (rep->r_flags & R_SENT) { 1592 rep->r_nmp->nm_sent -= NFS_CWNDSCALE; 1593 rep->r_flags &= ~R_SENT; 1594 } 1595 } 1596 1597 /* 1598 * Test for a termination condition pending on the process. 1599 * This is used for NFSMNT_INT mounts. 1600 */ 1601 int 1602 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td) 1603 { 1604 sigset_t tmpset; 1605 struct proc *p; 1606 1607 if (rep && (rep->r_flags & R_SOFTTERM)) 1608 return (EINTR); 1609 /* Terminate all requests while attempting a forced unmount. */ 1610 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) 1611 return (EINTR); 1612 if (!(nmp->nm_flag & NFSMNT_INT)) 1613 return (0); 1614 /* td might be NULL YYY */ 1615 if (td == NULL || (p = td->td_proc) == NULL) 1616 return (0); 1617 1618 tmpset = p->p_siglist; 1619 SIGSETNAND(tmpset, p->p_sigmask); 1620 SIGSETNAND(tmpset, p->p_sigignore); 1621 if (SIGNOTEMPTY(p->p_siglist) && NFSINT_SIGMASK(tmpset)) 1622 return (EINTR); 1623 1624 return (0); 1625 } 1626 1627 /* 1628 * Lock a socket against others. 1629 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply 1630 * and also to avoid race conditions between the processes with nfs requests 1631 * in progress when a reconnect is necessary. 1632 */ 1633 int 1634 nfs_sndlock(struct nfsreq *rep) 1635 { 1636 int *statep = &rep->r_nmp->nm_state; 1637 struct thread *td; 1638 int slptimeo; 1639 int slpflag; 1640 int error; 1641 1642 slpflag = 0; 1643 slptimeo = 0; 1644 td = rep->r_td; 1645 if (rep->r_nmp->nm_flag & NFSMNT_INT) 1646 slpflag = PCATCH; 1647 1648 error = 0; 1649 crit_enter(); 1650 while (*statep & NFSSTA_SNDLOCK) { 1651 *statep |= NFSSTA_WANTSND; 1652 if (nfs_sigintr(rep->r_nmp, rep, td)) { 1653 error = EINTR; 1654 break; 1655 } 1656 tsleep((caddr_t)statep, slpflag, "nfsndlck", slptimeo); 1657 if (slpflag == PCATCH) { 1658 slpflag = 0; 1659 slptimeo = 2 * hz; 1660 } 1661 } 1662 /* Always fail if our request has been cancelled. */ 1663 if ((rep->r_flags & R_SOFTTERM)) 1664 error = EINTR; 1665 if (error == 0) 1666 *statep |= NFSSTA_SNDLOCK; 1667 crit_exit(); 1668 return (error); 1669 } 1670 1671 /* 1672 * Unlock the stream socket for others. 1673 */ 1674 void 1675 nfs_sndunlock(struct nfsreq *rep) 1676 { 1677 int *statep = &rep->r_nmp->nm_state; 1678 1679 if ((*statep & NFSSTA_SNDLOCK) == 0) 1680 panic("nfs sndunlock"); 1681 crit_enter(); 1682 *statep &= ~NFSSTA_SNDLOCK; 1683 if (*statep & NFSSTA_WANTSND) { 1684 *statep &= ~NFSSTA_WANTSND; 1685 wakeup((caddr_t)statep); 1686 } 1687 crit_exit(); 1688 } 1689 1690 static int 1691 nfs_rcvlock(struct nfsreq *rep) 1692 { 1693 int *statep = &rep->r_nmp->nm_state; 1694 int slpflag; 1695 int slptimeo; 1696 int error; 1697 1698 /* 1699 * Unconditionally check for completion in case another nfsiod 1700 * get the packet while the caller was blocked, before the caller 1701 * called us. Packet reception is handled by mainline code which 1702 * is protected by the BGL at the moment. 1703 * 1704 * We do not strictly need the second check just before the 1705 * tsleep(), but it's good defensive programming. 1706 */ 1707 if (rep->r_mrep != NULL) 1708 return (EALREADY); 1709 1710 if (rep->r_nmp->nm_flag & NFSMNT_INT) 1711 slpflag = PCATCH; 1712 else 1713 slpflag = 0; 1714 slptimeo = 0; 1715 error = 0; 1716 crit_enter(); 1717 while (*statep & NFSSTA_RCVLOCK) { 1718 if (nfs_sigintr(rep->r_nmp, rep, rep->r_td)) { 1719 error = EINTR; 1720 break; 1721 } 1722 if (rep->r_mrep != NULL) { 1723 error = EALREADY; 1724 break; 1725 } 1726 *statep |= NFSSTA_WANTRCV; 1727 tsleep((caddr_t)statep, slpflag, "nfsrcvlk", slptimeo); 1728 /* 1729 * If our reply was recieved while we were sleeping, 1730 * then just return without taking the lock to avoid a 1731 * situation where a single iod could 'capture' the 1732 * recieve lock. 1733 */ 1734 if (rep->r_mrep != NULL) { 1735 error = EALREADY; 1736 break; 1737 } 1738 if (slpflag == PCATCH) { 1739 slpflag = 0; 1740 slptimeo = 2 * hz; 1741 } 1742 } 1743 if (error == 0) { 1744 *statep |= NFSSTA_RCVLOCK; 1745 rep->r_nmp->nm_rcvlock_td = curthread; /* DEBUGGING */ 1746 } 1747 crit_exit(); 1748 return (error); 1749 } 1750 1751 /* 1752 * Unlock the stream socket for others. 1753 */ 1754 static void 1755 nfs_rcvunlock(struct nfsreq *rep) 1756 { 1757 int *statep = &rep->r_nmp->nm_state; 1758 1759 if ((*statep & NFSSTA_RCVLOCK) == 0) 1760 panic("nfs rcvunlock"); 1761 crit_enter(); 1762 rep->r_nmp->nm_rcvlock_td = (void *)-1; /* DEBUGGING */ 1763 *statep &= ~NFSSTA_RCVLOCK; 1764 if (*statep & NFSSTA_WANTRCV) { 1765 *statep &= ~NFSSTA_WANTRCV; 1766 wakeup((caddr_t)statep); 1767 } 1768 crit_exit(); 1769 } 1770 1771 /* 1772 * nfs_realign: 1773 * 1774 * Check for badly aligned mbuf data and realign by copying the unaligned 1775 * portion of the data into a new mbuf chain and freeing the portions 1776 * of the old chain that were replaced. 1777 * 1778 * We cannot simply realign the data within the existing mbuf chain 1779 * because the underlying buffers may contain other rpc commands and 1780 * we cannot afford to overwrite them. 1781 * 1782 * We would prefer to avoid this situation entirely. The situation does 1783 * not occur with NFS/UDP and is supposed to only occassionally occur 1784 * with TCP. Use vfs.nfs.realign_count and realign_test to check this. 1785 */ 1786 static void 1787 nfs_realign(struct mbuf **pm, int hsiz) 1788 { 1789 struct mbuf *m; 1790 struct mbuf *n = NULL; 1791 int off = 0; 1792 1793 ++nfs_realign_test; 1794 1795 while ((m = *pm) != NULL) { 1796 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) { 1797 n = m_getl(m->m_len, MB_WAIT, MT_DATA, 0, NULL); 1798 n->m_len = 0; 1799 break; 1800 } 1801 pm = &m->m_next; 1802 } 1803 1804 /* 1805 * If n is non-NULL, loop on m copying data, then replace the 1806 * portion of the chain that had to be realigned. 1807 */ 1808 if (n != NULL) { 1809 ++nfs_realign_count; 1810 while (m) { 1811 m_copyback(n, off, m->m_len, mtod(m, caddr_t)); 1812 off += m->m_len; 1813 m = m->m_next; 1814 } 1815 m_freem(*pm); 1816 *pm = n; 1817 } 1818 } 1819 1820 #ifndef NFS_NOSERVER 1821 1822 /* 1823 * Parse an RPC request 1824 * - verify it 1825 * - fill in the cred struct. 1826 */ 1827 int 1828 nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header) 1829 { 1830 int len, i; 1831 u_int32_t *tl; 1832 int32_t t1; 1833 struct uio uio; 1834 struct iovec iov; 1835 caddr_t dpos, cp2, cp; 1836 u_int32_t nfsvers, auth_type; 1837 uid_t nickuid; 1838 int error = 0, nqnfs = 0, ticklen; 1839 struct mbuf *mrep, *md; 1840 struct nfsuid *nuidp; 1841 struct timeval tvin, tvout; 1842 #if 0 /* until encrypted keys are implemented */ 1843 NFSKERBKEYSCHED_T keys; /* stores key schedule */ 1844 #endif 1845 1846 mrep = nd->nd_mrep; 1847 md = nd->nd_md; 1848 dpos = nd->nd_dpos; 1849 if (has_header) { 1850 nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED); 1851 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++); 1852 if (*tl++ != rpc_call) { 1853 m_freem(mrep); 1854 return (EBADRPC); 1855 } 1856 } else 1857 nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 1858 nd->nd_repstat = 0; 1859 nd->nd_flag = 0; 1860 if (*tl++ != rpc_vers) { 1861 nd->nd_repstat = ERPCMISMATCH; 1862 nd->nd_procnum = NFSPROC_NOOP; 1863 return (0); 1864 } 1865 if (*tl != nfs_prog) { 1866 if (*tl == nqnfs_prog) 1867 nqnfs++; 1868 else { 1869 nd->nd_repstat = EPROGUNAVAIL; 1870 nd->nd_procnum = NFSPROC_NOOP; 1871 return (0); 1872 } 1873 } 1874 tl++; 1875 nfsvers = fxdr_unsigned(u_int32_t, *tl++); 1876 if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) || 1877 (nfsvers != NQNFS_VER3 && nqnfs)) { 1878 nd->nd_repstat = EPROGMISMATCH; 1879 nd->nd_procnum = NFSPROC_NOOP; 1880 return (0); 1881 } 1882 if (nqnfs) 1883 nd->nd_flag = (ND_NFSV3 | ND_NQNFS); 1884 else if (nfsvers == NFS_VER3) 1885 nd->nd_flag = ND_NFSV3; 1886 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++); 1887 if (nd->nd_procnum == NFSPROC_NULL) 1888 return (0); 1889 if (nd->nd_procnum >= NFS_NPROCS || 1890 (!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) || 1891 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) { 1892 nd->nd_repstat = EPROCUNAVAIL; 1893 nd->nd_procnum = NFSPROC_NOOP; 1894 return (0); 1895 } 1896 if ((nd->nd_flag & ND_NFSV3) == 0) 1897 nd->nd_procnum = nfsv3_procid[nd->nd_procnum]; 1898 auth_type = *tl++; 1899 len = fxdr_unsigned(int, *tl++); 1900 if (len < 0 || len > RPCAUTH_MAXSIZ) { 1901 m_freem(mrep); 1902 return (EBADRPC); 1903 } 1904 1905 nd->nd_flag &= ~ND_KERBAUTH; 1906 /* 1907 * Handle auth_unix or auth_kerb. 1908 */ 1909 if (auth_type == rpc_auth_unix) { 1910 len = fxdr_unsigned(int, *++tl); 1911 if (len < 0 || len > NFS_MAXNAMLEN) { 1912 m_freem(mrep); 1913 return (EBADRPC); 1914 } 1915 nfsm_adv(nfsm_rndup(len)); 1916 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1917 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred)); 1918 nd->nd_cr.cr_ref = 1; 1919 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++); 1920 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++); 1921 len = fxdr_unsigned(int, *tl); 1922 if (len < 0 || len > RPCAUTH_UNIXGIDS) { 1923 m_freem(mrep); 1924 return (EBADRPC); 1925 } 1926 nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED); 1927 for (i = 1; i <= len; i++) 1928 if (i < NGROUPS) 1929 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++); 1930 else 1931 tl++; 1932 nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1); 1933 if (nd->nd_cr.cr_ngroups > 1) 1934 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups); 1935 len = fxdr_unsigned(int, *++tl); 1936 if (len < 0 || len > RPCAUTH_MAXSIZ) { 1937 m_freem(mrep); 1938 return (EBADRPC); 1939 } 1940 if (len > 0) 1941 nfsm_adv(nfsm_rndup(len)); 1942 } else if (auth_type == rpc_auth_kerb) { 1943 switch (fxdr_unsigned(int, *tl++)) { 1944 case RPCAKN_FULLNAME: 1945 ticklen = fxdr_unsigned(int, *tl); 1946 *((u_int32_t *)nfsd->nfsd_authstr) = *tl; 1947 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED; 1948 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED; 1949 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) { 1950 m_freem(mrep); 1951 return (EBADRPC); 1952 } 1953 uio.uio_offset = 0; 1954 uio.uio_iov = &iov; 1955 uio.uio_iovcnt = 1; 1956 uio.uio_segflg = UIO_SYSSPACE; 1957 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4]; 1958 iov.iov_len = RPCAUTH_MAXSIZ - 4; 1959 nfsm_mtouio(&uio, uio.uio_resid); 1960 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1961 if (*tl++ != rpc_auth_kerb || 1962 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) { 1963 printf("Bad kerb verifier\n"); 1964 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 1965 nd->nd_procnum = NFSPROC_NOOP; 1966 return (0); 1967 } 1968 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED); 1969 tl = (u_int32_t *)cp; 1970 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) { 1971 printf("Not fullname kerb verifier\n"); 1972 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 1973 nd->nd_procnum = NFSPROC_NOOP; 1974 return (0); 1975 } 1976 cp += NFSX_UNSIGNED; 1977 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED); 1978 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED; 1979 nd->nd_flag |= ND_KERBFULL; 1980 nfsd->nfsd_flag |= NFSD_NEEDAUTH; 1981 break; 1982 case RPCAKN_NICKNAME: 1983 if (len != 2 * NFSX_UNSIGNED) { 1984 printf("Kerb nickname short\n"); 1985 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED); 1986 nd->nd_procnum = NFSPROC_NOOP; 1987 return (0); 1988 } 1989 nickuid = fxdr_unsigned(uid_t, *tl); 1990 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1991 if (*tl++ != rpc_auth_kerb || 1992 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) { 1993 printf("Kerb nick verifier bad\n"); 1994 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF); 1995 nd->nd_procnum = NFSPROC_NOOP; 1996 return (0); 1997 } 1998 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1999 tvin.tv_sec = *tl++; 2000 tvin.tv_usec = *tl; 2001 2002 for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first; 2003 nuidp != 0; nuidp = nuidp->nu_hash.le_next) { 2004 if (nuidp->nu_cr.cr_uid == nickuid && 2005 (!nd->nd_nam2 || 2006 netaddr_match(NU_NETFAM(nuidp), 2007 &nuidp->nu_haddr, nd->nd_nam2))) 2008 break; 2009 } 2010 if (!nuidp) { 2011 nd->nd_repstat = 2012 (NFSERR_AUTHERR|AUTH_REJECTCRED); 2013 nd->nd_procnum = NFSPROC_NOOP; 2014 return (0); 2015 } 2016 2017 /* 2018 * Now, decrypt the timestamp using the session key 2019 * and validate it. 2020 */ 2021 #ifdef NFSKERB 2022 XXX 2023 #endif 2024 2025 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec); 2026 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec); 2027 if (nuidp->nu_expire < time_second || 2028 nuidp->nu_timestamp.tv_sec > tvout.tv_sec || 2029 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec && 2030 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) { 2031 nuidp->nu_expire = 0; 2032 nd->nd_repstat = 2033 (NFSERR_AUTHERR|AUTH_REJECTVERF); 2034 nd->nd_procnum = NFSPROC_NOOP; 2035 return (0); 2036 } 2037 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr); 2038 nd->nd_flag |= ND_KERBNICK; 2039 }; 2040 } else { 2041 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED); 2042 nd->nd_procnum = NFSPROC_NOOP; 2043 return (0); 2044 } 2045 2046 /* 2047 * For nqnfs, get piggybacked lease request. 2048 */ 2049 if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) { 2050 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2051 nd->nd_flag |= fxdr_unsigned(int, *tl); 2052 if (nd->nd_flag & ND_LEASE) { 2053 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2054 nd->nd_duration = fxdr_unsigned(int32_t, *tl); 2055 } else 2056 nd->nd_duration = NQ_MINLEASE; 2057 } else 2058 nd->nd_duration = NQ_MINLEASE; 2059 nd->nd_md = md; 2060 nd->nd_dpos = dpos; 2061 return (0); 2062 nfsmout: 2063 return (error); 2064 } 2065 2066 #endif 2067 2068 /* 2069 * Send a message to the originating process's terminal. The thread and/or 2070 * process may be NULL. YYY the thread should not be NULL but there may 2071 * still be some uio_td's that are still being passed as NULL through to 2072 * nfsm_request(). 2073 */ 2074 static int 2075 nfs_msg(struct thread *td, char *server, char *msg) 2076 { 2077 tpr_t tpr; 2078 2079 if (td && td->td_proc) 2080 tpr = tprintf_open(td->td_proc); 2081 else 2082 tpr = NULL; 2083 tprintf(tpr, "nfs server %s: %s\n", server, msg); 2084 tprintf_close(tpr); 2085 return (0); 2086 } 2087 2088 #ifndef NFS_NOSERVER 2089 /* 2090 * Socket upcall routine for the nfsd sockets. 2091 * The caddr_t arg is a pointer to the "struct nfssvc_sock". 2092 * Essentially do as much as possible non-blocking, else punt and it will 2093 * be called with MB_WAIT from an nfsd. 2094 */ 2095 void 2096 nfsrv_rcv(struct socket *so, void *arg, int waitflag) 2097 { 2098 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg; 2099 struct mbuf *m; 2100 struct mbuf *mp; 2101 struct sockaddr *nam; 2102 struct uio auio; 2103 int flags, error; 2104 int nparallel_wakeup = 0; 2105 2106 if ((slp->ns_flag & SLP_VALID) == 0) 2107 return; 2108 2109 /* 2110 * Do not allow an infinite number of completed RPC records to build 2111 * up before we stop reading data from the socket. Otherwise we could 2112 * end up holding onto an unreasonable number of mbufs for requests 2113 * waiting for service. 2114 * 2115 * This should give pretty good feedback to the TCP 2116 * layer and prevents a memory crunch for other protocols. 2117 * 2118 * Note that the same service socket can be dispatched to several 2119 * nfs servers simultaniously. 2120 * 2121 * the tcp protocol callback calls us with MB_DONTWAIT. 2122 * nfsd calls us with MB_WAIT (typically). 2123 */ 2124 if (waitflag == MB_DONTWAIT && slp->ns_numrec >= nfsd_waiting / 2 + 1) { 2125 slp->ns_flag |= SLP_NEEDQ; 2126 goto dorecs; 2127 } 2128 2129 /* 2130 * Handle protocol specifics to parse an RPC request. We always 2131 * pull from the socket using non-blocking I/O. 2132 */ 2133 auio.uio_td = NULL; 2134 if (so->so_type == SOCK_STREAM) { 2135 /* 2136 * The data has to be read in an orderly fashion from a TCP 2137 * stream, unlike a UDP socket. It is possible for soreceive 2138 * and/or nfsrv_getstream() to block, so make sure only one 2139 * entity is messing around with the TCP stream at any given 2140 * moment. The receive sockbuf's lock in soreceive is not 2141 * sufficient. 2142 * 2143 * Note that this procedure can be called from any number of 2144 * NFS severs *OR* can be upcalled directly from a TCP 2145 * protocol thread. 2146 */ 2147 if (slp->ns_flag & SLP_GETSTREAM) { 2148 slp->ns_flag |= SLP_NEEDQ; 2149 goto dorecs; 2150 } 2151 slp->ns_flag |= SLP_GETSTREAM; 2152 2153 /* 2154 * Do soreceive(). 2155 */ 2156 auio.uio_resid = 1000000000; 2157 flags = MSG_DONTWAIT; 2158 error = so_pru_soreceive(so, &nam, &auio, &mp, NULL, &flags); 2159 if (error || mp == (struct mbuf *)0) { 2160 if (error == EWOULDBLOCK) 2161 slp->ns_flag |= SLP_NEEDQ; 2162 else 2163 slp->ns_flag |= SLP_DISCONN; 2164 slp->ns_flag &= ~SLP_GETSTREAM; 2165 goto dorecs; 2166 } 2167 m = mp; 2168 if (slp->ns_rawend) { 2169 slp->ns_rawend->m_next = m; 2170 slp->ns_cc += 1000000000 - auio.uio_resid; 2171 } else { 2172 slp->ns_raw = m; 2173 slp->ns_cc = 1000000000 - auio.uio_resid; 2174 } 2175 while (m->m_next) 2176 m = m->m_next; 2177 slp->ns_rawend = m; 2178 2179 /* 2180 * Now try and parse as many record(s) as we can out of the 2181 * raw stream data. 2182 */ 2183 error = nfsrv_getstream(slp, waitflag, &nparallel_wakeup); 2184 if (error) { 2185 if (error == EPERM) 2186 slp->ns_flag |= SLP_DISCONN; 2187 else 2188 slp->ns_flag |= SLP_NEEDQ; 2189 } 2190 slp->ns_flag &= ~SLP_GETSTREAM; 2191 } else { 2192 /* 2193 * For UDP soreceive typically pulls just one packet, loop 2194 * to get the whole batch. 2195 */ 2196 do { 2197 auio.uio_resid = 1000000000; 2198 flags = MSG_DONTWAIT; 2199 error = so_pru_soreceive(so, &nam, &auio, &mp, NULL, 2200 &flags); 2201 if (mp) { 2202 struct nfsrv_rec *rec; 2203 int mf = (waitflag & MB_DONTWAIT) ? 2204 M_NOWAIT : M_WAITOK; 2205 rec = malloc(sizeof(struct nfsrv_rec), 2206 M_NFSRVDESC, mf); 2207 if (!rec) { 2208 if (nam) 2209 FREE(nam, M_SONAME); 2210 m_freem(mp); 2211 continue; 2212 } 2213 nfs_realign(&mp, 10 * NFSX_UNSIGNED); 2214 rec->nr_address = nam; 2215 rec->nr_packet = mp; 2216 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link); 2217 ++slp->ns_numrec; 2218 ++nparallel_wakeup; 2219 } 2220 if (error) { 2221 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) 2222 && error != EWOULDBLOCK) { 2223 slp->ns_flag |= SLP_DISCONN; 2224 goto dorecs; 2225 } 2226 } 2227 } while (mp); 2228 } 2229 2230 /* 2231 * If we were upcalled from the tcp protocol layer and we have 2232 * fully parsed records ready to go, or there is new data pending, 2233 * or something went wrong, try to wake up an nfsd thread to deal 2234 * with it. 2235 */ 2236 dorecs: 2237 if (waitflag == MB_DONTWAIT && (slp->ns_numrec > 0 2238 || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN)))) { 2239 nfsrv_wakenfsd(slp, nparallel_wakeup); 2240 } 2241 } 2242 2243 /* 2244 * Try and extract an RPC request from the mbuf data list received on a 2245 * stream socket. The "waitflag" argument indicates whether or not it 2246 * can sleep. 2247 */ 2248 static int 2249 nfsrv_getstream(struct nfssvc_sock *slp, int waitflag, int *countp) 2250 { 2251 struct mbuf *m, **mpp; 2252 char *cp1, *cp2; 2253 int len; 2254 struct mbuf *om, *m2, *recm; 2255 u_int32_t recmark; 2256 2257 for (;;) { 2258 if (slp->ns_reclen == 0) { 2259 if (slp->ns_cc < NFSX_UNSIGNED) 2260 return (0); 2261 m = slp->ns_raw; 2262 if (m->m_len >= NFSX_UNSIGNED) { 2263 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED); 2264 m->m_data += NFSX_UNSIGNED; 2265 m->m_len -= NFSX_UNSIGNED; 2266 } else { 2267 cp1 = (caddr_t)&recmark; 2268 cp2 = mtod(m, caddr_t); 2269 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) { 2270 while (m->m_len == 0) { 2271 m = m->m_next; 2272 cp2 = mtod(m, caddr_t); 2273 } 2274 *cp1++ = *cp2++; 2275 m->m_data++; 2276 m->m_len--; 2277 } 2278 } 2279 slp->ns_cc -= NFSX_UNSIGNED; 2280 recmark = ntohl(recmark); 2281 slp->ns_reclen = recmark & ~0x80000000; 2282 if (recmark & 0x80000000) 2283 slp->ns_flag |= SLP_LASTFRAG; 2284 else 2285 slp->ns_flag &= ~SLP_LASTFRAG; 2286 if (slp->ns_reclen > NFS_MAXPACKET || slp->ns_reclen <= 0) { 2287 log(LOG_ERR, "%s (%d) from nfs client\n", 2288 "impossible packet length", 2289 slp->ns_reclen); 2290 return (EPERM); 2291 } 2292 } 2293 2294 /* 2295 * Now get the record part. 2296 * 2297 * Note that slp->ns_reclen may be 0. Linux sometimes 2298 * generates 0-length RPCs 2299 */ 2300 recm = NULL; 2301 if (slp->ns_cc == slp->ns_reclen) { 2302 recm = slp->ns_raw; 2303 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0; 2304 slp->ns_cc = slp->ns_reclen = 0; 2305 } else if (slp->ns_cc > slp->ns_reclen) { 2306 len = 0; 2307 m = slp->ns_raw; 2308 om = (struct mbuf *)0; 2309 2310 while (len < slp->ns_reclen) { 2311 if ((len + m->m_len) > slp->ns_reclen) { 2312 m2 = m_copym(m, 0, slp->ns_reclen - len, 2313 waitflag); 2314 if (m2) { 2315 if (om) { 2316 om->m_next = m2; 2317 recm = slp->ns_raw; 2318 } else 2319 recm = m2; 2320 m->m_data += slp->ns_reclen - len; 2321 m->m_len -= slp->ns_reclen - len; 2322 len = slp->ns_reclen; 2323 } else { 2324 return (EWOULDBLOCK); 2325 } 2326 } else if ((len + m->m_len) == slp->ns_reclen) { 2327 om = m; 2328 len += m->m_len; 2329 m = m->m_next; 2330 recm = slp->ns_raw; 2331 om->m_next = (struct mbuf *)0; 2332 } else { 2333 om = m; 2334 len += m->m_len; 2335 m = m->m_next; 2336 } 2337 } 2338 slp->ns_raw = m; 2339 slp->ns_cc -= len; 2340 slp->ns_reclen = 0; 2341 } else { 2342 return (0); 2343 } 2344 2345 /* 2346 * Accumulate the fragments into a record. 2347 */ 2348 mpp = &slp->ns_frag; 2349 while (*mpp) 2350 mpp = &((*mpp)->m_next); 2351 *mpp = recm; 2352 if (slp->ns_flag & SLP_LASTFRAG) { 2353 struct nfsrv_rec *rec; 2354 int mf = (waitflag & MB_DONTWAIT) ? M_NOWAIT : M_WAITOK; 2355 rec = malloc(sizeof(struct nfsrv_rec), M_NFSRVDESC, mf); 2356 if (!rec) { 2357 m_freem(slp->ns_frag); 2358 } else { 2359 nfs_realign(&slp->ns_frag, 10 * NFSX_UNSIGNED); 2360 rec->nr_address = (struct sockaddr *)0; 2361 rec->nr_packet = slp->ns_frag; 2362 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link); 2363 ++slp->ns_numrec; 2364 ++*countp; 2365 } 2366 slp->ns_frag = (struct mbuf *)0; 2367 } 2368 } 2369 } 2370 2371 /* 2372 * Parse an RPC header. 2373 */ 2374 int 2375 nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd, 2376 struct nfsrv_descript **ndp) 2377 { 2378 struct nfsrv_rec *rec; 2379 struct mbuf *m; 2380 struct sockaddr *nam; 2381 struct nfsrv_descript *nd; 2382 int error; 2383 2384 *ndp = NULL; 2385 if ((slp->ns_flag & SLP_VALID) == 0 || !STAILQ_FIRST(&slp->ns_rec)) 2386 return (ENOBUFS); 2387 rec = STAILQ_FIRST(&slp->ns_rec); 2388 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link); 2389 KKASSERT(slp->ns_numrec > 0); 2390 --slp->ns_numrec; 2391 nam = rec->nr_address; 2392 m = rec->nr_packet; 2393 free(rec, M_NFSRVDESC); 2394 MALLOC(nd, struct nfsrv_descript *, sizeof (struct nfsrv_descript), 2395 M_NFSRVDESC, M_WAITOK); 2396 nd->nd_md = nd->nd_mrep = m; 2397 nd->nd_nam2 = nam; 2398 nd->nd_dpos = mtod(m, caddr_t); 2399 error = nfs_getreq(nd, nfsd, TRUE); 2400 if (error) { 2401 if (nam) { 2402 FREE(nam, M_SONAME); 2403 } 2404 free((caddr_t)nd, M_NFSRVDESC); 2405 return (error); 2406 } 2407 *ndp = nd; 2408 nfsd->nfsd_nd = nd; 2409 return (0); 2410 } 2411 2412 /* 2413 * Try to assign service sockets to nfsd threads based on the number 2414 * of new rpc requests that have been queued on the service socket. 2415 * 2416 * If no nfsd's are available or additonal requests are pending, set the 2417 * NFSD_CHECKSLP flag so that one of the running nfsds will go look for 2418 * the work in the nfssvc_sock list when it is finished processing its 2419 * current work. This flag is only cleared when an nfsd can not find 2420 * any new work to perform. 2421 */ 2422 void 2423 nfsrv_wakenfsd(struct nfssvc_sock *slp, int nparallel) 2424 { 2425 struct nfsd *nd; 2426 2427 if ((slp->ns_flag & SLP_VALID) == 0) 2428 return; 2429 if (nparallel <= 1) 2430 nparallel = 1; 2431 TAILQ_FOREACH(nd, &nfsd_head, nfsd_chain) { 2432 if (nd->nfsd_flag & NFSD_WAITING) { 2433 nd->nfsd_flag &= ~NFSD_WAITING; 2434 if (nd->nfsd_slp) 2435 panic("nfsd wakeup"); 2436 slp->ns_sref++; 2437 nd->nfsd_slp = slp; 2438 wakeup((caddr_t)nd); 2439 if (--nparallel == 0) 2440 break; 2441 } 2442 } 2443 if (nparallel) { 2444 slp->ns_flag |= SLP_DOREC; 2445 nfsd_head_flag |= NFSD_CHECKSLP; 2446 } 2447 } 2448 #endif /* NFS_NOSERVER */ 2449