diff options
Diffstat (limited to 'net/sunrpc/auth.c')
-rw-r--r-- | net/sunrpc/auth.c | 162 |
1 files changed, 114 insertions, 48 deletions
diff --git a/net/sunrpc/auth.c b/net/sunrpc/auth.c index 8dc47f1d0001..36cb66022a27 100644 --- a/net/sunrpc/auth.c +++ b/net/sunrpc/auth.c | |||
@@ -19,6 +19,15 @@ | |||
19 | # define RPCDBG_FACILITY RPCDBG_AUTH | 19 | # define RPCDBG_FACILITY RPCDBG_AUTH |
20 | #endif | 20 | #endif |
21 | 21 | ||
22 | #define RPC_CREDCACHE_DEFAULT_HASHBITS (4) | ||
23 | struct rpc_cred_cache { | ||
24 | struct hlist_head *hashtable; | ||
25 | unsigned int hashbits; | ||
26 | spinlock_t lock; | ||
27 | }; | ||
28 | |||
29 | static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS; | ||
30 | |||
22 | static DEFINE_SPINLOCK(rpc_authflavor_lock); | 31 | static DEFINE_SPINLOCK(rpc_authflavor_lock); |
23 | static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = { | 32 | static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = { |
24 | &authnull_ops, /* AUTH_NULL */ | 33 | &authnull_ops, /* AUTH_NULL */ |
@@ -29,6 +38,47 @@ static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = { | |||
29 | static LIST_HEAD(cred_unused); | 38 | static LIST_HEAD(cred_unused); |
30 | static unsigned long number_cred_unused; | 39 | static unsigned long number_cred_unused; |
31 | 40 | ||
41 | #define MAX_HASHTABLE_BITS (10) | ||
42 | static int param_set_hashtbl_sz(const char *val, const struct kernel_param *kp) | ||
43 | { | ||
44 | unsigned long num; | ||
45 | unsigned int nbits; | ||
46 | int ret; | ||
47 | |||
48 | if (!val) | ||
49 | goto out_inval; | ||
50 | ret = strict_strtoul(val, 0, &num); | ||
51 | if (ret == -EINVAL) | ||
52 | goto out_inval; | ||
53 | nbits = fls(num); | ||
54 | if (num > (1U << nbits)) | ||
55 | nbits++; | ||
56 | if (nbits > MAX_HASHTABLE_BITS || nbits < 2) | ||
57 | goto out_inval; | ||
58 | *(unsigned int *)kp->arg = nbits; | ||
59 | return 0; | ||
60 | out_inval: | ||
61 | return -EINVAL; | ||
62 | } | ||
63 | |||
64 | static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp) | ||
65 | { | ||
66 | unsigned int nbits; | ||
67 | |||
68 | nbits = *(unsigned int *)kp->arg; | ||
69 | return sprintf(buffer, "%u", 1U << nbits); | ||
70 | } | ||
71 | |||
72 | #define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int); | ||
73 | |||
74 | static struct kernel_param_ops param_ops_hashtbl_sz = { | ||
75 | .set = param_set_hashtbl_sz, | ||
76 | .get = param_get_hashtbl_sz, | ||
77 | }; | ||
78 | |||
79 | module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644); | ||
80 | MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size"); | ||
81 | |||
32 | static u32 | 82 | static u32 |
33 | pseudoflavor_to_flavor(u32 flavor) { | 83 | pseudoflavor_to_flavor(u32 flavor) { |
34 | if (flavor >= RPC_AUTH_MAXFLAVOR) | 84 | if (flavor >= RPC_AUTH_MAXFLAVOR) |
@@ -145,16 +195,23 @@ int | |||
145 | rpcauth_init_credcache(struct rpc_auth *auth) | 195 | rpcauth_init_credcache(struct rpc_auth *auth) |
146 | { | 196 | { |
147 | struct rpc_cred_cache *new; | 197 | struct rpc_cred_cache *new; |
148 | int i; | 198 | unsigned int hashsize; |
149 | 199 | ||
150 | new = kmalloc(sizeof(*new), GFP_KERNEL); | 200 | new = kmalloc(sizeof(*new), GFP_KERNEL); |
151 | if (!new) | 201 | if (!new) |
152 | return -ENOMEM; | 202 | goto out_nocache; |
153 | for (i = 0; i < RPC_CREDCACHE_NR; i++) | 203 | new->hashbits = auth_hashbits; |
154 | INIT_HLIST_HEAD(&new->hashtable[i]); | 204 | hashsize = 1U << new->hashbits; |
205 | new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL); | ||
206 | if (!new->hashtable) | ||
207 | goto out_nohashtbl; | ||
155 | spin_lock_init(&new->lock); | 208 | spin_lock_init(&new->lock); |
156 | auth->au_credcache = new; | 209 | auth->au_credcache = new; |
157 | return 0; | 210 | return 0; |
211 | out_nohashtbl: | ||
212 | kfree(new); | ||
213 | out_nocache: | ||
214 | return -ENOMEM; | ||
158 | } | 215 | } |
159 | EXPORT_SYMBOL_GPL(rpcauth_init_credcache); | 216 | EXPORT_SYMBOL_GPL(rpcauth_init_credcache); |
160 | 217 | ||
@@ -183,11 +240,12 @@ rpcauth_clear_credcache(struct rpc_cred_cache *cache) | |||
183 | LIST_HEAD(free); | 240 | LIST_HEAD(free); |
184 | struct hlist_head *head; | 241 | struct hlist_head *head; |
185 | struct rpc_cred *cred; | 242 | struct rpc_cred *cred; |
243 | unsigned int hashsize = 1U << cache->hashbits; | ||
186 | int i; | 244 | int i; |
187 | 245 | ||
188 | spin_lock(&rpc_credcache_lock); | 246 | spin_lock(&rpc_credcache_lock); |
189 | spin_lock(&cache->lock); | 247 | spin_lock(&cache->lock); |
190 | for (i = 0; i < RPC_CREDCACHE_NR; i++) { | 248 | for (i = 0; i < hashsize; i++) { |
191 | head = &cache->hashtable[i]; | 249 | head = &cache->hashtable[i]; |
192 | while (!hlist_empty(head)) { | 250 | while (!hlist_empty(head)) { |
193 | cred = hlist_entry(head->first, struct rpc_cred, cr_hash); | 251 | cred = hlist_entry(head->first, struct rpc_cred, cr_hash); |
@@ -216,6 +274,7 @@ rpcauth_destroy_credcache(struct rpc_auth *auth) | |||
216 | if (cache) { | 274 | if (cache) { |
217 | auth->au_credcache = NULL; | 275 | auth->au_credcache = NULL; |
218 | rpcauth_clear_credcache(cache); | 276 | rpcauth_clear_credcache(cache); |
277 | kfree(cache->hashtable); | ||
219 | kfree(cache); | 278 | kfree(cache); |
220 | } | 279 | } |
221 | } | 280 | } |
@@ -297,7 +356,7 @@ rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred, | |||
297 | *entry, *new; | 356 | *entry, *new; |
298 | unsigned int nr; | 357 | unsigned int nr; |
299 | 358 | ||
300 | nr = hash_long(acred->uid, RPC_CREDCACHE_HASHBITS); | 359 | nr = hash_long(acred->uid, cache->hashbits); |
301 | 360 | ||
302 | rcu_read_lock(); | 361 | rcu_read_lock(); |
303 | hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) { | 362 | hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) { |
@@ -390,16 +449,16 @@ rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred, | |||
390 | } | 449 | } |
391 | EXPORT_SYMBOL_GPL(rpcauth_init_cred); | 450 | EXPORT_SYMBOL_GPL(rpcauth_init_cred); |
392 | 451 | ||
393 | void | 452 | struct rpc_cred * |
394 | rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags) | 453 | rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags) |
395 | { | 454 | { |
396 | task->tk_msg.rpc_cred = get_rpccred(cred); | ||
397 | dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid, | 455 | dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid, |
398 | cred->cr_auth->au_ops->au_name, cred); | 456 | cred->cr_auth->au_ops->au_name, cred); |
457 | return get_rpccred(cred); | ||
399 | } | 458 | } |
400 | EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred); | 459 | EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred); |
401 | 460 | ||
402 | static void | 461 | static struct rpc_cred * |
403 | rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags) | 462 | rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags) |
404 | { | 463 | { |
405 | struct rpc_auth *auth = task->tk_client->cl_auth; | 464 | struct rpc_auth *auth = task->tk_client->cl_auth; |
@@ -407,45 +466,43 @@ rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags) | |||
407 | .uid = 0, | 466 | .uid = 0, |
408 | .gid = 0, | 467 | .gid = 0, |
409 | }; | 468 | }; |
410 | struct rpc_cred *ret; | ||
411 | 469 | ||
412 | dprintk("RPC: %5u looking up %s cred\n", | 470 | dprintk("RPC: %5u looking up %s cred\n", |
413 | task->tk_pid, task->tk_client->cl_auth->au_ops->au_name); | 471 | task->tk_pid, task->tk_client->cl_auth->au_ops->au_name); |
414 | ret = auth->au_ops->lookup_cred(auth, &acred, lookupflags); | 472 | return auth->au_ops->lookup_cred(auth, &acred, lookupflags); |
415 | if (!IS_ERR(ret)) | ||
416 | task->tk_msg.rpc_cred = ret; | ||
417 | else | ||
418 | task->tk_status = PTR_ERR(ret); | ||
419 | } | 473 | } |
420 | 474 | ||
421 | static void | 475 | static struct rpc_cred * |
422 | rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags) | 476 | rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags) |
423 | { | 477 | { |
424 | struct rpc_auth *auth = task->tk_client->cl_auth; | 478 | struct rpc_auth *auth = task->tk_client->cl_auth; |
425 | struct rpc_cred *ret; | ||
426 | 479 | ||
427 | dprintk("RPC: %5u looking up %s cred\n", | 480 | dprintk("RPC: %5u looking up %s cred\n", |
428 | task->tk_pid, auth->au_ops->au_name); | 481 | task->tk_pid, auth->au_ops->au_name); |
429 | ret = rpcauth_lookupcred(auth, lookupflags); | 482 | return rpcauth_lookupcred(auth, lookupflags); |
430 | if (!IS_ERR(ret)) | ||
431 | task->tk_msg.rpc_cred = ret; | ||
432 | else | ||
433 | task->tk_status = PTR_ERR(ret); | ||
434 | } | 483 | } |
435 | 484 | ||
436 | void | 485 | static int |
437 | rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags) | 486 | rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags) |
438 | { | 487 | { |
488 | struct rpc_rqst *req = task->tk_rqstp; | ||
489 | struct rpc_cred *new; | ||
439 | int lookupflags = 0; | 490 | int lookupflags = 0; |
440 | 491 | ||
441 | if (flags & RPC_TASK_ASYNC) | 492 | if (flags & RPC_TASK_ASYNC) |
442 | lookupflags |= RPCAUTH_LOOKUP_NEW; | 493 | lookupflags |= RPCAUTH_LOOKUP_NEW; |
443 | if (cred != NULL) | 494 | if (cred != NULL) |
444 | cred->cr_ops->crbind(task, cred, lookupflags); | 495 | new = cred->cr_ops->crbind(task, cred, lookupflags); |
445 | else if (flags & RPC_TASK_ROOTCREDS) | 496 | else if (flags & RPC_TASK_ROOTCREDS) |
446 | rpcauth_bind_root_cred(task, lookupflags); | 497 | new = rpcauth_bind_root_cred(task, lookupflags); |
447 | else | 498 | else |
448 | rpcauth_bind_new_cred(task, lookupflags); | 499 | new = rpcauth_bind_new_cred(task, lookupflags); |
500 | if (IS_ERR(new)) | ||
501 | return PTR_ERR(new); | ||
502 | if (req->rq_cred != NULL) | ||
503 | put_rpccred(req->rq_cred); | ||
504 | req->rq_cred = new; | ||
505 | return 0; | ||
449 | } | 506 | } |
450 | 507 | ||
451 | void | 508 | void |
@@ -484,22 +541,10 @@ out_nodestroy: | |||
484 | } | 541 | } |
485 | EXPORT_SYMBOL_GPL(put_rpccred); | 542 | EXPORT_SYMBOL_GPL(put_rpccred); |
486 | 543 | ||
487 | void | ||
488 | rpcauth_unbindcred(struct rpc_task *task) | ||
489 | { | ||
490 | struct rpc_cred *cred = task->tk_msg.rpc_cred; | ||
491 | |||
492 | dprintk("RPC: %5u releasing %s cred %p\n", | ||
493 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); | ||
494 | |||
495 | put_rpccred(cred); | ||
496 | task->tk_msg.rpc_cred = NULL; | ||
497 | } | ||
498 | |||
499 | __be32 * | 544 | __be32 * |
500 | rpcauth_marshcred(struct rpc_task *task, __be32 *p) | 545 | rpcauth_marshcred(struct rpc_task *task, __be32 *p) |
501 | { | 546 | { |
502 | struct rpc_cred *cred = task->tk_msg.rpc_cred; | 547 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
503 | 548 | ||
504 | dprintk("RPC: %5u marshaling %s cred %p\n", | 549 | dprintk("RPC: %5u marshaling %s cred %p\n", |
505 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); | 550 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
@@ -510,7 +555,7 @@ rpcauth_marshcred(struct rpc_task *task, __be32 *p) | |||
510 | __be32 * | 555 | __be32 * |
511 | rpcauth_checkverf(struct rpc_task *task, __be32 *p) | 556 | rpcauth_checkverf(struct rpc_task *task, __be32 *p) |
512 | { | 557 | { |
513 | struct rpc_cred *cred = task->tk_msg.rpc_cred; | 558 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
514 | 559 | ||
515 | dprintk("RPC: %5u validating %s cred %p\n", | 560 | dprintk("RPC: %5u validating %s cred %p\n", |
516 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); | 561 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
@@ -522,7 +567,7 @@ int | |||
522 | rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp, | 567 | rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp, |
523 | __be32 *data, void *obj) | 568 | __be32 *data, void *obj) |
524 | { | 569 | { |
525 | struct rpc_cred *cred = task->tk_msg.rpc_cred; | 570 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
526 | 571 | ||
527 | dprintk("RPC: %5u using %s cred %p to wrap rpc data\n", | 572 | dprintk("RPC: %5u using %s cred %p to wrap rpc data\n", |
528 | task->tk_pid, cred->cr_ops->cr_name, cred); | 573 | task->tk_pid, cred->cr_ops->cr_name, cred); |
@@ -536,7 +581,7 @@ int | |||
536 | rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp, | 581 | rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp, |
537 | __be32 *data, void *obj) | 582 | __be32 *data, void *obj) |
538 | { | 583 | { |
539 | struct rpc_cred *cred = task->tk_msg.rpc_cred; | 584 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
540 | 585 | ||
541 | dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n", | 586 | dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n", |
542 | task->tk_pid, cred->cr_ops->cr_name, cred); | 587 | task->tk_pid, cred->cr_ops->cr_name, cred); |
@@ -550,13 +595,21 @@ rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp, | |||
550 | int | 595 | int |
551 | rpcauth_refreshcred(struct rpc_task *task) | 596 | rpcauth_refreshcred(struct rpc_task *task) |
552 | { | 597 | { |
553 | struct rpc_cred *cred = task->tk_msg.rpc_cred; | 598 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
554 | int err; | 599 | int err; |
555 | 600 | ||
601 | cred = task->tk_rqstp->rq_cred; | ||
602 | if (cred == NULL) { | ||
603 | err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags); | ||
604 | if (err < 0) | ||
605 | goto out; | ||
606 | cred = task->tk_rqstp->rq_cred; | ||
607 | }; | ||
556 | dprintk("RPC: %5u refreshing %s cred %p\n", | 608 | dprintk("RPC: %5u refreshing %s cred %p\n", |
557 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); | 609 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
558 | 610 | ||
559 | err = cred->cr_ops->crrefresh(task); | 611 | err = cred->cr_ops->crrefresh(task); |
612 | out: | ||
560 | if (err < 0) | 613 | if (err < 0) |
561 | task->tk_status = err; | 614 | task->tk_status = err; |
562 | return err; | 615 | return err; |
@@ -565,7 +618,7 @@ rpcauth_refreshcred(struct rpc_task *task) | |||
565 | void | 618 | void |
566 | rpcauth_invalcred(struct rpc_task *task) | 619 | rpcauth_invalcred(struct rpc_task *task) |
567 | { | 620 | { |
568 | struct rpc_cred *cred = task->tk_msg.rpc_cred; | 621 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
569 | 622 | ||
570 | dprintk("RPC: %5u invalidating %s cred %p\n", | 623 | dprintk("RPC: %5u invalidating %s cred %p\n", |
571 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); | 624 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
@@ -576,7 +629,7 @@ rpcauth_invalcred(struct rpc_task *task) | |||
576 | int | 629 | int |
577 | rpcauth_uptodatecred(struct rpc_task *task) | 630 | rpcauth_uptodatecred(struct rpc_task *task) |
578 | { | 631 | { |
579 | struct rpc_cred *cred = task->tk_msg.rpc_cred; | 632 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
580 | 633 | ||
581 | return cred == NULL || | 634 | return cred == NULL || |
582 | test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0; | 635 | test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0; |
@@ -587,14 +640,27 @@ static struct shrinker rpc_cred_shrinker = { | |||
587 | .seeks = DEFAULT_SEEKS, | 640 | .seeks = DEFAULT_SEEKS, |
588 | }; | 641 | }; |
589 | 642 | ||
590 | void __init rpcauth_init_module(void) | 643 | int __init rpcauth_init_module(void) |
591 | { | 644 | { |
592 | rpc_init_authunix(); | 645 | int err; |
593 | rpc_init_generic_auth(); | 646 | |
647 | err = rpc_init_authunix(); | ||
648 | if (err < 0) | ||
649 | goto out1; | ||
650 | err = rpc_init_generic_auth(); | ||
651 | if (err < 0) | ||
652 | goto out2; | ||
594 | register_shrinker(&rpc_cred_shrinker); | 653 | register_shrinker(&rpc_cred_shrinker); |
654 | return 0; | ||
655 | out2: | ||
656 | rpc_destroy_authunix(); | ||
657 | out1: | ||
658 | return err; | ||
595 | } | 659 | } |
596 | 660 | ||
597 | void __exit rpcauth_remove_module(void) | 661 | void __exit rpcauth_remove_module(void) |
598 | { | 662 | { |
663 | rpc_destroy_authunix(); | ||
664 | rpc_destroy_generic_auth(); | ||
599 | unregister_shrinker(&rpc_cred_shrinker); | 665 | unregister_shrinker(&rpc_cred_shrinker); |
600 | } | 666 | } |