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