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