blob: c1576b1109746f4b842fb0bafc5c9584dc27b768 [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{
287 if (!atomic_dec_and_test(&auth->au_count))
288 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 Myklebust95cd6232018-10-11 16:11:09 -0400498 if (atomic_read(&cred->cr_count) > 1) {
499 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) {
591 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) &&
592 !test_bit(RPCAUTH_CRED_NEW, &entry->cr_flags))
593 cred = entry;
594 break;
595 }
Trond Myklebust9499b432007-06-24 15:57:57 -0400596 spin_lock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400597 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
Trond Myklebust9499b432007-06-24 15:57:57 -0400598 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400599 continue;
600 }
601 cred = get_rpccred(entry);
Trond Myklebust9499b432007-06-24 15:57:57 -0400602 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400603 break;
604 }
605 rcu_read_unlock();
606
Trond Myklebust9499b432007-06-24 15:57:57 -0400607 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400608 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400609
NeilBrownbd956082014-07-14 11:28:20 +1000610 if (flags & RPCAUTH_LOOKUP_RCU)
611 return ERR_PTR(-ECHILD);
612
Jeff Layton3c6e0bc2016-04-21 20:51:54 -0400613 new = auth->au_ops->crcreate(auth, acred, flags, gfp);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400614 if (IS_ERR(new)) {
615 cred = new;
616 goto out;
617 }
618
Trond Myklebust9499b432007-06-24 15:57:57 -0400619 spin_lock(&cache->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800620 hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400621 if (!entry->cr_ops->crmatch(acred, entry, flags))
622 continue;
623 cred = get_rpccred(entry);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400624 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400626 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400627 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400628 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
Trond Myklebust95cd6232018-10-11 16:11:09 -0400629 atomic_inc(&cred->cr_count);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400630 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
631 } else
632 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400633 spin_unlock(&cache->lock);
Trond Myklebustbae67462014-07-21 13:32:42 -0400634 rpcauth_cache_enforce_limit();
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400635found:
Joe Perchesf64f9e72009-11-29 16:55:45 -0800636 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
637 cred->cr_ops->cr_init != NULL &&
638 !(flags & RPCAUTH_LOOKUP_NEW)) {
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500639 int res = cred->cr_ops->cr_init(auth, cred);
640 if (res < 0) {
641 put_rpccred(cred);
642 cred = ERR_PTR(res);
643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400645 rpcauth_destroy_credlist(&free);
646out:
647 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400649EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500652rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
David Howells86a264a2008-11-14 10:39:18 +1100654 struct auth_cred acred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 struct rpc_cred *ret;
David Howells86a264a2008-11-14 10:39:18 +1100656 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Chuck Lever46121cf2007-01-31 12:14:08 -0500658 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 auth->au_ops->au_name);
David Howells86a264a2008-11-14 10:39:18 +1100660
661 memset(&acred, 0, sizeof(acred));
662 acred.uid = cred->fsuid;
663 acred.gid = cred->fsgid;
NeilBrown122a8cd2014-08-04 16:24:00 +1000664 acred.group_info = cred->group_info;
Trond Myklebust8a317762006-02-01 12:18:36 -0500665 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return ret;
667}
Andy Adamson66b06862014-06-12 15:02:32 -0400668EXPORT_SYMBOL_GPL(rpcauth_lookupcred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Trond Myklebust5fe47552007-06-23 19:55:31 -0400670void
671rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
672 struct rpc_auth *auth, const struct rpc_credops *ops)
673{
674 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400675 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400676 atomic_set(&cred->cr_count, 1);
677 cred->cr_auth = auth;
678 cred->cr_ops = ops;
679 cred->cr_expire = jiffies;
Trond Myklebust5fe47552007-06-23 19:55:31 -0400680 cred->cr_uid = acred->uid;
681}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400682EXPORT_SYMBOL_GPL(rpcauth_init_cred);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400683
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400684struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400685rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400686{
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400687 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
688 cred->cr_auth->au_ops->au_name, cred);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400689 return get_rpccred(cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400690}
Trond Myklebust5c691042008-03-12 16:21:07 -0400691EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400692
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400693static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400694rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400696 struct rpc_auth *auth = task->tk_client->cl_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct auth_cred acred = {
Eric W. Biedermanbf37f792013-02-01 15:55:38 -0800698 .uid = GLOBAL_ROOT_UID,
699 .gid = GLOBAL_ROOT_GID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Chuck Lever46121cf2007-01-31 12:14:08 -0500702 dprintk("RPC: %5u looking up %s cred\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400703 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400704 return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
Trond Myklebustaf093832008-03-12 12:12:16 -0400705}
706
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400707static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400708rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
Trond Myklebustaf093832008-03-12 12:12:16 -0400709{
710 struct rpc_auth *auth = task->tk_client->cl_auth;
Trond Myklebustaf093832008-03-12 12:12:16 -0400711
712 dprintk("RPC: %5u looking up %s cred\n",
713 task->tk_pid, auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400714 return rpcauth_lookupcred(auth, lookupflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Trond Myklebusta17c2152010-07-31 14:29:08 -0400717static int
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400718rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400720 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400721 struct rpc_cred *new;
Trond Myklebust5d351752009-09-15 13:32:13 -0400722 int lookupflags = 0;
723
724 if (flags & RPC_TASK_ASYNC)
725 lookupflags |= RPCAUTH_LOOKUP_NEW;
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400726 if (cred != NULL)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400727 new = cred->cr_ops->crbind(task, cred, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400728 else if (flags & RPC_TASK_ROOTCREDS)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400729 new = rpcauth_bind_root_cred(task, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400730 else
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400731 new = rpcauth_bind_new_cred(task, lookupflags);
732 if (IS_ERR(new))
733 return PTR_ERR(new);
Trond Myklebust9a8f6b52016-05-16 17:42:42 -0400734 put_rpccred(req->rq_cred);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400735 req->rq_cred = new;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739void
740put_rpccred(struct rpc_cred *cred)
741{
Trond Myklebust9a8f6b52016-05-16 17:42:42 -0400742 if (cred == NULL)
743 return;
Trond Myklebust95cd6232018-10-11 16:11:09 -0400744 rcu_read_lock();
745 if (atomic_dec_and_test(&cred->cr_count))
746 goto destroy;
747 if (atomic_read(&cred->cr_count) != 1 ||
748 !test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags))
749 goto out;
750 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
751 cred->cr_expire = jiffies;
752 rpcauth_lru_add(cred);
753 /* Race breaker */
754 if (unlikely(!test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags)))
755 rpcauth_lru_remove(cred);
756 } else if (rpcauth_unhash_cred(cred)) {
757 rpcauth_lru_remove(cred);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500758 if (atomic_dec_and_test(&cred->cr_count))
Trond Myklebust95cd6232018-10-11 16:11:09 -0400759 goto destroy;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500760 }
Trond Myklebust95cd6232018-10-11 16:11:09 -0400761out:
762 rcu_read_unlock();
Trond Myklebustf0380f32009-12-03 08:10:17 -0500763 return;
Trond Myklebust95cd6232018-10-11 16:11:09 -0400764destroy:
765 rcu_read_unlock();
766 cred->cr_ops->crdestroy(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400768EXPORT_SYMBOL_GPL(put_rpccred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700770__be32 *
771rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400773 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Chuck Lever46121cf2007-01-31 12:14:08 -0500775 dprintk("RPC: %5u marshaling %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400776 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return cred->cr_ops->crmarshal(task, p);
779}
780
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700781__be32 *
782rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400784 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Chuck Lever46121cf2007-01-31 12:14:08 -0500786 dprintk("RPC: %5u validating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400787 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return cred->cr_ops->crvalidate(task, p);
790}
791
Chuck Lever9f06c712010-12-14 14:59:18 +0000792static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
793 __be32 *data, void *obj)
794{
795 struct xdr_stream xdr;
796
797 xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
798 encode(rqstp, &xdr, obj);
799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801int
Chuck Lever9f06c712010-12-14 14:59:18 +0000802rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700803 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400805 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Chuck Lever46121cf2007-01-31 12:14:08 -0500807 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 task->tk_pid, cred->cr_ops->cr_name, cred);
809 if (cred->cr_ops->crwrap_req)
810 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
811 /* By default, we encode the arguments normally. */
Chuck Lever9f06c712010-12-14 14:59:18 +0000812 rpcauth_wrap_req_encode(encode, rqstp, data, obj);
813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Chuck Leverbf269552010-12-14 14:59:29 +0000816static int
817rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
818 __be32 *data, void *obj)
819{
820 struct xdr_stream xdr;
821
822 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
823 return decode(rqstp, &xdr, obj);
824}
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826int
Chuck Leverbf269552010-12-14 14:59:29 +0000827rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700828 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400830 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Chuck Lever46121cf2007-01-31 12:14:08 -0500832 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 task->tk_pid, cred->cr_ops->cr_name, cred);
834 if (cred->cr_ops->crunwrap_resp)
835 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
836 data, obj);
837 /* By default, we decode the arguments normally. */
Chuck Leverbf269552010-12-14 14:59:29 +0000838 return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Trond Myklebust3021a5bb2018-08-14 13:50:21 -0400841bool
842rpcauth_xmit_need_reencode(struct rpc_task *task)
843{
844 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
845
846 if (!cred || !cred->cr_ops->crneed_reencode)
847 return false;
848 return cred->cr_ops->crneed_reencode(task);
849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851int
852rpcauth_refreshcred(struct rpc_task *task)
853{
Trond Myklebust9a84d382010-10-24 18:00:46 -0400854 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 int err;
856
Trond Myklebusta17c2152010-07-31 14:29:08 -0400857 cred = task->tk_rqstp->rq_cred;
858 if (cred == NULL) {
859 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
860 if (err < 0)
861 goto out;
862 cred = task->tk_rqstp->rq_cred;
Joe Perchesf81c6222011-06-03 11:51:19 +0000863 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500864 dprintk("RPC: %5u refreshing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400865 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 err = cred->cr_ops->crrefresh(task);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400868out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (err < 0)
870 task->tk_status = err;
871 return err;
872}
873
874void
875rpcauth_invalcred(struct rpc_task *task)
876{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400877 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400878
Chuck Lever46121cf2007-01-31 12:14:08 -0500879 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400880 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400881 if (cred)
882 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885int
886rpcauth_uptodatecred(struct rpc_task *task)
887{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400888 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400889
890 return cred == NULL ||
891 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400893
Rusty Russell8e1f9362007-07-17 04:03:17 -0700894static struct shrinker rpc_cred_shrinker = {
Dave Chinner70534a72013-08-28 10:18:14 +1000895 .count_objects = rpcauth_cache_shrink_count,
896 .scan_objects = rpcauth_cache_shrink_scan,
Rusty Russell8e1f9362007-07-17 04:03:17 -0700897 .seeks = DEFAULT_SEEKS,
898};
Trond Myklebustf5c21872007-06-25 17:11:20 -0400899
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400900int __init rpcauth_init_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400901{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400902 int err;
903
904 err = rpc_init_authunix();
905 if (err < 0)
906 goto out1;
907 err = rpc_init_generic_auth();
908 if (err < 0)
909 goto out2;
Kinglong Mee28644862017-02-07 21:46:39 +0800910 err = register_shrinker(&rpc_cred_shrinker);
911 if (err < 0)
912 goto out3;
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400913 return 0;
Kinglong Mee28644862017-02-07 21:46:39 +0800914out3:
915 rpc_destroy_generic_auth();
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400916out2:
917 rpc_destroy_authunix();
918out1:
919 return err;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400920}
921
Stephen Rothwellc135e842010-09-29 14:16:57 +1000922void rpcauth_remove_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400923{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400924 rpc_destroy_authunix();
925 rpc_destroy_generic_auth();
Rusty Russell8e1f9362007-07-17 04:03:17 -0700926 unregister_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400927}