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