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