xref: /onnv-gate/usr/src/uts/common/fs/nfs/nfs_auth.c (revision 12679:30c794f87c0d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52140Srmesta  * Common Development and Distribution License (the "License").
62140Srmesta  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2212081SThomas.Haynes@Sun.COM  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/param.h>
260Sstevel@tonic-gate #include <sys/errno.h>
270Sstevel@tonic-gate #include <sys/vfs.h>
280Sstevel@tonic-gate #include <sys/vnode.h>
290Sstevel@tonic-gate #include <sys/cred.h>
300Sstevel@tonic-gate #include <sys/cmn_err.h>
310Sstevel@tonic-gate #include <sys/systm.h>
320Sstevel@tonic-gate #include <sys/kmem.h>
330Sstevel@tonic-gate #include <sys/pathname.h>
340Sstevel@tonic-gate #include <sys/utsname.h>
350Sstevel@tonic-gate #include <sys/debug.h>
362140Srmesta #include <sys/door.h>
372140Srmesta #include <sys/sdt.h>
3812081SThomas.Haynes@Sun.COM #include <sys/thread.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <rpc/types.h>
410Sstevel@tonic-gate #include <rpc/auth.h>
420Sstevel@tonic-gate #include <rpc/clnt.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <nfs/nfs.h>
450Sstevel@tonic-gate #include <nfs/export.h>
460Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
472140Srmesta #include <nfs/auth.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #define	EQADDR(a1, a2)  \
500Sstevel@tonic-gate 	(bcmp((char *)(a1)->buf, (char *)(a2)->buf, (a1)->len) == 0 && \
510Sstevel@tonic-gate 	(a1)->len == (a2)->len)
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static struct knetconfig auth_knconf;
540Sstevel@tonic-gate static servinfo_t svp;
550Sstevel@tonic-gate static clinfo_t ci;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static struct kmem_cache *exi_cache_handle;
580Sstevel@tonic-gate static void exi_cache_reclaim(void *);
590Sstevel@tonic-gate static void exi_cache_trim(struct exportinfo *exi);
600Sstevel@tonic-gate 
6112081SThomas.Haynes@Sun.COM extern pri_t minclsyspri;
6212081SThomas.Haynes@Sun.COM 
630Sstevel@tonic-gate int nfsauth_cache_hit;
640Sstevel@tonic-gate int nfsauth_cache_miss;
6512081SThomas.Haynes@Sun.COM int nfsauth_cache_refresh;
660Sstevel@tonic-gate int nfsauth_cache_reclaim;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
6912081SThomas.Haynes@Sun.COM  * The lifetime of an auth cache entry:
7012081SThomas.Haynes@Sun.COM  * ------------------------------------
7112081SThomas.Haynes@Sun.COM  *
7212081SThomas.Haynes@Sun.COM  * An auth cache entry is created with both the auth_time
7312081SThomas.Haynes@Sun.COM  * and auth_freshness times set to the current time.
7412081SThomas.Haynes@Sun.COM  *
7512081SThomas.Haynes@Sun.COM  * Upon every client access which results in a hit, the
7612081SThomas.Haynes@Sun.COM  * auth_time will be updated.
7712081SThomas.Haynes@Sun.COM  *
7812081SThomas.Haynes@Sun.COM  * If a client access determines that the auth_freshness
7912081SThomas.Haynes@Sun.COM  * indicates that the entry is STALE, then it will be
8012081SThomas.Haynes@Sun.COM  * refreshed. Note that this will explicitly reset
8112081SThomas.Haynes@Sun.COM  * auth_time.
8212081SThomas.Haynes@Sun.COM  *
8312081SThomas.Haynes@Sun.COM  * When the REFRESH successfully occurs, then the
8412081SThomas.Haynes@Sun.COM  * auth_freshness is updated.
8512081SThomas.Haynes@Sun.COM  *
8612081SThomas.Haynes@Sun.COM  * There are two ways for an entry to leave the cache:
8712081SThomas.Haynes@Sun.COM  *
8812081SThomas.Haynes@Sun.COM  * 1) Purged by an action on the export (remove or changed)
8912081SThomas.Haynes@Sun.COM  * 2) Memory backpressure from the kernel (check against NFSAUTH_CACHE_TRIM)
9012081SThomas.Haynes@Sun.COM  *
9112081SThomas.Haynes@Sun.COM  * For 2) we check the timeout value against auth_time.
9212081SThomas.Haynes@Sun.COM  */
9312081SThomas.Haynes@Sun.COM 
9412081SThomas.Haynes@Sun.COM /*
9512081SThomas.Haynes@Sun.COM  * Number of seconds until we mark for refresh an auth cache entry.
9612081SThomas.Haynes@Sun.COM  */
9712081SThomas.Haynes@Sun.COM #define	NFSAUTH_CACHE_REFRESH 600
9812081SThomas.Haynes@Sun.COM 
9912081SThomas.Haynes@Sun.COM /*
10012081SThomas.Haynes@Sun.COM  * Number of idle seconds until we yield to backpressure
10112081SThomas.Haynes@Sun.COM  * to trim a cache entry.
10212081SThomas.Haynes@Sun.COM  */
10312081SThomas.Haynes@Sun.COM #define	NFSAUTH_CACHE_TRIM 3600
10412081SThomas.Haynes@Sun.COM 
10512081SThomas.Haynes@Sun.COM /*
10612081SThomas.Haynes@Sun.COM  * While we could encapuslate the exi_list inside the
10712081SThomas.Haynes@Sun.COM  * exi structure, we can't do that for the auth_list.
10812081SThomas.Haynes@Sun.COM  * So, to keep things looking clean, we keep them both
10912081SThomas.Haynes@Sun.COM  * in these external lists.
1100Sstevel@tonic-gate  */
11112081SThomas.Haynes@Sun.COM typedef struct refreshq_exi_node {
11212081SThomas.Haynes@Sun.COM 	struct exportinfo	*ren_exi;
11312081SThomas.Haynes@Sun.COM 	list_t			ren_authlist;
11412081SThomas.Haynes@Sun.COM 	list_node_t		ren_node;
11512081SThomas.Haynes@Sun.COM } refreshq_exi_node_t;
11612081SThomas.Haynes@Sun.COM 
11712081SThomas.Haynes@Sun.COM typedef struct refreshq_auth_node {
11812081SThomas.Haynes@Sun.COM 	struct auth_cache	*ran_auth;
11912081SThomas.Haynes@Sun.COM 	list_node_t		ran_node;
12012081SThomas.Haynes@Sun.COM } refreshq_auth_node_t;
12112081SThomas.Haynes@Sun.COM 
12212081SThomas.Haynes@Sun.COM /*
12312081SThomas.Haynes@Sun.COM  * Used to manipulate things on the refreshq_queue.
12412081SThomas.Haynes@Sun.COM  * Note that the refresh thread will effectively
12512081SThomas.Haynes@Sun.COM  * pop a node off of the queue, at which point it
12612081SThomas.Haynes@Sun.COM  * will no longer need to hold the mutex.
12712081SThomas.Haynes@Sun.COM  */
12812081SThomas.Haynes@Sun.COM static kmutex_t refreshq_lock;
12912081SThomas.Haynes@Sun.COM static list_t refreshq_queue;
13012081SThomas.Haynes@Sun.COM static kcondvar_t refreshq_cv;
13112081SThomas.Haynes@Sun.COM 
13212081SThomas.Haynes@Sun.COM /*
13312081SThomas.Haynes@Sun.COM  * A list_t would be overkill. These are auth_cache
13412081SThomas.Haynes@Sun.COM  * entries which are no longer linked to an exi.
13512081SThomas.Haynes@Sun.COM  * It should be the case that all of their states
13612081SThomas.Haynes@Sun.COM  * are NFS_AUTH_INVALID.
13712081SThomas.Haynes@Sun.COM  *
13812081SThomas.Haynes@Sun.COM  * I.e., the only way to be put on this list is
13912081SThomas.Haynes@Sun.COM  * iff their state indicated that they had been placed
14012081SThomas.Haynes@Sun.COM  * on the refreshq_queue.
14112081SThomas.Haynes@Sun.COM  *
14212081SThomas.Haynes@Sun.COM  * Note that while there is no link from the exi or
14312081SThomas.Haynes@Sun.COM  * back to the exi, the exi can not go away until
14412081SThomas.Haynes@Sun.COM  * these entries are harvested.
14512081SThomas.Haynes@Sun.COM  */
14612081SThomas.Haynes@Sun.COM static struct auth_cache	*refreshq_dead_entries;
14712081SThomas.Haynes@Sun.COM 
14812081SThomas.Haynes@Sun.COM /*
14912081SThomas.Haynes@Sun.COM  * If there is ever a problem with loading the
15012081SThomas.Haynes@Sun.COM  * module, then nfsauth_fini() needs to be called
15112081SThomas.Haynes@Sun.COM  * to remove state. In that event, since the
15212081SThomas.Haynes@Sun.COM  * refreshq thread has been started, they need to
15312081SThomas.Haynes@Sun.COM  * work together to get rid of state.
15412081SThomas.Haynes@Sun.COM  */
15512081SThomas.Haynes@Sun.COM typedef enum nfsauth_refreshq_thread_state {
15612081SThomas.Haynes@Sun.COM 	REFRESHQ_THREAD_RUNNING,
15712081SThomas.Haynes@Sun.COM 	REFRESHQ_THREAD_FINI_REQ,
15812081SThomas.Haynes@Sun.COM 	REFRESHQ_THREAD_HALTED
15912081SThomas.Haynes@Sun.COM } nfsauth_refreshq_thread_state_t;
16012081SThomas.Haynes@Sun.COM 
16112081SThomas.Haynes@Sun.COM nfsauth_refreshq_thread_state_t
16212081SThomas.Haynes@Sun.COM refreshq_thread_state = REFRESHQ_THREAD_HALTED;
16312081SThomas.Haynes@Sun.COM 
16412081SThomas.Haynes@Sun.COM static void nfsauth_free_node(struct auth_cache *);
16512081SThomas.Haynes@Sun.COM static void nfsauth_remove_dead_entry(struct auth_cache *);
16612081SThomas.Haynes@Sun.COM static void nfsauth_refresh_thread(void);
1670Sstevel@tonic-gate 
1682140Srmesta /*
1692140Srmesta  * mountd is a server-side only daemon. This will need to be
1702140Srmesta  * revisited if the NFS server is ever made zones-aware.
1712140Srmesta  */
1722140Srmesta kmutex_t	mountd_lock;
1732140Srmesta door_handle_t   mountd_dh;
1742140Srmesta 
1752140Srmesta void
mountd_args(uint_t did)1762140Srmesta mountd_args(uint_t did)
1772140Srmesta {
1782140Srmesta 	mutex_enter(&mountd_lock);
1792140Srmesta 	if (mountd_dh)
1802140Srmesta 		door_ki_rele(mountd_dh);
1812140Srmesta 	mountd_dh = door_ki_lookup(did);
1822140Srmesta 	mutex_exit(&mountd_lock);
1832140Srmesta }
1842140Srmesta 
1850Sstevel@tonic-gate void
nfsauth_init(void)1860Sstevel@tonic-gate nfsauth_init(void)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate 	/*
1892140Srmesta 	 * mountd can be restarted by smf(5). We need to make sure
1902140Srmesta 	 * the updated door handle will safely make it to mountd_dh
1910Sstevel@tonic-gate 	 */
1922140Srmesta 	mutex_init(&mountd_lock, NULL, MUTEX_DEFAULT, NULL);
1930Sstevel@tonic-gate 
19412081SThomas.Haynes@Sun.COM 	mutex_init(&refreshq_lock, NULL, MUTEX_DEFAULT, NULL);
19512081SThomas.Haynes@Sun.COM 	list_create(&refreshq_queue, sizeof (refreshq_exi_node_t),
19612081SThomas.Haynes@Sun.COM 	    offsetof(refreshq_exi_node_t, ren_node));
19712081SThomas.Haynes@Sun.COM 	refreshq_dead_entries = NULL;
19812081SThomas.Haynes@Sun.COM 
19912081SThomas.Haynes@Sun.COM 	cv_init(&refreshq_cv, NULL, CV_DEFAULT, NULL);
20012081SThomas.Haynes@Sun.COM 
2010Sstevel@tonic-gate 	/*
2020Sstevel@tonic-gate 	 * Allocate nfsauth cache handle
2030Sstevel@tonic-gate 	 */
2040Sstevel@tonic-gate 	exi_cache_handle = kmem_cache_create("exi_cache_handle",
2057961SNatalie.Li@Sun.COM 	    sizeof (struct auth_cache), 0, NULL, NULL,
2067961SNatalie.Li@Sun.COM 	    exi_cache_reclaim, NULL, NULL, 0);
20712081SThomas.Haynes@Sun.COM 
20812081SThomas.Haynes@Sun.COM 	refreshq_thread_state = REFRESHQ_THREAD_RUNNING;
20912081SThomas.Haynes@Sun.COM 	(void) zthread_create(NULL, 0, nfsauth_refresh_thread,
21012081SThomas.Haynes@Sun.COM 	    NULL, 0, minclsyspri);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate  * Finalization routine for nfsauth. It is important to call this routine
2150Sstevel@tonic-gate  * before destroying the exported_lock.
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate void
nfsauth_fini(void)2180Sstevel@tonic-gate nfsauth_fini(void)
2190Sstevel@tonic-gate {
22012081SThomas.Haynes@Sun.COM 	refreshq_exi_node_t	*ren;
22112081SThomas.Haynes@Sun.COM 	refreshq_auth_node_t	*ran;
22212081SThomas.Haynes@Sun.COM 	struct auth_cache	*p;
22312081SThomas.Haynes@Sun.COM 	struct auth_cache	*auth_next;
22412081SThomas.Haynes@Sun.COM 
22512081SThomas.Haynes@Sun.COM 	/*
22612081SThomas.Haynes@Sun.COM 	 * Prevent the refreshq_thread from getting new
22712081SThomas.Haynes@Sun.COM 	 * work.
22812081SThomas.Haynes@Sun.COM 	 */
22912081SThomas.Haynes@Sun.COM 	mutex_enter(&refreshq_lock);
23012081SThomas.Haynes@Sun.COM 	if (refreshq_thread_state != REFRESHQ_THREAD_HALTED) {
23112081SThomas.Haynes@Sun.COM 		refreshq_thread_state = REFRESHQ_THREAD_FINI_REQ;
23212081SThomas.Haynes@Sun.COM 		cv_broadcast(&refreshq_cv);
23312081SThomas.Haynes@Sun.COM 
23412081SThomas.Haynes@Sun.COM 		/*
23512081SThomas.Haynes@Sun.COM 		 * Also, wait for nfsauth_refresh_thread() to exit.
23612081SThomas.Haynes@Sun.COM 		 */
23712081SThomas.Haynes@Sun.COM 		while (refreshq_thread_state != REFRESHQ_THREAD_HALTED) {
23812081SThomas.Haynes@Sun.COM 			cv_wait(&refreshq_cv, &refreshq_lock);
23912081SThomas.Haynes@Sun.COM 		}
24012081SThomas.Haynes@Sun.COM 	}
24112081SThomas.Haynes@Sun.COM 
24212081SThomas.Haynes@Sun.COM 	/*
24312081SThomas.Haynes@Sun.COM 	 * Walk the exi_list and in turn, walk the
24412081SThomas.Haynes@Sun.COM 	 * auth_lists.
24512081SThomas.Haynes@Sun.COM 	 */
24612081SThomas.Haynes@Sun.COM 	while ((ren = list_remove_head(&refreshq_queue))) {
24712081SThomas.Haynes@Sun.COM 		while ((ran = list_remove_head(&ren->ren_authlist))) {
24812081SThomas.Haynes@Sun.COM 			kmem_free(ran, sizeof (refreshq_auth_node_t));
24912081SThomas.Haynes@Sun.COM 		}
25012081SThomas.Haynes@Sun.COM 
25112081SThomas.Haynes@Sun.COM 		list_destroy(&ren->ren_authlist);
25212081SThomas.Haynes@Sun.COM 		exi_rele(ren->ren_exi);
25312081SThomas.Haynes@Sun.COM 		kmem_free(ren, sizeof (refreshq_exi_node_t));
25412081SThomas.Haynes@Sun.COM 	}
25512081SThomas.Haynes@Sun.COM 
25612081SThomas.Haynes@Sun.COM 	/*
25712081SThomas.Haynes@Sun.COM 	 * Okay, now that the lists are deleted, we
25812081SThomas.Haynes@Sun.COM 	 * need to see if there are any dead entries
25912081SThomas.Haynes@Sun.COM 	 * to harvest.
26012081SThomas.Haynes@Sun.COM 	 */
26112081SThomas.Haynes@Sun.COM 	for (p = refreshq_dead_entries; p != NULL; p = auth_next) {
26212081SThomas.Haynes@Sun.COM 		auth_next = p->auth_next;
26312081SThomas.Haynes@Sun.COM 		nfsauth_free_node(p);
26412081SThomas.Haynes@Sun.COM 	}
26512081SThomas.Haynes@Sun.COM 
26612081SThomas.Haynes@Sun.COM 	mutex_exit(&refreshq_lock);
26712081SThomas.Haynes@Sun.COM 
26812081SThomas.Haynes@Sun.COM 	list_destroy(&refreshq_queue);
26912081SThomas.Haynes@Sun.COM 
27012081SThomas.Haynes@Sun.COM 	cv_destroy(&refreshq_cv);
27112081SThomas.Haynes@Sun.COM 	mutex_destroy(&refreshq_lock);
27212081SThomas.Haynes@Sun.COM 
27312081SThomas.Haynes@Sun.COM 	mutex_destroy(&mountd_lock);
27412081SThomas.Haynes@Sun.COM 
2750Sstevel@tonic-gate 	/*
2760Sstevel@tonic-gate 	 * Deallocate nfsauth cache handle
2770Sstevel@tonic-gate 	 */
2780Sstevel@tonic-gate 	kmem_cache_destroy(exi_cache_handle);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate  * Convert the address in a netbuf to
2830Sstevel@tonic-gate  * a hash index for the auth_cache table.
2840Sstevel@tonic-gate  */
2850Sstevel@tonic-gate static int
hash(struct netbuf * a)2860Sstevel@tonic-gate hash(struct netbuf *a)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate 	int i, h = 0;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	for (i = 0; i < a->len; i++)
2910Sstevel@tonic-gate 		h ^= a->buf[i];
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	return (h & (AUTH_TABLESIZE - 1));
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate  * Mask out the components of an
2980Sstevel@tonic-gate  * address that do not identify
2990Sstevel@tonic-gate  * a host. For socket addresses the
3000Sstevel@tonic-gate  * masking gets rid of the port number.
3010Sstevel@tonic-gate  */
3020Sstevel@tonic-gate static void
addrmask(struct netbuf * addr,struct netbuf * mask)3030Sstevel@tonic-gate addrmask(struct netbuf *addr, struct netbuf *mask)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 	int i;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	for (i = 0; i < addr->len; i++)
3080Sstevel@tonic-gate 		addr->buf[i] &= mask->buf[i];
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate /*
3120Sstevel@tonic-gate  * nfsauth4_access is used for NFS V4 auth checking. Besides doing
3130Sstevel@tonic-gate  * the common nfsauth_access(), it will check if the client can
3140Sstevel@tonic-gate  * have a limited access to this vnode even if the security flavor
3150Sstevel@tonic-gate  * used does not meet the policy.
3160Sstevel@tonic-gate  */
3170Sstevel@tonic-gate int
nfsauth4_access(struct exportinfo * exi,vnode_t * vp,struct svc_req * req)3180Sstevel@tonic-gate nfsauth4_access(struct exportinfo *exi, vnode_t *vp, struct svc_req *req)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	int access;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	access = nfsauth_access(exi, req);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	/*
3250Sstevel@tonic-gate 	 * There are cases that the server needs to allow the client
3260Sstevel@tonic-gate 	 * to have a limited view.
3270Sstevel@tonic-gate 	 *
3280Sstevel@tonic-gate 	 * e.g.
3290Sstevel@tonic-gate 	 * /export is shared as "sec=sys,rw=dfs-test-4,sec=krb5,rw"
3300Sstevel@tonic-gate 	 * /export/home is shared as "sec=sys,rw"
3310Sstevel@tonic-gate 	 *
3320Sstevel@tonic-gate 	 * When the client mounts /export with sec=sys, the client
3330Sstevel@tonic-gate 	 * would get a limited view with RO access on /export to see
3340Sstevel@tonic-gate 	 * "home" only because the client is allowed to access
3350Sstevel@tonic-gate 	 * /export/home with auth_sys.
3360Sstevel@tonic-gate 	 */
3370Sstevel@tonic-gate 	if (access & NFSAUTH_DENIED || access & NFSAUTH_WRONGSEC) {
3380Sstevel@tonic-gate 		/*
3390Sstevel@tonic-gate 		 * Allow ro permission with LIMITED view if there is a
3400Sstevel@tonic-gate 		 * sub-dir exported under vp.
3410Sstevel@tonic-gate 		 */
3427961SNatalie.Li@Sun.COM 		if (has_visible(exi, vp))
3430Sstevel@tonic-gate 			return (NFSAUTH_LIMITED);
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	return (access);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3492140Srmesta static void
sys_log(const char * msg)3502140Srmesta sys_log(const char *msg)
3512140Srmesta {
3522140Srmesta 	static time_t	tstamp = 0;
3532140Srmesta 	time_t		now;
3542140Srmesta 
3552140Srmesta 	/*
3562140Srmesta 	 * msg is shown (at most) once per minute
3572140Srmesta 	 */
3582140Srmesta 	now = gethrestime_sec();
3592140Srmesta 	if ((tstamp + 60) < now) {
3602140Srmesta 		tstamp = now;
3612140Srmesta 		cmn_err(CE_WARN, msg);
3622140Srmesta 	}
3632140Srmesta }
3642140Srmesta 
3650Sstevel@tonic-gate /*
36612081SThomas.Haynes@Sun.COM  * Callup to the mountd to get access information in the kernel.
3670Sstevel@tonic-gate  */
36812081SThomas.Haynes@Sun.COM static bool_t
nfsauth_retrieve(struct exportinfo * exi,char * req_netid,int flavor,struct netbuf * addr,int * access)36912081SThomas.Haynes@Sun.COM nfsauth_retrieve(struct exportinfo *exi, char *req_netid, int flavor,
37012081SThomas.Haynes@Sun.COM     struct netbuf *addr, int *access)
3710Sstevel@tonic-gate {
3722140Srmesta 	varg_t			  varg = {0};
3732140Srmesta 	nfsauth_res_t		  res = {0};
3742140Srmesta 	XDR			  xdrs_a;
3752140Srmesta 	XDR			  xdrs_r;
3762140Srmesta 	size_t			  absz;
3772140Srmesta 	caddr_t			  abuf;
3782140Srmesta 	size_t			  rbsz = (size_t)(BYTES_PER_XDR_UNIT * 2);
3792140Srmesta 	char			  result[BYTES_PER_XDR_UNIT * 2] = {0};
3802140Srmesta 	caddr_t			  rbuf = (caddr_t)&result;
3812140Srmesta 	int			  last = 0;
3822140Srmesta 	door_arg_t		  da;
3832140Srmesta 	door_info_t		  di;
3842140Srmesta 	door_handle_t		  dh;
3852140Srmesta 	uint_t			  ntries = 0;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	/*
3880Sstevel@tonic-gate 	 * No entry in the cache for this client/flavor
3890Sstevel@tonic-gate 	 * so we need to call the nfsauth service in the
3900Sstevel@tonic-gate 	 * mount daemon.
3910Sstevel@tonic-gate 	 */
3922140Srmesta retry:
3932140Srmesta 	mutex_enter(&mountd_lock);
3942140Srmesta 	dh = mountd_dh;
3952140Srmesta 	if (dh)
3962140Srmesta 		door_ki_hold(dh);
3972140Srmesta 	mutex_exit(&mountd_lock);
3980Sstevel@tonic-gate 
3992140Srmesta 	if (dh == NULL) {
4002140Srmesta 		/*
4012140Srmesta 		 * The rendezvous point has not been established yet !
4022140Srmesta 		 * This could mean that either mountd(1m) has not yet
4032140Srmesta 		 * been started or that _this_ routine nuked the door
4042140Srmesta 		 * handle after receiving an EINTR for a REVOKED door.
4052140Srmesta 		 *
4062140Srmesta 		 * Returning NFSAUTH_DROP will cause the NFS client
4072140Srmesta 		 * to retransmit the request, so let's try to be more
4082140Srmesta 		 * rescillient and attempt for ntries before we bail.
4092140Srmesta 		 */
4102140Srmesta 		if (++ntries % NFSAUTH_DR_TRYCNT) {
4112140Srmesta 			delay(hz);
4122140Srmesta 			goto retry;
4132140Srmesta 		}
41412081SThomas.Haynes@Sun.COM 
4152140Srmesta 		sys_log("nfsauth: mountd has not established door");
41612081SThomas.Haynes@Sun.COM 		*access = NFSAUTH_DROP;
41712081SThomas.Haynes@Sun.COM 		return (FALSE);
4180Sstevel@tonic-gate 	}
41912081SThomas.Haynes@Sun.COM 
4202140Srmesta 	ntries = 0;
4212140Srmesta 	varg.vers = V_PROTO;
4222140Srmesta 	varg.arg_u.arg.cmd = NFSAUTH_ACCESS;
42312081SThomas.Haynes@Sun.COM 	varg.arg_u.arg.areq.req_client.n_len = addr->len;
42412081SThomas.Haynes@Sun.COM 	varg.arg_u.arg.areq.req_client.n_bytes = addr->buf;
42512081SThomas.Haynes@Sun.COM 	varg.arg_u.arg.areq.req_netid = req_netid;
4262140Srmesta 	varg.arg_u.arg.areq.req_path = exi->exi_export.ex_path;
4272140Srmesta 	varg.arg_u.arg.areq.req_flavor = flavor;
4280Sstevel@tonic-gate 
4292140Srmesta 	/*
4302140Srmesta 	 * Setup the XDR stream for encoding the arguments. Notice that
4312140Srmesta 	 * in addition to the args having variable fields (req_netid and
4322140Srmesta 	 * req_path), the argument data structure is itself versioned,
4332140Srmesta 	 * so we need to make sure we can size the arguments buffer
4342140Srmesta 	 * appropriately to encode all the args. If we can't get sizing
4352140Srmesta 	 * info _or_ properly encode the arguments, there's really no
4362140Srmesta 	 * point in continuting, so we fail the request.
4372140Srmesta 	 */
4382140Srmesta 	DTRACE_PROBE1(nfsserv__func__nfsauth__varg, varg_t *, &varg);
4392140Srmesta 	if ((absz = xdr_sizeof(xdr_varg, (void *)&varg)) == 0) {
4402140Srmesta 		door_ki_rele(dh);
44112081SThomas.Haynes@Sun.COM 		*access = NFSAUTH_DENIED;
44212081SThomas.Haynes@Sun.COM 		return (FALSE);
4432140Srmesta 	}
44412081SThomas.Haynes@Sun.COM 
4452140Srmesta 	abuf = (caddr_t)kmem_alloc(absz, KM_SLEEP);
4462140Srmesta 	xdrmem_create(&xdrs_a, abuf, absz, XDR_ENCODE);
4472140Srmesta 	if (!xdr_varg(&xdrs_a, &varg)) {
4482140Srmesta 		door_ki_rele(dh);
4492140Srmesta 		goto fail;
4502140Srmesta 	}
4512140Srmesta 	XDR_DESTROY(&xdrs_a);
4520Sstevel@tonic-gate 
4532140Srmesta 	/*
4542140Srmesta 	 * The result (nfsauth_res_t) is always two int's, so we don't
4552140Srmesta 	 * have to dynamically size (or allocate) the results buffer.
4562140Srmesta 	 * Now that we've got what we need, we prep the door arguments
4572140Srmesta 	 * and place the call.
4582140Srmesta 	 */
4592140Srmesta 	da.data_ptr = (char *)abuf;
4602140Srmesta 	da.data_size = absz;
4612140Srmesta 	da.desc_ptr = NULL;
4622140Srmesta 	da.desc_num = 0;
4632140Srmesta 	da.rbuf = (char *)rbuf;
4642140Srmesta 	da.rsize = rbsz;
4650Sstevel@tonic-gate 
4666997Sjwadams 	switch (door_ki_upcall_limited(dh, &da, NULL, SIZE_MAX, 0)) {
4672140Srmesta 		case 0:				/* Success */
4682140Srmesta 			if (da.data_ptr != da.rbuf && da.data_size == 0) {
4692140Srmesta 				/*
4702140Srmesta 				 * The door_return that contained the data
4712140Srmesta 				 * failed ! We're here because of the 2nd
4722140Srmesta 				 * door_return (w/o data) such that we can
4732140Srmesta 				 * get control of the thread (and exit
4742140Srmesta 				 * gracefully).
4752140Srmesta 				 */
4762140Srmesta 				DTRACE_PROBE1(nfsserv__func__nfsauth__door__nil,
4772140Srmesta 				    door_arg_t *, &da);
4782140Srmesta 				door_ki_rele(dh);
4792140Srmesta 				goto fail;
4802140Srmesta 
4812140Srmesta 			} else if (rbuf != da.rbuf) {
4822140Srmesta 				/*
4832140Srmesta 				 * The only time this should be true
4842140Srmesta 				 * is iff userland wanted to hand us
4852140Srmesta 				 * a bigger response than what we
4862140Srmesta 				 * expect; that should not happen
4872140Srmesta 				 * (nfsauth_res_t is only 2 int's),
4882140Srmesta 				 * but we check nevertheless.
4892140Srmesta 				 */
4902140Srmesta 				rbuf = da.rbuf;
4912140Srmesta 				rbsz = da.rsize;
4920Sstevel@tonic-gate 
4932140Srmesta 			} else if (rbsz > da.data_size) {
4942140Srmesta 				/*
4952140Srmesta 				 * We were expecting two int's; but if
4962140Srmesta 				 * userland fails in encoding the XDR
4972140Srmesta 				 * stream, we detect that here, since
4982140Srmesta 				 * the mountd forces down only one byte
4992140Srmesta 				 * in such scenario.
5002140Srmesta 				 */
5012140Srmesta 				door_ki_rele(dh);
5022140Srmesta 				goto fail;
5032140Srmesta 			}
5042140Srmesta 			door_ki_rele(dh);
5052140Srmesta 			break;
5062140Srmesta 
5072140Srmesta 		case EAGAIN:
5082140Srmesta 			/*
5092140Srmesta 			 * Server out of resources; back off for a bit
5102140Srmesta 			 */
5112140Srmesta 			door_ki_rele(dh);
5122140Srmesta 			kmem_free(abuf, absz);
5132140Srmesta 			delay(hz);
5142140Srmesta 			goto retry;
5152140Srmesta 			/* NOTREACHED */
5160Sstevel@tonic-gate 
5172140Srmesta 		case EINTR:
5182140Srmesta 			if (!door_ki_info(dh, &di)) {
5192140Srmesta 				if (di.di_attributes & DOOR_REVOKED) {
5202140Srmesta 					/*
5212140Srmesta 					 * The server barfed and revoked
5222140Srmesta 					 * the (existing) door on us; we
5232140Srmesta 					 * want to wait to give smf(5) a
5242140Srmesta 					 * chance to restart mountd(1m)
5252140Srmesta 					 * and establish a new door handle.
5262140Srmesta 					 */
5272140Srmesta 					mutex_enter(&mountd_lock);
5282140Srmesta 					if (dh == mountd_dh)
5292140Srmesta 						mountd_dh = NULL;
5302140Srmesta 					mutex_exit(&mountd_lock);
5312140Srmesta 					door_ki_rele(dh);
5322140Srmesta 					kmem_free(abuf, absz);
5332140Srmesta 					delay(hz);
5342140Srmesta 					goto retry;
5352140Srmesta 				}
5362140Srmesta 				/*
5372140Srmesta 				 * If the door was _not_ revoked on us,
5382140Srmesta 				 * then more than likely we took an INTR,
5392140Srmesta 				 * so we need to fail the operation.
5402140Srmesta 				 */
5412140Srmesta 				door_ki_rele(dh);
5422140Srmesta 				goto fail;
5432140Srmesta 			}
5442140Srmesta 			/*
5452140Srmesta 			 * The only failure that can occur from getting
5462140Srmesta 			 * the door info is EINVAL, so we let the code
5472140Srmesta 			 * below handle it.
5482140Srmesta 			 */
5492140Srmesta 			/* FALLTHROUGH */
5502140Srmesta 
5512140Srmesta 		case EBADF:
5522140Srmesta 		case EINVAL:
5532140Srmesta 		default:
5542140Srmesta 			/*
5552140Srmesta 			 * If we have a stale door handle, give smf a last
5562140Srmesta 			 * chance to start it by sleeping for a little bit.
5572140Srmesta 			 * If we're still hosed, we'll fail the call.
5582140Srmesta 			 *
5592140Srmesta 			 * Since we're going to reacquire the door handle
5602140Srmesta 			 * upon the retry, we opt to sleep for a bit and
5612140Srmesta 			 * _not_ to clear mountd_dh. If mountd restarted
5622140Srmesta 			 * and was able to set mountd_dh, we should see
5632140Srmesta 			 * the new instance; if not, we won't get caught
5642140Srmesta 			 * up in the retry/DELAY loop.
5652140Srmesta 			 */
5662140Srmesta 			door_ki_rele(dh);
5672140Srmesta 			if (!last) {
5682140Srmesta 				delay(hz);
5692140Srmesta 				last++;
5702140Srmesta 				goto retry;
5712140Srmesta 			}
5722140Srmesta 			sys_log("nfsauth: stale mountd door handle");
5732140Srmesta 			goto fail;
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5762140Srmesta 	/*
5772140Srmesta 	 * No door errors encountered; setup the XDR stream for decoding
5782140Srmesta 	 * the results. If we fail to decode the results, we've got no
5792140Srmesta 	 * other recourse than to fail the request.
5802140Srmesta 	 */
5812140Srmesta 	xdrmem_create(&xdrs_r, rbuf, rbsz, XDR_DECODE);
5822140Srmesta 	if (!xdr_nfsauth_res(&xdrs_r, &res))
5832140Srmesta 		goto fail;
5842140Srmesta 	XDR_DESTROY(&xdrs_r);
5852140Srmesta 
5862140Srmesta 	DTRACE_PROBE1(nfsserv__func__nfsauth__results, nfsauth_res_t *, &res);
5872140Srmesta 	switch (res.stat) {
5882140Srmesta 		case NFSAUTH_DR_OKAY:
58912081SThomas.Haynes@Sun.COM 			*access = res.ares.auth_perm;
5902140Srmesta 			kmem_free(abuf, absz);
5912140Srmesta 			break;
5922140Srmesta 
5932140Srmesta 		case NFSAUTH_DR_EFAIL:
5942140Srmesta 		case NFSAUTH_DR_DECERR:
5952140Srmesta 		case NFSAUTH_DR_BADCMD:
5962140Srmesta 		default:
5972140Srmesta fail:
59812081SThomas.Haynes@Sun.COM 			*access = NFSAUTH_DENIED;
5992140Srmesta 			kmem_free(abuf, absz);
60012081SThomas.Haynes@Sun.COM 			return (FALSE);
6012140Srmesta 			/* NOTREACHED */
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 
60412081SThomas.Haynes@Sun.COM 	return (TRUE);
60512081SThomas.Haynes@Sun.COM }
60612081SThomas.Haynes@Sun.COM 
60712081SThomas.Haynes@Sun.COM static void
nfsauth_refresh_thread(void)60812081SThomas.Haynes@Sun.COM nfsauth_refresh_thread(void)
60912081SThomas.Haynes@Sun.COM {
61012081SThomas.Haynes@Sun.COM 	refreshq_exi_node_t	*ren;
61112081SThomas.Haynes@Sun.COM 	refreshq_auth_node_t	*ran;
61212081SThomas.Haynes@Sun.COM 
61312081SThomas.Haynes@Sun.COM 	struct exportinfo	*exi;
61412081SThomas.Haynes@Sun.COM 	struct auth_cache	*p;
61512081SThomas.Haynes@Sun.COM 
61612081SThomas.Haynes@Sun.COM 	int			access;
61712081SThomas.Haynes@Sun.COM 	bool_t			retrieval;
61812081SThomas.Haynes@Sun.COM 
61912081SThomas.Haynes@Sun.COM 	callb_cpr_t		cprinfo;
62012081SThomas.Haynes@Sun.COM 
62112081SThomas.Haynes@Sun.COM 	CALLB_CPR_INIT(&cprinfo, &refreshq_lock, callb_generic_cpr,
62212081SThomas.Haynes@Sun.COM 	    "nfsauth_refresh");
62312081SThomas.Haynes@Sun.COM 
62412081SThomas.Haynes@Sun.COM 	for (;;) {
62512081SThomas.Haynes@Sun.COM 		mutex_enter(&refreshq_lock);
62612081SThomas.Haynes@Sun.COM 		if (refreshq_thread_state != REFRESHQ_THREAD_RUNNING) {
62712081SThomas.Haynes@Sun.COM 			/* Keep the hold on the lock! */
62812081SThomas.Haynes@Sun.COM 			break;
62912081SThomas.Haynes@Sun.COM 		}
63012081SThomas.Haynes@Sun.COM 
63112081SThomas.Haynes@Sun.COM 		ren = list_remove_head(&refreshq_queue);
63212081SThomas.Haynes@Sun.COM 		if (ren == NULL) {
63312081SThomas.Haynes@Sun.COM 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
63412081SThomas.Haynes@Sun.COM 			cv_wait(&refreshq_cv, &refreshq_lock);
63512081SThomas.Haynes@Sun.COM 			CALLB_CPR_SAFE_END(&cprinfo, &refreshq_lock);
63612081SThomas.Haynes@Sun.COM 			mutex_exit(&refreshq_lock);
63712081SThomas.Haynes@Sun.COM 			continue;
63812081SThomas.Haynes@Sun.COM 		}
63912081SThomas.Haynes@Sun.COM 		mutex_exit(&refreshq_lock);
64012081SThomas.Haynes@Sun.COM 
64112081SThomas.Haynes@Sun.COM 		exi = ren->ren_exi;
64212081SThomas.Haynes@Sun.COM 		ASSERT(exi != NULL);
64312081SThomas.Haynes@Sun.COM 		rw_enter(&exi->exi_cache_lock, RW_READER);
64412081SThomas.Haynes@Sun.COM 
64512081SThomas.Haynes@Sun.COM 		while ((ran = list_remove_head(&ren->ren_authlist))) {
64612081SThomas.Haynes@Sun.COM 			/*
64712081SThomas.Haynes@Sun.COM 			 * We are shutting down. No need to refresh
64812081SThomas.Haynes@Sun.COM 			 * entries which are about to be nuked.
64912081SThomas.Haynes@Sun.COM 			 *
65012081SThomas.Haynes@Sun.COM 			 * So just throw them away until we are done
65112081SThomas.Haynes@Sun.COM 			 * with this exi node...
65212081SThomas.Haynes@Sun.COM 			 */
65312081SThomas.Haynes@Sun.COM 			if (refreshq_thread_state !=
65412081SThomas.Haynes@Sun.COM 			    REFRESHQ_THREAD_RUNNING) {
65512081SThomas.Haynes@Sun.COM 				kmem_free(ran, sizeof (refreshq_auth_node_t));
65612081SThomas.Haynes@Sun.COM 				continue;
65712081SThomas.Haynes@Sun.COM 			}
65812081SThomas.Haynes@Sun.COM 
65912081SThomas.Haynes@Sun.COM 			p = ran->ran_auth;
66012081SThomas.Haynes@Sun.COM 			ASSERT(p != NULL);
66112081SThomas.Haynes@Sun.COM 
66212081SThomas.Haynes@Sun.COM 			mutex_enter(&p->auth_lock);
66312081SThomas.Haynes@Sun.COM 
66412081SThomas.Haynes@Sun.COM 			/*
66512081SThomas.Haynes@Sun.COM 			 * Make sure the state is valid now that
66612081SThomas.Haynes@Sun.COM 			 * we have the lock. Note that once we
66712081SThomas.Haynes@Sun.COM 			 * change the state to NFS_AUTH_REFRESHING,
66812081SThomas.Haynes@Sun.COM 			 * no other thread will be able to work on
66912081SThomas.Haynes@Sun.COM 			 * this entry.
67012081SThomas.Haynes@Sun.COM 			 */
67112081SThomas.Haynes@Sun.COM 			if (p->auth_state != NFS_AUTH_STALE) {
67212081SThomas.Haynes@Sun.COM 				/*
67312081SThomas.Haynes@Sun.COM 				 * Once it goes INVALID, it can not
67412081SThomas.Haynes@Sun.COM 				 * change state.
67512081SThomas.Haynes@Sun.COM 				 */
67612081SThomas.Haynes@Sun.COM 				if (p->auth_state == NFS_AUTH_INVALID) {
67712081SThomas.Haynes@Sun.COM 					mutex_exit(&p->auth_lock);
67812081SThomas.Haynes@Sun.COM 					nfsauth_remove_dead_entry(p);
67912081SThomas.Haynes@Sun.COM 				} else
68012081SThomas.Haynes@Sun.COM 					mutex_exit(&p->auth_lock);
68112081SThomas.Haynes@Sun.COM 
68212081SThomas.Haynes@Sun.COM 				kmem_free(ran, sizeof (refreshq_auth_node_t));
68312081SThomas.Haynes@Sun.COM 				continue;
68412081SThomas.Haynes@Sun.COM 			}
68512081SThomas.Haynes@Sun.COM 
68612081SThomas.Haynes@Sun.COM 			p->auth_state = NFS_AUTH_REFRESHING;
68712081SThomas.Haynes@Sun.COM 			mutex_exit(&p->auth_lock);
68812081SThomas.Haynes@Sun.COM 
68912081SThomas.Haynes@Sun.COM 			DTRACE_PROBE2(nfsauth__debug__cache__refresh,
69012081SThomas.Haynes@Sun.COM 			    struct exportinfo *, exi,
69112081SThomas.Haynes@Sun.COM 			    struct auth_cache *, p);
69212081SThomas.Haynes@Sun.COM 
69312081SThomas.Haynes@Sun.COM 			/*
69412081SThomas.Haynes@Sun.COM 			 * The first caching of the access rights
69512081SThomas.Haynes@Sun.COM 			 * is done with the netid pulled out of the
69612081SThomas.Haynes@Sun.COM 			 * request from the client. All subsequent
69712081SThomas.Haynes@Sun.COM 			 * users of the cache may or may not have
69812081SThomas.Haynes@Sun.COM 			 * the same netid. It doesn't matter. So
69912081SThomas.Haynes@Sun.COM 			 * when we refresh, we simply use the netid
70012081SThomas.Haynes@Sun.COM 			 * of the request which triggered the
70112081SThomas.Haynes@Sun.COM 			 * refresh attempt.
70212081SThomas.Haynes@Sun.COM 			 */
70312081SThomas.Haynes@Sun.COM 			ASSERT(p->auth_netid != NULL);
70412081SThomas.Haynes@Sun.COM 
70512081SThomas.Haynes@Sun.COM 			retrieval = nfsauth_retrieve(exi, p->auth_netid,
70612081SThomas.Haynes@Sun.COM 			    p->auth_flavor, &p->auth_addr, &access);
70712081SThomas.Haynes@Sun.COM 
70812081SThomas.Haynes@Sun.COM 			/*
70912081SThomas.Haynes@Sun.COM 			 * This can only be set in one other place
71012081SThomas.Haynes@Sun.COM 			 * and the state has to be NFS_AUTH_FRESH.
71112081SThomas.Haynes@Sun.COM 			 */
71212081SThomas.Haynes@Sun.COM 			kmem_free(p->auth_netid, strlen(p->auth_netid) + 1);
71312081SThomas.Haynes@Sun.COM 			p->auth_netid = NULL;
71412081SThomas.Haynes@Sun.COM 
71512081SThomas.Haynes@Sun.COM 			/*
71612081SThomas.Haynes@Sun.COM 			 * We got an error, so do not reset the
71712081SThomas.Haynes@Sun.COM 			 * time. This will cause the next access
71812081SThomas.Haynes@Sun.COM 			 * check for the client to reschedule this
71912081SThomas.Haynes@Sun.COM 			 * node.
72012081SThomas.Haynes@Sun.COM 			 */
72112081SThomas.Haynes@Sun.COM 			if (retrieval == FALSE) {
72212081SThomas.Haynes@Sun.COM 				mutex_enter(&p->auth_lock);
72312081SThomas.Haynes@Sun.COM 				if (p->auth_state == NFS_AUTH_INVALID) {
72412081SThomas.Haynes@Sun.COM 					mutex_exit(&p->auth_lock);
72512081SThomas.Haynes@Sun.COM 					nfsauth_remove_dead_entry(p);
72612081SThomas.Haynes@Sun.COM 				} else {
72712081SThomas.Haynes@Sun.COM 					p->auth_state = NFS_AUTH_FRESH;
72812081SThomas.Haynes@Sun.COM 					mutex_exit(&p->auth_lock);
72912081SThomas.Haynes@Sun.COM 				}
73012081SThomas.Haynes@Sun.COM 
73112081SThomas.Haynes@Sun.COM 				kmem_free(ran, sizeof (refreshq_auth_node_t));
73212081SThomas.Haynes@Sun.COM 				continue;
73312081SThomas.Haynes@Sun.COM 			}
73412081SThomas.Haynes@Sun.COM 
73512081SThomas.Haynes@Sun.COM 			mutex_enter(&p->auth_lock);
73612081SThomas.Haynes@Sun.COM 			if (p->auth_state == NFS_AUTH_INVALID) {
73712081SThomas.Haynes@Sun.COM 				mutex_exit(&p->auth_lock);
73812081SThomas.Haynes@Sun.COM 				nfsauth_remove_dead_entry(p);
73912081SThomas.Haynes@Sun.COM 			} else {
74012081SThomas.Haynes@Sun.COM 				p->auth_access = access;
74112081SThomas.Haynes@Sun.COM 				p->auth_freshness = gethrestime_sec();
74212081SThomas.Haynes@Sun.COM 				p->auth_state = NFS_AUTH_FRESH;
74312081SThomas.Haynes@Sun.COM 				mutex_exit(&p->auth_lock);
74412081SThomas.Haynes@Sun.COM 			}
74512081SThomas.Haynes@Sun.COM 
74612081SThomas.Haynes@Sun.COM 			kmem_free(ran, sizeof (refreshq_auth_node_t));
74712081SThomas.Haynes@Sun.COM 		}
74812081SThomas.Haynes@Sun.COM 
74912081SThomas.Haynes@Sun.COM 		rw_exit(&exi->exi_cache_lock);
75012081SThomas.Haynes@Sun.COM 
75112081SThomas.Haynes@Sun.COM 		list_destroy(&ren->ren_authlist);
75212081SThomas.Haynes@Sun.COM 		exi_rele(ren->ren_exi);
75312081SThomas.Haynes@Sun.COM 		kmem_free(ren, sizeof (refreshq_exi_node_t));
75412081SThomas.Haynes@Sun.COM 	}
75512081SThomas.Haynes@Sun.COM 
75612081SThomas.Haynes@Sun.COM 	refreshq_thread_state = REFRESHQ_THREAD_HALTED;
75712081SThomas.Haynes@Sun.COM 	cv_broadcast(&refreshq_cv);
75812081SThomas.Haynes@Sun.COM 	CALLB_CPR_EXIT(&cprinfo);
75912081SThomas.Haynes@Sun.COM 	zthread_exit();
76012081SThomas.Haynes@Sun.COM }
76112081SThomas.Haynes@Sun.COM 
76212081SThomas.Haynes@Sun.COM /*
76312081SThomas.Haynes@Sun.COM  * Get the access information from the cache or callup to the mountd
76412081SThomas.Haynes@Sun.COM  * to get and cache the access information in the kernel.
76512081SThomas.Haynes@Sun.COM  */
76612081SThomas.Haynes@Sun.COM int
nfsauth_cache_get(struct exportinfo * exi,struct svc_req * req,int flavor)76712081SThomas.Haynes@Sun.COM nfsauth_cache_get(struct exportinfo *exi, struct svc_req *req, int flavor)
76812081SThomas.Haynes@Sun.COM {
76912553SKaren.Rochford@Sun.COM 	struct netbuf		*taddrmask;
77012081SThomas.Haynes@Sun.COM 	struct netbuf		addr;
77112081SThomas.Haynes@Sun.COM 	struct netbuf		*claddr;
77212081SThomas.Haynes@Sun.COM 	struct auth_cache	**head;
77312081SThomas.Haynes@Sun.COM 	struct auth_cache	*p;
77412081SThomas.Haynes@Sun.COM 	int			access;
77512081SThomas.Haynes@Sun.COM 	time_t			refresh;
77612081SThomas.Haynes@Sun.COM 
77712081SThomas.Haynes@Sun.COM 	refreshq_exi_node_t	*ren;
77812081SThomas.Haynes@Sun.COM 	refreshq_auth_node_t	*ran;
77912081SThomas.Haynes@Sun.COM 
78012081SThomas.Haynes@Sun.COM 	/*
78112081SThomas.Haynes@Sun.COM 	 * Now check whether this client already
78212081SThomas.Haynes@Sun.COM 	 * has an entry for this flavor in the cache
78312081SThomas.Haynes@Sun.COM 	 * for this export.
78412081SThomas.Haynes@Sun.COM 	 * Get the caller's address, mask off the
78512081SThomas.Haynes@Sun.COM 	 * parts of the address that do not identify
78612081SThomas.Haynes@Sun.COM 	 * the host (port number, etc), and then hash
78712081SThomas.Haynes@Sun.COM 	 * it to find the chain of cache entries.
78812081SThomas.Haynes@Sun.COM 	 */
78912081SThomas.Haynes@Sun.COM 
79012081SThomas.Haynes@Sun.COM 	claddr = svc_getrpccaller(req->rq_xprt);
79112081SThomas.Haynes@Sun.COM 	addr = *claddr;
79212081SThomas.Haynes@Sun.COM 	addr.buf = kmem_alloc(addr.len, KM_SLEEP);
79312081SThomas.Haynes@Sun.COM 	bcopy(claddr->buf, addr.buf, claddr->len);
79412553SKaren.Rochford@Sun.COM 	SVC_GETADDRMASK(req->rq_xprt, SVC_TATTR_ADDRMASK, (void **)&taddrmask);
79512553SKaren.Rochford@Sun.COM 	ASSERT(taddrmask != NULL);
79612553SKaren.Rochford@Sun.COM 	if (taddrmask)
79712553SKaren.Rochford@Sun.COM 		addrmask(&addr, taddrmask);
79812081SThomas.Haynes@Sun.COM 
79912081SThomas.Haynes@Sun.COM 	rw_enter(&exi->exi_cache_lock, RW_READER);
80012081SThomas.Haynes@Sun.COM 	head = &exi->exi_cache[hash(&addr)];
80112081SThomas.Haynes@Sun.COM 	for (p = *head; p; p = p->auth_next) {
80212081SThomas.Haynes@Sun.COM 		if (EQADDR(&addr, &p->auth_addr) && flavor == p->auth_flavor)
80312081SThomas.Haynes@Sun.COM 			break;
80412081SThomas.Haynes@Sun.COM 	}
80512081SThomas.Haynes@Sun.COM 
80612081SThomas.Haynes@Sun.COM 	if (p != NULL) {
80712081SThomas.Haynes@Sun.COM 		nfsauth_cache_hit++;
80812081SThomas.Haynes@Sun.COM 
80912081SThomas.Haynes@Sun.COM 		refresh = gethrestime_sec() - p->auth_freshness;
81012081SThomas.Haynes@Sun.COM 		DTRACE_PROBE2(nfsauth__debug__cache__hit,
81112081SThomas.Haynes@Sun.COM 		    int, nfsauth_cache_hit,
81212081SThomas.Haynes@Sun.COM 		    time_t, refresh);
81312081SThomas.Haynes@Sun.COM 
81412081SThomas.Haynes@Sun.COM 		mutex_enter(&p->auth_lock);
81512081SThomas.Haynes@Sun.COM 		if ((refresh > NFSAUTH_CACHE_REFRESH) &&
81612081SThomas.Haynes@Sun.COM 		    p->auth_state == NFS_AUTH_FRESH) {
81712081SThomas.Haynes@Sun.COM 			p->auth_state = NFS_AUTH_STALE;
81812081SThomas.Haynes@Sun.COM 			mutex_exit(&p->auth_lock);
81912081SThomas.Haynes@Sun.COM 
82012081SThomas.Haynes@Sun.COM 			ASSERT(p->auth_netid == NULL);
82112081SThomas.Haynes@Sun.COM 			p->auth_netid =
82212081SThomas.Haynes@Sun.COM 			    strdup(svc_getnetid(req->rq_xprt));
82312081SThomas.Haynes@Sun.COM 
82412081SThomas.Haynes@Sun.COM 			nfsauth_cache_refresh++;
82512081SThomas.Haynes@Sun.COM 
82612081SThomas.Haynes@Sun.COM 			DTRACE_PROBE3(nfsauth__debug__cache__stale,
82712081SThomas.Haynes@Sun.COM 			    struct exportinfo *, exi,
82812081SThomas.Haynes@Sun.COM 			    struct auth_cache *, p,
82912081SThomas.Haynes@Sun.COM 			    int, nfsauth_cache_refresh);
83012081SThomas.Haynes@Sun.COM 
83112081SThomas.Haynes@Sun.COM 			ran = kmem_alloc(sizeof (refreshq_auth_node_t),
83212081SThomas.Haynes@Sun.COM 			    KM_SLEEP);
83312081SThomas.Haynes@Sun.COM 			ran->ran_auth = p;
83412081SThomas.Haynes@Sun.COM 
83512081SThomas.Haynes@Sun.COM 			mutex_enter(&refreshq_lock);
83612081SThomas.Haynes@Sun.COM 			/*
83712081SThomas.Haynes@Sun.COM 			 * We should not add a work queue
83812081SThomas.Haynes@Sun.COM 			 * item if the thread is not
83912081SThomas.Haynes@Sun.COM 			 * accepting them.
84012081SThomas.Haynes@Sun.COM 			 */
84112081SThomas.Haynes@Sun.COM 			if (refreshq_thread_state == REFRESHQ_THREAD_RUNNING) {
84212081SThomas.Haynes@Sun.COM 				/*
84312081SThomas.Haynes@Sun.COM 				 * Is there an existing exi_list?
84412081SThomas.Haynes@Sun.COM 				 */
84512081SThomas.Haynes@Sun.COM 				for (ren = list_head(&refreshq_queue);
84612081SThomas.Haynes@Sun.COM 				    ren != NULL;
84712081SThomas.Haynes@Sun.COM 				    ren = list_next(&refreshq_queue, ren)) {
84812081SThomas.Haynes@Sun.COM 					if (ren->ren_exi == exi) {
84912081SThomas.Haynes@Sun.COM 						list_insert_tail(
85012081SThomas.Haynes@Sun.COM 						    &ren->ren_authlist, ran);
85112081SThomas.Haynes@Sun.COM 						break;
85212081SThomas.Haynes@Sun.COM 					}
85312081SThomas.Haynes@Sun.COM 				}
85412081SThomas.Haynes@Sun.COM 
85512081SThomas.Haynes@Sun.COM 				if (ren == NULL) {
85612081SThomas.Haynes@Sun.COM 					ren = kmem_alloc(
85712081SThomas.Haynes@Sun.COM 					    sizeof (refreshq_exi_node_t),
85812081SThomas.Haynes@Sun.COM 					    KM_SLEEP);
85912081SThomas.Haynes@Sun.COM 
86012081SThomas.Haynes@Sun.COM 					exi_hold(exi);
86112081SThomas.Haynes@Sun.COM 					ren->ren_exi = exi;
86212081SThomas.Haynes@Sun.COM 
86312081SThomas.Haynes@Sun.COM 					list_create(&ren->ren_authlist,
86412081SThomas.Haynes@Sun.COM 					    sizeof (refreshq_auth_node_t),
86512081SThomas.Haynes@Sun.COM 					    offsetof(refreshq_auth_node_t,
86612081SThomas.Haynes@Sun.COM 					    ran_node));
86712081SThomas.Haynes@Sun.COM 
86812081SThomas.Haynes@Sun.COM 					list_insert_tail(&ren->ren_authlist,
86912081SThomas.Haynes@Sun.COM 					    ran);
87012081SThomas.Haynes@Sun.COM 					list_insert_tail(&refreshq_queue, ren);
87112081SThomas.Haynes@Sun.COM 				}
87212081SThomas.Haynes@Sun.COM 
87312081SThomas.Haynes@Sun.COM 				cv_broadcast(&refreshq_cv);
87412081SThomas.Haynes@Sun.COM 			} else {
87512081SThomas.Haynes@Sun.COM 				kmem_free(ran, sizeof (refreshq_auth_node_t));
87612081SThomas.Haynes@Sun.COM 			}
87712081SThomas.Haynes@Sun.COM 
87812081SThomas.Haynes@Sun.COM 			mutex_exit(&refreshq_lock);
87912081SThomas.Haynes@Sun.COM 		} else {
88012081SThomas.Haynes@Sun.COM 			mutex_exit(&p->auth_lock);
88112081SThomas.Haynes@Sun.COM 		}
88212081SThomas.Haynes@Sun.COM 
88312081SThomas.Haynes@Sun.COM 		access = p->auth_access;
88412081SThomas.Haynes@Sun.COM 		p->auth_time = gethrestime_sec();
88512081SThomas.Haynes@Sun.COM 
88612081SThomas.Haynes@Sun.COM 		rw_exit(&exi->exi_cache_lock);
88712081SThomas.Haynes@Sun.COM 		kmem_free(addr.buf, addr.len);
88812081SThomas.Haynes@Sun.COM 
88912081SThomas.Haynes@Sun.COM 		return (access);
89012081SThomas.Haynes@Sun.COM 	}
89112081SThomas.Haynes@Sun.COM 
89212081SThomas.Haynes@Sun.COM 	rw_exit(&exi->exi_cache_lock);
89312081SThomas.Haynes@Sun.COM 
89412081SThomas.Haynes@Sun.COM 	nfsauth_cache_miss++;
89512081SThomas.Haynes@Sun.COM 
89612081SThomas.Haynes@Sun.COM 	if (!nfsauth_retrieve(exi, svc_getnetid(req->rq_xprt), flavor,
89712081SThomas.Haynes@Sun.COM 	    &addr, &access)) {
89812081SThomas.Haynes@Sun.COM 		kmem_free(addr.buf, addr.len);
89912081SThomas.Haynes@Sun.COM 		return (access);
90012081SThomas.Haynes@Sun.COM 	}
90112081SThomas.Haynes@Sun.COM 
9020Sstevel@tonic-gate 	/*
9030Sstevel@tonic-gate 	 * Now cache the result on the cache chain
9040Sstevel@tonic-gate 	 * for this export (if there's enough memory)
9050Sstevel@tonic-gate 	 */
90612081SThomas.Haynes@Sun.COM 	p = kmem_cache_alloc(exi_cache_handle, KM_NOSLEEP);
90712081SThomas.Haynes@Sun.COM 	if (p != NULL) {
90812081SThomas.Haynes@Sun.COM 		p->auth_addr = addr;
90912081SThomas.Haynes@Sun.COM 		p->auth_flavor = flavor;
91012081SThomas.Haynes@Sun.COM 		p->auth_access = access;
91112081SThomas.Haynes@Sun.COM 		p->auth_time = p->auth_freshness = gethrestime_sec();
91212081SThomas.Haynes@Sun.COM 		p->auth_state = NFS_AUTH_FRESH;
91312081SThomas.Haynes@Sun.COM 		p->auth_netid = NULL;
91412081SThomas.Haynes@Sun.COM 		mutex_init(&p->auth_lock, NULL, MUTEX_DEFAULT, NULL);
91512081SThomas.Haynes@Sun.COM 
9160Sstevel@tonic-gate 		rw_enter(&exi->exi_cache_lock, RW_WRITER);
91712081SThomas.Haynes@Sun.COM 		p->auth_next = *head;
91812081SThomas.Haynes@Sun.COM 		*head = p;
9190Sstevel@tonic-gate 		rw_exit(&exi->exi_cache_lock);
9200Sstevel@tonic-gate 	} else {
9210Sstevel@tonic-gate 		kmem_free(addr.buf, addr.len);
9220Sstevel@tonic-gate 	}
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	return (access);
9250Sstevel@tonic-gate }
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate /*
9280Sstevel@tonic-gate  * Check if the requesting client has access to the filesystem with
9290Sstevel@tonic-gate  * a given nfs flavor number which is an explicitly shared flavor.
9300Sstevel@tonic-gate  */
9310Sstevel@tonic-gate int
nfsauth4_secinfo_access(struct exportinfo * exi,struct svc_req * req,int flavor,int perm)9320Sstevel@tonic-gate nfsauth4_secinfo_access(struct exportinfo *exi, struct svc_req *req,
9330Sstevel@tonic-gate 			int flavor, int perm)
9340Sstevel@tonic-gate {
9350Sstevel@tonic-gate 	int access;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	if (! (perm & M_4SEC_EXPORTED)) {
9380Sstevel@tonic-gate 		return (NFSAUTH_DENIED);
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	/*
9420Sstevel@tonic-gate 	 * Optimize if there are no lists
9430Sstevel@tonic-gate 	 */
9447961SNatalie.Li@Sun.COM 	if ((perm & (M_ROOT|M_NONE)) == 0) {
9450Sstevel@tonic-gate 		perm &= ~M_4SEC_EXPORTED;
9460Sstevel@tonic-gate 		if (perm == M_RO)
9470Sstevel@tonic-gate 			return (NFSAUTH_RO);
9480Sstevel@tonic-gate 		if (perm == M_RW)
9490Sstevel@tonic-gate 			return (NFSAUTH_RW);
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	access = nfsauth_cache_get(exi, req, flavor);
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	return (access);
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate int
nfsauth_access(struct exportinfo * exi,struct svc_req * req)9580Sstevel@tonic-gate nfsauth_access(struct exportinfo *exi, struct svc_req *req)
9590Sstevel@tonic-gate {
9600Sstevel@tonic-gate 	int access, mapaccess;
9610Sstevel@tonic-gate 	struct secinfo *sp;
9620Sstevel@tonic-gate 	int i, flavor, perm;
9630Sstevel@tonic-gate 	int authnone_entry = -1;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	/*
9660Sstevel@tonic-gate 	 *  Get the nfs flavor number from xprt.
9670Sstevel@tonic-gate 	 */
9680Sstevel@tonic-gate 	flavor = (int)(uintptr_t)req->rq_xprt->xp_cookie;
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	/*
9710Sstevel@tonic-gate 	 * First check the access restrictions on the filesystem.  If
9720Sstevel@tonic-gate 	 * there are no lists associated with this flavor then there's no
9730Sstevel@tonic-gate 	 * need to make an expensive call to the nfsauth service or to
9740Sstevel@tonic-gate 	 * cache anything.
9750Sstevel@tonic-gate 	 */
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	sp = exi->exi_export.ex_secinfo;
9780Sstevel@tonic-gate 	for (i = 0; i < exi->exi_export.ex_seccnt; i++) {
9790Sstevel@tonic-gate 		if (flavor != sp[i].s_secinfo.sc_nfsnum) {
9800Sstevel@tonic-gate 			if (sp[i].s_secinfo.sc_nfsnum == AUTH_NONE)
9810Sstevel@tonic-gate 				authnone_entry = i;
9820Sstevel@tonic-gate 			continue;
9830Sstevel@tonic-gate 		}
9840Sstevel@tonic-gate 		break;
9850Sstevel@tonic-gate 	}
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	mapaccess = 0;
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	if (i >= exi->exi_export.ex_seccnt) {
9900Sstevel@tonic-gate 		/*
9910Sstevel@tonic-gate 		 * Flavor not found, but use AUTH_NONE if it exists
9920Sstevel@tonic-gate 		 */
9930Sstevel@tonic-gate 		if (authnone_entry == -1)
9940Sstevel@tonic-gate 			return (NFSAUTH_DENIED);
9950Sstevel@tonic-gate 		flavor = AUTH_NONE;
9960Sstevel@tonic-gate 		mapaccess = NFSAUTH_MAPNONE;
9970Sstevel@tonic-gate 		i = authnone_entry;
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	/*
10010Sstevel@tonic-gate 	 * If the flavor is in the ex_secinfo list, but not an explicitly
10020Sstevel@tonic-gate 	 * shared flavor by the user, it is a result of the nfsv4 server
10030Sstevel@tonic-gate 	 * namespace setup. We will grant an RO permission similar for
10040Sstevel@tonic-gate 	 * a pseudo node except that this node is a shared one.
10050Sstevel@tonic-gate 	 *
10060Sstevel@tonic-gate 	 * e.g. flavor in (flavor) indicates that it is not explictly
10070Sstevel@tonic-gate 	 *	shared by the user:
10080Sstevel@tonic-gate 	 *
10090Sstevel@tonic-gate 	 *		/	(sys, krb5)
10100Sstevel@tonic-gate 	 *		|
10110Sstevel@tonic-gate 	 *		export  #share -o sec=sys (krb5)
10120Sstevel@tonic-gate 	 *		|
10130Sstevel@tonic-gate 	 *		secure  #share -o sec=krb5
10140Sstevel@tonic-gate 	 *
10150Sstevel@tonic-gate 	 *	In this case, when a krb5 request coming in to access
10160Sstevel@tonic-gate 	 *	/export, RO permission is granted.
10170Sstevel@tonic-gate 	 */
10180Sstevel@tonic-gate 	if (!(sp[i].s_flags & M_4SEC_EXPORTED))
10190Sstevel@tonic-gate 		return (mapaccess | NFSAUTH_RO);
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	/*
10220Sstevel@tonic-gate 	 * Optimize if there are no lists
10230Sstevel@tonic-gate 	 */
10240Sstevel@tonic-gate 	perm = sp[i].s_flags;
10257961SNatalie.Li@Sun.COM 	if ((perm & (M_ROOT|M_NONE)) == 0) {
10260Sstevel@tonic-gate 		perm &= ~M_4SEC_EXPORTED;
10270Sstevel@tonic-gate 		if (perm == M_RO)
10280Sstevel@tonic-gate 			return (mapaccess | NFSAUTH_RO);
10290Sstevel@tonic-gate 		if (perm == M_RW)
10300Sstevel@tonic-gate 			return (mapaccess | NFSAUTH_RW);
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	access = nfsauth_cache_get(exi, req, flavor);
10348984SVallish.Vaidyeshwara@Sun.COM 
10358984SVallish.Vaidyeshwara@Sun.COM 	/*
10368984SVallish.Vaidyeshwara@Sun.COM 	 * Client's security flavor doesn't match with "ro" or
10378984SVallish.Vaidyeshwara@Sun.COM 	 * "rw" list. Try again using AUTH_NONE if present.
10388984SVallish.Vaidyeshwara@Sun.COM 	 */
10398984SVallish.Vaidyeshwara@Sun.COM 	if ((access & NFSAUTH_WRONGSEC) && (flavor != AUTH_NONE)) {
10408984SVallish.Vaidyeshwara@Sun.COM 		/*
10418984SVallish.Vaidyeshwara@Sun.COM 		 * Have we already encountered AUTH_NONE ?
10428984SVallish.Vaidyeshwara@Sun.COM 		 */
10438984SVallish.Vaidyeshwara@Sun.COM 		if (authnone_entry != -1) {
10448984SVallish.Vaidyeshwara@Sun.COM 			mapaccess = NFSAUTH_MAPNONE;
10458984SVallish.Vaidyeshwara@Sun.COM 			access = nfsauth_cache_get(exi, req, AUTH_NONE);
10468984SVallish.Vaidyeshwara@Sun.COM 		} else {
10478984SVallish.Vaidyeshwara@Sun.COM 			/*
10488984SVallish.Vaidyeshwara@Sun.COM 			 * Check for AUTH_NONE presence.
10498984SVallish.Vaidyeshwara@Sun.COM 			 */
10508984SVallish.Vaidyeshwara@Sun.COM 			for (; i < exi->exi_export.ex_seccnt; i++) {
10518984SVallish.Vaidyeshwara@Sun.COM 				if (sp[i].s_secinfo.sc_nfsnum == AUTH_NONE) {
10528984SVallish.Vaidyeshwara@Sun.COM 					mapaccess = NFSAUTH_MAPNONE;
10538984SVallish.Vaidyeshwara@Sun.COM 					access = nfsauth_cache_get(exi, req,
10548984SVallish.Vaidyeshwara@Sun.COM 					    AUTH_NONE);
10558984SVallish.Vaidyeshwara@Sun.COM 					break;
10568984SVallish.Vaidyeshwara@Sun.COM 				}
10578984SVallish.Vaidyeshwara@Sun.COM 			}
10588984SVallish.Vaidyeshwara@Sun.COM 		}
10598984SVallish.Vaidyeshwara@Sun.COM 	}
10608984SVallish.Vaidyeshwara@Sun.COM 
10617961SNatalie.Li@Sun.COM 	if (access & NFSAUTH_DENIED)
10627961SNatalie.Li@Sun.COM 		access = NFSAUTH_DENIED;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	return (access | mapaccess);
10650Sstevel@tonic-gate }
10660Sstevel@tonic-gate 
106712081SThomas.Haynes@Sun.COM static void
nfsauth_free_node(struct auth_cache * p)106812081SThomas.Haynes@Sun.COM nfsauth_free_node(struct auth_cache *p)
106912081SThomas.Haynes@Sun.COM {
107012081SThomas.Haynes@Sun.COM 	if (p->auth_netid != NULL)
107112081SThomas.Haynes@Sun.COM 		kmem_free(p->auth_netid, strlen(p->auth_netid) + 1);
107212081SThomas.Haynes@Sun.COM 	kmem_free(p->auth_addr.buf, p->auth_addr.len);
107312081SThomas.Haynes@Sun.COM 	mutex_destroy(&p->auth_lock);
107412081SThomas.Haynes@Sun.COM 	kmem_cache_free(exi_cache_handle, (void *)p);
107512081SThomas.Haynes@Sun.COM }
107612081SThomas.Haynes@Sun.COM 
107712081SThomas.Haynes@Sun.COM /*
107812081SThomas.Haynes@Sun.COM  * Remove the dead entry from the refreshq_dead_entries
107912081SThomas.Haynes@Sun.COM  * list.
108012081SThomas.Haynes@Sun.COM  */
108112081SThomas.Haynes@Sun.COM static void
nfsauth_remove_dead_entry(struct auth_cache * dead)108212081SThomas.Haynes@Sun.COM nfsauth_remove_dead_entry(struct auth_cache *dead)
108312081SThomas.Haynes@Sun.COM {
108412081SThomas.Haynes@Sun.COM 	struct auth_cache	*p;
108512081SThomas.Haynes@Sun.COM 	struct auth_cache	*prev;
108612081SThomas.Haynes@Sun.COM 	struct auth_cache	*next;
108712081SThomas.Haynes@Sun.COM 
108812081SThomas.Haynes@Sun.COM 	mutex_enter(&refreshq_lock);
108912081SThomas.Haynes@Sun.COM 	prev = NULL;
109012081SThomas.Haynes@Sun.COM 	for (p = refreshq_dead_entries; p != NULL; p = next) {
109112081SThomas.Haynes@Sun.COM 		next = p->auth_next;
109212081SThomas.Haynes@Sun.COM 
109312081SThomas.Haynes@Sun.COM 		if (p == dead) {
109412081SThomas.Haynes@Sun.COM 			if (prev == NULL)
109512081SThomas.Haynes@Sun.COM 				refreshq_dead_entries = next;
109612081SThomas.Haynes@Sun.COM 			else
109712081SThomas.Haynes@Sun.COM 				prev->auth_next = next;
109812081SThomas.Haynes@Sun.COM 
109912081SThomas.Haynes@Sun.COM 			nfsauth_free_node(dead);
110012081SThomas.Haynes@Sun.COM 			break;
110112081SThomas.Haynes@Sun.COM 		}
110212081SThomas.Haynes@Sun.COM 
110312081SThomas.Haynes@Sun.COM 		prev = p;
110412081SThomas.Haynes@Sun.COM 	}
110512081SThomas.Haynes@Sun.COM 	mutex_exit(&refreshq_lock);
110612081SThomas.Haynes@Sun.COM }
110712081SThomas.Haynes@Sun.COM 
11080Sstevel@tonic-gate /*
11090Sstevel@tonic-gate  * Free the nfsauth cache for a given export
11100Sstevel@tonic-gate  */
11110Sstevel@tonic-gate void
nfsauth_cache_free(struct exportinfo * exi)11120Sstevel@tonic-gate nfsauth_cache_free(struct exportinfo *exi)
11130Sstevel@tonic-gate {
11140Sstevel@tonic-gate 	int i;
11150Sstevel@tonic-gate 	struct auth_cache *p, *next;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	for (i = 0; i < AUTH_TABLESIZE; i++) {
11180Sstevel@tonic-gate 		for (p = exi->exi_cache[i]; p; p = next) {
11190Sstevel@tonic-gate 			next = p->auth_next;
112012081SThomas.Haynes@Sun.COM 
112112081SThomas.Haynes@Sun.COM 			/*
112212081SThomas.Haynes@Sun.COM 			 * The only way we got here
112312081SThomas.Haynes@Sun.COM 			 * was with an exi_rele, which
112412081SThomas.Haynes@Sun.COM 			 * means that no auth cache entry
112512081SThomas.Haynes@Sun.COM 			 * is being refreshed.
112612081SThomas.Haynes@Sun.COM 			 */
112712081SThomas.Haynes@Sun.COM 			nfsauth_free_node(p);
11280Sstevel@tonic-gate 		}
11290Sstevel@tonic-gate 	}
11300Sstevel@tonic-gate }
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate /*
11330Sstevel@tonic-gate  * Called by the kernel memory allocator when
11340Sstevel@tonic-gate  * memory is low. Free unused cache entries.
11350Sstevel@tonic-gate  * If that's not enough, the VM system will
11360Sstevel@tonic-gate  * call again for some more.
11370Sstevel@tonic-gate  */
11380Sstevel@tonic-gate /*ARGSUSED*/
11390Sstevel@tonic-gate void
exi_cache_reclaim(void * cdrarg)11400Sstevel@tonic-gate exi_cache_reclaim(void *cdrarg)
11410Sstevel@tonic-gate {
11420Sstevel@tonic-gate 	int i;
11430Sstevel@tonic-gate 	struct exportinfo *exi;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	rw_enter(&exported_lock, RW_READER);
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	for (i = 0; i < EXPTABLESIZE; i++) {
1148*12679SPavel.Filipensky@Sun.COM 		for (exi = exptable[i]; exi; exi = exi->fid_hash.next) {
11490Sstevel@tonic-gate 			exi_cache_trim(exi);
11500Sstevel@tonic-gate 		}
11510Sstevel@tonic-gate 	}
11520Sstevel@tonic-gate 	nfsauth_cache_reclaim++;
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	rw_exit(&exported_lock);
11550Sstevel@tonic-gate }
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate void
exi_cache_trim(struct exportinfo * exi)11580Sstevel@tonic-gate exi_cache_trim(struct exportinfo *exi)
11590Sstevel@tonic-gate {
11600Sstevel@tonic-gate 	struct auth_cache *p;
11610Sstevel@tonic-gate 	struct auth_cache *prev, *next;
11620Sstevel@tonic-gate 	int i;
11630Sstevel@tonic-gate 	time_t stale_time;
11640Sstevel@tonic-gate 
116512081SThomas.Haynes@Sun.COM 	stale_time = gethrestime_sec() - NFSAUTH_CACHE_TRIM;
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	rw_enter(&exi->exi_cache_lock, RW_WRITER);
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	for (i = 0; i < AUTH_TABLESIZE; i++) {
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 		/*
11720Sstevel@tonic-gate 		 * Free entries that have not been
117312081SThomas.Haynes@Sun.COM 		 * used for NFSAUTH_CACHE_TRIM seconds.
11740Sstevel@tonic-gate 		 */
11750Sstevel@tonic-gate 		prev = NULL;
11760Sstevel@tonic-gate 		for (p = exi->exi_cache[i]; p; p = next) {
11770Sstevel@tonic-gate 			next = p->auth_next;
11780Sstevel@tonic-gate 			if (p->auth_time > stale_time) {
11790Sstevel@tonic-gate 				prev = p;
11800Sstevel@tonic-gate 				continue;
11810Sstevel@tonic-gate 			}
11820Sstevel@tonic-gate 
118312081SThomas.Haynes@Sun.COM 			mutex_enter(&p->auth_lock);
118412081SThomas.Haynes@Sun.COM 			DTRACE_PROBE1(nfsauth__debug__trim__state,
118512081SThomas.Haynes@Sun.COM 			    auth_state_t, p->auth_state);
118612081SThomas.Haynes@Sun.COM 
118712081SThomas.Haynes@Sun.COM 			if (p->auth_state != NFS_AUTH_FRESH) {
118812081SThomas.Haynes@Sun.COM 				p->auth_state = NFS_AUTH_INVALID;
118912081SThomas.Haynes@Sun.COM 				mutex_exit(&p->auth_lock);
119012081SThomas.Haynes@Sun.COM 
119112081SThomas.Haynes@Sun.COM 				mutex_enter(&refreshq_lock);
119212081SThomas.Haynes@Sun.COM 				p->auth_next = refreshq_dead_entries;
119312081SThomas.Haynes@Sun.COM 				refreshq_dead_entries = p;
119412081SThomas.Haynes@Sun.COM 				mutex_exit(&refreshq_lock);
119512081SThomas.Haynes@Sun.COM 			} else {
119612081SThomas.Haynes@Sun.COM 				mutex_exit(&p->auth_lock);
119712081SThomas.Haynes@Sun.COM 				nfsauth_free_node(p);
119812081SThomas.Haynes@Sun.COM 			}
119912081SThomas.Haynes@Sun.COM 
12000Sstevel@tonic-gate 			if (prev == NULL)
12010Sstevel@tonic-gate 				exi->exi_cache[i] = next;
12020Sstevel@tonic-gate 			else
12030Sstevel@tonic-gate 				prev->auth_next = next;
12040Sstevel@tonic-gate 		}
12050Sstevel@tonic-gate 	}
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	rw_exit(&exi->exi_cache_lock);
12080Sstevel@tonic-gate }
1209