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