xref: /openbmc/linux/net/sunrpc/auth_gss/auth_gss.c (revision 0c77668d)
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>
537c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
54eb6dc19dSTrond Myklebust #include <linux/hashtable.h>
551da177e4SLinus Torvalds 
56abfdbd53STrond Myklebust #include "../netns.h"
57abfdbd53STrond Myklebust 
580c77668dSChuck Lever #include <trace/events/rpcgss.h>
590c77668dSChuck Lever 
60f1c0a861STrond Myklebust static const struct rpc_authops authgss_ops;
611da177e4SLinus Torvalds 
62f1c0a861STrond Myklebust static const struct rpc_credops gss_credops;
630df7fb74STrond Myklebust static const struct rpc_credops gss_nullops;
641da177e4SLinus Torvalds 
65126e216aSTrond Myklebust #define GSS_RETRY_EXPIRED 5
66126e216aSTrond Myklebust static unsigned int gss_expired_cred_retry_delay = GSS_RETRY_EXPIRED;
67126e216aSTrond Myklebust 
684de6caa2SAndy Adamson #define GSS_KEY_EXPIRE_TIMEO 240
694de6caa2SAndy Adamson static unsigned int gss_key_expire_timeo = GSS_KEY_EXPIRE_TIMEO;
704de6caa2SAndy Adamson 
71f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
721da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_AUTH
731da177e4SLinus Torvalds #endif
741da177e4SLinus Torvalds 
75725f2865SKevin Coffman #define GSS_CRED_SLACK		(RPC_MAX_AUTH_SIZE * 2)
761da177e4SLinus Torvalds /* length of a krb5 verifier (48), plus data added before arguments when
771da177e4SLinus Torvalds  * using integrity (two 4-byte integers): */
78adeb8133SOlga Kornievskaia #define GSS_VERF_SLACK		100
791da177e4SLinus Torvalds 
8023c323afSTrond Myklebust static DEFINE_HASHTABLE(gss_auth_hash_table, 4);
81eb6dc19dSTrond Myklebust static DEFINE_SPINLOCK(gss_auth_hash_lock);
82eb6dc19dSTrond Myklebust 
8319172284STrond Myklebust struct gss_pipe {
8419172284STrond Myklebust 	struct rpc_pipe_dir_object pdo;
8519172284STrond Myklebust 	struct rpc_pipe *pipe;
8619172284STrond Myklebust 	struct rpc_clnt *clnt;
8719172284STrond Myklebust 	const char *name;
88414a6295STrond Myklebust 	struct kref kref;
8919172284STrond Myklebust };
9019172284STrond Myklebust 
911da177e4SLinus Torvalds struct gss_auth {
920285ed1fSTrond Myklebust 	struct kref kref;
93eb6dc19dSTrond Myklebust 	struct hlist_node hash;
941da177e4SLinus Torvalds 	struct rpc_auth rpc_auth;
951da177e4SLinus Torvalds 	struct gss_api_mech *mech;
961da177e4SLinus Torvalds 	enum rpc_gss_svc service;
971da177e4SLinus Torvalds 	struct rpc_clnt *client;
98e726340aSTrond Myklebust 	struct net *net;
9934769fc4S\"J. Bruce Fields\ 	/*
10034769fc4S\"J. Bruce Fields\ 	 * There are two upcall pipes; dentry[1], named "gssd", is used
10134769fc4S\"J. Bruce Fields\ 	 * for the new text-based upcall; dentry[0] is named after the
10234769fc4S\"J. Bruce Fields\ 	 * mechanism (for example, "krb5") and exists for
10334769fc4S\"J. Bruce Fields\ 	 * backwards-compatibility with older gssd's.
10434769fc4S\"J. Bruce Fields\ 	 */
10519172284STrond Myklebust 	struct gss_pipe *gss_pipe[2];
106bd4a3eb1STrond Myklebust 	const char *target_name;
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds 
10979a3f20bS\"J. Bruce Fields\ /* pipe_version >= 0 if and only if someone has a pipe open. */
11079a3f20bS\"J. Bruce Fields\ static DEFINE_SPINLOCK(pipe_version_lock);
11179a3f20bS\"J. Bruce Fields\ static struct rpc_wait_queue pipe_version_rpc_waitqueue;
11279a3f20bS\"J. Bruce Fields\ static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue);
1139eb2ddb4STrond Myklebust static void gss_put_auth(struct gss_auth *gss_auth);
114cf81939dS\"J. Bruce Fields\ 
1155d28dc82STrond Myklebust static void gss_free_ctx(struct gss_cl_ctx *);
116b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v0;
117b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v1;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds static inline struct gss_cl_ctx *
1201da177e4SLinus Torvalds gss_get_ctx(struct gss_cl_ctx *ctx)
1211da177e4SLinus Torvalds {
1220fa10472SReshetova, Elena 	refcount_inc(&ctx->count);
1231da177e4SLinus Torvalds 	return ctx;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds static inline void
1271da177e4SLinus Torvalds gss_put_ctx(struct gss_cl_ctx *ctx)
1281da177e4SLinus Torvalds {
1290fa10472SReshetova, Elena 	if (refcount_dec_and_test(&ctx->count))
1305d28dc82STrond Myklebust 		gss_free_ctx(ctx);
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1335d28dc82STrond Myklebust /* gss_cred_set_ctx:
1345d28dc82STrond Myklebust  * called by gss_upcall_callback and gss_create_upcall in order
1355d28dc82STrond Myklebust  * to set the gss context. The actual exchange of an old context
1369beae467SStanislav Kinsbursky  * and a new one is protected by the pipe->lock.
1375d28dc82STrond Myklebust  */
1381da177e4SLinus Torvalds static void
1391da177e4SLinus Torvalds gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1425d28dc82STrond Myklebust 
143cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
144cd019f75STrond Myklebust 		return;
1457b6962b0STrond Myklebust 	gss_get_ctx(ctx);
146cf778b00SEric Dumazet 	rcu_assign_pointer(gss_cred->gc_ctx, ctx);
147fc432dd9STrond Myklebust 	set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
1484e857c58SPeter Zijlstra 	smp_mb__before_atomic();
149fc432dd9STrond Myklebust 	clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds static const void *
1531da177e4SLinus Torvalds simple_get_bytes(const void *p, const void *end, void *res, size_t len)
1541da177e4SLinus Torvalds {
1551da177e4SLinus Torvalds 	const void *q = (const void *)((const char *)p + len);
1561da177e4SLinus Torvalds 	if (unlikely(q > end || q < p))
1571da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
1581da177e4SLinus Torvalds 	memcpy(res, p, len);
1591da177e4SLinus Torvalds 	return q;
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds static inline const void *
1631da177e4SLinus Torvalds simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
1641da177e4SLinus Torvalds {
1651da177e4SLinus Torvalds 	const void *q;
1661da177e4SLinus Torvalds 	unsigned int len;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &len, sizeof(len));
1691da177e4SLinus Torvalds 	if (IS_ERR(p))
1701da177e4SLinus Torvalds 		return p;
1711da177e4SLinus Torvalds 	q = (const void *)((const char *)p + len);
1721da177e4SLinus Torvalds 	if (unlikely(q > end || q < p))
1731da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
1740f38b873STrond Myklebust 	dest->data = kmemdup(p, len, GFP_NOFS);
1751da177e4SLinus Torvalds 	if (unlikely(dest->data == NULL))
1761da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
1771da177e4SLinus Torvalds 	dest->len = len;
1781da177e4SLinus Torvalds 	return q;
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds static struct gss_cl_ctx *
1821da177e4SLinus Torvalds gss_cred_get_ctx(struct rpc_cred *cred)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1851da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = NULL;
1861da177e4SLinus Torvalds 
1875d28dc82STrond Myklebust 	rcu_read_lock();
188c5e6aecdSJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
189c5e6aecdSJeff Layton 	if (ctx)
190c5e6aecdSJeff Layton 		gss_get_ctx(ctx);
1915d28dc82STrond Myklebust 	rcu_read_unlock();
1921da177e4SLinus Torvalds 	return ctx;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds static struct gss_cl_ctx *
1961da177e4SLinus Torvalds gss_alloc_context(void)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
1991da177e4SLinus Torvalds 
2000f38b873STrond Myklebust 	ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
2011da177e4SLinus Torvalds 	if (ctx != NULL) {
2021da177e4SLinus Torvalds 		ctx->gc_proc = RPC_GSS_PROC_DATA;
2031da177e4SLinus Torvalds 		ctx->gc_seq = 1;	/* NetApp 6.4R1 doesn't accept seq. no. 0 */
2041da177e4SLinus Torvalds 		spin_lock_init(&ctx->gc_seq_lock);
2050fa10472SReshetova, Elena 		refcount_set(&ctx->count,1);
2061da177e4SLinus Torvalds 	}
2071da177e4SLinus Torvalds 	return ctx;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds #define GSSD_MIN_TIMEOUT (60 * 60)
2111da177e4SLinus Torvalds static const void *
2121da177e4SLinus Torvalds gss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	const void *q;
2151da177e4SLinus Torvalds 	unsigned int seclen;
2161da177e4SLinus Torvalds 	unsigned int timeout;
217620038f6SAndy Adamson 	unsigned long now = jiffies;
2181da177e4SLinus Torvalds 	u32 window_size;
2191da177e4SLinus Torvalds 	int ret;
2201da177e4SLinus Torvalds 
221620038f6SAndy Adamson 	/* First unsigned int gives the remaining lifetime in seconds of the
222620038f6SAndy Adamson 	 * credential - e.g. the remaining TGT lifetime for Kerberos or
223620038f6SAndy Adamson 	 * the -t value passed to GSSD.
224620038f6SAndy Adamson 	 */
2251da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
2261da177e4SLinus Torvalds 	if (IS_ERR(p))
2271da177e4SLinus Torvalds 		goto err;
2281da177e4SLinus Torvalds 	if (timeout == 0)
2291da177e4SLinus Torvalds 		timeout = GSSD_MIN_TIMEOUT;
230620038f6SAndy Adamson 	ctx->gc_expiry = now + ((unsigned long)timeout * HZ);
231620038f6SAndy Adamson 	/* Sequence number window. Determines the maximum number of
232620038f6SAndy Adamson 	 * simultaneous requests
233620038f6SAndy Adamson 	 */
2341da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
2351da177e4SLinus Torvalds 	if (IS_ERR(p))
2361da177e4SLinus Torvalds 		goto err;
2371da177e4SLinus Torvalds 	ctx->gc_win = window_size;
2381da177e4SLinus Torvalds 	/* gssd signals an error by passing ctx->gc_win = 0: */
2391da177e4SLinus Torvalds 	if (ctx->gc_win == 0) {
240dc5ddce9SJeff Layton 		/*
241dc5ddce9SJeff Layton 		 * in which case, p points to an error code. Anything other
242dc5ddce9SJeff Layton 		 * than -EKEYEXPIRED gets converted to -EACCES.
243dc5ddce9SJeff Layton 		 */
244dc5ddce9SJeff Layton 		p = simple_get_bytes(p, end, &ret, sizeof(ret));
245dc5ddce9SJeff Layton 		if (!IS_ERR(p))
246dc5ddce9SJeff Layton 			p = (ret == -EKEYEXPIRED) ? ERR_PTR(-EKEYEXPIRED) :
247dc5ddce9SJeff Layton 						    ERR_PTR(-EACCES);
2481da177e4SLinus Torvalds 		goto err;
2491da177e4SLinus Torvalds 	}
2501da177e4SLinus Torvalds 	/* copy the opaque wire context */
2511da177e4SLinus Torvalds 	p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
2521da177e4SLinus Torvalds 	if (IS_ERR(p))
2531da177e4SLinus Torvalds 		goto err;
2541da177e4SLinus Torvalds 	/* import the opaque security context */
2551da177e4SLinus Torvalds 	p  = simple_get_bytes(p, end, &seclen, sizeof(seclen));
2561da177e4SLinus Torvalds 	if (IS_ERR(p))
2571da177e4SLinus Torvalds 		goto err;
2581da177e4SLinus Torvalds 	q = (const void *)((const char *)p + seclen);
2591da177e4SLinus Torvalds 	if (unlikely(q > end || q < p)) {
2601da177e4SLinus Torvalds 		p = ERR_PTR(-EFAULT);
2611da177e4SLinus Torvalds 		goto err;
2621da177e4SLinus Torvalds 	}
263400f26b5SSimo Sorce 	ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx, NULL, GFP_NOFS);
2641da177e4SLinus Torvalds 	if (ret < 0) {
2650c77668dSChuck Lever 		trace_rpcgss_import_ctx(ret);
2661da177e4SLinus Torvalds 		p = ERR_PTR(ret);
2671da177e4SLinus Torvalds 		goto err;
2681da177e4SLinus Torvalds 	}
2692004c726SJeff Layton 
2702004c726SJeff Layton 	/* is there any trailing data? */
2712004c726SJeff Layton 	if (q == end) {
2722004c726SJeff Layton 		p = q;
2732004c726SJeff Layton 		goto done;
2742004c726SJeff Layton 	}
2752004c726SJeff Layton 
2762004c726SJeff Layton 	/* pull in acceptor name (if there is one) */
2772004c726SJeff Layton 	p = simple_get_netobj(q, end, &ctx->gc_acceptor);
2782004c726SJeff Layton 	if (IS_ERR(p))
2792004c726SJeff Layton 		goto err;
2802004c726SJeff Layton done:
2810c77668dSChuck Lever 	trace_rpcgss_context(ctx->gc_expiry, now, timeout,
2820c77668dSChuck Lever 			     ctx->gc_acceptor.len, ctx->gc_acceptor.data);
2831da177e4SLinus Torvalds err:
2841da177e4SLinus Torvalds 	return p;
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
287a1a23777SChuck Lever /* XXX: Need some documentation about why UPCALL_BUF_LEN is so small.
288a1a23777SChuck Lever  *	Is user space expecting no more than UPCALL_BUF_LEN bytes?
289a1a23777SChuck Lever  *	Note that there are now _two_ NI_MAXHOST sized data items
290a1a23777SChuck Lever  *	being passed in this string.
291a1a23777SChuck Lever  */
292a1a23777SChuck Lever #define UPCALL_BUF_LEN	256
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds struct gss_upcall_msg {
2957ff13969SReshetova, Elena 	refcount_t count;
2967eaf040bSEric W. Biederman 	kuid_t	uid;
2971da177e4SLinus Torvalds 	struct rpc_pipe_msg msg;
2981da177e4SLinus Torvalds 	struct list_head list;
2991da177e4SLinus Torvalds 	struct gss_auth *auth;
3009beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
3011da177e4SLinus Torvalds 	struct rpc_wait_queue rpc_waitqueue;
3021da177e4SLinus Torvalds 	wait_queue_head_t waitqueue;
3031da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
30434769fc4S\"J. Bruce Fields\ 	char databuf[UPCALL_BUF_LEN];
3051da177e4SLinus Torvalds };
3061da177e4SLinus Torvalds 
3072aed8b47STrond Myklebust static int get_pipe_version(struct net *net)
30879a3f20bS\"J. Bruce Fields\ {
3092aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
31079a3f20bS\"J. Bruce Fields\ 	int ret;
31179a3f20bS\"J. Bruce Fields\ 
31279a3f20bS\"J. Bruce Fields\ 	spin_lock(&pipe_version_lock);
3132aed8b47STrond Myklebust 	if (sn->pipe_version >= 0) {
3142aed8b47STrond Myklebust 		atomic_inc(&sn->pipe_users);
3152aed8b47STrond Myklebust 		ret = sn->pipe_version;
31679a3f20bS\"J. Bruce Fields\ 	} else
31779a3f20bS\"J. Bruce Fields\ 		ret = -EAGAIN;
31879a3f20bS\"J. Bruce Fields\ 	spin_unlock(&pipe_version_lock);
31979a3f20bS\"J. Bruce Fields\ 	return ret;
32079a3f20bS\"J. Bruce Fields\ }
32179a3f20bS\"J. Bruce Fields\ 
3222aed8b47STrond Myklebust static void put_pipe_version(struct net *net)
32379a3f20bS\"J. Bruce Fields\ {
3242aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
3252aed8b47STrond Myklebust 
3262aed8b47STrond Myklebust 	if (atomic_dec_and_lock(&sn->pipe_users, &pipe_version_lock)) {
3272aed8b47STrond Myklebust 		sn->pipe_version = -1;
32879a3f20bS\"J. Bruce Fields\ 		spin_unlock(&pipe_version_lock);
32979a3f20bS\"J. Bruce Fields\ 	}
33079a3f20bS\"J. Bruce Fields\ }
33179a3f20bS\"J. Bruce Fields\ 
3321da177e4SLinus Torvalds static void
3331da177e4SLinus Torvalds gss_release_msg(struct gss_upcall_msg *gss_msg)
3341da177e4SLinus Torvalds {
335e726340aSTrond Myklebust 	struct net *net = gss_msg->auth->net;
3367ff13969SReshetova, Elena 	if (!refcount_dec_and_test(&gss_msg->count))
3371da177e4SLinus Torvalds 		return;
3382aed8b47STrond Myklebust 	put_pipe_version(net);
3391da177e4SLinus Torvalds 	BUG_ON(!list_empty(&gss_msg->list));
3401da177e4SLinus Torvalds 	if (gss_msg->ctx != NULL)
3411da177e4SLinus Torvalds 		gss_put_ctx(gss_msg->ctx);
342f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue);
3439eb2ddb4STrond Myklebust 	gss_put_auth(gss_msg->auth);
3441da177e4SLinus Torvalds 	kfree(gss_msg);
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds static struct gss_upcall_msg *
3489130b8dbSOlga Kornievskaia __gss_find_upcall(struct rpc_pipe *pipe, kuid_t uid, const struct gss_auth *auth)
3491da177e4SLinus Torvalds {
3501da177e4SLinus Torvalds 	struct gss_upcall_msg *pos;
3519beae467SStanislav Kinsbursky 	list_for_each_entry(pos, &pipe->in_downcall, list) {
3520b4d51b0SEric W. Biederman 		if (!uid_eq(pos->uid, uid))
3531da177e4SLinus Torvalds 			continue;
3549130b8dbSOlga Kornievskaia 		if (auth && pos->auth->service != auth->service)
3559130b8dbSOlga Kornievskaia 			continue;
3567ff13969SReshetova, Elena 		refcount_inc(&pos->count);
3571da177e4SLinus Torvalds 		return pos;
3581da177e4SLinus Torvalds 	}
3591da177e4SLinus Torvalds 	return NULL;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
362720b8f2dS\\\"J. Bruce Fields\\\ /* Try to add an upcall to the pipefs queue.
3631da177e4SLinus Torvalds  * If an upcall owned by our uid already exists, then we return a reference
3641da177e4SLinus Torvalds  * to that upcall instead of adding the new upcall.
3651da177e4SLinus Torvalds  */
3661da177e4SLinus Torvalds static inline struct gss_upcall_msg *
367053e324fSSuresh Jayaraman gss_add_msg(struct gss_upcall_msg *gss_msg)
3681da177e4SLinus Torvalds {
3699beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
3701da177e4SLinus Torvalds 	struct gss_upcall_msg *old;
3711da177e4SLinus Torvalds 
3729beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
3739130b8dbSOlga Kornievskaia 	old = __gss_find_upcall(pipe, gss_msg->uid, gss_msg->auth);
3741da177e4SLinus Torvalds 	if (old == NULL) {
3757ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
3769beae467SStanislav Kinsbursky 		list_add(&gss_msg->list, &pipe->in_downcall);
3771da177e4SLinus Torvalds 	} else
3781da177e4SLinus Torvalds 		gss_msg = old;
3799beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
3801da177e4SLinus Torvalds 	return gss_msg;
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds static void
3841da177e4SLinus Torvalds __gss_unhash_msg(struct gss_upcall_msg *gss_msg)
3851da177e4SLinus Torvalds {
3861da177e4SLinus Torvalds 	list_del_init(&gss_msg->list);
3871da177e4SLinus Torvalds 	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
3881da177e4SLinus Torvalds 	wake_up_all(&gss_msg->waitqueue);
3897ff13969SReshetova, Elena 	refcount_dec(&gss_msg->count);
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds static void
3931da177e4SLinus Torvalds gss_unhash_msg(struct gss_upcall_msg *gss_msg)
3941da177e4SLinus Torvalds {
3959beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
3961da177e4SLinus Torvalds 
3973b68aaeaSTrond Myklebust 	if (list_empty(&gss_msg->list))
3983b68aaeaSTrond Myklebust 		return;
3999beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
4003b68aaeaSTrond Myklebust 	if (!list_empty(&gss_msg->list))
4011da177e4SLinus Torvalds 		__gss_unhash_msg(gss_msg);
4029beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds static void
406126e216aSTrond Myklebust gss_handle_downcall_result(struct gss_cred *gss_cred, struct gss_upcall_msg *gss_msg)
407126e216aSTrond Myklebust {
408126e216aSTrond Myklebust 	switch (gss_msg->msg.errno) {
409126e216aSTrond Myklebust 	case 0:
410126e216aSTrond Myklebust 		if (gss_msg->ctx == NULL)
411126e216aSTrond Myklebust 			break;
412126e216aSTrond Myklebust 		clear_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
413126e216aSTrond Myklebust 		gss_cred_set_ctx(&gss_cred->gc_base, gss_msg->ctx);
414126e216aSTrond Myklebust 		break;
415126e216aSTrond Myklebust 	case -EKEYEXPIRED:
416126e216aSTrond Myklebust 		set_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
417126e216aSTrond Myklebust 	}
418126e216aSTrond Myklebust 	gss_cred->gc_upcall_timestamp = jiffies;
419126e216aSTrond Myklebust 	gss_cred->gc_upcall = NULL;
420126e216aSTrond Myklebust 	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
421126e216aSTrond Myklebust }
422126e216aSTrond Myklebust 
423126e216aSTrond Myklebust static void
4241da177e4SLinus Torvalds gss_upcall_callback(struct rpc_task *task)
4251da177e4SLinus Torvalds {
426a17c2153STrond Myklebust 	struct gss_cred *gss_cred = container_of(task->tk_rqstp->rq_cred,
4271da177e4SLinus Torvalds 			struct gss_cred, gc_base);
4281da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
4299beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
4301da177e4SLinus Torvalds 
4319beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
432126e216aSTrond Myklebust 	gss_handle_downcall_result(gss_cred, gss_msg);
4339beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
434126e216aSTrond Myklebust 	task->tk_status = gss_msg->msg.errno;
4351da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
43834769fc4S\"J. Bruce Fields\ static void gss_encode_v0_msg(struct gss_upcall_msg *gss_msg)
43934769fc4S\"J. Bruce Fields\ {
44090602c7bSEric W. Biederman 	uid_t uid = from_kuid(&init_user_ns, gss_msg->uid);
44190602c7bSEric W. Biederman 	memcpy(gss_msg->databuf, &uid, sizeof(uid));
44290602c7bSEric W. Biederman 	gss_msg->msg.data = gss_msg->databuf;
44390602c7bSEric W. Biederman 	gss_msg->msg.len = sizeof(uid);
4449d3a2260STrond Myklebust 
4459d3a2260STrond Myklebust 	BUILD_BUG_ON(sizeof(uid) > sizeof(gss_msg->databuf));
44634769fc4S\"J. Bruce Fields\ }
44734769fc4S\"J. Bruce Fields\ 
4489d3a2260STrond Myklebust static int gss_encode_v1_msg(struct gss_upcall_msg *gss_msg,
449bd4a3eb1STrond Myklebust 				const char *service_name,
450bd4a3eb1STrond Myklebust 				const char *target_name)
45134769fc4S\"J. Bruce Fields\ {
452683ac665STrond Myklebust 	struct gss_api_mech *mech = gss_msg->auth->mech;
4538b1c7bf5SOlga Kornievskaia 	char *p = gss_msg->databuf;
4549d3a2260STrond Myklebust 	size_t buflen = sizeof(gss_msg->databuf);
4559d3a2260STrond Myklebust 	int len;
4568b1c7bf5SOlga Kornievskaia 
4579d3a2260STrond Myklebust 	len = scnprintf(p, buflen, "mech=%s uid=%d", mech->gm_name,
45890602c7bSEric W. Biederman 			from_kuid(&init_user_ns, gss_msg->uid));
4599d3a2260STrond Myklebust 	buflen -= len;
4609d3a2260STrond Myklebust 	p += len;
4619d3a2260STrond Myklebust 	gss_msg->msg.len = len;
462108b833cSChuck Lever 
463108b833cSChuck Lever 	/*
464108b833cSChuck Lever 	 * target= is a full service principal that names the remote
465108b833cSChuck Lever 	 * identity that we are authenticating to.
466108b833cSChuck Lever 	 */
467bd4a3eb1STrond Myklebust 	if (target_name) {
4689d3a2260STrond Myklebust 		len = scnprintf(p, buflen, " target=%s", target_name);
4699d3a2260STrond Myklebust 		buflen -= len;
4708b1c7bf5SOlga Kornievskaia 		p += len;
4718b1c7bf5SOlga Kornievskaia 		gss_msg->msg.len += len;
4728b1c7bf5SOlga Kornievskaia 	}
473108b833cSChuck Lever 
474108b833cSChuck Lever 	/*
475108b833cSChuck Lever 	 * gssd uses service= and srchost= to select a matching key from
476108b833cSChuck Lever 	 * the system's keytab to use as the source principal.
477108b833cSChuck Lever 	 *
478108b833cSChuck Lever 	 * service= is the service name part of the source principal,
479108b833cSChuck Lever 	 * or "*" (meaning choose any).
480108b833cSChuck Lever 	 *
481108b833cSChuck Lever 	 * srchost= is the hostname part of the source principal. When
482108b833cSChuck Lever 	 * not provided, gssd uses the local hostname.
483108b833cSChuck Lever 	 */
484a1a23777SChuck Lever 	if (service_name) {
485a1a23777SChuck Lever 		char *c = strchr(service_name, '@');
486a1a23777SChuck Lever 
487a1a23777SChuck Lever 		if (!c)
488a1a23777SChuck Lever 			len = scnprintf(p, buflen, " service=%s",
489a1a23777SChuck Lever 					service_name);
490a1a23777SChuck Lever 		else
491a1a23777SChuck Lever 			len = scnprintf(p, buflen,
492a1a23777SChuck Lever 					" service=%.*s srchost=%s",
493a1a23777SChuck Lever 					(int)(c - service_name),
494a1a23777SChuck Lever 					service_name, c + 1);
4959d3a2260STrond Myklebust 		buflen -= len;
4962efef708SOlga Kornievskaia 		p += len;
4972efef708SOlga Kornievskaia 		gss_msg->msg.len += len;
4982efef708SOlga Kornievskaia 	}
499108b833cSChuck Lever 
500683ac665STrond Myklebust 	if (mech->gm_upcall_enctypes) {
5019d3a2260STrond Myklebust 		len = scnprintf(p, buflen, " enctypes=%s",
5029d3a2260STrond Myklebust 				mech->gm_upcall_enctypes);
5039d3a2260STrond Myklebust 		buflen -= len;
504683ac665STrond Myklebust 		p += len;
505683ac665STrond Myklebust 		gss_msg->msg.len += len;
506683ac665STrond Myklebust 	}
5070c77668dSChuck Lever 	trace_rpcgss_upcall_msg(gss_msg->databuf);
5089d3a2260STrond Myklebust 	len = scnprintf(p, buflen, "\n");
5099d3a2260STrond Myklebust 	if (len == 0)
5109d3a2260STrond Myklebust 		goto out_overflow;
5118b1c7bf5SOlga Kornievskaia 	gss_msg->msg.len += len;
51234769fc4S\"J. Bruce Fields\ 	gss_msg->msg.data = gss_msg->databuf;
5139d3a2260STrond Myklebust 	return 0;
5149d3a2260STrond Myklebust out_overflow:
5159d3a2260STrond Myklebust 	WARN_ON_ONCE(1);
5169d3a2260STrond Myklebust 	return -ENOMEM;
51734769fc4S\"J. Bruce Fields\ }
51834769fc4S\"J. Bruce Fields\ 
51968c97153STrond Myklebust static struct gss_upcall_msg *
520e726340aSTrond Myklebust gss_alloc_msg(struct gss_auth *gss_auth,
5217eaf040bSEric W. Biederman 		kuid_t uid, const char *service_name)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
52479a3f20bS\"J. Bruce Fields\ 	int vers;
5259d3a2260STrond Myklebust 	int err = -ENOMEM;
5261da177e4SLinus Torvalds 
5270f38b873STrond Myklebust 	gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
528db75b3d6S\"J. Bruce Fields\ 	if (gss_msg == NULL)
5299d3a2260STrond Myklebust 		goto err;
530e726340aSTrond Myklebust 	vers = get_pipe_version(gss_auth->net);
5319d3a2260STrond Myklebust 	err = vers;
5329d3a2260STrond Myklebust 	if (err < 0)
5339d3a2260STrond Myklebust 		goto err_free_msg;
53419172284STrond Myklebust 	gss_msg->pipe = gss_auth->gss_pipe[vers]->pipe;
5351da177e4SLinus Torvalds 	INIT_LIST_HEAD(&gss_msg->list);
5361da177e4SLinus Torvalds 	rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
5371da177e4SLinus Torvalds 	init_waitqueue_head(&gss_msg->waitqueue);
5387ff13969SReshetova, Elena 	refcount_set(&gss_msg->count, 1);
5391da177e4SLinus Torvalds 	gss_msg->uid = uid;
5401da177e4SLinus Torvalds 	gss_msg->auth = gss_auth;
541bd4a3eb1STrond Myklebust 	switch (vers) {
542bd4a3eb1STrond Myklebust 	case 0:
543bd4a3eb1STrond Myklebust 		gss_encode_v0_msg(gss_msg);
5445fccc5b5STrond Myklebust 		break;
545bd4a3eb1STrond Myklebust 	default:
5469d3a2260STrond Myklebust 		err = gss_encode_v1_msg(gss_msg, service_name, gss_auth->target_name);
5479d3a2260STrond Myklebust 		if (err)
548e9776d0fSTrond Myklebust 			goto err_put_pipe_version;
54907d53ae4Szhong jiang 	}
5509eb2ddb4STrond Myklebust 	kref_get(&gss_auth->kref);
5511da177e4SLinus Torvalds 	return gss_msg;
552e9776d0fSTrond Myklebust err_put_pipe_version:
553e9776d0fSTrond Myklebust 	put_pipe_version(gss_auth->net);
5549d3a2260STrond Myklebust err_free_msg:
5559d3a2260STrond Myklebust 	kfree(gss_msg);
5569d3a2260STrond Myklebust err:
5579d3a2260STrond Myklebust 	return ERR_PTR(err);
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds static struct gss_upcall_msg *
561e726340aSTrond Myklebust gss_setup_upcall(struct gss_auth *gss_auth, struct rpc_cred *cred)
5621da177e4SLinus Torvalds {
5637c67db3aSTrond Myklebust 	struct gss_cred *gss_cred = container_of(cred,
5647c67db3aSTrond Myklebust 			struct gss_cred, gc_base);
5651da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_new, *gss_msg;
56604d1532bSNeilBrown 	kuid_t uid = cred->cr_cred->fsuid;
5671da177e4SLinus Torvalds 
568e726340aSTrond Myklebust 	gss_new = gss_alloc_msg(gss_auth, uid, gss_cred->gc_principal);
569db75b3d6S\"J. Bruce Fields\ 	if (IS_ERR(gss_new))
570db75b3d6S\"J. Bruce Fields\ 		return gss_new;
571053e324fSSuresh Jayaraman 	gss_msg = gss_add_msg(gss_new);
5721da177e4SLinus Torvalds 	if (gss_msg == gss_new) {
5731cded9d2SNeilBrown 		int res;
5747ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
5751cded9d2SNeilBrown 		res = rpc_queue_upcall(gss_new->pipe, &gss_new->msg);
5761da177e4SLinus Torvalds 		if (res) {
5771da177e4SLinus Torvalds 			gss_unhash_msg(gss_new);
5787ff13969SReshetova, Elena 			refcount_dec(&gss_msg->count);
5791cded9d2SNeilBrown 			gss_release_msg(gss_new);
5801da177e4SLinus Torvalds 			gss_msg = ERR_PTR(res);
5811da177e4SLinus Torvalds 		}
5821da177e4SLinus Torvalds 	} else
5831da177e4SLinus Torvalds 		gss_release_msg(gss_new);
5841da177e4SLinus Torvalds 	return gss_msg;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
587b03568c3S\"J. Bruce Fields\ static void warn_gssd(void)
588b03568c3S\"J. Bruce Fields\ {
5890ea9de0eSJeff Layton 	dprintk("AUTH_GSS upcall failed. Please check user daemon is running.\n");
590b03568c3S\"J. Bruce Fields\ }
591b03568c3S\"J. Bruce Fields\ 
5921da177e4SLinus Torvalds static inline int
5931da177e4SLinus Torvalds gss_refresh_upcall(struct rpc_task *task)
5941da177e4SLinus Torvalds {
595a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
5964a8c1344STrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth,
5971da177e4SLinus Torvalds 			struct gss_auth, rpc_auth);
5981da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred,
5991da177e4SLinus Torvalds 			struct gss_cred, gc_base);
6001da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
6019beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
6021da177e4SLinus Torvalds 	int err = 0;
6031da177e4SLinus Torvalds 
604e726340aSTrond Myklebust 	gss_msg = gss_setup_upcall(gss_auth, cred);
605480e3243SRoel Kluin 	if (PTR_ERR(gss_msg) == -EAGAIN) {
60679a3f20bS\"J. Bruce Fields\ 		/* XXX: warning on the first, under the assumption we
60779a3f20bS\"J. Bruce Fields\ 		 * shouldn't normally hit this case on a refresh. */
60879a3f20bS\"J. Bruce Fields\ 		warn_gssd();
60979a3f20bS\"J. Bruce Fields\ 		task->tk_timeout = 15*HZ;
61079a3f20bS\"J. Bruce Fields\ 		rpc_sleep_on(&pipe_version_rpc_waitqueue, task, NULL);
6110c77668dSChuck Lever 		err = -EAGAIN;
6120c77668dSChuck Lever 		goto out;
61379a3f20bS\"J. Bruce Fields\ 	}
6141da177e4SLinus Torvalds 	if (IS_ERR(gss_msg)) {
6151da177e4SLinus Torvalds 		err = PTR_ERR(gss_msg);
6161da177e4SLinus Torvalds 		goto out;
6171da177e4SLinus Torvalds 	}
6189beae467SStanislav Kinsbursky 	pipe = gss_msg->pipe;
6199beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
6201da177e4SLinus Torvalds 	if (gss_cred->gc_upcall != NULL)
6215d00837bSTrond Myklebust 		rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL);
622126e216aSTrond Myklebust 	else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
6231da177e4SLinus Torvalds 		task->tk_timeout = 0;
6241da177e4SLinus Torvalds 		gss_cred->gc_upcall = gss_msg;
6251da177e4SLinus Torvalds 		/* gss_upcall_callback will release the reference to gss_upcall_msg */
6267ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
6275d00837bSTrond Myklebust 		rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback);
628126e216aSTrond Myklebust 	} else {
629126e216aSTrond Myklebust 		gss_handle_downcall_result(gss_cred, gss_msg);
6301da177e4SLinus Torvalds 		err = gss_msg->msg.errno;
631126e216aSTrond Myklebust 	}
6329beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
6331da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
6341da177e4SLinus Torvalds out:
6350c77668dSChuck Lever 	trace_rpcgss_upcall_result(from_kuid(&init_user_ns,
6360c77668dSChuck Lever 					     cred->cr_cred->fsuid), err);
6371da177e4SLinus Torvalds 	return err;
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds static inline int
6411da177e4SLinus Torvalds gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
6421da177e4SLinus Torvalds {
643e726340aSTrond Myklebust 	struct net *net = gss_auth->net;
644abfdbd53STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
6459beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
6461da177e4SLinus Torvalds 	struct rpc_cred *cred = &gss_cred->gc_base;
6471da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
6481da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
649d36ccb9cSTrond Myklebust 	int err;
6501da177e4SLinus Torvalds 
65179a3f20bS\"J. Bruce Fields\ retry:
652d36ccb9cSTrond Myklebust 	err = 0;
65389f84243SJeff Layton 	/* if gssd is down, just skip upcalling altogether */
65489f84243SJeff Layton 	if (!gssd_running(net)) {
65589f84243SJeff Layton 		warn_gssd();
6560c77668dSChuck Lever 		err = -EACCES;
6570c77668dSChuck Lever 		goto out;
65889f84243SJeff Layton 	}
659e726340aSTrond Myklebust 	gss_msg = gss_setup_upcall(gss_auth, cred);
66079a3f20bS\"J. Bruce Fields\ 	if (PTR_ERR(gss_msg) == -EAGAIN) {
66179a3f20bS\"J. Bruce Fields\ 		err = wait_event_interruptible_timeout(pipe_version_waitqueue,
66289f84243SJeff Layton 				sn->pipe_version >= 0, 15 * HZ);
6632aed8b47STrond Myklebust 		if (sn->pipe_version < 0) {
664d1a8016aSBryan Schumaker 			warn_gssd();
665d1a8016aSBryan Schumaker 			err = -EACCES;
666d1a8016aSBryan Schumaker 		}
667d36ccb9cSTrond Myklebust 		if (err < 0)
66879a3f20bS\"J. Bruce Fields\ 			goto out;
66979a3f20bS\"J. Bruce Fields\ 		goto retry;
67079a3f20bS\"J. Bruce Fields\ 	}
6711da177e4SLinus Torvalds 	if (IS_ERR(gss_msg)) {
6721da177e4SLinus Torvalds 		err = PTR_ERR(gss_msg);
6731da177e4SLinus Torvalds 		goto out;
6741da177e4SLinus Torvalds 	}
6759beae467SStanislav Kinsbursky 	pipe = gss_msg->pipe;
6761da177e4SLinus Torvalds 	for (;;) {
6775afa9133STrond Myklebust 		prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_KILLABLE);
6789beae467SStanislav Kinsbursky 		spin_lock(&pipe->lock);
6791da177e4SLinus Torvalds 		if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
6801da177e4SLinus Torvalds 			break;
6811da177e4SLinus Torvalds 		}
6829beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
6835afa9133STrond Myklebust 		if (fatal_signal_pending(current)) {
6841da177e4SLinus Torvalds 			err = -ERESTARTSYS;
6851da177e4SLinus Torvalds 			goto out_intr;
6861da177e4SLinus Torvalds 		}
6871da177e4SLinus Torvalds 		schedule();
6881da177e4SLinus Torvalds 	}
6891da177e4SLinus Torvalds 	if (gss_msg->ctx)
6907b6962b0STrond Myklebust 		gss_cred_set_ctx(cred, gss_msg->ctx);
6911da177e4SLinus Torvalds 	else
6921da177e4SLinus Torvalds 		err = gss_msg->msg.errno;
6939beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
6941da177e4SLinus Torvalds out_intr:
6951da177e4SLinus Torvalds 	finish_wait(&gss_msg->waitqueue, &wait);
6961da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
6971da177e4SLinus Torvalds out:
6980c77668dSChuck Lever 	trace_rpcgss_upcall_result(from_kuid(&init_user_ns,
6990c77668dSChuck Lever 					     cred->cr_cred->fsuid), err);
7001da177e4SLinus Torvalds 	return err;
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds #define MSG_BUF_MAXSIZE 1024
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds static ssize_t
7061da177e4SLinus Torvalds gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	const void *p, *end;
7091da177e4SLinus Torvalds 	void *buf;
7101da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
711496ad9aaSAl Viro 	struct rpc_pipe *pipe = RPC_I(file_inode(filp))->pipe;
7121da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
71390602c7bSEric W. Biederman 	uid_t id;
71490602c7bSEric W. Biederman 	kuid_t uid;
7153b68aaeaSTrond Myklebust 	ssize_t err = -EFBIG;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	if (mlen > MSG_BUF_MAXSIZE)
7181da177e4SLinus Torvalds 		goto out;
7191da177e4SLinus Torvalds 	err = -ENOMEM;
7200f38b873STrond Myklebust 	buf = kmalloc(mlen, GFP_NOFS);
7211da177e4SLinus Torvalds 	if (!buf)
7221da177e4SLinus Torvalds 		goto out;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	err = -EFAULT;
7251da177e4SLinus Torvalds 	if (copy_from_user(buf, src, mlen))
7261da177e4SLinus Torvalds 		goto err;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	end = (const void *)((char *)buf + mlen);
72990602c7bSEric W. Biederman 	p = simple_get_bytes(buf, end, &id, sizeof(id));
7301da177e4SLinus Torvalds 	if (IS_ERR(p)) {
7311da177e4SLinus Torvalds 		err = PTR_ERR(p);
7321da177e4SLinus Torvalds 		goto err;
7331da177e4SLinus Torvalds 	}
7341da177e4SLinus Torvalds 
73590602c7bSEric W. Biederman 	uid = make_kuid(&init_user_ns, id);
73690602c7bSEric W. Biederman 	if (!uid_valid(uid)) {
73790602c7bSEric W. Biederman 		err = -EINVAL;
73890602c7bSEric W. Biederman 		goto err;
73990602c7bSEric W. Biederman 	}
74090602c7bSEric W. Biederman 
7411da177e4SLinus Torvalds 	err = -ENOMEM;
7421da177e4SLinus Torvalds 	ctx = gss_alloc_context();
7431da177e4SLinus Torvalds 	if (ctx == NULL)
7441da177e4SLinus Torvalds 		goto err;
7453b68aaeaSTrond Myklebust 
7463b68aaeaSTrond Myklebust 	err = -ENOENT;
7473b68aaeaSTrond Myklebust 	/* Find a matching upcall */
7489beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
7499130b8dbSOlga Kornievskaia 	gss_msg = __gss_find_upcall(pipe, uid, NULL);
7503b68aaeaSTrond Myklebust 	if (gss_msg == NULL) {
7519beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
7523b68aaeaSTrond Myklebust 		goto err_put_ctx;
7533b68aaeaSTrond Myklebust 	}
7543b68aaeaSTrond Myklebust 	list_del_init(&gss_msg->list);
7559beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
7563b68aaeaSTrond Myklebust 
7576e84c7b6STrond Myklebust 	p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
7581da177e4SLinus Torvalds 	if (IS_ERR(p)) {
7591da177e4SLinus Torvalds 		err = PTR_ERR(p);
760486bad2eSJeff Layton 		switch (err) {
761486bad2eSJeff Layton 		case -EACCES:
762dc5ddce9SJeff Layton 		case -EKEYEXPIRED:
763486bad2eSJeff Layton 			gss_msg->msg.errno = err;
764486bad2eSJeff Layton 			err = mlen;
765486bad2eSJeff Layton 			break;
766486bad2eSJeff Layton 		case -EFAULT:
767486bad2eSJeff Layton 		case -ENOMEM:
768486bad2eSJeff Layton 		case -EINVAL:
769486bad2eSJeff Layton 		case -ENOSYS:
770486bad2eSJeff Layton 			gss_msg->msg.errno = -EAGAIN;
771486bad2eSJeff Layton 			break;
772486bad2eSJeff Layton 		default:
773486bad2eSJeff Layton 			printk(KERN_CRIT "%s: bad return from "
7746c853099SRandy Dunlap 				"gss_fill_context: %zd\n", __func__, err);
775437b300cSScott Mayhew 			gss_msg->msg.errno = -EIO;
776486bad2eSJeff Layton 		}
7773b68aaeaSTrond Myklebust 		goto err_release_msg;
7781da177e4SLinus Torvalds 	}
7791da177e4SLinus Torvalds 	gss_msg->ctx = gss_get_ctx(ctx);
7803b68aaeaSTrond Myklebust 	err = mlen;
7813b68aaeaSTrond Myklebust 
7823b68aaeaSTrond Myklebust err_release_msg:
7839beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
7841da177e4SLinus Torvalds 	__gss_unhash_msg(gss_msg);
7859beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
7861da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
7871da177e4SLinus Torvalds err_put_ctx:
7881da177e4SLinus Torvalds 	gss_put_ctx(ctx);
7891da177e4SLinus Torvalds err:
7901da177e4SLinus Torvalds 	kfree(buf);
7911da177e4SLinus Torvalds out:
7921da177e4SLinus Torvalds 	return err;
7931da177e4SLinus Torvalds }
7941da177e4SLinus Torvalds 
79534769fc4S\"J. Bruce Fields\ static int gss_pipe_open(struct inode *inode, int new_version)
796cf81939dS\"J. Bruce Fields\ {
7972aed8b47STrond Myklebust 	struct net *net = inode->i_sb->s_fs_info;
7982aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
79934769fc4S\"J. Bruce Fields\ 	int ret = 0;
80034769fc4S\"J. Bruce Fields\ 
80179a3f20bS\"J. Bruce Fields\ 	spin_lock(&pipe_version_lock);
8022aed8b47STrond Myklebust 	if (sn->pipe_version < 0) {
80334769fc4S\"J. Bruce Fields\ 		/* First open of any gss pipe determines the version: */
8042aed8b47STrond Myklebust 		sn->pipe_version = new_version;
80579a3f20bS\"J. Bruce Fields\ 		rpc_wake_up(&pipe_version_rpc_waitqueue);
80679a3f20bS\"J. Bruce Fields\ 		wake_up(&pipe_version_waitqueue);
8072aed8b47STrond Myklebust 	} else if (sn->pipe_version != new_version) {
80834769fc4S\"J. Bruce Fields\ 		/* Trying to open a pipe of a different version */
80934769fc4S\"J. Bruce Fields\ 		ret = -EBUSY;
81034769fc4S\"J. Bruce Fields\ 		goto out;
81179a3f20bS\"J. Bruce Fields\ 	}
8122aed8b47STrond Myklebust 	atomic_inc(&sn->pipe_users);
81334769fc4S\"J. Bruce Fields\ out:
81479a3f20bS\"J. Bruce Fields\ 	spin_unlock(&pipe_version_lock);
81534769fc4S\"J. Bruce Fields\ 	return ret;
81634769fc4S\"J. Bruce Fields\ 
81734769fc4S\"J. Bruce Fields\ }
81834769fc4S\"J. Bruce Fields\ 
81934769fc4S\"J. Bruce Fields\ static int gss_pipe_open_v0(struct inode *inode)
82034769fc4S\"J. Bruce Fields\ {
82134769fc4S\"J. Bruce Fields\ 	return gss_pipe_open(inode, 0);
82234769fc4S\"J. Bruce Fields\ }
82334769fc4S\"J. Bruce Fields\ 
82434769fc4S\"J. Bruce Fields\ static int gss_pipe_open_v1(struct inode *inode)
82534769fc4S\"J. Bruce Fields\ {
82634769fc4S\"J. Bruce Fields\ 	return gss_pipe_open(inode, 1);
827cf81939dS\"J. Bruce Fields\ }
828cf81939dS\"J. Bruce Fields\ 
8291da177e4SLinus Torvalds static void
8301da177e4SLinus Torvalds gss_pipe_release(struct inode *inode)
8311da177e4SLinus Torvalds {
8322aed8b47STrond Myklebust 	struct net *net = inode->i_sb->s_fs_info;
8339beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
8341da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
8351da177e4SLinus Torvalds 
8365a67657aSTrond Myklebust restart:
8379beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
8389beae467SStanislav Kinsbursky 	list_for_each_entry(gss_msg, &pipe->in_downcall, list) {
8396e84c7b6STrond Myklebust 
8405a67657aSTrond Myklebust 		if (!list_empty(&gss_msg->msg.list))
8415a67657aSTrond Myklebust 			continue;
8421da177e4SLinus Torvalds 		gss_msg->msg.errno = -EPIPE;
8437ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
8441da177e4SLinus Torvalds 		__gss_unhash_msg(gss_msg);
8459beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
8461da177e4SLinus Torvalds 		gss_release_msg(gss_msg);
8475a67657aSTrond Myklebust 		goto restart;
8481da177e4SLinus Torvalds 	}
8499beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
850cf81939dS\"J. Bruce Fields\ 
8512aed8b47STrond Myklebust 	put_pipe_version(net);
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds static void
8551da177e4SLinus Torvalds gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
8561da177e4SLinus Torvalds {
8571da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	if (msg->errno < 0) {
8607ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
8611da177e4SLinus Torvalds 		gss_unhash_msg(gss_msg);
862b03568c3S\"J. Bruce Fields\ 		if (msg->errno == -ETIMEDOUT)
863b03568c3S\"J. Bruce Fields\ 			warn_gssd();
8641da177e4SLinus Torvalds 		gss_release_msg(gss_msg);
8651da177e4SLinus Torvalds 	}
8661cded9d2SNeilBrown 	gss_release_msg(gss_msg);
8671da177e4SLinus Torvalds }
8681da177e4SLinus Torvalds 
86919172284STrond Myklebust static void gss_pipe_dentry_destroy(struct dentry *dir,
87019172284STrond Myklebust 		struct rpc_pipe_dir_object *pdo)
871ccdc28f8SStanislav Kinsbursky {
87219172284STrond Myklebust 	struct gss_pipe *gss_pipe = pdo->pdo_data;
87319172284STrond Myklebust 	struct rpc_pipe *pipe = gss_pipe->pipe;
874ccdc28f8SStanislav Kinsbursky 
87519172284STrond Myklebust 	if (pipe->dentry != NULL) {
87619172284STrond Myklebust 		rpc_unlink(pipe->dentry);
87719172284STrond Myklebust 		pipe->dentry = NULL;
8786b2fddd3STrond Myklebust 	}
879ccdc28f8SStanislav Kinsbursky }
880ccdc28f8SStanislav Kinsbursky 
88119172284STrond Myklebust static int gss_pipe_dentry_create(struct dentry *dir,
88219172284STrond Myklebust 		struct rpc_pipe_dir_object *pdo)
883ccdc28f8SStanislav Kinsbursky {
88419172284STrond Myklebust 	struct gss_pipe *p = pdo->pdo_data;
8856b2fddd3STrond Myklebust 	struct dentry *dentry;
886ccdc28f8SStanislav Kinsbursky 
88719172284STrond Myklebust 	dentry = rpc_mkpipe_dentry(dir, p->name, p->clnt, p->pipe);
88819172284STrond Myklebust 	if (IS_ERR(dentry))
88919172284STrond Myklebust 		return PTR_ERR(dentry);
89019172284STrond Myklebust 	p->pipe->dentry = dentry;
891ccdc28f8SStanislav Kinsbursky 	return 0;
89219172284STrond Myklebust }
893ccdc28f8SStanislav Kinsbursky 
89419172284STrond Myklebust static const struct rpc_pipe_dir_object_ops gss_pipe_dir_object_ops = {
89519172284STrond Myklebust 	.create = gss_pipe_dentry_create,
89619172284STrond Myklebust 	.destroy = gss_pipe_dentry_destroy,
89719172284STrond Myklebust };
89819172284STrond Myklebust 
89919172284STrond Myklebust static struct gss_pipe *gss_pipe_alloc(struct rpc_clnt *clnt,
90019172284STrond Myklebust 		const char *name,
90119172284STrond Myklebust 		const struct rpc_pipe_ops *upcall_ops)
90219172284STrond Myklebust {
90319172284STrond Myklebust 	struct gss_pipe *p;
90419172284STrond Myklebust 	int err = -ENOMEM;
90519172284STrond Myklebust 
90619172284STrond Myklebust 	p = kmalloc(sizeof(*p), GFP_KERNEL);
90719172284STrond Myklebust 	if (p == NULL)
90819172284STrond Myklebust 		goto err;
90919172284STrond Myklebust 	p->pipe = rpc_mkpipe_data(upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
91019172284STrond Myklebust 	if (IS_ERR(p->pipe)) {
91119172284STrond Myklebust 		err = PTR_ERR(p->pipe);
91219172284STrond Myklebust 		goto err_free_gss_pipe;
91319172284STrond Myklebust 	}
91419172284STrond Myklebust 	p->name = name;
91519172284STrond Myklebust 	p->clnt = clnt;
916414a6295STrond Myklebust 	kref_init(&p->kref);
91719172284STrond Myklebust 	rpc_init_pipe_dir_object(&p->pdo,
91819172284STrond Myklebust 			&gss_pipe_dir_object_ops,
91919172284STrond Myklebust 			p);
92019172284STrond Myklebust 	return p;
92119172284STrond Myklebust err_free_gss_pipe:
92219172284STrond Myklebust 	kfree(p);
9236b2fddd3STrond Myklebust err:
92419172284STrond Myklebust 	return ERR_PTR(err);
925ccdc28f8SStanislav Kinsbursky }
926ccdc28f8SStanislav Kinsbursky 
927414a6295STrond Myklebust struct gss_alloc_pdo {
928414a6295STrond Myklebust 	struct rpc_clnt *clnt;
929414a6295STrond Myklebust 	const char *name;
930414a6295STrond Myklebust 	const struct rpc_pipe_ops *upcall_ops;
931414a6295STrond Myklebust };
932414a6295STrond Myklebust 
933414a6295STrond Myklebust static int gss_pipe_match_pdo(struct rpc_pipe_dir_object *pdo, void *data)
934414a6295STrond Myklebust {
935414a6295STrond Myklebust 	struct gss_pipe *gss_pipe;
936414a6295STrond Myklebust 	struct gss_alloc_pdo *args = data;
937414a6295STrond Myklebust 
938414a6295STrond Myklebust 	if (pdo->pdo_ops != &gss_pipe_dir_object_ops)
939414a6295STrond Myklebust 		return 0;
940414a6295STrond Myklebust 	gss_pipe = container_of(pdo, struct gss_pipe, pdo);
941414a6295STrond Myklebust 	if (strcmp(gss_pipe->name, args->name) != 0)
942414a6295STrond Myklebust 		return 0;
943414a6295STrond Myklebust 	if (!kref_get_unless_zero(&gss_pipe->kref))
944414a6295STrond Myklebust 		return 0;
945414a6295STrond Myklebust 	return 1;
946414a6295STrond Myklebust }
947414a6295STrond Myklebust 
948414a6295STrond Myklebust static struct rpc_pipe_dir_object *gss_pipe_alloc_pdo(void *data)
949414a6295STrond Myklebust {
950414a6295STrond Myklebust 	struct gss_pipe *gss_pipe;
951414a6295STrond Myklebust 	struct gss_alloc_pdo *args = data;
952414a6295STrond Myklebust 
953414a6295STrond Myklebust 	gss_pipe = gss_pipe_alloc(args->clnt, args->name, args->upcall_ops);
954414a6295STrond Myklebust 	if (!IS_ERR(gss_pipe))
955414a6295STrond Myklebust 		return &gss_pipe->pdo;
956414a6295STrond Myklebust 	return NULL;
957414a6295STrond Myklebust }
958414a6295STrond Myklebust 
959414a6295STrond Myklebust static struct gss_pipe *gss_pipe_get(struct rpc_clnt *clnt,
960414a6295STrond Myklebust 		const char *name,
961414a6295STrond Myklebust 		const struct rpc_pipe_ops *upcall_ops)
962414a6295STrond Myklebust {
963414a6295STrond Myklebust 	struct net *net = rpc_net_ns(clnt);
964414a6295STrond Myklebust 	struct rpc_pipe_dir_object *pdo;
965414a6295STrond Myklebust 	struct gss_alloc_pdo args = {
966414a6295STrond Myklebust 		.clnt = clnt,
967414a6295STrond Myklebust 		.name = name,
968414a6295STrond Myklebust 		.upcall_ops = upcall_ops,
969414a6295STrond Myklebust 	};
970414a6295STrond Myklebust 
971414a6295STrond Myklebust 	pdo = rpc_find_or_alloc_pipe_dir_object(net,
972414a6295STrond Myklebust 			&clnt->cl_pipedir_objects,
973414a6295STrond Myklebust 			gss_pipe_match_pdo,
974414a6295STrond Myklebust 			gss_pipe_alloc_pdo,
975414a6295STrond Myklebust 			&args);
976414a6295STrond Myklebust 	if (pdo != NULL)
977414a6295STrond Myklebust 		return container_of(pdo, struct gss_pipe, pdo);
978414a6295STrond Myklebust 	return ERR_PTR(-ENOMEM);
979414a6295STrond Myklebust }
980414a6295STrond Myklebust 
98119172284STrond Myklebust static void __gss_pipe_free(struct gss_pipe *p)
982ccdc28f8SStanislav Kinsbursky {
98319172284STrond Myklebust 	struct rpc_clnt *clnt = p->clnt;
98419172284STrond Myklebust 	struct net *net = rpc_net_ns(clnt);
985ccdc28f8SStanislav Kinsbursky 
98619172284STrond Myklebust 	rpc_remove_pipe_dir_object(net,
98719172284STrond Myklebust 			&clnt->cl_pipedir_objects,
98819172284STrond Myklebust 			&p->pdo);
98919172284STrond Myklebust 	rpc_destroy_pipe_data(p->pipe);
99019172284STrond Myklebust 	kfree(p);
991ccdc28f8SStanislav Kinsbursky }
992ccdc28f8SStanislav Kinsbursky 
993414a6295STrond Myklebust static void __gss_pipe_release(struct kref *kref)
994414a6295STrond Myklebust {
995414a6295STrond Myklebust 	struct gss_pipe *p = container_of(kref, struct gss_pipe, kref);
996414a6295STrond Myklebust 
997414a6295STrond Myklebust 	__gss_pipe_free(p);
998414a6295STrond Myklebust }
999414a6295STrond Myklebust 
100019172284STrond Myklebust static void gss_pipe_free(struct gss_pipe *p)
1001ccdc28f8SStanislav Kinsbursky {
100219172284STrond Myklebust 	if (p != NULL)
1003414a6295STrond Myklebust 		kref_put(&p->kref, __gss_pipe_release);
1004ccdc28f8SStanislav Kinsbursky }
1005ccdc28f8SStanislav Kinsbursky 
10061da177e4SLinus Torvalds /*
10071da177e4SLinus Torvalds  * NOTE: we have the opportunity to use different
10081da177e4SLinus Torvalds  * parameters based on the input flavor (which must be a pseudoflavor)
10091da177e4SLinus Torvalds  */
1010eb6dc19dSTrond Myklebust static struct gss_auth *
101182b98ca5SSargun Dhillon gss_create_new(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
10121da177e4SLinus Torvalds {
1013c2190661STrond Myklebust 	rpc_authflavor_t flavor = args->pseudoflavor;
10141da177e4SLinus Torvalds 	struct gss_auth *gss_auth;
101519172284STrond Myklebust 	struct gss_pipe *gss_pipe;
10161da177e4SLinus Torvalds 	struct rpc_auth * auth;
10176a19275aSJ. Bruce Fields 	int err = -ENOMEM; /* XXX? */
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	if (!try_module_get(THIS_MODULE))
10206a19275aSJ. Bruce Fields 		return ERR_PTR(err);
10211da177e4SLinus Torvalds 	if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
10221da177e4SLinus Torvalds 		goto out_dec;
1023eb6dc19dSTrond Myklebust 	INIT_HLIST_NODE(&gss_auth->hash);
1024bd4a3eb1STrond Myklebust 	gss_auth->target_name = NULL;
1025c2190661STrond Myklebust 	if (args->target_name) {
1026c2190661STrond Myklebust 		gss_auth->target_name = kstrdup(args->target_name, GFP_KERNEL);
1027bd4a3eb1STrond Myklebust 		if (gss_auth->target_name == NULL)
1028bd4a3eb1STrond Myklebust 			goto err_free;
1029bd4a3eb1STrond Myklebust 	}
10301da177e4SLinus Torvalds 	gss_auth->client = clnt;
1031e726340aSTrond Myklebust 	gss_auth->net = get_net(rpc_net_ns(clnt));
10326a19275aSJ. Bruce Fields 	err = -EINVAL;
10331da177e4SLinus Torvalds 	gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
10340c77668dSChuck Lever 	if (!gss_auth->mech)
1035e726340aSTrond Myklebust 		goto err_put_net;
10361da177e4SLinus Torvalds 	gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
1037438b6fdeSJ. Bruce Fields 	if (gss_auth->service == 0)
1038438b6fdeSJ. Bruce Fields 		goto err_put_mech;
1039a699d65eSTrond Myklebust 	if (!gssd_running(gss_auth->net))
1040a699d65eSTrond Myklebust 		goto err_put_mech;
10411da177e4SLinus Torvalds 	auth = &gss_auth->rpc_auth;
10421da177e4SLinus Torvalds 	auth->au_cslack = GSS_CRED_SLACK >> 2;
10431da177e4SLinus Torvalds 	auth->au_rslack = GSS_VERF_SLACK >> 2;
1044ce52914eSScott Mayhew 	auth->au_flags = 0;
10451da177e4SLinus Torvalds 	auth->au_ops = &authgss_ops;
10461da177e4SLinus Torvalds 	auth->au_flavor = flavor;
104765b80179SChuck Lever 	if (gss_pseudoflavor_to_datatouch(gss_auth->mech, flavor))
104865b80179SChuck Lever 		auth->au_flags |= RPCAUTH_AUTH_DATATOUCH;
1049331bc71cSTrond Myklebust 	refcount_set(&auth->au_count, 1);
10500285ed1fSTrond Myklebust 	kref_init(&gss_auth->kref);
10511da177e4SLinus Torvalds 
105219172284STrond Myklebust 	err = rpcauth_init_credcache(auth);
105319172284STrond Myklebust 	if (err)
105419172284STrond Myklebust 		goto err_put_mech;
105534769fc4S\"J. Bruce Fields\ 	/*
105634769fc4S\"J. Bruce Fields\ 	 * Note: if we created the old pipe first, then someone who
105734769fc4S\"J. Bruce Fields\ 	 * examined the directory at the right moment might conclude
105834769fc4S\"J. Bruce Fields\ 	 * that we supported only the old pipe.  So we instead create
105934769fc4S\"J. Bruce Fields\ 	 * the new pipe first.
106034769fc4S\"J. Bruce Fields\ 	 */
1061414a6295STrond Myklebust 	gss_pipe = gss_pipe_get(clnt, "gssd", &gss_upcall_ops_v1);
106219172284STrond Myklebust 	if (IS_ERR(gss_pipe)) {
106319172284STrond Myklebust 		err = PTR_ERR(gss_pipe);
106419172284STrond Myklebust 		goto err_destroy_credcache;
10656a19275aSJ. Bruce Fields 	}
106619172284STrond Myklebust 	gss_auth->gss_pipe[1] = gss_pipe;
10671da177e4SLinus Torvalds 
1068414a6295STrond Myklebust 	gss_pipe = gss_pipe_get(clnt, gss_auth->mech->gm_name,
106919172284STrond Myklebust 			&gss_upcall_ops_v0);
107019172284STrond Myklebust 	if (IS_ERR(gss_pipe)) {
107119172284STrond Myklebust 		err = PTR_ERR(gss_pipe);
1072c239d83bSStanislav Kinsbursky 		goto err_destroy_pipe_1;
1073c239d83bSStanislav Kinsbursky 	}
107419172284STrond Myklebust 	gss_auth->gss_pipe[0] = gss_pipe;
107507a2bf1dSTrond Myklebust 
1076eb6dc19dSTrond Myklebust 	return gss_auth;
1077c239d83bSStanislav Kinsbursky err_destroy_pipe_1:
1078414a6295STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[1]);
107919172284STrond Myklebust err_destroy_credcache:
108019172284STrond Myklebust 	rpcauth_destroy_credcache(auth);
10811da177e4SLinus Torvalds err_put_mech:
10821da177e4SLinus Torvalds 	gss_mech_put(gss_auth->mech);
1083e726340aSTrond Myklebust err_put_net:
1084e726340aSTrond Myklebust 	put_net(gss_auth->net);
10851da177e4SLinus Torvalds err_free:
1086bd4a3eb1STrond Myklebust 	kfree(gss_auth->target_name);
10871da177e4SLinus Torvalds 	kfree(gss_auth);
10881da177e4SLinus Torvalds out_dec:
10891da177e4SLinus Torvalds 	module_put(THIS_MODULE);
10900c77668dSChuck Lever 	trace_rpcgss_createauth(flavor, err);
10916a19275aSJ. Bruce Fields 	return ERR_PTR(err);
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds static void
10950285ed1fSTrond Myklebust gss_free(struct gss_auth *gss_auth)
10960285ed1fSTrond Myklebust {
109719172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[0]);
109819172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[1]);
10990285ed1fSTrond Myklebust 	gss_mech_put(gss_auth->mech);
1100e726340aSTrond Myklebust 	put_net(gss_auth->net);
1101bd4a3eb1STrond Myklebust 	kfree(gss_auth->target_name);
11020285ed1fSTrond Myklebust 
11030285ed1fSTrond Myklebust 	kfree(gss_auth);
11040285ed1fSTrond Myklebust 	module_put(THIS_MODULE);
11050285ed1fSTrond Myklebust }
11060285ed1fSTrond Myklebust 
11070285ed1fSTrond Myklebust static void
11080285ed1fSTrond Myklebust gss_free_callback(struct kref *kref)
11090285ed1fSTrond Myklebust {
11100285ed1fSTrond Myklebust 	struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
11110285ed1fSTrond Myklebust 
11120285ed1fSTrond Myklebust 	gss_free(gss_auth);
11130285ed1fSTrond Myklebust }
11140285ed1fSTrond Myklebust 
11150285ed1fSTrond Myklebust static void
11169eb2ddb4STrond Myklebust gss_put_auth(struct gss_auth *gss_auth)
11179eb2ddb4STrond Myklebust {
11189eb2ddb4STrond Myklebust 	kref_put(&gss_auth->kref, gss_free_callback);
11199eb2ddb4STrond Myklebust }
11209eb2ddb4STrond Myklebust 
11219eb2ddb4STrond Myklebust static void
11221da177e4SLinus Torvalds gss_destroy(struct rpc_auth *auth)
11231da177e4SLinus Torvalds {
112419172284STrond Myklebust 	struct gss_auth *gss_auth = container_of(auth,
112519172284STrond Myklebust 			struct gss_auth, rpc_auth);
11261da177e4SLinus Torvalds 
1127eb6dc19dSTrond Myklebust 	if (hash_hashed(&gss_auth->hash)) {
1128eb6dc19dSTrond Myklebust 		spin_lock(&gss_auth_hash_lock);
1129eb6dc19dSTrond Myklebust 		hash_del(&gss_auth->hash);
1130eb6dc19dSTrond Myklebust 		spin_unlock(&gss_auth_hash_lock);
1131eb6dc19dSTrond Myklebust 	}
1132eb6dc19dSTrond Myklebust 
113319172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[0]);
113419172284STrond Myklebust 	gss_auth->gss_pipe[0] = NULL;
113519172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[1]);
113619172284STrond Myklebust 	gss_auth->gss_pipe[1] = NULL;
11373ab9bb72STrond Myklebust 	rpcauth_destroy_credcache(auth);
11383ab9bb72STrond Myklebust 
11399eb2ddb4STrond Myklebust 	gss_put_auth(gss_auth);
11401da177e4SLinus Torvalds }
11411da177e4SLinus Torvalds 
1142a0f6ed8eSJ. Bruce Fields /*
1143a0f6ed8eSJ. Bruce Fields  * Auths may be shared between rpc clients that were cloned from a
1144a0f6ed8eSJ. Bruce Fields  * common client with the same xprt, if they also share the flavor and
1145a0f6ed8eSJ. Bruce Fields  * target_name.
1146a0f6ed8eSJ. Bruce Fields  *
1147a0f6ed8eSJ. Bruce Fields  * The auth is looked up from the oldest parent sharing the same
1148a0f6ed8eSJ. Bruce Fields  * cl_xprt, and the auth itself references only that common parent
1149a0f6ed8eSJ. Bruce Fields  * (which is guaranteed to last as long as any of its descendants).
1150a0f6ed8eSJ. Bruce Fields  */
1151eb6dc19dSTrond Myklebust static struct gss_auth *
115282b98ca5SSargun Dhillon gss_auth_find_or_add_hashed(const struct rpc_auth_create_args *args,
1153eb6dc19dSTrond Myklebust 		struct rpc_clnt *clnt,
1154eb6dc19dSTrond Myklebust 		struct gss_auth *new)
1155eb6dc19dSTrond Myklebust {
1156eb6dc19dSTrond Myklebust 	struct gss_auth *gss_auth;
1157eb6dc19dSTrond Myklebust 	unsigned long hashval = (unsigned long)clnt;
1158eb6dc19dSTrond Myklebust 
1159eb6dc19dSTrond Myklebust 	spin_lock(&gss_auth_hash_lock);
1160eb6dc19dSTrond Myklebust 	hash_for_each_possible(gss_auth_hash_table,
1161eb6dc19dSTrond Myklebust 			gss_auth,
1162eb6dc19dSTrond Myklebust 			hash,
1163eb6dc19dSTrond Myklebust 			hashval) {
1164a0f6ed8eSJ. Bruce Fields 		if (gss_auth->client != clnt)
1165a0f6ed8eSJ. Bruce Fields 			continue;
1166eb6dc19dSTrond Myklebust 		if (gss_auth->rpc_auth.au_flavor != args->pseudoflavor)
1167eb6dc19dSTrond Myklebust 			continue;
1168eb6dc19dSTrond Myklebust 		if (gss_auth->target_name != args->target_name) {
1169eb6dc19dSTrond Myklebust 			if (gss_auth->target_name == NULL)
1170eb6dc19dSTrond Myklebust 				continue;
1171eb6dc19dSTrond Myklebust 			if (args->target_name == NULL)
1172eb6dc19dSTrond Myklebust 				continue;
1173eb6dc19dSTrond Myklebust 			if (strcmp(gss_auth->target_name, args->target_name))
1174eb6dc19dSTrond Myklebust 				continue;
1175eb6dc19dSTrond Myklebust 		}
1176331bc71cSTrond Myklebust 		if (!refcount_inc_not_zero(&gss_auth->rpc_auth.au_count))
1177eb6dc19dSTrond Myklebust 			continue;
1178eb6dc19dSTrond Myklebust 		goto out;
1179eb6dc19dSTrond Myklebust 	}
1180eb6dc19dSTrond Myklebust 	if (new)
1181eb6dc19dSTrond Myklebust 		hash_add(gss_auth_hash_table, &new->hash, hashval);
1182eb6dc19dSTrond Myklebust 	gss_auth = new;
1183eb6dc19dSTrond Myklebust out:
1184eb6dc19dSTrond Myklebust 	spin_unlock(&gss_auth_hash_lock);
1185eb6dc19dSTrond Myklebust 	return gss_auth;
1186eb6dc19dSTrond Myklebust }
1187eb6dc19dSTrond Myklebust 
1188eb6dc19dSTrond Myklebust static struct gss_auth *
118982b98ca5SSargun Dhillon gss_create_hashed(const struct rpc_auth_create_args *args,
119082b98ca5SSargun Dhillon 		  struct rpc_clnt *clnt)
1191eb6dc19dSTrond Myklebust {
1192eb6dc19dSTrond Myklebust 	struct gss_auth *gss_auth;
1193eb6dc19dSTrond Myklebust 	struct gss_auth *new;
1194eb6dc19dSTrond Myklebust 
1195eb6dc19dSTrond Myklebust 	gss_auth = gss_auth_find_or_add_hashed(args, clnt, NULL);
1196eb6dc19dSTrond Myklebust 	if (gss_auth != NULL)
1197eb6dc19dSTrond Myklebust 		goto out;
1198eb6dc19dSTrond Myklebust 	new = gss_create_new(args, clnt);
1199eb6dc19dSTrond Myklebust 	if (IS_ERR(new))
1200eb6dc19dSTrond Myklebust 		return new;
1201eb6dc19dSTrond Myklebust 	gss_auth = gss_auth_find_or_add_hashed(args, clnt, new);
1202eb6dc19dSTrond Myklebust 	if (gss_auth != new)
1203eb6dc19dSTrond Myklebust 		gss_destroy(&new->rpc_auth);
1204eb6dc19dSTrond Myklebust out:
1205eb6dc19dSTrond Myklebust 	return gss_auth;
1206eb6dc19dSTrond Myklebust }
1207eb6dc19dSTrond Myklebust 
1208eb6dc19dSTrond Myklebust static struct rpc_auth *
120982b98ca5SSargun Dhillon gss_create(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
1210eb6dc19dSTrond Myklebust {
1211eb6dc19dSTrond Myklebust 	struct gss_auth *gss_auth;
1212ad01b2c6STrond Myklebust 	struct rpc_xprt_switch *xps = rcu_access_pointer(clnt->cl_xpi.xpi_xpswitch);
1213eb6dc19dSTrond Myklebust 
1214eb6dc19dSTrond Myklebust 	while (clnt != clnt->cl_parent) {
1215eb6dc19dSTrond Myklebust 		struct rpc_clnt *parent = clnt->cl_parent;
1216eb6dc19dSTrond Myklebust 		/* Find the original parent for this transport */
1217ad01b2c6STrond Myklebust 		if (rcu_access_pointer(parent->cl_xpi.xpi_xpswitch) != xps)
1218eb6dc19dSTrond Myklebust 			break;
1219eb6dc19dSTrond Myklebust 		clnt = parent;
1220eb6dc19dSTrond Myklebust 	}
1221eb6dc19dSTrond Myklebust 
1222eb6dc19dSTrond Myklebust 	gss_auth = gss_create_hashed(args, clnt);
1223eb6dc19dSTrond Myklebust 	if (IS_ERR(gss_auth))
1224eb6dc19dSTrond Myklebust 		return ERR_CAST(gss_auth);
1225eb6dc19dSTrond Myklebust 	return &gss_auth->rpc_auth;
1226eb6dc19dSTrond Myklebust }
1227eb6dc19dSTrond Myklebust 
1228a652a4bcSTrond Myklebust static struct gss_cred *
1229a652a4bcSTrond Myklebust gss_dup_cred(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
1230a652a4bcSTrond Myklebust {
1231a652a4bcSTrond Myklebust 	struct gss_cred *new;
1232a652a4bcSTrond Myklebust 
1233a652a4bcSTrond Myklebust 	/* Make a copy of the cred so that we can reference count it */
1234a652a4bcSTrond Myklebust 	new = kzalloc(sizeof(*gss_cred), GFP_NOIO);
1235a652a4bcSTrond Myklebust 	if (new) {
1236a652a4bcSTrond Myklebust 		struct auth_cred acred = {
12378276c902SNeilBrown 			.cred = gss_cred->gc_base.cr_cred,
1238a652a4bcSTrond Myklebust 		};
1239a652a4bcSTrond Myklebust 		struct gss_cl_ctx *ctx =
1240a652a4bcSTrond Myklebust 			rcu_dereference_protected(gss_cred->gc_ctx, 1);
1241a652a4bcSTrond Myklebust 
1242a652a4bcSTrond Myklebust 		rpcauth_init_cred(&new->gc_base, &acred,
1243a652a4bcSTrond Myklebust 				&gss_auth->rpc_auth,
1244a652a4bcSTrond Myklebust 				&gss_nullops);
1245a652a4bcSTrond Myklebust 		new->gc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
1246a652a4bcSTrond Myklebust 		new->gc_service = gss_cred->gc_service;
1247a652a4bcSTrond Myklebust 		new->gc_principal = gss_cred->gc_principal;
1248a652a4bcSTrond Myklebust 		kref_get(&gss_auth->kref);
1249a652a4bcSTrond Myklebust 		rcu_assign_pointer(new->gc_ctx, ctx);
1250a652a4bcSTrond Myklebust 		gss_get_ctx(ctx);
1251a652a4bcSTrond Myklebust 	}
1252a652a4bcSTrond Myklebust 	return new;
1253a652a4bcSTrond Myklebust }
1254a652a4bcSTrond Myklebust 
12550df7fb74STrond Myklebust /*
1256a652a4bcSTrond Myklebust  * gss_send_destroy_context will cause the RPCSEC_GSS to send a NULL RPC call
12570df7fb74STrond Myklebust  * to the server with the GSS control procedure field set to
12580df7fb74STrond Myklebust  * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
12590df7fb74STrond Myklebust  * all RPCSEC_GSS state associated with that context.
12600df7fb74STrond Myklebust  */
1261a652a4bcSTrond Myklebust static void
1262a652a4bcSTrond Myklebust gss_send_destroy_context(struct rpc_cred *cred)
12630df7fb74STrond Myklebust {
12640df7fb74STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
12650df7fb74STrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
1266c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx = rcu_dereference_protected(gss_cred->gc_ctx, 1);
1267a652a4bcSTrond Myklebust 	struct gss_cred *new;
12680df7fb74STrond Myklebust 	struct rpc_task *task;
12690df7fb74STrond Myklebust 
1270a652a4bcSTrond Myklebust 	new = gss_dup_cred(gss_auth, gss_cred);
1271a652a4bcSTrond Myklebust 	if (new) {
1272c5e6aecdSJeff Layton 		ctx->gc_proc = RPC_GSS_PROC_DESTROY;
12730df7fb74STrond Myklebust 
1274a652a4bcSTrond Myklebust 		task = rpc_call_null(gss_auth->client, &new->gc_base,
1275a652a4bcSTrond Myklebust 				RPC_TASK_ASYNC|RPC_TASK_SOFT);
12760df7fb74STrond Myklebust 		if (!IS_ERR(task))
12770df7fb74STrond Myklebust 			rpc_put_task(task);
12780df7fb74STrond Myklebust 
1279a652a4bcSTrond Myklebust 		put_rpccred(&new->gc_base);
1280a652a4bcSTrond Myklebust 	}
12810df7fb74STrond Myklebust }
12820df7fb74STrond Myklebust 
12830df7fb74STrond Myklebust /* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
12841da177e4SLinus Torvalds  * to create a new cred or context, so they check that things have been
12851da177e4SLinus Torvalds  * allocated before freeing them. */
12861da177e4SLinus Torvalds static void
12875d28dc82STrond Myklebust gss_do_free_ctx(struct gss_cl_ctx *ctx)
12881da177e4SLinus Torvalds {
12890d8a3746STrond Myklebust 	gss_delete_sec_context(&ctx->gc_gss_ctx);
12901da177e4SLinus Torvalds 	kfree(ctx->gc_wire_ctx.data);
12912004c726SJeff Layton 	kfree(ctx->gc_acceptor.data);
12921da177e4SLinus Torvalds 	kfree(ctx);
12931da177e4SLinus Torvalds }
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds static void
12965d28dc82STrond Myklebust gss_free_ctx_callback(struct rcu_head *head)
12975d28dc82STrond Myklebust {
12985d28dc82STrond Myklebust 	struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
12995d28dc82STrond Myklebust 	gss_do_free_ctx(ctx);
13005d28dc82STrond Myklebust }
13015d28dc82STrond Myklebust 
13025d28dc82STrond Myklebust static void
13035d28dc82STrond Myklebust gss_free_ctx(struct gss_cl_ctx *ctx)
13045d28dc82STrond Myklebust {
13055d28dc82STrond Myklebust 	call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
13065d28dc82STrond Myklebust }
13075d28dc82STrond Myklebust 
13085d28dc82STrond Myklebust static void
130931be5bf1STrond Myklebust gss_free_cred(struct gss_cred *gss_cred)
13101da177e4SLinus Torvalds {
131131be5bf1STrond Myklebust 	kfree(gss_cred);
131231be5bf1STrond Myklebust }
13131da177e4SLinus Torvalds 
131431be5bf1STrond Myklebust static void
131531be5bf1STrond Myklebust gss_free_cred_callback(struct rcu_head *head)
131631be5bf1STrond Myklebust {
131731be5bf1STrond Myklebust 	struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
131831be5bf1STrond Myklebust 	gss_free_cred(gss_cred);
131931be5bf1STrond Myklebust }
13201da177e4SLinus Torvalds 
132131be5bf1STrond Myklebust static void
13226dcd3926SJeff Layton gss_destroy_nullcred(struct rpc_cred *cred)
132331be5bf1STrond Myklebust {
13245d28dc82STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
13250285ed1fSTrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
1326c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx = rcu_dereference_protected(gss_cred->gc_ctx, 1);
13275d28dc82STrond Myklebust 
1328a9b3cd7fSStephen Hemminger 	RCU_INIT_POINTER(gss_cred->gc_ctx, NULL);
132997f68c6bSNeilBrown 	put_cred(cred->cr_cred);
133031be5bf1STrond Myklebust 	call_rcu(&cred->cr_rcu, gss_free_cred_callback);
13315d28dc82STrond Myklebust 	if (ctx)
13325d28dc82STrond Myklebust 		gss_put_ctx(ctx);
13339eb2ddb4STrond Myklebust 	gss_put_auth(gss_auth);
13341da177e4SLinus Torvalds }
13351da177e4SLinus Torvalds 
13366dcd3926SJeff Layton static void
13376dcd3926SJeff Layton gss_destroy_cred(struct rpc_cred *cred)
13386dcd3926SJeff Layton {
13396dcd3926SJeff Layton 
1340a652a4bcSTrond Myklebust 	if (test_and_clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0)
1341a652a4bcSTrond Myklebust 		gss_send_destroy_context(cred);
13426dcd3926SJeff Layton 	gss_destroy_nullcred(cred);
13436dcd3926SJeff Layton }
13446dcd3926SJeff Layton 
1345a960f8d6SFrank Sorenson static int
1346a960f8d6SFrank Sorenson gss_hash_cred(struct auth_cred *acred, unsigned int hashbits)
1347a960f8d6SFrank Sorenson {
13488276c902SNeilBrown 	return hash_64(from_kuid(&init_user_ns, acred->cred->fsuid), hashbits);
1349a960f8d6SFrank Sorenson }
1350a960f8d6SFrank Sorenson 
13511da177e4SLinus Torvalds /*
13521da177e4SLinus Torvalds  * Lookup RPCSEC_GSS cred for the current process
13531da177e4SLinus Torvalds  */
13541da177e4SLinus Torvalds static struct rpc_cred *
13558a317760STrond Myklebust gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
13561da177e4SLinus Torvalds {
13573c6e0bc8SJeff Layton 	return rpcauth_lookup_credcache(auth, acred, flags, GFP_NOFS);
13581da177e4SLinus Torvalds }
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds static struct rpc_cred *
13613c6e0bc8SJeff Layton gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags, gfp_t gfp)
13621da177e4SLinus Torvalds {
13631da177e4SLinus Torvalds 	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
13641da177e4SLinus Torvalds 	struct gss_cred	*cred = NULL;
13651da177e4SLinus Torvalds 	int err = -ENOMEM;
13661da177e4SLinus Torvalds 
13673c6e0bc8SJeff Layton 	if (!(cred = kzalloc(sizeof(*cred), gfp)))
13681da177e4SLinus Torvalds 		goto out_err;
13691da177e4SLinus Torvalds 
13705fe4755eSTrond Myklebust 	rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
13711da177e4SLinus Torvalds 	/*
13721da177e4SLinus Torvalds 	 * Note: in order to force a call to call_refresh(), we deliberately
13731da177e4SLinus Torvalds 	 * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
13741da177e4SLinus Torvalds 	 */
1375fc432dd9STrond Myklebust 	cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
13761da177e4SLinus Torvalds 	cred->gc_service = gss_auth->service;
137768c97153STrond Myklebust 	cred->gc_principal = acred->principal;
13780285ed1fSTrond Myklebust 	kref_get(&gss_auth->kref);
13791da177e4SLinus Torvalds 	return &cred->gc_base;
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds out_err:
13821da177e4SLinus Torvalds 	return ERR_PTR(err);
13831da177e4SLinus Torvalds }
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds static int
1386fba3bad4STrond Myklebust gss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
1387fba3bad4STrond Myklebust {
1388fba3bad4STrond Myklebust 	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
1389fba3bad4STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
1390fba3bad4STrond Myklebust 	int err;
1391fba3bad4STrond Myklebust 
1392fba3bad4STrond Myklebust 	do {
1393fba3bad4STrond Myklebust 		err = gss_create_upcall(gss_auth, gss_cred);
1394fba3bad4STrond Myklebust 	} while (err == -EAGAIN);
1395fba3bad4STrond Myklebust 	return err;
1396fba3bad4STrond Myklebust }
1397fba3bad4STrond Myklebust 
1398a0337d1dSJeff Layton static char *
1399a0337d1dSJeff Layton gss_stringify_acceptor(struct rpc_cred *cred)
1400a0337d1dSJeff Layton {
1401c5e6aecdSJeff Layton 	char *string = NULL;
1402a0337d1dSJeff Layton 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1403c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx;
1404b3ecba09SJeff Layton 	unsigned int len;
1405c5e6aecdSJeff Layton 	struct xdr_netobj *acceptor;
1406c5e6aecdSJeff Layton 
1407c5e6aecdSJeff Layton 	rcu_read_lock();
1408c5e6aecdSJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
1409c5e6aecdSJeff Layton 	if (!ctx)
1410c5e6aecdSJeff Layton 		goto out;
1411c5e6aecdSJeff Layton 
1412b3ecba09SJeff Layton 	len = ctx->gc_acceptor.len;
1413b3ecba09SJeff Layton 	rcu_read_unlock();
1414a0337d1dSJeff Layton 
1415a0337d1dSJeff Layton 	/* no point if there's no string */
1416b3ecba09SJeff Layton 	if (!len)
1417b3ecba09SJeff Layton 		return NULL;
1418b3ecba09SJeff Layton realloc:
1419b3ecba09SJeff Layton 	string = kmalloc(len + 1, GFP_KERNEL);
1420a0337d1dSJeff Layton 	if (!string)
1421b3ecba09SJeff Layton 		return NULL;
1422b3ecba09SJeff Layton 
1423b3ecba09SJeff Layton 	rcu_read_lock();
1424b3ecba09SJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
1425b3ecba09SJeff Layton 
1426b3ecba09SJeff Layton 	/* did the ctx disappear or was it replaced by one with no acceptor? */
1427b3ecba09SJeff Layton 	if (!ctx || !ctx->gc_acceptor.len) {
1428b3ecba09SJeff Layton 		kfree(string);
1429b3ecba09SJeff Layton 		string = NULL;
1430c5e6aecdSJeff Layton 		goto out;
1431b3ecba09SJeff Layton 	}
1432b3ecba09SJeff Layton 
1433b3ecba09SJeff Layton 	acceptor = &ctx->gc_acceptor;
1434b3ecba09SJeff Layton 
1435b3ecba09SJeff Layton 	/*
1436b3ecba09SJeff Layton 	 * Did we find a new acceptor that's longer than the original? Allocate
1437b3ecba09SJeff Layton 	 * a longer buffer and try again.
1438b3ecba09SJeff Layton 	 */
1439b3ecba09SJeff Layton 	if (len < acceptor->len) {
1440b3ecba09SJeff Layton 		len = acceptor->len;
1441b3ecba09SJeff Layton 		rcu_read_unlock();
1442b3ecba09SJeff Layton 		kfree(string);
1443b3ecba09SJeff Layton 		goto realloc;
1444b3ecba09SJeff Layton 	}
1445a0337d1dSJeff Layton 
1446a0337d1dSJeff Layton 	memcpy(string, acceptor->data, acceptor->len);
1447a0337d1dSJeff Layton 	string[acceptor->len] = '\0';
1448c5e6aecdSJeff Layton out:
1449c5e6aecdSJeff Layton 	rcu_read_unlock();
1450a0337d1dSJeff Layton 	return string;
1451a0337d1dSJeff Layton }
1452a0337d1dSJeff Layton 
14534de6caa2SAndy Adamson /*
14544de6caa2SAndy Adamson  * Returns -EACCES if GSS context is NULL or will expire within the
14554de6caa2SAndy Adamson  * timeout (miliseconds)
14564de6caa2SAndy Adamson  */
14574de6caa2SAndy Adamson static int
14584de6caa2SAndy Adamson gss_key_timeout(struct rpc_cred *rc)
14594de6caa2SAndy Adamson {
14604de6caa2SAndy Adamson 	struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
1461c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx;
1462cc6a7aabSArnd Bergmann 	unsigned long timeout = jiffies + (gss_key_expire_timeo * HZ);
1463cc6a7aabSArnd Bergmann 	int ret = 0;
14644de6caa2SAndy Adamson 
1465c5e6aecdSJeff Layton 	rcu_read_lock();
1466c5e6aecdSJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
1467cc6a7aabSArnd Bergmann 	if (!ctx || time_after(timeout, ctx->gc_expiry))
1468cc6a7aabSArnd Bergmann 		ret = -EACCES;
1469c5e6aecdSJeff Layton 	rcu_read_unlock();
1470cc6a7aabSArnd Bergmann 
1471cc6a7aabSArnd Bergmann 	return ret;
14724de6caa2SAndy Adamson }
14734de6caa2SAndy Adamson 
1474fba3bad4STrond Myklebust static int
14758a317760STrond Myklebust gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
14761da177e4SLinus Torvalds {
14771da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
1478c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx;
14794de6caa2SAndy Adamson 	int ret;
14801da177e4SLinus Torvalds 
1481cd019f75STrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
14828a317760STrond Myklebust 		goto out;
14831da177e4SLinus Torvalds 	/* Don't match with creds that have expired. */
1484c5e6aecdSJeff Layton 	rcu_read_lock();
1485c5e6aecdSJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
1486c5e6aecdSJeff Layton 	if (!ctx || time_after(jiffies, ctx->gc_expiry)) {
1487c5e6aecdSJeff Layton 		rcu_read_unlock();
1488cd019f75STrond Myklebust 		return 0;
1489c5e6aecdSJeff Layton 	}
1490c5e6aecdSJeff Layton 	rcu_read_unlock();
1491cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags))
14921da177e4SLinus Torvalds 		return 0;
14938a317760STrond Myklebust out:
149468c97153STrond Myklebust 	if (acred->principal != NULL) {
149568c97153STrond Myklebust 		if (gss_cred->gc_principal == NULL)
149668c97153STrond Myklebust 			return 0;
14974de6caa2SAndy Adamson 		ret = strcmp(acred->principal, gss_cred->gc_principal) == 0;
1498ddf529eeSNeilBrown 	} else {
149968c97153STrond Myklebust 		if (gss_cred->gc_principal != NULL)
15007c67db3aSTrond Myklebust 			return 0;
150104d1532bSNeilBrown 		ret = uid_eq(rc->cr_cred->fsuid, acred->cred->fsuid);
15024de6caa2SAndy Adamson 	}
15034de6caa2SAndy Adamson 	return ret;
15041da177e4SLinus Torvalds }
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds /*
15071da177e4SLinus Torvalds  * Marshal credentials.
1508e8680a24SChuck Lever  *
1509e8680a24SChuck Lever  * The expensive part is computing the verifier. We can't cache a
1510e8680a24SChuck Lever  * pre-computed version of the verifier because the seqno, which
1511e8680a24SChuck Lever  * is different every time, is included in the MIC.
15121da177e4SLinus Torvalds  */
1513e8680a24SChuck Lever static int gss_marshal(struct rpc_task *task, struct xdr_stream *xdr)
15141da177e4SLinus Torvalds {
1515a17c2153STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1516a17c2153STrond Myklebust 	struct rpc_cred *cred = req->rq_cred;
15171da177e4SLinus Torvalds 	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
15181da177e4SLinus Torvalds 						 gc_base);
15191da177e4SLinus Torvalds 	struct gss_cl_ctx	*ctx = gss_cred_get_ctx(cred);
1520e8680a24SChuck Lever 	__be32		*p, *cred_len;
15211da177e4SLinus Torvalds 	u32             maj_stat = 0;
15221da177e4SLinus Torvalds 	struct xdr_netobj mic;
15231da177e4SLinus Torvalds 	struct kvec	iov;
15241da177e4SLinus Torvalds 	struct xdr_buf	verf_buf;
15250c77668dSChuck Lever 	int status;
15261da177e4SLinus Torvalds 
1527e8680a24SChuck Lever 	/* Credential */
1528e8680a24SChuck Lever 
1529e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, 7 * sizeof(*p) +
1530e8680a24SChuck Lever 			      ctx->gc_wire_ctx.len);
1531e8680a24SChuck Lever 	if (!p)
15320c77668dSChuck Lever 		goto marshal_failed;
1533e8680a24SChuck Lever 	*p++ = rpc_auth_gss;
15341da177e4SLinus Torvalds 	cred_len = p++;
15351da177e4SLinus Torvalds 
15361da177e4SLinus Torvalds 	spin_lock(&ctx->gc_seq_lock);
153797b78ae9STrond Myklebust 	req->rq_seqno = (ctx->gc_seq < MAXSEQ) ? ctx->gc_seq++ : MAXSEQ;
15381da177e4SLinus Torvalds 	spin_unlock(&ctx->gc_seq_lock);
153997b78ae9STrond Myklebust 	if (req->rq_seqno == MAXSEQ)
15400c77668dSChuck Lever 		goto expired;
15410c77668dSChuck Lever 	trace_rpcgss_seqno(task);
15421da177e4SLinus Torvalds 
1543e8680a24SChuck Lever 	*p++ = cpu_to_be32(RPC_GSS_VERSION);
1544e8680a24SChuck Lever 	*p++ = cpu_to_be32(ctx->gc_proc);
1545e8680a24SChuck Lever 	*p++ = cpu_to_be32(req->rq_seqno);
1546e8680a24SChuck Lever 	*p++ = cpu_to_be32(gss_cred->gc_service);
15471da177e4SLinus Torvalds 	p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
1548e8680a24SChuck Lever 	*cred_len = cpu_to_be32((p - (cred_len + 1)) << 2);
1549e8680a24SChuck Lever 
1550e8680a24SChuck Lever 	/* Verifier */
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	/* We compute the checksum for the verifier over the xdr-encoded bytes
15531da177e4SLinus Torvalds 	 * starting with the xid and ending at the end of the credential: */
1554067fb11bSChuck Lever 	iov.iov_base = req->rq_snd_buf.head[0].iov_base;
15551da177e4SLinus Torvalds 	iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
15561da177e4SLinus Torvalds 	xdr_buf_from_iov(&iov, &verf_buf);
15571da177e4SLinus Torvalds 
1558e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, sizeof(*p));
1559e8680a24SChuck Lever 	if (!p)
15600c77668dSChuck Lever 		goto marshal_failed;
1561e8680a24SChuck Lever 	*p++ = rpc_auth_gss;
15621da177e4SLinus Torvalds 	mic.data = (u8 *)(p + 1);
156300fd6e14SJ. Bruce Fields 	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
1564e8680a24SChuck Lever 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
15650c77668dSChuck Lever 		goto expired;
1566e8680a24SChuck Lever 	else if (maj_stat != 0)
15670c77668dSChuck Lever 		goto bad_mic;
1568e8680a24SChuck Lever 	if (xdr_stream_encode_opaque_inline(xdr, (void **)&p, mic.len) < 0)
15690c77668dSChuck Lever 		goto marshal_failed;
15700c77668dSChuck Lever 	status = 0;
15710c77668dSChuck Lever out:
15721da177e4SLinus Torvalds 	gss_put_ctx(ctx);
15730c77668dSChuck Lever 	return status;
15740c77668dSChuck Lever expired:
157597b78ae9STrond Myklebust 	clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
15760c77668dSChuck Lever 	status = -EKEYEXPIRED;
15770c77668dSChuck Lever 	goto out;
15780c77668dSChuck Lever marshal_failed:
15790c77668dSChuck Lever 	status = -EMSGSIZE;
15800c77668dSChuck Lever 	goto out;
15810c77668dSChuck Lever bad_mic:
15820c77668dSChuck Lever 	trace_rpcgss_get_mic(task, maj_stat);
15830c77668dSChuck Lever 	status = -EIO;
15840c77668dSChuck Lever 	goto out;
15851da177e4SLinus Torvalds }
15861da177e4SLinus Torvalds 
1587cd019f75STrond Myklebust static int gss_renew_cred(struct rpc_task *task)
1588cd019f75STrond Myklebust {
1589a17c2153STrond Myklebust 	struct rpc_cred *oldcred = task->tk_rqstp->rq_cred;
1590cd019f75STrond Myklebust 	struct gss_cred *gss_cred = container_of(oldcred,
1591cd019f75STrond Myklebust 						 struct gss_cred,
1592cd019f75STrond Myklebust 						 gc_base);
1593cd019f75STrond Myklebust 	struct rpc_auth *auth = oldcred->cr_auth;
1594cd019f75STrond Myklebust 	struct auth_cred acred = {
159597f68c6bSNeilBrown 		.cred = oldcred->cr_cred,
159668c97153STrond Myklebust 		.principal = gss_cred->gc_principal,
1597cd019f75STrond Myklebust 	};
1598cd019f75STrond Myklebust 	struct rpc_cred *new;
1599cd019f75STrond Myklebust 
1600cd019f75STrond Myklebust 	new = gss_lookup_cred(auth, &acred, RPCAUTH_LOOKUP_NEW);
1601cd019f75STrond Myklebust 	if (IS_ERR(new))
1602cd019f75STrond Myklebust 		return PTR_ERR(new);
1603a17c2153STrond Myklebust 	task->tk_rqstp->rq_cred = new;
1604cd019f75STrond Myklebust 	put_rpccred(oldcred);
1605cd019f75STrond Myklebust 	return 0;
1606cd019f75STrond Myklebust }
1607cd019f75STrond Myklebust 
1608126e216aSTrond Myklebust static int gss_cred_is_negative_entry(struct rpc_cred *cred)
1609126e216aSTrond Myklebust {
1610126e216aSTrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEGATIVE, &cred->cr_flags)) {
1611126e216aSTrond Myklebust 		unsigned long now = jiffies;
1612126e216aSTrond Myklebust 		unsigned long begin, expire;
1613126e216aSTrond Myklebust 		struct gss_cred *gss_cred;
1614126e216aSTrond Myklebust 
1615126e216aSTrond Myklebust 		gss_cred = container_of(cred, struct gss_cred, gc_base);
1616126e216aSTrond Myklebust 		begin = gss_cred->gc_upcall_timestamp;
1617126e216aSTrond Myklebust 		expire = begin + gss_expired_cred_retry_delay * HZ;
1618126e216aSTrond Myklebust 
1619126e216aSTrond Myklebust 		if (time_in_range_open(now, begin, expire))
1620126e216aSTrond Myklebust 			return 1;
1621126e216aSTrond Myklebust 	}
1622126e216aSTrond Myklebust 	return 0;
1623126e216aSTrond Myklebust }
1624126e216aSTrond Myklebust 
16251da177e4SLinus Torvalds /*
16261da177e4SLinus Torvalds * Refresh credentials. XXX - finish
16271da177e4SLinus Torvalds */
16281da177e4SLinus Torvalds static int
16291da177e4SLinus Torvalds gss_refresh(struct rpc_task *task)
16301da177e4SLinus Torvalds {
1631a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
1632cd019f75STrond Myklebust 	int ret = 0;
16331da177e4SLinus Torvalds 
1634126e216aSTrond Myklebust 	if (gss_cred_is_negative_entry(cred))
1635126e216aSTrond Myklebust 		return -EKEYEXPIRED;
1636126e216aSTrond Myklebust 
1637cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
1638cd019f75STrond Myklebust 			!test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags)) {
1639cd019f75STrond Myklebust 		ret = gss_renew_cred(task);
1640cd019f75STrond Myklebust 		if (ret < 0)
1641cd019f75STrond Myklebust 			goto out;
1642a17c2153STrond Myklebust 		cred = task->tk_rqstp->rq_cred;
1643cd019f75STrond Myklebust 	}
1644cd019f75STrond Myklebust 
1645cd019f75STrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
1646cd019f75STrond Myklebust 		ret = gss_refresh_upcall(task);
1647cd019f75STrond Myklebust out:
1648cd019f75STrond Myklebust 	return ret;
16491da177e4SLinus Torvalds }
16501da177e4SLinus Torvalds 
16510df7fb74STrond Myklebust /* Dummy refresh routine: used only when destroying the context */
16520df7fb74STrond Myklebust static int
16530df7fb74STrond Myklebust gss_refresh_null(struct rpc_task *task)
16540df7fb74STrond Myklebust {
1655c297c8b9SAndy Adamson 	return 0;
16560df7fb74STrond Myklebust }
16570df7fb74STrond Myklebust 
1658a0584ee9SChuck Lever static int
1659a0584ee9SChuck Lever gss_validate(struct rpc_task *task, struct xdr_stream *xdr)
16601da177e4SLinus Torvalds {
1661a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
16621da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1663a0584ee9SChuck Lever 	__be32		*p, *seq = NULL;
16641da177e4SLinus Torvalds 	struct kvec	iov;
16651da177e4SLinus Torvalds 	struct xdr_buf	verf_buf;
16661da177e4SLinus Torvalds 	struct xdr_netobj mic;
1667a0584ee9SChuck Lever 	u32		len, maj_stat;
1668a0584ee9SChuck Lever 	int		status;
16691da177e4SLinus Torvalds 
1670a0584ee9SChuck Lever 	p = xdr_inline_decode(xdr, 2 * sizeof(*p));
1671a0584ee9SChuck Lever 	if (!p)
1672a0584ee9SChuck Lever 		goto validate_failed;
1673a0584ee9SChuck Lever 	if (*p++ != rpc_auth_gss)
1674a0584ee9SChuck Lever 		goto validate_failed;
1675a0584ee9SChuck Lever 	len = be32_to_cpup(p);
1676a0584ee9SChuck Lever 	if (len > RPC_MAX_AUTH_SIZE)
1677a0584ee9SChuck Lever 		goto validate_failed;
1678a0584ee9SChuck Lever 	p = xdr_inline_decode(xdr, len);
1679a0584ee9SChuck Lever 	if (!p)
1680a0584ee9SChuck Lever 		goto validate_failed;
16811da177e4SLinus Torvalds 
16822876a344SJ. Bruce Fields 	seq = kmalloc(4, GFP_NOFS);
16832876a344SJ. Bruce Fields 	if (!seq)
1684a0584ee9SChuck Lever 		goto validate_failed;
1685a0584ee9SChuck Lever 	*seq = cpu_to_be32(task->tk_rqstp->rq_seqno);
16862876a344SJ. Bruce Fields 	iov.iov_base = seq;
16872876a344SJ. Bruce Fields 	iov.iov_len = 4;
16881da177e4SLinus Torvalds 	xdr_buf_from_iov(&iov, &verf_buf);
16891da177e4SLinus Torvalds 	mic.data = (u8 *)p;
16901da177e4SLinus Torvalds 	mic.len = len;
169100fd6e14SJ. Bruce Fields 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
16921da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1693fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
1694a0584ee9SChuck Lever 	if (maj_stat)
1695a0584ee9SChuck Lever 		goto bad_mic;
1696a0584ee9SChuck Lever 
169724b2605bSJ. Bruce Fields 	/* We leave it to unwrap to calculate au_rslack. For now we just
169824b2605bSJ. Bruce Fields 	 * calculate the length of the verifier: */
16991be27f36STrond Myklebust 	cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
1700a0584ee9SChuck Lever 	status = 0;
1701a0584ee9SChuck Lever out:
17021da177e4SLinus Torvalds 	gss_put_ctx(ctx);
17032876a344SJ. Bruce Fields 	kfree(seq);
1704a0584ee9SChuck Lever 	return status;
1705a0584ee9SChuck Lever 
1706a0584ee9SChuck Lever validate_failed:
1707a0584ee9SChuck Lever 	status = -EIO;
1708a0584ee9SChuck Lever 	goto out;
1709a0584ee9SChuck Lever bad_mic:
17100c77668dSChuck Lever 	trace_rpcgss_verify_mic(task, maj_stat);
1711a0584ee9SChuck Lever 	status = -EACCES;
1712a0584ee9SChuck Lever 	goto out;
17131da177e4SLinus Torvalds }
17141da177e4SLinus Torvalds 
1715e8680a24SChuck Lever static int gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1716e8680a24SChuck Lever 			      struct rpc_task *task, struct xdr_stream *xdr)
17179f06c719SChuck Lever {
1718e8680a24SChuck Lever 	struct rpc_rqst *rqstp = task->tk_rqstp;
1719e8680a24SChuck Lever 	struct xdr_buf integ_buf, *snd_buf = &rqstp->rq_snd_buf;
17201da177e4SLinus Torvalds 	struct xdr_netobj mic;
1721e8680a24SChuck Lever 	__be32 *p, *integ_len;
1722e8680a24SChuck Lever 	u32 offset, maj_stat;
17231da177e4SLinus Torvalds 
1724e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, 2 * sizeof(*p));
1725e8680a24SChuck Lever 	if (!p)
1726e8680a24SChuck Lever 		goto wrap_failed;
17271da177e4SLinus Torvalds 	integ_len = p++;
1728e8680a24SChuck Lever 	*p = cpu_to_be32(rqstp->rq_seqno);
1729e8680a24SChuck Lever 
1730e8680a24SChuck Lever 	if (rpcauth_wrap_req_encode(task, xdr))
1731e8680a24SChuck Lever 		goto wrap_failed;
1732e8680a24SChuck Lever 
17331da177e4SLinus Torvalds 	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
17341da177e4SLinus Torvalds 	if (xdr_buf_subsegment(snd_buf, &integ_buf,
17351da177e4SLinus Torvalds 				offset, snd_buf->len - offset))
1736e8680a24SChuck Lever 		goto wrap_failed;
1737e8680a24SChuck Lever 	*integ_len = cpu_to_be32(integ_buf.len);
17381da177e4SLinus Torvalds 
1739e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, 0);
1740e8680a24SChuck Lever 	if (!p)
1741e8680a24SChuck Lever 		goto wrap_failed;
17421da177e4SLinus Torvalds 	mic.data = (u8 *)(p + 1);
174300fd6e14SJ. Bruce Fields 	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
17441da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1745fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
17461da177e4SLinus Torvalds 	else if (maj_stat)
17470c77668dSChuck Lever 		goto bad_mic;
1748e8680a24SChuck Lever 	/* Check that the trailing MIC fit in the buffer, after the fact */
1749e8680a24SChuck Lever 	if (xdr_stream_encode_opaque_inline(xdr, (void **)&p, mic.len) < 0)
1750e8680a24SChuck Lever 		goto wrap_failed;
17511da177e4SLinus Torvalds 	return 0;
1752e8680a24SChuck Lever wrap_failed:
1753e8680a24SChuck Lever 	return -EMSGSIZE;
17540c77668dSChuck Lever bad_mic:
17550c77668dSChuck Lever 	trace_rpcgss_get_mic(task, maj_stat);
17560c77668dSChuck Lever 	return -EIO;
17571da177e4SLinus Torvalds }
17581da177e4SLinus Torvalds 
17592d2da60cSJ. Bruce Fields static void
17602d2da60cSJ. Bruce Fields priv_release_snd_buf(struct rpc_rqst *rqstp)
17612d2da60cSJ. Bruce Fields {
17622d2da60cSJ. Bruce Fields 	int i;
17632d2da60cSJ. Bruce Fields 
17642d2da60cSJ. Bruce Fields 	for (i=0; i < rqstp->rq_enc_pages_num; i++)
17652d2da60cSJ. Bruce Fields 		__free_page(rqstp->rq_enc_pages[i]);
17662d2da60cSJ. Bruce Fields 	kfree(rqstp->rq_enc_pages);
17678dae5398SChuck Lever 	rqstp->rq_release_snd_buf = NULL;
17682d2da60cSJ. Bruce Fields }
17692d2da60cSJ. Bruce Fields 
17702d2da60cSJ. Bruce Fields static int
17712d2da60cSJ. Bruce Fields alloc_enc_pages(struct rpc_rqst *rqstp)
17722d2da60cSJ. Bruce Fields {
17732d2da60cSJ. Bruce Fields 	struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
17742d2da60cSJ. Bruce Fields 	int first, last, i;
17752d2da60cSJ. Bruce Fields 
17768dae5398SChuck Lever 	if (rqstp->rq_release_snd_buf)
17778dae5398SChuck Lever 		rqstp->rq_release_snd_buf(rqstp);
17788dae5398SChuck Lever 
17792d2da60cSJ. Bruce Fields 	if (snd_buf->page_len == 0) {
17802d2da60cSJ. Bruce Fields 		rqstp->rq_enc_pages_num = 0;
17812d2da60cSJ. Bruce Fields 		return 0;
17822d2da60cSJ. Bruce Fields 	}
17832d2da60cSJ. Bruce Fields 
178409cbfeafSKirill A. Shutemov 	first = snd_buf->page_base >> PAGE_SHIFT;
178509cbfeafSKirill A. Shutemov 	last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_SHIFT;
17862d2da60cSJ. Bruce Fields 	rqstp->rq_enc_pages_num = last - first + 1 + 1;
17872d2da60cSJ. Bruce Fields 	rqstp->rq_enc_pages
17886da2ec56SKees Cook 		= kmalloc_array(rqstp->rq_enc_pages_num,
17896da2ec56SKees Cook 				sizeof(struct page *),
17902d2da60cSJ. Bruce Fields 				GFP_NOFS);
17912d2da60cSJ. Bruce Fields 	if (!rqstp->rq_enc_pages)
17922d2da60cSJ. Bruce Fields 		goto out;
17932d2da60cSJ. Bruce Fields 	for (i=0; i < rqstp->rq_enc_pages_num; i++) {
17942d2da60cSJ. Bruce Fields 		rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
17952d2da60cSJ. Bruce Fields 		if (rqstp->rq_enc_pages[i] == NULL)
17962d2da60cSJ. Bruce Fields 			goto out_free;
17972d2da60cSJ. Bruce Fields 	}
17982d2da60cSJ. Bruce Fields 	rqstp->rq_release_snd_buf = priv_release_snd_buf;
17992d2da60cSJ. Bruce Fields 	return 0;
18002d2da60cSJ. Bruce Fields out_free:
1801cdead7cfSTrond Myklebust 	rqstp->rq_enc_pages_num = i;
1802cdead7cfSTrond Myklebust 	priv_release_snd_buf(rqstp);
18032d2da60cSJ. Bruce Fields out:
18042d2da60cSJ. Bruce Fields 	return -EAGAIN;
18052d2da60cSJ. Bruce Fields }
18062d2da60cSJ. Bruce Fields 
1807e8680a24SChuck Lever static int gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1808e8680a24SChuck Lever 			     struct rpc_task *task, struct xdr_stream *xdr)
18092d2da60cSJ. Bruce Fields {
1810e8680a24SChuck Lever 	struct rpc_rqst *rqstp = task->tk_rqstp;
18112d2da60cSJ. Bruce Fields 	struct xdr_buf	*snd_buf = &rqstp->rq_snd_buf;
1812e8680a24SChuck Lever 	u32		pad, offset, maj_stat;
18132d2da60cSJ. Bruce Fields 	int		status;
1814e8680a24SChuck Lever 	__be32		*p, *opaque_len;
18152d2da60cSJ. Bruce Fields 	struct page	**inpages;
18162d2da60cSJ. Bruce Fields 	int		first;
18172d2da60cSJ. Bruce Fields 	struct kvec	*iov;
18182d2da60cSJ. Bruce Fields 
1819e8680a24SChuck Lever 	status = -EIO;
1820e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, 2 * sizeof(*p));
1821e8680a24SChuck Lever 	if (!p)
1822e8680a24SChuck Lever 		goto wrap_failed;
18232d2da60cSJ. Bruce Fields 	opaque_len = p++;
1824e8680a24SChuck Lever 	*p = cpu_to_be32(rqstp->rq_seqno);
18252d2da60cSJ. Bruce Fields 
1826e8680a24SChuck Lever 	if (rpcauth_wrap_req_encode(task, xdr))
1827e8680a24SChuck Lever 		goto wrap_failed;
18282d2da60cSJ. Bruce Fields 
18292d2da60cSJ. Bruce Fields 	status = alloc_enc_pages(rqstp);
1830e8680a24SChuck Lever 	if (unlikely(status))
1831e8680a24SChuck Lever 		goto wrap_failed;
183209cbfeafSKirill A. Shutemov 	first = snd_buf->page_base >> PAGE_SHIFT;
18332d2da60cSJ. Bruce Fields 	inpages = snd_buf->pages + first;
18342d2da60cSJ. Bruce Fields 	snd_buf->pages = rqstp->rq_enc_pages;
183509cbfeafSKirill A. Shutemov 	snd_buf->page_base -= first << PAGE_SHIFT;
18367561042fSKevin Coffman 	/*
1837e8680a24SChuck Lever 	 * Move the tail into its own page, in case gss_wrap needs
1838e8680a24SChuck Lever 	 * more space in the head when wrapping.
18397561042fSKevin Coffman 	 *
1840e8680a24SChuck Lever 	 * Still... Why can't gss_wrap just slide the tail down?
18417561042fSKevin Coffman 	 */
18422d2da60cSJ. Bruce Fields 	if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
1843e8680a24SChuck Lever 		char *tmp;
1844e8680a24SChuck Lever 
18452d2da60cSJ. Bruce Fields 		tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
18462d2da60cSJ. Bruce Fields 		memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
18472d2da60cSJ. Bruce Fields 		snd_buf->tail[0].iov_base = tmp;
18482d2da60cSJ. Bruce Fields 	}
1849e8680a24SChuck Lever 	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
185000fd6e14SJ. Bruce Fields 	maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
18517561042fSKevin Coffman 	/* slack space should prevent this ever happening: */
1852e8680a24SChuck Lever 	if (unlikely(snd_buf->len > snd_buf->buflen))
1853e8680a24SChuck Lever 		goto wrap_failed;
18542d2da60cSJ. Bruce Fields 	/* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
18552d2da60cSJ. Bruce Fields 	 * done anyway, so it's safe to put the request on the wire: */
18562d2da60cSJ. Bruce Fields 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1857fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
18582d2da60cSJ. Bruce Fields 	else if (maj_stat)
18590c77668dSChuck Lever 		goto bad_wrap;
18602d2da60cSJ. Bruce Fields 
1861e8680a24SChuck Lever 	*opaque_len = cpu_to_be32(snd_buf->len - offset);
1862e8680a24SChuck Lever 	/* guess whether the pad goes into the head or the tail: */
18632d2da60cSJ. Bruce Fields 	if (snd_buf->page_len || snd_buf->tail[0].iov_len)
18642d2da60cSJ. Bruce Fields 		iov = snd_buf->tail;
18652d2da60cSJ. Bruce Fields 	else
18662d2da60cSJ. Bruce Fields 		iov = snd_buf->head;
18672d2da60cSJ. Bruce Fields 	p = iov->iov_base + iov->iov_len;
18682d2da60cSJ. Bruce Fields 	pad = 3 - ((snd_buf->len - offset - 1) & 3);
18692d2da60cSJ. Bruce Fields 	memset(p, 0, pad);
18702d2da60cSJ. Bruce Fields 	iov->iov_len += pad;
18712d2da60cSJ. Bruce Fields 	snd_buf->len += pad;
18722d2da60cSJ. Bruce Fields 
18732d2da60cSJ. Bruce Fields 	return 0;
1874e8680a24SChuck Lever wrap_failed:
1875e8680a24SChuck Lever 	return status;
18760c77668dSChuck Lever bad_wrap:
18770c77668dSChuck Lever 	trace_rpcgss_wrap(task, maj_stat);
18780c77668dSChuck Lever 	return -EIO;
18792d2da60cSJ. Bruce Fields }
18802d2da60cSJ. Bruce Fields 
1881e8680a24SChuck Lever static int gss_wrap_req(struct rpc_task *task, struct xdr_stream *xdr)
18821da177e4SLinus Torvalds {
1883a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
18841da177e4SLinus Torvalds 	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
18851da177e4SLinus Torvalds 			gc_base);
18861da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1887e8680a24SChuck Lever 	int status;
18881da177e4SLinus Torvalds 
1889e8680a24SChuck Lever 	status = -EIO;
18901da177e4SLinus Torvalds 	if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
18911da177e4SLinus Torvalds 		/* The spec seems a little ambiguous here, but I think that not
18921da177e4SLinus Torvalds 		 * wrapping context destruction requests makes the most sense.
18931da177e4SLinus Torvalds 		 */
1894e8680a24SChuck Lever 		status = rpcauth_wrap_req_encode(task, xdr);
18951da177e4SLinus Torvalds 		goto out;
18961da177e4SLinus Torvalds 	}
18971da177e4SLinus Torvalds 	switch (gss_cred->gc_service) {
18981da177e4SLinus Torvalds 	case RPC_GSS_SVC_NONE:
1899e8680a24SChuck Lever 		status = rpcauth_wrap_req_encode(task, xdr);
19001da177e4SLinus Torvalds 		break;
19011da177e4SLinus Torvalds 	case RPC_GSS_SVC_INTEGRITY:
1902e8680a24SChuck Lever 		status = gss_wrap_req_integ(cred, ctx, task, xdr);
19031da177e4SLinus Torvalds 		break;
19041da177e4SLinus Torvalds 	case RPC_GSS_SVC_PRIVACY:
1905e8680a24SChuck Lever 		status = gss_wrap_req_priv(cred, ctx, task, xdr);
19061da177e4SLinus Torvalds 		break;
19070c77668dSChuck Lever 	default:
19080c77668dSChuck Lever 		status = -EIO;
19091da177e4SLinus Torvalds 	}
19101da177e4SLinus Torvalds out:
19111da177e4SLinus Torvalds 	gss_put_ctx(ctx);
19121da177e4SLinus Torvalds 	return status;
19131da177e4SLinus Torvalds }
19141da177e4SLinus Torvalds 
1915a0584ee9SChuck Lever static int
1916a0584ee9SChuck Lever gss_unwrap_resp_auth(struct rpc_cred *cred)
19171da177e4SLinus Torvalds {
1918a0584ee9SChuck Lever 	cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize;
1919a0584ee9SChuck Lever 	return 0;
1920a0584ee9SChuck Lever }
19211da177e4SLinus Torvalds 
1922a0584ee9SChuck Lever static int
19230c77668dSChuck Lever gss_unwrap_resp_integ(struct rpc_task *task, struct rpc_cred *cred,
19240c77668dSChuck Lever 		      struct gss_cl_ctx *ctx, struct rpc_rqst *rqstp,
19250c77668dSChuck Lever 		      struct xdr_stream *xdr)
1926a0584ee9SChuck Lever {
1927a0584ee9SChuck Lever 	struct xdr_buf integ_buf, *rcv_buf = &rqstp->rq_rcv_buf;
1928a0584ee9SChuck Lever 	u32 data_offset, mic_offset, integ_len, maj_stat;
1929a0584ee9SChuck Lever 	struct xdr_netobj mic;
1930a0584ee9SChuck Lever 	__be32 *p;
1931a0584ee9SChuck Lever 
1932a0584ee9SChuck Lever 	p = xdr_inline_decode(xdr, 2 * sizeof(*p));
1933a0584ee9SChuck Lever 	if (unlikely(!p))
1934a0584ee9SChuck Lever 		goto unwrap_failed;
1935a0584ee9SChuck Lever 	integ_len = be32_to_cpup(p++);
19361da177e4SLinus Torvalds 	if (integ_len & 3)
1937a0584ee9SChuck Lever 		goto unwrap_failed;
1938a0584ee9SChuck Lever 	data_offset = (u8 *)(p) - (u8 *)rcv_buf->head[0].iov_base;
19391da177e4SLinus Torvalds 	mic_offset = integ_len + data_offset;
19401da177e4SLinus Torvalds 	if (mic_offset > rcv_buf->len)
1941a0584ee9SChuck Lever 		goto unwrap_failed;
1942a0584ee9SChuck Lever 	if (be32_to_cpup(p) != rqstp->rq_seqno)
19430c77668dSChuck Lever 		goto bad_seqno;
19441da177e4SLinus Torvalds 
1945a0584ee9SChuck Lever 	if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset, integ_len))
1946a0584ee9SChuck Lever 		goto unwrap_failed;
19471da177e4SLinus Torvalds 	if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
1948a0584ee9SChuck Lever 		goto unwrap_failed;
194900fd6e14SJ. Bruce Fields 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
19501da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1951fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
19521da177e4SLinus Torvalds 	if (maj_stat != GSS_S_COMPLETE)
1953a0584ee9SChuck Lever 		goto bad_mic;
1954a0584ee9SChuck Lever 
1955a0584ee9SChuck Lever 	cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + 2 +
1956a0584ee9SChuck Lever 				   1 + XDR_QUADLEN(mic.len);
19571da177e4SLinus Torvalds 	return 0;
1958a0584ee9SChuck Lever unwrap_failed:
19590c77668dSChuck Lever 	trace_rpcgss_unwrap_failed(task);
19600c77668dSChuck Lever 	return -EIO;
19610c77668dSChuck Lever bad_seqno:
19620c77668dSChuck Lever 	trace_rpcgss_bad_seqno(task, rqstp->rq_seqno, be32_to_cpup(p));
1963a0584ee9SChuck Lever 	return -EIO;
1964a0584ee9SChuck Lever bad_mic:
19650c77668dSChuck Lever 	trace_rpcgss_verify_mic(task, maj_stat);
1966a0584ee9SChuck Lever 	return -EIO;
19671da177e4SLinus Torvalds }
19681da177e4SLinus Torvalds 
1969a0584ee9SChuck Lever static int
19700c77668dSChuck Lever gss_unwrap_resp_priv(struct rpc_task *task, struct rpc_cred *cred,
19710c77668dSChuck Lever 		     struct gss_cl_ctx *ctx, struct rpc_rqst *rqstp,
19720c77668dSChuck Lever 		     struct xdr_stream *xdr)
19732d2da60cSJ. Bruce Fields {
19742d2da60cSJ. Bruce Fields 	struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
1975a0584ee9SChuck Lever 	struct kvec *head = rqstp->rq_rcv_buf.head;
1976a0584ee9SChuck Lever 	unsigned int savedlen = rcv_buf->len;
1977a0584ee9SChuck Lever 	u32 offset, opaque_len, maj_stat;
1978a0584ee9SChuck Lever 	__be32 *p;
19792d2da60cSJ. Bruce Fields 
1980a0584ee9SChuck Lever 	p = xdr_inline_decode(xdr, 2 * sizeof(*p));
1981a0584ee9SChuck Lever 	if (unlikely(!p))
1982a0584ee9SChuck Lever 		goto unwrap_failed;
1983a0584ee9SChuck Lever 	opaque_len = be32_to_cpup(p++);
1984a0584ee9SChuck Lever 	offset = (u8 *)(p) - (u8 *)head->iov_base;
19852d2da60cSJ. Bruce Fields 	if (offset + opaque_len > rcv_buf->len)
1986a0584ee9SChuck Lever 		goto unwrap_failed;
19872d2da60cSJ. Bruce Fields 	rcv_buf->len = offset + opaque_len;
19882d2da60cSJ. Bruce Fields 
198900fd6e14SJ. Bruce Fields 	maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
19902d2da60cSJ. Bruce Fields 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1991fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
19922d2da60cSJ. Bruce Fields 	if (maj_stat != GSS_S_COMPLETE)
1993a0584ee9SChuck Lever 		goto bad_unwrap;
1994a0584ee9SChuck Lever 	/* gss_unwrap decrypted the sequence number */
1995a0584ee9SChuck Lever 	if (be32_to_cpup(p++) != rqstp->rq_seqno)
19960c77668dSChuck Lever 		goto bad_seqno;
19972d2da60cSJ. Bruce Fields 
1998a0584ee9SChuck Lever 	/* gss_unwrap redacts the opaque blob from the head iovec.
1999a0584ee9SChuck Lever 	 * rcv_buf has changed, thus the stream needs to be reset.
2000a0584ee9SChuck Lever 	 */
2001a0584ee9SChuck Lever 	xdr_init_decode(xdr, rcv_buf, p, rqstp);
2002a0584ee9SChuck Lever 
2003a0584ee9SChuck Lever 	cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + 2 +
2004a0584ee9SChuck Lever 				   XDR_QUADLEN(savedlen - rcv_buf->len);
20052d2da60cSJ. Bruce Fields 	return 0;
2006a0584ee9SChuck Lever unwrap_failed:
20070c77668dSChuck Lever 	trace_rpcgss_unwrap_failed(task);
20080c77668dSChuck Lever 	return -EIO;
20090c77668dSChuck Lever bad_seqno:
20100c77668dSChuck Lever 	trace_rpcgss_bad_seqno(task, rqstp->rq_seqno, be32_to_cpup(--p));
2011a0584ee9SChuck Lever 	return -EIO;
2012a0584ee9SChuck Lever bad_unwrap:
20130c77668dSChuck Lever 	trace_rpcgss_unwrap(task, maj_stat);
2014a0584ee9SChuck Lever 	return -EIO;
2015bf269551SChuck Lever }
20162d2da60cSJ. Bruce Fields 
20173021a5bbSTrond Myklebust static bool
20183021a5bbSTrond Myklebust gss_seq_is_newer(u32 new, u32 old)
20193021a5bbSTrond Myklebust {
20203021a5bbSTrond Myklebust 	return (s32)(new - old) > 0;
20213021a5bbSTrond Myklebust }
20223021a5bbSTrond Myklebust 
20233021a5bbSTrond Myklebust static bool
20243021a5bbSTrond Myklebust gss_xmit_need_reencode(struct rpc_task *task)
20253021a5bbSTrond Myklebust {
20263021a5bbSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
20273021a5bbSTrond Myklebust 	struct rpc_cred *cred = req->rq_cred;
20283021a5bbSTrond Myklebust 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
20290c77668dSChuck Lever 	u32 win, seq_xmit = 0;
20303021a5bbSTrond Myklebust 	bool ret = true;
20313021a5bbSTrond Myklebust 
20323021a5bbSTrond Myklebust 	if (!ctx)
20330c77668dSChuck Lever 		goto out;
20343021a5bbSTrond Myklebust 
20353021a5bbSTrond Myklebust 	if (gss_seq_is_newer(req->rq_seqno, READ_ONCE(ctx->gc_seq)))
20360c77668dSChuck Lever 		goto out_ctx;
20373021a5bbSTrond Myklebust 
20383021a5bbSTrond Myklebust 	seq_xmit = READ_ONCE(ctx->gc_seq_xmit);
20393021a5bbSTrond Myklebust 	while (gss_seq_is_newer(req->rq_seqno, seq_xmit)) {
20403021a5bbSTrond Myklebust 		u32 tmp = seq_xmit;
20413021a5bbSTrond Myklebust 
20423021a5bbSTrond Myklebust 		seq_xmit = cmpxchg(&ctx->gc_seq_xmit, tmp, req->rq_seqno);
20433021a5bbSTrond Myklebust 		if (seq_xmit == tmp) {
20443021a5bbSTrond Myklebust 			ret = false;
20450c77668dSChuck Lever 			goto out_ctx;
20463021a5bbSTrond Myklebust 		}
20473021a5bbSTrond Myklebust 	}
20483021a5bbSTrond Myklebust 
20493021a5bbSTrond Myklebust 	win = ctx->gc_win;
20503021a5bbSTrond Myklebust 	if (win > 0)
20513021a5bbSTrond Myklebust 		ret = !gss_seq_is_newer(req->rq_seqno, seq_xmit - win);
20520c77668dSChuck Lever 
20530c77668dSChuck Lever out_ctx:
20543021a5bbSTrond Myklebust 	gss_put_ctx(ctx);
20550c77668dSChuck Lever out:
20560c77668dSChuck Lever 	trace_rpcgss_need_reencode(task, seq_xmit, ret);
20573021a5bbSTrond Myklebust 	return ret;
20583021a5bbSTrond Myklebust }
20593021a5bbSTrond Myklebust 
20601da177e4SLinus Torvalds static int
2061a0584ee9SChuck Lever gss_unwrap_resp(struct rpc_task *task, struct xdr_stream *xdr)
20621da177e4SLinus Torvalds {
2063a0584ee9SChuck Lever 	struct rpc_rqst *rqstp = task->tk_rqstp;
2064a0584ee9SChuck Lever 	struct rpc_cred *cred = rqstp->rq_cred;
20651da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
20661da177e4SLinus Torvalds 			gc_base);
20671da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
20681da177e4SLinus Torvalds 	int status = -EIO;
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds 	if (ctx->gc_proc != RPC_GSS_PROC_DATA)
20711da177e4SLinus Torvalds 		goto out_decode;
20721da177e4SLinus Torvalds 	switch (gss_cred->gc_service) {
20731da177e4SLinus Torvalds 	case RPC_GSS_SVC_NONE:
2074a0584ee9SChuck Lever 		status = gss_unwrap_resp_auth(cred);
20751da177e4SLinus Torvalds 		break;
20761da177e4SLinus Torvalds 	case RPC_GSS_SVC_INTEGRITY:
20770c77668dSChuck Lever 		status = gss_unwrap_resp_integ(task, cred, ctx, rqstp, xdr);
20781da177e4SLinus Torvalds 		break;
20791da177e4SLinus Torvalds 	case RPC_GSS_SVC_PRIVACY:
20800c77668dSChuck Lever 		status = gss_unwrap_resp_priv(task, cred, ctx, rqstp, xdr);
20811da177e4SLinus Torvalds 		break;
20821da177e4SLinus Torvalds 	}
2083a0584ee9SChuck Lever 	if (status)
2084a0584ee9SChuck Lever 		goto out;
2085a0584ee9SChuck Lever 
20861da177e4SLinus Torvalds out_decode:
2087a0584ee9SChuck Lever 	status = rpcauth_unwrap_resp_decode(task, xdr);
20881da177e4SLinus Torvalds out:
20891da177e4SLinus Torvalds 	gss_put_ctx(ctx);
20901da177e4SLinus Torvalds 	return status;
20911da177e4SLinus Torvalds }
20921da177e4SLinus Torvalds 
2093f1c0a861STrond Myklebust static const struct rpc_authops authgss_ops = {
20941da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
20951da177e4SLinus Torvalds 	.au_flavor	= RPC_AUTH_GSS,
20961da177e4SLinus Torvalds 	.au_name	= "RPCSEC_GSS",
20971da177e4SLinus Torvalds 	.create		= gss_create,
20981da177e4SLinus Torvalds 	.destroy	= gss_destroy,
2099a960f8d6SFrank Sorenson 	.hash_cred	= gss_hash_cred,
21001da177e4SLinus Torvalds 	.lookup_cred	= gss_lookup_cred,
210180df9d20SStanislav Kinsbursky 	.crcreate	= gss_create_cred,
21026a1a1e34SChuck Lever 	.list_pseudoflavors = gss_mech_list_pseudoflavors,
21039568c5e9SChuck Lever 	.info2flavor	= gss_mech_info2flavor,
2104a77c806fSChuck Lever 	.flavor2info	= gss_mech_flavor2info,
21051da177e4SLinus Torvalds };
21061da177e4SLinus Torvalds 
2107f1c0a861STrond Myklebust static const struct rpc_credops gss_credops = {
21081da177e4SLinus Torvalds 	.cr_name		= "AUTH_GSS",
21091da177e4SLinus Torvalds 	.crdestroy		= gss_destroy_cred,
2110fba3bad4STrond Myklebust 	.cr_init		= gss_cred_init,
21111da177e4SLinus Torvalds 	.crmatch		= gss_match,
21121da177e4SLinus Torvalds 	.crmarshal		= gss_marshal,
21131da177e4SLinus Torvalds 	.crrefresh		= gss_refresh,
21141da177e4SLinus Torvalds 	.crvalidate		= gss_validate,
21151da177e4SLinus Torvalds 	.crwrap_req		= gss_wrap_req,
21161da177e4SLinus Torvalds 	.crunwrap_resp		= gss_unwrap_resp,
21174de6caa2SAndy Adamson 	.crkey_timeout		= gss_key_timeout,
2118a0337d1dSJeff Layton 	.crstringify_acceptor	= gss_stringify_acceptor,
21193021a5bbSTrond Myklebust 	.crneed_reencode	= gss_xmit_need_reencode,
21201da177e4SLinus Torvalds };
21211da177e4SLinus Torvalds 
21220df7fb74STrond Myklebust static const struct rpc_credops gss_nullops = {
21230df7fb74STrond Myklebust 	.cr_name		= "AUTH_GSS",
21246dcd3926SJeff Layton 	.crdestroy		= gss_destroy_nullcred,
21250df7fb74STrond Myklebust 	.crmatch		= gss_match,
21260df7fb74STrond Myklebust 	.crmarshal		= gss_marshal,
21270df7fb74STrond Myklebust 	.crrefresh		= gss_refresh_null,
21280df7fb74STrond Myklebust 	.crvalidate		= gss_validate,
21290df7fb74STrond Myklebust 	.crwrap_req		= gss_wrap_req,
21300df7fb74STrond Myklebust 	.crunwrap_resp		= gss_unwrap_resp,
2131a0337d1dSJeff Layton 	.crstringify_acceptor	= gss_stringify_acceptor,
21320df7fb74STrond Myklebust };
21330df7fb74STrond Myklebust 
2134b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v0 = {
2135c1225158SPeng Tao 	.upcall		= rpc_pipe_generic_upcall,
21361da177e4SLinus Torvalds 	.downcall	= gss_pipe_downcall,
21371da177e4SLinus Torvalds 	.destroy_msg	= gss_pipe_destroy_msg,
213834769fc4S\"J. Bruce Fields\ 	.open_pipe	= gss_pipe_open_v0,
213934769fc4S\"J. Bruce Fields\ 	.release_pipe	= gss_pipe_release,
214034769fc4S\"J. Bruce Fields\ };
214134769fc4S\"J. Bruce Fields\ 
2142b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v1 = {
2143c1225158SPeng Tao 	.upcall		= rpc_pipe_generic_upcall,
214434769fc4S\"J. Bruce Fields\ 	.downcall	= gss_pipe_downcall,
214534769fc4S\"J. Bruce Fields\ 	.destroy_msg	= gss_pipe_destroy_msg,
214634769fc4S\"J. Bruce Fields\ 	.open_pipe	= gss_pipe_open_v1,
21471da177e4SLinus Torvalds 	.release_pipe	= gss_pipe_release,
21481da177e4SLinus Torvalds };
21491da177e4SLinus Torvalds 
2150a1db410dSStanislav Kinsbursky static __net_init int rpcsec_gss_init_net(struct net *net)
2151a1db410dSStanislav Kinsbursky {
2152a1db410dSStanislav Kinsbursky 	return gss_svc_init_net(net);
2153a1db410dSStanislav Kinsbursky }
2154a1db410dSStanislav Kinsbursky 
2155a1db410dSStanislav Kinsbursky static __net_exit void rpcsec_gss_exit_net(struct net *net)
2156a1db410dSStanislav Kinsbursky {
2157a1db410dSStanislav Kinsbursky 	gss_svc_shutdown_net(net);
2158a1db410dSStanislav Kinsbursky }
2159a1db410dSStanislav Kinsbursky 
2160a1db410dSStanislav Kinsbursky static struct pernet_operations rpcsec_gss_net_ops = {
2161a1db410dSStanislav Kinsbursky 	.init = rpcsec_gss_init_net,
2162a1db410dSStanislav Kinsbursky 	.exit = rpcsec_gss_exit_net,
2163a1db410dSStanislav Kinsbursky };
2164a1db410dSStanislav Kinsbursky 
21651da177e4SLinus Torvalds /*
21661da177e4SLinus Torvalds  * Initialize RPCSEC_GSS module
21671da177e4SLinus Torvalds  */
21681da177e4SLinus Torvalds static int __init init_rpcsec_gss(void)
21691da177e4SLinus Torvalds {
21701da177e4SLinus Torvalds 	int err = 0;
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds 	err = rpcauth_register(&authgss_ops);
21731da177e4SLinus Torvalds 	if (err)
21741da177e4SLinus Torvalds 		goto out;
21751da177e4SLinus Torvalds 	err = gss_svc_init();
21761da177e4SLinus Torvalds 	if (err)
21771da177e4SLinus Torvalds 		goto out_unregister;
2178a1db410dSStanislav Kinsbursky 	err = register_pernet_subsys(&rpcsec_gss_net_ops);
2179a1db410dSStanislav Kinsbursky 	if (err)
2180a1db410dSStanislav Kinsbursky 		goto out_svc_exit;
218179a3f20bS\"J. Bruce Fields\ 	rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version");
21821da177e4SLinus Torvalds 	return 0;
2183a1db410dSStanislav Kinsbursky out_svc_exit:
2184a1db410dSStanislav Kinsbursky 	gss_svc_shutdown();
21851da177e4SLinus Torvalds out_unregister:
21861da177e4SLinus Torvalds 	rpcauth_unregister(&authgss_ops);
21871da177e4SLinus Torvalds out:
21881da177e4SLinus Torvalds 	return err;
21891da177e4SLinus Torvalds }
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds static void __exit exit_rpcsec_gss(void)
21921da177e4SLinus Torvalds {
2193a1db410dSStanislav Kinsbursky 	unregister_pernet_subsys(&rpcsec_gss_net_ops);
21941da177e4SLinus Torvalds 	gss_svc_shutdown();
21951da177e4SLinus Torvalds 	rpcauth_unregister(&authgss_ops);
2196bf12691dSJesper Dangaard Brouer 	rcu_barrier(); /* Wait for completion of call_rcu()'s */
21971da177e4SLinus Torvalds }
21981da177e4SLinus Torvalds 
219971afa85eSChuck Lever MODULE_ALIAS("rpc-auth-6");
22001da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2201126e216aSTrond Myklebust module_param_named(expired_cred_retry_delay,
2202126e216aSTrond Myklebust 		   gss_expired_cred_retry_delay,
2203126e216aSTrond Myklebust 		   uint, 0644);
2204126e216aSTrond Myklebust MODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until "
2205126e216aSTrond Myklebust 		"the RPC engine retries an expired credential");
2206126e216aSTrond Myklebust 
22074de6caa2SAndy Adamson module_param_named(key_expire_timeo,
22084de6caa2SAndy Adamson 		   gss_key_expire_timeo,
22094de6caa2SAndy Adamson 		   uint, 0644);
22104de6caa2SAndy Adamson MODULE_PARM_DESC(key_expire_timeo, "Time (in seconds) at the end of a "
22114de6caa2SAndy Adamson 		"credential keys lifetime where the NFS layer cleans up "
22124de6caa2SAndy Adamson 		"prior to key expiration");
22134de6caa2SAndy Adamson 
22141da177e4SLinus Torvalds module_init(init_rpcsec_gss)
22151da177e4SLinus Torvalds module_exit(exit_rpcsec_gss)
2216