SUNRPC: Refactor nfsd4_do_encode_secinfo()
[linux-3.10.git] / net / sunrpc / auth.c
1 /*
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>
14 #include <linux/hash.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/sunrpc/gss_api.h>
17 #include <linux/spinlock.h>
18
19 #ifdef RPC_DEBUG
20 # define RPCDBG_FACILITY        RPCDBG_AUTH
21 #endif
22
23 #define RPC_CREDCACHE_DEFAULT_HASHBITS  (4)
24 struct rpc_cred_cache {
25         struct hlist_head       *hashtable;
26         unsigned int            hashbits;
27         spinlock_t              lock;
28 };
29
30 static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS;
31
32 static DEFINE_SPINLOCK(rpc_authflavor_lock);
33 static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
34         &authnull_ops,          /* AUTH_NULL */
35         &authunix_ops,          /* AUTH_UNIX */
36         NULL,                   /* others can be loadable modules */
37 };
38
39 static LIST_HEAD(cred_unused);
40 static unsigned long number_cred_unused;
41
42 #define MAX_HASHTABLE_BITS (14)
43 static int param_set_hashtbl_sz(const char *val, const struct kernel_param *kp)
44 {
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;
61 out_inval:
62         return -EINVAL;
63 }
64
65 static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp)
66 {
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
75 static struct kernel_param_ops param_ops_hashtbl_sz = {
76         .set = param_set_hashtbl_sz,
77         .get = param_get_hashtbl_sz,
78 };
79
80 module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
81 MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
82
83 static u32
84 pseudoflavor_to_flavor(u32 flavor) {
85         if (flavor >= RPC_AUTH_MAXFLAVOR)
86                 return RPC_AUTH_GSS;
87         return flavor;
88 }
89
90 int
91 rpcauth_register(const struct rpc_authops *ops)
92 {
93         rpc_authflavor_t flavor;
94         int ret = -EPERM;
95
96         if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
97                 return -EINVAL;
98         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;
105 }
106 EXPORT_SYMBOL_GPL(rpcauth_register);
107
108 int
109 rpcauth_unregister(const struct rpc_authops *ops)
110 {
111         rpc_authflavor_t flavor;
112         int ret = -EPERM;
113
114         if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
115                 return -EINVAL;
116         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;
123 }
124 EXPORT_SYMBOL_GPL(rpcauth_unregister);
125
126 /**
127  * 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  */
135 rpc_authflavor_t
136 rpcauth_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 }
159 EXPORT_SYMBOL_GPL(rpcauth_get_pseudoflavor);
160
161 /**
162  * 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  */
169 int
170 rpcauth_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 }
194 EXPORT_SYMBOL_GPL(rpcauth_get_gssinfo);
195
196 /**
197  * 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  */
206 int
207 rpcauth_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 }
247 EXPORT_SYMBOL_GPL(rpcauth_list_flavors);
248
249 struct rpc_auth *
250 rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
251 {
252         struct rpc_auth         *auth;
253         const struct rpc_authops *ops;
254         u32                     flavor = pseudoflavor_to_flavor(pseudoflavor);
255
256         auth = ERR_PTR(-EINVAL);
257         if (flavor >= RPC_AUTH_MAXFLAVOR)
258                 goto out;
259
260         if ((ops = auth_flavors[flavor]) == NULL)
261                 request_module("rpc-auth-%u", flavor);
262         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);
266                 goto out;
267         }
268         spin_unlock(&rpc_authflavor_lock);
269         auth = ops->create(clnt, pseudoflavor);
270         module_put(ops->owner);
271         if (IS_ERR(auth))
272                 return auth;
273         if (clnt->cl_auth)
274                 rpcauth_release(clnt->cl_auth);
275         clnt->cl_auth = auth;
276
277 out:
278         return auth;
279 }
280 EXPORT_SYMBOL_GPL(rpcauth_create);
281
282 void
283 rpcauth_release(struct rpc_auth *auth)
284 {
285         if (!atomic_dec_and_test(&auth->au_count))
286                 return;
287         auth->au_ops->destroy(auth);
288 }
289
290 static DEFINE_SPINLOCK(rpc_credcache_lock);
291
292 static void
293 rpcauth_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
300 static int
301 rpcauth_unhash_cred(struct rpc_cred *cred)
302 {
303         spinlock_t *cache_lock;
304         int ret;
305
306         cache_lock = &cred->cr_auth->au_credcache->lock;
307         spin_lock(cache_lock);
308         ret = atomic_read(&cred->cr_count) == 0;
309         if (ret)
310                 rpcauth_unhash_cred_locked(cred);
311         spin_unlock(cache_lock);
312         return ret;
313 }
314
315 /*
316  * Initialize RPC credential cache
317  */
318 int
319 rpcauth_init_credcache(struct rpc_auth *auth)
320 {
321         struct rpc_cred_cache *new;
322         unsigned int hashsize;
323
324         new = kmalloc(sizeof(*new), GFP_KERNEL);
325         if (!new)
326                 goto out_nocache;
327         new->hashbits = auth_hashbits;
328         hashsize = 1U << new->hashbits;
329         new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
330         if (!new->hashtable)
331                 goto out_nohashtbl;
332         spin_lock_init(&new->lock);
333         auth->au_credcache = new;
334         return 0;
335 out_nohashtbl:
336         kfree(new);
337 out_nocache:
338         return -ENOMEM;
339 }
340 EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
341
342 /*
343  * Destroy a list of credentials
344  */
345 static inline
346 void rpcauth_destroy_credlist(struct list_head *head)
347 {
348         struct rpc_cred *cred;
349
350         while (!list_empty(head)) {
351                 cred = list_entry(head->next, struct rpc_cred, cr_lru);
352                 list_del_init(&cred->cr_lru);
353                 put_rpccred(cred);
354         }
355 }
356
357 /*
358  * Clear the RPC credential cache, and delete those credentials
359  * that are not referenced.
360  */
361 void
362 rpcauth_clear_credcache(struct rpc_cred_cache *cache)
363 {
364         LIST_HEAD(free);
365         struct hlist_head *head;
366         struct rpc_cred *cred;
367         unsigned int hashsize = 1U << cache->hashbits;
368         int             i;
369
370         spin_lock(&rpc_credcache_lock);
371         spin_lock(&cache->lock);
372         for (i = 0; i < hashsize; i++) {
373                 head = &cache->hashtable[i];
374                 while (!hlist_empty(head)) {
375                         cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
376                         get_rpccred(cred);
377                         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);
382                         rpcauth_unhash_cred_locked(cred);
383                 }
384         }
385         spin_unlock(&cache->lock);
386         spin_unlock(&rpc_credcache_lock);
387         rpcauth_destroy_credlist(&free);
388 }
389
390 /*
391  * Destroy the RPC credential cache
392  */
393 void
394 rpcauth_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);
401                 kfree(cache->hashtable);
402                 kfree(cache);
403         }
404 }
405 EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
406
407
408 #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
409
410 /*
411  * Remove stale credentials. Avoid sleeping inside the loop.
412  */
413 static int
414 rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
415 {
416         spinlock_t *cache_lock;
417         struct rpc_cred *cred, *next;
418         unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
419
420         list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
421
422                 if (nr_to_scan-- == 0)
423                         break;
424                 /*
425                  * Enforce a 60 second garbage collection moratorium
426                  * Note that the cred_unused list must be time-ordered.
427                  */
428                 if (time_in_range(cred->cr_expire, expired, jiffies) &&
429                     test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
430                         return 0;
431
432                 list_del_init(&cred->cr_lru);
433                 number_cred_unused--;
434                 if (atomic_read(&cred->cr_count) != 0)
435                         continue;
436
437                 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);
445         }
446         return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
447 }
448
449 /*
450  * Run memory cache shrinker.
451  */
452 static int
453 rpcauth_cache_shrinker(struct shrinker *shrink, struct shrink_control *sc)
454 {
455         LIST_HEAD(free);
456         int res;
457         int nr_to_scan = sc->nr_to_scan;
458         gfp_t gfp_mask = sc->gfp_mask;
459
460         if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
461                 return (nr_to_scan == 0) ? 0 : -1;
462         if (list_empty(&cred_unused))
463                 return 0;
464         spin_lock(&rpc_credcache_lock);
465         res = rpcauth_prune_expired(&free, nr_to_scan);
466         spin_unlock(&rpc_credcache_lock);
467         rpcauth_destroy_credlist(&free);
468         return res;
469 }
470
471 /*
472  * Look up a process' credentials in the authentication cache
473  */
474 struct rpc_cred *
475 rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
476                 int flags)
477 {
478         LIST_HEAD(free);
479         struct rpc_cred_cache *cache = auth->au_credcache;
480         struct rpc_cred *cred = NULL,
481                         *entry, *new;
482         unsigned int nr;
483
484         nr = hash_long(from_kuid(&init_user_ns, acred->uid), cache->hashbits);
485
486         rcu_read_lock();
487         hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
488                 if (!entry->cr_ops->crmatch(acred, entry, flags))
489                         continue;
490                 spin_lock(&cache->lock);
491                 if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
492                         spin_unlock(&cache->lock);
493                         continue;
494                 }
495                 cred = get_rpccred(entry);
496                 spin_unlock(&cache->lock);
497                 break;
498         }
499         rcu_read_unlock();
500
501         if (cred != NULL)
502                 goto found;
503
504         new = auth->au_ops->crcreate(auth, acred, flags);
505         if (IS_ERR(new)) {
506                 cred = new;
507                 goto out;
508         }
509
510         spin_lock(&cache->lock);
511         hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
512                 if (!entry->cr_ops->crmatch(acred, entry, flags))
513                         continue;
514                 cred = get_rpccred(entry);
515                 break;
516         }
517         if (cred == NULL) {
518                 cred = new;
519                 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);
523         spin_unlock(&cache->lock);
524 found:
525         if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
526             cred->cr_ops->cr_init != NULL &&
527             !(flags & RPCAUTH_LOOKUP_NEW)) {
528                 int res = cred->cr_ops->cr_init(auth, cred);
529                 if (res < 0) {
530                         put_rpccred(cred);
531                         cred = ERR_PTR(res);
532                 }
533         }
534         rpcauth_destroy_credlist(&free);
535 out:
536         return cred;
537 }
538 EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
539
540 struct rpc_cred *
541 rpcauth_lookupcred(struct rpc_auth *auth, int flags)
542 {
543         struct auth_cred acred;
544         struct rpc_cred *ret;
545         const struct cred *cred = current_cred();
546
547         dprintk("RPC:       looking up %s cred\n",
548                 auth->au_ops->au_name);
549
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
555         ret = auth->au_ops->lookup_cred(auth, &acred, flags);
556         put_group_info(acred.group_info);
557         return ret;
558 }
559
560 void
561 rpcauth_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);
565         INIT_LIST_HEAD(&cred->cr_lru);
566         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 }
575 EXPORT_SYMBOL_GPL(rpcauth_init_cred);
576
577 struct rpc_cred *
578 rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
579 {
580         dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
581                         cred->cr_auth->au_ops->au_name, cred);
582         return get_rpccred(cred);
583 }
584 EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
585
586 static struct rpc_cred *
587 rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
588 {
589         struct rpc_auth *auth = task->tk_client->cl_auth;
590         struct auth_cred acred = {
591                 .uid = GLOBAL_ROOT_UID,
592                 .gid = GLOBAL_ROOT_GID,
593         };
594
595         dprintk("RPC: %5u looking up %s cred\n",
596                 task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
597         return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
598 }
599
600 static struct rpc_cred *
601 rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
602 {
603         struct rpc_auth *auth = task->tk_client->cl_auth;
604
605         dprintk("RPC: %5u looking up %s cred\n",
606                 task->tk_pid, auth->au_ops->au_name);
607         return rpcauth_lookupcred(auth, lookupflags);
608 }
609
610 static int
611 rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
612 {
613         struct rpc_rqst *req = task->tk_rqstp;
614         struct rpc_cred *new;
615         int lookupflags = 0;
616
617         if (flags & RPC_TASK_ASYNC)
618                 lookupflags |= RPCAUTH_LOOKUP_NEW;
619         if (cred != NULL)
620                 new = cred->cr_ops->crbind(task, cred, lookupflags);
621         else if (flags & RPC_TASK_ROOTCREDS)
622                 new = rpcauth_bind_root_cred(task, lookupflags);
623         else
624                 new = rpcauth_bind_new_cred(task, lookupflags);
625         if (IS_ERR(new))
626                 return PTR_ERR(new);
627         if (req->rq_cred != NULL)
628                 put_rpccred(req->rq_cred);
629         req->rq_cred = new;
630         return 0;
631 }
632
633 void
634 put_rpccred(struct rpc_cred *cred)
635 {
636         /* Fast path for unhashed credentials */
637         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);
640                 return;
641         }
642
643         if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
644                 return;
645         if (!list_empty(&cred->cr_lru)) {
646                 number_cred_unused--;
647                 list_del_init(&cred->cr_lru);
648         }
649         if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
650                 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                 }
660         }
661         spin_unlock(&rpc_credcache_lock);
662         cred->cr_ops->crdestroy(cred);
663         return;
664 out_nodestroy:
665         spin_unlock(&rpc_credcache_lock);
666 }
667 EXPORT_SYMBOL_GPL(put_rpccred);
668
669 __be32 *
670 rpcauth_marshcred(struct rpc_task *task, __be32 *p)
671 {
672         struct rpc_cred *cred = task->tk_rqstp->rq_cred;
673
674         dprintk("RPC: %5u marshaling %s cred %p\n",
675                 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
676
677         return cred->cr_ops->crmarshal(task, p);
678 }
679
680 __be32 *
681 rpcauth_checkverf(struct rpc_task *task, __be32 *p)
682 {
683         struct rpc_cred *cred = task->tk_rqstp->rq_cred;
684
685         dprintk("RPC: %5u validating %s cred %p\n",
686                 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
687
688         return cred->cr_ops->crvalidate(task, p);
689 }
690
691 static 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
700 int
701 rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
702                 __be32 *data, void *obj)
703 {
704         struct rpc_cred *cred = task->tk_rqstp->rq_cred;
705
706         dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
707                         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. */
711         rpcauth_wrap_req_encode(encode, rqstp, data, obj);
712         return 0;
713 }
714
715 static int
716 rpcauth_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
725 int
726 rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
727                 __be32 *data, void *obj)
728 {
729         struct rpc_cred *cred = task->tk_rqstp->rq_cred;
730
731         dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
732                         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. */
737         return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
738 }
739
740 int
741 rpcauth_refreshcred(struct rpc_task *task)
742 {
743         struct rpc_cred *cred;
744         int err;
745
746         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;
752         }
753         dprintk("RPC: %5u refreshing %s cred %p\n",
754                 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
755
756         err = cred->cr_ops->crrefresh(task);
757 out:
758         if (err < 0)
759                 task->tk_status = err;
760         return err;
761 }
762
763 void
764 rpcauth_invalcred(struct rpc_task *task)
765 {
766         struct rpc_cred *cred = task->tk_rqstp->rq_cred;
767
768         dprintk("RPC: %5u invalidating %s cred %p\n",
769                 task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
770         if (cred)
771                 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
772 }
773
774 int
775 rpcauth_uptodatecred(struct rpc_task *task)
776 {
777         struct rpc_cred *cred = task->tk_rqstp->rq_cred;
778
779         return cred == NULL ||
780                 test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
781 }
782
783 static struct shrinker rpc_cred_shrinker = {
784         .shrink = rpcauth_cache_shrinker,
785         .seeks = DEFAULT_SEEKS,
786 };
787
788 int __init rpcauth_init_module(void)
789 {
790         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;
798         register_shrinker(&rpc_cred_shrinker);
799         return 0;
800 out2:
801         rpc_destroy_authunix();
802 out1:
803         return err;
804 }
805
806 void rpcauth_remove_module(void)
807 {
808         rpc_destroy_authunix();
809         rpc_destroy_generic_auth();
810         unregister_shrinker(&rpc_cred_shrinker);
811 }