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