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