]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_subs.c
xnu-2422.90.20.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_subs.c
1 /*
2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
65 * FreeBSD-Id: nfs_subs.c,v 1.47 1997/11/07 08:53:24 phk Exp $
66 */
67
68 /*
69 * These functions support the macros and help fiddle mbuf chains for
70 * the nfs op functions. They do things like create the rpc header and
71 * copy data between mbuf chains and uio lists.
72 */
73 #include <sys/param.h>
74 #include <sys/proc.h>
75 #include <sys/kauth.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/mount_internal.h>
79 #include <sys/vnode_internal.h>
80 #include <sys/kpi_mbuf.h>
81 #include <sys/socket.h>
82 #include <sys/stat.h>
83 #include <sys/malloc.h>
84 #include <sys/syscall.h>
85 #include <sys/ubc_internal.h>
86 #include <sys/fcntl.h>
87 #include <sys/uio.h>
88 #include <sys/domain.h>
89 #include <libkern/OSAtomic.h>
90 #include <kern/thread_call.h>
91
92 #include <sys/vm.h>
93 #include <sys/vmparam.h>
94
95 #include <sys/time.h>
96 #include <kern/clock.h>
97
98 #include <nfs/rpcv2.h>
99 #include <nfs/nfsproto.h>
100 #include <nfs/nfs.h>
101 #include <nfs/nfsnode.h>
102 #if NFSCLIENT
103 #define _NFS_XDR_SUBS_FUNCS_ /* define this to get xdrbuf function definitions */
104 #endif
105 #include <nfs/xdr_subs.h>
106 #include <nfs/nfsm_subs.h>
107 #include <nfs/nfs_gss.h>
108 #include <nfs/nfsmount.h>
109 #include <nfs/nfs_lock.h>
110
111 #include <miscfs/specfs/specdev.h>
112
113 #include <netinet/in.h>
114 #include <net/kpi_interface.h>
115
116 #include <sys/utfconv.h>
117
118 /*
119 * NFS globals
120 */
121 struct nfsstats __attribute__((aligned(8))) nfsstats;
122 size_t nfs_mbuf_mhlen = 0, nfs_mbuf_minclsize = 0;
123
124 /*
125 * functions to convert between NFS and VFS types
126 */
127 nfstype
128 vtonfs_type(enum vtype vtype, int nfsvers)
129 {
130 switch (vtype) {
131 case VNON:
132 return NFNON;
133 case VREG:
134 return NFREG;
135 case VDIR:
136 return NFDIR;
137 case VBLK:
138 return NFBLK;
139 case VCHR:
140 return NFCHR;
141 case VLNK:
142 return NFLNK;
143 case VSOCK:
144 if (nfsvers > NFS_VER2)
145 return NFSOCK;
146 case VFIFO:
147 if (nfsvers > NFS_VER2)
148 return NFFIFO;
149 case VBAD:
150 case VSTR:
151 case VCPLX:
152 default:
153 return NFNON;
154 }
155 }
156
157 enum vtype
158 nfstov_type(nfstype nvtype, int nfsvers)
159 {
160 switch (nvtype) {
161 case NFNON:
162 return VNON;
163 case NFREG:
164 return VREG;
165 case NFDIR:
166 return VDIR;
167 case NFBLK:
168 return VBLK;
169 case NFCHR:
170 return VCHR;
171 case NFLNK:
172 return VLNK;
173 case NFSOCK:
174 if (nfsvers > NFS_VER2)
175 return VSOCK;
176 case NFFIFO:
177 if (nfsvers > NFS_VER2)
178 return VFIFO;
179 case NFATTRDIR:
180 if (nfsvers > NFS_VER3)
181 return VDIR;
182 case NFNAMEDATTR:
183 if (nfsvers > NFS_VER3)
184 return VREG;
185 default:
186 return VNON;
187 }
188 }
189
190 int
191 vtonfsv2_mode(enum vtype vtype, mode_t m)
192 {
193 switch (vtype) {
194 case VNON:
195 case VREG:
196 case VDIR:
197 case VBLK:
198 case VCHR:
199 case VLNK:
200 case VSOCK:
201 return vnode_makeimode(vtype, m);
202 case VFIFO:
203 return vnode_makeimode(VCHR, m);
204 case VBAD:
205 case VSTR:
206 case VCPLX:
207 default:
208 return vnode_makeimode(VNON, m);
209 }
210 }
211
212 #if NFSSERVER
213
214 /*
215 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
216 */
217 int nfsv3_procid[NFS_NPROCS] = {
218 NFSPROC_NULL,
219 NFSPROC_GETATTR,
220 NFSPROC_SETATTR,
221 NFSPROC_NOOP,
222 NFSPROC_LOOKUP,
223 NFSPROC_READLINK,
224 NFSPROC_READ,
225 NFSPROC_NOOP,
226 NFSPROC_WRITE,
227 NFSPROC_CREATE,
228 NFSPROC_REMOVE,
229 NFSPROC_RENAME,
230 NFSPROC_LINK,
231 NFSPROC_SYMLINK,
232 NFSPROC_MKDIR,
233 NFSPROC_RMDIR,
234 NFSPROC_READDIR,
235 NFSPROC_FSSTAT,
236 NFSPROC_NOOP,
237 NFSPROC_NOOP,
238 NFSPROC_NOOP,
239 NFSPROC_NOOP,
240 NFSPROC_NOOP
241 };
242
243 #endif /* NFSSERVER */
244
245 /*
246 * and the reverse mapping from generic to Version 2 procedure numbers
247 */
248 int nfsv2_procid[NFS_NPROCS] = {
249 NFSV2PROC_NULL,
250 NFSV2PROC_GETATTR,
251 NFSV2PROC_SETATTR,
252 NFSV2PROC_LOOKUP,
253 NFSV2PROC_NOOP,
254 NFSV2PROC_READLINK,
255 NFSV2PROC_READ,
256 NFSV2PROC_WRITE,
257 NFSV2PROC_CREATE,
258 NFSV2PROC_MKDIR,
259 NFSV2PROC_SYMLINK,
260 NFSV2PROC_CREATE,
261 NFSV2PROC_REMOVE,
262 NFSV2PROC_RMDIR,
263 NFSV2PROC_RENAME,
264 NFSV2PROC_LINK,
265 NFSV2PROC_READDIR,
266 NFSV2PROC_NOOP,
267 NFSV2PROC_STATFS,
268 NFSV2PROC_NOOP,
269 NFSV2PROC_NOOP,
270 NFSV2PROC_NOOP,
271 NFSV2PROC_NOOP
272 };
273
274
275 /*
276 * initialize NFS's cache of mbuf constants
277 */
278 void
279 nfs_mbuf_init(void)
280 {
281 struct mbuf_stat ms;
282
283 mbuf_stats(&ms);
284 nfs_mbuf_mhlen = ms.mhlen;
285 nfs_mbuf_minclsize = ms.minclsize;
286 }
287
288 #if NFSSERVER
289
290 /*
291 * allocate a list of mbufs to hold the given amount of data
292 */
293 int
294 nfsm_mbuf_get_list(size_t size, mbuf_t *mp, int *mbcnt)
295 {
296 int error, cnt;
297 mbuf_t mhead, mlast, m;
298 size_t len, mlen;
299
300 error = cnt = 0;
301 mhead = mlast = NULL;
302 len = 0;
303
304 while (len < size) {
305 nfsm_mbuf_get(error, &m, (size - len));
306 if (error)
307 break;
308 if (!mhead)
309 mhead = m;
310 if (mlast && ((error = mbuf_setnext(mlast, m)))) {
311 mbuf_free(m);
312 break;
313 }
314 mlen = mbuf_maxlen(m);
315 if ((len + mlen) > size)
316 mlen = size - len;
317 mbuf_setlen(m, mlen);
318 len += mlen;
319 cnt++;
320 mlast = m;
321 }
322
323 if (!error) {
324 *mp = mhead;
325 *mbcnt = cnt;
326 }
327 return (error);
328 }
329
330 #endif /* NFSSERVER */
331
332 /*
333 * nfsm_chain_new_mbuf()
334 *
335 * Add a new mbuf to the given chain.
336 */
337 int
338 nfsm_chain_new_mbuf(struct nfsm_chain *nmc, size_t sizehint)
339 {
340 mbuf_t mb;
341 int error = 0;
342
343 if (nmc->nmc_flags & NFSM_CHAIN_FLAG_ADD_CLUSTERS)
344 sizehint = nfs_mbuf_minclsize;
345
346 /* allocate a new mbuf */
347 nfsm_mbuf_get(error, &mb, sizehint);
348 if (error)
349 return (error);
350 if (mb == NULL)
351 panic("got NULL mbuf?");
352
353 /* do we have a current mbuf? */
354 if (nmc->nmc_mcur) {
355 /* first cap off current mbuf */
356 mbuf_setlen(nmc->nmc_mcur, nmc->nmc_ptr - (caddr_t)mbuf_data(nmc->nmc_mcur));
357 /* then append the new mbuf */
358 error = mbuf_setnext(nmc->nmc_mcur, mb);
359 if (error) {
360 mbuf_free(mb);
361 return (error);
362 }
363 }
364
365 /* set up for using the new mbuf */
366 nmc->nmc_mcur = mb;
367 nmc->nmc_ptr = mbuf_data(mb);
368 nmc->nmc_left = mbuf_trailingspace(mb);
369
370 return (0);
371 }
372
373 /*
374 * nfsm_chain_add_opaque_f()
375 *
376 * Add "len" bytes of opaque data pointed to by "buf" to the given chain.
377 */
378 int
379 nfsm_chain_add_opaque_f(struct nfsm_chain *nmc, const u_char *buf, uint32_t len)
380 {
381 uint32_t paddedlen, tlen;
382 int error;
383
384 paddedlen = nfsm_rndup(len);
385
386 while (paddedlen) {
387 if (!nmc->nmc_left) {
388 error = nfsm_chain_new_mbuf(nmc, paddedlen);
389 if (error)
390 return (error);
391 }
392 tlen = MIN(nmc->nmc_left, paddedlen);
393 if (tlen) {
394 if (len) {
395 if (tlen > len)
396 tlen = len;
397 bcopy(buf, nmc->nmc_ptr, tlen);
398 } else {
399 bzero(nmc->nmc_ptr, tlen);
400 }
401 nmc->nmc_ptr += tlen;
402 nmc->nmc_left -= tlen;
403 paddedlen -= tlen;
404 if (len) {
405 buf += tlen;
406 len -= tlen;
407 }
408 }
409 }
410 return (0);
411 }
412
413 /*
414 * nfsm_chain_add_opaque_nopad_f()
415 *
416 * Add "len" bytes of opaque data pointed to by "buf" to the given chain.
417 * Do not XDR pad.
418 */
419 int
420 nfsm_chain_add_opaque_nopad_f(struct nfsm_chain *nmc, const u_char *buf, uint32_t len)
421 {
422 uint32_t tlen;
423 int error;
424
425 while (len > 0) {
426 if (nmc->nmc_left <= 0) {
427 error = nfsm_chain_new_mbuf(nmc, len);
428 if (error)
429 return (error);
430 }
431 tlen = MIN(nmc->nmc_left, len);
432 bcopy(buf, nmc->nmc_ptr, tlen);
433 nmc->nmc_ptr += tlen;
434 nmc->nmc_left -= tlen;
435 len -= tlen;
436 buf += tlen;
437 }
438 return (0);
439 }
440
441 /*
442 * nfsm_chain_add_uio()
443 *
444 * Add "len" bytes of data from "uio" to the given chain.
445 */
446 int
447 nfsm_chain_add_uio(struct nfsm_chain *nmc, uio_t uio, uint32_t len)
448 {
449 uint32_t paddedlen, tlen;
450 int error;
451
452 paddedlen = nfsm_rndup(len);
453
454 while (paddedlen) {
455 if (!nmc->nmc_left) {
456 error = nfsm_chain_new_mbuf(nmc, paddedlen);
457 if (error)
458 return (error);
459 }
460 tlen = MIN(nmc->nmc_left, paddedlen);
461 if (tlen) {
462 if (len) {
463 if (tlen > len)
464 tlen = len;
465 uiomove(nmc->nmc_ptr, tlen, uio);
466 } else {
467 bzero(nmc->nmc_ptr, tlen);
468 }
469 nmc->nmc_ptr += tlen;
470 nmc->nmc_left -= tlen;
471 paddedlen -= tlen;
472 if (len)
473 len -= tlen;
474 }
475 }
476 return (0);
477 }
478
479 /*
480 * Find the length of the NFS mbuf chain
481 * up to the current encoding/decoding offset.
482 */
483 int
484 nfsm_chain_offset(struct nfsm_chain *nmc)
485 {
486 mbuf_t mb;
487 int len = 0;
488
489 for (mb = nmc->nmc_mhead; mb; mb = mbuf_next(mb)) {
490 if (mb == nmc->nmc_mcur)
491 return (len + (nmc->nmc_ptr - (caddr_t) mbuf_data(mb)));
492 len += mbuf_len(mb);
493 }
494
495 return (len);
496 }
497
498 /*
499 * nfsm_chain_advance()
500 *
501 * Advance an nfsm_chain by "len" bytes.
502 */
503 int
504 nfsm_chain_advance(struct nfsm_chain *nmc, uint32_t len)
505 {
506 mbuf_t mb;
507
508 while (len) {
509 if (nmc->nmc_left >= len) {
510 nmc->nmc_left -= len;
511 nmc->nmc_ptr += len;
512 return (0);
513 }
514 len -= nmc->nmc_left;
515 nmc->nmc_mcur = mb = mbuf_next(nmc->nmc_mcur);
516 if (!mb)
517 return (EBADRPC);
518 nmc->nmc_ptr = mbuf_data(mb);
519 nmc->nmc_left = mbuf_len(mb);
520 }
521
522 return (0);
523 }
524
525 /*
526 * nfsm_chain_reverse()
527 *
528 * Reverse decode offset in an nfsm_chain by "len" bytes.
529 */
530 int
531 nfsm_chain_reverse(struct nfsm_chain *nmc, uint32_t len)
532 {
533 uint32_t mlen, new_offset;
534 int error = 0;
535
536 mlen = nmc->nmc_ptr - (caddr_t) mbuf_data(nmc->nmc_mcur);
537 if (len <= mlen) {
538 nmc->nmc_ptr -= len;
539 nmc->nmc_left += len;
540 return (0);
541 }
542
543 new_offset = nfsm_chain_offset(nmc) - len;
544 nfsm_chain_dissect_init(error, nmc, nmc->nmc_mhead);
545 if (error)
546 return (error);
547
548 return (nfsm_chain_advance(nmc, new_offset));
549 }
550
551 /*
552 * nfsm_chain_get_opaque_pointer_f()
553 *
554 * Return a pointer to the next "len" bytes of contiguous data in
555 * the mbuf chain. If the next "len" bytes are not contiguous, we
556 * try to manipulate the mbuf chain so that it is.
557 *
558 * The nfsm_chain is advanced by nfsm_rndup("len") bytes.
559 */
560 int
561 nfsm_chain_get_opaque_pointer_f(struct nfsm_chain *nmc, uint32_t len, u_char **pptr)
562 {
563 mbuf_t mbcur, mb;
564 uint32_t left, need, mblen, cplen, padlen;
565 u_char *ptr;
566 int error = 0;
567
568 /* move to next mbuf with data */
569 while (nmc->nmc_mcur && (nmc->nmc_left == 0)) {
570 mb = mbuf_next(nmc->nmc_mcur);
571 nmc->nmc_mcur = mb;
572 if (!mb)
573 break;
574 nmc->nmc_ptr = mbuf_data(mb);
575 nmc->nmc_left = mbuf_len(mb);
576 }
577 /* check if we've run out of data */
578 if (!nmc->nmc_mcur)
579 return (EBADRPC);
580
581 /* do we already have a contiguous buffer? */
582 if (nmc->nmc_left >= len) {
583 /* the returned pointer will be the current pointer */
584 *pptr = (u_char*)nmc->nmc_ptr;
585 error = nfsm_chain_advance(nmc, nfsm_rndup(len));
586 return (error);
587 }
588
589 padlen = nfsm_rndup(len) - len;
590
591 /* we need (len - left) more bytes */
592 mbcur = nmc->nmc_mcur;
593 left = nmc->nmc_left;
594 need = len - left;
595
596 if (need > mbuf_trailingspace(mbcur)) {
597 /*
598 * The needed bytes won't fit in the current mbuf so we'll
599 * allocate a new mbuf to hold the contiguous range of data.
600 */
601 nfsm_mbuf_get(error, &mb, len);
602 if (error)
603 return (error);
604 /* double check that this mbuf can hold all the data */
605 if (mbuf_maxlen(mb) < len) {
606 mbuf_free(mb);
607 return (EOVERFLOW);
608 }
609
610 /* the returned pointer will be the new mbuf's data pointer */
611 *pptr = ptr = mbuf_data(mb);
612
613 /* copy "left" bytes to the new mbuf */
614 bcopy(nmc->nmc_ptr, ptr, left);
615 ptr += left;
616 mbuf_setlen(mb, left);
617
618 /* insert the new mbuf between the current and next mbufs */
619 error = mbuf_setnext(mb, mbuf_next(mbcur));
620 if (!error)
621 error = mbuf_setnext(mbcur, mb);
622 if (error) {
623 mbuf_free(mb);
624 return (error);
625 }
626
627 /* reduce current mbuf's length by "left" */
628 mbuf_setlen(mbcur, mbuf_len(mbcur) - left);
629
630 /*
631 * update nmc's state to point at the end of the mbuf
632 * where the needed data will be copied to.
633 */
634 nmc->nmc_mcur = mbcur = mb;
635 nmc->nmc_left = 0;
636 nmc->nmc_ptr = (caddr_t)ptr;
637 } else {
638 /* The rest of the data will fit in this mbuf. */
639
640 /* the returned pointer will be the current pointer */
641 *pptr = (u_char*)nmc->nmc_ptr;
642
643 /*
644 * update nmc's state to point at the end of the mbuf
645 * where the needed data will be copied to.
646 */
647 nmc->nmc_ptr += left;
648 nmc->nmc_left = 0;
649 }
650
651 /*
652 * move the next "need" bytes into the current
653 * mbuf from the mbufs that follow
654 */
655
656 /* extend current mbuf length */
657 mbuf_setlen(mbcur, mbuf_len(mbcur) + need);
658
659 /* mb follows mbufs we're copying/compacting data from */
660 mb = mbuf_next(mbcur);
661
662 while (need && mb) {
663 /* copy as much as we need/can */
664 ptr = mbuf_data(mb);
665 mblen = mbuf_len(mb);
666 cplen = MIN(mblen, need);
667 if (cplen) {
668 bcopy(ptr, nmc->nmc_ptr, cplen);
669 /*
670 * update the mbuf's pointer and length to reflect that
671 * the data was shifted to an earlier mbuf in the chain
672 */
673 error = mbuf_setdata(mb, ptr + cplen, mblen - cplen);
674 if (error) {
675 mbuf_setlen(mbcur, mbuf_len(mbcur) - need);
676 return (error);
677 }
678 /* update pointer/need */
679 nmc->nmc_ptr += cplen;
680 need -= cplen;
681 }
682 /* if more needed, go to next mbuf */
683 if (need)
684 mb = mbuf_next(mb);
685 }
686
687 /* did we run out of data in the mbuf chain? */
688 if (need) {
689 mbuf_setlen(mbcur, mbuf_len(mbcur) - need);
690 return (EBADRPC);
691 }
692
693 /*
694 * update nmc's state to point after this contiguous data
695 *
696 * "mb" points to the last mbuf we copied data from so we
697 * just set nmc to point at whatever remains in that mbuf.
698 */
699 nmc->nmc_mcur = mb;
700 nmc->nmc_ptr = mbuf_data(mb);
701 nmc->nmc_left = mbuf_len(mb);
702
703 /* move past any padding */
704 if (padlen)
705 error = nfsm_chain_advance(nmc, padlen);
706
707 return (error);
708 }
709
710 /*
711 * nfsm_chain_get_opaque_f()
712 *
713 * Read the next "len" bytes in the chain into "buf".
714 * The nfsm_chain is advanced by nfsm_rndup("len") bytes.
715 */
716 int
717 nfsm_chain_get_opaque_f(struct nfsm_chain *nmc, uint32_t len, u_char *buf)
718 {
719 uint32_t cplen, padlen;
720 int error = 0;
721
722 padlen = nfsm_rndup(len) - len;
723
724 /* loop through mbufs copying all the data we need */
725 while (len && nmc->nmc_mcur) {
726 /* copy as much as we need/can */
727 cplen = MIN(nmc->nmc_left, len);
728 if (cplen) {
729 bcopy(nmc->nmc_ptr, buf, cplen);
730 nmc->nmc_ptr += cplen;
731 nmc->nmc_left -= cplen;
732 buf += cplen;
733 len -= cplen;
734 }
735 /* if more needed, go to next mbuf */
736 if (len) {
737 mbuf_t mb = mbuf_next(nmc->nmc_mcur);
738 nmc->nmc_mcur = mb;
739 nmc->nmc_ptr = mb ? mbuf_data(mb) : NULL;
740 nmc->nmc_left = mb ? mbuf_len(mb) : 0;
741 }
742 }
743
744 /* did we run out of data in the mbuf chain? */
745 if (len)
746 return (EBADRPC);
747
748 if (padlen)
749 nfsm_chain_adv(error, nmc, padlen);
750
751 return (error);
752 }
753
754 /*
755 * nfsm_chain_get_uio()
756 *
757 * Read the next "len" bytes in the chain into the given uio.
758 * The nfsm_chain is advanced by nfsm_rndup("len") bytes.
759 */
760 int
761 nfsm_chain_get_uio(struct nfsm_chain *nmc, uint32_t len, uio_t uio)
762 {
763 uint32_t cplen, padlen;
764 int error = 0;
765
766 padlen = nfsm_rndup(len) - len;
767
768 /* loop through mbufs copying all the data we need */
769 while (len && nmc->nmc_mcur) {
770 /* copy as much as we need/can */
771 cplen = MIN(nmc->nmc_left, len);
772 if (cplen) {
773 error = uiomove(nmc->nmc_ptr, cplen, uio);
774 if (error)
775 return (error);
776 nmc->nmc_ptr += cplen;
777 nmc->nmc_left -= cplen;
778 len -= cplen;
779 }
780 /* if more needed, go to next mbuf */
781 if (len) {
782 mbuf_t mb = mbuf_next(nmc->nmc_mcur);
783 nmc->nmc_mcur = mb;
784 nmc->nmc_ptr = mb ? mbuf_data(mb) : NULL;
785 nmc->nmc_left = mb ? mbuf_len(mb) : 0;
786 }
787 }
788
789 /* did we run out of data in the mbuf chain? */
790 if (len)
791 return (EBADRPC);
792
793 if (padlen)
794 nfsm_chain_adv(error, nmc, padlen);
795
796 return (error);
797 }
798
799 #if NFSCLIENT
800
801 int
802 nfsm_chain_add_string_nfc(struct nfsm_chain *nmc, const uint8_t *s, uint32_t slen)
803 {
804 uint8_t smallbuf[64];
805 uint8_t *nfcname = smallbuf;
806 size_t buflen = sizeof(smallbuf), nfclen;
807 int error;
808
809 error = utf8_normalizestr(s, slen, nfcname, &nfclen, buflen, UTF_PRECOMPOSED|UTF_NO_NULL_TERM);
810 if (error == ENAMETOOLONG) {
811 buflen = MAXPATHLEN;
812 MALLOC_ZONE(nfcname, uint8_t *, MAXPATHLEN, M_NAMEI, M_WAITOK);
813 if (nfcname)
814 error = utf8_normalizestr(s, slen, nfcname, &nfclen, buflen, UTF_PRECOMPOSED|UTF_NO_NULL_TERM);
815 }
816
817 /* if we got an error, just use the original string */
818 if (error)
819 nfsm_chain_add_string(error, nmc, s, slen);
820 else
821 nfsm_chain_add_string(error, nmc, nfcname, nfclen);
822
823 if (nfcname && (nfcname != smallbuf))
824 FREE_ZONE(nfcname, MAXPATHLEN, M_NAMEI);
825 return (error);
826 }
827
828 /*
829 * Add an NFSv2 "sattr" structure to an mbuf chain
830 */
831 int
832 nfsm_chain_add_v2sattr_f(struct nfsm_chain *nmc, struct vnode_attr *vap, uint32_t szrdev)
833 {
834 int error = 0;
835
836 nfsm_chain_add_32(error, nmc, vtonfsv2_mode(vap->va_type,
837 (VATTR_IS_ACTIVE(vap, va_mode) ? vap->va_mode : 0600)));
838 nfsm_chain_add_32(error, nmc,
839 VATTR_IS_ACTIVE(vap, va_uid) ? vap->va_uid : (uint32_t)-1);
840 nfsm_chain_add_32(error, nmc,
841 VATTR_IS_ACTIVE(vap, va_gid) ? vap->va_gid : (uint32_t)-1);
842 nfsm_chain_add_32(error, nmc, szrdev);
843 nfsm_chain_add_v2time(error, nmc,
844 VATTR_IS_ACTIVE(vap, va_access_time) ?
845 &vap->va_access_time : NULL);
846 nfsm_chain_add_v2time(error, nmc,
847 VATTR_IS_ACTIVE(vap, va_modify_time) ?
848 &vap->va_modify_time : NULL);
849
850 return (error);
851 }
852
853 /*
854 * Add an NFSv3 "sattr" structure to an mbuf chain
855 */
856 int
857 nfsm_chain_add_v3sattr_f(struct nfsm_chain *nmc, struct vnode_attr *vap)
858 {
859 int error = 0;
860
861 if (VATTR_IS_ACTIVE(vap, va_mode)) {
862 nfsm_chain_add_32(error, nmc, TRUE);
863 nfsm_chain_add_32(error, nmc, vap->va_mode);
864 } else {
865 nfsm_chain_add_32(error, nmc, FALSE);
866 }
867 if (VATTR_IS_ACTIVE(vap, va_uid)) {
868 nfsm_chain_add_32(error, nmc, TRUE);
869 nfsm_chain_add_32(error, nmc, vap->va_uid);
870 } else {
871 nfsm_chain_add_32(error, nmc, FALSE);
872 }
873 if (VATTR_IS_ACTIVE(vap, va_gid)) {
874 nfsm_chain_add_32(error, nmc, TRUE);
875 nfsm_chain_add_32(error, nmc, vap->va_gid);
876 } else {
877 nfsm_chain_add_32(error, nmc, FALSE);
878 }
879 if (VATTR_IS_ACTIVE(vap, va_data_size)) {
880 nfsm_chain_add_32(error, nmc, TRUE);
881 nfsm_chain_add_64(error, nmc, vap->va_data_size);
882 } else {
883 nfsm_chain_add_32(error, nmc, FALSE);
884 }
885 if (vap->va_vaflags & VA_UTIMES_NULL) {
886 nfsm_chain_add_32(error, nmc, NFS_TIME_SET_TO_SERVER);
887 nfsm_chain_add_32(error, nmc, NFS_TIME_SET_TO_SERVER);
888 } else {
889 if (VATTR_IS_ACTIVE(vap, va_access_time)) {
890 nfsm_chain_add_32(error, nmc, NFS_TIME_SET_TO_CLIENT);
891 nfsm_chain_add_32(error, nmc, vap->va_access_time.tv_sec);
892 nfsm_chain_add_32(error, nmc, vap->va_access_time.tv_nsec);
893 } else {
894 nfsm_chain_add_32(error, nmc, NFS_TIME_DONT_CHANGE);
895 }
896 if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
897 nfsm_chain_add_32(error, nmc, NFS_TIME_SET_TO_CLIENT);
898 nfsm_chain_add_32(error, nmc, vap->va_modify_time.tv_sec);
899 nfsm_chain_add_32(error, nmc, vap->va_modify_time.tv_nsec);
900 } else {
901 nfsm_chain_add_32(error, nmc, NFS_TIME_DONT_CHANGE);
902 }
903 }
904
905 return (error);
906 }
907
908
909 /*
910 * nfsm_chain_get_fh_attr()
911 *
912 * Get the file handle and attributes from an mbuf chain. (NFSv2/v3)
913 */
914 int
915 nfsm_chain_get_fh_attr(
916 struct nfsm_chain *nmc,
917 nfsnode_t dnp,
918 vfs_context_t ctx,
919 int nfsvers,
920 uint64_t *xidp,
921 fhandle_t *fhp,
922 struct nfs_vattr *nvap)
923 {
924 int error = 0, gotfh, gotattr;
925
926 gotfh = gotattr = 1;
927
928 if (nfsvers == NFS_VER3) /* check for file handle */
929 nfsm_chain_get_32(error, nmc, gotfh);
930 if (!error && gotfh) /* get file handle */
931 nfsm_chain_get_fh(error, nmc, nfsvers, fhp);
932 else
933 fhp->fh_len = 0;
934 if (nfsvers == NFS_VER3) /* check for file attributes */
935 nfsm_chain_get_32(error, nmc, gotattr);
936 nfsmout_if(error);
937 if (gotattr) {
938 if (!gotfh) /* skip attributes */
939 nfsm_chain_adv(error, nmc, NFSX_V3FATTR);
940 else /* get attributes */
941 error = nfs_parsefattr(nmc, nfsvers, nvap);
942 } else if (gotfh) {
943 /* we need valid attributes in order to call nfs_nget() */
944 if (nfs3_getattr_rpc(NULL, NFSTOMP(dnp), fhp->fh_data, fhp->fh_len, 0, ctx, nvap, xidp)) {
945 gotattr = 0;
946 fhp->fh_len = 0;
947 }
948 }
949 nfsmout:
950 return (error);
951 }
952
953 /*
954 * Get and process NFSv3 WCC data from an mbuf chain
955 */
956 int
957 nfsm_chain_get_wcc_data_f(
958 struct nfsm_chain *nmc,
959 nfsnode_t np,
960 struct timespec *premtime,
961 int *newpostattr,
962 u_int64_t *xidp)
963 {
964 int error = 0;
965 uint32_t flag = 0;
966
967 nfsm_chain_get_32(error, nmc, flag);
968 if (!error && flag) {
969 nfsm_chain_adv(error, nmc, 2 * NFSX_UNSIGNED);
970 nfsm_chain_get_32(error, nmc, premtime->tv_sec);
971 nfsm_chain_get_32(error, nmc, premtime->tv_nsec);
972 nfsm_chain_adv(error, nmc, 2 * NFSX_UNSIGNED);
973 } else {
974 premtime->tv_sec = 0;
975 premtime->tv_nsec = 0;
976 }
977 nfsm_chain_postop_attr_update_flag(error, nmc, np, *newpostattr, xidp);
978
979 return (error);
980 }
981
982 /*
983 * Get the next RPC transaction ID (XID)
984 */
985 void
986 nfs_get_xid(uint64_t *xidp)
987 {
988 struct timeval tv;
989
990 lck_mtx_lock(nfs_request_mutex);
991 if (!nfs_xid) {
992 /*
993 * Derive initial xid from system time.
994 *
995 * Note: it's OK if this code inits nfs_xid to 0 (for example,
996 * due to a broken clock) because we immediately increment it
997 * and we guarantee to never use xid 0. So, nfs_xid should only
998 * ever be 0 the first time this function is called.
999 */
1000 microtime(&tv);
1001 nfs_xid = tv.tv_sec << 12;
1002 }
1003 if (++nfs_xid == 0) {
1004 /* Skip zero xid if it should ever happen. */
1005 nfs_xidwrap++;
1006 nfs_xid++;
1007 }
1008 *xidp = nfs_xid + ((uint64_t)nfs_xidwrap << 32);
1009 lck_mtx_unlock(nfs_request_mutex);
1010 }
1011
1012 /*
1013 * Build the RPC header and fill in the authorization info.
1014 * Returns the head of the mbuf list and the xid.
1015 */
1016
1017 int
1018 nfsm_rpchead(
1019 struct nfsreq *req,
1020 mbuf_t mrest,
1021 u_int64_t *xidp,
1022 mbuf_t *mreqp)
1023 {
1024 struct nfsmount *nmp = req->r_nmp;
1025 int nfsvers = nmp->nm_vers;
1026 int proc = ((nfsvers == NFS_VER2) ? nfsv2_procid[req->r_procnum] : (int)req->r_procnum);
1027
1028 return nfsm_rpchead2(nmp, nmp->nm_sotype, NFS_PROG, nfsvers, proc,
1029 req->r_auth, req->r_cred, req, mrest, xidp, mreqp);
1030 }
1031
1032 /*
1033 * get_auiliary_groups: Gets the supplementary groups from a credential.
1034 *
1035 * IN: cred: credential to get the associated groups from.
1036 * OUT: groups: An array of gids of NGROUPS size.
1037 * IN: count: The number of groups to get; i.e.; the number of groups the server supports
1038 *
1039 * returns: The number of groups found.
1040 *
1041 * Just a wrapper around kauth_cred_getgroups to handle the case of a server supporting less
1042 * than NGROUPS.
1043 */
1044 static int
1045 get_auxiliary_groups(kauth_cred_t cred, gid_t groups[NGROUPS], int count)
1046 {
1047 gid_t pgid;
1048 int maxcount = count < NGROUPS ? count + 1 : NGROUPS;
1049 int i;
1050
1051 for (i = 0; i < NGROUPS; i++)
1052 groups[i] = -2; /* Initialize to the nobody group */
1053
1054 (void)kauth_cred_getgroups(cred, groups, &maxcount);
1055 if (maxcount < 1)
1056 return (maxcount);
1057
1058 /*
1059 * kauth_get_groups returns the primary group followed by the
1060 * users auxiliary groups. If the number of groups the server supports
1061 * is less than NGROUPS, then we will drop the first group so that
1062 * we can send one more group over the wire.
1063 */
1064
1065
1066 if (count < NGROUPS) {
1067 pgid = kauth_cred_getgid(cred);
1068 if (pgid == groups[0]) {
1069 maxcount -= 1;
1070 for (i = 0; i < maxcount; i++) {
1071 groups[i] = groups[i+1];
1072 }
1073 }
1074 }
1075
1076 return (maxcount);
1077 }
1078
1079 int
1080 nfsm_rpchead2(struct nfsmount *nmp, int sotype, int prog, int vers, int proc, int auth_type,
1081 kauth_cred_t cred, struct nfsreq *req, mbuf_t mrest, u_int64_t *xidp, mbuf_t *mreqp)
1082 {
1083 mbuf_t mreq, mb;
1084 int error, i, auth_len = 0, authsiz, reqlen;
1085 size_t headlen;
1086 struct nfsm_chain nmreq;
1087 gid_t grouplist[NGROUPS];
1088 int groupcount;
1089
1090 /* calculate expected auth length */
1091 switch (auth_type) {
1092 case RPCAUTH_NONE:
1093 auth_len = 0;
1094 break;
1095 case RPCAUTH_SYS:
1096 {
1097 int count = nmp->nm_numgrps < NGROUPS ? nmp->nm_numgrps : NGROUPS;
1098
1099 if (!cred)
1100 return (EINVAL);
1101 groupcount = get_auxiliary_groups(cred, grouplist, count);
1102 if (groupcount < 0)
1103 return (EINVAL);
1104 auth_len = ((uint32_t)groupcount + 5) * NFSX_UNSIGNED;
1105 break;
1106 }
1107 case RPCAUTH_KRB5:
1108 case RPCAUTH_KRB5I:
1109 case RPCAUTH_KRB5P:
1110 if (!req || !cred)
1111 return (EINVAL);
1112 auth_len = 5 * NFSX_UNSIGNED + 0; // zero context handle for now
1113 break;
1114 default:
1115 return (EINVAL);
1116 }
1117 authsiz = nfsm_rndup(auth_len);
1118
1119 /* allocate the packet */
1120 headlen = authsiz + 10 * NFSX_UNSIGNED;
1121 if (sotype == SOCK_STREAM) /* also include room for any RPC Record Mark */
1122 headlen += NFSX_UNSIGNED;
1123 if (headlen >= nfs_mbuf_minclsize) {
1124 error = mbuf_getpacket(MBUF_WAITOK, &mreq);
1125 } else {
1126 error = mbuf_gethdr(MBUF_WAITOK, MBUF_TYPE_DATA, &mreq);
1127 if (!error) {
1128 if (headlen < nfs_mbuf_mhlen)
1129 mbuf_align_32(mreq, headlen);
1130 else
1131 mbuf_align_32(mreq, 8 * NFSX_UNSIGNED);
1132 }
1133 }
1134 if (error) {
1135 /* unable to allocate packet */
1136 /* XXX should we keep statistics for these errors? */
1137 return (error);
1138 }
1139
1140 /*
1141 * If the caller gave us a non-zero XID then use it because
1142 * it may be a higher-level resend with a GSSAPI credential.
1143 * Otherwise, allocate a new one.
1144 */
1145 if (*xidp == 0)
1146 nfs_get_xid(xidp);
1147
1148 /* build the header(s) */
1149 nfsm_chain_init(&nmreq, mreq);
1150
1151 /* First, if it's a TCP stream insert space for an RPC record mark */
1152 if (sotype == SOCK_STREAM)
1153 nfsm_chain_add_32(error, &nmreq, 0);
1154
1155 /* Then the RPC header. */
1156 nfsm_chain_add_32(error, &nmreq, (*xidp & 0xffffffff));
1157 nfsm_chain_add_32(error, &nmreq, RPC_CALL);
1158 nfsm_chain_add_32(error, &nmreq, RPC_VER2);
1159 nfsm_chain_add_32(error, &nmreq, prog);
1160 nfsm_chain_add_32(error, &nmreq, vers);
1161 nfsm_chain_add_32(error, &nmreq, proc);
1162
1163 add_cred:
1164 switch (auth_type) {
1165 case RPCAUTH_NONE:
1166 nfsm_chain_add_32(error, &nmreq, RPCAUTH_NONE); /* auth */
1167 nfsm_chain_add_32(error, &nmreq, 0); /* length */
1168 nfsm_chain_add_32(error, &nmreq, RPCAUTH_NONE); /* verf */
1169 nfsm_chain_add_32(error, &nmreq, 0); /* length */
1170 nfsm_chain_build_done(error, &nmreq);
1171 /* Append the args mbufs */
1172 if (!error)
1173 error = mbuf_setnext(nmreq.nmc_mcur, mrest);
1174 break;
1175 case RPCAUTH_SYS: {
1176 nfsm_chain_add_32(error, &nmreq, RPCAUTH_SYS);
1177 nfsm_chain_add_32(error, &nmreq, authsiz);
1178 nfsm_chain_add_32(error, &nmreq, 0); /* stamp */
1179 nfsm_chain_add_32(error, &nmreq, 0); /* zero-length hostname */
1180 nfsm_chain_add_32(error, &nmreq, kauth_cred_getuid(cred)); /* UID */
1181 nfsm_chain_add_32(error, &nmreq, kauth_cred_getgid(cred)); /* GID */
1182 nfsm_chain_add_32(error, &nmreq, groupcount);/* additional GIDs */
1183 for (i = 0; i < groupcount; i++)
1184 nfsm_chain_add_32(error, &nmreq, grouplist[i]);
1185
1186 /* And the verifier... */
1187 nfsm_chain_add_32(error, &nmreq, RPCAUTH_NONE); /* flavor */
1188 nfsm_chain_add_32(error, &nmreq, 0); /* length */
1189 nfsm_chain_build_done(error, &nmreq);
1190
1191 /* Append the args mbufs */
1192 if (!error)
1193 error = mbuf_setnext(nmreq.nmc_mcur, mrest);
1194 break;
1195 }
1196 case RPCAUTH_KRB5:
1197 case RPCAUTH_KRB5I:
1198 case RPCAUTH_KRB5P:
1199 error = nfs_gss_clnt_cred_put(req, &nmreq, mrest);
1200 if (error == ENEEDAUTH) {
1201 int count = nmp->nm_numgrps < NGROUPS ? nmp->nm_numgrps : NGROUPS;
1202
1203 /*
1204 * Use sec=sys for this user
1205 */
1206 error = 0;
1207 req->r_auth = auth_type = RPCAUTH_SYS;
1208 groupcount = get_auxiliary_groups(cred, grouplist, count);
1209 if (groupcount < 0)
1210 return (EINVAL);
1211 auth_len = ((uint32_t)groupcount + 5) * NFSX_UNSIGNED;
1212 authsiz = nfsm_rndup(auth_len);
1213 goto add_cred;
1214 }
1215 break;
1216 };
1217
1218 /* finish setting up the packet */
1219 if (!error)
1220 error = mbuf_pkthdr_setrcvif(mreq, 0);
1221
1222 if (error) {
1223 mbuf_freem(mreq);
1224 return (error);
1225 }
1226
1227 /* Calculate the size of the request */
1228 reqlen = 0;
1229 for (mb = nmreq.nmc_mhead; mb; mb = mbuf_next(mb))
1230 reqlen += mbuf_len(mb);
1231
1232 mbuf_pkthdr_setlen(mreq, reqlen);
1233
1234 /*
1235 * If the request goes on a TCP stream,
1236 * set its size in the RPC record mark.
1237 * The record mark count doesn't include itself
1238 * and the last fragment bit is set.
1239 */
1240 if (sotype == SOCK_STREAM)
1241 nfsm_chain_set_recmark(error, &nmreq,
1242 (reqlen - NFSX_UNSIGNED) | 0x80000000);
1243
1244 *mreqp = mreq;
1245 return (0);
1246 }
1247
1248 /*
1249 * Parse an NFS file attribute structure out of an mbuf chain.
1250 */
1251 int
1252 nfs_parsefattr(struct nfsm_chain *nmc, int nfsvers, struct nfs_vattr *nvap)
1253 {
1254 int error = 0;
1255 enum vtype vtype;
1256 nfstype nvtype;
1257 u_short vmode;
1258 uint32_t val, val2;
1259 dev_t rdev;
1260
1261 val = val2 = 0;
1262 NVATTR_INIT(nvap);
1263
1264 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_TYPE);
1265 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_MODE);
1266 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_NUMLINKS);
1267 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_OWNER);
1268 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_OWNER_GROUP);
1269 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_SIZE);
1270 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_SPACE_USED);
1271 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_RAWDEV);
1272 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_FSID);
1273 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_FILEID);
1274 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_TIME_ACCESS);
1275 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_TIME_MODIFY);
1276 NFS_BITMAP_SET(nvap->nva_bitmap, NFS_FATTR_TIME_METADATA);
1277
1278 nfsm_chain_get_32(error, nmc, nvtype);
1279 nfsm_chain_get_32(error, nmc, vmode);
1280 nfsmout_if(error);
1281
1282 if (nfsvers == NFS_VER3) {
1283 nvap->nva_type = vtype = nfstov_type(nvtype, nfsvers);
1284 } else {
1285 /*
1286 * The duplicate information returned in fa_type and fa_mode
1287 * is an ambiguity in the NFS version 2 protocol.
1288 *
1289 * VREG should be taken literally as a regular file. If a
1290 * server intends to return some type information differently
1291 * in the upper bits of the mode field (e.g. for sockets, or
1292 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1293 * leave the examination of the mode bits even in the VREG
1294 * case to avoid breakage for bogus servers, but we make sure
1295 * that there are actually type bits set in the upper part of
1296 * fa_mode (and failing that, trust the va_type field).
1297 *
1298 * NFSv3 cleared the issue, and requires fa_mode to not
1299 * contain any type information (while also introducing
1300 * sockets and FIFOs for fa_type).
1301 */
1302 vtype = nfstov_type(nvtype, nfsvers);
1303 if ((vtype == VNON) || ((vtype == VREG) && ((vmode & S_IFMT) != 0)))
1304 vtype = IFTOVT(vmode);
1305 nvap->nva_type = vtype;
1306 }
1307
1308 nvap->nva_mode = (vmode & 07777);
1309
1310 nfsm_chain_get_32(error, nmc, nvap->nva_nlink);
1311 nfsm_chain_get_32(error, nmc, nvap->nva_uid);
1312 nfsm_chain_get_32(error, nmc, nvap->nva_gid);
1313
1314 if (nfsvers == NFS_VER3) {
1315 nfsm_chain_get_64(error, nmc, nvap->nva_size);
1316 nfsm_chain_get_64(error, nmc, nvap->nva_bytes);
1317 nfsm_chain_get_32(error, nmc, nvap->nva_rawdev.specdata1);
1318 nfsm_chain_get_32(error, nmc, nvap->nva_rawdev.specdata2);
1319 nfsmout_if(error);
1320 nfsm_chain_get_64(error, nmc, nvap->nva_fsid.major);
1321 nvap->nva_fsid.minor = 0;
1322 nfsm_chain_get_64(error, nmc, nvap->nva_fileid);
1323 } else {
1324 nfsm_chain_get_32(error, nmc, nvap->nva_size);
1325 nfsm_chain_adv(error, nmc, NFSX_UNSIGNED);
1326 nfsm_chain_get_32(error, nmc, rdev);
1327 nfsmout_if(error);
1328 nvap->nva_rawdev.specdata1 = major(rdev);
1329 nvap->nva_rawdev.specdata2 = minor(rdev);
1330 nfsm_chain_get_32(error, nmc, val); /* blocks */
1331 nfsmout_if(error);
1332 nvap->nva_bytes = val * NFS_FABLKSIZE;
1333 nfsm_chain_get_32(error, nmc, val);
1334 nfsmout_if(error);
1335 nvap->nva_fsid.major = (uint64_t)val;
1336 nvap->nva_fsid.minor = 0;
1337 nfsm_chain_get_32(error, nmc, val);
1338 nfsmout_if(error);
1339 nvap->nva_fileid = (uint64_t)val;
1340 /* Really ugly NFSv2 kludge. */
1341 if ((vtype == VCHR) && (rdev == (dev_t)0xffffffff))
1342 nvap->nva_type = VFIFO;
1343 }
1344 nfsm_chain_get_time(error, nmc, nfsvers,
1345 nvap->nva_timesec[NFSTIME_ACCESS],
1346 nvap->nva_timensec[NFSTIME_ACCESS]);
1347 nfsm_chain_get_time(error, nmc, nfsvers,
1348 nvap->nva_timesec[NFSTIME_MODIFY],
1349 nvap->nva_timensec[NFSTIME_MODIFY]);
1350 nfsm_chain_get_time(error, nmc, nfsvers,
1351 nvap->nva_timesec[NFSTIME_CHANGE],
1352 nvap->nva_timensec[NFSTIME_CHANGE]);
1353 nfsmout:
1354 return (error);
1355 }
1356
1357 /*
1358 * Load the attribute cache (that lives in the nfsnode entry) with
1359 * the value pointed to by nvap, unless the file type in the attribute
1360 * cache doesn't match the file type in the nvap, in which case log a
1361 * warning and return ESTALE.
1362 *
1363 * If the dontshrink flag is set, then it's not safe to call ubc_setsize()
1364 * to shrink the size of the file.
1365 */
1366 int
1367 nfs_loadattrcache(
1368 nfsnode_t np,
1369 struct nfs_vattr *nvap,
1370 u_int64_t *xidp,
1371 int dontshrink)
1372 {
1373 mount_t mp;
1374 vnode_t vp;
1375 struct timeval now;
1376 struct nfs_vattr *npnvap;
1377 int xattr = np->n_vattr.nva_flags & NFS_FFLAG_IS_ATTR;
1378 int referral = np->n_vattr.nva_flags & NFS_FFLAG_TRIGGER_REFERRAL;
1379 int aclbit, monitored, error = 0;
1380 kauth_acl_t acl;
1381 struct nfsmount *nmp;
1382 uint32_t events = np->n_events;
1383
1384 if (np->n_hflag & NHINIT) {
1385 vp = NULL;
1386 mp = np->n_mount;
1387 } else {
1388 vp = NFSTOV(np);
1389 mp = vnode_mount(vp);
1390 }
1391 monitored = vp ? vnode_ismonitored(vp) : 0;
1392
1393 FSDBG_TOP(527, np, vp, *xidp >> 32, *xidp);
1394
1395 if (!((nmp = VFSTONFS(mp)))) {
1396 FSDBG_BOT(527, ENXIO, 1, 0, *xidp);
1397 return (ENXIO);
1398 }
1399
1400 if (*xidp < np->n_xid) {
1401 /*
1402 * We have already updated attributes with a response from
1403 * a later request. The attributes we have here are probably
1404 * stale so we drop them (just return). However, our
1405 * out-of-order receipt could be correct - if the requests were
1406 * processed out of order at the server. Given the uncertainty
1407 * we invalidate our cached attributes. *xidp is zeroed here
1408 * to indicate the attributes were dropped - only getattr
1409 * cares - it needs to retry the rpc.
1410 */
1411 NATTRINVALIDATE(np);
1412 FSDBG_BOT(527, 0, np, np->n_xid, *xidp);
1413 *xidp = 0;
1414 return (0);
1415 }
1416
1417 if (vp && (nvap->nva_type != vnode_vtype(vp))) {
1418 /*
1419 * The filehandle has changed type on us. This can be
1420 * caused by either the server not having unique filehandles
1421 * or because another client has removed the previous
1422 * filehandle and a new object (of a different type)
1423 * has been created with the same filehandle.
1424 *
1425 * We can't simply switch the type on the vnode because
1426 * there may be type-specific fields that need to be
1427 * cleaned up or set up.
1428 *
1429 * So, what should we do with this vnode?
1430 *
1431 * About the best we can do is log a warning and return
1432 * an error. ESTALE is about the closest error, but it
1433 * is a little strange that we come up with this error
1434 * internally instead of simply passing it through from
1435 * the server. Hopefully, the vnode will be reclaimed
1436 * soon so the filehandle can be reincarnated as the new
1437 * object type.
1438 */
1439 printf("nfs loadattrcache vnode changed type, was %d now %d\n",
1440 vnode_vtype(vp), nvap->nva_type);
1441 error = ESTALE;
1442 if (monitored)
1443 events |= VNODE_EVENT_DELETE;
1444 goto out;
1445 }
1446
1447 npnvap = &np->n_vattr;
1448
1449 /*
1450 * The ACL cache needs special handling because it is not
1451 * always updated. Save current ACL cache state so it can
1452 * be restored after copying the new attributes into place.
1453 */
1454 aclbit = NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_ACL);
1455 acl = npnvap->nva_acl;
1456
1457 if (monitored) {
1458 /*
1459 * For monitored nodes, check for attribute changes that should generate events.
1460 */
1461 if (NFS_BITMAP_ISSET(nvap->nva_bitmap, NFS_FATTR_NUMLINKS) &&
1462 (nvap->nva_nlink != npnvap->nva_nlink))
1463 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_LINK;
1464 if (events & VNODE_EVENT_PERMS)
1465 /* no need to do all the checking if it's already set */;
1466 else if (NFS_BITMAP_ISSET(nvap->nva_bitmap, NFS_FATTR_MODE) &&
1467 (nvap->nva_mode != npnvap->nva_mode))
1468 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_PERMS;
1469 else if (NFS_BITMAP_ISSET(nvap->nva_bitmap, NFS_FATTR_OWNER) &&
1470 (nvap->nva_uid != npnvap->nva_uid))
1471 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_PERMS;
1472 else if (NFS_BITMAP_ISSET(nvap->nva_bitmap, NFS_FATTR_OWNER_GROUP) &&
1473 (nvap->nva_gid != npnvap->nva_gid))
1474 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_PERMS;
1475 else if (nmp->nm_vers >= NFS_VER4) {
1476 if (NFS_BITMAP_ISSET(nvap->nva_bitmap, NFS_FATTR_OWNER) &&
1477 !kauth_guid_equal(&nvap->nva_uuuid, &npnvap->nva_uuuid))
1478 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_PERMS;
1479 else if (NFS_BITMAP_ISSET(nvap->nva_bitmap, NFS_FATTR_OWNER_GROUP) &&
1480 !kauth_guid_equal(&nvap->nva_guuid, &npnvap->nva_guuid))
1481 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_PERMS;
1482 else if ((NFS_BITMAP_ISSET(nvap->nva_bitmap, NFS_FATTR_ACL) &&
1483 nvap->nva_acl && npnvap->nva_acl &&
1484 ((nvap->nva_acl->acl_entrycount != npnvap->nva_acl->acl_entrycount) ||
1485 bcmp(nvap->nva_acl, npnvap->nva_acl, KAUTH_ACL_COPYSIZE(nvap->nva_acl)))))
1486 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_PERMS;
1487 }
1488 if (((nmp->nm_vers >= NFS_VER4) && (nvap->nva_change != npnvap->nva_change)) ||
1489 (NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_TIME_MODIFY) &&
1490 ((nvap->nva_timesec[NFSTIME_MODIFY] != npnvap->nva_timesec[NFSTIME_MODIFY]) ||
1491 (nvap->nva_timensec[NFSTIME_MODIFY] != npnvap->nva_timensec[NFSTIME_MODIFY]))))
1492 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_WRITE;
1493 if (!events && NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_RAWDEV) &&
1494 ((nvap->nva_rawdev.specdata1 != npnvap->nva_rawdev.specdata1) ||
1495 (nvap->nva_rawdev.specdata2 != npnvap->nva_rawdev.specdata2)))
1496 events |= VNODE_EVENT_ATTRIB;
1497 if (!events && NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_FILEID) &&
1498 (nvap->nva_fileid != npnvap->nva_fileid))
1499 events |= VNODE_EVENT_ATTRIB;
1500 if (!events && NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_ARCHIVE) &&
1501 ((nvap->nva_flags & NFS_FFLAG_ARCHIVED) != (npnvap->nva_flags & NFS_FFLAG_ARCHIVED)))
1502 events |= VNODE_EVENT_ATTRIB;
1503 if (!events && NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_HIDDEN) &&
1504 ((nvap->nva_flags & NFS_FFLAG_HIDDEN) != (npnvap->nva_flags & NFS_FFLAG_HIDDEN)))
1505 events |= VNODE_EVENT_ATTRIB;
1506 if (!events && NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_TIME_CREATE) &&
1507 ((nvap->nva_timesec[NFSTIME_CREATE] != npnvap->nva_timesec[NFSTIME_CREATE]) ||
1508 (nvap->nva_timensec[NFSTIME_CREATE] != npnvap->nva_timensec[NFSTIME_CREATE])))
1509 events |= VNODE_EVENT_ATTRIB;
1510 if (!events && NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_TIME_BACKUP) &&
1511 ((nvap->nva_timesec[NFSTIME_BACKUP] != npnvap->nva_timesec[NFSTIME_BACKUP]) ||
1512 (nvap->nva_timensec[NFSTIME_BACKUP] != npnvap->nva_timensec[NFSTIME_BACKUP])))
1513 events |= VNODE_EVENT_ATTRIB;
1514 }
1515
1516 /* Copy the attributes to the attribute cache */
1517 bcopy((caddr_t)nvap, (caddr_t)npnvap, sizeof(*nvap));
1518
1519 microuptime(&now);
1520 np->n_attrstamp = now.tv_sec;
1521 np->n_xid = *xidp;
1522 /* NFS_FFLAG_IS_ATTR and NFS_FFLAG_TRIGGER_REFERRAL need to be sticky... */
1523 if (vp && xattr)
1524 nvap->nva_flags |= xattr;
1525 if (vp && referral)
1526 nvap->nva_flags |= referral;
1527
1528 if (NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_ACL)) {
1529 /* we're updating the ACL */
1530 if (nvap->nva_acl) {
1531 /* make a copy of the acl for the cache */
1532 npnvap->nva_acl = kauth_acl_alloc(nvap->nva_acl->acl_entrycount);
1533 if (npnvap->nva_acl) {
1534 bcopy(nvap->nva_acl, npnvap->nva_acl, KAUTH_ACL_COPYSIZE(nvap->nva_acl));
1535 } else {
1536 /* can't make a copy to cache, invalidate ACL cache */
1537 NFS_BITMAP_CLR(npnvap->nva_bitmap, NFS_FATTR_ACL);
1538 NACLINVALIDATE(np);
1539 aclbit = 0;
1540 }
1541 }
1542 if (acl) {
1543 kauth_acl_free(acl);
1544 acl = NULL;
1545 }
1546 }
1547 if (NFS_BITMAP_ISSET(npnvap->nva_bitmap, NFS_FATTR_ACL)) {
1548 /* update the ACL timestamp */
1549 np->n_aclstamp = now.tv_sec;
1550 } else {
1551 /* we aren't updating the ACL, so restore original values */
1552 if (aclbit)
1553 NFS_BITMAP_SET(npnvap->nva_bitmap, NFS_FATTR_ACL);
1554 npnvap->nva_acl = acl;
1555 }
1556
1557 #if CONFIG_TRIGGERS
1558 /*
1559 * For NFSv4, if the fsid doesn't match the fsid for the mount, then
1560 * this node is for a different file system on the server. So we mark
1561 * this node as a trigger node that will trigger the mirror mount.
1562 */
1563 if ((nmp->nm_vers >= NFS_VER4) && (nvap->nva_type == VDIR) &&
1564 ((np->n_vattr.nva_fsid.major != nmp->nm_fsid.major) ||
1565 (np->n_vattr.nva_fsid.minor != nmp->nm_fsid.minor)))
1566 np->n_vattr.nva_flags |= NFS_FFLAG_TRIGGER;
1567 #endif
1568
1569 if (!vp || (nvap->nva_type != VREG)) {
1570 np->n_size = nvap->nva_size;
1571 } else if (nvap->nva_size != np->n_size) {
1572 FSDBG(527, np, nvap->nva_size, np->n_size, (nvap->nva_type == VREG) | (np->n_flag & NMODIFIED ? 6 : 4));
1573 if (!UBCINFOEXISTS(vp) || (dontshrink && (nvap->nva_size < np->n_size))) {
1574 /* asked not to shrink, so stick with current size */
1575 FSDBG(527, np, np->n_size, np->n_vattr.nva_size, 0xf00d0001);
1576 nvap->nva_size = np->n_size;
1577 NATTRINVALIDATE(np);
1578 } else if ((np->n_flag & NMODIFIED) && (nvap->nva_size < np->n_size)) {
1579 /* if we've modified, stick with larger size */
1580 FSDBG(527, np, np->n_size, np->n_vattr.nva_size, 0xf00d0002);
1581 nvap->nva_size = np->n_size;
1582 npnvap->nva_size = np->n_size;
1583 } else {
1584 /*
1585 * n_size is protected by the data lock, so we need to
1586 * defer updating it until it's safe. We save the new size
1587 * and set a flag and it'll get updated the next time we get/drop
1588 * the data lock or the next time we do a getattr.
1589 */
1590 np->n_newsize = nvap->nva_size;
1591 SET(np->n_flag, NUPDATESIZE);
1592 if (monitored)
1593 events |= VNODE_EVENT_ATTRIB | VNODE_EVENT_EXTEND;
1594 }
1595 }
1596
1597 if (np->n_flag & NCHG) {
1598 if (np->n_flag & NACC) {
1599 nvap->nva_timesec[NFSTIME_ACCESS] = np->n_atim.tv_sec;
1600 nvap->nva_timensec[NFSTIME_ACCESS] = np->n_atim.tv_nsec;
1601 }
1602 if (np->n_flag & NUPD) {
1603 nvap->nva_timesec[NFSTIME_MODIFY] = np->n_mtim.tv_sec;
1604 nvap->nva_timensec[NFSTIME_MODIFY] = np->n_mtim.tv_nsec;
1605 }
1606 }
1607
1608 out:
1609 if (monitored && events)
1610 nfs_vnode_notify(np, events);
1611 FSDBG_BOT(527, error, np, np->n_size, *xidp);
1612 return (error);
1613 }
1614
1615 /*
1616 * Calculate the attribute timeout based on
1617 * how recently the file has been modified.
1618 */
1619 int
1620 nfs_attrcachetimeout(nfsnode_t np)
1621 {
1622 struct nfsmount *nmp;
1623 struct timeval now;
1624 int isdir;
1625 uint32_t timeo;
1626
1627 if (!(nmp = NFSTONMP(np)))
1628 return (0);
1629
1630 isdir = vnode_isdir(NFSTOV(np));
1631
1632 if ((nmp->nm_vers >= NFS_VER4) && (np->n_openflags & N_DELEG_MASK)) {
1633 /* If we have a delegation, we always use the max timeout. */
1634 timeo = isdir ? nmp->nm_acdirmax : nmp->nm_acregmax;
1635 } else if ((np)->n_flag & NMODIFIED) {
1636 /* If we have modifications, we always use the min timeout. */
1637 timeo = isdir ? nmp->nm_acdirmin : nmp->nm_acregmin;
1638 } else {
1639 /* Otherwise, we base the timeout on how old the file seems. */
1640 /* Note that if the client and server clocks are way out of sync, */
1641 /* timeout will probably get clamped to a min or max value */
1642 microtime(&now);
1643 timeo = (now.tv_sec - (np)->n_vattr.nva_timesec[NFSTIME_MODIFY]) / 10;
1644 if (isdir) {
1645 if (timeo < nmp->nm_acdirmin)
1646 timeo = nmp->nm_acdirmin;
1647 else if (timeo > nmp->nm_acdirmax)
1648 timeo = nmp->nm_acdirmax;
1649 } else {
1650 if (timeo < nmp->nm_acregmin)
1651 timeo = nmp->nm_acregmin;
1652 else if (timeo > nmp->nm_acregmax)
1653 timeo = nmp->nm_acregmax;
1654 }
1655 }
1656
1657 return (timeo);
1658 }
1659
1660 /*
1661 * Check the attribute cache time stamp.
1662 * If the cache is valid, copy contents to *nvaper and return 0
1663 * otherwise return an error.
1664 * Must be called with the node locked.
1665 */
1666 int
1667 nfs_getattrcache(nfsnode_t np, struct nfs_vattr *nvaper, int flags)
1668 {
1669 struct nfs_vattr *nvap;
1670 struct timeval nowup;
1671 int32_t timeo;
1672
1673 /* Check if the attributes are valid. */
1674 if (!NATTRVALID(np) || ((flags & NGA_ACL) && !NACLVALID(np))) {
1675 FSDBG(528, np, 0, 0xffffff01, ENOENT);
1676 OSAddAtomic64(1, &nfsstats.attrcache_misses);
1677 return (ENOENT);
1678 }
1679
1680 /* Verify the cached attributes haven't timed out. */
1681 timeo = nfs_attrcachetimeout(np);
1682 microuptime(&nowup);
1683 if ((nowup.tv_sec - np->n_attrstamp) >= timeo) {
1684 FSDBG(528, np, 0, 0xffffff02, ENOENT);
1685 OSAddAtomic64(1, &nfsstats.attrcache_misses);
1686 return (ENOENT);
1687 }
1688 if ((flags & NGA_ACL) && ((nowup.tv_sec - np->n_aclstamp) >= timeo)) {
1689 FSDBG(528, np, 0, 0xffffff02, ENOENT);
1690 OSAddAtomic64(1, &nfsstats.attrcache_misses);
1691 return (ENOENT);
1692 }
1693
1694 nvap = &np->n_vattr;
1695 FSDBG(528, np, nvap->nva_size, np->n_size, 0xcace);
1696 OSAddAtomic64(1, &nfsstats.attrcache_hits);
1697
1698 if (nvap->nva_type != VREG) {
1699 np->n_size = nvap->nva_size;
1700 } else if (nvap->nva_size != np->n_size) {
1701 FSDBG(528, np, nvap->nva_size, np->n_size, (nvap->nva_type == VREG) | (np->n_flag & NMODIFIED ? 6 : 4));
1702 if ((np->n_flag & NMODIFIED) && (nvap->nva_size < np->n_size)) {
1703 /* if we've modified, stick with larger size */
1704 nvap->nva_size = np->n_size;
1705 } else {
1706 /*
1707 * n_size is protected by the data lock, so we need to
1708 * defer updating it until it's safe. We save the new size
1709 * and set a flag and it'll get updated the next time we get/drop
1710 * the data lock or the next time we do a getattr.
1711 */
1712 np->n_newsize = nvap->nva_size;
1713 SET(np->n_flag, NUPDATESIZE);
1714 }
1715 }
1716
1717 bcopy((caddr_t)nvap, (caddr_t)nvaper, sizeof(struct nfs_vattr));
1718 if (np->n_flag & NCHG) {
1719 if (np->n_flag & NACC) {
1720 nvaper->nva_timesec[NFSTIME_ACCESS] = np->n_atim.tv_sec;
1721 nvaper->nva_timensec[NFSTIME_ACCESS] = np->n_atim.tv_nsec;
1722 }
1723 if (np->n_flag & NUPD) {
1724 nvaper->nva_timesec[NFSTIME_MODIFY] = np->n_mtim.tv_sec;
1725 nvaper->nva_timensec[NFSTIME_MODIFY] = np->n_mtim.tv_nsec;
1726 }
1727 }
1728 if (nvap->nva_acl) {
1729 if (flags & NGA_ACL) {
1730 nvaper->nva_acl = kauth_acl_alloc(nvap->nva_acl->acl_entrycount);
1731 if (!nvaper->nva_acl)
1732 return (ENOMEM);
1733 bcopy(nvap->nva_acl, nvaper->nva_acl, KAUTH_ACL_COPYSIZE(nvap->nva_acl));
1734 } else {
1735 nvaper->nva_acl = NULL;
1736 }
1737 }
1738 return (0);
1739 }
1740
1741 /*
1742 * When creating file system objects:
1743 * Don't bother setting UID if it's the same as the credential performing the create.
1744 * Don't bother setting GID if it's the same as the directory or credential.
1745 */
1746 void
1747 nfs_avoid_needless_id_setting_on_create(nfsnode_t dnp, struct vnode_attr *vap, vfs_context_t ctx)
1748 {
1749 if (VATTR_IS_ACTIVE(vap, va_uid)) {
1750 if (kauth_cred_getuid(vfs_context_ucred(ctx)) == vap->va_uid) {
1751 VATTR_CLEAR_ACTIVE(vap, va_uid);
1752 VATTR_CLEAR_ACTIVE(vap, va_uuuid);
1753 }
1754 }
1755 if (VATTR_IS_ACTIVE(vap, va_gid)) {
1756 if ((vap->va_gid == dnp->n_vattr.nva_gid) ||
1757 (kauth_cred_getgid(vfs_context_ucred(ctx)) == vap->va_gid)) {
1758 VATTR_CLEAR_ACTIVE(vap, va_gid);
1759 VATTR_CLEAR_ACTIVE(vap, va_guuid);
1760 }
1761 }
1762 }
1763
1764 /*
1765 * Convert a universal address string to a sockaddr structure.
1766 *
1767 * Universal addresses can be in the following formats:
1768 *
1769 * d = decimal (IPv4)
1770 * x = hexadecimal (IPv6)
1771 * p = port (decimal)
1772 *
1773 * d.d.d.d
1774 * d.d.d.d.p.p
1775 * x:x:x:x:x:x:x:x
1776 * x:x:x:x:x:x:x:x.p.p
1777 * x:x:x:x:x:x:d.d.d.d
1778 * x:x:x:x:x:x:d.d.d.d.p.p
1779 *
1780 * IPv6 strings can also have a series of zeroes elided
1781 * IPv6 strings can also have a %scope suffix at the end (after any port)
1782 *
1783 * rules & exceptions:
1784 * - value before : is hex
1785 * - value before . is dec
1786 * - once . hit, all values are dec
1787 * - hex+port case means value before first dot is actually hex
1788 * - . is always preceded by digits except if last hex was double-colon
1789 *
1790 * scan, converting #s to bytes
1791 * first time a . is encountered, scan the rest to count them.
1792 * 2 dots = just port
1793 * 3 dots = just IPv4 no port
1794 * 5 dots = IPv4 and port
1795 */
1796
1797 #define IS_DIGIT(C) \
1798 (((C) >= '0') && ((C) <= '9'))
1799
1800 #define IS_XDIGIT(C) \
1801 (IS_DIGIT(C) || \
1802 (((C) >= 'A') && ((C) <= 'F')) || \
1803 (((C) >= 'a') && ((C) <= 'f')))
1804
1805 int
1806 nfs_uaddr2sockaddr(const char *uaddr, struct sockaddr *addr)
1807 {
1808 const char *p, *pd; /* pointers to current character in scan */
1809 const char *pnum; /* pointer to current number to decode */
1810 const char *pscope; /* pointer to IPv6 scope ID */
1811 uint8_t a[18]; /* octet array to store address bytes */
1812 int i; /* index of next octet to decode */
1813 int dci; /* index of octet to insert double-colon zeroes */
1814 int dcount, xdcount; /* count of digits in current number */
1815 int needmore; /* set when we know we need more input (e.g. after colon, period) */
1816 int dots; /* # of dots */
1817 int hex; /* contains hex values */
1818 unsigned long val; /* decoded value */
1819 int s; /* index used for sliding array to insert elided zeroes */
1820
1821 #define HEXVALUE 0
1822 #define DECIMALVALUE 1
1823 #define GET(TYPE) \
1824 do { \
1825 if ((dcount <= 0) || (dcount > (((TYPE) == DECIMALVALUE) ? 3 : 4))) \
1826 return (0); \
1827 if (((TYPE) == DECIMALVALUE) && xdcount) \
1828 return (0); \
1829 val = strtoul(pnum, NULL, ((TYPE) == DECIMALVALUE) ? 10 : 16); \
1830 if (((TYPE) == DECIMALVALUE) && (val >= 256)) \
1831 return (0); \
1832 /* check if there is room left in the array */ \
1833 if (i > (int)(sizeof(a) - (((TYPE) == HEXVALUE) ? 2 : 1) - ((dci != -1) ? 2 : 0))) \
1834 return (0); \
1835 if ((TYPE) == HEXVALUE) \
1836 a[i++] = ((val >> 8) & 0xff); \
1837 a[i++] = (val & 0xff); \
1838 } while (0)
1839
1840 hex = 0;
1841 dots = 0;
1842 dci = -1;
1843 i = dcount = xdcount = 0;
1844 pnum = p = uaddr;
1845 pscope = NULL;
1846 needmore = 1;
1847 if ((*p == ':') && (*++p != ':')) /* if it starts with colon, gotta be a double */
1848 return (0);
1849
1850 while (*p) {
1851 if (IS_XDIGIT(*p)) {
1852 dcount++;
1853 if (!IS_DIGIT(*p))
1854 xdcount++;
1855 needmore = 0;
1856 p++;
1857 } else if (*p == '.') {
1858 /* rest is decimal IPv4 dotted quad and/or port */
1859 if (!dots) {
1860 /* this is the first, so count them */
1861 for (pd = p; *pd; pd++) {
1862 if (*pd == '.') {
1863 if (++dots > 5)
1864 return (0);
1865 } else if (hex && (*pd == '%')) {
1866 break;
1867 } else if ((*pd < '0') || (*pd > '9')) {
1868 return (0);
1869 }
1870 }
1871 if ((dots != 2) && (dots != 3) && (dots != 5))
1872 return (0);
1873 if (hex && (dots == 2)) { /* hex+port */
1874 if (!dcount && needmore)
1875 return (0);
1876 if (dcount) /* last hex may be elided zero */
1877 GET(HEXVALUE);
1878 } else {
1879 GET(DECIMALVALUE);
1880 }
1881 } else {
1882 GET(DECIMALVALUE);
1883 }
1884 dcount = xdcount = 0;
1885 needmore = 1;
1886 pnum = ++p;
1887 } else if (*p == ':') {
1888 hex = 1;
1889 if (dots)
1890 return (0);
1891 if (!dcount) { /* missing number, probably double colon */
1892 if (dci >= 0) /* can only have one double colon */
1893 return (0);
1894 dci = i;
1895 needmore = 0;
1896 } else {
1897 GET(HEXVALUE);
1898 dcount = xdcount = 0;
1899 needmore = 1;
1900 }
1901 pnum = ++p;
1902 } else if (*p == '%') { /* scope ID delimiter */
1903 if (!hex)
1904 return (0);
1905 p++;
1906 pscope = p;
1907 break;
1908 } else { /* unexpected character */
1909 return (0);
1910 }
1911 }
1912 if (needmore && !dcount)
1913 return (0);
1914 if (dcount) /* decode trailing number */
1915 GET(dots ? DECIMALVALUE : HEXVALUE);
1916 if (dci >= 0) { /* got a double-colon at i, need to insert a range of zeroes */
1917 /* if we got a port, slide to end of array */
1918 /* otherwise, slide to end of address (non-port) values */
1919 int end = ((dots == 2) || (dots == 5)) ? sizeof(a) : (sizeof(a) - 2);
1920 if (i % 2) /* length of zero range must be multiple of 2 */
1921 return (0);
1922 if (i >= end) /* no room? */
1923 return (0);
1924 /* slide (i-dci) numbers up from index dci */
1925 for (s=0; s < (i - dci); s++)
1926 a[end-1-s] = a[i-1-s];
1927 /* zero (end-i) numbers at index dci */
1928 for (s=0; s < (end - i); s++)
1929 a[dci+s] = 0;
1930 i = end;
1931 }
1932
1933 /* copy out resulting socket address */
1934 if (hex) {
1935 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)addr;
1936 if ((((dots == 0) || (dots == 3)) && (i != (sizeof(a)-2))))
1937 return (0);
1938 if ((((dots == 2) || (dots == 5)) && (i != sizeof(a))))
1939 return (0);
1940 bzero(sin6, sizeof(struct sockaddr_in6));
1941 sin6->sin6_len = sizeof(struct sockaddr_in6);
1942 sin6->sin6_family = AF_INET6;
1943 bcopy(a, &sin6->sin6_addr.s6_addr, sizeof(struct in6_addr));
1944 if ((dots == 5) || (dots == 2))
1945 sin6->sin6_port = htons((a[16] << 8) | a[17]);
1946 if (pscope) {
1947 for (p=pscope; IS_DIGIT(*p); p++)
1948 ;
1949 if (*p && !IS_DIGIT(*p)) { /* name */
1950 ifnet_t interface = NULL;
1951 if (ifnet_find_by_name(pscope, &interface) == 0)
1952 sin6->sin6_scope_id = ifnet_index(interface);
1953 if (interface)
1954 ifnet_release(interface);
1955 } else { /* decimal number */
1956 sin6->sin6_scope_id = strtoul(pscope, NULL, 10);
1957 }
1958 /* XXX should we also embed scope id for linklocal? */
1959 }
1960 } else {
1961 struct sockaddr_in *sin = (struct sockaddr_in*)addr;
1962 if ((dots != 3) && (dots != 5))
1963 return (0);
1964 if ((dots == 3) && (i != 4))
1965 return (0);
1966 if ((dots == 5) && (i != 6))
1967 return (0);
1968 bzero(sin, sizeof(struct sockaddr_in));
1969 sin->sin_len = sizeof(struct sockaddr_in);
1970 sin->sin_family = AF_INET;
1971 bcopy(a, &sin->sin_addr.s_addr, sizeof(struct in_addr));
1972 if (dots == 5)
1973 sin->sin_port = htons((a[4] << 8) | a[5]);
1974 }
1975 return (1);
1976 }
1977
1978
1979 /* NFS Client debugging support */
1980 uint32_t nfs_debug_ctl;
1981
1982 #include <libkern/libkern.h>
1983 #include <stdarg.h>
1984
1985 void
1986 nfs_printf(int facility, int level, const char *fmt, ...)
1987 {
1988 va_list ap;
1989
1990 if ((uint32_t)level > NFS_DEBUG_LEVEL)
1991 return;
1992 if (NFS_DEBUG_FACILITY && !((uint32_t)facility & NFS_DEBUG_FACILITY))
1993 return;
1994
1995 va_start(ap, fmt);
1996 vprintf(fmt, ap);
1997 va_end(ap);
1998 }
1999
2000 #endif /* NFSCLIENT */
2001
2002 /*
2003 * Schedule a callout thread to run an NFS timer function
2004 * interval milliseconds in the future.
2005 */
2006 void
2007 nfs_interval_timer_start(thread_call_t call, int interval)
2008 {
2009 uint64_t deadline;
2010
2011 clock_interval_to_deadline(interval, 1000 * 1000, &deadline);
2012 thread_call_enter_delayed(call, deadline);
2013 }
2014
2015
2016 #if NFSSERVER
2017
2018 int nfsrv_cmp_secflavs(struct nfs_sec *, struct nfs_sec *);
2019 int nfsrv_hang_addrlist(struct nfs_export *, struct user_nfs_export_args *);
2020 int nfsrv_free_netopt(struct radix_node *, void *);
2021 int nfsrv_free_addrlist(struct nfs_export *, struct user_nfs_export_args *);
2022 struct nfs_export_options *nfsrv_export_lookup(struct nfs_export *, mbuf_t);
2023 struct nfs_export *nfsrv_fhtoexport(struct nfs_filehandle *);
2024 struct nfs_user_stat_node *nfsrv_get_user_stat_node(struct nfs_active_user_list *, struct sockaddr *, uid_t);
2025 void nfsrv_init_user_list(struct nfs_active_user_list *);
2026 void nfsrv_free_user_list(struct nfs_active_user_list *);
2027
2028 /*
2029 * add NFSv3 WCC data to an mbuf chain
2030 */
2031 int
2032 nfsm_chain_add_wcc_data_f(
2033 struct nfsrv_descript *nd,
2034 struct nfsm_chain *nmc,
2035 int preattrerr,
2036 struct vnode_attr *prevap,
2037 int postattrerr,
2038 struct vnode_attr *postvap)
2039 {
2040 int error = 0;
2041
2042 if (preattrerr) {
2043 nfsm_chain_add_32(error, nmc, FALSE);
2044 } else {
2045 nfsm_chain_add_32(error, nmc, TRUE);
2046 nfsm_chain_add_64(error, nmc, prevap->va_data_size);
2047 nfsm_chain_add_time(error, nmc, NFS_VER3, &prevap->va_modify_time);
2048 nfsm_chain_add_time(error, nmc, NFS_VER3, &prevap->va_change_time);
2049 }
2050 nfsm_chain_add_postop_attr(error, nd, nmc, postattrerr, postvap);
2051
2052 return (error);
2053 }
2054
2055 /*
2056 * Extract a lookup path from the given mbufs and store it in
2057 * a newly allocated buffer saved in the given nameidata structure.
2058 */
2059 int
2060 nfsm_chain_get_path_namei(
2061 struct nfsm_chain *nmc,
2062 uint32_t len,
2063 struct nameidata *nip)
2064 {
2065 struct componentname *cnp = &nip->ni_cnd;
2066 int error = 0;
2067 char *cp;
2068
2069 if (len > (MAXPATHLEN - 1))
2070 return (ENAMETOOLONG);
2071
2072 /*
2073 * Get a buffer for the name to be translated, and copy the
2074 * name into the buffer.
2075 */
2076 MALLOC_ZONE(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
2077 if (!cnp->cn_pnbuf)
2078 return (ENOMEM);
2079 cnp->cn_pnlen = MAXPATHLEN;
2080 cnp->cn_flags |= HASBUF;
2081
2082 /* Copy the name from the mbuf list to the string */
2083 cp = cnp->cn_pnbuf;
2084 nfsm_chain_get_opaque(error, nmc, len, cp);
2085 if (error)
2086 goto out;
2087 cnp->cn_pnbuf[len] = '\0';
2088
2089 /* sanity check the string */
2090 if ((strlen(cp) != len) || strchr(cp, '/'))
2091 error = EACCES;
2092 out:
2093 if (error) {
2094 if (cnp->cn_pnbuf)
2095 FREE_ZONE(cnp->cn_pnbuf, MAXPATHLEN, M_NAMEI);
2096 cnp->cn_flags &= ~HASBUF;
2097 } else {
2098 nip->ni_pathlen = len;
2099 }
2100 return (error);
2101 }
2102
2103 /*
2104 * Set up nameidata for a lookup() call and do it.
2105 */
2106 int
2107 nfsrv_namei(
2108 struct nfsrv_descript *nd,
2109 vfs_context_t ctx,
2110 struct nameidata *nip,
2111 struct nfs_filehandle *nfhp,
2112 vnode_t *retdirp,
2113 struct nfs_export **nxp,
2114 struct nfs_export_options **nxop)
2115 {
2116 vnode_t dp;
2117 int error;
2118 struct componentname *cnp = &nip->ni_cnd;
2119 uint32_t cnflags;
2120 char *tmppn;
2121
2122 *retdirp = NULL;
2123
2124 /*
2125 * Extract and set starting directory.
2126 */
2127 error = nfsrv_fhtovp(nfhp, nd, &dp, nxp, nxop);
2128 if (error)
2129 goto out;
2130 error = nfsrv_credcheck(nd, ctx, *nxp, *nxop);
2131 if (error || (vnode_vtype(dp) != VDIR)) {
2132 vnode_put(dp);
2133 error = ENOTDIR;
2134 goto out;
2135 }
2136 *retdirp = dp;
2137
2138 nip->ni_cnd.cn_context = ctx;
2139
2140 if (*nxop && ((*nxop)->nxo_flags & NX_READONLY))
2141 cnp->cn_flags |= RDONLY;
2142
2143 cnp->cn_flags |= NOCROSSMOUNT;
2144 cnp->cn_nameptr = cnp->cn_pnbuf;
2145 nip->ni_usedvp = nip->ni_startdir = dp;
2146
2147 /*
2148 * And call lookup() to do the real work
2149 */
2150 cnflags = nip->ni_cnd.cn_flags; /* store in case we have to restore */
2151 while ((error = lookup(nip)) == ERECYCLE) {
2152 nip->ni_cnd.cn_flags = cnflags;
2153 cnp->cn_nameptr = cnp->cn_pnbuf;
2154 nip->ni_usedvp = nip->ni_dvp = nip->ni_startdir = dp;
2155 }
2156 if (error)
2157 goto out;
2158
2159 /* Check for encountering a symbolic link */
2160 if (cnp->cn_flags & ISSYMLINK) {
2161 if (cnp->cn_flags & (LOCKPARENT | WANTPARENT))
2162 vnode_put(nip->ni_dvp);
2163 if (nip->ni_vp) {
2164 vnode_put(nip->ni_vp);
2165 nip->ni_vp = NULL;
2166 }
2167 error = EINVAL;
2168 }
2169 out:
2170 if (error) {
2171 tmppn = cnp->cn_pnbuf;
2172 cnp->cn_pnbuf = NULL;
2173 cnp->cn_flags &= ~HASBUF;
2174 FREE_ZONE(tmppn, cnp->cn_pnlen, M_NAMEI);
2175 }
2176 return (error);
2177 }
2178
2179 /*
2180 * A fiddled version of m_adj() that ensures null fill to a 4-byte
2181 * boundary and only trims off the back end
2182 */
2183 void
2184 nfsm_adj(mbuf_t mp, int len, int nul)
2185 {
2186 mbuf_t m, mnext;
2187 int count, i, mlen;
2188 char *cp;
2189
2190 /*
2191 * Trim from tail. Scan the mbuf chain,
2192 * calculating its length and finding the last mbuf.
2193 * If the adjustment only affects this mbuf, then just
2194 * adjust and return. Otherwise, rescan and truncate
2195 * after the remaining size.
2196 */
2197 count = 0;
2198 m = mp;
2199 for (;;) {
2200 mlen = mbuf_len(m);
2201 count += mlen;
2202 mnext = mbuf_next(m);
2203 if (mnext == NULL)
2204 break;
2205 m = mnext;
2206 }
2207 if (mlen > len) {
2208 mlen -= len;
2209 mbuf_setlen(m, mlen);
2210 if (nul > 0) {
2211 cp = (caddr_t)mbuf_data(m) + mlen - nul;
2212 for (i = 0; i < nul; i++)
2213 *cp++ = '\0';
2214 }
2215 return;
2216 }
2217 count -= len;
2218 if (count < 0)
2219 count = 0;
2220 /*
2221 * Correct length for chain is "count".
2222 * Find the mbuf with last data, adjust its length,
2223 * and toss data from remaining mbufs on chain.
2224 */
2225 for (m = mp; m; m = mbuf_next(m)) {
2226 mlen = mbuf_len(m);
2227 if (mlen >= count) {
2228 mlen = count;
2229 mbuf_setlen(m, count);
2230 if (nul > 0) {
2231 cp = (caddr_t)mbuf_data(m) + mlen - nul;
2232 for (i = 0; i < nul; i++)
2233 *cp++ = '\0';
2234 }
2235 break;
2236 }
2237 count -= mlen;
2238 }
2239 for (m = mbuf_next(m); m; m = mbuf_next(m))
2240 mbuf_setlen(m, 0);
2241 }
2242
2243 /*
2244 * Trim the header out of the mbuf list and trim off any trailing
2245 * junk so that the mbuf list has only the write data.
2246 */
2247 int
2248 nfsm_chain_trim_data(struct nfsm_chain *nmc, int len, int *mlen)
2249 {
2250 int cnt = 0, dlen, adjust;
2251 caddr_t data;
2252 mbuf_t m;
2253
2254 if (mlen)
2255 *mlen = 0;
2256
2257 /* trim header */
2258 for (m = nmc->nmc_mhead; m && (m != nmc->nmc_mcur); m = mbuf_next(m))
2259 mbuf_setlen(m, 0);
2260 if (!m)
2261 return (EIO);
2262
2263 /* trim current mbuf */
2264 data = mbuf_data(m);
2265 dlen = mbuf_len(m);
2266 adjust = nmc->nmc_ptr - data;
2267 dlen -= adjust;
2268 if ((dlen > 0) && (adjust > 0)) {
2269 if (mbuf_setdata(m, nmc->nmc_ptr, dlen))
2270 return(EIO);
2271 } else
2272 mbuf_setlen(m, dlen);
2273
2274 /* skip next len bytes */
2275 for (; m && (cnt < len); m = mbuf_next(m)) {
2276 dlen = mbuf_len(m);
2277 cnt += dlen;
2278 if (cnt > len) {
2279 /* truncate to end of data */
2280 mbuf_setlen(m, dlen - (cnt - len));
2281 if (m == nmc->nmc_mcur)
2282 nmc->nmc_left -= (cnt - len);
2283 cnt = len;
2284 }
2285 }
2286 if (mlen)
2287 *mlen = cnt;
2288
2289 /* trim any trailing data */
2290 if (m == nmc->nmc_mcur)
2291 nmc->nmc_left = 0;
2292 for (; m; m = mbuf_next(m))
2293 mbuf_setlen(m, 0);
2294
2295 return (0);
2296 }
2297
2298 int
2299 nfsm_chain_add_fattr(
2300 struct nfsrv_descript *nd,
2301 struct nfsm_chain *nmc,
2302 struct vnode_attr *vap)
2303 {
2304 int error = 0;
2305
2306 // XXX Should we assert here that all fields are supported?
2307
2308 nfsm_chain_add_32(error, nmc, vtonfs_type(vap->va_type, nd->nd_vers));
2309 if (nd->nd_vers == NFS_VER3) {
2310 nfsm_chain_add_32(error, nmc, vap->va_mode & 07777);
2311 } else {
2312 nfsm_chain_add_32(error, nmc, vtonfsv2_mode(vap->va_type, vap->va_mode));
2313 }
2314 nfsm_chain_add_32(error, nmc, vap->va_nlink);
2315 nfsm_chain_add_32(error, nmc, vap->va_uid);
2316 nfsm_chain_add_32(error, nmc, vap->va_gid);
2317 if (nd->nd_vers == NFS_VER3) {
2318 nfsm_chain_add_64(error, nmc, vap->va_data_size);
2319 nfsm_chain_add_64(error, nmc, vap->va_data_alloc);
2320 nfsm_chain_add_32(error, nmc, major(vap->va_rdev));
2321 nfsm_chain_add_32(error, nmc, minor(vap->va_rdev));
2322 nfsm_chain_add_64(error, nmc, vap->va_fsid);
2323 nfsm_chain_add_64(error, nmc, vap->va_fileid);
2324 } else {
2325 nfsm_chain_add_32(error, nmc, vap->va_data_size);
2326 nfsm_chain_add_32(error, nmc, NFS_FABLKSIZE);
2327 if (vap->va_type == VFIFO)
2328 nfsm_chain_add_32(error, nmc, 0xffffffff);
2329 else
2330 nfsm_chain_add_32(error, nmc, vap->va_rdev);
2331 nfsm_chain_add_32(error, nmc, vap->va_data_alloc / NFS_FABLKSIZE);
2332 nfsm_chain_add_32(error, nmc, vap->va_fsid);
2333 nfsm_chain_add_32(error, nmc, vap->va_fileid);
2334 }
2335 nfsm_chain_add_time(error, nmc, nd->nd_vers, &vap->va_access_time);
2336 nfsm_chain_add_time(error, nmc, nd->nd_vers, &vap->va_modify_time);
2337 nfsm_chain_add_time(error, nmc, nd->nd_vers, &vap->va_change_time);
2338
2339 return (error);
2340 }
2341
2342 int
2343 nfsm_chain_get_sattr(
2344 struct nfsrv_descript *nd,
2345 struct nfsm_chain *nmc,
2346 struct vnode_attr *vap)
2347 {
2348 int error = 0;
2349 uint32_t val = 0;
2350 uint64_t val64 = 0;
2351 struct timespec now;
2352
2353 if (nd->nd_vers == NFS_VER2) {
2354 /*
2355 * There is/was a bug in the Sun client that puts 0xffff in the mode
2356 * field of sattr when it should put in 0xffffffff. The u_short
2357 * doesn't sign extend. So check the low order 2 bytes for 0xffff.
2358 */
2359 nfsm_chain_get_32(error, nmc, val);
2360 if ((val & 0xffff) != 0xffff) {
2361 VATTR_SET(vap, va_mode, val & 07777);
2362 /* save the "type" bits for NFSv2 create */
2363 VATTR_SET(vap, va_type, IFTOVT(val));
2364 VATTR_CLEAR_ACTIVE(vap, va_type);
2365 }
2366 nfsm_chain_get_32(error, nmc, val);
2367 if (val != (uint32_t)-1)
2368 VATTR_SET(vap, va_uid, val);
2369 nfsm_chain_get_32(error, nmc, val);
2370 if (val != (uint32_t)-1)
2371 VATTR_SET(vap, va_gid, val);
2372 /* save the "size" bits for NFSv2 create (even if they appear unset) */
2373 nfsm_chain_get_32(error, nmc, val);
2374 VATTR_SET(vap, va_data_size, val);
2375 if (val == (uint32_t)-1)
2376 VATTR_CLEAR_ACTIVE(vap, va_data_size);
2377 nfsm_chain_get_time(error, nmc, NFS_VER2,
2378 vap->va_access_time.tv_sec,
2379 vap->va_access_time.tv_nsec);
2380 if (vap->va_access_time.tv_sec != -1)
2381 VATTR_SET_ACTIVE(vap, va_access_time);
2382 nfsm_chain_get_time(error, nmc, NFS_VER2,
2383 vap->va_modify_time.tv_sec,
2384 vap->va_modify_time.tv_nsec);
2385 if (vap->va_modify_time.tv_sec != -1)
2386 VATTR_SET_ACTIVE(vap, va_modify_time);
2387 return (error);
2388 }
2389
2390 /* NFSv3 */
2391 nfsm_chain_get_32(error, nmc, val);
2392 if (val) {
2393 nfsm_chain_get_32(error, nmc, val);
2394 VATTR_SET(vap, va_mode, val & 07777);
2395 }
2396 nfsm_chain_get_32(error, nmc, val);
2397 if (val) {
2398 nfsm_chain_get_32(error, nmc, val);
2399 VATTR_SET(vap, va_uid, val);
2400 }
2401 nfsm_chain_get_32(error, nmc, val);
2402 if (val) {
2403 nfsm_chain_get_32(error, nmc, val);
2404 VATTR_SET(vap, va_gid, val);
2405 }
2406 nfsm_chain_get_32(error, nmc, val);
2407 if (val) {
2408 nfsm_chain_get_64(error, nmc, val64);
2409 VATTR_SET(vap, va_data_size, val64);
2410 }
2411 nanotime(&now);
2412 nfsm_chain_get_32(error, nmc, val);
2413 switch (val) {
2414 case NFS_TIME_SET_TO_CLIENT:
2415 nfsm_chain_get_time(error, nmc, nd->nd_vers,
2416 vap->va_access_time.tv_sec,
2417 vap->va_access_time.tv_nsec);
2418 VATTR_SET_ACTIVE(vap, va_access_time);
2419 vap->va_vaflags &= ~VA_UTIMES_NULL;
2420 break;
2421 case NFS_TIME_SET_TO_SERVER:
2422 VATTR_SET(vap, va_access_time, now);
2423 vap->va_vaflags |= VA_UTIMES_NULL;
2424 break;
2425 }
2426 nfsm_chain_get_32(error, nmc, val);
2427 switch (val) {
2428 case NFS_TIME_SET_TO_CLIENT:
2429 nfsm_chain_get_time(error, nmc, nd->nd_vers,
2430 vap->va_modify_time.tv_sec,
2431 vap->va_modify_time.tv_nsec);
2432 VATTR_SET_ACTIVE(vap, va_modify_time);
2433 vap->va_vaflags &= ~VA_UTIMES_NULL;
2434 break;
2435 case NFS_TIME_SET_TO_SERVER:
2436 VATTR_SET(vap, va_modify_time, now);
2437 if (!VATTR_IS_ACTIVE(vap, va_access_time))
2438 vap->va_vaflags |= VA_UTIMES_NULL;
2439 break;
2440 }
2441
2442 return (error);
2443 }
2444
2445 /*
2446 * Compare two security flavor structs
2447 */
2448 int
2449 nfsrv_cmp_secflavs(struct nfs_sec *sf1, struct nfs_sec *sf2)
2450 {
2451 int i;
2452
2453 if (sf1->count != sf2->count)
2454 return 1;
2455 for (i = 0; i < sf1->count; i++)
2456 if (sf1->flavors[i] != sf2->flavors[i])
2457 return 1;
2458 return 0;
2459 }
2460
2461 /*
2462 * Build hash lists of net addresses and hang them off the NFS export.
2463 * Called by nfsrv_export() to set up the lists of export addresses.
2464 */
2465 int
2466 nfsrv_hang_addrlist(struct nfs_export *nx, struct user_nfs_export_args *unxa)
2467 {
2468 struct nfs_export_net_args nxna;
2469 struct nfs_netopt *no, *rn_no;
2470 struct radix_node_head *rnh;
2471 struct radix_node *rn;
2472 struct sockaddr *saddr, *smask;
2473 struct domain *dom;
2474 int i, error;
2475 unsigned int net;
2476 user_addr_t uaddr;
2477 kauth_cred_t cred;
2478
2479 uaddr = unxa->nxa_nets;
2480 for (net = 0; net < unxa->nxa_netcount; net++, uaddr += sizeof(nxna)) {
2481 error = copyin(uaddr, &nxna, sizeof(nxna));
2482 if (error)
2483 return (error);
2484
2485 if (nxna.nxna_flags & (NX_MAPROOT|NX_MAPALL)) {
2486 struct posix_cred temp_pcred;
2487 bzero(&temp_pcred, sizeof(temp_pcred));
2488 temp_pcred.cr_uid = nxna.nxna_cred.cr_uid;
2489 temp_pcred.cr_ngroups = nxna.nxna_cred.cr_ngroups;
2490 for (i=0; i < nxna.nxna_cred.cr_ngroups && i < NGROUPS; i++)
2491 temp_pcred.cr_groups[i] = nxna.nxna_cred.cr_groups[i];
2492 cred = posix_cred_create(&temp_pcred);
2493 if (!IS_VALID_CRED(cred))
2494 return (ENOMEM);
2495 } else {
2496 cred = NOCRED;
2497 }
2498
2499 if (nxna.nxna_addr.ss_len == 0) {
2500 /* No address means this is a default/world export */
2501 if (nx->nx_flags & NX_DEFAULTEXPORT) {
2502 if (IS_VALID_CRED(cred))
2503 kauth_cred_unref(&cred);
2504 return (EEXIST);
2505 }
2506 nx->nx_flags |= NX_DEFAULTEXPORT;
2507 nx->nx_defopt.nxo_flags = nxna.nxna_flags;
2508 nx->nx_defopt.nxo_cred = cred;
2509 bcopy(&nxna.nxna_sec, &nx->nx_defopt.nxo_sec, sizeof(struct nfs_sec));
2510 nx->nx_expcnt++;
2511 continue;
2512 }
2513
2514 i = sizeof(struct nfs_netopt);
2515 i += nxna.nxna_addr.ss_len + nxna.nxna_mask.ss_len;
2516 MALLOC(no, struct nfs_netopt *, i, M_NETADDR, M_WAITOK);
2517 if (!no) {
2518 if (IS_VALID_CRED(cred))
2519 kauth_cred_unref(&cred);
2520 return (ENOMEM);
2521 }
2522 bzero(no, sizeof(struct nfs_netopt));
2523 no->no_opt.nxo_flags = nxna.nxna_flags;
2524 no->no_opt.nxo_cred = cred;
2525 bcopy(&nxna.nxna_sec, &no->no_opt.nxo_sec, sizeof(struct nfs_sec));
2526
2527 saddr = (struct sockaddr *)(no + 1);
2528 bcopy(&nxna.nxna_addr, saddr, nxna.nxna_addr.ss_len);
2529 if (nxna.nxna_mask.ss_len) {
2530 smask = (struct sockaddr *)((caddr_t)saddr + nxna.nxna_addr.ss_len);
2531 bcopy(&nxna.nxna_mask, smask, nxna.nxna_mask.ss_len);
2532 } else {
2533 smask = NULL;
2534 }
2535 i = saddr->sa_family;
2536 if ((rnh = nx->nx_rtable[i]) == 0) {
2537 /*
2538 * Seems silly to initialize every AF when most are not
2539 * used, do so on demand here
2540 */
2541 TAILQ_FOREACH(dom, &domains, dom_entry) {
2542 if (dom->dom_family == i && dom->dom_rtattach) {
2543 dom->dom_rtattach((void **)&nx->nx_rtable[i],
2544 dom->dom_rtoffset);
2545 break;
2546 }
2547 }
2548 if ((rnh = nx->nx_rtable[i]) == 0) {
2549 if (IS_VALID_CRED(cred))
2550 kauth_cred_unref(&cred);
2551 _FREE(no, M_NETADDR);
2552 return (ENOBUFS);
2553 }
2554 }
2555 rn = (*rnh->rnh_addaddr)((caddr_t)saddr, (caddr_t)smask, rnh, no->no_rnodes);
2556 if (rn == 0) {
2557 /*
2558 * One of the reasons that rnh_addaddr may fail is that
2559 * the entry already exists. To check for this case, we
2560 * look up the entry to see if it is there. If so, we
2561 * do not need to make a new entry but do continue.
2562 *
2563 * XXX should this be rnh_lookup() instead?
2564 */
2565 int matched = 0;
2566 rn = (*rnh->rnh_matchaddr)((caddr_t)saddr, rnh);
2567 rn_no = (struct nfs_netopt *)rn;
2568 if (rn != 0 && (rn->rn_flags & RNF_ROOT) == 0 &&
2569 (rn_no->no_opt.nxo_flags == nxna.nxna_flags) &&
2570 (!nfsrv_cmp_secflavs(&rn_no->no_opt.nxo_sec, &nxna.nxna_sec))) {
2571 kauth_cred_t cred2 = rn_no->no_opt.nxo_cred;
2572 if (cred == cred2) {
2573 /* creds are same (or both NULL) */
2574 matched = 1;
2575 } else if (cred && cred2 && (kauth_cred_getuid(cred) == kauth_cred_getuid(cred2))) {
2576 /*
2577 * Now compare the effective and
2578 * supplementary groups...
2579 *
2580 * Note: This comparison, as written,
2581 * does not correctly indicate that
2582 * the groups are equivalent, since
2583 * other than the first supplementary
2584 * group, which is also the effective
2585 * group, order on the remaining groups
2586 * doesn't matter, and this is an
2587 * ordered compare.
2588 */
2589 gid_t groups[NGROUPS];
2590 gid_t groups2[NGROUPS];
2591 int groupcount = NGROUPS;
2592 int group2count = NGROUPS;
2593
2594 if (!kauth_cred_getgroups(cred, groups, &groupcount) &&
2595 !kauth_cred_getgroups(cred2, groups2, &group2count) &&
2596 groupcount == group2count) {
2597 for (i=0; i < group2count; i++)
2598 if (groups[i] != groups2[i])
2599 break;
2600 if (i >= group2count || i >= NGROUPS)
2601 matched = 1;
2602 }
2603 }
2604 }
2605 if (IS_VALID_CRED(cred))
2606 kauth_cred_unref(&cred);
2607 _FREE(no, M_NETADDR);
2608 if (matched)
2609 continue;
2610 return (EPERM);
2611 }
2612 nx->nx_expcnt++;
2613 }
2614
2615 return (0);
2616 }
2617
2618 /*
2619 * In order to properly track an export's netopt count, we need to pass
2620 * an additional argument to nfsrv_free_netopt() so that it can decrement
2621 * the export's netopt count.
2622 */
2623 struct nfsrv_free_netopt_arg {
2624 uint32_t *cnt;
2625 struct radix_node_head *rnh;
2626 };
2627
2628 int
2629 nfsrv_free_netopt(struct radix_node *rn, void *w)
2630 {
2631 struct nfsrv_free_netopt_arg *fna = (struct nfsrv_free_netopt_arg *)w;
2632 struct radix_node_head *rnh = fna->rnh;
2633 uint32_t *cnt = fna->cnt;
2634 struct nfs_netopt *nno = (struct nfs_netopt *)rn;
2635
2636 (*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
2637 if (IS_VALID_CRED(nno->no_opt.nxo_cred))
2638 kauth_cred_unref(&nno->no_opt.nxo_cred);
2639 _FREE((caddr_t)rn, M_NETADDR);
2640 *cnt -= 1;
2641 return (0);
2642 }
2643
2644 /*
2645 * Free the net address hash lists that are hanging off the mount points.
2646 */
2647 int
2648 nfsrv_free_addrlist(struct nfs_export *nx, struct user_nfs_export_args *unxa)
2649 {
2650 struct nfs_export_net_args nxna;
2651 struct radix_node_head *rnh;
2652 struct radix_node *rn;
2653 struct nfsrv_free_netopt_arg fna;
2654 struct nfs_netopt *nno;
2655 user_addr_t uaddr;
2656 unsigned int net;
2657 int i, error;
2658
2659 if (!unxa || !unxa->nxa_netcount) {
2660 /* delete everything */
2661 for (i = 0; i <= AF_MAX; i++)
2662 if ( (rnh = nx->nx_rtable[i]) ) {
2663 fna.rnh = rnh;
2664 fna.cnt = &nx->nx_expcnt;
2665 (*rnh->rnh_walktree)(rnh, nfsrv_free_netopt, (caddr_t)&fna);
2666 _FREE((caddr_t)rnh, M_RTABLE);
2667 nx->nx_rtable[i] = 0;
2668 }
2669 return (0);
2670 }
2671
2672 /* delete only the exports specified */
2673 uaddr = unxa->nxa_nets;
2674 for (net = 0; net < unxa->nxa_netcount; net++, uaddr += sizeof(nxna)) {
2675 error = copyin(uaddr, &nxna, sizeof(nxna));
2676 if (error)
2677 return (error);
2678
2679 if (nxna.nxna_addr.ss_len == 0) {
2680 /* No address means this is a default/world export */
2681 if (nx->nx_flags & NX_DEFAULTEXPORT) {
2682 nx->nx_flags &= ~NX_DEFAULTEXPORT;
2683 if (IS_VALID_CRED(nx->nx_defopt.nxo_cred)) {
2684 kauth_cred_unref(&nx->nx_defopt.nxo_cred);
2685 }
2686 nx->nx_expcnt--;
2687 }
2688 continue;
2689 }
2690
2691 if ((rnh = nx->nx_rtable[nxna.nxna_addr.ss_family]) == 0) {
2692 /* AF not initialized? */
2693 if (!(unxa->nxa_flags & NXA_ADD))
2694 printf("nfsrv_free_addrlist: address not found (0)\n");
2695 continue;
2696 }
2697
2698 rn = (*rnh->rnh_lookup)(&nxna.nxna_addr,
2699 nxna.nxna_mask.ss_len ? &nxna.nxna_mask : NULL, rnh);
2700 if (!rn || (rn->rn_flags & RNF_ROOT)) {
2701 if (!(unxa->nxa_flags & NXA_ADD))
2702 printf("nfsrv_free_addrlist: address not found (1)\n");
2703 continue;
2704 }
2705
2706 (*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
2707 nno = (struct nfs_netopt *)rn;
2708 if (IS_VALID_CRED(nno->no_opt.nxo_cred))
2709 kauth_cred_unref(&nno->no_opt.nxo_cred);
2710 _FREE((caddr_t)rn, M_NETADDR);
2711
2712 nx->nx_expcnt--;
2713 if (nx->nx_expcnt == ((nx->nx_flags & NX_DEFAULTEXPORT) ? 1 : 0)) {
2714 /* no more entries in rnh, so free it up */
2715 _FREE((caddr_t)rnh, M_RTABLE);
2716 nx->nx_rtable[nxna.nxna_addr.ss_family] = 0;
2717 }
2718 }
2719
2720 return (0);
2721 }
2722
2723 void enablequotas(struct mount *mp, vfs_context_t ctx); // XXX
2724
2725 int
2726 nfsrv_export(struct user_nfs_export_args *unxa, vfs_context_t ctx)
2727 {
2728 int error = 0;
2729 size_t pathlen;
2730 struct nfs_exportfs *nxfs, *nxfs2, *nxfs3;
2731 struct nfs_export *nx, *nx2, *nx3;
2732 struct nfs_filehandle nfh;
2733 struct nameidata mnd, xnd;
2734 vnode_t mvp = NULL, xvp = NULL;
2735 mount_t mp = NULL;
2736 char path[MAXPATHLEN];
2737 int expisroot;
2738
2739 if (unxa->nxa_flags == NXA_CHECK) {
2740 /* just check if the path is an NFS-exportable file system */
2741 error = copyinstr(unxa->nxa_fspath, path, MAXPATHLEN, &pathlen);
2742 if (error)
2743 return (error);
2744 NDINIT(&mnd, LOOKUP, OP_LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
2745 UIO_SYSSPACE, CAST_USER_ADDR_T(path), ctx);
2746 error = namei(&mnd);
2747 if (error)
2748 return (error);
2749 mvp = mnd.ni_vp;
2750 mp = vnode_mount(mvp);
2751 /* make sure it's the root of a file system */
2752 if (!vnode_isvroot(mvp))
2753 error = EINVAL;
2754 /* make sure the file system is NFS-exportable */
2755 if (!error) {
2756 nfh.nfh_len = NFSV3_MAX_FID_SIZE;
2757 error = VFS_VPTOFH(mvp, (int*)&nfh.nfh_len, &nfh.nfh_fid[0], NULL);
2758 }
2759 if (!error && (nfh.nfh_len > (int)NFSV3_MAX_FID_SIZE))
2760 error = EIO;
2761 if (!error && !(mp->mnt_vtable->vfc_vfsflags & VFC_VFSREADDIR_EXTENDED))
2762 error = EISDIR;
2763 vnode_put(mvp);
2764 nameidone(&mnd);
2765 return (error);
2766 }
2767
2768 /* all other operations: must be super user */
2769 if ((error = vfs_context_suser(ctx)))
2770 return (error);
2771
2772 if (unxa->nxa_flags & NXA_DELETE_ALL) {
2773 /* delete all exports on all file systems */
2774 lck_rw_lock_exclusive(&nfsrv_export_rwlock);
2775 while ((nxfs = LIST_FIRST(&nfsrv_exports))) {
2776 mp = vfs_getvfs_by_mntonname(nxfs->nxfs_path);
2777 if (mp) {
2778 vfs_clearflags(mp, MNT_EXPORTED);
2779 mount_iterdrop(mp);
2780 mp = NULL;
2781 }
2782 /* delete all exports on this file system */
2783 while ((nx = LIST_FIRST(&nxfs->nxfs_exports))) {
2784 LIST_REMOVE(nx, nx_next);
2785 LIST_REMOVE(nx, nx_hash);
2786 /* delete all netopts for this export */
2787 nfsrv_free_addrlist(nx, NULL);
2788 nx->nx_flags &= ~NX_DEFAULTEXPORT;
2789 if (IS_VALID_CRED(nx->nx_defopt.nxo_cred)) {
2790 kauth_cred_unref(&nx->nx_defopt.nxo_cred);
2791 }
2792 /* free active user list for this export */
2793 nfsrv_free_user_list(&nx->nx_user_list);
2794 FREE(nx->nx_path, M_TEMP);
2795 FREE(nx, M_TEMP);
2796 }
2797 LIST_REMOVE(nxfs, nxfs_next);
2798 FREE(nxfs->nxfs_path, M_TEMP);
2799 FREE(nxfs, M_TEMP);
2800 }
2801 if (nfsrv_export_hashtbl) {
2802 /* all exports deleted, clean up export hash table */
2803 FREE(nfsrv_export_hashtbl, M_TEMP);
2804 nfsrv_export_hashtbl = NULL;
2805 }
2806 lck_rw_done(&nfsrv_export_rwlock);
2807 return (0);
2808 }
2809
2810 error = copyinstr(unxa->nxa_fspath, path, MAXPATHLEN, &pathlen);
2811 if (error)
2812 return (error);
2813
2814 lck_rw_lock_exclusive(&nfsrv_export_rwlock);
2815
2816 /* init export hash table if not already */
2817 if (!nfsrv_export_hashtbl) {
2818 if (nfsrv_export_hash_size <= 0)
2819 nfsrv_export_hash_size = NFSRVEXPHASHSZ;
2820 nfsrv_export_hashtbl = hashinit(nfsrv_export_hash_size, M_TEMP, &nfsrv_export_hash);
2821 }
2822
2823 // first check if we've already got an exportfs with the given ID
2824 LIST_FOREACH(nxfs, &nfsrv_exports, nxfs_next) {
2825 if (nxfs->nxfs_id == unxa->nxa_fsid)
2826 break;
2827 }
2828 if (nxfs) {
2829 /* verify exported FS path matches given path */
2830 if (strncmp(path, nxfs->nxfs_path, MAXPATHLEN)) {
2831 error = EEXIST;
2832 goto unlock_out;
2833 }
2834 if ((unxa->nxa_flags & (NXA_ADD|NXA_OFFLINE)) == NXA_ADD) {
2835 /* if adding, verify that the mount is still what we expect */
2836 mp = vfs_getvfs_by_mntonname(nxfs->nxfs_path);
2837 if (mp) {
2838 mount_ref(mp, 0);
2839 mount_iterdrop(mp);
2840 }
2841 /* find exported FS root vnode */
2842 NDINIT(&mnd, LOOKUP, OP_LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
2843 UIO_SYSSPACE, CAST_USER_ADDR_T(nxfs->nxfs_path), ctx);
2844 error = namei(&mnd);
2845 if (error)
2846 goto unlock_out;
2847 mvp = mnd.ni_vp;
2848 /* make sure it's (still) the root of a file system */
2849 if (!vnode_isvroot(mvp)) {
2850 error = EINVAL;
2851 goto out;
2852 }
2853 /* sanity check: this should be same mount */
2854 if (mp != vnode_mount(mvp)) {
2855 error = EINVAL;
2856 goto out;
2857 }
2858 }
2859 } else {
2860 /* no current exported file system with that ID */
2861 if (!(unxa->nxa_flags & NXA_ADD)) {
2862 error = ENOENT;
2863 goto unlock_out;
2864 }
2865
2866 /* find exported FS root vnode */
2867 NDINIT(&mnd, LOOKUP, OP_LOOKUP, FOLLOW | LOCKLEAF | AUDITVNPATH1,
2868 UIO_SYSSPACE, CAST_USER_ADDR_T(path), ctx);
2869 error = namei(&mnd);
2870 if (error) {
2871 if (!(unxa->nxa_flags & NXA_OFFLINE))
2872 goto unlock_out;
2873 } else {
2874 mvp = mnd.ni_vp;
2875 /* make sure it's the root of a file system */
2876 if (!vnode_isvroot(mvp)) {
2877 /* bail if not marked offline */
2878 if (!(unxa->nxa_flags & NXA_OFFLINE)) {
2879 error = EINVAL;
2880 goto out;
2881 }
2882 vnode_put(mvp);
2883 nameidone(&mnd);
2884 mvp = NULL;
2885 } else {
2886 mp = vnode_mount(mvp);
2887 mount_ref(mp, 0);
2888
2889 /* make sure the file system is NFS-exportable */
2890 nfh.nfh_len = NFSV3_MAX_FID_SIZE;
2891 error = VFS_VPTOFH(mvp, (int*)&nfh.nfh_len, &nfh.nfh_fid[0], NULL);
2892 if (!error && (nfh.nfh_len > (int)NFSV3_MAX_FID_SIZE))
2893 error = EIO;
2894 if (!error && !(mp->mnt_vtable->vfc_vfsflags & VFC_VFSREADDIR_EXTENDED))
2895 error = EISDIR;
2896 if (error)
2897 goto out;
2898 }
2899 }
2900
2901 /* add an exportfs for it */
2902 MALLOC(nxfs, struct nfs_exportfs *, sizeof(struct nfs_exportfs), M_TEMP, M_WAITOK);
2903 if (!nxfs) {
2904 error = ENOMEM;
2905 goto out;
2906 }
2907 bzero(nxfs, sizeof(struct nfs_exportfs));
2908 nxfs->nxfs_id = unxa->nxa_fsid;
2909 MALLOC(nxfs->nxfs_path, char*, pathlen, M_TEMP, M_WAITOK);
2910 if (!nxfs->nxfs_path) {
2911 FREE(nxfs, M_TEMP);
2912 error = ENOMEM;
2913 goto out;
2914 }
2915 bcopy(path, nxfs->nxfs_path, pathlen);
2916 /* insert into list in reverse-sorted order */
2917 nxfs3 = NULL;
2918 LIST_FOREACH(nxfs2, &nfsrv_exports, nxfs_next) {
2919 if (strncmp(nxfs->nxfs_path, nxfs2->nxfs_path, MAXPATHLEN) > 0)
2920 break;
2921 nxfs3 = nxfs2;
2922 }
2923 if (nxfs2)
2924 LIST_INSERT_BEFORE(nxfs2, nxfs, nxfs_next);
2925 else if (nxfs3)
2926 LIST_INSERT_AFTER(nxfs3, nxfs, nxfs_next);
2927 else
2928 LIST_INSERT_HEAD(&nfsrv_exports, nxfs, nxfs_next);
2929
2930 /* make sure any quotas are enabled before we export the file system */
2931 if (mp)
2932 enablequotas(mp, ctx);
2933 }
2934
2935 if (unxa->nxa_exppath) {
2936 error = copyinstr(unxa->nxa_exppath, path, MAXPATHLEN, &pathlen);
2937 if (error)
2938 goto out;
2939 LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next) {
2940 if (nx->nx_id == unxa->nxa_expid)
2941 break;
2942 }
2943 if (nx) {
2944 /* verify exported FS path matches given path */
2945 if (strncmp(path, nx->nx_path, MAXPATHLEN)) {
2946 error = EEXIST;
2947 goto out;
2948 }
2949 } else {
2950 /* no current export with that ID */
2951 if (!(unxa->nxa_flags & NXA_ADD)) {
2952 error = ENOENT;
2953 goto out;
2954 }
2955 /* add an export for it */
2956 MALLOC(nx, struct nfs_export *, sizeof(struct nfs_export), M_TEMP, M_WAITOK);
2957 if (!nx) {
2958 error = ENOMEM;
2959 goto out1;
2960 }
2961 bzero(nx, sizeof(struct nfs_export));
2962 nx->nx_id = unxa->nxa_expid;
2963 nx->nx_fs = nxfs;
2964 microtime(&nx->nx_exptime);
2965 MALLOC(nx->nx_path, char*, pathlen, M_TEMP, M_WAITOK);
2966 if (!nx->nx_path) {
2967 error = ENOMEM;
2968 FREE(nx, M_TEMP);
2969 nx = NULL;
2970 goto out1;
2971 }
2972 bcopy(path, nx->nx_path, pathlen);
2973 /* initialize the active user list */
2974 nfsrv_init_user_list(&nx->nx_user_list);
2975 /* insert into list in reverse-sorted order */
2976 nx3 = NULL;
2977 LIST_FOREACH(nx2, &nxfs->nxfs_exports, nx_next) {
2978 if (strncmp(nx->nx_path, nx2->nx_path, MAXPATHLEN) > 0)
2979 break;
2980 nx3 = nx2;
2981 }
2982 if (nx2)
2983 LIST_INSERT_BEFORE(nx2, nx, nx_next);
2984 else if (nx3)
2985 LIST_INSERT_AFTER(nx3, nx, nx_next);
2986 else
2987 LIST_INSERT_HEAD(&nxfs->nxfs_exports, nx, nx_next);
2988 /* insert into hash */
2989 LIST_INSERT_HEAD(NFSRVEXPHASH(nxfs->nxfs_id, nx->nx_id), nx, nx_hash);
2990
2991 /*
2992 * We don't allow/support nested exports. Check if the new entry
2993 * nests with the entries before and after or if there's an
2994 * entry for the file system root and subdirs.
2995 */
2996 error = 0;
2997 if ((nx3 && !strncmp(nx3->nx_path, nx->nx_path, pathlen - 1) &&
2998 (nx3->nx_path[pathlen-1] == '/')) ||
2999 (nx2 && !strncmp(nx2->nx_path, nx->nx_path, strlen(nx2->nx_path)) &&
3000 (nx->nx_path[strlen(nx2->nx_path)] == '/')))
3001 error = EINVAL;
3002 if (!error) {
3003 /* check export conflict with fs root export and vice versa */
3004 expisroot = !nx->nx_path[0] ||
3005 ((nx->nx_path[0] == '.') && !nx->nx_path[1]);
3006 LIST_FOREACH(nx2, &nxfs->nxfs_exports, nx_next) {
3007 if (expisroot) {
3008 if (nx2 != nx)
3009 break;
3010 } else if (!nx2->nx_path[0])
3011 break;
3012 else if ((nx2->nx_path[0] == '.') && !nx2->nx_path[1])
3013 break;
3014 }
3015 if (nx2)
3016 error = EINVAL;
3017 }
3018 if (error) {
3019 /*
3020 * Don't actually return an error because mountd is
3021 * probably about to delete the conflicting export.
3022 * This can happen when a new export momentarily conflicts
3023 * with an old export while the transition is being made.
3024 * Theoretically, mountd could be written to avoid this
3025 * transient situation - but it would greatly increase the
3026 * complexity of mountd for very little overall benefit.
3027 */
3028 printf("nfsrv_export: warning: nested exports: %s/%s\n",
3029 nxfs->nxfs_path, nx->nx_path);
3030 error = 0;
3031 }
3032 nx->nx_fh.nfh_xh.nxh_flags = NXHF_INVALIDFH;
3033 }
3034 /* make sure file handle is set up */
3035 if ((nx->nx_fh.nfh_xh.nxh_version != htonl(NFS_FH_VERSION)) ||
3036 (nx->nx_fh.nfh_xh.nxh_flags & NXHF_INVALIDFH)) {
3037 /* try to set up export root file handle */
3038 nx->nx_fh.nfh_xh.nxh_version = htonl(NFS_FH_VERSION);
3039 nx->nx_fh.nfh_xh.nxh_fsid = htonl(nx->nx_fs->nxfs_id);
3040 nx->nx_fh.nfh_xh.nxh_expid = htonl(nx->nx_id);
3041 nx->nx_fh.nfh_xh.nxh_flags = 0;
3042 nx->nx_fh.nfh_xh.nxh_reserved = 0;
3043 nx->nx_fh.nfh_fhp = (u_char*)&nx->nx_fh.nfh_xh;
3044 bzero(&nx->nx_fh.nfh_fid[0], NFSV2_MAX_FID_SIZE);
3045 if (mvp) {
3046 /* find export root vnode */
3047 if (!nx->nx_path[0] || ((nx->nx_path[0] == '.') && !nx->nx_path[1])) {
3048 /* exporting file system's root directory */
3049 xvp = mvp;
3050 vnode_get(xvp);
3051 } else {
3052 xnd.ni_cnd.cn_nameiop = LOOKUP;
3053 #if CONFIG_TRIGGERS
3054 xnd.ni_op = OP_LOOKUP;
3055 #endif
3056 xnd.ni_cnd.cn_flags = LOCKLEAF;
3057 xnd.ni_pathlen = pathlen - 1;
3058 xnd.ni_cnd.cn_nameptr = xnd.ni_cnd.cn_pnbuf = path;
3059 xnd.ni_startdir = mvp;
3060 xnd.ni_usedvp = mvp;
3061 xnd.ni_cnd.cn_context = ctx;
3062 while ((error = lookup(&xnd)) == ERECYCLE) {
3063 xnd.ni_cnd.cn_flags = LOCKLEAF;
3064 xnd.ni_cnd.cn_nameptr = xnd.ni_cnd.cn_pnbuf;
3065 xnd.ni_usedvp = xnd.ni_dvp = xnd.ni_startdir = mvp;
3066 }
3067 if (error)
3068 goto out1;
3069 xvp = xnd.ni_vp;
3070 }
3071
3072 if (vnode_vtype(xvp) != VDIR) {
3073 error = EINVAL;
3074 vnode_put(xvp);
3075 goto out1;
3076 }
3077
3078 /* grab file handle */
3079 nx->nx_fh.nfh_len = NFSV3_MAX_FID_SIZE;
3080 error = VFS_VPTOFH(xvp, (int*)&nx->nx_fh.nfh_len, &nx->nx_fh.nfh_fid[0], NULL);
3081 if (!error && (nx->nx_fh.nfh_len > (int)NFSV3_MAX_FID_SIZE)) {
3082 error = EIO;
3083 } else {
3084 nx->nx_fh.nfh_xh.nxh_fidlen = nx->nx_fh.nfh_len;
3085 nx->nx_fh.nfh_len += sizeof(nx->nx_fh.nfh_xh);
3086 }
3087
3088 vnode_put(xvp);
3089 if (error)
3090 goto out1;
3091 } else {
3092 nx->nx_fh.nfh_xh.nxh_flags = NXHF_INVALIDFH;
3093 nx->nx_fh.nfh_xh.nxh_fidlen = 0;
3094 nx->nx_fh.nfh_len = sizeof(nx->nx_fh.nfh_xh);
3095 }
3096 }
3097 } else {
3098 nx = NULL;
3099 }
3100
3101 /* perform the export changes */
3102 if (unxa->nxa_flags & NXA_DELETE) {
3103 if (!nx) {
3104 /* delete all exports on this file system */
3105 while ((nx = LIST_FIRST(&nxfs->nxfs_exports))) {
3106 LIST_REMOVE(nx, nx_next);
3107 LIST_REMOVE(nx, nx_hash);
3108 /* delete all netopts for this export */
3109 nfsrv_free_addrlist(nx, NULL);
3110 nx->nx_flags &= ~NX_DEFAULTEXPORT;
3111 if (IS_VALID_CRED(nx->nx_defopt.nxo_cred)) {
3112 kauth_cred_unref(&nx->nx_defopt.nxo_cred);
3113 }
3114 /* delete active user list for this export */
3115 nfsrv_free_user_list(&nx->nx_user_list);
3116 FREE(nx->nx_path, M_TEMP);
3117 FREE(nx, M_TEMP);
3118 }
3119 goto out1;
3120 } else if (!unxa->nxa_netcount) {
3121 /* delete all netopts for this export */
3122 nfsrv_free_addrlist(nx, NULL);
3123 nx->nx_flags &= ~NX_DEFAULTEXPORT;
3124 if (IS_VALID_CRED(nx->nx_defopt.nxo_cred)) {
3125 kauth_cred_unref(&nx->nx_defopt.nxo_cred);
3126 }
3127 } else {
3128 /* delete only the netopts for the given addresses */
3129 error = nfsrv_free_addrlist(nx, unxa);
3130 if (error)
3131 goto out1;
3132 }
3133 }
3134 if (unxa->nxa_flags & NXA_ADD) {
3135 /*
3136 * If going offline set the export time so that when
3137 * coming back on line we will present a new write verifier
3138 * to the client.
3139 */
3140 if (unxa->nxa_flags & NXA_OFFLINE)
3141 microtime(&nx->nx_exptime);
3142
3143 error = nfsrv_hang_addrlist(nx, unxa);
3144 if (!error && mp)
3145 vfs_setflags(mp, MNT_EXPORTED);
3146 }
3147
3148 out1:
3149 if (nx && !nx->nx_expcnt) {
3150 /* export has no export options */
3151 LIST_REMOVE(nx, nx_next);
3152 LIST_REMOVE(nx, nx_hash);
3153 /* delete active user list for this export */
3154 nfsrv_free_user_list(&nx->nx_user_list);
3155 FREE(nx->nx_path, M_TEMP);
3156 FREE(nx, M_TEMP);
3157 }
3158 if (LIST_EMPTY(&nxfs->nxfs_exports)) {
3159 /* exported file system has no more exports */
3160 LIST_REMOVE(nxfs, nxfs_next);
3161 FREE(nxfs->nxfs_path, M_TEMP);
3162 FREE(nxfs, M_TEMP);
3163 if (mp)
3164 vfs_clearflags(mp, MNT_EXPORTED);
3165 }
3166
3167 out:
3168 if (mvp) {
3169 vnode_put(mvp);
3170 nameidone(&mnd);
3171 }
3172 unlock_out:
3173 if (mp)
3174 mount_drop(mp, 0);
3175 lck_rw_done(&nfsrv_export_rwlock);
3176 return (error);
3177 }
3178
3179 struct nfs_export_options *
3180 nfsrv_export_lookup(struct nfs_export *nx, mbuf_t nam)
3181 {
3182 struct nfs_export_options *nxo = NULL;
3183 struct nfs_netopt *no = NULL;
3184 struct radix_node_head *rnh;
3185 struct sockaddr *saddr;
3186
3187 /* Lookup in the export list first. */
3188 if (nam != NULL) {
3189 saddr = mbuf_data(nam);
3190 rnh = nx->nx_rtable[saddr->sa_family];
3191 if (rnh != NULL) {
3192 no = (struct nfs_netopt *)
3193 (*rnh->rnh_matchaddr)((caddr_t)saddr, rnh);
3194 if (no && no->no_rnodes->rn_flags & RNF_ROOT)
3195 no = NULL;
3196 if (no)
3197 nxo = &no->no_opt;
3198 }
3199 }
3200 /* If no address match, use the default if it exists. */
3201 if ((nxo == NULL) && (nx->nx_flags & NX_DEFAULTEXPORT))
3202 nxo = &nx->nx_defopt;
3203 return (nxo);
3204 }
3205
3206 /* find an export for the given handle */
3207 struct nfs_export *
3208 nfsrv_fhtoexport(struct nfs_filehandle *nfhp)
3209 {
3210 struct nfs_exphandle *nxh = (struct nfs_exphandle*)nfhp->nfh_fhp;
3211 struct nfs_export *nx;
3212 uint32_t fsid, expid;
3213
3214 if (!nfsrv_export_hashtbl)
3215 return (NULL);
3216 fsid = ntohl(nxh->nxh_fsid);
3217 expid = ntohl(nxh->nxh_expid);
3218 nx = NFSRVEXPHASH(fsid, expid)->lh_first;
3219 for (; nx; nx = LIST_NEXT(nx, nx_hash)) {
3220 if (nx->nx_fs->nxfs_id != fsid)
3221 continue;
3222 if (nx->nx_id != expid)
3223 continue;
3224 break;
3225 }
3226 return (nx);
3227 }
3228
3229 /*
3230 * nfsrv_fhtovp() - convert FH to vnode and export info
3231 */
3232 int
3233 nfsrv_fhtovp(
3234 struct nfs_filehandle *nfhp,
3235 struct nfsrv_descript *nd,
3236 vnode_t *vpp,
3237 struct nfs_export **nxp,
3238 struct nfs_export_options **nxop)
3239 {
3240 struct nfs_exphandle *nxh = (struct nfs_exphandle*)nfhp->nfh_fhp;
3241 struct nfs_export_options *nxo;
3242 u_char *fidp;
3243 int error;
3244 struct mount *mp;
3245 mbuf_t nam = NULL;
3246 uint32_t v;
3247 int i, valid;
3248
3249 *vpp = NULL;
3250 *nxp = NULL;
3251 *nxop = NULL;
3252
3253 if (nd != NULL)
3254 nam = nd->nd_nam;
3255
3256 v = ntohl(nxh->nxh_version);
3257 if (v != NFS_FH_VERSION) {
3258 /* file handle format not supported */
3259 return (ESTALE);
3260 }
3261 if (nfhp->nfh_len > NFSV3_MAX_FH_SIZE)
3262 return (EBADRPC);
3263 if (nfhp->nfh_len < (int)sizeof(struct nfs_exphandle))
3264 return (ESTALE);
3265 v = ntohs(nxh->nxh_flags);
3266 if (v & NXHF_INVALIDFH)
3267 return (ESTALE);
3268
3269 *nxp = nfsrv_fhtoexport(nfhp);
3270 if (!*nxp)
3271 return (ESTALE);
3272
3273 /* Get the export option structure for this <export, client> tuple. */
3274 *nxop = nxo = nfsrv_export_lookup(*nxp, nam);
3275 if (nam && (*nxop == NULL))
3276 return (EACCES);
3277
3278 if (nd != NULL) {
3279 /* Validate the security flavor of the request */
3280 for (i = 0, valid = 0; i < nxo->nxo_sec.count; i++) {
3281 if (nd->nd_sec == nxo->nxo_sec.flavors[i]) {
3282 valid = 1;
3283 break;
3284 }
3285 }
3286 if (!valid) {
3287 /*
3288 * RFC 2623 section 2.3.2 recommends no authentication
3289 * requirement for certain NFS procedures used for mounting.
3290 * This allows an unauthenticated superuser on the client
3291 * to do mounts for the benefit of authenticated users.
3292 */
3293 if (nd->nd_vers == NFS_VER2)
3294 if (nd->nd_procnum == NFSV2PROC_GETATTR ||
3295 nd->nd_procnum == NFSV2PROC_STATFS)
3296 valid = 1;
3297 if (nd->nd_vers == NFS_VER3)
3298 if (nd->nd_procnum == NFSPROC_FSINFO)
3299 valid = 1;
3300
3301 if (!valid)
3302 return (NFSERR_AUTHERR | AUTH_REJECTCRED);
3303 }
3304 }
3305
3306 if (nxo && (nxo->nxo_flags & NX_OFFLINE))
3307 return ((nd == NULL || nd->nd_vers == NFS_VER2) ? ESTALE : NFSERR_TRYLATER);
3308
3309 /* find mount structure */
3310 mp = vfs_getvfs_by_mntonname((*nxp)->nx_fs->nxfs_path);
3311 if (mp) {
3312 error = vfs_busy(mp, LK_NOWAIT);
3313 mount_iterdrop(mp);
3314 if (error)
3315 mp = NULL;
3316 }
3317 if (!mp) {
3318 /*
3319 * We have an export, but no mount?
3320 * Perhaps the export just hasn't been marked offline yet.
3321 */
3322 return ((nd == NULL || nd->nd_vers == NFS_VER2) ? ESTALE : NFSERR_TRYLATER);
3323 }
3324
3325 fidp = nfhp->nfh_fhp + sizeof(*nxh);
3326 error = VFS_FHTOVP(mp, nxh->nxh_fidlen, fidp, vpp, NULL);
3327 vfs_unbusy(mp);
3328 if (error)
3329 return (error);
3330 /* vnode pointer should be good at this point or ... */
3331 if (*vpp == NULL)
3332 return (ESTALE);
3333 return (0);
3334 }
3335
3336 /*
3337 * nfsrv_credcheck() - check/map credentials according
3338 * to given export options.
3339 */
3340 int
3341 nfsrv_credcheck(
3342 struct nfsrv_descript *nd,
3343 vfs_context_t ctx,
3344 __unused struct nfs_export *nx,
3345 struct nfs_export_options *nxo)
3346 {
3347 if (nxo && nxo->nxo_cred) {
3348 if ((nxo->nxo_flags & NX_MAPALL) ||
3349 ((nxo->nxo_flags & NX_MAPROOT) && !suser(nd->nd_cr, NULL))) {
3350 kauth_cred_ref(nxo->nxo_cred);
3351 kauth_cred_unref(&nd->nd_cr);
3352 nd->nd_cr = nxo->nxo_cred;
3353 }
3354 }
3355 ctx->vc_ucred = nd->nd_cr;
3356 return (0);
3357 }
3358
3359 /*
3360 * nfsrv_vptofh() - convert vnode to file handle for given export
3361 *
3362 * If the caller is passing in a vnode for a ".." directory entry,
3363 * they can pass a directory NFS file handle (dnfhp) which will be
3364 * checked against the root export file handle. If it matches, we
3365 * refuse to provide the file handle for the out-of-export directory.
3366 */
3367 int
3368 nfsrv_vptofh(
3369 struct nfs_export *nx,
3370 int nfsvers,
3371 struct nfs_filehandle *dnfhp,
3372 vnode_t vp,
3373 vfs_context_t ctx,
3374 struct nfs_filehandle *nfhp)
3375 {
3376 int error;
3377 uint32_t maxfidsize;
3378
3379 nfhp->nfh_fhp = (u_char*)&nfhp->nfh_xh;
3380 nfhp->nfh_xh.nxh_version = htonl(NFS_FH_VERSION);
3381 nfhp->nfh_xh.nxh_fsid = htonl(nx->nx_fs->nxfs_id);
3382 nfhp->nfh_xh.nxh_expid = htonl(nx->nx_id);
3383 nfhp->nfh_xh.nxh_flags = 0;
3384 nfhp->nfh_xh.nxh_reserved = 0;
3385
3386 if (nfsvers == NFS_VER2)
3387 bzero(&nfhp->nfh_fid[0], NFSV2_MAX_FID_SIZE);
3388
3389 /* if directory FH matches export root, return invalid FH */
3390 if (dnfhp && nfsrv_fhmatch(dnfhp, &nx->nx_fh)) {
3391 if (nfsvers == NFS_VER2)
3392 nfhp->nfh_len = NFSX_V2FH;
3393 else
3394 nfhp->nfh_len = sizeof(nfhp->nfh_xh);
3395 nfhp->nfh_xh.nxh_fidlen = 0;
3396 nfhp->nfh_xh.nxh_flags = htons(NXHF_INVALIDFH);
3397 return (0);
3398 }
3399
3400 if (nfsvers == NFS_VER2)
3401 maxfidsize = NFSV2_MAX_FID_SIZE;
3402 else
3403 maxfidsize = NFSV3_MAX_FID_SIZE;
3404 nfhp->nfh_len = maxfidsize;
3405
3406 error = VFS_VPTOFH(vp, (int*)&nfhp->nfh_len, &nfhp->nfh_fid[0], ctx);
3407 if (error)
3408 return (error);
3409 if (nfhp->nfh_len > maxfidsize)
3410 return (EOVERFLOW);
3411 nfhp->nfh_xh.nxh_fidlen = nfhp->nfh_len;
3412 nfhp->nfh_len += sizeof(nfhp->nfh_xh);
3413 if ((nfsvers == NFS_VER2) && (nfhp->nfh_len < NFSX_V2FH))
3414 nfhp->nfh_len = NFSX_V2FH;
3415
3416 return (0);
3417 }
3418
3419 /*
3420 * Compare two file handles to see it they're the same.
3421 * Note that we don't use nfh_len because that may include
3422 * padding in an NFSv2 file handle.
3423 */
3424 int
3425 nfsrv_fhmatch(struct nfs_filehandle *fh1, struct nfs_filehandle *fh2)
3426 {
3427 struct nfs_exphandle *nxh1, *nxh2;
3428 int len1, len2;
3429
3430 nxh1 = (struct nfs_exphandle *)fh1->nfh_fhp;
3431 nxh2 = (struct nfs_exphandle *)fh2->nfh_fhp;
3432 len1 = sizeof(fh1->nfh_xh) + nxh1->nxh_fidlen;
3433 len2 = sizeof(fh2->nfh_xh) + nxh2->nxh_fidlen;
3434 if (len1 != len2)
3435 return (0);
3436 if (bcmp(nxh1, nxh2, len1))
3437 return (0);
3438 return (1);
3439 }
3440
3441 /*
3442 * Functions for dealing with active user lists
3443 */
3444
3445 /*
3446 * Search the hash table for a user node with a matching IP address and uid field.
3447 * If found, the node's tm_last timestamp is updated and the node is returned.
3448 *
3449 * If not found, a new node is allocated (or reclaimed via LRU), initialized, and returned.
3450 * Returns NULL if a new node could not be allcoated.
3451 *
3452 * The list's user_mutex lock MUST be held.
3453 */
3454 struct nfs_user_stat_node *
3455 nfsrv_get_user_stat_node(struct nfs_active_user_list *list, struct sockaddr *saddr, uid_t uid)
3456 {
3457 struct nfs_user_stat_node *unode;
3458 struct timeval now;
3459 struct nfs_user_stat_hashtbl_head *head;
3460
3461 /* seach the hash table */
3462 head = NFS_USER_STAT_HASH(list->user_hashtbl, uid);
3463 LIST_FOREACH(unode, head, hash_link) {
3464 if ((uid == unode->uid) && (nfs_sockaddr_cmp(saddr, (struct sockaddr*)&unode->sock) == 0)) {
3465 /* found matching node */
3466 break;
3467 }
3468 }
3469
3470 if (unode) {
3471 /* found node in the hash table, now update lru position */
3472 TAILQ_REMOVE(&list->user_lru, unode, lru_link);
3473 TAILQ_INSERT_TAIL(&list->user_lru, unode, lru_link);
3474
3475 /* update time stamp */
3476 microtime(&now);
3477 unode->tm_last = (uint32_t)now.tv_sec;
3478 return unode;
3479 }
3480
3481 if (list->node_count < nfsrv_user_stat_max_nodes) {
3482 /* Allocate a new node */
3483 MALLOC(unode, struct nfs_user_stat_node *, sizeof(struct nfs_user_stat_node),
3484 M_TEMP, M_WAITOK | M_ZERO);
3485
3486 if (!unode)
3487 return NULL;
3488
3489 /* increment node count */
3490 OSAddAtomic(1, &nfsrv_user_stat_node_count);
3491 list->node_count++;
3492 } else {
3493 /* reuse the oldest node in the lru list */
3494 unode = TAILQ_FIRST(&list->user_lru);
3495
3496 if (!unode)
3497 return NULL;
3498
3499 /* Remove the node */
3500 TAILQ_REMOVE(&list->user_lru, unode, lru_link);
3501 LIST_REMOVE(unode, hash_link);
3502 }
3503
3504 /* Initialize the node */
3505 unode->uid = uid;
3506 bcopy(saddr, &unode->sock, saddr->sa_len);
3507 microtime(&now);
3508 unode->ops = 0;
3509 unode->bytes_read = 0;
3510 unode->bytes_written = 0;
3511 unode->tm_start = (uint32_t)now.tv_sec;
3512 unode->tm_last = (uint32_t)now.tv_sec;
3513
3514 /* insert the node */
3515 TAILQ_INSERT_TAIL(&list->user_lru, unode, lru_link);
3516 LIST_INSERT_HEAD(head, unode, hash_link);
3517
3518 return unode;
3519 }
3520
3521 void
3522 nfsrv_update_user_stat(struct nfs_export *nx, struct nfsrv_descript *nd, uid_t uid, u_int ops, u_int rd_bytes, u_int wr_bytes)
3523 {
3524 struct nfs_user_stat_node *unode;
3525 struct nfs_active_user_list *ulist;
3526 struct sockaddr *saddr;
3527
3528 if ((!nfsrv_user_stat_enabled) || (!nx) || (!nd) || (!nd->nd_nam))
3529 return;
3530
3531 saddr = (struct sockaddr *)mbuf_data(nd->nd_nam);
3532
3533 /* check address family before going any further */
3534 if ((saddr->sa_family != AF_INET) && (saddr->sa_family != AF_INET6))
3535 return;
3536
3537 ulist = &nx->nx_user_list;
3538
3539 /* lock the active user list */
3540 lck_mtx_lock(&ulist->user_mutex);
3541
3542 /* get the user node */
3543 unode = nfsrv_get_user_stat_node(ulist, saddr, uid);
3544
3545 if (!unode) {
3546 lck_mtx_unlock(&ulist->user_mutex);
3547 return;
3548 }
3549
3550 /* update counters */
3551 unode->ops += ops;
3552 unode->bytes_read += rd_bytes;
3553 unode->bytes_written += wr_bytes;
3554
3555 /* done */
3556 lck_mtx_unlock(&ulist->user_mutex);
3557 }
3558
3559 /* initialize an active user list */
3560 void
3561 nfsrv_init_user_list(struct nfs_active_user_list *ulist)
3562 {
3563 uint i;
3564
3565 /* initialize the lru */
3566 TAILQ_INIT(&ulist->user_lru);
3567
3568 /* initialize the hash table */
3569 for(i = 0; i < NFS_USER_STAT_HASH_SIZE; i++)
3570 LIST_INIT(&ulist->user_hashtbl[i]);
3571 ulist->node_count = 0;
3572
3573 lck_mtx_init(&ulist->user_mutex, nfsrv_active_user_mutex_group, LCK_ATTR_NULL);
3574 }
3575
3576 /* Free all nodes in an active user list */
3577 void
3578 nfsrv_free_user_list(struct nfs_active_user_list *ulist)
3579 {
3580 struct nfs_user_stat_node *unode;
3581
3582 if (!ulist)
3583 return;
3584
3585 while ((unode = TAILQ_FIRST(&ulist->user_lru))) {
3586 /* Remove node and free */
3587 TAILQ_REMOVE(&ulist->user_lru, unode, lru_link);
3588 LIST_REMOVE(unode, hash_link);
3589 FREE(unode, M_TEMP);
3590
3591 /* decrement node count */
3592 OSAddAtomic(-1, &nfsrv_user_stat_node_count);
3593 }
3594 ulist->node_count = 0;
3595
3596 lck_mtx_destroy(&ulist->user_mutex, nfsrv_active_user_mutex_group);
3597 }
3598
3599 /* Reclaim old expired user nodes from active user lists. */
3600 void
3601 nfsrv_active_user_list_reclaim(void)
3602 {
3603 struct nfs_exportfs *nxfs;
3604 struct nfs_export *nx;
3605 struct nfs_active_user_list *ulist;
3606 struct nfs_user_stat_hashtbl_head oldlist;
3607 struct nfs_user_stat_node *unode, *unode_next;
3608 struct timeval now;
3609 uint32_t tstale;
3610
3611 LIST_INIT(&oldlist);
3612
3613 lck_rw_lock_shared(&nfsrv_export_rwlock);
3614 microtime(&now);
3615 tstale = now.tv_sec - nfsrv_user_stat_max_idle_sec;
3616 LIST_FOREACH(nxfs, &nfsrv_exports, nxfs_next) {
3617 LIST_FOREACH(nx, &nxfs->nxfs_exports, nx_next) {
3618 /* Scan through all user nodes of this export */
3619 ulist = &nx->nx_user_list;
3620 lck_mtx_lock(&ulist->user_mutex);
3621 for (unode = TAILQ_FIRST(&ulist->user_lru); unode; unode = unode_next) {
3622 unode_next = TAILQ_NEXT(unode, lru_link);
3623
3624 /* check if this node has expired */
3625 if (unode->tm_last >= tstale)
3626 break;
3627
3628 /* Remove node from the active user list */
3629 TAILQ_REMOVE(&ulist->user_lru, unode, lru_link);
3630 LIST_REMOVE(unode, hash_link);
3631
3632 /* Add node to temp list */
3633 LIST_INSERT_HEAD(&oldlist, unode, hash_link);
3634
3635 /* decrement node count */
3636 OSAddAtomic(-1, &nfsrv_user_stat_node_count);
3637 ulist->node_count--;
3638 }
3639 /* can unlock this export's list now */
3640 lck_mtx_unlock(&ulist->user_mutex);
3641 }
3642 }
3643 lck_rw_done(&nfsrv_export_rwlock);
3644
3645 /* Free expired nodes */
3646 while ((unode = LIST_FIRST(&oldlist))) {
3647 LIST_REMOVE(unode, hash_link);
3648 FREE(unode, M_TEMP);
3649 }
3650 }
3651
3652 /*
3653 * Maps errno values to nfs error numbers.
3654 * Use NFSERR_IO as the catch all for ones not specifically defined in
3655 * RFC 1094.
3656 */
3657 static u_char nfsrv_v2errmap[] = {
3658 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
3659 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
3660 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
3661 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
3662 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
3663 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
3664 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
3665 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
3666 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
3667 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
3668 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
3669 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
3670 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
3671 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
3672 };
3673
3674 /*
3675 * Maps errno values to nfs error numbers.
3676 * Although it is not obvious whether or not NFS clients really care if
3677 * a returned error value is in the specified list for the procedure, the
3678 * safest thing to do is filter them appropriately. For Version 2, the
3679 * X/Open XNFS document is the only specification that defines error values
3680 * for each RPC (The RFC simply lists all possible error values for all RPCs),
3681 * so I have decided to not do this for Version 2.
3682 * The first entry is the default error return and the rest are the valid
3683 * errors for that RPC in increasing numeric order.
3684 */
3685 static short nfsv3err_null[] = {
3686 0,
3687 0,
3688 };
3689
3690 static short nfsv3err_getattr[] = {
3691 NFSERR_IO,
3692 NFSERR_IO,
3693 NFSERR_STALE,
3694 NFSERR_BADHANDLE,
3695 NFSERR_SERVERFAULT,
3696 NFSERR_TRYLATER,
3697 0,
3698 };
3699
3700 static short nfsv3err_setattr[] = {
3701 NFSERR_IO,
3702 NFSERR_PERM,
3703 NFSERR_IO,
3704 NFSERR_ACCES,
3705 NFSERR_INVAL,
3706 NFSERR_NOSPC,
3707 NFSERR_ROFS,
3708 NFSERR_DQUOT,
3709 NFSERR_STALE,
3710 NFSERR_BADHANDLE,
3711 NFSERR_NOT_SYNC,
3712 NFSERR_SERVERFAULT,
3713 NFSERR_TRYLATER,
3714 0,
3715 };
3716
3717 static short nfsv3err_lookup[] = {
3718 NFSERR_IO,
3719 NFSERR_NOENT,
3720 NFSERR_IO,
3721 NFSERR_ACCES,
3722 NFSERR_NOTDIR,
3723 NFSERR_NAMETOL,
3724 NFSERR_STALE,
3725 NFSERR_BADHANDLE,
3726 NFSERR_SERVERFAULT,
3727 NFSERR_TRYLATER,
3728 0,
3729 };
3730
3731 static short nfsv3err_access[] = {
3732 NFSERR_IO,
3733 NFSERR_IO,
3734 NFSERR_STALE,
3735 NFSERR_BADHANDLE,
3736 NFSERR_SERVERFAULT,
3737 NFSERR_TRYLATER,
3738 0,
3739 };
3740
3741 static short nfsv3err_readlink[] = {
3742 NFSERR_IO,
3743 NFSERR_IO,
3744 NFSERR_ACCES,
3745 NFSERR_INVAL,
3746 NFSERR_STALE,
3747 NFSERR_BADHANDLE,
3748 NFSERR_NOTSUPP,
3749 NFSERR_SERVERFAULT,
3750 NFSERR_TRYLATER,
3751 0,
3752 };
3753
3754 static short nfsv3err_read[] = {
3755 NFSERR_IO,
3756 NFSERR_IO,
3757 NFSERR_NXIO,
3758 NFSERR_ACCES,
3759 NFSERR_INVAL,
3760 NFSERR_STALE,
3761 NFSERR_BADHANDLE,
3762 NFSERR_SERVERFAULT,
3763 NFSERR_TRYLATER,
3764 0,
3765 };
3766
3767 static short nfsv3err_write[] = {
3768 NFSERR_IO,
3769 NFSERR_IO,
3770 NFSERR_ACCES,
3771 NFSERR_INVAL,
3772 NFSERR_FBIG,
3773 NFSERR_NOSPC,
3774 NFSERR_ROFS,
3775 NFSERR_DQUOT,
3776 NFSERR_STALE,
3777 NFSERR_BADHANDLE,
3778 NFSERR_SERVERFAULT,
3779 NFSERR_TRYLATER,
3780 0,
3781 };
3782
3783 static short nfsv3err_create[] = {
3784 NFSERR_IO,
3785 NFSERR_IO,
3786 NFSERR_ACCES,
3787 NFSERR_EXIST,
3788 NFSERR_NOTDIR,
3789 NFSERR_NOSPC,
3790 NFSERR_ROFS,
3791 NFSERR_NAMETOL,
3792 NFSERR_DQUOT,
3793 NFSERR_STALE,
3794 NFSERR_BADHANDLE,
3795 NFSERR_NOTSUPP,
3796 NFSERR_SERVERFAULT,
3797 NFSERR_TRYLATER,
3798 0,
3799 };
3800
3801 static short nfsv3err_mkdir[] = {
3802 NFSERR_IO,
3803 NFSERR_IO,
3804 NFSERR_ACCES,
3805 NFSERR_EXIST,
3806 NFSERR_NOTDIR,
3807 NFSERR_NOSPC,
3808 NFSERR_ROFS,
3809 NFSERR_NAMETOL,
3810 NFSERR_DQUOT,
3811 NFSERR_STALE,
3812 NFSERR_BADHANDLE,
3813 NFSERR_NOTSUPP,
3814 NFSERR_SERVERFAULT,
3815 NFSERR_TRYLATER,
3816 0,
3817 };
3818
3819 static short nfsv3err_symlink[] = {
3820 NFSERR_IO,
3821 NFSERR_IO,
3822 NFSERR_ACCES,
3823 NFSERR_EXIST,
3824 NFSERR_NOTDIR,
3825 NFSERR_NOSPC,
3826 NFSERR_ROFS,
3827 NFSERR_NAMETOL,
3828 NFSERR_DQUOT,
3829 NFSERR_STALE,
3830 NFSERR_BADHANDLE,
3831 NFSERR_NOTSUPP,
3832 NFSERR_SERVERFAULT,
3833 NFSERR_TRYLATER,
3834 0,
3835 };
3836
3837 static short nfsv3err_mknod[] = {
3838 NFSERR_IO,
3839 NFSERR_IO,
3840 NFSERR_ACCES,
3841 NFSERR_EXIST,
3842 NFSERR_NOTDIR,
3843 NFSERR_NOSPC,
3844 NFSERR_ROFS,
3845 NFSERR_NAMETOL,
3846 NFSERR_DQUOT,
3847 NFSERR_STALE,
3848 NFSERR_BADHANDLE,
3849 NFSERR_NOTSUPP,
3850 NFSERR_SERVERFAULT,
3851 NFSERR_BADTYPE,
3852 NFSERR_TRYLATER,
3853 0,
3854 };
3855
3856 static short nfsv3err_remove[] = {
3857 NFSERR_IO,
3858 NFSERR_NOENT,
3859 NFSERR_IO,
3860 NFSERR_ACCES,
3861 NFSERR_NOTDIR,
3862 NFSERR_ROFS,
3863 NFSERR_NAMETOL,
3864 NFSERR_STALE,
3865 NFSERR_BADHANDLE,
3866 NFSERR_SERVERFAULT,
3867 NFSERR_TRYLATER,
3868 0,
3869 };
3870
3871 static short nfsv3err_rmdir[] = {
3872 NFSERR_IO,
3873 NFSERR_NOENT,
3874 NFSERR_IO,
3875 NFSERR_ACCES,
3876 NFSERR_EXIST,
3877 NFSERR_NOTDIR,
3878 NFSERR_INVAL,
3879 NFSERR_ROFS,
3880 NFSERR_NAMETOL,
3881 NFSERR_NOTEMPTY,
3882 NFSERR_STALE,
3883 NFSERR_BADHANDLE,
3884 NFSERR_NOTSUPP,
3885 NFSERR_SERVERFAULT,
3886 NFSERR_TRYLATER,
3887 0,
3888 };
3889
3890 static short nfsv3err_rename[] = {
3891 NFSERR_IO,
3892 NFSERR_NOENT,
3893 NFSERR_IO,
3894 NFSERR_ACCES,
3895 NFSERR_EXIST,
3896 NFSERR_XDEV,
3897 NFSERR_NOTDIR,
3898 NFSERR_ISDIR,
3899 NFSERR_INVAL,
3900 NFSERR_NOSPC,
3901 NFSERR_ROFS,
3902 NFSERR_MLINK,
3903 NFSERR_NAMETOL,
3904 NFSERR_NOTEMPTY,
3905 NFSERR_DQUOT,
3906 NFSERR_STALE,
3907 NFSERR_BADHANDLE,
3908 NFSERR_NOTSUPP,
3909 NFSERR_SERVERFAULT,
3910 NFSERR_TRYLATER,
3911 0,
3912 };
3913
3914 static short nfsv3err_link[] = {
3915 NFSERR_IO,
3916 NFSERR_IO,
3917 NFSERR_ACCES,
3918 NFSERR_EXIST,
3919 NFSERR_XDEV,
3920 NFSERR_NOTDIR,
3921 NFSERR_INVAL,
3922 NFSERR_NOSPC,
3923 NFSERR_ROFS,
3924 NFSERR_MLINK,
3925 NFSERR_NAMETOL,
3926 NFSERR_DQUOT,
3927 NFSERR_STALE,
3928 NFSERR_BADHANDLE,
3929 NFSERR_NOTSUPP,
3930 NFSERR_SERVERFAULT,
3931 NFSERR_TRYLATER,
3932 0,
3933 };
3934
3935 static short nfsv3err_readdir[] = {
3936 NFSERR_IO,
3937 NFSERR_IO,
3938 NFSERR_ACCES,
3939 NFSERR_NOTDIR,
3940 NFSERR_STALE,
3941 NFSERR_BADHANDLE,
3942 NFSERR_BAD_COOKIE,
3943 NFSERR_TOOSMALL,
3944 NFSERR_SERVERFAULT,
3945 NFSERR_TRYLATER,
3946 0,
3947 };
3948
3949 static short nfsv3err_readdirplus[] = {
3950 NFSERR_IO,
3951 NFSERR_IO,
3952 NFSERR_ACCES,
3953 NFSERR_NOTDIR,
3954 NFSERR_STALE,
3955 NFSERR_BADHANDLE,
3956 NFSERR_BAD_COOKIE,
3957 NFSERR_NOTSUPP,
3958 NFSERR_TOOSMALL,
3959 NFSERR_SERVERFAULT,
3960 NFSERR_TRYLATER,
3961 0,
3962 };
3963
3964 static short nfsv3err_fsstat[] = {
3965 NFSERR_IO,
3966 NFSERR_IO,
3967 NFSERR_STALE,
3968 NFSERR_BADHANDLE,
3969 NFSERR_SERVERFAULT,
3970 NFSERR_TRYLATER,
3971 0,
3972 };
3973
3974 static short nfsv3err_fsinfo[] = {
3975 NFSERR_STALE,
3976 NFSERR_STALE,
3977 NFSERR_BADHANDLE,
3978 NFSERR_SERVERFAULT,
3979 NFSERR_TRYLATER,
3980 0,
3981 };
3982
3983 static short nfsv3err_pathconf[] = {
3984 NFSERR_STALE,
3985 NFSERR_STALE,
3986 NFSERR_BADHANDLE,
3987 NFSERR_SERVERFAULT,
3988 NFSERR_TRYLATER,
3989 0,
3990 };
3991
3992 static short nfsv3err_commit[] = {
3993 NFSERR_IO,
3994 NFSERR_IO,
3995 NFSERR_STALE,
3996 NFSERR_BADHANDLE,
3997 NFSERR_SERVERFAULT,
3998 NFSERR_TRYLATER,
3999 0,
4000 };
4001
4002 static short *nfsrv_v3errmap[] = {
4003 nfsv3err_null,
4004 nfsv3err_getattr,
4005 nfsv3err_setattr,
4006 nfsv3err_lookup,
4007 nfsv3err_access,
4008 nfsv3err_readlink,
4009 nfsv3err_read,
4010 nfsv3err_write,
4011 nfsv3err_create,
4012 nfsv3err_mkdir,
4013 nfsv3err_symlink,
4014 nfsv3err_mknod,
4015 nfsv3err_remove,
4016 nfsv3err_rmdir,
4017 nfsv3err_rename,
4018 nfsv3err_link,
4019 nfsv3err_readdir,
4020 nfsv3err_readdirplus,
4021 nfsv3err_fsstat,
4022 nfsv3err_fsinfo,
4023 nfsv3err_pathconf,
4024 nfsv3err_commit,
4025 };
4026
4027 /*
4028 * Map errnos to NFS error numbers. For Version 3 also filter out error
4029 * numbers not specified for the associated procedure.
4030 */
4031 int
4032 nfsrv_errmap(struct nfsrv_descript *nd, int err)
4033 {
4034 short *defaulterrp, *errp;
4035
4036 if (nd->nd_vers == NFS_VER2) {
4037 if (err <= (int)sizeof(nfsrv_v2errmap))
4038 return ((int)nfsrv_v2errmap[err - 1]);
4039 return (NFSERR_IO);
4040 }
4041 /* NFSv3 */
4042 if (nd->nd_procnum > NFSPROC_COMMIT)
4043 return (err & 0xffff);
4044 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
4045 while (*++errp) {
4046 if (*errp == err)
4047 return (err);
4048 else if (*errp > err)
4049 break;
4050 }
4051 return ((int)*defaulterrp);
4052 }
4053
4054 #endif /* NFSSERVER */
4055