blob: 2bc7bb82b16260bc66a35e897f50a3baaad96857 [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>
11#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/errno.h>
Trond Myklebust25337fd2008-03-12 14:40:14 -040014#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sunrpc/clnt.h>
Chuck Lever6a1a1e32012-07-11 16:31:08 -040016#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/spinlock.h>
18
19#ifdef RPC_DEBUG
20# define RPCDBG_FACILITY RPCDBG_AUTH
21#endif
22
Trond Myklebust241269b2010-07-31 14:29:08 -040023#define RPC_CREDCACHE_DEFAULT_HASHBITS (4)
24struct rpc_cred_cache {
25 struct hlist_head *hashtable;
26 unsigned int hashbits;
27 spinlock_t lock;
28};
29
30static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS;
31
Trond Myklebustfc1b3562007-06-09 16:15:46 -040032static DEFINE_SPINLOCK(rpc_authflavor_lock);
Trond Myklebustf1c0a862007-06-23 20:17:58 -040033static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 &authnull_ops, /* AUTH_NULL */
35 &authunix_ops, /* AUTH_UNIX */
36 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);
Trond Myklebust241269b2010-07-31 14:29:08 -040052 if (ret == -EINVAL)
53 goto out_inval;
54 nbits = fls(num);
55 if (num > (1U << nbits))
56 nbits++;
57 if (nbits > MAX_HASHTABLE_BITS || nbits < 2)
58 goto out_inval;
59 *(unsigned int *)kp->arg = nbits;
60 return 0;
61out_inval:
62 return -EINVAL;
63}
64
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100065static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp)
Trond Myklebust241269b2010-07-31 14:29:08 -040066{
67 unsigned int nbits;
68
69 nbits = *(unsigned int *)kp->arg;
70 return sprintf(buffer, "%u", 1U << nbits);
71}
72
73#define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
74
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100075static struct kernel_param_ops param_ops_hashtbl_sz = {
76 .set = param_set_hashtbl_sz,
77 .get = param_get_hashtbl_sz,
78};
79
Trond Myklebust241269b2010-07-31 14:29:08 -040080module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
81MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static u32
84pseudoflavor_to_flavor(u32 flavor) {
Chuck Lever1c74a242013-03-22 12:53:08 -040085 if (flavor > RPC_AUTH_MAXFLAVOR)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return RPC_AUTH_GSS;
87 return flavor;
88}
89
90int
Trond Myklebustf1c0a862007-06-23 20:17:58 -040091rpcauth_register(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040094 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
97 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040098 spin_lock(&rpc_authflavor_lock);
99 if (auth_flavors[flavor] == NULL) {
100 auth_flavors[flavor] = ops;
101 ret = 0;
102 }
103 spin_unlock(&rpc_authflavor_lock);
104 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400106EXPORT_SYMBOL_GPL(rpcauth_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108int
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400109rpcauth_unregister(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400112 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
115 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400116 spin_lock(&rpc_authflavor_lock);
117 if (auth_flavors[flavor] == ops) {
118 auth_flavors[flavor] = NULL;
119 ret = 0;
120 }
121 spin_unlock(&rpc_authflavor_lock);
122 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400124EXPORT_SYMBOL_GPL(rpcauth_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400126/**
Chuck Lever9568c5e2013-03-16 15:54:43 -0400127 * rpcauth_get_pseudoflavor - check if security flavor is supported
128 * @flavor: a security flavor
129 * @info: a GSS mech OID, quality of protection, and service value
130 *
131 * Verifies that an appropriate kernel module is available or already loaded.
132 * Returns an equivalent pseudoflavor, or RPC_AUTH_MAXFLAVOR if "flavor" is
133 * not supported locally.
134 */
135rpc_authflavor_t
136rpcauth_get_pseudoflavor(rpc_authflavor_t flavor, struct rpcsec_gss_info *info)
137{
138 const struct rpc_authops *ops;
139 rpc_authflavor_t pseudoflavor;
140
141 ops = auth_flavors[flavor];
142 if (ops == NULL)
143 request_module("rpc-auth-%u", flavor);
144 spin_lock(&rpc_authflavor_lock);
145 ops = auth_flavors[flavor];
146 if (ops == NULL || !try_module_get(ops->owner)) {
147 spin_unlock(&rpc_authflavor_lock);
148 return RPC_AUTH_MAXFLAVOR;
149 }
150 spin_unlock(&rpc_authflavor_lock);
151
152 pseudoflavor = flavor;
153 if (ops->info2flavor != NULL)
154 pseudoflavor = ops->info2flavor(info);
155
156 module_put(ops->owner);
157 return pseudoflavor;
158}
159EXPORT_SYMBOL_GPL(rpcauth_get_pseudoflavor);
160
161/**
Chuck Levera77c8062013-03-16 15:55:10 -0400162 * rpcauth_get_gssinfo - find GSS tuple matching a GSS pseudoflavor
163 * @pseudoflavor: GSS pseudoflavor to match
164 * @info: rpcsec_gss_info structure to fill in
165 *
166 * Returns zero and fills in "info" if pseudoflavor matches a
167 * supported mechanism.
168 */
169int
170rpcauth_get_gssinfo(rpc_authflavor_t pseudoflavor, struct rpcsec_gss_info *info)
171{
172 rpc_authflavor_t flavor = pseudoflavor_to_flavor(pseudoflavor);
173 const struct rpc_authops *ops;
174 int result;
175
Chuck Lever1c74a242013-03-22 12:53:08 -0400176 if (flavor >= RPC_AUTH_MAXFLAVOR)
177 return -EINVAL;
178
Chuck Levera77c8062013-03-16 15:55:10 -0400179 ops = auth_flavors[flavor];
180 if (ops == NULL)
181 request_module("rpc-auth-%u", flavor);
182 spin_lock(&rpc_authflavor_lock);
183 ops = auth_flavors[flavor];
184 if (ops == NULL || !try_module_get(ops->owner)) {
185 spin_unlock(&rpc_authflavor_lock);
186 return -ENOENT;
187 }
188 spin_unlock(&rpc_authflavor_lock);
189
190 result = -ENOENT;
191 if (ops->flavor2info != NULL)
192 result = ops->flavor2info(pseudoflavor, info);
193
194 module_put(ops->owner);
195 return result;
196}
197EXPORT_SYMBOL_GPL(rpcauth_get_gssinfo);
198
199/**
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400200 * rpcauth_list_flavors - discover registered flavors and pseudoflavors
201 * @array: array to fill in
202 * @size: size of "array"
203 *
204 * Returns the number of array items filled in, or a negative errno.
205 *
206 * The returned array is not sorted by any policy. Callers should not
207 * rely on the order of the items in the returned array.
208 */
209int
210rpcauth_list_flavors(rpc_authflavor_t *array, int size)
211{
212 rpc_authflavor_t flavor;
213 int result = 0;
214
215 spin_lock(&rpc_authflavor_lock);
216 for (flavor = 0; flavor < RPC_AUTH_MAXFLAVOR; flavor++) {
217 const struct rpc_authops *ops = auth_flavors[flavor];
218 rpc_authflavor_t pseudos[4];
219 int i, len;
220
221 if (result >= size) {
222 result = -ENOMEM;
223 break;
224 }
225
226 if (ops == NULL)
227 continue;
228 if (ops->list_pseudoflavors == NULL) {
229 array[result++] = ops->au_flavor;
230 continue;
231 }
232 len = ops->list_pseudoflavors(pseudos, ARRAY_SIZE(pseudos));
233 if (len < 0) {
234 result = len;
235 break;
236 }
237 for (i = 0; i < len; i++) {
238 if (result >= size) {
239 result = -ENOMEM;
240 break;
241 }
242 array[result++] = pseudos[i];
243 }
244 }
245 spin_unlock(&rpc_authflavor_lock);
246
247 dprintk("RPC: %s returns %d\n", __func__, result);
248 return result;
249}
250EXPORT_SYMBOL_GPL(rpcauth_list_flavors);
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252struct rpc_auth *
Trond Myklebustc2190662013-08-26 19:23:04 -0400253rpcauth_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct rpc_auth *auth;
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400256 const struct rpc_authops *ops;
Trond Myklebustc2190662013-08-26 19:23:04 -0400257 u32 flavor = pseudoflavor_to_flavor(args->pseudoflavor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500259 auth = ERR_PTR(-EINVAL);
260 if (flavor >= RPC_AUTH_MAXFLAVOR)
261 goto out;
262
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500263 if ((ops = auth_flavors[flavor]) == NULL)
264 request_module("rpc-auth-%u", flavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400265 spin_lock(&rpc_authflavor_lock);
266 ops = auth_flavors[flavor];
267 if (ops == NULL || !try_module_get(ops->owner)) {
268 spin_unlock(&rpc_authflavor_lock);
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500269 goto out;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400270 }
271 spin_unlock(&rpc_authflavor_lock);
Trond Myklebustc2190662013-08-26 19:23:04 -0400272 auth = ops->create(args, clnt);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400273 module_put(ops->owner);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000274 if (IS_ERR(auth))
275 return auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (clnt->cl_auth)
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400277 rpcauth_release(clnt->cl_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 clnt->cl_auth = auth;
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500279
280out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return auth;
282}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400283EXPORT_SYMBOL_GPL(rpcauth_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285void
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400286rpcauth_release(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 if (!atomic_dec_and_test(&auth->au_count))
289 return;
290 auth->au_ops->destroy(auth);
291}
292
293static DEFINE_SPINLOCK(rpc_credcache_lock);
294
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400295static void
296rpcauth_unhash_cred_locked(struct rpc_cred *cred)
297{
298 hlist_del_rcu(&cred->cr_hash);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100299 smp_mb__before_atomic();
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400300 clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
301}
302
Trond Myklebustf0380f32009-12-03 08:10:17 -0500303static int
Trond Myklebust9499b432007-06-24 15:57:57 -0400304rpcauth_unhash_cred(struct rpc_cred *cred)
305{
306 spinlock_t *cache_lock;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500307 int ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400308
309 cache_lock = &cred->cr_auth->au_credcache->lock;
310 spin_lock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500311 ret = atomic_read(&cred->cr_count) == 0;
312 if (ret)
Trond Myklebust9499b432007-06-24 15:57:57 -0400313 rpcauth_unhash_cred_locked(cred);
314 spin_unlock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500315 return ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
319 * Initialize RPC credential cache
320 */
321int
Trond Myklebustf5c21872007-06-25 17:11:20 -0400322rpcauth_init_credcache(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct rpc_cred_cache *new;
Trond Myklebust988664a2010-07-31 14:29:07 -0400325 unsigned int hashsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800327 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (!new)
Trond Myklebust241269b2010-07-31 14:29:08 -0400329 goto out_nocache;
330 new->hashbits = auth_hashbits;
Trond Myklebust988664a2010-07-31 14:29:07 -0400331 hashsize = 1U << new->hashbits;
Trond Myklebust241269b2010-07-31 14:29:08 -0400332 new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
333 if (!new->hashtable)
334 goto out_nohashtbl;
Trond Myklebust9499b432007-06-24 15:57:57 -0400335 spin_lock_init(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 auth->au_credcache = new;
337 return 0;
Trond Myklebust241269b2010-07-31 14:29:08 -0400338out_nohashtbl:
339 kfree(new);
340out_nocache:
341 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400343EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345/*
Andy Adamson4de6caa2013-08-14 11:59:15 -0400346 * Setup a credential key lifetime timeout notification
347 */
348int
349rpcauth_key_timeout_notify(struct rpc_auth *auth, struct rpc_cred *cred)
350{
351 if (!cred->cr_auth->au_ops->key_timeout)
352 return 0;
353 return cred->cr_auth->au_ops->key_timeout(auth, cred);
354}
355EXPORT_SYMBOL_GPL(rpcauth_key_timeout_notify);
356
357bool
358rpcauth_cred_key_to_expire(struct rpc_cred *cred)
359{
360 if (!cred->cr_ops->crkey_to_expire)
361 return false;
362 return cred->cr_ops->crkey_to_expire(cred);
363}
364EXPORT_SYMBOL_GPL(rpcauth_cred_key_to_expire);
365
Jeff Laytona0337d12014-06-21 20:52:16 -0400366char *
367rpcauth_stringify_acceptor(struct rpc_cred *cred)
368{
369 if (!cred->cr_ops->crstringify_acceptor)
370 return NULL;
371 return cred->cr_ops->crstringify_acceptor(cred);
372}
373EXPORT_SYMBOL_GPL(rpcauth_stringify_acceptor);
374
Andy Adamson4de6caa2013-08-14 11:59:15 -0400375/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * Destroy a list of credentials
377 */
378static inline
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400379void rpcauth_destroy_credlist(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct rpc_cred *cred;
382
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400383 while (!list_empty(head)) {
384 cred = list_entry(head->next, struct rpc_cred, cr_lru);
385 list_del_init(&cred->cr_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 put_rpccred(cred);
387 }
388}
389
390/*
391 * Clear the RPC credential cache, and delete those credentials
392 * that are not referenced.
393 */
394void
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400395rpcauth_clear_credcache(struct rpc_cred_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400397 LIST_HEAD(free);
398 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 struct rpc_cred *cred;
Trond Myklebust988664a2010-07-31 14:29:07 -0400400 unsigned int hashsize = 1U << cache->hashbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 int i;
402
403 spin_lock(&rpc_credcache_lock);
Trond Myklebust9499b432007-06-24 15:57:57 -0400404 spin_lock(&cache->lock);
Trond Myklebust988664a2010-07-31 14:29:07 -0400405 for (i = 0; i < hashsize; i++) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400406 head = &cache->hashtable[i];
407 while (!hlist_empty(head)) {
408 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
409 get_rpccred(cred);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400410 if (!list_empty(&cred->cr_lru)) {
411 list_del(&cred->cr_lru);
412 number_cred_unused--;
413 }
414 list_add_tail(&cred->cr_lru, &free);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400415 rpcauth_unhash_cred_locked(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 }
Trond Myklebust9499b432007-06-24 15:57:57 -0400418 spin_unlock(&cache->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 spin_unlock(&rpc_credcache_lock);
420 rpcauth_destroy_credlist(&free);
421}
422
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400423/*
424 * Destroy the RPC credential cache
425 */
426void
427rpcauth_destroy_credcache(struct rpc_auth *auth)
428{
429 struct rpc_cred_cache *cache = auth->au_credcache;
430
431 if (cache) {
432 auth->au_credcache = NULL;
433 rpcauth_clear_credcache(cache);
Trond Myklebust241269b2010-07-31 14:29:08 -0400434 kfree(cache->hashtable);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400435 kfree(cache);
436 }
437}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400438EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400439
Trond Myklebustd2b83142008-04-14 18:13:37 -0400440
441#define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/*
444 * Remove stale credentials. Avoid sleeping inside the loop.
445 */
Dave Chinner70534a72013-08-28 10:18:14 +1000446static long
Trond Myklebustf5c21872007-06-25 17:11:20 -0400447rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Trond Myklebust9499b432007-06-24 15:57:57 -0400449 spinlock_t *cache_lock;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400450 struct rpc_cred *cred, *next;
Trond Myklebustd2b83142008-04-14 18:13:37 -0400451 unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
Dave Chinner70534a72013-08-28 10:18:14 +1000452 long freed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Trond Myklebusteac0d182008-10-28 15:21:41 -0400454 list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
455
Trond Myklebust20673402010-05-13 12:51:06 -0400456 if (nr_to_scan-- == 0)
457 break;
Trond Myklebust93a05e62010-05-13 12:51:06 -0400458 /*
459 * Enforce a 60 second garbage collection moratorium
460 * Note that the cred_unused list must be time-ordered.
461 */
Trond Myklebust3d7b0892010-04-22 15:35:55 -0400462 if (time_in_range(cred->cr_expire, expired, jiffies) &&
Trond Myklebusteac0d182008-10-28 15:21:41 -0400463 test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
Dave Chinner70534a72013-08-28 10:18:14 +1000464 break;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400465
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400466 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400467 number_cred_unused--;
Dave Chinner70534a72013-08-28 10:18:14 +1000468 freed++;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400469 if (atomic_read(&cred->cr_count) != 0)
470 continue;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400471
Trond Myklebust9499b432007-06-24 15:57:57 -0400472 cache_lock = &cred->cr_auth->au_credcache->lock;
473 spin_lock(cache_lock);
474 if (atomic_read(&cred->cr_count) == 0) {
475 get_rpccred(cred);
476 list_add_tail(&cred->cr_lru, free);
477 rpcauth_unhash_cred_locked(cred);
478 }
479 spin_unlock(cache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Dave Chinner70534a72013-08-28 10:18:14 +1000481 return freed;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400482}
483
484/*
Trond Myklebustf5c21872007-06-25 17:11:20 -0400485 * Run memory cache shrinker.
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400486 */
Dave Chinner70534a72013-08-28 10:18:14 +1000487static unsigned long
488rpcauth_cache_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
489
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400490{
Trond Myklebustf5c21872007-06-25 17:11:20 -0400491 LIST_HEAD(free);
Dave Chinner70534a72013-08-28 10:18:14 +1000492 unsigned long freed;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400493
Dave Chinner70534a72013-08-28 10:18:14 +1000494 if ((sc->gfp_mask & GFP_KERNEL) != GFP_KERNEL)
495 return SHRINK_STOP;
496
497 /* nothing left, don't come back */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400498 if (list_empty(&cred_unused))
Dave Chinner70534a72013-08-28 10:18:14 +1000499 return SHRINK_STOP;
500
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400501 spin_lock(&rpc_credcache_lock);
Dave Chinner70534a72013-08-28 10:18:14 +1000502 freed = rpcauth_prune_expired(&free, sc->nr_to_scan);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400503 spin_unlock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400504 rpcauth_destroy_credlist(&free);
Dave Chinner70534a72013-08-28 10:18:14 +1000505
506 return freed;
507}
508
509static unsigned long
510rpcauth_cache_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
511
512{
513 return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516/*
517 * Look up a process' credentials in the authentication cache
518 */
519struct rpc_cred *
520rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Trond Myklebust8a317762006-02-01 12:18:36 -0500521 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400523 LIST_HEAD(free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct rpc_cred_cache *cache = auth->au_credcache;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400525 struct rpc_cred *cred = NULL,
526 *entry, *new;
Trond Myklebust25337fd2008-03-12 14:40:14 -0400527 unsigned int nr;
528
Eric W. Biederman9e469e32013-02-02 01:57:30 -0800529 nr = hash_long(from_kuid(&init_user_ns, acred->uid), cache->hashbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400531 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800532 hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400533 if (!entry->cr_ops->crmatch(acred, entry, flags))
534 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400535 spin_lock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400536 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
Trond Myklebust9499b432007-06-24 15:57:57 -0400537 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400538 continue;
539 }
540 cred = get_rpccred(entry);
Trond Myklebust9499b432007-06-24 15:57:57 -0400541 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400542 break;
543 }
544 rcu_read_unlock();
545
Trond Myklebust9499b432007-06-24 15:57:57 -0400546 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400547 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400548
549 new = auth->au_ops->crcreate(auth, acred, flags);
550 if (IS_ERR(new)) {
551 cred = new;
552 goto out;
553 }
554
Trond Myklebust9499b432007-06-24 15:57:57 -0400555 spin_lock(&cache->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800556 hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400557 if (!entry->cr_ops->crmatch(acred, entry, flags))
558 continue;
559 cred = get_rpccred(entry);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400560 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400562 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400563 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400564 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
565 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
566 } else
567 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400568 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400569found:
Joe Perchesf64f9e72009-11-29 16:55:45 -0800570 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
571 cred->cr_ops->cr_init != NULL &&
572 !(flags & RPCAUTH_LOOKUP_NEW)) {
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500573 int res = cred->cr_ops->cr_init(auth, cred);
574 if (res < 0) {
575 put_rpccred(cred);
576 cred = ERR_PTR(res);
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400579 rpcauth_destroy_credlist(&free);
580out:
581 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400583EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500586rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
David Howells86a264a2008-11-14 10:39:18 +1100588 struct auth_cred acred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 struct rpc_cred *ret;
David Howells86a264a2008-11-14 10:39:18 +1100590 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Chuck Lever46121cf2007-01-31 12:14:08 -0500592 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 auth->au_ops->au_name);
David Howells86a264a2008-11-14 10:39:18 +1100594
595 memset(&acred, 0, sizeof(acred));
596 acred.uid = cred->fsuid;
597 acred.gid = cred->fsgid;
598 acred.group_info = get_group_info(((struct cred *)cred)->group_info);
599
Trond Myklebust8a317762006-02-01 12:18:36 -0500600 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 put_group_info(acred.group_info);
602 return ret;
603}
Andy Adamson66b06862014-06-12 15:02:32 -0400604EXPORT_SYMBOL_GPL(rpcauth_lookupcred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Trond Myklebust5fe47552007-06-23 19:55:31 -0400606void
607rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
608 struct rpc_auth *auth, const struct rpc_credops *ops)
609{
610 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400611 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400612 atomic_set(&cred->cr_count, 1);
613 cred->cr_auth = auth;
614 cred->cr_ops = ops;
615 cred->cr_expire = jiffies;
616#ifdef RPC_DEBUG
617 cred->cr_magic = RPCAUTH_CRED_MAGIC;
618#endif
619 cred->cr_uid = acred->uid;
620}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400621EXPORT_SYMBOL_GPL(rpcauth_init_cred);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400622
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400623struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400624rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400625{
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400626 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
627 cred->cr_auth->au_ops->au_name, cred);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400628 return get_rpccred(cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400629}
Trond Myklebust5c691042008-03-12 16:21:07 -0400630EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400631
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400632static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400633rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400635 struct rpc_auth *auth = task->tk_client->cl_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct auth_cred acred = {
Eric W. Biedermanbf37f792013-02-01 15:55:38 -0800637 .uid = GLOBAL_ROOT_UID,
638 .gid = GLOBAL_ROOT_GID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Chuck Lever46121cf2007-01-31 12:14:08 -0500641 dprintk("RPC: %5u looking up %s cred\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400642 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400643 return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
Trond Myklebustaf093832008-03-12 12:12:16 -0400644}
645
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400646static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400647rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
Trond Myklebustaf093832008-03-12 12:12:16 -0400648{
649 struct rpc_auth *auth = task->tk_client->cl_auth;
Trond Myklebustaf093832008-03-12 12:12:16 -0400650
651 dprintk("RPC: %5u looking up %s cred\n",
652 task->tk_pid, auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400653 return rpcauth_lookupcred(auth, lookupflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Trond Myklebusta17c2152010-07-31 14:29:08 -0400656static int
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400657rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400659 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400660 struct rpc_cred *new;
Trond Myklebust5d351752009-09-15 13:32:13 -0400661 int lookupflags = 0;
662
663 if (flags & RPC_TASK_ASYNC)
664 lookupflags |= RPCAUTH_LOOKUP_NEW;
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400665 if (cred != NULL)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400666 new = cred->cr_ops->crbind(task, cred, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400667 else if (flags & RPC_TASK_ROOTCREDS)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400668 new = rpcauth_bind_root_cred(task, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400669 else
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400670 new = rpcauth_bind_new_cred(task, lookupflags);
671 if (IS_ERR(new))
672 return PTR_ERR(new);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400673 if (req->rq_cred != NULL)
674 put_rpccred(req->rq_cred);
675 req->rq_cred = new;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400676 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679void
680put_rpccred(struct rpc_cred *cred)
681{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400682 /* Fast path for unhashed credentials */
Trond Myklebustf0380f32009-12-03 08:10:17 -0500683 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
684 if (atomic_dec_and_test(&cred->cr_count))
685 cred->cr_ops->crdestroy(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500687 }
688
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400689 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
690 return;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400691 if (!list_empty(&cred->cr_lru)) {
692 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400693 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400694 }
Trond Myklebust5f707eb2008-10-28 15:21:42 -0400695 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
Trond Myklebustf0380f32009-12-03 08:10:17 -0500696 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
697 cred->cr_expire = jiffies;
698 list_add_tail(&cred->cr_lru, &cred_unused);
699 number_cred_unused++;
700 goto out_nodestroy;
701 }
702 if (!rpcauth_unhash_cred(cred)) {
703 /* We were hashed and someone looked us up... */
704 goto out_nodestroy;
705 }
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400706 }
707 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 cred->cr_ops->crdestroy(cred);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500709 return;
710out_nodestroy:
711 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400713EXPORT_SYMBOL_GPL(put_rpccred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700715__be32 *
716rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400718 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Chuck Lever46121cf2007-01-31 12:14:08 -0500720 dprintk("RPC: %5u marshaling %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400721 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return cred->cr_ops->crmarshal(task, p);
724}
725
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700726__be32 *
727rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400729 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Chuck Lever46121cf2007-01-31 12:14:08 -0500731 dprintk("RPC: %5u validating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400732 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return cred->cr_ops->crvalidate(task, p);
735}
736
Chuck Lever9f06c712010-12-14 14:59:18 +0000737static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
738 __be32 *data, void *obj)
739{
740 struct xdr_stream xdr;
741
742 xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
743 encode(rqstp, &xdr, obj);
744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746int
Chuck Lever9f06c712010-12-14 14:59:18 +0000747rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700748 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400750 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Chuck Lever46121cf2007-01-31 12:14:08 -0500752 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 task->tk_pid, cred->cr_ops->cr_name, cred);
754 if (cred->cr_ops->crwrap_req)
755 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
756 /* By default, we encode the arguments normally. */
Chuck Lever9f06c712010-12-14 14:59:18 +0000757 rpcauth_wrap_req_encode(encode, rqstp, data, obj);
758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Chuck Leverbf269552010-12-14 14:59:29 +0000761static int
762rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
763 __be32 *data, void *obj)
764{
765 struct xdr_stream xdr;
766
767 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
768 return decode(rqstp, &xdr, obj);
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771int
Chuck Leverbf269552010-12-14 14:59:29 +0000772rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700773 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400775 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Chuck Lever46121cf2007-01-31 12:14:08 -0500777 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 task->tk_pid, cred->cr_ops->cr_name, cred);
779 if (cred->cr_ops->crunwrap_resp)
780 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
781 data, obj);
782 /* By default, we decode the arguments normally. */
Chuck Leverbf269552010-12-14 14:59:29 +0000783 return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786int
787rpcauth_refreshcred(struct rpc_task *task)
788{
Trond Myklebust9a84d382010-10-24 18:00:46 -0400789 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 int err;
791
Trond Myklebusta17c2152010-07-31 14:29:08 -0400792 cred = task->tk_rqstp->rq_cred;
793 if (cred == NULL) {
794 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
795 if (err < 0)
796 goto out;
797 cred = task->tk_rqstp->rq_cred;
Joe Perchesf81c6222011-06-03 11:51:19 +0000798 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500799 dprintk("RPC: %5u refreshing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400800 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 err = cred->cr_ops->crrefresh(task);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400803out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (err < 0)
805 task->tk_status = err;
806 return err;
807}
808
809void
810rpcauth_invalcred(struct rpc_task *task)
811{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400812 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400813
Chuck Lever46121cf2007-01-31 12:14:08 -0500814 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400815 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400816 if (cred)
817 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
820int
821rpcauth_uptodatecred(struct rpc_task *task)
822{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400823 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400824
825 return cred == NULL ||
826 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400828
Rusty Russell8e1f9362007-07-17 04:03:17 -0700829static struct shrinker rpc_cred_shrinker = {
Dave Chinner70534a72013-08-28 10:18:14 +1000830 .count_objects = rpcauth_cache_shrink_count,
831 .scan_objects = rpcauth_cache_shrink_scan,
Rusty Russell8e1f9362007-07-17 04:03:17 -0700832 .seeks = DEFAULT_SEEKS,
833};
Trond Myklebustf5c21872007-06-25 17:11:20 -0400834
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400835int __init rpcauth_init_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400836{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400837 int err;
838
839 err = rpc_init_authunix();
840 if (err < 0)
841 goto out1;
842 err = rpc_init_generic_auth();
843 if (err < 0)
844 goto out2;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700845 register_shrinker(&rpc_cred_shrinker);
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400846 return 0;
847out2:
848 rpc_destroy_authunix();
849out1:
850 return err;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400851}
852
Stephen Rothwellc135e842010-09-29 14:16:57 +1000853void rpcauth_remove_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400854{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400855 rpc_destroy_authunix();
856 rpc_destroy_generic_auth();
Rusty Russell8e1f9362007-07-17 04:03:17 -0700857 unregister_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400858}