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