Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Trond Myklebust | 25337fd | 2008-03-12 14:40:14 -0400 | [diff] [blame] | 14 | #include <linux/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/sunrpc/clnt.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | |
| 18 | #ifdef RPC_DEBUG |
| 19 | # define RPCDBG_FACILITY RPCDBG_AUTH |
| 20 | #endif |
| 21 | |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 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 | |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 31 | static DEFINE_SPINLOCK(rpc_authflavor_lock); |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 32 | static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | &authnull_ops, /* AUTH_NULL */ |
| 34 | &authunix_ops, /* AUTH_UNIX */ |
| 35 | NULL, /* others can be loadable modules */ |
| 36 | }; |
| 37 | |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 38 | static LIST_HEAD(cred_unused); |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 39 | static unsigned long number_cred_unused; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 40 | |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 41 | #define MAX_HASHTABLE_BITS (10) |
| 42 | static int param_set_hashtbl_sz(const char *val, 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, 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 | module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644); |
| 75 | MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size"); |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | static u32 |
| 78 | pseudoflavor_to_flavor(u32 flavor) { |
| 79 | if (flavor >= RPC_AUTH_MAXFLAVOR) |
| 80 | return RPC_AUTH_GSS; |
| 81 | return flavor; |
| 82 | } |
| 83 | |
| 84 | int |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 85 | rpcauth_register(const struct rpc_authops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
| 87 | rpc_authflavor_t flavor; |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 88 | int ret = -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR) |
| 91 | return -EINVAL; |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 92 | spin_lock(&rpc_authflavor_lock); |
| 93 | if (auth_flavors[flavor] == NULL) { |
| 94 | auth_flavors[flavor] = ops; |
| 95 | ret = 0; |
| 96 | } |
| 97 | spin_unlock(&rpc_authflavor_lock); |
| 98 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 100 | EXPORT_SYMBOL_GPL(rpcauth_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | int |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 103 | rpcauth_unregister(const struct rpc_authops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
| 105 | rpc_authflavor_t flavor; |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 106 | int ret = -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR) |
| 109 | return -EINVAL; |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 110 | spin_lock(&rpc_authflavor_lock); |
| 111 | if (auth_flavors[flavor] == ops) { |
| 112 | auth_flavors[flavor] = NULL; |
| 113 | ret = 0; |
| 114 | } |
| 115 | spin_unlock(&rpc_authflavor_lock); |
| 116 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 118 | EXPORT_SYMBOL_GPL(rpcauth_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | struct rpc_auth * |
| 121 | rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt) |
| 122 | { |
| 123 | struct rpc_auth *auth; |
Trond Myklebust | f1c0a86 | 2007-06-23 20:17:58 -0400 | [diff] [blame] | 124 | const struct rpc_authops *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | u32 flavor = pseudoflavor_to_flavor(pseudoflavor); |
| 126 | |
Olaf Kirch | f344f6d | 2006-03-20 13:44:08 -0500 | [diff] [blame] | 127 | auth = ERR_PTR(-EINVAL); |
| 128 | if (flavor >= RPC_AUTH_MAXFLAVOR) |
| 129 | goto out; |
| 130 | |
Olaf Kirch | f344f6d | 2006-03-20 13:44:08 -0500 | [diff] [blame] | 131 | if ((ops = auth_flavors[flavor]) == NULL) |
| 132 | request_module("rpc-auth-%u", flavor); |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 133 | spin_lock(&rpc_authflavor_lock); |
| 134 | ops = auth_flavors[flavor]; |
| 135 | if (ops == NULL || !try_module_get(ops->owner)) { |
| 136 | spin_unlock(&rpc_authflavor_lock); |
Olaf Kirch | f344f6d | 2006-03-20 13:44:08 -0500 | [diff] [blame] | 137 | goto out; |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 138 | } |
| 139 | spin_unlock(&rpc_authflavor_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | auth = ops->create(clnt, pseudoflavor); |
Trond Myklebust | fc1b356 | 2007-06-09 16:15:46 -0400 | [diff] [blame] | 141 | module_put(ops->owner); |
J. Bruce Fields | 6a19275 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 142 | if (IS_ERR(auth)) |
| 143 | return auth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | if (clnt->cl_auth) |
Trond Myklebust | de7a8ce | 2007-06-23 10:46:47 -0400 | [diff] [blame] | 145 | rpcauth_release(clnt->cl_auth); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | clnt->cl_auth = auth; |
Olaf Kirch | f344f6d | 2006-03-20 13:44:08 -0500 | [diff] [blame] | 147 | |
| 148 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | return auth; |
| 150 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 151 | EXPORT_SYMBOL_GPL(rpcauth_create); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | void |
Trond Myklebust | de7a8ce | 2007-06-23 10:46:47 -0400 | [diff] [blame] | 154 | rpcauth_release(struct rpc_auth *auth) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | { |
| 156 | if (!atomic_dec_and_test(&auth->au_count)) |
| 157 | return; |
| 158 | auth->au_ops->destroy(auth); |
| 159 | } |
| 160 | |
| 161 | static DEFINE_SPINLOCK(rpc_credcache_lock); |
| 162 | |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 163 | static void |
| 164 | rpcauth_unhash_cred_locked(struct rpc_cred *cred) |
| 165 | { |
| 166 | hlist_del_rcu(&cred->cr_hash); |
| 167 | smp_mb__before_clear_bit(); |
| 168 | clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags); |
| 169 | } |
| 170 | |
Trond Myklebust | f0380f3 | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 171 | static int |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 172 | rpcauth_unhash_cred(struct rpc_cred *cred) |
| 173 | { |
| 174 | spinlock_t *cache_lock; |
Trond Myklebust | f0380f3 | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 175 | int ret; |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 176 | |
| 177 | cache_lock = &cred->cr_auth->au_credcache->lock; |
| 178 | spin_lock(cache_lock); |
Trond Myklebust | f0380f3 | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 179 | ret = atomic_read(&cred->cr_count) == 0; |
| 180 | if (ret) |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 181 | rpcauth_unhash_cred_locked(cred); |
| 182 | spin_unlock(cache_lock); |
Trond Myklebust | f0380f3 | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 183 | return ret; |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | /* |
| 187 | * Initialize RPC credential cache |
| 188 | */ |
| 189 | int |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 190 | rpcauth_init_credcache(struct rpc_auth *auth) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
| 192 | struct rpc_cred_cache *new; |
Trond Myklebust | 988664a | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 193 | unsigned int hashsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 195 | new = kmalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | if (!new) |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 197 | goto out_nocache; |
| 198 | new->hashbits = auth_hashbits; |
Trond Myklebust | 988664a | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 199 | hashsize = 1U << new->hashbits; |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 200 | new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL); |
| 201 | if (!new->hashtable) |
| 202 | goto out_nohashtbl; |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 203 | spin_lock_init(&new->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | auth->au_credcache = new; |
| 205 | return 0; |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 206 | out_nohashtbl: |
| 207 | kfree(new); |
| 208 | out_nocache: |
| 209 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 211 | EXPORT_SYMBOL_GPL(rpcauth_init_credcache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * Destroy a list of credentials |
| 215 | */ |
| 216 | static inline |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 217 | void rpcauth_destroy_credlist(struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
| 219 | struct rpc_cred *cred; |
| 220 | |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 221 | while (!list_empty(head)) { |
| 222 | cred = list_entry(head->next, struct rpc_cred, cr_lru); |
| 223 | list_del_init(&cred->cr_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | put_rpccred(cred); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Clear the RPC credential cache, and delete those credentials |
| 230 | * that are not referenced. |
| 231 | */ |
| 232 | void |
Trond Myklebust | 3ab9bb7 | 2007-06-09 15:41:42 -0400 | [diff] [blame] | 233 | rpcauth_clear_credcache(struct rpc_cred_cache *cache) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 235 | LIST_HEAD(free); |
| 236 | struct hlist_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | struct rpc_cred *cred; |
Trond Myklebust | 988664a | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 238 | unsigned int hashsize = 1U << cache->hashbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | int i; |
| 240 | |
| 241 | spin_lock(&rpc_credcache_lock); |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 242 | spin_lock(&cache->lock); |
Trond Myklebust | 988664a | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 243 | for (i = 0; i < hashsize; i++) { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 244 | head = &cache->hashtable[i]; |
| 245 | while (!hlist_empty(head)) { |
| 246 | cred = hlist_entry(head->first, struct rpc_cred, cr_hash); |
| 247 | get_rpccred(cred); |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 248 | if (!list_empty(&cred->cr_lru)) { |
| 249 | list_del(&cred->cr_lru); |
| 250 | number_cred_unused--; |
| 251 | } |
| 252 | list_add_tail(&cred->cr_lru, &free); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 253 | rpcauth_unhash_cred_locked(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 256 | spin_unlock(&cache->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | spin_unlock(&rpc_credcache_lock); |
| 258 | rpcauth_destroy_credlist(&free); |
| 259 | } |
| 260 | |
Trond Myklebust | 3ab9bb7 | 2007-06-09 15:41:42 -0400 | [diff] [blame] | 261 | /* |
| 262 | * Destroy the RPC credential cache |
| 263 | */ |
| 264 | void |
| 265 | rpcauth_destroy_credcache(struct rpc_auth *auth) |
| 266 | { |
| 267 | struct rpc_cred_cache *cache = auth->au_credcache; |
| 268 | |
| 269 | if (cache) { |
| 270 | auth->au_credcache = NULL; |
| 271 | rpcauth_clear_credcache(cache); |
Trond Myklebust | 241269b | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 272 | kfree(cache->hashtable); |
Trond Myklebust | 3ab9bb7 | 2007-06-09 15:41:42 -0400 | [diff] [blame] | 273 | kfree(cache); |
| 274 | } |
| 275 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 276 | EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache); |
Trond Myklebust | 3ab9bb7 | 2007-06-09 15:41:42 -0400 | [diff] [blame] | 277 | |
Trond Myklebust | d2b8314 | 2008-04-14 18:13:37 -0400 | [diff] [blame] | 278 | |
| 279 | #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ) |
| 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | /* |
| 282 | * Remove stale credentials. Avoid sleeping inside the loop. |
| 283 | */ |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 284 | static int |
| 285 | rpcauth_prune_expired(struct list_head *free, int nr_to_scan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 287 | spinlock_t *cache_lock; |
Trond Myklebust | eac0d18 | 2008-10-28 15:21:41 -0400 | [diff] [blame] | 288 | struct rpc_cred *cred, *next; |
Trond Myklebust | d2b8314 | 2008-04-14 18:13:37 -0400 | [diff] [blame] | 289 | unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Trond Myklebust | eac0d18 | 2008-10-28 15:21:41 -0400 | [diff] [blame] | 291 | list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) { |
| 292 | |
Trond Myklebust | 2067340 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 293 | if (nr_to_scan-- == 0) |
| 294 | break; |
Trond Myklebust | 93a05e6 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 295 | /* |
| 296 | * Enforce a 60 second garbage collection moratorium |
| 297 | * Note that the cred_unused list must be time-ordered. |
| 298 | */ |
Trond Myklebust | 3d7b089 | 2010-04-22 15:35:55 -0400 | [diff] [blame] | 299 | if (time_in_range(cred->cr_expire, expired, jiffies) && |
Trond Myklebust | eac0d18 | 2008-10-28 15:21:41 -0400 | [diff] [blame] | 300 | test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) |
Trond Myklebust | 93a05e6 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 301 | return 0; |
Trond Myklebust | eac0d18 | 2008-10-28 15:21:41 -0400 | [diff] [blame] | 302 | |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 303 | list_del_init(&cred->cr_lru); |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 304 | number_cred_unused--; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 305 | if (atomic_read(&cred->cr_count) != 0) |
| 306 | continue; |
Trond Myklebust | eac0d18 | 2008-10-28 15:21:41 -0400 | [diff] [blame] | 307 | |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 308 | cache_lock = &cred->cr_auth->au_credcache->lock; |
| 309 | spin_lock(cache_lock); |
| 310 | if (atomic_read(&cred->cr_count) == 0) { |
| 311 | get_rpccred(cred); |
| 312 | list_add_tail(&cred->cr_lru, free); |
| 313 | rpcauth_unhash_cred_locked(cred); |
| 314 | } |
| 315 | spin_unlock(cache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | } |
Trond Myklebust | 93a05e6 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 317 | return (number_cred_unused / 100) * sysctl_vfs_cache_pressure; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /* |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 321 | * Run memory cache shrinker. |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 322 | */ |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 323 | static int |
Dave Chinner | 567c7b0 | 2010-07-21 15:33:01 +1000 | [diff] [blame] | 324 | rpcauth_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask) |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 325 | { |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 326 | LIST_HEAD(free); |
| 327 | int res; |
| 328 | |
Trond Myklebust | d300a41 | 2010-05-13 12:51:03 -0400 | [diff] [blame] | 329 | if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) |
| 330 | return (nr_to_scan == 0) ? 0 : -1; |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 331 | if (list_empty(&cred_unused)) |
| 332 | return 0; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 333 | spin_lock(&rpc_credcache_lock); |
Trond Myklebust | 93a05e6 | 2010-05-13 12:51:06 -0400 | [diff] [blame] | 334 | res = rpcauth_prune_expired(&free, nr_to_scan); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 335 | spin_unlock(&rpc_credcache_lock); |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 336 | rpcauth_destroy_credlist(&free); |
| 337 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Look up a process' credentials in the authentication cache |
| 342 | */ |
| 343 | struct rpc_cred * |
| 344 | rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred, |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 345 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 347 | LIST_HEAD(free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | struct rpc_cred_cache *cache = auth->au_credcache; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 349 | struct hlist_node *pos; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 350 | struct rpc_cred *cred = NULL, |
| 351 | *entry, *new; |
Trond Myklebust | 25337fd | 2008-03-12 14:40:14 -0400 | [diff] [blame] | 352 | unsigned int nr; |
| 353 | |
Trond Myklebust | 988664a | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 354 | nr = hash_long(acred->uid, cache->hashbits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 356 | rcu_read_lock(); |
| 357 | hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) { |
| 358 | if (!entry->cr_ops->crmatch(acred, entry, flags)) |
| 359 | continue; |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 360 | spin_lock(&cache->lock); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 361 | if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) { |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 362 | spin_unlock(&cache->lock); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 363 | continue; |
| 364 | } |
| 365 | cred = get_rpccred(entry); |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 366 | spin_unlock(&cache->lock); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 367 | break; |
| 368 | } |
| 369 | rcu_read_unlock(); |
| 370 | |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 371 | if (cred != NULL) |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 372 | goto found; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 373 | |
| 374 | new = auth->au_ops->crcreate(auth, acred, flags); |
| 375 | if (IS_ERR(new)) { |
| 376 | cred = new; |
| 377 | goto out; |
| 378 | } |
| 379 | |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 380 | spin_lock(&cache->lock); |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 381 | hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) { |
| 382 | if (!entry->cr_ops->crmatch(acred, entry, flags)) |
| 383 | continue; |
| 384 | cred = get_rpccred(entry); |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 385 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 387 | if (cred == NULL) { |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 388 | cred = new; |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 389 | set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags); |
| 390 | hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]); |
| 391 | } else |
| 392 | list_add_tail(&new->cr_lru, &free); |
Trond Myklebust | 9499b43 | 2007-06-24 15:57:57 -0400 | [diff] [blame] | 393 | spin_unlock(&cache->lock); |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 394 | found: |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 395 | if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) && |
| 396 | cred->cr_ops->cr_init != NULL && |
| 397 | !(flags & RPCAUTH_LOOKUP_NEW)) { |
Trond Myklebust | fba3bad | 2006-02-01 12:19:27 -0500 | [diff] [blame] | 398 | int res = cred->cr_ops->cr_init(auth, cred); |
| 399 | if (res < 0) { |
| 400 | put_rpccred(cred); |
| 401 | cred = ERR_PTR(res); |
| 402 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
Trond Myklebust | 31be5bf | 2007-06-24 15:55:26 -0400 | [diff] [blame] | 404 | rpcauth_destroy_credlist(&free); |
| 405 | out: |
| 406 | return cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 408 | EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
| 410 | struct rpc_cred * |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 411 | rpcauth_lookupcred(struct rpc_auth *auth, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 413 | struct auth_cred acred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | struct rpc_cred *ret; |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 415 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 417 | dprintk("RPC: looking up %s cred\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | auth->au_ops->au_name); |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 419 | |
| 420 | memset(&acred, 0, sizeof(acred)); |
| 421 | acred.uid = cred->fsuid; |
| 422 | acred.gid = cred->fsgid; |
| 423 | acred.group_info = get_group_info(((struct cred *)cred)->group_info); |
| 424 | |
Trond Myklebust | 8a31776 | 2006-02-01 12:18:36 -0500 | [diff] [blame] | 425 | ret = auth->au_ops->lookup_cred(auth, &acred, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | put_group_info(acred.group_info); |
| 427 | return ret; |
| 428 | } |
| 429 | |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 430 | void |
| 431 | rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred, |
| 432 | struct rpc_auth *auth, const struct rpc_credops *ops) |
| 433 | { |
| 434 | INIT_HLIST_NODE(&cred->cr_hash); |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 435 | INIT_LIST_HEAD(&cred->cr_lru); |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 436 | atomic_set(&cred->cr_count, 1); |
| 437 | cred->cr_auth = auth; |
| 438 | cred->cr_ops = ops; |
| 439 | cred->cr_expire = jiffies; |
| 440 | #ifdef RPC_DEBUG |
| 441 | cred->cr_magic = RPCAUTH_CRED_MAGIC; |
| 442 | #endif |
| 443 | cred->cr_uid = acred->uid; |
| 444 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 445 | EXPORT_SYMBOL_GPL(rpcauth_init_cred); |
Trond Myklebust | 5fe4755 | 2007-06-23 19:55:31 -0400 | [diff] [blame] | 446 | |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 447 | struct rpc_cred * |
Trond Myklebust | 5d35175 | 2009-09-15 13:32:13 -0400 | [diff] [blame] | 448 | rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags) |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 449 | { |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 450 | dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid, |
| 451 | cred->cr_auth->au_ops->au_name, cred); |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 452 | return get_rpccred(cred); |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 453 | } |
Trond Myklebust | 5c69104 | 2008-03-12 16:21:07 -0400 | [diff] [blame] | 454 | EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred); |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 455 | |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 456 | static struct rpc_cred * |
Trond Myklebust | 5d35175 | 2009-09-15 13:32:13 -0400 | [diff] [blame] | 457 | rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 459 | struct rpc_auth *auth = task->tk_client->cl_auth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | struct auth_cred acred = { |
Trond Myklebust | af09383 | 2008-03-12 12:12:16 -0400 | [diff] [blame] | 461 | .uid = 0, |
| 462 | .gid = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 465 | dprintk("RPC: %5u looking up %s cred\n", |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 466 | task->tk_pid, task->tk_client->cl_auth->au_ops->au_name); |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 467 | return auth->au_ops->lookup_cred(auth, &acred, lookupflags); |
Trond Myklebust | af09383 | 2008-03-12 12:12:16 -0400 | [diff] [blame] | 468 | } |
| 469 | |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 470 | static struct rpc_cred * |
Trond Myklebust | 5d35175 | 2009-09-15 13:32:13 -0400 | [diff] [blame] | 471 | rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags) |
Trond Myklebust | af09383 | 2008-03-12 12:12:16 -0400 | [diff] [blame] | 472 | { |
| 473 | struct rpc_auth *auth = task->tk_client->cl_auth; |
Trond Myklebust | af09383 | 2008-03-12 12:12:16 -0400 | [diff] [blame] | 474 | |
| 475 | dprintk("RPC: %5u looking up %s cred\n", |
| 476 | task->tk_pid, auth->au_ops->au_name); |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 477 | return rpcauth_lookupcred(auth, lookupflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 480 | static int |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 481 | rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 483 | struct rpc_rqst *req = task->tk_rqstp; |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 484 | struct rpc_cred *new; |
Trond Myklebust | 5d35175 | 2009-09-15 13:32:13 -0400 | [diff] [blame] | 485 | int lookupflags = 0; |
| 486 | |
| 487 | if (flags & RPC_TASK_ASYNC) |
| 488 | lookupflags |= RPCAUTH_LOOKUP_NEW; |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 489 | if (cred != NULL) |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 490 | new = cred->cr_ops->crbind(task, cred, lookupflags); |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 491 | else if (flags & RPC_TASK_ROOTCREDS) |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 492 | new = rpcauth_bind_root_cred(task, lookupflags); |
Trond Myklebust | 4ccda2c | 2008-03-12 16:20:55 -0400 | [diff] [blame] | 493 | else |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 494 | new = rpcauth_bind_new_cred(task, lookupflags); |
| 495 | if (IS_ERR(new)) |
| 496 | return PTR_ERR(new); |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 497 | if (req->rq_cred != NULL) |
| 498 | put_rpccred(req->rq_cred); |
| 499 | req->rq_cred = new; |
Trond Myklebust | 8572b8e | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 500 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | void |
| 504 | put_rpccred(struct rpc_cred *cred) |
| 505 | { |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 506 | /* Fast path for unhashed credentials */ |
Trond Myklebust | f0380f3 | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 507 | if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) { |
| 508 | if (atomic_dec_and_test(&cred->cr_count)) |
| 509 | cred->cr_ops->crdestroy(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | return; |
Trond Myklebust | f0380f3 | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 511 | } |
| 512 | |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 513 | if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock)) |
| 514 | return; |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 515 | if (!list_empty(&cred->cr_lru)) { |
| 516 | number_cred_unused--; |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 517 | list_del_init(&cred->cr_lru); |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 518 | } |
Trond Myklebust | 5f707eb | 2008-10-28 15:21:42 -0400 | [diff] [blame] | 519 | if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) { |
Trond Myklebust | f0380f3 | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 520 | if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) { |
| 521 | cred->cr_expire = jiffies; |
| 522 | list_add_tail(&cred->cr_lru, &cred_unused); |
| 523 | number_cred_unused++; |
| 524 | goto out_nodestroy; |
| 525 | } |
| 526 | if (!rpcauth_unhash_cred(cred)) { |
| 527 | /* We were hashed and someone looked us up... */ |
| 528 | goto out_nodestroy; |
| 529 | } |
Trond Myklebust | e092bdc | 2007-06-23 19:45:36 -0400 | [diff] [blame] | 530 | } |
| 531 | spin_unlock(&rpc_credcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | cred->cr_ops->crdestroy(cred); |
Trond Myklebust | f0380f3 | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 533 | return; |
| 534 | out_nodestroy: |
| 535 | spin_unlock(&rpc_credcache_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
Trond Myklebust | e8914c6 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 537 | EXPORT_SYMBOL_GPL(put_rpccred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 539 | __be32 * |
| 540 | rpcauth_marshcred(struct rpc_task *task, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 542 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 544 | dprintk("RPC: %5u marshaling %s cred %p\n", |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 545 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | return cred->cr_ops->crmarshal(task, p); |
| 548 | } |
| 549 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 550 | __be32 * |
| 551 | rpcauth_checkverf(struct rpc_task *task, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 553 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 555 | dprintk("RPC: %5u validating %s cred %p\n", |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 556 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | return cred->cr_ops->crvalidate(task, p); |
| 559 | } |
| 560 | |
| 561 | int |
| 562 | rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp, |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 563 | __be32 *data, void *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 565 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 567 | dprintk("RPC: %5u using %s cred %p to wrap rpc data\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | task->tk_pid, cred->cr_ops->cr_name, cred); |
| 569 | if (cred->cr_ops->crwrap_req) |
| 570 | return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj); |
| 571 | /* By default, we encode the arguments normally. */ |
Trond Myklebust | 88a9fe8 | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 572 | return encode(rqstp, data, obj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | int |
| 576 | rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp, |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 577 | __be32 *data, void *obj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 579 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 581 | dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | task->tk_pid, cred->cr_ops->cr_name, cred); |
| 583 | if (cred->cr_ops->crunwrap_resp) |
| 584 | return cred->cr_ops->crunwrap_resp(task, decode, rqstp, |
| 585 | data, obj); |
| 586 | /* By default, we decode the arguments normally. */ |
Trond Myklebust | 88a9fe8 | 2008-12-23 15:21:31 -0500 | [diff] [blame] | 587 | return decode(rqstp, data, obj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | int |
| 591 | rpcauth_refreshcred(struct rpc_task *task) |
| 592 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 593 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | int err; |
| 595 | |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 596 | cred = task->tk_rqstp->rq_cred; |
| 597 | if (cred == NULL) { |
| 598 | err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags); |
| 599 | if (err < 0) |
| 600 | goto out; |
| 601 | cred = task->tk_rqstp->rq_cred; |
| 602 | }; |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 603 | dprintk("RPC: %5u refreshing %s cred %p\n", |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 604 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | err = cred->cr_ops->crrefresh(task); |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 607 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | if (err < 0) |
| 609 | task->tk_status = err; |
| 610 | return err; |
| 611 | } |
| 612 | |
| 613 | void |
| 614 | rpcauth_invalcred(struct rpc_task *task) |
| 615 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 616 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 617 | |
Chuck Lever | 46121cf | 2007-01-31 12:14:08 -0500 | [diff] [blame] | 618 | dprintk("RPC: %5u invalidating %s cred %p\n", |
Trond Myklebust | 1be27f3 | 2007-06-27 14:29:04 -0400 | [diff] [blame] | 619 | task->tk_pid, cred->cr_auth->au_ops->au_name, cred); |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 620 | if (cred) |
| 621 | clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | int |
| 625 | rpcauth_uptodatecred(struct rpc_task *task) |
| 626 | { |
Trond Myklebust | a17c215 | 2010-07-31 14:29:08 -0400 | [diff] [blame] | 627 | struct rpc_cred *cred = task->tk_rqstp->rq_cred; |
Trond Myklebust | fc432dd | 2007-06-25 10:15:15 -0400 | [diff] [blame] | 628 | |
| 629 | return cred == NULL || |
| 630 | test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 632 | |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 633 | static struct shrinker rpc_cred_shrinker = { |
| 634 | .shrink = rpcauth_cache_shrinker, |
| 635 | .seeks = DEFAULT_SEEKS, |
| 636 | }; |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 637 | |
Trond Myklebust | 5d8d9a4 | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 638 | int __init rpcauth_init_module(void) |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 639 | { |
Trond Myklebust | 5d8d9a4 | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 640 | int err; |
| 641 | |
| 642 | err = rpc_init_authunix(); |
| 643 | if (err < 0) |
| 644 | goto out1; |
| 645 | err = rpc_init_generic_auth(); |
| 646 | if (err < 0) |
| 647 | goto out2; |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 648 | register_shrinker(&rpc_cred_shrinker); |
Trond Myklebust | 5d8d9a4 | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 649 | return 0; |
| 650 | out2: |
| 651 | rpc_destroy_authunix(); |
| 652 | out1: |
| 653 | return err; |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | void __exit rpcauth_remove_module(void) |
| 657 | { |
Trond Myklebust | 5d8d9a4 | 2010-07-31 14:29:07 -0400 | [diff] [blame] | 658 | rpc_destroy_authunix(); |
| 659 | rpc_destroy_generic_auth(); |
Rusty Russell | 8e1f936 | 2007-07-17 04:03:17 -0700 | [diff] [blame] | 660 | unregister_shrinker(&rpc_cred_shrinker); |
Trond Myklebust | f5c2187 | 2007-06-25 17:11:20 -0400 | [diff] [blame] | 661 | } |