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