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