blob: 63e2d35c10d5b7aee2aab67359bd24d5c59bd512 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/auth.c
3 *
4 * Generic RPC client authentication API.
5 *
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
8
9#include <linux/types.h>
10#include <linux/sched.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010011#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/errno.h>
Trond Myklebust25337fd2008-03-12 14:40:14 -040015#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/sunrpc/clnt.h>
Chuck Lever6a1a1e32012-07-11 16:31:08 -040017#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/spinlock.h>
19
Jeff Laytonf895b252014-11-17 16:58:04 -050020#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021# define RPCDBG_FACILITY RPCDBG_AUTH
22#endif
23
Trond Myklebust241269b2010-07-31 14:29:08 -040024#define RPC_CREDCACHE_DEFAULT_HASHBITS (4)
25struct rpc_cred_cache {
26 struct hlist_head *hashtable;
27 unsigned int hashbits;
28 spinlock_t lock;
29};
30
31static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS;
32
Trond Myklebust4e4c3be2018-09-27 13:12:44 -040033static const struct rpc_authops __rcu *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
34 [RPC_AUTH_NULL] = (const struct rpc_authops __force __rcu *)&authnull_ops,
35 [RPC_AUTH_UNIX] = (const struct rpc_authops __force __rcu *)&authunix_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 NULL, /* others can be loadable modules */
37};
38
Trond Myklebuste092bdc2007-06-23 19:45:36 -040039static LIST_HEAD(cred_unused);
Trond Myklebustf5c21872007-06-25 17:11:20 -040040static unsigned long number_cred_unused;
Trond Myklebuste092bdc2007-06-23 19:45:36 -040041
Miquel van Smoorenburgdb5fe262010-09-12 19:55:26 -040042#define MAX_HASHTABLE_BITS (14)
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100043static int param_set_hashtbl_sz(const char *val, const struct kernel_param *kp)
Trond Myklebust241269b2010-07-31 14:29:08 -040044{
45 unsigned long num;
46 unsigned int nbits;
47 int ret;
48
49 if (!val)
50 goto out_inval;
Daniel Walter00cfaa92014-06-21 13:06:38 +010051 ret = kstrtoul(val, 0, &num);
Dan Carpenter1a54c0c2018-07-12 15:30:08 +030052 if (ret)
Trond Myklebust241269b2010-07-31 14:29:08 -040053 goto out_inval;
Frank Sorenson34ae6852016-06-27 15:17:19 -040054 nbits = fls(num - 1);
Trond Myklebust241269b2010-07-31 14:29:08 -040055 if (nbits > MAX_HASHTABLE_BITS || nbits < 2)
56 goto out_inval;
57 *(unsigned int *)kp->arg = nbits;
58 return 0;
59out_inval:
60 return -EINVAL;
61}
62
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100063static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp)
Trond Myklebust241269b2010-07-31 14:29:08 -040064{
65 unsigned int nbits;
66
67 nbits = *(unsigned int *)kp->arg;
68 return sprintf(buffer, "%u", 1U << nbits);
69}
70
71#define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
72
Luis R. Rodriguez9c278472015-05-27 11:09:38 +093073static const struct kernel_param_ops param_ops_hashtbl_sz = {
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100074 .set = param_set_hashtbl_sz,
75 .get = param_get_hashtbl_sz,
76};
77
Trond Myklebust241269b2010-07-31 14:29:08 -040078module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
79MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
80
Trond Myklebustbae67462014-07-21 13:32:42 -040081static unsigned long auth_max_cred_cachesize = ULONG_MAX;
82module_param(auth_max_cred_cachesize, ulong, 0644);
83MODULE_PARM_DESC(auth_max_cred_cachesize, "RPC credential maximum total cache size");
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static u32
86pseudoflavor_to_flavor(u32 flavor) {
Chuck Lever1c74a242013-03-22 12:53:08 -040087 if (flavor > RPC_AUTH_MAXFLAVOR)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return RPC_AUTH_GSS;
89 return flavor;
90}
91
92int
Trond Myklebustf1c0a862007-06-23 20:17:58 -040093rpcauth_register(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Trond Myklebust4e4c3be2018-09-27 13:12:44 -040095 const struct rpc_authops *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 rpc_authflavor_t flavor;
97
98 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
99 return -EINVAL;
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400100 old = cmpxchg((const struct rpc_authops ** __force)&auth_flavors[flavor], NULL, ops);
101 if (old == NULL || old == ops)
102 return 0;
103 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400105EXPORT_SYMBOL_GPL(rpcauth_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107int
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400108rpcauth_unregister(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400110 const struct rpc_authops *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 rpc_authflavor_t flavor;
112
113 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
114 return -EINVAL;
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400115
116 old = cmpxchg((const struct rpc_authops ** __force)&auth_flavors[flavor], ops, NULL);
117 if (old == ops || old == NULL)
118 return 0;
119 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400121EXPORT_SYMBOL_GPL(rpcauth_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400123static const struct rpc_authops *
124rpcauth_get_authops(rpc_authflavor_t flavor)
125{
126 const struct rpc_authops *ops;
127
128 if (flavor >= RPC_AUTH_MAXFLAVOR)
129 return NULL;
130
131 rcu_read_lock();
132 ops = rcu_dereference(auth_flavors[flavor]);
133 if (ops == NULL) {
134 rcu_read_unlock();
135 request_module("rpc-auth-%u", flavor);
136 rcu_read_lock();
137 ops = rcu_dereference(auth_flavors[flavor]);
138 if (ops == NULL)
139 goto out;
140 }
141 if (!try_module_get(ops->owner))
142 ops = NULL;
143out:
144 rcu_read_unlock();
145 return ops;
146}
147
148static void
149rpcauth_put_authops(const struct rpc_authops *ops)
150{
151 module_put(ops->owner);
152}
153
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400154/**
Chuck Lever9568c5e2013-03-16 15:54:43 -0400155 * rpcauth_get_pseudoflavor - check if security flavor is supported
156 * @flavor: a security flavor
157 * @info: a GSS mech OID, quality of protection, and service value
158 *
159 * Verifies that an appropriate kernel module is available or already loaded.
160 * Returns an equivalent pseudoflavor, or RPC_AUTH_MAXFLAVOR if "flavor" is
161 * not supported locally.
162 */
163rpc_authflavor_t
164rpcauth_get_pseudoflavor(rpc_authflavor_t flavor, struct rpcsec_gss_info *info)
165{
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400166 const struct rpc_authops *ops = rpcauth_get_authops(flavor);
Chuck Lever9568c5e2013-03-16 15:54:43 -0400167 rpc_authflavor_t pseudoflavor;
168
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400169 if (!ops)
Chuck Lever9568c5e2013-03-16 15:54:43 -0400170 return RPC_AUTH_MAXFLAVOR;
Chuck Lever9568c5e2013-03-16 15:54:43 -0400171 pseudoflavor = flavor;
172 if (ops->info2flavor != NULL)
173 pseudoflavor = ops->info2flavor(info);
174
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400175 rpcauth_put_authops(ops);
Chuck Lever9568c5e2013-03-16 15:54:43 -0400176 return pseudoflavor;
177}
178EXPORT_SYMBOL_GPL(rpcauth_get_pseudoflavor);
179
180/**
Chuck Levera77c8062013-03-16 15:55:10 -0400181 * rpcauth_get_gssinfo - find GSS tuple matching a GSS pseudoflavor
182 * @pseudoflavor: GSS pseudoflavor to match
183 * @info: rpcsec_gss_info structure to fill in
184 *
185 * Returns zero and fills in "info" if pseudoflavor matches a
186 * supported mechanism.
187 */
188int
189rpcauth_get_gssinfo(rpc_authflavor_t pseudoflavor, struct rpcsec_gss_info *info)
190{
191 rpc_authflavor_t flavor = pseudoflavor_to_flavor(pseudoflavor);
192 const struct rpc_authops *ops;
193 int result;
194
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400195 ops = rpcauth_get_authops(flavor);
Chuck Levera77c8062013-03-16 15:55:10 -0400196 if (ops == NULL)
Chuck Levera77c8062013-03-16 15:55:10 -0400197 return -ENOENT;
Chuck Levera77c8062013-03-16 15:55:10 -0400198
199 result = -ENOENT;
200 if (ops->flavor2info != NULL)
201 result = ops->flavor2info(pseudoflavor, info);
202
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400203 rpcauth_put_authops(ops);
Chuck Levera77c8062013-03-16 15:55:10 -0400204 return result;
205}
206EXPORT_SYMBOL_GPL(rpcauth_get_gssinfo);
207
208/**
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400209 * rpcauth_list_flavors - discover registered flavors and pseudoflavors
210 * @array: array to fill in
211 * @size: size of "array"
212 *
213 * Returns the number of array items filled in, or a negative errno.
214 *
215 * The returned array is not sorted by any policy. Callers should not
216 * rely on the order of the items in the returned array.
217 */
218int
219rpcauth_list_flavors(rpc_authflavor_t *array, int size)
220{
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400221 const struct rpc_authops *ops;
222 rpc_authflavor_t flavor, pseudos[4];
223 int i, len, result = 0;
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400224
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400225 rcu_read_lock();
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400226 for (flavor = 0; flavor < RPC_AUTH_MAXFLAVOR; flavor++) {
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400227 ops = rcu_dereference(auth_flavors[flavor]);
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400228 if (result >= size) {
229 result = -ENOMEM;
230 break;
231 }
232
233 if (ops == NULL)
234 continue;
235 if (ops->list_pseudoflavors == NULL) {
236 array[result++] = ops->au_flavor;
237 continue;
238 }
239 len = ops->list_pseudoflavors(pseudos, ARRAY_SIZE(pseudos));
240 if (len < 0) {
241 result = len;
242 break;
243 }
244 for (i = 0; i < len; i++) {
245 if (result >= size) {
246 result = -ENOMEM;
247 break;
248 }
249 array[result++] = pseudos[i];
250 }
251 }
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400252 rcu_read_unlock();
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400253
254 dprintk("RPC: %s returns %d\n", __func__, result);
255 return result;
256}
257EXPORT_SYMBOL_GPL(rpcauth_list_flavors);
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259struct rpc_auth *
Sargun Dhillon82b98ca2018-07-05 16:48:50 +0000260rpcauth_create(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400262 struct rpc_auth *auth = ERR_PTR(-EINVAL);
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400263 const struct rpc_authops *ops;
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400264 u32 flavor = pseudoflavor_to_flavor(args->pseudoflavor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400266 ops = rpcauth_get_authops(flavor);
267 if (ops == NULL)
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500268 goto out;
269
Trond Myklebustc2190662013-08-26 19:23:04 -0400270 auth = ops->create(args, clnt);
Trond Myklebust4e4c3be2018-09-27 13:12:44 -0400271
272 rpcauth_put_authops(ops);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000273 if (IS_ERR(auth))
274 return auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (clnt->cl_auth)
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400276 rpcauth_release(clnt->cl_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 clnt->cl_auth = auth;
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500278
279out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return auth;
281}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400282EXPORT_SYMBOL_GPL(rpcauth_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284void
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400285rpcauth_release(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Trond Myklebust331bc712018-10-14 10:40:29 -0400287 if (!refcount_dec_and_test(&auth->au_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return;
289 auth->au_ops->destroy(auth);
290}
291
292static DEFINE_SPINLOCK(rpc_credcache_lock);
293
Trond Myklebust95cd6232018-10-11 16:11:09 -0400294/*
295 * On success, the caller is responsible for freeing the reference
296 * held by the hashtable
297 */
298static bool
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400299rpcauth_unhash_cred_locked(struct rpc_cred *cred)
300{
Trond Myklebust95cd6232018-10-11 16:11:09 -0400301 if (!test_and_clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags))
302 return false;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400303 hlist_del_rcu(&cred->cr_hash);
Trond Myklebust95cd6232018-10-11 16:11:09 -0400304 return true;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400305}
306
Trond Myklebust95cd6232018-10-11 16:11:09 -0400307static bool
Trond Myklebust9499b432007-06-24 15:57:57 -0400308rpcauth_unhash_cred(struct rpc_cred *cred)
309{
310 spinlock_t *cache_lock;
Trond Myklebust95cd6232018-10-11 16:11:09 -0400311 bool ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400312
Trond Myklebust95cd6232018-10-11 16:11:09 -0400313 if (!test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags))
314 return false;
Trond Myklebust9499b432007-06-24 15:57:57 -0400315 cache_lock = &cred->cr_auth->au_credcache->lock;
316 spin_lock(cache_lock);
Trond Myklebust95cd6232018-10-11 16:11:09 -0400317 ret = rpcauth_unhash_cred_locked(cred);
Trond Myklebust9499b432007-06-24 15:57:57 -0400318 spin_unlock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500319 return ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/*
323 * Initialize RPC credential cache
324 */
325int
Trond Myklebustf5c21872007-06-25 17:11:20 -0400326rpcauth_init_credcache(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 struct rpc_cred_cache *new;
Trond Myklebust988664a2010-07-31 14:29:07 -0400329 unsigned int hashsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800331 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (!new)
Trond Myklebust241269b2010-07-31 14:29:08 -0400333 goto out_nocache;
334 new->hashbits = auth_hashbits;
Trond Myklebust988664a2010-07-31 14:29:07 -0400335 hashsize = 1U << new->hashbits;
Trond Myklebust241269b2010-07-31 14:29:08 -0400336 new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
337 if (!new->hashtable)
338 goto out_nohashtbl;
Trond Myklebust9499b432007-06-24 15:57:57 -0400339 spin_lock_init(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 auth->au_credcache = new;
341 return 0;
Trond Myklebust241269b2010-07-31 14:29:08 -0400342out_nohashtbl:
343 kfree(new);
344out_nocache:
345 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400347EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349/*
Andy Adamson4de6caa2013-08-14 11:59:15 -0400350 * Setup a credential key lifetime timeout notification
351 */
352int
353rpcauth_key_timeout_notify(struct rpc_auth *auth, struct rpc_cred *cred)
354{
355 if (!cred->cr_auth->au_ops->key_timeout)
356 return 0;
357 return cred->cr_auth->au_ops->key_timeout(auth, cred);
358}
359EXPORT_SYMBOL_GPL(rpcauth_key_timeout_notify);
360
361bool
Scott Mayhewce52914e2016-06-07 15:14:48 -0400362rpcauth_cred_key_to_expire(struct rpc_auth *auth, struct rpc_cred *cred)
Andy Adamson4de6caa2013-08-14 11:59:15 -0400363{
Scott Mayhewce52914e2016-06-07 15:14:48 -0400364 if (auth->au_flags & RPCAUTH_AUTH_NO_CRKEY_TIMEOUT)
365 return false;
Andy Adamson4de6caa2013-08-14 11:59:15 -0400366 if (!cred->cr_ops->crkey_to_expire)
367 return false;
368 return cred->cr_ops->crkey_to_expire(cred);
369}
370EXPORT_SYMBOL_GPL(rpcauth_cred_key_to_expire);
371
Jeff Laytona0337d12014-06-21 20:52:16 -0400372char *
373rpcauth_stringify_acceptor(struct rpc_cred *cred)
374{
375 if (!cred->cr_ops->crstringify_acceptor)
376 return NULL;
377 return cred->cr_ops->crstringify_acceptor(cred);
378}
379EXPORT_SYMBOL_GPL(rpcauth_stringify_acceptor);
380
Andy Adamson4de6caa2013-08-14 11:59:15 -0400381/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 * Destroy a list of credentials
383 */
384static inline
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400385void rpcauth_destroy_credlist(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 struct rpc_cred *cred;
388
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400389 while (!list_empty(head)) {
390 cred = list_entry(head->next, struct rpc_cred, cr_lru);
391 list_del_init(&cred->cr_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 put_rpccred(cred);
393 }
394}
395
Trond Myklebust95cd6232018-10-11 16:11:09 -0400396static void
397rpcauth_lru_add_locked(struct rpc_cred *cred)
398{
399 if (!list_empty(&cred->cr_lru))
400 return;
401 number_cred_unused++;
402 list_add_tail(&cred->cr_lru, &cred_unused);
403}
404
405static void
406rpcauth_lru_add(struct rpc_cred *cred)
407{
408 if (!list_empty(&cred->cr_lru))
409 return;
410 spin_lock(&rpc_credcache_lock);
411 rpcauth_lru_add_locked(cred);
412 spin_unlock(&rpc_credcache_lock);
413}
414
415static void
416rpcauth_lru_remove_locked(struct rpc_cred *cred)
417{
418 if (list_empty(&cred->cr_lru))
419 return;
420 number_cred_unused--;
421 list_del_init(&cred->cr_lru);
422}
423
424static void
425rpcauth_lru_remove(struct rpc_cred *cred)
426{
427 if (list_empty(&cred->cr_lru))
428 return;
429 spin_lock(&rpc_credcache_lock);
430 rpcauth_lru_remove_locked(cred);
431 spin_unlock(&rpc_credcache_lock);
432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
435 * Clear the RPC credential cache, and delete those credentials
436 * that are not referenced.
437 */
438void
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400439rpcauth_clear_credcache(struct rpc_cred_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400441 LIST_HEAD(free);
442 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct rpc_cred *cred;
Trond Myklebust988664a2010-07-31 14:29:07 -0400444 unsigned int hashsize = 1U << cache->hashbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 int i;
446
447 spin_lock(&rpc_credcache_lock);
Trond Myklebust9499b432007-06-24 15:57:57 -0400448 spin_lock(&cache->lock);
Trond Myklebust988664a2010-07-31 14:29:07 -0400449 for (i = 0; i < hashsize; i++) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400450 head = &cache->hashtable[i];
451 while (!hlist_empty(head)) {
452 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400453 rpcauth_unhash_cred_locked(cred);
Trond Myklebust95cd6232018-10-11 16:11:09 -0400454 /* Note: We now hold a reference to cred */
455 rpcauth_lru_remove_locked(cred);
456 list_add_tail(&cred->cr_lru, &free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 }
Trond Myklebust9499b432007-06-24 15:57:57 -0400459 spin_unlock(&cache->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 spin_unlock(&rpc_credcache_lock);
461 rpcauth_destroy_credlist(&free);
462}
463
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400464/*
465 * Destroy the RPC credential cache
466 */
467void
468rpcauth_destroy_credcache(struct rpc_auth *auth)
469{
470 struct rpc_cred_cache *cache = auth->au_credcache;
471
472 if (cache) {
473 auth->au_credcache = NULL;
474 rpcauth_clear_credcache(cache);
Trond Myklebust241269b2010-07-31 14:29:08 -0400475 kfree(cache->hashtable);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400476 kfree(cache);
477 }
478}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400479EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400480
Trond Myklebustd2b83142008-04-14 18:13:37 -0400481
482#define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/*
485 * Remove stale credentials. Avoid sleeping inside the loop.
486 */
Dave Chinner70534a72013-08-28 10:18:14 +1000487static long
Trond Myklebustf5c21872007-06-25 17:11:20 -0400488rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Trond Myklebusteac0d182008-10-28 15:21:41 -0400490 struct rpc_cred *cred, *next;
Trond Myklebustd2b83142008-04-14 18:13:37 -0400491 unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
Dave Chinner70534a72013-08-28 10:18:14 +1000492 long freed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Trond Myklebusteac0d182008-10-28 15:21:41 -0400494 list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
495
Trond Myklebust20673402010-05-13 12:51:06 -0400496 if (nr_to_scan-- == 0)
497 break;
Trond Myklebust79b18182018-10-14 10:34:31 -0400498 if (refcount_read(&cred->cr_count) > 1) {
Trond Myklebust95cd6232018-10-11 16:11:09 -0400499 rpcauth_lru_remove_locked(cred);
500 continue;
501 }
Trond Myklebust93a05e62010-05-13 12:51:06 -0400502 /*
503 * Enforce a 60 second garbage collection moratorium
504 * Note that the cred_unused list must be time-ordered.
505 */
Trond Myklebust95cd6232018-10-11 16:11:09 -0400506 if (!time_in_range(cred->cr_expire, expired, jiffies))
507 continue;
508 if (!rpcauth_unhash_cred(cred))
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400509 continue;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400510
Trond Myklebust95cd6232018-10-11 16:11:09 -0400511 rpcauth_lru_remove_locked(cred);
512 freed++;
513 list_add_tail(&cred->cr_lru, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Trond Myklebust95cd6232018-10-11 16:11:09 -0400515 return freed ? freed : SHRINK_STOP;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400516}
517
Trond Myklebustbae67462014-07-21 13:32:42 -0400518static unsigned long
519rpcauth_cache_do_shrink(int nr_to_scan)
520{
521 LIST_HEAD(free);
522 unsigned long freed;
523
524 spin_lock(&rpc_credcache_lock);
525 freed = rpcauth_prune_expired(&free, nr_to_scan);
526 spin_unlock(&rpc_credcache_lock);
527 rpcauth_destroy_credlist(&free);
528
529 return freed;
530}
531
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400532/*
Trond Myklebustf5c21872007-06-25 17:11:20 -0400533 * Run memory cache shrinker.
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400534 */
Dave Chinner70534a72013-08-28 10:18:14 +1000535static unsigned long
536rpcauth_cache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
537
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400538{
Dave Chinner70534a72013-08-28 10:18:14 +1000539 if ((sc->gfp_mask & GFP_KERNEL) != GFP_KERNEL)
540 return SHRINK_STOP;
541
542 /* nothing left, don't come back */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400543 if (list_empty(&cred_unused))
Dave Chinner70534a72013-08-28 10:18:14 +1000544 return SHRINK_STOP;
545
Trond Myklebustbae67462014-07-21 13:32:42 -0400546 return rpcauth_cache_do_shrink(sc->nr_to_scan);
Dave Chinner70534a72013-08-28 10:18:14 +1000547}
548
549static unsigned long
550rpcauth_cache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
551
552{
NeilBrown4c3ffd02017-01-06 14:19:58 +1100553 return number_cred_unused * sysctl_vfs_cache_pressure / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Trond Myklebustbae67462014-07-21 13:32:42 -0400556static void
557rpcauth_cache_enforce_limit(void)
558{
559 unsigned long diff;
560 unsigned int nr_to_scan;
561
562 if (number_cred_unused <= auth_max_cred_cachesize)
563 return;
564 diff = number_cred_unused - auth_max_cred_cachesize;
565 nr_to_scan = 100;
566 if (diff < nr_to_scan)
567 nr_to_scan = diff;
568 rpcauth_cache_do_shrink(nr_to_scan);
569}
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/*
572 * Look up a process' credentials in the authentication cache
573 */
574struct rpc_cred *
575rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Jeff Layton3c6e0bc2016-04-21 20:51:54 -0400576 int flags, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400578 LIST_HEAD(free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct rpc_cred_cache *cache = auth->au_credcache;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400580 struct rpc_cred *cred = NULL,
581 *entry, *new;
Trond Myklebust25337fd2008-03-12 14:40:14 -0400582 unsigned int nr;
583
Frank Sorenson66cbd4b2016-09-29 10:44:41 -0500584 nr = auth->au_ops->hash_cred(acred, cache->hashbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400586 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800587 hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400588 if (!entry->cr_ops->crmatch(acred, entry, flags))
589 continue;
NeilBrownbd956082014-07-14 11:28:20 +1000590 if (flags & RPCAUTH_LOOKUP_RCU) {
Trond Myklebust07d02a62018-10-12 13:28:26 -0400591 if (test_bit(RPCAUTH_CRED_NEW, &entry->cr_flags) ||
Trond Myklebust79b18182018-10-14 10:34:31 -0400592 refcount_read(&entry->cr_count) == 0)
Trond Myklebust07d02a62018-10-12 13:28:26 -0400593 continue;
594 cred = entry;
NeilBrownbd956082014-07-14 11:28:20 +1000595 break;
596 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400597 cred = get_rpccred(entry);
Trond Myklebust07d02a62018-10-12 13:28:26 -0400598 if (cred)
599 break;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400600 }
601 rcu_read_unlock();
602
Trond Myklebust9499b432007-06-24 15:57:57 -0400603 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400604 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400605
NeilBrownbd956082014-07-14 11:28:20 +1000606 if (flags & RPCAUTH_LOOKUP_RCU)
607 return ERR_PTR(-ECHILD);
608
Jeff Layton3c6e0bc2016-04-21 20:51:54 -0400609 new = auth->au_ops->crcreate(auth, acred, flags, gfp);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400610 if (IS_ERR(new)) {
611 cred = new;
612 goto out;
613 }
614
Trond Myklebust9499b432007-06-24 15:57:57 -0400615 spin_lock(&cache->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800616 hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400617 if (!entry->cr_ops->crmatch(acred, entry, flags))
618 continue;
619 cred = get_rpccred(entry);
Trond Myklebust07d02a62018-10-12 13:28:26 -0400620 if (cred)
621 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400623 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400624 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400625 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
Trond Myklebust79b18182018-10-14 10:34:31 -0400626 refcount_inc(&cred->cr_count);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400627 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
628 } else
629 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400630 spin_unlock(&cache->lock);
Trond Myklebustbae67462014-07-21 13:32:42 -0400631 rpcauth_cache_enforce_limit();
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400632found:
Joe Perchesf64f9e72009-11-29 16:55:45 -0800633 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
634 cred->cr_ops->cr_init != NULL &&
635 !(flags & RPCAUTH_LOOKUP_NEW)) {
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500636 int res = cred->cr_ops->cr_init(auth, cred);
637 if (res < 0) {
638 put_rpccred(cred);
639 cred = ERR_PTR(res);
640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400642 rpcauth_destroy_credlist(&free);
643out:
644 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400646EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500649rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
David Howells86a264a2008-11-14 10:39:18 +1100651 struct auth_cred acred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct rpc_cred *ret;
David Howells86a264a2008-11-14 10:39:18 +1100653 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Chuck Lever46121cf2007-01-31 12:14:08 -0500655 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 auth->au_ops->au_name);
David Howells86a264a2008-11-14 10:39:18 +1100657
658 memset(&acred, 0, sizeof(acred));
NeilBrown97f68c6b2018-12-03 11:30:30 +1100659 acred.cred = cred;
Trond Myklebust8a317762006-02-01 12:18:36 -0500660 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return ret;
662}
Andy Adamson66b06862014-06-12 15:02:32 -0400663EXPORT_SYMBOL_GPL(rpcauth_lookupcred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Trond Myklebust5fe47552007-06-23 19:55:31 -0400665void
666rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
667 struct rpc_auth *auth, const struct rpc_credops *ops)
668{
669 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400670 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust79b18182018-10-14 10:34:31 -0400671 refcount_set(&cred->cr_count, 1);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400672 cred->cr_auth = auth;
673 cred->cr_ops = ops;
674 cred->cr_expire = jiffies;
NeilBrown97f68c6b2018-12-03 11:30:30 +1100675 cred->cr_cred = get_cred(acred->cred);
NeilBrown8276c902018-12-03 11:30:30 +1100676 cred->cr_uid = acred->cred->fsuid;
Trond Myklebust5fe47552007-06-23 19:55:31 -0400677}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400678EXPORT_SYMBOL_GPL(rpcauth_init_cred);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400679
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400680struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400681rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400682{
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400683 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
684 cred->cr_auth->au_ops->au_name, cred);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400685 return get_rpccred(cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400686}
Trond Myklebust5c691042008-03-12 16:21:07 -0400687EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400688
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400689static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400690rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400692 struct rpc_auth *auth = task->tk_client->cl_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct auth_cred acred = {
NeilBrown97f68c6b2018-12-03 11:30:30 +1100694 .cred = get_task_cred(&init_task),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 };
NeilBrown97f68c6b2018-12-03 11:30:30 +1100696 struct rpc_cred *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Chuck Lever46121cf2007-01-31 12:14:08 -0500698 dprintk("RPC: %5u looking up %s cred\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400699 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
NeilBrown97f68c6b2018-12-03 11:30:30 +1100700 ret = auth->au_ops->lookup_cred(auth, &acred, lookupflags);
701 put_cred(acred.cred);
702 return ret;
Trond Myklebustaf093832008-03-12 12:12:16 -0400703}
704
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400705static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400706rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
Trond Myklebustaf093832008-03-12 12:12:16 -0400707{
708 struct rpc_auth *auth = task->tk_client->cl_auth;
Trond Myklebustaf093832008-03-12 12:12:16 -0400709
710 dprintk("RPC: %5u looking up %s cred\n",
711 task->tk_pid, auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400712 return rpcauth_lookupcred(auth, lookupflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Trond Myklebusta17c2152010-07-31 14:29:08 -0400715static int
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400716rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400718 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400719 struct rpc_cred *new;
Trond Myklebust5d351752009-09-15 13:32:13 -0400720 int lookupflags = 0;
721
722 if (flags & RPC_TASK_ASYNC)
723 lookupflags |= RPCAUTH_LOOKUP_NEW;
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400724 if (cred != NULL)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400725 new = cred->cr_ops->crbind(task, cred, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400726 else if (flags & RPC_TASK_ROOTCREDS)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400727 new = rpcauth_bind_root_cred(task, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400728 else
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400729 new = rpcauth_bind_new_cred(task, lookupflags);
730 if (IS_ERR(new))
731 return PTR_ERR(new);
Trond Myklebust9a8f6b52016-05-16 17:42:42 -0400732 put_rpccred(req->rq_cred);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400733 req->rq_cred = new;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400734 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737void
738put_rpccred(struct rpc_cred *cred)
739{
Trond Myklebust9a8f6b52016-05-16 17:42:42 -0400740 if (cred == NULL)
741 return;
Trond Myklebust95cd6232018-10-11 16:11:09 -0400742 rcu_read_lock();
Trond Myklebust79b18182018-10-14 10:34:31 -0400743 if (refcount_dec_and_test(&cred->cr_count))
Trond Myklebust95cd6232018-10-11 16:11:09 -0400744 goto destroy;
Trond Myklebust79b18182018-10-14 10:34:31 -0400745 if (refcount_read(&cred->cr_count) != 1 ||
Trond Myklebust95cd6232018-10-11 16:11:09 -0400746 !test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags))
747 goto out;
748 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
749 cred->cr_expire = jiffies;
750 rpcauth_lru_add(cred);
751 /* Race breaker */
752 if (unlikely(!test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags)))
753 rpcauth_lru_remove(cred);
754 } else if (rpcauth_unhash_cred(cred)) {
755 rpcauth_lru_remove(cred);
Trond Myklebust79b18182018-10-14 10:34:31 -0400756 if (refcount_dec_and_test(&cred->cr_count))
Trond Myklebust95cd6232018-10-11 16:11:09 -0400757 goto destroy;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500758 }
Trond Myklebust95cd6232018-10-11 16:11:09 -0400759out:
760 rcu_read_unlock();
Trond Myklebustf0380f32009-12-03 08:10:17 -0500761 return;
Trond Myklebust95cd6232018-10-11 16:11:09 -0400762destroy:
763 rcu_read_unlock();
764 cred->cr_ops->crdestroy(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400766EXPORT_SYMBOL_GPL(put_rpccred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700768__be32 *
769rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400771 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Chuck Lever46121cf2007-01-31 12:14:08 -0500773 dprintk("RPC: %5u marshaling %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400774 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return cred->cr_ops->crmarshal(task, p);
777}
778
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700779__be32 *
780rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400782 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Chuck Lever46121cf2007-01-31 12:14:08 -0500784 dprintk("RPC: %5u validating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400785 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return cred->cr_ops->crvalidate(task, p);
788}
789
Chuck Lever9f06c712010-12-14 14:59:18 +0000790static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
791 __be32 *data, void *obj)
792{
793 struct xdr_stream xdr;
794
795 xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
796 encode(rqstp, &xdr, obj);
797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799int
Chuck Lever9f06c712010-12-14 14:59:18 +0000800rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700801 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400803 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Chuck Lever46121cf2007-01-31 12:14:08 -0500805 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 task->tk_pid, cred->cr_ops->cr_name, cred);
807 if (cred->cr_ops->crwrap_req)
808 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
809 /* By default, we encode the arguments normally. */
Chuck Lever9f06c712010-12-14 14:59:18 +0000810 rpcauth_wrap_req_encode(encode, rqstp, data, obj);
811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Chuck Leverbf269552010-12-14 14:59:29 +0000814static int
815rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
816 __be32 *data, void *obj)
817{
818 struct xdr_stream xdr;
819
820 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
821 return decode(rqstp, &xdr, obj);
822}
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824int
Chuck Leverbf269552010-12-14 14:59:29 +0000825rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700826 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400828 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Chuck Lever46121cf2007-01-31 12:14:08 -0500830 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 task->tk_pid, cred->cr_ops->cr_name, cred);
832 if (cred->cr_ops->crunwrap_resp)
833 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
834 data, obj);
835 /* By default, we decode the arguments normally. */
Chuck Leverbf269552010-12-14 14:59:29 +0000836 return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Trond Myklebust3021a5bb2018-08-14 13:50:21 -0400839bool
840rpcauth_xmit_need_reencode(struct rpc_task *task)
841{
842 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
843
844 if (!cred || !cred->cr_ops->crneed_reencode)
845 return false;
846 return cred->cr_ops->crneed_reencode(task);
847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849int
850rpcauth_refreshcred(struct rpc_task *task)
851{
Trond Myklebust9a84d382010-10-24 18:00:46 -0400852 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 int err;
854
Trond Myklebusta17c2152010-07-31 14:29:08 -0400855 cred = task->tk_rqstp->rq_cred;
856 if (cred == NULL) {
857 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
858 if (err < 0)
859 goto out;
860 cred = task->tk_rqstp->rq_cred;
Joe Perchesf81c6222011-06-03 11:51:19 +0000861 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500862 dprintk("RPC: %5u refreshing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400863 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 err = cred->cr_ops->crrefresh(task);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400866out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (err < 0)
868 task->tk_status = err;
869 return err;
870}
871
872void
873rpcauth_invalcred(struct rpc_task *task)
874{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400875 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400876
Chuck Lever46121cf2007-01-31 12:14:08 -0500877 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400878 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400879 if (cred)
880 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883int
884rpcauth_uptodatecred(struct rpc_task *task)
885{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400886 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400887
888 return cred == NULL ||
889 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400891
Rusty Russell8e1f9362007-07-17 04:03:17 -0700892static struct shrinker rpc_cred_shrinker = {
Dave Chinner70534a72013-08-28 10:18:14 +1000893 .count_objects = rpcauth_cache_shrink_count,
894 .scan_objects = rpcauth_cache_shrink_scan,
Rusty Russell8e1f9362007-07-17 04:03:17 -0700895 .seeks = DEFAULT_SEEKS,
896};
Trond Myklebustf5c21872007-06-25 17:11:20 -0400897
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400898int __init rpcauth_init_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400899{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400900 int err;
901
902 err = rpc_init_authunix();
903 if (err < 0)
904 goto out1;
905 err = rpc_init_generic_auth();
906 if (err < 0)
907 goto out2;
Kinglong Mee28644862017-02-07 21:46:39 +0800908 err = register_shrinker(&rpc_cred_shrinker);
909 if (err < 0)
910 goto out3;
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400911 return 0;
Kinglong Mee28644862017-02-07 21:46:39 +0800912out3:
913 rpc_destroy_generic_auth();
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400914out2:
915 rpc_destroy_authunix();
916out1:
917 return err;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400918}
919
Stephen Rothwellc135e842010-09-29 14:16:57 +1000920void rpcauth_remove_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400921{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400922 rpc_destroy_authunix();
923 rpc_destroy_generic_auth();
Rusty Russell8e1f9362007-07-17 04:03:17 -0700924 unregister_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400925}