1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Neil Brown <neilb@cse.unsw.edu.au>
4  * J. Bruce Fields <bfields@umich.edu>
5  * Andy Adamson <andros@umich.edu>
6  * Dug Song <dugsong@monkey.org>
7  *
8  * RPCSEC_GSS server authentication.
9  * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
10  * (gssapi)
11  *
12  * The RPCSEC_GSS involves three stages:
13  *  1/ context creation
14  *  2/ data exchange
15  *  3/ context destruction
16  *
17  * Context creation is handled largely by upcalls to user-space.
18  *  In particular, GSS_Accept_sec_context is handled by an upcall
19  * Data exchange is handled entirely within the kernel
20  *  In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
21  * Context destruction is handled in-kernel
22  *  GSS_Delete_sec_context is in-kernel
23  *
24  * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
25  * The context handle and gss_token are used as a key into the rpcsec_init cache.
26  * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
27  * being major_status, minor_status, context_handle, reply_token.
28  * These are sent back to the client.
29  * Sequence window management is handled by the kernel.  The window size if currently
30  * a compile time constant.
31  *
32  * When user-space is happy that a context is established, it places an entry
33  * in the rpcsec_context cache. The key for this cache is the context_handle.
34  * The content includes:
35  *   uid/gidlist - for determining access rights
36  *   mechanism type
37  *   mechanism specific information, such as a key
38  *
39  */
40 
41 #include <linux/slab.h>
42 #include <linux/types.h>
43 #include <linux/module.h>
44 #include <linux/pagemap.h>
45 #include <linux/user_namespace.h>
46 
47 #include <linux/sunrpc/auth_gss.h>
48 #include <linux/sunrpc/gss_err.h>
49 #include <linux/sunrpc/svcauth.h>
50 #include <linux/sunrpc/svcauth_gss.h>
51 #include <linux/sunrpc/cache.h>
52 #include <linux/sunrpc/gss_krb5.h>
53 
54 #include <trace/events/rpcgss.h>
55 
56 #include "gss_rpc_upcall.h"
57 
58 /*
59  * Unfortunately there isn't a maximum checksum size exported via the
60  * GSS API. Manufacture one based on GSS mechanisms supported by this
61  * implementation.
62  */
63 #define GSS_MAX_CKSUMSIZE (GSS_KRB5_TOK_HDR_LEN + GSS_KRB5_MAX_CKSUM_LEN)
64 
65 /*
66  * This value may be increased in the future to accommodate other
67  * usage of the scratch buffer.
68  */
69 #define GSS_SCRATCH_SIZE GSS_MAX_CKSUMSIZE
70 
71 struct gss_svc_data {
72 	/* decoded gss client cred: */
73 	struct rpc_gss_wire_cred	clcred;
74 	/* save a pointer to the beginning of the encoded verifier,
75 	 * for use in encryption/checksumming in svcauth_gss_release: */
76 	__be32				*verf_start;
77 	struct rsc			*rsci;
78 
79 	/* for temporary results */
80 	u8				gsd_scratch[GSS_SCRATCH_SIZE];
81 };
82 
83 /* The rpcsec_init cache is used for mapping RPCSEC_GSS_{,CONT_}INIT requests
84  * into replies.
85  *
86  * Key is context handle (\x if empty) and gss_token.
87  * Content is major_status minor_status (integers) context_handle, reply_token.
88  *
89  */
90 
91 static int netobj_equal(struct xdr_netobj *a, struct xdr_netobj *b)
92 {
93 	return a->len == b->len && 0 == memcmp(a->data, b->data, a->len);
94 }
95 
96 #define	RSI_HASHBITS	6
97 #define	RSI_HASHMAX	(1<<RSI_HASHBITS)
98 
99 struct rsi {
100 	struct cache_head	h;
101 	struct xdr_netobj	in_handle, in_token;
102 	struct xdr_netobj	out_handle, out_token;
103 	int			major_status, minor_status;
104 	struct rcu_head		rcu_head;
105 };
106 
107 static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old);
108 static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item);
109 
110 static void rsi_free(struct rsi *rsii)
111 {
112 	kfree(rsii->in_handle.data);
113 	kfree(rsii->in_token.data);
114 	kfree(rsii->out_handle.data);
115 	kfree(rsii->out_token.data);
116 }
117 
118 static void rsi_free_rcu(struct rcu_head *head)
119 {
120 	struct rsi *rsii = container_of(head, struct rsi, rcu_head);
121 
122 	rsi_free(rsii);
123 	kfree(rsii);
124 }
125 
126 static void rsi_put(struct kref *ref)
127 {
128 	struct rsi *rsii = container_of(ref, struct rsi, h.ref);
129 
130 	call_rcu(&rsii->rcu_head, rsi_free_rcu);
131 }
132 
133 static inline int rsi_hash(struct rsi *item)
134 {
135 	return hash_mem(item->in_handle.data, item->in_handle.len, RSI_HASHBITS)
136 	     ^ hash_mem(item->in_token.data, item->in_token.len, RSI_HASHBITS);
137 }
138 
139 static int rsi_match(struct cache_head *a, struct cache_head *b)
140 {
141 	struct rsi *item = container_of(a, struct rsi, h);
142 	struct rsi *tmp = container_of(b, struct rsi, h);
143 	return netobj_equal(&item->in_handle, &tmp->in_handle) &&
144 	       netobj_equal(&item->in_token, &tmp->in_token);
145 }
146 
147 static int dup_to_netobj(struct xdr_netobj *dst, char *src, int len)
148 {
149 	dst->len = len;
150 	dst->data = (len ? kmemdup(src, len, GFP_KERNEL) : NULL);
151 	if (len && !dst->data)
152 		return -ENOMEM;
153 	return 0;
154 }
155 
156 static inline int dup_netobj(struct xdr_netobj *dst, struct xdr_netobj *src)
157 {
158 	return dup_to_netobj(dst, src->data, src->len);
159 }
160 
161 static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
162 {
163 	struct rsi *new = container_of(cnew, struct rsi, h);
164 	struct rsi *item = container_of(citem, struct rsi, h);
165 
166 	new->out_handle.data = NULL;
167 	new->out_handle.len = 0;
168 	new->out_token.data = NULL;
169 	new->out_token.len = 0;
170 	new->in_handle.len = item->in_handle.len;
171 	item->in_handle.len = 0;
172 	new->in_token.len = item->in_token.len;
173 	item->in_token.len = 0;
174 	new->in_handle.data = item->in_handle.data;
175 	item->in_handle.data = NULL;
176 	new->in_token.data = item->in_token.data;
177 	item->in_token.data = NULL;
178 }
179 
180 static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
181 {
182 	struct rsi *new = container_of(cnew, struct rsi, h);
183 	struct rsi *item = container_of(citem, struct rsi, h);
184 
185 	BUG_ON(new->out_handle.data || new->out_token.data);
186 	new->out_handle.len = item->out_handle.len;
187 	item->out_handle.len = 0;
188 	new->out_token.len = item->out_token.len;
189 	item->out_token.len = 0;
190 	new->out_handle.data = item->out_handle.data;
191 	item->out_handle.data = NULL;
192 	new->out_token.data = item->out_token.data;
193 	item->out_token.data = NULL;
194 
195 	new->major_status = item->major_status;
196 	new->minor_status = item->minor_status;
197 }
198 
199 static struct cache_head *rsi_alloc(void)
200 {
201 	struct rsi *rsii = kmalloc(sizeof(*rsii), GFP_KERNEL);
202 	if (rsii)
203 		return &rsii->h;
204 	else
205 		return NULL;
206 }
207 
208 static int rsi_upcall(struct cache_detail *cd, struct cache_head *h)
209 {
210 	return sunrpc_cache_pipe_upcall_timeout(cd, h);
211 }
212 
213 static void rsi_request(struct cache_detail *cd,
214 		       struct cache_head *h,
215 		       char **bpp, int *blen)
216 {
217 	struct rsi *rsii = container_of(h, struct rsi, h);
218 
219 	qword_addhex(bpp, blen, rsii->in_handle.data, rsii->in_handle.len);
220 	qword_addhex(bpp, blen, rsii->in_token.data, rsii->in_token.len);
221 	(*bpp)[-1] = '\n';
222 	WARN_ONCE(*blen < 0,
223 		  "RPCSEC/GSS credential too large - please use gssproxy\n");
224 }
225 
226 static int rsi_parse(struct cache_detail *cd,
227 		    char *mesg, int mlen)
228 {
229 	/* context token expiry major minor context token */
230 	char *buf = mesg;
231 	char *ep;
232 	int len;
233 	struct rsi rsii, *rsip = NULL;
234 	time64_t expiry;
235 	int status = -EINVAL;
236 
237 	memset(&rsii, 0, sizeof(rsii));
238 	/* handle */
239 	len = qword_get(&mesg, buf, mlen);
240 	if (len < 0)
241 		goto out;
242 	status = -ENOMEM;
243 	if (dup_to_netobj(&rsii.in_handle, buf, len))
244 		goto out;
245 
246 	/* token */
247 	len = qword_get(&mesg, buf, mlen);
248 	status = -EINVAL;
249 	if (len < 0)
250 		goto out;
251 	status = -ENOMEM;
252 	if (dup_to_netobj(&rsii.in_token, buf, len))
253 		goto out;
254 
255 	rsip = rsi_lookup(cd, &rsii);
256 	if (!rsip)
257 		goto out;
258 
259 	rsii.h.flags = 0;
260 	/* expiry */
261 	expiry = get_expiry(&mesg);
262 	status = -EINVAL;
263 	if (expiry == 0)
264 		goto out;
265 
266 	/* major/minor */
267 	len = qword_get(&mesg, buf, mlen);
268 	if (len <= 0)
269 		goto out;
270 	rsii.major_status = simple_strtoul(buf, &ep, 10);
271 	if (*ep)
272 		goto out;
273 	len = qword_get(&mesg, buf, mlen);
274 	if (len <= 0)
275 		goto out;
276 	rsii.minor_status = simple_strtoul(buf, &ep, 10);
277 	if (*ep)
278 		goto out;
279 
280 	/* out_handle */
281 	len = qword_get(&mesg, buf, mlen);
282 	if (len < 0)
283 		goto out;
284 	status = -ENOMEM;
285 	if (dup_to_netobj(&rsii.out_handle, buf, len))
286 		goto out;
287 
288 	/* out_token */
289 	len = qword_get(&mesg, buf, mlen);
290 	status = -EINVAL;
291 	if (len < 0)
292 		goto out;
293 	status = -ENOMEM;
294 	if (dup_to_netobj(&rsii.out_token, buf, len))
295 		goto out;
296 	rsii.h.expiry_time = expiry;
297 	rsip = rsi_update(cd, &rsii, rsip);
298 	status = 0;
299 out:
300 	rsi_free(&rsii);
301 	if (rsip)
302 		cache_put(&rsip->h, cd);
303 	else
304 		status = -ENOMEM;
305 	return status;
306 }
307 
308 static const struct cache_detail rsi_cache_template = {
309 	.owner		= THIS_MODULE,
310 	.hash_size	= RSI_HASHMAX,
311 	.name           = "auth.rpcsec.init",
312 	.cache_put      = rsi_put,
313 	.cache_upcall	= rsi_upcall,
314 	.cache_request  = rsi_request,
315 	.cache_parse    = rsi_parse,
316 	.match		= rsi_match,
317 	.init		= rsi_init,
318 	.update		= update_rsi,
319 	.alloc		= rsi_alloc,
320 };
321 
322 static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item)
323 {
324 	struct cache_head *ch;
325 	int hash = rsi_hash(item);
326 
327 	ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
328 	if (ch)
329 		return container_of(ch, struct rsi, h);
330 	else
331 		return NULL;
332 }
333 
334 static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old)
335 {
336 	struct cache_head *ch;
337 	int hash = rsi_hash(new);
338 
339 	ch = sunrpc_cache_update(cd, &new->h,
340 				 &old->h, hash);
341 	if (ch)
342 		return container_of(ch, struct rsi, h);
343 	else
344 		return NULL;
345 }
346 
347 
348 /*
349  * The rpcsec_context cache is used to store a context that is
350  * used in data exchange.
351  * The key is a context handle. The content is:
352  *  uid, gidlist, mechanism, service-set, mech-specific-data
353  */
354 
355 #define	RSC_HASHBITS	10
356 #define	RSC_HASHMAX	(1<<RSC_HASHBITS)
357 
358 #define GSS_SEQ_WIN	128
359 
360 struct gss_svc_seq_data {
361 	/* highest seq number seen so far: */
362 	u32			sd_max;
363 	/* for i such that sd_max-GSS_SEQ_WIN < i <= sd_max, the i-th bit of
364 	 * sd_win is nonzero iff sequence number i has been seen already: */
365 	unsigned long		sd_win[GSS_SEQ_WIN/BITS_PER_LONG];
366 	spinlock_t		sd_lock;
367 };
368 
369 struct rsc {
370 	struct cache_head	h;
371 	struct xdr_netobj	handle;
372 	struct svc_cred		cred;
373 	struct gss_svc_seq_data	seqdata;
374 	struct gss_ctx		*mechctx;
375 	struct rcu_head		rcu_head;
376 };
377 
378 static struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old);
379 static struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item);
380 
381 static void rsc_free(struct rsc *rsci)
382 {
383 	kfree(rsci->handle.data);
384 	if (rsci->mechctx)
385 		gss_delete_sec_context(&rsci->mechctx);
386 	free_svc_cred(&rsci->cred);
387 }
388 
389 static void rsc_free_rcu(struct rcu_head *head)
390 {
391 	struct rsc *rsci = container_of(head, struct rsc, rcu_head);
392 
393 	kfree(rsci->handle.data);
394 	kfree(rsci);
395 }
396 
397 static void rsc_put(struct kref *ref)
398 {
399 	struct rsc *rsci = container_of(ref, struct rsc, h.ref);
400 
401 	if (rsci->mechctx)
402 		gss_delete_sec_context(&rsci->mechctx);
403 	free_svc_cred(&rsci->cred);
404 	call_rcu(&rsci->rcu_head, rsc_free_rcu);
405 }
406 
407 static inline int
408 rsc_hash(struct rsc *rsci)
409 {
410 	return hash_mem(rsci->handle.data, rsci->handle.len, RSC_HASHBITS);
411 }
412 
413 static int
414 rsc_match(struct cache_head *a, struct cache_head *b)
415 {
416 	struct rsc *new = container_of(a, struct rsc, h);
417 	struct rsc *tmp = container_of(b, struct rsc, h);
418 
419 	return netobj_equal(&new->handle, &tmp->handle);
420 }
421 
422 static void
423 rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
424 {
425 	struct rsc *new = container_of(cnew, struct rsc, h);
426 	struct rsc *tmp = container_of(ctmp, struct rsc, h);
427 
428 	new->handle.len = tmp->handle.len;
429 	tmp->handle.len = 0;
430 	new->handle.data = tmp->handle.data;
431 	tmp->handle.data = NULL;
432 	new->mechctx = NULL;
433 	init_svc_cred(&new->cred);
434 }
435 
436 static void
437 update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
438 {
439 	struct rsc *new = container_of(cnew, struct rsc, h);
440 	struct rsc *tmp = container_of(ctmp, struct rsc, h);
441 
442 	new->mechctx = tmp->mechctx;
443 	tmp->mechctx = NULL;
444 	memset(&new->seqdata, 0, sizeof(new->seqdata));
445 	spin_lock_init(&new->seqdata.sd_lock);
446 	new->cred = tmp->cred;
447 	init_svc_cred(&tmp->cred);
448 }
449 
450 static struct cache_head *
451 rsc_alloc(void)
452 {
453 	struct rsc *rsci = kmalloc(sizeof(*rsci), GFP_KERNEL);
454 	if (rsci)
455 		return &rsci->h;
456 	else
457 		return NULL;
458 }
459 
460 static int rsc_upcall(struct cache_detail *cd, struct cache_head *h)
461 {
462 	return -EINVAL;
463 }
464 
465 static int rsc_parse(struct cache_detail *cd,
466 		     char *mesg, int mlen)
467 {
468 	/* contexthandle expiry [ uid gid N <n gids> mechname ...mechdata... ] */
469 	char *buf = mesg;
470 	int id;
471 	int len, rv;
472 	struct rsc rsci, *rscp = NULL;
473 	time64_t expiry;
474 	int status = -EINVAL;
475 	struct gss_api_mech *gm = NULL;
476 
477 	memset(&rsci, 0, sizeof(rsci));
478 	/* context handle */
479 	len = qword_get(&mesg, buf, mlen);
480 	if (len < 0) goto out;
481 	status = -ENOMEM;
482 	if (dup_to_netobj(&rsci.handle, buf, len))
483 		goto out;
484 
485 	rsci.h.flags = 0;
486 	/* expiry */
487 	expiry = get_expiry(&mesg);
488 	status = -EINVAL;
489 	if (expiry == 0)
490 		goto out;
491 
492 	rscp = rsc_lookup(cd, &rsci);
493 	if (!rscp)
494 		goto out;
495 
496 	/* uid, or NEGATIVE */
497 	rv = get_int(&mesg, &id);
498 	if (rv == -EINVAL)
499 		goto out;
500 	if (rv == -ENOENT)
501 		set_bit(CACHE_NEGATIVE, &rsci.h.flags);
502 	else {
503 		int N, i;
504 
505 		/*
506 		 * NOTE: we skip uid_valid()/gid_valid() checks here:
507 		 * instead, * -1 id's are later mapped to the
508 		 * (export-specific) anonymous id by nfsd_setuser.
509 		 *
510 		 * (But supplementary gid's get no such special
511 		 * treatment so are checked for validity here.)
512 		 */
513 		/* uid */
514 		rsci.cred.cr_uid = make_kuid(current_user_ns(), id);
515 
516 		/* gid */
517 		if (get_int(&mesg, &id))
518 			goto out;
519 		rsci.cred.cr_gid = make_kgid(current_user_ns(), id);
520 
521 		/* number of additional gid's */
522 		if (get_int(&mesg, &N))
523 			goto out;
524 		if (N < 0 || N > NGROUPS_MAX)
525 			goto out;
526 		status = -ENOMEM;
527 		rsci.cred.cr_group_info = groups_alloc(N);
528 		if (rsci.cred.cr_group_info == NULL)
529 			goto out;
530 
531 		/* gid's */
532 		status = -EINVAL;
533 		for (i=0; i<N; i++) {
534 			kgid_t kgid;
535 			if (get_int(&mesg, &id))
536 				goto out;
537 			kgid = make_kgid(current_user_ns(), id);
538 			if (!gid_valid(kgid))
539 				goto out;
540 			rsci.cred.cr_group_info->gid[i] = kgid;
541 		}
542 		groups_sort(rsci.cred.cr_group_info);
543 
544 		/* mech name */
545 		len = qword_get(&mesg, buf, mlen);
546 		if (len < 0)
547 			goto out;
548 		gm = rsci.cred.cr_gss_mech = gss_mech_get_by_name(buf);
549 		status = -EOPNOTSUPP;
550 		if (!gm)
551 			goto out;
552 
553 		status = -EINVAL;
554 		/* mech-specific data: */
555 		len = qword_get(&mesg, buf, mlen);
556 		if (len < 0)
557 			goto out;
558 		status = gss_import_sec_context(buf, len, gm, &rsci.mechctx,
559 						NULL, GFP_KERNEL);
560 		if (status)
561 			goto out;
562 
563 		/* get client name */
564 		len = qword_get(&mesg, buf, mlen);
565 		if (len > 0) {
566 			rsci.cred.cr_principal = kstrdup(buf, GFP_KERNEL);
567 			if (!rsci.cred.cr_principal) {
568 				status = -ENOMEM;
569 				goto out;
570 			}
571 		}
572 
573 	}
574 	rsci.h.expiry_time = expiry;
575 	rscp = rsc_update(cd, &rsci, rscp);
576 	status = 0;
577 out:
578 	rsc_free(&rsci);
579 	if (rscp)
580 		cache_put(&rscp->h, cd);
581 	else
582 		status = -ENOMEM;
583 	return status;
584 }
585 
586 static const struct cache_detail rsc_cache_template = {
587 	.owner		= THIS_MODULE,
588 	.hash_size	= RSC_HASHMAX,
589 	.name		= "auth.rpcsec.context",
590 	.cache_put	= rsc_put,
591 	.cache_upcall	= rsc_upcall,
592 	.cache_parse	= rsc_parse,
593 	.match		= rsc_match,
594 	.init		= rsc_init,
595 	.update		= update_rsc,
596 	.alloc		= rsc_alloc,
597 };
598 
599 static struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item)
600 {
601 	struct cache_head *ch;
602 	int hash = rsc_hash(item);
603 
604 	ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
605 	if (ch)
606 		return container_of(ch, struct rsc, h);
607 	else
608 		return NULL;
609 }
610 
611 static struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old)
612 {
613 	struct cache_head *ch;
614 	int hash = rsc_hash(new);
615 
616 	ch = sunrpc_cache_update(cd, &new->h,
617 				 &old->h, hash);
618 	if (ch)
619 		return container_of(ch, struct rsc, h);
620 	else
621 		return NULL;
622 }
623 
624 
625 static struct rsc *
626 gss_svc_searchbyctx(struct cache_detail *cd, struct xdr_netobj *handle)
627 {
628 	struct rsc rsci;
629 	struct rsc *found;
630 
631 	memset(&rsci, 0, sizeof(rsci));
632 	if (dup_to_netobj(&rsci.handle, handle->data, handle->len))
633 		return NULL;
634 	found = rsc_lookup(cd, &rsci);
635 	rsc_free(&rsci);
636 	if (!found)
637 		return NULL;
638 	if (cache_check(cd, &found->h, NULL))
639 		return NULL;
640 	return found;
641 }
642 
643 /**
644  * gss_check_seq_num - GSS sequence number window check
645  * @rqstp: RPC Call to use when reporting errors
646  * @rsci: cached GSS context state (updated on return)
647  * @seq_num: sequence number to check
648  *
649  * Implements sequence number algorithm as specified in
650  * RFC 2203, Section 5.3.3.1. "Context Management".
651  *
652  * Return values:
653  *   %true: @rqstp's GSS sequence number is inside the window
654  *   %false: @rqstp's GSS sequence number is outside the window
655  */
656 static bool gss_check_seq_num(const struct svc_rqst *rqstp, struct rsc *rsci,
657 			      u32 seq_num)
658 {
659 	struct gss_svc_seq_data *sd = &rsci->seqdata;
660 	bool result = false;
661 
662 	spin_lock(&sd->sd_lock);
663 	if (seq_num > sd->sd_max) {
664 		if (seq_num >= sd->sd_max + GSS_SEQ_WIN) {
665 			memset(sd->sd_win, 0, sizeof(sd->sd_win));
666 			sd->sd_max = seq_num;
667 		} else while (sd->sd_max < seq_num) {
668 			sd->sd_max++;
669 			__clear_bit(sd->sd_max % GSS_SEQ_WIN, sd->sd_win);
670 		}
671 		__set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win);
672 		goto ok;
673 	} else if (seq_num + GSS_SEQ_WIN <= sd->sd_max) {
674 		goto toolow;
675 	}
676 	if (__test_and_set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win))
677 		goto alreadyseen;
678 
679 ok:
680 	result = true;
681 out:
682 	spin_unlock(&sd->sd_lock);
683 	return result;
684 
685 toolow:
686 	trace_rpcgss_svc_seqno_low(rqstp, seq_num,
687 				   sd->sd_max - GSS_SEQ_WIN,
688 				   sd->sd_max);
689 	goto out;
690 alreadyseen:
691 	trace_rpcgss_svc_seqno_seen(rqstp, seq_num);
692 	goto out;
693 }
694 
695 static inline u32 round_up_to_quad(u32 i)
696 {
697 	return (i + 3 ) & ~3;
698 }
699 
700 static inline int
701 svc_safe_getnetobj(struct kvec *argv, struct xdr_netobj *o)
702 {
703 	int l;
704 
705 	if (argv->iov_len < 4)
706 		return -1;
707 	o->len = svc_getnl(argv);
708 	l = round_up_to_quad(o->len);
709 	if (argv->iov_len < l)
710 		return -1;
711 	o->data = argv->iov_base;
712 	argv->iov_base += l;
713 	argv->iov_len -= l;
714 	return 0;
715 }
716 
717 static inline int
718 svc_safe_putnetobj(struct kvec *resv, struct xdr_netobj *o)
719 {
720 	u8 *p;
721 
722 	if (resv->iov_len + 4 > PAGE_SIZE)
723 		return -1;
724 	svc_putnl(resv, o->len);
725 	p = resv->iov_base + resv->iov_len;
726 	resv->iov_len += round_up_to_quad(o->len);
727 	if (resv->iov_len > PAGE_SIZE)
728 		return -1;
729 	memcpy(p, o->data, o->len);
730 	memset(p + o->len, 0, round_up_to_quad(o->len) - o->len);
731 	return 0;
732 }
733 
734 /*
735  * Verify the checksum on the header and return SVC_OK on success.
736  * Otherwise, return SVC_DROP (in the case of a bad sequence number)
737  * or return SVC_DENIED and indicate error in rqstp->rq_auth_stat.
738  */
739 static int
740 gss_verify_header(struct svc_rqst *rqstp, struct rsc *rsci,
741 		  __be32 *rpcstart, struct rpc_gss_wire_cred *gc)
742 {
743 	struct gss_ctx		*ctx_id = rsci->mechctx;
744 	struct xdr_buf		rpchdr;
745 	struct xdr_netobj	checksum;
746 	u32			flavor = 0;
747 	struct kvec		*argv = &rqstp->rq_arg.head[0];
748 	struct kvec		iov;
749 
750 	/* data to compute the checksum over: */
751 	iov.iov_base = rpcstart;
752 	iov.iov_len = (u8 *)argv->iov_base - (u8 *)rpcstart;
753 	xdr_buf_from_iov(&iov, &rpchdr);
754 
755 	rqstp->rq_auth_stat = rpc_autherr_badverf;
756 	if (argv->iov_len < 4)
757 		return SVC_DENIED;
758 	flavor = svc_getnl(argv);
759 	if (flavor != RPC_AUTH_GSS)
760 		return SVC_DENIED;
761 	if (svc_safe_getnetobj(argv, &checksum))
762 		return SVC_DENIED;
763 
764 	if (rqstp->rq_deferred) /* skip verification of revisited request */
765 		return SVC_OK;
766 	if (gss_verify_mic(ctx_id, &rpchdr, &checksum) != GSS_S_COMPLETE) {
767 		rqstp->rq_auth_stat = rpcsec_gsserr_credproblem;
768 		return SVC_DENIED;
769 	}
770 
771 	if (gc->gc_seq > MAXSEQ) {
772 		trace_rpcgss_svc_seqno_large(rqstp, gc->gc_seq);
773 		rqstp->rq_auth_stat = rpcsec_gsserr_ctxproblem;
774 		return SVC_DENIED;
775 	}
776 	if (!gss_check_seq_num(rqstp, rsci, gc->gc_seq))
777 		return SVC_DROP;
778 	return SVC_OK;
779 }
780 
781 static int
782 gss_write_null_verf(struct svc_rqst *rqstp)
783 {
784 	__be32     *p;
785 
786 	svc_putnl(rqstp->rq_res.head, RPC_AUTH_NULL);
787 	p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
788 	/* don't really need to check if head->iov_len > PAGE_SIZE ... */
789 	*p++ = 0;
790 	if (!xdr_ressize_check(rqstp, p))
791 		return -1;
792 	return 0;
793 }
794 
795 static int
796 gss_write_verf(struct svc_rqst *rqstp, struct gss_ctx *ctx_id, u32 seq)
797 {
798 	__be32			*xdr_seq;
799 	u32			maj_stat;
800 	struct xdr_buf		verf_data;
801 	struct xdr_netobj	mic;
802 	__be32			*p;
803 	struct kvec		iov;
804 	int err = -1;
805 
806 	svc_putnl(rqstp->rq_res.head, RPC_AUTH_GSS);
807 	xdr_seq = kmalloc(4, GFP_KERNEL);
808 	if (!xdr_seq)
809 		return -ENOMEM;
810 	*xdr_seq = htonl(seq);
811 
812 	iov.iov_base = xdr_seq;
813 	iov.iov_len = 4;
814 	xdr_buf_from_iov(&iov, &verf_data);
815 	p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
816 	mic.data = (u8 *)(p + 1);
817 	maj_stat = gss_get_mic(ctx_id, &verf_data, &mic);
818 	if (maj_stat != GSS_S_COMPLETE)
819 		goto out;
820 	*p++ = htonl(mic.len);
821 	memset((u8 *)p + mic.len, 0, round_up_to_quad(mic.len) - mic.len);
822 	p += XDR_QUADLEN(mic.len);
823 	if (!xdr_ressize_check(rqstp, p))
824 		goto out;
825 	err = 0;
826 out:
827 	kfree(xdr_seq);
828 	return err;
829 }
830 
831 struct gss_domain {
832 	struct auth_domain	h;
833 	u32			pseudoflavor;
834 };
835 
836 static struct auth_domain *
837 find_gss_auth_domain(struct gss_ctx *ctx, u32 svc)
838 {
839 	char *name;
840 
841 	name = gss_service_to_auth_domain_name(ctx->mech_type, svc);
842 	if (!name)
843 		return NULL;
844 	return auth_domain_find(name);
845 }
846 
847 static struct auth_ops svcauthops_gss;
848 
849 u32 svcauth_gss_flavor(struct auth_domain *dom)
850 {
851 	struct gss_domain *gd = container_of(dom, struct gss_domain, h);
852 
853 	return gd->pseudoflavor;
854 }
855 
856 EXPORT_SYMBOL_GPL(svcauth_gss_flavor);
857 
858 struct auth_domain *
859 svcauth_gss_register_pseudoflavor(u32 pseudoflavor, char * name)
860 {
861 	struct gss_domain	*new;
862 	struct auth_domain	*test;
863 	int			stat = -ENOMEM;
864 
865 	new = kmalloc(sizeof(*new), GFP_KERNEL);
866 	if (!new)
867 		goto out;
868 	kref_init(&new->h.ref);
869 	new->h.name = kstrdup(name, GFP_KERNEL);
870 	if (!new->h.name)
871 		goto out_free_dom;
872 	new->h.flavour = &svcauthops_gss;
873 	new->pseudoflavor = pseudoflavor;
874 
875 	test = auth_domain_lookup(name, &new->h);
876 	if (test != &new->h) {
877 		pr_warn("svc: duplicate registration of gss pseudo flavour %s.\n",
878 			name);
879 		stat = -EADDRINUSE;
880 		auth_domain_put(test);
881 		goto out_free_name;
882 	}
883 	return test;
884 
885 out_free_name:
886 	kfree(new->h.name);
887 out_free_dom:
888 	kfree(new);
889 out:
890 	return ERR_PTR(stat);
891 }
892 EXPORT_SYMBOL_GPL(svcauth_gss_register_pseudoflavor);
893 
894 static inline int
895 read_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj)
896 {
897 	__be32  raw;
898 	int     status;
899 
900 	status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
901 	if (status)
902 		return status;
903 	*obj = ntohl(raw);
904 	return 0;
905 }
906 
907 /* It would be nice if this bit of code could be shared with the client.
908  * Obstacles:
909  *	The client shouldn't malloc(), would have to pass in own memory.
910  *	The server uses base of head iovec as read pointer, while the
911  *	client uses separate pointer. */
912 static int
913 unwrap_integ_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
914 {
915 	struct gss_svc_data *gsd = rqstp->rq_auth_data;
916 	u32 integ_len, rseqno, maj_stat;
917 	struct xdr_netobj mic;
918 	struct xdr_buf integ_buf;
919 
920 	/* NFS READ normally uses splice to send data in-place. However
921 	 * the data in cache can change after the reply's MIC is computed
922 	 * but before the RPC reply is sent. To prevent the client from
923 	 * rejecting the server-computed MIC in this somewhat rare case,
924 	 * do not use splice with the GSS integrity service.
925 	 */
926 	clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
927 
928 	/* Did we already verify the signature on the original pass through? */
929 	if (rqstp->rq_deferred)
930 		return 0;
931 
932 	integ_len = svc_getnl(&buf->head[0]);
933 	if (integ_len & 3)
934 		goto unwrap_failed;
935 	if (integ_len > buf->len)
936 		goto unwrap_failed;
937 	if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))
938 		goto unwrap_failed;
939 
940 	/* copy out mic... */
941 	if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
942 		goto unwrap_failed;
943 	if (mic.len > sizeof(gsd->gsd_scratch))
944 		goto unwrap_failed;
945 	mic.data = gsd->gsd_scratch;
946 	if (read_bytes_from_xdr_buf(buf, integ_len + 4, mic.data, mic.len))
947 		goto unwrap_failed;
948 	maj_stat = gss_verify_mic(ctx, &integ_buf, &mic);
949 	if (maj_stat != GSS_S_COMPLETE)
950 		goto bad_mic;
951 	rseqno = svc_getnl(&buf->head[0]);
952 	if (rseqno != seq)
953 		goto bad_seqno;
954 	/* trim off the mic and padding at the end before returning */
955 	xdr_buf_trim(buf, round_up_to_quad(mic.len) + 4);
956 	return 0;
957 
958 unwrap_failed:
959 	trace_rpcgss_svc_unwrap_failed(rqstp);
960 	return -EINVAL;
961 bad_seqno:
962 	trace_rpcgss_svc_seqno_bad(rqstp, seq, rseqno);
963 	return -EINVAL;
964 bad_mic:
965 	trace_rpcgss_svc_mic(rqstp, maj_stat);
966 	return -EINVAL;
967 }
968 
969 static inline int
970 total_buf_len(struct xdr_buf *buf)
971 {
972 	return buf->head[0].iov_len + buf->page_len + buf->tail[0].iov_len;
973 }
974 
975 static void
976 fix_priv_head(struct xdr_buf *buf, int pad)
977 {
978 	if (buf->page_len == 0) {
979 		/* We need to adjust head and buf->len in tandem in this
980 		 * case to make svc_defer() work--it finds the original
981 		 * buffer start using buf->len - buf->head[0].iov_len. */
982 		buf->head[0].iov_len -= pad;
983 	}
984 }
985 
986 static int
987 unwrap_priv_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
988 {
989 	u32 priv_len, maj_stat;
990 	int pad, remaining_len, offset;
991 	u32 rseqno;
992 
993 	clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
994 
995 	priv_len = svc_getnl(&buf->head[0]);
996 	if (rqstp->rq_deferred) {
997 		/* Already decrypted last time through! The sequence number
998 		 * check at out_seq is unnecessary but harmless: */
999 		goto out_seq;
1000 	}
1001 	/* buf->len is the number of bytes from the original start of the
1002 	 * request to the end, where head[0].iov_len is just the bytes
1003 	 * not yet read from the head, so these two values are different: */
1004 	remaining_len = total_buf_len(buf);
1005 	if (priv_len > remaining_len)
1006 		goto unwrap_failed;
1007 	pad = remaining_len - priv_len;
1008 	buf->len -= pad;
1009 	fix_priv_head(buf, pad);
1010 
1011 	maj_stat = gss_unwrap(ctx, 0, priv_len, buf);
1012 	pad = priv_len - buf->len;
1013 	/* The upper layers assume the buffer is aligned on 4-byte boundaries.
1014 	 * In the krb5p case, at least, the data ends up offset, so we need to
1015 	 * move it around. */
1016 	/* XXX: This is very inefficient.  It would be better to either do
1017 	 * this while we encrypt, or maybe in the receive code, if we can peak
1018 	 * ahead and work out the service and mechanism there. */
1019 	offset = xdr_pad_size(buf->head[0].iov_len);
1020 	if (offset) {
1021 		buf->buflen = RPCSVC_MAXPAYLOAD;
1022 		xdr_shift_buf(buf, offset);
1023 		fix_priv_head(buf, pad);
1024 	}
1025 	if (maj_stat != GSS_S_COMPLETE)
1026 		goto bad_unwrap;
1027 out_seq:
1028 	rseqno = svc_getnl(&buf->head[0]);
1029 	if (rseqno != seq)
1030 		goto bad_seqno;
1031 	return 0;
1032 
1033 unwrap_failed:
1034 	trace_rpcgss_svc_unwrap_failed(rqstp);
1035 	return -EINVAL;
1036 bad_seqno:
1037 	trace_rpcgss_svc_seqno_bad(rqstp, seq, rseqno);
1038 	return -EINVAL;
1039 bad_unwrap:
1040 	trace_rpcgss_svc_unwrap(rqstp, maj_stat);
1041 	return -EINVAL;
1042 }
1043 
1044 static int
1045 svcauth_gss_set_client(struct svc_rqst *rqstp)
1046 {
1047 	struct gss_svc_data *svcdata = rqstp->rq_auth_data;
1048 	struct rsc *rsci = svcdata->rsci;
1049 	struct rpc_gss_wire_cred *gc = &svcdata->clcred;
1050 	int stat;
1051 
1052 	rqstp->rq_auth_stat = rpc_autherr_badcred;
1053 
1054 	/*
1055 	 * A gss export can be specified either by:
1056 	 * 	export	*(sec=krb5,rw)
1057 	 * or by
1058 	 * 	export gss/krb5(rw)
1059 	 * The latter is deprecated; but for backwards compatibility reasons
1060 	 * the nfsd code will still fall back on trying it if the former
1061 	 * doesn't work; so we try to make both available to nfsd, below.
1062 	 */
1063 	rqstp->rq_gssclient = find_gss_auth_domain(rsci->mechctx, gc->gc_svc);
1064 	if (rqstp->rq_gssclient == NULL)
1065 		return SVC_DENIED;
1066 	stat = svcauth_unix_set_client(rqstp);
1067 	if (stat == SVC_DROP || stat == SVC_CLOSE)
1068 		return stat;
1069 
1070 	rqstp->rq_auth_stat = rpc_auth_ok;
1071 	return SVC_OK;
1072 }
1073 
1074 static inline int
1075 gss_write_init_verf(struct cache_detail *cd, struct svc_rqst *rqstp,
1076 		struct xdr_netobj *out_handle, int *major_status)
1077 {
1078 	struct rsc *rsci;
1079 	int        rc;
1080 
1081 	if (*major_status != GSS_S_COMPLETE)
1082 		return gss_write_null_verf(rqstp);
1083 	rsci = gss_svc_searchbyctx(cd, out_handle);
1084 	if (rsci == NULL) {
1085 		*major_status = GSS_S_NO_CONTEXT;
1086 		return gss_write_null_verf(rqstp);
1087 	}
1088 	rc = gss_write_verf(rqstp, rsci->mechctx, GSS_SEQ_WIN);
1089 	cache_put(&rsci->h, cd);
1090 	return rc;
1091 }
1092 
1093 static inline int
1094 gss_read_common_verf(struct rpc_gss_wire_cred *gc,
1095 		     struct kvec *argv, __be32 *authp,
1096 		     struct xdr_netobj *in_handle)
1097 {
1098 	/* Read the verifier; should be NULL: */
1099 	*authp = rpc_autherr_badverf;
1100 	if (argv->iov_len < 2 * 4)
1101 		return SVC_DENIED;
1102 	if (svc_getnl(argv) != RPC_AUTH_NULL)
1103 		return SVC_DENIED;
1104 	if (svc_getnl(argv) != 0)
1105 		return SVC_DENIED;
1106 	/* Martial context handle and token for upcall: */
1107 	*authp = rpc_autherr_badcred;
1108 	if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0)
1109 		return SVC_DENIED;
1110 	if (dup_netobj(in_handle, &gc->gc_ctx))
1111 		return SVC_CLOSE;
1112 	*authp = rpc_autherr_badverf;
1113 
1114 	return 0;
1115 }
1116 
1117 static inline int
1118 gss_read_verf(struct rpc_gss_wire_cred *gc,
1119 	      struct kvec *argv, __be32 *authp,
1120 	      struct xdr_netobj *in_handle,
1121 	      struct xdr_netobj *in_token)
1122 {
1123 	struct xdr_netobj tmpobj;
1124 	int res;
1125 
1126 	res = gss_read_common_verf(gc, argv, authp, in_handle);
1127 	if (res)
1128 		return res;
1129 
1130 	if (svc_safe_getnetobj(argv, &tmpobj)) {
1131 		kfree(in_handle->data);
1132 		return SVC_DENIED;
1133 	}
1134 	if (dup_netobj(in_token, &tmpobj)) {
1135 		kfree(in_handle->data);
1136 		return SVC_CLOSE;
1137 	}
1138 
1139 	return 0;
1140 }
1141 
1142 static void gss_free_in_token_pages(struct gssp_in_token *in_token)
1143 {
1144 	u32 inlen;
1145 	int i;
1146 
1147 	i = 0;
1148 	inlen = in_token->page_len;
1149 	while (inlen) {
1150 		if (in_token->pages[i])
1151 			put_page(in_token->pages[i]);
1152 		inlen -= inlen > PAGE_SIZE ? PAGE_SIZE : inlen;
1153 	}
1154 
1155 	kfree(in_token->pages);
1156 	in_token->pages = NULL;
1157 }
1158 
1159 static int gss_read_proxy_verf(struct svc_rqst *rqstp,
1160 			       struct rpc_gss_wire_cred *gc,
1161 			       struct xdr_netobj *in_handle,
1162 			       struct gssp_in_token *in_token)
1163 {
1164 	struct kvec *argv = &rqstp->rq_arg.head[0];
1165 	unsigned int length, pgto_offs, pgfrom_offs;
1166 	int pages, i, res, pgto, pgfrom;
1167 	size_t inlen, to_offs, from_offs;
1168 
1169 	res = gss_read_common_verf(gc, argv, &rqstp->rq_auth_stat, in_handle);
1170 	if (res)
1171 		return res;
1172 
1173 	inlen = svc_getnl(argv);
1174 	if (inlen > (argv->iov_len + rqstp->rq_arg.page_len)) {
1175 		kfree(in_handle->data);
1176 		return SVC_DENIED;
1177 	}
1178 
1179 	pages = DIV_ROUND_UP(inlen, PAGE_SIZE);
1180 	in_token->pages = kcalloc(pages, sizeof(struct page *), GFP_KERNEL);
1181 	if (!in_token->pages) {
1182 		kfree(in_handle->data);
1183 		return SVC_DENIED;
1184 	}
1185 	in_token->page_base = 0;
1186 	in_token->page_len = inlen;
1187 	for (i = 0; i < pages; i++) {
1188 		in_token->pages[i] = alloc_page(GFP_KERNEL);
1189 		if (!in_token->pages[i]) {
1190 			kfree(in_handle->data);
1191 			gss_free_in_token_pages(in_token);
1192 			return SVC_DENIED;
1193 		}
1194 	}
1195 
1196 	length = min_t(unsigned int, inlen, argv->iov_len);
1197 	memcpy(page_address(in_token->pages[0]), argv->iov_base, length);
1198 	inlen -= length;
1199 
1200 	to_offs = length;
1201 	from_offs = rqstp->rq_arg.page_base;
1202 	while (inlen) {
1203 		pgto = to_offs >> PAGE_SHIFT;
1204 		pgfrom = from_offs >> PAGE_SHIFT;
1205 		pgto_offs = to_offs & ~PAGE_MASK;
1206 		pgfrom_offs = from_offs & ~PAGE_MASK;
1207 
1208 		length = min_t(unsigned int, inlen,
1209 			 min_t(unsigned int, PAGE_SIZE - pgto_offs,
1210 			       PAGE_SIZE - pgfrom_offs));
1211 		memcpy(page_address(in_token->pages[pgto]) + pgto_offs,
1212 		       page_address(rqstp->rq_arg.pages[pgfrom]) + pgfrom_offs,
1213 		       length);
1214 
1215 		to_offs += length;
1216 		from_offs += length;
1217 		inlen -= length;
1218 	}
1219 	return 0;
1220 }
1221 
1222 static inline int
1223 gss_write_resv(struct kvec *resv, size_t size_limit,
1224 	       struct xdr_netobj *out_handle, struct xdr_netobj *out_token,
1225 	       int major_status, int minor_status)
1226 {
1227 	if (resv->iov_len + 4 > size_limit)
1228 		return -1;
1229 	svc_putnl(resv, RPC_SUCCESS);
1230 	if (svc_safe_putnetobj(resv, out_handle))
1231 		return -1;
1232 	if (resv->iov_len + 3 * 4 > size_limit)
1233 		return -1;
1234 	svc_putnl(resv, major_status);
1235 	svc_putnl(resv, minor_status);
1236 	svc_putnl(resv, GSS_SEQ_WIN);
1237 	if (svc_safe_putnetobj(resv, out_token))
1238 		return -1;
1239 	return 0;
1240 }
1241 
1242 /*
1243  * Having read the cred already and found we're in the context
1244  * initiation case, read the verifier and initiate (or check the results
1245  * of) upcalls to userspace for help with context initiation.  If
1246  * the upcall results are available, write the verifier and result.
1247  * Otherwise, drop the request pending an answer to the upcall.
1248  */
1249 static int svcauth_gss_legacy_init(struct svc_rqst *rqstp,
1250 				   struct rpc_gss_wire_cred *gc)
1251 {
1252 	struct kvec *argv = &rqstp->rq_arg.head[0];
1253 	struct kvec *resv = &rqstp->rq_res.head[0];
1254 	struct rsi *rsip, rsikey;
1255 	int ret;
1256 	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
1257 
1258 	memset(&rsikey, 0, sizeof(rsikey));
1259 	ret = gss_read_verf(gc, argv, &rqstp->rq_auth_stat,
1260 			    &rsikey.in_handle, &rsikey.in_token);
1261 	if (ret)
1262 		return ret;
1263 
1264 	/* Perform upcall, or find upcall result: */
1265 	rsip = rsi_lookup(sn->rsi_cache, &rsikey);
1266 	rsi_free(&rsikey);
1267 	if (!rsip)
1268 		return SVC_CLOSE;
1269 	if (cache_check(sn->rsi_cache, &rsip->h, &rqstp->rq_chandle) < 0)
1270 		/* No upcall result: */
1271 		return SVC_CLOSE;
1272 
1273 	ret = SVC_CLOSE;
1274 	/* Got an answer to the upcall; use it: */
1275 	if (gss_write_init_verf(sn->rsc_cache, rqstp,
1276 				&rsip->out_handle, &rsip->major_status))
1277 		goto out;
1278 	if (gss_write_resv(resv, PAGE_SIZE,
1279 			   &rsip->out_handle, &rsip->out_token,
1280 			   rsip->major_status, rsip->minor_status))
1281 		goto out;
1282 
1283 	ret = SVC_COMPLETE;
1284 out:
1285 	cache_put(&rsip->h, sn->rsi_cache);
1286 	return ret;
1287 }
1288 
1289 static int gss_proxy_save_rsc(struct cache_detail *cd,
1290 				struct gssp_upcall_data *ud,
1291 				uint64_t *handle)
1292 {
1293 	struct rsc rsci, *rscp = NULL;
1294 	static atomic64_t ctxhctr;
1295 	long long ctxh;
1296 	struct gss_api_mech *gm = NULL;
1297 	time64_t expiry;
1298 	int status;
1299 
1300 	memset(&rsci, 0, sizeof(rsci));
1301 	/* context handle */
1302 	status = -ENOMEM;
1303 	/* the handle needs to be just a unique id,
1304 	 * use a static counter */
1305 	ctxh = atomic64_inc_return(&ctxhctr);
1306 
1307 	/* make a copy for the caller */
1308 	*handle = ctxh;
1309 
1310 	/* make a copy for the rsc cache */
1311 	if (dup_to_netobj(&rsci.handle, (char *)handle, sizeof(uint64_t)))
1312 		goto out;
1313 	rscp = rsc_lookup(cd, &rsci);
1314 	if (!rscp)
1315 		goto out;
1316 
1317 	/* creds */
1318 	if (!ud->found_creds) {
1319 		/* userspace seem buggy, we should always get at least a
1320 		 * mapping to nobody */
1321 		goto out;
1322 	} else {
1323 		struct timespec64 boot;
1324 
1325 		/* steal creds */
1326 		rsci.cred = ud->creds;
1327 		memset(&ud->creds, 0, sizeof(struct svc_cred));
1328 
1329 		status = -EOPNOTSUPP;
1330 		/* get mech handle from OID */
1331 		gm = gss_mech_get_by_OID(&ud->mech_oid);
1332 		if (!gm)
1333 			goto out;
1334 		rsci.cred.cr_gss_mech = gm;
1335 
1336 		status = -EINVAL;
1337 		/* mech-specific data: */
1338 		status = gss_import_sec_context(ud->out_handle.data,
1339 						ud->out_handle.len,
1340 						gm, &rsci.mechctx,
1341 						&expiry, GFP_KERNEL);
1342 		if (status)
1343 			goto out;
1344 
1345 		getboottime64(&boot);
1346 		expiry -= boot.tv_sec;
1347 	}
1348 
1349 	rsci.h.expiry_time = expiry;
1350 	rscp = rsc_update(cd, &rsci, rscp);
1351 	status = 0;
1352 out:
1353 	rsc_free(&rsci);
1354 	if (rscp)
1355 		cache_put(&rscp->h, cd);
1356 	else
1357 		status = -ENOMEM;
1358 	return status;
1359 }
1360 
1361 static int svcauth_gss_proxy_init(struct svc_rqst *rqstp,
1362 				  struct rpc_gss_wire_cred *gc)
1363 {
1364 	struct kvec *resv = &rqstp->rq_res.head[0];
1365 	struct xdr_netobj cli_handle;
1366 	struct gssp_upcall_data ud;
1367 	uint64_t handle;
1368 	int status;
1369 	int ret;
1370 	struct net *net = SVC_NET(rqstp);
1371 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1372 
1373 	memset(&ud, 0, sizeof(ud));
1374 	ret = gss_read_proxy_verf(rqstp, gc, &ud.in_handle, &ud.in_token);
1375 	if (ret)
1376 		return ret;
1377 
1378 	ret = SVC_CLOSE;
1379 
1380 	/* Perform synchronous upcall to gss-proxy */
1381 	status = gssp_accept_sec_context_upcall(net, &ud);
1382 	if (status)
1383 		goto out;
1384 
1385 	trace_rpcgss_svc_accept_upcall(rqstp, ud.major_status, ud.minor_status);
1386 
1387 	switch (ud.major_status) {
1388 	case GSS_S_CONTINUE_NEEDED:
1389 		cli_handle = ud.out_handle;
1390 		break;
1391 	case GSS_S_COMPLETE:
1392 		status = gss_proxy_save_rsc(sn->rsc_cache, &ud, &handle);
1393 		if (status)
1394 			goto out;
1395 		cli_handle.data = (u8 *)&handle;
1396 		cli_handle.len = sizeof(handle);
1397 		break;
1398 	default:
1399 		goto out;
1400 	}
1401 
1402 	/* Got an answer to the upcall; use it: */
1403 	if (gss_write_init_verf(sn->rsc_cache, rqstp,
1404 				&cli_handle, &ud.major_status))
1405 		goto out;
1406 	if (gss_write_resv(resv, PAGE_SIZE,
1407 			   &cli_handle, &ud.out_token,
1408 			   ud.major_status, ud.minor_status))
1409 		goto out;
1410 
1411 	ret = SVC_COMPLETE;
1412 out:
1413 	gss_free_in_token_pages(&ud.in_token);
1414 	gssp_free_upcall_data(&ud);
1415 	return ret;
1416 }
1417 
1418 /*
1419  * Try to set the sn->use_gss_proxy variable to a new value. We only allow
1420  * it to be changed if it's currently undefined (-1). If it's any other value
1421  * then return -EBUSY unless the type wouldn't have changed anyway.
1422  */
1423 static int set_gss_proxy(struct net *net, int type)
1424 {
1425 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1426 	int ret;
1427 
1428 	WARN_ON_ONCE(type != 0 && type != 1);
1429 	ret = cmpxchg(&sn->use_gss_proxy, -1, type);
1430 	if (ret != -1 && ret != type)
1431 		return -EBUSY;
1432 	return 0;
1433 }
1434 
1435 static bool use_gss_proxy(struct net *net)
1436 {
1437 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1438 
1439 	/* If use_gss_proxy is still undefined, then try to disable it */
1440 	if (sn->use_gss_proxy == -1)
1441 		set_gss_proxy(net, 0);
1442 	return sn->use_gss_proxy;
1443 }
1444 
1445 #ifdef CONFIG_PROC_FS
1446 
1447 static ssize_t write_gssp(struct file *file, const char __user *buf,
1448 			 size_t count, loff_t *ppos)
1449 {
1450 	struct net *net = pde_data(file_inode(file));
1451 	char tbuf[20];
1452 	unsigned long i;
1453 	int res;
1454 
1455 	if (*ppos || count > sizeof(tbuf)-1)
1456 		return -EINVAL;
1457 	if (copy_from_user(tbuf, buf, count))
1458 		return -EFAULT;
1459 
1460 	tbuf[count] = 0;
1461 	res = kstrtoul(tbuf, 0, &i);
1462 	if (res)
1463 		return res;
1464 	if (i != 1)
1465 		return -EINVAL;
1466 	res = set_gssp_clnt(net);
1467 	if (res)
1468 		return res;
1469 	res = set_gss_proxy(net, 1);
1470 	if (res)
1471 		return res;
1472 	return count;
1473 }
1474 
1475 static ssize_t read_gssp(struct file *file, char __user *buf,
1476 			 size_t count, loff_t *ppos)
1477 {
1478 	struct net *net = pde_data(file_inode(file));
1479 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1480 	unsigned long p = *ppos;
1481 	char tbuf[10];
1482 	size_t len;
1483 
1484 	snprintf(tbuf, sizeof(tbuf), "%d\n", sn->use_gss_proxy);
1485 	len = strlen(tbuf);
1486 	if (p >= len)
1487 		return 0;
1488 	len -= p;
1489 	if (len > count)
1490 		len = count;
1491 	if (copy_to_user(buf, (void *)(tbuf+p), len))
1492 		return -EFAULT;
1493 	*ppos += len;
1494 	return len;
1495 }
1496 
1497 static const struct proc_ops use_gss_proxy_proc_ops = {
1498 	.proc_open	= nonseekable_open,
1499 	.proc_write	= write_gssp,
1500 	.proc_read	= read_gssp,
1501 };
1502 
1503 static int create_use_gss_proxy_proc_entry(struct net *net)
1504 {
1505 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1506 	struct proc_dir_entry **p = &sn->use_gssp_proc;
1507 
1508 	sn->use_gss_proxy = -1;
1509 	*p = proc_create_data("use-gss-proxy", S_IFREG | 0600,
1510 			      sn->proc_net_rpc,
1511 			      &use_gss_proxy_proc_ops, net);
1512 	if (!*p)
1513 		return -ENOMEM;
1514 	init_gssp_clnt(sn);
1515 	return 0;
1516 }
1517 
1518 static void destroy_use_gss_proxy_proc_entry(struct net *net)
1519 {
1520 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1521 
1522 	if (sn->use_gssp_proc) {
1523 		remove_proc_entry("use-gss-proxy", sn->proc_net_rpc);
1524 		clear_gssp_clnt(sn);
1525 	}
1526 }
1527 #else /* CONFIG_PROC_FS */
1528 
1529 static int create_use_gss_proxy_proc_entry(struct net *net)
1530 {
1531 	return 0;
1532 }
1533 
1534 static void destroy_use_gss_proxy_proc_entry(struct net *net) {}
1535 
1536 #endif /* CONFIG_PROC_FS */
1537 
1538 /*
1539  * Accept an rpcsec packet.
1540  * If context establishment, punt to user space
1541  * If data exchange, verify/decrypt
1542  * If context destruction, handle here
1543  * In the context establishment and destruction case we encode
1544  * response here and return SVC_COMPLETE.
1545  */
1546 static int
1547 svcauth_gss_accept(struct svc_rqst *rqstp)
1548 {
1549 	struct kvec	*argv = &rqstp->rq_arg.head[0];
1550 	struct kvec	*resv = &rqstp->rq_res.head[0];
1551 	u32		crlen;
1552 	struct gss_svc_data *svcdata = rqstp->rq_auth_data;
1553 	struct rpc_gss_wire_cred *gc;
1554 	struct rsc	*rsci = NULL;
1555 	__be32		*rpcstart;
1556 	__be32		*reject_stat = resv->iov_base + resv->iov_len;
1557 	int		ret;
1558 	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
1559 
1560 	rqstp->rq_auth_stat = rpc_autherr_badcred;
1561 	if (!svcdata)
1562 		svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL);
1563 	if (!svcdata)
1564 		goto auth_err;
1565 	rqstp->rq_auth_data = svcdata;
1566 	svcdata->verf_start = NULL;
1567 	svcdata->rsci = NULL;
1568 	gc = &svcdata->clcred;
1569 
1570 	/* start of rpc packet is 7 u32's back from here:
1571 	 * xid direction rpcversion prog vers proc flavour
1572 	 */
1573 	rpcstart = argv->iov_base;
1574 	rpcstart -= 7;
1575 
1576 	/* credential is:
1577 	 *   version(==1), proc(0,1,2,3), seq, service (1,2,3), handle
1578 	 * at least 5 u32s, and is preceded by length, so that makes 6.
1579 	 */
1580 
1581 	if (argv->iov_len < 5 * 4)
1582 		goto auth_err;
1583 	crlen = svc_getnl(argv);
1584 	if (svc_getnl(argv) != RPC_GSS_VERSION)
1585 		goto auth_err;
1586 	gc->gc_proc = svc_getnl(argv);
1587 	gc->gc_seq = svc_getnl(argv);
1588 	gc->gc_svc = svc_getnl(argv);
1589 	if (svc_safe_getnetobj(argv, &gc->gc_ctx))
1590 		goto auth_err;
1591 	if (crlen != round_up_to_quad(gc->gc_ctx.len) + 5 * 4)
1592 		goto auth_err;
1593 
1594 	if ((gc->gc_proc != RPC_GSS_PROC_DATA) && (rqstp->rq_proc != 0))
1595 		goto auth_err;
1596 
1597 	rqstp->rq_auth_stat = rpc_autherr_badverf;
1598 	switch (gc->gc_proc) {
1599 	case RPC_GSS_PROC_INIT:
1600 	case RPC_GSS_PROC_CONTINUE_INIT:
1601 		if (use_gss_proxy(SVC_NET(rqstp)))
1602 			return svcauth_gss_proxy_init(rqstp, gc);
1603 		else
1604 			return svcauth_gss_legacy_init(rqstp, gc);
1605 	case RPC_GSS_PROC_DATA:
1606 	case RPC_GSS_PROC_DESTROY:
1607 		/* Look up the context, and check the verifier: */
1608 		rqstp->rq_auth_stat = rpcsec_gsserr_credproblem;
1609 		rsci = gss_svc_searchbyctx(sn->rsc_cache, &gc->gc_ctx);
1610 		if (!rsci)
1611 			goto auth_err;
1612 		switch (gss_verify_header(rqstp, rsci, rpcstart, gc)) {
1613 		case SVC_OK:
1614 			break;
1615 		case SVC_DENIED:
1616 			goto auth_err;
1617 		case SVC_DROP:
1618 			goto drop;
1619 		}
1620 		break;
1621 	default:
1622 		rqstp->rq_auth_stat = rpc_autherr_rejectedcred;
1623 		goto auth_err;
1624 	}
1625 
1626 	/* now act upon the command: */
1627 	switch (gc->gc_proc) {
1628 	case RPC_GSS_PROC_DESTROY:
1629 		if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
1630 			goto auth_err;
1631 		/* Delete the entry from the cache_list and call cache_put */
1632 		sunrpc_cache_unhash(sn->rsc_cache, &rsci->h);
1633 		if (resv->iov_len + 4 > PAGE_SIZE)
1634 			goto drop;
1635 		svc_putnl(resv, RPC_SUCCESS);
1636 		goto complete;
1637 	case RPC_GSS_PROC_DATA:
1638 		rqstp->rq_auth_stat = rpcsec_gsserr_ctxproblem;
1639 		svcdata->verf_start = resv->iov_base + resv->iov_len;
1640 		if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
1641 			goto auth_err;
1642 		rqstp->rq_cred = rsci->cred;
1643 		get_group_info(rsci->cred.cr_group_info);
1644 		rqstp->rq_auth_stat = rpc_autherr_badcred;
1645 		switch (gc->gc_svc) {
1646 		case RPC_GSS_SVC_NONE:
1647 			break;
1648 		case RPC_GSS_SVC_INTEGRITY:
1649 			/* placeholders for length and seq. number: */
1650 			svc_putnl(resv, 0);
1651 			svc_putnl(resv, 0);
1652 			if (unwrap_integ_data(rqstp, &rqstp->rq_arg,
1653 					gc->gc_seq, rsci->mechctx))
1654 				goto garbage_args;
1655 			rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE;
1656 			break;
1657 		case RPC_GSS_SVC_PRIVACY:
1658 			/* placeholders for length and seq. number: */
1659 			svc_putnl(resv, 0);
1660 			svc_putnl(resv, 0);
1661 			if (unwrap_priv_data(rqstp, &rqstp->rq_arg,
1662 					gc->gc_seq, rsci->mechctx))
1663 				goto garbage_args;
1664 			rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE * 2;
1665 			break;
1666 		default:
1667 			goto auth_err;
1668 		}
1669 		svcdata->rsci = rsci;
1670 		cache_get(&rsci->h);
1671 		rqstp->rq_cred.cr_flavor = gss_svc_to_pseudoflavor(
1672 					rsci->mechctx->mech_type,
1673 					GSS_C_QOP_DEFAULT,
1674 					gc->gc_svc);
1675 		ret = SVC_OK;
1676 		trace_rpcgss_svc_authenticate(rqstp, gc);
1677 		goto out;
1678 	}
1679 garbage_args:
1680 	ret = SVC_GARBAGE;
1681 	goto out;
1682 auth_err:
1683 	/* Restore write pointer to its original value: */
1684 	xdr_ressize_check(rqstp, reject_stat);
1685 	ret = SVC_DENIED;
1686 	goto out;
1687 complete:
1688 	ret = SVC_COMPLETE;
1689 	goto out;
1690 drop:
1691 	ret = SVC_CLOSE;
1692 out:
1693 	if (rsci)
1694 		cache_put(&rsci->h, sn->rsc_cache);
1695 	return ret;
1696 }
1697 
1698 static __be32 *
1699 svcauth_gss_prepare_to_wrap(struct xdr_buf *resbuf, struct gss_svc_data *gsd)
1700 {
1701 	__be32 *p;
1702 	u32 verf_len;
1703 
1704 	p = gsd->verf_start;
1705 	gsd->verf_start = NULL;
1706 
1707 	/* If the reply stat is nonzero, don't wrap: */
1708 	if (*(p-1) != rpc_success)
1709 		return NULL;
1710 	/* Skip the verifier: */
1711 	p += 1;
1712 	verf_len = ntohl(*p++);
1713 	p += XDR_QUADLEN(verf_len);
1714 	/* move accept_stat to right place: */
1715 	memcpy(p, p + 2, 4);
1716 	/* Also don't wrap if the accept stat is nonzero: */
1717 	if (*p != rpc_success) {
1718 		resbuf->head[0].iov_len -= 2 * 4;
1719 		return NULL;
1720 	}
1721 	p++;
1722 	return p;
1723 }
1724 
1725 static inline int
1726 svcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)
1727 {
1728 	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
1729 	struct rpc_gss_wire_cred *gc = &gsd->clcred;
1730 	struct xdr_buf *resbuf = &rqstp->rq_res;
1731 	struct xdr_buf integ_buf;
1732 	struct xdr_netobj mic;
1733 	struct kvec *resv;
1734 	__be32 *p;
1735 	int integ_offset, integ_len;
1736 	int stat = -EINVAL;
1737 
1738 	p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
1739 	if (p == NULL)
1740 		goto out;
1741 	integ_offset = (u8 *)(p + 1) - (u8 *)resbuf->head[0].iov_base;
1742 	integ_len = resbuf->len - integ_offset;
1743 	if (integ_len & 3)
1744 		goto out;
1745 	*p++ = htonl(integ_len);
1746 	*p++ = htonl(gc->gc_seq);
1747 	if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len)) {
1748 		WARN_ON_ONCE(1);
1749 		goto out_err;
1750 	}
1751 	if (resbuf->tail[0].iov_base == NULL) {
1752 		if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)
1753 			goto out_err;
1754 		resbuf->tail[0].iov_base = resbuf->head[0].iov_base
1755 						+ resbuf->head[0].iov_len;
1756 		resbuf->tail[0].iov_len = 0;
1757 	}
1758 	resv = &resbuf->tail[0];
1759 	mic.data = (u8 *)resv->iov_base + resv->iov_len + 4;
1760 	if (gss_get_mic(gsd->rsci->mechctx, &integ_buf, &mic))
1761 		goto out_err;
1762 	svc_putnl(resv, mic.len);
1763 	memset(mic.data + mic.len, 0,
1764 			round_up_to_quad(mic.len) - mic.len);
1765 	resv->iov_len += XDR_QUADLEN(mic.len) << 2;
1766 	/* not strictly required: */
1767 	resbuf->len += XDR_QUADLEN(mic.len) << 2;
1768 	if (resv->iov_len > PAGE_SIZE)
1769 		goto out_err;
1770 out:
1771 	stat = 0;
1772 out_err:
1773 	return stat;
1774 }
1775 
1776 static inline int
1777 svcauth_gss_wrap_resp_priv(struct svc_rqst *rqstp)
1778 {
1779 	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
1780 	struct rpc_gss_wire_cred *gc = &gsd->clcred;
1781 	struct xdr_buf *resbuf = &rqstp->rq_res;
1782 	struct page **inpages = NULL;
1783 	__be32 *p, *len;
1784 	int offset;
1785 	int pad;
1786 
1787 	p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
1788 	if (p == NULL)
1789 		return 0;
1790 	len = p++;
1791 	offset = (u8 *)p - (u8 *)resbuf->head[0].iov_base;
1792 	*p++ = htonl(gc->gc_seq);
1793 	inpages = resbuf->pages;
1794 	/* XXX: Would be better to write some xdr helper functions for
1795 	 * nfs{2,3,4}xdr.c that place the data right, instead of copying: */
1796 
1797 	/*
1798 	 * If there is currently tail data, make sure there is
1799 	 * room for the head, tail, and 2 * RPC_MAX_AUTH_SIZE in
1800 	 * the page, and move the current tail data such that
1801 	 * there is RPC_MAX_AUTH_SIZE slack space available in
1802 	 * both the head and tail.
1803 	 */
1804 	if (resbuf->tail[0].iov_base) {
1805 		if (resbuf->tail[0].iov_base >=
1806 			resbuf->head[0].iov_base + PAGE_SIZE)
1807 			return -EINVAL;
1808 		if (resbuf->tail[0].iov_base < resbuf->head[0].iov_base)
1809 			return -EINVAL;
1810 		if (resbuf->tail[0].iov_len + resbuf->head[0].iov_len
1811 				+ 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE)
1812 			return -ENOMEM;
1813 		memmove(resbuf->tail[0].iov_base + RPC_MAX_AUTH_SIZE,
1814 			resbuf->tail[0].iov_base,
1815 			resbuf->tail[0].iov_len);
1816 		resbuf->tail[0].iov_base += RPC_MAX_AUTH_SIZE;
1817 	}
1818 	/*
1819 	 * If there is no current tail data, make sure there is
1820 	 * room for the head data, and 2 * RPC_MAX_AUTH_SIZE in the
1821 	 * allotted page, and set up tail information such that there
1822 	 * is RPC_MAX_AUTH_SIZE slack space available in both the
1823 	 * head and tail.
1824 	 */
1825 	if (resbuf->tail[0].iov_base == NULL) {
1826 		if (resbuf->head[0].iov_len + 2*RPC_MAX_AUTH_SIZE > PAGE_SIZE)
1827 			return -ENOMEM;
1828 		resbuf->tail[0].iov_base = resbuf->head[0].iov_base
1829 			+ resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE;
1830 		resbuf->tail[0].iov_len = 0;
1831 	}
1832 	if (gss_wrap(gsd->rsci->mechctx, offset, resbuf, inpages))
1833 		return -ENOMEM;
1834 	*len = htonl(resbuf->len - offset);
1835 	pad = 3 - ((resbuf->len - offset - 1)&3);
1836 	p = (__be32 *)(resbuf->tail[0].iov_base + resbuf->tail[0].iov_len);
1837 	memset(p, 0, pad);
1838 	resbuf->tail[0].iov_len += pad;
1839 	resbuf->len += pad;
1840 	return 0;
1841 }
1842 
1843 static int
1844 svcauth_gss_release(struct svc_rqst *rqstp)
1845 {
1846 	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
1847 	struct rpc_gss_wire_cred *gc;
1848 	struct xdr_buf *resbuf = &rqstp->rq_res;
1849 	int stat = -EINVAL;
1850 	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
1851 
1852 	if (!gsd)
1853 		goto out;
1854 	gc = &gsd->clcred;
1855 	if (gc->gc_proc != RPC_GSS_PROC_DATA)
1856 		goto out;
1857 	/* Release can be called twice, but we only wrap once. */
1858 	if (gsd->verf_start == NULL)
1859 		goto out;
1860 	/* normally not set till svc_send, but we need it here: */
1861 	/* XXX: what for?  Do we mess it up the moment we call svc_putu32
1862 	 * or whatever? */
1863 	resbuf->len = total_buf_len(resbuf);
1864 	switch (gc->gc_svc) {
1865 	case RPC_GSS_SVC_NONE:
1866 		break;
1867 	case RPC_GSS_SVC_INTEGRITY:
1868 		stat = svcauth_gss_wrap_resp_integ(rqstp);
1869 		if (stat)
1870 			goto out_err;
1871 		break;
1872 	case RPC_GSS_SVC_PRIVACY:
1873 		stat = svcauth_gss_wrap_resp_priv(rqstp);
1874 		if (stat)
1875 			goto out_err;
1876 		break;
1877 	/*
1878 	 * For any other gc_svc value, svcauth_gss_accept() already set
1879 	 * the auth_error appropriately; just fall through:
1880 	 */
1881 	}
1882 
1883 out:
1884 	stat = 0;
1885 out_err:
1886 	if (rqstp->rq_client)
1887 		auth_domain_put(rqstp->rq_client);
1888 	rqstp->rq_client = NULL;
1889 	if (rqstp->rq_gssclient)
1890 		auth_domain_put(rqstp->rq_gssclient);
1891 	rqstp->rq_gssclient = NULL;
1892 	if (rqstp->rq_cred.cr_group_info)
1893 		put_group_info(rqstp->rq_cred.cr_group_info);
1894 	rqstp->rq_cred.cr_group_info = NULL;
1895 	if (gsd && gsd->rsci) {
1896 		cache_put(&gsd->rsci->h, sn->rsc_cache);
1897 		gsd->rsci = NULL;
1898 	}
1899 	return stat;
1900 }
1901 
1902 static void
1903 svcauth_gss_domain_release_rcu(struct rcu_head *head)
1904 {
1905 	struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head);
1906 	struct gss_domain *gd = container_of(dom, struct gss_domain, h);
1907 
1908 	kfree(dom->name);
1909 	kfree(gd);
1910 }
1911 
1912 static void
1913 svcauth_gss_domain_release(struct auth_domain *dom)
1914 {
1915 	call_rcu(&dom->rcu_head, svcauth_gss_domain_release_rcu);
1916 }
1917 
1918 static struct auth_ops svcauthops_gss = {
1919 	.name		= "rpcsec_gss",
1920 	.owner		= THIS_MODULE,
1921 	.flavour	= RPC_AUTH_GSS,
1922 	.accept		= svcauth_gss_accept,
1923 	.release	= svcauth_gss_release,
1924 	.domain_release = svcauth_gss_domain_release,
1925 	.set_client	= svcauth_gss_set_client,
1926 };
1927 
1928 static int rsi_cache_create_net(struct net *net)
1929 {
1930 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1931 	struct cache_detail *cd;
1932 	int err;
1933 
1934 	cd = cache_create_net(&rsi_cache_template, net);
1935 	if (IS_ERR(cd))
1936 		return PTR_ERR(cd);
1937 	err = cache_register_net(cd, net);
1938 	if (err) {
1939 		cache_destroy_net(cd, net);
1940 		return err;
1941 	}
1942 	sn->rsi_cache = cd;
1943 	return 0;
1944 }
1945 
1946 static void rsi_cache_destroy_net(struct net *net)
1947 {
1948 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1949 	struct cache_detail *cd = sn->rsi_cache;
1950 
1951 	sn->rsi_cache = NULL;
1952 	cache_purge(cd);
1953 	cache_unregister_net(cd, net);
1954 	cache_destroy_net(cd, net);
1955 }
1956 
1957 static int rsc_cache_create_net(struct net *net)
1958 {
1959 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1960 	struct cache_detail *cd;
1961 	int err;
1962 
1963 	cd = cache_create_net(&rsc_cache_template, net);
1964 	if (IS_ERR(cd))
1965 		return PTR_ERR(cd);
1966 	err = cache_register_net(cd, net);
1967 	if (err) {
1968 		cache_destroy_net(cd, net);
1969 		return err;
1970 	}
1971 	sn->rsc_cache = cd;
1972 	return 0;
1973 }
1974 
1975 static void rsc_cache_destroy_net(struct net *net)
1976 {
1977 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1978 	struct cache_detail *cd = sn->rsc_cache;
1979 
1980 	sn->rsc_cache = NULL;
1981 	cache_purge(cd);
1982 	cache_unregister_net(cd, net);
1983 	cache_destroy_net(cd, net);
1984 }
1985 
1986 int
1987 gss_svc_init_net(struct net *net)
1988 {
1989 	int rv;
1990 
1991 	rv = rsc_cache_create_net(net);
1992 	if (rv)
1993 		return rv;
1994 	rv = rsi_cache_create_net(net);
1995 	if (rv)
1996 		goto out1;
1997 	rv = create_use_gss_proxy_proc_entry(net);
1998 	if (rv)
1999 		goto out2;
2000 	return 0;
2001 out2:
2002 	rsi_cache_destroy_net(net);
2003 out1:
2004 	rsc_cache_destroy_net(net);
2005 	return rv;
2006 }
2007 
2008 void
2009 gss_svc_shutdown_net(struct net *net)
2010 {
2011 	destroy_use_gss_proxy_proc_entry(net);
2012 	rsi_cache_destroy_net(net);
2013 	rsc_cache_destroy_net(net);
2014 }
2015 
2016 int
2017 gss_svc_init(void)
2018 {
2019 	return svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);
2020 }
2021 
2022 void
2023 gss_svc_shutdown(void)
2024 {
2025 	svc_auth_unregister(RPC_AUTH_GSS);
2026 }
2027