]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_socket.c
xnu-201.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_socket.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1989, 1991, 1993, 1995
25 * The Regents of the University of California. All rights reserved.
26 *
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
59 * FreeBSD-Id: nfs_socket.c,v 1.30 1997/10/28 15:59:07 bde Exp $
60 */
61
62 /*
63 * Socket operations for use by nfs
64 */
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/proc.h>
69 #include <sys/mount.h>
70 #include <sys/kernel.h>
71 #include <sys/mbuf.h>
72 #include <sys/malloc.h>
73 #include <sys/vnode.h>
74 #include <sys/domain.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/syslog.h>
79 #include <sys/tprintf.h>
80 #include <machine/spl.h>
81
82 #include <sys/time.h>
83 #include <kern/clock.h>
84
85 #include <netinet/in.h>
86 #include <netinet/tcp.h>
87
88 #include <nfs/rpcv2.h>
89 #include <nfs/nfsproto.h>
90 #include <nfs/nfs.h>
91 #include <nfs/xdr_subs.h>
92 #include <nfs/nfsm_subs.h>
93 #include <nfs/nfsmount.h>
94 #include <nfs/nfsnode.h>
95 #include <nfs/nfsrtt.h>
96 #include <nfs/nqnfs.h>
97
98 #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 */
123 extern u_long rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers, rpc_auth_unix,
124 rpc_msgaccepted, rpc_call, rpc_autherr,
125 rpc_auth_kerb;
126 extern u_long nfs_prog, nqnfs_prog;
127 extern time_t nqnfsstarttime;
128 extern struct nfsstats nfsstats;
129 extern int nfsv3_procid[NFS_NPROCS];
130 extern 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 */
140 static 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)
160 static int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
161 int nfsrtton = 0;
162 struct nfsrtt nfsrtt;
163
164 static int nfs_msg __P((struct proc *,char *,char *));
165 static int nfs_rcvlock __P((struct nfsreq *));
166 static void nfs_rcvunlock __P((int *flagp));
167 static int nfs_receive __P((struct nfsreq *rep, struct mbuf **aname,
168 struct mbuf **mp));
169 static int nfs_reconnect __P((struct nfsreq *rep));
170 #ifndef NFS_NOSERVER
171 static int nfsrv_getstream __P((struct nfssvc_sock *,int));
172
173 int (*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
207 int nfstraceindx = 0;
208 struct nfstracerec nfstracebuf[NFSTBUFSIZ] = {{0,0,0,0}};
209
210 #define NFSTRACESUSPENDERS
211 #ifdef NFSTRACESUSPENDERS
212 uint nfstracemask = 0xfff00200;
213 int nfstracexid = -1;
214 uint onfstracemask = 0;
215 int 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
241 uint nfstracemask = 0;
242 #endif /* NFSTRACESUSPENDERS */
243
244 int nfsprnttimo = 1;
245
246 int nfsodata[1024];
247 int nfsoprocnum, nfsolen;
248 int nfsbt[32], nfsbtlen;
249
250 #if defined(__ppc__)
251 int
252 backtrace(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__)
269 int
270 backtrace()
271 {
272 return (0); /* Till someone implements a real routine */
273 }
274 #else
275 #error architecture not implemented.
276 #endif
277
278 void
279 nfsdup(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 */
329 int
330 nfs_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
478 bad:
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 */
493 static int
494 nfs_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 */
523 void
524 nfs_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 */
552 int
553 nfs_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 */
658 static int
659 nfs_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);
696 tryagain:
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 }
849 errout:
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 }
904 shutout:
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 */
918 int
919 nfs_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 * If we slept after putting bits otw, then reply may have
954 * arrived. In which case returning is required, or we
955 * would hang trying to nfs_receive an already received reply.
956 */
957 if (myrep->r_mrep != NULL) {
958 nfs_rcvunlock(&nmp->nm_flag);
959 NFSTRACE4(NFSTRC_RCVALREADY, myrep->r_xid, myrep,
960 myrep->r_nmp, 2);
961 return (0);
962 }
963 /*
964 * Get the next Rpc reply off the socket. Assume myrep->r_nmp
965 * is still in tact by checks done in nfs_rcvlock.
966 */
967 error = nfs_receive(myrep, &nam, &mrep);
968 /*
969 * Bailout asap if nfsmount struct gone (unmounted).
970 */
971 if (!myrep->r_nmp) {
972 NFSTRACE4(NFSTRC_ECONN, myrep->r_xid, myrep, nmp, 2);
973 return (ECONNABORTED);
974 }
975 if (error) {
976 NFSTRACE4(NFSTRC_RCVERR, myrep->r_xid, myrep, nmp,
977 error);
978 nfs_rcvunlock(&nmp->nm_flag);
979
980 /*
981 * Ignore routing errors on connectionless protocols??
982 */
983 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
984 nmp->nm_so->so_error = 0;
985 if (myrep->r_flags & R_GETONEREP)
986 return (0);
987 continue;
988 }
989 return (error);
990 }
991 if (nam)
992 m_freem(nam);
993
994 /*
995 * We assume all is fine, but if we did not have an error
996 * and mrep is 0, better not dereference it. nfs_receieve
997 * calls soreceive which carefully sets error=0 when it got
998 * errors on sbwait (tsleep). In most cases, I assume that's
999 * so we could go back again. In tcp case, EPIPE is returned.
1000 * In udp, case nfs_receive gets back here with no error and no
1001 * mrep. Is the right fix to have soreceive check for process
1002 * aborted after sbwait and return something non-zero? Should
1003 * nfs_receive give an EPIPE? Too risky to play with those
1004 * two this late in game for a shutdown problem. Instead,
1005 * just check here and get out. (ekn)
1006 */
1007 if (!mrep) {
1008 NFSTRACE4(NFSTRC_ECONN, myrep->r_xid, myrep, nmp, 3);
1009 return (ECONNABORTED); /* sounds good */
1010 }
1011
1012 /*
1013 * Get the xid and check that it is an rpc reply
1014 */
1015 md = mrep;
1016 dpos = mtod(md, caddr_t);
1017 nfsm_dissect(tl, u_long *, 2*NFSX_UNSIGNED);
1018 rxid = *tl++;
1019 if (*tl != rpc_reply) {
1020 #ifndef NFS_NOSERVER
1021 if (nmp->nm_flag & NFSMNT_NQNFS) {
1022 if (nqnfs_callback(nmp, mrep, md, dpos))
1023 nfsstats.rpcinvalid++;
1024 } else {
1025 nfsstats.rpcinvalid++;
1026 m_freem(mrep);
1027 }
1028 #else
1029 nfsstats.rpcinvalid++;
1030 m_freem(mrep);
1031 #endif
1032 nfsmout:
1033 if (nmp->nm_flag & NFSMNT_RCVLOCK)
1034 nfs_rcvunlock(&nmp->nm_flag);
1035 if (myrep->r_flags & R_GETONEREP)
1036 return (0); /* this path used by NQNFS */
1037 continue;
1038 }
1039
1040 /*
1041 * Loop through the request list to match up the reply
1042 * Iff no match, just drop the datagram
1043 */
1044 for (rep = nfs_reqq.tqh_first; rep != 0;
1045 rep = rep->r_chain.tqe_next) {
1046 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
1047 /* Found it.. */
1048 rep->r_mrep = mrep;
1049 rep->r_md = md;
1050 rep->r_dpos = dpos;
1051 if (nfsrtton) {
1052 struct rttl *rt;
1053
1054 rt = &nfsrtt.rttl[nfsrtt.pos];
1055 rt->proc = rep->r_procnum;
1056 rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
1057 rt->sent = nmp->nm_sent;
1058 rt->cwnd = nmp->nm_cwnd;
1059 if (proct[rep->r_procnum] == 0)
1060 panic("nfs_reply: proct[%d] is zero", rep->r_procnum);
1061 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
1062 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
1063 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid;
1064 rt->tstamp = time;
1065 if (rep->r_flags & R_TIMING)
1066 rt->rtt = rep->r_rtt;
1067 else
1068 rt->rtt = 1000000;
1069 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
1070 }
1071 /*
1072 * Update congestion window.
1073 * Do the additive increase of
1074 * one rpc/rtt.
1075 */
1076 NFSTRACE4(NFSTRC_CWND_REPLY, rep->r_xid, rep,
1077 nmp->nm_sent, nmp->nm_cwnd);
1078 if (nmp->nm_cwnd <= nmp->nm_sent) {
1079 nmp->nm_cwnd +=
1080 (NFS_CWNDSCALE * NFS_CWNDSCALE +
1081 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
1082 if (nmp->nm_cwnd > NFS_MAXCWND)
1083 nmp->nm_cwnd = NFS_MAXCWND;
1084 }
1085 if (!(rep->r_flags & R_SENT))
1086 printf("nfs_reply: unsent xid=%x",
1087 rep->r_xid);
1088 rep->r_flags &= ~R_SENT;
1089 nmp->nm_sent -= NFS_CWNDSCALE;
1090 /*
1091 * Update rtt using a gain of 0.125 on the mean
1092 * and a gain of 0.25 on the deviation.
1093 */
1094 if (rep->r_flags & R_TIMING) {
1095 /*
1096 * Since the timer resolution of
1097 * NFS_HZ is so course, it can often
1098 * result in r_rtt == 0. Since
1099 * r_rtt == N means that the actual
1100 * rtt is between N+dt and N+2-dt ticks,
1101 * add 1.
1102 */
1103 if (proct[rep->r_procnum] == 0)
1104 panic("nfs_reply: proct[%d] is zero", rep->r_procnum);
1105 t1 = rep->r_rtt + 1;
1106 t1 -= (NFS_SRTT(rep) >> 3);
1107 NFS_SRTT(rep) += t1;
1108 if (t1 < 0)
1109 t1 = -t1;
1110 t1 -= (NFS_SDRTT(rep) >> 2);
1111 NFS_SDRTT(rep) += t1;
1112 }
1113 nmp->nm_timeouts = 0;
1114 break;
1115 }
1116 }
1117 nfs_rcvunlock(&nmp->nm_flag);
1118 /*
1119 * If not matched to a request, drop it.
1120 * If it's mine, get out.
1121 */
1122 if (rep == 0) {
1123 nfsstats.rpcunexpected++;
1124 m_freem(mrep);
1125 } else if (rep == myrep) {
1126 if (rep->r_mrep == NULL)
1127 panic("nfs_reply: nil r_mrep");
1128 return (0);
1129 }
1130 NFSTRACE4(NFSTRC_NOTMINE, myrep->r_xid, myrep, rep,
1131 rep ? rep->r_xid : myrep->r_flags);
1132 if (myrep->r_flags & R_GETONEREP)
1133 return (0); /* this path used by NQNFS */
1134 }
1135 }
1136
1137 /*
1138 * nfs_request - goes something like this
1139 * - fill in request struct
1140 * - links it into list
1141 * - calls nfs_send() for first transmit
1142 * - calls nfs_receive() to get reply
1143 * - break down rpc header and return with nfs reply pointed to
1144 * by mrep or error
1145 * nb: always frees up mreq mbuf list
1146 */
1147 int
1148 nfs_request(vp, mrest, procnum, procp, cred, mrp, mdp, dposp)
1149 struct vnode *vp;
1150 struct mbuf *mrest;
1151 int procnum;
1152 struct proc *procp;
1153 struct ucred *cred;
1154 struct mbuf **mrp;
1155 struct mbuf **mdp;
1156 caddr_t *dposp;
1157 {
1158 register struct mbuf *m, *mrep;
1159 register struct nfsreq *rep, *rp;
1160 register u_long *tl;
1161 register int i;
1162 struct nfsmount *nmp;
1163 struct mbuf *md, *mheadend;
1164 struct nfsnode *np;
1165 char nickv[RPCX_NICKVERF];
1166 time_t reqtime, waituntil;
1167 caddr_t dpos, cp2;
1168 int t1, nqlflag, cachable, s, error = 0, mrest_len, auth_len, auth_type;
1169 int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0, failed_auth = 0;
1170 int verf_len, verf_type;
1171 u_long xid;
1172 u_quad_t frev;
1173 char *auth_str, *verf_str;
1174 NFSKERBKEY_T key; /* save session key */
1175
1176 nmp = VFSTONFS(vp->v_mount);
1177 MALLOC_ZONE(rep, struct nfsreq *,
1178 sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
1179 NFSTRACE4(NFSTRC_REQ, vp, procnum, nmp, rep);
1180
1181 /*
1182 * make sure if we blocked above, that the file system didn't get
1183 * unmounted leaving nmp bogus value to trip on later and crash.
1184 * Note nfs_unmount will set rep->r_nmp if unmounted volume, but we
1185 * aren't that far yet. SO this is best we can do. I wanted to check
1186 * for vp->v_mount = 0 also below, but that caused reboot crash.
1187 * Something must think it's okay for vp-v_mount=0 during booting.
1188 * Thus the best I can do here is see if we still have a vnode.
1189 */
1190
1191 if (vp->v_type == VBAD) {
1192 NFSTRACE4(NFSTRC_VBAD, 1, vp, nmp, rep);
1193 _FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1194 return (EINVAL);
1195 }
1196 rep->r_nmp = nmp;
1197 rep->r_vp = vp;
1198 rep->r_procp = procp;
1199 rep->r_procnum = procnum;
1200 i = 0;
1201 m = mrest;
1202 while (m) {
1203 i += m->m_len;
1204 m = m->m_next;
1205 }
1206 mrest_len = i;
1207
1208 /*
1209 * Get the RPC header with authorization.
1210 */
1211 kerbauth:
1212 verf_str = auth_str = (char *)0;
1213 if (nmp->nm_flag & NFSMNT_KERB) {
1214 verf_str = nickv;
1215 verf_len = sizeof (nickv);
1216 auth_type = RPCAUTH_KERB4;
1217 bzero((caddr_t)key, sizeof (key));
1218 if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
1219 &auth_len, verf_str, verf_len)) {
1220 error = nfs_getauth(nmp, rep, cred, &auth_str,
1221 &auth_len, verf_str, &verf_len, key);
1222 if (error) {
1223 _FREE_ZONE((caddr_t)rep,
1224 sizeof (struct nfsreq), M_NFSREQ);
1225 m_freem(mrest);
1226 return (error);
1227 }
1228 }
1229 } else {
1230 auth_type = RPCAUTH_UNIX;
1231 if (cred->cr_ngroups < 1)
1232 panic("nfsreq nogrps");
1233 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
1234 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
1235 5 * NFSX_UNSIGNED;
1236 }
1237 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
1238 auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
1239 if (auth_str)
1240 _FREE(auth_str, M_TEMP);
1241
1242 /*
1243 * For stream protocols, insert a Sun RPC Record Mark.
1244 */
1245 if (nmp->nm_sotype == SOCK_STREAM) {
1246 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
1247 *mtod(m, u_long *) = htonl(0x80000000 |
1248 (m->m_pkthdr.len - NFSX_UNSIGNED));
1249 }
1250 rep->r_mreq = m;
1251 rep->r_xid = xid;
1252 tryagain:
1253 if (nmp->nm_flag & NFSMNT_SOFT)
1254 rep->r_retry = nmp->nm_retry;
1255 else
1256 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1257 rep->r_rtt = rep->r_rexmit = 0;
1258 if (proct[procnum] > 0)
1259 rep->r_flags = R_TIMING;
1260 else
1261 rep->r_flags = 0;
1262 rep->r_mrep = NULL;
1263
1264 /*
1265 * Do the client side RPC.
1266 */
1267 nfsstats.rpcrequests++;
1268 /*
1269 * Chain request into list of outstanding requests. Be sure
1270 * to put it LAST so timer finds oldest requests first.
1271 */
1272 s = splsoftclock();
1273 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1274
1275 /* Get send time for nqnfs */
1276 reqtime = time.tv_sec;
1277
1278 /*
1279 * If backing off another request or avoiding congestion, don't
1280 * send this one now but let timer do it. If not timing a request,
1281 * do it now.
1282 */
1283 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
1284 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1285 nmp->nm_sent < nmp->nm_cwnd)) {
1286 splx(s);
1287 if (nmp->nm_soflags & PR_CONNREQUIRED)
1288 error = nfs_sndlock(&nmp->nm_flag, rep);
1289
1290 /*
1291 * Set the R_SENT before doing the send in case another thread
1292 * processes the reply before the nfs_send returns here
1293 */
1294 if (!error) {
1295 if ((rep->r_flags & R_MUSTRESEND) == 0) {
1296 NFSTRACE4(NFSTRC_CWND_REQ1, rep->r_xid, rep,
1297 nmp->nm_sent, nmp->nm_cwnd);
1298 nmp->nm_sent += NFS_CWNDSCALE;
1299 rep->r_flags |= R_SENT;
1300 }
1301
1302 m = m_copym(m, 0, M_COPYALL, M_WAIT);
1303 error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep);
1304 if (nmp->nm_soflags & PR_CONNREQUIRED)
1305 nfs_sndunlock(&nmp->nm_flag);
1306 }
1307 if (error) {
1308 nmp->nm_sent -= NFS_CWNDSCALE;
1309 rep->r_flags &= ~R_SENT;
1310 }
1311 } else {
1312 splx(s);
1313 rep->r_rtt = -1;
1314 }
1315
1316 /*
1317 * Wait for the reply from our send or the timer's.
1318 */
1319 if (!error || error == EPIPE)
1320 error = nfs_reply(rep);
1321
1322 /*
1323 * RPC done, unlink the request.
1324 */
1325 s = splsoftclock();
1326 for (rp = nfs_reqq.tqh_first; rp;
1327 rp = rp->r_chain.tqe_next)
1328 if (rp == rep && rp->r_xid == xid)
1329 break;
1330 if (!rp)
1331 panic("nfs_request race, rep %x xid %x", rep, xid);
1332 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1333 splx(s);
1334
1335 /*
1336 * Decrement the outstanding request count.
1337 */
1338 if (rep->r_flags & R_SENT) {
1339 NFSTRACE4(NFSTRC_CWND_REQ2, rep->r_xid, rep, nmp->nm_sent,
1340 nmp->nm_cwnd);
1341 rep->r_flags &= ~R_SENT; /* paranoia */
1342 nmp->nm_sent -= NFS_CWNDSCALE;
1343 }
1344
1345 /*
1346 * If there was a successful reply and a tprintf msg.
1347 * tprintf a response.
1348 */
1349 if (!error && (rep->r_flags & R_TPRINTFMSG))
1350 nfs_msg(rep->r_procp, nmp->nm_mountp->mnt_stat.f_mntfromname,
1351 "is alive again");
1352 mrep = rep->r_mrep;
1353 md = rep->r_md;
1354 dpos = rep->r_dpos;
1355 if (error) {
1356 m_freem(rep->r_mreq);
1357 NFSTRACE4(NFSTRC_REQERR, error, rep->r_xid, nmp, rep);
1358 _FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1359 return (error);
1360 }
1361
1362 /*
1363 * break down the rpc header and check if ok
1364 */
1365 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
1366 if (*tl++ == rpc_msgdenied) {
1367 if (*tl == rpc_mismatch)
1368 error = EOPNOTSUPP;
1369 else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
1370 if (!failed_auth) {
1371 failed_auth++;
1372 mheadend->m_next = (struct mbuf *)0;
1373 m_freem(mrep);
1374 m_freem(rep->r_mreq);
1375 goto kerbauth;
1376 } else
1377 error = EAUTH;
1378 } else
1379 error = EACCES;
1380 m_freem(mrep);
1381 m_freem(rep->r_mreq);
1382 NFSTRACE4(NFSTRC_RPCERR, error, rep->r_xid, nmp, rep);
1383 _FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1384 return (error);
1385 }
1386
1387 /*
1388 * Grab any Kerberos verifier, otherwise just throw it away.
1389 */
1390 verf_type = fxdr_unsigned(int, *tl++);
1391 i = fxdr_unsigned(int, *tl);
1392 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
1393 error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
1394 if (error)
1395 goto nfsmout;
1396 } else if (i > 0)
1397 nfsm_adv(nfsm_rndup(i));
1398 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1399 /* 0 == ok */
1400 if (*tl == 0) {
1401 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1402 if (*tl != 0) {
1403 error = fxdr_unsigned(int, *tl);
1404 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1405 error == NFSERR_TRYLATER) {
1406 m_freem(mrep);
1407 error = 0;
1408 waituntil = time.tv_sec + trylater_delay;
1409 NFS_DPF(DUP,
1410 ("nfs_request %s flag=%x trylater_cnt=%x waituntil=%lx trylater_delay=%x\n",
1411 nmp->nm_mountp->mnt_stat.f_mntfromname,
1412 nmp->nm_flag, trylater_cnt, waituntil,
1413 trylater_delay));
1414 while (time.tv_sec < waituntil)
1415 (void)tsleep((caddr_t)&lbolt,
1416 PSOCK, "nqnfstry", 0);
1417 trylater_delay *= nfs_backoff[trylater_cnt];
1418 if (trylater_cnt < 7)
1419 trylater_cnt++;
1420 goto tryagain;
1421 }
1422
1423 /*
1424 * If the File Handle was stale, invalidate the
1425 * lookup cache, just in case.
1426 */
1427 if (error == ESTALE)
1428 cache_purge(vp);
1429 if (nmp->nm_flag & NFSMNT_NFSV3) {
1430 *mrp = mrep;
1431 *mdp = md;
1432 *dposp = dpos;
1433 error |= NFSERR_RETERR;
1434 } else
1435 m_freem(mrep);
1436 m_freem(rep->r_mreq);
1437 NFSTRACE4(NFSTRC_DISSECTERR, error, rep->r_xid, nmp,
1438 rep);
1439 _FREE_ZONE((caddr_t)rep,
1440 sizeof (struct nfsreq), M_NFSREQ);
1441 return (error);
1442 }
1443
1444 /*
1445 * For nqnfs, get any lease in reply
1446 */
1447 if (nmp->nm_flag & NFSMNT_NQNFS) {
1448 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1449 if (*tl) {
1450 np = VTONFS(vp);
1451 nqlflag = fxdr_unsigned(int, *tl);
1452 nfsm_dissect(tl, u_long *, 4*NFSX_UNSIGNED);
1453 cachable = fxdr_unsigned(int, *tl++);
1454 reqtime += fxdr_unsigned(int, *tl++);
1455 if (reqtime > time.tv_sec) {
1456 fxdr_hyper(tl, &frev);
1457 nqnfs_clientlease(nmp, np, nqlflag,
1458 cachable, reqtime, frev);
1459 }
1460 }
1461 }
1462 *mrp = mrep;
1463 *mdp = md;
1464 *dposp = dpos;
1465 m_freem(rep->r_mreq);
1466 NFSTRACE4(NFSTRC_REQFREE, 0xf0f0f0f0, rep->r_xid, nmp, rep);
1467 FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1468 return (0);
1469 }
1470 m_freem(mrep);
1471 error = EPROTONOSUPPORT;
1472 nfsmout:
1473 m_freem(rep->r_mreq);
1474 NFSTRACE4(NFSTRC_REQFREE, error, rep->r_xid, nmp, rep);
1475 _FREE_ZONE((caddr_t)rep, sizeof (struct nfsreq), M_NFSREQ);
1476 return (error);
1477 }
1478
1479 #ifndef NFS_NOSERVER
1480 /*
1481 * Generate the rpc reply header
1482 * siz arg. is used to decide if adding a cluster is worthwhile
1483 */
1484 int
1485 nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp)
1486 int siz;
1487 struct nfsrv_descript *nd;
1488 struct nfssvc_sock *slp;
1489 int err;
1490 int cache;
1491 u_quad_t *frev;
1492 struct mbuf **mrq;
1493 struct mbuf **mbp;
1494 caddr_t *bposp;
1495 {
1496 register u_long *tl;
1497 register struct mbuf *mreq;
1498 caddr_t bpos;
1499 struct mbuf *mb, *mb2;
1500
1501 MGETHDR(mreq, M_WAIT, MT_DATA);
1502 mb = mreq;
1503 /*
1504 * If this is a big reply, use a cluster else
1505 * try and leave leading space for the lower level headers.
1506 */
1507 siz += RPC_REPLYSIZ;
1508 if (siz >= MINCLSIZE) {
1509 MCLGET(mreq, M_WAIT);
1510 } else
1511 mreq->m_data += max_hdr;
1512 tl = mtod(mreq, u_long *);
1513 mreq->m_len = 6 * NFSX_UNSIGNED;
1514 bpos = ((caddr_t)tl) + mreq->m_len;
1515 *tl++ = txdr_unsigned(nd->nd_retxid);
1516 *tl++ = rpc_reply;
1517 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1518 *tl++ = rpc_msgdenied;
1519 if (err & NFSERR_AUTHERR) {
1520 *tl++ = rpc_autherr;
1521 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1522 mreq->m_len -= NFSX_UNSIGNED;
1523 bpos -= NFSX_UNSIGNED;
1524 } else {
1525 *tl++ = rpc_mismatch;
1526 *tl++ = txdr_unsigned(RPC_VER2);
1527 *tl = txdr_unsigned(RPC_VER2);
1528 }
1529 } else {
1530 *tl++ = rpc_msgaccepted;
1531
1532 /*
1533 * For Kerberos authentication, we must send the nickname
1534 * verifier back, otherwise just RPCAUTH_NULL.
1535 */
1536 if (nd->nd_flag & ND_KERBFULL) {
1537 register struct nfsuid *nuidp;
1538 struct timeval ktvin, ktvout;
1539
1540 for (nuidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first;
1541 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1542 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
1543 (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp),
1544 &nuidp->nu_haddr, nd->nd_nam2)))
1545 break;
1546 }
1547 if (nuidp) {
1548 ktvin.tv_sec =
1549 txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1);
1550 ktvin.tv_usec =
1551 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1552
1553 /*
1554 * Encrypt the timestamp in ecb mode using the
1555 * session key.
1556 */
1557 #if NFSKERB
1558 XXX
1559 #endif
1560
1561 *tl++ = rpc_auth_kerb;
1562 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1563 *tl = ktvout.tv_sec;
1564 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED);
1565 *tl++ = ktvout.tv_usec;
1566 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
1567 } else {
1568 *tl++ = 0;
1569 *tl++ = 0;
1570 }
1571 } else {
1572 *tl++ = 0;
1573 *tl++ = 0;
1574 }
1575 switch (err) {
1576 case EPROGUNAVAIL:
1577 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1578 break;
1579 case EPROGMISMATCH:
1580 *tl = txdr_unsigned(RPC_PROGMISMATCH);
1581 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1582 if (nd->nd_flag & ND_NQNFS) {
1583 *tl++ = txdr_unsigned(3);
1584 *tl = txdr_unsigned(3);
1585 } else {
1586 *tl++ = txdr_unsigned(2);
1587 *tl = txdr_unsigned(3);
1588 }
1589 break;
1590 case EPROCUNAVAIL:
1591 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1592 break;
1593 case EBADRPC:
1594 *tl = txdr_unsigned(RPC_GARBAGE);
1595 break;
1596 default:
1597 *tl = 0;
1598 if (err != NFSERR_RETVOID) {
1599 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1600 if (err)
1601 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1602 else
1603 *tl = 0;
1604 }
1605 break;
1606 };
1607 }
1608
1609 /*
1610 * For nqnfs, piggyback lease as requested.
1611 */
1612 if ((nd->nd_flag & ND_NQNFS) && err == 0) {
1613 if (nd->nd_flag & ND_LEASE) {
1614 nfsm_build(tl, u_long *, 5 * NFSX_UNSIGNED);
1615 *tl++ = txdr_unsigned(nd->nd_flag & ND_LEASE);
1616 *tl++ = txdr_unsigned(cache);
1617 *tl++ = txdr_unsigned(nd->nd_duration);
1618 txdr_hyper(frev, tl);
1619 } else {
1620 nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1621 *tl = 0;
1622 }
1623 }
1624 if (mrq != NULL)
1625 *mrq = mreq;
1626 *mbp = mb;
1627 *bposp = bpos;
1628 if (err != 0 && err != NFSERR_RETVOID)
1629 nfsstats.srvrpc_errs++;
1630 return (0);
1631 }
1632
1633
1634 #endif /* NFS_NOSERVER */
1635
1636
1637 /*
1638 * From FreeBSD 1.58, a Matt Dillon fix...
1639 * Flag a request as being about to terminate.
1640 * The nm_sent count is decremented now to avoid deadlocks when the process
1641 * in soreceive() hasn't yet managed to send its own request.
1642 */
1643 static void
1644 nfs_softterm(struct nfsreq *rep)
1645 {
1646 rep->r_flags |= R_SOFTTERM;
1647 if (rep->r_flags & R_SENT) {
1648 NFSTRACE4(NFSTRC_CWND_SOFT, rep->r_xid, rep,
1649 rep->r_nmp->nm_sent, rep->r_nmp->nm_cwnd);
1650 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1651 rep->r_flags &= ~R_SENT;
1652 }
1653 }
1654
1655 void
1656 nfs_timer_funnel(arg)
1657 void * arg;
1658 {
1659 (void) thread_funnel_set(kernel_flock, TRUE);
1660 nfs_timer(arg);
1661 (void) thread_funnel_set(kernel_flock, FALSE);
1662
1663 }
1664
1665 /*
1666 * Nfs timer routine
1667 * Scan the nfsreq list and retranmit any requests that have timed out
1668 * To avoid retransmission attempts on STREAM sockets (in the future) make
1669 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1670 */
1671 void
1672 nfs_timer(arg)
1673 void *arg; /* never used */
1674 {
1675 register struct nfsreq *rep, *rp;
1676 register struct mbuf *m;
1677 register struct socket *so;
1678 register struct nfsmount *nmp;
1679 register int timeo;
1680 int s, error;
1681 #ifndef NFS_NOSERVER
1682 static long lasttime = 0;
1683 register struct nfssvc_sock *slp;
1684 u_quad_t cur_usec;
1685 #endif /* NFS_NOSERVER */
1686 #if NFSDIAG
1687 int rttdiag;
1688 #endif
1689 int flags, rexmit, cwnd, sent;
1690 u_long xid;
1691
1692 s = splnet();
1693 /*
1694 * XXX If preemptable threads are implemented the spls used for the
1695 * outstanding request queue must be replaced with mutexes.
1696 */
1697 rescan:
1698 #ifdef NFSTRACESUSPENDERS
1699 if (NFSTRACE_SUSPENDING) {
1700 for (rep = nfs_reqq.tqh_first; rep != 0;
1701 rep = rep->r_chain.tqe_next)
1702 if (rep->r_xid == nfstracexid)
1703 break;
1704 if (!rep) {
1705 NFSTRACE_RESUME;
1706 } else if (NFSTRACE_SUSPENSEOVER) {
1707 NFSTRACE_SUSPEND;
1708 }
1709 }
1710 #endif
1711 for (rep = nfs_reqq.tqh_first; rep != 0; rep = rep->r_chain.tqe_next) {
1712 #ifdef NFSTRACESUSPENDERS
1713 if (rep->r_mrep && !NFSTRACE_SUSPENDING) {
1714 nfstracexid = rep->r_xid;
1715 NFSTRACE_STARTSUSPENDCOUNTDOWN;
1716 }
1717 #endif
1718 nmp = rep->r_nmp;
1719 if (!nmp) /* unmounted */
1720 continue;
1721 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1722 continue;
1723 if (nfs_sigintr(nmp, rep, rep->r_procp)) {
1724 nfs_softterm(rep);
1725 continue;
1726 }
1727 if (rep->r_rtt >= 0) {
1728 rep->r_rtt++;
1729 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1730 timeo = nmp->nm_timeo;
1731 else
1732 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1733 /* ensure 62.5 ms floor */
1734 while (16 * timeo < hz)
1735 timeo *= 2;
1736 if (nmp->nm_timeouts > 0)
1737 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1738 if (rep->r_rtt <= timeo)
1739 continue;
1740 if (nmp->nm_timeouts < 8)
1741 nmp->nm_timeouts++;
1742 }
1743 /*
1744 * Check for server not responding
1745 */
1746 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1747 rep->r_rexmit > nmp->nm_deadthresh) {
1748 nfs_msg(rep->r_procp,
1749 nmp->nm_mountp->mnt_stat.f_mntfromname,
1750 "not responding");
1751 rep->r_flags |= R_TPRINTFMSG;
1752 }
1753 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1754 nfsstats.rpctimeouts++;
1755 nfs_softterm(rep);
1756 continue;
1757 }
1758 if (nmp->nm_sotype != SOCK_DGRAM) {
1759 if (++rep->r_rexmit > NFS_MAXREXMIT)
1760 rep->r_rexmit = NFS_MAXREXMIT;
1761 continue;
1762 }
1763 if ((so = nmp->nm_so) == NULL)
1764 continue;
1765
1766 /*
1767 * If there is enough space and the window allows..
1768 * Resend it
1769 * Set r_rtt to -1 in case we fail to send it now.
1770 */
1771 #if NFSDIAG
1772 rttdiag = rep->r_rtt;
1773 #endif
1774 rep->r_rtt = -1;
1775 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1776 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1777 (rep->r_flags & R_SENT) ||
1778 nmp->nm_sent < nmp->nm_cwnd) &&
1779 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1780
1781 struct proc *p = current_proc();
1782
1783 #if NFSDIAG
1784 if (rep->r_flags & R_SENT && nfsprnttimo &&
1785 nmp->nm_timeouts >= nfsprnttimo) {
1786 int t = proct[rep->r_procnum];
1787 if (t)
1788 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]));
1789 else
1790 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));
1791 }
1792 nfsdup(rep);
1793 #endif /* NFSDIAG */
1794 /*
1795 * Iff first send, start timing
1796 * else turn timing off, backoff timer
1797 * and divide congestion window by 2.
1798 * We update these *before* the send to avoid
1799 * racing against receiving the reply.
1800 * We save them so we can restore them on send error.
1801 */
1802 flags = rep->r_flags;
1803 rexmit = rep->r_rexmit;
1804 cwnd = nmp->nm_cwnd;
1805 sent = nmp->nm_sent;
1806 xid = rep->r_xid;
1807 if (rep->r_flags & R_SENT) {
1808 rep->r_flags &= ~R_TIMING;
1809 if (++rep->r_rexmit > NFS_MAXREXMIT)
1810 rep->r_rexmit = NFS_MAXREXMIT;
1811 nmp->nm_cwnd >>= 1;
1812 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1813 nmp->nm_cwnd = NFS_CWNDSCALE;
1814 nfsstats.rpcretries++;
1815 } else {
1816 rep->r_flags |= R_SENT;
1817 nmp->nm_sent += NFS_CWNDSCALE;
1818 }
1819 NFSTRACE4(NFSTRC_CWND_TIMER, xid, rep,
1820 nmp->nm_sent, nmp->nm_cwnd);
1821
1822 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1823
1824 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1825 error = (*so->so_proto->pr_usrreqs->pru_send)
1826 (so, 0, m, 0, 0, p);
1827 else
1828 error = (*so->so_proto->pr_usrreqs->pru_send)
1829 (so, 0, m, mtod(nmp->nm_nam, struct sockaddr *), 0, p);
1830
1831 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1832
1833 NFSTRACE4(NFSTRC_CWND_TIMER, xid, error, sent, cwnd);
1834 /*
1835 * This is to fix "nfs_sigintr" DSI panics.
1836 * We may have slept during the send so the current
1837 * place in the request queue may have been released.
1838 * Due to zone_gc it may even be part of an
1839 * unrelated newly allocated data structure.
1840 * Restart the list scan from the top if needed...
1841 */
1842 for (rp = nfs_reqq.tqh_first; rp;
1843 rp = rp->r_chain.tqe_next)
1844 if (rp == rep && rp->r_xid == xid)
1845 break;
1846 if (!rp) {
1847 if (!error)
1848 goto rescan;
1849 panic("nfs_timer: race error %d xid 0x%x\n",
1850 error, xid);
1851 }
1852
1853 if (error) {
1854 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1855 so->so_error = 0;
1856 rep->r_flags = flags;
1857 rep->r_rexmit = rexmit;
1858 nmp->nm_cwnd = cwnd;
1859 nmp->nm_sent = sent;
1860 if (flags & R_SENT)
1861 nfsstats.rpcretries--;
1862 } else
1863 rep->r_rtt = 0;
1864 }
1865 }
1866 #ifndef NFS_NOSERVER
1867 /*
1868 * Call the nqnfs server timer once a second to handle leases.
1869 */
1870 if (lasttime != time.tv_sec) {
1871 lasttime = time.tv_sec;
1872 nqnfs_serverd();
1873 }
1874
1875 /*
1876 * Scan the write gathering queues for writes that need to be
1877 * completed now.
1878 */
1879 cur_usec = (u_quad_t)time.tv_sec * 1000000 + (u_quad_t)time.tv_usec;
1880 for (slp = nfssvc_sockhead.tqh_first; slp != 0;
1881 slp = slp->ns_chain.tqe_next) {
1882 if (slp->ns_tq.lh_first && slp->ns_tq.lh_first->nd_time<=cur_usec)
1883 nfsrv_wakenfsd(slp);
1884 }
1885 #endif /* NFS_NOSERVER */
1886 splx(s);
1887 timeout(nfs_timer_funnel, (void *)0, nfs_ticks);
1888
1889 }
1890
1891
1892 /*
1893 * Test for a termination condition pending on the process.
1894 * This is used for NFSMNT_INT mounts.
1895 */
1896 int
1897 nfs_sigintr(nmp, rep, p)
1898 struct nfsmount *nmp;
1899 struct nfsreq *rep;
1900 register struct proc *p;
1901 {
1902
1903 if (rep && (rep->r_flags & R_SOFTTERM))
1904 return (EINTR);
1905 if (!(nmp->nm_flag & NFSMNT_INT))
1906 return (0);
1907 if (p && p->p_siglist &&
1908 (((p->p_siglist & ~p->p_sigmask) & ~p->p_sigignore) &
1909 NFSINT_SIGMASK))
1910 return (EINTR);
1911 return (0);
1912 }
1913
1914 /*
1915 * Lock a socket against others.
1916 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1917 * and also to avoid race conditions between the processes with nfs requests
1918 * in progress when a reconnect is necessary.
1919 */
1920 int
1921 nfs_sndlock(flagp, rep)
1922 register int *flagp;
1923 struct nfsreq *rep;
1924 {
1925 struct proc *p;
1926 int slpflag = 0, slptimeo = 0;
1927
1928 if (rep) {
1929 p = rep->r_procp;
1930 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1931 slpflag = PCATCH;
1932 } else
1933 p = (struct proc *)0;
1934 while (*flagp & NFSMNT_SNDLOCK) {
1935 if (nfs_sigintr(rep->r_nmp, rep, p))
1936 return (EINTR);
1937 *flagp |= NFSMNT_WANTSND;
1938 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck",
1939 slptimeo);
1940 if (slpflag == PCATCH) {
1941 slpflag = 0;
1942 slptimeo = 2 * hz;
1943 }
1944 /*
1945 * Make sure while we slept that the mountpoint didn't go away.
1946 * nfs_sigintr and callers expect it in tact.
1947 */
1948 if (!rep->r_nmp)
1949 return (ECONNABORTED); /* don't have lock until out of loop */
1950 }
1951 *flagp |= NFSMNT_SNDLOCK;
1952 return (0);
1953 }
1954
1955 /*
1956 * Unlock the stream socket for others.
1957 */
1958 void
1959 nfs_sndunlock(flagp)
1960 register int *flagp;
1961 {
1962
1963 if ((*flagp & NFSMNT_SNDLOCK) == 0)
1964 panic("nfs sndunlock");
1965 *flagp &= ~NFSMNT_SNDLOCK;
1966 if (*flagp & NFSMNT_WANTSND) {
1967 *flagp &= ~NFSMNT_WANTSND;
1968 wakeup((caddr_t)flagp);
1969 }
1970 }
1971
1972 static int
1973 nfs_rcvlock(rep)
1974 register struct nfsreq *rep;
1975 {
1976 register int *flagp = &rep->r_nmp->nm_flag;
1977 int slpflag, slptimeo = 0;
1978
1979 if (*flagp & NFSMNT_INT)
1980 slpflag = PCATCH;
1981 else
1982 slpflag = 0;
1983 while (*flagp & NFSMNT_RCVLOCK) {
1984 if (nfs_sigintr(rep->r_nmp, rep, rep->r_procp)) {
1985 NFSTRACE4(NFSTRC_RCVLCKINTR, rep->r_xid, rep,
1986 rep->r_nmp, *flagp);
1987 return (EINTR);
1988 } else if (rep->r_mrep != NULL) {
1989 /*
1990 * Don't bother sleeping if reply already arrived
1991 */
1992 NFSTRACE4(NFSTRC_RCVALREADY, rep->r_xid, rep,
1993 rep->r_nmp, 1);
1994 return (EALREADY);
1995 }
1996 NFSTRACE4(NFSTRC_RCVLCKW, rep->r_xid, rep, rep->r_nmp, *flagp);
1997 *flagp |= NFSMNT_WANTRCV;
1998 (void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsrcvlk",
1999 slptimeo);
2000 if (slpflag == PCATCH) {
2001 slpflag = 0;
2002 slptimeo = 2 * hz;
2003 }
2004 /*
2005 * Make sure while we slept that the mountpoint didn't go away.
2006 * nfs_sigintr and caller nfs_reply expect it in tact.
2007 */
2008 if (!rep->r_nmp)
2009 return (ECONNABORTED); /* don't have lock until out of loop */
2010 }
2011 /*
2012 * nfs_reply will handle it if reply already arrived.
2013 * (We may have slept or been preempted while on network funnel).
2014 */
2015 NFSTRACE4(NFSTRC_RCVLCK, rep->r_xid, rep, rep->r_nmp, *flagp);
2016 *flagp |= NFSMNT_RCVLOCK;
2017 return (0);
2018 }
2019
2020 /*
2021 * Unlock the stream socket for others.
2022 */
2023 static void
2024 nfs_rcvunlock(flagp)
2025 register int *flagp;
2026 {
2027
2028 if ((*flagp & NFSMNT_RCVLOCK) == 0)
2029 panic("nfs rcvunlock");
2030 *flagp &= ~NFSMNT_RCVLOCK;
2031 if (*flagp & NFSMNT_WANTRCV) {
2032 NFSTRACE(NFSTRC_RCVUNLW, flagp);
2033 *flagp &= ~NFSMNT_WANTRCV;
2034 wakeup((caddr_t)flagp);
2035 } else {
2036 NFSTRACE(NFSTRC_RCVUNL, flagp);
2037 }
2038 }
2039
2040
2041 #ifndef NFS_NOSERVER
2042 /*
2043 * Socket upcall routine for the nfsd sockets.
2044 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2045 * Essentially do as much as possible non-blocking, else punt and it will
2046 * be called with M_WAIT from an nfsd.
2047 */
2048 /*
2049 * Needs to eun under network funnel
2050 */
2051 void
2052 nfsrv_rcv(so, arg, waitflag)
2053 struct socket *so;
2054 caddr_t arg;
2055 int waitflag;
2056 {
2057 register struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2058 register struct mbuf *m;
2059 struct mbuf *mp, *mhck;
2060 struct sockaddr *nam=0;
2061 struct uio auio;
2062 int flags, error;
2063 struct sockaddr_in *sin;
2064
2065 if ((slp->ns_flag & SLP_VALID) == 0)
2066 return;
2067 #ifdef notdef
2068 /*
2069 * Define this to test for nfsds handling this under heavy load.
2070 */
2071 if (waitflag == M_DONTWAIT) {
2072 slp->ns_flag |= SLP_NEEDQ; goto dorecs;
2073 }
2074 #endif
2075 auio.uio_procp = NULL;
2076 if (so->so_type == SOCK_STREAM) {
2077 /*
2078 * If there are already records on the queue, defer soreceive()
2079 * to an nfsd so that there is feedback to the TCP layer that
2080 * the nfs servers are heavily loaded.
2081 */
2082 if (slp->ns_rec && waitflag == M_DONTWAIT) {
2083 slp->ns_flag |= SLP_NEEDQ;
2084 goto dorecs;
2085 }
2086
2087 /*
2088 * Do soreceive().
2089 */
2090 auio.uio_resid = 1000000000;
2091 flags = MSG_DONTWAIT;
2092 error = soreceive(so, (struct sockaddr **) 0, &auio, &mp, (struct mbuf **)0, &flags);
2093 if (error || mp == (struct mbuf *)0) {
2094 if (error == EWOULDBLOCK)
2095 slp->ns_flag |= SLP_NEEDQ;
2096 else
2097 slp->ns_flag |= SLP_DISCONN;
2098 goto dorecs;
2099 }
2100 m = mp;
2101 if (slp->ns_rawend) {
2102 slp->ns_rawend->m_next = m;
2103 slp->ns_cc += 1000000000 - auio.uio_resid;
2104 } else {
2105 slp->ns_raw = m;
2106 slp->ns_cc = 1000000000 - auio.uio_resid;
2107 }
2108 while (m->m_next)
2109 m = m->m_next;
2110 slp->ns_rawend = m;
2111
2112 /*
2113 * Now try and parse record(s) out of the raw stream data.
2114 */
2115 error = nfsrv_getstream(slp, waitflag);
2116 if (error) {
2117 if (error == EPERM)
2118 slp->ns_flag |= SLP_DISCONN;
2119 else
2120 slp->ns_flag |= SLP_NEEDQ;
2121 }
2122 } else {
2123 do {
2124 auio.uio_resid = 1000000000;
2125 flags = MSG_DONTWAIT;
2126 nam = 0;
2127 error = soreceive(so, &nam, &auio, &mp,
2128 (struct mbuf **)0, &flags);
2129
2130 if (mp) {
2131 if (nam) {
2132 MGET(mhck, M_WAIT, MT_SONAME);
2133 mhck->m_len = nam->sa_len;
2134 sin = mtod(mhck, struct sockaddr_in *);
2135 bcopy(nam, sin, sizeof(struct sockaddr_in));
2136 mhck->m_hdr.mh_len = sizeof(struct sockaddr_in);
2137 FREE(nam, M_SONAME);
2138
2139 m = mhck;
2140 m->m_next = mp;
2141 } else
2142 m = mp;
2143 if (slp->ns_recend)
2144 slp->ns_recend->m_nextpkt = m;
2145 else
2146 slp->ns_rec = m;
2147 slp->ns_recend = m;
2148 m->m_nextpkt = (struct mbuf *)0;
2149 }
2150 if (error) {
2151 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
2152 && error != EWOULDBLOCK) {
2153 slp->ns_flag |= SLP_DISCONN;
2154 goto dorecs;
2155 }
2156 }
2157 } while (mp);
2158 }
2159
2160 /*
2161 * Now try and process the request records, non-blocking.
2162 */
2163 dorecs:
2164 if (waitflag == M_DONTWAIT &&
2165 (slp->ns_rec || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN)))) {
2166 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
2167 nfsrv_wakenfsd(slp);
2168 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
2169 }
2170 }
2171
2172 /*
2173 * Try and extract an RPC request from the mbuf data list received on a
2174 * stream socket. The "waitflag" argument indicates whether or not it
2175 * can sleep.
2176 */
2177 static int
2178 nfsrv_getstream(slp, waitflag)
2179 register struct nfssvc_sock *slp;
2180 int waitflag;
2181 {
2182 register struct mbuf *m, **mpp;
2183 register char *cp1, *cp2;
2184 register int len;
2185 struct mbuf *om, *m2, *recm = 0;
2186 u_long recmark;
2187
2188 if (slp->ns_flag & SLP_GETSTREAM)
2189 panic("nfs getstream");
2190 slp->ns_flag |= SLP_GETSTREAM;
2191 for (;;) {
2192 if (slp->ns_reclen == 0) {
2193 if (slp->ns_cc < NFSX_UNSIGNED) {
2194 slp->ns_flag &= ~SLP_GETSTREAM;
2195 return (0);
2196 }
2197 m = slp->ns_raw;
2198 if (m->m_len >= NFSX_UNSIGNED) {
2199 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
2200 m->m_data += NFSX_UNSIGNED;
2201 m->m_len -= NFSX_UNSIGNED;
2202 } else {
2203 cp1 = (caddr_t)&recmark;
2204 cp2 = mtod(m, caddr_t);
2205 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
2206 while (m->m_len == 0) {
2207 m = m->m_next;
2208 cp2 = mtod(m, caddr_t);
2209 }
2210 *cp1++ = *cp2++;
2211 m->m_data++;
2212 m->m_len--;
2213 }
2214 }
2215 slp->ns_cc -= NFSX_UNSIGNED;
2216 recmark = ntohl(recmark);
2217 slp->ns_reclen = recmark & ~0x80000000;
2218 if (recmark & 0x80000000)
2219 slp->ns_flag |= SLP_LASTFRAG;
2220 else
2221 slp->ns_flag &= ~SLP_LASTFRAG;
2222 if (slp->ns_reclen < NFS_MINPACKET || slp->ns_reclen > NFS_MAXPACKET) {
2223 slp->ns_flag &= ~SLP_GETSTREAM;
2224 return (EPERM);
2225 }
2226 }
2227
2228 /*
2229 * Now get the record part.
2230 */
2231 if (slp->ns_cc == slp->ns_reclen) {
2232 recm = slp->ns_raw;
2233 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
2234 slp->ns_cc = slp->ns_reclen = 0;
2235 } else if (slp->ns_cc > slp->ns_reclen) {
2236 len = 0;
2237 m = slp->ns_raw;
2238 om = (struct mbuf *)0;
2239 while (len < slp->ns_reclen) {
2240 if ((len + m->m_len) > slp->ns_reclen) {
2241 m2 = m_copym(m, 0, slp->ns_reclen - len,
2242 waitflag);
2243 if (m2) {
2244 if (om) {
2245 om->m_next = m2;
2246 recm = slp->ns_raw;
2247 } else
2248 recm = m2;
2249 m->m_data += slp->ns_reclen - len;
2250 m->m_len -= slp->ns_reclen - len;
2251 len = slp->ns_reclen;
2252 } else {
2253 slp->ns_flag &= ~SLP_GETSTREAM;
2254 return (EWOULDBLOCK);
2255 }
2256 } else if ((len + m->m_len) == slp->ns_reclen) {
2257 om = m;
2258 len += m->m_len;
2259 m = m->m_next;
2260 recm = slp->ns_raw;
2261 om->m_next = (struct mbuf *)0;
2262 } else {
2263 om = m;
2264 len += m->m_len;
2265 m = m->m_next;
2266 }
2267 }
2268 slp->ns_raw = m;
2269 slp->ns_cc -= len;
2270 slp->ns_reclen = 0;
2271 } else {
2272 slp->ns_flag &= ~SLP_GETSTREAM;
2273 return (0);
2274 }
2275
2276 /*
2277 * Accumulate the fragments into a record.
2278 */
2279 mpp = &slp->ns_frag;
2280 while (*mpp)
2281 mpp = &((*mpp)->m_next);
2282 *mpp = recm;
2283 if (slp->ns_flag & SLP_LASTFRAG) {
2284 if (slp->ns_recend)
2285 slp->ns_recend->m_nextpkt = slp->ns_frag;
2286 else
2287 slp->ns_rec = slp->ns_frag;
2288 slp->ns_recend = slp->ns_frag;
2289 slp->ns_frag = (struct mbuf *)0;
2290 }
2291 }
2292 }
2293
2294 /*
2295 * Parse an RPC header.
2296 */
2297 int
2298 nfsrv_dorec(slp, nfsd, ndp)
2299 register struct nfssvc_sock *slp;
2300 struct nfsd *nfsd;
2301 struct nfsrv_descript **ndp;
2302 {
2303 register struct mbuf *m;
2304 register struct mbuf *nam;
2305 register struct nfsrv_descript *nd;
2306 int error;
2307
2308 *ndp = NULL;
2309 if ((slp->ns_flag & SLP_VALID) == 0 ||
2310 (m = slp->ns_rec) == (struct mbuf *)0)
2311 return (ENOBUFS);
2312 slp->ns_rec = m->m_nextpkt;
2313 if (slp->ns_rec)
2314 m->m_nextpkt = (struct mbuf *)0;
2315 else
2316 slp->ns_recend = (struct mbuf *)0;
2317 if (m->m_type == MT_SONAME) {
2318 nam = m;
2319 m = m->m_next;
2320 nam->m_next = NULL;
2321 } else
2322 nam = NULL;
2323 MALLOC_ZONE(nd, struct nfsrv_descript *,
2324 sizeof (struct nfsrv_descript), M_NFSRVDESC, M_WAITOK);
2325 nd->nd_md = nd->nd_mrep = m;
2326 nd->nd_nam2 = nam;
2327 nd->nd_dpos = mtod(m, caddr_t);
2328 error = nfs_getreq(nd, nfsd, TRUE);
2329 if (error) {
2330 m_freem(nam);
2331 _FREE_ZONE((caddr_t)nd, sizeof *nd, M_NFSRVDESC);
2332 return (error);
2333 }
2334 *ndp = nd;
2335 nfsd->nfsd_nd = nd;
2336 return (0);
2337 }
2338
2339 /*
2340 * Parse an RPC request
2341 * - verify it
2342 * - fill in the cred struct.
2343 */
2344 int
2345 nfs_getreq(nd, nfsd, has_header)
2346 register struct nfsrv_descript *nd;
2347 struct nfsd *nfsd;
2348 int has_header;
2349 {
2350 register int len, i;
2351 register u_long *tl;
2352 register long t1;
2353 struct uio uio;
2354 struct iovec iov;
2355 caddr_t dpos, cp2, cp;
2356 u_long nfsvers, auth_type;
2357 uid_t nickuid;
2358 int error = 0, nqnfs = 0, ticklen;
2359 struct mbuf *mrep, *md;
2360 register struct nfsuid *nuidp;
2361 struct timeval tvin, tvout;
2362 #if 0 /* until encrypted keys are implemented */
2363 NFSKERBKEYSCHED_T keys; /* stores key schedule */
2364 #endif
2365
2366 mrep = nd->nd_mrep;
2367 md = nd->nd_md;
2368 dpos = nd->nd_dpos;
2369 if (has_header) {
2370 nfsm_dissect(tl, u_long *, 10 * NFSX_UNSIGNED);
2371 nd->nd_retxid = fxdr_unsigned(u_long, *tl++);
2372 if (*tl++ != rpc_call) {
2373 m_freem(mrep);
2374 return (EBADRPC);
2375 }
2376 } else
2377 nfsm_dissect(tl, u_long *, 8 * NFSX_UNSIGNED);
2378 nd->nd_repstat = 0;
2379 nd->nd_flag = 0;
2380 if (*tl++ != rpc_vers) {
2381 nd->nd_repstat = ERPCMISMATCH;
2382 nd->nd_procnum = NFSPROC_NOOP;
2383 return (0);
2384 }
2385 if (*tl != nfs_prog) {
2386 if (*tl == nqnfs_prog)
2387 nqnfs++;
2388 else {
2389 nd->nd_repstat = EPROGUNAVAIL;
2390 nd->nd_procnum = NFSPROC_NOOP;
2391 return (0);
2392 }
2393 }
2394 tl++;
2395 nfsvers = fxdr_unsigned(u_long, *tl++);
2396 if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) ||
2397 (nfsvers != NQNFS_VER3 && nqnfs)) {
2398 nd->nd_repstat = EPROGMISMATCH;
2399 nd->nd_procnum = NFSPROC_NOOP;
2400 return (0);
2401 }
2402 if (nqnfs)
2403 nd->nd_flag = (ND_NFSV3 | ND_NQNFS);
2404 else if (nfsvers == NFS_VER3)
2405 nd->nd_flag = ND_NFSV3;
2406 nd->nd_procnum = fxdr_unsigned(u_long, *tl++);
2407 if (nd->nd_procnum == NFSPROC_NULL)
2408 return (0);
2409 if (nd->nd_procnum >= NFS_NPROCS ||
2410 (!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
2411 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
2412 nd->nd_repstat = EPROCUNAVAIL;
2413 nd->nd_procnum = NFSPROC_NOOP;
2414 return (0);
2415 }
2416 if ((nd->nd_flag & ND_NFSV3) == 0)
2417 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
2418 auth_type = *tl++;
2419 len = fxdr_unsigned(int, *tl++);
2420 if (len < 0 || len > RPCAUTH_MAXSIZ) {
2421 m_freem(mrep);
2422 return (EBADRPC);
2423 }
2424
2425 nd->nd_flag &= ~ND_KERBAUTH;
2426 /*
2427 * Handle auth_unix or auth_kerb.
2428 */
2429 if (auth_type == rpc_auth_unix) {
2430 len = fxdr_unsigned(int, *++tl);
2431 if (len < 0 || len > NFS_MAXNAMLEN) {
2432 m_freem(mrep);
2433 return (EBADRPC);
2434 }
2435 nfsm_adv(nfsm_rndup(len));
2436 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
2437 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred));
2438 nd->nd_cr.cr_ref = 1;
2439 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
2440 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
2441 len = fxdr_unsigned(int, *tl);
2442 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
2443 m_freem(mrep);
2444 return (EBADRPC);
2445 }
2446 nfsm_dissect(tl, u_long *, (len + 2) * NFSX_UNSIGNED);
2447 for (i = 1; i <= len; i++)
2448 if (i < NGROUPS)
2449 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
2450 else
2451 tl++;
2452 nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1);
2453 if (nd->nd_cr.cr_ngroups > 1)
2454 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
2455 len = fxdr_unsigned(int, *++tl);
2456 if (len < 0 || len > RPCAUTH_MAXSIZ) {
2457 m_freem(mrep);
2458 return (EBADRPC);
2459 }
2460 if (len > 0)
2461 nfsm_adv(nfsm_rndup(len));
2462 } else if (auth_type == rpc_auth_kerb) {
2463 switch (fxdr_unsigned(int, *tl++)) {
2464 case RPCAKN_FULLNAME:
2465 ticklen = fxdr_unsigned(int, *tl);
2466 *((u_long *)nfsd->nfsd_authstr) = *tl;
2467 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
2468 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
2469 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
2470 m_freem(mrep);
2471 return (EBADRPC);
2472 }
2473 uio.uio_offset = 0;
2474 uio.uio_iov = &iov;
2475 uio.uio_iovcnt = 1;
2476 uio.uio_segflg = UIO_SYSSPACE;
2477 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
2478 iov.iov_len = RPCAUTH_MAXSIZ - 4;
2479 nfsm_mtouio(&uio, uio.uio_resid);
2480 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
2481 if (*tl++ != rpc_auth_kerb ||
2482 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
2483 printf("Bad kerb verifier\n");
2484 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2485 nd->nd_procnum = NFSPROC_NOOP;
2486 return (0);
2487 }
2488 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED);
2489 tl = (u_long *)cp;
2490 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
2491 printf("Not fullname kerb verifier\n");
2492 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2493 nd->nd_procnum = NFSPROC_NOOP;
2494 return (0);
2495 }
2496 cp += NFSX_UNSIGNED;
2497 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED);
2498 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
2499 nd->nd_flag |= ND_KERBFULL;
2500 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
2501 break;
2502 case RPCAKN_NICKNAME:
2503 if (len != 2 * NFSX_UNSIGNED) {
2504 printf("Kerb nickname short\n");
2505 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
2506 nd->nd_procnum = NFSPROC_NOOP;
2507 return (0);
2508 }
2509 nickuid = fxdr_unsigned(uid_t, *tl);
2510 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
2511 if (*tl++ != rpc_auth_kerb ||
2512 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
2513 printf("Kerb nick verifier bad\n");
2514 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
2515 nd->nd_procnum = NFSPROC_NOOP;
2516 return (0);
2517 }
2518 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
2519 tvin.tv_sec = *tl++;
2520 tvin.tv_usec = *tl;
2521
2522 for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first;
2523 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
2524 if (nuidp->nu_cr.cr_uid == nickuid &&
2525 (!nd->nd_nam2 ||
2526 netaddr_match(NU_NETFAM(nuidp),
2527 &nuidp->nu_haddr, nd->nd_nam2)))
2528 break;
2529 }
2530 if (!nuidp) {
2531 nd->nd_repstat =
2532 (NFSERR_AUTHERR|AUTH_REJECTCRED);
2533 nd->nd_procnum = NFSPROC_NOOP;
2534 return (0);
2535 }
2536
2537 /*
2538 * Now, decrypt the timestamp using the session key
2539 * and validate it.
2540 */
2541 #if NFSKERB
2542 XXX
2543 #endif
2544
2545 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
2546 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
2547 if (nuidp->nu_expire < time.tv_sec ||
2548 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
2549 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
2550 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
2551 nuidp->nu_expire = 0;
2552 nd->nd_repstat =
2553 (NFSERR_AUTHERR|AUTH_REJECTVERF);
2554 nd->nd_procnum = NFSPROC_NOOP;
2555 return (0);
2556 }
2557 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
2558 nd->nd_flag |= ND_KERBNICK;
2559 };
2560 } else {
2561 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
2562 nd->nd_procnum = NFSPROC_NOOP;
2563 return (0);
2564 }
2565
2566 /*
2567 * For nqnfs, get piggybacked lease request.
2568 */
2569 if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) {
2570 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
2571 nd->nd_flag |= fxdr_unsigned(int, *tl);
2572 if (nd->nd_flag & ND_LEASE) {
2573 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
2574 nd->nd_duration = fxdr_unsigned(int, *tl);
2575 } else
2576 nd->nd_duration = NQ_MINLEASE;
2577 } else
2578 nd->nd_duration = NQ_MINLEASE;
2579 nd->nd_md = md;
2580 nd->nd_dpos = dpos;
2581 return (0);
2582 nfsmout:
2583 return (error);
2584 }
2585
2586 /*
2587 * Search for a sleeping nfsd and wake it up.
2588 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2589 * running nfsds will go look for the work in the nfssvc_sock list.
2590 */
2591 void
2592 nfsrv_wakenfsd(slp)
2593 struct nfssvc_sock *slp;
2594 {
2595 register struct nfsd *nd;
2596
2597 if ((slp->ns_flag & SLP_VALID) == 0)
2598 return;
2599 for (nd = nfsd_head.tqh_first; nd != 0; nd = nd->nfsd_chain.tqe_next) {
2600 if (nd->nfsd_flag & NFSD_WAITING) {
2601 nd->nfsd_flag &= ~NFSD_WAITING;
2602 if (nd->nfsd_slp)
2603 panic("nfsd wakeup");
2604 slp->ns_sref++;
2605 nd->nfsd_slp = slp;
2606 wakeup((caddr_t)nd);
2607 return;
2608 }
2609 }
2610 slp->ns_flag |= SLP_DOREC;
2611 nfsd_head_flag |= NFSD_CHECKSLP;
2612 }
2613 #endif /* NFS_NOSERVER */
2614
2615 static int
2616 nfs_msg(p, server, msg)
2617 struct proc *p;
2618 char *server, *msg;
2619 {
2620 tpr_t tpr;
2621
2622 if (p)
2623 tpr = tprintf_open(p);
2624 else
2625 tpr = NULL;
2626 tprintf(tpr, "nfs server %s: %s\n", server, msg);
2627 tprintf_close(tpr);
2628 return (0);
2629 }