xref: /openbmc/linux/net/sunrpc/auth.c (revision 207f135d)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/net/sunrpc/auth.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Generic RPC client authentication API.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/sched.h>
125b825c3aSIngo Molnar #include <linux/cred.h>
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/slab.h>
151da177e4SLinus Torvalds #include <linux/errno.h>
1625337fdcSTrond Myklebust #include <linux/hash.h>
171da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
186a1a1e34SChuck Lever #include <linux/sunrpc/gss_api.h>
191da177e4SLinus Torvalds #include <linux/spinlock.h>
201da177e4SLinus Torvalds 
21a0584ee9SChuck Lever #include <trace/events/sunrpc.h>
22a0584ee9SChuck Lever 
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 
324e4c3befSTrond Myklebust static const struct rpc_authops __rcu *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
334e4c3befSTrond Myklebust 	[RPC_AUTH_NULL] = (const struct rpc_authops __force __rcu *)&authnull_ops,
344e4c3befSTrond Myklebust 	[RPC_AUTH_UNIX] = (const struct rpc_authops __force __rcu *)&authunix_ops,
3512072652SChuck Lever 	[RPC_AUTH_TLS]  = (const struct rpc_authops __force __rcu *)&authtls_ops,
361da177e4SLinus Torvalds };
371da177e4SLinus Torvalds 
38e092bdcdSTrond Myklebust static LIST_HEAD(cred_unused);
39f5c2187cSTrond Myklebust static unsigned long number_cred_unused;
40e092bdcdSTrond Myklebust 
41a52458b4SNeilBrown static struct cred machine_cred = {
42a52458b4SNeilBrown 	.usage = ATOMIC_INIT(1),
435e16923bSNeilBrown };
445e16923bSNeilBrown 
455e16923bSNeilBrown /*
465e16923bSNeilBrown  * Return the machine_cred pointer to be used whenever
475e16923bSNeilBrown  * the a generic machine credential is needed.
485e16923bSNeilBrown  */
rpc_machine_cred(void)49a52458b4SNeilBrown const struct cred *rpc_machine_cred(void)
505e16923bSNeilBrown {
515e16923bSNeilBrown 	return &machine_cred;
525e16923bSNeilBrown }
535e16923bSNeilBrown EXPORT_SYMBOL_GPL(rpc_machine_cred);
545e16923bSNeilBrown 
55db5fe265SMiquel van Smoorenburg #define MAX_HASHTABLE_BITS (14)
param_set_hashtbl_sz(const char * val,const struct kernel_param * kp)568e4e15d4SStephen Rothwell static int param_set_hashtbl_sz(const char *val, const struct kernel_param *kp)
57241269bdSTrond Myklebust {
58241269bdSTrond Myklebust 	unsigned long num;
59241269bdSTrond Myklebust 	unsigned int nbits;
60241269bdSTrond Myklebust 	int ret;
61241269bdSTrond Myklebust 
62241269bdSTrond Myklebust 	if (!val)
63241269bdSTrond Myklebust 		goto out_inval;
6400cfaa94SDaniel Walter 	ret = kstrtoul(val, 0, &num);
651a54c0cfSDan Carpenter 	if (ret)
66241269bdSTrond Myklebust 		goto out_inval;
6734ae685cSFrank Sorenson 	nbits = fls(num - 1);
68241269bdSTrond Myklebust 	if (nbits > MAX_HASHTABLE_BITS || nbits < 2)
69241269bdSTrond Myklebust 		goto out_inval;
70241269bdSTrond Myklebust 	*(unsigned int *)kp->arg = nbits;
71241269bdSTrond Myklebust 	return 0;
72241269bdSTrond Myklebust out_inval:
73241269bdSTrond Myklebust 	return -EINVAL;
74241269bdSTrond Myklebust }
75241269bdSTrond Myklebust 
param_get_hashtbl_sz(char * buffer,const struct kernel_param * kp)768e4e15d4SStephen Rothwell static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp)
77241269bdSTrond Myklebust {
78241269bdSTrond Myklebust 	unsigned int nbits;
79241269bdSTrond Myklebust 
80241269bdSTrond Myklebust 	nbits = *(unsigned int *)kp->arg;
812ac3ddc7SXiongfeng Wang 	return sprintf(buffer, "%u\n", 1U << nbits);
82241269bdSTrond Myklebust }
83241269bdSTrond Myklebust 
84241269bdSTrond Myklebust #define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
85241269bdSTrond Myklebust 
869c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_hashtbl_sz = {
878e4e15d4SStephen Rothwell 	.set = param_set_hashtbl_sz,
888e4e15d4SStephen Rothwell 	.get = param_get_hashtbl_sz,
898e4e15d4SStephen Rothwell };
908e4e15d4SStephen Rothwell 
91241269bdSTrond Myklebust module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
92241269bdSTrond Myklebust MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
93241269bdSTrond Myklebust 
94bae6746fSTrond Myklebust static unsigned long auth_max_cred_cachesize = ULONG_MAX;
95bae6746fSTrond Myklebust module_param(auth_max_cred_cachesize, ulong, 0644);
96bae6746fSTrond Myklebust MODULE_PARM_DESC(auth_max_cred_cachesize, "RPC credential maximum total cache size");
97bae6746fSTrond Myklebust 
981da177e4SLinus Torvalds static u32
pseudoflavor_to_flavor(u32 flavor)991da177e4SLinus Torvalds pseudoflavor_to_flavor(u32 flavor) {
1001c74a244SChuck Lever 	if (flavor > RPC_AUTH_MAXFLAVOR)
1011da177e4SLinus Torvalds 		return RPC_AUTH_GSS;
1021da177e4SLinus Torvalds 	return flavor;
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds int
rpcauth_register(const struct rpc_authops * ops)106f1c0a861STrond Myklebust rpcauth_register(const struct rpc_authops *ops)
1071da177e4SLinus Torvalds {
1084e4c3befSTrond Myklebust 	const struct rpc_authops *old;
1091da177e4SLinus Torvalds 	rpc_authflavor_t flavor;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
1121da177e4SLinus Torvalds 		return -EINVAL;
1134e4c3befSTrond Myklebust 	old = cmpxchg((const struct rpc_authops ** __force)&auth_flavors[flavor], NULL, ops);
1144e4c3befSTrond Myklebust 	if (old == NULL || old == ops)
1154e4c3befSTrond Myklebust 		return 0;
1164e4c3befSTrond Myklebust 	return -EPERM;
1171da177e4SLinus Torvalds }
118e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_register);
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds int
rpcauth_unregister(const struct rpc_authops * ops)121f1c0a861STrond Myklebust rpcauth_unregister(const struct rpc_authops *ops)
1221da177e4SLinus Torvalds {
1234e4c3befSTrond Myklebust 	const struct rpc_authops *old;
1241da177e4SLinus Torvalds 	rpc_authflavor_t flavor;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
1271da177e4SLinus Torvalds 		return -EINVAL;
1284e4c3befSTrond Myklebust 
1294e4c3befSTrond Myklebust 	old = cmpxchg((const struct rpc_authops ** __force)&auth_flavors[flavor], ops, NULL);
1304e4c3befSTrond Myklebust 	if (old == ops || old == NULL)
1314e4c3befSTrond Myklebust 		return 0;
1324e4c3befSTrond Myklebust 	return -EPERM;
1331da177e4SLinus Torvalds }
134e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_unregister);
1351da177e4SLinus Torvalds 
1364e4c3befSTrond Myklebust static const struct rpc_authops *
rpcauth_get_authops(rpc_authflavor_t flavor)1374e4c3befSTrond Myklebust rpcauth_get_authops(rpc_authflavor_t flavor)
1384e4c3befSTrond Myklebust {
1394e4c3befSTrond Myklebust 	const struct rpc_authops *ops;
1404e4c3befSTrond Myklebust 
1414e4c3befSTrond Myklebust 	if (flavor >= RPC_AUTH_MAXFLAVOR)
1424e4c3befSTrond Myklebust 		return NULL;
1434e4c3befSTrond Myklebust 
1444e4c3befSTrond Myklebust 	rcu_read_lock();
1454e4c3befSTrond Myklebust 	ops = rcu_dereference(auth_flavors[flavor]);
1464e4c3befSTrond Myklebust 	if (ops == NULL) {
1474e4c3befSTrond Myklebust 		rcu_read_unlock();
1484e4c3befSTrond Myklebust 		request_module("rpc-auth-%u", flavor);
1494e4c3befSTrond Myklebust 		rcu_read_lock();
1504e4c3befSTrond Myklebust 		ops = rcu_dereference(auth_flavors[flavor]);
1514e4c3befSTrond Myklebust 		if (ops == NULL)
1524e4c3befSTrond Myklebust 			goto out;
1534e4c3befSTrond Myklebust 	}
1544e4c3befSTrond Myklebust 	if (!try_module_get(ops->owner))
1554e4c3befSTrond Myklebust 		ops = NULL;
1564e4c3befSTrond Myklebust out:
1574e4c3befSTrond Myklebust 	rcu_read_unlock();
1584e4c3befSTrond Myklebust 	return ops;
1594e4c3befSTrond Myklebust }
1604e4c3befSTrond Myklebust 
1614e4c3befSTrond Myklebust static void
rpcauth_put_authops(const struct rpc_authops * ops)1624e4c3befSTrond Myklebust rpcauth_put_authops(const struct rpc_authops *ops)
1634e4c3befSTrond Myklebust {
1644e4c3befSTrond Myklebust 	module_put(ops->owner);
1654e4c3befSTrond Myklebust }
1664e4c3befSTrond Myklebust 
1676a1a1e34SChuck Lever /**
1689568c5e9SChuck Lever  * rpcauth_get_pseudoflavor - check if security flavor is supported
1699568c5e9SChuck Lever  * @flavor: a security flavor
1709568c5e9SChuck Lever  * @info: a GSS mech OID, quality of protection, and service value
1719568c5e9SChuck Lever  *
1729568c5e9SChuck Lever  * Verifies that an appropriate kernel module is available or already loaded.
1739568c5e9SChuck Lever  * Returns an equivalent pseudoflavor, or RPC_AUTH_MAXFLAVOR if "flavor" is
1749568c5e9SChuck Lever  * not supported locally.
1759568c5e9SChuck Lever  */
1769568c5e9SChuck Lever rpc_authflavor_t
rpcauth_get_pseudoflavor(rpc_authflavor_t flavor,struct rpcsec_gss_info * info)1779568c5e9SChuck Lever rpcauth_get_pseudoflavor(rpc_authflavor_t flavor, struct rpcsec_gss_info *info)
1789568c5e9SChuck Lever {
1794e4c3befSTrond Myklebust 	const struct rpc_authops *ops = rpcauth_get_authops(flavor);
1809568c5e9SChuck Lever 	rpc_authflavor_t pseudoflavor;
1819568c5e9SChuck Lever 
1824e4c3befSTrond Myklebust 	if (!ops)
1839568c5e9SChuck Lever 		return RPC_AUTH_MAXFLAVOR;
1849568c5e9SChuck Lever 	pseudoflavor = flavor;
1859568c5e9SChuck Lever 	if (ops->info2flavor != NULL)
1869568c5e9SChuck Lever 		pseudoflavor = ops->info2flavor(info);
1879568c5e9SChuck Lever 
1884e4c3befSTrond Myklebust 	rpcauth_put_authops(ops);
1899568c5e9SChuck Lever 	return pseudoflavor;
1909568c5e9SChuck Lever }
1919568c5e9SChuck Lever EXPORT_SYMBOL_GPL(rpcauth_get_pseudoflavor);
1929568c5e9SChuck Lever 
1939568c5e9SChuck Lever /**
194a77c806fSChuck Lever  * rpcauth_get_gssinfo - find GSS tuple matching a GSS pseudoflavor
195a77c806fSChuck Lever  * @pseudoflavor: GSS pseudoflavor to match
196a77c806fSChuck Lever  * @info: rpcsec_gss_info structure to fill in
197a77c806fSChuck Lever  *
198a77c806fSChuck Lever  * Returns zero and fills in "info" if pseudoflavor matches a
199a77c806fSChuck Lever  * supported mechanism.
200a77c806fSChuck Lever  */
201a77c806fSChuck Lever int
rpcauth_get_gssinfo(rpc_authflavor_t pseudoflavor,struct rpcsec_gss_info * info)202a77c806fSChuck Lever rpcauth_get_gssinfo(rpc_authflavor_t pseudoflavor, struct rpcsec_gss_info *info)
203a77c806fSChuck Lever {
204a77c806fSChuck Lever 	rpc_authflavor_t flavor = pseudoflavor_to_flavor(pseudoflavor);
205a77c806fSChuck Lever 	const struct rpc_authops *ops;
206a77c806fSChuck Lever 	int result;
207a77c806fSChuck Lever 
2084e4c3befSTrond Myklebust 	ops = rpcauth_get_authops(flavor);
209a77c806fSChuck Lever 	if (ops == NULL)
210a77c806fSChuck Lever 		return -ENOENT;
211a77c806fSChuck Lever 
212a77c806fSChuck Lever 	result = -ENOENT;
213a77c806fSChuck Lever 	if (ops->flavor2info != NULL)
214a77c806fSChuck Lever 		result = ops->flavor2info(pseudoflavor, info);
215a77c806fSChuck Lever 
2164e4c3befSTrond Myklebust 	rpcauth_put_authops(ops);
217a77c806fSChuck Lever 	return result;
218a77c806fSChuck Lever }
219a77c806fSChuck Lever EXPORT_SYMBOL_GPL(rpcauth_get_gssinfo);
220a77c806fSChuck Lever 
2211da177e4SLinus Torvalds struct rpc_auth *
rpcauth_create(const struct rpc_auth_create_args * args,struct rpc_clnt * clnt)22282b98ca5SSargun Dhillon rpcauth_create(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
2231da177e4SLinus Torvalds {
2244e4c3befSTrond Myklebust 	struct rpc_auth	*auth = ERR_PTR(-EINVAL);
225f1c0a861STrond Myklebust 	const struct rpc_authops *ops;
226c2190661STrond Myklebust 	u32 flavor = pseudoflavor_to_flavor(args->pseudoflavor);
2271da177e4SLinus Torvalds 
2284e4c3befSTrond Myklebust 	ops = rpcauth_get_authops(flavor);
2294e4c3befSTrond Myklebust 	if (ops == NULL)
230f344f6dfSOlaf Kirch 		goto out;
231f344f6dfSOlaf Kirch 
232c2190661STrond Myklebust 	auth = ops->create(args, clnt);
2334e4c3befSTrond Myklebust 
2344e4c3befSTrond Myklebust 	rpcauth_put_authops(ops);
2356a19275aSJ. Bruce Fields 	if (IS_ERR(auth))
2366a19275aSJ. Bruce Fields 		return auth;
2371da177e4SLinus Torvalds 	if (clnt->cl_auth)
238de7a8ce3STrond Myklebust 		rpcauth_release(clnt->cl_auth);
2391da177e4SLinus Torvalds 	clnt->cl_auth = auth;
240f344f6dfSOlaf Kirch 
241f344f6dfSOlaf Kirch out:
2421da177e4SLinus Torvalds 	return auth;
2431da177e4SLinus Torvalds }
244e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_create);
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds void
rpcauth_release(struct rpc_auth * auth)247de7a8ce3STrond Myklebust rpcauth_release(struct rpc_auth *auth)
2481da177e4SLinus Torvalds {
249331bc71cSTrond Myklebust 	if (!refcount_dec_and_test(&auth->au_count))
2501da177e4SLinus Torvalds 		return;
2511da177e4SLinus Torvalds 	auth->au_ops->destroy(auth);
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds static DEFINE_SPINLOCK(rpc_credcache_lock);
2551da177e4SLinus Torvalds 
25695cd6232STrond Myklebust /*
25795cd6232STrond Myklebust  * On success, the caller is responsible for freeing the reference
25895cd6232STrond Myklebust  * held by the hashtable
25995cd6232STrond Myklebust  */
26095cd6232STrond Myklebust static bool
rpcauth_unhash_cred_locked(struct rpc_cred * cred)26131be5bf1STrond Myklebust rpcauth_unhash_cred_locked(struct rpc_cred *cred)
26231be5bf1STrond Myklebust {
26395cd6232STrond Myklebust 	if (!test_and_clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags))
26495cd6232STrond Myklebust 		return false;
26531be5bf1STrond Myklebust 	hlist_del_rcu(&cred->cr_hash);
26695cd6232STrond Myklebust 	return true;
26731be5bf1STrond Myklebust }
26831be5bf1STrond Myklebust 
26995cd6232STrond Myklebust static bool
rpcauth_unhash_cred(struct rpc_cred * cred)2709499b434STrond Myklebust rpcauth_unhash_cred(struct rpc_cred *cred)
2719499b434STrond Myklebust {
2729499b434STrond Myklebust 	spinlock_t *cache_lock;
27395cd6232STrond Myklebust 	bool ret;
2749499b434STrond Myklebust 
27595cd6232STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags))
27695cd6232STrond Myklebust 		return false;
2779499b434STrond Myklebust 	cache_lock = &cred->cr_auth->au_credcache->lock;
2789499b434STrond Myklebust 	spin_lock(cache_lock);
27995cd6232STrond Myklebust 	ret = rpcauth_unhash_cred_locked(cred);
2809499b434STrond Myklebust 	spin_unlock(cache_lock);
281f0380f3dSTrond Myklebust 	return ret;
2829499b434STrond Myklebust }
2839499b434STrond Myklebust 
2841da177e4SLinus Torvalds /*
2851da177e4SLinus Torvalds  * Initialize RPC credential cache
2861da177e4SLinus Torvalds  */
2871da177e4SLinus Torvalds int
rpcauth_init_credcache(struct rpc_auth * auth)288f5c2187cSTrond Myklebust rpcauth_init_credcache(struct rpc_auth *auth)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds 	struct rpc_cred_cache *new;
291988664a0STrond Myklebust 	unsigned int hashsize;
2921da177e4SLinus Torvalds 
2938b3a7005SKris Katterjohn 	new = kmalloc(sizeof(*new), GFP_KERNEL);
2941da177e4SLinus Torvalds 	if (!new)
295241269bdSTrond Myklebust 		goto out_nocache;
296241269bdSTrond Myklebust 	new->hashbits = auth_hashbits;
297988664a0STrond Myklebust 	hashsize = 1U << new->hashbits;
298241269bdSTrond Myklebust 	new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
299241269bdSTrond Myklebust 	if (!new->hashtable)
300241269bdSTrond Myklebust 		goto out_nohashtbl;
3019499b434STrond Myklebust 	spin_lock_init(&new->lock);
3021da177e4SLinus Torvalds 	auth->au_credcache = new;
3031da177e4SLinus Torvalds 	return 0;
304241269bdSTrond Myklebust out_nohashtbl:
305241269bdSTrond Myklebust 	kfree(new);
306241269bdSTrond Myklebust out_nocache:
307241269bdSTrond Myklebust 	return -ENOMEM;
3081da177e4SLinus Torvalds }
309e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
3101da177e4SLinus Torvalds 
311a0337d1dSJeff Layton char *
rpcauth_stringify_acceptor(struct rpc_cred * cred)312a0337d1dSJeff Layton rpcauth_stringify_acceptor(struct rpc_cred *cred)
313a0337d1dSJeff Layton {
314a0337d1dSJeff Layton 	if (!cred->cr_ops->crstringify_acceptor)
315a0337d1dSJeff Layton 		return NULL;
316a0337d1dSJeff Layton 	return cred->cr_ops->crstringify_acceptor(cred);
317a0337d1dSJeff Layton }
318a0337d1dSJeff Layton EXPORT_SYMBOL_GPL(rpcauth_stringify_acceptor);
319a0337d1dSJeff Layton 
3204de6caa2SAndy Adamson /*
3211da177e4SLinus Torvalds  * Destroy a list of credentials
3221da177e4SLinus Torvalds  */
3231da177e4SLinus Torvalds static inline
rpcauth_destroy_credlist(struct list_head * head)324e092bdcdSTrond Myklebust void rpcauth_destroy_credlist(struct list_head *head)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	struct rpc_cred *cred;
3271da177e4SLinus Torvalds 
328e092bdcdSTrond Myklebust 	while (!list_empty(head)) {
329e092bdcdSTrond Myklebust 		cred = list_entry(head->next, struct rpc_cred, cr_lru);
330e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
3311da177e4SLinus Torvalds 		put_rpccred(cred);
3321da177e4SLinus Torvalds 	}
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
33595cd6232STrond Myklebust static void
rpcauth_lru_add_locked(struct rpc_cred * cred)33695cd6232STrond Myklebust rpcauth_lru_add_locked(struct rpc_cred *cred)
33795cd6232STrond Myklebust {
33895cd6232STrond Myklebust 	if (!list_empty(&cred->cr_lru))
33995cd6232STrond Myklebust 		return;
34095cd6232STrond Myklebust 	number_cred_unused++;
34195cd6232STrond Myklebust 	list_add_tail(&cred->cr_lru, &cred_unused);
34295cd6232STrond Myklebust }
34395cd6232STrond Myklebust 
34495cd6232STrond Myklebust static void
rpcauth_lru_add(struct rpc_cred * cred)34595cd6232STrond Myklebust rpcauth_lru_add(struct rpc_cred *cred)
34695cd6232STrond Myklebust {
34795cd6232STrond Myklebust 	if (!list_empty(&cred->cr_lru))
34895cd6232STrond Myklebust 		return;
34995cd6232STrond Myklebust 	spin_lock(&rpc_credcache_lock);
35095cd6232STrond Myklebust 	rpcauth_lru_add_locked(cred);
35195cd6232STrond Myklebust 	spin_unlock(&rpc_credcache_lock);
35295cd6232STrond Myklebust }
35395cd6232STrond Myklebust 
35495cd6232STrond Myklebust static void
rpcauth_lru_remove_locked(struct rpc_cred * cred)35595cd6232STrond Myklebust rpcauth_lru_remove_locked(struct rpc_cred *cred)
35695cd6232STrond Myklebust {
35795cd6232STrond Myklebust 	if (list_empty(&cred->cr_lru))
35895cd6232STrond Myklebust 		return;
35995cd6232STrond Myklebust 	number_cred_unused--;
36095cd6232STrond Myklebust 	list_del_init(&cred->cr_lru);
36195cd6232STrond Myklebust }
36295cd6232STrond Myklebust 
36395cd6232STrond Myklebust static void
rpcauth_lru_remove(struct rpc_cred * cred)36495cd6232STrond Myklebust rpcauth_lru_remove(struct rpc_cred *cred)
36595cd6232STrond Myklebust {
36695cd6232STrond Myklebust 	if (list_empty(&cred->cr_lru))
36795cd6232STrond Myklebust 		return;
36895cd6232STrond Myklebust 	spin_lock(&rpc_credcache_lock);
36995cd6232STrond Myklebust 	rpcauth_lru_remove_locked(cred);
37095cd6232STrond Myklebust 	spin_unlock(&rpc_credcache_lock);
37195cd6232STrond Myklebust }
37295cd6232STrond Myklebust 
3731da177e4SLinus Torvalds /*
3741da177e4SLinus Torvalds  * Clear the RPC credential cache, and delete those credentials
3751da177e4SLinus Torvalds  * that are not referenced.
3761da177e4SLinus Torvalds  */
3771da177e4SLinus Torvalds void
rpcauth_clear_credcache(struct rpc_cred_cache * cache)3783ab9bb72STrond Myklebust rpcauth_clear_credcache(struct rpc_cred_cache *cache)
3791da177e4SLinus Torvalds {
380e092bdcdSTrond Myklebust 	LIST_HEAD(free);
381e092bdcdSTrond Myklebust 	struct hlist_head *head;
3821da177e4SLinus Torvalds 	struct rpc_cred	*cred;
383988664a0STrond Myklebust 	unsigned int hashsize = 1U << cache->hashbits;
3841da177e4SLinus Torvalds 	int		i;
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	spin_lock(&rpc_credcache_lock);
3879499b434STrond Myklebust 	spin_lock(&cache->lock);
388988664a0STrond Myklebust 	for (i = 0; i < hashsize; i++) {
389e092bdcdSTrond Myklebust 		head = &cache->hashtable[i];
390e092bdcdSTrond Myklebust 		while (!hlist_empty(head)) {
391e092bdcdSTrond Myklebust 			cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
39231be5bf1STrond Myklebust 			rpcauth_unhash_cred_locked(cred);
39395cd6232STrond Myklebust 			/* Note: We now hold a reference to cred */
39495cd6232STrond Myklebust 			rpcauth_lru_remove_locked(cred);
39595cd6232STrond Myklebust 			list_add_tail(&cred->cr_lru, &free);
3961da177e4SLinus Torvalds 		}
3971da177e4SLinus Torvalds 	}
3989499b434STrond Myklebust 	spin_unlock(&cache->lock);
3991da177e4SLinus Torvalds 	spin_unlock(&rpc_credcache_lock);
4001da177e4SLinus Torvalds 	rpcauth_destroy_credlist(&free);
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4033ab9bb72STrond Myklebust /*
4043ab9bb72STrond Myklebust  * Destroy the RPC credential cache
4053ab9bb72STrond Myklebust  */
4063ab9bb72STrond Myklebust void
rpcauth_destroy_credcache(struct rpc_auth * auth)4073ab9bb72STrond Myklebust rpcauth_destroy_credcache(struct rpc_auth *auth)
4083ab9bb72STrond Myklebust {
4093ab9bb72STrond Myklebust 	struct rpc_cred_cache *cache = auth->au_credcache;
4103ab9bb72STrond Myklebust 
4113ab9bb72STrond Myklebust 	if (cache) {
4123ab9bb72STrond Myklebust 		auth->au_credcache = NULL;
4133ab9bb72STrond Myklebust 		rpcauth_clear_credcache(cache);
414241269bdSTrond Myklebust 		kfree(cache->hashtable);
4153ab9bb72STrond Myklebust 		kfree(cache);
4163ab9bb72STrond Myklebust 	}
4173ab9bb72STrond Myklebust }
418e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
4193ab9bb72STrond Myklebust 
420d2b83141STrond Myklebust 
421d2b83141STrond Myklebust #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
422d2b83141STrond Myklebust 
4231da177e4SLinus Torvalds /*
4241da177e4SLinus Torvalds  * Remove stale credentials. Avoid sleeping inside the loop.
4251da177e4SLinus Torvalds  */
42670534a73SDave Chinner static long
rpcauth_prune_expired(struct list_head * free,int nr_to_scan)427f5c2187cSTrond Myklebust rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
4281da177e4SLinus Torvalds {
429eac0d18dSTrond Myklebust 	struct rpc_cred *cred, *next;
430d2b83141STrond Myklebust 	unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
43170534a73SDave Chinner 	long freed = 0;
4321da177e4SLinus Torvalds 
433eac0d18dSTrond Myklebust 	list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
434eac0d18dSTrond Myklebust 
43520673406STrond Myklebust 		if (nr_to_scan-- == 0)
43620673406STrond Myklebust 			break;
43779b18181STrond Myklebust 		if (refcount_read(&cred->cr_count) > 1) {
43895cd6232STrond Myklebust 			rpcauth_lru_remove_locked(cred);
43995cd6232STrond Myklebust 			continue;
44095cd6232STrond Myklebust 		}
44193a05e65STrond Myklebust 		/*
44293a05e65STrond Myklebust 		 * Enforce a 60 second garbage collection moratorium
44393a05e65STrond Myklebust 		 * Note that the cred_unused list must be time-ordered.
44493a05e65STrond Myklebust 		 */
445f1bafa73SDan Aloni 		if (time_in_range(cred->cr_expire, expired, jiffies))
44695cd6232STrond Myklebust 			continue;
44795cd6232STrond Myklebust 		if (!rpcauth_unhash_cred(cred))
448e092bdcdSTrond Myklebust 			continue;
449eac0d18dSTrond Myklebust 
45095cd6232STrond Myklebust 		rpcauth_lru_remove_locked(cred);
45195cd6232STrond Myklebust 		freed++;
452e092bdcdSTrond Myklebust 		list_add_tail(&cred->cr_lru, free);
4531da177e4SLinus Torvalds 	}
45495cd6232STrond Myklebust 	return freed ? freed : SHRINK_STOP;
4551da177e4SLinus Torvalds }
456e092bdcdSTrond Myklebust 
457bae6746fSTrond Myklebust static unsigned long
rpcauth_cache_do_shrink(int nr_to_scan)458bae6746fSTrond Myklebust rpcauth_cache_do_shrink(int nr_to_scan)
459bae6746fSTrond Myklebust {
460bae6746fSTrond Myklebust 	LIST_HEAD(free);
461bae6746fSTrond Myklebust 	unsigned long freed;
462bae6746fSTrond Myklebust 
463bae6746fSTrond Myklebust 	spin_lock(&rpc_credcache_lock);
464bae6746fSTrond Myklebust 	freed = rpcauth_prune_expired(&free, nr_to_scan);
465bae6746fSTrond Myklebust 	spin_unlock(&rpc_credcache_lock);
466bae6746fSTrond Myklebust 	rpcauth_destroy_credlist(&free);
467bae6746fSTrond Myklebust 
468bae6746fSTrond Myklebust 	return freed;
469bae6746fSTrond Myklebust }
470bae6746fSTrond Myklebust 
471e092bdcdSTrond Myklebust /*
472f5c2187cSTrond Myklebust  * Run memory cache shrinker.
473e092bdcdSTrond Myklebust  */
47470534a73SDave Chinner static unsigned long
rpcauth_cache_shrink_scan(struct shrinker * shrink,struct shrink_control * sc)47570534a73SDave Chinner rpcauth_cache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
47670534a73SDave Chinner 
477e092bdcdSTrond Myklebust {
47870534a73SDave Chinner 	if ((sc->gfp_mask & GFP_KERNEL) != GFP_KERNEL)
47970534a73SDave Chinner 		return SHRINK_STOP;
48070534a73SDave Chinner 
48170534a73SDave Chinner 	/* nothing left, don't come back */
482f5c2187cSTrond Myklebust 	if (list_empty(&cred_unused))
48370534a73SDave Chinner 		return SHRINK_STOP;
48470534a73SDave Chinner 
485bae6746fSTrond Myklebust 	return rpcauth_cache_do_shrink(sc->nr_to_scan);
48670534a73SDave Chinner }
48770534a73SDave Chinner 
48870534a73SDave Chinner static unsigned long
rpcauth_cache_shrink_count(struct shrinker * shrink,struct shrink_control * sc)48970534a73SDave Chinner rpcauth_cache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
49070534a73SDave Chinner 
49170534a73SDave Chinner {
4924c3ffd05SNeilBrown 	return number_cred_unused * sysctl_vfs_cache_pressure / 100;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
495bae6746fSTrond Myklebust static void
rpcauth_cache_enforce_limit(void)496bae6746fSTrond Myklebust rpcauth_cache_enforce_limit(void)
497bae6746fSTrond Myklebust {
498bae6746fSTrond Myklebust 	unsigned long diff;
499bae6746fSTrond Myklebust 	unsigned int nr_to_scan;
500bae6746fSTrond Myklebust 
501bae6746fSTrond Myklebust 	if (number_cred_unused <= auth_max_cred_cachesize)
502bae6746fSTrond Myklebust 		return;
503bae6746fSTrond Myklebust 	diff = number_cred_unused - auth_max_cred_cachesize;
504bae6746fSTrond Myklebust 	nr_to_scan = 100;
505bae6746fSTrond Myklebust 	if (diff < nr_to_scan)
506bae6746fSTrond Myklebust 		nr_to_scan = diff;
507bae6746fSTrond Myklebust 	rpcauth_cache_do_shrink(nr_to_scan);
508bae6746fSTrond Myklebust }
509bae6746fSTrond Myklebust 
5101da177e4SLinus Torvalds /*
5111da177e4SLinus Torvalds  * Look up a process' credentials in the authentication cache
5121da177e4SLinus Torvalds  */
5131da177e4SLinus Torvalds struct rpc_cred *
rpcauth_lookup_credcache(struct rpc_auth * auth,struct auth_cred * acred,int flags,gfp_t gfp)5141da177e4SLinus Torvalds rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
5153c6e0bc8SJeff Layton 		int flags, gfp_t gfp)
5161da177e4SLinus Torvalds {
517e092bdcdSTrond Myklebust 	LIST_HEAD(free);
5181da177e4SLinus Torvalds 	struct rpc_cred_cache *cache = auth->au_credcache;
51931be5bf1STrond Myklebust 	struct rpc_cred	*cred = NULL,
52031be5bf1STrond Myklebust 			*entry, *new;
52125337fdcSTrond Myklebust 	unsigned int nr;
52225337fdcSTrond Myklebust 
52366cbd4baSFrank Sorenson 	nr = auth->au_ops->hash_cred(acred, cache->hashbits);
5241da177e4SLinus Torvalds 
52531be5bf1STrond Myklebust 	rcu_read_lock();
526b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
52731be5bf1STrond Myklebust 		if (!entry->cr_ops->crmatch(acred, entry, flags))
52831be5bf1STrond Myklebust 			continue;
52931be5bf1STrond Myklebust 		cred = get_rpccred(entry);
53007d02a67STrond Myklebust 		if (cred)
53131be5bf1STrond Myklebust 			break;
53231be5bf1STrond Myklebust 	}
53331be5bf1STrond Myklebust 	rcu_read_unlock();
53431be5bf1STrond Myklebust 
5359499b434STrond Myklebust 	if (cred != NULL)
53631be5bf1STrond Myklebust 		goto found;
53731be5bf1STrond Myklebust 
5383c6e0bc8SJeff Layton 	new = auth->au_ops->crcreate(auth, acred, flags, gfp);
53931be5bf1STrond Myklebust 	if (IS_ERR(new)) {
54031be5bf1STrond Myklebust 		cred = new;
54131be5bf1STrond Myklebust 		goto out;
54231be5bf1STrond Myklebust 	}
54331be5bf1STrond Myklebust 
5449499b434STrond Myklebust 	spin_lock(&cache->lock);
545b67bfe0dSSasha Levin 	hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
546e092bdcdSTrond Myklebust 		if (!entry->cr_ops->crmatch(acred, entry, flags))
547e092bdcdSTrond Myklebust 			continue;
548e092bdcdSTrond Myklebust 		cred = get_rpccred(entry);
54907d02a67STrond Myklebust 		if (cred)
5501da177e4SLinus Torvalds 			break;
5511da177e4SLinus Torvalds 	}
55231be5bf1STrond Myklebust 	if (cred == NULL) {
55331be5bf1STrond Myklebust 		cred = new;
55431be5bf1STrond Myklebust 		set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
55579b18181STrond Myklebust 		refcount_inc(&cred->cr_count);
55631be5bf1STrond Myklebust 		hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
55731be5bf1STrond Myklebust 	} else
558e092bdcdSTrond Myklebust 		list_add_tail(&new->cr_lru, &free);
5599499b434STrond Myklebust 	spin_unlock(&cache->lock);
560bae6746fSTrond Myklebust 	rpcauth_cache_enforce_limit();
56131be5bf1STrond Myklebust found:
562f64f9e71SJoe Perches 	if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
563f64f9e71SJoe Perches 	    cred->cr_ops->cr_init != NULL &&
564f64f9e71SJoe Perches 	    !(flags & RPCAUTH_LOOKUP_NEW)) {
565fba3bad4STrond Myklebust 		int res = cred->cr_ops->cr_init(auth, cred);
566fba3bad4STrond Myklebust 		if (res < 0) {
567fba3bad4STrond Myklebust 			put_rpccred(cred);
568fba3bad4STrond Myklebust 			cred = ERR_PTR(res);
569fba3bad4STrond Myklebust 		}
5701da177e4SLinus Torvalds 	}
57131be5bf1STrond Myklebust 	rpcauth_destroy_credlist(&free);
57231be5bf1STrond Myklebust out:
57331be5bf1STrond Myklebust 	return cred;
5741da177e4SLinus Torvalds }
575e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds struct rpc_cred *
rpcauth_lookupcred(struct rpc_auth * auth,int flags)5788a317760STrond Myklebust rpcauth_lookupcred(struct rpc_auth *auth, int flags)
5791da177e4SLinus Torvalds {
58086a264abSDavid Howells 	struct auth_cred acred;
5811da177e4SLinus Torvalds 	struct rpc_cred *ret;
58286a264abSDavid Howells 	const struct cred *cred = current_cred();
5831da177e4SLinus Torvalds 
58486a264abSDavid Howells 	memset(&acred, 0, sizeof(acred));
58597f68c6bSNeilBrown 	acred.cred = cred;
5868a317760STrond Myklebust 	ret = auth->au_ops->lookup_cred(auth, &acred, flags);
5871da177e4SLinus Torvalds 	return ret;
5881da177e4SLinus Torvalds }
58966b06860SAndy Adamson EXPORT_SYMBOL_GPL(rpcauth_lookupcred);
5901da177e4SLinus Torvalds 
5915fe4755eSTrond Myklebust void
rpcauth_init_cred(struct rpc_cred * cred,const struct auth_cred * acred,struct rpc_auth * auth,const struct rpc_credops * ops)5925fe4755eSTrond Myklebust rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
5935fe4755eSTrond Myklebust 		  struct rpc_auth *auth, const struct rpc_credops *ops)
5945fe4755eSTrond Myklebust {
5955fe4755eSTrond Myklebust 	INIT_HLIST_NODE(&cred->cr_hash);
596e092bdcdSTrond Myklebust 	INIT_LIST_HEAD(&cred->cr_lru);
59779b18181STrond Myklebust 	refcount_set(&cred->cr_count, 1);
5985fe4755eSTrond Myklebust 	cred->cr_auth = auth;
5992edd8d74SNeilBrown 	cred->cr_flags = 0;
6005fe4755eSTrond Myklebust 	cred->cr_ops = ops;
6015fe4755eSTrond Myklebust 	cred->cr_expire = jiffies;
60297f68c6bSNeilBrown 	cred->cr_cred = get_cred(acred->cred);
6035fe4755eSTrond Myklebust }
604e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_init_cred);
6055fe4755eSTrond Myklebust 
6068572b8e2STrond Myklebust static struct rpc_cred *
rpcauth_bind_root_cred(struct rpc_task * task,int lookupflags)6075d351754STrond Myklebust rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
6081da177e4SLinus Torvalds {
6091be27f36STrond Myklebust 	struct rpc_auth *auth = task->tk_client->cl_auth;
6101da177e4SLinus Torvalds 	struct auth_cred acred = {
61197f68c6bSNeilBrown 		.cred = get_task_cred(&init_task),
6121da177e4SLinus Torvalds 	};
61397f68c6bSNeilBrown 	struct rpc_cred *ret;
6141da177e4SLinus Torvalds 
615a41b05edSNeilBrown 	if (RPC_IS_ASYNC(task))
616a41b05edSNeilBrown 		lookupflags |= RPCAUTH_LOOKUP_ASYNC;
61797f68c6bSNeilBrown 	ret = auth->au_ops->lookup_cred(auth, &acred, lookupflags);
61897f68c6bSNeilBrown 	put_cred(acred.cred);
61997f68c6bSNeilBrown 	return ret;
620af093835STrond Myklebust }
621af093835STrond Myklebust 
6228572b8e2STrond Myklebust static struct rpc_cred *
rpcauth_bind_machine_cred(struct rpc_task * task,int lookupflags)6235e16923bSNeilBrown rpcauth_bind_machine_cred(struct rpc_task *task, int lookupflags)
6245e16923bSNeilBrown {
6255e16923bSNeilBrown 	struct rpc_auth *auth = task->tk_client->cl_auth;
6265e16923bSNeilBrown 	struct auth_cred acred = {
6275e16923bSNeilBrown 		.principal = task->tk_client->cl_principal,
6285e16923bSNeilBrown 		.cred = init_task.cred,
6295e16923bSNeilBrown 	};
6305e16923bSNeilBrown 
6315e16923bSNeilBrown 	if (!acred.principal)
6325e16923bSNeilBrown 		return NULL;
633a41b05edSNeilBrown 	if (RPC_IS_ASYNC(task))
634a41b05edSNeilBrown 		lookupflags |= RPCAUTH_LOOKUP_ASYNC;
6355e16923bSNeilBrown 	return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
6365e16923bSNeilBrown }
6375e16923bSNeilBrown 
6385e16923bSNeilBrown static struct rpc_cred *
rpcauth_bind_new_cred(struct rpc_task * task,int lookupflags)6395d351754STrond Myklebust rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
640af093835STrond Myklebust {
641af093835STrond Myklebust 	struct rpc_auth *auth = task->tk_client->cl_auth;
642af093835STrond Myklebust 
6438572b8e2STrond Myklebust 	return rpcauth_lookupcred(auth, lookupflags);
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
646a17c2153STrond Myklebust static int
rpcauth_bindcred(struct rpc_task * task,const struct cred * cred,int flags)647a52458b4SNeilBrown rpcauth_bindcred(struct rpc_task *task, const struct cred *cred, int flags)
6481da177e4SLinus Torvalds {
649a17c2153STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
6505e16923bSNeilBrown 	struct rpc_cred *new = NULL;
6515d351754STrond Myklebust 	int lookupflags = 0;
652a52458b4SNeilBrown 	struct rpc_auth *auth = task->tk_client->cl_auth;
653a52458b4SNeilBrown 	struct auth_cred acred = {
654a52458b4SNeilBrown 		.cred = cred,
655a52458b4SNeilBrown 	};
6565d351754STrond Myklebust 
6575d351754STrond Myklebust 	if (flags & RPC_TASK_ASYNC)
658a41b05edSNeilBrown 		lookupflags |= RPCAUTH_LOOKUP_NEW | RPCAUTH_LOOKUP_ASYNC;
6591de7eea9SNeilBrown 	if (task->tk_op_cred)
6601de7eea9SNeilBrown 		/* Task must use exactly this rpc_cred */
661d6efccd9SNeilBrown 		new = get_rpccred(task->tk_op_cred);
6621de7eea9SNeilBrown 	else if (cred != NULL && cred != &machine_cred)
663a52458b4SNeilBrown 		new = auth->au_ops->lookup_cred(auth, &acred, lookupflags);
6645e16923bSNeilBrown 	else if (cred == &machine_cred)
6655e16923bSNeilBrown 		new = rpcauth_bind_machine_cred(task, lookupflags);
6665e16923bSNeilBrown 
6675e16923bSNeilBrown 	/* If machine cred couldn't be bound, try a root cred */
6685e16923bSNeilBrown 	if (new)
6695e16923bSNeilBrown 		;
67089c2be8aSNeilBrown 	else if (cred == &machine_cred)
6718572b8e2STrond Myklebust 		new = rpcauth_bind_root_cred(task, lookupflags);
672a68a72e1SNeilBrown 	else if (flags & RPC_TASK_NULLCREDS)
673a68a72e1SNeilBrown 		new = authnull_ops.lookup_cred(NULL, NULL, 0);
6744ccda2cdSTrond Myklebust 	else
6758572b8e2STrond Myklebust 		new = rpcauth_bind_new_cred(task, lookupflags);
6768572b8e2STrond Myklebust 	if (IS_ERR(new))
6778572b8e2STrond Myklebust 		return PTR_ERR(new);
678a17c2153STrond Myklebust 	put_rpccred(req->rq_cred);
679a17c2153STrond Myklebust 	req->rq_cred = new;
6808572b8e2STrond Myklebust 	return 0;
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds void
put_rpccred(struct rpc_cred * cred)6841da177e4SLinus Torvalds put_rpccred(struct rpc_cred *cred)
6851da177e4SLinus Torvalds {
6869a8f6b5eSTrond Myklebust 	if (cred == NULL)
6879a8f6b5eSTrond Myklebust 		return;
68895cd6232STrond Myklebust 	rcu_read_lock();
68979b18181STrond Myklebust 	if (refcount_dec_and_test(&cred->cr_count))
69095cd6232STrond Myklebust 		goto destroy;
69179b18181STrond Myklebust 	if (refcount_read(&cred->cr_count) != 1 ||
69295cd6232STrond Myklebust 	    !test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags))
69395cd6232STrond Myklebust 		goto out;
694f0380f3dSTrond Myklebust 	if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
695e092bdcdSTrond Myklebust 		cred->cr_expire = jiffies;
69695cd6232STrond Myklebust 		rpcauth_lru_add(cred);
69795cd6232STrond Myklebust 		/* Race breaker */
69895cd6232STrond Myklebust 		if (unlikely(!test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags)))
69995cd6232STrond Myklebust 			rpcauth_lru_remove(cred);
70095cd6232STrond Myklebust 	} else if (rpcauth_unhash_cred(cred)) {
70195cd6232STrond Myklebust 		rpcauth_lru_remove(cred);
70279b18181STrond Myklebust 		if (refcount_dec_and_test(&cred->cr_count))
70395cd6232STrond Myklebust 			goto destroy;
704f0380f3dSTrond Myklebust 	}
70595cd6232STrond Myklebust out:
70695cd6232STrond Myklebust 	rcu_read_unlock();
707f0380f3dSTrond Myklebust 	return;
70895cd6232STrond Myklebust destroy:
70995cd6232STrond Myklebust 	rcu_read_unlock();
71095cd6232STrond Myklebust 	cred->cr_ops->crdestroy(cred);
7111da177e4SLinus Torvalds }
712e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(put_rpccred);
7131da177e4SLinus Torvalds 
714e8680a24SChuck Lever /**
715e8680a24SChuck Lever  * rpcauth_marshcred - Append RPC credential to end of @xdr
716e8680a24SChuck Lever  * @task: controlling RPC task
717e8680a24SChuck Lever  * @xdr: xdr_stream containing initial portion of RPC Call header
718e8680a24SChuck Lever  *
719e8680a24SChuck Lever  * On success, an appropriate verifier is added to @xdr, @xdr is
720e8680a24SChuck Lever  * updated to point past the verifier, and zero is returned.
721e8680a24SChuck Lever  * Otherwise, @xdr is in an undefined state and a negative errno
722e8680a24SChuck Lever  * is returned.
723e8680a24SChuck Lever  */
rpcauth_marshcred(struct rpc_task * task,struct xdr_stream * xdr)724e8680a24SChuck Lever int rpcauth_marshcred(struct rpc_task *task, struct xdr_stream *xdr)
7251da177e4SLinus Torvalds {
726e8680a24SChuck Lever 	const struct rpc_credops *ops = task->tk_rqstp->rq_cred->cr_ops;
7271da177e4SLinus Torvalds 
728e8680a24SChuck Lever 	return ops->crmarshal(task, xdr);
7291da177e4SLinus Torvalds }
7301da177e4SLinus Torvalds 
731e8680a24SChuck Lever /**
732e8680a24SChuck Lever  * rpcauth_wrap_req_encode - XDR encode the RPC procedure
733e8680a24SChuck Lever  * @task: controlling RPC task
734e8680a24SChuck Lever  * @xdr: stream where on-the-wire bytes are to be marshalled
735e8680a24SChuck Lever  *
736e8680a24SChuck Lever  * On success, @xdr contains the encoded and wrapped message.
737e8680a24SChuck Lever  * Otherwise, @xdr is in an undefined state.
738e8680a24SChuck Lever  */
rpcauth_wrap_req_encode(struct rpc_task * task,struct xdr_stream * xdr)739e8680a24SChuck Lever int rpcauth_wrap_req_encode(struct rpc_task *task, struct xdr_stream *xdr)
7409f06c719SChuck Lever {
741e8680a24SChuck Lever 	kxdreproc_t encode = task->tk_msg.rpc_proc->p_encode;
7429f06c719SChuck Lever 
743e8680a24SChuck Lever 	encode(task->tk_rqstp, xdr, task->tk_msg.rpc_argp);
7449f06c719SChuck Lever 	return 0;
7451da177e4SLinus Torvalds }
746e8680a24SChuck Lever EXPORT_SYMBOL_GPL(rpcauth_wrap_req_encode);
747e8680a24SChuck Lever 
748e8680a24SChuck Lever /**
749e8680a24SChuck Lever  * rpcauth_wrap_req - XDR encode and wrap the RPC procedure
750e8680a24SChuck Lever  * @task: controlling RPC task
751e8680a24SChuck Lever  * @xdr: stream where on-the-wire bytes are to be marshalled
752e8680a24SChuck Lever  *
753e8680a24SChuck Lever  * On success, @xdr contains the encoded and wrapped message,
754e8680a24SChuck Lever  * and zero is returned. Otherwise, @xdr is in an undefined
755e8680a24SChuck Lever  * state and a negative errno is returned.
756e8680a24SChuck Lever  */
rpcauth_wrap_req(struct rpc_task * task,struct xdr_stream * xdr)757e8680a24SChuck Lever int rpcauth_wrap_req(struct rpc_task *task, struct xdr_stream *xdr)
758e8680a24SChuck Lever {
759e8680a24SChuck Lever 	const struct rpc_credops *ops = task->tk_rqstp->rq_cred->cr_ops;
760e8680a24SChuck Lever 
761e8680a24SChuck Lever 	return ops->crwrap_req(task, xdr);
762e8680a24SChuck Lever }
7631da177e4SLinus Torvalds 
764a0584ee9SChuck Lever /**
765a0584ee9SChuck Lever  * rpcauth_checkverf - Validate verifier in RPC Reply header
766a0584ee9SChuck Lever  * @task: controlling RPC task
767a0584ee9SChuck Lever  * @xdr: xdr_stream containing RPC Reply header
768a0584ee9SChuck Lever  *
769*5623ecfcSChuck Lever  * Return values:
770*5623ecfcSChuck Lever  *   %0: Verifier is valid. @xdr now points past the verifier.
771*5623ecfcSChuck Lever  *   %-EIO: Verifier is corrupted or message ended early.
772*5623ecfcSChuck Lever  *   %-EACCES: Verifier is intact but not valid.
773*5623ecfcSChuck Lever  *   %-EPROTONOSUPPORT: Server does not support the requested auth type.
774*5623ecfcSChuck Lever  *
775*5623ecfcSChuck Lever  * When a negative errno is returned, @xdr is left in an undefined
776*5623ecfcSChuck Lever  * state.
777a0584ee9SChuck Lever  */
778a0584ee9SChuck Lever int
rpcauth_checkverf(struct rpc_task * task,struct xdr_stream * xdr)779a0584ee9SChuck Lever rpcauth_checkverf(struct rpc_task *task, struct xdr_stream *xdr)
780bf269551SChuck Lever {
781a0584ee9SChuck Lever 	const struct rpc_credops *ops = task->tk_rqstp->rq_cred->cr_ops;
782bf269551SChuck Lever 
783a0584ee9SChuck Lever 	return ops->crvalidate(task, xdr);
784bf269551SChuck Lever }
785bf269551SChuck Lever 
786a0584ee9SChuck Lever /**
787a0584ee9SChuck Lever  * rpcauth_unwrap_resp_decode - Invoke XDR decode function
788a0584ee9SChuck Lever  * @task: controlling RPC task
789a0584ee9SChuck Lever  * @xdr: stream where the Reply message resides
790a0584ee9SChuck Lever  *
791a0584ee9SChuck Lever  * Returns zero on success; otherwise a negative errno is returned.
792a0584ee9SChuck Lever  */
7931da177e4SLinus Torvalds int
rpcauth_unwrap_resp_decode(struct rpc_task * task,struct xdr_stream * xdr)794a0584ee9SChuck Lever rpcauth_unwrap_resp_decode(struct rpc_task *task, struct xdr_stream *xdr)
7951da177e4SLinus Torvalds {
796a0584ee9SChuck Lever 	kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
7971da177e4SLinus Torvalds 
798a0584ee9SChuck Lever 	return decode(task->tk_rqstp, xdr, task->tk_msg.rpc_resp);
799a0584ee9SChuck Lever }
800a0584ee9SChuck Lever EXPORT_SYMBOL_GPL(rpcauth_unwrap_resp_decode);
801a0584ee9SChuck Lever 
802a0584ee9SChuck Lever /**
803a0584ee9SChuck Lever  * rpcauth_unwrap_resp - Invoke unwrap and decode function for the cred
804a0584ee9SChuck Lever  * @task: controlling RPC task
805a0584ee9SChuck Lever  * @xdr: stream where the Reply message resides
806a0584ee9SChuck Lever  *
807a0584ee9SChuck Lever  * Returns zero on success; otherwise a negative errno is returned.
808a0584ee9SChuck Lever  */
809a0584ee9SChuck Lever int
rpcauth_unwrap_resp(struct rpc_task * task,struct xdr_stream * xdr)810a0584ee9SChuck Lever rpcauth_unwrap_resp(struct rpc_task *task, struct xdr_stream *xdr)
811a0584ee9SChuck Lever {
812a0584ee9SChuck Lever 	const struct rpc_credops *ops = task->tk_rqstp->rq_cred->cr_ops;
813a0584ee9SChuck Lever 
814a0584ee9SChuck Lever 	return ops->crunwrap_resp(task, xdr);
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
8173021a5bbSTrond Myklebust bool
rpcauth_xmit_need_reencode(struct rpc_task * task)8183021a5bbSTrond Myklebust rpcauth_xmit_need_reencode(struct rpc_task *task)
8193021a5bbSTrond Myklebust {
8203021a5bbSTrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
8213021a5bbSTrond Myklebust 
8223021a5bbSTrond Myklebust 	if (!cred || !cred->cr_ops->crneed_reencode)
8233021a5bbSTrond Myklebust 		return false;
8243021a5bbSTrond Myklebust 	return cred->cr_ops->crneed_reencode(task);
8253021a5bbSTrond Myklebust }
8263021a5bbSTrond Myklebust 
8271da177e4SLinus Torvalds int
rpcauth_refreshcred(struct rpc_task * task)8281da177e4SLinus Torvalds rpcauth_refreshcred(struct rpc_task *task)
8291da177e4SLinus Torvalds {
8309a84d380STrond Myklebust 	struct rpc_cred	*cred;
8311da177e4SLinus Torvalds 	int err;
8321da177e4SLinus Torvalds 
833a17c2153STrond Myklebust 	cred = task->tk_rqstp->rq_cred;
834a17c2153STrond Myklebust 	if (cred == NULL) {
835a17c2153STrond Myklebust 		err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
836a17c2153STrond Myklebust 		if (err < 0)
837a17c2153STrond Myklebust 			goto out;
838a17c2153STrond Myklebust 		cred = task->tk_rqstp->rq_cred;
839f81c6224SJoe Perches 	}
8400bbacc40SChuck Lever 
8411da177e4SLinus Torvalds 	err = cred->cr_ops->crrefresh(task);
842a17c2153STrond Myklebust out:
8431da177e4SLinus Torvalds 	if (err < 0)
8441da177e4SLinus Torvalds 		task->tk_status = err;
8451da177e4SLinus Torvalds 	return err;
8461da177e4SLinus Torvalds }
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds void
rpcauth_invalcred(struct rpc_task * task)8491da177e4SLinus Torvalds rpcauth_invalcred(struct rpc_task *task)
8501da177e4SLinus Torvalds {
851a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
852fc432dd9STrond Myklebust 
853fc432dd9STrond Myklebust 	if (cred)
854fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds int
rpcauth_uptodatecred(struct rpc_task * task)8581da177e4SLinus Torvalds rpcauth_uptodatecred(struct rpc_task *task)
8591da177e4SLinus Torvalds {
860a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
861fc432dd9STrond Myklebust 
862fc432dd9STrond Myklebust 	return cred == NULL ||
863fc432dd9STrond Myklebust 		test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
8641da177e4SLinus Torvalds }
865f5c2187cSTrond Myklebust 
8668e1f936bSRusty Russell static struct shrinker rpc_cred_shrinker = {
86770534a73SDave Chinner 	.count_objects = rpcauth_cache_shrink_count,
86870534a73SDave Chinner 	.scan_objects = rpcauth_cache_shrink_scan,
8698e1f936bSRusty Russell 	.seeks = DEFAULT_SEEKS,
8708e1f936bSRusty Russell };
871f5c2187cSTrond Myklebust 
rpcauth_init_module(void)8725d8d9a4dSTrond Myklebust int __init rpcauth_init_module(void)
873f5c2187cSTrond Myklebust {
8745d8d9a4dSTrond Myklebust 	int err;
8755d8d9a4dSTrond Myklebust 
8765d8d9a4dSTrond Myklebust 	err = rpc_init_authunix();
8775d8d9a4dSTrond Myklebust 	if (err < 0)
8785d8d9a4dSTrond Myklebust 		goto out1;
879e33c267aSRoman Gushchin 	err = register_shrinker(&rpc_cred_shrinker, "sunrpc_cred");
8802864486bSKinglong Mee 	if (err < 0)
88189a4f758SNeilBrown 		goto out2;
8825d8d9a4dSTrond Myklebust 	return 0;
8835d8d9a4dSTrond Myklebust out2:
8845d8d9a4dSTrond Myklebust 	rpc_destroy_authunix();
8855d8d9a4dSTrond Myklebust out1:
8865d8d9a4dSTrond Myklebust 	return err;
887f5c2187cSTrond Myklebust }
888f5c2187cSTrond Myklebust 
rpcauth_remove_module(void)889c135e84aSStephen Rothwell void rpcauth_remove_module(void)
890f5c2187cSTrond Myklebust {
8915d8d9a4dSTrond Myklebust 	rpc_destroy_authunix();
8928e1f936bSRusty Russell 	unregister_shrinker(&rpc_cred_shrinker);
893f5c2187cSTrond Myklebust }
894