1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_SEC
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
31 #include <liblustre.h>
34 #include <libcfs/kp30.h>
35 #include <linux/obd.h>
36 #include <linux/obd_class.h>
37 #include <linux/obd_support.h>
38 #include <linux/lustre_net.h>
39 #include <linux/lustre_import.h>
40 #include <linux/lustre_dlm.h>
41 #include <linux/lustre_sec.h>
43 static spinlock_t sectypes_lock = SPIN_LOCK_UNLOCKED;
44 static struct ptlrpc_sec_type *sectypes[PTLRPCS_FLVR_MAJOR_MAX] = {
48 int ptlrpcs_register(struct ptlrpc_sec_type *type)
50 __u32 flavor = type->pst_flavor;
52 LASSERT(type->pst_name);
53 LASSERT(type->pst_ops);
55 if (flavor >= PTLRPCS_FLVR_MAJOR_MAX)
58 spin_lock(§ypes_lock);
59 if (sectypes[flavor]) {
60 spin_unlock(§ypes_lock);
63 sectypes[flavor] = type;
64 atomic_set(&type->pst_inst, 0);
65 spin_unlock(§ypes_lock);
67 CWARN("%s: registered\n", type->pst_name);
71 int ptlrpcs_unregister(struct ptlrpc_sec_type *type)
73 __u32 major = type->pst_flavor;
75 LASSERT(major < PTLRPCS_FLVR_MAJOR_MAX);
77 spin_lock(§ypes_lock);
78 if (!sectypes[major]) {
79 spin_unlock(§ypes_lock);
80 CERROR("%s: already unregistered?\n", type->pst_name);
84 LASSERT(sectypes[major] == type);
86 if (atomic_read(&type->pst_inst)) {
87 CERROR("%s: still have %d instances\n",
88 type->pst_name, atomic_read(&type->pst_inst));
89 spin_unlock(§ypes_lock);
93 sectypes[major] = NULL;
94 spin_unlock(§ypes_lock);
96 CDEBUG(D_SEC, "%s: unregistered\n", type->pst_name);
101 struct ptlrpc_sec_type * ptlrpcs_flavor2type(__u32 flavor)
103 struct ptlrpc_sec_type *type;
104 __u32 major = SEC_FLAVOR_MAJOR(flavor);
106 if (major >= PTLRPCS_FLVR_MAJOR_MAX)
109 spin_lock(§ypes_lock);
110 type = sectypes[major];
111 if (type && !try_module_get(type->pst_owner))
113 spin_unlock(§ypes_lock);
118 void ptlrpcs_type_put(struct ptlrpc_sec_type *type)
120 module_put(type->pst_owner);
123 __u32 ptlrpcs_name2flavor(const char *name)
125 if (!strcmp(name, "null"))
126 return PTLRPCS_FLVR_NULL;
127 if (!strcmp(name, "krb5"))
128 return PTLRPCS_FLVR_KRB5;
129 if (!strcmp(name, "krb5i"))
130 return PTLRPCS_FLVR_KRB5I;
131 if (!strcmp(name, "krb5p"))
132 return PTLRPCS_FLVR_KRB5P;
134 return PTLRPCS_FLVR_INVALID;
137 char *ptlrpcs_flavor2name(__u32 flavor)
140 case PTLRPCS_FLVR_NULL:
142 case PTLRPCS_FLVR_KRB5:
144 case PTLRPCS_FLVR_KRB5I:
146 case PTLRPCS_FLVR_KRB5P:
149 CERROR("invalid flavor 0x%x\n", flavor);
154 /***********************************************
155 * credential cache helpers *
156 ***********************************************/
158 void ptlrpcs_init_credcache(struct ptlrpc_sec *sec)
161 for (i = 0; i < PTLRPC_CREDCACHE_NR; i++)
162 INIT_LIST_HEAD(&sec->ps_credcache[i]);
164 /* ps_nextgc == 0 means never do gc */
166 sec->ps_nextgc = get_seconds() + (sec->ps_expire >> 1);
170 * return 1 means we should also destroy the sec structure.
173 static int ptlrpcs_cred_destroy(struct ptlrpc_cred *cred)
175 struct ptlrpc_sec *sec = cred->pc_sec;
178 LASSERT(cred->pc_sec);
179 LASSERT(atomic_read(&cred->pc_refcount) == 0);
180 LASSERT(list_empty(&cred->pc_hash));
182 cred->pc_ops->destroy(cred);
184 /* spinlock to protect against ptlrpcs_sec_put() */
185 LASSERT(atomic_read(&sec->ps_credcount));
186 spin_lock(&sec->ps_lock);
187 if (atomic_dec_and_test(&sec->ps_credcount) &&
188 !atomic_read(&sec->ps_refcount))
190 spin_unlock(&sec->ps_lock);
194 static void ptlrpcs_destroy_credlist(struct list_head *head)
196 struct ptlrpc_cred *cred;
198 while (!list_empty(head)) {
199 cred = list_entry(head->next, struct ptlrpc_cred, pc_hash);
200 list_del_init(&cred->pc_hash);
201 ptlrpcs_cred_destroy(cred);
206 int cred_check_dead(struct ptlrpc_cred *cred,
207 struct list_head *freelist, int removal)
209 /* here we do the exact thing as asked. but an alternative
210 * way is remove dead entries immediately without be asked
211 * remove, since dead entry will not lead to further rpcs.
213 if (unlikely(ptlrpcs_cred_is_dead(cred))) {
214 /* don't try to destroy a busy entry */
215 if (atomic_read(&cred->pc_refcount))
220 /* a busy non-dead entry is considered as "good" one.
221 * Note in a very busy client where cred always busy, we
222 * will not be able to find the expire here, but some other
223 * part will, e.g. checking during refresh, or got error
224 * notification from server, etc. We don't touch busy cred
225 * here is because a busy cred's flag might be changed at
226 * anytime by the owner, we don't want to compete with them.
228 if (atomic_read(&cred->pc_refcount) != 0)
231 /* expire is 0 means never expire. a newly created gss cred
232 * which during upcall also has 0 expiration
234 if (cred->pc_expire == 0)
237 /* check real expiration */
238 if (time_after(cred->pc_expire, get_seconds()))
241 /* although we'v checked the bit right above, there's still
242 * possibility that somebody else set the bit elsewhere.
244 ptlrpcs_cred_expire(cred);
248 LASSERT(atomic_read(&cred->pc_refcount) >= 0);
249 LASSERT(cred->pc_sec);
250 LASSERT(spin_is_locked(&cred->pc_sec->ps_lock));
253 list_move(&cred->pc_hash, freelist);
259 void ptlrpcs_credcache_gc(struct ptlrpc_sec *sec,
260 struct list_head *freelist)
262 struct ptlrpc_cred *cred, *n;
266 CDEBUG(D_SEC, "do gc on sec %s\n", sec->ps_type->pst_name);
267 for (i = 0; i < PTLRPC_CREDCACHE_NR; i++) {
268 list_for_each_entry_safe(cred, n, &sec->ps_credcache[i],
270 cred_check_dead(cred, freelist, 1);
272 sec->ps_nextgc = get_seconds() + sec->ps_expire;
277 * @uid: which user. "-1" means flush all.
278 * @grace: mark cred DEAD, allow graceful destroy like notify
280 * @force: flush all entries, otherwise only free ones be flushed.
283 int flush_credcache(struct ptlrpc_sec *sec, unsigned long pag, uid_t uid,
284 int grace, int force)
286 struct ptlrpc_cred *cred, *n;
291 might_sleep_if(grace);
293 spin_lock(&sec->ps_lock);
294 for (i = 0; i < PTLRPC_CREDCACHE_NR; i++) {
295 list_for_each_entry_safe(cred, n, &sec->ps_credcache[i],
297 LASSERT(atomic_read(&cred->pc_refcount) >= 0);
299 if (sec->ps_flags & PTLRPC_SEC_FL_PAG) {
300 if (pag != -1 && pag != cred->pc_pag)
303 if (uid != -1 && uid != cred->pc_uid)
307 if (atomic_read(&cred->pc_refcount)) {
311 list_del_init(&cred->pc_hash);
312 CDEBUG(D_SEC, "sec %p: flush busy(%d) cred %p "
314 atomic_read(&cred->pc_refcount), cred);
316 list_move(&cred->pc_hash, &freelist);
318 set_bit(PTLRPC_CRED_DEAD_BIT, &cred->pc_flags);
320 clear_bit(PTLRPC_CRED_UPTODATE_BIT,
324 spin_unlock(&sec->ps_lock);
326 ptlrpcs_destroy_credlist(&freelist);
330 /**************************************************
332 **************************************************/
335 int ptlrpcs_cred_get_hash(__u64 pag)
337 LASSERT((pag & PTLRPC_CREDCACHE_MASK) < PTLRPC_CREDCACHE_NR);
338 return (pag & PTLRPC_CREDCACHE_MASK);
342 * return an uptodate or newly created cred entry.
345 struct ptlrpc_cred * cred_cache_lookup(struct ptlrpc_sec *sec,
346 struct vfs_cred *vcred,
347 int create, int remove_dead)
349 struct ptlrpc_cred *cred, *new = NULL, *n;
356 hash = ptlrpcs_cred_get_hash(vcred->vc_pag);
359 spin_lock(&sec->ps_lock);
361 /* do gc if expired */
363 sec->ps_nextgc && time_after(get_seconds(), sec->ps_nextgc))
364 ptlrpcs_credcache_gc(sec, &freelist);
366 list_for_each_entry_safe(cred, n, &sec->ps_credcache[hash], pc_hash) {
367 if (cred_check_dead(cred, &freelist, remove_dead))
369 if (cred->pc_ops->match(cred, vcred)) {
376 if (new && new != cred) {
377 /* lost the race, just free it */
378 list_add(&new->pc_hash, &freelist);
380 list_move(&cred->pc_hash, &sec->ps_credcache[hash]);
383 list_add(&new->pc_hash, &sec->ps_credcache[hash]);
386 spin_unlock(&sec->ps_lock);
387 new = sec->ps_type->pst_ops->create_cred(sec, vcred);
389 atomic_inc(&sec->ps_credcount);
398 atomic_inc(&cred->pc_refcount);
400 spin_unlock(&sec->ps_lock);
402 ptlrpcs_destroy_credlist(&freelist);
406 struct ptlrpc_cred * ptlrpcs_cred_lookup(struct ptlrpc_sec *sec,
407 struct vfs_cred *vcred)
409 struct ptlrpc_cred *cred;
412 cred = cred_cache_lookup(sec, vcred, 0, 1);
416 static struct ptlrpc_cred *get_cred(struct ptlrpc_sec *sec)
418 struct vfs_cred vcred;
422 if (sec->ps_flags & (PTLRPC_SEC_FL_MDS | PTLRPC_SEC_FL_REVERSE)) {
426 if (sec->ps_flags & PTLRPC_SEC_FL_PAG)
427 vcred.vc_pag = (__u64) current->pag;
429 vcred.vc_pag = (__u64) current->uid;
430 vcred.vc_uid = current->uid;
433 return cred_cache_lookup(sec, &vcred, 1, 1);
436 int ptlrpcs_req_get_cred(struct ptlrpc_request *req)
438 struct obd_import *imp = req->rq_import;
441 LASSERT(!req->rq_cred);
444 req->rq_cred = get_cred(imp->imp_sec);
447 CERROR("req %p: fail to get cred from cache\n", req);
455 * check whether current user have valid credential for an import or not.
456 * might repeatedly try in case of non-fatal errors.
457 * return 0 on success, 1 on failure
459 int ptlrpcs_check_cred(struct obd_import *imp)
461 struct ptlrpc_cred *cred;
466 cred = get_cred(imp->imp_sec);
470 if (ptlrpcs_cred_is_uptodate(cred)) {
471 /* get_cred() has done expire checking, so we don't
472 * expect it could expire so quickly, and actually
475 ptlrpcs_cred_put(cred, 1);
479 ptlrpcs_cred_refresh(cred);
480 if (ptlrpcs_cred_is_uptodate(cred)) {
481 ptlrpcs_cred_put(cred, 1);
485 if (cred->pc_flags & PTLRPC_CRED_ERROR ||
486 !imp->imp_replayable) {
487 ptlrpcs_cred_put(cred, 1);
491 ptlrpcs_cred_put(cred, 1);
493 if (signal_pending(current)) {
494 CWARN("%s: interrupted\n", current->comm);
500 static void ptlrpcs_sec_destroy(struct ptlrpc_sec *sec);
502 void ptlrpcs_cred_put(struct ptlrpc_cred *cred, int sync)
504 struct ptlrpc_sec *sec = cred->pc_sec;
507 LASSERT(atomic_read(&cred->pc_refcount));
509 spin_lock(&sec->ps_lock);
511 /* this has to be protected by ps_lock, because cred cache
512 * management code might increase ref against a 0-refed cred.
514 if (!atomic_dec_and_test(&cred->pc_refcount)) {
515 spin_unlock(&sec->ps_lock);
519 /* if sec already unused, we have to destroy the cred (prevent it
520 * hanging there for ever)
522 if (atomic_read(&sec->ps_refcount) == 0) {
523 if (!test_and_set_bit(PTLRPC_CRED_DEAD_BIT, &cred->pc_flags))
524 CWARN("cred %p: force expire on a unused sec\n", cred);
525 list_del_init(&cred->pc_hash);
526 } else if (unlikely(sync && ptlrpcs_cred_is_dead(cred)))
527 list_del_init(&cred->pc_hash);
529 if (!list_empty(&cred->pc_hash)) {
530 spin_unlock(&sec->ps_lock);
534 /* if required async, and we reached here, we have to clear
535 * the UPTODATE bit, thus no rpc is needed in destroy procedure.
538 clear_bit(PTLRPC_CRED_UPTODATE_BIT, &cred->pc_flags);
540 spin_unlock(&sec->ps_lock);
542 /* destroy this cred */
543 if (!ptlrpcs_cred_destroy(cred))
546 LASSERT(!atomic_read(&sec->ps_credcount));
547 LASSERT(!atomic_read(&sec->ps_refcount));
549 CWARN("sec %p(%s), put last cred, also destroy the sec\n",
550 sec, sec->ps_type->pst_name);
553 void ptlrpcs_req_drop_cred(struct ptlrpc_request *req)
558 LASSERT(req->rq_cred);
561 /* this could be called with spinlock hold, use async mode */
562 ptlrpcs_cred_put(req->rq_cred, 0);
565 CDEBUG(D_SEC, "req %p have no cred\n", req);
570 * request must have a cred. if failed to get new cred,
571 * just restore the old one
573 int ptlrpcs_req_replace_dead_cred(struct ptlrpc_request *req)
575 struct ptlrpc_cred *cred = req->rq_cred;
580 LASSERT(test_bit(PTLRPC_CRED_DEAD_BIT, &cred->pc_flags));
582 ptlrpcs_cred_get(cred);
583 ptlrpcs_req_drop_cred(req);
584 LASSERT(!req->rq_cred);
585 rc = ptlrpcs_req_get_cred(req);
587 LASSERT(req->rq_cred);
588 LASSERT(req->rq_cred != cred);
589 ptlrpcs_cred_put(cred, 1);
591 LASSERT(!req->rq_cred);
598 * since there's no lock on the cred, its status could be changed
599 * by other threads at any time, we allow this race. If an uptodate
600 * cred turn to dead quickly under us, we don't know and continue
601 * using it, that's fine. if necessary the later error handling code
604 int ptlrpcs_req_refresh_cred(struct ptlrpc_request *req)
606 struct ptlrpc_cred *cred = req->rq_cred;
611 if (!ptlrpcs_cred_check_uptodate(cred))
614 if (test_bit(PTLRPC_CRED_ERROR_BIT, &cred->pc_flags)) {
615 req->rq_ptlrpcs_err = 1;
619 if (test_bit(PTLRPC_CRED_DEAD_BIT, &cred->pc_flags)) {
620 if (ptlrpcs_req_replace_dead_cred(req) == 0) {
621 LASSERT(cred != req->rq_cred);
622 CDEBUG(D_SEC, "req %p: replace cred %p => %p\n",
623 req, cred, req->rq_cred);
626 LASSERT(cred == req->rq_cred);
627 CERROR("req %p: failed to replace dead cred %p\n",
629 req->rq_ptlrpcs_err = 1;
634 ptlrpcs_cred_refresh(cred);
636 if (!ptlrpcs_cred_is_uptodate(cred)) {
637 if (test_bit(PTLRPC_CRED_ERROR_BIT, &cred->pc_flags))
638 req->rq_ptlrpcs_err = 1;
640 CERROR("req %p: failed to refresh cred %p, fatal %d\n",
641 req, cred, req->rq_ptlrpcs_err);
647 int ptlrpcs_cli_wrap_request(struct ptlrpc_request *req)
649 struct ptlrpc_cred *cred;
653 LASSERT(req->rq_cred);
654 LASSERT(req->rq_cred->pc_sec);
655 LASSERT(req->rq_cred->pc_ops);
656 LASSERT(req->rq_reqbuf);
657 LASSERT(req->rq_reqbuf_len);
659 rc = ptlrpcs_req_refresh_cred(req);
663 CDEBUG(D_SEC, "wrap req %p\n", req);
666 switch (SEC_FLAVOR_SVC(req->rq_req_secflvr)) {
667 case PTLRPCS_SVC_NONE:
668 case PTLRPCS_SVC_AUTH:
669 if (req->rq_req_wrapped) {
670 CDEBUG(D_SEC, "req %p(o%u,x"LPU64",t"LPU64") "
671 "already signed, resend?\n", req,
672 req->rq_reqmsg ? req->rq_reqmsg->opc : -1,
673 req->rq_xid, req->rq_transno);
674 req->rq_req_wrapped = 0;
675 req->rq_reqdata_len = sizeof(struct ptlrpcs_wire_hdr) +
677 LASSERT(req->rq_reqdata_len % 8 == 0);
680 LASSERT(cred->pc_ops->sign);
681 rc = cred->pc_ops->sign(cred, req);
683 req->rq_req_wrapped = 1;
685 case PTLRPCS_SVC_PRIV:
686 if (req->rq_req_wrapped) {
687 CDEBUG(D_SEC, "req %p(o%u,x"LPU64",t"LPU64") "
688 "already encrypted, resend?\n", req,
689 req->rq_reqmsg ? req->rq_reqmsg->opc : -1,
690 req->rq_xid, req->rq_transno);
691 req->rq_req_wrapped = 0;
692 req->rq_reqdata_len = sizeof(struct ptlrpcs_wire_hdr);
693 LASSERT(req->rq_reqdata_len % 8 == 0);
696 LASSERT(cred->pc_ops->seal);
697 rc = cred->pc_ops->seal(cred, req);
699 req->rq_req_wrapped = 1;
704 LASSERT(req->rq_reqdata_len);
705 LASSERT(req->rq_reqdata_len % 8 == 0);
706 LASSERT(req->rq_reqdata_len >= sizeof(struct ptlrpcs_wire_hdr));
707 LASSERT(req->rq_reqdata_len <= req->rq_reqbuf_len);
712 /* rq_nob_received is the actual received data length */
713 int ptlrpcs_cli_unwrap_reply(struct ptlrpc_request *req)
715 struct ptlrpc_cred *cred = req->rq_cred;
716 struct ptlrpc_sec *sec;
717 struct ptlrpcs_wire_hdr *sec_hdr;
722 LASSERT(cred->pc_sec);
723 LASSERT(cred->pc_ops);
724 LASSERT(req->rq_repbuf);
726 if (req->rq_nob_received < sizeof(*sec_hdr)) {
727 CERROR("req %p: reply size only %d\n",
728 req, req->rq_nob_received);
732 sec_hdr = (struct ptlrpcs_wire_hdr *) req->rq_repbuf;
733 sec_hdr->flavor = le32_to_cpu(sec_hdr->flavor);
734 sec_hdr->msg_len = le32_to_cpu(sec_hdr->msg_len);
735 sec_hdr->sec_len = le32_to_cpu(sec_hdr->sec_len);
737 CDEBUG(D_SEC, "req %p, cred %p, flavor 0x%x\n",
738 req, cred, sec_hdr->flavor);
742 /* only compare major flavor, reply might use different subflavor.
744 if (SEC_FLAVOR_MAJOR(sec_hdr->flavor) !=
745 SEC_FLAVOR_MAJOR(req->rq_req_secflvr)) {
746 CERROR("got major flavor %u while expect %u\n",
747 SEC_FLAVOR_MAJOR(sec_hdr->flavor),
748 SEC_FLAVOR_MAJOR(req->rq_req_secflvr));
752 if (sizeof(*sec_hdr) + sec_hdr->msg_len + sec_hdr->sec_len >
753 req->rq_nob_received) {
754 CERROR("msg %u, sec %u, while only get %d\n",
755 sec_hdr->msg_len, sec_hdr->sec_len,
756 req->rq_nob_received);
760 switch (SEC_FLAVOR_SVC(sec_hdr->flavor)) {
761 case PTLRPCS_SVC_NONE:
762 case PTLRPCS_SVC_AUTH: {
763 LASSERT(cred->pc_ops->verify);
764 rc = cred->pc_ops->verify(cred, req);
765 LASSERT(rc || req->rq_repmsg || req->rq_ptlrpcs_restart);
767 case PTLRPCS_SVC_PRIV:
768 LASSERT(cred->pc_ops->unseal);
769 rc = cred->pc_ops->unseal(cred, req);
770 LASSERT(rc || req->rq_repmsg || req->rq_ptlrpcs_restart);
780 /**************************************************
782 **************************************************/
784 struct ptlrpc_sec * ptlrpcs_sec_create(__u32 flavor,
786 struct obd_import *import,
787 const char *pipe_dir,
790 struct ptlrpc_sec_type *type;
791 struct ptlrpc_sec *sec;
794 type = ptlrpcs_flavor2type(flavor);
796 CERROR("invalid flavor 0x%x\n", flavor);
800 sec = type->pst_ops->create_sec(flavor, pipe_dir, pipe_data);
802 spin_lock_init(&sec->ps_lock);
803 ptlrpcs_init_credcache(sec);
805 sec->ps_flavor = flavor;
806 sec->ps_flags = flags;
807 sec->ps_import = class_import_get(import);
808 atomic_set(&sec->ps_refcount, 1);
809 atomic_set(&sec->ps_credcount, 0);
810 atomic_inc(&type->pst_inst);
812 ptlrpcs_type_put(type);
817 static void ptlrpcs_sec_destroy(struct ptlrpc_sec *sec)
819 struct ptlrpc_sec_type *type = sec->ps_type;
820 struct obd_import *imp = sec->ps_import;
822 LASSERT(type && type->pst_ops);
823 LASSERT(type->pst_ops->destroy_sec);
825 type->pst_ops->destroy_sec(sec);
826 atomic_dec(&type->pst_inst);
827 ptlrpcs_type_put(type);
828 class_import_put(imp);
831 void ptlrpcs_sec_put(struct ptlrpc_sec *sec)
835 if (atomic_dec_and_test(&sec->ps_refcount)) {
836 flush_credcache(sec, -1, -1, 1, 1);
838 /* this spinlock is protect against ptlrpcs_cred_destroy() */
839 spin_lock(&sec->ps_lock);
840 ncred = atomic_read(&sec->ps_credcount);
841 spin_unlock(&sec->ps_lock);
844 ptlrpcs_sec_destroy(sec);
846 CWARN("%s %p is no usage while %d cred still "
847 "holded, destroy delayed\n",
848 sec->ps_type->pst_name, sec,
849 atomic_read(&sec->ps_credcount));
854 void ptlrpcs_sec_invalidate_cache(struct ptlrpc_sec *sec)
856 flush_credcache(sec, -1, -1, 0, 1);
859 int sec_alloc_reqbuf(struct ptlrpc_sec *sec,
860 struct ptlrpc_request *req,
861 int msgsize, int secsize)
863 struct ptlrpcs_wire_hdr *hdr;
866 LASSERT(msgsize % 8 == 0);
867 LASSERT(secsize % 8 == 0);
869 req->rq_reqbuf_len = sizeof(*hdr) + msgsize + secsize;
870 OBD_ALLOC(req->rq_reqbuf, req->rq_reqbuf_len);
871 if (!req->rq_reqbuf) {
872 CERROR("can't alloc %d\n", req->rq_reqbuf_len);
876 hdr = buf_to_sec_hdr(req->rq_reqbuf);
877 hdr->flavor = cpu_to_le32(req->rq_req_secflvr);
878 hdr->msg_len = msgsize;
879 /* security length will be filled later */
881 /* later reqdata_len will be added on actual security payload */
882 req->rq_reqdata_len = sizeof(*hdr) + msgsize;
883 req->rq_reqmsg = buf_to_lustre_msg(req->rq_reqbuf);
885 CDEBUG(D_SEC, "req %p: rqbuf at %p, len %d, msg %d, sec %d\n",
886 req, req->rq_reqbuf, req->rq_reqbuf_len,
892 /* when complete successfully, req->rq_reqmsg should point to the
895 int ptlrpcs_cli_alloc_reqbuf(struct ptlrpc_request *req, int msgsize)
897 struct ptlrpc_cred *cred = req->rq_cred;
898 struct ptlrpc_sec *sec;
899 struct ptlrpc_secops *ops;
901 LASSERT(msgsize % 8 == 0);
902 LASSERT(sizeof(struct ptlrpcs_wire_hdr) % 8 == 0);
904 LASSERT(atomic_read(&cred->pc_refcount));
905 LASSERT(cred->pc_sec);
906 LASSERT(cred->pc_sec->ps_type);
907 LASSERT(cred->pc_sec->ps_type->pst_ops);
908 LASSERT(req->rq_reqbuf == NULL);
909 LASSERT(req->rq_reqmsg == NULL);
912 ops = sec->ps_type->pst_ops;
913 if (ops->alloc_reqbuf)
914 return ops->alloc_reqbuf(sec, req, msgsize);
916 return sec_alloc_reqbuf(sec, req, msgsize, 0);
919 void sec_free_reqbuf(struct ptlrpc_sec *sec,
920 struct ptlrpc_request *req)
922 LASSERT(req->rq_reqbuf);
923 LASSERT(req->rq_reqbuf_len);
926 if (req->rq_reqmsg) {
927 LASSERT((char *) req->rq_reqmsg >= req->rq_reqbuf &&
928 (char *) req->rq_reqmsg < req->rq_reqbuf +
932 OBD_FREE(req->rq_reqbuf, req->rq_reqbuf_len);
933 req->rq_reqbuf = NULL;
934 req->rq_reqmsg = NULL;
937 void ptlrpcs_cli_free_reqbuf(struct ptlrpc_request *req)
939 struct ptlrpc_cred *cred = req->rq_cred;
940 struct ptlrpc_sec *sec;
941 struct ptlrpc_secops *ops;
944 LASSERT(atomic_read(&cred->pc_refcount));
945 LASSERT(cred->pc_sec);
946 LASSERT(cred->pc_sec->ps_type);
947 LASSERT(cred->pc_sec->ps_type->pst_ops);
948 LASSERT(req->rq_reqbuf);
951 ops = sec->ps_type->pst_ops;
952 if (ops->free_reqbuf)
953 ops->free_reqbuf(sec, req);
955 sec_free_reqbuf(sec, req);
958 int ptlrpcs_cli_alloc_repbuf(struct ptlrpc_request *req, int msgsize)
960 struct ptlrpc_cred *cred = req->rq_cred;
961 struct ptlrpc_sec *sec;
962 struct ptlrpc_secops *ops;
963 int msg_payload, sec_payload;
966 LASSERT(msgsize % 8 == 0);
967 LASSERT(sizeof(struct ptlrpcs_wire_hdr) % 8 == 0);
969 LASSERT(atomic_read(&cred->pc_refcount));
970 LASSERT(cred->pc_sec);
971 LASSERT(cred->pc_sec->ps_type);
972 LASSERT(cred->pc_sec->ps_type->pst_ops);
973 LASSERT(req->rq_repbuf == NULL);
976 ops = sec->ps_type->pst_ops;
977 if (ops->alloc_repbuf)
978 RETURN(ops->alloc_repbuf(sec, req, msgsize));
980 /* default allocation scheme */
981 msg_payload = SEC_FLAVOR_SVC(req->rq_req_secflvr) == PTLRPCS_SVC_PRIV ?
983 sec_payload = size_round(ptlrpcs_est_rep_payload(req, msgsize));
985 req->rq_repbuf_len = sizeof(struct ptlrpcs_wire_hdr) +
986 msg_payload + sec_payload;
987 OBD_ALLOC(req->rq_repbuf, req->rq_repbuf_len);
991 CDEBUG(D_SEC, "req %p: repbuf at %p, len %d, msg %d, sec %d\n",
992 req, req->rq_repbuf, req->rq_repbuf_len,
993 msg_payload, sec_payload);
998 void ptlrpcs_cli_free_repbuf(struct ptlrpc_request *req)
1000 struct ptlrpc_cred *cred = req->rq_cred;
1001 struct ptlrpc_sec *sec;
1002 struct ptlrpc_secops *ops;
1006 LASSERT(atomic_read(&cred->pc_refcount));
1007 LASSERT(cred->pc_sec);
1008 LASSERT(cred->pc_sec->ps_type);
1009 LASSERT(cred->pc_sec->ps_type->pst_ops);
1010 LASSERT(req->rq_repbuf);
1013 ops = sec->ps_type->pst_ops;
1014 if (ops->free_repbuf)
1015 ops->free_repbuf(sec, req);
1017 OBD_FREE(req->rq_repbuf, req->rq_repbuf_len);
1018 req->rq_repbuf = NULL;
1019 req->rq_repmsg = NULL;
1024 int ptlrpcs_import_get_sec(struct obd_import *imp)
1026 __u32 flavor = PTLRPCS_FLVR_NULL;
1027 unsigned long flags = 0;
1028 char *pipedir = NULL;
1031 LASSERT(imp->imp_obd);
1032 LASSERT(imp->imp_obd->obd_type);
1034 /* old sec might be still there in reconnecting */
1038 /* find actual flavor for client obd. right now server side
1039 * obd (reverse imp, etc) will simply use NULL. */
1040 if (!strcmp(imp->imp_obd->obd_type->typ_name, OBD_MDC_DEVICENAME) ||
1041 !strcmp(imp->imp_obd->obd_type->typ_name, OBD_OSC_DEVICENAME)) {
1042 struct client_obd *cli = &imp->imp_obd->u.cli;
1044 switch (SEC_FLAVOR_MAJOR(cli->cl_sec_flavor)) {
1045 case PTLRPCS_FLVR_MAJOR_NULL:
1046 CWARN("select security null for %s(%s)\n",
1047 imp->imp_obd->obd_type->typ_name,
1048 imp->imp_obd->obd_name);
1050 case PTLRPCS_FLVR_MAJOR_GSS:
1051 CWARN("select security %s for %s(%s)\n",
1052 ptlrpcs_flavor2name(cli->cl_sec_flavor),
1053 imp->imp_obd->obd_type->typ_name,
1054 imp->imp_obd->obd_name);
1055 flavor = cli->cl_sec_flavor;
1056 pipedir = imp->imp_obd->obd_name;
1059 CWARN("unknown security flavor for %s(%s), "
1061 imp->imp_obd->obd_type->typ_name,
1062 imp->imp_obd->obd_name);
1065 flags = cli->cl_sec_flags;
1068 imp->imp_sec = ptlrpcs_sec_create(flavor, flags, imp, pipedir, imp);
1075 void ptlrpcs_import_drop_sec(struct obd_import *imp)
1079 ptlrpcs_sec_put(imp->imp_sec);
1080 imp->imp_sec = NULL;
1085 void ptlrpcs_import_flush_current_creds(struct obd_import *imp)
1089 class_import_get(imp);
1091 flush_credcache(imp->imp_sec, current->pag, current->uid, 1, 1);
1092 class_import_put(imp);
1095 int __init ptlrpc_sec_init(void)
1099 if ((rc = ptlrpcs_null_init()))
1102 if ((rc = svcsec_null_init())) {
1103 ptlrpcs_null_exit();
1108 #if !defined __KERNEL__ && defined ENABLE_GSS
1115 #if defined __KERNEL__ && defined ENABLE_GSS
1116 static void __exit ptlrpc_sec_exit(void)
1119 ptlrpcs_null_exit();
1123 EXPORT_SYMBOL(ptlrpcs_register);
1124 EXPORT_SYMBOL(ptlrpcs_unregister);
1125 EXPORT_SYMBOL(ptlrpcs_sec_create);
1126 EXPORT_SYMBOL(ptlrpcs_sec_put);
1127 EXPORT_SYMBOL(ptlrpcs_sec_invalidate_cache);
1128 EXPORT_SYMBOL(ptlrpcs_import_get_sec);
1129 EXPORT_SYMBOL(ptlrpcs_import_drop_sec);
1130 EXPORT_SYMBOL(ptlrpcs_import_flush_current_creds);
1131 EXPORT_SYMBOL(ptlrpcs_cred_lookup);
1132 EXPORT_SYMBOL(ptlrpcs_cred_put);
1133 EXPORT_SYMBOL(ptlrpcs_req_get_cred);
1134 EXPORT_SYMBOL(ptlrpcs_req_drop_cred);
1135 EXPORT_SYMBOL(ptlrpcs_req_replace_dead_cred);
1136 EXPORT_SYMBOL(ptlrpcs_req_refresh_cred);
1137 EXPORT_SYMBOL(ptlrpcs_check_cred);
1138 EXPORT_SYMBOL(ptlrpcs_cli_alloc_reqbuf);
1139 EXPORT_SYMBOL(ptlrpcs_cli_free_reqbuf);
1140 EXPORT_SYMBOL(ptlrpcs_cli_alloc_repbuf);
1141 EXPORT_SYMBOL(ptlrpcs_cli_free_repbuf);
1142 EXPORT_SYMBOL(ptlrpcs_cli_wrap_request);
1143 EXPORT_SYMBOL(ptlrpcs_cli_unwrap_reply);
1144 EXPORT_SYMBOL(sec_alloc_reqbuf);
1145 EXPORT_SYMBOL(sec_free_reqbuf);
1147 EXPORT_SYMBOL(svcsec_register);
1148 EXPORT_SYMBOL(svcsec_unregister);
1149 EXPORT_SYMBOL(svcsec_accept);
1150 EXPORT_SYMBOL(svcsec_authorize);
1151 EXPORT_SYMBOL(svcsec_alloc_repbuf);
1152 EXPORT_SYMBOL(svcsec_cleanup_req);
1153 EXPORT_SYMBOL(svcsec_get);
1154 EXPORT_SYMBOL(svcsec_put);
1155 EXPORT_SYMBOL(svcsec_alloc_reply_state);
1156 EXPORT_SYMBOL(svcsec_free_reply_state);
1158 EXPORT_SYMBOL(ptlrpcs_name2flavor);
1159 EXPORT_SYMBOL(ptlrpcs_flavor2name);
1161 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1162 MODULE_DESCRIPTION("Lustre Security Support");
1163 MODULE_LICENSE("GPL");
1165 module_init(ptlrpc_sec_init);
1166 module_exit(ptlrpc_sec_exit);