xref: /openbmc/linux/net/sunrpc/auth_gss/auth_gss.c (revision c2190661)
11da177e4SLinus Torvalds /*
2f30c2269SUwe Zeisberger  * linux/net/sunrpc/auth_gss/auth_gss.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * RPCSEC_GSS client authentication.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  Copyright (c) 2000 The Regents of the University of Michigan.
71da177e4SLinus Torvalds  *  All rights reserved.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  Dug Song       <dugsong@monkey.org>
101da177e4SLinus Torvalds  *  Andy Adamson   <andros@umich.edu>
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *  Redistribution and use in source and binary forms, with or without
131da177e4SLinus Torvalds  *  modification, are permitted provided that the following conditions
141da177e4SLinus Torvalds  *  are met:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *  1. Redistributions of source code must retain the above copyright
171da177e4SLinus Torvalds  *     notice, this list of conditions and the following disclaimer.
181da177e4SLinus Torvalds  *  2. Redistributions in binary form must reproduce the above copyright
191da177e4SLinus Torvalds  *     notice, this list of conditions and the following disclaimer in the
201da177e4SLinus Torvalds  *     documentation and/or other materials provided with the distribution.
211da177e4SLinus Torvalds  *  3. Neither the name of the University nor the names of its
221da177e4SLinus Torvalds  *     contributors may be used to endorse or promote products derived
231da177e4SLinus Torvalds  *     from this software without specific prior written permission.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
261da177e4SLinus Torvalds  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
271da177e4SLinus Torvalds  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
281da177e4SLinus Torvalds  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291da177e4SLinus Torvalds  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
301da177e4SLinus Torvalds  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
311da177e4SLinus Torvalds  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
321da177e4SLinus Torvalds  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
331da177e4SLinus Torvalds  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
341da177e4SLinus Torvalds  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
351da177e4SLinus Torvalds  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <linux/module.h>
401da177e4SLinus Torvalds #include <linux/init.h>
411da177e4SLinus Torvalds #include <linux/types.h>
421da177e4SLinus Torvalds #include <linux/slab.h>
431da177e4SLinus Torvalds #include <linux/sched.h>
442d2da60cSJ. Bruce Fields #include <linux/pagemap.h>
451da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
461da177e4SLinus Torvalds #include <linux/sunrpc/auth.h>
471da177e4SLinus Torvalds #include <linux/sunrpc/auth_gss.h>
481da177e4SLinus Torvalds #include <linux/sunrpc/svcauth_gss.h>
491da177e4SLinus Torvalds #include <linux/sunrpc/gss_err.h>
501da177e4SLinus Torvalds #include <linux/workqueue.h>
511da177e4SLinus Torvalds #include <linux/sunrpc/rpc_pipe_fs.h>
521da177e4SLinus Torvalds #include <linux/sunrpc/gss_api.h>
531da177e4SLinus Torvalds #include <asm/uaccess.h>
541da177e4SLinus Torvalds 
55abfdbd53STrond Myklebust #include "../netns.h"
56abfdbd53STrond Myklebust 
57f1c0a861STrond Myklebust static const struct rpc_authops authgss_ops;
581da177e4SLinus Torvalds 
59f1c0a861STrond Myklebust static const struct rpc_credops gss_credops;
600df7fb74STrond Myklebust static const struct rpc_credops gss_nullops;
611da177e4SLinus Torvalds 
62126e216aSTrond Myklebust #define GSS_RETRY_EXPIRED 5
63126e216aSTrond Myklebust static unsigned int gss_expired_cred_retry_delay = GSS_RETRY_EXPIRED;
64126e216aSTrond Myklebust 
651da177e4SLinus Torvalds #ifdef RPC_DEBUG
661da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_AUTH
671da177e4SLinus Torvalds #endif
681da177e4SLinus Torvalds 
69725f2865SKevin Coffman #define GSS_CRED_SLACK		(RPC_MAX_AUTH_SIZE * 2)
701da177e4SLinus Torvalds /* length of a krb5 verifier (48), plus data added before arguments when
711da177e4SLinus Torvalds  * using integrity (two 4-byte integers): */
72adeb8133SOlga Kornievskaia #define GSS_VERF_SLACK		100
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds struct gss_auth {
750285ed1fSTrond Myklebust 	struct kref kref;
761da177e4SLinus Torvalds 	struct rpc_auth rpc_auth;
771da177e4SLinus Torvalds 	struct gss_api_mech *mech;
781da177e4SLinus Torvalds 	enum rpc_gss_svc service;
791da177e4SLinus Torvalds 	struct rpc_clnt *client;
8034769fc4S\"J. Bruce Fields\ 	/*
8134769fc4S\"J. Bruce Fields\ 	 * There are two upcall pipes; dentry[1], named "gssd", is used
8234769fc4S\"J. Bruce Fields\ 	 * for the new text-based upcall; dentry[0] is named after the
8334769fc4S\"J. Bruce Fields\ 	 * mechanism (for example, "krb5") and exists for
8434769fc4S\"J. Bruce Fields\ 	 * backwards-compatibility with older gssd's.
8534769fc4S\"J. Bruce Fields\ 	 */
86c239d83bSStanislav Kinsbursky 	struct rpc_pipe *pipe[2];
87bd4a3eb1STrond Myklebust 	const char *target_name;
881da177e4SLinus Torvalds };
891da177e4SLinus Torvalds 
9079a3f20bS\"J. Bruce Fields\ /* pipe_version >= 0 if and only if someone has a pipe open. */
9179a3f20bS\"J. Bruce Fields\ static DEFINE_SPINLOCK(pipe_version_lock);
9279a3f20bS\"J. Bruce Fields\ static struct rpc_wait_queue pipe_version_rpc_waitqueue;
9379a3f20bS\"J. Bruce Fields\ static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue);
94cf81939dS\"J. Bruce Fields\ 
955d28dc82STrond Myklebust static void gss_free_ctx(struct gss_cl_ctx *);
96b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v0;
97b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v1;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds static inline struct gss_cl_ctx *
1001da177e4SLinus Torvalds gss_get_ctx(struct gss_cl_ctx *ctx)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	atomic_inc(&ctx->count);
1031da177e4SLinus Torvalds 	return ctx;
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds static inline void
1071da177e4SLinus Torvalds gss_put_ctx(struct gss_cl_ctx *ctx)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds 	if (atomic_dec_and_test(&ctx->count))
1105d28dc82STrond Myklebust 		gss_free_ctx(ctx);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1135d28dc82STrond Myklebust /* gss_cred_set_ctx:
1145d28dc82STrond Myklebust  * called by gss_upcall_callback and gss_create_upcall in order
1155d28dc82STrond Myklebust  * to set the gss context. The actual exchange of an old context
1169beae467SStanislav Kinsbursky  * and a new one is protected by the pipe->lock.
1175d28dc82STrond Myklebust  */
1181da177e4SLinus Torvalds static void
1191da177e4SLinus Torvalds gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1225d28dc82STrond Myklebust 
123cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
124cd019f75STrond Myklebust 		return;
1257b6962b0STrond Myklebust 	gss_get_ctx(ctx);
126cf778b00SEric Dumazet 	rcu_assign_pointer(gss_cred->gc_ctx, ctx);
127fc432dd9STrond Myklebust 	set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
128cd019f75STrond Myklebust 	smp_mb__before_clear_bit();
129fc432dd9STrond Myklebust 	clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds static const void *
1331da177e4SLinus Torvalds simple_get_bytes(const void *p, const void *end, void *res, size_t len)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	const void *q = (const void *)((const char *)p + len);
1361da177e4SLinus Torvalds 	if (unlikely(q > end || q < p))
1371da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
1381da177e4SLinus Torvalds 	memcpy(res, p, len);
1391da177e4SLinus Torvalds 	return q;
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds static inline const void *
1431da177e4SLinus Torvalds simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	const void *q;
1461da177e4SLinus Torvalds 	unsigned int len;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &len, sizeof(len));
1491da177e4SLinus Torvalds 	if (IS_ERR(p))
1501da177e4SLinus Torvalds 		return p;
1511da177e4SLinus Torvalds 	q = (const void *)((const char *)p + len);
1521da177e4SLinus Torvalds 	if (unlikely(q > end || q < p))
1531da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
1540f38b873STrond Myklebust 	dest->data = kmemdup(p, len, GFP_NOFS);
1551da177e4SLinus Torvalds 	if (unlikely(dest->data == NULL))
1561da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
1571da177e4SLinus Torvalds 	dest->len = len;
1581da177e4SLinus Torvalds 	return q;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds static struct gss_cl_ctx *
1621da177e4SLinus Torvalds gss_cred_get_ctx(struct rpc_cred *cred)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1651da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = NULL;
1661da177e4SLinus Torvalds 
1675d28dc82STrond Myklebust 	rcu_read_lock();
1681da177e4SLinus Torvalds 	if (gss_cred->gc_ctx)
1691da177e4SLinus Torvalds 		ctx = gss_get_ctx(gss_cred->gc_ctx);
1705d28dc82STrond Myklebust 	rcu_read_unlock();
1711da177e4SLinus Torvalds 	return ctx;
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds static struct gss_cl_ctx *
1751da177e4SLinus Torvalds gss_alloc_context(void)
1761da177e4SLinus Torvalds {
1771da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
1781da177e4SLinus Torvalds 
1790f38b873STrond Myklebust 	ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
1801da177e4SLinus Torvalds 	if (ctx != NULL) {
1811da177e4SLinus Torvalds 		ctx->gc_proc = RPC_GSS_PROC_DATA;
1821da177e4SLinus Torvalds 		ctx->gc_seq = 1;	/* NetApp 6.4R1 doesn't accept seq. no. 0 */
1831da177e4SLinus Torvalds 		spin_lock_init(&ctx->gc_seq_lock);
1841da177e4SLinus Torvalds 		atomic_set(&ctx->count,1);
1851da177e4SLinus Torvalds 	}
1861da177e4SLinus Torvalds 	return ctx;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds #define GSSD_MIN_TIMEOUT (60 * 60)
1901da177e4SLinus Torvalds static const void *
1911da177e4SLinus Torvalds gss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	const void *q;
1941da177e4SLinus Torvalds 	unsigned int seclen;
1951da177e4SLinus Torvalds 	unsigned int timeout;
196620038f6SAndy Adamson 	unsigned long now = jiffies;
1971da177e4SLinus Torvalds 	u32 window_size;
1981da177e4SLinus Torvalds 	int ret;
1991da177e4SLinus Torvalds 
200620038f6SAndy Adamson 	/* First unsigned int gives the remaining lifetime in seconds of the
201620038f6SAndy Adamson 	 * credential - e.g. the remaining TGT lifetime for Kerberos or
202620038f6SAndy Adamson 	 * the -t value passed to GSSD.
203620038f6SAndy Adamson 	 */
2041da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
2051da177e4SLinus Torvalds 	if (IS_ERR(p))
2061da177e4SLinus Torvalds 		goto err;
2071da177e4SLinus Torvalds 	if (timeout == 0)
2081da177e4SLinus Torvalds 		timeout = GSSD_MIN_TIMEOUT;
209620038f6SAndy Adamson 	ctx->gc_expiry = now + ((unsigned long)timeout * HZ);
210620038f6SAndy Adamson 	/* Sequence number window. Determines the maximum number of
211620038f6SAndy Adamson 	 * simultaneous requests
212620038f6SAndy Adamson 	 */
2131da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
2141da177e4SLinus Torvalds 	if (IS_ERR(p))
2151da177e4SLinus Torvalds 		goto err;
2161da177e4SLinus Torvalds 	ctx->gc_win = window_size;
2171da177e4SLinus Torvalds 	/* gssd signals an error by passing ctx->gc_win = 0: */
2181da177e4SLinus Torvalds 	if (ctx->gc_win == 0) {
219dc5ddce9SJeff Layton 		/*
220dc5ddce9SJeff Layton 		 * in which case, p points to an error code. Anything other
221dc5ddce9SJeff Layton 		 * than -EKEYEXPIRED gets converted to -EACCES.
222dc5ddce9SJeff Layton 		 */
223dc5ddce9SJeff Layton 		p = simple_get_bytes(p, end, &ret, sizeof(ret));
224dc5ddce9SJeff Layton 		if (!IS_ERR(p))
225dc5ddce9SJeff Layton 			p = (ret == -EKEYEXPIRED) ? ERR_PTR(-EKEYEXPIRED) :
226dc5ddce9SJeff Layton 						    ERR_PTR(-EACCES);
2271da177e4SLinus Torvalds 		goto err;
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 	/* copy the opaque wire context */
2301da177e4SLinus Torvalds 	p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
2311da177e4SLinus Torvalds 	if (IS_ERR(p))
2321da177e4SLinus Torvalds 		goto err;
2331da177e4SLinus Torvalds 	/* import the opaque security context */
2341da177e4SLinus Torvalds 	p  = simple_get_bytes(p, end, &seclen, sizeof(seclen));
2351da177e4SLinus Torvalds 	if (IS_ERR(p))
2361da177e4SLinus Torvalds 		goto err;
2371da177e4SLinus Torvalds 	q = (const void *)((const char *)p + seclen);
2381da177e4SLinus Torvalds 	if (unlikely(q > end || q < p)) {
2391da177e4SLinus Torvalds 		p = ERR_PTR(-EFAULT);
2401da177e4SLinus Torvalds 		goto err;
2411da177e4SLinus Torvalds 	}
242400f26b5SSimo Sorce 	ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx, NULL, GFP_NOFS);
2431da177e4SLinus Torvalds 	if (ret < 0) {
2441da177e4SLinus Torvalds 		p = ERR_PTR(ret);
2451da177e4SLinus Torvalds 		goto err;
2461da177e4SLinus Torvalds 	}
247620038f6SAndy Adamson 	dprintk("RPC:       %s Success. gc_expiry %lu now %lu timeout %u\n",
248620038f6SAndy Adamson 		__func__, ctx->gc_expiry, now, timeout);
2491da177e4SLinus Torvalds 	return q;
2501da177e4SLinus Torvalds err:
251173db309SJeff Layton 	dprintk("RPC:       %s returns error %ld\n", __func__, -PTR_ERR(p));
2521da177e4SLinus Torvalds 	return p;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
25534769fc4S\"J. Bruce Fields\ #define UPCALL_BUF_LEN 128
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds struct gss_upcall_msg {
2581da177e4SLinus Torvalds 	atomic_t count;
2597eaf040bSEric W. Biederman 	kuid_t	uid;
2601da177e4SLinus Torvalds 	struct rpc_pipe_msg msg;
2611da177e4SLinus Torvalds 	struct list_head list;
2621da177e4SLinus Torvalds 	struct gss_auth *auth;
2639beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
2641da177e4SLinus Torvalds 	struct rpc_wait_queue rpc_waitqueue;
2651da177e4SLinus Torvalds 	wait_queue_head_t waitqueue;
2661da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
26734769fc4S\"J. Bruce Fields\ 	char databuf[UPCALL_BUF_LEN];
2681da177e4SLinus Torvalds };
2691da177e4SLinus Torvalds 
2702aed8b47STrond Myklebust static int get_pipe_version(struct net *net)
27179a3f20bS\"J. Bruce Fields\ {
2722aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
27379a3f20bS\"J. Bruce Fields\ 	int ret;
27479a3f20bS\"J. Bruce Fields\ 
27579a3f20bS\"J. Bruce Fields\ 	spin_lock(&pipe_version_lock);
2762aed8b47STrond Myklebust 	if (sn->pipe_version >= 0) {
2772aed8b47STrond Myklebust 		atomic_inc(&sn->pipe_users);
2782aed8b47STrond Myklebust 		ret = sn->pipe_version;
27979a3f20bS\"J. Bruce Fields\ 	} else
28079a3f20bS\"J. Bruce Fields\ 		ret = -EAGAIN;
28179a3f20bS\"J. Bruce Fields\ 	spin_unlock(&pipe_version_lock);
28279a3f20bS\"J. Bruce Fields\ 	return ret;
28379a3f20bS\"J. Bruce Fields\ }
28479a3f20bS\"J. Bruce Fields\ 
2852aed8b47STrond Myklebust static void put_pipe_version(struct net *net)
28679a3f20bS\"J. Bruce Fields\ {
2872aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2882aed8b47STrond Myklebust 
2892aed8b47STrond Myklebust 	if (atomic_dec_and_lock(&sn->pipe_users, &pipe_version_lock)) {
2902aed8b47STrond Myklebust 		sn->pipe_version = -1;
29179a3f20bS\"J. Bruce Fields\ 		spin_unlock(&pipe_version_lock);
29279a3f20bS\"J. Bruce Fields\ 	}
29379a3f20bS\"J. Bruce Fields\ }
29479a3f20bS\"J. Bruce Fields\ 
2951da177e4SLinus Torvalds static void
2961da177e4SLinus Torvalds gss_release_msg(struct gss_upcall_msg *gss_msg)
2971da177e4SLinus Torvalds {
2982aed8b47STrond Myklebust 	struct net *net = rpc_net_ns(gss_msg->auth->client);
2991da177e4SLinus Torvalds 	if (!atomic_dec_and_test(&gss_msg->count))
3001da177e4SLinus Torvalds 		return;
3012aed8b47STrond Myklebust 	put_pipe_version(net);
3021da177e4SLinus Torvalds 	BUG_ON(!list_empty(&gss_msg->list));
3031da177e4SLinus Torvalds 	if (gss_msg->ctx != NULL)
3041da177e4SLinus Torvalds 		gss_put_ctx(gss_msg->ctx);
305f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue);
3061da177e4SLinus Torvalds 	kfree(gss_msg);
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds static struct gss_upcall_msg *
3107eaf040bSEric W. Biederman __gss_find_upcall(struct rpc_pipe *pipe, kuid_t uid)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	struct gss_upcall_msg *pos;
3139beae467SStanislav Kinsbursky 	list_for_each_entry(pos, &pipe->in_downcall, list) {
3140b4d51b0SEric W. Biederman 		if (!uid_eq(pos->uid, uid))
3151da177e4SLinus Torvalds 			continue;
3161da177e4SLinus Torvalds 		atomic_inc(&pos->count);
317632f0d05SChuck Lever 		dprintk("RPC:       %s found msg %p\n", __func__, pos);
3181da177e4SLinus Torvalds 		return pos;
3191da177e4SLinus Torvalds 	}
320632f0d05SChuck Lever 	dprintk("RPC:       %s found nothing\n", __func__);
3211da177e4SLinus Torvalds 	return NULL;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
324720b8f2dS\\\"J. Bruce Fields\\\ /* Try to add an upcall to the pipefs queue.
3251da177e4SLinus Torvalds  * If an upcall owned by our uid already exists, then we return a reference
3261da177e4SLinus Torvalds  * to that upcall instead of adding the new upcall.
3271da177e4SLinus Torvalds  */
3281da177e4SLinus Torvalds static inline struct gss_upcall_msg *
329053e324fSSuresh Jayaraman gss_add_msg(struct gss_upcall_msg *gss_msg)
3301da177e4SLinus Torvalds {
3319beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
3321da177e4SLinus Torvalds 	struct gss_upcall_msg *old;
3331da177e4SLinus Torvalds 
3349beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
3359beae467SStanislav Kinsbursky 	old = __gss_find_upcall(pipe, gss_msg->uid);
3361da177e4SLinus Torvalds 	if (old == NULL) {
3371da177e4SLinus Torvalds 		atomic_inc(&gss_msg->count);
3389beae467SStanislav Kinsbursky 		list_add(&gss_msg->list, &pipe->in_downcall);
3391da177e4SLinus Torvalds 	} else
3401da177e4SLinus Torvalds 		gss_msg = old;
3419beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
3421da177e4SLinus Torvalds 	return gss_msg;
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds static void
3461da177e4SLinus Torvalds __gss_unhash_msg(struct gss_upcall_msg *gss_msg)
3471da177e4SLinus Torvalds {
3481da177e4SLinus Torvalds 	list_del_init(&gss_msg->list);
3491da177e4SLinus Torvalds 	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
3501da177e4SLinus Torvalds 	wake_up_all(&gss_msg->waitqueue);
3511da177e4SLinus Torvalds 	atomic_dec(&gss_msg->count);
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds static void
3551da177e4SLinus Torvalds gss_unhash_msg(struct gss_upcall_msg *gss_msg)
3561da177e4SLinus Torvalds {
3579beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
3581da177e4SLinus Torvalds 
3593b68aaeaSTrond Myklebust 	if (list_empty(&gss_msg->list))
3603b68aaeaSTrond Myklebust 		return;
3619beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
3623b68aaeaSTrond Myklebust 	if (!list_empty(&gss_msg->list))
3631da177e4SLinus Torvalds 		__gss_unhash_msg(gss_msg);
3649beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds static void
368126e216aSTrond Myklebust gss_handle_downcall_result(struct gss_cred *gss_cred, struct gss_upcall_msg *gss_msg)
369126e216aSTrond Myklebust {
370126e216aSTrond Myklebust 	switch (gss_msg->msg.errno) {
371126e216aSTrond Myklebust 	case 0:
372126e216aSTrond Myklebust 		if (gss_msg->ctx == NULL)
373126e216aSTrond Myklebust 			break;
374126e216aSTrond Myklebust 		clear_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
375126e216aSTrond Myklebust 		gss_cred_set_ctx(&gss_cred->gc_base, gss_msg->ctx);
376126e216aSTrond Myklebust 		break;
377126e216aSTrond Myklebust 	case -EKEYEXPIRED:
378126e216aSTrond Myklebust 		set_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
379126e216aSTrond Myklebust 	}
380126e216aSTrond Myklebust 	gss_cred->gc_upcall_timestamp = jiffies;
381126e216aSTrond Myklebust 	gss_cred->gc_upcall = NULL;
382126e216aSTrond Myklebust 	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
383126e216aSTrond Myklebust }
384126e216aSTrond Myklebust 
385126e216aSTrond Myklebust static void
3861da177e4SLinus Torvalds gss_upcall_callback(struct rpc_task *task)
3871da177e4SLinus Torvalds {
388a17c2153STrond Myklebust 	struct gss_cred *gss_cred = container_of(task->tk_rqstp->rq_cred,
3891da177e4SLinus Torvalds 			struct gss_cred, gc_base);
3901da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
3919beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
3921da177e4SLinus Torvalds 
3939beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
394126e216aSTrond Myklebust 	gss_handle_downcall_result(gss_cred, gss_msg);
3959beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
396126e216aSTrond Myklebust 	task->tk_status = gss_msg->msg.errno;
3971da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
40034769fc4S\"J. Bruce Fields\ static void gss_encode_v0_msg(struct gss_upcall_msg *gss_msg)
40134769fc4S\"J. Bruce Fields\ {
40290602c7bSEric W. Biederman 	uid_t uid = from_kuid(&init_user_ns, gss_msg->uid);
40390602c7bSEric W. Biederman 	memcpy(gss_msg->databuf, &uid, sizeof(uid));
40490602c7bSEric W. Biederman 	gss_msg->msg.data = gss_msg->databuf;
40590602c7bSEric W. Biederman 	gss_msg->msg.len = sizeof(uid);
40690602c7bSEric W. Biederman 	BUG_ON(sizeof(uid) > UPCALL_BUF_LEN);
40734769fc4S\"J. Bruce Fields\ }
40834769fc4S\"J. Bruce Fields\ 
4098b1c7bf5SOlga Kornievskaia static void gss_encode_v1_msg(struct gss_upcall_msg *gss_msg,
410bd4a3eb1STrond Myklebust 				const char *service_name,
411bd4a3eb1STrond Myklebust 				const char *target_name)
41234769fc4S\"J. Bruce Fields\ {
413683ac665STrond Myklebust 	struct gss_api_mech *mech = gss_msg->auth->mech;
4148b1c7bf5SOlga Kornievskaia 	char *p = gss_msg->databuf;
4158b1c7bf5SOlga Kornievskaia 	int len = 0;
4168b1c7bf5SOlga Kornievskaia 
4178b1c7bf5SOlga Kornievskaia 	gss_msg->msg.len = sprintf(gss_msg->databuf, "mech=%s uid=%d ",
418683ac665STrond Myklebust 				   mech->gm_name,
41990602c7bSEric W. Biederman 				   from_kuid(&init_user_ns, gss_msg->uid));
4208b1c7bf5SOlga Kornievskaia 	p += gss_msg->msg.len;
421bd4a3eb1STrond Myklebust 	if (target_name) {
422bd4a3eb1STrond Myklebust 		len = sprintf(p, "target=%s ", target_name);
4238b1c7bf5SOlga Kornievskaia 		p += len;
4248b1c7bf5SOlga Kornievskaia 		gss_msg->msg.len += len;
4258b1c7bf5SOlga Kornievskaia 	}
42668c97153STrond Myklebust 	if (service_name != NULL) {
42768c97153STrond Myklebust 		len = sprintf(p, "service=%s ", service_name);
4282efef708SOlga Kornievskaia 		p += len;
4292efef708SOlga Kornievskaia 		gss_msg->msg.len += len;
4302efef708SOlga Kornievskaia 	}
431683ac665STrond Myklebust 	if (mech->gm_upcall_enctypes) {
432f8628220SKevin Coffman 		len = sprintf(p, "enctypes=%s ", mech->gm_upcall_enctypes);
433683ac665STrond Myklebust 		p += len;
434683ac665STrond Myklebust 		gss_msg->msg.len += len;
435683ac665STrond Myklebust 	}
4368b1c7bf5SOlga Kornievskaia 	len = sprintf(p, "\n");
4378b1c7bf5SOlga Kornievskaia 	gss_msg->msg.len += len;
4388b1c7bf5SOlga Kornievskaia 
43934769fc4S\"J. Bruce Fields\ 	gss_msg->msg.data = gss_msg->databuf;
44034769fc4S\"J. Bruce Fields\ 	BUG_ON(gss_msg->msg.len > UPCALL_BUF_LEN);
44134769fc4S\"J. Bruce Fields\ }
44234769fc4S\"J. Bruce Fields\ 
44368c97153STrond Myklebust static struct gss_upcall_msg *
44468c97153STrond Myklebust gss_alloc_msg(struct gss_auth *gss_auth, struct rpc_clnt *clnt,
4457eaf040bSEric W. Biederman 		kuid_t uid, const char *service_name)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
44879a3f20bS\"J. Bruce Fields\ 	int vers;
4491da177e4SLinus Torvalds 
4500f38b873STrond Myklebust 	gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
451db75b3d6S\"J. Bruce Fields\ 	if (gss_msg == NULL)
452db75b3d6S\"J. Bruce Fields\ 		return ERR_PTR(-ENOMEM);
4532aed8b47STrond Myklebust 	vers = get_pipe_version(rpc_net_ns(clnt));
45479a3f20bS\"J. Bruce Fields\ 	if (vers < 0) {
45579a3f20bS\"J. Bruce Fields\ 		kfree(gss_msg);
45679a3f20bS\"J. Bruce Fields\ 		return ERR_PTR(vers);
45779a3f20bS\"J. Bruce Fields\ 	}
458c239d83bSStanislav Kinsbursky 	gss_msg->pipe = gss_auth->pipe[vers];
4591da177e4SLinus Torvalds 	INIT_LIST_HEAD(&gss_msg->list);
4601da177e4SLinus Torvalds 	rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
4611da177e4SLinus Torvalds 	init_waitqueue_head(&gss_msg->waitqueue);
4621da177e4SLinus Torvalds 	atomic_set(&gss_msg->count, 1);
4631da177e4SLinus Torvalds 	gss_msg->uid = uid;
4641da177e4SLinus Torvalds 	gss_msg->auth = gss_auth;
465bd4a3eb1STrond Myklebust 	switch (vers) {
466bd4a3eb1STrond Myklebust 	case 0:
467bd4a3eb1STrond Myklebust 		gss_encode_v0_msg(gss_msg);
468bd4a3eb1STrond Myklebust 	default:
469bd4a3eb1STrond Myklebust 		gss_encode_v1_msg(gss_msg, service_name, gss_auth->target_name);
470bd4a3eb1STrond Myklebust 	};
4711da177e4SLinus Torvalds 	return gss_msg;
4721da177e4SLinus Torvalds }
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds static struct gss_upcall_msg *
4751da177e4SLinus Torvalds gss_setup_upcall(struct rpc_clnt *clnt, struct gss_auth *gss_auth, struct rpc_cred *cred)
4761da177e4SLinus Torvalds {
4777c67db3aSTrond Myklebust 	struct gss_cred *gss_cred = container_of(cred,
4787c67db3aSTrond Myklebust 			struct gss_cred, gc_base);
4791da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_new, *gss_msg;
4807eaf040bSEric W. Biederman 	kuid_t uid = cred->cr_uid;
4811da177e4SLinus Torvalds 
48268c97153STrond Myklebust 	gss_new = gss_alloc_msg(gss_auth, clnt, uid, gss_cred->gc_principal);
483db75b3d6S\"J. Bruce Fields\ 	if (IS_ERR(gss_new))
484db75b3d6S\"J. Bruce Fields\ 		return gss_new;
485053e324fSSuresh Jayaraman 	gss_msg = gss_add_msg(gss_new);
4861da177e4SLinus Torvalds 	if (gss_msg == gss_new) {
4879beae467SStanislav Kinsbursky 		int res = rpc_queue_upcall(gss_new->pipe, &gss_new->msg);
4881da177e4SLinus Torvalds 		if (res) {
4891da177e4SLinus Torvalds 			gss_unhash_msg(gss_new);
4901da177e4SLinus Torvalds 			gss_msg = ERR_PTR(res);
4911da177e4SLinus Torvalds 		}
4921da177e4SLinus Torvalds 	} else
4931da177e4SLinus Torvalds 		gss_release_msg(gss_new);
4941da177e4SLinus Torvalds 	return gss_msg;
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds 
497b03568c3S\"J. Bruce Fields\ static void warn_gssd(void)
498b03568c3S\"J. Bruce Fields\ {
499b03568c3S\"J. Bruce Fields\ 	static unsigned long ratelimit;
500b03568c3S\"J. Bruce Fields\ 	unsigned long now = jiffies;
501b03568c3S\"J. Bruce Fields\ 
502b03568c3S\"J. Bruce Fields\ 	if (time_after(now, ratelimit)) {
503b03568c3S\"J. Bruce Fields\ 		printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
504b03568c3S\"J. Bruce Fields\ 				"Please check user daemon is running.\n");
505b03568c3S\"J. Bruce Fields\ 		ratelimit = now + 15*HZ;
506b03568c3S\"J. Bruce Fields\ 	}
507b03568c3S\"J. Bruce Fields\ }
508b03568c3S\"J. Bruce Fields\ 
5091da177e4SLinus Torvalds static inline int
5101da177e4SLinus Torvalds gss_refresh_upcall(struct rpc_task *task)
5111da177e4SLinus Torvalds {
512a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
5134a8c1344STrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth,
5141da177e4SLinus Torvalds 			struct gss_auth, rpc_auth);
5151da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred,
5161da177e4SLinus Torvalds 			struct gss_cred, gc_base);
5171da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
5189beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
5191da177e4SLinus Torvalds 	int err = 0;
5201da177e4SLinus Torvalds 
521632f0d05SChuck Lever 	dprintk("RPC: %5u %s for uid %u\n",
522cdba321eSEric W. Biederman 		task->tk_pid, __func__, from_kuid(&init_user_ns, cred->cr_uid));
5231da177e4SLinus Torvalds 	gss_msg = gss_setup_upcall(task->tk_client, gss_auth, cred);
524480e3243SRoel Kluin 	if (PTR_ERR(gss_msg) == -EAGAIN) {
52579a3f20bS\"J. Bruce Fields\ 		/* XXX: warning on the first, under the assumption we
52679a3f20bS\"J. Bruce Fields\ 		 * shouldn't normally hit this case on a refresh. */
52779a3f20bS\"J. Bruce Fields\ 		warn_gssd();
52879a3f20bS\"J. Bruce Fields\ 		task->tk_timeout = 15*HZ;
52979a3f20bS\"J. Bruce Fields\ 		rpc_sleep_on(&pipe_version_rpc_waitqueue, task, NULL);
530d1a8016aSBryan Schumaker 		return -EAGAIN;
53179a3f20bS\"J. Bruce Fields\ 	}
5321da177e4SLinus Torvalds 	if (IS_ERR(gss_msg)) {
5331da177e4SLinus Torvalds 		err = PTR_ERR(gss_msg);
5341da177e4SLinus Torvalds 		goto out;
5351da177e4SLinus Torvalds 	}
5369beae467SStanislav Kinsbursky 	pipe = gss_msg->pipe;
5379beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
5381da177e4SLinus Torvalds 	if (gss_cred->gc_upcall != NULL)
5395d00837bSTrond Myklebust 		rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL);
540126e216aSTrond Myklebust 	else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
5411da177e4SLinus Torvalds 		task->tk_timeout = 0;
5421da177e4SLinus Torvalds 		gss_cred->gc_upcall = gss_msg;
5431da177e4SLinus Torvalds 		/* gss_upcall_callback will release the reference to gss_upcall_msg */
5441da177e4SLinus Torvalds 		atomic_inc(&gss_msg->count);
5455d00837bSTrond Myklebust 		rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback);
546126e216aSTrond Myklebust 	} else {
547126e216aSTrond Myklebust 		gss_handle_downcall_result(gss_cred, gss_msg);
5481da177e4SLinus Torvalds 		err = gss_msg->msg.errno;
549126e216aSTrond Myklebust 	}
5509beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
5511da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
5521da177e4SLinus Torvalds out:
553632f0d05SChuck Lever 	dprintk("RPC: %5u %s for uid %u result %d\n",
554cdba321eSEric W. Biederman 		task->tk_pid, __func__,
555cdba321eSEric W. Biederman 		from_kuid(&init_user_ns, cred->cr_uid),	err);
5561da177e4SLinus Torvalds 	return err;
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds static inline int
5601da177e4SLinus Torvalds gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
5611da177e4SLinus Torvalds {
562abfdbd53STrond Myklebust 	struct net *net = rpc_net_ns(gss_auth->client);
563abfdbd53STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
5649beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
5651da177e4SLinus Torvalds 	struct rpc_cred *cred = &gss_cred->gc_base;
5661da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
567abfdbd53STrond Myklebust 	unsigned long timeout;
5681da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
569d36ccb9cSTrond Myklebust 	int err;
5701da177e4SLinus Torvalds 
571cdba321eSEric W. Biederman 	dprintk("RPC:       %s for uid %u\n",
572cdba321eSEric W. Biederman 		__func__, from_kuid(&init_user_ns, cred->cr_uid));
57379a3f20bS\"J. Bruce Fields\ retry:
574d36ccb9cSTrond Myklebust 	err = 0;
575abfdbd53STrond Myklebust 	/* Default timeout is 15s unless we know that gssd is not running */
576abfdbd53STrond Myklebust 	timeout = 15 * HZ;
577abfdbd53STrond Myklebust 	if (!sn->gssd_running)
578abfdbd53STrond Myklebust 		timeout = HZ >> 2;
5791da177e4SLinus Torvalds 	gss_msg = gss_setup_upcall(gss_auth->client, gss_auth, cred);
58079a3f20bS\"J. Bruce Fields\ 	if (PTR_ERR(gss_msg) == -EAGAIN) {
58179a3f20bS\"J. Bruce Fields\ 		err = wait_event_interruptible_timeout(pipe_version_waitqueue,
5822aed8b47STrond Myklebust 				sn->pipe_version >= 0, timeout);
5832aed8b47STrond Myklebust 		if (sn->pipe_version < 0) {
584abfdbd53STrond Myklebust 			if (err == 0)
585abfdbd53STrond Myklebust 				sn->gssd_running = 0;
586d1a8016aSBryan Schumaker 			warn_gssd();
587d1a8016aSBryan Schumaker 			err = -EACCES;
588d1a8016aSBryan Schumaker 		}
589d36ccb9cSTrond Myklebust 		if (err < 0)
59079a3f20bS\"J. Bruce Fields\ 			goto out;
59179a3f20bS\"J. Bruce Fields\ 		goto retry;
59279a3f20bS\"J. Bruce Fields\ 	}
5931da177e4SLinus Torvalds 	if (IS_ERR(gss_msg)) {
5941da177e4SLinus Torvalds 		err = PTR_ERR(gss_msg);
5951da177e4SLinus Torvalds 		goto out;
5961da177e4SLinus Torvalds 	}
5979beae467SStanislav Kinsbursky 	pipe = gss_msg->pipe;
5981da177e4SLinus Torvalds 	for (;;) {
5995afa9133STrond Myklebust 		prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_KILLABLE);
6009beae467SStanislav Kinsbursky 		spin_lock(&pipe->lock);
6011da177e4SLinus Torvalds 		if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
6021da177e4SLinus Torvalds 			break;
6031da177e4SLinus Torvalds 		}
6049beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
6055afa9133STrond Myklebust 		if (fatal_signal_pending(current)) {
6061da177e4SLinus Torvalds 			err = -ERESTARTSYS;
6071da177e4SLinus Torvalds 			goto out_intr;
6081da177e4SLinus Torvalds 		}
6091da177e4SLinus Torvalds 		schedule();
6101da177e4SLinus Torvalds 	}
6111da177e4SLinus Torvalds 	if (gss_msg->ctx)
6127b6962b0STrond Myklebust 		gss_cred_set_ctx(cred, gss_msg->ctx);
6131da177e4SLinus Torvalds 	else
6141da177e4SLinus Torvalds 		err = gss_msg->msg.errno;
6159beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
6161da177e4SLinus Torvalds out_intr:
6171da177e4SLinus Torvalds 	finish_wait(&gss_msg->waitqueue, &wait);
6181da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
6191da177e4SLinus Torvalds out:
620632f0d05SChuck Lever 	dprintk("RPC:       %s for uid %u result %d\n",
621cdba321eSEric W. Biederman 		__func__, from_kuid(&init_user_ns, cred->cr_uid), err);
6221da177e4SLinus Torvalds 	return err;
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds #define MSG_BUF_MAXSIZE 1024
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds static ssize_t
6281da177e4SLinus Torvalds gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
6291da177e4SLinus Torvalds {
6301da177e4SLinus Torvalds 	const void *p, *end;
6311da177e4SLinus Torvalds 	void *buf;
6321da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
633496ad9aaSAl Viro 	struct rpc_pipe *pipe = RPC_I(file_inode(filp))->pipe;
6341da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
63590602c7bSEric W. Biederman 	uid_t id;
63690602c7bSEric W. Biederman 	kuid_t uid;
6373b68aaeaSTrond Myklebust 	ssize_t err = -EFBIG;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	if (mlen > MSG_BUF_MAXSIZE)
6401da177e4SLinus Torvalds 		goto out;
6411da177e4SLinus Torvalds 	err = -ENOMEM;
6420f38b873STrond Myklebust 	buf = kmalloc(mlen, GFP_NOFS);
6431da177e4SLinus Torvalds 	if (!buf)
6441da177e4SLinus Torvalds 		goto out;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	err = -EFAULT;
6471da177e4SLinus Torvalds 	if (copy_from_user(buf, src, mlen))
6481da177e4SLinus Torvalds 		goto err;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	end = (const void *)((char *)buf + mlen);
65190602c7bSEric W. Biederman 	p = simple_get_bytes(buf, end, &id, sizeof(id));
6521da177e4SLinus Torvalds 	if (IS_ERR(p)) {
6531da177e4SLinus Torvalds 		err = PTR_ERR(p);
6541da177e4SLinus Torvalds 		goto err;
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 
65790602c7bSEric W. Biederman 	uid = make_kuid(&init_user_ns, id);
65890602c7bSEric W. Biederman 	if (!uid_valid(uid)) {
65990602c7bSEric W. Biederman 		err = -EINVAL;
66090602c7bSEric W. Biederman 		goto err;
66190602c7bSEric W. Biederman 	}
66290602c7bSEric W. Biederman 
6631da177e4SLinus Torvalds 	err = -ENOMEM;
6641da177e4SLinus Torvalds 	ctx = gss_alloc_context();
6651da177e4SLinus Torvalds 	if (ctx == NULL)
6661da177e4SLinus Torvalds 		goto err;
6673b68aaeaSTrond Myklebust 
6683b68aaeaSTrond Myklebust 	err = -ENOENT;
6693b68aaeaSTrond Myklebust 	/* Find a matching upcall */
6709beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
6719beae467SStanislav Kinsbursky 	gss_msg = __gss_find_upcall(pipe, uid);
6723b68aaeaSTrond Myklebust 	if (gss_msg == NULL) {
6739beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
6743b68aaeaSTrond Myklebust 		goto err_put_ctx;
6753b68aaeaSTrond Myklebust 	}
6763b68aaeaSTrond Myklebust 	list_del_init(&gss_msg->list);
6779beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
6783b68aaeaSTrond Myklebust 
6796e84c7b6STrond Myklebust 	p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
6801da177e4SLinus Torvalds 	if (IS_ERR(p)) {
6811da177e4SLinus Torvalds 		err = PTR_ERR(p);
682486bad2eSJeff Layton 		switch (err) {
683486bad2eSJeff Layton 		case -EACCES:
684dc5ddce9SJeff Layton 		case -EKEYEXPIRED:
685486bad2eSJeff Layton 			gss_msg->msg.errno = err;
686486bad2eSJeff Layton 			err = mlen;
687486bad2eSJeff Layton 			break;
688486bad2eSJeff Layton 		case -EFAULT:
689486bad2eSJeff Layton 		case -ENOMEM:
690486bad2eSJeff Layton 		case -EINVAL:
691486bad2eSJeff Layton 		case -ENOSYS:
692486bad2eSJeff Layton 			gss_msg->msg.errno = -EAGAIN;
693486bad2eSJeff Layton 			break;
694486bad2eSJeff Layton 		default:
695486bad2eSJeff Layton 			printk(KERN_CRIT "%s: bad return from "
6966c853099SRandy Dunlap 				"gss_fill_context: %zd\n", __func__, err);
697486bad2eSJeff Layton 			BUG();
698486bad2eSJeff Layton 		}
6993b68aaeaSTrond Myklebust 		goto err_release_msg;
7001da177e4SLinus Torvalds 	}
7011da177e4SLinus Torvalds 	gss_msg->ctx = gss_get_ctx(ctx);
7023b68aaeaSTrond Myklebust 	err = mlen;
7033b68aaeaSTrond Myklebust 
7043b68aaeaSTrond Myklebust err_release_msg:
7059beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
7061da177e4SLinus Torvalds 	__gss_unhash_msg(gss_msg);
7079beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
7081da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
7091da177e4SLinus Torvalds err_put_ctx:
7101da177e4SLinus Torvalds 	gss_put_ctx(ctx);
7111da177e4SLinus Torvalds err:
7121da177e4SLinus Torvalds 	kfree(buf);
7131da177e4SLinus Torvalds out:
714632f0d05SChuck Lever 	dprintk("RPC:       %s returning %Zd\n", __func__, err);
7151da177e4SLinus Torvalds 	return err;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
71834769fc4S\"J. Bruce Fields\ static int gss_pipe_open(struct inode *inode, int new_version)
719cf81939dS\"J. Bruce Fields\ {
7202aed8b47STrond Myklebust 	struct net *net = inode->i_sb->s_fs_info;
7212aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
72234769fc4S\"J. Bruce Fields\ 	int ret = 0;
72334769fc4S\"J. Bruce Fields\ 
72479a3f20bS\"J. Bruce Fields\ 	spin_lock(&pipe_version_lock);
7252aed8b47STrond Myklebust 	if (sn->pipe_version < 0) {
72634769fc4S\"J. Bruce Fields\ 		/* First open of any gss pipe determines the version: */
7272aed8b47STrond Myklebust 		sn->pipe_version = new_version;
72879a3f20bS\"J. Bruce Fields\ 		rpc_wake_up(&pipe_version_rpc_waitqueue);
72979a3f20bS\"J. Bruce Fields\ 		wake_up(&pipe_version_waitqueue);
7302aed8b47STrond Myklebust 	} else if (sn->pipe_version != new_version) {
73134769fc4S\"J. Bruce Fields\ 		/* Trying to open a pipe of a different version */
73234769fc4S\"J. Bruce Fields\ 		ret = -EBUSY;
73334769fc4S\"J. Bruce Fields\ 		goto out;
73479a3f20bS\"J. Bruce Fields\ 	}
7352aed8b47STrond Myklebust 	atomic_inc(&sn->pipe_users);
73634769fc4S\"J. Bruce Fields\ out:
73779a3f20bS\"J. Bruce Fields\ 	spin_unlock(&pipe_version_lock);
73834769fc4S\"J. Bruce Fields\ 	return ret;
73934769fc4S\"J. Bruce Fields\ 
74034769fc4S\"J. Bruce Fields\ }
74134769fc4S\"J. Bruce Fields\ 
74234769fc4S\"J. Bruce Fields\ static int gss_pipe_open_v0(struct inode *inode)
74334769fc4S\"J. Bruce Fields\ {
74434769fc4S\"J. Bruce Fields\ 	return gss_pipe_open(inode, 0);
74534769fc4S\"J. Bruce Fields\ }
74634769fc4S\"J. Bruce Fields\ 
74734769fc4S\"J. Bruce Fields\ static int gss_pipe_open_v1(struct inode *inode)
74834769fc4S\"J. Bruce Fields\ {
74934769fc4S\"J. Bruce Fields\ 	return gss_pipe_open(inode, 1);
750cf81939dS\"J. Bruce Fields\ }
751cf81939dS\"J. Bruce Fields\ 
7521da177e4SLinus Torvalds static void
7531da177e4SLinus Torvalds gss_pipe_release(struct inode *inode)
7541da177e4SLinus Torvalds {
7552aed8b47STrond Myklebust 	struct net *net = inode->i_sb->s_fs_info;
7569beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
7571da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
7581da177e4SLinus Torvalds 
7595a67657aSTrond Myklebust restart:
7609beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
7619beae467SStanislav Kinsbursky 	list_for_each_entry(gss_msg, &pipe->in_downcall, list) {
7626e84c7b6STrond Myklebust 
7635a67657aSTrond Myklebust 		if (!list_empty(&gss_msg->msg.list))
7645a67657aSTrond Myklebust 			continue;
7651da177e4SLinus Torvalds 		gss_msg->msg.errno = -EPIPE;
7661da177e4SLinus Torvalds 		atomic_inc(&gss_msg->count);
7671da177e4SLinus Torvalds 		__gss_unhash_msg(gss_msg);
7689beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
7691da177e4SLinus Torvalds 		gss_release_msg(gss_msg);
7705a67657aSTrond Myklebust 		goto restart;
7711da177e4SLinus Torvalds 	}
7729beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
773cf81939dS\"J. Bruce Fields\ 
7742aed8b47STrond Myklebust 	put_pipe_version(net);
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds static void
7781da177e4SLinus Torvalds gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
7791da177e4SLinus Torvalds {
7801da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	if (msg->errno < 0) {
783632f0d05SChuck Lever 		dprintk("RPC:       %s releasing msg %p\n",
784632f0d05SChuck Lever 			__func__, gss_msg);
7851da177e4SLinus Torvalds 		atomic_inc(&gss_msg->count);
7861da177e4SLinus Torvalds 		gss_unhash_msg(gss_msg);
787b03568c3S\"J. Bruce Fields\ 		if (msg->errno == -ETIMEDOUT)
788b03568c3S\"J. Bruce Fields\ 			warn_gssd();
7891da177e4SLinus Torvalds 		gss_release_msg(gss_msg);
7901da177e4SLinus Torvalds 	}
7911da177e4SLinus Torvalds }
7921da177e4SLinus Torvalds 
793ccdc28f8SStanislav Kinsbursky static void gss_pipes_dentries_destroy(struct rpc_auth *auth)
794ccdc28f8SStanislav Kinsbursky {
795ccdc28f8SStanislav Kinsbursky 	struct gss_auth *gss_auth;
796ccdc28f8SStanislav Kinsbursky 
797ccdc28f8SStanislav Kinsbursky 	gss_auth = container_of(auth, struct gss_auth, rpc_auth);
79880df9d20SStanislav Kinsbursky 	if (gss_auth->pipe[0]->dentry)
799ccdc28f8SStanislav Kinsbursky 		rpc_unlink(gss_auth->pipe[0]->dentry);
80080df9d20SStanislav Kinsbursky 	if (gss_auth->pipe[1]->dentry)
801ccdc28f8SStanislav Kinsbursky 		rpc_unlink(gss_auth->pipe[1]->dentry);
802ccdc28f8SStanislav Kinsbursky }
803ccdc28f8SStanislav Kinsbursky 
804ccdc28f8SStanislav Kinsbursky static int gss_pipes_dentries_create(struct rpc_auth *auth)
805ccdc28f8SStanislav Kinsbursky {
806ccdc28f8SStanislav Kinsbursky 	int err;
807ccdc28f8SStanislav Kinsbursky 	struct gss_auth *gss_auth;
808ccdc28f8SStanislav Kinsbursky 	struct rpc_clnt *clnt;
809ccdc28f8SStanislav Kinsbursky 
810ccdc28f8SStanislav Kinsbursky 	gss_auth = container_of(auth, struct gss_auth, rpc_auth);
811ccdc28f8SStanislav Kinsbursky 	clnt = gss_auth->client;
812ccdc28f8SStanislav Kinsbursky 
81330507f58SStanislav Kinsbursky 	gss_auth->pipe[1]->dentry = rpc_mkpipe_dentry(clnt->cl_dentry,
814ccdc28f8SStanislav Kinsbursky 						      "gssd",
815ccdc28f8SStanislav Kinsbursky 						      clnt, gss_auth->pipe[1]);
816ccdc28f8SStanislav Kinsbursky 	if (IS_ERR(gss_auth->pipe[1]->dentry))
817ccdc28f8SStanislav Kinsbursky 		return PTR_ERR(gss_auth->pipe[1]->dentry);
81830507f58SStanislav Kinsbursky 	gss_auth->pipe[0]->dentry = rpc_mkpipe_dentry(clnt->cl_dentry,
819ccdc28f8SStanislav Kinsbursky 						      gss_auth->mech->gm_name,
820ccdc28f8SStanislav Kinsbursky 						      clnt, gss_auth->pipe[0]);
821ccdc28f8SStanislav Kinsbursky 	if (IS_ERR(gss_auth->pipe[0]->dentry)) {
822ccdc28f8SStanislav Kinsbursky 		err = PTR_ERR(gss_auth->pipe[0]->dentry);
823ccdc28f8SStanislav Kinsbursky 		goto err_unlink_pipe_1;
824ccdc28f8SStanislav Kinsbursky 	}
825ccdc28f8SStanislav Kinsbursky 	return 0;
826ccdc28f8SStanislav Kinsbursky 
827ccdc28f8SStanislav Kinsbursky err_unlink_pipe_1:
828ccdc28f8SStanislav Kinsbursky 	rpc_unlink(gss_auth->pipe[1]->dentry);
829ccdc28f8SStanislav Kinsbursky 	return err;
830ccdc28f8SStanislav Kinsbursky }
831ccdc28f8SStanislav Kinsbursky 
832ccdc28f8SStanislav Kinsbursky static void gss_pipes_dentries_destroy_net(struct rpc_clnt *clnt,
833ccdc28f8SStanislav Kinsbursky 					   struct rpc_auth *auth)
834ccdc28f8SStanislav Kinsbursky {
8352446ab60STrond Myklebust 	struct net *net = rpc_net_ns(clnt);
836ccdc28f8SStanislav Kinsbursky 	struct super_block *sb;
837ccdc28f8SStanislav Kinsbursky 
838ccdc28f8SStanislav Kinsbursky 	sb = rpc_get_sb_net(net);
839ccdc28f8SStanislav Kinsbursky 	if (sb) {
84030507f58SStanislav Kinsbursky 		if (clnt->cl_dentry)
841ccdc28f8SStanislav Kinsbursky 			gss_pipes_dentries_destroy(auth);
842ccdc28f8SStanislav Kinsbursky 		rpc_put_sb_net(net);
843ccdc28f8SStanislav Kinsbursky 	}
844ccdc28f8SStanislav Kinsbursky }
845ccdc28f8SStanislav Kinsbursky 
846ccdc28f8SStanislav Kinsbursky static int gss_pipes_dentries_create_net(struct rpc_clnt *clnt,
847ccdc28f8SStanislav Kinsbursky 					 struct rpc_auth *auth)
848ccdc28f8SStanislav Kinsbursky {
8492446ab60STrond Myklebust 	struct net *net = rpc_net_ns(clnt);
850ccdc28f8SStanislav Kinsbursky 	struct super_block *sb;
851ccdc28f8SStanislav Kinsbursky 	int err = 0;
852ccdc28f8SStanislav Kinsbursky 
853ccdc28f8SStanislav Kinsbursky 	sb = rpc_get_sb_net(net);
854ccdc28f8SStanislav Kinsbursky 	if (sb) {
85530507f58SStanislav Kinsbursky 		if (clnt->cl_dentry)
856ccdc28f8SStanislav Kinsbursky 			err = gss_pipes_dentries_create(auth);
857ccdc28f8SStanislav Kinsbursky 		rpc_put_sb_net(net);
858ccdc28f8SStanislav Kinsbursky 	}
859ccdc28f8SStanislav Kinsbursky 	return err;
860ccdc28f8SStanislav Kinsbursky }
861ccdc28f8SStanislav Kinsbursky 
8621da177e4SLinus Torvalds /*
8631da177e4SLinus Torvalds  * NOTE: we have the opportunity to use different
8641da177e4SLinus Torvalds  * parameters based on the input flavor (which must be a pseudoflavor)
8651da177e4SLinus Torvalds  */
8661da177e4SLinus Torvalds static struct rpc_auth *
867c2190661STrond Myklebust gss_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
8681da177e4SLinus Torvalds {
869c2190661STrond Myklebust 	rpc_authflavor_t flavor = args->pseudoflavor;
8701da177e4SLinus Torvalds 	struct gss_auth *gss_auth;
8711da177e4SLinus Torvalds 	struct rpc_auth * auth;
8726a19275aSJ. Bruce Fields 	int err = -ENOMEM; /* XXX? */
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	dprintk("RPC:       creating GSS authenticator for client %p\n", clnt);
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	if (!try_module_get(THIS_MODULE))
8776a19275aSJ. Bruce Fields 		return ERR_PTR(err);
8781da177e4SLinus Torvalds 	if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
8791da177e4SLinus Torvalds 		goto out_dec;
880bd4a3eb1STrond Myklebust 	gss_auth->target_name = NULL;
881c2190661STrond Myklebust 	if (args->target_name) {
882c2190661STrond Myklebust 		gss_auth->target_name = kstrdup(args->target_name, GFP_KERNEL);
883bd4a3eb1STrond Myklebust 		if (gss_auth->target_name == NULL)
884bd4a3eb1STrond Myklebust 			goto err_free;
885bd4a3eb1STrond Myklebust 	}
8861da177e4SLinus Torvalds 	gss_auth->client = clnt;
8876a19275aSJ. Bruce Fields 	err = -EINVAL;
8881da177e4SLinus Torvalds 	gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
8891da177e4SLinus Torvalds 	if (!gss_auth->mech) {
8909b1d75b7STrond Myklebust 		dprintk("RPC:       Pseudoflavor %d not found!\n", flavor);
8911da177e4SLinus Torvalds 		goto err_free;
8921da177e4SLinus Torvalds 	}
8931da177e4SLinus Torvalds 	gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
894438b6fdeSJ. Bruce Fields 	if (gss_auth->service == 0)
895438b6fdeSJ. Bruce Fields 		goto err_put_mech;
8961da177e4SLinus Torvalds 	auth = &gss_auth->rpc_auth;
8971da177e4SLinus Torvalds 	auth->au_cslack = GSS_CRED_SLACK >> 2;
8981da177e4SLinus Torvalds 	auth->au_rslack = GSS_VERF_SLACK >> 2;
8991da177e4SLinus Torvalds 	auth->au_ops = &authgss_ops;
9001da177e4SLinus Torvalds 	auth->au_flavor = flavor;
9011da177e4SLinus Torvalds 	atomic_set(&auth->au_count, 1);
9020285ed1fSTrond Myklebust 	kref_init(&gss_auth->kref);
9031da177e4SLinus Torvalds 
90434769fc4S\"J. Bruce Fields\ 	/*
90534769fc4S\"J. Bruce Fields\ 	 * Note: if we created the old pipe first, then someone who
90634769fc4S\"J. Bruce Fields\ 	 * examined the directory at the right moment might conclude
90734769fc4S\"J. Bruce Fields\ 	 * that we supported only the old pipe.  So we instead create
90834769fc4S\"J. Bruce Fields\ 	 * the new pipe first.
90934769fc4S\"J. Bruce Fields\ 	 */
910c239d83bSStanislav Kinsbursky 	gss_auth->pipe[1] = rpc_mkpipe_data(&gss_upcall_ops_v1,
91134769fc4S\"J. Bruce Fields\ 					    RPC_PIPE_WAIT_FOR_OPEN);
912c239d83bSStanislav Kinsbursky 	if (IS_ERR(gss_auth->pipe[1])) {
913c239d83bSStanislav Kinsbursky 		err = PTR_ERR(gss_auth->pipe[1]);
9141da177e4SLinus Torvalds 		goto err_put_mech;
9156a19275aSJ. Bruce Fields 	}
9161da177e4SLinus Torvalds 
917c239d83bSStanislav Kinsbursky 	gss_auth->pipe[0] = rpc_mkpipe_data(&gss_upcall_ops_v0,
91834769fc4S\"J. Bruce Fields\ 					    RPC_PIPE_WAIT_FOR_OPEN);
919c239d83bSStanislav Kinsbursky 	if (IS_ERR(gss_auth->pipe[0])) {
920c239d83bSStanislav Kinsbursky 		err = PTR_ERR(gss_auth->pipe[0]);
921c239d83bSStanislav Kinsbursky 		goto err_destroy_pipe_1;
922c239d83bSStanislav Kinsbursky 	}
923ccdc28f8SStanislav Kinsbursky 	err = gss_pipes_dentries_create_net(clnt, auth);
924ccdc28f8SStanislav Kinsbursky 	if (err)
925c239d83bSStanislav Kinsbursky 		goto err_destroy_pipe_0;
926f5c2187cSTrond Myklebust 	err = rpcauth_init_credcache(auth);
92707a2bf1dSTrond Myklebust 	if (err)
928ccdc28f8SStanislav Kinsbursky 		goto err_unlink_pipes;
92907a2bf1dSTrond Myklebust 
9301da177e4SLinus Torvalds 	return auth;
931ccdc28f8SStanislav Kinsbursky err_unlink_pipes:
932ccdc28f8SStanislav Kinsbursky 	gss_pipes_dentries_destroy_net(clnt, auth);
933c239d83bSStanislav Kinsbursky err_destroy_pipe_0:
934c239d83bSStanislav Kinsbursky 	rpc_destroy_pipe_data(gss_auth->pipe[0]);
935c239d83bSStanislav Kinsbursky err_destroy_pipe_1:
936c239d83bSStanislav Kinsbursky 	rpc_destroy_pipe_data(gss_auth->pipe[1]);
9371da177e4SLinus Torvalds err_put_mech:
9381da177e4SLinus Torvalds 	gss_mech_put(gss_auth->mech);
9391da177e4SLinus Torvalds err_free:
940bd4a3eb1STrond Myklebust 	kfree(gss_auth->target_name);
9411da177e4SLinus Torvalds 	kfree(gss_auth);
9421da177e4SLinus Torvalds out_dec:
9431da177e4SLinus Torvalds 	module_put(THIS_MODULE);
9446a19275aSJ. Bruce Fields 	return ERR_PTR(err);
9451da177e4SLinus Torvalds }
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds static void
9480285ed1fSTrond Myklebust gss_free(struct gss_auth *gss_auth)
9490285ed1fSTrond Myklebust {
950ccdc28f8SStanislav Kinsbursky 	gss_pipes_dentries_destroy_net(gss_auth->client, &gss_auth->rpc_auth);
951c239d83bSStanislav Kinsbursky 	rpc_destroy_pipe_data(gss_auth->pipe[0]);
952c239d83bSStanislav Kinsbursky 	rpc_destroy_pipe_data(gss_auth->pipe[1]);
9530285ed1fSTrond Myklebust 	gss_mech_put(gss_auth->mech);
954bd4a3eb1STrond Myklebust 	kfree(gss_auth->target_name);
9550285ed1fSTrond Myklebust 
9560285ed1fSTrond Myklebust 	kfree(gss_auth);
9570285ed1fSTrond Myklebust 	module_put(THIS_MODULE);
9580285ed1fSTrond Myklebust }
9590285ed1fSTrond Myklebust 
9600285ed1fSTrond Myklebust static void
9610285ed1fSTrond Myklebust gss_free_callback(struct kref *kref)
9620285ed1fSTrond Myklebust {
9630285ed1fSTrond Myklebust 	struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
9640285ed1fSTrond Myklebust 
9650285ed1fSTrond Myklebust 	gss_free(gss_auth);
9660285ed1fSTrond Myklebust }
9670285ed1fSTrond Myklebust 
9680285ed1fSTrond Myklebust static void
9691da177e4SLinus Torvalds gss_destroy(struct rpc_auth *auth)
9701da177e4SLinus Torvalds {
9711da177e4SLinus Torvalds 	struct gss_auth *gss_auth;
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	dprintk("RPC:       destroying GSS authenticator %p flavor %d\n",
9741da177e4SLinus Torvalds 			auth, auth->au_flavor);
9751da177e4SLinus Torvalds 
9763ab9bb72STrond Myklebust 	rpcauth_destroy_credcache(auth);
9773ab9bb72STrond Myklebust 
9781da177e4SLinus Torvalds 	gss_auth = container_of(auth, struct gss_auth, rpc_auth);
9790285ed1fSTrond Myklebust 	kref_put(&gss_auth->kref, gss_free_callback);
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
9820df7fb74STrond Myklebust /*
9830df7fb74STrond Myklebust  * gss_destroying_context will cause the RPCSEC_GSS to send a NULL RPC call
9840df7fb74STrond Myklebust  * to the server with the GSS control procedure field set to
9850df7fb74STrond Myklebust  * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
9860df7fb74STrond Myklebust  * all RPCSEC_GSS state associated with that context.
9870df7fb74STrond Myklebust  */
9880df7fb74STrond Myklebust static int
9890df7fb74STrond Myklebust gss_destroying_context(struct rpc_cred *cred)
9900df7fb74STrond Myklebust {
9910df7fb74STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
9920df7fb74STrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
9930df7fb74STrond Myklebust 	struct rpc_task *task;
9940df7fb74STrond Myklebust 
9950df7fb74STrond Myklebust 	if (gss_cred->gc_ctx == NULL ||
9966dcd3926SJeff Layton 	    test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
9970df7fb74STrond Myklebust 		return 0;
9980df7fb74STrond Myklebust 
9990df7fb74STrond Myklebust 	gss_cred->gc_ctx->gc_proc = RPC_GSS_PROC_DESTROY;
10000df7fb74STrond Myklebust 	cred->cr_ops = &gss_nullops;
10010df7fb74STrond Myklebust 
10020df7fb74STrond Myklebust 	/* Take a reference to ensure the cred will be destroyed either
10030df7fb74STrond Myklebust 	 * by the RPC call or by the put_rpccred() below */
10040df7fb74STrond Myklebust 	get_rpccred(cred);
10050df7fb74STrond Myklebust 
1006080a1f14STrond Myklebust 	task = rpc_call_null(gss_auth->client, cred, RPC_TASK_ASYNC|RPC_TASK_SOFT);
10070df7fb74STrond Myklebust 	if (!IS_ERR(task))
10080df7fb74STrond Myklebust 		rpc_put_task(task);
10090df7fb74STrond Myklebust 
10100df7fb74STrond Myklebust 	put_rpccred(cred);
10110df7fb74STrond Myklebust 	return 1;
10120df7fb74STrond Myklebust }
10130df7fb74STrond Myklebust 
10140df7fb74STrond Myklebust /* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
10151da177e4SLinus Torvalds  * to create a new cred or context, so they check that things have been
10161da177e4SLinus Torvalds  * allocated before freeing them. */
10171da177e4SLinus Torvalds static void
10185d28dc82STrond Myklebust gss_do_free_ctx(struct gss_cl_ctx *ctx)
10191da177e4SLinus Torvalds {
1020632f0d05SChuck Lever 	dprintk("RPC:       %s\n", __func__);
10211da177e4SLinus Torvalds 
10220d8a3746STrond Myklebust 	gss_delete_sec_context(&ctx->gc_gss_ctx);
10231da177e4SLinus Torvalds 	kfree(ctx->gc_wire_ctx.data);
10241da177e4SLinus Torvalds 	kfree(ctx);
10251da177e4SLinus Torvalds }
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds static void
10285d28dc82STrond Myklebust gss_free_ctx_callback(struct rcu_head *head)
10295d28dc82STrond Myklebust {
10305d28dc82STrond Myklebust 	struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
10315d28dc82STrond Myklebust 	gss_do_free_ctx(ctx);
10325d28dc82STrond Myklebust }
10335d28dc82STrond Myklebust 
10345d28dc82STrond Myklebust static void
10355d28dc82STrond Myklebust gss_free_ctx(struct gss_cl_ctx *ctx)
10365d28dc82STrond Myklebust {
10375d28dc82STrond Myklebust 	call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
10385d28dc82STrond Myklebust }
10395d28dc82STrond Myklebust 
10405d28dc82STrond Myklebust static void
104131be5bf1STrond Myklebust gss_free_cred(struct gss_cred *gss_cred)
10421da177e4SLinus Torvalds {
1043632f0d05SChuck Lever 	dprintk("RPC:       %s cred=%p\n", __func__, gss_cred);
104431be5bf1STrond Myklebust 	kfree(gss_cred);
104531be5bf1STrond Myklebust }
10461da177e4SLinus Torvalds 
104731be5bf1STrond Myklebust static void
104831be5bf1STrond Myklebust gss_free_cred_callback(struct rcu_head *head)
104931be5bf1STrond Myklebust {
105031be5bf1STrond Myklebust 	struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
105131be5bf1STrond Myklebust 	gss_free_cred(gss_cred);
105231be5bf1STrond Myklebust }
10531da177e4SLinus Torvalds 
105431be5bf1STrond Myklebust static void
10556dcd3926SJeff Layton gss_destroy_nullcred(struct rpc_cred *cred)
105631be5bf1STrond Myklebust {
10575d28dc82STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
10580285ed1fSTrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
10595d28dc82STrond Myklebust 	struct gss_cl_ctx *ctx = gss_cred->gc_ctx;
10605d28dc82STrond Myklebust 
1061a9b3cd7fSStephen Hemminger 	RCU_INIT_POINTER(gss_cred->gc_ctx, NULL);
106231be5bf1STrond Myklebust 	call_rcu(&cred->cr_rcu, gss_free_cred_callback);
10635d28dc82STrond Myklebust 	if (ctx)
10645d28dc82STrond Myklebust 		gss_put_ctx(ctx);
10650285ed1fSTrond Myklebust 	kref_put(&gss_auth->kref, gss_free_callback);
10661da177e4SLinus Torvalds }
10671da177e4SLinus Torvalds 
10686dcd3926SJeff Layton static void
10696dcd3926SJeff Layton gss_destroy_cred(struct rpc_cred *cred)
10706dcd3926SJeff Layton {
10716dcd3926SJeff Layton 
10726dcd3926SJeff Layton 	if (gss_destroying_context(cred))
10736dcd3926SJeff Layton 		return;
10746dcd3926SJeff Layton 	gss_destroy_nullcred(cred);
10756dcd3926SJeff Layton }
10766dcd3926SJeff Layton 
10771da177e4SLinus Torvalds /*
10781da177e4SLinus Torvalds  * Lookup RPCSEC_GSS cred for the current process
10791da177e4SLinus Torvalds  */
10801da177e4SLinus Torvalds static struct rpc_cred *
10818a317760STrond Myklebust gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
10821da177e4SLinus Torvalds {
10838a317760STrond Myklebust 	return rpcauth_lookup_credcache(auth, acred, flags);
10841da177e4SLinus Torvalds }
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds static struct rpc_cred *
10878a317760STrond Myklebust gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
10881da177e4SLinus Torvalds {
10891da177e4SLinus Torvalds 	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
10901da177e4SLinus Torvalds 	struct gss_cred	*cred = NULL;
10911da177e4SLinus Torvalds 	int err = -ENOMEM;
10921da177e4SLinus Torvalds 
1093632f0d05SChuck Lever 	dprintk("RPC:       %s for uid %d, flavor %d\n",
1094cdba321eSEric W. Biederman 		__func__, from_kuid(&init_user_ns, acred->uid),
1095cdba321eSEric W. Biederman 		auth->au_flavor);
10961da177e4SLinus Torvalds 
10970f38b873STrond Myklebust 	if (!(cred = kzalloc(sizeof(*cred), GFP_NOFS)))
10981da177e4SLinus Torvalds 		goto out_err;
10991da177e4SLinus Torvalds 
11005fe4755eSTrond Myklebust 	rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
11011da177e4SLinus Torvalds 	/*
11021da177e4SLinus Torvalds 	 * Note: in order to force a call to call_refresh(), we deliberately
11031da177e4SLinus Torvalds 	 * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
11041da177e4SLinus Torvalds 	 */
1105fc432dd9STrond Myklebust 	cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
11061da177e4SLinus Torvalds 	cred->gc_service = gss_auth->service;
110768c97153STrond Myklebust 	cred->gc_principal = NULL;
110868c97153STrond Myklebust 	if (acred->machine_cred)
110968c97153STrond Myklebust 		cred->gc_principal = acred->principal;
11100285ed1fSTrond Myklebust 	kref_get(&gss_auth->kref);
11111da177e4SLinus Torvalds 	return &cred->gc_base;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds out_err:
1114632f0d05SChuck Lever 	dprintk("RPC:       %s failed with error %d\n", __func__, err);
11151da177e4SLinus Torvalds 	return ERR_PTR(err);
11161da177e4SLinus Torvalds }
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds static int
1119fba3bad4STrond Myklebust gss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
1120fba3bad4STrond Myklebust {
1121fba3bad4STrond Myklebust 	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
1122fba3bad4STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
1123fba3bad4STrond Myklebust 	int err;
1124fba3bad4STrond Myklebust 
1125fba3bad4STrond Myklebust 	do {
1126fba3bad4STrond Myklebust 		err = gss_create_upcall(gss_auth, gss_cred);
1127fba3bad4STrond Myklebust 	} while (err == -EAGAIN);
1128fba3bad4STrond Myklebust 	return err;
1129fba3bad4STrond Myklebust }
1130fba3bad4STrond Myklebust 
1131fba3bad4STrond Myklebust static int
11328a317760STrond Myklebust gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
11331da177e4SLinus Torvalds {
11341da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
11351da177e4SLinus Torvalds 
1136cd019f75STrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
11378a317760STrond Myklebust 		goto out;
11381da177e4SLinus Torvalds 	/* Don't match with creds that have expired. */
1139cd019f75STrond Myklebust 	if (time_after(jiffies, gss_cred->gc_ctx->gc_expiry))
1140cd019f75STrond Myklebust 		return 0;
1141cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags))
11421da177e4SLinus Torvalds 		return 0;
11438a317760STrond Myklebust out:
114468c97153STrond Myklebust 	if (acred->principal != NULL) {
114568c97153STrond Myklebust 		if (gss_cred->gc_principal == NULL)
114668c97153STrond Myklebust 			return 0;
114768c97153STrond Myklebust 		return strcmp(acred->principal, gss_cred->gc_principal) == 0;
114868c97153STrond Myklebust 	}
114968c97153STrond Myklebust 	if (gss_cred->gc_principal != NULL)
11507c67db3aSTrond Myklebust 		return 0;
11510b4d51b0SEric W. Biederman 	return uid_eq(rc->cr_uid, acred->uid);
11521da177e4SLinus Torvalds }
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds /*
11551da177e4SLinus Torvalds * Marshal credentials.
11561da177e4SLinus Torvalds * Maybe we should keep a cached credential for performance reasons.
11571da177e4SLinus Torvalds */
1158d8ed029dSAlexey Dobriyan static __be32 *
1159d8ed029dSAlexey Dobriyan gss_marshal(struct rpc_task *task, __be32 *p)
11601da177e4SLinus Torvalds {
1161a17c2153STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1162a17c2153STrond Myklebust 	struct rpc_cred *cred = req->rq_cred;
11631da177e4SLinus Torvalds 	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
11641da177e4SLinus Torvalds 						 gc_base);
11651da177e4SLinus Torvalds 	struct gss_cl_ctx	*ctx = gss_cred_get_ctx(cred);
1166d8ed029dSAlexey Dobriyan 	__be32		*cred_len;
11671da177e4SLinus Torvalds 	u32             maj_stat = 0;
11681da177e4SLinus Torvalds 	struct xdr_netobj mic;
11691da177e4SLinus Torvalds 	struct kvec	iov;
11701da177e4SLinus Torvalds 	struct xdr_buf	verf_buf;
11711da177e4SLinus Torvalds 
1172632f0d05SChuck Lever 	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	*p++ = htonl(RPC_AUTH_GSS);
11751da177e4SLinus Torvalds 	cred_len = p++;
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	spin_lock(&ctx->gc_seq_lock);
11781da177e4SLinus Torvalds 	req->rq_seqno = ctx->gc_seq++;
11791da177e4SLinus Torvalds 	spin_unlock(&ctx->gc_seq_lock);
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	*p++ = htonl((u32) RPC_GSS_VERSION);
11821da177e4SLinus Torvalds 	*p++ = htonl((u32) ctx->gc_proc);
11831da177e4SLinus Torvalds 	*p++ = htonl((u32) req->rq_seqno);
11841da177e4SLinus Torvalds 	*p++ = htonl((u32) gss_cred->gc_service);
11851da177e4SLinus Torvalds 	p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
11861da177e4SLinus Torvalds 	*cred_len = htonl((p - (cred_len + 1)) << 2);
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	/* We compute the checksum for the verifier over the xdr-encoded bytes
11891da177e4SLinus Torvalds 	 * starting with the xid and ending at the end of the credential: */
1190a4f0835cSTrond Myklebust 	iov.iov_base = xprt_skip_transport_header(req->rq_xprt,
1191808012fbSChuck Lever 					req->rq_snd_buf.head[0].iov_base);
11921da177e4SLinus Torvalds 	iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
11931da177e4SLinus Torvalds 	xdr_buf_from_iov(&iov, &verf_buf);
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	/* set verifier flavor*/
11961da177e4SLinus Torvalds 	*p++ = htonl(RPC_AUTH_GSS);
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 	mic.data = (u8 *)(p + 1);
119900fd6e14SJ. Bruce Fields 	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
12001da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
1201fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
12021da177e4SLinus Torvalds 	} else if (maj_stat != 0) {
12031da177e4SLinus Torvalds 		printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
12041da177e4SLinus Torvalds 		goto out_put_ctx;
12051da177e4SLinus Torvalds 	}
12061da177e4SLinus Torvalds 	p = xdr_encode_opaque(p, NULL, mic.len);
12071da177e4SLinus Torvalds 	gss_put_ctx(ctx);
12081da177e4SLinus Torvalds 	return p;
12091da177e4SLinus Torvalds out_put_ctx:
12101da177e4SLinus Torvalds 	gss_put_ctx(ctx);
12111da177e4SLinus Torvalds 	return NULL;
12121da177e4SLinus Torvalds }
12131da177e4SLinus Torvalds 
1214cd019f75STrond Myklebust static int gss_renew_cred(struct rpc_task *task)
1215cd019f75STrond Myklebust {
1216a17c2153STrond Myklebust 	struct rpc_cred *oldcred = task->tk_rqstp->rq_cred;
1217cd019f75STrond Myklebust 	struct gss_cred *gss_cred = container_of(oldcred,
1218cd019f75STrond Myklebust 						 struct gss_cred,
1219cd019f75STrond Myklebust 						 gc_base);
1220cd019f75STrond Myklebust 	struct rpc_auth *auth = oldcred->cr_auth;
1221cd019f75STrond Myklebust 	struct auth_cred acred = {
1222cd019f75STrond Myklebust 		.uid = oldcred->cr_uid,
122368c97153STrond Myklebust 		.principal = gss_cred->gc_principal,
122468c97153STrond Myklebust 		.machine_cred = (gss_cred->gc_principal != NULL ? 1 : 0),
1225cd019f75STrond Myklebust 	};
1226cd019f75STrond Myklebust 	struct rpc_cred *new;
1227cd019f75STrond Myklebust 
1228cd019f75STrond Myklebust 	new = gss_lookup_cred(auth, &acred, RPCAUTH_LOOKUP_NEW);
1229cd019f75STrond Myklebust 	if (IS_ERR(new))
1230cd019f75STrond Myklebust 		return PTR_ERR(new);
1231a17c2153STrond Myklebust 	task->tk_rqstp->rq_cred = new;
1232cd019f75STrond Myklebust 	put_rpccred(oldcred);
1233cd019f75STrond Myklebust 	return 0;
1234cd019f75STrond Myklebust }
1235cd019f75STrond Myklebust 
1236126e216aSTrond Myklebust static int gss_cred_is_negative_entry(struct rpc_cred *cred)
1237126e216aSTrond Myklebust {
1238126e216aSTrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEGATIVE, &cred->cr_flags)) {
1239126e216aSTrond Myklebust 		unsigned long now = jiffies;
1240126e216aSTrond Myklebust 		unsigned long begin, expire;
1241126e216aSTrond Myklebust 		struct gss_cred *gss_cred;
1242126e216aSTrond Myklebust 
1243126e216aSTrond Myklebust 		gss_cred = container_of(cred, struct gss_cred, gc_base);
1244126e216aSTrond Myklebust 		begin = gss_cred->gc_upcall_timestamp;
1245126e216aSTrond Myklebust 		expire = begin + gss_expired_cred_retry_delay * HZ;
1246126e216aSTrond Myklebust 
1247126e216aSTrond Myklebust 		if (time_in_range_open(now, begin, expire))
1248126e216aSTrond Myklebust 			return 1;
1249126e216aSTrond Myklebust 	}
1250126e216aSTrond Myklebust 	return 0;
1251126e216aSTrond Myklebust }
1252126e216aSTrond Myklebust 
12531da177e4SLinus Torvalds /*
12541da177e4SLinus Torvalds * Refresh credentials. XXX - finish
12551da177e4SLinus Torvalds */
12561da177e4SLinus Torvalds static int
12571da177e4SLinus Torvalds gss_refresh(struct rpc_task *task)
12581da177e4SLinus Torvalds {
1259a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
1260cd019f75STrond Myklebust 	int ret = 0;
12611da177e4SLinus Torvalds 
1262126e216aSTrond Myklebust 	if (gss_cred_is_negative_entry(cred))
1263126e216aSTrond Myklebust 		return -EKEYEXPIRED;
1264126e216aSTrond Myklebust 
1265cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
1266cd019f75STrond Myklebust 			!test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags)) {
1267cd019f75STrond Myklebust 		ret = gss_renew_cred(task);
1268cd019f75STrond Myklebust 		if (ret < 0)
1269cd019f75STrond Myklebust 			goto out;
1270a17c2153STrond Myklebust 		cred = task->tk_rqstp->rq_cred;
1271cd019f75STrond Myklebust 	}
1272cd019f75STrond Myklebust 
1273cd019f75STrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
1274cd019f75STrond Myklebust 		ret = gss_refresh_upcall(task);
1275cd019f75STrond Myklebust out:
1276cd019f75STrond Myklebust 	return ret;
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds 
12790df7fb74STrond Myklebust /* Dummy refresh routine: used only when destroying the context */
12800df7fb74STrond Myklebust static int
12810df7fb74STrond Myklebust gss_refresh_null(struct rpc_task *task)
12820df7fb74STrond Myklebust {
12830df7fb74STrond Myklebust 	return -EACCES;
12840df7fb74STrond Myklebust }
12850df7fb74STrond Myklebust 
1286d8ed029dSAlexey Dobriyan static __be32 *
1287d8ed029dSAlexey Dobriyan gss_validate(struct rpc_task *task, __be32 *p)
12881da177e4SLinus Torvalds {
1289a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
12901da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1291d8ed029dSAlexey Dobriyan 	__be32		seq;
12921da177e4SLinus Torvalds 	struct kvec	iov;
12931da177e4SLinus Torvalds 	struct xdr_buf	verf_buf;
12941da177e4SLinus Torvalds 	struct xdr_netobj mic;
12951da177e4SLinus Torvalds 	u32		flav,len;
12961da177e4SLinus Torvalds 	u32		maj_stat;
12971da177e4SLinus Torvalds 
1298632f0d05SChuck Lever 	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 	flav = ntohl(*p++);
13011da177e4SLinus Torvalds 	if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
13021da177e4SLinus Torvalds 		goto out_bad;
13031da177e4SLinus Torvalds 	if (flav != RPC_AUTH_GSS)
13041da177e4SLinus Torvalds 		goto out_bad;
13051da177e4SLinus Torvalds 	seq = htonl(task->tk_rqstp->rq_seqno);
13061da177e4SLinus Torvalds 	iov.iov_base = &seq;
13071da177e4SLinus Torvalds 	iov.iov_len = sizeof(seq);
13081da177e4SLinus Torvalds 	xdr_buf_from_iov(&iov, &verf_buf);
13091da177e4SLinus Torvalds 	mic.data = (u8 *)p;
13101da177e4SLinus Torvalds 	mic.len = len;
13111da177e4SLinus Torvalds 
131200fd6e14SJ. Bruce Fields 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
13131da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1314fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
13150df7fb74STrond Myklebust 	if (maj_stat) {
1316632f0d05SChuck Lever 		dprintk("RPC: %5u %s: gss_verify_mic returned error 0x%08x\n",
1317632f0d05SChuck Lever 			task->tk_pid, __func__, maj_stat);
13181da177e4SLinus Torvalds 		goto out_bad;
13190df7fb74STrond Myklebust 	}
132024b2605bSJ. Bruce Fields 	/* We leave it to unwrap to calculate au_rslack. For now we just
132124b2605bSJ. Bruce Fields 	 * calculate the length of the verifier: */
13221be27f36STrond Myklebust 	cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
13231da177e4SLinus Torvalds 	gss_put_ctx(ctx);
1324632f0d05SChuck Lever 	dprintk("RPC: %5u %s: gss_verify_mic succeeded.\n",
1325632f0d05SChuck Lever 			task->tk_pid, __func__);
13261da177e4SLinus Torvalds 	return p + XDR_QUADLEN(len);
13271da177e4SLinus Torvalds out_bad:
13281da177e4SLinus Torvalds 	gss_put_ctx(ctx);
1329632f0d05SChuck Lever 	dprintk("RPC: %5u %s failed.\n", task->tk_pid, __func__);
13301da177e4SLinus Torvalds 	return NULL;
13311da177e4SLinus Torvalds }
13321da177e4SLinus Torvalds 
13339f06c719SChuck Lever static void gss_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
13349f06c719SChuck Lever 				__be32 *p, void *obj)
13359f06c719SChuck Lever {
13369f06c719SChuck Lever 	struct xdr_stream xdr;
13379f06c719SChuck Lever 
13389f06c719SChuck Lever 	xdr_init_encode(&xdr, &rqstp->rq_snd_buf, p);
13399f06c719SChuck Lever 	encode(rqstp, &xdr, obj);
13409f06c719SChuck Lever }
13419f06c719SChuck Lever 
13421da177e4SLinus Torvalds static inline int
13431da177e4SLinus Torvalds gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
13449f06c719SChuck Lever 		   kxdreproc_t encode, struct rpc_rqst *rqstp,
13459f06c719SChuck Lever 		   __be32 *p, void *obj)
13461da177e4SLinus Torvalds {
13471da177e4SLinus Torvalds 	struct xdr_buf	*snd_buf = &rqstp->rq_snd_buf;
13481da177e4SLinus Torvalds 	struct xdr_buf	integ_buf;
1349d8ed029dSAlexey Dobriyan 	__be32          *integ_len = NULL;
13501da177e4SLinus Torvalds 	struct xdr_netobj mic;
1351d8ed029dSAlexey Dobriyan 	u32		offset;
1352d8ed029dSAlexey Dobriyan 	__be32		*q;
13531da177e4SLinus Torvalds 	struct kvec	*iov;
13541da177e4SLinus Torvalds 	u32             maj_stat = 0;
13551da177e4SLinus Torvalds 	int		status = -EIO;
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	integ_len = p++;
13581da177e4SLinus Torvalds 	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
13591da177e4SLinus Torvalds 	*p++ = htonl(rqstp->rq_seqno);
13601da177e4SLinus Torvalds 
13619f06c719SChuck Lever 	gss_wrap_req_encode(encode, rqstp, p, obj);
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds 	if (xdr_buf_subsegment(snd_buf, &integ_buf,
13641da177e4SLinus Torvalds 				offset, snd_buf->len - offset))
13651da177e4SLinus Torvalds 		return status;
13661da177e4SLinus Torvalds 	*integ_len = htonl(integ_buf.len);
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	/* guess whether we're in the head or the tail: */
13691da177e4SLinus Torvalds 	if (snd_buf->page_len || snd_buf->tail[0].iov_len)
13701da177e4SLinus Torvalds 		iov = snd_buf->tail;
13711da177e4SLinus Torvalds 	else
13721da177e4SLinus Torvalds 		iov = snd_buf->head;
13731da177e4SLinus Torvalds 	p = iov->iov_base + iov->iov_len;
13741da177e4SLinus Torvalds 	mic.data = (u8 *)(p + 1);
13751da177e4SLinus Torvalds 
137600fd6e14SJ. Bruce Fields 	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
13771da177e4SLinus Torvalds 	status = -EIO; /* XXX? */
13781da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1379fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
13801da177e4SLinus Torvalds 	else if (maj_stat)
13811da177e4SLinus Torvalds 		return status;
13821da177e4SLinus Torvalds 	q = xdr_encode_opaque(p, NULL, mic.len);
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 	offset = (u8 *)q - (u8 *)p;
13851da177e4SLinus Torvalds 	iov->iov_len += offset;
13861da177e4SLinus Torvalds 	snd_buf->len += offset;
13871da177e4SLinus Torvalds 	return 0;
13881da177e4SLinus Torvalds }
13891da177e4SLinus Torvalds 
13902d2da60cSJ. Bruce Fields static void
13912d2da60cSJ. Bruce Fields priv_release_snd_buf(struct rpc_rqst *rqstp)
13922d2da60cSJ. Bruce Fields {
13932d2da60cSJ. Bruce Fields 	int i;
13942d2da60cSJ. Bruce Fields 
13952d2da60cSJ. Bruce Fields 	for (i=0; i < rqstp->rq_enc_pages_num; i++)
13962d2da60cSJ. Bruce Fields 		__free_page(rqstp->rq_enc_pages[i]);
13972d2da60cSJ. Bruce Fields 	kfree(rqstp->rq_enc_pages);
13982d2da60cSJ. Bruce Fields }
13992d2da60cSJ. Bruce Fields 
14002d2da60cSJ. Bruce Fields static int
14012d2da60cSJ. Bruce Fields alloc_enc_pages(struct rpc_rqst *rqstp)
14022d2da60cSJ. Bruce Fields {
14032d2da60cSJ. Bruce Fields 	struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
14042d2da60cSJ. Bruce Fields 	int first, last, i;
14052d2da60cSJ. Bruce Fields 
14062d2da60cSJ. Bruce Fields 	if (snd_buf->page_len == 0) {
14072d2da60cSJ. Bruce Fields 		rqstp->rq_enc_pages_num = 0;
14082d2da60cSJ. Bruce Fields 		return 0;
14092d2da60cSJ. Bruce Fields 	}
14102d2da60cSJ. Bruce Fields 
14112d2da60cSJ. Bruce Fields 	first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
14122d2da60cSJ. Bruce Fields 	last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_CACHE_SHIFT;
14132d2da60cSJ. Bruce Fields 	rqstp->rq_enc_pages_num = last - first + 1 + 1;
14142d2da60cSJ. Bruce Fields 	rqstp->rq_enc_pages
14152d2da60cSJ. Bruce Fields 		= kmalloc(rqstp->rq_enc_pages_num * sizeof(struct page *),
14162d2da60cSJ. Bruce Fields 				GFP_NOFS);
14172d2da60cSJ. Bruce Fields 	if (!rqstp->rq_enc_pages)
14182d2da60cSJ. Bruce Fields 		goto out;
14192d2da60cSJ. Bruce Fields 	for (i=0; i < rqstp->rq_enc_pages_num; i++) {
14202d2da60cSJ. Bruce Fields 		rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
14212d2da60cSJ. Bruce Fields 		if (rqstp->rq_enc_pages[i] == NULL)
14222d2da60cSJ. Bruce Fields 			goto out_free;
14232d2da60cSJ. Bruce Fields 	}
14242d2da60cSJ. Bruce Fields 	rqstp->rq_release_snd_buf = priv_release_snd_buf;
14252d2da60cSJ. Bruce Fields 	return 0;
14262d2da60cSJ. Bruce Fields out_free:
1427cdead7cfSTrond Myklebust 	rqstp->rq_enc_pages_num = i;
1428cdead7cfSTrond Myklebust 	priv_release_snd_buf(rqstp);
14292d2da60cSJ. Bruce Fields out:
14302d2da60cSJ. Bruce Fields 	return -EAGAIN;
14312d2da60cSJ. Bruce Fields }
14322d2da60cSJ. Bruce Fields 
14332d2da60cSJ. Bruce Fields static inline int
14342d2da60cSJ. Bruce Fields gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
14359f06c719SChuck Lever 		  kxdreproc_t encode, struct rpc_rqst *rqstp,
14369f06c719SChuck Lever 		  __be32 *p, void *obj)
14372d2da60cSJ. Bruce Fields {
14382d2da60cSJ. Bruce Fields 	struct xdr_buf	*snd_buf = &rqstp->rq_snd_buf;
14392d2da60cSJ. Bruce Fields 	u32		offset;
14402d2da60cSJ. Bruce Fields 	u32             maj_stat;
14412d2da60cSJ. Bruce Fields 	int		status;
1442d8ed029dSAlexey Dobriyan 	__be32		*opaque_len;
14432d2da60cSJ. Bruce Fields 	struct page	**inpages;
14442d2da60cSJ. Bruce Fields 	int		first;
14452d2da60cSJ. Bruce Fields 	int		pad;
14462d2da60cSJ. Bruce Fields 	struct kvec	*iov;
14472d2da60cSJ. Bruce Fields 	char		*tmp;
14482d2da60cSJ. Bruce Fields 
14492d2da60cSJ. Bruce Fields 	opaque_len = p++;
14502d2da60cSJ. Bruce Fields 	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
14512d2da60cSJ. Bruce Fields 	*p++ = htonl(rqstp->rq_seqno);
14522d2da60cSJ. Bruce Fields 
14539f06c719SChuck Lever 	gss_wrap_req_encode(encode, rqstp, p, obj);
14542d2da60cSJ. Bruce Fields 
14552d2da60cSJ. Bruce Fields 	status = alloc_enc_pages(rqstp);
14562d2da60cSJ. Bruce Fields 	if (status)
14572d2da60cSJ. Bruce Fields 		return status;
14582d2da60cSJ. Bruce Fields 	first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
14592d2da60cSJ. Bruce Fields 	inpages = snd_buf->pages + first;
14602d2da60cSJ. Bruce Fields 	snd_buf->pages = rqstp->rq_enc_pages;
14612d2da60cSJ. Bruce Fields 	snd_buf->page_base -= first << PAGE_CACHE_SHIFT;
14627561042fSKevin Coffman 	/*
14637561042fSKevin Coffman 	 * Give the tail its own page, in case we need extra space in the
14647561042fSKevin Coffman 	 * head when wrapping:
14657561042fSKevin Coffman 	 *
14667561042fSKevin Coffman 	 * call_allocate() allocates twice the slack space required
14677561042fSKevin Coffman 	 * by the authentication flavor to rq_callsize.
14687561042fSKevin Coffman 	 * For GSS, slack is GSS_CRED_SLACK.
14697561042fSKevin Coffman 	 */
14702d2da60cSJ. Bruce Fields 	if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
14712d2da60cSJ. Bruce Fields 		tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
14722d2da60cSJ. Bruce Fields 		memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
14732d2da60cSJ. Bruce Fields 		snd_buf->tail[0].iov_base = tmp;
14742d2da60cSJ. Bruce Fields 	}
147500fd6e14SJ. Bruce Fields 	maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
14767561042fSKevin Coffman 	/* slack space should prevent this ever happening: */
14772d2da60cSJ. Bruce Fields 	BUG_ON(snd_buf->len > snd_buf->buflen);
14782d2da60cSJ. Bruce Fields 	status = -EIO;
14792d2da60cSJ. Bruce Fields 	/* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
14802d2da60cSJ. Bruce Fields 	 * done anyway, so it's safe to put the request on the wire: */
14812d2da60cSJ. Bruce Fields 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1482fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
14832d2da60cSJ. Bruce Fields 	else if (maj_stat)
14842d2da60cSJ. Bruce Fields 		return status;
14852d2da60cSJ. Bruce Fields 
14862d2da60cSJ. Bruce Fields 	*opaque_len = htonl(snd_buf->len - offset);
14872d2da60cSJ. Bruce Fields 	/* guess whether we're in the head or the tail: */
14882d2da60cSJ. Bruce Fields 	if (snd_buf->page_len || snd_buf->tail[0].iov_len)
14892d2da60cSJ. Bruce Fields 		iov = snd_buf->tail;
14902d2da60cSJ. Bruce Fields 	else
14912d2da60cSJ. Bruce Fields 		iov = snd_buf->head;
14922d2da60cSJ. Bruce Fields 	p = iov->iov_base + iov->iov_len;
14932d2da60cSJ. Bruce Fields 	pad = 3 - ((snd_buf->len - offset - 1) & 3);
14942d2da60cSJ. Bruce Fields 	memset(p, 0, pad);
14952d2da60cSJ. Bruce Fields 	iov->iov_len += pad;
14962d2da60cSJ. Bruce Fields 	snd_buf->len += pad;
14972d2da60cSJ. Bruce Fields 
14982d2da60cSJ. Bruce Fields 	return 0;
14992d2da60cSJ. Bruce Fields }
15002d2da60cSJ. Bruce Fields 
15011da177e4SLinus Torvalds static int
15021da177e4SLinus Torvalds gss_wrap_req(struct rpc_task *task,
15039f06c719SChuck Lever 	     kxdreproc_t encode, void *rqstp, __be32 *p, void *obj)
15041da177e4SLinus Torvalds {
1505a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
15061da177e4SLinus Torvalds 	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
15071da177e4SLinus Torvalds 			gc_base);
15081da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
15091da177e4SLinus Torvalds 	int             status = -EIO;
15101da177e4SLinus Torvalds 
1511632f0d05SChuck Lever 	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
15121da177e4SLinus Torvalds 	if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
15131da177e4SLinus Torvalds 		/* The spec seems a little ambiguous here, but I think that not
15141da177e4SLinus Torvalds 		 * wrapping context destruction requests makes the most sense.
15151da177e4SLinus Torvalds 		 */
15169f06c719SChuck Lever 		gss_wrap_req_encode(encode, rqstp, p, obj);
15179f06c719SChuck Lever 		status = 0;
15181da177e4SLinus Torvalds 		goto out;
15191da177e4SLinus Torvalds 	}
15201da177e4SLinus Torvalds 	switch (gss_cred->gc_service) {
15211da177e4SLinus Torvalds 	case RPC_GSS_SVC_NONE:
15229f06c719SChuck Lever 		gss_wrap_req_encode(encode, rqstp, p, obj);
15239f06c719SChuck Lever 		status = 0;
15241da177e4SLinus Torvalds 		break;
15251da177e4SLinus Torvalds 	case RPC_GSS_SVC_INTEGRITY:
152689f0e4feSJoe Perches 		status = gss_wrap_req_integ(cred, ctx, encode, rqstp, p, obj);
15271da177e4SLinus Torvalds 		break;
15281da177e4SLinus Torvalds 	case RPC_GSS_SVC_PRIVACY:
152989f0e4feSJoe Perches 		status = gss_wrap_req_priv(cred, ctx, encode, rqstp, p, obj);
15301da177e4SLinus Torvalds 		break;
15311da177e4SLinus Torvalds 	}
15321da177e4SLinus Torvalds out:
15331da177e4SLinus Torvalds 	gss_put_ctx(ctx);
1534632f0d05SChuck Lever 	dprintk("RPC: %5u %s returning %d\n", task->tk_pid, __func__, status);
15351da177e4SLinus Torvalds 	return status;
15361da177e4SLinus Torvalds }
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds static inline int
15391da177e4SLinus Torvalds gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1540d8ed029dSAlexey Dobriyan 		struct rpc_rqst *rqstp, __be32 **p)
15411da177e4SLinus Torvalds {
15421da177e4SLinus Torvalds 	struct xdr_buf	*rcv_buf = &rqstp->rq_rcv_buf;
15431da177e4SLinus Torvalds 	struct xdr_buf integ_buf;
15441da177e4SLinus Torvalds 	struct xdr_netobj mic;
15451da177e4SLinus Torvalds 	u32 data_offset, mic_offset;
15461da177e4SLinus Torvalds 	u32 integ_len;
15471da177e4SLinus Torvalds 	u32 maj_stat;
15481da177e4SLinus Torvalds 	int status = -EIO;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 	integ_len = ntohl(*(*p)++);
15511da177e4SLinus Torvalds 	if (integ_len & 3)
15521da177e4SLinus Torvalds 		return status;
15531da177e4SLinus Torvalds 	data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
15541da177e4SLinus Torvalds 	mic_offset = integ_len + data_offset;
15551da177e4SLinus Torvalds 	if (mic_offset > rcv_buf->len)
15561da177e4SLinus Torvalds 		return status;
15571da177e4SLinus Torvalds 	if (ntohl(*(*p)++) != rqstp->rq_seqno)
15581da177e4SLinus Torvalds 		return status;
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
15611da177e4SLinus Torvalds 				mic_offset - data_offset))
15621da177e4SLinus Torvalds 		return status;
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds 	if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
15651da177e4SLinus Torvalds 		return status;
15661da177e4SLinus Torvalds 
156700fd6e14SJ. Bruce Fields 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
15681da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1569fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
15701da177e4SLinus Torvalds 	if (maj_stat != GSS_S_COMPLETE)
15711da177e4SLinus Torvalds 		return status;
15721da177e4SLinus Torvalds 	return 0;
15731da177e4SLinus Torvalds }
15741da177e4SLinus Torvalds 
15752d2da60cSJ. Bruce Fields static inline int
15762d2da60cSJ. Bruce Fields gss_unwrap_resp_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1577d8ed029dSAlexey Dobriyan 		struct rpc_rqst *rqstp, __be32 **p)
15782d2da60cSJ. Bruce Fields {
15792d2da60cSJ. Bruce Fields 	struct xdr_buf  *rcv_buf = &rqstp->rq_rcv_buf;
15802d2da60cSJ. Bruce Fields 	u32 offset;
15812d2da60cSJ. Bruce Fields 	u32 opaque_len;
15822d2da60cSJ. Bruce Fields 	u32 maj_stat;
15832d2da60cSJ. Bruce Fields 	int status = -EIO;
15842d2da60cSJ. Bruce Fields 
15852d2da60cSJ. Bruce Fields 	opaque_len = ntohl(*(*p)++);
15862d2da60cSJ. Bruce Fields 	offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
15872d2da60cSJ. Bruce Fields 	if (offset + opaque_len > rcv_buf->len)
15882d2da60cSJ. Bruce Fields 		return status;
15892d2da60cSJ. Bruce Fields 	/* remove padding: */
15902d2da60cSJ. Bruce Fields 	rcv_buf->len = offset + opaque_len;
15912d2da60cSJ. Bruce Fields 
159200fd6e14SJ. Bruce Fields 	maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
15932d2da60cSJ. Bruce Fields 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1594fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
15952d2da60cSJ. Bruce Fields 	if (maj_stat != GSS_S_COMPLETE)
15962d2da60cSJ. Bruce Fields 		return status;
15972d2da60cSJ. Bruce Fields 	if (ntohl(*(*p)++) != rqstp->rq_seqno)
15982d2da60cSJ. Bruce Fields 		return status;
15992d2da60cSJ. Bruce Fields 
16002d2da60cSJ. Bruce Fields 	return 0;
16012d2da60cSJ. Bruce Fields }
16022d2da60cSJ. Bruce Fields 
1603bf269551SChuck Lever static int
1604bf269551SChuck Lever gss_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
1605bf269551SChuck Lever 		      __be32 *p, void *obj)
1606bf269551SChuck Lever {
1607bf269551SChuck Lever 	struct xdr_stream xdr;
1608bf269551SChuck Lever 
1609bf269551SChuck Lever 	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
1610bf269551SChuck Lever 	return decode(rqstp, &xdr, obj);
1611bf269551SChuck Lever }
16122d2da60cSJ. Bruce Fields 
16131da177e4SLinus Torvalds static int
16141da177e4SLinus Torvalds gss_unwrap_resp(struct rpc_task *task,
1615bf269551SChuck Lever 		kxdrdproc_t decode, void *rqstp, __be32 *p, void *obj)
16161da177e4SLinus Torvalds {
1617a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
16181da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
16191da177e4SLinus Torvalds 			gc_base);
16201da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1621d8ed029dSAlexey Dobriyan 	__be32		*savedp = p;
16222d2da60cSJ. Bruce Fields 	struct kvec	*head = ((struct rpc_rqst *)rqstp)->rq_rcv_buf.head;
16232d2da60cSJ. Bruce Fields 	int		savedlen = head->iov_len;
16241da177e4SLinus Torvalds 	int             status = -EIO;
16251da177e4SLinus Torvalds 
16261da177e4SLinus Torvalds 	if (ctx->gc_proc != RPC_GSS_PROC_DATA)
16271da177e4SLinus Torvalds 		goto out_decode;
16281da177e4SLinus Torvalds 	switch (gss_cred->gc_service) {
16291da177e4SLinus Torvalds 	case RPC_GSS_SVC_NONE:
16301da177e4SLinus Torvalds 		break;
16311da177e4SLinus Torvalds 	case RPC_GSS_SVC_INTEGRITY:
16321da177e4SLinus Torvalds 		status = gss_unwrap_resp_integ(cred, ctx, rqstp, &p);
16331da177e4SLinus Torvalds 		if (status)
16341da177e4SLinus Torvalds 			goto out;
16351da177e4SLinus Torvalds 		break;
16361da177e4SLinus Torvalds 	case RPC_GSS_SVC_PRIVACY:
16372d2da60cSJ. Bruce Fields 		status = gss_unwrap_resp_priv(cred, ctx, rqstp, &p);
16382d2da60cSJ. Bruce Fields 		if (status)
16392d2da60cSJ. Bruce Fields 			goto out;
16401da177e4SLinus Torvalds 		break;
16411da177e4SLinus Torvalds 	}
164224b2605bSJ. Bruce Fields 	/* take into account extra slack for integrity and privacy cases: */
16431be27f36STrond Myklebust 	cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + (p - savedp)
16442d2da60cSJ. Bruce Fields 						+ (savedlen - head->iov_len);
16451da177e4SLinus Torvalds out_decode:
1646bf269551SChuck Lever 	status = gss_unwrap_req_decode(decode, rqstp, p, obj);
16471da177e4SLinus Torvalds out:
16481da177e4SLinus Torvalds 	gss_put_ctx(ctx);
1649632f0d05SChuck Lever 	dprintk("RPC: %5u %s returning %d\n",
1650632f0d05SChuck Lever 		task->tk_pid, __func__, status);
16511da177e4SLinus Torvalds 	return status;
16521da177e4SLinus Torvalds }
16531da177e4SLinus Torvalds 
1654f1c0a861STrond Myklebust static const struct rpc_authops authgss_ops = {
16551da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
16561da177e4SLinus Torvalds 	.au_flavor	= RPC_AUTH_GSS,
16571da177e4SLinus Torvalds 	.au_name	= "RPCSEC_GSS",
16581da177e4SLinus Torvalds 	.create		= gss_create,
16591da177e4SLinus Torvalds 	.destroy	= gss_destroy,
16601da177e4SLinus Torvalds 	.lookup_cred	= gss_lookup_cred,
166180df9d20SStanislav Kinsbursky 	.crcreate	= gss_create_cred,
166280df9d20SStanislav Kinsbursky 	.pipes_create	= gss_pipes_dentries_create,
166380df9d20SStanislav Kinsbursky 	.pipes_destroy	= gss_pipes_dentries_destroy,
16646a1a1e34SChuck Lever 	.list_pseudoflavors = gss_mech_list_pseudoflavors,
16659568c5e9SChuck Lever 	.info2flavor	= gss_mech_info2flavor,
1666a77c806fSChuck Lever 	.flavor2info	= gss_mech_flavor2info,
16671da177e4SLinus Torvalds };
16681da177e4SLinus Torvalds 
1669f1c0a861STrond Myklebust static const struct rpc_credops gss_credops = {
16701da177e4SLinus Torvalds 	.cr_name	= "AUTH_GSS",
16711da177e4SLinus Torvalds 	.crdestroy	= gss_destroy_cred,
1672fba3bad4STrond Myklebust 	.cr_init	= gss_cred_init,
16735c691044STrond Myklebust 	.crbind		= rpcauth_generic_bind_cred,
16741da177e4SLinus Torvalds 	.crmatch	= gss_match,
16751da177e4SLinus Torvalds 	.crmarshal	= gss_marshal,
16761da177e4SLinus Torvalds 	.crrefresh	= gss_refresh,
16771da177e4SLinus Torvalds 	.crvalidate	= gss_validate,
16781da177e4SLinus Torvalds 	.crwrap_req	= gss_wrap_req,
16791da177e4SLinus Torvalds 	.crunwrap_resp	= gss_unwrap_resp,
16801da177e4SLinus Torvalds };
16811da177e4SLinus Torvalds 
16820df7fb74STrond Myklebust static const struct rpc_credops gss_nullops = {
16830df7fb74STrond Myklebust 	.cr_name	= "AUTH_GSS",
16846dcd3926SJeff Layton 	.crdestroy	= gss_destroy_nullcred,
16855c691044STrond Myklebust 	.crbind		= rpcauth_generic_bind_cred,
16860df7fb74STrond Myklebust 	.crmatch	= gss_match,
16870df7fb74STrond Myklebust 	.crmarshal	= gss_marshal,
16880df7fb74STrond Myklebust 	.crrefresh	= gss_refresh_null,
16890df7fb74STrond Myklebust 	.crvalidate	= gss_validate,
16900df7fb74STrond Myklebust 	.crwrap_req	= gss_wrap_req,
16910df7fb74STrond Myklebust 	.crunwrap_resp	= gss_unwrap_resp,
16920df7fb74STrond Myklebust };
16930df7fb74STrond Myklebust 
1694b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v0 = {
1695c1225158SPeng Tao 	.upcall		= rpc_pipe_generic_upcall,
16961da177e4SLinus Torvalds 	.downcall	= gss_pipe_downcall,
16971da177e4SLinus Torvalds 	.destroy_msg	= gss_pipe_destroy_msg,
169834769fc4S\"J. Bruce Fields\ 	.open_pipe	= gss_pipe_open_v0,
169934769fc4S\"J. Bruce Fields\ 	.release_pipe	= gss_pipe_release,
170034769fc4S\"J. Bruce Fields\ };
170134769fc4S\"J. Bruce Fields\ 
1702b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v1 = {
1703c1225158SPeng Tao 	.upcall		= rpc_pipe_generic_upcall,
170434769fc4S\"J. Bruce Fields\ 	.downcall	= gss_pipe_downcall,
170534769fc4S\"J. Bruce Fields\ 	.destroy_msg	= gss_pipe_destroy_msg,
170634769fc4S\"J. Bruce Fields\ 	.open_pipe	= gss_pipe_open_v1,
17071da177e4SLinus Torvalds 	.release_pipe	= gss_pipe_release,
17081da177e4SLinus Torvalds };
17091da177e4SLinus Torvalds 
1710a1db410dSStanislav Kinsbursky static __net_init int rpcsec_gss_init_net(struct net *net)
1711a1db410dSStanislav Kinsbursky {
1712a1db410dSStanislav Kinsbursky 	return gss_svc_init_net(net);
1713a1db410dSStanislav Kinsbursky }
1714a1db410dSStanislav Kinsbursky 
1715a1db410dSStanislav Kinsbursky static __net_exit void rpcsec_gss_exit_net(struct net *net)
1716a1db410dSStanislav Kinsbursky {
1717a1db410dSStanislav Kinsbursky 	gss_svc_shutdown_net(net);
1718a1db410dSStanislav Kinsbursky }
1719a1db410dSStanislav Kinsbursky 
1720a1db410dSStanislav Kinsbursky static struct pernet_operations rpcsec_gss_net_ops = {
1721a1db410dSStanislav Kinsbursky 	.init = rpcsec_gss_init_net,
1722a1db410dSStanislav Kinsbursky 	.exit = rpcsec_gss_exit_net,
1723a1db410dSStanislav Kinsbursky };
1724a1db410dSStanislav Kinsbursky 
17251da177e4SLinus Torvalds /*
17261da177e4SLinus Torvalds  * Initialize RPCSEC_GSS module
17271da177e4SLinus Torvalds  */
17281da177e4SLinus Torvalds static int __init init_rpcsec_gss(void)
17291da177e4SLinus Torvalds {
17301da177e4SLinus Torvalds 	int err = 0;
17311da177e4SLinus Torvalds 
17321da177e4SLinus Torvalds 	err = rpcauth_register(&authgss_ops);
17331da177e4SLinus Torvalds 	if (err)
17341da177e4SLinus Torvalds 		goto out;
17351da177e4SLinus Torvalds 	err = gss_svc_init();
17361da177e4SLinus Torvalds 	if (err)
17371da177e4SLinus Torvalds 		goto out_unregister;
1738a1db410dSStanislav Kinsbursky 	err = register_pernet_subsys(&rpcsec_gss_net_ops);
1739a1db410dSStanislav Kinsbursky 	if (err)
1740a1db410dSStanislav Kinsbursky 		goto out_svc_exit;
174179a3f20bS\"J. Bruce Fields\ 	rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version");
17421da177e4SLinus Torvalds 	return 0;
1743a1db410dSStanislav Kinsbursky out_svc_exit:
1744a1db410dSStanislav Kinsbursky 	gss_svc_shutdown();
17451da177e4SLinus Torvalds out_unregister:
17461da177e4SLinus Torvalds 	rpcauth_unregister(&authgss_ops);
17471da177e4SLinus Torvalds out:
17481da177e4SLinus Torvalds 	return err;
17491da177e4SLinus Torvalds }
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds static void __exit exit_rpcsec_gss(void)
17521da177e4SLinus Torvalds {
1753a1db410dSStanislav Kinsbursky 	unregister_pernet_subsys(&rpcsec_gss_net_ops);
17541da177e4SLinus Torvalds 	gss_svc_shutdown();
17551da177e4SLinus Torvalds 	rpcauth_unregister(&authgss_ops);
1756bf12691dSJesper Dangaard Brouer 	rcu_barrier(); /* Wait for completion of call_rcu()'s */
17571da177e4SLinus Torvalds }
17581da177e4SLinus Torvalds 
175971afa85eSChuck Lever MODULE_ALIAS("rpc-auth-6");
17601da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1761126e216aSTrond Myklebust module_param_named(expired_cred_retry_delay,
1762126e216aSTrond Myklebust 		   gss_expired_cred_retry_delay,
1763126e216aSTrond Myklebust 		   uint, 0644);
1764126e216aSTrond Myklebust MODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until "
1765126e216aSTrond Myklebust 		"the RPC engine retries an expired credential");
1766126e216aSTrond Myklebust 
17671da177e4SLinus Torvalds module_init(init_rpcsec_gss)
17681da177e4SLinus Torvalds module_exit(exit_rpcsec_gss)
1769