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