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