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