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