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