2 * linux/net/sunrpc/auth_gss.c
4 * RPCSEC_GSS client authentication.
6 * Copyright (c) 2000 The Regents of the University of Michigan.
9 * Dug Song <dugsong@monkey.org>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
41 #include <linux/module.h>
42 #include <linux/init.h>
43 #include <linux/types.h>
44 #include <linux/slab.h>
45 #include <linux/socket.h>
47 #include <linux/sched.h>
48 #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>
58 static struct rpc_authops authgss_ops;
60 static struct rpc_credops gss_credops;
63 # define RPCDBG_FACILITY RPCDBG_AUTH
66 #define NFS_NGROUPS 16
68 #define GSS_CRED_EXPIRE (60 * HZ) /* XXX: reasonable? */
69 #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): */
72 #define GSS_VERF_SLACK 56
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 */
78 #define CA_RUN_AS_MACHINE 0x00000200
80 /* dump the buffer in `emacs-hexl' style */
81 #define isprint(c) ((c > 0x1f) && (c < 0x7f))
83 static rwlock_t gss_ctx_lock = RW_LOCK_UNLOCKED;
86 struct rpc_auth rpc_auth;
87 struct gss_api_mech *mech;
88 struct list_head upcalls;
89 struct dentry *dentry;
94 static void gss_destroy_ctx(struct gss_cl_ctx *);
95 static struct rpc_pipe_ops gss_upcall_ops;
98 print_hexl(u32 *p, u_int length, u_int offset)
103 dprintk("RPC: print_hexl: length %d\n",length);
107 for (i = 0; i < length; i += 0x10) {
108 dprintk(" %04x: ", (u_int)(i + offset));
110 jm = jm > 16 ? 16 : jm;
112 for (j = 0; j < jm; j++) {
114 dprintk("%02x ", (u_int)cp[i+j]);
116 dprintk("%02x", (u_int)cp[i+j]);
118 for (; j < 16; j++) {
126 for (j = 0; j < jm; j++) {
128 c = isprint(c) ? c : '.';
135 EXPORT_SYMBOL(print_hexl);
137 static inline struct gss_cl_ctx *
138 gss_get_ctx(struct gss_cl_ctx *ctx)
140 atomic_inc(&ctx->count);
145 gss_put_ctx(struct gss_cl_ctx *ctx)
147 if (atomic_dec_and_test(&ctx->count))
148 gss_destroy_ctx(ctx);
152 gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
154 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
155 struct gss_cl_ctx *old;
156 write_lock(&gss_ctx_lock);
157 old = gss_cred->gc_ctx;
158 gss_cred->gc_ctx = ctx;
159 cred->cr_flags |= RPCAUTH_CRED_UPTODATE;
160 write_unlock(&gss_ctx_lock);
166 gss_cred_is_uptodate_ctx(struct rpc_cred *cred)
168 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
171 read_lock(&gss_ctx_lock);
172 if ((cred->cr_flags & RPCAUTH_CRED_UPTODATE) && gss_cred->gc_ctx)
174 read_unlock(&gss_ctx_lock);
179 simple_get_bytes(char **ptr, const char *end, void *res, int len)
184 if (q > end || q < p)
192 simple_get_netobj(char **ptr, const char *end, struct xdr_netobj *res)
196 if (simple_get_bytes(&p, end, &res->len, sizeof(res->len)))
199 if (q > end || q < p)
207 dup_netobj(struct xdr_netobj *source, struct xdr_netobj *dest)
209 dest->len = source->len;
210 if (!(dest->data = kmalloc(dest->len, GFP_KERNEL)))
212 memcpy(dest->data, source->data, dest->len);
216 static struct gss_cl_ctx *
217 gss_cred_get_ctx(struct rpc_cred *cred)
219 struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
220 struct gss_cl_ctx *ctx = NULL;
222 read_lock(&gss_ctx_lock);
223 if (gss_cred->gc_ctx)
224 ctx = gss_get_ctx(gss_cred->gc_ctx);
225 read_unlock(&gss_ctx_lock);
230 gss_parse_init_downcall(struct gss_api_mech *gm, struct xdr_netobj *buf,
231 struct gss_cl_ctx **gc, uid_t *uid, int *gss_err)
233 char *end = buf->data + buf->len;
235 struct gss_cl_ctx *ctx;
236 struct xdr_netobj tmp_buf;
237 unsigned int timeout;
240 if (!(ctx = kmalloc(sizeof(*ctx), GFP_KERNEL))) {
244 ctx->gc_proc = RPC_GSS_PROC_DATA;
245 ctx->gc_seq = 1; /* NetApp 6.4R1 doesn't accept seq. no. 0 */
246 spin_lock_init(&ctx->gc_seq_lock);
247 atomic_set(&ctx->count,1);
249 if (simple_get_bytes(&p, end, uid, sizeof(uid)))
251 /* FIXME: discarded timeout for now */
252 if (simple_get_bytes(&p, end, &timeout, sizeof(timeout)))
255 if (simple_get_bytes(&p, end, &ctx->gc_win, sizeof(ctx->gc_win)))
257 /* gssd signals an error by passing ctx->gc_win = 0: */
259 /* in which case the next int is an error code: */
260 if (simple_get_bytes(&p, end, gss_err, sizeof(*gss_err)))
265 if (simple_get_netobj(&p, end, &tmp_buf))
267 if (dup_netobj(&tmp_buf, &ctx->gc_wire_ctx)) {
271 if (simple_get_netobj(&p, end, &tmp_buf))
272 goto err_free_wire_ctx;
274 goto err_free_wire_ctx;
275 if (gss_import_sec_context(&tmp_buf, gm, &ctx->gc_gss_ctx))
276 goto err_free_wire_ctx;
280 kfree(ctx->gc_wire_ctx.data);
285 dprintk("RPC: gss_parse_init_downcall returning %d\n", err);
290 struct gss_upcall_msg {
291 struct rpc_pipe_msg msg;
292 struct list_head list;
293 struct gss_auth *auth;
294 struct rpc_wait_queue waitq;
300 gss_release_msg(struct gss_upcall_msg *gss_msg)
302 if (!atomic_dec_and_test(&gss_msg->count))
304 BUG_ON(!list_empty(&gss_msg->list));
308 static struct gss_upcall_msg *
309 __gss_find_upcall(struct gss_auth *gss_auth, uid_t uid)
311 struct gss_upcall_msg *pos;
312 list_for_each_entry(pos, &gss_auth->upcalls, list) {
315 atomic_inc(&pos->count);
316 dprintk("RPC: gss_find_upcall found msg %p\n", pos);
319 dprintk("RPC: gss_find_upcall found nothing\n");
324 __gss_unhash_msg(struct gss_upcall_msg *gss_msg)
326 if (list_empty(&gss_msg->list))
328 list_del_init(&gss_msg->list);
329 if (gss_msg->msg.errno < 0)
330 rpc_wake_up_status(&gss_msg->waitq, gss_msg->msg.errno);
332 rpc_wake_up(&gss_msg->waitq);
333 atomic_dec(&gss_msg->count);
337 gss_unhash_msg(struct gss_upcall_msg *gss_msg)
339 struct gss_auth *gss_auth = gss_msg->auth;
341 spin_lock(&gss_auth->lock);
342 __gss_unhash_msg(gss_msg);
343 spin_unlock(&gss_auth->lock);
347 gss_upcall(struct rpc_clnt *clnt, struct rpc_task *task, struct rpc_cred *cred)
349 struct gss_auth *gss_auth = container_of(clnt->cl_auth,
350 struct gss_auth, rpc_auth);
351 struct gss_upcall_msg *gss_msg, *gss_new = NULL;
352 struct rpc_pipe_msg *msg;
353 struct dentry *dentry = gss_auth->dentry;
354 uid_t uid = cred->cr_uid;
357 dprintk("RPC: %4u gss_upcall for uid %u\n", task->tk_pid, uid);
360 spin_lock(&gss_auth->lock);
361 gss_msg = __gss_find_upcall(gss_auth, uid);
364 if (gss_new == NULL) {
365 spin_unlock(&gss_auth->lock);
366 gss_new = kmalloc(sizeof(*gss_new), GFP_KERNEL);
368 dprintk("RPC: %4u gss_upcall -ENOMEM\n", task->tk_pid);
374 memset(gss_new, 0, sizeof(*gss_new));
375 INIT_LIST_HEAD(&gss_new->list);
376 rpc_init_wait_queue(&gss_new->waitq, "RPCSEC_GSS upcall waitq");
377 atomic_set(&gss_new->count, 2);
379 msg->data = &gss_new->uid;
380 msg->len = sizeof(gss_new->uid);
382 gss_new->auth = gss_auth;
383 list_add(&gss_new->list, &gss_auth->upcalls);
385 /* Has someone updated the credential behind our back? */
386 if (!gss_cred_is_uptodate_ctx(cred)) {
387 /* No, so do upcall and sleep */
388 task->tk_timeout = 0;
389 rpc_sleep_on(&gss_msg->waitq, task, NULL, NULL);
390 spin_unlock(&gss_auth->lock);
391 res = rpc_queue_upcall(dentry->d_inode, msg);
393 gss_unhash_msg(gss_msg);
395 /* Yes, so cancel upcall */
396 __gss_unhash_msg(gss_msg);
397 spin_unlock(&gss_auth->lock);
399 gss_release_msg(gss_msg);
400 dprintk("RPC: %4u gss_upcall for uid %u result %d", task->tk_pid,
404 task->tk_timeout = 0;
405 rpc_sleep_on(&gss_msg->waitq, task, NULL, NULL);
406 spin_unlock(&gss_auth->lock);
407 dprintk("RPC: %4u gss_upcall sleeping\n", task->tk_pid);
410 /* Note: we drop the reference here: we are automatically removed
411 * from the queue when we're woken up, and we should in any case
412 * have no further responsabilities w.r.t. the upcall.
414 gss_release_msg(gss_msg);
419 gss_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
420 char *dst, size_t buflen)
422 char *data = (char *)msg->data + msg->copied;
423 ssize_t mlen = msg->len;
428 left = copy_to_user(dst, data, mlen);
439 #define MSG_BUF_MAXSIZE 1024
442 gss_pipe_downcall(struct file *filp, const char *src, size_t mlen)
444 struct xdr_netobj obj = {
447 struct inode *inode = filp->f_dentry->d_inode;
448 struct rpc_inode *rpci = RPC_I(inode);
449 struct rpc_clnt *clnt;
450 struct rpc_auth *auth;
451 struct gss_auth *gss_auth;
452 struct gss_api_mech *mech;
453 struct auth_cred acred = { 0 };
454 struct rpc_cred *cred;
455 struct gss_upcall_msg *gss_msg;
456 struct gss_cl_ctx *ctx = NULL;
461 if (mlen > MSG_BUF_MAXSIZE)
463 obj.data = kmalloc(mlen, GFP_KERNEL);
466 left = copy_from_user(obj.data, src, mlen);
471 clnt = rpci->private;
472 atomic_inc(&clnt->cl_users);
473 auth = clnt->cl_auth;
474 gss_auth = container_of(auth, struct gss_auth, rpc_auth);
475 mech = gss_auth->mech;
476 err = gss_parse_init_downcall(mech, &obj, &ctx, &acred.uid, &gss_err);
479 cred = rpcauth_lookup_credcache(auth, &acred, 0);
483 cred->cr_flags |= RPCAUTH_CRED_DEAD;
485 gss_cred_set_ctx(cred, ctx);
486 spin_lock(&gss_auth->lock);
487 gss_msg = __gss_find_upcall(gss_auth, acred.uid);
489 __gss_unhash_msg(gss_msg);
490 spin_unlock(&gss_auth->lock);
491 gss_release_msg(gss_msg);
493 spin_unlock(&gss_auth->lock);
494 rpc_release_client(clnt);
496 dprintk("RPC: gss_pipe_downcall returning length %Zu\n", mlen);
500 gss_destroy_ctx(ctx);
501 rpc_release_client(clnt);
504 dprintk("RPC: gss_pipe_downcall returning %d\n", err);
509 gss_pipe_release(struct inode *inode)
511 struct rpc_inode *rpci = RPC_I(inode);
512 struct rpc_clnt *clnt;
513 struct rpc_auth *auth;
514 struct gss_auth *gss_auth;
516 clnt = rpci->private;
517 auth = clnt->cl_auth;
518 gss_auth = container_of(auth, struct gss_auth, rpc_auth);
519 spin_lock(&gss_auth->lock);
520 while (!list_empty(&gss_auth->upcalls)) {
521 struct gss_upcall_msg *gss_msg;
523 gss_msg = list_entry(gss_auth->upcalls.next,
524 struct gss_upcall_msg, list);
525 gss_msg->msg.errno = -EPIPE;
526 atomic_inc(&gss_msg->count);
527 __gss_unhash_msg(gss_msg);
528 spin_unlock(&gss_auth->lock);
529 gss_release_msg(gss_msg);
530 spin_lock(&gss_auth->lock);
532 spin_unlock(&gss_auth->lock);
536 gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
538 struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
539 static unsigned long ratelimit;
541 if (msg->errno < 0) {
542 dprintk("RPC: gss_pipe_destroy_msg releasing msg %p\n",
544 atomic_inc(&gss_msg->count);
545 gss_unhash_msg(gss_msg);
546 if (msg->errno == -ETIMEDOUT || msg->errno == -EPIPE) {
547 unsigned long now = jiffies;
548 if (time_after(now, ratelimit)) {
549 printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
550 "Please check user daemon is running!\n");
551 ratelimit = now + 15*HZ;
554 gss_release_msg(gss_msg);
559 * NOTE: we have the opportunity to use different
560 * parameters based on the input flavor (which must be a pseudoflavor)
562 static struct rpc_auth *
563 gss_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
565 struct gss_auth *gss_auth;
566 struct rpc_auth * auth;
568 dprintk("RPC: creating GSS authenticator for client %p\n",clnt);
570 if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
572 gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
573 if (!gss_auth->mech) {
574 printk(KERN_WARNING "%s: Pseudoflavor %d not found!",
575 __FUNCTION__, flavor);
578 INIT_LIST_HEAD(&gss_auth->upcalls);
579 spin_lock_init(&gss_auth->lock);
580 auth = &gss_auth->rpc_auth;
581 auth->au_cslack = GSS_CRED_SLACK >> 2;
582 auth->au_rslack = GSS_VERF_SLACK >> 2;
583 auth->au_expire = GSS_CRED_EXPIRE;
584 auth->au_ops = &authgss_ops;
585 auth->au_flavor = flavor;
587 rpcauth_init_credcache(auth);
589 snprintf(gss_auth->path, sizeof(gss_auth->path), "%s/%s",
591 gss_auth->mech->gm_name);
592 gss_auth->dentry = rpc_mkpipe(gss_auth->path, clnt, &gss_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
593 if (IS_ERR(gss_auth->dentry))
604 gss_destroy(struct rpc_auth *auth)
606 struct gss_auth *gss_auth;
608 dprintk("RPC: destroying GSS authenticator %p flavor %d\n",
609 auth, auth->au_flavor);
611 gss_auth = container_of(auth, struct gss_auth, rpc_auth);
612 rpc_unlink(gss_auth->path);
614 rpcauth_free_credcache(auth);
617 /* gss_destroy_cred (and gss_destroy_ctx) are used to clean up after failure
618 * to create a new cred or context, so they check that things have been
619 * allocated before freeing them. */
621 gss_destroy_ctx(struct gss_cl_ctx *ctx)
623 dprintk("RPC: gss_destroy_ctx\n");
626 gss_delete_sec_context(&ctx->gc_gss_ctx);
628 if (ctx->gc_wire_ctx.len > 0) {
629 kfree(ctx->gc_wire_ctx.data);
630 ctx->gc_wire_ctx.len = 0;
638 gss_destroy_cred(struct rpc_cred *rc)
640 struct gss_cred *cred = (struct gss_cred *)rc;
642 dprintk("RPC: gss_destroy_cred \n");
645 gss_put_ctx(cred->gc_ctx);
649 static struct rpc_cred *
650 gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int taskflags)
652 struct gss_cred *cred = NULL;
654 dprintk("RPC: gss_create_cred for uid %d, flavor %d\n",
655 acred->uid, auth->au_flavor);
657 if (!(cred = kmalloc(sizeof(*cred), GFP_KERNEL)))
660 memset(cred, 0, sizeof(*cred));
661 atomic_set(&cred->gc_count, 0);
662 cred->gc_uid = acred->uid;
664 * Note: in order to force a call to call_refresh(), we deliberately
665 * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
668 cred->gc_base.cr_ops = &gss_credops;
669 cred->gc_flavor = auth->au_flavor;
671 return (struct rpc_cred *) cred;
674 dprintk("RPC: gss_create_cred failed\n");
675 if (cred) gss_destroy_cred((struct rpc_cred *)cred);
680 gss_match(struct auth_cred *acred, struct rpc_cred *rc, int taskflags)
682 return (rc->cr_uid == acred->uid);
686 * Marshal credentials.
687 * Maybe we should keep a cached credential for performance reasons.
690 gss_marshal(struct rpc_task *task, u32 *p, int ruid)
692 struct rpc_cred *cred = task->tk_msg.rpc_cred;
693 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
695 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
697 struct rpc_rqst *req = task->tk_rqstp;
699 struct xdr_netobj mic;
701 struct xdr_buf verf_buf;
704 dprintk("RPC: %4u gss_marshal\n", task->tk_pid);
706 *p++ = htonl(RPC_AUTH_GSS);
709 service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
710 gss_cred->gc_flavor);
712 dprintk("RPC: %4u Bad pseudoflavor %d in gss_marshal\n",
713 task->tk_pid, gss_cred->gc_flavor);
716 spin_lock(&ctx->gc_seq_lock);
717 req->rq_seqno = ctx->gc_seq++;
718 spin_unlock(&ctx->gc_seq_lock);
720 *p++ = htonl((u32) RPC_GSS_VERSION);
721 *p++ = htonl((u32) ctx->gc_proc);
722 *p++ = htonl((u32) req->rq_seqno);
723 *p++ = htonl((u32) service);
724 p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
725 *cred_len = htonl((p - (cred_len + 1)) << 2);
727 /* We compute the checksum for the verifier over the xdr-encoded bytes
728 * starting with the xid and ending at the end of the credential: */
729 iov.iov_base = req->rq_snd_buf.head[0].iov_base;
730 if (task->tk_client->cl_xprt->stream)
731 /* See clnt.c:call_header() */
733 iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
734 xdr_buf_from_iov(&iov, &verf_buf);
736 /* set verifier flavor*/
737 *p++ = htonl(RPC_AUTH_GSS);
739 mic.data = (u8 *)(p + 1);
740 maj_stat = gss_get_mic(ctx->gc_gss_ctx,
744 printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
747 p = xdr_encode_opaque(p, NULL, mic.len);
756 * Refresh credentials. XXX - finish
759 gss_refresh(struct rpc_task *task)
761 struct rpc_clnt *clnt = task->tk_client;
762 struct rpc_cred *cred = task->tk_msg.rpc_cred;
764 if (!gss_cred_is_uptodate_ctx(cred))
765 return gss_upcall(clnt, task, cred);
770 gss_validate(struct rpc_task *task, u32 *p)
772 struct rpc_cred *cred = task->tk_msg.rpc_cred;
773 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
775 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
778 struct xdr_buf verf_buf;
779 struct xdr_netobj mic;
783 dprintk("RPC: %4u gss_validate\n", task->tk_pid);
786 if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
788 if (flav != RPC_AUTH_GSS)
790 seq = htonl(task->tk_rqstp->rq_seqno);
792 iov.iov_len = sizeof(seq);
793 xdr_buf_from_iov(&iov, &verf_buf);
797 if (gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic, &qop_state))
799 service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
800 gss_cred->gc_flavor);
802 case RPC_GSS_SVC_NONE:
803 /* verifier data, flavor, length: */
804 task->tk_auth->au_rslack = XDR_QUADLEN(len) + 2;
806 case RPC_GSS_SVC_INTEGRITY:
807 /* verifier data, flavor, length, length, sequence number: */
808 task->tk_auth->au_rslack = XDR_QUADLEN(len) + 4;
814 dprintk("RPC: %4u GSS gss_validate: gss_verify_mic succeeded.\n",
816 return p + XDR_QUADLEN(len);
819 dprintk("RPC: %4u gss_validate failed.\n", task->tk_pid);
824 gss_wrap_req_integ(struct gss_cl_ctx *ctx,
825 kxdrproc_t encode, void *rqstp, u32 *p, void *obj)
827 struct rpc_rqst *req = (struct rpc_rqst *)rqstp;
828 struct xdr_buf *snd_buf = &req->rq_snd_buf;
829 struct xdr_buf integ_buf;
830 u32 *integ_len = NULL;
831 struct xdr_netobj mic;
838 offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
839 *p++ = htonl(req->rq_seqno);
841 status = encode(rqstp, p, obj);
845 if (xdr_buf_subsegment(snd_buf, &integ_buf,
846 offset, snd_buf->len - offset))
848 *integ_len = htonl(integ_buf.len);
850 /* guess whether we're in the head or the tail: */
851 if (snd_buf->page_len || snd_buf->tail[0].iov_len)
855 p = iov->iov_base + iov->iov_len;
856 mic.data = (u8 *)(p + 1);
858 maj_stat = gss_get_mic(ctx->gc_gss_ctx,
859 GSS_C_QOP_DEFAULT, &integ_buf, &mic);
860 status = -EIO; /* XXX? */
863 q = xdr_encode_opaque(p, NULL, mic.len);
865 offset = (u8 *)q - (u8 *)p;
866 iov->iov_len += offset;
867 snd_buf->len += offset;
872 gss_wrap_req(struct rpc_task *task,
873 kxdrproc_t encode, void *rqstp, u32 *p, void *obj)
875 struct rpc_cred *cred = task->tk_msg.rpc_cred;
876 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
878 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
882 dprintk("RPC: %4u gss_wrap_req\n", task->tk_pid);
883 if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
884 /* The spec seems a little ambiguous here, but I think that not
885 * wrapping context destruction requests makes the most sense.
887 status = encode(rqstp, p, obj);
890 service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
891 gss_cred->gc_flavor);
893 case RPC_GSS_SVC_NONE:
894 status = encode(rqstp, p, obj);
896 case RPC_GSS_SVC_INTEGRITY:
897 status = gss_wrap_req_integ(ctx, encode, rqstp, p, obj);
899 case RPC_GSS_SVC_PRIVACY:
905 dprintk("RPC: %4u gss_wrap_req returning %d\n", task->tk_pid, status);
910 gss_unwrap_resp_integ(struct gss_cl_ctx *ctx,
911 kxdrproc_t decode, void *rqstp, u32 **p, void *obj)
913 struct rpc_rqst *req = (struct rpc_rqst *)rqstp;
914 struct xdr_buf *rcv_buf = &req->rq_rcv_buf;
915 struct xdr_buf integ_buf;
916 struct xdr_netobj mic;
917 u32 data_offset, mic_offset;
922 integ_len = ntohl(*(*p)++);
925 data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
926 mic_offset = integ_len + data_offset;
927 if (mic_offset > rcv_buf->len)
929 if (ntohl(*(*p)++) != req->rq_seqno)
932 if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
933 mic_offset - data_offset))
936 if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
939 maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf,
941 if (maj_stat != GSS_S_COMPLETE)
947 gss_unwrap_resp(struct rpc_task *task,
948 kxdrproc_t decode, void *rqstp, u32 *p, void *obj)
950 struct rpc_cred *cred = task->tk_msg.rpc_cred;
951 struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
953 struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
957 if (ctx->gc_proc != RPC_GSS_PROC_DATA)
959 service = gss_pseudoflavor_to_service(ctx->gc_gss_ctx->mech_type,
960 gss_cred->gc_flavor);
962 case RPC_GSS_SVC_NONE:
964 case RPC_GSS_SVC_INTEGRITY:
965 status = gss_unwrap_resp_integ(ctx, decode,
970 case RPC_GSS_SVC_PRIVACY:
975 status = decode(rqstp, p, obj);
978 dprintk("RPC: %4u gss_unwrap_resp returning %d\n", task->tk_pid,
983 static struct rpc_authops authgss_ops = {
984 .owner = THIS_MODULE,
985 .au_flavor = RPC_AUTH_GSS,
987 .au_name = "RPCSEC_GSS",
989 .create = gss_create,
990 .destroy = gss_destroy,
991 .crcreate = gss_create_cred
994 static struct rpc_credops gss_credops = {
995 .crdestroy = gss_destroy_cred,
996 .crmatch = gss_match,
997 .crmarshal = gss_marshal,
998 .crrefresh = gss_refresh,
999 .crvalidate = gss_validate,
1000 .crwrap_req = gss_wrap_req,
1001 .crunwrap_resp = gss_unwrap_resp,
1004 static struct rpc_pipe_ops gss_upcall_ops = {
1005 .upcall = gss_pipe_upcall,
1006 .downcall = gss_pipe_downcall,
1007 .destroy_msg = gss_pipe_destroy_msg,
1008 .release_pipe = gss_pipe_release,
1012 * Initialize RPCSEC_GSS module
1014 static int __init init_rpcsec_gss(void)
1018 err = rpcauth_register(&authgss_ops);
1021 err = gss_svc_init();
1023 goto out_unregister;
1026 rpcauth_unregister(&authgss_ops);
1031 static void __exit exit_rpcsec_gss(void)
1034 rpcauth_unregister(&authgss_ops);
1037 MODULE_LICENSE("GPL");
1038 module_init(init_rpcsec_gss)
1039 module_exit(exit_rpcsec_gss)