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