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