18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Neil Brown <neilb@cse.unsw.edu.au>
48c2ecf20Sopenharmony_ci * J. Bruce Fields <bfields@umich.edu>
58c2ecf20Sopenharmony_ci * Andy Adamson <andros@umich.edu>
68c2ecf20Sopenharmony_ci * Dug Song <dugsong@monkey.org>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * RPCSEC_GSS server authentication.
98c2ecf20Sopenharmony_ci * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
108c2ecf20Sopenharmony_ci * (gssapi)
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * The RPCSEC_GSS involves three stages:
138c2ecf20Sopenharmony_ci *  1/ context creation
148c2ecf20Sopenharmony_ci *  2/ data exchange
158c2ecf20Sopenharmony_ci *  3/ context destruction
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * Context creation is handled largely by upcalls to user-space.
188c2ecf20Sopenharmony_ci *  In particular, GSS_Accept_sec_context is handled by an upcall
198c2ecf20Sopenharmony_ci * Data exchange is handled entirely within the kernel
208c2ecf20Sopenharmony_ci *  In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
218c2ecf20Sopenharmony_ci * Context destruction is handled in-kernel
228c2ecf20Sopenharmony_ci *  GSS_Delete_sec_context is in-kernel
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
258c2ecf20Sopenharmony_ci * The context handle and gss_token are used as a key into the rpcsec_init cache.
268c2ecf20Sopenharmony_ci * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
278c2ecf20Sopenharmony_ci * being major_status, minor_status, context_handle, reply_token.
288c2ecf20Sopenharmony_ci * These are sent back to the client.
298c2ecf20Sopenharmony_ci * Sequence window management is handled by the kernel.  The window size if currently
308c2ecf20Sopenharmony_ci * a compile time constant.
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * When user-space is happy that a context is established, it places an entry
338c2ecf20Sopenharmony_ci * in the rpcsec_context cache. The key for this cache is the context_handle.
348c2ecf20Sopenharmony_ci * The content includes:
358c2ecf20Sopenharmony_ci *   uid/gidlist - for determining access rights
368c2ecf20Sopenharmony_ci *   mechanism type
378c2ecf20Sopenharmony_ci *   mechanism specific information, such as a key
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#include <linux/slab.h>
428c2ecf20Sopenharmony_ci#include <linux/types.h>
438c2ecf20Sopenharmony_ci#include <linux/module.h>
448c2ecf20Sopenharmony_ci#include <linux/pagemap.h>
458c2ecf20Sopenharmony_ci#include <linux/user_namespace.h>
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#include <linux/sunrpc/auth_gss.h>
488c2ecf20Sopenharmony_ci#include <linux/sunrpc/gss_err.h>
498c2ecf20Sopenharmony_ci#include <linux/sunrpc/svcauth.h>
508c2ecf20Sopenharmony_ci#include <linux/sunrpc/svcauth_gss.h>
518c2ecf20Sopenharmony_ci#include <linux/sunrpc/cache.h>
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#include <trace/events/rpcgss.h>
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#include "gss_rpc_upcall.h"
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* The rpcsec_init cache is used for mapping RPCSEC_GSS_{,CONT_}INIT requests
598c2ecf20Sopenharmony_ci * into replies.
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci * Key is context handle (\x if empty) and gss_token.
628c2ecf20Sopenharmony_ci * Content is major_status minor_status (integers) context_handle, reply_token.
638c2ecf20Sopenharmony_ci *
648c2ecf20Sopenharmony_ci */
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic int netobj_equal(struct xdr_netobj *a, struct xdr_netobj *b)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	return a->len == b->len && 0 == memcmp(a->data, b->data, a->len);
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define	RSI_HASHBITS	6
728c2ecf20Sopenharmony_ci#define	RSI_HASHMAX	(1<<RSI_HASHBITS)
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistruct rsi {
758c2ecf20Sopenharmony_ci	struct cache_head	h;
768c2ecf20Sopenharmony_ci	struct xdr_netobj	in_handle, in_token;
778c2ecf20Sopenharmony_ci	struct xdr_netobj	out_handle, out_token;
788c2ecf20Sopenharmony_ci	int			major_status, minor_status;
798c2ecf20Sopenharmony_ci	struct rcu_head		rcu_head;
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old);
838c2ecf20Sopenharmony_cistatic struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic void rsi_free(struct rsi *rsii)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	kfree(rsii->in_handle.data);
888c2ecf20Sopenharmony_ci	kfree(rsii->in_token.data);
898c2ecf20Sopenharmony_ci	kfree(rsii->out_handle.data);
908c2ecf20Sopenharmony_ci	kfree(rsii->out_token.data);
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic void rsi_free_rcu(struct rcu_head *head)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	struct rsi *rsii = container_of(head, struct rsi, rcu_head);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	rsi_free(rsii);
988c2ecf20Sopenharmony_ci	kfree(rsii);
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic void rsi_put(struct kref *ref)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct rsi *rsii = container_of(ref, struct rsi, h.ref);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	call_rcu(&rsii->rcu_head, rsi_free_rcu);
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic inline int rsi_hash(struct rsi *item)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	return hash_mem(item->in_handle.data, item->in_handle.len, RSI_HASHBITS)
1118c2ecf20Sopenharmony_ci	     ^ hash_mem(item->in_token.data, item->in_token.len, RSI_HASHBITS);
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic int rsi_match(struct cache_head *a, struct cache_head *b)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	struct rsi *item = container_of(a, struct rsi, h);
1178c2ecf20Sopenharmony_ci	struct rsi *tmp = container_of(b, struct rsi, h);
1188c2ecf20Sopenharmony_ci	return netobj_equal(&item->in_handle, &tmp->in_handle) &&
1198c2ecf20Sopenharmony_ci	       netobj_equal(&item->in_token, &tmp->in_token);
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic int dup_to_netobj(struct xdr_netobj *dst, char *src, int len)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	dst->len = len;
1258c2ecf20Sopenharmony_ci	dst->data = (len ? kmemdup(src, len, GFP_KERNEL) : NULL);
1268c2ecf20Sopenharmony_ci	if (len && !dst->data)
1278c2ecf20Sopenharmony_ci		return -ENOMEM;
1288c2ecf20Sopenharmony_ci	return 0;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic inline int dup_netobj(struct xdr_netobj *dst, struct xdr_netobj *src)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	return dup_to_netobj(dst, src->data, src->len);
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic void rsi_init(struct cache_head *cnew, struct cache_head *citem)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	struct rsi *new = container_of(cnew, struct rsi, h);
1398c2ecf20Sopenharmony_ci	struct rsi *item = container_of(citem, struct rsi, h);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	new->out_handle.data = NULL;
1428c2ecf20Sopenharmony_ci	new->out_handle.len = 0;
1438c2ecf20Sopenharmony_ci	new->out_token.data = NULL;
1448c2ecf20Sopenharmony_ci	new->out_token.len = 0;
1458c2ecf20Sopenharmony_ci	new->in_handle.len = item->in_handle.len;
1468c2ecf20Sopenharmony_ci	item->in_handle.len = 0;
1478c2ecf20Sopenharmony_ci	new->in_token.len = item->in_token.len;
1488c2ecf20Sopenharmony_ci	item->in_token.len = 0;
1498c2ecf20Sopenharmony_ci	new->in_handle.data = item->in_handle.data;
1508c2ecf20Sopenharmony_ci	item->in_handle.data = NULL;
1518c2ecf20Sopenharmony_ci	new->in_token.data = item->in_token.data;
1528c2ecf20Sopenharmony_ci	item->in_token.data = NULL;
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic void update_rsi(struct cache_head *cnew, struct cache_head *citem)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	struct rsi *new = container_of(cnew, struct rsi, h);
1588c2ecf20Sopenharmony_ci	struct rsi *item = container_of(citem, struct rsi, h);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	BUG_ON(new->out_handle.data || new->out_token.data);
1618c2ecf20Sopenharmony_ci	new->out_handle.len = item->out_handle.len;
1628c2ecf20Sopenharmony_ci	item->out_handle.len = 0;
1638c2ecf20Sopenharmony_ci	new->out_token.len = item->out_token.len;
1648c2ecf20Sopenharmony_ci	item->out_token.len = 0;
1658c2ecf20Sopenharmony_ci	new->out_handle.data = item->out_handle.data;
1668c2ecf20Sopenharmony_ci	item->out_handle.data = NULL;
1678c2ecf20Sopenharmony_ci	new->out_token.data = item->out_token.data;
1688c2ecf20Sopenharmony_ci	item->out_token.data = NULL;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	new->major_status = item->major_status;
1718c2ecf20Sopenharmony_ci	new->minor_status = item->minor_status;
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic struct cache_head *rsi_alloc(void)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	struct rsi *rsii = kmalloc(sizeof(*rsii), GFP_KERNEL);
1778c2ecf20Sopenharmony_ci	if (rsii)
1788c2ecf20Sopenharmony_ci		return &rsii->h;
1798c2ecf20Sopenharmony_ci	else
1808c2ecf20Sopenharmony_ci		return NULL;
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic int rsi_upcall(struct cache_detail *cd, struct cache_head *h)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	return sunrpc_cache_pipe_upcall_timeout(cd, h);
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistatic void rsi_request(struct cache_detail *cd,
1898c2ecf20Sopenharmony_ci		       struct cache_head *h,
1908c2ecf20Sopenharmony_ci		       char **bpp, int *blen)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	struct rsi *rsii = container_of(h, struct rsi, h);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	qword_addhex(bpp, blen, rsii->in_handle.data, rsii->in_handle.len);
1958c2ecf20Sopenharmony_ci	qword_addhex(bpp, blen, rsii->in_token.data, rsii->in_token.len);
1968c2ecf20Sopenharmony_ci	(*bpp)[-1] = '\n';
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic int rsi_parse(struct cache_detail *cd,
2008c2ecf20Sopenharmony_ci		    char *mesg, int mlen)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	/* context token expiry major minor context token */
2038c2ecf20Sopenharmony_ci	char *buf = mesg;
2048c2ecf20Sopenharmony_ci	char *ep;
2058c2ecf20Sopenharmony_ci	int len;
2068c2ecf20Sopenharmony_ci	struct rsi rsii, *rsip = NULL;
2078c2ecf20Sopenharmony_ci	time64_t expiry;
2088c2ecf20Sopenharmony_ci	int status = -EINVAL;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	memset(&rsii, 0, sizeof(rsii));
2118c2ecf20Sopenharmony_ci	/* handle */
2128c2ecf20Sopenharmony_ci	len = qword_get(&mesg, buf, mlen);
2138c2ecf20Sopenharmony_ci	if (len < 0)
2148c2ecf20Sopenharmony_ci		goto out;
2158c2ecf20Sopenharmony_ci	status = -ENOMEM;
2168c2ecf20Sopenharmony_ci	if (dup_to_netobj(&rsii.in_handle, buf, len))
2178c2ecf20Sopenharmony_ci		goto out;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	/* token */
2208c2ecf20Sopenharmony_ci	len = qword_get(&mesg, buf, mlen);
2218c2ecf20Sopenharmony_ci	status = -EINVAL;
2228c2ecf20Sopenharmony_ci	if (len < 0)
2238c2ecf20Sopenharmony_ci		goto out;
2248c2ecf20Sopenharmony_ci	status = -ENOMEM;
2258c2ecf20Sopenharmony_ci	if (dup_to_netobj(&rsii.in_token, buf, len))
2268c2ecf20Sopenharmony_ci		goto out;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	rsip = rsi_lookup(cd, &rsii);
2298c2ecf20Sopenharmony_ci	if (!rsip)
2308c2ecf20Sopenharmony_ci		goto out;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	rsii.h.flags = 0;
2338c2ecf20Sopenharmony_ci	/* expiry */
2348c2ecf20Sopenharmony_ci	expiry = get_expiry(&mesg);
2358c2ecf20Sopenharmony_ci	status = -EINVAL;
2368c2ecf20Sopenharmony_ci	if (expiry == 0)
2378c2ecf20Sopenharmony_ci		goto out;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* major/minor */
2408c2ecf20Sopenharmony_ci	len = qword_get(&mesg, buf, mlen);
2418c2ecf20Sopenharmony_ci	if (len <= 0)
2428c2ecf20Sopenharmony_ci		goto out;
2438c2ecf20Sopenharmony_ci	rsii.major_status = simple_strtoul(buf, &ep, 10);
2448c2ecf20Sopenharmony_ci	if (*ep)
2458c2ecf20Sopenharmony_ci		goto out;
2468c2ecf20Sopenharmony_ci	len = qword_get(&mesg, buf, mlen);
2478c2ecf20Sopenharmony_ci	if (len <= 0)
2488c2ecf20Sopenharmony_ci		goto out;
2498c2ecf20Sopenharmony_ci	rsii.minor_status = simple_strtoul(buf, &ep, 10);
2508c2ecf20Sopenharmony_ci	if (*ep)
2518c2ecf20Sopenharmony_ci		goto out;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	/* out_handle */
2548c2ecf20Sopenharmony_ci	len = qword_get(&mesg, buf, mlen);
2558c2ecf20Sopenharmony_ci	if (len < 0)
2568c2ecf20Sopenharmony_ci		goto out;
2578c2ecf20Sopenharmony_ci	status = -ENOMEM;
2588c2ecf20Sopenharmony_ci	if (dup_to_netobj(&rsii.out_handle, buf, len))
2598c2ecf20Sopenharmony_ci		goto out;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	/* out_token */
2628c2ecf20Sopenharmony_ci	len = qword_get(&mesg, buf, mlen);
2638c2ecf20Sopenharmony_ci	status = -EINVAL;
2648c2ecf20Sopenharmony_ci	if (len < 0)
2658c2ecf20Sopenharmony_ci		goto out;
2668c2ecf20Sopenharmony_ci	status = -ENOMEM;
2678c2ecf20Sopenharmony_ci	if (dup_to_netobj(&rsii.out_token, buf, len))
2688c2ecf20Sopenharmony_ci		goto out;
2698c2ecf20Sopenharmony_ci	rsii.h.expiry_time = expiry;
2708c2ecf20Sopenharmony_ci	rsip = rsi_update(cd, &rsii, rsip);
2718c2ecf20Sopenharmony_ci	status = 0;
2728c2ecf20Sopenharmony_ciout:
2738c2ecf20Sopenharmony_ci	rsi_free(&rsii);
2748c2ecf20Sopenharmony_ci	if (rsip)
2758c2ecf20Sopenharmony_ci		cache_put(&rsip->h, cd);
2768c2ecf20Sopenharmony_ci	else
2778c2ecf20Sopenharmony_ci		status = -ENOMEM;
2788c2ecf20Sopenharmony_ci	return status;
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic const struct cache_detail rsi_cache_template = {
2828c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
2838c2ecf20Sopenharmony_ci	.hash_size	= RSI_HASHMAX,
2848c2ecf20Sopenharmony_ci	.name           = "auth.rpcsec.init",
2858c2ecf20Sopenharmony_ci	.cache_put      = rsi_put,
2868c2ecf20Sopenharmony_ci	.cache_upcall	= rsi_upcall,
2878c2ecf20Sopenharmony_ci	.cache_request  = rsi_request,
2888c2ecf20Sopenharmony_ci	.cache_parse    = rsi_parse,
2898c2ecf20Sopenharmony_ci	.match		= rsi_match,
2908c2ecf20Sopenharmony_ci	.init		= rsi_init,
2918c2ecf20Sopenharmony_ci	.update		= update_rsi,
2928c2ecf20Sopenharmony_ci	.alloc		= rsi_alloc,
2938c2ecf20Sopenharmony_ci};
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item)
2968c2ecf20Sopenharmony_ci{
2978c2ecf20Sopenharmony_ci	struct cache_head *ch;
2988c2ecf20Sopenharmony_ci	int hash = rsi_hash(item);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
3018c2ecf20Sopenharmony_ci	if (ch)
3028c2ecf20Sopenharmony_ci		return container_of(ch, struct rsi, h);
3038c2ecf20Sopenharmony_ci	else
3048c2ecf20Sopenharmony_ci		return NULL;
3058c2ecf20Sopenharmony_ci}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cistatic struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old)
3088c2ecf20Sopenharmony_ci{
3098c2ecf20Sopenharmony_ci	struct cache_head *ch;
3108c2ecf20Sopenharmony_ci	int hash = rsi_hash(new);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	ch = sunrpc_cache_update(cd, &new->h,
3138c2ecf20Sopenharmony_ci				 &old->h, hash);
3148c2ecf20Sopenharmony_ci	if (ch)
3158c2ecf20Sopenharmony_ci		return container_of(ch, struct rsi, h);
3168c2ecf20Sopenharmony_ci	else
3178c2ecf20Sopenharmony_ci		return NULL;
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci/*
3228c2ecf20Sopenharmony_ci * The rpcsec_context cache is used to store a context that is
3238c2ecf20Sopenharmony_ci * used in data exchange.
3248c2ecf20Sopenharmony_ci * The key is a context handle. The content is:
3258c2ecf20Sopenharmony_ci *  uid, gidlist, mechanism, service-set, mech-specific-data
3268c2ecf20Sopenharmony_ci */
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci#define	RSC_HASHBITS	10
3298c2ecf20Sopenharmony_ci#define	RSC_HASHMAX	(1<<RSC_HASHBITS)
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci#define GSS_SEQ_WIN	128
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_cistruct gss_svc_seq_data {
3348c2ecf20Sopenharmony_ci	/* highest seq number seen so far: */
3358c2ecf20Sopenharmony_ci	u32			sd_max;
3368c2ecf20Sopenharmony_ci	/* for i such that sd_max-GSS_SEQ_WIN < i <= sd_max, the i-th bit of
3378c2ecf20Sopenharmony_ci	 * sd_win is nonzero iff sequence number i has been seen already: */
3388c2ecf20Sopenharmony_ci	unsigned long		sd_win[GSS_SEQ_WIN/BITS_PER_LONG];
3398c2ecf20Sopenharmony_ci	spinlock_t		sd_lock;
3408c2ecf20Sopenharmony_ci};
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_cistruct rsc {
3438c2ecf20Sopenharmony_ci	struct cache_head	h;
3448c2ecf20Sopenharmony_ci	struct xdr_netobj	handle;
3458c2ecf20Sopenharmony_ci	struct svc_cred		cred;
3468c2ecf20Sopenharmony_ci	struct gss_svc_seq_data	seqdata;
3478c2ecf20Sopenharmony_ci	struct gss_ctx		*mechctx;
3488c2ecf20Sopenharmony_ci	struct rcu_head		rcu_head;
3498c2ecf20Sopenharmony_ci};
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old);
3528c2ecf20Sopenharmony_cistatic struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic void rsc_free(struct rsc *rsci)
3558c2ecf20Sopenharmony_ci{
3568c2ecf20Sopenharmony_ci	kfree(rsci->handle.data);
3578c2ecf20Sopenharmony_ci	if (rsci->mechctx)
3588c2ecf20Sopenharmony_ci		gss_delete_sec_context(&rsci->mechctx);
3598c2ecf20Sopenharmony_ci	free_svc_cred(&rsci->cred);
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cistatic void rsc_free_rcu(struct rcu_head *head)
3638c2ecf20Sopenharmony_ci{
3648c2ecf20Sopenharmony_ci	struct rsc *rsci = container_of(head, struct rsc, rcu_head);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	kfree(rsci->handle.data);
3678c2ecf20Sopenharmony_ci	kfree(rsci);
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_cistatic void rsc_put(struct kref *ref)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	struct rsc *rsci = container_of(ref, struct rsc, h.ref);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	if (rsci->mechctx)
3758c2ecf20Sopenharmony_ci		gss_delete_sec_context(&rsci->mechctx);
3768c2ecf20Sopenharmony_ci	free_svc_cred(&rsci->cred);
3778c2ecf20Sopenharmony_ci	call_rcu(&rsci->rcu_head, rsc_free_rcu);
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic inline int
3818c2ecf20Sopenharmony_cirsc_hash(struct rsc *rsci)
3828c2ecf20Sopenharmony_ci{
3838c2ecf20Sopenharmony_ci	return hash_mem(rsci->handle.data, rsci->handle.len, RSC_HASHBITS);
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic int
3878c2ecf20Sopenharmony_cirsc_match(struct cache_head *a, struct cache_head *b)
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	struct rsc *new = container_of(a, struct rsc, h);
3908c2ecf20Sopenharmony_ci	struct rsc *tmp = container_of(b, struct rsc, h);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	return netobj_equal(&new->handle, &tmp->handle);
3938c2ecf20Sopenharmony_ci}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_cistatic void
3968c2ecf20Sopenharmony_cirsc_init(struct cache_head *cnew, struct cache_head *ctmp)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	struct rsc *new = container_of(cnew, struct rsc, h);
3998c2ecf20Sopenharmony_ci	struct rsc *tmp = container_of(ctmp, struct rsc, h);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	new->handle.len = tmp->handle.len;
4028c2ecf20Sopenharmony_ci	tmp->handle.len = 0;
4038c2ecf20Sopenharmony_ci	new->handle.data = tmp->handle.data;
4048c2ecf20Sopenharmony_ci	tmp->handle.data = NULL;
4058c2ecf20Sopenharmony_ci	new->mechctx = NULL;
4068c2ecf20Sopenharmony_ci	init_svc_cred(&new->cred);
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_cistatic void
4108c2ecf20Sopenharmony_ciupdate_rsc(struct cache_head *cnew, struct cache_head *ctmp)
4118c2ecf20Sopenharmony_ci{
4128c2ecf20Sopenharmony_ci	struct rsc *new = container_of(cnew, struct rsc, h);
4138c2ecf20Sopenharmony_ci	struct rsc *tmp = container_of(ctmp, struct rsc, h);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	new->mechctx = tmp->mechctx;
4168c2ecf20Sopenharmony_ci	tmp->mechctx = NULL;
4178c2ecf20Sopenharmony_ci	memset(&new->seqdata, 0, sizeof(new->seqdata));
4188c2ecf20Sopenharmony_ci	spin_lock_init(&new->seqdata.sd_lock);
4198c2ecf20Sopenharmony_ci	new->cred = tmp->cred;
4208c2ecf20Sopenharmony_ci	init_svc_cred(&tmp->cred);
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_cistatic struct cache_head *
4248c2ecf20Sopenharmony_cirsc_alloc(void)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	struct rsc *rsci = kmalloc(sizeof(*rsci), GFP_KERNEL);
4278c2ecf20Sopenharmony_ci	if (rsci)
4288c2ecf20Sopenharmony_ci		return &rsci->h;
4298c2ecf20Sopenharmony_ci	else
4308c2ecf20Sopenharmony_ci		return NULL;
4318c2ecf20Sopenharmony_ci}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_cistatic int rsc_upcall(struct cache_detail *cd, struct cache_head *h)
4348c2ecf20Sopenharmony_ci{
4358c2ecf20Sopenharmony_ci	return -EINVAL;
4368c2ecf20Sopenharmony_ci}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_cistatic int rsc_parse(struct cache_detail *cd,
4398c2ecf20Sopenharmony_ci		     char *mesg, int mlen)
4408c2ecf20Sopenharmony_ci{
4418c2ecf20Sopenharmony_ci	/* contexthandle expiry [ uid gid N <n gids> mechname ...mechdata... ] */
4428c2ecf20Sopenharmony_ci	char *buf = mesg;
4438c2ecf20Sopenharmony_ci	int id;
4448c2ecf20Sopenharmony_ci	int len, rv;
4458c2ecf20Sopenharmony_ci	struct rsc rsci, *rscp = NULL;
4468c2ecf20Sopenharmony_ci	time64_t expiry;
4478c2ecf20Sopenharmony_ci	int status = -EINVAL;
4488c2ecf20Sopenharmony_ci	struct gss_api_mech *gm = NULL;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	memset(&rsci, 0, sizeof(rsci));
4518c2ecf20Sopenharmony_ci	/* context handle */
4528c2ecf20Sopenharmony_ci	len = qword_get(&mesg, buf, mlen);
4538c2ecf20Sopenharmony_ci	if (len < 0) goto out;
4548c2ecf20Sopenharmony_ci	status = -ENOMEM;
4558c2ecf20Sopenharmony_ci	if (dup_to_netobj(&rsci.handle, buf, len))
4568c2ecf20Sopenharmony_ci		goto out;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	rsci.h.flags = 0;
4598c2ecf20Sopenharmony_ci	/* expiry */
4608c2ecf20Sopenharmony_ci	expiry = get_expiry(&mesg);
4618c2ecf20Sopenharmony_ci	status = -EINVAL;
4628c2ecf20Sopenharmony_ci	if (expiry == 0)
4638c2ecf20Sopenharmony_ci		goto out;
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	rscp = rsc_lookup(cd, &rsci);
4668c2ecf20Sopenharmony_ci	if (!rscp)
4678c2ecf20Sopenharmony_ci		goto out;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	/* uid, or NEGATIVE */
4708c2ecf20Sopenharmony_ci	rv = get_int(&mesg, &id);
4718c2ecf20Sopenharmony_ci	if (rv == -EINVAL)
4728c2ecf20Sopenharmony_ci		goto out;
4738c2ecf20Sopenharmony_ci	if (rv == -ENOENT)
4748c2ecf20Sopenharmony_ci		set_bit(CACHE_NEGATIVE, &rsci.h.flags);
4758c2ecf20Sopenharmony_ci	else {
4768c2ecf20Sopenharmony_ci		int N, i;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci		/*
4798c2ecf20Sopenharmony_ci		 * NOTE: we skip uid_valid()/gid_valid() checks here:
4808c2ecf20Sopenharmony_ci		 * instead, * -1 id's are later mapped to the
4818c2ecf20Sopenharmony_ci		 * (export-specific) anonymous id by nfsd_setuser.
4828c2ecf20Sopenharmony_ci		 *
4838c2ecf20Sopenharmony_ci		 * (But supplementary gid's get no such special
4848c2ecf20Sopenharmony_ci		 * treatment so are checked for validity here.)
4858c2ecf20Sopenharmony_ci		 */
4868c2ecf20Sopenharmony_ci		/* uid */
4878c2ecf20Sopenharmony_ci		rsci.cred.cr_uid = make_kuid(current_user_ns(), id);
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci		/* gid */
4908c2ecf20Sopenharmony_ci		if (get_int(&mesg, &id))
4918c2ecf20Sopenharmony_ci			goto out;
4928c2ecf20Sopenharmony_ci		rsci.cred.cr_gid = make_kgid(current_user_ns(), id);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci		/* number of additional gid's */
4958c2ecf20Sopenharmony_ci		if (get_int(&mesg, &N))
4968c2ecf20Sopenharmony_ci			goto out;
4978c2ecf20Sopenharmony_ci		if (N < 0 || N > NGROUPS_MAX)
4988c2ecf20Sopenharmony_ci			goto out;
4998c2ecf20Sopenharmony_ci		status = -ENOMEM;
5008c2ecf20Sopenharmony_ci		rsci.cred.cr_group_info = groups_alloc(N);
5018c2ecf20Sopenharmony_ci		if (rsci.cred.cr_group_info == NULL)
5028c2ecf20Sopenharmony_ci			goto out;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci		/* gid's */
5058c2ecf20Sopenharmony_ci		status = -EINVAL;
5068c2ecf20Sopenharmony_ci		for (i=0; i<N; i++) {
5078c2ecf20Sopenharmony_ci			kgid_t kgid;
5088c2ecf20Sopenharmony_ci			if (get_int(&mesg, &id))
5098c2ecf20Sopenharmony_ci				goto out;
5108c2ecf20Sopenharmony_ci			kgid = make_kgid(current_user_ns(), id);
5118c2ecf20Sopenharmony_ci			if (!gid_valid(kgid))
5128c2ecf20Sopenharmony_ci				goto out;
5138c2ecf20Sopenharmony_ci			rsci.cred.cr_group_info->gid[i] = kgid;
5148c2ecf20Sopenharmony_ci		}
5158c2ecf20Sopenharmony_ci		groups_sort(rsci.cred.cr_group_info);
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci		/* mech name */
5188c2ecf20Sopenharmony_ci		len = qword_get(&mesg, buf, mlen);
5198c2ecf20Sopenharmony_ci		if (len < 0)
5208c2ecf20Sopenharmony_ci			goto out;
5218c2ecf20Sopenharmony_ci		gm = rsci.cred.cr_gss_mech = gss_mech_get_by_name(buf);
5228c2ecf20Sopenharmony_ci		status = -EOPNOTSUPP;
5238c2ecf20Sopenharmony_ci		if (!gm)
5248c2ecf20Sopenharmony_ci			goto out;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci		status = -EINVAL;
5278c2ecf20Sopenharmony_ci		/* mech-specific data: */
5288c2ecf20Sopenharmony_ci		len = qword_get(&mesg, buf, mlen);
5298c2ecf20Sopenharmony_ci		if (len < 0)
5308c2ecf20Sopenharmony_ci			goto out;
5318c2ecf20Sopenharmony_ci		status = gss_import_sec_context(buf, len, gm, &rsci.mechctx,
5328c2ecf20Sopenharmony_ci						NULL, GFP_KERNEL);
5338c2ecf20Sopenharmony_ci		if (status)
5348c2ecf20Sopenharmony_ci			goto out;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci		/* get client name */
5378c2ecf20Sopenharmony_ci		len = qword_get(&mesg, buf, mlen);
5388c2ecf20Sopenharmony_ci		if (len > 0) {
5398c2ecf20Sopenharmony_ci			rsci.cred.cr_principal = kstrdup(buf, GFP_KERNEL);
5408c2ecf20Sopenharmony_ci			if (!rsci.cred.cr_principal) {
5418c2ecf20Sopenharmony_ci				status = -ENOMEM;
5428c2ecf20Sopenharmony_ci				goto out;
5438c2ecf20Sopenharmony_ci			}
5448c2ecf20Sopenharmony_ci		}
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	}
5478c2ecf20Sopenharmony_ci	rsci.h.expiry_time = expiry;
5488c2ecf20Sopenharmony_ci	rscp = rsc_update(cd, &rsci, rscp);
5498c2ecf20Sopenharmony_ci	status = 0;
5508c2ecf20Sopenharmony_ciout:
5518c2ecf20Sopenharmony_ci	rsc_free(&rsci);
5528c2ecf20Sopenharmony_ci	if (rscp)
5538c2ecf20Sopenharmony_ci		cache_put(&rscp->h, cd);
5548c2ecf20Sopenharmony_ci	else
5558c2ecf20Sopenharmony_ci		status = -ENOMEM;
5568c2ecf20Sopenharmony_ci	return status;
5578c2ecf20Sopenharmony_ci}
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_cistatic const struct cache_detail rsc_cache_template = {
5608c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
5618c2ecf20Sopenharmony_ci	.hash_size	= RSC_HASHMAX,
5628c2ecf20Sopenharmony_ci	.name		= "auth.rpcsec.context",
5638c2ecf20Sopenharmony_ci	.cache_put	= rsc_put,
5648c2ecf20Sopenharmony_ci	.cache_upcall	= rsc_upcall,
5658c2ecf20Sopenharmony_ci	.cache_parse	= rsc_parse,
5668c2ecf20Sopenharmony_ci	.match		= rsc_match,
5678c2ecf20Sopenharmony_ci	.init		= rsc_init,
5688c2ecf20Sopenharmony_ci	.update		= update_rsc,
5698c2ecf20Sopenharmony_ci	.alloc		= rsc_alloc,
5708c2ecf20Sopenharmony_ci};
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_cistatic struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item)
5738c2ecf20Sopenharmony_ci{
5748c2ecf20Sopenharmony_ci	struct cache_head *ch;
5758c2ecf20Sopenharmony_ci	int hash = rsc_hash(item);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	ch = sunrpc_cache_lookup_rcu(cd, &item->h, hash);
5788c2ecf20Sopenharmony_ci	if (ch)
5798c2ecf20Sopenharmony_ci		return container_of(ch, struct rsc, h);
5808c2ecf20Sopenharmony_ci	else
5818c2ecf20Sopenharmony_ci		return NULL;
5828c2ecf20Sopenharmony_ci}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_cistatic struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old)
5858c2ecf20Sopenharmony_ci{
5868c2ecf20Sopenharmony_ci	struct cache_head *ch;
5878c2ecf20Sopenharmony_ci	int hash = rsc_hash(new);
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	ch = sunrpc_cache_update(cd, &new->h,
5908c2ecf20Sopenharmony_ci				 &old->h, hash);
5918c2ecf20Sopenharmony_ci	if (ch)
5928c2ecf20Sopenharmony_ci		return container_of(ch, struct rsc, h);
5938c2ecf20Sopenharmony_ci	else
5948c2ecf20Sopenharmony_ci		return NULL;
5958c2ecf20Sopenharmony_ci}
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_cistatic struct rsc *
5998c2ecf20Sopenharmony_cigss_svc_searchbyctx(struct cache_detail *cd, struct xdr_netobj *handle)
6008c2ecf20Sopenharmony_ci{
6018c2ecf20Sopenharmony_ci	struct rsc rsci;
6028c2ecf20Sopenharmony_ci	struct rsc *found;
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	memset(&rsci, 0, sizeof(rsci));
6058c2ecf20Sopenharmony_ci	if (dup_to_netobj(&rsci.handle, handle->data, handle->len))
6068c2ecf20Sopenharmony_ci		return NULL;
6078c2ecf20Sopenharmony_ci	found = rsc_lookup(cd, &rsci);
6088c2ecf20Sopenharmony_ci	rsc_free(&rsci);
6098c2ecf20Sopenharmony_ci	if (!found)
6108c2ecf20Sopenharmony_ci		return NULL;
6118c2ecf20Sopenharmony_ci	if (cache_check(cd, &found->h, NULL))
6128c2ecf20Sopenharmony_ci		return NULL;
6138c2ecf20Sopenharmony_ci	return found;
6148c2ecf20Sopenharmony_ci}
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci/**
6178c2ecf20Sopenharmony_ci * gss_check_seq_num - GSS sequence number window check
6188c2ecf20Sopenharmony_ci * @rqstp: RPC Call to use when reporting errors
6198c2ecf20Sopenharmony_ci * @rsci: cached GSS context state (updated on return)
6208c2ecf20Sopenharmony_ci * @seq_num: sequence number to check
6218c2ecf20Sopenharmony_ci *
6228c2ecf20Sopenharmony_ci * Implements sequence number algorithm as specified in
6238c2ecf20Sopenharmony_ci * RFC 2203, Section 5.3.3.1. "Context Management".
6248c2ecf20Sopenharmony_ci *
6258c2ecf20Sopenharmony_ci * Return values:
6268c2ecf20Sopenharmony_ci *   %true: @rqstp's GSS sequence number is inside the window
6278c2ecf20Sopenharmony_ci *   %false: @rqstp's GSS sequence number is outside the window
6288c2ecf20Sopenharmony_ci */
6298c2ecf20Sopenharmony_cistatic bool gss_check_seq_num(const struct svc_rqst *rqstp, struct rsc *rsci,
6308c2ecf20Sopenharmony_ci			      u32 seq_num)
6318c2ecf20Sopenharmony_ci{
6328c2ecf20Sopenharmony_ci	struct gss_svc_seq_data *sd = &rsci->seqdata;
6338c2ecf20Sopenharmony_ci	bool result = false;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	spin_lock(&sd->sd_lock);
6368c2ecf20Sopenharmony_ci	if (seq_num > sd->sd_max) {
6378c2ecf20Sopenharmony_ci		if (seq_num >= sd->sd_max + GSS_SEQ_WIN) {
6388c2ecf20Sopenharmony_ci			memset(sd->sd_win, 0, sizeof(sd->sd_win));
6398c2ecf20Sopenharmony_ci			sd->sd_max = seq_num;
6408c2ecf20Sopenharmony_ci		} else while (sd->sd_max < seq_num) {
6418c2ecf20Sopenharmony_ci			sd->sd_max++;
6428c2ecf20Sopenharmony_ci			__clear_bit(sd->sd_max % GSS_SEQ_WIN, sd->sd_win);
6438c2ecf20Sopenharmony_ci		}
6448c2ecf20Sopenharmony_ci		__set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win);
6458c2ecf20Sopenharmony_ci		goto ok;
6468c2ecf20Sopenharmony_ci	} else if (seq_num + GSS_SEQ_WIN <= sd->sd_max) {
6478c2ecf20Sopenharmony_ci		goto toolow;
6488c2ecf20Sopenharmony_ci	}
6498c2ecf20Sopenharmony_ci	if (__test_and_set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win))
6508c2ecf20Sopenharmony_ci		goto alreadyseen;
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ciok:
6538c2ecf20Sopenharmony_ci	result = true;
6548c2ecf20Sopenharmony_ciout:
6558c2ecf20Sopenharmony_ci	spin_unlock(&sd->sd_lock);
6568c2ecf20Sopenharmony_ci	return result;
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_citoolow:
6598c2ecf20Sopenharmony_ci	trace_rpcgss_svc_seqno_low(rqstp, seq_num,
6608c2ecf20Sopenharmony_ci				   sd->sd_max - GSS_SEQ_WIN,
6618c2ecf20Sopenharmony_ci				   sd->sd_max);
6628c2ecf20Sopenharmony_ci	goto out;
6638c2ecf20Sopenharmony_cialreadyseen:
6648c2ecf20Sopenharmony_ci	trace_rpcgss_svc_seqno_seen(rqstp, seq_num);
6658c2ecf20Sopenharmony_ci	goto out;
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_cistatic inline u32 round_up_to_quad(u32 i)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	return (i + 3 ) & ~3;
6718c2ecf20Sopenharmony_ci}
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_cistatic inline int
6748c2ecf20Sopenharmony_cisvc_safe_getnetobj(struct kvec *argv, struct xdr_netobj *o)
6758c2ecf20Sopenharmony_ci{
6768c2ecf20Sopenharmony_ci	int l;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	if (argv->iov_len < 4)
6798c2ecf20Sopenharmony_ci		return -1;
6808c2ecf20Sopenharmony_ci	o->len = svc_getnl(argv);
6818c2ecf20Sopenharmony_ci	l = round_up_to_quad(o->len);
6828c2ecf20Sopenharmony_ci	if (argv->iov_len < l)
6838c2ecf20Sopenharmony_ci		return -1;
6848c2ecf20Sopenharmony_ci	o->data = argv->iov_base;
6858c2ecf20Sopenharmony_ci	argv->iov_base += l;
6868c2ecf20Sopenharmony_ci	argv->iov_len -= l;
6878c2ecf20Sopenharmony_ci	return 0;
6888c2ecf20Sopenharmony_ci}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_cistatic inline int
6918c2ecf20Sopenharmony_cisvc_safe_putnetobj(struct kvec *resv, struct xdr_netobj *o)
6928c2ecf20Sopenharmony_ci{
6938c2ecf20Sopenharmony_ci	u8 *p;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	if (resv->iov_len + 4 > PAGE_SIZE)
6968c2ecf20Sopenharmony_ci		return -1;
6978c2ecf20Sopenharmony_ci	svc_putnl(resv, o->len);
6988c2ecf20Sopenharmony_ci	p = resv->iov_base + resv->iov_len;
6998c2ecf20Sopenharmony_ci	resv->iov_len += round_up_to_quad(o->len);
7008c2ecf20Sopenharmony_ci	if (resv->iov_len > PAGE_SIZE)
7018c2ecf20Sopenharmony_ci		return -1;
7028c2ecf20Sopenharmony_ci	memcpy(p, o->data, o->len);
7038c2ecf20Sopenharmony_ci	memset(p + o->len, 0, round_up_to_quad(o->len) - o->len);
7048c2ecf20Sopenharmony_ci	return 0;
7058c2ecf20Sopenharmony_ci}
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci/*
7088c2ecf20Sopenharmony_ci * Verify the checksum on the header and return SVC_OK on success.
7098c2ecf20Sopenharmony_ci * Otherwise, return SVC_DROP (in the case of a bad sequence number)
7108c2ecf20Sopenharmony_ci * or return SVC_DENIED and indicate error in authp.
7118c2ecf20Sopenharmony_ci */
7128c2ecf20Sopenharmony_cistatic int
7138c2ecf20Sopenharmony_cigss_verify_header(struct svc_rqst *rqstp, struct rsc *rsci,
7148c2ecf20Sopenharmony_ci		  __be32 *rpcstart, struct rpc_gss_wire_cred *gc, __be32 *authp)
7158c2ecf20Sopenharmony_ci{
7168c2ecf20Sopenharmony_ci	struct gss_ctx		*ctx_id = rsci->mechctx;
7178c2ecf20Sopenharmony_ci	struct xdr_buf		rpchdr;
7188c2ecf20Sopenharmony_ci	struct xdr_netobj	checksum;
7198c2ecf20Sopenharmony_ci	u32			flavor = 0;
7208c2ecf20Sopenharmony_ci	struct kvec		*argv = &rqstp->rq_arg.head[0];
7218c2ecf20Sopenharmony_ci	struct kvec		iov;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	/* data to compute the checksum over: */
7248c2ecf20Sopenharmony_ci	iov.iov_base = rpcstart;
7258c2ecf20Sopenharmony_ci	iov.iov_len = (u8 *)argv->iov_base - (u8 *)rpcstart;
7268c2ecf20Sopenharmony_ci	xdr_buf_from_iov(&iov, &rpchdr);
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	*authp = rpc_autherr_badverf;
7298c2ecf20Sopenharmony_ci	if (argv->iov_len < 4)
7308c2ecf20Sopenharmony_ci		return SVC_DENIED;
7318c2ecf20Sopenharmony_ci	flavor = svc_getnl(argv);
7328c2ecf20Sopenharmony_ci	if (flavor != RPC_AUTH_GSS)
7338c2ecf20Sopenharmony_ci		return SVC_DENIED;
7348c2ecf20Sopenharmony_ci	if (svc_safe_getnetobj(argv, &checksum))
7358c2ecf20Sopenharmony_ci		return SVC_DENIED;
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	if (rqstp->rq_deferred) /* skip verification of revisited request */
7388c2ecf20Sopenharmony_ci		return SVC_OK;
7398c2ecf20Sopenharmony_ci	if (gss_verify_mic(ctx_id, &rpchdr, &checksum) != GSS_S_COMPLETE) {
7408c2ecf20Sopenharmony_ci		*authp = rpcsec_gsserr_credproblem;
7418c2ecf20Sopenharmony_ci		return SVC_DENIED;
7428c2ecf20Sopenharmony_ci	}
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	if (gc->gc_seq > MAXSEQ) {
7458c2ecf20Sopenharmony_ci		trace_rpcgss_svc_seqno_large(rqstp, gc->gc_seq);
7468c2ecf20Sopenharmony_ci		*authp = rpcsec_gsserr_ctxproblem;
7478c2ecf20Sopenharmony_ci		return SVC_DENIED;
7488c2ecf20Sopenharmony_ci	}
7498c2ecf20Sopenharmony_ci	if (!gss_check_seq_num(rqstp, rsci, gc->gc_seq))
7508c2ecf20Sopenharmony_ci		return SVC_DROP;
7518c2ecf20Sopenharmony_ci	return SVC_OK;
7528c2ecf20Sopenharmony_ci}
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_cistatic int
7558c2ecf20Sopenharmony_cigss_write_null_verf(struct svc_rqst *rqstp)
7568c2ecf20Sopenharmony_ci{
7578c2ecf20Sopenharmony_ci	__be32     *p;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	svc_putnl(rqstp->rq_res.head, RPC_AUTH_NULL);
7608c2ecf20Sopenharmony_ci	p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
7618c2ecf20Sopenharmony_ci	/* don't really need to check if head->iov_len > PAGE_SIZE ... */
7628c2ecf20Sopenharmony_ci	*p++ = 0;
7638c2ecf20Sopenharmony_ci	if (!xdr_ressize_check(rqstp, p))
7648c2ecf20Sopenharmony_ci		return -1;
7658c2ecf20Sopenharmony_ci	return 0;
7668c2ecf20Sopenharmony_ci}
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_cistatic int
7698c2ecf20Sopenharmony_cigss_write_verf(struct svc_rqst *rqstp, struct gss_ctx *ctx_id, u32 seq)
7708c2ecf20Sopenharmony_ci{
7718c2ecf20Sopenharmony_ci	__be32			*xdr_seq;
7728c2ecf20Sopenharmony_ci	u32			maj_stat;
7738c2ecf20Sopenharmony_ci	struct xdr_buf		verf_data;
7748c2ecf20Sopenharmony_ci	struct xdr_netobj	mic;
7758c2ecf20Sopenharmony_ci	__be32			*p;
7768c2ecf20Sopenharmony_ci	struct kvec		iov;
7778c2ecf20Sopenharmony_ci	int err = -1;
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	svc_putnl(rqstp->rq_res.head, RPC_AUTH_GSS);
7808c2ecf20Sopenharmony_ci	xdr_seq = kmalloc(4, GFP_KERNEL);
7818c2ecf20Sopenharmony_ci	if (!xdr_seq)
7828c2ecf20Sopenharmony_ci		return -1;
7838c2ecf20Sopenharmony_ci	*xdr_seq = htonl(seq);
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	iov.iov_base = xdr_seq;
7868c2ecf20Sopenharmony_ci	iov.iov_len = 4;
7878c2ecf20Sopenharmony_ci	xdr_buf_from_iov(&iov, &verf_data);
7888c2ecf20Sopenharmony_ci	p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
7898c2ecf20Sopenharmony_ci	mic.data = (u8 *)(p + 1);
7908c2ecf20Sopenharmony_ci	maj_stat = gss_get_mic(ctx_id, &verf_data, &mic);
7918c2ecf20Sopenharmony_ci	if (maj_stat != GSS_S_COMPLETE)
7928c2ecf20Sopenharmony_ci		goto out;
7938c2ecf20Sopenharmony_ci	*p++ = htonl(mic.len);
7948c2ecf20Sopenharmony_ci	memset((u8 *)p + mic.len, 0, round_up_to_quad(mic.len) - mic.len);
7958c2ecf20Sopenharmony_ci	p += XDR_QUADLEN(mic.len);
7968c2ecf20Sopenharmony_ci	if (!xdr_ressize_check(rqstp, p))
7978c2ecf20Sopenharmony_ci		goto out;
7988c2ecf20Sopenharmony_ci	err = 0;
7998c2ecf20Sopenharmony_ciout:
8008c2ecf20Sopenharmony_ci	kfree(xdr_seq);
8018c2ecf20Sopenharmony_ci	return err;
8028c2ecf20Sopenharmony_ci}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_cistruct gss_domain {
8058c2ecf20Sopenharmony_ci	struct auth_domain	h;
8068c2ecf20Sopenharmony_ci	u32			pseudoflavor;
8078c2ecf20Sopenharmony_ci};
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_cistatic struct auth_domain *
8108c2ecf20Sopenharmony_cifind_gss_auth_domain(struct gss_ctx *ctx, u32 svc)
8118c2ecf20Sopenharmony_ci{
8128c2ecf20Sopenharmony_ci	char *name;
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	name = gss_service_to_auth_domain_name(ctx->mech_type, svc);
8158c2ecf20Sopenharmony_ci	if (!name)
8168c2ecf20Sopenharmony_ci		return NULL;
8178c2ecf20Sopenharmony_ci	return auth_domain_find(name);
8188c2ecf20Sopenharmony_ci}
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_cistatic struct auth_ops svcauthops_gss;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ciu32 svcauth_gss_flavor(struct auth_domain *dom)
8238c2ecf20Sopenharmony_ci{
8248c2ecf20Sopenharmony_ci	struct gss_domain *gd = container_of(dom, struct gss_domain, h);
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	return gd->pseudoflavor;
8278c2ecf20Sopenharmony_ci}
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svcauth_gss_flavor);
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_cistruct auth_domain *
8328c2ecf20Sopenharmony_cisvcauth_gss_register_pseudoflavor(u32 pseudoflavor, char * name)
8338c2ecf20Sopenharmony_ci{
8348c2ecf20Sopenharmony_ci	struct gss_domain	*new;
8358c2ecf20Sopenharmony_ci	struct auth_domain	*test;
8368c2ecf20Sopenharmony_ci	int			stat = -ENOMEM;
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	new = kmalloc(sizeof(*new), GFP_KERNEL);
8398c2ecf20Sopenharmony_ci	if (!new)
8408c2ecf20Sopenharmony_ci		goto out;
8418c2ecf20Sopenharmony_ci	kref_init(&new->h.ref);
8428c2ecf20Sopenharmony_ci	new->h.name = kstrdup(name, GFP_KERNEL);
8438c2ecf20Sopenharmony_ci	if (!new->h.name)
8448c2ecf20Sopenharmony_ci		goto out_free_dom;
8458c2ecf20Sopenharmony_ci	new->h.flavour = &svcauthops_gss;
8468c2ecf20Sopenharmony_ci	new->pseudoflavor = pseudoflavor;
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	test = auth_domain_lookup(name, &new->h);
8498c2ecf20Sopenharmony_ci	if (test != &new->h) {
8508c2ecf20Sopenharmony_ci		pr_warn("svc: duplicate registration of gss pseudo flavour %s.\n",
8518c2ecf20Sopenharmony_ci			name);
8528c2ecf20Sopenharmony_ci		stat = -EADDRINUSE;
8538c2ecf20Sopenharmony_ci		auth_domain_put(test);
8548c2ecf20Sopenharmony_ci		goto out_free_name;
8558c2ecf20Sopenharmony_ci	}
8568c2ecf20Sopenharmony_ci	return test;
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ciout_free_name:
8598c2ecf20Sopenharmony_ci	kfree(new->h.name);
8608c2ecf20Sopenharmony_ciout_free_dom:
8618c2ecf20Sopenharmony_ci	kfree(new);
8628c2ecf20Sopenharmony_ciout:
8638c2ecf20Sopenharmony_ci	return ERR_PTR(stat);
8648c2ecf20Sopenharmony_ci}
8658c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svcauth_gss_register_pseudoflavor);
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_cistatic inline int
8688c2ecf20Sopenharmony_ciread_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj)
8698c2ecf20Sopenharmony_ci{
8708c2ecf20Sopenharmony_ci	__be32  raw;
8718c2ecf20Sopenharmony_ci	int     status;
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
8748c2ecf20Sopenharmony_ci	if (status)
8758c2ecf20Sopenharmony_ci		return status;
8768c2ecf20Sopenharmony_ci	*obj = ntohl(raw);
8778c2ecf20Sopenharmony_ci	return 0;
8788c2ecf20Sopenharmony_ci}
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci/* It would be nice if this bit of code could be shared with the client.
8818c2ecf20Sopenharmony_ci * Obstacles:
8828c2ecf20Sopenharmony_ci *	The client shouldn't malloc(), would have to pass in own memory.
8838c2ecf20Sopenharmony_ci *	The server uses base of head iovec as read pointer, while the
8848c2ecf20Sopenharmony_ci *	client uses separate pointer. */
8858c2ecf20Sopenharmony_cistatic int
8868c2ecf20Sopenharmony_ciunwrap_integ_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
8878c2ecf20Sopenharmony_ci{
8888c2ecf20Sopenharmony_ci	u32 integ_len, rseqno, maj_stat;
8898c2ecf20Sopenharmony_ci	int stat = -EINVAL;
8908c2ecf20Sopenharmony_ci	struct xdr_netobj mic;
8918c2ecf20Sopenharmony_ci	struct xdr_buf integ_buf;
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	mic.data = NULL;
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	/* NFS READ normally uses splice to send data in-place. However
8968c2ecf20Sopenharmony_ci	 * the data in cache can change after the reply's MIC is computed
8978c2ecf20Sopenharmony_ci	 * but before the RPC reply is sent. To prevent the client from
8988c2ecf20Sopenharmony_ci	 * rejecting the server-computed MIC in this somewhat rare case,
8998c2ecf20Sopenharmony_ci	 * do not use splice with the GSS integrity service.
9008c2ecf20Sopenharmony_ci	 */
9018c2ecf20Sopenharmony_ci	clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	/* Did we already verify the signature on the original pass through? */
9048c2ecf20Sopenharmony_ci	if (rqstp->rq_deferred)
9058c2ecf20Sopenharmony_ci		return 0;
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	integ_len = svc_getnl(&buf->head[0]);
9088c2ecf20Sopenharmony_ci	if (integ_len & 3)
9098c2ecf20Sopenharmony_ci		goto unwrap_failed;
9108c2ecf20Sopenharmony_ci	if (integ_len > buf->len)
9118c2ecf20Sopenharmony_ci		goto unwrap_failed;
9128c2ecf20Sopenharmony_ci	if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))
9138c2ecf20Sopenharmony_ci		goto unwrap_failed;
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	/* copy out mic... */
9168c2ecf20Sopenharmony_ci	if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
9178c2ecf20Sopenharmony_ci		goto unwrap_failed;
9188c2ecf20Sopenharmony_ci	if (mic.len > RPC_MAX_AUTH_SIZE)
9198c2ecf20Sopenharmony_ci		goto unwrap_failed;
9208c2ecf20Sopenharmony_ci	mic.data = kmalloc(mic.len, GFP_KERNEL);
9218c2ecf20Sopenharmony_ci	if (!mic.data)
9228c2ecf20Sopenharmony_ci		goto unwrap_failed;
9238c2ecf20Sopenharmony_ci	if (read_bytes_from_xdr_buf(buf, integ_len + 4, mic.data, mic.len))
9248c2ecf20Sopenharmony_ci		goto unwrap_failed;
9258c2ecf20Sopenharmony_ci	maj_stat = gss_verify_mic(ctx, &integ_buf, &mic);
9268c2ecf20Sopenharmony_ci	if (maj_stat != GSS_S_COMPLETE)
9278c2ecf20Sopenharmony_ci		goto bad_mic;
9288c2ecf20Sopenharmony_ci	rseqno = svc_getnl(&buf->head[0]);
9298c2ecf20Sopenharmony_ci	if (rseqno != seq)
9308c2ecf20Sopenharmony_ci		goto bad_seqno;
9318c2ecf20Sopenharmony_ci	/* trim off the mic and padding at the end before returning */
9328c2ecf20Sopenharmony_ci	xdr_buf_trim(buf, round_up_to_quad(mic.len) + 4);
9338c2ecf20Sopenharmony_ci	stat = 0;
9348c2ecf20Sopenharmony_ciout:
9358c2ecf20Sopenharmony_ci	kfree(mic.data);
9368c2ecf20Sopenharmony_ci	return stat;
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ciunwrap_failed:
9398c2ecf20Sopenharmony_ci	trace_rpcgss_svc_unwrap_failed(rqstp);
9408c2ecf20Sopenharmony_ci	goto out;
9418c2ecf20Sopenharmony_cibad_seqno:
9428c2ecf20Sopenharmony_ci	trace_rpcgss_svc_seqno_bad(rqstp, seq, rseqno);
9438c2ecf20Sopenharmony_ci	goto out;
9448c2ecf20Sopenharmony_cibad_mic:
9458c2ecf20Sopenharmony_ci	trace_rpcgss_svc_mic(rqstp, maj_stat);
9468c2ecf20Sopenharmony_ci	goto out;
9478c2ecf20Sopenharmony_ci}
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_cistatic inline int
9508c2ecf20Sopenharmony_citotal_buf_len(struct xdr_buf *buf)
9518c2ecf20Sopenharmony_ci{
9528c2ecf20Sopenharmony_ci	return buf->head[0].iov_len + buf->page_len + buf->tail[0].iov_len;
9538c2ecf20Sopenharmony_ci}
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_cistatic void
9568c2ecf20Sopenharmony_cifix_priv_head(struct xdr_buf *buf, int pad)
9578c2ecf20Sopenharmony_ci{
9588c2ecf20Sopenharmony_ci	if (buf->page_len == 0) {
9598c2ecf20Sopenharmony_ci		/* We need to adjust head and buf->len in tandem in this
9608c2ecf20Sopenharmony_ci		 * case to make svc_defer() work--it finds the original
9618c2ecf20Sopenharmony_ci		 * buffer start using buf->len - buf->head[0].iov_len. */
9628c2ecf20Sopenharmony_ci		buf->head[0].iov_len -= pad;
9638c2ecf20Sopenharmony_ci	}
9648c2ecf20Sopenharmony_ci}
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_cistatic int
9678c2ecf20Sopenharmony_ciunwrap_priv_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
9688c2ecf20Sopenharmony_ci{
9698c2ecf20Sopenharmony_ci	u32 priv_len, maj_stat;
9708c2ecf20Sopenharmony_ci	int pad, remaining_len, offset;
9718c2ecf20Sopenharmony_ci	u32 rseqno;
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci	clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci	priv_len = svc_getnl(&buf->head[0]);
9768c2ecf20Sopenharmony_ci	if (rqstp->rq_deferred) {
9778c2ecf20Sopenharmony_ci		/* Already decrypted last time through! The sequence number
9788c2ecf20Sopenharmony_ci		 * check at out_seq is unnecessary but harmless: */
9798c2ecf20Sopenharmony_ci		goto out_seq;
9808c2ecf20Sopenharmony_ci	}
9818c2ecf20Sopenharmony_ci	/* buf->len is the number of bytes from the original start of the
9828c2ecf20Sopenharmony_ci	 * request to the end, where head[0].iov_len is just the bytes
9838c2ecf20Sopenharmony_ci	 * not yet read from the head, so these two values are different: */
9848c2ecf20Sopenharmony_ci	remaining_len = total_buf_len(buf);
9858c2ecf20Sopenharmony_ci	if (priv_len > remaining_len)
9868c2ecf20Sopenharmony_ci		goto unwrap_failed;
9878c2ecf20Sopenharmony_ci	pad = remaining_len - priv_len;
9888c2ecf20Sopenharmony_ci	buf->len -= pad;
9898c2ecf20Sopenharmony_ci	fix_priv_head(buf, pad);
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	maj_stat = gss_unwrap(ctx, 0, priv_len, buf);
9928c2ecf20Sopenharmony_ci	pad = priv_len - buf->len;
9938c2ecf20Sopenharmony_ci	/* The upper layers assume the buffer is aligned on 4-byte boundaries.
9948c2ecf20Sopenharmony_ci	 * In the krb5p case, at least, the data ends up offset, so we need to
9958c2ecf20Sopenharmony_ci	 * move it around. */
9968c2ecf20Sopenharmony_ci	/* XXX: This is very inefficient.  It would be better to either do
9978c2ecf20Sopenharmony_ci	 * this while we encrypt, or maybe in the receive code, if we can peak
9988c2ecf20Sopenharmony_ci	 * ahead and work out the service and mechanism there. */
9998c2ecf20Sopenharmony_ci	offset = xdr_pad_size(buf->head[0].iov_len);
10008c2ecf20Sopenharmony_ci	if (offset) {
10018c2ecf20Sopenharmony_ci		buf->buflen = RPCSVC_MAXPAYLOAD;
10028c2ecf20Sopenharmony_ci		xdr_shift_buf(buf, offset);
10038c2ecf20Sopenharmony_ci		fix_priv_head(buf, pad);
10048c2ecf20Sopenharmony_ci	}
10058c2ecf20Sopenharmony_ci	if (maj_stat != GSS_S_COMPLETE)
10068c2ecf20Sopenharmony_ci		goto bad_unwrap;
10078c2ecf20Sopenharmony_ciout_seq:
10088c2ecf20Sopenharmony_ci	rseqno = svc_getnl(&buf->head[0]);
10098c2ecf20Sopenharmony_ci	if (rseqno != seq)
10108c2ecf20Sopenharmony_ci		goto bad_seqno;
10118c2ecf20Sopenharmony_ci	return 0;
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ciunwrap_failed:
10148c2ecf20Sopenharmony_ci	trace_rpcgss_svc_unwrap_failed(rqstp);
10158c2ecf20Sopenharmony_ci	return -EINVAL;
10168c2ecf20Sopenharmony_cibad_seqno:
10178c2ecf20Sopenharmony_ci	trace_rpcgss_svc_seqno_bad(rqstp, seq, rseqno);
10188c2ecf20Sopenharmony_ci	return -EINVAL;
10198c2ecf20Sopenharmony_cibad_unwrap:
10208c2ecf20Sopenharmony_ci	trace_rpcgss_svc_unwrap(rqstp, maj_stat);
10218c2ecf20Sopenharmony_ci	return -EINVAL;
10228c2ecf20Sopenharmony_ci}
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_cistruct gss_svc_data {
10258c2ecf20Sopenharmony_ci	/* decoded gss client cred: */
10268c2ecf20Sopenharmony_ci	struct rpc_gss_wire_cred	clcred;
10278c2ecf20Sopenharmony_ci	/* save a pointer to the beginning of the encoded verifier,
10288c2ecf20Sopenharmony_ci	 * for use in encryption/checksumming in svcauth_gss_release: */
10298c2ecf20Sopenharmony_ci	__be32				*verf_start;
10308c2ecf20Sopenharmony_ci	struct rsc			*rsci;
10318c2ecf20Sopenharmony_ci};
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_cistatic int
10348c2ecf20Sopenharmony_cisvcauth_gss_set_client(struct svc_rqst *rqstp)
10358c2ecf20Sopenharmony_ci{
10368c2ecf20Sopenharmony_ci	struct gss_svc_data *svcdata = rqstp->rq_auth_data;
10378c2ecf20Sopenharmony_ci	struct rsc *rsci = svcdata->rsci;
10388c2ecf20Sopenharmony_ci	struct rpc_gss_wire_cred *gc = &svcdata->clcred;
10398c2ecf20Sopenharmony_ci	int stat;
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci	/*
10428c2ecf20Sopenharmony_ci	 * A gss export can be specified either by:
10438c2ecf20Sopenharmony_ci	 * 	export	*(sec=krb5,rw)
10448c2ecf20Sopenharmony_ci	 * or by
10458c2ecf20Sopenharmony_ci	 * 	export gss/krb5(rw)
10468c2ecf20Sopenharmony_ci	 * The latter is deprecated; but for backwards compatibility reasons
10478c2ecf20Sopenharmony_ci	 * the nfsd code will still fall back on trying it if the former
10488c2ecf20Sopenharmony_ci	 * doesn't work; so we try to make both available to nfsd, below.
10498c2ecf20Sopenharmony_ci	 */
10508c2ecf20Sopenharmony_ci	rqstp->rq_gssclient = find_gss_auth_domain(rsci->mechctx, gc->gc_svc);
10518c2ecf20Sopenharmony_ci	if (rqstp->rq_gssclient == NULL)
10528c2ecf20Sopenharmony_ci		return SVC_DENIED;
10538c2ecf20Sopenharmony_ci	stat = svcauth_unix_set_client(rqstp);
10548c2ecf20Sopenharmony_ci	if (stat == SVC_DROP || stat == SVC_CLOSE)
10558c2ecf20Sopenharmony_ci		return stat;
10568c2ecf20Sopenharmony_ci	return SVC_OK;
10578c2ecf20Sopenharmony_ci}
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_cistatic inline int
10608c2ecf20Sopenharmony_cigss_write_init_verf(struct cache_detail *cd, struct svc_rqst *rqstp,
10618c2ecf20Sopenharmony_ci		struct xdr_netobj *out_handle, int *major_status)
10628c2ecf20Sopenharmony_ci{
10638c2ecf20Sopenharmony_ci	struct rsc *rsci;
10648c2ecf20Sopenharmony_ci	int        rc;
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	if (*major_status != GSS_S_COMPLETE)
10678c2ecf20Sopenharmony_ci		return gss_write_null_verf(rqstp);
10688c2ecf20Sopenharmony_ci	rsci = gss_svc_searchbyctx(cd, out_handle);
10698c2ecf20Sopenharmony_ci	if (rsci == NULL) {
10708c2ecf20Sopenharmony_ci		*major_status = GSS_S_NO_CONTEXT;
10718c2ecf20Sopenharmony_ci		return gss_write_null_verf(rqstp);
10728c2ecf20Sopenharmony_ci	}
10738c2ecf20Sopenharmony_ci	rc = gss_write_verf(rqstp, rsci->mechctx, GSS_SEQ_WIN);
10748c2ecf20Sopenharmony_ci	cache_put(&rsci->h, cd);
10758c2ecf20Sopenharmony_ci	return rc;
10768c2ecf20Sopenharmony_ci}
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_cistatic inline int
10798c2ecf20Sopenharmony_cigss_read_common_verf(struct rpc_gss_wire_cred *gc,
10808c2ecf20Sopenharmony_ci		     struct kvec *argv, __be32 *authp,
10818c2ecf20Sopenharmony_ci		     struct xdr_netobj *in_handle)
10828c2ecf20Sopenharmony_ci{
10838c2ecf20Sopenharmony_ci	/* Read the verifier; should be NULL: */
10848c2ecf20Sopenharmony_ci	*authp = rpc_autherr_badverf;
10858c2ecf20Sopenharmony_ci	if (argv->iov_len < 2 * 4)
10868c2ecf20Sopenharmony_ci		return SVC_DENIED;
10878c2ecf20Sopenharmony_ci	if (svc_getnl(argv) != RPC_AUTH_NULL)
10888c2ecf20Sopenharmony_ci		return SVC_DENIED;
10898c2ecf20Sopenharmony_ci	if (svc_getnl(argv) != 0)
10908c2ecf20Sopenharmony_ci		return SVC_DENIED;
10918c2ecf20Sopenharmony_ci	/* Martial context handle and token for upcall: */
10928c2ecf20Sopenharmony_ci	*authp = rpc_autherr_badcred;
10938c2ecf20Sopenharmony_ci	if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0)
10948c2ecf20Sopenharmony_ci		return SVC_DENIED;
10958c2ecf20Sopenharmony_ci	if (dup_netobj(in_handle, &gc->gc_ctx))
10968c2ecf20Sopenharmony_ci		return SVC_CLOSE;
10978c2ecf20Sopenharmony_ci	*authp = rpc_autherr_badverf;
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	return 0;
11008c2ecf20Sopenharmony_ci}
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_cistatic inline int
11038c2ecf20Sopenharmony_cigss_read_verf(struct rpc_gss_wire_cred *gc,
11048c2ecf20Sopenharmony_ci	      struct kvec *argv, __be32 *authp,
11058c2ecf20Sopenharmony_ci	      struct xdr_netobj *in_handle,
11068c2ecf20Sopenharmony_ci	      struct xdr_netobj *in_token)
11078c2ecf20Sopenharmony_ci{
11088c2ecf20Sopenharmony_ci	struct xdr_netobj tmpobj;
11098c2ecf20Sopenharmony_ci	int res;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	res = gss_read_common_verf(gc, argv, authp, in_handle);
11128c2ecf20Sopenharmony_ci	if (res)
11138c2ecf20Sopenharmony_ci		return res;
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	if (svc_safe_getnetobj(argv, &tmpobj)) {
11168c2ecf20Sopenharmony_ci		kfree(in_handle->data);
11178c2ecf20Sopenharmony_ci		return SVC_DENIED;
11188c2ecf20Sopenharmony_ci	}
11198c2ecf20Sopenharmony_ci	if (dup_netobj(in_token, &tmpobj)) {
11208c2ecf20Sopenharmony_ci		kfree(in_handle->data);
11218c2ecf20Sopenharmony_ci		return SVC_CLOSE;
11228c2ecf20Sopenharmony_ci	}
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci	return 0;
11258c2ecf20Sopenharmony_ci}
11268c2ecf20Sopenharmony_ci
11278c2ecf20Sopenharmony_cistatic void gss_free_in_token_pages(struct gssp_in_token *in_token)
11288c2ecf20Sopenharmony_ci{
11298c2ecf20Sopenharmony_ci	u32 inlen;
11308c2ecf20Sopenharmony_ci	int i;
11318c2ecf20Sopenharmony_ci
11328c2ecf20Sopenharmony_ci	i = 0;
11338c2ecf20Sopenharmony_ci	inlen = in_token->page_len;
11348c2ecf20Sopenharmony_ci	while (inlen) {
11358c2ecf20Sopenharmony_ci		if (in_token->pages[i])
11368c2ecf20Sopenharmony_ci			put_page(in_token->pages[i]);
11378c2ecf20Sopenharmony_ci		inlen -= inlen > PAGE_SIZE ? PAGE_SIZE : inlen;
11388c2ecf20Sopenharmony_ci	}
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	kfree(in_token->pages);
11418c2ecf20Sopenharmony_ci	in_token->pages = NULL;
11428c2ecf20Sopenharmony_ci}
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_cistatic int gss_read_proxy_verf(struct svc_rqst *rqstp,
11458c2ecf20Sopenharmony_ci			       struct rpc_gss_wire_cred *gc, __be32 *authp,
11468c2ecf20Sopenharmony_ci			       struct xdr_netobj *in_handle,
11478c2ecf20Sopenharmony_ci			       struct gssp_in_token *in_token)
11488c2ecf20Sopenharmony_ci{
11498c2ecf20Sopenharmony_ci	struct kvec *argv = &rqstp->rq_arg.head[0];
11508c2ecf20Sopenharmony_ci	unsigned int length, pgto_offs, pgfrom_offs;
11518c2ecf20Sopenharmony_ci	int pages, i, res, pgto, pgfrom;
11528c2ecf20Sopenharmony_ci	size_t inlen, to_offs, from_offs;
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci	res = gss_read_common_verf(gc, argv, authp, in_handle);
11558c2ecf20Sopenharmony_ci	if (res)
11568c2ecf20Sopenharmony_ci		return res;
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	inlen = svc_getnl(argv);
11598c2ecf20Sopenharmony_ci	if (inlen > (argv->iov_len + rqstp->rq_arg.page_len)) {
11608c2ecf20Sopenharmony_ci		kfree(in_handle->data);
11618c2ecf20Sopenharmony_ci		return SVC_DENIED;
11628c2ecf20Sopenharmony_ci	}
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_ci	pages = DIV_ROUND_UP(inlen, PAGE_SIZE);
11658c2ecf20Sopenharmony_ci	in_token->pages = kcalloc(pages, sizeof(struct page *), GFP_KERNEL);
11668c2ecf20Sopenharmony_ci	if (!in_token->pages) {
11678c2ecf20Sopenharmony_ci		kfree(in_handle->data);
11688c2ecf20Sopenharmony_ci		return SVC_DENIED;
11698c2ecf20Sopenharmony_ci	}
11708c2ecf20Sopenharmony_ci	in_token->page_base = 0;
11718c2ecf20Sopenharmony_ci	in_token->page_len = inlen;
11728c2ecf20Sopenharmony_ci	for (i = 0; i < pages; i++) {
11738c2ecf20Sopenharmony_ci		in_token->pages[i] = alloc_page(GFP_KERNEL);
11748c2ecf20Sopenharmony_ci		if (!in_token->pages[i]) {
11758c2ecf20Sopenharmony_ci			kfree(in_handle->data);
11768c2ecf20Sopenharmony_ci			gss_free_in_token_pages(in_token);
11778c2ecf20Sopenharmony_ci			return SVC_DENIED;
11788c2ecf20Sopenharmony_ci		}
11798c2ecf20Sopenharmony_ci	}
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_ci	length = min_t(unsigned int, inlen, argv->iov_len);
11828c2ecf20Sopenharmony_ci	memcpy(page_address(in_token->pages[0]), argv->iov_base, length);
11838c2ecf20Sopenharmony_ci	inlen -= length;
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	to_offs = length;
11868c2ecf20Sopenharmony_ci	from_offs = rqstp->rq_arg.page_base;
11878c2ecf20Sopenharmony_ci	while (inlen) {
11888c2ecf20Sopenharmony_ci		pgto = to_offs >> PAGE_SHIFT;
11898c2ecf20Sopenharmony_ci		pgfrom = from_offs >> PAGE_SHIFT;
11908c2ecf20Sopenharmony_ci		pgto_offs = to_offs & ~PAGE_MASK;
11918c2ecf20Sopenharmony_ci		pgfrom_offs = from_offs & ~PAGE_MASK;
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci		length = min_t(unsigned int, inlen,
11948c2ecf20Sopenharmony_ci			 min_t(unsigned int, PAGE_SIZE - pgto_offs,
11958c2ecf20Sopenharmony_ci			       PAGE_SIZE - pgfrom_offs));
11968c2ecf20Sopenharmony_ci		memcpy(page_address(in_token->pages[pgto]) + pgto_offs,
11978c2ecf20Sopenharmony_ci		       page_address(rqstp->rq_arg.pages[pgfrom]) + pgfrom_offs,
11988c2ecf20Sopenharmony_ci		       length);
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci		to_offs += length;
12018c2ecf20Sopenharmony_ci		from_offs += length;
12028c2ecf20Sopenharmony_ci		inlen -= length;
12038c2ecf20Sopenharmony_ci	}
12048c2ecf20Sopenharmony_ci	return 0;
12058c2ecf20Sopenharmony_ci}
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_cistatic inline int
12088c2ecf20Sopenharmony_cigss_write_resv(struct kvec *resv, size_t size_limit,
12098c2ecf20Sopenharmony_ci	       struct xdr_netobj *out_handle, struct xdr_netobj *out_token,
12108c2ecf20Sopenharmony_ci	       int major_status, int minor_status)
12118c2ecf20Sopenharmony_ci{
12128c2ecf20Sopenharmony_ci	if (resv->iov_len + 4 > size_limit)
12138c2ecf20Sopenharmony_ci		return -1;
12148c2ecf20Sopenharmony_ci	svc_putnl(resv, RPC_SUCCESS);
12158c2ecf20Sopenharmony_ci	if (svc_safe_putnetobj(resv, out_handle))
12168c2ecf20Sopenharmony_ci		return -1;
12178c2ecf20Sopenharmony_ci	if (resv->iov_len + 3 * 4 > size_limit)
12188c2ecf20Sopenharmony_ci		return -1;
12198c2ecf20Sopenharmony_ci	svc_putnl(resv, major_status);
12208c2ecf20Sopenharmony_ci	svc_putnl(resv, minor_status);
12218c2ecf20Sopenharmony_ci	svc_putnl(resv, GSS_SEQ_WIN);
12228c2ecf20Sopenharmony_ci	if (svc_safe_putnetobj(resv, out_token))
12238c2ecf20Sopenharmony_ci		return -1;
12248c2ecf20Sopenharmony_ci	return 0;
12258c2ecf20Sopenharmony_ci}
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_ci/*
12288c2ecf20Sopenharmony_ci * Having read the cred already and found we're in the context
12298c2ecf20Sopenharmony_ci * initiation case, read the verifier and initiate (or check the results
12308c2ecf20Sopenharmony_ci * of) upcalls to userspace for help with context initiation.  If
12318c2ecf20Sopenharmony_ci * the upcall results are available, write the verifier and result.
12328c2ecf20Sopenharmony_ci * Otherwise, drop the request pending an answer to the upcall.
12338c2ecf20Sopenharmony_ci */
12348c2ecf20Sopenharmony_cistatic int svcauth_gss_legacy_init(struct svc_rqst *rqstp,
12358c2ecf20Sopenharmony_ci			struct rpc_gss_wire_cred *gc, __be32 *authp)
12368c2ecf20Sopenharmony_ci{
12378c2ecf20Sopenharmony_ci	struct kvec *argv = &rqstp->rq_arg.head[0];
12388c2ecf20Sopenharmony_ci	struct kvec *resv = &rqstp->rq_res.head[0];
12398c2ecf20Sopenharmony_ci	struct rsi *rsip, rsikey;
12408c2ecf20Sopenharmony_ci	int ret;
12418c2ecf20Sopenharmony_ci	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci	memset(&rsikey, 0, sizeof(rsikey));
12448c2ecf20Sopenharmony_ci	ret = gss_read_verf(gc, argv, authp,
12458c2ecf20Sopenharmony_ci			    &rsikey.in_handle, &rsikey.in_token);
12468c2ecf20Sopenharmony_ci	if (ret)
12478c2ecf20Sopenharmony_ci		return ret;
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci	/* Perform upcall, or find upcall result: */
12508c2ecf20Sopenharmony_ci	rsip = rsi_lookup(sn->rsi_cache, &rsikey);
12518c2ecf20Sopenharmony_ci	rsi_free(&rsikey);
12528c2ecf20Sopenharmony_ci	if (!rsip)
12538c2ecf20Sopenharmony_ci		return SVC_CLOSE;
12548c2ecf20Sopenharmony_ci	if (cache_check(sn->rsi_cache, &rsip->h, &rqstp->rq_chandle) < 0)
12558c2ecf20Sopenharmony_ci		/* No upcall result: */
12568c2ecf20Sopenharmony_ci		return SVC_CLOSE;
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_ci	ret = SVC_CLOSE;
12598c2ecf20Sopenharmony_ci	/* Got an answer to the upcall; use it: */
12608c2ecf20Sopenharmony_ci	if (gss_write_init_verf(sn->rsc_cache, rqstp,
12618c2ecf20Sopenharmony_ci				&rsip->out_handle, &rsip->major_status))
12628c2ecf20Sopenharmony_ci		goto out;
12638c2ecf20Sopenharmony_ci	if (gss_write_resv(resv, PAGE_SIZE,
12648c2ecf20Sopenharmony_ci			   &rsip->out_handle, &rsip->out_token,
12658c2ecf20Sopenharmony_ci			   rsip->major_status, rsip->minor_status))
12668c2ecf20Sopenharmony_ci		goto out;
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci	ret = SVC_COMPLETE;
12698c2ecf20Sopenharmony_ciout:
12708c2ecf20Sopenharmony_ci	cache_put(&rsip->h, sn->rsi_cache);
12718c2ecf20Sopenharmony_ci	return ret;
12728c2ecf20Sopenharmony_ci}
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_cistatic int gss_proxy_save_rsc(struct cache_detail *cd,
12758c2ecf20Sopenharmony_ci				struct gssp_upcall_data *ud,
12768c2ecf20Sopenharmony_ci				uint64_t *handle)
12778c2ecf20Sopenharmony_ci{
12788c2ecf20Sopenharmony_ci	struct rsc rsci, *rscp = NULL;
12798c2ecf20Sopenharmony_ci	static atomic64_t ctxhctr;
12808c2ecf20Sopenharmony_ci	long long ctxh;
12818c2ecf20Sopenharmony_ci	struct gss_api_mech *gm = NULL;
12828c2ecf20Sopenharmony_ci	time64_t expiry;
12838c2ecf20Sopenharmony_ci	int status = -EINVAL;
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_ci	memset(&rsci, 0, sizeof(rsci));
12868c2ecf20Sopenharmony_ci	/* context handle */
12878c2ecf20Sopenharmony_ci	status = -ENOMEM;
12888c2ecf20Sopenharmony_ci	/* the handle needs to be just a unique id,
12898c2ecf20Sopenharmony_ci	 * use a static counter */
12908c2ecf20Sopenharmony_ci	ctxh = atomic64_inc_return(&ctxhctr);
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	/* make a copy for the caller */
12938c2ecf20Sopenharmony_ci	*handle = ctxh;
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	/* make a copy for the rsc cache */
12968c2ecf20Sopenharmony_ci	if (dup_to_netobj(&rsci.handle, (char *)handle, sizeof(uint64_t)))
12978c2ecf20Sopenharmony_ci		goto out;
12988c2ecf20Sopenharmony_ci	rscp = rsc_lookup(cd, &rsci);
12998c2ecf20Sopenharmony_ci	if (!rscp)
13008c2ecf20Sopenharmony_ci		goto out;
13018c2ecf20Sopenharmony_ci
13028c2ecf20Sopenharmony_ci	/* creds */
13038c2ecf20Sopenharmony_ci	if (!ud->found_creds) {
13048c2ecf20Sopenharmony_ci		/* userspace seem buggy, we should always get at least a
13058c2ecf20Sopenharmony_ci		 * mapping to nobody */
13068c2ecf20Sopenharmony_ci		goto out;
13078c2ecf20Sopenharmony_ci	} else {
13088c2ecf20Sopenharmony_ci		struct timespec64 boot;
13098c2ecf20Sopenharmony_ci
13108c2ecf20Sopenharmony_ci		/* steal creds */
13118c2ecf20Sopenharmony_ci		rsci.cred = ud->creds;
13128c2ecf20Sopenharmony_ci		memset(&ud->creds, 0, sizeof(struct svc_cred));
13138c2ecf20Sopenharmony_ci
13148c2ecf20Sopenharmony_ci		status = -EOPNOTSUPP;
13158c2ecf20Sopenharmony_ci		/* get mech handle from OID */
13168c2ecf20Sopenharmony_ci		gm = gss_mech_get_by_OID(&ud->mech_oid);
13178c2ecf20Sopenharmony_ci		if (!gm)
13188c2ecf20Sopenharmony_ci			goto out;
13198c2ecf20Sopenharmony_ci		rsci.cred.cr_gss_mech = gm;
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci		status = -EINVAL;
13228c2ecf20Sopenharmony_ci		/* mech-specific data: */
13238c2ecf20Sopenharmony_ci		status = gss_import_sec_context(ud->out_handle.data,
13248c2ecf20Sopenharmony_ci						ud->out_handle.len,
13258c2ecf20Sopenharmony_ci						gm, &rsci.mechctx,
13268c2ecf20Sopenharmony_ci						&expiry, GFP_KERNEL);
13278c2ecf20Sopenharmony_ci		if (status)
13288c2ecf20Sopenharmony_ci			goto out;
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci		getboottime64(&boot);
13318c2ecf20Sopenharmony_ci		expiry -= boot.tv_sec;
13328c2ecf20Sopenharmony_ci	}
13338c2ecf20Sopenharmony_ci
13348c2ecf20Sopenharmony_ci	rsci.h.expiry_time = expiry;
13358c2ecf20Sopenharmony_ci	rscp = rsc_update(cd, &rsci, rscp);
13368c2ecf20Sopenharmony_ci	status = 0;
13378c2ecf20Sopenharmony_ciout:
13388c2ecf20Sopenharmony_ci	rsc_free(&rsci);
13398c2ecf20Sopenharmony_ci	if (rscp)
13408c2ecf20Sopenharmony_ci		cache_put(&rscp->h, cd);
13418c2ecf20Sopenharmony_ci	else
13428c2ecf20Sopenharmony_ci		status = -ENOMEM;
13438c2ecf20Sopenharmony_ci	return status;
13448c2ecf20Sopenharmony_ci}
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_cistatic int svcauth_gss_proxy_init(struct svc_rqst *rqstp,
13478c2ecf20Sopenharmony_ci			struct rpc_gss_wire_cred *gc, __be32 *authp)
13488c2ecf20Sopenharmony_ci{
13498c2ecf20Sopenharmony_ci	struct kvec *resv = &rqstp->rq_res.head[0];
13508c2ecf20Sopenharmony_ci	struct xdr_netobj cli_handle;
13518c2ecf20Sopenharmony_ci	struct gssp_upcall_data ud;
13528c2ecf20Sopenharmony_ci	uint64_t handle;
13538c2ecf20Sopenharmony_ci	int status;
13548c2ecf20Sopenharmony_ci	int ret;
13558c2ecf20Sopenharmony_ci	struct net *net = SVC_NET(rqstp);
13568c2ecf20Sopenharmony_ci	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci	memset(&ud, 0, sizeof(ud));
13598c2ecf20Sopenharmony_ci	ret = gss_read_proxy_verf(rqstp, gc, authp,
13608c2ecf20Sopenharmony_ci				  &ud.in_handle, &ud.in_token);
13618c2ecf20Sopenharmony_ci	if (ret)
13628c2ecf20Sopenharmony_ci		return ret;
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci	ret = SVC_CLOSE;
13658c2ecf20Sopenharmony_ci
13668c2ecf20Sopenharmony_ci	/* Perform synchronous upcall to gss-proxy */
13678c2ecf20Sopenharmony_ci	status = gssp_accept_sec_context_upcall(net, &ud);
13688c2ecf20Sopenharmony_ci	if (status)
13698c2ecf20Sopenharmony_ci		goto out;
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci	trace_rpcgss_svc_accept_upcall(rqstp, ud.major_status, ud.minor_status);
13728c2ecf20Sopenharmony_ci
13738c2ecf20Sopenharmony_ci	switch (ud.major_status) {
13748c2ecf20Sopenharmony_ci	case GSS_S_CONTINUE_NEEDED:
13758c2ecf20Sopenharmony_ci		cli_handle = ud.out_handle;
13768c2ecf20Sopenharmony_ci		break;
13778c2ecf20Sopenharmony_ci	case GSS_S_COMPLETE:
13788c2ecf20Sopenharmony_ci		status = gss_proxy_save_rsc(sn->rsc_cache, &ud, &handle);
13798c2ecf20Sopenharmony_ci		if (status)
13808c2ecf20Sopenharmony_ci			goto out;
13818c2ecf20Sopenharmony_ci		cli_handle.data = (u8 *)&handle;
13828c2ecf20Sopenharmony_ci		cli_handle.len = sizeof(handle);
13838c2ecf20Sopenharmony_ci		break;
13848c2ecf20Sopenharmony_ci	default:
13858c2ecf20Sopenharmony_ci		goto out;
13868c2ecf20Sopenharmony_ci	}
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_ci	/* Got an answer to the upcall; use it: */
13898c2ecf20Sopenharmony_ci	if (gss_write_init_verf(sn->rsc_cache, rqstp,
13908c2ecf20Sopenharmony_ci				&cli_handle, &ud.major_status))
13918c2ecf20Sopenharmony_ci		goto out;
13928c2ecf20Sopenharmony_ci	if (gss_write_resv(resv, PAGE_SIZE,
13938c2ecf20Sopenharmony_ci			   &cli_handle, &ud.out_token,
13948c2ecf20Sopenharmony_ci			   ud.major_status, ud.minor_status))
13958c2ecf20Sopenharmony_ci		goto out;
13968c2ecf20Sopenharmony_ci
13978c2ecf20Sopenharmony_ci	ret = SVC_COMPLETE;
13988c2ecf20Sopenharmony_ciout:
13998c2ecf20Sopenharmony_ci	gss_free_in_token_pages(&ud.in_token);
14008c2ecf20Sopenharmony_ci	gssp_free_upcall_data(&ud);
14018c2ecf20Sopenharmony_ci	return ret;
14028c2ecf20Sopenharmony_ci}
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci/*
14058c2ecf20Sopenharmony_ci * Try to set the sn->use_gss_proxy variable to a new value. We only allow
14068c2ecf20Sopenharmony_ci * it to be changed if it's currently undefined (-1). If it's any other value
14078c2ecf20Sopenharmony_ci * then return -EBUSY unless the type wouldn't have changed anyway.
14088c2ecf20Sopenharmony_ci */
14098c2ecf20Sopenharmony_cistatic int set_gss_proxy(struct net *net, int type)
14108c2ecf20Sopenharmony_ci{
14118c2ecf20Sopenharmony_ci	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
14128c2ecf20Sopenharmony_ci	int ret;
14138c2ecf20Sopenharmony_ci
14148c2ecf20Sopenharmony_ci	WARN_ON_ONCE(type != 0 && type != 1);
14158c2ecf20Sopenharmony_ci	ret = cmpxchg(&sn->use_gss_proxy, -1, type);
14168c2ecf20Sopenharmony_ci	if (ret != -1 && ret != type)
14178c2ecf20Sopenharmony_ci		return -EBUSY;
14188c2ecf20Sopenharmony_ci	return 0;
14198c2ecf20Sopenharmony_ci}
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_cistatic bool use_gss_proxy(struct net *net)
14228c2ecf20Sopenharmony_ci{
14238c2ecf20Sopenharmony_ci	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_ci	/* If use_gss_proxy is still undefined, then try to disable it */
14268c2ecf20Sopenharmony_ci	if (sn->use_gss_proxy == -1)
14278c2ecf20Sopenharmony_ci		set_gss_proxy(net, 0);
14288c2ecf20Sopenharmony_ci	return sn->use_gss_proxy;
14298c2ecf20Sopenharmony_ci}
14308c2ecf20Sopenharmony_ci
14318c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_cistatic ssize_t write_gssp(struct file *file, const char __user *buf,
14348c2ecf20Sopenharmony_ci			 size_t count, loff_t *ppos)
14358c2ecf20Sopenharmony_ci{
14368c2ecf20Sopenharmony_ci	struct net *net = PDE_DATA(file_inode(file));
14378c2ecf20Sopenharmony_ci	char tbuf[20];
14388c2ecf20Sopenharmony_ci	unsigned long i;
14398c2ecf20Sopenharmony_ci	int res;
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	if (*ppos || count > sizeof(tbuf)-1)
14428c2ecf20Sopenharmony_ci		return -EINVAL;
14438c2ecf20Sopenharmony_ci	if (copy_from_user(tbuf, buf, count))
14448c2ecf20Sopenharmony_ci		return -EFAULT;
14458c2ecf20Sopenharmony_ci
14468c2ecf20Sopenharmony_ci	tbuf[count] = 0;
14478c2ecf20Sopenharmony_ci	res = kstrtoul(tbuf, 0, &i);
14488c2ecf20Sopenharmony_ci	if (res)
14498c2ecf20Sopenharmony_ci		return res;
14508c2ecf20Sopenharmony_ci	if (i != 1)
14518c2ecf20Sopenharmony_ci		return -EINVAL;
14528c2ecf20Sopenharmony_ci	res = set_gssp_clnt(net);
14538c2ecf20Sopenharmony_ci	if (res)
14548c2ecf20Sopenharmony_ci		return res;
14558c2ecf20Sopenharmony_ci	res = set_gss_proxy(net, 1);
14568c2ecf20Sopenharmony_ci	if (res)
14578c2ecf20Sopenharmony_ci		return res;
14588c2ecf20Sopenharmony_ci	return count;
14598c2ecf20Sopenharmony_ci}
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_cistatic ssize_t read_gssp(struct file *file, char __user *buf,
14628c2ecf20Sopenharmony_ci			 size_t count, loff_t *ppos)
14638c2ecf20Sopenharmony_ci{
14648c2ecf20Sopenharmony_ci	struct net *net = PDE_DATA(file_inode(file));
14658c2ecf20Sopenharmony_ci	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
14668c2ecf20Sopenharmony_ci	unsigned long p = *ppos;
14678c2ecf20Sopenharmony_ci	char tbuf[10];
14688c2ecf20Sopenharmony_ci	size_t len;
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	snprintf(tbuf, sizeof(tbuf), "%d\n", sn->use_gss_proxy);
14718c2ecf20Sopenharmony_ci	len = strlen(tbuf);
14728c2ecf20Sopenharmony_ci	if (p >= len)
14738c2ecf20Sopenharmony_ci		return 0;
14748c2ecf20Sopenharmony_ci	len -= p;
14758c2ecf20Sopenharmony_ci	if (len > count)
14768c2ecf20Sopenharmony_ci		len = count;
14778c2ecf20Sopenharmony_ci	if (copy_to_user(buf, (void *)(tbuf+p), len))
14788c2ecf20Sopenharmony_ci		return -EFAULT;
14798c2ecf20Sopenharmony_ci	*ppos += len;
14808c2ecf20Sopenharmony_ci	return len;
14818c2ecf20Sopenharmony_ci}
14828c2ecf20Sopenharmony_ci
14838c2ecf20Sopenharmony_cistatic const struct proc_ops use_gss_proxy_proc_ops = {
14848c2ecf20Sopenharmony_ci	.proc_open	= nonseekable_open,
14858c2ecf20Sopenharmony_ci	.proc_write	= write_gssp,
14868c2ecf20Sopenharmony_ci	.proc_read	= read_gssp,
14878c2ecf20Sopenharmony_ci};
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_cistatic int create_use_gss_proxy_proc_entry(struct net *net)
14908c2ecf20Sopenharmony_ci{
14918c2ecf20Sopenharmony_ci	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
14928c2ecf20Sopenharmony_ci	struct proc_dir_entry **p = &sn->use_gssp_proc;
14938c2ecf20Sopenharmony_ci
14948c2ecf20Sopenharmony_ci	sn->use_gss_proxy = -1;
14958c2ecf20Sopenharmony_ci	*p = proc_create_data("use-gss-proxy", S_IFREG | 0600,
14968c2ecf20Sopenharmony_ci			      sn->proc_net_rpc,
14978c2ecf20Sopenharmony_ci			      &use_gss_proxy_proc_ops, net);
14988c2ecf20Sopenharmony_ci	if (!*p)
14998c2ecf20Sopenharmony_ci		return -ENOMEM;
15008c2ecf20Sopenharmony_ci	init_gssp_clnt(sn);
15018c2ecf20Sopenharmony_ci	return 0;
15028c2ecf20Sopenharmony_ci}
15038c2ecf20Sopenharmony_ci
15048c2ecf20Sopenharmony_cistatic void destroy_use_gss_proxy_proc_entry(struct net *net)
15058c2ecf20Sopenharmony_ci{
15068c2ecf20Sopenharmony_ci	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
15078c2ecf20Sopenharmony_ci
15088c2ecf20Sopenharmony_ci	if (sn->use_gssp_proc) {
15098c2ecf20Sopenharmony_ci		remove_proc_entry("use-gss-proxy", sn->proc_net_rpc);
15108c2ecf20Sopenharmony_ci		clear_gssp_clnt(sn);
15118c2ecf20Sopenharmony_ci	}
15128c2ecf20Sopenharmony_ci}
15138c2ecf20Sopenharmony_ci#else /* CONFIG_PROC_FS */
15148c2ecf20Sopenharmony_ci
15158c2ecf20Sopenharmony_cistatic int create_use_gss_proxy_proc_entry(struct net *net)
15168c2ecf20Sopenharmony_ci{
15178c2ecf20Sopenharmony_ci	return 0;
15188c2ecf20Sopenharmony_ci}
15198c2ecf20Sopenharmony_ci
15208c2ecf20Sopenharmony_cistatic void destroy_use_gss_proxy_proc_entry(struct net *net) {}
15218c2ecf20Sopenharmony_ci
15228c2ecf20Sopenharmony_ci#endif /* CONFIG_PROC_FS */
15238c2ecf20Sopenharmony_ci
15248c2ecf20Sopenharmony_ci/*
15258c2ecf20Sopenharmony_ci * Accept an rpcsec packet.
15268c2ecf20Sopenharmony_ci * If context establishment, punt to user space
15278c2ecf20Sopenharmony_ci * If data exchange, verify/decrypt
15288c2ecf20Sopenharmony_ci * If context destruction, handle here
15298c2ecf20Sopenharmony_ci * In the context establishment and destruction case we encode
15308c2ecf20Sopenharmony_ci * response here and return SVC_COMPLETE.
15318c2ecf20Sopenharmony_ci */
15328c2ecf20Sopenharmony_cistatic int
15338c2ecf20Sopenharmony_cisvcauth_gss_accept(struct svc_rqst *rqstp, __be32 *authp)
15348c2ecf20Sopenharmony_ci{
15358c2ecf20Sopenharmony_ci	struct kvec	*argv = &rqstp->rq_arg.head[0];
15368c2ecf20Sopenharmony_ci	struct kvec	*resv = &rqstp->rq_res.head[0];
15378c2ecf20Sopenharmony_ci	u32		crlen;
15388c2ecf20Sopenharmony_ci	struct gss_svc_data *svcdata = rqstp->rq_auth_data;
15398c2ecf20Sopenharmony_ci	struct rpc_gss_wire_cred *gc;
15408c2ecf20Sopenharmony_ci	struct rsc	*rsci = NULL;
15418c2ecf20Sopenharmony_ci	__be32		*rpcstart;
15428c2ecf20Sopenharmony_ci	__be32		*reject_stat = resv->iov_base + resv->iov_len;
15438c2ecf20Sopenharmony_ci	int		ret;
15448c2ecf20Sopenharmony_ci	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ci	*authp = rpc_autherr_badcred;
15478c2ecf20Sopenharmony_ci	if (!svcdata)
15488c2ecf20Sopenharmony_ci		svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL);
15498c2ecf20Sopenharmony_ci	if (!svcdata)
15508c2ecf20Sopenharmony_ci		goto auth_err;
15518c2ecf20Sopenharmony_ci	rqstp->rq_auth_data = svcdata;
15528c2ecf20Sopenharmony_ci	svcdata->verf_start = NULL;
15538c2ecf20Sopenharmony_ci	svcdata->rsci = NULL;
15548c2ecf20Sopenharmony_ci	gc = &svcdata->clcred;
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci	/* start of rpc packet is 7 u32's back from here:
15578c2ecf20Sopenharmony_ci	 * xid direction rpcversion prog vers proc flavour
15588c2ecf20Sopenharmony_ci	 */
15598c2ecf20Sopenharmony_ci	rpcstart = argv->iov_base;
15608c2ecf20Sopenharmony_ci	rpcstart -= 7;
15618c2ecf20Sopenharmony_ci
15628c2ecf20Sopenharmony_ci	/* credential is:
15638c2ecf20Sopenharmony_ci	 *   version(==1), proc(0,1,2,3), seq, service (1,2,3), handle
15648c2ecf20Sopenharmony_ci	 * at least 5 u32s, and is preceded by length, so that makes 6.
15658c2ecf20Sopenharmony_ci	 */
15668c2ecf20Sopenharmony_ci
15678c2ecf20Sopenharmony_ci	if (argv->iov_len < 5 * 4)
15688c2ecf20Sopenharmony_ci		goto auth_err;
15698c2ecf20Sopenharmony_ci	crlen = svc_getnl(argv);
15708c2ecf20Sopenharmony_ci	if (svc_getnl(argv) != RPC_GSS_VERSION)
15718c2ecf20Sopenharmony_ci		goto auth_err;
15728c2ecf20Sopenharmony_ci	gc->gc_proc = svc_getnl(argv);
15738c2ecf20Sopenharmony_ci	gc->gc_seq = svc_getnl(argv);
15748c2ecf20Sopenharmony_ci	gc->gc_svc = svc_getnl(argv);
15758c2ecf20Sopenharmony_ci	if (svc_safe_getnetobj(argv, &gc->gc_ctx))
15768c2ecf20Sopenharmony_ci		goto auth_err;
15778c2ecf20Sopenharmony_ci	if (crlen != round_up_to_quad(gc->gc_ctx.len) + 5 * 4)
15788c2ecf20Sopenharmony_ci		goto auth_err;
15798c2ecf20Sopenharmony_ci
15808c2ecf20Sopenharmony_ci	if ((gc->gc_proc != RPC_GSS_PROC_DATA) && (rqstp->rq_proc != 0))
15818c2ecf20Sopenharmony_ci		goto auth_err;
15828c2ecf20Sopenharmony_ci
15838c2ecf20Sopenharmony_ci	*authp = rpc_autherr_badverf;
15848c2ecf20Sopenharmony_ci	switch (gc->gc_proc) {
15858c2ecf20Sopenharmony_ci	case RPC_GSS_PROC_INIT:
15868c2ecf20Sopenharmony_ci	case RPC_GSS_PROC_CONTINUE_INIT:
15878c2ecf20Sopenharmony_ci		if (use_gss_proxy(SVC_NET(rqstp)))
15888c2ecf20Sopenharmony_ci			return svcauth_gss_proxy_init(rqstp, gc, authp);
15898c2ecf20Sopenharmony_ci		else
15908c2ecf20Sopenharmony_ci			return svcauth_gss_legacy_init(rqstp, gc, authp);
15918c2ecf20Sopenharmony_ci	case RPC_GSS_PROC_DATA:
15928c2ecf20Sopenharmony_ci	case RPC_GSS_PROC_DESTROY:
15938c2ecf20Sopenharmony_ci		/* Look up the context, and check the verifier: */
15948c2ecf20Sopenharmony_ci		*authp = rpcsec_gsserr_credproblem;
15958c2ecf20Sopenharmony_ci		rsci = gss_svc_searchbyctx(sn->rsc_cache, &gc->gc_ctx);
15968c2ecf20Sopenharmony_ci		if (!rsci)
15978c2ecf20Sopenharmony_ci			goto auth_err;
15988c2ecf20Sopenharmony_ci		switch (gss_verify_header(rqstp, rsci, rpcstart, gc, authp)) {
15998c2ecf20Sopenharmony_ci		case SVC_OK:
16008c2ecf20Sopenharmony_ci			break;
16018c2ecf20Sopenharmony_ci		case SVC_DENIED:
16028c2ecf20Sopenharmony_ci			goto auth_err;
16038c2ecf20Sopenharmony_ci		case SVC_DROP:
16048c2ecf20Sopenharmony_ci			goto drop;
16058c2ecf20Sopenharmony_ci		}
16068c2ecf20Sopenharmony_ci		break;
16078c2ecf20Sopenharmony_ci	default:
16088c2ecf20Sopenharmony_ci		*authp = rpc_autherr_rejectedcred;
16098c2ecf20Sopenharmony_ci		goto auth_err;
16108c2ecf20Sopenharmony_ci	}
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_ci	/* now act upon the command: */
16138c2ecf20Sopenharmony_ci	switch (gc->gc_proc) {
16148c2ecf20Sopenharmony_ci	case RPC_GSS_PROC_DESTROY:
16158c2ecf20Sopenharmony_ci		if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
16168c2ecf20Sopenharmony_ci			goto auth_err;
16178c2ecf20Sopenharmony_ci		/* Delete the entry from the cache_list and call cache_put */
16188c2ecf20Sopenharmony_ci		sunrpc_cache_unhash(sn->rsc_cache, &rsci->h);
16198c2ecf20Sopenharmony_ci		if (resv->iov_len + 4 > PAGE_SIZE)
16208c2ecf20Sopenharmony_ci			goto drop;
16218c2ecf20Sopenharmony_ci		svc_putnl(resv, RPC_SUCCESS);
16228c2ecf20Sopenharmony_ci		goto complete;
16238c2ecf20Sopenharmony_ci	case RPC_GSS_PROC_DATA:
16248c2ecf20Sopenharmony_ci		*authp = rpcsec_gsserr_ctxproblem;
16258c2ecf20Sopenharmony_ci		svcdata->verf_start = resv->iov_base + resv->iov_len;
16268c2ecf20Sopenharmony_ci		if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
16278c2ecf20Sopenharmony_ci			goto auth_err;
16288c2ecf20Sopenharmony_ci		rqstp->rq_cred = rsci->cred;
16298c2ecf20Sopenharmony_ci		get_group_info(rsci->cred.cr_group_info);
16308c2ecf20Sopenharmony_ci		*authp = rpc_autherr_badcred;
16318c2ecf20Sopenharmony_ci		switch (gc->gc_svc) {
16328c2ecf20Sopenharmony_ci		case RPC_GSS_SVC_NONE:
16338c2ecf20Sopenharmony_ci			break;
16348c2ecf20Sopenharmony_ci		case RPC_GSS_SVC_INTEGRITY:
16358c2ecf20Sopenharmony_ci			/* placeholders for length and seq. number: */
16368c2ecf20Sopenharmony_ci			svc_putnl(resv, 0);
16378c2ecf20Sopenharmony_ci			svc_putnl(resv, 0);
16388c2ecf20Sopenharmony_ci			if (unwrap_integ_data(rqstp, &rqstp->rq_arg,
16398c2ecf20Sopenharmony_ci					gc->gc_seq, rsci->mechctx))
16408c2ecf20Sopenharmony_ci				goto garbage_args;
16418c2ecf20Sopenharmony_ci			rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE;
16428c2ecf20Sopenharmony_ci			break;
16438c2ecf20Sopenharmony_ci		case RPC_GSS_SVC_PRIVACY:
16448c2ecf20Sopenharmony_ci			/* placeholders for length and seq. number: */
16458c2ecf20Sopenharmony_ci			svc_putnl(resv, 0);
16468c2ecf20Sopenharmony_ci			svc_putnl(resv, 0);
16478c2ecf20Sopenharmony_ci			if (unwrap_priv_data(rqstp, &rqstp->rq_arg,
16488c2ecf20Sopenharmony_ci					gc->gc_seq, rsci->mechctx))
16498c2ecf20Sopenharmony_ci				goto garbage_args;
16508c2ecf20Sopenharmony_ci			rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE * 2;
16518c2ecf20Sopenharmony_ci			break;
16528c2ecf20Sopenharmony_ci		default:
16538c2ecf20Sopenharmony_ci			goto auth_err;
16548c2ecf20Sopenharmony_ci		}
16558c2ecf20Sopenharmony_ci		svcdata->rsci = rsci;
16568c2ecf20Sopenharmony_ci		cache_get(&rsci->h);
16578c2ecf20Sopenharmony_ci		rqstp->rq_cred.cr_flavor = gss_svc_to_pseudoflavor(
16588c2ecf20Sopenharmony_ci					rsci->mechctx->mech_type,
16598c2ecf20Sopenharmony_ci					GSS_C_QOP_DEFAULT,
16608c2ecf20Sopenharmony_ci					gc->gc_svc);
16618c2ecf20Sopenharmony_ci		ret = SVC_OK;
16628c2ecf20Sopenharmony_ci		trace_rpcgss_svc_authenticate(rqstp, gc);
16638c2ecf20Sopenharmony_ci		goto out;
16648c2ecf20Sopenharmony_ci	}
16658c2ecf20Sopenharmony_cigarbage_args:
16668c2ecf20Sopenharmony_ci	ret = SVC_GARBAGE;
16678c2ecf20Sopenharmony_ci	goto out;
16688c2ecf20Sopenharmony_ciauth_err:
16698c2ecf20Sopenharmony_ci	/* Restore write pointer to its original value: */
16708c2ecf20Sopenharmony_ci	xdr_ressize_check(rqstp, reject_stat);
16718c2ecf20Sopenharmony_ci	ret = SVC_DENIED;
16728c2ecf20Sopenharmony_ci	goto out;
16738c2ecf20Sopenharmony_cicomplete:
16748c2ecf20Sopenharmony_ci	ret = SVC_COMPLETE;
16758c2ecf20Sopenharmony_ci	goto out;
16768c2ecf20Sopenharmony_cidrop:
16778c2ecf20Sopenharmony_ci	ret = SVC_CLOSE;
16788c2ecf20Sopenharmony_ciout:
16798c2ecf20Sopenharmony_ci	if (rsci)
16808c2ecf20Sopenharmony_ci		cache_put(&rsci->h, sn->rsc_cache);
16818c2ecf20Sopenharmony_ci	return ret;
16828c2ecf20Sopenharmony_ci}
16838c2ecf20Sopenharmony_ci
16848c2ecf20Sopenharmony_cistatic __be32 *
16858c2ecf20Sopenharmony_cisvcauth_gss_prepare_to_wrap(struct xdr_buf *resbuf, struct gss_svc_data *gsd)
16868c2ecf20Sopenharmony_ci{
16878c2ecf20Sopenharmony_ci	__be32 *p;
16888c2ecf20Sopenharmony_ci	u32 verf_len;
16898c2ecf20Sopenharmony_ci
16908c2ecf20Sopenharmony_ci	p = gsd->verf_start;
16918c2ecf20Sopenharmony_ci	gsd->verf_start = NULL;
16928c2ecf20Sopenharmony_ci
16938c2ecf20Sopenharmony_ci	/* If the reply stat is nonzero, don't wrap: */
16948c2ecf20Sopenharmony_ci	if (*(p-1) != rpc_success)
16958c2ecf20Sopenharmony_ci		return NULL;
16968c2ecf20Sopenharmony_ci	/* Skip the verifier: */
16978c2ecf20Sopenharmony_ci	p += 1;
16988c2ecf20Sopenharmony_ci	verf_len = ntohl(*p++);
16998c2ecf20Sopenharmony_ci	p += XDR_QUADLEN(verf_len);
17008c2ecf20Sopenharmony_ci	/* move accept_stat to right place: */
17018c2ecf20Sopenharmony_ci	memcpy(p, p + 2, 4);
17028c2ecf20Sopenharmony_ci	/* Also don't wrap if the accept stat is nonzero: */
17038c2ecf20Sopenharmony_ci	if (*p != rpc_success) {
17048c2ecf20Sopenharmony_ci		resbuf->head[0].iov_len -= 2 * 4;
17058c2ecf20Sopenharmony_ci		return NULL;
17068c2ecf20Sopenharmony_ci	}
17078c2ecf20Sopenharmony_ci	p++;
17088c2ecf20Sopenharmony_ci	return p;
17098c2ecf20Sopenharmony_ci}
17108c2ecf20Sopenharmony_ci
17118c2ecf20Sopenharmony_cistatic inline int
17128c2ecf20Sopenharmony_cisvcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)
17138c2ecf20Sopenharmony_ci{
17148c2ecf20Sopenharmony_ci	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
17158c2ecf20Sopenharmony_ci	struct rpc_gss_wire_cred *gc = &gsd->clcred;
17168c2ecf20Sopenharmony_ci	struct xdr_buf *resbuf = &rqstp->rq_res;
17178c2ecf20Sopenharmony_ci	struct xdr_buf integ_buf;
17188c2ecf20Sopenharmony_ci	struct xdr_netobj mic;
17198c2ecf20Sopenharmony_ci	struct kvec *resv;
17208c2ecf20Sopenharmony_ci	__be32 *p;
17218c2ecf20Sopenharmony_ci	int integ_offset, integ_len;
17228c2ecf20Sopenharmony_ci	int stat = -EINVAL;
17238c2ecf20Sopenharmony_ci
17248c2ecf20Sopenharmony_ci	p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
17258c2ecf20Sopenharmony_ci	if (p == NULL)
17268c2ecf20Sopenharmony_ci		goto out;
17278c2ecf20Sopenharmony_ci	integ_offset = (u8 *)(p + 1) - (u8 *)resbuf->head[0].iov_base;
17288c2ecf20Sopenharmony_ci	integ_len = resbuf->len - integ_offset;
17298c2ecf20Sopenharmony_ci	if (integ_len & 3)
17308c2ecf20Sopenharmony_ci		goto out;
17318c2ecf20Sopenharmony_ci	*p++ = htonl(integ_len);
17328c2ecf20Sopenharmony_ci	*p++ = htonl(gc->gc_seq);
17338c2ecf20Sopenharmony_ci	if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len)) {
17348c2ecf20Sopenharmony_ci		WARN_ON_ONCE(1);
17358c2ecf20Sopenharmony_ci		goto out_err;
17368c2ecf20Sopenharmony_ci	}
17378c2ecf20Sopenharmony_ci	if (resbuf->tail[0].iov_base == NULL) {
17388c2ecf20Sopenharmony_ci		if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)
17398c2ecf20Sopenharmony_ci			goto out_err;
17408c2ecf20Sopenharmony_ci		resbuf->tail[0].iov_base = resbuf->head[0].iov_base
17418c2ecf20Sopenharmony_ci						+ resbuf->head[0].iov_len;
17428c2ecf20Sopenharmony_ci		resbuf->tail[0].iov_len = 0;
17438c2ecf20Sopenharmony_ci	}
17448c2ecf20Sopenharmony_ci	resv = &resbuf->tail[0];
17458c2ecf20Sopenharmony_ci	mic.data = (u8 *)resv->iov_base + resv->iov_len + 4;
17468c2ecf20Sopenharmony_ci	if (gss_get_mic(gsd->rsci->mechctx, &integ_buf, &mic))
17478c2ecf20Sopenharmony_ci		goto out_err;
17488c2ecf20Sopenharmony_ci	svc_putnl(resv, mic.len);
17498c2ecf20Sopenharmony_ci	memset(mic.data + mic.len, 0,
17508c2ecf20Sopenharmony_ci			round_up_to_quad(mic.len) - mic.len);
17518c2ecf20Sopenharmony_ci	resv->iov_len += XDR_QUADLEN(mic.len) << 2;
17528c2ecf20Sopenharmony_ci	/* not strictly required: */
17538c2ecf20Sopenharmony_ci	resbuf->len += XDR_QUADLEN(mic.len) << 2;
17548c2ecf20Sopenharmony_ci	if (resv->iov_len > PAGE_SIZE)
17558c2ecf20Sopenharmony_ci		goto out_err;
17568c2ecf20Sopenharmony_ciout:
17578c2ecf20Sopenharmony_ci	stat = 0;
17588c2ecf20Sopenharmony_ciout_err:
17598c2ecf20Sopenharmony_ci	return stat;
17608c2ecf20Sopenharmony_ci}
17618c2ecf20Sopenharmony_ci
17628c2ecf20Sopenharmony_cistatic inline int
17638c2ecf20Sopenharmony_cisvcauth_gss_wrap_resp_priv(struct svc_rqst *rqstp)
17648c2ecf20Sopenharmony_ci{
17658c2ecf20Sopenharmony_ci	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
17668c2ecf20Sopenharmony_ci	struct rpc_gss_wire_cred *gc = &gsd->clcred;
17678c2ecf20Sopenharmony_ci	struct xdr_buf *resbuf = &rqstp->rq_res;
17688c2ecf20Sopenharmony_ci	struct page **inpages = NULL;
17698c2ecf20Sopenharmony_ci	__be32 *p, *len;
17708c2ecf20Sopenharmony_ci	int offset;
17718c2ecf20Sopenharmony_ci	int pad;
17728c2ecf20Sopenharmony_ci
17738c2ecf20Sopenharmony_ci	p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
17748c2ecf20Sopenharmony_ci	if (p == NULL)
17758c2ecf20Sopenharmony_ci		return 0;
17768c2ecf20Sopenharmony_ci	len = p++;
17778c2ecf20Sopenharmony_ci	offset = (u8 *)p - (u8 *)resbuf->head[0].iov_base;
17788c2ecf20Sopenharmony_ci	*p++ = htonl(gc->gc_seq);
17798c2ecf20Sopenharmony_ci	inpages = resbuf->pages;
17808c2ecf20Sopenharmony_ci	/* XXX: Would be better to write some xdr helper functions for
17818c2ecf20Sopenharmony_ci	 * nfs{2,3,4}xdr.c that place the data right, instead of copying: */
17828c2ecf20Sopenharmony_ci
17838c2ecf20Sopenharmony_ci	/*
17848c2ecf20Sopenharmony_ci	 * If there is currently tail data, make sure there is
17858c2ecf20Sopenharmony_ci	 * room for the head, tail, and 2 * RPC_MAX_AUTH_SIZE in
17868c2ecf20Sopenharmony_ci	 * the page, and move the current tail data such that
17878c2ecf20Sopenharmony_ci	 * there is RPC_MAX_AUTH_SIZE slack space available in
17888c2ecf20Sopenharmony_ci	 * both the head and tail.
17898c2ecf20Sopenharmony_ci	 */
17908c2ecf20Sopenharmony_ci	if (resbuf->tail[0].iov_base) {
17918c2ecf20Sopenharmony_ci		if (resbuf->tail[0].iov_base >=
17928c2ecf20Sopenharmony_ci			resbuf->head[0].iov_base + PAGE_SIZE)
17938c2ecf20Sopenharmony_ci			return -EINVAL;
17948c2ecf20Sopenharmony_ci		if (resbuf->tail[0].iov_base < resbuf->head[0].iov_base)
17958c2ecf20Sopenharmony_ci			return -EINVAL;
17968c2ecf20Sopenharmony_ci		if (resbuf->tail[0].iov_len + resbuf->head[0].iov_len
17978c2ecf20Sopenharmony_ci				+ 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE)
17988c2ecf20Sopenharmony_ci			return -ENOMEM;
17998c2ecf20Sopenharmony_ci		memmove(resbuf->tail[0].iov_base + RPC_MAX_AUTH_SIZE,
18008c2ecf20Sopenharmony_ci			resbuf->tail[0].iov_base,
18018c2ecf20Sopenharmony_ci			resbuf->tail[0].iov_len);
18028c2ecf20Sopenharmony_ci		resbuf->tail[0].iov_base += RPC_MAX_AUTH_SIZE;
18038c2ecf20Sopenharmony_ci	}
18048c2ecf20Sopenharmony_ci	/*
18058c2ecf20Sopenharmony_ci	 * If there is no current tail data, make sure there is
18068c2ecf20Sopenharmony_ci	 * room for the head data, and 2 * RPC_MAX_AUTH_SIZE in the
18078c2ecf20Sopenharmony_ci	 * allotted page, and set up tail information such that there
18088c2ecf20Sopenharmony_ci	 * is RPC_MAX_AUTH_SIZE slack space available in both the
18098c2ecf20Sopenharmony_ci	 * head and tail.
18108c2ecf20Sopenharmony_ci	 */
18118c2ecf20Sopenharmony_ci	if (resbuf->tail[0].iov_base == NULL) {
18128c2ecf20Sopenharmony_ci		if (resbuf->head[0].iov_len + 2*RPC_MAX_AUTH_SIZE > PAGE_SIZE)
18138c2ecf20Sopenharmony_ci			return -ENOMEM;
18148c2ecf20Sopenharmony_ci		resbuf->tail[0].iov_base = resbuf->head[0].iov_base
18158c2ecf20Sopenharmony_ci			+ resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE;
18168c2ecf20Sopenharmony_ci		resbuf->tail[0].iov_len = 0;
18178c2ecf20Sopenharmony_ci	}
18188c2ecf20Sopenharmony_ci	if (gss_wrap(gsd->rsci->mechctx, offset, resbuf, inpages))
18198c2ecf20Sopenharmony_ci		return -ENOMEM;
18208c2ecf20Sopenharmony_ci	*len = htonl(resbuf->len - offset);
18218c2ecf20Sopenharmony_ci	pad = 3 - ((resbuf->len - offset - 1)&3);
18228c2ecf20Sopenharmony_ci	p = (__be32 *)(resbuf->tail[0].iov_base + resbuf->tail[0].iov_len);
18238c2ecf20Sopenharmony_ci	memset(p, 0, pad);
18248c2ecf20Sopenharmony_ci	resbuf->tail[0].iov_len += pad;
18258c2ecf20Sopenharmony_ci	resbuf->len += pad;
18268c2ecf20Sopenharmony_ci	return 0;
18278c2ecf20Sopenharmony_ci}
18288c2ecf20Sopenharmony_ci
18298c2ecf20Sopenharmony_cistatic int
18308c2ecf20Sopenharmony_cisvcauth_gss_release(struct svc_rqst *rqstp)
18318c2ecf20Sopenharmony_ci{
18328c2ecf20Sopenharmony_ci	struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
18338c2ecf20Sopenharmony_ci	struct rpc_gss_wire_cred *gc;
18348c2ecf20Sopenharmony_ci	struct xdr_buf *resbuf = &rqstp->rq_res;
18358c2ecf20Sopenharmony_ci	int stat = -EINVAL;
18368c2ecf20Sopenharmony_ci	struct sunrpc_net *sn = net_generic(SVC_NET(rqstp), sunrpc_net_id);
18378c2ecf20Sopenharmony_ci
18388c2ecf20Sopenharmony_ci	if (!gsd)
18398c2ecf20Sopenharmony_ci		goto out;
18408c2ecf20Sopenharmony_ci	gc = &gsd->clcred;
18418c2ecf20Sopenharmony_ci	if (gc->gc_proc != RPC_GSS_PROC_DATA)
18428c2ecf20Sopenharmony_ci		goto out;
18438c2ecf20Sopenharmony_ci	/* Release can be called twice, but we only wrap once. */
18448c2ecf20Sopenharmony_ci	if (gsd->verf_start == NULL)
18458c2ecf20Sopenharmony_ci		goto out;
18468c2ecf20Sopenharmony_ci	/* normally not set till svc_send, but we need it here: */
18478c2ecf20Sopenharmony_ci	/* XXX: what for?  Do we mess it up the moment we call svc_putu32
18488c2ecf20Sopenharmony_ci	 * or whatever? */
18498c2ecf20Sopenharmony_ci	resbuf->len = total_buf_len(resbuf);
18508c2ecf20Sopenharmony_ci	switch (gc->gc_svc) {
18518c2ecf20Sopenharmony_ci	case RPC_GSS_SVC_NONE:
18528c2ecf20Sopenharmony_ci		break;
18538c2ecf20Sopenharmony_ci	case RPC_GSS_SVC_INTEGRITY:
18548c2ecf20Sopenharmony_ci		stat = svcauth_gss_wrap_resp_integ(rqstp);
18558c2ecf20Sopenharmony_ci		if (stat)
18568c2ecf20Sopenharmony_ci			goto out_err;
18578c2ecf20Sopenharmony_ci		break;
18588c2ecf20Sopenharmony_ci	case RPC_GSS_SVC_PRIVACY:
18598c2ecf20Sopenharmony_ci		stat = svcauth_gss_wrap_resp_priv(rqstp);
18608c2ecf20Sopenharmony_ci		if (stat)
18618c2ecf20Sopenharmony_ci			goto out_err;
18628c2ecf20Sopenharmony_ci		break;
18638c2ecf20Sopenharmony_ci	/*
18648c2ecf20Sopenharmony_ci	 * For any other gc_svc value, svcauth_gss_accept() already set
18658c2ecf20Sopenharmony_ci	 * the auth_error appropriately; just fall through:
18668c2ecf20Sopenharmony_ci	 */
18678c2ecf20Sopenharmony_ci	}
18688c2ecf20Sopenharmony_ci
18698c2ecf20Sopenharmony_ciout:
18708c2ecf20Sopenharmony_ci	stat = 0;
18718c2ecf20Sopenharmony_ciout_err:
18728c2ecf20Sopenharmony_ci	if (rqstp->rq_client)
18738c2ecf20Sopenharmony_ci		auth_domain_put(rqstp->rq_client);
18748c2ecf20Sopenharmony_ci	rqstp->rq_client = NULL;
18758c2ecf20Sopenharmony_ci	if (rqstp->rq_gssclient)
18768c2ecf20Sopenharmony_ci		auth_domain_put(rqstp->rq_gssclient);
18778c2ecf20Sopenharmony_ci	rqstp->rq_gssclient = NULL;
18788c2ecf20Sopenharmony_ci	if (rqstp->rq_cred.cr_group_info)
18798c2ecf20Sopenharmony_ci		put_group_info(rqstp->rq_cred.cr_group_info);
18808c2ecf20Sopenharmony_ci	rqstp->rq_cred.cr_group_info = NULL;
18818c2ecf20Sopenharmony_ci	if (gsd && gsd->rsci) {
18828c2ecf20Sopenharmony_ci		cache_put(&gsd->rsci->h, sn->rsc_cache);
18838c2ecf20Sopenharmony_ci		gsd->rsci = NULL;
18848c2ecf20Sopenharmony_ci	}
18858c2ecf20Sopenharmony_ci	return stat;
18868c2ecf20Sopenharmony_ci}
18878c2ecf20Sopenharmony_ci
18888c2ecf20Sopenharmony_cistatic void
18898c2ecf20Sopenharmony_cisvcauth_gss_domain_release_rcu(struct rcu_head *head)
18908c2ecf20Sopenharmony_ci{
18918c2ecf20Sopenharmony_ci	struct auth_domain *dom = container_of(head, struct auth_domain, rcu_head);
18928c2ecf20Sopenharmony_ci	struct gss_domain *gd = container_of(dom, struct gss_domain, h);
18938c2ecf20Sopenharmony_ci
18948c2ecf20Sopenharmony_ci	kfree(dom->name);
18958c2ecf20Sopenharmony_ci	kfree(gd);
18968c2ecf20Sopenharmony_ci}
18978c2ecf20Sopenharmony_ci
18988c2ecf20Sopenharmony_cistatic void
18998c2ecf20Sopenharmony_cisvcauth_gss_domain_release(struct auth_domain *dom)
19008c2ecf20Sopenharmony_ci{
19018c2ecf20Sopenharmony_ci	call_rcu(&dom->rcu_head, svcauth_gss_domain_release_rcu);
19028c2ecf20Sopenharmony_ci}
19038c2ecf20Sopenharmony_ci
19048c2ecf20Sopenharmony_cistatic struct auth_ops svcauthops_gss = {
19058c2ecf20Sopenharmony_ci	.name		= "rpcsec_gss",
19068c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
19078c2ecf20Sopenharmony_ci	.flavour	= RPC_AUTH_GSS,
19088c2ecf20Sopenharmony_ci	.accept		= svcauth_gss_accept,
19098c2ecf20Sopenharmony_ci	.release	= svcauth_gss_release,
19108c2ecf20Sopenharmony_ci	.domain_release = svcauth_gss_domain_release,
19118c2ecf20Sopenharmony_ci	.set_client	= svcauth_gss_set_client,
19128c2ecf20Sopenharmony_ci};
19138c2ecf20Sopenharmony_ci
19148c2ecf20Sopenharmony_cistatic int rsi_cache_create_net(struct net *net)
19158c2ecf20Sopenharmony_ci{
19168c2ecf20Sopenharmony_ci	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
19178c2ecf20Sopenharmony_ci	struct cache_detail *cd;
19188c2ecf20Sopenharmony_ci	int err;
19198c2ecf20Sopenharmony_ci
19208c2ecf20Sopenharmony_ci	cd = cache_create_net(&rsi_cache_template, net);
19218c2ecf20Sopenharmony_ci	if (IS_ERR(cd))
19228c2ecf20Sopenharmony_ci		return PTR_ERR(cd);
19238c2ecf20Sopenharmony_ci	err = cache_register_net(cd, net);
19248c2ecf20Sopenharmony_ci	if (err) {
19258c2ecf20Sopenharmony_ci		cache_destroy_net(cd, net);
19268c2ecf20Sopenharmony_ci		return err;
19278c2ecf20Sopenharmony_ci	}
19288c2ecf20Sopenharmony_ci	sn->rsi_cache = cd;
19298c2ecf20Sopenharmony_ci	return 0;
19308c2ecf20Sopenharmony_ci}
19318c2ecf20Sopenharmony_ci
19328c2ecf20Sopenharmony_cistatic void rsi_cache_destroy_net(struct net *net)
19338c2ecf20Sopenharmony_ci{
19348c2ecf20Sopenharmony_ci	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
19358c2ecf20Sopenharmony_ci	struct cache_detail *cd = sn->rsi_cache;
19368c2ecf20Sopenharmony_ci
19378c2ecf20Sopenharmony_ci	sn->rsi_cache = NULL;
19388c2ecf20Sopenharmony_ci	cache_purge(cd);
19398c2ecf20Sopenharmony_ci	cache_unregister_net(cd, net);
19408c2ecf20Sopenharmony_ci	cache_destroy_net(cd, net);
19418c2ecf20Sopenharmony_ci}
19428c2ecf20Sopenharmony_ci
19438c2ecf20Sopenharmony_cistatic int rsc_cache_create_net(struct net *net)
19448c2ecf20Sopenharmony_ci{
19458c2ecf20Sopenharmony_ci	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
19468c2ecf20Sopenharmony_ci	struct cache_detail *cd;
19478c2ecf20Sopenharmony_ci	int err;
19488c2ecf20Sopenharmony_ci
19498c2ecf20Sopenharmony_ci	cd = cache_create_net(&rsc_cache_template, net);
19508c2ecf20Sopenharmony_ci	if (IS_ERR(cd))
19518c2ecf20Sopenharmony_ci		return PTR_ERR(cd);
19528c2ecf20Sopenharmony_ci	err = cache_register_net(cd, net);
19538c2ecf20Sopenharmony_ci	if (err) {
19548c2ecf20Sopenharmony_ci		cache_destroy_net(cd, net);
19558c2ecf20Sopenharmony_ci		return err;
19568c2ecf20Sopenharmony_ci	}
19578c2ecf20Sopenharmony_ci	sn->rsc_cache = cd;
19588c2ecf20Sopenharmony_ci	return 0;
19598c2ecf20Sopenharmony_ci}
19608c2ecf20Sopenharmony_ci
19618c2ecf20Sopenharmony_cistatic void rsc_cache_destroy_net(struct net *net)
19628c2ecf20Sopenharmony_ci{
19638c2ecf20Sopenharmony_ci	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
19648c2ecf20Sopenharmony_ci	struct cache_detail *cd = sn->rsc_cache;
19658c2ecf20Sopenharmony_ci
19668c2ecf20Sopenharmony_ci	sn->rsc_cache = NULL;
19678c2ecf20Sopenharmony_ci	cache_purge(cd);
19688c2ecf20Sopenharmony_ci	cache_unregister_net(cd, net);
19698c2ecf20Sopenharmony_ci	cache_destroy_net(cd, net);
19708c2ecf20Sopenharmony_ci}
19718c2ecf20Sopenharmony_ci
19728c2ecf20Sopenharmony_ciint
19738c2ecf20Sopenharmony_cigss_svc_init_net(struct net *net)
19748c2ecf20Sopenharmony_ci{
19758c2ecf20Sopenharmony_ci	int rv;
19768c2ecf20Sopenharmony_ci
19778c2ecf20Sopenharmony_ci	rv = rsc_cache_create_net(net);
19788c2ecf20Sopenharmony_ci	if (rv)
19798c2ecf20Sopenharmony_ci		return rv;
19808c2ecf20Sopenharmony_ci	rv = rsi_cache_create_net(net);
19818c2ecf20Sopenharmony_ci	if (rv)
19828c2ecf20Sopenharmony_ci		goto out1;
19838c2ecf20Sopenharmony_ci	rv = create_use_gss_proxy_proc_entry(net);
19848c2ecf20Sopenharmony_ci	if (rv)
19858c2ecf20Sopenharmony_ci		goto out2;
19868c2ecf20Sopenharmony_ci	return 0;
19878c2ecf20Sopenharmony_ciout2:
19888c2ecf20Sopenharmony_ci	rsi_cache_destroy_net(net);
19898c2ecf20Sopenharmony_ciout1:
19908c2ecf20Sopenharmony_ci	rsc_cache_destroy_net(net);
19918c2ecf20Sopenharmony_ci	return rv;
19928c2ecf20Sopenharmony_ci}
19938c2ecf20Sopenharmony_ci
19948c2ecf20Sopenharmony_civoid
19958c2ecf20Sopenharmony_cigss_svc_shutdown_net(struct net *net)
19968c2ecf20Sopenharmony_ci{
19978c2ecf20Sopenharmony_ci	destroy_use_gss_proxy_proc_entry(net);
19988c2ecf20Sopenharmony_ci	rsi_cache_destroy_net(net);
19998c2ecf20Sopenharmony_ci	rsc_cache_destroy_net(net);
20008c2ecf20Sopenharmony_ci}
20018c2ecf20Sopenharmony_ci
20028c2ecf20Sopenharmony_ciint
20038c2ecf20Sopenharmony_cigss_svc_init(void)
20048c2ecf20Sopenharmony_ci{
20058c2ecf20Sopenharmony_ci	return svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);
20068c2ecf20Sopenharmony_ci}
20078c2ecf20Sopenharmony_ci
20088c2ecf20Sopenharmony_civoid
20098c2ecf20Sopenharmony_cigss_svc_shutdown(void)
20108c2ecf20Sopenharmony_ci{
20118c2ecf20Sopenharmony_ci	svc_auth_unregister(RPC_AUTH_GSS);
20128c2ecf20Sopenharmony_ci}
2013