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