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