xref: /linux/net/sunrpc/auth.c (revision 1c74a244fcb61e6e1983d5725b8ccd5d3f51889c)
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) {
85*1c74a244SChuck 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 
176*1c74a244SChuck Lever 	if (flavor >= RPC_AUTH_MAXFLAVOR)
177*1c74a244SChuck Lever 		return -EINVAL;
178*1c74a244SChuck 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 *
2531da177e4SLinus Torvalds rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	struct rpc_auth		*auth;
256f1c0a861STrond Myklebust 	const struct rpc_authops *ops;
2571da177e4SLinus Torvalds 	u32			flavor = pseudoflavor_to_flavor(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);
2721da177e4SLinus Torvalds 	auth = ops->create(clnt, pseudoflavor);
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);
29931be5bf1STrond Myklebust 	smp_mb__before_clear_bit();
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 /*
3461da177e4SLinus Torvalds  * Destroy a list of credentials
3471da177e4SLinus Torvalds  */
3481da177e4SLinus Torvalds static inline
349e092bdcdSTrond Myklebust void rpcauth_destroy_credlist(struct list_head *head)
3501da177e4SLinus Torvalds {
3511da177e4SLinus Torvalds 	struct rpc_cred *cred;
3521da177e4SLinus Torvalds 
353e092bdcdSTrond Myklebust 	while (!list_empty(head)) {
354e092bdcdSTrond Myklebust 		cred = list_entry(head->next, struct rpc_cred, cr_lru);
355e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
3561da177e4SLinus Torvalds 		put_rpccred(cred);
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds /*
3611da177e4SLinus Torvalds  * Clear the RPC credential cache, and delete those credentials
3621da177e4SLinus Torvalds  * that are not referenced.
3631da177e4SLinus Torvalds  */
3641da177e4SLinus Torvalds void
3653ab9bb72STrond Myklebust rpcauth_clear_credcache(struct rpc_cred_cache *cache)
3661da177e4SLinus Torvalds {
367e092bdcdSTrond Myklebust 	LIST_HEAD(free);
368e092bdcdSTrond Myklebust 	struct hlist_head *head;
3691da177e4SLinus Torvalds 	struct rpc_cred	*cred;
370988664a0STrond Myklebust 	unsigned int hashsize = 1U << cache->hashbits;
3711da177e4SLinus Torvalds 	int		i;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	spin_lock(&rpc_credcache_lock);
3749499b434STrond Myklebust 	spin_lock(&cache->lock);
375988664a0STrond Myklebust 	for (i = 0; i < hashsize; i++) {
376e092bdcdSTrond Myklebust 		head = &cache->hashtable[i];
377e092bdcdSTrond Myklebust 		while (!hlist_empty(head)) {
378e092bdcdSTrond Myklebust 			cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
379e092bdcdSTrond Myklebust 			get_rpccred(cred);
380f5c2187cSTrond Myklebust 			if (!list_empty(&cred->cr_lru)) {
381f5c2187cSTrond Myklebust 				list_del(&cred->cr_lru);
382f5c2187cSTrond Myklebust 				number_cred_unused--;
383f5c2187cSTrond Myklebust 			}
384f5c2187cSTrond Myklebust 			list_add_tail(&cred->cr_lru, &free);
38531be5bf1STrond Myklebust 			rpcauth_unhash_cred_locked(cred);
3861da177e4SLinus Torvalds 		}
3871da177e4SLinus Torvalds 	}
3889499b434STrond Myklebust 	spin_unlock(&cache->lock);
3891da177e4SLinus Torvalds 	spin_unlock(&rpc_credcache_lock);
3901da177e4SLinus Torvalds 	rpcauth_destroy_credlist(&free);
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
3933ab9bb72STrond Myklebust /*
3943ab9bb72STrond Myklebust  * Destroy the RPC credential cache
3953ab9bb72STrond Myklebust  */
3963ab9bb72STrond Myklebust void
3973ab9bb72STrond Myklebust rpcauth_destroy_credcache(struct rpc_auth *auth)
3983ab9bb72STrond Myklebust {
3993ab9bb72STrond Myklebust 	struct rpc_cred_cache *cache = auth->au_credcache;
4003ab9bb72STrond Myklebust 
4013ab9bb72STrond Myklebust 	if (cache) {
4023ab9bb72STrond Myklebust 		auth->au_credcache = NULL;
4033ab9bb72STrond Myklebust 		rpcauth_clear_credcache(cache);
404241269bdSTrond Myklebust 		kfree(cache->hashtable);
4053ab9bb72STrond Myklebust 		kfree(cache);
4063ab9bb72STrond Myklebust 	}
4073ab9bb72STrond Myklebust }
408e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
4093ab9bb72STrond Myklebust 
410d2b83141STrond Myklebust 
411d2b83141STrond Myklebust #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
412d2b83141STrond Myklebust 
4131da177e4SLinus Torvalds /*
4141da177e4SLinus Torvalds  * Remove stale credentials. Avoid sleeping inside the loop.
4151da177e4SLinus Torvalds  */
416f5c2187cSTrond Myklebust static int
417f5c2187cSTrond Myklebust rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
4181da177e4SLinus Torvalds {
4199499b434STrond Myklebust 	spinlock_t *cache_lock;
420eac0d18dSTrond Myklebust 	struct rpc_cred *cred, *next;
421d2b83141STrond Myklebust 	unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
4221da177e4SLinus Torvalds 
423eac0d18dSTrond Myklebust 	list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
424eac0d18dSTrond Myklebust 
42520673406STrond Myklebust 		if (nr_to_scan-- == 0)
42620673406STrond Myklebust 			break;
42793a05e65STrond Myklebust 		/*
42893a05e65STrond Myklebust 		 * Enforce a 60 second garbage collection moratorium
42993a05e65STrond Myklebust 		 * Note that the cred_unused list must be time-ordered.
43093a05e65STrond Myklebust 		 */
4313d7b0894STrond Myklebust 		if (time_in_range(cred->cr_expire, expired, jiffies) &&
432eac0d18dSTrond Myklebust 		    test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
43393a05e65STrond Myklebust 			return 0;
434eac0d18dSTrond Myklebust 
435e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
436f5c2187cSTrond Myklebust 		number_cred_unused--;
437e092bdcdSTrond Myklebust 		if (atomic_read(&cred->cr_count) != 0)
438e092bdcdSTrond Myklebust 			continue;
439eac0d18dSTrond Myklebust 
4409499b434STrond Myklebust 		cache_lock = &cred->cr_auth->au_credcache->lock;
4419499b434STrond Myklebust 		spin_lock(cache_lock);
4429499b434STrond Myklebust 		if (atomic_read(&cred->cr_count) == 0) {
443e092bdcdSTrond Myklebust 			get_rpccred(cred);
444e092bdcdSTrond Myklebust 			list_add_tail(&cred->cr_lru, free);
44531be5bf1STrond Myklebust 			rpcauth_unhash_cred_locked(cred);
4461da177e4SLinus Torvalds 		}
4479499b434STrond Myklebust 		spin_unlock(cache_lock);
4489499b434STrond Myklebust 	}
44993a05e65STrond Myklebust 	return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
4501da177e4SLinus Torvalds }
451e092bdcdSTrond Myklebust 
452e092bdcdSTrond Myklebust /*
453f5c2187cSTrond Myklebust  * Run memory cache shrinker.
454e092bdcdSTrond Myklebust  */
455f5c2187cSTrond Myklebust static int
4561495f230SYing Han rpcauth_cache_shrinker(struct shrinker *shrink, struct shrink_control *sc)
457e092bdcdSTrond Myklebust {
458f5c2187cSTrond Myklebust 	LIST_HEAD(free);
459f5c2187cSTrond Myklebust 	int res;
4601495f230SYing Han 	int nr_to_scan = sc->nr_to_scan;
4611495f230SYing Han 	gfp_t gfp_mask = sc->gfp_mask;
462f5c2187cSTrond Myklebust 
463d300a41eSTrond Myklebust 	if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
464d300a41eSTrond Myklebust 		return (nr_to_scan == 0) ? 0 : -1;
465f5c2187cSTrond Myklebust 	if (list_empty(&cred_unused))
466f5c2187cSTrond Myklebust 		return 0;
46731be5bf1STrond Myklebust 	spin_lock(&rpc_credcache_lock);
46893a05e65STrond Myklebust 	res = rpcauth_prune_expired(&free, nr_to_scan);
46931be5bf1STrond Myklebust 	spin_unlock(&rpc_credcache_lock);
470f5c2187cSTrond Myklebust 	rpcauth_destroy_credlist(&free);
471f5c2187cSTrond Myklebust 	return res;
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds /*
4751da177e4SLinus Torvalds  * Look up a process' credentials in the authentication cache
4761da177e4SLinus Torvalds  */
4771da177e4SLinus Torvalds struct rpc_cred *
4781da177e4SLinus Torvalds rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
4798a317760STrond Myklebust 		int flags)
4801da177e4SLinus Torvalds {
481e092bdcdSTrond Myklebust 	LIST_HEAD(free);
4821da177e4SLinus Torvalds 	struct rpc_cred_cache *cache = auth->au_credcache;
48331be5bf1STrond Myklebust 	struct rpc_cred	*cred = NULL,
48431be5bf1STrond Myklebust 			*entry, *new;
48525337fdcSTrond Myklebust 	unsigned int nr;
48625337fdcSTrond Myklebust 
4879e469e30SEric W. Biederman 	nr = hash_long(from_kuid(&init_user_ns, acred->uid), cache->hashbits);
4881da177e4SLinus Torvalds 
48931be5bf1STrond Myklebust 	rcu_read_lock();
490b67bfe0dSSasha Levin 	hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
49131be5bf1STrond Myklebust 		if (!entry->cr_ops->crmatch(acred, entry, flags))
49231be5bf1STrond Myklebust 			continue;
4939499b434STrond Myklebust 		spin_lock(&cache->lock);
49431be5bf1STrond Myklebust 		if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
4959499b434STrond Myklebust 			spin_unlock(&cache->lock);
49631be5bf1STrond Myklebust 			continue;
49731be5bf1STrond Myklebust 		}
49831be5bf1STrond Myklebust 		cred = get_rpccred(entry);
4999499b434STrond Myklebust 		spin_unlock(&cache->lock);
50031be5bf1STrond Myklebust 		break;
50131be5bf1STrond Myklebust 	}
50231be5bf1STrond Myklebust 	rcu_read_unlock();
50331be5bf1STrond Myklebust 
5049499b434STrond Myklebust 	if (cred != NULL)
50531be5bf1STrond Myklebust 		goto found;
50631be5bf1STrond Myklebust 
50731be5bf1STrond Myklebust 	new = auth->au_ops->crcreate(auth, acred, flags);
50831be5bf1STrond Myklebust 	if (IS_ERR(new)) {
50931be5bf1STrond Myklebust 		cred = new;
51031be5bf1STrond Myklebust 		goto out;
51131be5bf1STrond Myklebust 	}
51231be5bf1STrond Myklebust 
5139499b434STrond Myklebust 	spin_lock(&cache->lock);
514b67bfe0dSSasha Levin 	hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
515e092bdcdSTrond Myklebust 		if (!entry->cr_ops->crmatch(acred, entry, flags))
516e092bdcdSTrond Myklebust 			continue;
517e092bdcdSTrond Myklebust 		cred = get_rpccred(entry);
5181da177e4SLinus Torvalds 		break;
5191da177e4SLinus Torvalds 	}
52031be5bf1STrond Myklebust 	if (cred == NULL) {
52131be5bf1STrond Myklebust 		cred = new;
52231be5bf1STrond Myklebust 		set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
52331be5bf1STrond Myklebust 		hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
52431be5bf1STrond Myklebust 	} else
525e092bdcdSTrond Myklebust 		list_add_tail(&new->cr_lru, &free);
5269499b434STrond Myklebust 	spin_unlock(&cache->lock);
52731be5bf1STrond Myklebust found:
528f64f9e71SJoe Perches 	if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
529f64f9e71SJoe Perches 	    cred->cr_ops->cr_init != NULL &&
530f64f9e71SJoe Perches 	    !(flags & RPCAUTH_LOOKUP_NEW)) {
531fba3bad4STrond Myklebust 		int res = cred->cr_ops->cr_init(auth, cred);
532fba3bad4STrond Myklebust 		if (res < 0) {
533fba3bad4STrond Myklebust 			put_rpccred(cred);
534fba3bad4STrond Myklebust 			cred = ERR_PTR(res);
535fba3bad4STrond Myklebust 		}
5361da177e4SLinus Torvalds 	}
53731be5bf1STrond Myklebust 	rpcauth_destroy_credlist(&free);
53831be5bf1STrond Myklebust out:
53931be5bf1STrond Myklebust 	return cred;
5401da177e4SLinus Torvalds }
541e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds struct rpc_cred *
5448a317760STrond Myklebust rpcauth_lookupcred(struct rpc_auth *auth, int flags)
5451da177e4SLinus Torvalds {
54686a264abSDavid Howells 	struct auth_cred acred;
5471da177e4SLinus Torvalds 	struct rpc_cred *ret;
54886a264abSDavid Howells 	const struct cred *cred = current_cred();
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	dprintk("RPC:       looking up %s cred\n",
5511da177e4SLinus Torvalds 		auth->au_ops->au_name);
55286a264abSDavid Howells 
55386a264abSDavid Howells 	memset(&acred, 0, sizeof(acred));
55486a264abSDavid Howells 	acred.uid = cred->fsuid;
55586a264abSDavid Howells 	acred.gid = cred->fsgid;
55686a264abSDavid Howells 	acred.group_info = get_group_info(((struct cred *)cred)->group_info);
55786a264abSDavid Howells 
5588a317760STrond Myklebust 	ret = auth->au_ops->lookup_cred(auth, &acred, flags);
5591da177e4SLinus Torvalds 	put_group_info(acred.group_info);
5601da177e4SLinus Torvalds 	return ret;
5611da177e4SLinus Torvalds }
5621da177e4SLinus Torvalds 
5635fe4755eSTrond Myklebust void
5645fe4755eSTrond Myklebust rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
5655fe4755eSTrond Myklebust 		  struct rpc_auth *auth, const struct rpc_credops *ops)
5665fe4755eSTrond Myklebust {
5675fe4755eSTrond Myklebust 	INIT_HLIST_NODE(&cred->cr_hash);
568e092bdcdSTrond Myklebust 	INIT_LIST_HEAD(&cred->cr_lru);
5695fe4755eSTrond Myklebust 	atomic_set(&cred->cr_count, 1);
5705fe4755eSTrond Myklebust 	cred->cr_auth = auth;
5715fe4755eSTrond Myklebust 	cred->cr_ops = ops;
5725fe4755eSTrond Myklebust 	cred->cr_expire = jiffies;
5735fe4755eSTrond Myklebust #ifdef RPC_DEBUG
5745fe4755eSTrond Myklebust 	cred->cr_magic = RPCAUTH_CRED_MAGIC;
5755fe4755eSTrond Myklebust #endif
5765fe4755eSTrond Myklebust 	cred->cr_uid = acred->uid;
5775fe4755eSTrond Myklebust }
578e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_init_cred);
5795fe4755eSTrond Myklebust 
5808572b8e2STrond Myklebust struct rpc_cred *
5815d351754STrond Myklebust rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
5824ccda2cdSTrond Myklebust {
5834ccda2cdSTrond Myklebust 	dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
5844ccda2cdSTrond Myklebust 			cred->cr_auth->au_ops->au_name, cred);
5858572b8e2STrond Myklebust 	return get_rpccred(cred);
5864ccda2cdSTrond Myklebust }
5875c691044STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
5884ccda2cdSTrond Myklebust 
5898572b8e2STrond Myklebust static struct rpc_cred *
5905d351754STrond Myklebust rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
5911da177e4SLinus Torvalds {
5921be27f36STrond Myklebust 	struct rpc_auth *auth = task->tk_client->cl_auth;
5931da177e4SLinus Torvalds 	struct auth_cred acred = {
594bf37f794SEric W. Biederman 		.uid = GLOBAL_ROOT_UID,
595bf37f794SEric W. Biederman 		.gid = GLOBAL_ROOT_GID,
5961da177e4SLinus Torvalds 	};
5971da177e4SLinus Torvalds 
59846121cf7SChuck Lever 	dprintk("RPC: %5u looking up %s cred\n",
5991be27f36STrond Myklebust 		task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
6008572b8e2STrond Myklebust 	return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
601af093835STrond Myklebust }
602af093835STrond Myklebust 
6038572b8e2STrond Myklebust static struct rpc_cred *
6045d351754STrond Myklebust rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
605af093835STrond Myklebust {
606af093835STrond Myklebust 	struct rpc_auth *auth = task->tk_client->cl_auth;
607af093835STrond Myklebust 
608af093835STrond Myklebust 	dprintk("RPC: %5u looking up %s cred\n",
609af093835STrond Myklebust 		task->tk_pid, auth->au_ops->au_name);
6108572b8e2STrond Myklebust 	return rpcauth_lookupcred(auth, lookupflags);
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
613a17c2153STrond Myklebust static int
6144ccda2cdSTrond Myklebust rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
6151da177e4SLinus Torvalds {
616a17c2153STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
6178572b8e2STrond Myklebust 	struct rpc_cred *new;
6185d351754STrond Myklebust 	int lookupflags = 0;
6195d351754STrond Myklebust 
6205d351754STrond Myklebust 	if (flags & RPC_TASK_ASYNC)
6215d351754STrond Myklebust 		lookupflags |= RPCAUTH_LOOKUP_NEW;
6224ccda2cdSTrond Myklebust 	if (cred != NULL)
6238572b8e2STrond Myklebust 		new = cred->cr_ops->crbind(task, cred, lookupflags);
6244ccda2cdSTrond Myklebust 	else if (flags & RPC_TASK_ROOTCREDS)
6258572b8e2STrond Myklebust 		new = rpcauth_bind_root_cred(task, lookupflags);
6264ccda2cdSTrond Myklebust 	else
6278572b8e2STrond Myklebust 		new = rpcauth_bind_new_cred(task, lookupflags);
6288572b8e2STrond Myklebust 	if (IS_ERR(new))
6298572b8e2STrond Myklebust 		return PTR_ERR(new);
630a17c2153STrond Myklebust 	if (req->rq_cred != NULL)
631a17c2153STrond Myklebust 		put_rpccred(req->rq_cred);
632a17c2153STrond Myklebust 	req->rq_cred = new;
6338572b8e2STrond Myklebust 	return 0;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds void
6371da177e4SLinus Torvalds put_rpccred(struct rpc_cred *cred)
6381da177e4SLinus Torvalds {
639e092bdcdSTrond Myklebust 	/* Fast path for unhashed credentials */
640f0380f3dSTrond Myklebust 	if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
641f0380f3dSTrond Myklebust 		if (atomic_dec_and_test(&cred->cr_count))
642f0380f3dSTrond Myklebust 			cred->cr_ops->crdestroy(cred);
6431da177e4SLinus Torvalds 		return;
644f0380f3dSTrond Myklebust 	}
645f0380f3dSTrond Myklebust 
646e092bdcdSTrond Myklebust 	if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
647e092bdcdSTrond Myklebust 		return;
648f5c2187cSTrond Myklebust 	if (!list_empty(&cred->cr_lru)) {
649f5c2187cSTrond Myklebust 		number_cred_unused--;
650e092bdcdSTrond Myklebust 		list_del_init(&cred->cr_lru);
651f5c2187cSTrond Myklebust 	}
6525f707eb4STrond Myklebust 	if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
653f0380f3dSTrond Myklebust 		if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
654e092bdcdSTrond Myklebust 			cred->cr_expire = jiffies;
655e092bdcdSTrond Myklebust 			list_add_tail(&cred->cr_lru, &cred_unused);
656f5c2187cSTrond Myklebust 			number_cred_unused++;
657f0380f3dSTrond Myklebust 			goto out_nodestroy;
658f0380f3dSTrond Myklebust 		}
659f0380f3dSTrond Myklebust 		if (!rpcauth_unhash_cred(cred)) {
660f0380f3dSTrond Myklebust 			/* We were hashed and someone looked us up... */
661f0380f3dSTrond Myklebust 			goto out_nodestroy;
662f0380f3dSTrond Myklebust 		}
663e092bdcdSTrond Myklebust 	}
664e092bdcdSTrond Myklebust 	spin_unlock(&rpc_credcache_lock);
6651da177e4SLinus Torvalds 	cred->cr_ops->crdestroy(cred);
666f0380f3dSTrond Myklebust 	return;
667f0380f3dSTrond Myklebust out_nodestroy:
668f0380f3dSTrond Myklebust 	spin_unlock(&rpc_credcache_lock);
6691da177e4SLinus Torvalds }
670e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(put_rpccred);
6711da177e4SLinus Torvalds 
672d8ed029dSAlexey Dobriyan __be32 *
673d8ed029dSAlexey Dobriyan rpcauth_marshcred(struct rpc_task *task, __be32 *p)
6741da177e4SLinus Torvalds {
675a17c2153STrond Myklebust 	struct rpc_cred	*cred = task->tk_rqstp->rq_cred;
6761da177e4SLinus Torvalds 
67746121cf7SChuck Lever 	dprintk("RPC: %5u marshaling %s cred %p\n",
6781be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
6790bbacc40SChuck Lever 
6801da177e4SLinus Torvalds 	return cred->cr_ops->crmarshal(task, p);
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds 
683d8ed029dSAlexey Dobriyan __be32 *
684d8ed029dSAlexey Dobriyan rpcauth_checkverf(struct rpc_task *task, __be32 *p)
6851da177e4SLinus Torvalds {
686a17c2153STrond Myklebust 	struct rpc_cred	*cred = task->tk_rqstp->rq_cred;
6871da177e4SLinus Torvalds 
68846121cf7SChuck Lever 	dprintk("RPC: %5u validating %s cred %p\n",
6891be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
6900bbacc40SChuck Lever 
6911da177e4SLinus Torvalds 	return cred->cr_ops->crvalidate(task, p);
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6949f06c719SChuck Lever static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
6959f06c719SChuck Lever 				   __be32 *data, void *obj)
6969f06c719SChuck Lever {
6979f06c719SChuck Lever 	struct xdr_stream xdr;
6989f06c719SChuck Lever 
6999f06c719SChuck Lever 	xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
7009f06c719SChuck Lever 	encode(rqstp, &xdr, obj);
7019f06c719SChuck Lever }
7029f06c719SChuck Lever 
7031da177e4SLinus Torvalds int
7049f06c719SChuck Lever rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
705d8ed029dSAlexey Dobriyan 		__be32 *data, void *obj)
7061da177e4SLinus Torvalds {
707a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
7081da177e4SLinus Torvalds 
70946121cf7SChuck Lever 	dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
7101da177e4SLinus Torvalds 			task->tk_pid, cred->cr_ops->cr_name, cred);
7111da177e4SLinus Torvalds 	if (cred->cr_ops->crwrap_req)
7121da177e4SLinus Torvalds 		return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
7131da177e4SLinus Torvalds 	/* By default, we encode the arguments normally. */
7149f06c719SChuck Lever 	rpcauth_wrap_req_encode(encode, rqstp, data, obj);
7159f06c719SChuck Lever 	return 0;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
718bf269551SChuck Lever static int
719bf269551SChuck Lever rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
720bf269551SChuck Lever 			  __be32 *data, void *obj)
721bf269551SChuck Lever {
722bf269551SChuck Lever 	struct xdr_stream xdr;
723bf269551SChuck Lever 
724bf269551SChuck Lever 	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
725bf269551SChuck Lever 	return decode(rqstp, &xdr, obj);
726bf269551SChuck Lever }
727bf269551SChuck Lever 
7281da177e4SLinus Torvalds int
729bf269551SChuck Lever rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
730d8ed029dSAlexey Dobriyan 		__be32 *data, void *obj)
7311da177e4SLinus Torvalds {
732a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
7331da177e4SLinus Torvalds 
73446121cf7SChuck Lever 	dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
7351da177e4SLinus Torvalds 			task->tk_pid, cred->cr_ops->cr_name, cred);
7361da177e4SLinus Torvalds 	if (cred->cr_ops->crunwrap_resp)
7371da177e4SLinus Torvalds 		return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
7381da177e4SLinus Torvalds 						   data, obj);
7391da177e4SLinus Torvalds 	/* By default, we decode the arguments normally. */
740bf269551SChuck Lever 	return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds int
7441da177e4SLinus Torvalds rpcauth_refreshcred(struct rpc_task *task)
7451da177e4SLinus Torvalds {
7469a84d380STrond Myklebust 	struct rpc_cred	*cred;
7471da177e4SLinus Torvalds 	int err;
7481da177e4SLinus Torvalds 
749a17c2153STrond Myklebust 	cred = task->tk_rqstp->rq_cred;
750a17c2153STrond Myklebust 	if (cred == NULL) {
751a17c2153STrond Myklebust 		err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
752a17c2153STrond Myklebust 		if (err < 0)
753a17c2153STrond Myklebust 			goto out;
754a17c2153STrond Myklebust 		cred = task->tk_rqstp->rq_cred;
755f81c6224SJoe Perches 	}
75646121cf7SChuck Lever 	dprintk("RPC: %5u refreshing %s cred %p\n",
7571be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
7580bbacc40SChuck Lever 
7591da177e4SLinus Torvalds 	err = cred->cr_ops->crrefresh(task);
760a17c2153STrond Myklebust out:
7611da177e4SLinus Torvalds 	if (err < 0)
7621da177e4SLinus Torvalds 		task->tk_status = err;
7631da177e4SLinus Torvalds 	return err;
7641da177e4SLinus Torvalds }
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds void
7671da177e4SLinus Torvalds rpcauth_invalcred(struct rpc_task *task)
7681da177e4SLinus Torvalds {
769a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
770fc432dd9STrond Myklebust 
77146121cf7SChuck Lever 	dprintk("RPC: %5u invalidating %s cred %p\n",
7721be27f36STrond Myklebust 		task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
773fc432dd9STrond Myklebust 	if (cred)
774fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds int
7781da177e4SLinus Torvalds rpcauth_uptodatecred(struct rpc_task *task)
7791da177e4SLinus Torvalds {
780a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
781fc432dd9STrond Myklebust 
782fc432dd9STrond Myklebust 	return cred == NULL ||
783fc432dd9STrond Myklebust 		test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
7841da177e4SLinus Torvalds }
785f5c2187cSTrond Myklebust 
7868e1f936bSRusty Russell static struct shrinker rpc_cred_shrinker = {
7878e1f936bSRusty Russell 	.shrink = rpcauth_cache_shrinker,
7888e1f936bSRusty Russell 	.seeks = DEFAULT_SEEKS,
7898e1f936bSRusty Russell };
790f5c2187cSTrond Myklebust 
7915d8d9a4dSTrond Myklebust int __init rpcauth_init_module(void)
792f5c2187cSTrond Myklebust {
7935d8d9a4dSTrond Myklebust 	int err;
7945d8d9a4dSTrond Myklebust 
7955d8d9a4dSTrond Myklebust 	err = rpc_init_authunix();
7965d8d9a4dSTrond Myklebust 	if (err < 0)
7975d8d9a4dSTrond Myklebust 		goto out1;
7985d8d9a4dSTrond Myklebust 	err = rpc_init_generic_auth();
7995d8d9a4dSTrond Myklebust 	if (err < 0)
8005d8d9a4dSTrond Myklebust 		goto out2;
8018e1f936bSRusty Russell 	register_shrinker(&rpc_cred_shrinker);
8025d8d9a4dSTrond Myklebust 	return 0;
8035d8d9a4dSTrond Myklebust out2:
8045d8d9a4dSTrond Myklebust 	rpc_destroy_authunix();
8055d8d9a4dSTrond Myklebust out1:
8065d8d9a4dSTrond Myklebust 	return err;
807f5c2187cSTrond Myklebust }
808f5c2187cSTrond Myklebust 
809c135e84aSStephen Rothwell void rpcauth_remove_module(void)
810f5c2187cSTrond Myklebust {
8115d8d9a4dSTrond Myklebust 	rpc_destroy_authunix();
8125d8d9a4dSTrond Myklebust 	rpc_destroy_generic_auth();
8138e1f936bSRusty Russell 	unregister_shrinker(&rpc_cred_shrinker);
814f5c2187cSTrond Myklebust }
815