blob: b5c067bccc4595204f21bff24bbf16631e54cad4 [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;
51 ret = strict_strtoul(val, 0, &num);
52 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) {
85 if (flavor >= RPC_AUTH_MAXFLAVOR)
86 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/**
127 * rpcauth_list_flavors - discover registered flavors and pseudoflavors
128 * @array: array to fill in
129 * @size: size of "array"
130 *
131 * Returns the number of array items filled in, or a negative errno.
132 *
133 * The returned array is not sorted by any policy. Callers should not
134 * rely on the order of the items in the returned array.
135 */
136int
137rpcauth_list_flavors(rpc_authflavor_t *array, int size)
138{
139 rpc_authflavor_t flavor;
140 int result = 0;
141
142 spin_lock(&rpc_authflavor_lock);
143 for (flavor = 0; flavor < RPC_AUTH_MAXFLAVOR; flavor++) {
144 const struct rpc_authops *ops = auth_flavors[flavor];
145 rpc_authflavor_t pseudos[4];
146 int i, len;
147
148 if (result >= size) {
149 result = -ENOMEM;
150 break;
151 }
152
153 if (ops == NULL)
154 continue;
155 if (ops->list_pseudoflavors == NULL) {
156 array[result++] = ops->au_flavor;
157 continue;
158 }
159 len = ops->list_pseudoflavors(pseudos, ARRAY_SIZE(pseudos));
160 if (len < 0) {
161 result = len;
162 break;
163 }
164 for (i = 0; i < len; i++) {
165 if (result >= size) {
166 result = -ENOMEM;
167 break;
168 }
169 array[result++] = pseudos[i];
170 }
171 }
172 spin_unlock(&rpc_authflavor_lock);
173
174 dprintk("RPC: %s returns %d\n", __func__, result);
175 return result;
176}
177EXPORT_SYMBOL_GPL(rpcauth_list_flavors);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179struct rpc_auth *
180rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
181{
182 struct rpc_auth *auth;
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400183 const struct rpc_authops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
185
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500186 auth = ERR_PTR(-EINVAL);
187 if (flavor >= RPC_AUTH_MAXFLAVOR)
188 goto out;
189
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500190 if ((ops = auth_flavors[flavor]) == NULL)
191 request_module("rpc-auth-%u", flavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400192 spin_lock(&rpc_authflavor_lock);
193 ops = auth_flavors[flavor];
194 if (ops == NULL || !try_module_get(ops->owner)) {
195 spin_unlock(&rpc_authflavor_lock);
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500196 goto out;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400197 }
198 spin_unlock(&rpc_authflavor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 auth = ops->create(clnt, pseudoflavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400200 module_put(ops->owner);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000201 if (IS_ERR(auth))
202 return auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (clnt->cl_auth)
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400204 rpcauth_release(clnt->cl_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 clnt->cl_auth = auth;
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500206
207out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return auth;
209}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400210EXPORT_SYMBOL_GPL(rpcauth_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212void
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400213rpcauth_release(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 if (!atomic_dec_and_test(&auth->au_count))
216 return;
217 auth->au_ops->destroy(auth);
218}
219
220static DEFINE_SPINLOCK(rpc_credcache_lock);
221
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400222static void
223rpcauth_unhash_cred_locked(struct rpc_cred *cred)
224{
225 hlist_del_rcu(&cred->cr_hash);
226 smp_mb__before_clear_bit();
227 clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
228}
229
Trond Myklebustf0380f32009-12-03 08:10:17 -0500230static int
Trond Myklebust9499b432007-06-24 15:57:57 -0400231rpcauth_unhash_cred(struct rpc_cred *cred)
232{
233 spinlock_t *cache_lock;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500234 int ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400235
236 cache_lock = &cred->cr_auth->au_credcache->lock;
237 spin_lock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500238 ret = atomic_read(&cred->cr_count) == 0;
239 if (ret)
Trond Myklebust9499b432007-06-24 15:57:57 -0400240 rpcauth_unhash_cred_locked(cred);
241 spin_unlock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500242 return ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/*
246 * Initialize RPC credential cache
247 */
248int
Trond Myklebustf5c21872007-06-25 17:11:20 -0400249rpcauth_init_credcache(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct rpc_cred_cache *new;
Trond Myklebust988664a2010-07-31 14:29:07 -0400252 unsigned int hashsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800254 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (!new)
Trond Myklebust241269b2010-07-31 14:29:08 -0400256 goto out_nocache;
257 new->hashbits = auth_hashbits;
Trond Myklebust988664a2010-07-31 14:29:07 -0400258 hashsize = 1U << new->hashbits;
Trond Myklebust241269b2010-07-31 14:29:08 -0400259 new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
260 if (!new->hashtable)
261 goto out_nohashtbl;
Trond Myklebust9499b432007-06-24 15:57:57 -0400262 spin_lock_init(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 auth->au_credcache = new;
264 return 0;
Trond Myklebust241269b2010-07-31 14:29:08 -0400265out_nohashtbl:
266 kfree(new);
267out_nocache:
268 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400270EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/*
273 * Destroy a list of credentials
274 */
275static inline
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400276void rpcauth_destroy_credlist(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 struct rpc_cred *cred;
279
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400280 while (!list_empty(head)) {
281 cred = list_entry(head->next, struct rpc_cred, cr_lru);
282 list_del_init(&cred->cr_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 put_rpccred(cred);
284 }
285}
286
287/*
288 * Clear the RPC credential cache, and delete those credentials
289 * that are not referenced.
290 */
291void
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400292rpcauth_clear_credcache(struct rpc_cred_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400294 LIST_HEAD(free);
295 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct rpc_cred *cred;
Trond Myklebust988664a2010-07-31 14:29:07 -0400297 unsigned int hashsize = 1U << cache->hashbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int i;
299
300 spin_lock(&rpc_credcache_lock);
Trond Myklebust9499b432007-06-24 15:57:57 -0400301 spin_lock(&cache->lock);
Trond Myklebust988664a2010-07-31 14:29:07 -0400302 for (i = 0; i < hashsize; i++) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400303 head = &cache->hashtable[i];
304 while (!hlist_empty(head)) {
305 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
306 get_rpccred(cred);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400307 if (!list_empty(&cred->cr_lru)) {
308 list_del(&cred->cr_lru);
309 number_cred_unused--;
310 }
311 list_add_tail(&cred->cr_lru, &free);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400312 rpcauth_unhash_cred_locked(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314 }
Trond Myklebust9499b432007-06-24 15:57:57 -0400315 spin_unlock(&cache->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 spin_unlock(&rpc_credcache_lock);
317 rpcauth_destroy_credlist(&free);
318}
319
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400320/*
321 * Destroy the RPC credential cache
322 */
323void
324rpcauth_destroy_credcache(struct rpc_auth *auth)
325{
326 struct rpc_cred_cache *cache = auth->au_credcache;
327
328 if (cache) {
329 auth->au_credcache = NULL;
330 rpcauth_clear_credcache(cache);
Trond Myklebust241269b2010-07-31 14:29:08 -0400331 kfree(cache->hashtable);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400332 kfree(cache);
333 }
334}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400335EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400336
Trond Myklebustd2b83142008-04-14 18:13:37 -0400337
338#define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/*
341 * Remove stale credentials. Avoid sleeping inside the loop.
342 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400343static int
344rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Trond Myklebust9499b432007-06-24 15:57:57 -0400346 spinlock_t *cache_lock;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400347 struct rpc_cred *cred, *next;
Trond Myklebustd2b83142008-04-14 18:13:37 -0400348 unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Trond Myklebusteac0d182008-10-28 15:21:41 -0400350 list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
351
Trond Myklebust20673402010-05-13 12:51:06 -0400352 if (nr_to_scan-- == 0)
353 break;
Trond Myklebust93a05e62010-05-13 12:51:06 -0400354 /*
355 * Enforce a 60 second garbage collection moratorium
356 * Note that the cred_unused list must be time-ordered.
357 */
Trond Myklebust3d7b0892010-04-22 15:35:55 -0400358 if (time_in_range(cred->cr_expire, expired, jiffies) &&
Trond Myklebusteac0d182008-10-28 15:21:41 -0400359 test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
Trond Myklebust93a05e62010-05-13 12:51:06 -0400360 return 0;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400361
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400362 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400363 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400364 if (atomic_read(&cred->cr_count) != 0)
365 continue;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400366
Trond Myklebust9499b432007-06-24 15:57:57 -0400367 cache_lock = &cred->cr_auth->au_credcache->lock;
368 spin_lock(cache_lock);
369 if (atomic_read(&cred->cr_count) == 0) {
370 get_rpccred(cred);
371 list_add_tail(&cred->cr_lru, free);
372 rpcauth_unhash_cred_locked(cred);
373 }
374 spin_unlock(cache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Trond Myklebust93a05e62010-05-13 12:51:06 -0400376 return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400377}
378
379/*
Trond Myklebustf5c21872007-06-25 17:11:20 -0400380 * Run memory cache shrinker.
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400381 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400382static int
Ying Han1495f232011-05-24 17:12:27 -0700383rpcauth_cache_shrinker(struct shrinker *shrink, struct shrink_control *sc)
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400384{
Trond Myklebustf5c21872007-06-25 17:11:20 -0400385 LIST_HEAD(free);
386 int res;
Ying Han1495f232011-05-24 17:12:27 -0700387 int nr_to_scan = sc->nr_to_scan;
388 gfp_t gfp_mask = sc->gfp_mask;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400389
Trond Myklebustd300a412010-05-13 12:51:03 -0400390 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
391 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400392 if (list_empty(&cred_unused))
393 return 0;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400394 spin_lock(&rpc_credcache_lock);
Trond Myklebust93a05e62010-05-13 12:51:06 -0400395 res = rpcauth_prune_expired(&free, nr_to_scan);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400396 spin_unlock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400397 rpcauth_destroy_credlist(&free);
398 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401/*
402 * Look up a process' credentials in the authentication cache
403 */
404struct rpc_cred *
405rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Trond Myklebust8a317762006-02-01 12:18:36 -0500406 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400408 LIST_HEAD(free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct rpc_cred_cache *cache = auth->au_credcache;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400410 struct hlist_node *pos;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400411 struct rpc_cred *cred = NULL,
412 *entry, *new;
Trond Myklebust25337fd2008-03-12 14:40:14 -0400413 unsigned int nr;
414
Trond Myklebust988664a2010-07-31 14:29:07 -0400415 nr = hash_long(acred->uid, cache->hashbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400417 rcu_read_lock();
418 hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) {
419 if (!entry->cr_ops->crmatch(acred, entry, flags))
420 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400421 spin_lock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400422 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
Trond Myklebust9499b432007-06-24 15:57:57 -0400423 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400424 continue;
425 }
426 cred = get_rpccred(entry);
Trond Myklebust9499b432007-06-24 15:57:57 -0400427 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400428 break;
429 }
430 rcu_read_unlock();
431
Trond Myklebust9499b432007-06-24 15:57:57 -0400432 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400433 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400434
435 new = auth->au_ops->crcreate(auth, acred, flags);
436 if (IS_ERR(new)) {
437 cred = new;
438 goto out;
439 }
440
Trond Myklebust9499b432007-06-24 15:57:57 -0400441 spin_lock(&cache->lock);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400442 hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) {
443 if (!entry->cr_ops->crmatch(acred, entry, flags))
444 continue;
445 cred = get_rpccred(entry);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400446 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400448 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400449 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400450 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
451 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
452 } else
453 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400454 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400455found:
Joe Perchesf64f9e72009-11-29 16:55:45 -0800456 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
457 cred->cr_ops->cr_init != NULL &&
458 !(flags & RPCAUTH_LOOKUP_NEW)) {
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500459 int res = cred->cr_ops->cr_init(auth, cred);
460 if (res < 0) {
461 put_rpccred(cred);
462 cred = ERR_PTR(res);
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400465 rpcauth_destroy_credlist(&free);
466out:
467 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400469EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500472rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
David Howells86a264a2008-11-14 10:39:18 +1100474 struct auth_cred acred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct rpc_cred *ret;
David Howells86a264a2008-11-14 10:39:18 +1100476 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Chuck Lever46121cf2007-01-31 12:14:08 -0500478 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 auth->au_ops->au_name);
David Howells86a264a2008-11-14 10:39:18 +1100480
481 memset(&acred, 0, sizeof(acred));
482 acred.uid = cred->fsuid;
483 acred.gid = cred->fsgid;
484 acred.group_info = get_group_info(((struct cred *)cred)->group_info);
485
Trond Myklebust8a317762006-02-01 12:18:36 -0500486 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 put_group_info(acred.group_info);
488 return ret;
489}
490
Trond Myklebust5fe47552007-06-23 19:55:31 -0400491void
492rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
493 struct rpc_auth *auth, const struct rpc_credops *ops)
494{
495 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400496 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400497 atomic_set(&cred->cr_count, 1);
498 cred->cr_auth = auth;
499 cred->cr_ops = ops;
500 cred->cr_expire = jiffies;
501#ifdef RPC_DEBUG
502 cred->cr_magic = RPCAUTH_CRED_MAGIC;
503#endif
504 cred->cr_uid = acred->uid;
505}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400506EXPORT_SYMBOL_GPL(rpcauth_init_cred);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400507
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400508struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400509rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400510{
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400511 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
512 cred->cr_auth->au_ops->au_name, cred);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400513 return get_rpccred(cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400514}
Trond Myklebust5c691042008-03-12 16:21:07 -0400515EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400516
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400517static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400518rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400520 struct rpc_auth *auth = task->tk_client->cl_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 struct auth_cred acred = {
Trond Myklebustaf093832008-03-12 12:12:16 -0400522 .uid = 0,
523 .gid = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Chuck Lever46121cf2007-01-31 12:14:08 -0500526 dprintk("RPC: %5u looking up %s cred\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400527 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400528 return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
Trond Myklebustaf093832008-03-12 12:12:16 -0400529}
530
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400531static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400532rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
Trond Myklebustaf093832008-03-12 12:12:16 -0400533{
534 struct rpc_auth *auth = task->tk_client->cl_auth;
Trond Myklebustaf093832008-03-12 12:12:16 -0400535
536 dprintk("RPC: %5u looking up %s cred\n",
537 task->tk_pid, auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400538 return rpcauth_lookupcred(auth, lookupflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Trond Myklebusta17c2152010-07-31 14:29:08 -0400541static int
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400542rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400544 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400545 struct rpc_cred *new;
Trond Myklebust5d351752009-09-15 13:32:13 -0400546 int lookupflags = 0;
547
548 if (flags & RPC_TASK_ASYNC)
549 lookupflags |= RPCAUTH_LOOKUP_NEW;
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400550 if (cred != NULL)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400551 new = cred->cr_ops->crbind(task, cred, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400552 else if (flags & RPC_TASK_ROOTCREDS)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400553 new = rpcauth_bind_root_cred(task, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400554 else
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400555 new = rpcauth_bind_new_cred(task, lookupflags);
556 if (IS_ERR(new))
557 return PTR_ERR(new);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400558 if (req->rq_cred != NULL)
559 put_rpccred(req->rq_cred);
560 req->rq_cred = new;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564void
565put_rpccred(struct rpc_cred *cred)
566{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400567 /* Fast path for unhashed credentials */
Trond Myklebustf0380f32009-12-03 08:10:17 -0500568 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
569 if (atomic_dec_and_test(&cred->cr_count))
570 cred->cr_ops->crdestroy(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500572 }
573
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400574 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
575 return;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400576 if (!list_empty(&cred->cr_lru)) {
577 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400578 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400579 }
Trond Myklebust5f707eb2008-10-28 15:21:42 -0400580 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
Trond Myklebustf0380f32009-12-03 08:10:17 -0500581 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
582 cred->cr_expire = jiffies;
583 list_add_tail(&cred->cr_lru, &cred_unused);
584 number_cred_unused++;
585 goto out_nodestroy;
586 }
587 if (!rpcauth_unhash_cred(cred)) {
588 /* We were hashed and someone looked us up... */
589 goto out_nodestroy;
590 }
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400591 }
592 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 cred->cr_ops->crdestroy(cred);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500594 return;
595out_nodestroy:
596 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400598EXPORT_SYMBOL_GPL(put_rpccred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700600__be32 *
601rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400603 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Chuck Lever46121cf2007-01-31 12:14:08 -0500605 dprintk("RPC: %5u marshaling %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400606 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return cred->cr_ops->crmarshal(task, p);
609}
610
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700611__be32 *
612rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400614 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Chuck Lever46121cf2007-01-31 12:14:08 -0500616 dprintk("RPC: %5u validating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400617 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return cred->cr_ops->crvalidate(task, p);
620}
621
Chuck Lever9f06c712010-12-14 14:59:18 +0000622static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
623 __be32 *data, void *obj)
624{
625 struct xdr_stream xdr;
626
627 xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
628 encode(rqstp, &xdr, obj);
629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631int
Chuck Lever9f06c712010-12-14 14:59:18 +0000632rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700633 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400635 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Chuck Lever46121cf2007-01-31 12:14:08 -0500637 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 task->tk_pid, cred->cr_ops->cr_name, cred);
639 if (cred->cr_ops->crwrap_req)
640 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
641 /* By default, we encode the arguments normally. */
Chuck Lever9f06c712010-12-14 14:59:18 +0000642 rpcauth_wrap_req_encode(encode, rqstp, data, obj);
643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Chuck Leverbf269552010-12-14 14:59:29 +0000646static int
647rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
648 __be32 *data, void *obj)
649{
650 struct xdr_stream xdr;
651
652 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
653 return decode(rqstp, &xdr, obj);
654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656int
Chuck Leverbf269552010-12-14 14:59:29 +0000657rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700658 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400660 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Chuck Lever46121cf2007-01-31 12:14:08 -0500662 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 task->tk_pid, cred->cr_ops->cr_name, cred);
664 if (cred->cr_ops->crunwrap_resp)
665 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
666 data, obj);
667 /* By default, we decode the arguments normally. */
Chuck Leverbf269552010-12-14 14:59:29 +0000668 return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671int
672rpcauth_refreshcred(struct rpc_task *task)
673{
Trond Myklebust9a84d382010-10-24 18:00:46 -0400674 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int err;
676
Trond Myklebusta17c2152010-07-31 14:29:08 -0400677 cred = task->tk_rqstp->rq_cred;
678 if (cred == NULL) {
679 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
680 if (err < 0)
681 goto out;
682 cred = task->tk_rqstp->rq_cred;
Joe Perchesf81c6222011-06-03 11:51:19 +0000683 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500684 dprintk("RPC: %5u refreshing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400685 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 err = cred->cr_ops->crrefresh(task);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400688out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (err < 0)
690 task->tk_status = err;
691 return err;
692}
693
694void
695rpcauth_invalcred(struct rpc_task *task)
696{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400697 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400698
Chuck Lever46121cf2007-01-31 12:14:08 -0500699 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400700 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400701 if (cred)
702 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705int
706rpcauth_uptodatecred(struct rpc_task *task)
707{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400708 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400709
710 return cred == NULL ||
711 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400713
Rusty Russell8e1f9362007-07-17 04:03:17 -0700714static struct shrinker rpc_cred_shrinker = {
715 .shrink = rpcauth_cache_shrinker,
716 .seeks = DEFAULT_SEEKS,
717};
Trond Myklebustf5c21872007-06-25 17:11:20 -0400718
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400719int __init rpcauth_init_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400720{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400721 int err;
722
723 err = rpc_init_authunix();
724 if (err < 0)
725 goto out1;
726 err = rpc_init_generic_auth();
727 if (err < 0)
728 goto out2;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700729 register_shrinker(&rpc_cred_shrinker);
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400730 return 0;
731out2:
732 rpc_destroy_authunix();
733out1:
734 return err;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400735}
736
Stephen Rothwellc135e842010-09-29 14:16:57 +1000737void rpcauth_remove_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400738{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400739 rpc_destroy_authunix();
740 rpc_destroy_generic_auth();
Rusty Russell8e1f9362007-07-17 04:03:17 -0700741 unregister_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400742}