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