blob: cd6e4aa19dbfca0de4d54ea5bf6bf5e0d1a79d9f [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
Miquel van Smoorenburgdb5fe262010-09-12 19:55:26 -040041#define MAX_HASHTABLE_BITS (14)
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
Ying Han1495f232011-05-24 17:12:27 -0700329rpcauth_cache_shrinker(struct shrinker *shrink, struct shrink_control *sc)
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400330{
Trond Myklebustf5c21872007-06-25 17:11:20 -0400331 LIST_HEAD(free);
332 int res;
Ying Han1495f232011-05-24 17:12:27 -0700333 int nr_to_scan = sc->nr_to_scan;
334 gfp_t gfp_mask = sc->gfp_mask;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400335
Trond Myklebustd300a412010-05-13 12:51:03 -0400336 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
337 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400338 if (list_empty(&cred_unused))
339 return 0;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400340 spin_lock(&rpc_credcache_lock);
Trond Myklebust93a05e62010-05-13 12:51:06 -0400341 res = rpcauth_prune_expired(&free, nr_to_scan);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400342 spin_unlock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400343 rpcauth_destroy_credlist(&free);
344 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347/*
348 * Look up a process' credentials in the authentication cache
349 */
350struct rpc_cred *
351rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Trond Myklebust8a317762006-02-01 12:18:36 -0500352 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400354 LIST_HEAD(free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct rpc_cred_cache *cache = auth->au_credcache;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400356 struct hlist_node *pos;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400357 struct rpc_cred *cred = NULL,
358 *entry, *new;
Trond Myklebust25337fd2008-03-12 14:40:14 -0400359 unsigned int nr;
360
Trond Myklebust988664a2010-07-31 14:29:07 -0400361 nr = hash_long(acred->uid, cache->hashbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400363 rcu_read_lock();
364 hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) {
365 if (!entry->cr_ops->crmatch(acred, entry, flags))
366 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400367 spin_lock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400368 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
Trond Myklebust9499b432007-06-24 15:57:57 -0400369 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400370 continue;
371 }
372 cred = get_rpccred(entry);
Trond Myklebust9499b432007-06-24 15:57:57 -0400373 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400374 break;
375 }
376 rcu_read_unlock();
377
Trond Myklebust9499b432007-06-24 15:57:57 -0400378 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400379 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400380
381 new = auth->au_ops->crcreate(auth, acred, flags);
382 if (IS_ERR(new)) {
383 cred = new;
384 goto out;
385 }
386
Trond Myklebust9499b432007-06-24 15:57:57 -0400387 spin_lock(&cache->lock);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400388 hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) {
389 if (!entry->cr_ops->crmatch(acred, entry, flags))
390 continue;
391 cred = get_rpccred(entry);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400392 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400394 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400395 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400396 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
397 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
398 } else
399 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400400 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400401found:
Joe Perchesf64f9e72009-11-29 16:55:45 -0800402 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
403 cred->cr_ops->cr_init != NULL &&
404 !(flags & RPCAUTH_LOOKUP_NEW)) {
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500405 int res = cred->cr_ops->cr_init(auth, cred);
406 if (res < 0) {
407 put_rpccred(cred);
408 cred = ERR_PTR(res);
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400411 rpcauth_destroy_credlist(&free);
412out:
413 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400415EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500418rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
David Howells86a264a2008-11-14 10:39:18 +1100420 struct auth_cred acred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct rpc_cred *ret;
David Howells86a264a2008-11-14 10:39:18 +1100422 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Chuck Lever46121cf2007-01-31 12:14:08 -0500424 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 auth->au_ops->au_name);
David Howells86a264a2008-11-14 10:39:18 +1100426
427 memset(&acred, 0, sizeof(acred));
428 acred.uid = cred->fsuid;
429 acred.gid = cred->fsgid;
430 acred.group_info = get_group_info(((struct cred *)cred)->group_info);
431
Trond Myklebust8a317762006-02-01 12:18:36 -0500432 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 put_group_info(acred.group_info);
434 return ret;
435}
436
Trond Myklebust5fe47552007-06-23 19:55:31 -0400437void
438rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
439 struct rpc_auth *auth, const struct rpc_credops *ops)
440{
441 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400442 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400443 atomic_set(&cred->cr_count, 1);
444 cred->cr_auth = auth;
445 cred->cr_ops = ops;
446 cred->cr_expire = jiffies;
447#ifdef RPC_DEBUG
448 cred->cr_magic = RPCAUTH_CRED_MAGIC;
449#endif
450 cred->cr_uid = acred->uid;
451}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400452EXPORT_SYMBOL_GPL(rpcauth_init_cred);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400453
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400454struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400455rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400456{
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400457 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
458 cred->cr_auth->au_ops->au_name, cred);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400459 return get_rpccred(cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400460}
Trond Myklebust5c691042008-03-12 16:21:07 -0400461EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400462
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400463static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400464rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400466 struct rpc_auth *auth = task->tk_client->cl_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct auth_cred acred = {
Trond Myklebustaf093832008-03-12 12:12:16 -0400468 .uid = 0,
469 .gid = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Chuck Lever46121cf2007-01-31 12:14:08 -0500472 dprintk("RPC: %5u looking up %s cred\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400473 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400474 return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
Trond Myklebustaf093832008-03-12 12:12:16 -0400475}
476
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400477static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400478rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
Trond Myklebustaf093832008-03-12 12:12:16 -0400479{
480 struct rpc_auth *auth = task->tk_client->cl_auth;
Trond Myklebustaf093832008-03-12 12:12:16 -0400481
482 dprintk("RPC: %5u looking up %s cred\n",
483 task->tk_pid, auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400484 return rpcauth_lookupcred(auth, lookupflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Trond Myklebusta17c2152010-07-31 14:29:08 -0400487static int
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400488rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400490 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400491 struct rpc_cred *new;
Trond Myklebust5d351752009-09-15 13:32:13 -0400492 int lookupflags = 0;
493
494 if (flags & RPC_TASK_ASYNC)
495 lookupflags |= RPCAUTH_LOOKUP_NEW;
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400496 if (cred != NULL)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400497 new = cred->cr_ops->crbind(task, cred, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400498 else if (flags & RPC_TASK_ROOTCREDS)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400499 new = rpcauth_bind_root_cred(task, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400500 else
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400501 new = rpcauth_bind_new_cred(task, lookupflags);
502 if (IS_ERR(new))
503 return PTR_ERR(new);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400504 if (req->rq_cred != NULL)
505 put_rpccred(req->rq_cred);
506 req->rq_cred = new;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510void
511put_rpccred(struct rpc_cred *cred)
512{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400513 /* Fast path for unhashed credentials */
Trond Myklebustf0380f32009-12-03 08:10:17 -0500514 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
515 if (atomic_dec_and_test(&cred->cr_count))
516 cred->cr_ops->crdestroy(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500518 }
519
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400520 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
521 return;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400522 if (!list_empty(&cred->cr_lru)) {
523 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400524 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400525 }
Trond Myklebust5f707eb2008-10-28 15:21:42 -0400526 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
Trond Myklebustf0380f32009-12-03 08:10:17 -0500527 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
528 cred->cr_expire = jiffies;
529 list_add_tail(&cred->cr_lru, &cred_unused);
530 number_cred_unused++;
531 goto out_nodestroy;
532 }
533 if (!rpcauth_unhash_cred(cred)) {
534 /* We were hashed and someone looked us up... */
535 goto out_nodestroy;
536 }
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400537 }
538 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 cred->cr_ops->crdestroy(cred);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500540 return;
541out_nodestroy:
542 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400544EXPORT_SYMBOL_GPL(put_rpccred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700546__be32 *
547rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400549 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Chuck Lever46121cf2007-01-31 12:14:08 -0500551 dprintk("RPC: %5u marshaling %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400552 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return cred->cr_ops->crmarshal(task, p);
555}
556
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700557__be32 *
558rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400560 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Chuck Lever46121cf2007-01-31 12:14:08 -0500562 dprintk("RPC: %5u validating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400563 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return cred->cr_ops->crvalidate(task, p);
566}
567
Chuck Lever9f06c712010-12-14 14:59:18 +0000568static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
569 __be32 *data, void *obj)
570{
571 struct xdr_stream xdr;
572
573 xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
574 encode(rqstp, &xdr, obj);
575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577int
Chuck Lever9f06c712010-12-14 14:59:18 +0000578rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700579 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400581 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Chuck Lever46121cf2007-01-31 12:14:08 -0500583 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 task->tk_pid, cred->cr_ops->cr_name, cred);
585 if (cred->cr_ops->crwrap_req)
586 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
587 /* By default, we encode the arguments normally. */
Chuck Lever9f06c712010-12-14 14:59:18 +0000588 rpcauth_wrap_req_encode(encode, rqstp, data, obj);
589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Chuck Leverbf269552010-12-14 14:59:29 +0000592static int
593rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
594 __be32 *data, void *obj)
595{
596 struct xdr_stream xdr;
597
598 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
599 return decode(rqstp, &xdr, obj);
600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602int
Chuck Leverbf269552010-12-14 14:59:29 +0000603rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700604 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400606 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Chuck Lever46121cf2007-01-31 12:14:08 -0500608 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 task->tk_pid, cred->cr_ops->cr_name, cred);
610 if (cred->cr_ops->crunwrap_resp)
611 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
612 data, obj);
613 /* By default, we decode the arguments normally. */
Chuck Leverbf269552010-12-14 14:59:29 +0000614 return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617int
618rpcauth_refreshcred(struct rpc_task *task)
619{
Trond Myklebust9a84d382010-10-24 18:00:46 -0400620 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 int err;
622
Trond Myklebusta17c2152010-07-31 14:29:08 -0400623 cred = task->tk_rqstp->rq_cred;
624 if (cred == NULL) {
625 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
626 if (err < 0)
627 goto out;
628 cred = task->tk_rqstp->rq_cred;
629 };
Chuck Lever46121cf2007-01-31 12:14:08 -0500630 dprintk("RPC: %5u refreshing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400631 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 err = cred->cr_ops->crrefresh(task);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400634out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (err < 0)
636 task->tk_status = err;
637 return err;
638}
639
640void
641rpcauth_invalcred(struct rpc_task *task)
642{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400643 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400644
Chuck Lever46121cf2007-01-31 12:14:08 -0500645 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400646 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400647 if (cred)
648 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651int
652rpcauth_uptodatecred(struct rpc_task *task)
653{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400654 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400655
656 return cred == NULL ||
657 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400659
Rusty Russell8e1f9362007-07-17 04:03:17 -0700660static struct shrinker rpc_cred_shrinker = {
661 .shrink = rpcauth_cache_shrinker,
662 .seeks = DEFAULT_SEEKS,
663};
Trond Myklebustf5c21872007-06-25 17:11:20 -0400664
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400665int __init rpcauth_init_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400666{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400667 int err;
668
669 err = rpc_init_authunix();
670 if (err < 0)
671 goto out1;
672 err = rpc_init_generic_auth();
673 if (err < 0)
674 goto out2;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700675 register_shrinker(&rpc_cred_shrinker);
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400676 return 0;
677out2:
678 rpc_destroy_authunix();
679out1:
680 return err;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400681}
682
Stephen Rothwellc135e842010-09-29 14:16:57 +1000683void rpcauth_remove_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400684{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400685 rpc_destroy_authunix();
686 rpc_destroy_generic_auth();
Rusty Russell8e1f9362007-07-17 04:03:17 -0700687 unregister_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400688}