xref: /openbmc/linux/net/sunrpc/auth_gss/auth_gss.c (revision a699d65e)
11da177e4SLinus Torvalds /*
2f30c2269SUwe Zeisberger  * linux/net/sunrpc/auth_gss/auth_gss.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * RPCSEC_GSS client authentication.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *  Copyright (c) 2000 The Regents of the University of Michigan.
71da177e4SLinus Torvalds  *  All rights reserved.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  Dug Song       <dugsong@monkey.org>
101da177e4SLinus Torvalds  *  Andy Adamson   <andros@umich.edu>
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *  Redistribution and use in source and binary forms, with or without
131da177e4SLinus Torvalds  *  modification, are permitted provided that the following conditions
141da177e4SLinus Torvalds  *  are met:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *  1. Redistributions of source code must retain the above copyright
171da177e4SLinus Torvalds  *     notice, this list of conditions and the following disclaimer.
181da177e4SLinus Torvalds  *  2. Redistributions in binary form must reproduce the above copyright
191da177e4SLinus Torvalds  *     notice, this list of conditions and the following disclaimer in the
201da177e4SLinus Torvalds  *     documentation and/or other materials provided with the distribution.
211da177e4SLinus Torvalds  *  3. Neither the name of the University nor the names of its
221da177e4SLinus Torvalds  *     contributors may be used to endorse or promote products derived
231da177e4SLinus Torvalds  *     from this software without specific prior written permission.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
261da177e4SLinus Torvalds  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
271da177e4SLinus Torvalds  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
281da177e4SLinus Torvalds  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291da177e4SLinus Torvalds  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
301da177e4SLinus Torvalds  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
311da177e4SLinus Torvalds  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
321da177e4SLinus Torvalds  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
331da177e4SLinus Torvalds  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
341da177e4SLinus Torvalds  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
351da177e4SLinus Torvalds  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <linux/module.h>
401da177e4SLinus Torvalds #include <linux/init.h>
411da177e4SLinus Torvalds #include <linux/types.h>
421da177e4SLinus Torvalds #include <linux/slab.h>
431da177e4SLinus Torvalds #include <linux/sched.h>
442d2da60cSJ. Bruce Fields #include <linux/pagemap.h>
451da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
461da177e4SLinus Torvalds #include <linux/sunrpc/auth.h>
471da177e4SLinus Torvalds #include <linux/sunrpc/auth_gss.h>
481da177e4SLinus Torvalds #include <linux/sunrpc/svcauth_gss.h>
491da177e4SLinus Torvalds #include <linux/sunrpc/gss_err.h>
501da177e4SLinus Torvalds #include <linux/workqueue.h>
511da177e4SLinus Torvalds #include <linux/sunrpc/rpc_pipe_fs.h>
521da177e4SLinus Torvalds #include <linux/sunrpc/gss_api.h>
531da177e4SLinus Torvalds #include <asm/uaccess.h>
54eb6dc19dSTrond Myklebust #include <linux/hashtable.h>
551da177e4SLinus Torvalds 
56abfdbd53STrond Myklebust #include "../netns.h"
57abfdbd53STrond Myklebust 
58f1c0a861STrond Myklebust static const struct rpc_authops authgss_ops;
591da177e4SLinus Torvalds 
60f1c0a861STrond Myklebust static const struct rpc_credops gss_credops;
610df7fb74STrond Myklebust static const struct rpc_credops gss_nullops;
621da177e4SLinus Torvalds 
63126e216aSTrond Myklebust #define GSS_RETRY_EXPIRED 5
64126e216aSTrond Myklebust static unsigned int gss_expired_cred_retry_delay = GSS_RETRY_EXPIRED;
65126e216aSTrond Myklebust 
664de6caa2SAndy Adamson #define GSS_KEY_EXPIRE_TIMEO 240
674de6caa2SAndy Adamson static unsigned int gss_key_expire_timeo = GSS_KEY_EXPIRE_TIMEO;
684de6caa2SAndy Adamson 
691da177e4SLinus Torvalds #ifdef RPC_DEBUG
701da177e4SLinus Torvalds # define RPCDBG_FACILITY	RPCDBG_AUTH
711da177e4SLinus Torvalds #endif
721da177e4SLinus Torvalds 
73725f2865SKevin Coffman #define GSS_CRED_SLACK		(RPC_MAX_AUTH_SIZE * 2)
741da177e4SLinus Torvalds /* length of a krb5 verifier (48), plus data added before arguments when
751da177e4SLinus Torvalds  * using integrity (two 4-byte integers): */
76adeb8133SOlga Kornievskaia #define GSS_VERF_SLACK		100
771da177e4SLinus Torvalds 
7823c323afSTrond Myklebust static DEFINE_HASHTABLE(gss_auth_hash_table, 4);
79eb6dc19dSTrond Myklebust static DEFINE_SPINLOCK(gss_auth_hash_lock);
80eb6dc19dSTrond Myklebust 
8119172284STrond Myklebust struct gss_pipe {
8219172284STrond Myklebust 	struct rpc_pipe_dir_object pdo;
8319172284STrond Myklebust 	struct rpc_pipe *pipe;
8419172284STrond Myklebust 	struct rpc_clnt *clnt;
8519172284STrond Myklebust 	const char *name;
86414a6295STrond Myklebust 	struct kref kref;
8719172284STrond Myklebust };
8819172284STrond Myklebust 
891da177e4SLinus Torvalds struct gss_auth {
900285ed1fSTrond Myklebust 	struct kref kref;
91eb6dc19dSTrond Myklebust 	struct hlist_node hash;
921da177e4SLinus Torvalds 	struct rpc_auth rpc_auth;
931da177e4SLinus Torvalds 	struct gss_api_mech *mech;
941da177e4SLinus Torvalds 	enum rpc_gss_svc service;
951da177e4SLinus Torvalds 	struct rpc_clnt *client;
96e726340aSTrond Myklebust 	struct net *net;
9734769fc4S\"J. Bruce Fields\ 	/*
9834769fc4S\"J. Bruce Fields\ 	 * There are two upcall pipes; dentry[1], named "gssd", is used
9934769fc4S\"J. Bruce Fields\ 	 * for the new text-based upcall; dentry[0] is named after the
10034769fc4S\"J. Bruce Fields\ 	 * mechanism (for example, "krb5") and exists for
10134769fc4S\"J. Bruce Fields\ 	 * backwards-compatibility with older gssd's.
10234769fc4S\"J. Bruce Fields\ 	 */
10319172284STrond Myklebust 	struct gss_pipe *gss_pipe[2];
104bd4a3eb1STrond Myklebust 	const char *target_name;
1051da177e4SLinus Torvalds };
1061da177e4SLinus Torvalds 
10779a3f20bS\"J. Bruce Fields\ /* pipe_version >= 0 if and only if someone has a pipe open. */
10879a3f20bS\"J. Bruce Fields\ static DEFINE_SPINLOCK(pipe_version_lock);
10979a3f20bS\"J. Bruce Fields\ static struct rpc_wait_queue pipe_version_rpc_waitqueue;
11079a3f20bS\"J. Bruce Fields\ static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue);
111cf81939dS\"J. Bruce Fields\ 
1125d28dc82STrond Myklebust static void gss_free_ctx(struct gss_cl_ctx *);
113b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v0;
114b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v1;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds static inline struct gss_cl_ctx *
1171da177e4SLinus Torvalds gss_get_ctx(struct gss_cl_ctx *ctx)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	atomic_inc(&ctx->count);
1201da177e4SLinus Torvalds 	return ctx;
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds static inline void
1241da177e4SLinus Torvalds gss_put_ctx(struct gss_cl_ctx *ctx)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	if (atomic_dec_and_test(&ctx->count))
1275d28dc82STrond Myklebust 		gss_free_ctx(ctx);
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
1305d28dc82STrond Myklebust /* gss_cred_set_ctx:
1315d28dc82STrond Myklebust  * called by gss_upcall_callback and gss_create_upcall in order
1325d28dc82STrond Myklebust  * to set the gss context. The actual exchange of an old context
1339beae467SStanislav Kinsbursky  * and a new one is protected by the pipe->lock.
1345d28dc82STrond Myklebust  */
1351da177e4SLinus Torvalds static void
1361da177e4SLinus Torvalds gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1395d28dc82STrond Myklebust 
140cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
141cd019f75STrond Myklebust 		return;
1427b6962b0STrond Myklebust 	gss_get_ctx(ctx);
143cf778b00SEric Dumazet 	rcu_assign_pointer(gss_cred->gc_ctx, ctx);
144fc432dd9STrond Myklebust 	set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
145cd019f75STrond Myklebust 	smp_mb__before_clear_bit();
146fc432dd9STrond Myklebust 	clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static const void *
1501da177e4SLinus Torvalds simple_get_bytes(const void *p, const void *end, void *res, size_t len)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	const void *q = (const void *)((const char *)p + len);
1531da177e4SLinus Torvalds 	if (unlikely(q > end || q < p))
1541da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
1551da177e4SLinus Torvalds 	memcpy(res, p, len);
1561da177e4SLinus Torvalds 	return q;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds static inline const void *
1601da177e4SLinus Torvalds simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	const void *q;
1631da177e4SLinus Torvalds 	unsigned int len;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &len, sizeof(len));
1661da177e4SLinus Torvalds 	if (IS_ERR(p))
1671da177e4SLinus Torvalds 		return p;
1681da177e4SLinus Torvalds 	q = (const void *)((const char *)p + len);
1691da177e4SLinus Torvalds 	if (unlikely(q > end || q < p))
1701da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
1710f38b873STrond Myklebust 	dest->data = kmemdup(p, len, GFP_NOFS);
1721da177e4SLinus Torvalds 	if (unlikely(dest->data == NULL))
1731da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
1741da177e4SLinus Torvalds 	dest->len = len;
1751da177e4SLinus Torvalds 	return q;
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds static struct gss_cl_ctx *
1791da177e4SLinus Torvalds gss_cred_get_ctx(struct rpc_cred *cred)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1821da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = NULL;
1831da177e4SLinus Torvalds 
1845d28dc82STrond Myklebust 	rcu_read_lock();
1851da177e4SLinus Torvalds 	if (gss_cred->gc_ctx)
1861da177e4SLinus Torvalds 		ctx = gss_get_ctx(gss_cred->gc_ctx);
1875d28dc82STrond Myklebust 	rcu_read_unlock();
1881da177e4SLinus Torvalds 	return ctx;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds static struct gss_cl_ctx *
1921da177e4SLinus Torvalds gss_alloc_context(void)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
1951da177e4SLinus Torvalds 
1960f38b873STrond Myklebust 	ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
1971da177e4SLinus Torvalds 	if (ctx != NULL) {
1981da177e4SLinus Torvalds 		ctx->gc_proc = RPC_GSS_PROC_DATA;
1991da177e4SLinus Torvalds 		ctx->gc_seq = 1;	/* NetApp 6.4R1 doesn't accept seq. no. 0 */
2001da177e4SLinus Torvalds 		spin_lock_init(&ctx->gc_seq_lock);
2011da177e4SLinus Torvalds 		atomic_set(&ctx->count,1);
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds 	return ctx;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds #define GSSD_MIN_TIMEOUT (60 * 60)
2071da177e4SLinus Torvalds static const void *
2081da177e4SLinus Torvalds gss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
2091da177e4SLinus Torvalds {
2101da177e4SLinus Torvalds 	const void *q;
2111da177e4SLinus Torvalds 	unsigned int seclen;
2121da177e4SLinus Torvalds 	unsigned int timeout;
213620038f6SAndy Adamson 	unsigned long now = jiffies;
2141da177e4SLinus Torvalds 	u32 window_size;
2151da177e4SLinus Torvalds 	int ret;
2161da177e4SLinus Torvalds 
217620038f6SAndy Adamson 	/* First unsigned int gives the remaining lifetime in seconds of the
218620038f6SAndy Adamson 	 * credential - e.g. the remaining TGT lifetime for Kerberos or
219620038f6SAndy Adamson 	 * the -t value passed to GSSD.
220620038f6SAndy Adamson 	 */
2211da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
2221da177e4SLinus Torvalds 	if (IS_ERR(p))
2231da177e4SLinus Torvalds 		goto err;
2241da177e4SLinus Torvalds 	if (timeout == 0)
2251da177e4SLinus Torvalds 		timeout = GSSD_MIN_TIMEOUT;
226620038f6SAndy Adamson 	ctx->gc_expiry = now + ((unsigned long)timeout * HZ);
227620038f6SAndy Adamson 	/* Sequence number window. Determines the maximum number of
228620038f6SAndy Adamson 	 * simultaneous requests
229620038f6SAndy Adamson 	 */
2301da177e4SLinus Torvalds 	p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
2311da177e4SLinus Torvalds 	if (IS_ERR(p))
2321da177e4SLinus Torvalds 		goto err;
2331da177e4SLinus Torvalds 	ctx->gc_win = window_size;
2341da177e4SLinus Torvalds 	/* gssd signals an error by passing ctx->gc_win = 0: */
2351da177e4SLinus Torvalds 	if (ctx->gc_win == 0) {
236dc5ddce9SJeff Layton 		/*
237dc5ddce9SJeff Layton 		 * in which case, p points to an error code. Anything other
238dc5ddce9SJeff Layton 		 * than -EKEYEXPIRED gets converted to -EACCES.
239dc5ddce9SJeff Layton 		 */
240dc5ddce9SJeff Layton 		p = simple_get_bytes(p, end, &ret, sizeof(ret));
241dc5ddce9SJeff Layton 		if (!IS_ERR(p))
242dc5ddce9SJeff Layton 			p = (ret == -EKEYEXPIRED) ? ERR_PTR(-EKEYEXPIRED) :
243dc5ddce9SJeff Layton 						    ERR_PTR(-EACCES);
2441da177e4SLinus Torvalds 		goto err;
2451da177e4SLinus Torvalds 	}
2461da177e4SLinus Torvalds 	/* copy the opaque wire context */
2471da177e4SLinus Torvalds 	p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
2481da177e4SLinus Torvalds 	if (IS_ERR(p))
2491da177e4SLinus Torvalds 		goto err;
2501da177e4SLinus Torvalds 	/* import the opaque security context */
2511da177e4SLinus Torvalds 	p  = simple_get_bytes(p, end, &seclen, sizeof(seclen));
2521da177e4SLinus Torvalds 	if (IS_ERR(p))
2531da177e4SLinus Torvalds 		goto err;
2541da177e4SLinus Torvalds 	q = (const void *)((const char *)p + seclen);
2551da177e4SLinus Torvalds 	if (unlikely(q > end || q < p)) {
2561da177e4SLinus Torvalds 		p = ERR_PTR(-EFAULT);
2571da177e4SLinus Torvalds 		goto err;
2581da177e4SLinus Torvalds 	}
259400f26b5SSimo Sorce 	ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx, NULL, GFP_NOFS);
2601da177e4SLinus Torvalds 	if (ret < 0) {
2611da177e4SLinus Torvalds 		p = ERR_PTR(ret);
2621da177e4SLinus Torvalds 		goto err;
2631da177e4SLinus Torvalds 	}
264620038f6SAndy Adamson 	dprintk("RPC:       %s Success. gc_expiry %lu now %lu timeout %u\n",
265620038f6SAndy Adamson 		__func__, ctx->gc_expiry, now, timeout);
2661da177e4SLinus Torvalds 	return q;
2671da177e4SLinus Torvalds err:
268173db309SJeff Layton 	dprintk("RPC:       %s returns error %ld\n", __func__, -PTR_ERR(p));
2691da177e4SLinus Torvalds 	return p;
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
27234769fc4S\"J. Bruce Fields\ #define UPCALL_BUF_LEN 128
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds struct gss_upcall_msg {
2751da177e4SLinus Torvalds 	atomic_t count;
2767eaf040bSEric W. Biederman 	kuid_t	uid;
2771da177e4SLinus Torvalds 	struct rpc_pipe_msg msg;
2781da177e4SLinus Torvalds 	struct list_head list;
2791da177e4SLinus Torvalds 	struct gss_auth *auth;
2809beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
2811da177e4SLinus Torvalds 	struct rpc_wait_queue rpc_waitqueue;
2821da177e4SLinus Torvalds 	wait_queue_head_t waitqueue;
2831da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
28434769fc4S\"J. Bruce Fields\ 	char databuf[UPCALL_BUF_LEN];
2851da177e4SLinus Torvalds };
2861da177e4SLinus Torvalds 
2872aed8b47STrond Myklebust static int get_pipe_version(struct net *net)
28879a3f20bS\"J. Bruce Fields\ {
2892aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
29079a3f20bS\"J. Bruce Fields\ 	int ret;
29179a3f20bS\"J. Bruce Fields\ 
29279a3f20bS\"J. Bruce Fields\ 	spin_lock(&pipe_version_lock);
2932aed8b47STrond Myklebust 	if (sn->pipe_version >= 0) {
2942aed8b47STrond Myklebust 		atomic_inc(&sn->pipe_users);
2952aed8b47STrond Myklebust 		ret = sn->pipe_version;
29679a3f20bS\"J. Bruce Fields\ 	} else
29779a3f20bS\"J. Bruce Fields\ 		ret = -EAGAIN;
29879a3f20bS\"J. Bruce Fields\ 	spin_unlock(&pipe_version_lock);
29979a3f20bS\"J. Bruce Fields\ 	return ret;
30079a3f20bS\"J. Bruce Fields\ }
30179a3f20bS\"J. Bruce Fields\ 
3022aed8b47STrond Myklebust static void put_pipe_version(struct net *net)
30379a3f20bS\"J. Bruce Fields\ {
3042aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
3052aed8b47STrond Myklebust 
3062aed8b47STrond Myklebust 	if (atomic_dec_and_lock(&sn->pipe_users, &pipe_version_lock)) {
3072aed8b47STrond Myklebust 		sn->pipe_version = -1;
30879a3f20bS\"J. Bruce Fields\ 		spin_unlock(&pipe_version_lock);
30979a3f20bS\"J. Bruce Fields\ 	}
31079a3f20bS\"J. Bruce Fields\ }
31179a3f20bS\"J. Bruce Fields\ 
3121da177e4SLinus Torvalds static void
3131da177e4SLinus Torvalds gss_release_msg(struct gss_upcall_msg *gss_msg)
3141da177e4SLinus Torvalds {
315e726340aSTrond Myklebust 	struct net *net = gss_msg->auth->net;
3161da177e4SLinus Torvalds 	if (!atomic_dec_and_test(&gss_msg->count))
3171da177e4SLinus Torvalds 		return;
3182aed8b47STrond Myklebust 	put_pipe_version(net);
3191da177e4SLinus Torvalds 	BUG_ON(!list_empty(&gss_msg->list));
3201da177e4SLinus Torvalds 	if (gss_msg->ctx != NULL)
3211da177e4SLinus Torvalds 		gss_put_ctx(gss_msg->ctx);
322f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue);
3231da177e4SLinus Torvalds 	kfree(gss_msg);
3241da177e4SLinus Torvalds }
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds static struct gss_upcall_msg *
3277eaf040bSEric W. Biederman __gss_find_upcall(struct rpc_pipe *pipe, kuid_t uid)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	struct gss_upcall_msg *pos;
3309beae467SStanislav Kinsbursky 	list_for_each_entry(pos, &pipe->in_downcall, list) {
3310b4d51b0SEric W. Biederman 		if (!uid_eq(pos->uid, uid))
3321da177e4SLinus Torvalds 			continue;
3331da177e4SLinus Torvalds 		atomic_inc(&pos->count);
334632f0d05SChuck Lever 		dprintk("RPC:       %s found msg %p\n", __func__, pos);
3351da177e4SLinus Torvalds 		return pos;
3361da177e4SLinus Torvalds 	}
337632f0d05SChuck Lever 	dprintk("RPC:       %s found nothing\n", __func__);
3381da177e4SLinus Torvalds 	return NULL;
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
341720b8f2dS\\\"J. Bruce Fields\\\ /* Try to add an upcall to the pipefs queue.
3421da177e4SLinus Torvalds  * If an upcall owned by our uid already exists, then we return a reference
3431da177e4SLinus Torvalds  * to that upcall instead of adding the new upcall.
3441da177e4SLinus Torvalds  */
3451da177e4SLinus Torvalds static inline struct gss_upcall_msg *
346053e324fSSuresh Jayaraman gss_add_msg(struct gss_upcall_msg *gss_msg)
3471da177e4SLinus Torvalds {
3489beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
3491da177e4SLinus Torvalds 	struct gss_upcall_msg *old;
3501da177e4SLinus Torvalds 
3519beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
3529beae467SStanislav Kinsbursky 	old = __gss_find_upcall(pipe, gss_msg->uid);
3531da177e4SLinus Torvalds 	if (old == NULL) {
3541da177e4SLinus Torvalds 		atomic_inc(&gss_msg->count);
3559beae467SStanislav Kinsbursky 		list_add(&gss_msg->list, &pipe->in_downcall);
3561da177e4SLinus Torvalds 	} else
3571da177e4SLinus Torvalds 		gss_msg = old;
3589beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
3591da177e4SLinus Torvalds 	return gss_msg;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds static void
3631da177e4SLinus Torvalds __gss_unhash_msg(struct gss_upcall_msg *gss_msg)
3641da177e4SLinus Torvalds {
3651da177e4SLinus Torvalds 	list_del_init(&gss_msg->list);
3661da177e4SLinus Torvalds 	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
3671da177e4SLinus Torvalds 	wake_up_all(&gss_msg->waitqueue);
3681da177e4SLinus Torvalds 	atomic_dec(&gss_msg->count);
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds static void
3721da177e4SLinus Torvalds gss_unhash_msg(struct gss_upcall_msg *gss_msg)
3731da177e4SLinus Torvalds {
3749beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
3751da177e4SLinus Torvalds 
3763b68aaeaSTrond Myklebust 	if (list_empty(&gss_msg->list))
3773b68aaeaSTrond Myklebust 		return;
3789beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
3793b68aaeaSTrond Myklebust 	if (!list_empty(&gss_msg->list))
3801da177e4SLinus Torvalds 		__gss_unhash_msg(gss_msg);
3819beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds static void
385126e216aSTrond Myklebust gss_handle_downcall_result(struct gss_cred *gss_cred, struct gss_upcall_msg *gss_msg)
386126e216aSTrond Myklebust {
387126e216aSTrond Myklebust 	switch (gss_msg->msg.errno) {
388126e216aSTrond Myklebust 	case 0:
389126e216aSTrond Myklebust 		if (gss_msg->ctx == NULL)
390126e216aSTrond Myklebust 			break;
391126e216aSTrond Myklebust 		clear_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
392126e216aSTrond Myklebust 		gss_cred_set_ctx(&gss_cred->gc_base, gss_msg->ctx);
393126e216aSTrond Myklebust 		break;
394126e216aSTrond Myklebust 	case -EKEYEXPIRED:
395126e216aSTrond Myklebust 		set_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
396126e216aSTrond Myklebust 	}
397126e216aSTrond Myklebust 	gss_cred->gc_upcall_timestamp = jiffies;
398126e216aSTrond Myklebust 	gss_cred->gc_upcall = NULL;
399126e216aSTrond Myklebust 	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
400126e216aSTrond Myklebust }
401126e216aSTrond Myklebust 
402126e216aSTrond Myklebust static void
4031da177e4SLinus Torvalds gss_upcall_callback(struct rpc_task *task)
4041da177e4SLinus Torvalds {
405a17c2153STrond Myklebust 	struct gss_cred *gss_cred = container_of(task->tk_rqstp->rq_cred,
4061da177e4SLinus Torvalds 			struct gss_cred, gc_base);
4071da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
4089beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = gss_msg->pipe;
4091da177e4SLinus Torvalds 
4109beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
411126e216aSTrond Myklebust 	gss_handle_downcall_result(gss_cred, gss_msg);
4129beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
413126e216aSTrond Myklebust 	task->tk_status = gss_msg->msg.errno;
4141da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
4151da177e4SLinus Torvalds }
4161da177e4SLinus Torvalds 
41734769fc4S\"J. Bruce Fields\ static void gss_encode_v0_msg(struct gss_upcall_msg *gss_msg)
41834769fc4S\"J. Bruce Fields\ {
41990602c7bSEric W. Biederman 	uid_t uid = from_kuid(&init_user_ns, gss_msg->uid);
42090602c7bSEric W. Biederman 	memcpy(gss_msg->databuf, &uid, sizeof(uid));
42190602c7bSEric W. Biederman 	gss_msg->msg.data = gss_msg->databuf;
42290602c7bSEric W. Biederman 	gss_msg->msg.len = sizeof(uid);
4239d3a2260STrond Myklebust 
4249d3a2260STrond Myklebust 	BUILD_BUG_ON(sizeof(uid) > sizeof(gss_msg->databuf));
42534769fc4S\"J. Bruce Fields\ }
42634769fc4S\"J. Bruce Fields\ 
4279d3a2260STrond Myklebust static int gss_encode_v1_msg(struct gss_upcall_msg *gss_msg,
428bd4a3eb1STrond Myklebust 				const char *service_name,
429bd4a3eb1STrond Myklebust 				const char *target_name)
43034769fc4S\"J. Bruce Fields\ {
431683ac665STrond Myklebust 	struct gss_api_mech *mech = gss_msg->auth->mech;
4328b1c7bf5SOlga Kornievskaia 	char *p = gss_msg->databuf;
4339d3a2260STrond Myklebust 	size_t buflen = sizeof(gss_msg->databuf);
4349d3a2260STrond Myklebust 	int len;
4358b1c7bf5SOlga Kornievskaia 
4369d3a2260STrond Myklebust 	len = scnprintf(p, buflen, "mech=%s uid=%d ", mech->gm_name,
43790602c7bSEric W. Biederman 			from_kuid(&init_user_ns, gss_msg->uid));
4389d3a2260STrond Myklebust 	buflen -= len;
4399d3a2260STrond Myklebust 	p += len;
4409d3a2260STrond Myklebust 	gss_msg->msg.len = len;
441bd4a3eb1STrond Myklebust 	if (target_name) {
4429d3a2260STrond Myklebust 		len = scnprintf(p, buflen, "target=%s ", target_name);
4439d3a2260STrond Myklebust 		buflen -= len;
4448b1c7bf5SOlga Kornievskaia 		p += len;
4458b1c7bf5SOlga Kornievskaia 		gss_msg->msg.len += len;
4468b1c7bf5SOlga Kornievskaia 	}
44768c97153STrond Myklebust 	if (service_name != NULL) {
4489d3a2260STrond Myklebust 		len = scnprintf(p, buflen, "service=%s ", service_name);
4499d3a2260STrond Myklebust 		buflen -= len;
4502efef708SOlga Kornievskaia 		p += len;
4512efef708SOlga Kornievskaia 		gss_msg->msg.len += len;
4522efef708SOlga Kornievskaia 	}
453683ac665STrond Myklebust 	if (mech->gm_upcall_enctypes) {
4549d3a2260STrond Myklebust 		len = scnprintf(p, buflen, "enctypes=%s ",
4559d3a2260STrond Myklebust 				mech->gm_upcall_enctypes);
4569d3a2260STrond Myklebust 		buflen -= len;
457683ac665STrond Myklebust 		p += len;
458683ac665STrond Myklebust 		gss_msg->msg.len += len;
459683ac665STrond Myklebust 	}
4609d3a2260STrond Myklebust 	len = scnprintf(p, buflen, "\n");
4619d3a2260STrond Myklebust 	if (len == 0)
4629d3a2260STrond Myklebust 		goto out_overflow;
4638b1c7bf5SOlga Kornievskaia 	gss_msg->msg.len += len;
4648b1c7bf5SOlga Kornievskaia 
46534769fc4S\"J. Bruce Fields\ 	gss_msg->msg.data = gss_msg->databuf;
4669d3a2260STrond Myklebust 	return 0;
4679d3a2260STrond Myklebust out_overflow:
4689d3a2260STrond Myklebust 	WARN_ON_ONCE(1);
4699d3a2260STrond Myklebust 	return -ENOMEM;
47034769fc4S\"J. Bruce Fields\ }
47134769fc4S\"J. Bruce Fields\ 
47268c97153STrond Myklebust static struct gss_upcall_msg *
473e726340aSTrond Myklebust gss_alloc_msg(struct gss_auth *gss_auth,
4747eaf040bSEric W. Biederman 		kuid_t uid, const char *service_name)
4751da177e4SLinus Torvalds {
4761da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
47779a3f20bS\"J. Bruce Fields\ 	int vers;
4789d3a2260STrond Myklebust 	int err = -ENOMEM;
4791da177e4SLinus Torvalds 
4800f38b873STrond Myklebust 	gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
481db75b3d6S\"J. Bruce Fields\ 	if (gss_msg == NULL)
4829d3a2260STrond Myklebust 		goto err;
483e726340aSTrond Myklebust 	vers = get_pipe_version(gss_auth->net);
4849d3a2260STrond Myklebust 	err = vers;
4859d3a2260STrond Myklebust 	if (err < 0)
4869d3a2260STrond Myklebust 		goto err_free_msg;
48719172284STrond Myklebust 	gss_msg->pipe = gss_auth->gss_pipe[vers]->pipe;
4881da177e4SLinus Torvalds 	INIT_LIST_HEAD(&gss_msg->list);
4891da177e4SLinus Torvalds 	rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
4901da177e4SLinus Torvalds 	init_waitqueue_head(&gss_msg->waitqueue);
4911da177e4SLinus Torvalds 	atomic_set(&gss_msg->count, 1);
4921da177e4SLinus Torvalds 	gss_msg->uid = uid;
4931da177e4SLinus Torvalds 	gss_msg->auth = gss_auth;
494bd4a3eb1STrond Myklebust 	switch (vers) {
495bd4a3eb1STrond Myklebust 	case 0:
496bd4a3eb1STrond Myklebust 		gss_encode_v0_msg(gss_msg);
4975fccc5b5STrond Myklebust 		break;
498bd4a3eb1STrond Myklebust 	default:
4999d3a2260STrond Myklebust 		err = gss_encode_v1_msg(gss_msg, service_name, gss_auth->target_name);
5009d3a2260STrond Myklebust 		if (err)
5019d3a2260STrond Myklebust 			goto err_free_msg;
502bd4a3eb1STrond Myklebust 	};
5031da177e4SLinus Torvalds 	return gss_msg;
5049d3a2260STrond Myklebust err_free_msg:
5059d3a2260STrond Myklebust 	kfree(gss_msg);
5069d3a2260STrond Myklebust err:
5079d3a2260STrond Myklebust 	return ERR_PTR(err);
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds static struct gss_upcall_msg *
511e726340aSTrond Myklebust gss_setup_upcall(struct gss_auth *gss_auth, struct rpc_cred *cred)
5121da177e4SLinus Torvalds {
5137c67db3aSTrond Myklebust 	struct gss_cred *gss_cred = container_of(cred,
5147c67db3aSTrond Myklebust 			struct gss_cred, gc_base);
5151da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_new, *gss_msg;
5167eaf040bSEric W. Biederman 	kuid_t uid = cred->cr_uid;
5171da177e4SLinus Torvalds 
518e726340aSTrond Myklebust 	gss_new = gss_alloc_msg(gss_auth, uid, gss_cred->gc_principal);
519db75b3d6S\"J. Bruce Fields\ 	if (IS_ERR(gss_new))
520db75b3d6S\"J. Bruce Fields\ 		return gss_new;
521053e324fSSuresh Jayaraman 	gss_msg = gss_add_msg(gss_new);
5221da177e4SLinus Torvalds 	if (gss_msg == gss_new) {
5239beae467SStanislav Kinsbursky 		int res = rpc_queue_upcall(gss_new->pipe, &gss_new->msg);
5241da177e4SLinus Torvalds 		if (res) {
5251da177e4SLinus Torvalds 			gss_unhash_msg(gss_new);
5261da177e4SLinus Torvalds 			gss_msg = ERR_PTR(res);
5271da177e4SLinus Torvalds 		}
5281da177e4SLinus Torvalds 	} else
5291da177e4SLinus Torvalds 		gss_release_msg(gss_new);
5301da177e4SLinus Torvalds 	return gss_msg;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
533b03568c3S\"J. Bruce Fields\ static void warn_gssd(void)
534b03568c3S\"J. Bruce Fields\ {
5350ea9de0eSJeff Layton 	dprintk("AUTH_GSS upcall failed. Please check user daemon is running.\n");
536b03568c3S\"J. Bruce Fields\ }
537b03568c3S\"J. Bruce Fields\ 
5381da177e4SLinus Torvalds static inline int
5391da177e4SLinus Torvalds gss_refresh_upcall(struct rpc_task *task)
5401da177e4SLinus Torvalds {
541a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
5424a8c1344STrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth,
5431da177e4SLinus Torvalds 			struct gss_auth, rpc_auth);
5441da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred,
5451da177e4SLinus Torvalds 			struct gss_cred, gc_base);
5461da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
5479beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
5481da177e4SLinus Torvalds 	int err = 0;
5491da177e4SLinus Torvalds 
550632f0d05SChuck Lever 	dprintk("RPC: %5u %s for uid %u\n",
551cdba321eSEric W. Biederman 		task->tk_pid, __func__, from_kuid(&init_user_ns, cred->cr_uid));
552e726340aSTrond Myklebust 	gss_msg = gss_setup_upcall(gss_auth, cred);
553480e3243SRoel Kluin 	if (PTR_ERR(gss_msg) == -EAGAIN) {
55479a3f20bS\"J. Bruce Fields\ 		/* XXX: warning on the first, under the assumption we
55579a3f20bS\"J. Bruce Fields\ 		 * shouldn't normally hit this case on a refresh. */
55679a3f20bS\"J. Bruce Fields\ 		warn_gssd();
55779a3f20bS\"J. Bruce Fields\ 		task->tk_timeout = 15*HZ;
55879a3f20bS\"J. Bruce Fields\ 		rpc_sleep_on(&pipe_version_rpc_waitqueue, task, NULL);
559d1a8016aSBryan Schumaker 		return -EAGAIN;
56079a3f20bS\"J. Bruce Fields\ 	}
5611da177e4SLinus Torvalds 	if (IS_ERR(gss_msg)) {
5621da177e4SLinus Torvalds 		err = PTR_ERR(gss_msg);
5631da177e4SLinus Torvalds 		goto out;
5641da177e4SLinus Torvalds 	}
5659beae467SStanislav Kinsbursky 	pipe = gss_msg->pipe;
5669beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
5671da177e4SLinus Torvalds 	if (gss_cred->gc_upcall != NULL)
5685d00837bSTrond Myklebust 		rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL);
569126e216aSTrond Myklebust 	else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
5701da177e4SLinus Torvalds 		task->tk_timeout = 0;
5711da177e4SLinus Torvalds 		gss_cred->gc_upcall = gss_msg;
5721da177e4SLinus Torvalds 		/* gss_upcall_callback will release the reference to gss_upcall_msg */
5731da177e4SLinus Torvalds 		atomic_inc(&gss_msg->count);
5745d00837bSTrond Myklebust 		rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback);
575126e216aSTrond Myklebust 	} else {
576126e216aSTrond Myklebust 		gss_handle_downcall_result(gss_cred, gss_msg);
5771da177e4SLinus Torvalds 		err = gss_msg->msg.errno;
578126e216aSTrond Myklebust 	}
5799beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
5801da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
5811da177e4SLinus Torvalds out:
582632f0d05SChuck Lever 	dprintk("RPC: %5u %s for uid %u result %d\n",
583cdba321eSEric W. Biederman 		task->tk_pid, __func__,
584cdba321eSEric W. Biederman 		from_kuid(&init_user_ns, cred->cr_uid),	err);
5851da177e4SLinus Torvalds 	return err;
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds static inline int
5891da177e4SLinus Torvalds gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
5901da177e4SLinus Torvalds {
591e726340aSTrond Myklebust 	struct net *net = gss_auth->net;
592abfdbd53STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
5939beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe;
5941da177e4SLinus Torvalds 	struct rpc_cred *cred = &gss_cred->gc_base;
5951da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
5961da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
597d36ccb9cSTrond Myklebust 	int err;
5981da177e4SLinus Torvalds 
599cdba321eSEric W. Biederman 	dprintk("RPC:       %s for uid %u\n",
600cdba321eSEric W. Biederman 		__func__, from_kuid(&init_user_ns, cred->cr_uid));
60179a3f20bS\"J. Bruce Fields\ retry:
602d36ccb9cSTrond Myklebust 	err = 0;
60389f84243SJeff Layton 	/* if gssd is down, just skip upcalling altogether */
60489f84243SJeff Layton 	if (!gssd_running(net)) {
60589f84243SJeff Layton 		warn_gssd();
60689f84243SJeff Layton 		return -EACCES;
60789f84243SJeff Layton 	}
608e726340aSTrond Myklebust 	gss_msg = gss_setup_upcall(gss_auth, cred);
60979a3f20bS\"J. Bruce Fields\ 	if (PTR_ERR(gss_msg) == -EAGAIN) {
61079a3f20bS\"J. Bruce Fields\ 		err = wait_event_interruptible_timeout(pipe_version_waitqueue,
61189f84243SJeff Layton 				sn->pipe_version >= 0, 15 * HZ);
6122aed8b47STrond Myklebust 		if (sn->pipe_version < 0) {
613d1a8016aSBryan Schumaker 			warn_gssd();
614d1a8016aSBryan Schumaker 			err = -EACCES;
615d1a8016aSBryan Schumaker 		}
616d36ccb9cSTrond Myklebust 		if (err < 0)
61779a3f20bS\"J. Bruce Fields\ 			goto out;
61879a3f20bS\"J. Bruce Fields\ 		goto retry;
61979a3f20bS\"J. Bruce Fields\ 	}
6201da177e4SLinus Torvalds 	if (IS_ERR(gss_msg)) {
6211da177e4SLinus Torvalds 		err = PTR_ERR(gss_msg);
6221da177e4SLinus Torvalds 		goto out;
6231da177e4SLinus Torvalds 	}
6249beae467SStanislav Kinsbursky 	pipe = gss_msg->pipe;
6251da177e4SLinus Torvalds 	for (;;) {
6265afa9133STrond Myklebust 		prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_KILLABLE);
6279beae467SStanislav Kinsbursky 		spin_lock(&pipe->lock);
6281da177e4SLinus Torvalds 		if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
6291da177e4SLinus Torvalds 			break;
6301da177e4SLinus Torvalds 		}
6319beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
6325afa9133STrond Myklebust 		if (fatal_signal_pending(current)) {
6331da177e4SLinus Torvalds 			err = -ERESTARTSYS;
6341da177e4SLinus Torvalds 			goto out_intr;
6351da177e4SLinus Torvalds 		}
6361da177e4SLinus Torvalds 		schedule();
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 	if (gss_msg->ctx)
6397b6962b0STrond Myklebust 		gss_cred_set_ctx(cred, gss_msg->ctx);
6401da177e4SLinus Torvalds 	else
6411da177e4SLinus Torvalds 		err = gss_msg->msg.errno;
6429beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
6431da177e4SLinus Torvalds out_intr:
6441da177e4SLinus Torvalds 	finish_wait(&gss_msg->waitqueue, &wait);
6451da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
6461da177e4SLinus Torvalds out:
647632f0d05SChuck Lever 	dprintk("RPC:       %s for uid %u result %d\n",
648cdba321eSEric W. Biederman 		__func__, from_kuid(&init_user_ns, cred->cr_uid), err);
6491da177e4SLinus Torvalds 	return err;
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds #define MSG_BUF_MAXSIZE 1024
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds static ssize_t
6551da177e4SLinus Torvalds gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
6561da177e4SLinus Torvalds {
6571da177e4SLinus Torvalds 	const void *p, *end;
6581da177e4SLinus Torvalds 	void *buf;
6591da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
660496ad9aaSAl Viro 	struct rpc_pipe *pipe = RPC_I(file_inode(filp))->pipe;
6611da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx;
66290602c7bSEric W. Biederman 	uid_t id;
66390602c7bSEric W. Biederman 	kuid_t uid;
6643b68aaeaSTrond Myklebust 	ssize_t err = -EFBIG;
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	if (mlen > MSG_BUF_MAXSIZE)
6671da177e4SLinus Torvalds 		goto out;
6681da177e4SLinus Torvalds 	err = -ENOMEM;
6690f38b873STrond Myklebust 	buf = kmalloc(mlen, GFP_NOFS);
6701da177e4SLinus Torvalds 	if (!buf)
6711da177e4SLinus Torvalds 		goto out;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	err = -EFAULT;
6741da177e4SLinus Torvalds 	if (copy_from_user(buf, src, mlen))
6751da177e4SLinus Torvalds 		goto err;
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	end = (const void *)((char *)buf + mlen);
67890602c7bSEric W. Biederman 	p = simple_get_bytes(buf, end, &id, sizeof(id));
6791da177e4SLinus Torvalds 	if (IS_ERR(p)) {
6801da177e4SLinus Torvalds 		err = PTR_ERR(p);
6811da177e4SLinus Torvalds 		goto err;
6821da177e4SLinus Torvalds 	}
6831da177e4SLinus Torvalds 
68490602c7bSEric W. Biederman 	uid = make_kuid(&init_user_ns, id);
68590602c7bSEric W. Biederman 	if (!uid_valid(uid)) {
68690602c7bSEric W. Biederman 		err = -EINVAL;
68790602c7bSEric W. Biederman 		goto err;
68890602c7bSEric W. Biederman 	}
68990602c7bSEric W. Biederman 
6901da177e4SLinus Torvalds 	err = -ENOMEM;
6911da177e4SLinus Torvalds 	ctx = gss_alloc_context();
6921da177e4SLinus Torvalds 	if (ctx == NULL)
6931da177e4SLinus Torvalds 		goto err;
6943b68aaeaSTrond Myklebust 
6953b68aaeaSTrond Myklebust 	err = -ENOENT;
6963b68aaeaSTrond Myklebust 	/* Find a matching upcall */
6979beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
6989beae467SStanislav Kinsbursky 	gss_msg = __gss_find_upcall(pipe, uid);
6993b68aaeaSTrond Myklebust 	if (gss_msg == NULL) {
7009beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
7013b68aaeaSTrond Myklebust 		goto err_put_ctx;
7023b68aaeaSTrond Myklebust 	}
7033b68aaeaSTrond Myklebust 	list_del_init(&gss_msg->list);
7049beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
7053b68aaeaSTrond Myklebust 
7066e84c7b6STrond Myklebust 	p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
7071da177e4SLinus Torvalds 	if (IS_ERR(p)) {
7081da177e4SLinus Torvalds 		err = PTR_ERR(p);
709486bad2eSJeff Layton 		switch (err) {
710486bad2eSJeff Layton 		case -EACCES:
711dc5ddce9SJeff Layton 		case -EKEYEXPIRED:
712486bad2eSJeff Layton 			gss_msg->msg.errno = err;
713486bad2eSJeff Layton 			err = mlen;
714486bad2eSJeff Layton 			break;
715486bad2eSJeff Layton 		case -EFAULT:
716486bad2eSJeff Layton 		case -ENOMEM:
717486bad2eSJeff Layton 		case -EINVAL:
718486bad2eSJeff Layton 		case -ENOSYS:
719486bad2eSJeff Layton 			gss_msg->msg.errno = -EAGAIN;
720486bad2eSJeff Layton 			break;
721486bad2eSJeff Layton 		default:
722486bad2eSJeff Layton 			printk(KERN_CRIT "%s: bad return from "
7236c853099SRandy Dunlap 				"gss_fill_context: %zd\n", __func__, err);
724486bad2eSJeff Layton 			BUG();
725486bad2eSJeff Layton 		}
7263b68aaeaSTrond Myklebust 		goto err_release_msg;
7271da177e4SLinus Torvalds 	}
7281da177e4SLinus Torvalds 	gss_msg->ctx = gss_get_ctx(ctx);
7293b68aaeaSTrond Myklebust 	err = mlen;
7303b68aaeaSTrond Myklebust 
7313b68aaeaSTrond Myklebust err_release_msg:
7329beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
7331da177e4SLinus Torvalds 	__gss_unhash_msg(gss_msg);
7349beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
7351da177e4SLinus Torvalds 	gss_release_msg(gss_msg);
7361da177e4SLinus Torvalds err_put_ctx:
7371da177e4SLinus Torvalds 	gss_put_ctx(ctx);
7381da177e4SLinus Torvalds err:
7391da177e4SLinus Torvalds 	kfree(buf);
7401da177e4SLinus Torvalds out:
741632f0d05SChuck Lever 	dprintk("RPC:       %s returning %Zd\n", __func__, err);
7421da177e4SLinus Torvalds 	return err;
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds 
74534769fc4S\"J. Bruce Fields\ static int gss_pipe_open(struct inode *inode, int new_version)
746cf81939dS\"J. Bruce Fields\ {
7472aed8b47STrond Myklebust 	struct net *net = inode->i_sb->s_fs_info;
7482aed8b47STrond Myklebust 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
74934769fc4S\"J. Bruce Fields\ 	int ret = 0;
75034769fc4S\"J. Bruce Fields\ 
75179a3f20bS\"J. Bruce Fields\ 	spin_lock(&pipe_version_lock);
7522aed8b47STrond Myklebust 	if (sn->pipe_version < 0) {
75334769fc4S\"J. Bruce Fields\ 		/* First open of any gss pipe determines the version: */
7542aed8b47STrond Myklebust 		sn->pipe_version = new_version;
75579a3f20bS\"J. Bruce Fields\ 		rpc_wake_up(&pipe_version_rpc_waitqueue);
75679a3f20bS\"J. Bruce Fields\ 		wake_up(&pipe_version_waitqueue);
7572aed8b47STrond Myklebust 	} else if (sn->pipe_version != new_version) {
75834769fc4S\"J. Bruce Fields\ 		/* Trying to open a pipe of a different version */
75934769fc4S\"J. Bruce Fields\ 		ret = -EBUSY;
76034769fc4S\"J. Bruce Fields\ 		goto out;
76179a3f20bS\"J. Bruce Fields\ 	}
7622aed8b47STrond Myklebust 	atomic_inc(&sn->pipe_users);
76334769fc4S\"J. Bruce Fields\ out:
76479a3f20bS\"J. Bruce Fields\ 	spin_unlock(&pipe_version_lock);
76534769fc4S\"J. Bruce Fields\ 	return ret;
76634769fc4S\"J. Bruce Fields\ 
76734769fc4S\"J. Bruce Fields\ }
76834769fc4S\"J. Bruce Fields\ 
76934769fc4S\"J. Bruce Fields\ static int gss_pipe_open_v0(struct inode *inode)
77034769fc4S\"J. Bruce Fields\ {
77134769fc4S\"J. Bruce Fields\ 	return gss_pipe_open(inode, 0);
77234769fc4S\"J. Bruce Fields\ }
77334769fc4S\"J. Bruce Fields\ 
77434769fc4S\"J. Bruce Fields\ static int gss_pipe_open_v1(struct inode *inode)
77534769fc4S\"J. Bruce Fields\ {
77634769fc4S\"J. Bruce Fields\ 	return gss_pipe_open(inode, 1);
777cf81939dS\"J. Bruce Fields\ }
778cf81939dS\"J. Bruce Fields\ 
7791da177e4SLinus Torvalds static void
7801da177e4SLinus Torvalds gss_pipe_release(struct inode *inode)
7811da177e4SLinus Torvalds {
7822aed8b47STrond Myklebust 	struct net *net = inode->i_sb->s_fs_info;
7839beae467SStanislav Kinsbursky 	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
7841da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg;
7851da177e4SLinus Torvalds 
7865a67657aSTrond Myklebust restart:
7879beae467SStanislav Kinsbursky 	spin_lock(&pipe->lock);
7889beae467SStanislav Kinsbursky 	list_for_each_entry(gss_msg, &pipe->in_downcall, list) {
7896e84c7b6STrond Myklebust 
7905a67657aSTrond Myklebust 		if (!list_empty(&gss_msg->msg.list))
7915a67657aSTrond Myklebust 			continue;
7921da177e4SLinus Torvalds 		gss_msg->msg.errno = -EPIPE;
7931da177e4SLinus Torvalds 		atomic_inc(&gss_msg->count);
7941da177e4SLinus Torvalds 		__gss_unhash_msg(gss_msg);
7959beae467SStanislav Kinsbursky 		spin_unlock(&pipe->lock);
7961da177e4SLinus Torvalds 		gss_release_msg(gss_msg);
7975a67657aSTrond Myklebust 		goto restart;
7981da177e4SLinus Torvalds 	}
7999beae467SStanislav Kinsbursky 	spin_unlock(&pipe->lock);
800cf81939dS\"J. Bruce Fields\ 
8012aed8b47STrond Myklebust 	put_pipe_version(net);
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds static void
8051da177e4SLinus Torvalds gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
8061da177e4SLinus Torvalds {
8071da177e4SLinus Torvalds 	struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	if (msg->errno < 0) {
810632f0d05SChuck Lever 		dprintk("RPC:       %s releasing msg %p\n",
811632f0d05SChuck Lever 			__func__, gss_msg);
8121da177e4SLinus Torvalds 		atomic_inc(&gss_msg->count);
8131da177e4SLinus Torvalds 		gss_unhash_msg(gss_msg);
814b03568c3S\"J. Bruce Fields\ 		if (msg->errno == -ETIMEDOUT)
815b03568c3S\"J. Bruce Fields\ 			warn_gssd();
8161da177e4SLinus Torvalds 		gss_release_msg(gss_msg);
8171da177e4SLinus Torvalds 	}
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
82019172284STrond Myklebust static void gss_pipe_dentry_destroy(struct dentry *dir,
82119172284STrond Myklebust 		struct rpc_pipe_dir_object *pdo)
822ccdc28f8SStanislav Kinsbursky {
82319172284STrond Myklebust 	struct gss_pipe *gss_pipe = pdo->pdo_data;
82419172284STrond Myklebust 	struct rpc_pipe *pipe = gss_pipe->pipe;
825ccdc28f8SStanislav Kinsbursky 
82619172284STrond Myklebust 	if (pipe->dentry != NULL) {
82719172284STrond Myklebust 		rpc_unlink(pipe->dentry);
82819172284STrond Myklebust 		pipe->dentry = NULL;
8296b2fddd3STrond Myklebust 	}
830ccdc28f8SStanislav Kinsbursky }
831ccdc28f8SStanislav Kinsbursky 
83219172284STrond Myklebust static int gss_pipe_dentry_create(struct dentry *dir,
83319172284STrond Myklebust 		struct rpc_pipe_dir_object *pdo)
834ccdc28f8SStanislav Kinsbursky {
83519172284STrond Myklebust 	struct gss_pipe *p = pdo->pdo_data;
8366b2fddd3STrond Myklebust 	struct dentry *dentry;
837ccdc28f8SStanislav Kinsbursky 
83819172284STrond Myklebust 	dentry = rpc_mkpipe_dentry(dir, p->name, p->clnt, p->pipe);
83919172284STrond Myklebust 	if (IS_ERR(dentry))
84019172284STrond Myklebust 		return PTR_ERR(dentry);
84119172284STrond Myklebust 	p->pipe->dentry = dentry;
842ccdc28f8SStanislav Kinsbursky 	return 0;
84319172284STrond Myklebust }
844ccdc28f8SStanislav Kinsbursky 
84519172284STrond Myklebust static const struct rpc_pipe_dir_object_ops gss_pipe_dir_object_ops = {
84619172284STrond Myklebust 	.create = gss_pipe_dentry_create,
84719172284STrond Myklebust 	.destroy = gss_pipe_dentry_destroy,
84819172284STrond Myklebust };
84919172284STrond Myklebust 
85019172284STrond Myklebust static struct gss_pipe *gss_pipe_alloc(struct rpc_clnt *clnt,
85119172284STrond Myklebust 		const char *name,
85219172284STrond Myklebust 		const struct rpc_pipe_ops *upcall_ops)
85319172284STrond Myklebust {
85419172284STrond Myklebust 	struct gss_pipe *p;
85519172284STrond Myklebust 	int err = -ENOMEM;
85619172284STrond Myklebust 
85719172284STrond Myklebust 	p = kmalloc(sizeof(*p), GFP_KERNEL);
85819172284STrond Myklebust 	if (p == NULL)
85919172284STrond Myklebust 		goto err;
86019172284STrond Myklebust 	p->pipe = rpc_mkpipe_data(upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
86119172284STrond Myklebust 	if (IS_ERR(p->pipe)) {
86219172284STrond Myklebust 		err = PTR_ERR(p->pipe);
86319172284STrond Myklebust 		goto err_free_gss_pipe;
86419172284STrond Myklebust 	}
86519172284STrond Myklebust 	p->name = name;
86619172284STrond Myklebust 	p->clnt = clnt;
867414a6295STrond Myklebust 	kref_init(&p->kref);
86819172284STrond Myklebust 	rpc_init_pipe_dir_object(&p->pdo,
86919172284STrond Myklebust 			&gss_pipe_dir_object_ops,
87019172284STrond Myklebust 			p);
87119172284STrond Myklebust 	return p;
87219172284STrond Myklebust err_free_gss_pipe:
87319172284STrond Myklebust 	kfree(p);
8746b2fddd3STrond Myklebust err:
87519172284STrond Myklebust 	return ERR_PTR(err);
876ccdc28f8SStanislav Kinsbursky }
877ccdc28f8SStanislav Kinsbursky 
878414a6295STrond Myklebust struct gss_alloc_pdo {
879414a6295STrond Myklebust 	struct rpc_clnt *clnt;
880414a6295STrond Myklebust 	const char *name;
881414a6295STrond Myklebust 	const struct rpc_pipe_ops *upcall_ops;
882414a6295STrond Myklebust };
883414a6295STrond Myklebust 
884414a6295STrond Myklebust static int gss_pipe_match_pdo(struct rpc_pipe_dir_object *pdo, void *data)
885414a6295STrond Myklebust {
886414a6295STrond Myklebust 	struct gss_pipe *gss_pipe;
887414a6295STrond Myklebust 	struct gss_alloc_pdo *args = data;
888414a6295STrond Myklebust 
889414a6295STrond Myklebust 	if (pdo->pdo_ops != &gss_pipe_dir_object_ops)
890414a6295STrond Myklebust 		return 0;
891414a6295STrond Myklebust 	gss_pipe = container_of(pdo, struct gss_pipe, pdo);
892414a6295STrond Myklebust 	if (strcmp(gss_pipe->name, args->name) != 0)
893414a6295STrond Myklebust 		return 0;
894414a6295STrond Myklebust 	if (!kref_get_unless_zero(&gss_pipe->kref))
895414a6295STrond Myklebust 		return 0;
896414a6295STrond Myklebust 	return 1;
897414a6295STrond Myklebust }
898414a6295STrond Myklebust 
899414a6295STrond Myklebust static struct rpc_pipe_dir_object *gss_pipe_alloc_pdo(void *data)
900414a6295STrond Myklebust {
901414a6295STrond Myklebust 	struct gss_pipe *gss_pipe;
902414a6295STrond Myklebust 	struct gss_alloc_pdo *args = data;
903414a6295STrond Myklebust 
904414a6295STrond Myklebust 	gss_pipe = gss_pipe_alloc(args->clnt, args->name, args->upcall_ops);
905414a6295STrond Myklebust 	if (!IS_ERR(gss_pipe))
906414a6295STrond Myklebust 		return &gss_pipe->pdo;
907414a6295STrond Myklebust 	return NULL;
908414a6295STrond Myklebust }
909414a6295STrond Myklebust 
910414a6295STrond Myklebust static struct gss_pipe *gss_pipe_get(struct rpc_clnt *clnt,
911414a6295STrond Myklebust 		const char *name,
912414a6295STrond Myklebust 		const struct rpc_pipe_ops *upcall_ops)
913414a6295STrond Myklebust {
914414a6295STrond Myklebust 	struct net *net = rpc_net_ns(clnt);
915414a6295STrond Myklebust 	struct rpc_pipe_dir_object *pdo;
916414a6295STrond Myklebust 	struct gss_alloc_pdo args = {
917414a6295STrond Myklebust 		.clnt = clnt,
918414a6295STrond Myklebust 		.name = name,
919414a6295STrond Myklebust 		.upcall_ops = upcall_ops,
920414a6295STrond Myklebust 	};
921414a6295STrond Myklebust 
922414a6295STrond Myklebust 	pdo = rpc_find_or_alloc_pipe_dir_object(net,
923414a6295STrond Myklebust 			&clnt->cl_pipedir_objects,
924414a6295STrond Myklebust 			gss_pipe_match_pdo,
925414a6295STrond Myklebust 			gss_pipe_alloc_pdo,
926414a6295STrond Myklebust 			&args);
927414a6295STrond Myklebust 	if (pdo != NULL)
928414a6295STrond Myklebust 		return container_of(pdo, struct gss_pipe, pdo);
929414a6295STrond Myklebust 	return ERR_PTR(-ENOMEM);
930414a6295STrond Myklebust }
931414a6295STrond Myklebust 
93219172284STrond Myklebust static void __gss_pipe_free(struct gss_pipe *p)
933ccdc28f8SStanislav Kinsbursky {
93419172284STrond Myklebust 	struct rpc_clnt *clnt = p->clnt;
93519172284STrond Myklebust 	struct net *net = rpc_net_ns(clnt);
936ccdc28f8SStanislav Kinsbursky 
93719172284STrond Myklebust 	rpc_remove_pipe_dir_object(net,
93819172284STrond Myklebust 			&clnt->cl_pipedir_objects,
93919172284STrond Myklebust 			&p->pdo);
94019172284STrond Myklebust 	rpc_destroy_pipe_data(p->pipe);
94119172284STrond Myklebust 	kfree(p);
942ccdc28f8SStanislav Kinsbursky }
943ccdc28f8SStanislav Kinsbursky 
944414a6295STrond Myklebust static void __gss_pipe_release(struct kref *kref)
945414a6295STrond Myklebust {
946414a6295STrond Myklebust 	struct gss_pipe *p = container_of(kref, struct gss_pipe, kref);
947414a6295STrond Myklebust 
948414a6295STrond Myklebust 	__gss_pipe_free(p);
949414a6295STrond Myklebust }
950414a6295STrond Myklebust 
95119172284STrond Myklebust static void gss_pipe_free(struct gss_pipe *p)
952ccdc28f8SStanislav Kinsbursky {
95319172284STrond Myklebust 	if (p != NULL)
954414a6295STrond Myklebust 		kref_put(&p->kref, __gss_pipe_release);
955ccdc28f8SStanislav Kinsbursky }
956ccdc28f8SStanislav Kinsbursky 
9571da177e4SLinus Torvalds /*
9581da177e4SLinus Torvalds  * NOTE: we have the opportunity to use different
9591da177e4SLinus Torvalds  * parameters based on the input flavor (which must be a pseudoflavor)
9601da177e4SLinus Torvalds  */
961eb6dc19dSTrond Myklebust static struct gss_auth *
962eb6dc19dSTrond Myklebust gss_create_new(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
9631da177e4SLinus Torvalds {
964c2190661STrond Myklebust 	rpc_authflavor_t flavor = args->pseudoflavor;
9651da177e4SLinus Torvalds 	struct gss_auth *gss_auth;
96619172284STrond Myklebust 	struct gss_pipe *gss_pipe;
9671da177e4SLinus Torvalds 	struct rpc_auth * auth;
9686a19275aSJ. Bruce Fields 	int err = -ENOMEM; /* XXX? */
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	dprintk("RPC:       creating GSS authenticator for client %p\n", clnt);
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 	if (!try_module_get(THIS_MODULE))
9736a19275aSJ. Bruce Fields 		return ERR_PTR(err);
9741da177e4SLinus Torvalds 	if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
9751da177e4SLinus Torvalds 		goto out_dec;
976eb6dc19dSTrond Myklebust 	INIT_HLIST_NODE(&gss_auth->hash);
977bd4a3eb1STrond Myklebust 	gss_auth->target_name = NULL;
978c2190661STrond Myklebust 	if (args->target_name) {
979c2190661STrond Myklebust 		gss_auth->target_name = kstrdup(args->target_name, GFP_KERNEL);
980bd4a3eb1STrond Myklebust 		if (gss_auth->target_name == NULL)
981bd4a3eb1STrond Myklebust 			goto err_free;
982bd4a3eb1STrond Myklebust 	}
9831da177e4SLinus Torvalds 	gss_auth->client = clnt;
984e726340aSTrond Myklebust 	gss_auth->net = get_net(rpc_net_ns(clnt));
9856a19275aSJ. Bruce Fields 	err = -EINVAL;
9861da177e4SLinus Torvalds 	gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
9871da177e4SLinus Torvalds 	if (!gss_auth->mech) {
9889b1d75b7STrond Myklebust 		dprintk("RPC:       Pseudoflavor %d not found!\n", flavor);
989e726340aSTrond Myklebust 		goto err_put_net;
9901da177e4SLinus Torvalds 	}
9911da177e4SLinus Torvalds 	gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
992438b6fdeSJ. Bruce Fields 	if (gss_auth->service == 0)
993438b6fdeSJ. Bruce Fields 		goto err_put_mech;
994a699d65eSTrond Myklebust 	if (!gssd_running(gss_auth->net))
995a699d65eSTrond Myklebust 		goto err_put_mech;
9961da177e4SLinus Torvalds 	auth = &gss_auth->rpc_auth;
9971da177e4SLinus Torvalds 	auth->au_cslack = GSS_CRED_SLACK >> 2;
9981da177e4SLinus Torvalds 	auth->au_rslack = GSS_VERF_SLACK >> 2;
9991da177e4SLinus Torvalds 	auth->au_ops = &authgss_ops;
10001da177e4SLinus Torvalds 	auth->au_flavor = flavor;
10011da177e4SLinus Torvalds 	atomic_set(&auth->au_count, 1);
10020285ed1fSTrond Myklebust 	kref_init(&gss_auth->kref);
10031da177e4SLinus Torvalds 
100419172284STrond Myklebust 	err = rpcauth_init_credcache(auth);
100519172284STrond Myklebust 	if (err)
100619172284STrond Myklebust 		goto err_put_mech;
100734769fc4S\"J. Bruce Fields\ 	/*
100834769fc4S\"J. Bruce Fields\ 	 * Note: if we created the old pipe first, then someone who
100934769fc4S\"J. Bruce Fields\ 	 * examined the directory at the right moment might conclude
101034769fc4S\"J. Bruce Fields\ 	 * that we supported only the old pipe.  So we instead create
101134769fc4S\"J. Bruce Fields\ 	 * the new pipe first.
101234769fc4S\"J. Bruce Fields\ 	 */
1013414a6295STrond Myklebust 	gss_pipe = gss_pipe_get(clnt, "gssd", &gss_upcall_ops_v1);
101419172284STrond Myklebust 	if (IS_ERR(gss_pipe)) {
101519172284STrond Myklebust 		err = PTR_ERR(gss_pipe);
101619172284STrond Myklebust 		goto err_destroy_credcache;
10176a19275aSJ. Bruce Fields 	}
101819172284STrond Myklebust 	gss_auth->gss_pipe[1] = gss_pipe;
10191da177e4SLinus Torvalds 
1020414a6295STrond Myklebust 	gss_pipe = gss_pipe_get(clnt, gss_auth->mech->gm_name,
102119172284STrond Myklebust 			&gss_upcall_ops_v0);
102219172284STrond Myklebust 	if (IS_ERR(gss_pipe)) {
102319172284STrond Myklebust 		err = PTR_ERR(gss_pipe);
1024c239d83bSStanislav Kinsbursky 		goto err_destroy_pipe_1;
1025c239d83bSStanislav Kinsbursky 	}
102619172284STrond Myklebust 	gss_auth->gss_pipe[0] = gss_pipe;
102707a2bf1dSTrond Myklebust 
1028eb6dc19dSTrond Myklebust 	return gss_auth;
1029c239d83bSStanislav Kinsbursky err_destroy_pipe_1:
1030414a6295STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[1]);
103119172284STrond Myklebust err_destroy_credcache:
103219172284STrond Myklebust 	rpcauth_destroy_credcache(auth);
10331da177e4SLinus Torvalds err_put_mech:
10341da177e4SLinus Torvalds 	gss_mech_put(gss_auth->mech);
1035e726340aSTrond Myklebust err_put_net:
1036e726340aSTrond Myklebust 	put_net(gss_auth->net);
10371da177e4SLinus Torvalds err_free:
1038bd4a3eb1STrond Myklebust 	kfree(gss_auth->target_name);
10391da177e4SLinus Torvalds 	kfree(gss_auth);
10401da177e4SLinus Torvalds out_dec:
10411da177e4SLinus Torvalds 	module_put(THIS_MODULE);
10426a19275aSJ. Bruce Fields 	return ERR_PTR(err);
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds static void
10460285ed1fSTrond Myklebust gss_free(struct gss_auth *gss_auth)
10470285ed1fSTrond Myklebust {
104819172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[0]);
104919172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[1]);
10500285ed1fSTrond Myklebust 	gss_mech_put(gss_auth->mech);
1051e726340aSTrond Myklebust 	put_net(gss_auth->net);
1052bd4a3eb1STrond Myklebust 	kfree(gss_auth->target_name);
10530285ed1fSTrond Myklebust 
10540285ed1fSTrond Myklebust 	kfree(gss_auth);
10550285ed1fSTrond Myklebust 	module_put(THIS_MODULE);
10560285ed1fSTrond Myklebust }
10570285ed1fSTrond Myklebust 
10580285ed1fSTrond Myklebust static void
10590285ed1fSTrond Myklebust gss_free_callback(struct kref *kref)
10600285ed1fSTrond Myklebust {
10610285ed1fSTrond Myklebust 	struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
10620285ed1fSTrond Myklebust 
10630285ed1fSTrond Myklebust 	gss_free(gss_auth);
10640285ed1fSTrond Myklebust }
10650285ed1fSTrond Myklebust 
10660285ed1fSTrond Myklebust static void
10671da177e4SLinus Torvalds gss_destroy(struct rpc_auth *auth)
10681da177e4SLinus Torvalds {
106919172284STrond Myklebust 	struct gss_auth *gss_auth = container_of(auth,
107019172284STrond Myklebust 			struct gss_auth, rpc_auth);
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds 	dprintk("RPC:       destroying GSS authenticator %p flavor %d\n",
10731da177e4SLinus Torvalds 			auth, auth->au_flavor);
10741da177e4SLinus Torvalds 
1075eb6dc19dSTrond Myklebust 	if (hash_hashed(&gss_auth->hash)) {
1076eb6dc19dSTrond Myklebust 		spin_lock(&gss_auth_hash_lock);
1077eb6dc19dSTrond Myklebust 		hash_del(&gss_auth->hash);
1078eb6dc19dSTrond Myklebust 		spin_unlock(&gss_auth_hash_lock);
1079eb6dc19dSTrond Myklebust 	}
1080eb6dc19dSTrond Myklebust 
108119172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[0]);
108219172284STrond Myklebust 	gss_auth->gss_pipe[0] = NULL;
108319172284STrond Myklebust 	gss_pipe_free(gss_auth->gss_pipe[1]);
108419172284STrond Myklebust 	gss_auth->gss_pipe[1] = NULL;
10853ab9bb72STrond Myklebust 	rpcauth_destroy_credcache(auth);
10863ab9bb72STrond Myklebust 
10870285ed1fSTrond Myklebust 	kref_put(&gss_auth->kref, gss_free_callback);
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds 
1090a0f6ed8eSJ. Bruce Fields /*
1091a0f6ed8eSJ. Bruce Fields  * Auths may be shared between rpc clients that were cloned from a
1092a0f6ed8eSJ. Bruce Fields  * common client with the same xprt, if they also share the flavor and
1093a0f6ed8eSJ. Bruce Fields  * target_name.
1094a0f6ed8eSJ. Bruce Fields  *
1095a0f6ed8eSJ. Bruce Fields  * The auth is looked up from the oldest parent sharing the same
1096a0f6ed8eSJ. Bruce Fields  * cl_xprt, and the auth itself references only that common parent
1097a0f6ed8eSJ. Bruce Fields  * (which is guaranteed to last as long as any of its descendants).
1098a0f6ed8eSJ. Bruce Fields  */
1099eb6dc19dSTrond Myklebust static struct gss_auth *
1100eb6dc19dSTrond Myklebust gss_auth_find_or_add_hashed(struct rpc_auth_create_args *args,
1101eb6dc19dSTrond Myklebust 		struct rpc_clnt *clnt,
1102eb6dc19dSTrond Myklebust 		struct gss_auth *new)
1103eb6dc19dSTrond Myklebust {
1104eb6dc19dSTrond Myklebust 	struct gss_auth *gss_auth;
1105eb6dc19dSTrond Myklebust 	unsigned long hashval = (unsigned long)clnt;
1106eb6dc19dSTrond Myklebust 
1107eb6dc19dSTrond Myklebust 	spin_lock(&gss_auth_hash_lock);
1108eb6dc19dSTrond Myklebust 	hash_for_each_possible(gss_auth_hash_table,
1109eb6dc19dSTrond Myklebust 			gss_auth,
1110eb6dc19dSTrond Myklebust 			hash,
1111eb6dc19dSTrond Myklebust 			hashval) {
1112a0f6ed8eSJ. Bruce Fields 		if (gss_auth->client != clnt)
1113a0f6ed8eSJ. Bruce Fields 			continue;
1114eb6dc19dSTrond Myklebust 		if (gss_auth->rpc_auth.au_flavor != args->pseudoflavor)
1115eb6dc19dSTrond Myklebust 			continue;
1116eb6dc19dSTrond Myklebust 		if (gss_auth->target_name != args->target_name) {
1117eb6dc19dSTrond Myklebust 			if (gss_auth->target_name == NULL)
1118eb6dc19dSTrond Myklebust 				continue;
1119eb6dc19dSTrond Myklebust 			if (args->target_name == NULL)
1120eb6dc19dSTrond Myklebust 				continue;
1121eb6dc19dSTrond Myklebust 			if (strcmp(gss_auth->target_name, args->target_name))
1122eb6dc19dSTrond Myklebust 				continue;
1123eb6dc19dSTrond Myklebust 		}
1124eb6dc19dSTrond Myklebust 		if (!atomic_inc_not_zero(&gss_auth->rpc_auth.au_count))
1125eb6dc19dSTrond Myklebust 			continue;
1126eb6dc19dSTrond Myklebust 		goto out;
1127eb6dc19dSTrond Myklebust 	}
1128eb6dc19dSTrond Myklebust 	if (new)
1129eb6dc19dSTrond Myklebust 		hash_add(gss_auth_hash_table, &new->hash, hashval);
1130eb6dc19dSTrond Myklebust 	gss_auth = new;
1131eb6dc19dSTrond Myklebust out:
1132eb6dc19dSTrond Myklebust 	spin_unlock(&gss_auth_hash_lock);
1133eb6dc19dSTrond Myklebust 	return gss_auth;
1134eb6dc19dSTrond Myklebust }
1135eb6dc19dSTrond Myklebust 
1136eb6dc19dSTrond Myklebust static struct gss_auth *
1137eb6dc19dSTrond Myklebust gss_create_hashed(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
1138eb6dc19dSTrond Myklebust {
1139eb6dc19dSTrond Myklebust 	struct gss_auth *gss_auth;
1140eb6dc19dSTrond Myklebust 	struct gss_auth *new;
1141eb6dc19dSTrond Myklebust 
1142eb6dc19dSTrond Myklebust 	gss_auth = gss_auth_find_or_add_hashed(args, clnt, NULL);
1143eb6dc19dSTrond Myklebust 	if (gss_auth != NULL)
1144eb6dc19dSTrond Myklebust 		goto out;
1145eb6dc19dSTrond Myklebust 	new = gss_create_new(args, clnt);
1146eb6dc19dSTrond Myklebust 	if (IS_ERR(new))
1147eb6dc19dSTrond Myklebust 		return new;
1148eb6dc19dSTrond Myklebust 	gss_auth = gss_auth_find_or_add_hashed(args, clnt, new);
1149eb6dc19dSTrond Myklebust 	if (gss_auth != new)
1150eb6dc19dSTrond Myklebust 		gss_destroy(&new->rpc_auth);
1151eb6dc19dSTrond Myklebust out:
1152eb6dc19dSTrond Myklebust 	return gss_auth;
1153eb6dc19dSTrond Myklebust }
1154eb6dc19dSTrond Myklebust 
1155eb6dc19dSTrond Myklebust static struct rpc_auth *
1156eb6dc19dSTrond Myklebust gss_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
1157eb6dc19dSTrond Myklebust {
1158eb6dc19dSTrond Myklebust 	struct gss_auth *gss_auth;
1159eb6dc19dSTrond Myklebust 	struct rpc_xprt *xprt = rcu_access_pointer(clnt->cl_xprt);
1160eb6dc19dSTrond Myklebust 
1161eb6dc19dSTrond Myklebust 	while (clnt != clnt->cl_parent) {
1162eb6dc19dSTrond Myklebust 		struct rpc_clnt *parent = clnt->cl_parent;
1163eb6dc19dSTrond Myklebust 		/* Find the original parent for this transport */
1164eb6dc19dSTrond Myklebust 		if (rcu_access_pointer(parent->cl_xprt) != xprt)
1165eb6dc19dSTrond Myklebust 			break;
1166eb6dc19dSTrond Myklebust 		clnt = parent;
1167eb6dc19dSTrond Myklebust 	}
1168eb6dc19dSTrond Myklebust 
1169eb6dc19dSTrond Myklebust 	gss_auth = gss_create_hashed(args, clnt);
1170eb6dc19dSTrond Myklebust 	if (IS_ERR(gss_auth))
1171eb6dc19dSTrond Myklebust 		return ERR_CAST(gss_auth);
1172eb6dc19dSTrond Myklebust 	return &gss_auth->rpc_auth;
1173eb6dc19dSTrond Myklebust }
1174eb6dc19dSTrond Myklebust 
11750df7fb74STrond Myklebust /*
11760df7fb74STrond Myklebust  * gss_destroying_context will cause the RPCSEC_GSS to send a NULL RPC call
11770df7fb74STrond Myklebust  * to the server with the GSS control procedure field set to
11780df7fb74STrond Myklebust  * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
11790df7fb74STrond Myklebust  * all RPCSEC_GSS state associated with that context.
11800df7fb74STrond Myklebust  */
11810df7fb74STrond Myklebust static int
11820df7fb74STrond Myklebust gss_destroying_context(struct rpc_cred *cred)
11830df7fb74STrond Myklebust {
11840df7fb74STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
11850df7fb74STrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
11860df7fb74STrond Myklebust 	struct rpc_task *task;
11870df7fb74STrond Myklebust 
11880df7fb74STrond Myklebust 	if (gss_cred->gc_ctx == NULL ||
11896dcd3926SJeff Layton 	    test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
11900df7fb74STrond Myklebust 		return 0;
11910df7fb74STrond Myklebust 
11920df7fb74STrond Myklebust 	gss_cred->gc_ctx->gc_proc = RPC_GSS_PROC_DESTROY;
11930df7fb74STrond Myklebust 	cred->cr_ops = &gss_nullops;
11940df7fb74STrond Myklebust 
11950df7fb74STrond Myklebust 	/* Take a reference to ensure the cred will be destroyed either
11960df7fb74STrond Myklebust 	 * by the RPC call or by the put_rpccred() below */
11970df7fb74STrond Myklebust 	get_rpccred(cred);
11980df7fb74STrond Myklebust 
1199080a1f14STrond Myklebust 	task = rpc_call_null(gss_auth->client, cred, RPC_TASK_ASYNC|RPC_TASK_SOFT);
12000df7fb74STrond Myklebust 	if (!IS_ERR(task))
12010df7fb74STrond Myklebust 		rpc_put_task(task);
12020df7fb74STrond Myklebust 
12030df7fb74STrond Myklebust 	put_rpccred(cred);
12040df7fb74STrond Myklebust 	return 1;
12050df7fb74STrond Myklebust }
12060df7fb74STrond Myklebust 
12070df7fb74STrond Myklebust /* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
12081da177e4SLinus Torvalds  * to create a new cred or context, so they check that things have been
12091da177e4SLinus Torvalds  * allocated before freeing them. */
12101da177e4SLinus Torvalds static void
12115d28dc82STrond Myklebust gss_do_free_ctx(struct gss_cl_ctx *ctx)
12121da177e4SLinus Torvalds {
1213632f0d05SChuck Lever 	dprintk("RPC:       %s\n", __func__);
12141da177e4SLinus Torvalds 
12150d8a3746STrond Myklebust 	gss_delete_sec_context(&ctx->gc_gss_ctx);
12161da177e4SLinus Torvalds 	kfree(ctx->gc_wire_ctx.data);
12171da177e4SLinus Torvalds 	kfree(ctx);
12181da177e4SLinus Torvalds }
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds static void
12215d28dc82STrond Myklebust gss_free_ctx_callback(struct rcu_head *head)
12225d28dc82STrond Myklebust {
12235d28dc82STrond Myklebust 	struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
12245d28dc82STrond Myklebust 	gss_do_free_ctx(ctx);
12255d28dc82STrond Myklebust }
12265d28dc82STrond Myklebust 
12275d28dc82STrond Myklebust static void
12285d28dc82STrond Myklebust gss_free_ctx(struct gss_cl_ctx *ctx)
12295d28dc82STrond Myklebust {
12305d28dc82STrond Myklebust 	call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
12315d28dc82STrond Myklebust }
12325d28dc82STrond Myklebust 
12335d28dc82STrond Myklebust static void
123431be5bf1STrond Myklebust gss_free_cred(struct gss_cred *gss_cred)
12351da177e4SLinus Torvalds {
1236632f0d05SChuck Lever 	dprintk("RPC:       %s cred=%p\n", __func__, gss_cred);
123731be5bf1STrond Myklebust 	kfree(gss_cred);
123831be5bf1STrond Myklebust }
12391da177e4SLinus Torvalds 
124031be5bf1STrond Myklebust static void
124131be5bf1STrond Myklebust gss_free_cred_callback(struct rcu_head *head)
124231be5bf1STrond Myklebust {
124331be5bf1STrond Myklebust 	struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
124431be5bf1STrond Myklebust 	gss_free_cred(gss_cred);
124531be5bf1STrond Myklebust }
12461da177e4SLinus Torvalds 
124731be5bf1STrond Myklebust static void
12486dcd3926SJeff Layton gss_destroy_nullcred(struct rpc_cred *cred)
124931be5bf1STrond Myklebust {
12505d28dc82STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
12510285ed1fSTrond Myklebust 	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
12525d28dc82STrond Myklebust 	struct gss_cl_ctx *ctx = gss_cred->gc_ctx;
12535d28dc82STrond Myklebust 
1254a9b3cd7fSStephen Hemminger 	RCU_INIT_POINTER(gss_cred->gc_ctx, NULL);
125531be5bf1STrond Myklebust 	call_rcu(&cred->cr_rcu, gss_free_cred_callback);
12565d28dc82STrond Myklebust 	if (ctx)
12575d28dc82STrond Myklebust 		gss_put_ctx(ctx);
12580285ed1fSTrond Myklebust 	kref_put(&gss_auth->kref, gss_free_callback);
12591da177e4SLinus Torvalds }
12601da177e4SLinus Torvalds 
12616dcd3926SJeff Layton static void
12626dcd3926SJeff Layton gss_destroy_cred(struct rpc_cred *cred)
12636dcd3926SJeff Layton {
12646dcd3926SJeff Layton 
12656dcd3926SJeff Layton 	if (gss_destroying_context(cred))
12666dcd3926SJeff Layton 		return;
12676dcd3926SJeff Layton 	gss_destroy_nullcred(cred);
12686dcd3926SJeff Layton }
12696dcd3926SJeff Layton 
12701da177e4SLinus Torvalds /*
12711da177e4SLinus Torvalds  * Lookup RPCSEC_GSS cred for the current process
12721da177e4SLinus Torvalds  */
12731da177e4SLinus Torvalds static struct rpc_cred *
12748a317760STrond Myklebust gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
12751da177e4SLinus Torvalds {
12768a317760STrond Myklebust 	return rpcauth_lookup_credcache(auth, acred, flags);
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds static struct rpc_cred *
12808a317760STrond Myklebust gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
12811da177e4SLinus Torvalds {
12821da177e4SLinus Torvalds 	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
12831da177e4SLinus Torvalds 	struct gss_cred	*cred = NULL;
12841da177e4SLinus Torvalds 	int err = -ENOMEM;
12851da177e4SLinus Torvalds 
1286632f0d05SChuck Lever 	dprintk("RPC:       %s for uid %d, flavor %d\n",
1287cdba321eSEric W. Biederman 		__func__, from_kuid(&init_user_ns, acred->uid),
1288cdba321eSEric W. Biederman 		auth->au_flavor);
12891da177e4SLinus Torvalds 
12900f38b873STrond Myklebust 	if (!(cred = kzalloc(sizeof(*cred), GFP_NOFS)))
12911da177e4SLinus Torvalds 		goto out_err;
12921da177e4SLinus Torvalds 
12935fe4755eSTrond Myklebust 	rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
12941da177e4SLinus Torvalds 	/*
12951da177e4SLinus Torvalds 	 * Note: in order to force a call to call_refresh(), we deliberately
12961da177e4SLinus Torvalds 	 * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
12971da177e4SLinus Torvalds 	 */
1298fc432dd9STrond Myklebust 	cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
12991da177e4SLinus Torvalds 	cred->gc_service = gss_auth->service;
130068c97153STrond Myklebust 	cred->gc_principal = NULL;
130168c97153STrond Myklebust 	if (acred->machine_cred)
130268c97153STrond Myklebust 		cred->gc_principal = acred->principal;
13030285ed1fSTrond Myklebust 	kref_get(&gss_auth->kref);
13041da177e4SLinus Torvalds 	return &cred->gc_base;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds out_err:
1307632f0d05SChuck Lever 	dprintk("RPC:       %s failed with error %d\n", __func__, err);
13081da177e4SLinus Torvalds 	return ERR_PTR(err);
13091da177e4SLinus Torvalds }
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds static int
1312fba3bad4STrond Myklebust gss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
1313fba3bad4STrond Myklebust {
1314fba3bad4STrond Myklebust 	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
1315fba3bad4STrond Myklebust 	struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
1316fba3bad4STrond Myklebust 	int err;
1317fba3bad4STrond Myklebust 
1318fba3bad4STrond Myklebust 	do {
1319fba3bad4STrond Myklebust 		err = gss_create_upcall(gss_auth, gss_cred);
1320fba3bad4STrond Myklebust 	} while (err == -EAGAIN);
1321fba3bad4STrond Myklebust 	return err;
1322fba3bad4STrond Myklebust }
1323fba3bad4STrond Myklebust 
13244de6caa2SAndy Adamson /*
13254de6caa2SAndy Adamson  * Returns -EACCES if GSS context is NULL or will expire within the
13264de6caa2SAndy Adamson  * timeout (miliseconds)
13274de6caa2SAndy Adamson  */
13284de6caa2SAndy Adamson static int
13294de6caa2SAndy Adamson gss_key_timeout(struct rpc_cred *rc)
13304de6caa2SAndy Adamson {
13314de6caa2SAndy Adamson 	struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
13324de6caa2SAndy Adamson 	unsigned long now = jiffies;
13334de6caa2SAndy Adamson 	unsigned long expire;
13344de6caa2SAndy Adamson 
13354de6caa2SAndy Adamson 	if (gss_cred->gc_ctx == NULL)
13364de6caa2SAndy Adamson 		return -EACCES;
13374de6caa2SAndy Adamson 
13384de6caa2SAndy Adamson 	expire = gss_cred->gc_ctx->gc_expiry - (gss_key_expire_timeo * HZ);
13394de6caa2SAndy Adamson 
13404de6caa2SAndy Adamson 	if (time_after(now, expire))
13414de6caa2SAndy Adamson 		return -EACCES;
13424de6caa2SAndy Adamson 	return 0;
13434de6caa2SAndy Adamson }
13444de6caa2SAndy Adamson 
1345fba3bad4STrond Myklebust static int
13468a317760STrond Myklebust gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
13471da177e4SLinus Torvalds {
13481da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
13494de6caa2SAndy Adamson 	int ret;
13501da177e4SLinus Torvalds 
1351cd019f75STrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
13528a317760STrond Myklebust 		goto out;
13531da177e4SLinus Torvalds 	/* Don't match with creds that have expired. */
1354cd019f75STrond Myklebust 	if (time_after(jiffies, gss_cred->gc_ctx->gc_expiry))
1355cd019f75STrond Myklebust 		return 0;
1356cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags))
13571da177e4SLinus Torvalds 		return 0;
13588a317760STrond Myklebust out:
135968c97153STrond Myklebust 	if (acred->principal != NULL) {
136068c97153STrond Myklebust 		if (gss_cred->gc_principal == NULL)
136168c97153STrond Myklebust 			return 0;
13624de6caa2SAndy Adamson 		ret = strcmp(acred->principal, gss_cred->gc_principal) == 0;
13634de6caa2SAndy Adamson 		goto check_expire;
136468c97153STrond Myklebust 	}
136568c97153STrond Myklebust 	if (gss_cred->gc_principal != NULL)
13667c67db3aSTrond Myklebust 		return 0;
13674de6caa2SAndy Adamson 	ret = uid_eq(rc->cr_uid, acred->uid);
13684de6caa2SAndy Adamson 
13694de6caa2SAndy Adamson check_expire:
13704de6caa2SAndy Adamson 	if (ret == 0)
13714de6caa2SAndy Adamson 		return ret;
13724de6caa2SAndy Adamson 
13734de6caa2SAndy Adamson 	/* Notify acred users of GSS context expiration timeout */
13744de6caa2SAndy Adamson 	if (test_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags) &&
13754de6caa2SAndy Adamson 	    (gss_key_timeout(rc) != 0)) {
13764de6caa2SAndy Adamson 		/* test will now be done from generic cred */
13774de6caa2SAndy Adamson 		test_and_clear_bit(RPC_CRED_NOTIFY_TIMEOUT, &acred->ac_flags);
13784de6caa2SAndy Adamson 		/* tell NFS layer that key will expire soon */
13794de6caa2SAndy Adamson 		set_bit(RPC_CRED_KEY_EXPIRE_SOON, &acred->ac_flags);
13804de6caa2SAndy Adamson 	}
13814de6caa2SAndy Adamson 	return ret;
13821da177e4SLinus Torvalds }
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds /*
13851da177e4SLinus Torvalds * Marshal credentials.
13861da177e4SLinus Torvalds * Maybe we should keep a cached credential for performance reasons.
13871da177e4SLinus Torvalds */
1388d8ed029dSAlexey Dobriyan static __be32 *
1389d8ed029dSAlexey Dobriyan gss_marshal(struct rpc_task *task, __be32 *p)
13901da177e4SLinus Torvalds {
1391a17c2153STrond Myklebust 	struct rpc_rqst *req = task->tk_rqstp;
1392a17c2153STrond Myklebust 	struct rpc_cred *cred = req->rq_cred;
13931da177e4SLinus Torvalds 	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
13941da177e4SLinus Torvalds 						 gc_base);
13951da177e4SLinus Torvalds 	struct gss_cl_ctx	*ctx = gss_cred_get_ctx(cred);
1396d8ed029dSAlexey Dobriyan 	__be32		*cred_len;
13971da177e4SLinus Torvalds 	u32             maj_stat = 0;
13981da177e4SLinus Torvalds 	struct xdr_netobj mic;
13991da177e4SLinus Torvalds 	struct kvec	iov;
14001da177e4SLinus Torvalds 	struct xdr_buf	verf_buf;
14011da177e4SLinus Torvalds 
1402632f0d05SChuck Lever 	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 	*p++ = htonl(RPC_AUTH_GSS);
14051da177e4SLinus Torvalds 	cred_len = p++;
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	spin_lock(&ctx->gc_seq_lock);
14081da177e4SLinus Torvalds 	req->rq_seqno = ctx->gc_seq++;
14091da177e4SLinus Torvalds 	spin_unlock(&ctx->gc_seq_lock);
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds 	*p++ = htonl((u32) RPC_GSS_VERSION);
14121da177e4SLinus Torvalds 	*p++ = htonl((u32) ctx->gc_proc);
14131da177e4SLinus Torvalds 	*p++ = htonl((u32) req->rq_seqno);
14141da177e4SLinus Torvalds 	*p++ = htonl((u32) gss_cred->gc_service);
14151da177e4SLinus Torvalds 	p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
14161da177e4SLinus Torvalds 	*cred_len = htonl((p - (cred_len + 1)) << 2);
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 	/* We compute the checksum for the verifier over the xdr-encoded bytes
14191da177e4SLinus Torvalds 	 * starting with the xid and ending at the end of the credential: */
1420a4f0835cSTrond Myklebust 	iov.iov_base = xprt_skip_transport_header(req->rq_xprt,
1421808012fbSChuck Lever 					req->rq_snd_buf.head[0].iov_base);
14221da177e4SLinus Torvalds 	iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
14231da177e4SLinus Torvalds 	xdr_buf_from_iov(&iov, &verf_buf);
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	/* set verifier flavor*/
14261da177e4SLinus Torvalds 	*p++ = htonl(RPC_AUTH_GSS);
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	mic.data = (u8 *)(p + 1);
142900fd6e14SJ. Bruce Fields 	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
14301da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
1431fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
14321da177e4SLinus Torvalds 	} else if (maj_stat != 0) {
14331da177e4SLinus Torvalds 		printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
14341da177e4SLinus Torvalds 		goto out_put_ctx;
14351da177e4SLinus Torvalds 	}
14361da177e4SLinus Torvalds 	p = xdr_encode_opaque(p, NULL, mic.len);
14371da177e4SLinus Torvalds 	gss_put_ctx(ctx);
14381da177e4SLinus Torvalds 	return p;
14391da177e4SLinus Torvalds out_put_ctx:
14401da177e4SLinus Torvalds 	gss_put_ctx(ctx);
14411da177e4SLinus Torvalds 	return NULL;
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
1444cd019f75STrond Myklebust static int gss_renew_cred(struct rpc_task *task)
1445cd019f75STrond Myklebust {
1446a17c2153STrond Myklebust 	struct rpc_cred *oldcred = task->tk_rqstp->rq_cred;
1447cd019f75STrond Myklebust 	struct gss_cred *gss_cred = container_of(oldcred,
1448cd019f75STrond Myklebust 						 struct gss_cred,
1449cd019f75STrond Myklebust 						 gc_base);
1450cd019f75STrond Myklebust 	struct rpc_auth *auth = oldcred->cr_auth;
1451cd019f75STrond Myklebust 	struct auth_cred acred = {
1452cd019f75STrond Myklebust 		.uid = oldcred->cr_uid,
145368c97153STrond Myklebust 		.principal = gss_cred->gc_principal,
145468c97153STrond Myklebust 		.machine_cred = (gss_cred->gc_principal != NULL ? 1 : 0),
1455cd019f75STrond Myklebust 	};
1456cd019f75STrond Myklebust 	struct rpc_cred *new;
1457cd019f75STrond Myklebust 
1458cd019f75STrond Myklebust 	new = gss_lookup_cred(auth, &acred, RPCAUTH_LOOKUP_NEW);
1459cd019f75STrond Myklebust 	if (IS_ERR(new))
1460cd019f75STrond Myklebust 		return PTR_ERR(new);
1461a17c2153STrond Myklebust 	task->tk_rqstp->rq_cred = new;
1462cd019f75STrond Myklebust 	put_rpccred(oldcred);
1463cd019f75STrond Myklebust 	return 0;
1464cd019f75STrond Myklebust }
1465cd019f75STrond Myklebust 
1466126e216aSTrond Myklebust static int gss_cred_is_negative_entry(struct rpc_cred *cred)
1467126e216aSTrond Myklebust {
1468126e216aSTrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEGATIVE, &cred->cr_flags)) {
1469126e216aSTrond Myklebust 		unsigned long now = jiffies;
1470126e216aSTrond Myklebust 		unsigned long begin, expire;
1471126e216aSTrond Myklebust 		struct gss_cred *gss_cred;
1472126e216aSTrond Myklebust 
1473126e216aSTrond Myklebust 		gss_cred = container_of(cred, struct gss_cred, gc_base);
1474126e216aSTrond Myklebust 		begin = gss_cred->gc_upcall_timestamp;
1475126e216aSTrond Myklebust 		expire = begin + gss_expired_cred_retry_delay * HZ;
1476126e216aSTrond Myklebust 
1477126e216aSTrond Myklebust 		if (time_in_range_open(now, begin, expire))
1478126e216aSTrond Myklebust 			return 1;
1479126e216aSTrond Myklebust 	}
1480126e216aSTrond Myklebust 	return 0;
1481126e216aSTrond Myklebust }
1482126e216aSTrond Myklebust 
14831da177e4SLinus Torvalds /*
14841da177e4SLinus Torvalds * Refresh credentials. XXX - finish
14851da177e4SLinus Torvalds */
14861da177e4SLinus Torvalds static int
14871da177e4SLinus Torvalds gss_refresh(struct rpc_task *task)
14881da177e4SLinus Torvalds {
1489a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
1490cd019f75STrond Myklebust 	int ret = 0;
14911da177e4SLinus Torvalds 
1492126e216aSTrond Myklebust 	if (gss_cred_is_negative_entry(cred))
1493126e216aSTrond Myklebust 		return -EKEYEXPIRED;
1494126e216aSTrond Myklebust 
1495cd019f75STrond Myklebust 	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
1496cd019f75STrond Myklebust 			!test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags)) {
1497cd019f75STrond Myklebust 		ret = gss_renew_cred(task);
1498cd019f75STrond Myklebust 		if (ret < 0)
1499cd019f75STrond Myklebust 			goto out;
1500a17c2153STrond Myklebust 		cred = task->tk_rqstp->rq_cred;
1501cd019f75STrond Myklebust 	}
1502cd019f75STrond Myklebust 
1503cd019f75STrond Myklebust 	if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
1504cd019f75STrond Myklebust 		ret = gss_refresh_upcall(task);
1505cd019f75STrond Myklebust out:
1506cd019f75STrond Myklebust 	return ret;
15071da177e4SLinus Torvalds }
15081da177e4SLinus Torvalds 
15090df7fb74STrond Myklebust /* Dummy refresh routine: used only when destroying the context */
15100df7fb74STrond Myklebust static int
15110df7fb74STrond Myklebust gss_refresh_null(struct rpc_task *task)
15120df7fb74STrond Myklebust {
1513c297c8b9SAndy Adamson 	return 0;
15140df7fb74STrond Myklebust }
15150df7fb74STrond Myklebust 
1516d8ed029dSAlexey Dobriyan static __be32 *
1517d8ed029dSAlexey Dobriyan gss_validate(struct rpc_task *task, __be32 *p)
15181da177e4SLinus Torvalds {
1519a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
15201da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1521d8ed029dSAlexey Dobriyan 	__be32		seq;
15221da177e4SLinus Torvalds 	struct kvec	iov;
15231da177e4SLinus Torvalds 	struct xdr_buf	verf_buf;
15241da177e4SLinus Torvalds 	struct xdr_netobj mic;
15251da177e4SLinus Torvalds 	u32		flav,len;
15261da177e4SLinus Torvalds 	u32		maj_stat;
152735fa5f7bSAndy Adamson 	__be32		*ret = ERR_PTR(-EIO);
15281da177e4SLinus Torvalds 
1529632f0d05SChuck Lever 	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	flav = ntohl(*p++);
15321da177e4SLinus Torvalds 	if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
15331da177e4SLinus Torvalds 		goto out_bad;
15341da177e4SLinus Torvalds 	if (flav != RPC_AUTH_GSS)
15351da177e4SLinus Torvalds 		goto out_bad;
15361da177e4SLinus Torvalds 	seq = htonl(task->tk_rqstp->rq_seqno);
15371da177e4SLinus Torvalds 	iov.iov_base = &seq;
15381da177e4SLinus Torvalds 	iov.iov_len = sizeof(seq);
15391da177e4SLinus Torvalds 	xdr_buf_from_iov(&iov, &verf_buf);
15401da177e4SLinus Torvalds 	mic.data = (u8 *)p;
15411da177e4SLinus Torvalds 	mic.len = len;
15421da177e4SLinus Torvalds 
154335fa5f7bSAndy Adamson 	ret = ERR_PTR(-EACCES);
154400fd6e14SJ. Bruce Fields 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
15451da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1546fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
15470df7fb74STrond Myklebust 	if (maj_stat) {
1548632f0d05SChuck Lever 		dprintk("RPC: %5u %s: gss_verify_mic returned error 0x%08x\n",
1549632f0d05SChuck Lever 			task->tk_pid, __func__, maj_stat);
15501da177e4SLinus Torvalds 		goto out_bad;
15510df7fb74STrond Myklebust 	}
155224b2605bSJ. Bruce Fields 	/* We leave it to unwrap to calculate au_rslack. For now we just
155324b2605bSJ. Bruce Fields 	 * calculate the length of the verifier: */
15541be27f36STrond Myklebust 	cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
15551da177e4SLinus Torvalds 	gss_put_ctx(ctx);
1556632f0d05SChuck Lever 	dprintk("RPC: %5u %s: gss_verify_mic succeeded.\n",
1557632f0d05SChuck Lever 			task->tk_pid, __func__);
15581da177e4SLinus Torvalds 	return p + XDR_QUADLEN(len);
15591da177e4SLinus Torvalds out_bad:
15601da177e4SLinus Torvalds 	gss_put_ctx(ctx);
156135fa5f7bSAndy Adamson 	dprintk("RPC: %5u %s failed ret %ld.\n", task->tk_pid, __func__,
156235fa5f7bSAndy Adamson 		PTR_ERR(ret));
156335fa5f7bSAndy Adamson 	return ret;
15641da177e4SLinus Torvalds }
15651da177e4SLinus Torvalds 
15669f06c719SChuck Lever static void gss_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
15679f06c719SChuck Lever 				__be32 *p, void *obj)
15689f06c719SChuck Lever {
15699f06c719SChuck Lever 	struct xdr_stream xdr;
15709f06c719SChuck Lever 
15719f06c719SChuck Lever 	xdr_init_encode(&xdr, &rqstp->rq_snd_buf, p);
15729f06c719SChuck Lever 	encode(rqstp, &xdr, obj);
15739f06c719SChuck Lever }
15749f06c719SChuck Lever 
15751da177e4SLinus Torvalds static inline int
15761da177e4SLinus Torvalds gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
15779f06c719SChuck Lever 		   kxdreproc_t encode, struct rpc_rqst *rqstp,
15789f06c719SChuck Lever 		   __be32 *p, void *obj)
15791da177e4SLinus Torvalds {
15801da177e4SLinus Torvalds 	struct xdr_buf	*snd_buf = &rqstp->rq_snd_buf;
15811da177e4SLinus Torvalds 	struct xdr_buf	integ_buf;
1582d8ed029dSAlexey Dobriyan 	__be32          *integ_len = NULL;
15831da177e4SLinus Torvalds 	struct xdr_netobj mic;
1584d8ed029dSAlexey Dobriyan 	u32		offset;
1585d8ed029dSAlexey Dobriyan 	__be32		*q;
15861da177e4SLinus Torvalds 	struct kvec	*iov;
15871da177e4SLinus Torvalds 	u32             maj_stat = 0;
15881da177e4SLinus Torvalds 	int		status = -EIO;
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds 	integ_len = p++;
15911da177e4SLinus Torvalds 	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
15921da177e4SLinus Torvalds 	*p++ = htonl(rqstp->rq_seqno);
15931da177e4SLinus Torvalds 
15949f06c719SChuck Lever 	gss_wrap_req_encode(encode, rqstp, p, obj);
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 	if (xdr_buf_subsegment(snd_buf, &integ_buf,
15971da177e4SLinus Torvalds 				offset, snd_buf->len - offset))
15981da177e4SLinus Torvalds 		return status;
15991da177e4SLinus Torvalds 	*integ_len = htonl(integ_buf.len);
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	/* guess whether we're in the head or the tail: */
16021da177e4SLinus Torvalds 	if (snd_buf->page_len || snd_buf->tail[0].iov_len)
16031da177e4SLinus Torvalds 		iov = snd_buf->tail;
16041da177e4SLinus Torvalds 	else
16051da177e4SLinus Torvalds 		iov = snd_buf->head;
16061da177e4SLinus Torvalds 	p = iov->iov_base + iov->iov_len;
16071da177e4SLinus Torvalds 	mic.data = (u8 *)(p + 1);
16081da177e4SLinus Torvalds 
160900fd6e14SJ. Bruce Fields 	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
16101da177e4SLinus Torvalds 	status = -EIO; /* XXX? */
16111da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1612fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
16131da177e4SLinus Torvalds 	else if (maj_stat)
16141da177e4SLinus Torvalds 		return status;
16151da177e4SLinus Torvalds 	q = xdr_encode_opaque(p, NULL, mic.len);
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds 	offset = (u8 *)q - (u8 *)p;
16181da177e4SLinus Torvalds 	iov->iov_len += offset;
16191da177e4SLinus Torvalds 	snd_buf->len += offset;
16201da177e4SLinus Torvalds 	return 0;
16211da177e4SLinus Torvalds }
16221da177e4SLinus Torvalds 
16232d2da60cSJ. Bruce Fields static void
16242d2da60cSJ. Bruce Fields priv_release_snd_buf(struct rpc_rqst *rqstp)
16252d2da60cSJ. Bruce Fields {
16262d2da60cSJ. Bruce Fields 	int i;
16272d2da60cSJ. Bruce Fields 
16282d2da60cSJ. Bruce Fields 	for (i=0; i < rqstp->rq_enc_pages_num; i++)
16292d2da60cSJ. Bruce Fields 		__free_page(rqstp->rq_enc_pages[i]);
16302d2da60cSJ. Bruce Fields 	kfree(rqstp->rq_enc_pages);
16312d2da60cSJ. Bruce Fields }
16322d2da60cSJ. Bruce Fields 
16332d2da60cSJ. Bruce Fields static int
16342d2da60cSJ. Bruce Fields alloc_enc_pages(struct rpc_rqst *rqstp)
16352d2da60cSJ. Bruce Fields {
16362d2da60cSJ. Bruce Fields 	struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
16372d2da60cSJ. Bruce Fields 	int first, last, i;
16382d2da60cSJ. Bruce Fields 
16392d2da60cSJ. Bruce Fields 	if (snd_buf->page_len == 0) {
16402d2da60cSJ. Bruce Fields 		rqstp->rq_enc_pages_num = 0;
16412d2da60cSJ. Bruce Fields 		return 0;
16422d2da60cSJ. Bruce Fields 	}
16432d2da60cSJ. Bruce Fields 
16442d2da60cSJ. Bruce Fields 	first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
16452d2da60cSJ. Bruce Fields 	last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_CACHE_SHIFT;
16462d2da60cSJ. Bruce Fields 	rqstp->rq_enc_pages_num = last - first + 1 + 1;
16472d2da60cSJ. Bruce Fields 	rqstp->rq_enc_pages
16482d2da60cSJ. Bruce Fields 		= kmalloc(rqstp->rq_enc_pages_num * sizeof(struct page *),
16492d2da60cSJ. Bruce Fields 				GFP_NOFS);
16502d2da60cSJ. Bruce Fields 	if (!rqstp->rq_enc_pages)
16512d2da60cSJ. Bruce Fields 		goto out;
16522d2da60cSJ. Bruce Fields 	for (i=0; i < rqstp->rq_enc_pages_num; i++) {
16532d2da60cSJ. Bruce Fields 		rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
16542d2da60cSJ. Bruce Fields 		if (rqstp->rq_enc_pages[i] == NULL)
16552d2da60cSJ. Bruce Fields 			goto out_free;
16562d2da60cSJ. Bruce Fields 	}
16572d2da60cSJ. Bruce Fields 	rqstp->rq_release_snd_buf = priv_release_snd_buf;
16582d2da60cSJ. Bruce Fields 	return 0;
16592d2da60cSJ. Bruce Fields out_free:
1660cdead7cfSTrond Myklebust 	rqstp->rq_enc_pages_num = i;
1661cdead7cfSTrond Myklebust 	priv_release_snd_buf(rqstp);
16622d2da60cSJ. Bruce Fields out:
16632d2da60cSJ. Bruce Fields 	return -EAGAIN;
16642d2da60cSJ. Bruce Fields }
16652d2da60cSJ. Bruce Fields 
16662d2da60cSJ. Bruce Fields static inline int
16672d2da60cSJ. Bruce Fields gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
16689f06c719SChuck Lever 		  kxdreproc_t encode, struct rpc_rqst *rqstp,
16699f06c719SChuck Lever 		  __be32 *p, void *obj)
16702d2da60cSJ. Bruce Fields {
16712d2da60cSJ. Bruce Fields 	struct xdr_buf	*snd_buf = &rqstp->rq_snd_buf;
16722d2da60cSJ. Bruce Fields 	u32		offset;
16732d2da60cSJ. Bruce Fields 	u32             maj_stat;
16742d2da60cSJ. Bruce Fields 	int		status;
1675d8ed029dSAlexey Dobriyan 	__be32		*opaque_len;
16762d2da60cSJ. Bruce Fields 	struct page	**inpages;
16772d2da60cSJ. Bruce Fields 	int		first;
16782d2da60cSJ. Bruce Fields 	int		pad;
16792d2da60cSJ. Bruce Fields 	struct kvec	*iov;
16802d2da60cSJ. Bruce Fields 	char		*tmp;
16812d2da60cSJ. Bruce Fields 
16822d2da60cSJ. Bruce Fields 	opaque_len = p++;
16832d2da60cSJ. Bruce Fields 	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
16842d2da60cSJ. Bruce Fields 	*p++ = htonl(rqstp->rq_seqno);
16852d2da60cSJ. Bruce Fields 
16869f06c719SChuck Lever 	gss_wrap_req_encode(encode, rqstp, p, obj);
16872d2da60cSJ. Bruce Fields 
16882d2da60cSJ. Bruce Fields 	status = alloc_enc_pages(rqstp);
16892d2da60cSJ. Bruce Fields 	if (status)
16902d2da60cSJ. Bruce Fields 		return status;
16912d2da60cSJ. Bruce Fields 	first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
16922d2da60cSJ. Bruce Fields 	inpages = snd_buf->pages + first;
16932d2da60cSJ. Bruce Fields 	snd_buf->pages = rqstp->rq_enc_pages;
16942d2da60cSJ. Bruce Fields 	snd_buf->page_base -= first << PAGE_CACHE_SHIFT;
16957561042fSKevin Coffman 	/*
16967561042fSKevin Coffman 	 * Give the tail its own page, in case we need extra space in the
16977561042fSKevin Coffman 	 * head when wrapping:
16987561042fSKevin Coffman 	 *
16997561042fSKevin Coffman 	 * call_allocate() allocates twice the slack space required
17007561042fSKevin Coffman 	 * by the authentication flavor to rq_callsize.
17017561042fSKevin Coffman 	 * For GSS, slack is GSS_CRED_SLACK.
17027561042fSKevin Coffman 	 */
17032d2da60cSJ. Bruce Fields 	if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
17042d2da60cSJ. Bruce Fields 		tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
17052d2da60cSJ. Bruce Fields 		memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
17062d2da60cSJ. Bruce Fields 		snd_buf->tail[0].iov_base = tmp;
17072d2da60cSJ. Bruce Fields 	}
170800fd6e14SJ. Bruce Fields 	maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
17097561042fSKevin Coffman 	/* slack space should prevent this ever happening: */
17102d2da60cSJ. Bruce Fields 	BUG_ON(snd_buf->len > snd_buf->buflen);
17112d2da60cSJ. Bruce Fields 	status = -EIO;
17122d2da60cSJ. Bruce Fields 	/* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
17132d2da60cSJ. Bruce Fields 	 * done anyway, so it's safe to put the request on the wire: */
17142d2da60cSJ. Bruce Fields 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1715fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
17162d2da60cSJ. Bruce Fields 	else if (maj_stat)
17172d2da60cSJ. Bruce Fields 		return status;
17182d2da60cSJ. Bruce Fields 
17192d2da60cSJ. Bruce Fields 	*opaque_len = htonl(snd_buf->len - offset);
17202d2da60cSJ. Bruce Fields 	/* guess whether we're in the head or the tail: */
17212d2da60cSJ. Bruce Fields 	if (snd_buf->page_len || snd_buf->tail[0].iov_len)
17222d2da60cSJ. Bruce Fields 		iov = snd_buf->tail;
17232d2da60cSJ. Bruce Fields 	else
17242d2da60cSJ. Bruce Fields 		iov = snd_buf->head;
17252d2da60cSJ. Bruce Fields 	p = iov->iov_base + iov->iov_len;
17262d2da60cSJ. Bruce Fields 	pad = 3 - ((snd_buf->len - offset - 1) & 3);
17272d2da60cSJ. Bruce Fields 	memset(p, 0, pad);
17282d2da60cSJ. Bruce Fields 	iov->iov_len += pad;
17292d2da60cSJ. Bruce Fields 	snd_buf->len += pad;
17302d2da60cSJ. Bruce Fields 
17312d2da60cSJ. Bruce Fields 	return 0;
17322d2da60cSJ. Bruce Fields }
17332d2da60cSJ. Bruce Fields 
17341da177e4SLinus Torvalds static int
17351da177e4SLinus Torvalds gss_wrap_req(struct rpc_task *task,
17369f06c719SChuck Lever 	     kxdreproc_t encode, void *rqstp, __be32 *p, void *obj)
17371da177e4SLinus Torvalds {
1738a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
17391da177e4SLinus Torvalds 	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
17401da177e4SLinus Torvalds 			gc_base);
17411da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
17421da177e4SLinus Torvalds 	int             status = -EIO;
17431da177e4SLinus Torvalds 
1744632f0d05SChuck Lever 	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
17451da177e4SLinus Torvalds 	if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
17461da177e4SLinus Torvalds 		/* The spec seems a little ambiguous here, but I think that not
17471da177e4SLinus Torvalds 		 * wrapping context destruction requests makes the most sense.
17481da177e4SLinus Torvalds 		 */
17499f06c719SChuck Lever 		gss_wrap_req_encode(encode, rqstp, p, obj);
17509f06c719SChuck Lever 		status = 0;
17511da177e4SLinus Torvalds 		goto out;
17521da177e4SLinus Torvalds 	}
17531da177e4SLinus Torvalds 	switch (gss_cred->gc_service) {
17541da177e4SLinus Torvalds 	case RPC_GSS_SVC_NONE:
17559f06c719SChuck Lever 		gss_wrap_req_encode(encode, rqstp, p, obj);
17569f06c719SChuck Lever 		status = 0;
17571da177e4SLinus Torvalds 		break;
17581da177e4SLinus Torvalds 	case RPC_GSS_SVC_INTEGRITY:
175989f0e4feSJoe Perches 		status = gss_wrap_req_integ(cred, ctx, encode, rqstp, p, obj);
17601da177e4SLinus Torvalds 		break;
17611da177e4SLinus Torvalds 	case RPC_GSS_SVC_PRIVACY:
176289f0e4feSJoe Perches 		status = gss_wrap_req_priv(cred, ctx, encode, rqstp, p, obj);
17631da177e4SLinus Torvalds 		break;
17641da177e4SLinus Torvalds 	}
17651da177e4SLinus Torvalds out:
17661da177e4SLinus Torvalds 	gss_put_ctx(ctx);
1767632f0d05SChuck Lever 	dprintk("RPC: %5u %s returning %d\n", task->tk_pid, __func__, status);
17681da177e4SLinus Torvalds 	return status;
17691da177e4SLinus Torvalds }
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds static inline int
17721da177e4SLinus Torvalds gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1773d8ed029dSAlexey Dobriyan 		struct rpc_rqst *rqstp, __be32 **p)
17741da177e4SLinus Torvalds {
17751da177e4SLinus Torvalds 	struct xdr_buf	*rcv_buf = &rqstp->rq_rcv_buf;
17761da177e4SLinus Torvalds 	struct xdr_buf integ_buf;
17771da177e4SLinus Torvalds 	struct xdr_netobj mic;
17781da177e4SLinus Torvalds 	u32 data_offset, mic_offset;
17791da177e4SLinus Torvalds 	u32 integ_len;
17801da177e4SLinus Torvalds 	u32 maj_stat;
17811da177e4SLinus Torvalds 	int status = -EIO;
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	integ_len = ntohl(*(*p)++);
17841da177e4SLinus Torvalds 	if (integ_len & 3)
17851da177e4SLinus Torvalds 		return status;
17861da177e4SLinus Torvalds 	data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
17871da177e4SLinus Torvalds 	mic_offset = integ_len + data_offset;
17881da177e4SLinus Torvalds 	if (mic_offset > rcv_buf->len)
17891da177e4SLinus Torvalds 		return status;
17901da177e4SLinus Torvalds 	if (ntohl(*(*p)++) != rqstp->rq_seqno)
17911da177e4SLinus Torvalds 		return status;
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 	if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
17941da177e4SLinus Torvalds 				mic_offset - data_offset))
17951da177e4SLinus Torvalds 		return status;
17961da177e4SLinus Torvalds 
17971da177e4SLinus Torvalds 	if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
17981da177e4SLinus Torvalds 		return status;
17991da177e4SLinus Torvalds 
180000fd6e14SJ. Bruce Fields 	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
18011da177e4SLinus Torvalds 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1802fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
18031da177e4SLinus Torvalds 	if (maj_stat != GSS_S_COMPLETE)
18041da177e4SLinus Torvalds 		return status;
18051da177e4SLinus Torvalds 	return 0;
18061da177e4SLinus Torvalds }
18071da177e4SLinus Torvalds 
18082d2da60cSJ. Bruce Fields static inline int
18092d2da60cSJ. Bruce Fields gss_unwrap_resp_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1810d8ed029dSAlexey Dobriyan 		struct rpc_rqst *rqstp, __be32 **p)
18112d2da60cSJ. Bruce Fields {
18122d2da60cSJ. Bruce Fields 	struct xdr_buf  *rcv_buf = &rqstp->rq_rcv_buf;
18132d2da60cSJ. Bruce Fields 	u32 offset;
18142d2da60cSJ. Bruce Fields 	u32 opaque_len;
18152d2da60cSJ. Bruce Fields 	u32 maj_stat;
18162d2da60cSJ. Bruce Fields 	int status = -EIO;
18172d2da60cSJ. Bruce Fields 
18182d2da60cSJ. Bruce Fields 	opaque_len = ntohl(*(*p)++);
18192d2da60cSJ. Bruce Fields 	offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
18202d2da60cSJ. Bruce Fields 	if (offset + opaque_len > rcv_buf->len)
18212d2da60cSJ. Bruce Fields 		return status;
18222d2da60cSJ. Bruce Fields 	/* remove padding: */
18232d2da60cSJ. Bruce Fields 	rcv_buf->len = offset + opaque_len;
18242d2da60cSJ. Bruce Fields 
182500fd6e14SJ. Bruce Fields 	maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
18262d2da60cSJ. Bruce Fields 	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1827fc432dd9STrond Myklebust 		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
18282d2da60cSJ. Bruce Fields 	if (maj_stat != GSS_S_COMPLETE)
18292d2da60cSJ. Bruce Fields 		return status;
18302d2da60cSJ. Bruce Fields 	if (ntohl(*(*p)++) != rqstp->rq_seqno)
18312d2da60cSJ. Bruce Fields 		return status;
18322d2da60cSJ. Bruce Fields 
18332d2da60cSJ. Bruce Fields 	return 0;
18342d2da60cSJ. Bruce Fields }
18352d2da60cSJ. Bruce Fields 
1836bf269551SChuck Lever static int
1837bf269551SChuck Lever gss_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
1838bf269551SChuck Lever 		      __be32 *p, void *obj)
1839bf269551SChuck Lever {
1840bf269551SChuck Lever 	struct xdr_stream xdr;
1841bf269551SChuck Lever 
1842bf269551SChuck Lever 	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
1843bf269551SChuck Lever 	return decode(rqstp, &xdr, obj);
1844bf269551SChuck Lever }
18452d2da60cSJ. Bruce Fields 
18461da177e4SLinus Torvalds static int
18471da177e4SLinus Torvalds gss_unwrap_resp(struct rpc_task *task,
1848bf269551SChuck Lever 		kxdrdproc_t decode, void *rqstp, __be32 *p, void *obj)
18491da177e4SLinus Torvalds {
1850a17c2153STrond Myklebust 	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
18511da177e4SLinus Torvalds 	struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
18521da177e4SLinus Torvalds 			gc_base);
18531da177e4SLinus Torvalds 	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1854d8ed029dSAlexey Dobriyan 	__be32		*savedp = p;
18552d2da60cSJ. Bruce Fields 	struct kvec	*head = ((struct rpc_rqst *)rqstp)->rq_rcv_buf.head;
18562d2da60cSJ. Bruce Fields 	int		savedlen = head->iov_len;
18571da177e4SLinus Torvalds 	int             status = -EIO;
18581da177e4SLinus Torvalds 
18591da177e4SLinus Torvalds 	if (ctx->gc_proc != RPC_GSS_PROC_DATA)
18601da177e4SLinus Torvalds 		goto out_decode;
18611da177e4SLinus Torvalds 	switch (gss_cred->gc_service) {
18621da177e4SLinus Torvalds 	case RPC_GSS_SVC_NONE:
18631da177e4SLinus Torvalds 		break;
18641da177e4SLinus Torvalds 	case RPC_GSS_SVC_INTEGRITY:
18651da177e4SLinus Torvalds 		status = gss_unwrap_resp_integ(cred, ctx, rqstp, &p);
18661da177e4SLinus Torvalds 		if (status)
18671da177e4SLinus Torvalds 			goto out;
18681da177e4SLinus Torvalds 		break;
18691da177e4SLinus Torvalds 	case RPC_GSS_SVC_PRIVACY:
18702d2da60cSJ. Bruce Fields 		status = gss_unwrap_resp_priv(cred, ctx, rqstp, &p);
18712d2da60cSJ. Bruce Fields 		if (status)
18722d2da60cSJ. Bruce Fields 			goto out;
18731da177e4SLinus Torvalds 		break;
18741da177e4SLinus Torvalds 	}
187524b2605bSJ. Bruce Fields 	/* take into account extra slack for integrity and privacy cases: */
18761be27f36STrond Myklebust 	cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + (p - savedp)
18772d2da60cSJ. Bruce Fields 						+ (savedlen - head->iov_len);
18781da177e4SLinus Torvalds out_decode:
1879bf269551SChuck Lever 	status = gss_unwrap_req_decode(decode, rqstp, p, obj);
18801da177e4SLinus Torvalds out:
18811da177e4SLinus Torvalds 	gss_put_ctx(ctx);
1882632f0d05SChuck Lever 	dprintk("RPC: %5u %s returning %d\n",
1883632f0d05SChuck Lever 		task->tk_pid, __func__, status);
18841da177e4SLinus Torvalds 	return status;
18851da177e4SLinus Torvalds }
18861da177e4SLinus Torvalds 
1887f1c0a861STrond Myklebust static const struct rpc_authops authgss_ops = {
18881da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
18891da177e4SLinus Torvalds 	.au_flavor	= RPC_AUTH_GSS,
18901da177e4SLinus Torvalds 	.au_name	= "RPCSEC_GSS",
18911da177e4SLinus Torvalds 	.create		= gss_create,
18921da177e4SLinus Torvalds 	.destroy	= gss_destroy,
18931da177e4SLinus Torvalds 	.lookup_cred	= gss_lookup_cred,
189480df9d20SStanislav Kinsbursky 	.crcreate	= gss_create_cred,
18956a1a1e34SChuck Lever 	.list_pseudoflavors = gss_mech_list_pseudoflavors,
18969568c5e9SChuck Lever 	.info2flavor	= gss_mech_info2flavor,
1897a77c806fSChuck Lever 	.flavor2info	= gss_mech_flavor2info,
18981da177e4SLinus Torvalds };
18991da177e4SLinus Torvalds 
1900f1c0a861STrond Myklebust static const struct rpc_credops gss_credops = {
19011da177e4SLinus Torvalds 	.cr_name	= "AUTH_GSS",
19021da177e4SLinus Torvalds 	.crdestroy	= gss_destroy_cred,
1903fba3bad4STrond Myklebust 	.cr_init	= gss_cred_init,
19045c691044STrond Myklebust 	.crbind		= rpcauth_generic_bind_cred,
19051da177e4SLinus Torvalds 	.crmatch	= gss_match,
19061da177e4SLinus Torvalds 	.crmarshal	= gss_marshal,
19071da177e4SLinus Torvalds 	.crrefresh	= gss_refresh,
19081da177e4SLinus Torvalds 	.crvalidate	= gss_validate,
19091da177e4SLinus Torvalds 	.crwrap_req	= gss_wrap_req,
19101da177e4SLinus Torvalds 	.crunwrap_resp	= gss_unwrap_resp,
19114de6caa2SAndy Adamson 	.crkey_timeout	= gss_key_timeout,
19121da177e4SLinus Torvalds };
19131da177e4SLinus Torvalds 
19140df7fb74STrond Myklebust static const struct rpc_credops gss_nullops = {
19150df7fb74STrond Myklebust 	.cr_name	= "AUTH_GSS",
19166dcd3926SJeff Layton 	.crdestroy	= gss_destroy_nullcred,
19175c691044STrond Myklebust 	.crbind		= rpcauth_generic_bind_cred,
19180df7fb74STrond Myklebust 	.crmatch	= gss_match,
19190df7fb74STrond Myklebust 	.crmarshal	= gss_marshal,
19200df7fb74STrond Myklebust 	.crrefresh	= gss_refresh_null,
19210df7fb74STrond Myklebust 	.crvalidate	= gss_validate,
19220df7fb74STrond Myklebust 	.crwrap_req	= gss_wrap_req,
19230df7fb74STrond Myklebust 	.crunwrap_resp	= gss_unwrap_resp,
19240df7fb74STrond Myklebust };
19250df7fb74STrond Myklebust 
1926b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v0 = {
1927c1225158SPeng Tao 	.upcall		= rpc_pipe_generic_upcall,
19281da177e4SLinus Torvalds 	.downcall	= gss_pipe_downcall,
19291da177e4SLinus Torvalds 	.destroy_msg	= gss_pipe_destroy_msg,
193034769fc4S\"J. Bruce Fields\ 	.open_pipe	= gss_pipe_open_v0,
193134769fc4S\"J. Bruce Fields\ 	.release_pipe	= gss_pipe_release,
193234769fc4S\"J. Bruce Fields\ };
193334769fc4S\"J. Bruce Fields\ 
1934b693ba4aSTrond Myklebust static const struct rpc_pipe_ops gss_upcall_ops_v1 = {
1935c1225158SPeng Tao 	.upcall		= rpc_pipe_generic_upcall,
193634769fc4S\"J. Bruce Fields\ 	.downcall	= gss_pipe_downcall,
193734769fc4S\"J. Bruce Fields\ 	.destroy_msg	= gss_pipe_destroy_msg,
193834769fc4S\"J. Bruce Fields\ 	.open_pipe	= gss_pipe_open_v1,
19391da177e4SLinus Torvalds 	.release_pipe	= gss_pipe_release,
19401da177e4SLinus Torvalds };
19411da177e4SLinus Torvalds 
1942a1db410dSStanislav Kinsbursky static __net_init int rpcsec_gss_init_net(struct net *net)
1943a1db410dSStanislav Kinsbursky {
1944a1db410dSStanislav Kinsbursky 	return gss_svc_init_net(net);
1945a1db410dSStanislav Kinsbursky }
1946a1db410dSStanislav Kinsbursky 
1947a1db410dSStanislav Kinsbursky static __net_exit void rpcsec_gss_exit_net(struct net *net)
1948a1db410dSStanislav Kinsbursky {
1949a1db410dSStanislav Kinsbursky 	gss_svc_shutdown_net(net);
1950a1db410dSStanislav Kinsbursky }
1951a1db410dSStanislav Kinsbursky 
1952a1db410dSStanislav Kinsbursky static struct pernet_operations rpcsec_gss_net_ops = {
1953a1db410dSStanislav Kinsbursky 	.init = rpcsec_gss_init_net,
1954a1db410dSStanislav Kinsbursky 	.exit = rpcsec_gss_exit_net,
1955a1db410dSStanislav Kinsbursky };
1956a1db410dSStanislav Kinsbursky 
19571da177e4SLinus Torvalds /*
19581da177e4SLinus Torvalds  * Initialize RPCSEC_GSS module
19591da177e4SLinus Torvalds  */
19601da177e4SLinus Torvalds static int __init init_rpcsec_gss(void)
19611da177e4SLinus Torvalds {
19621da177e4SLinus Torvalds 	int err = 0;
19631da177e4SLinus Torvalds 
19641da177e4SLinus Torvalds 	err = rpcauth_register(&authgss_ops);
19651da177e4SLinus Torvalds 	if (err)
19661da177e4SLinus Torvalds 		goto out;
19671da177e4SLinus Torvalds 	err = gss_svc_init();
19681da177e4SLinus Torvalds 	if (err)
19691da177e4SLinus Torvalds 		goto out_unregister;
1970a1db410dSStanislav Kinsbursky 	err = register_pernet_subsys(&rpcsec_gss_net_ops);
1971a1db410dSStanislav Kinsbursky 	if (err)
1972a1db410dSStanislav Kinsbursky 		goto out_svc_exit;
197379a3f20bS\"J. Bruce Fields\ 	rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version");
19741da177e4SLinus Torvalds 	return 0;
1975a1db410dSStanislav Kinsbursky out_svc_exit:
1976a1db410dSStanislav Kinsbursky 	gss_svc_shutdown();
19771da177e4SLinus Torvalds out_unregister:
19781da177e4SLinus Torvalds 	rpcauth_unregister(&authgss_ops);
19791da177e4SLinus Torvalds out:
19801da177e4SLinus Torvalds 	return err;
19811da177e4SLinus Torvalds }
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds static void __exit exit_rpcsec_gss(void)
19841da177e4SLinus Torvalds {
1985a1db410dSStanislav Kinsbursky 	unregister_pernet_subsys(&rpcsec_gss_net_ops);
19861da177e4SLinus Torvalds 	gss_svc_shutdown();
19871da177e4SLinus Torvalds 	rpcauth_unregister(&authgss_ops);
1988bf12691dSJesper Dangaard Brouer 	rcu_barrier(); /* Wait for completion of call_rcu()'s */
19891da177e4SLinus Torvalds }
19901da177e4SLinus Torvalds 
199171afa85eSChuck Lever MODULE_ALIAS("rpc-auth-6");
19921da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1993126e216aSTrond Myklebust module_param_named(expired_cred_retry_delay,
1994126e216aSTrond Myklebust 		   gss_expired_cred_retry_delay,
1995126e216aSTrond Myklebust 		   uint, 0644);
1996126e216aSTrond Myklebust MODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until "
1997126e216aSTrond Myklebust 		"the RPC engine retries an expired credential");
1998126e216aSTrond Myklebust 
19994de6caa2SAndy Adamson module_param_named(key_expire_timeo,
20004de6caa2SAndy Adamson 		   gss_key_expire_timeo,
20014de6caa2SAndy Adamson 		   uint, 0644);
20024de6caa2SAndy Adamson MODULE_PARM_DESC(key_expire_timeo, "Time (in seconds) at the end of a "
20034de6caa2SAndy Adamson 		"credential keys lifetime where the NFS layer cleans up "
20044de6caa2SAndy Adamson 		"prior to key expiration");
20054de6caa2SAndy Adamson 
20061da177e4SLinus Torvalds module_init(init_rpcsec_gss)
20071da177e4SLinus Torvalds module_exit(exit_rpcsec_gss)
2008