xref: /openbmc/linux/net/sunrpc/auth_gss/auth_gss.c (revision 19172284)
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 
7419172284STrond Myklebust struct gss_pipe {
7519172284STrond Myklebust 	struct rpc_pipe_dir_object pdo;
7619172284STrond Myklebust 	struct rpc_pipe *pipe;
7719172284STrond Myklebust 	struct rpc_clnt *clnt;
7819172284STrond Myklebust 	const char *name;
7919172284STrond Myklebust };
8019172284STrond Myklebust 
811da177e4SLinus Torvalds struct gss_auth {
820285ed1fSTrond Myklebust 	struct kref kref;
831da177e4SLinus Torvalds 	struct rpc_auth rpc_auth;
841da177e4SLinus Torvalds 	struct gss_api_mech *mech;
851da177e4SLinus Torvalds 	enum rpc_gss_svc service;
861da177e4SLinus Torvalds 	struct rpc_clnt *client;
87e726340aSTrond Myklebust 	struct net *net;
8834769fc4S\"J. Bruce Fields\ 	/*
8934769fc4S\"J. Bruce Fields\ 	 * There are two upcall pipes; dentry[1], named "gssd", is used
9034769fc4S\"J. Bruce Fields\ 	 * for the new text-based upcall; dentry[0] is named after the
9134769fc4S\"J. Bruce Fields\ 	 * mechanism (for example, "krb5") and exists for
9234769fc4S\"J. Bruce Fields\ 	 * backwards-compatibility with older gssd's.
9334769fc4S\"J. Bruce Fields\ 	 */
9419172284STrond Myklebust 	struct gss_pipe *gss_pipe[2];
95bd4a3eb1STrond Myklebust 	const char *target_name;
961da177e4SLinus Torvalds };
971da177e4SLinus Torvalds 
9879a3f20bS\"J. Bruce Fields\ /* pipe_version >= 0 if and only if someone has a pipe open. */
9979a3f20bS\"J. Bruce Fields\ static DEFINE_SPINLOCK(pipe_version_lock);
10079a3f20bS\"J. Bruce Fields\ static struct rpc_wait_queue pipe_version_rpc_waitqueue;
10179a3f20bS\"J. Bruce Fields\ static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue);
102cf81939dS\"J. Bruce Fields\ 
1035d28dc82STrond Myklebust static void gss_free_ctx(struct gss_cl_ctx *);
104b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v0;
105b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v1;
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds static inline struct gss_cl_ctx *
1081da177e4SLinus Torvalds gss_get_ctx(struct gss_cl_ctx *ctx)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	atomic_inc(&ctx->count);
1111da177e4SLinus Torvalds 	return ctx;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds static inline void
1151da177e4SLinus Torvalds gss_put_ctx(struct gss_cl_ctx *ctx)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	if (atomic_dec_and_test(&ctx->count))
1185d28dc82STrond Myklebust 		gss_free_ctx(ctx);
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1215d28dc82STrond Myklebust /* gss_cred_set_ctx:
1225d28dc82STrond Myklebust  * called by gss_upcall_callback and gss_create_upcall in order
1235d28dc82STrond Myklebust  * to set the gss context. The actual exchange of an old context
1249beae467SStanislav Kinsbursky  * and a new one is protected by the pipe->lock.
1255d28dc82STrond Myklebust  */
1261da177e4SLinus Torvalds static void
1271da177e4SLinus Torvalds gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1305d28dc82STrond Myklebust 
131cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
132cd019f75STrond Myklebust 		return;
1337b6962b0STrond Myklebust 	gss_get_ctx(ctx);
134cf778b00SEric Dumazet 	rcu_assign_pointer(gss_cred->gc_ctx, ctx);
135fc432dd9STrond Myklebust 	set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
136cd019f75STrond Myklebust 	smp_mb__before_clear_bit();
137fc432dd9STrond Myklebust 	clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds static const void *
1411da177e4SLinus Torvalds simple_get_bytes(const void *p, const void *end, void *res, size_t len)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	const void *q = (const void *)((const char *)p + len);
1441da177e4SLinus Torvalds 	if (unlikely(q > end || q < p))
1451da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
1461da177e4SLinus Torvalds 	memcpy(res, p, len);
1471da177e4SLinus Torvalds 	return q;
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static inline const void *
1511da177e4SLinus Torvalds simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	const void *q;
1541da177e4SLinus Torvalds 	unsigned int len;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &len, sizeof(len));
1571da177e4SLinus Torvalds 	if (IS_ERR(p))
1581da177e4SLinus Torvalds 		return p;
1591da177e4SLinus Torvalds 	q = (const void *)((const char *)p + len);
1601da177e4SLinus Torvalds 	if (unlikely(q > end || q < p))
1611da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
1620f38b873STrond Myklebust 	dest->data = kmemdup(p, len, GFP_NOFS);
1631da177e4SLinus Torvalds 	if (unlikely(dest->data == NULL))
1641da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
1651da177e4SLinus Torvalds 	dest->len = len;
1661da177e4SLinus Torvalds 	return q;
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds static struct gss_cl_ctx *
1701da177e4SLinus Torvalds gss_cred_get_ctx(struct rpc_cred *cred)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1731da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = NULL;
1741da177e4SLinus Torvalds 
1755d28dc82STrond Myklebust 	rcu_read_lock();
1761da177e4SLinus Torvalds 	if (gss_cred->gc_ctx)
1771da177e4SLinus Torvalds 		ctx = gss_get_ctx(gss_cred->gc_ctx);
1785d28dc82STrond Myklebust 	rcu_read_unlock();
1791da177e4SLinus Torvalds 	return ctx;
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds static struct gss_cl_ctx *
1831da177e4SLinus Torvalds gss_alloc_context(void)
1841da177e4SLinus Torvalds {
1851da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
1861da177e4SLinus Torvalds 
1870f38b873STrond Myklebust 	ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
1881da177e4SLinus Torvalds 	if (ctx != NULL) {
1891da177e4SLinus Torvalds 		ctx->gc_proc = RPC_GSS_PROC_DATA;
1901da177e4SLinus Torvalds 		ctx->gc_seq = 1;	/* NetApp 6.4R1 doesn't accept seq. no. 0 */
1911da177e4SLinus Torvalds 		spin_lock_init(&ctx->gc_seq_lock);
1921da177e4SLinus Torvalds 		atomic_set(&ctx->count,1);
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 	return ctx;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds #define GSSD_MIN_TIMEOUT (60 * 60)
1981da177e4SLinus Torvalds static const void *
1991da177e4SLinus Torvalds gss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	const void *q;
2021da177e4SLinus Torvalds 	unsigned int seclen;
2031da177e4SLinus Torvalds 	unsigned int timeout;
204620038f6SAndy Adamson 	unsigned long now = jiffies;
2051da177e4SLinus Torvalds 	u32 window_size;
2061da177e4SLinus Torvalds 	int ret;
2071da177e4SLinus Torvalds 
208620038f6SAndy Adamson 	/* First unsigned int gives the remaining lifetime in seconds of the
209620038f6SAndy Adamson 	 * credential - e.g. the remaining TGT lifetime for Kerberos or
210620038f6SAndy Adamson 	 * the -t value passed to GSSD.
211620038f6SAndy Adamson 	 */
2121da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
2131da177e4SLinus Torvalds 	if (IS_ERR(p))
2141da177e4SLinus Torvalds 		goto err;
2151da177e4SLinus Torvalds 	if (timeout == 0)
2161da177e4SLinus Torvalds 		timeout = GSSD_MIN_TIMEOUT;
217620038f6SAndy Adamson 	ctx->gc_expiry = now + ((unsigned long)timeout * HZ);
218620038f6SAndy Adamson 	/* Sequence number window. Determines the maximum number of
219620038f6SAndy Adamson 	 * simultaneous requests
220620038f6SAndy Adamson 	 */
2211da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
2221da177e4SLinus Torvalds 	if (IS_ERR(p))
2231da177e4SLinus Torvalds 		goto err;
2241da177e4SLinus Torvalds 	ctx->gc_win = window_size;
2251da177e4SLinus Torvalds 	/* gssd signals an error by passing ctx->gc_win = 0: */
2261da177e4SLinus Torvalds 	if (ctx->gc_win == 0) {
227dc5ddce9SJeff Layton 		/*
228dc5ddce9SJeff Layton 		 * in which case, p points to an error code. Anything other
229dc5ddce9SJeff Layton 		 * than -EKEYEXPIRED gets converted to -EACCES.
230dc5ddce9SJeff Layton 		 */
231dc5ddce9SJeff Layton 		p = simple_get_bytes(p, end, &ret, sizeof(ret));
232dc5ddce9SJeff Layton 		if (!IS_ERR(p))
233dc5ddce9SJeff Layton 			p = (ret == -EKEYEXPIRED) ? ERR_PTR(-EKEYEXPIRED) :
234dc5ddce9SJeff Layton 						    ERR_PTR(-EACCES);
2351da177e4SLinus Torvalds 		goto err;
2361da177e4SLinus Torvalds 	}
2371da177e4SLinus Torvalds 	/* copy the opaque wire context */
2381da177e4SLinus Torvalds 	p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
2391da177e4SLinus Torvalds 	if (IS_ERR(p))
2401da177e4SLinus Torvalds 		goto err;
2411da177e4SLinus Torvalds 	/* import the opaque security context */
2421da177e4SLinus Torvalds 	p  = simple_get_bytes(p, end, &seclen, sizeof(seclen));
2431da177e4SLinus Torvalds 	if (IS_ERR(p))
2441da177e4SLinus Torvalds 		goto err;
2451da177e4SLinus Torvalds 	q = (const void *)((const char *)p + seclen);
2461da177e4SLinus Torvalds 	if (unlikely(q > end || q < p)) {
2471da177e4SLinus Torvalds 		p = ERR_PTR(-EFAULT);
2481da177e4SLinus Torvalds 		goto err;
2491da177e4SLinus Torvalds 	}
250400f26b5SSimo Sorce 	ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx, NULL, GFP_NOFS);
2511da177e4SLinus Torvalds 	if (ret < 0) {
2521da177e4SLinus Torvalds 		p = ERR_PTR(ret);
2531da177e4SLinus Torvalds 		goto err;
2541da177e4SLinus Torvalds 	}
255620038f6SAndy Adamson 	dprintk("RPC:       %s Success. gc_expiry %lu now %lu timeout %u\n",
256620038f6SAndy Adamson 		__func__, ctx->gc_expiry, now, timeout);
2571da177e4SLinus Torvalds 	return q;
2581da177e4SLinus Torvalds err:
259173db309SJeff Layton 	dprintk("RPC:       %s returns error %ld\n", __func__, -PTR_ERR(p));
2601da177e4SLinus Torvalds 	return p;
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds 
26334769fc4S\"J. Bruce Fields\ #define UPCALL_BUF_LEN 128
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds struct gss_upcall_msg {
2661da177e4SLinus Torvalds 	atomic_t count;
2677eaf040bSEric W. Biederman 	kuid_t	uid;
2681da177e4SLinus Torvalds 	struct rpc_pipe_msg msg;
2691da177e4SLinus Torvalds 	struct list_head list;
2701da177e4SLinus Torvalds 	struct gss_auth *auth;
2719beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
2721da177e4SLinus Torvalds 	struct rpc_wait_queue rpc_waitqueue;
2731da177e4SLinus Torvalds 	wait_queue_head_t waitqueue;
2741da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
27534769fc4S\"J. Bruce Fields\ 	char databuf[UPCALL_BUF_LEN];
2761da177e4SLinus Torvalds };
2771da177e4SLinus Torvalds 
2782aed8b47STrond Myklebust static int get_pipe_version(struct net *net)
27979a3f20bS\"J. Bruce Fields\ {
2802aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
28179a3f20bS\"J. Bruce Fields\ 	int ret;
28279a3f20bS\"J. Bruce Fields\ 
28379a3f20bS\"J. Bruce Fields\ 	spin_lock(&pipe_version_lock);
2842aed8b47STrond Myklebust 	if (sn->pipe_version >= 0) {
2852aed8b47STrond Myklebust 		atomic_inc(&sn->pipe_users);
2862aed8b47STrond Myklebust 		ret = sn->pipe_version;
28779a3f20bS\"J. Bruce Fields\ 	} else
28879a3f20bS\"J. Bruce Fields\ 		ret = -EAGAIN;
28979a3f20bS\"J. Bruce Fields\ 	spin_unlock(&pipe_version_lock);
29079a3f20bS\"J. Bruce Fields\ 	return ret;
29179a3f20bS\"J. Bruce Fields\ }
29279a3f20bS\"J. Bruce Fields\ 
2932aed8b47STrond Myklebust static void put_pipe_version(struct net *net)
29479a3f20bS\"J. Bruce Fields\ {
2952aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2962aed8b47STrond Myklebust 
2972aed8b47STrond Myklebust 	if (atomic_dec_and_lock(&sn->pipe_users, &pipe_version_lock)) {
2982aed8b47STrond Myklebust 		sn->pipe_version = -1;
29979a3f20bS\"J. Bruce Fields\ 		spin_unlock(&pipe_version_lock);
30079a3f20bS\"J. Bruce Fields\ 	}
30179a3f20bS\"J. Bruce Fields\ }
30279a3f20bS\"J. Bruce Fields\ 
3031da177e4SLinus Torvalds static void
3041da177e4SLinus Torvalds gss_release_msg(struct gss_upcall_msg *gss_msg)
3051da177e4SLinus Torvalds {
306e726340aSTrond Myklebust 	struct net *net = gss_msg->auth->net;
3071da177e4SLinus Torvalds 	if (!atomic_dec_and_test(&gss_msg->count))
3081da177e4SLinus Torvalds 		return;
3092aed8b47STrond Myklebust 	put_pipe_version(net);
3101da177e4SLinus Torvalds 	BUG_ON(!list_empty(&gss_msg->list));
3111da177e4SLinus Torvalds 	if (gss_msg->ctx != NULL)
3121da177e4SLinus Torvalds 		gss_put_ctx(gss_msg->ctx);
313f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue);
3141da177e4SLinus Torvalds 	kfree(gss_msg);
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds static struct gss_upcall_msg *
3187eaf040bSEric W. Biederman __gss_find_upcall(struct rpc_pipe *pipe, kuid_t uid)
3191da177e4SLinus Torvalds {
3201da177e4SLinus Torvalds 	struct gss_upcall_msg *pos;
3219beae467SStanislav Kinsbursky 	list_for_each_entry(pos, &pipe->in_downcall, list) {
3220b4d51b0SEric W. Biederman 		if (!uid_eq(pos->uid, uid))
3231da177e4SLinus Torvalds 			continue;
3241da177e4SLinus Torvalds 		atomic_inc(&pos->count);
325632f0d05SChuck Lever 		dprintk("RPC:       %s found msg %p\n", __func__, pos);
3261da177e4SLinus Torvalds 		return pos;
3271da177e4SLinus Torvalds 	}
328632f0d05SChuck Lever 	dprintk("RPC:       %s found nothing\n", __func__);
3291da177e4SLinus Torvalds 	return NULL;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
332720b8f2dS\\\"J. Bruce Fields\\\ /* Try to add an upcall to the pipefs queue.
3331da177e4SLinus Torvalds  * If an upcall owned by our uid already exists, then we return a reference
3341da177e4SLinus Torvalds  * to that upcall instead of adding the new upcall.
3351da177e4SLinus Torvalds  */
3361da177e4SLinus Torvalds static inline struct gss_upcall_msg *
337053e324fSSuresh Jayaraman gss_add_msg(struct gss_upcall_msg *gss_msg)
3381da177e4SLinus Torvalds {
3399beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
3401da177e4SLinus Torvalds 	struct gss_upcall_msg *old;
3411da177e4SLinus Torvalds 
3429beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
3439beae467SStanislav Kinsbursky 	old = __gss_find_upcall(pipe, gss_msg->uid);
3441da177e4SLinus Torvalds 	if (old == NULL) {
3451da177e4SLinus Torvalds 		atomic_inc(&gss_msg->count);
3469beae467SStanislav Kinsbursky 		list_add(&gss_msg->list, &pipe->in_downcall);
3471da177e4SLinus Torvalds 	} else
3481da177e4SLinus Torvalds 		gss_msg = old;
3499beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
3501da177e4SLinus Torvalds 	return gss_msg;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds static void
3541da177e4SLinus Torvalds __gss_unhash_msg(struct gss_upcall_msg *gss_msg)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	list_del_init(&gss_msg->list);
3571da177e4SLinus Torvalds 	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
3581da177e4SLinus Torvalds 	wake_up_all(&gss_msg->waitqueue);
3591da177e4SLinus Torvalds 	atomic_dec(&gss_msg->count);
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds static void
3631da177e4SLinus Torvalds gss_unhash_msg(struct gss_upcall_msg *gss_msg)
3641da177e4SLinus Torvalds {
3659beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
3661da177e4SLinus Torvalds 
3673b68aaeaSTrond Myklebust 	if (list_empty(&gss_msg->list))
3683b68aaeaSTrond Myklebust 		return;
3699beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
3703b68aaeaSTrond Myklebust 	if (!list_empty(&gss_msg->list))
3711da177e4SLinus Torvalds 		__gss_unhash_msg(gss_msg);
3729beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds static void
376126e216aSTrond Myklebust gss_handle_downcall_result(struct gss_cred *gss_cred, struct gss_upcall_msg *gss_msg)
377126e216aSTrond Myklebust {
378126e216aSTrond Myklebust 	switch (gss_msg->msg.errno) {
379126e216aSTrond Myklebust 	case 0:
380126e216aSTrond Myklebust 		if (gss_msg->ctx == NULL)
381126e216aSTrond Myklebust 			break;
382126e216aSTrond Myklebust 		clear_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
383126e216aSTrond Myklebust 		gss_cred_set_ctx(&gss_cred->gc_base, gss_msg->ctx);
384126e216aSTrond Myklebust 		break;
385126e216aSTrond Myklebust 	case -EKEYEXPIRED:
386126e216aSTrond Myklebust 		set_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
387126e216aSTrond Myklebust 	}
388126e216aSTrond Myklebust 	gss_cred->gc_upcall_timestamp = jiffies;
389126e216aSTrond Myklebust 	gss_cred->gc_upcall = NULL;
390126e216aSTrond Myklebust 	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
391126e216aSTrond Myklebust }
392126e216aSTrond Myklebust 
393126e216aSTrond Myklebust static void
3941da177e4SLinus Torvalds gss_upcall_callback(struct rpc_task *task)
3951da177e4SLinus Torvalds {
396a17c2153STrond Myklebust 	struct gss_cred *gss_cred = container_of(task->tk_rqstp->rq_cred,
3971da177e4SLinus Torvalds 			struct gss_cred, gc_base);
3981da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
3999beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
4001da177e4SLinus Torvalds 
4019beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
402126e216aSTrond Myklebust 	gss_handle_downcall_result(gss_cred, gss_msg);
4039beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
404126e216aSTrond Myklebust 	task->tk_status = gss_msg->msg.errno;
4051da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
40834769fc4S\"J. Bruce Fields\ static void gss_encode_v0_msg(struct gss_upcall_msg *gss_msg)
40934769fc4S\"J. Bruce Fields\ {
41090602c7bSEric W. Biederman 	uid_t uid = from_kuid(&init_user_ns, gss_msg->uid);
41190602c7bSEric W. Biederman 	memcpy(gss_msg->databuf, &uid, sizeof(uid));
41290602c7bSEric W. Biederman 	gss_msg->msg.data = gss_msg->databuf;
41390602c7bSEric W. Biederman 	gss_msg->msg.len = sizeof(uid);
41490602c7bSEric W. Biederman 	BUG_ON(sizeof(uid) > UPCALL_BUF_LEN);
41534769fc4S\"J. Bruce Fields\ }
41634769fc4S\"J. Bruce Fields\ 
4178b1c7bf5SOlga Kornievskaia static void gss_encode_v1_msg(struct gss_upcall_msg *gss_msg,
418bd4a3eb1STrond Myklebust 				const char *service_name,
419bd4a3eb1STrond Myklebust 				const char *target_name)
42034769fc4S\"J. Bruce Fields\ {
421683ac665STrond Myklebust 	struct gss_api_mech *mech = gss_msg->auth->mech;
4228b1c7bf5SOlga Kornievskaia 	char *p = gss_msg->databuf;
4238b1c7bf5SOlga Kornievskaia 	int len = 0;
4248b1c7bf5SOlga Kornievskaia 
4258b1c7bf5SOlga Kornievskaia 	gss_msg->msg.len = sprintf(gss_msg->databuf, "mech=%s uid=%d ",
426683ac665STrond Myklebust 				   mech->gm_name,
42790602c7bSEric W. Biederman 				   from_kuid(&init_user_ns, gss_msg->uid));
4288b1c7bf5SOlga Kornievskaia 	p += gss_msg->msg.len;
429bd4a3eb1STrond Myklebust 	if (target_name) {
430bd4a3eb1STrond Myklebust 		len = sprintf(p, "target=%s ", target_name);
4318b1c7bf5SOlga Kornievskaia 		p += len;
4328b1c7bf5SOlga Kornievskaia 		gss_msg->msg.len += len;
4338b1c7bf5SOlga Kornievskaia 	}
43468c97153STrond Myklebust 	if (service_name != NULL) {
43568c97153STrond Myklebust 		len = sprintf(p, "service=%s ", service_name);
4362efef708SOlga Kornievskaia 		p += len;
4372efef708SOlga Kornievskaia 		gss_msg->msg.len += len;
4382efef708SOlga Kornievskaia 	}
439683ac665STrond Myklebust 	if (mech->gm_upcall_enctypes) {
440f8628220SKevin Coffman 		len = sprintf(p, "enctypes=%s ", mech->gm_upcall_enctypes);
441683ac665STrond Myklebust 		p += len;
442683ac665STrond Myklebust 		gss_msg->msg.len += len;
443683ac665STrond Myklebust 	}
4448b1c7bf5SOlga Kornievskaia 	len = sprintf(p, "\n");
4458b1c7bf5SOlga Kornievskaia 	gss_msg->msg.len += len;
4468b1c7bf5SOlga Kornievskaia 
44734769fc4S\"J. Bruce Fields\ 	gss_msg->msg.data = gss_msg->databuf;
44834769fc4S\"J. Bruce Fields\ 	BUG_ON(gss_msg->msg.len > UPCALL_BUF_LEN);
44934769fc4S\"J. Bruce Fields\ }
45034769fc4S\"J. Bruce Fields\ 
45168c97153STrond Myklebust static struct gss_upcall_msg *
452e726340aSTrond Myklebust gss_alloc_msg(struct gss_auth *gss_auth,
4537eaf040bSEric W. Biederman 		kuid_t uid, const char *service_name)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
45679a3f20bS\"J. Bruce Fields\ 	int vers;
4571da177e4SLinus Torvalds 
4580f38b873STrond Myklebust 	gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
459db75b3d6S\"J. Bruce Fields\ 	if (gss_msg == NULL)
460db75b3d6S\"J. Bruce Fields\ 		return ERR_PTR(-ENOMEM);
461e726340aSTrond Myklebust 	vers = get_pipe_version(gss_auth->net);
46279a3f20bS\"J. Bruce Fields\ 	if (vers < 0) {
46379a3f20bS\"J. Bruce Fields\ 		kfree(gss_msg);
46479a3f20bS\"J. Bruce Fields\ 		return ERR_PTR(vers);
46579a3f20bS\"J. Bruce Fields\ 	}
46619172284STrond Myklebust 	gss_msg->pipe = gss_auth->gss_pipe[vers]->pipe;
4671da177e4SLinus Torvalds 	INIT_LIST_HEAD(&gss_msg->list);
4681da177e4SLinus Torvalds 	rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
4691da177e4SLinus Torvalds 	init_waitqueue_head(&gss_msg->waitqueue);
4701da177e4SLinus Torvalds 	atomic_set(&gss_msg->count, 1);
4711da177e4SLinus Torvalds 	gss_msg->uid = uid;
4721da177e4SLinus Torvalds 	gss_msg->auth = gss_auth;
473bd4a3eb1STrond Myklebust 	switch (vers) {
474bd4a3eb1STrond Myklebust 	case 0:
475bd4a3eb1STrond Myklebust 		gss_encode_v0_msg(gss_msg);
476bd4a3eb1STrond Myklebust 	default:
477bd4a3eb1STrond Myklebust 		gss_encode_v1_msg(gss_msg, service_name, gss_auth->target_name);
478bd4a3eb1STrond Myklebust 	};
4791da177e4SLinus Torvalds 	return gss_msg;
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds static struct gss_upcall_msg *
483e726340aSTrond Myklebust gss_setup_upcall(struct gss_auth *gss_auth, struct rpc_cred *cred)
4841da177e4SLinus Torvalds {
4857c67db3aSTrond Myklebust 	struct gss_cred *gss_cred = container_of(cred,
4867c67db3aSTrond Myklebust 			struct gss_cred, gc_base);
4871da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_new, *gss_msg;
4887eaf040bSEric W. Biederman 	kuid_t uid = cred->cr_uid;
4891da177e4SLinus Torvalds 
490e726340aSTrond Myklebust 	gss_new = gss_alloc_msg(gss_auth, uid, gss_cred->gc_principal);
491db75b3d6S\"J. Bruce Fields\ 	if (IS_ERR(gss_new))
492db75b3d6S\"J. Bruce Fields\ 		return gss_new;
493053e324fSSuresh Jayaraman 	gss_msg = gss_add_msg(gss_new);
4941da177e4SLinus Torvalds 	if (gss_msg == gss_new) {
4959beae467SStanislav Kinsbursky 		int res = rpc_queue_upcall(gss_new->pipe, &gss_new->msg);
4961da177e4SLinus Torvalds 		if (res) {
4971da177e4SLinus Torvalds 			gss_unhash_msg(gss_new);
4981da177e4SLinus Torvalds 			gss_msg = ERR_PTR(res);
4991da177e4SLinus Torvalds 		}
5001da177e4SLinus Torvalds 	} else
5011da177e4SLinus Torvalds 		gss_release_msg(gss_new);
5021da177e4SLinus Torvalds 	return gss_msg;
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
505b03568c3S\"J. Bruce Fields\ static void warn_gssd(void)
506b03568c3S\"J. Bruce Fields\ {
507b03568c3S\"J. Bruce Fields\ 	static unsigned long ratelimit;
508b03568c3S\"J. Bruce Fields\ 	unsigned long now = jiffies;
509b03568c3S\"J. Bruce Fields\ 
510b03568c3S\"J. Bruce Fields\ 	if (time_after(now, ratelimit)) {
511b03568c3S\"J. Bruce Fields\ 		printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
512b03568c3S\"J. Bruce Fields\ 				"Please check user daemon is running.\n");
513b03568c3S\"J. Bruce Fields\ 		ratelimit = now + 15*HZ;
514b03568c3S\"J. Bruce Fields\ 	}
515b03568c3S\"J. Bruce Fields\ }
516b03568c3S\"J. Bruce Fields\ 
5171da177e4SLinus Torvalds static inline int
5181da177e4SLinus Torvalds gss_refresh_upcall(struct rpc_task *task)
5191da177e4SLinus Torvalds {
520a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
5214a8c1344STrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth,
5221da177e4SLinus Torvalds 			struct gss_auth, rpc_auth);
5231da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred,
5241da177e4SLinus Torvalds 			struct gss_cred, gc_base);
5251da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
5269beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
5271da177e4SLinus Torvalds 	int err = 0;
5281da177e4SLinus Torvalds 
529632f0d05SChuck Lever 	dprintk("RPC: %5u %s for uid %u\n",
530cdba321eSEric W. Biederman 		task->tk_pid, __func__, from_kuid(&init_user_ns, cred->cr_uid));
531e726340aSTrond Myklebust 	gss_msg = gss_setup_upcall(gss_auth, cred);
532480e3243SRoel Kluin 	if (PTR_ERR(gss_msg) == -EAGAIN) {
53379a3f20bS\"J. Bruce Fields\ 		/* XXX: warning on the first, under the assumption we
53479a3f20bS\"J. Bruce Fields\ 		 * shouldn't normally hit this case on a refresh. */
53579a3f20bS\"J. Bruce Fields\ 		warn_gssd();
53679a3f20bS\"J. Bruce Fields\ 		task->tk_timeout = 15*HZ;
53779a3f20bS\"J. Bruce Fields\ 		rpc_sleep_on(&pipe_version_rpc_waitqueue, task, NULL);
538d1a8016aSBryan Schumaker 		return -EAGAIN;
53979a3f20bS\"J. Bruce Fields\ 	}
5401da177e4SLinus Torvalds 	if (IS_ERR(gss_msg)) {
5411da177e4SLinus Torvalds 		err = PTR_ERR(gss_msg);
5421da177e4SLinus Torvalds 		goto out;
5431da177e4SLinus Torvalds 	}
5449beae467SStanislav Kinsbursky 	pipe = gss_msg->pipe;
5459beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
5461da177e4SLinus Torvalds 	if (gss_cred->gc_upcall != NULL)
5475d00837bSTrond Myklebust 		rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL);
548126e216aSTrond Myklebust 	else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
5491da177e4SLinus Torvalds 		task->tk_timeout = 0;
5501da177e4SLinus Torvalds 		gss_cred->gc_upcall = gss_msg;
5511da177e4SLinus Torvalds 		/* gss_upcall_callback will release the reference to gss_upcall_msg */
5521da177e4SLinus Torvalds 		atomic_inc(&gss_msg->count);
5535d00837bSTrond Myklebust 		rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback);
554126e216aSTrond Myklebust 	} else {
555126e216aSTrond Myklebust 		gss_handle_downcall_result(gss_cred, gss_msg);
5561da177e4SLinus Torvalds 		err = gss_msg->msg.errno;
557126e216aSTrond Myklebust 	}
5589beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
5591da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
5601da177e4SLinus Torvalds out:
561632f0d05SChuck Lever 	dprintk("RPC: %5u %s for uid %u result %d\n",
562cdba321eSEric W. Biederman 		task->tk_pid, __func__,
563cdba321eSEric W. Biederman 		from_kuid(&init_user_ns, cred->cr_uid),	err);
5641da177e4SLinus Torvalds 	return err;
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds static inline int
5681da177e4SLinus Torvalds gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
5691da177e4SLinus Torvalds {
570e726340aSTrond Myklebust 	struct net *net = gss_auth->net;
571abfdbd53STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
5729beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
5731da177e4SLinus Torvalds 	struct rpc_cred *cred = &gss_cred->gc_base;
5741da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
575abfdbd53STrond Myklebust 	unsigned long timeout;
5761da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
577d36ccb9cSTrond Myklebust 	int err;
5781da177e4SLinus Torvalds 
579cdba321eSEric W. Biederman 	dprintk("RPC:       %s for uid %u\n",
580cdba321eSEric W. Biederman 		__func__, from_kuid(&init_user_ns, cred->cr_uid));
58179a3f20bS\"J. Bruce Fields\ retry:
582d36ccb9cSTrond Myklebust 	err = 0;
583abfdbd53STrond Myklebust 	/* Default timeout is 15s unless we know that gssd is not running */
584abfdbd53STrond Myklebust 	timeout = 15 * HZ;
585abfdbd53STrond Myklebust 	if (!sn->gssd_running)
586abfdbd53STrond Myklebust 		timeout = HZ >> 2;
587e726340aSTrond Myklebust 	gss_msg = gss_setup_upcall(gss_auth, cred);
58879a3f20bS\"J. Bruce Fields\ 	if (PTR_ERR(gss_msg) == -EAGAIN) {
58979a3f20bS\"J. Bruce Fields\ 		err = wait_event_interruptible_timeout(pipe_version_waitqueue,
5902aed8b47STrond Myklebust 				sn->pipe_version >= 0, timeout);
5912aed8b47STrond Myklebust 		if (sn->pipe_version < 0) {
592abfdbd53STrond Myklebust 			if (err == 0)
593abfdbd53STrond Myklebust 				sn->gssd_running = 0;
594d1a8016aSBryan Schumaker 			warn_gssd();
595d1a8016aSBryan Schumaker 			err = -EACCES;
596d1a8016aSBryan Schumaker 		}
597d36ccb9cSTrond Myklebust 		if (err < 0)
59879a3f20bS\"J. Bruce Fields\ 			goto out;
59979a3f20bS\"J. Bruce Fields\ 		goto retry;
60079a3f20bS\"J. Bruce Fields\ 	}
6011da177e4SLinus Torvalds 	if (IS_ERR(gss_msg)) {
6021da177e4SLinus Torvalds 		err = PTR_ERR(gss_msg);
6031da177e4SLinus Torvalds 		goto out;
6041da177e4SLinus Torvalds 	}
6059beae467SStanislav Kinsbursky 	pipe = gss_msg->pipe;
6061da177e4SLinus Torvalds 	for (;;) {
6075afa9133STrond Myklebust 		prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_KILLABLE);
6089beae467SStanislav Kinsbursky 		spin_lock(&pipe->lock);
6091da177e4SLinus Torvalds 		if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
6101da177e4SLinus Torvalds 			break;
6111da177e4SLinus Torvalds 		}
6129beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
6135afa9133STrond Myklebust 		if (fatal_signal_pending(current)) {
6141da177e4SLinus Torvalds 			err = -ERESTARTSYS;
6151da177e4SLinus Torvalds 			goto out_intr;
6161da177e4SLinus Torvalds 		}
6171da177e4SLinus Torvalds 		schedule();
6181da177e4SLinus Torvalds 	}
6191da177e4SLinus Torvalds 	if (gss_msg->ctx)
6207b6962b0STrond Myklebust 		gss_cred_set_ctx(cred, gss_msg->ctx);
6211da177e4SLinus Torvalds 	else
6221da177e4SLinus Torvalds 		err = gss_msg->msg.errno;
6239beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
6241da177e4SLinus Torvalds out_intr:
6251da177e4SLinus Torvalds 	finish_wait(&gss_msg->waitqueue, &wait);
6261da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
6271da177e4SLinus Torvalds out:
628632f0d05SChuck Lever 	dprintk("RPC:       %s for uid %u result %d\n",
629cdba321eSEric W. Biederman 		__func__, from_kuid(&init_user_ns, cred->cr_uid), err);
6301da177e4SLinus Torvalds 	return err;
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds #define MSG_BUF_MAXSIZE 1024
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds static ssize_t
6361da177e4SLinus Torvalds gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
6371da177e4SLinus Torvalds {
6381da177e4SLinus Torvalds 	const void *p, *end;
6391da177e4SLinus Torvalds 	void *buf;
6401da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
641496ad9aaSAl Viro 	struct rpc_pipe *pipe = RPC_I(file_inode(filp))->pipe;
6421da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
64390602c7bSEric W. Biederman 	uid_t id;
64490602c7bSEric W. Biederman 	kuid_t uid;
6453b68aaeaSTrond Myklebust 	ssize_t err = -EFBIG;
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	if (mlen > MSG_BUF_MAXSIZE)
6481da177e4SLinus Torvalds 		goto out;
6491da177e4SLinus Torvalds 	err = -ENOMEM;
6500f38b873STrond Myklebust 	buf = kmalloc(mlen, GFP_NOFS);
6511da177e4SLinus Torvalds 	if (!buf)
6521da177e4SLinus Torvalds 		goto out;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	err = -EFAULT;
6551da177e4SLinus Torvalds 	if (copy_from_user(buf, src, mlen))
6561da177e4SLinus Torvalds 		goto err;
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	end = (const void *)((char *)buf + mlen);
65990602c7bSEric W. Biederman 	p = simple_get_bytes(buf, end, &id, sizeof(id));
6601da177e4SLinus Torvalds 	if (IS_ERR(p)) {
6611da177e4SLinus Torvalds 		err = PTR_ERR(p);
6621da177e4SLinus Torvalds 		goto err;
6631da177e4SLinus Torvalds 	}
6641da177e4SLinus Torvalds 
66590602c7bSEric W. Biederman 	uid = make_kuid(&init_user_ns, id);
66690602c7bSEric W. Biederman 	if (!uid_valid(uid)) {
66790602c7bSEric W. Biederman 		err = -EINVAL;
66890602c7bSEric W. Biederman 		goto err;
66990602c7bSEric W. Biederman 	}
67090602c7bSEric W. Biederman 
6711da177e4SLinus Torvalds 	err = -ENOMEM;
6721da177e4SLinus Torvalds 	ctx = gss_alloc_context();
6731da177e4SLinus Torvalds 	if (ctx == NULL)
6741da177e4SLinus Torvalds 		goto err;
6753b68aaeaSTrond Myklebust 
6763b68aaeaSTrond Myklebust 	err = -ENOENT;
6773b68aaeaSTrond Myklebust 	/* Find a matching upcall */
6789beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
6799beae467SStanislav Kinsbursky 	gss_msg = __gss_find_upcall(pipe, uid);
6803b68aaeaSTrond Myklebust 	if (gss_msg == NULL) {
6819beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
6823b68aaeaSTrond Myklebust 		goto err_put_ctx;
6833b68aaeaSTrond Myklebust 	}
6843b68aaeaSTrond Myklebust 	list_del_init(&gss_msg->list);
6859beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
6863b68aaeaSTrond Myklebust 
6876e84c7b6STrond Myklebust 	p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
6881da177e4SLinus Torvalds 	if (IS_ERR(p)) {
6891da177e4SLinus Torvalds 		err = PTR_ERR(p);
690486bad2eSJeff Layton 		switch (err) {
691486bad2eSJeff Layton 		case -EACCES:
692dc5ddce9SJeff Layton 		case -EKEYEXPIRED:
693486bad2eSJeff Layton 			gss_msg->msg.errno = err;
694486bad2eSJeff Layton 			err = mlen;
695486bad2eSJeff Layton 			break;
696486bad2eSJeff Layton 		case -EFAULT:
697486bad2eSJeff Layton 		case -ENOMEM:
698486bad2eSJeff Layton 		case -EINVAL:
699486bad2eSJeff Layton 		case -ENOSYS:
700486bad2eSJeff Layton 			gss_msg->msg.errno = -EAGAIN;
701486bad2eSJeff Layton 			break;
702486bad2eSJeff Layton 		default:
703486bad2eSJeff Layton 			printk(KERN_CRIT "%s: bad return from "
7046c853099SRandy Dunlap 				"gss_fill_context: %zd\n", __func__, err);
705486bad2eSJeff Layton 			BUG();
706486bad2eSJeff Layton 		}
7073b68aaeaSTrond Myklebust 		goto err_release_msg;
7081da177e4SLinus Torvalds 	}
7091da177e4SLinus Torvalds 	gss_msg->ctx = gss_get_ctx(ctx);
7103b68aaeaSTrond Myklebust 	err = mlen;
7113b68aaeaSTrond Myklebust 
7123b68aaeaSTrond Myklebust err_release_msg:
7139beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
7141da177e4SLinus Torvalds 	__gss_unhash_msg(gss_msg);
7159beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
7161da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
7171da177e4SLinus Torvalds err_put_ctx:
7181da177e4SLinus Torvalds 	gss_put_ctx(ctx);
7191da177e4SLinus Torvalds err:
7201da177e4SLinus Torvalds 	kfree(buf);
7211da177e4SLinus Torvalds out:
722632f0d05SChuck Lever 	dprintk("RPC:       %s returning %Zd\n", __func__, err);
7231da177e4SLinus Torvalds 	return err;
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
72634769fc4S\"J. Bruce Fields\ static int gss_pipe_open(struct inode *inode, int new_version)
727cf81939dS\"J. Bruce Fields\ {
7282aed8b47STrond Myklebust 	struct net *net = inode->i_sb->s_fs_info;
7292aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
73034769fc4S\"J. Bruce Fields\ 	int ret = 0;
73134769fc4S\"J. Bruce Fields\ 
73279a3f20bS\"J. Bruce Fields\ 	spin_lock(&pipe_version_lock);
7332aed8b47STrond Myklebust 	if (sn->pipe_version < 0) {
73434769fc4S\"J. Bruce Fields\ 		/* First open of any gss pipe determines the version: */
7352aed8b47STrond Myklebust 		sn->pipe_version = new_version;
73679a3f20bS\"J. Bruce Fields\ 		rpc_wake_up(&pipe_version_rpc_waitqueue);
73779a3f20bS\"J. Bruce Fields\ 		wake_up(&pipe_version_waitqueue);
7382aed8b47STrond Myklebust 	} else if (sn->pipe_version != new_version) {
73934769fc4S\"J. Bruce Fields\ 		/* Trying to open a pipe of a different version */
74034769fc4S\"J. Bruce Fields\ 		ret = -EBUSY;
74134769fc4S\"J. Bruce Fields\ 		goto out;
74279a3f20bS\"J. Bruce Fields\ 	}
7432aed8b47STrond Myklebust 	atomic_inc(&sn->pipe_users);
74434769fc4S\"J. Bruce Fields\ out:
74579a3f20bS\"J. Bruce Fields\ 	spin_unlock(&pipe_version_lock);
74634769fc4S\"J. Bruce Fields\ 	return ret;
74734769fc4S\"J. Bruce Fields\ 
74834769fc4S\"J. Bruce Fields\ }
74934769fc4S\"J. Bruce Fields\ 
75034769fc4S\"J. Bruce Fields\ static int gss_pipe_open_v0(struct inode *inode)
75134769fc4S\"J. Bruce Fields\ {
75234769fc4S\"J. Bruce Fields\ 	return gss_pipe_open(inode, 0);
75334769fc4S\"J. Bruce Fields\ }
75434769fc4S\"J. Bruce Fields\ 
75534769fc4S\"J. Bruce Fields\ static int gss_pipe_open_v1(struct inode *inode)
75634769fc4S\"J. Bruce Fields\ {
75734769fc4S\"J. Bruce Fields\ 	return gss_pipe_open(inode, 1);
758cf81939dS\"J. Bruce Fields\ }
759cf81939dS\"J. Bruce Fields\ 
7601da177e4SLinus Torvalds static void
7611da177e4SLinus Torvalds gss_pipe_release(struct inode *inode)
7621da177e4SLinus Torvalds {
7632aed8b47STrond Myklebust 	struct net *net = inode->i_sb->s_fs_info;
7649beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
7651da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
7661da177e4SLinus Torvalds 
7675a67657aSTrond Myklebust restart:
7689beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
7699beae467SStanislav Kinsbursky 	list_for_each_entry(gss_msg, &pipe->in_downcall, list) {
7706e84c7b6STrond Myklebust 
7715a67657aSTrond Myklebust 		if (!list_empty(&gss_msg->msg.list))
7725a67657aSTrond Myklebust 			continue;
7731da177e4SLinus Torvalds 		gss_msg->msg.errno = -EPIPE;
7741da177e4SLinus Torvalds 		atomic_inc(&gss_msg->count);
7751da177e4SLinus Torvalds 		__gss_unhash_msg(gss_msg);
7769beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
7771da177e4SLinus Torvalds 		gss_release_msg(gss_msg);
7785a67657aSTrond Myklebust 		goto restart;
7791da177e4SLinus Torvalds 	}
7809beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
781cf81939dS\"J. Bruce Fields\ 
7822aed8b47STrond Myklebust 	put_pipe_version(net);
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds static void
7861da177e4SLinus Torvalds gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
7871da177e4SLinus Torvalds {
7881da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	if (msg->errno < 0) {
791632f0d05SChuck Lever 		dprintk("RPC:       %s releasing msg %p\n",
792632f0d05SChuck Lever 			__func__, gss_msg);
7931da177e4SLinus Torvalds 		atomic_inc(&gss_msg->count);
7941da177e4SLinus Torvalds 		gss_unhash_msg(gss_msg);
795b03568c3S\"J. Bruce Fields\ 		if (msg->errno == -ETIMEDOUT)
796b03568c3S\"J. Bruce Fields\ 			warn_gssd();
7971da177e4SLinus Torvalds 		gss_release_msg(gss_msg);
7981da177e4SLinus Torvalds 	}
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds 
80119172284STrond Myklebust static void gss_pipe_dentry_destroy(struct dentry *dir,
80219172284STrond Myklebust 		struct rpc_pipe_dir_object *pdo)
803ccdc28f8SStanislav Kinsbursky {
80419172284STrond Myklebust 	struct gss_pipe *gss_pipe = pdo->pdo_data;
80519172284STrond Myklebust 	struct rpc_pipe *pipe = gss_pipe->pipe;
806ccdc28f8SStanislav Kinsbursky 
80719172284STrond Myklebust 	if (pipe->dentry != NULL) {
80819172284STrond Myklebust 		rpc_unlink(pipe->dentry);
80919172284STrond Myklebust 		pipe->dentry = NULL;
8106b2fddd3STrond Myklebust 	}
811ccdc28f8SStanislav Kinsbursky }
812ccdc28f8SStanislav Kinsbursky 
81319172284STrond Myklebust static int gss_pipe_dentry_create(struct dentry *dir,
81419172284STrond Myklebust 		struct rpc_pipe_dir_object *pdo)
815ccdc28f8SStanislav Kinsbursky {
81619172284STrond Myklebust 	struct gss_pipe *p = pdo->pdo_data;
8176b2fddd3STrond Myklebust 	struct dentry *dentry;
818ccdc28f8SStanislav Kinsbursky 
81919172284STrond Myklebust 	dentry = rpc_mkpipe_dentry(dir, p->name, p->clnt, p->pipe);
82019172284STrond Myklebust 	if (IS_ERR(dentry))
82119172284STrond Myklebust 		return PTR_ERR(dentry);
82219172284STrond Myklebust 	p->pipe->dentry = dentry;
823ccdc28f8SStanislav Kinsbursky 	return 0;
82419172284STrond Myklebust }
825ccdc28f8SStanislav Kinsbursky 
82619172284STrond Myklebust static const struct rpc_pipe_dir_object_ops gss_pipe_dir_object_ops = {
82719172284STrond Myklebust 	.create = gss_pipe_dentry_create,
82819172284STrond Myklebust 	.destroy = gss_pipe_dentry_destroy,
82919172284STrond Myklebust };
83019172284STrond Myklebust 
83119172284STrond Myklebust static struct gss_pipe *gss_pipe_alloc(struct rpc_clnt *clnt,
83219172284STrond Myklebust 		const char *name,
83319172284STrond Myklebust 		const struct rpc_pipe_ops *upcall_ops)
83419172284STrond Myklebust {
83519172284STrond Myklebust 	struct net *net = rpc_net_ns(clnt);
83619172284STrond Myklebust 	struct gss_pipe *p;
83719172284STrond Myklebust 	int err = -ENOMEM;
83819172284STrond Myklebust 
83919172284STrond Myklebust 	p = kmalloc(sizeof(*p), GFP_KERNEL);
84019172284STrond Myklebust 	if (p == NULL)
84119172284STrond Myklebust 		goto err;
84219172284STrond Myklebust 	p->pipe = rpc_mkpipe_data(upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
84319172284STrond Myklebust 	if (IS_ERR(p->pipe)) {
84419172284STrond Myklebust 		err = PTR_ERR(p->pipe);
84519172284STrond Myklebust 		goto err_free_gss_pipe;
84619172284STrond Myklebust 	}
84719172284STrond Myklebust 	p->name = name;
84819172284STrond Myklebust 	p->clnt = clnt;
84919172284STrond Myklebust 	rpc_init_pipe_dir_object(&p->pdo,
85019172284STrond Myklebust 			&gss_pipe_dir_object_ops,
85119172284STrond Myklebust 			p);
85219172284STrond Myklebust 	err = rpc_add_pipe_dir_object(net, &clnt->cl_pipedir_objects, &p->pdo);
85319172284STrond Myklebust 	if (!err)
85419172284STrond Myklebust 		return p;
85519172284STrond Myklebust 	rpc_destroy_pipe_data(p->pipe);
85619172284STrond Myklebust err_free_gss_pipe:
85719172284STrond Myklebust 	kfree(p);
8586b2fddd3STrond Myklebust err:
85919172284STrond Myklebust 	return ERR_PTR(err);
860ccdc28f8SStanislav Kinsbursky }
861ccdc28f8SStanislav Kinsbursky 
86219172284STrond Myklebust static void __gss_pipe_free(struct gss_pipe *p)
863ccdc28f8SStanislav Kinsbursky {
86419172284STrond Myklebust 	struct rpc_clnt *clnt = p->clnt;
86519172284STrond Myklebust 	struct net *net = rpc_net_ns(clnt);
866ccdc28f8SStanislav Kinsbursky 
86719172284STrond Myklebust 	rpc_remove_pipe_dir_object(net,
86819172284STrond Myklebust 			&clnt->cl_pipedir_objects,
86919172284STrond Myklebust 			&p->pdo);
87019172284STrond Myklebust 	rpc_destroy_pipe_data(p->pipe);
87119172284STrond Myklebust 	kfree(p);
872ccdc28f8SStanislav Kinsbursky }
873ccdc28f8SStanislav Kinsbursky 
87419172284STrond Myklebust static void gss_pipe_free(struct gss_pipe *p)
875ccdc28f8SStanislav Kinsbursky {
87619172284STrond Myklebust 	if (p != NULL)
87719172284STrond Myklebust 		__gss_pipe_free(p);
878ccdc28f8SStanislav Kinsbursky }
879ccdc28f8SStanislav Kinsbursky 
8801da177e4SLinus Torvalds /*
8811da177e4SLinus Torvalds  * NOTE: we have the opportunity to use different
8821da177e4SLinus Torvalds  * parameters based on the input flavor (which must be a pseudoflavor)
8831da177e4SLinus Torvalds  */
8841da177e4SLinus Torvalds static struct rpc_auth *
885c2190661STrond Myklebust gss_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
8861da177e4SLinus Torvalds {
887c2190661STrond Myklebust 	rpc_authflavor_t flavor = args->pseudoflavor;
8881da177e4SLinus Torvalds 	struct gss_auth *gss_auth;
88919172284STrond Myklebust 	struct gss_pipe *gss_pipe;
8901da177e4SLinus Torvalds 	struct rpc_auth * auth;
8916a19275aSJ. Bruce Fields 	int err = -ENOMEM; /* XXX? */
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 	dprintk("RPC:       creating GSS authenticator for client %p\n", clnt);
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	if (!try_module_get(THIS_MODULE))
8966a19275aSJ. Bruce Fields 		return ERR_PTR(err);
8971da177e4SLinus Torvalds 	if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
8981da177e4SLinus Torvalds 		goto out_dec;
899bd4a3eb1STrond Myklebust 	gss_auth->target_name = NULL;
900c2190661STrond Myklebust 	if (args->target_name) {
901c2190661STrond Myklebust 		gss_auth->target_name = kstrdup(args->target_name, GFP_KERNEL);
902bd4a3eb1STrond Myklebust 		if (gss_auth->target_name == NULL)
903bd4a3eb1STrond Myklebust 			goto err_free;
904bd4a3eb1STrond Myklebust 	}
9051da177e4SLinus Torvalds 	gss_auth->client = clnt;
906e726340aSTrond Myklebust 	gss_auth->net = get_net(rpc_net_ns(clnt));
9076a19275aSJ. Bruce Fields 	err = -EINVAL;
9081da177e4SLinus Torvalds 	gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
9091da177e4SLinus Torvalds 	if (!gss_auth->mech) {
9109b1d75b7STrond Myklebust 		dprintk("RPC:       Pseudoflavor %d not found!\n", flavor);
911e726340aSTrond Myklebust 		goto err_put_net;
9121da177e4SLinus Torvalds 	}
9131da177e4SLinus Torvalds 	gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
914438b6fdeSJ. Bruce Fields 	if (gss_auth->service == 0)
915438b6fdeSJ. Bruce Fields 		goto err_put_mech;
9161da177e4SLinus Torvalds 	auth = &gss_auth->rpc_auth;
9171da177e4SLinus Torvalds 	auth->au_cslack = GSS_CRED_SLACK >> 2;
9181da177e4SLinus Torvalds 	auth->au_rslack = GSS_VERF_SLACK >> 2;
9191da177e4SLinus Torvalds 	auth->au_ops = &authgss_ops;
9201da177e4SLinus Torvalds 	auth->au_flavor = flavor;
9211da177e4SLinus Torvalds 	atomic_set(&auth->au_count, 1);
9220285ed1fSTrond Myklebust 	kref_init(&gss_auth->kref);
9231da177e4SLinus Torvalds 
92419172284STrond Myklebust 	err = rpcauth_init_credcache(auth);
92519172284STrond Myklebust 	if (err)
92619172284STrond Myklebust 		goto err_put_mech;
92734769fc4S\"J. Bruce Fields\ 	/*
92834769fc4S\"J. Bruce Fields\ 	 * Note: if we created the old pipe first, then someone who
92934769fc4S\"J. Bruce Fields\ 	 * examined the directory at the right moment might conclude
93034769fc4S\"J. Bruce Fields\ 	 * that we supported only the old pipe.  So we instead create
93134769fc4S\"J. Bruce Fields\ 	 * the new pipe first.
93234769fc4S\"J. Bruce Fields\ 	 */
93319172284STrond Myklebust 	gss_pipe = gss_pipe_alloc(clnt, "gssd", &gss_upcall_ops_v1);
93419172284STrond Myklebust 	if (IS_ERR(gss_pipe)) {
93519172284STrond Myklebust 		err = PTR_ERR(gss_pipe);
93619172284STrond Myklebust 		goto err_destroy_credcache;
9376a19275aSJ. Bruce Fields 	}
93819172284STrond Myklebust 	gss_auth->gss_pipe[1] = gss_pipe;
9391da177e4SLinus Torvalds 
94019172284STrond Myklebust 	gss_pipe = gss_pipe_alloc(clnt, gss_auth->mech->gm_name,
94119172284STrond Myklebust 			&gss_upcall_ops_v0);
94219172284STrond Myklebust 	if (IS_ERR(gss_pipe)) {
94319172284STrond Myklebust 		err = PTR_ERR(gss_pipe);
944c239d83bSStanislav Kinsbursky 		goto err_destroy_pipe_1;
945c239d83bSStanislav Kinsbursky 	}
94619172284STrond Myklebust 	gss_auth->gss_pipe[0] = gss_pipe;
94707a2bf1dSTrond Myklebust 
9481da177e4SLinus Torvalds 	return auth;
949c239d83bSStanislav Kinsbursky err_destroy_pipe_1:
95019172284STrond Myklebust 	__gss_pipe_free(gss_auth->gss_pipe[1]);
95119172284STrond Myklebust err_destroy_credcache:
95219172284STrond Myklebust 	rpcauth_destroy_credcache(auth);
9531da177e4SLinus Torvalds err_put_mech:
9541da177e4SLinus Torvalds 	gss_mech_put(gss_auth->mech);
955e726340aSTrond Myklebust err_put_net:
956e726340aSTrond Myklebust 	put_net(gss_auth->net);
9571da177e4SLinus Torvalds err_free:
958bd4a3eb1STrond Myklebust 	kfree(gss_auth->target_name);
9591da177e4SLinus Torvalds 	kfree(gss_auth);
9601da177e4SLinus Torvalds out_dec:
9611da177e4SLinus Torvalds 	module_put(THIS_MODULE);
9626a19275aSJ. Bruce Fields 	return ERR_PTR(err);
9631da177e4SLinus Torvalds }
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds static void
9660285ed1fSTrond Myklebust gss_free(struct gss_auth *gss_auth)
9670285ed1fSTrond Myklebust {
96819172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[0]);
96919172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[1]);
9700285ed1fSTrond Myklebust 	gss_mech_put(gss_auth->mech);
971e726340aSTrond Myklebust 	put_net(gss_auth->net);
972bd4a3eb1STrond Myklebust 	kfree(gss_auth->target_name);
9730285ed1fSTrond Myklebust 
9740285ed1fSTrond Myklebust 	kfree(gss_auth);
9750285ed1fSTrond Myklebust 	module_put(THIS_MODULE);
9760285ed1fSTrond Myklebust }
9770285ed1fSTrond Myklebust 
9780285ed1fSTrond Myklebust static void
9790285ed1fSTrond Myklebust gss_free_callback(struct kref *kref)
9800285ed1fSTrond Myklebust {
9810285ed1fSTrond Myklebust 	struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
9820285ed1fSTrond Myklebust 
9830285ed1fSTrond Myklebust 	gss_free(gss_auth);
9840285ed1fSTrond Myklebust }
9850285ed1fSTrond Myklebust 
9860285ed1fSTrond Myklebust static void
9871da177e4SLinus Torvalds gss_destroy(struct rpc_auth *auth)
9881da177e4SLinus Torvalds {
98919172284STrond Myklebust 	struct gss_auth *gss_auth = container_of(auth,
99019172284STrond Myklebust 			struct gss_auth, rpc_auth);
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	dprintk("RPC:       destroying GSS authenticator %p flavor %d\n",
9931da177e4SLinus Torvalds 			auth, auth->au_flavor);
9941da177e4SLinus Torvalds 
99519172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[0]);
99619172284STrond Myklebust 	gss_auth->gss_pipe[0] = NULL;
99719172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[1]);
99819172284STrond Myklebust 	gss_auth->gss_pipe[1] = NULL;
9993ab9bb72STrond Myklebust 	rpcauth_destroy_credcache(auth);
10003ab9bb72STrond Myklebust 
10010285ed1fSTrond Myklebust 	kref_put(&gss_auth->kref, gss_free_callback);
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds 
10040df7fb74STrond Myklebust /*
10050df7fb74STrond Myklebust  * gss_destroying_context will cause the RPCSEC_GSS to send a NULL RPC call
10060df7fb74STrond Myklebust  * to the server with the GSS control procedure field set to
10070df7fb74STrond Myklebust  * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
10080df7fb74STrond Myklebust  * all RPCSEC_GSS state associated with that context.
10090df7fb74STrond Myklebust  */
10100df7fb74STrond Myklebust static int
10110df7fb74STrond Myklebust gss_destroying_context(struct rpc_cred *cred)
10120df7fb74STrond Myklebust {
10130df7fb74STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
10140df7fb74STrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
10150df7fb74STrond Myklebust 	struct rpc_task *task;
10160df7fb74STrond Myklebust 
10170df7fb74STrond Myklebust 	if (gss_cred->gc_ctx == NULL ||
10186dcd3926SJeff Layton 	    test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
10190df7fb74STrond Myklebust 		return 0;
10200df7fb74STrond Myklebust 
10210df7fb74STrond Myklebust 	gss_cred->gc_ctx->gc_proc = RPC_GSS_PROC_DESTROY;
10220df7fb74STrond Myklebust 	cred->cr_ops = &gss_nullops;
10230df7fb74STrond Myklebust 
10240df7fb74STrond Myklebust 	/* Take a reference to ensure the cred will be destroyed either
10250df7fb74STrond Myklebust 	 * by the RPC call or by the put_rpccred() below */
10260df7fb74STrond Myklebust 	get_rpccred(cred);
10270df7fb74STrond Myklebust 
1028080a1f14STrond Myklebust 	task = rpc_call_null(gss_auth->client, cred, RPC_TASK_ASYNC|RPC_TASK_SOFT);
10290df7fb74STrond Myklebust 	if (!IS_ERR(task))
10300df7fb74STrond Myklebust 		rpc_put_task(task);
10310df7fb74STrond Myklebust 
10320df7fb74STrond Myklebust 	put_rpccred(cred);
10330df7fb74STrond Myklebust 	return 1;
10340df7fb74STrond Myklebust }
10350df7fb74STrond Myklebust 
10360df7fb74STrond Myklebust /* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
10371da177e4SLinus Torvalds  * to create a new cred or context, so they check that things have been
10381da177e4SLinus Torvalds  * allocated before freeing them. */
10391da177e4SLinus Torvalds static void
10405d28dc82STrond Myklebust gss_do_free_ctx(struct gss_cl_ctx *ctx)
10411da177e4SLinus Torvalds {
1042632f0d05SChuck Lever 	dprintk("RPC:       %s\n", __func__);
10431da177e4SLinus Torvalds 
10440d8a3746STrond Myklebust 	gss_delete_sec_context(&ctx->gc_gss_ctx);
10451da177e4SLinus Torvalds 	kfree(ctx->gc_wire_ctx.data);
10461da177e4SLinus Torvalds 	kfree(ctx);
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds static void
10505d28dc82STrond Myklebust gss_free_ctx_callback(struct rcu_head *head)
10515d28dc82STrond Myklebust {
10525d28dc82STrond Myklebust 	struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
10535d28dc82STrond Myklebust 	gss_do_free_ctx(ctx);
10545d28dc82STrond Myklebust }
10555d28dc82STrond Myklebust 
10565d28dc82STrond Myklebust static void
10575d28dc82STrond Myklebust gss_free_ctx(struct gss_cl_ctx *ctx)
10585d28dc82STrond Myklebust {
10595d28dc82STrond Myklebust 	call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
10605d28dc82STrond Myklebust }
10615d28dc82STrond Myklebust 
10625d28dc82STrond Myklebust static void
106331be5bf1STrond Myklebust gss_free_cred(struct gss_cred *gss_cred)
10641da177e4SLinus Torvalds {
1065632f0d05SChuck Lever 	dprintk("RPC:       %s cred=%p\n", __func__, gss_cred);
106631be5bf1STrond Myklebust 	kfree(gss_cred);
106731be5bf1STrond Myklebust }
10681da177e4SLinus Torvalds 
106931be5bf1STrond Myklebust static void
107031be5bf1STrond Myklebust gss_free_cred_callback(struct rcu_head *head)
107131be5bf1STrond Myklebust {
107231be5bf1STrond Myklebust 	struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
107331be5bf1STrond Myklebust 	gss_free_cred(gss_cred);
107431be5bf1STrond Myklebust }
10751da177e4SLinus Torvalds 
107631be5bf1STrond Myklebust static void
10776dcd3926SJeff Layton gss_destroy_nullcred(struct rpc_cred *cred)
107831be5bf1STrond Myklebust {
10795d28dc82STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
10800285ed1fSTrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
10815d28dc82STrond Myklebust 	struct gss_cl_ctx *ctx = gss_cred->gc_ctx;
10825d28dc82STrond Myklebust 
1083a9b3cd7fSStephen Hemminger 	RCU_INIT_POINTER(gss_cred->gc_ctx, NULL);
108431be5bf1STrond Myklebust 	call_rcu(&cred->cr_rcu, gss_free_cred_callback);
10855d28dc82STrond Myklebust 	if (ctx)
10865d28dc82STrond Myklebust 		gss_put_ctx(ctx);
10870285ed1fSTrond Myklebust 	kref_put(&gss_auth->kref, gss_free_callback);
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds 
10906dcd3926SJeff Layton static void
10916dcd3926SJeff Layton gss_destroy_cred(struct rpc_cred *cred)
10926dcd3926SJeff Layton {
10936dcd3926SJeff Layton 
10946dcd3926SJeff Layton 	if (gss_destroying_context(cred))
10956dcd3926SJeff Layton 		return;
10966dcd3926SJeff Layton 	gss_destroy_nullcred(cred);
10976dcd3926SJeff Layton }
10986dcd3926SJeff Layton 
10991da177e4SLinus Torvalds /*
11001da177e4SLinus Torvalds  * Lookup RPCSEC_GSS cred for the current process
11011da177e4SLinus Torvalds  */
11021da177e4SLinus Torvalds static struct rpc_cred *
11038a317760STrond Myklebust gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
11041da177e4SLinus Torvalds {
11058a317760STrond Myklebust 	return rpcauth_lookup_credcache(auth, acred, flags);
11061da177e4SLinus Torvalds }
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds static struct rpc_cred *
11098a317760STrond Myklebust gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
11101da177e4SLinus Torvalds {
11111da177e4SLinus Torvalds 	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
11121da177e4SLinus Torvalds 	struct gss_cred	*cred = NULL;
11131da177e4SLinus Torvalds 	int err = -ENOMEM;
11141da177e4SLinus Torvalds 
1115632f0d05SChuck Lever 	dprintk("RPC:       %s for uid %d, flavor %d\n",
1116cdba321eSEric W. Biederman 		__func__, from_kuid(&init_user_ns, acred->uid),
1117cdba321eSEric W. Biederman 		auth->au_flavor);
11181da177e4SLinus Torvalds 
11190f38b873STrond Myklebust 	if (!(cred = kzalloc(sizeof(*cred), GFP_NOFS)))
11201da177e4SLinus Torvalds 		goto out_err;
11211da177e4SLinus Torvalds 
11225fe4755eSTrond Myklebust 	rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
11231da177e4SLinus Torvalds 	/*
11241da177e4SLinus Torvalds 	 * Note: in order to force a call to call_refresh(), we deliberately
11251da177e4SLinus Torvalds 	 * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
11261da177e4SLinus Torvalds 	 */
1127fc432dd9STrond Myklebust 	cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
11281da177e4SLinus Torvalds 	cred->gc_service = gss_auth->service;
112968c97153STrond Myklebust 	cred->gc_principal = NULL;
113068c97153STrond Myklebust 	if (acred->machine_cred)
113168c97153STrond Myklebust 		cred->gc_principal = acred->principal;
11320285ed1fSTrond Myklebust 	kref_get(&gss_auth->kref);
11331da177e4SLinus Torvalds 	return &cred->gc_base;
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds out_err:
1136632f0d05SChuck Lever 	dprintk("RPC:       %s failed with error %d\n", __func__, err);
11371da177e4SLinus Torvalds 	return ERR_PTR(err);
11381da177e4SLinus Torvalds }
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds static int
1141fba3bad4STrond Myklebust gss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
1142fba3bad4STrond Myklebust {
1143fba3bad4STrond Myklebust 	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
1144fba3bad4STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
1145fba3bad4STrond Myklebust 	int err;
1146fba3bad4STrond Myklebust 
1147fba3bad4STrond Myklebust 	do {
1148fba3bad4STrond Myklebust 		err = gss_create_upcall(gss_auth, gss_cred);
1149fba3bad4STrond Myklebust 	} while (err == -EAGAIN);
1150fba3bad4STrond Myklebust 	return err;
1151fba3bad4STrond Myklebust }
1152fba3bad4STrond Myklebust 
1153fba3bad4STrond Myklebust static int
11548a317760STrond Myklebust gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
11551da177e4SLinus Torvalds {
11561da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
11571da177e4SLinus Torvalds 
1158cd019f75STrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
11598a317760STrond Myklebust 		goto out;
11601da177e4SLinus Torvalds 	/* Don't match with creds that have expired. */
1161cd019f75STrond Myklebust 	if (time_after(jiffies, gss_cred->gc_ctx->gc_expiry))
1162cd019f75STrond Myklebust 		return 0;
1163cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags))
11641da177e4SLinus Torvalds 		return 0;
11658a317760STrond Myklebust out:
116668c97153STrond Myklebust 	if (acred->principal != NULL) {
116768c97153STrond Myklebust 		if (gss_cred->gc_principal == NULL)
116868c97153STrond Myklebust 			return 0;
116968c97153STrond Myklebust 		return strcmp(acred->principal, gss_cred->gc_principal) == 0;
117068c97153STrond Myklebust 	}
117168c97153STrond Myklebust 	if (gss_cred->gc_principal != NULL)
11727c67db3aSTrond Myklebust 		return 0;
11730b4d51b0SEric W. Biederman 	return uid_eq(rc->cr_uid, acred->uid);
11741da177e4SLinus Torvalds }
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds /*
11771da177e4SLinus Torvalds * Marshal credentials.
11781da177e4SLinus Torvalds * Maybe we should keep a cached credential for performance reasons.
11791da177e4SLinus Torvalds */
1180d8ed029dSAlexey Dobriyan static __be32 *
1181d8ed029dSAlexey Dobriyan gss_marshal(struct rpc_task *task, __be32 *p)
11821da177e4SLinus Torvalds {
1183a17c2153STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1184a17c2153STrond Myklebust 	struct rpc_cred *cred = req->rq_cred;
11851da177e4SLinus Torvalds 	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
11861da177e4SLinus Torvalds 						 gc_base);
11871da177e4SLinus Torvalds 	struct gss_cl_ctx	*ctx = gss_cred_get_ctx(cred);
1188d8ed029dSAlexey Dobriyan 	__be32		*cred_len;
11891da177e4SLinus Torvalds 	u32             maj_stat = 0;
11901da177e4SLinus Torvalds 	struct xdr_netobj mic;
11911da177e4SLinus Torvalds 	struct kvec	iov;
11921da177e4SLinus Torvalds 	struct xdr_buf	verf_buf;
11931da177e4SLinus Torvalds 
1194632f0d05SChuck Lever 	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	*p++ = htonl(RPC_AUTH_GSS);
11971da177e4SLinus Torvalds 	cred_len = p++;
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 	spin_lock(&ctx->gc_seq_lock);
12001da177e4SLinus Torvalds 	req->rq_seqno = ctx->gc_seq++;
12011da177e4SLinus Torvalds 	spin_unlock(&ctx->gc_seq_lock);
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 	*p++ = htonl((u32) RPC_GSS_VERSION);
12041da177e4SLinus Torvalds 	*p++ = htonl((u32) ctx->gc_proc);
12051da177e4SLinus Torvalds 	*p++ = htonl((u32) req->rq_seqno);
12061da177e4SLinus Torvalds 	*p++ = htonl((u32) gss_cred->gc_service);
12071da177e4SLinus Torvalds 	p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
12081da177e4SLinus Torvalds 	*cred_len = htonl((p - (cred_len + 1)) << 2);
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	/* We compute the checksum for the verifier over the xdr-encoded bytes
12111da177e4SLinus Torvalds 	 * starting with the xid and ending at the end of the credential: */
1212a4f0835cSTrond Myklebust 	iov.iov_base = xprt_skip_transport_header(req->rq_xprt,
1213808012fbSChuck Lever 					req->rq_snd_buf.head[0].iov_base);
12141da177e4SLinus Torvalds 	iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
12151da177e4SLinus Torvalds 	xdr_buf_from_iov(&iov, &verf_buf);
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	/* set verifier flavor*/
12181da177e4SLinus Torvalds 	*p++ = htonl(RPC_AUTH_GSS);
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	mic.data = (u8 *)(p + 1);
122100fd6e14SJ. Bruce Fields 	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
12221da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
1223fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
12241da177e4SLinus Torvalds 	} else if (maj_stat != 0) {
12251da177e4SLinus Torvalds 		printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
12261da177e4SLinus Torvalds 		goto out_put_ctx;
12271da177e4SLinus Torvalds 	}
12281da177e4SLinus Torvalds 	p = xdr_encode_opaque(p, NULL, mic.len);
12291da177e4SLinus Torvalds 	gss_put_ctx(ctx);
12301da177e4SLinus Torvalds 	return p;
12311da177e4SLinus Torvalds out_put_ctx:
12321da177e4SLinus Torvalds 	gss_put_ctx(ctx);
12331da177e4SLinus Torvalds 	return NULL;
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
1236cd019f75STrond Myklebust static int gss_renew_cred(struct rpc_task *task)
1237cd019f75STrond Myklebust {
1238a17c2153STrond Myklebust 	struct rpc_cred *oldcred = task->tk_rqstp->rq_cred;
1239cd019f75STrond Myklebust 	struct gss_cred *gss_cred = container_of(oldcred,
1240cd019f75STrond Myklebust 						 struct gss_cred,
1241cd019f75STrond Myklebust 						 gc_base);
1242cd019f75STrond Myklebust 	struct rpc_auth *auth = oldcred->cr_auth;
1243cd019f75STrond Myklebust 	struct auth_cred acred = {
1244cd019f75STrond Myklebust 		.uid = oldcred->cr_uid,
124568c97153STrond Myklebust 		.principal = gss_cred->gc_principal,
124668c97153STrond Myklebust 		.machine_cred = (gss_cred->gc_principal != NULL ? 1 : 0),
1247cd019f75STrond Myklebust 	};
1248cd019f75STrond Myklebust 	struct rpc_cred *new;
1249cd019f75STrond Myklebust 
1250cd019f75STrond Myklebust 	new = gss_lookup_cred(auth, &acred, RPCAUTH_LOOKUP_NEW);
1251cd019f75STrond Myklebust 	if (IS_ERR(new))
1252cd019f75STrond Myklebust 		return PTR_ERR(new);
1253a17c2153STrond Myklebust 	task->tk_rqstp->rq_cred = new;
1254cd019f75STrond Myklebust 	put_rpccred(oldcred);
1255cd019f75STrond Myklebust 	return 0;
1256cd019f75STrond Myklebust }
1257cd019f75STrond Myklebust 
1258126e216aSTrond Myklebust static int gss_cred_is_negative_entry(struct rpc_cred *cred)
1259126e216aSTrond Myklebust {
1260126e216aSTrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEGATIVE, &cred->cr_flags)) {
1261126e216aSTrond Myklebust 		unsigned long now = jiffies;
1262126e216aSTrond Myklebust 		unsigned long begin, expire;
1263126e216aSTrond Myklebust 		struct gss_cred *gss_cred;
1264126e216aSTrond Myklebust 
1265126e216aSTrond Myklebust 		gss_cred = container_of(cred, struct gss_cred, gc_base);
1266126e216aSTrond Myklebust 		begin = gss_cred->gc_upcall_timestamp;
1267126e216aSTrond Myklebust 		expire = begin + gss_expired_cred_retry_delay * HZ;
1268126e216aSTrond Myklebust 
1269126e216aSTrond Myklebust 		if (time_in_range_open(now, begin, expire))
1270126e216aSTrond Myklebust 			return 1;
1271126e216aSTrond Myklebust 	}
1272126e216aSTrond Myklebust 	return 0;
1273126e216aSTrond Myklebust }
1274126e216aSTrond Myklebust 
12751da177e4SLinus Torvalds /*
12761da177e4SLinus Torvalds * Refresh credentials. XXX - finish
12771da177e4SLinus Torvalds */
12781da177e4SLinus Torvalds static int
12791da177e4SLinus Torvalds gss_refresh(struct rpc_task *task)
12801da177e4SLinus Torvalds {
1281a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
1282cd019f75STrond Myklebust 	int ret = 0;
12831da177e4SLinus Torvalds 
1284126e216aSTrond Myklebust 	if (gss_cred_is_negative_entry(cred))
1285126e216aSTrond Myklebust 		return -EKEYEXPIRED;
1286126e216aSTrond Myklebust 
1287cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
1288cd019f75STrond Myklebust 			!test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags)) {
1289cd019f75STrond Myklebust 		ret = gss_renew_cred(task);
1290cd019f75STrond Myklebust 		if (ret < 0)
1291cd019f75STrond Myklebust 			goto out;
1292a17c2153STrond Myklebust 		cred = task->tk_rqstp->rq_cred;
1293cd019f75STrond Myklebust 	}
1294cd019f75STrond Myklebust 
1295cd019f75STrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
1296cd019f75STrond Myklebust 		ret = gss_refresh_upcall(task);
1297cd019f75STrond Myklebust out:
1298cd019f75STrond Myklebust 	return ret;
12991da177e4SLinus Torvalds }
13001da177e4SLinus Torvalds 
13010df7fb74STrond Myklebust /* Dummy refresh routine: used only when destroying the context */
13020df7fb74STrond Myklebust static int
13030df7fb74STrond Myklebust gss_refresh_null(struct rpc_task *task)
13040df7fb74STrond Myklebust {
13050df7fb74STrond Myklebust 	return -EACCES;
13060df7fb74STrond Myklebust }
13070df7fb74STrond Myklebust 
1308d8ed029dSAlexey Dobriyan static __be32 *
1309d8ed029dSAlexey Dobriyan gss_validate(struct rpc_task *task, __be32 *p)
13101da177e4SLinus Torvalds {
1311a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
13121da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1313d8ed029dSAlexey Dobriyan 	__be32		seq;
13141da177e4SLinus Torvalds 	struct kvec	iov;
13151da177e4SLinus Torvalds 	struct xdr_buf	verf_buf;
13161da177e4SLinus Torvalds 	struct xdr_netobj mic;
13171da177e4SLinus Torvalds 	u32		flav,len;
13181da177e4SLinus Torvalds 	u32		maj_stat;
13191da177e4SLinus Torvalds 
1320632f0d05SChuck Lever 	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	flav = ntohl(*p++);
13231da177e4SLinus Torvalds 	if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
13241da177e4SLinus Torvalds 		goto out_bad;
13251da177e4SLinus Torvalds 	if (flav != RPC_AUTH_GSS)
13261da177e4SLinus Torvalds 		goto out_bad;
13271da177e4SLinus Torvalds 	seq = htonl(task->tk_rqstp->rq_seqno);
13281da177e4SLinus Torvalds 	iov.iov_base = &seq;
13291da177e4SLinus Torvalds 	iov.iov_len = sizeof(seq);
13301da177e4SLinus Torvalds 	xdr_buf_from_iov(&iov, &verf_buf);
13311da177e4SLinus Torvalds 	mic.data = (u8 *)p;
13321da177e4SLinus Torvalds 	mic.len = len;
13331da177e4SLinus Torvalds 
133400fd6e14SJ. Bruce Fields 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
13351da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1336fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
13370df7fb74STrond Myklebust 	if (maj_stat) {
1338632f0d05SChuck Lever 		dprintk("RPC: %5u %s: gss_verify_mic returned error 0x%08x\n",
1339632f0d05SChuck Lever 			task->tk_pid, __func__, maj_stat);
13401da177e4SLinus Torvalds 		goto out_bad;
13410df7fb74STrond Myklebust 	}
134224b2605bSJ. Bruce Fields 	/* We leave it to unwrap to calculate au_rslack. For now we just
134324b2605bSJ. Bruce Fields 	 * calculate the length of the verifier: */
13441be27f36STrond Myklebust 	cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
13451da177e4SLinus Torvalds 	gss_put_ctx(ctx);
1346632f0d05SChuck Lever 	dprintk("RPC: %5u %s: gss_verify_mic succeeded.\n",
1347632f0d05SChuck Lever 			task->tk_pid, __func__);
13481da177e4SLinus Torvalds 	return p + XDR_QUADLEN(len);
13491da177e4SLinus Torvalds out_bad:
13501da177e4SLinus Torvalds 	gss_put_ctx(ctx);
1351632f0d05SChuck Lever 	dprintk("RPC: %5u %s failed.\n", task->tk_pid, __func__);
13521da177e4SLinus Torvalds 	return NULL;
13531da177e4SLinus Torvalds }
13541da177e4SLinus Torvalds 
13559f06c719SChuck Lever static void gss_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
13569f06c719SChuck Lever 				__be32 *p, void *obj)
13579f06c719SChuck Lever {
13589f06c719SChuck Lever 	struct xdr_stream xdr;
13599f06c719SChuck Lever 
13609f06c719SChuck Lever 	xdr_init_encode(&xdr, &rqstp->rq_snd_buf, p);
13619f06c719SChuck Lever 	encode(rqstp, &xdr, obj);
13629f06c719SChuck Lever }
13639f06c719SChuck Lever 
13641da177e4SLinus Torvalds static inline int
13651da177e4SLinus Torvalds gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
13669f06c719SChuck Lever 		   kxdreproc_t encode, struct rpc_rqst *rqstp,
13679f06c719SChuck Lever 		   __be32 *p, void *obj)
13681da177e4SLinus Torvalds {
13691da177e4SLinus Torvalds 	struct xdr_buf	*snd_buf = &rqstp->rq_snd_buf;
13701da177e4SLinus Torvalds 	struct xdr_buf	integ_buf;
1371d8ed029dSAlexey Dobriyan 	__be32          *integ_len = NULL;
13721da177e4SLinus Torvalds 	struct xdr_netobj mic;
1373d8ed029dSAlexey Dobriyan 	u32		offset;
1374d8ed029dSAlexey Dobriyan 	__be32		*q;
13751da177e4SLinus Torvalds 	struct kvec	*iov;
13761da177e4SLinus Torvalds 	u32             maj_stat = 0;
13771da177e4SLinus Torvalds 	int		status = -EIO;
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	integ_len = p++;
13801da177e4SLinus Torvalds 	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
13811da177e4SLinus Torvalds 	*p++ = htonl(rqstp->rq_seqno);
13821da177e4SLinus Torvalds 
13839f06c719SChuck Lever 	gss_wrap_req_encode(encode, rqstp, p, obj);
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	if (xdr_buf_subsegment(snd_buf, &integ_buf,
13861da177e4SLinus Torvalds 				offset, snd_buf->len - offset))
13871da177e4SLinus Torvalds 		return status;
13881da177e4SLinus Torvalds 	*integ_len = htonl(integ_buf.len);
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	/* guess whether we're in the head or the tail: */
13911da177e4SLinus Torvalds 	if (snd_buf->page_len || snd_buf->tail[0].iov_len)
13921da177e4SLinus Torvalds 		iov = snd_buf->tail;
13931da177e4SLinus Torvalds 	else
13941da177e4SLinus Torvalds 		iov = snd_buf->head;
13951da177e4SLinus Torvalds 	p = iov->iov_base + iov->iov_len;
13961da177e4SLinus Torvalds 	mic.data = (u8 *)(p + 1);
13971da177e4SLinus Torvalds 
139800fd6e14SJ. Bruce Fields 	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
13991da177e4SLinus Torvalds 	status = -EIO; /* XXX? */
14001da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1401fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
14021da177e4SLinus Torvalds 	else if (maj_stat)
14031da177e4SLinus Torvalds 		return status;
14041da177e4SLinus Torvalds 	q = xdr_encode_opaque(p, NULL, mic.len);
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 	offset = (u8 *)q - (u8 *)p;
14071da177e4SLinus Torvalds 	iov->iov_len += offset;
14081da177e4SLinus Torvalds 	snd_buf->len += offset;
14091da177e4SLinus Torvalds 	return 0;
14101da177e4SLinus Torvalds }
14111da177e4SLinus Torvalds 
14122d2da60cSJ. Bruce Fields static void
14132d2da60cSJ. Bruce Fields priv_release_snd_buf(struct rpc_rqst *rqstp)
14142d2da60cSJ. Bruce Fields {
14152d2da60cSJ. Bruce Fields 	int i;
14162d2da60cSJ. Bruce Fields 
14172d2da60cSJ. Bruce Fields 	for (i=0; i < rqstp->rq_enc_pages_num; i++)
14182d2da60cSJ. Bruce Fields 		__free_page(rqstp->rq_enc_pages[i]);
14192d2da60cSJ. Bruce Fields 	kfree(rqstp->rq_enc_pages);
14202d2da60cSJ. Bruce Fields }
14212d2da60cSJ. Bruce Fields 
14222d2da60cSJ. Bruce Fields static int
14232d2da60cSJ. Bruce Fields alloc_enc_pages(struct rpc_rqst *rqstp)
14242d2da60cSJ. Bruce Fields {
14252d2da60cSJ. Bruce Fields 	struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
14262d2da60cSJ. Bruce Fields 	int first, last, i;
14272d2da60cSJ. Bruce Fields 
14282d2da60cSJ. Bruce Fields 	if (snd_buf->page_len == 0) {
14292d2da60cSJ. Bruce Fields 		rqstp->rq_enc_pages_num = 0;
14302d2da60cSJ. Bruce Fields 		return 0;
14312d2da60cSJ. Bruce Fields 	}
14322d2da60cSJ. Bruce Fields 
14332d2da60cSJ. Bruce Fields 	first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
14342d2da60cSJ. Bruce Fields 	last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_CACHE_SHIFT;
14352d2da60cSJ. Bruce Fields 	rqstp->rq_enc_pages_num = last - first + 1 + 1;
14362d2da60cSJ. Bruce Fields 	rqstp->rq_enc_pages
14372d2da60cSJ. Bruce Fields 		= kmalloc(rqstp->rq_enc_pages_num * sizeof(struct page *),
14382d2da60cSJ. Bruce Fields 				GFP_NOFS);
14392d2da60cSJ. Bruce Fields 	if (!rqstp->rq_enc_pages)
14402d2da60cSJ. Bruce Fields 		goto out;
14412d2da60cSJ. Bruce Fields 	for (i=0; i < rqstp->rq_enc_pages_num; i++) {
14422d2da60cSJ. Bruce Fields 		rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
14432d2da60cSJ. Bruce Fields 		if (rqstp->rq_enc_pages[i] == NULL)
14442d2da60cSJ. Bruce Fields 			goto out_free;
14452d2da60cSJ. Bruce Fields 	}
14462d2da60cSJ. Bruce Fields 	rqstp->rq_release_snd_buf = priv_release_snd_buf;
14472d2da60cSJ. Bruce Fields 	return 0;
14482d2da60cSJ. Bruce Fields out_free:
1449cdead7cfSTrond Myklebust 	rqstp->rq_enc_pages_num = i;
1450cdead7cfSTrond Myklebust 	priv_release_snd_buf(rqstp);
14512d2da60cSJ. Bruce Fields out:
14522d2da60cSJ. Bruce Fields 	return -EAGAIN;
14532d2da60cSJ. Bruce Fields }
14542d2da60cSJ. Bruce Fields 
14552d2da60cSJ. Bruce Fields static inline int
14562d2da60cSJ. Bruce Fields gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
14579f06c719SChuck Lever 		  kxdreproc_t encode, struct rpc_rqst *rqstp,
14589f06c719SChuck Lever 		  __be32 *p, void *obj)
14592d2da60cSJ. Bruce Fields {
14602d2da60cSJ. Bruce Fields 	struct xdr_buf	*snd_buf = &rqstp->rq_snd_buf;
14612d2da60cSJ. Bruce Fields 	u32		offset;
14622d2da60cSJ. Bruce Fields 	u32             maj_stat;
14632d2da60cSJ. Bruce Fields 	int		status;
1464d8ed029dSAlexey Dobriyan 	__be32		*opaque_len;
14652d2da60cSJ. Bruce Fields 	struct page	**inpages;
14662d2da60cSJ. Bruce Fields 	int		first;
14672d2da60cSJ. Bruce Fields 	int		pad;
14682d2da60cSJ. Bruce Fields 	struct kvec	*iov;
14692d2da60cSJ. Bruce Fields 	char		*tmp;
14702d2da60cSJ. Bruce Fields 
14712d2da60cSJ. Bruce Fields 	opaque_len = p++;
14722d2da60cSJ. Bruce Fields 	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
14732d2da60cSJ. Bruce Fields 	*p++ = htonl(rqstp->rq_seqno);
14742d2da60cSJ. Bruce Fields 
14759f06c719SChuck Lever 	gss_wrap_req_encode(encode, rqstp, p, obj);
14762d2da60cSJ. Bruce Fields 
14772d2da60cSJ. Bruce Fields 	status = alloc_enc_pages(rqstp);
14782d2da60cSJ. Bruce Fields 	if (status)
14792d2da60cSJ. Bruce Fields 		return status;
14802d2da60cSJ. Bruce Fields 	first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
14812d2da60cSJ. Bruce Fields 	inpages = snd_buf->pages + first;
14822d2da60cSJ. Bruce Fields 	snd_buf->pages = rqstp->rq_enc_pages;
14832d2da60cSJ. Bruce Fields 	snd_buf->page_base -= first << PAGE_CACHE_SHIFT;
14847561042fSKevin Coffman 	/*
14857561042fSKevin Coffman 	 * Give the tail its own page, in case we need extra space in the
14867561042fSKevin Coffman 	 * head when wrapping:
14877561042fSKevin Coffman 	 *
14887561042fSKevin Coffman 	 * call_allocate() allocates twice the slack space required
14897561042fSKevin Coffman 	 * by the authentication flavor to rq_callsize.
14907561042fSKevin Coffman 	 * For GSS, slack is GSS_CRED_SLACK.
14917561042fSKevin Coffman 	 */
14922d2da60cSJ. Bruce Fields 	if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
14932d2da60cSJ. Bruce Fields 		tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
14942d2da60cSJ. Bruce Fields 		memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
14952d2da60cSJ. Bruce Fields 		snd_buf->tail[0].iov_base = tmp;
14962d2da60cSJ. Bruce Fields 	}
149700fd6e14SJ. Bruce Fields 	maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
14987561042fSKevin Coffman 	/* slack space should prevent this ever happening: */
14992d2da60cSJ. Bruce Fields 	BUG_ON(snd_buf->len > snd_buf->buflen);
15002d2da60cSJ. Bruce Fields 	status = -EIO;
15012d2da60cSJ. Bruce Fields 	/* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
15022d2da60cSJ. Bruce Fields 	 * done anyway, so it's safe to put the request on the wire: */
15032d2da60cSJ. Bruce Fields 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1504fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
15052d2da60cSJ. Bruce Fields 	else if (maj_stat)
15062d2da60cSJ. Bruce Fields 		return status;
15072d2da60cSJ. Bruce Fields 
15082d2da60cSJ. Bruce Fields 	*opaque_len = htonl(snd_buf->len - offset);
15092d2da60cSJ. Bruce Fields 	/* guess whether we're in the head or the tail: */
15102d2da60cSJ. Bruce Fields 	if (snd_buf->page_len || snd_buf->tail[0].iov_len)
15112d2da60cSJ. Bruce Fields 		iov = snd_buf->tail;
15122d2da60cSJ. Bruce Fields 	else
15132d2da60cSJ. Bruce Fields 		iov = snd_buf->head;
15142d2da60cSJ. Bruce Fields 	p = iov->iov_base + iov->iov_len;
15152d2da60cSJ. Bruce Fields 	pad = 3 - ((snd_buf->len - offset - 1) & 3);
15162d2da60cSJ. Bruce Fields 	memset(p, 0, pad);
15172d2da60cSJ. Bruce Fields 	iov->iov_len += pad;
15182d2da60cSJ. Bruce Fields 	snd_buf->len += pad;
15192d2da60cSJ. Bruce Fields 
15202d2da60cSJ. Bruce Fields 	return 0;
15212d2da60cSJ. Bruce Fields }
15222d2da60cSJ. Bruce Fields 
15231da177e4SLinus Torvalds static int
15241da177e4SLinus Torvalds gss_wrap_req(struct rpc_task *task,
15259f06c719SChuck Lever 	     kxdreproc_t encode, void *rqstp, __be32 *p, void *obj)
15261da177e4SLinus Torvalds {
1527a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
15281da177e4SLinus Torvalds 	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
15291da177e4SLinus Torvalds 			gc_base);
15301da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
15311da177e4SLinus Torvalds 	int             status = -EIO;
15321da177e4SLinus Torvalds 
1533632f0d05SChuck Lever 	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
15341da177e4SLinus Torvalds 	if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
15351da177e4SLinus Torvalds 		/* The spec seems a little ambiguous here, but I think that not
15361da177e4SLinus Torvalds 		 * wrapping context destruction requests makes the most sense.
15371da177e4SLinus Torvalds 		 */
15389f06c719SChuck Lever 		gss_wrap_req_encode(encode, rqstp, p, obj);
15399f06c719SChuck Lever 		status = 0;
15401da177e4SLinus Torvalds 		goto out;
15411da177e4SLinus Torvalds 	}
15421da177e4SLinus Torvalds 	switch (gss_cred->gc_service) {
15431da177e4SLinus Torvalds 	case RPC_GSS_SVC_NONE:
15449f06c719SChuck Lever 		gss_wrap_req_encode(encode, rqstp, p, obj);
15459f06c719SChuck Lever 		status = 0;
15461da177e4SLinus Torvalds 		break;
15471da177e4SLinus Torvalds 	case RPC_GSS_SVC_INTEGRITY:
154889f0e4feSJoe Perches 		status = gss_wrap_req_integ(cred, ctx, encode, rqstp, p, obj);
15491da177e4SLinus Torvalds 		break;
15501da177e4SLinus Torvalds 	case RPC_GSS_SVC_PRIVACY:
155189f0e4feSJoe Perches 		status = gss_wrap_req_priv(cred, ctx, encode, rqstp, p, obj);
15521da177e4SLinus Torvalds 		break;
15531da177e4SLinus Torvalds 	}
15541da177e4SLinus Torvalds out:
15551da177e4SLinus Torvalds 	gss_put_ctx(ctx);
1556632f0d05SChuck Lever 	dprintk("RPC: %5u %s returning %d\n", task->tk_pid, __func__, status);
15571da177e4SLinus Torvalds 	return status;
15581da177e4SLinus Torvalds }
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds static inline int
15611da177e4SLinus Torvalds gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1562d8ed029dSAlexey Dobriyan 		struct rpc_rqst *rqstp, __be32 **p)
15631da177e4SLinus Torvalds {
15641da177e4SLinus Torvalds 	struct xdr_buf	*rcv_buf = &rqstp->rq_rcv_buf;
15651da177e4SLinus Torvalds 	struct xdr_buf integ_buf;
15661da177e4SLinus Torvalds 	struct xdr_netobj mic;
15671da177e4SLinus Torvalds 	u32 data_offset, mic_offset;
15681da177e4SLinus Torvalds 	u32 integ_len;
15691da177e4SLinus Torvalds 	u32 maj_stat;
15701da177e4SLinus Torvalds 	int status = -EIO;
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 	integ_len = ntohl(*(*p)++);
15731da177e4SLinus Torvalds 	if (integ_len & 3)
15741da177e4SLinus Torvalds 		return status;
15751da177e4SLinus Torvalds 	data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
15761da177e4SLinus Torvalds 	mic_offset = integ_len + data_offset;
15771da177e4SLinus Torvalds 	if (mic_offset > rcv_buf->len)
15781da177e4SLinus Torvalds 		return status;
15791da177e4SLinus Torvalds 	if (ntohl(*(*p)++) != rqstp->rq_seqno)
15801da177e4SLinus Torvalds 		return status;
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
15831da177e4SLinus Torvalds 				mic_offset - data_offset))
15841da177e4SLinus Torvalds 		return status;
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
15871da177e4SLinus Torvalds 		return status;
15881da177e4SLinus Torvalds 
158900fd6e14SJ. Bruce Fields 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
15901da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1591fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
15921da177e4SLinus Torvalds 	if (maj_stat != GSS_S_COMPLETE)
15931da177e4SLinus Torvalds 		return status;
15941da177e4SLinus Torvalds 	return 0;
15951da177e4SLinus Torvalds }
15961da177e4SLinus Torvalds 
15972d2da60cSJ. Bruce Fields static inline int
15982d2da60cSJ. Bruce Fields gss_unwrap_resp_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1599d8ed029dSAlexey Dobriyan 		struct rpc_rqst *rqstp, __be32 **p)
16002d2da60cSJ. Bruce Fields {
16012d2da60cSJ. Bruce Fields 	struct xdr_buf  *rcv_buf = &rqstp->rq_rcv_buf;
16022d2da60cSJ. Bruce Fields 	u32 offset;
16032d2da60cSJ. Bruce Fields 	u32 opaque_len;
16042d2da60cSJ. Bruce Fields 	u32 maj_stat;
16052d2da60cSJ. Bruce Fields 	int status = -EIO;
16062d2da60cSJ. Bruce Fields 
16072d2da60cSJ. Bruce Fields 	opaque_len = ntohl(*(*p)++);
16082d2da60cSJ. Bruce Fields 	offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
16092d2da60cSJ. Bruce Fields 	if (offset + opaque_len > rcv_buf->len)
16102d2da60cSJ. Bruce Fields 		return status;
16112d2da60cSJ. Bruce Fields 	/* remove padding: */
16122d2da60cSJ. Bruce Fields 	rcv_buf->len = offset + opaque_len;
16132d2da60cSJ. Bruce Fields 
161400fd6e14SJ. Bruce Fields 	maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
16152d2da60cSJ. Bruce Fields 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1616fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
16172d2da60cSJ. Bruce Fields 	if (maj_stat != GSS_S_COMPLETE)
16182d2da60cSJ. Bruce Fields 		return status;
16192d2da60cSJ. Bruce Fields 	if (ntohl(*(*p)++) != rqstp->rq_seqno)
16202d2da60cSJ. Bruce Fields 		return status;
16212d2da60cSJ. Bruce Fields 
16222d2da60cSJ. Bruce Fields 	return 0;
16232d2da60cSJ. Bruce Fields }
16242d2da60cSJ. Bruce Fields 
1625bf269551SChuck Lever static int
1626bf269551SChuck Lever gss_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
1627bf269551SChuck Lever 		      __be32 *p, void *obj)
1628bf269551SChuck Lever {
1629bf269551SChuck Lever 	struct xdr_stream xdr;
1630bf269551SChuck Lever 
1631bf269551SChuck Lever 	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
1632bf269551SChuck Lever 	return decode(rqstp, &xdr, obj);
1633bf269551SChuck Lever }
16342d2da60cSJ. Bruce Fields 
16351da177e4SLinus Torvalds static int
16361da177e4SLinus Torvalds gss_unwrap_resp(struct rpc_task *task,
1637bf269551SChuck Lever 		kxdrdproc_t decode, void *rqstp, __be32 *p, void *obj)
16381da177e4SLinus Torvalds {
1639a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
16401da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
16411da177e4SLinus Torvalds 			gc_base);
16421da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1643d8ed029dSAlexey Dobriyan 	__be32		*savedp = p;
16442d2da60cSJ. Bruce Fields 	struct kvec	*head = ((struct rpc_rqst *)rqstp)->rq_rcv_buf.head;
16452d2da60cSJ. Bruce Fields 	int		savedlen = head->iov_len;
16461da177e4SLinus Torvalds 	int             status = -EIO;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	if (ctx->gc_proc != RPC_GSS_PROC_DATA)
16491da177e4SLinus Torvalds 		goto out_decode;
16501da177e4SLinus Torvalds 	switch (gss_cred->gc_service) {
16511da177e4SLinus Torvalds 	case RPC_GSS_SVC_NONE:
16521da177e4SLinus Torvalds 		break;
16531da177e4SLinus Torvalds 	case RPC_GSS_SVC_INTEGRITY:
16541da177e4SLinus Torvalds 		status = gss_unwrap_resp_integ(cred, ctx, rqstp, &p);
16551da177e4SLinus Torvalds 		if (status)
16561da177e4SLinus Torvalds 			goto out;
16571da177e4SLinus Torvalds 		break;
16581da177e4SLinus Torvalds 	case RPC_GSS_SVC_PRIVACY:
16592d2da60cSJ. Bruce Fields 		status = gss_unwrap_resp_priv(cred, ctx, rqstp, &p);
16602d2da60cSJ. Bruce Fields 		if (status)
16612d2da60cSJ. Bruce Fields 			goto out;
16621da177e4SLinus Torvalds 		break;
16631da177e4SLinus Torvalds 	}
166424b2605bSJ. Bruce Fields 	/* take into account extra slack for integrity and privacy cases: */
16651be27f36STrond Myklebust 	cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + (p - savedp)
16662d2da60cSJ. Bruce Fields 						+ (savedlen - head->iov_len);
16671da177e4SLinus Torvalds out_decode:
1668bf269551SChuck Lever 	status = gss_unwrap_req_decode(decode, rqstp, p, obj);
16691da177e4SLinus Torvalds out:
16701da177e4SLinus Torvalds 	gss_put_ctx(ctx);
1671632f0d05SChuck Lever 	dprintk("RPC: %5u %s returning %d\n",
1672632f0d05SChuck Lever 		task->tk_pid, __func__, status);
16731da177e4SLinus Torvalds 	return status;
16741da177e4SLinus Torvalds }
16751da177e4SLinus Torvalds 
1676f1c0a861STrond Myklebust static const struct rpc_authops authgss_ops = {
16771da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
16781da177e4SLinus Torvalds 	.au_flavor	= RPC_AUTH_GSS,
16791da177e4SLinus Torvalds 	.au_name	= "RPCSEC_GSS",
16801da177e4SLinus Torvalds 	.create		= gss_create,
16811da177e4SLinus Torvalds 	.destroy	= gss_destroy,
16821da177e4SLinus Torvalds 	.lookup_cred	= gss_lookup_cred,
168380df9d20SStanislav Kinsbursky 	.crcreate	= gss_create_cred,
16846a1a1e34SChuck Lever 	.list_pseudoflavors = gss_mech_list_pseudoflavors,
16859568c5e9SChuck Lever 	.info2flavor	= gss_mech_info2flavor,
1686a77c806fSChuck Lever 	.flavor2info	= gss_mech_flavor2info,
16871da177e4SLinus Torvalds };
16881da177e4SLinus Torvalds 
1689f1c0a861STrond Myklebust static const struct rpc_credops gss_credops = {
16901da177e4SLinus Torvalds 	.cr_name	= "AUTH_GSS",
16911da177e4SLinus Torvalds 	.crdestroy	= gss_destroy_cred,
1692fba3bad4STrond Myklebust 	.cr_init	= gss_cred_init,
16935c691044STrond Myklebust 	.crbind		= rpcauth_generic_bind_cred,
16941da177e4SLinus Torvalds 	.crmatch	= gss_match,
16951da177e4SLinus Torvalds 	.crmarshal	= gss_marshal,
16961da177e4SLinus Torvalds 	.crrefresh	= gss_refresh,
16971da177e4SLinus Torvalds 	.crvalidate	= gss_validate,
16981da177e4SLinus Torvalds 	.crwrap_req	= gss_wrap_req,
16991da177e4SLinus Torvalds 	.crunwrap_resp	= gss_unwrap_resp,
17001da177e4SLinus Torvalds };
17011da177e4SLinus Torvalds 
17020df7fb74STrond Myklebust static const struct rpc_credops gss_nullops = {
17030df7fb74STrond Myklebust 	.cr_name	= "AUTH_GSS",
17046dcd3926SJeff Layton 	.crdestroy	= gss_destroy_nullcred,
17055c691044STrond Myklebust 	.crbind		= rpcauth_generic_bind_cred,
17060df7fb74STrond Myklebust 	.crmatch	= gss_match,
17070df7fb74STrond Myklebust 	.crmarshal	= gss_marshal,
17080df7fb74STrond Myklebust 	.crrefresh	= gss_refresh_null,
17090df7fb74STrond Myklebust 	.crvalidate	= gss_validate,
17100df7fb74STrond Myklebust 	.crwrap_req	= gss_wrap_req,
17110df7fb74STrond Myklebust 	.crunwrap_resp	= gss_unwrap_resp,
17120df7fb74STrond Myklebust };
17130df7fb74STrond Myklebust 
1714b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v0 = {
1715c1225158SPeng Tao 	.upcall		= rpc_pipe_generic_upcall,
17161da177e4SLinus Torvalds 	.downcall	= gss_pipe_downcall,
17171da177e4SLinus Torvalds 	.destroy_msg	= gss_pipe_destroy_msg,
171834769fc4S\"J. Bruce Fields\ 	.open_pipe	= gss_pipe_open_v0,
171934769fc4S\"J. Bruce Fields\ 	.release_pipe	= gss_pipe_release,
172034769fc4S\"J. Bruce Fields\ };
172134769fc4S\"J. Bruce Fields\ 
1722b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v1 = {
1723c1225158SPeng Tao 	.upcall		= rpc_pipe_generic_upcall,
172434769fc4S\"J. Bruce Fields\ 	.downcall	= gss_pipe_downcall,
172534769fc4S\"J. Bruce Fields\ 	.destroy_msg	= gss_pipe_destroy_msg,
172634769fc4S\"J. Bruce Fields\ 	.open_pipe	= gss_pipe_open_v1,
17271da177e4SLinus Torvalds 	.release_pipe	= gss_pipe_release,
17281da177e4SLinus Torvalds };
17291da177e4SLinus Torvalds 
1730a1db410dSStanislav Kinsbursky static __net_init int rpcsec_gss_init_net(struct net *net)
1731a1db410dSStanislav Kinsbursky {
1732a1db410dSStanislav Kinsbursky 	return gss_svc_init_net(net);
1733a1db410dSStanislav Kinsbursky }
1734a1db410dSStanislav Kinsbursky 
1735a1db410dSStanislav Kinsbursky static __net_exit void rpcsec_gss_exit_net(struct net *net)
1736a1db410dSStanislav Kinsbursky {
1737a1db410dSStanislav Kinsbursky 	gss_svc_shutdown_net(net);
1738a1db410dSStanislav Kinsbursky }
1739a1db410dSStanislav Kinsbursky 
1740a1db410dSStanislav Kinsbursky static struct pernet_operations rpcsec_gss_net_ops = {
1741a1db410dSStanislav Kinsbursky 	.init = rpcsec_gss_init_net,
1742a1db410dSStanislav Kinsbursky 	.exit = rpcsec_gss_exit_net,
1743a1db410dSStanislav Kinsbursky };
1744a1db410dSStanislav Kinsbursky 
17451da177e4SLinus Torvalds /*
17461da177e4SLinus Torvalds  * Initialize RPCSEC_GSS module
17471da177e4SLinus Torvalds  */
17481da177e4SLinus Torvalds static int __init init_rpcsec_gss(void)
17491da177e4SLinus Torvalds {
17501da177e4SLinus Torvalds 	int err = 0;
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	err = rpcauth_register(&authgss_ops);
17531da177e4SLinus Torvalds 	if (err)
17541da177e4SLinus Torvalds 		goto out;
17551da177e4SLinus Torvalds 	err = gss_svc_init();
17561da177e4SLinus Torvalds 	if (err)
17571da177e4SLinus Torvalds 		goto out_unregister;
1758a1db410dSStanislav Kinsbursky 	err = register_pernet_subsys(&rpcsec_gss_net_ops);
1759a1db410dSStanislav Kinsbursky 	if (err)
1760a1db410dSStanislav Kinsbursky 		goto out_svc_exit;
176179a3f20bS\"J. Bruce Fields\ 	rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version");
17621da177e4SLinus Torvalds 	return 0;
1763a1db410dSStanislav Kinsbursky out_svc_exit:
1764a1db410dSStanislav Kinsbursky 	gss_svc_shutdown();
17651da177e4SLinus Torvalds out_unregister:
17661da177e4SLinus Torvalds 	rpcauth_unregister(&authgss_ops);
17671da177e4SLinus Torvalds out:
17681da177e4SLinus Torvalds 	return err;
17691da177e4SLinus Torvalds }
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds static void __exit exit_rpcsec_gss(void)
17721da177e4SLinus Torvalds {
1773a1db410dSStanislav Kinsbursky 	unregister_pernet_subsys(&rpcsec_gss_net_ops);
17741da177e4SLinus Torvalds 	gss_svc_shutdown();
17751da177e4SLinus Torvalds 	rpcauth_unregister(&authgss_ops);
1776bf12691dSJesper Dangaard Brouer 	rcu_barrier(); /* Wait for completion of call_rcu()'s */
17771da177e4SLinus Torvalds }
17781da177e4SLinus Torvalds 
177971afa85eSChuck Lever MODULE_ALIAS("rpc-auth-6");
17801da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1781126e216aSTrond Myklebust module_param_named(expired_cred_retry_delay,
1782126e216aSTrond Myklebust 		   gss_expired_cred_retry_delay,
1783126e216aSTrond Myklebust 		   uint, 0644);
1784126e216aSTrond Myklebust MODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until "
1785126e216aSTrond Myklebust 		"the RPC engine retries an expired credential");
1786126e216aSTrond Myklebust 
17871da177e4SLinus Torvalds module_init(init_rpcsec_gss)
17881da177e4SLinus Torvalds module_exit(exit_rpcsec_gss)
1789