xref: /openbmc/linux/net/sunrpc/auth.c (revision a17c2153)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * linux/net/sunrpc/auth.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Generic RPC client authentication API.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/types.h>
101da177e4SLinus Torvalds #include <linux/sched.h>
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/slab.h>
131da177e4SLinus Torvalds #include <linux/errno.h>
1425337fdcSTrond Myklebust #include <linux/hash.h>
151da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
161da177e4SLinus Torvalds #include <linux/spinlock.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #ifdef RPC_DEBUG
191da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_AUTH
201da177e4SLinus Torvalds #endif
211da177e4SLinus Torvalds 
22241269bdSTrond Myklebust #define RPC_CREDCACHE_DEFAULT_HASHBITS	(4)
23241269bdSTrond Myklebust struct rpc_cred_cache {
24241269bdSTrond Myklebust 	struct hlist_head	*hashtable;
25241269bdSTrond Myklebust 	unsigned int		hashbits;
26241269bdSTrond Myklebust 	spinlock_t		lock;
27241269bdSTrond Myklebust };
28241269bdSTrond Myklebust 
29241269bdSTrond Myklebust static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS;
30241269bdSTrond Myklebust 
31fc1b356fSTrond Myklebust static DEFINE_SPINLOCK(rpc_authflavor_lock);
32f1c0a861STrond Myklebust static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
331da177e4SLinus Torvalds 	&authnull_ops,		/* AUTH_NULL */
341da177e4SLinus Torvalds 	&authunix_ops,		/* AUTH_UNIX */
351da177e4SLinus Torvalds 	NULL,			/* others can be loadable modules */
361da177e4SLinus Torvalds };
371da177e4SLinus Torvalds 
38e092bdcdSTrond Myklebust static LIST_HEAD(cred_unused);
39f5c2187cSTrond Myklebust static unsigned long number_cred_unused;
40e092bdcdSTrond Myklebust 
41241269bdSTrond Myklebust #define MAX_HASHTABLE_BITS (10)
42241269bdSTrond Myklebust static int param_set_hashtbl_sz(const char *val, struct kernel_param *kp)
43241269bdSTrond Myklebust {
44241269bdSTrond Myklebust 	unsigned long num;
45241269bdSTrond Myklebust 	unsigned int nbits;
46241269bdSTrond Myklebust 	int ret;
47241269bdSTrond Myklebust 
48241269bdSTrond Myklebust 	if (!val)
49241269bdSTrond Myklebust 		goto out_inval;
50241269bdSTrond Myklebust 	ret = strict_strtoul(val, 0, &num);
51241269bdSTrond Myklebust 	if (ret == -EINVAL)
52241269bdSTrond Myklebust 		goto out_inval;
53241269bdSTrond Myklebust 	nbits = fls(num);
54241269bdSTrond Myklebust 	if (num > (1U << nbits))
55241269bdSTrond Myklebust 		nbits++;
56241269bdSTrond Myklebust 	if (nbits > MAX_HASHTABLE_BITS || nbits < 2)
57241269bdSTrond Myklebust 		goto out_inval;
58241269bdSTrond Myklebust 	*(unsigned int *)kp->arg = nbits;
59241269bdSTrond Myklebust 	return 0;
60241269bdSTrond Myklebust out_inval:
61241269bdSTrond Myklebust 	return -EINVAL;
62241269bdSTrond Myklebust }
63241269bdSTrond Myklebust 
64241269bdSTrond Myklebust static int param_get_hashtbl_sz(char *buffer, struct kernel_param *kp)
65241269bdSTrond Myklebust {
66241269bdSTrond Myklebust 	unsigned int nbits;
67241269bdSTrond Myklebust 
68241269bdSTrond Myklebust 	nbits = *(unsigned int *)kp->arg;
69241269bdSTrond Myklebust 	return sprintf(buffer, "%u", 1U << nbits);
70241269bdSTrond Myklebust }
71241269bdSTrond Myklebust 
72241269bdSTrond Myklebust #define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
73241269bdSTrond Myklebust 
74241269bdSTrond Myklebust module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
75241269bdSTrond Myklebust MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
76241269bdSTrond Myklebust 
771da177e4SLinus Torvalds static u32
781da177e4SLinus Torvalds pseudoflavor_to_flavor(u32 flavor) {
791da177e4SLinus Torvalds 	if (flavor >= RPC_AUTH_MAXFLAVOR)
801da177e4SLinus Torvalds 		return RPC_AUTH_GSS;
811da177e4SLinus Torvalds 	return flavor;
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds int
85f1c0a861STrond Myklebust rpcauth_register(const struct rpc_authops *ops)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	rpc_authflavor_t flavor;
88fc1b356fSTrond Myklebust 	int ret = -EPERM;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
911da177e4SLinus Torvalds 		return -EINVAL;
92fc1b356fSTrond Myklebust 	spin_lock(&rpc_authflavor_lock);
93fc1b356fSTrond Myklebust 	if (auth_flavors[flavor] == NULL) {
941da177e4SLinus Torvalds 		auth_flavors[flavor] = ops;
95fc1b356fSTrond Myklebust 		ret = 0;
96fc1b356fSTrond Myklebust 	}
97fc1b356fSTrond Myklebust 	spin_unlock(&rpc_authflavor_lock);
98fc1b356fSTrond Myklebust 	return ret;
991da177e4SLinus Torvalds }
100e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_register);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds int
103f1c0a861STrond Myklebust rpcauth_unregister(const struct rpc_authops *ops)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	rpc_authflavor_t flavor;
106fc1b356fSTrond Myklebust 	int ret = -EPERM;
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
1091da177e4SLinus Torvalds 		return -EINVAL;
110fc1b356fSTrond Myklebust 	spin_lock(&rpc_authflavor_lock);
111fc1b356fSTrond Myklebust 	if (auth_flavors[flavor] == ops) {
1121da177e4SLinus Torvalds 		auth_flavors[flavor] = NULL;
113fc1b356fSTrond Myklebust 		ret = 0;
114fc1b356fSTrond Myklebust 	}
115fc1b356fSTrond Myklebust 	spin_unlock(&rpc_authflavor_lock);
116fc1b356fSTrond Myklebust 	return ret;
1171da177e4SLinus Torvalds }
118e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_unregister);
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds struct rpc_auth *
1211da177e4SLinus Torvalds rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	struct rpc_auth		*auth;
124f1c0a861STrond Myklebust 	const struct rpc_authops *ops;
1251da177e4SLinus Torvalds 	u32			flavor = pseudoflavor_to_flavor(pseudoflavor);
1261da177e4SLinus Torvalds 
127f344f6dfSOlaf Kirch 	auth = ERR_PTR(-EINVAL);
128f344f6dfSOlaf Kirch 	if (flavor >= RPC_AUTH_MAXFLAVOR)
129f344f6dfSOlaf Kirch 		goto out;
130f344f6dfSOlaf Kirch 
131f344f6dfSOlaf Kirch 	if ((ops = auth_flavors[flavor]) == NULL)
132f344f6dfSOlaf Kirch 		request_module("rpc-auth-%u", flavor);
133fc1b356fSTrond Myklebust 	spin_lock(&rpc_authflavor_lock);
134fc1b356fSTrond Myklebust 	ops = auth_flavors[flavor];
135fc1b356fSTrond Myklebust 	if (ops == NULL || !try_module_get(ops->owner)) {
136fc1b356fSTrond Myklebust 		spin_unlock(&rpc_authflavor_lock);
137f344f6dfSOlaf Kirch 		goto out;
138fc1b356fSTrond Myklebust 	}
139fc1b356fSTrond Myklebust 	spin_unlock(&rpc_authflavor_lock);
1401da177e4SLinus Torvalds 	auth = ops->create(clnt, pseudoflavor);
141fc1b356fSTrond Myklebust 	module_put(ops->owner);
1426a19275aSJ. Bruce Fields 	if (IS_ERR(auth))
1436a19275aSJ. Bruce Fields 		return auth;
1441da177e4SLinus Torvalds 	if (clnt->cl_auth)
145de7a8ce3STrond Myklebust 		rpcauth_release(clnt->cl_auth);
1461da177e4SLinus Torvalds 	clnt->cl_auth = auth;
147f344f6dfSOlaf Kirch 
148f344f6dfSOlaf Kirch out:
1491da177e4SLinus Torvalds 	return auth;
1501da177e4SLinus Torvalds }
151e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_create);
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds void
154de7a8ce3STrond Myklebust rpcauth_release(struct rpc_auth *auth)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	if (!atomic_dec_and_test(&auth->au_count))
1571da177e4SLinus Torvalds 		return;
1581da177e4SLinus Torvalds 	auth->au_ops->destroy(auth);
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds static DEFINE_SPINLOCK(rpc_credcache_lock);
1621da177e4SLinus Torvalds 
16331be5bf1STrond Myklebust static void
16431be5bf1STrond Myklebust rpcauth_unhash_cred_locked(struct rpc_cred *cred)
16531be5bf1STrond Myklebust {
16631be5bf1STrond Myklebust 	hlist_del_rcu(&cred->cr_hash);
16731be5bf1STrond Myklebust 	smp_mb__before_clear_bit();
16831be5bf1STrond Myklebust 	clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
16931be5bf1STrond Myklebust }
17031be5bf1STrond Myklebust 
171f0380f3dSTrond Myklebust static int
1729499b434STrond Myklebust rpcauth_unhash_cred(struct rpc_cred *cred)
1739499b434STrond Myklebust {
1749499b434STrond Myklebust 	spinlock_t *cache_lock;
175f0380f3dSTrond Myklebust 	int ret;
1769499b434STrond Myklebust 
1779499b434STrond Myklebust 	cache_lock = &cred->cr_auth->au_credcache->lock;
1789499b434STrond Myklebust 	spin_lock(cache_lock);
179f0380f3dSTrond Myklebust 	ret = atomic_read(&cred->cr_count) == 0;
180f0380f3dSTrond Myklebust 	if (ret)
1819499b434STrond Myklebust 		rpcauth_unhash_cred_locked(cred);
1829499b434STrond Myklebust 	spin_unlock(cache_lock);
183f0380f3dSTrond Myklebust 	return ret;
1849499b434STrond Myklebust }
1859499b434STrond Myklebust 
1861da177e4SLinus Torvalds /*
1871da177e4SLinus Torvalds  * Initialize RPC credential cache
1881da177e4SLinus Torvalds  */
1891da177e4SLinus Torvalds int
190f5c2187cSTrond Myklebust rpcauth_init_credcache(struct rpc_auth *auth)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	struct rpc_cred_cache *new;
193988664a0STrond Myklebust 	unsigned int hashsize;
1941da177e4SLinus Torvalds 
1958b3a7005SKris Katterjohn 	new = kmalloc(sizeof(*new), GFP_KERNEL);
1961da177e4SLinus Torvalds 	if (!new)
197241269bdSTrond Myklebust 		goto out_nocache;
198241269bdSTrond Myklebust 	new->hashbits = auth_hashbits;
199988664a0STrond Myklebust 	hashsize = 1U << new->hashbits;
200241269bdSTrond Myklebust 	new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
201241269bdSTrond Myklebust 	if (!new->hashtable)
202241269bdSTrond Myklebust 		goto out_nohashtbl;
2039499b434STrond Myklebust 	spin_lock_init(&new->lock);
2041da177e4SLinus Torvalds 	auth->au_credcache = new;
2051da177e4SLinus Torvalds 	return 0;
206241269bdSTrond Myklebust out_nohashtbl:
207241269bdSTrond Myklebust 	kfree(new);
208241269bdSTrond Myklebust out_nocache:
209241269bdSTrond Myklebust 	return -ENOMEM;
2101da177e4SLinus Torvalds }
211e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds /*
2141da177e4SLinus Torvalds  * Destroy a list of credentials
2151da177e4SLinus Torvalds  */
2161da177e4SLinus Torvalds static inline
217e092bdcdSTrond Myklebust void rpcauth_destroy_credlist(struct list_head *head)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	struct rpc_cred *cred;
2201da177e4SLinus Torvalds 
221e092bdcdSTrond Myklebust 	while (!list_empty(head)) {
222e092bdcdSTrond Myklebust 		cred = list_entry(head->next, struct rpc_cred, cr_lru);
223e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
2241da177e4SLinus Torvalds 		put_rpccred(cred);
2251da177e4SLinus Torvalds 	}
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds /*
2291da177e4SLinus Torvalds  * Clear the RPC credential cache, and delete those credentials
2301da177e4SLinus Torvalds  * that are not referenced.
2311da177e4SLinus Torvalds  */
2321da177e4SLinus Torvalds void
2333ab9bb72STrond Myklebust rpcauth_clear_credcache(struct rpc_cred_cache *cache)
2341da177e4SLinus Torvalds {
235e092bdcdSTrond Myklebust 	LIST_HEAD(free);
236e092bdcdSTrond Myklebust 	struct hlist_head *head;
2371da177e4SLinus Torvalds 	struct rpc_cred	*cred;
238988664a0STrond Myklebust 	unsigned int hashsize = 1U << cache->hashbits;
2391da177e4SLinus Torvalds 	int		i;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	spin_lock(&rpc_credcache_lock);
2429499b434STrond Myklebust 	spin_lock(&cache->lock);
243988664a0STrond Myklebust 	for (i = 0; i < hashsize; i++) {
244e092bdcdSTrond Myklebust 		head = &cache->hashtable[i];
245e092bdcdSTrond Myklebust 		while (!hlist_empty(head)) {
246e092bdcdSTrond Myklebust 			cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
247e092bdcdSTrond Myklebust 			get_rpccred(cred);
248f5c2187cSTrond Myklebust 			if (!list_empty(&cred->cr_lru)) {
249f5c2187cSTrond Myklebust 				list_del(&cred->cr_lru);
250f5c2187cSTrond Myklebust 				number_cred_unused--;
251f5c2187cSTrond Myklebust 			}
252f5c2187cSTrond Myklebust 			list_add_tail(&cred->cr_lru, &free);
25331be5bf1STrond Myklebust 			rpcauth_unhash_cred_locked(cred);
2541da177e4SLinus Torvalds 		}
2551da177e4SLinus Torvalds 	}
2569499b434STrond Myklebust 	spin_unlock(&cache->lock);
2571da177e4SLinus Torvalds 	spin_unlock(&rpc_credcache_lock);
2581da177e4SLinus Torvalds 	rpcauth_destroy_credlist(&free);
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
2613ab9bb72STrond Myklebust /*
2623ab9bb72STrond Myklebust  * Destroy the RPC credential cache
2633ab9bb72STrond Myklebust  */
2643ab9bb72STrond Myklebust void
2653ab9bb72STrond Myklebust rpcauth_destroy_credcache(struct rpc_auth *auth)
2663ab9bb72STrond Myklebust {
2673ab9bb72STrond Myklebust 	struct rpc_cred_cache *cache = auth->au_credcache;
2683ab9bb72STrond Myklebust 
2693ab9bb72STrond Myklebust 	if (cache) {
2703ab9bb72STrond Myklebust 		auth->au_credcache = NULL;
2713ab9bb72STrond Myklebust 		rpcauth_clear_credcache(cache);
272241269bdSTrond Myklebust 		kfree(cache->hashtable);
2733ab9bb72STrond Myklebust 		kfree(cache);
2743ab9bb72STrond Myklebust 	}
2753ab9bb72STrond Myklebust }
276e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
2773ab9bb72STrond Myklebust 
278d2b83141STrond Myklebust 
279d2b83141STrond Myklebust #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
280d2b83141STrond Myklebust 
2811da177e4SLinus Torvalds /*
2821da177e4SLinus Torvalds  * Remove stale credentials. Avoid sleeping inside the loop.
2831da177e4SLinus Torvalds  */
284f5c2187cSTrond Myklebust static int
285f5c2187cSTrond Myklebust rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
2861da177e4SLinus Torvalds {
2879499b434STrond Myklebust 	spinlock_t *cache_lock;
288eac0d18dSTrond Myklebust 	struct rpc_cred *cred, *next;
289d2b83141STrond Myklebust 	unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
2901da177e4SLinus Torvalds 
291eac0d18dSTrond Myklebust 	list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
292eac0d18dSTrond Myklebust 
29320673406STrond Myklebust 		if (nr_to_scan-- == 0)
29420673406STrond Myklebust 			break;
29593a05e65STrond Myklebust 		/*
29693a05e65STrond Myklebust 		 * Enforce a 60 second garbage collection moratorium
29793a05e65STrond Myklebust 		 * Note that the cred_unused list must be time-ordered.
29893a05e65STrond Myklebust 		 */
2993d7b0894STrond Myklebust 		if (time_in_range(cred->cr_expire, expired, jiffies) &&
300eac0d18dSTrond Myklebust 		    test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
30193a05e65STrond Myklebust 			return 0;
302eac0d18dSTrond Myklebust 
303e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
304f5c2187cSTrond Myklebust 		number_cred_unused--;
305e092bdcdSTrond Myklebust 		if (atomic_read(&cred->cr_count) != 0)
306e092bdcdSTrond Myklebust 			continue;
307eac0d18dSTrond Myklebust 
3089499b434STrond Myklebust 		cache_lock = &cred->cr_auth->au_credcache->lock;
3099499b434STrond Myklebust 		spin_lock(cache_lock);
3109499b434STrond Myklebust 		if (atomic_read(&cred->cr_count) == 0) {
311e092bdcdSTrond Myklebust 			get_rpccred(cred);
312e092bdcdSTrond Myklebust 			list_add_tail(&cred->cr_lru, free);
31331be5bf1STrond Myklebust 			rpcauth_unhash_cred_locked(cred);
3141da177e4SLinus Torvalds 		}
3159499b434STrond Myklebust 		spin_unlock(cache_lock);
3169499b434STrond Myklebust 	}
31793a05e65STrond Myklebust 	return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
3181da177e4SLinus Torvalds }
319e092bdcdSTrond Myklebust 
320e092bdcdSTrond Myklebust /*
321f5c2187cSTrond Myklebust  * Run memory cache shrinker.
322e092bdcdSTrond Myklebust  */
323f5c2187cSTrond Myklebust static int
324f5c2187cSTrond Myklebust rpcauth_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
325e092bdcdSTrond Myklebust {
326f5c2187cSTrond Myklebust 	LIST_HEAD(free);
327f5c2187cSTrond Myklebust 	int res;
328f5c2187cSTrond Myklebust 
329d300a41eSTrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
330d300a41eSTrond Myklebust 		return (nr_to_scan == 0) ? 0 : -1;
331f5c2187cSTrond Myklebust 	if (list_empty(&cred_unused))
332f5c2187cSTrond Myklebust 		return 0;
33331be5bf1STrond Myklebust 	spin_lock(&rpc_credcache_lock);
33493a05e65STrond Myklebust 	res = rpcauth_prune_expired(&free, nr_to_scan);
33531be5bf1STrond Myklebust 	spin_unlock(&rpc_credcache_lock);
336f5c2187cSTrond Myklebust 	rpcauth_destroy_credlist(&free);
337f5c2187cSTrond Myklebust 	return res;
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds /*
3411da177e4SLinus Torvalds  * Look up a process' credentials in the authentication cache
3421da177e4SLinus Torvalds  */
3431da177e4SLinus Torvalds struct rpc_cred *
3441da177e4SLinus Torvalds rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
3458a317760STrond Myklebust 		int flags)
3461da177e4SLinus Torvalds {
347e092bdcdSTrond Myklebust 	LIST_HEAD(free);
3481da177e4SLinus Torvalds 	struct rpc_cred_cache *cache = auth->au_credcache;
349e092bdcdSTrond Myklebust 	struct hlist_node *pos;
35031be5bf1STrond Myklebust 	struct rpc_cred	*cred = NULL,
35131be5bf1STrond Myklebust 			*entry, *new;
35225337fdcSTrond Myklebust 	unsigned int nr;
35325337fdcSTrond Myklebust 
354988664a0STrond Myklebust 	nr = hash_long(acred->uid, cache->hashbits);
3551da177e4SLinus Torvalds 
35631be5bf1STrond Myklebust 	rcu_read_lock();
35731be5bf1STrond Myklebust 	hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) {
35831be5bf1STrond Myklebust 		if (!entry->cr_ops->crmatch(acred, entry, flags))
35931be5bf1STrond Myklebust 			continue;
3609499b434STrond Myklebust 		spin_lock(&cache->lock);
36131be5bf1STrond Myklebust 		if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
3629499b434STrond Myklebust 			spin_unlock(&cache->lock);
36331be5bf1STrond Myklebust 			continue;
36431be5bf1STrond Myklebust 		}
36531be5bf1STrond Myklebust 		cred = get_rpccred(entry);
3669499b434STrond Myklebust 		spin_unlock(&cache->lock);
36731be5bf1STrond Myklebust 		break;
36831be5bf1STrond Myklebust 	}
36931be5bf1STrond Myklebust 	rcu_read_unlock();
37031be5bf1STrond Myklebust 
3719499b434STrond Myklebust 	if (cred != NULL)
37231be5bf1STrond Myklebust 		goto found;
37331be5bf1STrond Myklebust 
37431be5bf1STrond Myklebust 	new = auth->au_ops->crcreate(auth, acred, flags);
37531be5bf1STrond Myklebust 	if (IS_ERR(new)) {
37631be5bf1STrond Myklebust 		cred = new;
37731be5bf1STrond Myklebust 		goto out;
37831be5bf1STrond Myklebust 	}
37931be5bf1STrond Myklebust 
3809499b434STrond Myklebust 	spin_lock(&cache->lock);
381e092bdcdSTrond Myklebust 	hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) {
382e092bdcdSTrond Myklebust 		if (!entry->cr_ops->crmatch(acred, entry, flags))
383e092bdcdSTrond Myklebust 			continue;
384e092bdcdSTrond Myklebust 		cred = get_rpccred(entry);
3851da177e4SLinus Torvalds 		break;
3861da177e4SLinus Torvalds 	}
38731be5bf1STrond Myklebust 	if (cred == NULL) {
38831be5bf1STrond Myklebust 		cred = new;
38931be5bf1STrond Myklebust 		set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
39031be5bf1STrond Myklebust 		hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
39131be5bf1STrond Myklebust 	} else
392e092bdcdSTrond Myklebust 		list_add_tail(&new->cr_lru, &free);
3939499b434STrond Myklebust 	spin_unlock(&cache->lock);
39431be5bf1STrond Myklebust found:
395f64f9e71SJoe Perches 	if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
396f64f9e71SJoe Perches 	    cred->cr_ops->cr_init != NULL &&
397f64f9e71SJoe Perches 	    !(flags & RPCAUTH_LOOKUP_NEW)) {
398fba3bad4STrond Myklebust 		int res = cred->cr_ops->cr_init(auth, cred);
399fba3bad4STrond Myklebust 		if (res < 0) {
400fba3bad4STrond Myklebust 			put_rpccred(cred);
401fba3bad4STrond Myklebust 			cred = ERR_PTR(res);
402fba3bad4STrond Myklebust 		}
4031da177e4SLinus Torvalds 	}
40431be5bf1STrond Myklebust 	rpcauth_destroy_credlist(&free);
40531be5bf1STrond Myklebust out:
40631be5bf1STrond Myklebust 	return cred;
4071da177e4SLinus Torvalds }
408e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds struct rpc_cred *
4118a317760STrond Myklebust rpcauth_lookupcred(struct rpc_auth *auth, int flags)
4121da177e4SLinus Torvalds {
41386a264abSDavid Howells 	struct auth_cred acred;
4141da177e4SLinus Torvalds 	struct rpc_cred *ret;
41586a264abSDavid Howells 	const struct cred *cred = current_cred();
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	dprintk("RPC:       looking up %s cred\n",
4181da177e4SLinus Torvalds 		auth->au_ops->au_name);
41986a264abSDavid Howells 
42086a264abSDavid Howells 	memset(&acred, 0, sizeof(acred));
42186a264abSDavid Howells 	acred.uid = cred->fsuid;
42286a264abSDavid Howells 	acred.gid = cred->fsgid;
42386a264abSDavid Howells 	acred.group_info = get_group_info(((struct cred *)cred)->group_info);
42486a264abSDavid Howells 
4258a317760STrond Myklebust 	ret = auth->au_ops->lookup_cred(auth, &acred, flags);
4261da177e4SLinus Torvalds 	put_group_info(acred.group_info);
4271da177e4SLinus Torvalds 	return ret;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4305fe4755eSTrond Myklebust void
4315fe4755eSTrond Myklebust rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
4325fe4755eSTrond Myklebust 		  struct rpc_auth *auth, const struct rpc_credops *ops)
4335fe4755eSTrond Myklebust {
4345fe4755eSTrond Myklebust 	INIT_HLIST_NODE(&cred->cr_hash);
435e092bdcdSTrond Myklebust 	INIT_LIST_HEAD(&cred->cr_lru);
4365fe4755eSTrond Myklebust 	atomic_set(&cred->cr_count, 1);
4375fe4755eSTrond Myklebust 	cred->cr_auth = auth;
4385fe4755eSTrond Myklebust 	cred->cr_ops = ops;
4395fe4755eSTrond Myklebust 	cred->cr_expire = jiffies;
4405fe4755eSTrond Myklebust #ifdef RPC_DEBUG
4415fe4755eSTrond Myklebust 	cred->cr_magic = RPCAUTH_CRED_MAGIC;
4425fe4755eSTrond Myklebust #endif
4435fe4755eSTrond Myklebust 	cred->cr_uid = acred->uid;
4445fe4755eSTrond Myklebust }
445e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_init_cred);
4465fe4755eSTrond Myklebust 
4478572b8e2STrond Myklebust struct rpc_cred *
4485d351754STrond Myklebust rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
4494ccda2cdSTrond Myklebust {
4504ccda2cdSTrond Myklebust 	dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
4514ccda2cdSTrond Myklebust 			cred->cr_auth->au_ops->au_name, cred);
4528572b8e2STrond Myklebust 	return get_rpccred(cred);
4534ccda2cdSTrond Myklebust }
4545c691044STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
4554ccda2cdSTrond Myklebust 
4568572b8e2STrond Myklebust static struct rpc_cred *
4575d351754STrond Myklebust rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
4581da177e4SLinus Torvalds {
4591be27f36STrond Myklebust 	struct rpc_auth *auth = task->tk_client->cl_auth;
4601da177e4SLinus Torvalds 	struct auth_cred acred = {
461af093835STrond Myklebust 		.uid = 0,
462af093835STrond Myklebust 		.gid = 0,
4631da177e4SLinus Torvalds 	};
4641da177e4SLinus Torvalds 
46546121cf7SChuck Lever 	dprintk("RPC: %5u looking up %s cred\n",
4661be27f36STrond Myklebust 		task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
4678572b8e2STrond Myklebust 	return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
468af093835STrond Myklebust }
469af093835STrond Myklebust 
4708572b8e2STrond Myklebust static struct rpc_cred *
4715d351754STrond Myklebust rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
472af093835STrond Myklebust {
473af093835STrond Myklebust 	struct rpc_auth *auth = task->tk_client->cl_auth;
474af093835STrond Myklebust 
475af093835STrond Myklebust 	dprintk("RPC: %5u looking up %s cred\n",
476af093835STrond Myklebust 		task->tk_pid, auth->au_ops->au_name);
4778572b8e2STrond Myklebust 	return rpcauth_lookupcred(auth, lookupflags);
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
480a17c2153STrond Myklebust static int
4814ccda2cdSTrond Myklebust rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
4821da177e4SLinus Torvalds {
483a17c2153STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
4848572b8e2STrond Myklebust 	struct rpc_cred *new;
4855d351754STrond Myklebust 	int lookupflags = 0;
4865d351754STrond Myklebust 
4875d351754STrond Myklebust 	if (flags & RPC_TASK_ASYNC)
4885d351754STrond Myklebust 		lookupflags |= RPCAUTH_LOOKUP_NEW;
4894ccda2cdSTrond Myklebust 	if (cred != NULL)
4908572b8e2STrond Myklebust 		new = cred->cr_ops->crbind(task, cred, lookupflags);
4914ccda2cdSTrond Myklebust 	else if (flags & RPC_TASK_ROOTCREDS)
4928572b8e2STrond Myklebust 		new = rpcauth_bind_root_cred(task, lookupflags);
4934ccda2cdSTrond Myklebust 	else
4948572b8e2STrond Myklebust 		new = rpcauth_bind_new_cred(task, lookupflags);
4958572b8e2STrond Myklebust 	if (IS_ERR(new))
4968572b8e2STrond Myklebust 		return PTR_ERR(new);
497a17c2153STrond Myklebust 	if (req->rq_cred != NULL)
498a17c2153STrond Myklebust 		put_rpccred(req->rq_cred);
499a17c2153STrond Myklebust 	req->rq_cred = new;
5008572b8e2STrond Myklebust 	return 0;
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds void
5041da177e4SLinus Torvalds put_rpccred(struct rpc_cred *cred)
5051da177e4SLinus Torvalds {
506e092bdcdSTrond Myklebust 	/* Fast path for unhashed credentials */
507f0380f3dSTrond Myklebust 	if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
508f0380f3dSTrond Myklebust 		if (atomic_dec_and_test(&cred->cr_count))
509f0380f3dSTrond Myklebust 			cred->cr_ops->crdestroy(cred);
5101da177e4SLinus Torvalds 		return;
511f0380f3dSTrond Myklebust 	}
512f0380f3dSTrond Myklebust 
513e092bdcdSTrond Myklebust 	if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
514e092bdcdSTrond Myklebust 		return;
515f5c2187cSTrond Myklebust 	if (!list_empty(&cred->cr_lru)) {
516f5c2187cSTrond Myklebust 		number_cred_unused--;
517e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
518f5c2187cSTrond Myklebust 	}
5195f707eb4STrond Myklebust 	if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
520f0380f3dSTrond Myklebust 		if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
521e092bdcdSTrond Myklebust 			cred->cr_expire = jiffies;
522e092bdcdSTrond Myklebust 			list_add_tail(&cred->cr_lru, &cred_unused);
523f5c2187cSTrond Myklebust 			number_cred_unused++;
524f0380f3dSTrond Myklebust 			goto out_nodestroy;
525f0380f3dSTrond Myklebust 		}
526f0380f3dSTrond Myklebust 		if (!rpcauth_unhash_cred(cred)) {
527f0380f3dSTrond Myklebust 			/* We were hashed and someone looked us up... */
528f0380f3dSTrond Myklebust 			goto out_nodestroy;
529f0380f3dSTrond Myklebust 		}
530e092bdcdSTrond Myklebust 	}
531e092bdcdSTrond Myklebust 	spin_unlock(&rpc_credcache_lock);
5321da177e4SLinus Torvalds 	cred->cr_ops->crdestroy(cred);
533f0380f3dSTrond Myklebust 	return;
534f0380f3dSTrond Myklebust out_nodestroy:
535f0380f3dSTrond Myklebust 	spin_unlock(&rpc_credcache_lock);
5361da177e4SLinus Torvalds }
537e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(put_rpccred);
5381da177e4SLinus Torvalds 
539d8ed029dSAlexey Dobriyan __be32 *
540d8ed029dSAlexey Dobriyan rpcauth_marshcred(struct rpc_task *task, __be32 *p)
5411da177e4SLinus Torvalds {
542a17c2153STrond Myklebust 	struct rpc_cred	*cred = task->tk_rqstp->rq_cred;
5431da177e4SLinus Torvalds 
54446121cf7SChuck Lever 	dprintk("RPC: %5u marshaling %s cred %p\n",
5451be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
5460bbacc40SChuck Lever 
5471da177e4SLinus Torvalds 	return cred->cr_ops->crmarshal(task, p);
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
550d8ed029dSAlexey Dobriyan __be32 *
551d8ed029dSAlexey Dobriyan rpcauth_checkverf(struct rpc_task *task, __be32 *p)
5521da177e4SLinus Torvalds {
553a17c2153STrond Myklebust 	struct rpc_cred	*cred = task->tk_rqstp->rq_cred;
5541da177e4SLinus Torvalds 
55546121cf7SChuck Lever 	dprintk("RPC: %5u validating %s cred %p\n",
5561be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
5570bbacc40SChuck Lever 
5581da177e4SLinus Torvalds 	return cred->cr_ops->crvalidate(task, p);
5591da177e4SLinus Torvalds }
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds int
5621da177e4SLinus Torvalds rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
563d8ed029dSAlexey Dobriyan 		__be32 *data, void *obj)
5641da177e4SLinus Torvalds {
565a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
5661da177e4SLinus Torvalds 
56746121cf7SChuck Lever 	dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
5681da177e4SLinus Torvalds 			task->tk_pid, cred->cr_ops->cr_name, cred);
5691da177e4SLinus Torvalds 	if (cred->cr_ops->crwrap_req)
5701da177e4SLinus Torvalds 		return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
5711da177e4SLinus Torvalds 	/* By default, we encode the arguments normally. */
57288a9fe8cSTrond Myklebust 	return encode(rqstp, data, obj);
5731da177e4SLinus Torvalds }
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds int
5761da177e4SLinus Torvalds rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
577d8ed029dSAlexey Dobriyan 		__be32 *data, void *obj)
5781da177e4SLinus Torvalds {
579a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
5801da177e4SLinus Torvalds 
58146121cf7SChuck Lever 	dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
5821da177e4SLinus Torvalds 			task->tk_pid, cred->cr_ops->cr_name, cred);
5831da177e4SLinus Torvalds 	if (cred->cr_ops->crunwrap_resp)
5841da177e4SLinus Torvalds 		return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
5851da177e4SLinus Torvalds 						   data, obj);
5861da177e4SLinus Torvalds 	/* By default, we decode the arguments normally. */
58788a9fe8cSTrond Myklebust 	return decode(rqstp, data, obj);
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds int
5911da177e4SLinus Torvalds rpcauth_refreshcred(struct rpc_task *task)
5921da177e4SLinus Torvalds {
593a17c2153STrond Myklebust 	struct rpc_cred	*cred = task->tk_rqstp->rq_cred;
5941da177e4SLinus Torvalds 	int err;
5951da177e4SLinus Torvalds 
596a17c2153STrond Myklebust 	cred = task->tk_rqstp->rq_cred;
597a17c2153STrond Myklebust 	if (cred == NULL) {
598a17c2153STrond Myklebust 		err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
599a17c2153STrond Myklebust 		if (err < 0)
600a17c2153STrond Myklebust 			goto out;
601a17c2153STrond Myklebust 		cred = task->tk_rqstp->rq_cred;
602a17c2153STrond Myklebust 	};
60346121cf7SChuck Lever 	dprintk("RPC: %5u refreshing %s cred %p\n",
6041be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
6050bbacc40SChuck Lever 
6061da177e4SLinus Torvalds 	err = cred->cr_ops->crrefresh(task);
607a17c2153STrond Myklebust out:
6081da177e4SLinus Torvalds 	if (err < 0)
6091da177e4SLinus Torvalds 		task->tk_status = err;
6101da177e4SLinus Torvalds 	return err;
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds void
6141da177e4SLinus Torvalds rpcauth_invalcred(struct rpc_task *task)
6151da177e4SLinus Torvalds {
616a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
617fc432dd9STrond Myklebust 
61846121cf7SChuck Lever 	dprintk("RPC: %5u invalidating %s cred %p\n",
6191be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
620fc432dd9STrond Myklebust 	if (cred)
621fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds int
6251da177e4SLinus Torvalds rpcauth_uptodatecred(struct rpc_task *task)
6261da177e4SLinus Torvalds {
627a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
628fc432dd9STrond Myklebust 
629fc432dd9STrond Myklebust 	return cred == NULL ||
630fc432dd9STrond Myklebust 		test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
6311da177e4SLinus Torvalds }
632f5c2187cSTrond Myklebust 
6338e1f936bSRusty Russell static struct shrinker rpc_cred_shrinker = {
6348e1f936bSRusty Russell 	.shrink = rpcauth_cache_shrinker,
6358e1f936bSRusty Russell 	.seeks = DEFAULT_SEEKS,
6368e1f936bSRusty Russell };
637f5c2187cSTrond Myklebust 
6385d8d9a4dSTrond Myklebust int __init rpcauth_init_module(void)
639f5c2187cSTrond Myklebust {
6405d8d9a4dSTrond Myklebust 	int err;
6415d8d9a4dSTrond Myklebust 
6425d8d9a4dSTrond Myklebust 	err = rpc_init_authunix();
6435d8d9a4dSTrond Myklebust 	if (err < 0)
6445d8d9a4dSTrond Myklebust 		goto out1;
6455d8d9a4dSTrond Myklebust 	err = rpc_init_generic_auth();
6465d8d9a4dSTrond Myklebust 	if (err < 0)
6475d8d9a4dSTrond Myklebust 		goto out2;
6488e1f936bSRusty Russell 	register_shrinker(&rpc_cred_shrinker);
6495d8d9a4dSTrond Myklebust 	return 0;
6505d8d9a4dSTrond Myklebust out2:
6515d8d9a4dSTrond Myklebust 	rpc_destroy_authunix();
6525d8d9a4dSTrond Myklebust out1:
6535d8d9a4dSTrond Myklebust 	return err;
654f5c2187cSTrond Myklebust }
655f5c2187cSTrond Myklebust 
656f5c2187cSTrond Myklebust void __exit rpcauth_remove_module(void)
657f5c2187cSTrond Myklebust {
6585d8d9a4dSTrond Myklebust 	rpc_destroy_authunix();
6595d8d9a4dSTrond Myklebust 	rpc_destroy_generic_auth();
6608e1f936bSRusty Russell 	unregister_shrinker(&rpc_cred_shrinker);
661f5c2187cSTrond Myklebust }
662