nfs_socket.c revision 1.92.2.4 1 /* $NetBSD: nfs_socket.c,v 1.92.2.4 2004/08/25 06:59:14 skrll 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.92.2.4 2004/08/25 06:59:14 skrll 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, l)
158 struct nfsmount *nmp;
159 struct nfsreq *rep;
160 struct lwp *l;
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, l);
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, &lwp0);
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, &lwp0);
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, l);
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_lwp)) != 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, l)
341 struct nfsreq *rep;
342 struct lwp *l;
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, l)) != 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, l)
434 struct socket *so;
435 struct mbuf *nam;
436 struct mbuf *top;
437 struct nfsreq *rep;
438 struct lwp *l;
439 {
440 struct mbuf *sendnam;
441 int error, soflags, flags;
442
443 /* XXX nfs_doio()/nfs_request() calls with rep->r_lwp == NULL */
444 if (l == NULL && rep->r_lwp == NULL)
445 l = curlwp;
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, l);
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, l)
532 struct nfsreq *rep;
533 struct mbuf **aname;
534 struct mbuf **mp;
535 struct lwp *l;
536 {
537 struct proc *p;
538 struct socket *so;
539 struct uio auio;
540 struct iovec aio;
541 struct mbuf *m;
542 struct mbuf *control;
543 u_int32_t len;
544 struct mbuf **getnam;
545 int error, sotype, rcvflg;
546
547 p = l->l_proc;
548 /*
549 * Set up arguments for soreceive()
550 */
551 *mp = (struct mbuf *)0;
552 *aname = (struct mbuf *)0;
553 sotype = rep->r_nmp->nm_sotype;
554
555 /*
556 * For reliable protocols, lock against other senders/receivers
557 * in case a reconnect is necessary.
558 * For SOCK_STREAM, first get the Record Mark to find out how much
559 * more there is to get.
560 * We must lock the socket against other receivers
561 * until we have an entire rpc request/reply.
562 */
563 if (sotype != SOCK_DGRAM) {
564 error = nfs_sndlock(&rep->r_nmp->nm_iflag, rep);
565 if (error)
566 return (error);
567 tryagain:
568 /*
569 * Check for fatal errors and resending request.
570 */
571 /*
572 * Ugh: If a reconnect attempt just happened, nm_so
573 * would have changed. NULL indicates a failed
574 * attempt that has essentially shut down this
575 * mount point.
576 */
577 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
578 nfs_sndunlock(&rep->r_nmp->nm_iflag);
579 return (EINTR);
580 }
581 so = rep->r_nmp->nm_so;
582 if (!so) {
583 error = nfs_reconnect(rep, l);
584 if (error) {
585 nfs_sndunlock(&rep->r_nmp->nm_iflag);
586 return (error);
587 }
588 goto tryagain;
589 }
590 while (rep->r_flags & R_MUSTRESEND) {
591 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
592 nfsstats.rpcretries++;
593 rep->r_rtt = 0;
594 rep->r_flags &= ~R_TIMING;
595 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep, l);
596 if (error) {
597 if (error == EINTR || error == ERESTART ||
598 (error = nfs_reconnect(rep, l)) != 0) {
599 nfs_sndunlock(&rep->r_nmp->nm_iflag);
600 return (error);
601 }
602 goto tryagain;
603 }
604 }
605 nfs_sndunlock(&rep->r_nmp->nm_iflag);
606 if (sotype == SOCK_STREAM) {
607 aio.iov_base = (caddr_t) &len;
608 aio.iov_len = sizeof(u_int32_t);
609 auio.uio_iov = &aio;
610 auio.uio_iovcnt = 1;
611 auio.uio_segflg = UIO_SYSSPACE;
612 auio.uio_rw = UIO_READ;
613 auio.uio_offset = 0;
614 auio.uio_resid = sizeof(u_int32_t);
615 auio.uio_lwp = l;
616 do {
617 rcvflg = MSG_WAITALL;
618 error = (*so->so_receive)(so, (struct mbuf **)0, &auio,
619 (struct mbuf **)0, (struct mbuf **)0, &rcvflg);
620 if (error == EWOULDBLOCK && rep) {
621 if (rep->r_flags & R_SOFTTERM)
622 return (EINTR);
623 /*
624 * if it seems that the server died after it
625 * received our request, set EPIPE so that
626 * we'll reconnect and retransmit requests.
627 */
628 if (rep->r_rexmit >= rep->r_nmp->nm_retry) {
629 nfsstats.rpctimeouts++;
630 error = EPIPE;
631 }
632 }
633 } while (error == EWOULDBLOCK);
634 if (!error && auio.uio_resid > 0) {
635 /*
636 * Don't log a 0 byte receive; it means
637 * that the socket has been closed, and
638 * can happen during normal operation
639 * (forcible unmount or Solaris server).
640 */
641 if (auio.uio_resid != sizeof (u_int32_t))
642 log(LOG_INFO,
643 "short receive (%lu/%lu) from nfs server %s\n",
644 (u_long)sizeof(u_int32_t) - auio.uio_resid,
645 (u_long)sizeof(u_int32_t),
646 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
647 error = EPIPE;
648 }
649 if (error)
650 goto errout;
651 len = ntohl(len) & ~0x80000000;
652 /*
653 * This is SERIOUS! We are out of sync with the sender
654 * and forcing a disconnect/reconnect is all I can do.
655 */
656 if (len > NFS_MAXPACKET) {
657 log(LOG_ERR, "%s (%d) from nfs server %s\n",
658 "impossible packet length",
659 len,
660 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
661 error = EFBIG;
662 goto errout;
663 }
664 auio.uio_resid = len;
665 do {
666 rcvflg = MSG_WAITALL;
667 error = (*so->so_receive)(so, (struct mbuf **)0,
668 &auio, mp, (struct mbuf **)0, &rcvflg);
669 } while (error == EWOULDBLOCK || error == EINTR ||
670 error == ERESTART);
671 if (!error && auio.uio_resid > 0) {
672 if (len != auio.uio_resid)
673 log(LOG_INFO,
674 "short receive (%lu/%d) from nfs server %s\n",
675 (u_long)len - auio.uio_resid, len,
676 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
677 error = EPIPE;
678 }
679 } else {
680 /*
681 * NB: Since uio_resid is big, MSG_WAITALL is ignored
682 * and soreceive() will return when it has either a
683 * control msg or a data msg.
684 * We have no use for control msg., but must grab them
685 * and then throw them away so we know what is going
686 * on.
687 */
688 auio.uio_resid = len = 100000000; /* Anything Big */
689 auio.uio_lwp = l;
690 do {
691 rcvflg = 0;
692 error = (*so->so_receive)(so, (struct mbuf **)0,
693 &auio, mp, &control, &rcvflg);
694 if (control)
695 m_freem(control);
696 if (error == EWOULDBLOCK && rep) {
697 if (rep->r_flags & R_SOFTTERM)
698 return (EINTR);
699 }
700 } while (error == EWOULDBLOCK ||
701 (!error && *mp == NULL && control));
702 if ((rcvflg & MSG_EOR) == 0)
703 printf("Egad!!\n");
704 if (!error && *mp == NULL)
705 error = EPIPE;
706 len -= auio.uio_resid;
707 }
708 errout:
709 if (error && error != EINTR && error != ERESTART) {
710 m_freem(*mp);
711 *mp = (struct mbuf *)0;
712 if (error != EPIPE)
713 log(LOG_INFO,
714 "receive error %d from nfs server %s\n",
715 error,
716 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
717 error = nfs_sndlock(&rep->r_nmp->nm_iflag, rep);
718 if (!error)
719 error = nfs_reconnect(rep, l);
720 if (!error)
721 goto tryagain;
722 else
723 nfs_sndunlock(&rep->r_nmp->nm_iflag);
724 }
725 } else {
726 if ((so = rep->r_nmp->nm_so) == NULL)
727 return (EACCES);
728 if (so->so_state & SS_ISCONNECTED)
729 getnam = (struct mbuf **)0;
730 else
731 getnam = aname;
732 auio.uio_resid = len = 1000000;
733 auio.uio_lwp = l;
734 do {
735 rcvflg = 0;
736 error = (*so->so_receive)(so, getnam, &auio, mp,
737 (struct mbuf **)0, &rcvflg);
738 if (error == EWOULDBLOCK &&
739 (rep->r_flags & R_SOFTTERM))
740 return (EINTR);
741 } while (error == EWOULDBLOCK);
742 len -= auio.uio_resid;
743 if (!error && *mp == NULL)
744 error = EPIPE;
745 }
746 if (error) {
747 m_freem(*mp);
748 *mp = (struct mbuf *)0;
749 }
750 return (error);
751 }
752
753 /*
754 * Implement receipt of reply on a socket.
755 * We must search through the list of received datagrams matching them
756 * with outstanding requests using the xid, until ours is found.
757 */
758 /* ARGSUSED */
759 int
760 nfs_reply(myrep, lwp)
761 struct nfsreq *myrep;
762 struct lwp *lwp;
763 {
764 struct nfsreq *rep;
765 struct nfsmount *nmp = myrep->r_nmp;
766 int32_t t1;
767 struct mbuf *mrep, *nam, *md;
768 u_int32_t rxid, *tl;
769 caddr_t dpos, cp2;
770 int error;
771
772 /*
773 * Loop around until we get our own reply
774 */
775 for (;;) {
776 /*
777 * Lock against other receivers so that I don't get stuck in
778 * sbwait() after someone else has received my reply for me.
779 * Also necessary for connection based protocols to avoid
780 * race conditions during a reconnect.
781 */
782 error = nfs_rcvlock(myrep);
783 if (error == EALREADY)
784 return (0);
785 if (error)
786 return (error);
787 /*
788 * Get the next Rpc reply off the socket
789 */
790 nmp->nm_waiters++;
791 error = nfs_receive(myrep, &nam, &mrep, lwp);
792 nfs_rcvunlock(nmp);
793 if (error) {
794
795 if (nmp->nm_iflag & NFSMNT_DISMNT) {
796 /*
797 * Oops, we're going away now..
798 */
799 nmp->nm_waiters--;
800 wakeup (&nmp->nm_waiters);
801 return error;
802 }
803 nmp->nm_waiters--;
804 /*
805 * Ignore routing errors on connectionless protocols? ?
806 */
807 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
808 nmp->nm_so->so_error = 0;
809 #ifdef DEBUG
810 printf("nfs_reply: ignoring error %d\n", error);
811 #endif
812 if (myrep->r_flags & R_GETONEREP)
813 return (0);
814 continue;
815 }
816 return (error);
817 }
818 nmp->nm_waiters--;
819 if (nam)
820 m_freem(nam);
821
822 /*
823 * Get the xid and check that it is an rpc reply
824 */
825 md = mrep;
826 dpos = mtod(md, caddr_t);
827 nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
828 rxid = *tl++;
829 if (*tl != rpc_reply) {
830 #ifndef NFS_V2_ONLY
831 if (nmp->nm_flag & NFSMNT_NQNFS) {
832 if (nqnfs_callback(nmp, mrep, md, dpos,
833 myrep->r_lwp))
834 nfsstats.rpcinvalid++;
835 } else
836 #endif
837 {
838 nfsstats.rpcinvalid++;
839 m_freem(mrep);
840 }
841 nfsmout:
842 if (myrep->r_flags & R_GETONEREP)
843 return (0);
844 continue;
845 }
846
847 /*
848 * Loop through the request list to match up the reply
849 * Iff no match, just drop the datagram
850 */
851 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
852 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
853 /* Found it.. */
854 rep->r_mrep = mrep;
855 rep->r_md = md;
856 rep->r_dpos = dpos;
857 if (nfsrtton) {
858 struct rttl *rt;
859
860 rt = &nfsrtt.rttl[nfsrtt.pos];
861 rt->proc = rep->r_procnum;
862 rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
863 rt->sent = nmp->nm_sent;
864 rt->cwnd = nmp->nm_cwnd;
865 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
866 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
867 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsidx;
868 rt->tstamp = time;
869 if (rep->r_flags & R_TIMING)
870 rt->rtt = rep->r_rtt;
871 else
872 rt->rtt = 1000000;
873 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
874 }
875 /*
876 * Update congestion window.
877 * Do the additive increase of
878 * one rpc/rtt.
879 */
880 if (nmp->nm_cwnd <= nmp->nm_sent) {
881 nmp->nm_cwnd +=
882 (NFS_CWNDSCALE * NFS_CWNDSCALE +
883 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
884 if (nmp->nm_cwnd > NFS_MAXCWND)
885 nmp->nm_cwnd = NFS_MAXCWND;
886 }
887 rep->r_flags &= ~R_SENT;
888 nmp->nm_sent -= NFS_CWNDSCALE;
889 /*
890 * Update rtt using a gain of 0.125 on the mean
891 * and a gain of 0.25 on the deviation.
892 */
893 if (rep->r_flags & R_TIMING) {
894 /*
895 * Since the timer resolution of
896 * NFS_HZ is so course, it can often
897 * result in r_rtt == 0. Since
898 * r_rtt == N means that the actual
899 * rtt is between N+dt and N+2-dt ticks,
900 * add 1.
901 */
902 t1 = rep->r_rtt + 1;
903 t1 -= (NFS_SRTT(rep) >> 3);
904 NFS_SRTT(rep) += t1;
905 if (t1 < 0)
906 t1 = -t1;
907 t1 -= (NFS_SDRTT(rep) >> 2);
908 NFS_SDRTT(rep) += t1;
909 }
910 nmp->nm_timeouts = 0;
911 break;
912 }
913 }
914 /*
915 * If not matched to a request, drop it.
916 * If it's mine, get out.
917 */
918 if (rep == 0) {
919 nfsstats.rpcunexpected++;
920 m_freem(mrep);
921 } else if (rep == myrep) {
922 if (rep->r_mrep == NULL)
923 panic("nfsreply nil");
924 return (0);
925 }
926 if (myrep->r_flags & R_GETONEREP)
927 return (0);
928 }
929 }
930
931 /*
932 * nfs_request - goes something like this
933 * - fill in request struct
934 * - links it into list
935 * - calls nfs_send() for first transmit
936 * - calls nfs_receive() to get reply
937 * - break down rpc header and return with nfs reply pointed to
938 * by mrep or error
939 * nb: always frees up mreq mbuf list
940 */
941 int
942 nfs_request(np, mrest, procnum, lwp, cred, mrp, mdp, dposp, rexmitp)
943 struct nfsnode *np;
944 struct mbuf *mrest;
945 int procnum;
946 struct lwp *lwp;
947 struct ucred *cred;
948 struct mbuf **mrp;
949 struct mbuf **mdp;
950 caddr_t *dposp;
951 int *rexmitp;
952 {
953 struct mbuf *m, *mrep;
954 struct nfsreq *rep;
955 u_int32_t *tl;
956 int i;
957 struct nfsmount *nmp;
958 struct mbuf *md, *mheadend;
959 char nickv[RPCX_NICKVERF];
960 time_t reqtime, waituntil;
961 caddr_t dpos, cp2;
962 int t1, s, error = 0, mrest_len, auth_len, auth_type;
963 int trylater_delay = NFS_TRYLATERDEL, failed_auth = 0;
964 int verf_len, verf_type;
965 u_int32_t xid;
966 char *auth_str, *verf_str;
967 NFSKERBKEY_T key; /* save session key */
968 struct ucred acred;
969 #ifndef NFS_V2_ONLY
970 int nqlflag, cachable;
971 u_quad_t frev;
972 #endif
973
974 if (rexmitp != NULL)
975 *rexmitp = 0;
976
977 KASSERT(cred != NULL);
978 nmp = VFSTONFS(np->n_vnode->v_mount);
979 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
980 rep->r_nmp = nmp;
981 rep->r_lwp = lwp;
982 rep->r_procnum = procnum;
983 i = 0;
984 m = mrest;
985 while (m) {
986 i += m->m_len;
987 m = m->m_next;
988 }
989 mrest_len = i;
990
991 /*
992 * Get the RPC header with authorization.
993 */
994 kerbauth:
995 verf_str = auth_str = (char *)0;
996 if (nmp->nm_flag & NFSMNT_KERB) {
997 verf_str = nickv;
998 verf_len = sizeof (nickv);
999 auth_type = RPCAUTH_KERB4;
1000 memset((caddr_t)key, 0, sizeof (key));
1001 if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
1002 &auth_len, verf_str, verf_len)) {
1003 error = nfs_getauth(nmp, rep, cred, &auth_str,
1004 &auth_len, verf_str, &verf_len, key);
1005 if (error) {
1006 free((caddr_t)rep, M_NFSREQ);
1007 m_freem(mrest);
1008 return (error);
1009 }
1010 }
1011 } else {
1012 switch (procnum) {
1013 case NFSPROC_READ:
1014 case NFSPROC_WRITE:
1015 case NFSPROC_COMMIT:
1016 acred.cr_uid = np->n_vattr->va_uid;
1017 acred.cr_gid = np->n_vattr->va_gid;
1018 acred.cr_ngroups = 0;
1019 acred.cr_ref = 2; /* Just to be safe.. */
1020 cred = &acred;
1021 break;
1022 }
1023 auth_type = RPCAUTH_UNIX;
1024 auth_len = (((cred->cr_ngroups > nmp->nm_numgrps) ?
1025 nmp->nm_numgrps : cred->cr_ngroups) << 2) +
1026 5 * NFSX_UNSIGNED;
1027 }
1028 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
1029 auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
1030 if (auth_str)
1031 free(auth_str, M_TEMP);
1032
1033 /*
1034 * For stream protocols, insert a Sun RPC Record Mark.
1035 */
1036 if (nmp->nm_sotype == SOCK_STREAM) {
1037 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
1038 *mtod(m, u_int32_t *) = htonl(0x80000000 |
1039 (m->m_pkthdr.len - NFSX_UNSIGNED));
1040 }
1041 rep->r_mreq = m;
1042 rep->r_xid = xid;
1043 tryagain:
1044 if (nmp->nm_flag & NFSMNT_SOFT)
1045 rep->r_retry = nmp->nm_retry;
1046 else
1047 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1048 rep->r_rtt = rep->r_rexmit = 0;
1049 if (proct[procnum] > 0)
1050 rep->r_flags = R_TIMING;
1051 else
1052 rep->r_flags = 0;
1053 rep->r_mrep = NULL;
1054
1055 /*
1056 * Do the client side RPC.
1057 */
1058 nfsstats.rpcrequests++;
1059 /*
1060 * Chain request into list of outstanding requests. Be sure
1061 * to put it LAST so timer finds oldest requests first.
1062 */
1063 s = splsoftnet();
1064 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1065
1066 /* Get send time for nqnfs */
1067 reqtime = time.tv_sec;
1068
1069 /*
1070 * If backing off another request or avoiding congestion, don't
1071 * send this one now but let timer do it. If not timing a request,
1072 * do it now.
1073 */
1074 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
1075 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1076 nmp->nm_sent < nmp->nm_cwnd)) {
1077 splx(s);
1078 if (nmp->nm_soflags & PR_CONNREQUIRED)
1079 error = nfs_sndlock(&nmp->nm_iflag, rep);
1080 if (!error) {
1081 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
1082 error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep, lwp);
1083 if (nmp->nm_soflags & PR_CONNREQUIRED)
1084 nfs_sndunlock(&nmp->nm_iflag);
1085 }
1086 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
1087 nmp->nm_sent += NFS_CWNDSCALE;
1088 rep->r_flags |= R_SENT;
1089 }
1090 } else {
1091 splx(s);
1092 rep->r_rtt = -1;
1093 }
1094
1095 /*
1096 * Wait for the reply from our send or the timer's.
1097 */
1098 if (!error || error == EPIPE)
1099 error = nfs_reply(rep, lwp);
1100
1101 /*
1102 * RPC done, unlink the request.
1103 */
1104 s = splsoftnet();
1105 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1106 splx(s);
1107
1108 /*
1109 * Decrement the outstanding request count.
1110 */
1111 if (rep->r_flags & R_SENT) {
1112 rep->r_flags &= ~R_SENT; /* paranoia */
1113 nmp->nm_sent -= NFS_CWNDSCALE;
1114 }
1115
1116 if (rexmitp != NULL) {
1117 int rexmit;
1118
1119 if (nmp->nm_sotype != SOCK_DGRAM)
1120 rexmit = (rep->r_flags & R_REXMITTED) != 0;
1121 else
1122 rexmit = rep->r_rexmit;
1123 *rexmitp = rexmit;
1124 }
1125
1126 /*
1127 * If there was a successful reply and a tprintf msg.
1128 * tprintf a response.
1129 */
1130 if (!error && (rep->r_flags & R_TPRINTFMSG))
1131 nfs_msg(rep->r_lwp, nmp->nm_mountp->mnt_stat.f_mntfromname,
1132 "is alive again");
1133 mrep = rep->r_mrep;
1134 md = rep->r_md;
1135 dpos = rep->r_dpos;
1136 if (error) {
1137 m_freem(rep->r_mreq);
1138 free((caddr_t)rep, M_NFSREQ);
1139 return (error);
1140 }
1141
1142 /*
1143 * break down the rpc header and check if ok
1144 */
1145 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1146 if (*tl++ == rpc_msgdenied) {
1147 if (*tl == rpc_mismatch)
1148 error = EOPNOTSUPP;
1149 else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
1150 if (!failed_auth) {
1151 failed_auth++;
1152 mheadend->m_next = (struct mbuf *)0;
1153 m_freem(mrep);
1154 m_freem(rep->r_mreq);
1155 goto kerbauth;
1156 } else
1157 error = EAUTH;
1158 } else
1159 error = EACCES;
1160 m_freem(mrep);
1161 m_freem(rep->r_mreq);
1162 free((caddr_t)rep, M_NFSREQ);
1163 return (error);
1164 }
1165
1166 /*
1167 * Grab any Kerberos verifier, otherwise just throw it away.
1168 */
1169 verf_type = fxdr_unsigned(int, *tl++);
1170 i = fxdr_unsigned(int32_t, *tl);
1171 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
1172 error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
1173 if (error)
1174 goto nfsmout;
1175 } else if (i > 0)
1176 nfsm_adv(nfsm_rndup(i));
1177 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1178 /* 0 == ok */
1179 if (*tl == 0) {
1180 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1181 if (*tl != 0) {
1182 error = fxdr_unsigned(int, *tl);
1183 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1184 error == NFSERR_TRYLATER) {
1185 m_freem(mrep);
1186 error = 0;
1187 waituntil = time.tv_sec + trylater_delay;
1188 while (time.tv_sec < waituntil)
1189 (void) tsleep((caddr_t)&lbolt,
1190 PSOCK, "nqnfstry", 0);
1191 trylater_delay *= NFS_TRYLATERDELMUL;
1192 if (trylater_delay > NFS_TRYLATERDELMAX)
1193 trylater_delay = NFS_TRYLATERDELMAX;
1194 /*
1195 * RFC1813:
1196 * The client should wait and then try
1197 * the request with a new RPC transaction ID.
1198 */
1199 nfs_renewxid(rep);
1200 goto tryagain;
1201 }
1202
1203 /*
1204 * If the File Handle was stale, invalidate the
1205 * lookup cache, just in case.
1206 */
1207 if (error == ESTALE)
1208 cache_purge(NFSTOV(np));
1209 if (nmp->nm_flag & NFSMNT_NFSV3) {
1210 *mrp = mrep;
1211 *mdp = md;
1212 *dposp = dpos;
1213 error |= NFSERR_RETERR;
1214 } else
1215 m_freem(mrep);
1216 m_freem(rep->r_mreq);
1217 free((caddr_t)rep, M_NFSREQ);
1218 return (error);
1219 }
1220
1221 #ifndef NFS_V2_ONLY
1222 /*
1223 * For nqnfs, get any lease in reply
1224 */
1225 if (nmp->nm_flag & NFSMNT_NQNFS) {
1226 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1227 if (*tl) {
1228 nqlflag = fxdr_unsigned(int, *tl);
1229 nfsm_dissect(tl, u_int32_t *, 4*NFSX_UNSIGNED);
1230 cachable = fxdr_unsigned(int, *tl++);
1231 reqtime += fxdr_unsigned(int, *tl++);
1232 if (reqtime > time.tv_sec) {
1233 frev = fxdr_hyper(tl);
1234 nqnfs_clientlease(nmp, np, nqlflag,
1235 cachable, reqtime, frev);
1236 }
1237 }
1238 }
1239 #endif
1240 *mrp = mrep;
1241 *mdp = md;
1242 *dposp = dpos;
1243 m_freem(rep->r_mreq);
1244 FREE((caddr_t)rep, M_NFSREQ);
1245 return (0);
1246 }
1247 m_freem(mrep);
1248 error = EPROTONOSUPPORT;
1249 nfsmout:
1250 m_freem(rep->r_mreq);
1251 free((caddr_t)rep, M_NFSREQ);
1252 return (error);
1253 }
1254 #endif /* NFS */
1255
1256 /*
1257 * Generate the rpc reply header
1258 * siz arg. is used to decide if adding a cluster is worthwhile
1259 */
1260 int
1261 nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp)
1262 int siz;
1263 struct nfsrv_descript *nd;
1264 struct nfssvc_sock *slp;
1265 int err;
1266 int cache;
1267 u_quad_t *frev;
1268 struct mbuf **mrq;
1269 struct mbuf **mbp;
1270 caddr_t *bposp;
1271 {
1272 u_int32_t *tl;
1273 struct mbuf *mreq;
1274 caddr_t bpos;
1275 struct mbuf *mb;
1276
1277 mreq = m_gethdr(M_WAIT, MT_DATA);
1278 MCLAIM(mreq, &nfs_mowner);
1279 mb = mreq;
1280 /*
1281 * If this is a big reply, use a cluster else
1282 * try and leave leading space for the lower level headers.
1283 */
1284 siz += RPC_REPLYSIZ;
1285 if (siz >= max_datalen) {
1286 m_clget(mreq, M_WAIT);
1287 } else
1288 mreq->m_data += max_hdr;
1289 tl = mtod(mreq, u_int32_t *);
1290 mreq->m_len = 6 * NFSX_UNSIGNED;
1291 bpos = ((caddr_t)tl) + mreq->m_len;
1292 *tl++ = txdr_unsigned(nd->nd_retxid);
1293 *tl++ = rpc_reply;
1294 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1295 *tl++ = rpc_msgdenied;
1296 if (err & NFSERR_AUTHERR) {
1297 *tl++ = rpc_autherr;
1298 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1299 mreq->m_len -= NFSX_UNSIGNED;
1300 bpos -= NFSX_UNSIGNED;
1301 } else {
1302 *tl++ = rpc_mismatch;
1303 *tl++ = txdr_unsigned(RPC_VER2);
1304 *tl = txdr_unsigned(RPC_VER2);
1305 }
1306 } else {
1307 *tl++ = rpc_msgaccepted;
1308
1309 /*
1310 * For Kerberos authentication, we must send the nickname
1311 * verifier back, otherwise just RPCAUTH_NULL.
1312 */
1313 if (nd->nd_flag & ND_KERBFULL) {
1314 struct nfsuid *nuidp;
1315 struct timeval ktvin, ktvout;
1316
1317 LIST_FOREACH(nuidp, NUIDHASH(slp, nd->nd_cr.cr_uid),
1318 nu_hash) {
1319 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
1320 (!nd->nd_nam2 || netaddr_match(
1321 NU_NETFAM(nuidp), &nuidp->nu_haddr,
1322 nd->nd_nam2)))
1323 break;
1324 }
1325 if (nuidp) {
1326 ktvin.tv_sec =
1327 txdr_unsigned(nuidp->nu_timestamp.tv_sec
1328 - 1);
1329 ktvin.tv_usec =
1330 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1331
1332 /*
1333 * Encrypt the timestamp in ecb mode using the
1334 * session key.
1335 */
1336 #ifdef NFSKERB
1337 XXX
1338 #endif
1339
1340 *tl++ = rpc_auth_kerb;
1341 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1342 *tl = ktvout.tv_sec;
1343 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1344 *tl++ = ktvout.tv_usec;
1345 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
1346 } else {
1347 *tl++ = 0;
1348 *tl++ = 0;
1349 }
1350 } else {
1351 *tl++ = 0;
1352 *tl++ = 0;
1353 }
1354 switch (err) {
1355 case EPROGUNAVAIL:
1356 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1357 break;
1358 case EPROGMISMATCH:
1359 *tl = txdr_unsigned(RPC_PROGMISMATCH);
1360 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1361 if (nd->nd_flag & ND_NQNFS) {
1362 *tl++ = txdr_unsigned(3);
1363 *tl = txdr_unsigned(3);
1364 } else {
1365 *tl++ = txdr_unsigned(2);
1366 *tl = txdr_unsigned(3);
1367 }
1368 break;
1369 case EPROCUNAVAIL:
1370 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1371 break;
1372 case EBADRPC:
1373 *tl = txdr_unsigned(RPC_GARBAGE);
1374 break;
1375 default:
1376 *tl = 0;
1377 if (err != NFSERR_RETVOID) {
1378 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1379 if (err)
1380 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1381 else
1382 *tl = 0;
1383 }
1384 break;
1385 };
1386 }
1387
1388 /*
1389 * For nqnfs, piggyback lease as requested.
1390 */
1391 if ((nd->nd_flag & ND_NQNFS) && err == 0) {
1392 if (nd->nd_flag & ND_LEASE) {
1393 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1394 *tl++ = txdr_unsigned(nd->nd_flag & ND_LEASE);
1395 *tl++ = txdr_unsigned(cache);
1396 *tl++ = txdr_unsigned(nd->nd_duration);
1397 txdr_hyper(*frev, tl);
1398 } else {
1399 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1400 *tl = 0;
1401 }
1402 }
1403 if (mrq != NULL)
1404 *mrq = mreq;
1405 *mbp = mb;
1406 *bposp = bpos;
1407 if (err != 0 && err != NFSERR_RETVOID)
1408 nfsstats.srvrpc_errs++;
1409 return (0);
1410 }
1411
1412 /*
1413 * Nfs timer routine
1414 * Scan the nfsreq list and retranmit any requests that have timed out
1415 * To avoid retransmission attempts on STREAM sockets (in the future) make
1416 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1417 */
1418 void
1419 nfs_timer(arg)
1420 void *arg; /* never used */
1421 {
1422 struct nfsreq *rep;
1423 struct mbuf *m;
1424 struct socket *so;
1425 struct nfsmount *nmp;
1426 int timeo;
1427 int s, error;
1428 #ifdef NFSSERVER
1429 struct nfssvc_sock *slp;
1430 static long lasttime = 0;
1431 u_quad_t cur_usec;
1432 #endif
1433
1434 s = splsoftnet();
1435 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1436 nmp = rep->r_nmp;
1437 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1438 continue;
1439 if (nfs_sigintr(nmp, rep, rep->r_lwp)) {
1440 rep->r_flags |= R_SOFTTERM;
1441 continue;
1442 }
1443 if (rep->r_rtt >= 0) {
1444 rep->r_rtt++;
1445 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1446 timeo = nmp->nm_timeo;
1447 else
1448 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1449 if (nmp->nm_timeouts > 0)
1450 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1451 if (rep->r_rtt <= timeo)
1452 continue;
1453 if (nmp->nm_timeouts <
1454 (sizeof(nfs_backoff) / sizeof(nfs_backoff[0])))
1455 nmp->nm_timeouts++;
1456 }
1457 /*
1458 * Check for server not responding
1459 */
1460 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1461 rep->r_rexmit > nmp->nm_deadthresh) {
1462 nfs_msg(rep->r_lwp,
1463 nmp->nm_mountp->mnt_stat.f_mntfromname,
1464 "not responding");
1465 rep->r_flags |= R_TPRINTFMSG;
1466 }
1467 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1468 nfsstats.rpctimeouts++;
1469 rep->r_flags |= R_SOFTTERM;
1470 continue;
1471 }
1472 if (nmp->nm_sotype != SOCK_DGRAM) {
1473 if (++rep->r_rexmit > NFS_MAXREXMIT)
1474 rep->r_rexmit = NFS_MAXREXMIT;
1475 continue;
1476 }
1477 if ((so = nmp->nm_so) == NULL)
1478 continue;
1479
1480 /*
1481 * If there is enough space and the window allows..
1482 * Resend it
1483 * Set r_rtt to -1 in case we fail to send it now.
1484 */
1485 rep->r_rtt = -1;
1486 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1487 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1488 (rep->r_flags & R_SENT) ||
1489 nmp->nm_sent < nmp->nm_cwnd) &&
1490 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1491 if (so->so_state & SS_ISCONNECTED)
1492 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1493 (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
1494 else
1495 error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1496 nmp->nm_nam, (struct mbuf *)0, (struct lwp *)0);
1497 if (error) {
1498 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
1499 #ifdef DEBUG
1500 printf("nfs_timer: ignoring error %d\n",
1501 error);
1502 #endif
1503 so->so_error = 0;
1504 }
1505 } else {
1506 /*
1507 * Iff first send, start timing
1508 * else turn timing off, backoff timer
1509 * and divide congestion window by 2.
1510 */
1511 if (rep->r_flags & R_SENT) {
1512 rep->r_flags &= ~R_TIMING;
1513 if (++rep->r_rexmit > NFS_MAXREXMIT)
1514 rep->r_rexmit = NFS_MAXREXMIT;
1515 nmp->nm_cwnd >>= 1;
1516 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1517 nmp->nm_cwnd = NFS_CWNDSCALE;
1518 nfsstats.rpcretries++;
1519 } else {
1520 rep->r_flags |= R_SENT;
1521 nmp->nm_sent += NFS_CWNDSCALE;
1522 }
1523 rep->r_rtt = 0;
1524 }
1525 }
1526 }
1527
1528 #ifdef NFSSERVER
1529 /*
1530 * Call the nqnfs server timer once a second to handle leases.
1531 */
1532 if (lasttime != time.tv_sec) {
1533 lasttime = time.tv_sec;
1534 nqnfs_serverd();
1535 }
1536
1537 /*
1538 * Scan the write gathering queues for writes that need to be
1539 * completed now.
1540 */
1541 cur_usec = (u_quad_t)time.tv_sec * 1000000 + (u_quad_t)time.tv_usec;
1542 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
1543 if (LIST_FIRST(&slp->ns_tq) &&
1544 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec)
1545 nfsrv_wakenfsd(slp);
1546 }
1547 #endif /* NFSSERVER */
1548 splx(s);
1549 callout_schedule(&nfs_timer_ch, nfs_ticks);
1550 }
1551
1552 /*ARGSUSED*/
1553 void
1554 nfs_exit(p, v)
1555 struct proc *p;
1556 void *v;
1557 {
1558 struct nfsreq *rp;
1559 int s = splsoftnet();
1560
1561 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
1562 if (rp->r_lwp->l_proc == p)
1563 TAILQ_REMOVE(&nfs_reqq, rp, r_chain);
1564 }
1565 splx(s);
1566 }
1567
1568 /*
1569 * Test for a termination condition pending on the process.
1570 * This is used for NFSMNT_INT mounts.
1571 */
1572 int
1573 nfs_sigintr(nmp, rep, l)
1574 struct nfsmount *nmp;
1575 struct nfsreq *rep;
1576 struct lwp *l;
1577 {
1578 sigset_t ss;
1579
1580 if (rep && (rep->r_flags & R_SOFTTERM))
1581 return (EINTR);
1582 if (!(nmp->nm_flag & NFSMNT_INT))
1583 return (0);
1584 if (l) {
1585 sigpending1(l->l_proc, &ss);
1586 #if 0
1587 sigminusset(&l->l_proc->p_sigctx.ps_sigignore, &ss);
1588 #endif
1589 if (sigismember(&ss, SIGINT) || sigismember(&ss, SIGTERM) ||
1590 sigismember(&ss, SIGKILL) || sigismember(&ss, SIGHUP) ||
1591 sigismember(&ss, SIGQUIT))
1592 return (EINTR);
1593 }
1594 return (0);
1595 }
1596
1597 /*
1598 * Lock a socket against others.
1599 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1600 * and also to avoid race conditions between the processes with nfs requests
1601 * in progress when a reconnect is necessary.
1602 */
1603 int
1604 nfs_sndlock(flagp, rep)
1605 int *flagp;
1606 struct nfsreq *rep;
1607 {
1608 struct lwp *l;
1609 int slpflag = 0, slptimeo = 0;
1610
1611 if (rep) {
1612 l = rep->r_lwp;
1613 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1614 slpflag = PCATCH;
1615 } else
1616 l = (struct lwp *)0;
1617 while (*flagp & NFSMNT_SNDLOCK) {
1618 if (rep && nfs_sigintr(rep->r_nmp, rep, l))
1619 return (EINTR);
1620 *flagp |= NFSMNT_WANTSND;
1621 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck",
1622 slptimeo);
1623 if (slpflag == PCATCH) {
1624 slpflag = 0;
1625 slptimeo = 2 * hz;
1626 }
1627 }
1628 *flagp |= NFSMNT_SNDLOCK;
1629 return (0);
1630 }
1631
1632 /*
1633 * Unlock the stream socket for others.
1634 */
1635 void
1636 nfs_sndunlock(flagp)
1637 int *flagp;
1638 {
1639
1640 if ((*flagp & NFSMNT_SNDLOCK) == 0)
1641 panic("nfs sndunlock");
1642 *flagp &= ~NFSMNT_SNDLOCK;
1643 if (*flagp & NFSMNT_WANTSND) {
1644 *flagp &= ~NFSMNT_WANTSND;
1645 wakeup((caddr_t)flagp);
1646 }
1647 }
1648
1649 int
1650 nfs_rcvlock(rep)
1651 struct nfsreq *rep;
1652 {
1653 struct nfsmount *nmp = rep->r_nmp;
1654 int *flagp = &nmp->nm_iflag;
1655 int slpflag, slptimeo = 0;
1656 int error = 0;
1657
1658 if (*flagp & NFSMNT_DISMNT)
1659 return EIO;
1660
1661 if (*flagp & NFSMNT_INT)
1662 slpflag = PCATCH;
1663 else
1664 slpflag = 0;
1665 simple_lock(&nmp->nm_slock);
1666 while (*flagp & NFSMNT_RCVLOCK) {
1667 if (nfs_sigintr(rep->r_nmp, rep, rep->r_lwp)) {
1668 error = EINTR;
1669 goto quit;
1670 }
1671 *flagp |= NFSMNT_WANTRCV;
1672 nmp->nm_waiters++;
1673 (void) ltsleep(flagp, slpflag | (PZERO - 1), "nfsrcvlk",
1674 slptimeo, &nmp->nm_slock);
1675 nmp->nm_waiters--;
1676 if (*flagp & NFSMNT_DISMNT) {
1677 wakeup(&nmp->nm_waiters);
1678 error = EIO;
1679 goto quit;
1680 }
1681 /* If our reply was received while we were sleeping,
1682 * then just return without taking the lock to avoid a
1683 * situation where a single iod could 'capture' the
1684 * receive lock.
1685 */
1686 if (rep->r_mrep != NULL) {
1687 error = EALREADY;
1688 goto quit;
1689 }
1690 if (slpflag == PCATCH) {
1691 slpflag = 0;
1692 slptimeo = 2 * hz;
1693 }
1694 }
1695 *flagp |= NFSMNT_RCVLOCK;
1696 quit:
1697 simple_unlock(&nmp->nm_slock);
1698 return error;
1699 }
1700
1701 /*
1702 * Unlock the stream socket for others.
1703 */
1704 void
1705 nfs_rcvunlock(nmp)
1706 struct nfsmount *nmp;
1707 {
1708 int *flagp = &nmp->nm_iflag;
1709
1710 simple_lock(&nmp->nm_slock);
1711 if ((*flagp & NFSMNT_RCVLOCK) == 0)
1712 panic("nfs rcvunlock");
1713 *flagp &= ~NFSMNT_RCVLOCK;
1714 if (*flagp & NFSMNT_WANTRCV) {
1715 *flagp &= ~NFSMNT_WANTRCV;
1716 wakeup((caddr_t)flagp);
1717 }
1718 simple_unlock(&nmp->nm_slock);
1719 }
1720
1721 /*
1722 * Parse an RPC request
1723 * - verify it
1724 * - fill in the cred struct.
1725 */
1726 int
1727 nfs_getreq(nd, nfsd, has_header)
1728 struct nfsrv_descript *nd;
1729 struct nfsd *nfsd;
1730 int has_header;
1731 {
1732 int len, i;
1733 u_int32_t *tl;
1734 int32_t t1;
1735 struct uio uio;
1736 struct iovec iov;
1737 caddr_t dpos, cp2, cp;
1738 u_int32_t nfsvers, auth_type;
1739 uid_t nickuid;
1740 int error = 0, nqnfs = 0, ticklen;
1741 struct mbuf *mrep, *md;
1742 struct nfsuid *nuidp;
1743 struct timeval tvin, tvout;
1744
1745 mrep = nd->nd_mrep;
1746 md = nd->nd_md;
1747 dpos = nd->nd_dpos;
1748 if (has_header) {
1749 nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1750 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
1751 if (*tl++ != rpc_call) {
1752 m_freem(mrep);
1753 return (EBADRPC);
1754 }
1755 } else
1756 nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
1757 nd->nd_repstat = 0;
1758 nd->nd_flag = 0;
1759 if (*tl++ != rpc_vers) {
1760 nd->nd_repstat = ERPCMISMATCH;
1761 nd->nd_procnum = NFSPROC_NOOP;
1762 return (0);
1763 }
1764 if (*tl != nfs_prog) {
1765 if (*tl == nqnfs_prog)
1766 nqnfs++;
1767 else {
1768 nd->nd_repstat = EPROGUNAVAIL;
1769 nd->nd_procnum = NFSPROC_NOOP;
1770 return (0);
1771 }
1772 }
1773 tl++;
1774 nfsvers = fxdr_unsigned(u_int32_t, *tl++);
1775 if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) ||
1776 (nfsvers != NQNFS_VER3 && nqnfs)) {
1777 nd->nd_repstat = EPROGMISMATCH;
1778 nd->nd_procnum = NFSPROC_NOOP;
1779 return (0);
1780 }
1781 if (nqnfs)
1782 nd->nd_flag = (ND_NFSV3 | ND_NQNFS);
1783 else if (nfsvers == NFS_VER3)
1784 nd->nd_flag = ND_NFSV3;
1785 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
1786 if (nd->nd_procnum == NFSPROC_NULL)
1787 return (0);
1788 if (nd->nd_procnum >= NFS_NPROCS ||
1789 (!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
1790 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
1791 nd->nd_repstat = EPROCUNAVAIL;
1792 nd->nd_procnum = NFSPROC_NOOP;
1793 return (0);
1794 }
1795 if ((nd->nd_flag & ND_NFSV3) == 0)
1796 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
1797 auth_type = *tl++;
1798 len = fxdr_unsigned(int, *tl++);
1799 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1800 m_freem(mrep);
1801 return (EBADRPC);
1802 }
1803
1804 nd->nd_flag &= ~ND_KERBAUTH;
1805 /*
1806 * Handle auth_unix or auth_kerb.
1807 */
1808 if (auth_type == rpc_auth_unix) {
1809 len = fxdr_unsigned(int, *++tl);
1810 if (len < 0 || len > NFS_MAXNAMLEN) {
1811 m_freem(mrep);
1812 return (EBADRPC);
1813 }
1814 nfsm_adv(nfsm_rndup(len));
1815 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1816 memset((caddr_t)&nd->nd_cr, 0, sizeof (struct ucred));
1817 nd->nd_cr.cr_ref = 1;
1818 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
1819 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
1820 len = fxdr_unsigned(int, *tl);
1821 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
1822 m_freem(mrep);
1823 return (EBADRPC);
1824 }
1825 nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
1826 for (i = 0; i < len; i++)
1827 if (i < NGROUPS)
1828 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
1829 else
1830 tl++;
1831 nd->nd_cr.cr_ngroups = (len > NGROUPS) ? NGROUPS : len;
1832 if (nd->nd_cr.cr_ngroups > 1)
1833 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
1834 len = fxdr_unsigned(int, *++tl);
1835 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1836 m_freem(mrep);
1837 return (EBADRPC);
1838 }
1839 if (len > 0)
1840 nfsm_adv(nfsm_rndup(len));
1841 } else if (auth_type == rpc_auth_kerb) {
1842 switch (fxdr_unsigned(int, *tl++)) {
1843 case RPCAKN_FULLNAME:
1844 ticklen = fxdr_unsigned(int, *tl);
1845 *((u_int32_t *)nfsd->nfsd_authstr) = *tl;
1846 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
1847 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
1848 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
1849 m_freem(mrep);
1850 return (EBADRPC);
1851 }
1852 uio.uio_offset = 0;
1853 uio.uio_iov = &iov;
1854 uio.uio_iovcnt = 1;
1855 uio.uio_segflg = UIO_SYSSPACE;
1856 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
1857 iov.iov_len = RPCAUTH_MAXSIZ - 4;
1858 nfsm_mtouio(&uio, uio.uio_resid);
1859 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1860 if (*tl++ != rpc_auth_kerb ||
1861 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
1862 printf("Bad kerb verifier\n");
1863 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1864 nd->nd_procnum = NFSPROC_NOOP;
1865 return (0);
1866 }
1867 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED);
1868 tl = (u_int32_t *)cp;
1869 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
1870 printf("Not fullname kerb verifier\n");
1871 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1872 nd->nd_procnum = NFSPROC_NOOP;
1873 return (0);
1874 }
1875 cp += NFSX_UNSIGNED;
1876 memcpy(nfsd->nfsd_verfstr, cp, 3 * NFSX_UNSIGNED);
1877 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
1878 nd->nd_flag |= ND_KERBFULL;
1879 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
1880 break;
1881 case RPCAKN_NICKNAME:
1882 if (len != 2 * NFSX_UNSIGNED) {
1883 printf("Kerb nickname short\n");
1884 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
1885 nd->nd_procnum = NFSPROC_NOOP;
1886 return (0);
1887 }
1888 nickuid = fxdr_unsigned(uid_t, *tl);
1889 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1890 if (*tl++ != rpc_auth_kerb ||
1891 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
1892 printf("Kerb nick verifier bad\n");
1893 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1894 nd->nd_procnum = NFSPROC_NOOP;
1895 return (0);
1896 }
1897 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1898 tvin.tv_sec = *tl++;
1899 tvin.tv_usec = *tl;
1900
1901 LIST_FOREACH(nuidp, NUIDHASH(nfsd->nfsd_slp, nickuid),
1902 nu_hash) {
1903 if (nuidp->nu_cr.cr_uid == nickuid &&
1904 (!nd->nd_nam2 ||
1905 netaddr_match(NU_NETFAM(nuidp),
1906 &nuidp->nu_haddr, nd->nd_nam2)))
1907 break;
1908 }
1909 if (!nuidp) {
1910 nd->nd_repstat =
1911 (NFSERR_AUTHERR|AUTH_REJECTCRED);
1912 nd->nd_procnum = NFSPROC_NOOP;
1913 return (0);
1914 }
1915
1916 /*
1917 * Now, decrypt the timestamp using the session key
1918 * and validate it.
1919 */
1920 #ifdef NFSKERB
1921 XXX
1922 #endif
1923
1924 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
1925 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
1926 if (nuidp->nu_expire < time.tv_sec ||
1927 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
1928 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
1929 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
1930 nuidp->nu_expire = 0;
1931 nd->nd_repstat =
1932 (NFSERR_AUTHERR|AUTH_REJECTVERF);
1933 nd->nd_procnum = NFSPROC_NOOP;
1934 return (0);
1935 }
1936 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
1937 nd->nd_flag |= ND_KERBNICK;
1938 };
1939 } else {
1940 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
1941 nd->nd_procnum = NFSPROC_NOOP;
1942 return (0);
1943 }
1944
1945 /*
1946 * For nqnfs, get piggybacked lease request.
1947 */
1948 if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) {
1949 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1950 nd->nd_flag |= fxdr_unsigned(int, *tl);
1951 if (nd->nd_flag & ND_LEASE) {
1952 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1953 nd->nd_duration = fxdr_unsigned(u_int32_t, *tl);
1954 } else
1955 nd->nd_duration = NQ_MINLEASE;
1956 } else
1957 nd->nd_duration = NQ_MINLEASE;
1958 nd->nd_md = md;
1959 nd->nd_dpos = dpos;
1960 return (0);
1961 nfsmout:
1962 return (error);
1963 }
1964
1965 int
1966 nfs_msg(l, server, msg)
1967 struct lwp *l;
1968 char *server, *msg;
1969 {
1970 tpr_t tpr;
1971
1972 if (l)
1973 tpr = tprintf_open(l->l_proc);
1974 else
1975 tpr = NULL;
1976 tprintf(tpr, "nfs server %s: %s\n", server, msg);
1977 tprintf_close(tpr);
1978 return (0);
1979 }
1980
1981 #ifdef NFSSERVER
1982 int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
1983 struct nfssvc_sock *, struct lwp *,
1984 struct mbuf **)) = {
1985 nfsrv_null,
1986 nfsrv_getattr,
1987 nfsrv_setattr,
1988 nfsrv_lookup,
1989 nfsrv3_access,
1990 nfsrv_readlink,
1991 nfsrv_read,
1992 nfsrv_write,
1993 nfsrv_create,
1994 nfsrv_mkdir,
1995 nfsrv_symlink,
1996 nfsrv_mknod,
1997 nfsrv_remove,
1998 nfsrv_rmdir,
1999 nfsrv_rename,
2000 nfsrv_link,
2001 nfsrv_readdir,
2002 nfsrv_readdirplus,
2003 nfsrv_statfs,
2004 nfsrv_fsinfo,
2005 nfsrv_pathconf,
2006 nfsrv_commit,
2007 nqnfsrv_getlease,
2008 nqnfsrv_vacated,
2009 nfsrv_noop,
2010 nfsrv_noop
2011 };
2012
2013 /*
2014 * Socket upcall routine for the nfsd sockets.
2015 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2016 * Essentially do as much as possible non-blocking, else punt and it will
2017 * be called with M_WAIT from an nfsd.
2018 */
2019 void
2020 nfsrv_rcv(so, arg, waitflag)
2021 struct socket *so;
2022 caddr_t arg;
2023 int waitflag;
2024 {
2025 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2026 struct mbuf *m;
2027 struct mbuf *mp, *nam;
2028 struct uio auio;
2029 int flags, error;
2030
2031 if ((slp->ns_flag & SLP_VALID) == 0)
2032 return;
2033 #ifdef notdef
2034 /*
2035 * Define this to test for nfsds handling this under heavy load.
2036 */
2037 if (waitflag == M_DONTWAIT) {
2038 slp->ns_flag |= SLP_NEEDQ; goto dorecs;
2039 }
2040 #endif
2041 /* XXX: was NULL, soreceive() requires non-NULL uio->uio_lwp */
2042 auio.uio_lwp = curlwp; /* XXX curlwp */
2043 if (so->so_type == SOCK_STREAM) {
2044 /*
2045 * If there are already records on the queue, defer soreceive()
2046 * to an nfsd so that there is feedback to the TCP layer that
2047 * the nfs servers are heavily loaded.
2048 */
2049 if (slp->ns_rec && waitflag == M_DONTWAIT) {
2050 slp->ns_flag |= SLP_NEEDQ;
2051 goto dorecs;
2052 }
2053
2054 /*
2055 * Do soreceive().
2056 */
2057 auio.uio_resid = 1000000000;
2058 flags = MSG_DONTWAIT;
2059 error = (*so->so_receive)(so, &nam, &auio, &mp, (struct mbuf **)0, &flags);
2060 if (error || mp == (struct mbuf *)0) {
2061 if (error == EWOULDBLOCK)
2062 slp->ns_flag |= SLP_NEEDQ;
2063 else
2064 slp->ns_flag |= SLP_DISCONN;
2065 goto dorecs;
2066 }
2067 m = mp;
2068 if (slp->ns_rawend) {
2069 slp->ns_rawend->m_next = m;
2070 slp->ns_cc += 1000000000 - auio.uio_resid;
2071 } else {
2072 slp->ns_raw = m;
2073 slp->ns_cc = 1000000000 - auio.uio_resid;
2074 }
2075 while (m->m_next)
2076 m = m->m_next;
2077 slp->ns_rawend = m;
2078
2079 /*
2080 * Now try and parse record(s) out of the raw stream data.
2081 */
2082 error = nfsrv_getstream(slp, waitflag);
2083 if (error) {
2084 if (error == EPERM)
2085 slp->ns_flag |= SLP_DISCONN;
2086 else
2087 slp->ns_flag |= SLP_NEEDQ;
2088 }
2089 } else {
2090 do {
2091 auio.uio_resid = 1000000000;
2092 flags = MSG_DONTWAIT;
2093 error = (*so->so_receive)(so, &nam, &auio, &mp,
2094 (struct mbuf **)0, &flags);
2095 if (mp) {
2096 if (nam) {
2097 m = nam;
2098 m->m_next = mp;
2099 } else
2100 m = mp;
2101 if (slp->ns_recend)
2102 slp->ns_recend->m_nextpkt = m;
2103 else
2104 slp->ns_rec = m;
2105 slp->ns_recend = m;
2106 m->m_nextpkt = (struct mbuf *)0;
2107 }
2108 if (error) {
2109 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
2110 && error != EWOULDBLOCK) {
2111 slp->ns_flag |= SLP_DISCONN;
2112 goto dorecs;
2113 }
2114 }
2115 } while (mp);
2116 }
2117
2118 /*
2119 * Now try and process the request records, non-blocking.
2120 */
2121 dorecs:
2122 if (waitflag == M_DONTWAIT &&
2123 (slp->ns_rec || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN))))
2124 nfsrv_wakenfsd(slp);
2125 }
2126
2127 /*
2128 * Try and extract an RPC request from the mbuf data list received on a
2129 * stream socket. The "waitflag" argument indicates whether or not it
2130 * can sleep.
2131 */
2132 int
2133 nfsrv_getstream(slp, waitflag)
2134 struct nfssvc_sock *slp;
2135 int waitflag;
2136 {
2137 struct mbuf *m, **mpp;
2138 struct mbuf *recm;
2139 u_int32_t recmark;
2140
2141 if (slp->ns_flag & SLP_GETSTREAM)
2142 panic("nfs getstream");
2143 slp->ns_flag |= SLP_GETSTREAM;
2144 for (;;) {
2145 if (slp->ns_reclen == 0) {
2146 if (slp->ns_cc < NFSX_UNSIGNED) {
2147 slp->ns_flag &= ~SLP_GETSTREAM;
2148 return (0);
2149 }
2150 m = slp->ns_raw;
2151 m_copydata(m, 0, NFSX_UNSIGNED, (caddr_t)&recmark);
2152 m_adj(m, NFSX_UNSIGNED);
2153 slp->ns_cc -= NFSX_UNSIGNED;
2154 recmark = ntohl(recmark);
2155 slp->ns_reclen = recmark & ~0x80000000;
2156 if (recmark & 0x80000000)
2157 slp->ns_flag |= SLP_LASTFRAG;
2158 else
2159 slp->ns_flag &= ~SLP_LASTFRAG;
2160 if (slp->ns_reclen > NFS_MAXPACKET) {
2161 slp->ns_flag &= ~SLP_GETSTREAM;
2162 return (EPERM);
2163 }
2164 }
2165
2166 /*
2167 * Now get the record part.
2168 *
2169 * Note that slp->ns_reclen may be 0. Linux sometimes
2170 * generates 0-length records.
2171 */
2172 if (slp->ns_cc == slp->ns_reclen) {
2173 recm = slp->ns_raw;
2174 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
2175 slp->ns_cc = slp->ns_reclen = 0;
2176 } else if (slp->ns_cc > slp->ns_reclen) {
2177 recm = slp->ns_raw;
2178 m = m_split(recm, slp->ns_reclen, waitflag);
2179 if (m == NULL) {
2180 slp->ns_flag &= ~SLP_GETSTREAM;
2181 return (EWOULDBLOCK);
2182 }
2183 m_claimm(recm, &nfs_mowner);
2184 slp->ns_raw = m;
2185 if (m->m_next == NULL)
2186 slp->ns_rawend = m;
2187 slp->ns_cc -= slp->ns_reclen;
2188 slp->ns_reclen = 0;
2189 } else {
2190 slp->ns_flag &= ~SLP_GETSTREAM;
2191 return (0);
2192 }
2193
2194 /*
2195 * Accumulate the fragments into a record.
2196 */
2197 mpp = &slp->ns_frag;
2198 while (*mpp)
2199 mpp = &((*mpp)->m_next);
2200 *mpp = recm;
2201 if (slp->ns_flag & SLP_LASTFRAG) {
2202 if (slp->ns_recend)
2203 slp->ns_recend->m_nextpkt = slp->ns_frag;
2204 else
2205 slp->ns_rec = slp->ns_frag;
2206 slp->ns_recend = slp->ns_frag;
2207 slp->ns_frag = (struct mbuf *)0;
2208 }
2209 }
2210 }
2211
2212 /*
2213 * Parse an RPC header.
2214 */
2215 int
2216 nfsrv_dorec(slp, nfsd, ndp)
2217 struct nfssvc_sock *slp;
2218 struct nfsd *nfsd;
2219 struct nfsrv_descript **ndp;
2220 {
2221 struct mbuf *m, *nam;
2222 struct nfsrv_descript *nd;
2223 int error;
2224
2225 *ndp = NULL;
2226 if ((slp->ns_flag & SLP_VALID) == 0 ||
2227 (m = slp->ns_rec) == (struct mbuf *)0)
2228 return (ENOBUFS);
2229 slp->ns_rec = m->m_nextpkt;
2230 if (slp->ns_rec)
2231 m->m_nextpkt = (struct mbuf *)0;
2232 else
2233 slp->ns_recend = (struct mbuf *)0;
2234 if (m->m_type == MT_SONAME) {
2235 nam = m;
2236 m = m->m_next;
2237 nam->m_next = NULL;
2238 } else
2239 nam = NULL;
2240 nd = pool_get(&nfs_srvdesc_pool, PR_WAITOK);
2241 nd->nd_md = nd->nd_mrep = m;
2242 nd->nd_nam2 = nam;
2243 nd->nd_dpos = mtod(m, caddr_t);
2244 error = nfs_getreq(nd, nfsd, TRUE);
2245 if (error) {
2246 m_freem(nam);
2247 pool_put(&nfs_srvdesc_pool, nd);
2248 return (error);
2249 }
2250 *ndp = nd;
2251 nfsd->nfsd_nd = nd;
2252 return (0);
2253 }
2254
2255
2256 /*
2257 * Search for a sleeping nfsd and wake it up.
2258 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2259 * running nfsds will go look for the work in the nfssvc_sock list.
2260 */
2261 void
2262 nfsrv_wakenfsd(slp)
2263 struct nfssvc_sock *slp;
2264 {
2265 struct nfsd *nd;
2266
2267 if ((slp->ns_flag & SLP_VALID) == 0)
2268 return;
2269 simple_lock(&nfsd_slock);
2270 if (slp->ns_flag & SLP_DOREC) {
2271 simple_unlock(&nfsd_slock);
2272 return;
2273 }
2274 nd = SLIST_FIRST(&nfsd_idle_head);
2275 if (nd) {
2276 SLIST_REMOVE_HEAD(&nfsd_idle_head, nfsd_idle);
2277 simple_unlock(&nfsd_slock);
2278
2279 KASSERT(nd->nfsd_flag & NFSD_WAITING);
2280 nd->nfsd_flag &= ~NFSD_WAITING;
2281 if (nd->nfsd_slp)
2282 panic("nfsd wakeup");
2283 slp->ns_sref++;
2284 nd->nfsd_slp = slp;
2285 wakeup(nd);
2286 return;
2287 }
2288 slp->ns_flag |= SLP_DOREC;
2289 nfsd_head_flag |= NFSD_CHECKSLP;
2290 TAILQ_INSERT_TAIL(&nfssvc_sockpending, slp, ns_pending);
2291 simple_unlock(&nfsd_slock);
2292 }
2293 #endif /* NFSSERVER */
2294