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