1 /* $NetBSD: nfs_syscalls.c,v 1.25 1997/03/24 21:50:48 mycroft 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/file.h> 45 #include <sys/stat.h> 46 #include <sys/vnode.h> 47 #include <sys/mount.h> 48 #include <sys/proc.h> 49 #include <sys/uio.h> 50 #include <sys/malloc.h> 51 #include <sys/buf.h> 52 #include <sys/mbuf.h> 53 #include <sys/socket.h> 54 #include <sys/socketvar.h> 55 #include <sys/domain.h> 56 #include <sys/protosw.h> 57 #include <sys/namei.h> 58 #include <sys/syslog.h> 59 #include <sys/filedesc.h> 60 61 #include <sys/syscallargs.h> 62 63 #include <netinet/in.h> 64 #include <netinet/tcp.h> 65 #ifdef ISO 66 #include <netiso/iso.h> 67 #endif 68 #include <nfs/xdr_subs.h> 69 #include <nfs/rpcv2.h> 70 #include <nfs/nfsproto.h> 71 #include <nfs/nfs.h> 72 #include <nfs/nfsm_subs.h> 73 #include <nfs/nfsrvcache.h> 74 #include <nfs/nfsmount.h> 75 #include <nfs/nfsnode.h> 76 #include <nfs/nqnfs.h> 77 #include <nfs/nfsrtt.h> 78 #include <nfs/nfs_var.h> 79 80 void nfsrv_zapsock __P((struct nfssvc_sock *)); 81 82 /* Global defs. */ 83 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *, 84 struct nfssvc_sock *, 85 struct proc *, struct mbuf **)); 86 extern int nfs_numasync; 87 extern time_t nqnfsstarttime; 88 extern int nqsrv_writeslack; 89 extern int nfsrtton; 90 extern struct nfsstats nfsstats; 91 extern int nfsrvw_procrastinate; 92 struct nfssvc_sock *nfs_udpsock, *nfs_cltpsock; 93 int nuidhash_max = NFS_MAXUIDHASH; 94 int nfsd_waiting = 0; 95 #ifdef NFSSERVER 96 static int nfs_numnfsd = 0; 97 static int notstarted = 1; 98 static int modify_flag = 0; 99 static struct nfsdrt nfsdrt; 100 #endif 101 102 #define TRUE 1 103 #define FALSE 0 104 105 #ifdef NFS 106 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON]; 107 #endif 108 109 #ifdef NFSSERVER 110 static void nfsd_rt __P((int, struct nfsrv_descript *, int)); 111 #endif 112 113 /* 114 * NFS server system calls 115 * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c 116 */ 117 118 /* 119 * Get file handle system call 120 */ 121 int 122 sys_getfh(p, v, retval) 123 struct proc *p; 124 register void *v; 125 register_t *retval; 126 { 127 register struct sys_getfh_args /* { 128 syscallarg(char *) fname; 129 syscallarg(fhandle_t *) fhp; 130 } */ *uap = v; 131 register struct vnode *vp; 132 fhandle_t fh; 133 int error; 134 struct nameidata nd; 135 136 /* 137 * Must be super user 138 */ 139 error = suser(p->p_ucred, &p->p_acflag); 140 if (error) 141 return (error); 142 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 143 SCARG(uap, fname), p); 144 error = namei(&nd); 145 if (error) 146 return (error); 147 vp = nd.ni_vp; 148 bzero((caddr_t)&fh, sizeof(fh)); 149 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid; 150 error = VFS_VPTOFH(vp, &fh.fh_fid); 151 vput(vp); 152 if (error) 153 return (error); 154 error = copyout((caddr_t)&fh, (caddr_t)SCARG(uap, fhp), sizeof (fh)); 155 return (error); 156 } 157 158 /* 159 * Nfs server pseudo system call for the nfsd's 160 * Based on the flag value it either: 161 * - adds a socket to the selection list 162 * - remains in the kernel as an nfsd 163 * - remains in the kernel as an nfsiod 164 */ 165 int 166 sys_nfssvc(p, v, retval) 167 struct proc *p; 168 void *v; 169 register_t *retval; 170 { 171 register struct sys_nfssvc_args /* { 172 syscallarg(int) flag; 173 syscallarg(caddr_t) argp; 174 } */ *uap = v; 175 int error; 176 #ifdef NFS 177 struct nameidata nd; 178 struct nfsmount *nmp; 179 struct nfsd_cargs ncd; 180 #endif 181 #ifdef NFSSERVER 182 struct file *fp; 183 struct mbuf *nam; 184 struct nfsd_args nfsdarg; 185 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs; 186 struct nfsd *nfsd; 187 struct nfssvc_sock *slp; 188 struct nfsuid *nuidp; 189 #endif 190 191 /* 192 * Must be super user 193 */ 194 error = suser(p->p_ucred, &p->p_acflag); 195 if(error) 196 return (error); 197 while (nfssvc_sockhead_flag & SLP_INIT) { 198 nfssvc_sockhead_flag |= SLP_WANTINIT; 199 (void) tsleep((caddr_t)&nfssvc_sockhead, PSOCK, "nfsd init", 0); 200 } 201 if (SCARG(uap, flag) & NFSSVC_BIOD) { 202 #ifdef NFS 203 error = nfssvc_iod(p); 204 #else 205 error = ENOSYS; 206 #endif 207 } else if (SCARG(uap, flag) & NFSSVC_MNTD) { 208 #ifndef NFS 209 error = ENOSYS; 210 #else 211 error = copyin(SCARG(uap, argp), (caddr_t)&ncd, sizeof (ncd)); 212 if (error) 213 return (error); 214 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 215 ncd.ncd_dirp, p); 216 error = namei(&nd); 217 if (error) 218 return (error); 219 if ((nd.ni_vp->v_flag & VROOT) == 0) 220 error = EINVAL; 221 nmp = VFSTONFS(nd.ni_vp->v_mount); 222 vput(nd.ni_vp); 223 if (error) 224 return (error); 225 if ((nmp->nm_flag & NFSMNT_MNTD) && 226 (SCARG(uap, flag) & NFSSVC_GOTAUTH) == 0) 227 return (0); 228 nmp->nm_flag |= NFSMNT_MNTD; 229 error = nqnfs_clientd(nmp, p->p_ucred, &ncd, SCARG(uap, flag), 230 SCARG(uap, argp), p); 231 #endif /* NFS */ 232 } else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) { 233 #ifndef NFSSERVER 234 error = ENOSYS; 235 #else 236 error = copyin(SCARG(uap, argp), (caddr_t)&nfsdarg, 237 sizeof(nfsdarg)); 238 if (error) 239 return (error); 240 error = getsock(p->p_fd, nfsdarg.sock, &fp); 241 if (error) 242 return (error); 243 /* 244 * Get the client address for connected sockets. 245 */ 246 if (nfsdarg.name == NULL || nfsdarg.namelen == 0) 247 nam = (struct mbuf *)0; 248 else { 249 error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen, 250 MT_SONAME); 251 if (error) 252 return (error); 253 } 254 error = nfssvc_addsock(fp, nam); 255 #endif /* !NFSSERVER */ 256 } else { 257 #ifndef NFSSERVER 258 error = ENOSYS; 259 #else 260 error = copyin(SCARG(uap, argp), (caddr_t)nsd, sizeof (*nsd)); 261 if (error) 262 return (error); 263 if ((SCARG(uap, flag) & NFSSVC_AUTHIN) && 264 ((nfsd = nsd->nsd_nfsd)) != NULL && 265 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) { 266 slp = nfsd->nfsd_slp; 267 268 /* 269 * First check to see if another nfsd has already 270 * added this credential. 271 */ 272 for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first; 273 nuidp != 0; nuidp = nuidp->nu_hash.le_next) { 274 if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid && 275 (!nfsd->nfsd_nd->nd_nam2 || 276 netaddr_match(NU_NETFAM(nuidp), 277 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2))) 278 break; 279 } 280 if (nuidp) { 281 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr); 282 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL; 283 } else { 284 /* 285 * Nope, so we will. 286 */ 287 if (slp->ns_numuids < nuidhash_max) { 288 slp->ns_numuids++; 289 nuidp = (struct nfsuid *) 290 malloc(sizeof (struct nfsuid), M_NFSUID, 291 M_WAITOK); 292 } else 293 nuidp = (struct nfsuid *)0; 294 if ((slp->ns_flag & SLP_VALID) == 0) { 295 if (nuidp) 296 free((caddr_t)nuidp, M_NFSUID); 297 } else { 298 if (nuidp == (struct nfsuid *)0) { 299 nuidp = slp->ns_uidlruhead.tqh_first; 300 LIST_REMOVE(nuidp, nu_hash); 301 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, 302 nu_lru); 303 if (nuidp->nu_flag & NU_NAM) 304 m_freem(nuidp->nu_nam); 305 } 306 nuidp->nu_flag = 0; 307 nuidp->nu_cr = nsd->nsd_cr; 308 if (nuidp->nu_cr.cr_ngroups > NGROUPS) 309 nuidp->nu_cr.cr_ngroups = NGROUPS; 310 nuidp->nu_cr.cr_ref = 1; 311 nuidp->nu_timestamp = nsd->nsd_timestamp; 312 nuidp->nu_expire = time.tv_sec + nsd->nsd_ttl; 313 /* 314 * and save the session key in nu_key. 315 */ 316 bcopy(nsd->nsd_key, nuidp->nu_key, 317 sizeof (nsd->nsd_key)); 318 if (nfsd->nfsd_nd->nd_nam2) { 319 struct sockaddr_in *saddr; 320 321 saddr = mtod(nfsd->nfsd_nd->nd_nam2, 322 struct sockaddr_in *); 323 switch (saddr->sin_family) { 324 case AF_INET: 325 nuidp->nu_flag |= NU_INETADDR; 326 nuidp->nu_inetaddr = 327 saddr->sin_addr.s_addr; 328 break; 329 case AF_ISO: 330 default: 331 nuidp->nu_flag |= NU_NAM; 332 nuidp->nu_nam = m_copym( 333 nfsd->nfsd_nd->nd_nam2, 0, 334 M_COPYALL, M_WAIT); 335 break; 336 }; 337 } 338 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp, 339 nu_lru); 340 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid), 341 nuidp, nu_hash); 342 nfsrv_setcred(&nuidp->nu_cr, 343 &nfsd->nfsd_nd->nd_cr); 344 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL; 345 } 346 } 347 } 348 if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) && 349 (nfsd = nsd->nsd_nfsd)) 350 nfsd->nfsd_flag |= NFSD_AUTHFAIL; 351 error = nfssvc_nfsd(nsd, SCARG(uap, argp), p); 352 #endif /* !NFSSERVER */ 353 } 354 if (error == EINTR || error == ERESTART) 355 error = 0; 356 return (error); 357 } 358 359 #ifdef NFSSERVER 360 /* 361 * Adds a socket to the list for servicing by nfsds. 362 */ 363 int 364 nfssvc_addsock(fp, mynam) 365 struct file *fp; 366 struct mbuf *mynam; 367 { 368 register struct mbuf *m; 369 register int siz; 370 register struct nfssvc_sock *slp; 371 register struct socket *so; 372 struct nfssvc_sock *tslp; 373 int error, s; 374 375 so = (struct socket *)fp->f_data; 376 tslp = (struct nfssvc_sock *)0; 377 /* 378 * Add it to the list, as required. 379 */ 380 if (so->so_proto->pr_protocol == IPPROTO_UDP) { 381 tslp = nfs_udpsock; 382 if (tslp->ns_flag & SLP_VALID) { 383 m_freem(mynam); 384 return (EPERM); 385 } 386 #ifdef ISO 387 } else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) { 388 tslp = nfs_cltpsock; 389 if (tslp->ns_flag & SLP_VALID) { 390 m_freem(mynam); 391 return (EPERM); 392 } 393 #endif /* ISO */ 394 } 395 if (so->so_type == SOCK_STREAM) 396 siz = NFS_MAXPACKET + sizeof (u_long); 397 else 398 siz = NFS_MAXPACKET; 399 error = soreserve(so, siz, siz); 400 if (error) { 401 m_freem(mynam); 402 return (error); 403 } 404 405 /* 406 * Set protocol specific options { for now TCP only } and 407 * reserve some space. For datagram sockets, this can get called 408 * repeatedly for the same socket, but that isn't harmful. 409 */ 410 if (so->so_type == SOCK_STREAM) { 411 MGET(m, M_WAIT, MT_SOOPTS); 412 *mtod(m, int32_t *) = 1; 413 m->m_len = sizeof(int32_t); 414 sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m); 415 } 416 if (so->so_proto->pr_domain->dom_family == AF_INET && 417 so->so_proto->pr_protocol == IPPROTO_TCP) { 418 MGET(m, M_WAIT, MT_SOOPTS); 419 *mtod(m, int32_t *) = 1; 420 m->m_len = sizeof(int32_t); 421 sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m); 422 } 423 so->so_rcv.sb_flags &= ~SB_NOINTR; 424 so->so_rcv.sb_timeo = 0; 425 so->so_snd.sb_flags &= ~SB_NOINTR; 426 so->so_snd.sb_timeo = 0; 427 if (tslp) 428 slp = tslp; 429 else { 430 slp = (struct nfssvc_sock *) 431 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 432 bzero((caddr_t)slp, sizeof (struct nfssvc_sock)); 433 TAILQ_INIT(&slp->ns_uidlruhead); 434 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain); 435 } 436 slp->ns_so = so; 437 slp->ns_nam = mynam; 438 fp->f_count++; 439 slp->ns_fp = fp; 440 s = splsoftnet(); 441 so->so_upcallarg = (caddr_t)slp; 442 so->so_upcall = nfsrv_rcv; 443 slp->ns_flag = (SLP_VALID | SLP_NEEDQ); 444 nfsrv_wakenfsd(slp); 445 splx(s); 446 return (0); 447 } 448 449 /* 450 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests 451 * until it is killed by a signal. 452 */ 453 int 454 nfssvc_nfsd(nsd, argp, p) 455 struct nfsd_srvargs *nsd; 456 caddr_t argp; 457 struct proc *p; 458 { 459 register struct mbuf *m; 460 register int siz; 461 register struct nfssvc_sock *slp; 462 register struct socket *so; 463 register int *solockp; 464 struct nfsd *nfsd = nsd->nsd_nfsd; 465 struct nfsrv_descript *nd = NULL; 466 struct mbuf *mreq; 467 int error = 0, cacherep, s, sotype, writes_todo; 468 u_quad_t cur_usec; 469 470 #ifndef nolint 471 cacherep = RC_DOIT; 472 writes_todo = 0; 473 #endif 474 s = splsoftnet(); 475 if (nfsd == (struct nfsd *)0) { 476 nsd->nsd_nfsd = nfsd = (struct nfsd *) 477 malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK); 478 bzero((caddr_t)nfsd, sizeof (struct nfsd)); 479 nfsd->nfsd_procp = p; 480 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain); 481 nfs_numnfsd++; 482 } 483 /* 484 * Loop getting rpc requests until SIGKILL. 485 */ 486 for (;;) { 487 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) { 488 while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 && 489 (nfsd_head_flag & NFSD_CHECKSLP) == 0) { 490 nfsd->nfsd_flag |= NFSD_WAITING; 491 nfsd_waiting++; 492 error = tsleep((caddr_t)nfsd, PSOCK | PCATCH, 493 "nfsd", 0); 494 nfsd_waiting--; 495 if (error) 496 goto done; 497 } 498 if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 && 499 (nfsd_head_flag & NFSD_CHECKSLP) != 0) { 500 for (slp = nfssvc_sockhead.tqh_first; slp != 0; 501 slp = slp->ns_chain.tqe_next) { 502 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC)) 503 == (SLP_VALID | SLP_DOREC)) { 504 slp->ns_flag &= ~SLP_DOREC; 505 slp->ns_sref++; 506 nfsd->nfsd_slp = slp; 507 break; 508 } 509 } 510 if (slp == 0) 511 nfsd_head_flag &= ~NFSD_CHECKSLP; 512 } 513 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0) 514 continue; 515 if (slp->ns_flag & SLP_VALID) { 516 if (slp->ns_flag & SLP_DISCONN) 517 nfsrv_zapsock(slp); 518 else if (slp->ns_flag & SLP_NEEDQ) { 519 slp->ns_flag &= ~SLP_NEEDQ; 520 (void) nfs_sndlock(&slp->ns_solock, 521 (struct nfsreq *)0); 522 nfsrv_rcv(slp->ns_so, (caddr_t)slp, 523 M_WAIT); 524 nfs_sndunlock(&slp->ns_solock); 525 } 526 error = nfsrv_dorec(slp, nfsd, &nd); 527 cur_usec = (u_quad_t)time.tv_sec * 1000000 + 528 (u_quad_t)time.tv_usec; 529 if (error && slp->ns_tq.lh_first && 530 slp->ns_tq.lh_first->nd_time <= cur_usec) { 531 error = 0; 532 cacherep = RC_DOIT; 533 writes_todo = 1; 534 } else 535 writes_todo = 0; 536 nfsd->nfsd_flag |= NFSD_REQINPROG; 537 } 538 } else { 539 error = 0; 540 slp = nfsd->nfsd_slp; 541 } 542 if (error || (slp->ns_flag & SLP_VALID) == 0) { 543 if (nd) { 544 free((caddr_t)nd, M_NFSRVDESC); 545 nd = NULL; 546 } 547 nfsd->nfsd_slp = (struct nfssvc_sock *)0; 548 nfsd->nfsd_flag &= ~NFSD_REQINPROG; 549 nfsrv_slpderef(slp); 550 continue; 551 } 552 splx(s); 553 so = slp->ns_so; 554 sotype = so->so_type; 555 if (so->so_proto->pr_flags & PR_CONNREQUIRED) 556 solockp = &slp->ns_solock; 557 else 558 solockp = (int *)0; 559 if (nd) { 560 nd->nd_starttime = time; 561 if (nd->nd_nam2) 562 nd->nd_nam = nd->nd_nam2; 563 else 564 nd->nd_nam = slp->ns_nam; 565 566 /* 567 * Check to see if authorization is needed. 568 */ 569 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) { 570 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH; 571 nsd->nsd_haddr = mtod(nd->nd_nam, 572 struct sockaddr_in *)->sin_addr.s_addr; 573 nsd->nsd_authlen = nfsd->nfsd_authlen; 574 nsd->nsd_verflen = nfsd->nfsd_verflen; 575 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr, 576 nfsd->nfsd_authlen) && 577 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr, 578 nfsd->nfsd_verflen) && 579 !copyout((caddr_t)nsd, argp, sizeof (*nsd))) 580 return (ENEEDAUTH); 581 cacherep = RC_DROPIT; 582 } else 583 cacherep = nfsrv_getcache(nd, slp, &mreq); 584 585 /* 586 * Check for just starting up for NQNFS and send 587 * fake "try again later" replies to the NQNFS clients. 588 */ 589 if (notstarted && nqnfsstarttime <= time.tv_sec) { 590 if (modify_flag) { 591 nqnfsstarttime = time.tv_sec + nqsrv_writeslack; 592 modify_flag = 0; 593 } else 594 notstarted = 0; 595 } 596 if (notstarted) { 597 if ((nd->nd_flag & ND_NQNFS) == 0) 598 cacherep = RC_DROPIT; 599 else if (nd->nd_procnum != NFSPROC_WRITE) { 600 nd->nd_procnum = NFSPROC_NOOP; 601 nd->nd_repstat = NQNFS_TRYLATER; 602 cacherep = RC_DOIT; 603 } else 604 modify_flag = 1; 605 } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) { 606 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL; 607 nd->nd_procnum = NFSPROC_NOOP; 608 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 609 cacherep = RC_DOIT; 610 } 611 } 612 613 /* 614 * Loop to get all the write rpc relies that have been 615 * gathered together. 616 */ 617 do { 618 switch (cacherep) { 619 case RC_DOIT: 620 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE && 621 nfsrvw_procrastinate > 0 && !notstarted)) 622 error = nfsrv_writegather(&nd, slp, 623 nfsd->nfsd_procp, &mreq); 624 else 625 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd, 626 slp, nfsd->nfsd_procp, &mreq); 627 if (mreq == NULL) 628 break; 629 if (error) { 630 if (nd->nd_procnum != NQNFSPROC_VACATED) 631 nfsstats.srv_errs++; 632 nfsrv_updatecache(nd, FALSE, mreq); 633 if (nd->nd_nam2) 634 m_freem(nd->nd_nam2); 635 break; 636 } 637 nfsstats.srvrpccnt[nd->nd_procnum]++; 638 nfsrv_updatecache(nd, TRUE, mreq); 639 nd->nd_mrep = (struct mbuf *)0; 640 case RC_REPLY: 641 m = mreq; 642 siz = 0; 643 while (m) { 644 siz += m->m_len; 645 m = m->m_next; 646 } 647 if (siz <= 0 || siz > NFS_MAXPACKET) { 648 printf("mbuf siz=%d\n",siz); 649 panic("Bad nfs svc reply"); 650 } 651 m = mreq; 652 m->m_pkthdr.len = siz; 653 m->m_pkthdr.rcvif = (struct ifnet *)0; 654 /* 655 * For stream protocols, prepend a Sun RPC 656 * Record Mark. 657 */ 658 if (sotype == SOCK_STREAM) { 659 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT); 660 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz); 661 } 662 if (solockp) 663 (void) nfs_sndlock(solockp, (struct nfsreq *)0); 664 if (slp->ns_flag & SLP_VALID) 665 error = nfs_send(so, nd->nd_nam2, m, NULL); 666 else { 667 error = EPIPE; 668 m_freem(m); 669 } 670 if (nfsrtton) 671 nfsd_rt(sotype, nd, cacherep); 672 if (nd->nd_nam2) 673 MFREE(nd->nd_nam2, m); 674 if (nd->nd_mrep) 675 m_freem(nd->nd_mrep); 676 if (error == EPIPE) 677 nfsrv_zapsock(slp); 678 if (solockp) 679 nfs_sndunlock(solockp); 680 if (error == EINTR || error == ERESTART) { 681 free((caddr_t)nd, M_NFSRVDESC); 682 nfsrv_slpderef(slp); 683 s = splsoftnet(); 684 goto done; 685 } 686 break; 687 case RC_DROPIT: 688 if (nfsrtton) 689 nfsd_rt(sotype, nd, cacherep); 690 m_freem(nd->nd_mrep); 691 m_freem(nd->nd_nam2); 692 break; 693 }; 694 if (nd) { 695 FREE((caddr_t)nd, M_NFSRVDESC); 696 nd = NULL; 697 } 698 699 /* 700 * Check to see if there are outstanding writes that 701 * need to be serviced. 702 */ 703 cur_usec = (u_quad_t)time.tv_sec * 1000000 + 704 (u_quad_t)time.tv_usec; 705 s = splsoftclock(); 706 if (slp->ns_tq.lh_first && 707 slp->ns_tq.lh_first->nd_time <= cur_usec) { 708 cacherep = RC_DOIT; 709 writes_todo = 1; 710 } else 711 writes_todo = 0; 712 splx(s); 713 } while (writes_todo); 714 s = splsoftnet(); 715 if (nfsrv_dorec(slp, nfsd, &nd)) { 716 nfsd->nfsd_flag &= ~NFSD_REQINPROG; 717 nfsd->nfsd_slp = NULL; 718 nfsrv_slpderef(slp); 719 } 720 } 721 done: 722 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain); 723 splx(s); 724 free((caddr_t)nfsd, M_NFSD); 725 nsd->nsd_nfsd = (struct nfsd *)0; 726 if (--nfs_numnfsd == 0) 727 nfsrv_init(TRUE); /* Reinitialize everything */ 728 return (error); 729 } 730 731 /* 732 * Shut down a socket associated with an nfssvc_sock structure. 733 * Should be called with the send lock set, if required. 734 * The trick here is to increment the sref at the start, so that the nfsds 735 * will stop using it and clear ns_flag at the end so that it will not be 736 * reassigned during cleanup. 737 */ 738 void 739 nfsrv_zapsock(slp) 740 register struct nfssvc_sock *slp; 741 { 742 register struct nfsuid *nuidp, *nnuidp; 743 register struct nfsrv_descript *nwp, *nnwp; 744 struct socket *so; 745 struct file *fp; 746 struct mbuf *m; 747 int s; 748 749 slp->ns_flag &= ~SLP_ALLFLAGS; 750 fp = slp->ns_fp; 751 if (fp) { 752 slp->ns_fp = (struct file *)0; 753 so = slp->ns_so; 754 so->so_upcall = NULL; 755 soshutdown(so, 2); 756 closef(fp, (struct proc *)0); 757 if (slp->ns_nam) 758 MFREE(slp->ns_nam, m); 759 m_freem(slp->ns_raw); 760 m_freem(slp->ns_rec); 761 for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0; 762 nuidp = nnuidp) { 763 nnuidp = nuidp->nu_lru.tqe_next; 764 LIST_REMOVE(nuidp, nu_hash); 765 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru); 766 if (nuidp->nu_flag & NU_NAM) 767 m_freem(nuidp->nu_nam); 768 free((caddr_t)nuidp, M_NFSUID); 769 } 770 s = splsoftclock(); 771 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) { 772 nnwp = nwp->nd_tq.le_next; 773 LIST_REMOVE(nwp, nd_tq); 774 free((caddr_t)nwp, M_NFSRVDESC); 775 } 776 LIST_INIT(&slp->ns_tq); 777 splx(s); 778 } 779 } 780 781 /* 782 * Derefence a server socket structure. If it has no more references and 783 * is no longer valid, you can throw it away. 784 */ 785 void 786 nfsrv_slpderef(slp) 787 register struct nfssvc_sock *slp; 788 { 789 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) { 790 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain); 791 free((caddr_t)slp, M_NFSSVC); 792 } 793 } 794 795 /* 796 * Initialize the data structures for the server. 797 * Handshake with any new nfsds starting up to avoid any chance of 798 * corruption. 799 */ 800 void 801 nfsrv_init(terminating) 802 int terminating; 803 { 804 register struct nfssvc_sock *slp, *nslp; 805 806 if (nfssvc_sockhead_flag & SLP_INIT) 807 panic("nfsd init"); 808 nfssvc_sockhead_flag |= SLP_INIT; 809 if (terminating) { 810 for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) { 811 nslp = slp->ns_chain.tqe_next; 812 if (slp->ns_flag & SLP_VALID) 813 nfsrv_zapsock(slp); 814 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain); 815 free((caddr_t)slp, M_NFSSVC); 816 } 817 nfsrv_cleancache(); /* And clear out server cache */ 818 } 819 820 TAILQ_INIT(&nfssvc_sockhead); 821 nfssvc_sockhead_flag &= ~SLP_INIT; 822 if (nfssvc_sockhead_flag & SLP_WANTINIT) { 823 nfssvc_sockhead_flag &= ~SLP_WANTINIT; 824 wakeup((caddr_t)&nfssvc_sockhead); 825 } 826 827 TAILQ_INIT(&nfsd_head); 828 nfsd_head_flag &= ~NFSD_CHECKSLP; 829 830 nfs_udpsock = (struct nfssvc_sock *) 831 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 832 bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock)); 833 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead); 834 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain); 835 836 nfs_cltpsock = (struct nfssvc_sock *) 837 malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK); 838 bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock)); 839 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead); 840 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain); 841 } 842 843 /* 844 * Add entries to the server monitor log. 845 */ 846 static void 847 nfsd_rt(sotype, nd, cacherep) 848 int sotype; 849 register struct nfsrv_descript *nd; 850 int cacherep; 851 { 852 register struct drt *rt; 853 854 rt = &nfsdrt.drt[nfsdrt.pos]; 855 if (cacherep == RC_DOIT) 856 rt->flag = 0; 857 else if (cacherep == RC_REPLY) 858 rt->flag = DRT_CACHEREPLY; 859 else 860 rt->flag = DRT_CACHEDROP; 861 if (sotype == SOCK_STREAM) 862 rt->flag |= DRT_TCP; 863 if (nd->nd_flag & ND_NQNFS) 864 rt->flag |= DRT_NQNFS; 865 else if (nd->nd_flag & ND_NFSV3) 866 rt->flag |= DRT_NFSV3; 867 rt->proc = nd->nd_procnum; 868 if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET) 869 rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr; 870 else 871 rt->ipadr = INADDR_ANY; 872 rt->resptime = ((time.tv_sec - nd->nd_starttime.tv_sec) * 1000000) + 873 (time.tv_usec - nd->nd_starttime.tv_usec); 874 rt->tstamp = time; 875 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ; 876 } 877 #endif /* NFSSERVER */ 878 879 #ifdef NFS 880 881 int nfs_defect = 0; 882 /* 883 * Asynchronous I/O daemons for client nfs. 884 * They do read-ahead and write-behind operations on the block I/O cache. 885 * Never returns unless it fails or gets killed. 886 */ 887 int 888 nfssvc_iod(p) 889 struct proc *p; 890 { 891 register struct buf *bp; 892 register int i, myiod; 893 struct nfsmount *nmp; 894 int error = 0; 895 896 /* 897 * Assign my position or return error if too many already running 898 */ 899 myiod = -1; 900 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) 901 if (nfs_asyncdaemon[i] == 0) { 902 nfs_asyncdaemon[i]++; 903 myiod = i; 904 break; 905 } 906 if (myiod == -1) 907 return (EBUSY); 908 nfs_numasync++; 909 /* 910 * Just loop around doin our stuff until SIGKILL 911 */ 912 for (;;) { 913 while (((nmp = nfs_iodmount[myiod]) == NULL 914 || nmp->nm_bufq.tqh_first == NULL) 915 && error == 0) { 916 if (nmp) 917 nmp->nm_bufqiods--; 918 nfs_iodwant[myiod] = p; 919 nfs_iodmount[myiod] = NULL; 920 error = tsleep((caddr_t)&nfs_iodwant[myiod], 921 PWAIT | PCATCH, "nfsidl", 0); 922 } 923 if (error) { 924 nfs_asyncdaemon[myiod] = 0; 925 if (nmp) 926 nmp->nm_bufqiods--; 927 nfs_iodmount[myiod] = NULL; 928 nfs_numasync--; 929 return (error); 930 } 931 while ((bp = nmp->nm_bufq.tqh_first) != NULL) { 932 /* Take one off the front of the list */ 933 TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist); 934 nmp->nm_bufqlen--; 935 if (nmp->nm_bufqwant && nmp->nm_bufqlen < 2 * nfs_numasync) { 936 nmp->nm_bufqwant = FALSE; 937 wakeup(&nmp->nm_bufq); 938 } 939 if (bp->b_flags & B_READ) 940 (void) nfs_doio(bp, bp->b_rcred, (struct proc *)0); 941 else 942 (void) nfs_doio(bp, bp->b_wcred, (struct proc *)0); 943 /* 944 * If there are more than one iod on this mount, then defect 945 * so that the iods can be shared out fairly between the mounts 946 */ 947 if (nfs_defect && nmp->nm_bufqiods > 1) { 948 nfs_iodmount[myiod] = NULL; 949 nmp->nm_bufqiods--; 950 break; 951 } 952 } 953 } 954 } 955 956 957 /* 958 * Get an authorization string for the uid by having the mount_nfs sitting 959 * on this mount point porpous out of the kernel and do it. 960 */ 961 int 962 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key) 963 register struct nfsmount *nmp; 964 struct nfsreq *rep; 965 struct ucred *cred; 966 char **auth_str; 967 int *auth_len; 968 char *verf_str; 969 int *verf_len; 970 NFSKERBKEY_T key; /* return session key */ 971 { 972 int error = 0; 973 974 while ((nmp->nm_flag & NFSMNT_WAITAUTH) == 0) { 975 nmp->nm_flag |= NFSMNT_WANTAUTH; 976 (void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK, 977 "nfsauth1", 2 * hz); 978 error = nfs_sigintr(nmp, rep, rep->r_procp); 979 if (error) { 980 nmp->nm_flag &= ~NFSMNT_WANTAUTH; 981 return (error); 982 } 983 } 984 nmp->nm_flag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH); 985 nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK); 986 nmp->nm_authlen = RPCAUTH_MAXSIZ; 987 nmp->nm_verfstr = verf_str; 988 nmp->nm_verflen = *verf_len; 989 nmp->nm_authuid = cred->cr_uid; 990 wakeup((caddr_t)&nmp->nm_authstr); 991 992 /* 993 * And wait for mount_nfs to do its stuff. 994 */ 995 while ((nmp->nm_flag & NFSMNT_HASAUTH) == 0 && error == 0) { 996 (void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK, 997 "nfsauth2", 2 * hz); 998 error = nfs_sigintr(nmp, rep, rep->r_procp); 999 } 1000 if (nmp->nm_flag & NFSMNT_AUTHERR) { 1001 nmp->nm_flag &= ~NFSMNT_AUTHERR; 1002 error = EAUTH; 1003 } 1004 if (error) 1005 free((caddr_t)*auth_str, M_TEMP); 1006 else { 1007 *auth_len = nmp->nm_authlen; 1008 *verf_len = nmp->nm_verflen; 1009 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key)); 1010 } 1011 nmp->nm_flag &= ~NFSMNT_HASAUTH; 1012 nmp->nm_flag |= NFSMNT_WAITAUTH; 1013 if (nmp->nm_flag & NFSMNT_WANTAUTH) { 1014 nmp->nm_flag &= ~NFSMNT_WANTAUTH; 1015 wakeup((caddr_t)&nmp->nm_authtype); 1016 } 1017 return (error); 1018 } 1019 1020 /* 1021 * Get a nickname authenticator and verifier. 1022 */ 1023 int 1024 nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len) 1025 struct nfsmount *nmp; 1026 struct ucred *cred; 1027 char **auth_str; 1028 int *auth_len; 1029 char *verf_str; 1030 int verf_len; 1031 { 1032 register struct nfsuid *nuidp; 1033 register u_int32_t *nickp, *verfp; 1034 struct timeval ktvin, ktvout; 1035 1036 #ifdef DIAGNOSTIC 1037 if (verf_len < (4 * NFSX_UNSIGNED)) 1038 panic("nfs_getnickauth verf too small"); 1039 #endif 1040 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first; 1041 nuidp != 0; nuidp = nuidp->nu_hash.le_next) { 1042 if (nuidp->nu_cr.cr_uid == cred->cr_uid) 1043 break; 1044 } 1045 if (!nuidp || nuidp->nu_expire < time.tv_sec) 1046 return (EACCES); 1047 1048 /* 1049 * Move to the end of the lru list (end of lru == most recently used). 1050 */ 1051 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru); 1052 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru); 1053 1054 nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK); 1055 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME); 1056 *nickp = txdr_unsigned(nuidp->nu_nickname); 1057 *auth_str = (char *)nickp; 1058 *auth_len = 2 * NFSX_UNSIGNED; 1059 1060 /* 1061 * Now we must encrypt the verifier and package it up. 1062 */ 1063 verfp = (u_int32_t *)verf_str; 1064 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME); 1065 if (time.tv_sec > nuidp->nu_timestamp.tv_sec || 1066 (time.tv_sec == nuidp->nu_timestamp.tv_sec && 1067 time.tv_usec > nuidp->nu_timestamp.tv_usec)) 1068 nuidp->nu_timestamp = time; 1069 else 1070 nuidp->nu_timestamp.tv_usec++; 1071 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec); 1072 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec); 1073 1074 /* 1075 * Now encrypt the timestamp verifier in ecb mode using the session 1076 * key. 1077 */ 1078 #ifdef NFSKERB 1079 XXX 1080 #endif 1081 1082 *verfp++ = ktvout.tv_sec; 1083 *verfp++ = ktvout.tv_usec; 1084 *verfp = 0; 1085 return (0); 1086 } 1087 1088 /* 1089 * Save the current nickname in a hash list entry on the mount point. 1090 */ 1091 int 1092 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep) 1093 register struct nfsmount *nmp; 1094 struct ucred *cred; 1095 int len; 1096 NFSKERBKEY_T key; 1097 struct mbuf **mdp; 1098 char **dposp; 1099 struct mbuf *mrep; 1100 { 1101 register struct nfsuid *nuidp; 1102 register u_int32_t *tl; 1103 register int32_t t1; 1104 struct mbuf *md = *mdp; 1105 struct timeval ktvin, ktvout; 1106 u_int32_t nick; 1107 char *dpos = *dposp, *cp2; 1108 int deltasec, error = 0; 1109 1110 if (len == (3 * NFSX_UNSIGNED)) { 1111 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1112 ktvin.tv_sec = *tl++; 1113 ktvin.tv_usec = *tl++; 1114 nick = fxdr_unsigned(u_int32_t, *tl); 1115 1116 /* 1117 * Decrypt the timestamp in ecb mode. 1118 */ 1119 #ifdef NFSKERB 1120 XXX 1121 #endif 1122 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec); 1123 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec); 1124 deltasec = time.tv_sec - ktvout.tv_sec; 1125 if (deltasec < 0) 1126 deltasec = -deltasec; 1127 /* 1128 * If ok, add it to the hash list for the mount point. 1129 */ 1130 if (deltasec <= NFS_KERBCLOCKSKEW) { 1131 if (nmp->nm_numuids < nuidhash_max) { 1132 nmp->nm_numuids++; 1133 nuidp = (struct nfsuid *) 1134 malloc(sizeof (struct nfsuid), M_NFSUID, 1135 M_WAITOK); 1136 } else { 1137 nuidp = nmp->nm_uidlruhead.tqh_first; 1138 LIST_REMOVE(nuidp, nu_hash); 1139 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, 1140 nu_lru); 1141 } 1142 nuidp->nu_flag = 0; 1143 nuidp->nu_cr.cr_uid = cred->cr_uid; 1144 nuidp->nu_expire = time.tv_sec + NFS_KERBTTL; 1145 nuidp->nu_timestamp = ktvout; 1146 nuidp->nu_nickname = nick; 1147 bcopy(key, nuidp->nu_key, sizeof (key)); 1148 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, 1149 nu_lru); 1150 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid), 1151 nuidp, nu_hash); 1152 } 1153 } else 1154 nfsm_adv(nfsm_rndup(len)); 1155 nfsmout: 1156 *mdp = md; 1157 *dposp = dpos; 1158 return (error); 1159 } 1160 #endif /* NFS */ 1161