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 362ce52914eSScott Mayhew rpcauth_cred_key_to_expire(struct rpc_auth *auth, struct rpc_cred *cred) 3634de6caa2SAndy Adamson { 364ce52914eSScott Mayhew if (auth->au_flags & RPCAUTH_AUTH_NO_CRKEY_TIMEOUT) 365ce52914eSScott Mayhew return false; 3664de6caa2SAndy Adamson if (!cred->cr_ops->crkey_to_expire) 3674de6caa2SAndy Adamson return false; 3684de6caa2SAndy Adamson return cred->cr_ops->crkey_to_expire(cred); 3694de6caa2SAndy Adamson } 3704de6caa2SAndy Adamson EXPORT_SYMBOL_GPL(rpcauth_cred_key_to_expire); 3714de6caa2SAndy Adamson 372a0337d1dSJeff Layton char * 373a0337d1dSJeff Layton rpcauth_stringify_acceptor(struct rpc_cred *cred) 374a0337d1dSJeff Layton { 375a0337d1dSJeff Layton if (!cred->cr_ops->crstringify_acceptor) 376a0337d1dSJeff Layton return NULL; 377a0337d1dSJeff Layton return cred->cr_ops->crstringify_acceptor(cred); 378a0337d1dSJeff Layton } 379a0337d1dSJeff Layton EXPORT_SYMBOL_GPL(rpcauth_stringify_acceptor); 380a0337d1dSJeff Layton 3814de6caa2SAndy Adamson /* 3821da177e4SLinus Torvalds * Destroy a list of credentials 3831da177e4SLinus Torvalds */ 3841da177e4SLinus Torvalds static inline 385e092bdcdSTrond Myklebust void rpcauth_destroy_credlist(struct list_head *head) 3861da177e4SLinus Torvalds { 3871da177e4SLinus Torvalds struct rpc_cred *cred; 3881da177e4SLinus Torvalds 389e092bdcdSTrond Myklebust while (!list_empty(head)) { 390e092bdcdSTrond Myklebust cred = list_entry(head->next, struct rpc_cred, cr_lru); 391e092bdcdSTrond Myklebust list_del_init(&cred->cr_lru); 3921da177e4SLinus Torvalds put_rpccred(cred); 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds /* 3971da177e4SLinus Torvalds * Clear the RPC credential cache, and delete those credentials 3981da177e4SLinus Torvalds * that are not referenced. 3991da177e4SLinus Torvalds */ 4001da177e4SLinus Torvalds void 4013ab9bb72STrond Myklebust rpcauth_clear_credcache(struct rpc_cred_cache *cache) 4021da177e4SLinus Torvalds { 403e092bdcdSTrond Myklebust LIST_HEAD(free); 404e092bdcdSTrond Myklebust struct hlist_head *head; 4051da177e4SLinus Torvalds struct rpc_cred *cred; 406988664a0STrond Myklebust unsigned int hashsize = 1U << cache->hashbits; 4071da177e4SLinus Torvalds int i; 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds spin_lock(&rpc_credcache_lock); 4109499b434STrond Myklebust spin_lock(&cache->lock); 411988664a0STrond Myklebust for (i = 0; i < hashsize; i++) { 412e092bdcdSTrond Myklebust head = &cache->hashtable[i]; 413e092bdcdSTrond Myklebust while (!hlist_empty(head)) { 414e092bdcdSTrond Myklebust cred = hlist_entry(head->first, struct rpc_cred, cr_hash); 415e092bdcdSTrond Myklebust get_rpccred(cred); 416f5c2187cSTrond Myklebust if (!list_empty(&cred->cr_lru)) { 417f5c2187cSTrond Myklebust list_del(&cred->cr_lru); 418f5c2187cSTrond Myklebust number_cred_unused--; 419f5c2187cSTrond Myklebust } 420f5c2187cSTrond Myklebust list_add_tail(&cred->cr_lru, &free); 42131be5bf1STrond Myklebust rpcauth_unhash_cred_locked(cred); 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds } 4249499b434STrond Myklebust spin_unlock(&cache->lock); 4251da177e4SLinus Torvalds spin_unlock(&rpc_credcache_lock); 4261da177e4SLinus Torvalds rpcauth_destroy_credlist(&free); 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4293ab9bb72STrond Myklebust /* 4303ab9bb72STrond Myklebust * Destroy the RPC credential cache 4313ab9bb72STrond Myklebust */ 4323ab9bb72STrond Myklebust void 4333ab9bb72STrond Myklebust rpcauth_destroy_credcache(struct rpc_auth *auth) 4343ab9bb72STrond Myklebust { 4353ab9bb72STrond Myklebust struct rpc_cred_cache *cache = auth->au_credcache; 4363ab9bb72STrond Myklebust 4373ab9bb72STrond Myklebust if (cache) { 4383ab9bb72STrond Myklebust auth->au_credcache = NULL; 4393ab9bb72STrond Myklebust rpcauth_clear_credcache(cache); 440241269bdSTrond Myklebust kfree(cache->hashtable); 4413ab9bb72STrond Myklebust kfree(cache); 4423ab9bb72STrond Myklebust } 4433ab9bb72STrond Myklebust } 444e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache); 4453ab9bb72STrond Myklebust 446d2b83141STrond Myklebust 447d2b83141STrond Myklebust #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ) 448d2b83141STrond Myklebust 4491da177e4SLinus Torvalds /* 4501da177e4SLinus Torvalds * Remove stale credentials. Avoid sleeping inside the loop. 4511da177e4SLinus Torvalds */ 45270534a73SDave Chinner static long 453f5c2187cSTrond Myklebust rpcauth_prune_expired(struct list_head *free, int nr_to_scan) 4541da177e4SLinus Torvalds { 4559499b434STrond Myklebust spinlock_t *cache_lock; 456eac0d18dSTrond Myklebust struct rpc_cred *cred, *next; 457d2b83141STrond Myklebust unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM; 45870534a73SDave Chinner long freed = 0; 4591da177e4SLinus Torvalds 460eac0d18dSTrond Myklebust list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) { 461eac0d18dSTrond Myklebust 46220673406STrond Myklebust if (nr_to_scan-- == 0) 46320673406STrond Myklebust break; 46493a05e65STrond Myklebust /* 46593a05e65STrond Myklebust * Enforce a 60 second garbage collection moratorium 46693a05e65STrond Myklebust * Note that the cred_unused list must be time-ordered. 46793a05e65STrond Myklebust */ 4683d7b0894STrond Myklebust if (time_in_range(cred->cr_expire, expired, jiffies) && 469eac0d18dSTrond Myklebust test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) 47070534a73SDave Chinner break; 471eac0d18dSTrond Myklebust 472e092bdcdSTrond Myklebust list_del_init(&cred->cr_lru); 473f5c2187cSTrond Myklebust number_cred_unused--; 47470534a73SDave Chinner freed++; 475e092bdcdSTrond Myklebust if (atomic_read(&cred->cr_count) != 0) 476e092bdcdSTrond Myklebust continue; 477eac0d18dSTrond Myklebust 4789499b434STrond Myklebust cache_lock = &cred->cr_auth->au_credcache->lock; 4799499b434STrond Myklebust spin_lock(cache_lock); 4809499b434STrond Myklebust if (atomic_read(&cred->cr_count) == 0) { 481e092bdcdSTrond Myklebust get_rpccred(cred); 482e092bdcdSTrond Myklebust list_add_tail(&cred->cr_lru, free); 48331be5bf1STrond Myklebust rpcauth_unhash_cred_locked(cred); 4841da177e4SLinus Torvalds } 4859499b434STrond Myklebust spin_unlock(cache_lock); 4869499b434STrond Myklebust } 48770534a73SDave Chinner return freed; 4881da177e4SLinus Torvalds } 489e092bdcdSTrond Myklebust 490bae6746fSTrond Myklebust static unsigned long 491bae6746fSTrond Myklebust rpcauth_cache_do_shrink(int nr_to_scan) 492bae6746fSTrond Myklebust { 493bae6746fSTrond Myklebust LIST_HEAD(free); 494bae6746fSTrond Myklebust unsigned long freed; 495bae6746fSTrond Myklebust 496bae6746fSTrond Myklebust spin_lock(&rpc_credcache_lock); 497bae6746fSTrond Myklebust freed = rpcauth_prune_expired(&free, nr_to_scan); 498bae6746fSTrond Myklebust spin_unlock(&rpc_credcache_lock); 499bae6746fSTrond Myklebust rpcauth_destroy_credlist(&free); 500bae6746fSTrond Myklebust 501bae6746fSTrond Myklebust return freed; 502bae6746fSTrond Myklebust } 503bae6746fSTrond Myklebust 504e092bdcdSTrond Myklebust /* 505f5c2187cSTrond Myklebust * Run memory cache shrinker. 506e092bdcdSTrond Myklebust */ 50770534a73SDave Chinner static unsigned long 50870534a73SDave Chinner rpcauth_cache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) 50970534a73SDave Chinner 510e092bdcdSTrond Myklebust { 51170534a73SDave Chinner if ((sc->gfp_mask & GFP_KERNEL) != GFP_KERNEL) 51270534a73SDave Chinner return SHRINK_STOP; 51370534a73SDave Chinner 51470534a73SDave Chinner /* nothing left, don't come back */ 515f5c2187cSTrond Myklebust if (list_empty(&cred_unused)) 51670534a73SDave Chinner return SHRINK_STOP; 51770534a73SDave Chinner 518bae6746fSTrond Myklebust return rpcauth_cache_do_shrink(sc->nr_to_scan); 51970534a73SDave Chinner } 52070534a73SDave Chinner 52170534a73SDave Chinner static unsigned long 52270534a73SDave Chinner rpcauth_cache_shrink_count(struct shrinker *shrink, struct shrink_control *sc) 52370534a73SDave Chinner 52470534a73SDave Chinner { 52570534a73SDave Chinner return (number_cred_unused / 100) * sysctl_vfs_cache_pressure; 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds 528bae6746fSTrond Myklebust static void 529bae6746fSTrond Myklebust rpcauth_cache_enforce_limit(void) 530bae6746fSTrond Myklebust { 531bae6746fSTrond Myklebust unsigned long diff; 532bae6746fSTrond Myklebust unsigned int nr_to_scan; 533bae6746fSTrond Myklebust 534bae6746fSTrond Myklebust if (number_cred_unused <= auth_max_cred_cachesize) 535bae6746fSTrond Myklebust return; 536bae6746fSTrond Myklebust diff = number_cred_unused - auth_max_cred_cachesize; 537bae6746fSTrond Myklebust nr_to_scan = 100; 538bae6746fSTrond Myklebust if (diff < nr_to_scan) 539bae6746fSTrond Myklebust nr_to_scan = diff; 540bae6746fSTrond Myklebust rpcauth_cache_do_shrink(nr_to_scan); 541bae6746fSTrond Myklebust } 542bae6746fSTrond Myklebust 5431da177e4SLinus Torvalds /* 5441da177e4SLinus Torvalds * Look up a process' credentials in the authentication cache 5451da177e4SLinus Torvalds */ 5461da177e4SLinus Torvalds struct rpc_cred * 5471da177e4SLinus Torvalds rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred, 5483c6e0bc8SJeff Layton int flags, gfp_t gfp) 5491da177e4SLinus Torvalds { 550e092bdcdSTrond Myklebust LIST_HEAD(free); 5511da177e4SLinus Torvalds struct rpc_cred_cache *cache = auth->au_credcache; 55231be5bf1STrond Myklebust struct rpc_cred *cred = NULL, 55331be5bf1STrond Myklebust *entry, *new; 55425337fdcSTrond Myklebust unsigned int nr; 55525337fdcSTrond Myklebust 5569e469e30SEric W. Biederman nr = hash_long(from_kuid(&init_user_ns, acred->uid), cache->hashbits); 5571da177e4SLinus Torvalds 55831be5bf1STrond Myklebust rcu_read_lock(); 559b67bfe0dSSasha Levin hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) { 56031be5bf1STrond Myklebust if (!entry->cr_ops->crmatch(acred, entry, flags)) 56131be5bf1STrond Myklebust continue; 562bd956080SNeilBrown if (flags & RPCAUTH_LOOKUP_RCU) { 563bd956080SNeilBrown if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) && 564bd956080SNeilBrown !test_bit(RPCAUTH_CRED_NEW, &entry->cr_flags)) 565bd956080SNeilBrown cred = entry; 566bd956080SNeilBrown break; 567bd956080SNeilBrown } 5689499b434STrond Myklebust spin_lock(&cache->lock); 56931be5bf1STrond Myklebust if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) { 5709499b434STrond Myklebust spin_unlock(&cache->lock); 57131be5bf1STrond Myklebust continue; 57231be5bf1STrond Myklebust } 57331be5bf1STrond Myklebust cred = get_rpccred(entry); 5749499b434STrond Myklebust spin_unlock(&cache->lock); 57531be5bf1STrond Myklebust break; 57631be5bf1STrond Myklebust } 57731be5bf1STrond Myklebust rcu_read_unlock(); 57831be5bf1STrond Myklebust 5799499b434STrond Myklebust if (cred != NULL) 58031be5bf1STrond Myklebust goto found; 58131be5bf1STrond Myklebust 582bd956080SNeilBrown if (flags & RPCAUTH_LOOKUP_RCU) 583bd956080SNeilBrown return ERR_PTR(-ECHILD); 584bd956080SNeilBrown 5853c6e0bc8SJeff Layton new = auth->au_ops->crcreate(auth, acred, flags, gfp); 58631be5bf1STrond Myklebust if (IS_ERR(new)) { 58731be5bf1STrond Myklebust cred = new; 58831be5bf1STrond Myklebust goto out; 58931be5bf1STrond Myklebust } 59031be5bf1STrond Myklebust 5919499b434STrond Myklebust spin_lock(&cache->lock); 592b67bfe0dSSasha Levin hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) { 593e092bdcdSTrond Myklebust if (!entry->cr_ops->crmatch(acred, entry, flags)) 594e092bdcdSTrond Myklebust continue; 595e092bdcdSTrond Myklebust cred = get_rpccred(entry); 5961da177e4SLinus Torvalds break; 5971da177e4SLinus Torvalds } 59831be5bf1STrond Myklebust if (cred == NULL) { 59931be5bf1STrond Myklebust cred = new; 60031be5bf1STrond Myklebust set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags); 60131be5bf1STrond Myklebust hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]); 60231be5bf1STrond Myklebust } else 603e092bdcdSTrond Myklebust list_add_tail(&new->cr_lru, &free); 6049499b434STrond Myklebust spin_unlock(&cache->lock); 605bae6746fSTrond Myklebust rpcauth_cache_enforce_limit(); 60631be5bf1STrond Myklebust found: 607f64f9e71SJoe Perches if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) && 608f64f9e71SJoe Perches cred->cr_ops->cr_init != NULL && 609f64f9e71SJoe Perches !(flags & RPCAUTH_LOOKUP_NEW)) { 610fba3bad4STrond Myklebust int res = cred->cr_ops->cr_init(auth, cred); 611fba3bad4STrond Myklebust if (res < 0) { 612fba3bad4STrond Myklebust put_rpccred(cred); 613fba3bad4STrond Myklebust cred = ERR_PTR(res); 614fba3bad4STrond Myklebust } 6151da177e4SLinus Torvalds } 61631be5bf1STrond Myklebust rpcauth_destroy_credlist(&free); 61731be5bf1STrond Myklebust out: 61831be5bf1STrond Myklebust return cred; 6191da177e4SLinus Torvalds } 620e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache); 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds struct rpc_cred * 6238a317760STrond Myklebust rpcauth_lookupcred(struct rpc_auth *auth, int flags) 6241da177e4SLinus Torvalds { 62586a264abSDavid Howells struct auth_cred acred; 6261da177e4SLinus Torvalds struct rpc_cred *ret; 62786a264abSDavid Howells const struct cred *cred = current_cred(); 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds dprintk("RPC: looking up %s cred\n", 6301da177e4SLinus Torvalds auth->au_ops->au_name); 63186a264abSDavid Howells 63286a264abSDavid Howells memset(&acred, 0, sizeof(acred)); 63386a264abSDavid Howells acred.uid = cred->fsuid; 63486a264abSDavid Howells acred.gid = cred->fsgid; 635122a8cdaSNeilBrown acred.group_info = cred->group_info; 6368a317760STrond Myklebust ret = auth->au_ops->lookup_cred(auth, &acred, flags); 6371da177e4SLinus Torvalds return ret; 6381da177e4SLinus Torvalds } 63966b06860SAndy Adamson EXPORT_SYMBOL_GPL(rpcauth_lookupcred); 6401da177e4SLinus Torvalds 6415fe4755eSTrond Myklebust void 6425fe4755eSTrond Myklebust rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred, 6435fe4755eSTrond Myklebust struct rpc_auth *auth, const struct rpc_credops *ops) 6445fe4755eSTrond Myklebust { 6455fe4755eSTrond Myklebust INIT_HLIST_NODE(&cred->cr_hash); 646e092bdcdSTrond Myklebust INIT_LIST_HEAD(&cred->cr_lru); 6475fe4755eSTrond Myklebust atomic_set(&cred->cr_count, 1); 6485fe4755eSTrond Myklebust cred->cr_auth = auth; 6495fe4755eSTrond Myklebust cred->cr_ops = ops; 6505fe4755eSTrond Myklebust cred->cr_expire = jiffies; 651f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 6525fe4755eSTrond Myklebust cred->cr_magic = RPCAUTH_CRED_MAGIC; 6535fe4755eSTrond Myklebust #endif 6545fe4755eSTrond Myklebust cred->cr_uid = acred->uid; 6555fe4755eSTrond Myklebust } 656e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_init_cred); 6575fe4755eSTrond Myklebust 6588572b8e2STrond Myklebust struct rpc_cred * 6595d351754STrond Myklebust rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags) 6604ccda2cdSTrond Myklebust { 6614ccda2cdSTrond Myklebust dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid, 6624ccda2cdSTrond Myklebust cred->cr_auth->au_ops->au_name, cred); 6638572b8e2STrond Myklebust return get_rpccred(cred); 6644ccda2cdSTrond Myklebust } 6655c691044STrond Myklebust EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred); 6664ccda2cdSTrond Myklebust 6678572b8e2STrond Myklebust static struct rpc_cred * 6685d351754STrond Myklebust rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags) 6691da177e4SLinus Torvalds { 6701be27f36STrond Myklebust struct rpc_auth *auth = task->tk_client->cl_auth; 6711da177e4SLinus Torvalds struct auth_cred acred = { 672bf37f794SEric W. Biederman .uid = GLOBAL_ROOT_UID, 673bf37f794SEric W. Biederman .gid = GLOBAL_ROOT_GID, 6741da177e4SLinus Torvalds }; 6751da177e4SLinus Torvalds 67646121cf7SChuck Lever dprintk("RPC: %5u looking up %s cred\n", 6771be27f36STrond Myklebust task->tk_pid, task->tk_client->cl_auth->au_ops->au_name); 6788572b8e2STrond Myklebust return auth->au_ops->lookup_cred(auth, &acred, lookupflags); 679af093835STrond Myklebust } 680af093835STrond Myklebust 6818572b8e2STrond Myklebust static struct rpc_cred * 6825d351754STrond Myklebust rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags) 683af093835STrond Myklebust { 684af093835STrond Myklebust struct rpc_auth *auth = task->tk_client->cl_auth; 685af093835STrond Myklebust 686af093835STrond Myklebust dprintk("RPC: %5u looking up %s cred\n", 687af093835STrond Myklebust task->tk_pid, auth->au_ops->au_name); 6888572b8e2STrond Myklebust return rpcauth_lookupcred(auth, lookupflags); 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds 691a17c2153STrond Myklebust static int 6924ccda2cdSTrond Myklebust rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags) 6931da177e4SLinus Torvalds { 694a17c2153STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 6958572b8e2STrond Myklebust struct rpc_cred *new; 6965d351754STrond Myklebust int lookupflags = 0; 6975d351754STrond Myklebust 6985d351754STrond Myklebust if (flags & RPC_TASK_ASYNC) 6995d351754STrond Myklebust lookupflags |= RPCAUTH_LOOKUP_NEW; 7004ccda2cdSTrond Myklebust if (cred != NULL) 7018572b8e2STrond Myklebust new = cred->cr_ops->crbind(task, cred, lookupflags); 7024ccda2cdSTrond Myklebust else if (flags & RPC_TASK_ROOTCREDS) 7038572b8e2STrond Myklebust new = rpcauth_bind_root_cred(task, lookupflags); 7044ccda2cdSTrond Myklebust else 7058572b8e2STrond Myklebust new = rpcauth_bind_new_cred(task, lookupflags); 7068572b8e2STrond Myklebust if (IS_ERR(new)) 7078572b8e2STrond Myklebust return PTR_ERR(new); 708a17c2153STrond Myklebust put_rpccred(req->rq_cred); 709a17c2153STrond Myklebust req->rq_cred = new; 7108572b8e2STrond Myklebust return 0; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds void 7141da177e4SLinus Torvalds put_rpccred(struct rpc_cred *cred) 7151da177e4SLinus Torvalds { 7169a8f6b5eSTrond Myklebust if (cred == NULL) 7179a8f6b5eSTrond Myklebust return; 718e092bdcdSTrond Myklebust /* Fast path for unhashed credentials */ 719f0380f3dSTrond Myklebust if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) { 720f0380f3dSTrond Myklebust if (atomic_dec_and_test(&cred->cr_count)) 721f0380f3dSTrond Myklebust cred->cr_ops->crdestroy(cred); 7221da177e4SLinus Torvalds return; 723f0380f3dSTrond Myklebust } 724f0380f3dSTrond Myklebust 725e092bdcdSTrond Myklebust if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock)) 726e092bdcdSTrond Myklebust return; 727f5c2187cSTrond Myklebust if (!list_empty(&cred->cr_lru)) { 728f5c2187cSTrond Myklebust number_cred_unused--; 729e092bdcdSTrond Myklebust list_del_init(&cred->cr_lru); 730f5c2187cSTrond Myklebust } 7315f707eb4STrond Myklebust if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) { 732f0380f3dSTrond Myklebust if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) { 733e092bdcdSTrond Myklebust cred->cr_expire = jiffies; 734e092bdcdSTrond Myklebust list_add_tail(&cred->cr_lru, &cred_unused); 735f5c2187cSTrond Myklebust number_cred_unused++; 736f0380f3dSTrond Myklebust goto out_nodestroy; 737f0380f3dSTrond Myklebust } 738f0380f3dSTrond Myklebust if (!rpcauth_unhash_cred(cred)) { 739f0380f3dSTrond Myklebust /* We were hashed and someone looked us up... */ 740f0380f3dSTrond Myklebust goto out_nodestroy; 741f0380f3dSTrond Myklebust } 742e092bdcdSTrond Myklebust } 743e092bdcdSTrond Myklebust spin_unlock(&rpc_credcache_lock); 7441da177e4SLinus Torvalds cred->cr_ops->crdestroy(cred); 745f0380f3dSTrond Myklebust return; 746f0380f3dSTrond Myklebust out_nodestroy: 747f0380f3dSTrond Myklebust spin_unlock(&rpc_credcache_lock); 7481da177e4SLinus Torvalds } 749e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(put_rpccred); 7501da177e4SLinus Torvalds 751d8ed029dSAlexey Dobriyan __be32 * 752d8ed029dSAlexey Dobriyan rpcauth_marshcred(struct rpc_task *task, __be32 *p) 7531da177e4SLinus Torvalds { 754a17c2153STrond Myklebust struct rpc_cred *cred = task->tk_rqstp->rq_cred; 7551da177e4SLinus Torvalds 75646121cf7SChuck Lever dprintk("RPC: %5u marshaling %s cred %p\n", 7571be27f36STrond Myklebust task->tk_pid, cred->cr_auth->au_ops->au_name, cred); 7580bbacc40SChuck Lever 7591da177e4SLinus Torvalds return cred->cr_ops->crmarshal(task, p); 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 762d8ed029dSAlexey Dobriyan __be32 * 763d8ed029dSAlexey Dobriyan rpcauth_checkverf(struct rpc_task *task, __be32 *p) 7641da177e4SLinus Torvalds { 765a17c2153STrond Myklebust struct rpc_cred *cred = task->tk_rqstp->rq_cred; 7661da177e4SLinus Torvalds 76746121cf7SChuck Lever dprintk("RPC: %5u validating %s cred %p\n", 7681be27f36STrond Myklebust task->tk_pid, cred->cr_auth->au_ops->au_name, cred); 7690bbacc40SChuck Lever 7701da177e4SLinus Torvalds return cred->cr_ops->crvalidate(task, p); 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 7739f06c719SChuck Lever static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp, 7749f06c719SChuck Lever __be32 *data, void *obj) 7759f06c719SChuck Lever { 7769f06c719SChuck Lever struct xdr_stream xdr; 7779f06c719SChuck Lever 7789f06c719SChuck Lever xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data); 7799f06c719SChuck Lever encode(rqstp, &xdr, obj); 7809f06c719SChuck Lever } 7819f06c719SChuck Lever 7821da177e4SLinus Torvalds int 7839f06c719SChuck Lever rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp, 784d8ed029dSAlexey Dobriyan __be32 *data, void *obj) 7851da177e4SLinus Torvalds { 786a17c2153STrond Myklebust struct rpc_cred *cred = task->tk_rqstp->rq_cred; 7871da177e4SLinus Torvalds 78846121cf7SChuck Lever dprintk("RPC: %5u using %s cred %p to wrap rpc data\n", 7891da177e4SLinus Torvalds task->tk_pid, cred->cr_ops->cr_name, cred); 7901da177e4SLinus Torvalds if (cred->cr_ops->crwrap_req) 7911da177e4SLinus Torvalds return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj); 7921da177e4SLinus Torvalds /* By default, we encode the arguments normally. */ 7939f06c719SChuck Lever rpcauth_wrap_req_encode(encode, rqstp, data, obj); 7949f06c719SChuck Lever return 0; 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds 797bf269551SChuck Lever static int 798bf269551SChuck Lever rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp, 799bf269551SChuck Lever __be32 *data, void *obj) 800bf269551SChuck Lever { 801bf269551SChuck Lever struct xdr_stream xdr; 802bf269551SChuck Lever 803bf269551SChuck Lever xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data); 804bf269551SChuck Lever return decode(rqstp, &xdr, obj); 805bf269551SChuck Lever } 806bf269551SChuck Lever 8071da177e4SLinus Torvalds int 808bf269551SChuck Lever rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp, 809d8ed029dSAlexey Dobriyan __be32 *data, void *obj) 8101da177e4SLinus Torvalds { 811a17c2153STrond Myklebust struct rpc_cred *cred = task->tk_rqstp->rq_cred; 8121da177e4SLinus Torvalds 81346121cf7SChuck Lever dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n", 8141da177e4SLinus Torvalds task->tk_pid, cred->cr_ops->cr_name, cred); 8151da177e4SLinus Torvalds if (cred->cr_ops->crunwrap_resp) 8161da177e4SLinus Torvalds return cred->cr_ops->crunwrap_resp(task, decode, rqstp, 8171da177e4SLinus Torvalds data, obj); 8181da177e4SLinus Torvalds /* By default, we decode the arguments normally. */ 819bf269551SChuck Lever return rpcauth_unwrap_req_decode(decode, rqstp, data, obj); 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds int 8231da177e4SLinus Torvalds rpcauth_refreshcred(struct rpc_task *task) 8241da177e4SLinus Torvalds { 8259a84d380STrond Myklebust struct rpc_cred *cred; 8261da177e4SLinus Torvalds int err; 8271da177e4SLinus Torvalds 828a17c2153STrond Myklebust cred = task->tk_rqstp->rq_cred; 829a17c2153STrond Myklebust if (cred == NULL) { 830a17c2153STrond Myklebust err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags); 831a17c2153STrond Myklebust if (err < 0) 832a17c2153STrond Myklebust goto out; 833a17c2153STrond Myklebust cred = task->tk_rqstp->rq_cred; 834f81c6224SJoe Perches } 83546121cf7SChuck Lever dprintk("RPC: %5u refreshing %s cred %p\n", 8361be27f36STrond Myklebust task->tk_pid, cred->cr_auth->au_ops->au_name, cred); 8370bbacc40SChuck Lever 8381da177e4SLinus Torvalds err = cred->cr_ops->crrefresh(task); 839a17c2153STrond Myklebust out: 8401da177e4SLinus Torvalds if (err < 0) 8411da177e4SLinus Torvalds task->tk_status = err; 8421da177e4SLinus Torvalds return err; 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds void 8461da177e4SLinus Torvalds rpcauth_invalcred(struct rpc_task *task) 8471da177e4SLinus Torvalds { 848a17c2153STrond Myklebust struct rpc_cred *cred = task->tk_rqstp->rq_cred; 849fc432dd9STrond Myklebust 85046121cf7SChuck Lever dprintk("RPC: %5u invalidating %s cred %p\n", 8511be27f36STrond Myklebust task->tk_pid, cred->cr_auth->au_ops->au_name, cred); 852fc432dd9STrond Myklebust if (cred) 853fc432dd9STrond Myklebust clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags); 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds int 8571da177e4SLinus Torvalds rpcauth_uptodatecred(struct rpc_task *task) 8581da177e4SLinus Torvalds { 859a17c2153STrond Myklebust struct rpc_cred *cred = task->tk_rqstp->rq_cred; 860fc432dd9STrond Myklebust 861fc432dd9STrond Myklebust return cred == NULL || 862fc432dd9STrond Myklebust test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0; 8631da177e4SLinus Torvalds } 864f5c2187cSTrond Myklebust 8658e1f936bSRusty Russell static struct shrinker rpc_cred_shrinker = { 86670534a73SDave Chinner .count_objects = rpcauth_cache_shrink_count, 86770534a73SDave Chinner .scan_objects = rpcauth_cache_shrink_scan, 8688e1f936bSRusty Russell .seeks = DEFAULT_SEEKS, 8698e1f936bSRusty Russell }; 870f5c2187cSTrond Myklebust 8715d8d9a4dSTrond Myklebust int __init rpcauth_init_module(void) 872f5c2187cSTrond Myklebust { 8735d8d9a4dSTrond Myklebust int err; 8745d8d9a4dSTrond Myklebust 8755d8d9a4dSTrond Myklebust err = rpc_init_authunix(); 8765d8d9a4dSTrond Myklebust if (err < 0) 8775d8d9a4dSTrond Myklebust goto out1; 8785d8d9a4dSTrond Myklebust err = rpc_init_generic_auth(); 8795d8d9a4dSTrond Myklebust if (err < 0) 8805d8d9a4dSTrond Myklebust goto out2; 8818e1f936bSRusty Russell register_shrinker(&rpc_cred_shrinker); 8825d8d9a4dSTrond Myklebust return 0; 8835d8d9a4dSTrond Myklebust out2: 8845d8d9a4dSTrond Myklebust rpc_destroy_authunix(); 8855d8d9a4dSTrond Myklebust out1: 8865d8d9a4dSTrond Myklebust return err; 887f5c2187cSTrond Myklebust } 888f5c2187cSTrond Myklebust 889c135e84aSStephen Rothwell void rpcauth_remove_module(void) 890f5c2187cSTrond Myklebust { 8915d8d9a4dSTrond Myklebust rpc_destroy_authunix(); 8925d8d9a4dSTrond Myklebust rpc_destroy_generic_auth(); 8938e1f936bSRusty Russell unregister_shrinker(&rpc_cred_shrinker); 894f5c2187cSTrond Myklebust } 895