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