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