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