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