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