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