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/lustre_idl.h>
64 #include <lustre_net.h>
65 #include <lustre_import.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 /****************************************
136 ****************************************/
138 #define RSI_HASHBITS (6)
139 #define RSI_HASHMAX (1 << RSI_HASHBITS)
140 #define RSI_HASHMASK (RSI_HASHMAX - 1)
146 wait_queue_head_t waitq;
147 rawobj_t in_handle, in_token;
148 rawobj_t out_handle, out_token;
149 int major_status, minor_status;
152 #ifdef HAVE_CACHE_HEAD_HLIST
153 static struct hlist_head rsi_table[RSI_HASHMAX];
155 static struct cache_head *rsi_table[RSI_HASHMAX];
157 static struct cache_detail rsi_cache;
158 static struct rsi *rsi_update(struct rsi *new, struct rsi *old);
159 static struct rsi *rsi_lookup(struct rsi *item);
161 static inline int rsi_hash(struct rsi *item)
163 return hash_mem((char *)item->in_handle.data, item->in_handle.len,
165 hash_mem((char *)item->in_token.data, item->in_token.len,
169 static inline int __rsi_match(struct rsi *item, struct rsi *tmp)
171 return (rawobj_equal(&item->in_handle, &tmp->in_handle) &&
172 rawobj_equal(&item->in_token, &tmp->in_token));
175 static void rsi_free(struct rsi *rsi)
177 rawobj_free(&rsi->in_handle);
178 rawobj_free(&rsi->in_token);
179 rawobj_free(&rsi->out_handle);
180 rawobj_free(&rsi->out_token);
183 static void rsi_request(struct cache_detail *cd,
184 struct cache_head *h,
185 char **bpp, int *blen)
187 struct rsi *rsi = container_of(h, struct rsi, h);
190 /* if in_handle is null, provide kernel suggestion */
191 if (rsi->in_handle.len == 0)
192 index = gss_get_next_ctx_index();
194 qword_addhex(bpp, blen, (char *) &rsi->lustre_svc,
195 sizeof(rsi->lustre_svc));
196 qword_addhex(bpp, blen, (char *) &rsi->nid, sizeof(rsi->nid));
197 qword_addhex(bpp, blen, (char *) &index, sizeof(index));
198 qword_addhex(bpp, blen, rsi->in_handle.data, rsi->in_handle.len);
199 qword_addhex(bpp, blen, rsi->in_token.data, rsi->in_token.len);
203 #ifdef HAVE_SUNRPC_UPCALL_HAS_3ARGS
204 static int rsi_upcall(struct cache_detail *cd, struct cache_head *h)
206 return sunrpc_cache_pipe_upcall(cd, h, rsi_request);
210 static int rsi_upcall(struct cache_detail *cd, struct cache_head *h)
212 return sunrpc_cache_pipe_upcall(cd, h);
216 static inline void __rsi_init(struct rsi *new, struct rsi *item)
218 new->out_handle = RAWOBJ_EMPTY;
219 new->out_token = RAWOBJ_EMPTY;
221 new->in_handle = item->in_handle;
222 item->in_handle = RAWOBJ_EMPTY;
223 new->in_token = item->in_token;
224 item->in_token = RAWOBJ_EMPTY;
226 new->lustre_svc = item->lustre_svc;
227 new->nid = item->nid;
228 init_waitqueue_head(&new->waitq);
231 static inline void __rsi_update(struct rsi *new, struct rsi *item)
233 LASSERT(new->out_handle.len == 0);
234 LASSERT(new->out_token.len == 0);
236 new->out_handle = item->out_handle;
237 item->out_handle = RAWOBJ_EMPTY;
238 new->out_token = item->out_token;
239 item->out_token = RAWOBJ_EMPTY;
241 new->major_status = item->major_status;
242 new->minor_status = item->minor_status;
245 static void rsi_put(struct kref *ref)
247 struct rsi *rsi = container_of(ref, struct rsi, h.ref);
249 #ifdef HAVE_CACHE_HEAD_HLIST
250 LASSERT(rsi->h.cache_list.next == NULL);
252 LASSERT(rsi->h.next == NULL);
258 static int rsi_match(struct cache_head *a, struct cache_head *b)
260 struct rsi *item = container_of(a, struct rsi, h);
261 struct rsi *tmp = container_of(b, struct rsi, h);
263 return __rsi_match(item, tmp);
266 static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
268 struct rsi *new = container_of(cnew, struct rsi, h);
269 struct rsi *item = container_of(citem, struct rsi, h);
271 __rsi_init(new, item);
274 static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
276 struct rsi *new = container_of(cnew, struct rsi, h);
277 struct rsi *item = container_of(citem, struct rsi, h);
279 __rsi_update(new, item);
282 static struct cache_head *rsi_alloc(void)
293 static int rsi_parse(struct cache_detail *cd, char *mesg, int mlen)
298 struct rsi rsii, *rsip = NULL;
300 int status = -EINVAL;
304 memset(&rsii, 0, sizeof(rsii));
307 len = qword_get(&mesg, buf, mlen);
310 if (rawobj_alloc(&rsii.in_handle, buf, len)) {
316 len = qword_get(&mesg, buf, mlen);
319 if (rawobj_alloc(&rsii.in_token, buf, len)) {
324 rsip = rsi_lookup(&rsii);
330 expiry = get_expiry(&mesg);
334 len = qword_get(&mesg, buf, mlen);
339 rsii.major_status = simple_strtol(buf, &ep, 10);
344 len = qword_get(&mesg, buf, mlen);
347 rsii.minor_status = simple_strtol(buf, &ep, 10);
352 len = qword_get(&mesg, buf, mlen);
355 if (rawobj_alloc(&rsii.out_handle, buf, len)) {
361 len = qword_get(&mesg, buf, mlen);
364 if (rawobj_alloc(&rsii.out_token, buf, len)) {
369 rsii.h.expiry_time = expiry;
370 rsip = rsi_update(&rsii, rsip);
375 wake_up_all(&rsip->waitq);
376 cache_put(&rsip->h, &rsi_cache);
382 CERROR("rsi parse error %d\n", status);
386 static struct cache_detail rsi_cache = {
387 .hash_size = RSI_HASHMAX,
388 .hash_table = rsi_table,
389 .name = "auth.sptlrpc.init",
390 .cache_put = rsi_put,
391 #ifndef HAVE_SUNRPC_UPCALL_HAS_3ARGS
392 .cache_request = rsi_request,
394 .cache_upcall = rsi_upcall,
395 .cache_parse = rsi_parse,
398 .update = update_rsi,
402 static struct rsi *rsi_lookup(struct rsi *item)
404 struct cache_head *ch;
405 int hash = rsi_hash(item);
407 ch = sunrpc_cache_lookup(&rsi_cache, &item->h, hash);
409 return container_of(ch, struct rsi, h);
414 static struct rsi *rsi_update(struct rsi *new, struct rsi *old)
416 struct cache_head *ch;
417 int hash = rsi_hash(new);
419 ch = sunrpc_cache_update(&rsi_cache, &new->h, &old->h, hash);
421 return container_of(ch, struct rsi, h);
426 /****************************************
428 ****************************************/
430 #define RSC_HASHBITS (10)
431 #define RSC_HASHMAX (1 << RSC_HASHBITS)
432 #define RSC_HASHMASK (RSC_HASHMAX - 1)
436 struct obd_device *target;
438 struct gss_svc_ctx ctx;
441 #ifdef HAVE_CACHE_HEAD_HLIST
442 static struct hlist_head rsc_table[RSC_HASHMAX];
444 static struct cache_head *rsc_table[RSC_HASHMAX];
446 static struct cache_detail rsc_cache;
447 static struct rsc *rsc_update(struct rsc *new, struct rsc *old);
448 static struct rsc *rsc_lookup(struct rsc *item);
450 static void rsc_free(struct rsc *rsci)
452 rawobj_free(&rsci->handle);
453 rawobj_free(&rsci->ctx.gsc_rvs_hdl);
454 lgss_delete_sec_context(&rsci->ctx.gsc_mechctx);
457 static inline int rsc_hash(struct rsc *rsci)
459 return hash_mem((char *)rsci->handle.data,
460 rsci->handle.len, RSC_HASHBITS);
463 static inline int __rsc_match(struct rsc *new, struct rsc *tmp)
465 return rawobj_equal(&new->handle, &tmp->handle);
468 static inline void __rsc_init(struct rsc *new, struct rsc *tmp)
470 new->handle = tmp->handle;
471 tmp->handle = RAWOBJ_EMPTY;
474 memset(&new->ctx, 0, sizeof(new->ctx));
475 new->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
478 static inline void __rsc_update(struct rsc *new, struct rsc *tmp)
481 tmp->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
482 tmp->ctx.gsc_mechctx = NULL;
484 memset(&new->ctx.gsc_seqdata, 0, sizeof(new->ctx.gsc_seqdata));
485 spin_lock_init(&new->ctx.gsc_seqdata.ssd_lock);
488 static void rsc_put(struct kref *ref)
490 struct rsc *rsci = container_of(ref, struct rsc, h.ref);
492 #ifdef HAVE_CACHE_HEAD_HLIST
493 LASSERT(rsci->h.cache_list.next == NULL);
495 LASSERT(rsci->h.next == NULL);
501 static int rsc_match(struct cache_head *a, struct cache_head *b)
503 struct rsc *new = container_of(a, struct rsc, h);
504 struct rsc *tmp = container_of(b, struct rsc, h);
506 return __rsc_match(new, tmp);
509 static void rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
511 struct rsc *new = container_of(cnew, struct rsc, h);
512 struct rsc *tmp = container_of(ctmp, struct rsc, h);
514 __rsc_init(new, tmp);
517 static void update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
519 struct rsc *new = container_of(cnew, struct rsc, h);
520 struct rsc *tmp = container_of(ctmp, struct rsc, h);
522 __rsc_update(new, tmp);
525 static struct cache_head * rsc_alloc(void)
536 static int rsc_parse(struct cache_detail *cd, char *mesg, int mlen)
539 int len, rv, tmp_int;
540 struct rsc rsci, *rscp = NULL;
542 int status = -EINVAL;
543 struct gss_api_mech *gm = NULL;
545 memset(&rsci, 0, sizeof(rsci));
548 len = qword_get(&mesg, buf, mlen);
549 if (len < 0) goto out;
551 if (rawobj_alloc(&rsci.handle, buf, len))
556 expiry = get_expiry(&mesg);
562 rv = get_int(&mesg, &tmp_int);
564 CERROR("fail to get remote flag\n");
567 rsci.ctx.gsc_remote = (tmp_int != 0);
570 rv = get_int(&mesg, &tmp_int);
572 CERROR("fail to get root user flag\n");
575 rsci.ctx.gsc_usr_root = (tmp_int != 0);
578 rv = get_int(&mesg, &tmp_int);
580 CERROR("fail to get mds user flag\n");
583 rsci.ctx.gsc_usr_mds = (tmp_int != 0);
586 rv = get_int(&mesg, &tmp_int);
588 CERROR("fail to get oss user flag\n");
591 rsci.ctx.gsc_usr_oss = (tmp_int != 0);
594 rv = get_int(&mesg, (int *) &rsci.ctx.gsc_mapped_uid);
596 CERROR("fail to get mapped uid\n");
600 rscp = rsc_lookup(&rsci);
604 /* uid, or NEGATIVE */
605 rv = get_int(&mesg, (int *) &rsci.ctx.gsc_uid);
609 CERROR("NOENT? set rsc entry negative\n");
610 set_bit(CACHE_NEGATIVE, &rsci.h.flags);
613 unsigned long ctx_expiry;
616 if (get_int(&mesg, (int *) &rsci.ctx.gsc_gid))
620 len = qword_get(&mesg, buf, mlen);
623 gm = lgss_name_to_mech(buf);
624 status = -EOPNOTSUPP;
629 /* mech-specific data: */
630 len = qword_get(&mesg, buf, mlen);
635 tmp_buf.data = (unsigned char *)buf;
636 if (lgss_import_sec_context(&tmp_buf, gm,
637 &rsci.ctx.gsc_mechctx))
640 /* currently the expiry time passed down from user-space
641 * is invalid, here we retrive it from mech. */
642 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
643 CERROR("unable to get expire time, drop it\n");
646 expiry = (time_t) ctx_expiry;
649 rsci.h.expiry_time = expiry;
650 rscp = rsc_update(&rsci, rscp);
657 cache_put(&rscp->h, &rsc_cache);
662 CERROR("parse rsc error %d\n", status);
666 static struct cache_detail rsc_cache = {
667 .hash_size = RSC_HASHMAX,
668 .hash_table = rsc_table,
669 .name = "auth.sptlrpc.context",
670 .cache_put = rsc_put,
671 .cache_parse = rsc_parse,
674 .update = update_rsc,
678 static struct rsc *rsc_lookup(struct rsc *item)
680 struct cache_head *ch;
681 int hash = rsc_hash(item);
683 ch = sunrpc_cache_lookup(&rsc_cache, &item->h, hash);
685 return container_of(ch, struct rsc, h);
690 static struct rsc *rsc_update(struct rsc *new, struct rsc *old)
692 struct cache_head *ch;
693 int hash = rsc_hash(new);
695 ch = sunrpc_cache_update(&rsc_cache, &new->h, &old->h, hash);
697 return container_of(ch, struct rsc, h);
702 #define COMPAT_RSC_PUT(item, cd) cache_put((item), (cd))
704 /****************************************
706 ****************************************/
708 typedef int rsc_entry_match(struct rsc *rscp, long data);
710 static void rsc_flush(rsc_entry_match *match, long data)
712 #ifdef HAVE_CACHE_HEAD_HLIST
713 struct cache_head *ch = NULL;
714 struct hlist_head *head;
716 struct cache_head **ch;
722 write_lock(&rsc_cache.hash_lock);
723 for (n = 0; n < RSC_HASHMAX; n++) {
724 #ifdef HAVE_CACHE_HEAD_HLIST
725 head = &rsc_cache.hash_table[n];
726 hlist_for_each_entry(ch, head, cache_list) {
727 rscp = container_of(ch, struct rsc, h);
729 for (ch = &rsc_cache.hash_table[n]; *ch;) {
730 rscp = container_of(*ch, struct rsc, h);
733 if (!match(rscp, data)) {
734 #ifndef HAVE_CACHE_HEAD_HLIST
740 /* it seems simply set NEGATIVE doesn't work */
741 #ifdef HAVE_CACHE_HEAD_HLIST
742 hlist_del_init(&ch->cache_list);
748 set_bit(CACHE_NEGATIVE, &rscp->h.flags);
749 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
753 write_unlock(&rsc_cache.hash_lock);
757 static int match_uid(struct rsc *rscp, long uid)
761 return ((int) rscp->ctx.gsc_uid == (int) uid);
764 static int match_target(struct rsc *rscp, long target)
766 return (rscp->target == (struct obd_device *) target);
769 static inline void rsc_flush_uid(int uid)
772 CWARN("flush all gss contexts...\n");
774 rsc_flush(match_uid, (long) uid);
777 static inline void rsc_flush_target(struct obd_device *target)
779 rsc_flush(match_target, (long) target);
782 void gss_secsvc_flush(struct obd_device *target)
784 rsc_flush_target(target);
787 static struct rsc *gss_svc_searchbyctx(rawobj_t *handle)
792 memset(&rsci, 0, sizeof(rsci));
793 if (rawobj_dup(&rsci.handle, handle))
796 found = rsc_lookup(&rsci);
800 if (cache_check(&rsc_cache, &found->h, NULL))
805 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
806 struct gss_sec *gsec,
807 struct gss_cli_ctx *gctx)
809 struct rsc rsci, *rscp = NULL;
810 unsigned long ctx_expiry;
815 memset(&rsci, 0, sizeof(rsci));
817 if (rawobj_alloc(&rsci.handle, (char *) &gsec->gs_rvs_hdl,
818 sizeof(gsec->gs_rvs_hdl)))
819 GOTO(out, rc = -ENOMEM);
821 rscp = rsc_lookup(&rsci);
823 GOTO(out, rc = -ENOMEM);
825 major = lgss_copy_reverse_context(gctx->gc_mechctx,
826 &rsci.ctx.gsc_mechctx);
827 if (major != GSS_S_COMPLETE)
828 GOTO(out, rc = -ENOMEM);
830 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
831 CERROR("unable to get expire time, drop it\n");
832 GOTO(out, rc = -EINVAL);
834 rsci.h.expiry_time = (time_t) ctx_expiry;
836 if (strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0)
837 rsci.ctx.gsc_usr_mds = 1;
838 else if (strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0)
839 rsci.ctx.gsc_usr_oss = 1;
841 rsci.ctx.gsc_usr_root = 1;
843 rscp = rsc_update(&rsci, rscp);
845 GOTO(out, rc = -ENOMEM);
847 rscp->target = imp->imp_obd;
848 rawobj_dup(&gctx->gc_svc_handle, &rscp->handle);
850 CWARN("create reverse svc ctx %p to %s: idx "LPX64"\n",
851 &rscp->ctx, obd2cli_tgt(imp->imp_obd), gsec->gs_rvs_hdl);
855 cache_put(&rscp->h, &rsc_cache);
859 CERROR("create reverse svc ctx: idx "LPX64", rc %d\n",
860 gsec->gs_rvs_hdl, rc);
864 int gss_svc_upcall_expire_rvs_ctx(rawobj_t *handle)
866 const cfs_time_t expire = 20;
869 rscp = gss_svc_searchbyctx(handle);
871 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) expire soon\n",
874 rscp->h.expiry_time = cfs_time_current_sec() + expire;
875 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
880 int gss_svc_upcall_dup_handle(rawobj_t *handle, struct gss_svc_ctx *ctx)
882 struct rsc *rscp = container_of(ctx, struct rsc, ctx);
884 return rawobj_dup(handle, &rscp->handle);
887 int gss_svc_upcall_update_sequence(rawobj_t *handle, __u32 seq)
891 rscp = gss_svc_searchbyctx(handle);
893 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) update seq to %u\n",
894 &rscp->ctx, rscp, seq + 1);
896 rscp->ctx.gsc_rvs_seq = seq + 1;
897 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
902 static struct cache_deferred_req* cache_upcall_defer(struct cache_req *req)
906 static struct cache_req cache_upcall_chandle = { cache_upcall_defer };
908 int gss_svc_upcall_handle_init(struct ptlrpc_request *req,
909 struct gss_svc_reqctx *grctx,
910 struct gss_wire_ctx *gw,
911 struct obd_device *target,
916 struct ptlrpc_reply_state *rs;
917 struct rsc *rsci = NULL;
918 struct rsi *rsip = NULL, rsikey;
920 int replen = sizeof(struct ptlrpc_body);
921 struct gss_rep_header *rephdr;
923 int rc = SECSVC_DROP;
926 memset(&rsikey, 0, sizeof(rsikey));
927 rsikey.lustre_svc = lustre_svc;
928 rsikey.nid = (__u64) req->rq_peer.nid;
930 /* duplicate context handle. for INIT it always 0 */
931 if (rawobj_dup(&rsikey.in_handle, &gw->gw_handle)) {
932 CERROR("fail to dup context handle\n");
936 if (rawobj_dup(&rsikey.in_token, in_token)) {
937 CERROR("can't duplicate token\n");
938 rawobj_free(&rsikey.in_handle);
942 rsip = rsi_lookup(&rsikey);
945 CERROR("error in rsi_lookup.\n");
947 if (!gss_pack_err_notify(req, GSS_S_FAILURE, 0))
948 rc = SECSVC_COMPLETE;
953 cache_get(&rsip->h); /* take an extra ref */
954 init_waitqueue_head(&rsip->waitq);
955 init_waitqueue_entry(&wait, current);
956 add_wait_queue(&rsip->waitq, &wait);
959 /* Note each time cache_check() will drop a reference if return
960 * non-zero. We hold an extra reference on initial rsip, but must
961 * take care of following calls. */
962 rc = cache_check(&rsi_cache, &rsip->h, &cache_upcall_chandle);
971 read_lock(&rsi_cache.hash_lock);
972 valid = test_bit(CACHE_VALID, &rsip->h.flags);
974 set_current_state(TASK_INTERRUPTIBLE);
975 read_unlock(&rsi_cache.hash_lock);
978 unsigned long jiffies;
979 jiffies = msecs_to_jiffies(MSEC_PER_SEC *
980 GSS_SVC_UPCALL_TIMEOUT);
981 schedule_timeout(jiffies);
986 CWARN("waited %ds timeout, drop\n", GSS_SVC_UPCALL_TIMEOUT);
990 CWARN("cache_check return ENOENT, drop\n");
993 /* if not the first check, we have to release the extra
994 * reference we just added on it. */
996 cache_put(&rsip->h, &rsi_cache);
997 CDEBUG(D_SEC, "cache_check is good\n");
1001 remove_wait_queue(&rsip->waitq, &wait);
1002 cache_put(&rsip->h, &rsi_cache);
1005 GOTO(out, rc = SECSVC_DROP);
1008 rsci = gss_svc_searchbyctx(&rsip->out_handle);
1010 CERROR("authentication failed\n");
1012 if (!gss_pack_err_notify(req, GSS_S_FAILURE, 0))
1013 rc = SECSVC_COMPLETE;
1017 cache_get(&rsci->h);
1018 grctx->src_ctx = &rsci->ctx;
1021 if (rawobj_dup(&rsci->ctx.gsc_rvs_hdl, rvs_hdl)) {
1022 CERROR("failed duplicate reverse handle\n");
1026 rsci->target = target;
1028 CDEBUG(D_SEC, "server create rsc %p(%u->%s)\n",
1029 rsci, rsci->ctx.gsc_uid, libcfs_nid2str(req->rq_peer.nid));
1031 if (rsip->out_handle.len > PTLRPC_GSS_MAX_HANDLE_SIZE) {
1032 CERROR("handle size %u too large\n", rsip->out_handle.len);
1033 GOTO(out, rc = SECSVC_DROP);
1036 grctx->src_init = 1;
1037 grctx->src_reserve_len = cfs_size_round4(rsip->out_token.len);
1039 rc = lustre_pack_reply_v2(req, 1, &replen, NULL, 0);
1041 CERROR("failed to pack reply: %d\n", rc);
1042 GOTO(out, rc = SECSVC_DROP);
1045 rs = req->rq_reply_state;
1046 LASSERT(rs->rs_repbuf->lm_bufcount == 3);
1047 LASSERT(rs->rs_repbuf->lm_buflens[0] >=
1048 sizeof(*rephdr) + rsip->out_handle.len);
1049 LASSERT(rs->rs_repbuf->lm_buflens[2] >= rsip->out_token.len);
1051 rephdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
1052 rephdr->gh_version = PTLRPC_GSS_VERSION;
1053 rephdr->gh_flags = 0;
1054 rephdr->gh_proc = PTLRPC_GSS_PROC_ERR;
1055 rephdr->gh_major = rsip->major_status;
1056 rephdr->gh_minor = rsip->minor_status;
1057 rephdr->gh_seqwin = GSS_SEQ_WIN;
1058 rephdr->gh_handle.len = rsip->out_handle.len;
1059 memcpy(rephdr->gh_handle.data, rsip->out_handle.data,
1060 rsip->out_handle.len);
1062 memcpy(lustre_msg_buf(rs->rs_repbuf, 2, 0), rsip->out_token.data,
1063 rsip->out_token.len);
1065 rs->rs_repdata_len = lustre_shrink_msg(rs->rs_repbuf, 2,
1066 rsip->out_token.len, 0);
1071 /* it looks like here we should put rsip also, but this mess up
1072 * with NFS cache mgmt code... FIXME */
1075 rsi_put(&rsip->h, &rsi_cache);
1079 /* if anything went wrong, we don't keep the context too */
1080 if (rc != SECSVC_OK)
1081 set_bit(CACHE_NEGATIVE, &rsci->h.flags);
1083 CDEBUG(D_SEC, "create rsc with idx "LPX64"\n",
1084 gss_handle_to_u64(&rsci->handle));
1086 COMPAT_RSC_PUT(&rsci->h, &rsc_cache);
1091 struct gss_svc_ctx *gss_svc_upcall_get_ctx(struct ptlrpc_request *req,
1092 struct gss_wire_ctx *gw)
1096 rsc = gss_svc_searchbyctx(&gw->gw_handle);
1098 CWARN("Invalid gss ctx idx "LPX64" from %s\n",
1099 gss_handle_to_u64(&gw->gw_handle),
1100 libcfs_nid2str(req->rq_peer.nid));
1107 void gss_svc_upcall_put_ctx(struct gss_svc_ctx *ctx)
1109 struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1111 COMPAT_RSC_PUT(&rsc->h, &rsc_cache);
1114 void gss_svc_upcall_destroy_ctx(struct gss_svc_ctx *ctx)
1116 struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1118 /* can't be found */
1119 set_bit(CACHE_NEGATIVE, &rsc->h.flags);
1120 /* to be removed at next scan */
1121 rsc->h.expiry_time = 1;
1124 int __init gss_init_svc_upcall(void)
1128 spin_lock_init(&__ctx_index_lock);
1130 * this helps reducing context index confliction. after server reboot,
1131 * conflicting request from clients might be filtered out by initial
1132 * sequence number checking, thus no chance to sent error notification
1135 cfs_get_random_bytes(&__ctx_index, sizeof(__ctx_index));
1137 rc = _cache_register_net(&rsi_cache, &init_net);
1141 rc = _cache_register_net(&rsc_cache, &init_net);
1143 _cache_unregister_net(&rsi_cache, &init_net);
1147 /* FIXME this looks stupid. we intend to give lsvcgssd a chance to open
1148 * the init upcall channel, otherwise there's big chance that the first
1149 * upcall issued before the channel be opened thus nfsv4 cache code will
1150 * drop the request direclty, thus lead to unnecessary recovery time.
1151 * here we wait at miximum 1.5 seconds. */
1152 for (i = 0; i < 6; i++) {
1153 if (atomic_read(&rsi_cache.readers) > 0)
1155 set_current_state(TASK_UNINTERRUPTIBLE);
1156 LASSERT(msecs_to_jiffies(MSEC_PER_SEC) >= 4);
1157 schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC / 4));
1160 if (atomic_read(&rsi_cache.readers) == 0)
1161 CWARN("Init channel is not opened by lsvcgssd, following "
1162 "request might be dropped until lsvcgssd is active\n");
1167 void gss_exit_svc_upcall(void)
1169 cache_purge(&rsi_cache);
1170 _cache_unregister_net(&rsi_cache, &init_net);
1172 cache_purge(&rsc_cache);
1173 _cache_unregister_net(&rsc_cache, &init_net);