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