Home | History | Annotate | Line # | Download | only in nfs
nfs_socket.c revision 1.192.4.1
      1 /*	$NetBSD: nfs_socket.c,v 1.192.4.1 2016/07/10 08:38:54 martin 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.192.4.1 2016/07/10 08:38:54 martin 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, s;
    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 	s = splsoftnet();
    374 	TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
    375 		if (rp->r_nmp == nmp) {
    376 			if ((rp->r_flags & R_MUSTRESEND) == 0)
    377 				rp->r_flags |= R_MUSTRESEND | R_REXMITTED;
    378 			rp->r_rexmit = 0;
    379 		}
    380 	}
    381 	splx(s);
    382 	return (0);
    383 }
    384 
    385 /*
    386  * NFS disconnect. Clean up and unlink.
    387  */
    388 void
    389 nfs_disconnect(struct nfsmount *nmp)
    390 {
    391 	struct socket *so;
    392 	int drain = 0;
    393 
    394 	if (nmp->nm_so) {
    395 		so = nmp->nm_so;
    396 		nmp->nm_so = NULL;
    397 		solock(so);
    398 		soshutdown(so, SHUT_RDWR);
    399 		sounlock(so);
    400 		drain = (nmp->nm_iflag & NFSMNT_DISMNT) != 0;
    401 		if (drain) {
    402 			/*
    403 			 * soshutdown() above should wake up the current
    404 			 * listener.
    405 			 * Now wake up those waiting for the receive lock, and
    406 			 * wait for them to go away unhappy, to prevent *nmp
    407 			 * from evaporating while they're sleeping.
    408 			 */
    409 			mutex_enter(&nmp->nm_lock);
    410 			while (nmp->nm_waiters > 0) {
    411 				cv_broadcast(&nmp->nm_rcvcv);
    412 				cv_broadcast(&nmp->nm_sndcv);
    413 				cv_wait(&nmp->nm_disconcv, &nmp->nm_lock);
    414 			}
    415 			mutex_exit(&nmp->nm_lock);
    416 		}
    417 		soclose(so);
    418 	}
    419 #ifdef DIAGNOSTIC
    420 	if (drain && (nmp->nm_waiters > 0))
    421 		panic("nfs_disconnect: waiters left after drain?");
    422 #endif
    423 }
    424 
    425 void
    426 nfs_safedisconnect(struct nfsmount *nmp)
    427 {
    428 	struct nfsreq dummyreq;
    429 
    430 	memset(&dummyreq, 0, sizeof(dummyreq));
    431 	dummyreq.r_nmp = nmp;
    432 	nfs_rcvlock(nmp, &dummyreq); /* XXX ignored error return */
    433 	nfs_disconnect(nmp);
    434 	nfs_rcvunlock(nmp);
    435 }
    436 
    437 /*
    438  * This is the nfs send routine. For connection based socket types, it
    439  * must be called with an nfs_sndlock() on the socket.
    440  * "rep == NULL" indicates that it has been called from a server.
    441  * For the client side:
    442  * - return EINTR if the RPC is terminated, 0 otherwise
    443  * - set R_MUSTRESEND if the send fails for any reason
    444  * - do any cleanup required by recoverable socket errors (? ? ?)
    445  * For the server side:
    446  * - return EINTR or ERESTART if interrupted by a signal
    447  * - return EPIPE if a connection is lost for connection based sockets (TCP...)
    448  * - do any cleanup required by recoverable socket errors (? ? ?)
    449  */
    450 int
    451 nfs_send(struct socket *so, struct mbuf *nam, struct mbuf *top, struct nfsreq *rep, struct lwp *l)
    452 {
    453 	struct mbuf *sendnam;
    454 	int error, soflags, flags;
    455 
    456 	/* XXX nfs_doio()/nfs_request() calls with  rep->r_lwp == NULL */
    457 	if (l == NULL && rep->r_lwp == NULL)
    458 		l = curlwp;
    459 
    460 	if (rep) {
    461 		if (rep->r_flags & R_SOFTTERM) {
    462 			m_freem(top);
    463 			return (EINTR);
    464 		}
    465 		if ((so = rep->r_nmp->nm_so) == NULL) {
    466 			rep->r_flags |= R_MUSTRESEND;
    467 			m_freem(top);
    468 			return (0);
    469 		}
    470 		rep->r_flags &= ~R_MUSTRESEND;
    471 		soflags = rep->r_nmp->nm_soflags;
    472 	} else
    473 		soflags = so->so_proto->pr_flags;
    474 	if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
    475 		sendnam = NULL;
    476 	else
    477 		sendnam = nam;
    478 	if (so->so_type == SOCK_SEQPACKET)
    479 		flags = MSG_EOR;
    480 	else
    481 		flags = 0;
    482 
    483 	error = (*so->so_send)(so, sendnam, NULL, top, NULL, flags,  l);
    484 	if (error) {
    485 		if (rep) {
    486 			if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
    487 				/*
    488 				 * We're too fast for the network/driver,
    489 				 * and UDP isn't flowcontrolled.
    490 				 * We need to resend. This is not fatal,
    491 				 * just try again.
    492 				 *
    493 				 * Could be smarter here by doing some sort
    494 				 * of a backoff, but this is rare.
    495 				 */
    496 				rep->r_flags |= R_MUSTRESEND;
    497 			} else {
    498 				if (error != EPIPE)
    499 					log(LOG_INFO,
    500 					    "nfs send error %d for %s\n",
    501 					    error,
    502 					    rep->r_nmp->nm_mountp->
    503 						    mnt_stat.f_mntfromname);
    504 				/*
    505 				 * Deal with errors for the client side.
    506 				 */
    507 				if (rep->r_flags & R_SOFTTERM)
    508 					error = EINTR;
    509 				else if (error != EMSGSIZE)
    510 					rep->r_flags |= R_MUSTRESEND;
    511 			}
    512 		} else {
    513 			/*
    514 			 * See above. This error can happen under normal
    515 			 * circumstances and the log is too noisy.
    516 			 * The error will still show up in nfsstat.
    517 			 */
    518 			if (error != ENOBUFS || so->so_type != SOCK_DGRAM)
    519 				log(LOG_INFO, "nfsd send error %d\n", error);
    520 		}
    521 
    522 		/*
    523 		 * Handle any recoverable (soft) socket errors here. (? ? ?)
    524 		 */
    525 		if (error != EINTR && error != ERESTART &&
    526 		    error != EWOULDBLOCK && error != EPIPE &&
    527 		    error != EMSGSIZE)
    528 			error = 0;
    529 	}
    530 	return (error);
    531 }
    532 
    533 /*
    534  * Generate the rpc reply header
    535  * siz arg. is used to decide if adding a cluster is worthwhile
    536  */
    537 int
    538 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)
    539 {
    540 	u_int32_t *tl;
    541 	struct mbuf *mreq;
    542 	char *bpos;
    543 	struct mbuf *mb;
    544 
    545 	mreq = m_gethdr(M_WAIT, MT_DATA);
    546 	MCLAIM(mreq, &nfs_mowner);
    547 	mb = mreq;
    548 	/*
    549 	 * If this is a big reply, use a cluster else
    550 	 * try and leave leading space for the lower level headers.
    551 	 */
    552 	siz += RPC_REPLYSIZ;
    553 	if (siz >= max_datalen) {
    554 		m_clget(mreq, M_WAIT);
    555 	} else
    556 		mreq->m_data += max_hdr;
    557 	tl = mtod(mreq, u_int32_t *);
    558 	mreq->m_len = 6 * NFSX_UNSIGNED;
    559 	bpos = ((char *)tl) + mreq->m_len;
    560 	*tl++ = txdr_unsigned(nd->nd_retxid);
    561 	*tl++ = rpc_reply;
    562 	if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
    563 		*tl++ = rpc_msgdenied;
    564 		if (err & NFSERR_AUTHERR) {
    565 			*tl++ = rpc_autherr;
    566 			*tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
    567 			mreq->m_len -= NFSX_UNSIGNED;
    568 			bpos -= NFSX_UNSIGNED;
    569 		} else {
    570 			*tl++ = rpc_mismatch;
    571 			*tl++ = txdr_unsigned(RPC_VER2);
    572 			*tl = txdr_unsigned(RPC_VER2);
    573 		}
    574 	} else {
    575 		*tl++ = rpc_msgaccepted;
    576 
    577 		/*
    578 		 * For Kerberos authentication, we must send the nickname
    579 		 * verifier back, otherwise just RPCAUTH_NULL.
    580 		 */
    581 		if (nd->nd_flag & ND_KERBFULL) {
    582 			struct nfsuid *nuidp;
    583 			struct timeval ktvin, ktvout;
    584 
    585 			memset(&ktvout, 0, sizeof ktvout);	/* XXX gcc */
    586 
    587 			LIST_FOREACH(nuidp,
    588 			    NUIDHASH(slp, kauth_cred_geteuid(nd->nd_cr)),
    589 			    nu_hash) {
    590 				if (kauth_cred_geteuid(nuidp->nu_cr) ==
    591 				kauth_cred_geteuid(nd->nd_cr) &&
    592 				    (!nd->nd_nam2 || netaddr_match(
    593 				    NU_NETFAM(nuidp), &nuidp->nu_haddr,
    594 				    nd->nd_nam2)))
    595 					break;
    596 			}
    597 			if (nuidp) {
    598 				ktvin.tv_sec =
    599 				    txdr_unsigned(nuidp->nu_timestamp.tv_sec
    600 					- 1);
    601 				ktvin.tv_usec =
    602 				    txdr_unsigned(nuidp->nu_timestamp.tv_usec);
    603 
    604 				/*
    605 				 * Encrypt the timestamp in ecb mode using the
    606 				 * session key.
    607 				 */
    608 #ifdef NFSKERB
    609 				XXX
    610 #else
    611 				(void)ktvin.tv_sec;
    612 #endif
    613 
    614 				*tl++ = rpc_auth_kerb;
    615 				*tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
    616 				*tl = ktvout.tv_sec;
    617 				nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
    618 				*tl++ = ktvout.tv_usec;
    619 				*tl++ = txdr_unsigned(
    620 				    kauth_cred_geteuid(nuidp->nu_cr));
    621 			} else {
    622 				*tl++ = 0;
    623 				*tl++ = 0;
    624 			}
    625 		} else {
    626 			*tl++ = 0;
    627 			*tl++ = 0;
    628 		}
    629 		switch (err) {
    630 		case EPROGUNAVAIL:
    631 			*tl = txdr_unsigned(RPC_PROGUNAVAIL);
    632 			break;
    633 		case EPROGMISMATCH:
    634 			*tl = txdr_unsigned(RPC_PROGMISMATCH);
    635 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
    636 			*tl++ = txdr_unsigned(2);
    637 			*tl = txdr_unsigned(3);
    638 			break;
    639 		case EPROCUNAVAIL:
    640 			*tl = txdr_unsigned(RPC_PROCUNAVAIL);
    641 			break;
    642 		case EBADRPC:
    643 			*tl = txdr_unsigned(RPC_GARBAGE);
    644 			break;
    645 		default:
    646 			*tl = 0;
    647 			if (err != NFSERR_RETVOID) {
    648 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    649 				if (err)
    650 				    *tl = txdr_unsigned(nfsrv_errmap(nd, err));
    651 				else
    652 				    *tl = 0;
    653 			}
    654 			break;
    655 		};
    656 	}
    657 
    658 	if (mrq != NULL)
    659 		*mrq = mreq;
    660 	*mbp = mb;
    661 	*bposp = bpos;
    662 	if (err != 0 && err != NFSERR_RETVOID)
    663 		nfsstats.srvrpc_errs++;
    664 	return (0);
    665 }
    666 
    667 static void
    668 nfs_timer_schedule(void)
    669 {
    670 
    671 	callout_schedule(&nfs_timer_ch, nfs_ticks);
    672 }
    673 
    674 void
    675 nfs_timer_start(void)
    676 {
    677 
    678 	if (callout_pending(&nfs_timer_ch))
    679 		return;
    680 
    681 	nfs_timer_start_ev.ev_count++;
    682 	nfs_timer_schedule();
    683 }
    684 
    685 void
    686 nfs_timer_init(void)
    687 {
    688 
    689 	mutex_init(&nfs_timer_lock, MUTEX_DEFAULT, IPL_NONE);
    690 	callout_init(&nfs_timer_ch, 0);
    691 	callout_setfunc(&nfs_timer_ch, nfs_timer, NULL);
    692 	evcnt_attach_dynamic(&nfs_timer_ev, EVCNT_TYPE_MISC, NULL,
    693 	    "nfs", "timer");
    694 	evcnt_attach_dynamic(&nfs_timer_start_ev, EVCNT_TYPE_MISC, NULL,
    695 	    "nfs", "timer start");
    696 	evcnt_attach_dynamic(&nfs_timer_stop_ev, EVCNT_TYPE_MISC, NULL,
    697 	    "nfs", "timer stop");
    698 }
    699 
    700 void
    701 nfs_timer_fini(void)
    702 {
    703 
    704 	callout_halt(&nfs_timer_ch, NULL);
    705 	callout_destroy(&nfs_timer_ch);
    706 	mutex_destroy(&nfs_timer_lock);
    707 	evcnt_detach(&nfs_timer_ev);
    708 	evcnt_detach(&nfs_timer_start_ev);
    709 	evcnt_detach(&nfs_timer_stop_ev);
    710 }
    711 
    712 void
    713 nfs_timer_srvinit(bool (*func)(void))
    714 {
    715 
    716 	nfs_timer_srvvec = func;
    717 }
    718 
    719 void
    720 nfs_timer_srvfini(void)
    721 {
    722 
    723 	mutex_enter(&nfs_timer_lock);
    724 	nfs_timer_srvvec = NULL;
    725 	mutex_exit(&nfs_timer_lock);
    726 }
    727 
    728 
    729 /*
    730  * Nfs timer routine
    731  * Scan the nfsreq list and retranmit any requests that have timed out
    732  * To avoid retransmission attempts on STREAM sockets (in the future) make
    733  * sure to set the r_retry field to 0 (implies nm_retry == 0).
    734  */
    735 void
    736 nfs_timer(void *arg)
    737 {
    738 	struct nfsreq *rep;
    739 	struct mbuf *m;
    740 	struct socket *so;
    741 	struct nfsmount *nmp;
    742 	int timeo;
    743 	int error;
    744 	bool more = false;
    745 
    746 	nfs_timer_ev.ev_count++;
    747 
    748 	mutex_enter(softnet_lock);	/* XXX PR 40491 */
    749 	TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
    750 		more = true;
    751 		nmp = rep->r_nmp;
    752 		if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
    753 			continue;
    754 		if (nfs_sigintr(nmp, rep, rep->r_lwp)) {
    755 			rep->r_flags |= R_SOFTTERM;
    756 			continue;
    757 		}
    758 		if (rep->r_rtt >= 0) {
    759 			rep->r_rtt++;
    760 			if (nmp->nm_flag & NFSMNT_DUMBTIMR)
    761 				timeo = nmp->nm_timeo;
    762 			else
    763 				timeo = NFS_RTO(nmp, nfs_proct[rep->r_procnum]);
    764 			if (nmp->nm_timeouts > 0)
    765 				timeo *= nfs_backoff[nmp->nm_timeouts - 1];
    766 			if (timeo > NFS_MAXTIMEO)
    767 				timeo = NFS_MAXTIMEO;
    768 			if (rep->r_rtt <= timeo)
    769 				continue;
    770 			if (nmp->nm_timeouts <
    771 			    (sizeof(nfs_backoff) / sizeof(nfs_backoff[0])))
    772 				nmp->nm_timeouts++;
    773 		}
    774 		/*
    775 		 * Check for server not responding
    776 		 */
    777 		if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
    778 		     rep->r_rexmit > nmp->nm_deadthresh) {
    779 			nfs_msg(rep->r_lwp,
    780 			    nmp->nm_mountp->mnt_stat.f_mntfromname,
    781 			    "not responding");
    782 			rep->r_flags |= R_TPRINTFMSG;
    783 		}
    784 		if (rep->r_rexmit >= rep->r_retry) {	/* too many */
    785 			nfsstats.rpctimeouts++;
    786 			rep->r_flags |= R_SOFTTERM;
    787 			continue;
    788 		}
    789 		if (nmp->nm_sotype != SOCK_DGRAM) {
    790 			if (++rep->r_rexmit > NFS_MAXREXMIT)
    791 				rep->r_rexmit = NFS_MAXREXMIT;
    792 			continue;
    793 		}
    794 		if ((so = nmp->nm_so) == NULL)
    795 			continue;
    796 
    797 		/*
    798 		 * If there is enough space and the window allows..
    799 		 *	Resend it
    800 		 * Set r_rtt to -1 in case we fail to send it now.
    801 		 */
    802 		/* solock(so);		XXX PR 40491 */
    803 		rep->r_rtt = -1;
    804 		if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
    805 		   ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
    806 		    (rep->r_flags & R_SENT) ||
    807 		    nmp->nm_sent < nmp->nm_cwnd) &&
    808 		   (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
    809 		        if (so->so_state & SS_ISCONNECTED)
    810 			    error = (*so->so_proto->pr_usrreqs->pr_send)(so,
    811 			    m, NULL, NULL, NULL);
    812 			else
    813 			    error = (*so->so_proto->pr_usrreqs->pr_send)(so,
    814 			    m, nmp->nm_nam, NULL, NULL);
    815 			if (error) {
    816 				if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
    817 #ifdef DEBUG
    818 					if (ratecheck(&nfs_timer_last_err_time,
    819 					    &nfs_err_interval))
    820 						printf("%s: ignoring error "
    821 						       "%d\n", __func__, error);
    822 #endif
    823 					so->so_error = 0;
    824 				}
    825 			} else {
    826 				/*
    827 				 * Iff first send, start timing
    828 				 * else turn timing off, backoff timer
    829 				 * and divide congestion window by 2.
    830 				 */
    831 				if (rep->r_flags & R_SENT) {
    832 					rep->r_flags &= ~R_TIMING;
    833 					if (++rep->r_rexmit > NFS_MAXREXMIT)
    834 						rep->r_rexmit = NFS_MAXREXMIT;
    835 					nmp->nm_cwnd >>= 1;
    836 					if (nmp->nm_cwnd < NFS_CWNDSCALE)
    837 						nmp->nm_cwnd = NFS_CWNDSCALE;
    838 					nfsstats.rpcretries++;
    839 				} else {
    840 					rep->r_flags |= R_SENT;
    841 					nmp->nm_sent += NFS_CWNDSCALE;
    842 				}
    843 				rep->r_rtt = 0;
    844 			}
    845 		}
    846 		/* sounlock(so);	XXX PR 40491 */
    847 	}
    848 	mutex_exit(softnet_lock);	/* XXX PR 40491 */
    849 
    850 	mutex_enter(&nfs_timer_lock);
    851 	if (nfs_timer_srvvec != NULL) {
    852 		more |= (*nfs_timer_srvvec)();
    853 	}
    854 	mutex_exit(&nfs_timer_lock);
    855 
    856 	if (more) {
    857 		nfs_timer_schedule();
    858 	} else {
    859 		nfs_timer_stop_ev.ev_count++;
    860 	}
    861 }
    862 
    863 /*
    864  * Test for a termination condition pending on the process.
    865  * This is used for NFSMNT_INT mounts.
    866  */
    867 int
    868 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct lwp *l)
    869 {
    870 	sigset_t ss;
    871 
    872 	if (rep && (rep->r_flags & R_SOFTTERM))
    873 		return (EINTR);
    874 	if (!(nmp->nm_flag & NFSMNT_INT))
    875 		return (0);
    876 	if (l) {
    877 		sigpending1(l, &ss);
    878 #if 0
    879 		sigminusset(&l->l_proc->p_sigctx.ps_sigignore, &ss);
    880 #endif
    881 		if (sigismember(&ss, SIGINT) || sigismember(&ss, SIGTERM) ||
    882 		    sigismember(&ss, SIGKILL) || sigismember(&ss, SIGHUP) ||
    883 		    sigismember(&ss, SIGQUIT))
    884 			return (EINTR);
    885 	}
    886 	return (0);
    887 }
    888 
    889 int
    890 nfs_rcvlock(struct nfsmount *nmp, struct nfsreq *rep)
    891 {
    892 	int *flagp = &nmp->nm_iflag;
    893 	int slptimeo = 0;
    894 	bool catch;
    895 	int error = 0;
    896 
    897 	KASSERT(nmp == rep->r_nmp);
    898 
    899 	catch = (nmp->nm_flag & NFSMNT_INT) != 0;
    900 	mutex_enter(&nmp->nm_lock);
    901 	while (/* CONSTCOND */ true) {
    902 		if (*flagp & NFSMNT_DISMNT) {
    903 			cv_signal(&nmp->nm_disconcv);
    904 			error = EIO;
    905 			break;
    906 		}
    907 		/* If our reply was received while we were sleeping,
    908 		 * then just return without taking the lock to avoid a
    909 		 * situation where a single iod could 'capture' the
    910 		 * receive lock.
    911 		 */
    912 		if (rep->r_mrep != NULL) {
    913 			cv_signal(&nmp->nm_rcvcv);
    914 			error = EALREADY;
    915 			break;
    916 		}
    917 		if (nfs_sigintr(rep->r_nmp, rep, rep->r_lwp)) {
    918 			cv_signal(&nmp->nm_rcvcv);
    919 			error = EINTR;
    920 			break;
    921 		}
    922 		if ((*flagp & NFSMNT_RCVLOCK) == 0) {
    923 			*flagp |= NFSMNT_RCVLOCK;
    924 			break;
    925 		}
    926 		if (catch) {
    927 			cv_timedwait_sig(&nmp->nm_rcvcv, &nmp->nm_lock,
    928 			    slptimeo);
    929 		} else {
    930 			cv_timedwait(&nmp->nm_rcvcv, &nmp->nm_lock,
    931 			    slptimeo);
    932 		}
    933 		if (catch) {
    934 			catch = false;
    935 			slptimeo = 2 * hz;
    936 		}
    937 	}
    938 	mutex_exit(&nmp->nm_lock);
    939 	return error;
    940 }
    941 
    942 /*
    943  * Unlock the stream socket for others.
    944  */
    945 void
    946 nfs_rcvunlock(struct nfsmount *nmp)
    947 {
    948 
    949 	mutex_enter(&nmp->nm_lock);
    950 	if ((nmp->nm_iflag & NFSMNT_RCVLOCK) == 0)
    951 		panic("nfs rcvunlock");
    952 	nmp->nm_iflag &= ~NFSMNT_RCVLOCK;
    953 	cv_signal(&nmp->nm_rcvcv);
    954 	mutex_exit(&nmp->nm_lock);
    955 }
    956 
    957 /*
    958  * Parse an RPC request
    959  * - verify it
    960  * - allocate and fill in the cred.
    961  */
    962 int
    963 nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header)
    964 {
    965 	int len, i;
    966 	u_int32_t *tl;
    967 	int32_t t1;
    968 	struct uio uio;
    969 	struct iovec iov;
    970 	char *dpos, *cp2, *cp;
    971 	u_int32_t nfsvers, auth_type;
    972 	uid_t nickuid;
    973 	int error = 0, ticklen;
    974 	struct mbuf *mrep, *md;
    975 	struct nfsuid *nuidp;
    976 	struct timeval tvin, tvout;
    977 
    978 	memset(&tvout, 0, sizeof tvout);	/* XXX gcc */
    979 
    980 	KASSERT(nd->nd_cr == NULL);
    981 	mrep = nd->nd_mrep;
    982 	md = nd->nd_md;
    983 	dpos = nd->nd_dpos;
    984 	if (has_header) {
    985 		nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
    986 		nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
    987 		if (*tl++ != rpc_call) {
    988 			m_freem(mrep);
    989 			return (EBADRPC);
    990 		}
    991 	} else
    992 		nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
    993 	nd->nd_repstat = 0;
    994 	nd->nd_flag = 0;
    995 	if (*tl++ != rpc_vers) {
    996 		nd->nd_repstat = ERPCMISMATCH;
    997 		nd->nd_procnum = NFSPROC_NOOP;
    998 		return (0);
    999 	}
   1000 	if (*tl != nfs_prog) {
   1001 		nd->nd_repstat = EPROGUNAVAIL;
   1002 		nd->nd_procnum = NFSPROC_NOOP;
   1003 		return (0);
   1004 	}
   1005 	tl++;
   1006 	nfsvers = fxdr_unsigned(u_int32_t, *tl++);
   1007 	if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) {
   1008 		nd->nd_repstat = EPROGMISMATCH;
   1009 		nd->nd_procnum = NFSPROC_NOOP;
   1010 		return (0);
   1011 	}
   1012 	if (nfsvers == NFS_VER3)
   1013 		nd->nd_flag = ND_NFSV3;
   1014 	nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
   1015 	if (nd->nd_procnum == NFSPROC_NULL)
   1016 		return (0);
   1017 	if (nd->nd_procnum > NFSPROC_COMMIT ||
   1018 	    (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
   1019 		nd->nd_repstat = EPROCUNAVAIL;
   1020 		nd->nd_procnum = NFSPROC_NOOP;
   1021 		return (0);
   1022 	}
   1023 	if ((nd->nd_flag & ND_NFSV3) == 0)
   1024 		nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
   1025 	auth_type = *tl++;
   1026 	len = fxdr_unsigned(int, *tl++);
   1027 	if (len < 0 || len > RPCAUTH_MAXSIZ) {
   1028 		m_freem(mrep);
   1029 		return (EBADRPC);
   1030 	}
   1031 
   1032 	nd->nd_flag &= ~ND_KERBAUTH;
   1033 	/*
   1034 	 * Handle auth_unix or auth_kerb.
   1035 	 */
   1036 	if (auth_type == rpc_auth_unix) {
   1037 		uid_t uid;
   1038 		gid_t gid;
   1039 
   1040 		nd->nd_cr = kauth_cred_alloc();
   1041 		len = fxdr_unsigned(int, *++tl);
   1042 		if (len < 0 || len > NFS_MAXNAMLEN) {
   1043 			m_freem(mrep);
   1044 			error = EBADRPC;
   1045 			goto errout;
   1046 		}
   1047 		nfsm_adv(nfsm_rndup(len));
   1048 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1049 
   1050 		uid = fxdr_unsigned(uid_t, *tl++);
   1051 		gid = fxdr_unsigned(gid_t, *tl++);
   1052 		kauth_cred_setuid(nd->nd_cr, uid);
   1053 		kauth_cred_seteuid(nd->nd_cr, uid);
   1054 		kauth_cred_setsvuid(nd->nd_cr, uid);
   1055 		kauth_cred_setgid(nd->nd_cr, gid);
   1056 		kauth_cred_setegid(nd->nd_cr, gid);
   1057 		kauth_cred_setsvgid(nd->nd_cr, gid);
   1058 
   1059 		len = fxdr_unsigned(int, *tl);
   1060 		if (len < 0 || len > RPCAUTH_UNIXGIDS) {
   1061 			m_freem(mrep);
   1062 			error = EBADRPC;
   1063 			goto errout;
   1064 		}
   1065 		nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
   1066 
   1067 		if (len > 0) {
   1068 			size_t grbuf_size = min(len, NGROUPS) * sizeof(gid_t);
   1069 			gid_t *grbuf = kmem_alloc(grbuf_size, KM_SLEEP);
   1070 
   1071 			for (i = 0; i < len; i++) {
   1072 				if (i < NGROUPS) /* XXX elad */
   1073 					grbuf[i] = fxdr_unsigned(gid_t, *tl++);
   1074 				else
   1075 					tl++;
   1076 			}
   1077 			kauth_cred_setgroups(nd->nd_cr, grbuf,
   1078 			    min(len, NGROUPS), -1, UIO_SYSSPACE);
   1079 			kmem_free(grbuf, grbuf_size);
   1080 		}
   1081 
   1082 		len = fxdr_unsigned(int, *++tl);
   1083 		if (len < 0 || len > RPCAUTH_MAXSIZ) {
   1084 			m_freem(mrep);
   1085 			error = EBADRPC;
   1086 			goto errout;
   1087 		}
   1088 		if (len > 0)
   1089 			nfsm_adv(nfsm_rndup(len));
   1090 	} else if (auth_type == rpc_auth_kerb) {
   1091 		switch (fxdr_unsigned(int, *tl++)) {
   1092 		case RPCAKN_FULLNAME:
   1093 			ticklen = fxdr_unsigned(int, *tl);
   1094 			*((u_int32_t *)nfsd->nfsd_authstr) = *tl;
   1095 			uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
   1096 			nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
   1097 			if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
   1098 				m_freem(mrep);
   1099 				error = EBADRPC;
   1100 				goto errout;
   1101 			}
   1102 			uio.uio_offset = 0;
   1103 			uio.uio_iov = &iov;
   1104 			uio.uio_iovcnt = 1;
   1105 			UIO_SETUP_SYSSPACE(&uio);
   1106 			iov.iov_base = (void *)&nfsd->nfsd_authstr[4];
   1107 			iov.iov_len = RPCAUTH_MAXSIZ - 4;
   1108 			nfsm_mtouio(&uio, uio.uio_resid);
   1109 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1110 			if (*tl++ != rpc_auth_kerb ||
   1111 				fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
   1112 				printf("Bad kerb verifier\n");
   1113 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
   1114 				nd->nd_procnum = NFSPROC_NOOP;
   1115 				return (0);
   1116 			}
   1117 			nfsm_dissect(cp, void *, 4 * NFSX_UNSIGNED);
   1118 			tl = (u_int32_t *)cp;
   1119 			if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
   1120 				printf("Not fullname kerb verifier\n");
   1121 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
   1122 				nd->nd_procnum = NFSPROC_NOOP;
   1123 				return (0);
   1124 			}
   1125 			cp += NFSX_UNSIGNED;
   1126 			memcpy(nfsd->nfsd_verfstr, cp, 3 * NFSX_UNSIGNED);
   1127 			nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
   1128 			nd->nd_flag |= ND_KERBFULL;
   1129 			nfsd->nfsd_flag |= NFSD_NEEDAUTH;
   1130 			break;
   1131 		case RPCAKN_NICKNAME:
   1132 			if (len != 2 * NFSX_UNSIGNED) {
   1133 				printf("Kerb nickname short\n");
   1134 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
   1135 				nd->nd_procnum = NFSPROC_NOOP;
   1136 				return (0);
   1137 			}
   1138 			nickuid = fxdr_unsigned(uid_t, *tl);
   1139 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1140 			if (*tl++ != rpc_auth_kerb ||
   1141 				fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
   1142 				printf("Kerb nick verifier bad\n");
   1143 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
   1144 				nd->nd_procnum = NFSPROC_NOOP;
   1145 				return (0);
   1146 			}
   1147 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1148 			tvin.tv_sec = *tl++;
   1149 			tvin.tv_usec = *tl;
   1150 
   1151 			LIST_FOREACH(nuidp, NUIDHASH(nfsd->nfsd_slp, nickuid),
   1152 			    nu_hash) {
   1153 				if (kauth_cred_geteuid(nuidp->nu_cr) == nickuid &&
   1154 				    (!nd->nd_nam2 ||
   1155 				     netaddr_match(NU_NETFAM(nuidp),
   1156 				      &nuidp->nu_haddr, nd->nd_nam2)))
   1157 					break;
   1158 			}
   1159 			if (!nuidp) {
   1160 				nd->nd_repstat =
   1161 					(NFSERR_AUTHERR|AUTH_REJECTCRED);
   1162 				nd->nd_procnum = NFSPROC_NOOP;
   1163 				return (0);
   1164 			}
   1165 
   1166 			/*
   1167 			 * Now, decrypt the timestamp using the session key
   1168 			 * and validate it.
   1169 			 */
   1170 #ifdef NFSKERB
   1171 			XXX
   1172 #else
   1173 			(void)tvin.tv_sec;
   1174 #endif
   1175 
   1176 			tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
   1177 			tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
   1178 			if (nuidp->nu_expire < time_second ||
   1179 			    nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
   1180 			    (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
   1181 			     nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
   1182 				nuidp->nu_expire = 0;
   1183 				nd->nd_repstat =
   1184 				    (NFSERR_AUTHERR|AUTH_REJECTVERF);
   1185 				nd->nd_procnum = NFSPROC_NOOP;
   1186 				return (0);
   1187 			}
   1188 			kauth_cred_hold(nuidp->nu_cr);
   1189 			nd->nd_cr = nuidp->nu_cr;
   1190 			nd->nd_flag |= ND_KERBNICK;
   1191 		}
   1192 	} else {
   1193 		nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
   1194 		nd->nd_procnum = NFSPROC_NOOP;
   1195 		return (0);
   1196 	}
   1197 
   1198 	nd->nd_md = md;
   1199 	nd->nd_dpos = dpos;
   1200 	KASSERT((nd->nd_cr == NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) != 0)
   1201 	     || (nd->nd_cr != NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) == 0));
   1202 	return (0);
   1203 nfsmout:
   1204 errout:
   1205 	KASSERT(error != 0);
   1206 	if (nd->nd_cr != NULL) {
   1207 		kauth_cred_free(nd->nd_cr);
   1208 		nd->nd_cr = NULL;
   1209 	}
   1210 	return (error);
   1211 }
   1212 
   1213 int
   1214 nfs_msg(struct lwp *l, const char *server, const char *msg)
   1215 {
   1216 	tpr_t tpr;
   1217 
   1218 #if 0 /* XXX nfs_timer can't block on proc_lock */
   1219 	if (l)
   1220 		tpr = tprintf_open(l->l_proc);
   1221 	else
   1222 #endif
   1223 		tpr = NULL;
   1224 	tprintf(tpr, "nfs server %s: %s\n", server, msg);
   1225 	tprintf_close(tpr);
   1226 	return (0);
   1227 }
   1228 
   1229 static struct pool nfs_srvdesc_pool;
   1230 
   1231 void
   1232 nfsdreq_init(void)
   1233 {
   1234 
   1235 	pool_init(&nfs_srvdesc_pool, sizeof(struct nfsrv_descript),
   1236 	    0, 0, 0, "nfsrvdescpl", &pool_allocator_nointr, IPL_NONE);
   1237 }
   1238 
   1239 void
   1240 nfsdreq_fini(void)
   1241 {
   1242 
   1243 	pool_destroy(&nfs_srvdesc_pool);
   1244 }
   1245 
   1246 struct nfsrv_descript *
   1247 nfsdreq_alloc(void)
   1248 {
   1249 	struct nfsrv_descript *nd;
   1250 
   1251 	nd = pool_get(&nfs_srvdesc_pool, PR_WAITOK);
   1252 	nd->nd_cr = NULL;
   1253 	return nd;
   1254 }
   1255 
   1256 void
   1257 nfsdreq_free(struct nfsrv_descript *nd)
   1258 {
   1259 	kauth_cred_t cr;
   1260 
   1261 	cr = nd->nd_cr;
   1262 	if (cr != NULL) {
   1263 		kauth_cred_free(cr);
   1264 	}
   1265 	pool_put(&nfs_srvdesc_pool, nd);
   1266 }
   1267