1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Modifications for Lustre
6 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
8 * Copyright (c) 2012, Whamcloud, Inc.
10 * Author: Eric Mei <ericm@clusterfs.com>
14 * Neil Brown <neilb@cse.unsw.edu.au>
15 * J. Bruce Fields <bfields@umich.edu>
16 * Andy Adamson <andros@umich.edu>
17 * Dug Song <dugsong@monkey.org>
19 * RPCSEC_GSS server authentication.
20 * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
23 * The RPCSEC_GSS involves three stages:
26 * 3/ context destruction
28 * Context creation is handled largely by upcalls to user-space.
29 * In particular, GSS_Accept_sec_context is handled by an upcall
30 * Data exchange is handled entirely within the kernel
31 * In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
32 * Context destruction is handled in-kernel
33 * GSS_Delete_sec_context is in-kernel
35 * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
36 * The context handle and gss_token are used as a key into the rpcsec_init cache.
37 * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
38 * being major_status, minor_status, context_handle, reply_token.
39 * These are sent back to the client.
40 * Sequence window management is handled by the kernel. The window size if currently
41 * a compile time constant.
43 * When user-space is happy that a context is established, it places an entry
44 * in the rpcsec_context cache. The key for this cache is the context_handle.
45 * The content includes:
46 * uid/gidlist - for determining access rights
48 * mechanism specific information, such as a key
52 #define DEBUG_SUBSYSTEM S_SEC
54 #include <linux/types.h>
55 #include <linux/init.h>
56 #include <linux/module.h>
57 #include <linux/slab.h>
58 #include <linux/hash.h>
59 #include <linux/mutex.h>
60 #include <linux/sunrpc/cache.h>
62 #include <liblustre.h>
66 #include <obd_class.h>
67 #include <obd_support.h>
68 #include <lustre/lustre_idl.h>
69 #include <lustre_net.h>
70 #include <lustre_import.h>
71 #include <lustre_sec.h>
74 #include "gss_internal.h"
77 #define GSS_SVC_UPCALL_TIMEOUT (20)
79 static cfs_spinlock_t __ctx_index_lock;
80 static __u64 __ctx_index;
82 __u64 gss_get_next_ctx_index(void)
86 cfs_spin_lock(&__ctx_index_lock);
88 cfs_spin_unlock(&__ctx_index_lock);
93 static inline unsigned long hash_mem(char *buf, int length, int bits)
95 unsigned long hash = 0;
110 if ((len & (BITS_PER_LONG/8-1)) == 0)
111 hash = cfs_hash_long(hash^l, BITS_PER_LONG);
114 return hash >> (BITS_PER_LONG - bits);
117 /****************************************
119 ****************************************/
121 #define RSI_HASHBITS (6)
122 #define RSI_HASHMAX (1 << RSI_HASHBITS)
123 #define RSI_HASHMASK (RSI_HASHMAX - 1)
130 rawobj_t in_handle, in_token;
131 rawobj_t out_handle, out_token;
132 int major_status, minor_status;
135 static struct cache_head *rsi_table[RSI_HASHMAX];
136 static struct cache_detail rsi_cache;
137 #ifdef HAVE_SUNRPC_CACHE_V2
138 static struct rsi *rsi_update(struct rsi *new, struct rsi *old);
139 static struct rsi *rsi_lookup(struct rsi *item);
141 static struct rsi *rsi_lookup(struct rsi *item, int set);
144 static inline int rsi_hash(struct rsi *item)
146 return hash_mem((char *)item->in_handle.data, item->in_handle.len,
148 hash_mem((char *)item->in_token.data, item->in_token.len,
152 static inline int __rsi_match(struct rsi *item, struct rsi *tmp)
154 return (rawobj_equal(&item->in_handle, &tmp->in_handle) &&
155 rawobj_equal(&item->in_token, &tmp->in_token));
158 static void rsi_free(struct rsi *rsi)
160 rawobj_free(&rsi->in_handle);
161 rawobj_free(&rsi->in_token);
162 rawobj_free(&rsi->out_handle);
163 rawobj_free(&rsi->out_token);
166 static void rsi_request(struct cache_detail *cd,
167 struct cache_head *h,
168 char **bpp, int *blen)
170 struct rsi *rsi = container_of(h, struct rsi, h);
173 /* if in_handle is null, provide kernel suggestion */
174 if (rsi->in_handle.len == 0)
175 index = gss_get_next_ctx_index();
177 qword_addhex(bpp, blen, (char *) &rsi->lustre_svc,
178 sizeof(rsi->lustre_svc));
179 qword_addhex(bpp, blen, (char *) &rsi->nid, sizeof(rsi->nid));
180 qword_addhex(bpp, blen, (char *) &index, sizeof(index));
181 qword_addhex(bpp, blen, rsi->in_handle.data, rsi->in_handle.len);
182 qword_addhex(bpp, blen, rsi->in_token.data, rsi->in_token.len);
186 #ifdef HAVE_CACHE_UPCALL
187 static int rsi_upcall(struct cache_detail *cd, struct cache_head *h)
189 return sunrpc_cache_pipe_upcall(cd, h, rsi_request);
193 static inline void __rsi_init(struct rsi *new, struct rsi *item)
195 new->out_handle = RAWOBJ_EMPTY;
196 new->out_token = RAWOBJ_EMPTY;
198 new->in_handle = item->in_handle;
199 item->in_handle = RAWOBJ_EMPTY;
200 new->in_token = item->in_token;
201 item->in_token = RAWOBJ_EMPTY;
203 new->lustre_svc = item->lustre_svc;
204 new->nid = item->nid;
205 cfs_waitq_init(&new->waitq);
208 static inline void __rsi_update(struct rsi *new, struct rsi *item)
210 LASSERT(new->out_handle.len == 0);
211 LASSERT(new->out_token.len == 0);
213 new->out_handle = item->out_handle;
214 item->out_handle = RAWOBJ_EMPTY;
215 new->out_token = item->out_token;
216 item->out_token = RAWOBJ_EMPTY;
218 new->major_status = item->major_status;
219 new->minor_status = item->minor_status;
222 #ifdef HAVE_SUNRPC_CACHE_V2
224 static void rsi_put(struct kref *ref)
226 struct rsi *rsi = container_of(ref, struct rsi, h.ref);
228 LASSERT(rsi->h.next == NULL);
233 static int rsi_match(struct cache_head *a, struct cache_head *b)
235 struct rsi *item = container_of(a, struct rsi, h);
236 struct rsi *tmp = container_of(b, struct rsi, h);
238 return __rsi_match(item, tmp);
241 static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
243 struct rsi *new = container_of(cnew, struct rsi, h);
244 struct rsi *item = container_of(citem, struct rsi, h);
246 __rsi_init(new, item);
249 static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
251 struct rsi *new = container_of(cnew, struct rsi, h);
252 struct rsi *item = container_of(citem, struct rsi, h);
254 __rsi_update(new, item);
257 static struct cache_head *rsi_alloc(void)
268 static int rsi_parse(struct cache_detail *cd, char *mesg, int mlen)
273 struct rsi rsii, *rsip = NULL;
275 int status = -EINVAL;
279 memset(&rsii, 0, sizeof(rsii));
282 len = qword_get(&mesg, buf, mlen);
285 if (rawobj_alloc(&rsii.in_handle, buf, len)) {
291 len = qword_get(&mesg, buf, mlen);
294 if (rawobj_alloc(&rsii.in_token, buf, len)) {
299 rsip = rsi_lookup(&rsii);
305 expiry = get_expiry(&mesg);
309 len = qword_get(&mesg, buf, mlen);
314 rsii.major_status = simple_strtol(buf, &ep, 10);
319 len = qword_get(&mesg, buf, mlen);
322 rsii.minor_status = simple_strtol(buf, &ep, 10);
327 len = qword_get(&mesg, buf, mlen);
330 if (rawobj_alloc(&rsii.out_handle, buf, len)) {
336 len = qword_get(&mesg, buf, mlen);
339 if (rawobj_alloc(&rsii.out_token, buf, len)) {
344 rsii.h.expiry_time = expiry;
345 rsip = rsi_update(&rsii, rsip);
350 cfs_waitq_broadcast(&rsip->waitq);
351 cache_put(&rsip->h, &rsi_cache);
357 CERROR("rsi parse error %d\n", status);
361 #else /* !HAVE_SUNRPC_CACHE_V2 */
363 static void rsi_put(struct cache_head *item, struct cache_detail *cd)
365 struct rsi *rsi = container_of(item, struct rsi, h);
367 LASSERT(cfs_atomic_read(&item->refcnt) > 0);
369 if (cache_put(item, cd)) {
370 LASSERT(item->next == NULL);
372 kfree(rsi); /* created by cache mgmt using kmalloc */
376 static inline int rsi_match(struct rsi *item, struct rsi *tmp)
378 return __rsi_match(item, tmp);
381 static inline void rsi_init(struct rsi *new, struct rsi *item)
383 __rsi_init(new, item);
386 static inline void rsi_update(struct rsi *new, struct rsi *item)
388 __rsi_update(new, item);
391 static int rsi_parse(struct cache_detail *cd, char *mesg, int mlen)
396 struct rsi rsii, *rsip = NULL;
398 int status = -EINVAL;
402 memset(&rsii, 0, sizeof(rsii));
405 len = qword_get(&mesg, buf, mlen);
408 if (rawobj_alloc(&rsii.in_handle, buf, len)) {
414 len = qword_get(&mesg, buf, mlen);
417 if (rawobj_alloc(&rsii.in_token, buf, len)) {
423 expiry = get_expiry(&mesg);
427 len = qword_get(&mesg, buf, mlen);
432 rsii.major_status = simple_strtol(buf, &ep, 10);
437 len = qword_get(&mesg, buf, mlen);
440 rsii.minor_status = simple_strtol(buf, &ep, 10);
445 len = qword_get(&mesg, buf, mlen);
448 if (rawobj_alloc(&rsii.out_handle, buf, len)) {
454 len = qword_get(&mesg, buf, mlen);
457 if (rawobj_alloc(&rsii.out_token, buf, len)) {
462 rsii.h.expiry_time = expiry;
463 rsip = rsi_lookup(&rsii, 1);
468 cfs_waitq_broadcast(&rsip->waitq);
469 rsi_put(&rsip->h, &rsi_cache);
473 CERROR("rsi parse error %d\n", status);
477 #endif /* HAVE_SUNRPC_CACHE_V2 */
479 static struct cache_detail rsi_cache = {
480 .hash_size = RSI_HASHMAX,
481 .hash_table = rsi_table,
482 .name = "auth.sptlrpc.init",
483 .cache_put = rsi_put,
484 #ifdef HAVE_CACHE_UPCALL
485 .cache_upcall = rsi_upcall,
487 .cache_request = rsi_request,
489 .cache_parse = rsi_parse,
490 #ifdef HAVE_SUNRPC_CACHE_V2
493 .update = update_rsi,
498 #ifdef HAVE_SUNRPC_CACHE_V2
500 static struct rsi *rsi_lookup(struct rsi *item)
502 struct cache_head *ch;
503 int hash = rsi_hash(item);
505 ch = sunrpc_cache_lookup(&rsi_cache, &item->h, hash);
507 return container_of(ch, struct rsi, h);
512 static struct rsi *rsi_update(struct rsi *new, struct rsi *old)
514 struct cache_head *ch;
515 int hash = rsi_hash(new);
517 ch = sunrpc_cache_update(&rsi_cache, &new->h, &old->h, hash);
519 return container_of(ch, struct rsi, h);
526 static DefineSimpleCacheLookup(rsi, 0)
530 /****************************************
532 ****************************************/
534 #define RSC_HASHBITS (10)
535 #define RSC_HASHMAX (1 << RSC_HASHBITS)
536 #define RSC_HASHMASK (RSC_HASHMAX - 1)
540 struct obd_device *target;
542 struct gss_svc_ctx ctx;
545 static struct cache_head *rsc_table[RSC_HASHMAX];
546 static struct cache_detail rsc_cache;
547 #ifdef HAVE_SUNRPC_CACHE_V2
548 static struct rsc *rsc_update(struct rsc *new, struct rsc *old);
549 static struct rsc *rsc_lookup(struct rsc *item);
551 static struct rsc *rsc_lookup(struct rsc *item, int set);
554 static void rsc_free(struct rsc *rsci)
556 rawobj_free(&rsci->handle);
557 rawobj_free(&rsci->ctx.gsc_rvs_hdl);
558 lgss_delete_sec_context(&rsci->ctx.gsc_mechctx);
561 static inline int rsc_hash(struct rsc *rsci)
563 return hash_mem((char *)rsci->handle.data,
564 rsci->handle.len, RSC_HASHBITS);
567 static inline int __rsc_match(struct rsc *new, struct rsc *tmp)
569 return rawobj_equal(&new->handle, &tmp->handle);
572 static inline void __rsc_init(struct rsc *new, struct rsc *tmp)
574 new->handle = tmp->handle;
575 tmp->handle = RAWOBJ_EMPTY;
578 memset(&new->ctx, 0, sizeof(new->ctx));
579 new->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
582 static inline void __rsc_update(struct rsc *new, struct rsc *tmp)
585 tmp->ctx.gsc_rvs_hdl = RAWOBJ_EMPTY;
586 tmp->ctx.gsc_mechctx = NULL;
588 memset(&new->ctx.gsc_seqdata, 0, sizeof(new->ctx.gsc_seqdata));
589 cfs_spin_lock_init(&new->ctx.gsc_seqdata.ssd_lock);
592 #ifdef HAVE_SUNRPC_CACHE_V2
594 static void rsc_put(struct kref *ref)
596 struct rsc *rsci = container_of(ref, struct rsc, h.ref);
598 LASSERT(rsci->h.next == NULL);
603 static int rsc_match(struct cache_head *a, struct cache_head *b)
605 struct rsc *new = container_of(a, struct rsc, h);
606 struct rsc *tmp = container_of(b, struct rsc, h);
608 return __rsc_match(new, tmp);
611 static void rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
613 struct rsc *new = container_of(cnew, struct rsc, h);
614 struct rsc *tmp = container_of(ctmp, struct rsc, h);
616 __rsc_init(new, tmp);
619 static void update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
621 struct rsc *new = container_of(cnew, struct rsc, h);
622 struct rsc *tmp = container_of(ctmp, struct rsc, h);
624 __rsc_update(new, tmp);
627 static struct cache_head * rsc_alloc(void)
638 static int rsc_parse(struct cache_detail *cd, char *mesg, int mlen)
641 int len, rv, tmp_int;
642 struct rsc rsci, *rscp = NULL;
644 int status = -EINVAL;
645 struct gss_api_mech *gm = NULL;
647 memset(&rsci, 0, sizeof(rsci));
650 len = qword_get(&mesg, buf, mlen);
651 if (len < 0) goto out;
653 if (rawobj_alloc(&rsci.handle, buf, len))
658 expiry = get_expiry(&mesg);
664 rv = get_int(&mesg, &tmp_int);
666 CERROR("fail to get remote flag\n");
669 rsci.ctx.gsc_remote = (tmp_int != 0);
672 rv = get_int(&mesg, &tmp_int);
674 CERROR("fail to get oss user flag\n");
677 rsci.ctx.gsc_usr_root = (tmp_int != 0);
680 rv = get_int(&mesg, &tmp_int);
682 CERROR("fail to get mds user flag\n");
685 rsci.ctx.gsc_usr_mds = (tmp_int != 0);
688 rv = get_int(&mesg, &tmp_int);
690 CERROR("fail to get oss user flag\n");
693 rsci.ctx.gsc_usr_oss = (tmp_int != 0);
696 rv = get_int(&mesg, (int *) &rsci.ctx.gsc_mapped_uid);
698 CERROR("fail to get mapped uid\n");
702 rscp = rsc_lookup(&rsci);
706 /* uid, or NEGATIVE */
707 rv = get_int(&mesg, (int *) &rsci.ctx.gsc_uid);
711 CERROR("NOENT? set rsc entry negative\n");
712 cfs_set_bit(CACHE_NEGATIVE, &rsci.h.flags);
715 unsigned long ctx_expiry;
718 if (get_int(&mesg, (int *) &rsci.ctx.gsc_gid))
722 len = qword_get(&mesg, buf, mlen);
725 gm = lgss_name_to_mech(buf);
726 status = -EOPNOTSUPP;
731 /* mech-specific data: */
732 len = qword_get(&mesg, buf, mlen);
737 tmp_buf.data = (unsigned char *)buf;
738 if (lgss_import_sec_context(&tmp_buf, gm,
739 &rsci.ctx.gsc_mechctx))
742 /* currently the expiry time passed down from user-space
743 * is invalid, here we retrive it from mech. */
744 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
745 CERROR("unable to get expire time, drop it\n");
748 expiry = (time_t) ctx_expiry;
751 rsci.h.expiry_time = expiry;
752 rscp = rsc_update(&rsci, rscp);
759 cache_put(&rscp->h, &rsc_cache);
764 CERROR("parse rsc error %d\n", status);
768 #else /* !HAVE_SUNRPC_CACHE_V2 */
770 static void rsc_put(struct cache_head *item, struct cache_detail *cd)
772 struct rsc *rsci = container_of(item, struct rsc, h);
774 LASSERT(cfs_atomic_read(&item->refcnt) > 0);
776 if (cache_put(item, cd)) {
777 LASSERT(item->next == NULL);
779 kfree(rsci); /* created by cache mgmt using kmalloc */
783 static inline int rsc_match(struct rsc *new, struct rsc *tmp)
785 return __rsc_match(new, tmp);
788 static inline void rsc_init(struct rsc *new, struct rsc *tmp)
790 __rsc_init(new, tmp);
793 static inline void rsc_update(struct rsc *new, struct rsc *tmp)
795 __rsc_update(new, tmp);
798 static int rsc_parse(struct cache_detail *cd, char *mesg, int mlen)
801 int len, rv, tmp_int;
802 struct rsc rsci, *rscp = NULL;
804 int status = -EINVAL;
806 memset(&rsci, 0, sizeof(rsci));
809 len = qword_get(&mesg, buf, mlen);
810 if (len < 0) goto out;
812 if (rawobj_alloc(&rsci.handle, buf, len))
817 expiry = get_expiry(&mesg);
823 rv = get_int(&mesg, &tmp_int);
825 CERROR("fail to get remote flag\n");
828 rsci.ctx.gsc_remote = (tmp_int != 0);
831 rv = get_int(&mesg, &tmp_int);
833 CERROR("fail to get oss user flag\n");
836 rsci.ctx.gsc_usr_root = (tmp_int != 0);
839 rv = get_int(&mesg, &tmp_int);
841 CERROR("fail to get mds user flag\n");
844 rsci.ctx.gsc_usr_mds = (tmp_int != 0);
847 rv = get_int(&mesg, &tmp_int);
849 CERROR("fail to get oss user flag\n");
852 rsci.ctx.gsc_usr_oss = (tmp_int != 0);
855 rv = get_int(&mesg, (int *) &rsci.ctx.gsc_mapped_uid);
857 CERROR("fail to get mapped uid\n");
861 /* uid, or NEGATIVE */
862 rv = get_int(&mesg, (int *) &rsci.ctx.gsc_uid);
866 CERROR("NOENT? set rsc entry negative\n");
867 cfs_set_bit(CACHE_NEGATIVE, &rsci.h.flags);
869 struct gss_api_mech *gm;
871 unsigned long ctx_expiry;
874 if (get_int(&mesg, (int *) &rsci.ctx.gsc_gid))
878 len = qword_get(&mesg, buf, mlen);
881 gm = lgss_name_to_mech(buf);
882 status = -EOPNOTSUPP;
887 /* mech-specific data: */
888 len = qword_get(&mesg, buf, mlen);
894 tmp_buf.data = (unsigned char *)buf;
895 if (lgss_import_sec_context(&tmp_buf, gm,
896 &rsci.ctx.gsc_mechctx)) {
901 /* currently the expiry time passed down from user-space
902 * is invalid, here we retrive it from mech. */
903 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
904 CERROR("unable to get expire time, drop it\n");
908 expiry = (time_t) ctx_expiry;
913 rsci.h.expiry_time = expiry;
914 rscp = rsc_lookup(&rsci, 1);
919 rsc_put(&rscp->h, &rsc_cache);
922 CERROR("parse rsc error %d\n", status);
926 #endif /* HAVE_SUNRPC_CACHE_V2 */
929 static struct cache_detail rsc_cache = {
930 .hash_size = RSC_HASHMAX,
931 .hash_table = rsc_table,
932 .name = "auth.sptlrpc.context",
933 .cache_put = rsc_put,
934 .cache_parse = rsc_parse,
935 #ifdef HAVE_SUNRPC_CACHE_V2
938 .update = update_rsc,
943 #ifdef HAVE_SUNRPC_CACHE_V2
945 static struct rsc *rsc_lookup(struct rsc *item)
947 struct cache_head *ch;
948 int hash = rsc_hash(item);
950 ch = sunrpc_cache_lookup(&rsc_cache, &item->h, hash);
952 return container_of(ch, struct rsc, h);
957 static struct rsc *rsc_update(struct rsc *new, struct rsc *old)
959 struct cache_head *ch;
960 int hash = rsc_hash(new);
962 ch = sunrpc_cache_update(&rsc_cache, &new->h, &old->h, hash);
964 return container_of(ch, struct rsc, h);
969 #define COMPAT_RSC_PUT(item, cd) cache_put((item), (cd))
973 static DefineSimpleCacheLookup(rsc, 0);
975 #define COMPAT_RSC_PUT(item, cd) rsc_put((item), (cd))
979 /****************************************
981 ****************************************/
983 typedef int rsc_entry_match(struct rsc *rscp, long data);
985 static void rsc_flush(rsc_entry_match *match, long data)
987 struct cache_head **ch;
992 cfs_write_lock(&rsc_cache.hash_lock);
993 for (n = 0; n < RSC_HASHMAX; n++) {
994 for (ch = &rsc_cache.hash_table[n]; *ch;) {
995 rscp = container_of(*ch, struct rsc, h);
997 if (!match(rscp, data)) {
1002 /* it seems simply set NEGATIVE doesn't work */
1004 rscp->h.next = NULL;
1005 cache_get(&rscp->h);
1006 cfs_set_bit(CACHE_NEGATIVE, &rscp->h.flags);
1007 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
1008 rsc_cache.entries--;
1011 cfs_write_unlock(&rsc_cache.hash_lock);
1015 static int match_uid(struct rsc *rscp, long uid)
1017 if ((int) uid == -1)
1019 return ((int) rscp->ctx.gsc_uid == (int) uid);
1022 static int match_target(struct rsc *rscp, long target)
1024 return (rscp->target == (struct obd_device *) target);
1027 static inline void rsc_flush_uid(int uid)
1030 CWARN("flush all gss contexts...\n");
1032 rsc_flush(match_uid, (long) uid);
1035 static inline void rsc_flush_target(struct obd_device *target)
1037 rsc_flush(match_target, (long) target);
1040 void gss_secsvc_flush(struct obd_device *target)
1042 rsc_flush_target(target);
1044 EXPORT_SYMBOL(gss_secsvc_flush);
1046 static struct rsc *gss_svc_searchbyctx(rawobj_t *handle)
1051 memset(&rsci, 0, sizeof(rsci));
1052 if (rawobj_dup(&rsci.handle, handle))
1055 #ifdef HAVE_SUNRPC_CACHE_V2
1056 found = rsc_lookup(&rsci);
1058 found = rsc_lookup(&rsci, 0);
1063 if (cache_check(&rsc_cache, &found->h, NULL))
1068 #ifdef HAVE_SUNRPC_CACHE_V2
1070 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
1071 struct gss_sec *gsec,
1072 struct gss_cli_ctx *gctx)
1074 struct rsc rsci, *rscp = NULL;
1075 unsigned long ctx_expiry;
1080 memset(&rsci, 0, sizeof(rsci));
1082 if (rawobj_alloc(&rsci.handle, (char *) &gsec->gs_rvs_hdl,
1083 sizeof(gsec->gs_rvs_hdl)))
1084 GOTO(out, rc = -ENOMEM);
1086 rscp = rsc_lookup(&rsci);
1088 GOTO(out, rc = -ENOMEM);
1090 major = lgss_copy_reverse_context(gctx->gc_mechctx,
1091 &rsci.ctx.gsc_mechctx);
1092 if (major != GSS_S_COMPLETE)
1093 GOTO(out, rc = -ENOMEM);
1095 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
1096 CERROR("unable to get expire time, drop it\n");
1097 GOTO(out, rc = -EINVAL);
1099 rsci.h.expiry_time = (time_t) ctx_expiry;
1101 if (strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0)
1102 rsci.ctx.gsc_usr_mds = 1;
1103 else if (strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0)
1104 rsci.ctx.gsc_usr_oss = 1;
1106 rsci.ctx.gsc_usr_root = 1;
1108 rscp = rsc_update(&rsci, rscp);
1110 GOTO(out, rc = -ENOMEM);
1112 rscp->target = imp->imp_obd;
1113 rawobj_dup(&gctx->gc_svc_handle, &rscp->handle);
1115 CWARN("create reverse svc ctx %p to %s: idx "LPX64"\n",
1116 &rscp->ctx, obd2cli_tgt(imp->imp_obd), gsec->gs_rvs_hdl);
1120 cache_put(&rscp->h, &rsc_cache);
1124 CERROR("create reverse svc ctx: idx "LPX64", rc %d\n",
1125 gsec->gs_rvs_hdl, rc);
1129 #else /* !HAVE_SUNRPC_CACHE_V2 */
1131 int gss_svc_upcall_install_rvs_ctx(struct obd_import *imp,
1132 struct gss_sec *gsec,
1133 struct gss_cli_ctx *gctx)
1135 struct rsc rsci, *rscp;
1136 unsigned long ctx_expiry;
1141 memset(&rsci, 0, sizeof(rsci));
1143 if (rawobj_alloc(&rsci.handle, (char *) &gsec->gs_rvs_hdl,
1144 sizeof(gsec->gs_rvs_hdl)))
1145 GOTO(out, rc = -ENOMEM);
1147 major = lgss_copy_reverse_context(gctx->gc_mechctx,
1148 &rsci.ctx.gsc_mechctx);
1149 if (major != GSS_S_COMPLETE)
1150 GOTO(out, rc = -ENOMEM);
1152 if (lgss_inquire_context(rsci.ctx.gsc_mechctx, &ctx_expiry)) {
1153 CERROR("unable to get expire time, drop it\n");
1154 GOTO(out, rc = -ENOMEM);
1156 rsci.h.expiry_time = (time_t) ctx_expiry;
1158 if (strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0)
1159 rsci.ctx.gsc_usr_mds = 1;
1160 else if (strcmp(imp->imp_obd->obd_type->typ_name, LUSTRE_OSC_NAME) == 0)
1161 rsci.ctx.gsc_usr_oss = 1;
1163 rsci.ctx.gsc_usr_root = 1;
1165 rscp = rsc_lookup(&rsci, 1);
1167 CERROR("rsc lookup failed\n");
1168 GOTO(out, rc = -ENOMEM);
1171 rscp->target = imp->imp_obd;
1172 rawobj_dup(&gctx->gc_svc_handle, &rscp->handle);
1174 CWARN("create reverse svc ctx %p to %s: idx "LPX64"\n",
1175 &rscp->ctx, obd2cli_tgt(imp->imp_obd), gsec->gs_rvs_hdl);
1176 rsc_put(&rscp->h, &rsc_cache);
1181 CERROR("create reverse svc ctx: idx "LPX64", rc %d\n",
1182 gsec->gs_rvs_hdl, rc);
1186 #endif /* HAVE_SUNRPC_CACHE_V2 */
1188 int gss_svc_upcall_expire_rvs_ctx(rawobj_t *handle)
1190 const cfs_time_t expire = 20;
1193 rscp = gss_svc_searchbyctx(handle);
1195 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) expire soon\n",
1198 rscp->h.expiry_time = cfs_time_current_sec() + expire;
1199 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
1204 int gss_svc_upcall_dup_handle(rawobj_t *handle, struct gss_svc_ctx *ctx)
1206 struct rsc *rscp = container_of(ctx, struct rsc, ctx);
1208 return rawobj_dup(handle, &rscp->handle);
1211 int gss_svc_upcall_update_sequence(rawobj_t *handle, __u32 seq)
1215 rscp = gss_svc_searchbyctx(handle);
1217 CDEBUG(D_SEC, "reverse svcctx %p (rsc %p) update seq to %u\n",
1218 &rscp->ctx, rscp, seq + 1);
1220 rscp->ctx.gsc_rvs_seq = seq + 1;
1221 COMPAT_RSC_PUT(&rscp->h, &rsc_cache);
1226 static struct cache_deferred_req* cache_upcall_defer(struct cache_req *req)
1230 static struct cache_req cache_upcall_chandle = { cache_upcall_defer };
1232 int gss_svc_upcall_handle_init(struct ptlrpc_request *req,
1233 struct gss_svc_reqctx *grctx,
1234 struct gss_wire_ctx *gw,
1235 struct obd_device *target,
1240 struct ptlrpc_reply_state *rs;
1241 struct rsc *rsci = NULL;
1242 struct rsi *rsip = NULL, rsikey;
1243 cfs_waitlink_t wait;
1244 int replen = sizeof(struct ptlrpc_body);
1245 struct gss_rep_header *rephdr;
1246 int first_check = 1;
1247 int rc = SECSVC_DROP;
1250 memset(&rsikey, 0, sizeof(rsikey));
1251 rsikey.lustre_svc = lustre_svc;
1252 rsikey.nid = (__u64) req->rq_peer.nid;
1254 /* duplicate context handle. for INIT it always 0 */
1255 if (rawobj_dup(&rsikey.in_handle, &gw->gw_handle)) {
1256 CERROR("fail to dup context handle\n");
1260 if (rawobj_dup(&rsikey.in_token, in_token)) {
1261 CERROR("can't duplicate token\n");
1262 rawobj_free(&rsikey.in_handle);
1266 #ifdef HAVE_SUNRPC_CACHE_V2
1267 rsip = rsi_lookup(&rsikey);
1269 rsip = rsi_lookup(&rsikey, 0);
1273 CERROR("error in rsi_lookup.\n");
1275 if (!gss_pack_err_notify(req, GSS_S_FAILURE, 0))
1276 rc = SECSVC_COMPLETE;
1281 cache_get(&rsip->h); /* take an extra ref */
1282 cfs_waitq_init(&rsip->waitq);
1283 cfs_waitlink_init(&wait);
1284 cfs_waitq_add(&rsip->waitq, &wait);
1287 /* Note each time cache_check() will drop a reference if return
1288 * non-zero. We hold an extra reference on initial rsip, but must
1289 * take care of following calls. */
1290 rc = cache_check(&rsi_cache, &rsip->h, &cache_upcall_chandle);
1298 read_lock(&rsi_cache.hash_lock);
1299 valid = cfs_test_bit(CACHE_VALID, &rsip->h.flags);
1301 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
1302 read_unlock(&rsi_cache.hash_lock);
1305 cfs_schedule_timeout(GSS_SVC_UPCALL_TIMEOUT *
1308 cache_get(&rsip->h);
1311 CWARN("waited %ds timeout, drop\n", GSS_SVC_UPCALL_TIMEOUT);
1315 CWARN("cache_check return ENOENT, drop\n");
1318 /* if not the first check, we have to release the extra
1319 * reference we just added on it. */
1321 cache_put(&rsip->h, &rsi_cache);
1322 CDEBUG(D_SEC, "cache_check is good\n");
1326 cfs_waitq_del(&rsip->waitq, &wait);
1327 cache_put(&rsip->h, &rsi_cache);
1330 GOTO(out, rc = SECSVC_DROP);
1333 rsci = gss_svc_searchbyctx(&rsip->out_handle);
1335 CERROR("authentication failed\n");
1337 if (!gss_pack_err_notify(req, GSS_S_FAILURE, 0))
1338 rc = SECSVC_COMPLETE;
1342 cache_get(&rsci->h);
1343 grctx->src_ctx = &rsci->ctx;
1346 if (rawobj_dup(&rsci->ctx.gsc_rvs_hdl, rvs_hdl)) {
1347 CERROR("failed duplicate reverse handle\n");
1351 rsci->target = target;
1353 CDEBUG(D_SEC, "server create rsc %p(%u->%s)\n",
1354 rsci, rsci->ctx.gsc_uid, libcfs_nid2str(req->rq_peer.nid));
1356 if (rsip->out_handle.len > PTLRPC_GSS_MAX_HANDLE_SIZE) {
1357 CERROR("handle size %u too large\n", rsip->out_handle.len);
1358 GOTO(out, rc = SECSVC_DROP);
1361 grctx->src_init = 1;
1362 grctx->src_reserve_len = cfs_size_round4(rsip->out_token.len);
1364 rc = lustre_pack_reply_v2(req, 1, &replen, NULL, 0);
1366 CERROR("failed to pack reply: %d\n", rc);
1367 GOTO(out, rc = SECSVC_DROP);
1370 rs = req->rq_reply_state;
1371 LASSERT(rs->rs_repbuf->lm_bufcount == 3);
1372 LASSERT(rs->rs_repbuf->lm_buflens[0] >=
1373 sizeof(*rephdr) + rsip->out_handle.len);
1374 LASSERT(rs->rs_repbuf->lm_buflens[2] >= rsip->out_token.len);
1376 rephdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
1377 rephdr->gh_version = PTLRPC_GSS_VERSION;
1378 rephdr->gh_flags = 0;
1379 rephdr->gh_proc = PTLRPC_GSS_PROC_ERR;
1380 rephdr->gh_major = rsip->major_status;
1381 rephdr->gh_minor = rsip->minor_status;
1382 rephdr->gh_seqwin = GSS_SEQ_WIN;
1383 rephdr->gh_handle.len = rsip->out_handle.len;
1384 memcpy(rephdr->gh_handle.data, rsip->out_handle.data,
1385 rsip->out_handle.len);
1387 memcpy(lustre_msg_buf(rs->rs_repbuf, 2, 0), rsip->out_token.data,
1388 rsip->out_token.len);
1390 rs->rs_repdata_len = lustre_shrink_msg(rs->rs_repbuf, 2,
1391 rsip->out_token.len, 0);
1396 /* it looks like here we should put rsip also, but this mess up
1397 * with NFS cache mgmt code... FIXME */
1400 rsi_put(&rsip->h, &rsi_cache);
1404 /* if anything went wrong, we don't keep the context too */
1405 if (rc != SECSVC_OK)
1406 cfs_set_bit(CACHE_NEGATIVE, &rsci->h.flags);
1408 CDEBUG(D_SEC, "create rsc with idx "LPX64"\n",
1409 gss_handle_to_u64(&rsci->handle));
1411 COMPAT_RSC_PUT(&rsci->h, &rsc_cache);
1416 struct gss_svc_ctx *gss_svc_upcall_get_ctx(struct ptlrpc_request *req,
1417 struct gss_wire_ctx *gw)
1421 rsc = gss_svc_searchbyctx(&gw->gw_handle);
1423 CWARN("Invalid gss ctx idx "LPX64" from %s\n",
1424 gss_handle_to_u64(&gw->gw_handle),
1425 libcfs_nid2str(req->rq_peer.nid));
1432 void gss_svc_upcall_put_ctx(struct gss_svc_ctx *ctx)
1434 struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1436 COMPAT_RSC_PUT(&rsc->h, &rsc_cache);
1439 void gss_svc_upcall_destroy_ctx(struct gss_svc_ctx *ctx)
1441 struct rsc *rsc = container_of(ctx, struct rsc, ctx);
1443 /* can't be found */
1444 cfs_set_bit(CACHE_NEGATIVE, &rsc->h.flags);
1445 /* to be removed at next scan */
1446 rsc->h.expiry_time = 1;
1449 int __init gss_init_svc_upcall(void)
1453 cfs_spin_lock_init(&__ctx_index_lock);
1455 * this helps reducing context index confliction. after server reboot,
1456 * conflicting request from clients might be filtered out by initial
1457 * sequence number checking, thus no chance to sent error notification
1460 cfs_get_random_bytes(&__ctx_index, sizeof(__ctx_index));
1463 cache_register(&rsi_cache);
1464 cache_register(&rsc_cache);
1466 /* FIXME this looks stupid. we intend to give lsvcgssd a chance to open
1467 * the init upcall channel, otherwise there's big chance that the first
1468 * upcall issued before the channel be opened thus nfsv4 cache code will
1469 * drop the request direclty, thus lead to unnecessary recovery time.
1470 * here we wait at miximum 1.5 seconds. */
1471 for (i = 0; i < 6; i++) {
1472 if (atomic_read(&rsi_cache.readers) > 0)
1474 cfs_set_current_state(TASK_UNINTERRUPTIBLE);
1475 LASSERT(CFS_HZ >= 4);
1476 cfs_schedule_timeout(CFS_HZ / 4);
1479 if (atomic_read(&rsi_cache.readers) == 0)
1480 CWARN("Init channel is not opened by lsvcgssd, following "
1481 "request might be dropped until lsvcgssd is active\n");
1486 void __exit gss_exit_svc_upcall(void)
1488 cache_purge(&rsi_cache);
1489 cache_unregister(&rsi_cache);
1491 cache_purge(&rsc_cache);
1492 cache_unregister(&rsc_cache);