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