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