xref: /openbmc/linux/net/sunrpc/auth_gss/auth_gss.c (revision ac83228a)
12573a464SChuck Lever // SPDX-License-Identifier: BSD-3-Clause
21da177e4SLinus Torvalds /*
3f30c2269SUwe Zeisberger  * linux/net/sunrpc/auth_gss/auth_gss.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * RPCSEC_GSS client authentication.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Copyright (c) 2000 The Regents of the University of Michigan.
81da177e4SLinus Torvalds  *  All rights reserved.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  Dug Song       <dugsong@monkey.org>
111da177e4SLinus Torvalds  *  Andy Adamson   <andros@umich.edu>
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/types.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/sched.h>
192d2da60cSJ. Bruce Fields #include <linux/pagemap.h>
201da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
211da177e4SLinus Torvalds #include <linux/sunrpc/auth.h>
221da177e4SLinus Torvalds #include <linux/sunrpc/auth_gss.h>
231da177e4SLinus Torvalds #include <linux/sunrpc/svcauth_gss.h>
241da177e4SLinus Torvalds #include <linux/sunrpc/gss_err.h>
251da177e4SLinus Torvalds #include <linux/workqueue.h>
261da177e4SLinus Torvalds #include <linux/sunrpc/rpc_pipe_fs.h>
271da177e4SLinus Torvalds #include <linux/sunrpc/gss_api.h>
287c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
29eb6dc19dSTrond Myklebust #include <linux/hashtable.h>
301da177e4SLinus Torvalds 
31abfdbd53STrond Myklebust #include "../netns.h"
32abfdbd53STrond Myklebust 
330c77668dSChuck Lever #include <trace/events/rpcgss.h>
340c77668dSChuck Lever 
35f1c0a861STrond Myklebust static const struct rpc_authops authgss_ops;
361da177e4SLinus Torvalds 
37f1c0a861STrond Myklebust static const struct rpc_credops gss_credops;
380df7fb74STrond Myklebust static const struct rpc_credops gss_nullops;
391da177e4SLinus Torvalds 
40126e216aSTrond Myklebust #define GSS_RETRY_EXPIRED 5
41126e216aSTrond Myklebust static unsigned int gss_expired_cred_retry_delay = GSS_RETRY_EXPIRED;
42126e216aSTrond Myklebust 
434de6caa2SAndy Adamson #define GSS_KEY_EXPIRE_TIMEO 240
444de6caa2SAndy Adamson static unsigned int gss_key_expire_timeo = GSS_KEY_EXPIRE_TIMEO;
454de6caa2SAndy Adamson 
46f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
471da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_AUTH
481da177e4SLinus Torvalds #endif
491da177e4SLinus Torvalds 
50725f2865SKevin Coffman #define GSS_CRED_SLACK		(RPC_MAX_AUTH_SIZE * 2)
511da177e4SLinus Torvalds /* length of a krb5 verifier (48), plus data added before arguments when
521da177e4SLinus Torvalds  * using integrity (two 4-byte integers): */
53adeb8133SOlga Kornievskaia #define GSS_VERF_SLACK		100
541da177e4SLinus Torvalds 
5523c323afSTrond Myklebust static DEFINE_HASHTABLE(gss_auth_hash_table, 4);
56eb6dc19dSTrond Myklebust static DEFINE_SPINLOCK(gss_auth_hash_lock);
57eb6dc19dSTrond Myklebust 
5819172284STrond Myklebust struct gss_pipe {
5919172284STrond Myklebust 	struct rpc_pipe_dir_object pdo;
6019172284STrond Myklebust 	struct rpc_pipe *pipe;
6119172284STrond Myklebust 	struct rpc_clnt *clnt;
6219172284STrond Myklebust 	const char *name;
63414a6295STrond Myklebust 	struct kref kref;
6419172284STrond Myklebust };
6519172284STrond Myklebust 
661da177e4SLinus Torvalds struct gss_auth {
670285ed1fSTrond Myklebust 	struct kref kref;
68eb6dc19dSTrond Myklebust 	struct hlist_node hash;
691da177e4SLinus Torvalds 	struct rpc_auth rpc_auth;
701da177e4SLinus Torvalds 	struct gss_api_mech *mech;
711da177e4SLinus Torvalds 	enum rpc_gss_svc service;
721da177e4SLinus Torvalds 	struct rpc_clnt *client;
73e726340aSTrond Myklebust 	struct net *net;
7434769fc4S\"J. Bruce Fields\ 	/*
7534769fc4S\"J. Bruce Fields\ 	 * There are two upcall pipes; dentry[1], named "gssd", is used
7634769fc4S\"J. Bruce Fields\ 	 * for the new text-based upcall; dentry[0] is named after the
7734769fc4S\"J. Bruce Fields\ 	 * mechanism (for example, "krb5") and exists for
7834769fc4S\"J. Bruce Fields\ 	 * backwards-compatibility with older gssd's.
7934769fc4S\"J. Bruce Fields\ 	 */
8019172284STrond Myklebust 	struct gss_pipe *gss_pipe[2];
81bd4a3eb1STrond Myklebust 	const char *target_name;
821da177e4SLinus Torvalds };
831da177e4SLinus Torvalds 
8479a3f20bS\"J. Bruce Fields\ /* pipe_version >= 0 if and only if someone has a pipe open. */
8579a3f20bS\"J. Bruce Fields\ static DEFINE_SPINLOCK(pipe_version_lock);
8679a3f20bS\"J. Bruce Fields\ static struct rpc_wait_queue pipe_version_rpc_waitqueue;
8779a3f20bS\"J. Bruce Fields\ static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue);
889eb2ddb4STrond Myklebust static void gss_put_auth(struct gss_auth *gss_auth);
89cf81939dS\"J. Bruce Fields\ 
905d28dc82STrond Myklebust static void gss_free_ctx(struct gss_cl_ctx *);
91b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v0;
92b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v1;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds static inline struct gss_cl_ctx *
951da177e4SLinus Torvalds gss_get_ctx(struct gss_cl_ctx *ctx)
961da177e4SLinus Torvalds {
970fa10472SReshetova, Elena 	refcount_inc(&ctx->count);
981da177e4SLinus Torvalds 	return ctx;
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds static inline void
1021da177e4SLinus Torvalds gss_put_ctx(struct gss_cl_ctx *ctx)
1031da177e4SLinus Torvalds {
1040fa10472SReshetova, Elena 	if (refcount_dec_and_test(&ctx->count))
1055d28dc82STrond Myklebust 		gss_free_ctx(ctx);
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
1085d28dc82STrond Myklebust /* gss_cred_set_ctx:
1095d28dc82STrond Myklebust  * called by gss_upcall_callback and gss_create_upcall in order
1105d28dc82STrond Myklebust  * to set the gss context. The actual exchange of an old context
1119beae467SStanislav Kinsbursky  * and a new one is protected by the pipe->lock.
1125d28dc82STrond Myklebust  */
1131da177e4SLinus Torvalds static void
1141da177e4SLinus Torvalds gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1175d28dc82STrond Myklebust 
118cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
119cd019f75STrond Myklebust 		return;
1207b6962b0STrond Myklebust 	gss_get_ctx(ctx);
121cf778b00SEric Dumazet 	rcu_assign_pointer(gss_cred->gc_ctx, ctx);
122fc432dd9STrond Myklebust 	set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
1234e857c58SPeter Zijlstra 	smp_mb__before_atomic();
124fc432dd9STrond Myklebust 	clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds static const void *
1281da177e4SLinus Torvalds simple_get_bytes(const void *p, const void *end, void *res, size_t len)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	const void *q = (const void *)((const char *)p + len);
1311da177e4SLinus Torvalds 	if (unlikely(q > end || q < p))
1321da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
1331da177e4SLinus Torvalds 	memcpy(res, p, len);
1341da177e4SLinus Torvalds 	return q;
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds static inline const void *
1381da177e4SLinus Torvalds simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
1391da177e4SLinus Torvalds {
1401da177e4SLinus Torvalds 	const void *q;
1411da177e4SLinus Torvalds 	unsigned int len;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &len, sizeof(len));
1441da177e4SLinus Torvalds 	if (IS_ERR(p))
1451da177e4SLinus Torvalds 		return p;
1461da177e4SLinus Torvalds 	q = (const void *)((const char *)p + len);
1471da177e4SLinus Torvalds 	if (unlikely(q > end || q < p))
1481da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
1490f38b873STrond Myklebust 	dest->data = kmemdup(p, len, GFP_NOFS);
1501da177e4SLinus Torvalds 	if (unlikely(dest->data == NULL))
1511da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
1521da177e4SLinus Torvalds 	dest->len = len;
1531da177e4SLinus Torvalds 	return q;
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds static struct gss_cl_ctx *
1571da177e4SLinus Torvalds gss_cred_get_ctx(struct rpc_cred *cred)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1601da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = NULL;
1611da177e4SLinus Torvalds 
1625d28dc82STrond Myklebust 	rcu_read_lock();
163c5e6aecdSJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
164c5e6aecdSJeff Layton 	if (ctx)
165c5e6aecdSJeff Layton 		gss_get_ctx(ctx);
1665d28dc82STrond Myklebust 	rcu_read_unlock();
1671da177e4SLinus Torvalds 	return ctx;
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds static struct gss_cl_ctx *
1711da177e4SLinus Torvalds gss_alloc_context(void)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
1741da177e4SLinus Torvalds 
1750f38b873STrond Myklebust 	ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
1761da177e4SLinus Torvalds 	if (ctx != NULL) {
1771da177e4SLinus Torvalds 		ctx->gc_proc = RPC_GSS_PROC_DATA;
1781da177e4SLinus Torvalds 		ctx->gc_seq = 1;	/* NetApp 6.4R1 doesn't accept seq. no. 0 */
1791da177e4SLinus Torvalds 		spin_lock_init(&ctx->gc_seq_lock);
1800fa10472SReshetova, Elena 		refcount_set(&ctx->count,1);
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds 	return ctx;
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds #define GSSD_MIN_TIMEOUT (60 * 60)
1861da177e4SLinus Torvalds static const void *
1871da177e4SLinus Torvalds gss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	const void *q;
1901da177e4SLinus Torvalds 	unsigned int seclen;
1911da177e4SLinus Torvalds 	unsigned int timeout;
192620038f6SAndy Adamson 	unsigned long now = jiffies;
1931da177e4SLinus Torvalds 	u32 window_size;
1941da177e4SLinus Torvalds 	int ret;
1951da177e4SLinus Torvalds 
196620038f6SAndy Adamson 	/* First unsigned int gives the remaining lifetime in seconds of the
197620038f6SAndy Adamson 	 * credential - e.g. the remaining TGT lifetime for Kerberos or
198620038f6SAndy Adamson 	 * the -t value passed to GSSD.
199620038f6SAndy Adamson 	 */
2001da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
2011da177e4SLinus Torvalds 	if (IS_ERR(p))
2021da177e4SLinus Torvalds 		goto err;
2031da177e4SLinus Torvalds 	if (timeout == 0)
2041da177e4SLinus Torvalds 		timeout = GSSD_MIN_TIMEOUT;
205620038f6SAndy Adamson 	ctx->gc_expiry = now + ((unsigned long)timeout * HZ);
206620038f6SAndy Adamson 	/* Sequence number window. Determines the maximum number of
207620038f6SAndy Adamson 	 * simultaneous requests
208620038f6SAndy Adamson 	 */
2091da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
2101da177e4SLinus Torvalds 	if (IS_ERR(p))
2111da177e4SLinus Torvalds 		goto err;
2121da177e4SLinus Torvalds 	ctx->gc_win = window_size;
2131da177e4SLinus Torvalds 	/* gssd signals an error by passing ctx->gc_win = 0: */
2141da177e4SLinus Torvalds 	if (ctx->gc_win == 0) {
215dc5ddce9SJeff Layton 		/*
216dc5ddce9SJeff Layton 		 * in which case, p points to an error code. Anything other
217dc5ddce9SJeff Layton 		 * than -EKEYEXPIRED gets converted to -EACCES.
218dc5ddce9SJeff Layton 		 */
219dc5ddce9SJeff Layton 		p = simple_get_bytes(p, end, &ret, sizeof(ret));
220dc5ddce9SJeff Layton 		if (!IS_ERR(p))
221dc5ddce9SJeff Layton 			p = (ret == -EKEYEXPIRED) ? ERR_PTR(-EKEYEXPIRED) :
222dc5ddce9SJeff Layton 						    ERR_PTR(-EACCES);
2231da177e4SLinus Torvalds 		goto err;
2241da177e4SLinus Torvalds 	}
2251da177e4SLinus Torvalds 	/* copy the opaque wire context */
2261da177e4SLinus Torvalds 	p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
2271da177e4SLinus Torvalds 	if (IS_ERR(p))
2281da177e4SLinus Torvalds 		goto err;
2291da177e4SLinus Torvalds 	/* import the opaque security context */
2301da177e4SLinus Torvalds 	p  = simple_get_bytes(p, end, &seclen, sizeof(seclen));
2311da177e4SLinus Torvalds 	if (IS_ERR(p))
2321da177e4SLinus Torvalds 		goto err;
2331da177e4SLinus Torvalds 	q = (const void *)((const char *)p + seclen);
2341da177e4SLinus Torvalds 	if (unlikely(q > end || q < p)) {
2351da177e4SLinus Torvalds 		p = ERR_PTR(-EFAULT);
2361da177e4SLinus Torvalds 		goto err;
2371da177e4SLinus Torvalds 	}
238400f26b5SSimo Sorce 	ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx, NULL, GFP_NOFS);
2391da177e4SLinus Torvalds 	if (ret < 0) {
2400c77668dSChuck Lever 		trace_rpcgss_import_ctx(ret);
2411da177e4SLinus Torvalds 		p = ERR_PTR(ret);
2421da177e4SLinus Torvalds 		goto err;
2431da177e4SLinus Torvalds 	}
2442004c726SJeff Layton 
2452004c726SJeff Layton 	/* is there any trailing data? */
2462004c726SJeff Layton 	if (q == end) {
2472004c726SJeff Layton 		p = q;
2482004c726SJeff Layton 		goto done;
2492004c726SJeff Layton 	}
2502004c726SJeff Layton 
2512004c726SJeff Layton 	/* pull in acceptor name (if there is one) */
2522004c726SJeff Layton 	p = simple_get_netobj(q, end, &ctx->gc_acceptor);
2532004c726SJeff Layton 	if (IS_ERR(p))
2542004c726SJeff Layton 		goto err;
2552004c726SJeff Layton done:
2560c77668dSChuck Lever 	trace_rpcgss_context(ctx->gc_expiry, now, timeout,
2570c77668dSChuck Lever 			     ctx->gc_acceptor.len, ctx->gc_acceptor.data);
2581da177e4SLinus Torvalds err:
2591da177e4SLinus Torvalds 	return p;
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
262a1a23777SChuck Lever /* XXX: Need some documentation about why UPCALL_BUF_LEN is so small.
263a1a23777SChuck Lever  *	Is user space expecting no more than UPCALL_BUF_LEN bytes?
264a1a23777SChuck Lever  *	Note that there are now _two_ NI_MAXHOST sized data items
265a1a23777SChuck Lever  *	being passed in this string.
266a1a23777SChuck Lever  */
267a1a23777SChuck Lever #define UPCALL_BUF_LEN	256
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds struct gss_upcall_msg {
2707ff13969SReshetova, Elena 	refcount_t count;
2717eaf040bSEric W. Biederman 	kuid_t	uid;
272ac83228aSTrond Myklebust 	const char *service_name;
2731da177e4SLinus Torvalds 	struct rpc_pipe_msg msg;
2741da177e4SLinus Torvalds 	struct list_head list;
2751da177e4SLinus Torvalds 	struct gss_auth *auth;
2769beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
2771da177e4SLinus Torvalds 	struct rpc_wait_queue rpc_waitqueue;
2781da177e4SLinus Torvalds 	wait_queue_head_t waitqueue;
2791da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
28034769fc4S\"J. Bruce Fields\ 	char databuf[UPCALL_BUF_LEN];
2811da177e4SLinus Torvalds };
2821da177e4SLinus Torvalds 
2832aed8b47STrond Myklebust static int get_pipe_version(struct net *net)
28479a3f20bS\"J. Bruce Fields\ {
2852aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
28679a3f20bS\"J. Bruce Fields\ 	int ret;
28779a3f20bS\"J. Bruce Fields\ 
28879a3f20bS\"J. Bruce Fields\ 	spin_lock(&pipe_version_lock);
2892aed8b47STrond Myklebust 	if (sn->pipe_version >= 0) {
2902aed8b47STrond Myklebust 		atomic_inc(&sn->pipe_users);
2912aed8b47STrond Myklebust 		ret = sn->pipe_version;
29279a3f20bS\"J. Bruce Fields\ 	} else
29379a3f20bS\"J. Bruce Fields\ 		ret = -EAGAIN;
29479a3f20bS\"J. Bruce Fields\ 	spin_unlock(&pipe_version_lock);
29579a3f20bS\"J. Bruce Fields\ 	return ret;
29679a3f20bS\"J. Bruce Fields\ }
29779a3f20bS\"J. Bruce Fields\ 
2982aed8b47STrond Myklebust static void put_pipe_version(struct net *net)
29979a3f20bS\"J. Bruce Fields\ {
3002aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
3012aed8b47STrond Myklebust 
3022aed8b47STrond Myklebust 	if (atomic_dec_and_lock(&sn->pipe_users, &pipe_version_lock)) {
3032aed8b47STrond Myklebust 		sn->pipe_version = -1;
30479a3f20bS\"J. Bruce Fields\ 		spin_unlock(&pipe_version_lock);
30579a3f20bS\"J. Bruce Fields\ 	}
30679a3f20bS\"J. Bruce Fields\ }
30779a3f20bS\"J. Bruce Fields\ 
3081da177e4SLinus Torvalds static void
3091da177e4SLinus Torvalds gss_release_msg(struct gss_upcall_msg *gss_msg)
3101da177e4SLinus Torvalds {
311e726340aSTrond Myklebust 	struct net *net = gss_msg->auth->net;
3127ff13969SReshetova, Elena 	if (!refcount_dec_and_test(&gss_msg->count))
3131da177e4SLinus Torvalds 		return;
3142aed8b47STrond Myklebust 	put_pipe_version(net);
3151da177e4SLinus Torvalds 	BUG_ON(!list_empty(&gss_msg->list));
3161da177e4SLinus Torvalds 	if (gss_msg->ctx != NULL)
3171da177e4SLinus Torvalds 		gss_put_ctx(gss_msg->ctx);
318f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue);
3199eb2ddb4STrond Myklebust 	gss_put_auth(gss_msg->auth);
320ac83228aSTrond Myklebust 	kfree_const(gss_msg->service_name);
3211da177e4SLinus Torvalds 	kfree(gss_msg);
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds static struct gss_upcall_msg *
3259130b8dbSOlga Kornievskaia __gss_find_upcall(struct rpc_pipe *pipe, kuid_t uid, const struct gss_auth *auth)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	struct gss_upcall_msg *pos;
3289beae467SStanislav Kinsbursky 	list_for_each_entry(pos, &pipe->in_downcall, list) {
3290b4d51b0SEric W. Biederman 		if (!uid_eq(pos->uid, uid))
3301da177e4SLinus Torvalds 			continue;
3319130b8dbSOlga Kornievskaia 		if (auth && pos->auth->service != auth->service)
3329130b8dbSOlga Kornievskaia 			continue;
3337ff13969SReshetova, Elena 		refcount_inc(&pos->count);
3341da177e4SLinus Torvalds 		return pos;
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 	return NULL;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
339720b8f2dS\\\"J. Bruce Fields\\\ /* Try to add an upcall to the pipefs queue.
3401da177e4SLinus Torvalds  * If an upcall owned by our uid already exists, then we return a reference
3411da177e4SLinus Torvalds  * to that upcall instead of adding the new upcall.
3421da177e4SLinus Torvalds  */
3431da177e4SLinus Torvalds static inline struct gss_upcall_msg *
344053e324fSSuresh Jayaraman gss_add_msg(struct gss_upcall_msg *gss_msg)
3451da177e4SLinus Torvalds {
3469beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
3471da177e4SLinus Torvalds 	struct gss_upcall_msg *old;
3481da177e4SLinus Torvalds 
3499beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
3509130b8dbSOlga Kornievskaia 	old = __gss_find_upcall(pipe, gss_msg->uid, gss_msg->auth);
3511da177e4SLinus Torvalds 	if (old == NULL) {
3527ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
3539beae467SStanislav Kinsbursky 		list_add(&gss_msg->list, &pipe->in_downcall);
3541da177e4SLinus Torvalds 	} else
3551da177e4SLinus Torvalds 		gss_msg = old;
3569beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
3571da177e4SLinus Torvalds 	return gss_msg;
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds static void
3611da177e4SLinus Torvalds __gss_unhash_msg(struct gss_upcall_msg *gss_msg)
3621da177e4SLinus Torvalds {
3631da177e4SLinus Torvalds 	list_del_init(&gss_msg->list);
3641da177e4SLinus Torvalds 	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
3651da177e4SLinus Torvalds 	wake_up_all(&gss_msg->waitqueue);
3667ff13969SReshetova, Elena 	refcount_dec(&gss_msg->count);
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds static void
3701da177e4SLinus Torvalds gss_unhash_msg(struct gss_upcall_msg *gss_msg)
3711da177e4SLinus Torvalds {
3729beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
3731da177e4SLinus Torvalds 
3743b68aaeaSTrond Myklebust 	if (list_empty(&gss_msg->list))
3753b68aaeaSTrond Myklebust 		return;
3769beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
3773b68aaeaSTrond Myklebust 	if (!list_empty(&gss_msg->list))
3781da177e4SLinus Torvalds 		__gss_unhash_msg(gss_msg);
3799beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds static void
383126e216aSTrond Myklebust gss_handle_downcall_result(struct gss_cred *gss_cred, struct gss_upcall_msg *gss_msg)
384126e216aSTrond Myklebust {
385126e216aSTrond Myklebust 	switch (gss_msg->msg.errno) {
386126e216aSTrond Myklebust 	case 0:
387126e216aSTrond Myklebust 		if (gss_msg->ctx == NULL)
388126e216aSTrond Myklebust 			break;
389126e216aSTrond Myklebust 		clear_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
390126e216aSTrond Myklebust 		gss_cred_set_ctx(&gss_cred->gc_base, gss_msg->ctx);
391126e216aSTrond Myklebust 		break;
392126e216aSTrond Myklebust 	case -EKEYEXPIRED:
393126e216aSTrond Myklebust 		set_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
394126e216aSTrond Myklebust 	}
395126e216aSTrond Myklebust 	gss_cred->gc_upcall_timestamp = jiffies;
396126e216aSTrond Myklebust 	gss_cred->gc_upcall = NULL;
397126e216aSTrond Myklebust 	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
398126e216aSTrond Myklebust }
399126e216aSTrond Myklebust 
400126e216aSTrond Myklebust static void
4011da177e4SLinus Torvalds gss_upcall_callback(struct rpc_task *task)
4021da177e4SLinus Torvalds {
403a17c2153STrond Myklebust 	struct gss_cred *gss_cred = container_of(task->tk_rqstp->rq_cred,
4041da177e4SLinus Torvalds 			struct gss_cred, gc_base);
4051da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
4069beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
4071da177e4SLinus Torvalds 
4089beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
409126e216aSTrond Myklebust 	gss_handle_downcall_result(gss_cred, gss_msg);
4109beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
411126e216aSTrond Myklebust 	task->tk_status = gss_msg->msg.errno;
4121da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
415ac83228aSTrond Myklebust static void gss_encode_v0_msg(struct gss_upcall_msg *gss_msg,
416ac83228aSTrond Myklebust 			      const struct cred *cred)
41734769fc4S\"J. Bruce Fields\ {
418ac83228aSTrond Myklebust 	struct user_namespace *userns = cred->user_ns;
419283ebe3eSTrond Myklebust 
420283ebe3eSTrond Myklebust 	uid_t uid = from_kuid_munged(userns, gss_msg->uid);
42190602c7bSEric W. Biederman 	memcpy(gss_msg->databuf, &uid, sizeof(uid));
42290602c7bSEric W. Biederman 	gss_msg->msg.data = gss_msg->databuf;
42390602c7bSEric W. Biederman 	gss_msg->msg.len = sizeof(uid);
4249d3a2260STrond Myklebust 
4259d3a2260STrond Myklebust 	BUILD_BUG_ON(sizeof(uid) > sizeof(gss_msg->databuf));
42634769fc4S\"J. Bruce Fields\ }
42734769fc4S\"J. Bruce Fields\ 
428ac83228aSTrond Myklebust static ssize_t
429ac83228aSTrond Myklebust gss_v0_upcall(struct file *file, struct rpc_pipe_msg *msg,
430ac83228aSTrond Myklebust 		char __user *buf, size_t buflen)
431ac83228aSTrond Myklebust {
432ac83228aSTrond Myklebust 	struct gss_upcall_msg *gss_msg = container_of(msg,
433ac83228aSTrond Myklebust 						      struct gss_upcall_msg,
434ac83228aSTrond Myklebust 						      msg);
435ac83228aSTrond Myklebust 	if (msg->copied == 0)
436ac83228aSTrond Myklebust 		gss_encode_v0_msg(gss_msg, file->f_cred);
437ac83228aSTrond Myklebust 	return rpc_pipe_generic_upcall(file, msg, buf, buflen);
438ac83228aSTrond Myklebust }
439ac83228aSTrond Myklebust 
4409d3a2260STrond Myklebust static int gss_encode_v1_msg(struct gss_upcall_msg *gss_msg,
441bd4a3eb1STrond Myklebust 				const char *service_name,
442ac83228aSTrond Myklebust 				const char *target_name,
443ac83228aSTrond Myklebust 				const struct cred *cred)
44434769fc4S\"J. Bruce Fields\ {
445ac83228aSTrond Myklebust 	struct user_namespace *userns = cred->user_ns;
446683ac665STrond Myklebust 	struct gss_api_mech *mech = gss_msg->auth->mech;
4478b1c7bf5SOlga Kornievskaia 	char *p = gss_msg->databuf;
4489d3a2260STrond Myklebust 	size_t buflen = sizeof(gss_msg->databuf);
4499d3a2260STrond Myklebust 	int len;
4508b1c7bf5SOlga Kornievskaia 
4519d3a2260STrond Myklebust 	len = scnprintf(p, buflen, "mech=%s uid=%d", mech->gm_name,
452283ebe3eSTrond Myklebust 			from_kuid_munged(userns, gss_msg->uid));
4539d3a2260STrond Myklebust 	buflen -= len;
4549d3a2260STrond Myklebust 	p += len;
4559d3a2260STrond Myklebust 	gss_msg->msg.len = len;
456108b833cSChuck Lever 
457108b833cSChuck Lever 	/*
458108b833cSChuck Lever 	 * target= is a full service principal that names the remote
459108b833cSChuck Lever 	 * identity that we are authenticating to.
460108b833cSChuck Lever 	 */
461bd4a3eb1STrond Myklebust 	if (target_name) {
4629d3a2260STrond Myklebust 		len = scnprintf(p, buflen, " target=%s", target_name);
4639d3a2260STrond Myklebust 		buflen -= len;
4648b1c7bf5SOlga Kornievskaia 		p += len;
4658b1c7bf5SOlga Kornievskaia 		gss_msg->msg.len += len;
4668b1c7bf5SOlga Kornievskaia 	}
467108b833cSChuck Lever 
468108b833cSChuck Lever 	/*
469108b833cSChuck Lever 	 * gssd uses service= and srchost= to select a matching key from
470108b833cSChuck Lever 	 * the system's keytab to use as the source principal.
471108b833cSChuck Lever 	 *
472108b833cSChuck Lever 	 * service= is the service name part of the source principal,
473108b833cSChuck Lever 	 * or "*" (meaning choose any).
474108b833cSChuck Lever 	 *
475108b833cSChuck Lever 	 * srchost= is the hostname part of the source principal. When
476108b833cSChuck Lever 	 * not provided, gssd uses the local hostname.
477108b833cSChuck Lever 	 */
478a1a23777SChuck Lever 	if (service_name) {
479a1a23777SChuck Lever 		char *c = strchr(service_name, '@');
480a1a23777SChuck Lever 
481a1a23777SChuck Lever 		if (!c)
482a1a23777SChuck Lever 			len = scnprintf(p, buflen, " service=%s",
483a1a23777SChuck Lever 					service_name);
484a1a23777SChuck Lever 		else
485a1a23777SChuck Lever 			len = scnprintf(p, buflen,
486a1a23777SChuck Lever 					" service=%.*s srchost=%s",
487a1a23777SChuck Lever 					(int)(c - service_name),
488a1a23777SChuck Lever 					service_name, c + 1);
4899d3a2260STrond Myklebust 		buflen -= len;
4902efef708SOlga Kornievskaia 		p += len;
4912efef708SOlga Kornievskaia 		gss_msg->msg.len += len;
4922efef708SOlga Kornievskaia 	}
493108b833cSChuck Lever 
494683ac665STrond Myklebust 	if (mech->gm_upcall_enctypes) {
4959d3a2260STrond Myklebust 		len = scnprintf(p, buflen, " enctypes=%s",
4969d3a2260STrond Myklebust 				mech->gm_upcall_enctypes);
4979d3a2260STrond Myklebust 		buflen -= len;
498683ac665STrond Myklebust 		p += len;
499683ac665STrond Myklebust 		gss_msg->msg.len += len;
500683ac665STrond Myklebust 	}
5010c77668dSChuck Lever 	trace_rpcgss_upcall_msg(gss_msg->databuf);
5029d3a2260STrond Myklebust 	len = scnprintf(p, buflen, "\n");
5039d3a2260STrond Myklebust 	if (len == 0)
5049d3a2260STrond Myklebust 		goto out_overflow;
5058b1c7bf5SOlga Kornievskaia 	gss_msg->msg.len += len;
50634769fc4S\"J. Bruce Fields\ 	gss_msg->msg.data = gss_msg->databuf;
5079d3a2260STrond Myklebust 	return 0;
5089d3a2260STrond Myklebust out_overflow:
5099d3a2260STrond Myklebust 	WARN_ON_ONCE(1);
5109d3a2260STrond Myklebust 	return -ENOMEM;
51134769fc4S\"J. Bruce Fields\ }
51234769fc4S\"J. Bruce Fields\ 
513ac83228aSTrond Myklebust static ssize_t
514ac83228aSTrond Myklebust gss_v1_upcall(struct file *file, struct rpc_pipe_msg *msg,
515ac83228aSTrond Myklebust 		char __user *buf, size_t buflen)
516ac83228aSTrond Myklebust {
517ac83228aSTrond Myklebust 	struct gss_upcall_msg *gss_msg = container_of(msg,
518ac83228aSTrond Myklebust 						      struct gss_upcall_msg,
519ac83228aSTrond Myklebust 						      msg);
520ac83228aSTrond Myklebust 	int err;
521ac83228aSTrond Myklebust 	if (msg->copied == 0) {
522ac83228aSTrond Myklebust 		err = gss_encode_v1_msg(gss_msg,
523ac83228aSTrond Myklebust 					gss_msg->service_name,
524ac83228aSTrond Myklebust 					gss_msg->auth->target_name,
525ac83228aSTrond Myklebust 					file->f_cred);
526ac83228aSTrond Myklebust 		if (err)
527ac83228aSTrond Myklebust 			return err;
528ac83228aSTrond Myklebust 	}
529ac83228aSTrond Myklebust 	return rpc_pipe_generic_upcall(file, msg, buf, buflen);
530ac83228aSTrond Myklebust }
531ac83228aSTrond Myklebust 
53268c97153STrond Myklebust static struct gss_upcall_msg *
533e726340aSTrond Myklebust gss_alloc_msg(struct gss_auth *gss_auth,
5347eaf040bSEric W. Biederman 		kuid_t uid, const char *service_name)
5351da177e4SLinus Torvalds {
5361da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
53779a3f20bS\"J. Bruce Fields\ 	int vers;
5389d3a2260STrond Myklebust 	int err = -ENOMEM;
5391da177e4SLinus Torvalds 
5400f38b873STrond Myklebust 	gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
541db75b3d6S\"J. Bruce Fields\ 	if (gss_msg == NULL)
5429d3a2260STrond Myklebust 		goto err;
543e726340aSTrond Myklebust 	vers = get_pipe_version(gss_auth->net);
5449d3a2260STrond Myklebust 	err = vers;
5459d3a2260STrond Myklebust 	if (err < 0)
5469d3a2260STrond Myklebust 		goto err_free_msg;
54719172284STrond Myklebust 	gss_msg->pipe = gss_auth->gss_pipe[vers]->pipe;
5481da177e4SLinus Torvalds 	INIT_LIST_HEAD(&gss_msg->list);
5491da177e4SLinus Torvalds 	rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
5501da177e4SLinus Torvalds 	init_waitqueue_head(&gss_msg->waitqueue);
5517ff13969SReshetova, Elena 	refcount_set(&gss_msg->count, 1);
5521da177e4SLinus Torvalds 	gss_msg->uid = uid;
5531da177e4SLinus Torvalds 	gss_msg->auth = gss_auth;
554ac83228aSTrond Myklebust 	if (service_name) {
555ac83228aSTrond Myklebust 		gss_msg->service_name = kstrdup_const(service_name, GFP_NOFS);
556ac83228aSTrond Myklebust 		if (!gss_msg->service_name)
557e9776d0fSTrond Myklebust 			goto err_put_pipe_version;
55807d53ae4Szhong jiang 	}
5591da177e4SLinus Torvalds 	return gss_msg;
560e9776d0fSTrond Myklebust err_put_pipe_version:
561e9776d0fSTrond Myklebust 	put_pipe_version(gss_auth->net);
5629d3a2260STrond Myklebust err_free_msg:
5639d3a2260STrond Myklebust 	kfree(gss_msg);
5649d3a2260STrond Myklebust err:
5659d3a2260STrond Myklebust 	return ERR_PTR(err);
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds static struct gss_upcall_msg *
569e726340aSTrond Myklebust gss_setup_upcall(struct gss_auth *gss_auth, struct rpc_cred *cred)
5701da177e4SLinus Torvalds {
5717c67db3aSTrond Myklebust 	struct gss_cred *gss_cred = container_of(cred,
5727c67db3aSTrond Myklebust 			struct gss_cred, gc_base);
5731da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_new, *gss_msg;
57404d1532bSNeilBrown 	kuid_t uid = cred->cr_cred->fsuid;
5751da177e4SLinus Torvalds 
576e726340aSTrond Myklebust 	gss_new = gss_alloc_msg(gss_auth, uid, gss_cred->gc_principal);
577db75b3d6S\"J. Bruce Fields\ 	if (IS_ERR(gss_new))
578db75b3d6S\"J. Bruce Fields\ 		return gss_new;
579053e324fSSuresh Jayaraman 	gss_msg = gss_add_msg(gss_new);
5801da177e4SLinus Torvalds 	if (gss_msg == gss_new) {
5811cded9d2SNeilBrown 		int res;
5827ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
5831cded9d2SNeilBrown 		res = rpc_queue_upcall(gss_new->pipe, &gss_new->msg);
5841da177e4SLinus Torvalds 		if (res) {
5851da177e4SLinus Torvalds 			gss_unhash_msg(gss_new);
5867ff13969SReshetova, Elena 			refcount_dec(&gss_msg->count);
5871cded9d2SNeilBrown 			gss_release_msg(gss_new);
5881da177e4SLinus Torvalds 			gss_msg = ERR_PTR(res);
5891da177e4SLinus Torvalds 		}
5901da177e4SLinus Torvalds 	} else
5911da177e4SLinus Torvalds 		gss_release_msg(gss_new);
5921da177e4SLinus Torvalds 	return gss_msg;
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
595b03568c3S\"J. Bruce Fields\ static void warn_gssd(void)
596b03568c3S\"J. Bruce Fields\ {
5970ea9de0eSJeff Layton 	dprintk("AUTH_GSS upcall failed. Please check user daemon is running.\n");
598b03568c3S\"J. Bruce Fields\ }
599b03568c3S\"J. Bruce Fields\ 
6001da177e4SLinus Torvalds static inline int
6011da177e4SLinus Torvalds gss_refresh_upcall(struct rpc_task *task)
6021da177e4SLinus Torvalds {
603a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
6044a8c1344STrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth,
6051da177e4SLinus Torvalds 			struct gss_auth, rpc_auth);
6061da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred,
6071da177e4SLinus Torvalds 			struct gss_cred, gc_base);
6081da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
6099beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
6101da177e4SLinus Torvalds 	int err = 0;
6111da177e4SLinus Torvalds 
612e726340aSTrond Myklebust 	gss_msg = gss_setup_upcall(gss_auth, cred);
613480e3243SRoel Kluin 	if (PTR_ERR(gss_msg) == -EAGAIN) {
61479a3f20bS\"J. Bruce Fields\ 		/* XXX: warning on the first, under the assumption we
61579a3f20bS\"J. Bruce Fields\ 		 * shouldn't normally hit this case on a refresh. */
61679a3f20bS\"J. Bruce Fields\ 		warn_gssd();
6176b2e6856STrond Myklebust 		rpc_sleep_on_timeout(&pipe_version_rpc_waitqueue,
6186b2e6856STrond Myklebust 				task, NULL, jiffies + (15 * HZ));
6190c77668dSChuck Lever 		err = -EAGAIN;
6200c77668dSChuck Lever 		goto out;
62179a3f20bS\"J. Bruce Fields\ 	}
6221da177e4SLinus Torvalds 	if (IS_ERR(gss_msg)) {
6231da177e4SLinus Torvalds 		err = PTR_ERR(gss_msg);
6241da177e4SLinus Torvalds 		goto out;
6251da177e4SLinus Torvalds 	}
6269beae467SStanislav Kinsbursky 	pipe = gss_msg->pipe;
6279beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
6281da177e4SLinus Torvalds 	if (gss_cred->gc_upcall != NULL)
6295d00837bSTrond Myklebust 		rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL);
630126e216aSTrond Myklebust 	else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
6311da177e4SLinus Torvalds 		gss_cred->gc_upcall = gss_msg;
6321da177e4SLinus Torvalds 		/* gss_upcall_callback will release the reference to gss_upcall_msg */
6337ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
6345d00837bSTrond Myklebust 		rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback);
635126e216aSTrond Myklebust 	} else {
636126e216aSTrond Myklebust 		gss_handle_downcall_result(gss_cred, gss_msg);
6371da177e4SLinus Torvalds 		err = gss_msg->msg.errno;
638126e216aSTrond Myklebust 	}
6399beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
6401da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
6411da177e4SLinus Torvalds out:
6420c77668dSChuck Lever 	trace_rpcgss_upcall_result(from_kuid(&init_user_ns,
6430c77668dSChuck Lever 					     cred->cr_cred->fsuid), err);
6441da177e4SLinus Torvalds 	return err;
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds static inline int
6481da177e4SLinus Torvalds gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
6491da177e4SLinus Torvalds {
650e726340aSTrond Myklebust 	struct net *net = gss_auth->net;
651abfdbd53STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
6529beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
6531da177e4SLinus Torvalds 	struct rpc_cred *cred = &gss_cred->gc_base;
6541da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
6551da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
656d36ccb9cSTrond Myklebust 	int err;
6571da177e4SLinus Torvalds 
65879a3f20bS\"J. Bruce Fields\ retry:
659d36ccb9cSTrond Myklebust 	err = 0;
66089f84243SJeff Layton 	/* if gssd is down, just skip upcalling altogether */
66189f84243SJeff Layton 	if (!gssd_running(net)) {
66289f84243SJeff Layton 		warn_gssd();
6630c77668dSChuck Lever 		err = -EACCES;
6640c77668dSChuck Lever 		goto out;
66589f84243SJeff Layton 	}
666e726340aSTrond Myklebust 	gss_msg = gss_setup_upcall(gss_auth, cred);
66779a3f20bS\"J. Bruce Fields\ 	if (PTR_ERR(gss_msg) == -EAGAIN) {
66879a3f20bS\"J. Bruce Fields\ 		err = wait_event_interruptible_timeout(pipe_version_waitqueue,
66989f84243SJeff Layton 				sn->pipe_version >= 0, 15 * HZ);
6702aed8b47STrond Myklebust 		if (sn->pipe_version < 0) {
671d1a8016aSBryan Schumaker 			warn_gssd();
672d1a8016aSBryan Schumaker 			err = -EACCES;
673d1a8016aSBryan Schumaker 		}
674d36ccb9cSTrond Myklebust 		if (err < 0)
67579a3f20bS\"J. Bruce Fields\ 			goto out;
67679a3f20bS\"J. Bruce Fields\ 		goto retry;
67779a3f20bS\"J. Bruce Fields\ 	}
6781da177e4SLinus Torvalds 	if (IS_ERR(gss_msg)) {
6791da177e4SLinus Torvalds 		err = PTR_ERR(gss_msg);
6801da177e4SLinus Torvalds 		goto out;
6811da177e4SLinus Torvalds 	}
6829beae467SStanislav Kinsbursky 	pipe = gss_msg->pipe;
6831da177e4SLinus Torvalds 	for (;;) {
6845afa9133STrond Myklebust 		prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_KILLABLE);
6859beae467SStanislav Kinsbursky 		spin_lock(&pipe->lock);
6861da177e4SLinus Torvalds 		if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
6871da177e4SLinus Torvalds 			break;
6881da177e4SLinus Torvalds 		}
6899beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
6905afa9133STrond Myklebust 		if (fatal_signal_pending(current)) {
6911da177e4SLinus Torvalds 			err = -ERESTARTSYS;
6921da177e4SLinus Torvalds 			goto out_intr;
6931da177e4SLinus Torvalds 		}
6941da177e4SLinus Torvalds 		schedule();
6951da177e4SLinus Torvalds 	}
6961da177e4SLinus Torvalds 	if (gss_msg->ctx)
6977b6962b0STrond Myklebust 		gss_cred_set_ctx(cred, gss_msg->ctx);
6981da177e4SLinus Torvalds 	else
6991da177e4SLinus Torvalds 		err = gss_msg->msg.errno;
7009beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
7011da177e4SLinus Torvalds out_intr:
7021da177e4SLinus Torvalds 	finish_wait(&gss_msg->waitqueue, &wait);
7031da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
7041da177e4SLinus Torvalds out:
7050c77668dSChuck Lever 	trace_rpcgss_upcall_result(from_kuid(&init_user_ns,
7060c77668dSChuck Lever 					     cred->cr_cred->fsuid), err);
7071da177e4SLinus Torvalds 	return err;
7081da177e4SLinus Torvalds }
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds #define MSG_BUF_MAXSIZE 1024
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds static ssize_t
7131da177e4SLinus Torvalds gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	const void *p, *end;
7161da177e4SLinus Torvalds 	void *buf;
7171da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
718496ad9aaSAl Viro 	struct rpc_pipe *pipe = RPC_I(file_inode(filp))->pipe;
7191da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
72090602c7bSEric W. Biederman 	uid_t id;
72190602c7bSEric W. Biederman 	kuid_t uid;
7223b68aaeaSTrond Myklebust 	ssize_t err = -EFBIG;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	if (mlen > MSG_BUF_MAXSIZE)
7251da177e4SLinus Torvalds 		goto out;
7261da177e4SLinus Torvalds 	err = -ENOMEM;
7270f38b873STrond Myklebust 	buf = kmalloc(mlen, GFP_NOFS);
7281da177e4SLinus Torvalds 	if (!buf)
7291da177e4SLinus Torvalds 		goto out;
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	err = -EFAULT;
7321da177e4SLinus Torvalds 	if (copy_from_user(buf, src, mlen))
7331da177e4SLinus Torvalds 		goto err;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	end = (const void *)((char *)buf + mlen);
73690602c7bSEric W. Biederman 	p = simple_get_bytes(buf, end, &id, sizeof(id));
7371da177e4SLinus Torvalds 	if (IS_ERR(p)) {
7381da177e4SLinus Torvalds 		err = PTR_ERR(p);
7391da177e4SLinus Torvalds 		goto err;
7401da177e4SLinus Torvalds 	}
7411da177e4SLinus Torvalds 
742283ebe3eSTrond Myklebust 	uid = make_kuid(current_user_ns(), id);
74390602c7bSEric W. Biederman 	if (!uid_valid(uid)) {
74490602c7bSEric W. Biederman 		err = -EINVAL;
74590602c7bSEric W. Biederman 		goto err;
74690602c7bSEric W. Biederman 	}
74790602c7bSEric W. Biederman 
7481da177e4SLinus Torvalds 	err = -ENOMEM;
7491da177e4SLinus Torvalds 	ctx = gss_alloc_context();
7501da177e4SLinus Torvalds 	if (ctx == NULL)
7511da177e4SLinus Torvalds 		goto err;
7523b68aaeaSTrond Myklebust 
7533b68aaeaSTrond Myklebust 	err = -ENOENT;
7543b68aaeaSTrond Myklebust 	/* Find a matching upcall */
7559beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
7569130b8dbSOlga Kornievskaia 	gss_msg = __gss_find_upcall(pipe, uid, NULL);
7573b68aaeaSTrond Myklebust 	if (gss_msg == NULL) {
7589beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
7593b68aaeaSTrond Myklebust 		goto err_put_ctx;
7603b68aaeaSTrond Myklebust 	}
7613b68aaeaSTrond Myklebust 	list_del_init(&gss_msg->list);
7629beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
7633b68aaeaSTrond Myklebust 
7646e84c7b6STrond Myklebust 	p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
7651da177e4SLinus Torvalds 	if (IS_ERR(p)) {
7661da177e4SLinus Torvalds 		err = PTR_ERR(p);
767486bad2eSJeff Layton 		switch (err) {
768486bad2eSJeff Layton 		case -EACCES:
769dc5ddce9SJeff Layton 		case -EKEYEXPIRED:
770486bad2eSJeff Layton 			gss_msg->msg.errno = err;
771486bad2eSJeff Layton 			err = mlen;
772486bad2eSJeff Layton 			break;
773486bad2eSJeff Layton 		case -EFAULT:
774486bad2eSJeff Layton 		case -ENOMEM:
775486bad2eSJeff Layton 		case -EINVAL:
776486bad2eSJeff Layton 		case -ENOSYS:
777486bad2eSJeff Layton 			gss_msg->msg.errno = -EAGAIN;
778486bad2eSJeff Layton 			break;
779486bad2eSJeff Layton 		default:
780486bad2eSJeff Layton 			printk(KERN_CRIT "%s: bad return from "
7816c853099SRandy Dunlap 				"gss_fill_context: %zd\n", __func__, err);
782437b300cSScott Mayhew 			gss_msg->msg.errno = -EIO;
783486bad2eSJeff Layton 		}
7843b68aaeaSTrond Myklebust 		goto err_release_msg;
7851da177e4SLinus Torvalds 	}
7861da177e4SLinus Torvalds 	gss_msg->ctx = gss_get_ctx(ctx);
7873b68aaeaSTrond Myklebust 	err = mlen;
7883b68aaeaSTrond Myklebust 
7893b68aaeaSTrond Myklebust err_release_msg:
7909beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
7911da177e4SLinus Torvalds 	__gss_unhash_msg(gss_msg);
7929beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
7931da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
7941da177e4SLinus Torvalds err_put_ctx:
7951da177e4SLinus Torvalds 	gss_put_ctx(ctx);
7961da177e4SLinus Torvalds err:
7971da177e4SLinus Torvalds 	kfree(buf);
7981da177e4SLinus Torvalds out:
7991da177e4SLinus Torvalds 	return err;
8001da177e4SLinus Torvalds }
8011da177e4SLinus Torvalds 
80234769fc4S\"J. Bruce Fields\ static int gss_pipe_open(struct inode *inode, int new_version)
803cf81939dS\"J. Bruce Fields\ {
8042aed8b47STrond Myklebust 	struct net *net = inode->i_sb->s_fs_info;
8052aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
80634769fc4S\"J. Bruce Fields\ 	int ret = 0;
80734769fc4S\"J. Bruce Fields\ 
80879a3f20bS\"J. Bruce Fields\ 	spin_lock(&pipe_version_lock);
8092aed8b47STrond Myklebust 	if (sn->pipe_version < 0) {
81034769fc4S\"J. Bruce Fields\ 		/* First open of any gss pipe determines the version: */
8112aed8b47STrond Myklebust 		sn->pipe_version = new_version;
81279a3f20bS\"J. Bruce Fields\ 		rpc_wake_up(&pipe_version_rpc_waitqueue);
81379a3f20bS\"J. Bruce Fields\ 		wake_up(&pipe_version_waitqueue);
8142aed8b47STrond Myklebust 	} else if (sn->pipe_version != new_version) {
81534769fc4S\"J. Bruce Fields\ 		/* Trying to open a pipe of a different version */
81634769fc4S\"J. Bruce Fields\ 		ret = -EBUSY;
81734769fc4S\"J. Bruce Fields\ 		goto out;
81879a3f20bS\"J. Bruce Fields\ 	}
8192aed8b47STrond Myklebust 	atomic_inc(&sn->pipe_users);
82034769fc4S\"J. Bruce Fields\ out:
82179a3f20bS\"J. Bruce Fields\ 	spin_unlock(&pipe_version_lock);
82234769fc4S\"J. Bruce Fields\ 	return ret;
82334769fc4S\"J. Bruce Fields\ 
82434769fc4S\"J. Bruce Fields\ }
82534769fc4S\"J. Bruce Fields\ 
82634769fc4S\"J. Bruce Fields\ static int gss_pipe_open_v0(struct inode *inode)
82734769fc4S\"J. Bruce Fields\ {
82834769fc4S\"J. Bruce Fields\ 	return gss_pipe_open(inode, 0);
82934769fc4S\"J. Bruce Fields\ }
83034769fc4S\"J. Bruce Fields\ 
83134769fc4S\"J. Bruce Fields\ static int gss_pipe_open_v1(struct inode *inode)
83234769fc4S\"J. Bruce Fields\ {
83334769fc4S\"J. Bruce Fields\ 	return gss_pipe_open(inode, 1);
834cf81939dS\"J. Bruce Fields\ }
835cf81939dS\"J. Bruce Fields\ 
8361da177e4SLinus Torvalds static void
8371da177e4SLinus Torvalds gss_pipe_release(struct inode *inode)
8381da177e4SLinus Torvalds {
8392aed8b47STrond Myklebust 	struct net *net = inode->i_sb->s_fs_info;
8409beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
8411da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
8421da177e4SLinus Torvalds 
8435a67657aSTrond Myklebust restart:
8449beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
8459beae467SStanislav Kinsbursky 	list_for_each_entry(gss_msg, &pipe->in_downcall, list) {
8466e84c7b6STrond Myklebust 
8475a67657aSTrond Myklebust 		if (!list_empty(&gss_msg->msg.list))
8485a67657aSTrond Myklebust 			continue;
8491da177e4SLinus Torvalds 		gss_msg->msg.errno = -EPIPE;
8507ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
8511da177e4SLinus Torvalds 		__gss_unhash_msg(gss_msg);
8529beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
8531da177e4SLinus Torvalds 		gss_release_msg(gss_msg);
8545a67657aSTrond Myklebust 		goto restart;
8551da177e4SLinus Torvalds 	}
8569beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
857cf81939dS\"J. Bruce Fields\ 
8582aed8b47STrond Myklebust 	put_pipe_version(net);
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds static void
8621da177e4SLinus Torvalds gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
8631da177e4SLinus Torvalds {
8641da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	if (msg->errno < 0) {
8677ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
8681da177e4SLinus Torvalds 		gss_unhash_msg(gss_msg);
869b03568c3S\"J. Bruce Fields\ 		if (msg->errno == -ETIMEDOUT)
870b03568c3S\"J. Bruce Fields\ 			warn_gssd();
8711da177e4SLinus Torvalds 		gss_release_msg(gss_msg);
8721da177e4SLinus Torvalds 	}
8731cded9d2SNeilBrown 	gss_release_msg(gss_msg);
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
87619172284STrond Myklebust static void gss_pipe_dentry_destroy(struct dentry *dir,
87719172284STrond Myklebust 		struct rpc_pipe_dir_object *pdo)
878ccdc28f8SStanislav Kinsbursky {
87919172284STrond Myklebust 	struct gss_pipe *gss_pipe = pdo->pdo_data;
88019172284STrond Myklebust 	struct rpc_pipe *pipe = gss_pipe->pipe;
881ccdc28f8SStanislav Kinsbursky 
88219172284STrond Myklebust 	if (pipe->dentry != NULL) {
88319172284STrond Myklebust 		rpc_unlink(pipe->dentry);
88419172284STrond Myklebust 		pipe->dentry = NULL;
8856b2fddd3STrond Myklebust 	}
886ccdc28f8SStanislav Kinsbursky }
887ccdc28f8SStanislav Kinsbursky 
88819172284STrond Myklebust static int gss_pipe_dentry_create(struct dentry *dir,
88919172284STrond Myklebust 		struct rpc_pipe_dir_object *pdo)
890ccdc28f8SStanislav Kinsbursky {
89119172284STrond Myklebust 	struct gss_pipe *p = pdo->pdo_data;
8926b2fddd3STrond Myklebust 	struct dentry *dentry;
893ccdc28f8SStanislav Kinsbursky 
89419172284STrond Myklebust 	dentry = rpc_mkpipe_dentry(dir, p->name, p->clnt, p->pipe);
89519172284STrond Myklebust 	if (IS_ERR(dentry))
89619172284STrond Myklebust 		return PTR_ERR(dentry);
89719172284STrond Myklebust 	p->pipe->dentry = dentry;
898ccdc28f8SStanislav Kinsbursky 	return 0;
89919172284STrond Myklebust }
900ccdc28f8SStanislav Kinsbursky 
90119172284STrond Myklebust static const struct rpc_pipe_dir_object_ops gss_pipe_dir_object_ops = {
90219172284STrond Myklebust 	.create = gss_pipe_dentry_create,
90319172284STrond Myklebust 	.destroy = gss_pipe_dentry_destroy,
90419172284STrond Myklebust };
90519172284STrond Myklebust 
90619172284STrond Myklebust static struct gss_pipe *gss_pipe_alloc(struct rpc_clnt *clnt,
90719172284STrond Myklebust 		const char *name,
90819172284STrond Myklebust 		const struct rpc_pipe_ops *upcall_ops)
90919172284STrond Myklebust {
91019172284STrond Myklebust 	struct gss_pipe *p;
91119172284STrond Myklebust 	int err = -ENOMEM;
91219172284STrond Myklebust 
91319172284STrond Myklebust 	p = kmalloc(sizeof(*p), GFP_KERNEL);
91419172284STrond Myklebust 	if (p == NULL)
91519172284STrond Myklebust 		goto err;
91619172284STrond Myklebust 	p->pipe = rpc_mkpipe_data(upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
91719172284STrond Myklebust 	if (IS_ERR(p->pipe)) {
91819172284STrond Myklebust 		err = PTR_ERR(p->pipe);
91919172284STrond Myklebust 		goto err_free_gss_pipe;
92019172284STrond Myklebust 	}
92119172284STrond Myklebust 	p->name = name;
92219172284STrond Myklebust 	p->clnt = clnt;
923414a6295STrond Myklebust 	kref_init(&p->kref);
92419172284STrond Myklebust 	rpc_init_pipe_dir_object(&p->pdo,
92519172284STrond Myklebust 			&gss_pipe_dir_object_ops,
92619172284STrond Myklebust 			p);
92719172284STrond Myklebust 	return p;
92819172284STrond Myklebust err_free_gss_pipe:
92919172284STrond Myklebust 	kfree(p);
9306b2fddd3STrond Myklebust err:
93119172284STrond Myklebust 	return ERR_PTR(err);
932ccdc28f8SStanislav Kinsbursky }
933ccdc28f8SStanislav Kinsbursky 
934414a6295STrond Myklebust struct gss_alloc_pdo {
935414a6295STrond Myklebust 	struct rpc_clnt *clnt;
936414a6295STrond Myklebust 	const char *name;
937414a6295STrond Myklebust 	const struct rpc_pipe_ops *upcall_ops;
938414a6295STrond Myklebust };
939414a6295STrond Myklebust 
940414a6295STrond Myklebust static int gss_pipe_match_pdo(struct rpc_pipe_dir_object *pdo, void *data)
941414a6295STrond Myklebust {
942414a6295STrond Myklebust 	struct gss_pipe *gss_pipe;
943414a6295STrond Myklebust 	struct gss_alloc_pdo *args = data;
944414a6295STrond Myklebust 
945414a6295STrond Myklebust 	if (pdo->pdo_ops != &gss_pipe_dir_object_ops)
946414a6295STrond Myklebust 		return 0;
947414a6295STrond Myklebust 	gss_pipe = container_of(pdo, struct gss_pipe, pdo);
948414a6295STrond Myklebust 	if (strcmp(gss_pipe->name, args->name) != 0)
949414a6295STrond Myklebust 		return 0;
950414a6295STrond Myklebust 	if (!kref_get_unless_zero(&gss_pipe->kref))
951414a6295STrond Myklebust 		return 0;
952414a6295STrond Myklebust 	return 1;
953414a6295STrond Myklebust }
954414a6295STrond Myklebust 
955414a6295STrond Myklebust static struct rpc_pipe_dir_object *gss_pipe_alloc_pdo(void *data)
956414a6295STrond Myklebust {
957414a6295STrond Myklebust 	struct gss_pipe *gss_pipe;
958414a6295STrond Myklebust 	struct gss_alloc_pdo *args = data;
959414a6295STrond Myklebust 
960414a6295STrond Myklebust 	gss_pipe = gss_pipe_alloc(args->clnt, args->name, args->upcall_ops);
961414a6295STrond Myklebust 	if (!IS_ERR(gss_pipe))
962414a6295STrond Myklebust 		return &gss_pipe->pdo;
963414a6295STrond Myklebust 	return NULL;
964414a6295STrond Myklebust }
965414a6295STrond Myklebust 
966414a6295STrond Myklebust static struct gss_pipe *gss_pipe_get(struct rpc_clnt *clnt,
967414a6295STrond Myklebust 		const char *name,
968414a6295STrond Myklebust 		const struct rpc_pipe_ops *upcall_ops)
969414a6295STrond Myklebust {
970414a6295STrond Myklebust 	struct net *net = rpc_net_ns(clnt);
971414a6295STrond Myklebust 	struct rpc_pipe_dir_object *pdo;
972414a6295STrond Myklebust 	struct gss_alloc_pdo args = {
973414a6295STrond Myklebust 		.clnt = clnt,
974414a6295STrond Myklebust 		.name = name,
975414a6295STrond Myklebust 		.upcall_ops = upcall_ops,
976414a6295STrond Myklebust 	};
977414a6295STrond Myklebust 
978414a6295STrond Myklebust 	pdo = rpc_find_or_alloc_pipe_dir_object(net,
979414a6295STrond Myklebust 			&clnt->cl_pipedir_objects,
980414a6295STrond Myklebust 			gss_pipe_match_pdo,
981414a6295STrond Myklebust 			gss_pipe_alloc_pdo,
982414a6295STrond Myklebust 			&args);
983414a6295STrond Myklebust 	if (pdo != NULL)
984414a6295STrond Myklebust 		return container_of(pdo, struct gss_pipe, pdo);
985414a6295STrond Myklebust 	return ERR_PTR(-ENOMEM);
986414a6295STrond Myklebust }
987414a6295STrond Myklebust 
98819172284STrond Myklebust static void __gss_pipe_free(struct gss_pipe *p)
989ccdc28f8SStanislav Kinsbursky {
99019172284STrond Myklebust 	struct rpc_clnt *clnt = p->clnt;
99119172284STrond Myklebust 	struct net *net = rpc_net_ns(clnt);
992ccdc28f8SStanislav Kinsbursky 
99319172284STrond Myklebust 	rpc_remove_pipe_dir_object(net,
99419172284STrond Myklebust 			&clnt->cl_pipedir_objects,
99519172284STrond Myklebust 			&p->pdo);
99619172284STrond Myklebust 	rpc_destroy_pipe_data(p->pipe);
99719172284STrond Myklebust 	kfree(p);
998ccdc28f8SStanislav Kinsbursky }
999ccdc28f8SStanislav Kinsbursky 
1000414a6295STrond Myklebust static void __gss_pipe_release(struct kref *kref)
1001414a6295STrond Myklebust {
1002414a6295STrond Myklebust 	struct gss_pipe *p = container_of(kref, struct gss_pipe, kref);
1003414a6295STrond Myklebust 
1004414a6295STrond Myklebust 	__gss_pipe_free(p);
1005414a6295STrond Myklebust }
1006414a6295STrond Myklebust 
100719172284STrond Myklebust static void gss_pipe_free(struct gss_pipe *p)
1008ccdc28f8SStanislav Kinsbursky {
100919172284STrond Myklebust 	if (p != NULL)
1010414a6295STrond Myklebust 		kref_put(&p->kref, __gss_pipe_release);
1011ccdc28f8SStanislav Kinsbursky }
1012ccdc28f8SStanislav Kinsbursky 
10131da177e4SLinus Torvalds /*
10141da177e4SLinus Torvalds  * NOTE: we have the opportunity to use different
10151da177e4SLinus Torvalds  * parameters based on the input flavor (which must be a pseudoflavor)
10161da177e4SLinus Torvalds  */
1017eb6dc19dSTrond Myklebust static struct gss_auth *
101882b98ca5SSargun Dhillon gss_create_new(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
10191da177e4SLinus Torvalds {
1020c2190661STrond Myklebust 	rpc_authflavor_t flavor = args->pseudoflavor;
10211da177e4SLinus Torvalds 	struct gss_auth *gss_auth;
102219172284STrond Myklebust 	struct gss_pipe *gss_pipe;
10231da177e4SLinus Torvalds 	struct rpc_auth * auth;
10246a19275aSJ. Bruce Fields 	int err = -ENOMEM; /* XXX? */
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	if (!try_module_get(THIS_MODULE))
10276a19275aSJ. Bruce Fields 		return ERR_PTR(err);
10281da177e4SLinus Torvalds 	if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
10291da177e4SLinus Torvalds 		goto out_dec;
1030eb6dc19dSTrond Myklebust 	INIT_HLIST_NODE(&gss_auth->hash);
1031bd4a3eb1STrond Myklebust 	gss_auth->target_name = NULL;
1032c2190661STrond Myklebust 	if (args->target_name) {
1033c2190661STrond Myklebust 		gss_auth->target_name = kstrdup(args->target_name, GFP_KERNEL);
1034bd4a3eb1STrond Myklebust 		if (gss_auth->target_name == NULL)
1035bd4a3eb1STrond Myklebust 			goto err_free;
1036bd4a3eb1STrond Myklebust 	}
10371da177e4SLinus Torvalds 	gss_auth->client = clnt;
1038e726340aSTrond Myklebust 	gss_auth->net = get_net(rpc_net_ns(clnt));
10396a19275aSJ. Bruce Fields 	err = -EINVAL;
10401da177e4SLinus Torvalds 	gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
10410c77668dSChuck Lever 	if (!gss_auth->mech)
1042e726340aSTrond Myklebust 		goto err_put_net;
10431da177e4SLinus Torvalds 	gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
1044438b6fdeSJ. Bruce Fields 	if (gss_auth->service == 0)
1045438b6fdeSJ. Bruce Fields 		goto err_put_mech;
1046a699d65eSTrond Myklebust 	if (!gssd_running(gss_auth->net))
1047a699d65eSTrond Myklebust 		goto err_put_mech;
10481da177e4SLinus Torvalds 	auth = &gss_auth->rpc_auth;
10491da177e4SLinus Torvalds 	auth->au_cslack = GSS_CRED_SLACK >> 2;
10501da177e4SLinus Torvalds 	auth->au_rslack = GSS_VERF_SLACK >> 2;
1051a00275baSChuck Lever 	auth->au_verfsize = GSS_VERF_SLACK >> 2;
105235e77d21SChuck Lever 	auth->au_ralign = GSS_VERF_SLACK >> 2;
1053ce52914eSScott Mayhew 	auth->au_flags = 0;
10541da177e4SLinus Torvalds 	auth->au_ops = &authgss_ops;
10551da177e4SLinus Torvalds 	auth->au_flavor = flavor;
105665b80179SChuck Lever 	if (gss_pseudoflavor_to_datatouch(gss_auth->mech, flavor))
105765b80179SChuck Lever 		auth->au_flags |= RPCAUTH_AUTH_DATATOUCH;
1058331bc71cSTrond Myklebust 	refcount_set(&auth->au_count, 1);
10590285ed1fSTrond Myklebust 	kref_init(&gss_auth->kref);
10601da177e4SLinus Torvalds 
106119172284STrond Myklebust 	err = rpcauth_init_credcache(auth);
106219172284STrond Myklebust 	if (err)
106319172284STrond Myklebust 		goto err_put_mech;
106434769fc4S\"J. Bruce Fields\ 	/*
106534769fc4S\"J. Bruce Fields\ 	 * Note: if we created the old pipe first, then someone who
106634769fc4S\"J. Bruce Fields\ 	 * examined the directory at the right moment might conclude
106734769fc4S\"J. Bruce Fields\ 	 * that we supported only the old pipe.  So we instead create
106834769fc4S\"J. Bruce Fields\ 	 * the new pipe first.
106934769fc4S\"J. Bruce Fields\ 	 */
1070414a6295STrond Myklebust 	gss_pipe = gss_pipe_get(clnt, "gssd", &gss_upcall_ops_v1);
107119172284STrond Myklebust 	if (IS_ERR(gss_pipe)) {
107219172284STrond Myklebust 		err = PTR_ERR(gss_pipe);
107319172284STrond Myklebust 		goto err_destroy_credcache;
10746a19275aSJ. Bruce Fields 	}
107519172284STrond Myklebust 	gss_auth->gss_pipe[1] = gss_pipe;
10761da177e4SLinus Torvalds 
1077414a6295STrond Myklebust 	gss_pipe = gss_pipe_get(clnt, gss_auth->mech->gm_name,
107819172284STrond Myklebust 			&gss_upcall_ops_v0);
107919172284STrond Myklebust 	if (IS_ERR(gss_pipe)) {
108019172284STrond Myklebust 		err = PTR_ERR(gss_pipe);
1081c239d83bSStanislav Kinsbursky 		goto err_destroy_pipe_1;
1082c239d83bSStanislav Kinsbursky 	}
108319172284STrond Myklebust 	gss_auth->gss_pipe[0] = gss_pipe;
108407a2bf1dSTrond Myklebust 
1085eb6dc19dSTrond Myklebust 	return gss_auth;
1086c239d83bSStanislav Kinsbursky err_destroy_pipe_1:
1087414a6295STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[1]);
108819172284STrond Myklebust err_destroy_credcache:
108919172284STrond Myklebust 	rpcauth_destroy_credcache(auth);
10901da177e4SLinus Torvalds err_put_mech:
10911da177e4SLinus Torvalds 	gss_mech_put(gss_auth->mech);
1092e726340aSTrond Myklebust err_put_net:
1093e726340aSTrond Myklebust 	put_net(gss_auth->net);
10941da177e4SLinus Torvalds err_free:
1095bd4a3eb1STrond Myklebust 	kfree(gss_auth->target_name);
10961da177e4SLinus Torvalds 	kfree(gss_auth);
10971da177e4SLinus Torvalds out_dec:
10981da177e4SLinus Torvalds 	module_put(THIS_MODULE);
10990c77668dSChuck Lever 	trace_rpcgss_createauth(flavor, err);
11006a19275aSJ. Bruce Fields 	return ERR_PTR(err);
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds static void
11040285ed1fSTrond Myklebust gss_free(struct gss_auth *gss_auth)
11050285ed1fSTrond Myklebust {
110619172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[0]);
110719172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[1]);
11080285ed1fSTrond Myklebust 	gss_mech_put(gss_auth->mech);
1109e726340aSTrond Myklebust 	put_net(gss_auth->net);
1110bd4a3eb1STrond Myklebust 	kfree(gss_auth->target_name);
11110285ed1fSTrond Myklebust 
11120285ed1fSTrond Myklebust 	kfree(gss_auth);
11130285ed1fSTrond Myklebust 	module_put(THIS_MODULE);
11140285ed1fSTrond Myklebust }
11150285ed1fSTrond Myklebust 
11160285ed1fSTrond Myklebust static void
11170285ed1fSTrond Myklebust gss_free_callback(struct kref *kref)
11180285ed1fSTrond Myklebust {
11190285ed1fSTrond Myklebust 	struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
11200285ed1fSTrond Myklebust 
11210285ed1fSTrond Myklebust 	gss_free(gss_auth);
11220285ed1fSTrond Myklebust }
11230285ed1fSTrond Myklebust 
11240285ed1fSTrond Myklebust static void
11259eb2ddb4STrond Myklebust gss_put_auth(struct gss_auth *gss_auth)
11269eb2ddb4STrond Myklebust {
11279eb2ddb4STrond Myklebust 	kref_put(&gss_auth->kref, gss_free_callback);
11289eb2ddb4STrond Myklebust }
11299eb2ddb4STrond Myklebust 
11309eb2ddb4STrond Myklebust static void
11311da177e4SLinus Torvalds gss_destroy(struct rpc_auth *auth)
11321da177e4SLinus Torvalds {
113319172284STrond Myklebust 	struct gss_auth *gss_auth = container_of(auth,
113419172284STrond Myklebust 			struct gss_auth, rpc_auth);
11351da177e4SLinus Torvalds 
1136eb6dc19dSTrond Myklebust 	if (hash_hashed(&gss_auth->hash)) {
1137eb6dc19dSTrond Myklebust 		spin_lock(&gss_auth_hash_lock);
1138eb6dc19dSTrond Myklebust 		hash_del(&gss_auth->hash);
1139eb6dc19dSTrond Myklebust 		spin_unlock(&gss_auth_hash_lock);
1140eb6dc19dSTrond Myklebust 	}
1141eb6dc19dSTrond Myklebust 
114219172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[0]);
114319172284STrond Myklebust 	gss_auth->gss_pipe[0] = NULL;
114419172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[1]);
114519172284STrond Myklebust 	gss_auth->gss_pipe[1] = NULL;
11463ab9bb72STrond Myklebust 	rpcauth_destroy_credcache(auth);
11473ab9bb72STrond Myklebust 
11489eb2ddb4STrond Myklebust 	gss_put_auth(gss_auth);
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
1151a0f6ed8eSJ. Bruce Fields /*
1152a0f6ed8eSJ. Bruce Fields  * Auths may be shared between rpc clients that were cloned from a
1153a0f6ed8eSJ. Bruce Fields  * common client with the same xprt, if they also share the flavor and
1154a0f6ed8eSJ. Bruce Fields  * target_name.
1155a0f6ed8eSJ. Bruce Fields  *
1156a0f6ed8eSJ. Bruce Fields  * The auth is looked up from the oldest parent sharing the same
1157a0f6ed8eSJ. Bruce Fields  * cl_xprt, and the auth itself references only that common parent
1158a0f6ed8eSJ. Bruce Fields  * (which is guaranteed to last as long as any of its descendants).
1159a0f6ed8eSJ. Bruce Fields  */
1160eb6dc19dSTrond Myklebust static struct gss_auth *
116182b98ca5SSargun Dhillon gss_auth_find_or_add_hashed(const struct rpc_auth_create_args *args,
1162eb6dc19dSTrond Myklebust 		struct rpc_clnt *clnt,
1163eb6dc19dSTrond Myklebust 		struct gss_auth *new)
1164eb6dc19dSTrond Myklebust {
1165eb6dc19dSTrond Myklebust 	struct gss_auth *gss_auth;
1166eb6dc19dSTrond Myklebust 	unsigned long hashval = (unsigned long)clnt;
1167eb6dc19dSTrond Myklebust 
1168eb6dc19dSTrond Myklebust 	spin_lock(&gss_auth_hash_lock);
1169eb6dc19dSTrond Myklebust 	hash_for_each_possible(gss_auth_hash_table,
1170eb6dc19dSTrond Myklebust 			gss_auth,
1171eb6dc19dSTrond Myklebust 			hash,
1172eb6dc19dSTrond Myklebust 			hashval) {
1173a0f6ed8eSJ. Bruce Fields 		if (gss_auth->client != clnt)
1174a0f6ed8eSJ. Bruce Fields 			continue;
1175eb6dc19dSTrond Myklebust 		if (gss_auth->rpc_auth.au_flavor != args->pseudoflavor)
1176eb6dc19dSTrond Myklebust 			continue;
1177eb6dc19dSTrond Myklebust 		if (gss_auth->target_name != args->target_name) {
1178eb6dc19dSTrond Myklebust 			if (gss_auth->target_name == NULL)
1179eb6dc19dSTrond Myklebust 				continue;
1180eb6dc19dSTrond Myklebust 			if (args->target_name == NULL)
1181eb6dc19dSTrond Myklebust 				continue;
1182eb6dc19dSTrond Myklebust 			if (strcmp(gss_auth->target_name, args->target_name))
1183eb6dc19dSTrond Myklebust 				continue;
1184eb6dc19dSTrond Myklebust 		}
1185331bc71cSTrond Myklebust 		if (!refcount_inc_not_zero(&gss_auth->rpc_auth.au_count))
1186eb6dc19dSTrond Myklebust 			continue;
1187eb6dc19dSTrond Myklebust 		goto out;
1188eb6dc19dSTrond Myklebust 	}
1189eb6dc19dSTrond Myklebust 	if (new)
1190eb6dc19dSTrond Myklebust 		hash_add(gss_auth_hash_table, &new->hash, hashval);
1191eb6dc19dSTrond Myklebust 	gss_auth = new;
1192eb6dc19dSTrond Myklebust out:
1193eb6dc19dSTrond Myklebust 	spin_unlock(&gss_auth_hash_lock);
1194eb6dc19dSTrond Myklebust 	return gss_auth;
1195eb6dc19dSTrond Myklebust }
1196eb6dc19dSTrond Myklebust 
1197eb6dc19dSTrond Myklebust static struct gss_auth *
119882b98ca5SSargun Dhillon gss_create_hashed(const struct rpc_auth_create_args *args,
119982b98ca5SSargun Dhillon 		  struct rpc_clnt *clnt)
1200eb6dc19dSTrond Myklebust {
1201eb6dc19dSTrond Myklebust 	struct gss_auth *gss_auth;
1202eb6dc19dSTrond Myklebust 	struct gss_auth *new;
1203eb6dc19dSTrond Myklebust 
1204eb6dc19dSTrond Myklebust 	gss_auth = gss_auth_find_or_add_hashed(args, clnt, NULL);
1205eb6dc19dSTrond Myklebust 	if (gss_auth != NULL)
1206eb6dc19dSTrond Myklebust 		goto out;
1207eb6dc19dSTrond Myklebust 	new = gss_create_new(args, clnt);
1208eb6dc19dSTrond Myklebust 	if (IS_ERR(new))
1209eb6dc19dSTrond Myklebust 		return new;
1210eb6dc19dSTrond Myklebust 	gss_auth = gss_auth_find_or_add_hashed(args, clnt, new);
1211eb6dc19dSTrond Myklebust 	if (gss_auth != new)
1212eb6dc19dSTrond Myklebust 		gss_destroy(&new->rpc_auth);
1213eb6dc19dSTrond Myklebust out:
1214eb6dc19dSTrond Myklebust 	return gss_auth;
1215eb6dc19dSTrond Myklebust }
1216eb6dc19dSTrond Myklebust 
1217eb6dc19dSTrond Myklebust static struct rpc_auth *
121882b98ca5SSargun Dhillon gss_create(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
1219eb6dc19dSTrond Myklebust {
1220eb6dc19dSTrond Myklebust 	struct gss_auth *gss_auth;
1221ad01b2c6STrond Myklebust 	struct rpc_xprt_switch *xps = rcu_access_pointer(clnt->cl_xpi.xpi_xpswitch);
1222eb6dc19dSTrond Myklebust 
1223eb6dc19dSTrond Myklebust 	while (clnt != clnt->cl_parent) {
1224eb6dc19dSTrond Myklebust 		struct rpc_clnt *parent = clnt->cl_parent;
1225eb6dc19dSTrond Myklebust 		/* Find the original parent for this transport */
1226ad01b2c6STrond Myklebust 		if (rcu_access_pointer(parent->cl_xpi.xpi_xpswitch) != xps)
1227eb6dc19dSTrond Myklebust 			break;
1228eb6dc19dSTrond Myklebust 		clnt = parent;
1229eb6dc19dSTrond Myklebust 	}
1230eb6dc19dSTrond Myklebust 
1231eb6dc19dSTrond Myklebust 	gss_auth = gss_create_hashed(args, clnt);
1232eb6dc19dSTrond Myklebust 	if (IS_ERR(gss_auth))
1233eb6dc19dSTrond Myklebust 		return ERR_CAST(gss_auth);
1234eb6dc19dSTrond Myklebust 	return &gss_auth->rpc_auth;
1235eb6dc19dSTrond Myklebust }
1236eb6dc19dSTrond Myklebust 
1237a652a4bcSTrond Myklebust static struct gss_cred *
1238a652a4bcSTrond Myklebust gss_dup_cred(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
1239a652a4bcSTrond Myklebust {
1240a652a4bcSTrond Myklebust 	struct gss_cred *new;
1241a652a4bcSTrond Myklebust 
1242a652a4bcSTrond Myklebust 	/* Make a copy of the cred so that we can reference count it */
124312a3ad61STrond Myklebust 	new = kzalloc(sizeof(*gss_cred), GFP_NOFS);
1244a652a4bcSTrond Myklebust 	if (new) {
1245a652a4bcSTrond Myklebust 		struct auth_cred acred = {
12468276c902SNeilBrown 			.cred = gss_cred->gc_base.cr_cred,
1247a652a4bcSTrond Myklebust 		};
1248a652a4bcSTrond Myklebust 		struct gss_cl_ctx *ctx =
1249a652a4bcSTrond Myklebust 			rcu_dereference_protected(gss_cred->gc_ctx, 1);
1250a652a4bcSTrond Myklebust 
1251a652a4bcSTrond Myklebust 		rpcauth_init_cred(&new->gc_base, &acred,
1252a652a4bcSTrond Myklebust 				&gss_auth->rpc_auth,
1253a652a4bcSTrond Myklebust 				&gss_nullops);
1254a652a4bcSTrond Myklebust 		new->gc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
1255a652a4bcSTrond Myklebust 		new->gc_service = gss_cred->gc_service;
1256a652a4bcSTrond Myklebust 		new->gc_principal = gss_cred->gc_principal;
1257a652a4bcSTrond Myklebust 		kref_get(&gss_auth->kref);
1258a652a4bcSTrond Myklebust 		rcu_assign_pointer(new->gc_ctx, ctx);
1259a652a4bcSTrond Myklebust 		gss_get_ctx(ctx);
1260a652a4bcSTrond Myklebust 	}
1261a652a4bcSTrond Myklebust 	return new;
1262a652a4bcSTrond Myklebust }
1263a652a4bcSTrond Myklebust 
12640df7fb74STrond Myklebust /*
1265a652a4bcSTrond Myklebust  * gss_send_destroy_context will cause the RPCSEC_GSS to send a NULL RPC call
12660df7fb74STrond Myklebust  * to the server with the GSS control procedure field set to
12670df7fb74STrond Myklebust  * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
12680df7fb74STrond Myklebust  * all RPCSEC_GSS state associated with that context.
12690df7fb74STrond Myklebust  */
1270a652a4bcSTrond Myklebust static void
1271a652a4bcSTrond Myklebust gss_send_destroy_context(struct rpc_cred *cred)
12720df7fb74STrond Myklebust {
12730df7fb74STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
12740df7fb74STrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
1275c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx = rcu_dereference_protected(gss_cred->gc_ctx, 1);
1276a652a4bcSTrond Myklebust 	struct gss_cred *new;
12770df7fb74STrond Myklebust 	struct rpc_task *task;
12780df7fb74STrond Myklebust 
1279a652a4bcSTrond Myklebust 	new = gss_dup_cred(gss_auth, gss_cred);
1280a652a4bcSTrond Myklebust 	if (new) {
1281c5e6aecdSJeff Layton 		ctx->gc_proc = RPC_GSS_PROC_DESTROY;
12820df7fb74STrond Myklebust 
1283a652a4bcSTrond Myklebust 		task = rpc_call_null(gss_auth->client, &new->gc_base,
1284a652a4bcSTrond Myklebust 				RPC_TASK_ASYNC|RPC_TASK_SOFT);
12850df7fb74STrond Myklebust 		if (!IS_ERR(task))
12860df7fb74STrond Myklebust 			rpc_put_task(task);
12870df7fb74STrond Myklebust 
1288a652a4bcSTrond Myklebust 		put_rpccred(&new->gc_base);
1289a652a4bcSTrond Myklebust 	}
12900df7fb74STrond Myklebust }
12910df7fb74STrond Myklebust 
12920df7fb74STrond Myklebust /* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
12931da177e4SLinus Torvalds  * to create a new cred or context, so they check that things have been
12941da177e4SLinus Torvalds  * allocated before freeing them. */
12951da177e4SLinus Torvalds static void
12965d28dc82STrond Myklebust gss_do_free_ctx(struct gss_cl_ctx *ctx)
12971da177e4SLinus Torvalds {
12980d8a3746STrond Myklebust 	gss_delete_sec_context(&ctx->gc_gss_ctx);
12991da177e4SLinus Torvalds 	kfree(ctx->gc_wire_ctx.data);
13002004c726SJeff Layton 	kfree(ctx->gc_acceptor.data);
13011da177e4SLinus Torvalds 	kfree(ctx);
13021da177e4SLinus Torvalds }
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds static void
13055d28dc82STrond Myklebust gss_free_ctx_callback(struct rcu_head *head)
13065d28dc82STrond Myklebust {
13075d28dc82STrond Myklebust 	struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
13085d28dc82STrond Myklebust 	gss_do_free_ctx(ctx);
13095d28dc82STrond Myklebust }
13105d28dc82STrond Myklebust 
13115d28dc82STrond Myklebust static void
13125d28dc82STrond Myklebust gss_free_ctx(struct gss_cl_ctx *ctx)
13135d28dc82STrond Myklebust {
13145d28dc82STrond Myklebust 	call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
13155d28dc82STrond Myklebust }
13165d28dc82STrond Myklebust 
13175d28dc82STrond Myklebust static void
131831be5bf1STrond Myklebust gss_free_cred(struct gss_cred *gss_cred)
13191da177e4SLinus Torvalds {
132031be5bf1STrond Myklebust 	kfree(gss_cred);
132131be5bf1STrond Myklebust }
13221da177e4SLinus Torvalds 
132331be5bf1STrond Myklebust static void
132431be5bf1STrond Myklebust gss_free_cred_callback(struct rcu_head *head)
132531be5bf1STrond Myklebust {
132631be5bf1STrond Myklebust 	struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
132731be5bf1STrond Myklebust 	gss_free_cred(gss_cred);
132831be5bf1STrond Myklebust }
13291da177e4SLinus Torvalds 
133031be5bf1STrond Myklebust static void
13316dcd3926SJeff Layton gss_destroy_nullcred(struct rpc_cred *cred)
133231be5bf1STrond Myklebust {
13335d28dc82STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
13340285ed1fSTrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
1335c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx = rcu_dereference_protected(gss_cred->gc_ctx, 1);
13365d28dc82STrond Myklebust 
1337a9b3cd7fSStephen Hemminger 	RCU_INIT_POINTER(gss_cred->gc_ctx, NULL);
133897f68c6bSNeilBrown 	put_cred(cred->cr_cred);
133931be5bf1STrond Myklebust 	call_rcu(&cred->cr_rcu, gss_free_cred_callback);
13405d28dc82STrond Myklebust 	if (ctx)
13415d28dc82STrond Myklebust 		gss_put_ctx(ctx);
13429eb2ddb4STrond Myklebust 	gss_put_auth(gss_auth);
13431da177e4SLinus Torvalds }
13441da177e4SLinus Torvalds 
13456dcd3926SJeff Layton static void
13466dcd3926SJeff Layton gss_destroy_cred(struct rpc_cred *cred)
13476dcd3926SJeff Layton {
13486dcd3926SJeff Layton 
1349a652a4bcSTrond Myklebust 	if (test_and_clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0)
1350a652a4bcSTrond Myklebust 		gss_send_destroy_context(cred);
13516dcd3926SJeff Layton 	gss_destroy_nullcred(cred);
13526dcd3926SJeff Layton }
13536dcd3926SJeff Layton 
1354a960f8d6SFrank Sorenson static int
1355a960f8d6SFrank Sorenson gss_hash_cred(struct auth_cred *acred, unsigned int hashbits)
1356a960f8d6SFrank Sorenson {
13578276c902SNeilBrown 	return hash_64(from_kuid(&init_user_ns, acred->cred->fsuid), hashbits);
1358a960f8d6SFrank Sorenson }
1359a960f8d6SFrank Sorenson 
13601da177e4SLinus Torvalds /*
13611da177e4SLinus Torvalds  * Lookup RPCSEC_GSS cred for the current process
13621da177e4SLinus Torvalds  */
13631da177e4SLinus Torvalds static struct rpc_cred *
13648a317760STrond Myklebust gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
13651da177e4SLinus Torvalds {
13663c6e0bc8SJeff Layton 	return rpcauth_lookup_credcache(auth, acred, flags, GFP_NOFS);
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds static struct rpc_cred *
13703c6e0bc8SJeff Layton gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags, gfp_t gfp)
13711da177e4SLinus Torvalds {
13721da177e4SLinus Torvalds 	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
13731da177e4SLinus Torvalds 	struct gss_cred	*cred = NULL;
13741da177e4SLinus Torvalds 	int err = -ENOMEM;
13751da177e4SLinus Torvalds 
13763c6e0bc8SJeff Layton 	if (!(cred = kzalloc(sizeof(*cred), gfp)))
13771da177e4SLinus Torvalds 		goto out_err;
13781da177e4SLinus Torvalds 
13795fe4755eSTrond Myklebust 	rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
13801da177e4SLinus Torvalds 	/*
13811da177e4SLinus Torvalds 	 * Note: in order to force a call to call_refresh(), we deliberately
13821da177e4SLinus Torvalds 	 * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
13831da177e4SLinus Torvalds 	 */
1384fc432dd9STrond Myklebust 	cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
13851da177e4SLinus Torvalds 	cred->gc_service = gss_auth->service;
138668c97153STrond Myklebust 	cred->gc_principal = acred->principal;
13870285ed1fSTrond Myklebust 	kref_get(&gss_auth->kref);
13881da177e4SLinus Torvalds 	return &cred->gc_base;
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds out_err:
13911da177e4SLinus Torvalds 	return ERR_PTR(err);
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds static int
1395fba3bad4STrond Myklebust gss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
1396fba3bad4STrond Myklebust {
1397fba3bad4STrond Myklebust 	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
1398fba3bad4STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
1399fba3bad4STrond Myklebust 	int err;
1400fba3bad4STrond Myklebust 
1401fba3bad4STrond Myklebust 	do {
1402fba3bad4STrond Myklebust 		err = gss_create_upcall(gss_auth, gss_cred);
1403fba3bad4STrond Myklebust 	} while (err == -EAGAIN);
1404fba3bad4STrond Myklebust 	return err;
1405fba3bad4STrond Myklebust }
1406fba3bad4STrond Myklebust 
1407a0337d1dSJeff Layton static char *
1408a0337d1dSJeff Layton gss_stringify_acceptor(struct rpc_cred *cred)
1409a0337d1dSJeff Layton {
1410c5e6aecdSJeff Layton 	char *string = NULL;
1411a0337d1dSJeff Layton 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1412c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx;
1413b3ecba09SJeff Layton 	unsigned int len;
1414c5e6aecdSJeff Layton 	struct xdr_netobj *acceptor;
1415c5e6aecdSJeff Layton 
1416c5e6aecdSJeff Layton 	rcu_read_lock();
1417c5e6aecdSJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
1418c5e6aecdSJeff Layton 	if (!ctx)
1419c5e6aecdSJeff Layton 		goto out;
1420c5e6aecdSJeff Layton 
1421b3ecba09SJeff Layton 	len = ctx->gc_acceptor.len;
1422b3ecba09SJeff Layton 	rcu_read_unlock();
1423a0337d1dSJeff Layton 
1424a0337d1dSJeff Layton 	/* no point if there's no string */
1425b3ecba09SJeff Layton 	if (!len)
1426b3ecba09SJeff Layton 		return NULL;
1427b3ecba09SJeff Layton realloc:
1428b3ecba09SJeff Layton 	string = kmalloc(len + 1, GFP_KERNEL);
1429a0337d1dSJeff Layton 	if (!string)
1430b3ecba09SJeff Layton 		return NULL;
1431b3ecba09SJeff Layton 
1432b3ecba09SJeff Layton 	rcu_read_lock();
1433b3ecba09SJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
1434b3ecba09SJeff Layton 
1435b3ecba09SJeff Layton 	/* did the ctx disappear or was it replaced by one with no acceptor? */
1436b3ecba09SJeff Layton 	if (!ctx || !ctx->gc_acceptor.len) {
1437b3ecba09SJeff Layton 		kfree(string);
1438b3ecba09SJeff Layton 		string = NULL;
1439c5e6aecdSJeff Layton 		goto out;
1440b3ecba09SJeff Layton 	}
1441b3ecba09SJeff Layton 
1442b3ecba09SJeff Layton 	acceptor = &ctx->gc_acceptor;
1443b3ecba09SJeff Layton 
1444b3ecba09SJeff Layton 	/*
1445b3ecba09SJeff Layton 	 * Did we find a new acceptor that's longer than the original? Allocate
1446b3ecba09SJeff Layton 	 * a longer buffer and try again.
1447b3ecba09SJeff Layton 	 */
1448b3ecba09SJeff Layton 	if (len < acceptor->len) {
1449b3ecba09SJeff Layton 		len = acceptor->len;
1450b3ecba09SJeff Layton 		rcu_read_unlock();
1451b3ecba09SJeff Layton 		kfree(string);
1452b3ecba09SJeff Layton 		goto realloc;
1453b3ecba09SJeff Layton 	}
1454a0337d1dSJeff Layton 
1455a0337d1dSJeff Layton 	memcpy(string, acceptor->data, acceptor->len);
1456a0337d1dSJeff Layton 	string[acceptor->len] = '\0';
1457c5e6aecdSJeff Layton out:
1458c5e6aecdSJeff Layton 	rcu_read_unlock();
1459a0337d1dSJeff Layton 	return string;
1460a0337d1dSJeff Layton }
1461a0337d1dSJeff Layton 
14624de6caa2SAndy Adamson /*
14634de6caa2SAndy Adamson  * Returns -EACCES if GSS context is NULL or will expire within the
14644de6caa2SAndy Adamson  * timeout (miliseconds)
14654de6caa2SAndy Adamson  */
14664de6caa2SAndy Adamson static int
14674de6caa2SAndy Adamson gss_key_timeout(struct rpc_cred *rc)
14684de6caa2SAndy Adamson {
14694de6caa2SAndy Adamson 	struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
1470c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx;
1471cc6a7aabSArnd Bergmann 	unsigned long timeout = jiffies + (gss_key_expire_timeo * HZ);
1472cc6a7aabSArnd Bergmann 	int ret = 0;
14734de6caa2SAndy Adamson 
1474c5e6aecdSJeff Layton 	rcu_read_lock();
1475c5e6aecdSJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
1476cc6a7aabSArnd Bergmann 	if (!ctx || time_after(timeout, ctx->gc_expiry))
1477cc6a7aabSArnd Bergmann 		ret = -EACCES;
1478c5e6aecdSJeff Layton 	rcu_read_unlock();
1479cc6a7aabSArnd Bergmann 
1480cc6a7aabSArnd Bergmann 	return ret;
14814de6caa2SAndy Adamson }
14824de6caa2SAndy Adamson 
1483fba3bad4STrond Myklebust static int
14848a317760STrond Myklebust gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
14851da177e4SLinus Torvalds {
14861da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
1487c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx;
14884de6caa2SAndy Adamson 	int ret;
14891da177e4SLinus Torvalds 
1490cd019f75STrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
14918a317760STrond Myklebust 		goto out;
14921da177e4SLinus Torvalds 	/* Don't match with creds that have expired. */
1493c5e6aecdSJeff Layton 	rcu_read_lock();
1494c5e6aecdSJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
1495c5e6aecdSJeff Layton 	if (!ctx || time_after(jiffies, ctx->gc_expiry)) {
1496c5e6aecdSJeff Layton 		rcu_read_unlock();
1497cd019f75STrond Myklebust 		return 0;
1498c5e6aecdSJeff Layton 	}
1499c5e6aecdSJeff Layton 	rcu_read_unlock();
1500cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags))
15011da177e4SLinus Torvalds 		return 0;
15028a317760STrond Myklebust out:
150368c97153STrond Myklebust 	if (acred->principal != NULL) {
150468c97153STrond Myklebust 		if (gss_cred->gc_principal == NULL)
150568c97153STrond Myklebust 			return 0;
15064de6caa2SAndy Adamson 		ret = strcmp(acred->principal, gss_cred->gc_principal) == 0;
1507ddf529eeSNeilBrown 	} else {
150868c97153STrond Myklebust 		if (gss_cred->gc_principal != NULL)
15097c67db3aSTrond Myklebust 			return 0;
151004d1532bSNeilBrown 		ret = uid_eq(rc->cr_cred->fsuid, acred->cred->fsuid);
15114de6caa2SAndy Adamson 	}
15124de6caa2SAndy Adamson 	return ret;
15131da177e4SLinus Torvalds }
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds /*
15161da177e4SLinus Torvalds  * Marshal credentials.
1517e8680a24SChuck Lever  *
1518e8680a24SChuck Lever  * The expensive part is computing the verifier. We can't cache a
1519e8680a24SChuck Lever  * pre-computed version of the verifier because the seqno, which
1520e8680a24SChuck Lever  * is different every time, is included in the MIC.
15211da177e4SLinus Torvalds  */
1522e8680a24SChuck Lever static int gss_marshal(struct rpc_task *task, struct xdr_stream *xdr)
15231da177e4SLinus Torvalds {
1524a17c2153STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1525a17c2153STrond Myklebust 	struct rpc_cred *cred = req->rq_cred;
15261da177e4SLinus Torvalds 	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
15271da177e4SLinus Torvalds 						 gc_base);
15281da177e4SLinus Torvalds 	struct gss_cl_ctx	*ctx = gss_cred_get_ctx(cred);
1529e8680a24SChuck Lever 	__be32		*p, *cred_len;
15301da177e4SLinus Torvalds 	u32             maj_stat = 0;
15311da177e4SLinus Torvalds 	struct xdr_netobj mic;
15321da177e4SLinus Torvalds 	struct kvec	iov;
15331da177e4SLinus Torvalds 	struct xdr_buf	verf_buf;
15340c77668dSChuck Lever 	int status;
15351da177e4SLinus Torvalds 
1536e8680a24SChuck Lever 	/* Credential */
1537e8680a24SChuck Lever 
1538e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, 7 * sizeof(*p) +
1539e8680a24SChuck Lever 			      ctx->gc_wire_ctx.len);
1540e8680a24SChuck Lever 	if (!p)
15410c77668dSChuck Lever 		goto marshal_failed;
1542e8680a24SChuck Lever 	*p++ = rpc_auth_gss;
15431da177e4SLinus Torvalds 	cred_len = p++;
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds 	spin_lock(&ctx->gc_seq_lock);
154697b78ae9STrond Myklebust 	req->rq_seqno = (ctx->gc_seq < MAXSEQ) ? ctx->gc_seq++ : MAXSEQ;
15471da177e4SLinus Torvalds 	spin_unlock(&ctx->gc_seq_lock);
154897b78ae9STrond Myklebust 	if (req->rq_seqno == MAXSEQ)
15490c77668dSChuck Lever 		goto expired;
15500c77668dSChuck Lever 	trace_rpcgss_seqno(task);
15511da177e4SLinus Torvalds 
1552e8680a24SChuck Lever 	*p++ = cpu_to_be32(RPC_GSS_VERSION);
1553e8680a24SChuck Lever 	*p++ = cpu_to_be32(ctx->gc_proc);
1554e8680a24SChuck Lever 	*p++ = cpu_to_be32(req->rq_seqno);
1555e8680a24SChuck Lever 	*p++ = cpu_to_be32(gss_cred->gc_service);
15561da177e4SLinus Torvalds 	p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
1557e8680a24SChuck Lever 	*cred_len = cpu_to_be32((p - (cred_len + 1)) << 2);
1558e8680a24SChuck Lever 
1559e8680a24SChuck Lever 	/* Verifier */
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	/* We compute the checksum for the verifier over the xdr-encoded bytes
15621da177e4SLinus Torvalds 	 * starting with the xid and ending at the end of the credential: */
1563067fb11bSChuck Lever 	iov.iov_base = req->rq_snd_buf.head[0].iov_base;
15641da177e4SLinus Torvalds 	iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
15651da177e4SLinus Torvalds 	xdr_buf_from_iov(&iov, &verf_buf);
15661da177e4SLinus Torvalds 
1567e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, sizeof(*p));
1568e8680a24SChuck Lever 	if (!p)
15690c77668dSChuck Lever 		goto marshal_failed;
1570e8680a24SChuck Lever 	*p++ = rpc_auth_gss;
15711da177e4SLinus Torvalds 	mic.data = (u8 *)(p + 1);
157200fd6e14SJ. Bruce Fields 	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
1573e8680a24SChuck Lever 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
15740c77668dSChuck Lever 		goto expired;
1575e8680a24SChuck Lever 	else if (maj_stat != 0)
15760c77668dSChuck Lever 		goto bad_mic;
1577e8680a24SChuck Lever 	if (xdr_stream_encode_opaque_inline(xdr, (void **)&p, mic.len) < 0)
15780c77668dSChuck Lever 		goto marshal_failed;
15790c77668dSChuck Lever 	status = 0;
15800c77668dSChuck Lever out:
15811da177e4SLinus Torvalds 	gss_put_ctx(ctx);
15820c77668dSChuck Lever 	return status;
15830c77668dSChuck Lever expired:
158497b78ae9STrond Myklebust 	clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
15850c77668dSChuck Lever 	status = -EKEYEXPIRED;
15860c77668dSChuck Lever 	goto out;
15870c77668dSChuck Lever marshal_failed:
15880c77668dSChuck Lever 	status = -EMSGSIZE;
15890c77668dSChuck Lever 	goto out;
15900c77668dSChuck Lever bad_mic:
15910c77668dSChuck Lever 	trace_rpcgss_get_mic(task, maj_stat);
15920c77668dSChuck Lever 	status = -EIO;
15930c77668dSChuck Lever 	goto out;
15941da177e4SLinus Torvalds }
15951da177e4SLinus Torvalds 
1596cd019f75STrond Myklebust static int gss_renew_cred(struct rpc_task *task)
1597cd019f75STrond Myklebust {
1598a17c2153STrond Myklebust 	struct rpc_cred *oldcred = task->tk_rqstp->rq_cred;
1599cd019f75STrond Myklebust 	struct gss_cred *gss_cred = container_of(oldcred,
1600cd019f75STrond Myklebust 						 struct gss_cred,
1601cd019f75STrond Myklebust 						 gc_base);
1602cd019f75STrond Myklebust 	struct rpc_auth *auth = oldcred->cr_auth;
1603cd019f75STrond Myklebust 	struct auth_cred acred = {
160497f68c6bSNeilBrown 		.cred = oldcred->cr_cred,
160568c97153STrond Myklebust 		.principal = gss_cred->gc_principal,
1606cd019f75STrond Myklebust 	};
1607cd019f75STrond Myklebust 	struct rpc_cred *new;
1608cd019f75STrond Myklebust 
1609cd019f75STrond Myklebust 	new = gss_lookup_cred(auth, &acred, RPCAUTH_LOOKUP_NEW);
1610cd019f75STrond Myklebust 	if (IS_ERR(new))
1611cd019f75STrond Myklebust 		return PTR_ERR(new);
1612a17c2153STrond Myklebust 	task->tk_rqstp->rq_cred = new;
1613cd019f75STrond Myklebust 	put_rpccred(oldcred);
1614cd019f75STrond Myklebust 	return 0;
1615cd019f75STrond Myklebust }
1616cd019f75STrond Myklebust 
1617126e216aSTrond Myklebust static int gss_cred_is_negative_entry(struct rpc_cred *cred)
1618126e216aSTrond Myklebust {
1619126e216aSTrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEGATIVE, &cred->cr_flags)) {
1620126e216aSTrond Myklebust 		unsigned long now = jiffies;
1621126e216aSTrond Myklebust 		unsigned long begin, expire;
1622126e216aSTrond Myklebust 		struct gss_cred *gss_cred;
1623126e216aSTrond Myklebust 
1624126e216aSTrond Myklebust 		gss_cred = container_of(cred, struct gss_cred, gc_base);
1625126e216aSTrond Myklebust 		begin = gss_cred->gc_upcall_timestamp;
1626126e216aSTrond Myklebust 		expire = begin + gss_expired_cred_retry_delay * HZ;
1627126e216aSTrond Myklebust 
1628126e216aSTrond Myklebust 		if (time_in_range_open(now, begin, expire))
1629126e216aSTrond Myklebust 			return 1;
1630126e216aSTrond Myklebust 	}
1631126e216aSTrond Myklebust 	return 0;
1632126e216aSTrond Myklebust }
1633126e216aSTrond Myklebust 
16341da177e4SLinus Torvalds /*
16351da177e4SLinus Torvalds * Refresh credentials. XXX - finish
16361da177e4SLinus Torvalds */
16371da177e4SLinus Torvalds static int
16381da177e4SLinus Torvalds gss_refresh(struct rpc_task *task)
16391da177e4SLinus Torvalds {
1640a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
1641cd019f75STrond Myklebust 	int ret = 0;
16421da177e4SLinus Torvalds 
1643126e216aSTrond Myklebust 	if (gss_cred_is_negative_entry(cred))
1644126e216aSTrond Myklebust 		return -EKEYEXPIRED;
1645126e216aSTrond Myklebust 
1646cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
1647cd019f75STrond Myklebust 			!test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags)) {
1648cd019f75STrond Myklebust 		ret = gss_renew_cred(task);
1649cd019f75STrond Myklebust 		if (ret < 0)
1650cd019f75STrond Myklebust 			goto out;
1651a17c2153STrond Myklebust 		cred = task->tk_rqstp->rq_cred;
1652cd019f75STrond Myklebust 	}
1653cd019f75STrond Myklebust 
1654cd019f75STrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
1655cd019f75STrond Myklebust 		ret = gss_refresh_upcall(task);
1656cd019f75STrond Myklebust out:
1657cd019f75STrond Myklebust 	return ret;
16581da177e4SLinus Torvalds }
16591da177e4SLinus Torvalds 
16600df7fb74STrond Myklebust /* Dummy refresh routine: used only when destroying the context */
16610df7fb74STrond Myklebust static int
16620df7fb74STrond Myklebust gss_refresh_null(struct rpc_task *task)
16630df7fb74STrond Myklebust {
1664c297c8b9SAndy Adamson 	return 0;
16650df7fb74STrond Myklebust }
16660df7fb74STrond Myklebust 
1667a0584ee9SChuck Lever static int
1668a0584ee9SChuck Lever gss_validate(struct rpc_task *task, struct xdr_stream *xdr)
16691da177e4SLinus Torvalds {
1670a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
16711da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1672a0584ee9SChuck Lever 	__be32		*p, *seq = NULL;
16731da177e4SLinus Torvalds 	struct kvec	iov;
16741da177e4SLinus Torvalds 	struct xdr_buf	verf_buf;
16751da177e4SLinus Torvalds 	struct xdr_netobj mic;
1676a0584ee9SChuck Lever 	u32		len, maj_stat;
1677a0584ee9SChuck Lever 	int		status;
16781da177e4SLinus Torvalds 
1679a0584ee9SChuck Lever 	p = xdr_inline_decode(xdr, 2 * sizeof(*p));
1680a0584ee9SChuck Lever 	if (!p)
1681a0584ee9SChuck Lever 		goto validate_failed;
1682a0584ee9SChuck Lever 	if (*p++ != rpc_auth_gss)
1683a0584ee9SChuck Lever 		goto validate_failed;
1684a0584ee9SChuck Lever 	len = be32_to_cpup(p);
1685a0584ee9SChuck Lever 	if (len > RPC_MAX_AUTH_SIZE)
1686a0584ee9SChuck Lever 		goto validate_failed;
1687a0584ee9SChuck Lever 	p = xdr_inline_decode(xdr, len);
1688a0584ee9SChuck Lever 	if (!p)
1689a0584ee9SChuck Lever 		goto validate_failed;
16901da177e4SLinus Torvalds 
16912876a344SJ. Bruce Fields 	seq = kmalloc(4, GFP_NOFS);
16922876a344SJ. Bruce Fields 	if (!seq)
1693a0584ee9SChuck Lever 		goto validate_failed;
1694a0584ee9SChuck Lever 	*seq = cpu_to_be32(task->tk_rqstp->rq_seqno);
16952876a344SJ. Bruce Fields 	iov.iov_base = seq;
16962876a344SJ. Bruce Fields 	iov.iov_len = 4;
16971da177e4SLinus Torvalds 	xdr_buf_from_iov(&iov, &verf_buf);
16981da177e4SLinus Torvalds 	mic.data = (u8 *)p;
16991da177e4SLinus Torvalds 	mic.len = len;
170000fd6e14SJ. Bruce Fields 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
17011da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1702fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
1703a0584ee9SChuck Lever 	if (maj_stat)
1704a0584ee9SChuck Lever 		goto bad_mic;
1705a0584ee9SChuck Lever 
170624b2605bSJ. Bruce Fields 	/* We leave it to unwrap to calculate au_rslack. For now we just
170724b2605bSJ. Bruce Fields 	 * calculate the length of the verifier: */
17081be27f36STrond Myklebust 	cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
1709a0584ee9SChuck Lever 	status = 0;
1710a0584ee9SChuck Lever out:
17111da177e4SLinus Torvalds 	gss_put_ctx(ctx);
17122876a344SJ. Bruce Fields 	kfree(seq);
1713a0584ee9SChuck Lever 	return status;
1714a0584ee9SChuck Lever 
1715a0584ee9SChuck Lever validate_failed:
1716a0584ee9SChuck Lever 	status = -EIO;
1717a0584ee9SChuck Lever 	goto out;
1718a0584ee9SChuck Lever bad_mic:
17190c77668dSChuck Lever 	trace_rpcgss_verify_mic(task, maj_stat);
1720a0584ee9SChuck Lever 	status = -EACCES;
1721a0584ee9SChuck Lever 	goto out;
17221da177e4SLinus Torvalds }
17231da177e4SLinus Torvalds 
1724e8680a24SChuck Lever static int gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1725e8680a24SChuck Lever 			      struct rpc_task *task, struct xdr_stream *xdr)
17269f06c719SChuck Lever {
1727e8680a24SChuck Lever 	struct rpc_rqst *rqstp = task->tk_rqstp;
1728e8680a24SChuck Lever 	struct xdr_buf integ_buf, *snd_buf = &rqstp->rq_snd_buf;
17291da177e4SLinus Torvalds 	struct xdr_netobj mic;
1730e8680a24SChuck Lever 	__be32 *p, *integ_len;
1731e8680a24SChuck Lever 	u32 offset, maj_stat;
17321da177e4SLinus Torvalds 
1733e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, 2 * sizeof(*p));
1734e8680a24SChuck Lever 	if (!p)
1735e8680a24SChuck Lever 		goto wrap_failed;
17361da177e4SLinus Torvalds 	integ_len = p++;
1737e8680a24SChuck Lever 	*p = cpu_to_be32(rqstp->rq_seqno);
1738e8680a24SChuck Lever 
1739e8680a24SChuck Lever 	if (rpcauth_wrap_req_encode(task, xdr))
1740e8680a24SChuck Lever 		goto wrap_failed;
1741e8680a24SChuck Lever 
17421da177e4SLinus Torvalds 	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
17431da177e4SLinus Torvalds 	if (xdr_buf_subsegment(snd_buf, &integ_buf,
17441da177e4SLinus Torvalds 				offset, snd_buf->len - offset))
1745e8680a24SChuck Lever 		goto wrap_failed;
1746e8680a24SChuck Lever 	*integ_len = cpu_to_be32(integ_buf.len);
17471da177e4SLinus Torvalds 
1748e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, 0);
1749e8680a24SChuck Lever 	if (!p)
1750e8680a24SChuck Lever 		goto wrap_failed;
17511da177e4SLinus Torvalds 	mic.data = (u8 *)(p + 1);
175200fd6e14SJ. Bruce Fields 	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
17531da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1754fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
17551da177e4SLinus Torvalds 	else if (maj_stat)
17560c77668dSChuck Lever 		goto bad_mic;
1757e8680a24SChuck Lever 	/* Check that the trailing MIC fit in the buffer, after the fact */
1758e8680a24SChuck Lever 	if (xdr_stream_encode_opaque_inline(xdr, (void **)&p, mic.len) < 0)
1759e8680a24SChuck Lever 		goto wrap_failed;
17601da177e4SLinus Torvalds 	return 0;
1761e8680a24SChuck Lever wrap_failed:
1762e8680a24SChuck Lever 	return -EMSGSIZE;
17630c77668dSChuck Lever bad_mic:
17640c77668dSChuck Lever 	trace_rpcgss_get_mic(task, maj_stat);
17650c77668dSChuck Lever 	return -EIO;
17661da177e4SLinus Torvalds }
17671da177e4SLinus Torvalds 
17682d2da60cSJ. Bruce Fields static void
17692d2da60cSJ. Bruce Fields priv_release_snd_buf(struct rpc_rqst *rqstp)
17702d2da60cSJ. Bruce Fields {
17712d2da60cSJ. Bruce Fields 	int i;
17722d2da60cSJ. Bruce Fields 
17732d2da60cSJ. Bruce Fields 	for (i=0; i < rqstp->rq_enc_pages_num; i++)
17742d2da60cSJ. Bruce Fields 		__free_page(rqstp->rq_enc_pages[i]);
17752d2da60cSJ. Bruce Fields 	kfree(rqstp->rq_enc_pages);
17768dae5398SChuck Lever 	rqstp->rq_release_snd_buf = NULL;
17772d2da60cSJ. Bruce Fields }
17782d2da60cSJ. Bruce Fields 
17792d2da60cSJ. Bruce Fields static int
17802d2da60cSJ. Bruce Fields alloc_enc_pages(struct rpc_rqst *rqstp)
17812d2da60cSJ. Bruce Fields {
17822d2da60cSJ. Bruce Fields 	struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
17832d2da60cSJ. Bruce Fields 	int first, last, i;
17842d2da60cSJ. Bruce Fields 
17858dae5398SChuck Lever 	if (rqstp->rq_release_snd_buf)
17868dae5398SChuck Lever 		rqstp->rq_release_snd_buf(rqstp);
17878dae5398SChuck Lever 
17882d2da60cSJ. Bruce Fields 	if (snd_buf->page_len == 0) {
17892d2da60cSJ. Bruce Fields 		rqstp->rq_enc_pages_num = 0;
17902d2da60cSJ. Bruce Fields 		return 0;
17912d2da60cSJ. Bruce Fields 	}
17922d2da60cSJ. Bruce Fields 
179309cbfeafSKirill A. Shutemov 	first = snd_buf->page_base >> PAGE_SHIFT;
179409cbfeafSKirill A. Shutemov 	last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_SHIFT;
17952d2da60cSJ. Bruce Fields 	rqstp->rq_enc_pages_num = last - first + 1 + 1;
17962d2da60cSJ. Bruce Fields 	rqstp->rq_enc_pages
17976da2ec56SKees Cook 		= kmalloc_array(rqstp->rq_enc_pages_num,
17986da2ec56SKees Cook 				sizeof(struct page *),
17992d2da60cSJ. Bruce Fields 				GFP_NOFS);
18002d2da60cSJ. Bruce Fields 	if (!rqstp->rq_enc_pages)
18012d2da60cSJ. Bruce Fields 		goto out;
18022d2da60cSJ. Bruce Fields 	for (i=0; i < rqstp->rq_enc_pages_num; i++) {
18032d2da60cSJ. Bruce Fields 		rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
18042d2da60cSJ. Bruce Fields 		if (rqstp->rq_enc_pages[i] == NULL)
18052d2da60cSJ. Bruce Fields 			goto out_free;
18062d2da60cSJ. Bruce Fields 	}
18072d2da60cSJ. Bruce Fields 	rqstp->rq_release_snd_buf = priv_release_snd_buf;
18082d2da60cSJ. Bruce Fields 	return 0;
18092d2da60cSJ. Bruce Fields out_free:
1810cdead7cfSTrond Myklebust 	rqstp->rq_enc_pages_num = i;
1811cdead7cfSTrond Myklebust 	priv_release_snd_buf(rqstp);
18122d2da60cSJ. Bruce Fields out:
18132d2da60cSJ. Bruce Fields 	return -EAGAIN;
18142d2da60cSJ. Bruce Fields }
18152d2da60cSJ. Bruce Fields 
1816e8680a24SChuck Lever static int gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1817e8680a24SChuck Lever 			     struct rpc_task *task, struct xdr_stream *xdr)
18182d2da60cSJ. Bruce Fields {
1819e8680a24SChuck Lever 	struct rpc_rqst *rqstp = task->tk_rqstp;
18202d2da60cSJ. Bruce Fields 	struct xdr_buf	*snd_buf = &rqstp->rq_snd_buf;
1821e8680a24SChuck Lever 	u32		pad, offset, maj_stat;
18222d2da60cSJ. Bruce Fields 	int		status;
1823e8680a24SChuck Lever 	__be32		*p, *opaque_len;
18242d2da60cSJ. Bruce Fields 	struct page	**inpages;
18252d2da60cSJ. Bruce Fields 	int		first;
18262d2da60cSJ. Bruce Fields 	struct kvec	*iov;
18272d2da60cSJ. Bruce Fields 
1828e8680a24SChuck Lever 	status = -EIO;
1829e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, 2 * sizeof(*p));
1830e8680a24SChuck Lever 	if (!p)
1831e8680a24SChuck Lever 		goto wrap_failed;
18322d2da60cSJ. Bruce Fields 	opaque_len = p++;
1833e8680a24SChuck Lever 	*p = cpu_to_be32(rqstp->rq_seqno);
18342d2da60cSJ. Bruce Fields 
1835e8680a24SChuck Lever 	if (rpcauth_wrap_req_encode(task, xdr))
1836e8680a24SChuck Lever 		goto wrap_failed;
18372d2da60cSJ. Bruce Fields 
18382d2da60cSJ. Bruce Fields 	status = alloc_enc_pages(rqstp);
1839e8680a24SChuck Lever 	if (unlikely(status))
1840e8680a24SChuck Lever 		goto wrap_failed;
184109cbfeafSKirill A. Shutemov 	first = snd_buf->page_base >> PAGE_SHIFT;
18422d2da60cSJ. Bruce Fields 	inpages = snd_buf->pages + first;
18432d2da60cSJ. Bruce Fields 	snd_buf->pages = rqstp->rq_enc_pages;
184409cbfeafSKirill A. Shutemov 	snd_buf->page_base -= first << PAGE_SHIFT;
18457561042fSKevin Coffman 	/*
1846e8680a24SChuck Lever 	 * Move the tail into its own page, in case gss_wrap needs
1847e8680a24SChuck Lever 	 * more space in the head when wrapping.
18487561042fSKevin Coffman 	 *
1849e8680a24SChuck Lever 	 * Still... Why can't gss_wrap just slide the tail down?
18507561042fSKevin Coffman 	 */
18512d2da60cSJ. Bruce Fields 	if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
1852e8680a24SChuck Lever 		char *tmp;
1853e8680a24SChuck Lever 
18542d2da60cSJ. Bruce Fields 		tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
18552d2da60cSJ. Bruce Fields 		memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
18562d2da60cSJ. Bruce Fields 		snd_buf->tail[0].iov_base = tmp;
18572d2da60cSJ. Bruce Fields 	}
1858e8680a24SChuck Lever 	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
185900fd6e14SJ. Bruce Fields 	maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
18607561042fSKevin Coffman 	/* slack space should prevent this ever happening: */
1861e8680a24SChuck Lever 	if (unlikely(snd_buf->len > snd_buf->buflen))
1862e8680a24SChuck Lever 		goto wrap_failed;
18632d2da60cSJ. Bruce Fields 	/* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
18642d2da60cSJ. Bruce Fields 	 * done anyway, so it's safe to put the request on the wire: */
18652d2da60cSJ. Bruce Fields 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1866fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
18672d2da60cSJ. Bruce Fields 	else if (maj_stat)
18680c77668dSChuck Lever 		goto bad_wrap;
18692d2da60cSJ. Bruce Fields 
1870e8680a24SChuck Lever 	*opaque_len = cpu_to_be32(snd_buf->len - offset);
1871e8680a24SChuck Lever 	/* guess whether the pad goes into the head or the tail: */
18722d2da60cSJ. Bruce Fields 	if (snd_buf->page_len || snd_buf->tail[0].iov_len)
18732d2da60cSJ. Bruce Fields 		iov = snd_buf->tail;
18742d2da60cSJ. Bruce Fields 	else
18752d2da60cSJ. Bruce Fields 		iov = snd_buf->head;
18762d2da60cSJ. Bruce Fields 	p = iov->iov_base + iov->iov_len;
18772d2da60cSJ. Bruce Fields 	pad = 3 - ((snd_buf->len - offset - 1) & 3);
18782d2da60cSJ. Bruce Fields 	memset(p, 0, pad);
18792d2da60cSJ. Bruce Fields 	iov->iov_len += pad;
18802d2da60cSJ. Bruce Fields 	snd_buf->len += pad;
18812d2da60cSJ. Bruce Fields 
18822d2da60cSJ. Bruce Fields 	return 0;
1883e8680a24SChuck Lever wrap_failed:
1884e8680a24SChuck Lever 	return status;
18850c77668dSChuck Lever bad_wrap:
18860c77668dSChuck Lever 	trace_rpcgss_wrap(task, maj_stat);
18870c77668dSChuck Lever 	return -EIO;
18882d2da60cSJ. Bruce Fields }
18892d2da60cSJ. Bruce Fields 
1890e8680a24SChuck Lever static int gss_wrap_req(struct rpc_task *task, struct xdr_stream *xdr)
18911da177e4SLinus Torvalds {
1892a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
18931da177e4SLinus Torvalds 	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
18941da177e4SLinus Torvalds 			gc_base);
18951da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1896e8680a24SChuck Lever 	int status;
18971da177e4SLinus Torvalds 
1898e8680a24SChuck Lever 	status = -EIO;
18991da177e4SLinus Torvalds 	if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
19001da177e4SLinus Torvalds 		/* The spec seems a little ambiguous here, but I think that not
19011da177e4SLinus Torvalds 		 * wrapping context destruction requests makes the most sense.
19021da177e4SLinus Torvalds 		 */
1903e8680a24SChuck Lever 		status = rpcauth_wrap_req_encode(task, xdr);
19041da177e4SLinus Torvalds 		goto out;
19051da177e4SLinus Torvalds 	}
19061da177e4SLinus Torvalds 	switch (gss_cred->gc_service) {
19071da177e4SLinus Torvalds 	case RPC_GSS_SVC_NONE:
1908e8680a24SChuck Lever 		status = rpcauth_wrap_req_encode(task, xdr);
19091da177e4SLinus Torvalds 		break;
19101da177e4SLinus Torvalds 	case RPC_GSS_SVC_INTEGRITY:
1911e8680a24SChuck Lever 		status = gss_wrap_req_integ(cred, ctx, task, xdr);
19121da177e4SLinus Torvalds 		break;
19131da177e4SLinus Torvalds 	case RPC_GSS_SVC_PRIVACY:
1914e8680a24SChuck Lever 		status = gss_wrap_req_priv(cred, ctx, task, xdr);
19151da177e4SLinus Torvalds 		break;
19160c77668dSChuck Lever 	default:
19170c77668dSChuck Lever 		status = -EIO;
19181da177e4SLinus Torvalds 	}
19191da177e4SLinus Torvalds out:
19201da177e4SLinus Torvalds 	gss_put_ctx(ctx);
19211da177e4SLinus Torvalds 	return status;
19221da177e4SLinus Torvalds }
19231da177e4SLinus Torvalds 
1924a0584ee9SChuck Lever static int
1925a0584ee9SChuck Lever gss_unwrap_resp_auth(struct rpc_cred *cred)
19261da177e4SLinus Torvalds {
192735e77d21SChuck Lever 	struct rpc_auth *auth = cred->cr_auth;
192835e77d21SChuck Lever 
192935e77d21SChuck Lever 	auth->au_rslack = auth->au_verfsize;
193035e77d21SChuck Lever 	auth->au_ralign = auth->au_verfsize;
1931a0584ee9SChuck Lever 	return 0;
1932a0584ee9SChuck Lever }
19331da177e4SLinus Torvalds 
1934a0584ee9SChuck Lever static int
19350c77668dSChuck Lever gss_unwrap_resp_integ(struct rpc_task *task, struct rpc_cred *cred,
19360c77668dSChuck Lever 		      struct gss_cl_ctx *ctx, struct rpc_rqst *rqstp,
19370c77668dSChuck Lever 		      struct xdr_stream *xdr)
1938a0584ee9SChuck Lever {
1939a0584ee9SChuck Lever 	struct xdr_buf integ_buf, *rcv_buf = &rqstp->rq_rcv_buf;
1940a0584ee9SChuck Lever 	u32 data_offset, mic_offset, integ_len, maj_stat;
194135e77d21SChuck Lever 	struct rpc_auth *auth = cred->cr_auth;
1942a0584ee9SChuck Lever 	struct xdr_netobj mic;
1943a0584ee9SChuck Lever 	__be32 *p;
1944a0584ee9SChuck Lever 
1945a0584ee9SChuck Lever 	p = xdr_inline_decode(xdr, 2 * sizeof(*p));
1946a0584ee9SChuck Lever 	if (unlikely(!p))
1947a0584ee9SChuck Lever 		goto unwrap_failed;
1948a0584ee9SChuck Lever 	integ_len = be32_to_cpup(p++);
19491da177e4SLinus Torvalds 	if (integ_len & 3)
1950a0584ee9SChuck Lever 		goto unwrap_failed;
1951a0584ee9SChuck Lever 	data_offset = (u8 *)(p) - (u8 *)rcv_buf->head[0].iov_base;
19521da177e4SLinus Torvalds 	mic_offset = integ_len + data_offset;
19531da177e4SLinus Torvalds 	if (mic_offset > rcv_buf->len)
1954a0584ee9SChuck Lever 		goto unwrap_failed;
1955a0584ee9SChuck Lever 	if (be32_to_cpup(p) != rqstp->rq_seqno)
19560c77668dSChuck Lever 		goto bad_seqno;
19571da177e4SLinus Torvalds 
1958a0584ee9SChuck Lever 	if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset, integ_len))
1959a0584ee9SChuck Lever 		goto unwrap_failed;
19601da177e4SLinus Torvalds 	if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
1961a0584ee9SChuck Lever 		goto unwrap_failed;
196200fd6e14SJ. Bruce Fields 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
19631da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1964fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
19651da177e4SLinus Torvalds 	if (maj_stat != GSS_S_COMPLETE)
1966a0584ee9SChuck Lever 		goto bad_mic;
1967a0584ee9SChuck Lever 
196835e77d21SChuck Lever 	auth->au_rslack = auth->au_verfsize + 2 + 1 + XDR_QUADLEN(mic.len);
196935e77d21SChuck Lever 	auth->au_ralign = auth->au_verfsize + 2;
19701da177e4SLinus Torvalds 	return 0;
1971a0584ee9SChuck Lever unwrap_failed:
19720c77668dSChuck Lever 	trace_rpcgss_unwrap_failed(task);
19730c77668dSChuck Lever 	return -EIO;
19740c77668dSChuck Lever bad_seqno:
19750c77668dSChuck Lever 	trace_rpcgss_bad_seqno(task, rqstp->rq_seqno, be32_to_cpup(p));
1976a0584ee9SChuck Lever 	return -EIO;
1977a0584ee9SChuck Lever bad_mic:
19780c77668dSChuck Lever 	trace_rpcgss_verify_mic(task, maj_stat);
1979a0584ee9SChuck Lever 	return -EIO;
19801da177e4SLinus Torvalds }
19811da177e4SLinus Torvalds 
1982a0584ee9SChuck Lever static int
19830c77668dSChuck Lever gss_unwrap_resp_priv(struct rpc_task *task, struct rpc_cred *cred,
19840c77668dSChuck Lever 		     struct gss_cl_ctx *ctx, struct rpc_rqst *rqstp,
19850c77668dSChuck Lever 		     struct xdr_stream *xdr)
19862d2da60cSJ. Bruce Fields {
19872d2da60cSJ. Bruce Fields 	struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
1988a0584ee9SChuck Lever 	struct kvec *head = rqstp->rq_rcv_buf.head;
198935e77d21SChuck Lever 	struct rpc_auth *auth = cred->cr_auth;
1990a0584ee9SChuck Lever 	unsigned int savedlen = rcv_buf->len;
1991a0584ee9SChuck Lever 	u32 offset, opaque_len, maj_stat;
1992a0584ee9SChuck Lever 	__be32 *p;
19932d2da60cSJ. Bruce Fields 
1994a0584ee9SChuck Lever 	p = xdr_inline_decode(xdr, 2 * sizeof(*p));
1995a0584ee9SChuck Lever 	if (unlikely(!p))
1996a0584ee9SChuck Lever 		goto unwrap_failed;
1997a0584ee9SChuck Lever 	opaque_len = be32_to_cpup(p++);
1998a0584ee9SChuck Lever 	offset = (u8 *)(p) - (u8 *)head->iov_base;
19992d2da60cSJ. Bruce Fields 	if (offset + opaque_len > rcv_buf->len)
2000a0584ee9SChuck Lever 		goto unwrap_failed;
20012d2da60cSJ. Bruce Fields 	rcv_buf->len = offset + opaque_len;
20022d2da60cSJ. Bruce Fields 
200300fd6e14SJ. Bruce Fields 	maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
20042d2da60cSJ. Bruce Fields 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
2005fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
20062d2da60cSJ. Bruce Fields 	if (maj_stat != GSS_S_COMPLETE)
2007a0584ee9SChuck Lever 		goto bad_unwrap;
2008a0584ee9SChuck Lever 	/* gss_unwrap decrypted the sequence number */
2009a0584ee9SChuck Lever 	if (be32_to_cpup(p++) != rqstp->rq_seqno)
20100c77668dSChuck Lever 		goto bad_seqno;
20112d2da60cSJ. Bruce Fields 
2012a0584ee9SChuck Lever 	/* gss_unwrap redacts the opaque blob from the head iovec.
2013a0584ee9SChuck Lever 	 * rcv_buf has changed, thus the stream needs to be reset.
2014a0584ee9SChuck Lever 	 */
2015a0584ee9SChuck Lever 	xdr_init_decode(xdr, rcv_buf, p, rqstp);
2016a0584ee9SChuck Lever 
201735e77d21SChuck Lever 	auth->au_rslack = auth->au_verfsize + 2 +
201835e77d21SChuck Lever 			  XDR_QUADLEN(savedlen - rcv_buf->len);
201935e77d21SChuck Lever 	auth->au_ralign = auth->au_verfsize + 2 +
2020a0584ee9SChuck Lever 			  XDR_QUADLEN(savedlen - rcv_buf->len);
20212d2da60cSJ. Bruce Fields 	return 0;
2022a0584ee9SChuck Lever unwrap_failed:
20230c77668dSChuck Lever 	trace_rpcgss_unwrap_failed(task);
20240c77668dSChuck Lever 	return -EIO;
20250c77668dSChuck Lever bad_seqno:
20260c77668dSChuck Lever 	trace_rpcgss_bad_seqno(task, rqstp->rq_seqno, be32_to_cpup(--p));
2027a0584ee9SChuck Lever 	return -EIO;
2028a0584ee9SChuck Lever bad_unwrap:
20290c77668dSChuck Lever 	trace_rpcgss_unwrap(task, maj_stat);
2030a0584ee9SChuck Lever 	return -EIO;
2031bf269551SChuck Lever }
20322d2da60cSJ. Bruce Fields 
20333021a5bbSTrond Myklebust static bool
20343021a5bbSTrond Myklebust gss_seq_is_newer(u32 new, u32 old)
20353021a5bbSTrond Myklebust {
20363021a5bbSTrond Myklebust 	return (s32)(new - old) > 0;
20373021a5bbSTrond Myklebust }
20383021a5bbSTrond Myklebust 
20393021a5bbSTrond Myklebust static bool
20403021a5bbSTrond Myklebust gss_xmit_need_reencode(struct rpc_task *task)
20413021a5bbSTrond Myklebust {
20423021a5bbSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
20433021a5bbSTrond Myklebust 	struct rpc_cred *cred = req->rq_cred;
20443021a5bbSTrond Myklebust 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
20450c77668dSChuck Lever 	u32 win, seq_xmit = 0;
20463021a5bbSTrond Myklebust 	bool ret = true;
20473021a5bbSTrond Myklebust 
20483021a5bbSTrond Myklebust 	if (!ctx)
20490c77668dSChuck Lever 		goto out;
20503021a5bbSTrond Myklebust 
20513021a5bbSTrond Myklebust 	if (gss_seq_is_newer(req->rq_seqno, READ_ONCE(ctx->gc_seq)))
20520c77668dSChuck Lever 		goto out_ctx;
20533021a5bbSTrond Myklebust 
20543021a5bbSTrond Myklebust 	seq_xmit = READ_ONCE(ctx->gc_seq_xmit);
20553021a5bbSTrond Myklebust 	while (gss_seq_is_newer(req->rq_seqno, seq_xmit)) {
20563021a5bbSTrond Myklebust 		u32 tmp = seq_xmit;
20573021a5bbSTrond Myklebust 
20583021a5bbSTrond Myklebust 		seq_xmit = cmpxchg(&ctx->gc_seq_xmit, tmp, req->rq_seqno);
20593021a5bbSTrond Myklebust 		if (seq_xmit == tmp) {
20603021a5bbSTrond Myklebust 			ret = false;
20610c77668dSChuck Lever 			goto out_ctx;
20623021a5bbSTrond Myklebust 		}
20633021a5bbSTrond Myklebust 	}
20643021a5bbSTrond Myklebust 
20653021a5bbSTrond Myklebust 	win = ctx->gc_win;
20663021a5bbSTrond Myklebust 	if (win > 0)
20673021a5bbSTrond Myklebust 		ret = !gss_seq_is_newer(req->rq_seqno, seq_xmit - win);
20680c77668dSChuck Lever 
20690c77668dSChuck Lever out_ctx:
20703021a5bbSTrond Myklebust 	gss_put_ctx(ctx);
20710c77668dSChuck Lever out:
20720c77668dSChuck Lever 	trace_rpcgss_need_reencode(task, seq_xmit, ret);
20733021a5bbSTrond Myklebust 	return ret;
20743021a5bbSTrond Myklebust }
20753021a5bbSTrond Myklebust 
20761da177e4SLinus Torvalds static int
2077a0584ee9SChuck Lever gss_unwrap_resp(struct rpc_task *task, struct xdr_stream *xdr)
20781da177e4SLinus Torvalds {
2079a0584ee9SChuck Lever 	struct rpc_rqst *rqstp = task->tk_rqstp;
2080a0584ee9SChuck Lever 	struct rpc_cred *cred = rqstp->rq_cred;
20811da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
20821da177e4SLinus Torvalds 			gc_base);
20831da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
20841da177e4SLinus Torvalds 	int status = -EIO;
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds 	if (ctx->gc_proc != RPC_GSS_PROC_DATA)
20871da177e4SLinus Torvalds 		goto out_decode;
20881da177e4SLinus Torvalds 	switch (gss_cred->gc_service) {
20891da177e4SLinus Torvalds 	case RPC_GSS_SVC_NONE:
2090a0584ee9SChuck Lever 		status = gss_unwrap_resp_auth(cred);
20911da177e4SLinus Torvalds 		break;
20921da177e4SLinus Torvalds 	case RPC_GSS_SVC_INTEGRITY:
20930c77668dSChuck Lever 		status = gss_unwrap_resp_integ(task, cred, ctx, rqstp, xdr);
20941da177e4SLinus Torvalds 		break;
20951da177e4SLinus Torvalds 	case RPC_GSS_SVC_PRIVACY:
20960c77668dSChuck Lever 		status = gss_unwrap_resp_priv(task, cred, ctx, rqstp, xdr);
20971da177e4SLinus Torvalds 		break;
20981da177e4SLinus Torvalds 	}
2099a0584ee9SChuck Lever 	if (status)
2100a0584ee9SChuck Lever 		goto out;
2101a0584ee9SChuck Lever 
21021da177e4SLinus Torvalds out_decode:
2103a0584ee9SChuck Lever 	status = rpcauth_unwrap_resp_decode(task, xdr);
21041da177e4SLinus Torvalds out:
21051da177e4SLinus Torvalds 	gss_put_ctx(ctx);
21061da177e4SLinus Torvalds 	return status;
21071da177e4SLinus Torvalds }
21081da177e4SLinus Torvalds 
2109f1c0a861STrond Myklebust static const struct rpc_authops authgss_ops = {
21101da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
21111da177e4SLinus Torvalds 	.au_flavor	= RPC_AUTH_GSS,
21121da177e4SLinus Torvalds 	.au_name	= "RPCSEC_GSS",
21131da177e4SLinus Torvalds 	.create		= gss_create,
21141da177e4SLinus Torvalds 	.destroy	= gss_destroy,
2115a960f8d6SFrank Sorenson 	.hash_cred	= gss_hash_cred,
21161da177e4SLinus Torvalds 	.lookup_cred	= gss_lookup_cred,
211780df9d20SStanislav Kinsbursky 	.crcreate	= gss_create_cred,
21186a1a1e34SChuck Lever 	.list_pseudoflavors = gss_mech_list_pseudoflavors,
21199568c5e9SChuck Lever 	.info2flavor	= gss_mech_info2flavor,
2120a77c806fSChuck Lever 	.flavor2info	= gss_mech_flavor2info,
21211da177e4SLinus Torvalds };
21221da177e4SLinus Torvalds 
2123f1c0a861STrond Myklebust static const struct rpc_credops gss_credops = {
21241da177e4SLinus Torvalds 	.cr_name		= "AUTH_GSS",
21251da177e4SLinus Torvalds 	.crdestroy		= gss_destroy_cred,
2126fba3bad4STrond Myklebust 	.cr_init		= gss_cred_init,
21271da177e4SLinus Torvalds 	.crmatch		= gss_match,
21281da177e4SLinus Torvalds 	.crmarshal		= gss_marshal,
21291da177e4SLinus Torvalds 	.crrefresh		= gss_refresh,
21301da177e4SLinus Torvalds 	.crvalidate		= gss_validate,
21311da177e4SLinus Torvalds 	.crwrap_req		= gss_wrap_req,
21321da177e4SLinus Torvalds 	.crunwrap_resp		= gss_unwrap_resp,
21334de6caa2SAndy Adamson 	.crkey_timeout		= gss_key_timeout,
2134a0337d1dSJeff Layton 	.crstringify_acceptor	= gss_stringify_acceptor,
21353021a5bbSTrond Myklebust 	.crneed_reencode	= gss_xmit_need_reencode,
21361da177e4SLinus Torvalds };
21371da177e4SLinus Torvalds 
21380df7fb74STrond Myklebust static const struct rpc_credops gss_nullops = {
21390df7fb74STrond Myklebust 	.cr_name		= "AUTH_GSS",
21406dcd3926SJeff Layton 	.crdestroy		= gss_destroy_nullcred,
21410df7fb74STrond Myklebust 	.crmatch		= gss_match,
21420df7fb74STrond Myklebust 	.crmarshal		= gss_marshal,
21430df7fb74STrond Myklebust 	.crrefresh		= gss_refresh_null,
21440df7fb74STrond Myklebust 	.crvalidate		= gss_validate,
21450df7fb74STrond Myklebust 	.crwrap_req		= gss_wrap_req,
21460df7fb74STrond Myklebust 	.crunwrap_resp		= gss_unwrap_resp,
2147a0337d1dSJeff Layton 	.crstringify_acceptor	= gss_stringify_acceptor,
21480df7fb74STrond Myklebust };
21490df7fb74STrond Myklebust 
2150b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v0 = {
2151ac83228aSTrond Myklebust 	.upcall		= gss_v0_upcall,
21521da177e4SLinus Torvalds 	.downcall	= gss_pipe_downcall,
21531da177e4SLinus Torvalds 	.destroy_msg	= gss_pipe_destroy_msg,
215434769fc4S\"J. Bruce Fields\ 	.open_pipe	= gss_pipe_open_v0,
215534769fc4S\"J. Bruce Fields\ 	.release_pipe	= gss_pipe_release,
215634769fc4S\"J. Bruce Fields\ };
215734769fc4S\"J. Bruce Fields\ 
2158b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v1 = {
2159ac83228aSTrond Myklebust 	.upcall		= gss_v1_upcall,
216034769fc4S\"J. Bruce Fields\ 	.downcall	= gss_pipe_downcall,
216134769fc4S\"J. Bruce Fields\ 	.destroy_msg	= gss_pipe_destroy_msg,
216234769fc4S\"J. Bruce Fields\ 	.open_pipe	= gss_pipe_open_v1,
21631da177e4SLinus Torvalds 	.release_pipe	= gss_pipe_release,
21641da177e4SLinus Torvalds };
21651da177e4SLinus Torvalds 
2166a1db410dSStanislav Kinsbursky static __net_init int rpcsec_gss_init_net(struct net *net)
2167a1db410dSStanislav Kinsbursky {
2168a1db410dSStanislav Kinsbursky 	return gss_svc_init_net(net);
2169a1db410dSStanislav Kinsbursky }
2170a1db410dSStanislav Kinsbursky 
2171a1db410dSStanislav Kinsbursky static __net_exit void rpcsec_gss_exit_net(struct net *net)
2172a1db410dSStanislav Kinsbursky {
2173a1db410dSStanislav Kinsbursky 	gss_svc_shutdown_net(net);
2174a1db410dSStanislav Kinsbursky }
2175a1db410dSStanislav Kinsbursky 
2176a1db410dSStanislav Kinsbursky static struct pernet_operations rpcsec_gss_net_ops = {
2177a1db410dSStanislav Kinsbursky 	.init = rpcsec_gss_init_net,
2178a1db410dSStanislav Kinsbursky 	.exit = rpcsec_gss_exit_net,
2179a1db410dSStanislav Kinsbursky };
2180a1db410dSStanislav Kinsbursky 
21811da177e4SLinus Torvalds /*
21821da177e4SLinus Torvalds  * Initialize RPCSEC_GSS module
21831da177e4SLinus Torvalds  */
21841da177e4SLinus Torvalds static int __init init_rpcsec_gss(void)
21851da177e4SLinus Torvalds {
21861da177e4SLinus Torvalds 	int err = 0;
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds 	err = rpcauth_register(&authgss_ops);
21891da177e4SLinus Torvalds 	if (err)
21901da177e4SLinus Torvalds 		goto out;
21911da177e4SLinus Torvalds 	err = gss_svc_init();
21921da177e4SLinus Torvalds 	if (err)
21931da177e4SLinus Torvalds 		goto out_unregister;
2194a1db410dSStanislav Kinsbursky 	err = register_pernet_subsys(&rpcsec_gss_net_ops);
2195a1db410dSStanislav Kinsbursky 	if (err)
2196a1db410dSStanislav Kinsbursky 		goto out_svc_exit;
219779a3f20bS\"J. Bruce Fields\ 	rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version");
21981da177e4SLinus Torvalds 	return 0;
2199a1db410dSStanislav Kinsbursky out_svc_exit:
2200a1db410dSStanislav Kinsbursky 	gss_svc_shutdown();
22011da177e4SLinus Torvalds out_unregister:
22021da177e4SLinus Torvalds 	rpcauth_unregister(&authgss_ops);
22031da177e4SLinus Torvalds out:
22041da177e4SLinus Torvalds 	return err;
22051da177e4SLinus Torvalds }
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds static void __exit exit_rpcsec_gss(void)
22081da177e4SLinus Torvalds {
2209a1db410dSStanislav Kinsbursky 	unregister_pernet_subsys(&rpcsec_gss_net_ops);
22101da177e4SLinus Torvalds 	gss_svc_shutdown();
22111da177e4SLinus Torvalds 	rpcauth_unregister(&authgss_ops);
2212bf12691dSJesper Dangaard Brouer 	rcu_barrier(); /* Wait for completion of call_rcu()'s */
22131da177e4SLinus Torvalds }
22141da177e4SLinus Torvalds 
221571afa85eSChuck Lever MODULE_ALIAS("rpc-auth-6");
22161da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2217126e216aSTrond Myklebust module_param_named(expired_cred_retry_delay,
2218126e216aSTrond Myklebust 		   gss_expired_cred_retry_delay,
2219126e216aSTrond Myklebust 		   uint, 0644);
2220126e216aSTrond Myklebust MODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until "
2221126e216aSTrond Myklebust 		"the RPC engine retries an expired credential");
2222126e216aSTrond Myklebust 
22234de6caa2SAndy Adamson module_param_named(key_expire_timeo,
22244de6caa2SAndy Adamson 		   gss_key_expire_timeo,
22254de6caa2SAndy Adamson 		   uint, 0644);
22264de6caa2SAndy Adamson MODULE_PARM_DESC(key_expire_timeo, "Time (in seconds) at the end of a "
22274de6caa2SAndy Adamson 		"credential keys lifetime where the NFS layer cleans up "
22284de6caa2SAndy Adamson 		"prior to key expiration");
22294de6caa2SAndy Adamson 
22301da177e4SLinus Torvalds module_init(init_rpcsec_gss)
22311da177e4SLinus Torvalds module_exit(exit_rpcsec_gss)
2232