blob: f5294047df77b03b0feae953405bfd5b41252b76 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/auth.c
3 *
4 * Generic RPC client authentication API.
5 *
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
8
9#include <linux/types.h>
10#include <linux/sched.h>
11#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/errno.h>
Trond Myklebust25337fd2008-03-12 14:40:14 -040014#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sunrpc/clnt.h>
Chuck Lever6a1a1e32012-07-11 16:31:08 -040016#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/spinlock.h>
18
19#ifdef RPC_DEBUG
20# define RPCDBG_FACILITY RPCDBG_AUTH
21#endif
22
Trond Myklebust241269b2010-07-31 14:29:08 -040023#define RPC_CREDCACHE_DEFAULT_HASHBITS (4)
24struct rpc_cred_cache {
25 struct hlist_head *hashtable;
26 unsigned int hashbits;
27 spinlock_t lock;
28};
29
30static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS;
31
Trond Myklebustfc1b3562007-06-09 16:15:46 -040032static DEFINE_SPINLOCK(rpc_authflavor_lock);
Trond Myklebustf1c0a862007-06-23 20:17:58 -040033static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 &authnull_ops, /* AUTH_NULL */
35 &authunix_ops, /* AUTH_UNIX */
36 NULL, /* others can be loadable modules */
37};
38
Trond Myklebuste092bdc2007-06-23 19:45:36 -040039static LIST_HEAD(cred_unused);
Trond Myklebustf5c21872007-06-25 17:11:20 -040040static unsigned long number_cred_unused;
Trond Myklebuste092bdc2007-06-23 19:45:36 -040041
Miquel van Smoorenburgdb5fe262010-09-12 19:55:26 -040042#define MAX_HASHTABLE_BITS (14)
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100043static int param_set_hashtbl_sz(const char *val, const struct kernel_param *kp)
Trond Myklebust241269b2010-07-31 14:29:08 -040044{
45 unsigned long num;
46 unsigned int nbits;
47 int ret;
48
49 if (!val)
50 goto out_inval;
51 ret = strict_strtoul(val, 0, &num);
52 if (ret == -EINVAL)
53 goto out_inval;
54 nbits = fls(num);
55 if (num > (1U << nbits))
56 nbits++;
57 if (nbits > MAX_HASHTABLE_BITS || nbits < 2)
58 goto out_inval;
59 *(unsigned int *)kp->arg = nbits;
60 return 0;
61out_inval:
62 return -EINVAL;
63}
64
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100065static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp)
Trond Myklebust241269b2010-07-31 14:29:08 -040066{
67 unsigned int nbits;
68
69 nbits = *(unsigned int *)kp->arg;
70 return sprintf(buffer, "%u", 1U << nbits);
71}
72
73#define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
74
Stephen Rothwell8e4e15d2010-08-04 12:11:22 +100075static struct kernel_param_ops param_ops_hashtbl_sz = {
76 .set = param_set_hashtbl_sz,
77 .get = param_get_hashtbl_sz,
78};
79
Trond Myklebust241269b2010-07-31 14:29:08 -040080module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
81MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static u32
84pseudoflavor_to_flavor(u32 flavor) {
85 if (flavor >= RPC_AUTH_MAXFLAVOR)
86 return RPC_AUTH_GSS;
87 return flavor;
88}
89
90int
Trond Myklebustf1c0a862007-06-23 20:17:58 -040091rpcauth_register(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040094 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
97 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -040098 spin_lock(&rpc_authflavor_lock);
99 if (auth_flavors[flavor] == NULL) {
100 auth_flavors[flavor] = ops;
101 ret = 0;
102 }
103 spin_unlock(&rpc_authflavor_lock);
104 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400106EXPORT_SYMBOL_GPL(rpcauth_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108int
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400109rpcauth_unregister(const struct rpc_authops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 rpc_authflavor_t flavor;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400112 int ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
115 return -EINVAL;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400116 spin_lock(&rpc_authflavor_lock);
117 if (auth_flavors[flavor] == ops) {
118 auth_flavors[flavor] = NULL;
119 ret = 0;
120 }
121 spin_unlock(&rpc_authflavor_lock);
122 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400124EXPORT_SYMBOL_GPL(rpcauth_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400126/**
127 * rpcauth_list_flavors - discover registered flavors and pseudoflavors
128 * @array: array to fill in
129 * @size: size of "array"
130 *
131 * Returns the number of array items filled in, or a negative errno.
132 *
133 * The returned array is not sorted by any policy. Callers should not
134 * rely on the order of the items in the returned array.
135 */
136int
137rpcauth_list_flavors(rpc_authflavor_t *array, int size)
138{
139 rpc_authflavor_t flavor;
140 int result = 0;
141
142 spin_lock(&rpc_authflavor_lock);
143 for (flavor = 0; flavor < RPC_AUTH_MAXFLAVOR; flavor++) {
144 const struct rpc_authops *ops = auth_flavors[flavor];
145 rpc_authflavor_t pseudos[4];
146 int i, len;
147
148 if (result >= size) {
149 result = -ENOMEM;
150 break;
151 }
152
153 if (ops == NULL)
154 continue;
155 if (ops->list_pseudoflavors == NULL) {
156 array[result++] = ops->au_flavor;
157 continue;
158 }
159 len = ops->list_pseudoflavors(pseudos, ARRAY_SIZE(pseudos));
160 if (len < 0) {
161 result = len;
162 break;
163 }
164 for (i = 0; i < len; i++) {
165 if (result >= size) {
166 result = -ENOMEM;
167 break;
168 }
169 array[result++] = pseudos[i];
170 }
171 }
172 spin_unlock(&rpc_authflavor_lock);
173
174 dprintk("RPC: %s returns %d\n", __func__, result);
175 return result;
176}
177EXPORT_SYMBOL_GPL(rpcauth_list_flavors);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179struct rpc_auth *
180rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
181{
182 struct rpc_auth *auth;
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400183 const struct rpc_authops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
185
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500186 auth = ERR_PTR(-EINVAL);
187 if (flavor >= RPC_AUTH_MAXFLAVOR)
188 goto out;
189
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500190 if ((ops = auth_flavors[flavor]) == NULL)
191 request_module("rpc-auth-%u", flavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400192 spin_lock(&rpc_authflavor_lock);
193 ops = auth_flavors[flavor];
194 if (ops == NULL || !try_module_get(ops->owner)) {
195 spin_unlock(&rpc_authflavor_lock);
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500196 goto out;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400197 }
198 spin_unlock(&rpc_authflavor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 auth = ops->create(clnt, pseudoflavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400200 module_put(ops->owner);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000201 if (IS_ERR(auth))
202 return auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (clnt->cl_auth)
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400204 rpcauth_release(clnt->cl_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 clnt->cl_auth = auth;
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500206
207out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return auth;
209}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400210EXPORT_SYMBOL_GPL(rpcauth_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212void
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400213rpcauth_release(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 if (!atomic_dec_and_test(&auth->au_count))
216 return;
217 auth->au_ops->destroy(auth);
218}
219
220static DEFINE_SPINLOCK(rpc_credcache_lock);
221
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400222static void
223rpcauth_unhash_cred_locked(struct rpc_cred *cred)
224{
225 hlist_del_rcu(&cred->cr_hash);
226 smp_mb__before_clear_bit();
227 clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
228}
229
Trond Myklebustf0380f32009-12-03 08:10:17 -0500230static int
Trond Myklebust9499b432007-06-24 15:57:57 -0400231rpcauth_unhash_cred(struct rpc_cred *cred)
232{
233 spinlock_t *cache_lock;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500234 int ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400235
236 cache_lock = &cred->cr_auth->au_credcache->lock;
237 spin_lock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500238 ret = atomic_read(&cred->cr_count) == 0;
239 if (ret)
Trond Myklebust9499b432007-06-24 15:57:57 -0400240 rpcauth_unhash_cred_locked(cred);
241 spin_unlock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500242 return ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/*
246 * Initialize RPC credential cache
247 */
248int
Trond Myklebustf5c21872007-06-25 17:11:20 -0400249rpcauth_init_credcache(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct rpc_cred_cache *new;
Trond Myklebust988664a2010-07-31 14:29:07 -0400252 unsigned int hashsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800254 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (!new)
Trond Myklebust241269b2010-07-31 14:29:08 -0400256 goto out_nocache;
257 new->hashbits = auth_hashbits;
Trond Myklebust988664a2010-07-31 14:29:07 -0400258 hashsize = 1U << new->hashbits;
Trond Myklebust241269b2010-07-31 14:29:08 -0400259 new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
260 if (!new->hashtable)
261 goto out_nohashtbl;
Trond Myklebust9499b432007-06-24 15:57:57 -0400262 spin_lock_init(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 auth->au_credcache = new;
264 return 0;
Trond Myklebust241269b2010-07-31 14:29:08 -0400265out_nohashtbl:
266 kfree(new);
267out_nocache:
268 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400270EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/*
273 * Destroy a list of credentials
274 */
275static inline
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400276void rpcauth_destroy_credlist(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 struct rpc_cred *cred;
279
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400280 while (!list_empty(head)) {
281 cred = list_entry(head->next, struct rpc_cred, cr_lru);
282 list_del_init(&cred->cr_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 put_rpccred(cred);
284 }
285}
286
287/*
288 * Clear the RPC credential cache, and delete those credentials
289 * that are not referenced.
290 */
291void
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400292rpcauth_clear_credcache(struct rpc_cred_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400294 LIST_HEAD(free);
295 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct rpc_cred *cred;
Trond Myklebust988664a2010-07-31 14:29:07 -0400297 unsigned int hashsize = 1U << cache->hashbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int i;
299
300 spin_lock(&rpc_credcache_lock);
Trond Myklebust9499b432007-06-24 15:57:57 -0400301 spin_lock(&cache->lock);
Trond Myklebust988664a2010-07-31 14:29:07 -0400302 for (i = 0; i < hashsize; i++) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400303 head = &cache->hashtable[i];
304 while (!hlist_empty(head)) {
305 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
306 get_rpccred(cred);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400307 if (!list_empty(&cred->cr_lru)) {
308 list_del(&cred->cr_lru);
309 number_cred_unused--;
310 }
311 list_add_tail(&cred->cr_lru, &free);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400312 rpcauth_unhash_cred_locked(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314 }
Trond Myklebust9499b432007-06-24 15:57:57 -0400315 spin_unlock(&cache->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 spin_unlock(&rpc_credcache_lock);
317 rpcauth_destroy_credlist(&free);
318}
319
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400320/*
321 * Destroy the RPC credential cache
322 */
323void
324rpcauth_destroy_credcache(struct rpc_auth *auth)
325{
326 struct rpc_cred_cache *cache = auth->au_credcache;
327
328 if (cache) {
329 auth->au_credcache = NULL;
330 rpcauth_clear_credcache(cache);
Trond Myklebust241269b2010-07-31 14:29:08 -0400331 kfree(cache->hashtable);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400332 kfree(cache);
333 }
334}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400335EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400336
Trond Myklebustd2b83142008-04-14 18:13:37 -0400337
338#define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/*
341 * Remove stale credentials. Avoid sleeping inside the loop.
342 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400343static int
344rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Trond Myklebust9499b432007-06-24 15:57:57 -0400346 spinlock_t *cache_lock;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400347 struct rpc_cred *cred, *next;
Trond Myklebustd2b83142008-04-14 18:13:37 -0400348 unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Trond Myklebusteac0d182008-10-28 15:21:41 -0400350 list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
351
Trond Myklebust20673402010-05-13 12:51:06 -0400352 if (nr_to_scan-- == 0)
353 break;
Trond Myklebust93a05e62010-05-13 12:51:06 -0400354 /*
355 * Enforce a 60 second garbage collection moratorium
356 * Note that the cred_unused list must be time-ordered.
357 */
Trond Myklebust3d7b0892010-04-22 15:35:55 -0400358 if (time_in_range(cred->cr_expire, expired, jiffies) &&
Trond Myklebusteac0d182008-10-28 15:21:41 -0400359 test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
Trond Myklebust93a05e62010-05-13 12:51:06 -0400360 return 0;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400361
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400362 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400363 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400364 if (atomic_read(&cred->cr_count) != 0)
365 continue;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400366
Trond Myklebust9499b432007-06-24 15:57:57 -0400367 cache_lock = &cred->cr_auth->au_credcache->lock;
368 spin_lock(cache_lock);
369 if (atomic_read(&cred->cr_count) == 0) {
370 get_rpccred(cred);
371 list_add_tail(&cred->cr_lru, free);
372 rpcauth_unhash_cred_locked(cred);
373 }
374 spin_unlock(cache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Trond Myklebust93a05e62010-05-13 12:51:06 -0400376 return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400377}
378
379/*
Trond Myklebustf5c21872007-06-25 17:11:20 -0400380 * Run memory cache shrinker.
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400381 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400382static int
Ying Han1495f232011-05-24 17:12:27 -0700383rpcauth_cache_shrinker(struct shrinker *shrink, struct shrink_control *sc)
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400384{
Trond Myklebustf5c21872007-06-25 17:11:20 -0400385 LIST_HEAD(free);
386 int res;
Ying Han1495f232011-05-24 17:12:27 -0700387 int nr_to_scan = sc->nr_to_scan;
388 gfp_t gfp_mask = sc->gfp_mask;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400389
Trond Myklebustd300a412010-05-13 12:51:03 -0400390 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
391 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400392 if (list_empty(&cred_unused))
393 return 0;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400394 spin_lock(&rpc_credcache_lock);
Trond Myklebust93a05e62010-05-13 12:51:06 -0400395 res = rpcauth_prune_expired(&free, nr_to_scan);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400396 spin_unlock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400397 rpcauth_destroy_credlist(&free);
398 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401/*
402 * Look up a process' credentials in the authentication cache
403 */
404struct rpc_cred *
405rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Trond Myklebust8a317762006-02-01 12:18:36 -0500406 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400408 LIST_HEAD(free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct rpc_cred_cache *cache = auth->au_credcache;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400410 struct rpc_cred *cred = NULL,
411 *entry, *new;
Trond Myklebust25337fd2008-03-12 14:40:14 -0400412 unsigned int nr;
413
Eric W. Biederman9e469e32013-02-02 01:57:30 -0800414 nr = hash_long(from_kuid(&init_user_ns, acred->uid), cache->hashbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400416 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800417 hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400418 if (!entry->cr_ops->crmatch(acred, entry, flags))
419 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400420 spin_lock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400421 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
Trond Myklebust9499b432007-06-24 15:57:57 -0400422 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400423 continue;
424 }
425 cred = get_rpccred(entry);
Trond Myklebust9499b432007-06-24 15:57:57 -0400426 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400427 break;
428 }
429 rcu_read_unlock();
430
Trond Myklebust9499b432007-06-24 15:57:57 -0400431 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400432 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400433
434 new = auth->au_ops->crcreate(auth, acred, flags);
435 if (IS_ERR(new)) {
436 cred = new;
437 goto out;
438 }
439
Trond Myklebust9499b432007-06-24 15:57:57 -0400440 spin_lock(&cache->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800441 hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400442 if (!entry->cr_ops->crmatch(acred, entry, flags))
443 continue;
444 cred = get_rpccred(entry);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400447 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400448 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400449 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
450 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
451 } else
452 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400453 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400454found:
Joe Perchesf64f9e72009-11-29 16:55:45 -0800455 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
456 cred->cr_ops->cr_init != NULL &&
457 !(flags & RPCAUTH_LOOKUP_NEW)) {
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500458 int res = cred->cr_ops->cr_init(auth, cred);
459 if (res < 0) {
460 put_rpccred(cred);
461 cred = ERR_PTR(res);
462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400464 rpcauth_destroy_credlist(&free);
465out:
466 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400468EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500471rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
David Howells86a264a2008-11-14 10:39:18 +1100473 struct auth_cred acred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct rpc_cred *ret;
David Howells86a264a2008-11-14 10:39:18 +1100475 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Chuck Lever46121cf2007-01-31 12:14:08 -0500477 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 auth->au_ops->au_name);
David Howells86a264a2008-11-14 10:39:18 +1100479
480 memset(&acred, 0, sizeof(acred));
481 acred.uid = cred->fsuid;
482 acred.gid = cred->fsgid;
483 acred.group_info = get_group_info(((struct cred *)cred)->group_info);
484
Trond Myklebust8a317762006-02-01 12:18:36 -0500485 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 put_group_info(acred.group_info);
487 return ret;
488}
489
Trond Myklebust5fe47552007-06-23 19:55:31 -0400490void
491rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
492 struct rpc_auth *auth, const struct rpc_credops *ops)
493{
494 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400495 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400496 atomic_set(&cred->cr_count, 1);
497 cred->cr_auth = auth;
498 cred->cr_ops = ops;
499 cred->cr_expire = jiffies;
500#ifdef RPC_DEBUG
501 cred->cr_magic = RPCAUTH_CRED_MAGIC;
502#endif
503 cred->cr_uid = acred->uid;
504}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400505EXPORT_SYMBOL_GPL(rpcauth_init_cred);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400506
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400507struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400508rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400509{
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400510 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
511 cred->cr_auth->au_ops->au_name, cred);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400512 return get_rpccred(cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400513}
Trond Myklebust5c691042008-03-12 16:21:07 -0400514EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400515
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400516static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400517rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400519 struct rpc_auth *auth = task->tk_client->cl_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct auth_cred acred = {
Eric W. Biedermanbf37f792013-02-01 15:55:38 -0800521 .uid = GLOBAL_ROOT_UID,
522 .gid = GLOBAL_ROOT_GID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Chuck Lever46121cf2007-01-31 12:14:08 -0500525 dprintk("RPC: %5u looking up %s cred\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400526 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400527 return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
Trond Myklebustaf093832008-03-12 12:12:16 -0400528}
529
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400530static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400531rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
Trond Myklebustaf093832008-03-12 12:12:16 -0400532{
533 struct rpc_auth *auth = task->tk_client->cl_auth;
Trond Myklebustaf093832008-03-12 12:12:16 -0400534
535 dprintk("RPC: %5u looking up %s cred\n",
536 task->tk_pid, auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400537 return rpcauth_lookupcred(auth, lookupflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Trond Myklebusta17c2152010-07-31 14:29:08 -0400540static int
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400541rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400543 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400544 struct rpc_cred *new;
Trond Myklebust5d351752009-09-15 13:32:13 -0400545 int lookupflags = 0;
546
547 if (flags & RPC_TASK_ASYNC)
548 lookupflags |= RPCAUTH_LOOKUP_NEW;
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400549 if (cred != NULL)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400550 new = cred->cr_ops->crbind(task, cred, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400551 else if (flags & RPC_TASK_ROOTCREDS)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400552 new = rpcauth_bind_root_cred(task, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400553 else
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400554 new = rpcauth_bind_new_cred(task, lookupflags);
555 if (IS_ERR(new))
556 return PTR_ERR(new);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400557 if (req->rq_cred != NULL)
558 put_rpccred(req->rq_cred);
559 req->rq_cred = new;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400560 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563void
564put_rpccred(struct rpc_cred *cred)
565{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400566 /* Fast path for unhashed credentials */
Trond Myklebustf0380f32009-12-03 08:10:17 -0500567 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
568 if (atomic_dec_and_test(&cred->cr_count))
569 cred->cr_ops->crdestroy(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500571 }
572
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400573 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
574 return;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400575 if (!list_empty(&cred->cr_lru)) {
576 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400577 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400578 }
Trond Myklebust5f707eb2008-10-28 15:21:42 -0400579 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
Trond Myklebustf0380f32009-12-03 08:10:17 -0500580 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
581 cred->cr_expire = jiffies;
582 list_add_tail(&cred->cr_lru, &cred_unused);
583 number_cred_unused++;
584 goto out_nodestroy;
585 }
586 if (!rpcauth_unhash_cred(cred)) {
587 /* We were hashed and someone looked us up... */
588 goto out_nodestroy;
589 }
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400590 }
591 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 cred->cr_ops->crdestroy(cred);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500593 return;
594out_nodestroy:
595 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400597EXPORT_SYMBOL_GPL(put_rpccred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700599__be32 *
600rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400602 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Chuck Lever46121cf2007-01-31 12:14:08 -0500604 dprintk("RPC: %5u marshaling %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400605 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return cred->cr_ops->crmarshal(task, p);
608}
609
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700610__be32 *
611rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400613 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Chuck Lever46121cf2007-01-31 12:14:08 -0500615 dprintk("RPC: %5u validating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400616 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return cred->cr_ops->crvalidate(task, p);
619}
620
Chuck Lever9f06c712010-12-14 14:59:18 +0000621static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
622 __be32 *data, void *obj)
623{
624 struct xdr_stream xdr;
625
626 xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
627 encode(rqstp, &xdr, obj);
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630int
Chuck Lever9f06c712010-12-14 14:59:18 +0000631rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700632 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400634 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Chuck Lever46121cf2007-01-31 12:14:08 -0500636 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 task->tk_pid, cred->cr_ops->cr_name, cred);
638 if (cred->cr_ops->crwrap_req)
639 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
640 /* By default, we encode the arguments normally. */
Chuck Lever9f06c712010-12-14 14:59:18 +0000641 rpcauth_wrap_req_encode(encode, rqstp, data, obj);
642 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Chuck Leverbf269552010-12-14 14:59:29 +0000645static int
646rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
647 __be32 *data, void *obj)
648{
649 struct xdr_stream xdr;
650
651 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
652 return decode(rqstp, &xdr, obj);
653}
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655int
Chuck Leverbf269552010-12-14 14:59:29 +0000656rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700657 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400659 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Chuck Lever46121cf2007-01-31 12:14:08 -0500661 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 task->tk_pid, cred->cr_ops->cr_name, cred);
663 if (cred->cr_ops->crunwrap_resp)
664 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
665 data, obj);
666 /* By default, we decode the arguments normally. */
Chuck Leverbf269552010-12-14 14:59:29 +0000667 return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670int
671rpcauth_refreshcred(struct rpc_task *task)
672{
Trond Myklebust9a84d382010-10-24 18:00:46 -0400673 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 int err;
675
Trond Myklebusta17c2152010-07-31 14:29:08 -0400676 cred = task->tk_rqstp->rq_cred;
677 if (cred == NULL) {
678 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
679 if (err < 0)
680 goto out;
681 cred = task->tk_rqstp->rq_cred;
Joe Perchesf81c6222011-06-03 11:51:19 +0000682 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500683 dprintk("RPC: %5u refreshing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400684 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 err = cred->cr_ops->crrefresh(task);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400687out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (err < 0)
689 task->tk_status = err;
690 return err;
691}
692
693void
694rpcauth_invalcred(struct rpc_task *task)
695{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400696 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400697
Chuck Lever46121cf2007-01-31 12:14:08 -0500698 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400699 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400700 if (cred)
701 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704int
705rpcauth_uptodatecred(struct rpc_task *task)
706{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400707 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400708
709 return cred == NULL ||
710 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400712
Rusty Russell8e1f9362007-07-17 04:03:17 -0700713static struct shrinker rpc_cred_shrinker = {
714 .shrink = rpcauth_cache_shrinker,
715 .seeks = DEFAULT_SEEKS,
716};
Trond Myklebustf5c21872007-06-25 17:11:20 -0400717
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400718int __init rpcauth_init_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400719{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400720 int err;
721
722 err = rpc_init_authunix();
723 if (err < 0)
724 goto out1;
725 err = rpc_init_generic_auth();
726 if (err < 0)
727 goto out2;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700728 register_shrinker(&rpc_cred_shrinker);
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400729 return 0;
730out2:
731 rpc_destroy_authunix();
732out1:
733 return err;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400734}
735
Stephen Rothwellc135e842010-09-29 14:16:57 +1000736void rpcauth_remove_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400737{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400738 rpc_destroy_authunix();
739 rpc_destroy_generic_auth();
Rusty Russell8e1f9362007-07-17 04:03:17 -0700740 unregister_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400741}