2 * Modifications for Lustre
4 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
6 * Copyright (c) 2012, 2014, Intel Corporation.
8 * Author: Eric Mei <ericm@clusterfs.com>
12 * Neil Brown <neilb@cse.unsw.edu.au>
13 * J. Bruce Fields <bfields@umich.edu>
14 * Andy Adamson <andros@umich.edu>
15 * Dug Song <dugsong@monkey.org>
17 * RPCSEC_GSS server authentication.
18 * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
21 * The RPCSEC_GSS involves three stages:
24 * 3/ context destruction
26 * Context creation is handled largely by upcalls to user-space.
27 * In particular, GSS_Accept_sec_context is handled by an upcall
28 * Data exchange is handled entirely within the kernel
29 * In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
30 * Context destruction is handled in-kernel
31 * GSS_Delete_sec_context is in-kernel
33 * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
34 * The context handle and gss_token are used as a key into the rpcsec_init cache.
35 * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
36 * being major_status, minor_status, context_handle, reply_token.
37 * These are sent back to the client.
38 * Sequence window management is handled by the kernel. The window size if currently
39 * a compile time constant.
41 * When user-space is happy that a context is established, it places an entry
42 * in the rpcsec_context cache. The key for this cache is the context_handle.
43 * The content includes:
44 * uid/gidlist - for determining access rights
46 * mechanism specific information, such as a key
50 #define DEBUG_SUBSYSTEM S_SEC
51 #include <linux/types.h>
52 #include <linux/init.h>
53 #include <linux/module.h>
54 #include <linux/slab.h>
55 #include <linux/hash.h>
56 #include <linux/mutex.h>
57 #include <linux/sunrpc/cache.h>
61 #include <obd_class.h>
62 #include <obd_support.h>
63 #include <lustre_import.h>
64 #include <lustre_net.h>
65 #include <lustre_nodemap.h>
66 #include <lustre_sec.h>
69 #include "gss_internal.h"
72 #define GSS_SVC_UPCALL_TIMEOUT (20)
74 static spinlock_t __ctx_index_lock;
75 static __u64 __ctx_index;
77 __u64 gss_get_next_ctx_index(void)
81 spin_lock(&__ctx_index_lock);
83 spin_unlock(&__ctx_index_lock);
88 static inline unsigned long hash_mem(char *buf, int length, int bits)
90 unsigned long hash = 0;
105 if ((len & (BITS_PER_LONG/8-1)) == 0)
106 hash = hash_long(hash^l, BITS_PER_LONG);
109 return hash >> (BITS_PER_LONG - bits);
112 /* This compatibility can be removed once kernel 3.3 is used,
113 * since cache_register_net/cache_unregister_net are exported.
114 * Note that since kernel 3.4 cache_register and cache_unregister
117 static inline int _cache_register_net(struct cache_detail *cd, struct net *net)
119 #ifdef HAVE_CACHE_REGISTER
120 return cache_register(cd);
122 return cache_register_net(cd, net);
125 static inline void _cache_unregister_net(struct cache_detail *cd,
128 #ifdef HAVE_CACHE_REGISTER
129 cache_unregister(cd);
131 cache_unregister_net(cd, net);
134 /****************************************
135 * rpc sec init (rsi) cache *
136 ****************************************/
138 #define RSI_HASHBITS (6)
139 #define RSI_HASHMAX (1 << RSI_HASHBITS)
140 #define RSI_HASHMASK (RSI_HASHMAX - 1)
146 char nm_name[LUSTRE_NODEMAP_NAME_LENGTH + 1];
147 wait_queue_head_t waitq;
148 rawobj_t in_handle, in_token;
149 rawobj_t out_handle, out_token;
150 int major_status, minor_status;
153 #ifdef HAVE_CACHE_HEAD_HLIST
154 static struct hlist_head rsi_table[RSI_HASHMAX];
156 static struct cache_head *rsi_table[RSI_HASHMAX];
158 static struct cache_detail rsi_cache;
159 static struct rsi *rsi_update(struct rsi *new, struct rsi *old);
160 static struct rsi *rsi_lookup(struct rsi *item);
162 static inline int rsi_hash(struct rsi *item)
164 return hash_mem((char *)item->in_handle.data, item->in_handle.len,
166 hash_mem((char *)item->in_token.data, item->in_token.len,
170 static inline int __rsi_match(struct rsi *item, struct rsi *tmp)
172 return (rawobj_equal(&item->in_handle, &tmp->in_handle) &&
173 rawobj_equal(&item->in_token, &tmp->in_token));
176 static void rsi_free(struct rsi *rsi)
178 rawobj_free(&rsi->in_handle);
179 rawobj_free(&rsi->in_token);
180 rawobj_free(&rsi->out_handle);
181 rawobj_free(&rsi->out_token);
184 /* See handle_channel_req() userspace for where the upcall data is read */
185 static void rsi_request(struct cache_detail *cd,
186 struct cache_head *h,
187 char **bpp, int *blen)
189 struct rsi *rsi = container_of(h, struct rsi, h);
192 /* if in_handle is null, provide kernel suggestion */
193 if (rsi->in_handle.len == 0)
194 index = gss_get_next_ctx_index();
196 qword_addhex(bpp, blen, (char *) &rsi->lustre_svc,
197 sizeof(rsi->lustre_svc));
198 qword_addhex(bpp, blen, (char *) &rsi->nid, sizeof(rsi->nid));
199 qword_addhex(bpp, blen, (char *) &index, sizeof(index));
200 qword_addhex(bpp, blen, (char *) rsi->nm_name,
201 strlen(rsi->nm_name) + 1);
202 qword_addhex(bpp, blen, rsi->in_handle.data, rsi->in_handle.len);
203 qword_addhex(bpp, blen, rsi->in_token.data, rsi->in_token.len);
207 #ifdef HAVE_SUNRPC_UPCALL_HAS_3ARGS
208 static int rsi_upcall(struct cache_detail *cd, struct cache_head *h)
210 return sunrpc_cache_pipe_upcall(cd, h, rsi_request);
214 static int rsi_upcall(struct cache_detail *cd, struct cache_head *h)
216 return sunrpc_cache_pipe_upcall(cd, h);
220 static inline void __rsi_init(struct rsi *new, struct rsi *item)
222 new->out_handle = RAWOBJ_EMPTY;
223 new->out_token = RAWOBJ_EMPTY;
225 new->in_handle = item->in_handle;
226 item->in_handle = RAWOBJ_EMPTY;
227 new->in_token = item->in_token;
228 item->in_token = RAWOBJ_EMPTY;
230 new->lustre_svc = item->lustre_svc;
231 new->nid = item->nid;
232 memcpy(new->nm_name, item->nm_name, sizeof(item->nm_name));
233 init_waitqueue_head(&new->waitq);
236 static inline void __rsi_update(struct rsi *new, struct rsi *item)
238 LASSERT(new->out_handle.len == 0);
239 LASSERT(new->out_token.len == 0);
241 new->out_handle = item->out_handle;
242 item->out_handle = RAWOBJ_EMPTY;
243 new->out_token = item->out_token;
244 item->out_token = RAWOBJ_EMPTY;
246 new->major_status = item->major_status;
247 new->minor_status = item->minor_status;
250 static void rsi_put(struct kref *ref)
252 struct rsi *rsi = container_of(ref, struct rsi, h.ref);
254 #ifdef HAVE_CACHE_HEAD_HLIST
255 LASSERT(rsi->h.cache_list.next == NULL);
257 LASSERT(rsi->h.next == NULL);
263 static int rsi_match(struct cache_head *a, struct cache_head *b)
265 struct rsi *item = container_of(a, struct rsi, h);
266 struct rsi *tmp = container_of(b, struct rsi, h);
268 return __rsi_match(item, tmp);
271 static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
273 struct rsi *new = container_of(cnew, struct rsi, h);
274 struct rsi *item = container_of(citem, struct rsi, h);
276 __rsi_init(new, item);
279 static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
281 struct rsi *new = container_of(cnew, struct rsi, h);
282 struct rsi *item = container_of(citem, struct rsi, h);
284 __rsi_update(new, item);
287 static struct cache_head *rsi_alloc(void)
298 static int rsi_parse(struct cache_detail *cd, char *mesg, int mlen)
303 struct rsi rsii, *rsip = NULL;
305 int status = -EINVAL;
309 memset(&rsii, 0, sizeof(rsii));
312 len = qword_get(&mesg, buf, mlen);
315 if (rawobj_alloc(&rsii.in_handle, buf, len)) {
321 len = qword_get(&mesg, buf, mlen);
324 if (rawobj_alloc(&rsii.in_token, buf, len)) {
329 rsip = rsi_lookup(&rsii);
335 expiry = get_expiry(&mesg);
339 len = qword_get(&mesg, buf, mlen);
344 rsii.major_status = simple_strtol(buf, &ep, 10);
349 len = qword_get(&mesg, buf, mlen);
352 rsii.minor_status = simple_strtol(buf, &ep, 10);
357 len = qword_get(&mesg, buf, mlen);
360 if (rawobj_alloc(&rsii.out_handle, buf, len)) {
366 len = qword_get(&mesg, buf, mlen);
369 if (rawobj_alloc(&rsii.out_token, buf, len)) {
374 rsii.h.expiry_time = expiry;
375 rsip = rsi_update(&rsii, rsip);
380 wake_up_all(&rsip->waitq);
381 cache_put(&rsip->h, &rsi_cache);
387 CERROR("rsi parse error %d\n", status);
391 static struct cache_detail rsi_cache = {
392 .hash_size = RSI_HASHMAX,
393 .hash_table = rsi_table,
394 .name = "auth.sptlrpc.init",
395 .cache_put = rsi_put,
396 #ifndef HAVE_SUNRPC_UPCALL_HAS_3ARGS
397 .cache_request = rsi_request,
399 .cache_upcall = rsi_upcall,
400 .cache_parse = rsi_parse,
403 .update = update_rsi,
407 static struct rsi *rsi_lookup(struct rsi *item)
409 struct cache_head *ch;
410 int hash = rsi_hash(item);
412 ch = sunrpc_cache_lookup(&rsi_cache, &item->h, hash);
414 return container_of(ch, struct rsi, h);
419 static struct rsi *rsi_update(struct rsi *new, struct rsi *old)
421 struct cache_head *ch;
422 int hash = rsi_hash(new);
424 ch = sunrpc_cache_update(&rsi_cache, &new->h, &old->h, hash);
426 return container_of(ch, struct rsi, h);
431 /****************************************
432 * rpc sec context (rsc) cache *
433 ****************************************/
435 #define RSC_HASHBITS (10)
436 #define RSC_HASHMAX (1 << RSC_HASHBITS)
437 #define RSC_HASHMASK (RSC_HASHMAX - 1)
441 struct obd_device *target;
443 struct gss_svc_ctx ctx;
446 #ifdef HAVE_CACHE_HEAD_HLIST
447 static struct hlist_head rsc_table[RSC_HASHMAX];
449 static struct cache_head *rsc_table[RSC_HASHMAX];
451 static struct cache_detail rsc_cache;
452 static struct rsc *rsc_update(struct rsc *new, struct rsc *old);
453 static struct rsc *rsc_lookup(struct rsc *item);
455 static void rsc_free(struct rsc *rsci)
457 rawobj_free(&rsci->handle);
458 rawobj_free(&rsci->ctx.gsc_rvs_hdl);
459 lgss_delete_sec_context(&rsci->ctx.gsc_mechctx);
462 static inline int rsc_hash(struct rsc *rsci)
464 return hash_mem((char *)rsci->handle.data,
465 rsci->handle.len, RSC_HASHBITS);
468 static inline int __rsc_match(struct rsc *new, struct rsc *tmp)
470 return rawobj_equal(&new->handle, &tmp->handle);
473 static inline void __rsc_init(struct rsc *new, struct rsc *tmp)
475 new->handle = tmp->handle;
476 tmp->handle = RAWOBJ_EMPTY;
479 memset(&new->ctx, 0, sizeof(new->ctx));
480 new->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
483 static inline void __rsc_update(struct rsc *new, struct rsc *tmp)
486 tmp->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
487 tmp->ctx.gsc_mechctx = NULL;
489 memset(&new->ctx.gsc_seqdata, 0, sizeof(new->ctx.gsc_seqdata));
490 spin_lock_init(&new->ctx.gsc_seqdata.ssd_lock);
493 static void rsc_put(struct kref *ref)
495 struct rsc *rsci = container_of(ref, struct rsc, h.ref);
497 #ifdef HAVE_CACHE_HEAD_HLIST
498 LASSERT(rsci->h.cache_list.next == NULL);
500 LASSERT(rsci->h.next == NULL);
506 static int rsc_match(struct cache_head *a, struct cache_head *b)
508 struct rsc *new = container_of(a, struct rsc, h);
509 struct rsc *tmp = container_of(b, struct rsc, h);
511 return __rsc_match(new, tmp);
514 static void rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
516 struct rsc *new = container_of(cnew, struct rsc, h);
517 struct rsc *tmp = container_of(ctmp, struct rsc, h);
519 __rsc_init(new, tmp);
522 static void update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
524 struct rsc *new = container_of(cnew, struct rsc, h);
525 struct rsc *tmp = container_of(ctmp, struct rsc, h);
527 __rsc_update(new, tmp);
530 static struct cache_head * rsc_alloc(void)
541 static int rsc_parse(struct cache_detail *cd, char *mesg, int mlen)
544 int len, rv, tmp_int;
545 struct rsc rsci, *rscp = NULL;
547 int status = -EINVAL;
548 struct gss_api_mech *gm = NULL;
550 memset(&rsci, 0, sizeof(rsci));
553 len = qword_get(&mesg, buf, mlen);
554 if (len < 0) goto out;
556 if (rawobj_alloc(&rsci.handle, buf, len))
561 expiry = get_expiry(&mesg);
567 rv = get_int(&mesg, &tmp_int);
569 CERROR("fail to get remote flag\n");
572 rsci.ctx.gsc_remote = (tmp_int != 0);
575 rv = get_int(&mesg, &tmp_int);
577 CERROR("fail to get root user flag\n");
580 rsci.ctx.gsc_usr_root = (tmp_int != 0);
583 rv = get_int(&mesg, &tmp_int);
585 CERROR("fail to get mds user flag\n");
588 rsci.ctx.gsc_usr_mds = (tmp_int != 0);
591 rv = get_int(&mesg, &tmp_int);
593 CERROR("fail to get oss user flag\n");
596 rsci.ctx.gsc_usr_oss = (tmp_int != 0);
599 rv = get_int(&mesg, (int *) &rsci.ctx.gsc_mapped_uid);
601 CERROR("fail to get mapped uid\n");
605 rscp = rsc_lookup(&rsci);
609 /* uid, or NEGATIVE */
610 rv = get_int(&mesg, (int *) &rsci.ctx.gsc_uid);
614 CERROR("NOENT? set rsc entry negative\n");
615 set_bit(CACHE_NEGATIVE, &rsci.h.flags);
618 unsigned long ctx_expiry;
621 if (get_int(&mesg, (int *) &rsci.ctx.gsc_gid))
625 len = qword_get(&mesg, buf, mlen);
628 gm = lgss_name_to_mech(buf);
629 status = -EOPNOTSUPP;
634 /* mech-specific data: */
635 len = qword_get(&mesg, buf, mlen);
640 tmp_buf.data = (unsigned char *)buf;
641 if (lgss_import_sec_context(&tmp_buf, gm,
642 &rsci.ctx.gsc_mechctx))
645 /* currently the expiry time passed down from user-space
646 * is invalid, here we retrive it from mech. */
647 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
648 CERROR("unable to get expire time, drop it\n");
651 expiry = (time_t) ctx_expiry;
654 rsci.h.expiry_time = expiry;
655 rscp = rsc_update(&rsci, rscp);
662 cache_put(&rscp->h, &rsc_cache);
667 CERROR("parse rsc error %d\n", status);
671 static struct cache_detail rsc_cache = {
672 .hash_size = RSC_HASHMAX,
673 .hash_table = rsc_table,
674 .name = "auth.sptlrpc.context",
675 .cache_put = rsc_put,
676 .cache_parse = rsc_parse,
679 .update = update_rsc,
683 static struct rsc *rsc_lookup(struct rsc *item)
685 struct cache_head *ch;
686 int hash = rsc_hash(item);
688 ch = sunrpc_cache_lookup(&rsc_cache, &item->h, hash);
690 return container_of(ch, struct rsc, h);
695 static struct rsc *rsc_update(struct rsc *new, struct rsc *old)
697 struct cache_head *ch;
698 int hash = rsc_hash(new);
700 ch = sunrpc_cache_update(&rsc_cache, &new->h, &old->h, hash);
702 return container_of(ch, struct rsc, h);
707 #define COMPAT_RSC_PUT(item, cd) cache_put((item), (cd))
709 /****************************************
711 ****************************************/
713 typedef int rsc_entry_match(struct rsc *rscp, long data);
715 static void rsc_flush(rsc_entry_match *match, long data)
717 #ifdef HAVE_CACHE_HEAD_HLIST
718 struct cache_head *ch = NULL;
719 struct hlist_head *head;
721 struct cache_head **ch;
727 write_lock(&rsc_cache.hash_lock);
728 for (n = 0; n < RSC_HASHMAX; n++) {
729 #ifdef HAVE_CACHE_HEAD_HLIST
730 head = &rsc_cache.hash_table[n];
731 hlist_for_each_entry(ch, head, cache_list) {
732 rscp = container_of(ch, struct rsc, h);
734 for (ch = &rsc_cache.hash_table[n]; *ch;) {
735 rscp = container_of(*ch, struct rsc, h);
738 if (!match(rscp, data)) {
739 #ifndef HAVE_CACHE_HEAD_HLIST
745 /* it seems simply set NEGATIVE doesn't work */
746 #ifdef HAVE_CACHE_HEAD_HLIST
747 hlist_del_init(&ch->cache_list);
753 set_bit(CACHE_NEGATIVE, &rscp->h.flags);
754 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
758 write_unlock(&rsc_cache.hash_lock);
762 static int match_uid(struct rsc *rscp, long uid)
766 return ((int) rscp->ctx.gsc_uid == (int) uid);
769 static int match_target(struct rsc *rscp, long target)
771 return (rscp->target == (struct obd_device *) target);
774 static inline void rsc_flush_uid(int uid)
777 CWARN("flush all gss contexts...\n");
779 rsc_flush(match_uid, (long) uid);
782 static inline void rsc_flush_target(struct obd_device *target)
784 rsc_flush(match_target, (long) target);
787 void gss_secsvc_flush(struct obd_device *target)
789 rsc_flush_target(target);
792 static struct rsc *gss_svc_searchbyctx(rawobj_t *handle)
797 memset(&rsci, 0, sizeof(rsci));
798 if (rawobj_dup(&rsci.handle, handle))
801 found = rsc_lookup(&rsci);
805 if (cache_check(&rsc_cache, &found->h, NULL))
810 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
811 struct gss_sec *gsec,
812 struct gss_cli_ctx *gctx)
814 struct rsc rsci, *rscp = NULL;
815 unsigned long ctx_expiry;
820 memset(&rsci, 0, sizeof(rsci));
822 if (rawobj_alloc(&rsci.handle, (char *) &gsec->gs_rvs_hdl,
823 sizeof(gsec->gs_rvs_hdl)))
824 GOTO(out, rc = -ENOMEM);
826 rscp = rsc_lookup(&rsci);
828 GOTO(out, rc = -ENOMEM);
830 major = lgss_copy_reverse_context(gctx->gc_mechctx,
831 &rsci.ctx.gsc_mechctx);
832 if (major != GSS_S_COMPLETE)
833 GOTO(out, rc = -ENOMEM);
835 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
836 CERROR("unable to get expire time, drop it\n");
837 GOTO(out, rc = -EINVAL);
839 rsci.h.expiry_time = (time_t) ctx_expiry;
841 switch (imp->imp_obd->u.cli.cl_sp_to) {
843 rsci.ctx.gsc_usr_mds = 1;
846 rsci.ctx.gsc_usr_oss = 1;
849 rsci.ctx.gsc_usr_root = 1;
854 rscp = rsc_update(&rsci, rscp);
856 GOTO(out, rc = -ENOMEM);
858 rscp->target = imp->imp_obd;
859 rawobj_dup(&gctx->gc_svc_handle, &rscp->handle);
861 CWARN("create reverse svc ctx %p to %s: idx %#llx\n",
862 &rscp->ctx, obd2cli_tgt(imp->imp_obd), gsec->gs_rvs_hdl);
866 cache_put(&rscp->h, &rsc_cache);
870 CERROR("create reverse svc ctx: idx %#llx, rc %d\n",
871 gsec->gs_rvs_hdl, rc);
875 int gss_svc_upcall_expire_rvs_ctx(rawobj_t *handle)
877 const cfs_time_t expire = 20;
880 rscp = gss_svc_searchbyctx(handle);
882 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) expire soon\n",
885 rscp->h.expiry_time = cfs_time_current_sec() + expire;
886 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
891 int gss_svc_upcall_dup_handle(rawobj_t *handle, struct gss_svc_ctx *ctx)
893 struct rsc *rscp = container_of(ctx, struct rsc, ctx);
895 return rawobj_dup(handle, &rscp->handle);
898 int gss_svc_upcall_update_sequence(rawobj_t *handle, __u32 seq)
902 rscp = gss_svc_searchbyctx(handle);
904 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) update seq to %u\n",
905 &rscp->ctx, rscp, seq + 1);
907 rscp->ctx.gsc_rvs_seq = seq + 1;
908 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
913 static struct cache_deferred_req* cache_upcall_defer(struct cache_req *req)
917 static struct cache_req cache_upcall_chandle = { cache_upcall_defer };
919 int gss_svc_upcall_handle_init(struct ptlrpc_request *req,
920 struct gss_svc_reqctx *grctx,
921 struct gss_wire_ctx *gw,
922 struct obd_device *target,
927 struct ptlrpc_reply_state *rs;
928 struct rsc *rsci = NULL;
929 struct rsi *rsip = NULL, rsikey;
931 int replen = sizeof(struct ptlrpc_body);
932 struct gss_rep_header *rephdr;
934 int rc = SECSVC_DROP;
937 memset(&rsikey, 0, sizeof(rsikey));
938 rsikey.lustre_svc = lustre_svc;
939 rsikey.nid = (__u64) req->rq_peer.nid;
940 nodemap_test_nid(req->rq_peer.nid, rsikey.nm_name,
941 sizeof(rsikey.nm_name));
943 /* duplicate context handle. for INIT it always 0 */
944 if (rawobj_dup(&rsikey.in_handle, &gw->gw_handle)) {
945 CERROR("fail to dup context handle\n");
949 if (rawobj_dup(&rsikey.in_token, in_token)) {
950 CERROR("can't duplicate token\n");
951 rawobj_free(&rsikey.in_handle);
955 rsip = rsi_lookup(&rsikey);
958 CERROR("error in rsi_lookup.\n");
960 if (!gss_pack_err_notify(req, GSS_S_FAILURE, 0))
961 rc = SECSVC_COMPLETE;
966 cache_get(&rsip->h); /* take an extra ref */
967 init_waitqueue_head(&rsip->waitq);
968 init_waitqueue_entry(&wait, current);
969 add_wait_queue(&rsip->waitq, &wait);
972 /* Note each time cache_check() will drop a reference if return
973 * non-zero. We hold an extra reference on initial rsip, but must
974 * take care of following calls. */
975 rc = cache_check(&rsi_cache, &rsip->h, &cache_upcall_chandle);
984 read_lock(&rsi_cache.hash_lock);
985 valid = test_bit(CACHE_VALID, &rsip->h.flags);
987 set_current_state(TASK_INTERRUPTIBLE);
988 read_unlock(&rsi_cache.hash_lock);
991 unsigned long jiffies;
992 jiffies = msecs_to_jiffies(MSEC_PER_SEC *
993 GSS_SVC_UPCALL_TIMEOUT);
994 schedule_timeout(jiffies);
999 CWARN("waited %ds timeout, drop\n", GSS_SVC_UPCALL_TIMEOUT);
1003 CDEBUG(D_SEC, "cache_check return ENOENT, drop\n");
1006 /* if not the first check, we have to release the extra
1007 * reference we just added on it. */
1009 cache_put(&rsip->h, &rsi_cache);
1010 CDEBUG(D_SEC, "cache_check is good\n");
1014 remove_wait_queue(&rsip->waitq, &wait);
1015 cache_put(&rsip->h, &rsi_cache);
1018 GOTO(out, rc = SECSVC_DROP);
1021 rsci = gss_svc_searchbyctx(&rsip->out_handle);
1023 CERROR("authentication failed\n");
1025 /* gss mechanism returned major and minor code so we return
1026 * those in error message */
1027 if (!gss_pack_err_notify(req, rsip->major_status,
1028 rsip->minor_status))
1029 rc = SECSVC_COMPLETE;
1033 cache_get(&rsci->h);
1034 grctx->src_ctx = &rsci->ctx;
1037 if (rawobj_dup(&rsci->ctx.gsc_rvs_hdl, rvs_hdl)) {
1038 CERROR("failed duplicate reverse handle\n");
1042 rsci->target = target;
1044 CDEBUG(D_SEC, "server create rsc %p(%u->%s)\n",
1045 rsci, rsci->ctx.gsc_uid, libcfs_nid2str(req->rq_peer.nid));
1047 if (rsip->out_handle.len > PTLRPC_GSS_MAX_HANDLE_SIZE) {
1048 CERROR("handle size %u too large\n", rsip->out_handle.len);
1049 GOTO(out, rc = SECSVC_DROP);
1052 grctx->src_init = 1;
1053 grctx->src_reserve_len = cfs_size_round4(rsip->out_token.len);
1055 rc = lustre_pack_reply_v2(req, 1, &replen, NULL, 0);
1057 CERROR("failed to pack reply: %d\n", rc);
1058 GOTO(out, rc = SECSVC_DROP);
1061 rs = req->rq_reply_state;
1062 LASSERT(rs->rs_repbuf->lm_bufcount == 3);
1063 LASSERT(rs->rs_repbuf->lm_buflens[0] >=
1064 sizeof(*rephdr) + rsip->out_handle.len);
1065 LASSERT(rs->rs_repbuf->lm_buflens[2] >= rsip->out_token.len);
1067 rephdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
1068 rephdr->gh_version = PTLRPC_GSS_VERSION;
1069 rephdr->gh_flags = 0;
1070 rephdr->gh_proc = PTLRPC_GSS_PROC_ERR;
1071 rephdr->gh_major = rsip->major_status;
1072 rephdr->gh_minor = rsip->minor_status;
1073 rephdr->gh_seqwin = GSS_SEQ_WIN;
1074 rephdr->gh_handle.len = rsip->out_handle.len;
1075 memcpy(rephdr->gh_handle.data, rsip->out_handle.data,
1076 rsip->out_handle.len);
1078 memcpy(lustre_msg_buf(rs->rs_repbuf, 2, 0), rsip->out_token.data,
1079 rsip->out_token.len);
1081 rs->rs_repdata_len = lustre_shrink_msg(rs->rs_repbuf, 2,
1082 rsip->out_token.len, 0);
1087 /* it looks like here we should put rsip also, but this mess up
1088 * with NFS cache mgmt code... FIXME
1091 * rsi_put(&rsip->h, &rsi_cache); */
1094 /* if anything went wrong, we don't keep the context too */
1095 if (rc != SECSVC_OK)
1096 set_bit(CACHE_NEGATIVE, &rsci->h.flags);
1098 CDEBUG(D_SEC, "create rsc with idx %#llx\n",
1099 gss_handle_to_u64(&rsci->handle));
1101 COMPAT_RSC_PUT(&rsci->h, &rsc_cache);
1106 struct gss_svc_ctx *gss_svc_upcall_get_ctx(struct ptlrpc_request *req,
1107 struct gss_wire_ctx *gw)
1111 rsc = gss_svc_searchbyctx(&gw->gw_handle);
1113 CWARN("Invalid gss ctx idx %#llx from %s\n",
1114 gss_handle_to_u64(&gw->gw_handle),
1115 libcfs_nid2str(req->rq_peer.nid));
1122 void gss_svc_upcall_put_ctx(struct gss_svc_ctx *ctx)
1124 struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1126 COMPAT_RSC_PUT(&rsc->h, &rsc_cache);
1129 void gss_svc_upcall_destroy_ctx(struct gss_svc_ctx *ctx)
1131 struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1133 /* can't be found */
1134 set_bit(CACHE_NEGATIVE, &rsc->h.flags);
1135 /* to be removed at next scan */
1136 rsc->h.expiry_time = 1;
1139 int __init gss_init_svc_upcall(void)
1143 spin_lock_init(&__ctx_index_lock);
1145 * this helps reducing context index confliction. after server reboot,
1146 * conflicting request from clients might be filtered out by initial
1147 * sequence number checking, thus no chance to sent error notification
1150 cfs_get_random_bytes(&__ctx_index, sizeof(__ctx_index));
1152 rc = _cache_register_net(&rsi_cache, &init_net);
1156 rc = _cache_register_net(&rsc_cache, &init_net);
1158 _cache_unregister_net(&rsi_cache, &init_net);
1162 /* FIXME this looks stupid. we intend to give lsvcgssd a chance to open
1163 * the init upcall channel, otherwise there's big chance that the first
1164 * upcall issued before the channel be opened thus nfsv4 cache code will
1165 * drop the request direclty, thus lead to unnecessary recovery time.
1166 * here we wait at miximum 1.5 seconds. */
1167 for (i = 0; i < 6; i++) {
1168 if (atomic_read(&rsi_cache.readers) > 0)
1170 set_current_state(TASK_UNINTERRUPTIBLE);
1171 LASSERT(msecs_to_jiffies(MSEC_PER_SEC) >= 4);
1172 schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC / 4));
1175 if (atomic_read(&rsi_cache.readers) == 0)
1176 CWARN("Init channel is not opened by lsvcgssd, following "
1177 "request might be dropped until lsvcgssd is active\n");
1182 void gss_exit_svc_upcall(void)
1184 cache_purge(&rsi_cache);
1185 _cache_unregister_net(&rsi_cache, &init_net);
1187 cache_purge(&rsc_cache);
1188 _cache_unregister_net(&rsc_cache, &init_net);