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 * Author: Eric Mei <ericm@clusterfs.com>
12 * linux/net/sunrpc/auth_gss.c
14 * RPCSEC_GSS client authentication.
16 * Copyright (c) 2000 The Regents of the University of Michigan.
17 * All rights reserved.
19 * Dug Song <dugsong@monkey.org>
20 * Andy Adamson <andros@umich.edu>
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its
32 * contributors may be used to endorse or promote products derived
33 * from this software without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 # define EXPORT_SYMTAB
52 #define DEBUG_SUBSYSTEM S_SEC
54 #include <linux/init.h>
55 #include <linux/module.h>
56 #include <linux/slab.h>
57 #include <linux/dcache.h>
59 #include <linux/mutex.h>
60 #include <linux/crypto.h>
61 #include <asm/atomic.h>
62 struct rpc_clnt; /* for rpc_pipefs */
63 #include <linux/sunrpc/rpc_pipe_fs.h>
65 #include <liblustre.h>
69 #include <obd_class.h>
70 #include <obd_support.h>
71 #include <lustre/lustre_idl.h>
72 #include <lustre_sec.h>
73 #include <lustre_net.h>
74 #include <lustre_import.h>
77 #include "gss_internal.h"
80 static struct ptlrpc_sec_policy gss_policy_pipefs;
81 static struct ptlrpc_ctx_ops gss_pipefs_ctxops;
83 static int gss_cli_ctx_refresh_pf(struct ptlrpc_cli_ctx *ctx);
85 static int gss_sec_pipe_upcall_init(struct gss_sec *gsec)
90 static void gss_sec_pipe_upcall_fini(struct gss_sec *gsec)
94 /****************************************
95 * internel context helpers *
96 ****************************************/
99 struct ptlrpc_cli_ctx *ctx_create_pf(struct ptlrpc_sec *sec,
100 struct vfs_cred *vcred)
102 struct gss_cli_ctx *gctx;
109 rc = gss_cli_ctx_init_common(sec, &gctx->gc_base,
110 &gss_pipefs_ctxops, vcred);
116 return &gctx->gc_base;
120 void ctx_destroy_pf(struct ptlrpc_sec *sec, struct ptlrpc_cli_ctx *ctx)
122 struct gss_cli_ctx *gctx = ctx2gctx(ctx);
124 if (gss_cli_ctx_fini_common(sec, ctx))
129 cfs_atomic_dec(&sec->ps_nctx);
130 sptlrpc_sec_put(sec);
134 void ctx_enhash_pf(struct ptlrpc_cli_ctx *ctx, cfs_hlist_head_t *hash)
136 cfs_set_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
137 cfs_atomic_inc(&ctx->cc_refcount);
138 cfs_hlist_add_head(&ctx->cc_cache, hash);
142 * caller must hold spinlock
145 void ctx_unhash_pf(struct ptlrpc_cli_ctx *ctx, cfs_hlist_head_t *freelist)
147 LASSERT_SPIN_LOCKED(&ctx->cc_sec->ps_lock);
148 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
149 LASSERT(cfs_test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
150 LASSERT(!cfs_hlist_unhashed(&ctx->cc_cache));
152 cfs_clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
154 if (cfs_atomic_dec_and_test(&ctx->cc_refcount)) {
155 __cfs_hlist_del(&ctx->cc_cache);
156 cfs_hlist_add_head(&ctx->cc_cache, freelist);
158 cfs_hlist_del_init(&ctx->cc_cache);
163 * return 1 if the context is dead.
166 int ctx_check_death_pf(struct ptlrpc_cli_ctx *ctx,
167 cfs_hlist_head_t *freelist)
169 if (cli_ctx_check_death(ctx)) {
171 ctx_unhash_pf(ctx, freelist);
179 int ctx_check_death_locked_pf(struct ptlrpc_cli_ctx *ctx,
180 cfs_hlist_head_t *freelist)
182 LASSERT(ctx->cc_sec);
183 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
184 LASSERT(cfs_test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
186 return ctx_check_death_pf(ctx, freelist);
190 int ctx_match_pf(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred)
192 /* a little bit optimization for null policy */
193 if (!ctx->cc_ops->match)
196 return ctx->cc_ops->match(ctx, vcred);
200 void ctx_list_destroy_pf(cfs_hlist_head_t *head)
202 struct ptlrpc_cli_ctx *ctx;
204 while (!cfs_hlist_empty(head)) {
205 ctx = cfs_hlist_entry(head->first, struct ptlrpc_cli_ctx,
208 LASSERT(cfs_atomic_read(&ctx->cc_refcount) == 0);
209 LASSERT(cfs_test_bit(PTLRPC_CTX_CACHED_BIT,
210 &ctx->cc_flags) == 0);
212 cfs_hlist_del_init(&ctx->cc_cache);
213 ctx_destroy_pf(ctx->cc_sec, ctx);
217 /****************************************
219 ****************************************/
222 int gss_cli_ctx_validate_pf(struct ptlrpc_cli_ctx *ctx)
224 if (ctx_check_death_pf(ctx, NULL))
226 if (cli_ctx_is_ready(ctx))
232 void gss_cli_ctx_die_pf(struct ptlrpc_cli_ctx *ctx, int grace)
234 LASSERT(ctx->cc_sec);
235 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
239 cfs_spin_lock(&ctx->cc_sec->ps_lock);
241 if (cfs_test_and_clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags)) {
242 LASSERT(!cfs_hlist_unhashed(&ctx->cc_cache));
243 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 1);
245 cfs_hlist_del_init(&ctx->cc_cache);
246 if (cfs_atomic_dec_and_test(&ctx->cc_refcount))
250 cfs_spin_unlock(&ctx->cc_sec->ps_lock);
253 /****************************************
254 * reverse context installation *
255 ****************************************/
258 unsigned int ctx_hash_index(int hashsize, __u64 key)
260 return (unsigned int) (key & ((__u64) hashsize - 1));
264 void gss_sec_ctx_replace_pf(struct gss_sec *gsec,
265 struct ptlrpc_cli_ctx *new)
267 struct gss_sec_pipefs *gsec_pf;
268 struct ptlrpc_cli_ctx *ctx;
269 cfs_hlist_node_t *pos, *next;
270 CFS_HLIST_HEAD(freelist);
274 gsec_pf = container_of(gsec, struct gss_sec_pipefs, gsp_base);
276 hash = ctx_hash_index(gsec_pf->gsp_chash_size,
277 (__u64) new->cc_vcred.vc_uid);
278 LASSERT(hash < gsec_pf->gsp_chash_size);
280 cfs_spin_lock(&gsec->gs_base.ps_lock);
282 cfs_hlist_for_each_entry_safe(ctx, pos, next,
283 &gsec_pf->gsp_chash[hash], cc_cache) {
284 if (!ctx_match_pf(ctx, &new->cc_vcred))
288 ctx_unhash_pf(ctx, &freelist);
292 ctx_enhash_pf(new, &gsec_pf->gsp_chash[hash]);
294 cfs_spin_unlock(&gsec->gs_base.ps_lock);
296 ctx_list_destroy_pf(&freelist);
301 int gss_install_rvs_cli_ctx_pf(struct gss_sec *gsec,
302 struct ptlrpc_svc_ctx *svc_ctx)
304 struct vfs_cred vcred;
305 struct ptlrpc_cli_ctx *cli_ctx;
312 cli_ctx = ctx_create_pf(&gsec->gs_base, &vcred);
316 rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
318 ctx_destroy_pf(cli_ctx->cc_sec, cli_ctx);
322 gss_sec_ctx_replace_pf(gsec, cli_ctx);
327 void gss_ctx_cache_gc_pf(struct gss_sec_pipefs *gsec_pf,
328 cfs_hlist_head_t *freelist)
330 struct ptlrpc_sec *sec;
331 struct ptlrpc_cli_ctx *ctx;
332 cfs_hlist_node_t *pos, *next;
336 sec = &gsec_pf->gsp_base.gs_base;
338 CDEBUG(D_SEC, "do gc on sec %s@%p\n", sec->ps_policy->sp_name, sec);
340 for (i = 0; i < gsec_pf->gsp_chash_size; i++) {
341 cfs_hlist_for_each_entry_safe(ctx, pos, next,
342 &gsec_pf->gsp_chash[i], cc_cache)
343 ctx_check_death_locked_pf(ctx, freelist);
346 sec->ps_gc_next = cfs_time_current_sec() + sec->ps_gc_interval;
351 struct ptlrpc_sec* gss_sec_create_pf(struct obd_import *imp,
352 struct ptlrpc_svc_ctx *ctx,
353 struct sptlrpc_flavor *sf)
355 struct gss_sec_pipefs *gsec_pf;
356 int alloc_size, hash_size, i;
359 #define GSS_SEC_PIPEFS_CTX_HASH_SIZE (32)
362 sf->sf_flags & (PTLRPC_SEC_FL_ROOTONLY | PTLRPC_SEC_FL_REVERSE))
365 hash_size = GSS_SEC_PIPEFS_CTX_HASH_SIZE;
367 alloc_size = sizeof(*gsec_pf) +
368 sizeof(cfs_hlist_head_t) * hash_size;
370 OBD_ALLOC(gsec_pf, alloc_size);
374 gsec_pf->gsp_chash_size = hash_size;
375 for (i = 0; i < hash_size; i++)
376 CFS_INIT_HLIST_HEAD(&gsec_pf->gsp_chash[i]);
378 if (gss_sec_create_common(&gsec_pf->gsp_base, &gss_policy_pipefs,
383 if (gss_sec_pipe_upcall_init(&gsec_pf->gsp_base))
386 if (gss_install_rvs_cli_ctx_pf(&gsec_pf->gsp_base, ctx))
390 RETURN(&gsec_pf->gsp_base.gs_base);
393 gss_sec_destroy_common(&gsec_pf->gsp_base);
395 OBD_FREE(gsec_pf, alloc_size);
400 void gss_sec_destroy_pf(struct ptlrpc_sec *sec)
402 struct gss_sec_pipefs *gsec_pf;
403 struct gss_sec *gsec;
405 CWARN("destroy %s@%p\n", sec->ps_policy->sp_name, sec);
407 gsec = container_of(sec, struct gss_sec, gs_base);
408 gsec_pf = container_of(gsec, struct gss_sec_pipefs, gsp_base);
410 LASSERT(gsec_pf->gsp_chash);
411 LASSERT(gsec_pf->gsp_chash_size);
413 gss_sec_pipe_upcall_fini(gsec);
415 gss_sec_destroy_common(gsec);
417 OBD_FREE(gsec, sizeof(*gsec_pf) +
418 sizeof(cfs_hlist_head_t) * gsec_pf->gsp_chash_size);
422 struct ptlrpc_cli_ctx * gss_sec_lookup_ctx_pf(struct ptlrpc_sec *sec,
423 struct vfs_cred *vcred,
424 int create, int remove_dead)
426 struct gss_sec *gsec;
427 struct gss_sec_pipefs *gsec_pf;
428 struct ptlrpc_cli_ctx *ctx = NULL, *new = NULL;
429 cfs_hlist_head_t *hash_head;
430 cfs_hlist_node_t *pos, *next;
431 CFS_HLIST_HEAD(freelist);
432 unsigned int hash, gc = 0, found = 0;
437 gsec = container_of(sec, struct gss_sec, gs_base);
438 gsec_pf = container_of(gsec, struct gss_sec_pipefs, gsp_base);
440 hash = ctx_hash_index(gsec_pf->gsp_chash_size,
441 (__u64) vcred->vc_uid);
442 hash_head = &gsec_pf->gsp_chash[hash];
443 LASSERT(hash < gsec_pf->gsp_chash_size);
446 cfs_spin_lock(&sec->ps_lock);
448 /* gc_next == 0 means never do gc */
449 if (remove_dead && sec->ps_gc_next &&
450 cfs_time_after(cfs_time_current_sec(), sec->ps_gc_next)) {
451 gss_ctx_cache_gc_pf(gsec_pf, &freelist);
455 cfs_hlist_for_each_entry_safe(ctx, pos, next, hash_head, cc_cache) {
457 ctx_check_death_locked_pf(ctx,
458 remove_dead ? &freelist : NULL))
461 if (ctx_match_pf(ctx, vcred)) {
468 if (new && new != ctx) {
469 /* lost the race, just free it */
470 cfs_hlist_add_head(&new->cc_cache, &freelist);
474 /* hot node, move to head */
475 if (hash_head->first != &ctx->cc_cache) {
476 __cfs_hlist_del(&ctx->cc_cache);
477 cfs_hlist_add_head(&ctx->cc_cache, hash_head);
480 /* don't allocate for reverse sec */
481 if (sec_is_reverse(sec)) {
482 cfs_spin_unlock(&sec->ps_lock);
487 ctx_enhash_pf(new, hash_head);
490 cfs_spin_unlock(&sec->ps_lock);
491 new = ctx_create_pf(sec, vcred);
493 cfs_clear_bit(PTLRPC_CTX_NEW_BIT,
503 cfs_atomic_inc(&ctx->cc_refcount);
505 cfs_spin_unlock(&sec->ps_lock);
507 /* the allocator of the context must give the first push to refresh */
510 gss_cli_ctx_refresh_pf(new);
513 ctx_list_destroy_pf(&freelist);
518 void gss_sec_release_ctx_pf(struct ptlrpc_sec *sec,
519 struct ptlrpc_cli_ctx *ctx,
522 LASSERT(cfs_test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
523 LASSERT(cfs_hlist_unhashed(&ctx->cc_cache));
525 /* if required async, we must clear the UPTODATE bit to prevent extra
526 * rpcs during destroy procedure. */
528 cfs_clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
530 /* destroy this context */
531 ctx_destroy_pf(sec, ctx);
535 * @uid: which user. "-1" means flush all.
536 * @grace: mark context DEAD, allow graceful destroy like notify
538 * @force: also flush busy entries.
540 * return the number of busy context encountered.
542 * In any cases, never touch "eternal" contexts.
545 int gss_sec_flush_ctx_cache_pf(struct ptlrpc_sec *sec,
547 int grace, int force)
549 struct gss_sec *gsec;
550 struct gss_sec_pipefs *gsec_pf;
551 struct ptlrpc_cli_ctx *ctx;
552 cfs_hlist_node_t *pos, *next;
553 CFS_HLIST_HEAD(freelist);
557 might_sleep_if(grace);
559 gsec = container_of(sec, struct gss_sec, gs_base);
560 gsec_pf = container_of(gsec, struct gss_sec_pipefs, gsp_base);
562 cfs_spin_lock(&sec->ps_lock);
563 for (i = 0; i < gsec_pf->gsp_chash_size; i++) {
564 cfs_hlist_for_each_entry_safe(ctx, pos, next,
565 &gsec_pf->gsp_chash[i],
567 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
569 if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
572 if (cfs_atomic_read(&ctx->cc_refcount) > 1) {
577 CWARN("flush busy(%d) ctx %p(%u->%s) by force, "
579 cfs_atomic_read(&ctx->cc_refcount),
580 ctx, ctx->cc_vcred.vc_uid,
581 sec2target_str(ctx->cc_sec), grace);
583 ctx_unhash_pf(ctx, &freelist);
585 cfs_set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
587 cfs_clear_bit(PTLRPC_CTX_UPTODATE_BIT,
591 cfs_spin_unlock(&sec->ps_lock);
593 ctx_list_destroy_pf(&freelist);
597 /****************************************
599 ****************************************/
602 int gss_svc_accept_pf(struct ptlrpc_request *req)
604 return gss_svc_accept(&gss_policy_pipefs, req);
608 int gss_svc_install_rctx_pf(struct obd_import *imp,
609 struct ptlrpc_svc_ctx *ctx)
611 struct ptlrpc_sec *sec;
614 sec = sptlrpc_import_sec_ref(imp);
616 rc = gss_install_rvs_cli_ctx_pf(sec2gsec(sec), ctx);
618 sptlrpc_sec_put(sec);
622 /****************************************
623 * rpc_pipefs definitions *
624 ****************************************/
626 #define LUSTRE_PIPE_ROOT "/lustre"
627 #define LUSTRE_PIPE_KRB5 LUSTRE_PIPE_ROOT"/krb5"
629 struct gss_upcall_msg_data {
633 __u32 gum_svc; /* MDS/OSS... */
634 __u64 gum_nid; /* peer NID */
635 __u8 gum_obd[64]; /* client obd name */
638 struct gss_upcall_msg {
639 struct rpc_pipe_msg gum_base;
640 cfs_atomic_t gum_refcount;
643 struct gss_sec *gum_gsec;
644 struct gss_cli_ctx *gum_gctx;
645 struct gss_upcall_msg_data gum_data;
648 static cfs_atomic_t upcall_seq = CFS_ATOMIC_INIT(0);
651 __u32 upcall_get_sequence(void)
653 return (__u32) cfs_atomic_inc_return(&upcall_seq);
662 __u32 mech_name2idx(const char *name)
664 LASSERT(!strcmp(name, "krb5"));
668 /* pipefs dentries for each mechanisms */
669 static struct dentry *de_pipes[MECH_MAX] = { NULL, };
670 /* all upcall messgaes linked here */
671 static cfs_list_t upcall_lists[MECH_MAX];
672 /* and protected by this */
673 static cfs_spinlock_t upcall_locks[MECH_MAX];
676 void upcall_list_lock(int idx)
678 cfs_spin_lock(&upcall_locks[idx]);
682 void upcall_list_unlock(int idx)
684 cfs_spin_unlock(&upcall_locks[idx]);
688 void upcall_msg_enlist(struct gss_upcall_msg *msg)
690 __u32 idx = msg->gum_mechidx;
692 upcall_list_lock(idx);
693 cfs_list_add(&msg->gum_list, &upcall_lists[idx]);
694 upcall_list_unlock(idx);
698 void upcall_msg_delist(struct gss_upcall_msg *msg)
700 __u32 idx = msg->gum_mechidx;
702 upcall_list_lock(idx);
703 cfs_list_del_init(&msg->gum_list);
704 upcall_list_unlock(idx);
707 /****************************************
708 * rpc_pipefs upcall helpers *
709 ****************************************/
712 void gss_release_msg(struct gss_upcall_msg *gmsg)
715 LASSERT(cfs_atomic_read(&gmsg->gum_refcount) > 0);
717 if (!cfs_atomic_dec_and_test(&gmsg->gum_refcount)) {
722 if (gmsg->gum_gctx) {
723 sptlrpc_cli_ctx_wakeup(&gmsg->gum_gctx->gc_base);
724 sptlrpc_cli_ctx_put(&gmsg->gum_gctx->gc_base, 1);
725 gmsg->gum_gctx = NULL;
728 LASSERT(cfs_list_empty(&gmsg->gum_list));
729 LASSERT(cfs_list_empty(&gmsg->gum_base.list));
735 void gss_unhash_msg_nolock(struct gss_upcall_msg *gmsg)
737 __u32 idx = gmsg->gum_mechidx;
739 LASSERT(idx < MECH_MAX);
740 LASSERT_SPIN_LOCKED(&upcall_locks[idx]);
742 if (cfs_list_empty(&gmsg->gum_list))
745 cfs_list_del_init(&gmsg->gum_list);
746 LASSERT(cfs_atomic_read(&gmsg->gum_refcount) > 1);
747 cfs_atomic_dec(&gmsg->gum_refcount);
751 void gss_unhash_msg(struct gss_upcall_msg *gmsg)
753 __u32 idx = gmsg->gum_mechidx;
755 LASSERT(idx < MECH_MAX);
756 upcall_list_lock(idx);
757 gss_unhash_msg_nolock(gmsg);
758 upcall_list_unlock(idx);
762 void gss_msg_fail_ctx(struct gss_upcall_msg *gmsg)
764 if (gmsg->gum_gctx) {
765 struct ptlrpc_cli_ctx *ctx = &gmsg->gum_gctx->gc_base;
767 LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
768 sptlrpc_cli_ctx_expire(ctx);
769 cfs_set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
774 struct gss_upcall_msg * gss_find_upcall(__u32 mechidx, __u32 seq)
776 struct gss_upcall_msg *gmsg;
778 upcall_list_lock(mechidx);
779 cfs_list_for_each_entry(gmsg, &upcall_lists[mechidx], gum_list) {
780 if (gmsg->gum_data.gum_seq != seq)
783 LASSERT(cfs_atomic_read(&gmsg->gum_refcount) > 0);
784 LASSERT(gmsg->gum_mechidx == mechidx);
786 cfs_atomic_inc(&gmsg->gum_refcount);
787 upcall_list_unlock(mechidx);
790 upcall_list_unlock(mechidx);
795 int simple_get_bytes(char **buf, __u32 *buflen, void *res, __u32 reslen)
797 if (*buflen < reslen) {
798 CERROR("buflen %u < %u\n", *buflen, reslen);
802 memcpy(res, *buf, reslen);
808 /****************************************
810 ****************************************/
813 ssize_t gss_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
814 char *dst, size_t buflen)
816 char *data = (char *)msg->data + msg->copied;
817 ssize_t mlen = msg->len;
823 left = cfs_copy_to_user(dst, data, mlen);
835 ssize_t gss_pipe_downcall(struct file *filp, const char *src, size_t mlen)
837 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
838 struct gss_upcall_msg *gss_msg;
839 struct ptlrpc_cli_ctx *ctx;
840 struct gss_cli_ctx *gctx = NULL;
844 __u32 mechidx, seq, gss_err;
847 mechidx = (__u32) (long) rpci->private;
848 LASSERT(mechidx < MECH_MAX);
850 OBD_ALLOC(buf, mlen);
854 if (cfs_copy_from_user(buf, src, mlen)) {
855 CERROR("failed copy user space data\n");
856 GOTO(out_free, rc = -EFAULT);
861 /* data passed down format:
865 * - wire_ctx (rawobj)
866 * - mech_ctx (rawobj)
868 if (simple_get_bytes(&data, &datalen, &seq, sizeof(seq))) {
869 CERROR("fail to get seq\n");
870 GOTO(out_free, rc = -EFAULT);
873 gss_msg = gss_find_upcall(mechidx, seq);
875 CERROR("upcall %u has aborted earlier\n", seq);
876 GOTO(out_free, rc = -EINVAL);
879 gss_unhash_msg(gss_msg);
880 gctx = gss_msg->gum_gctx;
882 LASSERT(cfs_atomic_read(&gctx->gc_base.cc_refcount) > 0);
884 /* timeout is not in use for now */
885 if (simple_get_bytes(&data, &datalen, &timeout, sizeof(timeout)))
886 GOTO(out_msg, rc = -EFAULT);
888 /* lgssd signal an error by gc_win == 0 */
889 if (simple_get_bytes(&data, &datalen, &gctx->gc_win,
890 sizeof(gctx->gc_win)))
891 GOTO(out_msg, rc = -EFAULT);
893 if (gctx->gc_win == 0) {
898 if (simple_get_bytes(&data, &datalen, &rc, sizeof(rc)))
899 GOTO(out_msg, rc = -EFAULT);
900 if (simple_get_bytes(&data, &datalen, &gss_err,sizeof(gss_err)))
901 GOTO(out_msg, rc = -EFAULT);
903 if (rc == 0 && gss_err == GSS_S_COMPLETE) {
904 CWARN("both rpc & gss error code not set\n");
911 if (rawobj_extract_local(&tmpobj, (__u32 **) &data, &datalen))
912 GOTO(out_msg, rc = -EFAULT);
913 if (rawobj_dup(&gctx->gc_handle, &tmpobj))
914 GOTO(out_msg, rc = -ENOMEM);
917 if (rawobj_extract_local(&tmpobj, (__u32 **) &data, &datalen))
918 GOTO(out_msg, rc = -EFAULT);
919 gss_err = lgss_import_sec_context(&tmpobj,
920 gss_msg->gum_gsec->gs_mech,
925 if (likely(rc == 0 && gss_err == GSS_S_COMPLETE)) {
926 gss_cli_ctx_uptodate(gctx);
928 ctx = &gctx->gc_base;
929 sptlrpc_cli_ctx_expire(ctx);
930 if (rc != -ERESTART || gss_err != GSS_S_COMPLETE)
931 cfs_set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
933 CERROR("refresh ctx %p(uid %d) failed: %d/0x%08x: %s\n",
934 ctx, ctx->cc_vcred.vc_uid, rc, gss_err,
935 cfs_test_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags) ?
936 "fatal error" : "non-fatal");
942 gss_release_msg(gss_msg);
947 * hack pipefs: always return asked length unless all following
948 * downcalls might be messed up. */
954 void gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
956 struct gss_upcall_msg *gmsg;
957 struct gss_upcall_msg_data *gumd;
958 static cfs_time_t ratelimit = 0;
961 LASSERT(cfs_list_empty(&msg->list));
963 /* normally errno is >= 0 */
964 if (msg->errno >= 0) {
969 gmsg = container_of(msg, struct gss_upcall_msg, gum_base);
970 gumd = &gmsg->gum_data;
971 LASSERT(cfs_atomic_read(&gmsg->gum_refcount) > 0);
973 CERROR("failed msg %p (seq %u, uid %u, svc %u, nid "LPX64", obd %.*s): "
974 "errno %d\n", msg, gumd->gum_seq, gumd->gum_uid, gumd->gum_svc,
975 gumd->gum_nid, (int) sizeof(gumd->gum_obd),
976 gumd->gum_obd, msg->errno);
978 cfs_atomic_inc(&gmsg->gum_refcount);
979 gss_unhash_msg(gmsg);
980 if (msg->errno == -ETIMEDOUT || msg->errno == -EPIPE) {
981 cfs_time_t now = cfs_time_current_sec();
983 if (cfs_time_after(now, ratelimit)) {
984 CWARN("upcall timed out, is lgssd running?\n");
985 ratelimit = now + 15;
988 gss_msg_fail_ctx(gmsg);
989 gss_release_msg(gmsg);
994 void gss_pipe_release(struct inode *inode)
996 struct rpc_inode *rpci = RPC_I(inode);
1000 idx = (__u32) (long) rpci->private;
1001 LASSERT(idx < MECH_MAX);
1003 upcall_list_lock(idx);
1004 while (!cfs_list_empty(&upcall_lists[idx])) {
1005 struct gss_upcall_msg *gmsg;
1006 struct gss_upcall_msg_data *gumd;
1008 gmsg = cfs_list_entry(upcall_lists[idx].next,
1009 struct gss_upcall_msg, gum_list);
1010 gumd = &gmsg->gum_data;
1011 LASSERT(cfs_list_empty(&gmsg->gum_base.list));
1013 CERROR("failing remaining msg %p:seq %u, uid %u, svc %u, "
1014 "nid "LPX64", obd %.*s\n", gmsg,
1015 gumd->gum_seq, gumd->gum_uid, gumd->gum_svc,
1016 gumd->gum_nid, (int) sizeof(gumd->gum_obd),
1019 gmsg->gum_base.errno = -EPIPE;
1020 cfs_atomic_inc(&gmsg->gum_refcount);
1021 gss_unhash_msg_nolock(gmsg);
1023 gss_msg_fail_ctx(gmsg);
1025 upcall_list_unlock(idx);
1026 gss_release_msg(gmsg);
1027 upcall_list_lock(idx);
1029 upcall_list_unlock(idx);
1033 static struct rpc_pipe_ops gss_upcall_ops = {
1034 .upcall = gss_pipe_upcall,
1035 .downcall = gss_pipe_downcall,
1036 .destroy_msg = gss_pipe_destroy_msg,
1037 .release_pipe = gss_pipe_release,
1040 /****************************************
1041 * upcall helper functions *
1042 ****************************************/
1045 int gss_ctx_refresh_pf(struct ptlrpc_cli_ctx *ctx)
1047 struct obd_import *imp;
1048 struct gss_sec *gsec;
1049 struct gss_upcall_msg *gmsg;
1055 LASSERT(ctx->cc_sec);
1056 LASSERT(ctx->cc_sec->ps_import);
1057 LASSERT(ctx->cc_sec->ps_import->imp_obd);
1059 imp = ctx->cc_sec->ps_import;
1060 if (!imp->imp_connection) {
1061 CERROR("import has no connection set\n");
1065 gsec = container_of(ctx->cc_sec, struct gss_sec, gs_base);
1067 OBD_ALLOC_PTR(gmsg);
1071 /* initialize pipefs base msg */
1072 CFS_INIT_LIST_HEAD(&gmsg->gum_base.list);
1073 gmsg->gum_base.data = &gmsg->gum_data;
1074 gmsg->gum_base.len = sizeof(gmsg->gum_data);
1075 gmsg->gum_base.copied = 0;
1076 gmsg->gum_base.errno = 0;
1078 /* init upcall msg */
1079 cfs_atomic_set(&gmsg->gum_refcount, 1);
1080 gmsg->gum_mechidx = mech_name2idx(gsec->gs_mech->gm_name);
1081 gmsg->gum_gsec = gsec;
1082 gmsg->gum_gctx = container_of(sptlrpc_cli_ctx_get(ctx),
1083 struct gss_cli_ctx, gc_base);
1084 gmsg->gum_data.gum_seq = upcall_get_sequence();
1085 gmsg->gum_data.gum_uid = ctx->cc_vcred.vc_uid;
1086 gmsg->gum_data.gum_gid = 0; /* not used for now */
1087 gmsg->gum_data.gum_svc = import_to_gss_svc(imp);
1088 gmsg->gum_data.gum_nid = imp->imp_connection->c_peer.nid;
1089 strncpy(gmsg->gum_data.gum_obd, imp->imp_obd->obd_name,
1090 sizeof(gmsg->gum_data.gum_obd));
1092 /* This only could happen when sysadmin set it dead/expired
1093 * using lctl by force. */
1094 if (ctx->cc_flags & PTLRPC_CTX_STATUS_MASK) {
1095 CWARN("ctx %p(%u->%s) was set flags %lx unexpectedly\n",
1096 ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
1099 LASSERT(!(ctx->cc_flags & PTLRPC_CTX_UPTODATE));
1100 ctx->cc_flags |= PTLRPC_CTX_DEAD | PTLRPC_CTX_ERROR;
1106 upcall_msg_enlist(gmsg);
1108 rc = rpc_queue_upcall(de_pipes[gmsg->gum_mechidx]->d_inode,
1111 CERROR("rpc_queue_upcall failed: %d\n", rc);
1113 upcall_msg_delist(gmsg);
1124 int gss_cli_ctx_refresh_pf(struct ptlrpc_cli_ctx *ctx)
1126 /* if we are refreshing for root, also update the reverse
1127 * handle index, do not confuse reverse contexts. */
1128 if (ctx->cc_vcred.vc_uid == 0) {
1129 struct gss_sec *gsec;
1131 gsec = container_of(ctx->cc_sec, struct gss_sec, gs_base);
1132 gsec->gs_rvs_hdl = gss_get_next_ctx_index();
1135 return gss_ctx_refresh_pf(ctx);
1138 /****************************************
1139 * lustre gss pipefs policy *
1140 ****************************************/
1142 static struct ptlrpc_ctx_ops gss_pipefs_ctxops = {
1143 .match = gss_cli_ctx_match,
1144 .refresh = gss_cli_ctx_refresh_pf,
1145 .validate = gss_cli_ctx_validate_pf,
1146 .die = gss_cli_ctx_die_pf,
1147 .sign = gss_cli_ctx_sign,
1148 .verify = gss_cli_ctx_verify,
1149 .seal = gss_cli_ctx_seal,
1150 .unseal = gss_cli_ctx_unseal,
1151 .wrap_bulk = gss_cli_ctx_wrap_bulk,
1152 .unwrap_bulk = gss_cli_ctx_unwrap_bulk,
1155 static struct ptlrpc_sec_cops gss_sec_pipefs_cops = {
1156 .create_sec = gss_sec_create_pf,
1157 .destroy_sec = gss_sec_destroy_pf,
1158 .kill_sec = gss_sec_kill,
1159 .lookup_ctx = gss_sec_lookup_ctx_pf,
1160 .release_ctx = gss_sec_release_ctx_pf,
1161 .flush_ctx_cache = gss_sec_flush_ctx_cache_pf,
1162 .install_rctx = gss_sec_install_rctx,
1163 .alloc_reqbuf = gss_alloc_reqbuf,
1164 .free_reqbuf = gss_free_reqbuf,
1165 .alloc_repbuf = gss_alloc_repbuf,
1166 .free_repbuf = gss_free_repbuf,
1167 .enlarge_reqbuf = gss_enlarge_reqbuf,
1170 static struct ptlrpc_sec_sops gss_sec_pipefs_sops = {
1171 .accept = gss_svc_accept_pf,
1172 .invalidate_ctx = gss_svc_invalidate_ctx,
1173 .alloc_rs = gss_svc_alloc_rs,
1174 .authorize = gss_svc_authorize,
1175 .free_rs = gss_svc_free_rs,
1176 .free_ctx = gss_svc_free_ctx,
1177 .unwrap_bulk = gss_svc_unwrap_bulk,
1178 .wrap_bulk = gss_svc_wrap_bulk,
1179 .install_rctx = gss_svc_install_rctx_pf,
1182 static struct ptlrpc_sec_policy gss_policy_pipefs = {
1183 .sp_owner = THIS_MODULE,
1184 .sp_name = "gss.pipefs",
1185 .sp_policy = SPTLRPC_POLICY_GSS_PIPEFS,
1186 .sp_cops = &gss_sec_pipefs_cops,
1187 .sp_sops = &gss_sec_pipefs_sops,
1191 int __init gss_init_pipefs_upcall(void)
1196 de = rpc_mkdir(LUSTRE_PIPE_ROOT, NULL);
1197 if (IS_ERR(de) && PTR_ERR(de) != -EEXIST) {
1198 CERROR("Failed to create gss pipe dir: %ld\n", PTR_ERR(de));
1202 /* FIXME hack pipefs: dput will sometimes cause oops during module
1203 * unload and lgssd close the pipe fds. */
1205 /* krb5 mechanism */
1206 de = rpc_mkpipe(LUSTRE_PIPE_KRB5, (void *) MECH_KRB5, &gss_upcall_ops,
1207 RPC_PIPE_WAIT_FOR_OPEN);
1208 if (!de || IS_ERR(de)) {
1209 CERROR("failed to make rpc_pipe %s: %ld\n",
1210 LUSTRE_PIPE_KRB5, PTR_ERR(de));
1211 rpc_rmdir(LUSTRE_PIPE_ROOT);
1215 de_pipes[MECH_KRB5] = de;
1216 CFS_INIT_LIST_HEAD(&upcall_lists[MECH_KRB5]);
1217 cfs_spin_lock_init(&upcall_locks[MECH_KRB5]);
1223 void __exit gss_exit_pipefs_upcall(void)
1227 for (i = 0; i < MECH_MAX; i++) {
1228 LASSERT(cfs_list_empty(&upcall_lists[i]));
1230 /* dput pipe dentry here might cause lgssd oops. */
1234 rpc_unlink(LUSTRE_PIPE_KRB5);
1235 rpc_rmdir(LUSTRE_PIPE_ROOT);
1238 int __init gss_init_pipefs(void)
1242 rc = gss_init_pipefs_upcall();
1246 rc = sptlrpc_register_policy(&gss_policy_pipefs);
1248 gss_exit_pipefs_upcall();
1255 void __exit gss_exit_pipefs(void)
1257 gss_exit_pipefs_upcall();
1258 sptlrpc_unregister_policy(&gss_policy_pipefs);