blob: abfda33bac64635a260e79ed2c5c1ce29fd5a53a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * linux/net/sunrpc/auth_gss/auth_gss.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * RPCSEC_GSS client authentication.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (c) 2000 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Dug Song <dugsong@monkey.org>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * $Id$
38 */
39
40
41#include <linux/module.h>
42#include <linux/init.h>
43#include <linux/types.h>
44#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sched.h>
Andrew Morton09561f42007-07-15 23:37:18 -070046#include <linux/smp_lock.h>
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -040047#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/sunrpc/clnt.h>
49#include <linux/sunrpc/auth.h>
50#include <linux/sunrpc/auth_gss.h>
51#include <linux/sunrpc/svcauth_gss.h>
52#include <linux/sunrpc/gss_err.h>
53#include <linux/workqueue.h>
54#include <linux/sunrpc/rpc_pipe_fs.h>
55#include <linux/sunrpc/gss_api.h>
56#include <asm/uaccess.h>
57
Trond Myklebustf1c0a862007-06-23 20:17:58 -040058static const struct rpc_authops authgss_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Trond Myklebustf1c0a862007-06-23 20:17:58 -040060static const struct rpc_credops gss_credops;
Trond Myklebust0df7fb72007-06-26 17:04:57 -040061static const struct rpc_credops gss_nullops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#ifdef RPC_DEBUG
64# define RPCDBG_FACILITY RPCDBG_AUTH
65#endif
66
67#define NFS_NGROUPS 16
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define GSS_CRED_SLACK 1024 /* XXX: unused */
70/* length of a krb5 verifier (48), plus data added before arguments when
71 * using integrity (two 4-byte integers): */
Olga Kornievskaiaadeb8132006-12-04 20:22:34 -050072#define GSS_VERF_SLACK 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/* XXX this define must match the gssd define
75* as it is passed to gssd to signal the use of
76* machine creds should be part of the shared rpc interface */
77
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -080078#define CA_RUN_AS_MACHINE 0x00000200
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* dump the buffer in `emacs-hexl' style */
81#define isprint(c) ((c > 0x1f) && (c < 0x7f))
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083struct gss_auth {
Trond Myklebust0285ed12007-06-27 14:29:12 -040084 struct kref kref;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 struct rpc_auth rpc_auth;
86 struct gss_api_mech *mech;
87 enum rpc_gss_svc service;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 struct rpc_clnt *client;
89 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090};
91
Trond Myklebust5d28dc82007-06-26 19:18:38 -040092static void gss_free_ctx(struct gss_cl_ctx *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static struct rpc_pipe_ops gss_upcall_ops;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static inline struct gss_cl_ctx *
96gss_get_ctx(struct gss_cl_ctx *ctx)
97{
98 atomic_inc(&ctx->count);
99 return ctx;
100}
101
102static inline void
103gss_put_ctx(struct gss_cl_ctx *ctx)
104{
105 if (atomic_dec_and_test(&ctx->count))
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400106 gss_free_ctx(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400109/* gss_cred_set_ctx:
110 * called by gss_upcall_callback and gss_create_upcall in order
111 * to set the gss context. The actual exchange of an old context
112 * and a new one is protected by the inode->i_lock.
113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static void
115gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
116{
117 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
118 struct gss_cl_ctx *old;
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 old = gss_cred->gc_ctx;
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400121 rcu_assign_pointer(gss_cred->gc_ctx, ctx);
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400122 set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
123 clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (old)
125 gss_put_ctx(old);
126}
127
128static int
129gss_cred_is_uptodate_ctx(struct rpc_cred *cred)
130{
131 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
132 int res = 0;
133
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400134 rcu_read_lock();
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400135 if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) && gss_cred->gc_ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 res = 1;
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400137 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return res;
139}
140
141static const void *
142simple_get_bytes(const void *p, const void *end, void *res, size_t len)
143{
144 const void *q = (const void *)((const char *)p + len);
145 if (unlikely(q > end || q < p))
146 return ERR_PTR(-EFAULT);
147 memcpy(res, p, len);
148 return q;
149}
150
151static inline const void *
152simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
153{
154 const void *q;
155 unsigned int len;
156
157 p = simple_get_bytes(p, end, &len, sizeof(len));
158 if (IS_ERR(p))
159 return p;
160 q = (const void *)((const char *)p + len);
161 if (unlikely(q > end || q < p))
162 return ERR_PTR(-EFAULT);
Arnaldo Carvalho de Meloe69062b2006-11-21 01:21:34 -0200163 dest->data = kmemdup(p, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (unlikely(dest->data == NULL))
165 return ERR_PTR(-ENOMEM);
166 dest->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return q;
168}
169
170static struct gss_cl_ctx *
171gss_cred_get_ctx(struct rpc_cred *cred)
172{
173 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
174 struct gss_cl_ctx *ctx = NULL;
175
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400176 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (gss_cred->gc_ctx)
178 ctx = gss_get_ctx(gss_cred->gc_ctx);
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400179 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return ctx;
181}
182
183static struct gss_cl_ctx *
184gss_alloc_context(void)
185{
186 struct gss_cl_ctx *ctx;
187
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700188 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (ctx != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 ctx->gc_proc = RPC_GSS_PROC_DATA;
191 ctx->gc_seq = 1; /* NetApp 6.4R1 doesn't accept seq. no. 0 */
192 spin_lock_init(&ctx->gc_seq_lock);
193 atomic_set(&ctx->count,1);
194 }
195 return ctx;
196}
197
198#define GSSD_MIN_TIMEOUT (60 * 60)
199static const void *
200gss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
201{
202 const void *q;
203 unsigned int seclen;
204 unsigned int timeout;
205 u32 window_size;
206 int ret;
207
208 /* First unsigned int gives the lifetime (in seconds) of the cred */
209 p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
210 if (IS_ERR(p))
211 goto err;
212 if (timeout == 0)
213 timeout = GSSD_MIN_TIMEOUT;
214 ctx->gc_expiry = jiffies + (unsigned long)timeout * HZ * 3 / 4;
215 /* Sequence number window. Determines the maximum number of simultaneous requests */
216 p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
217 if (IS_ERR(p))
218 goto err;
219 ctx->gc_win = window_size;
220 /* gssd signals an error by passing ctx->gc_win = 0: */
221 if (ctx->gc_win == 0) {
222 /* in which case, p points to an error code which we ignore */
223 p = ERR_PTR(-EACCES);
224 goto err;
225 }
226 /* copy the opaque wire context */
227 p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
228 if (IS_ERR(p))
229 goto err;
230 /* import the opaque security context */
231 p = simple_get_bytes(p, end, &seclen, sizeof(seclen));
232 if (IS_ERR(p))
233 goto err;
234 q = (const void *)((const char *)p + seclen);
235 if (unlikely(q > end || q < p)) {
236 p = ERR_PTR(-EFAULT);
237 goto err;
238 }
239 ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx);
240 if (ret < 0) {
241 p = ERR_PTR(ret);
242 goto err;
243 }
244 return q;
245err:
Chuck Lever8885cb32007-01-31 12:14:05 -0500246 dprintk("RPC: gss_fill_context returning %ld\n", -PTR_ERR(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return p;
248}
249
250
251struct gss_upcall_msg {
252 atomic_t count;
253 uid_t uid;
254 struct rpc_pipe_msg msg;
255 struct list_head list;
256 struct gss_auth *auth;
257 struct rpc_wait_queue rpc_waitqueue;
258 wait_queue_head_t waitqueue;
259 struct gss_cl_ctx *ctx;
260};
261
262static void
263gss_release_msg(struct gss_upcall_msg *gss_msg)
264{
265 if (!atomic_dec_and_test(&gss_msg->count))
266 return;
267 BUG_ON(!list_empty(&gss_msg->list));
268 if (gss_msg->ctx != NULL)
269 gss_put_ctx(gss_msg->ctx);
270 kfree(gss_msg);
271}
272
273static struct gss_upcall_msg *
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400274__gss_find_upcall(struct rpc_inode *rpci, uid_t uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct gss_upcall_msg *pos;
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400277 list_for_each_entry(pos, &rpci->in_downcall, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (pos->uid != uid)
279 continue;
280 atomic_inc(&pos->count);
Chuck Lever8885cb32007-01-31 12:14:05 -0500281 dprintk("RPC: gss_find_upcall found msg %p\n", pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return pos;
283 }
Chuck Lever8885cb32007-01-31 12:14:05 -0500284 dprintk("RPC: gss_find_upcall found nothing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return NULL;
286}
287
288/* Try to add a upcall to the pipefs queue.
289 * If an upcall owned by our uid already exists, then we return a reference
290 * to that upcall instead of adding the new upcall.
291 */
292static inline struct gss_upcall_msg *
293gss_add_msg(struct gss_auth *gss_auth, struct gss_upcall_msg *gss_msg)
294{
Trond Myklebustb185f832007-06-07 10:14:14 -0400295 struct inode *inode = gss_auth->dentry->d_inode;
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400296 struct rpc_inode *rpci = RPC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 struct gss_upcall_msg *old;
298
Trond Myklebustb185f832007-06-07 10:14:14 -0400299 spin_lock(&inode->i_lock);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400300 old = __gss_find_upcall(rpci, gss_msg->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (old == NULL) {
302 atomic_inc(&gss_msg->count);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400303 list_add(&gss_msg->list, &rpci->in_downcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 } else
305 gss_msg = old;
Trond Myklebustb185f832007-06-07 10:14:14 -0400306 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return gss_msg;
308}
309
310static void
311__gss_unhash_msg(struct gss_upcall_msg *gss_msg)
312{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 list_del_init(&gss_msg->list);
314 rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
315 wake_up_all(&gss_msg->waitqueue);
316 atomic_dec(&gss_msg->count);
317}
318
319static void
320gss_unhash_msg(struct gss_upcall_msg *gss_msg)
321{
322 struct gss_auth *gss_auth = gss_msg->auth;
Trond Myklebustb185f832007-06-07 10:14:14 -0400323 struct inode *inode = gss_auth->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400325 if (list_empty(&gss_msg->list))
326 return;
Trond Myklebustb185f832007-06-07 10:14:14 -0400327 spin_lock(&inode->i_lock);
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400328 if (!list_empty(&gss_msg->list))
329 __gss_unhash_msg(gss_msg);
Trond Myklebustb185f832007-06-07 10:14:14 -0400330 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
333static void
334gss_upcall_callback(struct rpc_task *task)
335{
336 struct gss_cred *gss_cred = container_of(task->tk_msg.rpc_cred,
337 struct gss_cred, gc_base);
338 struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
Trond Myklebustb185f832007-06-07 10:14:14 -0400339 struct inode *inode = gss_msg->auth->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400341 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (gss_msg->ctx)
343 gss_cred_set_ctx(task->tk_msg.rpc_cred, gss_get_ctx(gss_msg->ctx));
344 else
345 task->tk_status = gss_msg->msg.errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 gss_cred->gc_upcall = NULL;
347 rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
Trond Myklebustb185f832007-06-07 10:14:14 -0400348 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 gss_release_msg(gss_msg);
350}
351
352static inline struct gss_upcall_msg *
353gss_alloc_msg(struct gss_auth *gss_auth, uid_t uid)
354{
355 struct gss_upcall_msg *gss_msg;
356
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700357 gss_msg = kzalloc(sizeof(*gss_msg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (gss_msg != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 INIT_LIST_HEAD(&gss_msg->list);
360 rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
361 init_waitqueue_head(&gss_msg->waitqueue);
362 atomic_set(&gss_msg->count, 1);
363 gss_msg->msg.data = &gss_msg->uid;
364 gss_msg->msg.len = sizeof(gss_msg->uid);
365 gss_msg->uid = uid;
366 gss_msg->auth = gss_auth;
367 }
368 return gss_msg;
369}
370
371static struct gss_upcall_msg *
372gss_setup_upcall(struct rpc_clnt *clnt, struct gss_auth *gss_auth, struct rpc_cred *cred)
373{
374 struct gss_upcall_msg *gss_new, *gss_msg;
375
376 gss_new = gss_alloc_msg(gss_auth, cred->cr_uid);
377 if (gss_new == NULL)
378 return ERR_PTR(-ENOMEM);
379 gss_msg = gss_add_msg(gss_auth, gss_new);
380 if (gss_msg == gss_new) {
381 int res = rpc_queue_upcall(gss_auth->dentry->d_inode, &gss_new->msg);
382 if (res) {
383 gss_unhash_msg(gss_new);
384 gss_msg = ERR_PTR(res);
385 }
386 } else
387 gss_release_msg(gss_new);
388 return gss_msg;
389}
390
391static inline int
392gss_refresh_upcall(struct rpc_task *task)
393{
394 struct rpc_cred *cred = task->tk_msg.rpc_cred;
Trond Myklebust4a8c1342007-06-07 10:14:14 -0400395 struct gss_auth *gss_auth = container_of(cred->cr_auth,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct gss_auth, rpc_auth);
397 struct gss_cred *gss_cred = container_of(cred,
398 struct gss_cred, gc_base);
399 struct gss_upcall_msg *gss_msg;
Trond Myklebustb185f832007-06-07 10:14:14 -0400400 struct inode *inode = gss_auth->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 int err = 0;
402
Chuck Lever8885cb32007-01-31 12:14:05 -0500403 dprintk("RPC: %5u gss_refresh_upcall for uid %u\n", task->tk_pid,
404 cred->cr_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 gss_msg = gss_setup_upcall(task->tk_client, gss_auth, cred);
406 if (IS_ERR(gss_msg)) {
407 err = PTR_ERR(gss_msg);
408 goto out;
409 }
Trond Myklebustb185f832007-06-07 10:14:14 -0400410 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (gss_cred->gc_upcall != NULL)
412 rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL, NULL);
413 else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
414 task->tk_timeout = 0;
415 gss_cred->gc_upcall = gss_msg;
416 /* gss_upcall_callback will release the reference to gss_upcall_msg */
417 atomic_inc(&gss_msg->count);
418 rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback, NULL);
419 } else
420 err = gss_msg->msg.errno;
Trond Myklebustb185f832007-06-07 10:14:14 -0400421 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 gss_release_msg(gss_msg);
423out:
Chuck Lever8885cb32007-01-31 12:14:05 -0500424 dprintk("RPC: %5u gss_refresh_upcall for uid %u result %d\n",
425 task->tk_pid, cred->cr_uid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return err;
427}
428
429static inline int
430gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
431{
Trond Myklebustb185f832007-06-07 10:14:14 -0400432 struct inode *inode = gss_auth->dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct rpc_cred *cred = &gss_cred->gc_base;
434 struct gss_upcall_msg *gss_msg;
435 DEFINE_WAIT(wait);
436 int err = 0;
437
Chuck Lever8885cb32007-01-31 12:14:05 -0500438 dprintk("RPC: gss_upcall for uid %u\n", cred->cr_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 gss_msg = gss_setup_upcall(gss_auth->client, gss_auth, cred);
440 if (IS_ERR(gss_msg)) {
441 err = PTR_ERR(gss_msg);
442 goto out;
443 }
444 for (;;) {
445 prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_INTERRUPTIBLE);
Trond Myklebustb185f832007-06-07 10:14:14 -0400446 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 break;
449 }
Trond Myklebustb185f832007-06-07 10:14:14 -0400450 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (signalled()) {
452 err = -ERESTARTSYS;
453 goto out_intr;
454 }
455 schedule();
456 }
457 if (gss_msg->ctx)
458 gss_cred_set_ctx(cred, gss_get_ctx(gss_msg->ctx));
459 else
460 err = gss_msg->msg.errno;
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400461 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462out_intr:
463 finish_wait(&gss_msg->waitqueue, &wait);
464 gss_release_msg(gss_msg);
465out:
Chuck Lever8885cb32007-01-31 12:14:05 -0500466 dprintk("RPC: gss_create_upcall for uid %u result %d\n",
467 cred->cr_uid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return err;
469}
470
471static ssize_t
472gss_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
473 char __user *dst, size_t buflen)
474{
475 char *data = (char *)msg->data + msg->copied;
476 ssize_t mlen = msg->len;
477 ssize_t left;
478
479 if (mlen > buflen)
480 mlen = buflen;
481 left = copy_to_user(dst, data, mlen);
482 if (left < 0) {
483 msg->errno = left;
484 return left;
485 }
486 mlen -= left;
487 msg->copied += mlen;
488 msg->errno = 0;
489 return mlen;
490}
491
492#define MSG_BUF_MAXSIZE 1024
493
494static ssize_t
495gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
496{
497 const void *p, *end;
498 void *buf;
499 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 struct gss_upcall_msg *gss_msg;
Trond Myklebustb185f832007-06-07 10:14:14 -0400501 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct gss_cl_ctx *ctx;
503 uid_t uid;
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400504 ssize_t err = -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 if (mlen > MSG_BUF_MAXSIZE)
507 goto out;
508 err = -ENOMEM;
509 buf = kmalloc(mlen, GFP_KERNEL);
510 if (!buf)
511 goto out;
512
Trond Myklebustb185f832007-06-07 10:14:14 -0400513 clnt = RPC_I(inode)->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 err = -EFAULT;
515 if (copy_from_user(buf, src, mlen))
516 goto err;
517
518 end = (const void *)((char *)buf + mlen);
519 p = simple_get_bytes(buf, end, &uid, sizeof(uid));
520 if (IS_ERR(p)) {
521 err = PTR_ERR(p);
522 goto err;
523 }
524
525 err = -ENOMEM;
526 ctx = gss_alloc_context();
527 if (ctx == NULL)
528 goto err;
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400529
530 err = -ENOENT;
531 /* Find a matching upcall */
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400532 spin_lock(&inode->i_lock);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400533 gss_msg = __gss_find_upcall(RPC_I(inode), uid);
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400534 if (gss_msg == NULL) {
535 spin_unlock(&inode->i_lock);
536 goto err_put_ctx;
537 }
538 list_del_init(&gss_msg->list);
539 spin_unlock(&inode->i_lock);
540
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400541 p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (IS_ERR(p)) {
543 err = PTR_ERR(p);
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400544 gss_msg->msg.errno = (err == -EACCES) ? -EACCES : -EAGAIN;
545 goto err_release_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400547 gss_msg->ctx = gss_get_ctx(ctx);
548 err = mlen;
549
550err_release_msg:
Trond Myklebustb185f832007-06-07 10:14:14 -0400551 spin_lock(&inode->i_lock);
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400552 __gss_unhash_msg(gss_msg);
553 spin_unlock(&inode->i_lock);
554 gss_release_msg(gss_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555err_put_ctx:
556 gss_put_ctx(ctx);
557err:
558 kfree(buf);
559out:
Trond Myklebust3b68aae2007-06-07 10:14:15 -0400560 dprintk("RPC: gss_pipe_downcall returning %Zd\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return err;
562}
563
564static void
565gss_pipe_release(struct inode *inode)
566{
567 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400568 struct gss_upcall_msg *gss_msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Trond Myklebustb185f832007-06-07 10:14:14 -0400570 spin_lock(&inode->i_lock);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400571 while (!list_empty(&rpci->in_downcall)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400573 gss_msg = list_entry(rpci->in_downcall.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 struct gss_upcall_msg, list);
575 gss_msg->msg.errno = -EPIPE;
576 atomic_inc(&gss_msg->count);
577 __gss_unhash_msg(gss_msg);
Trond Myklebustb185f832007-06-07 10:14:14 -0400578 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 gss_release_msg(gss_msg);
Trond Myklebustb185f832007-06-07 10:14:14 -0400580 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Trond Myklebustb185f832007-06-07 10:14:14 -0400582 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585static void
586gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
587{
588 struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
589 static unsigned long ratelimit;
590
591 if (msg->errno < 0) {
Chuck Lever8885cb32007-01-31 12:14:05 -0500592 dprintk("RPC: gss_pipe_destroy_msg releasing msg %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 gss_msg);
594 atomic_inc(&gss_msg->count);
595 gss_unhash_msg(gss_msg);
Trond Myklebust48e49182005-12-19 17:11:22 -0500596 if (msg->errno == -ETIMEDOUT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 unsigned long now = jiffies;
598 if (time_after(now, ratelimit)) {
599 printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
600 "Please check user daemon is running!\n");
601 ratelimit = now + 15*HZ;
602 }
603 }
604 gss_release_msg(gss_msg);
605 }
606}
607
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800608/*
609 * NOTE: we have the opportunity to use different
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * parameters based on the input flavor (which must be a pseudoflavor)
611 */
612static struct rpc_auth *
613gss_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
614{
615 struct gss_auth *gss_auth;
616 struct rpc_auth * auth;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000617 int err = -ENOMEM; /* XXX? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Chuck Lever8885cb32007-01-31 12:14:05 -0500619 dprintk("RPC: creating GSS authenticator for client %p\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (!try_module_get(THIS_MODULE))
J. Bruce Fields6a192752005-06-22 17:16:23 +0000622 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
624 goto out_dec;
625 gss_auth->client = clnt;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000626 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
628 if (!gss_auth->mech) {
629 printk(KERN_WARNING "%s: Pseudoflavor %d not found!",
630 __FUNCTION__, flavor);
631 goto err_free;
632 }
633 gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
J. Bruce Fields438b6fd2005-06-22 17:16:23 +0000634 if (gss_auth->service == 0)
635 goto err_put_mech;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 auth = &gss_auth->rpc_auth;
637 auth->au_cslack = GSS_CRED_SLACK >> 2;
638 auth->au_rslack = GSS_VERF_SLACK >> 2;
639 auth->au_ops = &authgss_ops;
640 auth->au_flavor = flavor;
641 atomic_set(&auth->au_count, 1);
Trond Myklebust0285ed12007-06-27 14:29:12 -0400642 kref_init(&gss_auth->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Trond Myklebust158998b2006-08-24 01:03:17 -0400644 gss_auth->dentry = rpc_mkpipe(clnt->cl_dentry, gss_auth->mech->gm_name,
645 clnt, &gss_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000646 if (IS_ERR(gss_auth->dentry)) {
647 err = PTR_ERR(gss_auth->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 goto err_put_mech;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Trond Myklebustf5c21872007-06-25 17:11:20 -0400651 err = rpcauth_init_credcache(auth);
Trond Myklebust07a2bf12007-06-09 15:42:01 -0400652 if (err)
653 goto err_unlink_pipe;
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return auth;
Trond Myklebust07a2bf12007-06-09 15:42:01 -0400656err_unlink_pipe:
657 rpc_unlink(gss_auth->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658err_put_mech:
659 gss_mech_put(gss_auth->mech);
660err_free:
661 kfree(gss_auth);
662out_dec:
663 module_put(THIS_MODULE);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000664 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667static void
Trond Myklebust0285ed12007-06-27 14:29:12 -0400668gss_free(struct gss_auth *gss_auth)
669{
670 rpc_unlink(gss_auth->dentry);
671 gss_auth->dentry = NULL;
672 gss_mech_put(gss_auth->mech);
673
674 kfree(gss_auth);
675 module_put(THIS_MODULE);
676}
677
678static void
679gss_free_callback(struct kref *kref)
680{
681 struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
682
683 gss_free(gss_auth);
684}
685
686static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687gss_destroy(struct rpc_auth *auth)
688{
689 struct gss_auth *gss_auth;
690
Chuck Lever8885cb32007-01-31 12:14:05 -0500691 dprintk("RPC: destroying GSS authenticator %p flavor %d\n",
692 auth, auth->au_flavor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Trond Myklebust3ab9bb72007-06-09 15:41:42 -0400694 rpcauth_destroy_credcache(auth);
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 gss_auth = container_of(auth, struct gss_auth, rpc_auth);
Trond Myklebust0285ed12007-06-27 14:29:12 -0400697 kref_put(&gss_auth->kref, gss_free_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Trond Myklebust0df7fb72007-06-26 17:04:57 -0400700/*
701 * gss_destroying_context will cause the RPCSEC_GSS to send a NULL RPC call
702 * to the server with the GSS control procedure field set to
703 * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
704 * all RPCSEC_GSS state associated with that context.
705 */
706static int
707gss_destroying_context(struct rpc_cred *cred)
708{
709 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
710 struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
711 struct rpc_task *task;
712
713 if (gss_cred->gc_ctx == NULL ||
714 gss_cred->gc_ctx->gc_proc == RPC_GSS_PROC_DESTROY)
715 return 0;
716
717 gss_cred->gc_ctx->gc_proc = RPC_GSS_PROC_DESTROY;
718 cred->cr_ops = &gss_nullops;
719
720 /* Take a reference to ensure the cred will be destroyed either
721 * by the RPC call or by the put_rpccred() below */
722 get_rpccred(cred);
723
724 task = rpc_call_null(gss_auth->client, cred, RPC_TASK_ASYNC);
725 if (!IS_ERR(task))
726 rpc_put_task(task);
727
728 put_rpccred(cred);
729 return 1;
730}
731
732/* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * to create a new cred or context, so they check that things have been
734 * allocated before freeing them. */
735static void
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400736gss_do_free_ctx(struct gss_cl_ctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400738 dprintk("RPC: gss_free_ctx\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (ctx->gc_gss_ctx)
741 gss_delete_sec_context(&ctx->gc_gss_ctx);
742
743 kfree(ctx->gc_wire_ctx.data);
744 kfree(ctx);
745}
746
747static void
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400748gss_free_ctx_callback(struct rcu_head *head)
749{
750 struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
751 gss_do_free_ctx(ctx);
752}
753
754static void
755gss_free_ctx(struct gss_cl_ctx *ctx)
756{
757 call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
758}
759
760static void
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400761gss_free_cred(struct gss_cred *gss_cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400763 dprintk("RPC: gss_free_cred %p\n", gss_cred);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400764 kfree(gss_cred);
765}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400767static void
768gss_free_cred_callback(struct rcu_head *head)
769{
770 struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
771 gss_free_cred(gss_cred);
772}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400774static void
775gss_destroy_cred(struct rpc_cred *cred)
776{
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400777 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
Trond Myklebust0285ed12007-06-27 14:29:12 -0400778 struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400779 struct gss_cl_ctx *ctx = gss_cred->gc_ctx;
780
Trond Myklebust0df7fb72007-06-26 17:04:57 -0400781 if (gss_destroying_context(cred))
782 return;
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400783 rcu_assign_pointer(gss_cred->gc_ctx, NULL);
Trond Myklebust31be5bf2007-06-24 15:55:26 -0400784 call_rcu(&cred->cr_rcu, gss_free_cred_callback);
Trond Myklebust5d28dc82007-06-26 19:18:38 -0400785 if (ctx)
786 gss_put_ctx(ctx);
Trond Myklebust0285ed12007-06-27 14:29:12 -0400787 kref_put(&gss_auth->kref, gss_free_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790/*
791 * Lookup RPCSEC_GSS cred for the current process
792 */
793static struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500794gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Trond Myklebust8a317762006-02-01 12:18:36 -0500796 return rpcauth_lookup_credcache(auth, acred, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799static struct rpc_cred *
Trond Myklebust8a317762006-02-01 12:18:36 -0500800gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
803 struct gss_cred *cred = NULL;
804 int err = -ENOMEM;
805
Chuck Lever8885cb32007-01-31 12:14:05 -0500806 dprintk("RPC: gss_create_cred for uid %d, flavor %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 acred->uid, auth->au_flavor);
808
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700809 if (!(cred = kzalloc(sizeof(*cred), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 goto out_err;
811
Trond Myklebust5fe47552007-06-23 19:55:31 -0400812 rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /*
814 * Note: in order to force a call to call_refresh(), we deliberately
815 * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
816 */
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400817 cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 cred->gc_service = gss_auth->service;
Trond Myklebust0285ed12007-06-27 14:29:12 -0400819 kref_get(&gss_auth->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return &cred->gc_base;
821
822out_err:
Chuck Lever8885cb32007-01-31 12:14:05 -0500823 dprintk("RPC: gss_create_cred failed with error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return ERR_PTR(err);
825}
826
827static int
Trond Myklebustfba3bad2006-02-01 12:19:27 -0500828gss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
829{
830 struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
831 struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
832 int err;
833
834 do {
835 err = gss_create_upcall(gss_auth, gss_cred);
836 } while (err == -EAGAIN);
837 return err;
838}
839
840static int
Trond Myklebust8a317762006-02-01 12:18:36 -0500841gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
844
Trond Myklebust8a317762006-02-01 12:18:36 -0500845 /*
846 * If the searchflags have set RPCAUTH_LOOKUP_NEW, then
847 * we don't really care if the credential has expired or not,
848 * since the caller should be prepared to reinitialise it.
849 */
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400850 if ((flags & RPCAUTH_LOOKUP_NEW) && test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
Trond Myklebust8a317762006-02-01 12:18:36 -0500851 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* Don't match with creds that have expired. */
853 if (gss_cred->gc_ctx && time_after(jiffies, gss_cred->gc_ctx->gc_expiry))
854 return 0;
Trond Myklebust8a317762006-02-01 12:18:36 -0500855out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return (rc->cr_uid == acred->uid);
857}
858
859/*
860* Marshal credentials.
861* Maybe we should keep a cached credential for performance reasons.
862*/
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700863static __be32 *
864gss_marshal(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 struct rpc_cred *cred = task->tk_msg.rpc_cred;
867 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
868 gc_base);
869 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700870 __be32 *cred_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 struct rpc_rqst *req = task->tk_rqstp;
872 u32 maj_stat = 0;
873 struct xdr_netobj mic;
874 struct kvec iov;
875 struct xdr_buf verf_buf;
876
Chuck Lever8885cb32007-01-31 12:14:05 -0500877 dprintk("RPC: %5u gss_marshal\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 *p++ = htonl(RPC_AUTH_GSS);
880 cred_len = p++;
881
882 spin_lock(&ctx->gc_seq_lock);
883 req->rq_seqno = ctx->gc_seq++;
884 spin_unlock(&ctx->gc_seq_lock);
885
886 *p++ = htonl((u32) RPC_GSS_VERSION);
887 *p++ = htonl((u32) ctx->gc_proc);
888 *p++ = htonl((u32) req->rq_seqno);
889 *p++ = htonl((u32) gss_cred->gc_service);
890 p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
891 *cred_len = htonl((p - (cred_len + 1)) << 2);
892
893 /* We compute the checksum for the verifier over the xdr-encoded bytes
894 * starting with the xid and ending at the end of the credential: */
Chuck Lever808012f2005-08-25 16:25:49 -0700895 iov.iov_base = xprt_skip_transport_header(task->tk_xprt,
896 req->rq_snd_buf.head[0].iov_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
898 xdr_buf_from_iov(&iov, &verf_buf);
899
900 /* set verifier flavor*/
901 *p++ = htonl(RPC_AUTH_GSS);
902
903 mic.data = (u8 *)(p + 1);
J. Bruce Fields00fd6e12005-10-13 16:55:18 -0400904 maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400906 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 } else if (maj_stat != 0) {
908 printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
909 goto out_put_ctx;
910 }
911 p = xdr_encode_opaque(p, NULL, mic.len);
912 gss_put_ctx(ctx);
913 return p;
914out_put_ctx:
915 gss_put_ctx(ctx);
916 return NULL;
917}
918
919/*
920* Refresh credentials. XXX - finish
921*/
922static int
923gss_refresh(struct rpc_task *task)
924{
925
926 if (!gss_cred_is_uptodate_ctx(task->tk_msg.rpc_cred))
927 return gss_refresh_upcall(task);
928 return 0;
929}
930
Trond Myklebust0df7fb72007-06-26 17:04:57 -0400931/* Dummy refresh routine: used only when destroying the context */
932static int
933gss_refresh_null(struct rpc_task *task)
934{
935 return -EACCES;
936}
937
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700938static __be32 *
939gss_validate(struct rpc_task *task, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct rpc_cred *cred = task->tk_msg.rpc_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700943 __be32 seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 struct kvec iov;
945 struct xdr_buf verf_buf;
946 struct xdr_netobj mic;
947 u32 flav,len;
948 u32 maj_stat;
949
Chuck Lever8885cb32007-01-31 12:14:05 -0500950 dprintk("RPC: %5u gss_validate\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 flav = ntohl(*p++);
953 if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800954 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (flav != RPC_AUTH_GSS)
956 goto out_bad;
957 seq = htonl(task->tk_rqstp->rq_seqno);
958 iov.iov_base = &seq;
959 iov.iov_len = sizeof(seq);
960 xdr_buf_from_iov(&iov, &verf_buf);
961 mic.data = (u8 *)p;
962 mic.len = len;
963
J. Bruce Fields00fd6e12005-10-13 16:55:18 -0400964 maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -0400966 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Trond Myklebust0df7fb72007-06-26 17:04:57 -0400967 if (maj_stat) {
968 dprintk("RPC: %5u gss_validate: gss_verify_mic returned"
969 "error 0x%08x\n", task->tk_pid, maj_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 goto out_bad;
Trond Myklebust0df7fb72007-06-26 17:04:57 -0400971 }
J. Bruce Fields24b26052005-10-13 16:54:53 -0400972 /* We leave it to unwrap to calculate au_rslack. For now we just
973 * calculate the length of the verifier: */
Trond Myklebust1be27f32007-06-27 14:29:04 -0400974 cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 gss_put_ctx(ctx);
Chuck Lever8885cb32007-01-31 12:14:05 -0500976 dprintk("RPC: %5u gss_validate: gss_verify_mic succeeded.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 task->tk_pid);
978 return p + XDR_QUADLEN(len);
979out_bad:
980 gss_put_ctx(ctx);
Chuck Lever8885cb32007-01-31 12:14:05 -0500981 dprintk("RPC: %5u gss_validate failed.\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return NULL;
983}
984
985static inline int
986gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700987 kxdrproc_t encode, struct rpc_rqst *rqstp, __be32 *p, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
989 struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
990 struct xdr_buf integ_buf;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700991 __be32 *integ_len = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 struct xdr_netobj mic;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700993 u32 offset;
994 __be32 *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 struct kvec *iov;
996 u32 maj_stat = 0;
997 int status = -EIO;
998
999 integ_len = p++;
1000 offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
1001 *p++ = htonl(rqstp->rq_seqno);
1002
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -04001003 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 status = encode(rqstp, p, obj);
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -04001005 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (status)
1007 return status;
1008
1009 if (xdr_buf_subsegment(snd_buf, &integ_buf,
1010 offset, snd_buf->len - offset))
1011 return status;
1012 *integ_len = htonl(integ_buf.len);
1013
1014 /* guess whether we're in the head or the tail: */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001015 if (snd_buf->page_len || snd_buf->tail[0].iov_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 iov = snd_buf->tail;
1017 else
1018 iov = snd_buf->head;
1019 p = iov->iov_base + iov->iov_len;
1020 mic.data = (u8 *)(p + 1);
1021
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001022 maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 status = -EIO; /* XXX? */
1024 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001025 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 else if (maj_stat)
1027 return status;
1028 q = xdr_encode_opaque(p, NULL, mic.len);
1029
1030 offset = (u8 *)q - (u8 *)p;
1031 iov->iov_len += offset;
1032 snd_buf->len += offset;
1033 return 0;
1034}
1035
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001036static void
1037priv_release_snd_buf(struct rpc_rqst *rqstp)
1038{
1039 int i;
1040
1041 for (i=0; i < rqstp->rq_enc_pages_num; i++)
1042 __free_page(rqstp->rq_enc_pages[i]);
1043 kfree(rqstp->rq_enc_pages);
1044}
1045
1046static int
1047alloc_enc_pages(struct rpc_rqst *rqstp)
1048{
1049 struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
1050 int first, last, i;
1051
1052 if (snd_buf->page_len == 0) {
1053 rqstp->rq_enc_pages_num = 0;
1054 return 0;
1055 }
1056
1057 first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
1058 last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_CACHE_SHIFT;
1059 rqstp->rq_enc_pages_num = last - first + 1 + 1;
1060 rqstp->rq_enc_pages
1061 = kmalloc(rqstp->rq_enc_pages_num * sizeof(struct page *),
1062 GFP_NOFS);
1063 if (!rqstp->rq_enc_pages)
1064 goto out;
1065 for (i=0; i < rqstp->rq_enc_pages_num; i++) {
1066 rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
1067 if (rqstp->rq_enc_pages[i] == NULL)
1068 goto out_free;
1069 }
1070 rqstp->rq_release_snd_buf = priv_release_snd_buf;
1071 return 0;
1072out_free:
1073 for (i--; i >= 0; i--) {
1074 __free_page(rqstp->rq_enc_pages[i]);
1075 }
1076out:
1077 return -EAGAIN;
1078}
1079
1080static inline int
1081gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001082 kxdrproc_t encode, struct rpc_rqst *rqstp, __be32 *p, void *obj)
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001083{
1084 struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
1085 u32 offset;
1086 u32 maj_stat;
1087 int status;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001088 __be32 *opaque_len;
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001089 struct page **inpages;
1090 int first;
1091 int pad;
1092 struct kvec *iov;
1093 char *tmp;
1094
1095 opaque_len = p++;
1096 offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
1097 *p++ = htonl(rqstp->rq_seqno);
1098
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -04001099 lock_kernel();
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001100 status = encode(rqstp, p, obj);
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -04001101 unlock_kernel();
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001102 if (status)
1103 return status;
1104
1105 status = alloc_enc_pages(rqstp);
1106 if (status)
1107 return status;
1108 first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
1109 inpages = snd_buf->pages + first;
1110 snd_buf->pages = rqstp->rq_enc_pages;
1111 snd_buf->page_base -= first << PAGE_CACHE_SHIFT;
1112 /* Give the tail its own page, in case we need extra space in the
1113 * head when wrapping: */
1114 if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
1115 tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
1116 memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
1117 snd_buf->tail[0].iov_base = tmp;
1118 }
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001119 maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001120 /* RPC_SLACK_SPACE should prevent this ever happening: */
1121 BUG_ON(snd_buf->len > snd_buf->buflen);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001122 status = -EIO;
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001123 /* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
1124 * done anyway, so it's safe to put the request on the wire: */
1125 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001126 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001127 else if (maj_stat)
1128 return status;
1129
1130 *opaque_len = htonl(snd_buf->len - offset);
1131 /* guess whether we're in the head or the tail: */
1132 if (snd_buf->page_len || snd_buf->tail[0].iov_len)
1133 iov = snd_buf->tail;
1134 else
1135 iov = snd_buf->head;
1136 p = iov->iov_base + iov->iov_len;
1137 pad = 3 - ((snd_buf->len - offset - 1) & 3);
1138 memset(p, 0, pad);
1139 iov->iov_len += pad;
1140 snd_buf->len += pad;
1141
1142 return 0;
1143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145static int
1146gss_wrap_req(struct rpc_task *task,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001147 kxdrproc_t encode, void *rqstp, __be32 *p, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 struct rpc_cred *cred = task->tk_msg.rpc_cred;
1150 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
1151 gc_base);
1152 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1153 int status = -EIO;
1154
Chuck Lever8885cb32007-01-31 12:14:05 -05001155 dprintk("RPC: %5u gss_wrap_req\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
1157 /* The spec seems a little ambiguous here, but I think that not
1158 * wrapping context destruction requests makes the most sense.
1159 */
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -04001160 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 status = encode(rqstp, p, obj);
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -04001162 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 goto out;
1164 }
1165 switch (gss_cred->gc_service) {
1166 case RPC_GSS_SVC_NONE:
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -04001167 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 status = encode(rqstp, p, obj);
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -04001169 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 break;
1171 case RPC_GSS_SVC_INTEGRITY:
1172 status = gss_wrap_req_integ(cred, ctx, encode,
1173 rqstp, p, obj);
1174 break;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001175 case RPC_GSS_SVC_PRIVACY:
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001176 status = gss_wrap_req_priv(cred, ctx, encode,
1177 rqstp, p, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 break;
1179 }
1180out:
1181 gss_put_ctx(ctx);
Chuck Lever8885cb32007-01-31 12:14:05 -05001182 dprintk("RPC: %5u gss_wrap_req returning %d\n", task->tk_pid, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return status;
1184}
1185
1186static inline int
1187gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001188 struct rpc_rqst *rqstp, __be32 **p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
1191 struct xdr_buf integ_buf;
1192 struct xdr_netobj mic;
1193 u32 data_offset, mic_offset;
1194 u32 integ_len;
1195 u32 maj_stat;
1196 int status = -EIO;
1197
1198 integ_len = ntohl(*(*p)++);
1199 if (integ_len & 3)
1200 return status;
1201 data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
1202 mic_offset = integ_len + data_offset;
1203 if (mic_offset > rcv_buf->len)
1204 return status;
1205 if (ntohl(*(*p)++) != rqstp->rq_seqno)
1206 return status;
1207
1208 if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
1209 mic_offset - data_offset))
1210 return status;
1211
1212 if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
1213 return status;
1214
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001215 maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001217 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (maj_stat != GSS_S_COMPLETE)
1219 return status;
1220 return 0;
1221}
1222
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001223static inline int
1224gss_unwrap_resp_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001225 struct rpc_rqst *rqstp, __be32 **p)
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001226{
1227 struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
1228 u32 offset;
1229 u32 opaque_len;
1230 u32 maj_stat;
1231 int status = -EIO;
1232
1233 opaque_len = ntohl(*(*p)++);
1234 offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
1235 if (offset + opaque_len > rcv_buf->len)
1236 return status;
1237 /* remove padding: */
1238 rcv_buf->len = offset + opaque_len;
1239
J. Bruce Fields00fd6e12005-10-13 16:55:18 -04001240 maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001241 if (maj_stat == GSS_S_CONTEXT_EXPIRED)
Trond Myklebustfc432dd2007-06-25 10:15:15 -04001242 clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001243 if (maj_stat != GSS_S_COMPLETE)
1244 return status;
1245 if (ntohl(*(*p)++) != rqstp->rq_seqno)
1246 return status;
1247
1248 return 0;
1249}
1250
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252static int
1253gss_unwrap_resp(struct rpc_task *task,
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001254 kxdrproc_t decode, void *rqstp, __be32 *p, void *obj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 struct rpc_cred *cred = task->tk_msg.rpc_cred;
1257 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
1258 gc_base);
1259 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001260 __be32 *savedp = p;
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001261 struct kvec *head = ((struct rpc_rqst *)rqstp)->rq_rcv_buf.head;
1262 int savedlen = head->iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 int status = -EIO;
1264
1265 if (ctx->gc_proc != RPC_GSS_PROC_DATA)
1266 goto out_decode;
1267 switch (gss_cred->gc_service) {
1268 case RPC_GSS_SVC_NONE:
1269 break;
1270 case RPC_GSS_SVC_INTEGRITY:
1271 status = gss_unwrap_resp_integ(cred, ctx, rqstp, &p);
1272 if (status)
1273 goto out;
1274 break;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001275 case RPC_GSS_SVC_PRIVACY:
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001276 status = gss_unwrap_resp_priv(cred, ctx, rqstp, &p);
1277 if (status)
1278 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 break;
1280 }
J. Bruce Fields24b26052005-10-13 16:54:53 -04001281 /* take into account extra slack for integrity and privacy cases: */
Trond Myklebust1be27f32007-06-27 14:29:04 -04001282 cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + (p - savedp)
J. Bruce Fields2d2da60c2005-10-13 16:54:58 -04001283 + (savedlen - head->iov_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284out_decode:
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -04001285 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 status = decode(rqstp, p, obj);
J. Bruce Fieldsd8558f92007-07-10 15:19:26 -04001287 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288out:
1289 gss_put_ctx(ctx);
Chuck Lever8885cb32007-01-31 12:14:05 -05001290 dprintk("RPC: %5u gss_unwrap_resp returning %d\n", task->tk_pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 status);
1292 return status;
1293}
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001294
Trond Myklebustf1c0a862007-06-23 20:17:58 -04001295static const struct rpc_authops authgss_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 .owner = THIS_MODULE,
1297 .au_flavor = RPC_AUTH_GSS,
1298#ifdef RPC_DEBUG
1299 .au_name = "RPCSEC_GSS",
1300#endif
1301 .create = gss_create,
1302 .destroy = gss_destroy,
1303 .lookup_cred = gss_lookup_cred,
1304 .crcreate = gss_create_cred
1305};
1306
Trond Myklebustf1c0a862007-06-23 20:17:58 -04001307static const struct rpc_credops gss_credops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 .cr_name = "AUTH_GSS",
1309 .crdestroy = gss_destroy_cred,
Trond Myklebustfba3bad2006-02-01 12:19:27 -05001310 .cr_init = gss_cred_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 .crmatch = gss_match,
1312 .crmarshal = gss_marshal,
1313 .crrefresh = gss_refresh,
1314 .crvalidate = gss_validate,
1315 .crwrap_req = gss_wrap_req,
1316 .crunwrap_resp = gss_unwrap_resp,
1317};
1318
Trond Myklebust0df7fb72007-06-26 17:04:57 -04001319static const struct rpc_credops gss_nullops = {
1320 .cr_name = "AUTH_GSS",
1321 .crdestroy = gss_destroy_cred,
1322 .crmatch = gss_match,
1323 .crmarshal = gss_marshal,
1324 .crrefresh = gss_refresh_null,
1325 .crvalidate = gss_validate,
1326 .crwrap_req = gss_wrap_req,
1327 .crunwrap_resp = gss_unwrap_resp,
1328};
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330static struct rpc_pipe_ops gss_upcall_ops = {
1331 .upcall = gss_pipe_upcall,
1332 .downcall = gss_pipe_downcall,
1333 .destroy_msg = gss_pipe_destroy_msg,
1334 .release_pipe = gss_pipe_release,
1335};
1336
1337/*
1338 * Initialize RPCSEC_GSS module
1339 */
1340static int __init init_rpcsec_gss(void)
1341{
1342 int err = 0;
1343
1344 err = rpcauth_register(&authgss_ops);
1345 if (err)
1346 goto out;
1347 err = gss_svc_init();
1348 if (err)
1349 goto out_unregister;
1350 return 0;
1351out_unregister:
1352 rpcauth_unregister(&authgss_ops);
1353out:
1354 return err;
1355}
1356
1357static void __exit exit_rpcsec_gss(void)
1358{
1359 gss_svc_shutdown();
1360 rpcauth_unregister(&authgss_ops);
1361}
1362
1363MODULE_LICENSE("GPL");
1364module_init(init_rpcsec_gss)
1365module_exit(exit_rpcsec_gss)