1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2015 by Delphix. All rights reserved.
25 * Copyright 2018 Nexenta Systems, Inc. All rights reserved.
26 */
27
28 #include <sys/param.h>
29 #include <sys/errno.h>
30 #include <sys/vfs.h>
31 #include <sys/vnode.h>
32 #include <sys/cred.h>
33 #include <sys/cmn_err.h>
34 #include <sys/systm.h>
35 #include <sys/kmem.h>
36 #include <sys/pathname.h>
37 #include <sys/utsname.h>
38 #include <sys/debug.h>
39 #include <sys/door.h>
40 #include <sys/sdt.h>
41 #include <sys/thread.h>
42 #include <sys/avl.h>
43
44 #include <rpc/types.h>
45 #include <rpc/auth.h>
46 #include <rpc/clnt.h>
47
48 #include <nfs/nfs.h>
49 #include <nfs/export.h>
50 #include <nfs/nfs_clnt.h>
51 #include <nfs/auth.h>
52
53 static struct kmem_cache *exi_cache_handle;
54 static void exi_cache_reclaim(void *);
55 static void exi_cache_trim(struct exportinfo *exi);
56 static void *nfsauth_zone_init(zoneid_t);
57 static void nfsauth_zone_shutdown(zoneid_t zoneid, void *data);
58 static void nfsauth_zone_fini(zoneid_t, void *);
59
60 extern pri_t minclsyspri;
61
62 /* NFS auth cache statistics */
63 volatile uint_t nfsauth_cache_hit;
64 volatile uint_t nfsauth_cache_miss;
65 volatile uint_t nfsauth_cache_refresh;
66 volatile uint_t nfsauth_cache_reclaim;
67 volatile uint_t exi_cache_auth_reclaim_failed;
68 volatile uint_t exi_cache_clnt_reclaim_failed;
69
70 /*
71 * The lifetime of an auth cache entry:
72 * ------------------------------------
73 *
74 * An auth cache entry is created with both the auth_time
75 * and auth_freshness times set to the current time.
76 *
77 * Upon every client access which results in a hit, the
78 * auth_time will be updated.
79 *
80 * If a client access determines that the auth_freshness
81 * indicates that the entry is STALE, then it will be
82 * refreshed. Note that this will explicitly reset
83 * auth_time.
84 *
85 * When the REFRESH successfully occurs, then the
86 * auth_freshness is updated.
87 *
88 * There are two ways for an entry to leave the cache:
89 *
90 * 1) Purged by an action on the export (remove or changed)
91 * 2) Memory backpressure from the kernel (check against NFSAUTH_CACHE_TRIM)
92 *
93 * For 2) we check the timeout value against auth_time.
94 */
95
96 /*
97 * Number of seconds until we mark for refresh an auth cache entry.
98 */
99 #define NFSAUTH_CACHE_REFRESH 600
100
101 /*
102 * Number of idle seconds until we yield to backpressure
103 * to trim a cache entry.
104 */
105 #define NFSAUTH_CACHE_TRIM 3600
106
107 /*
108 * While we could encapuslate the exi_list inside the
109 * exi structure, we can't do that for the auth_list.
110 * So, to keep things looking clean, we keep them both
111 * in these external lists.
112 */
113 typedef struct refreshq_exi_node {
114 struct exportinfo *ren_exi;
115 list_t ren_authlist;
116 list_node_t ren_node;
117 } refreshq_exi_node_t;
118
119 typedef struct refreshq_auth_node {
120 struct auth_cache *ran_auth;
121 char *ran_netid;
122 list_node_t ran_node;
123 } refreshq_auth_node_t;
124
125 /*
126 * Used to manipulate things on the refreshq_queue. Note that the refresh
127 * thread will effectively pop a node off of the queue, at which point it
128 * will no longer need to hold the mutex.
129 */
130 static kmutex_t refreshq_lock;
131 static list_t refreshq_queue;
132 static kcondvar_t refreshq_cv;
133
134 /*
135 * If there is ever a problem with loading the module, then nfsauth_fini()
136 * needs to be called to remove state. In that event, since the refreshq
137 * thread has been started, they need to work together to get rid of state.
138 */
139 typedef enum nfsauth_refreshq_thread_state {
140 REFRESHQ_THREAD_RUNNING,
141 REFRESHQ_THREAD_FINI_REQ,
142 REFRESHQ_THREAD_HALTED,
143 REFRESHQ_THREAD_NEED_CREATE
144 } nfsauth_refreshq_thread_state_t;
145
146 typedef struct nfsauth_globals {
147 kmutex_t mountd_lock;
148 door_handle_t mountd_dh;
149
150 /*
151 * Used to manipulate things on the refreshq_queue. Note that the
152 * refresh thread will effectively pop a node off of the queue,
153 * at which point it will no longer need to hold the mutex.
154 */
155 kmutex_t refreshq_lock;
156 list_t refreshq_queue;
157 kcondvar_t refreshq_cv;
158
159 /*
160 * A list_t would be overkill. These are auth_cache entries which are
161 * no longer linked to an exi. It should be the case that all of their
162 * states are NFS_AUTH_INVALID, i.e., the only way to be put on this
163 * list is iff their state indicated that they had been placed on the
164 * refreshq_queue.
165 *
166 * Note that while there is no link from the exi or back to the exi,
167 * the exi can not go away until these entries are harvested.
168 */
169 struct auth_cache *refreshq_dead_entries;
170 nfsauth_refreshq_thread_state_t refreshq_thread_state;
171
172 } nfsauth_globals_t;
173
174 static void nfsauth_free_node(struct auth_cache *);
175 static void nfsauth_refresh_thread(nfsauth_globals_t *);
176
177 static int nfsauth_cache_compar(const void *, const void *);
178
179 static zone_key_t nfsauth_zone_key;
180
181 void
182 mountd_args(uint_t did)
183 {
184 nfsauth_globals_t *nag;
185
186 nag = zone_getspecific(nfsauth_zone_key, curzone);
187 mutex_enter(&nag->mountd_lock);
188 if (nag->mountd_dh != NULL)
189 door_ki_rele(nag->mountd_dh);
190 nag->mountd_dh = door_ki_lookup(did);
191 mutex_exit(&nag->mountd_lock);
192 }
193
194 void
195 nfsauth_init(void)
196 {
197 zone_key_create(&nfsauth_zone_key, nfsauth_zone_init,
198 nfsauth_zone_shutdown, nfsauth_zone_fini);
199
200 exi_cache_handle = kmem_cache_create("exi_cache_handle",
201 sizeof (struct auth_cache), 0, NULL, NULL,
202 exi_cache_reclaim, NULL, NULL, 0);
203 }
204
205 void
206 nfsauth_fini(void)
207 {
208 kmem_cache_destroy(exi_cache_handle);
209 }
210
211 /*ARGSUSED*/
212 static void *
213 nfsauth_zone_init(zoneid_t zoneid)
214 {
215 nfsauth_globals_t *nag;
216
217 nag = kmem_zalloc(sizeof (*nag), KM_SLEEP);
218
219 /*
220 * mountd can be restarted by smf(5). We need to make sure
221 * the updated door handle will safely make it to mountd_dh.
222 */
223 mutex_init(&nag->mountd_lock, NULL, MUTEX_DEFAULT, NULL);
224 mutex_init(&nag->refreshq_lock, NULL, MUTEX_DEFAULT, NULL);
225 list_create(&nag->refreshq_queue, sizeof (refreshq_exi_node_t),
226 offsetof(refreshq_exi_node_t, ren_node));
227 cv_init(&nag->refreshq_cv, NULL, CV_DEFAULT, NULL);
228 nag->refreshq_thread_state = REFRESHQ_THREAD_NEED_CREATE;
229
230 return (nag);
231 }
232
233 /*ARGSUSED*/
234 static void
235 nfsauth_zone_shutdown(zoneid_t zoneid, void *data)
236 {
237 refreshq_exi_node_t *ren;
238 nfsauth_globals_t *nag = data;
239
240 /* Prevent the nfsauth_refresh_thread from getting new work */
241 mutex_enter(&nag->refreshq_lock);
242 if (nag->refreshq_thread_state == REFRESHQ_THREAD_RUNNING) {
243 nag->refreshq_thread_state = REFRESHQ_THREAD_FINI_REQ;
244 cv_broadcast(&nag->refreshq_cv);
245
246 /* Wait for nfsauth_refresh_thread() to exit */
247 while (nag->refreshq_thread_state != REFRESHQ_THREAD_HALTED)
248 cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);
249 }
250 mutex_exit(&nag->refreshq_lock);
251
252 /*
253 * Walk the exi_list and in turn, walk the auth_lists and free all
254 * lists. In addition, free INVALID auth_cache entries.
255 */
256 while ((ren = list_remove_head(&nag->refreshq_queue))) {
257 refreshq_auth_node_t *ran;
258
259 while ((ran = list_remove_head(&ren->ren_authlist)) != NULL) {
260 struct auth_cache *p = ran->ran_auth;
261 if (p->auth_state == NFS_AUTH_INVALID)
262 nfsauth_free_node(p);
263 strfree(ran->ran_netid);
264 kmem_free(ran, sizeof (*ran));
265 }
266
267 list_destroy(&ren->ren_authlist);
268 exi_rele(ren->ren_exi);
269 kmem_free(ren, sizeof (*ren));
270 }
271 }
272
273 /*ARGSUSED*/
274 static void
275 nfsauth_zone_fini(zoneid_t zoneid, void *data)
276 {
277 nfsauth_globals_t *nag = data;
278
279 list_destroy(&nag->refreshq_queue);
280 cv_destroy(&nag->refreshq_cv);
281 mutex_destroy(&nag->refreshq_lock);
282 mutex_destroy(&nag->mountd_lock);
283 kmem_free(nag, sizeof (*nag));
284 }
285
286 /*
287 * Convert the address in a netbuf to
288 * a hash index for the auth_cache table.
289 */
290 static int
291 hash(struct netbuf *a)
292 {
293 int i, h = 0;
294
295 for (i = 0; i < a->len; i++)
296 h ^= a->buf[i];
297
298 return (h & (AUTH_TABLESIZE - 1));
299 }
300
301 /*
302 * Mask out the components of an
303 * address that do not identify
304 * a host. For socket addresses the
305 * masking gets rid of the port number.
306 */
307 static void
308 addrmask(struct netbuf *addr, struct netbuf *mask)
309 {
310 int i;
311
312 for (i = 0; i < addr->len; i++)
313 addr->buf[i] &= mask->buf[i];
314 }
315
316 /*
317 * nfsauth4_access is used for NFS V4 auth checking. Besides doing
318 * the common nfsauth_access(), it will check if the client can
319 * have a limited access to this vnode even if the security flavor
320 * used does not meet the policy.
321 */
322 int
323 nfsauth4_access(struct exportinfo *exi, vnode_t *vp, struct svc_req *req,
324 cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
325 {
326 int access;
327
328 access = nfsauth_access(exi, req, cr, uid, gid, ngids, gids);
329
330 /*
331 * There are cases that the server needs to allow the client
332 * to have a limited view.
333 *
334 * e.g.
335 * /export is shared as "sec=sys,rw=dfs-test-4,sec=krb5,rw"
336 * /export/home is shared as "sec=sys,rw"
337 *
338 * When the client mounts /export with sec=sys, the client
339 * would get a limited view with RO access on /export to see
340 * "home" only because the client is allowed to access
341 * /export/home with auth_sys.
342 */
343 if (access & NFSAUTH_DENIED || access & NFSAUTH_WRONGSEC) {
344 /*
345 * Allow ro permission with LIMITED view if there is a
346 * sub-dir exported under vp.
347 */
348 if (has_visible(exi, vp))
349 return (NFSAUTH_LIMITED);
350 }
351
352 return (access);
353 }
354
355 static void
356 sys_log(const char *msg)
357 {
358 static time_t tstamp = 0;
359 time_t now;
360
361 /*
362 * msg is shown (at most) once per minute
363 */
364 now = gethrestime_sec();
365 if ((tstamp + 60) < now) {
366 tstamp = now;
367 cmn_err(CE_WARN, msg);
368 }
369 }
370
371 /*
372 * Callup to the mountd to get access information in the kernel.
373 */
374 static bool_t
375 nfsauth_retrieve(nfsauth_globals_t *nag, struct exportinfo *exi,
376 char *req_netid, int flavor, struct netbuf *addr, int *access,
377 cred_t *clnt_cred, uid_t *srv_uid, gid_t *srv_gid, uint_t *srv_gids_cnt,
378 gid_t **srv_gids)
379 {
380 varg_t varg = {0};
381 nfsauth_res_t res = {0};
382 XDR xdrs;
383 size_t absz;
384 caddr_t abuf;
385 int last = 0;
386 door_arg_t da;
387 door_info_t di;
388 door_handle_t dh;
389 uint_t ntries = 0;
390
391 /*
392 * No entry in the cache for this client/flavor
393 * so we need to call the nfsauth service in the
394 * mount daemon.
395 */
396
397 varg.vers = V_PROTO;
398 varg.arg_u.arg.cmd = NFSAUTH_ACCESS;
399 varg.arg_u.arg.areq.req_client.n_len = addr->len;
400 varg.arg_u.arg.areq.req_client.n_bytes = addr->buf;
401 varg.arg_u.arg.areq.req_netid = req_netid;
402 varg.arg_u.arg.areq.req_path = exi->exi_export.ex_path;
403 varg.arg_u.arg.areq.req_flavor = flavor;
404 varg.arg_u.arg.areq.req_clnt_uid = crgetuid(clnt_cred);
405 varg.arg_u.arg.areq.req_clnt_gid = crgetgid(clnt_cred);
406 varg.arg_u.arg.areq.req_clnt_gids.len = crgetngroups(clnt_cred);
407 varg.arg_u.arg.areq.req_clnt_gids.val = (gid_t *)crgetgroups(clnt_cred);
408
409 DTRACE_PROBE1(nfsserv__func__nfsauth__varg, varg_t *, &varg);
410
411 /*
412 * Setup the XDR stream for encoding the arguments. Notice that
413 * in addition to the args having variable fields (req_netid and
414 * req_path), the argument data structure is itself versioned,
415 * so we need to make sure we can size the arguments buffer
416 * appropriately to encode all the args. If we can't get sizing
417 * info _or_ properly encode the arguments, there's really no
418 * point in continuting, so we fail the request.
419 */
420 if ((absz = xdr_sizeof(xdr_varg, &varg)) == 0) {
421 *access = NFSAUTH_DENIED;
422 return (FALSE);
423 }
424
425 abuf = (caddr_t)kmem_alloc(absz, KM_SLEEP);
426 xdrmem_create(&xdrs, abuf, absz, XDR_ENCODE);
427 if (!xdr_varg(&xdrs, &varg)) {
428 XDR_DESTROY(&xdrs);
429 goto fail;
430 }
431 XDR_DESTROY(&xdrs);
432
433 /*
434 * Prepare the door arguments
435 *
436 * We don't know the size of the message the daemon
437 * will pass back to us. By setting rbuf to NULL,
438 * we force the door code to allocate a buf of the
439 * appropriate size. We must set rsize > 0, however,
440 * else the door code acts as if no response was
441 * expected and doesn't pass the data to us.
442 */
443 da.data_ptr = (char *)abuf;
444 da.data_size = absz;
445 da.desc_ptr = NULL;
446 da.desc_num = 0;
447 da.rbuf = NULL;
448 da.rsize = 1;
449
450 retry:
451 mutex_enter(&nag->mountd_lock);
452 dh = nag->mountd_dh;
453 if (dh != NULL)
454 door_ki_hold(dh);
455 mutex_exit(&nag->mountd_lock);
456
457 if (dh == NULL) {
458 /*
459 * The rendezvous point has not been established yet!
460 * This could mean that either mountd(1m) has not yet
461 * been started or that _this_ routine nuked the door
462 * handle after receiving an EINTR for a REVOKED door.
463 *
464 * Returning NFSAUTH_DROP will cause the NFS client
465 * to retransmit the request, so let's try to be more
466 * rescillient and attempt for ntries before we bail.
467 */
468 if (++ntries % NFSAUTH_DR_TRYCNT) {
469 delay(hz);
470 goto retry;
471 }
472
473 kmem_free(abuf, absz);
474
475 sys_log("nfsauth: mountd has not established door");
476 *access = NFSAUTH_DROP;
477 return (FALSE);
478 }
479
480 ntries = 0;
481
482 /*
483 * Now that we've got what we need, place the call.
484 */
485 switch (door_ki_upcall_limited(dh, &da, NULL, SIZE_MAX, 0)) {
486 case 0: /* Success */
487 door_ki_rele(dh);
488
489 if (da.data_ptr == NULL && da.data_size == 0) {
490 /*
491 * The door_return that contained the data
492 * failed! We're here because of the 2nd
493 * door_return (w/o data) such that we can
494 * get control of the thread (and exit
495 * gracefully).
496 */
497 DTRACE_PROBE1(nfsserv__func__nfsauth__door__nil,
498 door_arg_t *, &da);
499 goto fail;
500 }
501
502 break;
503
504 case EAGAIN:
505 /*
506 * Server out of resources; back off for a bit
507 */
508 door_ki_rele(dh);
509 delay(hz);
510 goto retry;
511 /* NOTREACHED */
512
513 case EINTR:
514 if (!door_ki_info(dh, &di)) {
515 door_ki_rele(dh);
516
517 if (di.di_attributes & DOOR_REVOKED) {
518 /*
519 * The server barfed and revoked
520 * the (existing) door on us; we
521 * want to wait to give smf(5) a
522 * chance to restart mountd(1m)
523 * and establish a new door handle.
524 */
525 mutex_enter(&nag->mountd_lock);
526 if (dh == nag->mountd_dh) {
527 door_ki_rele(nag->mountd_dh);
528 nag->mountd_dh = NULL;
529 }
530 mutex_exit(&nag->mountd_lock);
531 delay(hz);
532 goto retry;
533 }
534 /*
535 * If the door was _not_ revoked on us,
536 * then more than likely we took an INTR,
537 * so we need to fail the operation.
538 */
539 goto fail;
540 }
541 /*
542 * The only failure that can occur from getting
543 * the door info is EINVAL, so we let the code
544 * below handle it.
545 */
546 /* FALLTHROUGH */
547
548 case EBADF:
549 case EINVAL:
550 default:
551 /*
552 * If we have a stale door handle, give smf a last
553 * chance to start it by sleeping for a little bit.
554 * If we're still hosed, we'll fail the call.
555 *
556 * Since we're going to reacquire the door handle
557 * upon the retry, we opt to sleep for a bit and
558 * _not_ to clear mountd_dh. If mountd restarted
559 * and was able to set mountd_dh, we should see
560 * the new instance; if not, we won't get caught
561 * up in the retry/DELAY loop.
562 */
563 door_ki_rele(dh);
564 if (!last) {
565 delay(hz);
566 last++;
567 goto retry;
568 }
569 sys_log("nfsauth: stale mountd door handle");
570 goto fail;
571 }
572
573 ASSERT(da.rbuf != NULL);
574
575 /*
576 * No door errors encountered; setup the XDR stream for decoding
577 * the results. If we fail to decode the results, we've got no
578 * other recourse than to fail the request.
579 */
580 xdrmem_create(&xdrs, da.rbuf, da.rsize, XDR_DECODE);
581 if (!xdr_nfsauth_res(&xdrs, &res)) {
582 xdr_free(xdr_nfsauth_res, (char *)&res);
583 XDR_DESTROY(&xdrs);
584 kmem_free(da.rbuf, da.rsize);
585 goto fail;
586 }
587 XDR_DESTROY(&xdrs);
588 kmem_free(da.rbuf, da.rsize);
589
590 DTRACE_PROBE1(nfsserv__func__nfsauth__results, nfsauth_res_t *, &res);
591 switch (res.stat) {
592 case NFSAUTH_DR_OKAY:
593 *access = res.ares.auth_perm;
594 *srv_uid = res.ares.auth_srv_uid;
595 *srv_gid = res.ares.auth_srv_gid;
596 *srv_gids_cnt = res.ares.auth_srv_gids.len;
597 *srv_gids = kmem_alloc(*srv_gids_cnt * sizeof (gid_t),
598 KM_SLEEP);
599 bcopy(res.ares.auth_srv_gids.val, *srv_gids,
600 *srv_gids_cnt * sizeof (gid_t));
601 break;
602
603 case NFSAUTH_DR_EFAIL:
604 case NFSAUTH_DR_DECERR:
605 case NFSAUTH_DR_BADCMD:
606 default:
607 xdr_free(xdr_nfsauth_res, (char *)&res);
608 fail:
609 *access = NFSAUTH_DENIED;
610 kmem_free(abuf, absz);
611 return (FALSE);
612 /* NOTREACHED */
613 }
614
615 xdr_free(xdr_nfsauth_res, (char *)&res);
616 kmem_free(abuf, absz);
617
618 return (TRUE);
619 }
620
621 static void
622 nfsauth_refresh_thread(nfsauth_globals_t *nag)
623 {
624 refreshq_exi_node_t *ren;
625 refreshq_auth_node_t *ran;
626
627 struct exportinfo *exi;
628
629 int access;
630 bool_t retrieval;
631
632 callb_cpr_t cprinfo;
633
634 CALLB_CPR_INIT(&cprinfo, &nag->refreshq_lock, callb_generic_cpr,
635 "nfsauth_refresh");
636
637 for (;;) {
638 mutex_enter(&nag->refreshq_lock);
639 if (nag->refreshq_thread_state != REFRESHQ_THREAD_RUNNING) {
640 /* Keep the hold on the lock! */
641 break;
642 }
643
644 ren = list_remove_head(&nag->refreshq_queue);
645 if (ren == NULL) {
646 CALLB_CPR_SAFE_BEGIN(&cprinfo);
647 cv_wait(&nag->refreshq_cv, &nag->refreshq_lock);
648 CALLB_CPR_SAFE_END(&cprinfo, &nag->refreshq_lock);
649 mutex_exit(&nag->refreshq_lock);
650 continue;
651 }
652 mutex_exit(&nag->refreshq_lock);
653
654 exi = ren->ren_exi;
655 ASSERT(exi != NULL);
656
657 /*
658 * Since the ren was removed from the refreshq_queue above,
659 * this is the only thread aware about the ren existence, so we
660 * have the exclusive ownership of it and we do not need to
661 * protect it by any lock.
662 */
663 while ((ran = list_remove_head(&ren->ren_authlist))) {
664 uid_t uid;
665 gid_t gid;
666 uint_t ngids;
667 gid_t *gids;
668 struct auth_cache *p = ran->ran_auth;
669 char *netid = ran->ran_netid;
670
671 ASSERT(p != NULL);
672 ASSERT(netid != NULL);
673
674 kmem_free(ran, sizeof (refreshq_auth_node_t));
675
676 mutex_enter(&p->auth_lock);
677
678 /*
679 * Once the entry goes INVALID, it can not change
680 * state.
681 *
682 * No need to refresh entries also in a case we are
683 * just shutting down.
684 *
685 * In general, there is no need to hold the
686 * refreshq_lock to test the refreshq_thread_state. We
687 * do hold it at other places because there is some
688 * related thread synchronization (or some other tasks)
689 * close to the refreshq_thread_state check.
690 *
691 * The check for the refreshq_thread_state value here
692 * is purely advisory to allow the faster
693 * nfsauth_refresh_thread() shutdown. In a case we
694 * will miss such advisory, nothing catastrophic
695 * happens: we will just spin longer here before the
696 * shutdown.
697 */
698 if (p->auth_state == NFS_AUTH_INVALID ||
699 nag->refreshq_thread_state !=
700 REFRESHQ_THREAD_RUNNING) {
701 mutex_exit(&p->auth_lock);
702
703 if (p->auth_state == NFS_AUTH_INVALID)
704 nfsauth_free_node(p);
705
706 strfree(netid);
707
708 continue;
709 }
710
711 /*
712 * Make sure the state is valid. Note that once we
713 * change the state to NFS_AUTH_REFRESHING, no other
714 * thread will be able to work on this entry.
715 */
716 ASSERT(p->auth_state == NFS_AUTH_STALE);
717
718 p->auth_state = NFS_AUTH_REFRESHING;
719 mutex_exit(&p->auth_lock);
720
721 DTRACE_PROBE2(nfsauth__debug__cache__refresh,
722 struct exportinfo *, exi,
723 struct auth_cache *, p);
724
725 /*
726 * The first caching of the access rights
727 * is done with the netid pulled out of the
728 * request from the client. All subsequent
729 * users of the cache may or may not have
730 * the same netid. It doesn't matter. So
731 * when we refresh, we simply use the netid
732 * of the request which triggered the
733 * refresh attempt.
734 */
735 retrieval = nfsauth_retrieve(nag, exi, netid,
736 p->auth_flavor, &p->auth_clnt->authc_addr, &access,
737 p->auth_clnt_cred, &uid, &gid, &ngids, &gids);
738
739 /*
740 * This can only be set in one other place
741 * and the state has to be NFS_AUTH_FRESH.
742 */
743 strfree(netid);
744
745 mutex_enter(&p->auth_lock);
746 if (p->auth_state == NFS_AUTH_INVALID) {
747 mutex_exit(&p->auth_lock);
748 nfsauth_free_node(p);
749 if (retrieval == TRUE)
750 kmem_free(gids, ngids * sizeof (gid_t));
751 } else {
752 /*
753 * If we got an error, do not reset the
754 * time. This will cause the next access
755 * check for the client to reschedule this
756 * node.
757 */
758 if (retrieval == TRUE) {
759 p->auth_access = access;
760
761 p->auth_srv_uid = uid;
762 p->auth_srv_gid = gid;
763 kmem_free(p->auth_srv_gids,
764 p->auth_srv_ngids * sizeof (gid_t));
765 p->auth_srv_ngids = ngids;
766 p->auth_srv_gids = gids;
767
768 p->auth_freshness = gethrestime_sec();
769 }
770 p->auth_state = NFS_AUTH_FRESH;
771
772 cv_broadcast(&p->auth_cv);
773 mutex_exit(&p->auth_lock);
774 }
775 }
776
777 list_destroy(&ren->ren_authlist);
778 exi_rele(ren->ren_exi);
779 kmem_free(ren, sizeof (refreshq_exi_node_t));
780 }
781
782 nag->refreshq_thread_state = REFRESHQ_THREAD_HALTED;
783 cv_broadcast(&nag->refreshq_cv);
784 CALLB_CPR_EXIT(&cprinfo);
785 DTRACE_PROBE(nfsauth__nfsauth__refresh__thread__exit);
786 zthread_exit();
787 }
788
789 int
790 nfsauth_cache_clnt_compar(const void *v1, const void *v2)
791 {
792 int c;
793
794 const struct auth_cache_clnt *a1 = (const struct auth_cache_clnt *)v1;
795 const struct auth_cache_clnt *a2 = (const struct auth_cache_clnt *)v2;
796
797 if (a1->authc_addr.len < a2->authc_addr.len)
798 return (-1);
799 if (a1->authc_addr.len > a2->authc_addr.len)
800 return (1);
801
802 c = memcmp(a1->authc_addr.buf, a2->authc_addr.buf, a1->authc_addr.len);
803 if (c < 0)
804 return (-1);
805 if (c > 0)
806 return (1);
807
808 return (0);
809 }
810
811 static int
812 nfsauth_cache_compar(const void *v1, const void *v2)
813 {
814 int c;
815
816 const struct auth_cache *a1 = (const struct auth_cache *)v1;
817 const struct auth_cache *a2 = (const struct auth_cache *)v2;
818
819 if (a1->auth_flavor < a2->auth_flavor)
820 return (-1);
821 if (a1->auth_flavor > a2->auth_flavor)
822 return (1);
823
824 if (crgetuid(a1->auth_clnt_cred) < crgetuid(a2->auth_clnt_cred))
825 return (-1);
826 if (crgetuid(a1->auth_clnt_cred) > crgetuid(a2->auth_clnt_cred))
827 return (1);
828
829 if (crgetgid(a1->auth_clnt_cred) < crgetgid(a2->auth_clnt_cred))
830 return (-1);
831 if (crgetgid(a1->auth_clnt_cred) > crgetgid(a2->auth_clnt_cred))
832 return (1);
833
834 if (crgetngroups(a1->auth_clnt_cred) < crgetngroups(a2->auth_clnt_cred))
835 return (-1);
836 if (crgetngroups(a1->auth_clnt_cred) > crgetngroups(a2->auth_clnt_cred))
837 return (1);
838
839 c = memcmp(crgetgroups(a1->auth_clnt_cred),
840 crgetgroups(a2->auth_clnt_cred), crgetngroups(a1->auth_clnt_cred));
841 if (c < 0)
842 return (-1);
843 if (c > 0)
844 return (1);
845
846 return (0);
847 }
848
849 /*
850 * Get the access information from the cache or callup to the mountd
851 * to get and cache the access information in the kernel.
852 */
853 static int
854 nfsauth_cache_get(struct exportinfo *exi, struct svc_req *req, int flavor,
855 cred_t *cr, uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
856 {
857 nfsauth_globals_t *nag;
858 struct netbuf *taddrmask;
859 struct netbuf addr; /* temporary copy of client's address */
860 const struct netbuf *claddr;
861 avl_tree_t *tree;
862 struct auth_cache ac; /* used as a template for avl_find() */
863 struct auth_cache_clnt *c;
864 struct auth_cache_clnt acc; /* used as a template for avl_find() */
865 struct auth_cache *p = NULL;
866 int access;
867
868 uid_t tmpuid;
869 gid_t tmpgid;
870 uint_t tmpngids;
871 gid_t *tmpgids;
872
873 avl_index_t where; /* used for avl_find()/avl_insert() */
874
875 ASSERT(cr != NULL);
876
877 nag = zone_getspecific(nfsauth_zone_key, curzone);
878
879 /*
880 * Now check whether this client already
881 * has an entry for this flavor in the cache
882 * for this export.
883 * Get the caller's address, mask off the
884 * parts of the address that do not identify
885 * the host (port number, etc), and then hash
886 * it to find the chain of cache entries.
887 */
888
889 claddr = svc_getrpccaller(req->rq_xprt);
890 addr = *claddr;
891 addr.buf = kmem_alloc(addr.maxlen, KM_SLEEP);
892 bcopy(claddr->buf, addr.buf, claddr->len);
893
894 SVC_GETADDRMASK(req->rq_xprt, SVC_TATTR_ADDRMASK, (void **)&taddrmask);
895 ASSERT(taddrmask != NULL);
896 addrmask(&addr, taddrmask);
897
898 ac.auth_flavor = flavor;
899 ac.auth_clnt_cred = crdup(cr);
900
901 acc.authc_addr = addr;
902
903 tree = exi->exi_cache[hash(&addr)];
904
905 rw_enter(&exi->exi_cache_lock, RW_READER);
906 c = (struct auth_cache_clnt *)avl_find(tree, &acc, NULL);
907
908 if (c == NULL) {
909 struct auth_cache_clnt *nc;
910
911 rw_exit(&exi->exi_cache_lock);
912
913 nc = kmem_alloc(sizeof (*nc), KM_NOSLEEP | KM_NORMALPRI);
914 if (nc == NULL)
915 goto retrieve;
916
917 /*
918 * Initialize the new auth_cache_clnt
919 */
920 nc->authc_addr = addr;
921 nc->authc_addr.buf = kmem_alloc(addr.maxlen,
922 KM_NOSLEEP | KM_NORMALPRI);
923 if (addr.maxlen != 0 && nc->authc_addr.buf == NULL) {
924 kmem_free(nc, sizeof (*nc));
925 goto retrieve;
926 }
927 bcopy(addr.buf, nc->authc_addr.buf, addr.len);
928 rw_init(&nc->authc_lock, NULL, RW_DEFAULT, NULL);
929 avl_create(&nc->authc_tree, nfsauth_cache_compar,
930 sizeof (struct auth_cache),
931 offsetof(struct auth_cache, auth_link));
932
933 rw_enter(&exi->exi_cache_lock, RW_WRITER);
934 c = (struct auth_cache_clnt *)avl_find(tree, &acc, &where);
935 if (c == NULL) {
936 avl_insert(tree, nc, where);
937 rw_downgrade(&exi->exi_cache_lock);
938 c = nc;
939 } else {
940 rw_downgrade(&exi->exi_cache_lock);
941
942 avl_destroy(&nc->authc_tree);
943 rw_destroy(&nc->authc_lock);
944 kmem_free(nc->authc_addr.buf, nc->authc_addr.maxlen);
945 kmem_free(nc, sizeof (*nc));
946 }
947 }
948
949 ASSERT(c != NULL);
950
951 rw_enter(&c->authc_lock, RW_READER);
952 p = (struct auth_cache *)avl_find(&c->authc_tree, &ac, NULL);
953
954 if (p == NULL) {
955 struct auth_cache *np;
956
957 rw_exit(&c->authc_lock);
958
959 np = kmem_cache_alloc(exi_cache_handle,
960 KM_NOSLEEP | KM_NORMALPRI);
961 if (np == NULL) {
962 rw_exit(&exi->exi_cache_lock);
963 goto retrieve;
964 }
965
966 /*
967 * Initialize the new auth_cache
968 */
969 np->auth_clnt = c;
970 np->auth_flavor = flavor;
971 np->auth_clnt_cred = ac.auth_clnt_cred;
972 np->auth_srv_ngids = 0;
973 np->auth_srv_gids = NULL;
974 np->auth_time = np->auth_freshness = gethrestime_sec();
975 np->auth_state = NFS_AUTH_NEW;
976 mutex_init(&np->auth_lock, NULL, MUTEX_DEFAULT, NULL);
977 cv_init(&np->auth_cv, NULL, CV_DEFAULT, NULL);
978
979 rw_enter(&c->authc_lock, RW_WRITER);
980 rw_exit(&exi->exi_cache_lock);
981
982 p = (struct auth_cache *)avl_find(&c->authc_tree, &ac, &where);
983 if (p == NULL) {
984 avl_insert(&c->authc_tree, np, where);
985 rw_downgrade(&c->authc_lock);
986 p = np;
987 } else {
988 rw_downgrade(&c->authc_lock);
989
990 cv_destroy(&np->auth_cv);
991 mutex_destroy(&np->auth_lock);
992 crfree(ac.auth_clnt_cred);
993 kmem_cache_free(exi_cache_handle, np);
994 }
995 } else {
996 rw_exit(&exi->exi_cache_lock);
997 crfree(ac.auth_clnt_cred);
998 }
999
1000 mutex_enter(&p->auth_lock);
1001 rw_exit(&c->authc_lock);
1002
1003 /*
1004 * If the entry is in the WAITING state then some other thread is just
1005 * retrieving the required info. The entry was either NEW, or the list
1006 * of client's supplemental groups is going to be changed (either by
1007 * this thread, or by some other thread). We need to wait until the
1008 * nfsauth_retrieve() is done.
1009 */
1010 while (p->auth_state == NFS_AUTH_WAITING)
1011 cv_wait(&p->auth_cv, &p->auth_lock);
1012
1013 /*
1014 * Here the entry cannot be in WAITING or INVALID state.
1015 */
1016 ASSERT(p->auth_state != NFS_AUTH_WAITING);
1017 ASSERT(p->auth_state != NFS_AUTH_INVALID);
1018
1019 /*
1020 * If the cache entry is not valid yet, we need to retrieve the
1021 * info ourselves.
1022 */
1023 if (p->auth_state == NFS_AUTH_NEW) {
1024 bool_t res;
1025 /*
1026 * NFS_AUTH_NEW is the default output auth_state value in a
1027 * case we failed somewhere below.
1028 */
1029 auth_state_t state = NFS_AUTH_NEW;
1030
1031 p->auth_state = NFS_AUTH_WAITING;
1032 mutex_exit(&p->auth_lock);
1033 kmem_free(addr.buf, addr.maxlen);
1034 addr = p->auth_clnt->authc_addr;
1035
1036 atomic_inc_uint(&nfsauth_cache_miss);
1037
1038 res = nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt),
1039 flavor, &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids,
1040 &tmpgids);
1041
1042 p->auth_access = access;
1043 p->auth_time = p->auth_freshness = gethrestime_sec();
1044
1045 if (res == TRUE) {
1046 if (uid != NULL)
1047 *uid = tmpuid;
1048 if (gid != NULL)
1049 *gid = tmpgid;
1050 if (ngids != NULL && gids != NULL) {
1051 *ngids = tmpngids;
1052 *gids = tmpgids;
1053
1054 /*
1055 * We need a copy of gids for the
1056 * auth_cache entry
1057 */
1058 tmpgids = kmem_alloc(tmpngids * sizeof (gid_t),
1059 KM_NOSLEEP | KM_NORMALPRI);
1060 if (tmpgids != NULL)
1061 bcopy(*gids, tmpgids,
1062 tmpngids * sizeof (gid_t));
1063 }
1064
1065 if (tmpgids != NULL || tmpngids == 0) {
1066 p->auth_srv_uid = tmpuid;
1067 p->auth_srv_gid = tmpgid;
1068 p->auth_srv_ngids = tmpngids;
1069 p->auth_srv_gids = tmpgids;
1070
1071 state = NFS_AUTH_FRESH;
1072 }
1073 }
1074
1075 /*
1076 * Set the auth_state and notify waiters.
1077 */
1078 mutex_enter(&p->auth_lock);
1079 p->auth_state = state;
1080 cv_broadcast(&p->auth_cv);
1081 mutex_exit(&p->auth_lock);
1082 } else {
1083 uint_t nach;
1084 time_t refresh;
1085
1086 refresh = gethrestime_sec() - p->auth_freshness;
1087
1088 p->auth_time = gethrestime_sec();
1089
1090 if (uid != NULL)
1091 *uid = p->auth_srv_uid;
1092 if (gid != NULL)
1093 *gid = p->auth_srv_gid;
1094 if (ngids != NULL && gids != NULL) {
1095 *ngids = p->auth_srv_ngids;
1096 *gids = kmem_alloc(*ngids * sizeof (gid_t), KM_SLEEP);
1097 bcopy(p->auth_srv_gids, *gids, *ngids * sizeof (gid_t));
1098 }
1099
1100 access = p->auth_access;
1101
1102 if ((refresh > NFSAUTH_CACHE_REFRESH) &&
1103 p->auth_state == NFS_AUTH_FRESH) {
1104 refreshq_auth_node_t *ran;
1105 uint_t nacr;
1106
1107 p->auth_state = NFS_AUTH_STALE;
1108 mutex_exit(&p->auth_lock);
1109
1110 nacr = atomic_inc_uint_nv(&nfsauth_cache_refresh);
1111 DTRACE_PROBE3(nfsauth__debug__cache__stale,
1112 struct exportinfo *, exi,
1113 struct auth_cache *, p,
1114 uint_t, nacr);
1115
1116 ran = kmem_alloc(sizeof (refreshq_auth_node_t),
1117 KM_SLEEP);
1118 ran->ran_auth = p;
1119 ran->ran_netid = strdup(svc_getnetid(req->rq_xprt));
1120
1121 mutex_enter(&nag->refreshq_lock);
1122
1123 if (nag->refreshq_thread_state ==
1124 REFRESHQ_THREAD_NEED_CREATE) {
1125 /* Launch nfsauth refresh thread */
1126 nag->refreshq_thread_state =
1127 REFRESHQ_THREAD_RUNNING;
1128 (void) zthread_create(NULL, 0,
1129 nfsauth_refresh_thread, nag, 0,
1130 minclsyspri);
1131 }
1132
1133 /*
1134 * We should not add a work queue item if the thread
1135 * is not accepting them.
1136 */
1137 if (nag->refreshq_thread_state ==
1138 REFRESHQ_THREAD_RUNNING) {
1139 refreshq_exi_node_t *ren;
1140
1141 /*
1142 * Is there an existing exi_list?
1143 */
1144 for (ren = list_head(&nag->refreshq_queue);
1145 ren != NULL;
1146 ren = list_next(&nag->refreshq_queue,
1147 ren)) {
1148 if (ren->ren_exi == exi) {
1149 list_insert_tail(
1150 &ren->ren_authlist, ran);
1151 break;
1152 }
1153 }
1154
1155 if (ren == NULL) {
1156 ren = kmem_alloc(
1157 sizeof (refreshq_exi_node_t),
1158 KM_SLEEP);
1159
1160 exi_hold(exi);
1161 ren->ren_exi = exi;
1162
1163 list_create(&ren->ren_authlist,
1164 sizeof (refreshq_auth_node_t),
1165 offsetof(refreshq_auth_node_t,
1166 ran_node));
1167
1168 list_insert_tail(&ren->ren_authlist,
1169 ran);
1170 list_insert_tail(&nag->refreshq_queue,
1171 ren);
1172 }
1173
1174 cv_broadcast(&nag->refreshq_cv);
1175 } else {
1176 strfree(ran->ran_netid);
1177 kmem_free(ran, sizeof (refreshq_auth_node_t));
1178 }
1179
1180 mutex_exit(&nag->refreshq_lock);
1181 } else {
1182 mutex_exit(&p->auth_lock);
1183 }
1184
1185 nach = atomic_inc_uint_nv(&nfsauth_cache_hit);
1186 DTRACE_PROBE2(nfsauth__debug__cache__hit,
1187 uint_t, nach,
1188 time_t, refresh);
1189
1190 kmem_free(addr.buf, addr.maxlen);
1191 }
1192
1193 return (access);
1194
1195 retrieve:
1196 crfree(ac.auth_clnt_cred);
1197
1198 /*
1199 * Retrieve the required data without caching.
1200 */
1201
1202 ASSERT(p == NULL);
1203
1204 atomic_inc_uint(&nfsauth_cache_miss);
1205
1206 if (nfsauth_retrieve(nag, exi, svc_getnetid(req->rq_xprt), flavor,
1207 &addr, &access, cr, &tmpuid, &tmpgid, &tmpngids, &tmpgids)) {
1208 if (uid != NULL)
1209 *uid = tmpuid;
1210 if (gid != NULL)
1211 *gid = tmpgid;
1212 if (ngids != NULL && gids != NULL) {
1213 *ngids = tmpngids;
1214 *gids = tmpgids;
1215 } else {
1216 kmem_free(tmpgids, tmpngids * sizeof (gid_t));
1217 }
1218 }
1219
1220 kmem_free(addr.buf, addr.maxlen);
1221
1222 return (access);
1223 }
1224
1225 /*
1226 * Check if the requesting client has access to the filesystem with
1227 * a given nfs flavor number which is an explicitly shared flavor.
1228 */
1229 int
1230 nfsauth4_secinfo_access(struct exportinfo *exi, struct svc_req *req,
1231 int flavor, int perm, cred_t *cr)
1232 {
1233 int access;
1234
1235 if (! (perm & M_4SEC_EXPORTED)) {
1236 return (NFSAUTH_DENIED);
1237 }
1238
1239 /*
1240 * Optimize if there are no lists
1241 */
1242 if ((perm & (M_ROOT | M_NONE | M_MAP)) == 0) {
1243 perm &= ~M_4SEC_EXPORTED;
1244 if (perm == M_RO)
1245 return (NFSAUTH_RO);
1246 if (perm == M_RW)
1247 return (NFSAUTH_RW);
1248 }
1249
1250 access = nfsauth_cache_get(exi, req, flavor, cr, NULL, NULL, NULL,
1251 NULL);
1252
1253 return (access);
1254 }
1255
1256 int
1257 nfsauth_access(struct exportinfo *exi, struct svc_req *req, cred_t *cr,
1258 uid_t *uid, gid_t *gid, uint_t *ngids, gid_t **gids)
1259 {
1260 int access, mapaccess;
1261 struct secinfo *sp;
1262 int i, flavor, perm;
1263 int authnone_entry = -1;
1264
1265 /*
1266 * By default root is mapped to anonymous user.
1267 * This might get overriden later in nfsauth_cache_get().
1268 */
1269 if (crgetuid(cr) == 0) {
1270 if (uid != NULL)
1271 *uid = exi->exi_export.ex_anon;
1272 if (gid != NULL)
1273 *gid = exi->exi_export.ex_anon;
1274 } else {
1275 if (uid != NULL)
1276 *uid = crgetuid(cr);
1277 if (gid != NULL)
1278 *gid = crgetgid(cr);
1279 }
1280
1281 if (ngids != NULL)
1282 *ngids = 0;
1283 if (gids != NULL)
1284 *gids = NULL;
1285
1286 /*
1287 * Get the nfs flavor number from xprt.
1288 */
1289 flavor = (int)(uintptr_t)req->rq_xprt->xp_cookie;
1290
1291 /*
1292 * First check the access restrictions on the filesystem. If
1293 * there are no lists associated with this flavor then there's no
1294 * need to make an expensive call to the nfsauth service or to
1295 * cache anything.
1296 */
1297
1298 sp = exi->exi_export.ex_secinfo;
1299 for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
1300 if (flavor != sp[i].s_secinfo.sc_nfsnum) {
1301 if (sp[i].s_secinfo.sc_nfsnum == AUTH_NONE)
1302 authnone_entry = i;
1303 continue;
1304 }
1305 break;
1306 }
1307
1308 mapaccess = 0;
1309
1310 if (i >= exi->exi_export.ex_seccnt) {
1311 /*
1312 * Flavor not found, but use AUTH_NONE if it exists
1313 */
1314 if (authnone_entry == -1)
1315 return (NFSAUTH_DENIED);
1316 flavor = AUTH_NONE;
1317 mapaccess = NFSAUTH_MAPNONE;
1318 i = authnone_entry;
1319 }
1320
1321 /*
1322 * If the flavor is in the ex_secinfo list, but not an explicitly
1323 * shared flavor by the user, it is a result of the nfsv4 server
1324 * namespace setup. We will grant an RO permission similar for
1325 * a pseudo node except that this node is a shared one.
1326 *
1327 * e.g. flavor in (flavor) indicates that it is not explictly
1328 * shared by the user:
1329 *
1330 * / (sys, krb5)
1331 * |
1332 * export #share -o sec=sys (krb5)
1333 * |
1334 * secure #share -o sec=krb5
1335 *
1336 * In this case, when a krb5 request coming in to access
1337 * /export, RO permission is granted.
1338 */
1339 if (!(sp[i].s_flags & M_4SEC_EXPORTED))
1340 return (mapaccess | NFSAUTH_RO);
1341
1342 /*
1343 * Optimize if there are no lists.
1344 * We cannot optimize for AUTH_SYS with NGRPS (16) supplemental groups.
1345 */
1346 perm = sp[i].s_flags;
1347 if ((perm & (M_ROOT | M_NONE | M_MAP)) == 0 && (ngroups_max <= NGRPS ||
1348 flavor != AUTH_SYS || crgetngroups(cr) < NGRPS)) {
1349 perm &= ~M_4SEC_EXPORTED;
1350 if (perm == M_RO)
1351 return (mapaccess | NFSAUTH_RO);
1352 if (perm == M_RW)
1353 return (mapaccess | NFSAUTH_RW);
1354 }
1355
1356 access = nfsauth_cache_get(exi, req, flavor, cr, uid, gid, ngids, gids);
1357
1358 /*
1359 * For both NFSAUTH_DENIED and NFSAUTH_WRONGSEC we do not care about
1360 * the supplemental groups.
1361 */
1362 if (access & NFSAUTH_DENIED || access & NFSAUTH_WRONGSEC) {
1363 if (ngids != NULL && gids != NULL) {
1364 kmem_free(*gids, *ngids * sizeof (gid_t));
1365 *ngids = 0;
1366 *gids = NULL;
1367 }
1368 }
1369
1370 /*
1371 * Client's security flavor doesn't match with "ro" or
1372 * "rw" list. Try again using AUTH_NONE if present.
1373 */
1374 if ((access & NFSAUTH_WRONGSEC) && (flavor != AUTH_NONE)) {
1375 /*
1376 * Have we already encountered AUTH_NONE ?
1377 */
1378 if (authnone_entry != -1) {
1379 mapaccess = NFSAUTH_MAPNONE;
1380 access = nfsauth_cache_get(exi, req, AUTH_NONE, cr,
1381 NULL, NULL, NULL, NULL);
1382 } else {
1383 /*
1384 * Check for AUTH_NONE presence.
1385 */
1386 for (; i < exi->exi_export.ex_seccnt; i++) {
1387 if (sp[i].s_secinfo.sc_nfsnum == AUTH_NONE) {
1388 mapaccess = NFSAUTH_MAPNONE;
1389 access = nfsauth_cache_get(exi, req,
1390 AUTH_NONE, cr, NULL, NULL, NULL,
1391 NULL);
1392 break;
1393 }
1394 }
1395 }
1396 }
1397
1398 if (access & NFSAUTH_DENIED)
1399 access = NFSAUTH_DENIED;
1400
1401 return (access | mapaccess);
1402 }
1403
1404 static void
1405 nfsauth_free_clnt_node(struct auth_cache_clnt *p)
1406 {
1407 void *cookie = NULL;
1408 struct auth_cache *node;
1409
1410 while ((node = avl_destroy_nodes(&p->authc_tree, &cookie)) != NULL)
1411 nfsauth_free_node(node);
1412 avl_destroy(&p->authc_tree);
1413
1414 kmem_free(p->authc_addr.buf, p->authc_addr.maxlen);
1415 rw_destroy(&p->authc_lock);
1416
1417 kmem_free(p, sizeof (*p));
1418 }
1419
1420 static void
1421 nfsauth_free_node(struct auth_cache *p)
1422 {
1423 crfree(p->auth_clnt_cred);
1424 kmem_free(p->auth_srv_gids, p->auth_srv_ngids * sizeof (gid_t));
1425 mutex_destroy(&p->auth_lock);
1426 cv_destroy(&p->auth_cv);
1427 kmem_cache_free(exi_cache_handle, p);
1428 }
1429
1430 /*
1431 * Free the nfsauth cache for a given export
1432 */
1433 void
1434 nfsauth_cache_free(struct exportinfo *exi)
1435 {
1436 int i;
1437
1438 /*
1439 * The only way we got here was with an exi_rele, which means that no
1440 * auth cache entry is being refreshed.
1441 */
1442
1443 for (i = 0; i < AUTH_TABLESIZE; i++) {
1444 avl_tree_t *tree = exi->exi_cache[i];
1445 void *cookie = NULL;
1446 struct auth_cache_clnt *node;
1447
1448 while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
1449 nfsauth_free_clnt_node(node);
1450 }
1451 }
1452
1453 /*
1454 * Called by the kernel memory allocator when
1455 * memory is low. Free unused cache entries.
1456 * If that's not enough, the VM system will
1457 * call again for some more.
1458 */
1459 /*ARGSUSED*/
1460 void
1461 exi_cache_reclaim(void *cdrarg)
1462 {
1463 int i;
1464 struct exportinfo *exi;
1465 nfs_export_t *ne = nfs_get_export();
1466
1467 rw_enter(&ne->exported_lock, RW_READER);
1468
1469 for (i = 0; i < EXPTABLESIZE; i++) {
1470 for (exi = ne->exptable[i]; exi; exi = exi->fid_hash.next)
1471 exi_cache_trim(exi);
1472 }
1473
1474 rw_exit(&ne->exported_lock);
1475
1476 atomic_inc_uint(&nfsauth_cache_reclaim);
1477 }
1478
1479 void
1480 exi_cache_trim(struct exportinfo *exi)
1481 {
1482 struct auth_cache_clnt *c;
1483 struct auth_cache_clnt *nextc;
1484 struct auth_cache *p;
1485 struct auth_cache *next;
1486 int i;
1487 time_t stale_time;
1488 avl_tree_t *tree;
1489
1490 for (i = 0; i < AUTH_TABLESIZE; i++) {
1491 tree = exi->exi_cache[i];
1492 stale_time = gethrestime_sec() - NFSAUTH_CACHE_TRIM;
1493 rw_enter(&exi->exi_cache_lock, RW_READER);
1494
1495 /*
1496 * Free entries that have not been
1497 * used for NFSAUTH_CACHE_TRIM seconds.
1498 */
1499 for (c = avl_first(tree); c != NULL; c = AVL_NEXT(tree, c)) {
1500 /*
1501 * We are being called by the kmem subsystem to reclaim
1502 * memory so don't block if we can't get the lock.
1503 */
1504 if (rw_tryenter(&c->authc_lock, RW_WRITER) == 0) {
1505 exi_cache_auth_reclaim_failed++;
1506 rw_exit(&exi->exi_cache_lock);
1507 return;
1508 }
1509
1510 for (p = avl_first(&c->authc_tree); p != NULL;
1511 p = next) {
1512 next = AVL_NEXT(&c->authc_tree, p);
1513
1514 ASSERT(p->auth_state != NFS_AUTH_INVALID);
1515
1516 mutex_enter(&p->auth_lock);
1517
1518 /*
1519 * We won't trim recently used and/or WAITING
1520 * entries.
1521 */
1522 if (p->auth_time > stale_time ||
1523 p->auth_state == NFS_AUTH_WAITING) {
1524 mutex_exit(&p->auth_lock);
1525 continue;
1526 }
1527
1528 DTRACE_PROBE1(nfsauth__debug__trim__state,
1529 auth_state_t, p->auth_state);
1530
1531 /*
1532 * STALE and REFRESHING entries needs to be
1533 * marked INVALID only because they are
1534 * referenced by some other structures or
1535 * threads. They will be freed later.
1536 */
1537 if (p->auth_state == NFS_AUTH_STALE ||
1538 p->auth_state == NFS_AUTH_REFRESHING) {
1539 p->auth_state = NFS_AUTH_INVALID;
1540 mutex_exit(&p->auth_lock);
1541
1542 avl_remove(&c->authc_tree, p);
1543 } else {
1544 mutex_exit(&p->auth_lock);
1545
1546 avl_remove(&c->authc_tree, p);
1547 nfsauth_free_node(p);
1548 }
1549 }
1550 rw_exit(&c->authc_lock);
1551 }
1552
1553 if (rw_tryupgrade(&exi->exi_cache_lock) == 0) {
1554 rw_exit(&exi->exi_cache_lock);
1555 exi_cache_clnt_reclaim_failed++;
1556 continue;
1557 }
1558
1559 for (c = avl_first(tree); c != NULL; c = nextc) {
1560 nextc = AVL_NEXT(tree, c);
1561
1562 if (avl_is_empty(&c->authc_tree) == B_FALSE)
1563 continue;
1564
1565 avl_remove(tree, c);
1566
1567 nfsauth_free_clnt_node(c);
1568 }
1569
1570 rw_exit(&exi->exi_cache_lock);
1571 }
1572 }