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