xref: /openbmc/linux/net/sunrpc/auth.c (revision 9c27847d)
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;
54241269bdSTrond Myklebust 	nbits = fls(num);
55241269bdSTrond Myklebust 	if (num > (1U << nbits))
56241269bdSTrond Myklebust 		nbits++;
57241269bdSTrond Myklebust 	if (nbits > MAX_HASHTABLE_BITS || nbits < 2)
58241269bdSTrond Myklebust 		goto out_inval;
59241269bdSTrond Myklebust 	*(unsigned int *)kp->arg = nbits;
60241269bdSTrond Myklebust 	return 0;
61241269bdSTrond Myklebust out_inval:
62241269bdSTrond Myklebust 	return -EINVAL;
63241269bdSTrond Myklebust }
64241269bdSTrond Myklebust 
658e4e15d4SStephen Rothwell static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp)
66241269bdSTrond Myklebust {
67241269bdSTrond Myklebust 	unsigned int nbits;
68241269bdSTrond Myklebust 
69241269bdSTrond Myklebust 	nbits = *(unsigned int *)kp->arg;
70241269bdSTrond Myklebust 	return sprintf(buffer, "%u", 1U << nbits);
71241269bdSTrond Myklebust }
72241269bdSTrond Myklebust 
73241269bdSTrond Myklebust #define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
74241269bdSTrond Myklebust 
759c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_hashtbl_sz = {
768e4e15d4SStephen Rothwell 	.set = param_set_hashtbl_sz,
778e4e15d4SStephen Rothwell 	.get = param_get_hashtbl_sz,
788e4e15d4SStephen Rothwell };
798e4e15d4SStephen Rothwell 
80241269bdSTrond Myklebust module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
81241269bdSTrond Myklebust MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
82241269bdSTrond Myklebust 
83bae6746fSTrond Myklebust static unsigned long auth_max_cred_cachesize = ULONG_MAX;
84bae6746fSTrond Myklebust module_param(auth_max_cred_cachesize, ulong, 0644);
85bae6746fSTrond Myklebust MODULE_PARM_DESC(auth_max_cred_cachesize, "RPC credential maximum total cache size");
86bae6746fSTrond Myklebust 
871da177e4SLinus Torvalds static u32
881da177e4SLinus Torvalds pseudoflavor_to_flavor(u32 flavor) {
891c74a244SChuck Lever 	if (flavor > RPC_AUTH_MAXFLAVOR)
901da177e4SLinus Torvalds 		return RPC_AUTH_GSS;
911da177e4SLinus Torvalds 	return flavor;
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds int
95f1c0a861STrond Myklebust rpcauth_register(const struct rpc_authops *ops)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds 	rpc_authflavor_t flavor;
98fc1b356fSTrond Myklebust 	int ret = -EPERM;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
1011da177e4SLinus Torvalds 		return -EINVAL;
102fc1b356fSTrond Myklebust 	spin_lock(&rpc_authflavor_lock);
103fc1b356fSTrond Myklebust 	if (auth_flavors[flavor] == NULL) {
1041da177e4SLinus Torvalds 		auth_flavors[flavor] = ops;
105fc1b356fSTrond Myklebust 		ret = 0;
106fc1b356fSTrond Myklebust 	}
107fc1b356fSTrond Myklebust 	spin_unlock(&rpc_authflavor_lock);
108fc1b356fSTrond Myklebust 	return ret;
1091da177e4SLinus Torvalds }
110e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_register);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds int
113f1c0a861STrond Myklebust rpcauth_unregister(const struct rpc_authops *ops)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds 	rpc_authflavor_t flavor;
116fc1b356fSTrond Myklebust 	int ret = -EPERM;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
1191da177e4SLinus Torvalds 		return -EINVAL;
120fc1b356fSTrond Myklebust 	spin_lock(&rpc_authflavor_lock);
121fc1b356fSTrond Myklebust 	if (auth_flavors[flavor] == ops) {
1221da177e4SLinus Torvalds 		auth_flavors[flavor] = NULL;
123fc1b356fSTrond Myklebust 		ret = 0;
124fc1b356fSTrond Myklebust 	}
125fc1b356fSTrond Myklebust 	spin_unlock(&rpc_authflavor_lock);
126fc1b356fSTrond Myklebust 	return ret;
1271da177e4SLinus Torvalds }
128e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_unregister);
1291da177e4SLinus Torvalds 
1306a1a1e34SChuck Lever /**
1319568c5e9SChuck Lever  * rpcauth_get_pseudoflavor - check if security flavor is supported
1329568c5e9SChuck Lever  * @flavor: a security flavor
1339568c5e9SChuck Lever  * @info: a GSS mech OID, quality of protection, and service value
1349568c5e9SChuck Lever  *
1359568c5e9SChuck Lever  * Verifies that an appropriate kernel module is available or already loaded.
1369568c5e9SChuck Lever  * Returns an equivalent pseudoflavor, or RPC_AUTH_MAXFLAVOR if "flavor" is
1379568c5e9SChuck Lever  * not supported locally.
1389568c5e9SChuck Lever  */
1399568c5e9SChuck Lever rpc_authflavor_t
1409568c5e9SChuck Lever rpcauth_get_pseudoflavor(rpc_authflavor_t flavor, struct rpcsec_gss_info *info)
1419568c5e9SChuck Lever {
1429568c5e9SChuck Lever 	const struct rpc_authops *ops;
1439568c5e9SChuck Lever 	rpc_authflavor_t pseudoflavor;
1449568c5e9SChuck Lever 
1459568c5e9SChuck Lever 	ops = auth_flavors[flavor];
1469568c5e9SChuck Lever 	if (ops == NULL)
1479568c5e9SChuck Lever 		request_module("rpc-auth-%u", flavor);
1489568c5e9SChuck Lever 	spin_lock(&rpc_authflavor_lock);
1499568c5e9SChuck Lever 	ops = auth_flavors[flavor];
1509568c5e9SChuck Lever 	if (ops == NULL || !try_module_get(ops->owner)) {
1519568c5e9SChuck Lever 		spin_unlock(&rpc_authflavor_lock);
1529568c5e9SChuck Lever 		return RPC_AUTH_MAXFLAVOR;
1539568c5e9SChuck Lever 	}
1549568c5e9SChuck Lever 	spin_unlock(&rpc_authflavor_lock);
1559568c5e9SChuck Lever 
1569568c5e9SChuck Lever 	pseudoflavor = flavor;
1579568c5e9SChuck Lever 	if (ops->info2flavor != NULL)
1589568c5e9SChuck Lever 		pseudoflavor = ops->info2flavor(info);
1599568c5e9SChuck Lever 
1609568c5e9SChuck Lever 	module_put(ops->owner);
1619568c5e9SChuck Lever 	return pseudoflavor;
1629568c5e9SChuck Lever }
1639568c5e9SChuck Lever EXPORT_SYMBOL_GPL(rpcauth_get_pseudoflavor);
1649568c5e9SChuck Lever 
1659568c5e9SChuck Lever /**
166a77c806fSChuck Lever  * rpcauth_get_gssinfo - find GSS tuple matching a GSS pseudoflavor
167a77c806fSChuck Lever  * @pseudoflavor: GSS pseudoflavor to match
168a77c806fSChuck Lever  * @info: rpcsec_gss_info structure to fill in
169a77c806fSChuck Lever  *
170a77c806fSChuck Lever  * Returns zero and fills in "info" if pseudoflavor matches a
171a77c806fSChuck Lever  * supported mechanism.
172a77c806fSChuck Lever  */
173a77c806fSChuck Lever int
174a77c806fSChuck Lever rpcauth_get_gssinfo(rpc_authflavor_t pseudoflavor, struct rpcsec_gss_info *info)
175a77c806fSChuck Lever {
176a77c806fSChuck Lever 	rpc_authflavor_t flavor = pseudoflavor_to_flavor(pseudoflavor);
177a77c806fSChuck Lever 	const struct rpc_authops *ops;
178a77c806fSChuck Lever 	int result;
179a77c806fSChuck Lever 
1801c74a244SChuck Lever 	if (flavor >= RPC_AUTH_MAXFLAVOR)
1811c74a244SChuck Lever 		return -EINVAL;
1821c74a244SChuck Lever 
183a77c806fSChuck Lever 	ops = auth_flavors[flavor];
184a77c806fSChuck Lever 	if (ops == NULL)
185a77c806fSChuck Lever 		request_module("rpc-auth-%u", flavor);
186a77c806fSChuck Lever 	spin_lock(&rpc_authflavor_lock);
187a77c806fSChuck Lever 	ops = auth_flavors[flavor];
188a77c806fSChuck Lever 	if (ops == NULL || !try_module_get(ops->owner)) {
189a77c806fSChuck Lever 		spin_unlock(&rpc_authflavor_lock);
190a77c806fSChuck Lever 		return -ENOENT;
191a77c806fSChuck Lever 	}
192a77c806fSChuck Lever 	spin_unlock(&rpc_authflavor_lock);
193a77c806fSChuck Lever 
194a77c806fSChuck Lever 	result = -ENOENT;
195a77c806fSChuck Lever 	if (ops->flavor2info != NULL)
196a77c806fSChuck Lever 		result = ops->flavor2info(pseudoflavor, info);
197a77c806fSChuck Lever 
198a77c806fSChuck Lever 	module_put(ops->owner);
199a77c806fSChuck Lever 	return result;
200a77c806fSChuck Lever }
201a77c806fSChuck Lever EXPORT_SYMBOL_GPL(rpcauth_get_gssinfo);
202a77c806fSChuck Lever 
203a77c806fSChuck Lever /**
2046a1a1e34SChuck Lever  * rpcauth_list_flavors - discover registered flavors and pseudoflavors
2056a1a1e34SChuck Lever  * @array: array to fill in
2066a1a1e34SChuck Lever  * @size: size of "array"
2076a1a1e34SChuck Lever  *
2086a1a1e34SChuck Lever  * Returns the number of array items filled in, or a negative errno.
2096a1a1e34SChuck Lever  *
2106a1a1e34SChuck Lever  * The returned array is not sorted by any policy.  Callers should not
2116a1a1e34SChuck Lever  * rely on the order of the items in the returned array.
2126a1a1e34SChuck Lever  */
2136a1a1e34SChuck Lever int
2146a1a1e34SChuck Lever rpcauth_list_flavors(rpc_authflavor_t *array, int size)
2156a1a1e34SChuck Lever {
2166a1a1e34SChuck Lever 	rpc_authflavor_t flavor;
2176a1a1e34SChuck Lever 	int result = 0;
2186a1a1e34SChuck Lever 
2196a1a1e34SChuck Lever 	spin_lock(&rpc_authflavor_lock);
2206a1a1e34SChuck Lever 	for (flavor = 0; flavor < RPC_AUTH_MAXFLAVOR; flavor++) {
2216a1a1e34SChuck Lever 		const struct rpc_authops *ops = auth_flavors[flavor];
2226a1a1e34SChuck Lever 		rpc_authflavor_t pseudos[4];
2236a1a1e34SChuck Lever 		int i, len;
2246a1a1e34SChuck Lever 
2256a1a1e34SChuck Lever 		if (result >= size) {
2266a1a1e34SChuck Lever 			result = -ENOMEM;
2276a1a1e34SChuck Lever 			break;
2286a1a1e34SChuck Lever 		}
2296a1a1e34SChuck Lever 
2306a1a1e34SChuck Lever 		if (ops == NULL)
2316a1a1e34SChuck Lever 			continue;
2326a1a1e34SChuck Lever 		if (ops->list_pseudoflavors == NULL) {
2336a1a1e34SChuck Lever 			array[result++] = ops->au_flavor;
2346a1a1e34SChuck Lever 			continue;
2356a1a1e34SChuck Lever 		}
2366a1a1e34SChuck Lever 		len = ops->list_pseudoflavors(pseudos, ARRAY_SIZE(pseudos));
2376a1a1e34SChuck Lever 		if (len < 0) {
2386a1a1e34SChuck Lever 			result = len;
2396a1a1e34SChuck Lever 			break;
2406a1a1e34SChuck Lever 		}
2416a1a1e34SChuck Lever 		for (i = 0; i < len; i++) {
2426a1a1e34SChuck Lever 			if (result >= size) {
2436a1a1e34SChuck Lever 				result = -ENOMEM;
2446a1a1e34SChuck Lever 				break;
2456a1a1e34SChuck Lever 			}
2466a1a1e34SChuck Lever 			array[result++] = pseudos[i];
2476a1a1e34SChuck Lever 		}
2486a1a1e34SChuck Lever 	}
2496a1a1e34SChuck Lever 	spin_unlock(&rpc_authflavor_lock);
2506a1a1e34SChuck Lever 
2516a1a1e34SChuck Lever 	dprintk("RPC:       %s returns %d\n", __func__, result);
2526a1a1e34SChuck Lever 	return result;
2536a1a1e34SChuck Lever }
2546a1a1e34SChuck Lever EXPORT_SYMBOL_GPL(rpcauth_list_flavors);
2556a1a1e34SChuck Lever 
2561da177e4SLinus Torvalds struct rpc_auth *
257c2190661STrond Myklebust rpcauth_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	struct rpc_auth		*auth;
260f1c0a861STrond Myklebust 	const struct rpc_authops *ops;
261c2190661STrond Myklebust 	u32			flavor = pseudoflavor_to_flavor(args->pseudoflavor);
2621da177e4SLinus Torvalds 
263f344f6dfSOlaf Kirch 	auth = ERR_PTR(-EINVAL);
264f344f6dfSOlaf Kirch 	if (flavor >= RPC_AUTH_MAXFLAVOR)
265f344f6dfSOlaf Kirch 		goto out;
266f344f6dfSOlaf Kirch 
267f344f6dfSOlaf Kirch 	if ((ops = auth_flavors[flavor]) == NULL)
268f344f6dfSOlaf Kirch 		request_module("rpc-auth-%u", flavor);
269fc1b356fSTrond Myklebust 	spin_lock(&rpc_authflavor_lock);
270fc1b356fSTrond Myklebust 	ops = auth_flavors[flavor];
271fc1b356fSTrond Myklebust 	if (ops == NULL || !try_module_get(ops->owner)) {
272fc1b356fSTrond Myklebust 		spin_unlock(&rpc_authflavor_lock);
273f344f6dfSOlaf Kirch 		goto out;
274fc1b356fSTrond Myklebust 	}
275fc1b356fSTrond Myklebust 	spin_unlock(&rpc_authflavor_lock);
276c2190661STrond Myklebust 	auth = ops->create(args, clnt);
277fc1b356fSTrond Myklebust 	module_put(ops->owner);
2786a19275aSJ. Bruce Fields 	if (IS_ERR(auth))
2796a19275aSJ. Bruce Fields 		return auth;
2801da177e4SLinus Torvalds 	if (clnt->cl_auth)
281de7a8ce3STrond Myklebust 		rpcauth_release(clnt->cl_auth);
2821da177e4SLinus Torvalds 	clnt->cl_auth = auth;
283f344f6dfSOlaf Kirch 
284f344f6dfSOlaf Kirch out:
2851da177e4SLinus Torvalds 	return auth;
2861da177e4SLinus Torvalds }
287e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_create);
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds void
290de7a8ce3STrond Myklebust rpcauth_release(struct rpc_auth *auth)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	if (!atomic_dec_and_test(&auth->au_count))
2931da177e4SLinus Torvalds 		return;
2941da177e4SLinus Torvalds 	auth->au_ops->destroy(auth);
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds static DEFINE_SPINLOCK(rpc_credcache_lock);
2981da177e4SLinus Torvalds 
29931be5bf1STrond Myklebust static void
30031be5bf1STrond Myklebust rpcauth_unhash_cred_locked(struct rpc_cred *cred)
30131be5bf1STrond Myklebust {
30231be5bf1STrond Myklebust 	hlist_del_rcu(&cred->cr_hash);
3034e857c58SPeter Zijlstra 	smp_mb__before_atomic();
30431be5bf1STrond Myklebust 	clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
30531be5bf1STrond Myklebust }
30631be5bf1STrond Myklebust 
307f0380f3dSTrond Myklebust static int
3089499b434STrond Myklebust rpcauth_unhash_cred(struct rpc_cred *cred)
3099499b434STrond Myklebust {
3109499b434STrond Myklebust 	spinlock_t *cache_lock;
311f0380f3dSTrond Myklebust 	int ret;
3129499b434STrond Myklebust 
3139499b434STrond Myklebust 	cache_lock = &cred->cr_auth->au_credcache->lock;
3149499b434STrond Myklebust 	spin_lock(cache_lock);
315f0380f3dSTrond Myklebust 	ret = atomic_read(&cred->cr_count) == 0;
316f0380f3dSTrond Myklebust 	if (ret)
3179499b434STrond Myklebust 		rpcauth_unhash_cred_locked(cred);
3189499b434STrond Myklebust 	spin_unlock(cache_lock);
319f0380f3dSTrond Myklebust 	return ret;
3209499b434STrond Myklebust }
3219499b434STrond Myklebust 
3221da177e4SLinus Torvalds /*
3231da177e4SLinus Torvalds  * Initialize RPC credential cache
3241da177e4SLinus Torvalds  */
3251da177e4SLinus Torvalds int
326f5c2187cSTrond Myklebust rpcauth_init_credcache(struct rpc_auth *auth)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	struct rpc_cred_cache *new;
329988664a0STrond Myklebust 	unsigned int hashsize;
3301da177e4SLinus Torvalds 
3318b3a7005SKris Katterjohn 	new = kmalloc(sizeof(*new), GFP_KERNEL);
3321da177e4SLinus Torvalds 	if (!new)
333241269bdSTrond Myklebust 		goto out_nocache;
334241269bdSTrond Myklebust 	new->hashbits = auth_hashbits;
335988664a0STrond Myklebust 	hashsize = 1U << new->hashbits;
336241269bdSTrond Myklebust 	new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
337241269bdSTrond Myklebust 	if (!new->hashtable)
338241269bdSTrond Myklebust 		goto out_nohashtbl;
3399499b434STrond Myklebust 	spin_lock_init(&new->lock);
3401da177e4SLinus Torvalds 	auth->au_credcache = new;
3411da177e4SLinus Torvalds 	return 0;
342241269bdSTrond Myklebust out_nohashtbl:
343241269bdSTrond Myklebust 	kfree(new);
344241269bdSTrond Myklebust out_nocache:
345241269bdSTrond Myklebust 	return -ENOMEM;
3461da177e4SLinus Torvalds }
347e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds /*
3504de6caa2SAndy Adamson  * Setup a credential key lifetime timeout notification
3514de6caa2SAndy Adamson  */
3524de6caa2SAndy Adamson int
3534de6caa2SAndy Adamson rpcauth_key_timeout_notify(struct rpc_auth *auth, struct rpc_cred *cred)
3544de6caa2SAndy Adamson {
3554de6caa2SAndy Adamson 	if (!cred->cr_auth->au_ops->key_timeout)
3564de6caa2SAndy Adamson 		return 0;
3574de6caa2SAndy Adamson 	return cred->cr_auth->au_ops->key_timeout(auth, cred);
3584de6caa2SAndy Adamson }
3594de6caa2SAndy Adamson EXPORT_SYMBOL_GPL(rpcauth_key_timeout_notify);
3604de6caa2SAndy Adamson 
3614de6caa2SAndy Adamson bool
3624de6caa2SAndy Adamson rpcauth_cred_key_to_expire(struct rpc_cred *cred)
3634de6caa2SAndy Adamson {
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) &&
467eac0d18dSTrond Myklebust 		    test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
46870534a73SDave Chinner 			break;
469eac0d18dSTrond Myklebust 
470e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
471f5c2187cSTrond Myklebust 		number_cred_unused--;
47270534a73SDave Chinner 		freed++;
473e092bdcdSTrond Myklebust 		if (atomic_read(&cred->cr_count) != 0)
474e092bdcdSTrond Myklebust 			continue;
475eac0d18dSTrond Myklebust 
4769499b434STrond Myklebust 		cache_lock = &cred->cr_auth->au_credcache->lock;
4779499b434STrond Myklebust 		spin_lock(cache_lock);
4789499b434STrond Myklebust 		if (atomic_read(&cred->cr_count) == 0) {
479e092bdcdSTrond Myklebust 			get_rpccred(cred);
480e092bdcdSTrond Myklebust 			list_add_tail(&cred->cr_lru, free);
48131be5bf1STrond Myklebust 			rpcauth_unhash_cred_locked(cred);
4821da177e4SLinus Torvalds 		}
4839499b434STrond Myklebust 		spin_unlock(cache_lock);
4849499b434STrond Myklebust 	}
48570534a73SDave Chinner 	return freed;
4861da177e4SLinus Torvalds }
487e092bdcdSTrond Myklebust 
488bae6746fSTrond Myklebust static unsigned long
489bae6746fSTrond Myklebust rpcauth_cache_do_shrink(int nr_to_scan)
490bae6746fSTrond Myklebust {
491bae6746fSTrond Myklebust 	LIST_HEAD(free);
492bae6746fSTrond Myklebust 	unsigned long freed;
493bae6746fSTrond Myklebust 
494bae6746fSTrond Myklebust 	spin_lock(&rpc_credcache_lock);
495bae6746fSTrond Myklebust 	freed = rpcauth_prune_expired(&free, nr_to_scan);
496bae6746fSTrond Myklebust 	spin_unlock(&rpc_credcache_lock);
497bae6746fSTrond Myklebust 	rpcauth_destroy_credlist(&free);
498bae6746fSTrond Myklebust 
499bae6746fSTrond Myklebust 	return freed;
500bae6746fSTrond Myklebust }
501bae6746fSTrond Myklebust 
502e092bdcdSTrond Myklebust /*
503f5c2187cSTrond Myklebust  * Run memory cache shrinker.
504e092bdcdSTrond Myklebust  */
50570534a73SDave Chinner static unsigned long
50670534a73SDave Chinner rpcauth_cache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
50770534a73SDave Chinner 
508e092bdcdSTrond Myklebust {
50970534a73SDave Chinner 	if ((sc->gfp_mask & GFP_KERNEL) != GFP_KERNEL)
51070534a73SDave Chinner 		return SHRINK_STOP;
51170534a73SDave Chinner 
51270534a73SDave Chinner 	/* nothing left, don't come back */
513f5c2187cSTrond Myklebust 	if (list_empty(&cred_unused))
51470534a73SDave Chinner 		return SHRINK_STOP;
51570534a73SDave Chinner 
516bae6746fSTrond Myklebust 	return rpcauth_cache_do_shrink(sc->nr_to_scan);
51770534a73SDave Chinner }
51870534a73SDave Chinner 
51970534a73SDave Chinner static unsigned long
52070534a73SDave Chinner rpcauth_cache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
52170534a73SDave Chinner 
52270534a73SDave Chinner {
52370534a73SDave Chinner 	return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
5241da177e4SLinus Torvalds }
5251da177e4SLinus Torvalds 
526bae6746fSTrond Myklebust static void
527bae6746fSTrond Myklebust rpcauth_cache_enforce_limit(void)
528bae6746fSTrond Myklebust {
529bae6746fSTrond Myklebust 	unsigned long diff;
530bae6746fSTrond Myklebust 	unsigned int nr_to_scan;
531bae6746fSTrond Myklebust 
532bae6746fSTrond Myklebust 	if (number_cred_unused <= auth_max_cred_cachesize)
533bae6746fSTrond Myklebust 		return;
534bae6746fSTrond Myklebust 	diff = number_cred_unused - auth_max_cred_cachesize;
535bae6746fSTrond Myklebust 	nr_to_scan = 100;
536bae6746fSTrond Myklebust 	if (diff < nr_to_scan)
537bae6746fSTrond Myklebust 		nr_to_scan = diff;
538bae6746fSTrond Myklebust 	rpcauth_cache_do_shrink(nr_to_scan);
539bae6746fSTrond Myklebust }
540bae6746fSTrond Myklebust 
5411da177e4SLinus Torvalds /*
5421da177e4SLinus Torvalds  * Look up a process' credentials in the authentication cache
5431da177e4SLinus Torvalds  */
5441da177e4SLinus Torvalds struct rpc_cred *
5451da177e4SLinus Torvalds rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
5468a317760STrond Myklebust 		int flags)
5471da177e4SLinus Torvalds {
548e092bdcdSTrond Myklebust 	LIST_HEAD(free);
5491da177e4SLinus Torvalds 	struct rpc_cred_cache *cache = auth->au_credcache;
55031be5bf1STrond Myklebust 	struct rpc_cred	*cred = NULL,
55131be5bf1STrond Myklebust 			*entry, *new;
55225337fdcSTrond Myklebust 	unsigned int nr;
55325337fdcSTrond Myklebust 
5549e469e30SEric W. Biederman 	nr = hash_long(from_kuid(&init_user_ns, acred->uid), cache->hashbits);
5551da177e4SLinus Torvalds 
55631be5bf1STrond Myklebust 	rcu_read_lock();
557b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
55831be5bf1STrond Myklebust 		if (!entry->cr_ops->crmatch(acred, entry, flags))
55931be5bf1STrond Myklebust 			continue;
560bd956080SNeilBrown 		if (flags & RPCAUTH_LOOKUP_RCU) {
561bd956080SNeilBrown 			if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) &&
562bd956080SNeilBrown 			    !test_bit(RPCAUTH_CRED_NEW, &entry->cr_flags))
563bd956080SNeilBrown 				cred = entry;
564bd956080SNeilBrown 			break;
565bd956080SNeilBrown 		}
5669499b434STrond Myklebust 		spin_lock(&cache->lock);
56731be5bf1STrond Myklebust 		if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
5689499b434STrond Myklebust 			spin_unlock(&cache->lock);
56931be5bf1STrond Myklebust 			continue;
57031be5bf1STrond Myklebust 		}
57131be5bf1STrond Myklebust 		cred = get_rpccred(entry);
5729499b434STrond Myklebust 		spin_unlock(&cache->lock);
57331be5bf1STrond Myklebust 		break;
57431be5bf1STrond Myklebust 	}
57531be5bf1STrond Myklebust 	rcu_read_unlock();
57631be5bf1STrond Myklebust 
5779499b434STrond Myklebust 	if (cred != NULL)
57831be5bf1STrond Myklebust 		goto found;
57931be5bf1STrond Myklebust 
580bd956080SNeilBrown 	if (flags & RPCAUTH_LOOKUP_RCU)
581bd956080SNeilBrown 		return ERR_PTR(-ECHILD);
582bd956080SNeilBrown 
58331be5bf1STrond Myklebust 	new = auth->au_ops->crcreate(auth, acred, flags);
58431be5bf1STrond Myklebust 	if (IS_ERR(new)) {
58531be5bf1STrond Myklebust 		cred = new;
58631be5bf1STrond Myklebust 		goto out;
58731be5bf1STrond Myklebust 	}
58831be5bf1STrond Myklebust 
5899499b434STrond Myklebust 	spin_lock(&cache->lock);
590b67bfe0dSSasha Levin 	hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
591e092bdcdSTrond Myklebust 		if (!entry->cr_ops->crmatch(acred, entry, flags))
592e092bdcdSTrond Myklebust 			continue;
593e092bdcdSTrond Myklebust 		cred = get_rpccred(entry);
5941da177e4SLinus Torvalds 		break;
5951da177e4SLinus Torvalds 	}
59631be5bf1STrond Myklebust 	if (cred == NULL) {
59731be5bf1STrond Myklebust 		cred = new;
59831be5bf1STrond Myklebust 		set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
59931be5bf1STrond Myklebust 		hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
60031be5bf1STrond Myklebust 	} else
601e092bdcdSTrond Myklebust 		list_add_tail(&new->cr_lru, &free);
6029499b434STrond Myklebust 	spin_unlock(&cache->lock);
603bae6746fSTrond Myklebust 	rpcauth_cache_enforce_limit();
60431be5bf1STrond Myklebust found:
605f64f9e71SJoe Perches 	if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
606f64f9e71SJoe Perches 	    cred->cr_ops->cr_init != NULL &&
607f64f9e71SJoe Perches 	    !(flags & RPCAUTH_LOOKUP_NEW)) {
608fba3bad4STrond Myklebust 		int res = cred->cr_ops->cr_init(auth, cred);
609fba3bad4STrond Myklebust 		if (res < 0) {
610fba3bad4STrond Myklebust 			put_rpccred(cred);
611fba3bad4STrond Myklebust 			cred = ERR_PTR(res);
612fba3bad4STrond Myklebust 		}
6131da177e4SLinus Torvalds 	}
61431be5bf1STrond Myklebust 	rpcauth_destroy_credlist(&free);
61531be5bf1STrond Myklebust out:
61631be5bf1STrond Myklebust 	return cred;
6171da177e4SLinus Torvalds }
618e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds struct rpc_cred *
6218a317760STrond Myklebust rpcauth_lookupcred(struct rpc_auth *auth, int flags)
6221da177e4SLinus Torvalds {
62386a264abSDavid Howells 	struct auth_cred acred;
6241da177e4SLinus Torvalds 	struct rpc_cred *ret;
62586a264abSDavid Howells 	const struct cred *cred = current_cred();
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	dprintk("RPC:       looking up %s cred\n",
6281da177e4SLinus Torvalds 		auth->au_ops->au_name);
62986a264abSDavid Howells 
63086a264abSDavid Howells 	memset(&acred, 0, sizeof(acred));
63186a264abSDavid Howells 	acred.uid = cred->fsuid;
63286a264abSDavid Howells 	acred.gid = cred->fsgid;
633122a8cdaSNeilBrown 	acred.group_info = cred->group_info;
6348a317760STrond Myklebust 	ret = auth->au_ops->lookup_cred(auth, &acred, flags);
6351da177e4SLinus Torvalds 	return ret;
6361da177e4SLinus Torvalds }
63766b06860SAndy Adamson EXPORT_SYMBOL_GPL(rpcauth_lookupcred);
6381da177e4SLinus Torvalds 
6395fe4755eSTrond Myklebust void
6405fe4755eSTrond Myklebust rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
6415fe4755eSTrond Myklebust 		  struct rpc_auth *auth, const struct rpc_credops *ops)
6425fe4755eSTrond Myklebust {
6435fe4755eSTrond Myklebust 	INIT_HLIST_NODE(&cred->cr_hash);
644e092bdcdSTrond Myklebust 	INIT_LIST_HEAD(&cred->cr_lru);
6455fe4755eSTrond Myklebust 	atomic_set(&cred->cr_count, 1);
6465fe4755eSTrond Myklebust 	cred->cr_auth = auth;
6475fe4755eSTrond Myklebust 	cred->cr_ops = ops;
6485fe4755eSTrond Myklebust 	cred->cr_expire = jiffies;
649f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
6505fe4755eSTrond Myklebust 	cred->cr_magic = RPCAUTH_CRED_MAGIC;
6515fe4755eSTrond Myklebust #endif
6525fe4755eSTrond Myklebust 	cred->cr_uid = acred->uid;
6535fe4755eSTrond Myklebust }
654e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_init_cred);
6555fe4755eSTrond Myklebust 
6568572b8e2STrond Myklebust struct rpc_cred *
6575d351754STrond Myklebust rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
6584ccda2cdSTrond Myklebust {
6594ccda2cdSTrond Myklebust 	dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
6604ccda2cdSTrond Myklebust 			cred->cr_auth->au_ops->au_name, cred);
6618572b8e2STrond Myklebust 	return get_rpccred(cred);
6624ccda2cdSTrond Myklebust }
6635c691044STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
6644ccda2cdSTrond Myklebust 
6658572b8e2STrond Myklebust static struct rpc_cred *
6665d351754STrond Myklebust rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
6671da177e4SLinus Torvalds {
6681be27f36STrond Myklebust 	struct rpc_auth *auth = task->tk_client->cl_auth;
6691da177e4SLinus Torvalds 	struct auth_cred acred = {
670bf37f794SEric W. Biederman 		.uid = GLOBAL_ROOT_UID,
671bf37f794SEric W. Biederman 		.gid = GLOBAL_ROOT_GID,
6721da177e4SLinus Torvalds 	};
6731da177e4SLinus Torvalds 
67446121cf7SChuck Lever 	dprintk("RPC: %5u looking up %s cred\n",
6751be27f36STrond Myklebust 		task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
6768572b8e2STrond Myklebust 	return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
677af093835STrond Myklebust }
678af093835STrond Myklebust 
6798572b8e2STrond Myklebust static struct rpc_cred *
6805d351754STrond Myklebust rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
681af093835STrond Myklebust {
682af093835STrond Myklebust 	struct rpc_auth *auth = task->tk_client->cl_auth;
683af093835STrond Myklebust 
684af093835STrond Myklebust 	dprintk("RPC: %5u looking up %s cred\n",
685af093835STrond Myklebust 		task->tk_pid, auth->au_ops->au_name);
6868572b8e2STrond Myklebust 	return rpcauth_lookupcred(auth, lookupflags);
6871da177e4SLinus Torvalds }
6881da177e4SLinus Torvalds 
689a17c2153STrond Myklebust static int
6904ccda2cdSTrond Myklebust rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
6911da177e4SLinus Torvalds {
692a17c2153STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
6938572b8e2STrond Myklebust 	struct rpc_cred *new;
6945d351754STrond Myklebust 	int lookupflags = 0;
6955d351754STrond Myklebust 
6965d351754STrond Myklebust 	if (flags & RPC_TASK_ASYNC)
6975d351754STrond Myklebust 		lookupflags |= RPCAUTH_LOOKUP_NEW;
6984ccda2cdSTrond Myklebust 	if (cred != NULL)
6998572b8e2STrond Myklebust 		new = cred->cr_ops->crbind(task, cred, lookupflags);
7004ccda2cdSTrond Myklebust 	else if (flags & RPC_TASK_ROOTCREDS)
7018572b8e2STrond Myklebust 		new = rpcauth_bind_root_cred(task, lookupflags);
7024ccda2cdSTrond Myklebust 	else
7038572b8e2STrond Myklebust 		new = rpcauth_bind_new_cred(task, lookupflags);
7048572b8e2STrond Myklebust 	if (IS_ERR(new))
7058572b8e2STrond Myklebust 		return PTR_ERR(new);
706a17c2153STrond Myklebust 	if (req->rq_cred != NULL)
707a17c2153STrond Myklebust 		put_rpccred(req->rq_cred);
708a17c2153STrond Myklebust 	req->rq_cred = new;
7098572b8e2STrond Myklebust 	return 0;
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds void
7131da177e4SLinus Torvalds put_rpccred(struct rpc_cred *cred)
7141da177e4SLinus Torvalds {
715e092bdcdSTrond Myklebust 	/* Fast path for unhashed credentials */
716f0380f3dSTrond Myklebust 	if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
717f0380f3dSTrond Myklebust 		if (atomic_dec_and_test(&cred->cr_count))
718f0380f3dSTrond Myklebust 			cred->cr_ops->crdestroy(cred);
7191da177e4SLinus Torvalds 		return;
720f0380f3dSTrond Myklebust 	}
721f0380f3dSTrond Myklebust 
722e092bdcdSTrond Myklebust 	if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
723e092bdcdSTrond Myklebust 		return;
724f5c2187cSTrond Myklebust 	if (!list_empty(&cred->cr_lru)) {
725f5c2187cSTrond Myklebust 		number_cred_unused--;
726e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
727f5c2187cSTrond Myklebust 	}
7285f707eb4STrond Myklebust 	if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
729f0380f3dSTrond Myklebust 		if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
730e092bdcdSTrond Myklebust 			cred->cr_expire = jiffies;
731e092bdcdSTrond Myklebust 			list_add_tail(&cred->cr_lru, &cred_unused);
732f5c2187cSTrond Myklebust 			number_cred_unused++;
733f0380f3dSTrond Myklebust 			goto out_nodestroy;
734f0380f3dSTrond Myklebust 		}
735f0380f3dSTrond Myklebust 		if (!rpcauth_unhash_cred(cred)) {
736f0380f3dSTrond Myklebust 			/* We were hashed and someone looked us up... */
737f0380f3dSTrond Myklebust 			goto out_nodestroy;
738f0380f3dSTrond Myklebust 		}
739e092bdcdSTrond Myklebust 	}
740e092bdcdSTrond Myklebust 	spin_unlock(&rpc_credcache_lock);
7411da177e4SLinus Torvalds 	cred->cr_ops->crdestroy(cred);
742f0380f3dSTrond Myklebust 	return;
743f0380f3dSTrond Myklebust out_nodestroy:
744f0380f3dSTrond Myklebust 	spin_unlock(&rpc_credcache_lock);
7451da177e4SLinus Torvalds }
746e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(put_rpccred);
7471da177e4SLinus Torvalds 
748d8ed029dSAlexey Dobriyan __be32 *
749d8ed029dSAlexey Dobriyan rpcauth_marshcred(struct rpc_task *task, __be32 *p)
7501da177e4SLinus Torvalds {
751a17c2153STrond Myklebust 	struct rpc_cred	*cred = task->tk_rqstp->rq_cred;
7521da177e4SLinus Torvalds 
75346121cf7SChuck Lever 	dprintk("RPC: %5u marshaling %s cred %p\n",
7541be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
7550bbacc40SChuck Lever 
7561da177e4SLinus Torvalds 	return cred->cr_ops->crmarshal(task, p);
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
759d8ed029dSAlexey Dobriyan __be32 *
760d8ed029dSAlexey Dobriyan rpcauth_checkverf(struct rpc_task *task, __be32 *p)
7611da177e4SLinus Torvalds {
762a17c2153STrond Myklebust 	struct rpc_cred	*cred = task->tk_rqstp->rq_cred;
7631da177e4SLinus Torvalds 
76446121cf7SChuck Lever 	dprintk("RPC: %5u validating %s cred %p\n",
7651be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
7660bbacc40SChuck Lever 
7671da177e4SLinus Torvalds 	return cred->cr_ops->crvalidate(task, p);
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
7709f06c719SChuck Lever static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
7719f06c719SChuck Lever 				   __be32 *data, void *obj)
7729f06c719SChuck Lever {
7739f06c719SChuck Lever 	struct xdr_stream xdr;
7749f06c719SChuck Lever 
7759f06c719SChuck Lever 	xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
7769f06c719SChuck Lever 	encode(rqstp, &xdr, obj);
7779f06c719SChuck Lever }
7789f06c719SChuck Lever 
7791da177e4SLinus Torvalds int
7809f06c719SChuck Lever rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
781d8ed029dSAlexey Dobriyan 		__be32 *data, void *obj)
7821da177e4SLinus Torvalds {
783a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
7841da177e4SLinus Torvalds 
78546121cf7SChuck Lever 	dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
7861da177e4SLinus Torvalds 			task->tk_pid, cred->cr_ops->cr_name, cred);
7871da177e4SLinus Torvalds 	if (cred->cr_ops->crwrap_req)
7881da177e4SLinus Torvalds 		return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
7891da177e4SLinus Torvalds 	/* By default, we encode the arguments normally. */
7909f06c719SChuck Lever 	rpcauth_wrap_req_encode(encode, rqstp, data, obj);
7919f06c719SChuck Lever 	return 0;
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
794bf269551SChuck Lever static int
795bf269551SChuck Lever rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
796bf269551SChuck Lever 			  __be32 *data, void *obj)
797bf269551SChuck Lever {
798bf269551SChuck Lever 	struct xdr_stream xdr;
799bf269551SChuck Lever 
800bf269551SChuck Lever 	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
801bf269551SChuck Lever 	return decode(rqstp, &xdr, obj);
802bf269551SChuck Lever }
803bf269551SChuck Lever 
8041da177e4SLinus Torvalds int
805bf269551SChuck Lever rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
806d8ed029dSAlexey Dobriyan 		__be32 *data, void *obj)
8071da177e4SLinus Torvalds {
808a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
8091da177e4SLinus Torvalds 
81046121cf7SChuck Lever 	dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
8111da177e4SLinus Torvalds 			task->tk_pid, cred->cr_ops->cr_name, cred);
8121da177e4SLinus Torvalds 	if (cred->cr_ops->crunwrap_resp)
8131da177e4SLinus Torvalds 		return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
8141da177e4SLinus Torvalds 						   data, obj);
8151da177e4SLinus Torvalds 	/* By default, we decode the arguments normally. */
816bf269551SChuck Lever 	return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds int
8201da177e4SLinus Torvalds rpcauth_refreshcred(struct rpc_task *task)
8211da177e4SLinus Torvalds {
8229a84d380STrond Myklebust 	struct rpc_cred	*cred;
8231da177e4SLinus Torvalds 	int err;
8241da177e4SLinus Torvalds 
825a17c2153STrond Myklebust 	cred = task->tk_rqstp->rq_cred;
826a17c2153STrond Myklebust 	if (cred == NULL) {
827a17c2153STrond Myklebust 		err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
828a17c2153STrond Myklebust 		if (err < 0)
829a17c2153STrond Myklebust 			goto out;
830a17c2153STrond Myklebust 		cred = task->tk_rqstp->rq_cred;
831f81c6224SJoe Perches 	}
83246121cf7SChuck Lever 	dprintk("RPC: %5u refreshing %s cred %p\n",
8331be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
8340bbacc40SChuck Lever 
8351da177e4SLinus Torvalds 	err = cred->cr_ops->crrefresh(task);
836a17c2153STrond Myklebust out:
8371da177e4SLinus Torvalds 	if (err < 0)
8381da177e4SLinus Torvalds 		task->tk_status = err;
8391da177e4SLinus Torvalds 	return err;
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds void
8431da177e4SLinus Torvalds rpcauth_invalcred(struct rpc_task *task)
8441da177e4SLinus Torvalds {
845a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
846fc432dd9STrond Myklebust 
84746121cf7SChuck Lever 	dprintk("RPC: %5u invalidating %s cred %p\n",
8481be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
849fc432dd9STrond Myklebust 	if (cred)
850fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
8511da177e4SLinus Torvalds }
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds int
8541da177e4SLinus Torvalds rpcauth_uptodatecred(struct rpc_task *task)
8551da177e4SLinus Torvalds {
856a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
857fc432dd9STrond Myklebust 
858fc432dd9STrond Myklebust 	return cred == NULL ||
859fc432dd9STrond Myklebust 		test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
8601da177e4SLinus Torvalds }
861f5c2187cSTrond Myklebust 
8628e1f936bSRusty Russell static struct shrinker rpc_cred_shrinker = {
86370534a73SDave Chinner 	.count_objects = rpcauth_cache_shrink_count,
86470534a73SDave Chinner 	.scan_objects = rpcauth_cache_shrink_scan,
8658e1f936bSRusty Russell 	.seeks = DEFAULT_SEEKS,
8668e1f936bSRusty Russell };
867f5c2187cSTrond Myklebust 
8685d8d9a4dSTrond Myklebust int __init rpcauth_init_module(void)
869f5c2187cSTrond Myklebust {
8705d8d9a4dSTrond Myklebust 	int err;
8715d8d9a4dSTrond Myklebust 
8725d8d9a4dSTrond Myklebust 	err = rpc_init_authunix();
8735d8d9a4dSTrond Myklebust 	if (err < 0)
8745d8d9a4dSTrond Myklebust 		goto out1;
8755d8d9a4dSTrond Myklebust 	err = rpc_init_generic_auth();
8765d8d9a4dSTrond Myklebust 	if (err < 0)
8775d8d9a4dSTrond Myklebust 		goto out2;
8788e1f936bSRusty Russell 	register_shrinker(&rpc_cred_shrinker);
8795d8d9a4dSTrond Myklebust 	return 0;
8805d8d9a4dSTrond Myklebust out2:
8815d8d9a4dSTrond Myklebust 	rpc_destroy_authunix();
8825d8d9a4dSTrond Myklebust out1:
8835d8d9a4dSTrond Myklebust 	return err;
884f5c2187cSTrond Myklebust }
885f5c2187cSTrond Myklebust 
886c135e84aSStephen Rothwell void rpcauth_remove_module(void)
887f5c2187cSTrond Myklebust {
8885d8d9a4dSTrond Myklebust 	rpc_destroy_authunix();
8895d8d9a4dSTrond Myklebust 	rpc_destroy_generic_auth();
8908e1f936bSRusty Russell 	unregister_shrinker(&rpc_cred_shrinker);
891f5c2187cSTrond Myklebust }
892