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