kernel - Add many sysctl definitions, sysv, vfs, nfs, etc.
[dragonfly.git] / sys / vfs / nfs / nfs_syscalls.c
CommitLineData
984263bc
MD
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 * $FreeBSD: src/sys/nfs/nfs_syscalls.c,v 1.58.2.1 2000/11/26 02:30:06 dillon Exp $
e7b4468c 38 * $DragonFly: src/sys/vfs/nfs/nfs_syscalls.c,v 1.31 2008/01/05 14:02:41 swildner Exp $
984263bc
MD
39 */
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/sysproto.h>
44#include <sys/kernel.h>
45#include <sys/sysctl.h>
46#include <sys/file.h>
47#include <sys/filedesc.h>
48#include <sys/vnode.h>
49#include <sys/malloc.h>
50#include <sys/mount.h>
51#include <sys/proc.h>
895c1f85 52#include <sys/priv.h>
984263bc
MD
53#include <sys/buf.h>
54#include <sys/mbuf.h>
e4700d00 55#include <sys/resourcevar.h>
984263bc
MD
56#include <sys/socket.h>
57#include <sys/socketvar.h>
58#include <sys/domain.h>
59#include <sys/protosw.h>
fad57d0e 60#include <sys/nlookup.h>
984263bc
MD
61#include <vm/vm_zone.h>
62
8684e6f9 63#include <sys/mutex2.h>
c6b43e93 64#include <sys/thread2.h>
8684e6f9 65
984263bc
MD
66#include <netinet/in.h>
67#include <netinet/tcp.h>
1f2de5d4
MD
68#include "xdr_subs.h"
69#include "rpcv2.h"
70#include "nfsproto.h"
71#include "nfs.h"
72#include "nfsm_subs.h"
73#include "nfsrvcache.h"
74#include "nfsmount.h"
75#include "nfsnode.h"
1f2de5d4 76#include "nfsrtt.h"
984263bc 77
165dba55
DR
78#include <sys/thread2.h>
79
984263bc
MD
80static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
81
984263bc
MD
82static int nuidhash_max = NFS_MAXUIDHASH;
83
84#ifndef NFS_NOSERVER
a6ee311a 85static void nfsrv_zapsock (struct nfssvc_sock *slp);
984263bc 86#endif
984263bc
MD
87
88#define TRUE 1
89#define FALSE 0
90
984263bc
MD
91SYSCTL_DECL(_vfs_nfs);
92
93#ifndef NFS_NOSERVER
94int nfsd_waiting = 0;
95static struct nfsdrt nfsdrt;
96static int nfs_numnfsd = 0;
a6ee311a
RG
97static void nfsd_rt (int sotype, struct nfsrv_descript *nd,
98 int cacherep);
99static int nfssvc_addsock (struct file *, struct sockaddr *,
100 struct thread *);
101static int nfssvc_nfsd (struct nfsd_srvargs *,caddr_t,struct thread *);
984263bc
MD
102
103static int nfs_privport = 0;
093e85dc
SG
104SYSCTL_INT(_vfs_nfs, NFS_NFSPRIVPORT, nfs_privport, CTLFLAG_RW, &nfs_privport,
105 0, "Enable privileged source port checks");
106SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay, CTLFLAG_RW, &nfsrvw_procrastinate, 0,
107 "Enable NFS request procrastination");
108SYSCTL_INT(_vfs_nfs, OID_AUTO, gatherdelay_v3, CTLFLAG_RW, &nfsrvw_procrastinate_v3, 0,
109 "Enable NFSv3 request procrastination");
cc7d050e 110int nfs_soreserve = NFS_MAXPACKET * NFS_MAXASYNCBIO;
093e85dc
SG
111SYSCTL_INT(_vfs_nfs, OID_AUTO, soreserve, CTLFLAG_RW, &nfs_soreserve, 0,
112 "Minimum NFS socket buffer size reservation");
984263bc
MD
113
114/*
115 * NFS server system calls
116 */
117
118#endif /* NFS_NOSERVER */
119/*
41c20dac
MD
120 * nfssvc_args(int flag, caddr_t argp)
121 *
984263bc
MD
122 * Nfs server psuedo system call for the nfsd's
123 * Based on the flag value it either:
124 * - adds a socket to the selection list
125 * - remains in the kernel as an nfsd
126 * - remains in the kernel as an nfsiod
3919ced0
MD
127 *
128 * MPALMOSTSAFE
984263bc 129 */
984263bc 130int
753fd850 131sys_nfssvc(struct nfssvc_args *uap)
984263bc
MD
132{
133#ifndef NFS_NOSERVER
fad57d0e 134 struct nlookupdata nd;
984263bc
MD
135 struct file *fp;
136 struct sockaddr *nam;
137 struct nfsd_args nfsdarg;
138 struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
139 struct nfsd_cargs ncd;
140 struct nfsd *nfsd;
141 struct nfssvc_sock *slp;
142 struct nfsuid *nuidp;
143 struct nfsmount *nmp;
fad57d0e 144 struct vnode *vp;
984263bc
MD
145#endif /* NFS_NOSERVER */
146 int error;
dadab5e9 147 struct thread *td = curthread;
984263bc
MD
148
149 /*
150 * Must be super user
151 */
895c1f85 152 error = priv_check(td, PRIV_ROOT);
3919ced0 153 if (error)
984263bc 154 return (error);
3919ced0 155
c6b43e93
MD
156 lwkt_gettoken(&nfs_token);
157
984263bc 158 while (nfssvc_sockhead_flag & SLP_INIT) {
3919ced0
MD
159 nfssvc_sockhead_flag |= SLP_WANTINIT;
160 tsleep((caddr_t)&nfssvc_sockhead, 0, "nfsd init", 0);
984263bc
MD
161 }
162 if (uap->flag & NFSSVC_BIOD)
52e1cf57 163 error = ENXIO; /* no longer need nfsiod's */
984263bc
MD
164#ifdef NFS_NOSERVER
165 else
166 error = ENXIO;
167#else /* !NFS_NOSERVER */
168 else if (uap->flag & NFSSVC_MNTD) {
169 error = copyin(uap->argp, (caddr_t)&ncd, sizeof (ncd));
170 if (error)
3919ced0 171 goto done;
fad57d0e
MD
172 vp = NULL;
173 error = nlookup_init(&nd, ncd.ncd_dirp, UIO_USERSPACE,
174 NLC_FOLLOW);
175 if (error == 0)
176 error = nlookup(&nd);
177 if (error == 0)
28623bf9 178 error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp);
fad57d0e 179 nlookup_done(&nd);
984263bc 180 if (error)
3919ced0 181 goto done;
fad57d0e
MD
182
183 if ((vp->v_flag & VROOT) == 0)
984263bc 184 error = EINVAL;
fad57d0e
MD
185 nmp = VFSTONFS(vp->v_mount);
186 vput(vp);
984263bc 187 if (error)
3919ced0 188 goto done;
984263bc 189 if ((nmp->nm_state & NFSSTA_MNTD) &&
3919ced0
MD
190 (uap->flag & NFSSVC_GOTAUTH) == 0) {
191 error = 0;
192 goto done;
193 }
984263bc 194 nmp->nm_state |= NFSSTA_MNTD;
9910d07b 195 error = nfs_clientd(nmp, td->td_ucred, &ncd, uap->flag,
e07fef60 196 uap->argp, td);
984263bc
MD
197 } else if (uap->flag & NFSSVC_ADDSOCK) {
198 error = copyin(uap->argp, (caddr_t)&nfsdarg, sizeof(nfsdarg));
199 if (error)
3919ced0 200 goto done;
dadab5e9 201 error = holdsock(td->td_proc->p_fd, nfsdarg.sock, &fp);
984263bc 202 if (error)
3919ced0 203 goto done;
984263bc
MD
204 /*
205 * Get the client address for connected sockets.
206 */
207 if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
60233e58 208 nam = NULL;
984263bc
MD
209 else {
210 error = getsockaddr(&nam, nfsdarg.name,
211 nfsdarg.namelen);
212 if (error) {
9f87144f 213 fdrop(fp);
3919ced0 214 goto done;
984263bc
MD
215 }
216 }
dadab5e9 217 error = nfssvc_addsock(fp, nam, td);
9f87144f 218 fdrop(fp);
984263bc
MD
219 } else {
220 error = copyin(uap->argp, (caddr_t)nsd, sizeof (*nsd));
221 if (error)
3919ced0 222 goto done;
984263bc
MD
223 if ((uap->flag & NFSSVC_AUTHIN) &&
224 ((nfsd = nsd->nsd_nfsd)) != NULL &&
225 (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
226 slp = nfsd->nfsd_slp;
227
228 /*
229 * First check to see if another nfsd has already
230 * added this credential.
231 */
232 for (nuidp = NUIDHASH(slp,nsd->nsd_cr.cr_uid)->lh_first;
233 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
234 if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
235 (!nfsd->nfsd_nd->nd_nam2 ||
236 netaddr_match(NU_NETFAM(nuidp),
237 &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
238 break;
239 }
240 if (nuidp) {
241 nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
242 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
243 } else {
244 /*
245 * Nope, so we will.
246 */
247 if (slp->ns_numuids < nuidhash_max) {
248 slp->ns_numuids++;
249 nuidp = (struct nfsuid *)
efda3bd0 250 kmalloc(sizeof (struct nfsuid), M_NFSUID,
984263bc
MD
251 M_WAITOK);
252 } else
60233e58 253 nuidp = NULL;
984263bc
MD
254 if ((slp->ns_flag & SLP_VALID) == 0) {
255 if (nuidp)
efda3bd0 256 kfree((caddr_t)nuidp, M_NFSUID);
984263bc 257 } else {
60233e58 258 if (nuidp == NULL) {
ecd80f47 259 nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
984263bc
MD
260 LIST_REMOVE(nuidp, nu_hash);
261 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
262 nu_lru);
263 if (nuidp->nu_flag & NU_NAM)
264 FREE(nuidp->nu_nam, M_SONAME);
265 }
266 nuidp->nu_flag = 0;
267 nuidp->nu_cr = nsd->nsd_cr;
268 if (nuidp->nu_cr.cr_ngroups > NGROUPS)
269 nuidp->nu_cr.cr_ngroups = NGROUPS;
270 nuidp->nu_cr.cr_ref = 1;
271 nuidp->nu_timestamp = nsd->nsd_timestamp;
272 nuidp->nu_expire = time_second + nsd->nsd_ttl;
273 /*
274 * and save the session key in nu_key.
275 */
276 bcopy(nsd->nsd_key, nuidp->nu_key,
277 sizeof (nsd->nsd_key));
278 if (nfsd->nfsd_nd->nd_nam2) {
279 struct sockaddr_in *saddr;
280
281 saddr = (struct sockaddr_in *)
282 nfsd->nfsd_nd->nd_nam2;
283 switch (saddr->sin_family) {
284 case AF_INET:
285 nuidp->nu_flag |= NU_INETADDR;
286 nuidp->nu_inetaddr =
287 saddr->sin_addr.s_addr;
288 break;
289 case AF_ISO:
290 default:
291 nuidp->nu_flag |= NU_NAM;
292 nuidp->nu_nam =
cfa2ba21 293 dup_sockaddr(nfsd->nfsd_nd->nd_nam2);
984263bc
MD
294 break;
295 };
296 }
297 TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
298 nu_lru);
299 LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
300 nuidp, nu_hash);
301 nfsrv_setcred(&nuidp->nu_cr,
302 &nfsd->nfsd_nd->nd_cr);
303 nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
304 }
305 }
306 }
307 if ((uap->flag & NFSSVC_AUTHINFAIL) && (nfsd = nsd->nsd_nfsd))
308 nfsd->nfsd_flag |= NFSD_AUTHFAIL;
dadab5e9 309 error = nfssvc_nfsd(nsd, uap->argp, td);
984263bc
MD
310 }
311#endif /* NFS_NOSERVER */
312 if (error == EINTR || error == ERESTART)
313 error = 0;
3919ced0 314done:
c6b43e93 315 lwkt_reltoken(&nfs_token);
984263bc
MD
316 return (error);
317}
318
319#ifndef NFS_NOSERVER
320/*
321 * Adds a socket to the list for servicing by nfsds.
322 */
323static int
dadab5e9 324nfssvc_addsock(struct file *fp, struct sockaddr *mynam, struct thread *td)
984263bc 325{
dadab5e9
MD
326 int siz;
327 struct nfssvc_sock *slp;
328 struct socket *so;
165dba55 329 int error;
984263bc
MD
330
331 so = (struct socket *)fp->f_data;
332#if 0
60233e58 333 tslp = NULL;
984263bc
MD
334 /*
335 * Add it to the list, as required.
336 */
337 if (so->so_proto->pr_protocol == IPPROTO_UDP) {
338 tslp = nfs_udpsock;
339 if (tslp->ns_flag & SLP_VALID) {
340 if (mynam != NULL)
341 FREE(mynam, M_SONAME);
342 return (EPERM);
343 }
344 }
345#endif
e9855d6b
MD
346 /*
347 * Reserve buffer space in the socket. Note that due to bugs in
348 * Linux's delayed-ack code, serious performance degredation may
349 * occur with linux hosts if the minimum is used.
cc7d050e
MD
350 *
351 * NFS sockets are not limited to the standard sb_max or by
352 * resource limits.
e9855d6b 353 */
984263bc
MD
354 if (so->so_type == SOCK_STREAM)
355 siz = NFS_MAXPACKET + sizeof (u_long);
356 else
357 siz = NFS_MAXPACKET;
e9855d6b
MD
358 if (siz < nfs_soreserve)
359 siz = nfs_soreserve;
e9855d6b 360
cc7d050e 361 error = soreserve(so, siz, siz, NULL);
984263bc
MD
362 if (error) {
363 if (mynam != NULL)
364 FREE(mynam, M_SONAME);
365 return (error);
366 }
367
368 /*
369 * Set protocol specific options { for now TCP only } and
370 * reserve some space. For datagram sockets, this can get called
371 * repeatedly for the same socket, but that isn't harmful.
372 */
373 if (so->so_type == SOCK_STREAM) {
374 struct sockopt sopt;
375 int val;
376
377 bzero(&sopt, sizeof sopt);
378 sopt.sopt_level = SOL_SOCKET;
379 sopt.sopt_name = SO_KEEPALIVE;
380 sopt.sopt_val = &val;
381 sopt.sopt_valsize = sizeof val;
382 val = 1;
383 sosetopt(so, &sopt);
384 }
385 if (so->so_proto->pr_domain->dom_family == AF_INET &&
386 so->so_proto->pr_protocol == IPPROTO_TCP) {
387 struct sockopt sopt;
388 int val;
389
390 bzero(&sopt, sizeof sopt);
391 sopt.sopt_level = IPPROTO_TCP;
392 sopt.sopt_name = TCP_NODELAY;
393 sopt.sopt_val = &val;
394 sopt.sopt_valsize = sizeof val;
395 val = 1;
396 sosetopt(so, &sopt);
8fdd18a4
MD
397
398 bzero(&sopt, sizeof sopt);
399 sopt.sopt_level = IPPROTO_TCP;
400 sopt.sopt_name = TCP_FASTKEEP;
401 sopt.sopt_val = &val;
402 sopt.sopt_valsize = sizeof val;
403 val = 1;
404 sosetopt(so, &sopt);
984263bc 405 }
14343ad3 406 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_NOINTR);
6d49aa6f 407 so->so_rcv.ssb_timeo = 0;
14343ad3 408 atomic_clear_int(&so->so_snd.ssb_flags, SSB_NOINTR);
6d49aa6f 409 so->so_snd.ssb_timeo = 0;
984263bc 410
95b801b2 411 slp = kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
8684e6f9 412 mtx_init(&slp->ns_solock);
984263bc
MD
413 STAILQ_INIT(&slp->ns_rec);
414 TAILQ_INIT(&slp->ns_uidlruhead);
c6b43e93
MD
415 lwkt_token_init(&slp->ns_token, 1, "nfssrv_token");
416
417 lwkt_gettoken(&nfs_token);
c6b43e93 418 nfsrv_slpref(slp);
95b801b2 419 TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
c6b43e93 420 lwkt_gettoken(&slp->ns_token);
984263bc
MD
421
422 slp->ns_so = so;
423 slp->ns_nam = mynam;
424 fp->f_count++;
425 slp->ns_fp = fp;
c6b43e93 426
984263bc 427 so->so_upcallarg = (caddr_t)slp;
c6b43e93 428 so->so_upcall = nfsrv_rcv_upcall;
14343ad3 429 atomic_set_int(&so->so_rcv.ssb_flags, SSB_UPCALL);
984263bc 430 slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
52553028 431 nfsrv_wakenfsd(slp, 1);
c6b43e93
MD
432
433 lwkt_reltoken(&slp->ns_token);
434 lwkt_reltoken(&nfs_token);
435
984263bc
MD
436 return (0);
437}
438
439/*
440 * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
441 * until it is killed by a signal.
442 */
443static int
dadab5e9 444nfssvc_nfsd(struct nfsd_srvargs *nsd, caddr_t argp, struct thread *td)
984263bc 445{
dadab5e9
MD
446 int siz;
447 struct nfssvc_sock *slp;
984263bc
MD
448 struct nfsd *nfsd = nsd->nsd_nfsd;
449 struct nfsrv_descript *nd = NULL;
450 struct mbuf *m, *mreq;
95b801b2 451 int error, cacherep, sotype, writes_todo;
984263bc
MD
452 int procrastinate;
453 u_quad_t cur_usec;
454
455#ifndef nolint
456 cacherep = RC_DOIT;
457 writes_todo = 0;
458#endif
c6b43e93
MD
459 lwkt_gettoken(&nfs_token);
460
60233e58 461 if (nfsd == NULL) {
984263bc 462 nsd->nsd_nfsd = nfsd = (struct nfsd *)
e7b4468c 463 kmalloc(sizeof (struct nfsd), M_NFSD, M_WAITOK|M_ZERO);
dadab5e9 464 nfsd->nfsd_td = td;
984263bc
MD
465 TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
466 nfs_numnfsd++;
c6b43e93 467 }
984263bc
MD
468
469 /*
470 * Loop getting rpc requests until SIGKILL.
471 */
472 for (;;) {
473 if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
52553028 474 while (nfsd->nfsd_slp == NULL &&
984263bc
MD
475 (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
476 nfsd->nfsd_flag |= NFSD_WAITING;
477 nfsd_waiting++;
95b801b2 478 error = tsleep(nfsd, PCATCH, "nfsd", 0);
984263bc 479 nfsd_waiting--;
95b801b2 480 if (error && nfsd->nfsd_slp == NULL)
984263bc
MD
481 goto done;
482 }
52553028 483 if (nfsd->nfsd_slp == NULL &&
95b801b2 484 (nfsd_head_flag & NFSD_CHECKSLP)) {
ecd80f47 485 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
95b801b2
MD
486 if ((slp->ns_flag & SLP_ACTION_MASK) ||
487 slp->ns_needq_upcall) {
c6b43e93 488 nfsrv_slpref(slp);
984263bc
MD
489 nfsd->nfsd_slp = slp;
490 break;
491 }
492 }
95b801b2 493 if (slp == NULL)
984263bc
MD
494 nfsd_head_flag &= ~NFSD_CHECKSLP;
495 }
60233e58 496 if ((slp = nfsd->nfsd_slp) == NULL)
984263bc 497 continue;
c6b43e93
MD
498
499 lwkt_reltoken(&nfs_token);
500 lwkt_gettoken(&slp->ns_token);
501
95b801b2
MD
502 if (slp->ns_needq_upcall) {
503 slp->ns_needq_upcall = 0;
504 slp->ns_flag |= SLP_NEEDQ;
505 }
506
984263bc 507 if (slp->ns_flag & SLP_VALID) {
95b801b2
MD
508 /*
509 * We can both process additional received
510 * data into new records and process existing
511 * records. This keeps the pipeline hot by
512 * allowing the tcp socket to continue to
513 * drain while we are processing records.
514 */
515 if (slp->ns_flag & SLP_DISCONN) {
984263bc 516 nfsrv_zapsock(slp);
95b801b2
MD
517 } else if (slp->ns_flag & SLP_NEEDQ) {
518 (void)nfs_slplock(slp, 1);
984263bc 519 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
95b801b2 520 MB_WAIT);
984263bc
MD
521 nfs_slpunlock(slp);
522 }
523 error = nfsrv_dorec(slp, nfsd, &nd);
524 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;
95b801b2 530 } else {
984263bc 531 writes_todo = 0;
95b801b2 532 }
984263bc 533 nfsd->nfsd_flag |= NFSD_REQINPROG;
95b801b2
MD
534 } else {
535 slp->ns_flag &= ~SLP_ACTION_MASK;
536 error = 0;
984263bc
MD
537 }
538 } else {
539 error = 0;
540 slp = nfsd->nfsd_slp;
95b801b2 541
c6b43e93
MD
542 lwkt_reltoken(&nfs_token);
543 lwkt_gettoken(&slp->ns_token);
95b801b2
MD
544
545 if (slp->ns_needq_upcall) {
546 slp->ns_needq_upcall = 0;
547 slp->ns_flag |= SLP_NEEDQ;
548 }
549 if (NFSRV_RECLIMIT(slp) == 0 &&
550 (slp->ns_flag & SLP_NEEDQ)) {
551 (void)nfs_slplock(slp, 1);
552 nfsrv_rcv(slp->ns_so, (caddr_t)slp,
553 MB_WAIT);
554 nfs_slpunlock(slp);
555 }
984263bc 556 }
c6b43e93
MD
557
558 /*
559 * nfs_token not held here. slp token is held.
560 */
984263bc
MD
561 if (error || (slp->ns_flag & SLP_VALID) == 0) {
562 if (nd) {
efda3bd0 563 kfree((caddr_t)nd, M_NFSRVDESC);
984263bc
MD
564 nd = NULL;
565 }
984263bc 566 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
95b801b2
MD
567 if (slp->ns_flag & SLP_ACTION_MASK) {
568 lwkt_reltoken(&slp->ns_token);
569 lwkt_gettoken(&nfs_token);
570 } else {
571 nfsd->nfsd_slp = NULL;
572 lwkt_reltoken(&slp->ns_token);
573 lwkt_gettoken(&nfs_token);
574 nfsrv_slpderef(slp);
575 }
984263bc
MD
576 continue;
577 }
c6b43e93
MD
578
579 /*
95b801b2
MD
580 * Execute the NFS request - handle the server side cache
581 *
c6b43e93
MD
582 * nfs_token not held here. slp token is held.
583 */
984263bc
MD
584 sotype = slp->ns_so->so_type;
585 if (nd) {
586 getmicrotime(&nd->nd_starttime);
587 if (nd->nd_nam2)
588 nd->nd_nam = nd->nd_nam2;
589 else
590 nd->nd_nam = slp->ns_nam;
591
592 /*
593 * Check to see if authorization is needed.
594 */
595 if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
596 nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
597 nsd->nsd_haddr =
598 ((struct sockaddr_in *)
599 nd->nd_nam)->sin_addr.s_addr;
600 nsd->nsd_authlen = nfsd->nfsd_authlen;
601 nsd->nsd_verflen = nfsd->nfsd_verflen;
602 if (!copyout(nfsd->nfsd_authstr,nsd->nsd_authstr,
603 nfsd->nfsd_authlen) &&
604 !copyout(nfsd->nfsd_verfstr, nsd->nsd_verfstr,
605 nfsd->nfsd_verflen) &&
606 !copyout((caddr_t)nsd, argp, sizeof (*nsd)))
c6b43e93 607 lwkt_reltoken(&slp->ns_token);
984263bc
MD
608 return (ENEEDAUTH);
609 cacherep = RC_DROPIT;
e07fef60 610 } else {
984263bc 611 cacherep = nfsrv_getcache(nd, slp, &mreq);
984263bc 612 }
e07fef60
MD
613
614 if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
984263bc
MD
615 nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
616 nd->nd_procnum = NFSPROC_NOOP;
617 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
618 cacherep = RC_DOIT;
619 } else if (nfs_privport) {
620 /* Check if source port is privileged */
621 u_short port;
622 struct sockaddr *nam = nd->nd_nam;
623 struct sockaddr_in *sin;
624
625 sin = (struct sockaddr_in *)nam;
626 port = ntohs(sin->sin_port);
627 if (port >= IPPORT_RESERVED &&
628 nd->nd_procnum != NFSPROC_NULL) {
629 nd->nd_procnum = NFSPROC_NOOP;
630 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
631 cacherep = RC_DOIT;
086c1d7e 632 kprintf("NFS request from unprivileged port (%s:%d)\n",
984263bc
MD
633 inet_ntoa(sin->sin_addr), port);
634 }
635 }
636
637 }
638
639 /*
95b801b2
MD
640 * Execute the NFS request - direct execution
641 *
c6b43e93 642 * Loop to get all the write rpc replies that have been
984263bc 643 * gathered together.
c6b43e93
MD
644 *
645 * nfs_token not held here. slp token is held.
984263bc
MD
646 */
647 do {
648 switch (cacherep) {
649 case RC_DOIT:
650 if (nd && (nd->nd_flag & ND_NFSV3))
651 procrastinate = nfsrvw_procrastinate_v3;
652 else
653 procrastinate = nfsrvw_procrastinate;
654 if (writes_todo || (nd->nd_procnum == NFSPROC_WRITE &&
e07fef60
MD
655 procrastinate > 0)
656 ) {
984263bc 657 error = nfsrv_writegather(&nd, slp,
95b801b2 658 nfsd->nfsd_td, &mreq);
e07fef60 659 } else {
95b801b2 660 /* NOT YET lwkt_reltoken(&slp->ns_token); */
984263bc 661 error = (*(nfsrv3_procs[nd->nd_procnum]))(nd,
95b801b2
MD
662 slp, nfsd->nfsd_td, &mreq);
663 /* NOT YET lwkt_gettoken(&slp->ns_token); */
e07fef60 664 }
984263bc
MD
665 if (mreq == NULL)
666 break;
667 if (error != 0 && error != NFSERR_RETVOID) {
668 if (nd->nd_procnum != NQNFSPROC_VACATED)
669 nfsstats.srv_errs++;
670 nfsrv_updatecache(nd, FALSE, mreq);
671 if (nd->nd_nam2)
672 FREE(nd->nd_nam2, M_SONAME);
673 break;
674 }
675 nfsstats.srvrpccnt[nd->nd_procnum]++;
676 nfsrv_updatecache(nd, TRUE, mreq);
60233e58 677 nd->nd_mrep = NULL;
95b801b2 678 /* FALL THROUGH */
984263bc
MD
679 case RC_REPLY:
680 m = mreq;
681 siz = 0;
682 while (m) {
683 siz += m->m_len;
684 m = m->m_next;
685 }
686 if (siz <= 0 || siz > NFS_MAXPACKET) {
086c1d7e 687 kprintf("mbuf siz=%d\n",siz);
984263bc
MD
688 panic("Bad nfs svc reply");
689 }
690 m = mreq;
691 m->m_pkthdr.len = siz;
60233e58 692 m->m_pkthdr.rcvif = NULL;
984263bc
MD
693 /*
694 * For stream protocols, prepend a Sun RPC
695 * Record Mark.
696 */
697 if (sotype == SOCK_STREAM) {
74f1caca 698 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
95b801b2
MD
699 if (m == NULL) {
700 error = ENOBUFS;
701 goto skip;
702 }
984263bc
MD
703 *mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
704 }
705 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
706 (void) nfs_slplock(slp, 1);
707 if (slp->ns_flag & SLP_VALID)
708 error = nfs_send(slp->ns_so, nd->nd_nam2, m, NULL);
709 else {
710 error = EPIPE;
711 m_freem(m);
712 }
95b801b2 713skip:
984263bc
MD
714 if (nfsrtton)
715 nfsd_rt(sotype, nd, cacherep);
716 if (nd->nd_nam2)
717 FREE(nd->nd_nam2, M_SONAME);
718 if (nd->nd_mrep)
719 m_freem(nd->nd_mrep);
95b801b2 720 if (error == EPIPE || error == ENOBUFS)
984263bc
MD
721 nfsrv_zapsock(slp);
722 if (slp->ns_so->so_proto->pr_flags & PR_CONNREQUIRED)
723 nfs_slpunlock(slp);
724 if (error == EINTR || error == ERESTART) {
efda3bd0 725 kfree((caddr_t)nd, M_NFSRVDESC);
c6b43e93
MD
726 lwkt_reltoken(&slp->ns_token);
727 lwkt_gettoken(&nfs_token);
95b801b2 728 nfsd->nfsd_slp = NULL;
984263bc 729 nfsrv_slpderef(slp);
984263bc
MD
730 goto done;
731 }
732 break;
733 case RC_DROPIT:
734 if (nfsrtton)
735 nfsd_rt(sotype, nd, cacherep);
736 m_freem(nd->nd_mrep);
737 if (nd->nd_nam2)
738 FREE(nd->nd_nam2, M_SONAME);
739 break;
740 };
741 if (nd) {
742 FREE((caddr_t)nd, M_NFSRVDESC);
743 nd = NULL;
744 }
745
746 /*
747 * Check to see if there are outstanding writes that
748 * need to be serviced.
749 */
750 cur_usec = nfs_curusec();
984263bc
MD
751 if (slp->ns_tq.lh_first &&
752 slp->ns_tq.lh_first->nd_time <= cur_usec) {
753 cacherep = RC_DOIT;
754 writes_todo = 1;
c6b43e93 755 } else {
984263bc 756 writes_todo = 0;
c6b43e93 757 }
984263bc 758 } while (writes_todo);
c6b43e93
MD
759
760 /*
761 * nfs_token not held here. slp token is held.
762 */
984263bc
MD
763 if (nfsrv_dorec(slp, nfsd, &nd)) {
764 nfsd->nfsd_flag &= ~NFSD_REQINPROG;
95b801b2
MD
765 if (slp->ns_flag & SLP_ACTION_MASK) {
766 lwkt_reltoken(&slp->ns_token);
767 lwkt_gettoken(&nfs_token);
768 } else {
769 nfsd->nfsd_slp = NULL;
770 lwkt_reltoken(&slp->ns_token);
771 lwkt_gettoken(&nfs_token);
772 nfsrv_slpderef(slp);
773 }
c6b43e93
MD
774 } else {
775 lwkt_reltoken(&slp->ns_token);
776 lwkt_gettoken(&nfs_token);
984263bc
MD
777 }
778 }
779done:
780 TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
efda3bd0 781 kfree((caddr_t)nfsd, M_NFSD);
60233e58 782 nsd->nsd_nfsd = NULL;
984263bc
MD
783 if (--nfs_numnfsd == 0)
784 nfsrv_init(TRUE); /* Reinitialize everything */
c6b43e93
MD
785
786 lwkt_reltoken(&nfs_token);
984263bc
MD
787 return (error);
788}
789
790/*
791 * Shut down a socket associated with an nfssvc_sock structure.
792 * Should be called with the send lock set, if required.
793 * The trick here is to increment the sref at the start, so that the nfsds
794 * will stop using it and clear ns_flag at the end so that it will not be
795 * reassigned during cleanup.
796 */
797static void
e851b29e 798nfsrv_zapsock(struct nfssvc_sock *slp)
984263bc 799{
40393ded
RG
800 struct nfsuid *nuidp, *nnuidp;
801 struct nfsrv_descript *nwp, *nnwp;
984263bc
MD
802 struct socket *so;
803 struct file *fp;
804 struct nfsrv_rec *rec;
984263bc
MD
805
806 slp->ns_flag &= ~SLP_ALLFLAGS;
807 fp = slp->ns_fp;
808 if (fp) {
60233e58 809 slp->ns_fp = NULL;
984263bc 810 so = slp->ns_so;
14343ad3 811 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_UPCALL);
984263bc
MD
812 so->so_upcall = NULL;
813 so->so_upcallarg = NULL;
e5d03018 814 soshutdown(so, SHUT_RDWR);
dadab5e9 815 closef(fp, NULL);
984263bc
MD
816 if (slp->ns_nam)
817 FREE(slp->ns_nam, M_SONAME);
818 m_freem(slp->ns_raw);
819 while ((rec = STAILQ_FIRST(&slp->ns_rec)) != NULL) {
52553028 820 --slp->ns_numrec;
984263bc
MD
821 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
822 if (rec->nr_address)
823 FREE(rec->nr_address, M_SONAME);
824 m_freem(rec->nr_packet);
efda3bd0 825 kfree(rec, M_NFSRVDESC);
984263bc 826 }
95b801b2
MD
827 KKASSERT(slp->ns_numrec == 0);
828
ecd80f47
JS
829 TAILQ_FOREACH_MUTABLE(nuidp, &slp->ns_uidlruhead, nu_lru,
830 nnuidp) {
984263bc
MD
831 LIST_REMOVE(nuidp, nu_hash);
832 TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
833 if (nuidp->nu_flag & NU_NAM)
834 FREE(nuidp->nu_nam, M_SONAME);
efda3bd0 835 kfree((caddr_t)nuidp, M_NFSUID);
984263bc 836 }
165dba55 837 crit_enter();
984263bc
MD
838 for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
839 nnwp = nwp->nd_tq.le_next;
840 LIST_REMOVE(nwp, nd_tq);
efda3bd0 841 kfree((caddr_t)nwp, M_NFSRVDESC);
984263bc
MD
842 }
843 LIST_INIT(&slp->ns_tq);
165dba55 844 crit_exit();
95b801b2 845 nfsrv_slpderef(slp);
984263bc
MD
846 }
847}
848
849/*
850 * Derefence a server socket structure. If it has no more references and
851 * is no longer valid, you can throw it away.
c6b43e93
MD
852 *
853 * Must be holding nfs_token!
984263bc
MD
854 */
855void
e851b29e 856nfsrv_slpderef(struct nfssvc_sock *slp)
984263bc 857{
c6b43e93 858 ASSERT_LWKT_TOKEN_HELD(&nfs_token);
95b801b2
MD
859 if (slp->ns_sref == 1) {
860 KKASSERT((slp->ns_flag & SLP_VALID) == 0);
984263bc 861 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
95b801b2 862 slp->ns_sref = 0;
efda3bd0 863 kfree((caddr_t)slp, M_NFSSVC);
95b801b2
MD
864 } else {
865 --slp->ns_sref;
984263bc
MD
866 }
867}
868
c6b43e93
MD
869void
870nfsrv_slpref(struct nfssvc_sock *slp)
871{
872 ASSERT_LWKT_TOKEN_HELD(&nfs_token);
873 ++slp->ns_sref;
874}
875
984263bc
MD
876/*
877 * Lock a socket against others.
8684e6f9
MD
878 *
879 * Returns 0 on failure, 1 on success.
984263bc
MD
880 */
881int
e851b29e 882nfs_slplock(struct nfssvc_sock *slp, int wait)
984263bc 883{
8684e6f9 884 mtx_t mtx = &slp->ns_solock;
984263bc 885
8684e6f9
MD
886 if (wait) {
887 mtx_lock_ex(mtx, "nfsslplck", 0, 0);
888 return(1);
889 } else if (mtx_lock_ex_try(mtx) == 0) {
890 return(1);
891 } else {
892 return(0);
984263bc 893 }
984263bc
MD
894}
895
896/*
897 * Unlock the stream socket for others.
898 */
899void
e851b29e 900nfs_slpunlock(struct nfssvc_sock *slp)
984263bc 901{
8684e6f9
MD
902 mtx_t mtx = &slp->ns_solock;
903
904 mtx_unlock(mtx);
984263bc
MD
905}
906
907/*
908 * Initialize the data structures for the server.
909 * Handshake with any new nfsds starting up to avoid any chance of
910 * corruption.
911 */
912void
e851b29e 913nfsrv_init(int terminating)
984263bc 914{
40393ded 915 struct nfssvc_sock *slp, *nslp;
984263bc 916
c6b43e93 917 lwkt_gettoken(&nfs_token);
984263bc
MD
918 if (nfssvc_sockhead_flag & SLP_INIT)
919 panic("nfsd init");
920 nfssvc_sockhead_flag |= SLP_INIT;
c6b43e93 921
984263bc 922 if (terminating) {
ecd80f47 923 TAILQ_FOREACH_MUTABLE(slp, &nfssvc_sockhead, ns_chain, nslp) {
984263bc
MD
924 if (slp->ns_flag & SLP_VALID)
925 nfsrv_zapsock(slp);
95b801b2 926 /*
984263bc 927 TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
efda3bd0 928 kfree((caddr_t)slp, M_NFSSVC);
95b801b2 929 */
984263bc
MD
930 }
931 nfsrv_cleancache(); /* And clear out server cache */
c6b43e93 932 } else {
984263bc 933 nfs_pub.np_valid = 0;
c6b43e93 934 }
984263bc
MD
935
936 TAILQ_INIT(&nfssvc_sockhead);
937 nfssvc_sockhead_flag &= ~SLP_INIT;
938 if (nfssvc_sockhead_flag & SLP_WANTINIT) {
939 nfssvc_sockhead_flag &= ~SLP_WANTINIT;
940 wakeup((caddr_t)&nfssvc_sockhead);
941 }
942
943 TAILQ_INIT(&nfsd_head);
944 nfsd_head_flag &= ~NFSD_CHECKSLP;
945
c6b43e93
MD
946 lwkt_reltoken(&nfs_token);
947
984263bc
MD
948#if 0
949 nfs_udpsock = (struct nfssvc_sock *)
e7b4468c 950 kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
8684e6f9 951 mtx_init(&nfs_udpsock->ns_solock);
984263bc
MD
952 STAILQ_INIT(&nfs_udpsock->ns_rec);
953 TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
954 TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
955
956 nfs_cltpsock = (struct nfssvc_sock *)
e7b4468c 957 kmalloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK | M_ZERO);
8684e6f9 958 mtx_init(&nfs_cltpsock->ns_solock);
984263bc
MD
959 STAILQ_INIT(&nfs_cltpsock->ns_rec);
960 TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
961 TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
962#endif
963}
964
965/*
966 * Add entries to the server monitor log.
967 */
968static void
e851b29e 969nfsd_rt(int sotype, struct nfsrv_descript *nd, int cacherep)
984263bc 970{
40393ded 971 struct drt *rt;
984263bc
MD
972
973 rt = &nfsdrt.drt[nfsdrt.pos];
974 if (cacherep == RC_DOIT)
975 rt->flag = 0;
976 else if (cacherep == RC_REPLY)
977 rt->flag = DRT_CACHEREPLY;
978 else
979 rt->flag = DRT_CACHEDROP;
980 if (sotype == SOCK_STREAM)
981 rt->flag |= DRT_TCP;
e07fef60 982 if (nd->nd_flag & ND_NFSV3)
984263bc
MD
983 rt->flag |= DRT_NFSV3;
984 rt->proc = nd->nd_procnum;
985 if (nd->nd_nam->sa_family == AF_INET)
986 rt->ipadr = ((struct sockaddr_in *)nd->nd_nam)->sin_addr.s_addr;
987 else
988 rt->ipadr = INADDR_ANY;
989 rt->resptime = nfs_curusec() - (nd->nd_starttime.tv_sec * 1000000 + nd->nd_starttime.tv_usec);
990 getmicrotime(&rt->tstamp);
991 nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
992}
993#endif /* NFS_NOSERVER */
994
984263bc 995/*
984263bc
MD
996 * Get an authorization string for the uid by having the mount_nfs sitting
997 * on this mount point porpous out of the kernel and do it.
998 */
999int
e851b29e
CP
1000nfs_getauth(struct nfsmount *nmp, struct nfsreq *rep,
1001 struct ucred *cred, char **auth_str, int *auth_len, char *verf_str,
1002 int *verf_len, NFSKERBKEY_T key /* return session key */)
984263bc
MD
1003{
1004 int error = 0;
1005
1006 while ((nmp->nm_state & NFSSTA_WAITAUTH) == 0) {
1007 nmp->nm_state |= NFSSTA_WANTAUTH;
377d4740 1008 (void) tsleep((caddr_t)&nmp->nm_authtype, 0,
984263bc 1009 "nfsauth1", 2 * hz);
dadab5e9 1010 error = nfs_sigintr(nmp, rep, rep->r_td);
984263bc
MD
1011 if (error) {
1012 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1013 return (error);
1014 }
1015 }
1016 nmp->nm_state &= ~(NFSSTA_WAITAUTH | NFSSTA_WANTAUTH);
efda3bd0 1017 nmp->nm_authstr = *auth_str = (char *)kmalloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
984263bc
MD
1018 nmp->nm_authlen = RPCAUTH_MAXSIZ;
1019 nmp->nm_verfstr = verf_str;
1020 nmp->nm_verflen = *verf_len;
1021 nmp->nm_authuid = cred->cr_uid;
1022 wakeup((caddr_t)&nmp->nm_authstr);
1023
1024 /*
1025 * And wait for mount_nfs to do its stuff.
1026 */
1027 while ((nmp->nm_state & NFSSTA_HASAUTH) == 0 && error == 0) {
377d4740 1028 (void) tsleep((caddr_t)&nmp->nm_authlen, 0,
984263bc 1029 "nfsauth2", 2 * hz);
dadab5e9 1030 error = nfs_sigintr(nmp, rep, rep->r_td);
984263bc
MD
1031 }
1032 if (nmp->nm_state & NFSSTA_AUTHERR) {
1033 nmp->nm_state &= ~NFSSTA_AUTHERR;
1034 error = EAUTH;
1035 }
1036 if (error)
efda3bd0 1037 kfree((caddr_t)*auth_str, M_TEMP);
984263bc
MD
1038 else {
1039 *auth_len = nmp->nm_authlen;
1040 *verf_len = nmp->nm_verflen;
1041 bcopy((caddr_t)nmp->nm_key, (caddr_t)key, sizeof (key));
1042 }
1043 nmp->nm_state &= ~NFSSTA_HASAUTH;
1044 nmp->nm_state |= NFSSTA_WAITAUTH;
1045 if (nmp->nm_state & NFSSTA_WANTAUTH) {
1046 nmp->nm_state &= ~NFSSTA_WANTAUTH;
1047 wakeup((caddr_t)&nmp->nm_authtype);
1048 }
1049 return (error);
1050}
1051
1052/*
1053 * Get a nickname authenticator and verifier.
1054 */
1055int
e851b29e
CP
1056nfs_getnickauth(struct nfsmount *nmp, struct ucred *cred, char **auth_str,
1057 int *auth_len, char *verf_str, int verf_len)
984263bc 1058{
40393ded
RG
1059 struct nfsuid *nuidp;
1060 u_int32_t *nickp, *verfp;
984263bc
MD
1061 struct timeval ktvin, ktvout;
1062
1063#ifdef DIAGNOSTIC
1064 if (verf_len < (4 * NFSX_UNSIGNED))
1065 panic("nfs_getnickauth verf too small");
1066#endif
1067 for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
1068 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1069 if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1070 break;
1071 }
1072 if (!nuidp || nuidp->nu_expire < time_second)
1073 return (EACCES);
1074
1075 /*
1076 * Move to the end of the lru list (end of lru == most recently used).
1077 */
1078 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1079 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1080
efda3bd0 1081 nickp = (u_int32_t *)kmalloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
984263bc
MD
1082 *nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1083 *nickp = txdr_unsigned(nuidp->nu_nickname);
1084 *auth_str = (char *)nickp;
1085 *auth_len = 2 * NFSX_UNSIGNED;
1086
1087 /*
1088 * Now we must encrypt the verifier and package it up.
1089 */
1090 verfp = (u_int32_t *)verf_str;
1091 *verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1092 if (time_second > nuidp->nu_timestamp.tv_sec ||
1093 (time_second == nuidp->nu_timestamp.tv_sec &&
1094 time_second > nuidp->nu_timestamp.tv_usec))
1095 getmicrotime(&nuidp->nu_timestamp);
1096 else
1097 nuidp->nu_timestamp.tv_usec++;
1098 ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1099 ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1100
1101 /*
1102 * Now encrypt the timestamp verifier in ecb mode using the session
1103 * key.
1104 */
1105#ifdef NFSKERB
1106 XXX
d557216f
MD
1107#else
1108 ktvout.tv_sec = 0;
1109 ktvout.tv_usec = 0;
984263bc
MD
1110#endif
1111
1112 *verfp++ = ktvout.tv_sec;
1113 *verfp++ = ktvout.tv_usec;
1114 *verfp = 0;
1115 return (0);
1116}
1117
1118/*
1119 * Save the current nickname in a hash list entry on the mount point.
1120 */
1121int
e851b29e
CP
1122nfs_savenickauth(struct nfsmount *nmp, struct ucred *cred, int len,
1123 NFSKERBKEY_T key, struct mbuf **mdp, char **dposp,
1124 struct mbuf *mrep)
984263bc 1125{
40393ded
RG
1126 struct nfsuid *nuidp;
1127 u_int32_t *tl;
984263bc
MD
1128 struct timeval ktvin, ktvout;
1129 u_int32_t nick;
984263bc 1130 int deltasec, error = 0;
42edf14f
MD
1131 struct nfsm_info info;
1132
1133 info.md = *mdp;
1134 info.dpos = *dposp;
1135 info.mrep = mrep;
984263bc
MD
1136
1137 if (len == (3 * NFSX_UNSIGNED)) {
42edf14f 1138 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
984263bc
MD
1139 ktvin.tv_sec = *tl++;
1140 ktvin.tv_usec = *tl++;
1141 nick = fxdr_unsigned(u_int32_t, *tl);
1142
1143 /*
1144 * Decrypt the timestamp in ecb mode.
1145 */
1146#ifdef NFSKERB
1147 XXX
d557216f
MD
1148#else
1149 ktvout.tv_sec = 0;
1150 ktvout.tv_usec = 0;
984263bc
MD
1151#endif
1152 ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1153 ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1154 deltasec = time_second - ktvout.tv_sec;
1155 if (deltasec < 0)
1156 deltasec = -deltasec;
1157 /*
1158 * If ok, add it to the hash list for the mount point.
1159 */
1160 if (deltasec <= NFS_KERBCLOCKSKEW) {
1161 if (nmp->nm_numuids < nuidhash_max) {
1162 nmp->nm_numuids++;
1163 nuidp = (struct nfsuid *)
efda3bd0 1164 kmalloc(sizeof (struct nfsuid), M_NFSUID,
984263bc
MD
1165 M_WAITOK);
1166 } else {
ecd80f47 1167 nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
984263bc
MD
1168 LIST_REMOVE(nuidp, nu_hash);
1169 TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1170 nu_lru);
1171 }
1172 nuidp->nu_flag = 0;
1173 nuidp->nu_cr.cr_uid = cred->cr_uid;
1174 nuidp->nu_expire = time_second + NFS_KERBTTL;
1175 nuidp->nu_timestamp = ktvout;
1176 nuidp->nu_nickname = nick;
1177 bcopy(key, nuidp->nu_key, sizeof (key));
1178 TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1179 nu_lru);
1180 LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1181 nuidp, nu_hash);
1182 }
42edf14f
MD
1183 } else {
1184 ERROROUT(nfsm_adv(&info, nfsm_rndup(len)));
1185 }
984263bc 1186nfsmout:
42edf14f
MD
1187 *mdp = info.md;
1188 *dposp = info.dpos;
984263bc
MD
1189 return (error);
1190}