]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_socket.c
xnu-201.19.3.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_socket.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1989, 1991, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
26 *
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
59 * FreeBSD-Id: nfs_socket.c,v 1.30 1997/10/28 15:59:07 bde Exp $
60 */
61
62 /*
63 * Socket operations for use by nfs
64 */
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/proc.h>
69 #include <sys/mount.h>
70 #include <sys/kernel.h>
71 #include <sys/mbuf.h>
72 #include <sys/malloc.h>
73 #include <sys/vnode.h>
74 #include <sys/domain.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/syslog.h>
79 #include <sys/tprintf.h>
80 #include <machine/spl.h>
81
82 #include <sys/time.h>
83 #include <kern/clock.h>
84
85 #include <netinet/in.h>
86 #include <netinet/tcp.h>
87
88 #include <nfs/rpcv2.h>
89 #include <nfs/nfsproto.h>
90 #include <nfs/nfs.h>
91 #include <nfs/xdr_subs.h>
92 #include <nfs/nfsm_subs.h>
93 #include <nfs/nfsmount.h>
94 #include <nfs/nfsnode.h>
95 #include <nfs/nfsrtt.h>
96 #include <nfs/nqnfs.h>
97
98 #include <sys/kdebug.h>
99
100 #define FSDBG(A, B, C, D, E) \
101 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_NONE, \
102 (int)(B), (int)(C), (int)(D), (int)(E), 0)
103 #define FSDBG_TOP(A, B, C, D, E) \
104 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_START, \
105 (int)(B), (int)(C), (int)(D), (int)(E), 0)
106 #define FSDBG_BOT(A, B, C, D, E) \
107 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, (A))) | DBG_FUNC_END, \
108 (int)(B), (int)(C), (int)(D), (int)(E), 0)
109
110 #define TRUE 1
111 #define FALSE 0
112
113 /*
114 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
115 * Use the mean and mean deviation of rtt for the appropriate type of rpc
116 * for the frequent rpcs and a default for the others.
117 * The justification for doing "other" this way is that these rpcs
118 * happen so infrequently that timer est. would probably be stale.
119 * Also, since many of these rpcs are
120 * non-idempotent, a conservative timeout is desired.
121 * getattr, lookup - A+2D
122 * read, write - A+4D
123 * other - nm_timeo
124 */
125 #define NFS_RTO(n, t) \
126 ((t) == 0 ? (n)->nm_timeo : \
127 ((t) < 3 ? \
128 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
129 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
130 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
131 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
132 /*
133 * External data, mostly RPC constants in XDR form
134 */
135 extern u_long rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers, rpc_auth_unix,
136 rpc_msgaccepted, rpc_call, rpc_autherr,
137 rpc_auth_kerb;
138 extern u_long nfs_prog, nqnfs_prog;
139 extern time_t nqnfsstarttime;
140 extern struct nfsstats nfsstats;
141 extern int nfsv3_procid[NFS_NPROCS];
142 extern int nfs_ticks;
143 extern u_long nfs_xidwrap;
144
145 /*
146 * Defines which timer to use for the procnum.
147 * 0 - default
148 * 1 - getattr
149 * 2 - lookup
150 * 3 - read
151 * 4 - write
152 */
153 static int proct[NFS_NPROCS] = {
154 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
155 0, 0, 0,
156 };
157
158 /*
159 * There is a congestion window for outstanding rpcs maintained per mount
160 * point. The cwnd size is adjusted in roughly the way that:
161 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
162 * SIGCOMM '88". ACM, August 1988.
163 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
164 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
165 * of rpcs is in progress.
166 * (The sent count and cwnd are scaled for integer arith.)
167 * Variants of "slow start" were tried and were found to be too much of a
168 * performance hit (ave. rtt 3 times larger),
169 * I suspect due to the large rtt that nfs rpcs have.
170 */
171 #define NFS_CWNDSCALE 256
172 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
173 static int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
174 int nfsrtton = 0;
175 struct nfsrtt nfsrtt;
176
177 static int nfs_msg __P((struct proc *,char *,char *));
178 static int nfs_rcvlock __P((struct nfsreq *));
179 static void nfs_rcvunlock __P((int *flagp));
180 static int nfs_receive __P((struct nfsreq *rep, struct mbuf **aname,
181 struct mbuf **mp));
182 static int nfs_reconnect __P((struct nfsreq *rep));
183 #ifndef NFS_NOSERVER
184 static int nfsrv_getstream __P((struct nfssvc_sock *,int));
185
186 int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *nd,
187 struct nfssvc_sock *slp,
188 struct proc *procp,
189 struct mbuf **mreqp)) = {
190 nfsrv_null,
191 nfsrv_getattr,
192 nfsrv_setattr,
193 nfsrv_lookup,
194 nfsrv3_access,
195 nfsrv_readlink,
196 nfsrv_read,
197 nfsrv_write,
198 nfsrv_create,
199 nfsrv_mkdir,
200 nfsrv_symlink,
201 nfsrv_mknod,
202 nfsrv_remove,
203 nfsrv_rmdir,
204 nfsrv_rename,
205 nfsrv_link,
206 nfsrv_readdir,
207 nfsrv_readdirplus,
208 nfsrv_statfs,
209 nfsrv_fsinfo,
210 nfsrv_pathconf,
211 nfsrv_commit,
212 nqnfsrv_getlease,
213 nqnfsrv_vacated,
214 nfsrv_noop,
215 nfsrv_noop
216 };
217 #endif /* NFS_NOSERVER */
218
219 /*
220 * NFSTRACE points were changed to FSDBG (KERNEL_DEBUG)
221 * But some of this code may prove useful someday...
222 */
223 #undef NFSDIAG
224 #if NFSDIAG
225 int nfstraceindx = 0;
226 struct nfstracerec nfstracebuf[NFSTBUFSIZ] = {{0,0,0,0}};
227
228 #define NFSTRACESUSPENDERS
229 #ifdef NFSTRACESUSPENDERS
230 uint nfstracemask = 0xfff00200;
231 int nfstracexid = -1;
232 uint onfstracemask = 0;
233 int nfstracesuspend = -1;
234 #define NFSTRACE_SUSPEND \
235 { \
236 if (nfstracemask) { \
237 onfstracemask = nfstracemask; \
238 nfstracemask = 0; \
239 } \
240 }
241 #define NFSTRACE_RESUME \
242 { \
243 nfstracesuspend = -1; \
244 if (!nfstracemask) \
245 nfstracemask = onfstracemask; \
246 }
247 #define NFSTRACE_STARTSUSPENDCOUNTDOWN \
248 { \
249 nfstracesuspend = (nfstraceindx+100) % NFSTBUFSIZ; \
250 }
251 #define NFSTRACE_SUSPENDING (nfstracesuspend != -1)
252 #define NFSTRACE_SUSPENSEOVER \
253 (nfstracesuspend > 100 ? \
254 (nfstraceindx >= nfstracesuspend || \
255 nfstraceindx < nfstracesuspend - 100) : \
256 (nfstraceindx >= nfstracesuspend && \
257 nfstraceindx < nfstracesuspend + 8192 - 100))
258 #else
259 uint nfstracemask = 0;
260 #endif /* NFSTRACESUSPENDERS */
261
262 int nfsprnttimo = 1;
263
264 int nfsodata[1024];
265 int nfsoprocnum, nfsolen;
266 int nfsbt[32], nfsbtlen;
267
268 #if defined(__ppc__)
269 int
270 backtrace(int *where, int size)
271 {
272 int register sp, *fp, numsaved;
273
274 __asm__ volatile("mr %0,r1" : "=r" (sp));
275
276 fp = (int *)*((int *)sp);
277 size /= sizeof(int);
278 for (numsaved = 0; numsaved < size; numsaved++) {
279 *where++ = fp[2];
280 if ((int)fp <= 0)
281 break;
282 fp = (int *)*fp;
283 }
284 return (numsaved);
285 }
286 #elif defined(__i386__)
287 int
288 backtrace()
289 {
290 return (0); /* Till someone implements a real routine */
291 }
292 #else
293 #error architecture not implemented.
294 #endif
295
296 void
297 nfsdup(struct nfsreq *rep)
298 {
299 int *ip, i, first = 1, end;
300 char *s, b[240];
301 struct mbuf *mb;
302
303 if ((nfs_debug & NFS_DEBUG_DUP) == 0)
304 return;
305 /* last mbuf in chain will be nfs content */
306 for (mb = rep->r_mreq; mb->m_next; mb = mb->m_next)
307 ;
308 if (rep->r_procnum == nfsoprocnum && mb->m_len == nfsolen &&
309 !bcmp((caddr_t)nfsodata, mb->m_data, nfsolen)) {
310 s = b + sprintf(b, "nfsdup x=%x p=%d h=", rep->r_xid,
311 rep->r_procnum);
312 end = (int)(VTONFS(rep->r_vp)->n_fhp);
313 ip = (int *)(end & ~3);
314 end += VTONFS(rep->r_vp)->n_fhsize;
315 while ((int)ip < end) {
316 i = *ip++;
317 if (first) { /* avoid leading zeroes */
318 if (i == 0)
319 continue;
320 first = 0;
321 s += sprintf(s, "%x", i);
322 } else
323 s += sprintf(s, "%08x", i);
324 }
325 if (first)
326 sprintf(s, "%x", 0);
327 else /* eliminate trailing zeroes */
328 while (*--s == '0')
329 *s = 0;
330 /*
331 * set a breakpoint here and you can view the
332 * current backtrace and the one saved in nfsbt
333 */
334 kprintf("%s\n", b);
335 }
336 nfsoprocnum = rep->r_procnum;
337 nfsolen = mb->m_len;
338 bcopy(mb->m_data, (caddr_t)nfsodata, mb->m_len);
339 nfsbtlen = backtrace(&nfsbt, sizeof(nfsbt));
340 }
341 #endif /* NFSDIAG */
342
343 /*
344 * Initialize sockets and congestion for a new NFS connection.
345 * We do not free the sockaddr if error.
346 */
347 int
348 nfs_connect(nmp, rep)
349 register struct nfsmount *nmp;
350 struct nfsreq *rep;
351 {
352 register struct socket *so;
353 int s, error, rcvreserve, sndreserve;
354 struct sockaddr *saddr;
355 struct sockaddr_in sin;
356 u_short tport;
357
358 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
359 nmp->nm_so = (struct socket *)0;
360 saddr = mtod(nmp->nm_nam, struct sockaddr *);
361 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
362 nmp->nm_soproto);
363 if (error) {
364 goto bad;
365 }
366 so = nmp->nm_so;
367 nmp->nm_soflags = so->so_proto->pr_flags;
368
369 /*
370 * Some servers require that the client port be a reserved port number.
371 */
372 if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
373 sin.sin_len = sizeof (struct sockaddr_in);
374 sin.sin_family = AF_INET;
375 sin.sin_addr.s_addr = INADDR_ANY;
376 tport = IPPORT_RESERVED - 1;
377 sin.sin_port = htons(tport);
378
379 while ((error = sobind(so, (struct sockaddr *) &sin) == EADDRINUSE) &&
380 (--tport > IPPORT_RESERVED / 2))
381 sin.sin_port = htons(tport);
382 if (error) {
383 goto bad;
384 }
385 }
386
387 /*
388 * Protocols that do not require connections may be optionally left
389 * unconnected for servers that reply from a port other than NFS_PORT.
390 */
391 if (nmp->nm_flag & NFSMNT_NOCONN) {
392 if (nmp->nm_soflags & PR_CONNREQUIRED) {
393 error = ENOTCONN;
394 goto bad;
395 }
396 } else {
397 error = soconnect(so, mtod(nmp->nm_nam, struct sockaddr *));
398 if (error) {
399 goto bad;
400 }
401
402 /*
403 * Wait for the connection to complete. Cribbed from the
404 * connect system call but with the wait timing out so
405 * that interruptible mounts don't hang here for a long time.
406 */
407 s = splnet();
408 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
409 (void) tsleep((caddr_t)&so->so_timeo, PSOCK,
410 "nfscon", 2 * hz);
411 if ((so->so_state & SS_ISCONNECTING) &&
412 so->so_error == 0 && rep &&
413 (error = nfs_sigintr(nmp, rep, rep->r_procp))) {
414 so->so_state &= ~SS_ISCONNECTING;
415 splx(s);
416 goto bad;
417 }
418 }
419 if (so->so_error) {
420 error = so->so_error;
421 so->so_error = 0;
422 splx(s);
423 goto bad;
424 }
425 splx(s);
426 }
427 if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT)) {
428 so->so_rcv.sb_timeo = (5 * hz);
429 so->so_snd.sb_timeo = (5 * hz);
430 } else {
431 so->so_rcv.sb_timeo = 0;
432 so->so_snd.sb_timeo = 0;
433 }
434 if (nmp->nm_sotype == SOCK_DGRAM) {
435 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
436 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR) * 2;
437 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
438 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
439 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR) * 2;
440 } else {
441 if (nmp->nm_sotype != SOCK_STREAM)
442 panic("nfscon sotype");
443
444 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
445 struct sockopt sopt;
446 int val;
447
448 bzero(&sopt, sizeof sopt);
449 sopt.sopt_level = SOL_SOCKET;
450 sopt.sopt_name = SO_KEEPALIVE;
451 sopt.sopt_val = &val;
452 sopt.sopt_valsize = sizeof val;
453 val = 1;
454 sosetopt(so, &sopt);
455 }
456 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
457 struct sockopt sopt;
458 int val;
459
460 bzero(&sopt, sizeof sopt);
461 sopt.sopt_level = IPPROTO_TCP;
462 sopt.sopt_name = TCP_NODELAY;
463 sopt.sopt_val = &val;
464 sopt.sopt_valsize = sizeof val;
465 val = 1;
466 sosetopt(so, &sopt);
467 }
468
469 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR + sizeof (u_long))
470 * 2;
471 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR + sizeof (u_long))
472 * 2;
473 }
474
475 error = soreserve(so, sndreserve, rcvreserve);
476 if (error) {
477 goto bad;
478 }
479 so->so_rcv.sb_flags |= SB_NOINTR;
480 so->so_snd.sb_flags |= SB_NOINTR;
481
482 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
483
484 /* Initialize other non-zero congestion variables */
485 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
486 nmp->nm_srtt[3] = (NFS_TIMEO << 3);
487 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
488 nmp->nm_sdrtt[3] = 0;
489 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
490 nmp->nm_sent = 0;
491 FSDBG(529, nmp, nmp->nm_flag, nmp->nm_soflags, nmp->nm_cwnd);
492 nmp->nm_timeouts = 0;
493 return (0);
494
495 bad:
496 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
497 nfs_disconnect(nmp);
498 return (error);
499 }
500
501 /*
502 * Reconnect routine:
503 * Called when a connection is broken on a reliable protocol.
504 * - clean up the old socket
505 * - nfs_connect() again
506 * - set R_MUSTRESEND for all outstanding requests on mount point
507 * If this fails the mount point is DEAD!
508 * nb: Must be called with the nfs_sndlock() set on the mount point.
509 */
510 static int
511 nfs_reconnect(rep)
512 register struct nfsreq *rep;
513 {
514 register struct nfsreq *rp;
515 register struct nfsmount *nmp = rep->r_nmp;
516 int error;
517
518 nfs_disconnect(nmp);
519 while ((error = nfs_connect(nmp, rep))) {
520 if (error == EINTR || error == ERESTART)
521 return (EINTR);
522 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfscon", 0);
523 }
524
525 NFS_DPF(DUP, ("nfs_reconnect RESEND\n"));
526 /*
527 * Loop through outstanding request list and fix up all requests
528 * on old socket.
529 */
530 for (rp = nfs_reqq.tqh_first; rp != 0; rp = rp->r_chain.tqe_next) {
531 if (rp->r_nmp == nmp)
532 rp->r_flags |= R_MUSTRESEND;
533 }
534 return (0);
535 }
536
537 /*
538 * NFS disconnect. Clean up and unlink.
539 */
540 void
541 nfs_disconnect(nmp)
542 register struct nfsmount *nmp;
543 {
544 register struct socket *so;
545
546 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
547 if (nmp->nm_so) {
548 so = nmp->nm_so;
549 nmp->nm_so = (struct socket *)0;
550 soshutdown(so, 2);
551 soclose(so);
552 }
553 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
554 }
555
556 /*
557 * This is the nfs send routine. For connection based socket types, it
558 * must be called with an nfs_sndlock() on the socket.
559 * "rep == NULL" indicates that it has been called from a server.
560 * For the client side:
561 * - return EINTR if the RPC is terminated, 0 otherwise
562 * - set R_MUSTRESEND if the send fails for any reason
563 * - do any cleanup required by recoverable socket errors (???)
564 * For the server side:
565 * - return EINTR or ERESTART if interrupted by a signal
566 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
567 * - do any cleanup required by recoverable socket errors (???)
568 */
569 int
570 nfs_send(so, nam, top, rep)
571 register struct socket *so;
572 struct mbuf *nam;
573 register struct mbuf *top;
574 struct nfsreq *rep;
575 {
576 struct sockaddr *sendnam;
577 int error, soflags, flags;
578 int xidqueued = 0;
579 struct nfsreq *rp;
580 char savenametolog[MNAMELEN];
581
582 if (rep) {
583 if (rep->r_flags & R_SOFTTERM) {
584 m_freem(top);
585 return (EINTR);
586 }
587 if ((so = rep->r_nmp->nm_so) == NULL) {
588 rep->r_flags |= R_MUSTRESEND;
589 m_freem(top);
590 return (0);
591 }
592 rep->r_flags &= ~R_MUSTRESEND;
593 soflags = rep->r_nmp->nm_soflags;
594 for (rp = nfs_reqq.tqh_first; rp; rp = rp->r_chain.tqe_next)
595 if (rp == rep)
596 break;
597 if (rp)
598 xidqueued = rp->r_xid;
599 } else
600 soflags = so->so_proto->pr_flags;
601 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED) ||
602 (nam == 0))
603 sendnam = (struct sockaddr *)0;
604 else
605 sendnam = mtod(nam, struct sockaddr *);
606
607 if (so->so_type == SOCK_SEQPACKET)
608 flags = MSG_EOR;
609 else
610 flags = 0;
611
612 #if NFSDIAG
613 if (rep)
614 nfsdup(rep);
615 #endif
616 /*
617 * Save the name here in case mount point goes away when we switch
618 * funnels. The name is using local stack and is large, but don't
619 * want to block if we malloc.
620 */
621 if (rep)
622 strncpy(savenametolog,
623 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname,
624 MNAMELEN);
625 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
626 error = sosend(so, sendnam, (struct uio *)0, top,
627 (struct mbuf *)0, flags);
628 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
629
630 if (error) {
631 if (rep) {
632 if (xidqueued) {
633 for (rp = nfs_reqq.tqh_first; rp;
634 rp = rp->r_chain.tqe_next)
635 if (rp == rep && rp->r_xid == xidqueued)
636 break;
637 if (!rp)
638 panic("nfs_send: error %d xid %x gone",
639 error, xidqueued);
640 }
641 log(LOG_INFO, "nfs send error %d for server %s\n",
642 error, savenametolog);
643 /*
644 * Deal with errors for the client side.
645 */
646 if (rep->r_flags & R_SOFTTERM)
647 error = EINTR;
648 else {
649 rep->r_flags |= R_MUSTRESEND;
650 NFS_DPF(DUP,
651 ("nfs_send RESEND error=%d\n", error));
652 }
653 } else
654 log(LOG_INFO, "nfsd send error %d\n", error);
655
656 /*
657 * Handle any recoverable (soft) socket errors here. (???)
658 */
659 if (error != EINTR && error != ERESTART &&
660 error != EWOULDBLOCK && error != EPIPE)
661 error = 0;
662 }
663 return (error);
664 }
665
666 /*
667 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
668 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
669 * Mark and consolidate the data into a new mbuf list.
670 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
671 * small mbufs.
672 * For SOCK_STREAM we must be very careful to read an entire record once
673 * we have read any of it, even if the system call has been interrupted.
674 */
675 static int
676 nfs_receive(rep, aname, mp)
677 register struct nfsreq *rep;
678 struct mbuf **aname;
679 struct mbuf **mp;
680 {
681 register struct socket *so;
682 struct uio auio;
683 struct iovec aio;
684 register struct mbuf *m;
685 struct mbuf *control;
686 u_long len;
687 struct sockaddr **getnam;
688 struct sockaddr *tmp_nam;
689 struct mbuf *mhck;
690 struct sockaddr_in *sin;
691 int error, sotype, rcvflg;
692 struct proc *p = current_proc(); /* XXX */
693
694 /*
695 * Set up arguments for soreceive()
696 */
697 *mp = (struct mbuf *)0;
698 *aname = (struct mbuf *)0;
699 sotype = rep->r_nmp->nm_sotype;
700
701 /*
702 * For reliable protocols, lock against other senders/receivers
703 * in case a reconnect is necessary.
704 * For SOCK_STREAM, first get the Record Mark to find out how much
705 * more there is to get.
706 * We must lock the socket against other receivers
707 * until we have an entire rpc request/reply.
708 */
709 if (sotype != SOCK_DGRAM) {
710 error = nfs_sndlock(&rep->r_nmp->nm_flag, rep);
711 if (error)
712 return (error);
713 tryagain:
714 /*
715 * Check for fatal errors and resending request.
716 */
717 /*
718 * Ugh: If a reconnect attempt just happened, nm_so
719 * would have changed. NULL indicates a failed
720 * attempt that has essentially shut down this
721 * mount point.
722 */
723 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
724 nfs_sndunlock(&rep->r_nmp->nm_flag);
725 return (EINTR);
726 }
727 so = rep->r_nmp->nm_so;
728 if (!so) {
729 error = nfs_reconnect(rep);
730 if (error) {
731 nfs_sndunlock(&rep->r_nmp->nm_flag);
732 return (error);
733 }
734 goto tryagain;
735 }
736 while (rep->r_flags & R_MUSTRESEND) {
737 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
738 nfsstats.rpcretries++;
739 NFS_DPF(DUP,
740 ("nfs_receive RESEND %s\n",
741 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname));
742 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
743 /*
744 * we also hold rcv lock so rep is still
745 * legit this point
746 */
747 if (error) {
748 if (error == EINTR || error == ERESTART ||
749 (error = nfs_reconnect(rep))) {
750 nfs_sndunlock(&rep->r_nmp->nm_flag);
751 return (error);
752 }
753 goto tryagain;
754 }
755 }
756 nfs_sndunlock(&rep->r_nmp->nm_flag);
757 if (sotype == SOCK_STREAM) {
758 aio.iov_base = (caddr_t) &len;
759 aio.iov_len = sizeof(u_long);
760 auio.uio_iov = &aio;
761 auio.uio_iovcnt = 1;
762 auio.uio_segflg = UIO_SYSSPACE;
763 auio.uio_rw = UIO_READ;
764 auio.uio_offset = 0;
765 auio.uio_resid = sizeof(u_long);
766 auio.uio_procp = p;
767 do {
768 rcvflg = MSG_WAITALL;
769 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
770 error = soreceive(so, (struct sockaddr **)0, &auio,
771 (struct mbuf **)0, (struct mbuf **)0, &rcvflg);
772 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
773 if (!rep->r_nmp) /* if unmounted then bailout */
774 goto shutout;
775 if (error == EWOULDBLOCK && rep) {
776 if (rep->r_flags & R_SOFTTERM)
777 return (EINTR);
778 }
779 } while (error == EWOULDBLOCK);
780 if (!error && auio.uio_resid > 0) {
781 log(LOG_INFO,
782 "short receive (%d/%d) from nfs server %s\n",
783 sizeof(u_long) - auio.uio_resid,
784 sizeof(u_long),
785 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
786 error = EPIPE;
787 }
788 if (error)
789 goto errout;
790 len = ntohl(len) & ~0x80000000;
791 /*
792 * This is SERIOUS! We are out of sync with the sender
793 * and forcing a disconnect/reconnect is all I can do.
794 */
795 if (len > NFS_MAXPACKET) {
796 log(LOG_ERR, "%s (%d) from nfs server %s\n",
797 "impossible packet length",
798 len,
799 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
800 error = EFBIG;
801 goto errout;
802 }
803 auio.uio_resid = len;
804
805 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
806 do {
807 rcvflg = MSG_WAITALL;
808 error = soreceive(so, (struct sockaddr **)0,
809 &auio, mp, (struct mbuf **)0, &rcvflg);
810 if (!rep->r_nmp) /* if unmounted then bailout */ {
811 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
812 goto shutout;
813 }
814 } while (error == EWOULDBLOCK || error == EINTR ||
815 error == ERESTART);
816
817 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
818
819 if (!error && auio.uio_resid > 0) {
820 log(LOG_INFO,
821 "short receive (%d/%d) from nfs server %s\n",
822 len - auio.uio_resid, len,
823 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
824 error = EPIPE;
825 }
826 } else {
827 /*
828 * NB: Since uio_resid is big, MSG_WAITALL is ignored
829 * and soreceive() will return when it has either a
830 * control msg or a data msg.
831 * We have no use for control msg., but must grab them
832 * and then throw them away so we know what is going
833 * on.
834 */
835 auio.uio_resid = len = 100000000; /* Anything Big */
836 auio.uio_procp = p;
837
838 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
839 do {
840 rcvflg = 0;
841 error = soreceive(so, (struct sockaddr **)0,
842 &auio, mp, &control, &rcvflg);
843 if (!rep->r_nmp) /* if unmounted then bailout */ {
844 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
845 goto shutout;
846 }
847 if (control)
848 m_freem(control);
849 if (error == EWOULDBLOCK && rep) {
850 if (rep->r_flags & R_SOFTTERM) {
851 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
852 return (EINTR);
853 }
854 }
855 } while (error == EWOULDBLOCK ||
856 (!error && *mp == NULL && control));
857
858 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
859
860 if ((rcvflg & MSG_EOR) == 0)
861 printf("Egad!!\n");
862 if (!error && *mp == NULL)
863 error = EPIPE;
864 len -= auio.uio_resid;
865 }
866 errout:
867 if (error && error != EINTR && error != ERESTART) {
868 m_freem(*mp);
869 *mp = (struct mbuf *)0;
870 if (error != EPIPE)
871 log(LOG_INFO,
872 "receive error %d from nfs server %s\n",
873 error,
874 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
875 error = nfs_sndlock(&rep->r_nmp->nm_flag, rep);
876 if (!error)
877 error = nfs_reconnect(rep);
878 if (!error)
879 goto tryagain;
880 }
881 } else {
882 if ((so = rep->r_nmp->nm_so) == NULL)
883 return (EACCES);
884 if (so->so_state & SS_ISCONNECTED)
885 getnam = (struct sockaddr **)0;
886 else
887 getnam = &tmp_nam;;
888 auio.uio_resid = len = 1000000;
889 auio.uio_procp = p;
890
891 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
892 do {
893 rcvflg = 0;
894 error = soreceive(so, getnam, &auio, mp,
895 (struct mbuf **)0, &rcvflg);
896
897 if ((getnam) && (*getnam)) {
898 MGET(mhck, M_WAIT, MT_SONAME);
899 mhck->m_len = (*getnam)->sa_len;
900 sin = mtod(mhck, struct sockaddr_in *);
901 bcopy(*getnam, sin, sizeof(struct sockaddr_in));
902 mhck->m_hdr.mh_len = sizeof(struct sockaddr_in);
903 FREE(*getnam, M_SONAME);
904 *aname = mhck;
905 }
906 if (!rep->r_nmp) /* if unmounted then bailout */ {
907 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
908 goto shutout;
909 }
910
911 if (error == EWOULDBLOCK &&
912 (rep->r_flags & R_SOFTTERM)) {
913 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
914 return (EINTR);
915 }
916 } while (error == EWOULDBLOCK);
917
918 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
919 len -= auio.uio_resid;
920 }
921 shutout:
922 if (error) {
923 m_freem(*mp);
924 *mp = (struct mbuf *)0;
925 }
926 return (error);
927 }
928
929 /*
930 * Implement receipt of reply on a socket.
931 * We must search through the list of received datagrams matching them
932 * with outstanding requests using the xid, until ours is found.
933 */
934 /* ARGSUSED */
935 int
936 nfs_reply(myrep)
937 struct nfsreq *myrep;
938 {
939 register struct nfsreq *rep;
940 register struct nfsmount *nmp = myrep->r_nmp;
941 register long t1;
942 struct mbuf *mrep, *md;
943 struct mbuf *nam;
944 u_long rxid, *tl;
945 caddr_t dpos, cp2;
946 int error;
947
948 /*
949 * Loop around until we get our own reply
950 */
951 for (;;) {
952 /*
953 * Lock against other receivers so that I don't get stuck in
954 * sbwait() after someone else has received my reply for me.
955 * Also necessary for connection based protocols to avoid
956 * race conditions during a reconnect.
957 * If nfs_rcvlock() returns EALREADY, that means that
958 * the reply has already been recieved by another
959 * process and we can return immediately. In this
960 * case, the lock is not taken to avoid races with
961 * other processes.
962 */
963 error = nfs_rcvlock(myrep);
964 if (error == EALREADY)
965 return (0);
966 if (error)
967 return (error);
968
969 /*
970 * If we slept after putting bits otw, then reply may have
971 * arrived. In which case returning is required, or we
972 * would hang trying to nfs_receive an already received reply.
973 */
974 if (myrep->r_mrep != NULL) {
975 nfs_rcvunlock(&nmp->nm_flag);
976 FSDBG(530, myrep->r_xid, myrep, myrep->r_nmp, -1);
977 return (0);
978 }
979 /*
980 * Get the next Rpc reply off the socket. Assume myrep->r_nmp
981 * is still intact by checks done in nfs_rcvlock.
982 */
983 error = nfs_receive(myrep, &nam, &mrep);
984 /*
985 * Bailout asap if nfsmount struct gone (unmounted).
986 */
987 if (!myrep->r_nmp) {
988 FSDBG(530, myrep->r_xid, myrep, nmp, -2);
989 return (ECONNABORTED);
990 }
991 if (error) {
992 FSDBG(530, myrep->r_xid, myrep, nmp, error);
993 nfs_rcvunlock(&nmp->nm_flag);
994
995 /*
996 * Ignore routing errors on connectionless protocols??
997 */
998 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
999 nmp->nm_so->so_error = 0;
1000 if (myrep->r_flags & R_GETONEREP)
1001 return (0);
1002 continue;
1003 }
1004 return (error);
1005 }
1006 if (nam)
1007 m_freem(nam);
1008
1009 /*
1010 * We assume all is fine, but if we did not have an error
1011 * and mrep is 0, better not dereference it. nfs_receieve
1012 * calls soreceive which carefully sets error=0 when it got
1013 * errors on sbwait (tsleep). In most cases, I assume that's
1014 * so we could go back again. In tcp case, EPIPE is returned.
1015 * In udp, case nfs_receive gets back here with no error and no
1016 * mrep. Is the right fix to have soreceive check for process
1017 * aborted after sbwait and return something non-zero? Should
1018 * nfs_receive give an EPIPE? Too risky to play with those
1019 * two this late in game for a shutdown problem. Instead,
1020 * just check here and get out. (ekn)
1021 */
1022 if (!mrep) {
1023 FSDBG(530, myrep->r_xid, myrep, nmp, -3);
1024 return (ECONNABORTED); /* sounds good */
1025 }
1026
1027 /*
1028 * Get the xid and check that it is an rpc reply
1029 */
1030 md = mrep;
1031 dpos = mtod(md, caddr_t);
1032 nfsm_dissect(tl, u_long *, 2*NFSX_UNSIGNED);
1033 rxid = *tl++;
1034 if (*tl != rpc_reply) {
1035 #ifndef NFS_NOSERVER
1036 if (nmp->nm_flag & NFSMNT_NQNFS) {
1037 if (nqnfs_callback(nmp, mrep, md, dpos))
1038 nfsstats.rpcinvalid++;
1039 } else {
1040 nfsstats.rpcinvalid++;
1041 m_freem(mrep);
1042 }
1043 #else
1044 nfsstats.rpcinvalid++;
1045 m_freem(mrep);
1046 #endif
1047 nfsmout:
1048 if (nmp->nm_flag & NFSMNT_RCVLOCK)
1049 nfs_rcvunlock(&nmp->nm_flag);
1050 if (myrep->r_flags & R_GETONEREP)
1051 return (0); /* this path used by NQNFS */
1052 continue;
1053 }
1054
1055 /*
1056 * Loop through the request list to match up the reply
1057 * Iff no match, just drop the datagram
1058 */
1059 for (rep = nfs_reqq.tqh_first; rep != 0;
1060 rep = rep->r_chain.tqe_next) {
1061 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
1062 /* Found it.. */
1063 rep->r_mrep = mrep;
1064 rep->r_md = md;
1065 rep->r_dpos = dpos;
1066 if (nfsrtton) {
1067 struct rttl *rt;
1068
1069 rt = &nfsrtt.rttl[nfsrtt.pos];
1070 rt->proc = rep->r_procnum;
1071 rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
1072 rt->sent = nmp->nm_sent;
1073 rt->cwnd = nmp->nm_cwnd;
1074 if (proct[rep->r_procnum] == 0)
1075 panic("nfs_reply: proct[%d] is zero", rep->r_procnum);
1076 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
1077 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
1078 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid;
1079 rt->tstamp = time;
1080 if (rep->r_flags & R_TIMING)
1081 rt->rtt = rep->r_rtt;
1082 else
1083 rt->rtt = 1000000;
1084 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
1085 }
1086 /*
1087 * Update congestion window.
1088 * Do the additive increase of
1089 * one rpc/rtt.
1090 */
1091 FSDBG(530, rep->r_xid, rep, nmp->nm_sent,
1092 nmp->nm_cwnd);
1093 if (nmp->nm_cwnd <= nmp->nm_sent) {
1094 nmp->nm_cwnd +=
1095 (NFS_CWNDSCALE * NFS_CWNDSCALE +
1096 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
1097 if (nmp->nm_cwnd > NFS_MAXCWND)
1098 nmp->nm_cwnd = NFS_MAXCWND;
1099 }
1100 if (!(rep->r_flags & R_SENT))
1101 printf("nfs_reply: unsent xid=%x",
1102 rep->r_xid);
1103 rep->r_flags &= ~R_SENT;
1104 nmp->nm_sent -= NFS_CWNDSCALE;
1105 /*
1106 * Update rtt using a gain of 0.125 on the mean
1107 * and a gain of 0.25 on the deviation.
1108 */
1109 if (rep->r_flags & R_TIMING) {
1110 /*
1111 * Since the timer resolution of
1112 * NFS_HZ is so course, it can often
1113 * result in r_rtt == 0. Since
1114 * r_rtt == N means that the actual
1115 * rtt is between N+dt and N+2-dt ticks,
1116 * add 1.
1117 */
1118 if (proct[rep->r_procnum] == 0)
1119 panic("nfs_reply: proct[%d] is zero", rep->r_procnum);
1120 t1 = rep->r_rtt + 1;
1121 t1 -= (NFS_SRTT(rep) >> 3);
1122 NFS_SRTT(rep) += t1;
1123 if (t1 < 0)
1124 t1 = -t1;
1125 t1 -= (NFS_SDRTT(rep) >> 2);
1126 NFS_SDRTT(rep) += t1;
1127 }
1128 nmp->nm_timeouts = 0;
1129 break;
1130 }
1131 }
1132 nfs_rcvunlock(&nmp->nm_flag);
1133 /*
1134 * If not matched to a request, drop it.
1135 * If it's mine, get out.
1136 */
1137 if (rep == 0) {
1138 nfsstats.rpcunexpected++;
1139 m_freem(mrep);
1140 } else if (rep == myrep) {
1141 if (rep->r_mrep == NULL)
1142 panic("nfs_reply: nil r_mrep");
1143 return (0);
1144 }
1145 FSDBG(530, myrep->r_xid, myrep, rep,
1146 rep ? rep->r_xid : myrep->r_flags);
1147 if (myrep->r_flags & R_GETONEREP)
1148 return (0); /* this path used by NQNFS */
1149 }
1150 }
1151
1152 /*
1153 * nfs_request - goes something like this
1154 * - fill in request struct
1155 * - links it into list
1156 * - calls nfs_send() for first transmit
1157 * - calls nfs_receive() to get reply
1158 * - break down rpc header and return with nfs reply pointed to
1159 * by mrep or error
1160 * nb: always frees up mreq mbuf list
1161 */
1162 int
1163 nfs_request(vp, mrest, procnum, procp, cred, mrp, mdp, dposp, xidp)
1164 struct vnode *vp;
1165 struct mbuf *mrest;
1166 int procnum;
1167 struct proc *procp;
1168 struct ucred *cred;
1169 struct mbuf **mrp;
1170 struct mbuf **mdp;
1171 caddr_t *dposp;
1172 u_int64_t *xidp;
1173 {
1174 register struct mbuf *m, *mrep;
1175 register struct nfsreq *rep, *rp;
1176 register u_long *tl;
1177 register int i;
1178 struct nfsmount *nmp;
1179 struct mbuf *md, *mheadend;
1180 struct nfsnode *np;
1181 char nickv[RPCX_NICKVERF];
1182 time_t reqtime, waituntil;
1183 caddr_t dpos, cp2;
1184 int t1, nqlflag, cachable, s, error = 0, mrest_len, auth_len, auth_type;
1185 int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0, failed_auth = 0;
1186 int verf_len, verf_type;
1187 u_long xid;
1188 u_quad_t frev;
1189 char *auth_str, *verf_str;
1190 NFSKERBKEY_T key; /* save session key */
1191
1192 if (xidp)
1193 *xidp = 0;
1194 nmp = VFSTONFS(vp->v_mount);
1195 MALLOC_ZONE(rep, struct nfsreq *,
1196 sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
1197 FSDBG_TOP(531, vp, procnum, nmp, rep);
1198
1199 /*
1200 * make sure if we blocked above, that the file system didn't get
1201 * unmounted leaving nmp bogus value to trip on later and crash.
1202 * Note nfs_unmount will set rep->r_nmp if unmounted volume, but we
1203 * aren't that far yet. SO this is best we can do. I wanted to check
1204 * for vp->v_mount = 0 also below, but that caused reboot crash.
1205 * Something must think it's okay for vp-v_mount=0 during booting.
1206 * Thus the best I can do here is see if we still have a vnode.
1207 */
1208
1209 if (vp->v_type == VBAD) {
1210 FSDBG_BOT(531, 1, vp, nmp, rep);
1211 _FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1212 return (EINVAL);
1213 }
1214 rep->r_nmp = nmp;
1215 rep->r_vp = vp;
1216 rep->r_procp = procp;
1217 rep->r_procnum = procnum;
1218 i = 0;
1219 m = mrest;
1220 while (m) {
1221 i += m->m_len;
1222 m = m->m_next;
1223 }
1224 mrest_len = i;
1225
1226 /*
1227 * Get the RPC header with authorization.
1228 */
1229 kerbauth:
1230 verf_str = auth_str = (char *)0;
1231 if (nmp->nm_flag & NFSMNT_KERB) {
1232 verf_str = nickv;
1233 verf_len = sizeof (nickv);
1234 auth_type = RPCAUTH_KERB4;
1235 bzero((caddr_t)key, sizeof (key));
1236 if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
1237 &auth_len, verf_str, verf_len)) {
1238 error = nfs_getauth(nmp, rep, cred, &auth_str,
1239 &auth_len, verf_str, &verf_len, key);
1240 if (error) {
1241 FSDBG_BOT(531, 2, vp, error, rep);
1242 _FREE_ZONE((caddr_t)rep,
1243 sizeof (struct nfsreq), M_NFSREQ);
1244 m_freem(mrest);
1245 return (error);
1246 }
1247 }
1248 } else {
1249 auth_type = RPCAUTH_UNIX;
1250 if (cred->cr_ngroups < 1)
1251 panic("nfsreq nogrps");
1252 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
1253 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
1254 5 * NFSX_UNSIGNED;
1255 }
1256 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
1257 auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
1258 if (xidp)
1259 *xidp = xid + ((u_int64_t)nfs_xidwrap << 32);
1260 if (auth_str)
1261 _FREE(auth_str, M_TEMP);
1262
1263 /*
1264 * For stream protocols, insert a Sun RPC Record Mark.
1265 */
1266 if (nmp->nm_sotype == SOCK_STREAM) {
1267 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
1268 *mtod(m, u_long *) = htonl(0x80000000 |
1269 (m->m_pkthdr.len - NFSX_UNSIGNED));
1270 }
1271 rep->r_mreq = m;
1272 rep->r_xid = xid;
1273 tryagain:
1274 if (nmp->nm_flag & NFSMNT_SOFT)
1275 rep->r_retry = nmp->nm_retry;
1276 else
1277 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1278 rep->r_rtt = rep->r_rexmit = 0;
1279 if (proct[procnum] > 0)
1280 rep->r_flags = R_TIMING;
1281 else
1282 rep->r_flags = 0;
1283 rep->r_mrep = NULL;
1284
1285 /*
1286 * Do the client side RPC.
1287 */
1288 nfsstats.rpcrequests++;
1289 /*
1290 * Chain request into list of outstanding requests. Be sure
1291 * to put it LAST so timer finds oldest requests first.
1292 */
1293 s = splsoftclock();
1294 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1295
1296 /* Get send time for nqnfs */
1297 reqtime = time.tv_sec;
1298
1299 /*
1300 * If backing off another request or avoiding congestion, don't
1301 * send this one now but let timer do it. If not timing a request,
1302 * do it now.
1303 */
1304 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
1305 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1306 nmp->nm_sent < nmp->nm_cwnd)) {
1307 splx(s);
1308 if (nmp->nm_soflags & PR_CONNREQUIRED)
1309 error = nfs_sndlock(&nmp->nm_flag, rep);
1310
1311 /*
1312 * Set the R_SENT before doing the send in case another thread
1313 * processes the reply before the nfs_send returns here
1314 */
1315 if (!error) {
1316 if ((rep->r_flags & R_MUSTRESEND) == 0) {
1317 FSDBG(531, rep->r_xid, rep, nmp->nm_sent,
1318 nmp->nm_cwnd);
1319 nmp->nm_sent += NFS_CWNDSCALE;
1320 rep->r_flags |= R_SENT;
1321 }
1322
1323 m = m_copym(m, 0, M_COPYALL, M_WAIT);
1324 error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep);
1325 if (nmp->nm_soflags & PR_CONNREQUIRED)
1326 nfs_sndunlock(&nmp->nm_flag);
1327 }
1328 if (error) {
1329 nmp->nm_sent -= NFS_CWNDSCALE;
1330 rep->r_flags &= ~R_SENT;
1331 }
1332 } else {
1333 splx(s);
1334 rep->r_rtt = -1;
1335 }
1336
1337 /*
1338 * Wait for the reply from our send or the timer's.
1339 */
1340 if (!error || error == EPIPE)
1341 error = nfs_reply(rep);
1342
1343 /*
1344 * RPC done, unlink the request.
1345 */
1346 s = splsoftclock();
1347 for (rp = nfs_reqq.tqh_first; rp;
1348 rp = rp->r_chain.tqe_next)
1349 if (rp == rep && rp->r_xid == xid)
1350 break;
1351 if (!rp)
1352 panic("nfs_request race, rep %x xid %x", rep, xid);
1353 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1354 splx(s);
1355
1356 /*
1357 * Decrement the outstanding request count.
1358 */
1359 if (rep->r_flags & R_SENT) {
1360 FSDBG(531, rep->r_xid, rep, nmp->nm_sent, nmp->nm_cwnd);
1361 rep->r_flags &= ~R_SENT; /* paranoia */
1362 nmp->nm_sent -= NFS_CWNDSCALE;
1363 }
1364
1365 /*
1366 * If there was a successful reply and a tprintf msg.
1367 * tprintf a response.
1368 */
1369 if (!error && (rep->r_flags & R_TPRINTFMSG))
1370 nfs_msg(rep->r_procp, nmp->nm_mountp->mnt_stat.f_mntfromname,
1371 "is alive again");
1372 mrep = rep->r_mrep;
1373 md = rep->r_md;
1374 dpos = rep->r_dpos;
1375 if (error) {
1376 m_freem(rep->r_mreq);
1377 FSDBG_BOT(531, error, rep->r_xid, nmp, rep);
1378 _FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1379 return (error);
1380 }
1381
1382 /*
1383 * break down the rpc header and check if ok
1384 */
1385 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
1386 if (*tl++ == rpc_msgdenied) {
1387 if (*tl == rpc_mismatch)
1388 error = EOPNOTSUPP;
1389 else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
1390 if (!failed_auth) {
1391 failed_auth++;
1392 mheadend->m_next = (struct mbuf *)0;
1393 m_freem(mrep);
1394 m_freem(rep->r_mreq);
1395 goto kerbauth;
1396 } else
1397 error = EAUTH;
1398 } else
1399 error = EACCES;
1400 m_freem(mrep);
1401 m_freem(rep->r_mreq);
1402 FSDBG_BOT(531, error, rep->r_xid, nmp, rep);
1403 _FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1404 return (error);
1405 }
1406
1407 /*
1408 * Grab any Kerberos verifier, otherwise just throw it away.
1409 */
1410 verf_type = fxdr_unsigned(int, *tl++);
1411 i = fxdr_unsigned(int, *tl);
1412 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
1413 error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
1414 if (error)
1415 goto nfsmout;
1416 } else if (i > 0)
1417 nfsm_adv(nfsm_rndup(i));
1418 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1419 /* 0 == ok */
1420 if (*tl == 0) {
1421 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1422 if (*tl != 0) {
1423 error = fxdr_unsigned(int, *tl);
1424 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1425 error == NFSERR_TRYLATER) {
1426 m_freem(mrep);
1427 error = 0;
1428 waituntil = time.tv_sec + trylater_delay;
1429 NFS_DPF(DUP,
1430 ("nfs_request %s flag=%x trylater_cnt=%x waituntil=%lx trylater_delay=%x\n",
1431 nmp->nm_mountp->mnt_stat.f_mntfromname,
1432 nmp->nm_flag, trylater_cnt, waituntil,
1433 trylater_delay));
1434 while (time.tv_sec < waituntil)
1435 (void)tsleep((caddr_t)&lbolt,
1436 PSOCK, "nqnfstry", 0);
1437 trylater_delay *= nfs_backoff[trylater_cnt];
1438 if (trylater_cnt < 7)
1439 trylater_cnt++;
1440 goto tryagain;
1441 }
1442
1443 /*
1444 * If the File Handle was stale, invalidate the
1445 * lookup cache, just in case.
1446 */
1447 if (error == ESTALE)
1448 cache_purge(vp);
1449 if (nmp->nm_flag & NFSMNT_NFSV3) {
1450 *mrp = mrep;
1451 *mdp = md;
1452 *dposp = dpos;
1453 error |= NFSERR_RETERR;
1454 } else
1455 m_freem(mrep);
1456 m_freem(rep->r_mreq);
1457 FSDBG_BOT(531, error, rep->r_xid, nmp, rep);
1458 _FREE_ZONE((caddr_t)rep,
1459 sizeof (struct nfsreq), M_NFSREQ);
1460 return (error);
1461 }
1462
1463 /*
1464 * For nqnfs, get any lease in reply
1465 */
1466 if (nmp->nm_flag & NFSMNT_NQNFS) {
1467 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1468 if (*tl) {
1469 np = VTONFS(vp);
1470 nqlflag = fxdr_unsigned(int, *tl);
1471 nfsm_dissect(tl, u_long *, 4*NFSX_UNSIGNED);
1472 cachable = fxdr_unsigned(int, *tl++);
1473 reqtime += fxdr_unsigned(int, *tl++);
1474 if (reqtime > time.tv_sec) {
1475 fxdr_hyper(tl, &frev);
1476 nqnfs_clientlease(nmp, np, nqlflag,
1477 cachable, reqtime, frev);
1478 }
1479 }
1480 }
1481 *mrp = mrep;
1482 *mdp = md;
1483 *dposp = dpos;
1484 m_freem(rep->r_mreq);
1485 FSDBG_BOT(531, 0xf0f0f0f0, rep->r_xid, nmp, rep);
1486 FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1487 return (0);
1488 }
1489 m_freem(mrep);
1490 error = EPROTONOSUPPORT;
1491 nfsmout:
1492 m_freem(rep->r_mreq);
1493 FSDBG_BOT(531, error, rep->r_xid, nmp, rep);
1494 _FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1495 return (error);
1496 }
1497
1498 #ifndef NFS_NOSERVER
1499 /*
1500 * Generate the rpc reply header
1501 * siz arg. is used to decide if adding a cluster is worthwhile
1502 */
1503 int
1504 nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp)
1505 int siz;
1506 struct nfsrv_descript *nd;
1507 struct nfssvc_sock *slp;
1508 int err;
1509 int cache;
1510 u_quad_t *frev;
1511 struct mbuf **mrq;
1512 struct mbuf **mbp;
1513 caddr_t *bposp;
1514 {
1515 register u_long *tl;
1516 register struct mbuf *mreq;
1517 caddr_t bpos;
1518 struct mbuf *mb, *mb2;
1519
1520 MGETHDR(mreq, M_WAIT, MT_DATA);
1521 mb = mreq;
1522 /*
1523 * If this is a big reply, use a cluster else
1524 * try and leave leading space for the lower level headers.
1525 */
1526 siz += RPC_REPLYSIZ;
1527 if (siz >= MINCLSIZE) {
1528 MCLGET(mreq, M_WAIT);
1529 } else
1530 mreq->m_data += max_hdr;
1531 tl = mtod(mreq, u_long *);
1532 mreq->m_len = 6 * NFSX_UNSIGNED;
1533 bpos = ((caddr_t)tl) + mreq->m_len;
1534 *tl++ = txdr_unsigned(nd->nd_retxid);
1535 *tl++ = rpc_reply;
1536 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1537 *tl++ = rpc_msgdenied;
1538 if (err & NFSERR_AUTHERR) {
1539 *tl++ = rpc_autherr;
1540 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1541 mreq->m_len -= NFSX_UNSIGNED;
1542 bpos -= NFSX_UNSIGNED;
1543 } else {
1544 *tl++ = rpc_mismatch;
1545 *tl++ = txdr_unsigned(RPC_VER2);
1546 *tl = txdr_unsigned(RPC_VER2);
1547 }
1548 } else {
1549 *tl++ = rpc_msgaccepted;
1550
1551 /*
1552 * For Kerberos authentication, we must send the nickname
1553 * verifier back, otherwise just RPCAUTH_NULL.
1554 */
1555 if (nd->nd_flag & ND_KERBFULL) {
1556 register struct nfsuid *nuidp;
1557 struct timeval ktvin, ktvout;
1558
1559 for (nuidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first;
1560 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1561 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
1562 (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp),
1563 &nuidp->nu_haddr, nd->nd_nam2)))
1564 break;
1565 }
1566 if (nuidp) {
1567 ktvin.tv_sec =
1568 txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1);
1569 ktvin.tv_usec =
1570 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1571
1572 /*
1573 * Encrypt the timestamp in ecb mode using the
1574 * session key.
1575 */
1576 #if NFSKERB
1577 XXX
1578 #endif
1579
1580 *tl++ = rpc_auth_kerb;
1581 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1582 *tl = ktvout.tv_sec;
1583 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED);
1584 *tl++ = ktvout.tv_usec;
1585 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
1586 } else {
1587 *tl++ = 0;
1588 *tl++ = 0;
1589 }
1590 } else {
1591 *tl++ = 0;
1592 *tl++ = 0;
1593 }
1594 switch (err) {
1595 case EPROGUNAVAIL:
1596 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1597 break;
1598 case EPROGMISMATCH:
1599 *tl = txdr_unsigned(RPC_PROGMISMATCH);
1600 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1601 if (nd->nd_flag & ND_NQNFS) {
1602 *tl++ = txdr_unsigned(3);
1603 *tl = txdr_unsigned(3);
1604 } else {
1605 *tl++ = txdr_unsigned(2);
1606 *tl = txdr_unsigned(3);
1607 }
1608 break;
1609 case EPROCUNAVAIL:
1610 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1611 break;
1612 case EBADRPC:
1613 *tl = txdr_unsigned(RPC_GARBAGE);
1614 break;
1615 default:
1616 *tl = 0;
1617 if (err != NFSERR_RETVOID) {
1618 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1619 if (err)
1620 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1621 else
1622 *tl = 0;
1623 }
1624 break;
1625 };
1626 }
1627
1628 /*
1629 * For nqnfs, piggyback lease as requested.
1630 */
1631 if ((nd->nd_flag & ND_NQNFS) && err == 0) {
1632 if (nd->nd_flag & ND_LEASE) {
1633 nfsm_build(tl, u_long *, 5 * NFSX_UNSIGNED);
1634 *tl++ = txdr_unsigned(nd->nd_flag & ND_LEASE);
1635 *tl++ = txdr_unsigned(cache);
1636 *tl++ = txdr_unsigned(nd->nd_duration);
1637 txdr_hyper(frev, tl);
1638 } else {
1639 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1640 *tl = 0;
1641 }
1642 }
1643 if (mrq != NULL)
1644 *mrq = mreq;
1645 *mbp = mb;
1646 *bposp = bpos;
1647 if (err != 0 && err != NFSERR_RETVOID)
1648 nfsstats.srvrpc_errs++;
1649 return (0);
1650 }
1651
1652
1653 #endif /* NFS_NOSERVER */
1654
1655
1656 /*
1657 * From FreeBSD 1.58, a Matt Dillon fix...
1658 * Flag a request as being about to terminate.
1659 * The nm_sent count is decremented now to avoid deadlocks when the process
1660 * in soreceive() hasn't yet managed to send its own request.
1661 */
1662 static void
1663 nfs_softterm(struct nfsreq *rep)
1664 {
1665 rep->r_flags |= R_SOFTTERM;
1666 if (rep->r_flags & R_SENT) {
1667 FSDBG(532, rep->r_xid, rep, rep->r_nmp->nm_sent,
1668 rep->r_nmp->nm_cwnd);
1669 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1670 rep->r_flags &= ~R_SENT;
1671 }
1672 }
1673
1674 void
1675 nfs_timer_funnel(arg)
1676 void * arg;
1677 {
1678 (void) thread_funnel_set(kernel_flock, TRUE);
1679 nfs_timer(arg);
1680 (void) thread_funnel_set(kernel_flock, FALSE);
1681
1682 }
1683
1684 /*
1685 * Nfs timer routine
1686 * Scan the nfsreq list and retranmit any requests that have timed out
1687 * To avoid retransmission attempts on STREAM sockets (in the future) make
1688 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1689 */
1690 void
1691 nfs_timer(arg)
1692 void *arg; /* never used */
1693 {
1694 register struct nfsreq *rep, *rp;
1695 register struct mbuf *m;
1696 register struct socket *so;
1697 register struct nfsmount *nmp;
1698 register int timeo;
1699 int s, error;
1700 #ifndef NFS_NOSERVER
1701 static long lasttime = 0;
1702 register struct nfssvc_sock *slp;
1703 u_quad_t cur_usec;
1704 #endif /* NFS_NOSERVER */
1705 #if NFSDIAG
1706 int rttdiag;
1707 #endif
1708 int flags, rexmit, cwnd, sent;
1709 u_long xid;
1710
1711 s = splnet();
1712 /*
1713 * XXX If preemptable threads are implemented the spls used for the
1714 * outstanding request queue must be replaced with mutexes.
1715 */
1716 rescan:
1717 #ifdef NFSTRACESUSPENDERS
1718 if (NFSTRACE_SUSPENDING) {
1719 for (rep = nfs_reqq.tqh_first; rep != 0;
1720 rep = rep->r_chain.tqe_next)
1721 if (rep->r_xid == nfstracexid)
1722 break;
1723 if (!rep) {
1724 NFSTRACE_RESUME;
1725 } else if (NFSTRACE_SUSPENSEOVER) {
1726 NFSTRACE_SUSPEND;
1727 }
1728 }
1729 #endif
1730 for (rep = nfs_reqq.tqh_first; rep != 0; rep = rep->r_chain.tqe_next) {
1731 #ifdef NFSTRACESUSPENDERS
1732 if (rep->r_mrep && !NFSTRACE_SUSPENDING) {
1733 nfstracexid = rep->r_xid;
1734 NFSTRACE_STARTSUSPENDCOUNTDOWN;
1735 }
1736 #endif
1737 nmp = rep->r_nmp;
1738 if (!nmp) /* unmounted */
1739 continue;
1740 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1741 continue;
1742 if (nfs_sigintr(nmp, rep, rep->r_procp)) {
1743 nfs_softterm(rep);
1744 continue;
1745 }
1746 if (rep->r_rtt >= 0) {
1747 rep->r_rtt++;
1748 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1749 timeo = nmp->nm_timeo;
1750 else
1751 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1752 /* ensure 62.5 ms floor */
1753 while (16 * timeo < hz)
1754 timeo *= 2;
1755 if (nmp->nm_timeouts > 0)
1756 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1757 if (rep->r_rtt <= timeo)
1758 continue;
1759 if (nmp->nm_timeouts < 8)
1760 nmp->nm_timeouts++;
1761 }
1762 /*
1763 * Check for server not responding
1764 */
1765 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1766 rep->r_rexmit > nmp->nm_deadthresh) {
1767 nfs_msg(rep->r_procp,
1768 nmp->nm_mountp->mnt_stat.f_mntfromname,
1769 "not responding");
1770 rep->r_flags |= R_TPRINTFMSG;
1771 }
1772 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1773 nfsstats.rpctimeouts++;
1774 nfs_softterm(rep);
1775 continue;
1776 }
1777 if (nmp->nm_sotype != SOCK_DGRAM) {
1778 if (++rep->r_rexmit > NFS_MAXREXMIT)
1779 rep->r_rexmit = NFS_MAXREXMIT;
1780 continue;
1781 }
1782 if ((so = nmp->nm_so) == NULL)
1783 continue;
1784
1785 /*
1786 * If there is enough space and the window allows..
1787 * Resend it
1788 * Set r_rtt to -1 in case we fail to send it now.
1789 */
1790 #if NFSDIAG
1791 rttdiag = rep->r_rtt;
1792 #endif
1793 rep->r_rtt = -1;
1794 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1795 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1796 (rep->r_flags & R_SENT) ||
1797 nmp->nm_sent < nmp->nm_cwnd) &&
1798 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1799
1800 struct proc *p = current_proc();
1801
1802 #if NFSDIAG
1803 if (rep->r_flags & R_SENT && nfsprnttimo &&
1804 nmp->nm_timeouts >= nfsprnttimo) {
1805 int t = proct[rep->r_procnum];
1806 if (t)
1807 NFS_DPF(DUP, ("nfs_timer %s nmtm=%d tms=%d rtt=%d tm=%d p=%d A=%d D=%d\n", nmp->nm_mountp->mnt_stat.f_mntfromname, nmp->nm_timeo, nmp->nm_timeouts, rttdiag, timeo, rep->r_procnum, nmp->nm_srtt[t-1], nmp->nm_sdrtt[t-1]));
1808 else
1809 NFS_DPF(DUP, ("nfs_timer %s nmtm=%d tms=%d rtt=%d tm=%d p=%d\n", nmp->nm_mountp->mnt_stat.f_mntfromname, nmp->nm_timeo, nmp->nm_timeouts, rttdiag, timeo, rep->r_procnum));
1810 }
1811 nfsdup(rep);
1812 #endif /* NFSDIAG */
1813 /*
1814 * Iff first send, start timing
1815 * else turn timing off, backoff timer
1816 * and divide congestion window by 2.
1817 * We update these *before* the send to avoid
1818 * racing against receiving the reply.
1819 * We save them so we can restore them on send error.
1820 */
1821 flags = rep->r_flags;
1822 rexmit = rep->r_rexmit;
1823 cwnd = nmp->nm_cwnd;
1824 sent = nmp->nm_sent;
1825 xid = rep->r_xid;
1826 if (rep->r_flags & R_SENT) {
1827 rep->r_flags &= ~R_TIMING;
1828 if (++rep->r_rexmit > NFS_MAXREXMIT)
1829 rep->r_rexmit = NFS_MAXREXMIT;
1830 nmp->nm_cwnd >>= 1;
1831 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1832 nmp->nm_cwnd = NFS_CWNDSCALE;
1833 nfsstats.rpcretries++;
1834 } else {
1835 rep->r_flags |= R_SENT;
1836 nmp->nm_sent += NFS_CWNDSCALE;
1837 }
1838 FSDBG(535, xid, rep, nmp->nm_sent, nmp->nm_cwnd);
1839
1840 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1841
1842 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1843 error = (*so->so_proto->pr_usrreqs->pru_send)
1844 (so, 0, m, 0, 0, p);
1845 else
1846 error = (*so->so_proto->pr_usrreqs->pru_send)
1847 (so, 0, m, mtod(nmp->nm_nam, struct sockaddr *), 0, p);
1848
1849 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1850
1851 FSDBG(535, xid, error, sent, cwnd);
1852 /*
1853 * This is to fix "nfs_sigintr" DSI panics.
1854 * We may have slept during the send so the current
1855 * place in the request queue may have been released.
1856 * Due to zone_gc it may even be part of an
1857 * unrelated newly allocated data structure.
1858 * Restart the list scan from the top if needed...
1859 */
1860 for (rp = nfs_reqq.tqh_first; rp;
1861 rp = rp->r_chain.tqe_next)
1862 if (rp == rep && rp->r_xid == xid)
1863 break;
1864 if (!rp) {
1865 if (!error)
1866 goto rescan;
1867 panic("nfs_timer: race error %d xid 0x%x\n",
1868 error, xid);
1869 }
1870
1871 if (error) {
1872 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1873 so->so_error = 0;
1874 rep->r_flags = flags;
1875 rep->r_rexmit = rexmit;
1876 nmp->nm_cwnd = cwnd;
1877 nmp->nm_sent = sent;
1878 if (flags & R_SENT)
1879 nfsstats.rpcretries--;
1880 } else
1881 rep->r_rtt = 0;
1882 }
1883 }
1884 #ifndef NFS_NOSERVER
1885 /*
1886 * Call the nqnfs server timer once a second to handle leases.
1887 */
1888 if (lasttime != time.tv_sec) {
1889 lasttime = time.tv_sec;
1890 nqnfs_serverd();
1891 }
1892
1893 /*
1894 * Scan the write gathering queues for writes that need to be
1895 * completed now.
1896 */
1897 cur_usec = (u_quad_t)time.tv_sec * 1000000 + (u_quad_t)time.tv_usec;
1898 for (slp = nfssvc_sockhead.tqh_first; slp != 0;
1899 slp = slp->ns_chain.tqe_next) {
1900 if (slp->ns_tq.lh_first && slp->ns_tq.lh_first->nd_time<=cur_usec)
1901 nfsrv_wakenfsd(slp);
1902 }
1903 #endif /* NFS_NOSERVER */
1904 splx(s);
1905 timeout(nfs_timer_funnel, (void *)0, nfs_ticks);
1906
1907 }
1908
1909
1910 /*
1911 * Test for a termination condition pending on the process.
1912 * This is used for NFSMNT_INT mounts.
1913 */
1914 int
1915 nfs_sigintr(nmp, rep, p)
1916 struct nfsmount *nmp;
1917 struct nfsreq *rep;
1918 register struct proc *p;
1919 {
1920
1921 if (rep && (rep->r_flags & R_SOFTTERM))
1922 return (EINTR);
1923 if (!(nmp->nm_flag & NFSMNT_INT))
1924 return (0);
1925 if (p && p->p_siglist &&
1926 (((p->p_siglist & ~p->p_sigmask) & ~p->p_sigignore) &
1927 NFSINT_SIGMASK))
1928 return (EINTR);
1929 return (0);
1930 }
1931
1932 /*
1933 * Lock a socket against others.
1934 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1935 * and also to avoid race conditions between the processes with nfs requests
1936 * in progress when a reconnect is necessary.
1937 */
1938 int
1939 nfs_sndlock(flagp, rep)
1940 register int *flagp;
1941 struct nfsreq *rep;
1942 {
1943 struct proc *p;
1944 int slpflag = 0, slptimeo = 0;
1945
1946 if (rep) {
1947 p = rep->r_procp;
1948 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1949 slpflag = PCATCH;
1950 } else
1951 p = (struct proc *)0;
1952 while (*flagp & NFSMNT_SNDLOCK) {
1953 if (nfs_sigintr(rep->r_nmp, rep, p))
1954 return (EINTR);
1955 *flagp |= NFSMNT_WANTSND;
1956 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck",
1957 slptimeo);
1958 if (slpflag == PCATCH) {
1959 slpflag = 0;
1960 slptimeo = 2 * hz;
1961 }
1962 /*
1963 * Make sure while we slept that the mountpoint didn't go away.
1964 * nfs_sigintr and callers expect it in tact.
1965 */
1966 if (!rep->r_nmp)
1967 return (ECONNABORTED); /* don't have lock until out of loop */
1968 }
1969 *flagp |= NFSMNT_SNDLOCK;
1970 return (0);
1971 }
1972
1973 /*
1974 * Unlock the stream socket for others.
1975 */
1976 void
1977 nfs_sndunlock(flagp)
1978 register int *flagp;
1979 {
1980
1981 if ((*flagp & NFSMNT_SNDLOCK) == 0)
1982 panic("nfs sndunlock");
1983 *flagp &= ~NFSMNT_SNDLOCK;
1984 if (*flagp & NFSMNT_WANTSND) {
1985 *flagp &= ~NFSMNT_WANTSND;
1986 wakeup((caddr_t)flagp);
1987 }
1988 }
1989
1990 static int
1991 nfs_rcvlock(rep)
1992 register struct nfsreq *rep;
1993 {
1994 register int *flagp = &rep->r_nmp->nm_flag;
1995 int slpflag, slptimeo = 0;
1996
1997 FSDBG_TOP(534, rep->r_xid, rep, rep->r_nmp, *flagp);
1998 if (*flagp & NFSMNT_INT)
1999 slpflag = PCATCH;
2000 else
2001 slpflag = 0;
2002 while (*flagp & NFSMNT_RCVLOCK) {
2003 if (nfs_sigintr(rep->r_nmp, rep, rep->r_procp)) {
2004 FSDBG_BOT(534, rep->r_xid, rep, rep->r_nmp, 0x100);
2005 return (EINTR);
2006 } else if (rep->r_mrep != NULL) {
2007 /*
2008 * Don't bother sleeping if reply already arrived
2009 */
2010 FSDBG_BOT(534, rep->r_xid, rep, rep->r_nmp, 0x101);
2011 return (EALREADY);
2012 }
2013 FSDBG(534, rep->r_xid, rep, rep->r_nmp, 0x102);
2014 *flagp |= NFSMNT_WANTRCV;
2015 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsrcvlk",
2016 slptimeo);
2017 if (slpflag == PCATCH) {
2018 slpflag = 0;
2019 slptimeo = 2 * hz;
2020 }
2021 /*
2022 * Make sure while we slept that the mountpoint didn't go away.
2023 * nfs_sigintr and caller nfs_reply expect it intact.
2024 */
2025 if (!rep->r_nmp) {
2026 FSDBG_BOT(534, rep->r_xid, rep, rep->r_nmp, 0x103);
2027 return (ECONNABORTED); /* don't have lock until out of loop */
2028 }
2029 }
2030 /*
2031 * nfs_reply will handle it if reply already arrived.
2032 * (We may have slept or been preempted while on network funnel).
2033 */
2034 FSDBG_BOT(534, rep->r_xid, rep, rep->r_nmp, *flagp);
2035 *flagp |= NFSMNT_RCVLOCK;
2036 return (0);
2037 }
2038
2039 /*
2040 * Unlock the stream socket for others.
2041 */
2042 static void
2043 nfs_rcvunlock(flagp)
2044 register int *flagp;
2045 {
2046
2047 FSDBG(533, flagp, *flagp, 0, 0);
2048 if ((*flagp & NFSMNT_RCVLOCK) == 0)
2049 panic("nfs rcvunlock");
2050 *flagp &= ~NFSMNT_RCVLOCK;
2051 if (*flagp & NFSMNT_WANTRCV) {
2052 *flagp &= ~NFSMNT_WANTRCV;
2053 wakeup((caddr_t)flagp);
2054 }
2055 }
2056
2057
2058 #ifndef NFS_NOSERVER
2059 /*
2060 * Socket upcall routine for the nfsd sockets.
2061 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2062 * Essentially do as much as possible non-blocking, else punt and it will
2063 * be called with M_WAIT from an nfsd.
2064 */
2065 /*
2066 * Needs to eun under network funnel
2067 */
2068 void
2069 nfsrv_rcv(so, arg, waitflag)
2070 struct socket *so;
2071 caddr_t arg;
2072 int waitflag;
2073 {
2074 register struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2075 register struct mbuf *m;
2076 struct mbuf *mp, *mhck;
2077 struct sockaddr *nam=0;
2078 struct uio auio;
2079 int flags, error;
2080 struct sockaddr_in *sin;
2081
2082 if ((slp->ns_flag & SLP_VALID) == 0)
2083 return;
2084 #ifdef notdef
2085 /*
2086 * Define this to test for nfsds handling this under heavy load.
2087 */
2088 if (waitflag == M_DONTWAIT) {
2089 slp->ns_flag |= SLP_NEEDQ; goto dorecs;
2090 }
2091 #endif
2092 auio.uio_procp = NULL;
2093 if (so->so_type == SOCK_STREAM) {
2094 /*
2095 * If there are already records on the queue, defer soreceive()
2096 * to an nfsd so that there is feedback to the TCP layer that
2097 * the nfs servers are heavily loaded.
2098 */
2099 if (slp->ns_rec && waitflag == M_DONTWAIT) {
2100 slp->ns_flag |= SLP_NEEDQ;
2101 goto dorecs;
2102 }
2103
2104 /*
2105 * Do soreceive().
2106 */
2107 auio.uio_resid = 1000000000;
2108 flags = MSG_DONTWAIT;
2109 error = soreceive(so, (struct sockaddr **) 0, &auio, &mp, (struct mbuf **)0, &flags);
2110 if (error || mp == (struct mbuf *)0) {
2111 if (error == EWOULDBLOCK)
2112 slp->ns_flag |= SLP_NEEDQ;
2113 else
2114 slp->ns_flag |= SLP_DISCONN;
2115 goto dorecs;
2116 }
2117 m = mp;
2118 if (slp->ns_rawend) {
2119 slp->ns_rawend->m_next = m;
2120 slp->ns_cc += 1000000000 - auio.uio_resid;
2121 } else {
2122 slp->ns_raw = m;
2123 slp->ns_cc = 1000000000 - auio.uio_resid;
2124 }
2125 while (m->m_next)
2126 m = m->m_next;
2127 slp->ns_rawend = m;
2128
2129 /*
2130 * Now try and parse record(s) out of the raw stream data.
2131 */
2132 error = nfsrv_getstream(slp, waitflag);
2133 if (error) {
2134 if (error == EPERM)
2135 slp->ns_flag |= SLP_DISCONN;
2136 else
2137 slp->ns_flag |= SLP_NEEDQ;
2138 }
2139 } else {
2140 do {
2141 auio.uio_resid = 1000000000;
2142 flags = MSG_DONTWAIT;
2143 nam = 0;
2144 error = soreceive(so, &nam, &auio, &mp,
2145 (struct mbuf **)0, &flags);
2146
2147 if (mp) {
2148 if (nam) {
2149 MGET(mhck, M_WAIT, MT_SONAME);
2150 mhck->m_len = nam->sa_len;
2151 sin = mtod(mhck, struct sockaddr_in *);
2152 bcopy(nam, sin, sizeof(struct sockaddr_in));
2153 mhck->m_hdr.mh_len = sizeof(struct sockaddr_in);
2154 FREE(nam, M_SONAME);
2155
2156 m = mhck;
2157 m->m_next = mp;
2158 } else
2159 m = mp;
2160 if (slp->ns_recend)
2161 slp->ns_recend->m_nextpkt = m;
2162 else
2163 slp->ns_rec = m;
2164 slp->ns_recend = m;
2165 m->m_nextpkt = (struct mbuf *)0;
2166 }
2167 if (error) {
2168 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
2169 && error != EWOULDBLOCK) {
2170 slp->ns_flag |= SLP_DISCONN;
2171 goto dorecs;
2172 }
2173 }
2174 } while (mp);
2175 }
2176
2177 /*
2178 * Now try and process the request records, non-blocking.
2179 */
2180 dorecs:
2181 if (waitflag == M_DONTWAIT &&
2182 (slp->ns_rec || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN)))) {
2183 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
2184 nfsrv_wakenfsd(slp);
2185 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
2186 }
2187 }
2188
2189 /*
2190 * Try and extract an RPC request from the mbuf data list received on a
2191 * stream socket. The "waitflag" argument indicates whether or not it
2192 * can sleep.
2193 */
2194 static int
2195 nfsrv_getstream(slp, waitflag)
2196 register struct nfssvc_sock *slp;
2197 int waitflag;
2198 {
2199 register struct mbuf *m, **mpp;
2200 register char *cp1, *cp2;
2201 register int len;
2202 struct mbuf *om, *m2, *recm = 0;
2203 u_long recmark;
2204
2205 if (slp->ns_flag & SLP_GETSTREAM)
2206 panic("nfs getstream");
2207 slp->ns_flag |= SLP_GETSTREAM;
2208 for (;;) {
2209 if (slp->ns_reclen == 0) {
2210 if (slp->ns_cc < NFSX_UNSIGNED) {
2211 slp->ns_flag &= ~SLP_GETSTREAM;
2212 return (0);
2213 }
2214 m = slp->ns_raw;
2215 if (m->m_len >= NFSX_UNSIGNED) {
2216 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
2217 m->m_data += NFSX_UNSIGNED;
2218 m->m_len -= NFSX_UNSIGNED;
2219 } else {
2220 cp1 = (caddr_t)&recmark;
2221 cp2 = mtod(m, caddr_t);
2222 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
2223 while (m->m_len == 0) {
2224 m = m->m_next;
2225 cp2 = mtod(m, caddr_t);
2226 }
2227 *cp1++ = *cp2++;
2228 m->m_data++;
2229 m->m_len--;
2230 }
2231 }
2232 slp->ns_cc -= NFSX_UNSIGNED;
2233 recmark = ntohl(recmark);
2234 slp->ns_reclen = recmark & ~0x80000000;
2235 if (recmark & 0x80000000)
2236 slp->ns_flag |= SLP_LASTFRAG;
2237 else
2238 slp->ns_flag &= ~SLP_LASTFRAG;
2239 if (slp->ns_reclen < NFS_MINPACKET || slp->ns_reclen > NFS_MAXPACKET) {
2240 slp->ns_flag &= ~SLP_GETSTREAM;
2241 return (EPERM);
2242 }
2243 }
2244
2245 /*
2246 * Now get the record part.
2247 */
2248 if (slp->ns_cc == slp->ns_reclen) {
2249 recm = slp->ns_raw;
2250 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
2251 slp->ns_cc = slp->ns_reclen = 0;
2252 } else if (slp->ns_cc > slp->ns_reclen) {
2253 len = 0;
2254 m = slp->ns_raw;
2255 om = (struct mbuf *)0;
2256 while (len < slp->ns_reclen) {
2257 if ((len + m->m_len) > slp->ns_reclen) {
2258 m2 = m_copym(m, 0, slp->ns_reclen - len,
2259 waitflag);
2260 if (m2) {
2261 if (om) {
2262 om->m_next = m2;
2263 recm = slp->ns_raw;
2264 } else
2265 recm = m2;
2266 m->m_data += slp->ns_reclen - len;
2267 m->m_len -= slp->ns_reclen - len;
2268 len = slp->ns_reclen;
2269 } else {
2270 slp->ns_flag &= ~SLP_GETSTREAM;
2271 return (EWOULDBLOCK);
2272 }
2273 } else if ((len + m->m_len) == slp->ns_reclen) {
2274 om = m;
2275 len += m->m_len;
2276 m = m->m_next;
2277 recm = slp->ns_raw;
2278 om->m_next = (struct mbuf *)0;
2279 } else {
2280 om = m;
2281 len += m->m_len;
2282 m = m->m_next;
2283 }
2284 }
2285 slp->ns_raw = m;
2286 slp->ns_cc -= len;
2287 slp->ns_reclen = 0;
2288 } else {
2289 slp->ns_flag &= ~SLP_GETSTREAM;
2290 return (0);
2291 }
2292
2293 /*
2294 * Accumulate the fragments into a record.
2295 */
2296 mpp = &slp->ns_frag;
2297 while (*mpp)
2298 mpp = &((*mpp)->m_next);
2299 *mpp = recm;
2300 if (slp->ns_flag & SLP_LASTFRAG) {
2301 if (slp->ns_recend)
2302 slp->ns_recend->m_nextpkt = slp->ns_frag;
2303 else
2304 slp->ns_rec = slp->ns_frag;
2305 slp->ns_recend = slp->ns_frag;
2306 slp->ns_frag = (struct mbuf *)0;
2307 }
2308 }
2309 }
2310
2311 /*
2312 * Parse an RPC header.
2313 */
2314 int
2315 nfsrv_dorec(slp, nfsd, ndp)
2316 register struct nfssvc_sock *slp;
2317 struct nfsd *nfsd;
2318 struct nfsrv_descript **ndp;
2319 {
2320 register struct mbuf *m;
2321 register struct mbuf *nam;
2322 register struct nfsrv_descript *nd;
2323 int error;
2324
2325 *ndp = NULL;
2326 if ((slp->ns_flag & SLP_VALID) == 0 ||
2327 (m = slp->ns_rec) == (struct mbuf *)0)
2328 return (ENOBUFS);
2329 slp->ns_rec = m->m_nextpkt;
2330 if (slp->ns_rec)
2331 m->m_nextpkt = (struct mbuf *)0;
2332 else
2333 slp->ns_recend = (struct mbuf *)0;
2334 if (m->m_type == MT_SONAME) {
2335 nam = m;
2336 m = m->m_next;
2337 nam->m_next = NULL;
2338 } else
2339 nam = NULL;
2340 MALLOC_ZONE(nd, struct nfsrv_descript *,
2341 sizeof (struct nfsrv_descript), M_NFSRVDESC, M_WAITOK);
2342 nd->nd_md = nd->nd_mrep = m;
2343 nd->nd_nam2 = nam;
2344 nd->nd_dpos = mtod(m, caddr_t);
2345 error = nfs_getreq(nd, nfsd, TRUE);
2346 if (error) {
2347 m_freem(nam);
2348 _FREE_ZONE((caddr_t)nd, sizeof *nd, M_NFSRVDESC);
2349 return (error);
2350 }
2351 *ndp = nd;
2352 nfsd->nfsd_nd = nd;
2353 return (0);
2354 }
2355
2356 /*
2357 * Parse an RPC request
2358 * - verify it
2359 * - fill in the cred struct.
2360 */
2361 int
2362 nfs_getreq(nd, nfsd, has_header)
2363 register struct nfsrv_descript *nd;
2364 struct nfsd *nfsd;
2365 int has_header;
2366 {
2367 register int len, i;
2368 register u_long *tl;
2369 register long t1;
2370 struct uio uio;
2371 struct iovec iov;
2372 caddr_t dpos, cp2, cp;
2373 u_long nfsvers, auth_type;
2374 uid_t nickuid;
2375 int error = 0, nqnfs = 0, ticklen;
2376 struct mbuf *mrep, *md;
2377 register struct nfsuid *nuidp;
2378 struct timeval tvin, tvout;
2379 #if 0 /* until encrypted keys are implemented */
2380 NFSKERBKEYSCHED_T keys; /* stores key schedule */
2381 #endif
2382
2383 mrep = nd->nd_mrep;
2384 md = nd->nd_md;
2385 dpos = nd->nd_dpos;
2386 if (has_header) {
2387 nfsm_dissect(tl, u_long *, 10 * NFSX_UNSIGNED);
2388 nd->nd_retxid = fxdr_unsigned(u_long, *tl++);
2389 if (*tl++ != rpc_call) {
2390 m_freem(mrep);
2391 return (EBADRPC);
2392 }
2393 } else
2394 nfsm_dissect(tl, u_long *, 8 * NFSX_UNSIGNED);
2395 nd->nd_repstat = 0;
2396 nd->nd_flag = 0;
2397 if (*tl++ != rpc_vers) {
2398 nd->nd_repstat = ERPCMISMATCH;
2399 nd->nd_procnum = NFSPROC_NOOP;
2400 return (0);
2401 }
2402 if (*tl != nfs_prog) {
2403 if (*tl == nqnfs_prog)
2404 nqnfs++;
2405 else {
2406 nd->nd_repstat = EPROGUNAVAIL;
2407 nd->nd_procnum = NFSPROC_NOOP;
2408 return (0);
2409 }
2410 }
2411 tl++;
2412 nfsvers = fxdr_unsigned(u_long, *tl++);
2413 if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) ||
2414 (nfsvers != NQNFS_VER3 && nqnfs)) {
2415 nd->nd_repstat = EPROGMISMATCH;
2416 nd->nd_procnum = NFSPROC_NOOP;
2417 return (0);
2418 }
2419 if (nqnfs)
2420 nd->nd_flag = (ND_NFSV3 | ND_NQNFS);
2421 else if (nfsvers == NFS_VER3)
2422 nd->nd_flag = ND_NFSV3;
2423 nd->nd_procnum = fxdr_unsigned(u_long, *tl++);
2424 if (nd->nd_procnum == NFSPROC_NULL)
2425 return (0);
2426 if (nd->nd_procnum >= NFS_NPROCS ||
2427 (!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
2428 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
2429 nd->nd_repstat = EPROCUNAVAIL;
2430 nd->nd_procnum = NFSPROC_NOOP;
2431 return (0);
2432 }
2433 if ((nd->nd_flag & ND_NFSV3) == 0)
2434 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
2435 auth_type = *tl++;
2436 len = fxdr_unsigned(int, *tl++);
2437 if (len < 0 || len > RPCAUTH_MAXSIZ) {
2438 m_freem(mrep);
2439 return (EBADRPC);
2440 }
2441
2442 nd->nd_flag &= ~ND_KERBAUTH;
2443 /*
2444 * Handle auth_unix or auth_kerb.
2445 */
2446 if (auth_type == rpc_auth_unix) {
2447 len = fxdr_unsigned(int, *++tl);
2448 if (len < 0 || len > NFS_MAXNAMLEN) {
2449 m_freem(mrep);
2450 return (EBADRPC);
2451 }
2452 nfsm_adv(nfsm_rndup(len));
2453 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
2454 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred));
2455 nd->nd_cr.cr_ref = 1;
2456 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
2457 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
2458 len = fxdr_unsigned(int, *tl);
2459 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
2460 m_freem(mrep);
2461 return (EBADRPC);
2462 }
2463 nfsm_dissect(tl, u_long *, (len + 2) * NFSX_UNSIGNED);
2464 for (i = 1; i <= len; i++)
2465 if (i < NGROUPS)
2466 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
2467 else
2468 tl++;
2469 nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1);
2470 if (nd->nd_cr.cr_ngroups > 1)
2471 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
2472 len = fxdr_unsigned(int, *++tl);
2473 if (len < 0 || len > RPCAUTH_MAXSIZ) {
2474 m_freem(mrep);
2475 return (EBADRPC);
2476 }
2477 if (len > 0)
2478 nfsm_adv(nfsm_rndup(len));
2479 } else if (auth_type == rpc_auth_kerb) {
2480 switch (fxdr_unsigned(int, *tl++)) {
2481 case RPCAKN_FULLNAME:
2482 ticklen = fxdr_unsigned(int, *tl);
2483 *((u_long *)nfsd->nfsd_authstr) = *tl;
2484 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
2485 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
2486 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
2487 m_freem(mrep);
2488 return (EBADRPC);
2489 }
2490 uio.uio_offset = 0;
2491 uio.uio_iov = &iov;
2492 uio.uio_iovcnt = 1;
2493 uio.uio_segflg = UIO_SYSSPACE;
2494 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
2495 iov.iov_len = RPCAUTH_MAXSIZ - 4;
2496 nfsm_mtouio(&uio, uio.uio_resid);
2497 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
2498 if (*tl++ != rpc_auth_kerb ||
2499 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
2500 printf("Bad kerb verifier\n");
2501 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2502 nd->nd_procnum = NFSPROC_NOOP;
2503 return (0);
2504 }
2505 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED);
2506 tl = (u_long *)cp;
2507 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
2508 printf("Not fullname kerb verifier\n");
2509 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2510 nd->nd_procnum = NFSPROC_NOOP;
2511 return (0);
2512 }
2513 cp += NFSX_UNSIGNED;
2514 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED);
2515 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
2516 nd->nd_flag |= ND_KERBFULL;
2517 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
2518 break;
2519 case RPCAKN_NICKNAME:
2520 if (len != 2 * NFSX_UNSIGNED) {
2521 printf("Kerb nickname short\n");
2522 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
2523 nd->nd_procnum = NFSPROC_NOOP;
2524 return (0);
2525 }
2526 nickuid = fxdr_unsigned(uid_t, *tl);
2527 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
2528 if (*tl++ != rpc_auth_kerb ||
2529 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
2530 printf("Kerb nick verifier bad\n");
2531 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2532 nd->nd_procnum = NFSPROC_NOOP;
2533 return (0);
2534 }
2535 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
2536 tvin.tv_sec = *tl++;
2537 tvin.tv_usec = *tl;
2538
2539 for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first;
2540 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
2541 if (nuidp->nu_cr.cr_uid == nickuid &&
2542 (!nd->nd_nam2 ||
2543 netaddr_match(NU_NETFAM(nuidp),
2544 &nuidp->nu_haddr, nd->nd_nam2)))
2545 break;
2546 }
2547 if (!nuidp) {
2548 nd->nd_repstat =
2549 (NFSERR_AUTHERR|AUTH_REJECTCRED);
2550 nd->nd_procnum = NFSPROC_NOOP;
2551 return (0);
2552 }
2553
2554 /*
2555 * Now, decrypt the timestamp using the session key
2556 * and validate it.
2557 */
2558 #if NFSKERB
2559 XXX
2560 #endif
2561
2562 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
2563 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
2564 if (nuidp->nu_expire < time.tv_sec ||
2565 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
2566 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
2567 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
2568 nuidp->nu_expire = 0;
2569 nd->nd_repstat =
2570 (NFSERR_AUTHERR|AUTH_REJECTVERF);
2571 nd->nd_procnum = NFSPROC_NOOP;
2572 return (0);
2573 }
2574 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
2575 nd->nd_flag |= ND_KERBNICK;
2576 };
2577 } else {
2578 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
2579 nd->nd_procnum = NFSPROC_NOOP;
2580 return (0);
2581 }
2582
2583 /*
2584 * For nqnfs, get piggybacked lease request.
2585 */
2586 if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) {
2587 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
2588 nd->nd_flag |= fxdr_unsigned(int, *tl);
2589 if (nd->nd_flag & ND_LEASE) {
2590 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
2591 nd->nd_duration = fxdr_unsigned(int, *tl);
2592 } else
2593 nd->nd_duration = NQ_MINLEASE;
2594 } else
2595 nd->nd_duration = NQ_MINLEASE;
2596 nd->nd_md = md;
2597 nd->nd_dpos = dpos;
2598 return (0);
2599 nfsmout:
2600 return (error);
2601 }
2602
2603 /*
2604 * Search for a sleeping nfsd and wake it up.
2605 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2606 * running nfsds will go look for the work in the nfssvc_sock list.
2607 */
2608 void
2609 nfsrv_wakenfsd(slp)
2610 struct nfssvc_sock *slp;
2611 {
2612 register struct nfsd *nd;
2613
2614 if ((slp->ns_flag & SLP_VALID) == 0)
2615 return;
2616 for (nd = nfsd_head.tqh_first; nd != 0; nd = nd->nfsd_chain.tqe_next) {
2617 if (nd->nfsd_flag & NFSD_WAITING) {
2618 nd->nfsd_flag &= ~NFSD_WAITING;
2619 if (nd->nfsd_slp)
2620 panic("nfsd wakeup");
2621 slp->ns_sref++;
2622 nd->nfsd_slp = slp;
2623 wakeup((caddr_t)nd);
2624 return;
2625 }
2626 }
2627 slp->ns_flag |= SLP_DOREC;
2628 nfsd_head_flag |= NFSD_CHECKSLP;
2629 }
2630 #endif /* NFS_NOSERVER */
2631
2632 static int
2633 nfs_msg(p, server, msg)
2634 struct proc *p;
2635 char *server, *msg;
2636 {
2637 tpr_t tpr;
2638
2639 if (p)
2640 tpr = tprintf_open(p);
2641 else
2642 tpr = NULL;
2643 tprintf(tpr, "nfs server %s: %s\n", server, msg);
2644 tprintf_close(tpr);
2645 return (0);
2646 }