xref: /openbmc/linux/net/sunrpc/auth.c (revision a0337d1d)
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 
191da177e4SLinus Torvalds #ifdef RPC_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;
51241269bdSTrond Myklebust 	ret = strict_strtoul(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 
758e4e15d4SStephen Rothwell static 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 
831da177e4SLinus Torvalds static u32
841da177e4SLinus Torvalds pseudoflavor_to_flavor(u32 flavor) {
851c74a244SChuck Lever 	if (flavor > RPC_AUTH_MAXFLAVOR)
861da177e4SLinus Torvalds 		return RPC_AUTH_GSS;
871da177e4SLinus Torvalds 	return flavor;
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds int
91f1c0a861STrond Myklebust rpcauth_register(const struct rpc_authops *ops)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	rpc_authflavor_t flavor;
94fc1b356fSTrond Myklebust 	int ret = -EPERM;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
971da177e4SLinus Torvalds 		return -EINVAL;
98fc1b356fSTrond Myklebust 	spin_lock(&rpc_authflavor_lock);
99fc1b356fSTrond Myklebust 	if (auth_flavors[flavor] == NULL) {
1001da177e4SLinus Torvalds 		auth_flavors[flavor] = ops;
101fc1b356fSTrond Myklebust 		ret = 0;
102fc1b356fSTrond Myklebust 	}
103fc1b356fSTrond Myklebust 	spin_unlock(&rpc_authflavor_lock);
104fc1b356fSTrond Myklebust 	return ret;
1051da177e4SLinus Torvalds }
106e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_register);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds int
109f1c0a861STrond Myklebust rpcauth_unregister(const struct rpc_authops *ops)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	rpc_authflavor_t flavor;
112fc1b356fSTrond Myklebust 	int ret = -EPERM;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
1151da177e4SLinus Torvalds 		return -EINVAL;
116fc1b356fSTrond Myklebust 	spin_lock(&rpc_authflavor_lock);
117fc1b356fSTrond Myklebust 	if (auth_flavors[flavor] == ops) {
1181da177e4SLinus Torvalds 		auth_flavors[flavor] = NULL;
119fc1b356fSTrond Myklebust 		ret = 0;
120fc1b356fSTrond Myklebust 	}
121fc1b356fSTrond Myklebust 	spin_unlock(&rpc_authflavor_lock);
122fc1b356fSTrond Myklebust 	return ret;
1231da177e4SLinus Torvalds }
124e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_unregister);
1251da177e4SLinus Torvalds 
1266a1a1e34SChuck Lever /**
1279568c5e9SChuck Lever  * rpcauth_get_pseudoflavor - check if security flavor is supported
1289568c5e9SChuck Lever  * @flavor: a security flavor
1299568c5e9SChuck Lever  * @info: a GSS mech OID, quality of protection, and service value
1309568c5e9SChuck Lever  *
1319568c5e9SChuck Lever  * Verifies that an appropriate kernel module is available or already loaded.
1329568c5e9SChuck Lever  * Returns an equivalent pseudoflavor, or RPC_AUTH_MAXFLAVOR if "flavor" is
1339568c5e9SChuck Lever  * not supported locally.
1349568c5e9SChuck Lever  */
1359568c5e9SChuck Lever rpc_authflavor_t
1369568c5e9SChuck Lever rpcauth_get_pseudoflavor(rpc_authflavor_t flavor, struct rpcsec_gss_info *info)
1379568c5e9SChuck Lever {
1389568c5e9SChuck Lever 	const struct rpc_authops *ops;
1399568c5e9SChuck Lever 	rpc_authflavor_t pseudoflavor;
1409568c5e9SChuck Lever 
1419568c5e9SChuck Lever 	ops = auth_flavors[flavor];
1429568c5e9SChuck Lever 	if (ops == NULL)
1439568c5e9SChuck Lever 		request_module("rpc-auth-%u", flavor);
1449568c5e9SChuck Lever 	spin_lock(&rpc_authflavor_lock);
1459568c5e9SChuck Lever 	ops = auth_flavors[flavor];
1469568c5e9SChuck Lever 	if (ops == NULL || !try_module_get(ops->owner)) {
1479568c5e9SChuck Lever 		spin_unlock(&rpc_authflavor_lock);
1489568c5e9SChuck Lever 		return RPC_AUTH_MAXFLAVOR;
1499568c5e9SChuck Lever 	}
1509568c5e9SChuck Lever 	spin_unlock(&rpc_authflavor_lock);
1519568c5e9SChuck Lever 
1529568c5e9SChuck Lever 	pseudoflavor = flavor;
1539568c5e9SChuck Lever 	if (ops->info2flavor != NULL)
1549568c5e9SChuck Lever 		pseudoflavor = ops->info2flavor(info);
1559568c5e9SChuck Lever 
1569568c5e9SChuck Lever 	module_put(ops->owner);
1579568c5e9SChuck Lever 	return pseudoflavor;
1589568c5e9SChuck Lever }
1599568c5e9SChuck Lever EXPORT_SYMBOL_GPL(rpcauth_get_pseudoflavor);
1609568c5e9SChuck Lever 
1619568c5e9SChuck Lever /**
162a77c806fSChuck Lever  * rpcauth_get_gssinfo - find GSS tuple matching a GSS pseudoflavor
163a77c806fSChuck Lever  * @pseudoflavor: GSS pseudoflavor to match
164a77c806fSChuck Lever  * @info: rpcsec_gss_info structure to fill in
165a77c806fSChuck Lever  *
166a77c806fSChuck Lever  * Returns zero and fills in "info" if pseudoflavor matches a
167a77c806fSChuck Lever  * supported mechanism.
168a77c806fSChuck Lever  */
169a77c806fSChuck Lever int
170a77c806fSChuck Lever rpcauth_get_gssinfo(rpc_authflavor_t pseudoflavor, struct rpcsec_gss_info *info)
171a77c806fSChuck Lever {
172a77c806fSChuck Lever 	rpc_authflavor_t flavor = pseudoflavor_to_flavor(pseudoflavor);
173a77c806fSChuck Lever 	const struct rpc_authops *ops;
174a77c806fSChuck Lever 	int result;
175a77c806fSChuck Lever 
1761c74a244SChuck Lever 	if (flavor >= RPC_AUTH_MAXFLAVOR)
1771c74a244SChuck Lever 		return -EINVAL;
1781c74a244SChuck Lever 
179a77c806fSChuck Lever 	ops = auth_flavors[flavor];
180a77c806fSChuck Lever 	if (ops == NULL)
181a77c806fSChuck Lever 		request_module("rpc-auth-%u", flavor);
182a77c806fSChuck Lever 	spin_lock(&rpc_authflavor_lock);
183a77c806fSChuck Lever 	ops = auth_flavors[flavor];
184a77c806fSChuck Lever 	if (ops == NULL || !try_module_get(ops->owner)) {
185a77c806fSChuck Lever 		spin_unlock(&rpc_authflavor_lock);
186a77c806fSChuck Lever 		return -ENOENT;
187a77c806fSChuck Lever 	}
188a77c806fSChuck Lever 	spin_unlock(&rpc_authflavor_lock);
189a77c806fSChuck Lever 
190a77c806fSChuck Lever 	result = -ENOENT;
191a77c806fSChuck Lever 	if (ops->flavor2info != NULL)
192a77c806fSChuck Lever 		result = ops->flavor2info(pseudoflavor, info);
193a77c806fSChuck Lever 
194a77c806fSChuck Lever 	module_put(ops->owner);
195a77c806fSChuck Lever 	return result;
196a77c806fSChuck Lever }
197a77c806fSChuck Lever EXPORT_SYMBOL_GPL(rpcauth_get_gssinfo);
198a77c806fSChuck Lever 
199a77c806fSChuck Lever /**
2006a1a1e34SChuck Lever  * rpcauth_list_flavors - discover registered flavors and pseudoflavors
2016a1a1e34SChuck Lever  * @array: array to fill in
2026a1a1e34SChuck Lever  * @size: size of "array"
2036a1a1e34SChuck Lever  *
2046a1a1e34SChuck Lever  * Returns the number of array items filled in, or a negative errno.
2056a1a1e34SChuck Lever  *
2066a1a1e34SChuck Lever  * The returned array is not sorted by any policy.  Callers should not
2076a1a1e34SChuck Lever  * rely on the order of the items in the returned array.
2086a1a1e34SChuck Lever  */
2096a1a1e34SChuck Lever int
2106a1a1e34SChuck Lever rpcauth_list_flavors(rpc_authflavor_t *array, int size)
2116a1a1e34SChuck Lever {
2126a1a1e34SChuck Lever 	rpc_authflavor_t flavor;
2136a1a1e34SChuck Lever 	int result = 0;
2146a1a1e34SChuck Lever 
2156a1a1e34SChuck Lever 	spin_lock(&rpc_authflavor_lock);
2166a1a1e34SChuck Lever 	for (flavor = 0; flavor < RPC_AUTH_MAXFLAVOR; flavor++) {
2176a1a1e34SChuck Lever 		const struct rpc_authops *ops = auth_flavors[flavor];
2186a1a1e34SChuck Lever 		rpc_authflavor_t pseudos[4];
2196a1a1e34SChuck Lever 		int i, len;
2206a1a1e34SChuck Lever 
2216a1a1e34SChuck Lever 		if (result >= size) {
2226a1a1e34SChuck Lever 			result = -ENOMEM;
2236a1a1e34SChuck Lever 			break;
2246a1a1e34SChuck Lever 		}
2256a1a1e34SChuck Lever 
2266a1a1e34SChuck Lever 		if (ops == NULL)
2276a1a1e34SChuck Lever 			continue;
2286a1a1e34SChuck Lever 		if (ops->list_pseudoflavors == NULL) {
2296a1a1e34SChuck Lever 			array[result++] = ops->au_flavor;
2306a1a1e34SChuck Lever 			continue;
2316a1a1e34SChuck Lever 		}
2326a1a1e34SChuck Lever 		len = ops->list_pseudoflavors(pseudos, ARRAY_SIZE(pseudos));
2336a1a1e34SChuck Lever 		if (len < 0) {
2346a1a1e34SChuck Lever 			result = len;
2356a1a1e34SChuck Lever 			break;
2366a1a1e34SChuck Lever 		}
2376a1a1e34SChuck Lever 		for (i = 0; i < len; i++) {
2386a1a1e34SChuck Lever 			if (result >= size) {
2396a1a1e34SChuck Lever 				result = -ENOMEM;
2406a1a1e34SChuck Lever 				break;
2416a1a1e34SChuck Lever 			}
2426a1a1e34SChuck Lever 			array[result++] = pseudos[i];
2436a1a1e34SChuck Lever 		}
2446a1a1e34SChuck Lever 	}
2456a1a1e34SChuck Lever 	spin_unlock(&rpc_authflavor_lock);
2466a1a1e34SChuck Lever 
2476a1a1e34SChuck Lever 	dprintk("RPC:       %s returns %d\n", __func__, result);
2486a1a1e34SChuck Lever 	return result;
2496a1a1e34SChuck Lever }
2506a1a1e34SChuck Lever EXPORT_SYMBOL_GPL(rpcauth_list_flavors);
2516a1a1e34SChuck Lever 
2521da177e4SLinus Torvalds struct rpc_auth *
253c2190661STrond Myklebust rpcauth_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	struct rpc_auth		*auth;
256f1c0a861STrond Myklebust 	const struct rpc_authops *ops;
257c2190661STrond Myklebust 	u32			flavor = pseudoflavor_to_flavor(args->pseudoflavor);
2581da177e4SLinus Torvalds 
259f344f6dfSOlaf Kirch 	auth = ERR_PTR(-EINVAL);
260f344f6dfSOlaf Kirch 	if (flavor >= RPC_AUTH_MAXFLAVOR)
261f344f6dfSOlaf Kirch 		goto out;
262f344f6dfSOlaf Kirch 
263f344f6dfSOlaf Kirch 	if ((ops = auth_flavors[flavor]) == NULL)
264f344f6dfSOlaf Kirch 		request_module("rpc-auth-%u", flavor);
265fc1b356fSTrond Myklebust 	spin_lock(&rpc_authflavor_lock);
266fc1b356fSTrond Myklebust 	ops = auth_flavors[flavor];
267fc1b356fSTrond Myklebust 	if (ops == NULL || !try_module_get(ops->owner)) {
268fc1b356fSTrond Myklebust 		spin_unlock(&rpc_authflavor_lock);
269f344f6dfSOlaf Kirch 		goto out;
270fc1b356fSTrond Myklebust 	}
271fc1b356fSTrond Myklebust 	spin_unlock(&rpc_authflavor_lock);
272c2190661STrond Myklebust 	auth = ops->create(args, clnt);
273fc1b356fSTrond Myklebust 	module_put(ops->owner);
2746a19275aSJ. Bruce Fields 	if (IS_ERR(auth))
2756a19275aSJ. Bruce Fields 		return auth;
2761da177e4SLinus Torvalds 	if (clnt->cl_auth)
277de7a8ce3STrond Myklebust 		rpcauth_release(clnt->cl_auth);
2781da177e4SLinus Torvalds 	clnt->cl_auth = auth;
279f344f6dfSOlaf Kirch 
280f344f6dfSOlaf Kirch out:
2811da177e4SLinus Torvalds 	return auth;
2821da177e4SLinus Torvalds }
283e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_create);
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds void
286de7a8ce3STrond Myklebust rpcauth_release(struct rpc_auth *auth)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	if (!atomic_dec_and_test(&auth->au_count))
2891da177e4SLinus Torvalds 		return;
2901da177e4SLinus Torvalds 	auth->au_ops->destroy(auth);
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds static DEFINE_SPINLOCK(rpc_credcache_lock);
2941da177e4SLinus Torvalds 
29531be5bf1STrond Myklebust static void
29631be5bf1STrond Myklebust rpcauth_unhash_cred_locked(struct rpc_cred *cred)
29731be5bf1STrond Myklebust {
29831be5bf1STrond Myklebust 	hlist_del_rcu(&cred->cr_hash);
2994e857c58SPeter Zijlstra 	smp_mb__before_atomic();
30031be5bf1STrond Myklebust 	clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
30131be5bf1STrond Myklebust }
30231be5bf1STrond Myklebust 
303f0380f3dSTrond Myklebust static int
3049499b434STrond Myklebust rpcauth_unhash_cred(struct rpc_cred *cred)
3059499b434STrond Myklebust {
3069499b434STrond Myklebust 	spinlock_t *cache_lock;
307f0380f3dSTrond Myklebust 	int ret;
3089499b434STrond Myklebust 
3099499b434STrond Myklebust 	cache_lock = &cred->cr_auth->au_credcache->lock;
3109499b434STrond Myklebust 	spin_lock(cache_lock);
311f0380f3dSTrond Myklebust 	ret = atomic_read(&cred->cr_count) == 0;
312f0380f3dSTrond Myklebust 	if (ret)
3139499b434STrond Myklebust 		rpcauth_unhash_cred_locked(cred);
3149499b434STrond Myklebust 	spin_unlock(cache_lock);
315f0380f3dSTrond Myklebust 	return ret;
3169499b434STrond Myklebust }
3179499b434STrond Myklebust 
3181da177e4SLinus Torvalds /*
3191da177e4SLinus Torvalds  * Initialize RPC credential cache
3201da177e4SLinus Torvalds  */
3211da177e4SLinus Torvalds int
322f5c2187cSTrond Myklebust rpcauth_init_credcache(struct rpc_auth *auth)
3231da177e4SLinus Torvalds {
3241da177e4SLinus Torvalds 	struct rpc_cred_cache *new;
325988664a0STrond Myklebust 	unsigned int hashsize;
3261da177e4SLinus Torvalds 
3278b3a7005SKris Katterjohn 	new = kmalloc(sizeof(*new), GFP_KERNEL);
3281da177e4SLinus Torvalds 	if (!new)
329241269bdSTrond Myklebust 		goto out_nocache;
330241269bdSTrond Myklebust 	new->hashbits = auth_hashbits;
331988664a0STrond Myklebust 	hashsize = 1U << new->hashbits;
332241269bdSTrond Myklebust 	new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
333241269bdSTrond Myklebust 	if (!new->hashtable)
334241269bdSTrond Myklebust 		goto out_nohashtbl;
3359499b434STrond Myklebust 	spin_lock_init(&new->lock);
3361da177e4SLinus Torvalds 	auth->au_credcache = new;
3371da177e4SLinus Torvalds 	return 0;
338241269bdSTrond Myklebust out_nohashtbl:
339241269bdSTrond Myklebust 	kfree(new);
340241269bdSTrond Myklebust out_nocache:
341241269bdSTrond Myklebust 	return -ENOMEM;
3421da177e4SLinus Torvalds }
343e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds /*
3464de6caa2SAndy Adamson  * Setup a credential key lifetime timeout notification
3474de6caa2SAndy Adamson  */
3484de6caa2SAndy Adamson int
3494de6caa2SAndy Adamson rpcauth_key_timeout_notify(struct rpc_auth *auth, struct rpc_cred *cred)
3504de6caa2SAndy Adamson {
3514de6caa2SAndy Adamson 	if (!cred->cr_auth->au_ops->key_timeout)
3524de6caa2SAndy Adamson 		return 0;
3534de6caa2SAndy Adamson 	return cred->cr_auth->au_ops->key_timeout(auth, cred);
3544de6caa2SAndy Adamson }
3554de6caa2SAndy Adamson EXPORT_SYMBOL_GPL(rpcauth_key_timeout_notify);
3564de6caa2SAndy Adamson 
3574de6caa2SAndy Adamson bool
3584de6caa2SAndy Adamson rpcauth_cred_key_to_expire(struct rpc_cred *cred)
3594de6caa2SAndy Adamson {
3604de6caa2SAndy Adamson 	if (!cred->cr_ops->crkey_to_expire)
3614de6caa2SAndy Adamson 		return false;
3624de6caa2SAndy Adamson 	return cred->cr_ops->crkey_to_expire(cred);
3634de6caa2SAndy Adamson }
3644de6caa2SAndy Adamson EXPORT_SYMBOL_GPL(rpcauth_cred_key_to_expire);
3654de6caa2SAndy Adamson 
366a0337d1dSJeff Layton char *
367a0337d1dSJeff Layton rpcauth_stringify_acceptor(struct rpc_cred *cred)
368a0337d1dSJeff Layton {
369a0337d1dSJeff Layton 	if (!cred->cr_ops->crstringify_acceptor)
370a0337d1dSJeff Layton 		return NULL;
371a0337d1dSJeff Layton 	return cred->cr_ops->crstringify_acceptor(cred);
372a0337d1dSJeff Layton }
373a0337d1dSJeff Layton EXPORT_SYMBOL_GPL(rpcauth_stringify_acceptor);
374a0337d1dSJeff Layton 
3754de6caa2SAndy Adamson /*
3761da177e4SLinus Torvalds  * Destroy a list of credentials
3771da177e4SLinus Torvalds  */
3781da177e4SLinus Torvalds static inline
379e092bdcdSTrond Myklebust void rpcauth_destroy_credlist(struct list_head *head)
3801da177e4SLinus Torvalds {
3811da177e4SLinus Torvalds 	struct rpc_cred *cred;
3821da177e4SLinus Torvalds 
383e092bdcdSTrond Myklebust 	while (!list_empty(head)) {
384e092bdcdSTrond Myklebust 		cred = list_entry(head->next, struct rpc_cred, cr_lru);
385e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
3861da177e4SLinus Torvalds 		put_rpccred(cred);
3871da177e4SLinus Torvalds 	}
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds /*
3911da177e4SLinus Torvalds  * Clear the RPC credential cache, and delete those credentials
3921da177e4SLinus Torvalds  * that are not referenced.
3931da177e4SLinus Torvalds  */
3941da177e4SLinus Torvalds void
3953ab9bb72STrond Myklebust rpcauth_clear_credcache(struct rpc_cred_cache *cache)
3961da177e4SLinus Torvalds {
397e092bdcdSTrond Myklebust 	LIST_HEAD(free);
398e092bdcdSTrond Myklebust 	struct hlist_head *head;
3991da177e4SLinus Torvalds 	struct rpc_cred	*cred;
400988664a0STrond Myklebust 	unsigned int hashsize = 1U << cache->hashbits;
4011da177e4SLinus Torvalds 	int		i;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	spin_lock(&rpc_credcache_lock);
4049499b434STrond Myklebust 	spin_lock(&cache->lock);
405988664a0STrond Myklebust 	for (i = 0; i < hashsize; i++) {
406e092bdcdSTrond Myklebust 		head = &cache->hashtable[i];
407e092bdcdSTrond Myklebust 		while (!hlist_empty(head)) {
408e092bdcdSTrond Myklebust 			cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
409e092bdcdSTrond Myklebust 			get_rpccred(cred);
410f5c2187cSTrond Myklebust 			if (!list_empty(&cred->cr_lru)) {
411f5c2187cSTrond Myklebust 				list_del(&cred->cr_lru);
412f5c2187cSTrond Myklebust 				number_cred_unused--;
413f5c2187cSTrond Myklebust 			}
414f5c2187cSTrond Myklebust 			list_add_tail(&cred->cr_lru, &free);
41531be5bf1STrond Myklebust 			rpcauth_unhash_cred_locked(cred);
4161da177e4SLinus Torvalds 		}
4171da177e4SLinus Torvalds 	}
4189499b434STrond Myklebust 	spin_unlock(&cache->lock);
4191da177e4SLinus Torvalds 	spin_unlock(&rpc_credcache_lock);
4201da177e4SLinus Torvalds 	rpcauth_destroy_credlist(&free);
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4233ab9bb72STrond Myklebust /*
4243ab9bb72STrond Myklebust  * Destroy the RPC credential cache
4253ab9bb72STrond Myklebust  */
4263ab9bb72STrond Myklebust void
4273ab9bb72STrond Myklebust rpcauth_destroy_credcache(struct rpc_auth *auth)
4283ab9bb72STrond Myklebust {
4293ab9bb72STrond Myklebust 	struct rpc_cred_cache *cache = auth->au_credcache;
4303ab9bb72STrond Myklebust 
4313ab9bb72STrond Myklebust 	if (cache) {
4323ab9bb72STrond Myklebust 		auth->au_credcache = NULL;
4333ab9bb72STrond Myklebust 		rpcauth_clear_credcache(cache);
434241269bdSTrond Myklebust 		kfree(cache->hashtable);
4353ab9bb72STrond Myklebust 		kfree(cache);
4363ab9bb72STrond Myklebust 	}
4373ab9bb72STrond Myklebust }
438e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
4393ab9bb72STrond Myklebust 
440d2b83141STrond Myklebust 
441d2b83141STrond Myklebust #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
442d2b83141STrond Myklebust 
4431da177e4SLinus Torvalds /*
4441da177e4SLinus Torvalds  * Remove stale credentials. Avoid sleeping inside the loop.
4451da177e4SLinus Torvalds  */
44670534a73SDave Chinner static long
447f5c2187cSTrond Myklebust rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
4481da177e4SLinus Torvalds {
4499499b434STrond Myklebust 	spinlock_t *cache_lock;
450eac0d18dSTrond Myklebust 	struct rpc_cred *cred, *next;
451d2b83141STrond Myklebust 	unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
45270534a73SDave Chinner 	long freed = 0;
4531da177e4SLinus Torvalds 
454eac0d18dSTrond Myklebust 	list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
455eac0d18dSTrond Myklebust 
45620673406STrond Myklebust 		if (nr_to_scan-- == 0)
45720673406STrond Myklebust 			break;
45893a05e65STrond Myklebust 		/*
45993a05e65STrond Myklebust 		 * Enforce a 60 second garbage collection moratorium
46093a05e65STrond Myklebust 		 * Note that the cred_unused list must be time-ordered.
46193a05e65STrond Myklebust 		 */
4623d7b0894STrond Myklebust 		if (time_in_range(cred->cr_expire, expired, jiffies) &&
463eac0d18dSTrond Myklebust 		    test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
46470534a73SDave Chinner 			break;
465eac0d18dSTrond Myklebust 
466e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
467f5c2187cSTrond Myklebust 		number_cred_unused--;
46870534a73SDave Chinner 		freed++;
469e092bdcdSTrond Myklebust 		if (atomic_read(&cred->cr_count) != 0)
470e092bdcdSTrond Myklebust 			continue;
471eac0d18dSTrond Myklebust 
4729499b434STrond Myklebust 		cache_lock = &cred->cr_auth->au_credcache->lock;
4739499b434STrond Myklebust 		spin_lock(cache_lock);
4749499b434STrond Myklebust 		if (atomic_read(&cred->cr_count) == 0) {
475e092bdcdSTrond Myklebust 			get_rpccred(cred);
476e092bdcdSTrond Myklebust 			list_add_tail(&cred->cr_lru, free);
47731be5bf1STrond Myklebust 			rpcauth_unhash_cred_locked(cred);
4781da177e4SLinus Torvalds 		}
4799499b434STrond Myklebust 		spin_unlock(cache_lock);
4809499b434STrond Myklebust 	}
48170534a73SDave Chinner 	return freed;
4821da177e4SLinus Torvalds }
483e092bdcdSTrond Myklebust 
484e092bdcdSTrond Myklebust /*
485f5c2187cSTrond Myklebust  * Run memory cache shrinker.
486e092bdcdSTrond Myklebust  */
48770534a73SDave Chinner static unsigned long
48870534a73SDave Chinner rpcauth_cache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
48970534a73SDave Chinner 
490e092bdcdSTrond Myklebust {
491f5c2187cSTrond Myklebust 	LIST_HEAD(free);
49270534a73SDave Chinner 	unsigned long freed;
493f5c2187cSTrond Myklebust 
49470534a73SDave Chinner 	if ((sc->gfp_mask & GFP_KERNEL) != GFP_KERNEL)
49570534a73SDave Chinner 		return SHRINK_STOP;
49670534a73SDave Chinner 
49770534a73SDave Chinner 	/* nothing left, don't come back */
498f5c2187cSTrond Myklebust 	if (list_empty(&cred_unused))
49970534a73SDave Chinner 		return SHRINK_STOP;
50070534a73SDave Chinner 
50131be5bf1STrond Myklebust 	spin_lock(&rpc_credcache_lock);
50270534a73SDave Chinner 	freed = rpcauth_prune_expired(&free, sc->nr_to_scan);
50331be5bf1STrond Myklebust 	spin_unlock(&rpc_credcache_lock);
504f5c2187cSTrond Myklebust 	rpcauth_destroy_credlist(&free);
50570534a73SDave Chinner 
50670534a73SDave Chinner 	return freed;
50770534a73SDave Chinner }
50870534a73SDave Chinner 
50970534a73SDave Chinner static unsigned long
51070534a73SDave Chinner rpcauth_cache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
51170534a73SDave Chinner 
51270534a73SDave Chinner {
51370534a73SDave Chinner 	return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds /*
5171da177e4SLinus Torvalds  * Look up a process' credentials in the authentication cache
5181da177e4SLinus Torvalds  */
5191da177e4SLinus Torvalds struct rpc_cred *
5201da177e4SLinus Torvalds rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
5218a317760STrond Myklebust 		int flags)
5221da177e4SLinus Torvalds {
523e092bdcdSTrond Myklebust 	LIST_HEAD(free);
5241da177e4SLinus Torvalds 	struct rpc_cred_cache *cache = auth->au_credcache;
52531be5bf1STrond Myklebust 	struct rpc_cred	*cred = NULL,
52631be5bf1STrond Myklebust 			*entry, *new;
52725337fdcSTrond Myklebust 	unsigned int nr;
52825337fdcSTrond Myklebust 
5299e469e30SEric W. Biederman 	nr = hash_long(from_kuid(&init_user_ns, acred->uid), cache->hashbits);
5301da177e4SLinus Torvalds 
53131be5bf1STrond Myklebust 	rcu_read_lock();
532b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
53331be5bf1STrond Myklebust 		if (!entry->cr_ops->crmatch(acred, entry, flags))
53431be5bf1STrond Myklebust 			continue;
5359499b434STrond Myklebust 		spin_lock(&cache->lock);
53631be5bf1STrond Myklebust 		if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
5379499b434STrond Myklebust 			spin_unlock(&cache->lock);
53831be5bf1STrond Myklebust 			continue;
53931be5bf1STrond Myklebust 		}
54031be5bf1STrond Myklebust 		cred = get_rpccred(entry);
5419499b434STrond Myklebust 		spin_unlock(&cache->lock);
54231be5bf1STrond Myklebust 		break;
54331be5bf1STrond Myklebust 	}
54431be5bf1STrond Myklebust 	rcu_read_unlock();
54531be5bf1STrond Myklebust 
5469499b434STrond Myklebust 	if (cred != NULL)
54731be5bf1STrond Myklebust 		goto found;
54831be5bf1STrond Myklebust 
54931be5bf1STrond Myklebust 	new = auth->au_ops->crcreate(auth, acred, flags);
55031be5bf1STrond Myklebust 	if (IS_ERR(new)) {
55131be5bf1STrond Myklebust 		cred = new;
55231be5bf1STrond Myklebust 		goto out;
55331be5bf1STrond Myklebust 	}
55431be5bf1STrond Myklebust 
5559499b434STrond Myklebust 	spin_lock(&cache->lock);
556b67bfe0dSSasha Levin 	hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
557e092bdcdSTrond Myklebust 		if (!entry->cr_ops->crmatch(acred, entry, flags))
558e092bdcdSTrond Myklebust 			continue;
559e092bdcdSTrond Myklebust 		cred = get_rpccred(entry);
5601da177e4SLinus Torvalds 		break;
5611da177e4SLinus Torvalds 	}
56231be5bf1STrond Myklebust 	if (cred == NULL) {
56331be5bf1STrond Myklebust 		cred = new;
56431be5bf1STrond Myklebust 		set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
56531be5bf1STrond Myklebust 		hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
56631be5bf1STrond Myklebust 	} else
567e092bdcdSTrond Myklebust 		list_add_tail(&new->cr_lru, &free);
5689499b434STrond Myklebust 	spin_unlock(&cache->lock);
56931be5bf1STrond Myklebust found:
570f64f9e71SJoe Perches 	if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
571f64f9e71SJoe Perches 	    cred->cr_ops->cr_init != NULL &&
572f64f9e71SJoe Perches 	    !(flags & RPCAUTH_LOOKUP_NEW)) {
573fba3bad4STrond Myklebust 		int res = cred->cr_ops->cr_init(auth, cred);
574fba3bad4STrond Myklebust 		if (res < 0) {
575fba3bad4STrond Myklebust 			put_rpccred(cred);
576fba3bad4STrond Myklebust 			cred = ERR_PTR(res);
577fba3bad4STrond Myklebust 		}
5781da177e4SLinus Torvalds 	}
57931be5bf1STrond Myklebust 	rpcauth_destroy_credlist(&free);
58031be5bf1STrond Myklebust out:
58131be5bf1STrond Myklebust 	return cred;
5821da177e4SLinus Torvalds }
583e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds struct rpc_cred *
5868a317760STrond Myklebust rpcauth_lookupcred(struct rpc_auth *auth, int flags)
5871da177e4SLinus Torvalds {
58886a264abSDavid Howells 	struct auth_cred acred;
5891da177e4SLinus Torvalds 	struct rpc_cred *ret;
59086a264abSDavid Howells 	const struct cred *cred = current_cred();
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	dprintk("RPC:       looking up %s cred\n",
5931da177e4SLinus Torvalds 		auth->au_ops->au_name);
59486a264abSDavid Howells 
59586a264abSDavid Howells 	memset(&acred, 0, sizeof(acred));
59686a264abSDavid Howells 	acred.uid = cred->fsuid;
59786a264abSDavid Howells 	acred.gid = cred->fsgid;
59886a264abSDavid Howells 	acred.group_info = get_group_info(((struct cred *)cred)->group_info);
59986a264abSDavid Howells 
6008a317760STrond Myklebust 	ret = auth->au_ops->lookup_cred(auth, &acred, flags);
6011da177e4SLinus Torvalds 	put_group_info(acred.group_info);
6021da177e4SLinus Torvalds 	return ret;
6031da177e4SLinus Torvalds }
60466b06860SAndy Adamson EXPORT_SYMBOL_GPL(rpcauth_lookupcred);
6051da177e4SLinus Torvalds 
6065fe4755eSTrond Myklebust void
6075fe4755eSTrond Myklebust rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
6085fe4755eSTrond Myklebust 		  struct rpc_auth *auth, const struct rpc_credops *ops)
6095fe4755eSTrond Myklebust {
6105fe4755eSTrond Myklebust 	INIT_HLIST_NODE(&cred->cr_hash);
611e092bdcdSTrond Myklebust 	INIT_LIST_HEAD(&cred->cr_lru);
6125fe4755eSTrond Myklebust 	atomic_set(&cred->cr_count, 1);
6135fe4755eSTrond Myklebust 	cred->cr_auth = auth;
6145fe4755eSTrond Myklebust 	cred->cr_ops = ops;
6155fe4755eSTrond Myklebust 	cred->cr_expire = jiffies;
6165fe4755eSTrond Myklebust #ifdef RPC_DEBUG
6175fe4755eSTrond Myklebust 	cred->cr_magic = RPCAUTH_CRED_MAGIC;
6185fe4755eSTrond Myklebust #endif
6195fe4755eSTrond Myklebust 	cred->cr_uid = acred->uid;
6205fe4755eSTrond Myklebust }
621e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_init_cred);
6225fe4755eSTrond Myklebust 
6238572b8e2STrond Myklebust struct rpc_cred *
6245d351754STrond Myklebust rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
6254ccda2cdSTrond Myklebust {
6264ccda2cdSTrond Myklebust 	dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
6274ccda2cdSTrond Myklebust 			cred->cr_auth->au_ops->au_name, cred);
6288572b8e2STrond Myklebust 	return get_rpccred(cred);
6294ccda2cdSTrond Myklebust }
6305c691044STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
6314ccda2cdSTrond Myklebust 
6328572b8e2STrond Myklebust static struct rpc_cred *
6335d351754STrond Myklebust rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
6341da177e4SLinus Torvalds {
6351be27f36STrond Myklebust 	struct rpc_auth *auth = task->tk_client->cl_auth;
6361da177e4SLinus Torvalds 	struct auth_cred acred = {
637bf37f794SEric W. Biederman 		.uid = GLOBAL_ROOT_UID,
638bf37f794SEric W. Biederman 		.gid = GLOBAL_ROOT_GID,
6391da177e4SLinus Torvalds 	};
6401da177e4SLinus Torvalds 
64146121cf7SChuck Lever 	dprintk("RPC: %5u looking up %s cred\n",
6421be27f36STrond Myklebust 		task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
6438572b8e2STrond Myklebust 	return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
644af093835STrond Myklebust }
645af093835STrond Myklebust 
6468572b8e2STrond Myklebust static struct rpc_cred *
6475d351754STrond Myklebust rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
648af093835STrond Myklebust {
649af093835STrond Myklebust 	struct rpc_auth *auth = task->tk_client->cl_auth;
650af093835STrond Myklebust 
651af093835STrond Myklebust 	dprintk("RPC: %5u looking up %s cred\n",
652af093835STrond Myklebust 		task->tk_pid, auth->au_ops->au_name);
6538572b8e2STrond Myklebust 	return rpcauth_lookupcred(auth, lookupflags);
6541da177e4SLinus Torvalds }
6551da177e4SLinus Torvalds 
656a17c2153STrond Myklebust static int
6574ccda2cdSTrond Myklebust rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
6581da177e4SLinus Torvalds {
659a17c2153STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
6608572b8e2STrond Myklebust 	struct rpc_cred *new;
6615d351754STrond Myklebust 	int lookupflags = 0;
6625d351754STrond Myklebust 
6635d351754STrond Myklebust 	if (flags & RPC_TASK_ASYNC)
6645d351754STrond Myklebust 		lookupflags |= RPCAUTH_LOOKUP_NEW;
6654ccda2cdSTrond Myklebust 	if (cred != NULL)
6668572b8e2STrond Myklebust 		new = cred->cr_ops->crbind(task, cred, lookupflags);
6674ccda2cdSTrond Myklebust 	else if (flags & RPC_TASK_ROOTCREDS)
6688572b8e2STrond Myklebust 		new = rpcauth_bind_root_cred(task, lookupflags);
6694ccda2cdSTrond Myklebust 	else
6708572b8e2STrond Myklebust 		new = rpcauth_bind_new_cred(task, lookupflags);
6718572b8e2STrond Myklebust 	if (IS_ERR(new))
6728572b8e2STrond Myklebust 		return PTR_ERR(new);
673a17c2153STrond Myklebust 	if (req->rq_cred != NULL)
674a17c2153STrond Myklebust 		put_rpccred(req->rq_cred);
675a17c2153STrond Myklebust 	req->rq_cred = new;
6768572b8e2STrond Myklebust 	return 0;
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds void
6801da177e4SLinus Torvalds put_rpccred(struct rpc_cred *cred)
6811da177e4SLinus Torvalds {
682e092bdcdSTrond Myklebust 	/* Fast path for unhashed credentials */
683f0380f3dSTrond Myklebust 	if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
684f0380f3dSTrond Myklebust 		if (atomic_dec_and_test(&cred->cr_count))
685f0380f3dSTrond Myklebust 			cred->cr_ops->crdestroy(cred);
6861da177e4SLinus Torvalds 		return;
687f0380f3dSTrond Myklebust 	}
688f0380f3dSTrond Myklebust 
689e092bdcdSTrond Myklebust 	if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
690e092bdcdSTrond Myklebust 		return;
691f5c2187cSTrond Myklebust 	if (!list_empty(&cred->cr_lru)) {
692f5c2187cSTrond Myklebust 		number_cred_unused--;
693e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
694f5c2187cSTrond Myklebust 	}
6955f707eb4STrond Myklebust 	if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
696f0380f3dSTrond Myklebust 		if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
697e092bdcdSTrond Myklebust 			cred->cr_expire = jiffies;
698e092bdcdSTrond Myklebust 			list_add_tail(&cred->cr_lru, &cred_unused);
699f5c2187cSTrond Myklebust 			number_cred_unused++;
700f0380f3dSTrond Myklebust 			goto out_nodestroy;
701f0380f3dSTrond Myklebust 		}
702f0380f3dSTrond Myklebust 		if (!rpcauth_unhash_cred(cred)) {
703f0380f3dSTrond Myklebust 			/* We were hashed and someone looked us up... */
704f0380f3dSTrond Myklebust 			goto out_nodestroy;
705f0380f3dSTrond Myklebust 		}
706e092bdcdSTrond Myklebust 	}
707e092bdcdSTrond Myklebust 	spin_unlock(&rpc_credcache_lock);
7081da177e4SLinus Torvalds 	cred->cr_ops->crdestroy(cred);
709f0380f3dSTrond Myklebust 	return;
710f0380f3dSTrond Myklebust out_nodestroy:
711f0380f3dSTrond Myklebust 	spin_unlock(&rpc_credcache_lock);
7121da177e4SLinus Torvalds }
713e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(put_rpccred);
7141da177e4SLinus Torvalds 
715d8ed029dSAlexey Dobriyan __be32 *
716d8ed029dSAlexey Dobriyan rpcauth_marshcred(struct rpc_task *task, __be32 *p)
7171da177e4SLinus Torvalds {
718a17c2153STrond Myklebust 	struct rpc_cred	*cred = task->tk_rqstp->rq_cred;
7191da177e4SLinus Torvalds 
72046121cf7SChuck Lever 	dprintk("RPC: %5u marshaling %s cred %p\n",
7211be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
7220bbacc40SChuck Lever 
7231da177e4SLinus Torvalds 	return cred->cr_ops->crmarshal(task, p);
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
726d8ed029dSAlexey Dobriyan __be32 *
727d8ed029dSAlexey Dobriyan rpcauth_checkverf(struct rpc_task *task, __be32 *p)
7281da177e4SLinus Torvalds {
729a17c2153STrond Myklebust 	struct rpc_cred	*cred = task->tk_rqstp->rq_cred;
7301da177e4SLinus Torvalds 
73146121cf7SChuck Lever 	dprintk("RPC: %5u validating %s cred %p\n",
7321be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
7330bbacc40SChuck Lever 
7341da177e4SLinus Torvalds 	return cred->cr_ops->crvalidate(task, p);
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
7379f06c719SChuck Lever static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
7389f06c719SChuck Lever 				   __be32 *data, void *obj)
7399f06c719SChuck Lever {
7409f06c719SChuck Lever 	struct xdr_stream xdr;
7419f06c719SChuck Lever 
7429f06c719SChuck Lever 	xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
7439f06c719SChuck Lever 	encode(rqstp, &xdr, obj);
7449f06c719SChuck Lever }
7459f06c719SChuck Lever 
7461da177e4SLinus Torvalds int
7479f06c719SChuck Lever rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
748d8ed029dSAlexey Dobriyan 		__be32 *data, void *obj)
7491da177e4SLinus Torvalds {
750a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
7511da177e4SLinus Torvalds 
75246121cf7SChuck Lever 	dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
7531da177e4SLinus Torvalds 			task->tk_pid, cred->cr_ops->cr_name, cred);
7541da177e4SLinus Torvalds 	if (cred->cr_ops->crwrap_req)
7551da177e4SLinus Torvalds 		return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
7561da177e4SLinus Torvalds 	/* By default, we encode the arguments normally. */
7579f06c719SChuck Lever 	rpcauth_wrap_req_encode(encode, rqstp, data, obj);
7589f06c719SChuck Lever 	return 0;
7591da177e4SLinus Torvalds }
7601da177e4SLinus Torvalds 
761bf269551SChuck Lever static int
762bf269551SChuck Lever rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
763bf269551SChuck Lever 			  __be32 *data, void *obj)
764bf269551SChuck Lever {
765bf269551SChuck Lever 	struct xdr_stream xdr;
766bf269551SChuck Lever 
767bf269551SChuck Lever 	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
768bf269551SChuck Lever 	return decode(rqstp, &xdr, obj);
769bf269551SChuck Lever }
770bf269551SChuck Lever 
7711da177e4SLinus Torvalds int
772bf269551SChuck Lever rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
773d8ed029dSAlexey Dobriyan 		__be32 *data, void *obj)
7741da177e4SLinus Torvalds {
775a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
7761da177e4SLinus Torvalds 
77746121cf7SChuck Lever 	dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
7781da177e4SLinus Torvalds 			task->tk_pid, cred->cr_ops->cr_name, cred);
7791da177e4SLinus Torvalds 	if (cred->cr_ops->crunwrap_resp)
7801da177e4SLinus Torvalds 		return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
7811da177e4SLinus Torvalds 						   data, obj);
7821da177e4SLinus Torvalds 	/* By default, we decode the arguments normally. */
783bf269551SChuck Lever 	return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds int
7871da177e4SLinus Torvalds rpcauth_refreshcred(struct rpc_task *task)
7881da177e4SLinus Torvalds {
7899a84d380STrond Myklebust 	struct rpc_cred	*cred;
7901da177e4SLinus Torvalds 	int err;
7911da177e4SLinus Torvalds 
792a17c2153STrond Myklebust 	cred = task->tk_rqstp->rq_cred;
793a17c2153STrond Myklebust 	if (cred == NULL) {
794a17c2153STrond Myklebust 		err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
795a17c2153STrond Myklebust 		if (err < 0)
796a17c2153STrond Myklebust 			goto out;
797a17c2153STrond Myklebust 		cred = task->tk_rqstp->rq_cred;
798f81c6224SJoe Perches 	}
79946121cf7SChuck Lever 	dprintk("RPC: %5u refreshing %s cred %p\n",
8001be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
8010bbacc40SChuck Lever 
8021da177e4SLinus Torvalds 	err = cred->cr_ops->crrefresh(task);
803a17c2153STrond Myklebust out:
8041da177e4SLinus Torvalds 	if (err < 0)
8051da177e4SLinus Torvalds 		task->tk_status = err;
8061da177e4SLinus Torvalds 	return err;
8071da177e4SLinus Torvalds }
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds void
8101da177e4SLinus Torvalds rpcauth_invalcred(struct rpc_task *task)
8111da177e4SLinus Torvalds {
812a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
813fc432dd9STrond Myklebust 
81446121cf7SChuck Lever 	dprintk("RPC: %5u invalidating %s cred %p\n",
8151be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
816fc432dd9STrond Myklebust 	if (cred)
817fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds int
8211da177e4SLinus Torvalds rpcauth_uptodatecred(struct rpc_task *task)
8221da177e4SLinus Torvalds {
823a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
824fc432dd9STrond Myklebust 
825fc432dd9STrond Myklebust 	return cred == NULL ||
826fc432dd9STrond Myklebust 		test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
8271da177e4SLinus Torvalds }
828f5c2187cSTrond Myklebust 
8298e1f936bSRusty Russell static struct shrinker rpc_cred_shrinker = {
83070534a73SDave Chinner 	.count_objects = rpcauth_cache_shrink_count,
83170534a73SDave Chinner 	.scan_objects = rpcauth_cache_shrink_scan,
8328e1f936bSRusty Russell 	.seeks = DEFAULT_SEEKS,
8338e1f936bSRusty Russell };
834f5c2187cSTrond Myklebust 
8355d8d9a4dSTrond Myklebust int __init rpcauth_init_module(void)
836f5c2187cSTrond Myklebust {
8375d8d9a4dSTrond Myklebust 	int err;
8385d8d9a4dSTrond Myklebust 
8395d8d9a4dSTrond Myklebust 	err = rpc_init_authunix();
8405d8d9a4dSTrond Myklebust 	if (err < 0)
8415d8d9a4dSTrond Myklebust 		goto out1;
8425d8d9a4dSTrond Myklebust 	err = rpc_init_generic_auth();
8435d8d9a4dSTrond Myklebust 	if (err < 0)
8445d8d9a4dSTrond Myklebust 		goto out2;
8458e1f936bSRusty Russell 	register_shrinker(&rpc_cred_shrinker);
8465d8d9a4dSTrond Myklebust 	return 0;
8475d8d9a4dSTrond Myklebust out2:
8485d8d9a4dSTrond Myklebust 	rpc_destroy_authunix();
8495d8d9a4dSTrond Myklebust out1:
8505d8d9a4dSTrond Myklebust 	return err;
851f5c2187cSTrond Myklebust }
852f5c2187cSTrond Myklebust 
853c135e84aSStephen Rothwell void rpcauth_remove_module(void)
854f5c2187cSTrond Myklebust {
8555d8d9a4dSTrond Myklebust 	rpc_destroy_authunix();
8565d8d9a4dSTrond Myklebust 	rpc_destroy_generic_auth();
8578e1f936bSRusty Russell 	unregister_shrinker(&rpc_cred_shrinker);
858f5c2187cSTrond Myklebust }
859