xref: /openbmc/linux/net/sunrpc/auth_gss/auth_gss.c (revision a7e429a6)
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>
23df513a77SOlga Kornievskaia #include <linux/sunrpc/gss_krb5.h>
241da177e4SLinus Torvalds #include <linux/sunrpc/svcauth_gss.h>
251da177e4SLinus Torvalds #include <linux/sunrpc/gss_err.h>
261da177e4SLinus Torvalds #include <linux/workqueue.h>
271da177e4SLinus Torvalds #include <linux/sunrpc/rpc_pipe_fs.h>
281da177e4SLinus Torvalds #include <linux/sunrpc/gss_api.h>
297c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
30eb6dc19dSTrond Myklebust #include <linux/hashtable.h>
311da177e4SLinus Torvalds 
32abfdbd53STrond Myklebust #include "../netns.h"
33abfdbd53STrond Myklebust 
340c77668dSChuck Lever #include <trace/events/rpcgss.h>
350c77668dSChuck Lever 
36f1c0a861STrond Myklebust static const struct rpc_authops authgss_ops;
371da177e4SLinus Torvalds 
38f1c0a861STrond Myklebust static const struct rpc_credops gss_credops;
390df7fb74STrond Myklebust static const struct rpc_credops gss_nullops;
401da177e4SLinus Torvalds 
41126e216aSTrond Myklebust #define GSS_RETRY_EXPIRED 5
42126e216aSTrond Myklebust static unsigned int gss_expired_cred_retry_delay = GSS_RETRY_EXPIRED;
43126e216aSTrond Myklebust 
444de6caa2SAndy Adamson #define GSS_KEY_EXPIRE_TIMEO 240
454de6caa2SAndy Adamson static unsigned int gss_key_expire_timeo = GSS_KEY_EXPIRE_TIMEO;
464de6caa2SAndy Adamson 
47f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
481da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_AUTH
491da177e4SLinus Torvalds #endif
501da177e4SLinus Torvalds 
51725f2865SKevin Coffman #define GSS_CRED_SLACK		(RPC_MAX_AUTH_SIZE * 2)
521da177e4SLinus Torvalds /* length of a krb5 verifier (48), plus data added before arguments when
531da177e4SLinus Torvalds  * using integrity (two 4-byte integers): */
54adeb8133SOlga Kornievskaia #define GSS_VERF_SLACK		100
551da177e4SLinus Torvalds 
5623c323afSTrond Myklebust static DEFINE_HASHTABLE(gss_auth_hash_table, 4);
57eb6dc19dSTrond Myklebust static DEFINE_SPINLOCK(gss_auth_hash_lock);
58eb6dc19dSTrond Myklebust 
5919172284STrond Myklebust struct gss_pipe {
6019172284STrond Myklebust 	struct rpc_pipe_dir_object pdo;
6119172284STrond Myklebust 	struct rpc_pipe *pipe;
6219172284STrond Myklebust 	struct rpc_clnt *clnt;
6319172284STrond Myklebust 	const char *name;
64414a6295STrond Myklebust 	struct kref kref;
6519172284STrond Myklebust };
6619172284STrond Myklebust 
671da177e4SLinus Torvalds struct gss_auth {
680285ed1fSTrond Myklebust 	struct kref kref;
69eb6dc19dSTrond Myklebust 	struct hlist_node hash;
701da177e4SLinus Torvalds 	struct rpc_auth rpc_auth;
711da177e4SLinus Torvalds 	struct gss_api_mech *mech;
721da177e4SLinus Torvalds 	enum rpc_gss_svc service;
731da177e4SLinus Torvalds 	struct rpc_clnt *client;
74e726340aSTrond Myklebust 	struct net *net;
7534769fc4S\"J. Bruce Fields\ 	/*
7634769fc4S\"J. Bruce Fields\ 	 * There are two upcall pipes; dentry[1], named "gssd", is used
7734769fc4S\"J. Bruce Fields\ 	 * for the new text-based upcall; dentry[0] is named after the
7834769fc4S\"J. Bruce Fields\ 	 * mechanism (for example, "krb5") and exists for
7934769fc4S\"J. Bruce Fields\ 	 * backwards-compatibility with older gssd's.
8034769fc4S\"J. Bruce Fields\ 	 */
8119172284STrond Myklebust 	struct gss_pipe *gss_pipe[2];
82bd4a3eb1STrond Myklebust 	const char *target_name;
831da177e4SLinus Torvalds };
841da177e4SLinus Torvalds 
8579a3f20bS\"J. Bruce Fields\ /* pipe_version >= 0 if and only if someone has a pipe open. */
8679a3f20bS\"J. Bruce Fields\ static DEFINE_SPINLOCK(pipe_version_lock);
8779a3f20bS\"J. Bruce Fields\ static struct rpc_wait_queue pipe_version_rpc_waitqueue;
8879a3f20bS\"J. Bruce Fields\ static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue);
899eb2ddb4STrond Myklebust static void gss_put_auth(struct gss_auth *gss_auth);
90cf81939dS\"J. Bruce Fields\ 
915d28dc82STrond Myklebust static void gss_free_ctx(struct gss_cl_ctx *);
92b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v0;
93b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v1;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds static inline struct gss_cl_ctx *
961da177e4SLinus Torvalds gss_get_ctx(struct gss_cl_ctx *ctx)
971da177e4SLinus Torvalds {
980fa10472SReshetova, Elena 	refcount_inc(&ctx->count);
991da177e4SLinus Torvalds 	return ctx;
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds static inline void
1031da177e4SLinus Torvalds gss_put_ctx(struct gss_cl_ctx *ctx)
1041da177e4SLinus Torvalds {
1050fa10472SReshetova, Elena 	if (refcount_dec_and_test(&ctx->count))
1065d28dc82STrond Myklebust 		gss_free_ctx(ctx);
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds 
1095d28dc82STrond Myklebust /* gss_cred_set_ctx:
1105d28dc82STrond Myklebust  * called by gss_upcall_callback and gss_create_upcall in order
1115d28dc82STrond Myklebust  * to set the gss context. The actual exchange of an old context
1129beae467SStanislav Kinsbursky  * and a new one is protected by the pipe->lock.
1135d28dc82STrond Myklebust  */
1141da177e4SLinus Torvalds static void
1151da177e4SLinus Torvalds gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1185d28dc82STrond Myklebust 
119cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
120cd019f75STrond Myklebust 		return;
1217b6962b0STrond Myklebust 	gss_get_ctx(ctx);
122cf778b00SEric Dumazet 	rcu_assign_pointer(gss_cred->gc_ctx, ctx);
123fc432dd9STrond Myklebust 	set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
1244e857c58SPeter Zijlstra 	smp_mb__before_atomic();
125fc432dd9STrond Myklebust 	clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds static const void *
1291da177e4SLinus Torvalds simple_get_bytes(const void *p, const void *end, void *res, size_t len)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	const void *q = (const void *)((const char *)p + len);
1321da177e4SLinus Torvalds 	if (unlikely(q > end || q < p))
1331da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
1341da177e4SLinus Torvalds 	memcpy(res, p, len);
1351da177e4SLinus Torvalds 	return q;
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds static inline const void *
1391da177e4SLinus Torvalds simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	const void *q;
1421da177e4SLinus Torvalds 	unsigned int len;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &len, sizeof(len));
1451da177e4SLinus Torvalds 	if (IS_ERR(p))
1461da177e4SLinus Torvalds 		return p;
1471da177e4SLinus Torvalds 	q = (const void *)((const char *)p + len);
1481da177e4SLinus Torvalds 	if (unlikely(q > end || q < p))
1491da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
1500f38b873STrond Myklebust 	dest->data = kmemdup(p, len, GFP_NOFS);
1511da177e4SLinus Torvalds 	if (unlikely(dest->data == NULL))
1521da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
1531da177e4SLinus Torvalds 	dest->len = len;
1541da177e4SLinus Torvalds 	return q;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds static struct gss_cl_ctx *
1581da177e4SLinus Torvalds gss_cred_get_ctx(struct rpc_cred *cred)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1611da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = NULL;
1621da177e4SLinus Torvalds 
1635d28dc82STrond Myklebust 	rcu_read_lock();
164c5e6aecdSJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
165c5e6aecdSJeff Layton 	if (ctx)
166c5e6aecdSJeff Layton 		gss_get_ctx(ctx);
1675d28dc82STrond Myklebust 	rcu_read_unlock();
1681da177e4SLinus Torvalds 	return ctx;
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds static struct gss_cl_ctx *
1721da177e4SLinus Torvalds gss_alloc_context(void)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
1751da177e4SLinus Torvalds 
1760f38b873STrond Myklebust 	ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
1771da177e4SLinus Torvalds 	if (ctx != NULL) {
1781da177e4SLinus Torvalds 		ctx->gc_proc = RPC_GSS_PROC_DATA;
1791da177e4SLinus Torvalds 		ctx->gc_seq = 1;	/* NetApp 6.4R1 doesn't accept seq. no. 0 */
1801da177e4SLinus Torvalds 		spin_lock_init(&ctx->gc_seq_lock);
1810fa10472SReshetova, Elena 		refcount_set(&ctx->count,1);
1821da177e4SLinus Torvalds 	}
1831da177e4SLinus Torvalds 	return ctx;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds #define GSSD_MIN_TIMEOUT (60 * 60)
1871da177e4SLinus Torvalds static const void *
1881da177e4SLinus Torvalds gss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	const void *q;
1911da177e4SLinus Torvalds 	unsigned int seclen;
1921da177e4SLinus Torvalds 	unsigned int timeout;
193620038f6SAndy Adamson 	unsigned long now = jiffies;
1941da177e4SLinus Torvalds 	u32 window_size;
1951da177e4SLinus Torvalds 	int ret;
1961da177e4SLinus Torvalds 
197620038f6SAndy Adamson 	/* First unsigned int gives the remaining lifetime in seconds of the
198620038f6SAndy Adamson 	 * credential - e.g. the remaining TGT lifetime for Kerberos or
199620038f6SAndy Adamson 	 * the -t value passed to GSSD.
200620038f6SAndy Adamson 	 */
2011da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
2021da177e4SLinus Torvalds 	if (IS_ERR(p))
2031da177e4SLinus Torvalds 		goto err;
2041da177e4SLinus Torvalds 	if (timeout == 0)
2051da177e4SLinus Torvalds 		timeout = GSSD_MIN_TIMEOUT;
206620038f6SAndy Adamson 	ctx->gc_expiry = now + ((unsigned long)timeout * HZ);
207620038f6SAndy Adamson 	/* Sequence number window. Determines the maximum number of
208620038f6SAndy Adamson 	 * simultaneous requests
209620038f6SAndy Adamson 	 */
2101da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
2111da177e4SLinus Torvalds 	if (IS_ERR(p))
2121da177e4SLinus Torvalds 		goto err;
2131da177e4SLinus Torvalds 	ctx->gc_win = window_size;
2141da177e4SLinus Torvalds 	/* gssd signals an error by passing ctx->gc_win = 0: */
2151da177e4SLinus Torvalds 	if (ctx->gc_win == 0) {
216dc5ddce9SJeff Layton 		/*
217dc5ddce9SJeff Layton 		 * in which case, p points to an error code. Anything other
218dc5ddce9SJeff Layton 		 * than -EKEYEXPIRED gets converted to -EACCES.
219dc5ddce9SJeff Layton 		 */
220dc5ddce9SJeff Layton 		p = simple_get_bytes(p, end, &ret, sizeof(ret));
221dc5ddce9SJeff Layton 		if (!IS_ERR(p))
222dc5ddce9SJeff Layton 			p = (ret == -EKEYEXPIRED) ? ERR_PTR(-EKEYEXPIRED) :
223dc5ddce9SJeff Layton 						    ERR_PTR(-EACCES);
2241da177e4SLinus Torvalds 		goto err;
2251da177e4SLinus Torvalds 	}
2261da177e4SLinus Torvalds 	/* copy the opaque wire context */
2271da177e4SLinus Torvalds 	p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
2281da177e4SLinus Torvalds 	if (IS_ERR(p))
2291da177e4SLinus Torvalds 		goto err;
2301da177e4SLinus Torvalds 	/* import the opaque security context */
2311da177e4SLinus Torvalds 	p  = simple_get_bytes(p, end, &seclen, sizeof(seclen));
2321da177e4SLinus Torvalds 	if (IS_ERR(p))
2331da177e4SLinus Torvalds 		goto err;
2341da177e4SLinus Torvalds 	q = (const void *)((const char *)p + seclen);
2351da177e4SLinus Torvalds 	if (unlikely(q > end || q < p)) {
2361da177e4SLinus Torvalds 		p = ERR_PTR(-EFAULT);
2371da177e4SLinus Torvalds 		goto err;
2381da177e4SLinus Torvalds 	}
239400f26b5SSimo Sorce 	ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx, NULL, GFP_NOFS);
2401da177e4SLinus Torvalds 	if (ret < 0) {
2410c77668dSChuck Lever 		trace_rpcgss_import_ctx(ret);
2421da177e4SLinus Torvalds 		p = ERR_PTR(ret);
2431da177e4SLinus Torvalds 		goto err;
2441da177e4SLinus Torvalds 	}
2452004c726SJeff Layton 
2462004c726SJeff Layton 	/* is there any trailing data? */
2472004c726SJeff Layton 	if (q == end) {
2482004c726SJeff Layton 		p = q;
2492004c726SJeff Layton 		goto done;
2502004c726SJeff Layton 	}
2512004c726SJeff Layton 
2522004c726SJeff Layton 	/* pull in acceptor name (if there is one) */
2532004c726SJeff Layton 	p = simple_get_netobj(q, end, &ctx->gc_acceptor);
2542004c726SJeff Layton 	if (IS_ERR(p))
2552004c726SJeff Layton 		goto err;
2562004c726SJeff Layton done:
2570c77668dSChuck Lever 	trace_rpcgss_context(ctx->gc_expiry, now, timeout,
2580c77668dSChuck Lever 			     ctx->gc_acceptor.len, ctx->gc_acceptor.data);
2591da177e4SLinus Torvalds err:
2601da177e4SLinus Torvalds 	return p;
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds 
263a1a23777SChuck Lever /* XXX: Need some documentation about why UPCALL_BUF_LEN is so small.
264a1a23777SChuck Lever  *	Is user space expecting no more than UPCALL_BUF_LEN bytes?
265a1a23777SChuck Lever  *	Note that there are now _two_ NI_MAXHOST sized data items
266a1a23777SChuck Lever  *	being passed in this string.
267a1a23777SChuck Lever  */
268a1a23777SChuck Lever #define UPCALL_BUF_LEN	256
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds struct gss_upcall_msg {
2717ff13969SReshetova, Elena 	refcount_t count;
2727eaf040bSEric W. Biederman 	kuid_t	uid;
273ac83228aSTrond Myklebust 	const char *service_name;
2741da177e4SLinus Torvalds 	struct rpc_pipe_msg msg;
2751da177e4SLinus Torvalds 	struct list_head list;
2761da177e4SLinus Torvalds 	struct gss_auth *auth;
2779beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
2781da177e4SLinus Torvalds 	struct rpc_wait_queue rpc_waitqueue;
2791da177e4SLinus Torvalds 	wait_queue_head_t waitqueue;
2801da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
28134769fc4S\"J. Bruce Fields\ 	char databuf[UPCALL_BUF_LEN];
2821da177e4SLinus Torvalds };
2831da177e4SLinus Torvalds 
2842aed8b47STrond Myklebust static int get_pipe_version(struct net *net)
28579a3f20bS\"J. Bruce Fields\ {
2862aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
28779a3f20bS\"J. Bruce Fields\ 	int ret;
28879a3f20bS\"J. Bruce Fields\ 
28979a3f20bS\"J. Bruce Fields\ 	spin_lock(&pipe_version_lock);
2902aed8b47STrond Myklebust 	if (sn->pipe_version >= 0) {
2912aed8b47STrond Myklebust 		atomic_inc(&sn->pipe_users);
2922aed8b47STrond Myklebust 		ret = sn->pipe_version;
29379a3f20bS\"J. Bruce Fields\ 	} else
29479a3f20bS\"J. Bruce Fields\ 		ret = -EAGAIN;
29579a3f20bS\"J. Bruce Fields\ 	spin_unlock(&pipe_version_lock);
29679a3f20bS\"J. Bruce Fields\ 	return ret;
29779a3f20bS\"J. Bruce Fields\ }
29879a3f20bS\"J. Bruce Fields\ 
2992aed8b47STrond Myklebust static void put_pipe_version(struct net *net)
30079a3f20bS\"J. Bruce Fields\ {
3012aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
3022aed8b47STrond Myklebust 
3032aed8b47STrond Myklebust 	if (atomic_dec_and_lock(&sn->pipe_users, &pipe_version_lock)) {
3042aed8b47STrond Myklebust 		sn->pipe_version = -1;
30579a3f20bS\"J. Bruce Fields\ 		spin_unlock(&pipe_version_lock);
30679a3f20bS\"J. Bruce Fields\ 	}
30779a3f20bS\"J. Bruce Fields\ }
30879a3f20bS\"J. Bruce Fields\ 
3091da177e4SLinus Torvalds static void
3101da177e4SLinus Torvalds gss_release_msg(struct gss_upcall_msg *gss_msg)
3111da177e4SLinus Torvalds {
312e726340aSTrond Myklebust 	struct net *net = gss_msg->auth->net;
3137ff13969SReshetova, Elena 	if (!refcount_dec_and_test(&gss_msg->count))
3141da177e4SLinus Torvalds 		return;
3152aed8b47STrond Myklebust 	put_pipe_version(net);
3161da177e4SLinus Torvalds 	BUG_ON(!list_empty(&gss_msg->list));
3171da177e4SLinus Torvalds 	if (gss_msg->ctx != NULL)
3181da177e4SLinus Torvalds 		gss_put_ctx(gss_msg->ctx);
319f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue);
3209eb2ddb4STrond Myklebust 	gss_put_auth(gss_msg->auth);
321ac83228aSTrond Myklebust 	kfree_const(gss_msg->service_name);
3221da177e4SLinus Torvalds 	kfree(gss_msg);
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds static struct gss_upcall_msg *
3269130b8dbSOlga Kornievskaia __gss_find_upcall(struct rpc_pipe *pipe, kuid_t uid, const struct gss_auth *auth)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	struct gss_upcall_msg *pos;
3299beae467SStanislav Kinsbursky 	list_for_each_entry(pos, &pipe->in_downcall, list) {
3300b4d51b0SEric W. Biederman 		if (!uid_eq(pos->uid, uid))
3311da177e4SLinus Torvalds 			continue;
3329130b8dbSOlga Kornievskaia 		if (auth && pos->auth->service != auth->service)
3339130b8dbSOlga Kornievskaia 			continue;
3347ff13969SReshetova, Elena 		refcount_inc(&pos->count);
3351da177e4SLinus Torvalds 		return pos;
3361da177e4SLinus Torvalds 	}
3371da177e4SLinus Torvalds 	return NULL;
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
340720b8f2dS\\\"J. Bruce Fields\\\ /* Try to add an upcall to the pipefs queue.
3411da177e4SLinus Torvalds  * If an upcall owned by our uid already exists, then we return a reference
3421da177e4SLinus Torvalds  * to that upcall instead of adding the new upcall.
3431da177e4SLinus Torvalds  */
3441da177e4SLinus Torvalds static inline struct gss_upcall_msg *
345053e324fSSuresh Jayaraman gss_add_msg(struct gss_upcall_msg *gss_msg)
3461da177e4SLinus Torvalds {
3479beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
3481da177e4SLinus Torvalds 	struct gss_upcall_msg *old;
3491da177e4SLinus Torvalds 
3509beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
3519130b8dbSOlga Kornievskaia 	old = __gss_find_upcall(pipe, gss_msg->uid, gss_msg->auth);
3521da177e4SLinus Torvalds 	if (old == NULL) {
3537ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
3549beae467SStanislav Kinsbursky 		list_add(&gss_msg->list, &pipe->in_downcall);
3551da177e4SLinus Torvalds 	} else
3561da177e4SLinus Torvalds 		gss_msg = old;
3579beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
3581da177e4SLinus Torvalds 	return gss_msg;
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds static void
3621da177e4SLinus Torvalds __gss_unhash_msg(struct gss_upcall_msg *gss_msg)
3631da177e4SLinus Torvalds {
3641da177e4SLinus Torvalds 	list_del_init(&gss_msg->list);
3651da177e4SLinus Torvalds 	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
3661da177e4SLinus Torvalds 	wake_up_all(&gss_msg->waitqueue);
3677ff13969SReshetova, Elena 	refcount_dec(&gss_msg->count);
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds static void
3711da177e4SLinus Torvalds gss_unhash_msg(struct gss_upcall_msg *gss_msg)
3721da177e4SLinus Torvalds {
3739beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
3741da177e4SLinus Torvalds 
3753b68aaeaSTrond Myklebust 	if (list_empty(&gss_msg->list))
3763b68aaeaSTrond Myklebust 		return;
3779beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
3783b68aaeaSTrond Myklebust 	if (!list_empty(&gss_msg->list))
3791da177e4SLinus Torvalds 		__gss_unhash_msg(gss_msg);
3809beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
3811da177e4SLinus Torvalds }
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds static void
384126e216aSTrond Myklebust gss_handle_downcall_result(struct gss_cred *gss_cred, struct gss_upcall_msg *gss_msg)
385126e216aSTrond Myklebust {
386126e216aSTrond Myklebust 	switch (gss_msg->msg.errno) {
387126e216aSTrond Myklebust 	case 0:
388126e216aSTrond Myklebust 		if (gss_msg->ctx == NULL)
389126e216aSTrond Myklebust 			break;
390126e216aSTrond Myklebust 		clear_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
391126e216aSTrond Myklebust 		gss_cred_set_ctx(&gss_cred->gc_base, gss_msg->ctx);
392126e216aSTrond Myklebust 		break;
393126e216aSTrond Myklebust 	case -EKEYEXPIRED:
394126e216aSTrond Myklebust 		set_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
395126e216aSTrond Myklebust 	}
396126e216aSTrond Myklebust 	gss_cred->gc_upcall_timestamp = jiffies;
397126e216aSTrond Myklebust 	gss_cred->gc_upcall = NULL;
398126e216aSTrond Myklebust 	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
399126e216aSTrond Myklebust }
400126e216aSTrond Myklebust 
401126e216aSTrond Myklebust static void
4021da177e4SLinus Torvalds gss_upcall_callback(struct rpc_task *task)
4031da177e4SLinus Torvalds {
404a17c2153STrond Myklebust 	struct gss_cred *gss_cred = container_of(task->tk_rqstp->rq_cred,
4051da177e4SLinus Torvalds 			struct gss_cred, gc_base);
4061da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
4079beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
4081da177e4SLinus Torvalds 
4099beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
410126e216aSTrond Myklebust 	gss_handle_downcall_result(gss_cred, gss_msg);
4119beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
412126e216aSTrond Myklebust 	task->tk_status = gss_msg->msg.errno;
4131da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
416ac83228aSTrond Myklebust static void gss_encode_v0_msg(struct gss_upcall_msg *gss_msg,
417ac83228aSTrond Myklebust 			      const struct cred *cred)
41834769fc4S\"J. Bruce Fields\ {
419ac83228aSTrond Myklebust 	struct user_namespace *userns = cred->user_ns;
420283ebe3eSTrond Myklebust 
421283ebe3eSTrond Myklebust 	uid_t uid = from_kuid_munged(userns, gss_msg->uid);
42290602c7bSEric W. Biederman 	memcpy(gss_msg->databuf, &uid, sizeof(uid));
42390602c7bSEric W. Biederman 	gss_msg->msg.data = gss_msg->databuf;
42490602c7bSEric W. Biederman 	gss_msg->msg.len = sizeof(uid);
4259d3a2260STrond Myklebust 
4269d3a2260STrond Myklebust 	BUILD_BUG_ON(sizeof(uid) > sizeof(gss_msg->databuf));
42734769fc4S\"J. Bruce Fields\ }
42834769fc4S\"J. Bruce Fields\ 
429ac83228aSTrond Myklebust static ssize_t
430ac83228aSTrond Myklebust gss_v0_upcall(struct file *file, struct rpc_pipe_msg *msg,
431ac83228aSTrond Myklebust 		char __user *buf, size_t buflen)
432ac83228aSTrond Myklebust {
433ac83228aSTrond Myklebust 	struct gss_upcall_msg *gss_msg = container_of(msg,
434ac83228aSTrond Myklebust 						      struct gss_upcall_msg,
435ac83228aSTrond Myklebust 						      msg);
436ac83228aSTrond Myklebust 	if (msg->copied == 0)
437ac83228aSTrond Myklebust 		gss_encode_v0_msg(gss_msg, file->f_cred);
438ac83228aSTrond Myklebust 	return rpc_pipe_generic_upcall(file, msg, buf, buflen);
439ac83228aSTrond Myklebust }
440ac83228aSTrond Myklebust 
4419d3a2260STrond Myklebust static int gss_encode_v1_msg(struct gss_upcall_msg *gss_msg,
442bd4a3eb1STrond Myklebust 				const char *service_name,
443ac83228aSTrond Myklebust 				const char *target_name,
444ac83228aSTrond Myklebust 				const struct cred *cred)
44534769fc4S\"J. Bruce Fields\ {
446ac83228aSTrond Myklebust 	struct user_namespace *userns = cred->user_ns;
447683ac665STrond Myklebust 	struct gss_api_mech *mech = gss_msg->auth->mech;
4488b1c7bf5SOlga Kornievskaia 	char *p = gss_msg->databuf;
4499d3a2260STrond Myklebust 	size_t buflen = sizeof(gss_msg->databuf);
4509d3a2260STrond Myklebust 	int len;
4518b1c7bf5SOlga Kornievskaia 
4529d3a2260STrond Myklebust 	len = scnprintf(p, buflen, "mech=%s uid=%d", mech->gm_name,
453283ebe3eSTrond Myklebust 			from_kuid_munged(userns, gss_msg->uid));
4549d3a2260STrond Myklebust 	buflen -= len;
4559d3a2260STrond Myklebust 	p += len;
4569d3a2260STrond Myklebust 	gss_msg->msg.len = len;
457108b833cSChuck Lever 
458108b833cSChuck Lever 	/*
459108b833cSChuck Lever 	 * target= is a full service principal that names the remote
460108b833cSChuck Lever 	 * identity that we are authenticating to.
461108b833cSChuck Lever 	 */
462bd4a3eb1STrond Myklebust 	if (target_name) {
4639d3a2260STrond Myklebust 		len = scnprintf(p, buflen, " target=%s", target_name);
4649d3a2260STrond Myklebust 		buflen -= len;
4658b1c7bf5SOlga Kornievskaia 		p += len;
4668b1c7bf5SOlga Kornievskaia 		gss_msg->msg.len += len;
4678b1c7bf5SOlga Kornievskaia 	}
468108b833cSChuck Lever 
469108b833cSChuck Lever 	/*
470108b833cSChuck Lever 	 * gssd uses service= and srchost= to select a matching key from
471108b833cSChuck Lever 	 * the system's keytab to use as the source principal.
472108b833cSChuck Lever 	 *
473108b833cSChuck Lever 	 * service= is the service name part of the source principal,
474108b833cSChuck Lever 	 * or "*" (meaning choose any).
475108b833cSChuck Lever 	 *
476108b833cSChuck Lever 	 * srchost= is the hostname part of the source principal. When
477108b833cSChuck Lever 	 * not provided, gssd uses the local hostname.
478108b833cSChuck Lever 	 */
479a1a23777SChuck Lever 	if (service_name) {
480a1a23777SChuck Lever 		char *c = strchr(service_name, '@');
481a1a23777SChuck Lever 
482a1a23777SChuck Lever 		if (!c)
483a1a23777SChuck Lever 			len = scnprintf(p, buflen, " service=%s",
484a1a23777SChuck Lever 					service_name);
485a1a23777SChuck Lever 		else
486a1a23777SChuck Lever 			len = scnprintf(p, buflen,
487a1a23777SChuck Lever 					" service=%.*s srchost=%s",
488a1a23777SChuck Lever 					(int)(c - service_name),
489a1a23777SChuck Lever 					service_name, c + 1);
4909d3a2260STrond Myklebust 		buflen -= len;
4912efef708SOlga Kornievskaia 		p += len;
4922efef708SOlga Kornievskaia 		gss_msg->msg.len += len;
4932efef708SOlga Kornievskaia 	}
494108b833cSChuck Lever 
495683ac665STrond Myklebust 	if (mech->gm_upcall_enctypes) {
4969d3a2260STrond Myklebust 		len = scnprintf(p, buflen, " enctypes=%s",
4979d3a2260STrond Myklebust 				mech->gm_upcall_enctypes);
4989d3a2260STrond Myklebust 		buflen -= len;
499683ac665STrond Myklebust 		p += len;
500683ac665STrond Myklebust 		gss_msg->msg.len += len;
501683ac665STrond Myklebust 	}
5020c77668dSChuck Lever 	trace_rpcgss_upcall_msg(gss_msg->databuf);
5039d3a2260STrond Myklebust 	len = scnprintf(p, buflen, "\n");
5049d3a2260STrond Myklebust 	if (len == 0)
5059d3a2260STrond Myklebust 		goto out_overflow;
5068b1c7bf5SOlga Kornievskaia 	gss_msg->msg.len += len;
50734769fc4S\"J. Bruce Fields\ 	gss_msg->msg.data = gss_msg->databuf;
5089d3a2260STrond Myklebust 	return 0;
5099d3a2260STrond Myklebust out_overflow:
5109d3a2260STrond Myklebust 	WARN_ON_ONCE(1);
5119d3a2260STrond Myklebust 	return -ENOMEM;
51234769fc4S\"J. Bruce Fields\ }
51334769fc4S\"J. Bruce Fields\ 
514ac83228aSTrond Myklebust static ssize_t
515ac83228aSTrond Myklebust gss_v1_upcall(struct file *file, struct rpc_pipe_msg *msg,
516ac83228aSTrond Myklebust 		char __user *buf, size_t buflen)
517ac83228aSTrond Myklebust {
518ac83228aSTrond Myklebust 	struct gss_upcall_msg *gss_msg = container_of(msg,
519ac83228aSTrond Myklebust 						      struct gss_upcall_msg,
520ac83228aSTrond Myklebust 						      msg);
521ac83228aSTrond Myklebust 	int err;
522ac83228aSTrond Myklebust 	if (msg->copied == 0) {
523ac83228aSTrond Myklebust 		err = gss_encode_v1_msg(gss_msg,
524ac83228aSTrond Myklebust 					gss_msg->service_name,
525ac83228aSTrond Myklebust 					gss_msg->auth->target_name,
526ac83228aSTrond Myklebust 					file->f_cred);
527ac83228aSTrond Myklebust 		if (err)
528ac83228aSTrond Myklebust 			return err;
529ac83228aSTrond Myklebust 	}
530ac83228aSTrond Myklebust 	return rpc_pipe_generic_upcall(file, msg, buf, buflen);
531ac83228aSTrond Myklebust }
532ac83228aSTrond Myklebust 
53368c97153STrond Myklebust static struct gss_upcall_msg *
534e726340aSTrond Myklebust gss_alloc_msg(struct gss_auth *gss_auth,
5357eaf040bSEric W. Biederman 		kuid_t uid, const char *service_name)
5361da177e4SLinus Torvalds {
5371da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
53879a3f20bS\"J. Bruce Fields\ 	int vers;
5399d3a2260STrond Myklebust 	int err = -ENOMEM;
5401da177e4SLinus Torvalds 
5410f38b873STrond Myklebust 	gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
542db75b3d6S\"J. Bruce Fields\ 	if (gss_msg == NULL)
5439d3a2260STrond Myklebust 		goto err;
544e726340aSTrond Myklebust 	vers = get_pipe_version(gss_auth->net);
5459d3a2260STrond Myklebust 	err = vers;
5469d3a2260STrond Myklebust 	if (err < 0)
5479d3a2260STrond Myklebust 		goto err_free_msg;
54819172284STrond Myklebust 	gss_msg->pipe = gss_auth->gss_pipe[vers]->pipe;
5491da177e4SLinus Torvalds 	INIT_LIST_HEAD(&gss_msg->list);
5501da177e4SLinus Torvalds 	rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
5511da177e4SLinus Torvalds 	init_waitqueue_head(&gss_msg->waitqueue);
5527ff13969SReshetova, Elena 	refcount_set(&gss_msg->count, 1);
5531da177e4SLinus Torvalds 	gss_msg->uid = uid;
5541da177e4SLinus Torvalds 	gss_msg->auth = gss_auth;
5555940d1cfSChuck Lever 	kref_get(&gss_auth->kref);
556ac83228aSTrond Myklebust 	if (service_name) {
557ac83228aSTrond Myklebust 		gss_msg->service_name = kstrdup_const(service_name, GFP_NOFS);
558fe31ce83SDan Carpenter 		if (!gss_msg->service_name) {
559fe31ce83SDan Carpenter 			err = -ENOMEM;
560e9776d0fSTrond Myklebust 			goto err_put_pipe_version;
56107d53ae4Szhong jiang 		}
562fe31ce83SDan Carpenter 	}
5631da177e4SLinus Torvalds 	return gss_msg;
564e9776d0fSTrond Myklebust err_put_pipe_version:
565e9776d0fSTrond Myklebust 	put_pipe_version(gss_auth->net);
5669d3a2260STrond Myklebust err_free_msg:
5679d3a2260STrond Myklebust 	kfree(gss_msg);
5689d3a2260STrond Myklebust err:
5699d3a2260STrond Myklebust 	return ERR_PTR(err);
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds static struct gss_upcall_msg *
573e726340aSTrond Myklebust gss_setup_upcall(struct gss_auth *gss_auth, struct rpc_cred *cred)
5741da177e4SLinus Torvalds {
5757c67db3aSTrond Myklebust 	struct gss_cred *gss_cred = container_of(cred,
5767c67db3aSTrond Myklebust 			struct gss_cred, gc_base);
5771da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_new, *gss_msg;
57804d1532bSNeilBrown 	kuid_t uid = cred->cr_cred->fsuid;
5791da177e4SLinus Torvalds 
580e726340aSTrond Myklebust 	gss_new = gss_alloc_msg(gss_auth, uid, gss_cred->gc_principal);
581db75b3d6S\"J. Bruce Fields\ 	if (IS_ERR(gss_new))
582db75b3d6S\"J. Bruce Fields\ 		return gss_new;
583053e324fSSuresh Jayaraman 	gss_msg = gss_add_msg(gss_new);
5841da177e4SLinus Torvalds 	if (gss_msg == gss_new) {
5851cded9d2SNeilBrown 		int res;
5867ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
5871cded9d2SNeilBrown 		res = rpc_queue_upcall(gss_new->pipe, &gss_new->msg);
5881da177e4SLinus Torvalds 		if (res) {
5891da177e4SLinus Torvalds 			gss_unhash_msg(gss_new);
5907ff13969SReshetova, Elena 			refcount_dec(&gss_msg->count);
5911cded9d2SNeilBrown 			gss_release_msg(gss_new);
5921da177e4SLinus Torvalds 			gss_msg = ERR_PTR(res);
5931da177e4SLinus Torvalds 		}
5941da177e4SLinus Torvalds 	} else
5951da177e4SLinus Torvalds 		gss_release_msg(gss_new);
5961da177e4SLinus Torvalds 	return gss_msg;
5971da177e4SLinus Torvalds }
5981da177e4SLinus Torvalds 
599b03568c3S\"J. Bruce Fields\ static void warn_gssd(void)
600b03568c3S\"J. Bruce Fields\ {
6010ea9de0eSJeff Layton 	dprintk("AUTH_GSS upcall failed. Please check user daemon is running.\n");
602b03568c3S\"J. Bruce Fields\ }
603b03568c3S\"J. Bruce Fields\ 
6041da177e4SLinus Torvalds static inline int
6051da177e4SLinus Torvalds gss_refresh_upcall(struct rpc_task *task)
6061da177e4SLinus Torvalds {
607a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
6084a8c1344STrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth,
6091da177e4SLinus Torvalds 			struct gss_auth, rpc_auth);
6101da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred,
6111da177e4SLinus Torvalds 			struct gss_cred, gc_base);
6121da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
6139beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
6141da177e4SLinus Torvalds 	int err = 0;
6151da177e4SLinus Torvalds 
616e726340aSTrond Myklebust 	gss_msg = gss_setup_upcall(gss_auth, cred);
617480e3243SRoel Kluin 	if (PTR_ERR(gss_msg) == -EAGAIN) {
61879a3f20bS\"J. Bruce Fields\ 		/* XXX: warning on the first, under the assumption we
61979a3f20bS\"J. Bruce Fields\ 		 * shouldn't normally hit this case on a refresh. */
62079a3f20bS\"J. Bruce Fields\ 		warn_gssd();
6216b2e6856STrond Myklebust 		rpc_sleep_on_timeout(&pipe_version_rpc_waitqueue,
6226b2e6856STrond Myklebust 				task, NULL, jiffies + (15 * HZ));
6230c77668dSChuck Lever 		err = -EAGAIN;
6240c77668dSChuck Lever 		goto out;
62579a3f20bS\"J. Bruce Fields\ 	}
6261da177e4SLinus Torvalds 	if (IS_ERR(gss_msg)) {
6271da177e4SLinus Torvalds 		err = PTR_ERR(gss_msg);
6281da177e4SLinus Torvalds 		goto out;
6291da177e4SLinus Torvalds 	}
6309beae467SStanislav Kinsbursky 	pipe = gss_msg->pipe;
6319beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
6321da177e4SLinus Torvalds 	if (gss_cred->gc_upcall != NULL)
6335d00837bSTrond Myklebust 		rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL);
634126e216aSTrond Myklebust 	else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
6351da177e4SLinus Torvalds 		gss_cred->gc_upcall = gss_msg;
6361da177e4SLinus Torvalds 		/* gss_upcall_callback will release the reference to gss_upcall_msg */
6377ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
6385d00837bSTrond Myklebust 		rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback);
639126e216aSTrond Myklebust 	} else {
640126e216aSTrond Myklebust 		gss_handle_downcall_result(gss_cred, gss_msg);
6411da177e4SLinus Torvalds 		err = gss_msg->msg.errno;
642126e216aSTrond Myklebust 	}
6439beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
6441da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
6451da177e4SLinus Torvalds out:
6460c77668dSChuck Lever 	trace_rpcgss_upcall_result(from_kuid(&init_user_ns,
6470c77668dSChuck Lever 					     cred->cr_cred->fsuid), err);
6481da177e4SLinus Torvalds 	return err;
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds static inline int
6521da177e4SLinus Torvalds gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
6531da177e4SLinus Torvalds {
654e726340aSTrond Myklebust 	struct net *net = gss_auth->net;
655abfdbd53STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
6569beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
6571da177e4SLinus Torvalds 	struct rpc_cred *cred = &gss_cred->gc_base;
6581da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
6591da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
660d36ccb9cSTrond Myklebust 	int err;
6611da177e4SLinus Torvalds 
66279a3f20bS\"J. Bruce Fields\ retry:
663d36ccb9cSTrond Myklebust 	err = 0;
66489f84243SJeff Layton 	/* if gssd is down, just skip upcalling altogether */
66589f84243SJeff Layton 	if (!gssd_running(net)) {
66689f84243SJeff Layton 		warn_gssd();
6670c77668dSChuck Lever 		err = -EACCES;
6680c77668dSChuck Lever 		goto out;
66989f84243SJeff Layton 	}
670e726340aSTrond Myklebust 	gss_msg = gss_setup_upcall(gss_auth, cred);
67179a3f20bS\"J. Bruce Fields\ 	if (PTR_ERR(gss_msg) == -EAGAIN) {
67279a3f20bS\"J. Bruce Fields\ 		err = wait_event_interruptible_timeout(pipe_version_waitqueue,
67389f84243SJeff Layton 				sn->pipe_version >= 0, 15 * HZ);
6742aed8b47STrond Myklebust 		if (sn->pipe_version < 0) {
675d1a8016aSBryan Schumaker 			warn_gssd();
676d1a8016aSBryan Schumaker 			err = -EACCES;
677d1a8016aSBryan Schumaker 		}
678d36ccb9cSTrond Myklebust 		if (err < 0)
67979a3f20bS\"J. Bruce Fields\ 			goto out;
68079a3f20bS\"J. Bruce Fields\ 		goto retry;
68179a3f20bS\"J. Bruce Fields\ 	}
6821da177e4SLinus Torvalds 	if (IS_ERR(gss_msg)) {
6831da177e4SLinus Torvalds 		err = PTR_ERR(gss_msg);
6841da177e4SLinus Torvalds 		goto out;
6851da177e4SLinus Torvalds 	}
6869beae467SStanislav Kinsbursky 	pipe = gss_msg->pipe;
6871da177e4SLinus Torvalds 	for (;;) {
6885afa9133STrond Myklebust 		prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_KILLABLE);
6899beae467SStanislav Kinsbursky 		spin_lock(&pipe->lock);
6901da177e4SLinus Torvalds 		if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
6911da177e4SLinus Torvalds 			break;
6921da177e4SLinus Torvalds 		}
6939beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
6945afa9133STrond Myklebust 		if (fatal_signal_pending(current)) {
6951da177e4SLinus Torvalds 			err = -ERESTARTSYS;
6961da177e4SLinus Torvalds 			goto out_intr;
6971da177e4SLinus Torvalds 		}
6981da177e4SLinus Torvalds 		schedule();
6991da177e4SLinus Torvalds 	}
7001da177e4SLinus Torvalds 	if (gss_msg->ctx)
7017b6962b0STrond Myklebust 		gss_cred_set_ctx(cred, gss_msg->ctx);
7021da177e4SLinus Torvalds 	else
7031da177e4SLinus Torvalds 		err = gss_msg->msg.errno;
7049beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
7051da177e4SLinus Torvalds out_intr:
7061da177e4SLinus Torvalds 	finish_wait(&gss_msg->waitqueue, &wait);
7071da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
7081da177e4SLinus Torvalds out:
7090c77668dSChuck Lever 	trace_rpcgss_upcall_result(from_kuid(&init_user_ns,
7100c77668dSChuck Lever 					     cred->cr_cred->fsuid), err);
7111da177e4SLinus Torvalds 	return err;
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds #define MSG_BUF_MAXSIZE 1024
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds static ssize_t
7171da177e4SLinus Torvalds gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
7181da177e4SLinus Torvalds {
7191da177e4SLinus Torvalds 	const void *p, *end;
7201da177e4SLinus Torvalds 	void *buf;
7211da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
722496ad9aaSAl Viro 	struct rpc_pipe *pipe = RPC_I(file_inode(filp))->pipe;
7231da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
72490602c7bSEric W. Biederman 	uid_t id;
72590602c7bSEric W. Biederman 	kuid_t uid;
7263b68aaeaSTrond Myklebust 	ssize_t err = -EFBIG;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	if (mlen > MSG_BUF_MAXSIZE)
7291da177e4SLinus Torvalds 		goto out;
7301da177e4SLinus Torvalds 	err = -ENOMEM;
7310f38b873STrond Myklebust 	buf = kmalloc(mlen, GFP_NOFS);
7321da177e4SLinus Torvalds 	if (!buf)
7331da177e4SLinus Torvalds 		goto out;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	err = -EFAULT;
7361da177e4SLinus Torvalds 	if (copy_from_user(buf, src, mlen))
7371da177e4SLinus Torvalds 		goto err;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	end = (const void *)((char *)buf + mlen);
74090602c7bSEric W. Biederman 	p = simple_get_bytes(buf, end, &id, sizeof(id));
7411da177e4SLinus Torvalds 	if (IS_ERR(p)) {
7421da177e4SLinus Torvalds 		err = PTR_ERR(p);
7431da177e4SLinus Torvalds 		goto err;
7441da177e4SLinus Torvalds 	}
7451da177e4SLinus Torvalds 
746283ebe3eSTrond Myklebust 	uid = make_kuid(current_user_ns(), id);
74790602c7bSEric W. Biederman 	if (!uid_valid(uid)) {
74890602c7bSEric W. Biederman 		err = -EINVAL;
74990602c7bSEric W. Biederman 		goto err;
75090602c7bSEric W. Biederman 	}
75190602c7bSEric W. Biederman 
7521da177e4SLinus Torvalds 	err = -ENOMEM;
7531da177e4SLinus Torvalds 	ctx = gss_alloc_context();
7541da177e4SLinus Torvalds 	if (ctx == NULL)
7551da177e4SLinus Torvalds 		goto err;
7563b68aaeaSTrond Myklebust 
7573b68aaeaSTrond Myklebust 	err = -ENOENT;
7583b68aaeaSTrond Myklebust 	/* Find a matching upcall */
7599beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
7609130b8dbSOlga Kornievskaia 	gss_msg = __gss_find_upcall(pipe, uid, NULL);
7613b68aaeaSTrond Myklebust 	if (gss_msg == NULL) {
7629beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
7633b68aaeaSTrond Myklebust 		goto err_put_ctx;
7643b68aaeaSTrond Myklebust 	}
7653b68aaeaSTrond Myklebust 	list_del_init(&gss_msg->list);
7669beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
7673b68aaeaSTrond Myklebust 
7686e84c7b6STrond Myklebust 	p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
7691da177e4SLinus Torvalds 	if (IS_ERR(p)) {
7701da177e4SLinus Torvalds 		err = PTR_ERR(p);
771486bad2eSJeff Layton 		switch (err) {
772486bad2eSJeff Layton 		case -EACCES:
773dc5ddce9SJeff Layton 		case -EKEYEXPIRED:
774486bad2eSJeff Layton 			gss_msg->msg.errno = err;
775486bad2eSJeff Layton 			err = mlen;
776486bad2eSJeff Layton 			break;
777486bad2eSJeff Layton 		case -EFAULT:
778486bad2eSJeff Layton 		case -ENOMEM:
779486bad2eSJeff Layton 		case -EINVAL:
780486bad2eSJeff Layton 		case -ENOSYS:
781486bad2eSJeff Layton 			gss_msg->msg.errno = -EAGAIN;
782486bad2eSJeff Layton 			break;
783486bad2eSJeff Layton 		default:
784486bad2eSJeff Layton 			printk(KERN_CRIT "%s: bad return from "
7856c853099SRandy Dunlap 				"gss_fill_context: %zd\n", __func__, err);
786437b300cSScott Mayhew 			gss_msg->msg.errno = -EIO;
787486bad2eSJeff Layton 		}
7883b68aaeaSTrond Myklebust 		goto err_release_msg;
7891da177e4SLinus Torvalds 	}
7901da177e4SLinus Torvalds 	gss_msg->ctx = gss_get_ctx(ctx);
7913b68aaeaSTrond Myklebust 	err = mlen;
7923b68aaeaSTrond Myklebust 
7933b68aaeaSTrond Myklebust err_release_msg:
7949beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
7951da177e4SLinus Torvalds 	__gss_unhash_msg(gss_msg);
7969beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
7971da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
7981da177e4SLinus Torvalds err_put_ctx:
7991da177e4SLinus Torvalds 	gss_put_ctx(ctx);
8001da177e4SLinus Torvalds err:
8011da177e4SLinus Torvalds 	kfree(buf);
8021da177e4SLinus Torvalds out:
8031da177e4SLinus Torvalds 	return err;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
80634769fc4S\"J. Bruce Fields\ static int gss_pipe_open(struct inode *inode, int new_version)
807cf81939dS\"J. Bruce Fields\ {
8082aed8b47STrond Myklebust 	struct net *net = inode->i_sb->s_fs_info;
8092aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
81034769fc4S\"J. Bruce Fields\ 	int ret = 0;
81134769fc4S\"J. Bruce Fields\ 
81279a3f20bS\"J. Bruce Fields\ 	spin_lock(&pipe_version_lock);
8132aed8b47STrond Myklebust 	if (sn->pipe_version < 0) {
81434769fc4S\"J. Bruce Fields\ 		/* First open of any gss pipe determines the version: */
8152aed8b47STrond Myklebust 		sn->pipe_version = new_version;
81679a3f20bS\"J. Bruce Fields\ 		rpc_wake_up(&pipe_version_rpc_waitqueue);
81779a3f20bS\"J. Bruce Fields\ 		wake_up(&pipe_version_waitqueue);
8182aed8b47STrond Myklebust 	} else if (sn->pipe_version != new_version) {
81934769fc4S\"J. Bruce Fields\ 		/* Trying to open a pipe of a different version */
82034769fc4S\"J. Bruce Fields\ 		ret = -EBUSY;
82134769fc4S\"J. Bruce Fields\ 		goto out;
82279a3f20bS\"J. Bruce Fields\ 	}
8232aed8b47STrond Myklebust 	atomic_inc(&sn->pipe_users);
82434769fc4S\"J. Bruce Fields\ out:
82579a3f20bS\"J. Bruce Fields\ 	spin_unlock(&pipe_version_lock);
82634769fc4S\"J. Bruce Fields\ 	return ret;
82734769fc4S\"J. Bruce Fields\ 
82834769fc4S\"J. Bruce Fields\ }
82934769fc4S\"J. Bruce Fields\ 
83034769fc4S\"J. Bruce Fields\ static int gss_pipe_open_v0(struct inode *inode)
83134769fc4S\"J. Bruce Fields\ {
83234769fc4S\"J. Bruce Fields\ 	return gss_pipe_open(inode, 0);
83334769fc4S\"J. Bruce Fields\ }
83434769fc4S\"J. Bruce Fields\ 
83534769fc4S\"J. Bruce Fields\ static int gss_pipe_open_v1(struct inode *inode)
83634769fc4S\"J. Bruce Fields\ {
83734769fc4S\"J. Bruce Fields\ 	return gss_pipe_open(inode, 1);
838cf81939dS\"J. Bruce Fields\ }
839cf81939dS\"J. Bruce Fields\ 
8401da177e4SLinus Torvalds static void
8411da177e4SLinus Torvalds gss_pipe_release(struct inode *inode)
8421da177e4SLinus Torvalds {
8432aed8b47STrond Myklebust 	struct net *net = inode->i_sb->s_fs_info;
8449beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
8451da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
8461da177e4SLinus Torvalds 
8475a67657aSTrond Myklebust restart:
8489beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
8499beae467SStanislav Kinsbursky 	list_for_each_entry(gss_msg, &pipe->in_downcall, list) {
8506e84c7b6STrond Myklebust 
8515a67657aSTrond Myklebust 		if (!list_empty(&gss_msg->msg.list))
8525a67657aSTrond Myklebust 			continue;
8531da177e4SLinus Torvalds 		gss_msg->msg.errno = -EPIPE;
8547ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
8551da177e4SLinus Torvalds 		__gss_unhash_msg(gss_msg);
8569beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
8571da177e4SLinus Torvalds 		gss_release_msg(gss_msg);
8585a67657aSTrond Myklebust 		goto restart;
8591da177e4SLinus Torvalds 	}
8609beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
861cf81939dS\"J. Bruce Fields\ 
8622aed8b47STrond Myklebust 	put_pipe_version(net);
8631da177e4SLinus Torvalds }
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds static void
8661da177e4SLinus Torvalds gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
8671da177e4SLinus Torvalds {
8681da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	if (msg->errno < 0) {
8717ff13969SReshetova, Elena 		refcount_inc(&gss_msg->count);
8721da177e4SLinus Torvalds 		gss_unhash_msg(gss_msg);
873b03568c3S\"J. Bruce Fields\ 		if (msg->errno == -ETIMEDOUT)
874b03568c3S\"J. Bruce Fields\ 			warn_gssd();
8751da177e4SLinus Torvalds 		gss_release_msg(gss_msg);
8761da177e4SLinus Torvalds 	}
8771cded9d2SNeilBrown 	gss_release_msg(gss_msg);
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
88019172284STrond Myklebust static void gss_pipe_dentry_destroy(struct dentry *dir,
88119172284STrond Myklebust 		struct rpc_pipe_dir_object *pdo)
882ccdc28f8SStanislav Kinsbursky {
88319172284STrond Myklebust 	struct gss_pipe *gss_pipe = pdo->pdo_data;
88419172284STrond Myklebust 	struct rpc_pipe *pipe = gss_pipe->pipe;
885ccdc28f8SStanislav Kinsbursky 
88619172284STrond Myklebust 	if (pipe->dentry != NULL) {
88719172284STrond Myklebust 		rpc_unlink(pipe->dentry);
88819172284STrond Myklebust 		pipe->dentry = NULL;
8896b2fddd3STrond Myklebust 	}
890ccdc28f8SStanislav Kinsbursky }
891ccdc28f8SStanislav Kinsbursky 
89219172284STrond Myklebust static int gss_pipe_dentry_create(struct dentry *dir,
89319172284STrond Myklebust 		struct rpc_pipe_dir_object *pdo)
894ccdc28f8SStanislav Kinsbursky {
89519172284STrond Myklebust 	struct gss_pipe *p = pdo->pdo_data;
8966b2fddd3STrond Myklebust 	struct dentry *dentry;
897ccdc28f8SStanislav Kinsbursky 
89819172284STrond Myklebust 	dentry = rpc_mkpipe_dentry(dir, p->name, p->clnt, p->pipe);
89919172284STrond Myklebust 	if (IS_ERR(dentry))
90019172284STrond Myklebust 		return PTR_ERR(dentry);
90119172284STrond Myklebust 	p->pipe->dentry = dentry;
902ccdc28f8SStanislav Kinsbursky 	return 0;
90319172284STrond Myklebust }
904ccdc28f8SStanislav Kinsbursky 
90519172284STrond Myklebust static const struct rpc_pipe_dir_object_ops gss_pipe_dir_object_ops = {
90619172284STrond Myklebust 	.create = gss_pipe_dentry_create,
90719172284STrond Myklebust 	.destroy = gss_pipe_dentry_destroy,
90819172284STrond Myklebust };
90919172284STrond Myklebust 
91019172284STrond Myklebust static struct gss_pipe *gss_pipe_alloc(struct rpc_clnt *clnt,
91119172284STrond Myklebust 		const char *name,
91219172284STrond Myklebust 		const struct rpc_pipe_ops *upcall_ops)
91319172284STrond Myklebust {
91419172284STrond Myklebust 	struct gss_pipe *p;
91519172284STrond Myklebust 	int err = -ENOMEM;
91619172284STrond Myklebust 
91719172284STrond Myklebust 	p = kmalloc(sizeof(*p), GFP_KERNEL);
91819172284STrond Myklebust 	if (p == NULL)
91919172284STrond Myklebust 		goto err;
92019172284STrond Myklebust 	p->pipe = rpc_mkpipe_data(upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
92119172284STrond Myklebust 	if (IS_ERR(p->pipe)) {
92219172284STrond Myklebust 		err = PTR_ERR(p->pipe);
92319172284STrond Myklebust 		goto err_free_gss_pipe;
92419172284STrond Myklebust 	}
92519172284STrond Myklebust 	p->name = name;
92619172284STrond Myklebust 	p->clnt = clnt;
927414a6295STrond Myklebust 	kref_init(&p->kref);
92819172284STrond Myklebust 	rpc_init_pipe_dir_object(&p->pdo,
92919172284STrond Myklebust 			&gss_pipe_dir_object_ops,
93019172284STrond Myklebust 			p);
93119172284STrond Myklebust 	return p;
93219172284STrond Myklebust err_free_gss_pipe:
93319172284STrond Myklebust 	kfree(p);
9346b2fddd3STrond Myklebust err:
93519172284STrond Myklebust 	return ERR_PTR(err);
936ccdc28f8SStanislav Kinsbursky }
937ccdc28f8SStanislav Kinsbursky 
938414a6295STrond Myklebust struct gss_alloc_pdo {
939414a6295STrond Myklebust 	struct rpc_clnt *clnt;
940414a6295STrond Myklebust 	const char *name;
941414a6295STrond Myklebust 	const struct rpc_pipe_ops *upcall_ops;
942414a6295STrond Myklebust };
943414a6295STrond Myklebust 
944414a6295STrond Myklebust static int gss_pipe_match_pdo(struct rpc_pipe_dir_object *pdo, void *data)
945414a6295STrond Myklebust {
946414a6295STrond Myklebust 	struct gss_pipe *gss_pipe;
947414a6295STrond Myklebust 	struct gss_alloc_pdo *args = data;
948414a6295STrond Myklebust 
949414a6295STrond Myklebust 	if (pdo->pdo_ops != &gss_pipe_dir_object_ops)
950414a6295STrond Myklebust 		return 0;
951414a6295STrond Myklebust 	gss_pipe = container_of(pdo, struct gss_pipe, pdo);
952414a6295STrond Myklebust 	if (strcmp(gss_pipe->name, args->name) != 0)
953414a6295STrond Myklebust 		return 0;
954414a6295STrond Myklebust 	if (!kref_get_unless_zero(&gss_pipe->kref))
955414a6295STrond Myklebust 		return 0;
956414a6295STrond Myklebust 	return 1;
957414a6295STrond Myklebust }
958414a6295STrond Myklebust 
959414a6295STrond Myklebust static struct rpc_pipe_dir_object *gss_pipe_alloc_pdo(void *data)
960414a6295STrond Myklebust {
961414a6295STrond Myklebust 	struct gss_pipe *gss_pipe;
962414a6295STrond Myklebust 	struct gss_alloc_pdo *args = data;
963414a6295STrond Myklebust 
964414a6295STrond Myklebust 	gss_pipe = gss_pipe_alloc(args->clnt, args->name, args->upcall_ops);
965414a6295STrond Myklebust 	if (!IS_ERR(gss_pipe))
966414a6295STrond Myklebust 		return &gss_pipe->pdo;
967414a6295STrond Myklebust 	return NULL;
968414a6295STrond Myklebust }
969414a6295STrond Myklebust 
970414a6295STrond Myklebust static struct gss_pipe *gss_pipe_get(struct rpc_clnt *clnt,
971414a6295STrond Myklebust 		const char *name,
972414a6295STrond Myklebust 		const struct rpc_pipe_ops *upcall_ops)
973414a6295STrond Myklebust {
974414a6295STrond Myklebust 	struct net *net = rpc_net_ns(clnt);
975414a6295STrond Myklebust 	struct rpc_pipe_dir_object *pdo;
976414a6295STrond Myklebust 	struct gss_alloc_pdo args = {
977414a6295STrond Myklebust 		.clnt = clnt,
978414a6295STrond Myklebust 		.name = name,
979414a6295STrond Myklebust 		.upcall_ops = upcall_ops,
980414a6295STrond Myklebust 	};
981414a6295STrond Myklebust 
982414a6295STrond Myklebust 	pdo = rpc_find_or_alloc_pipe_dir_object(net,
983414a6295STrond Myklebust 			&clnt->cl_pipedir_objects,
984414a6295STrond Myklebust 			gss_pipe_match_pdo,
985414a6295STrond Myklebust 			gss_pipe_alloc_pdo,
986414a6295STrond Myklebust 			&args);
987414a6295STrond Myklebust 	if (pdo != NULL)
988414a6295STrond Myklebust 		return container_of(pdo, struct gss_pipe, pdo);
989414a6295STrond Myklebust 	return ERR_PTR(-ENOMEM);
990414a6295STrond Myklebust }
991414a6295STrond Myklebust 
99219172284STrond Myklebust static void __gss_pipe_free(struct gss_pipe *p)
993ccdc28f8SStanislav Kinsbursky {
99419172284STrond Myklebust 	struct rpc_clnt *clnt = p->clnt;
99519172284STrond Myklebust 	struct net *net = rpc_net_ns(clnt);
996ccdc28f8SStanislav Kinsbursky 
99719172284STrond Myklebust 	rpc_remove_pipe_dir_object(net,
99819172284STrond Myklebust 			&clnt->cl_pipedir_objects,
99919172284STrond Myklebust 			&p->pdo);
100019172284STrond Myklebust 	rpc_destroy_pipe_data(p->pipe);
100119172284STrond Myklebust 	kfree(p);
1002ccdc28f8SStanislav Kinsbursky }
1003ccdc28f8SStanislav Kinsbursky 
1004414a6295STrond Myklebust static void __gss_pipe_release(struct kref *kref)
1005414a6295STrond Myklebust {
1006414a6295STrond Myklebust 	struct gss_pipe *p = container_of(kref, struct gss_pipe, kref);
1007414a6295STrond Myklebust 
1008414a6295STrond Myklebust 	__gss_pipe_free(p);
1009414a6295STrond Myklebust }
1010414a6295STrond Myklebust 
101119172284STrond Myklebust static void gss_pipe_free(struct gss_pipe *p)
1012ccdc28f8SStanislav Kinsbursky {
101319172284STrond Myklebust 	if (p != NULL)
1014414a6295STrond Myklebust 		kref_put(&p->kref, __gss_pipe_release);
1015ccdc28f8SStanislav Kinsbursky }
1016ccdc28f8SStanislav Kinsbursky 
10171da177e4SLinus Torvalds /*
10181da177e4SLinus Torvalds  * NOTE: we have the opportunity to use different
10191da177e4SLinus Torvalds  * parameters based on the input flavor (which must be a pseudoflavor)
10201da177e4SLinus Torvalds  */
1021eb6dc19dSTrond Myklebust static struct gss_auth *
102282b98ca5SSargun Dhillon gss_create_new(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
10231da177e4SLinus Torvalds {
1024c2190661STrond Myklebust 	rpc_authflavor_t flavor = args->pseudoflavor;
10251da177e4SLinus Torvalds 	struct gss_auth *gss_auth;
102619172284STrond Myklebust 	struct gss_pipe *gss_pipe;
10271da177e4SLinus Torvalds 	struct rpc_auth * auth;
10286a19275aSJ. Bruce Fields 	int err = -ENOMEM; /* XXX? */
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	if (!try_module_get(THIS_MODULE))
10316a19275aSJ. Bruce Fields 		return ERR_PTR(err);
10321da177e4SLinus Torvalds 	if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
10331da177e4SLinus Torvalds 		goto out_dec;
1034eb6dc19dSTrond Myklebust 	INIT_HLIST_NODE(&gss_auth->hash);
1035bd4a3eb1STrond Myklebust 	gss_auth->target_name = NULL;
1036c2190661STrond Myklebust 	if (args->target_name) {
1037c2190661STrond Myklebust 		gss_auth->target_name = kstrdup(args->target_name, GFP_KERNEL);
1038bd4a3eb1STrond Myklebust 		if (gss_auth->target_name == NULL)
1039bd4a3eb1STrond Myklebust 			goto err_free;
1040bd4a3eb1STrond Myklebust 	}
10411da177e4SLinus Torvalds 	gss_auth->client = clnt;
1042e726340aSTrond Myklebust 	gss_auth->net = get_net(rpc_net_ns(clnt));
10436a19275aSJ. Bruce Fields 	err = -EINVAL;
10441da177e4SLinus Torvalds 	gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
10450c77668dSChuck Lever 	if (!gss_auth->mech)
1046e726340aSTrond Myklebust 		goto err_put_net;
10471da177e4SLinus Torvalds 	gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
1048438b6fdeSJ. Bruce Fields 	if (gss_auth->service == 0)
1049438b6fdeSJ. Bruce Fields 		goto err_put_mech;
1050a699d65eSTrond Myklebust 	if (!gssd_running(gss_auth->net))
1051a699d65eSTrond Myklebust 		goto err_put_mech;
10521da177e4SLinus Torvalds 	auth = &gss_auth->rpc_auth;
10531da177e4SLinus Torvalds 	auth->au_cslack = GSS_CRED_SLACK >> 2;
1054df513a77SOlga Kornievskaia 	auth->au_rslack = GSS_KRB5_MAX_SLACK_NEEDED >> 2;
1055a00275baSChuck Lever 	auth->au_verfsize = GSS_VERF_SLACK >> 2;
105635e77d21SChuck Lever 	auth->au_ralign = GSS_VERF_SLACK >> 2;
1057ce52914eSScott Mayhew 	auth->au_flags = 0;
10581da177e4SLinus Torvalds 	auth->au_ops = &authgss_ops;
10591da177e4SLinus Torvalds 	auth->au_flavor = flavor;
106065b80179SChuck Lever 	if (gss_pseudoflavor_to_datatouch(gss_auth->mech, flavor))
106165b80179SChuck Lever 		auth->au_flags |= RPCAUTH_AUTH_DATATOUCH;
1062331bc71cSTrond Myklebust 	refcount_set(&auth->au_count, 1);
10630285ed1fSTrond Myklebust 	kref_init(&gss_auth->kref);
10641da177e4SLinus Torvalds 
106519172284STrond Myklebust 	err = rpcauth_init_credcache(auth);
106619172284STrond Myklebust 	if (err)
106719172284STrond Myklebust 		goto err_put_mech;
106834769fc4S\"J. Bruce Fields\ 	/*
106934769fc4S\"J. Bruce Fields\ 	 * Note: if we created the old pipe first, then someone who
107034769fc4S\"J. Bruce Fields\ 	 * examined the directory at the right moment might conclude
107134769fc4S\"J. Bruce Fields\ 	 * that we supported only the old pipe.  So we instead create
107234769fc4S\"J. Bruce Fields\ 	 * the new pipe first.
107334769fc4S\"J. Bruce Fields\ 	 */
1074414a6295STrond Myklebust 	gss_pipe = gss_pipe_get(clnt, "gssd", &gss_upcall_ops_v1);
107519172284STrond Myklebust 	if (IS_ERR(gss_pipe)) {
107619172284STrond Myklebust 		err = PTR_ERR(gss_pipe);
107719172284STrond Myklebust 		goto err_destroy_credcache;
10786a19275aSJ. Bruce Fields 	}
107919172284STrond Myklebust 	gss_auth->gss_pipe[1] = gss_pipe;
10801da177e4SLinus Torvalds 
1081414a6295STrond Myklebust 	gss_pipe = gss_pipe_get(clnt, gss_auth->mech->gm_name,
108219172284STrond Myklebust 			&gss_upcall_ops_v0);
108319172284STrond Myklebust 	if (IS_ERR(gss_pipe)) {
108419172284STrond Myklebust 		err = PTR_ERR(gss_pipe);
1085c239d83bSStanislav Kinsbursky 		goto err_destroy_pipe_1;
1086c239d83bSStanislav Kinsbursky 	}
108719172284STrond Myklebust 	gss_auth->gss_pipe[0] = gss_pipe;
108807a2bf1dSTrond Myklebust 
1089eb6dc19dSTrond Myklebust 	return gss_auth;
1090c239d83bSStanislav Kinsbursky err_destroy_pipe_1:
1091414a6295STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[1]);
109219172284STrond Myklebust err_destroy_credcache:
109319172284STrond Myklebust 	rpcauth_destroy_credcache(auth);
10941da177e4SLinus Torvalds err_put_mech:
10951da177e4SLinus Torvalds 	gss_mech_put(gss_auth->mech);
1096e726340aSTrond Myklebust err_put_net:
1097e726340aSTrond Myklebust 	put_net(gss_auth->net);
10981da177e4SLinus Torvalds err_free:
1099bd4a3eb1STrond Myklebust 	kfree(gss_auth->target_name);
11001da177e4SLinus Torvalds 	kfree(gss_auth);
11011da177e4SLinus Torvalds out_dec:
11021da177e4SLinus Torvalds 	module_put(THIS_MODULE);
11030c77668dSChuck Lever 	trace_rpcgss_createauth(flavor, err);
11046a19275aSJ. Bruce Fields 	return ERR_PTR(err);
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds static void
11080285ed1fSTrond Myklebust gss_free(struct gss_auth *gss_auth)
11090285ed1fSTrond Myklebust {
111019172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[0]);
111119172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[1]);
11120285ed1fSTrond Myklebust 	gss_mech_put(gss_auth->mech);
1113e726340aSTrond Myklebust 	put_net(gss_auth->net);
1114bd4a3eb1STrond Myklebust 	kfree(gss_auth->target_name);
11150285ed1fSTrond Myklebust 
11160285ed1fSTrond Myklebust 	kfree(gss_auth);
11170285ed1fSTrond Myklebust 	module_put(THIS_MODULE);
11180285ed1fSTrond Myklebust }
11190285ed1fSTrond Myklebust 
11200285ed1fSTrond Myklebust static void
11210285ed1fSTrond Myklebust gss_free_callback(struct kref *kref)
11220285ed1fSTrond Myklebust {
11230285ed1fSTrond Myklebust 	struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
11240285ed1fSTrond Myklebust 
11250285ed1fSTrond Myklebust 	gss_free(gss_auth);
11260285ed1fSTrond Myklebust }
11270285ed1fSTrond Myklebust 
11280285ed1fSTrond Myklebust static void
11299eb2ddb4STrond Myklebust gss_put_auth(struct gss_auth *gss_auth)
11309eb2ddb4STrond Myklebust {
11319eb2ddb4STrond Myklebust 	kref_put(&gss_auth->kref, gss_free_callback);
11329eb2ddb4STrond Myklebust }
11339eb2ddb4STrond Myklebust 
11349eb2ddb4STrond Myklebust static void
11351da177e4SLinus Torvalds gss_destroy(struct rpc_auth *auth)
11361da177e4SLinus Torvalds {
113719172284STrond Myklebust 	struct gss_auth *gss_auth = container_of(auth,
113819172284STrond Myklebust 			struct gss_auth, rpc_auth);
11391da177e4SLinus Torvalds 
1140eb6dc19dSTrond Myklebust 	if (hash_hashed(&gss_auth->hash)) {
1141eb6dc19dSTrond Myklebust 		spin_lock(&gss_auth_hash_lock);
1142eb6dc19dSTrond Myklebust 		hash_del(&gss_auth->hash);
1143eb6dc19dSTrond Myklebust 		spin_unlock(&gss_auth_hash_lock);
1144eb6dc19dSTrond Myklebust 	}
1145eb6dc19dSTrond Myklebust 
114619172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[0]);
114719172284STrond Myklebust 	gss_auth->gss_pipe[0] = NULL;
114819172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[1]);
114919172284STrond Myklebust 	gss_auth->gss_pipe[1] = NULL;
11503ab9bb72STrond Myklebust 	rpcauth_destroy_credcache(auth);
11513ab9bb72STrond Myklebust 
11529eb2ddb4STrond Myklebust 	gss_put_auth(gss_auth);
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
1155a0f6ed8eSJ. Bruce Fields /*
1156a0f6ed8eSJ. Bruce Fields  * Auths may be shared between rpc clients that were cloned from a
1157a0f6ed8eSJ. Bruce Fields  * common client with the same xprt, if they also share the flavor and
1158a0f6ed8eSJ. Bruce Fields  * target_name.
1159a0f6ed8eSJ. Bruce Fields  *
1160a0f6ed8eSJ. Bruce Fields  * The auth is looked up from the oldest parent sharing the same
1161a0f6ed8eSJ. Bruce Fields  * cl_xprt, and the auth itself references only that common parent
1162a0f6ed8eSJ. Bruce Fields  * (which is guaranteed to last as long as any of its descendants).
1163a0f6ed8eSJ. Bruce Fields  */
1164eb6dc19dSTrond Myklebust static struct gss_auth *
116582b98ca5SSargun Dhillon gss_auth_find_or_add_hashed(const struct rpc_auth_create_args *args,
1166eb6dc19dSTrond Myklebust 		struct rpc_clnt *clnt,
1167eb6dc19dSTrond Myklebust 		struct gss_auth *new)
1168eb6dc19dSTrond Myklebust {
1169eb6dc19dSTrond Myklebust 	struct gss_auth *gss_auth;
1170eb6dc19dSTrond Myklebust 	unsigned long hashval = (unsigned long)clnt;
1171eb6dc19dSTrond Myklebust 
1172eb6dc19dSTrond Myklebust 	spin_lock(&gss_auth_hash_lock);
1173eb6dc19dSTrond Myklebust 	hash_for_each_possible(gss_auth_hash_table,
1174eb6dc19dSTrond Myklebust 			gss_auth,
1175eb6dc19dSTrond Myklebust 			hash,
1176eb6dc19dSTrond Myklebust 			hashval) {
1177a0f6ed8eSJ. Bruce Fields 		if (gss_auth->client != clnt)
1178a0f6ed8eSJ. Bruce Fields 			continue;
1179eb6dc19dSTrond Myklebust 		if (gss_auth->rpc_auth.au_flavor != args->pseudoflavor)
1180eb6dc19dSTrond Myklebust 			continue;
1181eb6dc19dSTrond Myklebust 		if (gss_auth->target_name != args->target_name) {
1182eb6dc19dSTrond Myklebust 			if (gss_auth->target_name == NULL)
1183eb6dc19dSTrond Myklebust 				continue;
1184eb6dc19dSTrond Myklebust 			if (args->target_name == NULL)
1185eb6dc19dSTrond Myklebust 				continue;
1186eb6dc19dSTrond Myklebust 			if (strcmp(gss_auth->target_name, args->target_name))
1187eb6dc19dSTrond Myklebust 				continue;
1188eb6dc19dSTrond Myklebust 		}
1189331bc71cSTrond Myklebust 		if (!refcount_inc_not_zero(&gss_auth->rpc_auth.au_count))
1190eb6dc19dSTrond Myklebust 			continue;
1191eb6dc19dSTrond Myklebust 		goto out;
1192eb6dc19dSTrond Myklebust 	}
1193eb6dc19dSTrond Myklebust 	if (new)
1194eb6dc19dSTrond Myklebust 		hash_add(gss_auth_hash_table, &new->hash, hashval);
1195eb6dc19dSTrond Myklebust 	gss_auth = new;
1196eb6dc19dSTrond Myklebust out:
1197eb6dc19dSTrond Myklebust 	spin_unlock(&gss_auth_hash_lock);
1198eb6dc19dSTrond Myklebust 	return gss_auth;
1199eb6dc19dSTrond Myklebust }
1200eb6dc19dSTrond Myklebust 
1201eb6dc19dSTrond Myklebust static struct gss_auth *
120282b98ca5SSargun Dhillon gss_create_hashed(const struct rpc_auth_create_args *args,
120382b98ca5SSargun Dhillon 		  struct rpc_clnt *clnt)
1204eb6dc19dSTrond Myklebust {
1205eb6dc19dSTrond Myklebust 	struct gss_auth *gss_auth;
1206eb6dc19dSTrond Myklebust 	struct gss_auth *new;
1207eb6dc19dSTrond Myklebust 
1208eb6dc19dSTrond Myklebust 	gss_auth = gss_auth_find_or_add_hashed(args, clnt, NULL);
1209eb6dc19dSTrond Myklebust 	if (gss_auth != NULL)
1210eb6dc19dSTrond Myklebust 		goto out;
1211eb6dc19dSTrond Myklebust 	new = gss_create_new(args, clnt);
1212eb6dc19dSTrond Myklebust 	if (IS_ERR(new))
1213eb6dc19dSTrond Myklebust 		return new;
1214eb6dc19dSTrond Myklebust 	gss_auth = gss_auth_find_or_add_hashed(args, clnt, new);
1215eb6dc19dSTrond Myklebust 	if (gss_auth != new)
1216eb6dc19dSTrond Myklebust 		gss_destroy(&new->rpc_auth);
1217eb6dc19dSTrond Myklebust out:
1218eb6dc19dSTrond Myklebust 	return gss_auth;
1219eb6dc19dSTrond Myklebust }
1220eb6dc19dSTrond Myklebust 
1221eb6dc19dSTrond Myklebust static struct rpc_auth *
122282b98ca5SSargun Dhillon gss_create(const struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
1223eb6dc19dSTrond Myklebust {
1224eb6dc19dSTrond Myklebust 	struct gss_auth *gss_auth;
1225ad01b2c6STrond Myklebust 	struct rpc_xprt_switch *xps = rcu_access_pointer(clnt->cl_xpi.xpi_xpswitch);
1226eb6dc19dSTrond Myklebust 
1227eb6dc19dSTrond Myklebust 	while (clnt != clnt->cl_parent) {
1228eb6dc19dSTrond Myklebust 		struct rpc_clnt *parent = clnt->cl_parent;
1229eb6dc19dSTrond Myklebust 		/* Find the original parent for this transport */
1230ad01b2c6STrond Myklebust 		if (rcu_access_pointer(parent->cl_xpi.xpi_xpswitch) != xps)
1231eb6dc19dSTrond Myklebust 			break;
1232eb6dc19dSTrond Myklebust 		clnt = parent;
1233eb6dc19dSTrond Myklebust 	}
1234eb6dc19dSTrond Myklebust 
1235eb6dc19dSTrond Myklebust 	gss_auth = gss_create_hashed(args, clnt);
1236eb6dc19dSTrond Myklebust 	if (IS_ERR(gss_auth))
1237eb6dc19dSTrond Myklebust 		return ERR_CAST(gss_auth);
1238eb6dc19dSTrond Myklebust 	return &gss_auth->rpc_auth;
1239eb6dc19dSTrond Myklebust }
1240eb6dc19dSTrond Myklebust 
1241a652a4bcSTrond Myklebust static struct gss_cred *
1242a652a4bcSTrond Myklebust gss_dup_cred(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
1243a652a4bcSTrond Myklebust {
1244a652a4bcSTrond Myklebust 	struct gss_cred *new;
1245a652a4bcSTrond Myklebust 
1246a652a4bcSTrond Myklebust 	/* Make a copy of the cred so that we can reference count it */
124712a3ad61STrond Myklebust 	new = kzalloc(sizeof(*gss_cred), GFP_NOFS);
1248a652a4bcSTrond Myklebust 	if (new) {
1249a652a4bcSTrond Myklebust 		struct auth_cred acred = {
12508276c902SNeilBrown 			.cred = gss_cred->gc_base.cr_cred,
1251a652a4bcSTrond Myklebust 		};
1252a652a4bcSTrond Myklebust 		struct gss_cl_ctx *ctx =
1253a652a4bcSTrond Myklebust 			rcu_dereference_protected(gss_cred->gc_ctx, 1);
1254a652a4bcSTrond Myklebust 
1255a652a4bcSTrond Myklebust 		rpcauth_init_cred(&new->gc_base, &acred,
1256a652a4bcSTrond Myklebust 				&gss_auth->rpc_auth,
1257a652a4bcSTrond Myklebust 				&gss_nullops);
1258a652a4bcSTrond Myklebust 		new->gc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
1259a652a4bcSTrond Myklebust 		new->gc_service = gss_cred->gc_service;
1260a652a4bcSTrond Myklebust 		new->gc_principal = gss_cred->gc_principal;
1261a652a4bcSTrond Myklebust 		kref_get(&gss_auth->kref);
1262a652a4bcSTrond Myklebust 		rcu_assign_pointer(new->gc_ctx, ctx);
1263a652a4bcSTrond Myklebust 		gss_get_ctx(ctx);
1264a652a4bcSTrond Myklebust 	}
1265a652a4bcSTrond Myklebust 	return new;
1266a652a4bcSTrond Myklebust }
1267a652a4bcSTrond Myklebust 
12680df7fb74STrond Myklebust /*
1269a652a4bcSTrond Myklebust  * gss_send_destroy_context will cause the RPCSEC_GSS to send a NULL RPC call
12700df7fb74STrond Myklebust  * to the server with the GSS control procedure field set to
12710df7fb74STrond Myklebust  * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
12720df7fb74STrond Myklebust  * all RPCSEC_GSS state associated with that context.
12730df7fb74STrond Myklebust  */
1274a652a4bcSTrond Myklebust static void
1275a652a4bcSTrond Myklebust gss_send_destroy_context(struct rpc_cred *cred)
12760df7fb74STrond Myklebust {
12770df7fb74STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
12780df7fb74STrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
1279c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx = rcu_dereference_protected(gss_cred->gc_ctx, 1);
1280a652a4bcSTrond Myklebust 	struct gss_cred *new;
12810df7fb74STrond Myklebust 	struct rpc_task *task;
12820df7fb74STrond Myklebust 
1283a652a4bcSTrond Myklebust 	new = gss_dup_cred(gss_auth, gss_cred);
1284a652a4bcSTrond Myklebust 	if (new) {
1285c5e6aecdSJeff Layton 		ctx->gc_proc = RPC_GSS_PROC_DESTROY;
12860df7fb74STrond Myklebust 
1287a652a4bcSTrond Myklebust 		task = rpc_call_null(gss_auth->client, &new->gc_base,
1288a652a4bcSTrond Myklebust 				RPC_TASK_ASYNC|RPC_TASK_SOFT);
12890df7fb74STrond Myklebust 		if (!IS_ERR(task))
12900df7fb74STrond Myklebust 			rpc_put_task(task);
12910df7fb74STrond Myklebust 
1292a652a4bcSTrond Myklebust 		put_rpccred(&new->gc_base);
1293a652a4bcSTrond Myklebust 	}
12940df7fb74STrond Myklebust }
12950df7fb74STrond Myklebust 
12960df7fb74STrond Myklebust /* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
12971da177e4SLinus Torvalds  * to create a new cred or context, so they check that things have been
12981da177e4SLinus Torvalds  * allocated before freeing them. */
12991da177e4SLinus Torvalds static void
13005d28dc82STrond Myklebust gss_do_free_ctx(struct gss_cl_ctx *ctx)
13011da177e4SLinus Torvalds {
13020d8a3746STrond Myklebust 	gss_delete_sec_context(&ctx->gc_gss_ctx);
13031da177e4SLinus Torvalds 	kfree(ctx->gc_wire_ctx.data);
13042004c726SJeff Layton 	kfree(ctx->gc_acceptor.data);
13051da177e4SLinus Torvalds 	kfree(ctx);
13061da177e4SLinus Torvalds }
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds static void
13095d28dc82STrond Myklebust gss_free_ctx_callback(struct rcu_head *head)
13105d28dc82STrond Myklebust {
13115d28dc82STrond Myklebust 	struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
13125d28dc82STrond Myklebust 	gss_do_free_ctx(ctx);
13135d28dc82STrond Myklebust }
13145d28dc82STrond Myklebust 
13155d28dc82STrond Myklebust static void
13165d28dc82STrond Myklebust gss_free_ctx(struct gss_cl_ctx *ctx)
13175d28dc82STrond Myklebust {
13185d28dc82STrond Myklebust 	call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
13195d28dc82STrond Myklebust }
13205d28dc82STrond Myklebust 
13215d28dc82STrond Myklebust static void
132231be5bf1STrond Myklebust gss_free_cred(struct gss_cred *gss_cred)
13231da177e4SLinus Torvalds {
132431be5bf1STrond Myklebust 	kfree(gss_cred);
132531be5bf1STrond Myklebust }
13261da177e4SLinus Torvalds 
132731be5bf1STrond Myklebust static void
132831be5bf1STrond Myklebust gss_free_cred_callback(struct rcu_head *head)
132931be5bf1STrond Myklebust {
133031be5bf1STrond Myklebust 	struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
133131be5bf1STrond Myklebust 	gss_free_cred(gss_cred);
133231be5bf1STrond Myklebust }
13331da177e4SLinus Torvalds 
133431be5bf1STrond Myklebust static void
13356dcd3926SJeff Layton gss_destroy_nullcred(struct rpc_cred *cred)
133631be5bf1STrond Myklebust {
13375d28dc82STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
13380285ed1fSTrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
1339c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx = rcu_dereference_protected(gss_cred->gc_ctx, 1);
13405d28dc82STrond Myklebust 
1341a9b3cd7fSStephen Hemminger 	RCU_INIT_POINTER(gss_cred->gc_ctx, NULL);
134297f68c6bSNeilBrown 	put_cred(cred->cr_cred);
134331be5bf1STrond Myklebust 	call_rcu(&cred->cr_rcu, gss_free_cred_callback);
13445d28dc82STrond Myklebust 	if (ctx)
13455d28dc82STrond Myklebust 		gss_put_ctx(ctx);
13469eb2ddb4STrond Myklebust 	gss_put_auth(gss_auth);
13471da177e4SLinus Torvalds }
13481da177e4SLinus Torvalds 
13496dcd3926SJeff Layton static void
13506dcd3926SJeff Layton gss_destroy_cred(struct rpc_cred *cred)
13516dcd3926SJeff Layton {
13526dcd3926SJeff Layton 
1353a652a4bcSTrond Myklebust 	if (test_and_clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0)
1354a652a4bcSTrond Myklebust 		gss_send_destroy_context(cred);
13556dcd3926SJeff Layton 	gss_destroy_nullcred(cred);
13566dcd3926SJeff Layton }
13576dcd3926SJeff Layton 
1358a960f8d6SFrank Sorenson static int
1359a960f8d6SFrank Sorenson gss_hash_cred(struct auth_cred *acred, unsigned int hashbits)
1360a960f8d6SFrank Sorenson {
13618276c902SNeilBrown 	return hash_64(from_kuid(&init_user_ns, acred->cred->fsuid), hashbits);
1362a960f8d6SFrank Sorenson }
1363a960f8d6SFrank Sorenson 
13641da177e4SLinus Torvalds /*
13651da177e4SLinus Torvalds  * Lookup RPCSEC_GSS cred for the current process
13661da177e4SLinus Torvalds  */
13671da177e4SLinus Torvalds static struct rpc_cred *
13688a317760STrond Myklebust gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
13691da177e4SLinus Torvalds {
13703c6e0bc8SJeff Layton 	return rpcauth_lookup_credcache(auth, acred, flags, GFP_NOFS);
13711da177e4SLinus Torvalds }
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds static struct rpc_cred *
13743c6e0bc8SJeff Layton gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags, gfp_t gfp)
13751da177e4SLinus Torvalds {
13761da177e4SLinus Torvalds 	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
13771da177e4SLinus Torvalds 	struct gss_cred	*cred = NULL;
13781da177e4SLinus Torvalds 	int err = -ENOMEM;
13791da177e4SLinus Torvalds 
13803c6e0bc8SJeff Layton 	if (!(cred = kzalloc(sizeof(*cred), gfp)))
13811da177e4SLinus Torvalds 		goto out_err;
13821da177e4SLinus Torvalds 
13835fe4755eSTrond Myklebust 	rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
13841da177e4SLinus Torvalds 	/*
13851da177e4SLinus Torvalds 	 * Note: in order to force a call to call_refresh(), we deliberately
13861da177e4SLinus Torvalds 	 * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
13871da177e4SLinus Torvalds 	 */
1388fc432dd9STrond Myklebust 	cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
13891da177e4SLinus Torvalds 	cred->gc_service = gss_auth->service;
139068c97153STrond Myklebust 	cred->gc_principal = acred->principal;
13910285ed1fSTrond Myklebust 	kref_get(&gss_auth->kref);
13921da177e4SLinus Torvalds 	return &cred->gc_base;
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds out_err:
13951da177e4SLinus Torvalds 	return ERR_PTR(err);
13961da177e4SLinus Torvalds }
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds static int
1399fba3bad4STrond Myklebust gss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
1400fba3bad4STrond Myklebust {
1401fba3bad4STrond Myklebust 	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
1402fba3bad4STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
1403fba3bad4STrond Myklebust 	int err;
1404fba3bad4STrond Myklebust 
1405fba3bad4STrond Myklebust 	do {
1406fba3bad4STrond Myklebust 		err = gss_create_upcall(gss_auth, gss_cred);
1407fba3bad4STrond Myklebust 	} while (err == -EAGAIN);
1408fba3bad4STrond Myklebust 	return err;
1409fba3bad4STrond Myklebust }
1410fba3bad4STrond Myklebust 
1411a0337d1dSJeff Layton static char *
1412a0337d1dSJeff Layton gss_stringify_acceptor(struct rpc_cred *cred)
1413a0337d1dSJeff Layton {
1414c5e6aecdSJeff Layton 	char *string = NULL;
1415a0337d1dSJeff Layton 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1416c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx;
1417b3ecba09SJeff Layton 	unsigned int len;
1418c5e6aecdSJeff Layton 	struct xdr_netobj *acceptor;
1419c5e6aecdSJeff Layton 
1420c5e6aecdSJeff Layton 	rcu_read_lock();
1421c5e6aecdSJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
1422c5e6aecdSJeff Layton 	if (!ctx)
1423c5e6aecdSJeff Layton 		goto out;
1424c5e6aecdSJeff Layton 
1425b3ecba09SJeff Layton 	len = ctx->gc_acceptor.len;
1426b3ecba09SJeff Layton 	rcu_read_unlock();
1427a0337d1dSJeff Layton 
1428a0337d1dSJeff Layton 	/* no point if there's no string */
1429b3ecba09SJeff Layton 	if (!len)
1430b3ecba09SJeff Layton 		return NULL;
1431b3ecba09SJeff Layton realloc:
1432b3ecba09SJeff Layton 	string = kmalloc(len + 1, GFP_KERNEL);
1433a0337d1dSJeff Layton 	if (!string)
1434b3ecba09SJeff Layton 		return NULL;
1435b3ecba09SJeff Layton 
1436b3ecba09SJeff Layton 	rcu_read_lock();
1437b3ecba09SJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
1438b3ecba09SJeff Layton 
1439b3ecba09SJeff Layton 	/* did the ctx disappear or was it replaced by one with no acceptor? */
1440b3ecba09SJeff Layton 	if (!ctx || !ctx->gc_acceptor.len) {
1441b3ecba09SJeff Layton 		kfree(string);
1442b3ecba09SJeff Layton 		string = NULL;
1443c5e6aecdSJeff Layton 		goto out;
1444b3ecba09SJeff Layton 	}
1445b3ecba09SJeff Layton 
1446b3ecba09SJeff Layton 	acceptor = &ctx->gc_acceptor;
1447b3ecba09SJeff Layton 
1448b3ecba09SJeff Layton 	/*
1449b3ecba09SJeff Layton 	 * Did we find a new acceptor that's longer than the original? Allocate
1450b3ecba09SJeff Layton 	 * a longer buffer and try again.
1451b3ecba09SJeff Layton 	 */
1452b3ecba09SJeff Layton 	if (len < acceptor->len) {
1453b3ecba09SJeff Layton 		len = acceptor->len;
1454b3ecba09SJeff Layton 		rcu_read_unlock();
1455b3ecba09SJeff Layton 		kfree(string);
1456b3ecba09SJeff Layton 		goto realloc;
1457b3ecba09SJeff Layton 	}
1458a0337d1dSJeff Layton 
1459a0337d1dSJeff Layton 	memcpy(string, acceptor->data, acceptor->len);
1460a0337d1dSJeff Layton 	string[acceptor->len] = '\0';
1461c5e6aecdSJeff Layton out:
1462c5e6aecdSJeff Layton 	rcu_read_unlock();
1463a0337d1dSJeff Layton 	return string;
1464a0337d1dSJeff Layton }
1465a0337d1dSJeff Layton 
14664de6caa2SAndy Adamson /*
14674de6caa2SAndy Adamson  * Returns -EACCES if GSS context is NULL or will expire within the
14684de6caa2SAndy Adamson  * timeout (miliseconds)
14694de6caa2SAndy Adamson  */
14704de6caa2SAndy Adamson static int
14714de6caa2SAndy Adamson gss_key_timeout(struct rpc_cred *rc)
14724de6caa2SAndy Adamson {
14734de6caa2SAndy Adamson 	struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
1474c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx;
1475cc6a7aabSArnd Bergmann 	unsigned long timeout = jiffies + (gss_key_expire_timeo * HZ);
1476cc6a7aabSArnd Bergmann 	int ret = 0;
14774de6caa2SAndy Adamson 
1478c5e6aecdSJeff Layton 	rcu_read_lock();
1479c5e6aecdSJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
1480cc6a7aabSArnd Bergmann 	if (!ctx || time_after(timeout, ctx->gc_expiry))
1481cc6a7aabSArnd Bergmann 		ret = -EACCES;
1482c5e6aecdSJeff Layton 	rcu_read_unlock();
1483cc6a7aabSArnd Bergmann 
1484cc6a7aabSArnd Bergmann 	return ret;
14854de6caa2SAndy Adamson }
14864de6caa2SAndy Adamson 
1487fba3bad4STrond Myklebust static int
14888a317760STrond Myklebust gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
14891da177e4SLinus Torvalds {
14901da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
1491c5e6aecdSJeff Layton 	struct gss_cl_ctx *ctx;
14924de6caa2SAndy Adamson 	int ret;
14931da177e4SLinus Torvalds 
1494cd019f75STrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
14958a317760STrond Myklebust 		goto out;
14961da177e4SLinus Torvalds 	/* Don't match with creds that have expired. */
1497c5e6aecdSJeff Layton 	rcu_read_lock();
1498c5e6aecdSJeff Layton 	ctx = rcu_dereference(gss_cred->gc_ctx);
1499c5e6aecdSJeff Layton 	if (!ctx || time_after(jiffies, ctx->gc_expiry)) {
1500c5e6aecdSJeff Layton 		rcu_read_unlock();
1501cd019f75STrond Myklebust 		return 0;
1502c5e6aecdSJeff Layton 	}
1503c5e6aecdSJeff Layton 	rcu_read_unlock();
1504cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags))
15051da177e4SLinus Torvalds 		return 0;
15068a317760STrond Myklebust out:
150768c97153STrond Myklebust 	if (acred->principal != NULL) {
150868c97153STrond Myklebust 		if (gss_cred->gc_principal == NULL)
150968c97153STrond Myklebust 			return 0;
15104de6caa2SAndy Adamson 		ret = strcmp(acred->principal, gss_cred->gc_principal) == 0;
1511ddf529eeSNeilBrown 	} else {
151268c97153STrond Myklebust 		if (gss_cred->gc_principal != NULL)
15137c67db3aSTrond Myklebust 			return 0;
151404d1532bSNeilBrown 		ret = uid_eq(rc->cr_cred->fsuid, acred->cred->fsuid);
15154de6caa2SAndy Adamson 	}
15164de6caa2SAndy Adamson 	return ret;
15171da177e4SLinus Torvalds }
15181da177e4SLinus Torvalds 
15191da177e4SLinus Torvalds /*
15201da177e4SLinus Torvalds  * Marshal credentials.
1521e8680a24SChuck Lever  *
1522e8680a24SChuck Lever  * The expensive part is computing the verifier. We can't cache a
1523e8680a24SChuck Lever  * pre-computed version of the verifier because the seqno, which
1524e8680a24SChuck Lever  * is different every time, is included in the MIC.
15251da177e4SLinus Torvalds  */
1526e8680a24SChuck Lever static int gss_marshal(struct rpc_task *task, struct xdr_stream *xdr)
15271da177e4SLinus Torvalds {
1528a17c2153STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1529a17c2153STrond Myklebust 	struct rpc_cred *cred = req->rq_cred;
15301da177e4SLinus Torvalds 	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
15311da177e4SLinus Torvalds 						 gc_base);
15321da177e4SLinus Torvalds 	struct gss_cl_ctx	*ctx = gss_cred_get_ctx(cred);
1533e8680a24SChuck Lever 	__be32		*p, *cred_len;
15341da177e4SLinus Torvalds 	u32             maj_stat = 0;
15351da177e4SLinus Torvalds 	struct xdr_netobj mic;
15361da177e4SLinus Torvalds 	struct kvec	iov;
15371da177e4SLinus Torvalds 	struct xdr_buf	verf_buf;
15380c77668dSChuck Lever 	int status;
15391da177e4SLinus Torvalds 
1540e8680a24SChuck Lever 	/* Credential */
1541e8680a24SChuck Lever 
1542e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, 7 * sizeof(*p) +
1543e8680a24SChuck Lever 			      ctx->gc_wire_ctx.len);
1544e8680a24SChuck Lever 	if (!p)
15450c77668dSChuck Lever 		goto marshal_failed;
1546e8680a24SChuck Lever 	*p++ = rpc_auth_gss;
15471da177e4SLinus Torvalds 	cred_len = p++;
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	spin_lock(&ctx->gc_seq_lock);
155097b78ae9STrond Myklebust 	req->rq_seqno = (ctx->gc_seq < MAXSEQ) ? ctx->gc_seq++ : MAXSEQ;
15511da177e4SLinus Torvalds 	spin_unlock(&ctx->gc_seq_lock);
155297b78ae9STrond Myklebust 	if (req->rq_seqno == MAXSEQ)
15530c77668dSChuck Lever 		goto expired;
15540c77668dSChuck Lever 	trace_rpcgss_seqno(task);
15551da177e4SLinus Torvalds 
1556e8680a24SChuck Lever 	*p++ = cpu_to_be32(RPC_GSS_VERSION);
1557e8680a24SChuck Lever 	*p++ = cpu_to_be32(ctx->gc_proc);
1558e8680a24SChuck Lever 	*p++ = cpu_to_be32(req->rq_seqno);
1559e8680a24SChuck Lever 	*p++ = cpu_to_be32(gss_cred->gc_service);
15601da177e4SLinus Torvalds 	p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
1561e8680a24SChuck Lever 	*cred_len = cpu_to_be32((p - (cred_len + 1)) << 2);
1562e8680a24SChuck Lever 
1563e8680a24SChuck Lever 	/* Verifier */
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 	/* We compute the checksum for the verifier over the xdr-encoded bytes
15661da177e4SLinus Torvalds 	 * starting with the xid and ending at the end of the credential: */
1567067fb11bSChuck Lever 	iov.iov_base = req->rq_snd_buf.head[0].iov_base;
15681da177e4SLinus Torvalds 	iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
15691da177e4SLinus Torvalds 	xdr_buf_from_iov(&iov, &verf_buf);
15701da177e4SLinus Torvalds 
1571e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, sizeof(*p));
1572e8680a24SChuck Lever 	if (!p)
15730c77668dSChuck Lever 		goto marshal_failed;
1574e8680a24SChuck Lever 	*p++ = rpc_auth_gss;
15751da177e4SLinus Torvalds 	mic.data = (u8 *)(p + 1);
157600fd6e14SJ. Bruce Fields 	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
1577e8680a24SChuck Lever 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
15780c77668dSChuck Lever 		goto expired;
1579e8680a24SChuck Lever 	else if (maj_stat != 0)
15800c77668dSChuck Lever 		goto bad_mic;
1581e8680a24SChuck Lever 	if (xdr_stream_encode_opaque_inline(xdr, (void **)&p, mic.len) < 0)
15820c77668dSChuck Lever 		goto marshal_failed;
15830c77668dSChuck Lever 	status = 0;
15840c77668dSChuck Lever out:
15851da177e4SLinus Torvalds 	gss_put_ctx(ctx);
15860c77668dSChuck Lever 	return status;
15870c77668dSChuck Lever expired:
158897b78ae9STrond Myklebust 	clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
15890c77668dSChuck Lever 	status = -EKEYEXPIRED;
15900c77668dSChuck Lever 	goto out;
15910c77668dSChuck Lever marshal_failed:
15920c77668dSChuck Lever 	status = -EMSGSIZE;
15930c77668dSChuck Lever 	goto out;
15940c77668dSChuck Lever bad_mic:
15950c77668dSChuck Lever 	trace_rpcgss_get_mic(task, maj_stat);
15960c77668dSChuck Lever 	status = -EIO;
15970c77668dSChuck Lever 	goto out;
15981da177e4SLinus Torvalds }
15991da177e4SLinus Torvalds 
1600cd019f75STrond Myklebust static int gss_renew_cred(struct rpc_task *task)
1601cd019f75STrond Myklebust {
1602a17c2153STrond Myklebust 	struct rpc_cred *oldcred = task->tk_rqstp->rq_cred;
1603cd019f75STrond Myklebust 	struct gss_cred *gss_cred = container_of(oldcred,
1604cd019f75STrond Myklebust 						 struct gss_cred,
1605cd019f75STrond Myklebust 						 gc_base);
1606cd019f75STrond Myklebust 	struct rpc_auth *auth = oldcred->cr_auth;
1607cd019f75STrond Myklebust 	struct auth_cred acred = {
160897f68c6bSNeilBrown 		.cred = oldcred->cr_cred,
160968c97153STrond Myklebust 		.principal = gss_cred->gc_principal,
1610cd019f75STrond Myklebust 	};
1611cd019f75STrond Myklebust 	struct rpc_cred *new;
1612cd019f75STrond Myklebust 
1613cd019f75STrond Myklebust 	new = gss_lookup_cred(auth, &acred, RPCAUTH_LOOKUP_NEW);
1614cd019f75STrond Myklebust 	if (IS_ERR(new))
1615cd019f75STrond Myklebust 		return PTR_ERR(new);
1616a17c2153STrond Myklebust 	task->tk_rqstp->rq_cred = new;
1617cd019f75STrond Myklebust 	put_rpccred(oldcred);
1618cd019f75STrond Myklebust 	return 0;
1619cd019f75STrond Myklebust }
1620cd019f75STrond Myklebust 
1621126e216aSTrond Myklebust static int gss_cred_is_negative_entry(struct rpc_cred *cred)
1622126e216aSTrond Myklebust {
1623126e216aSTrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEGATIVE, &cred->cr_flags)) {
1624126e216aSTrond Myklebust 		unsigned long now = jiffies;
1625126e216aSTrond Myklebust 		unsigned long begin, expire;
1626126e216aSTrond Myklebust 		struct gss_cred *gss_cred;
1627126e216aSTrond Myklebust 
1628126e216aSTrond Myklebust 		gss_cred = container_of(cred, struct gss_cred, gc_base);
1629126e216aSTrond Myklebust 		begin = gss_cred->gc_upcall_timestamp;
1630126e216aSTrond Myklebust 		expire = begin + gss_expired_cred_retry_delay * HZ;
1631126e216aSTrond Myklebust 
1632126e216aSTrond Myklebust 		if (time_in_range_open(now, begin, expire))
1633126e216aSTrond Myklebust 			return 1;
1634126e216aSTrond Myklebust 	}
1635126e216aSTrond Myklebust 	return 0;
1636126e216aSTrond Myklebust }
1637126e216aSTrond Myklebust 
16381da177e4SLinus Torvalds /*
16391da177e4SLinus Torvalds * Refresh credentials. XXX - finish
16401da177e4SLinus Torvalds */
16411da177e4SLinus Torvalds static int
16421da177e4SLinus Torvalds gss_refresh(struct rpc_task *task)
16431da177e4SLinus Torvalds {
1644a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
1645cd019f75STrond Myklebust 	int ret = 0;
16461da177e4SLinus Torvalds 
1647126e216aSTrond Myklebust 	if (gss_cred_is_negative_entry(cred))
1648126e216aSTrond Myklebust 		return -EKEYEXPIRED;
1649126e216aSTrond Myklebust 
1650cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
1651cd019f75STrond Myklebust 			!test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags)) {
1652cd019f75STrond Myklebust 		ret = gss_renew_cred(task);
1653cd019f75STrond Myklebust 		if (ret < 0)
1654cd019f75STrond Myklebust 			goto out;
1655a17c2153STrond Myklebust 		cred = task->tk_rqstp->rq_cred;
1656cd019f75STrond Myklebust 	}
1657cd019f75STrond Myklebust 
1658cd019f75STrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
1659cd019f75STrond Myklebust 		ret = gss_refresh_upcall(task);
1660cd019f75STrond Myklebust out:
1661cd019f75STrond Myklebust 	return ret;
16621da177e4SLinus Torvalds }
16631da177e4SLinus Torvalds 
16640df7fb74STrond Myklebust /* Dummy refresh routine: used only when destroying the context */
16650df7fb74STrond Myklebust static int
16660df7fb74STrond Myklebust gss_refresh_null(struct rpc_task *task)
16670df7fb74STrond Myklebust {
1668c297c8b9SAndy Adamson 	return 0;
16690df7fb74STrond Myklebust }
16700df7fb74STrond Myklebust 
1671a0584ee9SChuck Lever static int
1672a0584ee9SChuck Lever gss_validate(struct rpc_task *task, struct xdr_stream *xdr)
16731da177e4SLinus Torvalds {
1674a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
16751da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1676a0584ee9SChuck Lever 	__be32		*p, *seq = NULL;
16771da177e4SLinus Torvalds 	struct kvec	iov;
16781da177e4SLinus Torvalds 	struct xdr_buf	verf_buf;
16791da177e4SLinus Torvalds 	struct xdr_netobj mic;
1680a0584ee9SChuck Lever 	u32		len, maj_stat;
1681a0584ee9SChuck Lever 	int		status;
16821da177e4SLinus Torvalds 
1683a0584ee9SChuck Lever 	p = xdr_inline_decode(xdr, 2 * sizeof(*p));
1684a0584ee9SChuck Lever 	if (!p)
1685a0584ee9SChuck Lever 		goto validate_failed;
1686a0584ee9SChuck Lever 	if (*p++ != rpc_auth_gss)
1687a0584ee9SChuck Lever 		goto validate_failed;
1688a0584ee9SChuck Lever 	len = be32_to_cpup(p);
1689a0584ee9SChuck Lever 	if (len > RPC_MAX_AUTH_SIZE)
1690a0584ee9SChuck Lever 		goto validate_failed;
1691a0584ee9SChuck Lever 	p = xdr_inline_decode(xdr, len);
1692a0584ee9SChuck Lever 	if (!p)
1693a0584ee9SChuck Lever 		goto validate_failed;
16941da177e4SLinus Torvalds 
16952876a344SJ. Bruce Fields 	seq = kmalloc(4, GFP_NOFS);
16962876a344SJ. Bruce Fields 	if (!seq)
1697a0584ee9SChuck Lever 		goto validate_failed;
1698a0584ee9SChuck Lever 	*seq = cpu_to_be32(task->tk_rqstp->rq_seqno);
16992876a344SJ. Bruce Fields 	iov.iov_base = seq;
17002876a344SJ. Bruce Fields 	iov.iov_len = 4;
17011da177e4SLinus Torvalds 	xdr_buf_from_iov(&iov, &verf_buf);
17021da177e4SLinus Torvalds 	mic.data = (u8 *)p;
17031da177e4SLinus Torvalds 	mic.len = len;
170400fd6e14SJ. Bruce Fields 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
17051da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1706fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
1707a0584ee9SChuck Lever 	if (maj_stat)
1708a0584ee9SChuck Lever 		goto bad_mic;
1709a0584ee9SChuck Lever 
171024b2605bSJ. Bruce Fields 	/* We leave it to unwrap to calculate au_rslack. For now we just
171124b2605bSJ. Bruce Fields 	 * calculate the length of the verifier: */
17121be27f36STrond Myklebust 	cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
1713a0584ee9SChuck Lever 	status = 0;
1714a0584ee9SChuck Lever out:
17151da177e4SLinus Torvalds 	gss_put_ctx(ctx);
17162876a344SJ. Bruce Fields 	kfree(seq);
1717a0584ee9SChuck Lever 	return status;
1718a0584ee9SChuck Lever 
1719a0584ee9SChuck Lever validate_failed:
1720a0584ee9SChuck Lever 	status = -EIO;
1721a0584ee9SChuck Lever 	goto out;
1722a0584ee9SChuck Lever bad_mic:
17230c77668dSChuck Lever 	trace_rpcgss_verify_mic(task, maj_stat);
1724a0584ee9SChuck Lever 	status = -EACCES;
1725a0584ee9SChuck Lever 	goto out;
17261da177e4SLinus Torvalds }
17271da177e4SLinus Torvalds 
1728d162372aSChuck Lever static noinline_for_stack int
1729d162372aSChuck Lever gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1730e8680a24SChuck Lever 		   struct rpc_task *task, struct xdr_stream *xdr)
17319f06c719SChuck Lever {
1732e8680a24SChuck Lever 	struct rpc_rqst *rqstp = task->tk_rqstp;
1733e8680a24SChuck Lever 	struct xdr_buf integ_buf, *snd_buf = &rqstp->rq_snd_buf;
17341da177e4SLinus Torvalds 	struct xdr_netobj mic;
1735e8680a24SChuck Lever 	__be32 *p, *integ_len;
1736e8680a24SChuck Lever 	u32 offset, maj_stat;
17371da177e4SLinus Torvalds 
1738e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, 2 * sizeof(*p));
1739e8680a24SChuck Lever 	if (!p)
1740e8680a24SChuck Lever 		goto wrap_failed;
17411da177e4SLinus Torvalds 	integ_len = p++;
1742e8680a24SChuck Lever 	*p = cpu_to_be32(rqstp->rq_seqno);
1743e8680a24SChuck Lever 
1744e8680a24SChuck Lever 	if (rpcauth_wrap_req_encode(task, xdr))
1745e8680a24SChuck Lever 		goto wrap_failed;
1746e8680a24SChuck Lever 
17471da177e4SLinus Torvalds 	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
17481da177e4SLinus Torvalds 	if (xdr_buf_subsegment(snd_buf, &integ_buf,
17491da177e4SLinus Torvalds 				offset, snd_buf->len - offset))
1750e8680a24SChuck Lever 		goto wrap_failed;
1751e8680a24SChuck Lever 	*integ_len = cpu_to_be32(integ_buf.len);
17521da177e4SLinus Torvalds 
1753e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, 0);
1754e8680a24SChuck Lever 	if (!p)
1755e8680a24SChuck Lever 		goto wrap_failed;
17561da177e4SLinus Torvalds 	mic.data = (u8 *)(p + 1);
175700fd6e14SJ. Bruce Fields 	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
17581da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1759fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
17601da177e4SLinus Torvalds 	else if (maj_stat)
17610c77668dSChuck Lever 		goto bad_mic;
1762e8680a24SChuck Lever 	/* Check that the trailing MIC fit in the buffer, after the fact */
1763e8680a24SChuck Lever 	if (xdr_stream_encode_opaque_inline(xdr, (void **)&p, mic.len) < 0)
1764e8680a24SChuck Lever 		goto wrap_failed;
17651da177e4SLinus Torvalds 	return 0;
1766e8680a24SChuck Lever wrap_failed:
1767e8680a24SChuck Lever 	return -EMSGSIZE;
17680c77668dSChuck Lever bad_mic:
17690c77668dSChuck Lever 	trace_rpcgss_get_mic(task, maj_stat);
17700c77668dSChuck Lever 	return -EIO;
17711da177e4SLinus Torvalds }
17721da177e4SLinus Torvalds 
17732d2da60cSJ. Bruce Fields static void
17742d2da60cSJ. Bruce Fields priv_release_snd_buf(struct rpc_rqst *rqstp)
17752d2da60cSJ. Bruce Fields {
17762d2da60cSJ. Bruce Fields 	int i;
17772d2da60cSJ. Bruce Fields 
17782d2da60cSJ. Bruce Fields 	for (i=0; i < rqstp->rq_enc_pages_num; i++)
17792d2da60cSJ. Bruce Fields 		__free_page(rqstp->rq_enc_pages[i]);
17802d2da60cSJ. Bruce Fields 	kfree(rqstp->rq_enc_pages);
17818dae5398SChuck Lever 	rqstp->rq_release_snd_buf = NULL;
17822d2da60cSJ. Bruce Fields }
17832d2da60cSJ. Bruce Fields 
17842d2da60cSJ. Bruce Fields static int
17852d2da60cSJ. Bruce Fields alloc_enc_pages(struct rpc_rqst *rqstp)
17862d2da60cSJ. Bruce Fields {
17872d2da60cSJ. Bruce Fields 	struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
17882d2da60cSJ. Bruce Fields 	int first, last, i;
17892d2da60cSJ. Bruce Fields 
17908dae5398SChuck Lever 	if (rqstp->rq_release_snd_buf)
17918dae5398SChuck Lever 		rqstp->rq_release_snd_buf(rqstp);
17928dae5398SChuck Lever 
17932d2da60cSJ. Bruce Fields 	if (snd_buf->page_len == 0) {
17942d2da60cSJ. Bruce Fields 		rqstp->rq_enc_pages_num = 0;
17952d2da60cSJ. Bruce Fields 		return 0;
17962d2da60cSJ. Bruce Fields 	}
17972d2da60cSJ. Bruce Fields 
179809cbfeafSKirill A. Shutemov 	first = snd_buf->page_base >> PAGE_SHIFT;
179909cbfeafSKirill A. Shutemov 	last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_SHIFT;
18002d2da60cSJ. Bruce Fields 	rqstp->rq_enc_pages_num = last - first + 1 + 1;
18012d2da60cSJ. Bruce Fields 	rqstp->rq_enc_pages
18026da2ec56SKees Cook 		= kmalloc_array(rqstp->rq_enc_pages_num,
18036da2ec56SKees Cook 				sizeof(struct page *),
18042d2da60cSJ. Bruce Fields 				GFP_NOFS);
18052d2da60cSJ. Bruce Fields 	if (!rqstp->rq_enc_pages)
18062d2da60cSJ. Bruce Fields 		goto out;
18072d2da60cSJ. Bruce Fields 	for (i=0; i < rqstp->rq_enc_pages_num; i++) {
18082d2da60cSJ. Bruce Fields 		rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
18092d2da60cSJ. Bruce Fields 		if (rqstp->rq_enc_pages[i] == NULL)
18102d2da60cSJ. Bruce Fields 			goto out_free;
18112d2da60cSJ. Bruce Fields 	}
18122d2da60cSJ. Bruce Fields 	rqstp->rq_release_snd_buf = priv_release_snd_buf;
18132d2da60cSJ. Bruce Fields 	return 0;
18142d2da60cSJ. Bruce Fields out_free:
1815cdead7cfSTrond Myklebust 	rqstp->rq_enc_pages_num = i;
1816cdead7cfSTrond Myklebust 	priv_release_snd_buf(rqstp);
18172d2da60cSJ. Bruce Fields out:
18182d2da60cSJ. Bruce Fields 	return -EAGAIN;
18192d2da60cSJ. Bruce Fields }
18202d2da60cSJ. Bruce Fields 
1821d162372aSChuck Lever static noinline_for_stack int
1822d162372aSChuck Lever gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1823e8680a24SChuck Lever 		  struct rpc_task *task, struct xdr_stream *xdr)
18242d2da60cSJ. Bruce Fields {
1825e8680a24SChuck Lever 	struct rpc_rqst *rqstp = task->tk_rqstp;
18262d2da60cSJ. Bruce Fields 	struct xdr_buf	*snd_buf = &rqstp->rq_snd_buf;
1827e8680a24SChuck Lever 	u32		pad, offset, maj_stat;
18282d2da60cSJ. Bruce Fields 	int		status;
1829e8680a24SChuck Lever 	__be32		*p, *opaque_len;
18302d2da60cSJ. Bruce Fields 	struct page	**inpages;
18312d2da60cSJ. Bruce Fields 	int		first;
18322d2da60cSJ. Bruce Fields 	struct kvec	*iov;
18332d2da60cSJ. Bruce Fields 
1834e8680a24SChuck Lever 	status = -EIO;
1835e8680a24SChuck Lever 	p = xdr_reserve_space(xdr, 2 * sizeof(*p));
1836e8680a24SChuck Lever 	if (!p)
1837e8680a24SChuck Lever 		goto wrap_failed;
18382d2da60cSJ. Bruce Fields 	opaque_len = p++;
1839e8680a24SChuck Lever 	*p = cpu_to_be32(rqstp->rq_seqno);
18402d2da60cSJ. Bruce Fields 
1841e8680a24SChuck Lever 	if (rpcauth_wrap_req_encode(task, xdr))
1842e8680a24SChuck Lever 		goto wrap_failed;
18432d2da60cSJ. Bruce Fields 
18442d2da60cSJ. Bruce Fields 	status = alloc_enc_pages(rqstp);
1845e8680a24SChuck Lever 	if (unlikely(status))
1846e8680a24SChuck Lever 		goto wrap_failed;
184709cbfeafSKirill A. Shutemov 	first = snd_buf->page_base >> PAGE_SHIFT;
18482d2da60cSJ. Bruce Fields 	inpages = snd_buf->pages + first;
18492d2da60cSJ. Bruce Fields 	snd_buf->pages = rqstp->rq_enc_pages;
185009cbfeafSKirill A. Shutemov 	snd_buf->page_base -= first << PAGE_SHIFT;
18517561042fSKevin Coffman 	/*
1852e8680a24SChuck Lever 	 * Move the tail into its own page, in case gss_wrap needs
1853e8680a24SChuck Lever 	 * more space in the head when wrapping.
18547561042fSKevin Coffman 	 *
1855e8680a24SChuck Lever 	 * Still... Why can't gss_wrap just slide the tail down?
18567561042fSKevin Coffman 	 */
18572d2da60cSJ. Bruce Fields 	if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
1858e8680a24SChuck Lever 		char *tmp;
1859e8680a24SChuck Lever 
18602d2da60cSJ. Bruce Fields 		tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
18612d2da60cSJ. Bruce Fields 		memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
18622d2da60cSJ. Bruce Fields 		snd_buf->tail[0].iov_base = tmp;
18632d2da60cSJ. Bruce Fields 	}
1864e8680a24SChuck Lever 	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
186500fd6e14SJ. Bruce Fields 	maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
18667561042fSKevin Coffman 	/* slack space should prevent this ever happening: */
1867e8680a24SChuck Lever 	if (unlikely(snd_buf->len > snd_buf->buflen))
1868e8680a24SChuck Lever 		goto wrap_failed;
18692d2da60cSJ. Bruce Fields 	/* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
18702d2da60cSJ. Bruce Fields 	 * done anyway, so it's safe to put the request on the wire: */
18712d2da60cSJ. Bruce Fields 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1872fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
18732d2da60cSJ. Bruce Fields 	else if (maj_stat)
18740c77668dSChuck Lever 		goto bad_wrap;
18752d2da60cSJ. Bruce Fields 
1876e8680a24SChuck Lever 	*opaque_len = cpu_to_be32(snd_buf->len - offset);
1877e8680a24SChuck Lever 	/* guess whether the pad goes into the head or the tail: */
18782d2da60cSJ. Bruce Fields 	if (snd_buf->page_len || snd_buf->tail[0].iov_len)
18792d2da60cSJ. Bruce Fields 		iov = snd_buf->tail;
18802d2da60cSJ. Bruce Fields 	else
18812d2da60cSJ. Bruce Fields 		iov = snd_buf->head;
18822d2da60cSJ. Bruce Fields 	p = iov->iov_base + iov->iov_len;
188396f194b7SChuck Lever 	pad = xdr_pad_size(snd_buf->len - offset);
18842d2da60cSJ. Bruce Fields 	memset(p, 0, pad);
18852d2da60cSJ. Bruce Fields 	iov->iov_len += pad;
18862d2da60cSJ. Bruce Fields 	snd_buf->len += pad;
18872d2da60cSJ. Bruce Fields 
18882d2da60cSJ. Bruce Fields 	return 0;
1889e8680a24SChuck Lever wrap_failed:
1890e8680a24SChuck Lever 	return status;
18910c77668dSChuck Lever bad_wrap:
18920c77668dSChuck Lever 	trace_rpcgss_wrap(task, maj_stat);
18930c77668dSChuck Lever 	return -EIO;
18942d2da60cSJ. Bruce Fields }
18952d2da60cSJ. Bruce Fields 
1896e8680a24SChuck Lever static int gss_wrap_req(struct rpc_task *task, struct xdr_stream *xdr)
18971da177e4SLinus Torvalds {
1898a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
18991da177e4SLinus Torvalds 	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
19001da177e4SLinus Torvalds 			gc_base);
19011da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1902e8680a24SChuck Lever 	int status;
19031da177e4SLinus Torvalds 
1904e8680a24SChuck Lever 	status = -EIO;
19051da177e4SLinus Torvalds 	if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
19061da177e4SLinus Torvalds 		/* The spec seems a little ambiguous here, but I think that not
19071da177e4SLinus Torvalds 		 * wrapping context destruction requests makes the most sense.
19081da177e4SLinus Torvalds 		 */
1909e8680a24SChuck Lever 		status = rpcauth_wrap_req_encode(task, xdr);
19101da177e4SLinus Torvalds 		goto out;
19111da177e4SLinus Torvalds 	}
19121da177e4SLinus Torvalds 	switch (gss_cred->gc_service) {
19131da177e4SLinus Torvalds 	case RPC_GSS_SVC_NONE:
1914e8680a24SChuck Lever 		status = rpcauth_wrap_req_encode(task, xdr);
19151da177e4SLinus Torvalds 		break;
19161da177e4SLinus Torvalds 	case RPC_GSS_SVC_INTEGRITY:
1917e8680a24SChuck Lever 		status = gss_wrap_req_integ(cred, ctx, task, xdr);
19181da177e4SLinus Torvalds 		break;
19191da177e4SLinus Torvalds 	case RPC_GSS_SVC_PRIVACY:
1920e8680a24SChuck Lever 		status = gss_wrap_req_priv(cred, ctx, task, xdr);
19211da177e4SLinus Torvalds 		break;
19220c77668dSChuck Lever 	default:
19230c77668dSChuck Lever 		status = -EIO;
19241da177e4SLinus Torvalds 	}
19251da177e4SLinus Torvalds out:
19261da177e4SLinus Torvalds 	gss_put_ctx(ctx);
19271da177e4SLinus Torvalds 	return status;
19281da177e4SLinus Torvalds }
19291da177e4SLinus Torvalds 
1930a0584ee9SChuck Lever static int
1931a0584ee9SChuck Lever gss_unwrap_resp_auth(struct rpc_cred *cred)
19321da177e4SLinus Torvalds {
193335e77d21SChuck Lever 	struct rpc_auth *auth = cred->cr_auth;
193435e77d21SChuck Lever 
193535e77d21SChuck Lever 	auth->au_rslack = auth->au_verfsize;
193635e77d21SChuck Lever 	auth->au_ralign = auth->au_verfsize;
1937a0584ee9SChuck Lever 	return 0;
1938a0584ee9SChuck Lever }
19391da177e4SLinus Torvalds 
19404047aa90SChuck Lever /*
19414047aa90SChuck Lever  * RFC 2203, Section 5.3.2.2
19424047aa90SChuck Lever  *
19434047aa90SChuck Lever  *	struct rpc_gss_integ_data {
19444047aa90SChuck Lever  *		opaque databody_integ<>;
19454047aa90SChuck Lever  *		opaque checksum<>;
19464047aa90SChuck Lever  *	};
19474047aa90SChuck Lever  *
19484047aa90SChuck Lever  *	struct rpc_gss_data_t {
19494047aa90SChuck Lever  *		unsigned int seq_num;
19504047aa90SChuck Lever  *		proc_req_arg_t arg;
19514047aa90SChuck Lever  *	};
19524047aa90SChuck Lever  */
1953d162372aSChuck Lever static noinline_for_stack int
19540c77668dSChuck Lever gss_unwrap_resp_integ(struct rpc_task *task, struct rpc_cred *cred,
19550c77668dSChuck Lever 		      struct gss_cl_ctx *ctx, struct rpc_rqst *rqstp,
19560c77668dSChuck Lever 		      struct xdr_stream *xdr)
1957a0584ee9SChuck Lever {
19584047aa90SChuck Lever 	struct xdr_buf gss_data, *rcv_buf = &rqstp->rq_rcv_buf;
195935e77d21SChuck Lever 	struct rpc_auth *auth = cred->cr_auth;
19604047aa90SChuck Lever 	u32 len, offset, seqno, maj_stat;
1961a0584ee9SChuck Lever 	struct xdr_netobj mic;
19624047aa90SChuck Lever 	int ret;
1963a0584ee9SChuck Lever 
19644047aa90SChuck Lever 	ret = -EIO;
19654047aa90SChuck Lever 	mic.data = NULL;
19664047aa90SChuck Lever 
19674047aa90SChuck Lever 	/* opaque databody_integ<>; */
19684047aa90SChuck Lever 	if (xdr_stream_decode_u32(xdr, &len))
1969a0584ee9SChuck Lever 		goto unwrap_failed;
19704047aa90SChuck Lever 	if (len & 3)
1971a0584ee9SChuck Lever 		goto unwrap_failed;
19724047aa90SChuck Lever 	offset = rcv_buf->len - xdr_stream_remaining(xdr);
19734047aa90SChuck Lever 	if (xdr_stream_decode_u32(xdr, &seqno))
1974a0584ee9SChuck Lever 		goto unwrap_failed;
19754047aa90SChuck Lever 	if (seqno != rqstp->rq_seqno)
19760c77668dSChuck Lever 		goto bad_seqno;
19774047aa90SChuck Lever 	if (xdr_buf_subsegment(rcv_buf, &gss_data, offset, len))
19784047aa90SChuck Lever 		goto unwrap_failed;
19791da177e4SLinus Torvalds 
19804047aa90SChuck Lever 	/*
19814047aa90SChuck Lever 	 * The xdr_stream now points to the beginning of the
19824047aa90SChuck Lever 	 * upper layer payload, to be passed below to
19834047aa90SChuck Lever 	 * rpcauth_unwrap_resp_decode(). The checksum, which
19844047aa90SChuck Lever 	 * follows the upper layer payload in @rcv_buf, is
19854047aa90SChuck Lever 	 * located and parsed without updating the xdr_stream.
19864047aa90SChuck Lever 	 */
19874047aa90SChuck Lever 
19884047aa90SChuck Lever 	/* opaque checksum<>; */
19894047aa90SChuck Lever 	offset += len;
19904047aa90SChuck Lever 	if (xdr_decode_word(rcv_buf, offset, &len))
1991a0584ee9SChuck Lever 		goto unwrap_failed;
19924047aa90SChuck Lever 	offset += sizeof(__be32);
19934047aa90SChuck Lever 	if (offset + len > rcv_buf->len)
1994a0584ee9SChuck Lever 		goto unwrap_failed;
19954047aa90SChuck Lever 	mic.len = len;
19964047aa90SChuck Lever 	mic.data = kmalloc(len, GFP_NOFS);
19974047aa90SChuck Lever 	if (!mic.data)
19984047aa90SChuck Lever 		goto unwrap_failed;
19994047aa90SChuck Lever 	if (read_bytes_from_xdr_buf(rcv_buf, offset, mic.data, mic.len))
20004047aa90SChuck Lever 		goto unwrap_failed;
20014047aa90SChuck Lever 
20024047aa90SChuck Lever 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &gss_data, &mic);
20031da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
2004fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
20051da177e4SLinus Torvalds 	if (maj_stat != GSS_S_COMPLETE)
2006a0584ee9SChuck Lever 		goto bad_mic;
2007a0584ee9SChuck Lever 
200835e77d21SChuck Lever 	auth->au_rslack = auth->au_verfsize + 2 + 1 + XDR_QUADLEN(mic.len);
200935e77d21SChuck Lever 	auth->au_ralign = auth->au_verfsize + 2;
20104047aa90SChuck Lever 	ret = 0;
20114047aa90SChuck Lever 
20124047aa90SChuck Lever out:
20134047aa90SChuck Lever 	kfree(mic.data);
20144047aa90SChuck Lever 	return ret;
20154047aa90SChuck Lever 
2016a0584ee9SChuck Lever unwrap_failed:
20170c77668dSChuck Lever 	trace_rpcgss_unwrap_failed(task);
20184047aa90SChuck Lever 	goto out;
20190c77668dSChuck Lever bad_seqno:
20204047aa90SChuck Lever 	trace_rpcgss_bad_seqno(task, rqstp->rq_seqno, seqno);
20214047aa90SChuck Lever 	goto out;
2022a0584ee9SChuck Lever bad_mic:
20230c77668dSChuck Lever 	trace_rpcgss_verify_mic(task, maj_stat);
20244047aa90SChuck Lever 	goto out;
20251da177e4SLinus Torvalds }
20261da177e4SLinus Torvalds 
2027d162372aSChuck Lever static noinline_for_stack int
20280c77668dSChuck Lever gss_unwrap_resp_priv(struct rpc_task *task, struct rpc_cred *cred,
20290c77668dSChuck Lever 		     struct gss_cl_ctx *ctx, struct rpc_rqst *rqstp,
20300c77668dSChuck Lever 		     struct xdr_stream *xdr)
20312d2da60cSJ. Bruce Fields {
20322d2da60cSJ. Bruce Fields 	struct xdr_buf *rcv_buf = &rqstp->rq_rcv_buf;
2033a0584ee9SChuck Lever 	struct kvec *head = rqstp->rq_rcv_buf.head;
203435e77d21SChuck Lever 	struct rpc_auth *auth = cred->cr_auth;
2035a0584ee9SChuck Lever 	u32 offset, opaque_len, maj_stat;
2036a0584ee9SChuck Lever 	__be32 *p;
20372d2da60cSJ. Bruce Fields 
2038a0584ee9SChuck Lever 	p = xdr_inline_decode(xdr, 2 * sizeof(*p));
2039a0584ee9SChuck Lever 	if (unlikely(!p))
2040a0584ee9SChuck Lever 		goto unwrap_failed;
2041a0584ee9SChuck Lever 	opaque_len = be32_to_cpup(p++);
2042a0584ee9SChuck Lever 	offset = (u8 *)(p) - (u8 *)head->iov_base;
20432d2da60cSJ. Bruce Fields 	if (offset + opaque_len > rcv_buf->len)
2044a0584ee9SChuck Lever 		goto unwrap_failed;
20452d2da60cSJ. Bruce Fields 
204631c9590aSChuck Lever 	maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset,
204731c9590aSChuck Lever 			      offset + opaque_len, rcv_buf);
20482d2da60cSJ. Bruce Fields 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
2049fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
20502d2da60cSJ. Bruce Fields 	if (maj_stat != GSS_S_COMPLETE)
2051a0584ee9SChuck Lever 		goto bad_unwrap;
2052a0584ee9SChuck Lever 	/* gss_unwrap decrypted the sequence number */
2053a0584ee9SChuck Lever 	if (be32_to_cpup(p++) != rqstp->rq_seqno)
20540c77668dSChuck Lever 		goto bad_seqno;
20552d2da60cSJ. Bruce Fields 
2056a0584ee9SChuck Lever 	/* gss_unwrap redacts the opaque blob from the head iovec.
2057a0584ee9SChuck Lever 	 * rcv_buf has changed, thus the stream needs to be reset.
2058a0584ee9SChuck Lever 	 */
2059a0584ee9SChuck Lever 	xdr_init_decode(xdr, rcv_buf, p, rqstp);
2060a0584ee9SChuck Lever 
2061a7e429a6SChuck Lever 	auth->au_rslack = auth->au_verfsize + 2 + ctx->gc_gss_ctx->slack;
2062a7e429a6SChuck Lever 	auth->au_ralign = auth->au_verfsize + 2 + ctx->gc_gss_ctx->align;
2063a7e429a6SChuck Lever 
20642d2da60cSJ. Bruce Fields 	return 0;
2065a0584ee9SChuck Lever unwrap_failed:
20660c77668dSChuck Lever 	trace_rpcgss_unwrap_failed(task);
20670c77668dSChuck Lever 	return -EIO;
20680c77668dSChuck Lever bad_seqno:
20690c77668dSChuck Lever 	trace_rpcgss_bad_seqno(task, rqstp->rq_seqno, be32_to_cpup(--p));
2070a0584ee9SChuck Lever 	return -EIO;
2071a0584ee9SChuck Lever bad_unwrap:
20720c77668dSChuck Lever 	trace_rpcgss_unwrap(task, maj_stat);
2073a0584ee9SChuck Lever 	return -EIO;
2074bf269551SChuck Lever }
20752d2da60cSJ. Bruce Fields 
20763021a5bbSTrond Myklebust static bool
20773021a5bbSTrond Myklebust gss_seq_is_newer(u32 new, u32 old)
20783021a5bbSTrond Myklebust {
20793021a5bbSTrond Myklebust 	return (s32)(new - old) > 0;
20803021a5bbSTrond Myklebust }
20813021a5bbSTrond Myklebust 
20823021a5bbSTrond Myklebust static bool
20833021a5bbSTrond Myklebust gss_xmit_need_reencode(struct rpc_task *task)
20843021a5bbSTrond Myklebust {
20853021a5bbSTrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
20863021a5bbSTrond Myklebust 	struct rpc_cred *cred = req->rq_cred;
20873021a5bbSTrond Myklebust 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
20880c77668dSChuck Lever 	u32 win, seq_xmit = 0;
20893021a5bbSTrond Myklebust 	bool ret = true;
20903021a5bbSTrond Myklebust 
20913021a5bbSTrond Myklebust 	if (!ctx)
20920c77668dSChuck Lever 		goto out;
20933021a5bbSTrond Myklebust 
20943021a5bbSTrond Myklebust 	if (gss_seq_is_newer(req->rq_seqno, READ_ONCE(ctx->gc_seq)))
20950c77668dSChuck Lever 		goto out_ctx;
20963021a5bbSTrond Myklebust 
20973021a5bbSTrond Myklebust 	seq_xmit = READ_ONCE(ctx->gc_seq_xmit);
20983021a5bbSTrond Myklebust 	while (gss_seq_is_newer(req->rq_seqno, seq_xmit)) {
20993021a5bbSTrond Myklebust 		u32 tmp = seq_xmit;
21003021a5bbSTrond Myklebust 
21013021a5bbSTrond Myklebust 		seq_xmit = cmpxchg(&ctx->gc_seq_xmit, tmp, req->rq_seqno);
21023021a5bbSTrond Myklebust 		if (seq_xmit == tmp) {
21033021a5bbSTrond Myklebust 			ret = false;
21040c77668dSChuck Lever 			goto out_ctx;
21053021a5bbSTrond Myklebust 		}
21063021a5bbSTrond Myklebust 	}
21073021a5bbSTrond Myklebust 
21083021a5bbSTrond Myklebust 	win = ctx->gc_win;
21093021a5bbSTrond Myklebust 	if (win > 0)
21103021a5bbSTrond Myklebust 		ret = !gss_seq_is_newer(req->rq_seqno, seq_xmit - win);
21110c77668dSChuck Lever 
21120c77668dSChuck Lever out_ctx:
21133021a5bbSTrond Myklebust 	gss_put_ctx(ctx);
21140c77668dSChuck Lever out:
21150c77668dSChuck Lever 	trace_rpcgss_need_reencode(task, seq_xmit, ret);
21163021a5bbSTrond Myklebust 	return ret;
21173021a5bbSTrond Myklebust }
21183021a5bbSTrond Myklebust 
21191da177e4SLinus Torvalds static int
2120a0584ee9SChuck Lever gss_unwrap_resp(struct rpc_task *task, struct xdr_stream *xdr)
21211da177e4SLinus Torvalds {
2122a0584ee9SChuck Lever 	struct rpc_rqst *rqstp = task->tk_rqstp;
2123a0584ee9SChuck Lever 	struct rpc_cred *cred = rqstp->rq_cred;
21241da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
21251da177e4SLinus Torvalds 			gc_base);
21261da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
21271da177e4SLinus Torvalds 	int status = -EIO;
21281da177e4SLinus Torvalds 
21291da177e4SLinus Torvalds 	if (ctx->gc_proc != RPC_GSS_PROC_DATA)
21301da177e4SLinus Torvalds 		goto out_decode;
21311da177e4SLinus Torvalds 	switch (gss_cred->gc_service) {
21321da177e4SLinus Torvalds 	case RPC_GSS_SVC_NONE:
2133a0584ee9SChuck Lever 		status = gss_unwrap_resp_auth(cred);
21341da177e4SLinus Torvalds 		break;
21351da177e4SLinus Torvalds 	case RPC_GSS_SVC_INTEGRITY:
21360c77668dSChuck Lever 		status = gss_unwrap_resp_integ(task, cred, ctx, rqstp, xdr);
21371da177e4SLinus Torvalds 		break;
21381da177e4SLinus Torvalds 	case RPC_GSS_SVC_PRIVACY:
21390c77668dSChuck Lever 		status = gss_unwrap_resp_priv(task, cred, ctx, rqstp, xdr);
21401da177e4SLinus Torvalds 		break;
21411da177e4SLinus Torvalds 	}
2142a0584ee9SChuck Lever 	if (status)
2143a0584ee9SChuck Lever 		goto out;
2144a0584ee9SChuck Lever 
21451da177e4SLinus Torvalds out_decode:
2146a0584ee9SChuck Lever 	status = rpcauth_unwrap_resp_decode(task, xdr);
21471da177e4SLinus Torvalds out:
21481da177e4SLinus Torvalds 	gss_put_ctx(ctx);
21491da177e4SLinus Torvalds 	return status;
21501da177e4SLinus Torvalds }
21511da177e4SLinus Torvalds 
2152f1c0a861STrond Myklebust static const struct rpc_authops authgss_ops = {
21531da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
21541da177e4SLinus Torvalds 	.au_flavor	= RPC_AUTH_GSS,
21551da177e4SLinus Torvalds 	.au_name	= "RPCSEC_GSS",
21561da177e4SLinus Torvalds 	.create		= gss_create,
21571da177e4SLinus Torvalds 	.destroy	= gss_destroy,
2158a960f8d6SFrank Sorenson 	.hash_cred	= gss_hash_cred,
21591da177e4SLinus Torvalds 	.lookup_cred	= gss_lookup_cred,
216080df9d20SStanislav Kinsbursky 	.crcreate	= gss_create_cred,
21619568c5e9SChuck Lever 	.info2flavor	= gss_mech_info2flavor,
2162a77c806fSChuck Lever 	.flavor2info	= gss_mech_flavor2info,
21631da177e4SLinus Torvalds };
21641da177e4SLinus Torvalds 
2165f1c0a861STrond Myklebust static const struct rpc_credops gss_credops = {
21661da177e4SLinus Torvalds 	.cr_name		= "AUTH_GSS",
21671da177e4SLinus Torvalds 	.crdestroy		= gss_destroy_cred,
2168fba3bad4STrond Myklebust 	.cr_init		= gss_cred_init,
21691da177e4SLinus Torvalds 	.crmatch		= gss_match,
21701da177e4SLinus Torvalds 	.crmarshal		= gss_marshal,
21711da177e4SLinus Torvalds 	.crrefresh		= gss_refresh,
21721da177e4SLinus Torvalds 	.crvalidate		= gss_validate,
21731da177e4SLinus Torvalds 	.crwrap_req		= gss_wrap_req,
21741da177e4SLinus Torvalds 	.crunwrap_resp		= gss_unwrap_resp,
21754de6caa2SAndy Adamson 	.crkey_timeout		= gss_key_timeout,
2176a0337d1dSJeff Layton 	.crstringify_acceptor	= gss_stringify_acceptor,
21773021a5bbSTrond Myklebust 	.crneed_reencode	= gss_xmit_need_reencode,
21781da177e4SLinus Torvalds };
21791da177e4SLinus Torvalds 
21800df7fb74STrond Myklebust static const struct rpc_credops gss_nullops = {
21810df7fb74STrond Myklebust 	.cr_name		= "AUTH_GSS",
21826dcd3926SJeff Layton 	.crdestroy		= gss_destroy_nullcred,
21830df7fb74STrond Myklebust 	.crmatch		= gss_match,
21840df7fb74STrond Myklebust 	.crmarshal		= gss_marshal,
21850df7fb74STrond Myklebust 	.crrefresh		= gss_refresh_null,
21860df7fb74STrond Myklebust 	.crvalidate		= gss_validate,
21870df7fb74STrond Myklebust 	.crwrap_req		= gss_wrap_req,
21880df7fb74STrond Myklebust 	.crunwrap_resp		= gss_unwrap_resp,
2189a0337d1dSJeff Layton 	.crstringify_acceptor	= gss_stringify_acceptor,
21900df7fb74STrond Myklebust };
21910df7fb74STrond Myklebust 
2192b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v0 = {
2193ac83228aSTrond Myklebust 	.upcall		= gss_v0_upcall,
21941da177e4SLinus Torvalds 	.downcall	= gss_pipe_downcall,
21951da177e4SLinus Torvalds 	.destroy_msg	= gss_pipe_destroy_msg,
219634769fc4S\"J. Bruce Fields\ 	.open_pipe	= gss_pipe_open_v0,
219734769fc4S\"J. Bruce Fields\ 	.release_pipe	= gss_pipe_release,
219834769fc4S\"J. Bruce Fields\ };
219934769fc4S\"J. Bruce Fields\ 
2200b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v1 = {
2201ac83228aSTrond Myklebust 	.upcall		= gss_v1_upcall,
220234769fc4S\"J. Bruce Fields\ 	.downcall	= gss_pipe_downcall,
220334769fc4S\"J. Bruce Fields\ 	.destroy_msg	= gss_pipe_destroy_msg,
220434769fc4S\"J. Bruce Fields\ 	.open_pipe	= gss_pipe_open_v1,
22051da177e4SLinus Torvalds 	.release_pipe	= gss_pipe_release,
22061da177e4SLinus Torvalds };
22071da177e4SLinus Torvalds 
2208a1db410dSStanislav Kinsbursky static __net_init int rpcsec_gss_init_net(struct net *net)
2209a1db410dSStanislav Kinsbursky {
2210a1db410dSStanislav Kinsbursky 	return gss_svc_init_net(net);
2211a1db410dSStanislav Kinsbursky }
2212a1db410dSStanislav Kinsbursky 
2213a1db410dSStanislav Kinsbursky static __net_exit void rpcsec_gss_exit_net(struct net *net)
2214a1db410dSStanislav Kinsbursky {
2215a1db410dSStanislav Kinsbursky 	gss_svc_shutdown_net(net);
2216a1db410dSStanislav Kinsbursky }
2217a1db410dSStanislav Kinsbursky 
2218a1db410dSStanislav Kinsbursky static struct pernet_operations rpcsec_gss_net_ops = {
2219a1db410dSStanislav Kinsbursky 	.init = rpcsec_gss_init_net,
2220a1db410dSStanislav Kinsbursky 	.exit = rpcsec_gss_exit_net,
2221a1db410dSStanislav Kinsbursky };
2222a1db410dSStanislav Kinsbursky 
22231da177e4SLinus Torvalds /*
22241da177e4SLinus Torvalds  * Initialize RPCSEC_GSS module
22251da177e4SLinus Torvalds  */
22261da177e4SLinus Torvalds static int __init init_rpcsec_gss(void)
22271da177e4SLinus Torvalds {
22281da177e4SLinus Torvalds 	int err = 0;
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds 	err = rpcauth_register(&authgss_ops);
22311da177e4SLinus Torvalds 	if (err)
22321da177e4SLinus Torvalds 		goto out;
22331da177e4SLinus Torvalds 	err = gss_svc_init();
22341da177e4SLinus Torvalds 	if (err)
22351da177e4SLinus Torvalds 		goto out_unregister;
2236a1db410dSStanislav Kinsbursky 	err = register_pernet_subsys(&rpcsec_gss_net_ops);
2237a1db410dSStanislav Kinsbursky 	if (err)
2238a1db410dSStanislav Kinsbursky 		goto out_svc_exit;
223979a3f20bS\"J. Bruce Fields\ 	rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version");
22401da177e4SLinus Torvalds 	return 0;
2241a1db410dSStanislav Kinsbursky out_svc_exit:
2242a1db410dSStanislav Kinsbursky 	gss_svc_shutdown();
22431da177e4SLinus Torvalds out_unregister:
22441da177e4SLinus Torvalds 	rpcauth_unregister(&authgss_ops);
22451da177e4SLinus Torvalds out:
22461da177e4SLinus Torvalds 	return err;
22471da177e4SLinus Torvalds }
22481da177e4SLinus Torvalds 
22491da177e4SLinus Torvalds static void __exit exit_rpcsec_gss(void)
22501da177e4SLinus Torvalds {
2251a1db410dSStanislav Kinsbursky 	unregister_pernet_subsys(&rpcsec_gss_net_ops);
22521da177e4SLinus Torvalds 	gss_svc_shutdown();
22531da177e4SLinus Torvalds 	rpcauth_unregister(&authgss_ops);
2254bf12691dSJesper Dangaard Brouer 	rcu_barrier(); /* Wait for completion of call_rcu()'s */
22551da177e4SLinus Torvalds }
22561da177e4SLinus Torvalds 
225771afa85eSChuck Lever MODULE_ALIAS("rpc-auth-6");
22581da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2259126e216aSTrond Myklebust module_param_named(expired_cred_retry_delay,
2260126e216aSTrond Myklebust 		   gss_expired_cred_retry_delay,
2261126e216aSTrond Myklebust 		   uint, 0644);
2262126e216aSTrond Myklebust MODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until "
2263126e216aSTrond Myklebust 		"the RPC engine retries an expired credential");
2264126e216aSTrond Myklebust 
22654de6caa2SAndy Adamson module_param_named(key_expire_timeo,
22664de6caa2SAndy Adamson 		   gss_key_expire_timeo,
22674de6caa2SAndy Adamson 		   uint, 0644);
22684de6caa2SAndy Adamson MODULE_PARM_DESC(key_expire_timeo, "Time (in seconds) at the end of a "
22694de6caa2SAndy Adamson 		"credential keys lifetime where the NFS layer cleans up "
22704de6caa2SAndy Adamson 		"prior to key expiration");
22714de6caa2SAndy Adamson 
22721da177e4SLinus Torvalds module_init(init_rpcsec_gss)
22731da177e4SLinus Torvalds module_exit(exit_rpcsec_gss)
2274