nfs_socket.c revision 1.93 1 /* $NetBSD: nfs_socket.c,v 1.93 2003/07/23 13:41:00 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.93 2003/07/23 13:41:00 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 goto tryagain;
1142 }
1143
1144 /*
1145 * If the File Handle was stale, invalidate the
1146 * lookup cache, just in case.
1147 */
1148 if (error == ESTALE)
1149 cache_purge(NFSTOV(np));
1150 if (nmp->nm_flag & NFSMNT_NFSV3) {
1151 *mrp = mrep;
1152 *mdp = md;
1153 *dposp = dpos;
1154 error |= NFSERR_RETERR;
1155 } else
1156 m_freem(mrep);
1157 m_freem(rep->r_mreq);
1158 free((caddr_t)rep, M_NFSREQ);
1159 return (error);
1160 }
1161
1162 #ifndef NFS_V2_ONLY
1163 /*
1164 * For nqnfs, get any lease in reply
1165 */
1166 if (nmp->nm_flag & NFSMNT_NQNFS) {
1167 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1168 if (*tl) {
1169 nqlflag = fxdr_unsigned(int, *tl);
1170 nfsm_dissect(tl, u_int32_t *, 4*NFSX_UNSIGNED);
1171 cachable = fxdr_unsigned(int, *tl++);
1172 reqtime += fxdr_unsigned(int, *tl++);
1173 if (reqtime > time.tv_sec) {
1174 frev = fxdr_hyper(tl);
1175 nqnfs_clientlease(nmp, np, nqlflag,
1176 cachable, reqtime, frev);
1177 }
1178 }
1179 }
1180 #endif
1181 *mrp = mrep;
1182 *mdp = md;
1183 *dposp = dpos;
1184 m_freem(rep->r_mreq);
1185 FREE((caddr_t)rep, M_NFSREQ);
1186 return (0);
1187 }
1188 m_freem(mrep);
1189 error = EPROTONOSUPPORT;
1190 nfsmout:
1191 m_freem(rep->r_mreq);
1192 free((caddr_t)rep, M_NFSREQ);
1193 return (error);
1194 }
1195 #endif /* NFS */
1196
1197 /*
1198 * Generate the rpc reply header
1199 * siz arg. is used to decide if adding a cluster is worthwhile
1200 */
1201 int
1202 nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp)
1203 int siz;
1204 struct nfsrv_descript *nd;
1205 struct nfssvc_sock *slp;
1206 int err;
1207 int cache;
1208 u_quad_t *frev;
1209 struct mbuf **mrq;
1210 struct mbuf **mbp;
1211 caddr_t *bposp;
1212 {
1213 u_int32_t *tl;
1214 struct mbuf *mreq;
1215 caddr_t bpos;
1216 struct mbuf *mb;
1217
1218 mreq = m_gethdr(M_WAIT, MT_DATA);
1219 MCLAIM(mreq, &nfs_mowner);
1220 mb = mreq;
1221 /*
1222 * If this is a big reply, use a cluster else
1223 * try and leave leading space for the lower level headers.
1224 */
1225 siz += RPC_REPLYSIZ;
1226 if (siz >= max_datalen) {
1227 m_clget(mreq, M_WAIT);
1228 } else
1229 mreq->m_data += max_hdr;
1230 tl = mtod(mreq, u_int32_t *);
1231 mreq->m_len = 6 * NFSX_UNSIGNED;
1232 bpos = ((caddr_t)tl) + mreq->m_len;
1233 *tl++ = txdr_unsigned(nd->nd_retxid);
1234 *tl++ = rpc_reply;
1235 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1236 *tl++ = rpc_msgdenied;
1237 if (err & NFSERR_AUTHERR) {
1238 *tl++ = rpc_autherr;
1239 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1240 mreq->m_len -= NFSX_UNSIGNED;
1241 bpos -= NFSX_UNSIGNED;
1242 } else {
1243 *tl++ = rpc_mismatch;
1244 *tl++ = txdr_unsigned(RPC_VER2);
1245 *tl = txdr_unsigned(RPC_VER2);
1246 }
1247 } else {
1248 *tl++ = rpc_msgaccepted;
1249
1250 /*
1251 * For Kerberos authentication, we must send the nickname
1252 * verifier back, otherwise just RPCAUTH_NULL.
1253 */
1254 if (nd->nd_flag & ND_KERBFULL) {
1255 struct nfsuid *nuidp;
1256 struct timeval ktvin, ktvout;
1257
1258 LIST_FOREACH(nuidp, NUIDHASH(slp, nd->nd_cr.cr_uid),
1259 nu_hash) {
1260 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
1261 (!nd->nd_nam2 || netaddr_match(
1262 NU_NETFAM(nuidp), &nuidp->nu_haddr,
1263 nd->nd_nam2)))
1264 break;
1265 }
1266 if (nuidp) {
1267 ktvin.tv_sec =
1268 txdr_unsigned(nuidp->nu_timestamp.tv_sec
1269 - 1);
1270 ktvin.tv_usec =
1271 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1272
1273 /*
1274 * Encrypt the timestamp in ecb mode using the
1275 * session key.
1276 */
1277 #ifdef NFSKERB
1278 XXX
1279 #endif
1280
1281 *tl++ = rpc_auth_kerb;
1282 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1283 *tl = ktvout.tv_sec;
1284 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1285 *tl++ = ktvout.tv_usec;
1286 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
1287 } else {
1288 *tl++ = 0;
1289 *tl++ = 0;
1290 }
1291 } else {
1292 *tl++ = 0;
1293 *tl++ = 0;
1294 }
1295 switch (err) {
1296 case EPROGUNAVAIL:
1297 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1298 break;
1299 case EPROGMISMATCH:
1300 *tl = txdr_unsigned(RPC_PROGMISMATCH);
1301 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1302 if (nd->nd_flag & ND_NQNFS) {
1303 *tl++ = txdr_unsigned(3);
1304 *tl = txdr_unsigned(3);
1305 } else {
1306 *tl++ = txdr_unsigned(2);
1307 *tl = txdr_unsigned(3);
1308 }
1309 break;
1310 case EPROCUNAVAIL:
1311 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1312 break;
1313 case EBADRPC:
1314 *tl = txdr_unsigned(RPC_GARBAGE);
1315 break;
1316 default:
1317 *tl = 0;
1318 if (err != NFSERR_RETVOID) {
1319 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1320 if (err)
1321 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1322 else
1323 *tl = 0;
1324 }
1325 break;
1326 };
1327 }
1328
1329 /*
1330 * For nqnfs, piggyback lease as requested.
1331 */
1332 if ((nd->nd_flag & ND_NQNFS) && err == 0) {
1333 if (nd->nd_flag & ND_LEASE) {
1334 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1335 *tl++ = txdr_unsigned(nd->nd_flag & ND_LEASE);
1336 *tl++ = txdr_unsigned(cache);
1337 *tl++ = txdr_unsigned(nd->nd_duration);
1338 txdr_hyper(*frev, tl);
1339 } else {
1340 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1341 *tl = 0;
1342 }
1343 }
1344 if (mrq != NULL)
1345 *mrq = mreq;
1346 *mbp = mb;
1347 *bposp = bpos;
1348 if (err != 0 && err != NFSERR_RETVOID)
1349 nfsstats.srvrpc_errs++;
1350 return (0);
1351 }
1352
1353 /*
1354 * Nfs timer routine
1355 * Scan the nfsreq list and retranmit any requests that have timed out
1356 * To avoid retransmission attempts on STREAM sockets (in the future) make
1357 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1358 */
1359 void
1360 nfs_timer(arg)
1361 void *arg; /* never used */
1362 {
1363 struct nfsreq *rep;
1364 struct mbuf *m;
1365 struct socket *so;
1366 struct nfsmount *nmp;
1367 int timeo;
1368 int s, error;
1369 #ifdef NFSSERVER
1370 struct nfssvc_sock *slp;
1371 static long lasttime = 0;
1372 u_quad_t cur_usec;
1373 #endif
1374
1375 s = splsoftnet();
1376 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1377 nmp = rep->r_nmp;
1378 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1379 continue;
1380 if (nfs_sigintr(nmp, rep, rep->r_procp)) {
1381 rep->r_flags |= R_SOFTTERM;
1382 continue;
1383 }
1384 if (rep->r_rtt >= 0) {
1385 rep->r_rtt++;
1386 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1387 timeo = nmp->nm_timeo;
1388 else
1389 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1390 if (nmp->nm_timeouts > 0)
1391 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1392 if (rep->r_rtt <= timeo)
1393 continue;
1394 if (nmp->nm_timeouts < 8)
1395 nmp->nm_timeouts++;
1396 }
1397 /*
1398 * Check for server not responding
1399 */
1400 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1401 rep->r_rexmit > nmp->nm_deadthresh) {
1402 nfs_msg(rep->r_procp,
1403 nmp->nm_mountp->mnt_stat.f_mntfromname,
1404 "not responding");
1405 rep->r_flags |= R_TPRINTFMSG;
1406 }
1407 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1408 nfsstats.rpctimeouts++;
1409 rep->r_flags |= R_SOFTTERM;
1410 continue;
1411 }
1412 if (nmp->nm_sotype != SOCK_DGRAM) {
1413 if (++rep->r_rexmit > NFS_MAXREXMIT)
1414 rep->r_rexmit = NFS_MAXREXMIT;
1415 continue;
1416 }
1417 if ((so = nmp->nm_so) == NULL)
1418 continue;
1419
1420 /*
1421 * If there is enough space and the window allows..
1422 * Resend it
1423 * Set r_rtt to -1 in case we fail to send it now.
1424 */
1425 rep->r_rtt = -1;
1426 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1427 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1428 (rep->r_flags & R_SENT) ||
1429 nmp->nm_sent < nmp->nm_cwnd) &&
1430 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1431 if (so->so_state & SS_ISCONNECTED)
1432 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1433 (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
1434 else
1435 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1436 nmp->nm_nam, (struct mbuf *)0, (struct proc *)0);
1437 if (error) {
1438 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
1439 #ifdef DEBUG
1440 printf("nfs_timer: ignoring error %d\n",
1441 error);
1442 #endif
1443 so->so_error = 0;
1444 }
1445 } else {
1446 /*
1447 * Iff first send, start timing
1448 * else turn timing off, backoff timer
1449 * and divide congestion window by 2.
1450 */
1451 if (rep->r_flags & R_SENT) {
1452 rep->r_flags &= ~R_TIMING;
1453 if (++rep->r_rexmit > NFS_MAXREXMIT)
1454 rep->r_rexmit = NFS_MAXREXMIT;
1455 nmp->nm_cwnd >>= 1;
1456 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1457 nmp->nm_cwnd = NFS_CWNDSCALE;
1458 nfsstats.rpcretries++;
1459 } else {
1460 rep->r_flags |= R_SENT;
1461 nmp->nm_sent += NFS_CWNDSCALE;
1462 }
1463 rep->r_rtt = 0;
1464 }
1465 }
1466 }
1467
1468 #ifdef NFSSERVER
1469 /*
1470 * Call the nqnfs server timer once a second to handle leases.
1471 */
1472 if (lasttime != time.tv_sec) {
1473 lasttime = time.tv_sec;
1474 nqnfs_serverd();
1475 }
1476
1477 /*
1478 * Scan the write gathering queues for writes that need to be
1479 * completed now.
1480 */
1481 cur_usec = (u_quad_t)time.tv_sec * 1000000 + (u_quad_t)time.tv_usec;
1482 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
1483 if (LIST_FIRST(&slp->ns_tq) &&
1484 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec)
1485 nfsrv_wakenfsd(slp);
1486 }
1487 #endif /* NFSSERVER */
1488 splx(s);
1489 callout_reset(&nfs_timer_ch, nfs_ticks, nfs_timer, NULL);
1490 }
1491
1492 /*ARGSUSED*/
1493 void
1494 nfs_exit(p, v)
1495 struct proc *p;
1496 void *v;
1497 {
1498 struct nfsreq *rp;
1499 int s = splsoftnet();
1500
1501 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
1502 if (rp->r_procp == p)
1503 TAILQ_REMOVE(&nfs_reqq, rp, r_chain);
1504 }
1505 splx(s);
1506 }
1507
1508 /*
1509 * Test for a termination condition pending on the process.
1510 * This is used for NFSMNT_INT mounts.
1511 */
1512 int
1513 nfs_sigintr(nmp, rep, p)
1514 struct nfsmount *nmp;
1515 struct nfsreq *rep;
1516 struct proc *p;
1517 {
1518 sigset_t ss;
1519
1520 if (rep && (rep->r_flags & R_SOFTTERM))
1521 return (EINTR);
1522 if (!(nmp->nm_flag & NFSMNT_INT))
1523 return (0);
1524 if (p) {
1525 sigpending1(p, &ss);
1526 #if 0
1527 sigminusset(&p->p_sigctx.ps_sigignore, &ss);
1528 #endif
1529 if (sigismember(&ss, SIGINT) || sigismember(&ss, SIGTERM) ||
1530 sigismember(&ss, SIGKILL) || sigismember(&ss, SIGHUP) ||
1531 sigismember(&ss, SIGQUIT))
1532 return (EINTR);
1533 }
1534 return (0);
1535 }
1536
1537 /*
1538 * Lock a socket against others.
1539 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1540 * and also to avoid race conditions between the processes with nfs requests
1541 * in progress when a reconnect is necessary.
1542 */
1543 int
1544 nfs_sndlock(flagp, rep)
1545 int *flagp;
1546 struct nfsreq *rep;
1547 {
1548 struct proc *p;
1549 int slpflag = 0, slptimeo = 0;
1550
1551 if (rep) {
1552 p = rep->r_procp;
1553 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1554 slpflag = PCATCH;
1555 } else
1556 p = (struct proc *)0;
1557 while (*flagp & NFSMNT_SNDLOCK) {
1558 if (nfs_sigintr(rep->r_nmp, rep, p))
1559 return (EINTR);
1560 *flagp |= NFSMNT_WANTSND;
1561 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck",
1562 slptimeo);
1563 if (slpflag == PCATCH) {
1564 slpflag = 0;
1565 slptimeo = 2 * hz;
1566 }
1567 }
1568 *flagp |= NFSMNT_SNDLOCK;
1569 return (0);
1570 }
1571
1572 /*
1573 * Unlock the stream socket for others.
1574 */
1575 void
1576 nfs_sndunlock(flagp)
1577 int *flagp;
1578 {
1579
1580 if ((*flagp & NFSMNT_SNDLOCK) == 0)
1581 panic("nfs sndunlock");
1582 *flagp &= ~NFSMNT_SNDLOCK;
1583 if (*flagp & NFSMNT_WANTSND) {
1584 *flagp &= ~NFSMNT_WANTSND;
1585 wakeup((caddr_t)flagp);
1586 }
1587 }
1588
1589 int
1590 nfs_rcvlock(rep)
1591 struct nfsreq *rep;
1592 {
1593 struct nfsmount *nmp = rep->r_nmp;
1594 int *flagp = &nmp->nm_iflag;
1595 int slpflag, slptimeo = 0;
1596 int error = 0;
1597
1598 if (*flagp & NFSMNT_DISMNT)
1599 return EIO;
1600
1601 if (*flagp & NFSMNT_INT)
1602 slpflag = PCATCH;
1603 else
1604 slpflag = 0;
1605 simple_lock(&nmp->nm_slock);
1606 while (*flagp & NFSMNT_RCVLOCK) {
1607 if (nfs_sigintr(rep->r_nmp, rep, rep->r_procp)) {
1608 error = EINTR;
1609 goto quit;
1610 }
1611 *flagp |= NFSMNT_WANTRCV;
1612 nmp->nm_waiters++;
1613 (void) ltsleep(flagp, slpflag | (PZERO - 1), "nfsrcvlk",
1614 slptimeo, &nmp->nm_slock);
1615 nmp->nm_waiters--;
1616 if (*flagp & NFSMNT_DISMNT) {
1617 wakeup(&nmp->nm_waiters);
1618 error = EIO;
1619 goto quit;
1620 }
1621 /* If our reply was received while we were sleeping,
1622 * then just return without taking the lock to avoid a
1623 * situation where a single iod could 'capture' the
1624 * receive lock.
1625 */
1626 if (rep->r_mrep != NULL) {
1627 error = EALREADY;
1628 goto quit;
1629 }
1630 if (slpflag == PCATCH) {
1631 slpflag = 0;
1632 slptimeo = 2 * hz;
1633 }
1634 }
1635 *flagp |= NFSMNT_RCVLOCK;
1636 quit:
1637 simple_unlock(&nmp->nm_slock);
1638 return error;
1639 }
1640
1641 /*
1642 * Unlock the stream socket for others.
1643 */
1644 void
1645 nfs_rcvunlock(nmp)
1646 struct nfsmount *nmp;
1647 {
1648 int *flagp = &nmp->nm_iflag;
1649
1650 simple_lock(&nmp->nm_slock);
1651 if ((*flagp & NFSMNT_RCVLOCK) == 0)
1652 panic("nfs rcvunlock");
1653 *flagp &= ~NFSMNT_RCVLOCK;
1654 if (*flagp & NFSMNT_WANTRCV) {
1655 *flagp &= ~NFSMNT_WANTRCV;
1656 wakeup((caddr_t)flagp);
1657 }
1658 simple_unlock(&nmp->nm_slock);
1659 }
1660
1661 /*
1662 * Parse an RPC request
1663 * - verify it
1664 * - fill in the cred struct.
1665 */
1666 int
1667 nfs_getreq(nd, nfsd, has_header)
1668 struct nfsrv_descript *nd;
1669 struct nfsd *nfsd;
1670 int has_header;
1671 {
1672 int len, i;
1673 u_int32_t *tl;
1674 int32_t t1;
1675 struct uio uio;
1676 struct iovec iov;
1677 caddr_t dpos, cp2, cp;
1678 u_int32_t nfsvers, auth_type;
1679 uid_t nickuid;
1680 int error = 0, nqnfs = 0, ticklen;
1681 struct mbuf *mrep, *md;
1682 struct nfsuid *nuidp;
1683 struct timeval tvin, tvout;
1684
1685 mrep = nd->nd_mrep;
1686 md = nd->nd_md;
1687 dpos = nd->nd_dpos;
1688 if (has_header) {
1689 nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1690 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
1691 if (*tl++ != rpc_call) {
1692 m_freem(mrep);
1693 return (EBADRPC);
1694 }
1695 } else
1696 nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
1697 nd->nd_repstat = 0;
1698 nd->nd_flag = 0;
1699 if (*tl++ != rpc_vers) {
1700 nd->nd_repstat = ERPCMISMATCH;
1701 nd->nd_procnum = NFSPROC_NOOP;
1702 return (0);
1703 }
1704 if (*tl != nfs_prog) {
1705 if (*tl == nqnfs_prog)
1706 nqnfs++;
1707 else {
1708 nd->nd_repstat = EPROGUNAVAIL;
1709 nd->nd_procnum = NFSPROC_NOOP;
1710 return (0);
1711 }
1712 }
1713 tl++;
1714 nfsvers = fxdr_unsigned(u_int32_t, *tl++);
1715 if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) ||
1716 (nfsvers != NQNFS_VER3 && nqnfs)) {
1717 nd->nd_repstat = EPROGMISMATCH;
1718 nd->nd_procnum = NFSPROC_NOOP;
1719 return (0);
1720 }
1721 if (nqnfs)
1722 nd->nd_flag = (ND_NFSV3 | ND_NQNFS);
1723 else if (nfsvers == NFS_VER3)
1724 nd->nd_flag = ND_NFSV3;
1725 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
1726 if (nd->nd_procnum == NFSPROC_NULL)
1727 return (0);
1728 if (nd->nd_procnum >= NFS_NPROCS ||
1729 (!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
1730 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
1731 nd->nd_repstat = EPROCUNAVAIL;
1732 nd->nd_procnum = NFSPROC_NOOP;
1733 return (0);
1734 }
1735 if ((nd->nd_flag & ND_NFSV3) == 0)
1736 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
1737 auth_type = *tl++;
1738 len = fxdr_unsigned(int, *tl++);
1739 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1740 m_freem(mrep);
1741 return (EBADRPC);
1742 }
1743
1744 nd->nd_flag &= ~ND_KERBAUTH;
1745 /*
1746 * Handle auth_unix or auth_kerb.
1747 */
1748 if (auth_type == rpc_auth_unix) {
1749 len = fxdr_unsigned(int, *++tl);
1750 if (len < 0 || len > NFS_MAXNAMLEN) {
1751 m_freem(mrep);
1752 return (EBADRPC);
1753 }
1754 nfsm_adv(nfsm_rndup(len));
1755 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1756 memset((caddr_t)&nd->nd_cr, 0, sizeof (struct ucred));
1757 nd->nd_cr.cr_ref = 1;
1758 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
1759 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
1760 len = fxdr_unsigned(int, *tl);
1761 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
1762 m_freem(mrep);
1763 return (EBADRPC);
1764 }
1765 nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
1766 for (i = 0; i < len; i++)
1767 if (i < NGROUPS)
1768 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
1769 else
1770 tl++;
1771 nd->nd_cr.cr_ngroups = (len > NGROUPS) ? NGROUPS : len;
1772 if (nd->nd_cr.cr_ngroups > 1)
1773 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
1774 len = fxdr_unsigned(int, *++tl);
1775 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1776 m_freem(mrep);
1777 return (EBADRPC);
1778 }
1779 if (len > 0)
1780 nfsm_adv(nfsm_rndup(len));
1781 } else if (auth_type == rpc_auth_kerb) {
1782 switch (fxdr_unsigned(int, *tl++)) {
1783 case RPCAKN_FULLNAME:
1784 ticklen = fxdr_unsigned(int, *tl);
1785 *((u_int32_t *)nfsd->nfsd_authstr) = *tl;
1786 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
1787 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
1788 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
1789 m_freem(mrep);
1790 return (EBADRPC);
1791 }
1792 uio.uio_offset = 0;
1793 uio.uio_iov = &iov;
1794 uio.uio_iovcnt = 1;
1795 uio.uio_segflg = UIO_SYSSPACE;
1796 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
1797 iov.iov_len = RPCAUTH_MAXSIZ - 4;
1798 nfsm_mtouio(&uio, uio.uio_resid);
1799 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1800 if (*tl++ != rpc_auth_kerb ||
1801 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
1802 printf("Bad kerb verifier\n");
1803 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1804 nd->nd_procnum = NFSPROC_NOOP;
1805 return (0);
1806 }
1807 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED);
1808 tl = (u_int32_t *)cp;
1809 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
1810 printf("Not fullname kerb verifier\n");
1811 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1812 nd->nd_procnum = NFSPROC_NOOP;
1813 return (0);
1814 }
1815 cp += NFSX_UNSIGNED;
1816 memcpy(nfsd->nfsd_verfstr, cp, 3 * NFSX_UNSIGNED);
1817 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
1818 nd->nd_flag |= ND_KERBFULL;
1819 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
1820 break;
1821 case RPCAKN_NICKNAME:
1822 if (len != 2 * NFSX_UNSIGNED) {
1823 printf("Kerb nickname short\n");
1824 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
1825 nd->nd_procnum = NFSPROC_NOOP;
1826 return (0);
1827 }
1828 nickuid = fxdr_unsigned(uid_t, *tl);
1829 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1830 if (*tl++ != rpc_auth_kerb ||
1831 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
1832 printf("Kerb nick verifier bad\n");
1833 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1834 nd->nd_procnum = NFSPROC_NOOP;
1835 return (0);
1836 }
1837 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1838 tvin.tv_sec = *tl++;
1839 tvin.tv_usec = *tl;
1840
1841 LIST_FOREACH(nuidp, NUIDHASH(nfsd->nfsd_slp, nickuid),
1842 nu_hash) {
1843 if (nuidp->nu_cr.cr_uid == nickuid &&
1844 (!nd->nd_nam2 ||
1845 netaddr_match(NU_NETFAM(nuidp),
1846 &nuidp->nu_haddr, nd->nd_nam2)))
1847 break;
1848 }
1849 if (!nuidp) {
1850 nd->nd_repstat =
1851 (NFSERR_AUTHERR|AUTH_REJECTCRED);
1852 nd->nd_procnum = NFSPROC_NOOP;
1853 return (0);
1854 }
1855
1856 /*
1857 * Now, decrypt the timestamp using the session key
1858 * and validate it.
1859 */
1860 #ifdef NFSKERB
1861 XXX
1862 #endif
1863
1864 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
1865 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
1866 if (nuidp->nu_expire < time.tv_sec ||
1867 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
1868 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
1869 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
1870 nuidp->nu_expire = 0;
1871 nd->nd_repstat =
1872 (NFSERR_AUTHERR|AUTH_REJECTVERF);
1873 nd->nd_procnum = NFSPROC_NOOP;
1874 return (0);
1875 }
1876 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
1877 nd->nd_flag |= ND_KERBNICK;
1878 };
1879 } else {
1880 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
1881 nd->nd_procnum = NFSPROC_NOOP;
1882 return (0);
1883 }
1884
1885 /*
1886 * For nqnfs, get piggybacked lease request.
1887 */
1888 if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) {
1889 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1890 nd->nd_flag |= fxdr_unsigned(int, *tl);
1891 if (nd->nd_flag & ND_LEASE) {
1892 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1893 nd->nd_duration = fxdr_unsigned(u_int32_t, *tl);
1894 } else
1895 nd->nd_duration = NQ_MINLEASE;
1896 } else
1897 nd->nd_duration = NQ_MINLEASE;
1898 nd->nd_md = md;
1899 nd->nd_dpos = dpos;
1900 return (0);
1901 nfsmout:
1902 return (error);
1903 }
1904
1905 int
1906 nfs_msg(p, server, msg)
1907 struct proc *p;
1908 char *server, *msg;
1909 {
1910 tpr_t tpr;
1911
1912 if (p)
1913 tpr = tprintf_open(p);
1914 else
1915 tpr = NULL;
1916 tprintf(tpr, "nfs server %s: %s\n", server, msg);
1917 tprintf_close(tpr);
1918 return (0);
1919 }
1920
1921 #ifdef NFSSERVER
1922 int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
1923 struct nfssvc_sock *, struct proc *,
1924 struct mbuf **)) = {
1925 nfsrv_null,
1926 nfsrv_getattr,
1927 nfsrv_setattr,
1928 nfsrv_lookup,
1929 nfsrv3_access,
1930 nfsrv_readlink,
1931 nfsrv_read,
1932 nfsrv_write,
1933 nfsrv_create,
1934 nfsrv_mkdir,
1935 nfsrv_symlink,
1936 nfsrv_mknod,
1937 nfsrv_remove,
1938 nfsrv_rmdir,
1939 nfsrv_rename,
1940 nfsrv_link,
1941 nfsrv_readdir,
1942 nfsrv_readdirplus,
1943 nfsrv_statfs,
1944 nfsrv_fsinfo,
1945 nfsrv_pathconf,
1946 nfsrv_commit,
1947 nqnfsrv_getlease,
1948 nqnfsrv_vacated,
1949 nfsrv_noop,
1950 nfsrv_noop
1951 };
1952
1953 /*
1954 * Socket upcall routine for the nfsd sockets.
1955 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
1956 * Essentially do as much as possible non-blocking, else punt and it will
1957 * be called with M_WAIT from an nfsd.
1958 */
1959 void
1960 nfsrv_rcv(so, arg, waitflag)
1961 struct socket *so;
1962 caddr_t arg;
1963 int waitflag;
1964 {
1965 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
1966 struct mbuf *m;
1967 struct mbuf *mp, *nam;
1968 struct uio auio;
1969 int flags, error;
1970
1971 if ((slp->ns_flag & SLP_VALID) == 0)
1972 return;
1973 #ifdef notdef
1974 /*
1975 * Define this to test for nfsds handling this under heavy load.
1976 */
1977 if (waitflag == M_DONTWAIT) {
1978 slp->ns_flag |= SLP_NEEDQ; goto dorecs;
1979 }
1980 #endif
1981 auio.uio_procp = NULL;
1982 if (so->so_type == SOCK_STREAM) {
1983 /*
1984 * If there are already records on the queue, defer soreceive()
1985 * to an nfsd so that there is feedback to the TCP layer that
1986 * the nfs servers are heavily loaded.
1987 */
1988 if (slp->ns_rec && waitflag == M_DONTWAIT) {
1989 slp->ns_flag |= SLP_NEEDQ;
1990 goto dorecs;
1991 }
1992
1993 /*
1994 * Do soreceive().
1995 */
1996 auio.uio_resid = 1000000000;
1997 flags = MSG_DONTWAIT;
1998 error = (*so->so_receive)(so, &nam, &auio, &mp, (struct mbuf **)0, &flags);
1999 if (error || mp == (struct mbuf *)0) {
2000 if (error == EWOULDBLOCK)
2001 slp->ns_flag |= SLP_NEEDQ;
2002 else
2003 slp->ns_flag |= SLP_DISCONN;
2004 goto dorecs;
2005 }
2006 m = mp;
2007 if (slp->ns_rawend) {
2008 slp->ns_rawend->m_next = m;
2009 slp->ns_cc += 1000000000 - auio.uio_resid;
2010 } else {
2011 slp->ns_raw = m;
2012 slp->ns_cc = 1000000000 - auio.uio_resid;
2013 }
2014 while (m->m_next)
2015 m = m->m_next;
2016 slp->ns_rawend = m;
2017
2018 /*
2019 * Now try and parse record(s) out of the raw stream data.
2020 */
2021 error = nfsrv_getstream(slp, waitflag);
2022 if (error) {
2023 if (error == EPERM)
2024 slp->ns_flag |= SLP_DISCONN;
2025 else
2026 slp->ns_flag |= SLP_NEEDQ;
2027 }
2028 } else {
2029 do {
2030 auio.uio_resid = 1000000000;
2031 flags = MSG_DONTWAIT;
2032 error = (*so->so_receive)(so, &nam, &auio, &mp,
2033 (struct mbuf **)0, &flags);
2034 if (mp) {
2035 if (nam) {
2036 m = nam;
2037 m->m_next = mp;
2038 } else
2039 m = mp;
2040 if (slp->ns_recend)
2041 slp->ns_recend->m_nextpkt = m;
2042 else
2043 slp->ns_rec = m;
2044 slp->ns_recend = m;
2045 m->m_nextpkt = (struct mbuf *)0;
2046 }
2047 if (error) {
2048 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
2049 && error != EWOULDBLOCK) {
2050 slp->ns_flag |= SLP_DISCONN;
2051 goto dorecs;
2052 }
2053 }
2054 } while (mp);
2055 }
2056
2057 /*
2058 * Now try and process the request records, non-blocking.
2059 */
2060 dorecs:
2061 if (waitflag == M_DONTWAIT &&
2062 (slp->ns_rec || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN))))
2063 nfsrv_wakenfsd(slp);
2064 }
2065
2066 /*
2067 * Try and extract an RPC request from the mbuf data list received on a
2068 * stream socket. The "waitflag" argument indicates whether or not it
2069 * can sleep.
2070 */
2071 int
2072 nfsrv_getstream(slp, waitflag)
2073 struct nfssvc_sock *slp;
2074 int waitflag;
2075 {
2076 struct mbuf *m, **mpp;
2077 struct mbuf *recm;
2078 u_int32_t recmark;
2079
2080 if (slp->ns_flag & SLP_GETSTREAM)
2081 panic("nfs getstream");
2082 slp->ns_flag |= SLP_GETSTREAM;
2083 for (;;) {
2084 if (slp->ns_reclen == 0) {
2085 if (slp->ns_cc < NFSX_UNSIGNED) {
2086 slp->ns_flag &= ~SLP_GETSTREAM;
2087 return (0);
2088 }
2089 m = slp->ns_raw;
2090 m_copydata(m, 0, NFSX_UNSIGNED, (caddr_t)&recmark);
2091 m_adj(m, NFSX_UNSIGNED);
2092 slp->ns_cc -= NFSX_UNSIGNED;
2093 recmark = ntohl(recmark);
2094 slp->ns_reclen = recmark & ~0x80000000;
2095 if (recmark & 0x80000000)
2096 slp->ns_flag |= SLP_LASTFRAG;
2097 else
2098 slp->ns_flag &= ~SLP_LASTFRAG;
2099 if (slp->ns_reclen > NFS_MAXPACKET) {
2100 slp->ns_flag &= ~SLP_GETSTREAM;
2101 return (EPERM);
2102 }
2103 }
2104
2105 /*
2106 * Now get the record part.
2107 *
2108 * Note that slp->ns_reclen may be 0. Linux sometimes
2109 * generates 0-length records.
2110 */
2111 if (slp->ns_cc == slp->ns_reclen) {
2112 recm = slp->ns_raw;
2113 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
2114 slp->ns_cc = slp->ns_reclen = 0;
2115 } else if (slp->ns_cc > slp->ns_reclen) {
2116 recm = slp->ns_raw;
2117 m = m_split(recm, slp->ns_reclen, waitflag);
2118 if (m == NULL) {
2119 slp->ns_flag &= ~SLP_GETSTREAM;
2120 return (EWOULDBLOCK);
2121 }
2122 m_claim(recm, &nfs_mowner);
2123 slp->ns_raw = m;
2124 if (m->m_next == NULL)
2125 slp->ns_rawend = m;
2126 slp->ns_cc -= slp->ns_reclen;
2127 slp->ns_reclen = 0;
2128 } else {
2129 slp->ns_flag &= ~SLP_GETSTREAM;
2130 return (0);
2131 }
2132
2133 /*
2134 * Accumulate the fragments into a record.
2135 */
2136 mpp = &slp->ns_frag;
2137 while (*mpp)
2138 mpp = &((*mpp)->m_next);
2139 *mpp = recm;
2140 if (slp->ns_flag & SLP_LASTFRAG) {
2141 if (slp->ns_recend)
2142 slp->ns_recend->m_nextpkt = slp->ns_frag;
2143 else
2144 slp->ns_rec = slp->ns_frag;
2145 slp->ns_recend = slp->ns_frag;
2146 slp->ns_frag = (struct mbuf *)0;
2147 }
2148 }
2149 }
2150
2151 /*
2152 * Parse an RPC header.
2153 */
2154 int
2155 nfsrv_dorec(slp, nfsd, ndp)
2156 struct nfssvc_sock *slp;
2157 struct nfsd *nfsd;
2158 struct nfsrv_descript **ndp;
2159 {
2160 struct mbuf *m, *nam;
2161 struct nfsrv_descript *nd;
2162 int error;
2163
2164 *ndp = NULL;
2165 if ((slp->ns_flag & SLP_VALID) == 0 ||
2166 (m = slp->ns_rec) == (struct mbuf *)0)
2167 return (ENOBUFS);
2168 slp->ns_rec = m->m_nextpkt;
2169 if (slp->ns_rec)
2170 m->m_nextpkt = (struct mbuf *)0;
2171 else
2172 slp->ns_recend = (struct mbuf *)0;
2173 if (m->m_type == MT_SONAME) {
2174 nam = m;
2175 m = m->m_next;
2176 nam->m_next = NULL;
2177 } else
2178 nam = NULL;
2179 nd = pool_get(&nfs_srvdesc_pool, PR_WAITOK);
2180 nd->nd_md = nd->nd_mrep = m;
2181 nd->nd_nam2 = nam;
2182 nd->nd_dpos = mtod(m, caddr_t);
2183 error = nfs_getreq(nd, nfsd, TRUE);
2184 if (error) {
2185 m_freem(nam);
2186 pool_put(&nfs_srvdesc_pool, nd);
2187 return (error);
2188 }
2189 *ndp = nd;
2190 nfsd->nfsd_nd = nd;
2191 return (0);
2192 }
2193
2194
2195 /*
2196 * Search for a sleeping nfsd and wake it up.
2197 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2198 * running nfsds will go look for the work in the nfssvc_sock list.
2199 */
2200 void
2201 nfsrv_wakenfsd(slp)
2202 struct nfssvc_sock *slp;
2203 {
2204 struct nfsd *nd;
2205
2206 if ((slp->ns_flag & SLP_VALID) == 0)
2207 return;
2208 simple_lock(&nfsd_slock);
2209 if (slp->ns_flag & SLP_DOREC) {
2210 simple_unlock(&nfsd_slock);
2211 return;
2212 }
2213 nd = SLIST_FIRST(&nfsd_idle_head);
2214 if (nd) {
2215 SLIST_REMOVE_HEAD(&nfsd_idle_head, nfsd_idle);
2216 simple_unlock(&nfsd_slock);
2217
2218 KASSERT(nd->nfsd_flag & NFSD_WAITING);
2219 nd->nfsd_flag &= ~NFSD_WAITING;
2220 if (nd->nfsd_slp)
2221 panic("nfsd wakeup");
2222 slp->ns_sref++;
2223 nd->nfsd_slp = slp;
2224 wakeup(nd);
2225 return;
2226 }
2227 slp->ns_flag |= SLP_DOREC;
2228 nfsd_head_flag |= NFSD_CHECKSLP;
2229 TAILQ_INSERT_TAIL(&nfssvc_sockpending, slp, ns_pending);
2230 simple_unlock(&nfsd_slock);
2231 }
2232 #endif /* NFSSERVER */
2233