blob: 36cb66022a279e96869de77c3c9cebbbe9824ff4 [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)
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100042static int param_set_hashtbl_sz(const char *val, const struct kernel_param *kp)
Trond Myklebust241269b2010-07-31 14:29:08 -040043{
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
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100064static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp)
Trond Myklebust241269b2010-07-31 14:29:08 -040065{
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
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100074static struct kernel_param_ops param_ops_hashtbl_sz = {
75 .set = param_set_hashtbl_sz,
76 .get = param_get_hashtbl_sz,
77};
78
Trond Myklebust241269b2010-07-31 14:29:08 -040079module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
80MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static u32
83pseudoflavor_to_flavor(u32 flavor) {
84 if (flavor >= RPC_AUTH_MAXFLAVOR)
85 return RPC_AUTH_GSS;
86 return flavor;
87}
88
89int
Trond Myklebustf1c0a862007-06-23 20:17:58 -040090rpcauth_register(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040093 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
96 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040097 spin_lock(&rpc_authflavor_lock);
98 if (auth_flavors[flavor] == NULL) {
99 auth_flavors[flavor] = ops;
100 ret = 0;
101 }
102 spin_unlock(&rpc_authflavor_lock);
103 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400105EXPORT_SYMBOL_GPL(rpcauth_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107int
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400108rpcauth_unregister(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400111 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
114 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400115 spin_lock(&rpc_authflavor_lock);
116 if (auth_flavors[flavor] == ops) {
117 auth_flavors[flavor] = NULL;
118 ret = 0;
119 }
120 spin_unlock(&rpc_authflavor_lock);
121 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400123EXPORT_SYMBOL_GPL(rpcauth_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125struct rpc_auth *
126rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
127{
128 struct rpc_auth *auth;
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400129 const struct rpc_authops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
131
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500132 auth = ERR_PTR(-EINVAL);
133 if (flavor >= RPC_AUTH_MAXFLAVOR)
134 goto out;
135
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500136 if ((ops = auth_flavors[flavor]) == NULL)
137 request_module("rpc-auth-%u", flavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400138 spin_lock(&rpc_authflavor_lock);
139 ops = auth_flavors[flavor];
140 if (ops == NULL || !try_module_get(ops->owner)) {
141 spin_unlock(&rpc_authflavor_lock);
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500142 goto out;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400143 }
144 spin_unlock(&rpc_authflavor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 auth = ops->create(clnt, pseudoflavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400146 module_put(ops->owner);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000147 if (IS_ERR(auth))
148 return auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (clnt->cl_auth)
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400150 rpcauth_release(clnt->cl_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 clnt->cl_auth = auth;
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500152
153out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return auth;
155}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400156EXPORT_SYMBOL_GPL(rpcauth_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158void
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400159rpcauth_release(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 if (!atomic_dec_and_test(&auth->au_count))
162 return;
163 auth->au_ops->destroy(auth);
164}
165
166static DEFINE_SPINLOCK(rpc_credcache_lock);
167
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400168static void
169rpcauth_unhash_cred_locked(struct rpc_cred *cred)
170{
171 hlist_del_rcu(&cred->cr_hash);
172 smp_mb__before_clear_bit();
173 clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
174}
175
Trond Myklebustf0380f32009-12-03 08:10:17 -0500176static int
Trond Myklebust9499b432007-06-24 15:57:57 -0400177rpcauth_unhash_cred(struct rpc_cred *cred)
178{
179 spinlock_t *cache_lock;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500180 int ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400181
182 cache_lock = &cred->cr_auth->au_credcache->lock;
183 spin_lock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500184 ret = atomic_read(&cred->cr_count) == 0;
185 if (ret)
Trond Myklebust9499b432007-06-24 15:57:57 -0400186 rpcauth_unhash_cred_locked(cred);
187 spin_unlock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500188 return ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
192 * Initialize RPC credential cache
193 */
194int
Trond Myklebustf5c21872007-06-25 17:11:20 -0400195rpcauth_init_credcache(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct rpc_cred_cache *new;
Trond Myklebust988664a2010-07-31 14:29:07 -0400198 unsigned int hashsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800200 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (!new)
Trond Myklebust241269b2010-07-31 14:29:08 -0400202 goto out_nocache;
203 new->hashbits = auth_hashbits;
Trond Myklebust988664a2010-07-31 14:29:07 -0400204 hashsize = 1U << new->hashbits;
Trond Myklebust241269b2010-07-31 14:29:08 -0400205 new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
206 if (!new->hashtable)
207 goto out_nohashtbl;
Trond Myklebust9499b432007-06-24 15:57:57 -0400208 spin_lock_init(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 auth->au_credcache = new;
210 return 0;
Trond Myklebust241269b2010-07-31 14:29:08 -0400211out_nohashtbl:
212 kfree(new);
213out_nocache:
214 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400216EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218/*
219 * Destroy a list of credentials
220 */
221static inline
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400222void rpcauth_destroy_credlist(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 struct rpc_cred *cred;
225
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400226 while (!list_empty(head)) {
227 cred = list_entry(head->next, struct rpc_cred, cr_lru);
228 list_del_init(&cred->cr_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 put_rpccred(cred);
230 }
231}
232
233/*
234 * Clear the RPC credential cache, and delete those credentials
235 * that are not referenced.
236 */
237void
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400238rpcauth_clear_credcache(struct rpc_cred_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400240 LIST_HEAD(free);
241 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 struct rpc_cred *cred;
Trond Myklebust988664a2010-07-31 14:29:07 -0400243 unsigned int hashsize = 1U << cache->hashbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 int i;
245
246 spin_lock(&rpc_credcache_lock);
Trond Myklebust9499b432007-06-24 15:57:57 -0400247 spin_lock(&cache->lock);
Trond Myklebust988664a2010-07-31 14:29:07 -0400248 for (i = 0; i < hashsize; i++) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400249 head = &cache->hashtable[i];
250 while (!hlist_empty(head)) {
251 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
252 get_rpccred(cred);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400253 if (!list_empty(&cred->cr_lru)) {
254 list_del(&cred->cr_lru);
255 number_cred_unused--;
256 }
257 list_add_tail(&cred->cr_lru, &free);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400258 rpcauth_unhash_cred_locked(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 }
Trond Myklebust9499b432007-06-24 15:57:57 -0400261 spin_unlock(&cache->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 spin_unlock(&rpc_credcache_lock);
263 rpcauth_destroy_credlist(&free);
264}
265
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400266/*
267 * Destroy the RPC credential cache
268 */
269void
270rpcauth_destroy_credcache(struct rpc_auth *auth)
271{
272 struct rpc_cred_cache *cache = auth->au_credcache;
273
274 if (cache) {
275 auth->au_credcache = NULL;
276 rpcauth_clear_credcache(cache);
Trond Myklebust241269b2010-07-31 14:29:08 -0400277 kfree(cache->hashtable);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400278 kfree(cache);
279 }
280}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400281EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400282
Trond Myklebustd2b83142008-04-14 18:13:37 -0400283
284#define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/*
287 * Remove stale credentials. Avoid sleeping inside the loop.
288 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400289static int
290rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Trond Myklebust9499b432007-06-24 15:57:57 -0400292 spinlock_t *cache_lock;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400293 struct rpc_cred *cred, *next;
Trond Myklebustd2b83142008-04-14 18:13:37 -0400294 unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Trond Myklebusteac0d182008-10-28 15:21:41 -0400296 list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
297
Trond Myklebust20673402010-05-13 12:51:06 -0400298 if (nr_to_scan-- == 0)
299 break;
Trond Myklebust93a05e62010-05-13 12:51:06 -0400300 /*
301 * Enforce a 60 second garbage collection moratorium
302 * Note that the cred_unused list must be time-ordered.
303 */
Trond Myklebust3d7b0892010-04-22 15:35:55 -0400304 if (time_in_range(cred->cr_expire, expired, jiffies) &&
Trond Myklebusteac0d182008-10-28 15:21:41 -0400305 test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
Trond Myklebust93a05e62010-05-13 12:51:06 -0400306 return 0;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400307
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400308 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400309 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400310 if (atomic_read(&cred->cr_count) != 0)
311 continue;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400312
Trond Myklebust9499b432007-06-24 15:57:57 -0400313 cache_lock = &cred->cr_auth->au_credcache->lock;
314 spin_lock(cache_lock);
315 if (atomic_read(&cred->cr_count) == 0) {
316 get_rpccred(cred);
317 list_add_tail(&cred->cr_lru, free);
318 rpcauth_unhash_cred_locked(cred);
319 }
320 spin_unlock(cache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
Trond Myklebust93a05e62010-05-13 12:51:06 -0400322 return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400323}
324
325/*
Trond Myklebustf5c21872007-06-25 17:11:20 -0400326 * Run memory cache shrinker.
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400327 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400328static int
Dave Chinner567c7b02010-07-21 15:33:01 +1000329rpcauth_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400330{
Trond Myklebustf5c21872007-06-25 17:11:20 -0400331 LIST_HEAD(free);
332 int res;
333
Trond Myklebustd300a412010-05-13 12:51:03 -0400334 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
335 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400336 if (list_empty(&cred_unused))
337 return 0;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400338 spin_lock(&rpc_credcache_lock);
Trond Myklebust93a05e62010-05-13 12:51:06 -0400339 res = rpcauth_prune_expired(&free, nr_to_scan);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400340 spin_unlock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400341 rpcauth_destroy_credlist(&free);
342 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345/*
346 * Look up a process' credentials in the authentication cache
347 */
348struct rpc_cred *
349rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Trond Myklebust8a317762006-02-01 12:18:36 -0500350 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400352 LIST_HEAD(free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 struct rpc_cred_cache *cache = auth->au_credcache;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400354 struct hlist_node *pos;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400355 struct rpc_cred *cred = NULL,
356 *entry, *new;
Trond Myklebust25337fd2008-03-12 14:40:14 -0400357 unsigned int nr;
358
Trond Myklebust988664a2010-07-31 14:29:07 -0400359 nr = hash_long(acred->uid, cache->hashbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400361 rcu_read_lock();
362 hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) {
363 if (!entry->cr_ops->crmatch(acred, entry, flags))
364 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400365 spin_lock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400366 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
Trond Myklebust9499b432007-06-24 15:57:57 -0400367 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400368 continue;
369 }
370 cred = get_rpccred(entry);
Trond Myklebust9499b432007-06-24 15:57:57 -0400371 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400372 break;
373 }
374 rcu_read_unlock();
375
Trond Myklebust9499b432007-06-24 15:57:57 -0400376 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400377 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400378
379 new = auth->au_ops->crcreate(auth, acred, flags);
380 if (IS_ERR(new)) {
381 cred = new;
382 goto out;
383 }
384
Trond Myklebust9499b432007-06-24 15:57:57 -0400385 spin_lock(&cache->lock);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400386 hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) {
387 if (!entry->cr_ops->crmatch(acred, entry, flags))
388 continue;
389 cred = get_rpccred(entry);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400390 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400392 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400393 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400394 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
395 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
396 } else
397 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400398 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400399found:
Joe Perchesf64f9e72009-11-29 16:55:45 -0800400 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
401 cred->cr_ops->cr_init != NULL &&
402 !(flags & RPCAUTH_LOOKUP_NEW)) {
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500403 int res = cred->cr_ops->cr_init(auth, cred);
404 if (res < 0) {
405 put_rpccred(cred);
406 cred = ERR_PTR(res);
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400409 rpcauth_destroy_credlist(&free);
410out:
411 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400413EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500416rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
David Howells86a264a2008-11-14 10:39:18 +1100418 struct auth_cred acred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct rpc_cred *ret;
David Howells86a264a2008-11-14 10:39:18 +1100420 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Chuck Lever46121cf2007-01-31 12:14:08 -0500422 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 auth->au_ops->au_name);
David Howells86a264a2008-11-14 10:39:18 +1100424
425 memset(&acred, 0, sizeof(acred));
426 acred.uid = cred->fsuid;
427 acred.gid = cred->fsgid;
428 acred.group_info = get_group_info(((struct cred *)cred)->group_info);
429
Trond Myklebust8a317762006-02-01 12:18:36 -0500430 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 put_group_info(acred.group_info);
432 return ret;
433}
434
Trond Myklebust5fe47552007-06-23 19:55:31 -0400435void
436rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
437 struct rpc_auth *auth, const struct rpc_credops *ops)
438{
439 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400440 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400441 atomic_set(&cred->cr_count, 1);
442 cred->cr_auth = auth;
443 cred->cr_ops = ops;
444 cred->cr_expire = jiffies;
445#ifdef RPC_DEBUG
446 cred->cr_magic = RPCAUTH_CRED_MAGIC;
447#endif
448 cred->cr_uid = acred->uid;
449}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400450EXPORT_SYMBOL_GPL(rpcauth_init_cred);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400451
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400452struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400453rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400454{
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400455 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
456 cred->cr_auth->au_ops->au_name, cred);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400457 return get_rpccred(cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400458}
Trond Myklebust5c691042008-03-12 16:21:07 -0400459EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400460
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400461static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400462rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400464 struct rpc_auth *auth = task->tk_client->cl_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct auth_cred acred = {
Trond Myklebustaf093832008-03-12 12:12:16 -0400466 .uid = 0,
467 .gid = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Chuck Lever46121cf2007-01-31 12:14:08 -0500470 dprintk("RPC: %5u looking up %s cred\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400471 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400472 return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
Trond Myklebustaf093832008-03-12 12:12:16 -0400473}
474
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400475static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400476rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
Trond Myklebustaf093832008-03-12 12:12:16 -0400477{
478 struct rpc_auth *auth = task->tk_client->cl_auth;
Trond Myklebustaf093832008-03-12 12:12:16 -0400479
480 dprintk("RPC: %5u looking up %s cred\n",
481 task->tk_pid, auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400482 return rpcauth_lookupcred(auth, lookupflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Trond Myklebusta17c2152010-07-31 14:29:08 -0400485static int
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400486rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400488 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400489 struct rpc_cred *new;
Trond Myklebust5d351752009-09-15 13:32:13 -0400490 int lookupflags = 0;
491
492 if (flags & RPC_TASK_ASYNC)
493 lookupflags |= RPCAUTH_LOOKUP_NEW;
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400494 if (cred != NULL)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400495 new = cred->cr_ops->crbind(task, cred, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400496 else if (flags & RPC_TASK_ROOTCREDS)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400497 new = rpcauth_bind_root_cred(task, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400498 else
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400499 new = rpcauth_bind_new_cred(task, lookupflags);
500 if (IS_ERR(new))
501 return PTR_ERR(new);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400502 if (req->rq_cred != NULL)
503 put_rpccred(req->rq_cred);
504 req->rq_cred = new;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
508void
509put_rpccred(struct rpc_cred *cred)
510{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400511 /* Fast path for unhashed credentials */
Trond Myklebustf0380f32009-12-03 08:10:17 -0500512 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
513 if (atomic_dec_and_test(&cred->cr_count))
514 cred->cr_ops->crdestroy(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500516 }
517
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400518 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
519 return;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400520 if (!list_empty(&cred->cr_lru)) {
521 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400522 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400523 }
Trond Myklebust5f707eb2008-10-28 15:21:42 -0400524 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
Trond Myklebustf0380f32009-12-03 08:10:17 -0500525 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
526 cred->cr_expire = jiffies;
527 list_add_tail(&cred->cr_lru, &cred_unused);
528 number_cred_unused++;
529 goto out_nodestroy;
530 }
531 if (!rpcauth_unhash_cred(cred)) {
532 /* We were hashed and someone looked us up... */
533 goto out_nodestroy;
534 }
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400535 }
536 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 cred->cr_ops->crdestroy(cred);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500538 return;
539out_nodestroy:
540 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400542EXPORT_SYMBOL_GPL(put_rpccred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700544__be32 *
545rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400547 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Chuck Lever46121cf2007-01-31 12:14:08 -0500549 dprintk("RPC: %5u marshaling %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400550 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return cred->cr_ops->crmarshal(task, p);
553}
554
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700555__be32 *
556rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400558 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Chuck Lever46121cf2007-01-31 12:14:08 -0500560 dprintk("RPC: %5u validating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400561 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return cred->cr_ops->crvalidate(task, p);
564}
565
566int
567rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700568 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400570 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Chuck Lever46121cf2007-01-31 12:14:08 -0500572 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 task->tk_pid, cred->cr_ops->cr_name, cred);
574 if (cred->cr_ops->crwrap_req)
575 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
576 /* By default, we encode the arguments normally. */
Trond Myklebust88a9fe82008-12-23 15:21:31 -0500577 return encode(rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580int
581rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700582 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400584 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Chuck Lever46121cf2007-01-31 12:14:08 -0500586 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 task->tk_pid, cred->cr_ops->cr_name, cred);
588 if (cred->cr_ops->crunwrap_resp)
589 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
590 data, obj);
591 /* By default, we decode the arguments normally. */
Trond Myklebust88a9fe82008-12-23 15:21:31 -0500592 return decode(rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595int
596rpcauth_refreshcred(struct rpc_task *task)
597{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400598 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int err;
600
Trond Myklebusta17c2152010-07-31 14:29:08 -0400601 cred = task->tk_rqstp->rq_cred;
602 if (cred == NULL) {
603 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
604 if (err < 0)
605 goto out;
606 cred = task->tk_rqstp->rq_cred;
607 };
Chuck Lever46121cf2007-01-31 12:14:08 -0500608 dprintk("RPC: %5u refreshing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400609 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 err = cred->cr_ops->crrefresh(task);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400612out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (err < 0)
614 task->tk_status = err;
615 return err;
616}
617
618void
619rpcauth_invalcred(struct rpc_task *task)
620{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400621 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400622
Chuck Lever46121cf2007-01-31 12:14:08 -0500623 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400624 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400625 if (cred)
626 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629int
630rpcauth_uptodatecred(struct rpc_task *task)
631{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400632 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400633
634 return cred == NULL ||
635 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400637
Rusty Russell8e1f9362007-07-17 04:03:17 -0700638static struct shrinker rpc_cred_shrinker = {
639 .shrink = rpcauth_cache_shrinker,
640 .seeks = DEFAULT_SEEKS,
641};
Trond Myklebustf5c21872007-06-25 17:11:20 -0400642
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400643int __init rpcauth_init_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400644{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400645 int err;
646
647 err = rpc_init_authunix();
648 if (err < 0)
649 goto out1;
650 err = rpc_init_generic_auth();
651 if (err < 0)
652 goto out2;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700653 register_shrinker(&rpc_cred_shrinker);
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400654 return 0;
655out2:
656 rpc_destroy_authunix();
657out1:
658 return err;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400659}
660
661void __exit rpcauth_remove_module(void)
662{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400663 rpc_destroy_authunix();
664 rpc_destroy_generic_auth();
Rusty Russell8e1f9362007-07-17 04:03:17 -0700665 unregister_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400666}