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