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