2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95
59 * FreeBSD-Id: nfs_syscalls.c,v 1.32 1997/11/07 08:53:25 phk Exp $
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 /* XXX CSM 11/25/97 FreeBSD's generated syscall prototypes */
66 #include <sys/sysproto.h>
68 #include <sys/kernel.h>
69 #include <sys/file_internal.h>
70 #include <sys/filedesc.h>
72 #include <sys/vnode_internal.h>
73 #include <sys/mount_internal.h>
74 #include <sys/proc_internal.h> /* for fdflags */
75 #include <sys/kauth.h>
76 #include <sys/sysctl.h>
79 #include <sys/malloc.h>
80 #include <sys/kpi_mbuf.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/domain.h>
84 #include <sys/protosw.h>
85 #include <sys/fcntl.h>
86 #include <sys/lockf.h>
87 #include <sys/syslog.h>
89 #include <sys/sysproto.h>
90 #include <sys/kpi_socket.h>
91 #include <libkern/OSAtomic.h>
93 #include <bsm/audit_kernel.h>
95 #include <netinet/in.h>
96 #include <netinet/tcp.h>
98 #include <netiso/iso.h>
100 #include <nfs/xdr_subs.h>
101 #include <nfs/rpcv2.h>
102 #include <nfs/nfsproto.h>
104 #include <nfs/nfsm_subs.h>
105 #include <nfs/nfsrvcache.h>
106 #include <nfs/nfsmount.h>
107 #include <nfs/nfsnode.h>
108 #include <nfs/nfsrtt.h>
109 #include <nfs/nfs_lock.h>
111 extern void unix_syscall_return(int);
114 extern int (*nfsrv3_procs
[NFS_NPROCS
])(struct nfsrv_descript
*nd
,
115 struct nfssvc_sock
*slp
,
118 extern int nfs_numasync
;
119 extern int nfs_ioddelwri
;
121 extern struct nfsstats nfsstats
;
122 extern int nfsrvw_procrastinate
;
123 extern int nfsrvw_procrastinate_v3
;
125 struct nfssvc_sock
*nfs_udpsock
, *nfs_cltpsock
;
126 static int nuidhash_max
= NFS_MAXUIDHASH
;
128 static void nfsrv_zapsock(struct nfssvc_sock
*slp
);
129 static int nfssvc_iod(proc_t
);
130 static int nfskerb_clientd(struct nfsmount
*, struct nfsd_cargs
*, int, user_addr_t
, proc_t
);
132 static int nfs_asyncdaemon
[NFS_MAXASYNCDAEMON
];
135 int nfsd_waiting
= 0;
136 static struct nfsdrt nfsdrt
;
138 static void nfsd_rt(int sotype
, struct nfsrv_descript
*nd
, int cacherep
);
139 static int nfssvc_addsock(socket_t
, mbuf_t
, proc_t
);
140 static int nfssvc_nfsd(struct nfsd_srvargs
*,user_addr_t
, proc_t
);
141 static int nfssvc_export(user_addr_t
, proc_t
);
143 static int nfs_privport
= 0;
144 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
146 SYSCTL_INT(_vfs_nfs
, NFS_NFSPRIVPORT
, nfs_privport
, CTLFLAG_RW
, &nfs_privport
, 0, "");
147 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, gatherdelay
, CTLFLAG_RW
, &nfsrvw_procrastinate
, 0, "");
148 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, gatherdelay_v3
, CTLFLAG_RW
, &nfsrvw_procrastinate_v3
, 0, "");
152 * NFS server system calls
153 * getfh() lives here too, but maybe should move to kern/vfs_syscalls.c
157 * Get file handle system call
160 getfh(proc_t p
, struct getfh_args
*uap
, __unused
int *retval
)
163 struct nfs_filehandle nfh
;
166 struct vfs_context context
;
167 char path
[MAXPATHLEN
], *ptr
;
169 struct nfs_exportfs
*nxfs
;
170 struct nfs_export
*nx
;
173 context
.vc_ucred
= kauth_cred_get();
178 error
= proc_suser(p
);
182 error
= copyinstr(uap
->fname
, path
, MAXPATHLEN
, (size_t *)&pathlen
);
186 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
187 UIO_SYSSPACE
, path
, &context
);
195 // find exportfs that matches f_mntonname
196 lck_rw_lock_shared(&nfs_export_rwlock
);
197 ptr
= vnode_mount(vp
)->mnt_vfsstat
.f_mntonname
;
198 LIST_FOREACH(nxfs
, &nfs_exports
, nxfs_next
) {
199 if (!strcmp(nxfs
->nxfs_path
, ptr
))
202 if (!nxfs
|| strncmp(nxfs
->nxfs_path
, path
, strlen(nxfs
->nxfs_path
))) {
206 // find export that best matches remainder of path
207 ptr
= path
+ strlen(nxfs
->nxfs_path
);
208 while (*ptr
&& (*ptr
== '/'))
210 LIST_FOREACH(nx
, &nxfs
->nxfs_exports
, nx_next
) {
211 int len
= strlen(nx
->nx_path
);
212 if (len
== 0) // we've hit the export entry for the root directory
214 if (!strncmp(nx
->nx_path
, ptr
, len
))
222 bzero(&nfh
, sizeof(nfh
));
223 nfh
.nfh_xh
.nxh_version
= NFS_FH_VERSION
;
224 nfh
.nfh_xh
.nxh_fsid
= nxfs
->nxfs_id
;
225 nfh
.nfh_xh
.nxh_expid
= nx
->nx_id
;
226 nfh
.nfh_xh
.nxh_flags
= 0;
227 nfh
.nfh_xh
.nxh_reserved
= 0;
228 nfh
.nfh_len
= NFS_MAX_FID_SIZE
;
229 error
= VFS_VPTOFH(vp
, &nfh
.nfh_len
, &nfh
.nfh_fid
[0], NULL
);
230 if (nfh
.nfh_len
> (int)NFS_MAX_FID_SIZE
)
232 nfh
.nfh_xh
.nxh_fidlen
= nfh
.nfh_len
;
233 nfh
.nfh_len
+= sizeof(nfh
.nfh_xh
);
236 lck_rw_done(&nfs_export_rwlock
);
240 error
= copyout((caddr_t
)&nfh
, uap
->fhp
, sizeof(nfh
));
244 #endif /* NFS_NOSERVER */
246 extern struct fileops vnops
;
249 * syscall for the rpc.lockd to use to translate a NFS file handle into
250 * an open descriptor.
252 * warning: do not remove the suser() call or this becomes one giant
257 struct fhopen_args
*uap
,
261 struct nfs_filehandle nfh
;
262 struct nfs_export
*nx
;
263 struct nfs_export_options
*nxo
;
265 struct fileproc
*fp
, *nfp
;
266 int fmode
, error
, type
;
268 kauth_cred_t cred
= proc_ucred(p
);
269 struct vfs_context context
;
270 kauth_action_t action
;
273 context
.vc_ucred
= cred
;
278 error
= suser(cred
, 0);
282 fmode
= FFLAGS(uap
->flags
);
283 /* why not allow a non-read/write open for our lockd? */
284 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
287 error
= copyin(uap
->u_fhp
, &nfh
.nfh_len
, sizeof(nfh
.nfh_len
));
290 if ((nfh
.nfh_len
< (int)sizeof(struct nfs_exphandle
)) ||
291 (nfh
.nfh_len
> (int)NFS_MAX_FH_SIZE
))
293 error
= copyin(uap
->u_fhp
, &nfh
, sizeof(nfh
.nfh_len
) + nfh
.nfh_len
);
297 lck_rw_lock_shared(&nfs_export_rwlock
);
298 /* now give me my vnode, it gets returned to me with a reference */
299 error
= nfsrv_fhtovp(&nfh
, NULL
, TRUE
, &vp
, &nx
, &nxo
);
300 lck_rw_done(&nfs_export_rwlock
);
305 * From now on we have to make sure not
306 * to forget about the vnode.
307 * Any error that causes an abort must vnode_put(vp).
308 * Just set error = err and 'goto bad;'.
314 if (vnode_vtype(vp
) == VSOCK
) {
319 /* disallow write operations on directories */
320 if (vnode_isdir(vp
) && (fmode
& (FWRITE
| O_TRUNC
))) {
325 /* compute action to be authorized */
328 action
|= KAUTH_VNODE_READ_DATA
;
329 if (fmode
& (FWRITE
| O_TRUNC
))
330 action
|= KAUTH_VNODE_WRITE_DATA
;
331 if ((error
= vnode_authorize(vp
, NULL
, action
, &context
)) != 0)
334 if ((error
= VNOP_OPEN(vp
, fmode
, &context
)))
336 if ((error
= vnode_ref_ext(vp
, fmode
)))
340 * end of vn_open code
343 // starting here... error paths should call vn_close/vnode_put
344 if ((error
= falloc(p
, &nfp
, &indx
)) != 0) {
345 vn_close(vp
, fmode
& FMASK
, cred
, p
);
350 fp
->f_fglob
->fg_flag
= fmode
& FMASK
;
351 fp
->f_fglob
->fg_type
= DTYPE_VNODE
;
352 fp
->f_fglob
->fg_ops
= &vnops
;
353 fp
->f_fglob
->fg_data
= (caddr_t
)vp
;
355 // XXX do we really need to support this with fhopen()?
356 if (fmode
& (O_EXLOCK
| O_SHLOCK
)) {
357 lf
.l_whence
= SEEK_SET
;
360 if (fmode
& O_EXLOCK
)
365 if ((fmode
& FNONBLOCK
) == 0)
367 if ((error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, type
, &context
))) {
368 vn_close(vp
, fp
->f_fglob
->fg_flag
, fp
->f_fglob
->fg_cred
, p
);
369 fp_free(p
, indx
, fp
);
372 fp
->f_fglob
->fg_flag
|= FHASLOCK
;
378 *fdflags(p
, indx
) &= ~UF_RESERVED
;
379 fp_drop(p
, indx
, fp
, 1);
391 * Nfs server psuedo system call for the nfsd's
392 * Based on the flag value it either:
393 * - adds a socket to the selection list
394 * - remains in the kernel as an nfsd
395 * - remains in the kernel as an nfsiod
398 nfssvc(proc_t p
, struct nfssvc_args
*uap
, __unused
int *retval
)
403 struct user_nfsd_args user_nfsdarg
;
404 struct nfsd_srvargs nfsd_srvargs
, *nsd
= &nfsd_srvargs
;
405 struct nfsd_cargs ncd
;
407 struct nfssvc_sock
*slp
;
408 struct nfsuid
*nuidp
;
409 struct nfsmount
*nmp
;
412 struct vfs_context context
;
413 struct ucred temp_cred
;
414 #endif /* NFS_NOSERVER */
417 AUDIT_ARG(cmd
, uap
->flag
);
422 error
= proc_suser(p
);
425 if (uap
->flag
& NFSSVC_BIOD
)
426 error
= nfssvc_iod(p
);
430 #else /* !NFS_NOSERVER */
431 else if (uap
->flag
& NFSSVC_MNTD
) {
434 context
.vc_ucred
= kauth_cred_get();
436 error
= copyin(uap
->argp
, (caddr_t
)&ncd
, sizeof (ncd
));
440 NDINIT(&nd
, LOOKUP
, FOLLOW
| LOCKLEAF
| AUDITVNPATH1
,
441 (proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
442 CAST_USER_ADDR_T(ncd
.ncd_dirp
), &context
);
448 if (vnode_isvroot(nd
.ni_vp
) == 0)
450 nmp
= VFSTONFS(vnode_mount(nd
.ni_vp
));
455 if ((nmp
->nm_state
& NFSSTA_MNTD
) &&
456 (uap
->flag
& NFSSVC_GOTAUTH
) == 0)
458 nmp
->nm_state
|= NFSSTA_MNTD
;
459 error
= nfskerb_clientd(nmp
, &ncd
, uap
->flag
, uap
->argp
, p
);
460 } else if (uap
->flag
& NFSSVC_ADDSOCK
) {
461 if (IS_64BIT_PROCESS(p
)) {
462 error
= copyin(uap
->argp
, (caddr_t
)&user_nfsdarg
, sizeof(user_nfsdarg
));
464 struct nfsd_args tmp_args
;
465 error
= copyin(uap
->argp
, (caddr_t
)&tmp_args
, sizeof(tmp_args
));
467 user_nfsdarg
.sock
= tmp_args
.sock
;
468 user_nfsdarg
.name
= CAST_USER_ADDR_T(tmp_args
.name
);
469 user_nfsdarg
.namelen
= tmp_args
.namelen
;
475 error
= file_socket(user_nfsdarg
.sock
, &so
);
478 /* Get the client address for connected sockets. */
479 if (user_nfsdarg
.name
== USER_ADDR_NULL
|| user_nfsdarg
.namelen
== 0) {
482 error
= sockargs(&nam
, user_nfsdarg
.name
, user_nfsdarg
.namelen
, MBUF_TYPE_SONAME
);
484 /* drop the iocount file_socket() grabbed on the file descriptor */
485 file_drop(user_nfsdarg
.sock
);
490 * nfssvc_addsock() will grab a retain count on the socket
491 * to keep the socket from being closed when nfsd closes its
492 * file descriptor for it.
494 error
= nfssvc_addsock(so
, nam
, p
);
495 /* drop the iocount file_socket() grabbed on the file descriptor */
496 file_drop(user_nfsdarg
.sock
);
497 } else if (uap
->flag
& NFSSVC_NFSD
) {
498 error
= copyin(uap
->argp
, (caddr_t
)nsd
, sizeof (*nsd
));
502 if ((uap
->flag
& NFSSVC_AUTHIN
) && ((nfsd
= nsd
->nsd_nfsd
)) &&
503 (nfsd
->nfsd_slp
->ns_flag
& SLP_VALID
)) {
504 slp
= nfsd
->nfsd_slp
;
507 * First check to see if another nfsd has already
508 * added this credential.
510 for (nuidp
= NUIDHASH(slp
,nsd
->nsd_cr
.cr_uid
)->lh_first
;
511 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
512 if (kauth_cred_getuid(nuidp
->nu_cr
) == nsd
->nsd_cr
.cr_uid
&&
513 (!nfsd
->nfsd_nd
->nd_nam2
||
514 netaddr_match(NU_NETFAM(nuidp
),
515 &nuidp
->nu_haddr
, nfsd
->nfsd_nd
->nd_nam2
)))
519 nfsrv_setcred(nuidp
->nu_cr
,nfsd
->nfsd_nd
->nd_cr
);
520 nfsd
->nfsd_nd
->nd_flag
|= ND_KERBFULL
;
525 if (slp
->ns_numuids
< nuidhash_max
) {
527 nuidp
= (struct nfsuid
*)
528 _MALLOC_ZONE(sizeof (struct nfsuid
),
531 nuidp
= (struct nfsuid
*)0;
532 if ((slp
->ns_flag
& SLP_VALID
) == 0) {
534 FREE_ZONE((caddr_t
)nuidp
,
535 sizeof (struct nfsuid
), M_NFSUID
);
539 if (nuidp
== (struct nfsuid
*)0) {
540 nuidp
= slp
->ns_uidlruhead
.tqh_first
;
543 LIST_REMOVE(nuidp
, nu_hash
);
544 TAILQ_REMOVE(&slp
->ns_uidlruhead
, nuidp
,
546 if (nuidp
->nu_flag
& NU_NAM
)
547 mbuf_freem(nuidp
->nu_nam
);
548 kauth_cred_rele(nuidp
->nu_cr
);
552 if (nsd
->nsd_cr
.cr_ngroups
> NGROUPS
)
553 nsd
->nsd_cr
.cr_ngroups
= NGROUPS
;
555 nfsrv_setcred(&nsd
->nsd_cr
, &temp_cred
);
556 nuidp
->nu_cr
= kauth_cred_create(&temp_cred
);
559 FREE_ZONE(nuidp
, sizeof(struct nfsuid
), M_NFSUID
);
563 nuidp
->nu_timestamp
= nsd
->nsd_timestamp
;
565 nuidp
->nu_expire
= now
.tv_sec
+ nsd
->nsd_ttl
;
567 * and save the session key in nu_key.
569 bcopy(nsd
->nsd_key
, nuidp
->nu_key
,
570 sizeof (nsd
->nsd_key
));
571 if (nfsd
->nfsd_nd
->nd_nam2
) {
572 struct sockaddr_in
*saddr
;
574 saddr
= mbuf_data(nfsd
->nfsd_nd
->nd_nam2
);
575 switch (saddr
->sin_family
) {
577 nuidp
->nu_flag
|= NU_INETADDR
;
579 saddr
->sin_addr
.s_addr
;
583 nuidp
->nu_flag
|= NU_NAM
;
584 error
= mbuf_copym(nfsd
->nfsd_nd
->nd_nam2
, 0,
585 MBUF_COPYALL
, MBUF_WAITOK
,
588 kauth_cred_rele(nuidp
->nu_cr
);
589 FREE_ZONE(nuidp
, sizeof(struct nfsuid
), M_NFSUID
);
596 TAILQ_INSERT_TAIL(&slp
->ns_uidlruhead
, nuidp
,
598 LIST_INSERT_HEAD(NUIDHASH(slp
, nsd
->nsd_uid
),
600 nfsrv_setcred(nuidp
->nu_cr
,
601 nfsd
->nfsd_nd
->nd_cr
);
602 nfsd
->nfsd_nd
->nd_flag
|= ND_KERBFULL
;
606 if ((uap
->flag
& NFSSVC_AUTHINFAIL
) && (nfsd
= nsd
->nsd_nfsd
))
607 nfsd
->nfsd_flag
|= NFSD_AUTHFAIL
;
608 error
= nfssvc_nfsd(nsd
, uap
->argp
, p
);
609 } else if (uap
->flag
& NFSSVC_EXPORT
) {
610 error
= nfssvc_export(uap
->argp
, p
);
614 #endif /* NFS_NOSERVER */
615 if (error
== EINTR
|| error
== ERESTART
)
621 * NFSKERB client helper daemon.
622 * Gets authorization strings for "kerb" mounts.
626 struct nfsmount
*nmp
,
627 struct nfsd_cargs
*ncd
,
632 struct nfsuid
*nuidp
, *nnuidp
;
638 * First initialize some variables
643 * If an authorization string is being passed in, get it.
645 if ((flag
& NFSSVC_GOTAUTH
) && (nmp
->nm_state
& NFSSTA_MOUNTED
) &&
646 ((nmp
->nm_state
& NFSSTA_WAITAUTH
) == 0)) {
647 if (nmp
->nm_state
& NFSSTA_HASAUTH
)
649 if ((flag
& NFSSVC_AUTHINFAIL
) == 0) {
650 if (ncd
->ncd_authlen
<= nmp
->nm_authlen
&&
651 ncd
->ncd_verflen
<= nmp
->nm_verflen
&&
652 !copyin(CAST_USER_ADDR_T(ncd
->ncd_authstr
),nmp
->nm_authstr
,ncd
->ncd_authlen
)&&
653 !copyin(CAST_USER_ADDR_T(ncd
->ncd_verfstr
),nmp
->nm_verfstr
,ncd
->ncd_verflen
)){
654 nmp
->nm_authtype
= ncd
->ncd_authtype
;
655 nmp
->nm_authlen
= ncd
->ncd_authlen
;
656 nmp
->nm_verflen
= ncd
->ncd_verflen
;
658 nmp
->nm_key
= ncd
->ncd_key
;
661 nmp
->nm_state
|= NFSSTA_AUTHERR
;
663 nmp
->nm_state
|= NFSSTA_AUTHERR
;
664 nmp
->nm_state
|= NFSSTA_HASAUTH
;
665 wakeup((caddr_t
)&nmp
->nm_authlen
);
667 nmp
->nm_state
|= NFSSTA_WAITAUTH
;
671 * Loop every second updating queue until there is a termination sig.
673 while (nmp
->nm_state
& NFSSTA_MOUNTED
) {
674 /* Get an authorization string, if required. */
675 if ((nmp
->nm_state
& (NFSSTA_WAITAUTH
| NFSSTA_HASAUTH
)) == 0) {
676 ncd
->ncd_authuid
= nmp
->nm_authuid
;
677 if (copyout((caddr_t
)ncd
, argp
, sizeof (struct nfsd_cargs
)))
678 nmp
->nm_state
|= NFSSTA_WAITAUTH
;
682 /* Wait a bit (no pun) and do it again. */
683 if ((nmp
->nm_state
& NFSSTA_MOUNTED
) &&
684 (nmp
->nm_state
& (NFSSTA_WAITAUTH
| NFSSTA_HASAUTH
))) {
685 error
= tsleep((caddr_t
)&nmp
->nm_authstr
, PSOCK
| PCATCH
,
686 "nfskrbtimr", hz
/ 3);
687 if (error
== EINTR
|| error
== ERESTART
)
688 dounmount(nmp
->nm_mountp
, 0, p
);
693 * Finally, we can free up the mount structure.
695 for (nuidp
= nmp
->nm_uidlruhead
.tqh_first
; nuidp
!= 0; nuidp
= nnuidp
) {
696 nnuidp
= nuidp
->nu_lru
.tqe_next
;
697 LIST_REMOVE(nuidp
, nu_hash
);
698 TAILQ_REMOVE(&nmp
->nm_uidlruhead
, nuidp
, nu_lru
);
699 kauth_cred_rele(nuidp
->nu_cr
);
700 FREE_ZONE((caddr_t
)nuidp
, sizeof (struct nfsuid
), M_NFSUID
);
703 * Loop through outstanding request list and remove dangling
704 * references to defunct nfsmount struct
706 for (rp
= nfs_reqq
.tqh_first
; rp
; rp
= rp
->r_chain
.tqe_next
)
707 if (rp
->r_nmp
== nmp
)
708 rp
->r_nmp
= (struct nfsmount
*)0;
709 /* Need to wake up any rcvlock waiters so they notice the unmount. */
710 if (nmp
->nm_state
& NFSSTA_WANTRCV
) {
711 nmp
->nm_state
&= ~NFSSTA_WANTRCV
;
712 wakeup(&nmp
->nm_state
);
714 FREE_ZONE((caddr_t
)nmp
, sizeof (struct nfsmount
), M_NFSMNT
);
715 if (error
== EWOULDBLOCK
)
722 * Adds a socket to the list for servicing by nfsds.
731 struct nfssvc_sock
*slp
;
732 struct nfssvc_sock
*tslp
= NULL
;
733 int error
, sodomain
, sotype
, soprotocol
, on
= 1;
734 struct timeval timeo
;
736 /* make sure mbuf constants are set up */
740 sock_gettype(so
, &sodomain
, &sotype
, &soprotocol
);
743 * Add it to the list, as required.
745 if (soprotocol
== IPPROTO_UDP
) {
747 if (!tslp
|| (tslp
->ns_flag
& SLP_VALID
)) {
752 } else if (soprotocol
== ISOPROTO_CLTP
) {
754 if (!tslp
|| (tslp
->ns_flag
& SLP_VALID
)) {
760 /* reserve buffer space for 2 maximally-sized packets */
762 if (sotype
== SOCK_STREAM
)
763 siz
+= sizeof (u_long
);
765 if (siz
> NFS_MAXSOCKBUF
)
766 siz
= NFS_MAXSOCKBUF
;
767 if ((error
= sock_setsockopt(so
, SOL_SOCKET
, SO_SNDBUF
, &siz
, sizeof(siz
))) ||
768 (error
= sock_setsockopt(so
, SOL_SOCKET
, SO_RCVBUF
, &siz
, sizeof(siz
)))) {
774 * Set protocol specific options { for now TCP only } and
775 * reserve some space. For datagram sockets, this can get called
776 * repeatedly for the same socket, but that isn't harmful.
778 if (sotype
== SOCK_STREAM
) {
779 sock_setsockopt(so
, SOL_SOCKET
, SO_KEEPALIVE
, &on
, sizeof(on
));
781 if (sodomain
== AF_INET
&& soprotocol
== IPPROTO_TCP
) {
782 sock_setsockopt(so
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof(on
));
785 sock_nointerrupt(so
, 0);
789 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_RCVTIMEO
, &timeo
, sizeof(timeo
));
790 error
= sock_setsockopt(so
, SOL_SOCKET
, SO_SNDTIMEO
, &timeo
, sizeof(timeo
));
794 lck_mtx_lock(nfsd_mutex
);
796 MALLOC(slp
, struct nfssvc_sock
*, sizeof(struct nfssvc_sock
),
802 bzero((caddr_t
)slp
, sizeof (struct nfssvc_sock
));
803 lck_rw_init(&slp
->ns_rwlock
, nfs_slp_rwlock_group
, nfs_slp_lock_attr
);
804 lck_mtx_init(&slp
->ns_wgmutex
, nfs_slp_mutex_group
, nfs_slp_lock_attr
);
805 TAILQ_INIT(&slp
->ns_uidlruhead
);
806 lck_mtx_lock(nfsd_mutex
);
807 TAILQ_INSERT_TAIL(&nfssvc_sockhead
, slp
, ns_chain
);
810 sock_retain(so
); /* grab a retain count on the socket */
812 slp
->ns_sotype
= sotype
;
816 so
->so_upcallarg
= (caddr_t
)slp
;
817 so
->so_upcall
= nfsrv_rcv
;
818 so
->so_rcv
.sb_flags
|= SB_UPCALL
; /* required for freebsd merge */
819 socket_unlock(so
, 1);
821 slp
->ns_flag
= SLP_VALID
| SLP_NEEDQ
;
824 lck_mtx_unlock(nfsd_mutex
);
830 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
831 * until it is killed by a signal.
834 nfssvc_nfsd(nsd
, argp
, p
)
835 struct nfsd_srvargs
*nsd
;
840 struct nfssvc_sock
*slp
;
841 struct nfsd
*nfsd
= nsd
->nsd_nfsd
;
842 struct nfsrv_descript
*nd
= NULL
;
843 int error
= 0, cacherep
, writes_todo
;
844 int siz
, procrastinate
;
847 boolean_t funnel_state
;
853 if (nfsd
== (struct nfsd
*)0) {
854 MALLOC(nfsd
, struct nfsd
*, sizeof(struct nfsd
), M_NFSD
, M_WAITOK
);
857 nsd
->nsd_nfsd
= nfsd
;
858 bzero((caddr_t
)nfsd
, sizeof (struct nfsd
));
859 nfsd
->nfsd_procp
= p
;
860 lck_mtx_lock(nfsd_mutex
);
861 TAILQ_INSERT_TAIL(&nfsd_head
, nfsd
, nfsd_chain
);
863 lck_mtx_unlock(nfsd_mutex
);
866 funnel_state
= thread_funnel_set(kernel_flock
, FALSE
);
869 * Loop getting rpc requests until SIGKILL.
872 if ((nfsd
->nfsd_flag
& NFSD_REQINPROG
) == 0) {
873 lck_mtx_lock(nfsd_mutex
);
874 while ((nfsd
->nfsd_slp
== NULL
) && !(nfsd_head_flag
& NFSD_CHECKSLP
)) {
875 nfsd
->nfsd_flag
|= NFSD_WAITING
;
877 error
= msleep(nfsd
, nfsd_mutex
, PSOCK
| PCATCH
, "nfsd", 0);
880 lck_mtx_unlock(nfsd_mutex
);
884 if ((nfsd
->nfsd_slp
== NULL
) && (nfsd_head_flag
& NFSD_CHECKSLP
)) {
885 TAILQ_FOREACH(slp
, &nfssvc_sockhead
, ns_chain
) {
886 lck_rw_lock_shared(&slp
->ns_rwlock
);
887 if ((slp
->ns_flag
& (SLP_VALID
| SLP_DOREC
))
888 == (SLP_VALID
| SLP_DOREC
)) {
889 if (lck_rw_lock_shared_to_exclusive(&slp
->ns_rwlock
)) {
890 /* upgrade failed and we lost the lock; take exclusive and recheck */
891 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
892 if ((slp
->ns_flag
& (SLP_VALID
| SLP_DOREC
))
893 != (SLP_VALID
| SLP_DOREC
)) {
894 /* flags no longer set, so skip this socket */
895 lck_rw_done(&slp
->ns_rwlock
);
899 slp
->ns_flag
&= ~SLP_DOREC
;
901 nfsd
->nfsd_slp
= slp
;
902 lck_rw_done(&slp
->ns_rwlock
);
905 lck_rw_done(&slp
->ns_rwlock
);
908 nfsd_head_flag
&= ~NFSD_CHECKSLP
;
910 lck_mtx_unlock(nfsd_mutex
);
911 if ((slp
= nfsd
->nfsd_slp
) == NULL
)
913 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
914 if (slp
->ns_flag
& SLP_VALID
) {
915 if (slp
->ns_flag
& SLP_DISCONN
) {
917 } else if (slp
->ns_flag
& SLP_NEEDQ
) {
918 slp
->ns_flag
&= ~SLP_NEEDQ
;
919 nfsrv_rcv_locked(slp
->ns_so
, slp
, MBUF_WAITOK
);
921 error
= nfsrv_dorec(slp
, nfsd
, &nd
);
923 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 +
924 (u_quad_t
)now
.tv_usec
;
925 if (error
&& slp
->ns_wgtime
&& (slp
->ns_wgtime
<= cur_usec
)) {
931 nfsd
->nfsd_flag
|= NFSD_REQINPROG
;
933 lck_rw_done(&slp
->ns_rwlock
);
936 slp
= nfsd
->nfsd_slp
;
938 if (error
|| (slp
->ns_flag
& SLP_VALID
) == 0) {
941 mbuf_freem(nd
->nd_nam2
);
943 kauth_cred_rele(nd
->nd_cr
);
944 FREE_ZONE((caddr_t
)nd
,
945 sizeof *nd
, M_NFSRVDESC
);
948 nfsd
->nfsd_slp
= NULL
;
949 nfsd
->nfsd_flag
&= ~NFSD_REQINPROG
;
954 microuptime(&nd
->nd_starttime
);
956 nd
->nd_nam
= nd
->nd_nam2
;
958 nd
->nd_nam
= slp
->ns_nam
;
961 * Check to see if authorization is needed.
963 if (nfsd
->nfsd_flag
& NFSD_NEEDAUTH
) {
964 nfsd
->nfsd_flag
&= ~NFSD_NEEDAUTH
;
965 nsd
->nsd_haddr
= ((struct sockaddr_in
*)mbuf_data(nd
->nd_nam
))->sin_addr
.s_addr
;
966 nsd
->nsd_authlen
= nfsd
->nfsd_authlen
;
967 nsd
->nsd_verflen
= nfsd
->nfsd_verflen
;
968 if (!copyout(nfsd
->nfsd_authstr
,CAST_USER_ADDR_T(nsd
->nsd_authstr
),
969 nfsd
->nfsd_authlen
) &&
970 !copyout(nfsd
->nfsd_verfstr
, CAST_USER_ADDR_T(nsd
->nsd_verfstr
),
971 nfsd
->nfsd_verflen
) &&
972 !copyout((caddr_t
)nsd
, argp
, sizeof (*nsd
))) {
973 thread_funnel_set(kernel_flock
, funnel_state
);
976 cacherep
= RC_DROPIT
;
978 cacherep
= nfsrv_getcache(nd
, slp
, &mreq
);
980 if (nfsd
->nfsd_flag
& NFSD_AUTHFAIL
) {
981 nfsd
->nfsd_flag
&= ~NFSD_AUTHFAIL
;
982 nd
->nd_procnum
= NFSPROC_NOOP
;
983 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_TOOWEAK
);
985 } else if (nfs_privport
) {
986 /* Check if source port is privileged */
988 struct sockaddr
*nam
= mbuf_data(nd
->nd_nam
);
989 struct sockaddr_in
*sin
;
991 sin
= (struct sockaddr_in
*)nam
;
992 port
= ntohs(sin
->sin_port
);
993 if (port
>= IPPORT_RESERVED
&&
994 nd
->nd_procnum
!= NFSPROC_NULL
) {
995 char strbuf
[MAX_IPv4_STR_LEN
];
996 nd
->nd_procnum
= NFSPROC_NOOP
;
997 nd
->nd_repstat
= (NFSERR_AUTHERR
| AUTH_TOOWEAK
);
999 printf("NFS request from unprivileged port (%s:%d)\n",
1000 inet_ntop(AF_INET
, &sin
->sin_addr
, strbuf
, sizeof(strbuf
)),
1008 * Loop to get all the write rpc relies that have been
1009 * gathered together.
1014 if (nd
&& (nd
->nd_flag
& ND_NFSV3
))
1015 procrastinate
= nfsrvw_procrastinate_v3
;
1017 procrastinate
= nfsrvw_procrastinate
;
1018 lck_rw_lock_shared(&nfs_export_rwlock
);
1019 if (writes_todo
|| ((nd
->nd_procnum
== NFSPROC_WRITE
) && (procrastinate
> 0)))
1020 error
= nfsrv_writegather(&nd
, slp
, nfsd
->nfsd_procp
, &mreq
);
1022 error
= (*(nfsrv3_procs
[nd
->nd_procnum
]))(nd
, slp
, nfsd
->nfsd_procp
, &mreq
);
1023 lck_rw_done(&nfs_export_rwlock
);
1027 OSAddAtomic(1, (SInt32
*)&nfsstats
.srv_errs
);
1028 nfsrv_updatecache(nd
, FALSE
, mreq
);
1030 mbuf_freem(nd
->nd_nam2
);
1035 OSAddAtomic(1, (SInt32
*)&nfsstats
.srvrpccnt
[nd
->nd_procnum
]);
1036 nfsrv_updatecache(nd
, TRUE
, mreq
);
1045 if (siz
<= 0 || siz
> NFS_MAXPACKET
) {
1046 printf("mbuf siz=%d\n",siz
);
1047 panic("Bad nfs svc reply");
1050 mbuf_pkthdr_setlen(m
, siz
);
1051 error
= mbuf_pkthdr_setrcvif(m
, NULL
);
1053 panic("nfsd setrcvif failed: %d", error
);
1055 * For stream protocols, prepend a Sun RPC
1058 if (slp
->ns_sotype
== SOCK_STREAM
) {
1059 error
= mbuf_prepend(&m
, NFSX_UNSIGNED
, MBUF_WAITOK
);
1061 *(u_long
*)mbuf_data(m
) = htonl(0x80000000 | siz
);
1064 if (slp
->ns_flag
& SLP_VALID
) {
1065 error
= nfs_send(slp
->ns_so
, nd
->nd_nam2
, m
, NULL
);
1075 nfsd_rt(slp
->ns_sotype
, nd
, cacherep
);
1077 mbuf_freem(nd
->nd_nam2
);
1081 mbuf_freem(nd
->nd_mrep
);
1084 if (error
== EPIPE
) {
1085 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
1087 lck_rw_done(&slp
->ns_rwlock
);
1089 if (error
== EINTR
|| error
== ERESTART
) {
1091 kauth_cred_rele(nd
->nd_cr
);
1092 FREE_ZONE((caddr_t
)nd
, sizeof *nd
, M_NFSRVDESC
);
1093 nfsrv_slpderef(slp
);
1099 nfsd_rt(slp
->ns_sotype
, nd
, cacherep
);
1100 mbuf_freem(nd
->nd_mrep
);
1101 mbuf_freem(nd
->nd_nam2
);
1102 nd
->nd_mrep
= nd
->nd_nam2
= NULL
;
1107 mbuf_freem(nd
->nd_mrep
);
1109 mbuf_freem(nd
->nd_nam2
);
1111 kauth_cred_rele(nd
->nd_cr
);
1112 FREE_ZONE((caddr_t
)nd
, sizeof *nd
, M_NFSRVDESC
);
1117 * Check to see if there are outstanding writes that
1118 * need to be serviced.
1121 cur_usec
= (u_quad_t
)now
.tv_sec
* 1000000 +
1122 (u_quad_t
)now
.tv_usec
;
1123 if (slp
->ns_wgtime
&& (slp
->ns_wgtime
<= cur_usec
)) {
1129 } while (writes_todo
);
1130 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
1131 if (nfsrv_dorec(slp
, nfsd
, &nd
)) {
1132 lck_rw_done(&slp
->ns_rwlock
);
1133 nfsd
->nfsd_flag
&= ~NFSD_REQINPROG
;
1134 nfsd
->nfsd_slp
= NULL
;
1135 nfsrv_slpderef(slp
);
1137 lck_rw_done(&slp
->ns_rwlock
);
1141 thread_funnel_set(kernel_flock
, funnel_state
);
1142 lck_mtx_lock(nfsd_mutex
);
1143 TAILQ_REMOVE(&nfsd_head
, nfsd
, nfsd_chain
);
1145 nsd
->nsd_nfsd
= (struct nfsd
*)0;
1146 if (--nfs_numnfsd
== 0)
1147 nfsrv_init(TRUE
); /* Reinitialize everything */
1148 lck_mtx_unlock(nfsd_mutex
);
1153 nfssvc_export(user_addr_t argp
, proc_t p
)
1155 int error
= 0, is_64bit
;
1156 struct user_nfs_export_args unxa
;
1157 struct vfs_context context
;
1159 context
.vc_proc
= p
;
1160 context
.vc_ucred
= kauth_cred_get();
1161 is_64bit
= IS_64BIT_PROCESS(p
);
1163 /* copy in pointers to path and export args */
1165 error
= copyin(argp
, (caddr_t
)&unxa
, sizeof(unxa
));
1167 struct nfs_export_args tnxa
;
1168 error
= copyin(argp
, (caddr_t
)&tnxa
, sizeof(tnxa
));
1170 /* munge into LP64 version of nfs_export_args structure */
1171 unxa
.nxa_fsid
= tnxa
.nxa_fsid
;
1172 unxa
.nxa_expid
= tnxa
.nxa_expid
;
1173 unxa
.nxa_fspath
= CAST_USER_ADDR_T(tnxa
.nxa_fspath
);
1174 unxa
.nxa_exppath
= CAST_USER_ADDR_T(tnxa
.nxa_exppath
);
1175 unxa
.nxa_flags
= tnxa
.nxa_flags
;
1176 unxa
.nxa_netcount
= tnxa
.nxa_netcount
;
1177 unxa
.nxa_nets
= CAST_USER_ADDR_T(tnxa
.nxa_nets
);
1183 error
= nfsrv_export(&unxa
, &context
);
1188 #endif /* NFS_NOSERVER */
1191 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
1193 SYSCTL_INT(_vfs_nfs
, OID_AUTO
, defect
, CTLFLAG_RW
, &nfs_defect
, 0, "");
1197 nfsclnt(proc_t p
, struct nfsclnt_args
*uap
, __unused
int *retval
)
1199 struct lockd_ans la
;
1202 if (uap
->flag
== NFSCLNT_LOCKDWAIT
) {
1203 return (nfslockdwait(p
));
1205 if (uap
->flag
== NFSCLNT_LOCKDANS
) {
1206 error
= copyin(uap
->argp
, &la
, sizeof(la
));
1207 return (error
!= 0 ? error
: nfslockdans(p
, &la
));
1209 if (uap
->flag
== NFSCLNT_LOCKDFD
)
1210 return (nfslockdfd(p
, CAST_DOWN(int, uap
->argp
)));
1215 static int nfssvc_iod_continue(int);
1218 * Asynchronous I/O daemons for client nfs.
1219 * They do read-ahead and write-behind operations on the block I/O cache.
1220 * Never returns unless it fails or gets killed.
1223 nfssvc_iod(__unused proc_t p
)
1225 register int i
, myiod
;
1229 * Assign my position or return error if too many already running
1232 for (i
= 0; i
< NFS_MAXASYNCDAEMON
; i
++)
1233 if (nfs_asyncdaemon
[i
] == 0) {
1234 nfs_asyncdaemon
[i
]++;
1242 /* stuff myiod into uthread to get off local stack for continuation */
1244 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1245 ut
->uu_state
.uu_nfs_myiod
= myiod
; /* squirrel away for continuation */
1247 nfssvc_iod_continue(0);
1253 * Continuation for Asynchronous I/O daemons for client nfs.
1256 nfssvc_iod_continue(int error
)
1258 register struct nfsbuf
*bp
;
1259 register int i
, myiod
;
1260 struct nfsmount
*nmp
;
1265 * real myiod is stored in uthread, recover it
1267 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
1268 myiod
= ut
->uu_state
.uu_nfs_myiod
;
1269 p
= current_proc(); // XXX
1272 * Just loop around doin our stuff until SIGKILL
1273 * - actually we don't loop with continuations...
1275 lck_mtx_lock(nfs_iod_mutex
);
1277 while (((nmp
= nfs_iodmount
[myiod
]) == NULL
1278 || nmp
->nm_bufq
.tqh_first
== NULL
)
1279 && error
== 0 && nfs_ioddelwri
== 0) {
1282 nfs_iodwant
[myiod
] = p
; // XXX this doesn't need to be a proc_t
1283 nfs_iodmount
[myiod
] = NULL
;
1284 error
= msleep0((caddr_t
)&nfs_iodwant
[myiod
], nfs_iod_mutex
,
1285 PWAIT
| PCATCH
| PDROP
, "nfsidl", 0, nfssvc_iod_continue
);
1286 lck_mtx_lock(nfs_iod_mutex
);
1289 nfs_asyncdaemon
[myiod
] = 0;
1290 if (nmp
) nmp
->nm_bufqiods
--;
1291 nfs_iodwant
[myiod
] = NULL
;
1292 nfs_iodmount
[myiod
] = NULL
;
1293 lck_mtx_unlock(nfs_iod_mutex
);
1295 if (error
== EINTR
|| error
== ERESTART
)
1297 unix_syscall_return(error
);
1300 while ((bp
= TAILQ_FIRST(&nmp
->nm_bufq
)) != NULL
) {
1301 /* Take one off the front of the list */
1302 TAILQ_REMOVE(&nmp
->nm_bufq
, bp
, nb_free
);
1303 bp
->nb_free
.tqe_next
= NFSNOLIST
;
1305 if (nmp
->nm_bufqwant
&& nmp
->nm_bufqlen
< 2 * nfs_numasync
) {
1306 nmp
->nm_bufqwant
= FALSE
;
1307 lck_mtx_unlock(nfs_iod_mutex
);
1308 wakeup(&nmp
->nm_bufq
);
1310 lck_mtx_unlock(nfs_iod_mutex
);
1313 SET(bp
->nb_flags
, NB_IOD
);
1314 if (ISSET(bp
->nb_flags
, NB_READ
))
1315 nfs_doio(bp
, bp
->nb_rcred
, NULL
);
1317 nfs_doio(bp
, bp
->nb_wcred
, NULL
);
1319 lck_mtx_lock(nfs_iod_mutex
);
1321 * If there are more than one iod on this mount, then defect
1322 * so that the iods can be shared out fairly between the mounts
1324 if (nfs_defect
&& nmp
->nm_bufqiods
> 1) {
1325 nfs_iodmount
[myiod
] = NULL
;
1331 lck_mtx_unlock(nfs_iod_mutex
);
1333 if (nfs_ioddelwri
) {
1336 lck_mtx_lock(nfs_buf_mutex
);
1337 while (i
< 8 && (bp
= TAILQ_FIRST(&nfsbufdelwri
)) != NULL
) {
1338 struct nfsnode
*np
= VTONFS(bp
->nb_vp
);
1339 nfs_buf_remfree(bp
);
1341 while ((error
= nfs_buf_acquire(bp
, 0, 0, 0)) == EAGAIN
);
1342 nfs_buf_refrele(bp
);
1346 /* buffer is no longer valid */
1350 if (ISSET(bp
->nb_flags
, NB_NEEDCOMMIT
)) {
1351 /* put buffer at end of delwri list */
1352 TAILQ_INSERT_TAIL(&nfsbufdelwri
, bp
, nb_free
);
1355 lck_mtx_unlock(nfs_buf_mutex
);
1356 nfs_flushcommits(np
->n_vnode
, NULL
, 1);
1358 SET(bp
->nb_flags
, (NB_ASYNC
| NB_IOD
));
1359 lck_mtx_unlock(nfs_buf_mutex
);
1363 lck_mtx_lock(nfs_buf_mutex
);
1365 lck_mtx_unlock(nfs_buf_mutex
);
1368 lck_mtx_lock(nfs_iod_mutex
);
1373 * Shut down a socket associated with an nfssvc_sock structure.
1374 * Should be called with the send lock set, if required.
1375 * The trick here is to increment the sref at the start, so that the nfsds
1376 * will stop using it and clear ns_flag at the end so that it will not be
1377 * reassigned during cleanup.
1380 nfsrv_zapsock(struct nfssvc_sock
*slp
)
1384 if ((slp
->ns_flag
& SLP_VALID
) == 0)
1386 slp
->ns_flag
&= ~SLP_ALLFLAGS
;
1393 so
->so_upcall
= NULL
;
1394 so
->so_rcv
.sb_flags
&= ~SB_UPCALL
;
1395 socket_unlock(so
, 1);
1396 sock_shutdown(so
, SHUT_RDWR
);
1400 * Get an authorization string for the uid by having the mount_nfs sitting
1401 * on this mount point porpous out of the kernel and do it.
1404 nfs_getauth(nmp
, rep
, cred
, auth_str
, auth_len
, verf_str
, verf_len
, key
)
1405 register struct nfsmount
*nmp
;
1412 NFSKERBKEY_T key
; /* return session key */
1416 while ((nmp
->nm_state
& NFSSTA_WAITAUTH
) == 0) {
1417 nmp
->nm_state
|= NFSSTA_WANTAUTH
;
1418 (void) tsleep((caddr_t
)&nmp
->nm_authtype
, PSOCK
,
1419 "nfsauth1", 2 * hz
);
1420 error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
);
1422 nmp
->nm_state
&= ~NFSSTA_WANTAUTH
;
1426 nmp
->nm_state
&= ~NFSSTA_WANTAUTH
;
1427 MALLOC(*auth_str
, char *, RPCAUTH_MAXSIZ
, M_TEMP
, M_WAITOK
);
1430 nmp
->nm_authstr
= *auth_str
;
1431 nmp
->nm_authlen
= RPCAUTH_MAXSIZ
;
1432 nmp
->nm_verfstr
= verf_str
;
1433 nmp
->nm_verflen
= *verf_len
;
1434 nmp
->nm_authuid
= kauth_cred_getuid(cred
);
1435 nmp
->nm_state
&= ~NFSSTA_WAITAUTH
;
1436 wakeup((caddr_t
)&nmp
->nm_authstr
);
1439 * And wait for mount_nfs to do its stuff.
1441 while ((nmp
->nm_state
& NFSSTA_HASAUTH
) == 0 && error
== 0) {
1442 (void) tsleep((caddr_t
)&nmp
->nm_authlen
, PSOCK
,
1443 "nfsauth2", 2 * hz
);
1444 error
= nfs_sigintr(nmp
, rep
, rep
->r_procp
);
1446 if (nmp
->nm_state
& NFSSTA_AUTHERR
) {
1447 nmp
->nm_state
&= ~NFSSTA_AUTHERR
;
1451 FREE(*auth_str
, M_TEMP
);
1453 *auth_len
= nmp
->nm_authlen
;
1454 *verf_len
= nmp
->nm_verflen
;
1455 bcopy((caddr_t
)nmp
->nm_key
, (caddr_t
)key
, sizeof (key
));
1457 nmp
->nm_state
&= ~NFSSTA_HASAUTH
;
1458 nmp
->nm_state
|= NFSSTA_WAITAUTH
;
1459 if (nmp
->nm_state
& NFSSTA_WANTAUTH
) {
1460 nmp
->nm_state
&= ~NFSSTA_WANTAUTH
;
1461 wakeup((caddr_t
)&nmp
->nm_authtype
);
1467 * Get a nickname authenticator and verifier.
1471 struct nfsmount
*nmp
,
1476 __unused
int verf_len
)
1478 register struct nfsuid
*nuidp
;
1479 register u_long
*nickp
, *verfp
;
1480 struct timeval ktvin
, ktvout
, now
;
1483 if (verf_len
< (4 * NFSX_UNSIGNED
))
1484 panic("nfs_getnickauth verf too small");
1486 for (nuidp
= NMUIDHASH(nmp
, kauth_cred_getuid(cred
))->lh_first
;
1487 nuidp
!= 0; nuidp
= nuidp
->nu_hash
.le_next
) {
1488 if (kauth_cred_getuid(nuidp
->nu_cr
) == kauth_cred_getuid(cred
))
1492 if (!nuidp
|| nuidp
->nu_expire
< now
.tv_sec
)
1495 MALLOC(nickp
, u_long
*, 2 * NFSX_UNSIGNED
, M_TEMP
, M_WAITOK
);
1500 * Move to the end of the lru list (end of lru == most recently used).
1502 TAILQ_REMOVE(&nmp
->nm_uidlruhead
, nuidp
, nu_lru
);
1503 TAILQ_INSERT_TAIL(&nmp
->nm_uidlruhead
, nuidp
, nu_lru
);
1505 *nickp
++ = txdr_unsigned(RPCAKN_NICKNAME
);
1506 *nickp
= txdr_unsigned(nuidp
->nu_nickname
);
1507 *auth_str
= (char *)nickp
;
1508 *auth_len
= 2 * NFSX_UNSIGNED
;
1511 * Now we must encrypt the verifier and package it up.
1513 verfp
= (u_long
*)verf_str
;
1514 *verfp
++ = txdr_unsigned(RPCAKN_NICKNAME
);
1516 if (now
.tv_sec
> nuidp
->nu_timestamp
.tv_sec
||
1517 (now
.tv_sec
== nuidp
->nu_timestamp
.tv_sec
&&
1518 now
.tv_usec
> nuidp
->nu_timestamp
.tv_usec
))
1519 nuidp
->nu_timestamp
= now
;
1521 nuidp
->nu_timestamp
.tv_usec
++;
1522 ktvin
.tv_sec
= txdr_unsigned(nuidp
->nu_timestamp
.tv_sec
);
1523 ktvin
.tv_usec
= txdr_unsigned(nuidp
->nu_timestamp
.tv_usec
);
1526 * Now encrypt the timestamp verifier in ecb mode using the session
1533 *verfp
++ = ktvout
.tv_sec
;
1534 *verfp
++ = ktvout
.tv_usec
;
1540 * Save the current nickname in a hash list entry on the mount point.
1543 nfs_savenickauth(nmp
, cred
, len
, key
, mdp
, dposp
, mrep
)
1544 register struct nfsmount
*nmp
;
1552 register struct nfsuid
*nuidp
;
1553 register u_long
*tl
;
1556 struct timeval ktvin
, ktvout
, now
;
1558 char *dpos
= *dposp
, *cp2
;
1559 int deltasec
, error
= 0;
1561 if (len
== (3 * NFSX_UNSIGNED
)) {
1562 nfsm_dissect(tl
, u_long
*, 3 * NFSX_UNSIGNED
);
1563 ktvin
.tv_sec
= *tl
++;
1564 ktvin
.tv_usec
= *tl
++;
1565 nick
= fxdr_unsigned(u_long
, *tl
);
1568 * Decrypt the timestamp in ecb mode.
1573 ktvout
.tv_sec
= fxdr_unsigned(long, ktvout
.tv_sec
);
1574 ktvout
.tv_usec
= fxdr_unsigned(long, ktvout
.tv_usec
);
1576 deltasec
= now
.tv_sec
- ktvout
.tv_sec
;
1578 deltasec
= -deltasec
;
1580 * If ok, add it to the hash list for the mount point.
1582 if (deltasec
<= NFS_KERBCLOCKSKEW
) {
1583 if (nmp
->nm_numuids
< nuidhash_max
) {
1585 MALLOC_ZONE(nuidp
, struct nfsuid
*,
1586 sizeof (struct nfsuid
),
1587 M_NFSUID
, M_WAITOK
);
1592 nuidp
= nmp
->nm_uidlruhead
.tqh_first
;
1597 LIST_REMOVE(nuidp
, nu_hash
);
1598 TAILQ_REMOVE(&nmp
->nm_uidlruhead
, nuidp
, nu_lru
);
1599 kauth_cred_rele(nuidp
->nu_cr
);
1602 kauth_cred_ref(cred
);
1603 nuidp
->nu_cr
= cred
;
1604 nuidp
->nu_expire
= now
.tv_sec
+ NFS_KERBTTL
;
1605 nuidp
->nu_timestamp
= ktvout
;
1606 nuidp
->nu_nickname
= nick
;
1607 bcopy(key
, nuidp
->nu_key
, sizeof (key
));
1608 TAILQ_INSERT_TAIL(&nmp
->nm_uidlruhead
, nuidp
, nu_lru
);
1609 LIST_INSERT_HEAD(NMUIDHASH(nmp
, kauth_cred_getuid(cred
)),
1613 nfsm_adv(nfsm_rndup(len
));
1620 #ifndef NFS_NOSERVER
1623 * cleanup and release a server socket structure.
1626 nfsrv_slpfree(struct nfssvc_sock
*slp
)
1628 struct nfsuid
*nuidp
, *nnuidp
;
1629 struct nfsrv_descript
*nwp
, *nnwp
;
1632 sock_release(slp
->ns_so
);
1636 mbuf_free(slp
->ns_nam
);
1638 mbuf_freem(slp
->ns_raw
);
1640 mbuf_freem(slp
->ns_rec
);
1641 slp
->ns_nam
= slp
->ns_raw
= slp
->ns_rec
= NULL
;
1643 for (nuidp
= slp
->ns_uidlruhead
.tqh_first
; nuidp
!= 0;
1645 nnuidp
= nuidp
->nu_lru
.tqe_next
;
1646 LIST_REMOVE(nuidp
, nu_hash
);
1647 TAILQ_REMOVE(&slp
->ns_uidlruhead
, nuidp
, nu_lru
);
1648 if (nuidp
->nu_flag
& NU_NAM
)
1649 mbuf_freem(nuidp
->nu_nam
);
1650 kauth_cred_rele(nuidp
->nu_cr
);
1651 FREE_ZONE((caddr_t
)nuidp
,
1652 sizeof (struct nfsuid
), M_NFSUID
);
1655 for (nwp
= slp
->ns_tq
.lh_first
; nwp
; nwp
= nnwp
) {
1656 nnwp
= nwp
->nd_tq
.le_next
;
1657 LIST_REMOVE(nwp
, nd_tq
);
1659 kauth_cred_rele(nwp
->nd_cr
);
1660 FREE_ZONE((caddr_t
)nwp
, sizeof *nwp
, M_NFSRVDESC
);
1662 LIST_INIT(&slp
->ns_tq
);
1664 lck_rw_destroy(&slp
->ns_rwlock
, nfs_slp_rwlock_group
);
1665 lck_mtx_destroy(&slp
->ns_wgmutex
, nfs_slp_mutex_group
);
1666 FREE(slp
, M_NFSSVC
);
1670 * Derefence a server socket structure. If it has no more references and
1671 * is no longer valid, you can throw it away.
1674 nfsrv_slpderef(struct nfssvc_sock
*slp
)
1676 lck_mtx_lock(nfsd_mutex
);
1677 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
1679 if (slp
->ns_sref
|| (slp
->ns_flag
& SLP_VALID
)) {
1680 lck_rw_done(&slp
->ns_rwlock
);
1681 lck_mtx_unlock(nfsd_mutex
);
1685 TAILQ_REMOVE(&nfssvc_sockhead
, slp
, ns_chain
);
1686 lck_mtx_unlock(nfsd_mutex
);
1693 * Initialize the data structures for the server.
1694 * Handshake with any new nfsds starting up to avoid any chance of
1698 nfsrv_init(terminating
)
1701 struct nfssvc_sock
*slp
, *nslp
;
1704 for (slp
= TAILQ_FIRST(&nfssvc_sockhead
); slp
!= 0; slp
= nslp
) {
1705 nslp
= TAILQ_NEXT(slp
, ns_chain
);
1706 if (slp
->ns_flag
& SLP_VALID
) {
1707 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
1709 lck_rw_done(&slp
->ns_rwlock
);
1711 TAILQ_REMOVE(&nfssvc_sockhead
, slp
, ns_chain
);
1712 /* grab the lock one final time in case anyone's using it */
1713 lck_rw_lock_exclusive(&slp
->ns_rwlock
);
1716 nfsrv_cleancache(); /* And clear out server cache */
1717 /* XXX Revisit when enabling WebNFS */
1718 #ifdef WEBNFS_ENABLED
1720 nfs_pub
.np_valid
= 0;
1725 TAILQ_INIT(&nfssvc_sockhead
);
1727 TAILQ_INIT(&nfsd_head
);
1728 nfsd_head_flag
&= ~NFSD_CHECKSLP
;
1730 MALLOC(nfs_udpsock
, struct nfssvc_sock
*, sizeof(struct nfssvc_sock
),
1731 M_NFSSVC
, M_WAITOK
);
1733 bzero((caddr_t
)nfs_udpsock
, sizeof (struct nfssvc_sock
));
1734 lck_rw_init(&nfs_udpsock
->ns_rwlock
, nfs_slp_rwlock_group
, nfs_slp_lock_attr
);
1735 TAILQ_INIT(&nfs_udpsock
->ns_uidlruhead
);
1736 TAILQ_INSERT_HEAD(&nfssvc_sockhead
, nfs_udpsock
, ns_chain
);
1738 printf("nfsrv_init() failed to allocate UDP socket\n");
1742 MALLOC(nfs_cltpsock
, struct nfssvc_sock
*, sizeof(struct nfssvc_sock
),
1743 M_NFSSVC
, M_WAITOK
);
1745 bzero((caddr_t
)nfs_cltpsock
, sizeof (struct nfssvc_sock
));
1746 lck_rw_init(&nfs_cltpsock
->ns_rwlock
, nfs_slp_rwlock_group
, nfs_slp_lock_attr
);
1747 TAILQ_INIT(&nfs_cltpsock
->ns_uidlruhead
);
1748 TAILQ_INSERT_TAIL(&nfssvc_sockhead
, nfs_cltpsock
, ns_chain
);
1750 printf("nfsrv_init() failed to allocate CLTP socket\n");
1756 * Add entries to the server monitor log.
1759 nfsd_rt(sotype
, nd
, cacherep
)
1761 register struct nfsrv_descript
*nd
;
1764 register struct drt
*rt
;
1767 rt
= &nfsdrt
.drt
[nfsdrt
.pos
];
1768 if (cacherep
== RC_DOIT
)
1770 else if (cacherep
== RC_REPLY
)
1771 rt
->flag
= DRT_CACHEREPLY
;
1773 rt
->flag
= DRT_CACHEDROP
;
1774 if (sotype
== SOCK_STREAM
)
1775 rt
->flag
|= DRT_TCP
;
1776 else if (nd
->nd_flag
& ND_NFSV3
)
1777 rt
->flag
|= DRT_NFSV3
;
1778 rt
->proc
= nd
->nd_procnum
;
1779 if (((struct sockaddr
*)mbuf_data(nd
->nd_nam
))->sa_family
== AF_INET
)
1780 rt
->ipadr
= ((struct sockaddr_in
*)mbuf_data(nd
->nd_nam
))->sin_addr
.s_addr
;
1782 rt
->ipadr
= INADDR_ANY
;
1784 rt
->resptime
= ((now
.tv_sec
- nd
->nd_starttime
.tv_sec
) * 1000000) +
1785 (now
.tv_usec
- nd
->nd_starttime
.tv_usec
);
1786 microtime(&rt
->tstamp
); // XXX unused
1787 nfsdrt
.pos
= (nfsdrt
.pos
+ 1) % NFSRTTLOGSIZ
;
1789 #endif /* NFS_NOSERVER */