nfs_syscalls.c revision 1.113 1 /* $NetBSD: nfs_syscalls.c,v 1.113 2007/06/01 14:43:17 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: nfs_syscalls.c,v 1.113 2007/06/01 14:43:17 yamt Exp $");
39
40 #include "fs_nfs.h"
41 #include "opt_nfs.h"
42 #include "opt_nfsserver.h"
43 #include "opt_iso.h"
44 #include "opt_inet.h"
45 #include "opt_compat_netbsd.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/file.h>
51 #include <sys/stat.h>
52 #include <sys/vnode.h>
53 #include <sys/mount.h>
54 #include <sys/proc.h>
55 #include <sys/uio.h>
56 #include <sys/malloc.h>
57 #include <sys/buf.h>
58 #include <sys/mbuf.h>
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
61 #include <sys/signalvar.h>
62 #include <sys/domain.h>
63 #include <sys/protosw.h>
64 #include <sys/namei.h>
65 #include <sys/syslog.h>
66 #include <sys/filedesc.h>
67 #include <sys/kthread.h>
68 #include <sys/kauth.h>
69 #include <sys/syscallargs.h>
70
71 #include <netinet/in.h>
72 #include <netinet/tcp.h>
73 #ifdef ISO
74 #include <netiso/iso.h>
75 #endif
76 #include <nfs/xdr_subs.h>
77 #include <nfs/rpcv2.h>
78 #include <nfs/nfsproto.h>
79 #include <nfs/nfs.h>
80 #include <nfs/nfsm_subs.h>
81 #include <nfs/nfsrvcache.h>
82 #include <nfs/nfsmount.h>
83 #include <nfs/nfsnode.h>
84 #include <nfs/nfsrtt.h>
85 #include <nfs/nfs_var.h>
86
87 /* Global defs. */
88 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
89 struct nfssvc_sock *,
90 struct lwp *, struct mbuf **));
91 extern int nfsrvw_procrastinate;
92
93 struct nfssvc_sock *nfs_udpsock;
94 #ifdef ISO
95 struct nfssvc_sock *nfs_cltpsock;
96 #endif
97 #ifdef INET6
98 struct nfssvc_sock *nfs_udp6sock;
99 #endif
100 int nuidhash_max = NFS_MAXUIDHASH;
101 #ifdef NFSSERVER
102 static int nfs_numnfsd = 0;
103 static struct nfsdrt nfsdrt;
104 #endif
105
106 #ifdef NFSSERVER
107 kmutex_t nfsd_lock;
108 struct nfssvc_sockhead nfssvc_sockhead;
109 kcondvar_t nfsd_initcv;
110 struct nfssvc_sockhead nfssvc_sockpending;
111 struct nfsdhead nfsd_head;
112 struct nfsdidlehead nfsd_idle_head;
113
114 int nfssvc_sockhead_flag;
115 int nfsd_head_flag;
116 #endif
117
118 MALLOC_DEFINE(M_NFSUID, "NFS uid", "Nfs uid mapping structure");
119
120 #ifdef NFS
121 struct nfs_iod nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
122 int nfs_niothreads = -1; /* == "0, and has never been set" */
123 #endif
124
125 #ifdef NFSSERVER
126 static struct nfssvc_sock *nfsrv_sockalloc __P((void));
127 static void nfsrv_sockfree __P((struct nfssvc_sock *));
128 static void nfsd_rt __P((int, struct nfsrv_descript *, int));
129 #endif
130
131 /*
132 * NFS server system calls
133 */
134
135
136 /*
137 * Nfs server pseudo system call for the nfsd's
138 * Based on the flag value it either:
139 * - adds a socket to the selection list
140 * - remains in the kernel as an nfsd
141 * - remains in the kernel as an nfsiod
142 */
143 int
144 sys_nfssvc(struct lwp *l, void *v, register_t *retval)
145 {
146 struct sys_nfssvc_args /* {
147 syscallarg(int) flag;
148 syscallarg(void *) argp;
149 } */ *uap = v;
150 int error;
151 #ifdef NFSSERVER
152 struct file *fp;
153 struct mbuf *nam;
154 struct nfsd_args nfsdarg;
155 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
156 struct nfsd *nfsd;
157 struct nfssvc_sock *slp;
158 struct nfsuid *nuidp;
159 #endif
160
161 /*
162 * Must be super user
163 */
164 error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
165 NULL);
166 if (error)
167 return (error);
168
169 /* Initialize NFS server / client shared data. */
170 nfs_init();
171
172 #ifdef NFSSERVER
173 mutex_enter(&nfsd_lock);
174 while (nfssvc_sockhead_flag & SLP_INIT) {
175 cv_wait(&nfsd_initcv, &nfsd_lock);
176 }
177 mutex_exit(&nfsd_lock);
178 #endif
179 if (SCARG(uap, flag) & NFSSVC_BIOD) {
180 #if defined(NFS) && defined(COMPAT_14)
181 error = nfssvc_iod(l);
182 #else
183 error = ENOSYS;
184 #endif
185 } else if (SCARG(uap, flag) & NFSSVC_MNTD) {
186 error = ENOSYS;
187 } else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) {
188 #ifndef NFSSERVER
189 error = ENOSYS;
190 #else
191 error = copyin(SCARG(uap, argp), (void *)&nfsdarg,
192 sizeof(nfsdarg));
193 if (error)
194 return (error);
195 /* getsock() will use the descriptor for us */
196 error = getsock(l->l_proc->p_fd, nfsdarg.sock, &fp);
197 if (error)
198 return (error);
199 /*
200 * Get the client address for connected sockets.
201 */
202 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
203 nam = (struct mbuf *)0;
204 else {
205 error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
206 MT_SONAME);
207 if (error) {
208 FILE_UNUSE(fp, NULL);
209 return (error);
210 }
211 }
212 error = nfssvc_addsock(fp, nam);
213 FILE_UNUSE(fp, NULL);
214 #endif /* !NFSSERVER */
215 } else if (SCARG(uap, flag) & NFSSVC_SETEXPORTSLIST) {
216 #ifndef NFSSERVER
217 error = ENOSYS;
218 #else
219 struct export_args *args;
220 struct mountd_exports_list mel;
221
222 error = copyin(SCARG(uap, argp), &mel, sizeof(mel));
223 if (error != 0)
224 return error;
225
226 args = (struct export_args *)malloc(mel.mel_nexports *
227 sizeof(struct export_args), M_TEMP, M_WAITOK);
228 error = copyin(mel.mel_exports, args, mel.mel_nexports *
229 sizeof(struct export_args));
230 if (error != 0) {
231 free(args, M_TEMP);
232 return error;
233 }
234 mel.mel_exports = args;
235
236 error = mountd_set_exports_list(&mel, l);
237
238 free(args, M_TEMP);
239 #endif /* !NFSSERVER */
240 } else {
241 #ifndef NFSSERVER
242 error = ENOSYS;
243 #else
244 error = copyin(SCARG(uap, argp), (void *)nsd, sizeof (*nsd));
245 if (error)
246 return (error);
247 if ((SCARG(uap, flag) & NFSSVC_AUTHIN) &&
248 ((nfsd = nsd->nsd_nfsd)) != NULL &&
249 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
250 slp = nfsd->nfsd_slp;
251
252 /*
253 * First check to see if another nfsd has already
254 * added this credential.
255 */
256 LIST_FOREACH(nuidp, NUIDHASH(slp, nsd->nsd_cr.cr_uid),
257 nu_hash) {
258 if (kauth_cred_geteuid(nuidp->nu_cr) ==
259 nsd->nsd_cr.cr_uid &&
260 (!nfsd->nfsd_nd->nd_nam2 ||
261 netaddr_match(NU_NETFAM(nuidp),
262 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
263 break;
264 }
265 if (nuidp) {
266 kauth_cred_hold(nuidp->nu_cr);
267 nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
268 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
269 } else {
270 /*
271 * Nope, so we will.
272 */
273 if (slp->ns_numuids < nuidhash_max) {
274 slp->ns_numuids++;
275 nuidp = (struct nfsuid *)
276 malloc(sizeof (struct nfsuid), M_NFSUID,
277 M_WAITOK);
278 } else
279 nuidp = (struct nfsuid *)0;
280 if ((slp->ns_flag & SLP_VALID) == 0) {
281 if (nuidp)
282 free((void *)nuidp, M_NFSUID);
283 } else {
284 if (nuidp == (struct nfsuid *)0) {
285 nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
286 LIST_REMOVE(nuidp, nu_hash);
287 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
288 nu_lru);
289 if (nuidp->nu_flag & NU_NAM)
290 m_freem(nuidp->nu_nam);
291 }
292 nuidp->nu_flag = 0;
293 kauth_uucred_to_cred(nuidp->nu_cr,
294 &nsd->nsd_cr);
295 nuidp->nu_timestamp = nsd->nsd_timestamp;
296 nuidp->nu_expire = time_second + nsd->nsd_ttl;
297 /*
298 * and save the session key in nu_key.
299 */
300 memcpy(nuidp->nu_key, nsd->nsd_key,
301 sizeof(nsd->nsd_key));
302 if (nfsd->nfsd_nd->nd_nam2) {
303 struct sockaddr_in *saddr;
304
305 saddr = mtod(nfsd->nfsd_nd->nd_nam2,
306 struct sockaddr_in *);
307 switch (saddr->sin_family) {
308 case AF_INET:
309 nuidp->nu_flag |= NU_INETADDR;
310 nuidp->nu_inetaddr =
311 saddr->sin_addr.s_addr;
312 break;
313 case AF_ISO:
314 default:
315 nuidp->nu_flag |= NU_NAM;
316 nuidp->nu_nam = m_copym(
317 nfsd->nfsd_nd->nd_nam2, 0,
318 M_COPYALL, M_WAIT);
319 break;
320 };
321 }
322 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
323 nu_lru);
324 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
325 nuidp, nu_hash);
326 kauth_cred_hold(nuidp->nu_cr);
327 nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
328 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
329 }
330 }
331 }
332 if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) &&
333 (nfsd = nsd->nsd_nfsd))
334 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
335 error = nfssvc_nfsd(nsd, SCARG(uap, argp), l);
336 #endif /* !NFSSERVER */
337 }
338 if (error == EINTR || error == ERESTART)
339 error = 0;
340 return (error);
341 }
342
343 #ifdef NFSSERVER
344 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
345 MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
346
347 static struct nfssvc_sock *
348 nfsrv_sockalloc()
349 {
350 struct nfssvc_sock *slp;
351
352 slp = (struct nfssvc_sock *)
353 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
354 memset(slp, 0, sizeof (struct nfssvc_sock));
355 mutex_init(&slp->ns_lock, MUTEX_DRIVER, IPL_SOFTNET);
356 cv_init(&slp->ns_cv, "nfsdsock");
357 TAILQ_INIT(&slp->ns_uidlruhead);
358 LIST_INIT(&slp->ns_tq);
359 SIMPLEQ_INIT(&slp->ns_sendq);
360 mutex_enter(&nfsd_lock);
361 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
362 mutex_exit(&nfsd_lock);
363
364 return slp;
365 }
366
367 static void
368 nfsrv_sockfree(struct nfssvc_sock *slp)
369 {
370
371 KASSERT(slp->ns_so == NULL);
372 KASSERT(slp->ns_fp == NULL);
373 KASSERT((slp->ns_flag & SLP_VALID) == 0);
374 mutex_destroy(&slp->ns_lock);
375 cv_destroy(&slp->ns_cv);
376 free(slp, M_NFSSVC);
377 }
378
379 /*
380 * Adds a socket to the list for servicing by nfsds.
381 */
382 int
383 nfssvc_addsock(fp, mynam)
384 struct file *fp;
385 struct mbuf *mynam;
386 {
387 struct mbuf *m;
388 int siz;
389 struct nfssvc_sock *slp;
390 struct socket *so;
391 struct nfssvc_sock *tslp;
392 int error, s;
393
394 so = (struct socket *)fp->f_data;
395 tslp = (struct nfssvc_sock *)0;
396 /*
397 * Add it to the list, as required.
398 */
399 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
400 #ifdef INET6
401 if (so->so_proto->pr_domain->dom_family == AF_INET6)
402 tslp = nfs_udp6sock;
403 else
404 #endif
405 tslp = nfs_udpsock;
406 if (tslp->ns_flag & SLP_VALID) {
407 m_freem(mynam);
408 return (EPERM);
409 }
410 #ifdef ISO
411 } else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
412 tslp = nfs_cltpsock;
413 if (tslp->ns_flag & SLP_VALID) {
414 m_freem(mynam);
415 return (EPERM);
416 }
417 #endif /* ISO */
418 }
419 if (so->so_type == SOCK_STREAM)
420 siz = NFS_MAXPACKET + sizeof (u_long);
421 else
422 siz = NFS_MAXPACKET;
423 error = soreserve(so, siz, siz);
424 if (error) {
425 m_freem(mynam);
426 return (error);
427 }
428
429 /*
430 * Set protocol specific options { for now TCP only } and
431 * reserve some space. For datagram sockets, this can get called
432 * repeatedly for the same socket, but that isn't harmful.
433 */
434 if (so->so_type == SOCK_STREAM) {
435 m = m_get(M_WAIT, MT_SOOPTS);
436 MCLAIM(m, &nfs_mowner);
437 *mtod(m, int32_t *) = 1;
438 m->m_len = sizeof(int32_t);
439 sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
440 }
441 if ((so->so_proto->pr_domain->dom_family == AF_INET
442 #ifdef INET6
443 || so->so_proto->pr_domain->dom_family == AF_INET6
444 #endif
445 ) &&
446 so->so_proto->pr_protocol == IPPROTO_TCP) {
447 m = m_get(M_WAIT, MT_SOOPTS);
448 MCLAIM(m, &nfs_mowner);
449 *mtod(m, int32_t *) = 1;
450 m->m_len = sizeof(int32_t);
451 sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
452 }
453 so->so_rcv.sb_flags &= ~SB_NOINTR;
454 so->so_rcv.sb_timeo = 0;
455 so->so_snd.sb_flags &= ~SB_NOINTR;
456 so->so_snd.sb_timeo = 0;
457 if (tslp) {
458 slp = tslp;
459 } else {
460 slp = nfsrv_sockalloc();
461 }
462 slp->ns_so = so;
463 slp->ns_nam = mynam;
464 fp->f_count++;
465 slp->ns_fp = fp;
466 s = splsoftnet();
467 so->so_upcallarg = (void *)slp;
468 so->so_upcall = nfsrv_rcv;
469 so->so_rcv.sb_flags |= SB_UPCALL;
470 slp->ns_flag = SLP_VALID | SLP_NEEDQ;
471 nfsrv_wakenfsd(slp);
472 splx(s);
473 return (0);
474 }
475
476 /*
477 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
478 * until it is killed by a signal.
479 */
480 int
481 nfssvc_nfsd(nsd, argp, l)
482 struct nfsd_srvargs *nsd;
483 void *argp;
484 struct lwp *l;
485 {
486 struct timeval tv;
487 struct mbuf *m;
488 struct nfssvc_sock *slp;
489 struct nfsd *nfsd = nsd->nsd_nfsd;
490 struct nfsrv_descript *nd = NULL;
491 struct mbuf *mreq;
492 u_quad_t cur_usec;
493 int error = 0, cacherep, siz, sotype, writes_todo;
494 struct proc *p = l->l_proc;
495 int s;
496
497 #ifndef nolint
498 cacherep = RC_DOIT;
499 writes_todo = 0;
500 #endif
501 if (nfsd == NULL) {
502 nsd->nsd_nfsd = nfsd =
503 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
504 memset(nfsd, 0, sizeof (struct nfsd));
505 cv_init(&nfsd->nfsd_cv, "nfsd");
506 nfsd->nfsd_procp = p;
507 mutex_enter(&nfsd_lock);
508 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
509 nfs_numnfsd++;
510 mutex_exit(&nfsd_lock);
511 }
512 PHOLD(l);
513 /*
514 * Loop getting rpc requests until SIGKILL.
515 */
516 for (;;) {
517 if ((curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
518 != 0) {
519 preempt();
520 }
521 if (nfsd->nfsd_slp == NULL) {
522 mutex_enter(&nfsd_lock);
523 while (nfsd->nfsd_slp == NULL &&
524 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
525 SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd,
526 nfsd_idle);
527 error = cv_wait_sig(&nfsd->nfsd_cv, &nfsd_lock);
528 if (error) {
529 slp = nfsd->nfsd_slp;
530 nfsd->nfsd_slp = NULL;
531 if (!slp)
532 SLIST_REMOVE(&nfsd_idle_head,
533 nfsd, nfsd, nfsd_idle);
534 mutex_exit(&nfsd_lock);
535 if (slp) {
536 nfsrv_wakenfsd(slp);
537 nfsrv_slpderef(slp);
538 }
539 goto done;
540 }
541 KASSERT(nfsd->nfsd_slp != NULL);
542 }
543 if (nfsd->nfsd_slp == NULL &&
544 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
545 slp = TAILQ_FIRST(&nfssvc_sockpending);
546 if (slp) {
547 KASSERT((slp->ns_flag &
548 (SLP_VALID | SLP_DOREC))
549 == (SLP_VALID | SLP_DOREC));
550 TAILQ_REMOVE(&nfssvc_sockpending, slp,
551 ns_pending);
552 slp->ns_flag &= ~SLP_DOREC;
553 slp->ns_sref++;
554 nfsd->nfsd_slp = slp;
555 } else
556 nfsd_head_flag &= ~NFSD_CHECKSLP;
557 }
558 mutex_exit(&nfsd_lock);
559 if ((slp = nfsd->nfsd_slp) == NULL)
560 continue;
561 if (slp->ns_flag & SLP_VALID) {
562 if (slp->ns_flag & SLP_DISCONN)
563 nfsrv_zapsock(slp);
564 else if ((slp->ns_flag & SLP_NEEDQ) != 0) {
565 nfsrv_rcv(slp->ns_so, (void *)slp,
566 M_WAIT);
567 }
568 error = nfsrv_dorec(slp, nfsd, &nd);
569 getmicrotime(&tv);
570 cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
571 (u_quad_t)tv.tv_usec;
572 if (error && LIST_FIRST(&slp->ns_tq) &&
573 LIST_FIRST(&slp->ns_tq)->nd_time <=
574 cur_usec) {
575 error = 0;
576 cacherep = RC_DOIT;
577 writes_todo = 1;
578 } else
579 writes_todo = 0;
580 if (error == 0 && slp->ns_rec != NULL) {
581 nfsrv_wakenfsd(slp);
582 }
583 }
584 } else {
585 error = 0;
586 slp = nfsd->nfsd_slp;
587 }
588 if (error || (slp->ns_flag & SLP_VALID) == 0) {
589 if (nd) {
590 nfsdreq_free(nd);
591 nd = NULL;
592 }
593 nfsd->nfsd_slp = NULL;
594 nfsrv_slpderef(slp);
595 continue;
596 }
597 sotype = slp->ns_so->so_type;
598 if (nd) {
599 getmicrotime(&nd->nd_starttime);
600 if (nd->nd_nam2)
601 nd->nd_nam = nd->nd_nam2;
602 else
603 nd->nd_nam = slp->ns_nam;
604
605 /*
606 * Check to see if authorization is needed.
607 */
608 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
609 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
610 nsd->nsd_haddr = mtod(nd->nd_nam,
611 struct sockaddr_in *)->sin_addr.s_addr;
612 nsd->nsd_authlen = nfsd->nfsd_authlen;
613 nsd->nsd_verflen = nfsd->nfsd_verflen;
614 if (!copyout(nfsd->nfsd_authstr,
615 nsd->nsd_authstr, nfsd->nfsd_authlen) &&
616 !copyout(nfsd->nfsd_verfstr,
617 nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
618 !copyout(nsd, argp, sizeof (*nsd))) {
619 PRELE(l);
620 return (ENEEDAUTH);
621 }
622 cacherep = RC_DROPIT;
623 } else
624 cacherep = nfsrv_getcache(nd, slp, &mreq);
625
626 if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
627 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
628 nd->nd_procnum = NFSPROC_NOOP;
629 nd->nd_repstat =
630 (NFSERR_AUTHERR | AUTH_TOOWEAK);
631 cacherep = RC_DOIT;
632 }
633 }
634
635 /*
636 * Loop to get all the write rpc relies that have been
637 * gathered together.
638 */
639 do {
640 #ifdef DIAGNOSTIC
641 int lockcount;
642 #endif
643 switch (cacherep) {
644 case RC_DOIT:
645 #ifdef DIAGNOSTIC
646 /*
647 * NFS server procs should neither release
648 * locks already held, nor leave things
649 * locked. Catch this sooner, rather than
650 * later (when we try to relock something we
651 * already have locked). Careful inspection
652 * of the failing routine usually turns up the
653 * lock leak.. once we know what it is..
654 */
655 lockcount = l->l_locks;
656 #endif
657 mreq = NULL;
658 netexport_rdlock();
659 if (writes_todo || nd == NULL ||
660 (!(nd->nd_flag & ND_NFSV3) &&
661 nd->nd_procnum == NFSPROC_WRITE &&
662 nfsrvw_procrastinate > 0))
663 error = nfsrv_writegather(&nd, slp,
664 l, &mreq);
665 else
666 error =
667 (*(nfsrv3_procs[nd->nd_procnum]))
668 (nd, slp, l, &mreq);
669 netexport_rdunlock();
670 #ifdef DIAGNOSTIC
671 if (l->l_locks != lockcount) {
672 /*
673 * If you see this panic, audit
674 * nfsrv3_procs[nd->nd_procnum] for
675 * vnode locking errors (usually, it's
676 * due to forgetting to vput()
677 * something).
678 */
679 #ifdef DEBUG
680 extern void printlockedvnodes(void);
681 printlockedvnodes();
682 #endif
683 printf("nfsd: locking botch in op %d"
684 " (before %d, after %d)\n",
685 nd ? nd->nd_procnum : -1,
686 lockcount, l->l_locks);
687 }
688 #endif
689 if (mreq == NULL) {
690 if (nd != NULL) {
691 if (nd->nd_nam2)
692 m_free(nd->nd_nam2);
693 if (nd->nd_mrep)
694 m_freem(nd->nd_mrep);
695 }
696 break;
697 }
698 if (error) {
699 nfsstats.srv_errs++;
700 nfsrv_updatecache(nd, false, mreq);
701 if (nd->nd_nam2)
702 m_freem(nd->nd_nam2);
703 break;
704 }
705 nfsstats.srvrpccnt[nd->nd_procnum]++;
706 nfsrv_updatecache(nd, true, mreq);
707 nd->nd_mrep = (struct mbuf *)0;
708 case RC_REPLY:
709 m = mreq;
710 siz = 0;
711 while (m) {
712 siz += m->m_len;
713 m = m->m_next;
714 }
715 if (siz <= 0 || siz > NFS_MAXPACKET) {
716 printf("mbuf siz=%d\n",siz);
717 panic("Bad nfs svc reply");
718 }
719 m = mreq;
720 m->m_pkthdr.len = siz;
721 m->m_pkthdr.rcvif = (struct ifnet *)0;
722 /*
723 * For stream protocols, prepend a Sun RPC
724 * Record Mark.
725 */
726 if (sotype == SOCK_STREAM) {
727 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
728 *mtod(m, u_int32_t *) =
729 htonl(0x80000000 | siz);
730 }
731 nd->nd_mreq = m;
732 if (nfsrtton) {
733 nfsd_rt(slp->ns_so->so_type, nd,
734 cacherep);
735 }
736 error = nfsdsock_sendreply(slp, nd);
737 nd = NULL;
738 if (error == EPIPE)
739 nfsrv_zapsock(slp);
740 if (error == EINTR || error == ERESTART) {
741 nfsrv_slpderef(slp);
742 goto done;
743 }
744 break;
745 case RC_DROPIT:
746 if (nfsrtton)
747 nfsd_rt(sotype, nd, cacherep);
748 m_freem(nd->nd_mrep);
749 m_freem(nd->nd_nam2);
750 break;
751 }
752 if (nd) {
753 nfsdreq_free(nd);
754 nd = NULL;
755 }
756
757 /*
758 * Check to see if there are outstanding writes that
759 * need to be serviced.
760 */
761 getmicrotime(&tv);
762 cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
763 (u_quad_t)tv.tv_usec;
764 s = splsoftclock();
765 if (LIST_FIRST(&slp->ns_tq) &&
766 LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
767 cacherep = RC_DOIT;
768 writes_todo = 1;
769 } else
770 writes_todo = 0;
771 splx(s);
772 } while (writes_todo);
773 if (nfsrv_dorec(slp, nfsd, &nd)) {
774 nfsd->nfsd_slp = NULL;
775 nfsrv_slpderef(slp);
776 }
777 }
778 done:
779 KASSERT(nfsd->nfsd_slp == NULL);
780 PRELE(l);
781 mutex_enter(&nfsd_lock);
782 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
783 mutex_exit(&nfsd_lock);
784 cv_destroy(&nfsd->nfsd_cv);
785 free(nfsd, M_NFSD);
786 nsd->nsd_nfsd = NULL;
787 if (--nfs_numnfsd == 0)
788 nfsrv_init(true); /* Reinitialize everything */
789 return (error);
790 }
791
792 /*
793 * Shut down a socket associated with an nfssvc_sock structure.
794 * Should be called with the send lock set, if required.
795 * The trick here is to increment the sref at the start, so that the nfsds
796 * will stop using it and clear ns_flag at the end so that it will not be
797 * reassigned during cleanup.
798 *
799 * called at splsoftnet.
800 */
801 void
802 nfsrv_zapsock(slp)
803 struct nfssvc_sock *slp;
804 {
805 struct nfsuid *nuidp, *nnuidp;
806 struct nfsrv_descript *nwp, *nnwp;
807 struct socket *so;
808 struct mbuf *m;
809 int s;
810
811 if (nfsdsock_drain(slp)) {
812 return;
813 }
814 mutex_enter(&nfsd_lock);
815 if (slp->ns_flag & SLP_DOREC) {
816 TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
817 }
818 mutex_exit(&nfsd_lock);
819
820 so = slp->ns_so;
821 KASSERT(so != NULL);
822 so->so_upcall = NULL;
823 so->so_upcallarg = NULL;
824 so->so_rcv.sb_flags &= ~SB_UPCALL;
825 soshutdown(so, SHUT_RDWR);
826
827 if (slp->ns_nam)
828 m_free(slp->ns_nam);
829 m_freem(slp->ns_raw);
830 m = slp->ns_rec;
831 while (m != NULL) {
832 struct mbuf *n;
833
834 n = m->m_nextpkt;
835 m_freem(m);
836 m = n;
837 }
838 for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
839 nuidp = nnuidp) {
840 nnuidp = TAILQ_NEXT(nuidp, nu_lru);
841 LIST_REMOVE(nuidp, nu_hash);
842 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
843 if (nuidp->nu_flag & NU_NAM)
844 m_freem(nuidp->nu_nam);
845 free((void *)nuidp, M_NFSUID);
846 }
847 s = splsoftclock();
848 for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
849 nnwp = LIST_NEXT(nwp, nd_tq);
850 LIST_REMOVE(nwp, nd_tq);
851 nfsdreq_free(nwp);
852 }
853 splx(s);
854 }
855
856 /*
857 * Derefence a server socket structure. If it has no more references and
858 * is no longer valid, you can throw it away.
859 */
860 void
861 nfsrv_slpderef(slp)
862 struct nfssvc_sock *slp;
863 {
864
865 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
866 struct file *fp;
867
868 mutex_enter(&nfsd_lock);
869 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
870 mutex_exit(&nfsd_lock);
871
872 fp = slp->ns_fp;
873 if (fp != NULL) {
874 slp->ns_fp = NULL;
875 KASSERT(fp != NULL);
876 KASSERT(fp->f_data == slp->ns_so);
877 simple_lock(&fp->f_slock);
878 FILE_USE(fp);
879 closef(fp, (struct lwp *)0);
880 slp->ns_so = NULL;
881 }
882
883 nfsrv_sockfree(slp);
884 }
885 }
886
887 /*
888 * Initialize the data structures for the server.
889 * Handshake with any new nfsds starting up to avoid any chance of
890 * corruption.
891 */
892 void
893 nfsrv_init(terminating)
894 int terminating;
895 {
896 struct nfssvc_sock *slp;
897
898 if (!terminating) {
899 mutex_init(&nfsd_lock, MUTEX_DRIVER, IPL_SOFTNET);
900 cv_init(&nfsd_initcv, "nfsdinit");
901 }
902
903 mutex_enter(&nfsd_lock);
904 if (nfssvc_sockhead_flag & SLP_INIT)
905 panic("nfsd init");
906 nfssvc_sockhead_flag |= SLP_INIT;
907
908 if (terminating) {
909 while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
910 mutex_exit(&nfsd_lock);
911 KASSERT(slp->ns_sref == 0);
912 slp->ns_sref++;
913 nfsrv_zapsock(slp);
914 nfsrv_slpderef(slp);
915 mutex_enter(&nfsd_lock);
916 }
917 mutex_exit(&nfsd_lock);
918 nfsrv_cleancache(); /* And clear out server cache */
919 } else {
920 mutex_exit(&nfsd_lock);
921 nfs_pub.np_valid = 0;
922 }
923
924 TAILQ_INIT(&nfssvc_sockhead);
925 TAILQ_INIT(&nfssvc_sockpending);
926
927 TAILQ_INIT(&nfsd_head);
928 SLIST_INIT(&nfsd_idle_head);
929 nfsd_head_flag &= ~NFSD_CHECKSLP;
930
931 nfs_udpsock = nfsrv_sockalloc();
932
933 #ifdef INET6
934 nfs_udp6sock = nfsrv_sockalloc();
935 #endif
936
937 #ifdef ISO
938 nfs_cltpsock = nfsrv_sockalloc();
939 #endif
940
941 mutex_enter(&nfsd_lock);
942 nfssvc_sockhead_flag &= ~SLP_INIT;
943 cv_broadcast(&nfsd_initcv);
944 mutex_exit(&nfsd_lock);
945 }
946
947 /*
948 * Add entries to the server monitor log.
949 */
950 static void
951 nfsd_rt(sotype, nd, cacherep)
952 int sotype;
953 struct nfsrv_descript *nd;
954 int cacherep;
955 {
956 struct timeval tv;
957 struct drt *rt;
958
959 rt = &nfsdrt.drt[nfsdrt.pos];
960 if (cacherep == RC_DOIT)
961 rt->flag = 0;
962 else if (cacherep == RC_REPLY)
963 rt->flag = DRT_CACHEREPLY;
964 else
965 rt->flag = DRT_CACHEDROP;
966 if (sotype == SOCK_STREAM)
967 rt->flag |= DRT_TCP;
968 if (nd->nd_flag & ND_NFSV3)
969 rt->flag |= DRT_NFSV3;
970 rt->proc = nd->nd_procnum;
971 if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
972 rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
973 else
974 rt->ipadr = INADDR_ANY;
975 getmicrotime(&tv);
976 rt->resptime = ((tv.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
977 (tv.tv_usec - nd->nd_starttime.tv_usec);
978 rt->tstamp = tv;
979 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
980 }
981 #endif /* NFSSERVER */
982
983 #ifdef NFS
984
985 int nfs_defect = 0;
986 /*
987 * Asynchronous I/O threads for client nfs.
988 * They do read-ahead and write-behind operations on the block I/O cache.
989 * Never returns unless it fails or gets killed.
990 */
991
992 int
993 nfssvc_iod(l)
994 struct lwp *l;
995 {
996 struct buf *bp;
997 int i;
998 struct nfs_iod *myiod;
999 struct nfsmount *nmp;
1000 int error = 0;
1001 struct proc *p = l->l_proc;
1002
1003 /*
1004 * Assign my position or return error if too many already running
1005 */
1006 myiod = NULL;
1007 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
1008 if (nfs_asyncdaemon[i].nid_proc == NULL) {
1009 myiod = &nfs_asyncdaemon[i];
1010 break;
1011 }
1012 if (myiod == NULL)
1013 return (EBUSY);
1014 myiod->nid_proc = p;
1015 nfs_numasync++;
1016 PHOLD(l);
1017 /*
1018 * Just loop around doing our stuff until SIGKILL
1019 */
1020 for (;;) {
1021 mutex_enter(&myiod->nid_lock);
1022 while (/*CONSTCOND*/ true) {
1023 nmp = myiod->nid_mount;
1024 if (nmp) {
1025 mutex_enter(&nmp->nm_lock);
1026 if (!TAILQ_EMPTY(&nmp->nm_bufq)) {
1027 mutex_exit(&myiod->nid_lock);
1028 break;
1029 }
1030 nmp->nm_bufqiods--;
1031 mutex_exit(&nmp->nm_lock);
1032 }
1033 myiod->nid_want = p;
1034 myiod->nid_mount = NULL;
1035 error = cv_wait_sig(&myiod->nid_cv, &myiod->nid_lock);
1036 if (error) {
1037 mutex_exit(&myiod->nid_lock);
1038 goto quit;
1039 }
1040 }
1041
1042 while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
1043 /* Take one off the front of the list */
1044 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
1045 nmp->nm_bufqlen--;
1046 if (nmp->nm_bufqlen < 2 * nfs_numasync) {
1047 cv_broadcast(&nmp->nm_aiocv);
1048 }
1049 mutex_exit(&nmp->nm_lock);
1050 (void)nfs_doio(bp);
1051 mutex_enter(&nmp->nm_lock);
1052 /*
1053 * If there are more than one iod on this mount,
1054 * then defect so that the iods can be shared out
1055 * fairly between the mounts
1056 */
1057 if (nfs_defect && nmp->nm_bufqiods > 1) {
1058 myiod->nid_mount = NULL;
1059 nmp->nm_bufqiods--;
1060 break;
1061 }
1062 }
1063 mutex_exit(&nmp->nm_lock);
1064 }
1065 quit:
1066 PRELE(l);
1067 if (nmp)
1068 nmp->nm_bufqiods--;
1069 myiod->nid_want = NULL;
1070 myiod->nid_mount = NULL;
1071 myiod->nid_proc = NULL;
1072 nfs_numasync--;
1073
1074 return error;
1075 }
1076
1077 void
1078 nfs_iodinit()
1079 {
1080 int i;
1081
1082 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1083 struct nfs_iod *nid = &nfs_asyncdaemon[i];
1084 mutex_init(&nid->nid_lock, MUTEX_DEFAULT, IPL_NONE);
1085 cv_init(&nid->nid_cv, "nfsiod");
1086 }
1087 }
1088
1089 void
1090 start_nfsio(void *arg)
1091 {
1092 nfssvc_iod(curlwp);
1093
1094 kthread_exit(0);
1095 }
1096
1097 void
1098 nfs_getset_niothreads(set)
1099 int set;
1100 {
1101 int i, have, start;
1102
1103 for (have = 0, i = 0; i < NFS_MAXASYNCDAEMON; i++)
1104 if (nfs_asyncdaemon[i].nid_proc != NULL)
1105 have++;
1106
1107 if (set) {
1108 /* clamp to sane range */
1109 nfs_niothreads = max(0, min(nfs_niothreads, NFS_MAXASYNCDAEMON));
1110
1111 start = nfs_niothreads - have;
1112
1113 while (start > 0) {
1114 kthread_create1(start_nfsio, NULL, NULL, "nfsio");
1115 start--;
1116 }
1117
1118 for (i = 0; (start < 0) && (i < NFS_MAXASYNCDAEMON); i++)
1119 if (nfs_asyncdaemon[i].nid_proc != NULL) {
1120 mutex_enter(&proclist_mutex);
1121 psignal(nfs_asyncdaemon[i].nid_proc, SIGKILL);
1122 mutex_exit(&proclist_mutex);
1123 start++;
1124 }
1125 } else {
1126 if (nfs_niothreads >= 0)
1127 nfs_niothreads = have;
1128 }
1129 }
1130
1131 /*
1132 * Get an authorization string for the uid by having the mount_nfs sitting
1133 * on this mount point porpous out of the kernel and do it.
1134 */
1135 int
1136 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
1137 struct nfsmount *nmp;
1138 struct nfsreq *rep;
1139 kauth_cred_t cred;
1140 char **auth_str;
1141 int *auth_len;
1142 char *verf_str;
1143 int *verf_len;
1144 NFSKERBKEY_T key; /* return session key */
1145 {
1146 int error = 0;
1147
1148 while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
1149 nmp->nm_iflag |= NFSMNT_WANTAUTH;
1150 (void) tsleep((void *)&nmp->nm_authtype, PSOCK,
1151 "nfsauth1", 2 * hz);
1152 error = nfs_sigintr(nmp, rep, rep->r_lwp);
1153 if (error) {
1154 nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
1155 return (error);
1156 }
1157 }
1158 nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
1159 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1160 nmp->nm_authlen = RPCAUTH_MAXSIZ;
1161 nmp->nm_verfstr = verf_str;
1162 nmp->nm_verflen = *verf_len;
1163 nmp->nm_authuid = kauth_cred_geteuid(cred);
1164 wakeup((void *)&nmp->nm_authstr);
1165
1166 /*
1167 * And wait for mount_nfs to do its stuff.
1168 */
1169 while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
1170 (void) tsleep((void *)&nmp->nm_authlen, PSOCK,
1171 "nfsauth2", 2 * hz);
1172 error = nfs_sigintr(nmp, rep, rep->r_lwp);
1173 }
1174 if (nmp->nm_iflag & NFSMNT_AUTHERR) {
1175 nmp->nm_iflag &= ~NFSMNT_AUTHERR;
1176 error = EAUTH;
1177 }
1178 if (error)
1179 free((void *)*auth_str, M_TEMP);
1180 else {
1181 *auth_len = nmp->nm_authlen;
1182 *verf_len = nmp->nm_verflen;
1183 memcpy(key, nmp->nm_key, sizeof (NFSKERBKEY_T));
1184 }
1185 nmp->nm_iflag &= ~NFSMNT_HASAUTH;
1186 nmp->nm_iflag |= NFSMNT_WAITAUTH;
1187 if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
1188 nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
1189 wakeup((void *)&nmp->nm_authtype);
1190 }
1191 return (error);
1192 }
1193
1194 /*
1195 * Get a nickname authenticator and verifier.
1196 */
1197 int
1198 nfs_getnickauth(struct nfsmount *nmp, kauth_cred_t cred, char **auth_str,
1199 int *auth_len, char *verf_str, int verf_len)
1200 {
1201 struct timeval ktvin, ktvout, tv;
1202 struct nfsuid *nuidp;
1203 u_int32_t *nickp, *verfp;
1204
1205 memset(&ktvout, 0, sizeof ktvout); /* XXX gcc */
1206
1207 #ifdef DIAGNOSTIC
1208 if (verf_len < (4 * NFSX_UNSIGNED))
1209 panic("nfs_getnickauth verf too small");
1210 #endif
1211 LIST_FOREACH(nuidp, NMUIDHASH(nmp, kauth_cred_geteuid(cred)), nu_hash) {
1212 if (kauth_cred_geteuid(nuidp->nu_cr) == kauth_cred_geteuid(cred))
1213 break;
1214 }
1215 if (!nuidp || nuidp->nu_expire < time_second)
1216 return (EACCES);
1217
1218 /*
1219 * Move to the end of the lru list (end of lru == most recently used).
1220 */
1221 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1222 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1223
1224 nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1225 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1226 *nickp = txdr_unsigned(nuidp->nu_nickname);
1227 *auth_str = (char *)nickp;
1228 *auth_len = 2 * NFSX_UNSIGNED;
1229
1230 /*
1231 * Now we must encrypt the verifier and package it up.
1232 */
1233 verfp = (u_int32_t *)verf_str;
1234 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1235 getmicrotime(&tv);
1236 if (tv.tv_sec > nuidp->nu_timestamp.tv_sec ||
1237 (tv.tv_sec == nuidp->nu_timestamp.tv_sec &&
1238 tv.tv_usec > nuidp->nu_timestamp.tv_usec))
1239 nuidp->nu_timestamp = tv;
1240 else
1241 nuidp->nu_timestamp.tv_usec++;
1242 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1243 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1244
1245 /*
1246 * Now encrypt the timestamp verifier in ecb mode using the session
1247 * key.
1248 */
1249 #ifdef NFSKERB
1250 XXX
1251 #endif
1252
1253 *verfp++ = ktvout.tv_sec;
1254 *verfp++ = ktvout.tv_usec;
1255 *verfp = 0;
1256 return (0);
1257 }
1258
1259 /*
1260 * Save the current nickname in a hash list entry on the mount point.
1261 */
1262 int
1263 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
1264 struct nfsmount *nmp;
1265 kauth_cred_t cred;
1266 int len;
1267 NFSKERBKEY_T key;
1268 struct mbuf **mdp;
1269 char **dposp;
1270 struct mbuf *mrep;
1271 {
1272 struct nfsuid *nuidp;
1273 u_int32_t *tl;
1274 int32_t t1;
1275 struct mbuf *md = *mdp;
1276 struct timeval ktvin, ktvout;
1277 u_int32_t nick;
1278 char *dpos = *dposp, *cp2;
1279 int deltasec, error = 0;
1280
1281 memset(&ktvout, 0, sizeof ktvout); /* XXX gcc */
1282
1283 if (len == (3 * NFSX_UNSIGNED)) {
1284 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1285 ktvin.tv_sec = *tl++;
1286 ktvin.tv_usec = *tl++;
1287 nick = fxdr_unsigned(u_int32_t, *tl);
1288
1289 /*
1290 * Decrypt the timestamp in ecb mode.
1291 */
1292 #ifdef NFSKERB
1293 XXX
1294 #endif
1295 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1296 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1297 deltasec = time_second - ktvout.tv_sec;
1298 if (deltasec < 0)
1299 deltasec = -deltasec;
1300 /*
1301 * If ok, add it to the hash list for the mount point.
1302 */
1303 if (deltasec <= NFS_KERBCLOCKSKEW) {
1304 if (nmp->nm_numuids < nuidhash_max) {
1305 nmp->nm_numuids++;
1306 nuidp = (struct nfsuid *)
1307 malloc(sizeof (struct nfsuid), M_NFSUID,
1308 M_WAITOK);
1309 } else {
1310 nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
1311 LIST_REMOVE(nuidp, nu_hash);
1312 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1313 nu_lru);
1314 }
1315 nuidp->nu_flag = 0;
1316 kauth_cred_seteuid(nuidp->nu_cr, kauth_cred_geteuid(cred));
1317 nuidp->nu_expire = time_second + NFS_KERBTTL;
1318 nuidp->nu_timestamp = ktvout;
1319 nuidp->nu_nickname = nick;
1320 memcpy(nuidp->nu_key, key, sizeof (NFSKERBKEY_T));
1321 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1322 nu_lru);
1323 LIST_INSERT_HEAD(NMUIDHASH(nmp, kauth_cred_geteuid(cred)),
1324 nuidp, nu_hash);
1325 }
1326 } else
1327 nfsm_adv(nfsm_rndup(len));
1328 nfsmout:
1329 *mdp = md;
1330 *dposp = dpos;
1331 return (error);
1332 }
1333 #endif /* NFS */
1334