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