xref: /netbsd-src/sys/nfs/nfs_socket.c (revision aad9773e38ed2370a628a6416e098f9008fc10a7)
1 /*	$NetBSD: nfs_socket.c,v 1.193 2014/09/05 05:34:57 matt 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.193 2014/09/05 05:34:57 matt Exp $");
43 
44 #ifdef _KERNEL_OPT
45 #include "opt_nfs.h"
46 #include "opt_mbuftrace.h"
47 #endif
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/evcnt.h>
52 #include <sys/callout.h>
53 #include <sys/proc.h>
54 #include <sys/mount.h>
55 #include <sys/kernel.h>
56 #include <sys/kmem.h>
57 #include <sys/mbuf.h>
58 #include <sys/vnode.h>
59 #include <sys/domain.h>
60 #include <sys/protosw.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/syslog.h>
64 #include <sys/tprintf.h>
65 #include <sys/namei.h>
66 #include <sys/signal.h>
67 #include <sys/signalvar.h>
68 #include <sys/kauth.h>
69 
70 #include <netinet/in.h>
71 #include <netinet/tcp.h>
72 
73 #include <nfs/rpcv2.h>
74 #include <nfs/nfsproto.h>
75 #include <nfs/nfs.h>
76 #include <nfs/xdr_subs.h>
77 #include <nfs/nfsm_subs.h>
78 #include <nfs/nfsmount.h>
79 #include <nfs/nfsnode.h>
80 #include <nfs/nfsrtt.h>
81 #include <nfs/nfs_var.h>
82 
83 #ifdef MBUFTRACE
84 struct mowner nfs_mowner = MOWNER_INIT("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[nfs_proct[(r)->r_procnum] - 1]
105 #define	NFS_SDRTT(r)	(r)->r_nmp->nm_sdrtt[nfs_proct[(r)->r_procnum] - 1]
106 
107 /*
108  * Defines which timer to use for the procnum.
109  * 0 - default
110  * 1 - getattr
111  * 2 - lookup
112  * 3 - read
113  * 4 - write
114  */
115 const int nfs_proct[NFS_NPROCS] = {
116 	[NFSPROC_NULL] = 0,
117 	[NFSPROC_GETATTR] = 1,
118 	[NFSPROC_SETATTR] = 0,
119 	[NFSPROC_LOOKUP] = 2,
120 	[NFSPROC_ACCESS] = 1,
121 	[NFSPROC_READLINK] = 3,
122 	[NFSPROC_READ] = 3,
123 	[NFSPROC_WRITE] = 4,
124 	[NFSPROC_CREATE] = 0,
125 	[NFSPROC_MKDIR] = 0,
126 	[NFSPROC_SYMLINK] = 0,
127 	[NFSPROC_MKNOD] = 0,
128 	[NFSPROC_REMOVE] = 0,
129 	[NFSPROC_RMDIR] = 0,
130 	[NFSPROC_RENAME] = 0,
131 	[NFSPROC_LINK] = 0,
132 	[NFSPROC_READDIR] = 3,
133 	[NFSPROC_READDIRPLUS] = 3,
134 	[NFSPROC_FSSTAT] = 0,
135 	[NFSPROC_FSINFO] = 0,
136 	[NFSPROC_PATHCONF] = 0,
137 	[NFSPROC_COMMIT] = 0,
138 	[NFSPROC_NOOP] = 0,
139 };
140 
141 #ifdef DEBUG
142 /*
143  * Avoid spamming the console with debugging messages.  We only print
144  * the nfs timer and reply error debugs every 10 seconds.
145  */
146 const struct timeval nfs_err_interval = { 10, 0 };
147 struct timeval nfs_reply_last_err_time;
148 struct timeval nfs_timer_last_err_time;
149 #endif
150 
151 /*
152  * There is a congestion window for outstanding rpcs maintained per mount
153  * point. The cwnd size is adjusted in roughly the way that:
154  * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
155  * SIGCOMM '88". ACM, August 1988.
156  * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
157  * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
158  * of rpcs is in progress.
159  * (The sent count and cwnd are scaled for integer arith.)
160  * Variants of "slow start" were tried and were found to be too much of a
161  * performance hit (ave. rtt 3 times larger),
162  * I suspect due to the large rtt that nfs rpcs have.
163  */
164 int nfsrtton = 0;
165 struct nfsrtt nfsrtt;
166 static const int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
167 struct nfsreqhead nfs_reqq;
168 static callout_t nfs_timer_ch;
169 static struct evcnt nfs_timer_ev;
170 static struct evcnt nfs_timer_start_ev;
171 static struct evcnt nfs_timer_stop_ev;
172 static kmutex_t nfs_timer_lock;
173 static bool (*nfs_timer_srvvec)(void);
174 
175 /*
176  * Initialize sockets and congestion for a new NFS connection.
177  * We do not free the sockaddr if error.
178  */
179 int
180 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep, struct lwp *l)
181 {
182 	struct socket *so;
183 	int error, rcvreserve, sndreserve;
184 	struct sockaddr *saddr;
185 	struct sockaddr_in *sin;
186 	struct sockaddr_in6 *sin6;
187 	struct mbuf *m;
188 	int val;
189 
190 	nmp->nm_so = NULL;
191 	saddr = mtod(nmp->nm_nam, struct sockaddr *);
192 	error = socreate(saddr->sa_family, &nmp->nm_so,
193 		nmp->nm_sotype, nmp->nm_soproto, l, NULL);
194 	if (error)
195 		goto bad;
196 	so = nmp->nm_so;
197 #ifdef MBUFTRACE
198 	so->so_mowner = &nfs_mowner;
199 	so->so_rcv.sb_mowner = &nfs_mowner;
200 	so->so_snd.sb_mowner = &nfs_mowner;
201 #endif
202 	nmp->nm_soflags = so->so_proto->pr_flags;
203 
204 	/*
205 	 * Some servers require that the client port be a reserved port number.
206 	 */
207 	if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
208 		val = IP_PORTRANGE_LOW;
209 
210 		if ((error = so_setsockopt(NULL, so, IPPROTO_IP, IP_PORTRANGE,
211 		    &val, sizeof(val))))
212 			goto bad;
213 		m = m_get(M_WAIT, MT_SONAME);
214 		MCLAIM(m, so->so_mowner);
215 		sin = mtod(m, struct sockaddr_in *);
216 		sin->sin_len = m->m_len = sizeof (struct sockaddr_in);
217 		sin->sin_family = AF_INET;
218 		sin->sin_addr.s_addr = INADDR_ANY;
219 		sin->sin_port = 0;
220 		error = sobind(so, m, &lwp0);
221 		m_freem(m);
222 		if (error)
223 			goto bad;
224 	}
225 	if (saddr->sa_family == AF_INET6 && (nmp->nm_flag & NFSMNT_RESVPORT)) {
226 		val = IPV6_PORTRANGE_LOW;
227 
228 		if ((error = so_setsockopt(NULL, so, IPPROTO_IPV6,
229 		    IPV6_PORTRANGE, &val, sizeof(val))))
230 			goto bad;
231 		m = m_get(M_WAIT, MT_SONAME);
232 		MCLAIM(m, so->so_mowner);
233 		sin6 = mtod(m, struct sockaddr_in6 *);
234 		memset(sin6, 0, sizeof(*sin6));
235 		sin6->sin6_len = m->m_len = sizeof (struct sockaddr_in6);
236 		sin6->sin6_family = AF_INET6;
237 		error = sobind(so, m, &lwp0);
238 		m_freem(m);
239 		if (error)
240 			goto bad;
241 	}
242 
243 	/*
244 	 * Protocols that do not require connections may be optionally left
245 	 * unconnected for servers that reply from a port other than NFS_PORT.
246 	 */
247 	solock(so);
248 	if (nmp->nm_flag & NFSMNT_NOCONN) {
249 		if (nmp->nm_soflags & PR_CONNREQUIRED) {
250 			sounlock(so);
251 			error = ENOTCONN;
252 			goto bad;
253 		}
254 	} else {
255 		error = soconnect(so, nmp->nm_nam, l);
256 		if (error) {
257 			sounlock(so);
258 			goto bad;
259 		}
260 
261 		/*
262 		 * Wait for the connection to complete. Cribbed from the
263 		 * connect system call but with the wait timing out so
264 		 * that interruptible mounts don't hang here for a long time.
265 		 */
266 		while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
267 			(void)sowait(so, false, 2 * hz);
268 			if ((so->so_state & SS_ISCONNECTING) &&
269 			    so->so_error == 0 && rep &&
270 			    (error = nfs_sigintr(nmp, rep, rep->r_lwp)) != 0){
271 				so->so_state &= ~SS_ISCONNECTING;
272 				sounlock(so);
273 				goto bad;
274 			}
275 		}
276 		if (so->so_error) {
277 			error = so->so_error;
278 			so->so_error = 0;
279 			sounlock(so);
280 			goto bad;
281 		}
282 	}
283 	if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT)) {
284 		so->so_rcv.sb_timeo = (5 * hz);
285 		so->so_snd.sb_timeo = (5 * hz);
286 	} else {
287 		/*
288 		 * enable receive timeout to detect server crash and reconnect.
289 		 * otherwise, we can be stuck in soreceive forever.
290 		 */
291 		so->so_rcv.sb_timeo = (5 * hz);
292 		so->so_snd.sb_timeo = 0;
293 	}
294 	if (nmp->nm_sotype == SOCK_DGRAM) {
295 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 3;
296 		rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
297 		    NFS_MAXPKTHDR) * 2;
298 	} else if (nmp->nm_sotype == SOCK_SEQPACKET) {
299 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 3;
300 		rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
301 		    NFS_MAXPKTHDR) * 3;
302 	} else {
303 		sounlock(so);
304 		if (nmp->nm_sotype != SOCK_STREAM)
305 			panic("nfscon sotype");
306 		if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
307 			val = 1;
308 			so_setsockopt(NULL, so, SOL_SOCKET, SO_KEEPALIVE, &val,
309 			    sizeof(val));
310 		}
311 		if (so->so_proto->pr_protocol == IPPROTO_TCP) {
312 			val = 1;
313 			so_setsockopt(NULL, so, IPPROTO_TCP, TCP_NODELAY, &val,
314 			    sizeof(val));
315 		}
316 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
317 		    sizeof (u_int32_t)) * 3;
318 		rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
319 		    sizeof (u_int32_t)) * 3;
320 		solock(so);
321 	}
322 	error = soreserve(so, sndreserve, rcvreserve);
323 	if (error) {
324 		sounlock(so);
325 		goto bad;
326 	}
327 	so->so_rcv.sb_flags |= SB_NOINTR;
328 	so->so_snd.sb_flags |= SB_NOINTR;
329 	sounlock(so);
330 
331 	/* Initialize other non-zero congestion variables */
332 	nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] = nmp->nm_srtt[3] =
333 		NFS_TIMEO << 3;
334 	nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
335 		nmp->nm_sdrtt[3] = 0;
336 	nmp->nm_cwnd = NFS_MAXCWND / 2;	    /* Initial send window */
337 	nmp->nm_sent = 0;
338 	nmp->nm_timeouts = 0;
339 	return (0);
340 
341 bad:
342 	nfs_disconnect(nmp);
343 	return (error);
344 }
345 
346 /*
347  * Reconnect routine:
348  * Called when a connection is broken on a reliable protocol.
349  * - clean up the old socket
350  * - nfs_connect() again
351  * - set R_MUSTRESEND for all outstanding requests on mount point
352  * If this fails the mount point is DEAD!
353  * nb: Must be called with the nfs_sndlock() set on the mount point.
354  */
355 int
356 nfs_reconnect(struct nfsreq *rep)
357 {
358 	struct nfsreq *rp;
359 	struct nfsmount *nmp = rep->r_nmp;
360 	int error;
361 
362 	nfs_disconnect(nmp);
363 	while ((error = nfs_connect(nmp, rep, &lwp0)) != 0) {
364 		if (error == EINTR || error == ERESTART)
365 			return (EINTR);
366 		kpause("nfscn2", false, hz, NULL);
367 	}
368 
369 	/*
370 	 * Loop through outstanding request list and fix up all requests
371 	 * on old socket.
372 	 */
373 	TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
374 		if (rp->r_nmp == nmp) {
375 			if ((rp->r_flags & R_MUSTRESEND) == 0)
376 				rp->r_flags |= R_MUSTRESEND | R_REXMITTED;
377 			rp->r_rexmit = 0;
378 		}
379 	}
380 	return (0);
381 }
382 
383 /*
384  * NFS disconnect. Clean up and unlink.
385  */
386 void
387 nfs_disconnect(struct nfsmount *nmp)
388 {
389 	struct socket *so;
390 	int drain = 0;
391 
392 	if (nmp->nm_so) {
393 		so = nmp->nm_so;
394 		nmp->nm_so = NULL;
395 		solock(so);
396 		soshutdown(so, SHUT_RDWR);
397 		sounlock(so);
398 		drain = (nmp->nm_iflag & NFSMNT_DISMNT) != 0;
399 		if (drain) {
400 			/*
401 			 * soshutdown() above should wake up the current
402 			 * listener.
403 			 * Now wake up those waiting for the receive lock, and
404 			 * wait for them to go away unhappy, to prevent *nmp
405 			 * from evaporating while they're sleeping.
406 			 */
407 			mutex_enter(&nmp->nm_lock);
408 			while (nmp->nm_waiters > 0) {
409 				cv_broadcast(&nmp->nm_rcvcv);
410 				cv_broadcast(&nmp->nm_sndcv);
411 				cv_wait(&nmp->nm_disconcv, &nmp->nm_lock);
412 			}
413 			mutex_exit(&nmp->nm_lock);
414 		}
415 		soclose(so);
416 	}
417 #ifdef DIAGNOSTIC
418 	if (drain && (nmp->nm_waiters > 0))
419 		panic("nfs_disconnect: waiters left after drain?");
420 #endif
421 }
422 
423 void
424 nfs_safedisconnect(struct nfsmount *nmp)
425 {
426 	struct nfsreq dummyreq;
427 
428 	memset(&dummyreq, 0, sizeof(dummyreq));
429 	dummyreq.r_nmp = nmp;
430 	nfs_rcvlock(nmp, &dummyreq); /* XXX ignored error return */
431 	nfs_disconnect(nmp);
432 	nfs_rcvunlock(nmp);
433 }
434 
435 /*
436  * This is the nfs send routine. For connection based socket types, it
437  * must be called with an nfs_sndlock() on the socket.
438  * "rep == NULL" indicates that it has been called from a server.
439  * For the client side:
440  * - return EINTR if the RPC is terminated, 0 otherwise
441  * - set R_MUSTRESEND if the send fails for any reason
442  * - do any cleanup required by recoverable socket errors (? ? ?)
443  * For the server side:
444  * - return EINTR or ERESTART if interrupted by a signal
445  * - return EPIPE if a connection is lost for connection based sockets (TCP...)
446  * - do any cleanup required by recoverable socket errors (? ? ?)
447  */
448 int
449 nfs_send(struct socket *so, struct mbuf *nam, struct mbuf *top, struct nfsreq *rep, struct lwp *l)
450 {
451 	struct mbuf *sendnam;
452 	int error, soflags, flags;
453 
454 	/* XXX nfs_doio()/nfs_request() calls with  rep->r_lwp == NULL */
455 	if (l == NULL && rep->r_lwp == NULL)
456 		l = curlwp;
457 
458 	if (rep) {
459 		if (rep->r_flags & R_SOFTTERM) {
460 			m_freem(top);
461 			return (EINTR);
462 		}
463 		if ((so = rep->r_nmp->nm_so) == NULL) {
464 			rep->r_flags |= R_MUSTRESEND;
465 			m_freem(top);
466 			return (0);
467 		}
468 		rep->r_flags &= ~R_MUSTRESEND;
469 		soflags = rep->r_nmp->nm_soflags;
470 	} else
471 		soflags = so->so_proto->pr_flags;
472 	if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
473 		sendnam = NULL;
474 	else
475 		sendnam = nam;
476 	if (so->so_type == SOCK_SEQPACKET)
477 		flags = MSG_EOR;
478 	else
479 		flags = 0;
480 
481 	error = (*so->so_send)(so, sendnam, NULL, top, NULL, flags,  l);
482 	if (error) {
483 		if (rep) {
484 			if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
485 				/*
486 				 * We're too fast for the network/driver,
487 				 * and UDP isn't flowcontrolled.
488 				 * We need to resend. This is not fatal,
489 				 * just try again.
490 				 *
491 				 * Could be smarter here by doing some sort
492 				 * of a backoff, but this is rare.
493 				 */
494 				rep->r_flags |= R_MUSTRESEND;
495 			} else {
496 				if (error != EPIPE)
497 					log(LOG_INFO,
498 					    "nfs send error %d for %s\n",
499 					    error,
500 					    rep->r_nmp->nm_mountp->
501 						    mnt_stat.f_mntfromname);
502 				/*
503 				 * Deal with errors for the client side.
504 				 */
505 				if (rep->r_flags & R_SOFTTERM)
506 					error = EINTR;
507 				else if (error != EMSGSIZE)
508 					rep->r_flags |= R_MUSTRESEND;
509 			}
510 		} else {
511 			/*
512 			 * See above. This error can happen under normal
513 			 * circumstances and the log is too noisy.
514 			 * The error will still show up in nfsstat.
515 			 */
516 			if (error != ENOBUFS || so->so_type != SOCK_DGRAM)
517 				log(LOG_INFO, "nfsd send error %d\n", error);
518 		}
519 
520 		/*
521 		 * Handle any recoverable (soft) socket errors here. (? ? ?)
522 		 */
523 		if (error != EINTR && error != ERESTART &&
524 		    error != EWOULDBLOCK && error != EPIPE &&
525 		    error != EMSGSIZE)
526 			error = 0;
527 	}
528 	return (error);
529 }
530 
531 /*
532  * Generate the rpc reply header
533  * siz arg. is used to decide if adding a cluster is worthwhile
534  */
535 int
536 nfs_rephead(int siz, struct nfsrv_descript *nd, struct nfssvc_sock *slp, int err, int cache, u_quad_t *frev, struct mbuf **mrq, struct mbuf **mbp, char **bposp)
537 {
538 	u_int32_t *tl;
539 	struct mbuf *mreq;
540 	char *bpos;
541 	struct mbuf *mb;
542 
543 	mreq = m_gethdr(M_WAIT, MT_DATA);
544 	MCLAIM(mreq, &nfs_mowner);
545 	mb = mreq;
546 	/*
547 	 * If this is a big reply, use a cluster else
548 	 * try and leave leading space for the lower level headers.
549 	 */
550 	siz += RPC_REPLYSIZ;
551 	if (siz >= max_datalen) {
552 		m_clget(mreq, M_WAIT);
553 	} else
554 		mreq->m_data += max_hdr;
555 	tl = mtod(mreq, u_int32_t *);
556 	mreq->m_len = 6 * NFSX_UNSIGNED;
557 	bpos = ((char *)tl) + mreq->m_len;
558 	*tl++ = txdr_unsigned(nd->nd_retxid);
559 	*tl++ = rpc_reply;
560 	if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
561 		*tl++ = rpc_msgdenied;
562 		if (err & NFSERR_AUTHERR) {
563 			*tl++ = rpc_autherr;
564 			*tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
565 			mreq->m_len -= NFSX_UNSIGNED;
566 			bpos -= NFSX_UNSIGNED;
567 		} else {
568 			*tl++ = rpc_mismatch;
569 			*tl++ = txdr_unsigned(RPC_VER2);
570 			*tl = txdr_unsigned(RPC_VER2);
571 		}
572 	} else {
573 		*tl++ = rpc_msgaccepted;
574 
575 		/*
576 		 * For Kerberos authentication, we must send the nickname
577 		 * verifier back, otherwise just RPCAUTH_NULL.
578 		 */
579 		if (nd->nd_flag & ND_KERBFULL) {
580 			struct nfsuid *nuidp;
581 			struct timeval ktvin, ktvout;
582 
583 			memset(&ktvout, 0, sizeof ktvout);	/* XXX gcc */
584 
585 			LIST_FOREACH(nuidp,
586 			    NUIDHASH(slp, kauth_cred_geteuid(nd->nd_cr)),
587 			    nu_hash) {
588 				if (kauth_cred_geteuid(nuidp->nu_cr) ==
589 				kauth_cred_geteuid(nd->nd_cr) &&
590 				    (!nd->nd_nam2 || netaddr_match(
591 				    NU_NETFAM(nuidp), &nuidp->nu_haddr,
592 				    nd->nd_nam2)))
593 					break;
594 			}
595 			if (nuidp) {
596 				ktvin.tv_sec =
597 				    txdr_unsigned(nuidp->nu_timestamp.tv_sec
598 					- 1);
599 				ktvin.tv_usec =
600 				    txdr_unsigned(nuidp->nu_timestamp.tv_usec);
601 
602 				/*
603 				 * Encrypt the timestamp in ecb mode using the
604 				 * session key.
605 				 */
606 #ifdef NFSKERB
607 				XXX
608 #else
609 				(void)ktvin.tv_sec;
610 #endif
611 
612 				*tl++ = rpc_auth_kerb;
613 				*tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
614 				*tl = ktvout.tv_sec;
615 				nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
616 				*tl++ = ktvout.tv_usec;
617 				*tl++ = txdr_unsigned(
618 				    kauth_cred_geteuid(nuidp->nu_cr));
619 			} else {
620 				*tl++ = 0;
621 				*tl++ = 0;
622 			}
623 		} else {
624 			*tl++ = 0;
625 			*tl++ = 0;
626 		}
627 		switch (err) {
628 		case EPROGUNAVAIL:
629 			*tl = txdr_unsigned(RPC_PROGUNAVAIL);
630 			break;
631 		case EPROGMISMATCH:
632 			*tl = txdr_unsigned(RPC_PROGMISMATCH);
633 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
634 			*tl++ = txdr_unsigned(2);
635 			*tl = txdr_unsigned(3);
636 			break;
637 		case EPROCUNAVAIL:
638 			*tl = txdr_unsigned(RPC_PROCUNAVAIL);
639 			break;
640 		case EBADRPC:
641 			*tl = txdr_unsigned(RPC_GARBAGE);
642 			break;
643 		default:
644 			*tl = 0;
645 			if (err != NFSERR_RETVOID) {
646 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
647 				if (err)
648 				    *tl = txdr_unsigned(nfsrv_errmap(nd, err));
649 				else
650 				    *tl = 0;
651 			}
652 			break;
653 		};
654 	}
655 
656 	if (mrq != NULL)
657 		*mrq = mreq;
658 	*mbp = mb;
659 	*bposp = bpos;
660 	if (err != 0 && err != NFSERR_RETVOID)
661 		nfsstats.srvrpc_errs++;
662 	return (0);
663 }
664 
665 static void
666 nfs_timer_schedule(void)
667 {
668 
669 	callout_schedule(&nfs_timer_ch, nfs_ticks);
670 }
671 
672 void
673 nfs_timer_start(void)
674 {
675 
676 	if (callout_pending(&nfs_timer_ch))
677 		return;
678 
679 	nfs_timer_start_ev.ev_count++;
680 	nfs_timer_schedule();
681 }
682 
683 void
684 nfs_timer_init(void)
685 {
686 
687 	mutex_init(&nfs_timer_lock, MUTEX_DEFAULT, IPL_NONE);
688 	callout_init(&nfs_timer_ch, 0);
689 	callout_setfunc(&nfs_timer_ch, nfs_timer, NULL);
690 	evcnt_attach_dynamic(&nfs_timer_ev, EVCNT_TYPE_MISC, NULL,
691 	    "nfs", "timer");
692 	evcnt_attach_dynamic(&nfs_timer_start_ev, EVCNT_TYPE_MISC, NULL,
693 	    "nfs", "timer start");
694 	evcnt_attach_dynamic(&nfs_timer_stop_ev, EVCNT_TYPE_MISC, NULL,
695 	    "nfs", "timer stop");
696 }
697 
698 void
699 nfs_timer_fini(void)
700 {
701 
702 	callout_halt(&nfs_timer_ch, NULL);
703 	callout_destroy(&nfs_timer_ch);
704 	mutex_destroy(&nfs_timer_lock);
705 	evcnt_detach(&nfs_timer_ev);
706 	evcnt_detach(&nfs_timer_start_ev);
707 	evcnt_detach(&nfs_timer_stop_ev);
708 }
709 
710 void
711 nfs_timer_srvinit(bool (*func)(void))
712 {
713 
714 	nfs_timer_srvvec = func;
715 }
716 
717 void
718 nfs_timer_srvfini(void)
719 {
720 
721 	mutex_enter(&nfs_timer_lock);
722 	nfs_timer_srvvec = NULL;
723 	mutex_exit(&nfs_timer_lock);
724 }
725 
726 
727 /*
728  * Nfs timer routine
729  * Scan the nfsreq list and retranmit any requests that have timed out
730  * To avoid retransmission attempts on STREAM sockets (in the future) make
731  * sure to set the r_retry field to 0 (implies nm_retry == 0).
732  */
733 void
734 nfs_timer(void *arg)
735 {
736 	struct nfsreq *rep;
737 	struct mbuf *m;
738 	struct socket *so;
739 	struct nfsmount *nmp;
740 	int timeo;
741 	int error;
742 	bool more = false;
743 
744 	nfs_timer_ev.ev_count++;
745 
746 	mutex_enter(softnet_lock);	/* XXX PR 40491 */
747 	TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
748 		more = true;
749 		nmp = rep->r_nmp;
750 		if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
751 			continue;
752 		if (nfs_sigintr(nmp, rep, rep->r_lwp)) {
753 			rep->r_flags |= R_SOFTTERM;
754 			continue;
755 		}
756 		if (rep->r_rtt >= 0) {
757 			rep->r_rtt++;
758 			if (nmp->nm_flag & NFSMNT_DUMBTIMR)
759 				timeo = nmp->nm_timeo;
760 			else
761 				timeo = NFS_RTO(nmp, nfs_proct[rep->r_procnum]);
762 			if (nmp->nm_timeouts > 0)
763 				timeo *= nfs_backoff[nmp->nm_timeouts - 1];
764 			if (timeo > NFS_MAXTIMEO)
765 				timeo = NFS_MAXTIMEO;
766 			if (rep->r_rtt <= timeo)
767 				continue;
768 			if (nmp->nm_timeouts <
769 			    (sizeof(nfs_backoff) / sizeof(nfs_backoff[0])))
770 				nmp->nm_timeouts++;
771 		}
772 		/*
773 		 * Check for server not responding
774 		 */
775 		if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
776 		     rep->r_rexmit > nmp->nm_deadthresh) {
777 			nfs_msg(rep->r_lwp,
778 			    nmp->nm_mountp->mnt_stat.f_mntfromname,
779 			    "not responding");
780 			rep->r_flags |= R_TPRINTFMSG;
781 		}
782 		if (rep->r_rexmit >= rep->r_retry) {	/* too many */
783 			nfsstats.rpctimeouts++;
784 			rep->r_flags |= R_SOFTTERM;
785 			continue;
786 		}
787 		if (nmp->nm_sotype != SOCK_DGRAM) {
788 			if (++rep->r_rexmit > NFS_MAXREXMIT)
789 				rep->r_rexmit = NFS_MAXREXMIT;
790 			continue;
791 		}
792 		if ((so = nmp->nm_so) == NULL)
793 			continue;
794 
795 		/*
796 		 * If there is enough space and the window allows..
797 		 *	Resend it
798 		 * Set r_rtt to -1 in case we fail to send it now.
799 		 */
800 		/* solock(so);		XXX PR 40491 */
801 		rep->r_rtt = -1;
802 		if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
803 		   ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
804 		    (rep->r_flags & R_SENT) ||
805 		    nmp->nm_sent < nmp->nm_cwnd) &&
806 		   (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
807 		        if (so->so_state & SS_ISCONNECTED)
808 			    error = (*so->so_proto->pr_usrreqs->pr_send)(so,
809 			    m, NULL, NULL, NULL);
810 			else
811 			    error = (*so->so_proto->pr_usrreqs->pr_send)(so,
812 			    m, nmp->nm_nam, NULL, NULL);
813 			if (error) {
814 				if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
815 #ifdef DEBUG
816 					if (ratecheck(&nfs_timer_last_err_time,
817 					    &nfs_err_interval))
818 						printf("%s: ignoring error "
819 						       "%d\n", __func__, error);
820 #endif
821 					so->so_error = 0;
822 				}
823 			} else {
824 				/*
825 				 * Iff first send, start timing
826 				 * else turn timing off, backoff timer
827 				 * and divide congestion window by 2.
828 				 */
829 				if (rep->r_flags & R_SENT) {
830 					rep->r_flags &= ~R_TIMING;
831 					if (++rep->r_rexmit > NFS_MAXREXMIT)
832 						rep->r_rexmit = NFS_MAXREXMIT;
833 					nmp->nm_cwnd >>= 1;
834 					if (nmp->nm_cwnd < NFS_CWNDSCALE)
835 						nmp->nm_cwnd = NFS_CWNDSCALE;
836 					nfsstats.rpcretries++;
837 				} else {
838 					rep->r_flags |= R_SENT;
839 					nmp->nm_sent += NFS_CWNDSCALE;
840 				}
841 				rep->r_rtt = 0;
842 			}
843 		}
844 		/* sounlock(so);	XXX PR 40491 */
845 	}
846 	mutex_exit(softnet_lock);	/* XXX PR 40491 */
847 
848 	mutex_enter(&nfs_timer_lock);
849 	if (nfs_timer_srvvec != NULL) {
850 		more |= (*nfs_timer_srvvec)();
851 	}
852 	mutex_exit(&nfs_timer_lock);
853 
854 	if (more) {
855 		nfs_timer_schedule();
856 	} else {
857 		nfs_timer_stop_ev.ev_count++;
858 	}
859 }
860 
861 /*
862  * Test for a termination condition pending on the process.
863  * This is used for NFSMNT_INT mounts.
864  */
865 int
866 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct lwp *l)
867 {
868 	sigset_t ss;
869 
870 	if (rep && (rep->r_flags & R_SOFTTERM))
871 		return (EINTR);
872 	if (!(nmp->nm_flag & NFSMNT_INT))
873 		return (0);
874 	if (l) {
875 		sigpending1(l, &ss);
876 #if 0
877 		sigminusset(&l->l_proc->p_sigctx.ps_sigignore, &ss);
878 #endif
879 		if (sigismember(&ss, SIGINT) || sigismember(&ss, SIGTERM) ||
880 		    sigismember(&ss, SIGKILL) || sigismember(&ss, SIGHUP) ||
881 		    sigismember(&ss, SIGQUIT))
882 			return (EINTR);
883 	}
884 	return (0);
885 }
886 
887 int
888 nfs_rcvlock(struct nfsmount *nmp, struct nfsreq *rep)
889 {
890 	int *flagp = &nmp->nm_iflag;
891 	int slptimeo = 0;
892 	bool catch_p;
893 	int error = 0;
894 
895 	KASSERT(nmp == rep->r_nmp);
896 
897 	catch_p = (nmp->nm_flag & NFSMNT_INT) != 0;
898 	mutex_enter(&nmp->nm_lock);
899 	while (/* CONSTCOND */ true) {
900 		if (*flagp & NFSMNT_DISMNT) {
901 			cv_signal(&nmp->nm_disconcv);
902 			error = EIO;
903 			break;
904 		}
905 		/* If our reply was received while we were sleeping,
906 		 * then just return without taking the lock to avoid a
907 		 * situation where a single iod could 'capture' the
908 		 * receive lock.
909 		 */
910 		if (rep->r_mrep != NULL) {
911 			cv_signal(&nmp->nm_rcvcv);
912 			error = EALREADY;
913 			break;
914 		}
915 		if (nfs_sigintr(rep->r_nmp, rep, rep->r_lwp)) {
916 			cv_signal(&nmp->nm_rcvcv);
917 			error = EINTR;
918 			break;
919 		}
920 		if ((*flagp & NFSMNT_RCVLOCK) == 0) {
921 			*flagp |= NFSMNT_RCVLOCK;
922 			break;
923 		}
924 		if (catch_p) {
925 			cv_timedwait_sig(&nmp->nm_rcvcv, &nmp->nm_lock,
926 			    slptimeo);
927 		} else {
928 			cv_timedwait(&nmp->nm_rcvcv, &nmp->nm_lock,
929 			    slptimeo);
930 		}
931 		if (catch_p) {
932 			catch_p = false;
933 			slptimeo = 2 * hz;
934 		}
935 	}
936 	mutex_exit(&nmp->nm_lock);
937 	return error;
938 }
939 
940 /*
941  * Unlock the stream socket for others.
942  */
943 void
944 nfs_rcvunlock(struct nfsmount *nmp)
945 {
946 
947 	mutex_enter(&nmp->nm_lock);
948 	if ((nmp->nm_iflag & NFSMNT_RCVLOCK) == 0)
949 		panic("nfs rcvunlock");
950 	nmp->nm_iflag &= ~NFSMNT_RCVLOCK;
951 	cv_signal(&nmp->nm_rcvcv);
952 	mutex_exit(&nmp->nm_lock);
953 }
954 
955 /*
956  * Parse an RPC request
957  * - verify it
958  * - allocate and fill in the cred.
959  */
960 int
961 nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header)
962 {
963 	int len, i;
964 	u_int32_t *tl;
965 	int32_t t1;
966 	struct uio uio;
967 	struct iovec iov;
968 	char *dpos, *cp2, *cp;
969 	u_int32_t nfsvers, auth_type;
970 	uid_t nickuid;
971 	int error = 0, ticklen;
972 	struct mbuf *mrep, *md;
973 	struct nfsuid *nuidp;
974 	struct timeval tvin, tvout;
975 
976 	memset(&tvout, 0, sizeof tvout);	/* XXX gcc */
977 
978 	KASSERT(nd->nd_cr == NULL);
979 	mrep = nd->nd_mrep;
980 	md = nd->nd_md;
981 	dpos = nd->nd_dpos;
982 	if (has_header) {
983 		nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
984 		nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
985 		if (*tl++ != rpc_call) {
986 			m_freem(mrep);
987 			return (EBADRPC);
988 		}
989 	} else
990 		nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
991 	nd->nd_repstat = 0;
992 	nd->nd_flag = 0;
993 	if (*tl++ != rpc_vers) {
994 		nd->nd_repstat = ERPCMISMATCH;
995 		nd->nd_procnum = NFSPROC_NOOP;
996 		return (0);
997 	}
998 	if (*tl != nfs_prog) {
999 		nd->nd_repstat = EPROGUNAVAIL;
1000 		nd->nd_procnum = NFSPROC_NOOP;
1001 		return (0);
1002 	}
1003 	tl++;
1004 	nfsvers = fxdr_unsigned(u_int32_t, *tl++);
1005 	if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) {
1006 		nd->nd_repstat = EPROGMISMATCH;
1007 		nd->nd_procnum = NFSPROC_NOOP;
1008 		return (0);
1009 	}
1010 	if (nfsvers == NFS_VER3)
1011 		nd->nd_flag = ND_NFSV3;
1012 	nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
1013 	if (nd->nd_procnum == NFSPROC_NULL)
1014 		return (0);
1015 	if (nd->nd_procnum > NFSPROC_COMMIT ||
1016 	    (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
1017 		nd->nd_repstat = EPROCUNAVAIL;
1018 		nd->nd_procnum = NFSPROC_NOOP;
1019 		return (0);
1020 	}
1021 	if ((nd->nd_flag & ND_NFSV3) == 0)
1022 		nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
1023 	auth_type = *tl++;
1024 	len = fxdr_unsigned(int, *tl++);
1025 	if (len < 0 || len > RPCAUTH_MAXSIZ) {
1026 		m_freem(mrep);
1027 		return (EBADRPC);
1028 	}
1029 
1030 	nd->nd_flag &= ~ND_KERBAUTH;
1031 	/*
1032 	 * Handle auth_unix or auth_kerb.
1033 	 */
1034 	if (auth_type == rpc_auth_unix) {
1035 		uid_t uid;
1036 		gid_t gid;
1037 
1038 		nd->nd_cr = kauth_cred_alloc();
1039 		len = fxdr_unsigned(int, *++tl);
1040 		if (len < 0 || len > NFS_MAXNAMLEN) {
1041 			m_freem(mrep);
1042 			error = EBADRPC;
1043 			goto errout;
1044 		}
1045 		nfsm_adv(nfsm_rndup(len));
1046 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1047 
1048 		uid = fxdr_unsigned(uid_t, *tl++);
1049 		gid = fxdr_unsigned(gid_t, *tl++);
1050 		kauth_cred_setuid(nd->nd_cr, uid);
1051 		kauth_cred_seteuid(nd->nd_cr, uid);
1052 		kauth_cred_setsvuid(nd->nd_cr, uid);
1053 		kauth_cred_setgid(nd->nd_cr, gid);
1054 		kauth_cred_setegid(nd->nd_cr, gid);
1055 		kauth_cred_setsvgid(nd->nd_cr, gid);
1056 
1057 		len = fxdr_unsigned(int, *tl);
1058 		if (len < 0 || len > RPCAUTH_UNIXGIDS) {
1059 			m_freem(mrep);
1060 			error = EBADRPC;
1061 			goto errout;
1062 		}
1063 		nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
1064 
1065 		if (len > 0) {
1066 			size_t grbuf_size = min(len, NGROUPS) * sizeof(gid_t);
1067 			gid_t *grbuf = kmem_alloc(grbuf_size, KM_SLEEP);
1068 
1069 			for (i = 0; i < len; i++) {
1070 				if (i < NGROUPS) /* XXX elad */
1071 					grbuf[i] = fxdr_unsigned(gid_t, *tl++);
1072 				else
1073 					tl++;
1074 			}
1075 			kauth_cred_setgroups(nd->nd_cr, grbuf,
1076 			    min(len, NGROUPS), -1, UIO_SYSSPACE);
1077 			kmem_free(grbuf, grbuf_size);
1078 		}
1079 
1080 		len = fxdr_unsigned(int, *++tl);
1081 		if (len < 0 || len > RPCAUTH_MAXSIZ) {
1082 			m_freem(mrep);
1083 			error = EBADRPC;
1084 			goto errout;
1085 		}
1086 		if (len > 0)
1087 			nfsm_adv(nfsm_rndup(len));
1088 	} else if (auth_type == rpc_auth_kerb) {
1089 		switch (fxdr_unsigned(int, *tl++)) {
1090 		case RPCAKN_FULLNAME:
1091 			ticklen = fxdr_unsigned(int, *tl);
1092 			*((u_int32_t *)nfsd->nfsd_authstr) = *tl;
1093 			uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
1094 			nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
1095 			if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
1096 				m_freem(mrep);
1097 				error = EBADRPC;
1098 				goto errout;
1099 			}
1100 			uio.uio_offset = 0;
1101 			uio.uio_iov = &iov;
1102 			uio.uio_iovcnt = 1;
1103 			UIO_SETUP_SYSSPACE(&uio);
1104 			iov.iov_base = (void *)&nfsd->nfsd_authstr[4];
1105 			iov.iov_len = RPCAUTH_MAXSIZ - 4;
1106 			nfsm_mtouio(&uio, uio.uio_resid);
1107 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1108 			if (*tl++ != rpc_auth_kerb ||
1109 				fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
1110 				printf("Bad kerb verifier\n");
1111 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1112 				nd->nd_procnum = NFSPROC_NOOP;
1113 				return (0);
1114 			}
1115 			nfsm_dissect(cp, void *, 4 * NFSX_UNSIGNED);
1116 			tl = (u_int32_t *)cp;
1117 			if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
1118 				printf("Not fullname kerb verifier\n");
1119 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1120 				nd->nd_procnum = NFSPROC_NOOP;
1121 				return (0);
1122 			}
1123 			cp += NFSX_UNSIGNED;
1124 			memcpy(nfsd->nfsd_verfstr, cp, 3 * NFSX_UNSIGNED);
1125 			nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
1126 			nd->nd_flag |= ND_KERBFULL;
1127 			nfsd->nfsd_flag |= NFSD_NEEDAUTH;
1128 			break;
1129 		case RPCAKN_NICKNAME:
1130 			if (len != 2 * NFSX_UNSIGNED) {
1131 				printf("Kerb nickname short\n");
1132 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
1133 				nd->nd_procnum = NFSPROC_NOOP;
1134 				return (0);
1135 			}
1136 			nickuid = fxdr_unsigned(uid_t, *tl);
1137 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1138 			if (*tl++ != rpc_auth_kerb ||
1139 				fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
1140 				printf("Kerb nick verifier bad\n");
1141 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1142 				nd->nd_procnum = NFSPROC_NOOP;
1143 				return (0);
1144 			}
1145 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1146 			tvin.tv_sec = *tl++;
1147 			tvin.tv_usec = *tl;
1148 
1149 			LIST_FOREACH(nuidp, NUIDHASH(nfsd->nfsd_slp, nickuid),
1150 			    nu_hash) {
1151 				if (kauth_cred_geteuid(nuidp->nu_cr) == nickuid &&
1152 				    (!nd->nd_nam2 ||
1153 				     netaddr_match(NU_NETFAM(nuidp),
1154 				      &nuidp->nu_haddr, nd->nd_nam2)))
1155 					break;
1156 			}
1157 			if (!nuidp) {
1158 				nd->nd_repstat =
1159 					(NFSERR_AUTHERR|AUTH_REJECTCRED);
1160 				nd->nd_procnum = NFSPROC_NOOP;
1161 				return (0);
1162 			}
1163 
1164 			/*
1165 			 * Now, decrypt the timestamp using the session key
1166 			 * and validate it.
1167 			 */
1168 #ifdef NFSKERB
1169 			XXX
1170 #else
1171 			(void)tvin.tv_sec;
1172 #endif
1173 
1174 			tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
1175 			tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
1176 			if (nuidp->nu_expire < time_second ||
1177 			    nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
1178 			    (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
1179 			     nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
1180 				nuidp->nu_expire = 0;
1181 				nd->nd_repstat =
1182 				    (NFSERR_AUTHERR|AUTH_REJECTVERF);
1183 				nd->nd_procnum = NFSPROC_NOOP;
1184 				return (0);
1185 			}
1186 			kauth_cred_hold(nuidp->nu_cr);
1187 			nd->nd_cr = nuidp->nu_cr;
1188 			nd->nd_flag |= ND_KERBNICK;
1189 		}
1190 	} else {
1191 		nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
1192 		nd->nd_procnum = NFSPROC_NOOP;
1193 		return (0);
1194 	}
1195 
1196 	nd->nd_md = md;
1197 	nd->nd_dpos = dpos;
1198 	KASSERT((nd->nd_cr == NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) != 0)
1199 	     || (nd->nd_cr != NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) == 0));
1200 	return (0);
1201 nfsmout:
1202 errout:
1203 	KASSERT(error != 0);
1204 	if (nd->nd_cr != NULL) {
1205 		kauth_cred_free(nd->nd_cr);
1206 		nd->nd_cr = NULL;
1207 	}
1208 	return (error);
1209 }
1210 
1211 int
1212 nfs_msg(struct lwp *l, const char *server, const char *msg)
1213 {
1214 	tpr_t tpr;
1215 
1216 #if 0 /* XXX nfs_timer can't block on proc_lock */
1217 	if (l)
1218 		tpr = tprintf_open(l->l_proc);
1219 	else
1220 #endif
1221 		tpr = NULL;
1222 	tprintf(tpr, "nfs server %s: %s\n", server, msg);
1223 	tprintf_close(tpr);
1224 	return (0);
1225 }
1226 
1227 static struct pool nfs_srvdesc_pool;
1228 
1229 void
1230 nfsdreq_init(void)
1231 {
1232 
1233 	pool_init(&nfs_srvdesc_pool, sizeof(struct nfsrv_descript),
1234 	    0, 0, 0, "nfsrvdescpl", &pool_allocator_nointr, IPL_NONE);
1235 }
1236 
1237 void
1238 nfsdreq_fini(void)
1239 {
1240 
1241 	pool_destroy(&nfs_srvdesc_pool);
1242 }
1243 
1244 struct nfsrv_descript *
1245 nfsdreq_alloc(void)
1246 {
1247 	struct nfsrv_descript *nd;
1248 
1249 	nd = pool_get(&nfs_srvdesc_pool, PR_WAITOK);
1250 	nd->nd_cr = NULL;
1251 	return nd;
1252 }
1253 
1254 void
1255 nfsdreq_free(struct nfsrv_descript *nd)
1256 {
1257 	kauth_cred_t cr;
1258 
1259 	cr = nd->nd_cr;
1260 	if (cr != NULL) {
1261 		kauth_cred_free(cr);
1262 	}
1263 	pool_put(&nfs_srvdesc_pool, nd);
1264 }
1265