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 void gss_free_in_token_pages(struct gssp_in_token *in_token)
1094 {
1095 	u32 inlen;
1096 	int i;
1097 
1098 	i = 0;
1099 	inlen = in_token->page_len;
1100 	while (inlen) {
1101 		if (in_token->pages[i])
1102 			put_page(in_token->pages[i]);
1103 		inlen -= inlen > PAGE_SIZE ? PAGE_SIZE : inlen;
1104 	}
1105 
1106 	kfree(in_token->pages);
1107 	in_token->pages = NULL;
1108 }
1109 
1110 static int gss_read_proxy_verf(struct svc_rqst *rqstp,
1111 			       struct rpc_gss_wire_cred *gc,
1112 			       struct xdr_netobj *in_handle,
1113 			       struct gssp_in_token *in_token)
1114 {
1115 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
1116 	unsigned int length, pgto_offs, pgfrom_offs;
1117 	int pages, i, pgto, pgfrom;
1118 	size_t to_offs, from_offs;
1119 	u32 inlen;
1120 
1121 	if (dup_netobj(in_handle, &gc->gc_ctx))
1122 		return SVC_CLOSE;
1123 
1124 	/*
1125 	 *  RFC 2203 Section 5.2.2
1126 	 *
1127 	 *	struct rpc_gss_init_arg {
1128 	 *		opaque gss_token<>;
1129 	 *	};
1130 	 */
1131 	if (xdr_stream_decode_u32(xdr, &inlen) < 0)
1132 		goto out_denied_free;
1133 	if (inlen > xdr_stream_remaining(xdr))
1134 		goto out_denied_free;
1135 
1136 	pages = DIV_ROUND_UP(inlen, PAGE_SIZE);
1137 	in_token->pages = kcalloc(pages, sizeof(struct page *), GFP_KERNEL);
1138 	if (!in_token->pages)
1139 		goto out_denied_free;
1140 	in_token->page_base = 0;
1141 	in_token->page_len = inlen;
1142 	for (i = 0; i < pages; i++) {
1143 		in_token->pages[i] = alloc_page(GFP_KERNEL);
1144 		if (!in_token->pages[i]) {
1145 			gss_free_in_token_pages(in_token);
1146 			goto out_denied_free;
1147 		}
1148 	}
1149 
1150 	length = min_t(unsigned int, inlen, (char *)xdr->end - (char *)xdr->p);
1151 	memcpy(page_address(in_token->pages[0]), xdr->p, length);
1152 	inlen -= length;
1153 
1154 	to_offs = length;
1155 	from_offs = rqstp->rq_arg.page_base;
1156 	while (inlen) {
1157 		pgto = to_offs >> PAGE_SHIFT;
1158 		pgfrom = from_offs >> PAGE_SHIFT;
1159 		pgto_offs = to_offs & ~PAGE_MASK;
1160 		pgfrom_offs = from_offs & ~PAGE_MASK;
1161 
1162 		length = min_t(unsigned int, inlen,
1163 			 min_t(unsigned int, PAGE_SIZE - pgto_offs,
1164 			       PAGE_SIZE - pgfrom_offs));
1165 		memcpy(page_address(in_token->pages[pgto]) + pgto_offs,
1166 		       page_address(rqstp->rq_arg.pages[pgfrom]) + pgfrom_offs,
1167 		       length);
1168 
1169 		to_offs += length;
1170 		from_offs += length;
1171 		inlen -= length;
1172 	}
1173 	return 0;
1174 
1175 out_denied_free:
1176 	kfree(in_handle->data);
1177 	return SVC_DENIED;
1178 }
1179 
1180 static inline int
1181 gss_write_resv(struct kvec *resv, size_t size_limit,
1182 	       struct xdr_netobj *out_handle, struct xdr_netobj *out_token,
1183 	       int major_status, int minor_status)
1184 {
1185 	if (resv->iov_len + 4 > size_limit)
1186 		return -1;
1187 	svc_putnl(resv, RPC_SUCCESS);
1188 	if (svc_safe_putnetobj(resv, out_handle))
1189 		return -1;
1190 	if (resv->iov_len + 3 * 4 > size_limit)
1191 		return -1;
1192 	svc_putnl(resv, major_status);
1193 	svc_putnl(resv, minor_status);
1194 	svc_putnl(resv, GSS_SEQ_WIN);
1195 	if (svc_safe_putnetobj(resv, out_token))
1196 		return -1;
1197 	return 0;
1198 }
1199 
1200 /*
1201  * Having read the cred already and found we're in the context
1202  * initiation case, read the verifier and initiate (or check the results
1203  * of) upcalls to userspace for help with context initiation.  If
1204  * the upcall results are available, write the verifier and result.
1205  * Otherwise, drop the request pending an answer to the upcall.
1206  */
1207 static int
1208 svcauth_gss_legacy_init(struct svc_rqst *rqstp,
1209 			struct rpc_gss_wire_cred *gc)
1210 {
1211 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
1212 	struct kvec *resv = &rqstp->rq_res.head[0];
1213 	struct rsi *rsip, rsikey;
1214 	__be32 *p;
1215 	u32 len;
1216 	int ret;
1217 	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
1218 
1219 	memset(&rsikey, 0, sizeof(rsikey));
1220 	if (dup_netobj(&rsikey.in_handle, &gc->gc_ctx))
1221 		return SVC_CLOSE;
1222 
1223 	/*
1224 	 *  RFC 2203 Section 5.2.2
1225 	 *
1226 	 *	struct rpc_gss_init_arg {
1227 	 *		opaque gss_token<>;
1228 	 *	};
1229 	 */
1230 	if (xdr_stream_decode_u32(xdr, &len) < 0) {
1231 		kfree(rsikey.in_handle.data);
1232 		return SVC_DENIED;
1233 	}
1234 	p = xdr_inline_decode(xdr, len);
1235 	if (!p) {
1236 		kfree(rsikey.in_handle.data);
1237 		return SVC_DENIED;
1238 	}
1239 	rsikey.in_token.data = kmalloc(len, GFP_KERNEL);
1240 	if (ZERO_OR_NULL_PTR(rsikey.in_token.data)) {
1241 		kfree(rsikey.in_handle.data);
1242 		return SVC_CLOSE;
1243 	}
1244 	memcpy(rsikey.in_token.data, p, len);
1245 	rsikey.in_token.len = len;
1246 
1247 	/* Perform upcall, or find upcall result: */
1248 	rsip = rsi_lookup(sn->rsi_cache, &rsikey);
1249 	rsi_free(&rsikey);
1250 	if (!rsip)
1251 		return SVC_CLOSE;
1252 	if (cache_check(sn->rsi_cache, &rsip->h, &rqstp->rq_chandle) < 0)
1253 		/* No upcall result: */
1254 		return SVC_CLOSE;
1255 
1256 	ret = SVC_CLOSE;
1257 	/* Got an answer to the upcall; use it: */
1258 	if (gss_write_init_verf(sn->rsc_cache, rqstp,
1259 				&rsip->out_handle, &rsip->major_status))
1260 		goto out;
1261 	if (gss_write_resv(resv, PAGE_SIZE,
1262 			   &rsip->out_handle, &rsip->out_token,
1263 			   rsip->major_status, rsip->minor_status))
1264 		goto out;
1265 
1266 	ret = SVC_COMPLETE;
1267 out:
1268 	cache_put(&rsip->h, sn->rsi_cache);
1269 	return ret;
1270 }
1271 
1272 static int gss_proxy_save_rsc(struct cache_detail *cd,
1273 				struct gssp_upcall_data *ud,
1274 				uint64_t *handle)
1275 {
1276 	struct rsc rsci, *rscp = NULL;
1277 	static atomic64_t ctxhctr;
1278 	long long ctxh;
1279 	struct gss_api_mech *gm = NULL;
1280 	time64_t expiry;
1281 	int status;
1282 
1283 	memset(&rsci, 0, sizeof(rsci));
1284 	/* context handle */
1285 	status = -ENOMEM;
1286 	/* the handle needs to be just a unique id,
1287 	 * use a static counter */
1288 	ctxh = atomic64_inc_return(&ctxhctr);
1289 
1290 	/* make a copy for the caller */
1291 	*handle = ctxh;
1292 
1293 	/* make a copy for the rsc cache */
1294 	if (dup_to_netobj(&rsci.handle, (char *)handle, sizeof(uint64_t)))
1295 		goto out;
1296 	rscp = rsc_lookup(cd, &rsci);
1297 	if (!rscp)
1298 		goto out;
1299 
1300 	/* creds */
1301 	if (!ud->found_creds) {
1302 		/* userspace seem buggy, we should always get at least a
1303 		 * mapping to nobody */
1304 		goto out;
1305 	} else {
1306 		struct timespec64 boot;
1307 
1308 		/* steal creds */
1309 		rsci.cred = ud->creds;
1310 		memset(&ud->creds, 0, sizeof(struct svc_cred));
1311 
1312 		status = -EOPNOTSUPP;
1313 		/* get mech handle from OID */
1314 		gm = gss_mech_get_by_OID(&ud->mech_oid);
1315 		if (!gm)
1316 			goto out;
1317 		rsci.cred.cr_gss_mech = gm;
1318 
1319 		status = -EINVAL;
1320 		/* mech-specific data: */
1321 		status = gss_import_sec_context(ud->out_handle.data,
1322 						ud->out_handle.len,
1323 						gm, &rsci.mechctx,
1324 						&expiry, GFP_KERNEL);
1325 		if (status)
1326 			goto out;
1327 
1328 		getboottime64(&boot);
1329 		expiry -= boot.tv_sec;
1330 	}
1331 
1332 	rsci.h.expiry_time = expiry;
1333 	rscp = rsc_update(cd, &rsci, rscp);
1334 	status = 0;
1335 out:
1336 	rsc_free(&rsci);
1337 	if (rscp)
1338 		cache_put(&rscp->h, cd);
1339 	else
1340 		status = -ENOMEM;
1341 	return status;
1342 }
1343 
1344 static int svcauth_gss_proxy_init(struct svc_rqst *rqstp,
1345 				  struct rpc_gss_wire_cred *gc)
1346 {
1347 	struct kvec *resv = &rqstp->rq_res.head[0];
1348 	struct xdr_netobj cli_handle;
1349 	struct gssp_upcall_data ud;
1350 	uint64_t handle;
1351 	int status;
1352 	int ret;
1353 	struct net *net = SVC_NET(rqstp);
1354 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1355 
1356 	memset(&ud, 0, sizeof(ud));
1357 	ret = gss_read_proxy_verf(rqstp, gc, &ud.in_handle, &ud.in_token);
1358 	if (ret)
1359 		return ret;
1360 
1361 	ret = SVC_CLOSE;
1362 
1363 	/* Perform synchronous upcall to gss-proxy */
1364 	status = gssp_accept_sec_context_upcall(net, &ud);
1365 	if (status)
1366 		goto out;
1367 
1368 	trace_rpcgss_svc_accept_upcall(rqstp, ud.major_status, ud.minor_status);
1369 
1370 	switch (ud.major_status) {
1371 	case GSS_S_CONTINUE_NEEDED:
1372 		cli_handle = ud.out_handle;
1373 		break;
1374 	case GSS_S_COMPLETE:
1375 		status = gss_proxy_save_rsc(sn->rsc_cache, &ud, &handle);
1376 		if (status)
1377 			goto out;
1378 		cli_handle.data = (u8 *)&handle;
1379 		cli_handle.len = sizeof(handle);
1380 		break;
1381 	default:
1382 		goto out;
1383 	}
1384 
1385 	/* Got an answer to the upcall; use it: */
1386 	if (gss_write_init_verf(sn->rsc_cache, rqstp,
1387 				&cli_handle, &ud.major_status))
1388 		goto out;
1389 	if (gss_write_resv(resv, PAGE_SIZE,
1390 			   &cli_handle, &ud.out_token,
1391 			   ud.major_status, ud.minor_status))
1392 		goto out;
1393 
1394 	ret = SVC_COMPLETE;
1395 out:
1396 	gss_free_in_token_pages(&ud.in_token);
1397 	gssp_free_upcall_data(&ud);
1398 	return ret;
1399 }
1400 
1401 /*
1402  * Try to set the sn->use_gss_proxy variable to a new value. We only allow
1403  * it to be changed if it's currently undefined (-1). If it's any other value
1404  * then return -EBUSY unless the type wouldn't have changed anyway.
1405  */
1406 static int set_gss_proxy(struct net *net, int type)
1407 {
1408 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1409 	int ret;
1410 
1411 	WARN_ON_ONCE(type != 0 && type != 1);
1412 	ret = cmpxchg(&sn->use_gss_proxy, -1, type);
1413 	if (ret != -1 && ret != type)
1414 		return -EBUSY;
1415 	return 0;
1416 }
1417 
1418 static bool use_gss_proxy(struct net *net)
1419 {
1420 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1421 
1422 	/* If use_gss_proxy is still undefined, then try to disable it */
1423 	if (sn->use_gss_proxy == -1)
1424 		set_gss_proxy(net, 0);
1425 	return sn->use_gss_proxy;
1426 }
1427 
1428 static noinline_for_stack int
1429 svcauth_gss_proc_init(struct svc_rqst *rqstp, struct rpc_gss_wire_cred *gc)
1430 {
1431 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
1432 	u32 flavor, len;
1433 	void *body;
1434 
1435 	svcxdr_init_decode(rqstp);
1436 
1437 	/* Call's verf field: */
1438 	if (xdr_stream_decode_opaque_auth(xdr, &flavor, &body, &len) < 0)
1439 		return SVC_GARBAGE;
1440 	if (flavor != RPC_AUTH_NULL || len != 0) {
1441 		rqstp->rq_auth_stat = rpc_autherr_badverf;
1442 		return SVC_DENIED;
1443 	}
1444 
1445 	if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0) {
1446 		rqstp->rq_auth_stat = rpc_autherr_badcred;
1447 		return SVC_DENIED;
1448 	}
1449 
1450 	if (!use_gss_proxy(SVC_NET(rqstp)))
1451 		return svcauth_gss_legacy_init(rqstp, gc);
1452 	return svcauth_gss_proxy_init(rqstp, gc);
1453 }
1454 
1455 #ifdef CONFIG_PROC_FS
1456 
1457 static ssize_t write_gssp(struct file *file, const char __user *buf,
1458 			 size_t count, loff_t *ppos)
1459 {
1460 	struct net *net = pde_data(file_inode(file));
1461 	char tbuf[20];
1462 	unsigned long i;
1463 	int res;
1464 
1465 	if (*ppos || count > sizeof(tbuf)-1)
1466 		return -EINVAL;
1467 	if (copy_from_user(tbuf, buf, count))
1468 		return -EFAULT;
1469 
1470 	tbuf[count] = 0;
1471 	res = kstrtoul(tbuf, 0, &i);
1472 	if (res)
1473 		return res;
1474 	if (i != 1)
1475 		return -EINVAL;
1476 	res = set_gssp_clnt(net);
1477 	if (res)
1478 		return res;
1479 	res = set_gss_proxy(net, 1);
1480 	if (res)
1481 		return res;
1482 	return count;
1483 }
1484 
1485 static ssize_t read_gssp(struct file *file, char __user *buf,
1486 			 size_t count, loff_t *ppos)
1487 {
1488 	struct net *net = pde_data(file_inode(file));
1489 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1490 	unsigned long p = *ppos;
1491 	char tbuf[10];
1492 	size_t len;
1493 
1494 	snprintf(tbuf, sizeof(tbuf), "%d\n", sn->use_gss_proxy);
1495 	len = strlen(tbuf);
1496 	if (p >= len)
1497 		return 0;
1498 	len -= p;
1499 	if (len > count)
1500 		len = count;
1501 	if (copy_to_user(buf, (void *)(tbuf+p), len))
1502 		return -EFAULT;
1503 	*ppos += len;
1504 	return len;
1505 }
1506 
1507 static const struct proc_ops use_gss_proxy_proc_ops = {
1508 	.proc_open	= nonseekable_open,
1509 	.proc_write	= write_gssp,
1510 	.proc_read	= read_gssp,
1511 };
1512 
1513 static int create_use_gss_proxy_proc_entry(struct net *net)
1514 {
1515 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1516 	struct proc_dir_entry **p = &sn->use_gssp_proc;
1517 
1518 	sn->use_gss_proxy = -1;
1519 	*p = proc_create_data("use-gss-proxy", S_IFREG | 0600,
1520 			      sn->proc_net_rpc,
1521 			      &use_gss_proxy_proc_ops, net);
1522 	if (!*p)
1523 		return -ENOMEM;
1524 	init_gssp_clnt(sn);
1525 	return 0;
1526 }
1527 
1528 static void destroy_use_gss_proxy_proc_entry(struct net *net)
1529 {
1530 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1531 
1532 	if (sn->use_gssp_proc) {
1533 		remove_proc_entry("use-gss-proxy", sn->proc_net_rpc);
1534 		clear_gssp_clnt(sn);
1535 	}
1536 }
1537 #else /* CONFIG_PROC_FS */
1538 
1539 static int create_use_gss_proxy_proc_entry(struct net *net)
1540 {
1541 	return 0;
1542 }
1543 
1544 static void destroy_use_gss_proxy_proc_entry(struct net *net) {}
1545 
1546 #endif /* CONFIG_PROC_FS */
1547 
1548 /*
1549  * Accept an rpcsec packet.
1550  * If context establishment, punt to user space
1551  * If data exchange, verify/decrypt
1552  * If context destruction, handle here
1553  * In the context establishment and destruction case we encode
1554  * response here and return SVC_COMPLETE.
1555  */
1556 static int
1557 svcauth_gss_accept(struct svc_rqst *rqstp)
1558 {
1559 	struct kvec	*argv = &rqstp->rq_arg.head[0];
1560 	struct kvec	*resv = &rqstp->rq_res.head[0];
1561 	u32		crlen;
1562 	struct gss_svc_data *svcdata = rqstp->rq_auth_data;
1563 	struct rpc_gss_wire_cred *gc;
1564 	struct rsc	*rsci = NULL;
1565 	__be32		*rpcstart;
1566 	__be32		*reject_stat = resv->iov_base + resv->iov_len;
1567 	int		ret;
1568 	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
1569 
1570 	rqstp->rq_auth_stat = rpc_autherr_badcred;
1571 	if (!svcdata)
1572 		svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL);
1573 	if (!svcdata)
1574 		goto auth_err;
1575 	rqstp->rq_auth_data = svcdata;
1576 	svcdata->verf_start = NULL;
1577 	svcdata->rsci = NULL;
1578 	gc = &svcdata->clcred;
1579 
1580 	/* start of rpc packet is 7 u32's back from here:
1581 	 * xid direction rpcversion prog vers proc flavour
1582 	 */
1583 	rpcstart = argv->iov_base;
1584 	rpcstart -= 7;
1585 
1586 	/* credential is:
1587 	 *   version(==1), proc(0,1,2,3), seq, service (1,2,3), handle
1588 	 * at least 5 u32s, and is preceded by length, so that makes 6.
1589 	 */
1590 
1591 	if (argv->iov_len < 5 * 4)
1592 		goto auth_err;
1593 	crlen = svc_getnl(argv);
1594 	if (svc_getnl(argv) != RPC_GSS_VERSION)
1595 		goto auth_err;
1596 	gc->gc_proc = svc_getnl(argv);
1597 	gc->gc_seq = svc_getnl(argv);
1598 	gc->gc_svc = svc_getnl(argv);
1599 	if (svc_safe_getnetobj(argv, &gc->gc_ctx))
1600 		goto auth_err;
1601 	if (crlen != round_up_to_quad(gc->gc_ctx.len) + 5 * 4)
1602 		goto auth_err;
1603 
1604 	if ((gc->gc_proc != RPC_GSS_PROC_DATA) && (rqstp->rq_proc != 0))
1605 		goto auth_err;
1606 
1607 	rqstp->rq_auth_stat = rpc_autherr_badverf;
1608 	switch (gc->gc_proc) {
1609 	case RPC_GSS_PROC_INIT:
1610 	case RPC_GSS_PROC_CONTINUE_INIT:
1611 		return svcauth_gss_proc_init(rqstp, gc);
1612 	case RPC_GSS_PROC_DATA:
1613 	case RPC_GSS_PROC_DESTROY:
1614 		/* Look up the context, and check the verifier: */
1615 		rqstp->rq_auth_stat = rpcsec_gsserr_credproblem;
1616 		rsci = gss_svc_searchbyctx(sn->rsc_cache, &gc->gc_ctx);
1617 		if (!rsci)
1618 			goto auth_err;
1619 		switch (gss_verify_header(rqstp, rsci, rpcstart, gc)) {
1620 		case SVC_OK:
1621 			break;
1622 		case SVC_DENIED:
1623 			goto auth_err;
1624 		case SVC_DROP:
1625 			goto drop;
1626 		}
1627 		break;
1628 	default:
1629 		rqstp->rq_auth_stat = rpc_autherr_rejectedcred;
1630 		goto auth_err;
1631 	}
1632 
1633 	/* now act upon the command: */
1634 	switch (gc->gc_proc) {
1635 	case RPC_GSS_PROC_DESTROY:
1636 		if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
1637 			goto auth_err;
1638 		/* Delete the entry from the cache_list and call cache_put */
1639 		sunrpc_cache_unhash(sn->rsc_cache, &rsci->h);
1640 		if (resv->iov_len + 4 > PAGE_SIZE)
1641 			goto drop;
1642 		svc_putnl(resv, RPC_SUCCESS);
1643 		goto complete;
1644 	case RPC_GSS_PROC_DATA:
1645 		rqstp->rq_auth_stat = rpcsec_gsserr_ctxproblem;
1646 		svcdata->verf_start = resv->iov_base + resv->iov_len;
1647 		if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
1648 			goto auth_err;
1649 		rqstp->rq_cred = rsci->cred;
1650 		get_group_info(rsci->cred.cr_group_info);
1651 		rqstp->rq_auth_stat = rpc_autherr_badcred;
1652 		switch (gc->gc_svc) {
1653 		case RPC_GSS_SVC_NONE:
1654 			svcxdr_init_decode(rqstp);
1655 			break;
1656 		case RPC_GSS_SVC_INTEGRITY:
1657 			/* placeholders for length and seq. number: */
1658 			svc_putnl(resv, 0);
1659 			svc_putnl(resv, 0);
1660 			if (unwrap_integ_data(rqstp, &rqstp->rq_arg,
1661 					gc->gc_seq, rsci->mechctx))
1662 				goto garbage_args;
1663 			rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE;
1664 			svcxdr_init_decode(rqstp);
1665 			break;
1666 		case RPC_GSS_SVC_PRIVACY:
1667 			/* placeholders for length and seq. number: */
1668 			svc_putnl(resv, 0);
1669 			svc_putnl(resv, 0);
1670 			if (unwrap_priv_data(rqstp, &rqstp->rq_arg,
1671 					gc->gc_seq, rsci->mechctx))
1672 				goto garbage_args;
1673 			rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE * 2;
1674 			svcxdr_init_decode(rqstp);
1675 			break;
1676 		default:
1677 			goto auth_err;
1678 		}
1679 		svcdata->rsci = rsci;
1680 		cache_get(&rsci->h);
1681 		rqstp->rq_cred.cr_flavor = gss_svc_to_pseudoflavor(
1682 					rsci->mechctx->mech_type,
1683 					GSS_C_QOP_DEFAULT,
1684 					gc->gc_svc);
1685 		ret = SVC_OK;
1686 		trace_rpcgss_svc_authenticate(rqstp, gc);
1687 		goto out;
1688 	}
1689 garbage_args:
1690 	ret = SVC_GARBAGE;
1691 	goto out;
1692 auth_err:
1693 	/* Restore write pointer to its original value: */
1694 	xdr_ressize_check(rqstp, reject_stat);
1695 	ret = SVC_DENIED;
1696 	goto out;
1697 complete:
1698 	ret = SVC_COMPLETE;
1699 	goto out;
1700 drop:
1701 	ret = SVC_CLOSE;
1702 out:
1703 	if (rsci)
1704 		cache_put(&rsci->h, sn->rsc_cache);
1705 	return ret;
1706 }
1707 
1708 static __be32 *
1709 svcauth_gss_prepare_to_wrap(struct xdr_buf *resbuf, struct gss_svc_data *gsd)
1710 {
1711 	__be32 *p;
1712 	u32 verf_len;
1713 
1714 	p = gsd->verf_start;
1715 	gsd->verf_start = NULL;
1716 
1717 	/* If the reply stat is nonzero, don't wrap: */
1718 	if (*(p-1) != rpc_success)
1719 		return NULL;
1720 	/* Skip the verifier: */
1721 	p += 1;
1722 	verf_len = ntohl(*p++);
1723 	p += XDR_QUADLEN(verf_len);
1724 	/* move accept_stat to right place: */
1725 	memcpy(p, p + 2, 4);
1726 	/* Also don't wrap if the accept stat is nonzero: */
1727 	if (*p != rpc_success) {
1728 		resbuf->head[0].iov_len -= 2 * 4;
1729 		return NULL;
1730 	}
1731 	p++;
1732 	return p;
1733 }
1734 
1735 static inline int
1736 svcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)
1737 {
1738 	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
1739 	struct rpc_gss_wire_cred *gc = &gsd->clcred;
1740 	struct xdr_buf *resbuf = &rqstp->rq_res;
1741 	struct xdr_buf integ_buf;
1742 	struct xdr_netobj mic;
1743 	struct kvec *resv;
1744 	__be32 *p;
1745 	int integ_offset, integ_len;
1746 	int stat = -EINVAL;
1747 
1748 	p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
1749 	if (p == NULL)
1750 		goto out;
1751 	integ_offset = (u8 *)(p + 1) - (u8 *)resbuf->head[0].iov_base;
1752 	integ_len = resbuf->len - integ_offset;
1753 	if (integ_len & 3)
1754 		goto out;
1755 	*p++ = htonl(integ_len);
1756 	*p++ = htonl(gc->gc_seq);
1757 	if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len)) {
1758 		WARN_ON_ONCE(1);
1759 		goto out_err;
1760 	}
1761 	if (resbuf->tail[0].iov_base == NULL) {
1762 		if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)
1763 			goto out_err;
1764 		resbuf->tail[0].iov_base = resbuf->head[0].iov_base
1765 						+ resbuf->head[0].iov_len;
1766 		resbuf->tail[0].iov_len = 0;
1767 	}
1768 	resv = &resbuf->tail[0];
1769 	mic.data = (u8 *)resv->iov_base + resv->iov_len + 4;
1770 	if (gss_get_mic(gsd->rsci->mechctx, &integ_buf, &mic))
1771 		goto out_err;
1772 	svc_putnl(resv, mic.len);
1773 	memset(mic.data + mic.len, 0,
1774 			round_up_to_quad(mic.len) - mic.len);
1775 	resv->iov_len += XDR_QUADLEN(mic.len) << 2;
1776 	/* not strictly required: */
1777 	resbuf->len += XDR_QUADLEN(mic.len) << 2;
1778 	if (resv->iov_len > PAGE_SIZE)
1779 		goto out_err;
1780 out:
1781 	stat = 0;
1782 out_err:
1783 	return stat;
1784 }
1785 
1786 static inline int
1787 svcauth_gss_wrap_resp_priv(struct svc_rqst *rqstp)
1788 {
1789 	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
1790 	struct rpc_gss_wire_cred *gc = &gsd->clcred;
1791 	struct xdr_buf *resbuf = &rqstp->rq_res;
1792 	struct page **inpages = NULL;
1793 	__be32 *p, *len;
1794 	int offset;
1795 	int pad;
1796 
1797 	p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
1798 	if (p == NULL)
1799 		return 0;
1800 	len = p++;
1801 	offset = (u8 *)p - (u8 *)resbuf->head[0].iov_base;
1802 	*p++ = htonl(gc->gc_seq);
1803 	inpages = resbuf->pages;
1804 	/* XXX: Would be better to write some xdr helper functions for
1805 	 * nfs{2,3,4}xdr.c that place the data right, instead of copying: */
1806 
1807 	/*
1808 	 * If there is currently tail data, make sure there is
1809 	 * room for the head, tail, and 2 * RPC_MAX_AUTH_SIZE in
1810 	 * the page, and move the current tail data such that
1811 	 * there is RPC_MAX_AUTH_SIZE slack space available in
1812 	 * both the head and tail.
1813 	 */
1814 	if (resbuf->tail[0].iov_base) {
1815 		if (resbuf->tail[0].iov_base >=
1816 			resbuf->head[0].iov_base + PAGE_SIZE)
1817 			return -EINVAL;
1818 		if (resbuf->tail[0].iov_base < resbuf->head[0].iov_base)
1819 			return -EINVAL;
1820 		if (resbuf->tail[0].iov_len + resbuf->head[0].iov_len
1821 				+ 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE)
1822 			return -ENOMEM;
1823 		memmove(resbuf->tail[0].iov_base + RPC_MAX_AUTH_SIZE,
1824 			resbuf->tail[0].iov_base,
1825 			resbuf->tail[0].iov_len);
1826 		resbuf->tail[0].iov_base += RPC_MAX_AUTH_SIZE;
1827 	}
1828 	/*
1829 	 * If there is no current tail data, make sure there is
1830 	 * room for the head data, and 2 * RPC_MAX_AUTH_SIZE in the
1831 	 * allotted page, and set up tail information such that there
1832 	 * is RPC_MAX_AUTH_SIZE slack space available in both the
1833 	 * head and tail.
1834 	 */
1835 	if (resbuf->tail[0].iov_base == NULL) {
1836 		if (resbuf->head[0].iov_len + 2*RPC_MAX_AUTH_SIZE > PAGE_SIZE)
1837 			return -ENOMEM;
1838 		resbuf->tail[0].iov_base = resbuf->head[0].iov_base
1839 			+ resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE;
1840 		resbuf->tail[0].iov_len = 0;
1841 	}
1842 	if (gss_wrap(gsd->rsci->mechctx, offset, resbuf, inpages))
1843 		return -ENOMEM;
1844 	*len = htonl(resbuf->len - offset);
1845 	pad = 3 - ((resbuf->len - offset - 1)&3);
1846 	p = (__be32 *)(resbuf->tail[0].iov_base + resbuf->tail[0].iov_len);
1847 	memset(p, 0, pad);
1848 	resbuf->tail[0].iov_len += pad;
1849 	resbuf->len += pad;
1850 	return 0;
1851 }
1852 
1853 static int
1854 svcauth_gss_release(struct svc_rqst *rqstp)
1855 {
1856 	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
1857 	struct rpc_gss_wire_cred *gc;
1858 	struct xdr_buf *resbuf = &rqstp->rq_res;
1859 	int stat = -EINVAL;
1860 	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
1861 
1862 	if (!gsd)
1863 		goto out;
1864 	gc = &gsd->clcred;
1865 	if (gc->gc_proc != RPC_GSS_PROC_DATA)
1866 		goto out;
1867 	/* Release can be called twice, but we only wrap once. */
1868 	if (gsd->verf_start == NULL)
1869 		goto out;
1870 	/* normally not set till svc_send, but we need it here: */
1871 	/* XXX: what for?  Do we mess it up the moment we call svc_putu32
1872 	 * or whatever? */
1873 	resbuf->len = total_buf_len(resbuf);
1874 	switch (gc->gc_svc) {
1875 	case RPC_GSS_SVC_NONE:
1876 		break;
1877 	case RPC_GSS_SVC_INTEGRITY:
1878 		stat = svcauth_gss_wrap_resp_integ(rqstp);
1879 		if (stat)
1880 			goto out_err;
1881 		break;
1882 	case RPC_GSS_SVC_PRIVACY:
1883 		stat = svcauth_gss_wrap_resp_priv(rqstp);
1884 		if (stat)
1885 			goto out_err;
1886 		break;
1887 	/*
1888 	 * For any other gc_svc value, svcauth_gss_accept() already set
1889 	 * the auth_error appropriately; just fall through:
1890 	 */
1891 	}
1892 
1893 out:
1894 	stat = 0;
1895 out_err:
1896 	if (rqstp->rq_client)
1897 		auth_domain_put(rqstp->rq_client);
1898 	rqstp->rq_client = NULL;
1899 	if (rqstp->rq_gssclient)
1900 		auth_domain_put(rqstp->rq_gssclient);
1901 	rqstp->rq_gssclient = NULL;
1902 	if (rqstp->rq_cred.cr_group_info)
1903 		put_group_info(rqstp->rq_cred.cr_group_info);
1904 	rqstp->rq_cred.cr_group_info = NULL;
1905 	if (gsd && gsd->rsci) {
1906 		cache_put(&gsd->rsci->h, sn->rsc_cache);
1907 		gsd->rsci = NULL;
1908 	}
1909 	return stat;
1910 }
1911 
1912 static void
1913 svcauth_gss_domain_release_rcu(struct rcu_head *head)
1914 {
1915 	struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head);
1916 	struct gss_domain *gd = container_of(dom, struct gss_domain, h);
1917 
1918 	kfree(dom->name);
1919 	kfree(gd);
1920 }
1921 
1922 static void
1923 svcauth_gss_domain_release(struct auth_domain *dom)
1924 {
1925 	call_rcu(&dom->rcu_head, svcauth_gss_domain_release_rcu);
1926 }
1927 
1928 static struct auth_ops svcauthops_gss = {
1929 	.name		= "rpcsec_gss",
1930 	.owner		= THIS_MODULE,
1931 	.flavour	= RPC_AUTH_GSS,
1932 	.accept		= svcauth_gss_accept,
1933 	.release	= svcauth_gss_release,
1934 	.domain_release = svcauth_gss_domain_release,
1935 	.set_client	= svcauth_gss_set_client,
1936 };
1937 
1938 static int rsi_cache_create_net(struct net *net)
1939 {
1940 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1941 	struct cache_detail *cd;
1942 	int err;
1943 
1944 	cd = cache_create_net(&rsi_cache_template, net);
1945 	if (IS_ERR(cd))
1946 		return PTR_ERR(cd);
1947 	err = cache_register_net(cd, net);
1948 	if (err) {
1949 		cache_destroy_net(cd, net);
1950 		return err;
1951 	}
1952 	sn->rsi_cache = cd;
1953 	return 0;
1954 }
1955 
1956 static void rsi_cache_destroy_net(struct net *net)
1957 {
1958 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1959 	struct cache_detail *cd = sn->rsi_cache;
1960 
1961 	sn->rsi_cache = NULL;
1962 	cache_purge(cd);
1963 	cache_unregister_net(cd, net);
1964 	cache_destroy_net(cd, net);
1965 }
1966 
1967 static int rsc_cache_create_net(struct net *net)
1968 {
1969 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1970 	struct cache_detail *cd;
1971 	int err;
1972 
1973 	cd = cache_create_net(&rsc_cache_template, net);
1974 	if (IS_ERR(cd))
1975 		return PTR_ERR(cd);
1976 	err = cache_register_net(cd, net);
1977 	if (err) {
1978 		cache_destroy_net(cd, net);
1979 		return err;
1980 	}
1981 	sn->rsc_cache = cd;
1982 	return 0;
1983 }
1984 
1985 static void rsc_cache_destroy_net(struct net *net)
1986 {
1987 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1988 	struct cache_detail *cd = sn->rsc_cache;
1989 
1990 	sn->rsc_cache = NULL;
1991 	cache_purge(cd);
1992 	cache_unregister_net(cd, net);
1993 	cache_destroy_net(cd, net);
1994 }
1995 
1996 int
1997 gss_svc_init_net(struct net *net)
1998 {
1999 	int rv;
2000 
2001 	rv = rsc_cache_create_net(net);
2002 	if (rv)
2003 		return rv;
2004 	rv = rsi_cache_create_net(net);
2005 	if (rv)
2006 		goto out1;
2007 	rv = create_use_gss_proxy_proc_entry(net);
2008 	if (rv)
2009 		goto out2;
2010 	return 0;
2011 out2:
2012 	rsi_cache_destroy_net(net);
2013 out1:
2014 	rsc_cache_destroy_net(net);
2015 	return rv;
2016 }
2017 
2018 void
2019 gss_svc_shutdown_net(struct net *net)
2020 {
2021 	destroy_use_gss_proxy_proc_entry(net);
2022 	rsi_cache_destroy_net(net);
2023 	rsc_cache_destroy_net(net);
2024 }
2025 
2026 int
2027 gss_svc_init(void)
2028 {
2029 	return svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);
2030 }
2031 
2032 void
2033 gss_svc_shutdown(void)
2034 {
2035 	svc_auth_unregister(RPC_AUTH_GSS);
2036 }
2037