auth_gss.c revision 400f26b542e86995662a0cc5483656b7b1f42af6
11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2f30c2269544bffc7bf1b0d7c0abe5be1be83b8cbUwe Zeisberger * linux/net/sunrpc/auth_gss/auth_gss.c
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * RPCSEC_GSS client authentication.
5cca5172a7ec10dfdb0b787cd8e9d5b0b8f179793YOSHIFUJI Hideaki *
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Copyright (c) 2000 The Regents of the University of Michigan.
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  All rights reserved.
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Dug Song       <dugsong@monkey.org>
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Andy Adamson   <andros@umich.edu>
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Redistribution and use in source and binary forms, with or without
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  modification, are permitted provided that the following conditions
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  are met:
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  1. Redistributions of source code must retain the above copyright
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *     notice, this list of conditions and the following disclaimer.
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  2. Redistributions in binary form must reproduce the above copyright
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *     notice, this list of conditions and the following disclaimer in the
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *     documentation and/or other materials provided with the distribution.
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  3. Neither the name of the University nor the names of its
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *     contributors may be used to endorse or promote products derived
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *     from this software without specific prior written permission.
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h>
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/types.h>
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/slab.h>
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/sched.h>
442d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields#include <linux/pagemap.h>
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/sunrpc/clnt.h>
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/sunrpc/auth.h>
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/sunrpc/auth_gss.h>
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/sunrpc/svcauth_gss.h>
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/sunrpc/gss_err.h>
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/workqueue.h>
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/sunrpc/rpc_pipe_fs.h>
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/sunrpc/gss_api.h>
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/uaccess.h>
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
55f1c0a8615090359d57e096157feb9f900cbb233cTrond Myklebuststatic const struct rpc_authops authgss_ops;
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
57f1c0a8615090359d57e096157feb9f900cbb233cTrond Myklebuststatic const struct rpc_credops gss_credops;
580df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebuststatic const struct rpc_credops gss_nullops;
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
60126e216a8730532dfb685205309275f87e3d133eTrond Myklebust#define GSS_RETRY_EXPIRED 5
61126e216a8730532dfb685205309275f87e3d133eTrond Myklebuststatic unsigned int gss_expired_cred_retry_delay = GSS_RETRY_EXPIRED;
62126e216a8730532dfb685205309275f87e3d133eTrond Myklebust
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef RPC_DEBUG
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds# define RPCDBG_FACILITY	RPCDBG_AUTH
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
67725f2865d4df31ac0768b13ae763beadc4bb8ce9Kevin Coffman#define GSS_CRED_SLACK		(RPC_MAX_AUTH_SIZE * 2)
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* length of a krb5 verifier (48), plus data added before arguments when
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * using integrity (two 4-byte integers): */
70adeb8133dd57f380e70a389a89a2ea3ae227f9e2Olga Kornievskaia#define GSS_VERF_SLACK		100
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct gss_auth {
730285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust	struct kref kref;
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct rpc_auth rpc_auth;
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_api_mech *mech;
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	enum rpc_gss_svc service;
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct rpc_clnt *client;
7834769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	/*
7934769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	 * There are two upcall pipes; dentry[1], named "gssd", is used
8034769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	 * for the new text-based upcall; dentry[0] is named after the
8134769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	 * mechanism (for example, "krb5") and exists for
8234769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	 * backwards-compatibility with older gssd's.
8334769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	 */
84c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky	struct rpc_pipe *pipe[2];
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8779a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\/* pipe_version >= 0 if and only if someone has a pipe open. */
8879a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\static int pipe_version = -1;
89cf81939d6fcdf381fcb069d780c29eceb516bccd\"J. Bruce Fields\static atomic_t pipe_users = ATOMIC_INIT(0);
9079a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\static DEFINE_SPINLOCK(pipe_version_lock);
9179a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\static struct rpc_wait_queue pipe_version_rpc_waitqueue;
9279a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue);
93cf81939d6fcdf381fcb069d780c29eceb516bccd\"J. Bruce Fields\
945d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebuststatic void gss_free_ctx(struct gss_cl_ctx *);
95b693ba4a338da15db1db4b5ebaa36e4ab9781c82Trond Myklebuststatic const struct rpc_pipe_ops gss_upcall_ops_v0;
96b693ba4a338da15db1db4b5ebaa36e4ab9781c82Trond Myklebuststatic const struct rpc_pipe_ops gss_upcall_ops_v1;
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline struct gss_cl_ctx *
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_get_ctx(struct gss_cl_ctx *ctx)
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	atomic_inc(&ctx->count);
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ctx;
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline void
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_put_ctx(struct gss_cl_ctx *ctx)
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (atomic_dec_and_test(&ctx->count))
1095d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust		gss_free_ctx(ctx);
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1125d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust/* gss_cred_set_ctx:
1135d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust * called by gss_upcall_callback and gss_create_upcall in order
1145d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust * to set the gss context. The actual exchange of an old context
1159beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky * and a new one is protected by the pipe->lock.
1165d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust */
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1215d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust
122cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
123cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust		return;
1247b6962b0a6000df48c8a5fd967d262f77704101bTrond Myklebust	gss_get_ctx(ctx);
125cf778b00e96df6d64f8e21b8395d1f8a859ecdc7Eric Dumazet	rcu_assign_pointer(gss_cred->gc_ctx, ctx);
126fc432dd90760a629c57026e57f65ff80a1a31d2fTrond Myklebust	set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
127cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	smp_mb__before_clear_bit();
128fc432dd90760a629c57026e57f65ff80a1a31d2fTrond Myklebust	clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const void *
1321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldssimple_get_bytes(const void *p, const void *end, void *res, size_t len)
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const void *q = (const void *)((const char *)p + len);
1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (unlikely(q > end || q < p))
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ERR_PTR(-EFAULT);
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memcpy(res, p, len);
1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return q;
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline const void *
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldssimple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const void *q;
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int len;
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p = simple_get_bytes(p, end, &len, sizeof(len));
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(p))
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return p;
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	q = (const void *)((const char *)p + len);
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (unlikely(q > end || q < p))
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ERR_PTR(-EFAULT);
1530f38b873aeaae698c3693748438547c8493165fbTrond Myklebust	dest->data = kmemdup(p, len, GFP_NOFS);
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (unlikely(dest->data == NULL))
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ERR_PTR(-ENOMEM);
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dest->len = len;
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return q;
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct gss_cl_ctx *
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_cred_get_ctx(struct rpc_cred *cred)
1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cl_ctx *ctx = NULL;
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1665d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust	rcu_read_lock();
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (gss_cred->gc_ctx)
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ctx = gss_get_ctx(gss_cred->gc_ctx);
1695d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust	rcu_read_unlock();
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ctx;
1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct gss_cl_ctx *
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_alloc_context(void)
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cl_ctx *ctx;
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1780f38b873aeaae698c3693748438547c8493165fbTrond Myklebust	ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ctx != NULL) {
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ctx->gc_proc = RPC_GSS_PROC_DATA;
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ctx->gc_seq = 1;	/* NetApp 6.4R1 doesn't accept seq. no. 0 */
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		spin_lock_init(&ctx->gc_seq_lock);
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		atomic_set(&ctx->count,1);
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ctx;
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define GSSD_MIN_TIMEOUT (60 * 60)
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const void *
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const void *q;
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int seclen;
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int timeout;
195620038f6d2304475dce800dc5c75fc335a19613aAndy Adamson	unsigned long now = jiffies;
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 window_size;
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
199620038f6d2304475dce800dc5c75fc335a19613aAndy Adamson	/* First unsigned int gives the remaining lifetime in seconds of the
200620038f6d2304475dce800dc5c75fc335a19613aAndy Adamson	 * credential - e.g. the remaining TGT lifetime for Kerberos or
201620038f6d2304475dce800dc5c75fc335a19613aAndy Adamson	 * the -t value passed to GSSD.
202620038f6d2304475dce800dc5c75fc335a19613aAndy Adamson	 */
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(p))
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err;
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (timeout == 0)
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		timeout = GSSD_MIN_TIMEOUT;
208620038f6d2304475dce800dc5c75fc335a19613aAndy Adamson	ctx->gc_expiry = now + ((unsigned long)timeout * HZ);
209620038f6d2304475dce800dc5c75fc335a19613aAndy Adamson	/* Sequence number window. Determines the maximum number of
210620038f6d2304475dce800dc5c75fc335a19613aAndy Adamson	 * simultaneous requests
211620038f6d2304475dce800dc5c75fc335a19613aAndy Adamson	 */
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(p))
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err;
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ctx->gc_win = window_size;
2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* gssd signals an error by passing ctx->gc_win = 0: */
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ctx->gc_win == 0) {
218dc5ddce956660247e004a4b20a26b7d137ab1644Jeff Layton		/*
219dc5ddce956660247e004a4b20a26b7d137ab1644Jeff Layton		 * in which case, p points to an error code. Anything other
220dc5ddce956660247e004a4b20a26b7d137ab1644Jeff Layton		 * than -EKEYEXPIRED gets converted to -EACCES.
221dc5ddce956660247e004a4b20a26b7d137ab1644Jeff Layton		 */
222dc5ddce956660247e004a4b20a26b7d137ab1644Jeff Layton		p = simple_get_bytes(p, end, &ret, sizeof(ret));
223dc5ddce956660247e004a4b20a26b7d137ab1644Jeff Layton		if (!IS_ERR(p))
224dc5ddce956660247e004a4b20a26b7d137ab1644Jeff Layton			p = (ret == -EKEYEXPIRED) ? ERR_PTR(-EKEYEXPIRED) :
225dc5ddce956660247e004a4b20a26b7d137ab1644Jeff Layton						    ERR_PTR(-EACCES);
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err;
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* copy the opaque wire context */
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(p))
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err;
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* import the opaque security context */
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p  = simple_get_bytes(p, end, &seclen, sizeof(seclen));
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(p))
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err;
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	q = (const void *)((const char *)p + seclen);
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (unlikely(q > end || q < p)) {
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		p = ERR_PTR(-EFAULT);
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err;
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
241400f26b542e86995662a0cc5483656b7b1f42af6Simo Sorce	ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx, NULL, GFP_NOFS);
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ret < 0) {
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		p = ERR_PTR(ret);
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err;
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
246620038f6d2304475dce800dc5c75fc335a19613aAndy Adamson	dprintk("RPC:       %s Success. gc_expiry %lu now %lu timeout %u\n",
247620038f6d2304475dce800dc5c75fc335a19613aAndy Adamson		__func__, ctx->gc_expiry, now, timeout);
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return q;
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldserr:
250173db3093474e7691f2c2f3136313565bd366f6cJeff Layton	dprintk("RPC:       %s returns error %ld\n", __func__, -PTR_ERR(p));
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return p;
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25434769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\#define UPCALL_BUF_LEN 128
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct gss_upcall_msg {
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	atomic_t count;
2587eaf040b720bc8c0ce5cd49151ca194ca2d56842Eric W. Biederman	kuid_t	uid;
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct rpc_pipe_msg msg;
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct list_head list;
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_auth *auth;
2629beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	struct rpc_pipe *pipe;
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct rpc_wait_queue rpc_waitqueue;
2641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wait_queue_head_t waitqueue;
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cl_ctx *ctx;
26634769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	char databuf[UPCALL_BUF_LEN];
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
26979a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\static int get_pipe_version(void)
27079a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\{
27179a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	int ret;
27279a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\
27379a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	spin_lock(&pipe_version_lock);
27479a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	if (pipe_version >= 0) {
27579a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		atomic_inc(&pipe_users);
27634769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\		ret = pipe_version;
27779a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	} else
27879a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		ret = -EAGAIN;
27979a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	spin_unlock(&pipe_version_lock);
28079a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	return ret;
28179a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\}
28279a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\
28379a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\static void put_pipe_version(void)
28479a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\{
28579a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	if (atomic_dec_and_lock(&pipe_users, &pipe_version_lock)) {
28679a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		pipe_version = -1;
28779a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		spin_unlock(&pipe_version_lock);
28879a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	}
28979a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\}
29079a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_release_msg(struct gss_upcall_msg *gss_msg)
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!atomic_dec_and_test(&gss_msg->count))
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
29679a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	put_pipe_version();
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	BUG_ON(!list_empty(&gss_msg->list));
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (gss_msg->ctx != NULL)
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		gss_put_ctx(gss_msg->ctx);
300f6a1cc89309f0ae847a9b6fe418d1c4215e5bc55Trond Myklebust	rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue);
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(gss_msg);
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct gss_upcall_msg *
3057eaf040b720bc8c0ce5cd49151ca194ca2d56842Eric W. Biederman__gss_find_upcall(struct rpc_pipe *pipe, kuid_t uid)
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_upcall_msg *pos;
3089beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	list_for_each_entry(pos, &pipe->in_downcall, list) {
3090b4d51b02a2e941beec6f02a6c7a32c5a28c5b43Eric W. Biederman		if (!uid_eq(pos->uid, uid))
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		atomic_inc(&pos->count);
312632f0d0503accb8ab749a1165af99d344579c37bChuck Lever		dprintk("RPC:       %s found msg %p\n", __func__, pos);
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return pos;
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
315632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC:       %s found nothing\n", __func__);
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return NULL;
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
319720b8f2d6f7de9e16f1217448cc7396e1604e175\\\"J. Bruce Fields\\\/* Try to add an upcall to the pipefs queue.
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * If an upcall owned by our uid already exists, then we return a reference
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * to that upcall instead of adding the new upcall.
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline struct gss_upcall_msg *
324053e324f67b9921fe7de0c4cbc720d29cb4bf207Suresh Jayaramangss_add_msg(struct gss_upcall_msg *gss_msg)
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3269beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	struct rpc_pipe *pipe = gss_msg->pipe;
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_upcall_msg *old;
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3299beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_lock(&pipe->lock);
3309beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	old = __gss_find_upcall(pipe, gss_msg->uid);
3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (old == NULL) {
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		atomic_inc(&gss_msg->count);
3339beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky		list_add(&gss_msg->list, &pipe->in_downcall);
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		gss_msg = old;
3369beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_unlock(&pipe->lock);
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return gss_msg;
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds__gss_unhash_msg(struct gss_upcall_msg *gss_msg)
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_del_init(&gss_msg->list);
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wake_up_all(&gss_msg->waitqueue);
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	atomic_dec(&gss_msg->count);
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_unhash_msg(struct gss_upcall_msg *gss_msg)
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3529beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	struct rpc_pipe *pipe = gss_msg->pipe;
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3543b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust	if (list_empty(&gss_msg->list))
3553b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust		return;
3569beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_lock(&pipe->lock);
3573b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust	if (!list_empty(&gss_msg->list))
3583b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust		__gss_unhash_msg(gss_msg);
3599beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_unlock(&pipe->lock);
3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
363126e216a8730532dfb685205309275f87e3d133eTrond Myklebustgss_handle_downcall_result(struct gss_cred *gss_cred, struct gss_upcall_msg *gss_msg)
364126e216a8730532dfb685205309275f87e3d133eTrond Myklebust{
365126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	switch (gss_msg->msg.errno) {
366126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	case 0:
367126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		if (gss_msg->ctx == NULL)
368126e216a8730532dfb685205309275f87e3d133eTrond Myklebust			break;
369126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		clear_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
370126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		gss_cred_set_ctx(&gss_cred->gc_base, gss_msg->ctx);
371126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		break;
372126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	case -EKEYEXPIRED:
373126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		set_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
374126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	}
375126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	gss_cred->gc_upcall_timestamp = jiffies;
376126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	gss_cred->gc_upcall = NULL;
377126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
378126e216a8730532dfb685205309275f87e3d133eTrond Myklebust}
379126e216a8730532dfb685205309275f87e3d133eTrond Myklebust
380126e216a8730532dfb685205309275f87e3d133eTrond Myklebuststatic void
3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_upcall_callback(struct rpc_task *task)
3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
383a17c2153d2e271b0cbacae9bed83b0eaa41db7e1Trond Myklebust	struct gss_cred *gss_cred = container_of(task->tk_rqstp->rq_cred,
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			struct gss_cred, gc_base);
3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
3869beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	struct rpc_pipe *pipe = gss_msg->pipe;
3871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3889beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_lock(&pipe->lock);
389126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	gss_handle_downcall_result(gss_cred, gss_msg);
3909beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_unlock(&pipe->lock);
391126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	task->tk_status = gss_msg->msg.errno;
3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_release_msg(gss_msg);
3931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
39534769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\static void gss_encode_v0_msg(struct gss_upcall_msg *gss_msg)
39634769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\{
39790602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman	uid_t uid = from_kuid(&init_user_ns, gss_msg->uid);
39890602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman	memcpy(gss_msg->databuf, &uid, sizeof(uid));
39990602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman	gss_msg->msg.data = gss_msg->databuf;
40090602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman	gss_msg->msg.len = sizeof(uid);
40190602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman	BUG_ON(sizeof(uid) > UPCALL_BUF_LEN);
40234769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\}
40334769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\
4048b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaiastatic void gss_encode_v1_msg(struct gss_upcall_msg *gss_msg,
40568c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust				struct rpc_clnt *clnt,
40668c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust				const char *service_name)
40734769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\{
408683ac6656cb05b6e83593770ffc049eee4a4d119Trond Myklebust	struct gss_api_mech *mech = gss_msg->auth->mech;
4098b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaia	char *p = gss_msg->databuf;
4108b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaia	int len = 0;
4118b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaia
4128b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaia	gss_msg->msg.len = sprintf(gss_msg->databuf, "mech=%s uid=%d ",
413683ac6656cb05b6e83593770ffc049eee4a4d119Trond Myklebust				   mech->gm_name,
41490602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman				   from_kuid(&init_user_ns, gss_msg->uid));
4158b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaia	p += gss_msg->msg.len;
4168b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaia	if (clnt->cl_principal) {
4178b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaia		len = sprintf(p, "target=%s ", clnt->cl_principal);
4188b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaia		p += len;
4198b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaia		gss_msg->msg.len += len;
4208b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaia	}
42168c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust	if (service_name != NULL) {
42268c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust		len = sprintf(p, "service=%s ", service_name);
4232efef7080f471d312a9c4feb3dc5ee038039c7edOlga Kornievskaia		p += len;
4242efef7080f471d312a9c4feb3dc5ee038039c7edOlga Kornievskaia		gss_msg->msg.len += len;
4252efef7080f471d312a9c4feb3dc5ee038039c7edOlga Kornievskaia	}
426683ac6656cb05b6e83593770ffc049eee4a4d119Trond Myklebust	if (mech->gm_upcall_enctypes) {
427f8628220bb395104697be9c447c1085846dfc97cKevin Coffman		len = sprintf(p, "enctypes=%s ", mech->gm_upcall_enctypes);
428683ac6656cb05b6e83593770ffc049eee4a4d119Trond Myklebust		p += len;
429683ac6656cb05b6e83593770ffc049eee4a4d119Trond Myklebust		gss_msg->msg.len += len;
430683ac6656cb05b6e83593770ffc049eee4a4d119Trond Myklebust	}
4318b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaia	len = sprintf(p, "\n");
4328b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaia	gss_msg->msg.len += len;
4338b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaia
43434769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	gss_msg->msg.data = gss_msg->databuf;
43534769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	BUG_ON(gss_msg->msg.len > UPCALL_BUF_LEN);
43634769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\}
43734769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\
4388b1c7bf5b624c9bc91b41ae577b9fc5c21641705Olga Kornievskaiastatic void gss_encode_msg(struct gss_upcall_msg *gss_msg,
43968c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust				struct rpc_clnt *clnt,
44068c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust				const char *service_name)
44134769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\{
44234769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	if (pipe_version == 0)
44334769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\		gss_encode_v0_msg(gss_msg);
44434769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	else /* pipe_version == 1 */
44568c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust		gss_encode_v1_msg(gss_msg, clnt, service_name);
44634769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\}
44734769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\
44868c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebuststatic struct gss_upcall_msg *
44968c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebustgss_alloc_msg(struct gss_auth *gss_auth, struct rpc_clnt *clnt,
4507eaf040b720bc8c0ce5cd49151ca194ca2d56842Eric W. Biederman		kuid_t uid, const char *service_name)
4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_upcall_msg *gss_msg;
45379a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	int vers;
4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4550f38b873aeaae698c3693748438547c8493165fbTrond Myklebust	gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
456db75b3d6b5b0dad29860370618ea94d2726641b4\"J. Bruce Fields\	if (gss_msg == NULL)
457db75b3d6b5b0dad29860370618ea94d2726641b4\"J. Bruce Fields\		return ERR_PTR(-ENOMEM);
45879a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	vers = get_pipe_version();
45979a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	if (vers < 0) {
46079a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		kfree(gss_msg);
46179a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		return ERR_PTR(vers);
46279a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	}
463c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky	gss_msg->pipe = gss_auth->pipe[vers];
464db75b3d6b5b0dad29860370618ea94d2726641b4\"J. Bruce Fields\	INIT_LIST_HEAD(&gss_msg->list);
465db75b3d6b5b0dad29860370618ea94d2726641b4\"J. Bruce Fields\	rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
466db75b3d6b5b0dad29860370618ea94d2726641b4\"J. Bruce Fields\	init_waitqueue_head(&gss_msg->waitqueue);
467db75b3d6b5b0dad29860370618ea94d2726641b4\"J. Bruce Fields\	atomic_set(&gss_msg->count, 1);
468db75b3d6b5b0dad29860370618ea94d2726641b4\"J. Bruce Fields\	gss_msg->uid = uid;
469db75b3d6b5b0dad29860370618ea94d2726641b4\"J. Bruce Fields\	gss_msg->auth = gss_auth;
47068c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust	gss_encode_msg(gss_msg, clnt, service_name);
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return gss_msg;
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct gss_upcall_msg *
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_setup_upcall(struct rpc_clnt *clnt, struct gss_auth *gss_auth, struct rpc_cred *cred)
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4777c67db3a8a98045744f06fcd6d8f476d9df0ba5cTrond Myklebust	struct gss_cred *gss_cred = container_of(cred,
4787c67db3a8a98045744f06fcd6d8f476d9df0ba5cTrond Myklebust			struct gss_cred, gc_base);
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_upcall_msg *gss_new, *gss_msg;
4807eaf040b720bc8c0ce5cd49151ca194ca2d56842Eric W. Biederman	kuid_t uid = cred->cr_uid;
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
48268c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust	gss_new = gss_alloc_msg(gss_auth, clnt, uid, gss_cred->gc_principal);
483db75b3d6b5b0dad29860370618ea94d2726641b4\"J. Bruce Fields\	if (IS_ERR(gss_new))
484db75b3d6b5b0dad29860370618ea94d2726641b4\"J. Bruce Fields\		return gss_new;
485053e324f67b9921fe7de0c4cbc720d29cb4bf207Suresh Jayaraman	gss_msg = gss_add_msg(gss_new);
4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (gss_msg == gss_new) {
4879beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky		int res = rpc_queue_upcall(gss_new->pipe, &gss_new->msg);
4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (res) {
4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			gss_unhash_msg(gss_new);
4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			gss_msg = ERR_PTR(res);
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else
4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		gss_release_msg(gss_new);
4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return gss_msg;
4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
497b03568c32226163cb3588ea8993adb268ed497a5\"J. Bruce Fields\static void warn_gssd(void)
498b03568c32226163cb3588ea8993adb268ed497a5\"J. Bruce Fields\{
499b03568c32226163cb3588ea8993adb268ed497a5\"J. Bruce Fields\	static unsigned long ratelimit;
500b03568c32226163cb3588ea8993adb268ed497a5\"J. Bruce Fields\	unsigned long now = jiffies;
501b03568c32226163cb3588ea8993adb268ed497a5\"J. Bruce Fields\
502b03568c32226163cb3588ea8993adb268ed497a5\"J. Bruce Fields\	if (time_after(now, ratelimit)) {
503b03568c32226163cb3588ea8993adb268ed497a5\"J. Bruce Fields\		printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
504b03568c32226163cb3588ea8993adb268ed497a5\"J. Bruce Fields\				"Please check user daemon is running.\n");
505b03568c32226163cb3588ea8993adb268ed497a5\"J. Bruce Fields\		ratelimit = now + 15*HZ;
506b03568c32226163cb3588ea8993adb268ed497a5\"J. Bruce Fields\	}
507b03568c32226163cb3588ea8993adb268ed497a5\"J. Bruce Fields\}
508b03568c32226163cb3588ea8993adb268ed497a5\"J. Bruce Fields\
5091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline int
5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_refresh_upcall(struct rpc_task *task)
5111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
512a17c2153d2e271b0cbacae9bed83b0eaa41db7e1Trond Myklebust	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
5134a8c1344dccb848dbcf0edabc8b5c51a8ecf2808Trond Myklebust	struct gss_auth *gss_auth = container_of(cred->cr_auth,
5141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			struct gss_auth, rpc_auth);
5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cred *gss_cred = container_of(cred,
5161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			struct gss_cred, gc_base);
5171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_upcall_msg *gss_msg;
5189beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	struct rpc_pipe *pipe;
5191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int err = 0;
5201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
521632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC: %5u %s for uid %u\n",
522cdba321e291f0fbf5abda4d88340292b858e3d4dEric W. Biederman		task->tk_pid, __func__, from_kuid(&init_user_ns, cred->cr_uid));
5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_msg = gss_setup_upcall(task->tk_client, gss_auth, cred);
524480e3243df156e39eea6c91057e2ae612a6bbe19Roel Kluin	if (PTR_ERR(gss_msg) == -EAGAIN) {
52579a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		/* XXX: warning on the first, under the assumption we
52679a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		 * shouldn't normally hit this case on a refresh. */
52779a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		warn_gssd();
52879a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		task->tk_timeout = 15*HZ;
52979a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		rpc_sleep_on(&pipe_version_rpc_waitqueue, task, NULL);
530d1a8016a2d1e75021ecc8715e3c81442d7218eb6Bryan Schumaker		return -EAGAIN;
53179a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	}
5321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(gss_msg)) {
5331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		err = PTR_ERR(gss_msg);
5341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
5351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5369beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	pipe = gss_msg->pipe;
5379beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_lock(&pipe->lock);
5381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (gss_cred->gc_upcall != NULL)
5395d00837b90340af9106dcd93af75fd664c8eb87fTrond Myklebust		rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL);
540126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
5411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		task->tk_timeout = 0;
5421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		gss_cred->gc_upcall = gss_msg;
5431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* gss_upcall_callback will release the reference to gss_upcall_msg */
5441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		atomic_inc(&gss_msg->count);
5455d00837b90340af9106dcd93af75fd664c8eb87fTrond Myklebust		rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback);
546126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	} else {
547126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		gss_handle_downcall_result(gss_cred, gss_msg);
5481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		err = gss_msg->msg.errno;
549126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	}
5509beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_unlock(&pipe->lock);
5511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_release_msg(gss_msg);
5521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout:
553632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC: %5u %s for uid %u result %d\n",
554cdba321e291f0fbf5abda4d88340292b858e3d4dEric W. Biederman		task->tk_pid, __func__,
555cdba321e291f0fbf5abda4d88340292b858e3d4dEric W. Biederman		from_kuid(&init_user_ns, cred->cr_uid),	err);
5561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return err;
5571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline int
5601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
5611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5629beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	struct rpc_pipe *pipe;
5631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct rpc_cred *cred = &gss_cred->gc_base;
5641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_upcall_msg *gss_msg;
5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	DEFINE_WAIT(wait);
5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int err = 0;
5671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
568cdba321e291f0fbf5abda4d88340292b858e3d4dEric W. Biederman	dprintk("RPC:       %s for uid %u\n",
569cdba321e291f0fbf5abda4d88340292b858e3d4dEric W. Biederman		__func__, from_kuid(&init_user_ns, cred->cr_uid));
57079a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\retry:
5711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_msg = gss_setup_upcall(gss_auth->client, gss_auth, cred);
57279a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	if (PTR_ERR(gss_msg) == -EAGAIN) {
57379a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		err = wait_event_interruptible_timeout(pipe_version_waitqueue,
57479a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\				pipe_version >= 0, 15*HZ);
575d1a8016a2d1e75021ecc8715e3c81442d7218eb6Bryan Schumaker		if (pipe_version < 0) {
576d1a8016a2d1e75021ecc8715e3c81442d7218eb6Bryan Schumaker			warn_gssd();
577d1a8016a2d1e75021ecc8715e3c81442d7218eb6Bryan Schumaker			err = -EACCES;
578d1a8016a2d1e75021ecc8715e3c81442d7218eb6Bryan Schumaker		}
57979a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		if (err)
58079a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\			goto out;
58179a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		goto retry;
58279a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	}
5831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(gss_msg)) {
5841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		err = PTR_ERR(gss_msg);
5851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
5861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5879beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	pipe = gss_msg->pipe;
5881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (;;) {
5895afa9133cfe67f1bfead6049a9640c9262a7101cTrond Myklebust		prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_KILLABLE);
5909beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky		spin_lock(&pipe->lock);
5911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
5921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
5931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
5949beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky		spin_unlock(&pipe->lock);
5955afa9133cfe67f1bfead6049a9640c9262a7101cTrond Myklebust		if (fatal_signal_pending(current)) {
5961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			err = -ERESTARTSYS;
5971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto out_intr;
5981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
5991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		schedule();
6001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
6011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (gss_msg->ctx)
6027b6962b0a6000df48c8a5fd967d262f77704101bTrond Myklebust		gss_cred_set_ctx(cred, gss_msg->ctx);
6031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
6041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		err = gss_msg->msg.errno;
6059beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_unlock(&pipe->lock);
6061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout_intr:
6071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	finish_wait(&gss_msg->waitqueue, &wait);
6081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_release_msg(gss_msg);
6091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout:
610632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC:       %s for uid %u result %d\n",
611cdba321e291f0fbf5abda4d88340292b858e3d4dEric W. Biederman		__func__, from_kuid(&init_user_ns, cred->cr_uid), err);
6121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return err;
6131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define MSG_BUF_MAXSIZE 1024
6161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic ssize_t
6181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
6191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const void *p, *end;
6211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	void *buf;
6221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_upcall_msg *gss_msg;
623496ad9aa8ef448058e36ca7a787c61f2e63f0f54Al Viro	struct rpc_pipe *pipe = RPC_I(file_inode(filp))->pipe;
6241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cl_ctx *ctx;
62590602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman	uid_t id;
62690602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman	kuid_t uid;
6273b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust	ssize_t err = -EFBIG;
6281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (mlen > MSG_BUF_MAXSIZE)
6301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
6311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = -ENOMEM;
6320f38b873aeaae698c3693748438547c8493165fbTrond Myklebust	buf = kmalloc(mlen, GFP_NOFS);
6331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!buf)
6341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
6351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = -EFAULT;
6371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (copy_from_user(buf, src, mlen))
6381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err;
6391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	end = (const void *)((char *)buf + mlen);
64190602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman	p = simple_get_bytes(buf, end, &id, sizeof(id));
6421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(p)) {
6431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		err = PTR_ERR(p);
6441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err;
6451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
6461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
64790602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman	uid = make_kuid(&init_user_ns, id);
64890602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman	if (!uid_valid(uid)) {
64990602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman		err = -EINVAL;
65090602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman		goto err;
65190602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman	}
65290602c7b192fdd3e6b7c7623479f4bc86ed7ee34Eric W. Biederman
6531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = -ENOMEM;
6541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ctx = gss_alloc_context();
6551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ctx == NULL)
6561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err;
6573b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust
6583b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust	err = -ENOENT;
6593b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust	/* Find a matching upcall */
6609beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_lock(&pipe->lock);
6619beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	gss_msg = __gss_find_upcall(pipe, uid);
6623b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust	if (gss_msg == NULL) {
6639beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky		spin_unlock(&pipe->lock);
6643b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust		goto err_put_ctx;
6653b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust	}
6663b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust	list_del_init(&gss_msg->list);
6679beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_unlock(&pipe->lock);
6683b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust
6696e84c7b66a0aa0be16a7728d1e687c57978dac2cTrond Myklebust	p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
6701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(p)) {
6711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		err = PTR_ERR(p);
672486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton		switch (err) {
673486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton		case -EACCES:
674dc5ddce956660247e004a4b20a26b7d137ab1644Jeff Layton		case -EKEYEXPIRED:
675486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton			gss_msg->msg.errno = err;
676486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton			err = mlen;
677486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton			break;
678486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton		case -EFAULT:
679486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton		case -ENOMEM:
680486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton		case -EINVAL:
681486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton		case -ENOSYS:
682486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton			gss_msg->msg.errno = -EAGAIN;
683486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton			break;
684486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton		default:
685486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton			printk(KERN_CRIT "%s: bad return from "
6866c8530993e1fdf1d6af0403e796fe14d80b4b097Randy Dunlap				"gss_fill_context: %zd\n", __func__, err);
687486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton			BUG();
688486bad2e40e938cd68fd853b7a9fa3115a9d3a4aJeff Layton		}
6893b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust		goto err_release_msg;
6901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
6913b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust	gss_msg->ctx = gss_get_ctx(ctx);
6923b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust	err = mlen;
6933b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust
6943b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebusterr_release_msg:
6959beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_lock(&pipe->lock);
6963b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust	__gss_unhash_msg(gss_msg);
6979beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_unlock(&pipe->lock);
6983b68aaeaf54065e5c44583a1d33ffb7793953ba4Trond Myklebust	gss_release_msg(gss_msg);
6991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldserr_put_ctx:
7001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_put_ctx(ctx);
7011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldserr:
7021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(buf);
7031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout:
704632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC:       %s returning %Zd\n", __func__, err);
7051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return err;
7061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
70834769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\static int gss_pipe_open(struct inode *inode, int new_version)
709cf81939d6fcdf381fcb069d780c29eceb516bccd\"J. Bruce Fields\{
71034769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	int ret = 0;
71134769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\
71279a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	spin_lock(&pipe_version_lock);
71379a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	if (pipe_version < 0) {
71434769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\		/* First open of any gss pipe determines the version: */
71534769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\		pipe_version = new_version;
71679a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		rpc_wake_up(&pipe_version_rpc_waitqueue);
71779a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\		wake_up(&pipe_version_waitqueue);
71834769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	} else if (pipe_version != new_version) {
71934769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\		/* Trying to open a pipe of a different version */
72034769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\		ret = -EBUSY;
72134769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\		goto out;
72279a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	}
723cf81939d6fcdf381fcb069d780c29eceb516bccd\"J. Bruce Fields\	atomic_inc(&pipe_users);
72434769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\out:
72579a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	spin_unlock(&pipe_version_lock);
72634769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	return ret;
72734769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\
72834769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\}
72934769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\
73034769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\static int gss_pipe_open_v0(struct inode *inode)
73134769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\{
73234769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	return gss_pipe_open(inode, 0);
73334769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\}
73434769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\
73534769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\static int gss_pipe_open_v1(struct inode *inode)
73634769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\{
73734769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	return gss_pipe_open(inode, 1);
738cf81939d6fcdf381fcb069d780c29eceb516bccd\"J. Bruce Fields\}
739cf81939d6fcdf381fcb069d780c29eceb516bccd\"J. Bruce Fields\
7401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
7411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_pipe_release(struct inode *inode)
7421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
7439beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
7446e84c7b66a0aa0be16a7728d1e687c57978dac2cTrond Myklebust	struct gss_upcall_msg *gss_msg;
7451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7465a67657a2e90c9e4a48518f95d4ba7777aa20fbbTrond Myklebustrestart:
7479beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_lock(&pipe->lock);
7489beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	list_for_each_entry(gss_msg, &pipe->in_downcall, list) {
7491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7505a67657a2e90c9e4a48518f95d4ba7777aa20fbbTrond Myklebust		if (!list_empty(&gss_msg->msg.list))
7515a67657a2e90c9e4a48518f95d4ba7777aa20fbbTrond Myklebust			continue;
7521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		gss_msg->msg.errno = -EPIPE;
7531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		atomic_inc(&gss_msg->count);
7541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		__gss_unhash_msg(gss_msg);
7559beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky		spin_unlock(&pipe->lock);
7561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		gss_release_msg(gss_msg);
7575a67657a2e90c9e4a48518f95d4ba7777aa20fbbTrond Myklebust		goto restart;
7581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
7599beae4677de76cfa4ce8899dc8cd1a1cf8cd8332Stanislav Kinsbursky	spin_unlock(&pipe->lock);
760cf81939d6fcdf381fcb069d780c29eceb516bccd\"J. Bruce Fields\
76179a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	put_pipe_version();
7621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
7651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
7661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
7671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
7681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (msg->errno < 0) {
770632f0d0503accb8ab749a1165af99d344579c37bChuck Lever		dprintk("RPC:       %s releasing msg %p\n",
771632f0d0503accb8ab749a1165af99d344579c37bChuck Lever			__func__, gss_msg);
7721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		atomic_inc(&gss_msg->count);
7731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		gss_unhash_msg(gss_msg);
774b03568c32226163cb3588ea8993adb268ed497a5\"J. Bruce Fields\		if (msg->errno == -ETIMEDOUT)
775b03568c32226163cb3588ea8993adb268ed497a5\"J. Bruce Fields\			warn_gssd();
7761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		gss_release_msg(gss_msg);
7771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
7781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
780ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsburskystatic void gss_pipes_dentries_destroy(struct rpc_auth *auth)
781ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky{
782ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	struct gss_auth *gss_auth;
783ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky
784ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	gss_auth = container_of(auth, struct gss_auth, rpc_auth);
78580df9d202255071c8ec610a6a3fdca5cac69f7bdStanislav Kinsbursky	if (gss_auth->pipe[0]->dentry)
78680df9d202255071c8ec610a6a3fdca5cac69f7bdStanislav Kinsbursky		rpc_unlink(gss_auth->pipe[0]->dentry);
78780df9d202255071c8ec610a6a3fdca5cac69f7bdStanislav Kinsbursky	if (gss_auth->pipe[1]->dentry)
78880df9d202255071c8ec610a6a3fdca5cac69f7bdStanislav Kinsbursky		rpc_unlink(gss_auth->pipe[1]->dentry);
789ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky}
790ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky
791ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsburskystatic int gss_pipes_dentries_create(struct rpc_auth *auth)
792ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky{
793ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	int err;
794ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	struct gss_auth *gss_auth;
795ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	struct rpc_clnt *clnt;
796ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky
797ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	gss_auth = container_of(auth, struct gss_auth, rpc_auth);
798ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	clnt = gss_auth->client;
799ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky
80030507f58ce11e7664512059c708347d7a7d75271Stanislav Kinsbursky	gss_auth->pipe[1]->dentry = rpc_mkpipe_dentry(clnt->cl_dentry,
801ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky						      "gssd",
802ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky						      clnt, gss_auth->pipe[1]);
803ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	if (IS_ERR(gss_auth->pipe[1]->dentry))
804ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky		return PTR_ERR(gss_auth->pipe[1]->dentry);
80530507f58ce11e7664512059c708347d7a7d75271Stanislav Kinsbursky	gss_auth->pipe[0]->dentry = rpc_mkpipe_dentry(clnt->cl_dentry,
806ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky						      gss_auth->mech->gm_name,
807ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky						      clnt, gss_auth->pipe[0]);
808ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	if (IS_ERR(gss_auth->pipe[0]->dentry)) {
809ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky		err = PTR_ERR(gss_auth->pipe[0]->dentry);
810ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky		goto err_unlink_pipe_1;
811ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	}
812ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	return 0;
813ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky
814ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsburskyerr_unlink_pipe_1:
815ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	rpc_unlink(gss_auth->pipe[1]->dentry);
816ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	return err;
817ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky}
818ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky
819ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsburskystatic void gss_pipes_dentries_destroy_net(struct rpc_clnt *clnt,
820ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky					   struct rpc_auth *auth)
821ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky{
8222446ab6070861aba2dd9229463ffbc40016a9f33Trond Myklebust	struct net *net = rpc_net_ns(clnt);
823ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	struct super_block *sb;
824ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky
825ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	sb = rpc_get_sb_net(net);
826ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	if (sb) {
82730507f58ce11e7664512059c708347d7a7d75271Stanislav Kinsbursky		if (clnt->cl_dentry)
828ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky			gss_pipes_dentries_destroy(auth);
829ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky		rpc_put_sb_net(net);
830ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	}
831ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky}
832ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky
833ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsburskystatic int gss_pipes_dentries_create_net(struct rpc_clnt *clnt,
834ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky					 struct rpc_auth *auth)
835ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky{
8362446ab6070861aba2dd9229463ffbc40016a9f33Trond Myklebust	struct net *net = rpc_net_ns(clnt);
837ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	struct super_block *sb;
838ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	int err = 0;
839ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky
840ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	sb = rpc_get_sb_net(net);
841ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	if (sb) {
84230507f58ce11e7664512059c708347d7a7d75271Stanislav Kinsbursky		if (clnt->cl_dentry)
843ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky			err = gss_pipes_dentries_create(auth);
844ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky		rpc_put_sb_net(net);
845ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	}
846ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	return err;
847ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky}
848ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky
849cca5172a7ec10dfdb0b787cd8e9d5b0b8f179793YOSHIFUJI Hideaki/*
850cca5172a7ec10dfdb0b787cd8e9d5b0b8f179793YOSHIFUJI Hideaki * NOTE: we have the opportunity to use different
8511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * parameters based on the input flavor (which must be a pseudoflavor)
8521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
8531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct rpc_auth *
8541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
8551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
8561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_auth *gss_auth;
8571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct rpc_auth * auth;
8586a19275ada9137435da58990c8f8d3f58e170bf1J. Bruce Fields	int err = -ENOMEM; /* XXX? */
8591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8608885cb367f86ce02bed3bf18192d74a53ac3b81fChuck Lever	dprintk("RPC:       creating GSS authenticator for client %p\n", clnt);
8611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!try_module_get(THIS_MODULE))
8636a19275ada9137435da58990c8f8d3f58e170bf1J. Bruce Fields		return ERR_PTR(err);
8641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
8651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out_dec;
8661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_auth->client = clnt;
8676a19275ada9137435da58990c8f8d3f58e170bf1J. Bruce Fields	err = -EINVAL;
8681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
8691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!gss_auth->mech) {
8703392c34922130d1dca9ad436c358330daa85e94eJames Morris		printk(KERN_WARNING "%s: Pseudoflavor %d not found!\n",
8710dc47877a3de00ceadea0005189656ae8dc52669Harvey Harrison				__func__, flavor);
8721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err_free;
8731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
8741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
875438b6fdebf2a2e8573e7290bc176feb4d4475f43J. Bruce Fields	if (gss_auth->service == 0)
876438b6fdebf2a2e8573e7290bc176feb4d4475f43J. Bruce Fields		goto err_put_mech;
8771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	auth = &gss_auth->rpc_auth;
8781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	auth->au_cslack = GSS_CRED_SLACK >> 2;
8791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	auth->au_rslack = GSS_VERF_SLACK >> 2;
8801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	auth->au_ops = &authgss_ops;
8811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	auth->au_flavor = flavor;
8821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	atomic_set(&auth->au_count, 1);
8830285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust	kref_init(&gss_auth->kref);
8841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
88534769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	/*
88634769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	 * Note: if we created the old pipe first, then someone who
88734769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	 * examined the directory at the right moment might conclude
88834769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	 * that we supported only the old pipe.  So we instead create
88934769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	 * the new pipe first.
89034769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	 */
891c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky	gss_auth->pipe[1] = rpc_mkpipe_data(&gss_upcall_ops_v1,
892c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky					    RPC_PIPE_WAIT_FOR_OPEN);
893c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky	if (IS_ERR(gss_auth->pipe[1])) {
894c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky		err = PTR_ERR(gss_auth->pipe[1]);
8951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto err_put_mech;
8966a19275ada9137435da58990c8f8d3f58e170bf1J. Bruce Fields	}
8971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
898c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky	gss_auth->pipe[0] = rpc_mkpipe_data(&gss_upcall_ops_v0,
899c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky					    RPC_PIPE_WAIT_FOR_OPEN);
900c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky	if (IS_ERR(gss_auth->pipe[0])) {
901c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky		err = PTR_ERR(gss_auth->pipe[0]);
902c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky		goto err_destroy_pipe_1;
903c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky	}
904ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	err = gss_pipes_dentries_create_net(clnt, auth);
905ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	if (err)
906c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky		goto err_destroy_pipe_0;
907f5c2187cfef628784d8a09b6d0f77888246d0c0fTrond Myklebust	err = rpcauth_init_credcache(auth);
90807a2bf1da4765d987ffd1d8045e92ba032e0ad78Trond Myklebust	if (err)
909ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky		goto err_unlink_pipes;
91007a2bf1da4765d987ffd1d8045e92ba032e0ad78Trond Myklebust
9111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return auth;
912ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsburskyerr_unlink_pipes:
913ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	gss_pipes_dentries_destroy_net(clnt, auth);
914c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsburskyerr_destroy_pipe_0:
915c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky	rpc_destroy_pipe_data(gss_auth->pipe[0]);
916c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsburskyerr_destroy_pipe_1:
917c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky	rpc_destroy_pipe_data(gss_auth->pipe[1]);
9181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldserr_put_mech:
9191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_mech_put(gss_auth->mech);
9201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldserr_free:
9211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(gss_auth);
9221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout_dec:
9231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	module_put(THIS_MODULE);
9246a19275ada9137435da58990c8f8d3f58e170bf1J. Bruce Fields	return ERR_PTR(err);
9251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
9280285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebustgss_free(struct gss_auth *gss_auth)
9290285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust{
930ccdc28f81c91f7ef2dc6c28d27f50264b19e4dd5Stanislav Kinsbursky	gss_pipes_dentries_destroy_net(gss_auth->client, &gss_auth->rpc_auth);
931c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky	rpc_destroy_pipe_data(gss_auth->pipe[0]);
932c239d83b9921b8a8005a3bcd23000cfe18acf5c2Stanislav Kinsbursky	rpc_destroy_pipe_data(gss_auth->pipe[1]);
9330285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust	gss_mech_put(gss_auth->mech);
9340285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust
9350285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust	kfree(gss_auth);
9360285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust	module_put(THIS_MODULE);
9370285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust}
9380285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust
9390285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebuststatic void
9400285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebustgss_free_callback(struct kref *kref)
9410285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust{
9420285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust	struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
9430285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust
9440285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust	gss_free(gss_auth);
9450285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust}
9460285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust
9470285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebuststatic void
9481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_destroy(struct rpc_auth *auth)
9491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
9501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_auth *gss_auth;
9511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9528885cb367f86ce02bed3bf18192d74a53ac3b81fChuck Lever	dprintk("RPC:       destroying GSS authenticator %p flavor %d\n",
9538885cb367f86ce02bed3bf18192d74a53ac3b81fChuck Lever			auth, auth->au_flavor);
9541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9553ab9bb7243489f9db3abf3d05521ddfc6b184c0aTrond Myklebust	rpcauth_destroy_credcache(auth);
9563ab9bb7243489f9db3abf3d05521ddfc6b184c0aTrond Myklebust
9571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_auth = container_of(auth, struct gss_auth, rpc_auth);
9580285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust	kref_put(&gss_auth->kref, gss_free_callback);
9591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9610df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust/*
9620df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust * gss_destroying_context will cause the RPCSEC_GSS to send a NULL RPC call
9630df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust * to the server with the GSS control procedure field set to
9640df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
9650df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust * all RPCSEC_GSS state associated with that context.
9660df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust */
9670df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebuststatic int
9680df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebustgss_destroying_context(struct rpc_cred *cred)
9690df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust{
9700df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
9710df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
9720df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	struct rpc_task *task;
9730df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust
9740df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	if (gss_cred->gc_ctx == NULL ||
9756dcd3926b214a1fb081df18305921dedae269977Jeff Layton	    test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
9760df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust		return 0;
9770df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust
9780df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	gss_cred->gc_ctx->gc_proc = RPC_GSS_PROC_DESTROY;
9790df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	cred->cr_ops = &gss_nullops;
9800df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust
9810df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	/* Take a reference to ensure the cred will be destroyed either
9820df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	 * by the RPC call or by the put_rpccred() below */
9830df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	get_rpccred(cred);
9840df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust
985080a1f148df0615f7a610e4776dd8f3fb706f54fTrond Myklebust	task = rpc_call_null(gss_auth->client, cred, RPC_TASK_ASYNC|RPC_TASK_SOFT);
9860df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	if (!IS_ERR(task))
9870df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust		rpc_put_task(task);
9880df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust
9890df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	put_rpccred(cred);
9900df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	return 1;
9910df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust}
9920df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust
9930df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust/* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
9941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * to create a new cred or context, so they check that things have been
9951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * allocated before freeing them. */
9961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
9975d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebustgss_do_free_ctx(struct gss_cl_ctx *ctx)
9981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
999632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC:       %s\n", __func__);
10001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10010d8a374673c9bc62bc78a2d8fe64553a51542cb4Trond Myklebust	gss_delete_sec_context(&ctx->gc_gss_ctx);
10021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(ctx->gc_wire_ctx.data);
10031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(ctx);
10041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void
10075d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebustgss_free_ctx_callback(struct rcu_head *head)
10085d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust{
10095d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust	struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
10105d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust	gss_do_free_ctx(ctx);
10115d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust}
10125d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust
10135d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebuststatic void
10145d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebustgss_free_ctx(struct gss_cl_ctx *ctx)
10155d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust{
10165d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust	call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
10175d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust}
10185d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust
10195d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebuststatic void
102031be5bf15f3dafffce110eb1afadccbf2e3067b4Trond Myklebustgss_free_cred(struct gss_cred *gss_cred)
10211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1022632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC:       %s cred=%p\n", __func__, gss_cred);
102331be5bf15f3dafffce110eb1afadccbf2e3067b4Trond Myklebust	kfree(gss_cred);
102431be5bf15f3dafffce110eb1afadccbf2e3067b4Trond Myklebust}
10251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
102631be5bf15f3dafffce110eb1afadccbf2e3067b4Trond Myklebuststatic void
102731be5bf15f3dafffce110eb1afadccbf2e3067b4Trond Myklebustgss_free_cred_callback(struct rcu_head *head)
102831be5bf15f3dafffce110eb1afadccbf2e3067b4Trond Myklebust{
102931be5bf15f3dafffce110eb1afadccbf2e3067b4Trond Myklebust	struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
103031be5bf15f3dafffce110eb1afadccbf2e3067b4Trond Myklebust	gss_free_cred(gss_cred);
103131be5bf15f3dafffce110eb1afadccbf2e3067b4Trond Myklebust}
10321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
103331be5bf15f3dafffce110eb1afadccbf2e3067b4Trond Myklebuststatic void
10346dcd3926b214a1fb081df18305921dedae269977Jeff Laytongss_destroy_nullcred(struct rpc_cred *cred)
103531be5bf15f3dafffce110eb1afadccbf2e3067b4Trond Myklebust{
10365d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
10370285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
10385d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust	struct gss_cl_ctx *ctx = gss_cred->gc_ctx;
10395d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust
1040a9b3cd7f323b2e57593e7215362a7b02fc933e3aStephen Hemminger	RCU_INIT_POINTER(gss_cred->gc_ctx, NULL);
104131be5bf15f3dafffce110eb1afadccbf2e3067b4Trond Myklebust	call_rcu(&cred->cr_rcu, gss_free_cred_callback);
10425d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust	if (ctx)
10435d28dc82074f1e64b22c9424b161abc1f5d6bcdbTrond Myklebust		gss_put_ctx(ctx);
10440285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust	kref_put(&gss_auth->kref, gss_free_callback);
10451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10476dcd3926b214a1fb081df18305921dedae269977Jeff Laytonstatic void
10486dcd3926b214a1fb081df18305921dedae269977Jeff Laytongss_destroy_cred(struct rpc_cred *cred)
10496dcd3926b214a1fb081df18305921dedae269977Jeff Layton{
10506dcd3926b214a1fb081df18305921dedae269977Jeff Layton
10516dcd3926b214a1fb081df18305921dedae269977Jeff Layton	if (gss_destroying_context(cred))
10526dcd3926b214a1fb081df18305921dedae269977Jeff Layton		return;
10536dcd3926b214a1fb081df18305921dedae269977Jeff Layton	gss_destroy_nullcred(cred);
10546dcd3926b214a1fb081df18305921dedae269977Jeff Layton}
10556dcd3926b214a1fb081df18305921dedae269977Jeff Layton
10561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
10571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Lookup RPCSEC_GSS cred for the current process
10581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
10591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct rpc_cred *
10608a3177604b729ec3b80e43790ee978863ac7551bTrond Myklebustgss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
10611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
10628a3177604b729ec3b80e43790ee978863ac7551bTrond Myklebust	return rpcauth_lookup_credcache(auth, acred, flags);
10631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct rpc_cred *
10668a3177604b729ec3b80e43790ee978863ac7551bTrond Myklebustgss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
10671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
10681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
10691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cred	*cred = NULL;
10701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int err = -ENOMEM;
10711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1072632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC:       %s for uid %d, flavor %d\n",
1073cdba321e291f0fbf5abda4d88340292b858e3d4dEric W. Biederman		__func__, from_kuid(&init_user_ns, acred->uid),
1074cdba321e291f0fbf5abda4d88340292b858e3d4dEric W. Biederman		auth->au_flavor);
10751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10760f38b873aeaae698c3693748438547c8493165fbTrond Myklebust	if (!(cred = kzalloc(sizeof(*cred), GFP_NOFS)))
10771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out_err;
10781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10795fe4755e2526a2aa82b7ed8daeb3aed74a236925Trond Myklebust	rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
10801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
10811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Note: in order to force a call to call_refresh(), we deliberately
10821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
10831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
1084fc432dd90760a629c57026e57f65ff80a1a31d2fTrond Myklebust	cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
10851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cred->gc_service = gss_auth->service;
108668c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust	cred->gc_principal = NULL;
108768c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust	if (acred->machine_cred)
108868c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust		cred->gc_principal = acred->principal;
10890285ed1f12298e5304f0f2642e2cf31a5f302e61Trond Myklebust	kref_get(&gss_auth->kref);
10901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &cred->gc_base;
10911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout_err:
1093632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC:       %s failed with error %d\n", __func__, err);
10941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return ERR_PTR(err);
10951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int
1098fba3bad488a2eec2d76c067edb7a5ff92ef42431Trond Myklebustgss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
1099fba3bad488a2eec2d76c067edb7a5ff92ef42431Trond Myklebust{
1100fba3bad488a2eec2d76c067edb7a5ff92ef42431Trond Myklebust	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
1101fba3bad488a2eec2d76c067edb7a5ff92ef42431Trond Myklebust	struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
1102fba3bad488a2eec2d76c067edb7a5ff92ef42431Trond Myklebust	int err;
1103fba3bad488a2eec2d76c067edb7a5ff92ef42431Trond Myklebust
1104fba3bad488a2eec2d76c067edb7a5ff92ef42431Trond Myklebust	do {
1105fba3bad488a2eec2d76c067edb7a5ff92ef42431Trond Myklebust		err = gss_create_upcall(gss_auth, gss_cred);
1106fba3bad488a2eec2d76c067edb7a5ff92ef42431Trond Myklebust	} while (err == -EAGAIN);
1107fba3bad488a2eec2d76c067edb7a5ff92ef42431Trond Myklebust	return err;
1108fba3bad488a2eec2d76c067edb7a5ff92ef42431Trond Myklebust}
1109fba3bad488a2eec2d76c067edb7a5ff92ef42431Trond Myklebust
1110fba3bad488a2eec2d76c067edb7a5ff92ef42431Trond Myklebuststatic int
11118a3177604b729ec3b80e43790ee978863ac7551bTrond Myklebustgss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
11121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
11131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
11141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1115cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
11168a3177604b729ec3b80e43790ee978863ac7551bTrond Myklebust		goto out;
11171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Don't match with creds that have expired. */
1118cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	if (time_after(jiffies, gss_cred->gc_ctx->gc_expiry))
1119cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust		return 0;
1120cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags))
11211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
11228a3177604b729ec3b80e43790ee978863ac7551bTrond Myklebustout:
112368c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust	if (acred->principal != NULL) {
112468c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust		if (gss_cred->gc_principal == NULL)
112568c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust			return 0;
112668c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust		return strcmp(acred->principal, gss_cred->gc_principal) == 0;
112768c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust	}
112868c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust	if (gss_cred->gc_principal != NULL)
11297c67db3a8a98045744f06fcd6d8f476d9df0ba5cTrond Myklebust		return 0;
11300b4d51b02a2e941beec6f02a6c7a32c5a28c5b43Eric W. Biederman	return uid_eq(rc->cr_uid, acred->uid);
11311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
11321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
11341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds* Marshal credentials.
11351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds* Maybe we should keep a cached credential for performance reasons.
11361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*/
1137d8ed029d6000ba2e2908d9286409e4833c091b4cAlexey Dobriyanstatic __be32 *
1138d8ed029d6000ba2e2908d9286409e4833c091b4cAlexey Dobriyangss_marshal(struct rpc_task *task, __be32 *p)
11391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1140a17c2153d2e271b0cbacae9bed83b0eaa41db7e1Trond Myklebust	struct rpc_rqst *req = task->tk_rqstp;
1141a17c2153d2e271b0cbacae9bed83b0eaa41db7e1Trond Myklebust	struct rpc_cred *cred = req->rq_cred;
11421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
11431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds						 gc_base);
11441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cl_ctx	*ctx = gss_cred_get_ctx(cred);
1145d8ed029d6000ba2e2908d9286409e4833c091b4cAlexey Dobriyan	__be32		*cred_len;
11461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32             maj_stat = 0;
11471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct xdr_netobj mic;
11481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct kvec	iov;
11491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct xdr_buf	verf_buf;
11501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1151632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
11521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*p++ = htonl(RPC_AUTH_GSS);
11541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	cred_len = p++;
11551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock(&ctx->gc_seq_lock);
11571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	req->rq_seqno = ctx->gc_seq++;
11581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock(&ctx->gc_seq_lock);
11591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*p++ = htonl((u32) RPC_GSS_VERSION);
11611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*p++ = htonl((u32) ctx->gc_proc);
11621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*p++ = htonl((u32) req->rq_seqno);
11631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*p++ = htonl((u32) gss_cred->gc_service);
11641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
11651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*cred_len = htonl((p - (cred_len + 1)) << 2);
11661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* We compute the checksum for the verifier over the xdr-encoded bytes
11681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * starting with the xid and ending at the end of the credential: */
1169a4f0835c604f80f945ab3e72ffd00547145c4b2bTrond Myklebust	iov.iov_base = xprt_skip_transport_header(req->rq_xprt,
1170808012fbb23a52ec59352445d2076d175ad4ab26Chuck Lever					req->rq_snd_buf.head[0].iov_base);
11711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
11721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	xdr_buf_from_iov(&iov, &verf_buf);
11731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set verifier flavor*/
11751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*p++ = htonl(RPC_AUTH_GSS);
11761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mic.data = (u8 *)(p + 1);
117800fd6e14255fe7a249315746386d640bc4e9e758J. Bruce Fields	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
11791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
1180fc432dd90760a629c57026e57f65ff80a1a31d2fTrond Myklebust		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
11811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else if (maj_stat != 0) {
11821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
11831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out_put_ctx;
11841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
11851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p = xdr_encode_opaque(p, NULL, mic.len);
11861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_put_ctx(ctx);
11871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return p;
11881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout_put_ctx:
11891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_put_ctx(ctx);
11901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return NULL;
11911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
11921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1193cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebuststatic int gss_renew_cred(struct rpc_task *task)
1194cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust{
1195a17c2153d2e271b0cbacae9bed83b0eaa41db7e1Trond Myklebust	struct rpc_cred *oldcred = task->tk_rqstp->rq_cred;
1196cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	struct gss_cred *gss_cred = container_of(oldcred,
1197cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust						 struct gss_cred,
1198cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust						 gc_base);
1199cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	struct rpc_auth *auth = oldcred->cr_auth;
1200cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	struct auth_cred acred = {
1201cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust		.uid = oldcred->cr_uid,
120268c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust		.principal = gss_cred->gc_principal,
120368c97153fb7f2877f98aa6c29546381d9cad2fedTrond Myklebust		.machine_cred = (gss_cred->gc_principal != NULL ? 1 : 0),
1204cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	};
1205cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	struct rpc_cred *new;
1206cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust
1207cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	new = gss_lookup_cred(auth, &acred, RPCAUTH_LOOKUP_NEW);
1208cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	if (IS_ERR(new))
1209cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust		return PTR_ERR(new);
1210a17c2153d2e271b0cbacae9bed83b0eaa41db7e1Trond Myklebust	task->tk_rqstp->rq_cred = new;
1211cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	put_rpccred(oldcred);
1212cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	return 0;
1213cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust}
1214cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust
1215126e216a8730532dfb685205309275f87e3d133eTrond Myklebuststatic int gss_cred_is_negative_entry(struct rpc_cred *cred)
1216126e216a8730532dfb685205309275f87e3d133eTrond Myklebust{
1217126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	if (test_bit(RPCAUTH_CRED_NEGATIVE, &cred->cr_flags)) {
1218126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		unsigned long now = jiffies;
1219126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		unsigned long begin, expire;
1220126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		struct gss_cred *gss_cred;
1221126e216a8730532dfb685205309275f87e3d133eTrond Myklebust
1222126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		gss_cred = container_of(cred, struct gss_cred, gc_base);
1223126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		begin = gss_cred->gc_upcall_timestamp;
1224126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		expire = begin + gss_expired_cred_retry_delay * HZ;
1225126e216a8730532dfb685205309275f87e3d133eTrond Myklebust
1226126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		if (time_in_range_open(now, begin, expire))
1227126e216a8730532dfb685205309275f87e3d133eTrond Myklebust			return 1;
1228126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	}
1229126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	return 0;
1230126e216a8730532dfb685205309275f87e3d133eTrond Myklebust}
1231126e216a8730532dfb685205309275f87e3d133eTrond Myklebust
12321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
12331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds* Refresh credentials. XXX - finish
12341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*/
12351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int
12361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_refresh(struct rpc_task *task)
12371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1238a17c2153d2e271b0cbacae9bed83b0eaa41db7e1Trond Myklebust	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
1239cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	int ret = 0;
1240cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust
1241126e216a8730532dfb685205309275f87e3d133eTrond Myklebust	if (gss_cred_is_negative_entry(cred))
1242126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		return -EKEYEXPIRED;
1243126e216a8730532dfb685205309275f87e3d133eTrond Myklebust
1244cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
1245cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust			!test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags)) {
1246cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust		ret = gss_renew_cred(task);
1247cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust		if (ret < 0)
1248cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust			goto out;
1249a17c2153d2e271b0cbacae9bed83b0eaa41db7e1Trond Myklebust		cred = task->tk_rqstp->rq_cred;
1250cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	}
12511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1252cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
1253cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust		ret = gss_refresh_upcall(task);
1254cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebustout:
1255cd019f7517206a74d8cdb64d5c82b1f76be608ccTrond Myklebust	return ret;
12561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12580df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust/* Dummy refresh routine: used only when destroying the context */
12590df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebuststatic int
12600df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebustgss_refresh_null(struct rpc_task *task)
12610df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust{
12620df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	return -EACCES;
12630df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust}
12640df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust
1265d8ed029d6000ba2e2908d9286409e4833c091b4cAlexey Dobriyanstatic __be32 *
1266d8ed029d6000ba2e2908d9286409e4833c091b4cAlexey Dobriyangss_validate(struct rpc_task *task, __be32 *p)
12671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1268a17c2153d2e271b0cbacae9bed83b0eaa41db7e1Trond Myklebust	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
12691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1270d8ed029d6000ba2e2908d9286409e4833c091b4cAlexey Dobriyan	__be32		seq;
12711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct kvec	iov;
12721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct xdr_buf	verf_buf;
12731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct xdr_netobj mic;
12741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32		flav,len;
12751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32		maj_stat;
12761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1277632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
12781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	flav = ntohl(*p++);
12801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
1281cca5172a7ec10dfdb0b787cd8e9d5b0b8f179793YOSHIFUJI Hideaki		goto out_bad;
12821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (flav != RPC_AUTH_GSS)
12831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out_bad;
12841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	seq = htonl(task->tk_rqstp->rq_seqno);
12851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	iov.iov_base = &seq;
12861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	iov.iov_len = sizeof(seq);
12871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	xdr_buf_from_iov(&iov, &verf_buf);
12881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mic.data = (u8 *)p;
12891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mic.len = len;
12901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
129100fd6e14255fe7a249315746386d640bc4e9e758J. Bruce Fields	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
12921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1293fc432dd90760a629c57026e57f65ff80a1a31d2fTrond Myklebust		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
12940df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	if (maj_stat) {
1295632f0d0503accb8ab749a1165af99d344579c37bChuck Lever		dprintk("RPC: %5u %s: gss_verify_mic returned error 0x%08x\n",
1296632f0d0503accb8ab749a1165af99d344579c37bChuck Lever			task->tk_pid, __func__, maj_stat);
12971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out_bad;
12980df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	}
129924b2605becc10ca63c4c30808fa59a8abbf68727J. Bruce Fields	/* We leave it to unwrap to calculate au_rslack. For now we just
130024b2605becc10ca63c4c30808fa59a8abbf68727J. Bruce Fields	 * calculate the length of the verifier: */
13011be27f36601973815171db684c711d30557cf50cTrond Myklebust	cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
13021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_put_ctx(ctx);
1303632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC: %5u %s: gss_verify_mic succeeded.\n",
1304632f0d0503accb8ab749a1165af99d344579c37bChuck Lever			task->tk_pid, __func__);
13051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return p + XDR_QUADLEN(len);
13061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout_bad:
13071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_put_ctx(ctx);
1308632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC: %5u %s failed.\n", task->tk_pid, __func__);
13091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return NULL;
13101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13129f06c719f474be7003763284a990bed6377bb0d4Chuck Leverstatic void gss_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
13139f06c719f474be7003763284a990bed6377bb0d4Chuck Lever				__be32 *p, void *obj)
13149f06c719f474be7003763284a990bed6377bb0d4Chuck Lever{
13159f06c719f474be7003763284a990bed6377bb0d4Chuck Lever	struct xdr_stream xdr;
13169f06c719f474be7003763284a990bed6377bb0d4Chuck Lever
13179f06c719f474be7003763284a990bed6377bb0d4Chuck Lever	xdr_init_encode(&xdr, &rqstp->rq_snd_buf, p);
13189f06c719f474be7003763284a990bed6377bb0d4Chuck Lever	encode(rqstp, &xdr, obj);
13199f06c719f474be7003763284a990bed6377bb0d4Chuck Lever}
13209f06c719f474be7003763284a990bed6377bb0d4Chuck Lever
13211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline int
13221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
13239f06c719f474be7003763284a990bed6377bb0d4Chuck Lever		   kxdreproc_t encode, struct rpc_rqst *rqstp,
13249f06c719f474be7003763284a990bed6377bb0d4Chuck Lever		   __be32 *p, void *obj)
13251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
13261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct xdr_buf	*snd_buf = &rqstp->rq_snd_buf;
13271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct xdr_buf	integ_buf;
1328d8ed029d6000ba2e2908d9286409e4833c091b4cAlexey Dobriyan	__be32          *integ_len = NULL;
13291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct xdr_netobj mic;
1330d8ed029d6000ba2e2908d9286409e4833c091b4cAlexey Dobriyan	u32		offset;
1331d8ed029d6000ba2e2908d9286409e4833c091b4cAlexey Dobriyan	__be32		*q;
13321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct kvec	*iov;
13331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32             maj_stat = 0;
13341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int		status = -EIO;
13351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	integ_len = p++;
13371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
13381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*p++ = htonl(rqstp->rq_seqno);
13391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13409f06c719f474be7003763284a990bed6377bb0d4Chuck Lever	gss_wrap_req_encode(encode, rqstp, p, obj);
13411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (xdr_buf_subsegment(snd_buf, &integ_buf,
13431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				offset, snd_buf->len - offset))
13441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return status;
13451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*integ_len = htonl(integ_buf.len);
13461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* guess whether we're in the head or the tail: */
1348cca5172a7ec10dfdb0b787cd8e9d5b0b8f179793YOSHIFUJI Hideaki	if (snd_buf->page_len || snd_buf->tail[0].iov_len)
13491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		iov = snd_buf->tail;
13501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
13511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		iov = snd_buf->head;
13521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p = iov->iov_base + iov->iov_len;
13531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mic.data = (u8 *)(p + 1);
13541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
135500fd6e14255fe7a249315746386d640bc4e9e758J. Bruce Fields	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
13561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	status = -EIO; /* XXX? */
13571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1358fc432dd90760a629c57026e57f65ff80a1a31d2fTrond Myklebust		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
13591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (maj_stat)
13601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return status;
13611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	q = xdr_encode_opaque(p, NULL, mic.len);
13621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	offset = (u8 *)q - (u8 *)p;
13641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	iov->iov_len += offset;
13651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	snd_buf->len += offset;
13661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
13671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13692d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fieldsstatic void
13702d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fieldspriv_release_snd_buf(struct rpc_rqst *rqstp)
13712d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields{
13722d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	int i;
13732d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
13742d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	for (i=0; i < rqstp->rq_enc_pages_num; i++)
13752d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		__free_page(rqstp->rq_enc_pages[i]);
13762d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	kfree(rqstp->rq_enc_pages);
13772d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields}
13782d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
13792d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fieldsstatic int
13802d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fieldsalloc_enc_pages(struct rpc_rqst *rqstp)
13812d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields{
13822d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
13832d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	int first, last, i;
13842d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
13852d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	if (snd_buf->page_len == 0) {
13862d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		rqstp->rq_enc_pages_num = 0;
13872d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		return 0;
13882d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	}
13892d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
13902d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
13912d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_CACHE_SHIFT;
13922d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	rqstp->rq_enc_pages_num = last - first + 1 + 1;
13932d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	rqstp->rq_enc_pages
13942d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		= kmalloc(rqstp->rq_enc_pages_num * sizeof(struct page *),
13952d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields				GFP_NOFS);
13962d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	if (!rqstp->rq_enc_pages)
13972d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		goto out;
13982d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	for (i=0; i < rqstp->rq_enc_pages_num; i++) {
13992d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
14002d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		if (rqstp->rq_enc_pages[i] == NULL)
14012d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields			goto out_free;
14022d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	}
14032d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	rqstp->rq_release_snd_buf = priv_release_snd_buf;
14042d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	return 0;
14052d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fieldsout_free:
1406cdead7cf12896c0e50a8be2e52de52c364603095Trond Myklebust	rqstp->rq_enc_pages_num = i;
1407cdead7cf12896c0e50a8be2e52de52c364603095Trond Myklebust	priv_release_snd_buf(rqstp);
14082d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fieldsout:
14092d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	return -EAGAIN;
14102d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields}
14112d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
14122d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fieldsstatic inline int
14132d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fieldsgss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
14149f06c719f474be7003763284a990bed6377bb0d4Chuck Lever		  kxdreproc_t encode, struct rpc_rqst *rqstp,
14159f06c719f474be7003763284a990bed6377bb0d4Chuck Lever		  __be32 *p, void *obj)
14162d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields{
14172d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	struct xdr_buf	*snd_buf = &rqstp->rq_snd_buf;
14182d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	u32		offset;
14192d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	u32             maj_stat;
14202d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	int		status;
1421d8ed029d6000ba2e2908d9286409e4833c091b4cAlexey Dobriyan	__be32		*opaque_len;
14222d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	struct page	**inpages;
14232d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	int		first;
14242d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	int		pad;
14252d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	struct kvec	*iov;
14262d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	char		*tmp;
14272d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
14282d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	opaque_len = p++;
14292d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
14302d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	*p++ = htonl(rqstp->rq_seqno);
14312d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
14329f06c719f474be7003763284a990bed6377bb0d4Chuck Lever	gss_wrap_req_encode(encode, rqstp, p, obj);
14332d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
14342d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	status = alloc_enc_pages(rqstp);
14352d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	if (status)
14362d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		return status;
14372d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
14382d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	inpages = snd_buf->pages + first;
14392d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	snd_buf->pages = rqstp->rq_enc_pages;
14402d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	snd_buf->page_base -= first << PAGE_CACHE_SHIFT;
14417561042fb7870be0b4ee57efddce68bda8968abfKevin Coffman	/*
14427561042fb7870be0b4ee57efddce68bda8968abfKevin Coffman	 * Give the tail its own page, in case we need extra space in the
14437561042fb7870be0b4ee57efddce68bda8968abfKevin Coffman	 * head when wrapping:
14447561042fb7870be0b4ee57efddce68bda8968abfKevin Coffman	 *
14457561042fb7870be0b4ee57efddce68bda8968abfKevin Coffman	 * call_allocate() allocates twice the slack space required
14467561042fb7870be0b4ee57efddce68bda8968abfKevin Coffman	 * by the authentication flavor to rq_callsize.
14477561042fb7870be0b4ee57efddce68bda8968abfKevin Coffman	 * For GSS, slack is GSS_CRED_SLACK.
14487561042fb7870be0b4ee57efddce68bda8968abfKevin Coffman	 */
14492d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
14502d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
14512d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
14522d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		snd_buf->tail[0].iov_base = tmp;
14532d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	}
145400fd6e14255fe7a249315746386d640bc4e9e758J. Bruce Fields	maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
14557561042fb7870be0b4ee57efddce68bda8968abfKevin Coffman	/* slack space should prevent this ever happening: */
14562d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	BUG_ON(snd_buf->len > snd_buf->buflen);
1457cca5172a7ec10dfdb0b787cd8e9d5b0b8f179793YOSHIFUJI Hideaki	status = -EIO;
14582d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	/* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
14592d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	 * done anyway, so it's safe to put the request on the wire: */
14602d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1461fc432dd90760a629c57026e57f65ff80a1a31d2fTrond Myklebust		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
14622d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	else if (maj_stat)
14632d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		return status;
14642d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
14652d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	*opaque_len = htonl(snd_buf->len - offset);
14662d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	/* guess whether we're in the head or the tail: */
14672d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	if (snd_buf->page_len || snd_buf->tail[0].iov_len)
14682d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		iov = snd_buf->tail;
14692d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	else
14702d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		iov = snd_buf->head;
14712d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	p = iov->iov_base + iov->iov_len;
14722d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	pad = 3 - ((snd_buf->len - offset - 1) & 3);
14732d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	memset(p, 0, pad);
14742d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	iov->iov_len += pad;
14752d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	snd_buf->len += pad;
14762d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
14772d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	return 0;
14782d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields}
14792d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
14801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int
14811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_wrap_req(struct rpc_task *task,
14829f06c719f474be7003763284a990bed6377bb0d4Chuck Lever	     kxdreproc_t encode, void *rqstp, __be32 *p, void *obj)
14831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1484a17c2153d2e271b0cbacae9bed83b0eaa41db7e1Trond Myklebust	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
14851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
14861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			gc_base);
14871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
14881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int             status = -EIO;
14891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1490632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
14911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
14921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* The spec seems a little ambiguous here, but I think that not
14931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * wrapping context destruction requests makes the most sense.
14941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
14959f06c719f474be7003763284a990bed6377bb0d4Chuck Lever		gss_wrap_req_encode(encode, rqstp, p, obj);
14969f06c719f474be7003763284a990bed6377bb0d4Chuck Lever		status = 0;
14971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
14981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
14991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (gss_cred->gc_service) {
150089f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches	case RPC_GSS_SVC_NONE:
150189f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches		gss_wrap_req_encode(encode, rqstp, p, obj);
150289f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches		status = 0;
150389f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches		break;
150489f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches	case RPC_GSS_SVC_INTEGRITY:
150589f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches		status = gss_wrap_req_integ(cred, ctx, encode, rqstp, p, obj);
150689f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches		break;
150789f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches	case RPC_GSS_SVC_PRIVACY:
150889f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches		status = gss_wrap_req_priv(cred, ctx, encode, rqstp, p, obj);
150989f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches		break;
15101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout:
15121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_put_ctx(ctx);
1513632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC: %5u %s returning %d\n", task->tk_pid, __func__, status);
15141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return status;
15151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline int
15181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1519d8ed029d6000ba2e2908d9286409e4833c091b4cAlexey Dobriyan		struct rpc_rqst *rqstp, __be32 **p)
15201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
15211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct xdr_buf	*rcv_buf = &rqstp->rq_rcv_buf;
15221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct xdr_buf integ_buf;
15231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct xdr_netobj mic;
15241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 data_offset, mic_offset;
15251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 integ_len;
15261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 maj_stat;
15271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int status = -EIO;
15281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	integ_len = ntohl(*(*p)++);
15301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (integ_len & 3)
15311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return status;
15321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
15331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mic_offset = integ_len + data_offset;
15341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (mic_offset > rcv_buf->len)
15351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return status;
15361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ntohl(*(*p)++) != rqstp->rq_seqno)
15371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return status;
15381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
15401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				mic_offset - data_offset))
15411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return status;
15421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
15441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return status;
15451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
154600fd6e14255fe7a249315746386d640bc4e9e758J. Bruce Fields	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
15471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1548fc432dd90760a629c57026e57f65ff80a1a31d2fTrond Myklebust		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
15491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (maj_stat != GSS_S_COMPLETE)
15501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return status;
15511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
15521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15542d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fieldsstatic inline int
15552d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fieldsgss_unwrap_resp_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1556d8ed029d6000ba2e2908d9286409e4833c091b4cAlexey Dobriyan		struct rpc_rqst *rqstp, __be32 **p)
15572d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields{
15582d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	struct xdr_buf  *rcv_buf = &rqstp->rq_rcv_buf;
15592d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	u32 offset;
15602d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	u32 opaque_len;
15612d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	u32 maj_stat;
15622d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	int status = -EIO;
15632d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
15642d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	opaque_len = ntohl(*(*p)++);
15652d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
15662d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	if (offset + opaque_len > rcv_buf->len)
15672d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		return status;
15682d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	/* remove padding: */
15692d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	rcv_buf->len = offset + opaque_len;
15702d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
157100fd6e14255fe7a249315746386d640bc4e9e758J. Bruce Fields	maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
15722d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1573fc432dd90760a629c57026e57f65ff80a1a31d2fTrond Myklebust		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
15742d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	if (maj_stat != GSS_S_COMPLETE)
15752d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		return status;
15762d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	if (ntohl(*(*p)++) != rqstp->rq_seqno)
15772d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields		return status;
15782d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
15792d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	return 0;
15802d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields}
15812d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
1582bf2695516db982e90a22fc94f93491b481796bb1Chuck Leverstatic int
1583bf2695516db982e90a22fc94f93491b481796bb1Chuck Levergss_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
1584bf2695516db982e90a22fc94f93491b481796bb1Chuck Lever		      __be32 *p, void *obj)
1585bf2695516db982e90a22fc94f93491b481796bb1Chuck Lever{
1586bf2695516db982e90a22fc94f93491b481796bb1Chuck Lever	struct xdr_stream xdr;
1587bf2695516db982e90a22fc94f93491b481796bb1Chuck Lever
1588bf2695516db982e90a22fc94f93491b481796bb1Chuck Lever	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
1589bf2695516db982e90a22fc94f93491b481796bb1Chuck Lever	return decode(rqstp, &xdr, obj);
1590bf2695516db982e90a22fc94f93491b481796bb1Chuck Lever}
15912d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields
15921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int
15931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsgss_unwrap_resp(struct rpc_task *task,
1594bf2695516db982e90a22fc94f93491b481796bb1Chuck Lever		kxdrdproc_t decode, void *rqstp, __be32 *p, void *obj)
15951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1596a17c2153d2e271b0cbacae9bed83b0eaa41db7e1Trond Myklebust	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
15971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
15981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			gc_base);
15991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1600d8ed029d6000ba2e2908d9286409e4833c091b4cAlexey Dobriyan	__be32		*savedp = p;
16012d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	struct kvec	*head = ((struct rpc_rqst *)rqstp)->rq_rcv_buf.head;
16022d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields	int		savedlen = head->iov_len;
16031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int             status = -EIO;
16041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ctx->gc_proc != RPC_GSS_PROC_DATA)
16061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out_decode;
16071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (gss_cred->gc_service) {
160889f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches	case RPC_GSS_SVC_NONE:
160989f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches		break;
161089f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches	case RPC_GSS_SVC_INTEGRITY:
161189f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches		status = gss_unwrap_resp_integ(cred, ctx, rqstp, &p);
161289f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches		if (status)
161389f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches			goto out;
161489f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches		break;
161589f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches	case RPC_GSS_SVC_PRIVACY:
161689f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches		status = gss_unwrap_resp_priv(cred, ctx, rqstp, &p);
161789f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches		if (status)
161889f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches			goto out;
161989f0e4feafb64643b0f0aba9d89984362bac9739Joe Perches		break;
16201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
162124b2605becc10ca63c4c30808fa59a8abbf68727J. Bruce Fields	/* take into account extra slack for integrity and privacy cases: */
16221be27f36601973815171db684c711d30557cf50cTrond Myklebust	cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + (p - savedp)
16232d2da60c63b67174add32f06e8d54c3a0c5cd9cfJ. Bruce Fields						+ (savedlen - head->iov_len);
16241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout_decode:
1625bf2695516db982e90a22fc94f93491b481796bb1Chuck Lever	status = gss_unwrap_req_decode(decode, rqstp, p, obj);
16261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout:
16271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_put_ctx(ctx);
1628632f0d0503accb8ab749a1165af99d344579c37bChuck Lever	dprintk("RPC: %5u %s returning %d\n",
1629632f0d0503accb8ab749a1165af99d344579c37bChuck Lever		task->tk_pid, __func__, status);
16301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return status;
16311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1632cca5172a7ec10dfdb0b787cd8e9d5b0b8f179793YOSHIFUJI Hideaki
1633f1c0a8615090359d57e096157feb9f900cbb233cTrond Myklebuststatic const struct rpc_authops authgss_ops = {
16341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.owner		= THIS_MODULE,
16351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.au_flavor	= RPC_AUTH_GSS,
16361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.au_name	= "RPCSEC_GSS",
16371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.create		= gss_create,
16381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.destroy	= gss_destroy,
16391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.lookup_cred	= gss_lookup_cred,
164080df9d202255071c8ec610a6a3fdca5cac69f7bdStanislav Kinsbursky	.crcreate	= gss_create_cred,
164180df9d202255071c8ec610a6a3fdca5cac69f7bdStanislav Kinsbursky	.pipes_create	= gss_pipes_dentries_create,
164280df9d202255071c8ec610a6a3fdca5cac69f7bdStanislav Kinsbursky	.pipes_destroy	= gss_pipes_dentries_destroy,
16436a1a1e34dc55f17e7bd260809207442dbb7a0296Chuck Lever	.list_pseudoflavors = gss_mech_list_pseudoflavors,
16441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
16451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1646f1c0a8615090359d57e096157feb9f900cbb233cTrond Myklebuststatic const struct rpc_credops gss_credops = {
16471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.cr_name	= "AUTH_GSS",
16481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.crdestroy	= gss_destroy_cred,
1649fba3bad488a2eec2d76c067edb7a5ff92ef42431Trond Myklebust	.cr_init	= gss_cred_init,
16505c691044ecbca04dd558fca4c754121689fe1b34Trond Myklebust	.crbind		= rpcauth_generic_bind_cred,
16511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.crmatch	= gss_match,
16521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.crmarshal	= gss_marshal,
16531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.crrefresh	= gss_refresh,
16541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.crvalidate	= gss_validate,
16551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.crwrap_req	= gss_wrap_req,
16561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.crunwrap_resp	= gss_unwrap_resp,
16571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
16581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16590df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebuststatic const struct rpc_credops gss_nullops = {
16600df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	.cr_name	= "AUTH_GSS",
16616dcd3926b214a1fb081df18305921dedae269977Jeff Layton	.crdestroy	= gss_destroy_nullcred,
16625c691044ecbca04dd558fca4c754121689fe1b34Trond Myklebust	.crbind		= rpcauth_generic_bind_cred,
16630df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	.crmatch	= gss_match,
16640df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	.crmarshal	= gss_marshal,
16650df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	.crrefresh	= gss_refresh_null,
16660df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	.crvalidate	= gss_validate,
16670df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	.crwrap_req	= gss_wrap_req,
16680df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust	.crunwrap_resp	= gss_unwrap_resp,
16690df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust};
16700df7fb74fbb709591301871a38aac7735a1d6583Trond Myklebust
1671b693ba4a338da15db1db4b5ebaa36e4ab9781c82Trond Myklebuststatic const struct rpc_pipe_ops gss_upcall_ops_v0 = {
1672c1225158a8dad9e9d5eee8a17dbbd9c7cda05ab9Peng Tao	.upcall		= rpc_pipe_generic_upcall,
167334769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	.downcall	= gss_pipe_downcall,
167434769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	.destroy_msg	= gss_pipe_destroy_msg,
167534769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	.open_pipe	= gss_pipe_open_v0,
167634769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	.release_pipe	= gss_pipe_release,
167734769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\};
167834769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\
1679b693ba4a338da15db1db4b5ebaa36e4ab9781c82Trond Myklebuststatic const struct rpc_pipe_ops gss_upcall_ops_v1 = {
1680c1225158a8dad9e9d5eee8a17dbbd9c7cda05ab9Peng Tao	.upcall		= rpc_pipe_generic_upcall,
16811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.downcall	= gss_pipe_downcall,
16821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.destroy_msg	= gss_pipe_destroy_msg,
168334769fc488b463cb753fc632f8f5ba56c918b7cb\"J. Bruce Fields\	.open_pipe	= gss_pipe_open_v1,
16841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.release_pipe	= gss_pipe_release,
16851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
16861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1687a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsburskystatic __net_init int rpcsec_gss_init_net(struct net *net)
1688a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky{
1689a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky	return gss_svc_init_net(net);
1690a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky}
1691a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky
1692a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsburskystatic __net_exit void rpcsec_gss_exit_net(struct net *net)
1693a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky{
1694a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky	gss_svc_shutdown_net(net);
1695a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky}
1696a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky
1697a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsburskystatic struct pernet_operations rpcsec_gss_net_ops = {
1698a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky	.init = rpcsec_gss_init_net,
1699a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky	.exit = rpcsec_gss_exit_net,
1700a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky};
1701a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky
17021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
17031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Initialize RPCSEC_GSS module
17041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
17051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int __init init_rpcsec_gss(void)
17061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
17071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int err = 0;
17081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = rpcauth_register(&authgss_ops);
17101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (err)
17111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
17121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = gss_svc_init();
17131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (err)
17141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out_unregister;
1715a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky	err = register_pernet_subsys(&rpcsec_gss_net_ops);
1716a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky	if (err)
1717a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky		goto out_svc_exit;
171879a3f20b641f9f93787ada49d1d7cfa98ee5a11e\"J. Bruce Fields\	rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version");
17191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
1720a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsburskyout_svc_exit:
1721a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky	gss_svc_shutdown();
17221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout_unregister:
17231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rpcauth_unregister(&authgss_ops);
17241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout:
17251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return err;
17261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
17271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void __exit exit_rpcsec_gss(void)
17291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1730a1db410d0bbadc49943f0fcddb21702ceb429396Stanislav Kinsbursky	unregister_pernet_subsys(&rpcsec_gss_net_ops);
17311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	gss_svc_shutdown();
17321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rpcauth_unregister(&authgss_ops);
1733bf12691d846b48fa6e3289810a822687457036cfJesper Dangaard Brouer	rcu_barrier(); /* Wait for completion of call_rcu()'s */
17341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
17351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
1737126e216a8730532dfb685205309275f87e3d133eTrond Myklebustmodule_param_named(expired_cred_retry_delay,
1738126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		   gss_expired_cred_retry_delay,
1739126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		   uint, 0644);
1740126e216a8730532dfb685205309275f87e3d133eTrond MyklebustMODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until "
1741126e216a8730532dfb685205309275f87e3d133eTrond Myklebust		"the RPC engine retries an expired credential");
1742126e216a8730532dfb685205309275f87e3d133eTrond Myklebust
17431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_init(init_rpcsec_gss)
17441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_exit(exit_rpcsec_gss)
1745