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