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