1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Modifications for Lustre
6 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
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
52 #include <linux/types.h>
53 #include <linux/init.h>
54 #include <linux/module.h>
55 #include <linux/slab.h>
56 #include <linux/hash.h>
57 #include <linux/mutex.h>
58 #include <linux/sunrpc/cache.h>
60 #include <liblustre.h>
64 #include <obd_class.h>
65 #include <obd_support.h>
66 #include <lustre/lustre_idl.h>
67 #include <lustre_net.h>
68 #include <lustre_import.h>
69 #include <lustre_sec.h>
72 #include "gss_internal.h"
75 #define GSS_SVC_UPCALL_TIMEOUT (20)
77 static spinlock_t __ctx_index_lock = SPIN_LOCK_UNLOCKED;
78 static __u64 __ctx_index;
80 __u64 gss_get_next_ctx_index(void)
84 spin_lock(&__ctx_index_lock);
86 spin_unlock(&__ctx_index_lock);
91 static inline unsigned long hash_mem(char *buf, int length, int bits)
93 unsigned long hash = 0;
108 if ((len & (BITS_PER_LONG/8-1)) == 0)
109 hash = hash_long(hash^l, BITS_PER_LONG);
112 return hash >> (BITS_PER_LONG - bits);
115 /****************************************
117 ****************************************/
119 #define RSI_HASHBITS (6)
120 #define RSI_HASHMAX (1 << RSI_HASHBITS)
121 #define RSI_HASHMASK (RSI_HASHMAX - 1)
127 wait_queue_head_t waitq;
128 rawobj_t in_handle, in_token;
129 rawobj_t out_handle, out_token;
130 int major_status, minor_status;
133 static struct cache_head *rsi_table[RSI_HASHMAX];
134 static struct cache_detail rsi_cache;
135 #ifdef HAVE_SUNRPC_CACHE_V2
136 static struct rsi *rsi_update(struct rsi *new, struct rsi *old);
137 static struct rsi *rsi_lookup(struct rsi *item);
139 static struct rsi *rsi_lookup(struct rsi *item, int set);
142 static inline int rsi_hash(struct rsi *item)
144 return hash_mem((char *)item->in_handle.data, item->in_handle.len,
146 hash_mem((char *)item->in_token.data, item->in_token.len,
150 static inline int __rsi_match(struct rsi *item, struct rsi *tmp)
152 return (rawobj_equal(&item->in_handle, &tmp->in_handle) &&
153 rawobj_equal(&item->in_token, &tmp->in_token));
156 static void rsi_free(struct rsi *rsi)
158 rawobj_free(&rsi->in_handle);
159 rawobj_free(&rsi->in_token);
160 rawobj_free(&rsi->out_handle);
161 rawobj_free(&rsi->out_token);
164 static void rsi_request(struct cache_detail *cd,
165 struct cache_head *h,
166 char **bpp, int *blen)
168 struct rsi *rsi = container_of(h, struct rsi, h);
171 /* if in_handle is null, provide kernel suggestion */
172 if (rsi->in_handle.len == 0)
173 index = gss_get_next_ctx_index();
175 qword_addhex(bpp, blen, (char *) &rsi->lustre_svc,
176 sizeof(rsi->lustre_svc));
177 qword_addhex(bpp, blen, (char *) &rsi->nid, sizeof(rsi->nid));
178 qword_addhex(bpp, blen, (char *) &index, sizeof(index));
179 qword_addhex(bpp, blen, rsi->in_handle.data, rsi->in_handle.len);
180 qword_addhex(bpp, blen, rsi->in_token.data, rsi->in_token.len);
184 static inline void __rsi_init(struct rsi *new, struct rsi *item)
186 new->out_handle = RAWOBJ_EMPTY;
187 new->out_token = RAWOBJ_EMPTY;
189 new->in_handle = item->in_handle;
190 item->in_handle = RAWOBJ_EMPTY;
191 new->in_token = item->in_token;
192 item->in_token = RAWOBJ_EMPTY;
194 new->lustre_svc = item->lustre_svc;
195 new->nid = item->nid;
196 init_waitqueue_head(&new->waitq);
199 static inline void __rsi_update(struct rsi *new, struct rsi *item)
201 LASSERT(new->out_handle.len == 0);
202 LASSERT(new->out_token.len == 0);
204 new->out_handle = item->out_handle;
205 item->out_handle = RAWOBJ_EMPTY;
206 new->out_token = item->out_token;
207 item->out_token = RAWOBJ_EMPTY;
209 new->major_status = item->major_status;
210 new->minor_status = item->minor_status;
213 #ifdef HAVE_SUNRPC_CACHE_V2
215 static void rsi_put(struct kref *ref)
217 struct rsi *rsi = container_of(ref, struct rsi, h.ref);
219 LASSERT(rsi->h.next == NULL);
224 static int rsi_match(struct cache_head *a, struct cache_head *b)
226 struct rsi *item = container_of(a, struct rsi, h);
227 struct rsi *tmp = container_of(b, struct rsi, h);
229 return __rsi_match(item, tmp);
232 static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
234 struct rsi *new = container_of(cnew, struct rsi, h);
235 struct rsi *item = container_of(citem, struct rsi, h);
237 __rsi_init(new, item);
240 static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
242 struct rsi *new = container_of(cnew, struct rsi, h);
243 struct rsi *item = container_of(citem, struct rsi, h);
245 __rsi_update(new, item);
248 static struct cache_head *rsi_alloc(void)
259 static int rsi_parse(struct cache_detail *cd, char *mesg, int mlen)
264 struct rsi rsii, *rsip = NULL;
266 int status = -EINVAL;
270 memset(&rsii, 0, sizeof(rsii));
273 len = qword_get(&mesg, buf, mlen);
276 if (rawobj_alloc(&rsii.in_handle, buf, len)) {
282 len = qword_get(&mesg, buf, mlen);
285 if (rawobj_alloc(&rsii.in_token, buf, len)) {
290 rsip = rsi_lookup(&rsii);
296 expiry = get_expiry(&mesg);
300 len = qword_get(&mesg, buf, mlen);
305 rsii.major_status = simple_strtol(buf, &ep, 10);
310 len = qword_get(&mesg, buf, mlen);
313 rsii.minor_status = simple_strtol(buf, &ep, 10);
318 len = qword_get(&mesg, buf, mlen);
321 if (rawobj_alloc(&rsii.out_handle, buf, len)) {
327 len = qword_get(&mesg, buf, mlen);
330 if (rawobj_alloc(&rsii.out_token, buf, len)) {
335 rsii.h.expiry_time = expiry;
336 rsip = rsi_update(&rsii, rsip);
341 wake_up_all(&rsip->waitq);
342 cache_put(&rsip->h, &rsi_cache);
348 CERROR("rsi parse error %d\n", status);
352 #else /* !HAVE_SUNRPC_CACHE_V2 */
354 static void rsi_put(struct cache_head *item, struct cache_detail *cd)
356 struct rsi *rsi = container_of(item, struct rsi, h);
358 LASSERT(atomic_read(&item->refcnt) > 0);
360 if (cache_put(item, cd)) {
361 LASSERT(item->next == NULL);
363 kfree(rsi); /* created by cache mgmt using kmalloc */
367 static inline int rsi_match(struct rsi *item, struct rsi *tmp)
369 return __rsi_match(item, tmp);
372 static inline void rsi_init(struct rsi *new, struct rsi *item)
374 __rsi_init(new, item);
377 static inline void rsi_update(struct rsi *new, struct rsi *item)
379 __rsi_update(new, item);
382 static int rsi_parse(struct cache_detail *cd, char *mesg, int mlen)
387 struct rsi rsii, *rsip = NULL;
389 int status = -EINVAL;
393 memset(&rsii, 0, sizeof(rsii));
396 len = qword_get(&mesg, buf, mlen);
399 if (rawobj_alloc(&rsii.in_handle, buf, len)) {
405 len = qword_get(&mesg, buf, mlen);
408 if (rawobj_alloc(&rsii.in_token, buf, len)) {
414 expiry = get_expiry(&mesg);
418 len = qword_get(&mesg, buf, mlen);
423 rsii.major_status = simple_strtol(buf, &ep, 10);
428 len = qword_get(&mesg, buf, mlen);
431 rsii.minor_status = simple_strtol(buf, &ep, 10);
436 len = qword_get(&mesg, buf, mlen);
439 if (rawobj_alloc(&rsii.out_handle, buf, len)) {
445 len = qword_get(&mesg, buf, mlen);
448 if (rawobj_alloc(&rsii.out_token, buf, len)) {
453 rsii.h.expiry_time = expiry;
454 rsip = rsi_lookup(&rsii, 1);
459 wake_up_all(&rsip->waitq);
460 rsi_put(&rsip->h, &rsi_cache);
464 CERROR("rsi parse error %d\n", status);
468 #endif /* HAVE_SUNRPC_CACHE_V2 */
470 static struct cache_detail rsi_cache = {
471 .hash_size = RSI_HASHMAX,
472 .hash_table = rsi_table,
473 .name = "auth.sptlrpc.init",
474 .cache_put = rsi_put,
475 .cache_request = rsi_request,
476 .cache_parse = rsi_parse,
477 #ifdef HAVE_SUNRPC_CACHE_V2
480 .update = update_rsi,
485 #ifdef HAVE_SUNRPC_CACHE_V2
487 static struct rsi *rsi_lookup(struct rsi *item)
489 struct cache_head *ch;
490 int hash = rsi_hash(item);
492 ch = sunrpc_cache_lookup(&rsi_cache, &item->h, hash);
494 return container_of(ch, struct rsi, h);
499 static struct rsi *rsi_update(struct rsi *new, struct rsi *old)
501 struct cache_head *ch;
502 int hash = rsi_hash(new);
504 ch = sunrpc_cache_update(&rsi_cache, &new->h, &old->h, hash);
506 return container_of(ch, struct rsi, h);
513 static DefineSimpleCacheLookup(rsi, 0)
517 /****************************************
519 ****************************************/
521 #define RSC_HASHBITS (10)
522 #define RSC_HASHMAX (1 << RSC_HASHBITS)
523 #define RSC_HASHMASK (RSC_HASHMAX - 1)
527 struct obd_device *target;
529 struct gss_svc_ctx ctx;
532 static struct cache_head *rsc_table[RSC_HASHMAX];
533 static struct cache_detail rsc_cache;
534 #ifdef HAVE_SUNRPC_CACHE_V2
535 static struct rsc *rsc_update(struct rsc *new, struct rsc *old);
536 static struct rsc *rsc_lookup(struct rsc *item);
538 static struct rsc *rsc_lookup(struct rsc *item, int set);
541 static void rsc_free(struct rsc *rsci)
543 rawobj_free(&rsci->handle);
544 rawobj_free(&rsci->ctx.gsc_rvs_hdl);
545 lgss_delete_sec_context(&rsci->ctx.gsc_mechctx);
548 static inline int rsc_hash(struct rsc *rsci)
550 return hash_mem((char *)rsci->handle.data,
551 rsci->handle.len, RSC_HASHBITS);
554 static inline int __rsc_match(struct rsc *new, struct rsc *tmp)
556 return rawobj_equal(&new->handle, &tmp->handle);
559 static inline void __rsc_init(struct rsc *new, struct rsc *tmp)
561 new->handle = tmp->handle;
562 tmp->handle = RAWOBJ_EMPTY;
565 memset(&new->ctx, 0, sizeof(new->ctx));
566 new->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
569 static inline void __rsc_update(struct rsc *new, struct rsc *tmp)
572 tmp->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
573 tmp->ctx.gsc_mechctx = NULL;
575 memset(&new->ctx.gsc_seqdata, 0, sizeof(new->ctx.gsc_seqdata));
576 spin_lock_init(&new->ctx.gsc_seqdata.ssd_lock);
579 #ifdef HAVE_SUNRPC_CACHE_V2
581 static void rsc_put(struct kref *ref)
583 struct rsc *rsci = container_of(ref, struct rsc, h.ref);
585 LASSERT(rsci->h.next == NULL);
590 static int rsc_match(struct cache_head *a, struct cache_head *b)
592 struct rsc *new = container_of(a, struct rsc, h);
593 struct rsc *tmp = container_of(b, struct rsc, h);
595 return __rsc_match(new, tmp);
598 static void rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
600 struct rsc *new = container_of(cnew, struct rsc, h);
601 struct rsc *tmp = container_of(ctmp, struct rsc, h);
603 __rsc_init(new, tmp);
606 static void update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
608 struct rsc *new = container_of(cnew, struct rsc, h);
609 struct rsc *tmp = container_of(ctmp, struct rsc, h);
611 __rsc_update(new, tmp);
614 static struct cache_head * rsc_alloc(void)
625 static int rsc_parse(struct cache_detail *cd, char *mesg, int mlen)
628 int len, rv, tmp_int;
629 struct rsc rsci, *rscp = NULL;
631 int status = -EINVAL;
632 struct gss_api_mech *gm = NULL;
634 memset(&rsci, 0, sizeof(rsci));
637 len = qword_get(&mesg, buf, mlen);
638 if (len < 0) goto out;
640 if (rawobj_alloc(&rsci.handle, buf, len))
645 expiry = get_expiry(&mesg);
651 rv = get_int(&mesg, &tmp_int);
653 CERROR("fail to get remote flag\n");
656 rsci.ctx.gsc_remote = (tmp_int != 0);
659 rv = get_int(&mesg, &tmp_int);
661 CERROR("fail to get oss user flag\n");
664 rsci.ctx.gsc_usr_root = (tmp_int != 0);
667 rv = get_int(&mesg, &tmp_int);
669 CERROR("fail to get mds user flag\n");
672 rsci.ctx.gsc_usr_mds = (tmp_int != 0);
675 rv = get_int(&mesg, (int *) &rsci.ctx.gsc_mapped_uid);
677 CERROR("fail to get mapped uid\n");
681 rscp = rsc_lookup(&rsci);
685 /* uid, or NEGATIVE */
686 rv = get_int(&mesg, (int *) &rsci.ctx.gsc_uid);
690 CERROR("NOENT? set rsc entry negative\n");
691 set_bit(CACHE_NEGATIVE, &rsci.h.flags);
694 unsigned long ctx_expiry;
697 if (get_int(&mesg, (int *) &rsci.ctx.gsc_gid))
701 len = qword_get(&mesg, buf, mlen);
704 gm = lgss_name_to_mech(buf);
705 status = -EOPNOTSUPP;
710 /* mech-specific data: */
711 len = qword_get(&mesg, buf, mlen);
716 tmp_buf.data = (unsigned char *)buf;
717 if (lgss_import_sec_context(&tmp_buf, gm,
718 &rsci.ctx.gsc_mechctx))
721 /* currently the expiry time passed down from user-space
722 * is invalid, here we retrive it from mech. */
723 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
724 CERROR("unable to get expire time, drop it\n");
727 expiry = (time_t) ctx_expiry;
730 rsci.h.expiry_time = expiry;
731 rscp = rsc_update(&rsci, rscp);
738 cache_put(&rscp->h, &rsc_cache);
743 CERROR("parse rsc error %d\n", status);
747 #else /* !HAVE_SUNRPC_CACHE_V2 */
749 static void rsc_put(struct cache_head *item, struct cache_detail *cd)
751 struct rsc *rsci = container_of(item, struct rsc, h);
753 LASSERT(atomic_read(&item->refcnt) > 0);
755 if (cache_put(item, cd)) {
756 LASSERT(item->next == NULL);
758 kfree(rsci); /* created by cache mgmt using kmalloc */
762 static inline int rsc_match(struct rsc *new, struct rsc *tmp)
764 return __rsc_match(new, tmp);
767 static inline void rsc_init(struct rsc *new, struct rsc *tmp)
769 __rsc_init(new, tmp);
772 static inline void rsc_update(struct rsc *new, struct rsc *tmp)
774 __rsc_update(new, tmp);
777 static int rsc_parse(struct cache_detail *cd, char *mesg, int mlen)
780 int len, rv, tmp_int;
781 struct rsc rsci, *rscp = NULL;
783 int status = -EINVAL;
785 memset(&rsci, 0, sizeof(rsci));
788 len = qword_get(&mesg, buf, mlen);
789 if (len < 0) goto out;
791 if (rawobj_alloc(&rsci.handle, buf, len))
796 expiry = get_expiry(&mesg);
802 rv = get_int(&mesg, &tmp_int);
804 CERROR("fail to get remote flag\n");
807 rsci.ctx.gsc_remote = (tmp_int != 0);
810 rv = get_int(&mesg, &tmp_int);
812 CERROR("fail to get oss user flag\n");
815 rsci.ctx.gsc_usr_root = (tmp_int != 0);
818 rv = get_int(&mesg, &tmp_int);
820 CERROR("fail to get mds user flag\n");
823 rsci.ctx.gsc_usr_mds = (tmp_int != 0);
826 rv = get_int(&mesg, (int *) &rsci.ctx.gsc_mapped_uid);
828 CERROR("fail to get mapped uid\n");
832 /* uid, or NEGATIVE */
833 rv = get_int(&mesg, (int *) &rsci.ctx.gsc_uid);
837 CERROR("NOENT? set rsc entry negative\n");
838 set_bit(CACHE_NEGATIVE, &rsci.h.flags);
840 struct gss_api_mech *gm;
842 unsigned long ctx_expiry;
845 if (get_int(&mesg, (int *) &rsci.ctx.gsc_gid))
849 len = qword_get(&mesg, buf, mlen);
852 gm = lgss_name_to_mech(buf);
853 status = -EOPNOTSUPP;
858 /* mech-specific data: */
859 len = qword_get(&mesg, buf, mlen);
865 tmp_buf.data = (unsigned char *)buf;
866 if (lgss_import_sec_context(&tmp_buf, gm,
867 &rsci.ctx.gsc_mechctx)) {
872 /* currently the expiry time passed down from user-space
873 * is invalid, here we retrive it from mech. */
874 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
875 CERROR("unable to get expire time, drop it\n");
879 expiry = (time_t) ctx_expiry;
884 rsci.h.expiry_time = expiry;
885 rscp = rsc_lookup(&rsci, 1);
890 rsc_put(&rscp->h, &rsc_cache);
893 CERROR("parse rsc error %d\n", status);
897 #endif /* HAVE_SUNRPC_CACHE_V2 */
900 static struct cache_detail rsc_cache = {
901 .hash_size = RSC_HASHMAX,
902 .hash_table = rsc_table,
903 .name = "auth.sptlrpc.context",
904 .cache_put = rsc_put,
905 .cache_parse = rsc_parse,
906 #ifdef HAVE_SUNRPC_CACHE_V2
909 .update = update_rsc,
914 #ifdef HAVE_SUNRPC_CACHE_V2
916 static struct rsc *rsc_lookup(struct rsc *item)
918 struct cache_head *ch;
919 int hash = rsc_hash(item);
921 ch = sunrpc_cache_lookup(&rsc_cache, &item->h, hash);
923 return container_of(ch, struct rsc, h);
928 static struct rsc *rsc_update(struct rsc *new, struct rsc *old)
930 struct cache_head *ch;
931 int hash = rsc_hash(new);
933 ch = sunrpc_cache_update(&rsc_cache, &new->h, &old->h, hash);
935 return container_of(ch, struct rsc, h);
940 #define COMPAT_RSC_PUT(item, cd) cache_put((item), (cd))
944 static DefineSimpleCacheLookup(rsc, 0);
946 #define COMPAT_RSC_PUT(item, cd) rsc_put((item), (cd))
950 /****************************************
952 ****************************************/
954 typedef int rsc_entry_match(struct rsc *rscp, long data);
956 static void rsc_flush(rsc_entry_match *match, long data)
958 struct cache_head **ch;
963 write_lock(&rsc_cache.hash_lock);
964 for (n = 0; n < RSC_HASHMAX; n++) {
965 for (ch = &rsc_cache.hash_table[n]; *ch;) {
966 rscp = container_of(*ch, struct rsc, h);
968 if (!match(rscp, data)) {
973 /* it seems simply set NEGATIVE doesn't work */
977 set_bit(CACHE_NEGATIVE, &rscp->h.flags);
978 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
982 write_unlock(&rsc_cache.hash_lock);
986 static int match_uid(struct rsc *rscp, long uid)
990 return ((int) rscp->ctx.gsc_uid == (int) uid);
993 static int match_target(struct rsc *rscp, long target)
995 return (rscp->target == (struct obd_device *) target);
998 static inline void rsc_flush_uid(int uid)
1001 CWARN("flush all gss contexts...\n");
1003 rsc_flush(match_uid, (long) uid);
1006 static inline void rsc_flush_target(struct obd_device *target)
1008 rsc_flush(match_target, (long) target);
1011 void gss_secsvc_flush(struct obd_device *target)
1013 rsc_flush_target(target);
1015 EXPORT_SYMBOL(gss_secsvc_flush);
1017 static struct rsc *gss_svc_searchbyctx(rawobj_t *handle)
1022 memset(&rsci, 0, sizeof(rsci));
1023 if (rawobj_dup(&rsci.handle, handle))
1026 #ifdef HAVE_SUNRPC_CACHE_V2
1027 found = rsc_lookup(&rsci);
1029 found = rsc_lookup(&rsci, 0);
1034 if (cache_check(&rsc_cache, &found->h, NULL))
1039 #ifdef HAVE_SUNRPC_CACHE_V2
1041 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
1042 struct gss_sec *gsec,
1043 struct gss_cli_ctx *gctx)
1045 struct rsc rsci, *rscp = NULL;
1046 unsigned long ctx_expiry;
1051 memset(&rsci, 0, sizeof(rsci));
1053 if (rawobj_alloc(&rsci.handle, (char *) &gsec->gs_rvs_hdl,
1054 sizeof(gsec->gs_rvs_hdl)))
1055 GOTO(out, rc = -ENOMEM);
1057 rscp = rsc_lookup(&rsci);
1059 GOTO(out, rc = -ENOMEM);
1061 major = lgss_copy_reverse_context(gctx->gc_mechctx,
1062 &rsci.ctx.gsc_mechctx);
1063 if (major != GSS_S_COMPLETE)
1064 GOTO(out, rc = -ENOMEM);
1066 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
1067 CERROR("unable to get expire time, drop it\n");
1068 GOTO(out, rc = -EINVAL);
1070 rsci.h.expiry_time = (time_t) ctx_expiry;
1073 rsci.ctx.gsc_usr_root = 1;
1074 rsci.ctx.gsc_usr_mds= 1;
1075 rsci.ctx.gsc_reverse = 1;
1077 rscp = rsc_update(&rsci, rscp);
1079 GOTO(out, rc = -ENOMEM);
1081 rscp->target = imp->imp_obd;
1082 rawobj_dup(&gctx->gc_svc_handle, &rscp->handle);
1084 CWARN("create reverse svc ctx %p to %s: idx "LPX64"\n",
1085 &rscp->ctx, obd2cli_tgt(imp->imp_obd), gsec->gs_rvs_hdl);
1089 cache_put(&rscp->h, &rsc_cache);
1093 CERROR("create reverse svc ctx: idx "LPX64", rc %d\n",
1094 gsec->gs_rvs_hdl, rc);
1098 #else /* !HAVE_SUNRPC_CACHE_V2 */
1100 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
1101 struct gss_sec *gsec,
1102 struct gss_cli_ctx *gctx)
1104 struct rsc rsci, *rscp;
1105 unsigned long ctx_expiry;
1110 memset(&rsci, 0, sizeof(rsci));
1112 if (rawobj_alloc(&rsci.handle, (char *) &gsec->gs_rvs_hdl,
1113 sizeof(gsec->gs_rvs_hdl)))
1114 GOTO(out, rc = -ENOMEM);
1116 major = lgss_copy_reverse_context(gctx->gc_mechctx,
1117 &rsci.ctx.gsc_mechctx);
1118 if (major != GSS_S_COMPLETE)
1119 GOTO(out, rc = -ENOMEM);
1121 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
1122 CERROR("unable to get expire time, drop it\n");
1123 GOTO(out, rc = -ENOMEM);
1125 rsci.h.expiry_time = (time_t) ctx_expiry;
1128 rsci.ctx.gsc_usr_root = 1;
1129 rsci.ctx.gsc_usr_mds= 1;
1130 rsci.ctx.gsc_reverse = 1;
1132 rscp = rsc_lookup(&rsci, 1);
1134 CERROR("rsc lookup failed\n");
1135 GOTO(out, rc = -ENOMEM);
1138 rscp->target = imp->imp_obd;
1139 rawobj_dup(&gctx->gc_svc_handle, &rscp->handle);
1141 CWARN("create reverse svc ctx %p to %s: idx "LPX64"\n",
1142 &rscp->ctx, obd2cli_tgt(imp->imp_obd), gsec->gs_rvs_hdl);
1143 rsc_put(&rscp->h, &rsc_cache);
1148 CERROR("create reverse svc ctx: idx "LPX64", rc %d\n",
1149 gsec->gs_rvs_hdl, rc);
1153 #endif /* HAVE_SUNRPC_CACHE_V2 */
1155 int gss_svc_upcall_expire_rvs_ctx(rawobj_t *handle)
1157 const cfs_time_t expire = 20;
1160 rscp = gss_svc_searchbyctx(handle);
1162 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) expire soon\n",
1165 rscp->h.expiry_time = cfs_time_current_sec() + expire;
1166 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
1171 int gss_svc_upcall_dup_handle(rawobj_t *handle, struct gss_svc_ctx *ctx)
1173 struct rsc *rscp = container_of(ctx, struct rsc, ctx);
1175 return rawobj_dup(handle, &rscp->handle);
1178 int gss_svc_upcall_update_sequence(rawobj_t *handle, __u32 seq)
1182 rscp = gss_svc_searchbyctx(handle);
1184 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) update seq to %u\n",
1185 &rscp->ctx, rscp, seq + 1);
1187 rscp->ctx.gsc_rvs_seq = seq + 1;
1188 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
1193 static struct cache_deferred_req* cache_upcall_defer(struct cache_req *req)
1197 static struct cache_req cache_upcall_chandle = { cache_upcall_defer };
1199 int gss_svc_upcall_handle_init(struct ptlrpc_request *req,
1200 struct gss_svc_reqctx *grctx,
1201 struct gss_wire_ctx *gw,
1202 struct obd_device *target,
1207 struct ptlrpc_reply_state *rs;
1208 struct rsc *rsci = NULL;
1209 struct rsi *rsip = NULL, rsikey;
1211 int replen = sizeof(struct ptlrpc_body);
1212 struct gss_rep_header *rephdr;
1213 int first_check = 1;
1214 int rc = SECSVC_DROP;
1217 memset(&rsikey, 0, sizeof(rsikey));
1218 rsikey.lustre_svc = lustre_svc;
1219 rsikey.nid = (__u64) req->rq_peer.nid;
1221 /* duplicate context handle. for INIT it always 0 */
1222 if (rawobj_dup(&rsikey.in_handle, &gw->gw_handle)) {
1223 CERROR("fail to dup context handle\n");
1227 if (rawobj_dup(&rsikey.in_token, in_token)) {
1228 CERROR("can't duplicate token\n");
1229 rawobj_free(&rsikey.in_handle);
1233 #ifdef HAVE_SUNRPC_CACHE_V2
1234 rsip = rsi_lookup(&rsikey);
1236 rsip = rsi_lookup(&rsikey, 0);
1240 CERROR("error in rsi_lookup.\n");
1242 if (!gss_pack_err_notify(req, GSS_S_FAILURE, 0))
1243 rc = SECSVC_COMPLETE;
1248 cache_get(&rsip->h); /* take an extra ref */
1249 init_waitqueue_head(&rsip->waitq);
1250 init_waitqueue_entry(&wait, current);
1251 add_wait_queue(&rsip->waitq, &wait);
1254 /* Note each time cache_check() will drop a reference if return
1255 * non-zero. We hold an extra reference on initial rsip, but must
1256 * take care of following calls. */
1257 rc = cache_check(&rsi_cache, &rsip->h, &cache_upcall_chandle);
1265 read_lock(&rsi_cache.hash_lock);
1266 valid = test_bit(CACHE_VALID, &rsip->h.flags);
1268 set_current_state(TASK_INTERRUPTIBLE);
1269 read_unlock(&rsi_cache.hash_lock);
1272 schedule_timeout(GSS_SVC_UPCALL_TIMEOUT * HZ);
1274 cache_get(&rsip->h);
1277 CWARN("waited %ds timeout, drop\n", GSS_SVC_UPCALL_TIMEOUT);
1281 CWARN("cache_check return ENOENT, drop\n");
1284 /* if not the first check, we have to release the extra
1285 * reference we just added on it. */
1287 cache_put(&rsip->h, &rsi_cache);
1288 CDEBUG(D_SEC, "cache_check is good\n");
1292 remove_wait_queue(&rsip->waitq, &wait);
1293 cache_put(&rsip->h, &rsi_cache);
1296 GOTO(out, rc = SECSVC_DROP);
1299 rsci = gss_svc_searchbyctx(&rsip->out_handle);
1301 CERROR("authentication failed\n");
1303 if (!gss_pack_err_notify(req, GSS_S_FAILURE, 0))
1304 rc = SECSVC_COMPLETE;
1308 cache_get(&rsci->h);
1309 grctx->src_ctx = &rsci->ctx;
1312 if (rawobj_dup(&rsci->ctx.gsc_rvs_hdl, rvs_hdl)) {
1313 CERROR("failed duplicate reverse handle\n");
1317 rsci->target = target;
1319 CDEBUG(D_SEC, "server create rsc %p(%u->%s)\n",
1320 rsci, rsci->ctx.gsc_uid, libcfs_nid2str(req->rq_peer.nid));
1322 if (rsip->out_handle.len > PTLRPC_GSS_MAX_HANDLE_SIZE) {
1323 CERROR("handle size %u too large\n", rsip->out_handle.len);
1324 GOTO(out, rc = SECSVC_DROP);
1327 grctx->src_init = 1;
1328 grctx->src_reserve_len = size_round4(rsip->out_token.len);
1330 rc = lustre_pack_reply_v2(req, 1, &replen, NULL, 0);
1332 CERROR("failed to pack reply: %d\n", rc);
1333 GOTO(out, rc = SECSVC_DROP);
1336 rs = req->rq_reply_state;
1337 LASSERT(rs->rs_repbuf->lm_bufcount == 3);
1338 LASSERT(rs->rs_repbuf->lm_buflens[0] >=
1339 sizeof(*rephdr) + rsip->out_handle.len);
1340 LASSERT(rs->rs_repbuf->lm_buflens[2] >= rsip->out_token.len);
1342 rephdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
1343 rephdr->gh_version = PTLRPC_GSS_VERSION;
1344 rephdr->gh_flags = 0;
1345 rephdr->gh_proc = PTLRPC_GSS_PROC_ERR;
1346 rephdr->gh_major = rsip->major_status;
1347 rephdr->gh_minor = rsip->minor_status;
1348 rephdr->gh_seqwin = GSS_SEQ_WIN;
1349 rephdr->gh_handle.len = rsip->out_handle.len;
1350 memcpy(rephdr->gh_handle.data, rsip->out_handle.data,
1351 rsip->out_handle.len);
1353 memcpy(lustre_msg_buf(rs->rs_repbuf, 2, 0), rsip->out_token.data,
1354 rsip->out_token.len);
1356 rs->rs_repdata_len = lustre_shrink_msg(rs->rs_repbuf, 2,
1357 rsip->out_token.len, 0);
1362 /* it looks like here we should put rsip also, but this mess up
1363 * with NFS cache mgmt code... FIXME */
1366 rsi_put(&rsip->h, &rsi_cache);
1370 /* if anything went wrong, we don't keep the context too */
1371 if (rc != SECSVC_OK)
1372 set_bit(CACHE_NEGATIVE, &rsci->h.flags);
1374 CDEBUG(D_SEC, "create rsc with idx "LPX64"\n",
1375 gss_handle_to_u64(&rsci->handle));
1377 COMPAT_RSC_PUT(&rsci->h, &rsc_cache);
1382 struct gss_svc_ctx *gss_svc_upcall_get_ctx(struct ptlrpc_request *req,
1383 struct gss_wire_ctx *gw)
1387 rsc = gss_svc_searchbyctx(&gw->gw_handle);
1389 CWARN("Invalid gss ctx idx "LPX64" from %s\n",
1390 gss_handle_to_u64(&gw->gw_handle),
1391 libcfs_nid2str(req->rq_peer.nid));
1398 void gss_svc_upcall_put_ctx(struct gss_svc_ctx *ctx)
1400 struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1402 COMPAT_RSC_PUT(&rsc->h, &rsc_cache);
1405 void gss_svc_upcall_destroy_ctx(struct gss_svc_ctx *ctx)
1407 struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1409 /* can't be found */
1410 set_bit(CACHE_NEGATIVE, &rsc->h.flags);
1411 /* to be removed at next scan */
1412 rsc->h.expiry_time = 1;
1415 int __init gss_init_svc_upcall(void)
1419 cache_register(&rsi_cache);
1420 cache_register(&rsc_cache);
1422 /* FIXME this looks stupid. we intend to give lsvcgssd a chance to open
1423 * the init upcall channel, otherwise there's big chance that the first
1424 * upcall issued before the channel be opened thus nfsv4 cache code will
1425 * drop the request direclty, thus lead to unnecessary recovery time.
1426 * here we wait at miximum 1.5 seconds. */
1427 for (i = 0; i < 6; i++) {
1428 if (atomic_read(&rsi_cache.readers) > 0)
1430 set_current_state(TASK_UNINTERRUPTIBLE);
1432 schedule_timeout(HZ / 4);
1435 if (atomic_read(&rsi_cache.readers) == 0)
1436 CWARN("Init channel is not opened by lsvcgssd, following "
1437 "request might be dropped until lsvcgssd is active\n");
1439 /* this helps reducing context index confliction. after server reboot,
1440 * conflicting request from clients might be filtered out by initial
1441 * sequence number checking, thus no chance to sent error notification
1442 * back to clients. */
1443 get_random_bytes(&__ctx_index, sizeof(__ctx_index));
1448 void __exit gss_exit_svc_upcall(void)
1452 cache_purge(&rsi_cache);
1453 if ((rc = cache_unregister(&rsi_cache)))
1454 CERROR("unregister rsi cache: %d\n", rc);
1456 cache_purge(&rsc_cache);
1457 if ((rc = cache_unregister(&rsc_cache)))
1458 CERROR("unregister rsc cache: %d\n", rc);