blob: 2bc0cc2196e030adbec8cdcb37e40c6b72130209 [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/**
Chuck Lever9568c5e2013-03-16 15:54:43 -0400127 * rpcauth_get_pseudoflavor - check if security flavor is supported
128 * @flavor: a security flavor
129 * @info: a GSS mech OID, quality of protection, and service value
130 *
131 * Verifies that an appropriate kernel module is available or already loaded.
132 * Returns an equivalent pseudoflavor, or RPC_AUTH_MAXFLAVOR if "flavor" is
133 * not supported locally.
134 */
135rpc_authflavor_t
136rpcauth_get_pseudoflavor(rpc_authflavor_t flavor, struct rpcsec_gss_info *info)
137{
138 const struct rpc_authops *ops;
139 rpc_authflavor_t pseudoflavor;
140
141 ops = auth_flavors[flavor];
142 if (ops == NULL)
143 request_module("rpc-auth-%u", flavor);
144 spin_lock(&rpc_authflavor_lock);
145 ops = auth_flavors[flavor];
146 if (ops == NULL || !try_module_get(ops->owner)) {
147 spin_unlock(&rpc_authflavor_lock);
148 return RPC_AUTH_MAXFLAVOR;
149 }
150 spin_unlock(&rpc_authflavor_lock);
151
152 pseudoflavor = flavor;
153 if (ops->info2flavor != NULL)
154 pseudoflavor = ops->info2flavor(info);
155
156 module_put(ops->owner);
157 return pseudoflavor;
158}
159EXPORT_SYMBOL_GPL(rpcauth_get_pseudoflavor);
160
161/**
Chuck Levera77c8062013-03-16 15:55:10 -0400162 * rpcauth_get_gssinfo - find GSS tuple matching a GSS pseudoflavor
163 * @pseudoflavor: GSS pseudoflavor to match
164 * @info: rpcsec_gss_info structure to fill in
165 *
166 * Returns zero and fills in "info" if pseudoflavor matches a
167 * supported mechanism.
168 */
169int
170rpcauth_get_gssinfo(rpc_authflavor_t pseudoflavor, struct rpcsec_gss_info *info)
171{
172 rpc_authflavor_t flavor = pseudoflavor_to_flavor(pseudoflavor);
173 const struct rpc_authops *ops;
174 int result;
175
176 ops = auth_flavors[flavor];
177 if (ops == NULL)
178 request_module("rpc-auth-%u", flavor);
179 spin_lock(&rpc_authflavor_lock);
180 ops = auth_flavors[flavor];
181 if (ops == NULL || !try_module_get(ops->owner)) {
182 spin_unlock(&rpc_authflavor_lock);
183 return -ENOENT;
184 }
185 spin_unlock(&rpc_authflavor_lock);
186
187 result = -ENOENT;
188 if (ops->flavor2info != NULL)
189 result = ops->flavor2info(pseudoflavor, info);
190
191 module_put(ops->owner);
192 return result;
193}
194EXPORT_SYMBOL_GPL(rpcauth_get_gssinfo);
195
196/**
Chuck Lever6a1a1e32012-07-11 16:31:08 -0400197 * rpcauth_list_flavors - discover registered flavors and pseudoflavors
198 * @array: array to fill in
199 * @size: size of "array"
200 *
201 * Returns the number of array items filled in, or a negative errno.
202 *
203 * The returned array is not sorted by any policy. Callers should not
204 * rely on the order of the items in the returned array.
205 */
206int
207rpcauth_list_flavors(rpc_authflavor_t *array, int size)
208{
209 rpc_authflavor_t flavor;
210 int result = 0;
211
212 spin_lock(&rpc_authflavor_lock);
213 for (flavor = 0; flavor < RPC_AUTH_MAXFLAVOR; flavor++) {
214 const struct rpc_authops *ops = auth_flavors[flavor];
215 rpc_authflavor_t pseudos[4];
216 int i, len;
217
218 if (result >= size) {
219 result = -ENOMEM;
220 break;
221 }
222
223 if (ops == NULL)
224 continue;
225 if (ops->list_pseudoflavors == NULL) {
226 array[result++] = ops->au_flavor;
227 continue;
228 }
229 len = ops->list_pseudoflavors(pseudos, ARRAY_SIZE(pseudos));
230 if (len < 0) {
231 result = len;
232 break;
233 }
234 for (i = 0; i < len; i++) {
235 if (result >= size) {
236 result = -ENOMEM;
237 break;
238 }
239 array[result++] = pseudos[i];
240 }
241 }
242 spin_unlock(&rpc_authflavor_lock);
243
244 dprintk("RPC: %s returns %d\n", __func__, result);
245 return result;
246}
247EXPORT_SYMBOL_GPL(rpcauth_list_flavors);
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249struct rpc_auth *
250rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
251{
252 struct rpc_auth *auth;
Trond Myklebustf1c0a862007-06-23 20:17:58 -0400253 const struct rpc_authops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
255
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500256 auth = ERR_PTR(-EINVAL);
257 if (flavor >= RPC_AUTH_MAXFLAVOR)
258 goto out;
259
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500260 if ((ops = auth_flavors[flavor]) == NULL)
261 request_module("rpc-auth-%u", flavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400262 spin_lock(&rpc_authflavor_lock);
263 ops = auth_flavors[flavor];
264 if (ops == NULL || !try_module_get(ops->owner)) {
265 spin_unlock(&rpc_authflavor_lock);
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500266 goto out;
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400267 }
268 spin_unlock(&rpc_authflavor_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 auth = ops->create(clnt, pseudoflavor);
Trond Myklebustfc1b3562007-06-09 16:15:46 -0400270 module_put(ops->owner);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000271 if (IS_ERR(auth))
272 return auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (clnt->cl_auth)
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400274 rpcauth_release(clnt->cl_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 clnt->cl_auth = auth;
Olaf Kirchf344f6d2006-03-20 13:44:08 -0500276
277out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return auth;
279}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400280EXPORT_SYMBOL_GPL(rpcauth_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282void
Trond Myklebustde7a8ce2007-06-23 10:46:47 -0400283rpcauth_release(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 if (!atomic_dec_and_test(&auth->au_count))
286 return;
287 auth->au_ops->destroy(auth);
288}
289
290static DEFINE_SPINLOCK(rpc_credcache_lock);
291
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400292static void
293rpcauth_unhash_cred_locked(struct rpc_cred *cred)
294{
295 hlist_del_rcu(&cred->cr_hash);
296 smp_mb__before_clear_bit();
297 clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
298}
299
Trond Myklebustf0380f32009-12-03 08:10:17 -0500300static int
Trond Myklebust9499b432007-06-24 15:57:57 -0400301rpcauth_unhash_cred(struct rpc_cred *cred)
302{
303 spinlock_t *cache_lock;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500304 int ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400305
306 cache_lock = &cred->cr_auth->au_credcache->lock;
307 spin_lock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500308 ret = atomic_read(&cred->cr_count) == 0;
309 if (ret)
Trond Myklebust9499b432007-06-24 15:57:57 -0400310 rpcauth_unhash_cred_locked(cred);
311 spin_unlock(cache_lock);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500312 return ret;
Trond Myklebust9499b432007-06-24 15:57:57 -0400313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/*
316 * Initialize RPC credential cache
317 */
318int
Trond Myklebustf5c21872007-06-25 17:11:20 -0400319rpcauth_init_credcache(struct rpc_auth *auth)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 struct rpc_cred_cache *new;
Trond Myklebust988664a2010-07-31 14:29:07 -0400322 unsigned int hashsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800324 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (!new)
Trond Myklebust241269b2010-07-31 14:29:08 -0400326 goto out_nocache;
327 new->hashbits = auth_hashbits;
Trond Myklebust988664a2010-07-31 14:29:07 -0400328 hashsize = 1U << new->hashbits;
Trond Myklebust241269b2010-07-31 14:29:08 -0400329 new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
330 if (!new->hashtable)
331 goto out_nohashtbl;
Trond Myklebust9499b432007-06-24 15:57:57 -0400332 spin_lock_init(&new->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 auth->au_credcache = new;
334 return 0;
Trond Myklebust241269b2010-07-31 14:29:08 -0400335out_nohashtbl:
336 kfree(new);
337out_nocache:
338 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400340EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342/*
343 * Destroy a list of credentials
344 */
345static inline
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400346void rpcauth_destroy_credlist(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct rpc_cred *cred;
349
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400350 while (!list_empty(head)) {
351 cred = list_entry(head->next, struct rpc_cred, cr_lru);
352 list_del_init(&cred->cr_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 put_rpccred(cred);
354 }
355}
356
357/*
358 * Clear the RPC credential cache, and delete those credentials
359 * that are not referenced.
360 */
361void
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400362rpcauth_clear_credcache(struct rpc_cred_cache *cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400364 LIST_HEAD(free);
365 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 struct rpc_cred *cred;
Trond Myklebust988664a2010-07-31 14:29:07 -0400367 unsigned int hashsize = 1U << cache->hashbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int i;
369
370 spin_lock(&rpc_credcache_lock);
Trond Myklebust9499b432007-06-24 15:57:57 -0400371 spin_lock(&cache->lock);
Trond Myklebust988664a2010-07-31 14:29:07 -0400372 for (i = 0; i < hashsize; i++) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400373 head = &cache->hashtable[i];
374 while (!hlist_empty(head)) {
375 cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
376 get_rpccred(cred);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400377 if (!list_empty(&cred->cr_lru)) {
378 list_del(&cred->cr_lru);
379 number_cred_unused--;
380 }
381 list_add_tail(&cred->cr_lru, &free);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400382 rpcauth_unhash_cred_locked(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 }
Trond Myklebust9499b432007-06-24 15:57:57 -0400385 spin_unlock(&cache->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_unlock(&rpc_credcache_lock);
387 rpcauth_destroy_credlist(&free);
388}
389
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400390/*
391 * Destroy the RPC credential cache
392 */
393void
394rpcauth_destroy_credcache(struct rpc_auth *auth)
395{
396 struct rpc_cred_cache *cache = auth->au_credcache;
397
398 if (cache) {
399 auth->au_credcache = NULL;
400 rpcauth_clear_credcache(cache);
Trond Myklebust241269b2010-07-31 14:29:08 -0400401 kfree(cache->hashtable);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400402 kfree(cache);
403 }
404}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400405EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400406
Trond Myklebustd2b83142008-04-14 18:13:37 -0400407
408#define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/*
411 * Remove stale credentials. Avoid sleeping inside the loop.
412 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400413static int
414rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Trond Myklebust9499b432007-06-24 15:57:57 -0400416 spinlock_t *cache_lock;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400417 struct rpc_cred *cred, *next;
Trond Myklebustd2b83142008-04-14 18:13:37 -0400418 unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Trond Myklebusteac0d182008-10-28 15:21:41 -0400420 list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
421
Trond Myklebust20673402010-05-13 12:51:06 -0400422 if (nr_to_scan-- == 0)
423 break;
Trond Myklebust93a05e62010-05-13 12:51:06 -0400424 /*
425 * Enforce a 60 second garbage collection moratorium
426 * Note that the cred_unused list must be time-ordered.
427 */
Trond Myklebust3d7b0892010-04-22 15:35:55 -0400428 if (time_in_range(cred->cr_expire, expired, jiffies) &&
Trond Myklebusteac0d182008-10-28 15:21:41 -0400429 test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
Trond Myklebust93a05e62010-05-13 12:51:06 -0400430 return 0;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400431
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400432 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400433 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400434 if (atomic_read(&cred->cr_count) != 0)
435 continue;
Trond Myklebusteac0d182008-10-28 15:21:41 -0400436
Trond Myklebust9499b432007-06-24 15:57:57 -0400437 cache_lock = &cred->cr_auth->au_credcache->lock;
438 spin_lock(cache_lock);
439 if (atomic_read(&cred->cr_count) == 0) {
440 get_rpccred(cred);
441 list_add_tail(&cred->cr_lru, free);
442 rpcauth_unhash_cred_locked(cred);
443 }
444 spin_unlock(cache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
Trond Myklebust93a05e62010-05-13 12:51:06 -0400446 return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400447}
448
449/*
Trond Myklebustf5c21872007-06-25 17:11:20 -0400450 * Run memory cache shrinker.
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400451 */
Trond Myklebustf5c21872007-06-25 17:11:20 -0400452static int
Ying Han1495f232011-05-24 17:12:27 -0700453rpcauth_cache_shrinker(struct shrinker *shrink, struct shrink_control *sc)
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400454{
Trond Myklebustf5c21872007-06-25 17:11:20 -0400455 LIST_HEAD(free);
456 int res;
Ying Han1495f232011-05-24 17:12:27 -0700457 int nr_to_scan = sc->nr_to_scan;
458 gfp_t gfp_mask = sc->gfp_mask;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400459
Trond Myklebustd300a412010-05-13 12:51:03 -0400460 if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
461 return (nr_to_scan == 0) ? 0 : -1;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400462 if (list_empty(&cred_unused))
463 return 0;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400464 spin_lock(&rpc_credcache_lock);
Trond Myklebust93a05e62010-05-13 12:51:06 -0400465 res = rpcauth_prune_expired(&free, nr_to_scan);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400466 spin_unlock(&rpc_credcache_lock);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400467 rpcauth_destroy_credlist(&free);
468 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471/*
472 * Look up a process' credentials in the authentication cache
473 */
474struct rpc_cred *
475rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
Trond Myklebust8a317762006-02-01 12:18:36 -0500476 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400478 LIST_HEAD(free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 struct rpc_cred_cache *cache = auth->au_credcache;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400480 struct rpc_cred *cred = NULL,
481 *entry, *new;
Trond Myklebust25337fd2008-03-12 14:40:14 -0400482 unsigned int nr;
483
Eric W. Biederman9e469e32013-02-02 01:57:30 -0800484 nr = hash_long(from_kuid(&init_user_ns, acred->uid), cache->hashbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400486 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800487 hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400488 if (!entry->cr_ops->crmatch(acred, entry, flags))
489 continue;
Trond Myklebust9499b432007-06-24 15:57:57 -0400490 spin_lock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400491 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
Trond Myklebust9499b432007-06-24 15:57:57 -0400492 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400493 continue;
494 }
495 cred = get_rpccred(entry);
Trond Myklebust9499b432007-06-24 15:57:57 -0400496 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400497 break;
498 }
499 rcu_read_unlock();
500
Trond Myklebust9499b432007-06-24 15:57:57 -0400501 if (cred != NULL)
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400502 goto found;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400503
504 new = auth->au_ops->crcreate(auth, acred, flags);
505 if (IS_ERR(new)) {
506 cred = new;
507 goto out;
508 }
509
Trond Myklebust9499b432007-06-24 15:57:57 -0400510 spin_lock(&cache->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800511 hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400512 if (!entry->cr_ops->crmatch(acred, entry, flags))
513 continue;
514 cred = get_rpccred(entry);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400515 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400517 if (cred == NULL) {
Trond Myklebust5fe47552007-06-23 19:55:31 -0400518 cred = new;
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400519 set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
520 hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
521 } else
522 list_add_tail(&new->cr_lru, &free);
Trond Myklebust9499b432007-06-24 15:57:57 -0400523 spin_unlock(&cache->lock);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400524found:
Joe Perchesf64f9e72009-11-29 16:55:45 -0800525 if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
526 cred->cr_ops->cr_init != NULL &&
527 !(flags & RPCAUTH_LOOKUP_NEW)) {
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500528 int res = cred->cr_ops->cr_init(auth, cred);
529 if (res < 0) {
530 put_rpccred(cred);
531 cred = ERR_PTR(res);
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400534 rpcauth_destroy_credlist(&free);
535out:
536 return cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400538EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500541rpcauth_lookupcred(struct rpc_auth *auth, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
David Howells86a264a2008-11-14 10:39:18 +1100543 struct auth_cred acred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct rpc_cred *ret;
David Howells86a264a2008-11-14 10:39:18 +1100545 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Chuck Lever46121cf2007-01-31 12:14:08 -0500547 dprintk("RPC: looking up %s cred\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 auth->au_ops->au_name);
David Howells86a264a2008-11-14 10:39:18 +1100549
550 memset(&acred, 0, sizeof(acred));
551 acred.uid = cred->fsuid;
552 acred.gid = cred->fsgid;
553 acred.group_info = get_group_info(((struct cred *)cred)->group_info);
554
Trond Myklebust8a317762006-02-01 12:18:36 -0500555 ret = auth->au_ops->lookup_cred(auth, &acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 put_group_info(acred.group_info);
557 return ret;
558}
559
Trond Myklebust5fe47552007-06-23 19:55:31 -0400560void
561rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
562 struct rpc_auth *auth, const struct rpc_credops *ops)
563{
564 INIT_HLIST_NODE(&cred->cr_hash);
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400565 INIT_LIST_HEAD(&cred->cr_lru);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400566 atomic_set(&cred->cr_count, 1);
567 cred->cr_auth = auth;
568 cred->cr_ops = ops;
569 cred->cr_expire = jiffies;
570#ifdef RPC_DEBUG
571 cred->cr_magic = RPCAUTH_CRED_MAGIC;
572#endif
573 cred->cr_uid = acred->uid;
574}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400575EXPORT_SYMBOL_GPL(rpcauth_init_cred);
Trond Myklebust5fe47552007-06-23 19:55:31 -0400576
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400577struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400578rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400579{
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400580 dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
581 cred->cr_auth->au_ops->au_name, cred);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400582 return get_rpccred(cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400583}
Trond Myklebust5c691042008-03-12 16:21:07 -0400584EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400585
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400586static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400587rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400589 struct rpc_auth *auth = task->tk_client->cl_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct auth_cred acred = {
Eric W. Biedermanbf37f792013-02-01 15:55:38 -0800591 .uid = GLOBAL_ROOT_UID,
592 .gid = GLOBAL_ROOT_GID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Chuck Lever46121cf2007-01-31 12:14:08 -0500595 dprintk("RPC: %5u looking up %s cred\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400596 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400597 return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
Trond Myklebustaf093832008-03-12 12:12:16 -0400598}
599
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400600static struct rpc_cred *
Trond Myklebust5d351752009-09-15 13:32:13 -0400601rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
Trond Myklebustaf093832008-03-12 12:12:16 -0400602{
603 struct rpc_auth *auth = task->tk_client->cl_auth;
Trond Myklebustaf093832008-03-12 12:12:16 -0400604
605 dprintk("RPC: %5u looking up %s cred\n",
606 task->tk_pid, auth->au_ops->au_name);
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400607 return rpcauth_lookupcred(auth, lookupflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Trond Myklebusta17c2152010-07-31 14:29:08 -0400610static int
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400611rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400613 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400614 struct rpc_cred *new;
Trond Myklebust5d351752009-09-15 13:32:13 -0400615 int lookupflags = 0;
616
617 if (flags & RPC_TASK_ASYNC)
618 lookupflags |= RPCAUTH_LOOKUP_NEW;
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400619 if (cred != NULL)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400620 new = cred->cr_ops->crbind(task, cred, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400621 else if (flags & RPC_TASK_ROOTCREDS)
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400622 new = rpcauth_bind_root_cred(task, lookupflags);
Trond Myklebust4ccda2c2008-03-12 16:20:55 -0400623 else
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400624 new = rpcauth_bind_new_cred(task, lookupflags);
625 if (IS_ERR(new))
626 return PTR_ERR(new);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400627 if (req->rq_cred != NULL)
628 put_rpccred(req->rq_cred);
629 req->rq_cred = new;
Trond Myklebust8572b8e2010-07-31 14:29:08 -0400630 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633void
634put_rpccred(struct rpc_cred *cred)
635{
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400636 /* Fast path for unhashed credentials */
Trond Myklebustf0380f32009-12-03 08:10:17 -0500637 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
638 if (atomic_dec_and_test(&cred->cr_count))
639 cred->cr_ops->crdestroy(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return;
Trond Myklebustf0380f32009-12-03 08:10:17 -0500641 }
642
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400643 if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
644 return;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400645 if (!list_empty(&cred->cr_lru)) {
646 number_cred_unused--;
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400647 list_del_init(&cred->cr_lru);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400648 }
Trond Myklebust5f707eb2008-10-28 15:21:42 -0400649 if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
Trond Myklebustf0380f32009-12-03 08:10:17 -0500650 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
651 cred->cr_expire = jiffies;
652 list_add_tail(&cred->cr_lru, &cred_unused);
653 number_cred_unused++;
654 goto out_nodestroy;
655 }
656 if (!rpcauth_unhash_cred(cred)) {
657 /* We were hashed and someone looked us up... */
658 goto out_nodestroy;
659 }
Trond Myklebuste092bdc2007-06-23 19:45:36 -0400660 }
661 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 cred->cr_ops->crdestroy(cred);
Trond Myklebustf0380f32009-12-03 08:10:17 -0500663 return;
664out_nodestroy:
665 spin_unlock(&rpc_credcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400667EXPORT_SYMBOL_GPL(put_rpccred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700669__be32 *
670rpcauth_marshcred(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400672 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Chuck Lever46121cf2007-01-31 12:14:08 -0500674 dprintk("RPC: %5u marshaling %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400675 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return cred->cr_ops->crmarshal(task, p);
678}
679
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700680__be32 *
681rpcauth_checkverf(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400683 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Chuck Lever46121cf2007-01-31 12:14:08 -0500685 dprintk("RPC: %5u validating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400686 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return cred->cr_ops->crvalidate(task, p);
689}
690
Chuck Lever9f06c712010-12-14 14:59:18 +0000691static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
692 __be32 *data, void *obj)
693{
694 struct xdr_stream xdr;
695
696 xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
697 encode(rqstp, &xdr, obj);
698}
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700int
Chuck Lever9f06c712010-12-14 14:59:18 +0000701rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700702 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400704 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Chuck Lever46121cf2007-01-31 12:14:08 -0500706 dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 task->tk_pid, cred->cr_ops->cr_name, cred);
708 if (cred->cr_ops->crwrap_req)
709 return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
710 /* By default, we encode the arguments normally. */
Chuck Lever9f06c712010-12-14 14:59:18 +0000711 rpcauth_wrap_req_encode(encode, rqstp, data, obj);
712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Chuck Leverbf269552010-12-14 14:59:29 +0000715static int
716rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
717 __be32 *data, void *obj)
718{
719 struct xdr_stream xdr;
720
721 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
722 return decode(rqstp, &xdr, obj);
723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725int
Chuck Leverbf269552010-12-14 14:59:29 +0000726rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700727 __be32 *data, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400729 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Chuck Lever46121cf2007-01-31 12:14:08 -0500731 dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 task->tk_pid, cred->cr_ops->cr_name, cred);
733 if (cred->cr_ops->crunwrap_resp)
734 return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
735 data, obj);
736 /* By default, we decode the arguments normally. */
Chuck Leverbf269552010-12-14 14:59:29 +0000737 return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740int
741rpcauth_refreshcred(struct rpc_task *task)
742{
Trond Myklebust9a84d382010-10-24 18:00:46 -0400743 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 int err;
745
Trond Myklebusta17c2152010-07-31 14:29:08 -0400746 cred = task->tk_rqstp->rq_cred;
747 if (cred == NULL) {
748 err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
749 if (err < 0)
750 goto out;
751 cred = task->tk_rqstp->rq_cred;
Joe Perchesf81c6222011-06-03 11:51:19 +0000752 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500753 dprintk("RPC: %5u refreshing %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400754 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Chuck Lever0bbacc42005-11-01 16:53:32 -0500755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 err = cred->cr_ops->crrefresh(task);
Trond Myklebusta17c2152010-07-31 14:29:08 -0400757out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (err < 0)
759 task->tk_status = err;
760 return err;
761}
762
763void
764rpcauth_invalcred(struct rpc_task *task)
765{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400766 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400767
Chuck Lever46121cf2007-01-31 12:14:08 -0500768 dprintk("RPC: %5u invalidating %s cred %p\n",
Trond Myklebust1be27f32007-06-27 14:29:04 -0400769 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400770 if (cred)
771 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774int
775rpcauth_uptodatecred(struct rpc_task *task)
776{
Trond Myklebusta17c2152010-07-31 14:29:08 -0400777 struct rpc_cred *cred = task->tk_rqstp->rq_cred;
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400778
779 return cred == NULL ||
780 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
Trond Myklebustf5c21872007-06-25 17:11:20 -0400782
Rusty Russell8e1f9362007-07-17 04:03:17 -0700783static struct shrinker rpc_cred_shrinker = {
784 .shrink = rpcauth_cache_shrinker,
785 .seeks = DEFAULT_SEEKS,
786};
Trond Myklebustf5c21872007-06-25 17:11:20 -0400787
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400788int __init rpcauth_init_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400789{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400790 int err;
791
792 err = rpc_init_authunix();
793 if (err < 0)
794 goto out1;
795 err = rpc_init_generic_auth();
796 if (err < 0)
797 goto out2;
Rusty Russell8e1f9362007-07-17 04:03:17 -0700798 register_shrinker(&rpc_cred_shrinker);
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400799 return 0;
800out2:
801 rpc_destroy_authunix();
802out1:
803 return err;
Trond Myklebustf5c21872007-06-25 17:11:20 -0400804}
805
Stephen Rothwellc135e842010-09-29 14:16:57 +1000806void rpcauth_remove_module(void)
Trond Myklebustf5c21872007-06-25 17:11:20 -0400807{
Trond Myklebust5d8d9a42010-07-31 14:29:07 -0400808 rpc_destroy_authunix();
809 rpc_destroy_generic_auth();
Rusty Russell8e1f9362007-07-17 04:03:17 -0700810 unregister_shrinker(&rpc_cred_shrinker);
Trond Myklebustf5c21872007-06-25 17:11:20 -0400811}