auth_gss.c revision eb6dc19d8e72ce3a957af5511d20c0db0a8bd007
1/*
2 * linux/net/sunrpc/auth_gss/auth_gss.c
3 *
4 * RPCSEC_GSS client authentication.
5 *
6 *  Copyright (c) 2000 The Regents of the University of Michigan.
7 *  All rights reserved.
8 *
9 *  Dug Song       <dugsong@monkey.org>
10 *  Andy Adamson   <andros@umich.edu>
11 *
12 *  Redistribution and use in source and binary forms, with or without
13 *  modification, are permitted provided that the following conditions
14 *  are met:
15 *
16 *  1. Redistributions of source code must retain the above copyright
17 *     notice, this list of conditions and the following disclaimer.
18 *  2. Redistributions in binary form must reproduce the above copyright
19 *     notice, this list of conditions and the following disclaimer in the
20 *     documentation and/or other materials provided with the distribution.
21 *  3. Neither the name of the University nor the names of its
22 *     contributors may be used to endorse or promote products derived
23 *     from this software without specific prior written permission.
24 *
25 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/types.h>
42#include <linux/slab.h>
43#include <linux/sched.h>
44#include <linux/pagemap.h>
45#include <linux/sunrpc/clnt.h>
46#include <linux/sunrpc/auth.h>
47#include <linux/sunrpc/auth_gss.h>
48#include <linux/sunrpc/svcauth_gss.h>
49#include <linux/sunrpc/gss_err.h>
50#include <linux/workqueue.h>
51#include <linux/sunrpc/rpc_pipe_fs.h>
52#include <linux/sunrpc/gss_api.h>
53#include <asm/uaccess.h>
54#include <linux/hashtable.h>
55
56#include "../netns.h"
57
58static const struct rpc_authops authgss_ops;
59
60static const struct rpc_credops gss_credops;
61static const struct rpc_credops gss_nullops;
62
63#define GSS_RETRY_EXPIRED 5
64static unsigned int gss_expired_cred_retry_delay = GSS_RETRY_EXPIRED;
65
66#ifdef RPC_DEBUG
67# define RPCDBG_FACILITY	RPCDBG_AUTH
68#endif
69
70#define GSS_CRED_SLACK		(RPC_MAX_AUTH_SIZE * 2)
71/* length of a krb5 verifier (48), plus data added before arguments when
72 * using integrity (two 4-byte integers): */
73#define GSS_VERF_SLACK		100
74
75static DEFINE_HASHTABLE(gss_auth_hash_table, 16);
76static DEFINE_SPINLOCK(gss_auth_hash_lock);
77
78struct gss_pipe {
79	struct rpc_pipe_dir_object pdo;
80	struct rpc_pipe *pipe;
81	struct rpc_clnt *clnt;
82	const char *name;
83	struct kref kref;
84};
85
86struct gss_auth {
87	struct kref kref;
88	struct hlist_node hash;
89	struct rpc_auth rpc_auth;
90	struct gss_api_mech *mech;
91	enum rpc_gss_svc service;
92	struct rpc_clnt *client;
93	struct net *net;
94	/*
95	 * There are two upcall pipes; dentry[1], named "gssd", is used
96	 * for the new text-based upcall; dentry[0] is named after the
97	 * mechanism (for example, "krb5") and exists for
98	 * backwards-compatibility with older gssd's.
99	 */
100	struct gss_pipe *gss_pipe[2];
101	const char *target_name;
102};
103
104/* pipe_version >= 0 if and only if someone has a pipe open. */
105static DEFINE_SPINLOCK(pipe_version_lock);
106static struct rpc_wait_queue pipe_version_rpc_waitqueue;
107static DECLARE_WAIT_QUEUE_HEAD(pipe_version_waitqueue);
108
109static void gss_free_ctx(struct gss_cl_ctx *);
110static const struct rpc_pipe_ops gss_upcall_ops_v0;
111static const struct rpc_pipe_ops gss_upcall_ops_v1;
112
113static inline struct gss_cl_ctx *
114gss_get_ctx(struct gss_cl_ctx *ctx)
115{
116	atomic_inc(&ctx->count);
117	return ctx;
118}
119
120static inline void
121gss_put_ctx(struct gss_cl_ctx *ctx)
122{
123	if (atomic_dec_and_test(&ctx->count))
124		gss_free_ctx(ctx);
125}
126
127/* gss_cred_set_ctx:
128 * called by gss_upcall_callback and gss_create_upcall in order
129 * to set the gss context. The actual exchange of an old context
130 * and a new one is protected by the pipe->lock.
131 */
132static void
133gss_cred_set_ctx(struct rpc_cred *cred, struct gss_cl_ctx *ctx)
134{
135	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
136
137	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
138		return;
139	gss_get_ctx(ctx);
140	rcu_assign_pointer(gss_cred->gc_ctx, ctx);
141	set_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
142	smp_mb__before_clear_bit();
143	clear_bit(RPCAUTH_CRED_NEW, &cred->cr_flags);
144}
145
146static const void *
147simple_get_bytes(const void *p, const void *end, void *res, size_t len)
148{
149	const void *q = (const void *)((const char *)p + len);
150	if (unlikely(q > end || q < p))
151		return ERR_PTR(-EFAULT);
152	memcpy(res, p, len);
153	return q;
154}
155
156static inline const void *
157simple_get_netobj(const void *p, const void *end, struct xdr_netobj *dest)
158{
159	const void *q;
160	unsigned int len;
161
162	p = simple_get_bytes(p, end, &len, sizeof(len));
163	if (IS_ERR(p))
164		return p;
165	q = (const void *)((const char *)p + len);
166	if (unlikely(q > end || q < p))
167		return ERR_PTR(-EFAULT);
168	dest->data = kmemdup(p, len, GFP_NOFS);
169	if (unlikely(dest->data == NULL))
170		return ERR_PTR(-ENOMEM);
171	dest->len = len;
172	return q;
173}
174
175static struct gss_cl_ctx *
176gss_cred_get_ctx(struct rpc_cred *cred)
177{
178	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
179	struct gss_cl_ctx *ctx = NULL;
180
181	rcu_read_lock();
182	if (gss_cred->gc_ctx)
183		ctx = gss_get_ctx(gss_cred->gc_ctx);
184	rcu_read_unlock();
185	return ctx;
186}
187
188static struct gss_cl_ctx *
189gss_alloc_context(void)
190{
191	struct gss_cl_ctx *ctx;
192
193	ctx = kzalloc(sizeof(*ctx), GFP_NOFS);
194	if (ctx != NULL) {
195		ctx->gc_proc = RPC_GSS_PROC_DATA;
196		ctx->gc_seq = 1;	/* NetApp 6.4R1 doesn't accept seq. no. 0 */
197		spin_lock_init(&ctx->gc_seq_lock);
198		atomic_set(&ctx->count,1);
199	}
200	return ctx;
201}
202
203#define GSSD_MIN_TIMEOUT (60 * 60)
204static const void *
205gss_fill_context(const void *p, const void *end, struct gss_cl_ctx *ctx, struct gss_api_mech *gm)
206{
207	const void *q;
208	unsigned int seclen;
209	unsigned int timeout;
210	unsigned long now = jiffies;
211	u32 window_size;
212	int ret;
213
214	/* First unsigned int gives the remaining lifetime in seconds of the
215	 * credential - e.g. the remaining TGT lifetime for Kerberos or
216	 * the -t value passed to GSSD.
217	 */
218	p = simple_get_bytes(p, end, &timeout, sizeof(timeout));
219	if (IS_ERR(p))
220		goto err;
221	if (timeout == 0)
222		timeout = GSSD_MIN_TIMEOUT;
223	ctx->gc_expiry = now + ((unsigned long)timeout * HZ);
224	/* Sequence number window. Determines the maximum number of
225	 * simultaneous requests
226	 */
227	p = simple_get_bytes(p, end, &window_size, sizeof(window_size));
228	if (IS_ERR(p))
229		goto err;
230	ctx->gc_win = window_size;
231	/* gssd signals an error by passing ctx->gc_win = 0: */
232	if (ctx->gc_win == 0) {
233		/*
234		 * in which case, p points to an error code. Anything other
235		 * than -EKEYEXPIRED gets converted to -EACCES.
236		 */
237		p = simple_get_bytes(p, end, &ret, sizeof(ret));
238		if (!IS_ERR(p))
239			p = (ret == -EKEYEXPIRED) ? ERR_PTR(-EKEYEXPIRED) :
240						    ERR_PTR(-EACCES);
241		goto err;
242	}
243	/* copy the opaque wire context */
244	p = simple_get_netobj(p, end, &ctx->gc_wire_ctx);
245	if (IS_ERR(p))
246		goto err;
247	/* import the opaque security context */
248	p  = simple_get_bytes(p, end, &seclen, sizeof(seclen));
249	if (IS_ERR(p))
250		goto err;
251	q = (const void *)((const char *)p + seclen);
252	if (unlikely(q > end || q < p)) {
253		p = ERR_PTR(-EFAULT);
254		goto err;
255	}
256	ret = gss_import_sec_context(p, seclen, gm, &ctx->gc_gss_ctx, NULL, GFP_NOFS);
257	if (ret < 0) {
258		p = ERR_PTR(ret);
259		goto err;
260	}
261	dprintk("RPC:       %s Success. gc_expiry %lu now %lu timeout %u\n",
262		__func__, ctx->gc_expiry, now, timeout);
263	return q;
264err:
265	dprintk("RPC:       %s returns error %ld\n", __func__, -PTR_ERR(p));
266	return p;
267}
268
269#define UPCALL_BUF_LEN 128
270
271struct gss_upcall_msg {
272	atomic_t count;
273	kuid_t	uid;
274	struct rpc_pipe_msg msg;
275	struct list_head list;
276	struct gss_auth *auth;
277	struct rpc_pipe *pipe;
278	struct rpc_wait_queue rpc_waitqueue;
279	wait_queue_head_t waitqueue;
280	struct gss_cl_ctx *ctx;
281	char databuf[UPCALL_BUF_LEN];
282};
283
284static int get_pipe_version(struct net *net)
285{
286	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
287	int ret;
288
289	spin_lock(&pipe_version_lock);
290	if (sn->pipe_version >= 0) {
291		atomic_inc(&sn->pipe_users);
292		ret = sn->pipe_version;
293	} else
294		ret = -EAGAIN;
295	spin_unlock(&pipe_version_lock);
296	return ret;
297}
298
299static void put_pipe_version(struct net *net)
300{
301	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
302
303	if (atomic_dec_and_lock(&sn->pipe_users, &pipe_version_lock)) {
304		sn->pipe_version = -1;
305		spin_unlock(&pipe_version_lock);
306	}
307}
308
309static void
310gss_release_msg(struct gss_upcall_msg *gss_msg)
311{
312	struct net *net = gss_msg->auth->net;
313	if (!atomic_dec_and_test(&gss_msg->count))
314		return;
315	put_pipe_version(net);
316	BUG_ON(!list_empty(&gss_msg->list));
317	if (gss_msg->ctx != NULL)
318		gss_put_ctx(gss_msg->ctx);
319	rpc_destroy_wait_queue(&gss_msg->rpc_waitqueue);
320	kfree(gss_msg);
321}
322
323static struct gss_upcall_msg *
324__gss_find_upcall(struct rpc_pipe *pipe, kuid_t uid)
325{
326	struct gss_upcall_msg *pos;
327	list_for_each_entry(pos, &pipe->in_downcall, list) {
328		if (!uid_eq(pos->uid, uid))
329			continue;
330		atomic_inc(&pos->count);
331		dprintk("RPC:       %s found msg %p\n", __func__, pos);
332		return pos;
333	}
334	dprintk("RPC:       %s found nothing\n", __func__);
335	return NULL;
336}
337
338/* Try to add an upcall to the pipefs queue.
339 * If an upcall owned by our uid already exists, then we return a reference
340 * to that upcall instead of adding the new upcall.
341 */
342static inline struct gss_upcall_msg *
343gss_add_msg(struct gss_upcall_msg *gss_msg)
344{
345	struct rpc_pipe *pipe = gss_msg->pipe;
346	struct gss_upcall_msg *old;
347
348	spin_lock(&pipe->lock);
349	old = __gss_find_upcall(pipe, gss_msg->uid);
350	if (old == NULL) {
351		atomic_inc(&gss_msg->count);
352		list_add(&gss_msg->list, &pipe->in_downcall);
353	} else
354		gss_msg = old;
355	spin_unlock(&pipe->lock);
356	return gss_msg;
357}
358
359static void
360__gss_unhash_msg(struct gss_upcall_msg *gss_msg)
361{
362	list_del_init(&gss_msg->list);
363	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
364	wake_up_all(&gss_msg->waitqueue);
365	atomic_dec(&gss_msg->count);
366}
367
368static void
369gss_unhash_msg(struct gss_upcall_msg *gss_msg)
370{
371	struct rpc_pipe *pipe = gss_msg->pipe;
372
373	if (list_empty(&gss_msg->list))
374		return;
375	spin_lock(&pipe->lock);
376	if (!list_empty(&gss_msg->list))
377		__gss_unhash_msg(gss_msg);
378	spin_unlock(&pipe->lock);
379}
380
381static void
382gss_handle_downcall_result(struct gss_cred *gss_cred, struct gss_upcall_msg *gss_msg)
383{
384	switch (gss_msg->msg.errno) {
385	case 0:
386		if (gss_msg->ctx == NULL)
387			break;
388		clear_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
389		gss_cred_set_ctx(&gss_cred->gc_base, gss_msg->ctx);
390		break;
391	case -EKEYEXPIRED:
392		set_bit(RPCAUTH_CRED_NEGATIVE, &gss_cred->gc_base.cr_flags);
393	}
394	gss_cred->gc_upcall_timestamp = jiffies;
395	gss_cred->gc_upcall = NULL;
396	rpc_wake_up_status(&gss_msg->rpc_waitqueue, gss_msg->msg.errno);
397}
398
399static void
400gss_upcall_callback(struct rpc_task *task)
401{
402	struct gss_cred *gss_cred = container_of(task->tk_rqstp->rq_cred,
403			struct gss_cred, gc_base);
404	struct gss_upcall_msg *gss_msg = gss_cred->gc_upcall;
405	struct rpc_pipe *pipe = gss_msg->pipe;
406
407	spin_lock(&pipe->lock);
408	gss_handle_downcall_result(gss_cred, gss_msg);
409	spin_unlock(&pipe->lock);
410	task->tk_status = gss_msg->msg.errno;
411	gss_release_msg(gss_msg);
412}
413
414static void gss_encode_v0_msg(struct gss_upcall_msg *gss_msg)
415{
416	uid_t uid = from_kuid(&init_user_ns, gss_msg->uid);
417	memcpy(gss_msg->databuf, &uid, sizeof(uid));
418	gss_msg->msg.data = gss_msg->databuf;
419	gss_msg->msg.len = sizeof(uid);
420	BUG_ON(sizeof(uid) > UPCALL_BUF_LEN);
421}
422
423static void gss_encode_v1_msg(struct gss_upcall_msg *gss_msg,
424				const char *service_name,
425				const char *target_name)
426{
427	struct gss_api_mech *mech = gss_msg->auth->mech;
428	char *p = gss_msg->databuf;
429	int len = 0;
430
431	gss_msg->msg.len = sprintf(gss_msg->databuf, "mech=%s uid=%d ",
432				   mech->gm_name,
433				   from_kuid(&init_user_ns, gss_msg->uid));
434	p += gss_msg->msg.len;
435	if (target_name) {
436		len = sprintf(p, "target=%s ", target_name);
437		p += len;
438		gss_msg->msg.len += len;
439	}
440	if (service_name != NULL) {
441		len = sprintf(p, "service=%s ", service_name);
442		p += len;
443		gss_msg->msg.len += len;
444	}
445	if (mech->gm_upcall_enctypes) {
446		len = sprintf(p, "enctypes=%s ", mech->gm_upcall_enctypes);
447		p += len;
448		gss_msg->msg.len += len;
449	}
450	len = sprintf(p, "\n");
451	gss_msg->msg.len += len;
452
453	gss_msg->msg.data = gss_msg->databuf;
454	BUG_ON(gss_msg->msg.len > UPCALL_BUF_LEN);
455}
456
457static struct gss_upcall_msg *
458gss_alloc_msg(struct gss_auth *gss_auth,
459		kuid_t uid, const char *service_name)
460{
461	struct gss_upcall_msg *gss_msg;
462	int vers;
463
464	gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
465	if (gss_msg == NULL)
466		return ERR_PTR(-ENOMEM);
467	vers = get_pipe_version(gss_auth->net);
468	if (vers < 0) {
469		kfree(gss_msg);
470		return ERR_PTR(vers);
471	}
472	gss_msg->pipe = gss_auth->gss_pipe[vers]->pipe;
473	INIT_LIST_HEAD(&gss_msg->list);
474	rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
475	init_waitqueue_head(&gss_msg->waitqueue);
476	atomic_set(&gss_msg->count, 1);
477	gss_msg->uid = uid;
478	gss_msg->auth = gss_auth;
479	switch (vers) {
480	case 0:
481		gss_encode_v0_msg(gss_msg);
482	default:
483		gss_encode_v1_msg(gss_msg, service_name, gss_auth->target_name);
484	};
485	return gss_msg;
486}
487
488static struct gss_upcall_msg *
489gss_setup_upcall(struct gss_auth *gss_auth, struct rpc_cred *cred)
490{
491	struct gss_cred *gss_cred = container_of(cred,
492			struct gss_cred, gc_base);
493	struct gss_upcall_msg *gss_new, *gss_msg;
494	kuid_t uid = cred->cr_uid;
495
496	gss_new = gss_alloc_msg(gss_auth, uid, gss_cred->gc_principal);
497	if (IS_ERR(gss_new))
498		return gss_new;
499	gss_msg = gss_add_msg(gss_new);
500	if (gss_msg == gss_new) {
501		int res = rpc_queue_upcall(gss_new->pipe, &gss_new->msg);
502		if (res) {
503			gss_unhash_msg(gss_new);
504			gss_msg = ERR_PTR(res);
505		}
506	} else
507		gss_release_msg(gss_new);
508	return gss_msg;
509}
510
511static void warn_gssd(void)
512{
513	static unsigned long ratelimit;
514	unsigned long now = jiffies;
515
516	if (time_after(now, ratelimit)) {
517		printk(KERN_WARNING "RPC: AUTH_GSS upcall timed out.\n"
518				"Please check user daemon is running.\n");
519		ratelimit = now + 15*HZ;
520	}
521}
522
523static inline int
524gss_refresh_upcall(struct rpc_task *task)
525{
526	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
527	struct gss_auth *gss_auth = container_of(cred->cr_auth,
528			struct gss_auth, rpc_auth);
529	struct gss_cred *gss_cred = container_of(cred,
530			struct gss_cred, gc_base);
531	struct gss_upcall_msg *gss_msg;
532	struct rpc_pipe *pipe;
533	int err = 0;
534
535	dprintk("RPC: %5u %s for uid %u\n",
536		task->tk_pid, __func__, from_kuid(&init_user_ns, cred->cr_uid));
537	gss_msg = gss_setup_upcall(gss_auth, cred);
538	if (PTR_ERR(gss_msg) == -EAGAIN) {
539		/* XXX: warning on the first, under the assumption we
540		 * shouldn't normally hit this case on a refresh. */
541		warn_gssd();
542		task->tk_timeout = 15*HZ;
543		rpc_sleep_on(&pipe_version_rpc_waitqueue, task, NULL);
544		return -EAGAIN;
545	}
546	if (IS_ERR(gss_msg)) {
547		err = PTR_ERR(gss_msg);
548		goto out;
549	}
550	pipe = gss_msg->pipe;
551	spin_lock(&pipe->lock);
552	if (gss_cred->gc_upcall != NULL)
553		rpc_sleep_on(&gss_cred->gc_upcall->rpc_waitqueue, task, NULL);
554	else if (gss_msg->ctx == NULL && gss_msg->msg.errno >= 0) {
555		task->tk_timeout = 0;
556		gss_cred->gc_upcall = gss_msg;
557		/* gss_upcall_callback will release the reference to gss_upcall_msg */
558		atomic_inc(&gss_msg->count);
559		rpc_sleep_on(&gss_msg->rpc_waitqueue, task, gss_upcall_callback);
560	} else {
561		gss_handle_downcall_result(gss_cred, gss_msg);
562		err = gss_msg->msg.errno;
563	}
564	spin_unlock(&pipe->lock);
565	gss_release_msg(gss_msg);
566out:
567	dprintk("RPC: %5u %s for uid %u result %d\n",
568		task->tk_pid, __func__,
569		from_kuid(&init_user_ns, cred->cr_uid),	err);
570	return err;
571}
572
573static inline int
574gss_create_upcall(struct gss_auth *gss_auth, struct gss_cred *gss_cred)
575{
576	struct net *net = gss_auth->net;
577	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
578	struct rpc_pipe *pipe;
579	struct rpc_cred *cred = &gss_cred->gc_base;
580	struct gss_upcall_msg *gss_msg;
581	unsigned long timeout;
582	DEFINE_WAIT(wait);
583	int err;
584
585	dprintk("RPC:       %s for uid %u\n",
586		__func__, from_kuid(&init_user_ns, cred->cr_uid));
587retry:
588	err = 0;
589	/* Default timeout is 15s unless we know that gssd is not running */
590	timeout = 15 * HZ;
591	if (!sn->gssd_running)
592		timeout = HZ >> 2;
593	gss_msg = gss_setup_upcall(gss_auth, cred);
594	if (PTR_ERR(gss_msg) == -EAGAIN) {
595		err = wait_event_interruptible_timeout(pipe_version_waitqueue,
596				sn->pipe_version >= 0, timeout);
597		if (sn->pipe_version < 0) {
598			if (err == 0)
599				sn->gssd_running = 0;
600			warn_gssd();
601			err = -EACCES;
602		}
603		if (err < 0)
604			goto out;
605		goto retry;
606	}
607	if (IS_ERR(gss_msg)) {
608		err = PTR_ERR(gss_msg);
609		goto out;
610	}
611	pipe = gss_msg->pipe;
612	for (;;) {
613		prepare_to_wait(&gss_msg->waitqueue, &wait, TASK_KILLABLE);
614		spin_lock(&pipe->lock);
615		if (gss_msg->ctx != NULL || gss_msg->msg.errno < 0) {
616			break;
617		}
618		spin_unlock(&pipe->lock);
619		if (fatal_signal_pending(current)) {
620			err = -ERESTARTSYS;
621			goto out_intr;
622		}
623		schedule();
624	}
625	if (gss_msg->ctx)
626		gss_cred_set_ctx(cred, gss_msg->ctx);
627	else
628		err = gss_msg->msg.errno;
629	spin_unlock(&pipe->lock);
630out_intr:
631	finish_wait(&gss_msg->waitqueue, &wait);
632	gss_release_msg(gss_msg);
633out:
634	dprintk("RPC:       %s for uid %u result %d\n",
635		__func__, from_kuid(&init_user_ns, cred->cr_uid), err);
636	return err;
637}
638
639#define MSG_BUF_MAXSIZE 1024
640
641static ssize_t
642gss_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
643{
644	const void *p, *end;
645	void *buf;
646	struct gss_upcall_msg *gss_msg;
647	struct rpc_pipe *pipe = RPC_I(file_inode(filp))->pipe;
648	struct gss_cl_ctx *ctx;
649	uid_t id;
650	kuid_t uid;
651	ssize_t err = -EFBIG;
652
653	if (mlen > MSG_BUF_MAXSIZE)
654		goto out;
655	err = -ENOMEM;
656	buf = kmalloc(mlen, GFP_NOFS);
657	if (!buf)
658		goto out;
659
660	err = -EFAULT;
661	if (copy_from_user(buf, src, mlen))
662		goto err;
663
664	end = (const void *)((char *)buf + mlen);
665	p = simple_get_bytes(buf, end, &id, sizeof(id));
666	if (IS_ERR(p)) {
667		err = PTR_ERR(p);
668		goto err;
669	}
670
671	uid = make_kuid(&init_user_ns, id);
672	if (!uid_valid(uid)) {
673		err = -EINVAL;
674		goto err;
675	}
676
677	err = -ENOMEM;
678	ctx = gss_alloc_context();
679	if (ctx == NULL)
680		goto err;
681
682	err = -ENOENT;
683	/* Find a matching upcall */
684	spin_lock(&pipe->lock);
685	gss_msg = __gss_find_upcall(pipe, uid);
686	if (gss_msg == NULL) {
687		spin_unlock(&pipe->lock);
688		goto err_put_ctx;
689	}
690	list_del_init(&gss_msg->list);
691	spin_unlock(&pipe->lock);
692
693	p = gss_fill_context(p, end, ctx, gss_msg->auth->mech);
694	if (IS_ERR(p)) {
695		err = PTR_ERR(p);
696		switch (err) {
697		case -EACCES:
698		case -EKEYEXPIRED:
699			gss_msg->msg.errno = err;
700			err = mlen;
701			break;
702		case -EFAULT:
703		case -ENOMEM:
704		case -EINVAL:
705		case -ENOSYS:
706			gss_msg->msg.errno = -EAGAIN;
707			break;
708		default:
709			printk(KERN_CRIT "%s: bad return from "
710				"gss_fill_context: %zd\n", __func__, err);
711			BUG();
712		}
713		goto err_release_msg;
714	}
715	gss_msg->ctx = gss_get_ctx(ctx);
716	err = mlen;
717
718err_release_msg:
719	spin_lock(&pipe->lock);
720	__gss_unhash_msg(gss_msg);
721	spin_unlock(&pipe->lock);
722	gss_release_msg(gss_msg);
723err_put_ctx:
724	gss_put_ctx(ctx);
725err:
726	kfree(buf);
727out:
728	dprintk("RPC:       %s returning %Zd\n", __func__, err);
729	return err;
730}
731
732static int gss_pipe_open(struct inode *inode, int new_version)
733{
734	struct net *net = inode->i_sb->s_fs_info;
735	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
736	int ret = 0;
737
738	spin_lock(&pipe_version_lock);
739	if (sn->pipe_version < 0) {
740		/* First open of any gss pipe determines the version: */
741		sn->pipe_version = new_version;
742		rpc_wake_up(&pipe_version_rpc_waitqueue);
743		wake_up(&pipe_version_waitqueue);
744	} else if (sn->pipe_version != new_version) {
745		/* Trying to open a pipe of a different version */
746		ret = -EBUSY;
747		goto out;
748	}
749	atomic_inc(&sn->pipe_users);
750out:
751	spin_unlock(&pipe_version_lock);
752	return ret;
753
754}
755
756static int gss_pipe_open_v0(struct inode *inode)
757{
758	return gss_pipe_open(inode, 0);
759}
760
761static int gss_pipe_open_v1(struct inode *inode)
762{
763	return gss_pipe_open(inode, 1);
764}
765
766static void
767gss_pipe_release(struct inode *inode)
768{
769	struct net *net = inode->i_sb->s_fs_info;
770	struct rpc_pipe *pipe = RPC_I(inode)->pipe;
771	struct gss_upcall_msg *gss_msg;
772
773restart:
774	spin_lock(&pipe->lock);
775	list_for_each_entry(gss_msg, &pipe->in_downcall, list) {
776
777		if (!list_empty(&gss_msg->msg.list))
778			continue;
779		gss_msg->msg.errno = -EPIPE;
780		atomic_inc(&gss_msg->count);
781		__gss_unhash_msg(gss_msg);
782		spin_unlock(&pipe->lock);
783		gss_release_msg(gss_msg);
784		goto restart;
785	}
786	spin_unlock(&pipe->lock);
787
788	put_pipe_version(net);
789}
790
791static void
792gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
793{
794	struct gss_upcall_msg *gss_msg = container_of(msg, struct gss_upcall_msg, msg);
795
796	if (msg->errno < 0) {
797		dprintk("RPC:       %s releasing msg %p\n",
798			__func__, gss_msg);
799		atomic_inc(&gss_msg->count);
800		gss_unhash_msg(gss_msg);
801		if (msg->errno == -ETIMEDOUT)
802			warn_gssd();
803		gss_release_msg(gss_msg);
804	}
805}
806
807static void gss_pipe_dentry_destroy(struct dentry *dir,
808		struct rpc_pipe_dir_object *pdo)
809{
810	struct gss_pipe *gss_pipe = pdo->pdo_data;
811	struct rpc_pipe *pipe = gss_pipe->pipe;
812
813	if (pipe->dentry != NULL) {
814		rpc_unlink(pipe->dentry);
815		pipe->dentry = NULL;
816	}
817}
818
819static int gss_pipe_dentry_create(struct dentry *dir,
820		struct rpc_pipe_dir_object *pdo)
821{
822	struct gss_pipe *p = pdo->pdo_data;
823	struct dentry *dentry;
824
825	dentry = rpc_mkpipe_dentry(dir, p->name, p->clnt, p->pipe);
826	if (IS_ERR(dentry))
827		return PTR_ERR(dentry);
828	p->pipe->dentry = dentry;
829	return 0;
830}
831
832static const struct rpc_pipe_dir_object_ops gss_pipe_dir_object_ops = {
833	.create = gss_pipe_dentry_create,
834	.destroy = gss_pipe_dentry_destroy,
835};
836
837static struct gss_pipe *gss_pipe_alloc(struct rpc_clnt *clnt,
838		const char *name,
839		const struct rpc_pipe_ops *upcall_ops)
840{
841	struct gss_pipe *p;
842	int err = -ENOMEM;
843
844	p = kmalloc(sizeof(*p), GFP_KERNEL);
845	if (p == NULL)
846		goto err;
847	p->pipe = rpc_mkpipe_data(upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
848	if (IS_ERR(p->pipe)) {
849		err = PTR_ERR(p->pipe);
850		goto err_free_gss_pipe;
851	}
852	p->name = name;
853	p->clnt = clnt;
854	kref_init(&p->kref);
855	rpc_init_pipe_dir_object(&p->pdo,
856			&gss_pipe_dir_object_ops,
857			p);
858	return p;
859err_free_gss_pipe:
860	kfree(p);
861err:
862	return ERR_PTR(err);
863}
864
865struct gss_alloc_pdo {
866	struct rpc_clnt *clnt;
867	const char *name;
868	const struct rpc_pipe_ops *upcall_ops;
869};
870
871static int gss_pipe_match_pdo(struct rpc_pipe_dir_object *pdo, void *data)
872{
873	struct gss_pipe *gss_pipe;
874	struct gss_alloc_pdo *args = data;
875
876	if (pdo->pdo_ops != &gss_pipe_dir_object_ops)
877		return 0;
878	gss_pipe = container_of(pdo, struct gss_pipe, pdo);
879	if (strcmp(gss_pipe->name, args->name) != 0)
880		return 0;
881	if (!kref_get_unless_zero(&gss_pipe->kref))
882		return 0;
883	return 1;
884}
885
886static struct rpc_pipe_dir_object *gss_pipe_alloc_pdo(void *data)
887{
888	struct gss_pipe *gss_pipe;
889	struct gss_alloc_pdo *args = data;
890
891	gss_pipe = gss_pipe_alloc(args->clnt, args->name, args->upcall_ops);
892	if (!IS_ERR(gss_pipe))
893		return &gss_pipe->pdo;
894	return NULL;
895}
896
897static struct gss_pipe *gss_pipe_get(struct rpc_clnt *clnt,
898		const char *name,
899		const struct rpc_pipe_ops *upcall_ops)
900{
901	struct net *net = rpc_net_ns(clnt);
902	struct rpc_pipe_dir_object *pdo;
903	struct gss_alloc_pdo args = {
904		.clnt = clnt,
905		.name = name,
906		.upcall_ops = upcall_ops,
907	};
908
909	pdo = rpc_find_or_alloc_pipe_dir_object(net,
910			&clnt->cl_pipedir_objects,
911			gss_pipe_match_pdo,
912			gss_pipe_alloc_pdo,
913			&args);
914	if (pdo != NULL)
915		return container_of(pdo, struct gss_pipe, pdo);
916	return ERR_PTR(-ENOMEM);
917}
918
919static void __gss_pipe_free(struct gss_pipe *p)
920{
921	struct rpc_clnt *clnt = p->clnt;
922	struct net *net = rpc_net_ns(clnt);
923
924	rpc_remove_pipe_dir_object(net,
925			&clnt->cl_pipedir_objects,
926			&p->pdo);
927	rpc_destroy_pipe_data(p->pipe);
928	kfree(p);
929}
930
931static void __gss_pipe_release(struct kref *kref)
932{
933	struct gss_pipe *p = container_of(kref, struct gss_pipe, kref);
934
935	__gss_pipe_free(p);
936}
937
938static void gss_pipe_free(struct gss_pipe *p)
939{
940	if (p != NULL)
941		kref_put(&p->kref, __gss_pipe_release);
942}
943
944/*
945 * NOTE: we have the opportunity to use different
946 * parameters based on the input flavor (which must be a pseudoflavor)
947 */
948static struct gss_auth *
949gss_create_new(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
950{
951	rpc_authflavor_t flavor = args->pseudoflavor;
952	struct gss_auth *gss_auth;
953	struct gss_pipe *gss_pipe;
954	struct rpc_auth * auth;
955	int err = -ENOMEM; /* XXX? */
956
957	dprintk("RPC:       creating GSS authenticator for client %p\n", clnt);
958
959	if (!try_module_get(THIS_MODULE))
960		return ERR_PTR(err);
961	if (!(gss_auth = kmalloc(sizeof(*gss_auth), GFP_KERNEL)))
962		goto out_dec;
963	INIT_HLIST_NODE(&gss_auth->hash);
964	gss_auth->target_name = NULL;
965	if (args->target_name) {
966		gss_auth->target_name = kstrdup(args->target_name, GFP_KERNEL);
967		if (gss_auth->target_name == NULL)
968			goto err_free;
969	}
970	gss_auth->client = clnt;
971	gss_auth->net = get_net(rpc_net_ns(clnt));
972	err = -EINVAL;
973	gss_auth->mech = gss_mech_get_by_pseudoflavor(flavor);
974	if (!gss_auth->mech) {
975		dprintk("RPC:       Pseudoflavor %d not found!\n", flavor);
976		goto err_put_net;
977	}
978	gss_auth->service = gss_pseudoflavor_to_service(gss_auth->mech, flavor);
979	if (gss_auth->service == 0)
980		goto err_put_mech;
981	auth = &gss_auth->rpc_auth;
982	auth->au_cslack = GSS_CRED_SLACK >> 2;
983	auth->au_rslack = GSS_VERF_SLACK >> 2;
984	auth->au_ops = &authgss_ops;
985	auth->au_flavor = flavor;
986	atomic_set(&auth->au_count, 1);
987	kref_init(&gss_auth->kref);
988
989	err = rpcauth_init_credcache(auth);
990	if (err)
991		goto err_put_mech;
992	/*
993	 * Note: if we created the old pipe first, then someone who
994	 * examined the directory at the right moment might conclude
995	 * that we supported only the old pipe.  So we instead create
996	 * the new pipe first.
997	 */
998	gss_pipe = gss_pipe_get(clnt, "gssd", &gss_upcall_ops_v1);
999	if (IS_ERR(gss_pipe)) {
1000		err = PTR_ERR(gss_pipe);
1001		goto err_destroy_credcache;
1002	}
1003	gss_auth->gss_pipe[1] = gss_pipe;
1004
1005	gss_pipe = gss_pipe_get(clnt, gss_auth->mech->gm_name,
1006			&gss_upcall_ops_v0);
1007	if (IS_ERR(gss_pipe)) {
1008		err = PTR_ERR(gss_pipe);
1009		goto err_destroy_pipe_1;
1010	}
1011	gss_auth->gss_pipe[0] = gss_pipe;
1012
1013	return gss_auth;
1014err_destroy_pipe_1:
1015	gss_pipe_free(gss_auth->gss_pipe[1]);
1016err_destroy_credcache:
1017	rpcauth_destroy_credcache(auth);
1018err_put_mech:
1019	gss_mech_put(gss_auth->mech);
1020err_put_net:
1021	put_net(gss_auth->net);
1022err_free:
1023	kfree(gss_auth->target_name);
1024	kfree(gss_auth);
1025out_dec:
1026	module_put(THIS_MODULE);
1027	return ERR_PTR(err);
1028}
1029
1030static void
1031gss_free(struct gss_auth *gss_auth)
1032{
1033	gss_pipe_free(gss_auth->gss_pipe[0]);
1034	gss_pipe_free(gss_auth->gss_pipe[1]);
1035	gss_mech_put(gss_auth->mech);
1036	put_net(gss_auth->net);
1037	kfree(gss_auth->target_name);
1038
1039	kfree(gss_auth);
1040	module_put(THIS_MODULE);
1041}
1042
1043static void
1044gss_free_callback(struct kref *kref)
1045{
1046	struct gss_auth *gss_auth = container_of(kref, struct gss_auth, kref);
1047
1048	gss_free(gss_auth);
1049}
1050
1051static void
1052gss_destroy(struct rpc_auth *auth)
1053{
1054	struct gss_auth *gss_auth = container_of(auth,
1055			struct gss_auth, rpc_auth);
1056
1057	dprintk("RPC:       destroying GSS authenticator %p flavor %d\n",
1058			auth, auth->au_flavor);
1059
1060	if (hash_hashed(&gss_auth->hash)) {
1061		spin_lock(&gss_auth_hash_lock);
1062		hash_del(&gss_auth->hash);
1063		spin_unlock(&gss_auth_hash_lock);
1064	}
1065
1066	gss_pipe_free(gss_auth->gss_pipe[0]);
1067	gss_auth->gss_pipe[0] = NULL;
1068	gss_pipe_free(gss_auth->gss_pipe[1]);
1069	gss_auth->gss_pipe[1] = NULL;
1070	rpcauth_destroy_credcache(auth);
1071
1072	kref_put(&gss_auth->kref, gss_free_callback);
1073}
1074
1075static struct gss_auth *
1076gss_auth_find_or_add_hashed(struct rpc_auth_create_args *args,
1077		struct rpc_clnt *clnt,
1078		struct gss_auth *new)
1079{
1080	struct gss_auth *gss_auth;
1081	unsigned long hashval = (unsigned long)clnt;
1082
1083	spin_lock(&gss_auth_hash_lock);
1084	hash_for_each_possible(gss_auth_hash_table,
1085			gss_auth,
1086			hash,
1087			hashval) {
1088		if (gss_auth->rpc_auth.au_flavor != args->pseudoflavor)
1089			continue;
1090		if (gss_auth->target_name != args->target_name) {
1091			if (gss_auth->target_name == NULL)
1092				continue;
1093			if (args->target_name == NULL)
1094				continue;
1095			if (strcmp(gss_auth->target_name, args->target_name))
1096				continue;
1097		}
1098		if (!atomic_inc_not_zero(&gss_auth->rpc_auth.au_count))
1099			continue;
1100		goto out;
1101	}
1102	if (new)
1103		hash_add(gss_auth_hash_table, &new->hash, hashval);
1104	gss_auth = new;
1105out:
1106	spin_unlock(&gss_auth_hash_lock);
1107	return gss_auth;
1108}
1109
1110static struct gss_auth *
1111gss_create_hashed(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
1112{
1113	struct gss_auth *gss_auth;
1114	struct gss_auth *new;
1115
1116	gss_auth = gss_auth_find_or_add_hashed(args, clnt, NULL);
1117	if (gss_auth != NULL)
1118		goto out;
1119	new = gss_create_new(args, clnt);
1120	if (IS_ERR(new))
1121		return new;
1122	gss_auth = gss_auth_find_or_add_hashed(args, clnt, new);
1123	if (gss_auth != new)
1124		gss_destroy(&new->rpc_auth);
1125out:
1126	return gss_auth;
1127}
1128
1129static struct rpc_auth *
1130gss_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
1131{
1132	struct gss_auth *gss_auth;
1133	struct rpc_xprt *xprt = rcu_access_pointer(clnt->cl_xprt);
1134
1135	while (clnt != clnt->cl_parent) {
1136		struct rpc_clnt *parent = clnt->cl_parent;
1137		/* Find the original parent for this transport */
1138		if (rcu_access_pointer(parent->cl_xprt) != xprt)
1139			break;
1140		clnt = parent;
1141	}
1142
1143	gss_auth = gss_create_hashed(args, clnt);
1144	if (IS_ERR(gss_auth))
1145		return ERR_CAST(gss_auth);
1146	return &gss_auth->rpc_auth;
1147}
1148
1149/*
1150 * gss_destroying_context will cause the RPCSEC_GSS to send a NULL RPC call
1151 * to the server with the GSS control procedure field set to
1152 * RPC_GSS_PROC_DESTROY. This should normally cause the server to release
1153 * all RPCSEC_GSS state associated with that context.
1154 */
1155static int
1156gss_destroying_context(struct rpc_cred *cred)
1157{
1158	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1159	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
1160	struct rpc_task *task;
1161
1162	if (gss_cred->gc_ctx == NULL ||
1163	    test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) == 0)
1164		return 0;
1165
1166	gss_cred->gc_ctx->gc_proc = RPC_GSS_PROC_DESTROY;
1167	cred->cr_ops = &gss_nullops;
1168
1169	/* Take a reference to ensure the cred will be destroyed either
1170	 * by the RPC call or by the put_rpccred() below */
1171	get_rpccred(cred);
1172
1173	task = rpc_call_null(gss_auth->client, cred, RPC_TASK_ASYNC|RPC_TASK_SOFT);
1174	if (!IS_ERR(task))
1175		rpc_put_task(task);
1176
1177	put_rpccred(cred);
1178	return 1;
1179}
1180
1181/* gss_destroy_cred (and gss_free_ctx) are used to clean up after failure
1182 * to create a new cred or context, so they check that things have been
1183 * allocated before freeing them. */
1184static void
1185gss_do_free_ctx(struct gss_cl_ctx *ctx)
1186{
1187	dprintk("RPC:       %s\n", __func__);
1188
1189	gss_delete_sec_context(&ctx->gc_gss_ctx);
1190	kfree(ctx->gc_wire_ctx.data);
1191	kfree(ctx);
1192}
1193
1194static void
1195gss_free_ctx_callback(struct rcu_head *head)
1196{
1197	struct gss_cl_ctx *ctx = container_of(head, struct gss_cl_ctx, gc_rcu);
1198	gss_do_free_ctx(ctx);
1199}
1200
1201static void
1202gss_free_ctx(struct gss_cl_ctx *ctx)
1203{
1204	call_rcu(&ctx->gc_rcu, gss_free_ctx_callback);
1205}
1206
1207static void
1208gss_free_cred(struct gss_cred *gss_cred)
1209{
1210	dprintk("RPC:       %s cred=%p\n", __func__, gss_cred);
1211	kfree(gss_cred);
1212}
1213
1214static void
1215gss_free_cred_callback(struct rcu_head *head)
1216{
1217	struct gss_cred *gss_cred = container_of(head, struct gss_cred, gc_base.cr_rcu);
1218	gss_free_cred(gss_cred);
1219}
1220
1221static void
1222gss_destroy_nullcred(struct rpc_cred *cred)
1223{
1224	struct gss_cred *gss_cred = container_of(cred, struct gss_cred, gc_base);
1225	struct gss_auth *gss_auth = container_of(cred->cr_auth, struct gss_auth, rpc_auth);
1226	struct gss_cl_ctx *ctx = gss_cred->gc_ctx;
1227
1228	RCU_INIT_POINTER(gss_cred->gc_ctx, NULL);
1229	call_rcu(&cred->cr_rcu, gss_free_cred_callback);
1230	if (ctx)
1231		gss_put_ctx(ctx);
1232	kref_put(&gss_auth->kref, gss_free_callback);
1233}
1234
1235static void
1236gss_destroy_cred(struct rpc_cred *cred)
1237{
1238
1239	if (gss_destroying_context(cred))
1240		return;
1241	gss_destroy_nullcred(cred);
1242}
1243
1244/*
1245 * Lookup RPCSEC_GSS cred for the current process
1246 */
1247static struct rpc_cred *
1248gss_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
1249{
1250	return rpcauth_lookup_credcache(auth, acred, flags);
1251}
1252
1253static struct rpc_cred *
1254gss_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
1255{
1256	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
1257	struct gss_cred	*cred = NULL;
1258	int err = -ENOMEM;
1259
1260	dprintk("RPC:       %s for uid %d, flavor %d\n",
1261		__func__, from_kuid(&init_user_ns, acred->uid),
1262		auth->au_flavor);
1263
1264	if (!(cred = kzalloc(sizeof(*cred), GFP_NOFS)))
1265		goto out_err;
1266
1267	rpcauth_init_cred(&cred->gc_base, acred, auth, &gss_credops);
1268	/*
1269	 * Note: in order to force a call to call_refresh(), we deliberately
1270	 * fail to flag the credential as RPCAUTH_CRED_UPTODATE.
1271	 */
1272	cred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_NEW;
1273	cred->gc_service = gss_auth->service;
1274	cred->gc_principal = NULL;
1275	if (acred->machine_cred)
1276		cred->gc_principal = acred->principal;
1277	kref_get(&gss_auth->kref);
1278	return &cred->gc_base;
1279
1280out_err:
1281	dprintk("RPC:       %s failed with error %d\n", __func__, err);
1282	return ERR_PTR(err);
1283}
1284
1285static int
1286gss_cred_init(struct rpc_auth *auth, struct rpc_cred *cred)
1287{
1288	struct gss_auth *gss_auth = container_of(auth, struct gss_auth, rpc_auth);
1289	struct gss_cred *gss_cred = container_of(cred,struct gss_cred, gc_base);
1290	int err;
1291
1292	do {
1293		err = gss_create_upcall(gss_auth, gss_cred);
1294	} while (err == -EAGAIN);
1295	return err;
1296}
1297
1298static int
1299gss_match(struct auth_cred *acred, struct rpc_cred *rc, int flags)
1300{
1301	struct gss_cred *gss_cred = container_of(rc, struct gss_cred, gc_base);
1302
1303	if (test_bit(RPCAUTH_CRED_NEW, &rc->cr_flags))
1304		goto out;
1305	/* Don't match with creds that have expired. */
1306	if (time_after(jiffies, gss_cred->gc_ctx->gc_expiry))
1307		return 0;
1308	if (!test_bit(RPCAUTH_CRED_UPTODATE, &rc->cr_flags))
1309		return 0;
1310out:
1311	if (acred->principal != NULL) {
1312		if (gss_cred->gc_principal == NULL)
1313			return 0;
1314		return strcmp(acred->principal, gss_cred->gc_principal) == 0;
1315	}
1316	if (gss_cred->gc_principal != NULL)
1317		return 0;
1318	return uid_eq(rc->cr_uid, acred->uid);
1319}
1320
1321/*
1322* Marshal credentials.
1323* Maybe we should keep a cached credential for performance reasons.
1324*/
1325static __be32 *
1326gss_marshal(struct rpc_task *task, __be32 *p)
1327{
1328	struct rpc_rqst *req = task->tk_rqstp;
1329	struct rpc_cred *cred = req->rq_cred;
1330	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
1331						 gc_base);
1332	struct gss_cl_ctx	*ctx = gss_cred_get_ctx(cred);
1333	__be32		*cred_len;
1334	u32             maj_stat = 0;
1335	struct xdr_netobj mic;
1336	struct kvec	iov;
1337	struct xdr_buf	verf_buf;
1338
1339	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
1340
1341	*p++ = htonl(RPC_AUTH_GSS);
1342	cred_len = p++;
1343
1344	spin_lock(&ctx->gc_seq_lock);
1345	req->rq_seqno = ctx->gc_seq++;
1346	spin_unlock(&ctx->gc_seq_lock);
1347
1348	*p++ = htonl((u32) RPC_GSS_VERSION);
1349	*p++ = htonl((u32) ctx->gc_proc);
1350	*p++ = htonl((u32) req->rq_seqno);
1351	*p++ = htonl((u32) gss_cred->gc_service);
1352	p = xdr_encode_netobj(p, &ctx->gc_wire_ctx);
1353	*cred_len = htonl((p - (cred_len + 1)) << 2);
1354
1355	/* We compute the checksum for the verifier over the xdr-encoded bytes
1356	 * starting with the xid and ending at the end of the credential: */
1357	iov.iov_base = xprt_skip_transport_header(req->rq_xprt,
1358					req->rq_snd_buf.head[0].iov_base);
1359	iov.iov_len = (u8 *)p - (u8 *)iov.iov_base;
1360	xdr_buf_from_iov(&iov, &verf_buf);
1361
1362	/* set verifier flavor*/
1363	*p++ = htonl(RPC_AUTH_GSS);
1364
1365	mic.data = (u8 *)(p + 1);
1366	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
1367	if (maj_stat == GSS_S_CONTEXT_EXPIRED) {
1368		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
1369	} else if (maj_stat != 0) {
1370		printk("gss_marshal: gss_get_mic FAILED (%d)\n", maj_stat);
1371		goto out_put_ctx;
1372	}
1373	p = xdr_encode_opaque(p, NULL, mic.len);
1374	gss_put_ctx(ctx);
1375	return p;
1376out_put_ctx:
1377	gss_put_ctx(ctx);
1378	return NULL;
1379}
1380
1381static int gss_renew_cred(struct rpc_task *task)
1382{
1383	struct rpc_cred *oldcred = task->tk_rqstp->rq_cred;
1384	struct gss_cred *gss_cred = container_of(oldcred,
1385						 struct gss_cred,
1386						 gc_base);
1387	struct rpc_auth *auth = oldcred->cr_auth;
1388	struct auth_cred acred = {
1389		.uid = oldcred->cr_uid,
1390		.principal = gss_cred->gc_principal,
1391		.machine_cred = (gss_cred->gc_principal != NULL ? 1 : 0),
1392	};
1393	struct rpc_cred *new;
1394
1395	new = gss_lookup_cred(auth, &acred, RPCAUTH_LOOKUP_NEW);
1396	if (IS_ERR(new))
1397		return PTR_ERR(new);
1398	task->tk_rqstp->rq_cred = new;
1399	put_rpccred(oldcred);
1400	return 0;
1401}
1402
1403static int gss_cred_is_negative_entry(struct rpc_cred *cred)
1404{
1405	if (test_bit(RPCAUTH_CRED_NEGATIVE, &cred->cr_flags)) {
1406		unsigned long now = jiffies;
1407		unsigned long begin, expire;
1408		struct gss_cred *gss_cred;
1409
1410		gss_cred = container_of(cred, struct gss_cred, gc_base);
1411		begin = gss_cred->gc_upcall_timestamp;
1412		expire = begin + gss_expired_cred_retry_delay * HZ;
1413
1414		if (time_in_range_open(now, begin, expire))
1415			return 1;
1416	}
1417	return 0;
1418}
1419
1420/*
1421* Refresh credentials. XXX - finish
1422*/
1423static int
1424gss_refresh(struct rpc_task *task)
1425{
1426	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
1427	int ret = 0;
1428
1429	if (gss_cred_is_negative_entry(cred))
1430		return -EKEYEXPIRED;
1431
1432	if (!test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
1433			!test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags)) {
1434		ret = gss_renew_cred(task);
1435		if (ret < 0)
1436			goto out;
1437		cred = task->tk_rqstp->rq_cred;
1438	}
1439
1440	if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags))
1441		ret = gss_refresh_upcall(task);
1442out:
1443	return ret;
1444}
1445
1446/* Dummy refresh routine: used only when destroying the context */
1447static int
1448gss_refresh_null(struct rpc_task *task)
1449{
1450	return -EACCES;
1451}
1452
1453static __be32 *
1454gss_validate(struct rpc_task *task, __be32 *p)
1455{
1456	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
1457	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1458	__be32		seq;
1459	struct kvec	iov;
1460	struct xdr_buf	verf_buf;
1461	struct xdr_netobj mic;
1462	u32		flav,len;
1463	u32		maj_stat;
1464
1465	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
1466
1467	flav = ntohl(*p++);
1468	if ((len = ntohl(*p++)) > RPC_MAX_AUTH_SIZE)
1469		goto out_bad;
1470	if (flav != RPC_AUTH_GSS)
1471		goto out_bad;
1472	seq = htonl(task->tk_rqstp->rq_seqno);
1473	iov.iov_base = &seq;
1474	iov.iov_len = sizeof(seq);
1475	xdr_buf_from_iov(&iov, &verf_buf);
1476	mic.data = (u8 *)p;
1477	mic.len = len;
1478
1479	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &verf_buf, &mic);
1480	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1481		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
1482	if (maj_stat) {
1483		dprintk("RPC: %5u %s: gss_verify_mic returned error 0x%08x\n",
1484			task->tk_pid, __func__, maj_stat);
1485		goto out_bad;
1486	}
1487	/* We leave it to unwrap to calculate au_rslack. For now we just
1488	 * calculate the length of the verifier: */
1489	cred->cr_auth->au_verfsize = XDR_QUADLEN(len) + 2;
1490	gss_put_ctx(ctx);
1491	dprintk("RPC: %5u %s: gss_verify_mic succeeded.\n",
1492			task->tk_pid, __func__);
1493	return p + XDR_QUADLEN(len);
1494out_bad:
1495	gss_put_ctx(ctx);
1496	dprintk("RPC: %5u %s failed.\n", task->tk_pid, __func__);
1497	return NULL;
1498}
1499
1500static void gss_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
1501				__be32 *p, void *obj)
1502{
1503	struct xdr_stream xdr;
1504
1505	xdr_init_encode(&xdr, &rqstp->rq_snd_buf, p);
1506	encode(rqstp, &xdr, obj);
1507}
1508
1509static inline int
1510gss_wrap_req_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1511		   kxdreproc_t encode, struct rpc_rqst *rqstp,
1512		   __be32 *p, void *obj)
1513{
1514	struct xdr_buf	*snd_buf = &rqstp->rq_snd_buf;
1515	struct xdr_buf	integ_buf;
1516	__be32          *integ_len = NULL;
1517	struct xdr_netobj mic;
1518	u32		offset;
1519	__be32		*q;
1520	struct kvec	*iov;
1521	u32             maj_stat = 0;
1522	int		status = -EIO;
1523
1524	integ_len = p++;
1525	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
1526	*p++ = htonl(rqstp->rq_seqno);
1527
1528	gss_wrap_req_encode(encode, rqstp, p, obj);
1529
1530	if (xdr_buf_subsegment(snd_buf, &integ_buf,
1531				offset, snd_buf->len - offset))
1532		return status;
1533	*integ_len = htonl(integ_buf.len);
1534
1535	/* guess whether we're in the head or the tail: */
1536	if (snd_buf->page_len || snd_buf->tail[0].iov_len)
1537		iov = snd_buf->tail;
1538	else
1539		iov = snd_buf->head;
1540	p = iov->iov_base + iov->iov_len;
1541	mic.data = (u8 *)(p + 1);
1542
1543	maj_stat = gss_get_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
1544	status = -EIO; /* XXX? */
1545	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1546		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
1547	else if (maj_stat)
1548		return status;
1549	q = xdr_encode_opaque(p, NULL, mic.len);
1550
1551	offset = (u8 *)q - (u8 *)p;
1552	iov->iov_len += offset;
1553	snd_buf->len += offset;
1554	return 0;
1555}
1556
1557static void
1558priv_release_snd_buf(struct rpc_rqst *rqstp)
1559{
1560	int i;
1561
1562	for (i=0; i < rqstp->rq_enc_pages_num; i++)
1563		__free_page(rqstp->rq_enc_pages[i]);
1564	kfree(rqstp->rq_enc_pages);
1565}
1566
1567static int
1568alloc_enc_pages(struct rpc_rqst *rqstp)
1569{
1570	struct xdr_buf *snd_buf = &rqstp->rq_snd_buf;
1571	int first, last, i;
1572
1573	if (snd_buf->page_len == 0) {
1574		rqstp->rq_enc_pages_num = 0;
1575		return 0;
1576	}
1577
1578	first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
1579	last = (snd_buf->page_base + snd_buf->page_len - 1) >> PAGE_CACHE_SHIFT;
1580	rqstp->rq_enc_pages_num = last - first + 1 + 1;
1581	rqstp->rq_enc_pages
1582		= kmalloc(rqstp->rq_enc_pages_num * sizeof(struct page *),
1583				GFP_NOFS);
1584	if (!rqstp->rq_enc_pages)
1585		goto out;
1586	for (i=0; i < rqstp->rq_enc_pages_num; i++) {
1587		rqstp->rq_enc_pages[i] = alloc_page(GFP_NOFS);
1588		if (rqstp->rq_enc_pages[i] == NULL)
1589			goto out_free;
1590	}
1591	rqstp->rq_release_snd_buf = priv_release_snd_buf;
1592	return 0;
1593out_free:
1594	rqstp->rq_enc_pages_num = i;
1595	priv_release_snd_buf(rqstp);
1596out:
1597	return -EAGAIN;
1598}
1599
1600static inline int
1601gss_wrap_req_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1602		  kxdreproc_t encode, struct rpc_rqst *rqstp,
1603		  __be32 *p, void *obj)
1604{
1605	struct xdr_buf	*snd_buf = &rqstp->rq_snd_buf;
1606	u32		offset;
1607	u32             maj_stat;
1608	int		status;
1609	__be32		*opaque_len;
1610	struct page	**inpages;
1611	int		first;
1612	int		pad;
1613	struct kvec	*iov;
1614	char		*tmp;
1615
1616	opaque_len = p++;
1617	offset = (u8 *)p - (u8 *)snd_buf->head[0].iov_base;
1618	*p++ = htonl(rqstp->rq_seqno);
1619
1620	gss_wrap_req_encode(encode, rqstp, p, obj);
1621
1622	status = alloc_enc_pages(rqstp);
1623	if (status)
1624		return status;
1625	first = snd_buf->page_base >> PAGE_CACHE_SHIFT;
1626	inpages = snd_buf->pages + first;
1627	snd_buf->pages = rqstp->rq_enc_pages;
1628	snd_buf->page_base -= first << PAGE_CACHE_SHIFT;
1629	/*
1630	 * Give the tail its own page, in case we need extra space in the
1631	 * head when wrapping:
1632	 *
1633	 * call_allocate() allocates twice the slack space required
1634	 * by the authentication flavor to rq_callsize.
1635	 * For GSS, slack is GSS_CRED_SLACK.
1636	 */
1637	if (snd_buf->page_len || snd_buf->tail[0].iov_len) {
1638		tmp = page_address(rqstp->rq_enc_pages[rqstp->rq_enc_pages_num - 1]);
1639		memcpy(tmp, snd_buf->tail[0].iov_base, snd_buf->tail[0].iov_len);
1640		snd_buf->tail[0].iov_base = tmp;
1641	}
1642	maj_stat = gss_wrap(ctx->gc_gss_ctx, offset, snd_buf, inpages);
1643	/* slack space should prevent this ever happening: */
1644	BUG_ON(snd_buf->len > snd_buf->buflen);
1645	status = -EIO;
1646	/* We're assuming that when GSS_S_CONTEXT_EXPIRED, the encryption was
1647	 * done anyway, so it's safe to put the request on the wire: */
1648	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1649		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
1650	else if (maj_stat)
1651		return status;
1652
1653	*opaque_len = htonl(snd_buf->len - offset);
1654	/* guess whether we're in the head or the tail: */
1655	if (snd_buf->page_len || snd_buf->tail[0].iov_len)
1656		iov = snd_buf->tail;
1657	else
1658		iov = snd_buf->head;
1659	p = iov->iov_base + iov->iov_len;
1660	pad = 3 - ((snd_buf->len - offset - 1) & 3);
1661	memset(p, 0, pad);
1662	iov->iov_len += pad;
1663	snd_buf->len += pad;
1664
1665	return 0;
1666}
1667
1668static int
1669gss_wrap_req(struct rpc_task *task,
1670	     kxdreproc_t encode, void *rqstp, __be32 *p, void *obj)
1671{
1672	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
1673	struct gss_cred	*gss_cred = container_of(cred, struct gss_cred,
1674			gc_base);
1675	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1676	int             status = -EIO;
1677
1678	dprintk("RPC: %5u %s\n", task->tk_pid, __func__);
1679	if (ctx->gc_proc != RPC_GSS_PROC_DATA) {
1680		/* The spec seems a little ambiguous here, but I think that not
1681		 * wrapping context destruction requests makes the most sense.
1682		 */
1683		gss_wrap_req_encode(encode, rqstp, p, obj);
1684		status = 0;
1685		goto out;
1686	}
1687	switch (gss_cred->gc_service) {
1688	case RPC_GSS_SVC_NONE:
1689		gss_wrap_req_encode(encode, rqstp, p, obj);
1690		status = 0;
1691		break;
1692	case RPC_GSS_SVC_INTEGRITY:
1693		status = gss_wrap_req_integ(cred, ctx, encode, rqstp, p, obj);
1694		break;
1695	case RPC_GSS_SVC_PRIVACY:
1696		status = gss_wrap_req_priv(cred, ctx, encode, rqstp, p, obj);
1697		break;
1698	}
1699out:
1700	gss_put_ctx(ctx);
1701	dprintk("RPC: %5u %s returning %d\n", task->tk_pid, __func__, status);
1702	return status;
1703}
1704
1705static inline int
1706gss_unwrap_resp_integ(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1707		struct rpc_rqst *rqstp, __be32 **p)
1708{
1709	struct xdr_buf	*rcv_buf = &rqstp->rq_rcv_buf;
1710	struct xdr_buf integ_buf;
1711	struct xdr_netobj mic;
1712	u32 data_offset, mic_offset;
1713	u32 integ_len;
1714	u32 maj_stat;
1715	int status = -EIO;
1716
1717	integ_len = ntohl(*(*p)++);
1718	if (integ_len & 3)
1719		return status;
1720	data_offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
1721	mic_offset = integ_len + data_offset;
1722	if (mic_offset > rcv_buf->len)
1723		return status;
1724	if (ntohl(*(*p)++) != rqstp->rq_seqno)
1725		return status;
1726
1727	if (xdr_buf_subsegment(rcv_buf, &integ_buf, data_offset,
1728				mic_offset - data_offset))
1729		return status;
1730
1731	if (xdr_buf_read_netobj(rcv_buf, &mic, mic_offset))
1732		return status;
1733
1734	maj_stat = gss_verify_mic(ctx->gc_gss_ctx, &integ_buf, &mic);
1735	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1736		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
1737	if (maj_stat != GSS_S_COMPLETE)
1738		return status;
1739	return 0;
1740}
1741
1742static inline int
1743gss_unwrap_resp_priv(struct rpc_cred *cred, struct gss_cl_ctx *ctx,
1744		struct rpc_rqst *rqstp, __be32 **p)
1745{
1746	struct xdr_buf  *rcv_buf = &rqstp->rq_rcv_buf;
1747	u32 offset;
1748	u32 opaque_len;
1749	u32 maj_stat;
1750	int status = -EIO;
1751
1752	opaque_len = ntohl(*(*p)++);
1753	offset = (u8 *)(*p) - (u8 *)rcv_buf->head[0].iov_base;
1754	if (offset + opaque_len > rcv_buf->len)
1755		return status;
1756	/* remove padding: */
1757	rcv_buf->len = offset + opaque_len;
1758
1759	maj_stat = gss_unwrap(ctx->gc_gss_ctx, offset, rcv_buf);
1760	if (maj_stat == GSS_S_CONTEXT_EXPIRED)
1761		clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
1762	if (maj_stat != GSS_S_COMPLETE)
1763		return status;
1764	if (ntohl(*(*p)++) != rqstp->rq_seqno)
1765		return status;
1766
1767	return 0;
1768}
1769
1770static int
1771gss_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
1772		      __be32 *p, void *obj)
1773{
1774	struct xdr_stream xdr;
1775
1776	xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
1777	return decode(rqstp, &xdr, obj);
1778}
1779
1780static int
1781gss_unwrap_resp(struct rpc_task *task,
1782		kxdrdproc_t decode, void *rqstp, __be32 *p, void *obj)
1783{
1784	struct rpc_cred *cred = task->tk_rqstp->rq_cred;
1785	struct gss_cred *gss_cred = container_of(cred, struct gss_cred,
1786			gc_base);
1787	struct gss_cl_ctx *ctx = gss_cred_get_ctx(cred);
1788	__be32		*savedp = p;
1789	struct kvec	*head = ((struct rpc_rqst *)rqstp)->rq_rcv_buf.head;
1790	int		savedlen = head->iov_len;
1791	int             status = -EIO;
1792
1793	if (ctx->gc_proc != RPC_GSS_PROC_DATA)
1794		goto out_decode;
1795	switch (gss_cred->gc_service) {
1796	case RPC_GSS_SVC_NONE:
1797		break;
1798	case RPC_GSS_SVC_INTEGRITY:
1799		status = gss_unwrap_resp_integ(cred, ctx, rqstp, &p);
1800		if (status)
1801			goto out;
1802		break;
1803	case RPC_GSS_SVC_PRIVACY:
1804		status = gss_unwrap_resp_priv(cred, ctx, rqstp, &p);
1805		if (status)
1806			goto out;
1807		break;
1808	}
1809	/* take into account extra slack for integrity and privacy cases: */
1810	cred->cr_auth->au_rslack = cred->cr_auth->au_verfsize + (p - savedp)
1811						+ (savedlen - head->iov_len);
1812out_decode:
1813	status = gss_unwrap_req_decode(decode, rqstp, p, obj);
1814out:
1815	gss_put_ctx(ctx);
1816	dprintk("RPC: %5u %s returning %d\n",
1817		task->tk_pid, __func__, status);
1818	return status;
1819}
1820
1821static const struct rpc_authops authgss_ops = {
1822	.owner		= THIS_MODULE,
1823	.au_flavor	= RPC_AUTH_GSS,
1824	.au_name	= "RPCSEC_GSS",
1825	.create		= gss_create,
1826	.destroy	= gss_destroy,
1827	.lookup_cred	= gss_lookup_cred,
1828	.crcreate	= gss_create_cred,
1829	.list_pseudoflavors = gss_mech_list_pseudoflavors,
1830	.info2flavor	= gss_mech_info2flavor,
1831	.flavor2info	= gss_mech_flavor2info,
1832};
1833
1834static const struct rpc_credops gss_credops = {
1835	.cr_name	= "AUTH_GSS",
1836	.crdestroy	= gss_destroy_cred,
1837	.cr_init	= gss_cred_init,
1838	.crbind		= rpcauth_generic_bind_cred,
1839	.crmatch	= gss_match,
1840	.crmarshal	= gss_marshal,
1841	.crrefresh	= gss_refresh,
1842	.crvalidate	= gss_validate,
1843	.crwrap_req	= gss_wrap_req,
1844	.crunwrap_resp	= gss_unwrap_resp,
1845};
1846
1847static const struct rpc_credops gss_nullops = {
1848	.cr_name	= "AUTH_GSS",
1849	.crdestroy	= gss_destroy_nullcred,
1850	.crbind		= rpcauth_generic_bind_cred,
1851	.crmatch	= gss_match,
1852	.crmarshal	= gss_marshal,
1853	.crrefresh	= gss_refresh_null,
1854	.crvalidate	= gss_validate,
1855	.crwrap_req	= gss_wrap_req,
1856	.crunwrap_resp	= gss_unwrap_resp,
1857};
1858
1859static const struct rpc_pipe_ops gss_upcall_ops_v0 = {
1860	.upcall		= rpc_pipe_generic_upcall,
1861	.downcall	= gss_pipe_downcall,
1862	.destroy_msg	= gss_pipe_destroy_msg,
1863	.open_pipe	= gss_pipe_open_v0,
1864	.release_pipe	= gss_pipe_release,
1865};
1866
1867static const struct rpc_pipe_ops gss_upcall_ops_v1 = {
1868	.upcall		= rpc_pipe_generic_upcall,
1869	.downcall	= gss_pipe_downcall,
1870	.destroy_msg	= gss_pipe_destroy_msg,
1871	.open_pipe	= gss_pipe_open_v1,
1872	.release_pipe	= gss_pipe_release,
1873};
1874
1875static __net_init int rpcsec_gss_init_net(struct net *net)
1876{
1877	return gss_svc_init_net(net);
1878}
1879
1880static __net_exit void rpcsec_gss_exit_net(struct net *net)
1881{
1882	gss_svc_shutdown_net(net);
1883}
1884
1885static struct pernet_operations rpcsec_gss_net_ops = {
1886	.init = rpcsec_gss_init_net,
1887	.exit = rpcsec_gss_exit_net,
1888};
1889
1890/*
1891 * Initialize RPCSEC_GSS module
1892 */
1893static int __init init_rpcsec_gss(void)
1894{
1895	int err = 0;
1896
1897	err = rpcauth_register(&authgss_ops);
1898	if (err)
1899		goto out;
1900	err = gss_svc_init();
1901	if (err)
1902		goto out_unregister;
1903	err = register_pernet_subsys(&rpcsec_gss_net_ops);
1904	if (err)
1905		goto out_svc_exit;
1906	rpc_init_wait_queue(&pipe_version_rpc_waitqueue, "gss pipe version");
1907	return 0;
1908out_svc_exit:
1909	gss_svc_shutdown();
1910out_unregister:
1911	rpcauth_unregister(&authgss_ops);
1912out:
1913	return err;
1914}
1915
1916static void __exit exit_rpcsec_gss(void)
1917{
1918	unregister_pernet_subsys(&rpcsec_gss_net_ops);
1919	gss_svc_shutdown();
1920	rpcauth_unregister(&authgss_ops);
1921	rcu_barrier(); /* Wait for completion of call_rcu()'s */
1922}
1923
1924MODULE_ALIAS("rpc-auth-6");
1925MODULE_LICENSE("GPL");
1926module_param_named(expired_cred_retry_delay,
1927		   gss_expired_cred_retry_delay,
1928		   uint, 0644);
1929MODULE_PARM_DESC(expired_cred_retry_delay, "Timeout (in seconds) until "
1930		"the RPC engine retries an expired credential");
1931
1932module_init(init_rpcsec_gss)
1933module_exit(exit_rpcsec_gss)
1934