xref: /openbmc/linux/net/sunrpc/auth.c (revision 4c3ffd05)
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>
166a1a1e34SChuck Lever #include <linux/sunrpc/gss_api.h>
171da177e4SLinus Torvalds #include <linux/spinlock.h>
181da177e4SLinus Torvalds 
19f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
201da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_AUTH
211da177e4SLinus Torvalds #endif
221da177e4SLinus Torvalds 
23241269bdSTrond Myklebust #define RPC_CREDCACHE_DEFAULT_HASHBITS	(4)
24241269bdSTrond Myklebust struct rpc_cred_cache {
25241269bdSTrond Myklebust 	struct hlist_head	*hashtable;
26241269bdSTrond Myklebust 	unsigned int		hashbits;
27241269bdSTrond Myklebust 	spinlock_t		lock;
28241269bdSTrond Myklebust };
29241269bdSTrond Myklebust 
30241269bdSTrond Myklebust static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS;
31241269bdSTrond Myklebust 
32fc1b356fSTrond Myklebust static DEFINE_SPINLOCK(rpc_authflavor_lock);
33f1c0a861STrond Myklebust static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
341da177e4SLinus Torvalds 	&authnull_ops,		/* AUTH_NULL */
351da177e4SLinus Torvalds 	&authunix_ops,		/* AUTH_UNIX */
361da177e4SLinus Torvalds 	NULL,			/* others can be loadable modules */
371da177e4SLinus Torvalds };
381da177e4SLinus Torvalds 
39e092bdcdSTrond Myklebust static LIST_HEAD(cred_unused);
40f5c2187cSTrond Myklebust static unsigned long number_cred_unused;
41e092bdcdSTrond Myklebust 
42db5fe265SMiquel van Smoorenburg #define MAX_HASHTABLE_BITS (14)
438e4e15d4SStephen Rothwell static int param_set_hashtbl_sz(const char *val, const struct kernel_param *kp)
44241269bdSTrond Myklebust {
45241269bdSTrond Myklebust 	unsigned long num;
46241269bdSTrond Myklebust 	unsigned int nbits;
47241269bdSTrond Myklebust 	int ret;
48241269bdSTrond Myklebust 
49241269bdSTrond Myklebust 	if (!val)
50241269bdSTrond Myklebust 		goto out_inval;
5100cfaa94SDaniel Walter 	ret = kstrtoul(val, 0, &num);
52241269bdSTrond Myklebust 	if (ret == -EINVAL)
53241269bdSTrond Myklebust 		goto out_inval;
5434ae685cSFrank Sorenson 	nbits = fls(num - 1);
55241269bdSTrond Myklebust 	if (nbits > MAX_HASHTABLE_BITS || nbits < 2)
56241269bdSTrond Myklebust 		goto out_inval;
57241269bdSTrond Myklebust 	*(unsigned int *)kp->arg = nbits;
58241269bdSTrond Myklebust 	return 0;
59241269bdSTrond Myklebust out_inval:
60241269bdSTrond Myklebust 	return -EINVAL;
61241269bdSTrond Myklebust }
62241269bdSTrond Myklebust 
638e4e15d4SStephen Rothwell static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp)
64241269bdSTrond Myklebust {
65241269bdSTrond Myklebust 	unsigned int nbits;
66241269bdSTrond Myklebust 
67241269bdSTrond Myklebust 	nbits = *(unsigned int *)kp->arg;
68241269bdSTrond Myklebust 	return sprintf(buffer, "%u", 1U << nbits);
69241269bdSTrond Myklebust }
70241269bdSTrond Myklebust 
71241269bdSTrond Myklebust #define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
72241269bdSTrond Myklebust 
739c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_hashtbl_sz = {
748e4e15d4SStephen Rothwell 	.set = param_set_hashtbl_sz,
758e4e15d4SStephen Rothwell 	.get = param_get_hashtbl_sz,
768e4e15d4SStephen Rothwell };
778e4e15d4SStephen Rothwell 
78241269bdSTrond Myklebust module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
79241269bdSTrond Myklebust MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
80241269bdSTrond Myklebust 
81bae6746fSTrond Myklebust static unsigned long auth_max_cred_cachesize = ULONG_MAX;
82bae6746fSTrond Myklebust module_param(auth_max_cred_cachesize, ulong, 0644);
83bae6746fSTrond Myklebust MODULE_PARM_DESC(auth_max_cred_cachesize, "RPC credential maximum total cache size");
84bae6746fSTrond Myklebust 
851da177e4SLinus Torvalds static u32
861da177e4SLinus Torvalds pseudoflavor_to_flavor(u32 flavor) {
871c74a244SChuck Lever 	if (flavor > RPC_AUTH_MAXFLAVOR)
881da177e4SLinus Torvalds 		return RPC_AUTH_GSS;
891da177e4SLinus Torvalds 	return flavor;
901da177e4SLinus Torvalds }
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds int
93f1c0a861STrond Myklebust rpcauth_register(const struct rpc_authops *ops)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	rpc_authflavor_t flavor;
96fc1b356fSTrond Myklebust 	int ret = -EPERM;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
991da177e4SLinus Torvalds 		return -EINVAL;
100fc1b356fSTrond Myklebust 	spin_lock(&rpc_authflavor_lock);
101fc1b356fSTrond Myklebust 	if (auth_flavors[flavor] == NULL) {
1021da177e4SLinus Torvalds 		auth_flavors[flavor] = ops;
103fc1b356fSTrond Myklebust 		ret = 0;
104fc1b356fSTrond Myklebust 	}
105fc1b356fSTrond Myklebust 	spin_unlock(&rpc_authflavor_lock);
106fc1b356fSTrond Myklebust 	return ret;
1071da177e4SLinus Torvalds }
108e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_register);
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds int
111f1c0a861STrond Myklebust rpcauth_unregister(const struct rpc_authops *ops)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	rpc_authflavor_t flavor;
114fc1b356fSTrond Myklebust 	int ret = -EPERM;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
1171da177e4SLinus Torvalds 		return -EINVAL;
118fc1b356fSTrond Myklebust 	spin_lock(&rpc_authflavor_lock);
119fc1b356fSTrond Myklebust 	if (auth_flavors[flavor] == ops) {
1201da177e4SLinus Torvalds 		auth_flavors[flavor] = NULL;
121fc1b356fSTrond Myklebust 		ret = 0;
122fc1b356fSTrond Myklebust 	}
123fc1b356fSTrond Myklebust 	spin_unlock(&rpc_authflavor_lock);
124fc1b356fSTrond Myklebust 	return ret;
1251da177e4SLinus Torvalds }
126e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_unregister);
1271da177e4SLinus Torvalds 
1286a1a1e34SChuck Lever /**
1299568c5e9SChuck Lever  * rpcauth_get_pseudoflavor - check if security flavor is supported
1309568c5e9SChuck Lever  * @flavor: a security flavor
1319568c5e9SChuck Lever  * @info: a GSS mech OID, quality of protection, and service value
1329568c5e9SChuck Lever  *
1339568c5e9SChuck Lever  * Verifies that an appropriate kernel module is available or already loaded.
1349568c5e9SChuck Lever  * Returns an equivalent pseudoflavor, or RPC_AUTH_MAXFLAVOR if "flavor" is
1359568c5e9SChuck Lever  * not supported locally.
1369568c5e9SChuck Lever  */
1379568c5e9SChuck Lever rpc_authflavor_t
1389568c5e9SChuck Lever rpcauth_get_pseudoflavor(rpc_authflavor_t flavor, struct rpcsec_gss_info *info)
1399568c5e9SChuck Lever {
1409568c5e9SChuck Lever 	const struct rpc_authops *ops;
1419568c5e9SChuck Lever 	rpc_authflavor_t pseudoflavor;
1429568c5e9SChuck Lever 
1439568c5e9SChuck Lever 	ops = auth_flavors[flavor];
1449568c5e9SChuck Lever 	if (ops == NULL)
1459568c5e9SChuck Lever 		request_module("rpc-auth-%u", flavor);
1469568c5e9SChuck Lever 	spin_lock(&rpc_authflavor_lock);
1479568c5e9SChuck Lever 	ops = auth_flavors[flavor];
1489568c5e9SChuck Lever 	if (ops == NULL || !try_module_get(ops->owner)) {
1499568c5e9SChuck Lever 		spin_unlock(&rpc_authflavor_lock);
1509568c5e9SChuck Lever 		return RPC_AUTH_MAXFLAVOR;
1519568c5e9SChuck Lever 	}
1529568c5e9SChuck Lever 	spin_unlock(&rpc_authflavor_lock);
1539568c5e9SChuck Lever 
1549568c5e9SChuck Lever 	pseudoflavor = flavor;
1559568c5e9SChuck Lever 	if (ops->info2flavor != NULL)
1569568c5e9SChuck Lever 		pseudoflavor = ops->info2flavor(info);
1579568c5e9SChuck Lever 
1589568c5e9SChuck Lever 	module_put(ops->owner);
1599568c5e9SChuck Lever 	return pseudoflavor;
1609568c5e9SChuck Lever }
1619568c5e9SChuck Lever EXPORT_SYMBOL_GPL(rpcauth_get_pseudoflavor);
1629568c5e9SChuck Lever 
1639568c5e9SChuck Lever /**
164a77c806fSChuck Lever  * rpcauth_get_gssinfo - find GSS tuple matching a GSS pseudoflavor
165a77c806fSChuck Lever  * @pseudoflavor: GSS pseudoflavor to match
166a77c806fSChuck Lever  * @info: rpcsec_gss_info structure to fill in
167a77c806fSChuck Lever  *
168a77c806fSChuck Lever  * Returns zero and fills in "info" if pseudoflavor matches a
169a77c806fSChuck Lever  * supported mechanism.
170a77c806fSChuck Lever  */
171a77c806fSChuck Lever int
172a77c806fSChuck Lever rpcauth_get_gssinfo(rpc_authflavor_t pseudoflavor, struct rpcsec_gss_info *info)
173a77c806fSChuck Lever {
174a77c806fSChuck Lever 	rpc_authflavor_t flavor = pseudoflavor_to_flavor(pseudoflavor);
175a77c806fSChuck Lever 	const struct rpc_authops *ops;
176a77c806fSChuck Lever 	int result;
177a77c806fSChuck Lever 
1781c74a244SChuck Lever 	if (flavor >= RPC_AUTH_MAXFLAVOR)
1791c74a244SChuck Lever 		return -EINVAL;
1801c74a244SChuck Lever 
181a77c806fSChuck Lever 	ops = auth_flavors[flavor];
182a77c806fSChuck Lever 	if (ops == NULL)
183a77c806fSChuck Lever 		request_module("rpc-auth-%u", flavor);
184a77c806fSChuck Lever 	spin_lock(&rpc_authflavor_lock);
185a77c806fSChuck Lever 	ops = auth_flavors[flavor];
186a77c806fSChuck Lever 	if (ops == NULL || !try_module_get(ops->owner)) {
187a77c806fSChuck Lever 		spin_unlock(&rpc_authflavor_lock);
188a77c806fSChuck Lever 		return -ENOENT;
189a77c806fSChuck Lever 	}
190a77c806fSChuck Lever 	spin_unlock(&rpc_authflavor_lock);
191a77c806fSChuck Lever 
192a77c806fSChuck Lever 	result = -ENOENT;
193a77c806fSChuck Lever 	if (ops->flavor2info != NULL)
194a77c806fSChuck Lever 		result = ops->flavor2info(pseudoflavor, info);
195a77c806fSChuck Lever 
196a77c806fSChuck Lever 	module_put(ops->owner);
197a77c806fSChuck Lever 	return result;
198a77c806fSChuck Lever }
199a77c806fSChuck Lever EXPORT_SYMBOL_GPL(rpcauth_get_gssinfo);
200a77c806fSChuck Lever 
201a77c806fSChuck Lever /**
2026a1a1e34SChuck Lever  * rpcauth_list_flavors - discover registered flavors and pseudoflavors
2036a1a1e34SChuck Lever  * @array: array to fill in
2046a1a1e34SChuck Lever  * @size: size of "array"
2056a1a1e34SChuck Lever  *
2066a1a1e34SChuck Lever  * Returns the number of array items filled in, or a negative errno.
2076a1a1e34SChuck Lever  *
2086a1a1e34SChuck Lever  * The returned array is not sorted by any policy.  Callers should not
2096a1a1e34SChuck Lever  * rely on the order of the items in the returned array.
2106a1a1e34SChuck Lever  */
2116a1a1e34SChuck Lever int
2126a1a1e34SChuck Lever rpcauth_list_flavors(rpc_authflavor_t *array, int size)
2136a1a1e34SChuck Lever {
2146a1a1e34SChuck Lever 	rpc_authflavor_t flavor;
2156a1a1e34SChuck Lever 	int result = 0;
2166a1a1e34SChuck Lever 
2176a1a1e34SChuck Lever 	spin_lock(&rpc_authflavor_lock);
2186a1a1e34SChuck Lever 	for (flavor = 0; flavor < RPC_AUTH_MAXFLAVOR; flavor++) {
2196a1a1e34SChuck Lever 		const struct rpc_authops *ops = auth_flavors[flavor];
2206a1a1e34SChuck Lever 		rpc_authflavor_t pseudos[4];
2216a1a1e34SChuck Lever 		int i, len;
2226a1a1e34SChuck Lever 
2236a1a1e34SChuck Lever 		if (result >= size) {
2246a1a1e34SChuck Lever 			result = -ENOMEM;
2256a1a1e34SChuck Lever 			break;
2266a1a1e34SChuck Lever 		}
2276a1a1e34SChuck Lever 
2286a1a1e34SChuck Lever 		if (ops == NULL)
2296a1a1e34SChuck Lever 			continue;
2306a1a1e34SChuck Lever 		if (ops->list_pseudoflavors == NULL) {
2316a1a1e34SChuck Lever 			array[result++] = ops->au_flavor;
2326a1a1e34SChuck Lever 			continue;
2336a1a1e34SChuck Lever 		}
2346a1a1e34SChuck Lever 		len = ops->list_pseudoflavors(pseudos, ARRAY_SIZE(pseudos));
2356a1a1e34SChuck Lever 		if (len < 0) {
2366a1a1e34SChuck Lever 			result = len;
2376a1a1e34SChuck Lever 			break;
2386a1a1e34SChuck Lever 		}
2396a1a1e34SChuck Lever 		for (i = 0; i < len; i++) {
2406a1a1e34SChuck Lever 			if (result >= size) {
2416a1a1e34SChuck Lever 				result = -ENOMEM;
2426a1a1e34SChuck Lever 				break;
2436a1a1e34SChuck Lever 			}
2446a1a1e34SChuck Lever 			array[result++] = pseudos[i];
2456a1a1e34SChuck Lever 		}
2466a1a1e34SChuck Lever 	}
2476a1a1e34SChuck Lever 	spin_unlock(&rpc_authflavor_lock);
2486a1a1e34SChuck Lever 
2496a1a1e34SChuck Lever 	dprintk("RPC:       %s returns %d\n", __func__, result);
2506a1a1e34SChuck Lever 	return result;
2516a1a1e34SChuck Lever }
2526a1a1e34SChuck Lever EXPORT_SYMBOL_GPL(rpcauth_list_flavors);
2536a1a1e34SChuck Lever 
2541da177e4SLinus Torvalds struct rpc_auth *
255c2190661STrond Myklebust rpcauth_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
2561da177e4SLinus Torvalds {
2571da177e4SLinus Torvalds 	struct rpc_auth		*auth;
258f1c0a861STrond Myklebust 	const struct rpc_authops *ops;
259c2190661STrond Myklebust 	u32			flavor = pseudoflavor_to_flavor(args->pseudoflavor);
2601da177e4SLinus Torvalds 
261f344f6dfSOlaf Kirch 	auth = ERR_PTR(-EINVAL);
262f344f6dfSOlaf Kirch 	if (flavor >= RPC_AUTH_MAXFLAVOR)
263f344f6dfSOlaf Kirch 		goto out;
264f344f6dfSOlaf Kirch 
265f344f6dfSOlaf Kirch 	if ((ops = auth_flavors[flavor]) == NULL)
266f344f6dfSOlaf Kirch 		request_module("rpc-auth-%u", flavor);
267fc1b356fSTrond Myklebust 	spin_lock(&rpc_authflavor_lock);
268fc1b356fSTrond Myklebust 	ops = auth_flavors[flavor];
269fc1b356fSTrond Myklebust 	if (ops == NULL || !try_module_get(ops->owner)) {
270fc1b356fSTrond Myklebust 		spin_unlock(&rpc_authflavor_lock);
271f344f6dfSOlaf Kirch 		goto out;
272fc1b356fSTrond Myklebust 	}
273fc1b356fSTrond Myklebust 	spin_unlock(&rpc_authflavor_lock);
274c2190661STrond Myklebust 	auth = ops->create(args, clnt);
275fc1b356fSTrond Myklebust 	module_put(ops->owner);
2766a19275aSJ. Bruce Fields 	if (IS_ERR(auth))
2776a19275aSJ. Bruce Fields 		return auth;
2781da177e4SLinus Torvalds 	if (clnt->cl_auth)
279de7a8ce3STrond Myklebust 		rpcauth_release(clnt->cl_auth);
2801da177e4SLinus Torvalds 	clnt->cl_auth = auth;
281f344f6dfSOlaf Kirch 
282f344f6dfSOlaf Kirch out:
2831da177e4SLinus Torvalds 	return auth;
2841da177e4SLinus Torvalds }
285e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_create);
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds void
288de7a8ce3STrond Myklebust rpcauth_release(struct rpc_auth *auth)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds 	if (!atomic_dec_and_test(&auth->au_count))
2911da177e4SLinus Torvalds 		return;
2921da177e4SLinus Torvalds 	auth->au_ops->destroy(auth);
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds static DEFINE_SPINLOCK(rpc_credcache_lock);
2961da177e4SLinus Torvalds 
29731be5bf1STrond Myklebust static void
29831be5bf1STrond Myklebust rpcauth_unhash_cred_locked(struct rpc_cred *cred)
29931be5bf1STrond Myklebust {
30031be5bf1STrond Myklebust 	hlist_del_rcu(&cred->cr_hash);
3014e857c58SPeter Zijlstra 	smp_mb__before_atomic();
30231be5bf1STrond Myklebust 	clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
30331be5bf1STrond Myklebust }
30431be5bf1STrond Myklebust 
305f0380f3dSTrond Myklebust static int
3069499b434STrond Myklebust rpcauth_unhash_cred(struct rpc_cred *cred)
3079499b434STrond Myklebust {
3089499b434STrond Myklebust 	spinlock_t *cache_lock;
309f0380f3dSTrond Myklebust 	int ret;
3109499b434STrond Myklebust 
3119499b434STrond Myklebust 	cache_lock = &cred->cr_auth->au_credcache->lock;
3129499b434STrond Myklebust 	spin_lock(cache_lock);
313f0380f3dSTrond Myklebust 	ret = atomic_read(&cred->cr_count) == 0;
314f0380f3dSTrond Myklebust 	if (ret)
3159499b434STrond Myklebust 		rpcauth_unhash_cred_locked(cred);
3169499b434STrond Myklebust 	spin_unlock(cache_lock);
317f0380f3dSTrond Myklebust 	return ret;
3189499b434STrond Myklebust }
3199499b434STrond Myklebust 
3201da177e4SLinus Torvalds /*
3211da177e4SLinus Torvalds  * Initialize RPC credential cache
3221da177e4SLinus Torvalds  */
3231da177e4SLinus Torvalds int
324f5c2187cSTrond Myklebust rpcauth_init_credcache(struct rpc_auth *auth)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	struct rpc_cred_cache *new;
327988664a0STrond Myklebust 	unsigned int hashsize;
3281da177e4SLinus Torvalds 
3298b3a7005SKris Katterjohn 	new = kmalloc(sizeof(*new), GFP_KERNEL);
3301da177e4SLinus Torvalds 	if (!new)
331241269bdSTrond Myklebust 		goto out_nocache;
332241269bdSTrond Myklebust 	new->hashbits = auth_hashbits;
333988664a0STrond Myklebust 	hashsize = 1U << new->hashbits;
334241269bdSTrond Myklebust 	new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
335241269bdSTrond Myklebust 	if (!new->hashtable)
336241269bdSTrond Myklebust 		goto out_nohashtbl;
3379499b434STrond Myklebust 	spin_lock_init(&new->lock);
3381da177e4SLinus Torvalds 	auth->au_credcache = new;
3391da177e4SLinus Torvalds 	return 0;
340241269bdSTrond Myklebust out_nohashtbl:
341241269bdSTrond Myklebust 	kfree(new);
342241269bdSTrond Myklebust out_nocache:
343241269bdSTrond Myklebust 	return -ENOMEM;
3441da177e4SLinus Torvalds }
345e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds /*
3484de6caa2SAndy Adamson  * Setup a credential key lifetime timeout notification
3494de6caa2SAndy Adamson  */
3504de6caa2SAndy Adamson int
3514de6caa2SAndy Adamson rpcauth_key_timeout_notify(struct rpc_auth *auth, struct rpc_cred *cred)
3524de6caa2SAndy Adamson {
3534de6caa2SAndy Adamson 	if (!cred->cr_auth->au_ops->key_timeout)
3544de6caa2SAndy Adamson 		return 0;
3554de6caa2SAndy Adamson 	return cred->cr_auth->au_ops->key_timeout(auth, cred);
3564de6caa2SAndy Adamson }
3574de6caa2SAndy Adamson EXPORT_SYMBOL_GPL(rpcauth_key_timeout_notify);
3584de6caa2SAndy Adamson 
3594de6caa2SAndy Adamson bool
360ce52914eSScott Mayhew rpcauth_cred_key_to_expire(struct rpc_auth *auth, struct rpc_cred *cred)
3614de6caa2SAndy Adamson {
362ce52914eSScott Mayhew 	if (auth->au_flags & RPCAUTH_AUTH_NO_CRKEY_TIMEOUT)
363ce52914eSScott Mayhew 		return false;
3644de6caa2SAndy Adamson 	if (!cred->cr_ops->crkey_to_expire)
3654de6caa2SAndy Adamson 		return false;
3664de6caa2SAndy Adamson 	return cred->cr_ops->crkey_to_expire(cred);
3674de6caa2SAndy Adamson }
3684de6caa2SAndy Adamson EXPORT_SYMBOL_GPL(rpcauth_cred_key_to_expire);
3694de6caa2SAndy Adamson 
370a0337d1dSJeff Layton char *
371a0337d1dSJeff Layton rpcauth_stringify_acceptor(struct rpc_cred *cred)
372a0337d1dSJeff Layton {
373a0337d1dSJeff Layton 	if (!cred->cr_ops->crstringify_acceptor)
374a0337d1dSJeff Layton 		return NULL;
375a0337d1dSJeff Layton 	return cred->cr_ops->crstringify_acceptor(cred);
376a0337d1dSJeff Layton }
377a0337d1dSJeff Layton EXPORT_SYMBOL_GPL(rpcauth_stringify_acceptor);
378a0337d1dSJeff Layton 
3794de6caa2SAndy Adamson /*
3801da177e4SLinus Torvalds  * Destroy a list of credentials
3811da177e4SLinus Torvalds  */
3821da177e4SLinus Torvalds static inline
383e092bdcdSTrond Myklebust void rpcauth_destroy_credlist(struct list_head *head)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	struct rpc_cred *cred;
3861da177e4SLinus Torvalds 
387e092bdcdSTrond Myklebust 	while (!list_empty(head)) {
388e092bdcdSTrond Myklebust 		cred = list_entry(head->next, struct rpc_cred, cr_lru);
389e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
3901da177e4SLinus Torvalds 		put_rpccred(cred);
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds /*
3951da177e4SLinus Torvalds  * Clear the RPC credential cache, and delete those credentials
3961da177e4SLinus Torvalds  * that are not referenced.
3971da177e4SLinus Torvalds  */
3981da177e4SLinus Torvalds void
3993ab9bb72STrond Myklebust rpcauth_clear_credcache(struct rpc_cred_cache *cache)
4001da177e4SLinus Torvalds {
401e092bdcdSTrond Myklebust 	LIST_HEAD(free);
402e092bdcdSTrond Myklebust 	struct hlist_head *head;
4031da177e4SLinus Torvalds 	struct rpc_cred	*cred;
404988664a0STrond Myklebust 	unsigned int hashsize = 1U << cache->hashbits;
4051da177e4SLinus Torvalds 	int		i;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	spin_lock(&rpc_credcache_lock);
4089499b434STrond Myklebust 	spin_lock(&cache->lock);
409988664a0STrond Myklebust 	for (i = 0; i < hashsize; i++) {
410e092bdcdSTrond Myklebust 		head = &cache->hashtable[i];
411e092bdcdSTrond Myklebust 		while (!hlist_empty(head)) {
412e092bdcdSTrond Myklebust 			cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
413e092bdcdSTrond Myklebust 			get_rpccred(cred);
414f5c2187cSTrond Myklebust 			if (!list_empty(&cred->cr_lru)) {
415f5c2187cSTrond Myklebust 				list_del(&cred->cr_lru);
416f5c2187cSTrond Myklebust 				number_cred_unused--;
417f5c2187cSTrond Myklebust 			}
418f5c2187cSTrond Myklebust 			list_add_tail(&cred->cr_lru, &free);
41931be5bf1STrond Myklebust 			rpcauth_unhash_cred_locked(cred);
4201da177e4SLinus Torvalds 		}
4211da177e4SLinus Torvalds 	}
4229499b434STrond Myklebust 	spin_unlock(&cache->lock);
4231da177e4SLinus Torvalds 	spin_unlock(&rpc_credcache_lock);
4241da177e4SLinus Torvalds 	rpcauth_destroy_credlist(&free);
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
4273ab9bb72STrond Myklebust /*
4283ab9bb72STrond Myklebust  * Destroy the RPC credential cache
4293ab9bb72STrond Myklebust  */
4303ab9bb72STrond Myklebust void
4313ab9bb72STrond Myklebust rpcauth_destroy_credcache(struct rpc_auth *auth)
4323ab9bb72STrond Myklebust {
4333ab9bb72STrond Myklebust 	struct rpc_cred_cache *cache = auth->au_credcache;
4343ab9bb72STrond Myklebust 
4353ab9bb72STrond Myklebust 	if (cache) {
4363ab9bb72STrond Myklebust 		auth->au_credcache = NULL;
4373ab9bb72STrond Myklebust 		rpcauth_clear_credcache(cache);
438241269bdSTrond Myklebust 		kfree(cache->hashtable);
4393ab9bb72STrond Myklebust 		kfree(cache);
4403ab9bb72STrond Myklebust 	}
4413ab9bb72STrond Myklebust }
442e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
4433ab9bb72STrond Myklebust 
444d2b83141STrond Myklebust 
445d2b83141STrond Myklebust #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
446d2b83141STrond Myklebust 
4471da177e4SLinus Torvalds /*
4481da177e4SLinus Torvalds  * Remove stale credentials. Avoid sleeping inside the loop.
4491da177e4SLinus Torvalds  */
45070534a73SDave Chinner static long
451f5c2187cSTrond Myklebust rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
4521da177e4SLinus Torvalds {
4539499b434STrond Myklebust 	spinlock_t *cache_lock;
454eac0d18dSTrond Myklebust 	struct rpc_cred *cred, *next;
455d2b83141STrond Myklebust 	unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
45670534a73SDave Chinner 	long freed = 0;
4571da177e4SLinus Torvalds 
458eac0d18dSTrond Myklebust 	list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
459eac0d18dSTrond Myklebust 
46020673406STrond Myklebust 		if (nr_to_scan-- == 0)
46120673406STrond Myklebust 			break;
46293a05e65STrond Myklebust 		/*
46393a05e65STrond Myklebust 		 * Enforce a 60 second garbage collection moratorium
46493a05e65STrond Myklebust 		 * Note that the cred_unused list must be time-ordered.
46593a05e65STrond Myklebust 		 */
4663d7b0894STrond Myklebust 		if (time_in_range(cred->cr_expire, expired, jiffies) &&
4674c3ffd05SNeilBrown 		    test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
4684c3ffd05SNeilBrown 			freed = SHRINK_STOP;
46970534a73SDave Chinner 			break;
4704c3ffd05SNeilBrown 		}
471eac0d18dSTrond Myklebust 
472e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
473f5c2187cSTrond Myklebust 		number_cred_unused--;
47470534a73SDave Chinner 		freed++;
475e092bdcdSTrond Myklebust 		if (atomic_read(&cred->cr_count) != 0)
476e092bdcdSTrond Myklebust 			continue;
477eac0d18dSTrond Myklebust 
4789499b434STrond Myklebust 		cache_lock = &cred->cr_auth->au_credcache->lock;
4799499b434STrond Myklebust 		spin_lock(cache_lock);
4809499b434STrond Myklebust 		if (atomic_read(&cred->cr_count) == 0) {
481e092bdcdSTrond Myklebust 			get_rpccred(cred);
482e092bdcdSTrond Myklebust 			list_add_tail(&cred->cr_lru, free);
48331be5bf1STrond Myklebust 			rpcauth_unhash_cred_locked(cred);
4841da177e4SLinus Torvalds 		}
4859499b434STrond Myklebust 		spin_unlock(cache_lock);
4869499b434STrond Myklebust 	}
48770534a73SDave Chinner 	return freed;
4881da177e4SLinus Torvalds }
489e092bdcdSTrond Myklebust 
490bae6746fSTrond Myklebust static unsigned long
491bae6746fSTrond Myklebust rpcauth_cache_do_shrink(int nr_to_scan)
492bae6746fSTrond Myklebust {
493bae6746fSTrond Myklebust 	LIST_HEAD(free);
494bae6746fSTrond Myklebust 	unsigned long freed;
495bae6746fSTrond Myklebust 
496bae6746fSTrond Myklebust 	spin_lock(&rpc_credcache_lock);
497bae6746fSTrond Myklebust 	freed = rpcauth_prune_expired(&free, nr_to_scan);
498bae6746fSTrond Myklebust 	spin_unlock(&rpc_credcache_lock);
499bae6746fSTrond Myklebust 	rpcauth_destroy_credlist(&free);
500bae6746fSTrond Myklebust 
501bae6746fSTrond Myklebust 	return freed;
502bae6746fSTrond Myklebust }
503bae6746fSTrond Myklebust 
504e092bdcdSTrond Myklebust /*
505f5c2187cSTrond Myklebust  * Run memory cache shrinker.
506e092bdcdSTrond Myklebust  */
50770534a73SDave Chinner static unsigned long
50870534a73SDave Chinner rpcauth_cache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
50970534a73SDave Chinner 
510e092bdcdSTrond Myklebust {
51170534a73SDave Chinner 	if ((sc->gfp_mask & GFP_KERNEL) != GFP_KERNEL)
51270534a73SDave Chinner 		return SHRINK_STOP;
51370534a73SDave Chinner 
51470534a73SDave Chinner 	/* nothing left, don't come back */
515f5c2187cSTrond Myklebust 	if (list_empty(&cred_unused))
51670534a73SDave Chinner 		return SHRINK_STOP;
51770534a73SDave Chinner 
518bae6746fSTrond Myklebust 	return rpcauth_cache_do_shrink(sc->nr_to_scan);
51970534a73SDave Chinner }
52070534a73SDave Chinner 
52170534a73SDave Chinner static unsigned long
52270534a73SDave Chinner rpcauth_cache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
52370534a73SDave Chinner 
52470534a73SDave Chinner {
5254c3ffd05SNeilBrown 	return number_cred_unused * sysctl_vfs_cache_pressure / 100;
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
528bae6746fSTrond Myklebust static void
529bae6746fSTrond Myklebust rpcauth_cache_enforce_limit(void)
530bae6746fSTrond Myklebust {
531bae6746fSTrond Myklebust 	unsigned long diff;
532bae6746fSTrond Myklebust 	unsigned int nr_to_scan;
533bae6746fSTrond Myklebust 
534bae6746fSTrond Myklebust 	if (number_cred_unused <= auth_max_cred_cachesize)
535bae6746fSTrond Myklebust 		return;
536bae6746fSTrond Myklebust 	diff = number_cred_unused - auth_max_cred_cachesize;
537bae6746fSTrond Myklebust 	nr_to_scan = 100;
538bae6746fSTrond Myklebust 	if (diff < nr_to_scan)
539bae6746fSTrond Myklebust 		nr_to_scan = diff;
540bae6746fSTrond Myklebust 	rpcauth_cache_do_shrink(nr_to_scan);
541bae6746fSTrond Myklebust }
542bae6746fSTrond Myklebust 
5431da177e4SLinus Torvalds /*
5441da177e4SLinus Torvalds  * Look up a process' credentials in the authentication cache
5451da177e4SLinus Torvalds  */
5461da177e4SLinus Torvalds struct rpc_cred *
5471da177e4SLinus Torvalds rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
5483c6e0bc8SJeff Layton 		int flags, gfp_t gfp)
5491da177e4SLinus Torvalds {
550e092bdcdSTrond Myklebust 	LIST_HEAD(free);
5511da177e4SLinus Torvalds 	struct rpc_cred_cache *cache = auth->au_credcache;
55231be5bf1STrond Myklebust 	struct rpc_cred	*cred = NULL,
55331be5bf1STrond Myklebust 			*entry, *new;
55425337fdcSTrond Myklebust 	unsigned int nr;
55525337fdcSTrond Myklebust 
55666cbd4baSFrank Sorenson 	nr = auth->au_ops->hash_cred(acred, cache->hashbits);
5571da177e4SLinus Torvalds 
55831be5bf1STrond Myklebust 	rcu_read_lock();
559b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
56031be5bf1STrond Myklebust 		if (!entry->cr_ops->crmatch(acred, entry, flags))
56131be5bf1STrond Myklebust 			continue;
562bd956080SNeilBrown 		if (flags & RPCAUTH_LOOKUP_RCU) {
563bd956080SNeilBrown 			if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) &&
564bd956080SNeilBrown 			    !test_bit(RPCAUTH_CRED_NEW, &entry->cr_flags))
565bd956080SNeilBrown 				cred = entry;
566bd956080SNeilBrown 			break;
567bd956080SNeilBrown 		}
5689499b434STrond Myklebust 		spin_lock(&cache->lock);
56931be5bf1STrond Myklebust 		if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
5709499b434STrond Myklebust 			spin_unlock(&cache->lock);
57131be5bf1STrond Myklebust 			continue;
57231be5bf1STrond Myklebust 		}
57331be5bf1STrond Myklebust 		cred = get_rpccred(entry);
5749499b434STrond Myklebust 		spin_unlock(&cache->lock);
57531be5bf1STrond Myklebust 		break;
57631be5bf1STrond Myklebust 	}
57731be5bf1STrond Myklebust 	rcu_read_unlock();
57831be5bf1STrond Myklebust 
5799499b434STrond Myklebust 	if (cred != NULL)
58031be5bf1STrond Myklebust 		goto found;
58131be5bf1STrond Myklebust 
582bd956080SNeilBrown 	if (flags & RPCAUTH_LOOKUP_RCU)
583bd956080SNeilBrown 		return ERR_PTR(-ECHILD);
584bd956080SNeilBrown 
5853c6e0bc8SJeff Layton 	new = auth->au_ops->crcreate(auth, acred, flags, gfp);
58631be5bf1STrond Myklebust 	if (IS_ERR(new)) {
58731be5bf1STrond Myklebust 		cred = new;
58831be5bf1STrond Myklebust 		goto out;
58931be5bf1STrond Myklebust 	}
59031be5bf1STrond Myklebust 
5919499b434STrond Myklebust 	spin_lock(&cache->lock);
592b67bfe0dSSasha Levin 	hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
593e092bdcdSTrond Myklebust 		if (!entry->cr_ops->crmatch(acred, entry, flags))
594e092bdcdSTrond Myklebust 			continue;
595e092bdcdSTrond Myklebust 		cred = get_rpccred(entry);
5961da177e4SLinus Torvalds 		break;
5971da177e4SLinus Torvalds 	}
59831be5bf1STrond Myklebust 	if (cred == NULL) {
59931be5bf1STrond Myklebust 		cred = new;
60031be5bf1STrond Myklebust 		set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
60131be5bf1STrond Myklebust 		hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
60231be5bf1STrond Myklebust 	} else
603e092bdcdSTrond Myklebust 		list_add_tail(&new->cr_lru, &free);
6049499b434STrond Myklebust 	spin_unlock(&cache->lock);
605bae6746fSTrond Myklebust 	rpcauth_cache_enforce_limit();
60631be5bf1STrond Myklebust found:
607f64f9e71SJoe Perches 	if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
608f64f9e71SJoe Perches 	    cred->cr_ops->cr_init != NULL &&
609f64f9e71SJoe Perches 	    !(flags & RPCAUTH_LOOKUP_NEW)) {
610fba3bad4STrond Myklebust 		int res = cred->cr_ops->cr_init(auth, cred);
611fba3bad4STrond Myklebust 		if (res < 0) {
612fba3bad4STrond Myklebust 			put_rpccred(cred);
613fba3bad4STrond Myklebust 			cred = ERR_PTR(res);
614fba3bad4STrond Myklebust 		}
6151da177e4SLinus Torvalds 	}
61631be5bf1STrond Myklebust 	rpcauth_destroy_credlist(&free);
61731be5bf1STrond Myklebust out:
61831be5bf1STrond Myklebust 	return cred;
6191da177e4SLinus Torvalds }
620e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds struct rpc_cred *
6238a317760STrond Myklebust rpcauth_lookupcred(struct rpc_auth *auth, int flags)
6241da177e4SLinus Torvalds {
62586a264abSDavid Howells 	struct auth_cred acred;
6261da177e4SLinus Torvalds 	struct rpc_cred *ret;
62786a264abSDavid Howells 	const struct cred *cred = current_cred();
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	dprintk("RPC:       looking up %s cred\n",
6301da177e4SLinus Torvalds 		auth->au_ops->au_name);
63186a264abSDavid Howells 
63286a264abSDavid Howells 	memset(&acred, 0, sizeof(acred));
63386a264abSDavid Howells 	acred.uid = cred->fsuid;
63486a264abSDavid Howells 	acred.gid = cred->fsgid;
635122a8cdaSNeilBrown 	acred.group_info = cred->group_info;
6368a317760STrond Myklebust 	ret = auth->au_ops->lookup_cred(auth, &acred, flags);
6371da177e4SLinus Torvalds 	return ret;
6381da177e4SLinus Torvalds }
63966b06860SAndy Adamson EXPORT_SYMBOL_GPL(rpcauth_lookupcred);
6401da177e4SLinus Torvalds 
6415fe4755eSTrond Myklebust void
6425fe4755eSTrond Myklebust rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
6435fe4755eSTrond Myklebust 		  struct rpc_auth *auth, const struct rpc_credops *ops)
6445fe4755eSTrond Myklebust {
6455fe4755eSTrond Myklebust 	INIT_HLIST_NODE(&cred->cr_hash);
646e092bdcdSTrond Myklebust 	INIT_LIST_HEAD(&cred->cr_lru);
6475fe4755eSTrond Myklebust 	atomic_set(&cred->cr_count, 1);
6485fe4755eSTrond Myklebust 	cred->cr_auth = auth;
6495fe4755eSTrond Myklebust 	cred->cr_ops = ops;
6505fe4755eSTrond Myklebust 	cred->cr_expire = jiffies;
651f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
6525fe4755eSTrond Myklebust 	cred->cr_magic = RPCAUTH_CRED_MAGIC;
6535fe4755eSTrond Myklebust #endif
6545fe4755eSTrond Myklebust 	cred->cr_uid = acred->uid;
6555fe4755eSTrond Myklebust }
656e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_init_cred);
6575fe4755eSTrond Myklebust 
6588572b8e2STrond Myklebust struct rpc_cred *
6595d351754STrond Myklebust rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
6604ccda2cdSTrond Myklebust {
6614ccda2cdSTrond Myklebust 	dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
6624ccda2cdSTrond Myklebust 			cred->cr_auth->au_ops->au_name, cred);
6638572b8e2STrond Myklebust 	return get_rpccred(cred);
6644ccda2cdSTrond Myklebust }
6655c691044STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
6664ccda2cdSTrond Myklebust 
6678572b8e2STrond Myklebust static struct rpc_cred *
6685d351754STrond Myklebust rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
6691da177e4SLinus Torvalds {
6701be27f36STrond Myklebust 	struct rpc_auth *auth = task->tk_client->cl_auth;
6711da177e4SLinus Torvalds 	struct auth_cred acred = {
672bf37f794SEric W. Biederman 		.uid = GLOBAL_ROOT_UID,
673bf37f794SEric W. Biederman 		.gid = GLOBAL_ROOT_GID,
6741da177e4SLinus Torvalds 	};
6751da177e4SLinus Torvalds 
67646121cf7SChuck Lever 	dprintk("RPC: %5u looking up %s cred\n",
6771be27f36STrond Myklebust 		task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
6788572b8e2STrond Myklebust 	return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
679af093835STrond Myklebust }
680af093835STrond Myklebust 
6818572b8e2STrond Myklebust static struct rpc_cred *
6825d351754STrond Myklebust rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
683af093835STrond Myklebust {
684af093835STrond Myklebust 	struct rpc_auth *auth = task->tk_client->cl_auth;
685af093835STrond Myklebust 
686af093835STrond Myklebust 	dprintk("RPC: %5u looking up %s cred\n",
687af093835STrond Myklebust 		task->tk_pid, auth->au_ops->au_name);
6888572b8e2STrond Myklebust 	return rpcauth_lookupcred(auth, lookupflags);
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
691a17c2153STrond Myklebust static int
6924ccda2cdSTrond Myklebust rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
6931da177e4SLinus Torvalds {
694a17c2153STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
6958572b8e2STrond Myklebust 	struct rpc_cred *new;
6965d351754STrond Myklebust 	int lookupflags = 0;
6975d351754STrond Myklebust 
6985d351754STrond Myklebust 	if (flags & RPC_TASK_ASYNC)
6995d351754STrond Myklebust 		lookupflags |= RPCAUTH_LOOKUP_NEW;
7004ccda2cdSTrond Myklebust 	if (cred != NULL)
7018572b8e2STrond Myklebust 		new = cred->cr_ops->crbind(task, cred, lookupflags);
7024ccda2cdSTrond Myklebust 	else if (flags & RPC_TASK_ROOTCREDS)
7038572b8e2STrond Myklebust 		new = rpcauth_bind_root_cred(task, lookupflags);
7044ccda2cdSTrond Myklebust 	else
7058572b8e2STrond Myklebust 		new = rpcauth_bind_new_cred(task, lookupflags);
7068572b8e2STrond Myklebust 	if (IS_ERR(new))
7078572b8e2STrond Myklebust 		return PTR_ERR(new);
708a17c2153STrond Myklebust 	put_rpccred(req->rq_cred);
709a17c2153STrond Myklebust 	req->rq_cred = new;
7108572b8e2STrond Myklebust 	return 0;
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds void
7141da177e4SLinus Torvalds put_rpccred(struct rpc_cred *cred)
7151da177e4SLinus Torvalds {
7169a8f6b5eSTrond Myklebust 	if (cred == NULL)
7179a8f6b5eSTrond Myklebust 		return;
718e092bdcdSTrond Myklebust 	/* Fast path for unhashed credentials */
719f0380f3dSTrond Myklebust 	if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
720f0380f3dSTrond Myklebust 		if (atomic_dec_and_test(&cred->cr_count))
721f0380f3dSTrond Myklebust 			cred->cr_ops->crdestroy(cred);
7221da177e4SLinus Torvalds 		return;
723f0380f3dSTrond Myklebust 	}
724f0380f3dSTrond Myklebust 
725e092bdcdSTrond Myklebust 	if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
726e092bdcdSTrond Myklebust 		return;
727f5c2187cSTrond Myklebust 	if (!list_empty(&cred->cr_lru)) {
728f5c2187cSTrond Myklebust 		number_cred_unused--;
729e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
730f5c2187cSTrond Myklebust 	}
7315f707eb4STrond Myklebust 	if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
732f0380f3dSTrond Myklebust 		if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
733e092bdcdSTrond Myklebust 			cred->cr_expire = jiffies;
734e092bdcdSTrond Myklebust 			list_add_tail(&cred->cr_lru, &cred_unused);
735f5c2187cSTrond Myklebust 			number_cred_unused++;
736f0380f3dSTrond Myklebust 			goto out_nodestroy;
737f0380f3dSTrond Myklebust 		}
738f0380f3dSTrond Myklebust 		if (!rpcauth_unhash_cred(cred)) {
739f0380f3dSTrond Myklebust 			/* We were hashed and someone looked us up... */
740f0380f3dSTrond Myklebust 			goto out_nodestroy;
741f0380f3dSTrond Myklebust 		}
742e092bdcdSTrond Myklebust 	}
743e092bdcdSTrond Myklebust 	spin_unlock(&rpc_credcache_lock);
7441da177e4SLinus Torvalds 	cred->cr_ops->crdestroy(cred);
745f0380f3dSTrond Myklebust 	return;
746f0380f3dSTrond Myklebust out_nodestroy:
747f0380f3dSTrond Myklebust 	spin_unlock(&rpc_credcache_lock);
7481da177e4SLinus Torvalds }
749e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(put_rpccred);
7501da177e4SLinus Torvalds 
751d8ed029dSAlexey Dobriyan __be32 *
752d8ed029dSAlexey Dobriyan rpcauth_marshcred(struct rpc_task *task, __be32 *p)
7531da177e4SLinus Torvalds {
754a17c2153STrond Myklebust 	struct rpc_cred	*cred = task->tk_rqstp->rq_cred;
7551da177e4SLinus Torvalds 
75646121cf7SChuck Lever 	dprintk("RPC: %5u marshaling %s cred %p\n",
7571be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
7580bbacc40SChuck Lever 
7591da177e4SLinus Torvalds 	return cred->cr_ops->crmarshal(task, p);
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
762d8ed029dSAlexey Dobriyan __be32 *
763d8ed029dSAlexey Dobriyan rpcauth_checkverf(struct rpc_task *task, __be32 *p)
7641da177e4SLinus Torvalds {
765a17c2153STrond Myklebust 	struct rpc_cred	*cred = task->tk_rqstp->rq_cred;
7661da177e4SLinus Torvalds 
76746121cf7SChuck Lever 	dprintk("RPC: %5u validating %s cred %p\n",
7681be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
7690bbacc40SChuck Lever 
7701da177e4SLinus Torvalds 	return cred->cr_ops->crvalidate(task, p);
7711da177e4SLinus Torvalds }
7721da177e4SLinus Torvalds 
7739f06c719SChuck Lever static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
7749f06c719SChuck Lever 				   __be32 *data, void *obj)
7759f06c719SChuck Lever {
7769f06c719SChuck Lever 	struct xdr_stream xdr;
7779f06c719SChuck Lever 
7789f06c719SChuck Lever 	xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
7799f06c719SChuck Lever 	encode(rqstp, &xdr, obj);
7809f06c719SChuck Lever }
7819f06c719SChuck Lever 
7821da177e4SLinus Torvalds int
7839f06c719SChuck Lever rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
784d8ed029dSAlexey Dobriyan 		__be32 *data, void *obj)
7851da177e4SLinus Torvalds {
786a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
7871da177e4SLinus Torvalds 
78846121cf7SChuck Lever 	dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
7891da177e4SLinus Torvalds 			task->tk_pid, cred->cr_ops->cr_name, cred);
7901da177e4SLinus Torvalds 	if (cred->cr_ops->crwrap_req)
7911da177e4SLinus Torvalds 		return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
7921da177e4SLinus Torvalds 	/* By default, we encode the arguments normally. */
7939f06c719SChuck Lever 	rpcauth_wrap_req_encode(encode, rqstp, data, obj);
7949f06c719SChuck Lever 	return 0;
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
797bf269551SChuck Lever static int
798bf269551SChuck Lever rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
799bf269551SChuck Lever 			  __be32 *data, void *obj)
800bf269551SChuck Lever {
801bf269551SChuck Lever 	struct xdr_stream xdr;
802bf269551SChuck Lever 
803bf269551SChuck Lever 	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
804bf269551SChuck Lever 	return decode(rqstp, &xdr, obj);
805bf269551SChuck Lever }
806bf269551SChuck Lever 
8071da177e4SLinus Torvalds int
808bf269551SChuck Lever rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
809d8ed029dSAlexey Dobriyan 		__be32 *data, void *obj)
8101da177e4SLinus Torvalds {
811a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
8121da177e4SLinus Torvalds 
81346121cf7SChuck Lever 	dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
8141da177e4SLinus Torvalds 			task->tk_pid, cred->cr_ops->cr_name, cred);
8151da177e4SLinus Torvalds 	if (cred->cr_ops->crunwrap_resp)
8161da177e4SLinus Torvalds 		return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
8171da177e4SLinus Torvalds 						   data, obj);
8181da177e4SLinus Torvalds 	/* By default, we decode the arguments normally. */
819bf269551SChuck Lever 	return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds int
8231da177e4SLinus Torvalds rpcauth_refreshcred(struct rpc_task *task)
8241da177e4SLinus Torvalds {
8259a84d380STrond Myklebust 	struct rpc_cred	*cred;
8261da177e4SLinus Torvalds 	int err;
8271da177e4SLinus Torvalds 
828a17c2153STrond Myklebust 	cred = task->tk_rqstp->rq_cred;
829a17c2153STrond Myklebust 	if (cred == NULL) {
830a17c2153STrond Myklebust 		err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
831a17c2153STrond Myklebust 		if (err < 0)
832a17c2153STrond Myklebust 			goto out;
833a17c2153STrond Myklebust 		cred = task->tk_rqstp->rq_cred;
834f81c6224SJoe Perches 	}
83546121cf7SChuck Lever 	dprintk("RPC: %5u refreshing %s cred %p\n",
8361be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
8370bbacc40SChuck Lever 
8381da177e4SLinus Torvalds 	err = cred->cr_ops->crrefresh(task);
839a17c2153STrond Myklebust out:
8401da177e4SLinus Torvalds 	if (err < 0)
8411da177e4SLinus Torvalds 		task->tk_status = err;
8421da177e4SLinus Torvalds 	return err;
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds void
8461da177e4SLinus Torvalds rpcauth_invalcred(struct rpc_task *task)
8471da177e4SLinus Torvalds {
848a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
849fc432dd9STrond Myklebust 
85046121cf7SChuck Lever 	dprintk("RPC: %5u invalidating %s cred %p\n",
8511be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
852fc432dd9STrond Myklebust 	if (cred)
853fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
8541da177e4SLinus Torvalds }
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds int
8571da177e4SLinus Torvalds rpcauth_uptodatecred(struct rpc_task *task)
8581da177e4SLinus Torvalds {
859a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
860fc432dd9STrond Myklebust 
861fc432dd9STrond Myklebust 	return cred == NULL ||
862fc432dd9STrond Myklebust 		test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
8631da177e4SLinus Torvalds }
864f5c2187cSTrond Myklebust 
8658e1f936bSRusty Russell static struct shrinker rpc_cred_shrinker = {
86670534a73SDave Chinner 	.count_objects = rpcauth_cache_shrink_count,
86770534a73SDave Chinner 	.scan_objects = rpcauth_cache_shrink_scan,
8688e1f936bSRusty Russell 	.seeks = DEFAULT_SEEKS,
8698e1f936bSRusty Russell };
870f5c2187cSTrond Myklebust 
8715d8d9a4dSTrond Myklebust int __init rpcauth_init_module(void)
872f5c2187cSTrond Myklebust {
8735d8d9a4dSTrond Myklebust 	int err;
8745d8d9a4dSTrond Myklebust 
8755d8d9a4dSTrond Myklebust 	err = rpc_init_authunix();
8765d8d9a4dSTrond Myklebust 	if (err < 0)
8775d8d9a4dSTrond Myklebust 		goto out1;
8785d8d9a4dSTrond Myklebust 	err = rpc_init_generic_auth();
8795d8d9a4dSTrond Myklebust 	if (err < 0)
8805d8d9a4dSTrond Myklebust 		goto out2;
8818e1f936bSRusty Russell 	register_shrinker(&rpc_cred_shrinker);
8825d8d9a4dSTrond Myklebust 	return 0;
8835d8d9a4dSTrond Myklebust out2:
8845d8d9a4dSTrond Myklebust 	rpc_destroy_authunix();
8855d8d9a4dSTrond Myklebust out1:
8865d8d9a4dSTrond Myklebust 	return err;
887f5c2187cSTrond Myklebust }
888f5c2187cSTrond Myklebust 
889c135e84aSStephen Rothwell void rpcauth_remove_module(void)
890f5c2187cSTrond Myklebust {
8915d8d9a4dSTrond Myklebust 	rpc_destroy_authunix();
8925d8d9a4dSTrond Myklebust 	rpc_destroy_generic_auth();
8938e1f936bSRusty Russell 	unregister_shrinker(&rpc_cred_shrinker);
894f5c2187cSTrond Myklebust }
895