blob: 880d0de3f50fe14c4ce3daba30ab11266f53948c [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>
16#include <linux/spinlock.h>
17
18#ifdef RPC_DEBUG
19# define RPCDBG_FACILITY RPCDBG_AUTH
20#endif
21
Trond Myklebust241269b2010-07-31 14:29:08 -040022#define RPC_CREDCACHE_DEFAULT_HASHBITS (4)
23struct rpc_cred_cache {
24 struct hlist_head *hashtable;
25 unsigned int hashbits;
26 spinlock_t lock;
27};
28
29static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS;
30
Trond Myklebustfc1b3562007-06-09 16:15:46 -040031static DEFINE_SPINLOCK(rpc_authflavor_lock);
Trond Myklebustf1c0a862007-06-23 20:17:58 -040032static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 &authnull_ops, /* AUTH_NULL */
34 &authunix_ops, /* AUTH_UNIX */
35 NULL, /* others can be loadable modules */
36};
37
Trond Myklebuste092bdc2007-06-23 19:45:36 -040038static LIST_HEAD(cred_unused);
Trond Myklebustf5c21872007-06-25 17:11:20 -040039static unsigned long number_cred_unused;
Trond Myklebuste092bdc2007-06-23 19:45:36 -040040
Trond Myklebust241269b2010-07-31 14:29:08 -040041#define MAX_HASHTABLE_BITS (10)
42static int param_set_hashtbl_sz(const char *val, struct kernel_param *kp)
43{
44 unsigned long num;
45 unsigned int nbits;
46 int ret;
47
48 if (!val)
49 goto out_inval;
50 ret = strict_strtoul(val, 0, &num);
51 if (ret == -EINVAL)
52 goto out_inval;
53 nbits = fls(num);
54 if (num > (1U << nbits))
55 nbits++;
56 if (nbits > MAX_HASHTABLE_BITS || nbits < 2)
57 goto out_inval;
58 *(unsigned int *)kp->arg = nbits;
59 return 0;
60out_inval:
61 return -EINVAL;
62}
63
64static int param_get_hashtbl_sz(char *buffer, struct kernel_param *kp)
65{
66 unsigned int nbits;
67
68 nbits = *(unsigned int *)kp->arg;
69 return sprintf(buffer, "%u", 1U << nbits);
70}
71
72#define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
73
74module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
75MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static u32
78pseudoflavor_to_flavor(u32 flavor) {
79 if (flavor >= RPC_AUTH_MAXFLAVOR)
80 return RPC_AUTH_GSS;
81 return flavor;
82}
83
84int
Trond Myklebustf1c0a862007-06-23 20:17:58 -040085rpcauth_register(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040088 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
91 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040092 spin_lock(&rpc_authflavor_lock);
93 if (auth_flavors[flavor] == NULL) {
94 auth_flavors[flavor] = ops;
95 ret = 0;
96 }
97 spin_unlock(&rpc_authflavor_lock);
98 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400100EXPORT_SYMBOL_GPL(rpcauth_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102int
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400103rpcauth_unregister(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400106 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
109 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400110 spin_lock(&rpc_authflavor_lock);
111 if (auth_flavors[flavor] == ops) {
112 auth_flavors[flavor] = NULL;
113 ret = 0;
114 }
115 spin_unlock(&rpc_authflavor_lock);
116 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400118EXPORT_SYMBOL_GPL(rpcauth_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120struct rpc_auth *
121rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
122{
123 struct rpc_auth *auth;
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400124 const struct rpc_authops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
126
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500127 auth = ERR_PTR(-EINVAL);
128 if (flavor >= RPC_AUTH_MAXFLAVOR)
129 goto out;
130
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500131 if ((ops = auth_flavors[flavor]) == NULL)
132 request_module("rpc-auth-%u", flavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400133 spin_lock(&rpc_authflavor_lock);
134 ops = auth_flavors[flavor];
135 if (ops == NULL || !try_module_get(ops->owner)) {
136 spin_unlock(&rpc_authflavor_lock);
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500137 goto out;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400138 }
139 spin_unlock(&rpc_authflavor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 auth = ops->create(clnt, pseudoflavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400141 module_put(ops->owner);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000142 if (IS_ERR(auth))
143 return auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (clnt->cl_auth)
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400145 rpcauth_release(clnt->cl_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 clnt->cl_auth = auth;
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500147
148out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return auth;
150}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400151EXPORT_SYMBOL_GPL(rpcauth_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153void
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400154rpcauth_release(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 if (!atomic_dec_and_test(&auth->au_count))
157 return;
158 auth->au_ops->destroy(auth);
159}
160
161static DEFINE_SPINLOCK(rpc_credcache_lock);
162
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400163static void
164rpcauth_unhash_cred_locked(struct rpc_cred *cred)
165{
166 hlist_del_rcu(&cred->cr_hash);
167 smp_mb__before_clear_bit();
168 clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
169}
170
Trond Myklebustf0380f32009-12-03 08:10:17 -0500171static int
Trond Myklebust9499b432007-06-24 15:57:57 -0400172rpcauth_unhash_cred(struct rpc_cred *cred)
173{
174 spinlock_t *cache_lock;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500175 int ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400176
177 cache_lock = &cred->cr_auth->au_credcache->lock;
178 spin_lock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500179 ret = atomic_read(&cred->cr_count) == 0;
180 if (ret)
Trond Myklebust9499b432007-06-24 15:57:57 -0400181 rpcauth_unhash_cred_locked(cred);
182 spin_unlock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500183 return ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
187 * Initialize RPC credential cache
188 */
189int
Trond Myklebustf5c21872007-06-25 17:11:20 -0400190rpcauth_init_credcache(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 struct rpc_cred_cache *new;
Trond Myklebust988664a2010-07-31 14:29:07 -0400193 unsigned int hashsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800195 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (!new)
Trond Myklebust241269b2010-07-31 14:29:08 -0400197 goto out_nocache;
198 new->hashbits = auth_hashbits;
Trond Myklebust988664a2010-07-31 14:29:07 -0400199 hashsize = 1U << new->hashbits;
Trond Myklebust241269b2010-07-31 14:29:08 -0400200 new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
201 if (!new->hashtable)
202 goto out_nohashtbl;
Trond Myklebust9499b432007-06-24 15:57:57 -0400203 spin_lock_init(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 auth->au_credcache = new;
205 return 0;
Trond Myklebust241269b2010-07-31 14:29:08 -0400206out_nohashtbl:
207 kfree(new);
208out_nocache:
209 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400211EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213/*
214 * Destroy a list of credentials
215 */
216static inline
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400217void rpcauth_destroy_credlist(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct rpc_cred *cred;
220
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400221 while (!list_empty(head)) {
222 cred = list_entry(head->next, struct rpc_cred, cr_lru);
223 list_del_init(&cred->cr_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 put_rpccred(cred);
225 }
226}
227
228/*
229 * Clear the RPC credential cache, and delete those credentials
230 * that are not referenced.
231 */
232void
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400233rpcauth_clear_credcache(struct rpc_cred_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400235 LIST_HEAD(free);
236 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 struct rpc_cred *cred;
Trond Myklebust988664a2010-07-31 14:29:07 -0400238 unsigned int hashsize = 1U << cache->hashbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 int i;
240
241 spin_lock(&rpc_credcache_lock);
Trond Myklebust9499b432007-06-24 15:57:57 -0400242 spin_lock(&cache->lock);
Trond Myklebust988664a2010-07-31 14:29:07 -0400243 for (i = 0; i < hashsize; i++) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400244 head = &cache->hashtable[i];
245 while (!hlist_empty(head)) {
246 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
247 get_rpccred(cred);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400248 if (!list_empty(&cred->cr_lru)) {
249 list_del(&cred->cr_lru);
250 number_cred_unused--;
251 }
252 list_add_tail(&cred->cr_lru, &free);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400253 rpcauth_unhash_cred_locked(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255 }
Trond Myklebust9499b432007-06-24 15:57:57 -0400256 spin_unlock(&cache->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 spin_unlock(&rpc_credcache_lock);
258 rpcauth_destroy_credlist(&free);
259}
260
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400261/*
262 * Destroy the RPC credential cache
263 */
264void
265rpcauth_destroy_credcache(struct rpc_auth *auth)
266{
267 struct rpc_cred_cache *cache = auth->au_credcache;
268
269 if (cache) {
270 auth->au_credcache = NULL;
271 rpcauth_clear_credcache(cache);
Trond Myklebust241269b2010-07-31 14:29:08 -0400272 kfree(cache->hashtable);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400273 kfree(cache);
274 }
275}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400276EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400277
Trond Myklebustd2b83142008-04-14 18:13:37 -0400278
279#define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281/*
282 * Remove stale credentials. Avoid sleeping inside the loop.
283 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400284static int
285rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Trond Myklebust9499b432007-06-24 15:57:57 -0400287 spinlock_t *cache_lock;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400288 struct rpc_cred *cred, *next;
Trond Myklebustd2b83142008-04-14 18:13:37 -0400289 unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Trond Myklebusteac0d182008-10-28 15:21:41 -0400291 list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
292
Trond Myklebust20673402010-05-13 12:51:06 -0400293 if (nr_to_scan-- == 0)
294 break;
Trond Myklebust93a05e62010-05-13 12:51:06 -0400295 /*
296 * Enforce a 60 second garbage collection moratorium
297 * Note that the cred_unused list must be time-ordered.
298 */
Trond Myklebust3d7b0892010-04-22 15:35:55 -0400299 if (time_in_range(cred->cr_expire, expired, jiffies) &&
Trond Myklebusteac0d182008-10-28 15:21:41 -0400300 test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
Trond Myklebust93a05e62010-05-13 12:51:06 -0400301 return 0;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400302
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400303 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400304 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400305 if (atomic_read(&cred->cr_count) != 0)
306 continue;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400307
Trond Myklebust9499b432007-06-24 15:57:57 -0400308 cache_lock = &cred->cr_auth->au_credcache->lock;
309 spin_lock(cache_lock);
310 if (atomic_read(&cred->cr_count) == 0) {
311 get_rpccred(cred);
312 list_add_tail(&cred->cr_lru, free);
313 rpcauth_unhash_cred_locked(cred);
314 }
315 spin_unlock(cache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
Trond Myklebust93a05e62010-05-13 12:51:06 -0400317 return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400318}
319
320/*
Trond Myklebustf5c21872007-06-25 17:11:20 -0400321 * Run memory cache shrinker.
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400322 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400323static int
Dave Chinner567c7b02010-07-21 15:33:01 +1000324rpcauth_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400325{
Trond Myklebustf5c21872007-06-25 17:11:20 -0400326 LIST_HEAD(free);
327 int res;
328
Trond Myklebustd300a412010-05-13 12:51:03 -0400329 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
330 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400331 if (list_empty(&cred_unused))
332 return 0;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400333 spin_lock(&rpc_credcache_lock);
Trond Myklebust93a05e62010-05-13 12:51:06 -0400334 res = rpcauth_prune_expired(&free, nr_to_scan);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400335 spin_unlock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400336 rpcauth_destroy_credlist(&free);
337 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340/*
341 * Look up a process' credentials in the authentication cache
342 */
343struct rpc_cred *
344rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Trond Myklebust8a317762006-02-01 12:18:36 -0500345 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400347 LIST_HEAD(free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct rpc_cred_cache *cache = auth->au_credcache;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400349 struct hlist_node *pos;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400350 struct rpc_cred *cred = NULL,
351 *entry, *new;
Trond Myklebust25337fd2008-03-12 14:40:14 -0400352 unsigned int nr;
353
Trond Myklebust988664a2010-07-31 14:29:07 -0400354 nr = hash_long(acred->uid, cache->hashbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400356 rcu_read_lock();
357 hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) {
358 if (!entry->cr_ops->crmatch(acred, entry, flags))
359 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400360 spin_lock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400361 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
Trond Myklebust9499b432007-06-24 15:57:57 -0400362 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400363 continue;
364 }
365 cred = get_rpccred(entry);
Trond Myklebust9499b432007-06-24 15:57:57 -0400366 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400367 break;
368 }
369 rcu_read_unlock();
370
Trond Myklebust9499b432007-06-24 15:57:57 -0400371 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400372 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400373
374 new = auth->au_ops->crcreate(auth, acred, flags);
375 if (IS_ERR(new)) {
376 cred = new;
377 goto out;
378 }
379
Trond Myklebust9499b432007-06-24 15:57:57 -0400380 spin_lock(&cache->lock);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400381 hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) {
382 if (!entry->cr_ops->crmatch(acred, entry, flags))
383 continue;
384 cred = get_rpccred(entry);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400385 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400387 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400388 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400389 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
390 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
391 } else
392 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400393 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400394found:
Joe Perchesf64f9e72009-11-29 16:55:45 -0800395 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
396 cred->cr_ops->cr_init != NULL &&
397 !(flags & RPCAUTH_LOOKUP_NEW)) {
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500398 int res = cred->cr_ops->cr_init(auth, cred);
399 if (res < 0) {
400 put_rpccred(cred);
401 cred = ERR_PTR(res);
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400404 rpcauth_destroy_credlist(&free);
405out:
406 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400408EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500411rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
David Howells86a264a2008-11-14 10:39:18 +1100413 struct auth_cred acred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct rpc_cred *ret;
David Howells86a264a2008-11-14 10:39:18 +1100415 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Chuck Lever46121cf2007-01-31 12:14:08 -0500417 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 auth->au_ops->au_name);
David Howells86a264a2008-11-14 10:39:18 +1100419
420 memset(&acred, 0, sizeof(acred));
421 acred.uid = cred->fsuid;
422 acred.gid = cred->fsgid;
423 acred.group_info = get_group_info(((struct cred *)cred)->group_info);
424
Trond Myklebust8a317762006-02-01 12:18:36 -0500425 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 put_group_info(acred.group_info);
427 return ret;
428}
429
Trond Myklebust5fe47552007-06-23 19:55:31 -0400430void
431rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
432 struct rpc_auth *auth, const struct rpc_credops *ops)
433{
434 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400435 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400436 atomic_set(&cred->cr_count, 1);
437 cred->cr_auth = auth;
438 cred->cr_ops = ops;
439 cred->cr_expire = jiffies;
440#ifdef RPC_DEBUG
441 cred->cr_magic = RPCAUTH_CRED_MAGIC;
442#endif
443 cred->cr_uid = acred->uid;
444}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400445EXPORT_SYMBOL_GPL(rpcauth_init_cred);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400446
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400447struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400448rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400449{
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400450 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
451 cred->cr_auth->au_ops->au_name, cred);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400452 return get_rpccred(cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400453}
Trond Myklebust5c691042008-03-12 16:21:07 -0400454EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400455
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400456static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400457rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400459 struct rpc_auth *auth = task->tk_client->cl_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct auth_cred acred = {
Trond Myklebustaf093832008-03-12 12:12:16 -0400461 .uid = 0,
462 .gid = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Chuck Lever46121cf2007-01-31 12:14:08 -0500465 dprintk("RPC: %5u looking up %s cred\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400466 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400467 return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
Trond Myklebustaf093832008-03-12 12:12:16 -0400468}
469
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400470static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400471rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
Trond Myklebustaf093832008-03-12 12:12:16 -0400472{
473 struct rpc_auth *auth = task->tk_client->cl_auth;
Trond Myklebustaf093832008-03-12 12:12:16 -0400474
475 dprintk("RPC: %5u looking up %s cred\n",
476 task->tk_pid, auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400477 return rpcauth_lookupcred(auth, lookupflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Trond Myklebusta17c2152010-07-31 14:29:08 -0400480static int
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400481rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400483 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400484 struct rpc_cred *new;
Trond Myklebust5d351752009-09-15 13:32:13 -0400485 int lookupflags = 0;
486
487 if (flags & RPC_TASK_ASYNC)
488 lookupflags |= RPCAUTH_LOOKUP_NEW;
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400489 if (cred != NULL)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400490 new = cred->cr_ops->crbind(task, cred, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400491 else if (flags & RPC_TASK_ROOTCREDS)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400492 new = rpcauth_bind_root_cred(task, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400493 else
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400494 new = rpcauth_bind_new_cred(task, lookupflags);
495 if (IS_ERR(new))
496 return PTR_ERR(new);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400497 if (req->rq_cred != NULL)
498 put_rpccred(req->rq_cred);
499 req->rq_cred = new;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503void
504put_rpccred(struct rpc_cred *cred)
505{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400506 /* Fast path for unhashed credentials */
Trond Myklebustf0380f32009-12-03 08:10:17 -0500507 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
508 if (atomic_dec_and_test(&cred->cr_count))
509 cred->cr_ops->crdestroy(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500511 }
512
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400513 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
514 return;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400515 if (!list_empty(&cred->cr_lru)) {
516 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400517 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400518 }
Trond Myklebust5f707eb2008-10-28 15:21:42 -0400519 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
Trond Myklebustf0380f32009-12-03 08:10:17 -0500520 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
521 cred->cr_expire = jiffies;
522 list_add_tail(&cred->cr_lru, &cred_unused);
523 number_cred_unused++;
524 goto out_nodestroy;
525 }
526 if (!rpcauth_unhash_cred(cred)) {
527 /* We were hashed and someone looked us up... */
528 goto out_nodestroy;
529 }
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400530 }
531 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 cred->cr_ops->crdestroy(cred);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500533 return;
534out_nodestroy:
535 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400537EXPORT_SYMBOL_GPL(put_rpccred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700539__be32 *
540rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400542 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Chuck Lever46121cf2007-01-31 12:14:08 -0500544 dprintk("RPC: %5u marshaling %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400545 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return cred->cr_ops->crmarshal(task, p);
548}
549
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700550__be32 *
551rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400553 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Chuck Lever46121cf2007-01-31 12:14:08 -0500555 dprintk("RPC: %5u validating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400556 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return cred->cr_ops->crvalidate(task, p);
559}
560
561int
562rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700563 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400565 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Chuck Lever46121cf2007-01-31 12:14:08 -0500567 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 task->tk_pid, cred->cr_ops->cr_name, cred);
569 if (cred->cr_ops->crwrap_req)
570 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
571 /* By default, we encode the arguments normally. */
Trond Myklebust88a9fe82008-12-23 15:21:31 -0500572 return encode(rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575int
576rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700577 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400579 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Chuck Lever46121cf2007-01-31 12:14:08 -0500581 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 task->tk_pid, cred->cr_ops->cr_name, cred);
583 if (cred->cr_ops->crunwrap_resp)
584 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
585 data, obj);
586 /* By default, we decode the arguments normally. */
Trond Myklebust88a9fe82008-12-23 15:21:31 -0500587 return decode(rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590int
591rpcauth_refreshcred(struct rpc_task *task)
592{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400593 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 int err;
595
Trond Myklebusta17c2152010-07-31 14:29:08 -0400596 cred = task->tk_rqstp->rq_cred;
597 if (cred == NULL) {
598 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
599 if (err < 0)
600 goto out;
601 cred = task->tk_rqstp->rq_cred;
602 };
Chuck Lever46121cf2007-01-31 12:14:08 -0500603 dprintk("RPC: %5u refreshing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400604 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 err = cred->cr_ops->crrefresh(task);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400607out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (err < 0)
609 task->tk_status = err;
610 return err;
611}
612
613void
614rpcauth_invalcred(struct rpc_task *task)
615{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400616 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400617
Chuck Lever46121cf2007-01-31 12:14:08 -0500618 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400619 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400620 if (cred)
621 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624int
625rpcauth_uptodatecred(struct rpc_task *task)
626{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400627 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400628
629 return cred == NULL ||
630 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400632
Rusty Russell8e1f9362007-07-17 04:03:17 -0700633static struct shrinker rpc_cred_shrinker = {
634 .shrink = rpcauth_cache_shrinker,
635 .seeks = DEFAULT_SEEKS,
636};
Trond Myklebustf5c21872007-06-25 17:11:20 -0400637
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400638int __init rpcauth_init_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400639{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400640 int err;
641
642 err = rpc_init_authunix();
643 if (err < 0)
644 goto out1;
645 err = rpc_init_generic_auth();
646 if (err < 0)
647 goto out2;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700648 register_shrinker(&rpc_cred_shrinker);
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400649 return 0;
650out2:
651 rpc_destroy_authunix();
652out1:
653 return err;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400654}
655
656void __exit rpcauth_remove_module(void)
657{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400658 rpc_destroy_authunix();
659 rpc_destroy_generic_auth();
Rusty Russell8e1f9362007-07-17 04:03:17 -0700660 unregister_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400661}