]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_syscalls.c
xnu-517.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_syscalls.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26 /*
27 * Copyright (c) 1989, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * This code is derived from software contributed to Berkeley by
31 * Rick Macklem at The University of Guelph.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
62 * FreeBSD-Id: nfs_syscalls.c,v 1.32 1997/11/07 08:53:25 phk Exp $
63 */
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 /* XXX CSM 11/25/97 FreeBSD's generated syscall prototypes */
68 #ifdef notyet
69 #include <sys/sysproto.h>
70 #endif
71 #include <sys/kernel.h>
72 #include <sys/file.h>
73 #include <sys/filedesc.h>
74 #include <sys/stat.h>
75 #include <sys/vnode.h>
76 #include <sys/mount.h>
77 #include <sys/proc.h>
78 #include <sys/sysctl.h>
79 #include <sys/ubc.h>
80 #include <sys/uio.h>
81 #include <sys/malloc.h>
82 #include <sys/mbuf.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/domain.h>
86 #include <sys/protosw.h>
87 #include <sys/namei.h>
88 #include <sys/fcntl.h>
89 #include <sys/lockf.h>
90 #include <sys/syslog.h>
91 #include <sys/user.h>
92 #include <machine/spl.h>
93
94 #include <netinet/in.h>
95 #include <netinet/tcp.h>
96 #if ISO
97 #include <netiso/iso.h>
98 #endif
99 #include <nfs/xdr_subs.h>
100 #include <nfs/rpcv2.h>
101 #include <nfs/nfsproto.h>
102 #include <nfs/nfs.h>
103 #include <nfs/nfsm_subs.h>
104 #include <nfs/nfsrvcache.h>
105 #include <nfs/nfsmount.h>
106 #include <nfs/nfsnode.h>
107 #include <nfs/nqnfs.h>
108 #include <nfs/nfsrtt.h>
109 #include <nfs/nfs_lock.h>
110
111 /* Global defs. */
112 extern int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *nd,
113 struct nfssvc_sock *slp,
114 struct proc *procp,
115 struct mbuf **mreqp));
116 extern int nfs_numasync;
117 extern int nfs_ioddelwri;
118 extern time_t nqnfsstarttime;
119 extern int nqsrv_writeslack;
120 extern int nfsrtton;
121 extern struct nfsstats nfsstats;
122 extern int nfsrvw_procrastinate;
123 extern int nfsrvw_procrastinate_v3;
124 struct nfssvc_sock *nfs_udpsock, *nfs_cltpsock;
125 static int nuidhash_max = NFS_MAXUIDHASH;
126
127 static void nfsrv_zapsock __P((struct nfssvc_sock *slp));
128 static int nfssvc_iod __P((struct proc *));
129
130 #define TRUE 1
131 #define FALSE 0
132
133 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
134
135 #ifndef NFS_NOSERVER
136 int nfsd_waiting = 0;
137 static struct nfsdrt nfsdrt;
138 static int nfs_numnfsd = 0;
139 static int notstarted = 1;
140 static int modify_flag = 0;
141 static void nfsd_rt __P((int sotype, struct nfsrv_descript *nd,
142 int cacherep));
143 static int nfssvc_addsock __P((struct file *, struct mbuf *,
144 struct proc *));
145 static int nfssvc_nfsd __P((struct nfsd_srvargs *,caddr_t,struct proc *));
146
147 static int nfs_privport = 0;
148 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
149 #ifdef notyet
150 SYSCTL_INT(_vfs_nfs, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW, &nfs_privport, 0, "");
151 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay, CTLFLAG_RW, &nfsrvw_procrastinate, 0, "");
152 SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay_v3, CTLFLAG_RW, &nfsrvw_procrastinate_v3, 0, "");
153 #endif
154
155 /*
156 * NFS server system calls
157 * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c
158 */
159
160 /*
161 * Get file handle system call
162 */
163 #ifndef _SYS_SYSPROTO_H_
164 struct getfh_args {
165 char *fname;
166 fhandle_t *fhp;
167 };
168 #endif
169 int
170 getfh(p, uap)
171 struct proc *p;
172 register struct getfh_args *uap;
173 {
174 register struct vnode *vp;
175 fhandle_t fh;
176 int error;
177 struct nameidata nd;
178
179 /*
180 * Must be super user
181 */
182 error = suser(p->p_ucred, &p->p_acflag);
183 if(error)
184 return (error);
185 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1, UIO_USERSPACE, uap->fname, p);
186 error = namei(&nd);
187 if (error)
188 return (error);
189 vp = nd.ni_vp;
190 bzero((caddr_t)&fh, sizeof(fh));
191 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
192 error = VFS_VPTOFH(vp, &fh.fh_fid);
193 vput(vp);
194 if (error)
195 return (error);
196 error = copyout((caddr_t)&fh, (caddr_t)uap->fhp, sizeof (fh));
197 return (error);
198 }
199
200 #endif /* NFS_NOSERVER */
201
202 /*
203 * syscall for the rpc.lockd to use to translate a NFS file handle into
204 * an open descriptor.
205 *
206 * warning: do not remove the suser() call or this becomes one giant
207 * security hole.
208 */
209 #ifndef _SYS_SYSPROTO_H_
210 struct fhopen_args {
211 const struct fhandle *u_fhp;
212 int flags;
213 };
214 #endif
215 int
216 fhopen(p, uap, retval)
217 struct proc *p;
218 register struct fhopen_args *uap;
219 register_t *retval;
220 {
221 struct mount *mp;
222 struct vnode *vp;
223 struct fhandle fhp;
224 struct vattr vat;
225 struct vattr *vap = &vat;
226 struct flock lf;
227 struct file *fp;
228 register struct filedesc *fdp = p->p_fd;
229 int fmode, mode, error, type;
230 struct file *nfp;
231 int indx;
232 struct ucred *credanon;
233 int exflags;
234 struct ucred *cred = p->p_ucred;
235 int didhold = 0;
236 extern struct fileops vnops;
237
238 /*
239 * Must be super user
240 */
241 error = suser(cred, &p->p_acflag);
242 if (error)
243 return (error);
244
245 fmode = FFLAGS(uap->flags);
246 /* why not allow a non-read/write open for our lockd? */
247 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
248 return (EINVAL);
249 error = copyin((void*)uap->u_fhp, &fhp, sizeof(fhp));
250 if (error)
251 return (error);
252 /* find the mount point */
253 mp = vfs_getvfs(&fhp.fh_fsid);
254 if (mp == NULL)
255 return (ESTALE);
256 /* now give me my vnode, it gets returned to me locked */
257 /* XXX CSM need to split VFS_CHECKEXP out of VFS_FHTOVP? */
258 error = VFS_FHTOVP(mp, &fhp.fh_fid, NULL, &vp, &exflags, &credanon);
259 if (error)
260 return (error);
261 /*
262 * from now on we have to make sure not
263 * to forget about the vnode
264 * any error that causes an abort must vput(vp)
265 * just set error = err and 'goto bad;'.
266 */
267
268 /*
269 * from vn_open
270 */
271 if (vp->v_type == VSOCK) {
272 error = EOPNOTSUPP;
273 goto bad;
274 }
275
276 if (UBCINFOEXISTS(vp) && ((didhold = ubc_hold(vp)) == 0)) {
277 error = ENOENT;
278 goto bad;
279 }
280
281 if (fmode & FREAD && fmode & (FWRITE | O_TRUNC)) {
282 int err = 0;
283 if (vp->v_type == VDIR)
284 err = EISDIR;
285 else
286 err = vn_writechk(vp);
287 if (err && !(error = VOP_ACCESS(vp, VREAD, cred, p)))
288 error = err;
289 if (error || (error = VOP_ACCESS(vp, VREAD|VWRITE, cred, p)))
290 goto bad;
291 } else if (fmode & FREAD) {
292 if ((error = VOP_ACCESS(vp, VREAD, cred, p)))
293 goto bad;
294 } else if (fmode & (FWRITE | O_TRUNC)) {
295 if (vp->v_type == VDIR) {
296 error = EISDIR;
297 goto bad;
298 }
299 if ((error = vn_writechk(vp)) ||
300 (error = VOP_ACCESS(vp, VWRITE, cred, p)))
301 goto bad;
302 }
303 if (fmode & O_TRUNC) {
304 VOP_UNLOCK(vp, 0, p); /* XXX */
305 VOP_LEASE(vp, p, cred, LEASE_WRITE);
306 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* XXX */
307 VATTR_NULL(vap);
308 vap->va_size = 0;
309 error = VOP_SETATTR(vp, vap, cred, p);
310 if (error)
311 goto bad;
312 }
313
314 error = VOP_OPEN(vp, fmode, cred, p);
315 if (error)
316 goto bad;
317
318 if (fmode & FWRITE)
319 if (++vp->v_writecount <= 0)
320 panic("fhopen: v_writecount");
321 /*
322 * end of vn_open code
323 */
324
325 if ((error = falloc(p, &nfp, &indx)) != 0) {
326 if (fmode & FWRITE)
327 vp->v_writecount--;
328 goto bad;
329 }
330 fp = nfp;
331
332 /*
333 * Hold an extra reference to avoid having fp ripped out
334 * from under us while we block in the lock op
335 */
336 fref(fp);
337 nfp->f_data = (caddr_t)vp;
338 nfp->f_flag = fmode & FMASK;
339 nfp->f_ops = &vnops;
340 nfp->f_type = DTYPE_VNODE;
341 if (fmode & (O_EXLOCK | O_SHLOCK)) {
342 lf.l_whence = SEEK_SET;
343 lf.l_start = 0;
344 lf.l_len = 0;
345 if (fmode & O_EXLOCK)
346 lf.l_type = F_WRLCK;
347 else
348 lf.l_type = F_RDLCK;
349 type = F_FLOCK;
350 if ((fmode & FNONBLOCK) == 0)
351 type |= F_WAIT;
352 VOP_UNLOCK(vp, 0, p);
353 if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
354 type)) != 0) {
355 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
356 ffree(fp);
357 fdrelse(p, indx);
358 /*
359 * release our private reference
360 */
361 frele(fp);
362
363 return (error);
364 }
365 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
366 fp->f_flag |= FHASLOCK;
367 }
368
369 VOP_UNLOCK(vp, 0, p);
370 *fdflags(p, indx) &= ~UF_RESERVED;
371 frele(fp);
372 *retval = indx;
373 return (0);
374
375 bad:
376 VOP_UNLOCK(vp, 0, p);
377 if (didhold)
378 ubc_rele(vp);
379 vrele(vp);
380 return (error);
381 }
382
383 /*
384 * Nfs server psuedo system call for the nfsd's
385 * Based on the flag value it either:
386 * - adds a socket to the selection list
387 * - remains in the kernel as an nfsd
388 * - remains in the kernel as an nfsiod
389 */
390 #ifndef _SYS_SYSPROTO_H_
391 struct nfssvc_args {
392 int flag;
393 caddr_t argp;
394 };
395 #endif
396 int
397 nfssvc(p, uap)
398 struct proc *p;
399 register struct nfssvc_args *uap;
400 {
401 #ifndef NFS_NOSERVER
402 struct nameidata nd;
403 struct file *fp;
404 struct mbuf *nam;
405 struct nfsd_args nfsdarg;
406 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
407 struct nfsd_cargs ncd;
408 struct nfsd *nfsd;
409 struct nfssvc_sock *slp;
410 struct nfsuid *nuidp;
411 struct nfsmount *nmp;
412 struct timeval now;
413 #endif /* NFS_NOSERVER */
414 int error;
415
416 /*
417 * Must be super user
418 */
419 error = suser(p->p_ucred, &p->p_acflag);
420 if(error)
421 return (error);
422 while (nfssvc_sockhead_flag & SLP_INIT) {
423 nfssvc_sockhead_flag |= SLP_WANTINIT;
424 (void) tsleep((caddr_t)&nfssvc_sockhead, PSOCK, "nfsd init", 0);
425 }
426 if (uap->flag & NFSSVC_BIOD)
427 error = nfssvc_iod(p);
428 #ifdef NFS_NOSERVER
429 else
430 error = ENXIO;
431 #else /* !NFS_NOSERVER */
432 else if (uap->flag & NFSSVC_MNTD) {
433 error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
434 if (error)
435 return (error);
436 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
437 ncd.ncd_dirp, p);
438 error = namei(&nd);
439 if (error)
440 return (error);
441 if ((nd.ni_vp->v_flag & VROOT) == 0)
442 error = EINVAL;
443 nmp = VFSTONFS(nd.ni_vp->v_mount);
444 vput(nd.ni_vp);
445 if (error)
446 return (error);
447
448 if ((nmp->nm_state & NFSSTA_MNTD) &&
449 (uap->flag & NFSSVC_GOTAUTH) == 0)
450 return (0);
451 nmp->nm_state |= NFSSTA_MNTD;
452 error = nqnfs_clientd(nmp, p->p_ucred, &ncd, uap->flag,
453 uap->argp, p);
454 } else if (uap->flag & NFSSVC_ADDSOCK) {
455 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
456 if (error)
457 return (error);
458 error = getsock(p->p_fd, nfsdarg.sock, &fp);
459 if (error)
460 return (error);
461 /*
462 * Get the client address for connected sockets.
463 */
464 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
465 nam = (struct mbuf *)0;
466 else {
467 error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
468 MT_SONAME);
469 if (error)
470 return (error);
471 }
472 error = nfssvc_addsock(fp, nam, p);
473 } else {
474 error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
475 if (error)
476 return (error);
477
478 if ((uap->flag & NFSSVC_AUTHIN) && ((nfsd = nsd->nsd_nfsd)) &&
479 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
480 slp = nfsd->nfsd_slp;
481
482 /*
483 * First check to see if another nfsd has already
484 * added this credential.
485 */
486 for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
487 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
488 if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
489 (!nfsd->nfsd_nd->nd_nam2 ||
490 netaddr_match(NU_NETFAM(nuidp),
491 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
492 break;
493 }
494 if (nuidp) {
495 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
496 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
497 } else {
498 /*
499 * Nope, so we will.
500 */
501 if (slp->ns_numuids < nuidhash_max) {
502 slp->ns_numuids++;
503 nuidp = (struct nfsuid *)
504 _MALLOC_ZONE(sizeof (struct nfsuid),
505 M_NFSUID, M_WAITOK);
506 } else
507 nuidp = (struct nfsuid *)0;
508 if ((slp->ns_flag & SLP_VALID) == 0) {
509 if (nuidp)
510 FREE_ZONE((caddr_t)nuidp,
511 sizeof (struct nfsuid), M_NFSUID);
512 } else {
513 if (nuidp == (struct nfsuid *)0) {
514 nuidp = slp->ns_uidlruhead.tqh_first;
515 LIST_REMOVE(nuidp, nu_hash);
516 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
517 nu_lru);
518 if (nuidp->nu_flag & NU_NAM)
519 m_freem(nuidp->nu_nam);
520 }
521 nuidp->nu_flag = 0;
522 nuidp->nu_cr = nsd->nsd_cr;
523 if (nuidp->nu_cr.cr_ngroups > NGROUPS)
524 nuidp->nu_cr.cr_ngroups = NGROUPS;
525 nuidp->nu_cr.cr_ref = 1;
526 nuidp->nu_timestamp = nsd->nsd_timestamp;
527 microtime(&now);
528 nuidp->nu_expire = now.tv_sec + nsd->nsd_ttl;
529 /*
530 * and save the session key in nu_key.
531 */
532 bcopy(nsd->nsd_key, nuidp->nu_key,
533 sizeof (nsd->nsd_key));
534 if (nfsd->nfsd_nd->nd_nam2) {
535 struct sockaddr_in *saddr;
536
537 saddr = mtod(nfsd->nfsd_nd->nd_nam2,
538 struct sockaddr_in *);
539 switch (saddr->sin_family) {
540 case AF_INET:
541 nuidp->nu_flag |= NU_INETADDR;
542 nuidp->nu_inetaddr =
543 saddr->sin_addr.s_addr;
544 break;
545 case AF_ISO:
546 default:
547 nuidp->nu_flag |= NU_NAM;
548 nuidp->nu_nam = m_copym(
549 nfsd->nfsd_nd->nd_nam2, 0,
550 M_COPYALL, M_WAIT);
551 break;
552 };
553 }
554 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
555 nu_lru);
556 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
557 nuidp, nu_hash);
558 nfsrv_setcred(&nuidp->nu_cr,
559 &nfsd->nfsd_nd->nd_cr);
560 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
561 }
562 }
563 }
564 if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
565 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
566 error = nfssvc_nfsd(nsd, uap->argp, p);
567 }
568 #endif /* NFS_NOSERVER */
569 if (error == EINTR || error == ERESTART)
570 error = 0;
571 return (error);
572 }
573
574 #ifndef NFS_NOSERVER
575 /*
576 * Adds a socket to the list for servicing by nfsds.
577 */
578 static int
579 nfssvc_addsock(fp, mynam, p)
580 struct file *fp;
581 struct mbuf *mynam;
582 struct proc *p;
583 {
584 register struct mbuf *m;
585 register int siz;
586 register struct nfssvc_sock *slp;
587 register struct socket *so;
588 struct nfssvc_sock *tslp;
589 int error, s;
590
591 so = (struct socket *)fp->f_data;
592 tslp = (struct nfssvc_sock *)0;
593 /*
594 * Add it to the list, as required.
595 */
596 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
597 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
598 tslp = nfs_udpsock;
599 if (tslp->ns_flag & SLP_VALID) {
600 m_freem(mynam);
601 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
602 return (EPERM);
603 }
604 #if ISO
605 } else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
606 tslp = nfs_cltpsock;
607 if (tslp->ns_flag & SLP_VALID) {
608 m_freem(mynam);
609 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
610 return (EPERM);
611 }
612 #endif /* ISO */
613 }
614 /* reserve buffer space for 2 maximally-sized packets */
615 siz = NFS_MAXPACKET;
616 if (so->so_type == SOCK_STREAM)
617 siz += sizeof (u_long);
618 siz *= 2;
619 if (siz > NFS_MAXSOCKBUF)
620 siz = NFS_MAXSOCKBUF;
621 error = soreserve(so, siz, siz);
622 if (error) {
623 m_freem(mynam);
624 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
625 return (error);
626 }
627
628 /*
629 * Set protocol specific options { for now TCP only } and
630 * reserve some space. For datagram sockets, this can get called
631 * repeatedly for the same socket, but that isn't harmful.
632 */
633 if (so->so_type == SOCK_STREAM) {
634 struct sockopt sopt;
635 int val;
636
637 bzero(&sopt, sizeof sopt);
638 sopt.sopt_dir = SOPT_SET;
639 sopt.sopt_level = SOL_SOCKET;
640 sopt.sopt_name = SO_KEEPALIVE;
641 sopt.sopt_val = &val;
642 sopt.sopt_valsize = sizeof val;
643 val = 1;
644 sosetopt(so, &sopt);
645 }
646 if (so->so_proto->pr_domain->dom_family == AF_INET &&
647 so->so_proto->pr_protocol == IPPROTO_TCP) {
648 struct sockopt sopt;
649 int val;
650
651 bzero(&sopt, sizeof sopt);
652 sopt.sopt_dir = SOPT_SET;
653 sopt.sopt_level = IPPROTO_TCP;
654 sopt.sopt_name = TCP_NODELAY;
655 sopt.sopt_val = &val;
656 sopt.sopt_valsize = sizeof val;
657 val = 1;
658 sosetopt(so, &sopt);
659 }
660
661 so->so_rcv.sb_flags &= ~SB_NOINTR;
662 so->so_rcv.sb_timeo = 0;
663 so->so_snd.sb_flags &= ~SB_NOINTR;
664 so->so_snd.sb_timeo = 0;
665 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
666 if (tslp)
667 slp = tslp;
668 else {
669 MALLOC(slp, struct nfssvc_sock *, sizeof(struct nfssvc_sock),
670 M_NFSSVC, M_WAITOK);
671 bzero((caddr_t)slp, sizeof (struct nfssvc_sock));
672 TAILQ_INIT(&slp->ns_uidlruhead);
673 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
674 }
675 slp->ns_so = so;
676 slp->ns_nam = mynam;
677 slp->ns_fp = fp;
678 (void)fref(fp);
679 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
680 s = splnet();
681 so->so_upcallarg = (caddr_t)slp;
682 so->so_upcall = nfsrv_rcv;
683 so->so_rcv.sb_flags |= SB_UPCALL; /* required for freebsd merge */
684 slp->ns_nflag = SLPN_NEEDQ;
685 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
686 slp->ns_flag = SLP_VALID;
687 nfsrv_wakenfsd(slp);
688 splx(s);
689 return (0);
690 }
691
692 /*
693 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
694 * until it is killed by a signal.
695 */
696 static int
697 nfssvc_nfsd(nsd, argp, p)
698 struct nfsd_srvargs *nsd;
699 caddr_t argp;
700 struct proc *p;
701 {
702 register struct mbuf *m;
703 register int siz;
704 register struct nfssvc_sock *slp;
705 register struct socket *so;
706 struct nfsd *nfsd = nsd->nsd_nfsd;
707 struct nfsrv_descript *nd = NULL;
708 struct mbuf *mreq;
709 int error = 0, cacherep, s, sotype, writes_todo;
710 int procrastinate;
711 u_quad_t cur_usec;
712 struct timeval now;
713
714 #ifndef nolint
715 cacherep = RC_DOIT;
716 writes_todo = 0;
717 #endif
718 s = splnet();
719 if (nfsd == (struct nfsd *)0) {
720 MALLOC(nfsd, struct nfsd *, sizeof(struct nfsd), M_NFSD, M_WAITOK);
721 nsd->nsd_nfsd = nfsd;
722 bzero((caddr_t)nfsd, sizeof (struct nfsd));
723 nfsd->nfsd_procp = p;
724 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
725 nfs_numnfsd++;
726 }
727 /*
728 * Loop getting rpc requests until SIGKILL.
729 */
730 for (;;) {
731 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
732 while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
733 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
734 nfsd->nfsd_flag |= NFSD_WAITING;
735 nfsd_waiting++;
736 error = tsleep((caddr_t)nfsd, PSOCK | PCATCH,
737 "nfsd", 0);
738 nfsd_waiting--;
739 if (error)
740 goto done;
741 }
742 if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
743 (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
744 for (slp = nfssvc_sockhead.tqh_first; slp != 0;
745 slp = slp->ns_chain.tqe_next) {
746 if ((slp->ns_flag & (SLP_VALID | SLP_DOREC))
747 == (SLP_VALID | SLP_DOREC)) {
748 slp->ns_flag &= ~SLP_DOREC;
749 slp->ns_sref++;
750 nfsd->nfsd_slp = slp;
751 break;
752 }
753 }
754 if (slp == 0)
755 nfsd_head_flag &= ~NFSD_CHECKSLP;
756 }
757 if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
758 continue;
759 if (slp->ns_flag & SLP_VALID) {
760 nfs_slplock(slp, 1);
761 if (slp->ns_nflag & SLPN_DISCONN) {
762 nfsrv_zapsock(slp);
763 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
764 } else if (slp->ns_nflag & SLPN_NEEDQ) {
765 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
766 slp->ns_nflag &= ~SLPN_NEEDQ;
767 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
768 M_WAIT);
769 } else
770 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
771 error = nfsrv_dorec(slp, nfsd, &nd);
772 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
773 nfs_slpunlock(slp);
774 microuptime(&now);
775 cur_usec = (u_quad_t)now.tv_sec * 1000000 +
776 (u_quad_t)now.tv_usec;
777 if (error && slp->ns_tq.lh_first &&
778 slp->ns_tq.lh_first->nd_time <= cur_usec) {
779 error = 0;
780 cacherep = RC_DOIT;
781 writes_todo = 1;
782 } else
783 writes_todo = 0;
784 nfsd->nfsd_flag |= NFSD_REQINPROG;
785 }
786 } else {
787 error = 0;
788 slp = nfsd->nfsd_slp;
789 }
790 if (error || (slp->ns_flag & SLP_VALID) == 0) {
791 if (nd) {
792 if (nd->nd_nam2)
793 m_freem(nd->nd_nam2);
794 FREE_ZONE((caddr_t)nd,
795 sizeof *nd, M_NFSRVDESC);
796 nd = NULL;
797 }
798 nfsd->nfsd_slp = (struct nfssvc_sock *)0;
799 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
800 nfsrv_slpderef(slp);
801 continue;
802 }
803 splx(s);
804 so = slp->ns_so;
805 sotype = so->so_type;
806 if (nd) {
807 microuptime(&nd->nd_starttime);
808 if (nd->nd_nam2)
809 nd->nd_nam = nd->nd_nam2;
810 else
811 nd->nd_nam = slp->ns_nam;
812
813 /*
814 * Check to see if authorization is needed.
815 */
816 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
817 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
818 nsd->nsd_haddr = mtod(nd->nd_nam,
819 struct sockaddr_in *)->sin_addr.s_addr;
820 nsd->nsd_authlen = nfsd->nfsd_authlen;
821 nsd->nsd_verflen = nfsd->nfsd_verflen;
822 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
823 nfsd->nfsd_authlen) &&
824 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
825 nfsd->nfsd_verflen) &&
826 !copyout((caddr_t)nsd, argp, sizeof (*nsd)))
827 return (ENEEDAUTH);
828 cacherep = RC_DROPIT;
829 } else
830 cacherep = nfsrv_getcache(nd, slp, &mreq);
831
832 /*
833 * Check for just starting up for NQNFS and send
834 * fake "try again later" replies to the NQNFS clients.
835 */
836 microtime(&now);
837 if (notstarted && nqnfsstarttime <= now.tv_sec) {
838 if (modify_flag) {
839 nqnfsstarttime = now.tv_sec + nqsrv_writeslack;
840 modify_flag = 0;
841 } else
842 notstarted = 0;
843 }
844 if (notstarted) {
845 if ((nd->nd_flag & ND_NQNFS) == 0)
846 cacherep = RC_DROPIT;
847 else if (nd->nd_procnum != NFSPROC_WRITE) {
848 nd->nd_procnum = NFSPROC_NOOP;
849 nd->nd_repstat = NQNFS_TRYLATER;
850 cacherep = RC_DOIT;
851 } else
852 modify_flag = 1;
853 } else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
854 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
855 nd->nd_procnum = NFSPROC_NOOP;
856 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
857 cacherep = RC_DOIT;
858 } else if (nfs_privport) {
859 /* Check if source port is privileged */
860 u_short port;
861 struct sockaddr *nam = mtod(nd->nd_nam, struct sockaddr*);
862 struct sockaddr_in *sin;
863
864 sin = (struct sockaddr_in *)nam;
865 port = ntohs(sin->sin_port);
866 if (port >= IPPORT_RESERVED &&
867 nd->nd_procnum != NFSPROC_NULL) {
868 nd->nd_procnum = NFSPROC_NOOP;
869 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
870 cacherep = RC_DOIT;
871 printf("NFS request from unprivileged port (%s:%d)\n",
872 (char *)(inet_ntoa(sin->sin_addr)), port);
873 }
874 }
875
876 }
877
878 /*
879 * Loop to get all the write rpc relies that have been
880 * gathered together.
881 */
882 do {
883 switch (cacherep) {
884 case RC_DOIT:
885 if (nd && (nd->nd_flag & ND_NFSV3))
886 procrastinate = nfsrvw_procrastinate_v3;
887 else
888 procrastinate = nfsrvw_procrastinate;
889 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
890 procrastinate > 0 && !notstarted))
891 error = nfsrv_writegather(&nd, slp,
892 nfsd->nfsd_procp, &mreq);
893 else
894 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
895 slp, nfsd->nfsd_procp, &mreq);
896 if (mreq == NULL)
897 break;
898 if (error) {
899 if (nd->nd_procnum != NQNFSPROC_VACATED)
900 nfsstats.srv_errs++;
901 nfsrv_updatecache(nd, FALSE, mreq);
902 if (nd->nd_nam2) {
903 m_freem(nd->nd_nam2);
904 nd->nd_nam2 = NULL;
905 }
906 break;
907 }
908 nfsstats.srvrpccnt[nd->nd_procnum]++;
909 nfsrv_updatecache(nd, TRUE, mreq);
910 nd->nd_mrep = (struct mbuf *)0;
911 case RC_REPLY:
912 m = mreq;
913 siz = 0;
914 while (m) {
915 siz += m->m_len;
916 m = m->m_next;
917 }
918 if (siz <= 0 || siz > NFS_MAXPACKET) {
919 printf("mbuf siz=%d\n",siz);
920 panic("Bad nfs svc reply");
921 }
922 m = mreq;
923 m->m_pkthdr.len = siz;
924 m->m_pkthdr.rcvif = (struct ifnet *)0;
925 /*
926 * For stream protocols, prepend a Sun RPC
927 * Record Mark.
928 */
929 if (sotype == SOCK_STREAM) {
930 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
931 *mtod(m, u_long *) = htonl(0x80000000 | siz);
932 }
933 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
934 (void) nfs_slplock(slp, 1);
935 if (slp->ns_flag & SLP_VALID)
936 error = nfs_send(so, nd->nd_nam2, m, NULL);
937 else {
938 error = EPIPE;
939 m_freem(m);
940 }
941 mreq = NULL;
942 if (nfsrtton)
943 nfsd_rt(sotype, nd, cacherep);
944 if (nd->nd_nam2) {
945 MFREE(nd->nd_nam2, m);
946 nd->nd_nam2 = NULL;
947 }
948 if (nd->nd_mrep) {
949 m_freem(nd->nd_mrep);
950 nd->nd_mrep = NULL;
951 }
952 if (error == EPIPE)
953 nfsrv_zapsock(slp);
954 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
955 nfs_slpunlock(slp);
956 if (error == EINTR || error == ERESTART) {
957 FREE_ZONE((caddr_t)nd,
958 sizeof *nd, M_NFSRVDESC);
959 nfsrv_slpderef(slp);
960 s = splnet();
961 goto done;
962 }
963 break;
964 case RC_DROPIT:
965 if (nfsrtton)
966 nfsd_rt(sotype, nd, cacherep);
967 m_freem(nd->nd_mrep);
968 m_freem(nd->nd_nam2);
969 nd->nd_mrep = nd->nd_nam2 = NULL;
970 break;
971 };
972 if (nd) {
973 if (nd->nd_mrep)
974 m_freem(nd->nd_mrep);
975 if (nd->nd_nam2)
976 m_freem(nd->nd_nam2);
977 FREE_ZONE((caddr_t)nd, sizeof *nd, M_NFSRVDESC);
978 nd = NULL;
979 }
980
981 /*
982 * Check to see if there are outstanding writes that
983 * need to be serviced.
984 */
985 microuptime(&now);
986 cur_usec = (u_quad_t)now.tv_sec * 1000000 +
987 (u_quad_t)now.tv_usec;
988 s = splsoftclock();
989 if (slp->ns_tq.lh_first &&
990 slp->ns_tq.lh_first->nd_time <= cur_usec) {
991 cacherep = RC_DOIT;
992 writes_todo = 1;
993 } else
994 writes_todo = 0;
995 splx(s);
996 } while (writes_todo);
997 s = splnet();
998 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
999 if (nfsrv_dorec(slp, nfsd, &nd)) {
1000 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1001 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
1002 nfsd->nfsd_slp = NULL;
1003 nfsrv_slpderef(slp);
1004 } else
1005 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1006 }
1007 done:
1008 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
1009 splx(s);
1010 _FREE((caddr_t)nfsd, M_NFSD);
1011 nsd->nsd_nfsd = (struct nfsd *)0;
1012 if (--nfs_numnfsd == 0)
1013 nfsrv_init(TRUE); /* Reinitialize everything */
1014 return (error);
1015 }
1016 #endif /* NFS_NOSERVER */
1017
1018 int nfs_defect = 0;
1019 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
1020 #ifdef notyet
1021 SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, "");
1022 #endif
1023
1024 #ifndef _SYS_SYSPROTO_H_
1025 struct nfsclnt_args {
1026 int flag;
1027 caddr_t argp;
1028 };
1029 #endif
1030 int
1031 nfsclnt(struct proc *p, struct nfsclnt_args *uap)
1032 {
1033 struct lockd_ans la;
1034 int error;
1035
1036 if (uap->flag == NFSCLNT_LOCKDWAIT) {
1037 return (nfslockdwait(p));
1038 }
1039 if (uap->flag == NFSCLNT_LOCKDANS) {
1040 error = copyin(uap->argp, &la, sizeof(la));
1041 return (error != 0 ? error : nfslockdans(p, &la));
1042 }
1043 if (uap->flag == NFSCLNT_LOCKDFD)
1044 return (nfslockdfd(p, (int)uap->argp));
1045 return EINVAL;
1046 }
1047
1048
1049 static int nfssvc_iod_continue(int);
1050
1051 /*
1052 * Asynchronous I/O daemons for client nfs.
1053 * They do read-ahead and write-behind operations on the block I/O cache.
1054 * Never returns unless it fails or gets killed.
1055 */
1056 static int
1057 nfssvc_iod(p)
1058 struct proc *p;
1059 {
1060 register int i, myiod;
1061 struct nfsmount *nmp;
1062 int error = 0;
1063 struct uthread *ut;
1064
1065 /*
1066 * Assign my position or return error if too many already running
1067 */
1068 myiod = -1;
1069 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
1070 if (nfs_asyncdaemon[i] == 0) {
1071 nfs_asyncdaemon[i]++;
1072 myiod = i;
1073 break;
1074 }
1075 if (myiod == -1)
1076 return (EBUSY);
1077 nfs_numasync++;
1078
1079 /* stuff myiod into uthread to get off local stack for continuation */
1080
1081 ut = (struct uthread *)get_bsdthread_info(current_act());
1082 ut->uu_state.uu_nfs_myiod = myiod; /* squirrel away for continuation */
1083
1084 nfssvc_iod_continue(0);
1085 /* NOTREACHED */
1086
1087 }
1088
1089 /*
1090 * Continuation for Asynchronous I/O daemons for client nfs.
1091 */
1092 static int
1093 nfssvc_iod_continue(error)
1094 {
1095 register struct nfsbuf *bp;
1096 register int i, myiod;
1097 struct nfsmount *nmp;
1098 struct uthread *ut;
1099 struct proc *p;
1100
1101 /*
1102 * real myiod is stored in uthread, recover it
1103 */
1104 ut = (struct uthread *)get_bsdthread_info(current_act());
1105 myiod = ut->uu_state.uu_nfs_myiod;
1106 p = current_proc();
1107
1108 /*
1109 * Just loop around doin our stuff until SIGKILL
1110 * - actually we don't loop with continuations...
1111 */
1112 for (;;) {
1113 while (((nmp = nfs_iodmount[myiod]) == NULL
1114 || nmp->nm_bufq.tqh_first == NULL)
1115 && error == 0 && nfs_ioddelwri == 0) {
1116 if (nmp)
1117 nmp->nm_bufqiods--;
1118 nfs_iodwant[myiod] = p;
1119 nfs_iodmount[myiod] = NULL;
1120 error = tsleep0((caddr_t)&nfs_iodwant[myiod],
1121 PWAIT | PCATCH, "nfsidl", 0, nfssvc_iod_continue);
1122 /* NOTREACHED */
1123 }
1124 if (error) {
1125 nfs_asyncdaemon[myiod] = 0;
1126 if (nmp) nmp->nm_bufqiods--;
1127 nfs_iodwant[myiod] = NULL;
1128 nfs_iodmount[myiod] = NULL;
1129 nfs_numasync--;
1130 if (error == EINTR || error == ERESTART)
1131 error = 0;
1132 unix_syscall_return(error);
1133 }
1134 if (nmp != NULL) {
1135 while ((bp = nmp->nm_bufq.tqh_first) != NULL) {
1136 /* Take one off the front of the list */
1137 TAILQ_REMOVE(&nmp->nm_bufq, bp, nb_free);
1138 bp->nb_free.tqe_next = NFSNOLIST;
1139 nmp->nm_bufqlen--;
1140 if (nmp->nm_bufqwant && nmp->nm_bufqlen < 2 * nfs_numasync) {
1141 nmp->nm_bufqwant = FALSE;
1142 wakeup(&nmp->nm_bufq);
1143 }
1144 if (ISSET(bp->nb_flags, NB_READ))
1145 (void) nfs_doio(bp, bp->nb_rcred, (struct proc *)0);
1146 else
1147 (void) nfs_doio(bp, bp->nb_wcred, (struct proc *)0);
1148
1149 /*
1150 * If there are more than one iod on this mount, then defect
1151 * so that the iods can be shared out fairly between the mounts
1152 */
1153 if (nfs_defect && nmp->nm_bufqiods > 1) {
1154 NFS_DPF(ASYNCIO,
1155 ("nfssvc_iod: iod %d defecting from mount %p\n",
1156 myiod, nmp));
1157 nfs_iodmount[myiod] = NULL;
1158 nmp->nm_bufqiods--;
1159 break;
1160 }
1161 }
1162 }
1163 if (nfs_ioddelwri) {
1164 i = 0;
1165 nfs_ioddelwri = 0;
1166 while (i < 8 && (bp = TAILQ_FIRST(&nfsbufdelwri)) != NULL) {
1167 struct nfsnode *np = VTONFS(bp->nb_vp);
1168 nfs_buf_remfree(bp);
1169 if (ISSET(bp->nb_flags, NB_NEEDCOMMIT)) {
1170 /* put buffer at end of delwri list */
1171 TAILQ_INSERT_TAIL(&nfsbufdelwri, bp, nb_free);
1172 nfsbufdelwricnt++;
1173 nfs_flushcommits(np->n_vnode, (struct proc *)0);
1174 } else {
1175 SET(bp->nb_flags, (NB_BUSY | NB_ASYNC | NB_IOD));
1176 nfs_buf_write(bp);
1177 }
1178 i++;
1179 }
1180 }
1181 }
1182 }
1183
1184 /*
1185 * Shut down a socket associated with an nfssvc_sock structure.
1186 * Should be called with the send lock set, if required.
1187 * The trick here is to increment the sref at the start, so that the nfsds
1188 * will stop using it and clear ns_flag at the end so that it will not be
1189 * reassigned during cleanup.
1190 */
1191 static void
1192 nfsrv_zapsock(slp)
1193 register struct nfssvc_sock *slp;
1194 {
1195 register struct nfsuid *nuidp, *nnuidp;
1196 register struct nfsrv_descript *nwp, *nnwp;
1197 struct socket *so;
1198 struct file *fp;
1199 struct mbuf *m;
1200 int s;
1201
1202 slp->ns_flag &= ~SLP_ALLFLAGS;
1203 slp->ns_nflag &= ~SLP_ALLFLAGS;
1204 fp = slp->ns_fp;
1205 if (fp) {
1206 slp->ns_fp = (struct file *)0;
1207 so = slp->ns_so;
1208 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1209 so->so_upcall = NULL;
1210 so->so_rcv.sb_flags &= ~SB_UPCALL;
1211 soshutdown(so, 2);
1212 if (slp->ns_nam)
1213 MFREE(slp->ns_nam, m);
1214 m_freem(slp->ns_raw);
1215 m_freem(slp->ns_rec);
1216 slp->ns_nam = slp->ns_raw = slp->ns_rec = NULL;
1217 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1218 closef(fp, (struct proc *)0);
1219 for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0;
1220 nuidp = nnuidp) {
1221 nnuidp = nuidp->nu_lru.tqe_next;
1222 LIST_REMOVE(nuidp, nu_hash);
1223 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
1224 if (nuidp->nu_flag & NU_NAM)
1225 m_freem(nuidp->nu_nam);
1226 FREE_ZONE((caddr_t)nuidp,
1227 sizeof (struct nfsuid), M_NFSUID);
1228 }
1229 s = splsoftclock();
1230 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
1231 nnwp = nwp->nd_tq.le_next;
1232 LIST_REMOVE(nwp, nd_tq);
1233 FREE_ZONE((caddr_t)nwp, sizeof *nwp, M_NFSRVDESC);
1234 }
1235 LIST_INIT(&slp->ns_tq);
1236 splx(s);
1237 }
1238 }
1239
1240 /*
1241 * Get an authorization string for the uid by having the mount_nfs sitting
1242 * on this mount point porpous out of the kernel and do it.
1243 */
1244 int
1245 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
1246 register struct nfsmount *nmp;
1247 struct nfsreq *rep;
1248 struct ucred *cred;
1249 char **auth_str;
1250 int *auth_len;
1251 char *verf_str;
1252 int *verf_len;
1253 NFSKERBKEY_T key; /* return session key */
1254 {
1255 int error = 0;
1256
1257 while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) {
1258 nmp->nm_state |= NFSSTA_WANTAUTH;
1259 (void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
1260 "nfsauth1", 2 * hz);
1261 error = nfs_sigintr(nmp, rep, rep->r_procp);
1262 if (error) {
1263 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1264 return (error);
1265 }
1266 }
1267 nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH);
1268 MALLOC(*auth_str, char *, RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1269 nmp->nm_authstr = *auth_str;
1270 nmp->nm_authlen = RPCAUTH_MAXSIZ;
1271 nmp->nm_verfstr = verf_str;
1272 nmp->nm_verflen = *verf_len;
1273 nmp->nm_authuid = cred->cr_uid;
1274 wakeup((caddr_t)&nmp->nm_authstr);
1275
1276 /*
1277 * And wait for mount_nfs to do its stuff.
1278 */
1279 while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) {
1280 (void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
1281 "nfsauth2", 2 * hz);
1282 error = nfs_sigintr(nmp, rep, rep->r_procp);
1283 }
1284 if (nmp->nm_state & NFSSTA_AUTHERR) {
1285 nmp->nm_state &= ~NFSSTA_AUTHERR;
1286 error = EAUTH;
1287 }
1288 if (error)
1289 _FREE((caddr_t)*auth_str, M_TEMP);
1290 else {
1291 *auth_len = nmp->nm_authlen;
1292 *verf_len = nmp->nm_verflen;
1293 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
1294 }
1295 nmp->nm_state &= ~NFSSTA_HASAUTH;
1296 nmp->nm_state |= NFSSTA_WAITAUTH;
1297 if (nmp->nm_state & NFSSTA_WANTAUTH) {
1298 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1299 wakeup((caddr_t)&nmp->nm_authtype);
1300 }
1301 return (error);
1302 }
1303
1304 /*
1305 * Get a nickname authenticator and verifier.
1306 */
1307 int
1308 nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len)
1309 struct nfsmount *nmp;
1310 struct ucred *cred;
1311 char **auth_str;
1312 int *auth_len;
1313 char *verf_str;
1314 int verf_len;
1315 {
1316 register struct nfsuid *nuidp;
1317 register u_long *nickp, *verfp;
1318 struct timeval ktvin, ktvout, now;
1319
1320 #if DIAGNOSTIC
1321 if (verf_len < (4 * NFSX_UNSIGNED))
1322 panic("nfs_getnickauth verf too small");
1323 #endif
1324 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
1325 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1326 if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1327 break;
1328 }
1329 microtime(&now);
1330 if (!nuidp || nuidp->nu_expire < now.tv_sec)
1331 return (EACCES);
1332
1333 /*
1334 * Move to the end of the lru list (end of lru == most recently used).
1335 */
1336 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1337 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1338
1339 MALLOC(nickp, u_long *, 2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1340 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1341 *nickp = txdr_unsigned(nuidp->nu_nickname);
1342 *auth_str = (char *)nickp;
1343 *auth_len = 2 * NFSX_UNSIGNED;
1344
1345 /*
1346 * Now we must encrypt the verifier and package it up.
1347 */
1348 verfp = (u_long *)verf_str;
1349 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1350 microtime(&now);
1351 if (now.tv_sec > nuidp->nu_timestamp.tv_sec ||
1352 (now.tv_sec == nuidp->nu_timestamp.tv_sec &&
1353 now.tv_usec > nuidp->nu_timestamp.tv_usec))
1354 nuidp->nu_timestamp = now;
1355 else
1356 nuidp->nu_timestamp.tv_usec++;
1357 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1358 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1359
1360 /*
1361 * Now encrypt the timestamp verifier in ecb mode using the session
1362 * key.
1363 */
1364 #if NFSKERB
1365 XXX
1366 #endif
1367
1368 *verfp++ = ktvout.tv_sec;
1369 *verfp++ = ktvout.tv_usec;
1370 *verfp = 0;
1371 return (0);
1372 }
1373
1374 /*
1375 * Save the current nickname in a hash list entry on the mount point.
1376 */
1377 int
1378 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
1379 register struct nfsmount *nmp;
1380 struct ucred *cred;
1381 int len;
1382 NFSKERBKEY_T key;
1383 struct mbuf **mdp;
1384 char **dposp;
1385 struct mbuf *mrep;
1386 {
1387 register struct nfsuid *nuidp;
1388 register u_long *tl;
1389 register long t1;
1390 struct mbuf *md = *mdp;
1391 struct timeval ktvin, ktvout, now;
1392 u_long nick;
1393 char *dpos = *dposp, *cp2;
1394 int deltasec, error = 0;
1395
1396 if (len == (3 * NFSX_UNSIGNED)) {
1397 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
1398 ktvin.tv_sec = *tl++;
1399 ktvin.tv_usec = *tl++;
1400 nick = fxdr_unsigned(u_long, *tl);
1401
1402 /*
1403 * Decrypt the timestamp in ecb mode.
1404 */
1405 #if NFSKERB
1406 XXX
1407 #endif
1408 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1409 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1410 microtime(&now);
1411 deltasec = now.tv_sec - ktvout.tv_sec;
1412 if (deltasec < 0)
1413 deltasec = -deltasec;
1414 /*
1415 * If ok, add it to the hash list for the mount point.
1416 */
1417 if (deltasec <= NFS_KERBCLOCKSKEW) {
1418 if (nmp->nm_numuids < nuidhash_max) {
1419 nmp->nm_numuids++;
1420 MALLOC_ZONE(nuidp, struct nfsuid *,
1421 sizeof (struct nfsuid),
1422 M_NFSUID, M_WAITOK);
1423 } else {
1424 nuidp = nmp->nm_uidlruhead.tqh_first;
1425 LIST_REMOVE(nuidp, nu_hash);
1426 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1427 nu_lru);
1428 }
1429 nuidp->nu_flag = 0;
1430 nuidp->nu_cr.cr_uid = cred->cr_uid;
1431 nuidp->nu_expire = now.tv_sec + NFS_KERBTTL;
1432 nuidp->nu_timestamp = ktvout;
1433 nuidp->nu_nickname = nick;
1434 bcopy(key, nuidp->nu_key, sizeof (key));
1435 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1436 nu_lru);
1437 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1438 nuidp, nu_hash);
1439 }
1440 } else
1441 nfsm_adv(nfsm_rndup(len));
1442 nfsmout:
1443 *mdp = md;
1444 *dposp = dpos;
1445 return (error);
1446 }
1447
1448 #ifndef NFS_NOSERVER
1449
1450 /*
1451 * Derefence a server socket structure. If it has no more references and
1452 * is no longer valid, you can throw it away.
1453 */
1454 void
1455 nfsrv_slpderef(slp)
1456 register struct nfssvc_sock *slp;
1457 {
1458 if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
1459 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
1460 _FREE((caddr_t)slp, M_NFSSVC);
1461 }
1462 }
1463
1464 /*
1465 * Lock a socket against others.
1466 */
1467 int
1468 nfs_slplock(slp, wait)
1469 register struct nfssvc_sock *slp;
1470 int wait;
1471 {
1472 int *statep = &slp->ns_solock;
1473
1474 if (!wait && (*statep & NFSSTA_SNDLOCK))
1475 return(0); /* already locked, fail */
1476 while (*statep & NFSSTA_SNDLOCK) {
1477 *statep |= NFSSTA_WANTSND;
1478 (void) tsleep((caddr_t)statep, PZERO - 1, "nfsslplck", 0);
1479 }
1480 *statep |= NFSSTA_SNDLOCK;
1481 return (1);
1482 }
1483
1484 /*
1485 * Unlock the stream socket for others.
1486 */
1487 void
1488 nfs_slpunlock(slp)
1489 struct nfssvc_sock *slp;
1490 {
1491 int *statep = &slp->ns_solock;
1492
1493 if ((*statep & NFSSTA_SNDLOCK) == 0)
1494 panic("nfs slpunlock");
1495 *statep &= ~NFSSTA_SNDLOCK;
1496 if (*statep & NFSSTA_WANTSND) {
1497 *statep &= ~NFSSTA_WANTSND;
1498 wakeup((caddr_t)statep);
1499 }
1500 }
1501
1502 /*
1503 * Initialize the data structures for the server.
1504 * Handshake with any new nfsds starting up to avoid any chance of
1505 * corruption.
1506 */
1507 void
1508 nfsrv_init(terminating)
1509 int terminating;
1510 {
1511 register struct nfssvc_sock *slp, *nslp;
1512
1513 if (nfssvc_sockhead_flag & SLP_INIT)
1514 panic("nfsd init");
1515 nfssvc_sockhead_flag |= SLP_INIT;
1516 if (terminating) {
1517 for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) {
1518 nslp = slp->ns_chain.tqe_next;
1519 if (slp->ns_flag & SLP_VALID)
1520 nfsrv_zapsock(slp);
1521 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
1522 _FREE((caddr_t)slp, M_NFSSVC);
1523 }
1524 nfsrv_cleancache(); /* And clear out server cache */
1525 /* XXX CSM 12/4/97 Revisit when enabling WebNFS */
1526 #ifdef notyet
1527 } else
1528 nfs_pub.np_valid = 0;
1529 #else
1530 }
1531 #endif
1532
1533 TAILQ_INIT(&nfssvc_sockhead);
1534 nfssvc_sockhead_flag &= ~SLP_INIT;
1535 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
1536 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
1537 wakeup((caddr_t)&nfssvc_sockhead);
1538 }
1539
1540 TAILQ_INIT(&nfsd_head);
1541 nfsd_head_flag &= ~NFSD_CHECKSLP;
1542
1543 MALLOC(nfs_udpsock, struct nfssvc_sock *, sizeof(struct nfssvc_sock),
1544 M_NFSSVC, M_WAITOK);
1545 bzero((caddr_t)nfs_udpsock, sizeof (struct nfssvc_sock));
1546 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
1547 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
1548
1549 MALLOC(nfs_cltpsock, struct nfssvc_sock *, sizeof(struct nfssvc_sock),
1550 M_NFSSVC, M_WAITOK);
1551 bzero((caddr_t)nfs_cltpsock, sizeof (struct nfssvc_sock));
1552 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
1553 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
1554 }
1555
1556 /*
1557 * Add entries to the server monitor log.
1558 */
1559 static void
1560 nfsd_rt(sotype, nd, cacherep)
1561 int sotype;
1562 register struct nfsrv_descript *nd;
1563 int cacherep;
1564 {
1565 register struct drt *rt;
1566 struct timeval now;
1567
1568 rt = &nfsdrt.drt[nfsdrt.pos];
1569 if (cacherep == RC_DOIT)
1570 rt->flag = 0;
1571 else if (cacherep == RC_REPLY)
1572 rt->flag = DRT_CACHEREPLY;
1573 else
1574 rt->flag = DRT_CACHEDROP;
1575 if (sotype == SOCK_STREAM)
1576 rt->flag |= DRT_TCP;
1577 if (nd->nd_flag & ND_NQNFS)
1578 rt->flag |= DRT_NQNFS;
1579 else if (nd->nd_flag & ND_NFSV3)
1580 rt->flag |= DRT_NFSV3;
1581 rt->proc = nd->nd_procnum;
1582 if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
1583 rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
1584 else
1585 rt->ipadr = INADDR_ANY;
1586 microuptime(&now);
1587 rt->resptime = ((now.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
1588 (now.tv_usec - nd->nd_starttime.tv_usec);
1589 microtime(&rt->tstamp); // XXX unused
1590 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
1591 }
1592 #endif /* NFS_NOSERVER */